SatCat5
ip_icmp.h
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Protocol handler for the Internet Control Message Protocol (ICMP)
6 
7 #pragma once
8 
9 #include <satcat5/net_core.h>
10 #include <satcat5/ip_core.h>
11 #include <satcat5/list.h>
12 #include <satcat5/polling.h>
13 
14 namespace satcat5 {
15  namespace ip {
18  constexpr u16 ICMP_ECHO_REPLY = 0x0000;
19  constexpr u16 ICMP_UNREACHABLE_NET = 0x0300;
20  constexpr u16 ICMP_UNREACHABLE_HOST = 0x0301;
21  constexpr u16 ICMP_UNREACHABLE_PROTO = 0x0302;
22  constexpr u16 ICMP_UNREACHABLE_PORT = 0x0303;
23  constexpr u16 ICMP_FRAG_REQUIRED = 0x0304;
24  constexpr u16 ICMP_SRC_ROUTE_FAILED = 0x0305;
25  constexpr u16 ICMP_DST_NET_UNKNOWN = 0x0306;
26  constexpr u16 ICMP_DST_HOST_UNKNOWN = 0x0307;
27  constexpr u16 ICMP_SRC_HOST_ISOLATED = 0x0308;
28  constexpr u16 ICMP_NET_PROHIBITED = 0x0309;
29  constexpr u16 ICMP_HOST_PROHIBITED = 0x030A;
30  constexpr u16 ICMP_TOS_NET = 0x030B;
31  constexpr u16 ICMP_TOS_HOST = 0x030C;
32  constexpr u16 ICMP_COMM_PROHIBITED = 0x030D;
33  constexpr u16 ICMP_HOST_PRECEDENCE = 0x030E;
34  constexpr u16 ICMP_PRECEDENCE_CUTOFF = 0x030F;
35  constexpr u16 ICMP_REDIRECT_NET = 0x0500;
36  constexpr u16 ICMP_REDIRECT_HOST = 0x0501;
37  constexpr u16 ICMP_REDIRECT_NET_TOS = 0x0502;
38  constexpr u16 ICMP_REDIRECT_HOST_TOS = 0x0503;
39  constexpr u16 ICMP_ECHO_REQUEST = 0x0800;
40  constexpr u16 ICMP_TTL_EXPIRED = 0x0B00;
41  constexpr u16 ICMP_FRAG_TIMEOUT = 0x0B01;
42  constexpr u16 ICMP_IP_HDR_POINTER = 0x0C00;
43  constexpr u16 ICMP_IP_HDR_OPTION = 0x0C01;
44  constexpr u16 ICMP_IP_HDR_LENGTH = 0x0C02;
45  constexpr u16 ICMP_TIME_REQUEST = 0x0D00;
46  constexpr u16 ICMP_TIME_REPLY = 0x0E00;
48 
51  constexpr u16 ICMP_TYPE_MASK = 0xFF00;
52  constexpr u16 ICMP_TYPE_UNREACHABLE = 0x0300; // 0x0300 - 03FF
53  constexpr u16 ICMP_TYPE_REDIRECT = 0x0500; // 0x0500 - 05FF
54  constexpr u16 ICMP_TYPE_TIME_EXCEED = 0x0B00; // 0x0B00 - 0BFF
55  constexpr u16 ICMP_TYPE_BAD_IP_HDR = 0x0C00; // 0x0C00 - 0CFF
57 
59  constexpr unsigned ICMP_ECHO_BYTES = 8;
60 
62  class PingListener {
63  public:
66  virtual void ping_event(
67  const satcat5::ip::Addr& from, u32 elapsed_usec) = 0;
68 
69  private:
70  // Linked list to the next listener object.
73  };
74 
79  class ProtoIcmp final
80  : public satcat5::net::Protocol
81  , protected satcat5::poll::Timer {
82  public:
85  ~ProtoIcmp() SATCAT5_OPTIONAL_DTOR;
86 
94  bool send_error(
95  u16 type, satcat5::io::Readable* src, uint32_t arg = 0);
96 
100 
104 
111  inline void set_log_cooldown(unsigned wait_msec)
112  { m_wait_msec = wait_msec; }
113 
117  {m_listeners.add(cb);}
119  {m_listeners.remove(cb);}
121 
122  protected:
123  void frame_rcvd(satcat5::io::LimitedRead& src) override;
124  void timer_event() override;
125  bool write_icmp(
127  unsigned wcount, u16* data);
128 
129  satcat5::ip::Dispatch* const m_iface;
131  unsigned m_wait_msec;
132  };
133  }
134 }
Limited read of next N bytes.
Definition: io_readable.h:255
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Connection metadata for an IPv4 address.
Definition: ip_address.h:38
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
Definition: ip_dispatch.h:43
Callback API for handling "ping" reponses.
Definition: ip_icmp.h:62
virtual void ping_event(const satcat5::ip::Addr &from, u32 elapsed_usec)=0
Callback method when a ping response is received.
Protocol handler for the Internet Control Message Protocol (ICMP).
Definition: ip_icmp.h:81
bool send_timereq(satcat5::ip::Address &dst)
Initiate a timestamp request.
Definition: ip_icmp.cc:168
void timer_event() override
Child class MUST override this method.
Definition: ip_icmp.cc:268
void set_log_cooldown(unsigned wait_msec)
Adjust the rate-limit for ICMP error logging.
Definition: ip_icmp.h:111
void remove(satcat5::ip::PingListener *cb)
Add/remove callback handlers for Ping responses.
Definition: ip_icmp.h:118
void add(satcat5::ip::PingListener *cb)
Add/remove callback handlers for Ping responses.
Definition: ip_icmp.h:116
bool send_error(u16 type, satcat5::io::Readable *src, uint32_t arg=0)
Send a specific error message.
Definition: ip_icmp.cc:120
bool send_ping(satcat5::ip::Address &dst)
Initiate a ping (Echo request = Type 8.0).
Definition: ip_icmp.cc:151
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: ip_icmp.cc:191
ProtoIcmp(satcat5::ip::Dispatch *iface)
Link this handler to an IPv4 network interface.
Definition: ip_icmp.cc:105
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Helper functions for manipulating singly-linked lists.
Definition: list.h:52
void add(T *item)
Add new item to front or back, whichever is simpler.
Definition: list.h:221
void remove(T *item)
Remove the designated item from the list.
Definition: list.h:277
Templated functions for manipulating singly-linked lists.
Generic network Dispatch API.
Core event-processing loop for SatCat5 software.
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15