Imported Upstream version 1.37
[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 authorative */
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 const struct connman_notifier dnsproxy_notifier = {
2916         .name                   = "dnsproxy",
2917         .default_changed        = dnsproxy_default_changed,
2918         .offline_mode           = dnsproxy_offline_mode,
2919 };
2920
2921 static const unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2922
2923 static int parse_request(unsigned char *buf, size_t len,
2924                                         char *name, unsigned int size)
2925 {
2926         struct domain_hdr *hdr = (void *) buf;
2927         uint16_t qdcount = ntohs(hdr->qdcount);
2928         uint16_t ancount = ntohs(hdr->ancount);
2929         uint16_t nscount = ntohs(hdr->nscount);
2930         uint16_t arcount = ntohs(hdr->arcount);
2931         unsigned char *ptr;
2932         unsigned int remain, used = 0;
2933
2934         if (len < sizeof(*hdr) + sizeof(struct qtype_qclass) ||
2935                         hdr->qr || qdcount != 1 || ancount || nscount) {
2936                 DBG("Dropped DNS request qr %d with len %zd qdcount %d "
2937                         "ancount %d nscount %d", hdr->qr, len, qdcount, ancount,
2938                         nscount);
2939
2940                 return -EINVAL;
2941         }
2942
2943         if (!name || !size)
2944                 return -EINVAL;
2945
2946         debug("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2947                                         hdr->id, hdr->qr, hdr->opcode,
2948                                                         qdcount, arcount);
2949
2950         name[0] = '\0';
2951
2952         ptr = buf + sizeof(struct domain_hdr);
2953         remain = len - sizeof(struct domain_hdr);
2954
2955         while (remain > 0) {
2956                 uint8_t label_len = *ptr;
2957
2958                 if (label_len == 0x00) {
2959                         uint8_t class;
2960                         struct qtype_qclass *q =
2961                                 (struct qtype_qclass *)(ptr + 1);
2962
2963                         if (remain < sizeof(*q)) {
2964                                 DBG("Dropped malformed DNS query");
2965                                 return -EINVAL;
2966                         }
2967
2968                         class = ntohs(q->qclass);
2969                         if (class != 1 && class != 255) {
2970                                 DBG("Dropped non-IN DNS class %d", class);
2971                                 return -EINVAL;
2972                         }
2973
2974                         ptr += sizeof(*q) + 1;
2975                         remain -= (sizeof(*q) + 1);
2976                         break;
2977                 }
2978
2979                 if (used + label_len + 1 > size)
2980                         return -ENOBUFS;
2981
2982                 strncat(name, (char *) (ptr + 1), label_len);
2983                 strcat(name, ".");
2984
2985                 used += label_len + 1;
2986
2987                 ptr += label_len + 1;
2988                 remain -= label_len + 1;
2989         }
2990
2991         if (arcount && remain >= sizeof(struct domain_rr) + 1 && !ptr[0] &&
2992                 ptr[1] == opt_edns0_type[0] && ptr[2] == opt_edns0_type[1]) {
2993                 struct domain_rr *edns0 = (struct domain_rr *)(ptr + 1);
2994
2995                 DBG("EDNS0 buffer size %u", ntohs(edns0->class));
2996         } else if (!arcount && remain) {
2997                 DBG("DNS request with %d garbage bytes", remain);
2998         }
2999
3000         debug("query %s", name);
3001
3002         return 0;
3003 }
3004
3005 static void client_reset(struct tcp_partial_client_data *client)
3006 {
3007         if (!client)
3008                 return;
3009
3010         if (client->channel) {
3011                 debug("client %d closing",
3012                         g_io_channel_unix_get_fd(client->channel));
3013
3014                 g_io_channel_unref(client->channel);
3015                 client->channel = NULL;
3016         }
3017
3018         if (client->watch > 0) {
3019                 g_source_remove(client->watch);
3020                 client->watch = 0;
3021         }
3022
3023         if (client->timeout > 0) {
3024                 g_source_remove(client->timeout);
3025                 client->timeout = 0;
3026         }
3027
3028         g_free(client->buf);
3029         client->buf = NULL;
3030
3031         client->buf_end = 0;
3032 }
3033
3034 static unsigned int get_msg_len(unsigned char *buf)
3035 {
3036         return buf[0]<<8 | buf[1];
3037 }
3038
3039 static bool read_tcp_data(struct tcp_partial_client_data *client,
3040                                 void *client_addr, socklen_t client_addr_len,
3041                                 int read_len)
3042 {
3043         char query[TCP_MAX_BUF_LEN];
3044         struct request_data *req;
3045         int client_sk, err;
3046         unsigned int msg_len;
3047         GSList *list;
3048         bool waiting_for_connect = false;
3049         int qtype = 0;
3050         struct cache_entry *entry;
3051
3052         client_sk = g_io_channel_unix_get_fd(client->channel);
3053
3054         if (read_len == 0) {
3055                 debug("client %d closed, pending %d bytes",
3056                         client_sk, client->buf_end);
3057                 g_hash_table_remove(partial_tcp_req_table,
3058                                         GINT_TO_POINTER(client_sk));
3059                 return false;
3060         }
3061
3062         debug("client %d received %d bytes", client_sk, read_len);
3063
3064         client->buf_end += read_len;
3065
3066         if (client->buf_end < 2)
3067                 return true;
3068
3069         msg_len = get_msg_len(client->buf);
3070         if (msg_len > TCP_MAX_BUF_LEN) {
3071                 debug("client %d sent too much data %d", client_sk, msg_len);
3072                 g_hash_table_remove(partial_tcp_req_table,
3073                                         GINT_TO_POINTER(client_sk));
3074                 return false;
3075         }
3076
3077 read_another:
3078         debug("client %d msg len %d end %d past end %d", client_sk, msg_len,
3079                 client->buf_end, client->buf_end - (msg_len + 2));
3080
3081         if (client->buf_end < (msg_len + 2)) {
3082                 debug("client %d still missing %d bytes",
3083                         client_sk,
3084                         msg_len + 2 - client->buf_end);
3085                 return true;
3086         }
3087
3088         debug("client %d all data %d received", client_sk, msg_len);
3089
3090         err = parse_request(client->buf + 2, msg_len,
3091                         query, sizeof(query));
3092         if (err < 0 || (g_slist_length(server_list) == 0)) {
3093                 send_response(client_sk, client->buf, msg_len + 2,
3094                         NULL, 0, IPPROTO_TCP);
3095                 return true;
3096         }
3097
3098         req = g_try_new0(struct request_data, 1);
3099         if (!req)
3100                 return true;
3101
3102         memcpy(&req->sa, client_addr, client_addr_len);
3103         req->sa_len = client_addr_len;
3104         req->client_sk = client_sk;
3105         req->protocol = IPPROTO_TCP;
3106         req->family = client->family;
3107
3108         req->srcid = client->buf[2] | (client->buf[3] << 8);
3109         req->dstid = get_id();
3110         req->altid = get_id();
3111         req->request_len = msg_len + 2;
3112
3113         client->buf[2] = req->dstid & 0xff;
3114         client->buf[3] = req->dstid >> 8;
3115
3116         req->numserv = 0;
3117         req->ifdata = client->ifdata;
3118         req->append_domain = false;
3119
3120         /*
3121          * Check if the answer is found in the cache before
3122          * creating sockets to the server.
3123          */
3124         entry = cache_check(client->buf, &qtype, IPPROTO_TCP);
3125         if (entry) {
3126                 int ttl_left = 0;
3127                 struct cache_data *data;
3128
3129                 debug("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
3130                 if (qtype == 1)
3131                         data = entry->ipv4;
3132                 else
3133                         data = entry->ipv6;
3134
3135                 if (data) {
3136                         ttl_left = data->valid_until - time(NULL);
3137                         entry->hits++;
3138
3139                         send_cached_response(client_sk, data->data,
3140                                         data->data_len, NULL, 0, IPPROTO_TCP,
3141                                         req->srcid, data->answers, ttl_left);
3142
3143                         g_free(req);
3144                         goto out;
3145                 } else
3146                         debug("data missing, ignoring cache for this query");
3147         }
3148
3149         for (list = server_list; list; list = list->next) {
3150                 struct server_data *data = list->data;
3151
3152                 if (data->protocol != IPPROTO_UDP || !data->enabled)
3153                         continue;
3154
3155                 if (!create_server(data->index, NULL, data->server,
3156                                         IPPROTO_TCP))
3157                         continue;
3158
3159                 waiting_for_connect = true;
3160         }
3161
3162         if (!waiting_for_connect) {
3163                 /* No server is waiting for connect */
3164                 send_response(client_sk, client->buf,
3165                         req->request_len, NULL, 0, IPPROTO_TCP);
3166                 g_free(req);
3167                 return true;
3168         }
3169
3170         /*
3171          * The server is not connected yet.
3172          * Copy the relevant buffers.
3173          * The request will actually be sent once we're
3174          * properly connected over TCP to the nameserver.
3175          */
3176         req->request = g_try_malloc0(req->request_len);
3177         if (!req->request) {
3178                 send_response(client_sk, client->buf,
3179                         req->request_len, NULL, 0, IPPROTO_TCP);
3180                 g_free(req);
3181                 goto out;
3182         }
3183         memcpy(req->request, client->buf, req->request_len);
3184
3185         req->name = g_try_malloc0(sizeof(query));
3186         if (!req->name) {
3187                 send_response(client_sk, client->buf,
3188                         req->request_len, NULL, 0, IPPROTO_TCP);
3189                 g_free(req->request);
3190                 g_free(req);
3191                 goto out;
3192         }
3193         memcpy(req->name, query, sizeof(query));
3194
3195         req->timeout = g_timeout_add_seconds(30, request_timeout, req);
3196
3197         request_list = g_slist_append(request_list, req);
3198
3199 out:
3200         if (client->buf_end > (msg_len + 2)) {
3201                 debug("client %d buf %p -> %p end %d len %d new %d",
3202                         client_sk,
3203                         client->buf + msg_len + 2,
3204                         client->buf, client->buf_end,
3205                         TCP_MAX_BUF_LEN - client->buf_end,
3206                         client->buf_end - (msg_len + 2));
3207                 memmove(client->buf, client->buf + msg_len + 2,
3208                         TCP_MAX_BUF_LEN - client->buf_end);
3209                 client->buf_end = client->buf_end - (msg_len + 2);
3210
3211                 /*
3212                  * If we have a full message waiting, just read it
3213                  * immediately.
3214                  */
3215                 msg_len = get_msg_len(client->buf);
3216                 if ((msg_len + 2) == client->buf_end) {
3217                         debug("client %d reading another %d bytes", client_sk,
3218                                                                 msg_len + 2);
3219                         goto read_another;
3220                 }
3221         } else {
3222                 debug("client %d clearing reading buffer", client_sk);
3223
3224                 client->buf_end = 0;
3225                 memset(client->buf, 0, TCP_MAX_BUF_LEN);
3226
3227                 /*
3228                  * We received all the packets from client so we must also
3229                  * remove the timeout handler here otherwise we might get
3230                  * timeout while waiting the results from server.
3231                  */
3232                 g_source_remove(client->timeout);
3233                 client->timeout = 0;
3234         }
3235
3236         return true;
3237 }
3238
3239 static gboolean tcp_client_event(GIOChannel *channel, GIOCondition condition,
3240                                 gpointer user_data)
3241 {
3242         struct tcp_partial_client_data *client = user_data;
3243         struct sockaddr_in6 client_addr6;
3244         socklen_t client_addr6_len = sizeof(client_addr6);
3245         struct sockaddr_in client_addr4;
3246         socklen_t client_addr4_len = sizeof(client_addr4);
3247         void *client_addr;
3248         socklen_t *client_addr_len;
3249         int len, client_sk;
3250
3251         client_sk = g_io_channel_unix_get_fd(channel);
3252
3253         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3254                 g_hash_table_remove(partial_tcp_req_table,
3255                                         GINT_TO_POINTER(client_sk));
3256
3257                 connman_error("Error with TCP client %d channel", client_sk);
3258                 return FALSE;
3259         }
3260
3261         switch (client->family) {
3262         case AF_INET:
3263                 client_addr = &client_addr4;
3264                 client_addr_len = &client_addr4_len;
3265                 break;
3266         case AF_INET6:
3267                 client_addr = &client_addr6;
3268                 client_addr_len = &client_addr6_len;
3269                 break;
3270         default:
3271                 g_hash_table_remove(partial_tcp_req_table,
3272                                         GINT_TO_POINTER(client_sk));
3273                 connman_error("client %p corrupted", client);
3274                 return FALSE;
3275         }
3276
3277         len = recvfrom(client_sk, client->buf + client->buf_end,
3278                         TCP_MAX_BUF_LEN - client->buf_end, 0,
3279                         client_addr, client_addr_len);
3280         if (len < 0) {
3281                 if (errno == EAGAIN || errno == EWOULDBLOCK)
3282                         return TRUE;
3283
3284                 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3285                         strerror(errno));
3286                 g_hash_table_remove(partial_tcp_req_table,
3287                                         GINT_TO_POINTER(client_sk));
3288                 return FALSE;
3289         }
3290
3291         return read_tcp_data(client, client_addr, *client_addr_len, len);
3292 }
3293
3294 static gboolean client_timeout(gpointer user_data)
3295 {
3296         struct tcp_partial_client_data *client = user_data;
3297         int sock;
3298
3299         sock = g_io_channel_unix_get_fd(client->channel);
3300
3301         debug("client %d timeout pending %d bytes", sock, client->buf_end);
3302
3303         g_hash_table_remove(partial_tcp_req_table, GINT_TO_POINTER(sock));
3304
3305         return FALSE;
3306 }
3307
3308 static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
3309                                 struct listener_data *ifdata, int family,
3310                                 guint *listener_watch)
3311 {
3312         int sk, client_sk, len;
3313         unsigned int msg_len;
3314         struct tcp_partial_client_data *client;
3315         struct sockaddr_in6 client_addr6;
3316         socklen_t client_addr6_len = sizeof(client_addr6);
3317         struct sockaddr_in client_addr4;
3318         socklen_t client_addr4_len = sizeof(client_addr4);
3319         void *client_addr;
3320         socklen_t *client_addr_len;
3321         struct timeval tv;
3322         fd_set readfds;
3323
3324         debug("condition 0x%02x channel %p ifdata %p family %d",
3325                 condition, channel, ifdata, family);
3326
3327         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3328                 if (*listener_watch > 0)
3329                         g_source_remove(*listener_watch);
3330                 *listener_watch = 0;
3331
3332                 connman_error("Error with TCP listener channel");
3333
3334                 return false;
3335         }
3336
3337         sk = g_io_channel_unix_get_fd(channel);
3338
3339         if (family == AF_INET) {
3340                 client_addr = &client_addr4;
3341                 client_addr_len = &client_addr4_len;
3342         } else {
3343                 client_addr = &client_addr6;
3344                 client_addr_len = &client_addr6_len;
3345         }
3346
3347         tv.tv_sec = tv.tv_usec = 0;
3348         FD_ZERO(&readfds);
3349         FD_SET(sk, &readfds);
3350
3351         select(sk + 1, &readfds, NULL, NULL, &tv);
3352         if (FD_ISSET(sk, &readfds)) {
3353                 client_sk = accept(sk, client_addr, client_addr_len);
3354                 debug("client %d accepted", client_sk);
3355         } else {
3356                 debug("No data to read from master %d, waiting.", sk);
3357                 return true;
3358         }
3359
3360         if (client_sk < 0) {
3361                 connman_error("Accept failure on TCP listener");
3362                 *listener_watch = 0;
3363                 return false;
3364         }
3365
3366         fcntl(client_sk, F_SETFL, O_NONBLOCK);
3367
3368         client = g_hash_table_lookup(partial_tcp_req_table,
3369                                         GINT_TO_POINTER(client_sk));
3370         if (!client) {
3371                 client = g_try_new0(struct tcp_partial_client_data, 1);
3372                 if (!client) {
3373                         close(client_sk);
3374                         return false;
3375                 }
3376
3377                 g_hash_table_insert(partial_tcp_req_table,
3378                                         GINT_TO_POINTER(client_sk),
3379                                         client);
3380
3381                 client->channel = g_io_channel_unix_new(client_sk);
3382                 g_io_channel_set_close_on_unref(client->channel, TRUE);
3383
3384                 client->watch = g_io_add_watch(client->channel,
3385                                                 G_IO_IN, tcp_client_event,
3386                                                 (gpointer)client);
3387
3388                 client->ifdata = ifdata;
3389
3390                 debug("client %d created %p", client_sk, client);
3391         } else {
3392                 debug("client %d already exists %p", client_sk, client);
3393         }
3394
3395         if (!client->buf) {
3396                 client->buf = g_try_malloc(TCP_MAX_BUF_LEN);
3397                 if (!client->buf)
3398                         return false;
3399         }
3400         memset(client->buf, 0, TCP_MAX_BUF_LEN);
3401         client->buf_end = 0;
3402         client->family = family;
3403
3404         if (client->timeout == 0)
3405                 client->timeout = g_timeout_add_seconds(2, client_timeout,
3406                                                         client);
3407
3408         /*
3409          * Check how much data there is. If all is there, then we can
3410          * proceed normally, otherwise read the bits until everything
3411          * is received or timeout occurs.
3412          */
3413         len = recv(client_sk, client->buf, TCP_MAX_BUF_LEN, 0);
3414         if (len < 0) {
3415                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
3416                         debug("client %d no data to read, waiting", client_sk);
3417                         return true;
3418                 }
3419
3420                 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3421                         strerror(errno));
3422                 g_hash_table_remove(partial_tcp_req_table,
3423                                         GINT_TO_POINTER(client_sk));
3424                 return true;
3425         }
3426
3427         if (len < 2) {
3428                 debug("client %d not enough data to read, waiting", client_sk);
3429                 client->buf_end += len;
3430                 return true;
3431         }
3432
3433         msg_len = get_msg_len(client->buf);
3434         if (msg_len > TCP_MAX_BUF_LEN) {
3435                 debug("client %d invalid message length %u ignoring packet",
3436                         client_sk, msg_len);
3437                 g_hash_table_remove(partial_tcp_req_table,
3438                                         GINT_TO_POINTER(client_sk));
3439                 return true;
3440         }
3441
3442         /*
3443          * The packet length bytes do not contain the total message length,
3444          * that is the reason to -2 below.
3445          */
3446         if (msg_len != (unsigned int)(len - 2)) {
3447                 debug("client %d sent %d bytes but expecting %u pending %d",
3448                         client_sk, len, msg_len + 2, msg_len + 2 - len);
3449
3450                 client->buf_end += len;
3451                 return true;
3452         }
3453
3454         return read_tcp_data(client, client_addr, *client_addr_len, len);
3455 }
3456
3457 static gboolean tcp4_listener_event(GIOChannel *channel, GIOCondition condition,
3458                                 gpointer user_data)
3459 {
3460         struct listener_data *ifdata = user_data;
3461
3462         return tcp_listener_event(channel, condition, ifdata, AF_INET,
3463                                 &ifdata->tcp4_listener_watch);
3464 }
3465
3466 static gboolean tcp6_listener_event(GIOChannel *channel, GIOCondition condition,
3467                                 gpointer user_data)
3468 {
3469         struct listener_data *ifdata = user_data;
3470
3471         return tcp_listener_event(channel, condition, user_data, AF_INET6,
3472                                 &ifdata->tcp6_listener_watch);
3473 }
3474
3475 static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
3476                                 struct listener_data *ifdata, int family,
3477                                 guint *listener_watch)
3478 {
3479         unsigned char buf[768];
3480         char query[512];
3481         struct request_data *req;
3482         struct sockaddr_in6 client_addr6;
3483         socklen_t client_addr6_len = sizeof(client_addr6);
3484         struct sockaddr_in client_addr4;
3485         socklen_t client_addr4_len = sizeof(client_addr4);
3486         void *client_addr;
3487         socklen_t *client_addr_len;
3488         int sk, err, len;
3489
3490         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3491                 connman_error("Error with UDP listener channel");
3492                 *listener_watch = 0;
3493                 return false;
3494         }
3495
3496         sk = g_io_channel_unix_get_fd(channel);
3497
3498         if (family == AF_INET) {
3499                 client_addr = &client_addr4;
3500                 client_addr_len = &client_addr4_len;
3501         } else {
3502                 client_addr = &client_addr6;
3503                 client_addr_len = &client_addr6_len;
3504         }
3505
3506         memset(client_addr, 0, *client_addr_len);
3507         len = recvfrom(sk, buf, sizeof(buf), 0, client_addr, client_addr_len);
3508         if (len < 2)
3509                 return true;
3510
3511         debug("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
3512
3513         err = parse_request(buf, len, query, sizeof(query));
3514         if (err < 0 || (g_slist_length(server_list) == 0)) {
3515                 send_response(sk, buf, len, client_addr,
3516                                 *client_addr_len, IPPROTO_UDP);
3517                 return true;
3518         }
3519
3520         req = g_try_new0(struct request_data, 1);
3521         if (!req)
3522                 return true;
3523
3524         memcpy(&req->sa, client_addr, *client_addr_len);
3525         req->sa_len = *client_addr_len;
3526         req->client_sk = 0;
3527         req->protocol = IPPROTO_UDP;
3528         req->family = family;
3529
3530         req->srcid = buf[0] | (buf[1] << 8);
3531         req->dstid = get_id();
3532         req->altid = get_id();
3533         req->request_len = len;
3534
3535         buf[0] = req->dstid & 0xff;
3536         buf[1] = req->dstid >> 8;
3537
3538         req->numserv = 0;
3539         req->ifdata = ifdata;
3540         req->append_domain = false;
3541
3542         if (resolv(req, buf, query)) {
3543                 /* a cached result was sent, so the request can be released */
3544                 g_free(req);
3545                 return true;
3546         }
3547
3548         req->name = g_strdup(query);
3549         req->request = g_malloc(len);
3550         memcpy(req->request, buf, len);
3551         req->timeout = g_timeout_add_seconds(5, request_timeout, req);
3552         request_list = g_slist_append(request_list, req);
3553
3554         return true;
3555 }
3556
3557 static gboolean udp4_listener_event(GIOChannel *channel, GIOCondition condition,
3558                                 gpointer user_data)
3559 {
3560         struct listener_data *ifdata = user_data;
3561
3562         return udp_listener_event(channel, condition, ifdata, AF_INET,
3563                                 &ifdata->udp4_listener_watch);
3564 }
3565
3566 static gboolean udp6_listener_event(GIOChannel *channel, GIOCondition condition,
3567                                 gpointer user_data)
3568 {
3569         struct listener_data *ifdata = user_data;
3570
3571         return udp_listener_event(channel, condition, user_data, AF_INET6,
3572                                 &ifdata->udp6_listener_watch);
3573 }
3574
3575 static GIOChannel *get_listener(int family, int protocol, int index)
3576 {
3577         GIOChannel *channel;
3578         const char *proto;
3579         union {
3580                 struct sockaddr sa;
3581                 struct sockaddr_in6 sin6;
3582                 struct sockaddr_in sin;
3583         } s;
3584         socklen_t slen;
3585         int sk, type;
3586         char *interface;
3587
3588         debug("family %d protocol %d index %d", family, protocol, index);
3589
3590         switch (protocol) {
3591         case IPPROTO_UDP:
3592                 proto = "UDP";
3593                 type = SOCK_DGRAM | SOCK_CLOEXEC;
3594                 break;
3595
3596         case IPPROTO_TCP:
3597                 proto = "TCP";
3598                 type = SOCK_STREAM | SOCK_CLOEXEC;
3599                 break;
3600
3601         default:
3602                 return NULL;
3603         }
3604
3605         sk = socket(family, type, protocol);
3606         if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
3607                 connman_error("No IPv6 support");
3608                 return NULL;
3609         }
3610
3611         if (sk < 0) {
3612                 connman_error("Failed to create %s listener socket", proto);
3613                 return NULL;
3614         }
3615
3616         interface = connman_inet_ifname(index);
3617         if (!interface || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
3618                                         interface,
3619                                         strlen(interface) + 1) < 0) {
3620                 connman_error("Failed to bind %s listener interface "
3621                         "for %s (%d/%s)",
3622                         proto, family == AF_INET ? "IPv4" : "IPv6",
3623                         -errno, strerror(errno));
3624                 close(sk);
3625                 g_free(interface);
3626                 return NULL;
3627         }
3628         g_free(interface);
3629
3630         if (family == AF_INET6) {
3631                 memset(&s.sin6, 0, sizeof(s.sin6));
3632                 s.sin6.sin6_family = AF_INET6;
3633                 s.sin6.sin6_port = htons(53);
3634                 slen = sizeof(s.sin6);
3635
3636                 if (__connman_inet_get_interface_address(index,
3637                                                 AF_INET6,
3638                                                 &s.sin6.sin6_addr) < 0) {
3639                         /* So we could not find suitable IPv6 address for
3640                          * the interface. This could happen if we have
3641                          * disabled IPv6 for the interface.
3642                          */
3643                         close(sk);
3644                         return NULL;
3645                 }
3646
3647         } else if (family == AF_INET) {
3648                 memset(&s.sin, 0, sizeof(s.sin));
3649                 s.sin.sin_family = AF_INET;
3650                 s.sin.sin_port = htons(53);
3651                 slen = sizeof(s.sin);
3652
3653                 if (__connman_inet_get_interface_address(index,
3654                                                 AF_INET,
3655                                                 &s.sin.sin_addr) < 0) {
3656                         close(sk);
3657                         return NULL;
3658                 }
3659         } else {
3660                 close(sk);
3661                 return NULL;
3662         }
3663
3664         if (bind(sk, &s.sa, slen) < 0) {
3665                 connman_error("Failed to bind %s listener socket", proto);
3666                 close(sk);
3667                 return NULL;
3668         }
3669
3670         if (protocol == IPPROTO_TCP) {
3671
3672                 if (listen(sk, 10) < 0) {
3673                         connman_error("Failed to listen on TCP socket %d/%s",
3674                                 -errno, strerror(errno));
3675                         close(sk);
3676                         return NULL;
3677                 }
3678
3679                 fcntl(sk, F_SETFL, O_NONBLOCK);
3680         }
3681
3682         channel = g_io_channel_unix_new(sk);
3683         if (!channel) {
3684                 connman_error("Failed to create %s listener channel", proto);
3685                 close(sk);
3686                 return NULL;
3687         }
3688
3689         g_io_channel_set_close_on_unref(channel, TRUE);
3690
3691         return channel;
3692 }
3693
3694 #define UDP_IPv4_FAILED 0x01
3695 #define TCP_IPv4_FAILED 0x02
3696 #define UDP_IPv6_FAILED 0x04
3697 #define TCP_IPv6_FAILED 0x08
3698 #define UDP_FAILED (UDP_IPv4_FAILED | UDP_IPv6_FAILED)
3699 #define TCP_FAILED (TCP_IPv4_FAILED | TCP_IPv6_FAILED)
3700 #define IPv6_FAILED (UDP_IPv6_FAILED | TCP_IPv6_FAILED)
3701 #define IPv4_FAILED (UDP_IPv4_FAILED | TCP_IPv4_FAILED)
3702
3703 static int create_dns_listener(int protocol, struct listener_data *ifdata)
3704 {
3705         int ret = 0;
3706
3707         if (protocol == IPPROTO_TCP) {
3708                 ifdata->tcp4_listener_channel = get_listener(AF_INET, protocol,
3709                                                         ifdata->index);
3710                 if (ifdata->tcp4_listener_channel)
3711                         ifdata->tcp4_listener_watch =
3712                                 g_io_add_watch(ifdata->tcp4_listener_channel,
3713                                         G_IO_IN, tcp4_listener_event,
3714                                         (gpointer)ifdata);
3715                 else
3716                         ret |= TCP_IPv4_FAILED;
3717
3718                 ifdata->tcp6_listener_channel = get_listener(AF_INET6, protocol,
3719                                                         ifdata->index);
3720                 if (ifdata->tcp6_listener_channel)
3721                         ifdata->tcp6_listener_watch =
3722                                 g_io_add_watch(ifdata->tcp6_listener_channel,
3723                                         G_IO_IN, tcp6_listener_event,
3724                                         (gpointer)ifdata);
3725                 else
3726                         ret |= TCP_IPv6_FAILED;
3727         } else {
3728                 ifdata->udp4_listener_channel = get_listener(AF_INET, protocol,
3729                                                         ifdata->index);
3730                 if (ifdata->udp4_listener_channel)
3731                         ifdata->udp4_listener_watch =
3732                                 g_io_add_watch(ifdata->udp4_listener_channel,
3733                                         G_IO_IN, udp4_listener_event,
3734                                         (gpointer)ifdata);
3735                 else
3736                         ret |= UDP_IPv4_FAILED;
3737
3738                 ifdata->udp6_listener_channel = get_listener(AF_INET6, protocol,
3739                                                         ifdata->index);
3740                 if (ifdata->udp6_listener_channel)
3741                         ifdata->udp6_listener_watch =
3742                                 g_io_add_watch(ifdata->udp6_listener_channel,
3743                                         G_IO_IN, udp6_listener_event,
3744                                         (gpointer)ifdata);
3745                 else
3746                         ret |= UDP_IPv6_FAILED;
3747         }
3748
3749         return ret;
3750 }
3751
3752 static void destroy_udp_listener(struct listener_data *ifdata)
3753 {
3754         DBG("index %d", ifdata->index);
3755
3756         if (ifdata->udp4_listener_watch > 0)
3757                 g_source_remove(ifdata->udp4_listener_watch);
3758
3759         if (ifdata->udp6_listener_watch > 0)
3760                 g_source_remove(ifdata->udp6_listener_watch);
3761
3762         if (ifdata->udp4_listener_channel)
3763                 g_io_channel_unref(ifdata->udp4_listener_channel);
3764         if (ifdata->udp6_listener_channel)
3765                 g_io_channel_unref(ifdata->udp6_listener_channel);
3766 }
3767
3768 static void destroy_tcp_listener(struct listener_data *ifdata)
3769 {
3770         DBG("index %d", ifdata->index);
3771
3772         if (ifdata->tcp4_listener_watch > 0)
3773                 g_source_remove(ifdata->tcp4_listener_watch);
3774         if (ifdata->tcp6_listener_watch > 0)
3775                 g_source_remove(ifdata->tcp6_listener_watch);
3776
3777         if (ifdata->tcp4_listener_channel)
3778                 g_io_channel_unref(ifdata->tcp4_listener_channel);
3779         if (ifdata->tcp6_listener_channel)
3780                 g_io_channel_unref(ifdata->tcp6_listener_channel);
3781 }
3782
3783 static int create_listener(struct listener_data *ifdata)
3784 {
3785         int err, index;
3786
3787         err = create_dns_listener(IPPROTO_UDP, ifdata);
3788         if ((err & UDP_FAILED) == UDP_FAILED)
3789                 return -EIO;
3790
3791         err |= create_dns_listener(IPPROTO_TCP, ifdata);
3792         if ((err & TCP_FAILED) == TCP_FAILED) {
3793                 destroy_udp_listener(ifdata);
3794                 return -EIO;
3795         }
3796
3797         index = connman_inet_ifindex("lo");
3798         if (ifdata->index == index) {
3799                 if ((err & IPv6_FAILED) != IPv6_FAILED)
3800                         __connman_resolvfile_append(index, NULL, "::1");
3801
3802                 if ((err & IPv4_FAILED) != IPv4_FAILED)
3803                         __connman_resolvfile_append(index, NULL, "127.0.0.1");
3804         }
3805
3806         return 0;
3807 }
3808
3809 static void destroy_listener(struct listener_data *ifdata)
3810 {
3811         int index;
3812         GSList *list;
3813
3814         index = connman_inet_ifindex("lo");
3815         if (ifdata->index == index) {
3816                 __connman_resolvfile_remove(index, NULL, "127.0.0.1");
3817                 __connman_resolvfile_remove(index, NULL, "::1");
3818         }
3819
3820         for (list = request_list; list; list = list->next) {
3821                 struct request_data *req = list->data;
3822
3823                 debug("Dropping request (id 0x%04x -> 0x%04x)",
3824                                                 req->srcid, req->dstid);
3825                 destroy_request_data(req);
3826                 list->data = NULL;
3827         }
3828
3829         g_slist_free(request_list);
3830         request_list = NULL;
3831
3832         destroy_tcp_listener(ifdata);
3833         destroy_udp_listener(ifdata);
3834 }
3835
3836 int __connman_dnsproxy_add_listener(int index)
3837 {
3838         struct listener_data *ifdata;
3839         int err;
3840
3841         DBG("index %d", index);
3842
3843         if (index < 0)
3844                 return -EINVAL;
3845
3846         if (!listener_table)
3847                 return -ENOENT;
3848
3849         if (g_hash_table_lookup(listener_table, GINT_TO_POINTER(index)))
3850                 return 0;
3851
3852         ifdata = g_try_new0(struct listener_data, 1);
3853         if (!ifdata)
3854                 return -ENOMEM;
3855
3856         ifdata->index = index;
3857         ifdata->udp4_listener_channel = NULL;
3858         ifdata->udp4_listener_watch = 0;
3859         ifdata->tcp4_listener_channel = NULL;
3860         ifdata->tcp4_listener_watch = 0;
3861         ifdata->udp6_listener_channel = NULL;
3862         ifdata->udp6_listener_watch = 0;
3863         ifdata->tcp6_listener_channel = NULL;
3864         ifdata->tcp6_listener_watch = 0;
3865
3866         err = create_listener(ifdata);
3867         if (err < 0) {
3868                 connman_error("Couldn't create listener for index %d err %d",
3869                                 index, err);
3870                 g_free(ifdata);
3871                 return err;
3872         }
3873         g_hash_table_insert(listener_table, GINT_TO_POINTER(ifdata->index),
3874                         ifdata);
3875         return 0;
3876 }
3877
3878 void __connman_dnsproxy_remove_listener(int index)
3879 {
3880         struct listener_data *ifdata;
3881
3882         DBG("index %d", index);
3883
3884         if (!listener_table)
3885                 return;
3886
3887         ifdata = g_hash_table_lookup(listener_table, GINT_TO_POINTER(index));
3888         if (!ifdata)
3889                 return;
3890
3891         destroy_listener(ifdata);
3892
3893         g_hash_table_remove(listener_table, GINT_TO_POINTER(index));
3894 }
3895
3896 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
3897 {
3898         int index = GPOINTER_TO_INT(key);
3899         struct listener_data *ifdata = value;
3900
3901         DBG("index %d", index);
3902
3903         destroy_listener(ifdata);
3904 }
3905
3906 static void free_partial_reqs(gpointer value)
3907 {
3908         struct tcp_partial_client_data *data = value;
3909
3910         client_reset(data);
3911         g_free(data);
3912 }
3913
3914 int __connman_dnsproxy_init(void)
3915 {
3916         int err, index;
3917
3918         DBG("");
3919
3920         listener_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
3921                                                         NULL, g_free);
3922
3923         partial_tcp_req_table = g_hash_table_new_full(g_direct_hash,
3924                                                         g_direct_equal,
3925                                                         NULL,
3926                                                         free_partial_reqs);
3927
3928         index = connman_inet_ifindex("lo");
3929         err = __connman_dnsproxy_add_listener(index);
3930         if (err < 0)
3931                 return err;
3932
3933         err = connman_notifier_register(&dnsproxy_notifier);
3934         if (err < 0)
3935                 goto destroy;
3936
3937         return 0;
3938
3939 destroy:
3940         __connman_dnsproxy_remove_listener(index);
3941         g_hash_table_destroy(listener_table);
3942         g_hash_table_destroy(partial_tcp_req_table);
3943
3944         return err;
3945 }
3946
3947 int __connman_dnsproxy_set_mdns(int index, bool enabled)
3948 {
3949         return -ENOTSUP;
3950 }
3951
3952 void __connman_dnsproxy_cleanup(void)
3953 {
3954         DBG("");
3955
3956         if (cache_timer) {
3957                 g_source_remove(cache_timer);
3958                 cache_timer = 0;
3959         }
3960
3961         if (cache) {
3962                 g_hash_table_destroy(cache);
3963                 cache = NULL;
3964         }
3965
3966         connman_notifier_unregister(&dnsproxy_notifier);
3967
3968         g_hash_table_foreach(listener_table, remove_listener, NULL);
3969
3970         g_hash_table_destroy(listener_table);
3971
3972         g_hash_table_destroy(partial_tcp_req_table);
3973
3974         if (ipv4_resolve)
3975                 g_resolv_unref(ipv4_resolve);
3976         if (ipv6_resolve)
3977                 g_resolv_unref(ipv6_resolve);
3978 }