11 using satcat5::eth::MACADDR_BROADCAST;
12 using satcat5::eth::MACADDR_NONE;
18 namespace ip = satcat5::ip;
20 static constexpr
Route ROUTE_NONE = {
21 ip::DEFAULT_ROUTE, ip::ADDR_NONE, MACADDR_NONE, 0, 0};
23 static constexpr
Route ROUTE_LOCAL = {
24 ip::DEFAULT_ROUTE, ip::ADDR_BROADCAST, MACADDR_NONE, 0, 0};
26 inline constexpr
Route simple_route(
28 {
return {{addr, ip::MASK_32}, addr, dstmac, port, flags}; }
32 if (
gateway == ip::ADDR_BROADCAST) {
54 , m_wridx_ephemeral(SATCAT5_ROUTING_TABLE-1)
60 wr.
wr_str(
"Static routes");
65 for (
unsigned a = 0 ; a < m_wridx_static ; ++a) {
75 m_wridx_ephemeral = SATCAT5_ROUTING_TABLE-1;
77 for (
unsigned a = 0 ; a < SATCAT5_ROUTING_TABLE ; ++a)
82 m_wridx_ephemeral = SATCAT5_ROUTING_TABLE-1;
83 for (
unsigned a = 0 ; a < SATCAT5_ROUTING_TABLE ; ++a) {
87 if (a < m_wridx_static) {
89 temp.
dstmac = MACADDR_NONE;
102 return route_wrdef({satcat5::ip::DEFAULT_ROUTE, gateway, dstmac, port, flags});
118 if (subnet == ip::DEFAULT_ROUTE) {
123 for (
unsigned a = 0 ; a < m_wridx_static ; ++a) {
125 return route_write(a, {subnet, gateway, dstmac, port, flags});
131 if (m_wridx_static < SATCAT5_ROUTING_TABLE) {
132 return route_write(m_wridx_static++, {subnet, gateway, dstmac, port, flags});
146 bool self_match =
false;
147 for (
unsigned a = 0 ; a < SATCAT5_ROUTING_TABLE ; ++a) {
158 if (self_match)
return true;
161 if (m_wridx_static >= SATCAT5_ROUTING_TABLE)
return false;
166 u8 flags = best.
flags;
172 if (m_wridx_ephemeral < m_wridx_static || m_wridx_ephemeral >= SATCAT5_ROUTING_TABLE)
173 m_wridx_ephemeral = SATCAT5_ROUTING_TABLE - 1;
174 route_write(m_wridx_ephemeral--, simple_route(gateway, dstmac, best.
port, flags));
182 for (
unsigned a = 0 ; a < m_wridx_static ; ++a) {
184 unsigned last = --m_wridx_static;
191 for (
unsigned a = m_wridx_static ; a < SATCAT5_ROUTING_TABLE ; ++a) {
201 if (dstaddr.
is_multicast())
return simple_route(dstaddr, MACADDR_BROADCAST);
202 if (dstaddr == ip::ADDR_NONE)
return simple_route(ip::ADDR_NONE, MACADDR_NONE);
209 for (
unsigned a = 0 ; a < SATCAT5_ROUTING_TABLE ; ++a) {
223 : m_route_default(ROUTE_LOCAL)
224 , m_route_table{ROUTE_NONE}
230 m_route_default = route;
237 m_route_table[idx] = route;
An array of routes with read and write accessors.
RouteArray()
Create an empty table.
virtual bool route_wrdef(const satcat5::ip::Route &route)
Internal methods used to access "m_route_default".
virtual bool route_write(unsigned idx, const satcat5::ip::Route &route)
Internal methods used to access "m_route_table".
const satcat5::ip::Route & route_rddef() const
Internal methods used to access "m_route_default".
const satcat5::ip::Route & route_read(unsigned idx) const
Internal methods used to access "m_route_table".
Table()
Construct an empty table with a local default route.
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.
satcat5::ip::Route route_lookup(const satcat5::ip::Addr &dstaddr) const
Next-hop routing lookup for the given destination address.
void route_clear(bool lockdown=true)
Clear all routes, including the default.
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.
void log_to(satcat5::log::LogBuffer &wr) const
Create a log entry with the full contents of this table.
bool route_local(const satcat5::ip::Subnet &subnet, u8 port=0, u8 flags=0)
Create or update a local static route.
bool route_cache(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac)
Update matching MAC address cache entries.
bool route_remove(const satcat5::ip::Subnet &subnet)
Remove a single static route.
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.
void route_flush()
Flush cached MAC-addresses.
Internal buffer used by the Log class.
void wr_h32(u32 val, unsigned nhex=8)
Write an integer (u32) in hexadecimal format.
void wr_str(const char *str)
Write a null-terminated UTF-8 string.
void wr_d32(u32 val, unsigned zpad=0)
Write an unsigned integer (u32) in decimal format.
Internet Protocol v4 (IPv4) forwarding table.
Diagnostic logging to UART and/or Ethernet ports.
An Ethernet MAC address (with serializable interface).
bool is_valid() const
Any nonzero address.
void log_to(satcat5::log::LogBuffer &wr) const
Format this field as a human-readable string.
bool is_unicast() const
Any normal single-destination address.
IPv4 address is a 32-bit unsigned integer.
u32 value
Raw access to the underlying representation.
bool is_multicast() const
IP multicast (224.*.*.*)
bool is_unicast() const
Any normal single-destination address.
void log_to(satcat5::log::LogBuffer &wr) const
Format this IP address in standard form, e.g., "192.168.1.2".
IPv4 subnet masks share functionality with a basic address, but are constructed differently to match ...
A single entry in the static routing table.
satcat5::eth::MacAddr dstmac
Next-hop MAC address.
u8 flags
Additional flags.
void log_to(satcat5::log::LogBuffer &wr) const
Format a one-line log entry containing all route parameters.
static constexpr u8 FLAG_MAC_FIXED
Fixed MAC-address for this route? If this flag is set, then the user specified the MAC address.
satcat5::ip::Subnet subnet
Subnet address + mask.
u8 port
Next-hop port number.
satcat5::ip::Addr gateway
Next-hop IPv4 address.
An IPv4 subnet consists of a base address and a subnet mask.
void log_to(satcat5::log::LogBuffer &wr) const
Format this subnet in CIDR form, e.g., "192.168.1.0/24".
satcat5::ip::Mask mask
Subnet mask.
constexpr bool contains(const satcat5::ip::Addr &other) const
Does this subnet contain the given address?
Miscellaneous mathematical utility functions.
void clr_mask_u8(volatile u8 &val, u8 mask)
Set or clear bit masks.
void set_mask_u8(volatile u8 &val, u8 mask)
Set or clear bit masks.