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>
43 #if __BYTE_ORDER == __LITTLE_ENDIAN
58 } __attribute__ ((packed));
59 #elif __BYTE_ORDER == __BIG_ENDIAN
74 } __attribute__ ((packed));
76 #error "Unknown byte order"
79 struct partial_reply {
89 struct sockaddr *server_addr;
90 socklen_t server_addr_len;
97 struct partial_reply *incoming_reply;
100 struct request_data {
102 struct sockaddr_in6 __sin6; /* Only for the length */
120 struct listener_data *ifdata;
121 gboolean append_domain;
124 struct listener_data {
126 GIOChannel *udp_listener_channel;
127 guint udp_listener_watch;
128 GIOChannel *tcp_listener_channel;
129 guint tcp_listener_watch;
139 unsigned int data_len;
140 unsigned char *data; /* contains DNS header + body */
147 struct cache_data *ipv4;
148 struct cache_data *ipv6;
151 struct domain_question {
154 } __attribute__ ((packed));
161 } __attribute__ ((packed));
164 * We limit how long the cached DNS entry stays in the cache.
165 * By default the TTL (time-to-live) of the DNS response is used
166 * when setting the cache entry life time. The value is in seconds.
168 #define MAX_CACHE_TTL (60 * 30)
170 * Also limit the other end, cache at least for 30 seconds.
172 #define MIN_CACHE_TTL (30)
175 * We limit the cache size to some sane value so that cached data does
176 * not occupy too much memory. Each cached entry occupies on average
177 * about 100 bytes memory (depending on DNS name length).
178 * Example: caching www.connman.net uses 97 bytes memory.
179 * The value is the max amount of cached DNS responses (count).
181 #define MAX_CACHE_SIZE 256
183 static int cache_size;
184 static GHashTable *cache;
185 static int cache_refcount;
186 static GSList *server_list = NULL;
187 static GSList *request_list = NULL;
188 static GHashTable *listener_table = NULL;
189 static time_t next_refresh;
191 static guint16 get_id()
196 static int protocol_offset(int protocol)
212 * There is a power and efficiency benefit to have entries
213 * in our cache expire at the same time. To this extend,
214 * we round down the cache valid time to common boundaries.
216 static time_t round_down_ttl(time_t end_time, int ttl)
221 /* Less than 5 minutes, round to 10 second boundary */
223 end_time = end_time / 10;
224 end_time = end_time * 10;
225 } else { /* 5 or more minutes, round to 30 seconds */
226 end_time = end_time / 30;
227 end_time = end_time * 30;
232 static struct request_data *find_request(guint16 id)
236 for (list = request_list; list; list = list->next) {
237 struct request_data *req = list->data;
239 if (req->dstid == id || req->altid == id)
246 static struct server_data *find_server(int index,
252 DBG("index %d server %s proto %d", index, server, protocol);
254 for (list = server_list; list; list = list->next) {
255 struct server_data *data = list->data;
257 if (index < 0 && data->index < 0 &&
258 g_str_equal(data->server, server) == TRUE &&
259 data->protocol == protocol)
263 data->index < 0 || data->server == NULL)
266 if (data->index == index &&
267 g_str_equal(data->server, server) == TRUE &&
268 data->protocol == protocol)
275 /* we can keep using the same resolve's */
276 static GResolv *ipv4_resolve;
277 static GResolv *ipv6_resolve;
279 static void dummy_resolve_func(GResolvResultStatus status,
280 char **results, gpointer user_data)
285 * Refresh a DNS entry, but also age the hit count a bit */
286 static void refresh_dns_entry(struct cache_entry *entry, char *name)
290 if (ipv4_resolve == NULL) {
291 ipv4_resolve = g_resolv_new(0);
292 g_resolv_set_address_family(ipv4_resolve, AF_INET);
293 g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
296 if (ipv6_resolve == NULL) {
297 ipv6_resolve = g_resolv_new(0);
298 g_resolv_set_address_family(ipv6_resolve, AF_INET6);
299 g_resolv_add_nameserver(ipv6_resolve, "127.0.0.1", 53, 0);
302 if (entry->ipv4 == NULL) {
303 DBG("Refresing A record for %s", name);
304 g_resolv_lookup_hostname(ipv4_resolve, name,
305 dummy_resolve_func, NULL);
309 if (entry->ipv6 == NULL) {
310 DBG("Refresing AAAA record for %s", name);
311 g_resolv_lookup_hostname(ipv6_resolve, name,
312 dummy_resolve_func, NULL);
321 static int dns_name_length(unsigned char *buf)
323 if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
325 return strlen((char *)buf);
328 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
335 /* skip the header */
339 /* skip the query, which is a name and 2 16 bit words */
340 l = dns_name_length(c);
346 /* now we get the answer records */
350 l = dns_name_length(c);
355 /* then type + class, 2 bytes each */
361 /* now the 4 byte TTL field */
369 /* now the 2 byte rdlen field */
372 len -= ntohs(*w) + 2;
376 static void send_cached_response(int sk, unsigned char *buf, int len,
377 const struct sockaddr *to, socklen_t tolen,
378 int protocol, int id, uint16_t answers, int ttl)
380 struct domain_hdr *hdr;
381 unsigned char *ptr = buf;
382 int err, offset, dns_len, adj_len = len - 2;
385 * The cached packet contains always the TCP offset (two bytes)
386 * so skip them for UDP.
397 dns_len = ptr[0] * 256 + ptr[1];
406 hdr = (void *) (ptr + offset);
411 hdr->ancount = htons(answers);
415 /* if this is a negative reply, we are authorative */
419 update_cached_ttl((unsigned char *)hdr, adj_len, ttl);
421 DBG("sk %d id 0x%04x answers %d ptr %p length %d dns %d",
422 sk, hdr->id, answers, ptr, len, dns_len);
424 err = sendto(sk, ptr, len, MSG_NOSIGNAL, to, tolen);
426 connman_error("Cannot send cached DNS response: %s",
431 if (err != len || (dns_len != (len - 2) && protocol == IPPROTO_TCP) ||
432 (dns_len != len && protocol == IPPROTO_UDP))
433 DBG("Packet length mismatch, sent %d wanted %d dns %d",
437 static void send_response(int sk, unsigned char *buf, int len,
438 const struct sockaddr *to, socklen_t tolen,
441 struct domain_hdr *hdr;
442 int err, offset = protocol_offset(protocol);
452 hdr = (void *) (buf + offset);
454 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
463 err = sendto(sk, buf, len, MSG_NOSIGNAL, to, tolen);
465 connman_error("Failed to send DNS response to %d: %s",
466 sk, strerror(errno));
471 static gboolean request_timeout(gpointer user_data)
473 struct request_data *req = user_data;
474 struct listener_data *ifdata;
476 DBG("id 0x%04x", req->srcid);
481 ifdata = req->ifdata;
483 request_list = g_slist_remove(request_list, req);
486 if (req->resplen > 0 && req->resp != NULL) {
489 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
491 err = sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
492 &req->sa, req->sa_len);
495 } else if (req->request && req->numserv == 0) {
496 struct domain_hdr *hdr;
498 if (req->protocol == IPPROTO_TCP) {
499 hdr = (void *) (req->request + 2);
500 hdr->id = req->srcid;
501 send_response(req->client_sk, req->request,
502 req->request_len, NULL, 0, IPPROTO_TCP);
504 } else if (req->protocol == IPPROTO_UDP) {
507 hdr = (void *) (req->request);
508 hdr->id = req->srcid;
509 sk = g_io_channel_unix_get_fd(
510 ifdata->udp_listener_channel);
511 send_response(sk, req->request, req->request_len,
512 &req->sa, req->sa_len, IPPROTO_UDP);
522 static int append_query(unsigned char *buf, unsigned int size,
523 const char *query, const char *domain)
525 unsigned char *ptr = buf;
528 DBG("query %s domain %s", query, domain);
530 while (query != NULL) {
533 tmp = strchr(query, '.');
539 memcpy(ptr + 1, query, len);
545 memcpy(ptr + 1, query, tmp - query);
546 ptr += tmp - query + 1;
551 while (domain != NULL) {
554 tmp = strchr(domain, '.');
556 len = strlen(domain);
560 memcpy(ptr + 1, domain, len);
566 memcpy(ptr + 1, domain, tmp - domain);
567 ptr += tmp - domain + 1;
577 static gboolean cache_check_is_valid(struct cache_data *data,
583 if (data->cache_until < current_time)
590 * remove stale cached entries so that they can be refreshed
592 static void cache_enforce_validity(struct cache_entry *entry)
594 time_t current_time = time(NULL);
596 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE
598 DBG("cache timeout \"%s\" type A", entry->key);
599 g_free(entry->ipv4->data);
605 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE
607 DBG("cache timeout \"%s\" type AAAA", entry->key);
608 g_free(entry->ipv6->data);
614 static uint16_t cache_check_validity(char *question, uint16_t type,
615 struct cache_entry *entry)
617 time_t current_time = time(NULL);
618 int want_refresh = 0;
621 * if we have a popular entry, we want a refresh instead of
622 * total destruction of the entry.
627 cache_enforce_validity(entry);
631 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE) {
632 DBG("cache %s \"%s\" type A", entry->ipv4 ?
633 "timeout" : "entry missing", question);
636 entry->want_refresh = 1;
639 * We do not remove cache entry if there is still
640 * valid IPv6 entry found in the cache.
642 if (cache_check_is_valid(entry->ipv6, current_time)
643 == FALSE && want_refresh == FALSE) {
644 g_hash_table_remove(cache, question);
651 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE) {
652 DBG("cache %s \"%s\" type AAAA", entry->ipv6 ?
653 "timeout" : "entry missing", question);
656 entry->want_refresh = 1;
658 if (cache_check_is_valid(entry->ipv4, current_time)
659 == FALSE && want_refresh == FALSE) {
660 g_hash_table_remove(cache, question);
670 static struct cache_entry *cache_check(gpointer request, int *qtype, int proto)
673 struct cache_entry *entry;
674 struct domain_question *q;
676 int offset, proto_offset;
681 proto_offset = protocol_offset(proto);
682 if (proto_offset < 0)
685 question = request + proto_offset + 12;
687 offset = strlen(question) + 1;
688 q = (void *) (question + offset);
689 type = ntohs(q->type);
691 /* We only cache either A (1) or AAAA (28) requests */
692 if (type != 1 && type != 28)
695 entry = g_hash_table_lookup(cache, question);
699 type = cache_check_validity(question, type, entry);
708 * Get a label/name from DNS resource record. The function decompresses the
709 * label if necessary. The function does not convert the name to presentation
710 * form. This means that the result string will contain label lengths instead
711 * of dots between labels. We intentionally do not want to convert to dotted
712 * format so that we can cache the wire format string directly.
714 static int get_name(int counter,
715 unsigned char *pkt, unsigned char *start, unsigned char *max,
716 unsigned char *output, int output_max, int *output_len,
717 unsigned char **end, char *name, int *name_len)
721 /* Limit recursion to 10 (this means up to 10 labels in domain name) */
727 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
728 uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
730 if (offset >= max - pkt)
736 return get_name(counter + 1, pkt, pkt + offset, max,
737 output, output_max, output_len, end,
740 unsigned label_len = *p;
742 if (pkt + label_len > max)
745 if (*output_len > output_max)
749 * We need the original name in order to check
750 * if this answer is the correct one.
752 name[(*name_len)++] = label_len;
753 memcpy(name + *name_len, p + 1, label_len + 1);
754 *name_len += label_len;
756 /* We compress the result */
757 output[0] = NS_CMPRSFLGS;
774 static int parse_rr(unsigned char *buf, unsigned char *start,
776 unsigned char *response, unsigned int *response_size,
777 uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
781 struct domain_rr *rr;
783 int name_len = 0, output_len = 0, max_rsp = *response_size;
785 err = get_name(0, buf, start, max, response, max_rsp,
786 &output_len, end, name, &name_len);
792 if ((unsigned int) offset > *response_size)
795 rr = (void *) (*end);
800 *type = ntohs(rr->type);
801 *class = ntohs(rr->class);
802 *ttl = ntohl(rr->ttl);
803 *rdlen = ntohs(rr->rdlen);
808 memcpy(response + offset, *end, sizeof(struct domain_rr));
810 offset += sizeof(struct domain_rr);
811 *end += sizeof(struct domain_rr);
813 if ((unsigned int) (offset + *rdlen) > *response_size)
816 memcpy(response + offset, *end, *rdlen);
820 *response_size = offset + *rdlen;
825 static gboolean check_alias(GSList *aliases, char *name)
829 if (aliases != NULL) {
830 for (list = aliases; list; list = list->next) {
831 int len = strlen((char *)list->data);
832 if (strncmp((char *)list->data, name, len) == 0)
840 static int parse_response(unsigned char *buf, int buflen,
841 char *question, int qlen,
842 uint16_t *type, uint16_t *class, int *ttl,
843 unsigned char *response, unsigned int *response_len,
846 struct domain_hdr *hdr = (void *) buf;
847 struct domain_question *q;
849 uint16_t qdcount = ntohs(hdr->qdcount);
850 uint16_t ancount = ntohs(hdr->ancount);
852 uint16_t qtype, qclass;
853 unsigned char *next = NULL;
854 unsigned int maxlen = *response_len;
855 GSList *aliases = NULL, *list;
856 char name[NS_MAXDNAME + 1];
861 DBG("qr %d qdcount %d", hdr->qr, qdcount);
863 /* We currently only cache responses where question count is 1 */
864 if (hdr->qr != 1 || qdcount != 1)
867 ptr = buf + sizeof(struct domain_hdr);
869 strncpy(question, (char *) ptr, qlen);
870 qlen = strlen(question);
871 ptr += qlen + 1; /* skip \0 */
874 qtype = ntohs(q->type);
876 /* We cache only A and AAAA records */
877 if (qtype != 1 && qtype != 28)
880 qclass = ntohs(q->class);
882 ptr += 2 + 2; /* ptr points now to answers */
889 * We have a bunch of answers (like A, AAAA, CNAME etc) to
890 * A or AAAA question. We traverse the answers and parse the
891 * resource records. Only A and AAAA records are cached, all
892 * the other records in answers are skipped.
894 for (i = 0; i < ancount; i++) {
896 * Get one address at a time to this buffer.
897 * The max size of the answer is
898 * 2 (pointer) + 2 (type) + 2 (class) +
899 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
900 * for A or AAAA record.
901 * For CNAME the size can be bigger.
903 unsigned char rsp[NS_MAXCDNAME];
904 unsigned int rsp_len = sizeof(rsp) - 1;
907 memset(rsp, 0, sizeof(rsp));
909 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
910 type, class, ttl, &rdlen, &next, name);
917 * Now rsp contains compressed or uncompressed resource
918 * record. Next we check if this record answers the question.
919 * The name var contains the uncompressed label.
920 * One tricky bit is the CNAME records as they alias
921 * the name we might be interested in.
925 * Go to next answer if the class is not the one we are
928 if (*class != qclass) {
935 * Try to resolve aliases also, type is CNAME(5).
936 * This is important as otherwise the aliased names would not
937 * be cached at all as the cache would not contain the aliased
940 * If any CNAME is found in DNS packet, then we cache the alias
941 * IP address instead of the question (as the server
942 * said that question has only an alias).
943 * This means in practice that if e.g., ipv6.google.com is
944 * queried, DNS server returns CNAME of that name which is
945 * ipv6.l.google.com. We then cache the address of the CNAME
946 * but return the question name to client. So the alias
947 * status of the name is not saved in cache and thus not
948 * returned to the client. We do not return DNS packets from
949 * cache to client saying that ipv6.google.com is an alias to
950 * ipv6.l.google.com but we return instead a DNS packet that
951 * says ipv6.google.com has address xxx which is in fact the
952 * address of ipv6.l.google.com. For caching purposes this
953 * should not cause any issues.
955 if (*type == 5 && strncmp(question, name, qlen) == 0) {
957 * So now the alias answered the question. This is
958 * not very useful from caching point of view as
959 * the following A or AAAA records will not match the
960 * question. We need to find the real A/AAAA record
961 * of the alias and cache that.
963 unsigned char *end = NULL;
964 int name_len = 0, output_len;
966 memset(rsp, 0, sizeof(rsp));
967 rsp_len = sizeof(rsp) - 1;
970 * Alias is in rdata part of the message,
971 * and next-rdlen points to it. So we need to get
972 * the real name of the alias.
974 ret = get_name(0, buf, next - rdlen, buf + buflen,
975 rsp, rsp_len, &output_len, &end,
978 /* just ignore the error at this point */
985 * We should now have the alias of the entry we might
986 * want to cache. Just remember it for a while.
987 * We check the alias list when we have parsed the
990 aliases = g_slist_prepend(aliases, g_strdup(name));
997 if (*type == qtype) {
999 * We found correct type (A or AAAA)
1001 if (check_alias(aliases, name) == TRUE ||
1002 (aliases == NULL && strncmp(question, name,
1005 * We found an alias or the name of the rr
1006 * matches the question. If so, we append
1007 * the compressed label to the cache.
1008 * The end result is a response buffer that
1009 * will contain one or more cached and
1010 * compressed resource records.
1012 if (*response_len + rsp_len > maxlen) {
1016 memcpy(response + *response_len, rsp, rsp_len);
1017 *response_len += rsp_len;
1028 for (list = aliases; list; list = list->next)
1030 g_slist_free(aliases);
1035 struct cache_timeout {
1036 time_t current_time;
1041 static gboolean cache_check_entry(gpointer key, gpointer value,
1044 struct cache_timeout *data = user_data;
1045 struct cache_entry *entry = value;
1048 /* Scale the number of hits by half as part of cache aging */
1053 * If either IPv4 or IPv6 cached entry has expired, we
1054 * remove both from the cache.
1057 if (entry->ipv4 != NULL && entry->ipv4->timeout > 0) {
1058 max_timeout = entry->ipv4->cache_until;
1059 if (max_timeout > data->max_timeout)
1060 data->max_timeout = max_timeout;
1062 if (entry->ipv4->cache_until < data->current_time)
1066 if (entry->ipv6 != NULL && entry->ipv6->timeout > 0) {
1067 max_timeout = entry->ipv6->cache_until;
1068 if (max_timeout > data->max_timeout)
1069 data->max_timeout = max_timeout;
1071 if (entry->ipv6->cache_until < data->current_time)
1076 * if we're asked to try harder, also remove entries that have
1079 if (data->try_harder && entry->hits < 4)
1085 static void cache_cleanup(void)
1087 static int max_timeout;
1088 struct cache_timeout data;
1091 data.current_time = time(NULL);
1092 data.max_timeout = 0;
1093 data.try_harder = 0;
1096 * In the first pass, we only remove entries that have timed out.
1097 * We use a cache of the first time to expire to do this only
1098 * when it makes sense.
1100 if (max_timeout <= data.current_time) {
1101 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1104 DBG("removed %d in the first pass", count);
1107 * In the second pass, if the first pass turned up blank,
1108 * we also expire entries with a low hit count,
1109 * while aging the hit count at the same time.
1111 data.try_harder = 1;
1113 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1118 * If we could not remove anything, then remember
1119 * what is the max timeout and do nothing if we
1120 * have not yet reached it. This will prevent
1121 * constant traversal of the cache if it is full.
1123 max_timeout = data.max_timeout;
1128 static gboolean cache_invalidate_entry(gpointer key, gpointer value,
1131 struct cache_entry *entry = value;
1133 /* first, delete any expired elements */
1134 cache_enforce_validity(entry);
1136 /* if anything is not expired, mark the entry for refresh */
1137 if (entry->hits > 0 && (entry->ipv4 || entry->ipv6))
1138 entry->want_refresh = 1;
1140 /* delete the cached data */
1142 g_free(entry->ipv4->data);
1143 g_free(entry->ipv4);
1148 g_free(entry->ipv6->data);
1149 g_free(entry->ipv6);
1153 /* keep the entry if we want it refreshed, delete it otherwise */
1154 if (entry->want_refresh)
1161 * cache_invalidate is called from places where the DNS landscape
1162 * has changed, say because connections are added or we entered a VPN.
1163 * The logic is to wipe all cache data, but mark all non-expired
1164 * parts of the cache for refresh rather than deleting the whole cache.
1166 static void cache_invalidate(void)
1168 DBG("Invalidating the DNS cache %p", cache);
1173 g_hash_table_foreach_remove(cache, cache_invalidate_entry, NULL);
1176 static void cache_refresh_entry(struct cache_entry *entry)
1179 cache_enforce_validity(entry);
1181 if (entry->hits > 2 && entry->ipv4 == NULL)
1182 entry->want_refresh = 1;
1183 if (entry->hits > 2 && entry->ipv6 == NULL)
1184 entry->want_refresh = 1;
1186 if (entry->want_refresh) {
1188 char dns_name[NS_MAXDNAME + 1];
1189 entry->want_refresh = 0;
1191 /* turn a DNS name into a hostname with dots */
1192 strncpy(dns_name, entry->key, NS_MAXDNAME);
1200 DBG("Refreshing %s\n", dns_name);
1201 /* then refresh the hostname */
1202 refresh_dns_entry(entry, &dns_name[1]);
1206 static void cache_refresh_iterator(gpointer key, gpointer value,
1209 struct cache_entry *entry = value;
1211 cache_refresh_entry(entry);
1214 static void cache_refresh(void)
1219 g_hash_table_foreach(cache, cache_refresh_iterator, NULL);
1222 static int reply_query_type(unsigned char *msg, int len)
1229 /* skip the header */
1230 c = msg + sizeof(struct domain_hdr);
1231 len -= sizeof(struct domain_hdr);
1236 /* now the query, which is a name and 2 16 bit words */
1237 l = dns_name_length(c) + 1;
1245 static int cache_update(struct server_data *srv, unsigned char *msg,
1246 unsigned int msg_len)
1248 int offset = protocol_offset(srv->protocol);
1249 int err, qlen, ttl = 0;
1250 uint16_t answers = 0, type = 0, class = 0;
1251 struct domain_hdr *hdr = (void *)(msg + offset);
1252 struct domain_question *q;
1253 struct cache_entry *entry;
1254 struct cache_data *data;
1255 char question[NS_MAXDNAME + 1];
1256 unsigned char response[NS_MAXDNAME + 1];
1258 unsigned int rsplen;
1259 gboolean new_entry = TRUE;
1260 time_t current_time;
1262 if (cache_size >= MAX_CACHE_SIZE) {
1264 if (cache_size >= MAX_CACHE_SIZE)
1268 current_time = time(NULL);
1270 /* don't do a cache refresh more than twice a minute */
1271 if (next_refresh < current_time) {
1273 next_refresh = current_time + 30;
1279 DBG("offset %d hdr %p msg %p rcode %d", offset, hdr, msg, hdr->rcode);
1281 /* Continue only if response code is 0 (=ok) */
1282 if (hdr->rcode != 0)
1285 rsplen = sizeof(response) - 1;
1286 question[sizeof(question) - 1] = '\0';
1288 err = parse_response(msg + offset, msg_len - offset,
1289 question, sizeof(question) - 1,
1290 &type, &class, &ttl,
1291 response, &rsplen, &answers);
1294 * special case: if we do a ipv6 lookup and get no result
1295 * for a record that's already in our ipv4 cache.. we want
1296 * to cache the negative response.
1298 if ((err == -ENOMSG || err == -ENOBUFS) &&
1299 reply_query_type(msg + offset,
1300 msg_len - offset) == 28) {
1301 entry = g_hash_table_lookup(cache, question);
1302 if (entry && entry->ipv4 && entry->ipv6 == NULL) {
1303 int cache_offset = 0;
1305 data = g_try_new(struct cache_data, 1);
1308 data->inserted = entry->ipv4->inserted;
1310 data->answers = hdr->ancount;
1311 data->timeout = entry->ipv4->timeout;
1312 if (srv->protocol == IPPROTO_UDP)
1314 data->data_len = msg_len + cache_offset;
1315 data->data = ptr = g_malloc(data->data_len);
1316 ptr[0] = (data->data_len - 2) / 256;
1317 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1318 if (srv->protocol == IPPROTO_UDP)
1320 data->valid_until = entry->ipv4->valid_until;
1321 data->cache_until = entry->ipv4->cache_until;
1322 memcpy(ptr, msg, msg_len);
1325 * we will get a "hit" when we serve the response
1329 if (entry->hits < 0)
1335 if (err < 0 || ttl == 0)
1338 qlen = strlen(question);
1341 * If the cache contains already data, check if the
1342 * type of the cached data is the same and do not add
1343 * to cache if data is already there.
1344 * This is needed so that we can cache both A and AAAA
1345 * records for the same name.
1347 entry = g_hash_table_lookup(cache, question);
1348 if (entry == NULL) {
1349 entry = g_try_new(struct cache_entry, 1);
1353 data = g_try_new(struct cache_data, 1);
1359 entry->key = g_strdup(question);
1360 entry->ipv4 = entry->ipv6 = NULL;
1361 entry->want_refresh = 0;
1369 if (type == 1 && entry->ipv4 != NULL)
1372 if (type == 28 && entry->ipv6 != NULL)
1375 data = g_try_new(struct cache_data, 1);
1385 * compensate for the hit we'll get for serving
1386 * the response out of the cache
1389 if (entry->hits < 0)
1395 if (ttl < MIN_CACHE_TTL)
1396 ttl = MIN_CACHE_TTL;
1398 data->inserted = current_time;
1400 data->answers = answers;
1401 data->timeout = ttl;
1403 * The "2" in start of the length is the TCP offset. We allocate it
1404 * here even for UDP packet because it simplifies the sending
1407 data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
1408 data->data = ptr = g_malloc(data->data_len);
1409 data->valid_until = current_time + ttl;
1412 * Restrict the cached DNS record TTL to some sane value
1413 * in order to prevent data staying in the cache too long.
1415 if (ttl > MAX_CACHE_TTL)
1416 ttl = MAX_CACHE_TTL;
1418 data->cache_until = round_down_ttl(current_time + ttl, ttl);
1420 if (data->data == NULL) {
1428 * We cache the two extra bytes at the start of the message
1429 * in a TCP packet. When sending UDP packet, we skip the first
1430 * two bytes. This way we do not need to know the format
1431 * (UDP/TCP) of the cached message.
1433 ptr[0] = (data->data_len - 2) / 256;
1434 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1435 if (srv->protocol == IPPROTO_UDP)
1438 memcpy(ptr, msg, offset + 12);
1439 memcpy(ptr + offset + 12, question, qlen + 1); /* copy also the \0 */
1441 q = (void *) (ptr + offset + 12 + qlen + 1);
1442 q->type = htons(type);
1443 q->class = htons(class);
1444 memcpy(ptr + offset + 12 + qlen + 1 + sizeof(struct domain_question),
1447 if (new_entry == TRUE) {
1448 g_hash_table_replace(cache, entry->key, entry);
1452 DBG("cache %d %squestion \"%s\" type %d ttl %d size %zd packet %u "
1454 cache_size, new_entry ? "new " : "old ",
1455 question, type, ttl,
1456 sizeof(*entry) + sizeof(*data) + data->data_len + qlen,
1458 srv->protocol == IPPROTO_TCP ?
1459 (unsigned int)(data->data[0] * 256 + data->data[1]) :
1465 static int ns_resolv(struct server_data *server, struct request_data *req,
1466 gpointer request, gpointer name)
1469 int sk, err, type = 0;
1470 char *dot, *lookup = (char *) name;
1471 struct cache_entry *entry;
1473 entry = cache_check(request, &type, req->protocol);
1474 if (entry != NULL) {
1476 struct cache_data *data;
1478 DBG("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1485 ttl_left = data->valid_until - time(NULL);
1489 if (data != NULL && req->protocol == IPPROTO_TCP) {
1490 send_cached_response(req->client_sk, data->data,
1491 data->data_len, NULL, 0, IPPROTO_TCP,
1492 req->srcid, data->answers, ttl_left);
1496 if (data != NULL && req->protocol == IPPROTO_UDP) {
1497 int udp_sk = g_io_channel_unix_get_fd(
1498 req->ifdata->udp_listener_channel);
1500 send_cached_response(udp_sk, data->data,
1501 data->data_len, &req->sa, req->sa_len,
1502 IPPROTO_UDP, req->srcid, data->answers,
1508 sk = g_io_channel_unix_get_fd(server->channel);
1510 err = sendto(sk, request, req->request_len, MSG_NOSIGNAL,
1511 server->server_addr, server->server_addr_len);
1513 DBG("Cannot send message to server %s sock %d "
1514 "protocol %d (%s/%d)",
1515 server->server, sk, server->protocol,
1516 strerror(errno), errno);
1522 /* If we have more than one dot, we don't add domains */
1523 dot = strchr(lookup, '.');
1524 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
1527 if (server->domains != NULL && server->domains->data != NULL)
1528 req->append_domain = TRUE;
1530 for (list = server->domains; list; list = list->next) {
1532 unsigned char alt[1024];
1533 struct domain_hdr *hdr = (void *) &alt;
1534 int altlen, domlen, offset;
1536 domain = list->data;
1541 offset = protocol_offset(server->protocol);
1545 domlen = strlen(domain) + 1;
1549 alt[offset] = req->altid & 0xff;
1550 alt[offset + 1] = req->altid >> 8;
1552 memcpy(alt + offset + 2, request + offset + 2, 10);
1553 hdr->qdcount = htons(1);
1555 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1562 memcpy(alt + offset + altlen,
1563 request + offset + altlen - domlen,
1564 req->request_len - altlen - offset + domlen);
1566 if (server->protocol == IPPROTO_TCP) {
1567 int req_len = req->request_len + domlen - 2;
1569 alt[0] = (req_len >> 8) & 0xff;
1570 alt[1] = req_len & 0xff;
1573 DBG("req %p dstid 0x%04x altid 0x%04x", req, req->dstid,
1576 err = send(sk, alt, req->request_len + domlen, MSG_NOSIGNAL);
1586 static void destroy_request_data(struct request_data *req)
1588 if (req->timeout > 0)
1589 g_source_remove(req->timeout);
1592 g_free(req->request);
1597 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1598 struct server_data *data)
1600 struct domain_hdr *hdr;
1601 struct request_data *req;
1602 int dns_id, sk, err, offset = protocol_offset(protocol);
1603 struct listener_data *ifdata;
1608 hdr = (void *)(reply + offset);
1609 dns_id = reply[offset] | reply[offset + 1] << 8;
1611 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1613 req = find_request(dns_id);
1617 DBG("req %p dstid 0x%04x altid 0x%04x rcode %d",
1618 req, req->dstid, req->altid, hdr->rcode);
1620 ifdata = req->ifdata;
1622 reply[offset] = req->srcid & 0xff;
1623 reply[offset + 1] = req->srcid >> 8;
1627 if (hdr->rcode == 0 || req->resp == NULL) {
1630 * If the domain name was append
1631 * remove it before forwarding the reply.
1633 if (req->append_domain == TRUE) {
1634 unsigned int domain_len = 0;
1637 unsigned int header_len;
1640 * ptr points to the first char of the hostname.
1641 * ->hostname.domain.net
1643 header_len = offset + sizeof(struct domain_hdr);
1644 ptr = reply + header_len;
1647 domain_len = strnlen((const char *)ptr + 1 +
1649 reply_len - header_len);
1652 DBG("host len %d domain len %d", host_len, domain_len);
1655 * Remove the domain name and replace it by the end
1656 * of reply. Check if the domain is really there
1657 * before trying to copy the data. The domain_len can
1658 * be 0 because if the original query did not contain
1659 * a domain name, then we are sending two packets,
1660 * first without the domain name and the second packet
1661 * with domain name. The append_domain is set to true
1662 * even if we sent the first packet without domain
1663 * name. In this case we end up in this branch.
1665 if (domain_len > 0) {
1667 * Note that we must use memmove() here,
1668 * because the memory areas can overlap.
1670 memmove(ptr + host_len + 1,
1671 ptr + host_len + domain_len + 1,
1672 reply_len - header_len - domain_len);
1674 reply_len = reply_len - domain_len;
1681 req->resp = g_try_malloc(reply_len);
1682 if (req->resp == NULL)
1685 memcpy(req->resp, reply, reply_len);
1686 req->resplen = reply_len;
1688 cache_update(data, reply, reply_len);
1691 if (hdr->rcode > 0 && req->numresp < req->numserv)
1694 request_list = g_slist_remove(request_list, req);
1696 if (protocol == IPPROTO_UDP) {
1697 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
1698 err = sendto(sk, req->resp, req->resplen, 0,
1699 &req->sa, req->sa_len);
1701 sk = req->client_sk;
1702 err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
1707 DBG("Cannot send msg, sk %d proto %d errno %d/%s", sk,
1708 protocol, errno, strerror(errno));
1710 DBG("proto %d sent %d bytes to %d", protocol, err, sk);
1712 destroy_request_data(req);
1717 static void cache_element_destroy(gpointer value)
1719 struct cache_entry *entry = value;
1724 if (entry->ipv4 != NULL) {
1725 g_free(entry->ipv4->data);
1726 g_free(entry->ipv4);
1729 if (entry->ipv6 != NULL) {
1730 g_free(entry->ipv6->data);
1731 g_free(entry->ipv6);
1737 if (--cache_size < 0)
1741 static gboolean try_remove_cache(gpointer user_data)
1743 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1) {
1744 DBG("No cache users, removing it.");
1746 g_hash_table_destroy(cache);
1753 static void server_destroy_socket(struct server_data *data)
1755 DBG("index %d server %s proto %d", data->index,
1756 data->server, data->protocol);
1758 if (data->watch > 0) {
1759 g_source_remove(data->watch);
1763 if (data->timeout > 0) {
1764 g_source_remove(data->timeout);
1768 if (data->channel != NULL) {
1769 g_io_channel_shutdown(data->channel, TRUE, NULL);
1770 g_io_channel_unref(data->channel);
1771 data->channel = NULL;
1774 g_free(data->incoming_reply);
1775 data->incoming_reply = NULL;
1778 static void destroy_server(struct server_data *server)
1782 DBG("index %d server %s sock %d", server->index, server->server,
1783 server->channel != NULL ?
1784 g_io_channel_unix_get_fd(server->channel): -1);
1786 server_list = g_slist_remove(server_list, server);
1787 server_destroy_socket(server);
1789 if (server->protocol == IPPROTO_UDP && server->enabled)
1790 DBG("Removing DNS server %s", server->server);
1792 g_free(server->server);
1793 for (list = server->domains; list; list = list->next) {
1794 char *domain = list->data;
1796 server->domains = g_list_remove(server->domains, domain);
1799 g_free(server->server_addr);
1802 * We do not remove cache right away but delay it few seconds.
1803 * The idea is that when IPv6 DNS server is added via RDNSS, it has a
1804 * lifetime. When the lifetime expires we decrease the refcount so it
1805 * is possible that the cache is then removed. Because a new DNS server
1806 * is usually created almost immediately we would then loose the cache
1807 * without any good reason. The small delay allows the new RDNSS to
1808 * create a new DNS server instance and the refcount does not go to 0.
1810 g_timeout_add_seconds(3, try_remove_cache, NULL);
1815 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
1818 unsigned char buf[4096];
1820 struct server_data *data = user_data;
1822 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1823 connman_error("Error with UDP server %s", data->server);
1824 server_destroy_socket(data);
1828 sk = g_io_channel_unix_get_fd(channel);
1830 len = recv(sk, buf, sizeof(buf), 0);
1834 err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
1841 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
1845 struct server_data *server = user_data;
1847 sk = g_io_channel_unix_get_fd(channel);
1851 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1854 DBG("TCP server channel closed, sk %d", sk);
1857 * Discard any partial response which is buffered; better
1858 * to get a proper response from a working server.
1860 g_free(server->incoming_reply);
1861 server->incoming_reply = NULL;
1863 for (list = request_list; list; list = list->next) {
1864 struct request_data *req = list->data;
1865 struct domain_hdr *hdr;
1867 if (req->protocol == IPPROTO_UDP)
1870 if (req->request == NULL)
1874 * If we're not waiting for any further response
1875 * from another name server, then we send an error
1876 * response to the client.
1878 if (req->numserv && --(req->numserv))
1881 hdr = (void *) (req->request + 2);
1882 hdr->id = req->srcid;
1883 send_response(req->client_sk, req->request,
1884 req->request_len, NULL, 0, IPPROTO_TCP);
1886 request_list = g_slist_remove(request_list, req);
1889 destroy_server(server);
1894 if ((condition & G_IO_OUT) && !server->connected) {
1897 int no_request_sent = TRUE;
1898 struct server_data *udp_server;
1900 udp_server = find_server(server->index, server->server,
1902 if (udp_server != NULL) {
1903 for (domains = udp_server->domains; domains;
1904 domains = domains->next) {
1905 char *dom = domains->data;
1907 DBG("Adding domain %s to %s",
1908 dom, server->server);
1910 server->domains = g_list_append(server->domains,
1915 server->connected = TRUE;
1916 server_list = g_slist_append(server_list, server);
1918 if (server->timeout > 0) {
1919 g_source_remove(server->timeout);
1920 server->timeout = 0;
1923 for (list = request_list; list; ) {
1924 struct request_data *req = list->data;
1927 if (req->protocol == IPPROTO_UDP) {
1932 DBG("Sending req %s over TCP", (char *)req->name);
1934 status = ns_resolv(server, req,
1935 req->request, req->name);
1938 * A cached result was sent,
1939 * so the request can be released
1942 request_list = g_slist_remove(request_list, req);
1943 destroy_request_data(req);
1952 no_request_sent = FALSE;
1954 if (req->timeout > 0)
1955 g_source_remove(req->timeout);
1957 req->timeout = g_timeout_add_seconds(30,
1958 request_timeout, req);
1962 if (no_request_sent == TRUE) {
1963 destroy_server(server);
1967 } else if (condition & G_IO_IN) {
1968 struct partial_reply *reply = server->incoming_reply;
1972 unsigned char reply_len_buf[2];
1975 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
1978 } else if (bytes_recv < 0) {
1979 if (errno == EAGAIN || errno == EWOULDBLOCK)
1982 connman_error("DNS proxy error %s",
1985 } else if (bytes_recv < 2)
1988 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
1991 DBG("TCP reply %d bytes from %d", reply_len, sk);
1993 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
1997 reply->len = reply_len;
1998 reply->received = 0;
2000 server->incoming_reply = reply;
2003 while (reply->received < reply->len) {
2004 bytes_recv = recv(sk, reply->buf + reply->received,
2005 reply->len - reply->received, 0);
2007 connman_error("DNS proxy TCP disconnect");
2009 } else if (bytes_recv < 0) {
2010 if (errno == EAGAIN || errno == EWOULDBLOCK)
2013 connman_error("DNS proxy error %s",
2017 reply->received += bytes_recv;
2020 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
2024 server->incoming_reply = NULL;
2026 destroy_server(server);
2034 static gboolean tcp_idle_timeout(gpointer user_data)
2036 struct server_data *server = user_data;
2043 destroy_server(server);
2048 static int server_create_socket(struct server_data *data)
2053 DBG("index %d server %s proto %d", data->index,
2054 data->server, data->protocol);
2056 sk = socket(data->server_addr->sa_family,
2057 data->protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM,
2061 connman_error("Failed to create server %s socket",
2063 server_destroy_socket(data);
2069 interface = connman_inet_ifname(data->index);
2070 if (interface != NULL) {
2071 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2073 strlen(interface) + 1) < 0) {
2075 connman_error("Failed to bind server %s "
2077 data->server, interface);
2079 server_destroy_socket(data);
2086 data->channel = g_io_channel_unix_new(sk);
2087 if (data->channel == NULL) {
2088 connman_error("Failed to create server %s channel",
2091 server_destroy_socket(data);
2095 g_io_channel_set_close_on_unref(data->channel, TRUE);
2097 if (data->protocol == IPPROTO_TCP) {
2098 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
2099 data->watch = g_io_add_watch(data->channel,
2100 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
2101 tcp_server_event, data);
2102 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
2105 data->watch = g_io_add_watch(data->channel,
2106 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2107 udp_server_event, data);
2109 if (connect(sk, data->server_addr, data->server_addr_len) < 0) {
2112 if ((data->protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
2113 data->protocol == IPPROTO_UDP) {
2115 connman_error("Failed to connect to server %s",
2117 server_destroy_socket(data);
2122 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
2123 cache = g_hash_table_new_full(g_str_hash,
2126 cache_element_destroy);
2131 static struct server_data *create_server(int index,
2132 const char *domain, const char *server,
2135 struct server_data *data;
2136 struct addrinfo hints, *rp;
2139 DBG("index %d server %s", index, server);
2141 data = g_try_new0(struct server_data, 1);
2143 connman_error("Failed to allocate server %s data", server);
2147 data->index = index;
2149 data->domains = g_list_append(data->domains, g_strdup(domain));
2150 data->server = g_strdup(server);
2151 data->protocol = protocol;
2153 memset(&hints, 0, sizeof(hints));
2157 hints.ai_socktype = SOCK_DGRAM;
2161 hints.ai_socktype = SOCK_STREAM;
2165 destroy_server(data);
2168 hints.ai_family = AF_UNSPEC;
2169 hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
2171 ret = getaddrinfo(data->server, "53", &hints, &rp);
2173 connman_error("Failed to parse server %s address: %s\n",
2174 data->server, gai_strerror(ret));
2175 destroy_server(data);
2179 /* Do not blindly copy this code elsewhere; it doesn't loop over the
2180 results using ->ai_next as it should. That's OK in *this* case
2181 because it was a numeric lookup; we *know* there's only one. */
2183 data->server_addr_len = rp->ai_addrlen;
2185 switch (rp->ai_family) {
2187 data->server_addr = (struct sockaddr *)
2188 g_try_new0(struct sockaddr_in, 1);
2191 data->server_addr = (struct sockaddr *)
2192 g_try_new0(struct sockaddr_in6, 1);
2195 connman_error("Wrong address family %d", rp->ai_family);
2198 if (data->server_addr == NULL) {
2200 destroy_server(data);
2203 memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
2206 if (server_create_socket(data) != 0) {
2207 destroy_server(data);
2211 if (protocol == IPPROTO_UDP) {
2212 /* Enable new servers by default */
2213 data->enabled = TRUE;
2214 DBG("Adding DNS server %s", data->server);
2216 server_list = g_slist_append(server_list, data);
2222 static gboolean resolv(struct request_data *req,
2223 gpointer request, gpointer name)
2227 for (list = server_list; list; list = list->next) {
2228 struct server_data *data = list->data;
2230 if (data->protocol == IPPROTO_TCP) {
2231 DBG("server %s ignored proto TCP", data->server);
2235 DBG("server %s enabled %d", data->server, data->enabled);
2237 if (data->enabled == FALSE)
2240 if (data->channel == NULL && data->protocol == IPPROTO_UDP) {
2241 if (server_create_socket(data) < 0) {
2242 DBG("socket creation failed while resolving");
2247 if (ns_resolv(data, req, request, name) > 0)
2254 static void append_domain(int index, const char *domain)
2258 DBG("index %d domain %s", index, domain);
2263 for (list = server_list; list; list = list->next) {
2264 struct server_data *data = list->data;
2267 gboolean dom_found = FALSE;
2269 if (data->index < 0)
2272 if (data->index != index)
2275 for (dom_list = data->domains; dom_list;
2276 dom_list = dom_list->next) {
2277 dom = dom_list->data;
2279 if (g_str_equal(dom, domain)) {
2285 if (dom_found == FALSE) {
2287 g_list_append(data->domains, g_strdup(domain));
2292 int __connman_dnsproxy_append(int index, const char *domain,
2295 struct server_data *data;
2297 DBG("index %d server %s", index, server);
2299 if (server == NULL && domain == NULL)
2302 if (server == NULL) {
2303 append_domain(index, domain);
2308 if (g_str_equal(server, "127.0.0.1") == TRUE)
2311 data = find_server(index, server, IPPROTO_UDP);
2313 append_domain(index, domain);
2317 data = create_server(index, domain, server, IPPROTO_UDP);
2324 static void remove_server(int index, const char *domain,
2325 const char *server, int protocol)
2327 struct server_data *data;
2329 data = find_server(index, server, protocol);
2333 destroy_server(data);
2336 int __connman_dnsproxy_remove(int index, const char *domain,
2339 DBG("index %d server %s", index, server);
2344 if (g_str_equal(server, "127.0.0.1") == TRUE)
2347 remove_server(index, domain, server, IPPROTO_UDP);
2348 remove_server(index, domain, server, IPPROTO_TCP);
2353 void __connman_dnsproxy_flush(void)
2357 list = request_list;
2359 struct request_data *req = list->data;
2363 if (resolv(req, req->request, req->name) == TRUE) {
2365 * A cached result was sent,
2366 * so the request can be released
2369 g_slist_remove(request_list, req);
2370 destroy_request_data(req);
2374 if (req->timeout > 0)
2375 g_source_remove(req->timeout);
2376 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2380 static void dnsproxy_offline_mode(connman_bool_t enabled)
2384 DBG("enabled %d", enabled);
2386 for (list = server_list; list; list = list->next) {
2387 struct server_data *data = list->data;
2389 if (enabled == FALSE) {
2390 DBG("Enabling DNS server %s", data->server);
2391 data->enabled = TRUE;
2395 DBG("Disabling DNS server %s", data->server);
2396 data->enabled = FALSE;
2402 static void dnsproxy_default_changed(struct connman_service *service)
2407 DBG("service %p", service);
2409 /* DNS has changed, invalidate the cache */
2412 if (service == NULL) {
2413 /* When no services are active, then disable DNS proxying */
2414 dnsproxy_offline_mode(TRUE);
2418 index = __connman_service_get_index(service);
2422 for (list = server_list; list; list = list->next) {
2423 struct server_data *data = list->data;
2425 if (data->index == index) {
2426 DBG("Enabling DNS server %s", data->server);
2427 data->enabled = TRUE;
2429 DBG("Disabling DNS server %s", data->server);
2430 data->enabled = FALSE;
2437 static struct connman_notifier dnsproxy_notifier = {
2439 .default_changed = dnsproxy_default_changed,
2440 .offline_mode = dnsproxy_offline_mode,
2443 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2445 static int parse_request(unsigned char *buf, int len,
2446 char *name, unsigned int size)
2448 struct domain_hdr *hdr = (void *) buf;
2449 uint16_t qdcount = ntohs(hdr->qdcount);
2450 uint16_t arcount = ntohs(hdr->arcount);
2452 char *last_label = NULL;
2453 unsigned int remain, used = 0;
2458 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2459 hdr->id, hdr->qr, hdr->opcode,
2462 if (hdr->qr != 0 || qdcount != 1)
2467 ptr = buf + sizeof(struct domain_hdr);
2468 remain = len - sizeof(struct domain_hdr);
2470 while (remain > 0) {
2471 uint8_t label_len = *ptr;
2473 if (label_len == 0x00) {
2474 last_label = (char *) (ptr + 1);
2478 if (used + label_len + 1 > size)
2481 strncat(name, (char *) (ptr + 1), label_len);
2484 used += label_len + 1;
2486 ptr += label_len + 1;
2487 remain -= label_len + 1;
2490 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
2491 !memcmp(last_label + 5, opt_edns0_type, 2)) {
2492 uint16_t edns0_bufsize;
2494 edns0_bufsize = last_label[7] << 8 | last_label[8];
2496 DBG("EDNS0 buffer size %u", edns0_bufsize);
2498 /* This is an evil hack until full TCP support has been
2501 * Somtimes the EDNS0 request gets send with a too-small
2502 * buffer size. Since glibc doesn't seem to crash when it
2503 * gets a response biffer then it requested, just bump
2504 * the buffer size up to 4KiB.
2506 if (edns0_bufsize < 0x1000) {
2507 last_label[7] = 0x10;
2508 last_label[8] = 0x00;
2512 DBG("query %s", name);
2517 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
2520 unsigned char buf[768];
2522 struct request_data *req;
2523 int sk, client_sk, len, err;
2524 struct sockaddr_in6 client_addr;
2525 socklen_t client_addr_len = sizeof(client_addr);
2527 struct listener_data *ifdata = user_data;
2528 int waiting_for_connect = FALSE, qtype = 0;
2529 struct cache_entry *entry;
2531 DBG("condition 0x%x", condition);
2533 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2534 if (ifdata->tcp_listener_watch > 0)
2535 g_source_remove(ifdata->tcp_listener_watch);
2536 ifdata->tcp_listener_watch = 0;
2538 connman_error("Error with TCP listener channel");
2543 sk = g_io_channel_unix_get_fd(channel);
2545 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
2546 if (client_sk < 0) {
2547 connman_error("Accept failure on TCP listener");
2548 ifdata->tcp_listener_watch = 0;
2552 len = recv(client_sk, buf, sizeof(buf), 0);
2556 DBG("Received %d bytes (id 0x%04x) from %d", len,
2557 buf[2] | buf[3] << 8, client_sk);
2559 err = parse_request(buf + 2, len - 2, query, sizeof(query));
2560 if (err < 0 || (g_slist_length(server_list) == 0)) {
2561 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2565 req = g_try_new0(struct request_data, 1);
2569 memcpy(&req->sa, &client_addr, client_addr_len);
2570 req->sa_len = client_addr_len;
2571 req->client_sk = client_sk;
2572 req->protocol = IPPROTO_TCP;
2574 req->srcid = buf[2] | (buf[3] << 8);
2575 req->dstid = get_id();
2576 req->altid = get_id();
2577 req->request_len = len;
2579 buf[2] = req->dstid & 0xff;
2580 buf[3] = req->dstid >> 8;
2583 req->ifdata = (struct listener_data *) ifdata;
2584 req->append_domain = FALSE;
2587 * Check if the answer is found in the cache before
2588 * creating sockets to the server.
2590 entry = cache_check(buf, &qtype, IPPROTO_TCP);
2591 if (entry != NULL) {
2593 struct cache_data *data;
2595 DBG("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
2602 ttl_left = data->valid_until - time(NULL);
2605 send_cached_response(client_sk, data->data,
2606 data->data_len, NULL, 0, IPPROTO_TCP,
2607 req->srcid, data->answers, ttl_left);
2612 DBG("data missing, ignoring cache for this query");
2615 for (list = server_list; list; list = list->next) {
2616 struct server_data *data = list->data;
2618 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
2621 if(create_server(data->index, NULL,
2622 data->server, IPPROTO_TCP) == NULL)
2625 waiting_for_connect = TRUE;
2628 if (waiting_for_connect == FALSE) {
2629 /* No server is waiting for connect */
2630 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2636 * The server is not connected yet.
2637 * Copy the relevant buffers.
2638 * The request will actually be sent once we're
2639 * properly connected over TCP to the nameserver.
2641 req->request = g_try_malloc0(req->request_len);
2642 if (req->request == NULL) {
2643 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2647 memcpy(req->request, buf, req->request_len);
2649 req->name = g_try_malloc0(sizeof(query));
2650 if (req->name == NULL) {
2651 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2652 g_free(req->request);
2656 memcpy(req->name, query, sizeof(query));
2658 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
2660 request_list = g_slist_append(request_list, req);
2665 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
2668 unsigned char buf[768];
2670 struct request_data *req;
2671 struct sockaddr_in6 client_addr;
2672 socklen_t client_addr_len = sizeof(client_addr);
2674 struct listener_data *ifdata = user_data;
2676 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2677 connman_error("Error with UDP listener channel");
2678 ifdata->udp_listener_watch = 0;
2682 sk = g_io_channel_unix_get_fd(channel);
2684 memset(&client_addr, 0, client_addr_len);
2685 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
2690 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
2692 err = parse_request(buf, len, query, sizeof(query));
2693 if (err < 0 || (g_slist_length(server_list) == 0)) {
2694 send_response(sk, buf, len, (void *)&client_addr,
2695 client_addr_len, IPPROTO_UDP);
2699 req = g_try_new0(struct request_data, 1);
2703 memcpy(&req->sa, &client_addr, client_addr_len);
2704 req->sa_len = client_addr_len;
2706 req->protocol = IPPROTO_UDP;
2708 req->srcid = buf[0] | (buf[1] << 8);
2709 req->dstid = get_id();
2710 req->altid = get_id();
2711 req->request_len = len;
2713 buf[0] = req->dstid & 0xff;
2714 buf[1] = req->dstid >> 8;
2717 req->ifdata = (struct listener_data *) ifdata;
2718 req->append_domain = FALSE;
2720 if (resolv(req, buf, query) == TRUE) {
2721 /* a cached result was sent, so the request can be released */
2726 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2727 request_list = g_slist_append(request_list, req);
2732 static int create_dns_listener(int protocol, struct listener_data *ifdata)
2734 GIOChannel *channel;
2738 struct sockaddr_in6 sin6;
2739 struct sockaddr_in sin;
2742 int sk, type, v6only = 0;
2743 int family = AF_INET6;
2746 DBG("index %d", ifdata->index);
2751 type = SOCK_DGRAM | SOCK_CLOEXEC;
2756 type = SOCK_STREAM | SOCK_CLOEXEC;
2763 sk = socket(family, type, protocol);
2764 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
2765 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
2767 sk = socket(family, type, protocol);
2770 connman_error("Failed to create %s listener socket", proto);
2774 interface = connman_inet_ifname(ifdata->index);
2775 if (interface == NULL || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2777 strlen(interface) + 1) < 0) {
2778 connman_error("Failed to bind %s listener interface", proto);
2785 /* Ensure it accepts Legacy IP connections too */
2786 if (family == AF_INET6 &&
2787 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
2788 &v6only, sizeof(v6only)) < 0) {
2789 connman_error("Failed to clear V6ONLY on %s listener socket",
2795 if (family == AF_INET) {
2796 memset(&s.sin, 0, sizeof(s.sin));
2797 s.sin.sin_family = AF_INET;
2798 s.sin.sin_port = htons(53);
2799 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
2800 slen = sizeof(s.sin);
2802 memset(&s.sin6, 0, sizeof(s.sin6));
2803 s.sin6.sin6_family = AF_INET6;
2804 s.sin6.sin6_port = htons(53);
2805 s.sin6.sin6_addr = in6addr_any;
2806 slen = sizeof(s.sin6);
2809 if (bind(sk, &s.sa, slen) < 0) {
2810 connman_error("Failed to bind %s listener socket", proto);
2815 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
2816 connman_error("Failed to listen on TCP socket");
2821 channel = g_io_channel_unix_new(sk);
2822 if (channel == NULL) {
2823 connman_error("Failed to create %s listener channel", proto);
2828 g_io_channel_set_close_on_unref(channel, TRUE);
2830 if (protocol == IPPROTO_TCP) {
2831 ifdata->tcp_listener_channel = channel;
2832 ifdata->tcp_listener_watch = g_io_add_watch(channel,
2833 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
2835 ifdata->udp_listener_channel = channel;
2836 ifdata->udp_listener_watch = g_io_add_watch(channel,
2837 G_IO_IN, udp_listener_event, (gpointer) ifdata);
2843 static void destroy_udp_listener(struct listener_data *ifdata)
2845 DBG("index %d", ifdata->index);
2847 if (ifdata->udp_listener_watch > 0)
2848 g_source_remove(ifdata->udp_listener_watch);
2850 g_io_channel_unref(ifdata->udp_listener_channel);
2853 static void destroy_tcp_listener(struct listener_data *ifdata)
2855 DBG("index %d", ifdata->index);
2857 if (ifdata->tcp_listener_watch > 0)
2858 g_source_remove(ifdata->tcp_listener_watch);
2860 g_io_channel_unref(ifdata->tcp_listener_channel);
2863 static int create_listener(struct listener_data *ifdata)
2867 err = create_dns_listener(IPPROTO_UDP, ifdata);
2871 err = create_dns_listener(IPPROTO_TCP, ifdata);
2873 destroy_udp_listener(ifdata);
2877 index = connman_inet_ifindex("lo");
2878 if (ifdata->index == index)
2879 __connman_resolvfile_append(index, NULL, "127.0.0.1");
2884 static void destroy_listener(struct listener_data *ifdata)
2889 index = connman_inet_ifindex("lo");
2890 if (ifdata->index == index)
2891 __connman_resolvfile_remove(index, NULL, "127.0.0.1");
2893 for (list = request_list; list; list = list->next) {
2894 struct request_data *req = list->data;
2896 DBG("Dropping request (id 0x%04x -> 0x%04x)",
2897 req->srcid, req->dstid);
2898 destroy_request_data(req);
2902 g_slist_free(request_list);
2903 request_list = NULL;
2905 destroy_tcp_listener(ifdata);
2906 destroy_udp_listener(ifdata);
2909 int __connman_dnsproxy_add_listener(int index)
2911 struct listener_data *ifdata;
2914 DBG("index %d", index);
2919 if (g_hash_table_lookup(listener_table, GINT_TO_POINTER(index)) != NULL)
2922 ifdata = g_try_new0(struct listener_data, 1);
2926 ifdata->index = index;
2927 ifdata->udp_listener_channel = NULL;
2928 ifdata->udp_listener_watch = 0;
2929 ifdata->tcp_listener_channel = NULL;
2930 ifdata->tcp_listener_watch = 0;
2932 err = create_listener(ifdata);
2934 connman_error("Couldn't create listener for index %d err %d",
2939 g_hash_table_insert(listener_table, GINT_TO_POINTER(ifdata->index),
2944 void __connman_dnsproxy_remove_listener(int index)
2946 struct listener_data *ifdata;
2948 DBG("index %d", index);
2950 ifdata = g_hash_table_lookup(listener_table, GINT_TO_POINTER(index));
2954 destroy_listener(ifdata);
2956 g_hash_table_remove(listener_table, GINT_TO_POINTER(index));
2959 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
2961 int index = GPOINTER_TO_INT(key);
2962 struct listener_data *ifdata = value;
2964 DBG("index %d", index);
2966 destroy_listener(ifdata);
2969 int __connman_dnsproxy_init(void)
2975 srandom(time(NULL));
2977 listener_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2980 index = connman_inet_ifindex("lo");
2981 err = __connman_dnsproxy_add_listener(index);
2985 err = connman_notifier_register(&dnsproxy_notifier);
2992 __connman_dnsproxy_remove_listener(index);
2993 g_hash_table_destroy(listener_table);
2998 void __connman_dnsproxy_cleanup(void)
3002 connman_notifier_unregister(&dnsproxy_notifier);
3004 g_hash_table_foreach(listener_table, remove_listener, NULL);
3006 g_hash_table_destroy(listener_table);