Kea 3.3.3
icmp_msg.h
Go to the documentation of this file.
1// Copyright (C) 2023-2026 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 <cstdint>
13
14#include <boost/shared_ptr.hpp>
15
16#include <arpa/inet.h>
17#include <netinet/in.h>
18#include <netinet/ip.h>
19#include <netinet/ip_icmp.h>
20#include <unistd.h>
21
22namespace isc {
23namespace ping_check {
24
25// Forward class definition.
26class ICMPMsg;
27
29typedef boost::shared_ptr<ICMPMsg> ICMPMsgPtr;
30
32typedef boost::shared_ptr<struct icmp> ICMPPtr;
33
38class ICMPMsg {
39public:
42 enum ICMPMsgType : uint16_t {
46 };
47
52 constexpr static size_t ICMP_HEADER_SIZE = 8;
53
55 ICMPMsg();
56
58 virtual ~ICMPMsg() = default;
59
70 static ICMPMsgPtr unpack(const uint8_t* wire_data, size_t length);
71
75 ICMPPtr pack() const;
76
80 uint8_t getType() const {
81 return (msg_type_);
82 }
83
87 void setType(uint8_t msg_type) {
88 msg_type_ = msg_type;
89 }
90
94 uint8_t getCode() const {
95 return (code_);
96 }
97
101 void setCode(uint8_t code) {
102 code_ = code;
103 }
104
108 uint16_t getChecksum() const {
109 return (check_sum_);
110 }
111
115 void setChecksum(uint16_t check_sum) {
116 check_sum_ = check_sum;
117 }
118
122 uint16_t getId() const {
123 return (id_);
124 }
125
129 void setId(const uint16_t id) {
130 id_ = id;
131 }
132
136 uint16_t getSequence() const {
137 return (sequence_);
138 }
139
143 void setSequence(uint16_t sequence) {
144 sequence_ = sequence;
145 }
146
151 return (source_);
152 }
153
158 source_ = source;
159 }
160
165 return (destination_);
166 }
167
171 void setDestination(const isc::asiolink::IOAddress& destination) {
172 destination_ = destination;
173 }
174
178 const std::vector<uint8_t>& getPayload() const {
179 return (payload_);
180 }
181
186 void setPayload(const uint8_t* data, size_t length);
187
194 static uint32_t calcChecksum(const uint8_t* data, size_t length);
195
196private:
199
201 isc::asiolink::IOAddress destination_;
202
204 uint8_t msg_type_;
205
207 uint8_t code_;
208
210 uint16_t check_sum_;
211
213 uint16_t id_;
214
216 uint16_t sequence_;
217
218 // data beyond the ICMP header
219 std::vector<uint8_t> payload_;
220};
221
222
223} // end of namespace ping_check
224} // end of namespace isc
225
226#endif
Embodies an ICMP message.
Definition icmp_msg.h:38
const isc::asiolink::IOAddress & getDestination() const
Fetches the destination IP address.
Definition icmp_msg.h:164
uint16_t getChecksum() const
Fetches the checksum.
Definition icmp_msg.h:108
void setDestination(const isc::asiolink::IOAddress &destination)
Sets the destination IP address.
Definition icmp_msg.h:171
uint8_t getType() const
Fetches the ICMP message type (e.g.
Definition icmp_msg.h:80
void setPayload(const uint8_t *data, size_t length)
Sets the message payload to the given data.
Definition icmp_msg.cc:85
uint8_t getCode() const
Fetches the ICMP message code.
Definition icmp_msg.h:94
const isc::asiolink::IOAddress & getSource() const
Fetches the source IP address.
Definition icmp_msg.h:150
static ICMPMsgPtr unpack(const uint8_t *wire_data, size_t length)
Unpacks an ICMP message from the given wire_data.
Definition icmp_msg.cc:32
ICMPMsg()
Constructor.
Definition icmp_msg.cc:24
uint16_t getId() const
Fetches the message id.
Definition icmp_msg.h:122
void setChecksum(uint16_t check_sum)
Sets the check sum.
Definition icmp_msg.h:115
uint16_t getSequence() const
Fetches the message sequence number.
Definition icmp_msg.h:136
void setSource(const isc::asiolink::IOAddress &source)
Sets the source IP address.
Definition icmp_msg.h:157
ICMPPtr pack() const
Packs the message into an ICMP structure.
Definition icmp_msg.cc:72
void setId(const uint16_t id)
Sets the message id.
Definition icmp_msg.h:129
virtual ~ICMPMsg()=default
Destructor.
void setSequence(uint16_t sequence)
Sets the message sequence number.
Definition icmp_msg.h:143
ICMPMsgType
ICMP message types.
Definition icmp_msg.h:42
void setCode(uint8_t code)
Sets the ICMP code.
Definition icmp_msg.h:101
static uint32_t calcChecksum(const uint8_t *data, size_t length)
Calculates the checksum of the given data buffer.
Definition icmp_msg.cc:90
static constexpr size_t ICMP_HEADER_SIZE
Size in octets of ICMP message header.
Definition icmp_msg.h:52
void setType(uint8_t msg_type)
Sets the ICMP message type.
Definition icmp_msg.h:87
const std::vector< uint8_t > & getPayload() const
Fetches the message payload.
Definition icmp_msg.h:178
boost::shared_ptr< ICMPMsg > ICMPMsgPtr
Shared pointer type for ICMPMsg.
Definition icmp_msg.h:29
boost::shared_ptr< struct icmp > ICMPPtr
Shared pointer type for struct icmp.
Definition icmp_msg.h:32
Defines the logger used by the top-level component of kea-lfc.