Here is the code to query for addresses in their numeric form:
Code:
#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
int ga_hints( const int, char ** , struct addrinfo * );
int main(int argc, char ** argv){
struct addrinfo * res, * result, hints;
int error = 0, host_arg = 0;
char host[NI_MAXHOST];
host_arg = ga_hints( argc, argv, &hints );
if( ( error = getaddrinfo( argv[ host_arg ], NULL, &hints, &result ) ) != 0 ){
fputs( gai_strerror( error ), stderr );
exit( -1 );
}
for( res = result; res != NULL; res = res->ai_next ){
if( ( error = getnameinfo( res->ai_addr, res->ai_addrlen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST ) ) != 0){
fputs( gai_strerror( error ), stderr );
exit( -1 );
}
printf(" - %s\n", host);
}
free(result);
return 0;
}
// Usage: ./galookup -f [inet4 | inet6 | inet4-6] -h [hostname]
int ga_hints( const int argc, char ** argv, struct addrinfo * hints ){
int arg, host;
bzero( hints, sizeof( *hints) );
for( arg = 1; arg < argc; arg++ ){
if( !strcmp( "-f", argv[ arg ] ) ){
switch( atoi( argv[ ++arg ] ) ){
case 4:
hints->ai_family = AF_INET;
break;
case 6:
hints->ai_family = AF_INET6;
break;
case 0:
hints->ai_family = AF_UNSPEC;
break;
default:
printf( "%s is not a valid address family\nDefaulting to AF_UNSPEC...\n", argv[ arg ] );
hints->ai_family = AF_UNSPEC;
break;
} // Switch
} //if( !strcmp())
else if( !strcmp( "-h", argv[ arg ] ) )
host = arg + 1;
} //for(...)
return host;
}
and look what happens when I run it:
Code:
$ ./galookup -f 0 -h localhost
- 127.0.0.1
- 127.0.0.1
- 127.0.0.1
- ::1
- ::1
- ::1
$
$
$
$ ./galookup -f 0 -h www.google.com
- 74.125.227.146
- 74.125.227.145
- 74.125.227.144
- 74.125.227.147
- 74.125.227.148
- 74.125.227.144
- 74.125.227.145
- 74.125.227.148
- 74.125.227.147
- 74.125.227.148
- 74.125.227.144
- 74.125.227.145
- 74.125.227.146
- 74.125.227.147
- 74.125.227.146
- 2001:4860:4002:800::1010
- 2001:4860:4002:800::1010
- 2001:4860:4002:800::1010
$
$
$ ./galookup -f 0 -h FreeBSD-01v
- 127.0.0.1
- 127.0.0.1
- 127.0.0.1
- 192.168.0.4
- 192.168.0.4
- 192.168.0.4
$
Here's what's in /etc/hosts
Code:
::1 localhost localhost.my.domain
127.0.0.1 localhost FreeBSD-01v localhost.my.domain
192.168.0.4 FreeBSD-01v
Also, when I do an nslookup, the addresses are totally different for websites but they all seem to work... What gives with that also?