LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-04-2011, 04:39 AM   #1
rachitraj
LQ Newbie
 
Registered: Feb 2011
Location: Bangalore
Distribution: Suse
Posts: 1

Rep: Reputation: 0
Question Unable to get IPv6 addresses using ioctl()


Hi,

I am able to retrieve IPv4 addresses with ioctl. But it is not returning IPv6 addresses. My requirement is to get both IPv4 and IPv6 addresses. I can not use getifaddrs() since it has to be common code between linux, solaris and aix. Solaris and AIX are not having getifaddrs(). Following code is returning only IPv4 addresses.

Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define num 5

int n;
int main(){

    int sd,err,ifsize;
    struct ifconf ifc;
    struct ifreq *ifr;

    struct ifreq ifreq;
    struct sockaddr_in *sa;
    char addrss[INET6_ADDRSTRLEN];
    char *cp,*i;
    void *in_addr;
    sd=socket(AF_INET6,SOCK_DGRAM,0);
    ifsize=num*sizeof(struct ifreq);
    ifc.ifc_buf=(char *)malloc(ifsize);
    ifc.ifc_len=ifsize;
    err=ioctl(sd, (int)SIOCGIFCONF, (caddr_t)&ifc);     n=(ifc.ifc_len/sizeof(struct ifreq));     printf("Interfaces = %d\n",n);
    i=ifc.ifc_len+ifc.ifc_buf;

    for(cp=ifc.ifc_buf;cp<i;cp+=sizeof(ifr->ifr_name)+sizeof(ifr->ifr_addr)){

          ifr=(struct ifreq *)cp;
          ifreq=*ifr;
          sa=(struct sockaddr_in *)&ifreq.ifr_addr;
          in_addr = &(sa->sin_addr);
          inet_ntop(sa->sin_family,in_addr,addrss, INET6_ADDRSTRLEN);
          printf("interface name=%s %s\n",ifreq.ifr_name,addrss);
          ifc.ifc_req++;
    }
    close(sd);
    return 0;
}

Please help me out in correcting this code for getting both IPv4 and IPv6 addresses.
 
Old 02-23-2011, 07:24 AM   #2
umwai
Member
 
Registered: May 2008
Location: Sri Lanka
Distribution: Ubuntu
Posts: 57
Blog Entries: 1

Rep: Reputation: 16
Lightbulb

As I've heard you can't use SIOCGIFCONF to get IPv6 addresses.
 
  


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
how to get IPv6 address using ioctl() SIOCGIFADDR ashok449 Linux - Networking 6 01-03-2012 11:39 PM
bind() fails with EADDRNOTAVAIL after calling an SIOCSIFADDR ioctl for IPV6 address lprasanna Linux - Networking 2 08-15-2011 02:29 PM
openntpd talking to itself via two different IPv6 addresses Skaperen Linux - Networking 0 10-26-2010 12:05 PM
Cannot create an IPv6 virtual IP (alias interface) using ioctl(SIOCSIFADDR) the_rock Linux - Newbie 2 08-20-2010 04:14 PM
[SOLVED] Validate IPv6 addresses ashok.g Programming 2 06-19-2010 04:49 PM

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

All times are GMT -5. The time now is 07:49 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