summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libcruft/inet_aton.c
blob: ac7d9d00708113a0fee2f18693b0c5f990c7187e (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
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>

/* inet_aton() converts the Internet host address cp from the standard
 * numbers-and-dots  notation  into  binary data  and  stores it in the
 * structure that inp points to. inet_aton returns nonzero if the
 * address is valid, zero if not. */

/* problem is, inet_aton is historically quite, uh, lenient.
 * the following are all acceptable:
 *   0x7f000001 == 127.1 == 127.0.0.1.0 == 127.0.0.1
 * btw: 127.0.0.x.y == 127.0.0.(x|y)
 * and: 10.1.1 == 10.1.0.1 (huh?!)
 * and: 10 == 0.0.0.10 (?!?!?)
 * The Berkeley people must have been so stoned that they are still high.
 */

/* I hereby disclaim that I wrote this code. */
int inet_aton(const char *cp, struct in_addr *inp) {
  int i;
  unsigned int ip=0;
  char *tmp=(char*)cp;
  for (i=24; ;) {
    long j;
    j=strtol(tmp,&tmp,0);
    if (*tmp==0) {
      ip|=j;
      break;
    }
    if (*tmp=='.') {
      if (j>255) return 0;
      ip|=(j<<i);
      if (i>0) i-=8;
      ++tmp;
      continue;
    }
    return 0;
  }
  inp->s_addr=htonl(ip);
  return 1;
}