how to get IPv6 address using ioctl() SIOCGIFADDR
Hello All,
I have retrieved IPv4 address successfully using Code:
struct ifreq ifr; Code:
struct ifreq ifr; Regards, Ashok |
Any chance you've figured this out? I'm having the same problem. I'm trying to use ioctl to find my ipv6 addresses but I'm only getting back ipv4. I'm using kernel 2.6.18
|
Hello,
I tried for some time and not succeeded yet :) any how here is the code where I've reached regarding this issue. Code:
Thanks Ashok |
I found this site which may be useful to you. I could not get it to print ipv6 addresses though.
http://www.adamrisi.com/?p=84 |
If you don't need to use the ioctl call, you can use the method getifaddrs.
Code:
#include <sys/types.h> |
Thanks for the Post, Its working.
|
getting if index from IPv6 address
use this method instead of ioctl it will work. Hope this helps
/****************************************************************** * FUNCTION: my_get_ipv6_ifindex_from_addr * * DESCRIPTION: this function returns ifindex for a given IPV6 address string * * PARAMETERS: 'ipv6_addr_str' char string in the format given by inet_ntop() * * RETURN VALUE: On Success return value is greater than Zero, * Zero or Negetive on Error. *******************************************************************/ int my_get_ipv6_ifindex_from_addr(char *ipv6_addr_str) { struct ifaddrs *if_addr, *ifap; int family, res; char host_id[NI_MAXHOST], *host_addr; int ifcount; int ifindex_matched = -1; if (getifaddrs(&if_addr) == -1) { perror("getif_addrs"); return(ERROR); } ifcount = 0; for (ifap = if_addr; ifap != NULL; ifap = ifap->ifa_next) { if (ifap->ifa_addr == NULL) { ifcount++; continue; } family = ifap->ifa_addr->sa_family; if (family == AF_INET6) { res = getnameinfo(ifap->ifa_addr, sizeof(struct sockaddr_in6), host_id, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (res != 0) { /* failure to get IPv6 address */ continue; } host_addr = strtok(host_id, "%"); if ((strncmp(host_id, ipv6_addr_str, INET6_ADDRSTRLEN)) == 0) { ifindex_matched = if_nametoindex(ifap->ifa_name); break; } ifcount++; } } return (ifindex_matched); } |
All times are GMT -5. The time now is 02:04 AM. |