From 3d57f7801acb7a36ff7a68c98f044c247636c556 Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Wed, 9 Dec 2015 12:41:42 -0500 Subject: [PATCH] Fix up the IPv6 detection routine so it matches what ifconfig displays. --- src/library/NetDevice.cpp | 38 +++++++++++++++++++++++++------------ src/library/sysadm-global.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/library/NetDevice.cpp b/src/library/NetDevice.cpp index 6511bf8..f82ee0d 100644 --- a/src/library/NetDevice.cpp +++ b/src/library/NetDevice.cpp @@ -66,20 +66,34 @@ QString NetDevice::ipAsString(){ //Fetch the IPv6 and return it as a QString QString NetDevice::ipv6AsString(){ - //Note: New on 6/24/15 - still needs testing - struct ifreq ifr; - memset(&ifr, 0, sizeof(struct ifreq)); - - strncpy(ifr.ifr_name, name.toLocal8Bit(), IFNAMSIZ); - int s = socket(PF_INET6, SOCK_DGRAM, 0); - - ioctl(s, SIOCGIFADDR, &ifr); - struct in6_addr in = ((sockaddr_in6 *) &ifr.ifr_addr)->sin6_addr; - char straddr[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, &in, straddr, sizeof(straddr)); - return QString(straddr); + //Get the sockaddr for the device + struct sockaddr *sadd = 0; + struct ifaddrs *addrs; + if( 0!=getifaddrs( &addrs ) ){ qDebug() << "Could not get addrs"; return ""; } + while(sadd==0 && addrs!=0){ + if( QString(addrs->ifa_name)==name && addrs->ifa_addr->sa_family==AF_INET6){ + //Found device (with IPv6 address) + sadd = addrs->ifa_addr; + break; + }else{ + //Move to the next device + addrs = addrs->ifa_next; + } + } + free(addrs); + if(sadd==0){ qDebug() << "No socket address found"; return ""; } + //Now get the IPv6 address in string form + char straddr[INET6_ADDRSTRLEN]; + int err = getnameinfo(sadd, sadd->sa_len, straddr, sizeof(straddr),NULL, 0, NI_NUMERICHOST); + if(err!=0){ + qDebug() << "getnameinfo error:" << gai_strerror(err); + return ""; + }else{ + return QString(straddr); + } } + //Fetch the netmask and return it as a QString QString NetDevice::netmaskAsString(){ struct ifreq ifr; diff --git a/src/library/sysadm-global.h b/src/library/sysadm-global.h index f9700d1..0dd3576 100644 --- a/src/library/sysadm-global.h +++ b/src/library/sysadm-global.h @@ -16,6 +16,7 @@ #include #include #include +#include //FreeBSD Includes #include