diff options
author | 2009-01-30 16:21:58 +0000 | |
---|---|---|
committer | 2009-01-30 16:21:58 +0000 | |
commit | e4156b191770c0ec1ef35ccfd3a0b1d138501ebf (patch) | |
tree | 586f48fe1a57dd0a17dfef6eda35bf3561b24002 | |
parent | If the "peer" address is not specified or derived from "to" for (diff) | |
download | wireguard-openbsd-e4156b191770c0ec1ef35ccfd3a0b1d138501ebf.tar.xz wireguard-openbsd-e4156b191770c0ec1ef35ccfd3a0b1d138501ebf.zip |
Use if_nametosdl implementation from rtadvd, which is much nicer.
From Mischa Diehm.
"looks fine to me" pyr@
-rw-r--r-- | usr.sbin/rtsold/if.c | 68 |
1 files changed, 21 insertions, 47 deletions
diff --git a/usr.sbin/rtsold/if.c b/usr.sbin/rtsold/if.c index 264d058ff9b..e6662086b4a 100644 --- a/usr.sbin/rtsold/if.c +++ b/usr.sbin/rtsold/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.20 2008/06/09 21:53:57 reyk Exp $ */ +/* $OpenBSD: if.c,v 1.21 2009/01/30 16:21:58 rainer Exp $ */ /* $KAME: if.c,v 1.18 2002/05/31 10:10:03 itojun Exp $ */ /* @@ -57,6 +57,7 @@ #include <errno.h> #include <limits.h> #include <ifaddrs.h> +#include <netdb.h> #include "rtsold.h" @@ -235,56 +236,29 @@ lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt) struct sockaddr_dl * if_nametosdl(char *name) { - int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0}; - char *buf, *next, *lim; - size_t len; - struct if_msghdr *ifm; - struct sockaddr *sa, *rti_info[RTAX_MAX]; - struct sockaddr_dl *sdl = NULL, *ret_sdl; - - if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) - return(NULL); - if ((buf = malloc(len)) == NULL) - return(NULL); - if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { - free(buf); - return(NULL); - } + struct ifaddrs *ifap, *ifa; + struct sockaddr_dl *sdl; - lim = buf + len; - for (next = buf; next < lim; next += ifm->ifm_msglen) { - ifm = (struct if_msghdr *)next; - if (ifm->ifm_type == RTM_IFINFO) { - sa = (struct sockaddr *)(ifm + 1); - get_rtaddrs(ifm->ifm_addrs, sa, rti_info); - if ((sa = rti_info[RTAX_IFP]) != NULL) { - if (sa->sa_family == AF_LINK) { - sdl = (struct sockaddr_dl *)sa; - if (strlen(name) != sdl->sdl_nlen) - continue; /* not same len */ - if (strncmp(&sdl->sdl_data[0], - name, - sdl->sdl_nlen) == 0) { - break; - } - } - } - } - } - if (next == lim) { - /* search failed */ - free(buf); - return(NULL); - } + if (getifaddrs(&ifap) != 0) + return (NULL); + + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (strcmp(ifa->ifa_name, name) != 0) + continue; + if (ifa->ifa_addr->sa_family != AF_LINK) + continue; - if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) { - free(buf); - return(NULL); + sdl = malloc(ifa->ifa_addr->sa_len); + if (!sdl) + continue; /*XXX*/ + + memcpy(sdl, ifa->ifa_addr, ifa->ifa_addr->sa_len); + freeifaddrs(ifap); + return (sdl); } - memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len); - free(buf); - return(ret_sdl); + freeifaddrs(ifap); + return (NULL); } int |