PostgreSQL Source Code git master
Loading...
Searching...
No Matches
inet_aton.c
Go to the documentation of this file.
1/*
2 * src/port/inet_aton.c
3 *
4 * This inet_aton() function was taken from the GNU C library and
5 * incorporated into Postgres for those systems which do not have this
6 * routine in their standard C libraries.
7 *
8 * The function was been extracted whole from the file inet_aton.c in
9 * Release 5.3.12 of the Linux C library, which is derived from the
10 * GNU C library, by Bryan Henderson in October 1996. The copyright
11 * notice from that file is below.
12 */
13
14/*
15 * Copyright (c) 1983, 1990, 1993
16 * The Regents of the University of California. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43#include "c.h"
44
45#include <netinet/in.h>
46#include <ctype.h>
47
48#include "port/pg_bswap.h"
49
50/*
51 * Check whether "cp" is a valid ascii representation
52 * of an Internet address and convert to a binary address.
53 * Returns 1 if the address is valid, 0 if not.
54 * This replaces inet_addr, the return value from which
55 * cannot distinguish between failure and a local broadcast address.
56 */
57int
58inet_aton(const char *cp, struct in_addr *addr)
59{
60 unsigned int val;
61 int base,
62 n;
63 char c;
64 u_int parts[4];
65 u_int *pp = parts;
66
67 for (;;)
68 {
69 /*
70 * Collect number up to ``.''. Values are specified as for C: 0x=hex,
71 * 0=octal, other=decimal.
72 */
73 val = 0;
74 base = 10;
75 if (*cp == '0')
76 {
77 if (*++cp == 'x' || *cp == 'X')
78 base = 16, cp++;
79 else
80 base = 8;
81 }
82 while ((c = *cp) != '\0')
83 {
84 if (isdigit((unsigned char) c))
85 {
86 val = (val * base) + (c - '0');
87 cp++;
88 continue;
89 }
90 if (base == 16 && isxdigit((unsigned char) c))
91 {
92 val = (val << 4) +
93 (c + 10 - (islower((unsigned char) c) ? 'a' : 'A'));
94 cp++;
95 continue;
96 }
97 break;
98 }
99 if (*cp == '.')
100 {
101 /*
102 * Internet format: a.b.c.d a.b.c (with c treated as 16-bits)
103 * a.b (with b treated as 24 bits)
104 */
105 if (pp >= parts + 3 || val > 0xff)
106 return 0;
107 *pp++ = val, cp++;
108 }
109 else
110 break;
111 }
112
113 /*
114 * Check for trailing junk.
115 */
116 while (*cp)
117 if (!isspace((unsigned char) *cp++))
118 return 0;
119
120 /*
121 * Concoct the address according to the number of parts specified.
122 */
123 n = pp - parts + 1;
124 switch (n)
125 {
126
127 case 1: /* a -- 32 bits */
128 break;
129
130 case 2: /* a.b -- 8.24 bits */
131 if (val > 0xffffff)
132 return 0;
133 val |= parts[0] << 24;
134 break;
135
136 case 3: /* a.b.c -- 8.8.16 bits */
137 if (val > 0xffff)
138 return 0;
139 val |= (parts[0] << 24) | (parts[1] << 16);
140 break;
141
142 case 4: /* a.b.c.d -- 8.8.8.8 bits */
143 if (val > 0xff)
144 return 0;
145 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
146 break;
147 }
148 if (addr)
149 addr->s_addr = pg_hton32(val);
150 return 1;
151}
int inet_aton(const char *cp, struct in_addr *addr)
Definition inet_aton.c:58
long val
Definition informix.c:689
#define pg_hton32(x)
Definition pg_bswap.h:121
char * c
static int fb(int x)