From a06ccccc63df831f4a7bdf38a0000ba36edbcb15 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 29 Mar 2009 10:51:26 +0200 Subject: added a simple getaddrinfo test --- tests/port/test_getaddrinfo.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'tests/port') diff --git a/tests/port/test_getaddrinfo.c b/tests/port/test_getaddrinfo.c index f7abfc0..5226b20 100644 --- a/tests/port/test_getaddrinfo.c +++ b/tests/port/test_getaddrinfo.c @@ -1,18 +1,29 @@ #include "port/sys.h" #define TEST_GETADDRINFO -#include "port/netdb.c" /* for getaddrinfo */ +#include "port/netdb.c" /* for getaddrinfo, NI_MAXHOST */ -#include /* for exit, EXIT_SUCCESS, free */ -#include +#include "port/stdlib.h" /* for exit, EXIT_SUCCESS, itoa */ +#include /* for fprintf */ +#include /* for memset */ int main( void ) { + struct addrinfo hints; struct addrinfo *result; struct addrinfo *res; int error; + char port_or_service[10]; + + /* tell getaddrinfo what we want */ + memset( &hints, 0, sizeof( struct addrinfo ) ); + hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; /* resolve the domain name into a list of addresses */ - error = getaddrinfo( "www.andreasbaumann.cc", NULL, NULL, &result ); + itoa( 80, port_or_service, 10 ); + error = getaddrinfo( "www.andreasbaumann.cc", port_or_service, &hints, &result ); if( error != 0 ) { fprintf( stderr, "getaddrinfo failed: %s (%d)\n", gai_strerror( error ), error ); @@ -21,16 +32,12 @@ int main( void ) { /* loop over all returned results and do inverse lookup */ for( res = result; res != NULL; res = res->ai_next ) { - char hostname[NI_MAXHOST] = ""; - - error = getnameinfo( res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0 ); - if( error != 0 ) { - fprintf( stderr, "getnameinfo failed: %s (%d)\n", - gai_strerror( error ), error ); - return EXIT_FAILURE; - } + char s[100]; + + memset( s, 0, 100 ); + inet_ntop( res->ai_family, &res->ai_addr, s, 100 ); - printf( "hostname: %s\n", hostname ); + printf( "%d %d %s\n", res->ai_family, res->ai_socktype, s ); } freeaddrinfo( result ); -- cgit v1.2.3-54-g00ecf