git: 9front

Download patch

ref: e4b29e4f526a870862e4e1a2bc567a9e00978209
parent: 04383f1ea8bac85faf2b8dfac6ca1d8caade8136
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Mon Apr 1 15:16:18 EDT 2013

ape: add inet_aton()

--- a/sys/include/ape/netinet/in.h
+++ b/sys/include/ape/netinet/in.h
@@ -163,8 +163,8 @@
 extern unsigned long	htonl(unsigned long x);
 extern unsigned short	htons(unsigned short x);
 extern unsigned long	inet_addr(char*);
+extern int		inet_aton(char*, struct in_addr*);
 extern char*		inet_ntoa(struct in_addr);
-extern unsigned long	nptohl(void*);
 
 extern char*		inet_ntop(int af, void *src, char *dst, int size);
 extern int		inet_pton(int af, char *src, void *dst);
--- a/sys/src/ape/lib/bsd/gethostbyname.c
+++ b/sys/src/ape/lib/bsd/gethostbyname.c
@@ -31,7 +31,7 @@
 	int i, t, fd, m;
 	char *p, *k, *bp;
 	int nn, na;
-	unsigned long x;
+	struct in_addr in;
 	static struct hostent h;
 	static char buf[1024];
 	static char *nptr[Nname+1];
@@ -98,15 +98,10 @@
 			if(nn < Nname)
 				nptr[nn++] = p;
 		} else if(strcmp(k, "ip") == 0){
-			if(strchr(p, ':') != 0)
-				continue;	/* ignore ipv6 addresses */
-			x = inet_addr(p);
-			x = ntohl(x);
+			if(inet_aton(p, &in) == 0)
+				continue;
 			if(na < Nname){
-				addr[na][0] = x>>24;
-				addr[na][1] = x>>16;
-				addr[na][2] = x>>8;
-				addr[na][3] = x;
+				memmove(addr[na], (unsigned char*)&in.s_addr, 4);
 				aptr[na] = addr[na];
 				na++;
 			}
--- a/sys/src/ape/lib/bsd/inet_addr.c
+++ b/sys/src/ape/lib/bsd/inet_addr.c
@@ -9,44 +9,12 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 
-#define CLASS(x)	(x[0]>>6)
-
 unsigned long
 inet_addr(char *from)
 {
-	int i;
-	char *p;
-	unsigned char to[4];
-	unsigned long x;
- 
-	p = from;
-	memset(to, 0, 4);
-	for(i = 0; i < 4 && *p; i++){
-		to[i] = strtoul(p, &p, 0);
-		if(*p == '.')
-			p++;
-	}
+	struct in_addr in;
 
-	switch(CLASS(to)){
-	case 0:	/* class A - 1 byte net */
-	case 1:
-		if(i == 3){
-			to[3] = to[2];
-			to[2] = to[1];
-			to[1] = 0;
-		} else if (i == 2){
-			to[3] = to[1];
-			to[1] = 0;
-		}
-		break;
-	case 2:	/* class B - 2 byte net */
-		if(i == 3){
-			to[3] = to[2];
-			to[2] = 0;
-		}
-		break;
-	}
-	x = nptohl(to);
-	x = htonl(x);
-	return x;
+	if(inet_aton(from, &in) == 0)
+		return INADDR_NONE;
+	return in.s_addr;
 }
--- /dev/null
+++ b/sys/src/ape/lib/bsd/inet_aton.c
@@ -1,0 +1,57 @@
+/* posix */
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* bsd extensions */
+#include <sys/uio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#define CLASS(x)	(x[0]>>6)
+
+int
+inet_aton(char *from, struct in_addr *in)
+{
+	unsigned char *to;
+	unsigned long x;
+	char *p;
+	int i;
+
+	in->s_addr = 0;
+	to = (unsigned char*)&in->s_addr;
+	if(*from == 0)
+		return 0;
+	for(i = 0; i < 4 && *from; i++, from = p){
+		x = strtoul(from, &p, 0);
+		if(x != (unsigned char)x || p == from)
+			return 0;	/* parse error */
+		to[i] = x;
+		if(*p == '.')
+			p++;
+		else if(*p != 0)
+			return 0;	/* parse error */
+	}
+
+	switch(CLASS(to)){
+	case 0:	/* class A - 1 byte net */
+	case 1:
+		if(i == 3){
+			to[3] = to[2];
+			to[2] = to[1];
+			to[1] = 0;
+		} else if (i == 2){
+			to[3] = to[1];
+			to[1] = 0;
+		}
+		break;
+	case 2:	/* class B - 2 byte net */
+		if(i == 3){
+			to[3] = to[2];
+			to[2] = 0;
+		}
+		break;
+	}
+	return 1;
+}
--- a/sys/src/ape/lib/bsd/inet_pton.c
+++ b/sys/src/ape/lib/bsd/inet_pton.c
@@ -36,10 +36,8 @@
 	unsigned long x;
 	char *p, *op;
 
-	if(af == AF_INET){
-		((struct in_addr*)dst)->s_addr = inet_addr(src);
-		return 1;
-	}
+	if(af == AF_INET)
+		return inet_aton(src, (struct in_addr*)dst);
 
 	if(af != AF_INET6){
 		errno = EAFNOSUPPORT;
--- a/sys/src/ape/lib/bsd/mkfile
+++ b/sys/src/ape/lib/bsd/mkfile
@@ -25,6 +25,7 @@
 	gettimeofday.$O\
 	in6_addr.$O\
 	inet_addr.$O\
+	inet_aton.$O\
 	inet_ntoa.$O\
 	inet_ntop.$O\
 	inet_pton.$O\
@@ -33,7 +34,6 @@
 	lstat.$O\
 	mktemp.$O\
 	ntohl.$O\
-	nptohl.$O\
 	popen.$O\
 	rcmd.$O\
 	readv.$O\
--- a/sys/src/ape/lib/bsd/nptohl.c
+++ /dev/null
@@ -1,10 +1,0 @@
-unsigned long
-nptohl(void *p)
-{
-	unsigned char *up;
-	unsigned long x;
-
-	up = p;
-	x = (up[0]<<24)|(up[1]<<16)|(up[2]<<8)|up[3];
-	return x;
-}
--- a/sys/src/cmd/python/pyconfig.h
+++ b/sys/src/cmd/python/pyconfig.h
@@ -276,7 +276,7 @@
 #define HAVE_HYPOT 1
 
 /* Define if you have the 'inet_aton' function. */
-/* #undef HAVE_INET_ATON */
+#define HAVE_INET_ATON 1
 
 /* Define if you have the 'inet_pton' function. */
 #define HAVE_INET_PTON 1
--