Kea 3.1.1
pkt4.h
Go to the documentation of this file.
1// Copyright (C) 2011-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef PKT4_H
8#define PKT4_H
9
10#include <asiolink/io_address.h>
11#include <dhcp/duid.h>
12#include <util/buffer.h>
13#include <dhcp/option.h>
14#include <dhcp/classify.h>
15#include <dhcp/pkt.h>
16
17#include <boost/shared_ptr.hpp>
18
19#include <iostream>
20#include <vector>
21#include <set>
22#include <list>
23
24#include <time.h>
25
26namespace isc {
27
28namespace dhcp {
29
37class Pkt4 : public Pkt {
38public:
39
41 const static size_t MAX_CHADDR_LEN = 16;
42
44 const static size_t MAX_SNAME_LEN = 64;
45
47 const static size_t MAX_FILE_LEN = 128;
48
50 const static size_t DHCPV4_PKT_HDR_LEN = 236;
51
54 const static uint16_t FLAG_BROADCAST_MASK = 0x8000;
55
60 Pkt4(uint8_t msg_type, uint32_t transid);
61
69 Pkt4(const uint8_t* data, size_t len);
70
79 virtual void pack();
80
89 virtual void unpack();
90
101 std::string getLabel() const;
102
112 static std::string makeLabel(const HWAddrPtr& hwaddr,
113 const ClientIdPtr& client_id,
114 const uint32_t transid);
115
123 static std::string makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id);
124
131 std::string toText(bool verbose = false) const;
132
139 size_t len();
140
144 void setHops(uint8_t hops) { hops_ = hops; }
145
149 uint8_t getHops() const { return (hops_); }
150
151 // Note: There's no need to manipulate OP field directly,
152 // thus no setOp() method. See op_ comment.
153
157 uint8_t getOp() const { return (op_); }
158
162 void setSecs(uint16_t secs) { secs_ = secs; }
163
167 uint16_t getSecs() const { return (secs_); }
168
172 void setFlags(uint16_t flags) { flags_ = flags; }
173
177 uint16_t getFlags() const { return (flags_); }
178
183 getCiaddr() const { return (ciaddr_); }
184
188 void
189 setCiaddr(const isc::asiolink::IOAddress& ciaddr) { ciaddr_ = ciaddr; }
190
195 getSiaddr() const { return (siaddr_); }
196
200 void
201 setSiaddr(const isc::asiolink::IOAddress& siaddr) { siaddr_ = siaddr; }
202
207 getYiaddr() const { return (yiaddr_); }
208
212 void
213 setYiaddr(const isc::asiolink::IOAddress& yiaddr) { yiaddr_ = yiaddr; }
214
219 getGiaddr() const { return (giaddr_); }
220
224 void
225 setGiaddr(const isc::asiolink::IOAddress& giaddr) { giaddr_ = giaddr; }
226
233 uint8_t getType() const;
234
238 void setType(uint8_t type);
239
250 static const char* getName(const uint8_t type);
251
257 const char* getName() const;
258
265 const OptionBuffer
266 getSname() const { return (std::vector<uint8_t>(sname_, &sname_[MAX_SNAME_LEN])); }
267
272 void setSname(const uint8_t* sname, size_t sname_len);
273
280 const OptionBuffer
281 getFile() const { return (std::vector<uint8_t>(file_, &file_[MAX_FILE_LEN])); }
282
287 void
288 setFile(const uint8_t* file, size_t file_len);
289
301 void setHWAddr(uint8_t htype, uint8_t hlen,
302 const std::vector<uint8_t>& mac_addr);
303
309 void setHWAddr(const HWAddrPtr& addr);
310
314 uint8_t
315 getHtype() const;
316
320 uint8_t
321 getHlen() const;
322
325 HWAddrPtr getHWAddr() const { return (hwaddr_); }
326
333 std::string getHWAddrLabel() const;
334
343 virtual void
344 addOption(const OptionPtr& opt);
345
356 void setLocalHWAddr(const uint8_t htype, const uint8_t hlen,
357 const std::vector<uint8_t>& mac_addr);
358
368 void setLocalHWAddr(const HWAddrPtr& addr);
369
374 return (local_hwaddr_);
375 }
376
386 std::list<uint16_t>& getDeferredOptions() {
387 return (deferred_options_);
388 }
389
400 bool isRelayed() const;
401
406 virtual bool isDhcp4o6() const {
407 return (false);
408 }
409
410private:
411
423 virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
424 const std::vector<uint8_t>& mac_addr,
425 HWAddrPtr& hw_addr);
426
427protected:
428
434 uint8_t
435 DHCPTypeToBootpType(uint8_t dhcpType);
436
446 return (HWAddrPtr());
447 }
448
458 return (HWAddrPtr());
459 }
460
469 return (HWAddrPtr());
470 }
471
480 return (HWAddrPtr());
481 }
482
491 return (HWAddrPtr());
492 }
493
501 return (HWAddrPtr());
502 }
503
506
507 // @brief List of deferred option codes
508 std::list<uint16_t> deferred_options_;
509
517 uint8_t op_;
518
524
526 uint8_t hops_;
527
529 uint16_t secs_;
530
532 uint16_t flags_;
533
536
539
542
545
548
551
552 // end of real DHCPv4 fields
553}; // Pkt4 class
554
556typedef boost::shared_ptr<Pkt4> Pkt4Ptr;
557
558} // isc::dhcp namespace
559} // isc namespace
560
561#endif
Defines elements for storing the names of client classes.
virtual void addOption(const OptionPtr &opt)
Add an option.
Definition pkt4.cc:621
void setCiaddr(const isc::asiolink::IOAddress &ciaddr)
Sets ciaddr field.
Definition pkt4.h:189
virtual void unpack()
Parses on-wire form of DHCPv4 packet.
Definition pkt4.cc:153
const isc::asiolink::IOAddress & getSiaddr() const
Returns siaddr field.
Definition pkt4.h:195
const isc::asiolink::IOAddress & getYiaddr() const
Returns yiaddr field.
Definition pkt4.h:207
uint8_t getHops() const
Returns hops field.
Definition pkt4.h:149
std::list< uint16_t > deferred_options_
Definition pkt4.h:508
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()
No-op.
Definition pkt4.h:445
HWAddrPtr hwaddr_
link-layer address and hardware information represents 3 fields: htype (hardware type,...
Definition pkt4.h:523
void setHops(uint8_t hops)
Sets hops field.
Definition pkt4.h:144
const char * getName() const
Returns name of the DHCP message.
Definition pkt4.cc:359
HWAddrPtr local_hwaddr_
local HW address (dst if receiving packet, src if sending packet)
Definition pkt4.h:505
void setSecs(uint16_t secs)
Sets secs field.
Definition pkt4.h:162
static const size_t MAX_CHADDR_LEN
length of the CHADDR field in DHCPv4 message
Definition pkt4.h:41
void setLocalHWAddr(const uint8_t htype, const uint8_t hlen, const std::vector< uint8_t > &mac_addr)
Sets local HW address.
Definition pkt4.cc:525
std::string getHWAddrLabel() const
Returns text representation of the hardware address.
Definition pkt4.cc:637
virtual HWAddrPtr getMACFromDUID()
No-op.
Definition pkt4.h:479
uint8_t DHCPTypeToBootpType(uint8_t dhcpType)
converts DHCP message type to BOOTP op type
Definition pkt4.cc:577
const isc::asiolink::IOAddress & getGiaddr() const
Returns giaddr field.
Definition pkt4.h:219
std::string getLabel() const
Returns text representation of the primary packet identifiers.
Definition pkt4.cc:367
virtual void pack()
Prepares on-wire format of DHCPv4 packet.
Definition pkt4.cc:73
isc::asiolink::IOAddress giaddr_
giaddr field (32 bits): Gateway IP address
Definition pkt4.h:544
uint8_t file_[MAX_FILE_LEN]
file field (128 bytes)
Definition pkt4.h:550
const isc::asiolink::IOAddress & getCiaddr() const
Returns ciaddr field.
Definition pkt4.h:183
uint8_t op_
message operation code
Definition pkt4.h:517
uint16_t getSecs() const
Returns secs field.
Definition pkt4.h:167
uint8_t hops_
Number of relay agents traversed.
Definition pkt4.h:526
const OptionBuffer getFile() const
Returns file field.
Definition pkt4.h:281
const OptionBuffer getSname() const
Returns sname field.
Definition pkt4.h:266
std::list< uint16_t > & getDeferredOptions()
Returns a reference to option codes which unpacking will be deferred.
Definition pkt4.h:386
HWAddrPtr getHWAddr() const
returns hardware address information
Definition pkt4.h:325
virtual HWAddrPtr getMACFromDocsisCMTS()
No-op.
Definition pkt4.h:490
void setSname(const uint8_t *sname, size_t sname_len)
Sets sname field.
Definition pkt4.cc:540
Pkt4(uint8_t msg_type, uint32_t transid)
Constructor, used in replying to a message.
Definition pkt4.cc:34
uint8_t getHlen() const
Returns hlen field.
Definition pkt4.cc:612
void setYiaddr(const isc::asiolink::IOAddress &yiaddr)
Sets yiaddr field.
Definition pkt4.h:213
static const size_t DHCPV4_PKT_HDR_LEN
specifies DHCPv4 packet header length (fixed part)
Definition pkt4.h:50
bool isRelayed() const
Checks if a DHCPv4 message has been relayed.
Definition pkt4.cc:632
void setFile(const uint8_t *file, size_t file_len)
Sets file field.
Definition pkt4.cc:558
static const size_t MAX_SNAME_LEN
length of the SNAME field in DHCPv4 message
Definition pkt4.h:44
static std::string makeLabel(const HWAddrPtr &hwaddr, const ClientIdPtr &client_id, const uint32_t transid)
Returns text representation of the given packet identifiers.
Definition pkt4.cc:399
void setSiaddr(const isc::asiolink::IOAddress &siaddr)
Sets siaddr field.
Definition pkt4.h:201
uint8_t getType() const
Returns DHCP message type (e.g.
Definition pkt4.cc:236
uint16_t getFlags() const
Returns flags field.
Definition pkt4.h:177
void setType(uint8_t type)
Sets DHCP message type (e.g.
Definition pkt4.cc:253
HWAddrPtr getLocalHWAddr() const
Returns local HW address.
Definition pkt4.h:373
isc::asiolink::IOAddress siaddr_
siaddr field (32 bits): next server IP address in boot process(e.g.TFTP)
Definition pkt4.h:541
size_t len()
Returns the size of the required buffer to build the packet.
Definition pkt4.cc:61
uint16_t secs_
elapsed (number of seconds since beginning of transmission)
Definition pkt4.h:529
isc::asiolink::IOAddress ciaddr_
ciaddr field (32 bits): Client's IP address
Definition pkt4.h:535
std::string toText(bool verbose=false) const
Returns text representation of the packet.
Definition pkt4.cc:422
static const uint16_t FLAG_BROADCAST_MASK
Mask for the value of flags field in the DHCPv4 message to check whether client requested broadcast r...
Definition pkt4.h:54
isc::asiolink::IOAddress yiaddr_
yiaddr field (32 bits): Client's IP address ("your"), set by server
Definition pkt4.h:538
uint8_t sname_[MAX_SNAME_LEN]
sname field (64 bytes)
Definition pkt4.h:547
virtual HWAddrPtr getMACFromDocsisModem()
No-op.
Definition pkt4.h:468
virtual HWAddrPtr getMACFromRemoteIdRelayOption()
No-op.
Definition pkt4.h:500
virtual bool isDhcp4o6() const
Checks if a DHCPv4 message has been transported over DHCPv6.
Definition pkt4.h:406
uint8_t getOp() const
Returns op field.
Definition pkt4.h:157
void setHWAddr(uint8_t htype, uint8_t hlen, const std::vector< uint8_t > &mac_addr)
Sets hardware address.
Definition pkt4.cc:490
uint8_t getHtype() const
Returns htype field.
Definition pkt4.cc:604
void setFlags(uint16_t flags)
Sets flags field.
Definition pkt4.h:172
virtual HWAddrPtr getMACFromIPv6RelayOpt()
No-op.
Definition pkt4.h:457
uint16_t flags_
flags
Definition pkt4.h:532
static const size_t MAX_FILE_LEN
length of the FILE field in DHCPv4 message
Definition pkt4.h:47
void setGiaddr(const isc::asiolink::IOAddress &giaddr)
Sets giaddr field.
Definition pkt4.h:225
Pkt(uint32_t transid, const isc::asiolink::IOAddress &local_addr, const isc::asiolink::IOAddress &remote_addr, uint16_t local_port, uint16_t remote_port)
Constructor.
Definition pkt.cc:24
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:154
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition duid.h:216
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
Defines the logger used by the top-level component of kea-lfc.