PostgreSQL Source Code git master
inet_net_pton.c File Reference
#include "postgres.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include "utils/builtins.h"
#include "utils/inet.h"
Include dependency graph for inet_net_pton.c:

Go to the source code of this file.

Macros

#define NS_IN6ADDRSZ   16
 
#define NS_INT16SZ   2
 
#define NS_INADDRSZ   4
 

Functions

static int inet_net_pton_ipv4 (const char *src, u_char *dst)
 
static int inet_cidr_pton_ipv4 (const char *src, u_char *dst, size_t size)
 
static int inet_net_pton_ipv6 (const char *src, u_char *dst)
 
static int inet_cidr_pton_ipv6 (const char *src, u_char *dst, size_t size)
 
int pg_inet_net_pton (int af, const char *src, void *dst, size_t size)
 
static int getbits (const char *src, int *bitsp)
 
static int getv4 (const char *src, u_char *dst, int *bitsp)
 

Macro Definition Documentation

◆ NS_IN6ADDRSZ

#define NS_IN6ADDRSZ   16

Definition at line 432 of file inet_net_pton.c.

◆ NS_INADDRSZ

#define NS_INADDRSZ   4

Definition at line 434 of file inet_net_pton.c.

◆ NS_INT16SZ

#define NS_INT16SZ   2

Definition at line 433 of file inet_net_pton.c.

Function Documentation

◆ getbits()

static int getbits ( const char *  src,
int *  bitsp 
)
static

Definition at line 347 of file inet_net_pton.c.

348{
349 static const char digits[] = "0123456789";
350 int n;
351 int val;
352 char ch;
353
354 val = 0;
355 n = 0;
356 while ((ch = *src++) != '\0')
357 {
358 const char *pch;
359
360 pch = strchr(digits, ch);
361 if (pch != NULL)
362 {
363 if (n++ != 0 && val == 0) /* no leading zeros */
364 return 0;
365 val *= 10;
366 val += (pch - digits);
367 if (val > 128) /* range */
368 return 0;
369 continue;
370 }
371 return 0;
372 }
373 if (n == 0)
374 return 0;
375 *bitsp = val;
376 return 1;
377}
long val
Definition: informix.c:689
int digits
Definition: informix.c:691

References digits, and val.

Referenced by getv4(), and inet_cidr_pton_ipv6().

◆ getv4()

static int getv4 ( const char *  src,
u_char *  dst,
int *  bitsp 
)
static

Definition at line 380 of file inet_net_pton.c.

381{
382 static const char digits[] = "0123456789";
383 u_char *odst = dst;
384 int n;
385 u_int val;
386 char ch;
387
388 val = 0;
389 n = 0;
390 while ((ch = *src++) != '\0')
391 {
392 const char *pch;
393
394 pch = strchr(digits, ch);
395 if (pch != NULL)
396 {
397 if (n++ != 0 && val == 0) /* no leading zeros */
398 return 0;
399 val *= 10;
400 val += (pch - digits);
401 if (val > 255) /* range */
402 return 0;
403 continue;
404 }
405 if (ch == '.' || ch == '/')
406 {
407 if (dst - odst > 3) /* too many octets? */
408 return 0;
409 *dst++ = val;
410 if (ch == '/')
411 return getbits(src, bitsp);
412 val = 0;
413 n = 0;
414 continue;
415 }
416 return 0;
417 }
418 if (n == 0)
419 return 0;
420 if (dst - odst > 3) /* too many octets? */
421 return 0;
422 *dst++ = val;
423 return 1;
424}
static int getbits(const char *src, int *bitsp)

References digits, getbits(), and val.

Referenced by inet_cidr_pton_ipv6().

◆ inet_cidr_pton_ipv4()

static int inet_cidr_pton_ipv4 ( const char *  src,
u_char *  dst,
size_t  size 
)
static

Definition at line 96 of file inet_net_pton.c.

