1 /* posix */ 2 #include <sys/types.h> 3 #include <unistd.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 /* bsd extensions */ 8 #include <sys/uio.h> 9 #include <sys/socket.h> 10 #include <netinet/in.h> 11 12 #define CLASS(x) (x[0]>>6) 13 14 unsigned long inet_addr(char * from)15inet_addr(char *from) 16 { 17 int i; 18 char *p; 19 unsigned char to[4]; 20 unsigned long x; 21 22 p = from; 23 memset(to, 0, 4); 24 for(i = 0; i < 4 && *p; i++){ 25 to[i] = strtoul(p, &p, 0); 26 if(*p == '.') 27 p++; 28 } 29 30 switch(CLASS(to)){ 31 case 0: /* class A - 1 byte net */ 32 case 1: 33 if(i == 3){ 34 to[3] = to[2]; 35 to[2] = to[1]; 36 to[1] = 0; 37 } else if (i == 2){ 38 to[3] = to[1]; 39 to[1] = 0; 40 } 41 break; 42 case 2: /* class B - 2 byte net */ 43 if(i == 3){ 44 to[3] = to[2]; 45 to[2] = 0; 46 } 47 break; 48 } 49 x = nptohl(to); 50 x = htonl(x); 51 return x; 52 } 53