Thread: DNS Lookup

  1. #1

    Question DNS Lookup

    I'm trying to write a function to do a DNS lookup which takes an IP address and finds the hostname corresponding to the IP as well as the ALIASES associated with this IP address since a single IP address may be associated with multiple domain names.

    I have reached as far as getting the hostname associated with the IP address but I have been unable to retrieve any aliases associated with this IP address.

    The code I have written so far is.....

    #include <iostream>
    using namespace std;

    #include <winsock2.h>

    bool InitializeWinsock()
    WSADATA wsa;

    if( WSAStartup( MAKEWORD(2, 2), &wsa) != 0 )
    return false;

    return true;

    int main()

    char* ip = ""; // google

    // Convert IP to long.....
    unsigned int x = inet_addr(ip);

    if(x == INADDR_NONE)
    cout << "Cannot resolve IP address!" << endl;

    // Resolve IP address to hostname...
    hostent* h = gethostbyaddr( (const char*)&x, sizeof(x), AF_INET);

    if(h == NULL)
    cout << "Failed to get hostname!" << endl;

    // Print the hostname...
    cout << h->h_name << endl;

    // How to get aliases now?
    // This where I am stuck!

    return 0;

    I'm not sure that "" has any aliases associated with it but even if you choose a site that has an alias I still don't know how to retrieve that alias. If anyone knows how to get the aliases associated with an IP please help.

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    You've already gotten the aliases through the call to gethostbyaddr(). The HOSTENT struct contains a member named h_aliases:

    typedef struct hostent {
       char FAR* h_name;
       char FAR  FAR** h_aliases;
       short h_addrtype;
       short h_length;
       char FAR  FAR** h_addr_list;
    } hostent;
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DNS issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-02-2008, 10:15 AM
  2. Lookup in lists
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 12-07-2007, 02:14 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. DNS lookup?
    By Devil Panther in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2004, 07:05 AM
  5. win2k dns lookup
    By serrrrrver in forum Tech Board
    Replies: 11
    Last Post: 01-13-2003, 12:50 PM