File Coverage

functions.c
Criterion Covered Total %
statement 1048 1069 98.0
branch 626 664 94.2
condition n/a
subroutine n/a
pod n/a
total 1674 1733 96.6


line stmt bran cond sub pod time code
1             /*
2             functions.c - Core functions for Net::IP::XS.
3              
4             Copyright (C) 2010-2023 Tom Harrison
5             Original inet_pton4, inet_pton6 are Copyright (C) 2006 Free Software
6             Foundation.
7             Original interface, and the auth and ip_auth functions, are Copyright
8             (C) 1999-2002 RIPE NCC.
9              
10             This program is free software; you can redistribute it and/or modify
11             it under the terms of the GNU General Public License as published by
12             the Free Software Foundation; either version 2 of the License, or
13             (at your option) any later version.
14              
15             This program is distributed in the hope that it will be useful,
16             but WITHOUT ANY WARRANTY; without even the implied warranty of
17             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             GNU General Public License for more details.
19              
20             You should have received a copy of the GNU General Public License along
21             with this program; if not, write to the Free Software Foundation, Inc.,
22             51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23             */
24              
25             #include "EXTERN.h"
26             #include "perl.h"
27             #include "XSUB.h"
28              
29             #include "functions.h"
30             #include "inet_pton.h"
31              
32             #ifdef __cplusplus
33             extern "C" {
34             #endif
35              
36             /* Global error string and error number (tied to $Net::IP::XS::ERROR
37             * and $Net::IP::XS::ERRNO). */
38             static char netip_Error[512];
39             static int netip_Errno;
40              
41             /**
42             * NI_hv_get_pv(): get string value from hashref.
43             * @object: reference to a hashref.
44             * @key: hashref key as a string.
45             * @keylen: the length of the hashref key.
46             *
47             * Returns a pointer to the beginning of the string to which @key is
48             * mapped in the hashref. If @key does not exist in the hashref,
49             * returns 0.
50             */
51             const char*
52 2814           NI_hv_get_pv(SV *object, const char *key, int keylen)
53             {
54             SV **ref;
55              
56 2814           ref = hv_fetch((HV*) SvRV(object), key, keylen, 0);
57 2814 100         if (!ref) {
58 87           return NULL;
59             }
60 2727           return SvPV(*ref, PL_na);
61             }
62              
63             /**
64             * NI_hv_get_iv(): get integer value from hashref.
65             * @object: reference to a hashref.
66             * @key: hashref key as a string.
67             * @keylen: the length of the hashref key.
68             *
69             * Returns the integer to which @key is mapped in the hashref. If
70             * @key does not exist in the hashref, returns 0.
71             */
72             int
73 1916           NI_hv_get_iv(SV *object, const char *key, int keylen)
74             {
75             SV **ref;
76              
77 1916           ref = hv_fetch((HV*) SvRV(object), key, keylen, 0);
78 1916 100         if (!ref) {
79 20           return -1;
80             }
81 1896           return SvIV(*ref);
82             }
83              
84             /**
85             * NI_hv_get_uv(): get unsigned integer value from hashref.
86             * @object: reference to a hashref.
87             * @key: hashref key as a string.
88             * @keylen: the length of the hashref key.
89             *
90             * Returns the unsigned integer to which @key is mapped in the
91             * hashref. If @key does not exist in the hashref, returns 0.
92             */
93             unsigned int
94 928           NI_hv_get_uv(SV *object, const char *key, int keylen)
95             {
96             SV **ref;
97              
98 928           ref = hv_fetch((HV*) SvRV(object), key, keylen, 0);
99 928 100         if (!ref) {
100 2           return -1;
101             }
102 926           return SvUV(*ref);
103             }
104              
105             /**
106             * NI_set_Errno() - set the global error number.
107             * @Errno: the new error number.
108             */
109             void
110 3           NI_set_Errno(int Errno)
111             {
112 3           netip_Errno = Errno;
113 3           }
114              
115             /**
116             * NI_get_Errno() - get the global error number.
117             */
118             int
119 76           NI_get_Errno(void)
120             {
121 76           return netip_Errno;
122             }
123              
124             /**
125             * NI_set_Error() - set the global error string.
126             * @Error: the new error string.
127             *
128             * If the error string is more than 512 characters in length
129             * (including the ending null), only the first 511 characters will be
130             * used (with a null as the 512th character).
131             */
132             void
133 3           NI_set_Error(const char *Error)
134             {
135             int len;
136              
137 3           len = strlen(Error);
138 3 100         if (len > 511) {
139 1           len = 511;
140             }
141 3           memcpy(netip_Error, Error, len);
142 3           netip_Error[len] = '\0';
143 3           }
144              
145             /**
146             * NI_get_Error() - get the global error string.
147             */
148             const char*
149 73           NI_get_Error(void)
150             {
151 73           return (const char *) netip_Error;
152             }
153              
154             /**
155             * NI_set_Error_Errno() - set the global error number and string.
156             * @Errno: the new error number.
157             * @Error: the new error string (can include printf modifiers).
158             * @...: format arguments to substitute into @Error.
159             */
160             void
161 442           NI_set_Error_Errno(int Errno, const char *Error, ...)
162             {
163             va_list args;
164              
165 442           va_start(args, Error);
166 442           vsnprintf(netip_Error, 512, Error, args);
167 442           netip_Error[511] = '\0';
168 442           netip_Errno = Errno;
169 442           va_end(args);
170 442           }
171              
172             /**
173             * NI_ip_uchars_to_n128(): make N128 integer from array of chars.
174             * @uchars: array of at least 16 unsigned chars.
175             * @buf: N128 integer buffer.
176             */
177             void
178 52           NI_ip_uchars_to_n128(unsigned char uchars[16], n128_t *num)
179             {
180             int i;
181             int j;
182             unsigned long k;
183              
184 260 100         for (i = 0; i < 4; i++) {
185 208           j = i * 4;
186 208           k = ( (uchars[j + 3])
187 208           | (uchars[j + 2] << 8)
188 208           | (uchars[j + 1] << 16)
189 208           | (uchars[j] << 24));
190 208           num->nums[i] = k;
191             }
192 52           }
193              
194             /**
195             * NI_ip_uchars_to_ulong(): get whole integer from array of chars.
196             * @uchars: array of at least 4 unsigned chars.
197             */
198             unsigned long
199 1043           NI_ip_uchars_to_ulong(unsigned char uchars[4])
200             {
201             return
202 1043           (uchars[3]
203 1043           | (uchars[2] << 8)
204 1043           | (uchars[1] << 16)
205 1043           | (uchars[0] << 24));
206             }
207              
208             /**
209             * NI_hdtoi(): convert hexadecimal character to integer.
210             * @c: hexadecimal character.
211             *
212             * Returns -1 when the character is not a valid hexadecimal character.
213             */
214             int
215 27606           NI_hdtoi(char c)
216             {
217 27606           c = tolower(c);
218              
219             return
220 39770           (isdigit(c)) ? c - '0'
221 58487 100         : ((c >= 'a') && (c <= 'f')) ? 10 + (c - 'a')
    50          
222 30881 100         : -1;
223             }
224              
225             /**
226             * NI_trailing_zeroes(): get trailing zeroes from number treated as binary.
227             * @n: the number.
228             */
229             int
230 5561           NI_trailing_zeroes(unsigned long n)
231             {
232             int c;
233              
234 5561 100         if (!n) {
235 53           return CHAR_BIT * sizeof(n);
236             }
237              
238 5508           n = (n ^ (n - 1)) >> 1;
239 109846 100         for (c = 0; n; c++) {
240 104338           n >>= 1;
241             }
242              
243 5508           return c;
244             }
245              
246             /**
247             * NI_bintoint(): convert bitstring to integer.
248             * @bitstr: the bitstring.
249             * @len: the number of characters to use from the bitstring.
250             */
251             unsigned long
252 66129           NI_bintoint(const char *bitstr, int len)
253             {
254             unsigned long res;
255             int i;
256             int help;
257              
258 66129           res = 0;
259 66129           help = len - 1;
260              
261 2182099 100         for (i = 0; i < len; i++) {
262 2115970           res += (((unsigned long) (bitstr[i] == '1')) << (help - i));
263             }
264              
265 66129           return res;
266             }
267              
268             /**
269             * NI_bintoint_nonzero(): convert bitstring to integer.
270             * @bitstr: the bitstring.
271             * @len: the number of characters to use from the bitstring.
272             *
273             * This function treats all non-zero characters in the bitstring as
274             * if they were '1', whereas NI_bintoint() treats all non-one
275             * characters as if they were '0'.
276             */
277             unsigned long
278 7           NI_bintoint_nonzero(const char *bitstr, int len)
279             {
280             unsigned long res;
281             int i;
282             int help;
283              
284 7           res = 0;
285 7           help = len - 1;
286              
287 88 100         for (i = 0; i < len; i++) {
288 81           res += (((unsigned long) (bitstr[i] != '0')) << (help - i));
289             }
290              
291 7           return res;
292             }
293              
294             /**
295             * NI_iplengths() - return length in bits of the version of IP address.
296             * @version: IP version as integer (either 4 or 6).
297             *
298             * Returns 0 if @version is an invalid value.
299             */
300             int
301 19329           NI_iplengths(int version)
302             {
303 19329           switch (version) {
304 1881           case 4: return 32;
305 17439           case 6: return 128;
306 9           default: return 0;
307             };
308             }
309              
310             /**
311             * NI_ip_n128tobin(): make bitstring from N128 integer.
312             * @num: N128 integer.
313             * @len: the number of bits to write to the buffer.
314             * @buf: the bitstring buffer.
315             *
316             * This does not null-terminate the buffer.
317             */
318             void
319 16008           NI_ip_n128tobin(n128_t *num, int len, char *buf)
320             {
321             int i;
322              
323 16008 100         if (len == 0) {
324 3           return;
325             }
326              
327 2063800 100         for (i = 0; i < len; i++) {
328 2047795 100         buf[len - 1 - i] = (n128_tstbit(num, i) ? '1' : '0');
329             }
330             }
331              
332             /**
333             * NI_ip_inttobin_str() - make bitstring from IP address integer.
334             * @ip_int_str: the IP address integer as a string.
335             * @version: the IP address version.
336             * @buf: the bitstring buffer.
337             *
338             * Returns 1 if the buffer is able to be populated properly. Returns 0
339             * if the IP version is invalid. This function null-terminates the
340             * buffer. The buffer must have at least 129 characters' capacity.
341             */
342             int
343 13           NI_ip_inttobin_str(const char *ip_int_str, int version, char *buf)
344             {
345             n128_t num;
346             int i;
347             int len;
348             int res;
349              
350 13 100         if (!version) {
351 1           NI_set_Error_Errno(101, "Cannot determine IP "
352             "version for %s", ip_int_str);
353 1           return 0;
354             }
355              
356 12           len = strlen(ip_int_str);
357 165 100         for (i = 0; i < len; i++) {
358 155 100         if (!isdigit(ip_int_str[i])) {
359 2 100         memset(buf, '0', (version == 4) ? 32 : 128);
360 2 100         buf[(version == 4) ? 32 : 128] = '\0';
361 2           return 1;
362             }
363             }
364              
365 10           n128_set_ui(&num, 0);
366 10           res = n128_set_str_decimal(&num, ip_int_str, strlen(ip_int_str));
367 10 100         if (!res) {
368 1           return 0;
369             }
370              
371 9           n128_print_bin(&num, buf, (version == 4));
372              
373 9           return 1;
374             }
375              
376             /**
377             * NI_ip_bintoint_str(): convert bitstring to integer string.
378             * @bitstr: the bitstring.
379             * @buf: the integer string buffer.
380             */
381             int
382 10           NI_ip_bintoint_str(const char *bitstr, char *buf)
383             {
384             unsigned long num_ulong;
385             n128_t num_n128;
386             int len;
387              
388 10           len = strlen(bitstr);
389              
390 10 100         if (len <= 32) {
391 7           num_ulong = NI_bintoint_nonzero(bitstr, len);
392 7           sprintf(buf, "%lu", num_ulong);
393 7           return 1;
394             }
395              
396 3           n128_set_ui(&num_n128, 0);
397              
398 3           n128_set_str_binary(&num_n128, bitstr, len);
399 3           n128_print_dec(&num_n128, buf);
400              
401 3           return 1;
402             }
403              
404             /**
405             * NI_ip_is_ipv4(): check whether string is an IPv4 address.
406             * @str: IP address string (null-terminated).
407             */
408             int
409 949           NI_ip_is_ipv4(const char *str)
410             {
411             int i;
412             int len;
413 949           int quads = 0;
414             int quadspots[3];
415             long current_quad;
416             int cq_index;
417             char *endptr;
418              
419 949           len = strlen(str);
420 949 100         if (!len) {
421 2           NI_set_Error_Errno(107, "Invalid chars in IP ");
422 2           return 0;
423             }
424              
425             /* Contains invalid characters. */
426              
427 11120 100         for (i = 0; i < len; i++) {
428 10530 100         if (!isdigit(str[i]) && str[i] != '.') {
    100          
429 357           NI_set_Error_Errno(107, "Invalid chars in IP %s", str);
430 357           return 0;
431             }
432             }
433              
434             /* Starts or ends with '.'. */
435              
436 590 100         if (str[0] == '.') {
437 1           NI_set_Error_Errno(103, "Invalid IP %s - starts with a dot", str);
438 1           return 0;
439             }
440              
441 589 100         if (str[len - 1] == '.') {
442 1           NI_set_Error_Errno(104, "Invalid IP %s - ends with a dot", str);
443 1           return 0;
444             }
445              
446             /* Contains more than four quads (octets). */
447            
448 10740 100         for (i = 0; i < len; i++) {
449 10153 100         if (str[i] == '.') {
450 1656 100         if (quads == 3) {
451 1           NI_set_Error_Errno(105, "Invalid IP address %s", str);
452 1           return 0;
453             }
454 1655           quadspots[quads] = i + 1;
455 1655           quads++;
456             }
457             }
458              
459             /* Contains an empty quad. */
460              
461 10140 100         for (i = 0; i < (len - 1); i++) {
462 9554 100         if ((str[i] == '.') && (str[i + 1] == '.')) {
    100          
463 1           NI_set_Error_Errno(106, "Empty quad in IP address %s", str);
464 1           return 0;
465             }
466             }
467              
468             /* Contains an invalid quad value. */
469              
470 2812 100         for (cq_index = 0; cq_index <= quads; cq_index++) {
471 2235 100         i = (cq_index > 0) ? (quadspots[cq_index - 1]) : 0;
472              
473 2235           endptr = NULL;
474              
475 2235           current_quad = strtol(str + i, &endptr, 10);
476 2235 100         if (STRTOL_FAILED(current_quad, str + i, endptr)
    50          
    50          
    100          
    50          
477 2232 50         || (!(current_quad >= 0 && current_quad < 256))) {
    100          
478 9           NI_set_Error_Errno(107, "Invalid quad in IP address "
479             "%s - %d", str, current_quad);
480 9           return 0;
481             }
482             }
483              
484 577           return 1;
485             }
486              
487             /**
488             * NI_ip_is_ipv6(): check whether string is an IPv6 address.
489             * @str: the IP address string.
490             */
491             int
492 447           NI_ip_is_ipv6(const char *str)
493             {
494             int i;
495             int len;
496 447           int octs = 0;
497             int octspots[8];
498             int oct_index;
499             const char *double_colon;
500             const char *next_oct;
501             const char *cc;
502             int count;
503             int is_hd;
504             int max_colons;
505              
506 447           len = strlen(str);
507              
508 447           double_colon = strstr(str, "::");
509 447 100         max_colons = (double_colon == NULL) ? 7 : 8;
510              
511             /* Store a pointer to the next character after each ':' in
512             * octspots. */
513              
514 17099 100         for (i = 0; i < len; i++) {
515 16654 100         if (str[i] == ':') {
516 2646 100         if (octs == max_colons) {
517 2           return 0;
518             }
519 2644           octspots[octs++] = i + 1;
520             }
521             }
522              
523 445 100         if (!octs) {
524 12           return 0;
525             }
526              
527 3490 100         for (oct_index = 0; oct_index <= octs; oct_index++) {
528 3060 100         i = (oct_index > 0) ? (octspots[oct_index - 1]) : 0;
529              
530             /* Empty octet. */
531              
532 3060 100         if (str[i] == ':') {
533 119           continue;
534             }
535 2941 100         if (strlen(str + i) == 0) {
536 68           continue;
537             }
538              
539             /* Last octet can be an IPv4 address. */
540              
541 2873           cc = str + i;
542 2873 100         if ((oct_index == octs) && NI_ip_is_ipv4(cc)) {
    100          
543 16           continue;
544             }
545              
546             /* 1-4 hex digits. */
547              
548 2857           next_oct = strchr(str + i, ':');
549 2857 100         if (next_oct == NULL) {
550 347           next_oct = (str + len);
551             }
552              
553 2857           count = next_oct - cc;
554 2857           is_hd = 1;
555              
556 14193 100         while (cc != next_oct) {
557 11338 100         if (!isxdigit(*cc)) {
558 2           is_hd = 0;
559 2           break;
560             }
561 11336           cc++;
562             }
563              
564 2857 100         if (is_hd && (count <= 4)) {
    100          
565 2854           continue;
566             }
567              
568 3           NI_set_Error_Errno(108, "Invalid IP address %s", str);
569 3           return 0;
570             }
571              
572             /* Starts or ends with ':'. */
573              
574 430 100         if ((str[0] == ':') && (str[1] != ':')) {
    100          
575 1           NI_set_Error_Errno(109, "Invalid address %s "
576             "(starts with :)", str);
577 1           return 0;
578             }
579              
580 429 100         if ((str[len - 1] == ':') && (str[len - 2] != ':')) {
    100          
581 1           NI_set_Error_Errno(110, "Invalid address %s "
582             "(ends with :)", str);
583 1           return 0;
584             }
585              
586             /* Contains more than one '::'. */
587              
588 428 100         if ((double_colon != NULL) && (strstr(double_colon + 1, "::"))) {
    100          
589 1           NI_set_Error_Errno(111, "Invalid address %s "
590             "(More than one :: pattern)", str);
591 1           return 0;
592             }
593              
594             /* Doesn't contain '::', though it has fewer than eight segments. */
595              
596 427 100         if ((octs != 7) && (double_colon == NULL)) {
    100          
597 1           NI_set_Error_Errno(112, "Invalid number of octets %s", str);
598 1           return 0;
599             }
600              
601 426           return 1;
602             }
603              
604             /**
605             * NI_ip_get_version(): return the version of the IP address string.
606             * @str: the IP address string.
607             *
608             * Returns 0 if the string is neither an IPv4 nor an IPv6 address
609             * string.
610             */
611             int
612 983           NI_ip_get_version(const char *str)
613             {
614 983 100         if ((!strchr(str, ':')) && NI_ip_is_ipv4(str)) {
    100          
615 549           return 4;
616 434 100         } else if (NI_ip_is_ipv6(str)) {
617 421           return 6;
618             } else {
619 13           return 0;
620             }
621             }
622              
623             /**
624             * NI_ip_get_mask(): make bitstring network mask.
625             * @len: the mask's prefix length.
626             * @version: the mask's IP address version.
627             * @buf: the bitstring mask buffer.
628             *
629             * If @len is larger than the number of bits for an IP address of the
630             * specified version, then @len is set to equal that number of bits.
631             * So if 48 were specified for @len for an IPv4 address, it would be
632             * set to 32. This function does not null-terminate the buffer. The
633             * buffer must have 32 or 128 characters' capacity for IPv4 and IPv6
634             * respectively.
635             */
636             int
637 144           NI_ip_get_mask(int len, int version, char *buf)
638             {
639             int size;
640              
641 144 100         if (!version) {
642 1           NI_set_Error_Errno(101, "Cannot determine IP version");
643 1           return 0;
644             }
645              
646 143           size = NI_iplengths(version);
647              
648 143 100         if (len < 0) {
649 1           len = 0;
650 142 100         } else if (len > size) {
651 2           len = size;
652             }
653              
654 143           memset(buf, '1', len);
655 143           memset(buf + len, '0', (size - len));
656              
657 143           return 1;
658             }
659              
660             /**
661             * NI_ip_last_address_ipv6(): get last address of prefix.
662             * @ip: the beginning IP address.
663             * @len: the prefix length.
664             * @buf: N128 integer buffer for the last address.
665             */
666             int
667 47           NI_ip_last_address_ipv6(n128_t *ip, int len, n128_t *buf)
668             {
669             int i;
670              
671 47           memcpy(buf, ip, sizeof(*ip));
672              
673 47 100         len = (len == 0) ? 128 : (128 - len);
674              
675 4529 100         for (i = 0; i < len; i++) {
676 4482           n128_setbit(buf, i);
677             }
678              
679 47           return 1;
680             }
681              
682             /**
683             * NI_ip_last_address_ipv4(): get last address of prefix.
684             * @ip: the beginning IP address.
685             * @len: the prefix length.
686             */
687             unsigned long
688 61           NI_ip_last_address_ipv4(unsigned long ip, int len)
689             {
690             unsigned long mask;
691              
692 61 100         mask = (len == 0) ? 0xFFFFFFFF : ((1 << (32 - len)) - 1);
693 61           return ip | mask;
694             }
695              
696             /**
697             * NI_ip_last_address_bin(): make last address of prefix as a bitstring.
698             * @bitstr: the beginning IP address as a bitstring.
699             * @len: the prefix length.
700             * @version: the IP address version.
701             * @buf: the last address bitstring buffer.
702             *
703             * This function does not null-terminate the buffer. The buffer must
704             * have 32 or 128 characters' capacity for IPv4 and IPv6 respectively.
705             */
706             int
707 28           NI_ip_last_address_bin(const char *bitstr, int len, int version, char *buf)
708             {
709             int size;
710              
711 28 100         if (!version) {
712 1           NI_set_Error_Errno(101, "Cannot determine IP version");
713 1           return 0;
714             }
715              
716 27           size = NI_iplengths(version);
717              
718 27 100         if ((len < 0) || (len > size)) {
    100          
719 3           len = size;
720             }
721              
722 27           strncpy(buf, bitstr, len);
723 27           memset(buf + len, '1', (size - len));
724              
725 27           return 1;
726             }
727              
728             /**
729             * NI_ip_bincomp(): compare two bitstrings.
730             * @bitstr_1: first bitstring.
731             * @op_str: the comparator as a string.
732             * @bitstr_2: second bitstring.
733             * @result: a pointer to an integer.
734             *
735             * The bitstrings and the comparator must be null-terminated. The
736             * comparator must be one of 'gt', 'ge', 'lt', and 'le'. 'gt' means
737             * 'greater than', 'ge' means 'greater than or equal to', 'lt' means
738             * 'less than', 'le' means 'less than or equal to'. Returns 1 or 0
739             * depending on whether the strings were able to be compared
740             * successfully. If the comparison was able to be made, the result of
741             * the comparison is stored in @result. This function will not compare
742             * two bitstrings of different lengths.
743             */
744             int
745 954           NI_ip_bincomp(const char *bitstr_1, const char *op_str,
746             const char *bitstr_2, int *result)
747             {
748             const char *b;
749             const char *e;
750             int op;
751             int res;
752              
753 1908           op = (!strcmp(op_str, "gt")) ? GT
754 1903 100         : (!strcmp(op_str, "lt")) ? LT
755 1888 100         : (!strcmp(op_str, "le")) ? LE
756 948 100         : (!strcmp(op_str, "ge")) ? GE
757 9 100         : 0;
758              
759 954 100         if (!op) {
760 2           NI_set_Error_Errno(131, "Invalid Operator %s", op_str);
761 2           return 0;
762             }
763              
764 952 100         if ((op == GT) || (op == GE)) {
    100          
765 12           b = bitstr_1;
766 12           e = bitstr_2;
767             } else {
768 940           b = bitstr_2;
769 940           e = bitstr_1;
770             }
771              
772 952 100         if (strlen(b) != (strlen(e))) {
773 2           NI_set_Error_Errno(130, "IP addresses of different length");
774 2           return 0;
775             }
776              
777 950           res = strcmp(b, e);
778              
779 950           *result =
780 20 100         (!res && ((op == GE) || (op == LE)))
    100          
781             ? 1
782 970 100         : (res > 0);
    100          
783              
784 950           return 1;
785             }
786              
787             /**
788             * NI_ip_is_overlap_ipv6(): get overlap status of two ranges.
789             * @begin_1: beginning address of first range.
790             * @end_1: ending address of first range.
791             * @begin_2: beginning address of second range.
792             * @end_2: ending address of second range.
793             * @result: a pointer to an integer.
794             */
795             void
796 15           NI_ip_is_overlap_ipv6(n128_t *begin_1, n128_t *end_1,
797             n128_t *begin_2, n128_t *end_2, int *result)
798             {
799             int res;
800              
801 15 100         if (!n128_cmp(begin_1, begin_2)) {
802 5 100         if (!n128_cmp(end_1, end_2)) {
803 2           *result = IP_IDENTICAL;
804 2           return;
805             }
806 3           res = n128_cmp(end_1, end_2);
807 3           *result = (res < 0) ? IP_A_IN_B_OVERLAP
808 3 100         : IP_B_IN_A_OVERLAP;
809 3           return;
810             }
811              
812 10 100         if (!n128_cmp(end_1, end_2)) {
813 2           res = n128_cmp(begin_1, begin_2);
814 2           *result = (res < 0) ? IP_B_IN_A_OVERLAP
815 2 100         : IP_A_IN_B_OVERLAP;
816 2           return;
817             }
818              
819 8           res = n128_cmp(begin_1, begin_2);
820 8 100         if (res < 0) {
821 5           res = n128_cmp(end_1, begin_2);
822 5 100         if (res < 0) {
823 2           *result = IP_NO_OVERLAP;
824 2           return;
825             }
826 3           res = n128_cmp(end_1, end_2);
827 3           *result = (res < 0) ? IP_PARTIAL_OVERLAP
828 3 100         : IP_B_IN_A_OVERLAP;
829 3           return;
830             }
831              
832 3           res = n128_cmp(end_2, begin_1);
833 3 100         if (res < 0) {
834 1           *result = IP_NO_OVERLAP;
835 1           return;
836             }
837              
838 2           res = n128_cmp(end_2, end_1);
839 2           *result = (res < 0) ? IP_PARTIAL_OVERLAP
840 2 100         : IP_A_IN_B_OVERLAP;
841              
842 2           return;
843             }
844              
845             /**
846             * NI_ip_is_overlap_ipv4(): get overlap status of two ranges.
847             * @begin_1: beginning address of first range.
848             * @end_1: ending address of first range.
849             * @begin_2: beginning address of second range.
850             * @end_2: ending address of second range.
851             * @result: a pointer to an integer.
852             */
853             void
854 13           NI_ip_is_overlap_ipv4(unsigned long begin_1, unsigned long end_1,
855             unsigned long begin_2, unsigned long end_2,
856             int *result)
857             {
858 13 100         if (begin_1 == begin_2) {
859 4 100         if (end_1 == end_2) {
860 2           *result = IP_IDENTICAL;
861 2           return;
862             }
863 2           *result =
864             (end_1 < end_2)
865             ? IP_A_IN_B_OVERLAP
866 2 100         : IP_B_IN_A_OVERLAP;
867 2           return;
868             }
869              
870 9 100         if (end_1 == end_2) {
871 2           *result =
872             (begin_1 < begin_2)
873             ? IP_B_IN_A_OVERLAP
874 2 100         : IP_A_IN_B_OVERLAP;
875 2           return;
876             }
877              
878 7 100         if (begin_1 < begin_2) {
879 4 100         if (end_1 < begin_2) {
880 2           *result = IP_NO_OVERLAP;
881 2           return;
882             }
883 2           *result =
884             (end_1 < end_2)
885             ? IP_PARTIAL_OVERLAP
886 2 100         : IP_B_IN_A_OVERLAP;
887 2           return;
888             }
889              
890 3 100         if (end_2 < begin_1) {
891 1           *result = IP_NO_OVERLAP;
892 1           return;
893             }
894              
895 2           *result =
896             (end_2 < end_1)
897             ? IP_PARTIAL_OVERLAP
898 2 100         : IP_A_IN_B_OVERLAP;
899              
900 2           return;
901             }
902              
903             /**
904             * NI_ip_is_overlap(): get overlap status of two ranges.
905             * @begin_1: beginning bitstring IP address for first range.
906             * @end_1: ending bitstring IP address for first range.
907             * @begin_2: beginning bitstring IP address for second range.
908             * @end_2: ending bitstring IP address for second range.
909             * @result: a pointer to an integer.
910             *
911             * Each bitstring must be null-terminated. Returns 1 or 0 depending
912             * on whether the ranges are able to be compared successfully. If the
913             * overlap status is able to be determined, stores that status in
914             * @result. Returns 0 if the bitstrings are not all of equal length.
915             * The possible overlap statuses are NO_OVERLAP, IP_PARTIAL_OVERLAP,
916             * IP_A_IN_B_OVERLAP (first range completely contained within second
917             * range), IP_B_IN_A_OVERLAP (second range completely contained within
918             * first range) and IP_IDENTICAL.
919             */
920             int
921 23           NI_ip_is_overlap(const char *begin_1, const char *end_1,
922             const char *begin_2, const char *end_2, int *result)
923             {
924             int b1_len;
925             int b2_len;
926 23           int res = 0;
927             n128_t begin_1_n128;
928             n128_t end_1_n128;
929             n128_t begin_2_n128;
930             n128_t end_2_n128;
931             unsigned long begin_1_ulong;
932             unsigned long begin_2_ulong;
933             unsigned long end_1_ulong;
934             unsigned long end_2_ulong;
935              
936 23           b1_len = strlen(begin_1);
937 23           b2_len = strlen(begin_2);
938              
939 44 100         if (!( (b1_len == (int) strlen(end_1))
    100          
940 22 100         && (b2_len == (int) strlen(end_2))
941             && (b1_len == b2_len))) {
942 3           NI_set_Error_Errno(130, "IP addresses of different length");
943 3           return 0;
944             }
945              
946 20           NI_ip_bincomp(begin_1, "le", end_1, &res);
947 20 100         if (!res) {
948 1           NI_set_Error_Errno(140, "Invalid range %s - %s", begin_1, end_1);
949 1           return 0;
950             }
951              
952 19           NI_ip_bincomp(begin_2, "le", end_2, &res);
953 19 100         if (!res) {
954 1           NI_set_Error_Errno(140, "Invalid range %s - %s", begin_2, end_2);
955 1           return 0;
956             }
957              
958             /* IPv4-specific version (avoids using N128). */
959              
960 18 100         if (b1_len <= 32) {
961 8           begin_1_ulong = NI_bintoint(begin_1, b1_len);
962 8           begin_2_ulong = NI_bintoint(begin_2, b1_len);
963 8           end_1_ulong = NI_bintoint(end_1, b1_len);
964 8           end_2_ulong = NI_bintoint(end_2, b1_len);
965 8           NI_ip_is_overlap_ipv4(begin_1_ulong, end_1_ulong,
966             begin_2_ulong, end_2_ulong, result);
967 8           return 1;
968             }
969              
970             /* IPv6 version (using N128). */
971              
972 10           n128_set_str_binary(&begin_1_n128, begin_1, b1_len);
973 10           n128_set_str_binary(&begin_2_n128, begin_2, b1_len);
974 10           n128_set_str_binary(&end_1_n128, end_1, b1_len);
975 10           n128_set_str_binary(&end_2_n128, end_2, b1_len);
976              
977 10           NI_ip_is_overlap_ipv6(&begin_1_n128, &end_1_n128,
978             &begin_2_n128, &end_2_n128, result);
979              
980 10           return 1;
981             }
982              
983             /**
984             * NI_ip_check_prefix_ipv6(): check whether prefix length is valid.
985             * @ip: IP address.
986             * @len: the prefix length.
987             */
988             int
989 50           NI_ip_check_prefix_ipv6(n128_t *ip, int len)
990             {
991             n128_t mask;
992             char buf[IPV6_BITSTR_LEN];
993             int i;
994              
995 50 100         if ((len < 0) || (len > 128)) {
    100          
996 2           NI_set_Error_Errno(172, "Invalid prefix length /%d", len);
997 2           return 0;
998             }
999              
1000 48           n128_set_ui(&mask, 0);
1001 4657 100         for (i = 0; i < (128 - len); i++) {
1002 4609           n128_setbit(&mask, i);
1003             }
1004 48           n128_and(&mask, ip);
1005              
1006 48 100         if (n128_cmp_ui(&mask, 0)) {
1007 1           NI_ip_n128tobin(ip, len, buf);
1008 1           buf[len] = '\0';
1009 1           NI_set_Error_Errno(171, "Invalid prefix %s/%d", buf, len);
1010 1           return 0;
1011             }
1012              
1013 47           return 1;
1014             }
1015              
1016             /**
1017             * NI_ip_check_prefix_ipv4(): check whether prefix length is valid.
1018             * @ip: IP address.
1019             * @len: the prefix length.
1020             */
1021             int
1022 64           NI_ip_check_prefix_ipv4(unsigned long ip, int len)
1023             {
1024             unsigned long mask;
1025              
1026 64 100         if ((len < 0) || (len > 32)) {
    100          
1027 2           NI_set_Error_Errno(172, "Invalid prefix length /%d", len);
1028 2           return 0;
1029             }
1030              
1031 62 100         mask = (len == 0) ? 0xFFFFFFFF : ((1 << (32 - len)) - 1);
1032              
1033 62 100         if ((ip & mask) != 0) {
1034 1           NI_set_Error_Errno(171, "Invalid prefix %u/%d", ip, len);
1035 1           return 0;
1036             }
1037              
1038 61           return 1;
1039             }
1040              
1041             /**
1042             * NI_ip_check_prefix(): check whether prefix length is valid for address.
1043             * @bitstr: the bitstring IP address.
1044             * @len: the prefix length.
1045             * @version: the IP address version.
1046             *
1047             * The bitstring must be null-terminated.
1048             */
1049             int
1050 160           NI_ip_check_prefix(const char *bitstr, int len, int version)
1051             {
1052             int iplen;
1053             const char *c;
1054              
1055 160 100         if (len < 0) {
1056 1           NI_set_Error_Errno(172, "Invalid prefix length /%d", len);
1057 1           return 0;
1058             }
1059              
1060 159           iplen = strlen(bitstr);
1061              
1062 159 100         if (len > iplen) {
1063 1           NI_set_Error_Errno(170, "Prefix length %d is longer than "
1064             "IP address (%d)", len, iplen);
1065 1           return 0;
1066             }
1067              
1068 158           c = bitstr + len;
1069              
1070 6218 100         while (*c != '\0') {
1071 6062 100         if (*c != '0') {
1072 2           NI_set_Error_Errno(171, "Invalid prefix %s/%d", bitstr, len);
1073 2           return 0;
1074             }
1075 6060           c++;
1076             }
1077              
1078 156 100         if (iplen != NI_iplengths(version)) {
1079 1           NI_set_Error_Errno(172, "Invalid prefix length /%d", len);
1080 1           return 0;
1081             }
1082              
1083 155           return 1;
1084             }
1085              
1086             /**
1087             * NI_ip_get_prefix_length_ipv4(): get prefix length for a given range.
1088             * @begin: first IP address.
1089             * @end: second IP address.
1090             * @bits: number of bits to check.
1091             * @len: a pointer to an integer.
1092             */
1093             void
1094 5561           NI_ip_get_prefix_length_ipv4(unsigned long begin, unsigned long end,
1095             int bits, int *len)
1096             {
1097             int i;
1098 5561           int res = 0;
1099              
1100 110742 100         for (i = 0; i < bits; i++) {
1101 110732 100         if ((begin & 1) == (end & 1)) {
1102 5551           res = bits - i;
1103 5551           break;
1104             }
1105 105181           begin >>= 1;
1106 105181           end >>= 1;
1107             }
1108              
1109 5561           *len = res;
1110 5561           }
1111              
1112             /**
1113             * NI_ip_get_prefix_length_ipv6(): get prefix length for a given range.
1114             * @num1: first IP address as an N128 integer.
1115             * @num2: second IP address as an N128 integer.
1116             * @bits: number of bits to check
1117             * @len: a pointer to an integer.
1118             *
1119             * Returns 1 or 0 depending on whether the prefix length could be
1120             * calculated. Stores the prefix length in @len if it is able to be
1121             * calculated.
1122             */
1123             void
1124 15995           NI_ip_get_prefix_length_ipv6(n128_t *num1, n128_t *num2, int bits, int *len)
1125             {
1126             int i;
1127 15995           int res = 0;
1128              
1129 1316809 100         for (i = 0; i < bits; i++) {
1130 1316777 100         if (n128_tstbit(num1, i) == n128_tstbit(num2, i)) {
1131 15963           res = bits - i;
1132 15963           break;
1133             }
1134             }
1135              
1136 15995           *len = res;
1137 15995           }
1138              
1139             /**
1140             * NI_ip_get_prefix_length(): get prefix length for a given range.
1141             * @bitstr_1: first IP address as a bitstring.
1142             * @bitstr_2: second IP address as a bitstring.
1143             * @len: a pointer to an integer.
1144             *
1145             * Returns 1 or 0 depending on whether the prefix length could be
1146             * calculated. Stores the prefix length in @len if it is able to be
1147             * calculated.
1148             */
1149             int
1150 15           NI_ip_get_prefix_length(const char *bitstr_1, const char *bitstr_2, int *len)
1151             {
1152             int bin1_len;
1153             int bin2_len;
1154             int i;
1155             int res;
1156              
1157 15           bin1_len = strlen(bitstr_1);
1158 15           bin2_len = strlen(bitstr_2);
1159              
1160 15 100         if (bin1_len != bin2_len) {
1161 1           NI_set_Error_Errno(130, "IP addresses of different length");
1162 1           return 0;
1163             }
1164              
1165 14           res = bin1_len;
1166              
1167 245 100         for (i = (bin1_len - 1); i >= 0; i--) {
1168 243 100         if (bitstr_1[i] == bitstr_2[i]) {
1169 12           res = (bin1_len - 1 - i);
1170 12           break;
1171             }
1172             }
1173              
1174 14           *len = res;
1175 14           return 1;
1176             }
1177              
1178             /**
1179             * NI_ip_inttoip_ipv4(): make IPv4 address from integer.
1180             * @n: the IP address as a number.
1181             * @buf: the IP address buffer.
1182             */
1183             void
1184 7544           NI_ip_inttoip_ipv4(unsigned long n, char *buf)
1185             {
1186 7544           sprintf(buf, "%lu.%lu.%lu.%lu", (n >> 24) & 0xFF,
1187 7544           (n >> 16) & 0xFF,
1188 7544           (n >> 8) & 0xFF,
1189             (n >> 0) & 0xFF);
1190 7544           }
1191              
1192             /**
1193             * NI_ip_inttoip_ipv6(): make IPv6 address from integers.
1194             * @n1: the most significant 32 bits of the address.
1195             * @n2: the next-most significant 32 bits of the address.
1196             * @n3: the next-most significant 32 bits of the address.
1197             * @n4: the least significant 32 bits of the address.
1198             * @buf: the IP address buffer.
1199             */
1200             void
1201 17525           NI_ip_inttoip_ipv6(unsigned long n1, unsigned long n2,
1202             unsigned long n3, unsigned long n4, char *buf)
1203             {
1204 17525           sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
1205 17525           (unsigned int) (n1 >> 16) & 0xFFFF,
1206 17525           (unsigned int) (n1 ) & 0xFFFF,
1207 17525           (unsigned int) (n2 >> 16) & 0xFFFF,
1208 17525           (unsigned int) (n2 ) & 0xFFFF,
1209 17525           (unsigned int) (n3 >> 16) & 0xFFFF,
1210 17525           (unsigned int) (n3 ) & 0xFFFF,
1211 17525           (unsigned int) (n4 >> 16) & 0xFFFF,
1212 17525           (unsigned int) (n4 ) & 0xFFFF);
1213 17525           }
1214              
1215             /**
1216             * NI_ip_inttoip_n128(): make IPv6 address from N128 integer.
1217             * @ip: IP address.
1218             * @buf: the IP address buffer.
1219             */
1220             void
1221 774           NI_ip_inttoip_n128(n128_t *ip, char *buf)
1222             {
1223 774           NI_ip_inttoip_ipv6(ip->nums[0], ip->nums[1],
1224 774           ip->nums[2], ip->nums[3], buf);
1225 774           }
1226              
1227             /**
1228             * NI_ip_bintoip(): make IP address from bitstring.
1229             * @bitstr: the IP address as a bitstring.
1230             * @version: IP address version as integer.
1231             * @buf: the IP address buffer.
1232             *
1233             * The bitstring must be null-terminated. This function null-terminates the
1234             * buffer, so it has to have between eight and sixteen characters' capacity
1235             * (inclusive) for IPv4 addresses, depending on the value of the address, and
1236             * 40 characters' capacity for IPv6 addresses. Bitstrings that have fewer
1237             * characters than there are bits in the relevant version of IP address will be
1238             * treated as if they were left-padded with '0' characters until that number of
1239             * bits is met: e.g., passing "1" and 4 as the first two arguments to this
1240             * function will yield "0.0.0.1" in @buf.
1241             */
1242             int
1243 16037           NI_ip_bintoip(const char *bitstr, int version, char *buf)
1244             {
1245             int size;
1246             int iplen;
1247             int longs;
1248             int i;
1249             int j;
1250             int excess;
1251             int bits;
1252             unsigned long nums[4];
1253              
1254 16037           iplen = NI_iplengths(version);
1255 16037           size = strlen(bitstr);
1256 16037 100         if (size > iplen) {
1257 4           NI_set_Error_Errno(189, "Invalid IP length for "
1258             "binary IP %s", bitstr);
1259 4           return 0;
1260             }
1261              
1262 16033 100         if (version == 4) {
1263 24           nums[0] = NI_bintoint(bitstr, size);
1264 24           NI_ip_inttoip_ipv4(nums[0], buf);
1265 24           return 1;
1266             }
1267              
1268 80045 100         for (i = 0; i < 4; i++) {
1269 64036           nums[i] = 0;
1270             }
1271              
1272 16009           excess = size % 32;
1273 16009           longs = (size / 32) + (!excess ? 0 : 1);
1274              
1275 80020 100         for (i = (4 - longs), j = 0; i < 4; i++, j++) {
1276 64011           bits =
1277 16005 100         (i == (4 - longs) && excess)
1278             ? excess
1279 80016 100         : 32;
1280 64011           nums[i] = NI_bintoint(bitstr + (j * 32), bits);
1281             }
1282              
1283 16009           NI_ip_inttoip_ipv6(nums[0], nums[1], nums[2], nums[3], buf);
1284 16009           return 1;
1285             }
1286              
1287             /**
1288             * NI_ip_binadd(): add two bitstring IP addresses.
1289             * @ip1: first bitstring IP address.
1290             * @ip2: second bitstring IP address.
1291             * @buf: result buffer.
1292             * @maxlen: maximum capacity of buffer.
1293             *
1294             * Both bitstrings must be null-terminated and of the same length as
1295             * each other. The result is stored as a bitstring and
1296             * null-terminated. The result will be of the length of the
1297             * bitstrings, regardless of the result of the addition.
1298             */
1299             int
1300 17           NI_ip_binadd(const char *ip1, const char *ip2, char *buf, int maxlen)
1301             {
1302             n128_t num1;
1303             n128_t num2;
1304             int len1;
1305             int len2;
1306              
1307 17           len1 = strlen(ip1);
1308 17           len2 = strlen(ip2);
1309              
1310 17 100         if (len1 != len2) {
1311 3           NI_set_Error_Errno(130, "IP addresses of different length");
1312 3           return 0;
1313             }
1314 14 100         if (len1 > (maxlen - 1)) {
1315 2           return 0;
1316             }
1317              
1318 12           n128_set_str_binary(&num1, ip1, len1);
1319 12           n128_set_str_binary(&num2, ip2, len2);
1320 12           n128_add(&num1, &num2);
1321 12           NI_ip_n128tobin(&num1, len1, buf);
1322              
1323 12           buf[len2] = '\0';
1324 12           return 1;
1325             }
1326              
1327             /**
1328             * NI_ip_range_to_prefix_ipv4(): get prefixes contained within range.
1329             * @begin: beginning address.
1330             * @end: ending address.
1331             * @version: IP address version.
1332             * @prefixes: prefix strings buffer.
1333             * @pcount: prefix count buffer.
1334             *
1335             * Will write at most 32 prefix strings to @prefixes.
1336             */
1337             int
1338 519           NI_ip_range_to_prefix_ipv4(unsigned long begin, unsigned long end,
1339             int version, char **prefixes, int *pcount)
1340             {
1341             unsigned long current;
1342             unsigned long mask;
1343             unsigned long tempmask;
1344              
1345             unsigned long zeroes;
1346             int iplen;
1347             int res;
1348             int i;
1349             int prefix_length;
1350             char *new_prefix;
1351             char range[4];
1352              
1353 519           current = 0;
1354 519           mask = 0;
1355              
1356 519           iplen = NI_iplengths(version);
1357              
1358 519           *pcount = 0;
1359              
1360 5638 100         while (begin <= end) {
1361             /* Calculate the number of zeroes that exist on the right of
1362             * 'begin', and create a mask for that number of bits. */
1363 5561           zeroes = NI_trailing_zeroes(begin);
1364 5561 100         if (zeroes > 32) {
1365 53           zeroes = 32;
1366             }
1367 5561           mask = 0;
1368              
1369 111595 100         for (i = 0; i < (int) zeroes; i++) {
1370 106034           tempmask = 1 << i;
1371 106034           mask |= tempmask;
1372             }
1373              
1374             /* Find the largest range (from 'begin' to 'current') that
1375             * does not exceed 'end'. */
1376              
1377             do {
1378 8110           current = begin;
1379 8110           current |= mask;
1380 8110           mask >>= 1;
1381 8110 100         } while (current > end);
1382              
1383             /* Get the prefix length for the range and add the stringified
1384             * range to @prefixes. */
1385              
1386 5561           NI_ip_get_prefix_length_ipv4(begin, current,
1387             iplen, &prefix_length);
1388              
1389 5561           new_prefix = (char *) malloc(MAX_IPV4_RANGE_STR_LEN);
1390 5561 50         if (!new_prefix) {
1391 0           printf("NI_ip_range_to_prefix: malloc failed!\n");
1392 0           return 0;
1393             }
1394              
1395 5561           prefixes[(*pcount)++] = new_prefix;
1396 5561           NI_ip_inttoip_ipv4(begin, new_prefix);
1397 5561           strcat(new_prefix, "/");
1398 5561           res = snprintf(range, 4, "%d", prefix_length);
1399 5561           strncat(new_prefix, range, res);
1400              
1401 5561           begin = current + 1;
1402              
1403             /* Do not continue getting prefixes if 'current' completely
1404             * comprises set bits. */
1405              
1406 5561 100         if (current == 0xFFFFFFFF) {
1407 442           break;
1408             }
1409             }
1410              
1411 519           return 1;
1412             }
1413              
1414             /**
1415             * NI_ip_range_to_prefix_ipv6(): get prefixes contained within range.
1416             * @begin: beginning address.
1417             * @end: ending address.
1418             * @version: IP address version.
1419             * @prefixes: prefix strings buffer.
1420             * @pcount: prefix count buffer.
1421             *
1422             * Will write at most 128 prefix strings to @prefixes.
1423             */
1424             int
1425 411           NI_ip_range_to_prefix_ipv6(n128_t *begin, n128_t *end,
1426             int version, char **prefixes, int *pcount)
1427             {
1428             n128_t current;
1429             n128_t mask;
1430             unsigned long zeroes;
1431             int iplen;
1432             unsigned long res;
1433             int i;
1434             int prefix_length;
1435             char *new_prefix;
1436             char tempip[IPV6_BITSTR_LEN];
1437             char range[4];
1438              
1439 411           iplen = NI_iplengths(version);
1440              
1441 411           tempip[iplen] = '\0';
1442 411           *pcount = 0;
1443              
1444 16030 100         while (n128_cmp(begin, end) <= 0) {
1445 15995 50         if (*pcount == 128) {
1446 0           return 0;
1447             }
1448              
1449             /* Calculate the number of zeroes that exist on the right of
1450             * 'begin', and create a mask for that number of bits. */
1451              
1452 15995           zeroes = n128_scan1(begin);
1453 15995 100         zeroes = ((zeroes == INT_MAX) ? (unsigned) iplen : zeroes) - 1;
1454              
1455 15995           n128_set_ui(&mask, 0);
1456 1319844 100         for (i = 0; i < ((int) zeroes + 1); i++) {
1457 1303849           n128_setbit(&mask, i);
1458             }
1459              
1460             /* Find the largest range (from 'begin' to 'current') that
1461             * does not exceed 'end'. */
1462              
1463             do {
1464 19030           n128_set(¤t, begin);
1465 19030           n128_ior(¤t, &mask);
1466 19030           n128_clrbit(&mask, zeroes);
1467 19030           zeroes--;
1468 19030 100         } while (n128_cmp(¤t, end) > 0);
1469              
1470             /* Get the prefix length for the range and add the stringified
1471             * range to @prefixes. */
1472              
1473 15995           NI_ip_get_prefix_length_ipv6(begin, ¤t,
1474             iplen, &prefix_length);
1475              
1476 15995           new_prefix = (char *) malloc(MAX_IPV6_RANGE_STR_LEN);
1477 15995 50         if (!new_prefix) {
1478 0           printf("NI_ip_range_to_prefix: malloc failed!\n");
1479 0           return 0;
1480             }
1481              
1482 15995           prefixes[(*pcount)++] = new_prefix;
1483 15995           NI_ip_n128tobin(begin, iplen, tempip);
1484 15995           NI_ip_bintoip(tempip, version, new_prefix);
1485 15995           strcat(new_prefix, "/");
1486 15995           res = snprintf(range, 4, "%d", prefix_length);
1487 15995           strncat(new_prefix, range, res);
1488              
1489 15995           n128_set(begin, ¤t);
1490 15995           n128_add_ui(begin, 1);
1491              
1492             /* Do not continue getting prefixes if 'current' completely
1493             * comprises set bits. */
1494              
1495 15995           res = n128_scan0(¤t);
1496 15995 100         if (res == INT_MAX) {
1497 376           break;
1498             }
1499             }
1500              
1501 411           return 1;
1502             }
1503              
1504             /**
1505             * NI_ip_range_to_prefix(): get prefixes contained within range.
1506             * @begin: first IP address as a bitstring.
1507             * @end: second IP address as a bitstring.
1508             * @version: IP address version.
1509             * @prefixes: prefix strings buffer.
1510             * @pcount: prefix count buffer.
1511             *
1512             * Both bitstrings must be null-terminated. If unsure of the number of
1513             * prefixes that will be created, @prefixes must contain space for 128
1514             * character pointers. Returns 1/0 depending on whether it completed
1515             * successfully.
1516             */
1517             int
1518 923           NI_ip_range_to_prefix(const char *begin, const char *end,
1519             int version, char **prefixes, int *pcount)
1520             {
1521             n128_t begin_n128;
1522             n128_t end_n128;
1523             unsigned long begin_ulong;
1524             unsigned long end_ulong;
1525             int iplen;
1526             int res;
1527              
1528 923 100         if (!version) {
1529 2           NI_set_Error_Errno(101, "Cannot determine IP version");
1530 2           return 0;
1531             }
1532              
1533 921 100         if (strlen(begin) != strlen(end)) {
1534 1           NI_set_Error_Errno(130, "IP addresses of different length");
1535 1           return 0;
1536             }
1537              
1538 920           iplen = NI_iplengths(version);
1539 920 100         if (!iplen) {
1540 1           return 0;
1541             }
1542              
1543 919 100         if (version == 4) {
1544 513           begin_ulong = NI_bintoint(begin, 32);
1545 513           end_ulong = NI_bintoint(end, 32);
1546 513           return NI_ip_range_to_prefix_ipv4(begin_ulong, end_ulong,
1547             version, prefixes, pcount);
1548             }
1549              
1550 406           n128_set_str_binary(&begin_n128, begin, strlen(begin));
1551 406           n128_set_str_binary(&end_n128, end, strlen(end));
1552              
1553 406           res = NI_ip_range_to_prefix_ipv6(&begin_n128, &end_n128,
1554             version, prefixes, pcount);
1555              
1556 406           return res;
1557             }
1558              
1559             /**
1560             * NI_ip_aggregate_tail(): post-processing after version-specific aggregation.
1561             * @res: the result of the relevant ip_range_to_prefix function.
1562             * @prefixes: prefix strings buffer.
1563             * @pcount: prefix count.
1564             * @version: IP address version.
1565             * @buf: the buffer for the new range.
1566             *
1567             * If @res is false, then frees the prefixes and returns zero. If no
1568             * prefixes were returned, returns zero. If more than one prefix was
1569              
1570             * returned, frees the prefixes and returns 161. Otherwise, populates
1571             * the buffer (null-terminated) with the first range from @prefixes
1572             * and returns 1.
1573             */
1574             int
1575 11           NI_ip_aggregate_tail(int res, char **prefixes, int pcount,
1576             int version, char *buf)
1577             {
1578             int i;
1579             int len;
1580             int max;
1581              
1582 11 50         if (!res) {
1583 0 0         for (i = 0; i < pcount; i++) {
1584 0           free(prefixes[i]);
1585             }
1586 0           return 0;
1587             }
1588              
1589 11 100         if (pcount == 0) {
1590 1           return 0;
1591             }
1592              
1593 10 100         if (pcount > 1) {
1594 33 100         for (i = 0; i < pcount; i++) {
1595 32           free(prefixes[i]);
1596             }
1597 1           return 161;
1598             }
1599              
1600 9           len = strlen(*prefixes);
1601 9           max = (version == 4) ? MAX_IPV4_RANGE_STR_LEN - 1
1602 9 100         : MAX_IPV6_RANGE_STR_LEN - 1;
1603 9 50         if (len > max) {
1604 0           len = max;
1605             }
1606              
1607 9           strncpy(buf, *prefixes, len);
1608 9           buf[len] = 0;
1609              
1610 9           return 1;
1611             }
1612              
1613             /**
1614             * NI_ip_aggregate_ipv6(): get the aggregate range of two ranges as a string.
1615             * @begin_1: beginning N128 integer IP address for first range.
1616             * @end_1: ending N128 integer IP address for first range.
1617             * @begin_2: beginning N128 integer IP address for second range.
1618             * @end_2: ending N128 integer IP address for second range.
1619             * @version: IP address version.
1620             * @buf: the buffer for the new range.
1621             *
1622             * See NI_ip_aggregate().
1623             */
1624             int
1625 7           NI_ip_aggregate_ipv6(n128_t *b1, n128_t *e1, n128_t *b2, n128_t *e2,
1626             int version, char *buf)
1627             {
1628             char *prefixes[128];
1629             int pcount;
1630             int res;
1631              
1632 7           n128_add_ui(e1, 1);
1633 7 100         if (n128_cmp(e1, b2)) {
1634 2           return 160;
1635             }
1636              
1637 5           pcount = 0;
1638 5           res = NI_ip_range_to_prefix_ipv6(b1, e2, version, prefixes, &pcount);
1639 5           return NI_ip_aggregate_tail(res, prefixes, pcount, version, buf);
1640             }
1641              
1642             /**
1643             * NI_ip_aggregate_ipv4(): get the aggregate range of two ranges as a string.
1644             * @begin_1: beginning integer IP address for first range.
1645             * @end_1: ending integer IP address for first range.
1646             * @begin_2: beginning integer IP address for second range.
1647             * @end_2: ending integer IP address for second range.
1648             * @version: IP address version.
1649             * @buf: the buffer for the new range.
1650             *
1651             * See NI_ip_aggregate().
1652             */
1653             int
1654 7           NI_ip_aggregate_ipv4(unsigned long b1, unsigned long e1,
1655             unsigned long b2, unsigned long e2,
1656             int version, char *buf)
1657             {
1658             char *prefixes[128];
1659             int pcount;
1660             int res;
1661              
1662 7 100         if (e1 + 1 != b2) {
1663 1           return 160;
1664             }
1665              
1666 6           pcount = 0;
1667 6           res = NI_ip_range_to_prefix_ipv4(b1, e2, version, prefixes, &pcount);
1668 6           return NI_ip_aggregate_tail(res, prefixes, pcount, version, buf);
1669             }
1670              
1671             /**
1672             * NI_ip_aggregate(): get the aggregate range of two ranges as a string.
1673             * @begin_1: beginning bitstring IP address for first range.
1674             * @end_1: ending bitstring IP address for first range.
1675             * @begin_2: beginning bitstring IP address for second range.
1676             * @end_2: ending bitstring IP address for second range.
1677             * @version: IP address version.
1678             * @buf: the buffer for the new range.
1679             *
1680             * Returns zero and sets error messages if the ranges are not
1681             * contiguous or the aggregate of the ranges cannot be represented as
1682             * a single prefix. Otherwise, populates the buffer with the aggregate
1683             * of the two ranges as a prefix range string (e.g. '1.0.0.0/8').
1684             */
1685             int
1686 14           NI_ip_aggregate(const char *b1, const char *e1,
1687             const char *b2, const char *e2,
1688             int version, char *buf)
1689             {
1690             int res;
1691             int i;
1692             n128_t b1_n128;
1693             n128_t e1_n128;
1694             n128_t b2_n128;
1695             n128_t e2_n128;
1696             unsigned long b1_ulong;
1697             unsigned long e1_ulong;
1698             unsigned long b2_ulong;
1699             unsigned long e2_ulong;
1700             const char *addr_args[4];
1701 14           addr_args[0] = b1;
1702 14           addr_args[1] = b2;
1703 14           addr_args[2] = e1;
1704 14           addr_args[3] = e2;
1705              
1706 14 100         if (!version) {
1707 1           NI_set_Error_Errno(101, "Cannot determine IP version for %s",
1708             b1);
1709 1           return 0;
1710 13 100         } else if (version == 4) {
1711 31 100         for (i = 0; i < 4; i++) {
1712 25 100         if (strlen(addr_args[i]) != 32) {
1713 1           NI_set_Error_Errno(107, "Invalid IP address %s",
1714             addr_args[i]);
1715 1           return 0;
1716             }
1717             }
1718 6           b1_ulong = NI_bintoint(b1, 32);
1719 6           e1_ulong = NI_bintoint(e1, 32);
1720 6           b2_ulong = NI_bintoint(b2, 32);
1721 6           e2_ulong = NI_bintoint(e2, 32);
1722 6           res = NI_ip_aggregate_ipv4(b1_ulong, e1_ulong,
1723             b2_ulong, e2_ulong, version, buf);
1724             } else {
1725 26 100         for (i = 0; i < 4; i++) {
1726 21 100         if (strlen(addr_args[i]) != 128) {
1727 1           NI_set_Error_Errno(108, "Invalid IP address %s",
1728             addr_args[i]);
1729 1           return 0;
1730             }
1731             }
1732 5           n128_set_str_binary(&b1_n128, b1, strlen(b1));
1733 5           n128_set_str_binary(&e1_n128, e1, strlen(e1));
1734 5           n128_set_str_binary(&b2_n128, b2, strlen(b2));
1735 5           n128_set_str_binary(&e2_n128, e2, strlen(e2));
1736 5           res = NI_ip_aggregate_ipv6(&b1_n128, &e1_n128,
1737             &b2_n128, &e2_n128, version, buf);
1738             }
1739              
1740 11 100         if (res == 0) {
1741 1           return 0;
1742             }
1743 10 100         if (res == 160) {
1744 2           NI_set_Error_Errno(160, "Ranges not contiguous - %s - %s", e1, b2);
1745 2           return 0;
1746             }
1747 8 100         if (res == 161) {
1748 1           NI_set_Error_Errno(161, "%s - %s is not a single prefix", b1, e2);
1749 1           return 0;
1750             }
1751 7           return 1;
1752             }
1753              
1754             /**
1755             * NI_ip_iptobin(): get bitstring from IP address.
1756             * @ip: single IP address as a string.
1757             * @version: IP address version.
1758             * @buf: bitstring buffer.
1759             *
1760             * Returns zero (and may also set Error/Errno) if the IP address is
1761             * invalid. If an IPv6 address is provided, it must be fully expanded
1762             * - IPv6 addresses like '0::0' or '0:0:0:0:0:0:0:0' will not be
1763             * handled correctly.
1764             */
1765              
1766             int
1767 2000           NI_ip_iptobin(const char *ip, int ipversion, char *buf)
1768             {
1769             int res;
1770             int j;
1771             int k;
1772             int y;
1773             int i;
1774             char c;
1775             int ncount;
1776             unsigned char ipv4[4];
1777              
1778 2000 100         if (ipversion == 4) {
1779 1131           res = inet_pton4(ip, ipv4);
1780 1131 100         if (res == 0) {
1781 4           return 0;
1782             }
1783              
1784 5635 100         for (j = 0; j < 4; j++) {
1785 40572 100         for (i = 0; i < 8; i++) {
1786 36064           buf[(j * 8) + i] =
1787 36064 100         ((ipv4[j] & (1 << (8 - i - 1)))) ? '1' : '0';
1788             }
1789             }
1790 1127           return 1;
1791             } else {
1792 869           j = 0;
1793 869           ncount = 0;
1794 34618 100         while ((c = ip[j])) {
1795 33749 100         if (c != ':') {
1796 27701           ncount++;
1797             }
1798 33749           j++;
1799             }
1800 869 100         if (ncount != 32) {
1801 4           NI_set_Error_Errno(102, "Bad IP address %s", ip);
1802 4           return 0;
1803             }
1804              
1805 865           i = -1;
1806 34499 100         for (j = 0; ip[j] != '\0'; j++) {
1807 33637 100         if (ip[j] == ':') {
1808 6031           continue;
1809             } else {
1810 27606           i++;
1811             }
1812              
1813 27606           y = NI_hdtoi(ip[j]);
1814 27606 100         if (y == -1) {
1815 3           return 0;
1816             }
1817              
1818 138015 100         for (k = 0; k < 4; k++) {
1819 110412           buf[ (i * 4) + k ] =
1820 110412 100         ((y >> (3 - k)) & 1) ? '1' : '0';
1821             }
1822             }
1823 862           return 1;
1824             }
1825             }
1826              
1827             /**
1828             * NI_ip_expand_address_ipv4(): expand an IPv4 address.
1829             * @ip: the IPv4 address as a string.
1830             * @buf: the IP address buffer.
1831             *
1832             * The IPv4 address string must be null-terminated. The buffer will be
1833             * null-terminated on success.
1834             */
1835             int
1836 969           NI_ip_expand_address_ipv4(const char *ip, char *buf)
1837             {
1838             int res;
1839             unsigned char ipv4[4];
1840              
1841 969           res = inet_pton4(ip, ipv4);
1842 969 100         if (!res) {
1843 4           return 0;
1844             }
1845              
1846 965           NI_ip_inttoip_ipv4(NI_ip_uchars_to_ulong(ipv4), buf);
1847              
1848 965           return 1;
1849             }
1850              
1851             /**
1852             * NI_ip_expand_address_ipv6(): expand an IPv6 address.
1853             * @ip: the IPv6 address as a string.
1854             * @buf: the IP address buffer.
1855             *
1856             * The IPv6 address string must be null-terminated. The buffer will be
1857             * null-terminated on success.
1858             */
1859             int
1860 753           NI_ip_expand_address_ipv6(const char *ip, char *retbuf)
1861             {
1862             int res;
1863             int i;
1864             unsigned char ipv6[16];
1865             unsigned long n[4];
1866              
1867 753           res = inet_pton6(ip, ipv6);
1868 753 100         if (!res) {
1869 11           return 0;
1870             }
1871              
1872 3710 100         for (i = 0; i < 4; i++) {
1873 2968           n[i] = (ipv6[(i * 4) + 0] << 24)
1874 2968           | (ipv6[(i * 4) + 1] << 16)
1875 2968           | (ipv6[(i * 4) + 2] << 8)
1876 2968           | (ipv6[(i * 4) + 3]);
1877             }
1878              
1879 742           NI_ip_inttoip_ipv6(n[0], n[1], n[2], n[3], retbuf);
1880              
1881 742           return 1;
1882             }
1883              
1884             /**
1885             * NI_ip_expand_address(): expand an IP address.
1886             * @ip: the IP address as a string.
1887             * @version: the IP address version.
1888             * @buf: the IP address buffer.
1889             *
1890             * See NI_ip_expand_address_ipv4() and NI_ip_expand_address_ipv6(). This
1891             * function dispatches to one of those functions depending on the
1892             * value of the @version argument.
1893             */
1894             int
1895 1722           NI_ip_expand_address(const char *ip, int version, char *buf)
1896             {
1897             return
1898             (version == 4)
1899 969           ? NI_ip_expand_address_ipv4(ip, buf)
1900 2691 100         : NI_ip_expand_address_ipv6(ip, buf);
1901             }
1902              
1903             /**
1904             * NI_ip_reverse_ipv4(): get reverse domain for an IPv4 address.
1905             * @ip: the IP address as a string.
1906             * @len: the prefix length of the reverse domain.
1907             * @buf: the reverse domain buffer.
1908             *
1909             * If the length is not evenly divisible by eight, then it will be
1910             * treated as though it were the next number lower than it that is
1911             * evenly divisible by eight when determining how many octets to
1912             * print. So e.g. if the length is 31, three octets from the address
1913             * will be included in the domain. The buffer is null-terminated. The
1914             * longest possible IPv4 reverse domain name contains 25 characters
1915             * (including the null terminator).
1916             */
1917             int
1918 14           NI_ip_reverse_ipv4(const char *ip, int len, char *buf)
1919             {
1920             int res;
1921             int quads;
1922             int i;
1923             char numbuf[5];
1924             unsigned char ipv4[4];
1925              
1926 14 100         if ((len < 0) || (len > 32)) {
    100          
1927 2           return 0;
1928             }
1929 12           quads = len / 8;
1930              
1931 12           res = inet_pton4(ip, ipv4);
1932 12 100         if (!res) {
1933 1           return 0;
1934             }
1935              
1936 39 100         for (i = (quads - 1); i >= 0; i--) {
1937 28           sprintf(numbuf, "%u.", ipv4[i]);
1938 28           strcat(buf, numbuf);
1939             }
1940              
1941 11           strcat(buf, "in-addr.arpa.");
1942 11           return 1;
1943             }
1944              
1945             /**
1946             * NI_ip_reverse_ipv6(): get reverse domain for an IPv4 address.
1947             * @ip: the IP address as a string.
1948             * @len: the prefix length of the reverse domain.
1949             * @buf: the reverse domain buffer.
1950             *
1951             * If the length is not evenly divisible by four, then it will be
1952             * treated as though it were the next number lower than it that is
1953             * evenly divisible by four when determining how many nibbles to
1954             * print. So e.g. if the length is 10, two nibbles from the address
1955             * will be included in the domain. The buffer is null-terminated. The
1956             * longest possible IPv6 reverse domain name contains 74 characters
1957             * (including the null terminator).
1958             */
1959             int
1960 19           NI_ip_reverse_ipv6(const char *ip, int len, char *buf)
1961             {
1962             int res;
1963             int i;
1964             int index;
1965             int shift;
1966             unsigned char ipv6[16];
1967              
1968 19 100         if ((len < 0) || (len > 128)) {
    100          
1969 2           return 0;
1970             }
1971 17           len = (len / 4);
1972              
1973 17           res = inet_pton6(ip, ipv6);
1974 17 100         if (!res) {
1975 1           return 0;
1976             }
1977              
1978 126 100         for (i = (len - 1); i >= 0; i--) {
1979 110           index = i / 2;
1980 110 100         shift = !(i % 2) * 4;
1981 110           sprintf(buf, "%x.", ((ipv6[index] >> shift) & 0xF));
1982 110           buf += 2;
1983             }
1984 16           strcat(buf, "ip6.arpa.");
1985 16           return 1;
1986             }
1987              
1988             /**
1989             * NI_ip_reverse(): get reverse domain for an IP address.
1990             * @ip: the IP address as a string.
1991             * @len: the prefix length of the reverse domain.
1992             * @buf: the reverse domain buffer.
1993             *
1994             * See NI_ip_reverse_ipv4() and NI_ip_reverse_ipv6().
1995             */
1996             int
1997 35           NI_ip_reverse(const char *ip, int len, int ipversion, char *buf)
1998             {
1999 35 100         if (!ipversion) {
2000 1           ipversion = NI_ip_get_version(ip);
2001             }
2002 35 100         if (!ipversion) {
2003 1           NI_set_Error_Errno(101, "Cannot determine IP "
2004             "version for %s", ip);
2005 1           return 0;
2006             }
2007              
2008 34 100         if (ipversion == 4) {
2009 14           return NI_ip_reverse_ipv4(ip, len, buf);
2010 20 100         } else if (ipversion == 6) {
2011 19           return NI_ip_reverse_ipv6(ip, len, buf);
2012             }
2013              
2014 1           return 0;
2015             }
2016              
2017             /**
2018             * NI_ip_normalize_prefix_ipv4(): get first and last address from prefix range.
2019             * @ip: IP address.
2020             * @slash: pointer to first '/' in original string.
2021             * @ip1buf: first IP address buffer.
2022             * @ip2buf: second IP address buffer.
2023             *
2024             * Both buffers are null-terminated on success.
2025             */
2026             int
2027 63           NI_ip_normalize_prefix_ipv4(unsigned long ip, char *slash,
2028             char *ip1buf, char *ip2buf)
2029             {
2030             unsigned long current;
2031 63           char *endptr = NULL;
2032             int res;
2033 63           long clen = 0;
2034 63           int addcst = 0;
2035             char c;
2036              
2037 63           current = ip;
2038              
2039             for (;;) {
2040 124           c = *slash++;
2041 124 100         if (c != '/') {
2042 54           break;
2043             }
2044              
2045 70           endptr = NULL;
2046              
2047 70           clen = strtol(slash, &endptr, 10);
2048 70 50         if (STRTOL_FAILED(clen, slash, endptr)) {
    50          
    0          
    100          
    100          
2049 3           return 0;
2050             }
2051 67 100         if (*endptr == ',') {
2052 8           addcst = 1;
2053 59 100         } else if (endptr != (slash + strlen(slash))) {
2054 3           NI_set_Error_Errno(172, "Invalid prefix length /%s", slash);
2055 3           return 0;
2056             } else {
2057 56           addcst = 0;
2058             }
2059              
2060 64           res = NI_ip_check_prefix_ipv4(current, clen);
2061 64 100         if (!res) {
2062 3           return 0;
2063             }
2064              
2065 61           current = NI_ip_last_address_ipv4(current, clen);
2066              
2067 61 100         if (addcst) {
2068 8           current += 1;
2069 8           slash = endptr + 1;
2070             }
2071             }
2072              
2073 54           NI_ip_inttoip_ipv4(ip, ip1buf);
2074 54           NI_ip_inttoip_ipv4(current, ip2buf);
2075              
2076 54           return 2;
2077             }
2078              
2079             /**
2080             * NI_ip_normalize_prefix_ipv6(): get first and last address from prefix range.
2081             * @ip: IP address (N128 integer).
2082             * @slash: pointer to first '/' in original string.
2083             * @ip1buf: first IP address buffer.
2084             * @ip2buf: second IP address buffer.
2085             *
2086             * Both buffers are null-terminated on success.
2087             */
2088             int
2089 48           NI_ip_normalize_prefix_ipv6(n128_t *ip, char *slash,
2090             char *ip1buf, char *ip2buf)
2091             {
2092             n128_t current;
2093 48           char *endptr = NULL;
2094             int res;
2095 48           long clen = 0;
2096 48           int addcst = 0;
2097             char c;
2098              
2099 48           n128_set(¤t, ip);
2100              
2101             for (;;) {
2102 95           c = *slash++;
2103 95 100         if (c != '/') {
2104 42           break;
2105             }
2106              
2107 53           endptr = NULL;
2108              
2109 53           clen = strtol(slash, &endptr, 10);
2110 53 50         if (STRTOL_FAILED(clen, slash, endptr)) {
    50          
    0          
    100          
    100          
2111 1           return 0;
2112             }
2113 52 100         if (*endptr == ',') {
2114 5           addcst = 1;
2115 47 100         } else if (endptr != (slash + strlen(slash))) {
2116 2           NI_set_Error_Errno(172, "Invalid prefix length /%s", slash);
2117 2           return 0;
2118             } else {
2119 45           addcst = 0;
2120             }
2121              
2122 50           res = NI_ip_check_prefix_ipv6(¤t, clen);
2123 50 100         if (!res) {
2124 3           return 0;
2125             }
2126              
2127 47           NI_ip_last_address_ipv6(¤t, clen, ¤t);
2128              
2129 47 100         if (addcst) {
2130 5           n128_add_ui(¤t, 1);
2131 5           slash = endptr + 1;
2132             }
2133             }
2134              
2135 42           NI_ip_inttoip_n128(ip, ip1buf);
2136 42           NI_ip_inttoip_n128(¤t, ip2buf);
2137              
2138 42           return 2;
2139             }
2140              
2141             /**
2142             * NI_ip_normalize_prefix(): get first and last address from prefix range.
2143             * @ip: IP address prefix range as a string.
2144             * @ip1buf: first IP address buffer.
2145             * @ip2buf: second IP address buffer.
2146             *
2147             * The range can include commas and additional prefixes, e.g.
2148             * '0.0.0.0/32,/32,/32' will yield '0.0.0.0' and '0.0.0.2'.
2149             */
2150             int
2151 978           NI_ip_normalize_prefix(char *ip, char *ip1buf, char *ip2buf)
2152             {
2153             char c;
2154             int res;
2155             int i;
2156             char *slash;
2157             int islash;
2158             char *start;
2159             unsigned char ipnum[16];
2160             unsigned long ipv4;
2161             n128_t ipv6;
2162             int ipversion;
2163              
2164 978           i = 0;
2165 978           slash = NULL;
2166 978           islash = -1;
2167 978           start = ip;
2168              
2169 24087 100         while ((c = *ip)) {
2170 23930 100         if (isspace(c)) {
2171 821           return -1;
2172             }
2173 23109 100         if (i && (c == '/') && (!slash)) {
    100          
    100          
2174 112           slash = ip;
2175 112           islash = i;
2176             }
2177 23109           i++;
2178 23109           ip++;
2179             }
2180              
2181 157 100         if (islash < 1) {
2182 45           return -1;
2183             }
2184              
2185 112           *slash = '\0';
2186 112           ipversion = NI_ip_get_version(start);
2187              
2188 112 100         if (ipversion == 4) {
2189 63           res = inet_pton4(start, ipnum);
2190 63 50         if (!res) {
2191 0           return 0;
2192             }
2193 63           *slash = '/';
2194 63           ipv4 = NI_ip_uchars_to_ulong(ipnum);
2195 63           return NI_ip_normalize_prefix_ipv4(ipv4,
2196             slash,
2197             ip1buf,
2198             ip2buf);
2199 49 100         } else if (ipversion == 6) {
2200 48           res = inet_pton6(start, ipnum);
2201 48 50         if (!res) {
2202 0           return 0;
2203             }
2204 48           *slash = '/';
2205 48           NI_ip_uchars_to_n128(ipnum, &ipv6);
2206 48           res = NI_ip_normalize_prefix_ipv6(&ipv6,
2207             slash,
2208             ip1buf,
2209             ip2buf);
2210 48           return res;
2211             } else {
2212 1           return 0;
2213             }
2214             }
2215              
2216             /**
2217             * NI_ip_tokenize_on_char(): get parts of string before and after char.
2218             * @str: the string to tokenize.
2219             * @separator: the char that separates the two parts of the string.
2220             * @end_first: buffer for the end of the first string.
2221             * @second: buffer for the start of the second string.
2222             *
2223             * Tokenizes the string based on the @separator character. Ignores
2224             * whitespace occurring before and after @separator. For example, if
2225             * the string provided is '127.0.0.1 - 127.0.0.255', @end_first will
2226             * point to the space immediately after the first IP address, and
2227             * @second will point to the second IP address.
2228             */
2229             int
2230 920           NI_ip_tokenize_on_char(char *str, char separator,
2231             char **end_first, char **second)
2232             {
2233             char c;
2234             char *break_char;
2235             int i;
2236             int hit_separator;
2237              
2238 920           break_char = NULL;
2239 920           i = 0;
2240 920           hit_separator = 0;
2241              
2242 26936 100         while ((c = *str)) {
2243 26852 100         if (c == separator) {
2244 836           hit_separator = 1;
2245 836 100         if (!break_char) {
2246 17 100         if (!i) {
2247 3           return 0;
2248             } else {
2249 14           break_char = str;
2250             }
2251             }
2252 833           break;
2253 26016 100         } else if (isspace(c)) {
2254 856 100         if (!break_char) {
2255 849           break_char = str;
2256             }
2257             } else {
2258 25160           break_char = NULL;
2259             }
2260 26016           str++;
2261 26016           i++;
2262             }
2263              
2264 917 100         if (!hit_separator) {
2265 84           return 0;
2266             }
2267              
2268 833           str++;
2269 833           c = *str;
2270 833 100         if (c == '\0') {
2271 1           return 0;
2272             }
2273              
2274 1657 100         while ((c = *str) && (isspace(c))) {
    100          
2275 825           str++;
2276             }
2277              
2278 832 100         if (c == '\0') {
2279 1           return 0;
2280             }
2281              
2282 831           *end_first = break_char;
2283 831           *second = str;
2284              
2285 831           return 1;
2286             }
2287              
2288             /**
2289             * NI_ip_normalize_range(): get first and last address from a range.
2290             * @ip: the IP address range to normalize.
2291             * @ipbuf1: first IP address buffer.
2292             * @ipbuf2: second IP address buffer.
2293             *
2294             * @ip must be a range containing a hyphen, e.g. '127.0.0.0 -
2295             * 127.0.0.255'. Whitespace before and after the hyphen is ignored.
2296             * The IP address buffers will be null-terminated on success.
2297             */
2298             int
2299 866           NI_ip_normalize_range(char *ip, char *ipbuf1, char *ipbuf2)
2300             {
2301             char *break_char;
2302             char *start;
2303             int ipversion;
2304             int res;
2305             char old_char;
2306              
2307 866           res = NI_ip_tokenize_on_char(ip, '-', &break_char, &start);
2308 866 100         if (!res) {
2309 54           return -1;
2310             }
2311              
2312 812           old_char = *break_char;
2313 812           *break_char = '\0';
2314              
2315 812           ipversion = NI_ip_get_version(start);
2316 812 100         if (!ipversion) {
2317 1           *break_char = old_char;
2318 1           return 0;
2319             }
2320              
2321 811           res = NI_ip_expand_address(ip, ipversion, ipbuf1);
2322 811           *break_char = old_char;
2323              
2324 811 100         if (!res) {
2325 1           return 0;
2326             }
2327              
2328 810           res = NI_ip_expand_address(start, ipversion, ipbuf2);
2329 810 50         if (!res) {
2330 0           return 0;
2331             }
2332              
2333 810           return 2;
2334             }
2335              
2336             /**
2337             * NI_ip_normalize_plus_ipv4(): get first and last address from addition.
2338             * @ip: the IP address string.
2339             * @num: the number of addresses to add as a string.
2340             * @ipbuf1: first IP address buffer.
2341             * @ipbuf2: second IP address buffer.
2342             *
2343             * The IP address buffers will be null-terminated on success.
2344             */
2345             int
2346 15           NI_ip_normalize_plus_ipv4(char *ip, char *num,
2347             char *ipbuf1, char *ipbuf2)
2348             {
2349             int res;
2350             char *endptr;
2351             unsigned char ipnum[4];
2352             unsigned long ipv4;
2353             unsigned long addnum;
2354              
2355 15           res = inet_pton4(ip, ipnum);
2356 15 50         if (!res) {
2357 0           return 0;
2358             }
2359              
2360 15           ipv4 = NI_ip_uchars_to_ulong(ipnum);
2361              
2362 15           endptr = NULL;
2363              
2364 15           addnum = strtoul(num, &endptr, 10);
2365 15 100         if (STRTOUL_FAILED(addnum, num, endptr)) {
    100          
    50          
    50          
    0          
2366 2           return 0;
2367             }
2368 13 100         if (addnum > 0xFFFFFFFF) {
2369 1           return 0;
2370             }
2371              
2372 12           NI_ip_inttoip_ipv4(ipv4, ipbuf1);
2373 12           ipv4 += addnum;
2374 12           NI_ip_inttoip_ipv4(ipv4, ipbuf2);
2375              
2376 12           return 2;
2377             }
2378              
2379             /**
2380             * NI_ip_normalize_plus_ipv6(): get first and last address from addition.
2381             * @ip: the IP address string.
2382             * @num: the number of addresses to add as a string.
2383             * @ipbuf1: first IP address buffer.
2384             * @ipbuf2: second IP address buffer.
2385             *
2386             * The IP address buffers will be null-terminated on success.
2387             */
2388             int
2389 4           NI_ip_normalize_plus_ipv6(char *ip, char *num,
2390             char *ipbuf1, char *ipbuf2)
2391             {
2392             unsigned char ipnum[16];
2393             n128_t ipv6;
2394             n128_t addnum;
2395             int res;
2396              
2397 4           res = inet_pton6(ip, ipnum);
2398 4 50         if (!res) {
2399 0           return 0;
2400             }
2401              
2402 4           NI_ip_uchars_to_n128(ipnum, &ipv6);
2403              
2404 4           res = n128_set_str_decimal(&addnum, num, strlen(num));
2405 4 100         if (!res) {
2406 2           return 0;
2407             }
2408              
2409 2           NI_ip_inttoip_n128(&ipv6, ipbuf1);
2410 2           n128_add(&ipv6, &addnum);
2411 2           NI_ip_inttoip_n128(&ipv6, ipbuf2);
2412              
2413 2           return 2;
2414             }
2415              
2416             /**
2417             * NI_ip_normalize_plus(): get first and last address from addition.
2418             * @ip: the IP address string.
2419             * @ipbuf1: first IP address buffer.
2420             * @ipbuf2: second IP address buffer.
2421             *
2422             * @ip must begin with an IP address, then contain a '+' and an
2423             * integer, e.g. '127.0.0.0 + 16777216', '2000::+1234849245892845'.
2424             * The IP address buffers will be null-terminated on success.
2425             */
2426             int
2427 54           NI_ip_normalize_plus(char *ip1, char *ipbuf1, char *ipbuf2)
2428             {
2429             char *break_char;
2430             char *start;
2431             int ipversion;
2432             int res;
2433             char old_char;
2434              
2435 54           res = NI_ip_tokenize_on_char(ip1, '+', &break_char, &start);
2436 54 100         if (!res) {
2437 35           return -1;
2438             }
2439              
2440 19           old_char = *break_char;
2441 19           *break_char = '\0';
2442              
2443 19           ipversion = NI_ip_get_version(ip1);
2444              
2445 19           switch (ipversion) {
2446 15           case 4: res = NI_ip_normalize_plus_ipv4(ip1, start, ipbuf1, ipbuf2);
2447 15           break;
2448 4           case 6: res = NI_ip_normalize_plus_ipv6(ip1, start, ipbuf1, ipbuf2);
2449 4           break;
2450 0           default: res = 0;
2451             }
2452              
2453 19           *break_char = old_char;
2454 19           return res;
2455             }
2456              
2457             /**
2458             * NI_ip_normalize_bare(): normalize a single IP address.
2459             * @ip: the IP address string.
2460             * @ipbuf1: the IP address buffer.
2461             *
2462             * Checks the version of the IP address and then expands it. For a
2463             * valid IP address, this function has the same effect as calling
2464             * NI_ip_expand_address(). The IP address buffer will be
2465             * null-terminated on success.
2466             */
2467             int
2468 35           NI_ip_normalize_bare(char *ip, char *ipbuf1)
2469             {
2470             int ipversion;
2471             int res;
2472              
2473 35           ipversion = NI_ip_get_version(ip);
2474 35 100         if (!ipversion) {
2475 9           return 0;
2476             }
2477              
2478 26           res = NI_ip_expand_address(ip, ipversion, ipbuf1);
2479 26 50         if (!res) {
2480 0           return 0;
2481             }
2482              
2483 26           return 1;
2484             }
2485              
2486             /**
2487             * NI_ip_normalize(): normalize an IP address string.
2488             * @ip: the IP address string.
2489             * @ipbuf1: the first IP address buffer.
2490             * @ipbuf2: the second IP address buffer.
2491             *
2492             * The various formats that @ip can take are described in
2493             * NI_ip_normalize_prefix(), NI_ip_normalize_range(),
2494             * NI_ip_normalize_plus() and NI_ip_normalize_bare(). Returns zero on
2495             * failure, otherwise returns the number of IP address buffers that
2496             * were populated. Those buffers will be null-terminated on success.
2497             */
2498             int
2499 978           NI_ip_normalize(char *ip, char *ipbuf1, char *ipbuf2)
2500             {
2501             int res;
2502              
2503 978           res = NI_ip_normalize_prefix(ip, ipbuf1, ipbuf2);
2504 978 100         if (res >= 0) {
2505 112           return res;
2506             }
2507              
2508 866           res = NI_ip_normalize_range(ip, ipbuf1, ipbuf2);
2509 866 100         if (res >= 0) {
2510 812           return res;
2511             }
2512              
2513 54           res = NI_ip_normalize_plus(ip, ipbuf1, ipbuf2);
2514 54 100         if (res >= 0) {
2515 19           return res;
2516             }
2517              
2518 35           res = NI_ip_normalize_bare(ip, ipbuf1);
2519 35 50         if (res >= 0) {
2520 35           return res;
2521             }
2522              
2523 0           return 0;
2524             }
2525              
2526             /**
2527             * NI_ip_normal_range(): normalize an IP address string into a range.
2528             * @ip: the IP address string.
2529             * @buf: the IP address range buffer.
2530             *
2531             * Uses NI_ip_normalize() to get the first and last (if applicable)
2532             * addresses from the string. Sets the buffer so that it is always in
2533             * range format (i.e. first address, hyphen, second address), even
2534             * where the IP address string contains only one address, in which
2535             * case both of the addresses will be the same. @buf is
2536             * null-terminated on success.
2537             */
2538             int
2539 9           NI_ip_normal_range(char *ip, char *buf)
2540             {
2541             char ip1buf[MAX_IPV6_STR_LEN];
2542             char ip2buf[MAX_IPV6_STR_LEN];
2543             int res;
2544              
2545 9           res = NI_ip_normalize(ip, ip1buf, ip2buf);
2546 9 100         if (!res) {
2547 3           return 0;
2548             }
2549              
2550 6 100         sprintf(buf, "%s - %s", ip1buf,
2551             (res == 1) ? ip1buf : ip2buf);
2552              
2553 6           return 1;
2554             }
2555              
2556             /**
2557             * NI_ip_compress_v4_prefix(): get smallest representation of IPv4 prefix range.
2558             * @ip: the IP address.
2559             * @len: the prefix length of the range.
2560             * @buf: buffer for the compressed representation.
2561             * @maxlen: maximum capacity of buffer.
2562             */
2563             int
2564 52           NI_ip_compress_v4_prefix(const char *ip, int len, char *buf, int maxlen)
2565             {
2566             int dotcount;
2567             const char *c;
2568             int buflen;
2569              
2570 52 100         if ((len < 0) || (len > 32)) {
    100          
2571 3           return 0;
2572             }
2573 49 100         if (strlen(ip) > (MAX_IPV4_RANGE_STR_LEN - 1)) {
2574 2           return 0;
2575             }
2576              
2577 47           c = ip;
2578 47 100         dotcount = (len == 0) ? 1 : ((len / 8) + (!(len % 8) ? 0 : 1));
2579 141 100         while (dotcount--) {
2580 108           c = strchr(c, '.');
2581 108 100         if (c == NULL) {
2582 14           c = ip + (strlen(ip) + 1);
2583 14           break;
2584             }
2585 94 50         if (*(c + 1) != '\0') {
2586 94           c++;
2587             }
2588             }
2589              
2590 47           buflen = c - ip - 1;
2591 47 50         if (buflen > maxlen) {
2592 0           buflen = maxlen;
2593             }
2594              
2595 47           strncpy(buf, ip, buflen);
2596 47           buf[buflen] = '\0';
2597              
2598 47           return 1;
2599             }
2600              
2601             /**
2602             * NI_ip_compress_address(): get smallest representation of IPv6 prefix range.
2603             * @ip: the IP address.
2604             * @version: the IP address version.
2605             * @buf: buffer for the compressed representation.
2606             *
2607             * If @ip is an IPv4 address, it will simply be copied to @buf.
2608             */
2609             int
2610 19           NI_ip_compress_address(const char *ip, int version, char *buf)
2611             {
2612             unsigned char ipv6[16];
2613             int i;
2614             char mybuf[5];
2615             int res;
2616 19           int in_ws = 0;
2617 19           int ws_index = -1;
2618             int ws_start[4];
2619             int ws_count[4];
2620             int largest_index;
2621             int largest;
2622              
2623 19           memset(ws_start, 0, 4 * sizeof(int));
2624 19           memset(ws_count, 0, 4 * sizeof(int));
2625              
2626 19 100         if (!version) {
2627 1           NI_set_Error_Errno(101, "Cannot determine IP version for %s",
2628             ip);
2629 1           return 0;
2630             }
2631              
2632 18 100         if (version == 4) {
2633 1           strcpy(buf, ip);
2634 1           return 1;
2635             }
2636              
2637 17           res = inet_pton6(ip, ipv6);
2638 17 100         if (!res) {
2639 1           return 0;
2640             }
2641              
2642 144 100         for (i = 0; i < 16; i += 2) {
2643 128 100         if ((ipv6[i] == 0) && (ipv6[i + 1] == 0)) {
    100          
2644 72 100         if (!in_ws) {
2645 26           in_ws = 1;
2646 26           ws_start[++ws_index] = i;
2647             }
2648 72           ws_count[ws_index] += 1;
2649             } else {
2650 56           in_ws = 0;
2651             }
2652             }
2653              
2654 16           largest = 0;
2655 16           largest_index = -1;
2656              
2657 80 100         for (i = 0; i < 4; i++) {
2658             /* "The symbol '::' MUST NOT be used to shorten just one 16-bit 0
2659             field. For example, the representation 2001:db8:0:1:1:1:1:1 is
2660             correct, but 2001:db8::1:1:1:1:1 is not correct"
2661             (RFC 5952, [4.2.2]). So make sure that ws_count is greater
2662             than 1. */
2663 64 100         if (ws_count[i] > largest && ws_count[i] > 1) {
    100          
2664 14           largest = ws_count[i];
2665 14           largest_index = i;
2666             }
2667             }
2668              
2669 101 100         for (i = 0; i < 16; i += 2) {
2670 85 100         if ((largest_index != -1) && (i == ws_start[largest_index])) {
    100          
2671 12 100         if (i == 0) {
2672 2           strcat(buf, ":");
2673             }
2674 12           i += ((largest * 2) - 2);
2675 12           strcat(buf, ":");
2676             } else {
2677 73           sprintf(mybuf, "%x",
2678 73           (ipv6[i] << 8) + ipv6[i + 1]);
2679 73           strcat(buf, mybuf);
2680 73 100         if (i < 14) {
2681 59           strcat(buf, ":");
2682             }
2683             }
2684             }
2685              
2686 16           return 1;
2687             }
2688              
2689             /**
2690             * NI_ip_splitprefix(): split range into IP address and prefix length.
2691             * @prefix: the IP address prefix range.
2692             * @ipbuf: the IP address buffer.
2693             * @lenbuf: the prefix length buffer.
2694             */
2695             int
2696 147           NI_ip_splitprefix(const char *prefix, char *ipbuf, int *lenbuf)
2697             {
2698             const char *c;
2699             const char *slash;
2700             char *endptr;
2701             long num;
2702             int len;
2703              
2704 147           c = slash = strchr(prefix, '/');
2705 147 100         if (!slash) {
2706 2           return 0;
2707             }
2708              
2709 145           len = slash - prefix;
2710 145 100         if ((len == 0) || (len > (MAX_IPV6_STR_LEN - 1))) {
    100          
2711 3           return 0;
2712             }
2713              
2714 142           c++;
2715 142 100         if (*c == '\0') {
2716 1           return 0;
2717             }
2718              
2719 141           endptr = NULL;
2720              
2721 141           num = strtol(c, &endptr, 10);
2722 141 50         if (STRTOL_FAILED(num, c, endptr)) {
    100          
    50          
    100          
    100          
2723 2           return 0;
2724             }
2725 139 100         if (num < 0) {
2726 1           return 0;
2727             }
2728              
2729 138           memcpy(ipbuf, prefix, len);
2730 138           ipbuf[len] = '\0';
2731 138           *lenbuf = num;
2732              
2733 138           return 1;
2734             }
2735              
2736             /**
2737             * NI_ip_iptype(): get type of IP address as a string.
2738             * @ip: the IP address.
2739             * @version: the IP address version.
2740             * @buf: the type buffer.
2741             *
2742             * The type buffer will be null-terminated on success. Relies on
2743             * IPv4ranges and IPv6ranges for determining types.
2744             */
2745             int
2746 16           NI_ip_iptype(const char *ip, int version, char *buf)
2747             {
2748             HV *hash;
2749             HE *entry;
2750             char *key;
2751             I32 keylen;
2752             SV *value;
2753             STRLEN len;
2754             int current_keylen;
2755             char *typestr;
2756              
2757 16 100         hash = get_hv(
2758             (version == 4 ? "Net::IP::XS::IPv4ranges"
2759             : "Net::IP::XS::IPv6ranges"), 0);
2760              
2761 16 50         if (!hash) {
2762 0           return 0;
2763             }
2764              
2765 16           hv_iterinit(hash);
2766 16           current_keylen = 0;
2767              
2768 284 100         while ((entry = hv_iternext(hash))) {
2769 268           key = hv_iterkey(entry, &keylen);
2770 268 100         if (keylen > current_keylen) {
2771 178 100         if (!strncmp(key, ip, keylen)) {
2772 13           current_keylen = keylen;
2773 13           value = hv_iterval(hash, entry);
2774 13           typestr = SvPV(value, len);
2775 13 100         if (len > (MAX_TYPE_STR_LEN - 1)) {
2776 1           len = (MAX_TYPE_STR_LEN - 1);
2777             }
2778 13           memcpy(buf, typestr, len);
2779 13           buf[len] = '\0';
2780             }
2781             }
2782             }
2783              
2784 16 100         if (current_keylen) {
2785 10           return 1;
2786             }
2787              
2788 6 100         if (version == 4) {
2789 3           memcpy(buf, "PUBLIC", 6);
2790 3           buf[6] = '\0';
2791 3           return 1;
2792             }
2793              
2794 3           NI_set_Error_Errno(180, "Cannot determine type for %s", ip);
2795              
2796 3           return 0;
2797             }
2798              
2799             /**
2800             * NI_ip_is_valid_mask(): determine the validity of a bitstring mask.
2801             * @mask: bitstring mask.
2802             * @version: mask's IP address version.
2803             */
2804             int
2805 9           NI_ip_is_valid_mask(const char *mask, int version)
2806             {
2807             const char *c;
2808             int iplen;
2809             int mask_len;
2810             int state;
2811              
2812 9 100         if (!version) {
2813 1           NI_set_Error_Errno(101, "Cannot determine IP version for %s",
2814             mask);
2815 1           return 0;
2816             }
2817              
2818 8           iplen = NI_iplengths(version);
2819 8           mask_len = strlen(mask);
2820              
2821 8 100         if (mask_len != iplen) {
2822 2           NI_set_Error_Errno(150, "Invalid mask length for %s", mask);
2823 2           return 0;
2824             }
2825              
2826 6           state = 0;
2827 6           c = mask;
2828              
2829 103 100         while (*c != '\0') {
2830 99 100         if ((*c == '1') && (state == 0)) {
    100          
2831 33           c++;
2832 33           continue;
2833             }
2834 66 100         if (*c == '0') {
2835 64 100         if (state == 0) {
2836 3           state = 1;
2837             }
2838 64           c++;
2839 64           continue;
2840             }
2841 2           NI_set_Error_Errno(151, "Invalid mask %s", mask);
2842 2           return 0;
2843             }
2844              
2845 4           return 1;
2846             }
2847              
2848             /**
2849             * NI_ip_prefix_to_range(): get begin/end addresses from address and length.
2850             * @ip: IP address.
2851             * @len: prefix length of range.
2852             * @version: IP address version.
2853             * @buf: last IP address buffer.
2854             */
2855             int
2856 22           NI_ip_prefix_to_range(const char *ip, int len, int version, char *buf)
2857             {
2858             char bitstr1[IPV6_BITSTR_LEN];
2859             char bitstr2[IPV6_BITSTR_LEN];
2860              
2861 22 100         if (!version) {
2862 1           NI_set_Error_Errno(101, "Cannot determine IP version");
2863 1           return 0;
2864             }
2865              
2866 21 100         if (!NI_ip_expand_address(ip, version, buf)) {
2867 1           return 0;
2868             }
2869              
2870 20 50         if (!NI_ip_iptobin(ip, version, bitstr1)) {
2871 0           return 0;
2872             }
2873              
2874 20 100         bitstr1[(version == 4) ? 32 : 128] = '\0';
2875              
2876 20 100         if (!NI_ip_check_prefix(bitstr1, len, version)) {
2877 1           return 0;
2878             }
2879              
2880 19           NI_ip_last_address_bin(bitstr1, len, version, bitstr2);
2881              
2882 19 100         bitstr2[(version == 4) ? 32 : 128] = '\0';
2883              
2884 19 50         if (!NI_ip_bintoip(bitstr2, version, buf)) {
2885 0           return 0;
2886             }
2887              
2888 19           return 1;
2889             }
2890              
2891             /**
2892             * NI_ip_get_embedded_ipv4(): get IPv4 address contained within IPv6 address.
2893             * @ipv6: IPv6 address as a string.
2894             * @buf: IPv4 address buffer.
2895             */
2896             int
2897 8           NI_ip_get_embedded_ipv4(const char *ipv6, char *buf)
2898             {
2899             const char *c;
2900             int len;
2901              
2902 8           c = strrchr(ipv6, ':');
2903 8 100         if (c == NULL) {
2904 3           c = ipv6;
2905             } else {
2906 5           c++;
2907             }
2908              
2909 8           len = strlen(c);
2910 8 100         if (len > (MAX_IPV4_STR_LEN - 1)) {
2911 1           len = (MAX_IPV4_STR_LEN - 1);
2912             }
2913 8 100         if ((len > 0) && NI_ip_is_ipv4(c)) {
    100          
2914 4           strncpy(buf, c, len);
2915 4           buf[len] = '\0';
2916 4           return 1;
2917             } else {
2918 4           return 0;
2919             }
2920             }
2921              
2922             #ifdef __cplusplus
2923             }
2924             #endif