Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_BGP_INET_INET_ROUTE_H_ 6 : #define SRC_BGP_INET_INET_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 Ip4Prefix { 17 : public: 18 205752 : Ip4Prefix() : prefixlen_(0) { } 19 2279780 : Ip4Prefix(Ip4Address addr, int prefixlen) 20 2279780 : : ip4_addr_(addr), prefixlen_(prefixlen) { 21 2279678 : } 22 : int CompareTo(const Ip4Prefix &rhs) const; 23 : 24 91341 : Ip4Address addr() const { return ip4_addr_; } 25 7039037 : Ip4Address ip4_addr() const { return ip4_addr_; } 26 : 27 1112785 : int prefixlen() const { return prefixlen_; } 28 : 29 : static int FromProtoPrefix(const BgpProtoPrefix &proto_prefix, 30 : Ip4Prefix *prefix); 31 : static int FromProtoPrefix(BgpServer *server, 32 : const BgpProtoPrefix &proto_prefix, 33 : const BgpAttr *attr, 34 : const Address::Family family, Ip4Prefix *prefix, 35 : BgpAttrPtr *new_attr, uint32_t *label, 36 : uint32_t *l3_label); 37 : static Ip4Prefix FromString(const std::string &str, 38 : boost::system::error_code *errorp = NULL); 39 : 40 : std::string ToString() const; 41 : 42 13990 : bool operator==(const Ip4Prefix &rhs) const { 43 13990 : return (CompareTo(rhs) == 0); 44 : } 45 4567 : bool operator!=(const Ip4Prefix &rhs) const { 46 4567 : return (CompareTo(rhs) != 0); 47 : } 48 34844 : bool operator<(const Ip4Prefix &rhs) const { 49 34844 : int cmp = CompareTo(rhs); 50 34843 : return (cmp < 0); 51 : } 52 : bool operator>(const Ip4Prefix &rhs) const { 53 : int cmp = CompareTo(rhs); 54 : return (cmp > 0); 55 : } 56 : 57 : // Check whether 'this' is more specific than rhs. 58 : bool IsMoreSpecific(const Ip4Prefix &rhs) const; 59 : 60 : private: 61 : Ip4Address ip4_addr_; 62 : int prefixlen_; 63 : }; 64 : 65 : class InetRoute : public BgpRoute { 66 : public: 67 : explicit InetRoute(const Ip4Prefix &prefix); 68 : virtual int CompareTo(const Route &rhs) const; 69 1309130 : virtual std::string ToString() const { return prefix_str_; } 70 4416131 : const Ip4Prefix &GetPrefix() const { return prefix_; } 71 : 72 : virtual KeyPtr GetDBRequestKey() const; 73 : virtual void SetKey(const DBRequestKey *reqkey); 74 : 75 : virtual void BuildProtoPrefix(BgpProtoPrefix *prefix, 76 : const BgpAttr *attr = NULL, 77 : uint32_t label = 0, 78 : uint32_t l3_label = 0) const; 79 : virtual void BuildBgpProtoNextHop(std::vector<uint8_t> &nh, 80 : IpAddress nexthop) const; 81 : 82 6003606 : virtual bool IsLess(const DBEntry &genrhs) const { 83 6003606 : const InetRoute &rhs = static_cast<const InetRoute &>(genrhs); 84 6003606 : int cmp = CompareTo(rhs); 85 6001856 : return (cmp < 0); 86 : } 87 : 88 : virtual bool IsMoreSpecific(const std::string &match) const; 89 : virtual bool IsLessSpecific(const std::string &match) const; 90 : 91 : private: 92 : Ip4Prefix prefix_; 93 : std::string prefix_str_; 94 : 95 : DISALLOW_COPY_AND_ASSIGN(InetRoute); 96 : }; 97 : 98 : #endif // SRC_BGP_INET_INET_ROUTE_H_