6 #include <satcat5/ip_dispatch.h>
10 using satcat5::eth::ETYPE_IPV4;
15 namespace ip = satcat5::ip;
16 namespace log = satcat5::log;
19 static const unsigned DEBUG_VERBOSE = 0;
25 : Protocol(
Type(ETYPE_IPV4.value))
32 , m_reply_dst(ip::ADDR_NONE)
33 , m_reply_src(ip::ADDR_NONE)
40 satcat5::util::prng.
seed(SATCAT5_CLOCK->raw());
41 m_ident = u16(satcat5::util::prng.next());
44 #if SATCAT5_ALLOW_DELETION
45 Dispatch::~Dispatch() {
52 return open_write(m_iface->reply_mac(), m_iface->reply_vtag(), m_reply_src, type.
as_u8(), len);
62 if (DEBUG_VERBOSE > 1)
63 log::Log(log::DEBUG,
"IpDispatch: open_write");
80 u8 protocol,
const ip::Addr& dst,
unsigned inner_bytes, u8 ttl)
83 u16 ttl_word = 256 * u16(ttl) + protocol;
84 u16 len_total = (u16)(inner_bytes + ip::HDR_MIN_BYTES);
89 hdr.
data[1] = len_total;
90 hdr.
data[2] = m_ident++;
92 hdr.
data[4] = ttl_word;
107 if (DEBUG_VERBOSE > 1)
112 if (ok && DEBUG_VERBOSE > 1) {
113 log::Log(log::DEBUG,
"IpDispatch: Header OK");
114 }
else if (!ok && DEBUG_VERBOSE > 0) {
120 if (m_reply_hdr.
frg())
return;
123 m_reply_dst = m_reply_hdr.
dst();
124 m_reply_src = m_reply_hdr.
src();
127 bool accept = (m_reply_dst == m_addr)
129 || (m_addr == ip::ADDR_NONE);
139 m_icmp.
send_error(ICMP_UNREACHABLE_PROTO, &rd);
Implemention of "net::Dispatch" for Ethernet frames.
satcat5::io::Writeable * open_write(const satcat5::eth::MacAddr &dst, const satcat5::eth::MacType &type, satcat5::eth::VlanTag vtag=satcat5::eth::VTAG_NONE)
Send a frame to the designated Ethernet address/VLAN.
void set_ipaddr(const satcat5::ip::Addr &ipaddr)
Set the local IP address.
void add(satcat5::eth::ArpListener *evt)
Register an event-listener.
void remove(satcat5::eth::ArpListener *evt)
Unregister an event-listener.
Limited read of next N bytes.
unsigned get_read_ready() const override
How many bytes can be read without blocking?
Abstract API for writing byte-streams and packets.
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
bool route_cache(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac)
Routing table shortcuts.
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.
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
satcat5::io::Writeable * open_reply(const satcat5::net::Type &type, unsigned len) override
Get Writeable object for reply to the last received datagram.
void arp_event(const satcat5::eth::MacAddr &mac, const satcat5::ip::Addr &ip) override
Callback for any announced MAC/IP address pair.
void set_addr(const satcat5::ip::Addr &addr)
Set the local IP-address.
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.
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.
void seed(u64 seed)
Reset to the provided PRNG state.
Diagnostic logging to UART and/or Ethernet ports.
An Ethernet MAC address (with serializable interface).
Header contents for an 802.1Q Virtual-LAN tag.
IPv4 address is a 32-bit unsigned integer.
u32 value
Raw access to the underlying representation.
bool is_multicast() const
IP multicast (224.*.*.*)
Multipurpose filter for matching fields in network packets.
u8 as_u8() const
Accessors for m_value.
TimeRef and TimeVal define the API for monotonic timers.