libbgp  0.6
A C++ BGP Library.
prefix4.h
Go to the documentation of this file.
1 
11 #ifndef BGP_PREFIX4_H_
12 #define BGP_PREFIX4_H_
13 #include <stdint.h>
14 #include <unistd.h>
15 #include "prefix.h"
16 
17 namespace libbgp {
18 
19 uint32_t cidr_to_mask(uint8_t cidr);
20 
25 class Prefix4 : public Prefix {
26 public:
27  Prefix4();
28  Prefix4(uint32_t prefix, uint8_t length);
29  Prefix4(const char* prefix, uint8_t length);
30 
31  ssize_t parse(const uint8_t *buffer, size_t buf_sz);
32  ssize_t write(uint8_t *buffer, size_t buf_sz) const;
33 
34  // static utility functions for route include test
35  static bool Includes (uint32_t prefix, uint8_t length, uint32_t address);
36  static bool Includes (uint32_t prefix_a, uint8_t length_a, uint32_t prefix_b, uint8_t length_b);
37 
38  // test if address in prefix
39  bool includes (uint32_t address) const;
40  bool includes (const char* address) const;
41 
42  // test if route other is sub-prefix
43  bool includes (const Prefix &other) const;
44  bool includes (uint32_t prefix, uint8_t length) const;
45  bool includes (const char* prefix, uint8_t length) const;
46 
47  // test if length & prefix equals to other
48  bool operator== (const Prefix &other) const;
49 
50  // test if length smaller (prefix size bigger) then other. prefix must be
51  // same to do this.
52  bool operator> (const Prefix &other) const;
53 
54  // test if length bigger (prefix size smaller) then other. prefix must be
55  // same to do this.
56  bool operator< (const Prefix &other) const;
57 
58  bool operator>= (const Prefix &other) const;
59  bool operator<= (const Prefix &other) const;
60  bool operator!= (const Prefix &other) const;
61 
62  bool set(uint32_t prefix, uint8_t length);
63  bool setPrefix(uint32_t prefix);
64  bool setLength(uint8_t length);
65  uint32_t getPrefix() const;
66  uint8_t getLength() const;
67  uint32_t getMask() const;
68 
69 private:
70  uint8_t length;
71  uint32_t prefix;
72 };
73 
74 }
75 
76 #endif // BGP_PREFIX4_H_
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
Route/Prefix related utilities.
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
Definition: bgp-afi.h:14
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