summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@gmx.de>2013-04-01 19:16:18 +0200
committercinap_lenrek <cinap_lenrek@gmx.de>2013-04-01 19:16:18 +0200
commite8a02760901e0b700ba845c9f57601f662e0e0aa (patch)
tree016c9e31ff1b3182e7b3492b4abf2de6c8d469d1
parentbbe95085d4e4cc1efc26d236c29edcc9714a1524 (diff)
ape: add inet_aton()
-rw-r--r--sys/include/ape/netinet/in.h2
-rw-r--r--sys/src/ape/lib/bsd/gethostbyname.c13
-rw-r--r--sys/src/ape/lib/bsd/inet_addr.c40
-rw-r--r--sys/src/ape/lib/bsd/inet_aton.c57
-rw-r--r--sys/src/ape/lib/bsd/inet_pton.c6
-rw-r--r--sys/src/ape/lib/bsd/mkfile2
-rw-r--r--sys/src/ape/lib/bsd/nptohl.c10
-rw-r--r--sys/src/cmd/python/pyconfig.h2
8 files changed, 70 insertions, 62 deletions
diff --git a/sys/include/ape/netinet/in.h b/sys/include/ape/netinet/in.h
index 8444c1bcf..339f1c084 100644
--- a/sys/include/ape/netinet/in.h
+++ b/sys/include/ape/netinet/in.h
@@ -163,8 +163,8 @@ extern unsigned short ntohs(unsigned short x);
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);
diff --git a/sys/src/ape/lib/bsd/gethostbyname.c b/sys/src/ape/lib/bsd/gethostbyname.c
index 9393865fb..ee788e527 100644
--- a/sys/src/ape/lib/bsd/gethostbyname.c
+++ b/sys/src/ape/lib/bsd/gethostbyname.c
@@ -31,7 +31,7 @@ gethostbyname(char *name)
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 @@ gethostbyname(char *name)
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++;
}
diff --git a/sys/src/ape/lib/bsd/inet_addr.c b/sys/src/ape/lib/bsd/inet_addr.c
index 63cd69cc4..3f6c032e8 100644
--- 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;
}
diff --git a/sys/src/ape/lib/bsd/inet_aton.c b/sys/src/ape/lib/bsd/inet_aton.c
new file mode 100644
index 000000000..8af377862
--- /dev/null
+++ b/sys/src/ape/lib/bsd/inet_aton.c
@@ -0,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;
+}
diff --git a/sys/src/ape/lib/bsd/inet_pton.c b/sys/src/ape/lib/bsd/inet_pton.c
index 863124b00..0e10ab5cf 100644
--- a/sys/src/ape/lib/bsd/inet_pton.c
+++ b/sys/src/ape/lib/bsd/inet_pton.c
@@ -36,10 +36,8 @@ inet_pton(int af, char *src, void *dst)
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;
diff --git a/sys/src/ape/lib/bsd/mkfile b/sys/src/ape/lib/bsd/mkfile
index 5f95c9b02..886b599ee 100644
--- a/sys/src/ape/lib/bsd/mkfile
+++ b/sys/src/ape/lib/bsd/mkfile
@@ -25,6 +25,7 @@ OFILES=\
gettimeofday.$O\
in6_addr.$O\
inet_addr.$O\
+ inet_aton.$O\
inet_ntoa.$O\
inet_ntop.$O\
inet_pton.$O\
@@ -33,7 +34,6 @@ OFILES=\
lstat.$O\
mktemp.$O\
ntohl.$O\
- nptohl.$O\
popen.$O\
rcmd.$O\
readv.$O\
diff --git a/sys/src/ape/lib/bsd/nptohl.c b/sys/src/ape/lib/bsd/nptohl.c
deleted file mode 100644
index c012493e9..000000000
--- a/sys/src/ape/lib/bsd/nptohl.c
+++ /dev/null
@@ -1,10 +0,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;
-}
diff --git a/sys/src/cmd/python/pyconfig.h b/sys/src/cmd/python/pyconfig.h
index c586448f0..712e65653 100644
--- a/sys/src/cmd/python/pyconfig.h
+++ b/sys/src/cmd/python/pyconfig.h
@@ -276,7 +276,7 @@ typedef unsigned long u_long;
#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