libbgp  0.6
A C++ BGP Library.
prefix4.cc
Go to the documentation of this file.
1 
11 #include "prefix4.h"
12 #include "value-op.h"
13 #include <arpa/inet.h>
14 
15 namespace libbgp {
16 
17 const uint32_t CIDR_MASK_MAP[33] = {
18  0x00000000, 0x00000080, 0x000000c0, 0x000000e0, 0x000000f0, 0x000000f8,
19  0x000000fc, 0x000000fe, 0x000000ff, 0x000080ff, 0x0000c0ff, 0x0000e0ff,
20  0x0000f0ff, 0x0000f8ff, 0x0000fcff, 0x0000feff, 0x0000ffff, 0x0080ffff,
21  0x00c0ffff, 0x00e0ffff, 0x00f0ffff, 0x00f8ffff, 0x00fcffff, 0x00feffff,
22  0x00ffffff, 0x80ffffff, 0xc0ffffff, 0xe0ffffff, 0xf0ffffff, 0xf8ffffff,
23  0xfcffffff, 0xfeffffff, 0xffffffff
24 };
25 
33 uint32_t cidr_to_mask(uint8_t cidr) {
34  if (cidr > 32) throw "bad_route_length";
35  return CIDR_MASK_MAP[cidr];
36 }
37 
43  afi = IPV4;
44  prefix = length = 0;
45 }
46 
54 Prefix4::Prefix4(uint32_t prefix, uint8_t length) {
55  if (length > 32) throw "bad_route_length";
56  afi = IPV4;
57  this->prefix = prefix;
58  this->length = length;
59 }
60 
68 Prefix4::Prefix4(const char* prefix, uint8_t length) {
69  if (length > 32) throw "bad_route_length";
70  afi = IPV4;
71  this->length = length;
72  inet_pton(AF_INET, prefix, &(this->prefix));
73 }
74 
84 ssize_t Prefix4::parse(const uint8_t *buffer, size_t buf_sz) {
85  if (buf_sz < 1) return -1;
86  length = getValue<uint8_t>(&buffer);
87  size_t prefix_buf_len = (length + 7) / 8;
88  if (prefix_buf_len + 1 > buf_sz) return -1;
89  prefix = 0;
90  memcpy(&prefix, buffer, prefix_buf_len);
91  return prefix_buf_len + 1;
92 }
93 
103 ssize_t Prefix4::write(uint8_t *buffer, size_t buf_sz) const {
104  size_t prefix_buf_len = (length + 7) / 8;
105  if (buf_sz < 1 + prefix_buf_len) return -1;
106  putValue<uint8_t>(&buffer, length);
107  memcpy(buffer, &prefix, prefix_buf_len);
108  return prefix_buf_len + 1;
109 }
110 
120 bool Prefix4::Includes (uint32_t prefix, uint8_t length, uint32_t address) {
121  if (length > 32) return false;
122  return (address & CIDR_MASK_MAP[length]) == prefix;
123 }
124 
135 bool Prefix4::Includes (uint32_t prefix_a, uint8_t length_a, uint32_t prefix_b, uint8_t length_b) {
136  if (length_a > 32 || length_b > 32) return false;
137  if (length_b < length_a) return false;
138  return (prefix_b & CIDR_MASK_MAP[length_a]) == prefix_a;
139 }
140 
148 bool Prefix4::includes (uint32_t address) const {
149  return (address & CIDR_MASK_MAP[length]) == prefix;
150 }
151 
159 bool Prefix4::includes (const char* address) const {
160  uint32_t address_int = 0;
161  if (inet_pton(AF_INET, address, &address_int) <= 0) return false;
162  return includes (address_int);
163 }
164 
173 bool Prefix4::includes (uint32_t prefix, uint8_t length) const {
174  if (length > 32) return false;
175  if (length < this->length) return false;
176  return (prefix & CIDR_MASK_MAP[this->length]) == this->prefix;
177 }
178 
186 bool Prefix4::includes (const Prefix &other) const {
187  if (other.afi != IPV4) return false;
188  const Prefix4 &other_4 = dynamic_cast<const Prefix4 &>(other);
189  return includes (other_4.prefix, other_4.length);
190 }
191 
200 bool Prefix4::includes (const char* prefix, uint8_t length) const {
201  uint32_t prefix_int = 0;
202  if (inet_pton(AF_INET, prefix, &prefix_int) <= 0) return false;
203  return includes(prefix_int, length);
204 }
205 
213 bool Prefix4::operator== (const Prefix &other) const {
214  if (other.afi != IPV4) return false;
215  const Prefix4 &other_4 = dynamic_cast<const Prefix4 &>(other);
216  return other_4.prefix == prefix && other_4.length == length;
217 }
218 
219 bool Prefix4::operator> (const Prefix &other) const {
220  if (other.afi != IPV4) return false;
221  const Prefix4 &other_4 = dynamic_cast<const Prefix4 &>(other);
222  return length < other_4.length;
223 }
224 
225 bool Prefix4::operator< (const Prefix &other) const {
226  if (other.afi != IPV4) return false;
227  const Prefix4 &other_4 = dynamic_cast<const Prefix4 &>(other);
228  return length > other_4.length;
229 }
230 
231 bool Prefix4::operator>= (const Prefix &other) const {
232  return !(*this < other);
233 }
234 
235 bool Prefix4::operator<= (const Prefix &other) const {
236  return !(*this > other);
237 }
238 
239 bool Prefix4::operator!= (const Prefix &other) const {
240  return !(*this == other);
241 }
242 
251 bool Prefix4::set(uint32_t prefix, uint8_t length) {
252  if (length > 32) return false;
253  this->length = length;
254  this->prefix = prefix;
255  return true;
256 }
257 
265 bool Prefix4::setPrefix(uint32_t prefix) {
266  this->prefix = prefix;
267  return true;
268 }
269 
277 bool Prefix4::setLength(uint8_t length) {
278  if (length > 32) return false;
279  this->length = length;
280  return true;
281 }
282 
288 uint32_t Prefix4::getPrefix() const {
289  return prefix;
290 }
291 
297 uint8_t Prefix4::getLength() const {
298  return length;
299 }
300 
307 uint32_t Prefix4::getMask() const {
308  if (length > 32) throw "bad_route_length";
309  return CIDR_MASK_MAP[length];
310 }
311 
312 }
uint32_t getPrefix() const
Get prefix.
Definition: prefix4.cc:288
uint32_t getMask() const
Get netmask.
Definition: prefix4.cc:307
static bool Includes(uint32_t prefix, uint8_t length, uint32_t address)
Test if an address is inside a prefix.
Definition: prefix4.cc:120
bool set(uint32_t prefix, uint8_t length)
Set Prefix4.
Definition: prefix4.cc:251
bool setLength(uint8_t length)
Set netmask.
Definition: prefix4.cc:277
bool includes(uint32_t address) const
Test if an address is inside this prefix.
Definition: prefix4.cc:148
bool operator==(const Prefix &other) const
Test if two routes are equals.
Definition: prefix4.cc:213
uint8_t getLength() const
Get netmask.
Definition: prefix4.cc:297
uint32_t cidr_to_mask(uint8_t cidr)
Convert netmask in CIDR notation to network bytes integer.
Definition: prefix4.cc:33
Prefix4()
Construct a new Prefix4 object.
Definition: prefix4.cc:42
bool setPrefix(uint32_t prefix)
Set prefix.
Definition: prefix4.cc:265
IPv4 Route/Prefix related utilities.
Definition: prefix4.h:25
Buffer operation helpers.
Definition: bgp-afi.h:14
IPv4 Route/Prefix related utilities.
ssize_t write(uint8_t *buffer, size_t buf_sz) const
Write a IPv4 prefix to NLRI buffer.
Definition: prefix4.cc:103
Route/Prefix related utilities.
Definition: prefix.h:25
ssize_t parse(const uint8_t *buffer, size_t buf_sz)
Parse a IPv4 NLRI prefix from buffer.
Definition: prefix4.cc:84