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;
135 unsigned int data_len;
136 unsigned char *data; /* contains DNS header + body */
141 struct cache_data *ipv4;
142 struct cache_data *ipv6;
145 struct domain_question {
148 } __attribute__ ((packed));
155 } __attribute__ ((packed));
158 * We limit how long the cached DNS entry stays in the cache.
159 * By default the TTL (time-to-live) of the DNS response is used
160 * when setting the cache entry life time. The value is in seconds.
162 #define MAX_CACHE_TTL (60 * 30)
165 * We limit the cache size to some sane value so that cached data does
166 * not occupy too much memory. Each cached entry occupies on average
167 * about 100 bytes memory (depending on DNS name length).
168 * Example: caching www.connman.net uses 97 bytes memory.
169 * The value is the max amount of cached DNS responses (count).
171 #define MAX_CACHE_SIZE 256
173 static int cache_size;
174 static GHashTable *cache;
175 static int cache_refcount;
176 static GSList *server_list = NULL;
177 static GSList *request_list = NULL;
178 static GSList *request_pending_list = NULL;
179 static guint16 request_id = 0x0000;
180 static GHashTable *listener_table = NULL;
182 static int protocol_offset(int protocol)
198 * There is a power and efficiency benefit to have entries
199 * in our cache expire at the same time. To this extend,
200 * we round down the cache valid time to common boundaries.
202 static time_t round_down_ttl(time_t end_time, int ttl)
207 /* Less than 5 minutes, round to 10 second boundary */
209 end_time = end_time / 10;
210 end_time = end_time * 10;
211 } else { /* 5 or more minutes, round to 30 seconds */
212 end_time = end_time / 30;
213 end_time = end_time * 30;
218 static struct request_data *find_request(guint16 id)
222 for (list = request_list; list; list = list->next) {
223 struct request_data *req = list->data;
225 if (req->dstid == id || req->altid == id)
232 static struct server_data *find_server(const char *interface,
238 DBG("interface %s server %s", interface, server);
240 for (list = server_list; list; list = list->next) {
241 struct server_data *data = list->data;
243 if (interface == NULL && data->interface == NULL &&
244 g_str_equal(data->server, server) == TRUE &&
245 data->protocol == protocol)
248 if (interface == NULL ||
249 data->interface == NULL || data->server == NULL)
252 if (g_str_equal(data->interface, interface) == TRUE &&
253 g_str_equal(data->server, server) == TRUE &&
254 data->protocol == protocol)
261 static int dns_name_length(unsigned char *buf)
263 if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
265 return strlen((char *)buf);
268 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
275 /* skip the header */
279 /* skip the query, which is a name and 2 16 bit words */
280 l = dns_name_length(c);
286 /* now we get the answer records */
290 l = dns_name_length(c);
295 /* then type + class, 2 bytes each */
301 /* now the 4 byte TTL field */
309 /* now the 2 byte rdlen field */
312 len -= ntohs(*w) + 2;
318 static void send_cached_response(int sk, unsigned char *buf, int len,
319 const struct sockaddr *to, socklen_t tolen,
320 int protocol, int id, uint16_t answers, int ttl)
322 struct domain_hdr *hdr;
323 int err, offset = protocol_offset(protocol);
331 hdr = (void *) (buf + offset);
336 hdr->ancount = htons(answers);
340 update_cached_ttl(buf, len, ttl);
342 DBG("id 0x%04x answers %d", hdr->id, answers);
344 err = sendto(sk, buf, len, 0, to, tolen);
346 connman_error("Cannot send cached DNS response: %s",
352 static void send_response(int sk, unsigned char *buf, int len,
353 const struct sockaddr *to, socklen_t tolen,
356 struct domain_hdr *hdr;
357 int err, offset = protocol_offset(protocol);
367 hdr = (void *) (buf + offset);
369 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
378 err = sendto(sk, buf, len, 0, to, tolen);
380 connman_error("Failed to send DNS response: %s",
386 static gboolean request_timeout(gpointer user_data)
388 struct request_data *req = user_data;
389 struct listener_data *ifdata;
391 DBG("id 0x%04x", req->srcid);
396 ifdata = req->ifdata;
398 request_list = g_slist_remove(request_list, req);
401 if (req->resplen > 0 && req->resp != NULL) {
404 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
406 err = sendto(sk, req->resp, req->resplen, 0,
407 &req->sa, req->sa_len);
410 } else if (req->request && req->numserv == 0) {
411 struct domain_hdr *hdr;
413 if (req->protocol == IPPROTO_TCP) {
414 hdr = (void *) (req->request + 2);
415 hdr->id = req->srcid;
416 send_response(req->client_sk, req->request,
417 req->request_len, NULL, 0, IPPROTO_TCP);
419 } else if (req->protocol == IPPROTO_UDP) {
422 hdr = (void *) (req->request);
423 hdr->id = req->srcid;
424 sk = g_io_channel_unix_get_fd(
425 ifdata->udp_listener_channel);
426 send_response(sk, req->request, req->request_len,
427 &req->sa, req->sa_len, IPPROTO_UDP);
437 static int append_query(unsigned char *buf, unsigned int size,
438 const char *query, const char *domain)
440 unsigned char *ptr = buf;
444 DBG("query %s domain %s", query, domain);
446 offset = (char *) query;
447 while (offset != NULL) {
450 tmp = strchr(offset, '.');
452 len = strlen(offset);
456 memcpy(ptr + 1, offset, len);
462 memcpy(ptr + 1, offset, tmp - offset);
463 ptr += tmp - offset + 1;
468 offset = (char *) domain;
469 while (offset != NULL) {
472 tmp = strchr(offset, '.');
474 len = strlen(offset);
478 memcpy(ptr + 1, offset, len);
484 memcpy(ptr + 1, offset, tmp - offset);
485 ptr += tmp - offset + 1;
495 static gboolean cache_check_is_valid(struct cache_data *data,
501 if (data->cache_until < current_time)
508 * remove stale cached entries so that they can be refreshed
510 static void cache_enforce_validity(struct cache_entry *entry)
512 time_t current_time = time(0);
514 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE
516 DBG("cache timeout \"%s\" type A", entry->key);
517 g_free(entry->ipv4->data);
523 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE
525 DBG("cache timeout \"%s\" type AAAA", entry->key);
526 g_free(entry->ipv6->data);
533 static uint16_t cache_check_validity(char *question, uint16_t type,
534 struct cache_entry *entry)
536 time_t current_time = time(0);
538 cache_enforce_validity(entry);
542 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE) {
543 DBG("cache %s \"%s\" type A", entry->ipv4 ?
544 "timeout" : "entry missing", question);
547 * We do not remove cache entry if there is still
548 * valid IPv6 entry found in the cache.
550 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE)
551 g_hash_table_remove(cache, question);
558 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE) {
559 DBG("cache %s \"%s\" type AAAA", entry->ipv6 ?
560 "timeout" : "entry missing", question);
562 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE)
563 g_hash_table_remove(cache, question);
573 static struct cache_entry *cache_check(gpointer request, int *qtype)
575 char *question = request + 12;
576 struct cache_entry *entry;
577 struct domain_question *q;
581 offset = strlen(question) + 1;
582 q = (void *) (question + offset);
583 type = ntohs(q->type);
585 /* We only cache either A (1) or AAAA (28) requests */
586 if (type != 1 && type != 28)
589 entry = g_hash_table_lookup(cache, question);
593 type = cache_check_validity(question, type, entry);
602 * Get a label/name from DNS resource record. The function decompresses the
603 * label if necessary. The function does not convert the name to presentation
604 * form. This means that the result string will contain label lengths instead
605 * of dots between labels. We intentionally do not want to convert to dotted
606 * format so that we can cache the wire format string directly.
608 static int get_name(int counter,
609 unsigned char *pkt, unsigned char *start, unsigned char *max,
610 unsigned char *output, int output_max, int *output_len,
611 unsigned char **end, char *name, int *name_len)
615 /* Limit recursion to 10 (this means up to 10 labels in domain name) */
621 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
622 uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
624 if (offset >= max - pkt)
630 return get_name(counter + 1, pkt, pkt + offset, max,
631 output, output_max, output_len, end,
634 unsigned label_len = *p;
636 if (pkt + label_len > max)
639 if (*output_len > output_max)
643 * We need the original name in order to check
644 * if this answer is the correct one.
646 name[(*name_len)++] = label_len;
647 memcpy(name + *name_len, p + 1, label_len + 1);
648 *name_len += label_len;
650 /* We compress the result */
651 output[0] = NS_CMPRSFLGS;
668 static int parse_rr(unsigned char *buf, unsigned char *start,
670 unsigned char *response, unsigned int *response_size,
671 uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
675 struct domain_rr *rr;
677 int name_len = 0, output_len = 0, max_rsp = *response_size;
679 err = get_name(0, buf, start, max, response, max_rsp,
680 &output_len, end, name, &name_len);
686 if ((unsigned int) offset > *response_size)
689 rr = (void *) (*end);
694 *type = ntohs(rr->type);
695 *class = ntohs(rr->class);
696 *ttl = ntohl(rr->ttl);
697 *rdlen = ntohs(rr->rdlen);
702 memcpy(response + offset, *end, sizeof(struct domain_rr));
704 offset += sizeof(struct domain_rr);
705 *end += sizeof(struct domain_rr);
707 if ((unsigned int) (offset + *rdlen) > *response_size)
710 memcpy(response + offset, *end, *rdlen);
714 *response_size = offset + *rdlen;
719 static gboolean check_alias(GSList *aliases, char *name)
723 if (aliases != NULL) {
724 for (list = aliases; list; list = list->next) {
725 int len = strlen((char *)list->data);
726 if (strncmp((char *)list->data, name, len) == 0)
734 static int parse_response(unsigned char *buf, int buflen,
735 char *question, int qlen,
736 uint16_t *type, uint16_t *class, int *ttl,
737 unsigned char *response, unsigned int *response_len,
740 struct domain_hdr *hdr = (void *) buf;
741 struct domain_question *q;
743 uint16_t qdcount = ntohs(hdr->qdcount);
744 uint16_t ancount = ntohs(hdr->ancount);
746 uint16_t qtype, qclass;
747 unsigned char *next = NULL;
748 unsigned int maxlen = *response_len;
749 GSList *aliases = NULL, *list;
750 char name[NS_MAXDNAME + 1];
755 DBG("qr %d qdcount %d", hdr->qr, qdcount);
757 /* We currently only cache responses where question count is 1 */
758 if (hdr->qr != 1 || qdcount != 1)
761 ptr = buf + sizeof(struct domain_hdr);
763 strncpy(question, (char *) ptr, qlen);
764 qlen = strlen(question);
765 ptr += qlen + 1; /* skip \0 */
768 qtype = ntohs(q->type);
770 /* We cache only A and AAAA records */
771 if (qtype != 1 && qtype != 28)
774 qclass = ntohs(q->class);
776 ptr += 2 + 2; /* ptr points now to answers */
783 * We have a bunch of answers (like A, AAAA, CNAME etc) to
784 * A or AAAA question. We traverse the answers and parse the
785 * resource records. Only A and AAAA records are cached, all
786 * the other records in answers are skipped.
788 for (i = 0; i < ancount; i++) {
790 * Get one address at a time to this buffer.
791 * The max size of the answer is
792 * 2 (pointer) + 2 (type) + 2 (class) +
793 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
794 * for A or AAAA record.
795 * For CNAME the size can be bigger.
797 unsigned char rsp[NS_MAXCDNAME];
798 unsigned int rsp_len = sizeof(rsp) - 1;
801 memset(rsp, 0, sizeof(rsp));
803 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
804 type, class, ttl, &rdlen, &next, name);
811 * Now rsp contains compressed or uncompressed resource
812 * record. Next we check if this record answers the question.
813 * The name var contains the uncompressed label.
814 * One tricky bit is the CNAME records as they alias
815 * the name we might be interested in.
819 * Go to next answer if the class is not the one we are
822 if (*class != qclass) {
829 * Try to resolve aliases also, type is CNAME(5).
830 * This is important as otherwise the aliased names would not
831 * be cached at all as the cache would not contain the aliased
834 * If any CNAME is found in DNS packet, then we cache the alias
835 * IP address instead of the question (as the server
836 * said that question has only an alias).
837 * This means in practice that if e.g., ipv6.google.com is
838 * queried, DNS server returns CNAME of that name which is
839 * ipv6.l.google.com. We then cache the address of the CNAME
840 * but return the question name to client. So the alias
841 * status of the name is not saved in cache and thus not
842 * returned to the client. We do not return DNS packets from
843 * cache to client saying that ipv6.google.com is an alias to
844 * ipv6.l.google.com but we return instead a DNS packet that
845 * says ipv6.google.com has address xxx which is in fact the
846 * address of ipv6.l.google.com. For caching purposes this
847 * should not cause any issues.
849 if (*type == 5 && strncmp(question, name, qlen) == 0) {
851 * So now the alias answered the question. This is
852 * not very useful from caching point of view as
853 * the following A or AAAA records will not match the
854 * question. We need to find the real A/AAAA record
855 * of the alias and cache that.
857 unsigned char *end = NULL;
858 int name_len = 0, output_len;
860 memset(rsp, 0, sizeof(rsp));
861 rsp_len = sizeof(rsp) - 1;
864 * Alias is in rdata part of the message,
865 * and next-rdlen points to it. So we need to get
866 * the real name of the alias.
868 ret = get_name(0, buf, next - rdlen, buf + buflen,
869 rsp, rsp_len, &output_len, &end,
872 /* just ignore the error at this point */
879 * We should now have the alias of the entry we might
880 * want to cache. Just remember it for a while.
881 * We check the alias list when we have parsed the
884 aliases = g_slist_prepend(aliases, g_strdup(name));
891 if (*type == qtype) {
893 * We found correct type (A or AAAA)
895 if (check_alias(aliases, name) == TRUE ||
896 (aliases == NULL && strncmp(question, name,
899 * We found an alias or the name of the rr
900 * matches the question. If so, we append
901 * the compressed label to the cache.
902 * The end result is a response buffer that
903 * will contain one or more cached and
904 * compressed resource records.
906 if (*response_len + rsp_len > maxlen) {
910 memcpy(response + *response_len, rsp, rsp_len);
911 *response_len += rsp_len;
922 for (list = aliases; list; list = list->next)
924 g_slist_free(aliases);
929 struct cache_timeout {
934 static gboolean cache_check_entry(gpointer key, gpointer value,
937 struct cache_timeout *data = user_data;
938 struct cache_entry *entry = value;
942 * If either IPv4 or IPv6 cached entry has expired, we
943 * remove both from the cache.
946 if (entry->ipv4 != NULL && entry->ipv4->timeout > 0) {
947 max_timeout = entry->ipv4->cache_until;
948 if (max_timeout > data->max_timeout)
949 data->max_timeout = max_timeout;
951 if (entry->ipv4->cache_until < data->current_time)
955 if (entry->ipv6 != NULL && entry->ipv6->timeout > 0) {
956 max_timeout = entry->ipv6->cache_until;
957 if (max_timeout > data->max_timeout)
958 data->max_timeout = max_timeout;
960 if (entry->ipv6->cache_until < data->current_time)
967 static void cache_cleanup(void)
969 static int max_timeout;
970 struct cache_timeout data;
973 data.current_time = time(0);
974 data.max_timeout = 0;
976 if (max_timeout > data.current_time) {
977 DBG("waiting %ld secs before cleaning cache",
978 max_timeout - data.current_time);
982 count = g_hash_table_foreach_remove(cache, cache_check_entry,
984 DBG("removed %d", count);
988 * If we could not remove anything, then remember
989 * what is the max timeout and do nothing if we
990 * have not yet reached it. This will prevent
991 * constant traversal of the cache if it is full.
993 max_timeout = data.max_timeout;
998 static int cache_update(struct server_data *srv, unsigned char *msg,
999 unsigned int msg_len)
1001 int offset = protocol_offset(srv->protocol);
1002 int err, qlen, ttl = 0;
1003 uint16_t answers, type = 0, class = 0;
1004 struct domain_question *q;
1005 struct cache_entry *entry;
1006 struct cache_data *data;
1007 char question[NS_MAXDNAME + 1];
1008 unsigned char response[NS_MAXDNAME + 1];
1010 unsigned int rsplen;
1011 gboolean new_entry = TRUE;
1012 time_t current_time;
1014 if (cache_size >= MAX_CACHE_SIZE) {
1016 if (cache_size >= MAX_CACHE_SIZE)
1020 /* Continue only if response code is 0 (=ok) */
1027 rsplen = sizeof(response) - 1;
1028 question[sizeof(question) - 1] = '\0';
1030 err = parse_response(msg + offset, msg_len - offset,
1031 question, sizeof(question) - 1,
1032 &type, &class, &ttl,
1033 response, &rsplen, &answers);
1034 if (err < 0 || ttl == 0)
1037 qlen = strlen(question);
1038 current_time = time(0);
1041 * If the cache contains already data, check if the
1042 * type of the cached data is the same and do not add
1043 * to cache if data is already there.
1044 * This is needed so that we can cache both A and AAAA
1045 * records for the same name.
1047 entry = g_hash_table_lookup(cache, question);
1048 if (entry == NULL) {
1049 entry = g_try_new(struct cache_entry, 1);
1053 data = g_try_new(struct cache_data, 1);
1059 entry->key = g_strdup(question);
1060 entry->ipv4 = entry->ipv6 = NULL;
1067 if (type == 1 && entry->ipv4 != NULL)
1070 if (type == 28 && entry->ipv6 != NULL)
1073 data = g_try_new(struct cache_data, 1);
1085 data->inserted = current_time;
1087 data->answers = answers;
1088 data->timeout = ttl;
1089 data->data_len = 12 + qlen + 1 + 2 + 2 + rsplen;
1090 data->data = ptr = g_malloc(data->data_len);
1091 data->valid_until = current_time + ttl;
1094 * Restrict the cached DNS record TTL to some sane value
1095 * in order to prevent data staying in the cache too long.
1097 if (ttl > MAX_CACHE_TTL)
1098 ttl = MAX_CACHE_TTL;
1100 data->cache_until = round_down_ttl(current_time + ttl, ttl);
1102 if (data->data == NULL) {
1109 memcpy(ptr, msg, 12);
1110 memcpy(ptr + 12, question, qlen + 1); /* copy also the \0 */
1112 q = (void *) (ptr + 12 + qlen + 1);
1113 q->type = htons(type);
1114 q->class = htons(class);
1115 memcpy(ptr + 12 + qlen + 1 + sizeof(struct domain_question),
1118 if (new_entry == TRUE) {
1119 g_hash_table_replace(cache, entry->key, entry);
1123 DBG("cache %d %squestion \"%s\" type %d ttl %d size %zd",
1124 cache_size, new_entry ? "new " : "old ",
1125 question, type, ttl,
1126 sizeof(*entry) + sizeof(*data) + data->data_len + qlen);
1131 static int ns_resolv(struct server_data *server, struct request_data *req,
1132 gpointer request, gpointer name)
1135 int sk, err, type = 0;
1136 char *dot, *lookup = (char *) name;
1137 struct cache_entry *entry;
1139 entry = cache_check(request, &type);
1140 if (entry != NULL) {
1142 struct cache_data *data;
1144 DBG("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1151 ttl_left = data->valid_until - time(0);
1153 if (data != NULL && req->protocol == IPPROTO_TCP) {
1154 send_cached_response(req->client_sk, data->data,
1155 data->data_len, NULL, 0, IPPROTO_TCP,
1156 req->srcid, data->answers, ttl_left);
1160 if (data != NULL && req->protocol == IPPROTO_UDP) {
1162 sk = g_io_channel_unix_get_fd(
1163 req->ifdata->udp_listener_channel);
1165 send_cached_response(sk, data->data,
1166 data->data_len, &req->sa, req->sa_len,
1167 IPPROTO_UDP, req->srcid, data->answers,
1173 sk = g_io_channel_unix_get_fd(server->channel);
1175 err = send(sk, request, req->request_len, 0);
1179 /* If we have more than one dot, we don't add domains */
1180 dot = strchr(lookup, '.');
1181 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
1184 if (server->domains != NULL && server->domains->data != NULL)
1185 req->append_domain = TRUE;
1187 for (list = server->domains; list; list = list->next) {
1189 unsigned char alt[1024];
1190 struct domain_hdr *hdr = (void *) &alt;
1191 int altlen, domlen, offset;
1193 domain = list->data;
1198 offset = protocol_offset(server->protocol);
1202 domlen = strlen(domain) + 1;
1206 alt[offset] = req->altid & 0xff;
1207 alt[offset + 1] = req->altid >> 8;
1209 memcpy(alt + offset + 2, request + offset + 2, 10);
1210 hdr->qdcount = htons(1);
1212 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1219 memcpy(alt + offset + altlen,
1220 request + offset + altlen - domlen,
1221 req->request_len - altlen - offset + domlen);
1223 if (server->protocol == IPPROTO_TCP) {
1224 int req_len = req->request_len + domlen - 2;
1226 alt[0] = (req_len >> 8) & 0xff;
1227 alt[1] = req_len & 0xff;
1230 err = send(sk, alt, req->request_len + domlen, 0);
1240 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1241 struct server_data *data)
1243 struct domain_hdr *hdr;
1244 struct request_data *req;
1245 int dns_id, sk, err, offset = protocol_offset(protocol);
1246 struct listener_data *ifdata;
1251 hdr = (void *)(reply + offset);
1252 dns_id = reply[offset] | reply[offset + 1] << 8;
1254 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1256 req = find_request(dns_id);
1260 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
1262 ifdata = req->ifdata;
1264 reply[offset] = req->srcid & 0xff;
1265 reply[offset + 1] = req->srcid >> 8;
1269 if (hdr->rcode == 0 || req->resp == NULL) {
1272 * If the domain name was append
1273 * remove it before forwarding the reply.
1275 if (req->append_domain == TRUE) {
1278 unsigned int domain_len;
1281 * ptr points to the first char of the hostname.
1282 * ->hostname.domain.net
1284 ptr = reply + offset + sizeof(struct domain_hdr);
1286 domain_len = strlen((const char *)ptr) - host_len - 1;
1289 * remove the domain name and replaced it by the end
1292 memmove(ptr + host_len + 1,
1293 ptr + host_len + domain_len + 1,
1294 reply_len - (ptr - reply + domain_len));
1296 reply_len = reply_len - domain_len;
1302 req->resp = g_try_malloc(reply_len);
1303 if (req->resp == NULL)
1306 memcpy(req->resp, reply, reply_len);
1307 req->resplen = reply_len;
1309 cache_update(data, reply, reply_len);
1312 if (hdr->rcode > 0 && req->numresp < req->numserv)
1315 if (req->timeout > 0)
1316 g_source_remove(req->timeout);
1318 request_list = g_slist_remove(request_list, req);
1320 if (protocol == IPPROTO_UDP) {
1321 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
1322 err = sendto(sk, req->resp, req->resplen, 0,
1323 &req->sa, req->sa_len);
1325 sk = req->client_sk;
1326 err = send(sk, req->resp, req->resplen, 0);
1336 static void cache_element_destroy(gpointer value)
1338 struct cache_entry *entry = value;
1343 if (entry->ipv4 != NULL) {
1344 g_free(entry->ipv4->data);
1345 g_free(entry->ipv4);
1348 if (entry->ipv6 != NULL) {
1349 g_free(entry->ipv6->data);
1350 g_free(entry->ipv6);
1356 if (--cache_size < 0)
1360 static void destroy_server(struct server_data *server)
1364 DBG("interface %s server %s", server->interface, server->server);
1366 server_list = g_slist_remove(server_list, server);
1368 if (server->watch > 0)
1369 g_source_remove(server->watch);
1371 if (server->timeout > 0)
1372 g_source_remove(server->timeout);
1374 g_io_channel_unref(server->channel);
1376 if (server->protocol == IPPROTO_UDP)
1377 connman_info("Removing DNS server %s", server->server);
1379 g_free(server->incoming_reply);
1380 g_free(server->server);
1381 for (list = server->domains; list; list = list->next) {
1382 char *domain = list->data;
1384 server->domains = g_list_remove(server->domains, domain);
1387 g_free(server->interface);
1389 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1)
1390 g_hash_table_destroy(cache);
1395 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
1398 unsigned char buf[4096];
1400 struct server_data *data = user_data;
1402 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1403 connman_error("Error with UDP server %s", data->server);
1408 sk = g_io_channel_unix_get_fd(channel);
1410 len = recv(sk, buf, sizeof(buf), 0);
1414 err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
1421 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
1425 struct server_data *server = user_data;
1427 sk = g_io_channel_unix_get_fd(channel);
1431 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1434 DBG("TCP server channel closed");
1437 * Discard any partial response which is buffered; better
1438 * to get a proper response from a working server.
1440 g_free(server->incoming_reply);
1441 server->incoming_reply = NULL;
1443 for (list = request_list; list; list = list->next) {
1444 struct request_data *req = list->data;
1445 struct domain_hdr *hdr;
1447 if (req->protocol == IPPROTO_UDP)
1450 if (req->request == NULL)
1454 * If we're not waiting for any further response
1455 * from another name server, then we send an error
1456 * response to the client.
1458 if (req->numserv && --(req->numserv))
1461 hdr = (void *) (req->request + 2);
1462 hdr->id = req->srcid;
1463 send_response(req->client_sk, req->request,
1464 req->request_len, NULL, 0, IPPROTO_TCP);
1466 request_list = g_slist_remove(request_list, req);
1469 destroy_server(server);
1474 if ((condition & G_IO_OUT) && !server->connected) {
1477 struct server_data *udp_server;
1479 udp_server = find_server(server->interface, server->server,
1481 if (udp_server != NULL) {
1482 for (domains = udp_server->domains; domains;
1483 domains = domains->next) {
1484 char *dom = domains->data;
1486 DBG("Adding domain %s to %s",
1487 dom, server->server);
1489 server->domains = g_list_append(server->domains,
1494 server->connected = TRUE;
1495 server_list = g_slist_append(server_list, server);
1497 if (server->timeout > 0) {
1498 g_source_remove(server->timeout);
1499 server->timeout = 0;
1502 for (list = request_list; list; list = list->next) {
1503 struct request_data *req = list->data;
1505 if (req->protocol == IPPROTO_UDP)
1508 DBG("Sending req %s over TCP", (char *)req->name);
1510 if (req->timeout > 0)
1511 g_source_remove(req->timeout);
1513 req->timeout = g_timeout_add_seconds(30,
1514 request_timeout, req);
1515 if (ns_resolv(server, req, req->request,
1517 /* We sent cached result so no need for timeout
1520 if (req->timeout > 0) {
1521 g_source_remove(req->timeout);
1527 } else if (condition & G_IO_IN) {
1528 struct partial_reply *reply = server->incoming_reply;
1532 unsigned char reply_len_buf[2];
1535 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
1538 } else if (bytes_recv < 0) {
1539 if (errno == EAGAIN || errno == EWOULDBLOCK)
1542 connman_error("DNS proxy error %s",
1545 } else if (bytes_recv < 2)
1548 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
1551 DBG("TCP reply %d bytes", reply_len);
1553 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
1557 reply->len = reply_len;
1558 reply->received = 0;
1560 server->incoming_reply = reply;
1563 while (reply->received < reply->len) {
1564 bytes_recv = recv(sk, reply->buf + reply->received,
1565 reply->len - reply->received, 0);
1567 connman_error("DNS proxy TCP disconnect");
1569 } else if (bytes_recv < 0) {
1570 if (errno == EAGAIN || errno == EWOULDBLOCK)
1573 connman_error("DNS proxy error %s",
1577 reply->received += bytes_recv;
1580 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
1584 server->incoming_reply = NULL;
1586 destroy_server(server);
1594 static gboolean tcp_idle_timeout(gpointer user_data)
1596 struct server_data *server = user_data;
1603 destroy_server(server);
1608 static struct server_data *create_server(const char *interface,
1609 const char *domain, const char *server,
1612 struct addrinfo hints, *rp;
1613 struct server_data *data;
1616 DBG("interface %s server %s", interface, server);
1618 memset(&hints, 0, sizeof(hints));
1622 hints.ai_socktype = SOCK_DGRAM;
1626 hints.ai_socktype = SOCK_STREAM;
1632 hints.ai_family = AF_UNSPEC;
1633 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
1635 ret = getaddrinfo(server, "53", &hints, &rp);
1637 connman_error("Failed to parse server %s address: %s\n",
1638 server, gai_strerror(ret));
1641 /* Do not blindly copy this code elsewhere; it doesn't loop over the
1642 results using ->ai_next as it should. That's OK in *this* case
1643 because it was a numeric lookup; we *know* there's only one. */
1645 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1647 connman_error("Failed to create server %s socket", server);
1652 if (interface != NULL) {
1653 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1654 interface, strlen(interface) + 1) < 0) {
1655 connman_error("Failed to bind server %s "
1664 data = g_try_new0(struct server_data, 1);
1666 connman_error("Failed to allocate server %s data", server);
1672 data->channel = g_io_channel_unix_new(sk);
1673 if (data->channel == NULL) {
1674 connman_error("Failed to create server %s channel", server);
1681 g_io_channel_set_close_on_unref(data->channel, TRUE);
1683 if (protocol == IPPROTO_TCP) {
1684 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
1685 data->watch = g_io_add_watch(data->channel,
1686 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1687 tcp_server_event, data);
1688 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
1691 data->watch = g_io_add_watch(data->channel,
1692 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1693 udp_server_event, data);
1695 data->interface = g_strdup(interface);
1697 data->domains = g_list_append(data->domains, g_strdup(domain));
1698 data->server = g_strdup(server);
1699 data->protocol = protocol;
1701 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
1704 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
1705 protocol == IPPROTO_UDP) {
1708 connman_error("Failed to connect to server %s", server);
1709 if (data->watch > 0)
1710 g_source_remove(data->watch);
1711 if (data->timeout > 0)
1712 g_source_remove(data->timeout);
1714 g_io_channel_unref(data->channel);
1717 g_free(data->server);
1718 g_free(data->interface);
1719 for (list = data->domains; list; list = list->next) {
1720 char *domain = list->data;
1722 data->domains = g_list_remove(data->domains,
1731 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
1732 cache = g_hash_table_new_full(g_str_hash,
1735 cache_element_destroy);
1737 if (protocol == IPPROTO_UDP) {
1738 /* Enable new servers by default */
1739 data->enabled = TRUE;
1740 connman_info("Adding DNS server %s", data->server);
1742 server_list = g_slist_append(server_list, data);
1750 static gboolean resolv(struct request_data *req,
1751 gpointer request, gpointer name)
1756 for (list = server_list; list; list = list->next) {
1757 struct server_data *data = list->data;
1759 DBG("server %s enabled %d", data->server, data->enabled);
1761 if (data->enabled == FALSE)
1764 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
1765 data->watch = g_io_add_watch(data->channel,
1766 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1767 udp_server_event, data);
1769 status = ns_resolv(data, req, request, name);
1774 if (req->timeout > 0) {
1775 g_source_remove(req->timeout);
1784 static void append_domain(const char *interface, const char *domain)
1788 DBG("interface %s domain %s", interface, domain);
1793 for (list = server_list; list; list = list->next) {
1794 struct server_data *data = list->data;
1797 gboolean dom_found = FALSE;
1799 if (data->interface == NULL)
1802 if (g_str_equal(data->interface, interface) == FALSE)
1805 for (dom_list = data->domains; dom_list;
1806 dom_list = dom_list->next) {
1807 dom = dom_list->data;
1809 if (g_str_equal(dom, domain)) {
1815 if (dom_found == FALSE) {
1817 g_list_append(data->domains, g_strdup(domain));
1822 int __connman_dnsproxy_append(const char *interface, const char *domain,
1825 struct server_data *data;
1827 DBG("interface %s server %s", interface, server);
1829 if (server == NULL && domain == NULL)
1832 if (server == NULL) {
1833 append_domain(interface, domain);
1838 if (g_str_equal(server, "127.0.0.1") == TRUE)
1841 data = find_server(interface, server, IPPROTO_UDP);
1843 append_domain(interface, domain);
1847 data = create_server(interface, domain, server, IPPROTO_UDP);
1854 static void remove_server(const char *interface, const char *domain,
1855 const char *server, int protocol)
1857 struct server_data *data;
1859 data = find_server(interface, server, protocol);
1863 destroy_server(data);
1866 int __connman_dnsproxy_remove(const char *interface, const char *domain,
1869 DBG("interface %s server %s", interface, server);
1874 if (g_str_equal(server, "127.0.0.1") == TRUE)
1877 remove_server(interface, domain, server, IPPROTO_UDP);
1878 remove_server(interface, domain, server, IPPROTO_TCP);
1883 void __connman_dnsproxy_flush(void)
1887 list = request_pending_list;
1889 struct request_data *req = list->data;
1893 request_pending_list =
1894 g_slist_remove(request_pending_list, req);
1895 resolv(req, req->request, req->name);
1896 g_free(req->request);
1901 static void dnsproxy_offline_mode(connman_bool_t enabled)
1905 DBG("enabled %d", enabled);
1907 for (list = server_list; list; list = list->next) {
1908 struct server_data *data = list->data;
1910 if (enabled == FALSE) {
1911 connman_info("Enabling DNS server %s", data->server);
1912 data->enabled = TRUE;
1914 connman_info("Disabling DNS server %s", data->server);
1915 data->enabled = FALSE;
1920 static void dnsproxy_default_changed(struct connman_service *service)
1925 DBG("service %p", service);
1927 if (service == NULL) {
1928 /* When no services are active, then disable DNS proxying */
1929 dnsproxy_offline_mode(TRUE);
1933 interface = connman_service_get_interface(service);
1934 if (interface == NULL)
1937 for (list = server_list; list; list = list->next) {
1938 struct server_data *data = list->data;
1940 if (g_strcmp0(data->interface, interface) == 0) {
1941 connman_info("Enabling DNS server %s", data->server);
1942 data->enabled = TRUE;
1944 connman_info("Disabling DNS server %s", data->server);
1945 data->enabled = FALSE;
1952 static struct connman_notifier dnsproxy_notifier = {
1954 .default_changed = dnsproxy_default_changed,
1955 .offline_mode = dnsproxy_offline_mode,
1958 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
1960 static int parse_request(unsigned char *buf, int len,
1961 char *name, unsigned int size)
1963 struct domain_hdr *hdr = (void *) buf;
1964 uint16_t qdcount = ntohs(hdr->qdcount);
1965 uint16_t arcount = ntohs(hdr->arcount);
1967 char *last_label = NULL;
1968 unsigned int remain, used = 0;
1973 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
1974 hdr->id, hdr->qr, hdr->opcode,
1977 if (hdr->qr != 0 || qdcount != 1)
1980 memset(name, 0, size);
1982 ptr = buf + sizeof(struct domain_hdr);
1983 remain = len - sizeof(struct domain_hdr);
1985 while (remain > 0) {
1989 last_label = (char *) (ptr + 1);
1993 if (used + len + 1 > size)
1996 strncat(name, (char *) (ptr + 1), len);
2005 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
2006 !memcmp(last_label + 5, opt_edns0_type, 2)) {
2007 uint16_t edns0_bufsize;
2009 edns0_bufsize = last_label[7] << 8 | last_label[8];
2011 DBG("EDNS0 buffer size %u", edns0_bufsize);
2013 /* This is an evil hack until full TCP support has been
2016 * Somtimes the EDNS0 request gets send with a too-small
2017 * buffer size. Since glibc doesn't seem to crash when it
2018 * gets a response biffer then it requested, just bump
2019 * the buffer size up to 4KiB.
2021 if (edns0_bufsize < 0x1000) {
2022 last_label[7] = 0x10;
2023 last_label[8] = 0x00;
2027 DBG("query %s", name);
2032 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
2035 unsigned char buf[768];
2037 struct request_data *req;
2038 struct server_data *server;
2039 int sk, client_sk, len, err;
2040 struct sockaddr_in6 client_addr;
2041 socklen_t client_addr_len = sizeof(client_addr);
2043 struct listener_data *ifdata = user_data;
2045 DBG("condition 0x%x", condition);
2047 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2048 if (ifdata->tcp_listener_watch > 0)
2049 g_source_remove(ifdata->tcp_listener_watch);
2050 ifdata->tcp_listener_watch = 0;
2052 connman_error("Error with TCP listener channel");
2057 sk = g_io_channel_unix_get_fd(channel);
2059 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
2060 if (client_sk < 0) {
2061 connman_error("Accept failure on TCP listener");
2062 ifdata->tcp_listener_watch = 0;
2066 len = recv(client_sk, buf, sizeof(buf), 0);
2070 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
2072 err = parse_request(buf + 2, len - 2, query, sizeof(query));
2073 if (err < 0 || (g_slist_length(server_list) == 0)) {
2074 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
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;
2084 req->client_sk = client_sk;
2085 req->protocol = IPPROTO_TCP;
2088 if (request_id == 0x0000 || request_id == 0xffff)
2091 req->srcid = buf[2] | (buf[3] << 8);
2092 req->dstid = request_id;
2093 req->altid = request_id + 1;
2094 req->request_len = len;
2096 buf[2] = req->dstid & 0xff;
2097 buf[3] = req->dstid >> 8;
2100 req->ifdata = (struct listener_data *) ifdata;
2101 req->append_domain = FALSE;
2102 request_list = g_slist_append(request_list, req);
2104 for (list = server_list; list; list = list->next) {
2105 struct server_data *data = list->data;
2108 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
2111 server = create_server(data->interface, NULL,
2112 data->server, IPPROTO_TCP);
2115 * If server is NULL, we're not connected yet.
2116 * Copy the relevant buffers and continue with
2117 * the next nameserver.
2118 * The request will actually be sent once we're
2119 * properly connected over TCP to this nameserver.
2121 if (server == NULL) {
2122 req->request = g_try_malloc0(req->request_len);
2123 if (req->request == NULL)
2126 memcpy(req->request, buf, req->request_len);
2128 req->name = g_try_malloc0(sizeof(query));
2129 if (req->name == NULL) {
2130 g_free(req->request);
2133 memcpy(req->name, query, sizeof(query));
2138 if (req->timeout > 0)
2139 g_source_remove(req->timeout);
2141 for (domains = data->domains; domains;
2142 domains = domains->next) {
2143 char *dom = domains->data;
2145 DBG("Adding domain %s to %s", dom, server->server);
2147 server->domains = g_list_append(server->domains,
2151 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
2152 if (ns_resolv(server, req, buf, query) > 0) {
2153 if (req->timeout > 0) {
2154 g_source_remove(req->timeout);
2163 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
2166 unsigned char buf[768];
2168 struct request_data *req;
2169 struct sockaddr_in6 client_addr;
2170 socklen_t client_addr_len = sizeof(client_addr);
2172 struct listener_data *ifdata = user_data;
2174 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2175 connman_error("Error with UDP listener channel");
2176 ifdata->udp_listener_watch = 0;
2180 sk = g_io_channel_unix_get_fd(channel);
2182 memset(&client_addr, 0, client_addr_len);
2183 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
2188 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
2190 err = parse_request(buf, len, query, sizeof(query));
2191 if (err < 0 || (g_slist_length(server_list) == 0)) {
2192 send_response(sk, buf, len, (void *)&client_addr,
2193 client_addr_len, IPPROTO_UDP);
2197 req = g_try_new0(struct request_data, 1);
2201 memcpy(&req->sa, &client_addr, client_addr_len);
2202 req->sa_len = client_addr_len;
2204 req->protocol = IPPROTO_UDP;
2207 if (request_id == 0x0000 || request_id == 0xffff)
2210 req->srcid = buf[0] | (buf[1] << 8);
2211 req->dstid = request_id;
2212 req->altid = request_id + 1;
2213 req->request_len = len;
2215 buf[0] = req->dstid & 0xff;
2216 buf[1] = req->dstid >> 8;
2219 req->ifdata = (struct listener_data *) ifdata;
2220 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2221 req->append_domain = FALSE;
2222 request_list = g_slist_append(request_list, req);
2224 return resolv(req, buf, query);
2227 static int create_dns_listener(int protocol, struct listener_data *ifdata)
2229 GIOChannel *channel;
2233 struct sockaddr_in6 sin6;
2234 struct sockaddr_in sin;
2237 int sk, type, v6only = 0;
2238 int family = AF_INET6;
2241 DBG("interface %s", ifdata->ifname);
2246 type = SOCK_DGRAM | SOCK_CLOEXEC;
2251 type = SOCK_STREAM | SOCK_CLOEXEC;
2258 sk = socket(family, type, protocol);
2259 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
2260 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
2262 sk = socket(family, type, protocol);
2265 connman_error("Failed to create %s listener socket", proto);
2269 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2271 strlen(ifdata->ifname) + 1) < 0) {
2272 connman_error("Failed to bind %s listener interface", proto);
2276 /* Ensure it accepts Legacy IP connections too */
2277 if (family == AF_INET6 &&
2278 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
2279 &v6only, sizeof(v6only)) < 0) {
2280 connman_error("Failed to clear V6ONLY on %s listener socket",
2286 if (family == AF_INET) {
2287 memset(&s.sin, 0, sizeof(s.sin));
2288 s.sin.sin_family = AF_INET;
2289 s.sin.sin_port = htons(53);
2290 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
2291 slen = sizeof(s.sin);
2293 memset(&s.sin6, 0, sizeof(s.sin6));
2294 s.sin6.sin6_family = AF_INET6;
2295 s.sin6.sin6_port = htons(53);
2296 s.sin6.sin6_addr = in6addr_any;
2297 slen = sizeof(s.sin6);
2300 if (bind(sk, &s.sa, slen) < 0) {
2301 connman_error("Failed to bind %s listener socket", proto);
2306 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
2307 connman_error("Failed to listen on TCP socket");
2312 channel = g_io_channel_unix_new(sk);
2313 if (channel == NULL) {
2314 connman_error("Failed to create %s listener channel", proto);
2319 g_io_channel_set_close_on_unref(channel, TRUE);
2321 if (protocol == IPPROTO_TCP) {
2322 ifdata->tcp_listener_channel = channel;
2323 ifdata->tcp_listener_watch = g_io_add_watch(channel,
2324 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
2326 ifdata->udp_listener_channel = channel;
2327 ifdata->udp_listener_watch = g_io_add_watch(channel,
2328 G_IO_IN, udp_listener_event, (gpointer) ifdata);
2334 static void destroy_udp_listener(struct listener_data *ifdata)
2336 DBG("interface %s", ifdata->ifname);
2338 if (ifdata->udp_listener_watch > 0)
2339 g_source_remove(ifdata->udp_listener_watch);
2341 g_io_channel_unref(ifdata->udp_listener_channel);
2344 static void destroy_tcp_listener(struct listener_data *ifdata)
2346 DBG("interface %s", ifdata->ifname);
2348 if (ifdata->tcp_listener_watch > 0)
2349 g_source_remove(ifdata->tcp_listener_watch);
2351 g_io_channel_unref(ifdata->tcp_listener_channel);
2354 static int create_listener(struct listener_data *ifdata)
2358 err = create_dns_listener(IPPROTO_UDP, ifdata);
2362 err = create_dns_listener(IPPROTO_TCP, ifdata);
2364 destroy_udp_listener(ifdata);
2368 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2369 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
2374 static void destroy_listener(struct listener_data *ifdata)
2378 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2379 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
2381 for (list = request_pending_list; list; list = list->next) {
2382 struct request_data *req = list->data;
2384 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
2385 req->srcid, req->dstid);
2388 g_free(req->request);
2394 g_slist_free(request_pending_list);
2395 request_pending_list = NULL;
2397 for (list = request_list; list; list = list->next) {
2398 struct request_data *req = list->data;
2400 DBG("Dropping request (id 0x%04x -> 0x%04x)",
2401 req->srcid, req->dstid);
2404 g_free(req->request);
2410 g_slist_free(request_list);
2411 request_list = NULL;
2413 destroy_tcp_listener(ifdata);
2414 destroy_udp_listener(ifdata);
2417 int __connman_dnsproxy_add_listener(const char *interface)
2419 struct listener_data *ifdata;
2422 DBG("interface %s", interface);
2424 if (g_hash_table_lookup(listener_table, interface) != NULL)
2427 ifdata = g_try_new0(struct listener_data, 1);
2431 ifdata->ifname = g_strdup(interface);
2432 ifdata->udp_listener_channel = NULL;
2433 ifdata->udp_listener_watch = 0;
2434 ifdata->tcp_listener_channel = NULL;
2435 ifdata->tcp_listener_watch = 0;
2437 err = create_listener(ifdata);
2439 connman_error("Couldn't create listener for %s err %d",
2441 g_free(ifdata->ifname);
2445 g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
2449 void __connman_dnsproxy_remove_listener(const char *interface)
2451 struct listener_data *ifdata;
2453 DBG("interface %s", interface);
2455 ifdata = g_hash_table_lookup(listener_table, interface);
2459 destroy_listener(ifdata);
2461 g_hash_table_remove(listener_table, interface);
2464 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
2466 __connman_dnsproxy_remove_listener(key);
2469 int __connman_dnsproxy_init(void)
2475 listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2477 err = __connman_dnsproxy_add_listener("lo");
2481 err = connman_notifier_register(&dnsproxy_notifier);
2488 __connman_dnsproxy_remove_listener("lo");
2489 g_hash_table_destroy(listener_table);
2494 void __connman_dnsproxy_cleanup(void)
2498 connman_notifier_unregister(&dnsproxy_notifier);
2500 g_hash_table_foreach(listener_table, remove_listener, NULL);
2502 g_hash_table_destroy(listener_table);