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