SatCat5
udp_core.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 // Type definitions for UDP datagrams and protocol handlers
6 
7 #pragma once
8 
9 #include <satcat5/ip_address.h>
10 #include <satcat5/net_core.h>
11 
12 namespace satcat5 {
13  namespace udp {
14  // Alias for address and port types in the "udp" namespace.
15  typedef satcat5::ip::Addr Addr;
16  typedef satcat5::ip::Port Port;
17 
19  static constexpr unsigned HEADER_BYTES = 8;
20 
24  constexpr satcat5::udp::Port PORT_NONE = {0};
25  constexpr satcat5::udp::Port PORT_ECHO = {7};
26  constexpr satcat5::udp::Port PORT_DHCP_SERVER = {67};
27  constexpr satcat5::udp::Port PORT_DHCP_CLIENT = {68};
28  constexpr satcat5::udp::Port PORT_TFTP_SERVER = {69};
29  constexpr satcat5::udp::Port PORT_NTP_SERVER = {123};
30  constexpr satcat5::udp::Port PORT_PTP_EVENT = {319};
31  constexpr satcat5::udp::Port PORT_PTP_GENERAL = {320};
32  constexpr satcat5::udp::Port PORT_COAP = {5683};
34 
37  constexpr satcat5::udp::Port PORT_CFGBUS_CMD = {0x5A61};
38  constexpr satcat5::udp::Port PORT_CFGBUS_ACK = {0x5A62};
39  constexpr satcat5::udp::Port PORT_CBOR_TLM = {0x5A63};
40  constexpr satcat5::udp::Port PORT_SWITCH_LOG = {0x5A64};
42 
44  constexpr satcat5::ip::Addr MULTICAST_COAP(224, 0, 1, 187);
45 
48  struct Header {
49  // UDP header fields.
52  u16 length;
53 
54  // Explicitly declare default constructor and assignment methods.
55  Header() = default;
56  Header(const Header& t) = default;
57  Header& operator=(const Header& t) = default;
58 
61  void write_to(satcat5::io::Writeable* wr) const;
62 
66  };
67 
68  constexpr satcat5::udp::Header HEADER_EMPTY =
69  {satcat5::udp::PORT_NONE, satcat5::udp::PORT_NONE, 0};
70 
72  class Address final : public satcat5::net::Address {
73  public:
78  ~Address() {}
79 
84 
86  void connect(
87  const satcat5::udp::Addr& dstaddr,
88  const satcat5::eth::MacAddr& dstmac,
89  const satcat5::udp::Port& dstport,
90  const satcat5::udp::Port& srcport = satcat5::udp::PORT_NONE,
91  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
92 
95  void connect(
96  const satcat5::udp::Addr& dstaddr,
97  const satcat5::udp::Port& dstport,
98  const satcat5::udp::Port& srcport = satcat5::udp::PORT_NONE,
99  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
100 
101  // Required overrides from net::Address.
102  void close() override {m_addr.close();}
103  bool ready() const override {return m_addr.ready();}
104  void retry() override {m_addr.retry();}
105  satcat5::net::Dispatch* iface() const override;
106  satcat5::io::Writeable* open_write(unsigned len) override;
107  bool is_multicast() const override
108  { return m_addr.is_multicast(); }
109  bool matches_reply_address() const override;
110  bool reply_is_multicast() const override
111  { return m_addr.reply_is_multicast(); }
112  void save_reply_address() override;
113 
114  // Various accessors.
115  inline satcat5::ip::Addr dstaddr() const
116  { return m_addr.dstaddr();}
117  inline satcat5::eth::MacAddr dstmac() const
118  { return m_addr.dstmac();}
119  inline satcat5::udp::Port dstport() const
120  { return m_dstport;}
121  inline satcat5::eth::Dispatch* eth() const
122  { return m_addr.eth(); }
123  inline satcat5::eth::Header eth_header() const
124  { return m_addr.eth_header(); }
125  satcat5::ip::Header ip_header(unsigned len) const;
126  inline satcat5::ip::Addr gateway() const
127  { return m_addr.gateway(); }
128  inline satcat5::eth::MacAddr srcmac() const
129  { return m_addr.srcmac(); }
130  inline satcat5::udp::Port srcport() const
131  { return m_srcport; }
132  inline satcat5::udp::Dispatch* udp() const
133  { return m_iface; }
134  inline satcat5::udp::Header udp_header(unsigned len) const
135  { return {m_srcport, m_dstport, u16(len + 8)}; }
136  inline satcat5::eth::VlanTag vtag() const
137  { return m_addr.vtag(); }
138 
139  protected:
140  satcat5::udp::Dispatch* m_iface;
141  satcat5::ip::Address m_addr;
142  satcat5::udp::Port m_dstport;
143  satcat5::udp::Port m_srcport;
144  };
145 
150  public:
151  // Various accessors.
152  inline satcat5::ip::Addr dstaddr() const
153  { return m_addr.dstaddr(); }
154  inline satcat5::eth::MacAddr dstmac() const
155  { return m_addr.dstmac(); }
156  inline satcat5::udp::Port dstport() const
157  { return m_addr.dstport(); }
158  inline satcat5::eth::Dispatch* eth() const
159  { return m_addr.eth(); }
160  satcat5::net::Dispatch* iface() const
161  { return m_addr.iface(); }
162  inline satcat5::ip::Addr gateway() const
163  { return m_addr.gateway(); }
164  inline satcat5::udp::Port srcport() const
165  { return m_addr.srcport(); }
166  inline satcat5::udp::Dispatch* udp() const
167  { return m_addr.udp(); }
168 
169  protected:
171  : m_addr(iface) {}
172  ~AddressContainer() {}
173  satcat5::udp::Address m_addr;
174  };
175  }
176 }
Implemention of "net::Dispatch" for Ethernet frames.
Definition: eth_dispatch.h:21
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
bool ready() const override
Is this address object ready for use? Child MUST override this method.
Definition: ip_address.h:68
void close() override
Close any open connections and revert to idle.
Definition: ip_address.cc:108
bool reply_is_multicast() const override
Was the parent interface's incoming message sent to a multicast address? (i.e., Could that message ha...
Definition: ip_address.cc:148
bool is_multicast() const override
Is the destination a broadcast or multicast address? Child MUST override these method.
Definition: ip_address.cc:135
void retry() override
If this Address is not in the ready() state, reattempt any steps required to do so,...
Definition: ip_address.cc:101
Defines a generic API for sending data to a specific destination, such as a MAC address,...
Definition: net_address.h:30
The "Dispatch" is a generic interface that knows how to read a designated protocol layer and sort inc...
Definition: net_dispatch.h:36
Inheritable container for a udp::Address.
Definition: udp_core.h:149
Implementation of "net::Address" for UDP Dispatch.
Definition: udp_core.h:72
bool ready() const override
Is this address object ready for use? Child MUST override this method.
Definition: udp_core.h:103
bool reply_is_multicast() const override
Was the parent interface's incoming message sent to a multicast address? (i.e., Could that message ha...
Definition: udp_core.h:110
void save_reply_address() override
Bind this Address object to the parent interface's current reply address, as provided in net::Dispatc...
Definition: udp_core.cc:75
satcat5::net::Dispatch * iface() const override
Fetch a pointer to the underlying interface.
Definition: udp_core.cc:62
void retry() override
If this Address is not in the ready() state, reattempt any steps required to do so,...
Definition: udp_core.h:104
Address(satcat5::udp::Dispatch *iface)
Connect through the specified UDP interface.
Definition: udp_core.cc:14
satcat5::io::Writeable * open_write(unsigned len) override
Open a new frame to the designated address and type.
Definition: udp_core.cc:71
void connect(const satcat5::udp::Addr &dstaddr, const satcat5::eth::MacAddr &dstmac, const satcat5::udp::Port &dstport, const satcat5::udp::Port &srcport=satcat5::udp::PORT_NONE, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Manual address resolution (user supplies IP + MAC).
Definition: udp_core.cc:30
void close() override
Close any open connections and revert to idle.
Definition: udp_core.h:102
bool matches_reply_address() const override
Does this Address object match the parent interface's current reply address? Multicast destinations s...
Definition: udp_core.cc:55
bool is_multicast() const override
Is the destination a broadcast or multicast address? Child MUST override these method.
Definition: udp_core.h:107
void init(satcat5::udp::Dispatch *iface)
Deferred initialization of the upstream interface.
Definition: udp_core.cc:23
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
Generic network Dispatch API.
An Ethernet frame header.
Definition: eth_header.h:167
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
Structure for holding an IPv4 Header, including options.
Definition: ip_core.h:180
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
UDP header contents.
Definition: udp_core.h:48
u16 length
Length of contained data.
Definition: udp_core.h:52
satcat5::udp::Port dst
Destination port.
Definition: udp_core.h:51
void write_to(satcat5::io::Writeable *wr) const
Write UDP header to the designated stream.
Definition: udp_core.cc:83
satcat5::udp::Port src
Source port.
Definition: udp_core.h:50
bool read_from(satcat5::io::Readable *rd)
Read UDP header from the designated stream.
Definition: udp_core.cc:91