Kea 2.7.5
pkt4.h
Go to the documentation of this file.
1// Copyright (C) 2011-2024 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
130 std::string toText() const;
131
138 size_t len();
139
143 void setHops(uint8_t hops) { hops_ = hops; }
144
148 uint8_t getHops() const { return (hops_); }
149
150 // Note: There's no need to manipulate OP field directly,
151 // thus no setOp() method. See op_ comment.
152
156 uint8_t getOp() const { return (op_); }
157
161 void setSecs(uint16_t secs) { secs_ = secs; }
162
166 uint16_t getSecs() const { return (secs_); }
167
171 void setFlags(uint16_t flags) { flags_ = flags; }
172
176 uint16_t getFlags() const { return (flags_); }
177
182 getCiaddr() const { return (ciaddr_); }
183
187 void
188 setCiaddr(const isc::asiolink::IOAddress& ciaddr) { ciaddr_ = ciaddr; }
189
194 getSiaddr() const { return (siaddr_); }
195
199 void
200 setSiaddr(const isc::asiolink::IOAddress& siaddr) { siaddr_ = siaddr; }
201
206 getYiaddr() const { return (yiaddr_); }
207
211 void
212 setYiaddr(const isc::asiolink::IOAddress& yiaddr) { yiaddr_ = yiaddr; }
213
218 getGiaddr() const { return (giaddr_); }
219
223 void
224 setGiaddr(const isc::asiolink::IOAddress& giaddr) { giaddr_ = giaddr; }
225
232 uint8_t getType() const;
233
237 void setType(uint8_t type);
238
249 static const char* getName(const uint8_t type);
250
256 const char* getName() const;
257
264 const OptionBuffer
265 getSname() const { return (std::vector<uint8_t>(sname_, &sname_[MAX_SNAME_LEN])); }
266
271 void setSname(const uint8_t* sname, size_t sname_len);
272
279 const OptionBuffer
280 getFile() const { return (std::vector<uint8_t>(file_, &file_[MAX_FILE_LEN])); }
281
286 void
287 setFile(const uint8_t* file, size_t file_len);
288
300 void setHWAddr(uint8_t htype, uint8_t hlen,
301 const std::vector<uint8_t>& mac_addr);
302
308 void setHWAddr(const HWAddrPtr& addr);
309
313 uint8_t
314 getHtype() const;
315
319 uint8_t
320 getHlen() const;
321
324 HWAddrPtr getHWAddr() const { return (hwaddr_); }
325
332 std::string getHWAddrLabel() const;
333
342 virtual void
343 addOption(const OptionPtr& opt);
344
355 void setLocalHWAddr(const uint8_t htype, const uint8_t hlen,
356 const std::vector<uint8_t>& mac_addr);
357
367 void setLocalHWAddr(const HWAddrPtr& addr);
368
373 return (local_hwaddr_);
374 }
375
385 std::list<uint16_t>& getDeferredOptions() {
386 return (deferred_options_);
387 }
388
399 bool isRelayed() const;
400
405 virtual bool isDhcp4o6() const {
406 return (false);
407 }
408
409private:
410
422 virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
423 const std::vector<uint8_t>& mac_addr,
424 HWAddrPtr& hw_addr);
425
426protected:
427
433 uint8_t
434 DHCPTypeToBootpType(uint8_t dhcpType);
435
445 return (HWAddrPtr());
446 }
447
457 return (HWAddrPtr());
458 }
459
468 return (HWAddrPtr());
469 }
470
479 return (HWAddrPtr());
480 }
481
490 return (HWAddrPtr());
491 }
492
500 return (HWAddrPtr());
501 }
502
505
506 // @brief List of deferred option codes
507 std::list<uint16_t> deferred_options_;
508
516 uint8_t op_;
517
523
525 uint8_t hops_;
526
528 uint16_t secs_;
529
531 uint16_t flags_;
532
535
538
541
544
547
550
551 // end of real DHCPv4 fields
552}; // Pkt4 class
553
555typedef boost::shared_ptr<Pkt4> Pkt4Ptr;
556
557} // isc::dhcp namespace
558} // isc namespace
559
560#endif
Defines elements for storing the names of client classes.
Represents DHCPv4 packet.
Definition pkt4.h:37
virtual void addOption(const OptionPtr &opt)
Add an option.
Definition pkt4.cc:589
std::string toText() const
Returns text representation of the packet.
Definition pkt4.cc:420
void setCiaddr(const isc::asiolink::IOAddress &ciaddr)
Sets ciaddr field.
Definition pkt4.h:188
virtual void unpack()
Parses on-wire form of DHCPv4 packet.
Definition pkt4.cc:151
const isc::asiolink::IOAddress & getSiaddr() const
Returns siaddr field.
Definition pkt4.h:194
const isc::asiolink::IOAddress & getYiaddr() const
Returns yiaddr field.
Definition pkt4.h:206
uint8_t getHops() const
Returns hops field.
Definition pkt4.h:148
std::list< uint16_t > deferred_options_
Definition pkt4.h:507
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()
No-op.
Definition pkt4.h:444
HWAddrPtr hwaddr_
link-layer address and hardware information represents 3 fields: htype (hardware type,...
Definition pkt4.h:522
void setHops(uint8_t hops)
Sets hops field.
Definition pkt4.h:143
const char * getName() const
Returns name of the DHCP message.
Definition pkt4.cc:357
HWAddrPtr local_hwaddr_
local HW address (dst if receiving packet, src if sending packet)
Definition pkt4.h:504
void setSecs(uint16_t secs)
Sets secs field.
Definition pkt4.h:161
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:493
std::string getHWAddrLabel() const
Returns text representation of the hardware address.
Definition pkt4.cc:605
virtual HWAddrPtr getMACFromDUID()
No-op.
Definition pkt4.h:478
uint8_t DHCPTypeToBootpType(uint8_t dhcpType)
converts DHCP message type to BOOTP op type
Definition pkt4.cc:545
const isc::asiolink::IOAddress & getGiaddr() const
Returns giaddr field.
Definition pkt4.h:218
std::string getLabel() const
Returns text representation of the primary packet identifiers.
Definition pkt4.cc:365
virtual void pack()
Prepares on-wire format of DHCPv4 packet.
Definition pkt4.cc:71
isc::asiolink::IOAddress giaddr_
giaddr field (32 bits): Gateway IP address
Definition pkt4.h:543
uint8_t file_[MAX_FILE_LEN]
file field (128 bytes)
Definition pkt4.h:549
const isc::asiolink::IOAddress & getCiaddr() const
Returns ciaddr field.
Definition pkt4.h:182
uint8_t op_
message operation code
Definition pkt4.h:516
uint16_t getSecs() const
Returns secs field.
Definition pkt4.h:166
uint8_t hops_
Number of relay agents traversed.
Definition pkt4.h:525
const OptionBuffer getFile() const
Returns file field.
Definition pkt4.h:280
const OptionBuffer getSname() const
Returns sname field.
Definition pkt4.h:265
std::list< uint16_t > & getDeferredOptions()
Returns a reference to option codes which unpacking will be deferred.
Definition pkt4.h:385
HWAddrPtr getHWAddr() const
returns hardware address information
Definition pkt4.h:324
virtual HWAddrPtr getMACFromDocsisCMTS()
No-op.
Definition pkt4.h:489
void setSname(const uint8_t *sname, size_t sname_len)
Sets sname field.
Definition pkt4.cc:508
Pkt4(uint8_t msg_type, uint32_t transid)
Constructor, used in replying to a message.
Definition pkt4.cc:32
uint8_t getHlen() const
Returns hlen field.
Definition pkt4.cc:580
void setYiaddr(const isc::asiolink::IOAddress &yiaddr)
Sets yiaddr field.
Definition pkt4.h:212
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:600
void setFile(const uint8_t *file, size_t file_len)
Sets file field.
Definition pkt4.cc:526
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:397
void setSiaddr(const isc::asiolink::IOAddress &siaddr)
Sets siaddr field.
Definition pkt4.h:200
uint8_t getType() const
Returns DHCP message type (e.g.
Definition pkt4.cc:234
uint16_t getFlags() const
Returns flags field.
Definition pkt4.h:176
void setType(uint8_t type)
Sets DHCP message type (e.g.
Definition pkt4.cc:251
HWAddrPtr getLocalHWAddr() const
Returns local HW address.
Definition pkt4.h:372
isc::asiolink::IOAddress siaddr_
siaddr field (32 bits): next server IP address in boot process(e.g.TFTP)
Definition pkt4.h:540
size_t len()
Returns the size of the required buffer to build the packet.
Definition pkt4.cc:59
uint16_t secs_
elapsed (number of seconds since beginning of transmission)
Definition pkt4.h:528
isc::asiolink::IOAddress ciaddr_
ciaddr field (32 bits): Client's IP address
Definition pkt4.h:534
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:537
uint8_t sname_[MAX_SNAME_LEN]
sname field (64 bytes)
Definition pkt4.h:546
virtual HWAddrPtr getMACFromDocsisModem()
No-op.
Definition pkt4.h:467
virtual HWAddrPtr getMACFromRemoteIdRelayOption()
No-op.
Definition pkt4.h:499
virtual bool isDhcp4o6() const
Checks if a DHCPv4 message has been transported over DHCPv6.
Definition pkt4.h:405
uint8_t getOp() const
Returns op field.
Definition pkt4.h:156
void setHWAddr(uint8_t htype, uint8_t hlen, const std::vector< uint8_t > &mac_addr)
Sets hardware address.
Definition pkt4.cc:458
uint8_t getHtype() const
Returns htype field.
Definition pkt4.cc:572
void setFlags(uint16_t flags)
Sets flags field.
Definition pkt4.h:171
virtual HWAddrPtr getMACFromIPv6RelayOpt()
No-op.
Definition pkt4.h:456
uint16_t flags_
flags
Definition pkt4.h:531
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:224
Base class for classes representing DHCP messages.
Definition pkt.h:161
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:555
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.