summaryrefslogtreecommitdiff
path: root/src/arpa/inet/inet_pton.c
diff options
context:
space:
mode:
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;
+}