6 #include <satcat5/ip_dispatch.h>
8 #include <satcat5/udp_dispatch.h>
12 using satcat5::ip::PROTO_UDP;
15 using satcat5::udp::HEADER_BYTES;
16 namespace log = satcat5::log;
19 static const unsigned DEBUG_VERBOSE = 0;
22 #ifndef SATCAT5_UDP_MAXDYN
23 #define SATCAT5_UDP_MAXDYN 16384
27 static_assert(SATCAT5_UDP_MAXDYN <= 49152);
28 static constexpr u16 DYNAMIC_PORT_MAX = 0xFFFF;
29 static constexpr u16 DYNAMIC_PORT_MIN = DYNAMIC_PORT_MAX + 1 - SATCAT5_UDP_MAXDYN;
32 : satcat5::net::Protocol(
Type(PROTO_UDP))
34 , m_next_port(DYNAMIC_PORT_MIN - 1)
35 , m_reply_src(satcat5::udp::PORT_NONE)
36 , m_reply_dst(satcat5::udp::PORT_NONE)
41 #if SATCAT5_ALLOW_DELETION
53 u16 wrap = m_next_port;
56 m_next_port = (m_next_port < DYNAMIC_PORT_MAX)
57 ? (m_next_port + 1) : (DYNAMIC_PORT_MIN);
62 if (m_next_port == wrap) {
63 log::Log(log::WARNING,
"UdpDispatch: Ports full");
64 return satcat5::udp::PORT_NONE;
76 m_iface->reply_vtag());
90 if (DEBUG_VERBOSE > 1)
94 unsigned total_len = len + HEADER_BYTES;
101 wr->write_u16((u16)total_len);
109 if (DEBUG_VERBOSE > 1)
118 u16 len = src.read_u16();
119 u16 chk = src.read_u16();
122 unsigned len_eff = len - HEADER_BYTES;
124 if (DEBUG_VERBOSE > 0)
134 bool ok =
deliver(type2, &src, len_eff) ||
deliver(type1, &src, len_eff);
136 if (DEBUG_VERBOSE > 0 && !ok)
138 if (DEBUG_VERBOSE > 1 && ok)
155 m_iface->icmp()->
send_error(satcat5::ip::ICMP_UNREACHABLE_PORT, &rd);
Ephemeral Readable interface for a simple array.
unsigned written_len() const
Report total length after write_finalize() is called.
bool write_finalize() override
Mark end of frame and release temporary working data.
const u8 * buffer() const
Read-only access to the working buffer.
Thin wrapper for ArrayWrite with a built-in buffer.
Limited read of next N bytes.
unsigned get_read_ready() const override
How many bytes can be read without blocking?
bool read_obj(T &t)
Templated wrapper for any object with the following method: bool read_from(satcat5::io::Readable* rd)...
unsigned copy_to(satcat5::io::Writeable *dst)
Copy data to a Writeable object, without finalizing.
Abstract API for writing byte-streams and packets.
void write_obj(const T &obj)
Templated wrapper for any object with the following method: void write_to(satcat5::io::Writeable* wr)...
Connection metadata for an IPv4 address.
void connect(const satcat5::ip::Addr &dstaddr, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Automatic address resolution using routing table + ARP.
satcat5::io::Writeable * open_write(unsigned len) override
Open a new frame to the designated address and type.
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
bool send_error(u16 type, satcat5::io::Readable *src, uint32_t arg=0)
Send a specific error message.
The Log class creates and formats one log message.
Log & write(const char *str)
Formatting methods for various data types.
void add(satcat5::net::Protocol *proto)
Register a Protocol object.
bool deliver(const satcat5::net::Type &type, satcat5::io::Readable *src, unsigned len)
Deliver current message by calling Protocol::frame_rcvd(...).
void remove(satcat5::net::Protocol *proto)
Unregister a Protocol object.
bool bound(const satcat5::net::Type &type) const
Check if a given net::Type has a matching Protocol.
Dispatcher sorts incoming UDP messages by port index.
satcat5::io::Writeable * open_write(satcat5::ip::Address &addr, const satcat5::udp::Port &src, const satcat5::udp::Port &dst, unsigned len)
Send a datagram to the designated UDP address and port.
satcat5::io::Writeable * open_reply(const satcat5::net::Type &type, unsigned nbytes) override
Reply to the sender of the most recent received message.
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
satcat5::udp::Port next_free_port()
Get the next unclaimed dynamically-allocated port index.
Diagnostic logging to UART and/or Ethernet ports.
IPv4 address is a 32-bit unsigned integer.
bool is_unicast() const
Any normal single-destination address.
UDP and TCP ports are both 16-bit unsigned integers.
u16 value
Raw access to the underlying representation.
Multipurpose filter for matching fields in network packets.
Miscellaneous mathematical utility functions.