6 #include <satcat5/ip_core.h>
23 for (
unsigned a = 0 ; a < 4 ; ++a) {
38 return (value == 0xFFFFFFFFu);
42 if (value == 0xFFFFFFFFu)
44 else if (0xE0000000u <= value && value <= 0xEFFFFFFFu)
51 if (value <= 0x00FFFFFFu)
53 else if (0x7F000000u <= value && value <= 0x7FFFFFFFu)
60 return value && !is_multicast();
68 return satcat5::util::popcount(value);
73 u16 satcat5::ip::checksum(
unsigned wcount,
const u16* data, u16 prev) {
74 u32 sum = u32(~prev & 0xFFFF);
75 for (
unsigned a = 0 ; a < wcount ; ++a)
78 sum = (sum & UINT16_MAX) + (sum >> 16);
84 unsigned wcount = 2 *
ihl();
85 u16 chk_tmp =
data[5] ?
data[5] : ip::checksum(wcount,
data);
87 for (
unsigned a = 0 ; a <= 4 ; ++a)
88 wr->write_u16(
data[a]);
89 wr->write_u16(chk_tmp);
90 for (
unsigned a = 6 ; a < wcount ; ++a)
91 wr->write_u16(
data[a]);
96 u16 tmp[2] = {u16(~prev), next};
97 data[5] = satcat5::ip::checksum(2, tmp,
chk());
100 void Header::chk_incr32(u32 prev, u32 next) {
102 u32 tmp[2] = {u32(~prev), next};
103 data[5] = satcat5::ip::checksum(4, (u16*)tmp,
chk());
107 unsigned wrpos = 2 *
ihl();
108 if (wrpos + 2 > HDR_MAX_SHORTS)
return;
111 data[wrpos++] = 0x9404;
112 data[wrpos++] = 0x0000;
120 for (
unsigned a = 0 ; a < ip::HDR_MIN_SHORTS ; ++a)
121 data[a] = rd->read_u16();
132 unsigned hdr = 2 *
ihl();
133 unsigned rem = 2 * (hdr - ip::HDR_MIN_SHORTS);
141 for (
unsigned a = ip::HDR_MIN_SHORTS ; a < hdr ; ++a)
142 data[a] = rd->read_u16();
145 u16
chk = ip::checksum(hdr,
data);
Abstract API for reading byte-streams and packets.
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
Abstract API for writing byte-streams and packets.
Internal buffer used by the Log class.
void wr_str(const char *str)
Write a null-terminated UTF-8 string.
void wr_dec(u32 val)
Legacy alias for wr_d32.
Diagnostic logging to UART and/or Ethernet ports.
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".
bool is_valid() const
Any nonzero address.
bool is_reserved() const
Reserved blocks (0.*.*.* or 127.*.*.*)
bool is_broadcast() const
Limited broadcast (255.255.255.255)
unsigned prefix() const
Convert this subnet-mask to a CIDR prefix length.
void log_to(satcat5::log::LogBuffer &wr) const
Format this subnet in CIDR form, e.g., "192.168.1.0/24".
Miscellaneous mathematical utility functions.