2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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>
34 #include <net/if_types.h>
35 #include <net/route.h>
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 #include <arpa/inet.h>
54 #include "slcompress.h"
56 #include "throughput.h"
59 #include "descriptor.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
76 #define ncprange_ip6addr u.ip6.ipaddr
77 #define ncprange_ip6width u.ip6.width
78 #define ncpaddr_ip6addr u.ip6addr
81 #define NCP_ASCIIBUFFERSIZE 52
86 struct in_addr result
;
87 u_int32_t bit
= 0x80000000;
97 result
.s_addr
= htonl(result
.s_addr
);
102 mask42bits(struct in_addr mask
)
104 u_int32_t msk
= ntohl(mask
.s_addr
);
108 for (ret
= 32, tst
= 1; tst
; ret
--, tst
<<= 1)
112 for (tst
<<= 1; tst
; tst
<<= 1)
116 return tst
? -1 : ret
;
120 static struct in6_addr
123 struct in6_addr result
;
124 u_int32_t bit
= 0x80;
125 u_char
*c
= result
.s6_addr
;
127 memset(&result
, '\0', sizeof result
);
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
;
149 p
= (const u_char
*)mask
;
150 for (masklen
= 0, end
= p
+ 16; p
< end
&& *p
== 0xff; p
++)
154 for (c
= masks
; c
< masks
+ sizeof masks
; c
++)
156 masklen
+= c
- masks
;
165 adjust_linklocal(struct sockaddr_in6
*sin6
)
167 /* XXX: ?????!?!?!!!!! This is horrible ! */
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;
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
) {
201 if (addr
->ncpaddr_ip4addr
.s_addr
== INADDR_ANY
)
207 if (IN6_IS_ADDR_UNSPECIFIED(&addr
->ncpaddr_ip6addr
))
217 ncpaddr_equal(const struct ncpaddr
*addr
, const struct ncpaddr
*cmp
)
219 if (addr
->ncpaddr_family
!= cmp
->ncpaddr_family
)
222 switch (addr
->ncpaddr_family
) {
224 return addr
->ncpaddr_ip4addr
.s_addr
== cmp
->ncpaddr_ip4addr
.s_addr
;
228 return !memcmp(&addr
->ncpaddr_ip6addr
, &cmp
->ncpaddr_ip6addr
,
229 sizeof addr
->ncpaddr_ip6addr
);
240 ncpaddr_copy(struct ncpaddr
*addr
, const struct ncpaddr
*from
)
242 switch (from
->ncpaddr_family
) {
244 addr
->ncpaddr_family
= AF_INET
;
245 addr
->ncpaddr_ip4addr
= from
->ncpaddr_ip4addr
;
249 addr
->ncpaddr_family
= AF_INET6
;
250 addr
->ncpaddr_ip6addr
= from
->ncpaddr_ip6addr
;
254 addr
->ncpaddr_family
= AF_UNSPEC
;
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
)
270 *ip
= addr
->ncpaddr_ip4addr
.s_addr
;
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
)
286 *ip
= addr
->ncpaddr_ip4addr
;
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
)
303 *ip6
= addr
->ncpaddr_ip6addr
;
309 ncpaddr_getsa(const struct ncpaddr
*addr
, struct sockaddr_storage
*host
)
311 struct sockaddr_in
*host4
= (struct sockaddr_in
*)host
;
313 struct sockaddr_in6
*host6
= (struct sockaddr_in6
*)host
;
316 memset(host
, '\0', sizeof(*host
));
318 switch (addr
->ncpaddr_family
) {
320 host4
->sin_family
= AF_INET
;
321 host4
->sin_len
= sizeof(*host4
);
322 host4
->sin_addr
= addr
->ncpaddr_ip4addr
;
327 host6
->sin6_family
= AF_INET6
;
328 host6
->sin6_len
= sizeof(*host6
);
329 host6
->sin6_addr
= addr
->ncpaddr_ip6addr
;
334 host
->ss_family
= AF_UNSPEC
;
340 ncpaddr_setsa(struct ncpaddr
*addr
, const struct sockaddr
*host
)
342 const struct sockaddr_in
*host4
= (const struct sockaddr_in
*)host
;
344 const struct sockaddr_in6
*host6
= (const struct sockaddr_in6
*)host
;
347 switch (host
->sa_family
) {
349 addr
->ncpaddr_family
= AF_INET
;
350 addr
->ncpaddr_ip4addr
= host4
->sin_addr
;
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);
360 addr
->ncpaddr_family
= AF_INET6
;
361 addr
->ncpaddr_ip6addr
= host6
->sin6_addr
;
367 addr
->ncpaddr_family
= AF_UNSPEC
;
372 ncpaddr_ntowa(const struct ncpaddr
*addr
)
374 static char res
[NCP_ASCIIBUFFERSIZE
];
376 struct sockaddr_in6 sin6
;
379 switch (addr
->ncpaddr_family
) {
381 snprintf(res
, sizeof res
, "%s", inet_ntoa(addr
->ncpaddr_ip4addr
));
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)
395 if (getnameinfo((struct sockaddr
*)&sin6
, sizeof sin6
, res
, sizeof(res
),
396 NULL
, 0, NI_NUMERICHOST
) != 0)
404 snprintf(res
, sizeof res
, "<AF_UNSPEC>");
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
))
423 if (range
.ncprange_family
== AF_INET
&& range
.ncprange_ip4width
!= 32) {
424 log_Printf(LogWARN
, "ncpaddr_aton: %s: Only 32 bits allowed\n", data
);
429 if (range
.ncprange_family
== AF_INET6
&& range
.ncprange_ip6width
!= 128) {
430 log_Printf(LogWARN
, "ncpaddr_aton: %s: Only 128 bits allowed\n", data
);
435 switch (range
.ncprange_family
) {
437 addr
->ncpaddr_family
= range
.ncprange_family
;
438 addr
->ncpaddr_ip4addr
= range
.ncprange_ip4addr
;
443 addr
->ncpaddr_family
= range
.ncprange_family
;
444 addr
->ncpaddr_ip6addr
= range
.ncprange_ip6addr
;
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
)
470 switch (range
->ncprange_family
) {
472 if (range
->ncprange_ip4addr
.s_addr
!= cmp
->ncprange_ip4addr
.s_addr
)
474 return range
->ncprange_ip4mask
.s_addr
== cmp
->ncprange_ip4mask
.s_addr
;
478 if (range
->ncprange_ip6width
!= cmp
->ncprange_ip6width
)
480 return !memcmp(&range
->ncprange_ip6addr
, &cmp
->ncprange_ip6addr
,
481 sizeof range
->ncprange_ip6addr
);
492 ncprange_isdefault(const struct ncprange
*range
)
494 switch (range
->ncprange_family
) {
496 if (range
->ncprange_ip4addr
.s_addr
== INADDR_ANY
)
502 if (range
->ncprange_ip6width
== 0 &&
503 IN6_IS_ADDR_UNSPECIFIED(&range
->ncprange_ip6addr
))
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
)
523 const u_char masks
[] = { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
524 const u_char
*addrp
, *rangep
;
528 if (range
->ncprange_family
!= addr
->ncpaddr_family
)
531 switch (range
->ncprange_family
) {
533 return !((addr
->ncpaddr_ip4addr
.s_addr
^ range
->ncprange_ip4addr
.s_addr
) &
534 range
->ncprange_ip4mask
.s_addr
);
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])
553 ncprange_containsip4(const struct ncprange
*range
, struct in_addr addr
)
555 switch (range
->ncprange_family
) {
557 return !((addr
.s_addr
^ range
->ncprange_ip4addr
.s_addr
) &
558 range
->ncprange_ip4mask
.s_addr
);
565 ncprange_copy(struct ncprange
*range
, const struct ncprange
*from
)
567 switch (from
->ncprange_family
) {
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
;
577 range
->ncprange_family
= AF_INET6
;
578 range
->ncprange_ip6addr
= from
->ncprange_ip6addr
;
579 range
->ncprange_ip6width
= from
->ncprange_ip6width
;
584 range
->ncprange_family
= AF_UNSPEC
;
589 ncprange_set(struct ncprange
*range
, const struct ncpaddr
*addr
, int width
)
591 ncprange_sethost(range
, addr
);
592 ncprange_setwidth(range
, width
);
596 ncprange_sethost(struct ncprange
*range
, const struct ncpaddr
*from
)
598 switch (from
->ncpaddr_family
) {
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;
606 range
->ncprange_ip4mask
.s_addr
= INADDR_BROADCAST
;
607 range
->ncprange_ip4width
= 32;
613 range
->ncprange_family
= AF_INET6
;
614 range
->ncprange_ip6addr
= from
->ncpaddr_ip6addr
;
615 range
->ncprange_ip6width
= 128;
620 range
->ncprange_family
= AF_UNSPEC
;
625 ncprange_ishost(const struct ncprange
*range
)
627 switch (range
->ncprange_family
) {
629 return range
->ncprange_ip4width
== 32;
632 return range
->ncprange_ip6width
== 128;
640 ncprange_setwidth(struct ncprange
*range
, int width
)
642 switch (range
->ncprange_family
) {
644 if (width
< 0 || width
> 32)
646 range
->ncprange_ip4width
= width
;
647 range
->ncprange_ip4mask
= bits2mask4(width
);
652 if (width
< 0 || width
> 128)
654 range
->ncprange_ip6width
= width
;
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;
674 range
->ncprange_ip4mask
.s_addr
= INADDR_BROADCAST
;
675 range
->ncprange_ip4width
= 32;
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
)
694 range
->ncprange_ip4mask
= mask
;
695 range
->ncprange_ip4width
= mask42bits(mask
);
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
;
706 const struct sockaddr_in6
*host6
= (const struct sockaddr_in6
*)host
;
707 const struct sockaddr_in6
*mask6
= (const struct sockaddr_in6
*)mask
;
710 switch (host
->sa_family
) {
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
);
721 range
->ncprange_ip4mask
.s_addr
= INADDR_BROADCAST
;
722 range
->ncprange_ip4width
= 32;
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;
733 range
->ncprange_ip6width
= mask6
? mask62bits(&mask6
->sin6_addr
) : 128;
738 range
->ncprange_family
= AF_UNSPEC
;
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
;
749 struct sockaddr_in6
*host6
= (struct sockaddr_in6
*)host
;
750 struct sockaddr_in6
*mask6
= (struct sockaddr_in6
*)mask
;
753 memset(host
, '\0', sizeof(*host
));
755 memset(mask
, '\0', sizeof(*mask
));
757 switch (range
->ncprange_family
) {
759 host4
->sin_family
= AF_INET
;
760 host4
->sin_len
= sizeof(*host4
);
761 host4
->sin_addr
= range
->ncprange_ip4addr
;
763 mask4
->sin_family
= AF_INET
;
764 mask4
->sin_len
= sizeof(*host4
);
765 mask4
->sin_addr
= range
->ncprange_ip4mask
;
771 host6
->sin6_family
= AF_INET6
;
772 host6
->sin6_len
= sizeof(*host6
);
773 host6
->sin6_addr
= range
->ncprange_ip6addr
;
775 mask6
->sin6_family
= AF_INET6
;
776 mask6
->sin6_len
= sizeof(*host6
);
777 mask6
->sin6_addr
= bits2mask6(range
->ncprange_ip6width
);
783 host
->ss_family
= AF_UNSPEC
;
785 mask
->ss_family
= AF_UNSPEC
;
791 ncprange_getaddr(const struct ncprange
*range
, struct ncpaddr
*addr
)
793 switch (range
->ncprange_family
) {
795 addr
->ncpaddr_family
= AF_INET
;
796 addr
->ncpaddr_ip4addr
= range
->ncprange_ip4addr
;
800 addr
->ncpaddr_family
= AF_INET6
;
801 addr
->ncpaddr_ip6addr
= range
->ncprange_ip6addr
;
810 ncprange_getip4addr(const struct ncprange
*range
, struct in_addr
*addr
)
812 if (range
->ncprange_family
!= AF_INET
)
815 *addr
= range
->ncprange_ip4addr
;
820 ncprange_getip4mask(const struct ncprange
*range
, struct in_addr
*mask
)
822 switch (range
->ncprange_family
) {
824 *mask
= range
->ncprange_ip4mask
;
832 ncprange_getwidth(const struct ncprange
*range
, int *width
)
834 switch (range
->ncprange_family
) {
836 *width
= range
->ncprange_ip4width
;
840 *width
= range
->ncprange_ip6width
;
849 ncprange_ntoa(const struct ncprange
*range
)
855 if (!ncprange_getaddr(range
, &addr
))
856 return "<AF_UNSPEC>";
858 res
= ncpaddr_ntowa(&addr
);
860 if (len
>= NCP_ASCIIBUFFERSIZE
- 1)
863 switch (range
->ncprange_family
) {
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"))
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
);
879 if (range
->ncprange_ip6width
!= 128)
880 snprintf(res
+ len
, strlen(res
) - len
, "/%d", range
->ncprange_ip6width
);
886 return "<AF_UNSPEC>";
891 ncprange_scopeid(const struct ncprange
*range
)
893 const struct in6_addr
*sin6
;
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)
908 ncprange_aton(struct ncprange
*range
, struct ncp
*ncp
, const char *data
)
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;
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;
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;
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;
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;
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;
957 s
= (char *)alloca(len
+ 1);
958 strncpy(s
, data
, len
);
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");
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
);
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");
990 range
->ncprange_ip4mask
= bits2mask4(bits
);
991 range
->ncprange_ip4width
= bits
;
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
);
1004 if (IN6_IS_ADDR_UNSPECIFIED(&range
->ncprange_ip6addr
))
1005 range
->ncprange_ip6width
= 0;
1007 range
->ncprange_ip6width
= (bits
== -1) ? 128 : bits
;