gethostbyname.c
00001 #include <sys/types.h>
00002 #include <sys/socket.h>
00003 #include <netinet/in.h>
00004 #include <netdb.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <arpa/inet.h>
00008
00009 void print_hostent_info(struct hostent * hp);
00010
00011 int main( int argc, char* argv[])
00012 {
00013 extern int h_errno;
00014 struct hostent* hp;
00015 if ( argc != 2) {
00016 fprintf(stderr, "Usage: %s <host_name>|<host_addr>\n",argv[0]);
00017 return EXIT_FAILURE;
00018 }
00019 if (( hp = gethostbyname(argv[1])) == NULL) {
00020 fprintf(stderr, "[Erreur] gethostbyname : %s\n", hstrerror(h_errno));
00021 return EXIT_FAILURE;
00022 }
00023 print_hostent_info(hp);
00024 return EXIT_SUCCESS;
00025 }
00026
00027 void print_hostent_info(struct hostent * hp) {
00028 unsigned int i,j;
00029
00030
00031 printf("Champ h_name : %s\n",
00032 (hp->h_name == NULL)?"NULL":hp->h_name);
00033
00034 if (hp->h_aliases != NULL && *hp->h_aliases != NULL) {
00035 i = 0;
00036 printf( "Champ h_aliases :");
00037 do {
00038 printf( " %s", hp->h_aliases[i]);
00039 i++;
00040 } while (hp->h_aliases[i] != NULL);
00041 printf("\n");
00042 }
00043
00044 printf( "Champ h_addrtype : %d (%s)\n",
00045 hp->h_addrtype,
00046 (hp->h_addrtype == AF_INET)?"AF_INET":"!AF_INET");
00047
00048 printf( "Champ h_length : %d\n", hp->h_length);
00049
00050 printf( "Champ h_addr_list: \n");
00051 for( i = 0 ; hp->h_addr_list[i] != NULL; i++) {
00052 printf(" [%d] ",i);
00053 for ( j = 0; j < hp->h_length; j++)
00054 printf( "%02x", hp->h_addr_list[i][j]);
00055 printf( "(%s)\n", inet_ntoa( * (struct in_addr *)( hp->h_addr_list[i]) ));
00056 }
00057 }