15#include <netinet/ip_icmp.h>
25 : source_(
IOAddress::IPV4_ZERO_ADDRESS()),
26 destination_(
IOAddress::IPV4_ZERO_ADDRESS()),
27 msg_type_(0), code_(0), check_sum_(0), id_(0), sequence_(0),
34 if (length <
sizeof(
struct ip)) {
36 "ICMPMsg::unpack - truncated ip header, length: "
41 struct ip const* ip_header =
reinterpret_cast<struct ip const*
>(wire_data);
42 auto hlen = (ip_header->ip_hl << 2);
45 if (length < (hlen +
sizeof(
struct icmp))) {
47 << length <<
", hlen: " << hlen);
51 msg->setSource(
IOAddress(ntohl(ip_header->ip_src.s_addr)));
52 msg->setDestination(
IOAddress(ntohl(ip_header->ip_dst.s_addr)));
55 struct icmp const* reply =
56 reinterpret_cast<struct icmp const*
>(wire_data + hlen);
57 msg->setType(reply->icmp_type);
58 msg->setCode(reply->icmp_code);
60 msg->setChecksum(ntohs(reply->icmp_cksum));
61 msg->setId(ntohs(reply->icmp_hun.ih_idseq.icd_id));
62 msg->setSequence(ntohs(reply->icmp_hun.ih_idseq.icd_seq));
65 const uint8_t* idata =
reinterpret_cast<const uint8_t*
>(&reply->icmp_dun);
66 msg->setPayload(idata, payload_len);
73 ICMPPtr outbound(
new struct icmp());
74 memset(outbound.get(), 0x00,
sizeof(
struct icmp));
75 outbound->icmp_type = msg_type_;
76 outbound->icmp_id = htons(id_);
77 outbound->icmp_seq = htons(sequence_);
79 uint8_t* odata =
reinterpret_cast<uint8_t*
>(outbound.get());
80 outbound->icmp_cksum = htons(~
calcChecksum(odata,
sizeof(
struct icmp)));
86 payload_.insert(payload_.end(),
data,
data + length);
95 for (i = 0; i < (length & ~1U); i += 2) {
96 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.