gethostbyname allways NULL

This is a discussion on gethostbyname allways NULL within the Windows Programming forums, part of the Platform Specific Boards category; I'm calling gethostbyname() giving as parameter an MX record but it is allways NULL... The code below, given a mail, ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    gethostbyname allways NULL

    I'm calling gethostbyname() giving as parameter an MX record but it is allways NULL...

    The code below, given a mail, checks the MX record of that provider and tryes to retrieve the IP address... but it fails while calling gethostbyname()

    Code:
    int getMX(char* email) {
        char* dominio;
        struct hostent *host;
        
    	DNS_RECORD* pRecordList = NULL;
    	DNS_RECORD* pRecord     = NULL;
    
        dominio = strtok (email, "@");
        dominio = strtok (NULL, "@");
     
    	/* Send the DNS query... */
    	if (NOERROR == DnsQuery_A(dominio,
                                   DNS_TYPE_MX,
                                   DNS_QUERY_STANDARD,
                                   NULL,
                                   &pRecordList,
                                   NULL)) {
         	// get the first MX record
    	    for (pRecord = pRecordList; pRecord != NULL; pRecord = pRecord->pNext) {
    		    if (pRecord->wType == DNS_TYPE_MX) {
    			    printf("-> MX Server:  %s\n", (char*)pRecord->Data.MX.pNameExchange);
    			    // try to resolve it
    			    if ((host = gethostbyname((char*)pRecord->Data.MX.pNameExchange)) != NULL){
    			              printf("   Host: %s\n", host->h_name);
                    } else printf("Unable to resolve the IP.\n");
    		    } else printf("-> MX Server NOT FOUND\n");
          	    break;
            }
    	DnsRecordListFree(pRecordList, DnsFreeRecordList);
    	return 0;
        } else { printf("-> Unreachable.\n");
                 return -1;
          }
    }
    the output is:

    Mail: test@gmail.com
    -> MX Server: gsmtp185.google.com
    Unable to resolve the IP.
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Start by determining what WSAGetLastError() returns after gethostbyname(). My call to gethostbyname() succeeds resolving to 64.233.185.27. Are you calling WSAStartup()?

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    oh great, I was missing the initialization step... now it works but I'm unable to retrieve the IP because host->h_name will give me the hostname... how can i retrieve it?

    I'm confused about the Null-terminated list of addresses for the host in the http://msdn.microsoft.com/library/de.../hostent_2.asp struct. Is it here?
    This forum is the best one I've ever seen. Great ppl, great coders

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Download my multithreaded socket library on my library page (see sig for link) and take a look at the ResolveToIP() function in mtsocket.cpp for an example. You can also browse the code for other concepts regarding socket handling.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Thanx for ur code
    I'm also trying to use an A query to retrieve the IP from the MX responce:

    Code:
    // A
    DNS_RECORD* pRecordList2 = NULL;
    DNS_RECORD* pRecord2     = NULL;
    
    ...
    ...
    
    // resolve the IP
                    if (NOERROR == DnsQuery_A((char*)pRecord->Data.MX.pNameExchange,
                                              DNS_TYPE_A,
                                              DNS_QUERY_STANDARD,
                                              NULL,
                                              &pRecordList2,
                                              NULL)) {
                      for (pRecord2 = pRecordList2; pRecord2 != NULL; pRecord2 = pRecord2->pNext) {
                          if (pRecord2->wType == DNS_TYPE_A) {
                              printf("-> IP:  %s\n", (char*)pRecord2->Data.A.IpAddress);
                          } else printf ("Unable to resolve the IP.\n");
                          
                          break;
                      }
                    
                    } else printf("Error, query A.\n");
    but it crashes while trying to printf the IP.. how can I print an IPV4_ADDRESS ?
    This forum is the best one I've ever seen. Great ppl, great coders

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can use the inet_ntoa function to convert an IPv4 address into a string. This function takes a struct in_addr so we will need to create one:
    Code:
    		if (pRecord->wType == DNS_TYPE_A)
    		{
    			struct in_addr ipAddr;
    			ipAddr.S_un.S_addr = pRecord2->Data.A.IpAddress;
    
    			printf("  IP Address: %s\n", inet_ntoa(ipAddr));
    		}
    There is no guarantee that there will be any A records in an MX lookup.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    perfect, it works

    ps: I'm not looking for an A record into an MX lookup in the additional info.. I'm getting the MX hostname from a domain and sending another dns packet containing an A request to the MX hostname, for a total of 2 packets.
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  3. Tweakable Radar...
    By DoraTehExploda in forum Game Programming
    Replies: 8
    Last Post: 06-07-2005, 10:49 AM
  4. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM

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