Thread: gethostbyaddr() returning NULL

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    gethostbyaddr() returning NULL

    When calling gethostbyname("64.85.65.229") it returns NULL. 64.85.65.229 is the IP of a game server to which I normally can connect without a problem. When using my self-made proxy however, I can connect to all servers except this one. "nslookup 64.85.65.229" gives me: "can't find 64.85.65.229: Non-existent domain". So does anyone know how to fix this problem? Where does the problem lie, at my ISP's DNS server or at my code?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What OS? What compiler? Please provide some sample code...

    From the Winsock pages:
    The gethostbyname function cannot resolve IP address strings passed to it. Such a request is treated exactly as if an unknown host name were passed. Use inet_addr to convert an IP address string the string to an actual IP address, then use another function, gethostbyaddr, to obtain the contents of the hostent structure.
    From the Linux pages:
    The gethostbyname() function returns a structure of type hostent for the given host name. Here name is either a host name, or an IPv4 address in standard dot notation, or an IPv6 address in colon (and possibly dot) notation. (See RFC 1884 for the description of IPv6 addresses.) If name is an IPv4 or IPv6 address, no lookup is performed and gethostbyname() simply copies name into the h_name field and its struct in_addr equivalent into the h_addr_list[0] field of the returned hostent structure.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    I'm using Dev-C++ with gcc (GCC) 3.2 (mingw special 20020817-1) on windows 2k
    This code for example returns NULL:

    Code:
    #include <stdio.h>
    #include <winsock2.h>
    
    int main() {
      WSADATA wsaData;
      WSAStartup(MAKEWORD(2,0),&wsaData);
    
      unsigned long ip     = inet_addr("64.85.65.229");
      struct hostent *host = gethostbyaddr((char *)&ip,4,0);
    
      if(host == NULL)
        printf("NULL\n");
      else
        printf("Ok\n");
    
      return 0;
    }
    Could someone test to see wether you get the same result?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are getting NULL because the lookup of the ip address you entered failed. If you entered a different ip address, it would probably work just fine. I hope you realize that the point of the gethostbyaddr() function is to do a reverse DNS lookup on an IP address.

    What is it you are trying to do?

  5. #5
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    I need this code for my socks proxy server. The client sends the IP from the target host to my proxy. Let's say the IP is now stored in char ip[4]. What I've learned to do then is this:

    Code:
    struct hostent *host;
    if((host = gethostbyaddr(ip,4,0)) == NULL)
      return 0;
    
    struct sockaddr_in remote;
    remote.sin_family      = AF_INET;
    remote.sin_port        = htons(80);
    remote.sin_addr.s_addr = *((struct in_addr *)host->h_addr);
    
    // connect and stuff

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If something fails, then it's a good idea to print out an error message, like so:
    >printf("error#:%ld\n", WSAGetLastError());

    In your last post, you suggest that you are given an IP in binary format, you then reverse look it up, and use the IP from the reverse lookup result to populate the remote struct. Any reason you don't use use the original binary IP in the remote struct?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Ah I get it now. I always used gethostbyaddr in the way shown above (that's the way I learned it somewhere), I didn't know that the sin_addr.s_addr field was simply the IP address. Thanks, got it working now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  4. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM