11 #include <boost/static_assert.hpp>
15 #include <netinet/in_systm.h>
16 #include <netinet/ip.h>
30 <<
"packet is invalid, expected at least "
31 << ETHERNET_HEADER_LEN <<
" bytes, received "
44 BOOST_STATIC_ASSERT(ETHERNET_HEADER_LEN > HWAddr::ETHERNET_HWADDR_LEN);
50 std::vector<uint8_t> dest_addr;
51 buf.
readVector(dest_addr, HWAddr::ETHERNET_HWADDR_LEN);
52 pkt->setLocalHWAddr(HWTYPE_ETHERNET, HWAddr::ETHERNET_HWADDR_LEN, dest_addr);
54 std::vector<uint8_t> src_addr;
55 buf.
readVector(src_addr, HWAddr::ETHERNET_HWADDR_LEN);
56 pkt->setRemoteHWAddr(HWTYPE_ETHERNET, HWAddr::ETHERNET_HWADDR_LEN, src_addr);
67 <<
"received packet is invalid, expected at least "
68 << MIN_IP_HEADER_LEN + UDP_HEADER_LEN
79 BOOST_STATIC_ASSERT(IP_SRC_ADDR_OFFSET < MIN_IP_HEADER_LEN);
90 <<
" lower than 5 words. The length of the received header is "
91 << static_cast<unsigned>(ip_len) <<
".");
112 buf.
setPosition(start_pos + ip_len * 4 + UDP_HEADER_LEN);
118 HWAddrPtr remote_addr = pkt->getRemoteHWAddr();
120 if (remote_addr->hwaddr_.size() == HWAddr::ETHERNET_HWADDR_LEN) {
121 out_buf.
writeData(&remote_addr->hwaddr_[0],
122 HWAddr::ETHERNET_HWADDR_LEN);
125 << remote_addr->hwaddr_.size() <<
" when constructing"
126 <<
" an ethernet frame header; expected size is"
127 <<
" " << HWAddr::ETHERNET_HWADDR_LEN);
134 out_buf.
writeData(&std::vector<uint8_t>(HWAddr::ETHERNET_HWADDR_LEN)[0],
135 HWAddr::ETHERNET_HWADDR_LEN);
139 HWAddrPtr local_addr = pkt->getLocalHWAddr();
141 if (local_addr->hwaddr_.size() == HWAddr::ETHERNET_HWADDR_LEN) {
142 out_buf.
writeData(&local_addr->hwaddr_[0],
143 HWAddr::ETHERNET_HWADDR_LEN);
146 << local_addr->hwaddr_.size() <<
" when constructing"
147 <<
" an ethernet frame header; expected size is"
148 <<
" " << HWAddr::ETHERNET_HWADDR_LEN);
152 out_buf.
writeData(&std::vector<uint8_t>(HWAddr::ETHERNET_HWADDR_LEN)[0],
153 HWAddr::ETHERNET_HWADDR_LEN);
165 out_buf.
writeUint16(28 + pkt->getBuffer().getLength());
171 out_buf.
writeUint32(pkt->getLocalAddr().toUint32());
172 out_buf.
writeUint32(pkt->getRemoteAddr().toUint32());
177 uint32_t udp_len = 8 + pkt->getBuffer().getLength();
184 uint16_t pseudo_hdr_checksum =
186 8, IPPROTO_UDP + udp_len);
204 uint16_t udp_checksum =
206 calcChecksum(static_cast<const uint8_t*>(pkt->getBuffer().getData()),
207 pkt->getBuffer().getLength(),
208 pseudo_hdr_checksum));
214 calcChecksum(
const uint8_t* buf,
const uint32_t buf_size, uint32_t sum) {
216 for (i = 0; i < (buf_size & ~1U); i += 2) {
217 uint16_t chunk = buf[i] << 8 | buf[i + 1];
void writeEthernetHeader(const Pkt4Ptr &pkt, OutputBuffer &out_buf)
Writes ethernet frame header into a buffer.
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
uint16_t calcChecksum(const uint8_t *buf, const uint32_t buf_size, uint32_t sum)
Calculates checksum for provided buffer.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
void decodeEthernetHeader(InputBuffer &buf, Pkt4Ptr &pkt)
Decode the Ethernet header.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void writeUint16At(uint16_t data, size_t pos)
Write an unsigned 16-bit integer in host byte order at the specified position of the buffer in networ...
void writeIpUdpHeader(const Pkt4Ptr &pkt, util::OutputBuffer &out_buf)
Writes both IP and UDP header into output buffer.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order...
Defines the logger used by the top-level component of kea-dhcp-ddns.
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order...
The IOAddress class represents an IP addresses (version agnostic)
size_t getLength() const
Return the length of data written in the buffer.
void decodeIpUdpHeader(InputBuffer &buf, Pkt4Ptr &pkt)
Decode IP and UDP header.