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