-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.c
More file actions
52 lines (37 loc) · 1.16 KB
/
Copy pathresolver.c
File metadata and controls
52 lines (37 loc) · 1.16 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "resolver.h"
int main() {
int status;
struct addrinfo hints;
struct addrinfo *servinfo;
int port = 4444;
char ipstr[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IP v4/v6 agnostic
hints.ai_socktype = SOCK_STREAM;
status = getaddrinfo("google.com", NULL, &hints, &servinfo);
if ( status != 0 ) {
fprintf(stderr, "[!] getaddrinfo error : %s\n", gai_strerror(status));
exit(1);
}
struct addrinfo *info;
for (info = servinfo; info != NULL; info = info->ai_next) {
void *addr;
char *ipver;
if (info->ai_family == AF_INET) {
// handles IPv4 Addressing
struct sockaddr_in *ipv4 = (struct sockaddr_in *)info->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else {
// handles IPv6 Addressing
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)info->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert IP to string
inet_ntop(info->ai_family, addr, ipstr, sizeof ipstr);
printf("\t%s: %s\n", ipver, ipstr);
}
freeaddrinfo(servinfo);
return 0;
}