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) {
122 << remote_addr->hwaddr_.size() <<
" when constructing"
123 <<
" an ethernet frame header; expected size is"
124 <<
" " << HWAddr::ETHERNET_HWADDR_LEN);
125 }
else if (!pkt->isRelayed() &&
126 (pkt->getFlags() & Pkt4::FLAG_BROADCAST_MASK)) {
127 out_buf.
writeData(&std::vector<uint8_t>(HWAddr::ETHERNET_HWADDR_LEN,255)[0],
128 HWAddr::ETHERNET_HWADDR_LEN);
130 out_buf.
writeData(&remote_addr->hwaddr_[0],
131 HWAddr::ETHERNET_HWADDR_LEN);
138 out_buf.
writeData(&std::vector<uint8_t>(HWAddr::ETHERNET_HWADDR_LEN)[0],
139 HWAddr::ETHERNET_HWADDR_LEN);
143 HWAddrPtr local_addr = pkt->getLocalHWAddr();
145 if (local_addr->hwaddr_.size() == HWAddr::ETHERNET_HWADDR_LEN) {
146 out_buf.
writeData(&local_addr->hwaddr_[0],
147 HWAddr::ETHERNET_HWADDR_LEN);
150 << local_addr->hwaddr_.size() <<
" when constructing"
151 <<
" an ethernet frame header; expected size is"
152 <<
" " << HWAddr::ETHERNET_HWADDR_LEN);
156 out_buf.
writeData(&std::vector<uint8_t>(HWAddr::ETHERNET_HWADDR_LEN)[0],
157 HWAddr::ETHERNET_HWADDR_LEN);
169 out_buf.
writeUint16(28 + pkt->getBuffer().getLength());
175 out_buf.
writeUint32(pkt->getLocalAddr().toUint32());
176 out_buf.
writeUint32(pkt->getRemoteAddr().toUint32());
181 uint32_t udp_len = 8 + pkt->getBuffer().getLength();
188 uint16_t pseudo_hdr_checksum =
190 8, IPPROTO_UDP + udp_len);
208 uint16_t udp_checksum =
210 calcChecksum(
static_cast<const uint8_t*
>(pkt->getBuffer().getData()),
211 pkt->getBuffer().getLength(),
212 pseudo_hdr_checksum));
218 calcChecksum(
const uint8_t* buf,
const uint32_t buf_size, uint32_t sum) {
220 for (i = 0; i < (buf_size & ~1U); i += 2) {
221 uint16_t chunk = buf[i] << 8 | buf[i + 1];
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
The IOAddress class represents an IP addresses (version agnostic)
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
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...
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
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.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order.
size_t getLength() const
Return the length of data written in the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
void decodeEthernetHeader(InputBuffer &buf, Pkt4Ptr &pkt)
Decode the Ethernet header.
void writeEthernetHeader(const Pkt4Ptr &pkt, OutputBuffer &out_buf)
Writes ethernet frame header into a buffer.
void decodeIpUdpHeader(InputBuffer &buf, Pkt4Ptr &pkt)
Decode IP and UDP header.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
uint16_t calcChecksum(const uint8_t *buf, const uint32_t buf_size, uint32_t sum)
Calculates checksum for provided buffer.
void writeIpUdpHeader(const Pkt4Ptr &pkt, util::OutputBuffer &out_buf)
Writes both IP and UDP header into output buffer.
Defines the logger used by the top-level component of kea-lfc.