97{
98 static const char xdigits[] = "0123456789abcdef";
99 static const char digits[] = "0123456789";
100 int n,
101 ch,
102 tmp = 0,
103 dirty,
104 bits;
105 const u_char *odst = dst;
106
107 ch = *src++;
108 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
109 && isxdigit((unsigned char) src[1]))
110 {
111 /* Hexadecimal: Eat nybble string. */
112 if (size <= 0U)
113 goto emsgsize;
114 dirty = 0;
115 src++; /* skip x or X. */
116 while ((ch = *src++) != '\0' && isxdigit((unsigned char) ch))
117 {
118 ch = pg_ascii_tolower((unsigned char) ch);
119 n = strchr(xdigits, ch) - xdigits;
120 assert(n >= 0 && n <= 15);
121 if (dirty == 0)
122 tmp = n;
123 else
124 tmp = (tmp << 4) | n;
125 if (++dirty == 2)
126 {
127 if (size-- <= 0U)
128 goto emsgsize;
129 *dst++ = (u_char) tmp;
130 dirty = 0;
131 }
132 }
133 if (dirty)
134 { /* Odd trailing nybble? */
135 if (size-- <= 0U)
136 goto emsgsize;
137 *dst++ = (u_char) (tmp << 4);
138 }
139 }
140 else if (isdigit((unsigned char) ch))
141 {
142 /* Decimal: eat dotted digit string. */
143 for (;;)
144 {
145 tmp = 0;
146 do
147 {
148 n = strchr(digits, ch) - digits;
149 assert(n >= 0 && n <= 9);
150 tmp *= 10;
151 tmp += n;
152 if (tmp > 255)
153 goto enoent;
154 } while ((ch = *src++) != '\0' &&
155 isdigit((unsigned char) ch));
156 if (size-- <= 0U)
157 goto emsgsize;
158 *dst++ = (u_char) tmp;
159 if (ch == '\0' || ch == '/')
160 break;
161 if (ch != '.')
162 goto enoent;
163 ch = *src++;
164 if (!isdigit((unsigned char) ch))
165 goto enoent;
166 }
167 }
168 else
169 goto enoent;
170
171 bits = -1;
172 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
173 {
174 /* CIDR width specifier. Nothing can follow it. */
175 ch = *src++; /* Skip over the /. */
176 bits = 0;
177 do
178 {
179 n = strchr(digits, ch) - digits;
180 assert(n >= 0 && n <= 9);
181 bits *= 10;
182 bits += n;
183 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
184 if (ch != '\0')
185 goto enoent;
186 if (bits > 32)
187 goto emsgsize;
188 }
189
190 /* Fiery death and destruction unless we prefetched EOS. */
191 if (ch != '\0')
192 goto enoent;
193
194 /* If nothing was written to the destination, we found no address. */
195 if (dst == odst)
196 goto enoent;
197 /* If no CIDR spec was given, infer width from net class. */
198 if (bits == -1)
199 {
200 if (*odst >= 240) /* Class E */
201 bits = 32;
202 else if (*odst >= 224) /* Class D */
203 bits = 8;
204 else if (*odst >= 192) /* Class C */
205 bits = 24;
206 else if (*odst >= 128) /* Class B */
207 bits = 16;
208 else
209 /* Class A */
210 bits = 8;
211 /* If imputed mask is narrower than specified octets, widen. */
212 if (bits < ((dst - odst) * 8))
213 bits = (dst - odst) * 8;
214
215 /*
216 * If there are no additional bits specified for a class D address
217 * adjust bits to 4.
218 */
219 if (bits == 8 && *odst == 224)
220 bits = 4;
221 }
222 /* Extend network to cover the actual mask. */
223 while (bits > ((dst - odst) * 8))
224 {
225 if (size-- <= 0U)
226 goto emsgsize;
227 *dst++ = '\0';
228 }
229 return bits;
230
231enoent:
232 errno = ENOENT;
233 return -1;
234
235emsgsize:
236 errno = EMSGSIZE;
237 return -1;
238}
static unsigned char pg_ascii_tolower(unsigned char ch)
Definition: port.h:188
#define assert(x)
Definition: regcustom.h:56
#define EMSGSIZE
Definition: win32_port.h:366

References assert, digits, EMSGSIZE, and pg_ascii_tolower().

