-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
62 lines (50 loc) · 1.5 KB
/
main.c
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
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[]) {
struct addrinfo hint, *res, *p;
int err;
if(argc != 2) {
fprintf(stderr," usage: iplookup hostname\n ex: iplookup ddo.me\n");
return 1;
}
memset(&hint, 0, sizeof hint);
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
// look up
err = getaddrinfo(argv[1], "http", &hint, &res);
if(err != 0) {
printf("ERR: %s\n", gai_strerror(err));
return err;
}
// list all ips
void *addr;
struct sockaddr_in *ipv4;
struct sockaddr_in6 *ipv6;
char ipstr[INET6_ADDRSTRLEN];
for(p = res ;p != NULL; p = p->ai_next) {
// ipv4
if(p->ai_family == AF_INET) {
// convert generic addr to internet addr
ipv4 = (struct sockaddr_in *)p->ai_addr;
// point to binary addr for convert to str later
addr = &(ipv4->sin_addr);
// ipv6
} else {
// convert generic addr to internet addr
ipv6 = (struct sockaddr_in6 *)p->ai_addr;
// point to binary addr for convert to str later
addr = &(ipv6->sin6_addr);
}
// convert binary addr to str
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf("%s\n", ipstr);
}
// don't forget to free the addrinfo
freeaddrinfo(res);
return 0;
}