LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2010, 04:46 AM   #1
ashok449
Member
 
Registered: Sep 2007
Location: noida
Distribution: suse
Posts: 63

Rep: Reputation: 16
how to get IPv6 address using ioctl() SIOCGIFADDR


Hello All,

I have retrieved IPv4 address successfully using
Code:
struct ifreq ifr;
 fd = socket(AF_INET, SOCK_DGRAM, 0);
 ifr.ifr_addr.sa_family = AF_INET;
ioctl(fd, SIOCGIFADDR, &ifr)
for IPv6 address I tried
Code:
struct ifreq ifr;
 fd = socket(AF_INET6, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET6;
ioctl(fd, SIOCGIFADDR, &ifr)
but ioctl() returning error here, please suggest



Regards,
Ashok
 
Old 06-08-2010, 12:05 PM   #2
climbingdrummer
LQ Newbie
 
Registered: Jun 2010
Posts: 3

Rep: Reputation: 1
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
 
Old 06-09-2010, 04:33 AM   #3
ashok449
Member
 
Registered: Sep 2007
Location: noida
Distribution: suse
Posts: 63

Original Poster
Rep: Reputation: 16
Hello,

I tried for some time and not succeeded yet

any how here is the code where I've reached regarding this issue.
Code:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
   int main(void)
   {
     int                sfd, i;
  struct in6_ifreq {
        struct in6_addr ifr6_addr;
        uint32_t        ifr6_prefixlen;
        int             ifr6_ifindex;
};
  struct in6_ifreq       ifr;
     struct sockaddr_in6 *sin = (struct sockaddr_in6 *) &ifr.ifr6_addr;

     memset(&ifr, 0, sizeof ifr);

     if (0 > (sfd = socket(AF_INET6, SOCK_DGRAM, 0))) {
       perror("socket()");
       exit(1);
     }

   //  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ -1);
     sin->sin6_family = AF_INET6;

     if (0 == ioctl(sfd, SIOCGIFADDR, &ifr)) {

         //printf("%s: %s\n", ifr.ifr_name, inet_ntoa(sin->sin6_addr));

        }
     else
        printf("ioctl() ---error\n");


char str[INET6_ADDRSTRLEN];
printf("ntop ret= %s\n",inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sin)->sin6_addr), str, INET6_ADDRSTRLEN));

printf("%s\n", str);


     return 0;
   }
please let me know if there is a break through.


Thanks
Ashok
 
Old 06-09-2010, 10:43 AM   #4
climbingdrummer
LQ Newbie
 
Registered: Jun 2010
Posts: 3

Rep: Reputation: 1
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
 
Old 06-09-2010, 05:31 PM   #5
climbingdrummer
LQ Newbie
 
Registered: Jun 2010
Posts: 3

Rep: Reputation: 1
If you don't need to use the ioctl call, you can use the method getifaddrs.

Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <vector>
#include <string>
#include <iostream>
#include <netinet/in.h>
#include <net/if.h>

using namespace std;
 
std::vector<std::string> listAddresses();
void printAddresses(const std::vector<std::string>& addresses);

int main(int argc, char* argv[], char* envp[])
{
    addresses = listAddresses();
    printAddresses(addresses);
    return 0;
}

void printAddresses(const vector<string>& addresses) {
    for(vector<string>::const_iterator it=addresses.begin(); it!=addresses.end(); ++it) {
        string address = *it;
	cout << address << endl;
    }
}

vector<string> listAddresses()
{
    vector<string> addresses;

    struct ifaddrs *ifa=NULL,*ifEntry=NULL;
    void *addPtr = NULL;
    int rc = 0;
    char addressBuffer[INET6_ADDRSTRLEN];

    rc = getifaddrs(&ifa);
    if (rc==0) {
        for(ifEntry=ifa; ifEntry!=NULL; ifEntry=ifEntry->ifa_next) {
	    if(ifEntry->ifa_addr->sa_data == NULL) {
                continue;
	    }
            if(ifEntry->ifa_addr->sa_family==AF_INET) {
                 addPtr = &((struct sockaddr_in *)ifEntry->ifa_addr)->sin_addr;
            } else if(ifEntry->ifa_addr->sa_family==AF_INET6) {
                 addPtr = &((struct sockaddr_in6 *)ifEntry->ifa_addr)->sin6_addr;
            } else {
                //It isn't IPv4 or IPv6
		continue;
	    }

            const char *a = inet_ntop(ifEntry->ifa_addr->sa_family,
                          addPtr,
                          addressBuffer,
                          sizeof(addressBuffer));
	    if(a != NULL) {
                addresses.push_back(string(a));
 	    }
        }
    }
    freeifaddrs(ifa);

    return addresses;
}

Last edited by climbingdrummer; 06-09-2010 at 05:32 PM. Reason: put code tags
 
Old 06-11-2010, 04:35 AM   #6
ashok449
Member
 
Registered: Sep 2007
Location: noida
Distribution: suse
Posts: 63

Original Poster
Rep: Reputation: 16
Thanks for the Post, Its working.
 
Old 01-03-2012, 11:39 PM   #7
ssitaram
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
Thumbs up 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);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot create an IPv6 virtual IP (alias interface) using ioctl(SIOCSIFADDR) the_rock Linux - Newbie 2 08-20-2010 04:14 PM
ioctl -> Bad address keros Programming 4 02-19-2009 02:51 AM
How to set IPv6 address on IPv6 router tlemons Linux - Networking 3 09-17-2007 01:25 PM
ioctl[SIOCGIFADDR]: Cannot assign requested address mmcgann Linux - Wireless Networking 1 06-20-2006 12:09 PM
ifup eth0 causing error messages - SIOCGIFADDR: Cannot assign requested address pxumsgdxpcvjm Linux - Networking 7 01-20-2006 06:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration