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 /* if this is a negative reply, we are authorative */
344 update_cached_ttl(buf, len, ttl);
346 DBG("id 0x%04x answers %d", hdr->id, answers);
348 err = sendto(sk, buf, len, 0, to, tolen);
350 connman_error("Cannot send cached DNS response: %s",
356 static void send_response(int sk, unsigned char *buf, int len,
357 const struct sockaddr *to, socklen_t tolen,
360 struct domain_hdr *hdr;
361 int err, offset = protocol_offset(protocol);
371 hdr = (void *) (buf + offset);
373 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
382 err = sendto(sk, buf, len, 0, to, tolen);
384 connman_error("Failed to send DNS response: %s",
390 static gboolean request_timeout(gpointer user_data)
392 struct request_data *req = user_data;
393 struct listener_data *ifdata;
395 DBG("id 0x%04x", req->srcid);
400 ifdata = req->ifdata;
402 request_list = g_slist_remove(request_list, req);
405 if (req->resplen > 0 && req->resp != NULL) {
408 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
410 err = sendto(sk, req->resp, req->resplen, 0,
411 &req->sa, req->sa_len);
414 } else if (req->request && req->numserv == 0) {
415 struct domain_hdr *hdr;
417 if (req->protocol == IPPROTO_TCP) {
418 hdr = (void *) (req->request + 2);
419 hdr->id = req->srcid;
420 send_response(req->client_sk, req->request,
421 req->request_len, NULL, 0, IPPROTO_TCP);
423 } else if (req->protocol == IPPROTO_UDP) {
426 hdr = (void *) (req->request);
427 hdr->id = req->srcid;
428 sk = g_io_channel_unix_get_fd(
429 ifdata->udp_listener_channel);
430 send_response(sk, req->request, req->request_len,
431 &req->sa, req->sa_len, IPPROTO_UDP);
441 static int append_query(unsigned char *buf, unsigned int size,
442 const char *query, const char *domain)
444 unsigned char *ptr = buf;
448 DBG("query %s domain %s", query, domain);
450 offset = (char *) query;
451 while (offset != NULL) {
454 tmp = strchr(offset, '.');
456 len = strlen(offset);
460 memcpy(ptr + 1, offset, len);
466 memcpy(ptr + 1, offset, tmp - offset);
467 ptr += tmp - offset + 1;
472 offset = (char *) domain;
473 while (offset != NULL) {
476 tmp = strchr(offset, '.');
478 len = strlen(offset);
482 memcpy(ptr + 1, offset, len);
488 memcpy(ptr + 1, offset, tmp - offset);
489 ptr += tmp - offset + 1;
499 static gboolean cache_check_is_valid(struct cache_data *data,
505 if (data->cache_until < current_time)
512 * remove stale cached entries so that they can be refreshed
514 static void cache_enforce_validity(struct cache_entry *entry)
516 time_t current_time = time(0);
518 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE
520 DBG("cache timeout \"%s\" type A", entry->key);
521 g_free(entry->ipv4->data);
527 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE
529 DBG("cache timeout \"%s\" type AAAA", entry->key);
530 g_free(entry->ipv6->data);
537 static uint16_t cache_check_validity(char *question, uint16_t type,
538 struct cache_entry *entry)
540 time_t current_time = time(0);
542 cache_enforce_validity(entry);
546 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE) {
547 DBG("cache %s \"%s\" type A", entry->ipv4 ?
548 "timeout" : "entry missing", question);
551 * We do not remove cache entry if there is still
552 * valid IPv6 entry found in the cache.
554 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE)
555 g_hash_table_remove(cache, question);
562 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE) {
563 DBG("cache %s \"%s\" type AAAA", entry->ipv6 ?
564 "timeout" : "entry missing", question);
566 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE)
567 g_hash_table_remove(cache, question);
577 static struct cache_entry *cache_check(gpointer request, int *qtype)
579 char *question = request + 12;
580 struct cache_entry *entry;
581 struct domain_question *q;
585 offset = strlen(question) + 1;
586 q = (void *) (question + offset);
587 type = ntohs(q->type);
589 /* We only cache either A (1) or AAAA (28) requests */
590 if (type != 1 && type != 28)
593 entry = g_hash_table_lookup(cache, question);
597 type = cache_check_validity(question, type, entry);
606 * Get a label/name from DNS resource record. The function decompresses the
607 * label if necessary. The function does not convert the name to presentation
608 * form. This means that the result string will contain label lengths instead
609 * of dots between labels. We intentionally do not want to convert to dotted
610 * format so that we can cache the wire format string directly.
612 static int get_name(int counter,
613 unsigned char *pkt, unsigned char *start, unsigned char *max,
614 unsigned char *output, int output_max, int *output_len,
615 unsigned char **end, char *name, int *name_len)
619 /* Limit recursion to 10 (this means up to 10 labels in domain name) */
625 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
626 uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
628 if (offset >= max - pkt)
634 return get_name(counter + 1, pkt, pkt + offset, max,
635 output, output_max, output_len, end,
638 unsigned label_len = *p;
640 if (pkt + label_len > max)
643 if (*output_len > output_max)
647 * We need the original name in order to check
648 * if this answer is the correct one.
650 name[(*name_len)++] = label_len;
651 memcpy(name + *name_len, p + 1, label_len + 1);
652 *name_len += label_len;
654 /* We compress the result */
655 output[0] = NS_CMPRSFLGS;
672 static int parse_rr(unsigned char *buf, unsigned char *start,
674 unsigned char *response, unsigned int *response_size,
675 uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
679 struct domain_rr *rr;
681 int name_len = 0, output_len = 0, max_rsp = *response_size;
683 err = get_name(0, buf, start, max, response, max_rsp,
684 &output_len, end, name, &name_len);
690 if ((unsigned int) offset > *response_size)
693 rr = (void *) (*end);
698 *type = ntohs(rr->type);
699 *class = ntohs(rr->class);
700 *ttl = ntohl(rr->ttl);
701 *rdlen = ntohs(rr->rdlen);
706 memcpy(response + offset, *end, sizeof(struct domain_rr));
708 offset += sizeof(struct domain_rr);
709 *end += sizeof(struct domain_rr);
711 if ((unsigned int) (offset + *rdlen) > *response_size)
714 memcpy(response + offset, *end, *rdlen);
718 *response_size = offset + *rdlen;
723 static gboolean check_alias(GSList *aliases, char *name)
727 if (aliases != NULL) {
728 for (list = aliases; list; list = list->next) {
729 int len = strlen((char *)list->data);
730 if (strncmp((char *)list->data, name, len) == 0)
738 static int parse_response(unsigned char *buf, int buflen,
739 char *question, int qlen,
740 uint16_t *type, uint16_t *class, int *ttl,
741 unsigned char *response, unsigned int *response_len,
744 struct domain_hdr *hdr = (void *) buf;
745 struct domain_question *q;
747 uint16_t qdcount = ntohs(hdr->qdcount);
748 uint16_t ancount = ntohs(hdr->ancount);
750 uint16_t qtype, qclass;
751 unsigned char *next = NULL;
752 unsigned int maxlen = *response_len;
753 GSList *aliases = NULL, *list;
754 char name[NS_MAXDNAME + 1];
759 DBG("qr %d qdcount %d", hdr->qr, qdcount);
761 /* We currently only cache responses where question count is 1 */
762 if (hdr->qr != 1 || qdcount != 1)
765 ptr = buf + sizeof(struct domain_hdr);
767 strncpy(question, (char *) ptr, qlen);
768 qlen = strlen(question);
769 ptr += qlen + 1; /* skip \0 */
772 qtype = ntohs(q->type);
774 /* We cache only A and AAAA records */
775 if (qtype != 1 && qtype != 28)
778 qclass = ntohs(q->class);
780 ptr += 2 + 2; /* ptr points now to answers */
787 * We have a bunch of answers (like A, AAAA, CNAME etc) to
788 * A or AAAA question. We traverse the answers and parse the
789 * resource records. Only A and AAAA records are cached, all
790 * the other records in answers are skipped.
792 for (i = 0; i < ancount; i++) {
794 * Get one address at a time to this buffer.
795 * The max size of the answer is
796 * 2 (pointer) + 2 (type) + 2 (class) +
797 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
798 * for A or AAAA record.
799 * For CNAME the size can be bigger.
801 unsigned char rsp[NS_MAXCDNAME];
802 unsigned int rsp_len = sizeof(rsp) - 1;
805 memset(rsp, 0, sizeof(rsp));
807 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
808 type, class, ttl, &rdlen, &next, name);
815 * Now rsp contains compressed or uncompressed resource
816 * record. Next we check if this record answers the question.
817 * The name var contains the uncompressed label.
818 * One tricky bit is the CNAME records as they alias
819 * the name we might be interested in.
823 * Go to next answer if the class is not the one we are
826 if (*class != qclass) {
833 * Try to resolve aliases also, type is CNAME(5).
834 * This is important as otherwise the aliased names would not
835 * be cached at all as the cache would not contain the aliased
838 * If any CNAME is found in DNS packet, then we cache the alias
839 * IP address instead of the question (as the server
840 * said that question has only an alias).
841 * This means in practice that if e.g., ipv6.google.com is
842 * queried, DNS server returns CNAME of that name which is
843 * ipv6.l.google.com. We then cache the address of the CNAME
844 * but return the question name to client. So the alias
845 * status of the name is not saved in cache and thus not
846 * returned to the client. We do not return DNS packets from
847 * cache to client saying that ipv6.google.com is an alias to
848 * ipv6.l.google.com but we return instead a DNS packet that
849 * says ipv6.google.com has address xxx which is in fact the
850 * address of ipv6.l.google.com. For caching purposes this
851 * should not cause any issues.
853 if (*type == 5 && strncmp(question, name, qlen) == 0) {
855 * So now the alias answered the question. This is
856 * not very useful from caching point of view as
857 * the following A or AAAA records will not match the
858 * question. We need to find the real A/AAAA record
859 * of the alias and cache that.
861 unsigned char *end = NULL;
862 int name_len = 0, output_len;
864 memset(rsp, 0, sizeof(rsp));
865 rsp_len = sizeof(rsp) - 1;
868 * Alias is in rdata part of the message,
869 * and next-rdlen points to it. So we need to get
870 * the real name of the alias.
872 ret = get_name(0, buf, next - rdlen, buf + buflen,
873 rsp, rsp_len, &output_len, &end,
876 /* just ignore the error at this point */
883 * We should now have the alias of the entry we might
884 * want to cache. Just remember it for a while.
885 * We check the alias list when we have parsed the
888 aliases = g_slist_prepend(aliases, g_strdup(name));
895 if (*type == qtype) {
897 * We found correct type (A or AAAA)
899 if (check_alias(aliases, name) == TRUE ||
900 (aliases == NULL && strncmp(question, name,
903 * We found an alias or the name of the rr
904 * matches the question. If so, we append
905 * the compressed label to the cache.
906 * The end result is a response buffer that
907 * will contain one or more cached and
908 * compressed resource records.
910 if (*response_len + rsp_len > maxlen) {
914 memcpy(response + *response_len, rsp, rsp_len);
915 *response_len += rsp_len;
926 for (list = aliases; list; list = list->next)
928 g_slist_free(aliases);
933 struct cache_timeout {
938 static gboolean cache_check_entry(gpointer key, gpointer value,
941 struct cache_timeout *data = user_data;
942 struct cache_entry *entry = value;
946 * If either IPv4 or IPv6 cached entry has expired, we
947 * remove both from the cache.
950 if (entry->ipv4 != NULL && entry->ipv4->timeout > 0) {
951 max_timeout = entry->ipv4->cache_until;
952 if (max_timeout > data->max_timeout)
953 data->max_timeout = max_timeout;
955 if (entry->ipv4->cache_until < data->current_time)
959 if (entry->ipv6 != NULL && entry->ipv6->timeout > 0) {
960 max_timeout = entry->ipv6->cache_until;
961 if (max_timeout > data->max_timeout)
962 data->max_timeout = max_timeout;
964 if (entry->ipv6->cache_until < data->current_time)
971 static void cache_cleanup(void)
973 static int max_timeout;
974 struct cache_timeout data;
977 data.current_time = time(0);
978 data.max_timeout = 0;
980 if (max_timeout > data.current_time) {
981 DBG("waiting %ld secs before cleaning cache",
982 max_timeout - data.current_time);
986 count = g_hash_table_foreach_remove(cache, cache_check_entry,
988 DBG("removed %d", count);
992 * If we could not remove anything, then remember
993 * what is the max timeout and do nothing if we
994 * have not yet reached it. This will prevent
995 * constant traversal of the cache if it is full.
997 max_timeout = data.max_timeout;
1002 static int reply_query_type(unsigned char *msg, int len)
1009 /* skip the header */
1010 c = msg + sizeof(struct domain_hdr);
1011 len -= sizeof(struct domain_hdr);
1016 /* now the query, which is a name and 2 16 bit words */
1017 l = dns_name_length(c) + 1;
1025 static int cache_update(struct server_data *srv, unsigned char *msg,
1026 unsigned int msg_len)
1028 int offset = protocol_offset(srv->protocol);
1029 int err, qlen, ttl = 0;
1030 uint16_t answers = 0, type = 0, class = 0;
1031 struct domain_question *q;
1032 struct cache_entry *entry;
1033 struct cache_data *data;
1034 char question[NS_MAXDNAME + 1];
1035 unsigned char response[NS_MAXDNAME + 1];
1037 unsigned int rsplen;
1038 gboolean new_entry = TRUE;
1039 time_t current_time;
1041 if (cache_size >= MAX_CACHE_SIZE) {
1043 if (cache_size >= MAX_CACHE_SIZE)
1047 /* Continue only if response code is 0 (=ok) */
1054 rsplen = sizeof(response) - 1;
1055 question[sizeof(question) - 1] = '\0';
1057 err = parse_response(msg + offset, msg_len - offset,
1058 question, sizeof(question) - 1,
1059 &type, &class, &ttl,
1060 response, &rsplen, &answers);
1063 * special case: if we do a ipv6 lookup and get no result
1064 * for a record that's already in our ipv4 cache.. we want
1065 * to cache the negative response.
1067 if ((err == -ENOMSG || err == -ENOBUFS) &&
1068 reply_query_type(msg, msg_len) == 28) {
1069 entry = g_hash_table_lookup(cache, question);
1070 if (entry && entry->ipv4 && entry->ipv6 == NULL) {
1071 data = g_try_new(struct cache_data, 1);
1074 data->inserted = entry->ipv4->inserted;
1076 data->answers = msg[5];
1077 data->timeout = entry->ipv4->timeout;
1078 data->data_len = msg_len;
1079 data->data = ptr = g_malloc(msg_len);
1080 data->valid_until = entry->ipv4->valid_until;
1081 data->cache_until = entry->ipv4->cache_until;
1082 memcpy(data->data, msg, msg_len);
1088 if (err < 0 || ttl == 0)
1091 qlen = strlen(question);
1092 current_time = time(0);
1095 * If the cache contains already data, check if the
1096 * type of the cached data is the same and do not add
1097 * to cache if data is already there.
1098 * This is needed so that we can cache both A and AAAA
1099 * records for the same name.
1101 entry = g_hash_table_lookup(cache, question);
1102 if (entry == NULL) {
1103 entry = g_try_new(struct cache_entry, 1);
1107 data = g_try_new(struct cache_data, 1);
1113 entry->key = g_strdup(question);
1114 entry->ipv4 = entry->ipv6 = NULL;
1121 if (type == 1 && entry->ipv4 != NULL)
1124 if (type == 28 && entry->ipv6 != NULL)
1127 data = g_try_new(struct cache_data, 1);
1139 data->inserted = current_time;
1141 data->answers = answers;
1142 data->timeout = ttl;
1143 data->data_len = 12 + qlen + 1 + 2 + 2 + rsplen;
1144 data->data = ptr = g_malloc(data->data_len);
1145 data->valid_until = current_time + ttl;
1148 * Restrict the cached DNS record TTL to some sane value
1149 * in order to prevent data staying in the cache too long.
1151 if (ttl > MAX_CACHE_TTL)
1152 ttl = MAX_CACHE_TTL;
1154 data->cache_until = round_down_ttl(current_time + ttl, ttl);
1156 if (data->data == NULL) {
1163 memcpy(ptr, msg, 12);
1164 memcpy(ptr + 12, question, qlen + 1); /* copy also the \0 */
1166 q = (void *) (ptr + 12 + qlen + 1);
1167 q->type = htons(type);
1168 q->class = htons(class);
1169 memcpy(ptr + 12 + qlen + 1 + sizeof(struct domain_question),
1172 if (new_entry == TRUE) {
1173 g_hash_table_replace(cache, entry->key, entry);
1177 DBG("cache %d %squestion \"%s\" type %d ttl %d size %zd",
1178 cache_size, new_entry ? "new " : "old ",
1179 question, type, ttl,
1180 sizeof(*entry) + sizeof(*data) + data->data_len + qlen);
1185 static int ns_resolv(struct server_data *server, struct request_data *req,
1186 gpointer request, gpointer name)
1189 int sk, err, type = 0;
1190 char *dot, *lookup = (char *) name;
1191 struct cache_entry *entry;
1193 entry = cache_check(request, &type);
1194 if (entry != NULL) {
1196 struct cache_data *data;
1198 DBG("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1205 ttl_left = data->valid_until - time(0);
1207 if (data != NULL && req->protocol == IPPROTO_TCP) {
1208 send_cached_response(req->client_sk, data->data,
1209 data->data_len, NULL, 0, IPPROTO_TCP,
1210 req->srcid, data->answers, ttl_left);
1214 if (data != NULL && req->protocol == IPPROTO_UDP) {
1216 sk = g_io_channel_unix_get_fd(
1217 req->ifdata->udp_listener_channel);
1219 send_cached_response(sk, data->data,
1220 data->data_len, &req->sa, req->sa_len,
1221 IPPROTO_UDP, req->srcid, data->answers,
1227 sk = g_io_channel_unix_get_fd(server->channel);
1229 err = send(sk, request, req->request_len, 0);
1233 /* If we have more than one dot, we don't add domains */
1234 dot = strchr(lookup, '.');
1235 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
1238 if (server->domains != NULL && server->domains->data != NULL)
1239 req->append_domain = TRUE;
1241 for (list = server->domains; list; list = list->next) {
1243 unsigned char alt[1024];
1244 struct domain_hdr *hdr = (void *) &alt;
1245 int altlen, domlen, offset;
1247 domain = list->data;
1252 offset = protocol_offset(server->protocol);
1256 domlen = strlen(domain) + 1;
1260 alt[offset] = req->altid & 0xff;
1261 alt[offset + 1] = req->altid >> 8;
1263 memcpy(alt + offset + 2, request + offset + 2, 10);
1264 hdr->qdcount = htons(1);
1266 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1273 memcpy(alt + offset + altlen,
1274 request + offset + altlen - domlen,
1275 req->request_len - altlen - offset + domlen);
1277 if (server->protocol == IPPROTO_TCP) {
1278 int req_len = req->request_len + domlen - 2;
1280 alt[0] = (req_len >> 8) & 0xff;
1281 alt[1] = req_len & 0xff;
1284 err = send(sk, alt, req->request_len + domlen, 0);
1294 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1295 struct server_data *data)
1297 struct domain_hdr *hdr;
1298 struct request_data *req;
1299 int dns_id, sk, err, offset = protocol_offset(protocol);
1300 struct listener_data *ifdata;
1305 hdr = (void *)(reply + offset);
1306 dns_id = reply[offset] | reply[offset + 1] << 8;
1308 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1310 req = find_request(dns_id);
1314 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
1316 ifdata = req->ifdata;
1318 reply[offset] = req->srcid & 0xff;
1319 reply[offset + 1] = req->srcid >> 8;
1323 if (hdr->rcode == 0 || req->resp == NULL) {
1326 * If the domain name was append
1327 * remove it before forwarding the reply.
1329 if (req->append_domain == TRUE) {
1332 unsigned int domain_len;
1335 * ptr points to the first char of the hostname.
1336 * ->hostname.domain.net
1338 ptr = reply + offset + sizeof(struct domain_hdr);
1340 domain_len = strlen((const char *)ptr) - host_len - 1;
1343 * remove the domain name and replaced it by the end
1346 memmove(ptr + host_len + 1,
1347 ptr + host_len + domain_len + 1,
1348 reply_len - (ptr - reply + domain_len));
1350 reply_len = reply_len - domain_len;
1356 req->resp = g_try_malloc(reply_len);
1357 if (req->resp == NULL)
1360 memcpy(req->resp, reply, reply_len);
1361 req->resplen = reply_len;
1363 cache_update(data, reply, reply_len);
1366 if (hdr->rcode > 0 && req->numresp < req->numserv)
1369 if (req->timeout > 0)
1370 g_source_remove(req->timeout);
1372 request_list = g_slist_remove(request_list, req);
1374 if (protocol == IPPROTO_UDP) {
1375 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
1376 err = sendto(sk, req->resp, req->resplen, 0,
1377 &req->sa, req->sa_len);
1379 sk = req->client_sk;
1380 err = send(sk, req->resp, req->resplen, 0);
1390 static void cache_element_destroy(gpointer value)
1392 struct cache_entry *entry = value;
1397 if (entry->ipv4 != NULL) {
1398 g_free(entry->ipv4->data);
1399 g_free(entry->ipv4);
1402 if (entry->ipv6 != NULL) {
1403 g_free(entry->ipv6->data);
1404 g_free(entry->ipv6);
1410 if (--cache_size < 0)
1414 static void destroy_server(struct server_data *server)
1418 DBG("interface %s server %s", server->interface, server->server);
1420 server_list = g_slist_remove(server_list, server);
1422 if (server->watch > 0)
1423 g_source_remove(server->watch);
1425 if (server->timeout > 0)
1426 g_source_remove(server->timeout);
1428 g_io_channel_unref(server->channel);
1430 if (server->protocol == IPPROTO_UDP)
1431 connman_info("Removing DNS server %s", server->server);
1433 g_free(server->incoming_reply);
1434 g_free(server->server);
1435 for (list = server->domains; list; list = list->next) {
1436 char *domain = list->data;
1438 server->domains = g_list_remove(server->domains, domain);
1441 g_free(server->interface);
1443 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1)
1444 g_hash_table_destroy(cache);
1449 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
1452 unsigned char buf[4096];
1454 struct server_data *data = user_data;
1456 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1457 connman_error("Error with UDP server %s", data->server);
1462 sk = g_io_channel_unix_get_fd(channel);
1464 len = recv(sk, buf, sizeof(buf), 0);
1468 err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
1475 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
1479 struct server_data *server = user_data;
1481 sk = g_io_channel_unix_get_fd(channel);
1485 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1488 DBG("TCP server channel closed");
1491 * Discard any partial response which is buffered; better
1492 * to get a proper response from a working server.
1494 g_free(server->incoming_reply);
1495 server->incoming_reply = NULL;
1497 for (list = request_list; list; list = list->next) {
1498 struct request_data *req = list->data;
1499 struct domain_hdr *hdr;
1501 if (req->protocol == IPPROTO_UDP)
1504 if (req->request == NULL)
1508 * If we're not waiting for any further response
1509 * from another name server, then we send an error
1510 * response to the client.
1512 if (req->numserv && --(req->numserv))
1515 hdr = (void *) (req->request + 2);
1516 hdr->id = req->srcid;
1517 send_response(req->client_sk, req->request,
1518 req->request_len, NULL, 0, IPPROTO_TCP);
1520 request_list = g_slist_remove(request_list, req);
1523 destroy_server(server);
1528 if ((condition & G_IO_OUT) && !server->connected) {
1531 struct server_data *udp_server;
1533 udp_server = find_server(server->interface, server->server,
1535 if (udp_server != NULL) {
1536 for (domains = udp_server->domains; domains;
1537 domains = domains->next) {
1538 char *dom = domains->data;
1540 DBG("Adding domain %s to %s",
1541 dom, server->server);
1543 server->domains = g_list_append(server->domains,
1548 server->connected = TRUE;
1549 server_list = g_slist_append(server_list, server);
1551 if (server->timeout > 0) {
1552 g_source_remove(server->timeout);
1553 server->timeout = 0;
1556 for (list = request_list; list; list = list->next) {
1557 struct request_data *req = list->data;
1559 if (req->protocol == IPPROTO_UDP)
1562 DBG("Sending req %s over TCP", (char *)req->name);
1564 if (req->timeout > 0)
1565 g_source_remove(req->timeout);
1567 req->timeout = g_timeout_add_seconds(30,
1568 request_timeout, req);
1569 if (ns_resolv(server, req, req->request,
1571 /* We sent cached result so no need for timeout
1574 if (req->timeout > 0) {
1575 g_source_remove(req->timeout);
1581 } else if (condition & G_IO_IN) {
1582 struct partial_reply *reply = server->incoming_reply;
1586 unsigned char reply_len_buf[2];
1589 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
1592 } else if (bytes_recv < 0) {
1593 if (errno == EAGAIN || errno == EWOULDBLOCK)
1596 connman_error("DNS proxy error %s",
1599 } else if (bytes_recv < 2)
1602 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
1605 DBG("TCP reply %d bytes", reply_len);
1607 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
1611 reply->len = reply_len;
1612 reply->received = 0;
1614 server->incoming_reply = reply;
1617 while (reply->received < reply->len) {
1618 bytes_recv = recv(sk, reply->buf + reply->received,
1619 reply->len - reply->received, 0);
1621 connman_error("DNS proxy TCP disconnect");
1623 } else if (bytes_recv < 0) {
1624 if (errno == EAGAIN || errno == EWOULDBLOCK)
1627 connman_error("DNS proxy error %s",
1631 reply->received += bytes_recv;
1634 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
1638 server->incoming_reply = NULL;
1640 destroy_server(server);
1648 static gboolean tcp_idle_timeout(gpointer user_data)
1650 struct server_data *server = user_data;
1657 destroy_server(server);
1662 static struct server_data *create_server(const char *interface,
1663 const char *domain, const char *server,
1666 struct addrinfo hints, *rp;
1667 struct server_data *data;
1670 DBG("interface %s server %s", interface, server);
1672 memset(&hints, 0, sizeof(hints));
1676 hints.ai_socktype = SOCK_DGRAM;
1680 hints.ai_socktype = SOCK_STREAM;
1686 hints.ai_family = AF_UNSPEC;
1687 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
1689 ret = getaddrinfo(server, "53", &hints, &rp);
1691 connman_error("Failed to parse server %s address: %s\n",
1692 server, gai_strerror(ret));
1695 /* Do not blindly copy this code elsewhere; it doesn't loop over the
1696 results using ->ai_next as it should. That's OK in *this* case
1697 because it was a numeric lookup; we *know* there's only one. */
1699 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1701 connman_error("Failed to create server %s socket", server);
1706 if (interface != NULL) {
1707 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1708 interface, strlen(interface) + 1) < 0) {
1709 connman_error("Failed to bind server %s "
1718 data = g_try_new0(struct server_data, 1);
1720 connman_error("Failed to allocate server %s data", server);
1726 data->channel = g_io_channel_unix_new(sk);
1727 if (data->channel == NULL) {
1728 connman_error("Failed to create server %s channel", server);
1735 g_io_channel_set_close_on_unref(data->channel, TRUE);
1737 if (protocol == IPPROTO_TCP) {
1738 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
1739 data->watch = g_io_add_watch(data->channel,
1740 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1741 tcp_server_event, data);
1742 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
1745 data->watch = g_io_add_watch(data->channel,
1746 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1747 udp_server_event, data);
1749 data->interface = g_strdup(interface);
1751 data->domains = g_list_append(data->domains, g_strdup(domain));
1752 data->server = g_strdup(server);
1753 data->protocol = protocol;
1755 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
1758 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
1759 protocol == IPPROTO_UDP) {
1762 connman_error("Failed to connect to server %s", server);
1763 if (data->watch > 0)
1764 g_source_remove(data->watch);
1765 if (data->timeout > 0)
1766 g_source_remove(data->timeout);
1768 g_io_channel_unref(data->channel);
1771 g_free(data->server);
1772 g_free(data->interface);
1773 for (list = data->domains; list; list = list->next) {
1774 char *domain = list->data;
1776 data->domains = g_list_remove(data->domains,
1785 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
1786 cache = g_hash_table_new_full(g_str_hash,
1789 cache_element_destroy);
1791 if (protocol == IPPROTO_UDP) {
1792 /* Enable new servers by default */
1793 data->enabled = TRUE;
1794 connman_info("Adding DNS server %s", data->server);
1796 server_list = g_slist_append(server_list, data);
1804 static gboolean resolv(struct request_data *req,
1805 gpointer request, gpointer name)
1810 for (list = server_list; list; list = list->next) {
1811 struct server_data *data = list->data;
1813 DBG("server %s enabled %d", data->server, data->enabled);
1815 if (data->enabled == FALSE)
1818 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
1819 data->watch = g_io_add_watch(data->channel,
1820 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1821 udp_server_event, data);
1823 status = ns_resolv(data, req, request, name);
1828 if (req->timeout > 0) {
1829 g_source_remove(req->timeout);
1838 static void append_domain(const char *interface, const char *domain)
1842 DBG("interface %s domain %s", interface, domain);
1847 for (list = server_list; list; list = list->next) {
1848 struct server_data *data = list->data;
1851 gboolean dom_found = FALSE;
1853 if (data->interface == NULL)
1856 if (g_str_equal(data->interface, interface) == FALSE)
1859 for (dom_list = data->domains; dom_list;
1860 dom_list = dom_list->next) {
1861 dom = dom_list->data;
1863 if (g_str_equal(dom, domain)) {
1869 if (dom_found == FALSE) {
1871 g_list_append(data->domains, g_strdup(domain));
1876 int __connman_dnsproxy_append(const char *interface, const char *domain,
1879 struct server_data *data;
1881 DBG("interface %s server %s", interface, server);
1883 if (server == NULL && domain == NULL)
1886 if (server == NULL) {
1887 append_domain(interface, domain);
1892 if (g_str_equal(server, "127.0.0.1") == TRUE)
1895 data = find_server(interface, server, IPPROTO_UDP);
1897 append_domain(interface, domain);
1901 data = create_server(interface, domain, server, IPPROTO_UDP);
1908 static void remove_server(const char *interface, const char *domain,
1909 const char *server, int protocol)
1911 struct server_data *data;
1913 data = find_server(interface, server, protocol);
1917 destroy_server(data);
1920 int __connman_dnsproxy_remove(const char *interface, const char *domain,
1923 DBG("interface %s server %s", interface, server);
1928 if (g_str_equal(server, "127.0.0.1") == TRUE)
1931 remove_server(interface, domain, server, IPPROTO_UDP);
1932 remove_server(interface, domain, server, IPPROTO_TCP);
1937 void __connman_dnsproxy_flush(void)
1941 list = request_pending_list;
1943 struct request_data *req = list->data;
1947 request_pending_list =
1948 g_slist_remove(request_pending_list, req);
1949 resolv(req, req->request, req->name);
1950 g_free(req->request);
1955 static void dnsproxy_offline_mode(connman_bool_t enabled)
1959 DBG("enabled %d", enabled);
1961 for (list = server_list; list; list = list->next) {
1962 struct server_data *data = list->data;
1964 if (enabled == FALSE) {
1965 connman_info("Enabling DNS server %s", data->server);
1966 data->enabled = TRUE;
1968 connman_info("Disabling DNS server %s", data->server);
1969 data->enabled = FALSE;
1974 static void dnsproxy_default_changed(struct connman_service *service)
1979 DBG("service %p", service);
1981 if (service == NULL) {
1982 /* When no services are active, then disable DNS proxying */
1983 dnsproxy_offline_mode(TRUE);
1987 interface = connman_service_get_interface(service);
1988 if (interface == NULL)
1991 for (list = server_list; list; list = list->next) {
1992 struct server_data *data = list->data;
1994 if (g_strcmp0(data->interface, interface) == 0) {
1995 connman_info("Enabling DNS server %s", data->server);
1996 data->enabled = TRUE;
1998 connman_info("Disabling DNS server %s", data->server);
1999 data->enabled = FALSE;
2006 static struct connman_notifier dnsproxy_notifier = {
2008 .default_changed = dnsproxy_default_changed,
2009 .offline_mode = dnsproxy_offline_mode,
2012 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2014 static int parse_request(unsigned char *buf, int len,
2015 char *name, unsigned int size)
2017 struct domain_hdr *hdr = (void *) buf;
2018 uint16_t qdcount = ntohs(hdr->qdcount);
2019 uint16_t arcount = ntohs(hdr->arcount);
2021 char *last_label = NULL;
2022 unsigned int remain, used = 0;
2027 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2028 hdr->id, hdr->qr, hdr->opcode,
2031 if (hdr->qr != 0 || qdcount != 1)
2034 memset(name, 0, size);
2036 ptr = buf + sizeof(struct domain_hdr);
2037 remain = len - sizeof(struct domain_hdr);
2039 while (remain > 0) {
2043 last_label = (char *) (ptr + 1);
2047 if (used + len + 1 > size)
2050 strncat(name, (char *) (ptr + 1), len);
2059 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
2060 !memcmp(last_label + 5, opt_edns0_type, 2)) {
2061 uint16_t edns0_bufsize;
2063 edns0_bufsize = last_label[7] << 8 | last_label[8];
2065 DBG("EDNS0 buffer size %u", edns0_bufsize);
2067 /* This is an evil hack until full TCP support has been
2070 * Somtimes the EDNS0 request gets send with a too-small
2071 * buffer size. Since glibc doesn't seem to crash when it
2072 * gets a response biffer then it requested, just bump
2073 * the buffer size up to 4KiB.
2075 if (edns0_bufsize < 0x1000) {
2076 last_label[7] = 0x10;
2077 last_label[8] = 0x00;
2081 DBG("query %s", name);
2086 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
2089 unsigned char buf[768];
2091 struct request_data *req;
2092 struct server_data *server;
2093 int sk, client_sk, len, err;
2094 struct sockaddr_in6 client_addr;
2095 socklen_t client_addr_len = sizeof(client_addr);
2097 struct listener_data *ifdata = user_data;
2099 DBG("condition 0x%x", condition);
2101 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2102 if (ifdata->tcp_listener_watch > 0)
2103 g_source_remove(ifdata->tcp_listener_watch);
2104 ifdata->tcp_listener_watch = 0;
2106 connman_error("Error with TCP listener channel");
2111 sk = g_io_channel_unix_get_fd(channel);
2113 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
2114 if (client_sk < 0) {
2115 connman_error("Accept failure on TCP listener");
2116 ifdata->tcp_listener_watch = 0;
2120 len = recv(client_sk, buf, sizeof(buf), 0);
2124 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
2126 err = parse_request(buf + 2, len - 2, query, sizeof(query));
2127 if (err < 0 || (g_slist_length(server_list) == 0)) {
2128 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2132 req = g_try_new0(struct request_data, 1);
2136 memcpy(&req->sa, &client_addr, client_addr_len);
2137 req->sa_len = client_addr_len;
2138 req->client_sk = client_sk;
2139 req->protocol = IPPROTO_TCP;
2142 if (request_id == 0x0000 || request_id == 0xffff)
2145 req->srcid = buf[2] | (buf[3] << 8);
2146 req->dstid = request_id;
2147 req->altid = request_id + 1;
2148 req->request_len = len;
2150 buf[2] = req->dstid & 0xff;
2151 buf[3] = req->dstid >> 8;
2154 req->ifdata = (struct listener_data *) ifdata;
2155 req->append_domain = FALSE;
2156 request_list = g_slist_append(request_list, req);
2158 for (list = server_list; list; list = list->next) {
2159 struct server_data *data = list->data;
2162 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
2165 server = create_server(data->interface, NULL,
2166 data->server, IPPROTO_TCP);
2169 * If server is NULL, we're not connected yet.
2170 * Copy the relevant buffers and continue with
2171 * the next nameserver.
2172 * The request will actually be sent once we're
2173 * properly connected over TCP to this nameserver.
2175 if (server == NULL) {
2176 req->request = g_try_malloc0(req->request_len);
2177 if (req->request == NULL)
2180 memcpy(req->request, buf, req->request_len);
2182 req->name = g_try_malloc0(sizeof(query));
2183 if (req->name == NULL) {
2184 g_free(req->request);
2187 memcpy(req->name, query, sizeof(query));
2192 if (req->timeout > 0)
2193 g_source_remove(req->timeout);
2195 for (domains = data->domains; domains;
2196 domains = domains->next) {
2197 char *dom = domains->data;
2199 DBG("Adding domain %s to %s", dom, server->server);
2201 server->domains = g_list_append(server->domains,
2205 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
2206 if (ns_resolv(server, req, buf, query) > 0) {
2207 if (req->timeout > 0) {
2208 g_source_remove(req->timeout);
2217 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
2220 unsigned char buf[768];
2222 struct request_data *req;
2223 struct sockaddr_in6 client_addr;
2224 socklen_t client_addr_len = sizeof(client_addr);
2226 struct listener_data *ifdata = user_data;
2228 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2229 connman_error("Error with UDP listener channel");
2230 ifdata->udp_listener_watch = 0;
2234 sk = g_io_channel_unix_get_fd(channel);
2236 memset(&client_addr, 0, client_addr_len);
2237 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
2242 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
2244 err = parse_request(buf, len, query, sizeof(query));
2245 if (err < 0 || (g_slist_length(server_list) == 0)) {
2246 send_response(sk, buf, len, (void *)&client_addr,
2247 client_addr_len, IPPROTO_UDP);
2251 req = g_try_new0(struct request_data, 1);
2255 memcpy(&req->sa, &client_addr, client_addr_len);
2256 req->sa_len = client_addr_len;
2258 req->protocol = IPPROTO_UDP;
2261 if (request_id == 0x0000 || request_id == 0xffff)
2264 req->srcid = buf[0] | (buf[1] << 8);
2265 req->dstid = request_id;
2266 req->altid = request_id + 1;
2267 req->request_len = len;
2269 buf[0] = req->dstid & 0xff;
2270 buf[1] = req->dstid >> 8;
2273 req->ifdata = (struct listener_data *) ifdata;
2274 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2275 req->append_domain = FALSE;
2276 request_list = g_slist_append(request_list, req);
2278 return resolv(req, buf, query);
2281 static int create_dns_listener(int protocol, struct listener_data *ifdata)
2283 GIOChannel *channel;
2287 struct sockaddr_in6 sin6;
2288 struct sockaddr_in sin;
2291 int sk, type, v6only = 0;
2292 int family = AF_INET6;
2295 DBG("interface %s", ifdata->ifname);
2300 type = SOCK_DGRAM | SOCK_CLOEXEC;
2305 type = SOCK_STREAM | SOCK_CLOEXEC;
2312 sk = socket(family, type, protocol);
2313 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
2314 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
2316 sk = socket(family, type, protocol);
2319 connman_error("Failed to create %s listener socket", proto);
2323 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2325 strlen(ifdata->ifname) + 1) < 0) {
2326 connman_error("Failed to bind %s listener interface", proto);
2330 /* Ensure it accepts Legacy IP connections too */
2331 if (family == AF_INET6 &&
2332 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
2333 &v6only, sizeof(v6only)) < 0) {
2334 connman_error("Failed to clear V6ONLY on %s listener socket",
2340 if (family == AF_INET) {
2341 memset(&s.sin, 0, sizeof(s.sin));
2342 s.sin.sin_family = AF_INET;
2343 s.sin.sin_port = htons(53);
2344 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
2345 slen = sizeof(s.sin);
2347 memset(&s.sin6, 0, sizeof(s.sin6));
2348 s.sin6.sin6_family = AF_INET6;
2349 s.sin6.sin6_port = htons(53);
2350 s.sin6.sin6_addr = in6addr_any;
2351 slen = sizeof(s.sin6);
2354 if (bind(sk, &s.sa, slen) < 0) {
2355 connman_error("Failed to bind %s listener socket", proto);
2360 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
2361 connman_error("Failed to listen on TCP socket");
2366 channel = g_io_channel_unix_new(sk);
2367 if (channel == NULL) {
2368 connman_error("Failed to create %s listener channel", proto);
2373 g_io_channel_set_close_on_unref(channel, TRUE);
2375 if (protocol == IPPROTO_TCP) {
2376 ifdata->tcp_listener_channel = channel;
2377 ifdata->tcp_listener_watch = g_io_add_watch(channel,
2378 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
2380 ifdata->udp_listener_channel = channel;
2381 ifdata->udp_listener_watch = g_io_add_watch(channel,
2382 G_IO_IN, udp_listener_event, (gpointer) ifdata);
2388 static void destroy_udp_listener(struct listener_data *ifdata)
2390 DBG("interface %s", ifdata->ifname);
2392 if (ifdata->udp_listener_watch > 0)
2393 g_source_remove(ifdata->udp_listener_watch);
2395 g_io_channel_unref(ifdata->udp_listener_channel);
2398 static void destroy_tcp_listener(struct listener_data *ifdata)
2400 DBG("interface %s", ifdata->ifname);
2402 if (ifdata->tcp_listener_watch > 0)
2403 g_source_remove(ifdata->tcp_listener_watch);
2405 g_io_channel_unref(ifdata->tcp_listener_channel);
2408 static int create_listener(struct listener_data *ifdata)
2412 err = create_dns_listener(IPPROTO_UDP, ifdata);
2416 err = create_dns_listener(IPPROTO_TCP, ifdata);
2418 destroy_udp_listener(ifdata);
2422 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2423 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
2428 static void destroy_listener(struct listener_data *ifdata)
2432 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2433 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
2435 for (list = request_pending_list; list; list = list->next) {
2436 struct request_data *req = list->data;
2438 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
2439 req->srcid, req->dstid);
2442 g_free(req->request);
2448 g_slist_free(request_pending_list);
2449 request_pending_list = NULL;
2451 for (list = request_list; list; list = list->next) {
2452 struct request_data *req = list->data;
2454 DBG("Dropping request (id 0x%04x -> 0x%04x)",
2455 req->srcid, req->dstid);
2458 g_free(req->request);
2464 g_slist_free(request_list);
2465 request_list = NULL;
2467 destroy_tcp_listener(ifdata);
2468 destroy_udp_listener(ifdata);
2471 int __connman_dnsproxy_add_listener(const char *interface)
2473 struct listener_data *ifdata;
2476 DBG("interface %s", interface);
2478 if (g_hash_table_lookup(listener_table, interface) != NULL)
2481 ifdata = g_try_new0(struct listener_data, 1);
2485 ifdata->ifname = g_strdup(interface);
2486 ifdata->udp_listener_channel = NULL;
2487 ifdata->udp_listener_watch = 0;
2488 ifdata->tcp_listener_channel = NULL;
2489 ifdata->tcp_listener_watch = 0;
2491 err = create_listener(ifdata);
2493 connman_error("Couldn't create listener for %s err %d",
2495 g_free(ifdata->ifname);
2499 g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
2503 void __connman_dnsproxy_remove_listener(const char *interface)
2505 struct listener_data *ifdata;
2507 DBG("interface %s", interface);
2509 ifdata = g_hash_table_lookup(listener_table, interface);
2513 destroy_listener(ifdata);
2515 g_hash_table_remove(listener_table, interface);
2518 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
2520 __connman_dnsproxy_remove_listener(key);
2523 int __connman_dnsproxy_init(void)
2529 listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2531 err = __connman_dnsproxy_add_listener("lo");
2535 err = connman_notifier_register(&dnsproxy_notifier);
2542 __connman_dnsproxy_remove_listener("lo");
2543 g_hash_table_destroy(listener_table);
2548 void __connman_dnsproxy_cleanup(void)
2552 connman_notifier_unregister(&dnsproxy_notifier);
2554 g_hash_table_foreach(listener_table, remove_listener, NULL);
2556 g_hash_table_destroy(listener_table);