1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help Needed in sockets in C

Discussion in 'C' started by lionaneesh, May 2, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Code:
     /*
    ** showip.c -- show IP addresses for a host given on the command line
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    
    int main(int argc, char *argv[])
    {
        struct addrinfo hints, *res, *p;
        int status;
        char ipstr[INET6_ADDRSTRLEN];
    
        if (argc != 2) {
            fprintf(stderr,"usage: showip hostname\n");
            return 1;
        }
    
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
        hints.ai_socktype = SOCK_STREAM;
    
        if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
            fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
            return 2;
        }
    
        printf("IP addresses for %s:\n\n", argv[1]);
    
        for(p = res;p != NULL; p = p->ai_next) {
            void *addr;
            char *ipver;
    
            // get the pointer to the address itself,
            // different fields in IPv4 and IPv6:
            if (p->ai_family == AF_INET) { // IPv4
                struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
                addr = &(ipv4->sin_addr);
                ipver = "IPv4";
            } else { // IPv6
                struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
                addr = &(ipv6->sin6_addr);
                ipver = "IPv6";
            }
    
            // convert the IP to a string and print it:
            inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
            printf("  %s: %s\n", ipver, ipstr);
        }
    
        freeaddrinfo(res); // free the linked list
    
        return 0;
    }  
    I am having problems to understand this program i know the function getaddrinfo(); and the structs but having problem with the linked list and the following loop:-

    Code:
         for(p = res;p != NULL; p = p->ai_next) {
            void *addr;
            char *ipver;
    
            // get the pointer to the address itself,
            // different fields in IPv4 and IPv6:
            if (p->ai_family == AF_INET) { // IPv4
                struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
                addr = &(ipv4->sin_addr);
                ipver = "IPv4";
            } else { // IPv6
                struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
                addr = &(ipv6->sin6_addr);
                ipver = "IPv6";
            }  
    questions:-

    1. Why do we need this loop and what is the mechanism of this p->next etc.

    2.please explain me the whole loop rely seeking for your help .

    and i have a problem in understanding this function
    inet_ntop

    and this code how it works :-
    Code:
    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
            printf("  %s: %s\n", ipver, ipstr);
    
    please guyz i am stuck at this topic and am reading
    more and more tutorials but none of them help please help me.

    Rely seeking your help.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Do not post the same thread more than once and your other thread is deleted.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    res points to a struct addrinfo

    This article explains how linked lists work: http://en.wikipedia.org/wiki/Linked_list
    The X box at the right of the topmost illustration would typically be NULL.

    > i have a problem in understanding this function inet_ntop

    Well, did you try Googling it? No, methinks, because the top hit at http://lmgtfy.com/?q=inet_ntop is an OpenGroup documentation link. Did you try reading the comment just before it ("// convert the IP to a string and print it:")? I guess not otherwise you wouldn't have asked.
     

Share This Page