gweb: Remove usage of unused variables
[framework/connectivity/connman.git] / gweb / gresolv.c
1 /*
2  *
3  *  Resolver library with GLib integration
4  *
5  *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <resolv.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netdb.h>
35 #include <arpa/inet.h>
36 #include <arpa/nameser.h>
37
38 #include "gresolv.h"
39
40 struct sort_result {
41         int precedence;
42         int src_scope;
43         int dst_scope;
44         int src_label;
45         int dst_label;
46         gboolean reachable;
47         union {
48                 struct sockaddr sa;
49                 struct sockaddr_in sin;
50                 struct sockaddr_in6 sin6;
51         } src;
52         union {
53                 struct sockaddr sa;
54                 struct sockaddr_in sin;
55                 struct sockaddr_in6 sin6;
56         } dst;
57 };
58
59 struct resolv_query;
60
61 struct resolv_lookup {
62         GResolv *resolv;
63         guint id;
64
65         int nr_results;
66         struct sort_result *results;
67
68         struct resolv_query *ipv4_query;
69         struct resolv_query *ipv6_query;
70
71         guint ipv4_status;
72         guint ipv6_status;
73
74         GResolvResultFunc result_func;
75         gpointer result_data;
76 };
77
78 struct resolv_query {
79         GResolv *resolv;
80
81         guint timeout;
82
83         uint16_t msgid;
84
85         struct resolv_lookup *lookup;
86 };
87
88 struct resolv_nameserver {
89         GResolv *resolv;
90
91         char *address;
92         uint16_t port;
93         unsigned long flags;
94
95         GIOChannel *udp_channel;
96         guint udp_watch;
97 };
98
99 struct _GResolv {
100         gint ref_count;
101
102         guint next_lookup_id;
103         GQueue *lookup_queue;
104         GQueue *query_queue;
105
106         int index;
107         GList *nameserver_list;
108
109         struct __res_state res;
110
111         GResolvDebugFunc debug_func;
112         gpointer debug_data;
113 };
114
115 static inline void debug(GResolv *resolv, const char *format, ...)
116 {
117         char str[256];
118         va_list ap;
119
120         if (resolv->debug_func == NULL)
121                 return;
122
123         va_start(ap, format);
124
125         if (vsnprintf(str, sizeof(str), format, ap) > 0)
126                 resolv->debug_func(str, resolv->debug_data);
127
128         va_end(ap);
129 }
130
131 static void destroy_query(struct resolv_query *query)
132 {
133         if (query->timeout > 0)
134                 g_source_remove(query->timeout);
135
136         g_free(query);
137 }
138
139 static void destroy_lookup(struct resolv_lookup *lookup)
140 {
141         if (lookup->ipv4_query != NULL) {
142                 g_queue_remove(lookup->resolv->query_queue,
143                                                 lookup->ipv4_query);
144                 destroy_query(lookup->ipv4_query);
145         }
146
147         if (lookup->ipv6_query != NULL) {
148                 g_queue_remove(lookup->resolv->query_queue,
149                                                 lookup->ipv6_query);
150                 destroy_query(lookup->ipv6_query);
151         }
152
153         g_free(lookup->results);
154         g_free(lookup);
155 }
156
157 static void find_srcaddr(struct sort_result *res)
158 {
159         socklen_t sl = sizeof(res->src);
160         int fd;
161
162         fd = socket(res->dst.sa.sa_family, SOCK_DGRAM, IPPROTO_IP);
163         if (fd < 0)
164                 return;
165
166         if (connect(fd, &res->dst.sa, sizeof(res->dst)) < 0) {
167                 close(fd);
168                 return;
169         }
170
171         if (getsockname(fd, &res->src.sa, &sl) < 0) {
172                 close(fd);
173                 return;
174         }
175
176         res->reachable = TRUE;
177         close(fd);
178 }
179
180 struct gai_table
181 {
182         unsigned char addr[NS_IN6ADDRSZ];
183         int mask;
184         int value;
185 };
186
187 static const struct gai_table gai_labels[] = {
188         {
189                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
191                 .mask = 128,
192                 .value = 0,
193         }, {
194                 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
196                 .mask = 16,
197                 .value = 2,
198         }, {
199                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
201                 .mask = 96,
202                 .value = 3,
203         }, {
204                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205                           0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
206                 .mask = 96,
207                 .value = 4,
208         }, {
209                 /* Variations from RFC 3484, matching glibc behaviour */
210                 .addr = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
212                 .mask = 10,
213                 .value = 5,
214         }, {
215                 .addr = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
217                 .mask = 7,
218                 .value = 6,
219         }, {
220                 .addr = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
222                 .mask = 32,
223                 .value = 7,
224         }, {
225                 /* catch-all */
226                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
228                 .mask = 0,
229                 .value = 1,
230         }
231 };
232
233 static const struct gai_table gai_precedences[] = {
234         {
235                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
237                 .mask = 128,
238                 .value = 50,
239         }, {
240                 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
242                 .mask = 16,
243                 .value = 30,
244         }, {
245                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
247                 .mask = 96,
248                 .value = 20,
249         }, {
250                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251                           0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
252                 .mask = 96,
253                 .value = 10,
254         }, {
255                 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
256                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
257                 .mask = 0,
258                 .value = 40,
259         }
260 };
261
262 static unsigned char v4mapped[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
263                                     0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
264
265 static gboolean mask_compare(const unsigned char *one,
266                                         const unsigned char *two, int mask)
267 {
268         if (mask > 8) {
269                 if (memcmp(one, two, mask / 8))
270                         return FALSE;
271                 one += mask / 8;
272                 two += mask / 8;
273                 mask %= 8;
274         }
275
276         if (mask && ((*one ^ *two) >> (8 - mask)))
277                 return FALSE;
278
279         return TRUE;
280 }
281
282 static int match_gai_table(struct sockaddr *sa, const struct gai_table *tbl)
283 {
284         struct sockaddr_in *sin = (void *)sa;
285         struct sockaddr_in6 *sin6 = (void *)sa;
286         void *addr;
287
288         if (sa->sa_family == AF_INET) {
289                 addr = v4mapped;
290                 memcpy(v4mapped+12, &sin->sin_addr, NS_INADDRSZ);
291         } else
292                 addr = &sin6->sin6_addr;
293
294         while (1) {
295                 if (mask_compare(addr, tbl->addr, tbl->mask))
296                         return tbl->value;
297                 tbl++;
298         }
299 }
300
301 #define DQUAD(_a,_b,_c,_d) ( ((_a)<<24) | ((_b)<<16) | ((_c)<<8) | (_d) )
302 #define V4MATCH(addr, a,b,c,d, m) ( ((addr) ^ DQUAD(a,b,c,d)) >> (32 - (m)) )
303
304 #define RFC3484_SCOPE_LINK      2
305 #define RFC3484_SCOPE_SITE      5
306 #define RFC3484_SCOPE_GLOBAL    14
307
308 static int addr_scope(struct sockaddr *sa)
309 {
310         if (sa->sa_family == AF_INET) {
311                 struct sockaddr_in *sin = (void *)sa;
312                 guint32 addr = ntohl(sin->sin_addr.s_addr);
313
314                 if (V4MATCH(addr, 169,254,0,0, 16) ||
315                                         V4MATCH(addr, 127,0,0,0, 8))
316                         return RFC3484_SCOPE_LINK;
317
318                 /* Site-local */
319                 if (V4MATCH(addr, 10,0,0,0, 8) ||
320                                 V4MATCH(addr, 172,16,0,0, 12) ||
321                                 V4MATCH(addr, 192,168,0,0, 16))
322                         return RFC3484_SCOPE_SITE;
323
324                 /* Global */
325                 return RFC3484_SCOPE_GLOBAL;
326         } else {
327                 struct sockaddr_in6 *sin6 = (void *)sa;
328
329                 /* Multicast addresses have a 4-bit scope field */
330                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
331                         return sin6->sin6_addr.s6_addr[1] & 0xf;
332
333                 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
334                                 IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
335                         return RFC3484_SCOPE_LINK;
336
337                 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
338                         return RFC3484_SCOPE_SITE;
339
340                 return RFC3484_SCOPE_GLOBAL;
341         }
342 }
343
344 static int rfc3484_compare(const void *__one, const void *__two)
345 {
346         const struct sort_result *one = __one;
347         const struct sort_result *two = __two;
348
349         /* Rule 1: Avoid unusable destinations */
350         if (one->reachable && !two->reachable)
351                 return -1;
352         else if (two->reachable && !one->reachable)
353                 return 1;
354
355         /* Rule 2: Prefer matching scope */
356         if (one->dst_scope == one->src_scope &&
357                                 two->dst_scope != two->src_scope)
358                 return -1;
359         else if (two->dst_scope == two->src_scope &&
360                                 one->dst_scope != one->src_scope)
361                 return 1;
362
363         /* Rule 3: Avoid deprecated addresses */
364
365         /* Rule 4: Prefer home addresses */
366
367         /* Rule 5: Prefer matching label */
368         if (one->dst_label == one->src_label &&
369                                 two->dst_label != two->src_label)
370                 return -1;
371         else if (two->dst_label == two->src_label &&
372                                 one->dst_label != one->src_label)
373                 return 1;
374
375         /* Rule 6: Prefer higher precedence */
376         if (one->precedence > two->precedence)
377                 return -1;
378         else if (two->precedence > one->precedence)
379                 return 1;
380
381         /* Rule 7: Prefer native transport */
382
383         /* Rule 8: Prefer smaller scope */
384         if (one->dst_scope != two->dst_scope)
385                 return one->dst_scope - two->dst_scope;
386
387         /* Rule 9: Use longest matching prefix */
388         if (one->dst.sa.sa_family == AF_INET) {
389                 /*
390                  * Rule 9 is meaningless and counterproductive for Legacy IP
391                  * unless perhaps we can tell that it's actually on the local
392                  * subnet. But we don't (yet) have local interface config
393                  * information, so do nothing here for Legacy IP for now.
394                  */
395         } else {
396                 int i;
397
398                 for (i = 0; i < 4; i++) {
399                         guint32 cmp_one, cmp_two;
400
401                         cmp_one = one->src.sin6.sin6_addr.s6_addr32[i] ^
402                                         one->dst.sin6.sin6_addr.s6_addr32[i];
403                         cmp_two = two->src.sin6.sin6_addr.s6_addr32[i] ^
404                                         two->dst.sin6.sin6_addr.s6_addr32[i];
405
406                         if (!cmp_two && !cmp_one)
407                                 continue;
408
409                         if (cmp_one && !cmp_two)
410                                 return 1;
411                         if (cmp_two && !cmp_one)
412                                 return -1;
413
414                         /* g_bit_storage() is effectively fls() */
415                         cmp_one = g_bit_storage(ntohl(cmp_one));
416                         cmp_two = g_bit_storage(ntohl(cmp_two));
417
418                         if (cmp_one == cmp_two)
419                                 break;
420
421                         return cmp_one - cmp_two;
422                 }
423         }
424
425
426         /* Rule 10: Otherwise, leave the order unchanged */
427         if (one < two)
428                 return -1;
429         else
430                 return 1;
431 }
432
433 static void rfc3484_sort_results(struct resolv_lookup *lookup)
434 {
435         int i;
436
437         for (i = 0; i < lookup->nr_results; i++) {
438                 struct sort_result *res = &lookup->results[i];
439                 find_srcaddr(res);
440                 res->precedence = match_gai_table(&res->dst.sa,
441                                                         gai_precedences);
442                 res->dst_label = match_gai_table(&res->dst.sa, gai_labels);
443                 res->src_label = match_gai_table(&res->src.sa, gai_labels);
444                 res->dst_scope = addr_scope(&res->dst.sa);
445                 res->src_scope = addr_scope(&res->src.sa);
446         }
447
448         qsort(lookup->results, lookup->nr_results,
449                         sizeof(struct sort_result), rfc3484_compare);
450 }
451
452 static void sort_and_return_results(struct resolv_lookup *lookup)
453 {
454         char buf[100];
455         GResolvResultStatus status;
456         char **results = g_try_new0(char *, lookup->nr_results + 1);
457         int i, n = 0;
458
459         if (!results)
460                 return;
461
462         rfc3484_sort_results(lookup);
463
464         for (i = 0; i < lookup->nr_results; i++) {
465                 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
466                         if (inet_ntop(AF_INET,
467                                         &lookup->results[i].dst.sin.sin_addr,
468                                         buf, sizeof(buf)) == NULL)
469                                 continue;
470                 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
471                         if (inet_ntop(AF_INET6,
472                                         &lookup->results[i].dst.sin6.sin6_addr,
473                                         buf, sizeof(buf)) == NULL)
474                                 continue;
475                 } else
476                         continue;
477
478                 results[n++] = strdup(buf);
479         }
480
481         results[n++] = NULL;
482
483         status = lookup->ipv4_status;
484
485         if (status == G_RESOLV_RESULT_STATUS_SUCCESS)
486                 status = lookup->ipv6_status;
487
488         lookup->result_func(status, results, lookup->result_data);
489
490         g_strfreev(results);
491         g_queue_remove(lookup->resolv->lookup_queue, lookup);
492         destroy_lookup(lookup);
493 }
494
495 static gboolean query_timeout(gpointer user_data)
496 {
497         struct resolv_query *query = user_data;
498         struct resolv_lookup *lookup = query->lookup;
499         GResolv *resolv = query->resolv;
500
501         query->timeout = 0;
502
503         if (query == lookup->ipv4_query) {
504                 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
505                 lookup->ipv4_query = NULL;
506         } else if (query == lookup->ipv6_query) {
507                 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
508                 lookup->ipv6_query = NULL;
509         }
510
511         if (lookup->ipv4_query == NULL && lookup->ipv4_query == NULL)
512                 sort_and_return_results(lookup);
513
514         destroy_query(query);
515         g_queue_remove(resolv->query_queue, query);
516
517         return FALSE;
518 }
519
520 static void free_nameserver(struct resolv_nameserver *nameserver)
521 {
522         if (nameserver == NULL)
523                 return;
524
525         if (nameserver->udp_watch > 0)
526                 g_source_remove(nameserver->udp_watch);
527
528         if (nameserver->udp_channel != NULL)
529                 g_io_channel_unref(nameserver->udp_channel);
530
531         g_free(nameserver->address);
532         g_free(nameserver);
533 }
534
535 static void flush_nameservers(GResolv *resolv)
536 {
537         GList *list;
538
539         for (list = g_list_first(resolv->nameserver_list);
540                                         list; list = g_list_next(list))
541                 free_nameserver(list->data);
542
543         g_list_free(resolv->nameserver_list);
544         resolv->nameserver_list = NULL;
545 }
546
547 static int send_query(GResolv *resolv, const unsigned char *buf, int len)
548 {
549         GList *list;
550
551         if (resolv->nameserver_list == NULL)
552                 return -ENOENT;
553
554         for (list = g_list_first(resolv->nameserver_list);
555                                         list; list = g_list_next(list)) {
556                 struct resolv_nameserver *nameserver = list->data;
557                 int sk, sent;
558
559                 if (nameserver->udp_channel == NULL)
560                         continue;
561
562                 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
563
564                 sent = send(sk, buf, len, 0);
565                 if (sent < 0)
566                         continue;
567         }
568
569         return 0;
570 }
571
572 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
573 {
574         const struct resolv_lookup *lookup = a;
575         guint id = GPOINTER_TO_UINT(b);
576
577         if (lookup->id < id)
578                 return -1;
579
580         if (lookup->id > id)
581                 return 1;
582
583         return 0;
584 }
585
586 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
587 {
588         const struct resolv_query *query = a;
589         uint16_t msgid = GPOINTER_TO_UINT(b);
590
591         if (query->msgid < msgid)
592                 return -1;
593
594         if (query->msgid > msgid)
595                 return 1;
596
597         return 0;
598 }
599
600 static void add_result(struct resolv_lookup *lookup, int family,
601                                                         const void *data)
602 {
603         int n = lookup->nr_results++;
604         lookup->results = g_realloc(lookup->results,
605                                         sizeof(struct sort_result) * (n + 1));
606
607         memset(&lookup->results[n], 0, sizeof(struct sort_result));
608
609         lookup->results[n].dst.sa.sa_family = family;
610         if (family == AF_INET)
611                 memcpy(&lookup->results[n].dst.sin.sin_addr,
612                                                 data, NS_INADDRSZ);
613         else
614                 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
615                                                 data, NS_IN6ADDRSZ);
616 }
617
618 static void parse_response(struct resolv_nameserver *nameserver,
619                                         const unsigned char *buf, int len)
620 {
621         GResolv *resolv = nameserver->resolv;
622         GResolvResultStatus status;
623         struct resolv_query *query;
624         struct resolv_lookup *lookup;
625         GList *list;
626         ns_msg msg;
627         ns_rr rr;
628         int i, rcode, count;
629
630         debug(resolv, "response from %s", nameserver->address);
631
632         ns_initparse(buf, len, &msg);
633
634         rcode = ns_msg_getflag(msg, ns_f_rcode);
635         count = ns_msg_count(msg, ns_s_an);
636
637         debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
638                                         ns_msg_id(msg), rcode, count);
639
640         switch (rcode) {
641         case 0:
642                 status = G_RESOLV_RESULT_STATUS_SUCCESS;
643                 break;
644         case 1:
645                 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
646                 break;
647         case 2:
648                 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
649                 break;
650         case 3:
651                 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
652                 break;
653         case 4:
654                 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
655                 break;
656         case 5:
657                 status = G_RESOLV_RESULT_STATUS_REFUSED;
658                 break;
659         default:
660                 status = G_RESOLV_RESULT_STATUS_ERROR;
661                 break;
662         }
663
664         list = g_queue_find_custom(resolv->query_queue,
665                         GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
666         if (!list)
667                 return;
668
669         query = list->data;
670         lookup = query->lookup;
671
672         if (query == lookup->ipv6_query) {
673                 lookup->ipv6_status = status;
674                 lookup->ipv6_query = NULL;
675         } else if (query == lookup->ipv4_query) {
676                 lookup->ipv4_status = status;
677                 lookup->ipv4_query = NULL;
678         }
679
680         for (i = 0; i < count; i++) {
681                 ns_parserr(&msg, ns_s_an, i, &rr);
682
683                 if (ns_rr_class(rr) != ns_c_in)
684                         continue;
685
686                 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
687                                 offsetof(struct sockaddr_in6, sin6_flowinfo));
688
689                 if (ns_rr_type(rr) == ns_t_a &&
690                                         ns_rr_rdlen(rr) == NS_INADDRSZ) {
691                         add_result(lookup, AF_INET, ns_rr_rdata(rr));
692                 } else if (ns_rr_type(rr) == ns_t_aaaa &&
693                                         ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
694                         add_result(lookup, AF_INET6, ns_rr_rdata(rr));
695                 }
696         }
697
698         if (lookup->ipv4_query == NULL && lookup->ipv6_query == NULL)
699                 sort_and_return_results(lookup);
700
701         destroy_query(query);
702         g_queue_remove(resolv->query_queue, query);
703 }
704
705 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
706                                                         gpointer user_data)
707 {
708         struct resolv_nameserver *nameserver = user_data;
709         unsigned char buf[4096];
710         int sk, len;
711
712         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
713                 nameserver->udp_watch = 0;
714                 return FALSE;
715         }
716
717         sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
718
719         len = recv(sk, buf, sizeof(buf), 0);
720         if (len < 12)
721                 return TRUE;
722
723         parse_response(nameserver, buf, len);
724
725         return TRUE;
726 }
727
728 static int connect_udp_channel(struct resolv_nameserver *nameserver)
729 {
730         struct addrinfo hints, *rp;
731         char portnr[6];
732         int err, sk;
733
734         memset(&hints, 0, sizeof(hints));
735         hints.ai_family = AF_UNSPEC;
736         hints.ai_socktype = SOCK_DGRAM;
737         hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
738
739         sprintf(portnr, "%d", nameserver->port);
740         err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
741         if (err)
742                 return -EINVAL;
743
744         /*
745          * Do not blindly copy this code elsewhere; it doesn't loop over the
746          * results using ->ai_next as it should. That's OK in *this* case
747          * because it was a numeric lookup; we *know* there's only one.
748          */
749         if (!rp)
750                 return -EINVAL;
751
752         sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
753         if (sk < 0) {
754                 freeaddrinfo(rp);
755                 return -EIO;
756         }
757
758         if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
759                 close(sk);
760                 freeaddrinfo(rp);
761                 return -EIO;
762         }
763
764         freeaddrinfo(rp);
765
766         nameserver->udp_channel = g_io_channel_unix_new(sk);
767         if (nameserver->udp_channel == NULL) {
768                 close(sk);
769                 return -ENOMEM;
770         }
771
772         g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
773
774         nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
775                                 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
776                                 received_udp_data, nameserver);
777
778         return 0;
779 }
780
781 GResolv *g_resolv_new(int index)
782 {
783         GResolv *resolv;
784
785         if (index < 0)
786                 return NULL;
787
788         resolv = g_try_new0(GResolv, 1);
789         if (resolv == NULL)
790                 return NULL;
791
792         resolv->ref_count = 1;
793
794         resolv->next_lookup_id = 1;
795
796         resolv->query_queue = g_queue_new();
797         if (resolv->query_queue == NULL) {
798                 g_free(resolv);
799                 return NULL;
800         }
801
802         resolv->lookup_queue = g_queue_new();
803         if (resolv->lookup_queue == NULL) {
804                 g_queue_free(resolv->query_queue);
805                 g_free(resolv);
806                 return NULL;
807         }
808
809         resolv->index = index;
810         resolv->nameserver_list = NULL;
811
812         res_ninit(&resolv->res);
813
814         return resolv;
815 }
816
817 GResolv *g_resolv_ref(GResolv *resolv)
818 {
819         if (resolv == NULL)
820                 return NULL;
821
822         g_atomic_int_inc(&resolv->ref_count);
823
824         return resolv;
825 }
826
827 void g_resolv_unref(GResolv *resolv)
828 {
829         struct resolv_query *query;
830
831         if (resolv == NULL)
832                 return;
833
834         if (g_atomic_int_dec_and_test(&resolv->ref_count) == FALSE)
835                 return;
836
837         while ((query = g_queue_pop_head(resolv->query_queue)))
838                 destroy_query(query);
839
840         g_queue_free(resolv->query_queue);
841         g_queue_free(resolv->lookup_queue);
842
843         flush_nameservers(resolv);
844
845         res_nclose(&resolv->res);
846
847         g_free(resolv);
848 }
849
850 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
851                                                 gpointer user_data)
852 {
853         if (resolv == NULL)
854                 return;
855
856         resolv->debug_func = func;
857         resolv->debug_data = user_data;
858 }
859
860 gboolean g_resolv_add_nameserver(GResolv *resolv, const char *address,
861                                         uint16_t port, unsigned long flags)
862 {
863         struct resolv_nameserver *nameserver;
864
865         if (resolv == NULL)
866                 return FALSE;
867
868         nameserver = g_try_new0(struct resolv_nameserver, 1);
869         if (nameserver == NULL)
870                 return FALSE;
871
872         nameserver->address = g_strdup(address);
873         nameserver->port = port;
874         nameserver->flags = flags;
875
876         if (connect_udp_channel(nameserver) < 0) {
877                 free_nameserver(nameserver);
878                 return FALSE;
879         }
880
881         nameserver->resolv = resolv;
882
883         resolv->nameserver_list = g_list_append(resolv->nameserver_list,
884                                                                 nameserver);
885
886         debug(resolv, "setting nameserver %s", address);
887
888         return TRUE;
889 }
890
891 void g_resolv_flush_nameservers(GResolv *resolv)
892 {
893         if (resolv == NULL)
894                 return;
895
896         flush_nameservers(resolv);
897 }
898
899 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
900 {
901         struct resolv_query *query = g_try_new0(struct resolv_query, 1);
902         unsigned char buf[4096];
903         int len;
904
905         if (query == NULL)
906                 return -ENOMEM;
907
908         len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
909                                         NULL, 0, NULL, buf, sizeof(buf));
910
911         query->msgid = buf[0] << 8 | buf[1];
912
913         if (send_query(lookup->resolv, buf, len) < 0) {
914                 g_free(query);
915                 return -EIO;
916         }
917
918         query->resolv = lookup->resolv;
919         query->lookup = lookup;
920
921         g_queue_push_tail(lookup->resolv->query_queue, query);
922
923         query->timeout = g_timeout_add_seconds(5, query_timeout, query);
924
925         if (type == ns_t_aaaa)
926                 lookup->ipv6_query = query;
927         else
928                 lookup->ipv4_query = query;
929
930         return 0;
931 }
932
933 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
934                                 GResolvResultFunc func, gpointer user_data)
935 {
936         struct resolv_lookup *lookup;
937
938         debug(resolv, "lookup hostname %s", hostname);
939
940         if (resolv == NULL)
941                 return 0;
942
943         if (resolv->nameserver_list == NULL) {
944                 int i;
945
946                 for (i = 0; i < resolv->res.nscount; i++) {
947                         char buf[100];
948                         int family = resolv->res.nsaddr_list[i].sin_family;
949                         void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
950
951                         if (family != AF_INET &&
952                                         resolv->res._u._ext.nsaddrs[i]) {
953                                 family = AF_INET6;
954                                 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
955                         }
956
957                         if (family != AF_INET && family != AF_INET6)
958                                 continue;
959
960                         if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
961                                 g_resolv_add_nameserver(resolv, buf, 53, 0);
962                 }
963
964                 if (resolv->nameserver_list == NULL)
965                         g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
966         }
967
968         lookup = g_try_new0(struct resolv_lookup, 1);
969         if (lookup == NULL)
970                 return 0;
971
972         lookup->resolv = resolv;
973         lookup->result_func = func;
974         lookup->result_data = user_data;
975         lookup->id = resolv->next_lookup_id++;
976
977         if (add_query(lookup, hostname, ns_t_a)) {
978                 g_free(lookup);
979                 return -EIO;
980         }
981
982         if (add_query(lookup, hostname, ns_t_aaaa)) {
983                 destroy_query(lookup->ipv4_query);
984                 g_queue_remove(resolv->query_queue, lookup->ipv4_query);
985                 g_free(lookup);
986                 return -EIO;
987         }
988
989         g_queue_push_tail(resolv->lookup_queue, lookup);
990         return lookup->id;
991 }
992
993 gboolean g_resolv_cancel_lookup(GResolv *resolv, guint id)
994 {
995         GList *list;
996
997         list = g_queue_find_custom(resolv->lookup_queue,
998                                 GUINT_TO_POINTER(id), compare_lookup_id);
999
1000         if (list == NULL)
1001                 return FALSE;
1002
1003         destroy_lookup(list->data);
1004         g_queue_remove(resolv->query_queue, list->data);
1005
1006         return TRUE;
1007 }