diff options
author | dzwdz | 2022-08-22 18:14:31 +0200 |
---|---|---|
committer | dzwdz | 2022-08-22 18:14:31 +0200 |
commit | fcdadf5df39e1d72f9ac79fa384fc6b98be0b1aa (patch) | |
tree | 988b1c47a3f4e0689df4644bd7b16095d01877a5 /src/user/app/netstack/util.c | |
parent | 55900142023f7a27d467c7ce6a61d2e5ecead4e3 (diff) |
user/netstack: ip_parse
Diffstat (limited to 'src/user/app/netstack/util.c')
-rw-r--r-- | src/user/app/netstack/util.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/user/app/netstack/util.c b/src/user/app/netstack/util.c index 9981aa3..e9d3118 100644 --- a/src/user/app/netstack/util.c +++ b/src/user/app/netstack/util.c @@ -29,3 +29,20 @@ uint16_t ip_checksum(const uint8_t *buf, size_t len) { 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; +} |