OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inetvpn_route.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include <algorithm>
8 
10 
11 using std::copy;
12 using std::string;
13 using std::vector;
14 
16  : prefix_(prefix) {
17 }
18 
19 int InetVpnRoute::CompareTo(const Route &rhs) const {
20  const InetVpnRoute &other = static_cast<const InetVpnRoute &>(rhs);
23  if (res != 0) {
24  return res;
25  }
26  Ip4Address laddr = prefix_.addr();
27  Ip4Address raddr = other.prefix_.addr();
28  if (laddr < raddr) {
29  return -1;
30  }
31  if (laddr > raddr) {
32  return 1;
33  }
34  if (prefix_.prefixlen() < other.prefix_.prefixlen()) {
35  return -1;
36  }
37  if (prefix_.prefixlen() > other.prefix_.prefixlen()) {
38  return 1;
39  }
40  return 0;
41 }
42 
43 string InetVpnRoute::ToString() const {
44  string repr = prefix_.route_distinguisher().ToString() + ":";
45  repr += prefix_.addr().to_string();
46  char strplen[4];
47  snprintf(strplen, sizeof(strplen), "/%d", prefix_.prefixlen());
48  repr.append(strplen);
49 
50  return repr;
51 }
52 
53 void InetVpnRoute::SetKey(const DBRequestKey *reqkey) {
54  const InetVpnTable::RequestKey *key =
55  static_cast<const InetVpnTable::RequestKey *>(reqkey);
56  prefix_ = key->prefix;
57 }
58 
60  const BgpAttr *attr,
61  uint32_t label,
62  uint32_t l3_label) const {
63  prefix_.BuildProtoPrefix(label, prefix, attr);
64 }
65 
66 void InetVpnRoute::BuildBgpProtoNextHop(vector<uint8_t> &nh,
67  IpAddress nexthop) const {
68  nh.resize(4+RouteDistinguisher::kSize);
69  const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes();
70  copy(addr_bytes.begin(), addr_bytes.end(),
71  nh.begin() + RouteDistinguisher::kSize);
72 }
73 
77  return KeyPtr(key);
78 }
79 
80 // Check whether 'this' is more specific than rhs.
81 bool InetVpnRoute::IsMoreSpecific(const string &match) const {
82  boost::system::error_code ec;
83 
84  InetVpnPrefix prefix = InetVpnPrefix::FromString(match, &ec);
85  if (!ec) {
86  return GetPrefix().IsMoreSpecific(prefix);
87  }
88 
89  return false;
90 }
91 
92 // Check whether 'this' is less specific than rhs.
93 bool InetVpnRoute::IsLessSpecific(const string &match) const {
94  boost::system::error_code ec;
95 
96  InetVpnPrefix prefix = InetVpnPrefix::FromString(match, &ec);
97  if (!ec) {
98  return prefix.IsMoreSpecific(GetPrefix());
99  }
100 
101  return false;
102 }
Ip4Address addr() const
boost::asio::ip::address IpAddress
Definition: address.h:13
Definition: route.h:14
const InetVpnPrefix & GetPrefix() const
Definition: inetvpn_route.h:27
const RouteDistinguisher & route_distinguisher() const
InetVpnPrefix prefix_
Definition: inetvpn_route.h:53
static InetVpnPrefix FromString(const std::string &str, boost::system::error_code *errorp=NULL)
int CompareTo(const RouteDistinguisher &rhs) const
Definition: rd.cc:157
virtual void BuildProtoPrefix(BgpProtoPrefix *prefix, const BgpAttr *attr=NULL, uint32_t label=0, uint32_t l3_label=0) const
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
bool IsMoreSpecific(const InetVpnPrefix &rhs) const
virtual void SetKey(const DBRequestKey *reqkey)
static const size_t kSize
Definition: rd.h:13
virtual std::string ToString() const
virtual bool IsLessSpecific(const std::string &match) const
virtual int CompareTo(const Route &rhs) const
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
virtual void BuildBgpProtoNextHop(std::vector< uint8_t > &nh, IpAddress nexthop) const
std::string ToString() const
Definition: rd.cc:56
virtual bool IsMoreSpecific(const std::string &match) const
int prefixlen() const
void BuildProtoPrefix(uint32_t label, BgpProtoPrefix *proto_prefix, const BgpAttr *attr=NULL) const
virtual KeyPtr GetDBRequestKey() const
InetVpnRoute(const InetVpnPrefix &prefix)