Get list of all IP addresses

Discussion in 'C++' started by svirdi, Aug 8, 2006.

  1. svirdi

    svirdi Guest

    I have been trying to get all ip addresses from all interfaces, with
    the code listed below.
    However, all I get is the 1st IP only. I have configured 2 IPs on a
    single interface, I want to get both from my c++ code.
    thanks
    svirdi

    #include <unistd.h>
    #include <stropts.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <net/if.h>
    #include <sys/ioctl.h>
    //#include <sys/sockio.h>


    int main(int argn,char** argv)
    {
    int sock,n;
    struct ifreq *ifr;
    struct ifconf ifc;
    char buf[1024],addres[16];
    unsigned char* adr;


    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    // sock = socket(2, 2, 0);
    printf("sock=%d\n", sock);


    if (sock == -1)
    {
    printf("There is trouble of sock!! Exiting\n\n");
    return 0;
    }


    ifc.ifc_buf = buf;
    //ifc.ifc_len = 1024;


    int retVal = ioctl(sock, SIOCGIFCONF, &ifc);
    printf("retval ioctl(sock, SIOCGIFCONF, &ifc)=%d\n\n", retVal);



    if (retVal < 0)
    {
    printf("There is trouble with ioctl\n\n");
    close (sock);
    return 0;
    }


    n = ifc.ifc_len/sizeof(struct ifreq);


    for (ifr = ifc.ifc_req; n > 0; n--, ifr++)
    {
    int rval = ioctl( sock, SIOCGIFADDR, ifr);
    printf(" ioctl( sock, SIOCGIFADDR, ifr) rval = %d\n",
    rval);


    if (ifr->ifr_addr.sa_family == AF_INET ||
    ifr->ifr_addr.sa_family == AF_INET6)
    {
    adr = reinterpret_cast<unsigned
    char*>(ifr->ifr_ifru.ifru_addr.sa_data+2);


    snprintf(addres,16,"%i.%i.%i.%i",adr[0],adr[1],adr[2],adr[3]);
    printf("%s = %s\n",ifr->ifr_name,addres);
    }
    }
    close(sock);
    return 0;
    }
     
    svirdi, Aug 8, 2006
    #1
    1. Advertising

  2. svirdi wrote:
    > I have been trying to get all ip addresses from all interfaces, with
    > the code listed below.
    > However, all I get is the 1st IP only. I have configured 2 IPs on a
    > single interface, I want to get both from my c++ code.
    > [..]


    We cannot help you with that. Whatever mechanism you use to get all
    IP addresses, it has nothing to do with C++. Neither the language nor
    the library has any means for Internet communication, it is all done
    using the functionality provided by your OS. Ask in the OS newsgroup.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 8, 2006
    #2
    1. Advertising

  3. svirdi

    Default User Guest

    svirdi wrote:

    > I have been trying to get all ip addresses from all interfaces, with
    > the code listed below.
    > However, all I get is the 1st IP only. I have configured 2 IPs on a
    > single interface, I want to get both from my c++ code.
    > thanks
    > svirdi
    >
    > #include <unistd.h>
    > #include <stropts.h>
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <sys/socket.h>
    > #include <net/if.h>
    > #include <sys/ioctl.h>


    I recommend comp.unix.programmer.




    Brian
     
    Default User, Aug 8, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hari

    retrieve all ip addresses in system

    hari, Jul 8, 2003, in forum: C Programming
    Replies:
    2
    Views:
    299
    Richard Bos
    Jul 8, 2003
  2. Steve Pinard
    Replies:
    1
    Views:
    1,374
    David Bolen
    Jul 14, 2003
  3. damacy
    Replies:
    31
    Views:
    914
    damacy
    Sep 1, 2006
  4. namespace1
    Replies:
    3
    Views:
    922
  5. Deboo
    Replies:
    4
    Views:
    136
    Mohammed Sameer
    Aug 5, 2003
Loading...

Share This Page