Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Simple IPv6 socket programming

    Hello Guys!

    I have a question about some IPv6 socket programming (in C). I'm in charged of doing some specific task in IPv6 and I'm getting my bearings in that field.

    Since i'm new to this, I decided to start with some basic. I thought a good practice will be to use the gethostbyname2() function so given the host name of the PC it will return to you (using the hostent struct and so forth) the IPv6 address. But even something as simple as that, I have not been able to accomplished.

    When I compiled it using gcc it returns no errors, but when I run it, the printf function you will see (that is actually a dummy one, kind of making sure the program is getting there) can be seen but afterwards, it "thinks" for a couple of seconds, and then says "segmentation faults".

    Here is the core of the code:
    /*TESTING gethostbyname2();*/
            printf("I'm in\n");
            struct hostent *hp;
            struct in6_addr a;
            char *buf_addr;     //not using right now
            hp = gethostbyname2(HOST, AF_INET6);
            printf("Official name: %s\n\n",hp->h_name); //Seg.Faul occurs around here and I can't see the IPv6 address of my Ubuntu
    HOST is declared as:
    #define HOST "antonio-desktop" //which is the hostname of my ubuntu

    It must be something with the gethostbyname2() since if I comment it, there is no segmentation faults.

    All libraries are included:
    #include <stdio.h>
    #include <sys/socket.h> //: socket(), connect(), send(), recv(),
    #include <arpa/inet.h> //: htons(),
    #include <string.h> //: memset(),
    #include <sys/types.h> //:
    #include <netinet/in.h>

    What I'm starting to think is that It must be something with the IPv6 setup? Im running Ubuntu 9.04, which I know that it has IPv6 features.

    I really thank you for your time and effort.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Frankly, I think that your call to gethostbyname2() is failing, setting hp to a null pointer. So, when you access it, you segfault. Check hp after gethostbyname2() (or gethostbyname()), and if null, then check the external variable h_errno which could be set to HOST_NOT_FOUND, NO_ADDRESS, NO_DATA, NO_RECOVERY, or TRY_AGAIN. Also, look at the man pages for gethostbyname for more information.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts