5 * Copyright (C) 2007-2012 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
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <gweb/gresolv.h>
44 #include <sys/smack.h>
47 #if __BYTE_ORDER == __LITTLE_ENDIAN
62 } __attribute__ ((packed));
63 #elif __BYTE_ORDER == __BIG_ENDIAN
78 } __attribute__ ((packed));
80 #error "Unknown byte order"
83 struct partial_reply {
99 struct partial_reply *incoming_reply;
102 struct request_data {
104 struct sockaddr_in6 __sin6; /* Only for the length */
122 struct listener_data *ifdata;
123 gboolean append_domain;
126 struct listener_data {
128 GIOChannel *udp_listener_channel;
129 guint udp_listener_watch;
130 GIOChannel *tcp_listener_channel;
131 guint tcp_listener_watch;
141 unsigned int data_len;
142 unsigned char *data; /* contains DNS header + body */
149 struct cache_data *ipv4;
150 struct cache_data *ipv6;
153 struct domain_question {
156 } __attribute__ ((packed));
163 } __attribute__ ((packed));
166 * We limit how long the cached DNS entry stays in the cache.
167 * By default the TTL (time-to-live) of the DNS response is used
168 * when setting the cache entry life time. The value is in seconds.
170 #define MAX_CACHE_TTL (60 * 30)
172 * Also limit the other end, cache at least for 30 seconds.
174 #define MIN_CACHE_TTL (30)
177 * We limit the cache size to some sane value so that cached data does
178 * not occupy too much memory. Each cached entry occupies on average
179 * about 100 bytes memory (depending on DNS name length).
180 * Example: caching www.connman.net uses 97 bytes memory.
181 * The value is the max amount of cached DNS responses (count).
183 #define MAX_CACHE_SIZE 256
185 static int cache_size;
186 static GHashTable *cache;
187 static int cache_refcount;
188 static GSList *server_list = NULL;
189 static GSList *request_list = NULL;
190 static GHashTable *listener_table = NULL;
191 static time_t next_refresh;
193 static guint16 get_id()
198 static int protocol_offset(int protocol)
214 * There is a power and efficiency benefit to have entries
215 * in our cache expire at the same time. To this extend,
216 * we round down the cache valid time to common boundaries.
218 static time_t round_down_ttl(time_t end_time, int ttl)
223 /* Less than 5 minutes, round to 10 second boundary */
225 end_time = end_time / 10;
226 end_time = end_time * 10;
227 } else { /* 5 or more minutes, round to 30 seconds */
228 end_time = end_time / 30;
229 end_time = end_time * 30;
234 static struct request_data *find_request(guint16 id)
238 for (list = request_list; list; list = list->next) {
239 struct request_data *req = list->data;
241 if (req->dstid == id || req->altid == id)
248 static struct server_data *find_server(const char *interface,
254 DBG("interface %s server %s", interface, server);
256 for (list = server_list; list; list = list->next) {
257 struct server_data *data = list->data;
259 if (interface == NULL && data->interface == NULL &&
260 g_str_equal(data->server, server) == TRUE &&
261 data->protocol == protocol)
264 if (interface == NULL ||
265 data->interface == NULL || data->server == NULL)
268 if (g_str_equal(data->interface, interface) == TRUE &&
269 g_str_equal(data->server, server) == TRUE &&
270 data->protocol == protocol)
277 /* we can keep using the same resolve's */
278 static GResolv *ipv4_resolve;
279 static GResolv *ipv6_resolve;
281 static void dummy_resolve_func(GResolvResultStatus status,
282 char **results, gpointer user_data)
287 * Refresh a DNS entry, but also age the hit count a bit */
288 static void refresh_dns_entry(struct cache_entry *entry, char *name)
292 if (ipv4_resolve == NULL) {
293 ipv4_resolve = g_resolv_new(0);
294 g_resolv_set_address_family(ipv4_resolve, AF_INET);
295 g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
298 if (ipv6_resolve == NULL) {
299 ipv6_resolve = g_resolv_new(0);
300 g_resolv_set_address_family(ipv6_resolve, AF_INET6);
301 g_resolv_add_nameserver(ipv6_resolve, "127.0.0.1", 53, 0);
304 if (entry->ipv4 == NULL) {
305 DBG("Refresing A record for %s", name);
306 g_resolv_lookup_hostname(ipv4_resolve, name,
307 dummy_resolve_func, NULL);
311 if (entry->ipv6 == NULL) {
312 DBG("Refresing AAAA record for %s", name);
313 g_resolv_lookup_hostname(ipv6_resolve, name,
314 dummy_resolve_func, NULL);
323 static int dns_name_length(unsigned char *buf)
325 if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
327 return strlen((char *)buf);
330 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
337 /* skip the header */
341 /* skip the query, which is a name and 2 16 bit words */
342 l = dns_name_length(c);
348 /* now we get the answer records */
352 l = dns_name_length(c);
357 /* then type + class, 2 bytes each */
363 /* now the 4 byte TTL field */
371 /* now the 2 byte rdlen field */
374 len -= ntohs(*w) + 2;
378 static void send_cached_response(int sk, unsigned char *buf, int len,
379 const struct sockaddr *to, socklen_t tolen,
380 int protocol, int id, uint16_t answers, int ttl)
382 struct domain_hdr *hdr;
383 unsigned char *ptr = buf;
384 int err, offset, dns_len, adj_len = len - 2;
387 * The cached packet contains always the TCP offset (two bytes)
388 * so skip them for UDP.
399 dns_len = ptr[0] * 256 + ptr[1];
408 hdr = (void *) (ptr + offset);
413 hdr->ancount = htons(answers);
417 /* if this is a negative reply, we are authorative */
421 update_cached_ttl((unsigned char *)hdr, adj_len, ttl);
423 DBG("sk %d id 0x%04x answers %d ptr %p length %d dns %d",
424 sk, hdr->id, answers, ptr, len, dns_len);
426 err = sendto(sk, ptr, len, MSG_NOSIGNAL, to, tolen);
428 connman_error("Cannot send cached DNS response: %s",
433 if (err != len || (dns_len != (len - 2) && protocol == IPPROTO_TCP) ||
434 (dns_len != len && protocol == IPPROTO_UDP))
435 DBG("Packet length mismatch, sent %d wanted %d dns %d",
439 static void send_response(int sk, unsigned char *buf, int len,
440 const struct sockaddr *to, socklen_t tolen,
443 struct domain_hdr *hdr;
444 int err, offset = protocol_offset(protocol);
454 hdr = (void *) (buf + offset);
456 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
465 err = sendto(sk, buf, len, MSG_NOSIGNAL, to, tolen);
467 connman_error("Failed to send DNS response to %d: %s",
468 sk, strerror(errno));
473 static gboolean request_timeout(gpointer user_data)
475 struct request_data *req = user_data;
476 struct listener_data *ifdata;
478 DBG("id 0x%04x", req->srcid);
483 ifdata = req->ifdata;
485 request_list = g_slist_remove(request_list, req);
488 if (req->resplen > 0 && req->resp != NULL) {
491 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
493 err = sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
494 &req->sa, req->sa_len);
497 } else if (req->request && req->numserv == 0) {
498 struct domain_hdr *hdr;
500 if (req->protocol == IPPROTO_TCP) {
501 hdr = (void *) (req->request + 2);
502 hdr->id = req->srcid;
503 send_response(req->client_sk, req->request,
504 req->request_len, NULL, 0, IPPROTO_TCP);
506 } else if (req->protocol == IPPROTO_UDP) {
509 hdr = (void *) (req->request);
510 hdr->id = req->srcid;
511 sk = g_io_channel_unix_get_fd(
512 ifdata->udp_listener_channel);
513 send_response(sk, req->request, req->request_len,
514 &req->sa, req->sa_len, IPPROTO_UDP);
524 static int append_query(unsigned char *buf, unsigned int size,
525 const char *query, const char *domain)
527 unsigned char *ptr = buf;
530 DBG("query %s domain %s", query, domain);
532 while (query != NULL) {
535 tmp = strchr(query, '.');
541 memcpy(ptr + 1, query, len);
547 memcpy(ptr + 1, query, tmp - query);
548 ptr += tmp - query + 1;
553 while (domain != NULL) {
556 tmp = strchr(domain, '.');
558 len = strlen(domain);
562 memcpy(ptr + 1, domain, len);
568 memcpy(ptr + 1, domain, tmp - domain);
569 ptr += tmp - domain + 1;
579 static gboolean cache_check_is_valid(struct cache_data *data,
585 if (data->cache_until < current_time)
592 * remove stale cached entries so that they can be refreshed
594 static void cache_enforce_validity(struct cache_entry *entry)
596 time_t current_time = time(NULL);
598 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE
600 DBG("cache timeout \"%s\" type A", entry->key);
601 g_free(entry->ipv4->data);
607 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE
609 DBG("cache timeout \"%s\" type AAAA", entry->key);
610 g_free(entry->ipv6->data);
616 static uint16_t cache_check_validity(char *question, uint16_t type,
617 struct cache_entry *entry)
619 time_t current_time = time(NULL);
620 int want_refresh = 0;
623 * if we have a popular entry, we want a refresh instead of
624 * total destruction of the entry.
629 cache_enforce_validity(entry);
633 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE) {
634 DBG("cache %s \"%s\" type A", entry->ipv4 ?
635 "timeout" : "entry missing", question);
638 entry->want_refresh = 1;
641 * We do not remove cache entry if there is still
642 * valid IPv6 entry found in the cache.
644 if (cache_check_is_valid(entry->ipv6, current_time)
645 == FALSE && want_refresh == FALSE) {
646 g_hash_table_remove(cache, question);
653 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE) {
654 DBG("cache %s \"%s\" type AAAA", entry->ipv6 ?
655 "timeout" : "entry missing", question);
658 entry->want_refresh = 1;
660 if (cache_check_is_valid(entry->ipv4, current_time)
661 == FALSE && want_refresh == FALSE) {
662 g_hash_table_remove(cache, question);
672 static struct cache_entry *cache_check(gpointer request, int *qtype, int proto)
675 struct cache_entry *entry;
676 struct domain_question *q;
678 int offset, proto_offset;
683 proto_offset = protocol_offset(proto);
684 if (proto_offset < 0)
687 question = request + proto_offset + 12;
689 offset = strlen(question) + 1;
690 q = (void *) (question + offset);
691 type = ntohs(q->type);
693 /* We only cache either A (1) or AAAA (28) requests */
694 if (type != 1 && type != 28)
697 entry = g_hash_table_lookup(cache, question);
701 type = cache_check_validity(question, type, entry);
710 * Get a label/name from DNS resource record. The function decompresses the
711 * label if necessary. The function does not convert the name to presentation
712 * form. This means that the result string will contain label lengths instead
713 * of dots between labels. We intentionally do not want to convert to dotted
714 * format so that we can cache the wire format string directly.
716 static int get_name(int counter,
717 unsigned char *pkt, unsigned char *start, unsigned char *max,
718 unsigned char *output, int output_max, int *output_len,
719 unsigned char **end, char *name, int *name_len)
723 /* Limit recursion to 10 (this means up to 10 labels in domain name) */
729 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
730 uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
732 if (offset >= max - pkt)
738 return get_name(counter + 1, pkt, pkt + offset, max,
739 output, output_max, output_len, end,
742 unsigned label_len = *p;
744 if (pkt + label_len > max)
747 if (*output_len > output_max)
751 * We need the original name in order to check
752 * if this answer is the correct one.
754 name[(*name_len)++] = label_len;
755 memcpy(name + *name_len, p + 1, label_len + 1);
756 *name_len += label_len;
758 /* We compress the result */
759 output[0] = NS_CMPRSFLGS;
776 static int parse_rr(unsigned char *buf, unsigned char *start,
778 unsigned char *response, unsigned int *response_size,
779 uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
783 struct domain_rr *rr;
785 int name_len = 0, output_len = 0, max_rsp = *response_size;
787 err = get_name(0, buf, start, max, response, max_rsp,
788 &output_len, end, name, &name_len);
794 if ((unsigned int) offset > *response_size)
797 rr = (void *) (*end);
802 *type = ntohs(rr->type);
803 *class = ntohs(rr->class);
804 *ttl = ntohl(rr->ttl);
805 *rdlen = ntohs(rr->rdlen);
810 memcpy(response + offset, *end, sizeof(struct domain_rr));
812 offset += sizeof(struct domain_rr);
813 *end += sizeof(struct domain_rr);
815 if ((unsigned int) (offset + *rdlen) > *response_size)
818 memcpy(response + offset, *end, *rdlen);
822 *response_size = offset + *rdlen;
827 static gboolean check_alias(GSList *aliases, char *name)
831 if (aliases != NULL) {
832 for (list = aliases; list; list = list->next) {
833 int len = strlen((char *)list->data);
834 if (strncmp((char *)list->data, name, len) == 0)
842 static int parse_response(unsigned char *buf, int buflen,
843 char *question, int qlen,
844 uint16_t *type, uint16_t *class, int *ttl,
845 unsigned char *response, unsigned int *response_len,
848 struct domain_hdr *hdr = (void *) buf;
849 struct domain_question *q;
851 uint16_t qdcount = ntohs(hdr->qdcount);
852 uint16_t ancount = ntohs(hdr->ancount);
854 uint16_t qtype, qclass;
855 unsigned char *next = NULL;
856 unsigned int maxlen = *response_len;
857 GSList *aliases = NULL, *list;
858 char name[NS_MAXDNAME + 1];
863 DBG("qr %d qdcount %d", hdr->qr, qdcount);
865 /* We currently only cache responses where question count is 1 */
866 if (hdr->qr != 1 || qdcount != 1)
869 ptr = buf + sizeof(struct domain_hdr);
871 strncpy(question, (char *) ptr, qlen);
872 qlen = strlen(question);
873 ptr += qlen + 1; /* skip \0 */
876 qtype = ntohs(q->type);
878 /* We cache only A and AAAA records */
879 if (qtype != 1 && qtype != 28)
882 qclass = ntohs(q->class);
884 ptr += 2 + 2; /* ptr points now to answers */
891 * We have a bunch of answers (like A, AAAA, CNAME etc) to
892 * A or AAAA question. We traverse the answers and parse the
893 * resource records. Only A and AAAA records are cached, all
894 * the other records in answers are skipped.
896 for (i = 0; i < ancount; i++) {
898 * Get one address at a time to this buffer.
899 * The max size of the answer is
900 * 2 (pointer) + 2 (type) + 2 (class) +
901 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
902 * for A or AAAA record.
903 * For CNAME the size can be bigger.
905 unsigned char rsp[NS_MAXCDNAME];
906 unsigned int rsp_len = sizeof(rsp) - 1;
909 memset(rsp, 0, sizeof(rsp));
911 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
912 type, class, ttl, &rdlen, &next, name);
919 * Now rsp contains compressed or uncompressed resource
920 * record. Next we check if this record answers the question.
921 * The name var contains the uncompressed label.
922 * One tricky bit is the CNAME records as they alias
923 * the name we might be interested in.
927 * Go to next answer if the class is not the one we are
930 if (*class != qclass) {
937 * Try to resolve aliases also, type is CNAME(5).
938 * This is important as otherwise the aliased names would not
939 * be cached at all as the cache would not contain the aliased
942 * If any CNAME is found in DNS packet, then we cache the alias
943 * IP address instead of the question (as the server
944 * said that question has only an alias).
945 * This means in practice that if e.g., ipv6.google.com is
946 * queried, DNS server returns CNAME of that name which is
947 * ipv6.l.google.com. We then cache the address of the CNAME
948 * but return the question name to client. So the alias
949 * status of the name is not saved in cache and thus not
950 * returned to the client. We do not return DNS packets from
951 * cache to client saying that ipv6.google.com is an alias to
952 * ipv6.l.google.com but we return instead a DNS packet that
953 * says ipv6.google.com has address xxx which is in fact the
954 * address of ipv6.l.google.com. For caching purposes this
955 * should not cause any issues.
957 if (*type == 5 && strncmp(question, name, qlen) == 0) {
959 * So now the alias answered the question. This is
960 * not very useful from caching point of view as
961 * the following A or AAAA records will not match the
962 * question. We need to find the real A/AAAA record
963 * of the alias and cache that.
965 unsigned char *end = NULL;
966 int name_len = 0, output_len;
968 memset(rsp, 0, sizeof(rsp));
969 rsp_len = sizeof(rsp) - 1;
972 * Alias is in rdata part of the message,
973 * and next-rdlen points to it. So we need to get
974 * the real name of the alias.
976 ret = get_name(0, buf, next - rdlen, buf + buflen,
977 rsp, rsp_len, &output_len, &end,
980 /* just ignore the error at this point */
987 * We should now have the alias of the entry we might
988 * want to cache. Just remember it for a while.
989 * We check the alias list when we have parsed the
992 aliases = g_slist_prepend(aliases, g_strdup(name));
999 if (*type == qtype) {
1001 * We found correct type (A or AAAA)
1003 if (check_alias(aliases, name) == TRUE ||
1004 (aliases == NULL && strncmp(question, name,
1007 * We found an alias or the name of the rr
1008 * matches the question. If so, we append
1009 * the compressed label to the cache.
1010 * The end result is a response buffer that
1011 * will contain one or more cached and
1012 * compressed resource records.
1014 if (*response_len + rsp_len > maxlen) {
1018 memcpy(response + *response_len, rsp, rsp_len);
1019 *response_len += rsp_len;
1030 for (list = aliases; list; list = list->next)
1032 g_slist_free(aliases);
1037 struct cache_timeout {
1038 time_t current_time;
1043 static gboolean cache_check_entry(gpointer key, gpointer value,
1046 struct cache_timeout *data = user_data;
1047 struct cache_entry *entry = value;
1050 /* Scale the number of hits by half as part of cache aging */
1055 * If either IPv4 or IPv6 cached entry has expired, we
1056 * remove both from the cache.
1059 if (entry->ipv4 != NULL && entry->ipv4->timeout > 0) {
1060 max_timeout = entry->ipv4->cache_until;
1061 if (max_timeout > data->max_timeout)
1062 data->max_timeout = max_timeout;
1064 if (entry->ipv4->cache_until < data->current_time)
1068 if (entry->ipv6 != NULL && entry->ipv6->timeout > 0) {
1069 max_timeout = entry->ipv6->cache_until;
1070 if (max_timeout > data->max_timeout)
1071 data->max_timeout = max_timeout;
1073 if (entry->ipv6->cache_until < data->current_time)
1078 * if we're asked to try harder, also remove entries that have
1081 if (data->try_harder && entry->hits < 4)
1087 static void cache_cleanup(void)
1089 static int max_timeout;
1090 struct cache_timeout data;
1093 data.current_time = time(NULL);
1094 data.max_timeout = 0;
1095 data.try_harder = 0;
1098 * In the first pass, we only remove entries that have timed out.
1099 * We use a cache of the first time to expire to do this only
1100 * when it makes sense.
1102 if (max_timeout <= data.current_time) {
1103 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1106 DBG("removed %d in the first pass", count);
1109 * In the second pass, if the first pass turned up blank,
1110 * we also expire entries with a low hit count,
1111 * while aging the hit count at the same time.
1113 data.try_harder = 1;
1115 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1120 * If we could not remove anything, then remember
1121 * what is the max timeout and do nothing if we
1122 * have not yet reached it. This will prevent
1123 * constant traversal of the cache if it is full.
1125 max_timeout = data.max_timeout;
1130 static gboolean cache_invalidate_entry(gpointer key, gpointer value,
1133 struct cache_entry *entry = value;
1135 /* first, delete any expired elements */
1136 cache_enforce_validity(entry);
1138 /* if anything is not expired, mark the entry for refresh */
1139 if (entry->hits > 0 && (entry->ipv4 || entry->ipv6))
1140 entry->want_refresh = 1;
1142 /* delete the cached data */
1144 g_free(entry->ipv4->data);
1145 g_free(entry->ipv4);
1150 g_free(entry->ipv6->data);
1151 g_free(entry->ipv6);
1155 /* keep the entry if we want it refreshed, delete it otherwise */
1156 if (entry->want_refresh)
1163 * cache_invalidate is called from places where the DNS landscape
1164 * has changed, say because connections are added or we entered a VPN.
1165 * The logic is to wipe all cache data, but mark all non-expired
1166 * parts of the cache for refresh rather than deleting the whole cache.
1168 static void cache_invalidate(void)
1170 DBG("Invalidating the DNS cache %p", cache);
1175 g_hash_table_foreach_remove(cache, cache_invalidate_entry, NULL);
1178 static void cache_refresh_entry(struct cache_entry *entry)
1181 cache_enforce_validity(entry);
1183 if (entry->hits > 2 && entry->ipv4 == NULL)
1184 entry->want_refresh = 1;
1185 if (entry->hits > 2 && entry->ipv6 == NULL)
1186 entry->want_refresh = 1;
1188 if (entry->want_refresh) {
1190 char dns_name[NS_MAXDNAME + 1];
1191 entry->want_refresh = 0;
1193 /* turn a DNS name into a hostname with dots */
1194 strncpy(dns_name, entry->key, NS_MAXDNAME);
1202 DBG("Refreshing %s\n", dns_name);
1203 /* then refresh the hostname */
1204 refresh_dns_entry(entry, &dns_name[1]);
1208 static void cache_refresh_iterator(gpointer key, gpointer value,
1211 struct cache_entry *entry = value;
1213 cache_refresh_entry(entry);
1216 static void cache_refresh(void)
1221 g_hash_table_foreach(cache, cache_refresh_iterator, NULL);
1224 static int reply_query_type(unsigned char *msg, int len)
1231 /* skip the header */
1232 c = msg + sizeof(struct domain_hdr);
1233 len -= sizeof(struct domain_hdr);
1238 /* now the query, which is a name and 2 16 bit words */
1239 l = dns_name_length(c) + 1;
1247 static int cache_update(struct server_data *srv, unsigned char *msg,
1248 unsigned int msg_len)
1250 int offset = protocol_offset(srv->protocol);
1251 int err, qlen, ttl = 0;
1252 uint16_t answers = 0, type = 0, class = 0;
1253 struct domain_hdr *hdr = (void *)(msg + offset);
1254 struct domain_question *q;
1255 struct cache_entry *entry;
1256 struct cache_data *data;
1257 char question[NS_MAXDNAME + 1];
1258 unsigned char response[NS_MAXDNAME + 1];
1260 unsigned int rsplen;
1261 gboolean new_entry = TRUE;
1262 time_t current_time;
1264 if (cache_size >= MAX_CACHE_SIZE) {
1266 if (cache_size >= MAX_CACHE_SIZE)
1270 current_time = time(NULL);
1272 /* don't do a cache refresh more than twice a minute */
1273 if (next_refresh < current_time) {
1275 next_refresh = current_time + 30;
1281 DBG("offset %d hdr %p msg %p rcode %d", offset, hdr, msg, hdr->rcode);
1283 /* Continue only if response code is 0 (=ok) */
1284 if (hdr->rcode != 0)
1287 rsplen = sizeof(response) - 1;
1288 question[sizeof(question) - 1] = '\0';
1290 err = parse_response(msg + offset, msg_len - offset,
1291 question, sizeof(question) - 1,
1292 &type, &class, &ttl,
1293 response, &rsplen, &answers);
1296 * special case: if we do a ipv6 lookup and get no result
1297 * for a record that's already in our ipv4 cache.. we want
1298 * to cache the negative response.
1300 if ((err == -ENOMSG || err == -ENOBUFS) &&
1301 reply_query_type(msg + offset,
1302 msg_len - offset) == 28) {
1303 entry = g_hash_table_lookup(cache, question);
1304 if (entry && entry->ipv4 && entry->ipv6 == NULL) {
1305 int cache_offset = 0;
1307 data = g_try_new(struct cache_data, 1);
1310 data->inserted = entry->ipv4->inserted;
1312 data->answers = hdr->ancount;
1313 data->timeout = entry->ipv4->timeout;
1314 if (srv->protocol == IPPROTO_UDP)
1316 data->data_len = msg_len + cache_offset;
1317 data->data = ptr = g_malloc(data->data_len);
1318 ptr[0] = (data->data_len - 2) / 256;
1319 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1320 if (srv->protocol == IPPROTO_UDP)
1322 data->valid_until = entry->ipv4->valid_until;
1323 data->cache_until = entry->ipv4->cache_until;
1324 memcpy(ptr, msg, msg_len);
1327 * we will get a "hit" when we serve the response
1331 if (entry->hits < 0)
1337 if (err < 0 || ttl == 0)
1340 qlen = strlen(question);
1343 * If the cache contains already data, check if the
1344 * type of the cached data is the same and do not add
1345 * to cache if data is already there.
1346 * This is needed so that we can cache both A and AAAA
1347 * records for the same name.
1349 entry = g_hash_table_lookup(cache, question);
1350 if (entry == NULL) {
1351 entry = g_try_new(struct cache_entry, 1);
1355 data = g_try_new(struct cache_data, 1);
1361 entry->key = g_strdup(question);
1362 entry->ipv4 = entry->ipv6 = NULL;
1363 entry->want_refresh = 0;
1371 if (type == 1 && entry->ipv4 != NULL)
1374 if (type == 28 && entry->ipv6 != NULL)
1377 data = g_try_new(struct cache_data, 1);
1387 * compensate for the hit we'll get for serving
1388 * the response out of the cache
1391 if (entry->hits < 0)
1397 if (ttl < MIN_CACHE_TTL)
1398 ttl = MIN_CACHE_TTL;
1400 data->inserted = current_time;
1402 data->answers = answers;
1403 data->timeout = ttl;
1405 * The "2" in start of the length is the TCP offset. We allocate it
1406 * here even for UDP packet because it simplifies the sending
1409 data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
1410 data->data = ptr = g_malloc(data->data_len);
1411 data->valid_until = current_time + ttl;
1414 * Restrict the cached DNS record TTL to some sane value
1415 * in order to prevent data staying in the cache too long.
1417 if (ttl > MAX_CACHE_TTL)
1418 ttl = MAX_CACHE_TTL;
1420 data->cache_until = round_down_ttl(current_time + ttl, ttl);
1422 if (data->data == NULL) {
1430 * We cache the two extra bytes at the start of the message
1431 * in a TCP packet. When sending UDP packet, we skip the first
1432 * two bytes. This way we do not need to know the format
1433 * (UDP/TCP) of the cached message.
1435 ptr[0] = (data->data_len - 2) / 256;
1436 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1437 if (srv->protocol == IPPROTO_UDP)
1440 memcpy(ptr, msg, offset + 12);
1441 memcpy(ptr + offset + 12, question, qlen + 1); /* copy also the \0 */
1443 q = (void *) (ptr + offset + 12 + qlen + 1);
1444 q->type = htons(type);
1445 q->class = htons(class);
1446 memcpy(ptr + offset + 12 + qlen + 1 + sizeof(struct domain_question),
1449 if (new_entry == TRUE) {
1450 g_hash_table_replace(cache, entry->key, entry);
1454 DBG("cache %d %squestion \"%s\" type %d ttl %d size %zd packet %u "
1456 cache_size, new_entry ? "new " : "old ",
1457 question, type, ttl,
1458 sizeof(*entry) + sizeof(*data) + data->data_len + qlen,
1460 srv->protocol == IPPROTO_TCP ?
1461 (unsigned int)(data->data[0] * 256 + data->data[1]) :
1467 static int ns_resolv(struct server_data *server, struct request_data *req,
1468 gpointer request, gpointer name)
1471 int sk, err, type = 0;
1472 char *dot, *lookup = (char *) name;
1473 struct cache_entry *entry;
1475 entry = cache_check(request, &type, req->protocol);
1476 if (entry != NULL) {
1478 struct cache_data *data;
1480 DBG("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1487 ttl_left = data->valid_until - time(NULL);
1491 if (data != NULL && req->protocol == IPPROTO_TCP) {
1492 send_cached_response(req->client_sk, data->data,
1493 data->data_len, NULL, 0, IPPROTO_TCP,
1494 req->srcid, data->answers, ttl_left);
1498 if (data != NULL && req->protocol == IPPROTO_UDP) {
1500 sk = g_io_channel_unix_get_fd(
1501 req->ifdata->udp_listener_channel);
1503 send_cached_response(sk, data->data,
1504 data->data_len, &req->sa, req->sa_len,
1505 IPPROTO_UDP, req->srcid, data->answers,
1511 sk = g_io_channel_unix_get_fd(server->channel);
1513 err = send(sk, request, req->request_len, MSG_NOSIGNAL);
1519 /* If we have more than one dot, we don't add domains */
1520 dot = strchr(lookup, '.');
1521 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
1524 if (server->domains != NULL && server->domains->data != NULL)
1525 req->append_domain = TRUE;
1527 for (list = server->domains; list; list = list->next) {
1529 unsigned char alt[1024];
1530 struct domain_hdr *hdr = (void *) &alt;
1531 int altlen, domlen, offset;
1533 domain = list->data;
1538 offset = protocol_offset(server->protocol);
1542 domlen = strlen(domain) + 1;
1546 alt[offset] = req->altid & 0xff;
1547 alt[offset + 1] = req->altid >> 8;
1549 memcpy(alt + offset + 2, request + offset + 2, 10);
1550 hdr->qdcount = htons(1);
1552 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1559 memcpy(alt + offset + altlen,
1560 request + offset + altlen - domlen,
1561 req->request_len - altlen - offset + domlen);
1563 if (server->protocol == IPPROTO_TCP) {
1564 int req_len = req->request_len + domlen - 2;
1566 alt[0] = (req_len >> 8) & 0xff;
1567 alt[1] = req_len & 0xff;
1570 err = send(sk, alt, req->request_len + domlen, MSG_NOSIGNAL);
1580 static void destroy_request_data(struct request_data *req)
1582 if (req->timeout > 0)
1583 g_source_remove(req->timeout);
1586 g_free(req->request);
1591 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1592 struct server_data *data)
1594 struct domain_hdr *hdr;
1595 struct request_data *req;
1596 int dns_id, sk, err, offset = protocol_offset(protocol);
1597 struct listener_data *ifdata;
1602 hdr = (void *)(reply + offset);
1603 dns_id = reply[offset] | reply[offset + 1] << 8;
1605 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1607 req = find_request(dns_id);
1611 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
1613 ifdata = req->ifdata;
1615 reply[offset] = req->srcid & 0xff;
1616 reply[offset + 1] = req->srcid >> 8;
1620 if (hdr->rcode == 0 || req->resp == NULL) {
1623 * If the domain name was append
1624 * remove it before forwarding the reply.
1626 if (req->append_domain == TRUE) {
1629 unsigned int domain_len;
1632 * ptr points to the first char of the hostname.
1633 * ->hostname.domain.net
1635 ptr = reply + offset + sizeof(struct domain_hdr);
1637 domain_len = strlen((const char *)ptr + host_len + 1);
1640 * Remove the domain name and replace it by the end
1641 * of reply. Check if the domain is really there
1642 * before trying to copy the data. The domain_len can
1643 * be 0 because if the original query did not contain
1644 * a domain name, then we are sending two packets,
1645 * first without the domain name and the second packet
1646 * with domain name. The append_domain is set to true
1647 * even if we sent the first packet without domain
1648 * name. In this case we end up in this branch.
1650 if (domain_len > 0) {
1652 * Note that we must use memmove() here,
1653 * because the memory areas can overlap.
1655 memmove(ptr + host_len + 1,
1656 ptr + host_len + domain_len + 1,
1657 reply_len - (ptr - reply + domain_len));
1659 reply_len = reply_len - domain_len;
1666 req->resp = g_try_malloc(reply_len);
1667 if (req->resp == NULL)
1670 memcpy(req->resp, reply, reply_len);
1671 req->resplen = reply_len;
1673 cache_update(data, reply, reply_len);
1676 if (hdr->rcode > 0 && req->numresp < req->numserv)
1679 request_list = g_slist_remove(request_list, req);
1681 if (protocol == IPPROTO_UDP) {
1682 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
1683 err = sendto(sk, req->resp, req->resplen, 0,
1684 &req->sa, req->sa_len);
1686 sk = req->client_sk;
1687 err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
1692 DBG("Cannot send msg, sk %d proto %d errno %d/%s", sk,
1693 protocol, errno, strerror(errno));
1695 DBG("proto %d sent %d bytes to %d", protocol, err, sk);
1697 destroy_request_data(req);
1702 static void cache_element_destroy(gpointer value)
1704 struct cache_entry *entry = value;
1709 if (entry->ipv4 != NULL) {
1710 g_free(entry->ipv4->data);
1711 g_free(entry->ipv4);
1714 if (entry->ipv6 != NULL) {
1715 g_free(entry->ipv6->data);
1716 g_free(entry->ipv6);
1722 if (--cache_size < 0)
1726 static gboolean try_remove_cache(gpointer user_data)
1728 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1) {
1729 DBG("No cache users, removing it.");
1731 g_hash_table_destroy(cache);
1738 static void destroy_server(struct server_data *server)
1742 DBG("interface %s server %s sock %d", server->interface, server->server,
1743 g_io_channel_unix_get_fd(server->channel));
1745 server_list = g_slist_remove(server_list, server);
1747 if (server->watch > 0)
1748 g_source_remove(server->watch);
1750 if (server->timeout > 0)
1751 g_source_remove(server->timeout);
1753 g_io_channel_unref(server->channel);
1755 if (server->protocol == IPPROTO_UDP)
1756 DBG("Removing DNS server %s", server->server);
1758 g_free(server->incoming_reply);
1759 g_free(server->server);
1760 for (list = server->domains; list; list = list->next) {
1761 char *domain = list->data;
1763 server->domains = g_list_remove(server->domains, domain);
1766 g_free(server->interface);
1769 * We do not remove cache right away but delay it few seconds.
1770 * The idea is that when IPv6 DNS server is added via RDNSS, it has a
1771 * lifetime. When the lifetime expires we decrease the refcount so it
1772 * is possible that the cache is then removed. Because a new DNS server
1773 * is usually created almost immediately we would then loose the cache
1774 * without any good reason. The small delay allows the new RDNSS to
1775 * create a new DNS server instance and the refcount does not go to 0.
1777 g_timeout_add_seconds(3, try_remove_cache, NULL);
1782 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
1785 unsigned char buf[4096];
1787 struct server_data *data = user_data;
1789 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1790 connman_error("Error with UDP server %s", data->server);
1795 sk = g_io_channel_unix_get_fd(channel);
1797 len = recv(sk, buf, sizeof(buf), 0);
1801 err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
1808 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
1812 struct server_data *server = user_data;
1814 sk = g_io_channel_unix_get_fd(channel);
1818 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1821 DBG("TCP server channel closed, sk %d", sk);
1824 * Discard any partial response which is buffered; better
1825 * to get a proper response from a working server.
1827 g_free(server->incoming_reply);
1828 server->incoming_reply = NULL;
1830 for (list = request_list; list; list = list->next) {
1831 struct request_data *req = list->data;
1832 struct domain_hdr *hdr;
1834 if (req->protocol == IPPROTO_UDP)
1837 if (req->request == NULL)
1841 * If we're not waiting for any further response
1842 * from another name server, then we send an error
1843 * response to the client.
1845 if (req->numserv && --(req->numserv))
1848 hdr = (void *) (req->request + 2);
1849 hdr->id = req->srcid;
1850 send_response(req->client_sk, req->request,
1851 req->request_len, NULL, 0, IPPROTO_TCP);
1853 request_list = g_slist_remove(request_list, req);
1856 destroy_server(server);
1861 if ((condition & G_IO_OUT) && !server->connected) {
1864 int no_request_sent = TRUE;
1865 struct server_data *udp_server;
1867 udp_server = find_server(server->interface, server->server,
1869 if (udp_server != NULL) {
1870 for (domains = udp_server->domains; domains;
1871 domains = domains->next) {
1872 char *dom = domains->data;
1874 DBG("Adding domain %s to %s",
1875 dom, server->server);
1877 server->domains = g_list_append(server->domains,
1882 server->connected = TRUE;
1883 server_list = g_slist_append(server_list, server);
1885 if (server->timeout > 0) {
1886 g_source_remove(server->timeout);
1887 server->timeout = 0;
1890 for (list = request_list; list; ) {
1891 struct request_data *req = list->data;
1894 if (req->protocol == IPPROTO_UDP) {
1899 DBG("Sending req %s over TCP", (char *)req->name);
1901 status = ns_resolv(server, req,
1902 req->request, req->name);
1905 * A cached result was sent,
1906 * so the request can be released
1909 request_list = g_slist_remove(request_list, req);
1910 destroy_request_data(req);
1919 no_request_sent = FALSE;
1921 if (req->timeout > 0)
1922 g_source_remove(req->timeout);
1924 req->timeout = g_timeout_add_seconds(30,
1925 request_timeout, req);
1929 if (no_request_sent == TRUE) {
1930 destroy_server(server);
1934 } else if (condition & G_IO_IN) {
1935 struct partial_reply *reply = server->incoming_reply;
1939 unsigned char reply_len_buf[2];
1942 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
1945 } else if (bytes_recv < 0) {
1946 if (errno == EAGAIN || errno == EWOULDBLOCK)
1949 connman_error("DNS proxy error %s",
1952 } else if (bytes_recv < 2)
1955 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
1958 DBG("TCP reply %d bytes from %d", reply_len, sk);
1960 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
1964 reply->len = reply_len;
1965 reply->received = 0;
1967 server->incoming_reply = reply;
1970 while (reply->received < reply->len) {
1971 bytes_recv = recv(sk, reply->buf + reply->received,
1972 reply->len - reply->received, 0);
1974 connman_error("DNS proxy TCP disconnect");
1976 } else if (bytes_recv < 0) {
1977 if (errno == EAGAIN || errno == EWOULDBLOCK)
1980 connman_error("DNS proxy error %s",
1984 reply->received += bytes_recv;
1987 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
1991 server->incoming_reply = NULL;
1993 destroy_server(server);
2001 static gboolean tcp_idle_timeout(gpointer user_data)
2003 struct server_data *server = user_data;
2010 destroy_server(server);
2015 static struct server_data *create_server(const char *interface,
2016 const char *domain, const char *server,
2019 struct addrinfo hints, *rp;
2020 struct server_data *data;
2023 DBG("interface %s server %s", interface, server);
2025 memset(&hints, 0, sizeof(hints));
2029 hints.ai_socktype = SOCK_DGRAM;
2033 hints.ai_socktype = SOCK_STREAM;
2039 hints.ai_family = AF_UNSPEC;
2040 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
2042 ret = getaddrinfo(server, "53", &hints, &rp);
2044 connman_error("Failed to parse server %s address: %s\n",
2045 server, gai_strerror(ret));
2048 /* Do not blindly copy this code elsewhere; it doesn't loop over the
2049 results using ->ai_next as it should. That's OK in *this* case
2050 because it was a numeric lookup; we *know* there's only one. */
2052 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
2054 connman_error("Failed to create server %s socket", server);
2061 if (interface != NULL) {
2062 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2063 interface, strlen(interface) + 1) < 0) {
2064 connman_error("Failed to bind server %s "
2073 data = g_try_new0(struct server_data, 1);
2075 connman_error("Failed to allocate server %s data", server);
2081 data->channel = g_io_channel_unix_new(sk);
2082 if (data->channel == NULL) {
2083 connman_error("Failed to create server %s channel", server);
2090 g_io_channel_set_close_on_unref(data->channel, TRUE);
2092 if (protocol == IPPROTO_TCP) {
2093 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
2094 data->watch = g_io_add_watch(data->channel,
2095 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
2096 tcp_server_event, data);
2097 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
2100 data->watch = g_io_add_watch(data->channel,
2101 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2102 udp_server_event, data);
2104 data->interface = g_strdup(interface);
2106 data->domains = g_list_append(data->domains, g_strdup(domain));
2107 data->server = g_strdup(server);
2108 data->protocol = protocol;
2110 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
2113 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
2114 protocol == IPPROTO_UDP) {
2117 connman_error("Failed to connect to server %s", server);
2118 if (data->watch > 0)
2119 g_source_remove(data->watch);
2120 if (data->timeout > 0)
2121 g_source_remove(data->timeout);
2123 g_io_channel_unref(data->channel);
2126 g_free(data->server);
2127 g_free(data->interface);
2128 for (list = data->domains; list; list = list->next) {
2129 char *domain = list->data;
2131 data->domains = g_list_remove(data->domains,
2140 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
2141 cache = g_hash_table_new_full(g_str_hash,
2144 cache_element_destroy);
2146 if (protocol == IPPROTO_UDP) {
2147 /* Enable new servers by default */
2148 data->enabled = TRUE;
2149 DBG("Adding DNS server %s", data->server);
2151 server_list = g_slist_append(server_list, data);
2157 static gboolean resolv(struct request_data *req,
2158 gpointer request, gpointer name)
2162 for (list = server_list; list; list = list->next) {
2163 struct server_data *data = list->data;
2165 DBG("server %s enabled %d", data->server, data->enabled);
2167 if (data->enabled == FALSE)
2170 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
2171 data->watch = g_io_add_watch(data->channel,
2172 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2173 udp_server_event, data);
2175 if (ns_resolv(data, req, request, name) > 0)
2182 static void append_domain(const char *interface, const char *domain)
2186 DBG("interface %s domain %s", interface, domain);
2191 for (list = server_list; list; list = list->next) {
2192 struct server_data *data = list->data;
2195 gboolean dom_found = FALSE;
2197 if (data->interface == NULL)
2200 if (g_str_equal(data->interface, interface) == FALSE)
2203 for (dom_list = data->domains; dom_list;
2204 dom_list = dom_list->next) {
2205 dom = dom_list->data;
2207 if (g_str_equal(dom, domain)) {
2213 if (dom_found == FALSE) {
2215 g_list_append(data->domains, g_strdup(domain));
2220 int __connman_dnsproxy_append(const char *interface, const char *domain,
2223 struct server_data *data;
2225 DBG("interface %s server %s", interface, server);
2227 if (server == NULL && domain == NULL)
2230 if (server == NULL) {
2231 append_domain(interface, domain);
2236 if (g_str_equal(server, "127.0.0.1") == TRUE)
2239 data = find_server(interface, server, IPPROTO_UDP);
2241 append_domain(interface, domain);
2245 data = create_server(interface, domain, server, IPPROTO_UDP);
2252 static void remove_server(const char *interface, const char *domain,
2253 const char *server, int protocol)
2255 struct server_data *data;
2257 data = find_server(interface, server, protocol);
2261 destroy_server(data);
2264 int __connman_dnsproxy_remove(const char *interface, const char *domain,
2267 DBG("interface %s server %s", interface, server);
2272 if (g_str_equal(server, "127.0.0.1") == TRUE)
2275 remove_server(interface, domain, server, IPPROTO_UDP);
2276 remove_server(interface, domain, server, IPPROTO_TCP);
2281 void __connman_dnsproxy_flush(void)
2285 list = request_list;
2287 struct request_data *req = list->data;
2291 if (resolv(req, req->request, req->name) == TRUE) {
2293 * A cached result was sent,
2294 * so the request can be released
2297 g_slist_remove(request_list, req);
2298 destroy_request_data(req);
2302 if (req->timeout > 0)
2303 g_source_remove(req->timeout);
2304 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2308 static void dnsproxy_offline_mode(connman_bool_t enabled)
2312 DBG("enabled %d", enabled);
2314 for (list = server_list; list; list = list->next) {
2315 struct server_data *data = list->data;
2317 if (enabled == FALSE) {
2318 DBG("Enabling DNS server %s", data->server);
2319 data->enabled = TRUE;
2323 DBG("Disabling DNS server %s", data->server);
2324 data->enabled = FALSE;
2330 static void dnsproxy_default_changed(struct connman_service *service)
2335 DBG("service %p", service);
2337 /* DNS has changed, invalidate the cache */
2340 if (service == NULL) {
2341 /* When no services are active, then disable DNS proxying */
2342 dnsproxy_offline_mode(TRUE);
2346 interface = connman_service_get_interface(service);
2347 if (interface == NULL)
2350 for (list = server_list; list; list = list->next) {
2351 struct server_data *data = list->data;
2353 if (g_strcmp0(data->interface, interface) == 0) {
2354 DBG("Enabling DNS server %s", data->server);
2355 data->enabled = TRUE;
2357 DBG("Disabling DNS server %s", data->server);
2358 data->enabled = FALSE;
2366 static struct connman_notifier dnsproxy_notifier = {
2368 .default_changed = dnsproxy_default_changed,
2369 .offline_mode = dnsproxy_offline_mode,
2372 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2374 static int parse_request(unsigned char *buf, int len,
2375 char *name, unsigned int size)
2377 struct domain_hdr *hdr = (void *) buf;
2378 uint16_t qdcount = ntohs(hdr->qdcount);
2379 uint16_t arcount = ntohs(hdr->arcount);
2381 char *last_label = NULL;
2382 unsigned int remain, used = 0;
2387 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2388 hdr->id, hdr->qr, hdr->opcode,
2391 if (hdr->qr != 0 || qdcount != 1)
2396 ptr = buf + sizeof(struct domain_hdr);
2397 remain = len - sizeof(struct domain_hdr);
2399 while (remain > 0) {
2403 last_label = (char *) (ptr + 1);
2407 if (used + len + 1 > size)
2410 strncat(name, (char *) (ptr + 1), len);
2419 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
2420 !memcmp(last_label + 5, opt_edns0_type, 2)) {
2421 uint16_t edns0_bufsize;
2423 edns0_bufsize = last_label[7] << 8 | last_label[8];
2425 DBG("EDNS0 buffer size %u", edns0_bufsize);
2427 /* This is an evil hack until full TCP support has been
2430 * Somtimes the EDNS0 request gets send with a too-small
2431 * buffer size. Since glibc doesn't seem to crash when it
2432 * gets a response biffer then it requested, just bump
2433 * the buffer size up to 4KiB.
2435 if (edns0_bufsize < 0x1000) {
2436 last_label[7] = 0x10;
2437 last_label[8] = 0x00;
2441 DBG("query %s", name);
2446 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
2449 unsigned char buf[768];
2451 struct request_data *req;
2452 int sk, client_sk, len, err;
2453 struct sockaddr_in6 client_addr;
2454 socklen_t client_addr_len = sizeof(client_addr);
2456 struct listener_data *ifdata = user_data;
2457 int waiting_for_connect = FALSE, qtype = 0;
2458 struct cache_entry *entry;
2460 DBG("condition 0x%x", condition);
2462 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2463 if (ifdata->tcp_listener_watch > 0)
2464 g_source_remove(ifdata->tcp_listener_watch);
2465 ifdata->tcp_listener_watch = 0;
2467 connman_error("Error with TCP listener channel");
2472 sk = g_io_channel_unix_get_fd(channel);
2474 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
2475 if (client_sk < 0) {
2476 connman_error("Accept failure on TCP listener");
2477 ifdata->tcp_listener_watch = 0;
2481 len = recv(client_sk, buf, sizeof(buf), 0);
2485 DBG("Received %d bytes (id 0x%04x) from %d", len,
2486 buf[2] | buf[3] << 8, client_sk);
2488 err = parse_request(buf + 2, len - 2, query, sizeof(query));
2489 if (err < 0 || (g_slist_length(server_list) == 0)) {
2490 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2494 req = g_try_new0(struct request_data, 1);
2498 memcpy(&req->sa, &client_addr, client_addr_len);
2499 req->sa_len = client_addr_len;
2500 req->client_sk = client_sk;
2501 req->protocol = IPPROTO_TCP;
2503 req->srcid = buf[2] | (buf[3] << 8);
2504 req->dstid = get_id();
2505 req->altid = get_id();
2506 req->request_len = len;
2508 buf[2] = req->dstid & 0xff;
2509 buf[3] = req->dstid >> 8;
2512 req->ifdata = (struct listener_data *) ifdata;
2513 req->append_domain = FALSE;
2516 * Check if the answer is found in the cache before
2517 * creating sockets to the server.
2519 entry = cache_check(buf, &qtype, IPPROTO_TCP);
2520 if (entry != NULL) {
2522 struct cache_data *data;
2524 DBG("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
2531 ttl_left = data->valid_until - time(NULL);
2534 send_cached_response(client_sk, data->data,
2535 data->data_len, NULL, 0, IPPROTO_TCP,
2536 req->srcid, data->answers, ttl_left);
2541 DBG("data missing, ignoring cache for this query");
2544 for (list = server_list; list; list = list->next) {
2545 struct server_data *data = list->data;
2547 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
2550 if(create_server(data->interface, NULL,
2551 data->server, IPPROTO_TCP) == NULL)
2554 waiting_for_connect = TRUE;
2557 if (waiting_for_connect == FALSE) {
2558 /* No server is waiting for connect */
2559 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2565 * The server is not connected yet.
2566 * Copy the relevant buffers.
2567 * The request will actually be sent once we're
2568 * properly connected over TCP to the nameserver.
2570 req->request = g_try_malloc0(req->request_len);
2571 if (req->request == NULL) {
2572 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2576 memcpy(req->request, buf, req->request_len);
2578 req->name = g_try_malloc0(sizeof(query));
2579 if (req->name == NULL) {
2580 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2581 g_free(req->request);
2585 memcpy(req->name, query, sizeof(query));
2587 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
2589 request_list = g_slist_append(request_list, req);
2594 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
2597 unsigned char buf[768];
2599 struct request_data *req;
2600 struct sockaddr_in6 client_addr;
2601 socklen_t client_addr_len = sizeof(client_addr);
2603 struct listener_data *ifdata = user_data;
2605 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2606 connman_error("Error with UDP listener channel");
2607 ifdata->udp_listener_watch = 0;
2611 sk = g_io_channel_unix_get_fd(channel);
2613 memset(&client_addr, 0, client_addr_len);
2614 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
2619 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
2621 err = parse_request(buf, len, query, sizeof(query));
2622 if (err < 0 || (g_slist_length(server_list) == 0)) {
2623 send_response(sk, buf, len, (void *)&client_addr,
2624 client_addr_len, IPPROTO_UDP);
2628 req = g_try_new0(struct request_data, 1);
2632 memcpy(&req->sa, &client_addr, client_addr_len);
2633 req->sa_len = client_addr_len;
2635 req->protocol = IPPROTO_UDP;
2637 req->srcid = buf[0] | (buf[1] << 8);
2638 req->dstid = get_id();
2639 req->altid = get_id();
2640 req->request_len = len;
2642 buf[0] = req->dstid & 0xff;
2643 buf[1] = req->dstid >> 8;
2646 req->ifdata = (struct listener_data *) ifdata;
2647 req->append_domain = FALSE;
2649 if (resolv(req, buf, query) == TRUE) {
2650 /* a cached result was sent, so the request can be released */
2655 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2656 request_list = g_slist_append(request_list, req);
2661 static int create_dns_listener(int protocol, struct listener_data *ifdata)
2663 GIOChannel *channel;
2667 struct sockaddr_in6 sin6;
2668 struct sockaddr_in sin;
2671 int sk, type, v6only = 0;
2672 int family = AF_INET6;
2675 DBG("interface %s", ifdata->ifname);
2680 type = SOCK_DGRAM | SOCK_CLOEXEC;
2685 type = SOCK_STREAM | SOCK_CLOEXEC;
2692 sk = socket(family, type, protocol);
2693 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
2694 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
2696 sk = socket(family, type, protocol);
2699 connman_error("Failed to create %s listener socket", proto);
2703 #if !defined TIZEN_EXT
2704 /* ConnMan listens DNS from multiple interfaces
2705 * E.g. various technology based and tethered interfaces
2707 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2709 strlen(ifdata->ifname) + 1) < 0) {
2710 connman_error("Failed to bind %s listener interface", proto);
2715 /* Ensure it accepts Legacy IP connections too */
2716 if (family == AF_INET6 &&
2717 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
2718 &v6only, sizeof(v6only)) < 0) {
2719 connman_error("Failed to clear V6ONLY on %s listener socket",
2725 if (family == AF_INET) {
2726 memset(&s.sin, 0, sizeof(s.sin));
2727 s.sin.sin_family = AF_INET;
2728 s.sin.sin_port = htons(53);
2729 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
2730 slen = sizeof(s.sin);
2732 memset(&s.sin6, 0, sizeof(s.sin6));
2733 s.sin6.sin6_family = AF_INET6;
2734 s.sin6.sin6_port = htons(53);
2735 s.sin6.sin6_addr = in6addr_any;
2736 slen = sizeof(s.sin6);
2739 #if defined TIZEN_EXT
2740 if (smack_fsetlabel(sk, "system::use_internet", SMACK_LABEL_IPOUT) != 0)
2741 connman_error("Failed to label system::user_internet");
2743 if (smack_fsetlabel(sk, "system::use_internet", SMACK_LABEL_IPIN) != 0)
2744 connman_error("Failed to label system::user_internet");
2746 if (bind(sk, &s.sa, slen) < 0) {
2747 connman_error("Failed to bind %s listener socket", proto);
2752 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
2753 connman_error("Failed to listen on TCP socket");
2758 channel = g_io_channel_unix_new(sk);
2759 if (channel == NULL) {
2760 connman_error("Failed to create %s listener channel", proto);
2765 g_io_channel_set_close_on_unref(channel, TRUE);
2767 if (protocol == IPPROTO_TCP) {
2768 ifdata->tcp_listener_channel = channel;
2769 ifdata->tcp_listener_watch = g_io_add_watch(channel,
2770 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
2772 ifdata->udp_listener_channel = channel;
2773 ifdata->udp_listener_watch = g_io_add_watch(channel,
2774 G_IO_IN, udp_listener_event, (gpointer) ifdata);
2780 static void destroy_udp_listener(struct listener_data *ifdata)
2782 DBG("interface %s", ifdata->ifname);
2784 if (ifdata->udp_listener_watch > 0)
2785 g_source_remove(ifdata->udp_listener_watch);
2787 g_io_channel_unref(ifdata->udp_listener_channel);
2790 static void destroy_tcp_listener(struct listener_data *ifdata)
2792 DBG("interface %s", ifdata->ifname);
2794 if (ifdata->tcp_listener_watch > 0)
2795 g_source_remove(ifdata->tcp_listener_watch);
2797 g_io_channel_unref(ifdata->tcp_listener_channel);
2800 static int create_listener(struct listener_data *ifdata)
2804 err = create_dns_listener(IPPROTO_UDP, ifdata);
2808 err = create_dns_listener(IPPROTO_TCP, ifdata);
2810 destroy_udp_listener(ifdata);
2814 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2815 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
2820 static void destroy_listener(struct listener_data *ifdata)
2824 if (g_strcmp0(ifdata->ifname, "lo") == 0)
2825 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
2827 for (list = request_list; list; list = list->next) {
2828 struct request_data *req = list->data;
2830 DBG("Dropping request (id 0x%04x -> 0x%04x)",
2831 req->srcid, req->dstid);
2832 destroy_request_data(req);
2836 g_slist_free(request_list);
2837 request_list = NULL;
2839 destroy_tcp_listener(ifdata);
2840 destroy_udp_listener(ifdata);
2843 int __connman_dnsproxy_add_listener(const char *interface)
2845 struct listener_data *ifdata;
2848 DBG("interface %s", interface);
2850 if (g_hash_table_lookup(listener_table, interface) != NULL)
2853 ifdata = g_try_new0(struct listener_data, 1);
2857 ifdata->ifname = g_strdup(interface);
2858 ifdata->udp_listener_channel = NULL;
2859 ifdata->udp_listener_watch = 0;
2860 ifdata->tcp_listener_channel = NULL;
2861 ifdata->tcp_listener_watch = 0;
2863 err = create_listener(ifdata);
2865 connman_error("Couldn't create listener for %s err %d",
2867 g_free(ifdata->ifname);
2871 g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
2875 void __connman_dnsproxy_remove_listener(const char *interface)
2877 struct listener_data *ifdata;
2879 DBG("interface %s", interface);
2881 ifdata = g_hash_table_lookup(listener_table, interface);
2885 destroy_listener(ifdata);
2887 g_hash_table_remove(listener_table, interface);
2890 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
2892 const char *interface = key;
2893 struct listener_data *ifdata = value;
2895 DBG("interface %s", interface);
2897 destroy_listener(ifdata);
2900 int __connman_dnsproxy_init(void)
2906 srandom(time(NULL));
2908 listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2910 err = __connman_dnsproxy_add_listener("lo");
2914 err = connman_notifier_register(&dnsproxy_notifier);
2921 __connman_dnsproxy_remove_listener("lo");
2922 g_hash_table_destroy(listener_table);
2927 void __connman_dnsproxy_cleanup(void)
2931 connman_notifier_unregister(&dnsproxy_notifier);
2933 g_hash_table_foreach(listener_table, remove_listener, NULL);
2935 g_hash_table_destroy(listener_table);