SatCat5
ip_dispatch.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 and dispatch unit for Internet Protocol v4 (IPv4)
6 
7 #pragma once
8 
9 #include <satcat5/eth_arp.h>
10 #include <satcat5/eth_dispatch.h>
11 #include <satcat5/igmp_client.h>
12 #include <satcat5/ip_core.h>
13 #include <satcat5/ip_icmp.h>
14 #include <satcat5/ip_table.h>
15 #include <satcat5/net_core.h>
16 #include <satcat5/utils.h>
17 
18 // Time To Live (TTL) sets maximum number of router hops.
19 // This parameter sets the default for outgoing packets.
20 #ifndef SATCAT5_IP_TTL
21 #define SATCAT5_IP_TTL 128
22 #endif
23 
24 namespace satcat5 {
25  namespace ip {
39  class Dispatch final
41  , public satcat5::net::Protocol
42  , public satcat5::net::Dispatch
43  {
44  public:
49  Dispatch(
50  const satcat5::ip::Addr& addr,
52  satcat5::ip::Table* route);
53  ~Dispatch() SATCAT5_OPTIONAL_DTOR;
54 
57  const satcat5::net::Type& type, unsigned len) override;
58 
61  const satcat5::eth::MacAddr& mac, // Destination MAC
62  const satcat5::eth::VlanTag& vtag, // VLAN information
63  const satcat5::ip::Addr& ip, // Destination IP
64  u8 protocol, // Protocol (UDP/TCP/etc)
65  unsigned len); // Length after IP header
66 
68  void set_addr(const satcat5::ip::Addr& addr);
69 
76  u8 protocol,
77  const satcat5::ip::Addr& dst,
78  unsigned inner_bytes,
79  u8 ttl = SATCAT5_IP_TTL);
80 
83  inline void route_clear(bool lockdown = true)
84  { m_route->route_clear(lockdown); }
85  inline bool route_cache(
86  const satcat5::ip::Addr& gateway,
87  const satcat5::eth::MacAddr& dstmac)
88  { return m_route->route_cache(gateway, dstmac); }
89  inline bool route_default(
90  const satcat5::ip::Addr& gateway,
91  const satcat5::eth::MacAddr& dstmac = satcat5::eth::MACADDR_NONE)
92  { return m_route->route_default(gateway, dstmac); }
93  inline bool route_local(const satcat5::ip::Subnet& subnet)
94  { return m_route->route_local(subnet); }
95  inline bool route_simple(
96  const satcat5::ip::Addr& gateway,
97  const satcat5::ip::Mask& subnet = satcat5::ip::MASK_24)
98  { return m_route->route_simple(gateway, subnet); }
99  inline bool route_static(
100  const satcat5::ip::Subnet& subnet,
101  const satcat5::ip::Addr& gateway,
102  const satcat5::eth::MacAddr& dstmac = satcat5::eth::MACADDR_NONE)
103  { return m_route->route_static(subnet, gateway, dstmac); }
104  inline bool route_remove(const satcat5::ip::Subnet& subnet)
105  { return m_route->route_remove(subnet); }
106  inline bool route_remove(const satcat5::ip::Addr& addr)
107  { return m_route->route_remove(addr); }
109  const satcat5::ip::Addr& dstaddr) const
110  { return m_route->route_lookup(dstaddr); }
112 
113  // Other accessors:
114  inline satcat5::eth::ProtoArp* arp()
115  { return &m_arp; }
116  inline satcat5::ip::ProtoIcmp* icmp()
117  { return &m_icmp; }
118  inline satcat5::igmp::Client* igmp()
119  { return &m_igmp; }
120  inline satcat5::eth::Dispatch* iface() const
121  { return m_iface; }
122  inline satcat5::ip::Addr ipaddr() const
123  { return m_addr; }
124  inline satcat5::eth::MacAddr macaddr() const
125  { return m_iface->macaddr(); }
126  inline satcat5::eth::VlanTag reply_vtag() const
127  { return m_iface->reply_vtag(); }
128  inline satcat5::eth::MacAddr reply_mac() const
129  { return m_iface->reply_mac(); }
130  inline bool reply_is_multicast() const
131  { return m_reply_dst.is_multicast(); }
132  inline satcat5::ip::Addr reply_ip() const
133  { return m_reply_src; }
134  inline const satcat5::ip::Header& reply_hdr() const
135  { return m_reply_hdr; }
136  inline void set_ipaddr(const satcat5::ip::Addr& addr)
137  { set_addr(addr); }
138  inline void set_macaddr(const satcat5::eth::MacAddr& macaddr)
139  { m_iface->set_macaddr(macaddr); }
140  inline satcat5::ip::Table* table()
141  { return m_route; }
142 
145  inline void set_ident(u16 ident)
146  { m_ident = ident; }
147 
148  protected:
149  // Event handler for the ARP cache.
150  void arp_event(
151  const satcat5::eth::MacAddr& mac,
152  const satcat5::ip::Addr& ip) override;
153 
154  // Event handler for incoming IPv4 frames.
155  void frame_rcvd(satcat5::io::LimitedRead& src) override;
156 
157  // Pointer to the parent interface.
158  satcat5::eth::Dispatch* const m_iface;
159  satcat5::ip::Table* const m_route;
160 
161  // IP address for this interface.
162  satcat5::ip::Addr m_addr;
163 
164  // ARP, ICMP, and IGMP handlers for this interface.
166  satcat5::ip::ProtoIcmp m_icmp;
167  satcat5::igmp::Client m_igmp;
168 
169  // The current reply state (address + complete header).
170  satcat5::ip::Addr m_reply_dst;
171  satcat5::ip::Addr m_reply_src;
172  satcat5::ip::Header m_reply_hdr;
173 
174  // Identification field for outgoing packets.
175  u16 m_ident;
176  };
177  }
178 }
Callback interface for responding to ARP and ICMP events.
Definition: eth_arp.h:46
Implemention of "net::Dispatch" for Ethernet frames.
Definition: eth_dispatch.h:21
void set_macaddr(const satcat5::eth::MacAddr &macaddr)
Set the local MAC-address.
Protocol handler for Ethernet-to-IPv4 ARP queries and replies.
Definition: eth_arp.h:68
Host/Client for the Internet Group Management Protocol (IGMP).
Definition: igmp_client.h:108
Limited read of next N bytes.
Definition: io_readable.h:255
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
Definition: ip_dispatch.h:43
bool route_cache(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac)
Routing table shortcuts.
Definition: ip_dispatch.h:85
bool route_default(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac=satcat5::eth::MACADDR_NONE)
Routing table shortcuts.
Definition: ip_dispatch.h:89
satcat5::io::Writeable * open_write(const satcat5::eth::MacAddr &mac, const satcat5::eth::VlanTag &vtag, const satcat5::ip::Addr &ip, u8 protocol, unsigned len)
Get Writeable object for sending to a specific IP/MAC address.
Definition: ip_dispatch.cc:55
satcat5::ip::Route route_lookup(const satcat5::ip::Addr &dstaddr) const
Routing table shortcuts.
Definition: ip_dispatch.h:108
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_dispatch.cc:106
bool route_remove(const satcat5::ip::Subnet &subnet)
Routing table shortcuts.
Definition: ip_dispatch.h:104
satcat5::io::Writeable * open_reply(const satcat5::net::Type &type, unsigned len) override
Get Writeable object for reply to the last received datagram.
Definition: ip_dispatch.cc:51
void arp_event(const satcat5::eth::MacAddr &mac, const satcat5::ip::Addr &ip) override
Callback for any announced MAC/IP address pair.
Definition: ip_dispatch.cc:101
bool route_static(const satcat5::ip::Subnet &subnet, const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac=satcat5::eth::MACADDR_NONE)
Routing table shortcuts.
Definition: ip_dispatch.h:99
void set_addr(const satcat5::ip::Addr &addr)
Set the local IP-address.
Definition: ip_dispatch.cc:74
bool route_simple(const satcat5::ip::Addr &gateway, const satcat5::ip::Mask &subnet=satcat5::ip::MASK_24)
Routing table shortcuts.
Definition: ip_dispatch.h:95
satcat5::ip::Header next_header(u8 protocol, const satcat5::ip::Addr &dst, unsigned inner_bytes, u8 ttl=SATCAT5_IP_TTL)
Create a basic IPv4 header with the specified information.
Definition: ip_dispatch.cc:79
void route_clear(bool lockdown=true)
Routing table shortcuts.
Definition: ip_dispatch.h:83
bool route_remove(const satcat5::ip::Addr &addr)
Routing table shortcuts.
Definition: ip_dispatch.h:106
bool route_local(const satcat5::ip::Subnet &subnet)
Routing table shortcuts.
Definition: ip_dispatch.h:93
void set_ident(u16 ident)
For testing purposes only, reset the "ident" field.
Definition: ip_dispatch.h:145
Protocol handler for the Internet Control Message Protocol (ICMP).
Definition: ip_icmp.h:81
IPv4 forwarding table.
Definition: ip_table.h:144
bool route_default(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac=satcat5::eth::MACADDR_NONE, u8 port=0, u8 flags=0)
Set the default behavior when no other routes match.
Definition: ip_table.cc:97
satcat5::ip::Route route_lookup(const satcat5::ip::Addr &dstaddr) const
Next-hop routing lookup for the given destination address.
Definition: ip_table.cc:199
void route_clear(bool lockdown=true)
Clear all routes, including the default.
Definition: ip_table.cc:73
bool route_static(const satcat5::ip::Subnet &subnet, const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac=satcat5::eth::MACADDR_NONE, u8 port=0, u8 flags=0)
Create or update a single static route.
Definition: ip_table.cc:111
bool route_local(const satcat5::ip::Subnet &subnet, u8 port=0, u8 flags=0)
Create or update a local static route.
Definition: ip_table.h:192
bool route_cache(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac)
Update matching MAC address cache entries.
Definition: ip_table.cc:138
bool route_remove(const satcat5::ip::Subnet &subnet)
Remove a single static route.
Definition: ip_table.cc:178
bool route_simple(const satcat5::ip::Addr &gateway, const satcat5::ip::Mask &subnet=satcat5::ip::MASK_24)
Simplified one-step setup for a typical home network.
Definition: ip_table.cc:105
The "Dispatch" is a generic interface that knows how to read a designated protocol layer and sort inc...
Definition: net_dispatch.h:36
Dispatch()
Constructor and destructor access restricted to children.
Definition: net_dispatch.h:68
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Protocol handler for the Address Resolution Protocol (ARP)
Client-side implementation of the Internet Group Management Protocol (IGMP)
Internet Protocol v4 (IPv4) forwarding table.
Generic network Dispatch API.
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
bool is_multicast() const
IP multicast (224.*.*.*)
Definition: ip_core.cc:41
Structure for holding an IPv4 Header, including options.
Definition: ip_core.h:180
IPv4 subnet masks share functionality with a basic address, but are constructed differently to match ...
Definition: ip_core.h:66
A single entry in the static routing table.
Definition: ip_table.h:57
An IPv4 subnet consists of a base address and a subnet mask.
Definition: ip_core.h:87
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38
Miscellaneous mathematical utility functions.