My Project
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
soc_inet_aton.c
Go to the documentation of this file.
1 #include "soc_common.h"
2 #include <arpa/inet.h>
3 #include <ctype.h>
4 #include <stdint.h>
5 
6 int inet_aton(const char *cp, struct in_addr *inp)
7 {
8  int base;
9  uint32_t val;
10  int c;
11  char bytes[4];
12  size_t num_bytes = 0;
13 
14  c = *cp;
15  for(;;) {
16  if(!isdigit(c)) return 0;
17 
18  val = 0;
19  base = 10;
20  if(c == '0') {
21  c = *++cp;
22  if(c == 'x' || c == 'X') {
23  base = 16;
24  c = *++cp;
25  }
26  else base = 8;
27  }
28 
29  for(;;) {
30  if(isdigit(c)) {
31  if(base == 8 && c >= '8') return 0;
32  val *= base;
33  val += c - '0';
34  c = *++cp;
35  }
36  else if(base == 16 && isxdigit(c)) {
37  val *= base;
38  val += c + 10 - (islower(c) ? 'a' : 'A');
39  c = *++cp;
40  }
41  else break;
42  }
43 
44  if(c == '.') {
45  if(num_bytes > 3) return 0;
46  if(val > 0xFF) return 0;
47  bytes[num_bytes++] = val;
48  c = *++cp;
49  }
50  else break;
51  }
52 
53  if(c != 0) return 0;
54 
55  switch(num_bytes) {
56  case 0:
57  return 0;
58 
59  case 1:
60  break;
61 
62  case 2:
63  if(val > 0xFFFFFF) return 0;
64  val |= bytes[0] << 24;
65  break;
66 
67  case 3:
68  if(val > 0xFFFF) return 0;
69  val |= bytes[0] << 24;
70  val |= bytes[1] << 16;
71  break;
72 
73  case 4:
74  if(val > 0xFF) return 0;
75  val |= bytes[0] << 24;
76  val |= bytes[1] << 16;
77  val |= bytes[2] << 8;
78  break;
79  }
80 
81  if(inp)
82  inp->s_addr = htonl(val);
83 
84  return 1;
85 }
Definition: in.h:19
in_addr_t s_addr
Definition: in.h:20
int inet_aton(const char *cp, struct in_addr *inp)
Definition: soc_inet_aton.c:6