libtins 4.5
Loading...
Searching...
No Matches
bootp.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_BOOTP_H
31#define TINS_BOOTP_H
32
33#include <stdint.h>
34#include <vector>
35#include <tins/pdu.h>
36#include <tins/macros.h>
37#include <tins/endianness.h>
38#include <tins/ip_address.h>
39#include <tins/hw_address.h>
40
41namespace Tins {
42
47class TINS_API BootP : public PDU {
48public:
53
58
62 typedef std::vector<uint8_t> vend_type;
63
67 static const PDU::PDUType pdu_flag = PDU::BOOTP;
68
72 enum OpCodes {
73 BOOTREQUEST = 1,
74 BOOTREPLY = 2
75 };
76
83 BootP();
84
96 BootP(const uint8_t* buffer, uint32_t total_sz, uint32_t vend_field_size = 64);
97
98 /* Getters */
99
104 uint8_t opcode() const { return bootp_.opcode; }
105
110 uint8_t htype() const { return bootp_.htype; }
111
116 uint8_t hlen() const { return bootp_.hlen; }
117
122 uint8_t hops() const { return bootp_.hops; }
123
128 uint32_t xid() const { return Endian::be_to_host(bootp_.xid); }
129
134 uint16_t secs() const { return Endian::be_to_host(bootp_.secs); }
135
139 uint16_t padding() const { return Endian::be_to_host(bootp_.padding); }
140
145 ipaddress_type ciaddr() const { return ipaddress_type(bootp_.ciaddr); }
146
151 ipaddress_type yiaddr() const { return ipaddress_type(bootp_.yiaddr); }
152
157 ipaddress_type siaddr() const { return ipaddress_type(bootp_.siaddr); }
158
163 ipaddress_type giaddr() const { return ipaddress_type(bootp_.giaddr); }
164
169 chaddr_type chaddr() const { return bootp_.chaddr; }
170
175 const uint8_t* sname() const { return bootp_.sname; }
176
181 const uint8_t* file() const { return bootp_.file; }
182
187 const vend_type& vend() const { return vend_; }
188
194 uint32_t header_size() const;
195 /* Setters */
196
201 void opcode(uint8_t code);
202
207 void htype(uint8_t type);
208
213 void hlen(uint8_t length);
214
219 void hops(uint8_t count);
220
225 void xid(uint32_t identifier);
226
231 void secs(uint16_t value);
232
237 void padding(uint16_t value);
238
243 void ciaddr(ipaddress_type address);
244
249 void yiaddr(ipaddress_type address);
250
255 void siaddr(ipaddress_type address);
256
261 void giaddr(ipaddress_type address);
262
268 template<size_t n>
269 void chaddr(const HWAddress<n>& new_chaddr) {
270 size_t copy_threshold = std::min(n, sizeof(bootp_.chaddr));
271 for (size_t i = 0; i < copy_threshold; ++i) {
272 if (i < copy_threshold) {
273 bootp_.chaddr[i] = new_chaddr[i];
274 }
275 else {
276 bootp_.chaddr[i] = 0;
277 }
278 }
279 }
280
285 void sname(const uint8_t* new_sname);
286
291 void file(const uint8_t* new_file);
292
297 void vend(const vend_type& newvend_);
298
308 bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
309
314 PDUType pdu_type() const { return pdu_flag; }
315
319 BootP* clone() const {
320 return new BootP(*this);
321 }
322protected:
331 vend_type& vend() { return vend_; }
332
333 void write_serialization(uint8_t* buffer, uint32_t total_sz);
334
338 TINS_BEGIN_PACK
340 uint8_t opcode;
341 uint8_t htype;
342 uint8_t hlen;
343 uint8_t hops;
344 uint32_t xid;
345 uint16_t secs;
346 uint16_t padding;
347 uint32_t ciaddr;
348 uint32_t yiaddr;
349 uint32_t siaddr;
350 uint32_t giaddr;
351 uint8_t chaddr[16];
352 uint8_t sname[64];
353 uint8_t file[128];
354 } TINS_END_PACK;
355
356private:
357 bootp_header bootp_;
358 vend_type vend_;
359};
360
361} // Tins
362
363#endif // TINS_BOOTP_H
Represents a BootP PDU.
Definition bootp.h:47
PDUType pdu_type() const
Getter for the PDU's type.
Definition bootp.h:314
vend_type & vend()
Getter for the vend field.
Definition bootp.h:331
ipaddress_type yiaddr() const
Getter for the yiaddr field.
Definition bootp.h:151
chaddr_type chaddr() const
Getter for the chaddr field.
Definition bootp.h:169
uint8_t hops() const
Getter for the hops field.
Definition bootp.h:122
uint16_t padding() const
Getter for the padding field.
Definition bootp.h:139
ipaddress_type siaddr() const
Getter for the siaddr field.
Definition bootp.h:157
uint32_t xid() const
Getter for the xid field.
Definition bootp.h:128
BootP * clone() const
Definition bootp.h:319
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:269
HWAddress< 16 > chaddr_type
Definition bootp.h:57
const uint8_t * file() const
Getter for the file field.
Definition bootp.h:181
const vend_type & vend() const
Getter for the vend field.
Definition bootp.h:187
std::vector< uint8_t > vend_type
Definition bootp.h:62
uint16_t secs() const
Getter for the secs field.
Definition bootp.h:134
const uint8_t * sname() const
Getter for the sname field.
Definition bootp.h:175
ipaddress_type giaddr() const
Getter for the giaddr field.
Definition bootp.h:163
uint8_t opcode() const
Getter for the opcode field.
Definition bootp.h:104
OpCodes
Enum which contains the different opcodes BootP messages.
Definition bootp.h:72
ipaddress_type ciaddr() const
Getter for the ciaddr field.
Definition bootp.h:145
uint8_t htype() const
Getter for the htype field.
Definition bootp.h:110
uint8_t hlen() const
Getter for the hlen field.
Definition bootp.h:116
IPv4Address ipaddress_type
Definition bootp.h:52
Represents a hardware address.
Definition resolve_utils.h:43
Abstraction of an IPv4 address.
Definition ip_address.h:45
Base class for protocol data units.
Definition pdu.h:107
PDUType
Enum which identifies each type of PDU.
Definition pdu.h:127
The Tins namespace.
Definition address_range.h:38
Definition bootp.h:339