simple gethostbyname example

Here’s a simple example using gethostbyname() that emulates the host command.

I wrote it to debug a DNS problem. On the network I’m on right now, gethostbyname() returns a different answer than host and dig. It is overaggressively caching other hostnames. i.e. nsloookup shrub.ca is returning 17.250.248.64, which is mail.mac.com (!).

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    if (
argc < 2) {
        
printf("Usage: %s hostname", argv[0]);
        exit(-
1);
    }

    struct hostent *hp = gethostbyname(argv[1]);

    if (hp == NULL) {
       
printf("gethostbyname() failed\n");
    } else {
       
printf("%s = ", hp->h_name);
       
unsigned int i=0;
       while (
hp -> h_addr_list[i] != NULL) {
          
printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
          
i++;
       }
       
printf("\n");
    }
}

Join the Conversation

9 Comments

  1. Oops hit the return button to quickly
    3) exit (-1) doest not make much sense, what you get is the same as if you had written exit(255), since -1 representation is 0xffffffff in general on a 32bit system and the status returned to the parent process is truncated to the last 8 bits, exit(1) is what is used in general

    To summarize all those cosmetics changes I would recommend the following:

    /* for inet_ntoa() */
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #include <netdb.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char **argv) {
     if (argc < 2) {
       fprintf(stderr,"Usage: %s hostname\n", argv[0]);
       exit(1);
     }
    
     struct hostent *hp = gethostbyname(argv[1]);
    
     if (hp == NULL) {
       fprintf(stderr,"gethostbyname() failed\n");
       exit(1);
     } else {
       printf("%s = ", hp->h_name);
       unsigned int i=0;
       while ( hp -> h_addr_list[i] != NULL) {
         printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
         i++;
       }
       printf("\n");
       exit(0);
     }
    }
    
  2. Thank you, I used this for Windows, for those, who will find this page, here is what you need to get it working under Windows

    [code]
    #include
    #include
    WSADATA wsaData;
    WORD version;
    int error;
    version = MAKEWORD( 2, 0 );
    error = WSAStartup( version, &wsaData );

    if (argc < 2) {
    printf("Usage: %s hostname", argv[0]);
    exit(-1);
    }[/code]

  3. The code for Windows (based on previous responses to this post) is below. Remember to add -lws2_32 to the linker arguments. I compiled using MinGW using Eclipse on Windows 7.


    #include <stdio.h>
    #include <winsock2.h>

    WSADATA wsaData;
    WORD version;
    int error;

    int main(int argc, char **argv) {

    if (argc < 2) {
    fprintf(stderr,"Usage: %s hostname\n", argv[0]);
    exit(1);
    }

    struct hostent *hp = gethostbyname(argv[1]);
    if (hp == NULL) {
    fprintf(stderr,"gethostbyname() failed\n");
    exit(1);
    } else {
    printf("%s = ", hp->h_name);
    unsigned int i=0;
    while ( hp -> h_addr_list[i] != NULL) {
    printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
    i++;
    }
    printf("\n");
    exit(0);
    }
    }

  4. The code for Windows (based on previous responses to this post) is below. Remember to add -lws2_32 to the linker arguments. I compiled using MinGW using Eclipse on Windows 7.


    #include <stdio.h>
    #include <winsock2.h>

    WSADATA wsaData;
    WORD version;
    int error;

    int main(int argc, char **argv) {

    version = MAKEWORD( 2, 0 );
    error = WSAStartup( version, &wsaData );

    if (argc < 2) {
    fprintf(stderr,"Usage: %s hostname\n", argv[0]);
    exit(1);
    }

    struct hostent *hp = gethostbyname(argv[1]);
    if (hp == NULL) {
    fprintf(stderr,"gethostbyname() failed\n");
    exit(1);
    } else {
    printf("%s = ", hp->h_name);
    unsigned int i=0;
    while ( hp -> h_addr_list[i] != NULL) {
    printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
    i++;
    }
    printf("\n");
    exit(0);
    }
    }

  5. Hi Kevin,

    You mentioned “Remember to add -lws2_32 to the linker arguments. I compiled using MinGW using Eclipse on Windows 7”
    I selected the project -> Properties -> C/C== Building -> Settings -> MinGW C++ Linker -> Libraries , then at Libraries (-l) I typed “ws2_32”, then OK

    When I built project I got an errors “cannot find -lws2_32” , I wonder what did I miss?

    Thanks,
    Yin

Leave a comment

Your email address will not be published. Required fields are marked *