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);
582 *type = ntohs(rr->type);
583 *class = ntohs(rr->class);
584 *ttl = ntohl(rr->ttl);
585 *rdlen = ntohs(rr->rdlen);
590 memcpy(response + offset, *end, sizeof(struct domain_rr));
592 offset += sizeof(struct domain_rr);
593 *end += sizeof(struct domain_rr);
595 if ((unsigned int) (offset + *rdlen) > *response_size)
598 memcpy(response + offset, *end, *rdlen);
602 *response_size = offset + *rdlen;
607 static gboolean check_alias(GSList *aliases, char *name)
611 if (aliases != NULL) {
612 for (list = aliases; list; list = list->next) {
613 int len = strlen((char *)list->data);
614 if (strncmp((char *)list->data, name, len) == 0)
622 static int parse_response(unsigned char *buf, int buflen,
623 char *question, int qlen,
624 uint16_t *type, uint16_t *class, int *ttl,
625 unsigned char *response, unsigned int *response_len,
628 struct domain_hdr *hdr = (void *) buf;
629 struct domain_question *q;
631 uint16_t qdcount = ntohs(hdr->qdcount);
632 uint16_t ancount = ntohs(hdr->ancount);
634 uint16_t qtype, qclass;
635 unsigned char *next = NULL;
636 unsigned int maxlen = *response_len;
637 GSList *aliases = NULL, *list;
638 char name[NS_MAXDNAME + 1];
643 DBG("qr %d qdcount %d", hdr->qr, qdcount);
645 /* We currently only cache responses where question count is 1 */
646 if (hdr->qr != 1 || qdcount != 1)
649 ptr = buf + sizeof(struct domain_hdr);
651 strncpy(question, (char *) ptr, qlen);
652 qlen = strlen(question);
653 ptr += qlen + 1; /* skip \0 */
656 qtype = ntohs(q->type);
658 /* We cache only A and AAAA records */
659 if (qtype != 1 && qtype != 28)
662 qclass = ntohs(q->class);
664 ptr += 2 + 2; /* ptr points now to answers */
671 * We have a bunch of answers (like A, AAAA, CNAME etc) to
672 * A or AAAA question. We traverse the answers and parse the
673 * resource records. Only A and AAAA records are cached, all
674 * the other records in answers are skipped.
676 for (i = 0; i < ancount; i++) {
678 * Get one address at a time to this buffer.
679 * The max size of the answer is
680 * 2 (pointer) + 2 (type) + 2 (class) +
681 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
682 * for A or AAAA record.
683 * For CNAME the size can be bigger.
685 unsigned char rsp[NS_MAXCDNAME];
686 unsigned int rsp_len = sizeof(rsp) - 1;
689 memset(rsp, 0, sizeof(rsp));
691 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
692 type, class, ttl, &rdlen, &next, name);
699 * Now rsp contains compressed or uncompressed resource
700 * record. Next we check if this record answers the question.
701 * The name var contains the uncompressed label.
702 * One tricky bit is the CNAME records as they alias
703 * the name we might be interested in.
707 * Go to next answer if the class is not the one we are
710 if (*class != qclass) {
717 * Try to resolve aliases also, type is CNAME(5).
718 * This is important as otherwise the aliased names would not
719 * be cached at all as the cache would not contain the aliased
722 * If any CNAME is found in DNS packet, then we cache the alias
723 * IP address instead of the question (as the server
724 * said that question has only an alias).
725 * This means in practice that if e.g., ipv6.google.com is
726 * queried, DNS server returns CNAME of that name which is
727 * ipv6.l.google.com. We then cache the address of the CNAME
728 * but return the question name to client. So the alias
729 * status of the name is not saved in cache and thus not
730 * returned to the client. We do not return DNS packets from
731 * cache to client saying that ipv6.google.com is an alias to
732 * ipv6.l.google.com but we return instead a DNS packet that
733 * says ipv6.google.com has address xxx which is in fact the
734 * address of ipv6.l.google.com. For caching purposes this
735 * should not cause any issues.
737 if (*type == 5 && strncmp(question, name, qlen) == 0) {
739 * So now the alias answered the question. This is
740 * not very useful from caching point of view as
741 * the following A or AAAA records will not match the
742 * question. We need to find the real A/AAAA record
743 * of the alias and cache that.
745 unsigned char *end = NULL;
746 int name_len = 0, output_len;
748 memset(rsp, 0, sizeof(rsp));
749 rsp_len = sizeof(rsp) - 1;
752 * Alias is in rdata part of the message,
753 * and next-rdlen points to it. So we need to get
754 * the real name of the alias.
756 ret = get_name(0, buf, next - rdlen, buf + buflen,
757 rsp, rsp_len, &output_len, &end,
760 /* just ignore the error at this point */
767 * We should now have the alias of the entry we might
768 * want to cache. Just remember it for a while.
769 * We check the alias list when we have parsed the
772 aliases = g_slist_prepend(aliases, g_strdup(name));
779 if (*type == qtype) {
781 * We found correct type (A or AAAA)
783 if (check_alias(aliases, name) == TRUE ||
784 (aliases == NULL && strncmp(question, name,
787 * We found an alias or the name of the rr
788 * matches the question. If so, we append
789 * the compressed label to the cache.
790 * The end result is a response buffer that
791 * will contain one or more cached and
792 * compressed resource records.
794 if (*response_len + rsp_len > maxlen) {
798 memcpy(response + *response_len, rsp, rsp_len);
799 *response_len += rsp_len;
810 for (list = aliases; list; list = list->next)
812 g_slist_free(aliases);
817 struct cache_timeout {
822 static gboolean cache_check_entry(gpointer key, gpointer value,
825 struct cache_timeout *data = user_data;
826 struct cache_entry *entry = value;
830 * If either IPv4 or IPv6 cached entry has expired, we
831 * remove both from the cache.
834 if (entry->ipv4 != NULL && entry->ipv4->timeout > 0) {
835 max_timeout = entry->ipv4->inserted + entry->ipv4->timeout;
836 if (max_timeout > data->max_timeout)
837 data->max_timeout = max_timeout;
839 if (entry->ipv4->inserted + entry->ipv4->timeout
840 < data->current_time)
844 if (entry->ipv6 != NULL && entry->ipv6->timeout > 0) {
845 max_timeout = entry->ipv6->inserted + entry->ipv6->timeout;
846 if (max_timeout > data->max_timeout)
847 data->max_timeout = max_timeout;
849 if (entry->ipv6->inserted + entry->ipv6->timeout
850 < data->current_time)
857 static void cache_cleanup(void)
859 static int max_timeout;
860 struct cache_timeout data;
863 data.current_time = time(0);
864 data.max_timeout = 0;
866 if (max_timeout > data.current_time) {
867 DBG("waiting %ld secs before cleaning cache",
868 max_timeout - data.current_time);
872 count = g_hash_table_foreach_remove(cache, cache_check_entry,
874 DBG("removed %d", count);
878 * If we could not remove anything, then remember
879 * what is the max timeout and do nothing if we
880 * have not yet reached it. This will prevent
881 * constant traversal of the cache if it is full.
883 max_timeout = data.max_timeout;
888 static int cache_update(struct server_data *srv, unsigned char *msg,
889 unsigned int msg_len)
891 int offset = protocol_offset(srv->protocol);
892 int err, qlen, ttl = 0;
893 uint16_t answers, type = 0, class = 0;
894 struct domain_question *q;
895 struct cache_entry *entry;
896 struct cache_data *data;
897 char question[NS_MAXDNAME + 1];
898 unsigned char response[NS_MAXDNAME + 1];
901 gboolean new_entry = TRUE;
904 if (cache_size >= MAX_CACHE_SIZE) {
906 if (cache_size >= MAX_CACHE_SIZE)
910 /* Continue only if response code is 0 (=ok) */
917 rsplen = sizeof(response) - 1;
918 question[sizeof(question) - 1] = '\0';
920 err = parse_response(msg + offset, msg_len - offset,
921 question, sizeof(question) - 1,
923 response, &rsplen, &answers);
924 if (err < 0 || ttl == 0)
927 qlen = strlen(question);
928 current_time = time(0);
931 * If the cache contains already data, check if the
932 * type of the cached data is the same and do not add
933 * to cache if data is already there.
934 * This is needed so that we can cache both A and AAAA
935 * records for the same name.
937 entry = g_hash_table_lookup(cache, question);
939 entry = g_try_new(struct cache_entry, 1);
943 data = g_try_new(struct cache_data, 1);
949 entry->key = g_strdup(question);
950 entry->ipv4 = entry->ipv6 = NULL;
957 if (type == 1 && entry->ipv4 != NULL)
960 if (type == 28 && entry->ipv6 != NULL)
963 data = g_try_new(struct cache_data, 1);
976 * Restrict the cached DNS record TTL to some sane value
977 * in order to prevent data staying in the cache too long.
979 if (ttl > MAX_CACHE_TTL)
982 data->inserted = current_time;
984 data->answers = answers;
986 data->data_len = 12 + qlen + 1 + 2 + 2 + rsplen;
987 data->data = ptr = g_malloc(data->data_len);
988 if (data->data == NULL) {
995 memcpy(ptr, msg, 12);
996 memcpy(ptr + 12, question, qlen + 1); /* copy also the \0 */
998 q = (void *) (ptr + 12 + qlen + 1);
999 q->type = htons(type);
1000 q->class = htons(class);
1001 memcpy(ptr + 12 + qlen + 1 + sizeof(struct domain_question),
1004 if (new_entry == TRUE) {
1005 g_hash_table_replace(cache, entry->key, entry);
1009 DBG("cache %d %squestion \"%s\" type %d ttl %d size %zd",
1010 cache_size, new_entry ? "new " : "old ",
1011 question, type, ttl,
1012 sizeof(*entry) + sizeof(*data) + data->data_len + qlen);
1017 static int ns_resolv(struct server_data *server, struct request_data *req,
1018 gpointer request, gpointer name)
1021 int sk, err, type = 0;
1022 char *dot, *lookup = (char *) name;
1023 struct cache_entry *entry;
1025 entry = cache_check(request, &type);
1026 if (entry != NULL) {
1027 struct cache_data *data;
1029 DBG("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1035 if (data != NULL && req->protocol == IPPROTO_TCP) {
1036 send_cached_response(req->client_sk, data->data,
1037 data->data_len, NULL, 0, IPPROTO_TCP,
1038 req->srcid, data->answers);
1042 if (data != NULL && req->protocol == IPPROTO_UDP) {
1044 sk = g_io_channel_unix_get_fd(
1045 req->ifdata->udp_listener_channel);
1047 send_cached_response(sk, data->data,
1048 data->data_len, &req->sa, req->sa_len,
1049 IPPROTO_UDP, req->srcid, data->answers);
1054 sk = g_io_channel_unix_get_fd(server->channel);
1056 err = send(sk, request, req->request_len, 0);
1060 /* If we have more than one dot, we don't add domains */
1061 dot = strchr(lookup, '.');
1062 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
1065 if (server->domains != NULL && server->domains->data != NULL)
1066 req->append_domain = TRUE;
1068 for (list = server->domains; list; list = list->next) {
1070 unsigned char alt[1024];
1071 struct domain_hdr *hdr = (void *) &alt;
1072 int altlen, domlen, offset;
1074 domain = list->data;
1079 offset = protocol_offset(server->protocol);
1083 domlen = strlen(domain) + 1;
1087 alt[offset] = req->altid & 0xff;
1088 alt[offset + 1] = req->altid >> 8;
1090 memcpy(alt + offset + 2, request + offset + 2, 10);
1091 hdr->qdcount = htons(1);
1093 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1100 memcpy(alt + offset + altlen,
1101 request + offset + altlen - domlen,
1102 req->request_len - altlen - offset + domlen);
1104 if (server->protocol == IPPROTO_TCP) {
1105 int req_len = req->request_len + domlen - 2;
1107 alt[0] = (req_len >> 8) & 0xff;
1108 alt[1] = req_len & 0xff;
1111 err = send(sk, alt, req->request_len + domlen, 0);
1121 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1122 struct server_data *data)
1124 struct domain_hdr *hdr;
1125 struct request_data *req;
1126 int dns_id, sk, err, offset = protocol_offset(protocol);
1127 struct listener_data *ifdata;
1132 hdr = (void *)(reply + offset);
1133 dns_id = reply[offset] | reply[offset + 1] << 8;
1135 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1137 req = find_request(dns_id);
1141 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
1143 ifdata = req->ifdata;
1145 reply[offset] = req->srcid & 0xff;
1146 reply[offset + 1] = req->srcid >> 8;
1150 if (hdr->rcode == 0 || req->resp == NULL) {
1153 * If the domain name was append
1154 * remove it before forwarding the reply.
1156 if (req->append_domain == TRUE) {
1159 unsigned int domain_len;
1162 * ptr points to the first char of the hostname.
1163 * ->hostname.domain.net
1165 ptr = reply + offset + sizeof(struct domain_hdr);
1167 domain_len = strlen((const char *)ptr) - host_len - 1;
1170 * remove the domain name and replaced it by the end
1173 memmove(ptr + host_len + 1,
1174 ptr + host_len + domain_len + 1,
1175 reply_len - (ptr - reply + domain_len));
1177 reply_len = reply_len - domain_len;
1183 req->resp = g_try_malloc(reply_len);
1184 if (req->resp == NULL)
1187 memcpy(req->resp, reply, reply_len);
1188 req->resplen = reply_len;
1190 cache_update(data, reply, reply_len);
1193 if (hdr->rcode > 0 && req->numresp < req->numserv)
1196 if (req->timeout > 0)
1197 g_source_remove(req->timeout);
1199 request_list = g_slist_remove(request_list, req);
1201 if (protocol == IPPROTO_UDP) {
1202 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
1203 err = sendto(sk, req->resp, req->resplen, 0,
1204 &req->sa, req->sa_len);
1206 sk = req->client_sk;
1207 err = send(sk, req->resp, req->resplen, 0);
1217 static void cache_element_destroy(gpointer value)
1219 struct cache_entry *entry = value;
1224 if (entry->ipv4 != NULL) {
1225 g_free(entry->ipv4->data);
1226 g_free(entry->ipv4);
1229 if (entry->ipv6 != NULL) {
1230 g_free(entry->ipv6->data);
1231 g_free(entry->ipv6);
1237 if (--cache_size < 0)
1241 static void destroy_server(struct server_data *server)
1245 DBG("interface %s server %s", server->interface, server->server);
1247 server_list = g_slist_remove(server_list, server);
1249 if (server->watch > 0)
1250 g_source_remove(server->watch);
1252 if (server->timeout > 0)
1253 g_source_remove(server->timeout);
1255 g_io_channel_unref(server->channel);
1257 if (server->protocol == IPPROTO_UDP)
1258 connman_info("Removing DNS server %s", server->server);
1260 g_free(server->incoming_reply);
1261 g_free(server->server);
1262 for (list = server->domains; list; list = list->next) {
1263 char *domain = list->data;
1265 server->domains = g_list_remove(server->domains, domain);
1268 g_free(server->interface);
1270 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1)
1271 g_hash_table_destroy(cache);
1276 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
1279 unsigned char buf[4096];
1281 struct server_data *data = user_data;
1283 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1284 connman_error("Error with UDP server %s", data->server);
1289 sk = g_io_channel_unix_get_fd(channel);
1291 len = recv(sk, buf, sizeof(buf), 0);
1295 err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
1302 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
1306 struct server_data *server = user_data;
1308 sk = g_io_channel_unix_get_fd(channel);
1312 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1315 DBG("TCP server channel closed");
1318 * Discard any partial response which is buffered; better
1319 * to get a proper response from a working server.
1321 g_free(server->incoming_reply);
1322 server->incoming_reply = NULL;
1324 for (list = request_list; list; list = list->next) {
1325 struct request_data *req = list->data;
1326 struct domain_hdr *hdr;
1328 if (req->protocol == IPPROTO_UDP)
1331 if (req->request == NULL)
1335 * If we're not waiting for any further response
1336 * from another name server, then we send an error
1337 * response to the client.
1339 if (req->numserv && --(req->numserv))
1342 hdr = (void *) (req->request + 2);
1343 hdr->id = req->srcid;
1344 send_response(req->client_sk, req->request,
1345 req->request_len, NULL, 0, IPPROTO_TCP);
1347 request_list = g_slist_remove(request_list, req);
1350 destroy_server(server);
1355 if ((condition & G_IO_OUT) && !server->connected) {
1358 struct server_data *udp_server;
1360 udp_server = find_server(server->interface, server->server,
1362 if (udp_server != NULL) {
1363 for (domains = udp_server->domains; domains;
1364 domains = domains->next) {
1365 char *dom = domains->data;
1367 DBG("Adding domain %s to %s",
1368 dom, server->server);
1370 server->domains = g_list_append(server->domains,
1375 server->connected = TRUE;
1376 server_list = g_slist_append(server_list, server);
1378 if (server->timeout > 0) {
1379 g_source_remove(server->timeout);
1380 server->timeout = 0;
1383 for (list = request_list; list; list = list->next) {
1384 struct request_data *req = list->data;
1386 if (req->protocol == IPPROTO_UDP)
1389 DBG("Sending req %s over TCP", (char *)req->name);
1391 if (req->timeout > 0)
1392 g_source_remove(req->timeout);
1394 req->timeout = g_timeout_add_seconds(30,
1395 request_timeout, req);
1396 if (ns_resolv(server, req, req->request,
1398 /* We sent cached result so no need for timeout
1401 if (req->timeout > 0) {
1402 g_source_remove(req->timeout);
1408 } else if (condition & G_IO_IN) {
1409 struct partial_reply *reply = server->incoming_reply;
1413 unsigned char reply_len_buf[2];
1416 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
1419 } else if (bytes_recv < 0) {
1420 if (errno == EAGAIN || errno == EWOULDBLOCK)
1423 connman_error("DNS proxy error %s",
1426 } else if (bytes_recv < 2)
1429 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
1432 DBG("TCP reply %d bytes", reply_len);
1434 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
1438 reply->len = reply_len;
1439 reply->received = 0;
1441 server->incoming_reply = reply;
1444 while (reply->received < reply->len) {
1445 bytes_recv = recv(sk, reply->buf + reply->received,
1446 reply->len - reply->received, 0);
1448 connman_error("DNS proxy TCP disconnect");
1450 } else if (bytes_recv < 0) {
1451 if (errno == EAGAIN || errno == EWOULDBLOCK)
1454 connman_error("DNS proxy error %s",
1458 reply->received += bytes_recv;
1461 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
1465 server->incoming_reply = NULL;
1467 destroy_server(server);
1475 static gboolean tcp_idle_timeout(gpointer user_data)
1477 struct server_data *server = user_data;
1484 destroy_server(server);
1489 static struct server_data *create_server(const char *interface,
1490 const char *domain, const char *server,
1493 struct addrinfo hints, *rp;
1494 struct server_data *data;
1497 DBG("interface %s server %s", interface, server);
1499 memset(&hints, 0, sizeof(hints));
1503 hints.ai_socktype = SOCK_DGRAM;
1507 hints.ai_socktype = SOCK_STREAM;
1513 hints.ai_family = AF_UNSPEC;
1514 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
1516 ret = getaddrinfo(server, "53", &hints, &rp);
1518 connman_error("Failed to parse server %s address: %s\n",
1519 server, gai_strerror(ret));
1522 /* Do not blindly copy this code elsewhere; it doesn't loop over the
1523 results using ->ai_next as it should. That's OK in *this* case
1524 because it was a numeric lookup; we *know* there's only one. */
1526 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1528 connman_error("Failed to create server %s socket", server);
1533 if (interface != NULL) {
1534 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1535 interface, strlen(interface) + 1) < 0) {
1536 connman_error("Failed to bind server %s "
1545 data = g_try_new0(struct server_data, 1);
1547 connman_error("Failed to allocate server %s data", server);
1553 data->channel = g_io_channel_unix_new(sk);
1554 if (data->channel == NULL) {
1555 connman_error("Failed to create server %s channel", server);
1562 g_io_channel_set_close_on_unref(data->channel, TRUE);
1564 if (protocol == IPPROTO_TCP) {
1565 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
1566 data->watch = g_io_add_watch(data->channel,
1567 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1568 tcp_server_event, data);
1569 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
1572 data->watch = g_io_add_watch(data->channel,
1573 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1574 udp_server_event, data);
1576 data->interface = g_strdup(interface);
1578 data->domains = g_list_append(data->domains, g_strdup(domain));
1579 data->server = g_strdup(server);
1580 data->protocol = protocol;
1582 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
1585 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
1586 protocol == IPPROTO_UDP) {
1589 connman_error("Failed to connect to server %s", server);
1590 if (data->watch > 0)
1591 g_source_remove(data->watch);
1592 if (data->timeout > 0)
1593 g_source_remove(data->timeout);
1595 g_io_channel_unref(data->channel);
1598 g_free(data->server);
1599 g_free(data->interface);
1600 for (list = data->domains; list; list = list->next) {
1601 char *domain = list->data;
1603 data->domains = g_list_remove(data->domains,
1612 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
1613 cache = g_hash_table_new_full(g_str_hash,
1616 cache_element_destroy);
1618 if (protocol == IPPROTO_UDP) {
1619 /* Enable new servers by default */
1620 data->enabled = TRUE;
1621 connman_info("Adding DNS server %s", data->server);
1623 server_list = g_slist_append(server_list, data);
1631 static gboolean resolv(struct request_data *req,
1632 gpointer request, gpointer name)
1637 for (list = server_list; list; list = list->next) {
1638 struct server_data *data = list->data;
1640 DBG("server %s enabled %d", data->server, data->enabled);
1642 if (data->enabled == FALSE)
1645 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
1646 data->watch = g_io_add_watch(data->channel,
1647 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1648 udp_server_event, data);
1650 status = ns_resolv(data, req, request, name);
1655 if (req->timeout > 0) {
1656 g_source_remove(req->timeout);
1665 static void append_domain(const char *interface, const char *domain)
1669 DBG("interface %s domain %s", interface, domain);
1674 for (list = server_list; list; list = list->next) {
1675 struct server_data *data = list->data;
1678 gboolean dom_found = FALSE;
1680 if (data->interface == NULL)
1683 if (g_str_equal(data->interface, interface) == FALSE)
1686 for (dom_list = data->domains; dom_list;
1687 dom_list = dom_list->next) {
1688 dom = dom_list->data;
1690 if (g_str_equal(dom, domain)) {
1696 if (dom_found == FALSE) {
1698 g_list_append(data->domains, g_strdup(domain));
1703 int __connman_dnsproxy_append(const char *interface, const char *domain,
1706 struct server_data *data;
1708 DBG("interface %s server %s", interface, server);
1710 if (server == NULL && domain == NULL)
1713 if (server == NULL) {
1714 append_domain(interface, domain);
1719 if (g_str_equal(server, "127.0.0.1") == TRUE)
1722 data = find_server(interface, server, IPPROTO_UDP);
1724 append_domain(interface, domain);
1728 data = create_server(interface, domain, server, IPPROTO_UDP);
1735 static void remove_server(const char *interface, const char *domain,
1736 const char *server, int protocol)
1738 struct server_data *data;
1740 data = find_server(interface, server, protocol);
1744 destroy_server(data);
1747 int __connman_dnsproxy_remove(const char *interface, const char *domain,
1750 DBG("interface %s server %s", interface, server);
1755 if (g_str_equal(server, "127.0.0.1") == TRUE)
1758 remove_server(interface, domain, server, IPPROTO_UDP);
1759 remove_server(interface, domain, server, IPPROTO_TCP);
1764 void __connman_dnsproxy_flush(void)
1768 list = request_pending_list;
1770 struct request_data *req = list->data;
1774 request_pending_list =
1775 g_slist_remove(request_pending_list, req);
1776 resolv(req, req->request, req->name);
1777 g_free(req->request);
1782 static void dnsproxy_offline_mode(connman_bool_t enabled)
1786 DBG("enabled %d", enabled);
1788 for (list = server_list; list; list = list->next) {
1789 struct server_data *data = list->data;
1791 if (enabled == FALSE) {
1792 connman_info("Enabling DNS server %s", data->server);
1793 data->enabled = TRUE;
1795 connman_info("Disabling DNS server %s", data->server);
1796 data->enabled = FALSE;
1801 static void dnsproxy_default_changed(struct connman_service *service)
1806 DBG("service %p", service);
1808 if (service == NULL) {
1809 /* When no services are active, then disable DNS proxying */
1810 dnsproxy_offline_mode(TRUE);
1814 interface = connman_service_get_interface(service);
1815 if (interface == NULL)
1818 for (list = server_list; list; list = list->next) {
1819 struct server_data *data = list->data;
1821 if (g_strcmp0(data->interface, interface) == 0) {
1822 connman_info("Enabling DNS server %s", data->server);
1823 data->enabled = TRUE;
1825 connman_info("Disabling DNS server %s", data->server);
1826 data->enabled = FALSE;
1833 static struct connman_notifier dnsproxy_notifier = {
1835 .default_changed = dnsproxy_default_changed,
1836 .offline_mode = dnsproxy_offline_mode,
1839 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
1841 static int parse_request(unsigned char *buf, int len,
1842 char *name, unsigned int size)
1844 struct domain_hdr *hdr = (void *) buf;
1845 uint16_t qdcount = ntohs(hdr->qdcount);
1846 uint16_t arcount = ntohs(hdr->arcount);
1848 char *last_label = NULL;
1849 unsigned int remain, used = 0;
1854 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
1855 hdr->id, hdr->qr, hdr->opcode,
1858 if (hdr->qr != 0 || qdcount != 1)
1861 memset(name, 0, size);
1863 ptr = buf + sizeof(struct domain_hdr);
1864 remain = len - sizeof(struct domain_hdr);
1866 while (remain > 0) {
1870 last_label = (char *) (ptr + 1);
1874 if (used + len + 1 > size)
1877 strncat(name, (char *) (ptr + 1), len);
1886 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
1887 !memcmp(last_label + 5, opt_edns0_type, 2)) {
1888 uint16_t edns0_bufsize;
1890 edns0_bufsize = last_label[7] << 8 | last_label[8];
1892 DBG("EDNS0 buffer size %u", edns0_bufsize);
1894 /* This is an evil hack until full TCP support has been
1897 * Somtimes the EDNS0 request gets send with a too-small
1898 * buffer size. Since glibc doesn't seem to crash when it
1899 * gets a response biffer then it requested, just bump
1900 * the buffer size up to 4KiB.
1902 if (edns0_bufsize < 0x1000) {
1903 last_label[7] = 0x10;
1904 last_label[8] = 0x00;
1908 DBG("query %s", name);
1913 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
1916 unsigned char buf[768];
1918 struct request_data *req;
1919 struct server_data *server;
1920 int sk, client_sk, len, err;
1921 struct sockaddr_in6 client_addr;
1922 socklen_t client_addr_len = sizeof(client_addr);
1924 struct listener_data *ifdata = user_data;
1926 DBG("condition 0x%x", condition);
1928 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1929 if (ifdata->tcp_listener_watch > 0)
1930 g_source_remove(ifdata->tcp_listener_watch);
1931 ifdata->tcp_listener_watch = 0;
1933 connman_error("Error with TCP listener channel");
1938 sk = g_io_channel_unix_get_fd(channel);
1940 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
1941 if (client_sk < 0) {
1942 connman_error("Accept failure on TCP listener");
1943 ifdata->tcp_listener_watch = 0;
1947 len = recv(client_sk, buf, sizeof(buf), 0);
1951 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
1953 err = parse_request(buf + 2, len - 2, query, sizeof(query));
1954 if (err < 0 || (g_slist_length(server_list) == 0)) {
1955 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
1959 req = g_try_new0(struct request_data, 1);
1963 memcpy(&req->sa, &client_addr, client_addr_len);
1964 req->sa_len = client_addr_len;
1965 req->client_sk = client_sk;
1966 req->protocol = IPPROTO_TCP;
1969 if (request_id == 0x0000 || request_id == 0xffff)
1972 req->srcid = buf[2] | (buf[3] << 8);
1973 req->dstid = request_id;
1974 req->altid = request_id + 1;
1975 req->request_len = len;
1977 buf[2] = req->dstid & 0xff;
1978 buf[3] = req->dstid >> 8;
1981 req->ifdata = (struct listener_data *) ifdata;
1982 req->append_domain = FALSE;
1983 request_list = g_slist_append(request_list, req);
1985 for (list = server_list; list; list = list->next) {
1986 struct server_data *data = list->data;
1989 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
1992 server = create_server(data->interface, NULL,
1993 data->server, IPPROTO_TCP);
1996 * If server is NULL, we're not connected yet.
1997 * Copy the relevant buffers and continue with
1998 * the next nameserver.
1999 * The request will actually be sent once we're
2000 * properly connected over TCP to this nameserver.
2002 if (server == NULL) {
2003 req->request = g_try_malloc0(req->request_len);
2004 if (req->request == NULL)
2007 memcpy(req->request, buf, req->request_len);
2009 req->name = g_try_malloc0(sizeof(query));
2010 if (req->name == NULL) {
2011 g_free(req->request);
2014 memcpy(req->name, query, sizeof(query));
2019 if (req->timeout > 0)
2020 g_source_remove(req->timeout);
2022 for (domains = data->domains; domains;
2023 domains = domains->next) {
2024 char *dom = domains->data;
2026 DBG("Adding domain %s to %s", dom, server->server);
2028 server->domains = g_list_append(server->domains,
2032 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
2033 if (ns_resolv(server, req, buf, query) > 0) {
2034 if (req->timeout > 0) {
2035 g_source_remove(req->timeout);
2044 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
2047 unsigned char buf[768];
2049 struct request_data *req;
2050 struct sockaddr_in6 client_addr;
2051 socklen_t client_addr_len = sizeof(client_addr);
2053 struct listener_data *ifdata = user_data;
2055 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2056 connman_error("Error with UDP listener channel");
2057 ifdata->udp_listener_watch = 0;
2061 sk = g_io_channel_unix_get_fd(channel);
2063 memset(&client_addr, 0, client_addr_len);
2064 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
2069 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
2071 err = parse_request(buf, len, query, sizeof(query));
2072 if (err < 0 || (g_slist_length(server_list) == 0)) {
2073 send_response(sk, buf, len, (void *)&client_addr,
2074 client_addr_len, IPPROTO_UDP);
2078 req = g_try_new0(struct request_data, 1);
2082 memcpy(&req->sa, &client_addr, client_addr_len);
2083 req->sa_len = client_addr_len;
2085 req->protocol = IPPROTO_UDP;
2088 if (request_id == 0x0000 || request_id == 0xffff)
2091 req->srcid = buf[0] | (buf[1] << 8);
2092 req->dstid = request_id;
2093 req->altid = request_id + 1;
2094 req->request_len = len;
2096 buf[0] = req->dstid & 0xff;
2097 buf[1] = req->dstid >> 8;
2100 req->ifdata = (struct listener_data *) ifdata;
2101 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2102 req->append_domain = FALSE;
2103 request_list = g_slist_append(request_list, req);
2105 return resolv(req, buf, query);
2108 static int create_dns_listener(int protocol, struct listener_data *ifdata)
2110 GIOChannel *channel;
2114 struct sockaddr_in6 sin6;
2115 struct sockaddr_in sin;
2118 int sk, type, v6only = 0;
2119 int family = AF_INET6;
2122 DBG("interface %s", ifdata->ifname);
2127 type = SOCK_DGRAM | SOCK_CLOEXEC;
2132 type = SOCK_STREAM | SOCK_CLOEXEC;
2139 sk = socket(family, type, protocol);
2140 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
2141 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
2143 sk = socket(family, type, protocol);
2146 connman_error("Failed to create %s listener socket", proto);
2150 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2152 strlen(ifdata->ifname) + 1) < 0) {
2153 connman_error("Failed to bind %s listener interface", proto);
2157 /* Ensure it accepts Legacy IP connections too */
2158 if (family == AF_INET6 &&
2159 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
2160 &v6only, sizeof(v6only)) < 0) {
2161 connman_error("Failed to clear V6ONLY on %s listener socket",
2167 if (family == AF_INET) {
2168 memset(&s.sin, 0, sizeof(s.sin));
2169 s.sin.sin_family = AF_INET;
2170 s.sin.sin_port = htons(53);
2171 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
2172 slen = sizeof(s.sin);
2174 memset(&s.sin6, 0, sizeof(s.sin6));
2175 s.sin6.sin6_family = AF_INET6;
2176 s.sin6.sin6_port = htons(53);
2177 s.sin6.sin6_addr = in6addr_any;
2178 slen = sizeof(s.sin6);
2181 if (bind(sk, &s.sa, slen) < 0) {
2182 connman_error("Failed to bind %s listener socket", proto);
2187 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
2188 connman_error("Failed to listen on TCP socket");
2193 channel = g_io_channel_unix_new(sk);
2194 if (channel == NULL) {
2195 connman_error("Failed to create %s listener channel", proto);
2200 g_io_channel_set_close_on_unref(channel, TRUE);
2202 if (protocol == IPPROTO_TCP) {
2203 ifdata->tcp_listener_channel = channel;
2204 ifdata->tcp_listener_watch = g_io_add_watch(channel,
2205 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
2207 ifdata->udp_listener_channel = channel;
2208 ifdata->udp_listener_watch = g_io_add_watch(channel,
2209 G_IO_IN, udp_listener_event, (gpointer) ifdata);
2215 static void destroy_udp_listener(struct listener_data *ifdata)
2217 DBG("interface %s", ifdata->ifname);
2219 if (ifdata->udp_listener_watch > 0)
2220 g_source_remove(ifdata->udp_listener_watch);
2222 g_io_channel_unref(ifdata->udp_listener_channel);
2225 static void destroy_tcp_listener(struct listener_data *ifdata)
2227 DBG("interface %s", ifdata->ifname);
2229 if (ifdata->tcp_listener_watch > 0)
2230 g_source_remove(ifdata->tcp_listener_watch);
2232 g_io_channel_unref(ifdata->tcp_listener_channel);
2235 static int create_listener(struct listener_data *ifdata)
2239 err = create_dns_listener(IPPROTO_UDP, ifdata);
2243 err = create_dns_listener(IPPROTO_TCP, ifdata);
2245 destroy_udp_listener(ifdata);
2249 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2250 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
2255 static void destroy_listener(struct listener_data *ifdata)
2259 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2260 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
2262 for (list = request_pending_list; list; list = list->next) {
2263 struct request_data *req = list->data;
2265 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
2266 req->srcid, req->dstid);
2269 g_free(req->request);
2275 g_slist_free(request_pending_list);
2276 request_pending_list = NULL;
2278 for (list = request_list; list; list = list->next) {
2279 struct request_data *req = list->data;
2281 DBG("Dropping request (id 0x%04x -> 0x%04x)",
2282 req->srcid, req->dstid);
2285 g_free(req->request);
2291 g_slist_free(request_list);
2292 request_list = NULL;
2294 destroy_tcp_listener(ifdata);
2295 destroy_udp_listener(ifdata);
2298 int __connman_dnsproxy_add_listener(const char *interface)
2300 struct listener_data *ifdata;
2303 DBG("interface %s", interface);
2305 if (g_hash_table_lookup(listener_table, interface) != NULL)
2308 ifdata = g_try_new0(struct listener_data, 1);
2312 ifdata->ifname = g_strdup(interface);
2313 ifdata->udp_listener_channel = NULL;
2314 ifdata->udp_listener_watch = 0;
2315 ifdata->tcp_listener_channel = NULL;
2316 ifdata->tcp_listener_watch = 0;
2318 err = create_listener(ifdata);
2320 connman_error("Couldn't create listener for %s err %d",
2322 g_free(ifdata->ifname);
2326 g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
2330 void __connman_dnsproxy_remove_listener(const char *interface)
2332 struct listener_data *ifdata;
2334 DBG("interface %s", interface);
2336 ifdata = g_hash_table_lookup(listener_table, interface);
2340 destroy_listener(ifdata);
2342 g_hash_table_remove(listener_table, interface);
2345 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
2347 __connman_dnsproxy_remove_listener(key);
2350 int __connman_dnsproxy_init(void)
2356 listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2358 err = __connman_dnsproxy_add_listener("lo");
2362 err = connman_notifier_register(&dnsproxy_notifier);
2369 __connman_dnsproxy_remove_listener("lo");
2370 g_hash_table_destroy(listener_table);
2375 void __connman_dnsproxy_cleanup(void)
2379 connman_notifier_unregister(&dnsproxy_notifier);
2381 g_hash_table_foreach(listener_table, remove_listener, NULL);
2383 g_hash_table_destroy(listener_table);