libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
bootp.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_BOOTP_H
31 #define TINS_BOOTP_H
32 
33 #include <stdint.h>
34 #include <algorithm>
35 #include <vector>
36 #include "pdu.h"
37 #include "macros.h"
38 #include "endianness.h"
39 #include "ip_address.h"
40 #include "hw_address.h"
41 
42 namespace Tins {
43 
48 class TINS_API BootP : public PDU {
49 public:
54 
59 
63  typedef std::vector<uint8_t> vend_type;
64 
68  static const PDU::PDUType pdu_flag = PDU::BOOTP;
69 
73  enum OpCodes {
74  BOOTREQUEST = 1,
75  BOOTREPLY = 2
76  };
77 
84  BootP();
85 
97  BootP(const uint8_t* buffer, uint32_t total_sz, uint32_t vend_field_size = 64);
98 
99  /* Getters */
100 
105  uint8_t opcode() const { return bootp_.opcode; }
106 
111  uint8_t htype() const { return bootp_.htype; }
112 
117  uint8_t hlen() const { return bootp_.hlen; }
118 
123  uint8_t hops() const { return bootp_.hops; }
124 
129  uint32_t xid() const { return Endian::be_to_host(bootp_.xid); }
130 
135  uint16_t secs() const { return Endian::be_to_host(bootp_.secs); }
136 
140  uint16_t padding() const { return Endian::be_to_host(bootp_.padding); }
141 
146  ipaddress_type ciaddr() const { return ipaddress_type(bootp_.ciaddr); }
147 
152  ipaddress_type yiaddr() const { return ipaddress_type(bootp_.yiaddr); }
153 
158  ipaddress_type siaddr() const { return ipaddress_type(bootp_.siaddr); }
159 
164  ipaddress_type giaddr() const { return ipaddress_type(bootp_.giaddr); }
165 
170  chaddr_type chaddr() const { return bootp_.chaddr; }
171 
176  const uint8_t* sname() const { return bootp_.sname; }
177 
182  const uint8_t* file() const { return bootp_.file; }
183 
188  const vend_type& vend() const { return vend_; }
189 
195  uint32_t header_size() const;
196  /* Setters */
197 
202  void opcode(uint8_t code);
203 
208  void htype(uint8_t type);
209 
214  void hlen(uint8_t length);
215 
220  void hops(uint8_t count);
221 
226  void xid(uint32_t identifier);
227 
232  void secs(uint16_t value);
233 
238  void padding(uint16_t value);
239 
244  void ciaddr(ipaddress_type address);
245 
250  void yiaddr(ipaddress_type address);
251 
256  void siaddr(ipaddress_type address);
257 
262  void giaddr(ipaddress_type address);
263 
269  template<size_t n>
270  void chaddr(const HWAddress<n>& new_chaddr) {
271  // Copy the new addr
272  uint8_t* end = std::copy(
273  new_chaddr.begin(),
274  new_chaddr.begin() + std::min(n, sizeof(bootp_.chaddr)),
275  bootp_.chaddr
276  );
277  // Fill what's left with zeros
278  if (end < bootp_.chaddr + chaddr_type::address_size) {
279  std::fill(end, bootp_.chaddr + chaddr_type::address_size, 0);
280  }
281  }
282 
287  void sname(const uint8_t* new_sname);
288 
293  void file(const uint8_t* new_file);
294 
299  void vend(const vend_type& newvend_);
300 
310  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
311 
316  PDUType pdu_type() const { return pdu_flag; }
317 
321  BootP* clone() const {
322  return new BootP(*this);
323  }
324 protected:
333  vend_type& vend() { return vend_; }
334 
335  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
336 
340  TINS_BEGIN_PACK
341  struct bootp_header {
342  uint8_t opcode;
343  uint8_t htype;
344  uint8_t hlen;
345  uint8_t hops;
346  uint32_t xid;
347  uint16_t secs;
348  uint16_t padding;
349  uint32_t ciaddr;
350  uint32_t yiaddr;
351  uint32_t siaddr;
352  uint32_t giaddr;
353  uint8_t chaddr[16];
354  uint8_t sname[64];
355  uint8_t file[128];
356  } TINS_END_PACK;
357 
358 private:
359  bootp_header bootp_;
360  vend_type vend_;
361 };
362 
363 } // Tins
364 
365 #endif // TINS_BOOTP_H
iterator begin()
Retrieves an iterator pointing to the begining of the address.
Definition: hw_address.h:186
uint32_t xid() const
Getter for the xid field.
Definition: bootp.h:129
uint8_t htype() const
Getter for the htype field.
Definition: bootp.h:111
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
chaddr_type chaddr() const
Getter for the chaddr field.
Definition: bootp.h:170
vend_type & vend()
Getter for the vend field.
Definition: bootp.h:333
std::vector< uint8_t > vend_type
Definition: bootp.h:63
uint8_t hlen() const
Getter for the hlen field.
Definition: bootp.h:117
ipaddress_type giaddr() const
Getter for the giaddr field.
Definition: bootp.h:164
Definition: bootp.h:341
const uint8_t * sname() const
Getter for the sname field.
Definition: bootp.h:176
uint16_t secs() const
Getter for the secs field.
Definition: bootp.h:135
void chaddr(const HWAddress< n > &new_chaddr)
Setter for the chaddr field. The new_chaddr pointer must be at least BOOTP::hlen() bytes long...
Definition: bootp.h:270
Represents a hardware address.
Definition: hw_address.h:65
ipaddress_type yiaddr() const
Getter for the yiaddr field.
Definition: bootp.h:152
HWAddress< 16 > chaddr_type
Definition: bootp.h:58
BootP * clone() const
Definition: bootp.h:321
ipaddress_type ciaddr() const
Getter for the ciaddr field.
Definition: bootp.h:146
ipaddress_type siaddr() const
Getter for the siaddr field.
Definition: bootp.h:158
uint8_t hops() const
Getter for the hops field.
Definition: bootp.h:123
uint16_t padding() const
Getter for the padding field.
Definition: bootp.h:140
OpCodes
Enum which contains the different opcodes BootP messages.
Definition: bootp.h:73
PDUType pdu_type() const
Getter for the PDU's type.
Definition: bootp.h:316
IPv4Address ipaddress_type
Definition: bootp.h:53
Abstraction of an IPv4 address.
Definition: ip_address.h:44
const vend_type & vend() const
Getter for the vend field.
Definition: bootp.h:188
Represents a BootP PDU.
Definition: bootp.h:48
uint8_t opcode() const
Getter for the opcode field.
Definition: bootp.h:105
Base class for protocol data units.
Definition: pdu.h:108
const uint8_t * file() const
Getter for the file field.
Definition: bootp.h:182