summaryrefslogtreecommitdiff
path: root/tests/port/test_getaddrinfo.c
blob: f7abfc05b97c421547b4c48d26f5aade3e98dc64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "port/sys.h"

#define TEST_GETADDRINFO
#include "port/netdb.c"		/* for getaddrinfo */

#include <stdlib.h>		/* for exit, EXIT_SUCCESS, free */
#include <stdio.h>

int main( void ) {
	struct addrinfo *result;
	struct addrinfo *res;
	int error;
 
	/* resolve the domain name into a list of addresses */
	error = getaddrinfo( "www.andreasbaumann.cc", NULL, NULL, &result );
	if( error != 0 ) {
		fprintf( stderr, "getaddrinfo failed: %s (%d)\n",
			gai_strerror( error ), error );
		return EXIT_FAILURE;
	}
 
	/* 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;
		}

		printf( "hostname: %s\n", hostname );
	}
 
	freeaddrinfo( result );
 
	return EXIT_SUCCESS;
}