From: Roy Marples Date: Fri, 1 May 2015 12:34:07 +0000 (+0000) Subject: Move address learning into a separate function for future use. X-Git-Tag: v6.8.2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c37a324e88ba54650abef7aea0e02795b978c80f;p=thirdparty%2Fdhcpcd.git Move address learning into a separate function for future use. --- diff --git a/if.c b/if.c index 308b7f9f..2f1a7e57 100644 --- a/if.c +++ b/if.c @@ -160,6 +160,67 @@ if_hasconf(struct dhcpcd_ctx *ctx, const char *ifname) return 0; } +static void if_learnaddrs1(struct dhcpcd_ctx *ctx, struct if_head *ifs, + struct ifaddrs *ifaddrs) +{ + struct ifaddrs *ifa; + struct interface *ifp; +#ifdef INET + const struct sockaddr_in *addr, *net, *dst; +#endif +#ifdef INET6 + struct sockaddr_in6 *sin6, *net6; +#endif + int ifa_flags; + + + for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL) + continue; + if ((ifp = if_find(ifs, ifa->ifa_name)) == NULL) + continue; + switch(ifa->ifa_addr->sa_family) { +#ifdef INET + case AF_INET: + addr = (const struct sockaddr_in *) + (void *)ifa->ifa_addr; + net = (const struct sockaddr_in *) + (void *)ifa->ifa_netmask; + if (ifa->ifa_flags & IFF_POINTOPOINT) + dst = (const struct sockaddr_in *) + (void *)ifa->ifa_dstaddr; + else + dst = NULL; + ifa_flags = if_addrflags(&addr->sin_addr, ifp); + ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name, + &addr->sin_addr, + &net->sin_addr, + dst ? &dst->sin_addr : NULL, ifa_flags); + break; +#endif +#ifdef INET6 + case AF_INET6: + sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr; + net6 = (struct sockaddr_in6 *)(void *)ifa->ifa_netmask; +#ifdef __KAME__ + if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) + /* Remove the scope from the address */ + sin6->sin6_addr.s6_addr[2] = + sin6->sin6_addr.s6_addr[3] = '\0'; +#endif + ifa_flags = if_addrflags6(&sin6->sin6_addr, ifp); + if (ifa_flags != -1) + ipv6_handleifa(ctx, RTM_NEWADDR, ifs, + ifa->ifa_name, + &sin6->sin6_addr, + ipv6_prefixlen(&net6->sin6_addr), + ifa_flags); + break; +#endif + } + } +} + struct if_head * if_discover(struct dhcpcd_ctx *ctx, int argc, char * const *argv) { @@ -171,15 +232,6 @@ if_discover(struct dhcpcd_ctx *ctx, int argc, char * const *argv) #ifdef __linux__ char ifn[IF_NAMESIZE]; #endif -#ifdef INET - const struct sockaddr_in *addr; - const struct sockaddr_in *net; - const struct sockaddr_in *dst; -#endif -#ifdef INET6 - struct sockaddr_in6 *sin6, *net6; - int ifa_flags; -#endif #ifdef AF_LINK const struct sockaddr_dl *sdl; #ifdef SIOCGIFPRIORITY @@ -475,52 +527,7 @@ if_discover(struct dhcpcd_ctx *ctx, int argc, char * const *argv) TAILQ_INSERT_TAIL(ifs, ifp, next); } - for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) - continue; - if ((ifp = if_find(ifs, ifa->ifa_name)) == NULL) - continue; - switch(ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - addr = (const struct sockaddr_in *) - (void *)ifa->ifa_addr; - net = (const struct sockaddr_in *) - (void *)ifa->ifa_netmask; - if (ifa->ifa_flags & IFF_POINTOPOINT) - dst = (const struct sockaddr_in *) - (void *)ifa->ifa_dstaddr; - else - dst = NULL; - ifa_flags = if_addrflags(&addr->sin_addr, ifp); - ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name, - &addr->sin_addr, - &net->sin_addr, - dst ? &dst->sin_addr : NULL, ifa_flags); - break; -#endif -#ifdef INET6 - case AF_INET6: - sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr; - net6 = (struct sockaddr_in6 *)(void *)ifa->ifa_netmask; -#ifdef __KAME__ - if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) - /* Remove the scope from the address */ - sin6->sin6_addr.s6_addr[2] = - sin6->sin6_addr.s6_addr[3] = '\0'; -#endif - ifa_flags = if_addrflags6(&sin6->sin6_addr, ifp); - if (ifa_flags != -1) - ipv6_handleifa(ctx, RTM_NEWADDR, ifs, - ifa->ifa_name, - &sin6->sin6_addr, - ipv6_prefixlen(&net6->sin6_addr), - ifa_flags); - break; -#endif - } - } - + if_learnaddrs1(ctx, ifs, ifaddrs); freeifaddrs(ifaddrs); #ifdef SIOCGIFPRIORITY