#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"
Go to the source code of this file.
|
| 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) |
| |
◆ NS_IN6ADDRSZ
◆ NS_INADDRSZ
◆ NS_INT16SZ
◆ getbits()
Definition at line 347 of file inet_net_pton.c.
348{
349 static const char digits[] =
"0123456789";
350 int n;
353
355 n = 0;
356 while ((
ch = *src++) !=
'\0')
357 {
359
362 {
363 if (n++ != 0 &&
val == 0)
364 return 0;
368 return 0;
369 continue;
370 }
371 return 0;
372 }
373 if (n == 0)
374 return 0;
376 return 1;
377}
References digits, fb(), and val.
Referenced by getv4(), and inet_cidr_pton_ipv6().
◆ getv4()
Definition at line 380 of file inet_net_pton.c.
381{
382 static const char digits[] =
"0123456789";
384 int n;
387
389 n = 0;
390 while ((
ch = *src++) !=
'\0')
391 {
393
396 {
397 if (n++ != 0 &&
val == 0)
398 return 0;
402 return 0;
403 continue;
404 }
405 if (
ch ==
'.' ||
ch ==
'/')
406 {
408 return 0;
413 n = 0;
414 continue;
415 }
416 return 0;
417 }
418 if (n == 0)
419 return 0;
421 return 0;
423 return 1;
424}
static int getbits(const char *src, int *bitsp)
References digits, fb(), getbits(), and val.
Referenced by inet_cidr_pton_ipv6().
◆ inet_cidr_pton_ipv4()
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,
102 tmp = 0,
103 dirty,
104 bits;
106
108 if (
ch ==
'0' && (src[0] ==
'x' || src[0] ==
'X')
109 &&
isxdigit((
unsigned char) src[1]))
110 {
111
114 dirty = 0;
115 src++;
116 while ((
ch = *src++) !=
'\0' &&
isxdigit((
unsigned char)
ch))
117 {
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 {
130 dirty = 0;
131 }
132 }
133 if (dirty)
134 {
138 }
139 }
141 {
142
143 for (;;)
144 {
145 tmp = 0;
146 do
147 {
150 tmp *= 10;
151 tmp += n;
152 if (tmp > 255)
154 }
while ((
ch = *src++) !=
'\0' &&
159 if (
ch ==
'\0' ||
ch ==
'/')
160 break;
166 }
167 }
168 else
170
171 bits = -1;
173 {
174
176 bits = 0;
177 do
178 {
181 bits *= 10;
182 bits += n;
183 }
while ((
ch = *src++) !=
'\0' &&
isdigit((
unsigned char)
ch));
186 if (bits > 32)
188 }
189
190
193
194
197
198 if (bits == -1)
199 {
201 bits = 32;
202 else if (*
odst >= 224)
203 bits = 8;
204 else if (*
odst >= 192)
205 bits = 24;
206 else if (*
odst >= 128)
207 bits = 16;
208 else
209
210 bits = 8;
211
214
215
216
217
218
219 if (bits == 8 && *
odst == 224)
220 bits = 4;
221 }
222
223 while (bits > ((
dst -
odst) * 8))
224 {
228 }
229 return bits;
230
233 return -1;
234
237 return -1;
238}
static unsigned char pg_ascii_tolower(unsigned char ch)
References assert, digits, EMSGSIZE, fb(), and pg_ascii_tolower().
Referenced by pg_inet_net_pton().
◆ inet_cidr_pton_ipv6()
Definition at line 437 of file inet_net_pton.c.
438{
439 static const char xdigits_l[] =
"0123456789abcdef",
442 *tp,
451 int bits;
452
455
459
460 if (*src == ':')
461 if (*++src != ':')
467 bits = -1;
468 while ((
ch = *src++) !=
'\0')
469 {
471
475 {
481 continue;
482 }
484 {
487 {
491 continue;
492 }
493 else if (*src == '\0')
502 continue;
503 }
506 {
509 break;
510 }
511 if (
ch ==
'/' &&
getbits(src, &bits) > 0)
512 break;
514 }
516 {
521 }
522 if (bits == -1)
523 bits = 128;
524
526
528 {
529
530
531
532
533 const int n = tp -
colonp;
535
538 for (
i = 1;
i <= n;
i++)
539 {
542 }
544 }
547
548
549
550
552
553 return bits;
554
557 return -1;
558
561 return -1;
562}
static int getv4(const char *src, u_char *dst, int *bitsp)
References digits, EMSGSIZE, fb(), 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()
Definition at line 258 of file inet_net_pton.c.
259{
260 static const char digits[] =
"0123456789";
262 int n,
264 tmp,
265 bits;
266 size_t size = 4;
267
268
270 {
271 tmp = 0;
272 do
273 {
276 tmp *= 10;
277 tmp += n;
278 if (tmp > 255)
280 }
while ((
ch = *src++) !=
'\0' &&
isdigit((
unsigned char)
ch));
281 if (size-- == 0)
284 if (
ch ==
'\0' ||
ch ==
'/')
285 break;
288 }
289
290
291 bits = -1;
293 {
294
296 bits = 0;
297 do
298 {
301 bits *= 10;
302 bits += n;
303 }
while ((
ch = *src++) !=
'\0' &&
isdigit((
unsigned char)
ch));
306 if (bits > 32)
308 }
309
310
313
314
315 if (bits == -1)
316 {
318 bits = 32;
319 else
321 }
322
323
326
327
330
331
332 while (size-- > 0)
334
335 return bits;
336
339 return -1;
340
343 return -1;
344}
References assert, digits, EMSGSIZE, and fb().
Referenced by pg_inet_net_pton().
◆ inet_net_pton_ipv6()
◆ pg_inet_net_pton()
Definition at line 61 of file inet_net_pton.c.
62{
63 switch (af)
64 {
66 return size == -1 ?
70 return size == -1 ?
73 default:
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)
References EAFNOSUPPORT, fb(), 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().