From 3ed34783981881856b14dcd3e98246495dc6e419 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 13 Mar 2014 10:42:07 +0100 Subject: [PATCH] miniupnpd/getifaddr.c: clean up and fix getifaddr_in6() --- miniupnpd/getifaddr.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/miniupnpd/getifaddr.c b/miniupnpd/getifaddr.c index 3bdc281..f3459a5 100644 --- a/miniupnpd/getifaddr.c +++ b/miniupnpd/getifaddr.c @@ -121,9 +121,13 @@ getifaddr(const char * ifname, char * buf, int len, #ifdef ENABLE_PCP /* XXX I don't know if this function should return * IPv4 or IPv6 if both are enabled... */ -int getifaddr_in6(const char * ifname, struct in6_addr * addr){ +int getifaddr_in6(const char * ifname, struct in6_addr * addr) +{ struct ifaddrs * ifap; struct ifaddrs * ife; +#ifdef ENABLE_IPV6 + const struct sockaddr_in6 * tmpaddr; +#endif /* ENABLE_IPV6 */ int found = 0; if(!ifname || ifname[0]=='\0') @@ -143,27 +147,25 @@ int getifaddr_in6(const char * ifname, struct in6_addr * addr){ switch(ife->ifa_addr->sa_family) { case AF_INET: -#if 0 - addr->s6_addr32[0]=0; - addr->s6_addr32[1]=0; - addr->s6_addr32[2]=htonl(0xffff); - addr->s6_addr32[3]=((struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr; -#endif + /* IPv4-mapped IPv6 address ::ffff:1.2.3.4 */ memset(addr->s6_addr, 0, 10); addr->s6_addr[10] = 0xff; addr->s6_addr[11] = 0xff; - memcpy(addr->s6_addr + 12, &(((struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr), 4); - /*inet_ntop(ife->ifa_addr->sa_family, - &((struct sockaddr_in *)ife->ifa_addr)->sin_addr, - buf, len);*/ + memcpy(addr->s6_addr + 12, + &(((struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr), + 4); found = 1; break; #ifdef ENABLE_IPV6 case AF_INET6: - if(!IN6_IS_ADDR_LOOPBACK(addr) - && !IN6_IS_ADDR_LINKLOCAL(addr)) { - memcpy(addr->s6_addr, &((struct sockaddr_in6 *)ife->ifa_addr)->sin6_addr, 16); + tmpaddr = (const struct sockaddr_in6 *)ife->ifa_addr; + if(!IN6_IS_ADDR_LOOPBACK(&tmpaddr->sin6_addr) + && !IN6_IS_ADDR_LINKLOCAL(&tmpaddr->sin6_addr)) + { + memcpy(addr->s6_addr, + &tmpaddr->sin6_addr, + 16); found = 1; } break;