SatCat5
udp_socket.cc
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/udp_socket.h>
7 
10 
12  : SocketCore(iface,
13  m_txbuff, SATCAT5_UDP_BUFFSIZE, SATCAT5_UDP_PACKETS,
14  m_rxbuff, SATCAT5_UDP_BUFFSIZE, SATCAT5_UDP_PACKETS)
15 {
16  // No other initialization required.
17 }
18 
20  : SocketCore(iface,
21  nullptr, 0, 0,
22  m_rxbuff, SATCAT5_UDP_BUFFSIZE, SATCAT5_UDP_PACKETS)
23 {
24  // No other initialization required.
25 }
26 
28  : SocketCore(iface,
29  m_txbuff, SATCAT5_UDP_BUFFSIZE, SATCAT5_UDP_PACKETS,
30  nullptr, 0, 0)
31 {
32  // No other initialization required.
33 }
34 
35 SocketCore::SocketCore(
37  u8* txbuff, unsigned txbytes, unsigned txpkt,
38  u8* rxbuff, unsigned rxbytes, unsigned rxpkt)
39  : satcat5::udp::AddressContainer(iface)
40  , satcat5::net::SocketCore(&m_addr,
41  txbuff, txbytes, txpkt,
42  rxbuff, rxbytes, rxpkt)
43 {
44  // No other initialization required.
45 }
46 
48  m_addr.close(); // Unbind Tx
49  m_filter = Type(port.value); // Rebind Rx
50 }
51 
53  const satcat5::udp::Addr& dstaddr,
54  const satcat5::eth::MacAddr& dstmac,
55  const satcat5::udp::Port& dstport,
56  satcat5::udp::Port srcport,
57  const satcat5::eth::VlanTag& vtag)
58 {
59  // If no source port is specified, assign one automatically.
60  if (srcport == satcat5::udp::PORT_NONE)
61  srcport = m_addr.udp()->next_free_port();
62  m_addr.connect(dstaddr, dstmac, dstport, srcport, vtag);
63  // Rebind Rx to the paired source + destination ports.
64  // TODO: For now, we are filtering on UDP port numbers only. To be fully
65  // compliant, it should also bind to the source and destination address.
66  if (srcport.value) m_filter = Type(dstport.value, srcport.value);
67 }
68 
70  const satcat5::udp::Addr& dstaddr,
71  const satcat5::udp::Port& dstport,
72  satcat5::udp::Port srcport,
73  const satcat5::eth::VlanTag& vtag)
74 {
75  // If no source port is specified, assign one automatically.
76  if (srcport == satcat5::udp::PORT_NONE)
77  srcport = m_addr.udp()->next_free_port();
78  m_addr.connect(dstaddr, dstport, srcport, vtag);
79  // Rebind Rx to the paired source + destination ports.
80  // TODO: For now, we are filtering on UDP port numbers only. To be fully
81  // compliant, it should also bind to the source and destination address.
82  if (srcport.value) m_filter = Type(dstport.value, srcport.value);
83 }
satcat5::net::Type m_filter
Incoming packet filter.
Definition: net_protocol.h:52
Inheritable container for a udp::Address.
Definition: udp_core.h:149
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
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
satcat5::udp::Port next_free_port()
Get the next unclaimed dynamically-allocated port index.
Definition: udp_dispatch.cc:48
BufferedIO wrapper for two-way UDP communication.
Definition: udp_socket.h:32
void connect(const satcat5::udp::Addr &dstaddr, const satcat5::eth::MacAddr &dstmac, const satcat5::udp::Port &dstport, satcat5::udp::Port srcport=satcat5::udp::PORT_NONE, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Connect with manual address resolution.
Definition: udp_socket.cc:52
void bind(const satcat5::udp::Port &port)
Listening mode only (no remote address).
Definition: udp_socket.cc:47
Socket(satcat5::udp::Dispatch *iface)
Connect this Socket to a network interface.
Definition: udp_socket.cc:11
SocketRx(satcat5::udp::Dispatch *iface)
Connect this Socket to a network interface.
Definition: udp_socket.cc:19
SocketTx(satcat5::udp::Dispatch *iface)
Connect this Socket to a network interface.
Definition: udp_socket.cc:27
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
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
u16 value
Raw access to the underlying representation.
Definition: ip_core.h:121
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38