1 /* $NetBSD: util.c,v 1.12 2009/08/07 18:53:37 dyoung Exp $ */
4 * Copyright (c) 2008 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: util.c,v 1.12 2009/08/07 18:53:37 dyoung Exp $");
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
49 #include <sys/ioctl.h>
51 #include <net/if_dl.h>
52 #include <netinet/in.h> /* XXX */
61 static int oaf
= -1, s
;
63 if (oaf
== naf
|| (oaf
!= -1 && naf
== AF_UNSPEC
))
72 s
= socket(naf
, SOCK_DGRAM
, 0);
81 get_string(const char *val
, const char *sep
, u_int8_t
*buf
, int *lenp
)
89 hexstr
= (val
[0] == '0' && tolower((u_char
)val
[1]) == 'x');
95 if (sep
!= NULL
&& strchr(sep
, *val
) != NULL
) {
100 if (!isxdigit((u_char
)val
[0]) ||
101 !isxdigit((u_char
)val
[1])) {
102 warnx("bad hexadecimal digits");
108 warnx("hexadecimal digits too long");
110 warnx("strings too long");
114 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
115 *p
++ = (tohex((u_char
)val
[0]) << 4) |
116 tohex((u_char
)val
[1]);
124 memset(p
, 0, *lenp
- len
);
130 print_string(const u_int8_t
*buf
, int len
)
137 if (len
< 2 || buf
[0] != '0' || tolower(buf
[1]) != 'x') {
138 for (; i
< len
; i
++) {
139 if (!isprint(buf
[i
]))
146 if (hasspc
|| len
== 0)
147 printf("\"%.*s\"", len
, buf
);
149 printf("%.*s", len
, buf
);
152 for (i
= 0; i
< len
; i
++)
153 printf("%02x", buf
[i
]);
157 struct paddr_prefix
*
158 prefixlen_to_mask(int af
, int plen
)
162 struct sockaddr_in sin
;
163 struct sockaddr_in6 sin6
;
165 struct paddr_prefix
*pfx
;
170 memset(&u
, 0, sizeof(u
));
174 addrlen
= sizeof(u
.sin
.sin_addr
);
175 addr
= (uint8_t *)&u
.sin
.sin_addr
;
176 u
.sa
.sa_len
= sizeof(u
.sin
);
179 addrlen
= sizeof(u
.sin6
.sin6_addr
);
180 addr
= (uint8_t *)&u
.sin6
.sin6_addr
;
181 u
.sa
.sa_len
= sizeof(u
.sin6
);
189 if (plen
< 0 || (size_t)plen
> addrlen
* NBBY
) {
195 plen
= addrlen
* NBBY
;
197 memset(addr
, 0xff, (plen
+ NBBY
- 1) / NBBY
);
201 addr
[plen
/ NBBY
] &= ~((uint8_t)0xff >> nbit
);
202 pfx
= malloc(offsetof(struct paddr_prefix
, pfx_addr
) + u
.sa
.sa_len
);
206 memcpy(&pfx
->pfx_addr
, &u
.sa
, u
.sa
.sa_len
);
212 direct_ioctl(prop_dictionary_t env
, unsigned long cmd
, void *data
)
217 if ((s
= getsock(AF_UNSPEC
)) == -1)
218 err(EXIT_FAILURE
, "getsock");
220 if ((ifname
= getifname(env
)) == NULL
)
221 err(EXIT_FAILURE
, "getifname");
223 estrlcpy(data
, ifname
, IFNAMSIZ
);
225 return ioctl(s
, cmd
, data
);
229 indirect_ioctl(prop_dictionary_t env
, unsigned long cmd
, void *data
)
233 memset(&ifr
, 0, sizeof(ifr
));
237 return direct_ioctl(env
, cmd
, &ifr
);
241 print_link_addresses(prop_dictionary_t env
, bool print_active_only
)
243 char hbuf
[NI_MAXHOST
];
246 struct ifaddrs
*ifa
, *ifap
;
247 const struct sockaddr_dl
*sdl
;
248 struct if_laddrreq iflr
;
250 if ((ifname
= getifname(env
)) == NULL
)
251 err(EXIT_FAILURE
, "%s: getifname", __func__
);
253 if ((s
= getsock(AF_LINK
)) == -1)
254 err(EXIT_FAILURE
, "%s: getsock", __func__
);
256 if (getifaddrs(&ifap
) == -1)
257 err(EXIT_FAILURE
, "%s: getifaddrs", __func__
);
259 memset(&iflr
, 0, sizeof(iflr
));
261 strlcpy(iflr
.iflr_name
, ifname
, sizeof(iflr
.iflr_name
));
263 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
264 if (strcmp(ifname
, ifa
->ifa_name
) != 0)
266 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
269 sdl
= satocsdl(ifa
->ifa_addr
);
271 memcpy(&iflr
.addr
, ifa
->ifa_addr
, MIN(ifa
->ifa_addr
->sa_len
,
273 iflr
.flags
= IFLR_PREFIX
;
274 iflr
.prefixlen
= sdl
->sdl_alen
* NBBY
;
276 if (ioctl(s
, SIOCGLIFADDR
, &iflr
) == -1)
277 err(EXIT_FAILURE
, "%s: ioctl", __func__
);
279 if (((iflr
.flags
& IFLR_ACTIVE
) != 0) != print_active_only
)
282 if (getnameinfo(ifa
->ifa_addr
, ifa
->ifa_addr
->sa_len
,
283 hbuf
, sizeof(hbuf
), NULL
, 0,
284 Nflag
? 0 : NI_NUMERICHOST
) == 0 &&
287 print_active_only
? "address:" : "link", hbuf
);
294 ifa_get_preference(const char *ifname
, const struct sockaddr
*sa
)
296 struct if_addrprefreq ifap
;
299 if ((s
= getsock(sa
->sa_family
)) == -1) {
300 if (errno
== EPROTONOSUPPORT
)
302 err(EXIT_FAILURE
, "socket");
304 memset(&ifap
, 0, sizeof(ifap
));
305 estrlcpy(ifap
.ifap_name
, ifname
, sizeof(ifap
.ifap_name
));
306 memcpy(&ifap
.ifap_addr
, sa
, MIN(sizeof(ifap
.ifap_addr
), sa
->sa_len
));
307 if (ioctl(s
, SIOCGIFADDRPREF
, &ifap
) == -1) {
308 if (errno
== EADDRNOTAVAIL
|| errno
== EAFNOSUPPORT
)
310 warn("SIOCGIFADDRPREF");
312 return ifap
.ifap_preference
;
316 ifa_print_preference(const char *ifname
, const struct sockaddr
*sa
)
323 preference
= ifa_get_preference(ifname
, sa
);
324 printf(" preference %" PRId16
, preference
);
328 ifa_any_preferences(const char *ifname
, struct ifaddrs
*ifap
, int family
)
332 /* Print address preference numbers if any address has a non-zero
333 * preference assigned.
335 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
336 if (strcmp(ifname
, ifa
->ifa_name
) != 0)
338 if (ifa
->ifa_addr
->sa_family
!= family
)
340 if (ifa_get_preference(ifa
->ifa_name
, ifa
->ifa_addr
) != 0)
348 /* KAME idiosyncrasy */
350 in6_fillscopeid(struct sockaddr_in6
*sin6
)
352 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
353 sin6
->sin6_scope_id
=
354 ntohs(*(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2]);
355 sin6
->sin6_addr
.s6_addr
[2] = sin6
->sin6_addr
.s6_addr
[3] = 0;