blob: 8af377862eba2a64ab50303f5f99294d7c24b2c5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}
|