16 #include <boost/algorithm/string.hpp> 29 l3_proto_t::l3_proto_t(
int v,
const std::string& s)
30 : enum_base<l3_proto_t>(v, s)
37 return (*
this == IPV6);
43 return (*
this == IPV4);
71 nh_proto_t::nh_proto_t(
int v,
const std::string& s)
105 : m_addr(boost::asio::ip::
address::from_string(s))
135 const boost::asio::ip::address&
150 if (m_len == o.m_len) {
151 return (m_addr < o.m_addr);
153 return (m_len < o.m_len);
160 return (m_len == o.m_len && m_addr == o.m_addr);
166 return (!(*
this == o));
172 std::ostringstream s;
174 s << m_addr.to_string() <<
"/" << std::to_string(m_len);
179 boost::asio::ip::address
182 boost::asio::ip::address
addr;
185 std::array<uint8_t, 16>
a;
186 std::copy(bytes, bytes + 16, std::begin(a));
187 boost::asio::ip::address_v6 v6(a);
190 std::array<uint8_t, 4>
a;
191 std::copy(bytes, bytes + 4, std::begin(a));
192 boost::asio::ip::address_v4 v4(a);
207 memcpy(array, addr.to_bytes().data(), 16);
213 memcpy(array, addr.to_bytes().data(), 4);
217 to_bytes(
const boost::asio::ip::address&
addr, uint8_t* is_ip6, uint8_t* array)
247 if (m_addr.is_v6()) {
264 boost::asio::ip::address_v4
266 const boost::asio::ip::address_v4& addr2)
269 a = addr1.to_ulong() | addr2.to_ulong();
270 boost::asio::ip::address_v4
addr(a);
274 boost::asio::ip::address_v4
operator&(
const boost::asio::ip::address_v4& addr1,
275 const boost::asio::ip::address_v4& addr2)
278 a = addr1.to_ulong() & addr2.to_ulong();
279 boost::asio::ip::address_v4
addr(a);
283 boost::asio::ip::address_v4
operator~(
const boost::asio::ip::address_v4& addr1)
286 a = ~addr1.to_ulong();
287 boost::asio::ip::address_v4
addr(a);
291 boost::asio::ip::address_v6
293 const boost::asio::ip::address_v6& addr2)
295 boost::asio::ip::address_v6::bytes_type b1 = addr1.to_bytes();
296 boost::asio::ip::address_v6::bytes_type b2 = addr2.to_bytes();
298 for (boost::asio::ip::address_v6::bytes_type::size_type ii = 0;
299 ii < b1.max_size(); ii++) {
303 boost::asio::ip::address_v6
addr(b1);
307 boost::asio::ip::address_v6
operator&(
const boost::asio::ip::address_v6& addr1,
308 const boost::asio::ip::address_v6& addr2)
310 boost::asio::ip::address_v6::bytes_type b1 = addr1.to_bytes();
311 boost::asio::ip::address_v6::bytes_type b2 = addr2.to_bytes();
313 for (boost::asio::ip::address_v6::bytes_type::size_type ii = 0;
314 ii < b1.max_size(); ii++) {
318 boost::asio::ip::address_v6
addr(b1);
322 boost::asio::ip::address_v6
operator~(
const boost::asio::ip::address_v6& addr1)
324 boost::asio::ip::address_v6::bytes_type b1 = addr1.to_bytes();
326 for (boost::asio::ip::address_v6::bytes_type::size_type ii = 0;
327 ii < b1.max_size(); ii++) {
331 boost::asio::ip::address_v6
addr(b1);
334 boost::asio::ip::address
336 const boost::asio::ip::address& addr2)
339 return (addr1.to_v6() | addr2.to_v6());
341 return (addr1.to_v4() | addr2.to_v4());
344 boost::asio::ip::address
operator&(
const boost::asio::ip::address& addr1,
345 const boost::asio::ip::address& addr2)
348 return (addr1.to_v6() & addr2.to_v6());
350 return (addr1.to_v4() & addr2.to_v4());
353 boost::asio::ip::address
operator~(
const boost::asio::ip::address& addr1)
356 return ~(addr1.to_v6());
358 return ~(addr1.to_v4());
361 boost::asio::ip::address
364 if (m_addr.is_v6()) {
365 boost::asio::ip::address_v6::bytes_type b =
370 for (boost::asio::ip::address_v6::bytes_type::size_type ii = 0;
371 ii < b.max_size(); ii++) {
372 for (int8_t bit = 7; bit >= 0 && n_bits; bit--) {
380 return (boost::asio::ip::address_v6(b));
386 return (boost::asio::ip::address_v4(a));
395 pfx.m_addr = pfx.m_addr & pfx.
mask();
405 pfx.m_addr = pfx.m_addr | ~pfx.
mask();
bool operator<(const prefix_t &o) const
Less than operator.
l3_proto_t l3_proto() const
Get the L3 protocol.
static const nh_proto_t IPV6
static const nh_proto_t IPV4
static const prefix_t ZEROv6
The all Zeros v6 prefix.
boost::asio::ip::address mask() const
Return a address representation of the mask, e.g.
Types belonging to Routing.
static const nh_proto_t & from_address(const boost::asio::ip::address &addr)
bool operator!=(const prefix_t &o) const
not equal opartor
static const l3_proto_t MPLS
prefix_t low() const
get the lowest address in the prefix
std::ostream & operator<<(std::ostream &os, const ip_route::key_t &key)
A next-hop protocol describes the protocol of a peer to which packets are sent after matching a route...
static const l3_proto_t IPV4
static const l3_proto_t IPV6
void to_bytes(const boost::asio::ip::address_v6 &addr, uint8_t *array)
std::string to_string() const
convert to string format for debug purposes
boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
prefix_t high() const
Get the highest address in the prefix.
boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4 &addr1)
prefix_t()
Default Constructor - creates ::/0.
void to_vpp(uint8_t *is_ip6, uint8_t *addr, uint8_t *len) const
Convert the prefix into VPP API parameters.
boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
prefix_t & operator=(const prefix_t &)
Assignement.
boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
bool operator==(const prefix_t &o) const
equals operator
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
static const l3_proto_t & from_address(const boost::asio::ip::address &addr)
static const nh_proto_t MPLS
The VPP Object Model (VOM) library.
static const prefix_t ZERO
The all Zeros prefix.
uint8_t mask_width() const
Get the network mask width.
const std::string & to_string() const
convert to string format for debug purposes
const boost::asio::ip::address & address() const
Get the address.
static const nh_proto_t ETHERNET