Trying to get an IP address from "www.facebook.com"

This is a discussion on Trying to get an IP address from "www.facebook.com" within the Networking/Device Communication forums, part of the General Programming Boards category; I'm using Beej's tutorial as Salem suggested, and although painful, I finally got it linked up properly with the Windows ...

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    Trying to get an IP address from "www.facebook.com"

    I'm using Beej's tutorial as Salem suggested, and although painful, I finally got it linked up properly with the Windows library.

    While I don't get any compiler errors or warnings from this program, I get the following remark from the system with the WSAddressToString function :

    Code:
    WSAAddressToString failed with : 10014,
    System remark : The system detected an invalid pointer address in attempting to use a pointer argument in a call.
    Or at least that's what my program outputs. I'm not sure which parameter it thinks is invalid, as far as I'm concerned I used all the right ones.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define _WIN32_WINNT 0x0501
    
    
    #include <winsock2.h>
    #include <Ws2tcpip.h>
    
    
    int main ( )
    {
        WSADATA wsaData;
    
    
        if ( WSAStartup(MAKEWORD(2,2), &wsaData ) )
        {
            fprintf(stderr, "WSAStartup failed with : %d,\nSystem remark : %s", WSAGetLastError(), gai_strerror(WSAGetLastError()));
            return 1;
        }
        else
            printf("WSAStartup initialized!\n\n");
    
    
        int status = 0;
    
    
        DWORD strlen = 0;
    
    
        struct addrinfo hints, * res = NULL, * temp = NULL;
    
    
        struct sockaddr_in * ipv4 = NULL;
        struct sockaddr_in6 * ipv6 = NULL;
    
    
        char * ipver = malloc(10);
    
    
        if ( !ipver )
        {
            perror("Malloc");
            WSACleanup();
            return 1;
        }
    
    
        memset(ipver, 0, 10);
        char ipstr[INET6_ADDRSTRLEN];
    
    
        memset( &hints, 0, sizeof(hints) );
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE;
    
    
        if ( (status = getaddrinfo("www.facebook.com", NULL, &hints, &res )) )
        {
            fprintf(stderr, "getaddrinfo failed with : %s", gai_strerror(status));
            free(ipver);
            WSACleanup();
            return 1;
        }
    
    
        for ( temp = res; temp; temp = temp->ai_next, strlen = 0 )
        {
            if ( temp->ai_family == AF_INET )
            {
                ipv4 = ( struct sockaddr_in * )temp->ai_addr;
                ipver = strcpy(ipver, "IPv4");
    
    
                if ( WSAAddressToString( (LPSOCKADDR)ipv4, sizeof(*ipv4), NULL, ipstr, &strlen ) )
                {
                    fprintf(stderr, "WSAAddressToString failed with : %d,\nSystem remark : %s", WSAGetLastError(), gai_strerror(WSAGetLastError()));
                    free(ipver);
                    WSACleanup();
                    return 1;
                }
            }
            else
            {
                ipv6 = ( struct sockaddr_in6 * )temp->ai_addr;
                ipver = strcpy(ipver, "IPv6");
    
    
                if ( WSAAddressToString( (LPSOCKADDR)ipv6, sizeof(*ipv6), NULL, ipstr, &strlen ) )
                {
                    fprintf(stderr, "WSAAddressToString failed with : %d,\nSystem remark : %s", WSAGetLastError(), gai_strerror(WSAGetLastError()));
                    free(ipver);
                    WSACleanup();
                    return 1;
                }
            }
    
    
            printf("The IP address for \"www.facebook.com\" is...\n\n");
    
    
            printf("\n%s : %s\n", ipver, ipstr);
            memset(ipver, 0, 10);
        }
    
    
        freeaddrinfo( res );
        free(ipver);
    
    
        if ( !WSACleanup() )
            printf("Clean up was sucessful, program exited normally!");
        else
        {
            fprintf(stderr, "WSACleanup failed with : %d,\nSystem Remark : %s", WSAGetLastError(), gai_strerror(WSAGetLastError()));
            return 1;
        }
    
    
        return 0;
    }
    Any help is appreciated.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Check out WSAStartup function (Windows)
    Note what WSAStartup returns on success.
    So there is no error and therefore the error string is meaningless.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by oogabooga View Post
    Check out WSAStartup function (Windows)
    Note what WSAStartup returns on success.
    So there is no error and therefore the error string is meaningless.
    I already knew WSAStartup returns 0, that's why if it returns as true, I displayed an error message. Is there something that I'm not seeing in that? It was the WSAAdressToString function giving me problems...
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by HelpfulPerson View Post
    I already knew WSAStartup returns 0, that's why if it returns as true, I displayed an error message. Is there something that I'm not seeing in that? It was the WSAAdressToString function giving me problems...
    It does not return true.. It returns error code you are ignoring

    You are missing in the function description:
    WSAGetLastError function is not needed and should not be used.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by HelpfulPerson View Post
    I already knew WSAStartup returns 0, that's why if it returns as true, I displayed an error message. Is there something that I'm not seeing in that? It was the WSAAdressToString function giving me problems...
    Sorry about that. Don't know what I was thinking.

    strlen is the name of a C library function, so it's a bad variable name.
    But that's probably not the error.
    I'll think about it ....

    EDIT: Hmmm, it does say that "On input, this parameter specifies the length of the buffer pointed to by the lpszAddressStringparameter."
    And of course on output it will be the length of the returned string (including the null terminator, apparently.)
    Could be the problem. Could you test that?

    Last edited by oogabooga; 09-02-2013 at 01:46 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    I have run your code using VS2012... After fixing buffer length - it works ok

    WSAAddressToString function (Windows)

    lpdwAddressStringLength [in, out]

    On input, this parameter specifies the length of the buffer pointed to by the lpszAddressString parameter. The length is represented in bytes for ANSI strings, and in WCHARs for Unicode strings. On output, this parameter returns the length of the string including the NULL terminator actually copied into the buffer pointed to by the lpszAddressString parameter. If the specified buffer is not large enough, the function fails with a specific error of WSAEFAULT and this parameter is updated with the required size.
    PS. You are passing as input 0 - so the function fails since it thinks there is not enough space in the buffer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by vart View Post
    It does not return true.. It returns error code you are ignoring

    You are missing in the function description:
    If it returns non zero on error, I consider that true. You are correct though, in that case I should've set status to the return value and turned that value into a string. Also, the input value is the length of the string and the output value is the length of the actual string? Ugh, that's the error I missed then.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 01-06-2012, 02:01 PM
  2. Printf error "address pointing at code space is taken"
    By KMAN999 in forum C Programming
    Replies: 2
    Last Post: 07-04-2011, 03:27 PM
  3. "*x + *x++" Address/Pointer "?"
    By Marth_01 in forum C Programming
    Replies: 10
    Last Post: 11-05-2008, 03:33 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21