OpenSDN source code
inet6_route.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "base/misc_utils.h"
7 #include "base/string_util.h"
10 
11 using boost::system::error_code;
12 using std::copy;
13 using std::string;
14 using std::vector;
15 
17  Inet6Prefix *prefix) {
18  if (proto_prefix.prefix.size() > Address::kMaxV6Bytes)
19  return -1;
20  prefix->prefixlen_ = proto_prefix.prefixlen;
21  Ip6Address::bytes_type bt = { { 0 } };
22  copy(proto_prefix.prefix.begin(), proto_prefix.prefix.end(), bt.begin());
23  prefix->ip6_addr_ = Ip6Address(bt);
24 
25  return 0;
26 }
27 
29  const BgpProtoPrefix &proto_prefix,
30  const BgpAttr *attr,
31  const Address::Family family,
32  Inet6Prefix *prefix, BgpAttrPtr *new_attr,
33  uint32_t *label, uint32_t *l3_label) {
34  return FromProtoPrefix(proto_prefix, prefix);
35 }
36 
37 string Inet6Prefix::ToString() const {
38  string repr(ip6_addr().to_string());
39  repr.append("/" + integerToString(prefixlen()));
40  return repr;
41 }
42 
43 int Inet6Prefix::CompareTo(const Inet6Prefix &rhs) const {
44  if (ip6_addr_ < rhs.ip6_addr_) {
45  return -1;
46  }
47  if (ip6_addr_ > rhs.ip6_addr_) {
48  return 1;
49  }
50  if (prefixlen_ < rhs.prefixlen_) {
51  return -1;
52  }
53  if (prefixlen_ > rhs.prefixlen_) {
54  return 1;
55  }
56  return 0;
57 }
58 
59 Inet6Prefix Inet6Prefix::FromString(const string &str, error_code *error) {
60  Inet6Prefix prefix;
61  error_code pfxerr = Inet6SubnetParse(str, &prefix.ip6_addr_,
62  &prefix.prefixlen_);
63  if (error != NULL) {
64  *error = pfxerr;
65  }
66  return prefix;
67 }
68 
69 // Check whether 'this' is more specific than rhs.
70 bool Inet6Prefix::IsMoreSpecific(const Inet6Prefix &rhs) const {
71  // My prefixlen must be longer in order to be more specific.
72  if (prefixlen_ < rhs.prefixlen()) {
73  return false;
74  }
76  Inet6Prefix left = operator&(mask);
77  Inet6Prefix right = rhs.operator&(mask);
78 
79  return (left.ToBytes() == right.ToBytes());
80 }
81 
83  Ip6Address::bytes_type addr_bytes;
84  ARRAYBYTES_FILL(addr_bytes,0);
85 
86  Ip6Address::bytes_type lhs = ToBytes();
87  Ip6Address::bytes_type rhs = right.ToBytes();
88 
89  int plen = (prefixlen_ <= right.prefixlen_ ? prefixlen() : right.prefixlen());
90 
91  for (size_t i = 0; i < sizeof(Ip6Address::bytes_type); ++i) {
92  addr_bytes[i] = lhs[i] & rhs[i];
93  }
94 
95  return Inet6Prefix(Ip6Address(addr_bytes), plen);
96 }
97 
98 // Routines for class Inet6Route
99 
101  : prefix_(prefix),
102  prefix_str_(prefix.ToString()) {
103 }
104 
105 int Inet6Route::CompareTo(const Route &rhs) const {
106  const Inet6Route &rt_other = static_cast<const Inet6Route &>(rhs);
107  return prefix_.CompareTo(rt_other.prefix_);
108 }
109 
110 // Check whether 'this' is more specific than rhs.
111 bool Inet6Route::IsMoreSpecific(const string &match) const {
112  error_code ec;
113 
114  Inet6Prefix prefix = Inet6Prefix::FromString(match, &ec);
115  if (!ec) {
116  return GetPrefix().IsMoreSpecific(prefix);
117  }
118 
119  return false;
120 }
121 
122 // Check whether 'this' is less specific than rhs.
123 bool Inet6Route::IsLessSpecific(const string &match) const {
124  error_code ec;
125 
126  Inet6Prefix prefix = Inet6Prefix::FromString(match, &ec);
127  if (!ec) {
128  return prefix.IsMoreSpecific(GetPrefix());
129  }
130 
131  return false;
132 }
133 
136  return KeyPtr(key);
137 }
138 
139 void Inet6Route::SetKey(const DBRequestKey *reqkey) {
140  const Inet6Table::RequestKey *key =
141  static_cast<const Inet6Table::RequestKey *>(reqkey);
142  prefix_ = key->prefix;
143 }
144 
146  const BgpAttr *attr,
147  uint32_t label,
148  uint32_t l3_label) const {
149  prefix->prefixlen = prefix_.prefixlen();
150  prefix->prefix.clear();
151  const Ip6Address::bytes_type &addr_bytes = prefix_.ip6_addr().to_bytes();
152  int num_bytes = (prefix->prefixlen + 7) / 8;
153  copy(addr_bytes.begin(), addr_bytes.begin() + num_bytes,
154  back_inserter(prefix->prefix));
155 }
156 
157 //
158 // Fill in the vector based on the supplied nexthop.
159 //
160 void Inet6Route::BuildBgpProtoNextHop(vector<uint8_t> &nh,
161  IpAddress nexthop) const {
162  nh.resize(Address::kMaxV6Bytes);
163  Ip6Address address;
164  if (nexthop.is_v4()) {
165  address = Ip6Address::v4_mapped(nexthop.to_v4());
166  } else {
167  address = nexthop.to_v6();
168  }
169 
170  const Ip6Address::bytes_type &addr_bytes = address.to_bytes();
171  copy(addr_bytes.begin(), addr_bytes.end(), nh.begin());
172 }
173 
174 // Routines for class Inet6Masks
175 
176 // Definitions of the static members
177 bool Inet6Masks::initialized_ = false;
178 vector<Inet6Prefix> Inet6Masks::masks_;
179 
180 const Inet6Prefix& Inet6Masks::PrefixlenToMask(uint8_t prefix_len) {
181  assert(prefix_len <= Inet6Prefix::kMaxV6PrefixLen);
182  return masks_.at(prefix_len);
183 }
184 
186  assert(initialized_ == false);
187  for (int i = 0; i <= Inet6Prefix::kMaxV6PrefixLen; ++i) {
188  masks_.push_back(CalculateMaskFromPrefixlen(i));
189  }
190  initialized_ = true;
191 }
192 
194  masks_.clear();
195  initialized_ = false;
196 }
197 
199  int num_bytes = prefixlen / 8;
200  int num_bits = prefixlen % 8;
201 
202  Ip6Address::bytes_type addr_bytes;
203  ARRAYBYTES_FILL(addr_bytes,0);
204 
205  for (int i = 0; i < num_bytes; ++i) {
206  addr_bytes[i] = 0xff;
207  }
208  if (num_bits) {
209  uint8_t hex_val = 0xff << (8 - num_bits);
210  addr_bytes[num_bytes] = hex_val;
211  }
212  return Inet6Prefix(Ip6Address(addr_bytes), prefixlen);
213 }
214 
215 static void Inet6InitRoutines() {
217 }
boost::system::error_code Inet6SubnetParse(const string &str, Ip6Address *addr, int *plen)
Definition: address.cc:163
boost::asio::ip::address_v6 Ip6Address
Definition: address.h:16
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:999
Family
Definition: address.h:25
static const uint8_t kMaxV6Bytes
Definition: address.h:22
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
static std::vector< Inet6Prefix > masks_
Definition: inet6_route.h:117
static const Inet6Prefix & PrefixlenToMask(uint8_t prefix_len)
Definition: inet6_route.cc:180
static Inet6Prefix CalculateMaskFromPrefixlen(int prefixlen)
Definition: inet6_route.cc:198
static void Clear()
Definition: inet6_route.cc:193
static bool initialized_
Definition: inet6_route.h:118
static void Init()
Definition: inet6_route.cc:185
static Inet6Prefix FromString(const std::string &str, boost::system::error_code *errorp=NULL)
Definition: inet6_route.cc:59
Inet6Prefix operator&(const Inet6Prefix &rhs) const
Definition: inet6_route.cc:82
std::string ToString() const
Definition: inet6_route.cc:37
Ip6Address ip6_addr() const
Definition: inet6_route.h:28
Ip6Address ip6_addr_
Definition: inet6_route.h:71
static const uint8_t kMaxV6PrefixLen
Definition: inet6_route.h:19
int prefixlen() const
Definition: inet6_route.h:34
int CompareTo(const Inet6Prefix &rhs) const
Definition: inet6_route.cc:43
const Ip6Address::bytes_type ToBytes() const
Definition: inet6_route.h:30
static int FromProtoPrefix(const BgpProtoPrefix &proto_prefix, Inet6Prefix *prefix)
Definition: inet6_route.cc:16
bool IsMoreSpecific(const Inet6Prefix &rhs) const
Definition: inet6_route.cc:70
int prefixlen_
Definition: inet6_route.h:72
Inet6Route(const Inet6Prefix &prefix)
Definition: inet6_route.cc:100
virtual bool IsLessSpecific(const std::string &match) const
Definition: inet6_route.cc:123
virtual void BuildProtoPrefix(BgpProtoPrefix *prefix, const BgpAttr *attr=NULL, uint32_t label=0, uint32_t l3_label=0) const
Definition: inet6_route.cc:145
virtual void BuildBgpProtoNextHop(std::vector< uint8_t > &nh, IpAddress nexthop) const
Definition: inet6_route.cc:160
const Inet6Prefix & GetPrefix() const
Definition: inet6_route.h:81
virtual bool IsMoreSpecific(const std::string &match) const
Definition: inet6_route.cc:111
virtual int CompareTo(const Route &rhs) const
Definition: inet6_route.cc:105
virtual void SetKey(const DBRequestKey *reqkey)
Definition: inet6_route.cc:139
Inet6Prefix prefix_
Definition: inet6_route.h:103
virtual KeyPtr GetDBRequestKey() const
Definition: inet6_route.cc:134
Definition: route.h:15
static void Inet6InitRoutines()
Definition: inet6_route.cc:215
MODULE_INITIALIZER(Inet6InitRoutines)
#define ARRAYBYTES_FILL(obj, val)
Definition: misc_utils.h:32
static string ToString(PhysicalDevice::ManagementProtocol proto)
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
std::vector< uint8_t > prefix