]> Chaos Git - corbenik/ctrulib.git/commitdiff
added inet_pton and inet_ntop
authorLectem <lectem@gmail.com>
Sun, 17 Jan 2016 17:58:21 +0000 (12:58 -0500)
committerLectem <lectem@gmail.com>
Sun, 17 Jan 2016 17:58:21 +0000 (12:58 -0500)
libctru/include/arpa/inet.h
libctru/source/services/soc/soc_inet_ntop.c [new file with mode: 0644]
libctru/source/services/soc/soc_inet_pton.c [new file with mode: 0644]

index 1052c6beece7f026e7dd099599ea13feedf3d72e..5e044a9c30a37a70fbb2c0ecbd29e7e87c8a4c17 100644 (file)
@@ -31,6 +31,9 @@ extern "C" {
        int       inet_aton(const char *cp, struct in_addr *inp);
        char*     inet_ntoa(struct in_addr in);
 
+       const char *inet_ntop(int af, const void *restrict src, char *restrict dst, socklen_t size);
+       int        inet_pton(int af, const char *restrict src, void *restrict dst);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libctru/source/services/soc/soc_inet_ntop.c b/libctru/source/services/soc/soc_inet_ntop.c
new file mode 100644 (file)
index 0000000..68016d4
--- /dev/null
@@ -0,0 +1,28 @@
+#include "soc_common.h"
+#include <arpa/inet.h>
+#include <stdio.h>
+
+
+static const char *inet_ntop4(const void *restrict src, char *restrict dst, socklen_t size)
+{
+       const u8 * ip = src;
+       if(size < INET_ADDRSTRLEN)
+       {
+               errno = ENOSPC;
+               return NULL;
+       }
+       snprintf(dst,size,"%hhu.%hhu.%hhu.%hhu",ip[0], ip[1], ip[2], ip[3]);
+       return dst;
+}
+
+
+const char *inet_ntop(int af, const void *restrict src, char *restrict dst, socklen_t size)
+{
+       if(af == AF_INET)
+       {
+               return inet_ntop4(src,dst,size);
+       }
+       // only support IPv4
+       errno = EAFNOSUPPORT;
+       return NULL;
+}
diff --git a/libctru/source/services/soc/soc_inet_pton.c b/libctru/source/services/soc/soc_inet_pton.c
new file mode 100644 (file)
index 0000000..dc8bba9
--- /dev/null
@@ -0,0 +1,24 @@
+#include "soc_common.h"
+#include <arpa/inet.h>
+#include <stdio.h>
+
+
+static int inet_pton4(const char *restrict src, void *restrict dst)
+{
+       u8 ip[4];
+       if(sscanf(src,"%hhu.%hhu.%hhu.%hhu",&ip[0], &ip[1], &ip[2], &ip[3]) != 4) return 0;
+       *(u32*)dst = *(u32*)ip;
+       return 1;
+}
+
+
+int inet_pton(int af, const char *restrict src, void *restrict dst)
+{
+       if(af == AF_INET)
+       {
+               return inet_pton4(src,dst);
+       }
+       // only support IPv4
+       errno = EAFNOSUPPORT;
+       return -1;
+}