25#ifndef JOIN_FABRIC_DHCP_MESSAGE_HPP
26#define JOIN_FABRIC_DHCP_MESSAGE_HPP
36#include <net/ethernet.h>
37#include <netinet/in.h>
147 const char* end = data + maxSize;
150 uint8_t head[4] = {packet.
op,
_ethernet, ETH_ALEN, 0};
151 ok &=
writeBytes (cur, end, head,
sizeof (head));
153 uint32_t
id = htonl (packet.
id);
154 ok &=
writeBytes (cur, end, &
id,
sizeof (
id));
156 uint16_t secs = htons (packet.
secs);
157 ok &=
writeBytes (cur, end, &secs,
sizeof (secs));
159 uint16_t flags = htons (packet.
flags);
160 ok &=
writeBytes (cur, end, &flags,
sizeof (flags));
169 ok &=
writeBytes (cur, end, chaddr,
sizeof (chaddr));
172 ok &=
writeBytes (cur, end, padding,
sizeof (padding));
175 ok &=
writeBytes (cur, end, &cookie,
sizeof (cookie));
182 const size_t written =
static_cast<size_t> (cur - data);
206 const char* cur = data;
207 const char* end = data + size;
209 uint8_t head[4] = {};
210 if (!
readBytes (cur, end, head,
sizeof (head)))
217 (head[2] != ETH_ALEN))
224 uint16_t secs = 0, flags = 0;
226 if (!
readBytes (cur, end, &
id,
sizeof (
id)) || !
readBytes (cur, end, &secs,
sizeof (secs)) ||
227 !
readBytes (cur, end, &flags,
sizeof (flags)))
232 packet.
id = ntohl (
id);
233 packet.
secs = ntohs (secs);
234 packet.
flags = ntohs (flags);
243 if (!
readBytes (cur, end, chaddr,
sizeof (chaddr)))
253 if (!
readBytes (cur, end, sname,
sizeof (sname)) || !
readBytes (cur, end, file,
sizeof (file)) ||
254 !
readBytes (cur, end, &cookie,
sizeof (cookie)))
326 static bool writeHead (
char*& cur,
const char* end, uint8_t code,
size_t size);
337 struct in_addr addr = {};
339 if (
address.family () == AF_INET)
341 ::memcpy (&addr,
address.addr (), sizeof (addr));
344 return writeBytes (cur, end, &addr,
sizeof (addr));
356 struct in_addr addr = {};
358 if (!
readBytes (cur, end, &addr,
sizeof (addr)))
DHCP message codec.
Definition dhcp_message.hpp:88
Operation
DHCP operation codes.
Definition dhcp_message.hpp:109
@ BootReply
Definition dhcp_message.hpp:111
@ BootRequest
Definition dhcp_message.hpp:110
static constexpr uint32_t magicCookie
magic cookie introducing the option field.
Definition dhcp_message.hpp:288
Overload
option overload values.
Definition dhcp_message.hpp:126
@ SnameOverload
Definition dhcp_message.hpp:128
@ FileOverload
Definition dhcp_message.hpp:127
ssize_t serialize(const DhcpPacket &packet, char *data, size_t maxSize) const
serialize a DHCP message into a buffer.
Definition dhcp_message.hpp:138
MessageType
DHCP message types, RFC 2132 section 9.6.
Definition dhcp_message.hpp:94
@ Request
Definition dhcp_message.hpp:97
@ Nak
Definition dhcp_message.hpp:100
@ Discover
Definition dhcp_message.hpp:95
@ Ack
Definition dhcp_message.hpp:99
@ Decline
Definition dhcp_message.hpp:98
@ Offer
Definition dhcp_message.hpp:96
@ Inform
Definition dhcp_message.hpp:102
@ Release
Definition dhcp_message.hpp:101
static constexpr size_t _fileSize
size of the boot file name field.
Definition dhcp_message.hpp:378
static bool writeHead(char *&cur, const char *end, uint8_t code, size_t size)
write an option header into a buffer.
Definition dhcp_message.cpp:40
Flag
message flags.
Definition dhcp_message.hpp:118
@ BroadcastFlag
Definition dhcp_message.hpp:119
static constexpr size_t _snameSize
size of the server name field.
Definition dhcp_message.hpp:375
static constexpr size_t _chaddrSize
size of the client hardware address field.
Definition dhcp_message.hpp:372
int deserialize(DhcpPacket &packet, const char *data, size_t size) const
deserialize a DHCP message from a buffer.
Definition dhcp_message.hpp:202
static constexpr size_t minMsgSize
smallest message a BOOTP implementation accepts, RFC 951 and RFC 1542 section 2.1.
Definition dhcp_message.hpp:294
static constexpr size_t maxOptionSize
biggest payload a single option can carry, its length field is one octet.
Definition dhcp_message.hpp:297
static constexpr size_t headerSize
size of the fixed part of a DHCP message, magic cookie included.
Definition dhcp_message.hpp:291
static constexpr uint8_t _ethernet
hardware address type of an Ethernet link.
Definition dhcp_message.hpp:369
static bool writeAddress(char *&cur, const char *end, const IpAddress &address)
write an IPv4 address into a buffer.
Definition dhcp_message.hpp:335
static bool readAddress(const char *&cur, const char *end, IpAddress &address)
read an IPv4 address from a buffer.
Definition dhcp_message.hpp:354
DHCP option list.
Definition dhcp_option.hpp:64
@ OptionOverload
Definition dhcp_option.hpp:123
const T * getIf(uint8_t code) const
get the value identified by code if it is present and holds the requested type.
Definition dhcp_option.hpp:246
void clear()
remove all elements from the list.
Definition dhcp_option.hpp:339
IPv6, IPv4 address class.
Definition ip_address.hpp:52
MAC address class.
Definition mac_address.hpp:47
const uint8_t * addr() const
get the internal MAC address array address.
Definition mac_address.cpp:164
Definition acceptor.hpp:32
bool readBytes(const char *&cur, const char *end, void *out, size_t size) noexcept
read bytes from a buffer.
Definition utils.hpp:276
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
bool writeBytes(char *&cur, const char *end, const void *in, size_t size) noexcept
write bytes to a buffer.
Definition utils.hpp:298
DHCP message.
Definition dhcp_message.hpp:46
uint16_t flags
message flags.
Definition dhcp_message.hpp:75
MacAddress hardware
client hardware address, carried by the message itself.
Definition dhcp_message.hpp:54
IpAddress client
client IP address, set when the client already owns a lease.
Definition dhcp_message.hpp:57
uint8_t op
operation code.
Definition dhcp_message.hpp:78
MacAddress src
link layer source address, set by the transport.
Definition dhcp_message.hpp:48
IpAddress your
IP address the server assigns to the client.
Definition dhcp_message.hpp:60
DhcpOption options
message options.
Definition dhcp_message.hpp:81
uint16_t secs
seconds elapsed since the client started the acquisition.
Definition dhcp_message.hpp:72
MacAddress dest
link layer destination address, set by the transport.
Definition dhcp_message.hpp:51
IpAddress gateway
IP address of the relay agent the message went through.
Definition dhcp_message.hpp:66
IpAddress server
IP address of the next server to use at boot time.
Definition dhcp_message.hpp:63
uint32_t id
transaction identifier.
Definition dhcp_message.hpp:69
overload resolution.
Definition traits.hpp:38
IpAddress address
Definition tcp_acceptor_test.cpp:35