Referenced by pg_inet_net_pton().

◆ inet_cidr_pton_ipv6()

static int inet_cidr_pton_ipv6 ( const char *  src,
u_char *  dst,
size_t  size 
)
static

Definition at line 437 of file inet_net_pton.c.

438{
439 static const char xdigits_l[] = "0123456789abcdef",
440 xdigits_u[] = "0123456789ABCDEF";
441 u_char tmp[NS_IN6ADDRSZ],
442 *tp,
443 *endp,
444 *colonp;
445 const char *xdigits,
446 *curtok;
447 int ch,
448 saw_xdigit;
449 u_int val;
450 int digits;
451 int bits;
452
453 if (size < NS_IN6ADDRSZ)
454 goto emsgsize;
455
456 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
457 endp = tp + NS_IN6ADDRSZ;
458 colonp = NULL;
459 /* Leading :: requires some special handling. */
460 if (*src == ':')
461 if (*++src != ':')
462 goto enoent;
463 curtok = src;
464 saw_xdigit = 0;
465 val = 0;
466 digits = 0;
467 bits = -1;
468 while ((ch = *src++) != '\0')
469 {
470 const char *pch;
471
472 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
473 pch = strchr((xdigits = xdigits_u), ch);
474 if (pch != NULL)
475 {
476 val <<= 4;
477 val |= (pch - xdigits);
478 if (++digits > 4)
479 goto enoent;
480 saw_xdigit = 1;
481 continue;
482 }
483 if (ch == ':')
484 {
485 curtok = src;
486 if (!saw_xdigit)
487 {
488 if (colonp)
489 goto enoent;
490 colonp = tp;
491 continue;
492 }
493 else if (*src == '\0')
494 goto enoent;
495 if (tp + NS_INT16SZ > endp)
496 goto enoent;
497 *tp++ = (u_char) (val >> 8) & 0xff;
498 *tp++ = (u_char) val & 0xff;
499 saw_xdigit = 0;
500 digits = 0;
501 val = 0;
502 continue;
503 }
504 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
505 getv4(curtok, tp, &bits) > 0)
506 {
507 tp += NS_INADDRSZ;
508 saw_xdigit = 0;
509 break; /* '\0' was seen by inet_pton4(). */
510 }
511 if (ch == '/' && getbits(src, &bits) > 0)
512 break;
513 goto enoent;
514 }
515 if (saw_xdigit)
516 {
517 if (tp + NS_INT16SZ > endp)
518 goto enoent;
519 *tp++ = (u_char) (val >> 8) & 0xff;
520 *tp++ = (u_char) val & 0xff;
521 }
522 if (bits == -1)
523 bits = 128;
524
525 endp = tmp + 16;
526
527 if (colonp != NULL)
528 {
529 /*
530 * Since some memmove()'s erroneously fail to handle overlapping
531 * regions, we'll do the shift by hand.
532 */
533 const int n = tp - colonp;
534 int i;
535
536 if (tp == endp)
537 goto enoent;
538 for (i = 1; i <= n; i++)
539 {
540 endp[-i] = colonp[n - i];
541 colonp[n - i] = 0;
542 }
543 tp = endp;
544 }
545 if (tp != endp)
546 goto enoent;
547
548 /*
549 * Copy out the result.
550 */
551 memcpy(dst, tmp, NS_IN6ADDRSZ);
552
553 return bits;
554
555enoent:
556 errno = ENOENT;
557 return -1;
558
559emsgsize:
560 errno = EMSGSIZE;
561 return -1;
562}
#define NS_IN6ADDRSZ
#define NS_INT16SZ
static int getv4(const char *src, u_char *dst, int *bitsp)
#define NS_INADDRSZ
int i
Definition: isn.c:77

References digits, EMSGSIZE, getbits(), getv4(), i, NS_IN6ADDRSZ, NS_INADDRSZ, NS_INT16SZ, and val.

Referenced by inet_net_pton_ipv6(), and pg_inet_net_pton().

◆ inet_net_pton_ipv4()

static int inet_net_pton_ipv4 ( const char *  src,
u_char *  dst 
)
static

