summaryrefslogtreecommitdiff
path: root/src/user/app/netstack/util.c
diff options
context:
space:
mode:
authordzwdz2022-08-22 18:14:31 +0200
committerdzwdz2022-08-22 18:14:31 +0200
commitfcdadf5df39e1d72f9ac79fa384fc6b98be0b1aa (patch)
tree988b1c47a3f4e0689df4644bd7b16095d01877a5 /src/user/app/netstack/util.c
parent55900142023f7a27d467c7ce6a61d2e5ecead4e3 (diff)
user/netstack: ip_parse
Diffstat (limited to 'src/user/app/netstack/util.c')
-rw-r--r--src/user/app/netstack/util.c17
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;
+}