ape: add inet_aton()

This commit is contained in:
cinap_lenrek 2013-04-01 19:16:18 +02:00
parent bbe95085d4
commit e8a0276090
8 changed files with 70 additions and 62 deletions

View file

@ -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);

View file

@ -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++;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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\

View file

@ -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;
}

View file

@ -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