blob: d52c93ff8f5dc9f2d4a22a3bda4d551956e6aa86 (
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
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include <netdb.h>
#include <assert.h>
#include <stddef.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main() {
struct addrinfo *res = NULL;
struct addrinfo hints = {0};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
int ret = getaddrinfo(NULL, "443", &hints, &res);
assert(ret == 0);
struct sockaddr_in *addr = (struct sockaddr_in*)(res[0].ai_addr);
assert(addr->sin_port == htons(443));
assert(res[0].ai_socktype == SOCK_STREAM);
assert(res[0].ai_protocol == IPPROTO_TCP);
freeaddrinfo(res);
res = NULL;
/* check we can resolve any domain */
ret = getaddrinfo("example.net", NULL, &hints, &res);
assert(ret == 0);
freeaddrinfo(res);
res = NULL;
hints.ai_flags = AI_NUMERICHOST;
ret = getaddrinfo("10.10.10.10", NULL, &hints, &res);
assert(ret == 0);
addr = (struct sockaddr_in*)res[0].ai_addr;
assert((addr->sin_addr.s_addr & 0xFF) == 10);
assert(((addr->sin_addr.s_addr >> 8) & 0xFF) == 10);
assert(((addr->sin_addr.s_addr >> 16) & 0xFF) == 10);
assert(((addr->sin_addr.s_addr >> 24) & 0xFF) == 10);
freeaddrinfo(res);
res = NULL;
ret = getaddrinfo("example.net", NULL, &hints, &res);
assert(ret == EAI_NONAME);
freeaddrinfo(res);
return 0;
}
|