-
Notifications
You must be signed in to change notification settings - Fork 0
/
ares_test.c
179 lines (137 loc) · 4.47 KB
/
ares_test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <ares.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
void ares_query_cb(void *arg, int status, int timeouts, struct hostent *host)
{
printf("ares_query_cb\n");
if(!host || status != ARES_SUCCESS){
printf("Failed to lookup %s\n", ares_strerror(status));
return;
}
printf("Found address name %s\n", host->h_name);
char ip[INET6_ADDRSTRLEN];
int i = 0;
for (i = 0; host->h_addr_list[i]; ++i) {
inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip));
printf("%s\n", ip);
}
}
static void wait_ares_select(ares_channel channel)
{
while(1) {
struct timeval *tvp, tv;
fd_set read_fds, write_fds;
int nfds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
nfds = ares_fds(channel, &read_fds, &write_fds);
if(nfds == 0){
break;
}
tvp = ares_timeout(channel, NULL, &tv);
select(nfds, &read_fds, &write_fds, NULL, tvp);
ares_process(channel, &read_fds, &write_fds);
}
}
static void wait_ares_epoll(ares_channel channel)
{
struct epoll_event epoll_events[ARES_GETSOCK_MAXNUM];
ares_socket_t sockets[ARES_GETSOCK_MAXNUM];
int socket_count;
int event_count;
int epoll_fd;
// fd_set readers;
// fd_set writers;
int result;
int index;
// int nfds;
struct timeval *tvp;
struct timeval tv;
while (1) {
result = ares_getsock(channel, sockets, ARES_GETSOCK_MAXNUM);
printf("[%d]\n", result);
if (result == 0){
break;
}
// nfds = ares_fds(channel, &readers, &writers);
epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
fprintf(stderr, "Failed to create epoll file descriptor\n");
break;
}
tvp = ares_timeout(channel, NULL, &tv);
for (index = 0 ; ; index++) {
epoll_events[index].events = 0;
// epoll_events[index].fd = -1;
if (0 != ARES_GETSOCK_READABLE(result, index)) {
epoll_events[index].events |= EPOLLIN;
}
if (0 != ARES_GETSOCK_WRITABLE(result, index)) {
epoll_events[index].events |= EPOLLOUT;
}
if (0 != epoll_events[index].events) {
epoll_events[index].events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
epoll_events[index].data.fd = sockets[index];
printf("Added %d to epoll events\n", sockets[index]);
if (0 != epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockets[index], &epoll_events[index])) {
fprintf(stderr, "Failed to add file descriptor to epoll\n");
break;
}
}
else
{
break;
}
}
socket_count = index;
if (0 == socket_count) {
break;
}
memset(epoll_events, 0, sizeof(epoll_events));
event_count = epoll_wait(epoll_fd, epoll_events, socket_count, 30000); //change timeout
if (close(epoll_fd)) {
fprintf(stderr, "Failed to close epoll file descriptor\n");
break;
}
epoll_fd = -1;
for (index = 0 ; index < event_count ; index++) {
printf("Calling ares_process for fd %d\n", epoll_events[index].data.fd);
ares_process_fd(channel, epoll_events[index].data.fd, epoll_events[index].data.fd);
}
}
if (-1 != epoll_fd) {
if (close(epoll_fd)) {
fprintf(stderr, "Failed to close epoll file descriptor\n");
}
epoll_fd = -1;
}
}
int main(int argc, char **argv)
{
int result;
result = ares_library_init(ARES_LIB_INIT_ALL);
if (0 != result) {
printf("error: ares_library_init\n");
return -1;
}
{
ares_channel query_channel;
result = ares_init(&query_channel);
if (ARES_SUCCESS != result) {
printf("error: ares_init\n");
return -2;
}
ares_gethostbyname(query_channel, "pedo.com", AF_INET, ares_query_cb, NULL);
// sleep(10);
// wait_ares_select(query_channel);
wait_ares_epoll(query_channel);
ares_destroy(query_channel);
}
return 0;
}