From owner-freebsd-net@freebsd.org Wed Dec 9 14:11:46 2015 Return-Path: Delivered-To: freebsd-net@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C970D9D58F0 for ; Wed, 9 Dec 2015 14:11:46 +0000 (UTC) (envelope-from ken@pcbsd.org) Received: from barracuda.ixsystems.com (mail.ixsystems.com [69.198.165.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.ixsystems.com", Issuer "Go Daddy Secure Certificate Authority - G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B421F1810 for ; Wed, 9 Dec 2015 14:11:46 +0000 (UTC) (envelope-from ken@pcbsd.org) X-ASG-Debug-ID: 1449670304-08ca042abd06970002-QdxwpM Received: from [192.168.1.130] (24-151-177-78.dhcp.kgpt.tn.charter.com [24.151.177.78]) by barracuda.ixsystems.com with ESMTP id 4gggmiTvoADdeZpM (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO) for ; Wed, 09 Dec 2015 06:11:45 -0800 (PST) X-Barracuda-Envelope-From: ken@pcbsd.org X-Barracuda-AUTH-User: ken@pcbsd.org X-Barracuda-Apparent-Source-IP: 24.151.177.78 To: freebsd-net@freebsd.org From: Ken Moore Subject: IPv6 Address as text (C) Message-ID: <5668369F.9020309@pcbsd.org> X-ASG-Orig-Subj: IPv6 Address as text (C) Date: Wed, 9 Dec 2015 09:11:43 -0500 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Barracuda-Connect: 24-151-177-78.dhcp.kgpt.tn.charter.com[24.151.177.78] X-Barracuda-Start-Time: 1449670304 X-Barracuda-Encrypted: ECDHE-RSA-AES128-GCM-SHA256 X-Barracuda-URL: https://10.2.0.41:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at ixsystems.com X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.25106 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Dec 2015 14:11:47 -0000 Note: Please CC me on replies - I am not subscribed to this list. I am having a bit of trouble getting an accurate string representation of the current IPv6 address for a given device using the C system libraries and was wondering of somebody with more experience than me might be able to spot the error... Background: I have been working on a couple simple C/C++/Qt functions to return printable forms of the current ipv4 and ipv6 addresses assigned to a particular device, and while the ipv4 function works fine the ipv6 address is consistently wrong and almost always the same string - making me think it is converting some internal error code to a string ("::XXe2:ffff:ff7f:0" where the "X"s are the only two characters which ever change). The two functions are nearly identical, and I think the error probably comes from needing to use inet_ntop() for the ipv6 address because there is no ipv6-compatible version of the inet_ntoa() function. Do you have any thoughts or ideas about where the error might be coming from or a better way to read off the ipv6 address as a string? Here are the two functions: Note: "name" is the QString of the device name (wlan0, re0, other...), and is an internal variable for the overall "NetDevice" class [code] //Fetch the IPv4 address and return it as a QString QString NetDevice::ipAsString(){ struct ifreq ifr; memset(&ifr, 0, sizeof(struct ifreq)); strncpy(ifr.ifr_name, name.toLocal8Bit(), IFNAMSIZ); int s = socket(PF_INET, SOCK_DGRAM, 0); ioctl(s, SIOCGIFADDR, &ifr); struct in_addr in = ((sockaddr_in *) &ifr.ifr_addr)->sin_addr; return QString( inet_ntoa(in) ); } //Fetch the IPv6 address and return it as a QString QString NetDevice::ipv6AsString(){ 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( inet_ntop(AF_INET6, &in, straddr, sizeof(straddr)) ); } [/code] Thanks! Any input is appreciated! -- ~~ Ken Moore ~~ PC-BSD/iXsystems