Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef vnsw_agent_oper_dhcp_options_h_ 6 : #define vnsw_agent_oper_dhcp_options_h_ 7 : 8 : #include <vnc_cfg_types.h> 9 : 10 : namespace autogen { 11 : struct DhcpOptionType; 12 : struct RouteType; 13 : } 14 : 15 : // DHCP options coming from config 16 : class OperDhcpOptions { 17 : public: 18 : typedef std::vector<autogen::DhcpOptionType> DhcpOptionsList; 19 : typedef std::vector<autogen::RouteType> HostOptionsList; 20 : 21 : struct HostRoute { 22 : Ip4Address prefix_; 23 : uint32_t plen_; 24 : Ip4Address gw_; 25 : 26 0 : HostRoute() : prefix_(), plen_(0), gw_() {} 27 : bool operator<(const HostRoute &rhs) const { 28 : if (prefix_ != rhs.prefix_) 29 : return prefix_ < rhs.prefix_; 30 : if (plen_ != rhs.plen_) 31 : return plen_ < rhs.plen_; 32 : return gw_ < rhs.gw_; 33 : } 34 0 : bool operator==(const HostRoute &rhs) const { 35 0 : return prefix_ == rhs.prefix_ && plen_ == rhs.plen_ && 36 0 : gw_ == rhs.gw_; 37 : } 38 0 : std::string ToString() const { 39 : char len[32]; 40 0 : snprintf(len, sizeof(len), "%u", plen_); 41 0 : return prefix_.to_string() + "/" + std::string(len) + 42 0 : " -> " + gw_.to_string(); 43 : } 44 : }; 45 : 46 1294 : OperDhcpOptions() {} 47 291 : OperDhcpOptions(const OperDhcpOptions &options) { 48 291 : dhcp_options_ = options.dhcp_options_; 49 291 : host_routes_ = options.host_routes_; 50 291 : } 51 1585 : virtual ~OperDhcpOptions() {} 52 : 53 0 : const DhcpOptionsList &dhcp_options() const { return dhcp_options_; } 54 78 : const std::vector<HostRoute> &host_routes() const { return host_routes_; } 55 115 : void set_options(const DhcpOptionsList &options) { dhcp_options_ = options; } 56 160 : void set_host_routes(const HostOptionsList &host_routes) { 57 160 : host_routes_.clear(); 58 160 : update_host_routes(host_routes); 59 160 : } 60 160 : void update_host_routes(const HostOptionsList &host_routes) { 61 160 : host_routes_.clear(); 62 160 : for (unsigned int i = 0; i < host_routes.size(); ++i) { 63 0 : HostRoute host_route; 64 0 : boost::system::error_code ec = Ip4PrefixParse(host_routes[i].prefix, 65 : &host_route.prefix_, 66 : (int *)&host_route.plen_); 67 0 : if (ec || host_route.plen_ > 32) { 68 0 : continue; 69 : } 70 0 : host_route.gw_ = Ip4Address::from_string(host_routes[i].next_hop, ec); 71 0 : if (ec) { 72 0 : host_route.gw_ = Ip4Address(); 73 : } 74 0 : host_routes_.push_back(host_route); 75 : } 76 160 : } 77 : 78 0 : bool are_dhcp_options_set() const { 79 0 : return dhcp_options_.size() > 0; 80 : } 81 0 : bool are_host_routes_set() const { 82 0 : return host_routes_.size() > 0; 83 : } 84 : 85 : private: 86 : DhcpOptionsList dhcp_options_; 87 : std::vector<HostRoute> host_routes_; 88 : }; 89 : 90 : #endif // vnsw_agent_oper_dhcp_options_h_