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