Debian Codegraph @

src/canl_dns.c

raw
1 #include "canl_locl.h"
2
3 static int decrement_timeout(struct timeval *timeout, struct timeval before, struct timeval after);
4
5 //#if ARES_VERSION >= 0x010500
6 static void callback_ares_gethostbyname(void *arg, int status, int timeouts, struct hostent *h)
7 //#else
8 //static void callback_ares_gethostbyname(void *arg, int status, struct hostent *h)
9 //#endif
10 {
11 asyn_result *arp = (asyn_result *) arg;
12 int n_addr = 0;
13 int i = 0;
14
15 switch (status) {
16 case ARES_SUCCESS:
17 if (h == NULL || h->h_addr_list[0] == NULL){
18 arp->err = NO_DATA;
19 break;
20 }
21 /*how many addresses are there in h->h_addr_list*/
22 while (h->h_addr_list[n_addr])
23 n_addr++;
24
25 arp->ent->h_addr_list = (char **) calloc((n_addr+1), sizeof(char *));
26 if (arp->ent->h_addr_list == NULL) {
27 arp->err = NETDB_INTERNAL;
28 break;
29 }
30 for (i = 0; i < n_addr; i++) {
31 arp->ent->h_addr_list[i] = malloc(h->h_length);
32 if (arp->ent->h_addr_list[i] == NULL) {
33 free_hostent (arp->ent);
34 arp->ent = NULL;
35 arp->err = NETDB_INTERNAL;
36 break;
37 }
38 memcpy(arp->ent->h_addr_list[i], h->h_addr_list[i],
39 h->h_length);
40 }
41 /* rest of h members might be assigned here(name,aliases), not necessery now */
42 arp->ent->h_addr_list[n_addr] = NULL;
43 arp->ent->h_addrtype = h->h_addrtype;
44 arp->ent->h_length = h->h_length;
45 arp->err = NETDB_SUCCESS;
46 break;
47 case ARES_EBADNAME:
48 case ARES_ENOTFOUND:
49 case ARES_ENODATA:
50 arp->err = HOST_NOT_FOUND;
51 break;
52 case ARES_ENOTIMP:
53 arp->err = NO_RECOVERY;
54 break;
55 case ARES_ENOMEM:
56 case ARES_EDESTRUCTION:
57 default:
58 arp->err = NETDB_INTERNAL;
59 break;
60 }
61 }
62
63 static int decrement_timeout(struct timeval *timeout, struct timeval before, struct timeval after)
64 {
65 (*timeout).tv_sec = (*timeout).tv_sec - (after.tv_sec - before.tv_sec);
66 (*timeout).tv_usec = (*timeout).tv_usec - (after.tv_usec - before.tv_usec);
67 while ( (*timeout).tv_usec < 0) {
68 (*timeout).tv_sec--;
69 (*timeout).tv_usec += 1000000;
70 }
71 if ( ((*timeout).tv_sec < 0) || (((*timeout).tv_sec == 0) && ((*timeout).tv_usec == 0)) ) return(1);
72 else return(0);
73 }
74
75
76 void free_hostent(struct hostent *h)
77 {
78 int i;
79
80 if (h) {
81 if (h->h_name) free(h->h_name);
82 if (h->h_aliases) {
83 for (i=0; h->h_aliases[i]; i++) free(h->h_aliases[i]);
84 free(h->h_aliases);
85 }
86 if (h->h_addr_list) {
87 for (i=0; h->h_addr_list[i]; i++) free(h->h_addr_list[i]);
88 free(h->h_addr_list);
89 }
90 free(h);
91 }
92 }
93
94 int canl_asyn_getservbyname(int a_family, asyn_result *ares_result,char const *name,
95 struct timeval *timeout)
96 {
97 int err;
98 ares_channel channel;
99 int nfds;
100 fd_set readers, writers;
101 struct timeval tv, *tvp;
102 struct timeval start_time,check_time;
103
104 /* start timer */
105 gettimeofday(&start_time,0);
106
107 /* ares init */
108 if ( ares_init(&channel) != ARES_SUCCESS )
109 return(NETDB_INTERNAL); //TODO return value...
110
111 /* query DNS server asynchronously */
112 ares_gethostbyname(channel, name, a_family, callback_ares_gethostbyname,
113 (void *) ares_result);
114
115 /* wait for result */
116 while (1) {
117 FD_ZERO(&readers);
118 FD_ZERO(&writers);
119 nfds = ares_fds(channel, &readers, &writers);
120 if (nfds == 0)
121 break;
122
123 gettimeofday(&check_time,0);
124 if (timeout && decrement_timeout(timeout, start_time, check_time)) {
125 ares_destroy(channel);
126 return(TRY_AGAIN);
127 }
128 start_time = check_time;
129
130 tvp = ares_timeout(channel, timeout, &tv);
131
132 switch ( select(nfds, &readers, &writers, NULL, tvp) ) {
133 case -1: if (errno != EINTR) {
134 ares_destroy(channel);
135 return NETDB_INTERNAL;
136 } else
137 continue;
138 case 0:
139 FD_ZERO(&readers);
140 FD_ZERO(&writers);
141 /* fallthrough */
142 default: ares_process(channel, &readers, &writers);
143 }
144 }
145 err = ares_result->err;
146
147 ares_destroy(channel);
148
149 return err;
150 }