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* ip_header = (
struct ip*)(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* reply = (
struct icmp*)(wire_data + hlen);
54 msg->setType(reply->icmp_type);
55 msg->setCode(reply->icmp_code);
57 msg->setChecksum(ntohs(reply->icmp_cksum));
58 msg->setId(ntohs(reply->icmp_hun.ih_idseq.icd_id));
59 msg->setSequence(ntohs(reply->icmp_hun.ih_idseq.icd_seq));
62 msg->setPayload((
const uint8_t*)(&reply->icmp_dun), payload_len);
69 ICMPPtr outbound(
new struct icmp());
70 memset(outbound.get(), 0x00,
sizeof(
struct icmp));
71 outbound->icmp_type = msg_type_;
72 outbound->icmp_id = htons(id_);
73 outbound->icmp_seq = htons(sequence_);
75 outbound->icmp_cksum = htons(~
calcChecksum((
const uint8_t*)(outbound.get()),
sizeof(
struct icmp)));
81 payload_.insert(payload_.end(),
data,
data + length);
90 for (i = 0; i < (length & ~1U); i += 2) {
91 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.