]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/inet_net_pton.c
Fix for inet_net_pton() from Tom.
[postgresql] / src / backend / utils / adt / inet_net_pton.c
1 /*
2  * Copyright (c) 1996 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: inet_net_pton.c,v 1.3 1998/10/12 01:30:26 momjian Exp $";
20
21 #endif
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27
28 #include <assert.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include <postgres.h>
36 #include <utils/builtins.h>
37
38 #ifdef SPRINTF_CHAR
39 #define SPRINTF(x) strlen(sprintf/**/x)
40 #else
41 #define SPRINTF(x) ((size_t)sprintf x)
42 #endif
43
44 static int      inet_net_pton_ipv4(const char *src, u_char *dst, size_t size);
45
46 /*
47  * static int
48  * inet_net_pton(af, src, dst, size)
49  *      convert network number from presentation to network format.
50  *      accepts hex octets, hex strings, decimal octets, and /CIDR.
51  *      "size" is in bytes and describes "dst".
52  * return:
53  *      number of bits, either imputed classfully or specified with /CIDR,
54  *      or -1 if some failure occurred (check errno).  ENOENT means it was
55  *      not a valid network specification.
56  * author:
57  *      Paul Vixie (ISC), June 1996
58  */
59 int
60 inet_net_pton(int af, const char *src, void *dst, size_t size)
61 {
62         switch (af)
63         {
64                 case AF_INET:
65                         return (inet_net_pton_ipv4(src, dst, size));
66                 default:
67                         errno = EAFNOSUPPORT;
68                         return (-1);
69         }
70 }
71
72 /*
73  * static int
74  * inet_net_pton_ipv4(src, dst, size)
75  *      convert IPv4 network number from presentation to network format.
76  *      accepts hex octets, hex strings, decimal octets, and /CIDR.
77  *      "size" is in bytes and describes "dst".
78  * return:
79  *      number of bits, either imputed classfully or specified with /CIDR,
80  *      or -1 if some failure occurred (check errno).  ENOENT means it was
81  *      not an IPv4 network specification.
82  * note:
83  *      network byte order assumed.  this means 192.5.5.240/28 has
84  *      0x11110000 in its fourth octet.
85  * author:
86  *      Paul Vixie (ISC), June 1996
87  */
88 static int
89 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size)
90 {
91         static const char
92                                 xdigits[] = "0123456789abcdef",
93                                 digits[] = "0123456789";
94         int                     n,
95                                 ch,
96                                 tmp,
97                                 dirty,
98                                 bits;
99         const u_char *odst = dst;
100
101         ch = *src++;
102         if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
103                 && isascii(src[1]) && isxdigit(src[1]))
104         {
105                 /* Hexadecimal: Eat nybble string. */
106                 if (size <= 0)
107                         goto emsgsize;
108                 *dst = 0, dirty = 0;
109                 src++;                                  /* skip x or X. */
110                 while ((ch = *src++) != '\0' &&
111                            isascii(ch) && isxdigit(ch))
112                 {
113                         if (isupper(ch))
114                                 ch = tolower(ch);
115                         n = strchr(xdigits, ch) - xdigits;
116                         assert(n >= 0 && n <= 15);
117                         *dst |= n;
118                         if (!dirty++)
119                                 *dst <<= 4;
120                         else if (--size > 0)
121                                 *++dst = 0, dirty = 0;
122                         else
123                                 goto emsgsize;
124                 }
125                 if (dirty)
126                         size--;
127         }
128         else if (isascii(ch) && isdigit(ch))
129         {
130                 /* Decimal: eat dotted digit string. */
131                 for (;;)
132                 {
133                         tmp = 0;
134                         do
135                         {
136                                 n = strchr(digits, ch) - digits;
137                                 assert(n >= 0 && n <= 9);
138                                 tmp *= 10;
139                                 tmp += n;
140                                 if (tmp > 255)
141                                         goto enoent;
142                         } while ((ch = *src++) != '\0' &&
143                                          isascii(ch) && isdigit(ch));
144                         if (size-- <= 0)
145                                 goto emsgsize;
146                         *dst++ = (u_char) tmp;
147                         if (ch == '\0' || ch == '/')
148                                 break;
149                         if (ch != '.')
150                                 goto enoent;
151                         ch = *src++;
152                         if (!isascii(ch) || !isdigit(ch))
153                                 goto enoent;
154                 }
155         }
156         else
157                 goto enoent;
158
159         bits = -1;
160         if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst)
161         {
162                 /* CIDR width specifier.  Nothing can follow it. */
163                 ch = *src++;                    /* Skip over the /. */
164                 bits = 0;
165                 do
166                 {
167                         n = strchr(digits, ch) - digits;
168                         assert(n >= 0 && n <= 9);
169                         bits *= 10;
170                         bits += n;
171                 } while ((ch = *src++) != '\0' &&
172                                  isascii(ch) && isdigit(ch));
173                 if (ch != '\0')
174                         goto enoent;
175                 if (bits > 32)
176                         goto emsgsize;
177         }
178
179         /* Firey death and destruction unless we prefetched EOS. */
180         if (ch != '\0')
181                 goto enoent;
182
183         /* If nothing was written to the destination, we found no address. */
184         if (dst == odst)
185                 goto enoent;
186         /* If no CIDR spec was given, infer width from net class. */
187         if (bits == -1)
188         {
189                 if (*odst >= 240)               /* Class E */
190                         bits = 32;
191                 else if (*odst >= 224)  /* Class D */
192                         bits = 4;
193                 else if (*odst >= 192)  /* Class C */
194                         bits = 24;
195                 else if (*odst >= 128)  /* Class B */
196                         bits = 16;
197                 else
198 /* Class A */
199                         bits = 8;
200                 /* If imputed mask is narrower than specified octets, widen. */
201                 if (bits >= 8 && bits < ((dst - odst) * 8))
202                         bits = (dst - odst) * 8;
203         }
204         /* Extend network to cover the actual mask. */
205         while (bits > ((dst - odst) * 8))
206         {
207                 if (size-- <= 0)
208                         goto emsgsize;
209                 *dst++ = '\0';
210         }
211         return (bits);
212
213 enoent:
214         errno = ENOENT;
215         return (-1);
216
217 emsgsize:
218         errno = EMSGSIZE;
219         return (-1);
220 }