libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
dhcp.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_DHCP_H
31 #define TINS_DHCP_H
32 
33 #include <vector>
34 #include <string>
35 #include <tins/bootp.h>
36 #include <tins/macros.h>
37 #include <tins/pdu_option.h>
38 #include <tins/cxxstd.h>
39 
40 namespace Tins {
41 
67 class TINS_API DHCP : public BootP {
68 public:
72  static const PDU::PDUType pdu_flag = PDU::DHCP;
73 
77  enum Flags {
78  DISCOVER = 1,
79  OFFER = 2,
80  REQUEST = 3,
81  DECLINE = 4,
82  ACK = 5,
83  NAK = 6,
84  RELEASE = 7,
85  INFORM = 8
86  };
87 
91  enum OptionTypes {
92  PAD,
93  SUBNET_MASK,
94  TIME_OFFSET,
95  ROUTERS,
96  TIME_SERVERS,
97  NAME_SERVERS,
98  DOMAIN_NAME_SERVERS,
99  LOG_SERVERS,
100  COOKIE_SERVERS,
101  LPR_SERVERS,
102  IMPRESS_SERVERS,
103  RESOURCE_LOCATION_SERVERS,
104  HOST_NAME,
105  BOOT_SIZE,
106  MERIT_DUMP,
107  DOMAIN_NAME,
108  SWAP_SERVER,
109  ROOT_PATH,
110  EXTENSIONS_PATH,
111  IP_FORWARDING,
112  NON_LOCAL_SOURCE_ROUTING,
113  POLICY_FILTER,
114  MAX_DGRAM_REASSEMBLY,
115  DEFAULT_IP_TTL,
116  PATH_MTU_AGING_TIMEOUT,
117  PATH_MTU_PLATEAU_TABLE,
118  INTERFACE_MTU,
119  ALL_SUBNETS_LOCAL,
120  BROADCAST_ADDRESS,
121  PERFORM_MASK_DISCOVERY,
122  MASK_SUPPLIER,
123  ROUTER_DISCOVERY,
124  ROUTER_SOLICITATION_ADDRESS,
125  STATIC_ROUTES,
126  TRAILER_ENCAPSULATION,
127  ARP_CACHE_TIMEOUT,
128  IEEE802_3_ENCAPSULATION,
129  DEFAULT_TCP_TTL,
130  TCP_KEEPALIVE_INTERVAL,
131  TCP_KEEPALIVE_GARBAGE,
132  NIS_DOMAIN,
133  NIS_SERVERS,
134  NTP_SERVERS,
135  VENDOR_ENCAPSULATED_OPTIONS,
136  NETBIOS_NAME_SERVERS,
137  NETBIOS_DD_SERVER,
138  NETBIOS_NODE_TYPE,
139  NETBIOS_SCOPE,
140  FONT_SERVERS,
141  X_DISPLAY_MANAGER,
142  DHCP_REQUESTED_ADDRESS,
143  DHCP_LEASE_TIME,
144  DHCP_OPTION_OVERLOAD,
145  DHCP_MESSAGE_TYPE,
146  DHCP_SERVER_IDENTIFIER,
147  DHCP_PARAMETER_REQUEST_LIST,
148  DHCP_MESSAGE,
149  DHCP_MAX_MESSAGE_SIZE,
150  DHCP_RENEWAL_TIME,
151  DHCP_REBINDING_TIME,
152  VENDOR_CLASS_IDENTIFIER,
153  DHCP_CLIENT_IDENTIFIER,
154  NWIP_DOMAIN_NAME,
155  NWIP_SUBOPTIONS,
156  USER_CLASS = 77,
157  FQDN = 81,
158  DHCP_AGENT_OPTIONS = 82,
159  SUBNET_SELECTION = 118,
160  AUTHENTICATE = 210,
161  END = 255
162  };
163 
168 
172  typedef std::vector<option> options_type;
173 
180  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
181 
188  DHCP();
189 
200  DHCP(const uint8_t* buffer, uint32_t total_sz);
201 
206  void add_option(const option& opt);
207 
208  #if TINS_IS_CXX11
209 
216  void add_option(option &&opt) {
217  internal_add_option(opt);
218  options_.push_back(std::move(opt));
219  }
220  #endif
221 
231  bool remove_option(OptionTypes type);
232 
238  const option* search_option(OptionTypes opt) const;
239 
247  void type(Flags type);
248 
258  void end();
259 
267  void server_identifier(ipaddress_type ip);
268 
276  void lease_time(uint32_t time);
277 
285  void renewal_time(uint32_t time);
286 
294  void rebind_time(uint32_t time);
295 
303  void subnet_mask(ipaddress_type mask);
304 
312  void routers(const std::vector<ipaddress_type>& routers);
313 
321  void domain_name_servers(const std::vector<ipaddress_type>& dns);
322 
330  void broadcast(ipaddress_type addr);
331 
339  void requested_ip(ipaddress_type addr);
340 
348  void domain_name(const std::string& name);
349 
357  void hostname(const std::string& name);
358 
359  // Option getters
360 
369  uint8_t type() const;
370 
379  ipaddress_type server_identifier() const;
380 
389  uint32_t lease_time() const;
390 
399  uint32_t renewal_time() const;
400 
409  uint32_t rebind_time() const;
410 
419  ipaddress_type subnet_mask() const;
420 
430  std::vector<ipaddress_type> routers() const;
431 
440  std::vector<ipaddress_type> domain_name_servers() const;
441 
450  ipaddress_type broadcast() const;
451 
460  ipaddress_type requested_ip() const;
461 
470  std::string domain_name() const;
471 
480  std::string hostname() const;
481 
486  const options_type options() const { return options_; }
487 
492  PDUType pdu_type() const { return pdu_flag; }
493 
499  uint32_t header_size() const;
500 
504  DHCP* clone() const {
505  return new DHCP(*this);
506  }
507 private:
508  static const uint32_t MAX_DHCP_SIZE;
509 
510  void write_serialization(uint8_t* buffer, uint32_t total_sz);
511 
512  template <typename T>
513  T search_and_convert(OptionTypes opt) const {
514  const option* option = search_option(opt);
515  if (!option) {
516  throw option_not_found();
517  }
518  return option->to<T>();
519  }
520 
521  void internal_add_option(const option& opt);
522  serialization_type serialize_list(const std::vector<ipaddress_type>& ip_list);
523  options_type::const_iterator search_option_iterator(OptionTypes opt) const;
524  options_type::iterator search_option_iterator(OptionTypes opt);
525 
526  options_type options_;
527  uint32_t size_;
528 };
529 
530 } // Tins
531 
532 #endif // TINS_DHCP_H
Flags
Definition: dhcp.h:77
PDUType pdu_type() const
Getter for the PDU's type.
Definition: dhcp.h:492
DHCP * clone() const
Definition: dhcp.h:504
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
Represents a PDU option field.
Definition: pdu_option.h:201
OptionTypes
DHCP options enum.
Definition: dhcp.h:91
const options_type options() const
Getter for the options list.
Definition: dhcp.h:486
Type used to store a PDU header's data.
Definition: pdu.h:194
std::vector< option > options_type
Definition: dhcp.h:172
PDUOption< uint8_t, DHCP > option
Definition: dhcp.h:167
Represents the DHCP PDU.
Definition: dhcp.h:67
void add_option(option &&opt)
Adds a new option to this DHCP PDU.
Definition: dhcp.h:216
Represents a BootP PDU.
Definition: bootp.h:47
Exception thrown when an option is not found.
Definition: exceptions.h:56