a7bf87a144356ec870df9b2f1f3bbb039d86d515
[platform/upstream/connman.git] / src / dnsproxy.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2014  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 <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <fcntl.h>
36 #include <netdb.h>
37 #include <resolv.h>
38 #include <gweb/gresolv.h>
39
40 #include <glib.h>
41
42 #include "connman.h"
43
44 #define debug(fmt...) do { } while (0)
45
46 #if __BYTE_ORDER == __LITTLE_ENDIAN
47 struct domain_hdr {
48         uint16_t id;
49         uint8_t rd:1;
50         uint8_t tc:1;
51         uint8_t aa:1;
52         uint8_t opcode:4;
53         uint8_t qr:1;
54         uint8_t rcode:4;
55         uint8_t z:3;
56         uint8_t ra:1;
57         uint16_t qdcount;
58         uint16_t ancount;
59         uint16_t nscount;
60         uint16_t arcount;
61 } __attribute__ ((packed));
62 #elif __BYTE_ORDER == __BIG_ENDIAN
63 struct domain_hdr {
64         uint16_t id;
65         uint8_t qr:1;
66         uint8_t opcode:4;
67         uint8_t aa:1;
68         uint8_t tc:1;
69         uint8_t rd:1;
70         uint8_t ra:1;
71         uint8_t z:3;
72         uint8_t rcode:4;
73         uint16_t qdcount;
74         uint16_t ancount;
75         uint16_t nscount;
76         uint16_t arcount;
77 } __attribute__ ((packed));
78 #else
79 #error "Unknown byte order"
80 #endif
81
82 struct qtype_qclass {
83         uint16_t qtype;
84         uint16_t qclass;
85 } __attribute__ ((packed));
86
87 struct partial_reply {
88         uint16_t len;
89         uint16_t received;
90         unsigned char buf[];
91 };
92
93 struct server_data {
94         int index;
95         GList *domains;
96         char *server;
97         struct sockaddr *server_addr;
98         socklen_t server_addr_len;
99         int protocol;
100         GIOChannel *channel;
101         guint watch;
102         guint timeout;
103         bool enabled;
104         bool connected;
105         struct partial_reply *incoming_reply;
106 };
107
108 struct request_data {
109         union {
110                 struct sockaddr_in6 __sin6; /* Only for the length */
111                 struct sockaddr sa;
112         };
113         socklen_t sa_len;
114         int client_sk;
115         int protocol;
116         int family;
117         guint16 srcid;
118         guint16 dstid;
119         guint16 altid;
120         guint timeout;
121         guint watch;
122         guint numserv;
123         guint numresp;
124         gpointer request;
125         gsize request_len;
126         gpointer name;
127         gpointer resp;
128         gsize resplen;
129         struct listener_data *ifdata;
130         bool append_domain;
131 };
132
133 struct listener_data {
134         int index;
135
136         GIOChannel *udp4_listener_channel;
137         GIOChannel *tcp4_listener_channel;
138         guint udp4_listener_watch;
139         guint tcp4_listener_watch;
140
141         GIOChannel *udp6_listener_channel;
142         GIOChannel *tcp6_listener_channel;
143         guint udp6_listener_watch;
144         guint tcp6_listener_watch;
145 };
146
147 /*
148  * The TCP client requires some extra handling as we need to
149  * be prepared to receive also partial DNS requests.
150  */
151 struct tcp_partial_client_data {
152         int family;
153         struct listener_data *ifdata;
154         GIOChannel *channel;
155         guint watch;
156         unsigned char *buf;
157         unsigned int buf_end;
158         guint timeout;
159 };
160
161 struct cache_data {
162         time_t inserted;
163         time_t valid_until;
164         time_t cache_until;
165         int timeout;
166         uint16_t type;
167         uint16_t answers;
168         unsigned int data_len;
169         unsigned char *data; /* contains DNS header + body */
170 };
171
172 struct cache_entry {
173         char *key;
174         bool want_refresh;
175         int hits;
176         struct cache_data *ipv4;
177         struct cache_data *ipv6;
178 };
179
180 struct domain_question {
181         uint16_t type;
182         uint16_t class;
183 } __attribute__ ((packed));
184
185 struct domain_rr {
186         uint16_t type;
187         uint16_t class;
188         uint32_t ttl;
189         uint16_t rdlen;
190 } __attribute__ ((packed));
191
192 /*
193  * Max length of the DNS TCP packet.
194  */
195 #define TCP_MAX_BUF_LEN 4096
196
197 /*
198  * We limit how long the cached DNS entry stays in the cache.
199  * By default the TTL (time-to-live) of the DNS response is used
200  * when setting the cache entry life time. The value is in seconds.
201  */
202 #define MAX_CACHE_TTL (60 * 30)
203 /*
204  * Also limit the other end, cache at least for 30 seconds.
205  */
206 #define MIN_CACHE_TTL (30)
207
208 /*
209  * We limit the cache size to some sane value so that cached data does
210  * not occupy too much memory. Each cached entry occupies on average
211  * about 100 bytes memory (depending on DNS name length).
212  * Example: caching www.connman.net uses 97 bytes memory.
213  * The value is the max amount of cached DNS responses (count).
214  */
215 #define MAX_CACHE_SIZE 256
216
217 static int cache_size;
218 static GHashTable *cache;
219 static int cache_refcount;
220 static GSList *server_list = NULL;
221 static GSList *request_list = NULL;
222 static GHashTable *listener_table = NULL;
223 static time_t next_refresh;
224 static GHashTable *partial_tcp_req_table;
225 static guint cache_timer = 0;
226
227 static guint16 get_id(void)
228 {
229         uint64_t rand;
230
231         __connman_util_get_random(&rand);
232
233         return rand;
234 }
235
236 static int protocol_offset(int protocol)
237 {
238         switch (protocol) {
239         case IPPROTO_UDP:
240                 return 0;
241
242         case IPPROTO_TCP:
243                 return 2;
244
245         default:
246                 return -EINVAL;
247         }
248
249 }
250
251 /*
252  * There is a power and efficiency benefit to have entries
253  * in our cache expire at the same time. To this extend,
254  * we round down the cache valid time to common boundaries.
255  */
256 static time_t round_down_ttl(time_t end_time, int ttl)
257 {
258         if (ttl < 15)
259                 return end_time;
260
261         /* Less than 5 minutes, round to 10 second boundary */
262         if (ttl < 300) {
263                 end_time = end_time / 10;
264                 end_time = end_time * 10;
265         } else { /* 5 or more minutes, round to 30 seconds */
266                 end_time = end_time / 30;
267                 end_time = end_time * 30;
268         }
269         return end_time;
270 }
271
272 static struct request_data *find_request(guint16 id)
273 {
274         GSList *list;
275
276         for (list = request_list; list; list = list->next) {
277                 struct request_data *req = list->data;
278
279                 if (req->dstid == id || req->altid == id)
280                         return req;
281         }
282
283         return NULL;
284 }
285
286 static struct server_data *find_server(int index,
287                                         const char *server,
288                                                 int protocol)
289 {
290         GSList *list;
291
292         debug("index %d server %s proto %d", index, server, protocol);
293
294         for (list = server_list; list; list = list->next) {
295                 struct server_data *data = list->data;
296
297                 if (index < 0 && data->index < 0 &&
298                                 g_str_equal(data->server, server) &&
299                                 data->protocol == protocol)
300                         return data;
301
302                 if (index < 0 ||
303                                 data->index < 0 || !data->server)
304                         continue;
305
306                 if (data->index == index &&
307                                 g_str_equal(data->server, server) &&
308                                 data->protocol == protocol)
309                         return data;
310         }
311
312         return NULL;
313 }
314
315 /* we can keep using the same resolve's */
316 static GResolv *ipv4_resolve;
317 static GResolv *ipv6_resolve;
318
319 static void dummy_resolve_func(GResolvResultStatus status,
320                                         char **results, gpointer user_data)
321 {
322 }
323
324 /*
325  * Refresh a DNS entry, but also age the hit count a bit */
326 static void refresh_dns_entry(struct cache_entry *entry, char *name)
327 {
328         int age = 1;
329
330         if (!ipv4_resolve) {
331                 ipv4_resolve = g_resolv_new(0);
332                 g_resolv_set_address_family(ipv4_resolve, AF_INET);
333                 g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
334         }
335
336         if (!ipv6_resolve) {
337                 ipv6_resolve = g_resolv_new(0);
338                 g_resolv_set_address_family(ipv6_resolve, AF_INET6);
339                 g_resolv_add_nameserver(ipv6_resolve, "::1", 53, 0);
340         }
341
342         if (!entry->ipv4) {
343                 debug("Refreshing A record for %s", name);
344                 g_resolv_lookup_hostname(ipv4_resolve, name,
345                                         dummy_resolve_func, NULL);
346                 age = 4;
347         }
348
349         if (!entry->ipv6) {
350                 debug("Refreshing AAAA record for %s", name);
351                 g_resolv_lookup_hostname(ipv6_resolve, name,
352                                         dummy_resolve_func, NULL);
353                 age = 4;
354         }
355
356         entry->hits -= age;
357         if (entry->hits < 0)
358                 entry->hits = 0;
359 }
360
361 static int dns_name_length(unsigned char *buf)
362 {
363         if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
364                 return 2;
365         return strlen((char *)buf) + 1;
366 }
367
368 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
369 {
370         unsigned char *c;
371         uint16_t w;
372         int l;
373
374         /* skip the header */
375         c = buf + 12;
376         len -= 12;
377
378         /* skip the query, which is a name and 2 16 bit words */
379         l = dns_name_length(c);
380         c += l;
381         len -= l;
382         c += 4;
383         len -= 4;
384
385         /* now we get the answer records */
386
387         while (len > 0) {
388                 /* first a name */
389                 l = dns_name_length(c);
390                 c += l;
391                 len -= l;
392                 if (len < 0)
393                         break;
394                 /* then type + class, 2 bytes each */
395                 c += 4;
396                 len -= 4;
397                 if (len < 0)
398                         break;
399
400                 /* now the 4 byte TTL field */
401                 c[0] = new_ttl >> 24 & 0xff;
402                 c[1] = new_ttl >> 16 & 0xff;
403                 c[2] = new_ttl >> 8 & 0xff;
404                 c[3] = new_ttl & 0xff;
405                 c += 4;
406                 len -= 4;
407                 if (len < 0)
408                         break;
409
410                 /* now the 2 byte rdlen field */
411                 w = c[0] << 8 | c[1];
412                 c += w + 2;
413                 len -= w + 2;
414         }
415 }
416
417 static void send_cached_response(int sk, unsigned char *buf, int len,
418                                 const struct sockaddr *to, socklen_t tolen,
419                                 int protocol, int id, uint16_t answers, int ttl)
420 {
421         struct domain_hdr *hdr;
422         unsigned char *ptr = buf;
423         int err, offset, dns_len, adj_len = len - 2;
424
425         /*
426          * The cached packet contains always the TCP offset (two bytes)
427          * so skip them for UDP.
428          */
429         switch (protocol) {
430         case IPPROTO_UDP:
431                 ptr += 2;
432                 len -= 2;
433                 dns_len = len;
434                 offset = 0;
435                 break;
436         case IPPROTO_TCP:
437                 offset = 2;
438                 dns_len = ptr[0] * 256 + ptr[1];
439                 break;
440         default:
441                 return;
442         }
443
444         if (len < 12)
445                 return;
446
447         hdr = (void *) (ptr + offset);
448
449         hdr->id = id;
450         hdr->qr = 1;
451         hdr->rcode = ns_r_noerror;
452         hdr->ancount = htons(answers);
453         hdr->nscount = 0;
454         hdr->arcount = 0;
455
456         /* if this is a negative reply, we are authoritative */
457         if (answers == 0)
458                 hdr->aa = 1;
459         else
460                 update_cached_ttl((unsigned char *)hdr, adj_len, ttl);
461
462         debug("sk %d id 0x%04x answers %d ptr %p length %d dns %d",
463                 sk, hdr->id, answers, ptr, len, dns_len);
464
465         err = sendto(sk, ptr, len, MSG_NOSIGNAL, to, tolen);
466         if (err < 0) {
467                 connman_error("Cannot send cached DNS response: %s",
468                                 strerror(errno));
469                 return;
470         }
471
472         if (err != len || (dns_len != (len - 2) && protocol == IPPROTO_TCP) ||
473                                 (dns_len != len && protocol == IPPROTO_UDP))
474                 debug("Packet length mismatch, sent %d wanted %d dns %d",
475                         err, len, dns_len);
476 }
477
478 static void send_response(int sk, unsigned char *buf, size_t len,
479                                 const struct sockaddr *to, socklen_t tolen,
480                                 int protocol)
481 {
482         struct domain_hdr *hdr;
483         int err, offset = protocol_offset(protocol);
484
485         debug("sk %d", sk);
486
487         if (offset < 0)
488                 return;
489
490         if (len < sizeof(*hdr) + offset)
491                 return;
492
493         hdr = (void *) (buf + offset);
494         if (offset) {
495                 buf[0] = 0;
496                 buf[1] = sizeof(*hdr);
497         }
498
499         debug("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
500
501         hdr->qr = 1;
502         hdr->rcode = ns_r_servfail;
503
504         hdr->qdcount = 0;
505         hdr->ancount = 0;
506         hdr->nscount = 0;
507         hdr->arcount = 0;
508
509         err = sendto(sk, buf, sizeof(*hdr) + offset, MSG_NOSIGNAL, to, tolen);
510         if (err < 0) {
511                 connman_error("Failed to send DNS response to %d: %s",
512                                 sk, strerror(errno));
513                 return;
514         }
515 }
516
517 static int get_req_udp_socket(struct request_data *req)
518 {
519         GIOChannel *channel;
520
521         if (req->family == AF_INET)
522                 channel = req->ifdata->udp4_listener_channel;
523         else
524                 channel = req->ifdata->udp6_listener_channel;
525
526         if (!channel)
527                 return -1;
528
529         return g_io_channel_unix_get_fd(channel);
530 }
531
532 static void destroy_request_data(struct request_data *req)
533 {
534         if (req->timeout > 0)
535                 g_source_remove(req->timeout);
536
537         g_free(req->resp);
538         g_free(req->request);
539         g_free(req->name);
540         g_free(req);
541 }
542
543 static gboolean request_timeout(gpointer user_data)
544 {
545         struct request_data *req = user_data;
546         struct sockaddr *sa;
547         int sk;
548
549         if (!req)
550                 return FALSE;
551
552         debug("id 0x%04x", req->srcid);
553
554         request_list = g_slist_remove(request_list, req);
555
556         if (req->protocol == IPPROTO_UDP) {
557                 sk = get_req_udp_socket(req);
558                 sa = &req->sa;
559         } else if (req->protocol == IPPROTO_TCP) {
560                 sk = req->client_sk;
561                 sa = NULL;
562         } else
563                 goto out;
564
565         if (req->resplen > 0 && req->resp) {
566                 /*
567                  * Here we have received at least one reply (probably telling
568                  * "not found" result), so send that back to client instead
569                  * of more fatal server failed error.
570                  */
571                 if (sk >= 0)
572                         sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
573                                 sa, req->sa_len);
574
575         } else if (req->request) {
576                 /*
577                  * There was not reply from server at all.
578                  */
579                 struct domain_hdr *hdr;
580
581                 hdr = (void *)(req->request + protocol_offset(req->protocol));
582                 hdr->id = req->srcid;
583
584                 if (sk >= 0)
585                         send_response(sk, req->request, req->request_len,
586                                 sa, req->sa_len, req->protocol);
587         }
588
589         /*
590          * We cannot leave TCP client hanging so just kick it out
591          * if we get a request timeout from server.
592          */
593         if (req->protocol == IPPROTO_TCP) {
594                 debug("client %d removed", req->client_sk);
595                 g_hash_table_remove(partial_tcp_req_table,
596                                 GINT_TO_POINTER(req->client_sk));
597         }
598
599 out:
600         req->timeout = 0;
601         destroy_request_data(req);
602
603         return FALSE;
604 }
605
606 static int append_query(unsigned char *buf, unsigned int size,
607                                 const char *query, const char *domain)
608 {
609         unsigned char *ptr = buf;
610         int len;
611
612         debug("query %s domain %s", query, domain);
613
614         while (query) {
615                 const char *tmp;
616
617                 tmp = strchr(query, '.');
618                 if (!tmp) {
619                         len = strlen(query);
620                         if (len == 0)
621                                 break;
622                         *ptr = len;
623                         memcpy(ptr + 1, query, len);
624                         ptr += len + 1;
625                         break;
626                 }
627
628                 *ptr = tmp - query;
629                 memcpy(ptr + 1, query, tmp - query);
630                 ptr += tmp - query + 1;
631
632                 query = tmp + 1;
633         }
634
635         while (domain) {
636                 const char *tmp;
637
638                 tmp = strchr(domain, '.');
639                 if (!tmp) {
640                         len = strlen(domain);
641                         if (len == 0)
642                                 break;
643                         *ptr = len;
644                         memcpy(ptr + 1, domain, len);
645                         ptr += len + 1;
646                         break;
647                 }
648
649                 *ptr = tmp - domain;
650                 memcpy(ptr + 1, domain, tmp - domain);
651                 ptr += tmp - domain + 1;
652
653                 domain = tmp + 1;
654         }
655
656         *ptr++ = 0x00;
657
658         return ptr - buf;
659 }
660
661 static bool cache_check_is_valid(struct cache_data *data,
662                                 time_t current_time)
663 {
664         if (!data)
665                 return false;
666
667         if (data->cache_until < current_time)
668                 return false;
669
670         return true;
671 }
672
673 /*
674  * remove stale cached entries so that they can be refreshed
675  */
676 static void cache_enforce_validity(struct cache_entry *entry)
677 {
678         time_t current_time = time(NULL);
679
680         if (!cache_check_is_valid(entry->ipv4, current_time)
681                                                         && entry->ipv4) {
682                 debug("cache timeout \"%s\" type A", entry->key);
683                 g_free(entry->ipv4->data);
684                 g_free(entry->ipv4);
685                 entry->ipv4 = NULL;
686
687         }
688
689         if (!cache_check_is_valid(entry->ipv6, current_time)
690                                                         && entry->ipv6) {
691                 debug("cache timeout \"%s\" type AAAA", entry->key);
692                 g_free(entry->ipv6->data);
693                 g_free(entry->ipv6);
694                 entry->ipv6 = NULL;
695         }
696 }
697
698 static uint16_t cache_check_validity(char *question, uint16_t type,
699                                 struct cache_entry *entry)
700 {
701         time_t current_time = time(NULL);
702         bool want_refresh = false;
703
704         /*
705          * if we have a popular entry, we want a refresh instead of
706          * total destruction of the entry.
707          */
708         if (entry->hits > 2)
709                 want_refresh = true;
710
711         cache_enforce_validity(entry);
712
713         switch (type) {
714         case 1:         /* IPv4 */
715                 if (!cache_check_is_valid(entry->ipv4, current_time)) {
716                         debug("cache %s \"%s\" type A", entry->ipv4 ?
717                                         "timeout" : "entry missing", question);
718
719                         if (want_refresh)
720                                 entry->want_refresh = true;
721
722                         /*
723                          * We do not remove cache entry if there is still
724                          * valid IPv6 entry found in the cache.
725                          */
726                         if (!cache_check_is_valid(entry->ipv6, current_time) && !want_refresh) {
727                                 g_hash_table_remove(cache, question);
728                                 type = 0;
729                         }
730                 }
731                 break;
732
733         case 28:        /* IPv6 */
734                 if (!cache_check_is_valid(entry->ipv6, current_time)) {
735                         debug("cache %s \"%s\" type AAAA", entry->ipv6 ?
736                                         "timeout" : "entry missing", question);
737
738                         if (want_refresh)
739                                 entry->want_refresh = true;
740
741                         if (!cache_check_is_valid(entry->ipv4, current_time) && !want_refresh) {
742                                 g_hash_table_remove(cache, question);
743                                 type = 0;
744                         }
745                 }
746                 break;
747         }
748
749         return type;
750 }
751
752 static void cache_element_destroy(gpointer value)
753 {
754         struct cache_entry *entry = value;
755
756         if (!entry)
757                 return;
758
759         if (entry->ipv4) {
760                 g_free(entry->ipv4->data);
761                 g_free(entry->ipv4);
762         }
763
764         if (entry->ipv6) {
765                 g_free(entry->ipv6->data);
766                 g_free(entry->ipv6);
767         }
768
769         g_free(entry->key);
770         g_free(entry);
771
772         if (--cache_size < 0)
773                 cache_size = 0;
774 }
775
776 static gboolean try_remove_cache(gpointer user_data)
777 {
778         cache_timer = 0;
779
780         if (__sync_fetch_and_sub(&cache_refcount, 1) == 1) {
781                 debug("No cache users, removing it.");
782
783                 g_hash_table_destroy(cache);
784                 cache = NULL;
785         }
786
787         return FALSE;
788 }
789
790 static void create_cache(void)
791 {
792         if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
793                 cache = g_hash_table_new_full(g_str_hash,
794                                         g_str_equal,
795                                         NULL,
796                                         cache_element_destroy);
797 }
798
799 static struct cache_entry *cache_check(gpointer request, int *qtype, int proto)
800 {
801         char *question;
802         struct cache_entry *entry;
803         struct domain_question *q;
804         uint16_t type;
805         int offset, proto_offset;
806
807         if (!request)
808                 return NULL;
809
810         proto_offset = protocol_offset(proto);
811         if (proto_offset < 0)
812                 return NULL;
813
814         question = request + proto_offset + 12;
815
816         offset = strlen(question) + 1;
817         q = (void *) (question + offset);
818         type = ntohs(q->type);
819
820         /* We only cache either A (1) or AAAA (28) requests */
821         if (type != 1 && type != 28)
822                 return NULL;
823
824         if (!cache) {
825                 create_cache();
826                 return NULL;
827         }
828
829         entry = g_hash_table_lookup(cache, question);
830         if (!entry)
831                 return NULL;
832
833         type = cache_check_validity(question, type, entry);
834         if (type == 0)
835                 return NULL;
836
837         *qtype = type;
838         return entry;
839 }
840
841 /*
842  * Get a label/name from DNS resource record. The function decompresses the
843  * label if necessary. The function does not convert the name to presentation
844  * form. This means that the result string will contain label lengths instead
845  * of dots between labels. We intentionally do not want to convert to dotted
846  * format so that we can cache the wire format string directly.
847  */
848 static int get_name(int counter,
849                 unsigned char *pkt, unsigned char *start, unsigned char *max,
850                 unsigned char *output, int output_max, int *output_len,
851                 unsigned char **end, char *name, size_t max_name, int *name_len)
852 {
853         unsigned char *p;
854
855         /* Limit recursion to 10 (this means up to 10 labels in domain name) */
856         if (counter > 10)
857                 return -EINVAL;
858
859         p = start;
860         while (*p) {
861                 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
862                         uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
863
864                         if (offset >= max - pkt)
865                                 return -ENOBUFS;
866
867                         if (!*end)
868                                 *end = p + 2;
869
870                         return get_name(counter + 1, pkt, pkt + offset, max,
871                                         output, output_max, output_len, end,
872                                         name, max_name, name_len);
873                 } else {
874                         unsigned label_len = *p;
875
876                         if (pkt + label_len > max)
877                                 return -ENOBUFS;
878
879                         if (*output_len > output_max)
880                                 return -ENOBUFS;
881
882                         if ((*name_len + 1 + label_len + 1) > max_name)
883                                 return -ENOBUFS;
884
885                         /*
886                          * We need the original name in order to check
887                          * if this answer is the correct one.
888                          */
889                         name[(*name_len)++] = label_len;
890                         memcpy(name + *name_len, p + 1, label_len + 1);
891                         *name_len += label_len;
892
893                         /* We compress the result */
894                         output[0] = NS_CMPRSFLGS;
895                         output[1] = 0x0C;
896                         *output_len = 2;
897
898                         p += label_len + 1;
899
900                         if (!*end)
901                                 *end = p;
902
903                         if (p >= max)
904                                 return -ENOBUFS;
905                 }
906         }
907
908         return 0;
909 }
910
911 static int parse_rr(unsigned char *buf, unsigned char *start,
912                         unsigned char *max,
913                         unsigned char *response, unsigned int *response_size,
914                         uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
915                         unsigned char **end,
916                         char *name, size_t max_name)
917 {
918         struct domain_rr *rr;
919         int err, offset;
920         int name_len = 0, output_len = 0, max_rsp = *response_size;
921
922         err = get_name(0, buf, start, max, response, max_rsp,
923                         &output_len, end, name, max_name, &name_len);
924         if (err < 0)
925                 return err;
926
927         offset = output_len;
928
929         if ((unsigned int) offset > *response_size)
930                 return -ENOBUFS;
931
932         rr = (void *) (*end);
933
934         if (!rr)
935                 return -EINVAL;
936
937         *type = ntohs(rr->type);
938         *class = ntohs(rr->class);
939         *ttl = ntohl(rr->ttl);
940         *rdlen = ntohs(rr->rdlen);
941
942         if (*ttl < 0)
943                 return -EINVAL;
944
945         memcpy(response + offset, *end, sizeof(struct domain_rr));
946
947         offset += sizeof(struct domain_rr);
948         *end += sizeof(struct domain_rr);
949
950         if ((unsigned int) (offset + *rdlen) > *response_size)
951                 return -ENOBUFS;
952
953         memcpy(response + offset, *end, *rdlen);
954
955         *end += *rdlen;
956
957         *response_size = offset + *rdlen;
958
959         return 0;
960 }
961
962 static bool check_alias(GSList *aliases, char *name)
963 {
964         GSList *list;
965
966         if (aliases) {
967                 for (list = aliases; list; list = list->next) {
968                         int len = strlen((char *)list->data);
969                         if (strncmp((char *)list->data, name, len) == 0)
970                                 return true;
971                 }
972         }
973
974         return false;
975 }
976
977 static int parse_response(unsigned char *buf, int buflen,
978                         char *question, int qlen,
979                         uint16_t *type, uint16_t *class, int *ttl,
980                         unsigned char *response, unsigned int *response_len,
981                         uint16_t *answers)
982 {
983         struct domain_hdr *hdr = (void *) buf;
984         struct domain_question *q;
985         unsigned char *ptr;
986         uint16_t qdcount = ntohs(hdr->qdcount);
987         uint16_t ancount = ntohs(hdr->ancount);
988         int err, i;
989         uint16_t qtype, qclass;
990         unsigned char *next = NULL;
991         unsigned int maxlen = *response_len;
992         GSList *aliases = NULL, *list;
993         char name[NS_MAXDNAME + 1];
994
995         if (buflen < 12)
996                 return -EINVAL;
997
998         debug("qr %d qdcount %d", hdr->qr, qdcount);
999
1000         /* We currently only cache responses where question count is 1 */
1001         if (hdr->qr != 1 || qdcount != 1)
1002                 return -EINVAL;
1003
1004         ptr = buf + sizeof(struct domain_hdr);
1005
1006         strncpy(question, (char *) ptr, qlen);
1007         qlen = strlen(question);
1008         ptr += qlen + 1; /* skip \0 */
1009
1010         q = (void *) ptr;
1011         qtype = ntohs(q->type);
1012
1013         /* We cache only A and AAAA records */
1014         if (qtype != 1 && qtype != 28)
1015                 return -ENOMSG;
1016
1017         qclass = ntohs(q->class);
1018
1019         ptr += 2 + 2; /* ptr points now to answers */
1020
1021         err = -ENOMSG;
1022         *response_len = 0;
1023         *answers = 0;
1024
1025         memset(name, 0, sizeof(name));
1026
1027         /*
1028          * We have a bunch of answers (like A, AAAA, CNAME etc) to
1029          * A or AAAA question. We traverse the answers and parse the
1030          * resource records. Only A and AAAA records are cached, all
1031          * the other records in answers are skipped.
1032          */
1033         for (i = 0; i < ancount; i++) {
1034                 /*
1035                  * Get one address at a time to this buffer.
1036                  * The max size of the answer is
1037                  *   2 (pointer) + 2 (type) + 2 (class) +
1038                  *   4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
1039                  * for A or AAAA record.
1040                  * For CNAME the size can be bigger.
1041                  */
1042                 unsigned char rsp[NS_MAXCDNAME];
1043                 unsigned int rsp_len = sizeof(rsp) - 1;
1044                 int ret, rdlen;
1045
1046                 memset(rsp, 0, sizeof(rsp));
1047
1048                 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
1049                         type, class, ttl, &rdlen, &next, name,
1050                         sizeof(name) - 1);
1051                 if (ret != 0) {
1052                         err = ret;
1053                         goto out;
1054                 }
1055
1056                 /*
1057                  * Now rsp contains compressed or uncompressed resource
1058                  * record. Next we check if this record answers the question.
1059                  * The name var contains the uncompressed label.
1060                  * One tricky bit is the CNAME records as they alias
1061                  * the name we might be interested in.
1062                  */
1063
1064                 /*
1065                  * Go to next answer if the class is not the one we are
1066                  * looking for.
1067                  */
1068                 if (*class != qclass) {
1069                         ptr = next;
1070                         next = NULL;
1071                         continue;
1072                 }
1073
1074                 /*
1075                  * Try to resolve aliases also, type is CNAME(5).
1076                  * This is important as otherwise the aliased names would not
1077                  * be cached at all as the cache would not contain the aliased
1078                  * question.
1079                  *
1080                  * If any CNAME is found in DNS packet, then we cache the alias
1081                  * IP address instead of the question (as the server
1082                  * said that question has only an alias).
1083                  * This means in practice that if e.g., ipv6.google.com is
1084                  * queried, DNS server returns CNAME of that name which is
1085                  * ipv6.l.google.com. We then cache the address of the CNAME
1086                  * but return the question name to client. So the alias
1087                  * status of the name is not saved in cache and thus not
1088                  * returned to the client. We do not return DNS packets from
1089                  * cache to client saying that ipv6.google.com is an alias to
1090                  * ipv6.l.google.com but we return instead a DNS packet that
1091                  * says ipv6.google.com has address xxx which is in fact the
1092                  * address of ipv6.l.google.com. For caching purposes this
1093                  * should not cause any issues.
1094                  */
1095                 if (*type == 5 && strncmp(question, name, qlen) == 0) {
1096                         /*
1097                          * So now the alias answered the question. This is
1098                          * not very useful from caching point of view as
1099                          * the following A or AAAA records will not match the
1100                          * question. We need to find the real A/AAAA record
1101                          * of the alias and cache that.
1102                          */
1103                         unsigned char *end = NULL;
1104                         int name_len = 0, output_len = 0;
1105
1106                         memset(rsp, 0, sizeof(rsp));
1107                         rsp_len = sizeof(rsp) - 1;
1108
1109                         /*
1110                          * Alias is in rdata part of the message,
1111                          * and next-rdlen points to it. So we need to get
1112                          * the real name of the alias.
1113                          */
1114                         ret = get_name(0, buf, next - rdlen, buf + buflen,
1115                                         rsp, rsp_len, &output_len, &end,
1116                                         name, sizeof(name) - 1, &name_len);
1117                         if (ret != 0) {
1118                                 /* just ignore the error at this point */
1119                                 ptr = next;
1120                                 next = NULL;
1121                                 continue;
1122                         }
1123
1124                         /*
1125                          * We should now have the alias of the entry we might
1126                          * want to cache. Just remember it for a while.
1127                          * We check the alias list when we have parsed the
1128                          * A or AAAA record.
1129                          */
1130                         aliases = g_slist_prepend(aliases, g_strdup(name));
1131
1132                         ptr = next;
1133                         next = NULL;
1134                         continue;
1135                 }
1136
1137                 if (*type == qtype) {
1138                         /*
1139                          * We found correct type (A or AAAA)
1140                          */
1141                         if (check_alias(aliases, name) ||
1142                                 (!aliases && strncmp(question, name,
1143                                                         qlen) == 0)) {
1144                                 /*
1145                                  * We found an alias or the name of the rr
1146                                  * matches the question. If so, we append
1147                                  * the compressed label to the cache.
1148                                  * The end result is a response buffer that
1149                                  * will contain one or more cached and
1150                                  * compressed resource records.
1151                                  */
1152                                 if (*response_len + rsp_len > maxlen) {
1153                                         err = -ENOBUFS;
1154                                         goto out;
1155                                 }
1156                                 memcpy(response + *response_len, rsp, rsp_len);
1157                                 *response_len += rsp_len;
1158                                 (*answers)++;
1159                                 err = 0;
1160                         }
1161                 }
1162
1163                 ptr = next;
1164                 next = NULL;
1165         }
1166
1167 out:
1168         for (list = aliases; list; list = list->next)
1169                 g_free(list->data);
1170         g_slist_free(aliases);
1171
1172         return err;
1173 }
1174
1175 struct cache_timeout {
1176         time_t current_time;
1177         int max_timeout;
1178         int try_harder;
1179 };
1180
1181 static gboolean cache_check_entry(gpointer key, gpointer value,
1182                                         gpointer user_data)
1183 {
1184         struct cache_timeout *data = user_data;
1185         struct cache_entry *entry = value;
1186         int max_timeout;
1187
1188         /* Scale the number of hits by half as part of cache aging */
1189
1190         entry->hits /= 2;
1191
1192         /*
1193          * If either IPv4 or IPv6 cached entry has expired, we
1194          * remove both from the cache.
1195          */
1196
1197         if (entry->ipv4 && entry->ipv4->timeout > 0) {
1198                 max_timeout = entry->ipv4->cache_until;
1199                 if (max_timeout > data->max_timeout)
1200                         data->max_timeout = max_timeout;
1201
1202                 if (entry->ipv4->cache_until < data->current_time)
1203                         return TRUE;
1204         }
1205
1206         if (entry->ipv6 && entry->ipv6->timeout > 0) {
1207                 max_timeout = entry->ipv6->cache_until;
1208                 if (max_timeout > data->max_timeout)
1209                         data->max_timeout = max_timeout;
1210
1211                 if (entry->ipv6->cache_until < data->current_time)
1212                         return TRUE;
1213         }
1214
1215         /*
1216          * if we're asked to try harder, also remove entries that have
1217          * few hits
1218          */
1219         if (data->try_harder && entry->hits < 4)
1220                 return TRUE;
1221
1222         return FALSE;
1223 }
1224
1225 static void cache_cleanup(void)
1226 {
1227         static int max_timeout;
1228         struct cache_timeout data;
1229         int count = 0;
1230
1231         data.current_time = time(NULL);
1232         data.max_timeout = 0;
1233         data.try_harder = 0;
1234
1235         /*
1236          * In the first pass, we only remove entries that have timed out.
1237          * We use a cache of the first time to expire to do this only
1238          * when it makes sense.
1239          */
1240         if (max_timeout <= data.current_time) {
1241                 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1242                                                 &data);
1243         }
1244         debug("removed %d in the first pass", count);
1245
1246         /*
1247          * In the second pass, if the first pass turned up blank,
1248          * we also expire entries with a low hit count,
1249          * while aging the hit count at the same time.
1250          */
1251         data.try_harder = 1;
1252         if (count == 0)
1253                 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1254                                                 &data);
1255
1256         if (count == 0)
1257                 /*
1258                  * If we could not remove anything, then remember
1259                  * what is the max timeout and do nothing if we
1260                  * have not yet reached it. This will prevent
1261                  * constant traversal of the cache if it is full.
1262                  */
1263                 max_timeout = data.max_timeout;
1264         else
1265                 max_timeout = 0;
1266 }
1267
1268 static gboolean cache_invalidate_entry(gpointer key, gpointer value,
1269                                         gpointer user_data)
1270 {
1271         struct cache_entry *entry = value;
1272
1273         /* first, delete any expired elements */
1274         cache_enforce_validity(entry);
1275
1276         /* if anything is not expired, mark the entry for refresh */
1277         if (entry->hits > 0 && (entry->ipv4 || entry->ipv6))
1278                 entry->want_refresh = true;
1279
1280         /* delete the cached data */
1281         if (entry->ipv4) {
1282                 g_free(entry->ipv4->data);
1283                 g_free(entry->ipv4);
1284                 entry->ipv4 = NULL;
1285         }
1286
1287         if (entry->ipv6) {
1288                 g_free(entry->ipv6->data);
1289                 g_free(entry->ipv6);
1290                 entry->ipv6 = NULL;
1291         }
1292
1293         /* keep the entry if we want it refreshed, delete it otherwise */
1294         if (entry->want_refresh)
1295                 return FALSE;
1296         else
1297                 return TRUE;
1298 }
1299
1300 /*
1301  * cache_invalidate is called from places where the DNS landscape
1302  * has changed, say because connections are added or we entered a VPN.
1303  * The logic is to wipe all cache data, but mark all non-expired
1304  * parts of the cache for refresh rather than deleting the whole cache.
1305  */
1306 static void cache_invalidate(void)
1307 {
1308         debug("Invalidating the DNS cache %p", cache);
1309
1310         if (!cache)
1311                 return;
1312
1313         g_hash_table_foreach_remove(cache, cache_invalidate_entry, NULL);
1314 }
1315
1316 static void cache_refresh_entry(struct cache_entry *entry)
1317 {
1318
1319         cache_enforce_validity(entry);
1320
1321         if (entry->hits > 2 && !entry->ipv4)
1322                 entry->want_refresh = true;
1323         if (entry->hits > 2 && !entry->ipv6)
1324                 entry->want_refresh = true;
1325
1326         if (entry->want_refresh) {
1327                 char *c;
1328                 char dns_name[NS_MAXDNAME + 1];
1329                 entry->want_refresh = false;
1330
1331                 /* turn a DNS name into a hostname with dots */
1332                 strncpy(dns_name, entry->key, NS_MAXDNAME);
1333                 c = dns_name;
1334                 while (c && *c) {
1335                         int jump;
1336                         jump = *c;
1337                         *c = '.';
1338                         c += jump + 1;
1339                 }
1340                 debug("Refreshing %s\n", dns_name);
1341                 /* then refresh the hostname */
1342                 refresh_dns_entry(entry, &dns_name[1]);
1343         }
1344 }
1345
1346 static void cache_refresh_iterator(gpointer key, gpointer value,
1347                                         gpointer user_data)
1348 {
1349         struct cache_entry *entry = value;
1350
1351         cache_refresh_entry(entry);
1352 }
1353
1354 static void cache_refresh(void)
1355 {
1356         if (!cache)
1357                 return;
1358
1359         g_hash_table_foreach(cache, cache_refresh_iterator, NULL);
1360 }
1361
1362 static int reply_query_type(unsigned char *msg, int len)
1363 {
1364         unsigned char *c;
1365         int l;
1366         int type;
1367
1368         /* skip the header */
1369         c = msg + sizeof(struct domain_hdr);
1370         len -= sizeof(struct domain_hdr);
1371
1372         if (len < 0)
1373                 return 0;
1374
1375         /* now the query, which is a name and 2 16 bit words */
1376         l = dns_name_length(c);
1377         c += l;
1378         type = c[0] << 8 | c[1];
1379
1380         return type;
1381 }
1382
1383 static int cache_update(struct server_data *srv, unsigned char *msg,
1384                         unsigned int msg_len)
1385 {
1386         int offset = protocol_offset(srv->protocol);
1387         int err, qlen, ttl = 0;
1388         uint16_t answers = 0, type = 0, class = 0;
1389         struct domain_hdr *hdr = (void *)(msg + offset);
1390         struct domain_question *q;
1391         struct cache_entry *entry;
1392         struct cache_data *data;
1393         char question[NS_MAXDNAME + 1];
1394         unsigned char response[NS_MAXDNAME + 1];
1395         unsigned char *ptr;
1396         unsigned int rsplen;
1397         bool new_entry = true;
1398         time_t current_time;
1399
1400         if (cache_size >= MAX_CACHE_SIZE) {
1401                 cache_cleanup();
1402                 if (cache_size >= MAX_CACHE_SIZE)
1403                         return 0;
1404         }
1405
1406         current_time = time(NULL);
1407
1408         /* don't do a cache refresh more than twice a minute */
1409         if (next_refresh < current_time) {
1410                 cache_refresh();
1411                 next_refresh = current_time + 30;
1412         }
1413
1414         if (offset < 0)
1415                 return 0;
1416
1417         debug("offset %d hdr %p msg %p rcode %d", offset, hdr, msg, hdr->rcode);
1418
1419         /* Continue only if response code is 0 (=ok) */
1420         if (hdr->rcode != ns_r_noerror)
1421                 return 0;
1422
1423         if (!cache)
1424                 create_cache();
1425
1426         rsplen = sizeof(response) - 1;
1427         question[sizeof(question) - 1] = '\0';
1428
1429         err = parse_response(msg + offset, msg_len - offset,
1430                                 question, sizeof(question) - 1,
1431                                 &type, &class, &ttl,
1432                                 response, &rsplen, &answers);
1433
1434         /*
1435          * special case: if we do a ipv6 lookup and get no result
1436          * for a record that's already in our ipv4 cache.. we want
1437          * to cache the negative response.
1438          */
1439         if ((err == -ENOMSG || err == -ENOBUFS) &&
1440                         reply_query_type(msg + offset,
1441                                         msg_len - offset) == 28) {
1442                 entry = g_hash_table_lookup(cache, question);
1443                 if (entry && entry->ipv4 && !entry->ipv6) {
1444                         int cache_offset = 0;
1445
1446                         data = g_try_new(struct cache_data, 1);
1447                         if (!data)
1448                                 return -ENOMEM;
1449                         data->inserted = entry->ipv4->inserted;
1450                         data->type = type;
1451                         data->answers = ntohs(hdr->ancount);
1452                         data->timeout = entry->ipv4->timeout;
1453                         if (srv->protocol == IPPROTO_UDP)
1454                                 cache_offset = 2;
1455                         data->data_len = msg_len + cache_offset;
1456                         data->data = ptr = g_malloc(data->data_len);
1457                         ptr[0] = (data->data_len - 2) / 256;
1458                         ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1459                         if (srv->protocol == IPPROTO_UDP)
1460                                 ptr += 2;
1461                         data->valid_until = entry->ipv4->valid_until;
1462                         data->cache_until = entry->ipv4->cache_until;
1463                         memcpy(ptr, msg, msg_len);
1464                         entry->ipv6 = data;
1465                         /*
1466                          * we will get a "hit" when we serve the response
1467                          * out of the cache
1468                          */
1469                         entry->hits--;
1470                         if (entry->hits < 0)
1471                                 entry->hits = 0;
1472                         return 0;
1473                 }
1474         }
1475
1476         if (err < 0 || ttl == 0)
1477                 return 0;
1478
1479         qlen = strlen(question);
1480
1481         /*
1482          * If the cache contains already data, check if the
1483          * type of the cached data is the same and do not add
1484          * to cache if data is already there.
1485          * This is needed so that we can cache both A and AAAA
1486          * records for the same name.
1487          */
1488         entry = g_hash_table_lookup(cache, question);
1489         if (!entry) {
1490                 entry = g_try_new(struct cache_entry, 1);
1491                 if (!entry)
1492                         return -ENOMEM;
1493
1494                 data = g_try_new(struct cache_data, 1);
1495                 if (!data) {
1496                         g_free(entry);
1497                         return -ENOMEM;
1498                 }
1499
1500                 entry->key = g_strdup(question);
1501                 entry->ipv4 = entry->ipv6 = NULL;
1502                 entry->want_refresh = false;
1503                 entry->hits = 0;
1504
1505                 if (type == 1)
1506                         entry->ipv4 = data;
1507                 else
1508                         entry->ipv6 = data;
1509         } else {
1510                 if (type == 1 && entry->ipv4)
1511                         return 0;
1512
1513                 if (type == 28 && entry->ipv6)
1514                         return 0;
1515
1516                 data = g_try_new(struct cache_data, 1);
1517                 if (!data)
1518                         return -ENOMEM;
1519
1520                 if (type == 1)
1521                         entry->ipv4 = data;
1522                 else
1523                         entry->ipv6 = data;
1524
1525                 /*
1526                  * compensate for the hit we'll get for serving
1527                  * the response out of the cache
1528                  */
1529                 entry->hits--;
1530                 if (entry->hits < 0)
1531                         entry->hits = 0;
1532
1533                 new_entry = false;
1534         }
1535
1536         if (ttl < MIN_CACHE_TTL)
1537                 ttl = MIN_CACHE_TTL;
1538
1539         data->inserted = current_time;
1540         data->type = type;
1541         data->answers = answers;
1542         data->timeout = ttl;
1543         /*
1544          * The "2" in start of the length is the TCP offset. We allocate it
1545          * here even for UDP packet because it simplifies the sending
1546          * of cached packet.
1547          */
1548         data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
1549         data->data = ptr = g_malloc(data->data_len);
1550         data->valid_until = current_time + ttl;
1551
1552         /*
1553          * Restrict the cached DNS record TTL to some sane value
1554          * in order to prevent data staying in the cache too long.
1555          */
1556         if (ttl > MAX_CACHE_TTL)
1557                 ttl = MAX_CACHE_TTL;
1558
1559         data->cache_until = round_down_ttl(current_time + ttl, ttl);
1560
1561         if (!data->data) {
1562                 g_free(entry->key);
1563                 g_free(data);
1564                 g_free(entry);
1565                 return -ENOMEM;
1566         }
1567
1568         /*
1569          * We cache the two extra bytes at the start of the message
1570          * in a TCP packet. When sending UDP packet, we skip the first
1571          * two bytes. This way we do not need to know the format
1572          * (UDP/TCP) of the cached message.
1573          */
1574         if (srv->protocol == IPPROTO_UDP)
1575                 memcpy(ptr + 2, msg, offset + 12);
1576         else
1577                 memcpy(ptr, msg, offset + 12);
1578
1579         ptr[0] = (data->data_len - 2) / 256;
1580         ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1581         if (srv->protocol == IPPROTO_UDP)
1582                 ptr += 2;
1583
1584         memcpy(ptr + offset + 12, question, qlen + 1); /* copy also the \0 */
1585
1586         q = (void *) (ptr + offset + 12 + qlen + 1);
1587         q->type = htons(type);
1588         q->class = htons(class);
1589         memcpy(ptr + offset + 12 + qlen + 1 + sizeof(struct domain_question),
1590                 response, rsplen);
1591
1592         if (new_entry) {
1593                 g_hash_table_replace(cache, entry->key, entry);
1594                 cache_size++;
1595         }
1596
1597         debug("cache %d %squestion \"%s\" type %d ttl %d size %zd packet %u "
1598                                                                 "dns len %u",
1599                 cache_size, new_entry ? "new " : "old ",
1600                 question, type, ttl,
1601                 sizeof(*entry) + sizeof(*data) + data->data_len + qlen,
1602                 data->data_len,
1603                 srv->protocol == IPPROTO_TCP ?
1604                         (unsigned int)(data->data[0] * 256 + data->data[1]) :
1605                         data->data_len);
1606
1607         return 0;
1608 }
1609
1610 static int ns_resolv(struct server_data *server, struct request_data *req,
1611                                 gpointer request, gpointer name)
1612 {
1613         GList *list;
1614         int sk, err, type = 0;
1615         char *dot, *lookup = (char *) name;
1616         struct cache_entry *entry;
1617
1618         entry = cache_check(request, &type, req->protocol);
1619         if (entry) {
1620                 int ttl_left = 0;
1621                 struct cache_data *data;
1622
1623                 debug("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1624                 if (type == 1)
1625                         data = entry->ipv4;
1626                 else
1627                         data = entry->ipv6;
1628
1629                 if (data) {
1630                         ttl_left = data->valid_until - time(NULL);
1631                         entry->hits++;
1632                 }
1633
1634                 if (data && req->protocol == IPPROTO_TCP) {
1635                         send_cached_response(req->client_sk, data->data,
1636                                         data->data_len, NULL, 0, IPPROTO_TCP,
1637                                         req->srcid, data->answers, ttl_left);
1638                         return 1;
1639                 }
1640
1641                 if (data && req->protocol == IPPROTO_UDP) {
1642                         int udp_sk = get_req_udp_socket(req);
1643
1644                         if (udp_sk < 0)
1645                                 return -EIO;
1646
1647                         send_cached_response(udp_sk, data->data,
1648                                 data->data_len, &req->sa, req->sa_len,
1649                                 IPPROTO_UDP, req->srcid, data->answers,
1650                                 ttl_left);
1651                         return 1;
1652                 }
1653         }
1654
1655         sk = g_io_channel_unix_get_fd(server->channel);
1656
1657         err = sendto(sk, request, req->request_len, MSG_NOSIGNAL,
1658                         server->server_addr, server->server_addr_len);
1659         if (err < 0) {
1660                 debug("Cannot send message to server %s sock %d "
1661                         "protocol %d (%s/%d)",
1662                         server->server, sk, server->protocol,
1663                         strerror(errno), errno);
1664                 return -EIO;
1665         }
1666
1667         req->numserv++;
1668
1669         /* If we have more than one dot, we don't add domains */
1670         dot = strchr(lookup, '.');
1671         if (dot && dot != lookup + strlen(lookup) - 1)
1672                 return 0;
1673
1674         if (server->domains && server->domains->data)
1675                 req->append_domain = true;
1676
1677         for (list = server->domains; list; list = list->next) {
1678                 char *domain;
1679                 unsigned char alt[1024];
1680                 struct domain_hdr *hdr = (void *) &alt;
1681                 int altlen, domlen, offset;
1682
1683                 domain = list->data;
1684
1685                 if (!domain)
1686                         continue;
1687
1688                 offset = protocol_offset(server->protocol);
1689                 if (offset < 0)
1690                         return offset;
1691
1692                 domlen = strlen(domain) + 1;
1693                 if (domlen < 5)
1694                         return -EINVAL;
1695
1696                 alt[offset] = req->altid & 0xff;
1697                 alt[offset + 1] = req->altid >> 8;
1698
1699                 memcpy(alt + offset + 2, request + offset + 2, 10);
1700                 hdr->qdcount = htons(1);
1701
1702                 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1703                                         name, domain);
1704                 if (altlen < 0)
1705                         return -EINVAL;
1706
1707                 altlen += 12;
1708
1709                 memcpy(alt + offset + altlen,
1710                         request + offset + altlen - domlen,
1711                                 req->request_len - altlen - offset + domlen);
1712
1713                 if (server->protocol == IPPROTO_TCP) {
1714                         int req_len = req->request_len + domlen - 2;
1715
1716                         alt[0] = (req_len >> 8) & 0xff;
1717                         alt[1] = req_len & 0xff;
1718                 }
1719
1720                 debug("req %p dstid 0x%04x altid 0x%04x", req, req->dstid,
1721                                 req->altid);
1722
1723                 err = send(sk, alt, req->request_len + domlen, MSG_NOSIGNAL);
1724                 if (err < 0)
1725                         return -EIO;
1726
1727                 req->numserv++;
1728         }
1729
1730         return 0;
1731 }
1732
1733 static char *convert_label(char *start, char *end, char *ptr, char *uptr,
1734                         int remaining_len, int *used_comp, int *used_uncomp)
1735 {
1736         int pos, comp_pos;
1737         char name[NS_MAXLABEL];
1738
1739         pos = dn_expand((u_char *)start, (u_char *)end, (u_char *)ptr,
1740                         name, NS_MAXLABEL);
1741         if (pos < 0) {
1742                 debug("uncompress error [%d/%s]", errno, strerror(errno));
1743                 goto out;
1744         }
1745
1746         /*
1747          * We need to compress back the name so that we get back to internal
1748          * label presentation.
1749          */
1750         comp_pos = dn_comp(name, (u_char *)uptr, remaining_len, NULL, NULL);
1751         if (comp_pos < 0) {
1752                 debug("compress error [%d/%s]", errno, strerror(errno));
1753                 goto out;
1754         }
1755
1756         *used_comp = pos;
1757         *used_uncomp = comp_pos;
1758
1759         return ptr;
1760
1761 out:
1762         return NULL;
1763 }
1764
1765 static char *uncompress(int16_t field_count, char *start, char *end,
1766                         char *ptr, char *uncompressed, int uncomp_len,
1767                         char **uncompressed_ptr)
1768 {
1769         char *uptr = *uncompressed_ptr; /* position in result buffer */
1770
1771         debug("count %d ptr %p end %p uptr %p", field_count, ptr, end, uptr);
1772
1773         while (field_count-- > 0 && ptr < end) {
1774                 int dlen;               /* data field length */
1775                 int ulen;               /* uncompress length */
1776                 int pos;                /* position in compressed string */
1777                 char name[NS_MAXLABEL]; /* tmp label */
1778                 uint16_t dns_type, dns_class;
1779                 int comp_pos;
1780
1781                 if (!convert_label(start, end, ptr, name, NS_MAXLABEL,
1782                                         &pos, &comp_pos))
1783                         goto out;
1784
1785                 /*
1786                  * Copy the uncompressed resource record, type, class and \0 to
1787                  * tmp buffer.
1788                  */
1789
1790                 ulen = strlen(name);
1791                 strncpy(uptr, name, uncomp_len - (uptr - uncompressed));
1792
1793                 debug("pos %d ulen %d left %d name %s", pos, ulen,
1794                         (int)(uncomp_len - (uptr - uncompressed)), uptr);
1795
1796                 uptr += ulen;
1797                 *uptr++ = '\0';
1798
1799                 ptr += pos;
1800
1801                 /*
1802                  * We copy also the fixed portion of the result (type, class,
1803                  * ttl, address length and the address)
1804                  */
1805                 memcpy(uptr, ptr, NS_RRFIXEDSZ);
1806
1807                 dns_type = uptr[0] << 8 | uptr[1];
1808                 dns_class = uptr[2] << 8 | uptr[3];
1809
1810                 if (dns_class != ns_c_in)
1811                         goto out;
1812
1813                 ptr += NS_RRFIXEDSZ;
1814                 uptr += NS_RRFIXEDSZ;
1815
1816                 /*
1817                  * Then the variable portion of the result (data length).
1818                  * Typically this portion is also compressed
1819                  * so we need to uncompress it also when necessary.
1820                  */
1821                 if (dns_type == ns_t_cname) {
1822                         if (!convert_label(start, end, ptr, uptr,
1823                                         uncomp_len - (uptr - uncompressed),
1824                                                 &pos, &comp_pos))
1825                                 goto out;
1826
1827                         uptr[-2] = comp_pos << 8;
1828                         uptr[-1] = comp_pos & 0xff;
1829
1830                         uptr += comp_pos;
1831                         ptr += pos;
1832
1833                 } else if (dns_type == ns_t_a || dns_type == ns_t_aaaa) {
1834                         dlen = uptr[-2] << 8 | uptr[-1];
1835
1836                         if (ptr + dlen > end) {
1837                                 debug("data len %d too long", dlen);
1838                                 goto out;
1839                         }
1840
1841                         memcpy(uptr, ptr, dlen);
1842                         uptr += dlen;
1843                         ptr += dlen;
1844
1845                 } else if (dns_type == ns_t_soa) {
1846                         int total_len = 0;
1847                         char *len_ptr;
1848
1849                         /* Primary name server expansion */
1850                         if (!convert_label(start, end, ptr, uptr,
1851                                         uncomp_len - (uptr - uncompressed),
1852                                                 &pos, &comp_pos))
1853                                 goto out;
1854
1855                         total_len += comp_pos;
1856                         len_ptr = &uptr[-2];
1857                         ptr += pos;
1858                         uptr += comp_pos;
1859
1860                         /* Responsible authority's mailbox */
1861                         if (!convert_label(start, end, ptr, uptr,
1862                                         uncomp_len - (uptr - uncompressed),
1863                                                 &pos, &comp_pos))
1864                                 goto out;
1865
1866                         total_len += comp_pos;
1867                         ptr += pos;
1868                         uptr += comp_pos;
1869
1870                         /*
1871                          * Copy rest of the soa fields (serial number,
1872                          * refresh interval, retry interval, expiration
1873                          * limit and minimum ttl). They are 20 bytes long.
1874                          */
1875                         memcpy(uptr, ptr, 20);
1876                         uptr += 20;
1877                         ptr += 20;
1878                         total_len += 20;
1879
1880                         /*
1881                          * Finally fix the length of the data part
1882                          */
1883                         len_ptr[0] = total_len << 8;
1884                         len_ptr[1] = total_len & 0xff;
1885                 }
1886
1887                 *uncompressed_ptr = uptr;
1888         }
1889
1890         return ptr;
1891
1892 out:
1893         return NULL;
1894 }
1895
1896 static int strip_domains(char *name, char *answers, int maxlen)
1897 {
1898         uint16_t data_len;
1899         int name_len = strlen(name);
1900         char *ptr, *start = answers, *end = answers + maxlen;
1901
1902         while (maxlen > 0) {
1903                 ptr = strstr(answers, name);
1904                 if (ptr) {
1905                         char *domain = ptr + name_len;
1906
1907                         if (*domain) {
1908                                 int domain_len = strlen(domain);
1909
1910                                 memmove(answers + name_len,
1911                                         domain + domain_len,
1912                                         end - (domain + domain_len));
1913
1914                                 end -= domain_len;
1915                                 maxlen -= domain_len;
1916                         }
1917                 }
1918
1919                 answers += strlen(answers) + 1;
1920                 answers += 2 + 2 + 4;  /* skip type, class and ttl fields */
1921
1922                 data_len = answers[0] << 8 | answers[1];
1923                 answers += 2; /* skip the length field */
1924
1925                 if (answers + data_len > end)
1926                         return -EINVAL;
1927
1928                 answers += data_len;
1929                 maxlen -= answers - ptr;
1930         }
1931
1932         return end - start;
1933 }
1934
1935 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1936                                 struct server_data *data)
1937 {
1938         struct domain_hdr *hdr;
1939         struct request_data *req;
1940         int dns_id, sk, err, offset = protocol_offset(protocol);
1941
1942         if (offset < 0)
1943                 return offset;
1944
1945         hdr = (void *)(reply + offset);
1946         dns_id = reply[offset] | reply[offset + 1] << 8;
1947
1948         debug("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1949
1950         req = find_request(dns_id);
1951         if (!req)
1952                 return -EINVAL;
1953
1954         debug("req %p dstid 0x%04x altid 0x%04x rcode %d",
1955                         req, req->dstid, req->altid, hdr->rcode);
1956
1957         reply[offset] = req->srcid & 0xff;
1958         reply[offset + 1] = req->srcid >> 8;
1959
1960         req->numresp++;
1961
1962         if (hdr->rcode == ns_r_noerror || !req->resp) {
1963                 unsigned char *new_reply = NULL;
1964
1965                 /*
1966                  * If the domain name was append
1967                  * remove it before forwarding the reply.
1968                  * If there were more than one question, then this
1969                  * domain name ripping can be hairy so avoid that
1970                  * and bail out in that that case.
1971                  *
1972                  * The reason we are doing this magic is that if the
1973                  * user's DNS client tries to resolv hostname without
1974                  * domain part, it also expects to get the result without
1975                  * a domain name part.
1976                  */
1977                 if (req->append_domain && ntohs(hdr->qdcount) == 1) {
1978                         uint16_t domain_len = 0;
1979                         uint16_t header_len;
1980                         uint16_t dns_type, dns_class;
1981                         uint8_t host_len, dns_type_pos;
1982                         char uncompressed[NS_MAXDNAME], *uptr;
1983                         char *ptr, *eom = (char *)reply + reply_len;
1984
1985                         /*
1986                          * ptr points to the first char of the hostname.
1987                          * ->hostname.domain.net
1988                          */
1989                         header_len = offset + sizeof(struct domain_hdr);
1990                         ptr = (char *)reply + header_len;
1991
1992                         host_len = *ptr;
1993                         if (host_len > 0)
1994                                 domain_len = strnlen(ptr + 1 + host_len,
1995                                                 reply_len - header_len);
1996
1997                         /*
1998                          * If the query type is anything other than A or AAAA,
1999                          * then bail out and pass the message as is.
2000                          * We only want to deal with IPv4 or IPv6 addresses.
2001                          */
2002                         dns_type_pos = host_len + 1 + domain_len + 1;
2003
2004                         dns_type = ptr[dns_type_pos] << 8 |
2005                                                         ptr[dns_type_pos + 1];
2006                         dns_class = ptr[dns_type_pos + 2] << 8 |
2007                                                         ptr[dns_type_pos + 3];
2008                         if (dns_type != ns_t_a && dns_type != ns_t_aaaa &&
2009                                         dns_class != ns_c_in) {
2010                                 debug("Pass msg dns type %d class %d",
2011                                         dns_type, dns_class);
2012                                 goto pass;
2013                         }
2014
2015                         /*
2016                          * Remove the domain name and replace it by the end
2017                          * of reply. Check if the domain is really there
2018                          * before trying to copy the data. We also need to
2019                          * uncompress the answers if necessary.
2020                          * The domain_len can be 0 because if the original
2021                          * query did not contain a domain name, then we are
2022                          * sending two packets, first without the domain name
2023                          * and the second packet with domain name.
2024                          * The append_domain is set to true even if we sent
2025                          * the first packet without domain name. In this
2026                          * case we end up in this branch.
2027                          */
2028                         if (domain_len > 0) {
2029                                 int len = host_len + 1;
2030                                 int new_len, fixed_len;
2031                                 char *answers;
2032
2033                                 /*
2034                                  * First copy host (without domain name) into
2035                                  * tmp buffer.
2036                                  */
2037                                 uptr = &uncompressed[0];
2038                                 memcpy(uptr, ptr, len);
2039
2040                                 uptr[len] = '\0'; /* host termination */
2041                                 uptr += len + 1;
2042
2043                                 /*
2044                                  * Copy type and class fields of the question.
2045                                  */
2046                                 ptr += len + domain_len + 1;
2047                                 memcpy(uptr, ptr, NS_QFIXEDSZ);
2048
2049                                 /*
2050                                  * ptr points to answers after this
2051                                  */
2052                                 ptr += NS_QFIXEDSZ;
2053                                 uptr += NS_QFIXEDSZ;
2054                                 answers = uptr;
2055                                 fixed_len = answers - uncompressed;
2056
2057                                 /*
2058                                  * We then uncompress the result to buffer
2059                                  * so that we can rip off the domain name
2060                                  * part from the question. First answers,
2061                                  * then name server (authority) information,
2062                                  * and finally additional record info.
2063                                  */
2064
2065                                 ptr = uncompress(ntohs(hdr->ancount),
2066                                                 (char *)reply + offset, eom,
2067                                                 ptr, uncompressed, NS_MAXDNAME,
2068                                                 &uptr);
2069                                 if (!ptr)
2070                                         goto out;
2071
2072                                 ptr = uncompress(ntohs(hdr->nscount),
2073                                                 (char *)reply + offset, eom,
2074                                                 ptr, uncompressed, NS_MAXDNAME,
2075                                                 &uptr);
2076                                 if (!ptr)
2077                                         goto out;
2078
2079                                 ptr = uncompress(ntohs(hdr->arcount),
2080                                                 (char *)reply + offset, eom,
2081                                                 ptr, uncompressed, NS_MAXDNAME,
2082                                                 &uptr);
2083                                 if (!ptr)
2084                                         goto out;
2085
2086                                 /*
2087                                  * The uncompressed buffer now contains almost
2088                                  * valid response. Final step is to get rid of
2089                                  * the domain name because at least glibc
2090                                  * gethostbyname() implementation does extra
2091                                  * checks and expects to find an answer without
2092                                  * domain name if we asked a query without
2093                                  * domain part. Note that glibc getaddrinfo()
2094                                  * works differently and accepts FQDN in answer
2095                                  */
2096                                 new_len = strip_domains(uncompressed, answers,
2097                                                         uptr - answers);
2098                                 if (new_len < 0) {
2099                                         debug("Corrupted packet");
2100                                         return -EINVAL;
2101                                 }
2102
2103                                 /*
2104                                  * Because we have now uncompressed the answers
2105                                  * we might have to create a bigger buffer to
2106                                  * hold all that data.
2107                                  */
2108
2109                                 reply_len = header_len + new_len + fixed_len;
2110
2111                                 new_reply = g_try_malloc(reply_len);
2112                                 if (!new_reply)
2113                                         return -ENOMEM;
2114
2115                                 memcpy(new_reply, reply, header_len);
2116                                 memcpy(new_reply + header_len, uncompressed,
2117                                         new_len + fixed_len);
2118
2119                                 reply = new_reply;
2120                         }
2121                 }
2122
2123         pass:
2124                 g_free(req->resp);
2125                 req->resplen = 0;
2126
2127                 req->resp = g_try_malloc(reply_len);
2128                 if (!req->resp)
2129                         return -ENOMEM;
2130
2131                 memcpy(req->resp, reply, reply_len);
2132                 req->resplen = reply_len;
2133
2134                 cache_update(data, reply, reply_len);
2135
2136                 g_free(new_reply);
2137         }
2138
2139 out:
2140         if (req->numresp < req->numserv) {
2141                 if (hdr->rcode > ns_r_noerror) {
2142                         return -EINVAL;
2143                 } else if (hdr->ancount == 0 && req->append_domain) {
2144                         return -EINVAL;
2145                 }
2146         }
2147
2148         request_list = g_slist_remove(request_list, req);
2149
2150         if (protocol == IPPROTO_UDP) {
2151                 sk = get_req_udp_socket(req);
2152                 if (sk < 0) {
2153                         errno = -EIO;
2154                         err = -EIO;
2155                 } else
2156                         err = sendto(sk, req->resp, req->resplen, 0,
2157                                 &req->sa, req->sa_len);
2158         } else {
2159                 sk = req->client_sk;
2160                 err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
2161         }
2162
2163         if (err < 0)
2164                 debug("Cannot send msg, sk %d proto %d errno %d/%s", sk,
2165                         protocol, errno, strerror(errno));
2166         else
2167                 debug("proto %d sent %d bytes to %d", protocol, err, sk);
2168
2169         destroy_request_data(req);
2170
2171         return err;
2172 }
2173
2174 static void server_destroy_socket(struct server_data *data)
2175 {
2176         debug("index %d server %s proto %d", data->index,
2177                                         data->server, data->protocol);
2178
2179         if (data->watch > 0) {
2180                 g_source_remove(data->watch);
2181                 data->watch = 0;
2182         }
2183
2184         if (data->timeout > 0) {
2185                 g_source_remove(data->timeout);
2186                 data->timeout = 0;
2187         }
2188
2189         if (data->channel) {
2190                 g_io_channel_shutdown(data->channel, TRUE, NULL);
2191                 g_io_channel_unref(data->channel);
2192                 data->channel = NULL;
2193         }
2194
2195         g_free(data->incoming_reply);
2196         data->incoming_reply = NULL;
2197 }
2198
2199 static void destroy_server(struct server_data *server)
2200 {
2201         debug("index %d server %s sock %d", server->index, server->server,
2202                         server->channel ?
2203                         g_io_channel_unix_get_fd(server->channel): -1);
2204
2205         server_list = g_slist_remove(server_list, server);
2206         server_destroy_socket(server);
2207
2208         if (server->protocol == IPPROTO_UDP && server->enabled)
2209                 debug("Removing DNS server %s", server->server);
2210
2211         g_free(server->server);
2212         g_list_free_full(server->domains, g_free);
2213         g_free(server->server_addr);
2214
2215         /*
2216          * We do not remove cache right away but delay it few seconds.
2217          * The idea is that when IPv6 DNS server is added via RDNSS, it has a
2218          * lifetime. When the lifetime expires we decrease the refcount so it
2219          * is possible that the cache is then removed. Because a new DNS server
2220          * is usually created almost immediately we would then loose the cache
2221          * without any good reason. The small delay allows the new RDNSS to
2222          * create a new DNS server instance and the refcount does not go to 0.
2223          */
2224         if (cache && !cache_timer)
2225                 cache_timer = g_timeout_add_seconds(3, try_remove_cache, NULL);
2226
2227         g_free(server);
2228 }
2229
2230 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
2231                                                         gpointer user_data)
2232 {
2233         unsigned char buf[4096];
2234         int sk, len;
2235         struct server_data *data = user_data;
2236
2237         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2238                 connman_error("Error with UDP server %s", data->server);
2239                 server_destroy_socket(data);
2240                 return FALSE;
2241         }
2242
2243         sk = g_io_channel_unix_get_fd(channel);
2244
2245         len = recv(sk, buf, sizeof(buf), 0);
2246
2247         if (len >= 12)
2248                 forward_dns_reply(buf, len, IPPROTO_UDP, data);
2249
2250         return TRUE;
2251 }
2252
2253 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
2254                                                         gpointer user_data)
2255 {
2256         int sk;
2257         struct server_data *server = user_data;
2258
2259         sk = g_io_channel_unix_get_fd(channel);
2260         if (sk == 0)
2261                 return FALSE;
2262
2263         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2264                 GSList *list;
2265 hangup:
2266                 debug("TCP server channel closed, sk %d", sk);
2267
2268                 /*
2269                  * Discard any partial response which is buffered; better
2270                  * to get a proper response from a working server.
2271                  */
2272                 g_free(server->incoming_reply);
2273                 server->incoming_reply = NULL;
2274
2275                 list = request_list;
2276                 while (list) {
2277                         struct request_data *req = list->data;
2278                         struct domain_hdr *hdr;
2279                         list = list->next;
2280
2281                         if (req->protocol == IPPROTO_UDP)
2282                                 continue;
2283
2284                         if (!req->request)
2285                                 continue;
2286
2287                         /*
2288                          * If we're not waiting for any further response
2289                          * from another name server, then we send an error
2290                          * response to the client.
2291                          */
2292                         if (req->numserv && --(req->numserv))
2293                                 continue;
2294
2295                         hdr = (void *) (req->request + 2);
2296                         hdr->id = req->srcid;
2297                         send_response(req->client_sk, req->request,
2298                                 req->request_len, NULL, 0, IPPROTO_TCP);
2299
2300                         request_list = g_slist_remove(request_list, req);
2301                 }
2302
2303                 destroy_server(server);
2304
2305                 return FALSE;
2306         }
2307
2308         if ((condition & G_IO_OUT) && !server->connected) {
2309                 GSList *list;
2310                 GList *domains;
2311                 bool no_request_sent = true;
2312                 struct server_data *udp_server;
2313
2314                 udp_server = find_server(server->index, server->server,
2315                                                                 IPPROTO_UDP);
2316                 if (udp_server) {
2317                         for (domains = udp_server->domains; domains;
2318                                                 domains = domains->next) {
2319                                 char *dom = domains->data;
2320
2321                                 debug("Adding domain %s to %s",
2322                                                 dom, server->server);
2323
2324                                 server->domains = g_list_append(server->domains,
2325                                                                 g_strdup(dom));
2326                         }
2327                 }
2328
2329                 server->connected = true;
2330                 server_list = g_slist_append(server_list, server);
2331
2332                 if (server->timeout > 0) {
2333                         g_source_remove(server->timeout);
2334                         server->timeout = 0;
2335                 }
2336
2337                 for (list = request_list; list; ) {
2338                         struct request_data *req = list->data;
2339                         int status;
2340
2341                         if (req->protocol == IPPROTO_UDP) {
2342                                 list = list->next;
2343                                 continue;
2344                         }
2345
2346                         debug("Sending req %s over TCP", (char *)req->name);
2347
2348                         status = ns_resolv(server, req,
2349                                                 req->request, req->name);
2350                         if (status > 0) {
2351                                 /*
2352                                  * A cached result was sent,
2353                                  * so the request can be released
2354                                  */
2355                                 list = list->next;
2356                                 request_list = g_slist_remove(request_list, req);
2357                                 destroy_request_data(req);
2358                                 continue;
2359                         }
2360
2361                         if (status < 0) {
2362                                 list = list->next;
2363                                 continue;
2364                         }
2365
2366                         no_request_sent = false;
2367
2368                         if (req->timeout > 0)
2369                                 g_source_remove(req->timeout);
2370
2371                         req->timeout = g_timeout_add_seconds(30,
2372                                                 request_timeout, req);
2373                         list = list->next;
2374                 }
2375
2376                 if (no_request_sent) {
2377                         destroy_server(server);
2378                         return FALSE;
2379                 }
2380
2381         } else if (condition & G_IO_IN) {
2382                 struct partial_reply *reply = server->incoming_reply;
2383                 int bytes_recv;
2384
2385                 if (!reply) {
2386                         unsigned char reply_len_buf[2];
2387                         uint16_t reply_len;
2388
2389                         bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
2390                         if (!bytes_recv) {
2391                                 goto hangup;
2392                         } else if (bytes_recv < 0) {
2393                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
2394                                         return TRUE;
2395
2396                                 connman_error("DNS proxy error %s",
2397                                                 strerror(errno));
2398                                 goto hangup;
2399                         } else if (bytes_recv < 2)
2400                                 return TRUE;
2401
2402                         reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
2403                         reply_len += 2;
2404
2405                         debug("TCP reply %d bytes from %d", reply_len, sk);
2406
2407                         reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
2408                         if (!reply)
2409                                 return TRUE;
2410
2411                         reply->len = reply_len;
2412                         reply->received = 0;
2413
2414                         server->incoming_reply = reply;
2415                 }
2416
2417                 while (reply->received < reply->len) {
2418                         bytes_recv = recv(sk, reply->buf + reply->received,
2419                                         reply->len - reply->received, 0);
2420                         if (!bytes_recv) {
2421                                 connman_error("DNS proxy TCP disconnect");
2422                                 break;
2423                         } else if (bytes_recv < 0) {
2424                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
2425                                         return TRUE;
2426
2427                                 connman_error("DNS proxy error %s",
2428                                                 strerror(errno));
2429                                 break;
2430                         }
2431                         reply->received += bytes_recv;
2432                 }
2433
2434                 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
2435                                         server);
2436
2437                 g_free(reply);
2438                 server->incoming_reply = NULL;
2439
2440                 destroy_server(server);
2441
2442                 return FALSE;
2443         }
2444
2445         return TRUE;
2446 }
2447
2448 static gboolean tcp_idle_timeout(gpointer user_data)
2449 {
2450         struct server_data *server = user_data;
2451
2452         debug("");
2453
2454         if (!server)
2455                 return FALSE;
2456
2457         destroy_server(server);
2458
2459         return FALSE;
2460 }
2461
2462 static int server_create_socket(struct server_data *data)
2463 {
2464         int sk, err;
2465         char *interface;
2466
2467         debug("index %d server %s proto %d", data->index,
2468                                         data->server, data->protocol);
2469
2470         sk = socket(data->server_addr->sa_family,
2471                 data->protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM,
2472                 data->protocol);
2473         if (sk < 0) {
2474                 err = errno;
2475                 connman_error("Failed to create server %s socket",
2476                                                         data->server);
2477                 server_destroy_socket(data);
2478                 return -err;
2479         }
2480
2481         debug("sk %d", sk);
2482
2483         interface = connman_inet_ifname(data->index);
2484         if (interface) {
2485                 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2486                                         interface,
2487                                         strlen(interface) + 1) < 0) {
2488                         err = errno;
2489                         connman_error("Failed to bind server %s "
2490                                                 "to interface %s",
2491                                                 data->server, interface);
2492                         close(sk);
2493                         server_destroy_socket(data);
2494                         g_free(interface);
2495                         return -err;
2496                 }
2497                 g_free(interface);
2498         }
2499
2500         data->channel = g_io_channel_unix_new(sk);
2501         if (!data->channel) {
2502                 connman_error("Failed to create server %s channel",
2503                                                         data->server);
2504                 close(sk);
2505                 server_destroy_socket(data);
2506                 return -ENOMEM;
2507         }
2508
2509         g_io_channel_set_close_on_unref(data->channel, TRUE);
2510
2511         if (data->protocol == IPPROTO_TCP) {
2512                 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
2513                 data->watch = g_io_add_watch(data->channel,
2514                         G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
2515                                                 tcp_server_event, data);
2516                 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
2517                                                                 data);
2518         } else
2519                 data->watch = g_io_add_watch(data->channel,
2520                         G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2521                                                 udp_server_event, data);
2522
2523         if (connect(sk, data->server_addr, data->server_addr_len) < 0) {
2524                 err = errno;
2525
2526                 if ((data->protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
2527                                 data->protocol == IPPROTO_UDP) {
2528
2529                         connman_error("Failed to connect to server %s",
2530                                                                 data->server);
2531                         server_destroy_socket(data);
2532                         return -err;
2533                 }
2534         }
2535
2536         create_cache();
2537
2538         return 0;
2539 }
2540
2541 static void enable_fallback(bool enable)
2542 {
2543         GSList *list;
2544
2545         for (list = server_list; list; list = list->next) {
2546                 struct server_data *data = list->data;
2547
2548                 if (data->index != -1)
2549                         continue;
2550
2551                 if (enable)
2552                         DBG("Enabling fallback DNS server %s", data->server);
2553                 else
2554                         DBG("Disabling fallback DNS server %s", data->server);
2555
2556                 data->enabled = enable;
2557         }
2558 }
2559
2560 static struct server_data *create_server(int index,
2561                                         const char *domain, const char *server,
2562                                         int protocol)
2563 {
2564         struct server_data *data;
2565         struct addrinfo hints, *rp;
2566         int ret;
2567
2568         DBG("index %d server %s", index, server);
2569
2570         data = g_try_new0(struct server_data, 1);
2571         if (!data) {
2572                 connman_error("Failed to allocate server %s data", server);
2573                 return NULL;
2574         }
2575
2576         data->index = index;
2577         if (domain)
2578                 data->domains = g_list_append(data->domains, g_strdup(domain));
2579         data->server = g_strdup(server);
2580         data->protocol = protocol;
2581
2582         memset(&hints, 0, sizeof(hints));
2583
2584         switch (protocol) {
2585         case IPPROTO_UDP:
2586                 hints.ai_socktype = SOCK_DGRAM;
2587                 break;
2588
2589         case IPPROTO_TCP:
2590                 hints.ai_socktype = SOCK_STREAM;
2591                 break;
2592
2593         default:
2594                 destroy_server(data);
2595                 return NULL;
2596         }
2597         hints.ai_family = AF_UNSPEC;
2598         hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
2599
2600         ret = getaddrinfo(data->server, "53", &hints, &rp);
2601         if (ret) {
2602                 connman_error("Failed to parse server %s address: %s\n",
2603                               data->server, gai_strerror(ret));
2604                 destroy_server(data);
2605                 return NULL;
2606         }
2607
2608         /* Do not blindly copy this code elsewhere; it doesn't loop over the
2609            results using ->ai_next as it should. That's OK in *this* case
2610            because it was a numeric lookup; we *know* there's only one. */
2611
2612         data->server_addr_len = rp->ai_addrlen;
2613
2614         switch (rp->ai_family) {
2615         case AF_INET:
2616                 data->server_addr = (struct sockaddr *)
2617                                         g_try_new0(struct sockaddr_in, 1);
2618                 break;
2619         case AF_INET6:
2620                 data->server_addr = (struct sockaddr *)
2621                                         g_try_new0(struct sockaddr_in6, 1);
2622                 break;
2623         default:
2624                 connman_error("Wrong address family %d", rp->ai_family);
2625                 break;
2626         }
2627         if (!data->server_addr) {
2628                 freeaddrinfo(rp);
2629                 destroy_server(data);
2630                 return NULL;
2631         }
2632         memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
2633         freeaddrinfo(rp);
2634
2635         if (server_create_socket(data) != 0) {
2636                 destroy_server(data);
2637                 return NULL;
2638         }
2639
2640         if (protocol == IPPROTO_UDP) {
2641                 if (__connman_service_index_is_default(data->index) ||
2642                                 __connman_service_index_is_split_routing(
2643                                                                 data->index)) {
2644                         data->enabled = true;
2645                         DBG("Adding DNS server %s", data->server);
2646
2647                         enable_fallback(false);
2648                 }
2649
2650                 server_list = g_slist_append(server_list, data);
2651         }
2652
2653         return data;
2654 }
2655
2656 static bool resolv(struct request_data *req,
2657                                 gpointer request, gpointer name)
2658 {
2659         GSList *list;
2660
2661         for (list = server_list; list; list = list->next) {
2662                 struct server_data *data = list->data;
2663
2664                 if (data->protocol == IPPROTO_TCP) {
2665                         DBG("server %s ignored proto TCP", data->server);
2666                         continue;
2667                 }
2668
2669                 debug("server %s enabled %d", data->server, data->enabled);
2670
2671                 if (!data->enabled)
2672                         continue;
2673
2674                 if (!data->channel && data->protocol == IPPROTO_UDP) {
2675                         if (server_create_socket(data) < 0) {
2676                                 DBG("socket creation failed while resolving");
2677                                 continue;
2678                         }
2679                 }
2680
2681                 if (ns_resolv(data, req, request, name) > 0)
2682                         return true;
2683         }
2684
2685         return false;
2686 }
2687
2688 static void update_domain(int index, const char *domain, bool append)
2689 {
2690         GSList *list;
2691
2692         DBG("index %d domain %s", index, domain);
2693
2694         if (!domain)
2695                 return;
2696
2697         for (list = server_list; list; list = list->next) {
2698                 struct server_data *data = list->data;
2699                 GList *dom_list;
2700                 char *dom;
2701                 bool dom_found = false;
2702
2703                 if (data->index < 0)
2704                         continue;
2705
2706                 if (data->index != index)
2707                         continue;
2708
2709                 for (dom_list = data->domains; dom_list;
2710                                 dom_list = dom_list->next) {
2711                         dom = dom_list->data;
2712
2713                         if (g_str_equal(dom, domain)) {
2714                                 dom_found = true;
2715                                 break;
2716                         }
2717                 }
2718
2719                 if (!dom_found && append) {
2720                         data->domains =
2721                                 g_list_append(data->domains, g_strdup(domain));
2722                 } else if (dom_found && !append) {
2723                         data->domains =
2724                                 g_list_remove(data->domains, dom);
2725                         g_free(dom);
2726                 }
2727         }
2728 }
2729
2730 static void append_domain(int index, const char *domain)
2731 {
2732         update_domain(index, domain, true);
2733 }
2734
2735 static void remove_domain(int index, const char *domain)
2736 {
2737         update_domain(index, domain, false);
2738 }
2739
2740 static void flush_requests(struct server_data *server)
2741 {
2742         GSList *list;
2743
2744         list = request_list;
2745         while (list) {
2746                 struct request_data *req = list->data;
2747
2748                 list = list->next;
2749
2750                 if (ns_resolv(server, req, req->request, req->name)) {
2751                         /*
2752                          * A cached result was sent,
2753                          * so the request can be released
2754                          */
2755                         request_list =
2756                                 g_slist_remove(request_list, req);
2757                         destroy_request_data(req);
2758                         continue;
2759                 }
2760
2761                 if (req->timeout > 0)
2762                         g_source_remove(req->timeout);
2763
2764                 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2765         }
2766 }
2767
2768 int __connman_dnsproxy_append(int index, const char *domain,
2769                                                         const char *server)
2770 {
2771         struct server_data *data;
2772
2773         DBG("index %d server %s", index, server);
2774
2775         if (!server && !domain)
2776                 return -EINVAL;
2777
2778         if (!server) {
2779                 append_domain(index, domain);
2780
2781                 return 0;
2782         }
2783
2784         if (g_str_equal(server, "127.0.0.1"))
2785                 return -ENODEV;
2786
2787         if (g_str_equal(server, "::1"))
2788                 return -ENODEV;
2789
2790         data = find_server(index, server, IPPROTO_UDP);
2791         if (data) {
2792                 append_domain(index, domain);
2793                 return 0;
2794         }
2795
2796         data = create_server(index, domain, server, IPPROTO_UDP);
2797         if (!data)
2798                 return -EIO;
2799
2800         flush_requests(data);
2801
2802         return 0;
2803 }
2804
2805 static void remove_server(int index, const char *domain,
2806                         const char *server, int protocol)
2807 {
2808         struct server_data *data;
2809         GSList *list;
2810
2811         data = find_server(index, server, protocol);
2812         if (!data)
2813                 return;
2814
2815         destroy_server(data);
2816
2817         for (list = server_list; list; list = list->next) {
2818                 struct server_data *data = list->data;
2819
2820                 if (data->index != -1 && data->enabled == true)
2821                         return;
2822         }
2823
2824         enable_fallback(true);
2825 }
2826
2827 int __connman_dnsproxy_remove(int index, const char *domain,
2828                                                         const char *server)
2829 {
2830         DBG("index %d server %s", index, server);
2831
2832         if (!server && !domain)
2833                 return -EINVAL;
2834
2835         if (!server) {
2836                 remove_domain(index, domain);
2837
2838                 return 0;
2839         }
2840
2841         if (g_str_equal(server, "127.0.0.1"))
2842                 return -ENODEV;
2843
2844         if (g_str_equal(server, "::1"))
2845                 return -ENODEV;
2846
2847         remove_server(index, domain, server, IPPROTO_UDP);
2848         remove_server(index, domain, server, IPPROTO_TCP);
2849
2850         return 0;
2851 }
2852
2853 static void dnsproxy_offline_mode(bool enabled)
2854 {
2855         GSList *list;
2856
2857         DBG("enabled %d", enabled);
2858
2859         for (list = server_list; list; list = list->next) {
2860                 struct server_data *data = list->data;
2861
2862                 if (!enabled) {
2863                         DBG("Enabling DNS server %s", data->server);
2864                         data->enabled = true;
2865                         cache_invalidate();
2866                         cache_refresh();
2867                 } else {
2868                         DBG("Disabling DNS server %s", data->server);
2869                         data->enabled = false;
2870                         cache_invalidate();
2871                 }
2872         }
2873 }
2874
2875 static void dnsproxy_default_changed(struct connman_service *service)
2876 {
2877         bool server_enabled = false;
2878         GSList *list;
2879         int index;
2880
2881         DBG("service %p", service);
2882
2883         /* DNS has changed, invalidate the cache */
2884         cache_invalidate();
2885
2886         if (!service) {
2887                 /* When no services are active, then disable DNS proxying */
2888                 dnsproxy_offline_mode(true);
2889                 return;
2890         }
2891
2892         index = __connman_service_get_index(service);
2893         if (index < 0)
2894                 return;
2895
2896         for (list = server_list; list; list = list->next) {
2897                 struct server_data *data = list->data;
2898
2899                 if (data->index == index) {
2900                         DBG("Enabling DNS server %s", data->server);
2901                         data->enabled = true;
2902                         server_enabled = true;
2903                 } else {
2904                         DBG("Disabling DNS server %s", data->server);
2905                         data->enabled = false;
2906                 }
2907         }
2908
2909         if (!server_enabled)
2910                 enable_fallback(true);
2911
2912         cache_refresh();
2913 }
2914
2915 static void dnsproxy_service_state_changed(struct connman_service *service,
2916                         enum connman_service_state state)
2917 {
2918         GSList *list;
2919         int index;
2920
2921         switch (state) {
2922         case CONNMAN_SERVICE_STATE_DISCONNECT:
2923         case CONNMAN_SERVICE_STATE_IDLE:
2924                 break;
2925         case CONNMAN_SERVICE_STATE_ASSOCIATION:
2926         case CONNMAN_SERVICE_STATE_CONFIGURATION:
2927         case CONNMAN_SERVICE_STATE_FAILURE:
2928         case CONNMAN_SERVICE_STATE_ONLINE:
2929         case CONNMAN_SERVICE_STATE_READY:
2930         case CONNMAN_SERVICE_STATE_UNKNOWN:
2931                 return;
2932         }
2933
2934         index = __connman_service_get_index(service);
2935         list = server_list;
2936
2937         while (list) {
2938                 struct server_data *data = list->data;
2939
2940                 /* Get next before the list is changed by destroy_server() */
2941                 list = list->next;
2942
2943                 if (data->index == index) {
2944                         DBG("removing server data of index %d", index);
2945                         destroy_server(data);
2946                 }
2947         }
2948 }
2949
2950 static const struct connman_notifier dnsproxy_notifier = {
2951         .name                   = "dnsproxy",
2952         .default_changed        = dnsproxy_default_changed,
2953         .offline_mode           = dnsproxy_offline_mode,
2954         .service_state_changed  = dnsproxy_service_state_changed,
2955 };
2956
2957 static const unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2958
2959 static int parse_request(unsigned char *buf, size_t len,
2960                                         char *name, unsigned int size)
2961 {
2962         struct domain_hdr *hdr = (void *) buf;
2963         uint16_t qdcount = ntohs(hdr->qdcount);
2964         uint16_t ancount = ntohs(hdr->ancount);
2965         uint16_t nscount = ntohs(hdr->nscount);
2966         uint16_t arcount = ntohs(hdr->arcount);
2967         unsigned char *ptr;
2968         unsigned int remain, used = 0;
2969
2970         if (len < sizeof(*hdr) + sizeof(struct qtype_qclass) ||
2971                         hdr->qr || qdcount != 1 || ancount || nscount) {
2972                 DBG("Dropped DNS request qr %d with len %zd qdcount %d "
2973                         "ancount %d nscount %d", hdr->qr, len, qdcount, ancount,
2974                         nscount);
2975
2976                 return -EINVAL;
2977         }
2978
2979         if (!name || !size)
2980                 return -EINVAL;
2981
2982         debug("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2983                                         hdr->id, hdr->qr, hdr->opcode,
2984                                                         qdcount, arcount);
2985
2986         name[0] = '\0';
2987
2988         ptr = buf + sizeof(struct domain_hdr);
2989         remain = len - sizeof(struct domain_hdr);
2990
2991         while (remain > 0) {
2992                 uint8_t label_len = *ptr;
2993
2994                 if (label_len == 0x00) {
2995                         uint8_t class;
2996                         struct qtype_qclass *q =
2997                                 (struct qtype_qclass *)(ptr + 1);
2998
2999                         if (remain < sizeof(*q)) {
3000                                 DBG("Dropped malformed DNS query");
3001                                 return -EINVAL;
3002                         }
3003
3004                         class = ntohs(q->qclass);
3005                         if (class != 1 && class != 255) {
3006                                 DBG("Dropped non-IN DNS class %d", class);
3007                                 return -EINVAL;
3008                         }
3009
3010                         ptr += sizeof(*q) + 1;
3011                         remain -= (sizeof(*q) + 1);
3012                         break;
3013                 }
3014
3015                 if (used + label_len + 1 > size)
3016                         return -ENOBUFS;
3017
3018                 strncat(name, (char *) (ptr + 1), label_len);
3019                 strcat(name, ".");
3020
3021                 used += label_len + 1;
3022
3023                 ptr += label_len + 1;
3024                 remain -= label_len + 1;
3025         }
3026
3027         if (arcount && remain >= sizeof(struct domain_rr) + 1 && !ptr[0] &&
3028                 ptr[1] == opt_edns0_type[0] && ptr[2] == opt_edns0_type[1]) {
3029                 struct domain_rr *edns0 = (struct domain_rr *)(ptr + 1);
3030
3031                 DBG("EDNS0 buffer size %u", ntohs(edns0->class));
3032         } else if (!arcount && remain) {
3033                 DBG("DNS request with %d garbage bytes", remain);
3034         }
3035
3036         debug("query %s", name);
3037
3038         return 0;
3039 }
3040
3041 static void client_reset(struct tcp_partial_client_data *client)
3042 {
3043         if (!client)
3044                 return;
3045
3046         if (client->channel) {
3047                 debug("client %d closing",
3048                         g_io_channel_unix_get_fd(client->channel));
3049
3050                 g_io_channel_unref(client->channel);
3051                 client->channel = NULL;
3052         }
3053
3054         if (client->watch > 0) {
3055                 g_source_remove(client->watch);
3056                 client->watch = 0;
3057         }
3058
3059         if (client->timeout > 0) {
3060                 g_source_remove(client->timeout);
3061                 client->timeout = 0;
3062         }
3063
3064         g_free(client->buf);
3065         client->buf = NULL;
3066
3067         client->buf_end = 0;
3068 }
3069
3070 static unsigned int get_msg_len(unsigned char *buf)
3071 {
3072         return buf[0]<<8 | buf[1];
3073 }
3074
3075 static bool read_tcp_data(struct tcp_partial_client_data *client,
3076                                 void *client_addr, socklen_t client_addr_len,
3077                                 int read_len)
3078 {
3079         char query[TCP_MAX_BUF_LEN];
3080         struct request_data *req;
3081         int client_sk, err;
3082         unsigned int msg_len;
3083         GSList *list;
3084         bool waiting_for_connect = false;
3085         int qtype = 0;
3086         struct cache_entry *entry;
3087
3088         client_sk = g_io_channel_unix_get_fd(client->channel);
3089
3090         if (read_len == 0) {
3091                 debug("client %d closed, pending %d bytes",
3092                         client_sk, client->buf_end);
3093                 g_hash_table_remove(partial_tcp_req_table,
3094                                         GINT_TO_POINTER(client_sk));
3095                 return false;
3096         }
3097
3098         debug("client %d received %d bytes", client_sk, read_len);
3099
3100         client->buf_end += read_len;
3101
3102         if (client->buf_end < 2)
3103                 return true;
3104
3105         msg_len = get_msg_len(client->buf);
3106         if (msg_len > TCP_MAX_BUF_LEN) {
3107                 debug("client %d sent too much data %d", client_sk, msg_len);
3108                 g_hash_table_remove(partial_tcp_req_table,
3109                                         GINT_TO_POINTER(client_sk));
3110                 return false;
3111         }
3112
3113 read_another:
3114         debug("client %d msg len %d end %d past end %d", client_sk, msg_len,
3115                 client->buf_end, client->buf_end - (msg_len + 2));
3116
3117         if (client->buf_end < (msg_len + 2)) {
3118                 debug("client %d still missing %d bytes",
3119                         client_sk,
3120                         msg_len + 2 - client->buf_end);
3121                 return true;
3122         }
3123
3124         debug("client %d all data %d received", client_sk, msg_len);
3125
3126         err = parse_request(client->buf + 2, msg_len,
3127                         query, sizeof(query));
3128         if (err < 0 || (g_slist_length(server_list) == 0)) {
3129                 send_response(client_sk, client->buf, msg_len + 2,
3130                         NULL, 0, IPPROTO_TCP);
3131                 return true;
3132         }
3133
3134         req = g_try_new0(struct request_data, 1);
3135         if (!req)
3136                 return true;
3137
3138         memcpy(&req->sa, client_addr, client_addr_len);
3139         req->sa_len = client_addr_len;
3140         req->client_sk = client_sk;
3141         req->protocol = IPPROTO_TCP;
3142         req->family = client->family;
3143
3144         req->srcid = client->buf[2] | (client->buf[3] << 8);
3145         req->dstid = get_id();
3146         req->altid = get_id();
3147         req->request_len = msg_len + 2;
3148
3149         client->buf[2] = req->dstid & 0xff;
3150         client->buf[3] = req->dstid >> 8;
3151
3152         req->numserv = 0;
3153         req->ifdata = client->ifdata;
3154         req->append_domain = false;
3155
3156         /*
3157          * Check if the answer is found in the cache before
3158          * creating sockets to the server.
3159          */
3160         entry = cache_check(client->buf, &qtype, IPPROTO_TCP);
3161         if (entry) {
3162                 int ttl_left = 0;
3163                 struct cache_data *data;
3164
3165                 debug("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
3166                 if (qtype == 1)
3167                         data = entry->ipv4;
3168                 else
3169                         data = entry->ipv6;
3170
3171                 if (data) {
3172                         ttl_left = data->valid_until - time(NULL);
3173                         entry->hits++;
3174
3175                         send_cached_response(client_sk, data->data,
3176                                         data->data_len, NULL, 0, IPPROTO_TCP,
3177                                         req->srcid, data->answers, ttl_left);
3178
3179                         g_free(req);
3180                         goto out;
3181                 } else
3182                         debug("data missing, ignoring cache for this query");
3183         }
3184
3185         for (list = server_list; list; list = list->next) {
3186                 struct server_data *data = list->data;
3187
3188                 if (data->protocol != IPPROTO_UDP || !data->enabled)
3189                         continue;
3190
3191                 if (!create_server(data->index, NULL, data->server,
3192                                         IPPROTO_TCP))
3193                         continue;
3194
3195                 waiting_for_connect = true;
3196         }
3197
3198         if (!waiting_for_connect) {
3199                 /* No server is waiting for connect */
3200                 send_response(client_sk, client->buf,
3201                         req->request_len, NULL, 0, IPPROTO_TCP);
3202                 g_free(req);
3203                 return true;
3204         }
3205
3206         /*
3207          * The server is not connected yet.
3208          * Copy the relevant buffers.
3209          * The request will actually be sent once we're
3210          * properly connected over TCP to the nameserver.
3211          */
3212         req->request = g_try_malloc0(req->request_len);
3213         if (!req->request) {
3214                 send_response(client_sk, client->buf,
3215                         req->request_len, NULL, 0, IPPROTO_TCP);
3216                 g_free(req);
3217                 goto out;
3218         }
3219         memcpy(req->request, client->buf, req->request_len);
3220
3221         req->name = g_try_malloc0(sizeof(query));
3222         if (!req->name) {
3223                 send_response(client_sk, client->buf,
3224                         req->request_len, NULL, 0, IPPROTO_TCP);
3225                 g_free(req->request);
3226                 g_free(req);
3227                 goto out;
3228         }
3229         memcpy(req->name, query, sizeof(query));
3230
3231         req->timeout = g_timeout_add_seconds(30, request_timeout, req);
3232
3233         request_list = g_slist_append(request_list, req);
3234
3235 out:
3236         if (client->buf_end > (msg_len + 2)) {
3237                 debug("client %d buf %p -> %p end %d len %d new %d",
3238                         client_sk,
3239                         client->buf + msg_len + 2,
3240                         client->buf, client->buf_end,
3241                         TCP_MAX_BUF_LEN - client->buf_end,
3242                         client->buf_end - (msg_len + 2));
3243                 memmove(client->buf, client->buf + msg_len + 2,
3244                         TCP_MAX_BUF_LEN - client->buf_end);
3245                 client->buf_end = client->buf_end - (msg_len + 2);
3246
3247                 /*
3248                  * If we have a full message waiting, just read it
3249                  * immediately.
3250                  */
3251                 msg_len = get_msg_len(client->buf);
3252                 if ((msg_len + 2) == client->buf_end) {
3253                         debug("client %d reading another %d bytes", client_sk,
3254                                                                 msg_len + 2);
3255                         goto read_another;
3256                 }
3257         } else {
3258                 debug("client %d clearing reading buffer", client_sk);
3259
3260                 client->buf_end = 0;
3261                 memset(client->buf, 0, TCP_MAX_BUF_LEN);
3262
3263                 /*
3264                  * We received all the packets from client so we must also
3265                  * remove the timeout handler here otherwise we might get
3266                  * timeout while waiting the results from server.
3267                  */
3268                 g_source_remove(client->timeout);
3269                 client->timeout = 0;
3270         }
3271
3272         return true;
3273 }
3274
3275 static gboolean tcp_client_event(GIOChannel *channel, GIOCondition condition,
3276                                 gpointer user_data)
3277 {
3278         struct tcp_partial_client_data *client = user_data;
3279         struct sockaddr_in6 client_addr6;
3280         socklen_t client_addr6_len = sizeof(client_addr6);
3281         struct sockaddr_in client_addr4;
3282         socklen_t client_addr4_len = sizeof(client_addr4);
3283         void *client_addr;
3284         socklen_t *client_addr_len;
3285         int len, client_sk;
3286
3287         client_sk = g_io_channel_unix_get_fd(channel);
3288
3289         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3290                 g_hash_table_remove(partial_tcp_req_table,
3291                                         GINT_TO_POINTER(client_sk));
3292
3293                 connman_error("Error with TCP client %d channel", client_sk);
3294                 return FALSE;
3295         }
3296
3297         switch (client->family) {
3298         case AF_INET:
3299                 client_addr = &client_addr4;
3300                 client_addr_len = &client_addr4_len;
3301                 break;
3302         case AF_INET6:
3303                 client_addr = &client_addr6;
3304                 client_addr_len = &client_addr6_len;
3305                 break;
3306         default:
3307                 g_hash_table_remove(partial_tcp_req_table,
3308                                         GINT_TO_POINTER(client_sk));
3309                 connman_error("client %p corrupted", client);
3310                 return FALSE;
3311         }
3312
3313         len = recvfrom(client_sk, client->buf + client->buf_end,
3314                         TCP_MAX_BUF_LEN - client->buf_end, 0,
3315                         client_addr, client_addr_len);
3316         if (len < 0) {
3317                 if (errno == EAGAIN || errno == EWOULDBLOCK)
3318                         return TRUE;
3319
3320                 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3321                         strerror(errno));
3322                 g_hash_table_remove(partial_tcp_req_table,
3323                                         GINT_TO_POINTER(client_sk));
3324                 return FALSE;
3325         }
3326
3327         return read_tcp_data(client, client_addr, *client_addr_len, len);
3328 }
3329
3330 static gboolean client_timeout(gpointer user_data)
3331 {
3332         struct tcp_partial_client_data *client = user_data;
3333         int sock;
3334
3335         sock = g_io_channel_unix_get_fd(client->channel);
3336
3337         debug("client %d timeout pending %d bytes", sock, client->buf_end);
3338
3339         g_hash_table_remove(partial_tcp_req_table, GINT_TO_POINTER(sock));
3340
3341         return FALSE;
3342 }
3343
3344 static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
3345                                 struct listener_data *ifdata, int family,
3346                                 guint *listener_watch)
3347 {
3348         int sk, client_sk, len;
3349         unsigned int msg_len;
3350         struct tcp_partial_client_data *client;
3351         struct sockaddr_in6 client_addr6;
3352         socklen_t client_addr6_len = sizeof(client_addr6);
3353         struct sockaddr_in client_addr4;
3354         socklen_t client_addr4_len = sizeof(client_addr4);
3355         void *client_addr;
3356         socklen_t *client_addr_len;
3357         struct timeval tv;
3358         fd_set readfds;
3359
3360         debug("condition 0x%02x channel %p ifdata %p family %d",
3361                 condition, channel, ifdata, family);
3362
3363         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3364                 if (*listener_watch > 0)
3365                         g_source_remove(*listener_watch);
3366                 *listener_watch = 0;
3367
3368                 connman_error("Error with TCP listener channel");
3369
3370                 return false;
3371         }
3372
3373         sk = g_io_channel_unix_get_fd(channel);
3374
3375         if (family == AF_INET) {
3376                 client_addr = &client_addr4;
3377                 client_addr_len = &client_addr4_len;
3378         } else {
3379                 client_addr = &client_addr6;
3380                 client_addr_len = &client_addr6_len;
3381         }
3382
3383         tv.tv_sec = tv.tv_usec = 0;
3384         FD_ZERO(&readfds);
3385         FD_SET(sk, &readfds);
3386
3387         select(sk + 1, &readfds, NULL, NULL, &tv);
3388         if (FD_ISSET(sk, &readfds)) {
3389                 client_sk = accept(sk, client_addr, client_addr_len);
3390                 debug("client %d accepted", client_sk);
3391         } else {
3392                 debug("No data to read from master %d, waiting.", sk);
3393                 return true;
3394         }
3395
3396         if (client_sk < 0) {
3397                 connman_error("Accept failure on TCP listener");
3398                 *listener_watch = 0;
3399                 return false;
3400         }
3401
3402         fcntl(client_sk, F_SETFL, O_NONBLOCK);
3403
3404         client = g_hash_table_lookup(partial_tcp_req_table,
3405                                         GINT_TO_POINTER(client_sk));
3406         if (!client) {
3407                 client = g_try_new0(struct tcp_partial_client_data, 1);
3408                 if (!client) {
3409                         close(client_sk);
3410                         return false;
3411                 }
3412
3413                 g_hash_table_insert(partial_tcp_req_table,
3414                                         GINT_TO_POINTER(client_sk),
3415                                         client);
3416
3417                 client->channel = g_io_channel_unix_new(client_sk);
3418                 g_io_channel_set_close_on_unref(client->channel, TRUE);
3419
3420                 client->watch = g_io_add_watch(client->channel,
3421                                                 G_IO_IN, tcp_client_event,
3422                                                 (gpointer)client);
3423
3424                 client->ifdata = ifdata;
3425
3426                 debug("client %d created %p", client_sk, client);
3427         } else {
3428                 debug("client %d already exists %p", client_sk, client);
3429         }
3430
3431         if (!client->buf) {
3432                 client->buf = g_try_malloc(TCP_MAX_BUF_LEN);
3433                 if (!client->buf)
3434                         return false;
3435         }
3436         memset(client->buf, 0, TCP_MAX_BUF_LEN);
3437         client->buf_end = 0;
3438         client->family = family;
3439
3440         if (client->timeout == 0)
3441                 client->timeout = g_timeout_add_seconds(2, client_timeout,
3442                                                         client);
3443
3444         /*
3445          * Check how much data there is. If all is there, then we can
3446          * proceed normally, otherwise read the bits until everything
3447          * is received or timeout occurs.
3448          */
3449         len = recv(client_sk, client->buf, TCP_MAX_BUF_LEN, 0);
3450         if (len < 0) {
3451                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
3452                         debug("client %d no data to read, waiting", client_sk);
3453                         return true;
3454                 }
3455
3456                 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3457                         strerror(errno));
3458                 g_hash_table_remove(partial_tcp_req_table,
3459                                         GINT_TO_POINTER(client_sk));
3460                 return true;
3461         }
3462
3463         if (len < 2) {
3464                 debug("client %d not enough data to read, waiting", client_sk);
3465                 client->buf_end += len;
3466                 return true;
3467         }
3468
3469         msg_len = get_msg_len(client->buf);
3470         if (msg_len > TCP_MAX_BUF_LEN) {
3471                 debug("client %d invalid message length %u ignoring packet",
3472                         client_sk, msg_len);
3473                 g_hash_table_remove(partial_tcp_req_table,
3474                                         GINT_TO_POINTER(client_sk));
3475                 return true;
3476         }
3477
3478         /*
3479          * The packet length bytes do not contain the total message length,
3480          * that is the reason to -2 below.
3481          */
3482         if (msg_len != (unsigned int)(len - 2)) {
3483                 debug("client %d sent %d bytes but expecting %u pending %d",
3484                         client_sk, len, msg_len + 2, msg_len + 2 - len);
3485
3486                 client->buf_end += len;
3487                 return true;
3488         }
3489
3490         return read_tcp_data(client, client_addr, *client_addr_len, len);
3491 }
3492
3493 static gboolean tcp4_listener_event(GIOChannel *channel, GIOCondition condition,
3494                                 gpointer user_data)
3495 {
3496         struct listener_data *ifdata = user_data;
3497
3498         return tcp_listener_event(channel, condition, ifdata, AF_INET,
3499                                 &ifdata->tcp4_listener_watch);
3500 }
3501
3502 static gboolean tcp6_listener_event(GIOChannel *channel, GIOCondition condition,
3503                                 gpointer user_data)
3504 {
3505         struct listener_data *ifdata = user_data;
3506
3507         return tcp_listener_event(channel, condition, user_data, AF_INET6,
3508                                 &ifdata->tcp6_listener_watch);
3509 }
3510
3511 static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
3512                                 struct listener_data *ifdata, int family,
3513                                 guint *listener_watch)
3514 {
3515         unsigned char buf[768];
3516         char query[512];
3517         struct request_data *req;
3518         struct sockaddr_in6 client_addr6;
3519         socklen_t client_addr6_len = sizeof(client_addr6);
3520         struct sockaddr_in client_addr4;
3521         socklen_t client_addr4_len = sizeof(client_addr4);
3522         void *client_addr;
3523         socklen_t *client_addr_len;
3524         int sk, err, len;
3525
3526         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3527                 connman_error("Error with UDP listener channel");
3528                 *listener_watch = 0;
3529                 return false;
3530         }
3531
3532         sk = g_io_channel_unix_get_fd(channel);
3533
3534         if (family == AF_INET) {
3535                 client_addr = &client_addr4;
3536                 client_addr_len = &client_addr4_len;
3537         } else {
3538                 client_addr = &client_addr6;
3539                 client_addr_len = &client_addr6_len;
3540         }
3541
3542         memset(client_addr, 0, *client_addr_len);
3543         len = recvfrom(sk, buf, sizeof(buf), 0, client_addr, client_addr_len);
3544         if (len < 2)
3545                 return true;
3546
3547         debug("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
3548
3549         err = parse_request(buf, len, query, sizeof(query));
3550         if (err < 0 || (g_slist_length(server_list) == 0)) {
3551                 send_response(sk, buf, len, client_addr,
3552                                 *client_addr_len, IPPROTO_UDP);
3553                 return true;
3554         }
3555
3556         req = g_try_new0(struct request_data, 1);
3557         if (!req)
3558                 return true;
3559
3560         memcpy(&req->sa, client_addr, *client_addr_len);
3561         req->sa_len = *client_addr_len;
3562         req->client_sk = 0;
3563         req->protocol = IPPROTO_UDP;
3564         req->family = family;
3565
3566         req->srcid = buf[0] | (buf[1] << 8);
3567         req->dstid = get_id();
3568         req->altid = get_id();
3569         req->request_len = len;
3570
3571         buf[0] = req->dstid & 0xff;
3572         buf[1] = req->dstid >> 8;
3573
3574         req->numserv = 0;
3575         req->ifdata = ifdata;
3576         req->append_domain = false;
3577
3578         if (resolv(req, buf, query)) {
3579                 /* a cached result was sent, so the request can be released */
3580                 g_free(req);
3581                 return true;
3582         }
3583
3584         req->name = g_strdup(query);
3585         req->request = g_malloc(len);
3586         memcpy(req->request, buf, len);
3587         req->timeout = g_timeout_add_seconds(5, request_timeout, req);
3588         request_list = g_slist_append(request_list, req);
3589
3590         return true;
3591 }
3592
3593 static gboolean udp4_listener_event(GIOChannel *channel, GIOCondition condition,
3594                                 gpointer user_data)
3595 {
3596         struct listener_data *ifdata = user_data;
3597
3598         return udp_listener_event(channel, condition, ifdata, AF_INET,
3599                                 &ifdata->udp4_listener_watch);
3600 }
3601
3602 static gboolean udp6_listener_event(GIOChannel *channel, GIOCondition condition,
3603                                 gpointer user_data)
3604 {
3605         struct listener_data *ifdata = user_data;
3606
3607         return udp_listener_event(channel, condition, user_data, AF_INET6,
3608                                 &ifdata->udp6_listener_watch);
3609 }
3610
3611 static GIOChannel *get_listener(int family, int protocol, int index)
3612 {
3613         GIOChannel *channel;
3614         const char *proto;
3615         union {
3616                 struct sockaddr sa;
3617                 struct sockaddr_in6 sin6;
3618                 struct sockaddr_in sin;
3619         } s;
3620         socklen_t slen;
3621         int sk, type;
3622         char *interface;
3623
3624         debug("family %d protocol %d index %d", family, protocol, index);
3625
3626         switch (protocol) {
3627         case IPPROTO_UDP:
3628                 proto = "UDP";
3629                 type = SOCK_DGRAM | SOCK_CLOEXEC;
3630                 break;
3631
3632         case IPPROTO_TCP:
3633                 proto = "TCP";
3634                 type = SOCK_STREAM | SOCK_CLOEXEC;
3635                 break;
3636
3637         default:
3638                 return NULL;
3639         }
3640
3641         sk = socket(family, type, protocol);
3642         if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
3643                 connman_error("No IPv6 support");
3644                 return NULL;
3645         }
3646
3647         if (sk < 0) {
3648                 connman_error("Failed to create %s listener socket", proto);
3649                 return NULL;
3650         }
3651
3652         interface = connman_inet_ifname(index);
3653         if (!interface || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
3654                                         interface,
3655                                         strlen(interface) + 1) < 0) {
3656                 connman_error("Failed to bind %s listener interface "
3657                         "for %s (%d/%s)",
3658                         proto, family == AF_INET ? "IPv4" : "IPv6",
3659                         -errno, strerror(errno));
3660                 close(sk);
3661                 g_free(interface);
3662                 return NULL;
3663         }
3664         g_free(interface);
3665
3666         if (family == AF_INET6) {
3667                 memset(&s.sin6, 0, sizeof(s.sin6));
3668                 s.sin6.sin6_family = AF_INET6;
3669                 s.sin6.sin6_port = htons(53);
3670                 slen = sizeof(s.sin6);
3671
3672                 if (__connman_inet_get_interface_address(index,
3673                                                 AF_INET6,
3674                                                 &s.sin6.sin6_addr) < 0) {
3675                         /* So we could not find suitable IPv6 address for
3676                          * the interface. This could happen if we have
3677                          * disabled IPv6 for the interface.
3678                          */
3679                         close(sk);
3680                         return NULL;
3681                 }
3682
3683         } else if (family == AF_INET) {
3684                 memset(&s.sin, 0, sizeof(s.sin));
3685                 s.sin.sin_family = AF_INET;
3686                 s.sin.sin_port = htons(53);
3687                 slen = sizeof(s.sin);
3688
3689                 if (__connman_inet_get_interface_address(index,
3690                                                 AF_INET,
3691                                                 &s.sin.sin_addr) < 0) {
3692                         close(sk);
3693                         return NULL;
3694                 }
3695         } else {
3696                 close(sk);
3697                 return NULL;
3698         }
3699
3700         if (bind(sk, &s.sa, slen) < 0) {
3701                 connman_error("Failed to bind %s listener socket", proto);
3702                 close(sk);
3703                 return NULL;
3704         }
3705
3706         if (protocol == IPPROTO_TCP) {
3707
3708                 if (listen(sk, 10) < 0) {
3709                         connman_error("Failed to listen on TCP socket %d/%s",
3710                                 -errno, strerror(errno));
3711                         close(sk);
3712                         return NULL;
3713                 }
3714
3715                 fcntl(sk, F_SETFL, O_NONBLOCK);
3716         }
3717
3718         channel = g_io_channel_unix_new(sk);
3719         if (!channel) {
3720                 connman_error("Failed to create %s listener channel", proto);
3721                 close(sk);
3722                 return NULL;
3723         }
3724
3725         g_io_channel_set_close_on_unref(channel, TRUE);
3726
3727         return channel;
3728 }
3729
3730 #define UDP_IPv4_FAILED 0x01
3731 #define TCP_IPv4_FAILED 0x02
3732 #define UDP_IPv6_FAILED 0x04
3733 #define TCP_IPv6_FAILED 0x08
3734 #define UDP_FAILED (UDP_IPv4_FAILED | UDP_IPv6_FAILED)
3735 #define TCP_FAILED (TCP_IPv4_FAILED | TCP_IPv6_FAILED)
3736 #define IPv6_FAILED (UDP_IPv6_FAILED | TCP_IPv6_FAILED)
3737 #define IPv4_FAILED (UDP_IPv4_FAILED | TCP_IPv4_FAILED)
3738
3739 static int create_dns_listener(int protocol, struct listener_data *ifdata)
3740 {
3741         int ret = 0;
3742
3743         if (protocol == IPPROTO_TCP) {
3744                 ifdata->tcp4_listener_channel = get_listener(AF_INET, protocol,
3745                                                         ifdata->index);
3746                 if (ifdata->tcp4_listener_channel)
3747                         ifdata->tcp4_listener_watch =
3748                                 g_io_add_watch(ifdata->tcp4_listener_channel,
3749                                         G_IO_IN, tcp4_listener_event,
3750                                         (gpointer)ifdata);
3751                 else
3752                         ret |= TCP_IPv4_FAILED;
3753
3754                 ifdata->tcp6_listener_channel = get_listener(AF_INET6, protocol,
3755                                                         ifdata->index);
3756                 if (ifdata->tcp6_listener_channel)
3757                         ifdata->tcp6_listener_watch =
3758                                 g_io_add_watch(ifdata->tcp6_listener_channel,
3759                                         G_IO_IN, tcp6_listener_event,
3760                                         (gpointer)ifdata);
3761                 else
3762                         ret |= TCP_IPv6_FAILED;
3763         } else {
3764                 ifdata->udp4_listener_channel = get_listener(AF_INET, protocol,
3765                                                         ifdata->index);
3766                 if (ifdata->udp4_listener_channel)
3767                         ifdata->udp4_listener_watch =
3768                                 g_io_add_watch(ifdata->udp4_listener_channel,
3769                                         G_IO_IN, udp4_listener_event,
3770                                         (gpointer)ifdata);
3771                 else
3772                         ret |= UDP_IPv4_FAILED;
3773
3774                 ifdata->udp6_listener_channel = get_listener(AF_INET6, protocol,
3775                                                         ifdata->index);
3776                 if (ifdata->udp6_listener_channel)
3777                         ifdata->udp6_listener_watch =
3778                                 g_io_add_watch(ifdata->udp6_listener_channel,
3779                                         G_IO_IN, udp6_listener_event,
3780                                         (gpointer)ifdata);
3781                 else
3782                         ret |= UDP_IPv6_FAILED;
3783         }
3784
3785         return ret;
3786 }
3787
3788 static void destroy_udp_listener(struct listener_data *ifdata)
3789 {
3790         DBG("index %d", ifdata->index);
3791
3792         if (ifdata->udp4_listener_watch > 0)
3793                 g_source_remove(ifdata->udp4_listener_watch);
3794
3795         if (ifdata->udp6_listener_watch > 0)
3796                 g_source_remove(ifdata->udp6_listener_watch);
3797
3798         if (ifdata->udp4_listener_channel)
3799                 g_io_channel_unref(ifdata->udp4_listener_channel);
3800         if (ifdata->udp6_listener_channel)
3801                 g_io_channel_unref(ifdata->udp6_listener_channel);
3802 }
3803
3804 static void destroy_tcp_listener(struct listener_data *ifdata)
3805 {
3806         DBG("index %d", ifdata->index);
3807
3808         if (ifdata->tcp4_listener_watch > 0)
3809                 g_source_remove(ifdata->tcp4_listener_watch);
3810         if (ifdata->tcp6_listener_watch > 0)
3811                 g_source_remove(ifdata->tcp6_listener_watch);
3812
3813         if (ifdata->tcp4_listener_channel)
3814                 g_io_channel_unref(ifdata->tcp4_listener_channel);
3815         if (ifdata->tcp6_listener_channel)
3816                 g_io_channel_unref(ifdata->tcp6_listener_channel);
3817 }
3818
3819 static int create_listener(struct listener_data *ifdata)
3820 {
3821         int err, index;
3822
3823         err = create_dns_listener(IPPROTO_UDP, ifdata);
3824         if ((err & UDP_FAILED) == UDP_FAILED)
3825                 return -EIO;
3826
3827         err |= create_dns_listener(IPPROTO_TCP, ifdata);
3828         if ((err & TCP_FAILED) == TCP_FAILED) {
3829                 destroy_udp_listener(ifdata);
3830                 return -EIO;
3831         }
3832
3833         index = connman_inet_ifindex("lo");
3834         if (ifdata->index == index) {
3835                 if ((err & IPv6_FAILED) != IPv6_FAILED)
3836                         __connman_resolvfile_append(index, NULL, "::1");
3837
3838                 if ((err & IPv4_FAILED) != IPv4_FAILED)
3839                         __connman_resolvfile_append(index, NULL, "127.0.0.1");
3840         }
3841
3842         return 0;
3843 }
3844
3845 static void destroy_listener(struct listener_data *ifdata)
3846 {
3847         int index;
3848         GSList *list;
3849
3850         index = connman_inet_ifindex("lo");
3851         if (ifdata->index == index) {
3852                 __connman_resolvfile_remove(index, NULL, "127.0.0.1");
3853                 __connman_resolvfile_remove(index, NULL, "::1");
3854         }
3855
3856         for (list = request_list; list; list = list->next) {
3857                 struct request_data *req = list->data;
3858
3859                 debug("Dropping request (id 0x%04x -> 0x%04x)",
3860                                                 req->srcid, req->dstid);
3861                 destroy_request_data(req);
3862                 list->data = NULL;
3863         }
3864
3865         g_slist_free(request_list);
3866         request_list = NULL;
3867
3868         destroy_tcp_listener(ifdata);
3869         destroy_udp_listener(ifdata);
3870 }
3871
3872 int __connman_dnsproxy_add_listener(int index)
3873 {
3874         struct listener_data *ifdata;
3875         int err;
3876
3877         DBG("index %d", index);
3878
3879         if (index < 0)
3880                 return -EINVAL;
3881
3882         if (!listener_table)
3883                 return -ENOENT;
3884
3885         if (g_hash_table_lookup(listener_table, GINT_TO_POINTER(index)))
3886                 return 0;
3887
3888         ifdata = g_try_new0(struct listener_data, 1);
3889         if (!ifdata)
3890                 return -ENOMEM;
3891
3892         ifdata->index = index;
3893         ifdata->udp4_listener_channel = NULL;
3894         ifdata->udp4_listener_watch = 0;
3895         ifdata->tcp4_listener_channel = NULL;
3896         ifdata->tcp4_listener_watch = 0;
3897         ifdata->udp6_listener_channel = NULL;
3898         ifdata->udp6_listener_watch = 0;
3899         ifdata->tcp6_listener_channel = NULL;
3900         ifdata->tcp6_listener_watch = 0;
3901
3902         err = create_listener(ifdata);
3903         if (err < 0) {
3904                 connman_error("Couldn't create listener for index %d err %d",
3905                                 index, err);
3906                 g_free(ifdata);
3907                 return err;
3908         }
3909         g_hash_table_insert(listener_table, GINT_TO_POINTER(ifdata->index),
3910                         ifdata);
3911         return 0;
3912 }
3913
3914 void __connman_dnsproxy_remove_listener(int index)
3915 {
3916         struct listener_data *ifdata;
3917
3918         DBG("index %d", index);
3919
3920         if (!listener_table)
3921                 return;
3922
3923         ifdata = g_hash_table_lookup(listener_table, GINT_TO_POINTER(index));
3924         if (!ifdata)
3925                 return;
3926
3927         destroy_listener(ifdata);
3928
3929         g_hash_table_remove(listener_table, GINT_TO_POINTER(index));
3930 }
3931
3932 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
3933 {
3934         int index = GPOINTER_TO_INT(key);
3935         struct listener_data *ifdata = value;
3936
3937         DBG("index %d", index);
3938
3939         destroy_listener(ifdata);
3940 }
3941
3942 static void free_partial_reqs(gpointer value)
3943 {
3944         struct tcp_partial_client_data *data = value;
3945
3946         client_reset(data);
3947         g_free(data);
3948 }
3949
3950 int __connman_dnsproxy_init(void)
3951 {
3952         int err, index;
3953
3954         DBG("");
3955
3956         listener_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
3957                                                         NULL, g_free);
3958
3959         partial_tcp_req_table = g_hash_table_new_full(g_direct_hash,
3960                                                         g_direct_equal,
3961                                                         NULL,
3962                                                         free_partial_reqs);
3963
3964         index = connman_inet_ifindex("lo");
3965         err = __connman_dnsproxy_add_listener(index);
3966         if (err < 0)
3967                 return err;
3968
3969         err = connman_notifier_register(&dnsproxy_notifier);
3970         if (err < 0)
3971                 goto destroy;
3972
3973         return 0;
3974
3975 destroy:
3976         __connman_dnsproxy_remove_listener(index);
3977         g_hash_table_destroy(listener_table);
3978         g_hash_table_destroy(partial_tcp_req_table);
3979
3980         return err;
3981 }
3982
3983 int __connman_dnsproxy_set_mdns(int index, bool enabled)
3984 {
3985         return -ENOTSUP;
3986 }
3987
3988 void __connman_dnsproxy_cleanup(void)
3989 {
3990         DBG("");
3991
3992         if (cache_timer) {
3993                 g_source_remove(cache_timer);
3994                 cache_timer = 0;
3995         }
3996
3997         if (cache) {
3998                 g_hash_table_destroy(cache);
3999                 cache = NULL;
4000         }
4001
4002         connman_notifier_unregister(&dnsproxy_notifier);
4003
4004         g_hash_table_foreach(listener_table, remove_listener, NULL);
4005
4006         g_hash_table_destroy(listener_table);
4007
4008         g_hash_table_destroy(partial_tcp_req_table);
4009
4010         if (ipv4_resolve)
4011                 g_resolv_unref(ipv4_resolve);
4012         if (ipv6_resolve)
4013                 g_resolv_unref(ipv6_resolve);
4014 }