12#include <netinet/ip_icmp.h>
23 : source_(
IOAddress::IPV4_ZERO_ADDRESS()),
24 destination_(
IOAddress::IPV4_ZERO_ADDRESS()),
25 msg_type_(0), code_(0), check_sum_(0), id_(0), sequence_(0),
32 if (length <
sizeof(
struct ip)) {
34 "ICMPMsg::unpack - truncated ip header, length: "
39 struct ip const* ip_header =
reinterpret_cast<struct ip const*
>(wire_data);
40 auto hlen = (ip_header->ip_hl << 2);
43 if (length < (hlen +
sizeof(
struct icmp))) {
45 << length <<
", hlen: " << hlen);
49 msg->setSource(
IOAddress(ntohl(ip_header->ip_src.s_addr)));
50 msg->setDestination(
IOAddress(ntohl(ip_header->ip_dst.s_addr)));
53 struct icmp const* reply =
54 reinterpret_cast<struct icmp const*
>(wire_data + hlen);
55 msg->setType(reply->icmp_type);
56 msg->setCode(reply->icmp_code);
58 msg->setChecksum(ntohs(reply->icmp_cksum));
59 msg->setId(ntohs(reply->icmp_hun.ih_idseq.icd_id));
60 msg->setSequence(ntohs(reply->icmp_hun.ih_idseq.icd_seq));
63 const uint8_t* idata =
reinterpret_cast<const uint8_t*
>(&reply->icmp_dun);
64 msg->setPayload(idata, payload_len);
71 ICMPPtr outbound(
new struct icmp());
72 memset(outbound.get(), 0x00,
sizeof(
struct icmp));
73 outbound->icmp_type = msg_type_;
74 outbound->icmp_id = htons(id_);
75 outbound->icmp_seq = htons(sequence_);
77 uint8_t* odata =
reinterpret_cast<uint8_t*
>(outbound.get());
78 outbound->icmp_cksum = htons(~
calcChecksum(odata,
sizeof(
struct icmp)));
84 payload_.insert(payload_.end(),
data,
data + length);
93 for (i = 0; i < (length & ~1U); i += 2) {
94 sum +=
static_cast<uint32_t
>(
readUint16(buf + i,
sizeof(uint16_t)));
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).
void setPayload(const uint8_t *data, size_t length)
Sets the message payload to the given data.
static ICMPMsgPtr unpack(const uint8_t *wire_data, size_t length)
Unpacks an ICMP message from the given wire_data.
ICMPPtr pack() const
Packs the message into an ICMP structure.
static uint32_t calcChecksum(const uint8_t *data, size_t length)
Calculates the checksum of the given data buffer.
static constexpr size_t ICMP_HEADER_SIZE
Size in octets of ICMP message header.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< ICMPMsg > ICMPMsgPtr
Shared pointer type for ICMPMsg.
boost::shared_ptr< struct icmp > ICMPPtr
Shared pointer type for struct icmp.
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Defines the logger used by the top-level component of kea-lfc.