Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
ICMP.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_Network_InternetProtocol_ICMP_h_
5#define _Stroika_Foundation_IO_Network_InternetProtocol_ICMP_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <cstdint>
10
11#include "Stroika/Foundation/Common/Common.h"
13#include "Stroika/Foundation/Execution/Exceptions.h"
15#include "Stroika/Foundation/Memory/Common.h"
16
17/**
18 *
19 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
20 *
21 *
22 * @see https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol
23 *
24 */
25
27
28 namespace V4 {
29
30 /*
31 * ICMP packet types - from https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol "Control messages"
32 */
33 enum class ICMP_PacketTypes : uint8_t {
34 ICMP_ECHO_REPLY = 0,
35 ICMP_DEST_UNREACH = 3,
36 ICMP_ECHO_REQUEST = 8,
37 ICMP_TTL_EXPIRE = 11,
38 };
39 using ICMP_PacketTypes::ICMP_DEST_UNREACH;
40 using ICMP_PacketTypes::ICMP_ECHO_REPLY;
41 using ICMP_PacketTypes::ICMP_ECHO_REQUEST;
42 using ICMP_PacketTypes::ICMP_TTL_EXPIRE;
43
44 /**
45 * ICMP packet header (does not include IP header).
46 *
47 * @see https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol
48 */
49 Stroika_Foundation_Common_STRUCT_PACKED (struct PacketHeader {
50 ICMP_PacketTypes type; // ICMP packet type
51 uint8_t code; // Type sub code
52 uint16_t checksum;
53 uint16_t id;
54 uint16_t seq;
55 uint32_t timestamp; // not part of ICMP, but we need it
56 });
57 static_assert (sizeof (PacketHeader) == 12, "Check Stroika_Foundation_Common_STRUCT_PACKED: ICMP::PacketHeader size wrong");
58
59 // Minimum ICMP packet size, in bytes
60 constexpr size_t ICMP_MIN{8};
61
62 /**
63 * @see https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol - Destination unreachable message
64 */
66 private:
68
69 public:
70 /**
71 */
72 DestinationUnreachableException (uint8_t code, const InternetAddress& unreachedIP);
73
74 public:
75 /**
76 */
77 nonvirtual uint8_t GetCode () const;
78
79 public:
80 nonvirtual InternetAddress GetUnreachedIP () const;
81
82 private:
83 uint8_t fCode_;
84 InternetAddress fUnreachedIP_;
85 };
86
87 /**
88 */
89 class UnknownICMPPacket : public Execution::RuntimeErrorException<> {
90 private:
91 using inherited = Execution::RuntimeErrorException<>;
92
93 public:
94 /**
95 */
96 UnknownICMPPacket (ICMP_PacketTypes type);
97
98 public:
99 /**
100 */
101 nonvirtual ICMP_PacketTypes GetType () const;
102
103 private:
104 ICMP_PacketTypes fType_;
105 };
106
107 /**
108 * @see https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol - means hop count (TTL) not large
109 * enough to reach destination
110 *
111 * 'ReachedIP' is the address reached by the packet when the TTL expired.
112 */
114 private:
116
117 public:
118 /**
119 */
120 TTLExpiredException (const InternetAddress& unreachedIP);
121
122 public:
123 nonvirtual InternetAddress GetUnreachedIP () const;
124
125 private:
126 InternetAddress fUnreachedIP_;
127 };
128 }
129
130}
131
132/*
133 ********************************************************************************
134 ***************************** Implementation Details ***************************
135 ********************************************************************************
136 */
137#include "ICMP.inl"
138
139#endif /*_Stroika_Foundation_IO_Network_InternetProtocol_ICMP_h_*/