summaryrefslogtreecommitdiff
path: root/src/arpa/inet/inet_pton.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-10-29 15:57:00 -0400
committerJakob Kaivo <jkk@ung.org>2020-10-29 15:57:00 -0400
commit972d2eee3a3518f18e0958e6f8a1b2ffafd6d39a (patch)
tree52fa9568b8c64e70ecbadc39ef63b35157731312 /src/arpa/inet/inet_pton.c
parent097e3e2dc18091096fc846fcc15402accf906448 (diff)
outline stuff from arpa/inet.h
Diffstat (limited to 'src/arpa/inet/inet_pton.c')
-rw-r--r--src/arpa/inet/inet_pton.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/arpa/inet/inet_pton.c b/src/arpa/inet/inet_pton.c
new file mode 100644
index 00000000..14e7219b
--- /dev/null
+++ b/src/arpa/inet/inet_pton.c
@@ -0,0 +1,43 @@
+#include <arpa/inet.h>
+#include <errno.h>
+#include <stdlib.h>
+
+int inet_pton(int af, const char *restrict src, void *restrict dst)
+{
+ if (af == AF_INET) {
+ in_addr_t *a = dst;
+ unsigned long parts[4] = { 0 };
+ size_t part = 0;
+
+ while (*src && part < sizeof(parts)) {
+ char *next = NULL;
+ parts[part] = strtoul(src, &next, 0);
+ if (parts[part] > 255) {
+ return 0;
+ }
+
+ if (*next == '.') {
+ part++;
+ src = next + 1;
+ } else {
+ src = next;
+ }
+
+ }
+
+ if (part != sizeof(parts) - 1) {
+ return 0;
+ }
+
+ *a = htonl((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]);
+ return 1;
+ }
+
+ if (af != AF_INET6) {
+ errno = EAFNOSUPPORT;
+ return -1;
+ }
+
+ /* do ipv6 conversion */
+ return 1;
+}