/* ============================================================================ Name : test-gethostbyname.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include #include //gethostbyname() #include //inet_ntoa() #include #include #include //sleep() #include void print_error( int h_error ) { switch( h_error ) { case HOST_NOT_FOUND: printf("%s HOST_NOT_FOUND The specified host is unknown.\n", __func__ ); break; case NO_ADDRESS: //case NO_DATA: printf("%s NO_ADDRESS or NO_DATA The requested name is valid but does not have an IP address.\n", __func__ ); break; case NO_RECOVERY: printf("%s NO_RECOVERY A nonrecoverable name server error occurred.\n", __func__ ); break; case TRY_AGAIN: printf("%s TRY_AGAIN, name server could not be connected.\n", __func__ ); break; default: printf("%s unknown state (h_error %d).\n", __func__, h_error ); } } int main(void) { int idx; struct hostent* p_hostent; printf("%s start.\n", __func__ ); #define TEST01 //also TEST02 #ifdef TEST01 #ifdef MYHOSTENT /* Description of data base entry for a single host. */ struct hostent { char *h_name; /* Official name of host. */ char **h_aliases; /* Alias list. */ int h_addrtype; /* Host address type. */ int h_length; /* Length of address. */ char **h_addr_list; /* List of addresses from name server. */ p_aline #ifdef __USE_MISC # define h_addr h_addr_list[0] /* Address, for backward compatibility.*/ #endif }; #endif //MYHOSTENT //https://man7.org/linux/man-pages/man3/gethostbyname.3.html p_hostent = gethostbyname( "www.msn.com" ); //p_hostent = gethostbyname( "www.teledyne.com" ); //p_hostent = gethostbyname( "www.google.com" ); //p_hostent = gethostbyname( "www.yahoo.com" ); if( p_hostent == NULL ) { printf("%s p_hostent is NULL\n", __func__ ); print_error( h_errno ); } else { if( p_hostent->h_name ) printf("%s h_name %s\n", __func__, p_hostent->h_name ); idx = 0; while( p_hostent->h_aliases[ idx ] != NULL ) { printf("%s h_aliases[%d] %s\n", __func__, idx, p_hostent->h_aliases[ idx ] ); idx++; } printf("%s h_addrtype %d\n", __func__, (int)p_hostent->h_addrtype ); printf("%s h_length %d\n", __func__, (int)p_hostent->h_length); idx = 0; while( p_hostent->h_addr_list[ idx ] != NULL ) { printf("%s h_addr_list[%d] %s\n", __func__, idx, inet_ntoa( *( struct in_addr* )p_hostent->h_addr_list[ idx ] ) ); idx++; } } #endif //TEST01 printf("%s finished.\n", __func__ ); return EXIT_SUCCESS; }