PostgreSQL Source Code  git master
inet_aton.c File Reference
#include "c.h"
#include <netinet/in.h>
#include <ctype.h>
#include "port/pg_bswap.h"
Include dependency graph for inet_aton.c:

Go to the source code of this file.

Functions

int inet_aton (const char *cp, struct in_addr *addr)
 

Function Documentation

◆ inet_aton()

int inet_aton ( const char *  cp,
struct in_addr *  addr 
)

Definition at line 56 of file inet_aton.c.

References pg_hton32, and val.

Referenced by getaddrinfo().

57 {
58  unsigned int val;
59  int base,
60  n;
61  char c;
62  u_int parts[4];
63  u_int *pp = parts;
64 
65  for (;;)
66  {
67  /*
68  * Collect number up to ``.''. Values are specified as for C: 0x=hex,
69  * 0=octal, other=decimal.
70  */
71  val = 0;
72  base = 10;
73  if (*cp == '0')
74  {
75  if (*++cp == 'x' || *cp == 'X')
76  base = 16, cp++;
77  else
78  base = 8;
79  }
80  while ((c = *cp) != '\0')
81  {
82  if (isdigit((unsigned char) c))
83  {
84  val = (val * base) + (c - '0');
85  cp++;
86  continue;
87  }
88  if (base == 16 && isxdigit((unsigned char) c))
89  {
90  val = (val << 4) +
91  (c + 10 - (islower((unsigned char) c) ? 'a' : 'A'));
92  cp++;
93  continue;
94  }
95  break;
96  }
97  if (*cp == '.')
98  {
99  /*
100  * Internet format: a.b.c.d a.b.c (with c treated as 16-bits)
101  * a.b (with b treated as 24 bits)
102  */
103  if (pp >= parts + 3 || val > 0xff)
104  return 0;
105  *pp++ = val, cp++;
106  }
107  else
108  break;
109  }
110 
111  /*
112  * Check for trailing junk.
113  */
114  while (*cp)
115  if (!isspace((unsigned char) *cp++))
116  return 0;
117 
118  /*
119  * Concoct the address according to the number of parts specified.
120  */
121  n = pp - parts + 1;
122  switch (n)
123  {
124 
125  case 1: /* a -- 32 bits */
126  break;
127 
128  case 2: /* a.b -- 8.24 bits */
129  if (val > 0xffffff)
130  return 0;
131  val |= parts[0] << 24;
132  break;
133 
134  case 3: /* a.b.c -- 8.8.16 bits */
135  if (val > 0xffff)
136  return 0;
137  val |= (parts[0] << 24) | (parts[1] << 16);
138  break;
139 
140  case 4: /* a.b.c.d -- 8.8.8.8 bits */
141  if (val > 0xff)
142  return 0;
143  val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
144  break;
145  }
146  if (addr)
147  addr->s_addr = pg_hton32(val);
148  return 1;
149 }
#define pg_hton32(x)
Definition: pg_bswap.h:121
char * c
long val
Definition: informix.c:684