Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef SRC_BGP_INET6_INET6_ROUTE_H_ 7 : #define SRC_BGP_INET6_INET6_ROUTE_H_ 8 : 9 : #include <string> 10 : #include <vector> 11 : 12 : #include "bgp/bgp_attr.h" 13 : #include "bgp/bgp_route.h" 14 : #include "base/address.h" 15 : #include "net/bgp_af.h" 16 : 17 : class Inet6Prefix { 18 : public: 19 : static const uint8_t kMaxV6PrefixLen = Address::kMaxV6PrefixLen; 20 : 21 95117 : Inet6Prefix() : prefixlen_(0) { } 22 1930049 : Inet6Prefix(Ip6Address addr, int prefixlen) 23 1930049 : : ip6_addr_(addr), prefixlen_(prefixlen) { 24 1929806 : } 25 : int CompareTo(const Inet6Prefix &rhs) const; 26 : 27 50715 : Ip6Address addr() const { return ip6_addr_; } 28 765847 : Ip6Address ip6_addr() const { return ip6_addr_; } 29 : 30 5163177 : const Ip6Address::bytes_type ToBytes() const { 31 5163177 : return ip6_addr_.to_bytes(); 32 : } 33 : 34 880511 : int prefixlen() const { return prefixlen_; } 35 : 36 : static int FromProtoPrefix(const BgpProtoPrefix &proto_prefix, 37 : Inet6Prefix *prefix); 38 : static int FromProtoPrefix(BgpServer *server, 39 : const BgpProtoPrefix &proto_prefix, 40 : const BgpAttr *attr, 41 : const Address::Family family, 42 : Inet6Prefix *prefix, BgpAttrPtr *new_attr, 43 : uint32_t *label, uint32_t *l3_label); 44 : static Inet6Prefix FromString(const std::string &str, 45 : boost::system::error_code *errorp = NULL); 46 : std::string ToString() const; 47 : 48 11203 : bool operator==(const Inet6Prefix &rhs) const { 49 11203 : return (CompareTo(rhs) == 0); 50 : } 51 100 : bool operator!=(const Inet6Prefix &rhs) const { 52 100 : return (CompareTo(rhs) != 0); 53 : } 54 31616 : bool operator<(const Inet6Prefix &rhs) const { 55 31616 : int cmp = CompareTo(rhs); 56 31616 : return (cmp < 0); 57 : } 58 : bool operator>(const Inet6Prefix &rhs) const { 59 : int cmp = CompareTo(rhs); 60 : return (cmp > 0); 61 : } 62 : 63 : // Check whether 'this' is more specific than rhs. 64 : bool IsMoreSpecific(const Inet6Prefix &rhs) const; 65 : 66 : Inet6Prefix operator&(const Inet6Prefix& rhs) const; 67 : 68 : private: 69 41130 : Ip6Address::bytes_type ToBytes() { return ip6_addr_.to_bytes(); } 70 : 71 : Ip6Address ip6_addr_; 72 : int prefixlen_; 73 : }; 74 : 75 : class Inet6Route : public BgpRoute { 76 : public: 77 : explicit Inet6Route(const Inet6Prefix &prefix); 78 : virtual int CompareTo(const Route &rhs) const; 79 1025148 : virtual std::string ToString() const { return prefix_str_; } 80 : 81 3818093 : const Inet6Prefix &GetPrefix() const { return prefix_; } 82 : 83 : virtual KeyPtr GetDBRequestKey() const; 84 : virtual void SetKey(const DBRequestKey *reqkey); 85 : 86 : virtual void BuildProtoPrefix(BgpProtoPrefix *prefix, 87 : const BgpAttr *attr = NULL, 88 : uint32_t label = 0, 89 : uint32_t l3_label = 0) const; 90 : virtual void BuildBgpProtoNextHop(std::vector<uint8_t> &nh, 91 : IpAddress nexthop) const; 92 : 93 5372754 : virtual bool IsLess(const DBEntry &genrhs) const { 94 5372754 : const Inet6Route &rhs = static_cast<const Inet6Route &>(genrhs); 95 5372754 : int cmp = CompareTo(rhs); 96 5371235 : return (cmp < 0); 97 : } 98 : 99 : virtual bool IsMoreSpecific(const std::string &match) const; 100 : virtual bool IsLessSpecific(const std::string &match) const; 101 : 102 : private: 103 : Inet6Prefix prefix_; 104 : std::string prefix_str_; 105 : 106 : DISALLOW_COPY_AND_ASSIGN(Inet6Route); 107 : }; 108 : 109 : class Inet6Masks { 110 : public: 111 : static void Init(); 112 : static void Clear(); 113 : static const Inet6Prefix& PrefixlenToMask(uint8_t prefix_len); 114 : private: 115 : static Inet6Prefix CalculateMaskFromPrefixlen(int prefixlen); 116 : 117 : static std::vector<Inet6Prefix> masks_; 118 : static bool initialized_; 119 : }; 120 : 121 : #endif // SRC_BGP_INET6_INET6_ROUTE_H_