libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
dhcp.h
1 /*
2  * Copyright (c) 2016, 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 <list>
34 #include <vector>
35 #include <string>
36 #include "bootp.h"
37 #include "macros.h"
38 #include "pdu_option.h"
39 #include "cxxstd.h"
40 
41 namespace Tins {
42 
68 class TINS_API DHCP : public BootP {
69 public:
73  static const PDU::PDUType pdu_flag = PDU::DHCP;
74 
78  enum Flags {
79  DISCOVER = 1,
80  OFFER = 2,
81  REQUEST = 3,
82  DECLINE = 4,
83  ACK = 5,
84  NAK = 6,
85  RELEASE = 7,
86  INFORM = 8
87  };
88 
92  enum OptionTypes {
93  PAD,
94  SUBNET_MASK,
95  TIME_OFFSET,
96  ROUTERS,
97  TIME_SERVERS,
98  NAME_SERVERS,
99  DOMAIN_NAME_SERVERS,
100  LOG_SERVERS,
101  COOKIE_SERVERS,
102  LPR_SERVERS,
103  IMPRESS_SERVERS,
104  RESOURCE_LOCATION_SERVERS,
105  HOST_NAME,
106  BOOT_SIZE,
107  MERIT_DUMP,
108  DOMAIN_NAME,
109  SWAP_SERVER,
110  ROOT_PATH,
111  EXTENSIONS_PATH,
112  IP_FORWARDING,
113  NON_LOCAL_SOURCE_ROUTING,
114  POLICY_FILTER,
115  MAX_DGRAM_REASSEMBLY,
116  DEFAULT_IP_TTL,
117  PATH_MTU_AGING_TIMEOUT,
118  PATH_MTU_PLATEAU_TABLE,
119  INTERFACE_MTU,
120  ALL_SUBNETS_LOCAL,
121  BROADCAST_ADDRESS,
122  PERFORM_MASK_DISCOVERY,
123  MASK_SUPPLIER,
124  ROUTER_DISCOVERY,
125  ROUTER_SOLICITATION_ADDRESS,
126  STATIC_ROUTES,
127  TRAILER_ENCAPSULATION,
128  ARP_CACHE_TIMEOUT,
129  IEEE802_3_ENCAPSULATION,
130  DEFAULT_TCP_TTL,
131  TCP_KEEPALIVE_INTERVAL,
132  TCP_KEEPALIVE_GARBAGE,
133  NIS_DOMAIN,
134  NIS_SERVERS,
135  NTP_SERVERS,
136  VENDOR_ENCAPSULATED_OPTIONS,
137  NETBIOS_NAME_SERVERS,
138  NETBIOS_DD_SERVER,
139  NETBIOS_NODE_TYPE,
140  NETBIOS_SCOPE,
141  FONT_SERVERS,
142  X_DISPLAY_MANAGER,
143  DHCP_REQUESTED_ADDRESS,
144  DHCP_LEASE_TIME,
145  DHCP_OPTION_OVERLOAD,
146  DHCP_MESSAGE_TYPE,
147  DHCP_SERVER_IDENTIFIER,
148  DHCP_PARAMETER_REQUEST_LIST,
149  DHCP_MESSAGE,
150  DHCP_MAX_MESSAGE_SIZE,
151  DHCP_RENEWAL_TIME,
152  DHCP_REBINDING_TIME,
153  VENDOR_CLASS_IDENTIFIER,
154  DHCP_CLIENT_IDENTIFIER,
155  NWIP_DOMAIN_NAME,
156  NWIP_SUBOPTIONS,
157  USER_CLASS = 77,
158  FQDN = 81,
159  DHCP_AGENT_OPTIONS = 82,
160  SUBNET_SELECTION = 118,
161  AUTHENTICATE = 210,
162  END = 255
163  };
164 
169 
173  typedef std::list<option> options_type;
174 
181  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
182 
189  DHCP();
190 
201  DHCP(const uint8_t* buffer, uint32_t total_sz);
202 
207  void add_option(const option& opt);
208 
209  #if TINS_IS_CXX11
210 
217  void add_option(option &&opt) {
218  internal_add_option(opt);
219  options_.push_back(std::move(opt));
220  }
221  #endif
222 
232  bool remove_option(OptionTypes type);
233 
239  const option* search_option(OptionTypes opt) const;
240 
248  void type(Flags type);
249 
259  void end();
260 
268  void server_identifier(ipaddress_type ip);
269 
277  void lease_time(uint32_t time);
278 
286  void renewal_time(uint32_t time);
287 
295  void rebind_time(uint32_t time);
296 
304  void subnet_mask(ipaddress_type mask);
305 
313  void routers(const std::vector<ipaddress_type>& routers);
314 
322  void domain_name_servers(const std::vector<ipaddress_type>& dns);
323 
331  void broadcast(ipaddress_type addr);
332 
340  void requested_ip(ipaddress_type addr);
341 
349  void domain_name(const std::string& name);
350 
358  void hostname(const std::string& name);
359 
360  // Option getters
361 
370  uint8_t type() const;
371 
380  ipaddress_type server_identifier() const;
381 
390  uint32_t lease_time() const;
391 
400  uint32_t renewal_time() const;
401 
410  uint32_t rebind_time() const;
411 
420  ipaddress_type subnet_mask() const;
421 
431  std::vector<ipaddress_type> routers() const;
432 
442  std::vector<ipaddress_type> domain_name_servers() const;
443 
452  ipaddress_type broadcast() const;
453 
462  ipaddress_type requested_ip() const;
463 
472  std::string domain_name() const;
473 
482  std::string hostname() const;
483 
488  const options_type options() const { return options_; }
489 
494  PDUType pdu_type() const { return pdu_flag; }
495 
501  uint32_t header_size() const;
502 
506  DHCP* clone() const {
507  return new DHCP(*this);
508  }
509 private:
510  static const uint32_t MAX_DHCP_SIZE;
511 
512  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
513 
514  template <typename T>
515  T search_and_convert(OptionTypes opt) const {
516  const option* option = search_option(opt);
517  if (!option) {
518  throw option_not_found();
519  }
520  return option->to<T>();
521  }
522 
523  void internal_add_option(const option& opt);
524  serialization_type serialize_list(const std::vector<ipaddress_type>& ip_list);
525  options_type::const_iterator search_option_iterator(OptionTypes opt) const;
526  options_type::iterator search_option_iterator(OptionTypes opt);
527 
528  options_type options_;
529  uint32_t size_;
530 };
531 
532 } // Tins
533 
534 #endif // TINS_DHCP_H
Flags
Definition: dhcp.h:78
PDUType pdu_type() const
Getter for the PDU's type.
Definition: dhcp.h:494
DHCP * clone() const
Definition: dhcp.h:506
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
Represents a PDU option field.
Definition: pdu_option.h:320
OptionTypes
DHCP options enum.
Definition: dhcp.h:92
const options_type options() const
Getter for the options list.
Definition: dhcp.h:488
Type used to store a PDU header's data.
Definition: pdu.h:195
PDUOption< uint8_t, DHCP > option
Definition: dhcp.h:168
Represents the DHCP PDU.
Definition: dhcp.h:68
void add_option(option &&opt)
Adds a new option to this DHCP PDU.
Definition: dhcp.h:217
Represents a BootP PDU.
Definition: bootp.h:48
Base class for protocol data units.
Definition: pdu.h:108
Exception thrown when an option is not found.
Definition: exceptions.h:53
std::list< option > options_type
Definition: dhcp.h:173