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