Definition at line 258 of file inet_net_pton.c.

259{
260 static const char digits[] = "0123456789";
261 const u_char *odst = dst;
262 int n,
263 ch,
264 tmp,
265 bits;
266 size_t size = 4;
267
268 /* Get the mantissa. */
269 while (ch = *src++, isdigit((unsigned char) ch))
270 {
271 tmp = 0;
272 do
273 {
274 n = strchr(digits, ch) - digits;
275 assert(n >= 0 && n <= 9);
276 tmp *= 10;
277 tmp += n;
278 if (tmp > 255)
279 goto enoent;
280 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
281 if (size-- == 0)
282 goto emsgsize;
283 *dst++ = (u_char) tmp;
284 if (ch == '\0' || ch == '/')
285 break;
286 if (ch != '.')
287 goto enoent;
288 }
289
290 /* Get the prefix length if any. */
291 bits = -1;
292 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
293 {
294 /* CIDR width specifier. Nothing can follow it. */
295 ch = *src++; /* Skip over the /. */
296 bits = 0;
297 do
298 {
299 n = strchr(digits, ch) - digits;
300 assert(n >= 0 && n <= 9);
301 bits *= 10;
302 bits += n;
303 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
304 if (ch != '\0')
305 goto enoent;
306 if (bits > 32)
307 goto emsgsize;
308 }
309
310 /* Fiery death and destruction unless we prefetched EOS. */
311 if (ch != '\0')
312 goto enoent;
313
314 /* Prefix length can default to /32 only if all four octets spec'd. */
315 if (bits == -1)
316 {
317 if (dst - odst == 4)
318 bits = 32;
319 else
320 goto enoent;
321 }
322
323 /* If nothing was written to the destination, we found no address. */
324 if (dst == odst)
325 goto enoent;
326
327 /* If prefix length overspecifies mantissa, life is bad. */
328 if ((bits / 8) > (dst - odst))
329 goto enoent;
330
331 /* Extend address to four octets. */
332 while (size-- > 0)
333 *dst++ = 0;
334
335 return bits;
336
337enoent:
338 errno = ENOENT;
339 return -1;
340
341emsgsize:
342 errno = EMSGSIZE;
343 return -1;
344}

References assert, digits, and EMSGSIZE.

Referenced by pg_inet_net_pton().

◆ inet_net_pton_ipv6()

static int inet_net_pton_ipv6 ( const char *  src,
u_char *  dst 
)
static

Definition at line 427 of file inet_net_pton.c.

428{
429 return inet_cidr_pton_ipv6(src, dst, 16);
430}
static int inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size)

References inet_cidr_pton_ipv6().

Referenced by pg_inet_net_pton().

◆ pg_inet_net_pton()

int pg_inet_net_pton ( int  af,
const char *  src,
void *  dst,
size_t  size 
)

Definition at line 61 of file inet_net_pton.c.

62{
63 switch (af)
64 {
65 case PGSQL_AF_INET:
66 return size == -1 ?
67 inet_net_pton_ipv4(src, dst) :
68 inet_cidr_pton_ipv4(src, dst, size);
69 case PGSQL_AF_INET6:
70 return size == -1 ?
71 inet_net_pton_ipv6(src, dst) :
72 inet_cidr_pton_ipv6(src, dst, size);
73 default:
74 errno = EAFNOSUPPORT;
75 return -1;
76 }
77}
static int inet_net_pton_ipv6(const char *src, u_char *dst)
static int inet_net_pton_ipv4(const char *src, u_char *dst)
static int inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size)
Definition: inet_net_pton.c:96
#define PGSQL_AF_INET
Definition: inet.h:39
#define PGSQL_AF_INET6
Definition: inet.h:40
#define EAFNOSUPPORT
Definition: win32_port.h:368

References EAFNOSUPPORT, inet_cidr_pton_ipv4(), inet_cidr_pton_ipv6(), inet_net_pton_ipv4(), inet_net_pton_ipv6(), PGSQL_AF_INET, and PGSQL_AF_INET6.

Referenced by network_in().