udf: Remove NULL dereference.
[dragonfly.git] / usr.sbin / ppp / ncpaddr.c
blob0687d8f30e5de12f36d05383788e7c32cca425d6
1 /*-
2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/ppp/ncpaddr.c,v 1.10.2.3 2003/04/29 16:05:55 ume Exp $
27 * $DragonFly: src/usr.sbin/ppp/ncpaddr.c,v 1.4 2008/05/19 10:19:49 corecode Exp $
30 #include <sys/types.h>
31 #include <sys/select.h>
32 #include <sys/socket.h>
33 #ifdef __OpenBSD__
34 #include <net/if_types.h>
35 #include <net/route.h>
36 #endif
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 #include <arpa/inet.h>
41 #include <sys/un.h>
43 #include <netdb.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <termios.h>
49 #include "log.h"
50 #include "ncpaddr.h"
51 #include "timer.h"
52 #include "fsm.h"
53 #include "defs.h"
54 #include "slcompress.h"
55 #include "iplist.h"
56 #include "throughput.h"
57 #include "mbuf.h"
58 #include "ipcp.h"
59 #include "descriptor.h"
60 #include "layer.h"
61 #include "lqr.h"
62 #include "hdlc.h"
63 #include "lcp.h"
64 #include "ccp.h"
65 #include "link.h"
66 #include "mp.h"
67 #include "ipv6cp.h"
68 #include "ncp.h"
71 #define ncprange_ip4addr u.ip4.ipaddr
72 #define ncprange_ip4mask u.ip4.mask
73 #define ncprange_ip4width u.ip4.width
74 #define ncpaddr_ip4addr u.ip4addr
75 #ifndef NOINET6
76 #define ncprange_ip6addr u.ip6.ipaddr
77 #define ncprange_ip6width u.ip6.width
78 #define ncpaddr_ip6addr u.ip6addr
79 #endif
81 #define NCP_ASCIIBUFFERSIZE 52
83 static struct in_addr
84 bits2mask4(int bits)
86 struct in_addr result;
87 u_int32_t bit = 0x80000000;
89 result.s_addr = 0;
91 while (bits) {
92 result.s_addr |= bit;
93 bit >>= 1;
94 bits--;
97 result.s_addr = htonl(result.s_addr);
98 return result;
101 static int
102 mask42bits(struct in_addr mask)
104 u_int32_t msk = ntohl(mask.s_addr);
105 u_int32_t tst;
106 int ret;
108 for (ret = 32, tst = 1; tst; ret--, tst <<= 1)
109 if (msk & tst)
110 break;
112 for (tst <<= 1; tst; tst <<= 1)
113 if (!(msk & tst))
114 break;
116 return tst ? -1 : ret;
119 #ifndef NOINET6
120 static struct in6_addr
121 bits2mask6(int bits)
123 struct in6_addr result;
124 u_int32_t bit = 0x80;
125 u_char *c = result.s6_addr;
127 memset(&result, '\0', sizeof result);
129 while (bits) {
130 if (bit == 0) {
131 bit = 0x80;
132 c++;
134 *c |= bit;
135 bit >>= 1;
136 bits--;
139 return result;
142 static int
143 mask62bits(const struct in6_addr *mask)
145 const u_char masks[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
146 const u_char *c, *p, *end;
147 int masklen;
149 p = (const u_char *)mask;
150 for (masklen = 0, end = p + 16; p < end && *p == 0xff; p++)
151 masklen += 8;
153 if (p < end) {
154 for (c = masks; c < masks + sizeof masks; c++)
155 if (*c == *p) {
156 masklen += c - masks;
157 break;
161 return masklen;
164 static void
165 adjust_linklocal(struct sockaddr_in6 *sin6)
167 /* XXX: ?????!?!?!!!!! This is horrible ! */
168 #if 0
170 * The kernel does not understand sin6_scope_id for routing at this moment.
171 * We should rather keep the embedded ID.
172 * jinmei@kame.net, 20011026
174 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
175 IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr)) {
176 sin6->sin6_scope_id =
177 ntohs(*(u_short *)&sin6->sin6_addr.s6_addr[2]);
178 *(u_short *)&sin6->sin6_addr.s6_addr[2] = 0;
180 #endif
182 #endif
184 void
185 ncpaddr_init(struct ncpaddr *addr)
187 addr->ncpaddr_family = AF_UNSPEC;
191 ncpaddr_isset(const struct ncpaddr *addr)
193 return addr->ncpaddr_family != AF_UNSPEC;
197 ncpaddr_isdefault(const struct ncpaddr *addr)
199 switch (addr->ncpaddr_family) {
200 case AF_INET:
201 if (addr->ncpaddr_ip4addr.s_addr == INADDR_ANY)
202 return 1;
203 break;
205 #ifndef NOINET6
206 case AF_INET6:
207 if (IN6_IS_ADDR_UNSPECIFIED(&addr->ncpaddr_ip6addr))
208 return 1;
209 break;
210 #endif
213 return 0;
217 ncpaddr_equal(const struct ncpaddr *addr, const struct ncpaddr *cmp)
219 if (addr->ncpaddr_family != cmp->ncpaddr_family)
220 return 0;
222 switch (addr->ncpaddr_family) {
223 case AF_INET:
224 return addr->ncpaddr_ip4addr.s_addr == cmp->ncpaddr_ip4addr.s_addr;
226 #ifndef NOINET6
227 case AF_INET6:
228 return !memcmp(&addr->ncpaddr_ip6addr, &cmp->ncpaddr_ip6addr,
229 sizeof addr->ncpaddr_ip6addr);
230 #endif
232 case AF_UNSPEC:
233 return 1;
236 return 0;
239 void
240 ncpaddr_copy(struct ncpaddr *addr, const struct ncpaddr *from)
242 switch (from->ncpaddr_family) {
243 case AF_INET:
244 addr->ncpaddr_family = AF_INET;
245 addr->ncpaddr_ip4addr = from->ncpaddr_ip4addr;
246 break;
247 #ifndef NOINET6
248 case AF_INET6:
249 addr->ncpaddr_family = AF_INET6;
250 addr->ncpaddr_ip6addr = from->ncpaddr_ip6addr;
251 break;
252 #endif
253 default:
254 addr->ncpaddr_family = AF_UNSPEC;
258 void
259 ncpaddr_setip4addr(struct ncpaddr *addr, u_int32_t ip)
261 addr->ncpaddr_family = AF_INET;
262 addr->ncpaddr_ip4addr.s_addr = ip;
266 ncpaddr_getip4addr(const struct ncpaddr *addr, u_int32_t *ip)
268 if (addr->ncpaddr_family != AF_INET)
269 return 0;
270 *ip = addr->ncpaddr_ip4addr.s_addr;
271 return 1;
274 void
275 ncpaddr_setip4(struct ncpaddr *addr, struct in_addr ip)
277 addr->ncpaddr_family = AF_INET;
278 addr->ncpaddr_ip4addr = ip;
282 ncpaddr_getip4(const struct ncpaddr *addr, struct in_addr *ip)
284 if (addr->ncpaddr_family != AF_INET)
285 return 0;
286 *ip = addr->ncpaddr_ip4addr;
287 return 1;
290 #ifndef NOINET6
291 void
292 ncpaddr_setip6(struct ncpaddr *addr, const struct in6_addr *ip6)
294 addr->ncpaddr_family = AF_INET6;
295 addr->ncpaddr_ip6addr = *ip6;
299 ncpaddr_getip6(const struct ncpaddr *addr, struct in6_addr *ip6)
301 if (addr->ncpaddr_family != AF_INET6)
302 return 0;
303 *ip6 = addr->ncpaddr_ip6addr;
304 return 1;
306 #endif
308 void
309 ncpaddr_getsa(const struct ncpaddr *addr, struct sockaddr_storage *host)
311 struct sockaddr_in *host4 = (struct sockaddr_in *)host;
312 #ifndef NOINET6
313 struct sockaddr_in6 *host6 = (struct sockaddr_in6 *)host;
314 #endif
316 memset(host, '\0', sizeof(*host));
318 switch (addr->ncpaddr_family) {
319 case AF_INET:
320 host4->sin_family = AF_INET;
321 host4->sin_len = sizeof(*host4);
322 host4->sin_addr = addr->ncpaddr_ip4addr;
323 break;
325 #ifndef NOINET6
326 case AF_INET6:
327 host6->sin6_family = AF_INET6;
328 host6->sin6_len = sizeof(*host6);
329 host6->sin6_addr = addr->ncpaddr_ip6addr;
330 break;
331 #endif
333 default:
334 host->ss_family = AF_UNSPEC;
335 break;
339 void
340 ncpaddr_setsa(struct ncpaddr *addr, const struct sockaddr *host)
342 const struct sockaddr_in *host4 = (const struct sockaddr_in *)host;
343 #ifndef NOINET6
344 const struct sockaddr_in6 *host6 = (const struct sockaddr_in6 *)host;
345 #endif
347 switch (host->sa_family) {
348 case AF_INET:
349 addr->ncpaddr_family = AF_INET;
350 addr->ncpaddr_ip4addr = host4->sin_addr;
351 break;
353 #ifndef NOINET6
354 case AF_INET6:
355 if (IN6_IS_ADDR_V4MAPPED(&host6->sin6_addr)) {
356 addr->ncpaddr_family = AF_INET;
357 addr->ncpaddr_ip4addr.s_addr =
358 *(const u_int32_t *)(host6->sin6_addr.s6_addr + 12);
359 } else {
360 addr->ncpaddr_family = AF_INET6;
361 addr->ncpaddr_ip6addr = host6->sin6_addr;
363 break;
364 #endif
366 default:
367 addr->ncpaddr_family = AF_UNSPEC;
371 static char *
372 ncpaddr_ntowa(const struct ncpaddr *addr)
374 static char res[NCP_ASCIIBUFFERSIZE];
375 #ifndef NOINET6
376 struct sockaddr_in6 sin6;
377 #endif
379 switch (addr->ncpaddr_family) {
380 case AF_INET:
381 snprintf(res, sizeof res, "%s", inet_ntoa(addr->ncpaddr_ip4addr));
382 return res;
384 #ifndef NOINET6
385 case AF_INET6:
386 memset(&sin6, '\0', sizeof(sin6));
387 sin6.sin6_len = sizeof(sin6);
388 sin6.sin6_family = AF_INET6;
389 sin6.sin6_addr = addr->ncpaddr_ip6addr;
390 adjust_linklocal(&sin6);
391 #ifdef NI_WITHSCOPEID
392 if (getnameinfo((struct sockaddr *)&sin6, sizeof sin6, res, sizeof(res),
393 NULL, 0, NI_WITHSCOPEID | NI_NUMERICHOST) != 0)
394 #else
395 if (getnameinfo((struct sockaddr *)&sin6, sizeof sin6, res, sizeof(res),
396 NULL, 0, NI_NUMERICHOST) != 0)
397 #endif
398 break;
400 return res;
401 #endif
404 snprintf(res, sizeof res, "<AF_UNSPEC>");
405 return res;
408 const char *
409 ncpaddr_ntoa(const struct ncpaddr *addr)
411 return ncpaddr_ntowa(addr);
416 ncpaddr_aton(struct ncpaddr *addr, struct ncp *ncp, const char *data)
418 struct ncprange range;
420 if (!ncprange_aton(&range, ncp, data))
421 return 0;
423 if (range.ncprange_family == AF_INET && range.ncprange_ip4width != 32) {
424 log_Printf(LogWARN, "ncpaddr_aton: %s: Only 32 bits allowed\n", data);
425 return 0;
428 #ifndef NOINET6
429 if (range.ncprange_family == AF_INET6 && range.ncprange_ip6width != 128) {
430 log_Printf(LogWARN, "ncpaddr_aton: %s: Only 128 bits allowed\n", data);
431 return 0;
433 #endif
435 switch (range.ncprange_family) {
436 case AF_INET:
437 addr->ncpaddr_family = range.ncprange_family;
438 addr->ncpaddr_ip4addr = range.ncprange_ip4addr;
439 return 1;
441 #ifndef NOINET6
442 case AF_INET6:
443 addr->ncpaddr_family = range.ncprange_family;
444 addr->ncpaddr_ip6addr = range.ncprange_ip6addr;
445 return 1;
446 #endif
449 return 0;
452 void
453 ncprange_init(struct ncprange *range)
455 range->ncprange_family = AF_UNSPEC;
459 ncprange_isset(const struct ncprange *range)
461 return range->ncprange_family != AF_UNSPEC;
465 ncprange_equal(const struct ncprange *range, const struct ncprange *cmp)
467 if (range->ncprange_family != cmp->ncprange_family)
468 return 0;
470 switch (range->ncprange_family) {
471 case AF_INET:
472 if (range->ncprange_ip4addr.s_addr != cmp->ncprange_ip4addr.s_addr)
473 return 0;
474 return range->ncprange_ip4mask.s_addr == cmp->ncprange_ip4mask.s_addr;
476 #ifndef NOINET6
477 case AF_INET6:
478 if (range->ncprange_ip6width != cmp->ncprange_ip6width)
479 return 0;
480 return !memcmp(&range->ncprange_ip6addr, &cmp->ncprange_ip6addr,
481 sizeof range->ncprange_ip6addr);
482 #endif
484 case AF_UNSPEC:
485 return 1;
488 return 0;
492 ncprange_isdefault(const struct ncprange *range)
494 switch (range->ncprange_family) {
495 case AF_INET:
496 if (range->ncprange_ip4addr.s_addr == INADDR_ANY)
497 return 1;
498 break;
500 #ifndef NOINET6
501 case AF_INET6:
502 if (range->ncprange_ip6width == 0 &&
503 IN6_IS_ADDR_UNSPECIFIED(&range->ncprange_ip6addr))
504 return 1;
505 break;
506 #endif
509 return 0;
512 void
513 ncprange_setdefault(struct ncprange *range, int af)
515 memset(range, '\0', sizeof *range);
516 range->ncprange_family = af;
520 ncprange_contains(const struct ncprange *range, const struct ncpaddr *addr)
522 #ifndef NOINET6
523 const u_char masks[] = { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
524 const u_char *addrp, *rangep;
525 int bits;
526 #endif
528 if (range->ncprange_family != addr->ncpaddr_family)
529 return 0;
531 switch (range->ncprange_family) {
532 case AF_INET:
533 return !((addr->ncpaddr_ip4addr.s_addr ^ range->ncprange_ip4addr.s_addr) &
534 range->ncprange_ip4mask.s_addr);
536 #ifndef NOINET6
537 case AF_INET6:
538 rangep = (const u_char *)range->ncprange_ip6addr.s6_addr;
539 addrp = (const u_char *)addr->ncpaddr_ip6addr.s6_addr;
541 for (bits = range->ncprange_ip6width; bits > 0; bits -= 8)
542 if ((*addrp++ ^ *rangep++) & masks[bits > 7 ? 7 : bits - 1])
543 return 0;
545 return 1;
546 #endif
549 return 0;
553 ncprange_containsip4(const struct ncprange *range, struct in_addr addr)
555 switch (range->ncprange_family) {
556 case AF_INET:
557 return !((addr.s_addr ^ range->ncprange_ip4addr.s_addr) &
558 range->ncprange_ip4mask.s_addr);
561 return 0;
564 void
565 ncprange_copy(struct ncprange *range, const struct ncprange *from)
567 switch (from->ncprange_family) {
568 case AF_INET:
569 range->ncprange_family = AF_INET;
570 range->ncprange_ip4addr = from->ncprange_ip4addr;
571 range->ncprange_ip4mask = from->ncprange_ip4mask;
572 range->ncprange_ip4width = from->ncprange_ip4width;
573 break;
575 #ifndef NOINET6
576 case AF_INET6:
577 range->ncprange_family = AF_INET6;
578 range->ncprange_ip6addr = from->ncprange_ip6addr;
579 range->ncprange_ip6width = from->ncprange_ip6width;
580 break;
581 #endif
583 default:
584 range->ncprange_family = AF_UNSPEC;
588 void
589 ncprange_set(struct ncprange *range, const struct ncpaddr *addr, int width)
591 ncprange_sethost(range, addr);
592 ncprange_setwidth(range, width);
595 void
596 ncprange_sethost(struct ncprange *range, const struct ncpaddr *from)
598 switch (from->ncpaddr_family) {
599 case AF_INET:
600 range->ncprange_family = AF_INET;
601 range->ncprange_ip4addr = from->ncpaddr_ip4addr;
602 if (from->ncpaddr_ip4addr.s_addr == INADDR_ANY) {
603 range->ncprange_ip4mask.s_addr = INADDR_ANY;
604 range->ncprange_ip4width = 0;
605 } else {
606 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
607 range->ncprange_ip4width = 32;
609 break;
611 #ifndef NOINET6
612 case AF_INET6:
613 range->ncprange_family = AF_INET6;
614 range->ncprange_ip6addr = from->ncpaddr_ip6addr;
615 range->ncprange_ip6width = 128;
616 break;
617 #endif
619 default:
620 range->ncprange_family = AF_UNSPEC;
625 ncprange_ishost(const struct ncprange *range)
627 switch (range->ncprange_family) {
628 case AF_INET:
629 return range->ncprange_ip4width == 32;
630 #ifndef NOINET6
631 case AF_INET6:
632 return range->ncprange_ip6width == 128;
633 #endif
636 return (0);
640 ncprange_setwidth(struct ncprange *range, int width)
642 switch (range->ncprange_family) {
643 case AF_INET:
644 if (width < 0 || width > 32)
645 break;
646 range->ncprange_ip4width = width;
647 range->ncprange_ip4mask = bits2mask4(width);
648 break;
650 #ifndef NOINET6
651 case AF_INET6:
652 if (width < 0 || width > 128)
653 break;
654 range->ncprange_ip6width = width;
655 break;
656 #endif
658 case AF_UNSPEC:
659 return 1;
662 return 0;
665 void
666 ncprange_setip4host(struct ncprange *range, struct in_addr from)
668 range->ncprange_family = AF_INET;
669 range->ncprange_ip4addr = from;
670 if (from.s_addr == INADDR_ANY) {
671 range->ncprange_ip4mask.s_addr = INADDR_ANY;
672 range->ncprange_ip4width = 0;
673 } else {
674 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
675 range->ncprange_ip4width = 32;
679 void
680 ncprange_setip4(struct ncprange *range, struct in_addr from, struct in_addr msk)
682 range->ncprange_family = AF_INET;
683 range->ncprange_ip4addr = from;
684 range->ncprange_ip4mask = msk;
685 range->ncprange_ip4width = mask42bits(msk);
690 ncprange_setip4mask(struct ncprange *range, struct in_addr mask)
692 if (range->ncprange_family != AF_INET)
693 return 0;
694 range->ncprange_ip4mask = mask;
695 range->ncprange_ip4width = mask42bits(mask);
696 return 1;
699 void
700 ncprange_setsa(struct ncprange *range, const struct sockaddr *host,
701 const struct sockaddr *mask)
703 const struct sockaddr_in *host4 = (const struct sockaddr_in *)host;
704 const struct sockaddr_in *mask4 = (const struct sockaddr_in *)mask;
705 #ifndef NOINET6
706 const struct sockaddr_in6 *host6 = (const struct sockaddr_in6 *)host;
707 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *)mask;
708 #endif
710 switch (host->sa_family) {
711 case AF_INET:
712 range->ncprange_family = AF_INET;
713 range->ncprange_ip4addr = host4->sin_addr;
714 if (host4->sin_addr.s_addr == INADDR_ANY) {
715 range->ncprange_ip4mask.s_addr = INADDR_ANY;
716 range->ncprange_ip4width = 0;
717 } else if (mask4 && mask4->sin_family == AF_INET) {
718 range->ncprange_ip4mask.s_addr = mask4->sin_addr.s_addr;
719 range->ncprange_ip4width = mask42bits(mask4->sin_addr);
720 } else {
721 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
722 range->ncprange_ip4width = 32;
724 break;
726 #ifndef NOINET6
727 case AF_INET6:
728 range->ncprange_family = AF_INET6;
729 range->ncprange_ip6addr = host6->sin6_addr;
730 if (IN6_IS_ADDR_UNSPECIFIED(&host6->sin6_addr))
731 range->ncprange_ip6width = 0;
732 else
733 range->ncprange_ip6width = mask6 ? mask62bits(&mask6->sin6_addr) : 128;
734 break;
735 #endif
737 default:
738 range->ncprange_family = AF_UNSPEC;
742 void
743 ncprange_getsa(const struct ncprange *range, struct sockaddr_storage *host,
744 struct sockaddr_storage *mask)
746 struct sockaddr_in *host4 = (struct sockaddr_in *)host;
747 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
748 #ifndef NOINET6
749 struct sockaddr_in6 *host6 = (struct sockaddr_in6 *)host;
750 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
751 #endif
753 memset(host, '\0', sizeof(*host));
754 if (mask)
755 memset(mask, '\0', sizeof(*mask));
757 switch (range->ncprange_family) {
758 case AF_INET:
759 host4->sin_family = AF_INET;
760 host4->sin_len = sizeof(*host4);
761 host4->sin_addr = range->ncprange_ip4addr;
762 if (mask4) {
763 mask4->sin_family = AF_INET;
764 mask4->sin_len = sizeof(*host4);
765 mask4->sin_addr = range->ncprange_ip4mask;
767 break;
769 #ifndef NOINET6
770 case AF_INET6:
771 host6->sin6_family = AF_INET6;
772 host6->sin6_len = sizeof(*host6);
773 host6->sin6_addr = range->ncprange_ip6addr;
774 if (mask6) {
775 mask6->sin6_family = AF_INET6;
776 mask6->sin6_len = sizeof(*host6);
777 mask6->sin6_addr = bits2mask6(range->ncprange_ip6width);
779 break;
780 #endif
782 default:
783 host->ss_family = AF_UNSPEC;
784 if (mask)
785 mask->ss_family = AF_UNSPEC;
786 break;
791 ncprange_getaddr(const struct ncprange *range, struct ncpaddr *addr)
793 switch (range->ncprange_family) {
794 case AF_INET:
795 addr->ncpaddr_family = AF_INET;
796 addr->ncpaddr_ip4addr = range->ncprange_ip4addr;
797 return 1;
798 #ifndef NOINET6
799 case AF_INET6:
800 addr->ncpaddr_family = AF_INET6;
801 addr->ncpaddr_ip6addr = range->ncprange_ip6addr;
802 return 1;
803 #endif
806 return 0;
810 ncprange_getip4addr(const struct ncprange *range, struct in_addr *addr)
812 if (range->ncprange_family != AF_INET)
813 return 0;
815 *addr = range->ncprange_ip4addr;
816 return 1;
820 ncprange_getip4mask(const struct ncprange *range, struct in_addr *mask)
822 switch (range->ncprange_family) {
823 case AF_INET:
824 *mask = range->ncprange_ip4mask;
825 return 1;
828 return 0;
832 ncprange_getwidth(const struct ncprange *range, int *width)
834 switch (range->ncprange_family) {
835 case AF_INET:
836 *width = range->ncprange_ip4width;
837 return 1;
838 #ifndef NOINET6
839 case AF_INET6:
840 *width = range->ncprange_ip6width;
841 return 1;
842 #endif
845 return 0;
848 const char *
849 ncprange_ntoa(const struct ncprange *range)
851 char *res;
852 struct ncpaddr addr;
853 int len;
855 if (!ncprange_getaddr(range, &addr))
856 return "<AF_UNSPEC>";
858 res = ncpaddr_ntowa(&addr);
859 len = strlen(res);
860 if (len >= NCP_ASCIIBUFFERSIZE - 1)
861 return res;
863 switch (range->ncprange_family) {
864 case AF_INET:
865 if (range->ncprange_ip4width == -1) {
866 /* A non-contiguous mask */
867 for (; len >= 3; res[len -= 2] = '\0')
868 if (strcmp(res + len - 2, ".0"))
869 break;
870 snprintf(res + len, strlen(res) - len, "&0x%08lx",
871 (unsigned long)ntohl(range->ncprange_ip4mask.s_addr));
872 } else if (range->ncprange_ip4width < 32)
873 snprintf(res + len, strlen(res) - len, "/%d", range->ncprange_ip4width);
875 return res;
877 #ifndef NOINET6
878 case AF_INET6:
879 if (range->ncprange_ip6width != 128)
880 snprintf(res + len, strlen(res) - len, "/%d", range->ncprange_ip6width);
882 return res;
883 #endif
886 return "<AF_UNSPEC>";
889 #ifndef NOINET6
891 ncprange_scopeid(const struct ncprange *range)
893 const struct in6_addr *sin6;
894 int scopeid = -1;
896 if (range->ncprange_family == AF_INET6) {
897 sin6 = &range->ncprange_ip6addr;
898 if (IN6_IS_ADDR_LINKLOCAL(sin6) || IN6_IS_ADDR_MC_LINKLOCAL(sin6))
899 if ((scopeid = ntohs(*(const u_short *)&sin6->s6_addr[2])) == 0)
900 scopeid = -1;
903 return scopeid;
905 #endif
908 ncprange_aton(struct ncprange *range, struct ncp *ncp, const char *data)
910 int bits, len;
911 char *wp;
912 const char *cp;
913 char *s;
915 len = strcspn(data, "/");
917 if (ncp && strncasecmp(data, "HISADDR", len) == 0) {
918 range->ncprange_family = AF_INET;
919 range->ncprange_ip4addr = ncp->ipcp.peer_ip;
920 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
921 range->ncprange_ip4width = 32;
922 return 1;
923 #ifndef NOINET6
924 } else if (ncp && strncasecmp(data, "HISADDR6", len) == 0) {
925 range->ncprange_family = AF_INET6;
926 range->ncprange_ip6addr = ncp->ipv6cp.hisaddr.ncpaddr_ip6addr;
927 range->ncprange_ip6width = 128;
928 return 1;
929 #endif
930 } else if (ncp && strncasecmp(data, "MYADDR", len) == 0) {
931 range->ncprange_family = AF_INET;
932 range->ncprange_ip4addr = ncp->ipcp.my_ip;
933 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
934 range->ncprange_ip4width = 32;
935 return 1;
936 #ifndef NOINET6
937 } else if (ncp && strncasecmp(data, "MYADDR6", len) == 0) {
938 range->ncprange_family = AF_INET6;
939 range->ncprange_ip6addr = ncp->ipv6cp.myaddr.ncpaddr_ip6addr;
940 range->ncprange_ip6width = 128;
941 return 1;
942 #endif
943 } else if (ncp && strncasecmp(data, "DNS0", len) == 0) {
944 range->ncprange_family = AF_INET;
945 range->ncprange_ip4addr = ncp->ipcp.ns.dns[0];
946 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
947 range->ncprange_ip4width = 32;
948 return 1;
949 } else if (ncp && strncasecmp(data, "DNS1", len) == 0) {
950 range->ncprange_family = AF_INET;
951 range->ncprange_ip4addr = ncp->ipcp.ns.dns[1];
952 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
953 range->ncprange_ip4width = 32;
954 return 1;
957 s = (char *)alloca(len + 1);
958 strncpy(s, data, len);
959 s[len] = '\0';
960 bits = -1;
962 if (data[len] != '\0') {
963 bits = strtol(data + len + 1, &wp, 0);
964 if (*wp || wp == data + len + 1 || bits < 0 || bits > 128) {
965 log_Printf(LogWARN, "ncprange_aton: bad mask width.\n");
966 return 0;
970 if ((cp = strchr(data, ':')) == NULL) {
971 range->ncprange_family = AF_INET;
973 range->ncprange_ip4addr = GetIpAddr(s);
975 if (range->ncprange_ip4addr.s_addr == INADDR_NONE) {
976 log_Printf(LogWARN, "ncprange_aton: %s: Bad address\n", s);
977 return 0;
980 if (range->ncprange_ip4addr.s_addr == INADDR_ANY) {
981 range->ncprange_ip4mask.s_addr = INADDR_ANY;
982 range->ncprange_ip4width = 0;
983 } else if (bits == -1) {
984 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
985 range->ncprange_ip4width = 32;
986 } else if (bits > 32) {
987 log_Printf(LogWARN, "ncprange_aton: bad mask width.\n");
988 return 0;
989 } else {
990 range->ncprange_ip4mask = bits2mask4(bits);
991 range->ncprange_ip4width = bits;
994 return 1;
995 #ifndef NOINET6
996 } else if (strchr(cp + 1, ':') != NULL) {
997 range->ncprange_family = AF_INET6;
999 if (inet_pton(AF_INET6, s, &range->ncprange_ip6addr) != 1) {
1000 log_Printf(LogWARN, "ncprange_aton: %s: Bad address\n", s);
1001 return 0;
1004 if (IN6_IS_ADDR_UNSPECIFIED(&range->ncprange_ip6addr))
1005 range->ncprange_ip6width = 0;
1006 else
1007 range->ncprange_ip6width = (bits == -1) ? 128 : bits;
1008 return 1;
1009 #endif
1012 return 0;