diff options
author | dzwdz | 2023-08-14 18:51:07 +0200 |
---|---|---|
committer | dzwdz | 2023-08-14 18:51:07 +0200 |
commit | 642b5fb0007b64c77d186fcb018d571152ee1d47 (patch) | |
tree | 1c466461f3602d306be309a053edae558ef2568e /src/cmd/netstack/util.c | |
parent | 8050069c57b729c18c19b1a03ab6e4bf63b4735e (diff) |
reorganization: first steps
Diffstat (limited to 'src/cmd/netstack/util.c')
-rw-r--r-- | src/cmd/netstack/util.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/cmd/netstack/util.c b/src/cmd/netstack/util.c new file mode 100644 index 0000000..68092aa --- /dev/null +++ b/src/cmd/netstack/util.c @@ -0,0 +1,60 @@ +#include "util.h" + +/* https://www.w3.org/TR/PNG/#D-CRCAppendix */ +static uint32_t crc_table[256]; +uint32_t crc32(const uint8_t *buf, size_t len) { + if (!crc_table[1]) { + for (int i = 0; i < 256; i++) { + uint32_t c = i; + for (int j = 0; j < 8; j++) + c = ((c&1) ? 0xedb88320 : 0) ^ (c >> 1); + crc_table[i] = c; + } + } + + uint32_t c = 0xFFFFFFFF; + for (size_t i = 0; i < len; i++) + c = crc_table[(c ^ buf[i]) & 0xff] ^ (c >> 8); + return ~c; +} + +uint16_t ip_checksum(const uint8_t *buf, size_t len) { + uint32_t c = 0; + while (len >= 2) { + c += nget16(buf); + buf += 2; len -= 2; + } + if (len) c += (*buf) << 8; + while (c > 0xFFFF) c = (c & 0xFFFF) + (c >> 16); + return ~c; +} + +uint16_t ip_checksumphdr( + const uint8_t *buf, size_t len, + uint32_t ip1, uint32_t ip2, + uint16_t proto) +{ + uint32_t c = (uint16_t)~ip_checksum(buf, len); + c += (ip1 & 0xFFFF) + (ip1 >> 16); + c += (ip2 & 0xFFFF) + (ip2 >> 16); + c += proto + len; + while (c > 0xFFFF) c = (c & 0xFFFF) + (c >> 16); + return ~c; +} + +int ip_parse(const char *s, uint32_t *dest) { + if (!s) return -1; + + uint32_t ip = strtol(s, (char**)&s, 0); + int parts = 1; + for (; parts < 4; parts++) { + if (!*s) break; + if (*s++ != '.') return -1; + ip <<= 8; + ip += strtol(s, (char**)&s, 0); + } + if (parts > 1) + ip = ((ip & 0xFFFFFF00) << 8 * (4 - parts)) | (ip & 0xFF); + *dest = ip; + return 0; +} |