Kea 3.1.1
icmp_msg.h
Go to the documentation of this file.
1// Copyright (C) 2023-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef ICMP_MSG_H
8#define ICMP_MSG_H
9
10#include <asiolink/io_address.h>
11
12#include <arpa/inet.h>
13#include <netinet/in.h>
14#include <netinet/ip.h>
15#include <unistd.h>
16#include <netinet/ip_icmp.h>
17#include <boost/shared_ptr.hpp>
18
19namespace isc {
20namespace ping_check {
21
22// Forward class definition.
23class ICMPMsg;
24
26typedef boost::shared_ptr<ICMPMsg> ICMPMsgPtr;
27
29typedef boost::shared_ptr<struct icmp> ICMPPtr;
30
35class ICMPMsg {
36public:
44
49 constexpr static size_t ICMP_HEADER_SIZE = 8;
50
52 ICMPMsg();
53
55 virtual ~ICMPMsg() = default;
56
67 static ICMPMsgPtr unpack(const uint8_t* wire_data, size_t length);
68
72 ICMPPtr pack() const;
73
77 uint8_t getType() const {
78 return (msg_type_);
79 }
80
84 void setType(uint8_t msg_type) {
85 msg_type_ = msg_type;
86 }
87
91 uint8_t getCode() const {
92 return (code_);
93 }
94
98 void setCode(uint8_t code) {
99 code_ = code;
100 }
101
105 uint16_t getChecksum() const {
106 return (check_sum_);
107 }
108
112 void setChecksum(uint16_t check_sum) {
113 check_sum_ = check_sum;
114 }
115
119 uint16_t getId() const {
120 return (id_);
121 }
122
126 void setId(const uint16_t id) {
127 id_ = id;
128 }
129
133 uint16_t getSequence() const {
134 return (sequence_);
135 }
136
140 void setSequence(uint16_t sequence) {
141 sequence_ = sequence;
142 }
143
148 return (source_);
149 }
150
155 source_ = source;
156 }
157
162 return (destination_);
163 }
164
168 void setDestination(const isc::asiolink::IOAddress& destination) {
169 destination_ = destination;
170 }
171
175 const std::vector<uint8_t>& getPayload() const {
176 return (payload_);
177 }
178
183 void setPayload(const uint8_t* data, size_t length);
184
191 static uint32_t calcChecksum(const uint8_t* data, size_t length);
192
193private:
196
198 isc::asiolink::IOAddress destination_;
199
201 uint8_t msg_type_;
202
204 uint8_t code_;
205
207 uint16_t check_sum_;
208
210 uint16_t id_;
211
213 uint16_t sequence_;
214
215 // data beyond the ICMP header
216 std::vector<uint8_t> payload_;
217};
218
219
220} // end of namespace ping_check
221} // end of namespace isc
222
223#endif
Embodies an ICMP message.
Definition icmp_msg.h:35
const isc::asiolink::IOAddress & getDestination() const
Fetches the destination IP address.
Definition icmp_msg.h:161
uint16_t getChecksum() const
Fetches the checksum.
Definition icmp_msg.h:105
void setDestination(const isc::asiolink::IOAddress &destination)
Sets the destination IP address.
Definition icmp_msg.h:168
ICMPMsgType
ICMP message types.
Definition icmp_msg.h:39
uint8_t getType() const
Fetches the ICMP message type (e.g.
Definition icmp_msg.h:77
void setPayload(const uint8_t *data, size_t length)
Sets the message payload to the given data.
Definition icmp_msg.cc:80
uint8_t getCode() const
Fetches the ICMP message code.
Definition icmp_msg.h:91
const isc::asiolink::IOAddress & getSource() const
Fetches the source IP address.
Definition icmp_msg.h:147
static ICMPMsgPtr unpack(const uint8_t *wire_data, size_t length)
Unpacks an ICMP message from the given wire_data.
Definition icmp_msg.cc:30
ICMPMsg()
Constructor.
Definition icmp_msg.cc:22
uint16_t getId() const
Fetches the message id.
Definition icmp_msg.h:119
void setChecksum(uint16_t check_sum)
Sets the check sum.
Definition icmp_msg.h:112
uint16_t getSequence() const
Fetches the message sequence number.
Definition icmp_msg.h:133
void setSource(const isc::asiolink::IOAddress &source)
Sets the source IP address.
Definition icmp_msg.h:154
ICMPPtr pack() const
Packs the message into an ICMP structure.
Definition icmp_msg.cc:68
void setId(const uint16_t id)
Sets the message id.
Definition icmp_msg.h:126
virtual ~ICMPMsg()=default
Destructor.
void setSequence(uint16_t sequence)
Sets the message sequence number.
Definition icmp_msg.h:140
void setCode(uint8_t code)
Sets the ICMP code.
Definition icmp_msg.h:98
static uint32_t calcChecksum(const uint8_t *data, size_t length)
Calculates the checksum of the given data buffer.
Definition icmp_msg.cc:85
static constexpr size_t ICMP_HEADER_SIZE
Size in octets of ICMP message header.
Definition icmp_msg.h:49
void setType(uint8_t msg_type)
Sets the ICMP message type.
Definition icmp_msg.h:84
const std::vector< uint8_t > & getPayload() const
Fetches the message payload.
Definition icmp_msg.h:175
boost::shared_ptr< ICMPMsg > ICMPMsgPtr
Shared pointer type for ICMPMsg.
Definition icmp_msg.h:26
boost::shared_ptr< struct icmp > ICMPPtr
Shared pointer type for struct icmp.
Definition icmp_msg.h:29
Defines the logger used by the top-level component of kea-lfc.