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