5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
41 #if __BYTE_ORDER == __LITTLE_ENDIAN
56 } __attribute__ ((packed));
57 #elif __BYTE_ORDER == __BIG_ENDIAN
72 } __attribute__ ((packed));
74 #error "Unknown byte order"
77 struct partial_reply {
93 struct partial_reply *incoming_reply;
98 struct sockaddr_in6 __sin6; /* Only for the length */
116 struct listener_data *ifdata;
117 gboolean append_domain;
120 struct listener_data {
122 GIOChannel *udp_listener_channel;
123 guint udp_listener_watch;
124 GIOChannel *tcp_listener_channel;
125 guint tcp_listener_watch;
133 unsigned int data_len;
134 unsigned char *data; /* contains DNS header + body */
139 struct cache_data *ipv4;
140 struct cache_data *ipv6;
143 struct domain_question {
146 } __attribute__ ((packed));
153 } __attribute__ ((packed));
156 * We limit how long the cached DNS entry stays in the cache.
157 * By default the TTL (time-to-live) of the DNS response is used
158 * when setting the cache entry life time. The value is in seconds.
160 #define MAX_CACHE_TTL (60 * 30)
163 * We limit the cache size to some sane value so that cached data does
164 * not occupy too much memory. Each cached entry occupies on average
165 * about 100 bytes memory (depending on DNS name length).
166 * Example: caching www.connman.net uses 97 bytes memory.
167 * The value is the max amount of cached DNS responses (count).
169 #define MAX_CACHE_SIZE 256
171 static int cache_size;
172 static GHashTable *cache;
173 static int cache_refcount;
174 static GSList *server_list = NULL;
175 static GSList *request_list = NULL;
176 static GSList *request_pending_list = NULL;
177 static guint16 request_id = 0x0000;
178 static GHashTable *listener_table = NULL;
180 static int protocol_offset(int protocol)
195 static struct request_data *find_request(guint16 id)
199 for (list = request_list; list; list = list->next) {
200 struct request_data *req = list->data;
202 if (req->dstid == id || req->altid == id)
209 static struct server_data *find_server(const char *interface,
215 DBG("interface %s server %s", interface, server);
217 for (list = server_list; list; list = list->next) {
218 struct server_data *data = list->data;
220 if (interface == NULL && data->interface == NULL &&
221 g_str_equal(data->server, server) == TRUE &&
222 data->protocol == protocol)
225 if (interface == NULL ||
226 data->interface == NULL || data->server == NULL)
229 if (g_str_equal(data->interface, interface) == TRUE &&
230 g_str_equal(data->server, server) == TRUE &&
231 data->protocol == protocol)
238 static void send_cached_response(int sk, unsigned char *buf, int len,
239 const struct sockaddr *to, socklen_t tolen,
240 int protocol, int id, uint16_t answers)
242 struct domain_hdr *hdr;
243 int err, offset = protocol_offset(protocol);
251 hdr = (void *) (buf + offset);
256 hdr->ancount = htons(answers);
260 DBG("id 0x%04x answers %d", hdr->id, answers);
262 err = sendto(sk, buf, len, 0, to, tolen);
264 connman_error("Cannot send cached DNS response: %s",
270 static void send_response(int sk, unsigned char *buf, int len,
271 const struct sockaddr *to, socklen_t tolen,
274 struct domain_hdr *hdr;
275 int err, offset = protocol_offset(protocol);
285 hdr = (void *) (buf + offset);
287 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
296 err = sendto(sk, buf, len, 0, to, tolen);
298 connman_error("Failed to send DNS response: %s",
304 static gboolean request_timeout(gpointer user_data)
306 struct request_data *req = user_data;
307 struct listener_data *ifdata;
309 DBG("id 0x%04x", req->srcid);
314 ifdata = req->ifdata;
316 request_list = g_slist_remove(request_list, req);
319 if (req->resplen > 0 && req->resp != NULL) {
322 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
324 err = sendto(sk, req->resp, req->resplen, 0,
325 &req->sa, req->sa_len);
328 } else if (req->request && req->numserv == 0) {
329 struct domain_hdr *hdr;
331 if (req->protocol == IPPROTO_TCP) {
332 hdr = (void *) (req->request + 2);
333 hdr->id = req->srcid;
334 send_response(req->client_sk, req->request,
335 req->request_len, NULL, 0, IPPROTO_TCP);
337 } else if (req->protocol == IPPROTO_UDP) {
340 hdr = (void *) (req->request);
341 hdr->id = req->srcid;
342 sk = g_io_channel_unix_get_fd(
343 ifdata->udp_listener_channel);
344 send_response(sk, req->request, req->request_len,
345 &req->sa, req->sa_len, IPPROTO_UDP);
355 static int append_query(unsigned char *buf, unsigned int size,
356 const char *query, const char *domain)
358 unsigned char *ptr = buf;
362 DBG("query %s domain %s", query, domain);
364 offset = (char *) query;
365 while (offset != NULL) {
368 tmp = strchr(offset, '.');
370 len = strlen(offset);
374 memcpy(ptr + 1, offset, len);
380 memcpy(ptr + 1, offset, tmp - offset);
381 ptr += tmp - offset + 1;
386 offset = (char *) domain;
387 while (offset != NULL) {
390 tmp = strchr(offset, '.');
392 len = strlen(offset);
396 memcpy(ptr + 1, offset, len);
402 memcpy(ptr + 1, offset, tmp - offset);
403 ptr += tmp - offset + 1;
413 static gboolean cache_check_is_valid(struct cache_data *data,
419 if (data->inserted + data->timeout < current_time)
425 static uint16_t cache_check_validity(char *question, uint16_t type,
426 struct cache_data *ipv4,
427 struct cache_data *ipv6)
429 time_t current_time = time(0);
433 if (cache_check_is_valid(ipv4, current_time) == FALSE) {
434 DBG("cache %s \"%s\" type A",
435 ipv4 ? "timeout" : "entry missing", question);
438 * We do not remove cache entry if there is still
439 * valid IPv6 entry found in the cache.
441 if (cache_check_is_valid(ipv6, current_time) == FALSE)
442 g_hash_table_remove(cache, question);
449 if (cache_check_is_valid(ipv6, current_time) == FALSE) {
450 DBG("cache %s \"%s\" type AAAA",
451 ipv6 ? "timeout" : "entry missing", question);
453 if (cache_check_is_valid(ipv4, current_time) == FALSE)
454 g_hash_table_remove(cache, question);
464 static struct cache_entry *cache_check(gpointer request, int *qtype)
466 char *question = request + 12;
467 struct cache_entry *entry;
468 struct domain_question *q;
472 offset = strlen(question) + 1;
473 q = (void *) (question + offset);
474 type = ntohs(q->type);
476 /* We only cache either A (1) or AAAA (28) requests */
477 if (type != 1 && type != 28)
480 entry = g_hash_table_lookup(cache, question);
484 type = cache_check_validity(question, type, entry->ipv4, entry->ipv6);
493 * Get a label/name from DNS resource record. The function decompresses the
494 * label if necessary. The function does not convert the name to presentation
495 * form. This means that the result string will contain label lengths instead
496 * of dots between labels. We intentionally do not want to convert to dotted
497 * format so that we can cache the wire format string directly.
499 static int get_name(int counter,
500 unsigned char *pkt, unsigned char *start, unsigned char *max,
501 unsigned char *output, int output_max, int *output_len,
502 unsigned char **end, char *name, int *name_len)
506 /* Limit recursion to 10 (this means up to 10 labels in domain name) */
512 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
513 uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
515 if (offset >= max - pkt)
521 return get_name(counter + 1, pkt, pkt + offset, max,
522 output, output_max, output_len, end,
525 unsigned label_len = *p;
527 if (pkt + label_len > max)
530 if (*output_len > output_max)
534 * We need the original name in order to check
535 * if this answer is the correct one.
537 name[(*name_len)++] = label_len;
538 memcpy(name + *name_len, p + 1, label_len + 1);
539 *name_len += label_len;
541 /* We compress the result */
542 output[0] = NS_CMPRSFLGS;
559 static int parse_rr(unsigned char *buf, unsigned char *start,
561 unsigned char *response, unsigned int *response_size,
562 uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
566 struct domain_rr *rr;
568 int name_len = 0, output_len = 0, max_rsp = *response_size;
570 err = get_name(0, buf, start, max, response, max_rsp,
571 &output_len, end, name, &name_len);
577 if ((unsigned int) offset > *response_size)
580 rr = (void *) (*end);
585 *type = ntohs(rr->type);
586 *class = ntohs(rr->class);
587 *ttl = ntohl(rr->ttl);
588 *rdlen = ntohs(rr->rdlen);
593 memcpy(response + offset, *end, sizeof(struct domain_rr));
595 offset += sizeof(struct domain_rr);
596 *end += sizeof(struct domain_rr);
598 if ((unsigned int) (offset + *rdlen) > *response_size)
601 memcpy(response + offset, *end, *rdlen);
605 *response_size = offset + *rdlen;
610 static gboolean check_alias(GSList *aliases, char *name)
614 if (aliases != NULL) {
615 for (list = aliases; list; list = list->next) {
616 int len = strlen((char *)list->data);
617 if (strncmp((char *)list->data, name, len) == 0)
625 static int parse_response(unsigned char *buf, int buflen,
626 char *question, int qlen,
627 uint16_t *type, uint16_t *class, int *ttl,
628 unsigned char *response, unsigned int *response_len,
631 struct domain_hdr *hdr = (void *) buf;
632 struct domain_question *q;
634 uint16_t qdcount = ntohs(hdr->qdcount);
635 uint16_t ancount = ntohs(hdr->ancount);
637 uint16_t qtype, qclass;
638 unsigned char *next = NULL;
639 unsigned int maxlen = *response_len;
640 GSList *aliases = NULL, *list;
641 char name[NS_MAXDNAME + 1];
646 DBG("qr %d qdcount %d", hdr->qr, qdcount);
648 /* We currently only cache responses where question count is 1 */
649 if (hdr->qr != 1 || qdcount != 1)
652 ptr = buf + sizeof(struct domain_hdr);
654 strncpy(question, (char *) ptr, qlen);
655 qlen = strlen(question);
656 ptr += qlen + 1; /* skip \0 */
659 qtype = ntohs(q->type);
661 /* We cache only A and AAAA records */
662 if (qtype != 1 && qtype != 28)
665 qclass = ntohs(q->class);
667 ptr += 2 + 2; /* ptr points now to answers */
674 * We have a bunch of answers (like A, AAAA, CNAME etc) to
675 * A or AAAA question. We traverse the answers and parse the
676 * resource records. Only A and AAAA records are cached, all
677 * the other records in answers are skipped.
679 for (i = 0; i < ancount; i++) {
681 * Get one address at a time to this buffer.
682 * The max size of the answer is
683 * 2 (pointer) + 2 (type) + 2 (class) +
684 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
685 * for A or AAAA record.
686 * For CNAME the size can be bigger.
688 unsigned char rsp[NS_MAXCDNAME];
689 unsigned int rsp_len = sizeof(rsp) - 1;
692 memset(rsp, 0, sizeof(rsp));
694 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
695 type, class, ttl, &rdlen, &next, name);
702 * Now rsp contains compressed or uncompressed resource
703 * record. Next we check if this record answers the question.
704 * The name var contains the uncompressed label.
705 * One tricky bit is the CNAME records as they alias
706 * the name we might be interested in.
710 * Go to next answer if the class is not the one we are
713 if (*class != qclass) {
720 * Try to resolve aliases also, type is CNAME(5).
721 * This is important as otherwise the aliased names would not
722 * be cached at all as the cache would not contain the aliased
725 * If any CNAME is found in DNS packet, then we cache the alias
726 * IP address instead of the question (as the server
727 * said that question has only an alias).
728 * This means in practice that if e.g., ipv6.google.com is
729 * queried, DNS server returns CNAME of that name which is
730 * ipv6.l.google.com. We then cache the address of the CNAME
731 * but return the question name to client. So the alias
732 * status of the name is not saved in cache and thus not
733 * returned to the client. We do not return DNS packets from
734 * cache to client saying that ipv6.google.com is an alias to
735 * ipv6.l.google.com but we return instead a DNS packet that
736 * says ipv6.google.com has address xxx which is in fact the
737 * address of ipv6.l.google.com. For caching purposes this
738 * should not cause any issues.
740 if (*type == 5 && strncmp(question, name, qlen) == 0) {
742 * So now the alias answered the question. This is
743 * not very useful from caching point of view as
744 * the following A or AAAA records will not match the
745 * question. We need to find the real A/AAAA record
746 * of the alias and cache that.
748 unsigned char *end = NULL;
749 int name_len = 0, output_len;
751 memset(rsp, 0, sizeof(rsp));
752 rsp_len = sizeof(rsp) - 1;
755 * Alias is in rdata part of the message,
756 * and next-rdlen points to it. So we need to get
757 * the real name of the alias.
759 ret = get_name(0, buf, next - rdlen, buf + buflen,
760 rsp, rsp_len, &output_len, &end,
763 /* just ignore the error at this point */
770 * We should now have the alias of the entry we might
771 * want to cache. Just remember it for a while.
772 * We check the alias list when we have parsed the
775 aliases = g_slist_prepend(aliases, g_strdup(name));
782 if (*type == qtype) {
784 * We found correct type (A or AAAA)
786 if (check_alias(aliases, name) == TRUE ||
787 (aliases == NULL && strncmp(question, name,
790 * We found an alias or the name of the rr
791 * matches the question. If so, we append
792 * the compressed label to the cache.
793 * The end result is a response buffer that
794 * will contain one or more cached and
795 * compressed resource records.
797 if (*response_len + rsp_len > maxlen) {
801 memcpy(response + *response_len, rsp, rsp_len);
802 *response_len += rsp_len;
813 for (list = aliases; list; list = list->next)
815 g_slist_free(aliases);
820 struct cache_timeout {
825 static gboolean cache_check_entry(gpointer key, gpointer value,
828 struct cache_timeout *data = user_data;
829 struct cache_entry *entry = value;
833 * If either IPv4 or IPv6 cached entry has expired, we
834 * remove both from the cache.
837 if (entry->ipv4 != NULL && entry->ipv4->timeout > 0) {
838 max_timeout = entry->ipv4->inserted + entry->ipv4->timeout;
839 if (max_timeout > data->max_timeout)
840 data->max_timeout = max_timeout;
842 if (entry->ipv4->inserted + entry->ipv4->timeout
843 < data->current_time)
847 if (entry->ipv6 != NULL && entry->ipv6->timeout > 0) {
848 max_timeout = entry->ipv6->inserted + entry->ipv6->timeout;
849 if (max_timeout > data->max_timeout)
850 data->max_timeout = max_timeout;
852 if (entry->ipv6->inserted + entry->ipv6->timeout
853 < data->current_time)
860 static void cache_cleanup(void)
862 static int max_timeout;
863 struct cache_timeout data;
866 data.current_time = time(0);
867 data.max_timeout = 0;
869 if (max_timeout > data.current_time) {
870 DBG("waiting %ld secs before cleaning cache",
871 max_timeout - data.current_time);
875 count = g_hash_table_foreach_remove(cache, cache_check_entry,
877 DBG("removed %d", count);
881 * If we could not remove anything, then remember
882 * what is the max timeout and do nothing if we
883 * have not yet reached it. This will prevent
884 * constant traversal of the cache if it is full.
886 max_timeout = data.max_timeout;
891 static int cache_update(struct server_data *srv, unsigned char *msg,
892 unsigned int msg_len)
894 int offset = protocol_offset(srv->protocol);
895 int err, qlen, ttl = 0;
896 uint16_t answers, type = 0, class = 0;
897 struct domain_question *q;
898 struct cache_entry *entry;
899 struct cache_data *data;
900 char question[NS_MAXDNAME + 1];
901 unsigned char response[NS_MAXDNAME + 1];
904 gboolean new_entry = TRUE;
907 if (cache_size >= MAX_CACHE_SIZE) {
909 if (cache_size >= MAX_CACHE_SIZE)
913 /* Continue only if response code is 0 (=ok) */
920 rsplen = sizeof(response) - 1;
921 question[sizeof(question) - 1] = '\0';
923 err = parse_response(msg + offset, msg_len - offset,
924 question, sizeof(question) - 1,
926 response, &rsplen, &answers);
927 if (err < 0 || ttl == 0)
930 qlen = strlen(question);
931 current_time = time(0);
934 * If the cache contains already data, check if the
935 * type of the cached data is the same and do not add
936 * to cache if data is already there.
937 * This is needed so that we can cache both A and AAAA
938 * records for the same name.
940 entry = g_hash_table_lookup(cache, question);
942 entry = g_try_new(struct cache_entry, 1);
946 data = g_try_new(struct cache_data, 1);
952 entry->key = g_strdup(question);
953 entry->ipv4 = entry->ipv6 = NULL;
960 if (type == 1 && entry->ipv4 != NULL)
963 if (type == 28 && entry->ipv6 != NULL)
966 data = g_try_new(struct cache_data, 1);
979 * Restrict the cached DNS record TTL to some sane value
980 * in order to prevent data staying in the cache too long.
982 if (ttl > MAX_CACHE_TTL)
985 data->inserted = current_time;
987 data->answers = answers;
989 data->data_len = 12 + qlen + 1 + 2 + 2 + rsplen;
990 data->data = ptr = g_malloc(data->data_len);
991 if (data->data == NULL) {
998 memcpy(ptr, msg, 12);
999 memcpy(ptr + 12, question, qlen + 1); /* copy also the \0 */
1001 q = (void *) (ptr + 12 + qlen + 1);
1002 q->type = htons(type);
1003 q->class = htons(class);
1004 memcpy(ptr + 12 + qlen + 1 + sizeof(struct domain_question),
1007 if (new_entry == TRUE) {
1008 g_hash_table_replace(cache, entry->key, entry);
1012 DBG("cache %d %squestion \"%s\" type %d ttl %d size %zd",
1013 cache_size, new_entry ? "new " : "old ",
1014 question, type, ttl,
1015 sizeof(*entry) + sizeof(*data) + data->data_len + qlen);
1020 static int ns_resolv(struct server_data *server, struct request_data *req,
1021 gpointer request, gpointer name)
1024 int sk, err, type = 0;
1025 char *dot, *lookup = (char *) name;
1026 struct cache_entry *entry;
1028 entry = cache_check(request, &type);
1029 if (entry != NULL) {
1030 struct cache_data *data;
1032 DBG("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1038 if (data != NULL && req->protocol == IPPROTO_TCP) {
1039 send_cached_response(req->client_sk, data->data,
1040 data->data_len, NULL, 0, IPPROTO_TCP,
1041 req->srcid, data->answers);
1045 if (data != NULL && req->protocol == IPPROTO_UDP) {
1047 sk = g_io_channel_unix_get_fd(
1048 req->ifdata->udp_listener_channel);
1050 send_cached_response(sk, data->data,
1051 data->data_len, &req->sa, req->sa_len,
1052 IPPROTO_UDP, req->srcid, data->answers);
1057 sk = g_io_channel_unix_get_fd(server->channel);
1059 err = send(sk, request, req->request_len, 0);
1063 /* If we have more than one dot, we don't add domains */
1064 dot = strchr(lookup, '.');
1065 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
1068 if (server->domains != NULL && server->domains->data != NULL)
1069 req->append_domain = TRUE;
1071 for (list = server->domains; list; list = list->next) {
1073 unsigned char alt[1024];
1074 struct domain_hdr *hdr = (void *) &alt;
1075 int altlen, domlen, offset;
1077 domain = list->data;
1082 offset = protocol_offset(server->protocol);
1086 domlen = strlen(domain) + 1;
1090 alt[offset] = req->altid & 0xff;
1091 alt[offset + 1] = req->altid >> 8;
1093 memcpy(alt + offset + 2, request + offset + 2, 10);
1094 hdr->qdcount = htons(1);
1096 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1103 memcpy(alt + offset + altlen,
1104 request + offset + altlen - domlen,
1105 req->request_len - altlen - offset + domlen);
1107 if (server->protocol == IPPROTO_TCP) {
1108 int req_len = req->request_len + domlen - 2;
1110 alt[0] = (req_len >> 8) & 0xff;
1111 alt[1] = req_len & 0xff;
1114 err = send(sk, alt, req->request_len + domlen, 0);
1124 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1125 struct server_data *data)
1127 struct domain_hdr *hdr;
1128 struct request_data *req;
1129 int dns_id, sk, err, offset = protocol_offset(protocol);
1130 struct listener_data *ifdata;
1135 hdr = (void *)(reply + offset);
1136 dns_id = reply[offset] | reply[offset + 1] << 8;
1138 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1140 req = find_request(dns_id);
1144 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
1146 ifdata = req->ifdata;
1148 reply[offset] = req->srcid & 0xff;
1149 reply[offset + 1] = req->srcid >> 8;
1153 if (hdr->rcode == 0 || req->resp == NULL) {
1156 * If the domain name was append
1157 * remove it before forwarding the reply.
1159 if (req->append_domain == TRUE) {
1162 unsigned int domain_len;
1165 * ptr points to the first char of the hostname.
1166 * ->hostname.domain.net
1168 ptr = reply + offset + sizeof(struct domain_hdr);
1170 domain_len = strlen((const char *)ptr) - host_len - 1;
1173 * remove the domain name and replaced it by the end
1176 memmove(ptr + host_len + 1,
1177 ptr + host_len + domain_len + 1,
1178 reply_len - (ptr - reply + domain_len));
1180 reply_len = reply_len - domain_len;
1186 req->resp = g_try_malloc(reply_len);
1187 if (req->resp == NULL)
1190 memcpy(req->resp, reply, reply_len);
1191 req->resplen = reply_len;
1193 cache_update(data, reply, reply_len);
1196 if (hdr->rcode > 0 && req->numresp < req->numserv)
1199 if (req->timeout > 0)
1200 g_source_remove(req->timeout);
1202 request_list = g_slist_remove(request_list, req);
1204 if (protocol == IPPROTO_UDP) {
1205 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
1206 err = sendto(sk, req->resp, req->resplen, 0,
1207 &req->sa, req->sa_len);
1209 sk = req->client_sk;
1210 err = send(sk, req->resp, req->resplen, 0);
1220 static void cache_element_destroy(gpointer value)
1222 struct cache_entry *entry = value;
1227 if (entry->ipv4 != NULL) {
1228 g_free(entry->ipv4->data);
1229 g_free(entry->ipv4);
1232 if (entry->ipv6 != NULL) {
1233 g_free(entry->ipv6->data);
1234 g_free(entry->ipv6);
1240 if (--cache_size < 0)
1244 static void destroy_server(struct server_data *server)
1248 DBG("interface %s server %s", server->interface, server->server);
1250 server_list = g_slist_remove(server_list, server);
1252 if (server->watch > 0)
1253 g_source_remove(server->watch);
1255 if (server->timeout > 0)
1256 g_source_remove(server->timeout);
1258 g_io_channel_unref(server->channel);
1260 if (server->protocol == IPPROTO_UDP)
1261 connman_info("Removing DNS server %s", server->server);
1263 g_free(server->incoming_reply);
1264 g_free(server->server);
1265 for (list = server->domains; list; list = list->next) {
1266 char *domain = list->data;
1268 server->domains = g_list_remove(server->domains, domain);
1271 g_free(server->interface);
1273 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1)
1274 g_hash_table_destroy(cache);
1279 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
1282 unsigned char buf[4096];
1284 struct server_data *data = user_data;
1286 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1287 connman_error("Error with UDP server %s", data->server);
1292 sk = g_io_channel_unix_get_fd(channel);
1294 len = recv(sk, buf, sizeof(buf), 0);
1298 err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
1305 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
1309 struct server_data *server = user_data;
1311 sk = g_io_channel_unix_get_fd(channel);
1315 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1318 DBG("TCP server channel closed");
1321 * Discard any partial response which is buffered; better
1322 * to get a proper response from a working server.
1324 g_free(server->incoming_reply);
1325 server->incoming_reply = NULL;
1327 for (list = request_list; list; list = list->next) {
1328 struct request_data *req = list->data;
1329 struct domain_hdr *hdr;
1331 if (req->protocol == IPPROTO_UDP)
1334 if (req->request == NULL)
1338 * If we're not waiting for any further response
1339 * from another name server, then we send an error
1340 * response to the client.
1342 if (req->numserv && --(req->numserv))
1345 hdr = (void *) (req->request + 2);
1346 hdr->id = req->srcid;
1347 send_response(req->client_sk, req->request,
1348 req->request_len, NULL, 0, IPPROTO_TCP);
1350 request_list = g_slist_remove(request_list, req);
1353 destroy_server(server);
1358 if ((condition & G_IO_OUT) && !server->connected) {
1361 struct server_data *udp_server;
1363 udp_server = find_server(server->interface, server->server,
1365 if (udp_server != NULL) {
1366 for (domains = udp_server->domains; domains;
1367 domains = domains->next) {
1368 char *dom = domains->data;
1370 DBG("Adding domain %s to %s",
1371 dom, server->server);
1373 server->domains = g_list_append(server->domains,
1378 server->connected = TRUE;
1379 server_list = g_slist_append(server_list, server);
1381 if (server->timeout > 0) {
1382 g_source_remove(server->timeout);
1383 server->timeout = 0;
1386 for (list = request_list; list; list = list->next) {
1387 struct request_data *req = list->data;
1389 if (req->protocol == IPPROTO_UDP)
1392 DBG("Sending req %s over TCP", (char *)req->name);
1394 if (req->timeout > 0)
1395 g_source_remove(req->timeout);
1397 req->timeout = g_timeout_add_seconds(30,
1398 request_timeout, req);
1399 if (ns_resolv(server, req, req->request,
1401 /* We sent cached result so no need for timeout
1404 if (req->timeout > 0) {
1405 g_source_remove(req->timeout);
1411 } else if (condition & G_IO_IN) {
1412 struct partial_reply *reply = server->incoming_reply;
1416 unsigned char reply_len_buf[2];
1419 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
1422 } else if (bytes_recv < 0) {
1423 if (errno == EAGAIN || errno == EWOULDBLOCK)
1426 connman_error("DNS proxy error %s",
1429 } else if (bytes_recv < 2)
1432 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
1435 DBG("TCP reply %d bytes", reply_len);
1437 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
1441 reply->len = reply_len;
1442 reply->received = 0;
1444 server->incoming_reply = reply;
1447 while (reply->received < reply->len) {
1448 bytes_recv = recv(sk, reply->buf + reply->received,
1449 reply->len - reply->received, 0);
1451 connman_error("DNS proxy TCP disconnect");
1453 } else if (bytes_recv < 0) {
1454 if (errno == EAGAIN || errno == EWOULDBLOCK)
1457 connman_error("DNS proxy error %s",
1461 reply->received += bytes_recv;
1464 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
1468 server->incoming_reply = NULL;
1470 destroy_server(server);
1478 static gboolean tcp_idle_timeout(gpointer user_data)
1480 struct server_data *server = user_data;
1487 destroy_server(server);
1492 static struct server_data *create_server(const char *interface,
1493 const char *domain, const char *server,
1496 struct addrinfo hints, *rp;
1497 struct server_data *data;
1500 DBG("interface %s server %s", interface, server);
1502 memset(&hints, 0, sizeof(hints));
1506 hints.ai_socktype = SOCK_DGRAM;
1510 hints.ai_socktype = SOCK_STREAM;
1516 hints.ai_family = AF_UNSPEC;
1517 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
1519 ret = getaddrinfo(server, "53", &hints, &rp);
1521 connman_error("Failed to parse server %s address: %s\n",
1522 server, gai_strerror(ret));
1525 /* Do not blindly copy this code elsewhere; it doesn't loop over the
1526 results using ->ai_next as it should. That's OK in *this* case
1527 because it was a numeric lookup; we *know* there's only one. */
1529 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1531 connman_error("Failed to create server %s socket", server);
1536 if (interface != NULL) {
1537 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1538 interface, strlen(interface) + 1) < 0) {
1539 connman_error("Failed to bind server %s "
1548 data = g_try_new0(struct server_data, 1);
1550 connman_error("Failed to allocate server %s data", server);
1556 data->channel = g_io_channel_unix_new(sk);
1557 if (data->channel == NULL) {
1558 connman_error("Failed to create server %s channel", server);
1565 g_io_channel_set_close_on_unref(data->channel, TRUE);
1567 if (protocol == IPPROTO_TCP) {
1568 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
1569 data->watch = g_io_add_watch(data->channel,
1570 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1571 tcp_server_event, data);
1572 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
1575 data->watch = g_io_add_watch(data->channel,
1576 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1577 udp_server_event, data);
1579 data->interface = g_strdup(interface);
1581 data->domains = g_list_append(data->domains, g_strdup(domain));
1582 data->server = g_strdup(server);
1583 data->protocol = protocol;
1585 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
1588 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
1589 protocol == IPPROTO_UDP) {
1592 connman_error("Failed to connect to server %s", server);
1593 if (data->watch > 0)
1594 g_source_remove(data->watch);
1595 if (data->timeout > 0)
1596 g_source_remove(data->timeout);
1598 g_io_channel_unref(data->channel);
1601 g_free(data->server);
1602 g_free(data->interface);
1603 for (list = data->domains; list; list = list->next) {
1604 char *domain = list->data;
1606 data->domains = g_list_remove(data->domains,
1615 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
1616 cache = g_hash_table_new_full(g_str_hash,
1619 cache_element_destroy);
1621 if (protocol == IPPROTO_UDP) {
1622 /* Enable new servers by default */
1623 data->enabled = TRUE;
1624 connman_info("Adding DNS server %s", data->server);
1626 server_list = g_slist_append(server_list, data);
1634 static gboolean resolv(struct request_data *req,
1635 gpointer request, gpointer name)
1640 for (list = server_list; list; list = list->next) {
1641 struct server_data *data = list->data;
1643 DBG("server %s enabled %d", data->server, data->enabled);
1645 if (data->enabled == FALSE)
1648 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
1649 data->watch = g_io_add_watch(data->channel,
1650 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1651 udp_server_event, data);
1653 status = ns_resolv(data, req, request, name);
1658 if (req->timeout > 0) {
1659 g_source_remove(req->timeout);
1668 static void append_domain(const char *interface, const char *domain)
1672 DBG("interface %s domain %s", interface, domain);
1677 for (list = server_list; list; list = list->next) {
1678 struct server_data *data = list->data;
1681 gboolean dom_found = FALSE;
1683 if (data->interface == NULL)
1686 if (g_str_equal(data->interface, interface) == FALSE)
1689 for (dom_list = data->domains; dom_list;
1690 dom_list = dom_list->next) {
1691 dom = dom_list->data;
1693 if (g_str_equal(dom, domain)) {
1699 if (dom_found == FALSE) {
1701 g_list_append(data->domains, g_strdup(domain));
1706 int __connman_dnsproxy_append(const char *interface, const char *domain,
1709 struct server_data *data;
1711 DBG("interface %s server %s", interface, server);
1713 if (server == NULL && domain == NULL)
1716 if (server == NULL) {
1717 append_domain(interface, domain);
1722 if (g_str_equal(server, "127.0.0.1") == TRUE)
1725 data = find_server(interface, server, IPPROTO_UDP);
1727 append_domain(interface, domain);
1731 data = create_server(interface, domain, server, IPPROTO_UDP);
1738 static void remove_server(const char *interface, const char *domain,
1739 const char *server, int protocol)
1741 struct server_data *data;
1743 data = find_server(interface, server, protocol);
1747 destroy_server(data);
1750 int __connman_dnsproxy_remove(const char *interface, const char *domain,
1753 DBG("interface %s server %s", interface, server);
1758 if (g_str_equal(server, "127.0.0.1") == TRUE)
1761 remove_server(interface, domain, server, IPPROTO_UDP);
1762 remove_server(interface, domain, server, IPPROTO_TCP);
1767 void __connman_dnsproxy_flush(void)
1771 list = request_pending_list;
1773 struct request_data *req = list->data;
1777 request_pending_list =
1778 g_slist_remove(request_pending_list, req);
1779 resolv(req, req->request, req->name);
1780 g_free(req->request);
1785 static void dnsproxy_offline_mode(connman_bool_t enabled)
1789 DBG("enabled %d", enabled);
1791 for (list = server_list; list; list = list->next) {
1792 struct server_data *data = list->data;
1794 if (enabled == FALSE) {
1795 connman_info("Enabling DNS server %s", data->server);
1796 data->enabled = TRUE;
1798 connman_info("Disabling DNS server %s", data->server);
1799 data->enabled = FALSE;
1804 static void dnsproxy_default_changed(struct connman_service *service)
1809 DBG("service %p", service);
1811 if (service == NULL) {
1812 /* When no services are active, then disable DNS proxying */
1813 dnsproxy_offline_mode(TRUE);
1817 interface = connman_service_get_interface(service);
1818 if (interface == NULL)
1821 for (list = server_list; list; list = list->next) {
1822 struct server_data *data = list->data;
1824 if (g_strcmp0(data->interface, interface) == 0) {
1825 connman_info("Enabling DNS server %s", data->server);
1826 data->enabled = TRUE;
1828 connman_info("Disabling DNS server %s", data->server);
1829 data->enabled = FALSE;
1836 static struct connman_notifier dnsproxy_notifier = {
1838 .default_changed = dnsproxy_default_changed,
1839 .offline_mode = dnsproxy_offline_mode,
1842 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
1844 static int parse_request(unsigned char *buf, int len,
1845 char *name, unsigned int size)
1847 struct domain_hdr *hdr = (void *) buf;
1848 uint16_t qdcount = ntohs(hdr->qdcount);
1849 uint16_t arcount = ntohs(hdr->arcount);
1851 char *last_label = NULL;
1852 unsigned int remain, used = 0;
1857 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
1858 hdr->id, hdr->qr, hdr->opcode,
1861 if (hdr->qr != 0 || qdcount != 1)
1864 memset(name, 0, size);
1866 ptr = buf + sizeof(struct domain_hdr);
1867 remain = len - sizeof(struct domain_hdr);
1869 while (remain > 0) {
1873 last_label = (char *) (ptr + 1);
1877 if (used + len + 1 > size)
1880 strncat(name, (char *) (ptr + 1), len);
1889 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
1890 !memcmp(last_label + 5, opt_edns0_type, 2)) {
1891 uint16_t edns0_bufsize;
1893 edns0_bufsize = last_label[7] << 8 | last_label[8];
1895 DBG("EDNS0 buffer size %u", edns0_bufsize);
1897 /* This is an evil hack until full TCP support has been
1900 * Somtimes the EDNS0 request gets send with a too-small
1901 * buffer size. Since glibc doesn't seem to crash when it
1902 * gets a response biffer then it requested, just bump
1903 * the buffer size up to 4KiB.
1905 if (edns0_bufsize < 0x1000) {
1906 last_label[7] = 0x10;
1907 last_label[8] = 0x00;
1911 DBG("query %s", name);
1916 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
1919 unsigned char buf[768];
1921 struct request_data *req;
1922 struct server_data *server;
1923 int sk, client_sk, len, err;
1924 struct sockaddr_in6 client_addr;
1925 socklen_t client_addr_len = sizeof(client_addr);
1927 struct listener_data *ifdata = user_data;
1929 DBG("condition 0x%x", condition);
1931 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1932 if (ifdata->tcp_listener_watch > 0)
1933 g_source_remove(ifdata->tcp_listener_watch);
1934 ifdata->tcp_listener_watch = 0;
1936 connman_error("Error with TCP listener channel");
1941 sk = g_io_channel_unix_get_fd(channel);
1943 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
1944 if (client_sk < 0) {
1945 connman_error("Accept failure on TCP listener");
1946 ifdata->tcp_listener_watch = 0;
1950 len = recv(client_sk, buf, sizeof(buf), 0);
1954 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
1956 err = parse_request(buf + 2, len - 2, query, sizeof(query));
1957 if (err < 0 || (g_slist_length(server_list) == 0)) {
1958 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
1962 req = g_try_new0(struct request_data, 1);
1966 memcpy(&req->sa, &client_addr, client_addr_len);
1967 req->sa_len = client_addr_len;
1968 req->client_sk = client_sk;
1969 req->protocol = IPPROTO_TCP;
1972 if (request_id == 0x0000 || request_id == 0xffff)
1975 req->srcid = buf[2] | (buf[3] << 8);
1976 req->dstid = request_id;
1977 req->altid = request_id + 1;
1978 req->request_len = len;
1980 buf[2] = req->dstid & 0xff;
1981 buf[3] = req->dstid >> 8;
1984 req->ifdata = (struct listener_data *) ifdata;
1985 req->append_domain = FALSE;
1986 request_list = g_slist_append(request_list, req);
1988 for (list = server_list; list; list = list->next) {
1989 struct server_data *data = list->data;
1992 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
1995 server = create_server(data->interface, NULL,
1996 data->server, IPPROTO_TCP);
1999 * If server is NULL, we're not connected yet.
2000 * Copy the relevant buffers and continue with
2001 * the next nameserver.
2002 * The request will actually be sent once we're
2003 * properly connected over TCP to this nameserver.
2005 if (server == NULL) {
2006 req->request = g_try_malloc0(req->request_len);
2007 if (req->request == NULL)
2010 memcpy(req->request, buf, req->request_len);
2012 req->name = g_try_malloc0(sizeof(query));
2013 if (req->name == NULL) {
2014 g_free(req->request);
2017 memcpy(req->name, query, sizeof(query));
2022 if (req->timeout > 0)
2023 g_source_remove(req->timeout);
2025 for (domains = data->domains; domains;
2026 domains = domains->next) {
2027 char *dom = domains->data;
2029 DBG("Adding domain %s to %s", dom, server->server);
2031 server->domains = g_list_append(server->domains,
2035 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
2036 if (ns_resolv(server, req, buf, query) > 0) {
2037 if (req->timeout > 0) {
2038 g_source_remove(req->timeout);
2047 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
2050 unsigned char buf[768];
2052 struct request_data *req;
2053 struct sockaddr_in6 client_addr;
2054 socklen_t client_addr_len = sizeof(client_addr);
2056 struct listener_data *ifdata = user_data;
2058 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2059 connman_error("Error with UDP listener channel");
2060 ifdata->udp_listener_watch = 0;
2064 sk = g_io_channel_unix_get_fd(channel);
2066 memset(&client_addr, 0, client_addr_len);
2067 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
2072 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
2074 err = parse_request(buf, len, query, sizeof(query));
2075 if (err < 0 || (g_slist_length(server_list) == 0)) {
2076 send_response(sk, buf, len, (void *)&client_addr,
2077 client_addr_len, IPPROTO_UDP);
2081 req = g_try_new0(struct request_data, 1);
2085 memcpy(&req->sa, &client_addr, client_addr_len);
2086 req->sa_len = client_addr_len;
2088 req->protocol = IPPROTO_UDP;
2091 if (request_id == 0x0000 || request_id == 0xffff)
2094 req->srcid = buf[0] | (buf[1] << 8);
2095 req->dstid = request_id;
2096 req->altid = request_id + 1;
2097 req->request_len = len;
2099 buf[0] = req->dstid & 0xff;
2100 buf[1] = req->dstid >> 8;
2103 req->ifdata = (struct listener_data *) ifdata;
2104 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2105 req->append_domain = FALSE;
2106 request_list = g_slist_append(request_list, req);
2108 return resolv(req, buf, query);
2111 static int create_dns_listener(int protocol, struct listener_data *ifdata)
2113 GIOChannel *channel;
2117 struct sockaddr_in6 sin6;
2118 struct sockaddr_in sin;
2121 int sk, type, v6only = 0;
2122 int family = AF_INET6;
2125 DBG("interface %s", ifdata->ifname);
2130 type = SOCK_DGRAM | SOCK_CLOEXEC;
2135 type = SOCK_STREAM | SOCK_CLOEXEC;
2142 sk = socket(family, type, protocol);
2143 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
2144 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
2146 sk = socket(family, type, protocol);
2149 connman_error("Failed to create %s listener socket", proto);
2153 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2155 strlen(ifdata->ifname) + 1) < 0) {
2156 connman_error("Failed to bind %s listener interface", proto);
2160 /* Ensure it accepts Legacy IP connections too */
2161 if (family == AF_INET6 &&
2162 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
2163 &v6only, sizeof(v6only)) < 0) {
2164 connman_error("Failed to clear V6ONLY on %s listener socket",
2170 if (family == AF_INET) {
2171 memset(&s.sin, 0, sizeof(s.sin));
2172 s.sin.sin_family = AF_INET;
2173 s.sin.sin_port = htons(53);
2174 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
2175 slen = sizeof(s.sin);
2177 memset(&s.sin6, 0, sizeof(s.sin6));
2178 s.sin6.sin6_family = AF_INET6;
2179 s.sin6.sin6_port = htons(53);
2180 s.sin6.sin6_addr = in6addr_any;
2181 slen = sizeof(s.sin6);
2184 if (bind(sk, &s.sa, slen) < 0) {
2185 connman_error("Failed to bind %s listener socket", proto);
2190 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
2191 connman_error("Failed to listen on TCP socket");
2196 channel = g_io_channel_unix_new(sk);
2197 if (channel == NULL) {
2198 connman_error("Failed to create %s listener channel", proto);
2203 g_io_channel_set_close_on_unref(channel, TRUE);
2205 if (protocol == IPPROTO_TCP) {
2206 ifdata->tcp_listener_channel = channel;
2207 ifdata->tcp_listener_watch = g_io_add_watch(channel,
2208 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
2210 ifdata->udp_listener_channel = channel;
2211 ifdata->udp_listener_watch = g_io_add_watch(channel,
2212 G_IO_IN, udp_listener_event, (gpointer) ifdata);
2218 static void destroy_udp_listener(struct listener_data *ifdata)
2220 DBG("interface %s", ifdata->ifname);
2222 if (ifdata->udp_listener_watch > 0)
2223 g_source_remove(ifdata->udp_listener_watch);
2225 g_io_channel_unref(ifdata->udp_listener_channel);
2228 static void destroy_tcp_listener(struct listener_data *ifdata)
2230 DBG("interface %s", ifdata->ifname);
2232 if (ifdata->tcp_listener_watch > 0)
2233 g_source_remove(ifdata->tcp_listener_watch);
2235 g_io_channel_unref(ifdata->tcp_listener_channel);
2238 static int create_listener(struct listener_data *ifdata)
2242 err = create_dns_listener(IPPROTO_UDP, ifdata);
2246 err = create_dns_listener(IPPROTO_TCP, ifdata);
2248 destroy_udp_listener(ifdata);
2252 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2253 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
2258 static void destroy_listener(struct listener_data *ifdata)
2262 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2263 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
2265 for (list = request_pending_list; list; list = list->next) {
2266 struct request_data *req = list->data;
2268 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
2269 req->srcid, req->dstid);
2272 g_free(req->request);
2278 g_slist_free(request_pending_list);
2279 request_pending_list = NULL;
2281 for (list = request_list; list; list = list->next) {
2282 struct request_data *req = list->data;
2284 DBG("Dropping request (id 0x%04x -> 0x%04x)",
2285 req->srcid, req->dstid);
2288 g_free(req->request);
2294 g_slist_free(request_list);
2295 request_list = NULL;
2297 destroy_tcp_listener(ifdata);
2298 destroy_udp_listener(ifdata);
2301 int __connman_dnsproxy_add_listener(const char *interface)
2303 struct listener_data *ifdata;
2306 DBG("interface %s", interface);
2308 if (g_hash_table_lookup(listener_table, interface) != NULL)
2311 ifdata = g_try_new0(struct listener_data, 1);
2315 ifdata->ifname = g_strdup(interface);
2316 ifdata->udp_listener_channel = NULL;
2317 ifdata->udp_listener_watch = 0;
2318 ifdata->tcp_listener_channel = NULL;
2319 ifdata->tcp_listener_watch = 0;
2321 err = create_listener(ifdata);
2323 connman_error("Couldn't create listener for %s err %d",
2325 g_free(ifdata->ifname);
2329 g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
2333 void __connman_dnsproxy_remove_listener(const char *interface)
2335 struct listener_data *ifdata;
2337 DBG("interface %s", interface);
2339 ifdata = g_hash_table_lookup(listener_table, interface);
2343 destroy_listener(ifdata);
2345 g_hash_table_remove(listener_table, interface);
2348 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
2350 __connman_dnsproxy_remove_listener(key);
2353 int __connman_dnsproxy_init(void)
2359 listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2361 err = __connman_dnsproxy_add_listener("lo");
2365 err = connman_notifier_register(&dnsproxy_notifier);
2372 __connman_dnsproxy_remove_listener("lo");
2373 g_hash_table_destroy(listener_table);
2378 void __connman_dnsproxy_cleanup(void)
2382 connman_notifier_unregister(&dnsproxy_notifier);
2384 g_hash_table_foreach(listener_table, remove_listener, NULL);
2386 g_hash_table_destroy(listener_table);