OpenSDN source code
oper_dhcp_options.h
Go to the documentation of this file.
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
18 public:
19  typedef std::vector<autogen::DhcpOptionType> DhcpOptionsList;
20  typedef std::vector<autogen::RouteType> HostOptionsList;
21 
22  struct HostRoute {
24  uint32_t plen_;
26 
27  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  bool operator==(const HostRoute &rhs) const {
36  return prefix_ == rhs.prefix_ && plen_ == rhs.plen_ &&
37  gw_ == rhs.gw_;
38  }
39  std::string ToString() const {
40  char len[32];
41  snprintf(len, sizeof(len), "%u", plen_);
42  return prefix_.to_string() + "/" + std::string(len) +
43  " -> " + gw_.to_string();
44  }
45  };
46 
49  dhcp_options_ = options.dhcp_options_;
50  host_routes_ = options.host_routes_;
51  }
52  virtual ~OperDhcpOptions() {}
53 
54  const DhcpOptionsList &dhcp_options() const { return dhcp_options_; }
55  const std::vector<HostRoute> &host_routes() const { return host_routes_; }
58  host_routes_.clear();
60  }
62  host_routes_.clear();
63  for (unsigned int i = 0; i < host_routes.size(); ++i) {
64  HostRoute host_route;
65  boost::system::error_code ec = Ip4PrefixParse(host_routes[i].prefix,
66  &host_route.prefix_,
67  (int *)&host_route.plen_);
68  if (ec || host_route.plen_ > 32) {
69  continue;
70  }
71  host_route.gw_ = Ip4Address::from_string(host_routes[i].next_hop, ec);
72  if (ec) {
73  host_route.gw_ = Ip4Address();
74  }
75  host_routes_.push_back(host_route);
76  }
77  }
78 
79  bool are_dhcp_options_set() const {
80  return dhcp_options_.size() > 0;
81  }
82  bool are_host_routes_set() const {
83  return host_routes_.size() > 0;
84  }
85 
86 private:
88  std::vector<HostRoute> host_routes_;
89 };
90 
91 #endif // vnsw_agent_oper_dhcp_options_h_
boost::system::error_code Ip4PrefixParse(const string &str, Ip4Address *addr, int *plen)
Definition: address.cc:108
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
bool are_dhcp_options_set() const
std::vector< autogen::RouteType > HostOptionsList
void set_options(const DhcpOptionsList &options)
std::vector< HostRoute > host_routes_
DhcpOptionsList dhcp_options_
virtual ~OperDhcpOptions()
OperDhcpOptions(const OperDhcpOptions &options)
std::vector< autogen::DhcpOptionType > DhcpOptionsList
void set_host_routes(const HostOptionsList &host_routes)
const std::vector< HostRoute > & host_routes() const
const DhcpOptionsList & dhcp_options() const
bool are_host_routes_set() const
void update_host_routes(const HostOptionsList &host_routes)
static Options options
bool operator<(const HostRoute &rhs) const
std::string ToString() const
bool operator==(const HostRoute &rhs) const