5 * Copyright (C) 2007-2014 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>
38 #include <gweb/gresolv.h>
44 #define debug(fmt...) do { } while (0)
46 #if __BYTE_ORDER == __LITTLE_ENDIAN
61 } __attribute__ ((packed));
62 #elif __BYTE_ORDER == __BIG_ENDIAN
77 } __attribute__ ((packed));
79 #error "Unknown byte order"
85 } __attribute__ ((packed));
87 struct partial_reply {
97 struct sockaddr *server_addr;
98 socklen_t server_addr_len;
105 struct partial_reply *incoming_reply;
108 struct request_data {
110 struct sockaddr_in6 __sin6; /* Only for the length */
129 struct listener_data *ifdata;
133 struct listener_data {
136 GIOChannel *udp4_listener_channel;
137 GIOChannel *tcp4_listener_channel;
138 guint udp4_listener_watch;
139 guint tcp4_listener_watch;
141 GIOChannel *udp6_listener_channel;
142 GIOChannel *tcp6_listener_channel;
143 guint udp6_listener_watch;
144 guint tcp6_listener_watch;
148 * The TCP client requires some extra handling as we need to
149 * be prepared to receive also partial DNS requests.
151 struct tcp_partial_client_data {
153 struct listener_data *ifdata;
157 unsigned int buf_end;
168 unsigned int data_len;
169 unsigned char *data; /* contains DNS header + body */
176 struct cache_data *ipv4;
177 struct cache_data *ipv6;
180 struct domain_question {
183 } __attribute__ ((packed));
190 } __attribute__ ((packed));
193 * Max length of the DNS TCP packet.
195 #define TCP_MAX_BUF_LEN 4096
198 * We limit how long the cached DNS entry stays in the cache.
199 * By default the TTL (time-to-live) of the DNS response is used
200 * when setting the cache entry life time. The value is in seconds.
202 #define MAX_CACHE_TTL (60 * 30)
204 * Also limit the other end, cache at least for 30 seconds.
206 #define MIN_CACHE_TTL (30)
209 * We limit the cache size to some sane value so that cached data does
210 * not occupy too much memory. Each cached entry occupies on average
211 * about 100 bytes memory (depending on DNS name length).
212 * Example: caching www.connman.net uses 97 bytes memory.
213 * The value is the max amount of cached DNS responses (count).
215 #define MAX_CACHE_SIZE 256
217 static int cache_size;
218 static GHashTable *cache;
219 static int cache_refcount;
220 static GSList *server_list = NULL;
221 static GSList *request_list = NULL;
222 static GHashTable *listener_table = NULL;
223 static time_t next_refresh;
224 static GHashTable *partial_tcp_req_table;
225 static guint cache_timer = 0;
227 static guint16 get_id(void)
231 __connman_util_get_random(&rand);
236 static int protocol_offset(int protocol)
252 * There is a power and efficiency benefit to have entries
253 * in our cache expire at the same time. To this extend,
254 * we round down the cache valid time to common boundaries.
256 static time_t round_down_ttl(time_t end_time, int ttl)
261 /* Less than 5 minutes, round to 10 second boundary */
263 end_time = end_time / 10;
264 end_time = end_time * 10;
265 } else { /* 5 or more minutes, round to 30 seconds */
266 end_time = end_time / 30;
267 end_time = end_time * 30;
272 static struct request_data *find_request(guint16 id)
276 for (list = request_list; list; list = list->next) {
277 struct request_data *req = list->data;
279 if (req->dstid == id || req->altid == id)
286 static struct server_data *find_server(int index,
292 debug("index %d server %s proto %d", index, server, protocol);
294 for (list = server_list; list; list = list->next) {
295 struct server_data *data = list->data;
297 if (index < 0 && data->index < 0 &&
298 g_str_equal(data->server, server) &&
299 data->protocol == protocol)
303 data->index < 0 || !data->server)
306 if (data->index == index &&
307 g_str_equal(data->server, server) &&
308 data->protocol == protocol)
315 /* we can keep using the same resolve's */
316 static GResolv *ipv4_resolve;
317 static GResolv *ipv6_resolve;
319 static void dummy_resolve_func(GResolvResultStatus status,
320 char **results, gpointer user_data)
325 * Refresh a DNS entry, but also age the hit count a bit */
326 static void refresh_dns_entry(struct cache_entry *entry, char *name)
331 ipv4_resolve = g_resolv_new(0);
332 g_resolv_set_address_family(ipv4_resolve, AF_INET);
333 g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
337 ipv6_resolve = g_resolv_new(0);
338 g_resolv_set_address_family(ipv6_resolve, AF_INET6);
339 g_resolv_add_nameserver(ipv6_resolve, "::1", 53, 0);
343 debug("Refreshing A record for %s", name);
344 g_resolv_lookup_hostname(ipv4_resolve, name,
345 dummy_resolve_func, NULL);
350 debug("Refreshing AAAA record for %s", name);
351 g_resolv_lookup_hostname(ipv6_resolve, name,
352 dummy_resolve_func, NULL);
361 static int dns_name_length(unsigned char *buf)
363 if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
365 return strlen((char *)buf) + 1;
368 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
374 /* skip the header */
378 /* skip the query, which is a name and 2 16 bit words */
379 l = dns_name_length(c);
385 /* now we get the answer records */
389 l = dns_name_length(c);
394 /* then type + class, 2 bytes each */
400 /* now the 4 byte TTL field */
401 c[0] = new_ttl >> 24 & 0xff;
402 c[1] = new_ttl >> 16 & 0xff;
403 c[2] = new_ttl >> 8 & 0xff;
404 c[3] = new_ttl & 0xff;
410 /* now the 2 byte rdlen field */
411 w = c[0] << 8 | c[1];
417 static void send_cached_response(int sk, unsigned char *buf, int len,
418 const struct sockaddr *to, socklen_t tolen,
419 int protocol, int id, uint16_t answers, int ttl)
421 struct domain_hdr *hdr;
422 unsigned char *ptr = buf;
423 int err, offset, dns_len, adj_len = len - 2;
426 * The cached packet contains always the TCP offset (two bytes)
427 * so skip them for UDP.
438 dns_len = ptr[0] * 256 + ptr[1];
447 hdr = (void *) (ptr + offset);
451 hdr->rcode = ns_r_noerror;
452 hdr->ancount = htons(answers);
456 /* if this is a negative reply, we are authoritative */
460 update_cached_ttl((unsigned char *)hdr, adj_len, ttl);
462 debug("sk %d id 0x%04x answers %d ptr %p length %d dns %d",
463 sk, hdr->id, answers, ptr, len, dns_len);
465 err = sendto(sk, ptr, len, MSG_NOSIGNAL, to, tolen);
467 connman_error("Cannot send cached DNS response: %s",
472 if (err != len || (dns_len != (len - 2) && protocol == IPPROTO_TCP) ||
473 (dns_len != len && protocol == IPPROTO_UDP))
474 debug("Packet length mismatch, sent %d wanted %d dns %d",
478 static void send_response(int sk, unsigned char *buf, size_t len,
479 const struct sockaddr *to, socklen_t tolen,
482 struct domain_hdr *hdr;
483 int err, offset = protocol_offset(protocol);
490 if (len < sizeof(*hdr) + offset)
493 hdr = (void *) (buf + offset);
496 buf[1] = sizeof(*hdr);
499 debug("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
502 hdr->rcode = ns_r_servfail;
509 err = sendto(sk, buf, sizeof(*hdr) + offset, MSG_NOSIGNAL, to, tolen);
511 connman_error("Failed to send DNS response to %d: %s",
512 sk, strerror(errno));
517 static int get_req_udp_socket(struct request_data *req)
521 if (req->family == AF_INET)
522 channel = req->ifdata->udp4_listener_channel;
524 channel = req->ifdata->udp6_listener_channel;
529 return g_io_channel_unix_get_fd(channel);
532 static void destroy_request_data(struct request_data *req)
534 if (req->timeout > 0)
535 g_source_remove(req->timeout);
538 g_free(req->request);
543 static gboolean request_timeout(gpointer user_data)
545 struct request_data *req = user_data;
552 debug("id 0x%04x", req->srcid);
554 request_list = g_slist_remove(request_list, req);
556 if (req->protocol == IPPROTO_UDP) {
557 sk = get_req_udp_socket(req);
559 } else if (req->protocol == IPPROTO_TCP) {
565 if (req->resplen > 0 && req->resp) {
567 * Here we have received at least one reply (probably telling
568 * "not found" result), so send that back to client instead
569 * of more fatal server failed error.
572 sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
575 } else if (req->request) {
577 * There was not reply from server at all.
579 struct domain_hdr *hdr;
581 hdr = (void *)(req->request + protocol_offset(req->protocol));
582 hdr->id = req->srcid;
585 send_response(sk, req->request, req->request_len,
586 sa, req->sa_len, req->protocol);
590 * We cannot leave TCP client hanging so just kick it out
591 * if we get a request timeout from server.
593 if (req->protocol == IPPROTO_TCP) {
594 debug("client %d removed", req->client_sk);
595 g_hash_table_remove(partial_tcp_req_table,
596 GINT_TO_POINTER(req->client_sk));
601 destroy_request_data(req);
606 static int append_query(unsigned char *buf, unsigned int size,
607 const char *query, const char *domain)
609 unsigned char *ptr = buf;
612 debug("query %s domain %s", query, domain);
617 tmp = strchr(query, '.');
623 memcpy(ptr + 1, query, len);
629 memcpy(ptr + 1, query, tmp - query);
630 ptr += tmp - query + 1;
638 tmp = strchr(domain, '.');
640 len = strlen(domain);
644 memcpy(ptr + 1, domain, len);
650 memcpy(ptr + 1, domain, tmp - domain);
651 ptr += tmp - domain + 1;
661 static bool cache_check_is_valid(struct cache_data *data,
667 if (data->cache_until < current_time)
674 * remove stale cached entries so that they can be refreshed
676 static void cache_enforce_validity(struct cache_entry *entry)
678 time_t current_time = time(NULL);
680 if (!cache_check_is_valid(entry->ipv4, current_time)
682 debug("cache timeout \"%s\" type A", entry->key);
683 g_free(entry->ipv4->data);
689 if (!cache_check_is_valid(entry->ipv6, current_time)
691 debug("cache timeout \"%s\" type AAAA", entry->key);
692 g_free(entry->ipv6->data);
698 static uint16_t cache_check_validity(char *question, uint16_t type,
699 struct cache_entry *entry)
701 time_t current_time = time(NULL);
702 bool want_refresh = false;
705 * if we have a popular entry, we want a refresh instead of
706 * total destruction of the entry.
711 cache_enforce_validity(entry);
715 if (!cache_check_is_valid(entry->ipv4, current_time)) {
716 debug("cache %s \"%s\" type A", entry->ipv4 ?
717 "timeout" : "entry missing", question);
720 entry->want_refresh = true;
723 * We do not remove cache entry if there is still
724 * valid IPv6 entry found in the cache.
726 if (!cache_check_is_valid(entry->ipv6, current_time) && !want_refresh) {
727 g_hash_table_remove(cache, question);
734 if (!cache_check_is_valid(entry->ipv6, current_time)) {
735 debug("cache %s \"%s\" type AAAA", entry->ipv6 ?
736 "timeout" : "entry missing", question);
739 entry->want_refresh = true;
741 if (!cache_check_is_valid(entry->ipv4, current_time) && !want_refresh) {
742 g_hash_table_remove(cache, question);
752 static void cache_element_destroy(gpointer value)
754 struct cache_entry *entry = value;
760 g_free(entry->ipv4->data);
765 g_free(entry->ipv6->data);
772 if (--cache_size < 0)
776 static gboolean try_remove_cache(gpointer user_data)
780 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1) {
781 debug("No cache users, removing it.");
783 g_hash_table_destroy(cache);
790 static void create_cache(void)
792 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
793 cache = g_hash_table_new_full(g_str_hash,
796 cache_element_destroy);
799 static struct cache_entry *cache_check(gpointer request, int *qtype, int proto)
802 struct cache_entry *entry;
803 struct domain_question *q;
805 int offset, proto_offset;
810 proto_offset = protocol_offset(proto);
811 if (proto_offset < 0)
814 question = request + proto_offset + 12;
816 offset = strlen(question) + 1;
817 q = (void *) (question + offset);
818 type = ntohs(q->type);
820 /* We only cache either A (1) or AAAA (28) requests */
821 if (type != 1 && type != 28)
829 entry = g_hash_table_lookup(cache, question);
833 type = cache_check_validity(question, type, entry);
842 * Get a label/name from DNS resource record. The function decompresses the
843 * label if necessary. The function does not convert the name to presentation
844 * form. This means that the result string will contain label lengths instead
845 * of dots between labels. We intentionally do not want to convert to dotted
846 * format so that we can cache the wire format string directly.
848 static int get_name(int counter,
849 unsigned char *pkt, unsigned char *start, unsigned char *max,
850 unsigned char *output, int output_max, int *output_len,
851 unsigned char **end, char *name, size_t max_name, int *name_len)
855 /* Limit recursion to 10 (this means up to 10 labels in domain name) */
861 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
862 uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
864 if (offset >= max - pkt)
870 return get_name(counter + 1, pkt, pkt + offset, max,
871 output, output_max, output_len, end,
872 name, max_name, name_len);
874 unsigned label_len = *p;
876 if (pkt + label_len > max)
879 if (*output_len > output_max)
882 if ((*name_len + 1 + label_len + 1) > max_name)
886 * We need the original name in order to check
887 * if this answer is the correct one.
889 name[(*name_len)++] = label_len;
890 memcpy(name + *name_len, p + 1, label_len + 1);
891 *name_len += label_len;
893 /* We compress the result */
894 output[0] = NS_CMPRSFLGS;
911 static int parse_rr(unsigned char *buf, unsigned char *start,
913 unsigned char *response, unsigned int *response_size,
914 uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
916 char *name, size_t max_name)
918 struct domain_rr *rr;
920 int name_len = 0, output_len = 0, max_rsp = *response_size;
922 err = get_name(0, buf, start, max, response, max_rsp,
923 &output_len, end, name, max_name, &name_len);
929 if ((unsigned int) offset > *response_size)
932 rr = (void *) (*end);
937 *type = ntohs(rr->type);
938 *class = ntohs(rr->class);
939 *ttl = ntohl(rr->ttl);
940 *rdlen = ntohs(rr->rdlen);
945 memcpy(response + offset, *end, sizeof(struct domain_rr));
947 offset += sizeof(struct domain_rr);
948 *end += sizeof(struct domain_rr);
950 if ((unsigned int) (offset + *rdlen) > *response_size)
953 memcpy(response + offset, *end, *rdlen);
957 *response_size = offset + *rdlen;
962 static bool check_alias(GSList *aliases, char *name)
967 for (list = aliases; list; list = list->next) {
968 int len = strlen((char *)list->data);
969 if (strncmp((char *)list->data, name, len) == 0)
977 static int parse_response(unsigned char *buf, int buflen,
978 char *question, int qlen,
979 uint16_t *type, uint16_t *class, int *ttl,
980 unsigned char *response, unsigned int *response_len,
983 struct domain_hdr *hdr = (void *) buf;
984 struct domain_question *q;
986 uint16_t qdcount = ntohs(hdr->qdcount);
987 uint16_t ancount = ntohs(hdr->ancount);
989 uint16_t qtype, qclass;
990 unsigned char *next = NULL;
991 unsigned int maxlen = *response_len;
992 GSList *aliases = NULL, *list;
993 char name[NS_MAXDNAME + 1];
998 debug("qr %d qdcount %d", hdr->qr, qdcount);
1000 /* We currently only cache responses where question count is 1 */
1001 if (hdr->qr != 1 || qdcount != 1)
1004 ptr = buf + sizeof(struct domain_hdr);
1006 strncpy(question, (char *) ptr, qlen);
1007 qlen = strlen(question);
1008 ptr += qlen + 1; /* skip \0 */
1011 qtype = ntohs(q->type);
1013 /* We cache only A and AAAA records */
1014 if (qtype != 1 && qtype != 28)
1017 qclass = ntohs(q->class);
1019 ptr += 2 + 2; /* ptr points now to answers */
1025 memset(name, 0, sizeof(name));
1028 * We have a bunch of answers (like A, AAAA, CNAME etc) to
1029 * A or AAAA question. We traverse the answers and parse the
1030 * resource records. Only A and AAAA records are cached, all
1031 * the other records in answers are skipped.
1033 for (i = 0; i < ancount; i++) {
1035 * Get one address at a time to this buffer.
1036 * The max size of the answer is
1037 * 2 (pointer) + 2 (type) + 2 (class) +
1038 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
1039 * for A or AAAA record.
1040 * For CNAME the size can be bigger.
1042 unsigned char rsp[NS_MAXCDNAME];
1043 unsigned int rsp_len = sizeof(rsp) - 1;
1046 memset(rsp, 0, sizeof(rsp));
1048 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
1049 type, class, ttl, &rdlen, &next, name,
1057 * Now rsp contains compressed or uncompressed resource
1058 * record. Next we check if this record answers the question.
1059 * The name var contains the uncompressed label.
1060 * One tricky bit is the CNAME records as they alias
1061 * the name we might be interested in.
1065 * Go to next answer if the class is not the one we are
1068 if (*class != qclass) {
1075 * Try to resolve aliases also, type is CNAME(5).
1076 * This is important as otherwise the aliased names would not
1077 * be cached at all as the cache would not contain the aliased
1080 * If any CNAME is found in DNS packet, then we cache the alias
1081 * IP address instead of the question (as the server
1082 * said that question has only an alias).
1083 * This means in practice that if e.g., ipv6.google.com is
1084 * queried, DNS server returns CNAME of that name which is
1085 * ipv6.l.google.com. We then cache the address of the CNAME
1086 * but return the question name to client. So the alias
1087 * status of the name is not saved in cache and thus not
1088 * returned to the client. We do not return DNS packets from
1089 * cache to client saying that ipv6.google.com is an alias to
1090 * ipv6.l.google.com but we return instead a DNS packet that
1091 * says ipv6.google.com has address xxx which is in fact the
1092 * address of ipv6.l.google.com. For caching purposes this
1093 * should not cause any issues.
1095 if (*type == 5 && strncmp(question, name, qlen) == 0) {
1097 * So now the alias answered the question. This is
1098 * not very useful from caching point of view as
1099 * the following A or AAAA records will not match the
1100 * question. We need to find the real A/AAAA record
1101 * of the alias and cache that.
1103 unsigned char *end = NULL;
1104 int name_len = 0, output_len = 0;
1106 memset(rsp, 0, sizeof(rsp));
1107 rsp_len = sizeof(rsp) - 1;
1110 * Alias is in rdata part of the message,
1111 * and next-rdlen points to it. So we need to get
1112 * the real name of the alias.
1114 ret = get_name(0, buf, next - rdlen, buf + buflen,
1115 rsp, rsp_len, &output_len, &end,
1116 name, sizeof(name) - 1, &name_len);
1118 /* just ignore the error at this point */
1125 * We should now have the alias of the entry we might
1126 * want to cache. Just remember it for a while.
1127 * We check the alias list when we have parsed the
1130 aliases = g_slist_prepend(aliases, g_strdup(name));
1137 if (*type == qtype) {
1139 * We found correct type (A or AAAA)
1141 if (check_alias(aliases, name) ||
1142 (!aliases && strncmp(question, name,
1145 * We found an alias or the name of the rr
1146 * matches the question. If so, we append
1147 * the compressed label to the cache.
1148 * The end result is a response buffer that
1149 * will contain one or more cached and
1150 * compressed resource records.
1152 if (*response_len + rsp_len > maxlen) {
1156 memcpy(response + *response_len, rsp, rsp_len);
1157 *response_len += rsp_len;
1168 for (list = aliases; list; list = list->next)
1170 g_slist_free(aliases);
1175 struct cache_timeout {
1176 time_t current_time;
1181 static gboolean cache_check_entry(gpointer key, gpointer value,
1184 struct cache_timeout *data = user_data;
1185 struct cache_entry *entry = value;
1188 /* Scale the number of hits by half as part of cache aging */
1193 * If either IPv4 or IPv6 cached entry has expired, we
1194 * remove both from the cache.
1197 if (entry->ipv4 && entry->ipv4->timeout > 0) {
1198 max_timeout = entry->ipv4->cache_until;
1199 if (max_timeout > data->max_timeout)
1200 data->max_timeout = max_timeout;
1202 if (entry->ipv4->cache_until < data->current_time)
1206 if (entry->ipv6 && entry->ipv6->timeout > 0) {
1207 max_timeout = entry->ipv6->cache_until;
1208 if (max_timeout > data->max_timeout)
1209 data->max_timeout = max_timeout;
1211 if (entry->ipv6->cache_until < data->current_time)
1216 * if we're asked to try harder, also remove entries that have
1219 if (data->try_harder && entry->hits < 4)
1225 static void cache_cleanup(void)
1227 static int max_timeout;
1228 struct cache_timeout data;
1231 data.current_time = time(NULL);
1232 data.max_timeout = 0;
1233 data.try_harder = 0;
1236 * In the first pass, we only remove entries that have timed out.
1237 * We use a cache of the first time to expire to do this only
1238 * when it makes sense.
1240 if (max_timeout <= data.current_time) {
1241 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1244 debug("removed %d in the first pass", count);
1247 * In the second pass, if the first pass turned up blank,
1248 * we also expire entries with a low hit count,
1249 * while aging the hit count at the same time.
1251 data.try_harder = 1;
1253 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1258 * If we could not remove anything, then remember
1259 * what is the max timeout and do nothing if we
1260 * have not yet reached it. This will prevent
1261 * constant traversal of the cache if it is full.
1263 max_timeout = data.max_timeout;
1268 static gboolean cache_invalidate_entry(gpointer key, gpointer value,
1271 struct cache_entry *entry = value;
1273 /* first, delete any expired elements */
1274 cache_enforce_validity(entry);
1276 /* if anything is not expired, mark the entry for refresh */
1277 if (entry->hits > 0 && (entry->ipv4 || entry->ipv6))
1278 entry->want_refresh = true;
1280 /* delete the cached data */
1282 g_free(entry->ipv4->data);
1283 g_free(entry->ipv4);
1288 g_free(entry->ipv6->data);
1289 g_free(entry->ipv6);
1293 /* keep the entry if we want it refreshed, delete it otherwise */
1294 if (entry->want_refresh)
1301 * cache_invalidate is called from places where the DNS landscape
1302 * has changed, say because connections are added or we entered a VPN.
1303 * The logic is to wipe all cache data, but mark all non-expired
1304 * parts of the cache for refresh rather than deleting the whole cache.
1306 static void cache_invalidate(void)
1308 debug("Invalidating the DNS cache %p", cache);
1313 g_hash_table_foreach_remove(cache, cache_invalidate_entry, NULL);
1316 static void cache_refresh_entry(struct cache_entry *entry)
1319 cache_enforce_validity(entry);
1321 if (entry->hits > 2 && !entry->ipv4)
1322 entry->want_refresh = true;
1323 if (entry->hits > 2 && !entry->ipv6)
1324 entry->want_refresh = true;
1326 if (entry->want_refresh) {
1328 char dns_name[NS_MAXDNAME + 1];
1329 entry->want_refresh = false;
1331 /* turn a DNS name into a hostname with dots */
1332 strncpy(dns_name, entry->key, NS_MAXDNAME);
1340 debug("Refreshing %s\n", dns_name);
1341 /* then refresh the hostname */
1342 refresh_dns_entry(entry, &dns_name[1]);
1346 static void cache_refresh_iterator(gpointer key, gpointer value,
1349 struct cache_entry *entry = value;
1351 cache_refresh_entry(entry);
1354 static void cache_refresh(void)
1359 g_hash_table_foreach(cache, cache_refresh_iterator, NULL);
1362 static int reply_query_type(unsigned char *msg, int len)
1368 /* skip the header */
1369 c = msg + sizeof(struct domain_hdr);
1370 len -= sizeof(struct domain_hdr);
1375 /* now the query, which is a name and 2 16 bit words */
1376 l = dns_name_length(c);
1378 type = c[0] << 8 | c[1];
1383 static int cache_update(struct server_data *srv, unsigned char *msg,
1384 unsigned int msg_len)
1386 int offset = protocol_offset(srv->protocol);
1387 int err, qlen, ttl = 0;
1388 uint16_t answers = 0, type = 0, class = 0;
1389 struct domain_hdr *hdr = (void *)(msg + offset);
1390 struct domain_question *q;
1391 struct cache_entry *entry;
1392 struct cache_data *data;
1393 char question[NS_MAXDNAME + 1];
1394 unsigned char response[NS_MAXDNAME + 1];
1396 unsigned int rsplen;
1397 bool new_entry = true;
1398 time_t current_time;
1400 if (cache_size >= MAX_CACHE_SIZE) {
1402 if (cache_size >= MAX_CACHE_SIZE)
1406 current_time = time(NULL);
1408 /* don't do a cache refresh more than twice a minute */
1409 if (next_refresh < current_time) {
1411 next_refresh = current_time + 30;
1417 debug("offset %d hdr %p msg %p rcode %d", offset, hdr, msg, hdr->rcode);
1419 /* Continue only if response code is 0 (=ok) */
1420 if (hdr->rcode != ns_r_noerror)
1426 rsplen = sizeof(response) - 1;
1427 question[sizeof(question) - 1] = '\0';
1429 err = parse_response(msg + offset, msg_len - offset,
1430 question, sizeof(question) - 1,
1431 &type, &class, &ttl,
1432 response, &rsplen, &answers);
1435 * special case: if we do a ipv6 lookup and get no result
1436 * for a record that's already in our ipv4 cache.. we want
1437 * to cache the negative response.
1439 if ((err == -ENOMSG || err == -ENOBUFS) &&
1440 reply_query_type(msg + offset,
1441 msg_len - offset) == 28) {
1442 entry = g_hash_table_lookup(cache, question);
1443 if (entry && entry->ipv4 && !entry->ipv6) {
1444 int cache_offset = 0;
1446 data = g_try_new(struct cache_data, 1);
1449 data->inserted = entry->ipv4->inserted;
1451 data->answers = ntohs(hdr->ancount);
1452 data->timeout = entry->ipv4->timeout;
1453 if (srv->protocol == IPPROTO_UDP)
1455 data->data_len = msg_len + cache_offset;
1456 data->data = ptr = g_malloc(data->data_len);
1457 ptr[0] = (data->data_len - 2) / 256;
1458 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1459 if (srv->protocol == IPPROTO_UDP)
1461 data->valid_until = entry->ipv4->valid_until;
1462 data->cache_until = entry->ipv4->cache_until;
1463 memcpy(ptr, msg, msg_len);
1466 * we will get a "hit" when we serve the response
1470 if (entry->hits < 0)
1476 if (err < 0 || ttl == 0)
1479 qlen = strlen(question);
1482 * If the cache contains already data, check if the
1483 * type of the cached data is the same and do not add
1484 * to cache if data is already there.
1485 * This is needed so that we can cache both A and AAAA
1486 * records for the same name.
1488 entry = g_hash_table_lookup(cache, question);
1490 entry = g_try_new(struct cache_entry, 1);
1494 data = g_try_new(struct cache_data, 1);
1500 entry->key = g_strdup(question);
1501 entry->ipv4 = entry->ipv6 = NULL;
1502 entry->want_refresh = false;
1510 if (type == 1 && entry->ipv4)
1513 if (type == 28 && entry->ipv6)
1516 data = g_try_new(struct cache_data, 1);
1526 * compensate for the hit we'll get for serving
1527 * the response out of the cache
1530 if (entry->hits < 0)
1536 if (ttl < MIN_CACHE_TTL)
1537 ttl = MIN_CACHE_TTL;
1539 data->inserted = current_time;
1541 data->answers = answers;
1542 data->timeout = ttl;
1544 * The "2" in start of the length is the TCP offset. We allocate it
1545 * here even for UDP packet because it simplifies the sending
1548 data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
1549 data->data = ptr = g_malloc(data->data_len);
1550 data->valid_until = current_time + ttl;
1553 * Restrict the cached DNS record TTL to some sane value
1554 * in order to prevent data staying in the cache too long.
1556 if (ttl > MAX_CACHE_TTL)
1557 ttl = MAX_CACHE_TTL;
1559 data->cache_until = round_down_ttl(current_time + ttl, ttl);
1569 * We cache the two extra bytes at the start of the message
1570 * in a TCP packet. When sending UDP packet, we skip the first
1571 * two bytes. This way we do not need to know the format
1572 * (UDP/TCP) of the cached message.
1574 if (srv->protocol == IPPROTO_UDP)
1575 memcpy(ptr + 2, msg, offset + 12);
1577 memcpy(ptr, msg, offset + 12);
1579 ptr[0] = (data->data_len - 2) / 256;
1580 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1581 if (srv->protocol == IPPROTO_UDP)
1584 memcpy(ptr + offset + 12, question, qlen + 1); /* copy also the \0 */
1586 q = (void *) (ptr + offset + 12 + qlen + 1);
1587 q->type = htons(type);
1588 q->class = htons(class);
1589 memcpy(ptr + offset + 12 + qlen + 1 + sizeof(struct domain_question),
1593 g_hash_table_replace(cache, entry->key, entry);
1597 debug("cache %d %squestion \"%s\" type %d ttl %d size %zd packet %u "
1599 cache_size, new_entry ? "new " : "old ",
1600 question, type, ttl,
1601 sizeof(*entry) + sizeof(*data) + data->data_len + qlen,
1603 srv->protocol == IPPROTO_TCP ?
1604 (unsigned int)(data->data[0] * 256 + data->data[1]) :
1610 static int ns_resolv(struct server_data *server, struct request_data *req,
1611 gpointer request, gpointer name)
1614 int sk, err, type = 0;
1615 char *dot, *lookup = (char *) name;
1616 struct cache_entry *entry;
1618 entry = cache_check(request, &type, req->protocol);
1621 struct cache_data *data;
1623 debug("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1630 ttl_left = data->valid_until - time(NULL);
1634 if (data && req->protocol == IPPROTO_TCP) {
1635 send_cached_response(req->client_sk, data->data,
1636 data->data_len, NULL, 0, IPPROTO_TCP,
1637 req->srcid, data->answers, ttl_left);
1641 if (data && req->protocol == IPPROTO_UDP) {
1642 int udp_sk = get_req_udp_socket(req);
1647 send_cached_response(udp_sk, data->data,
1648 data->data_len, &req->sa, req->sa_len,
1649 IPPROTO_UDP, req->srcid, data->answers,
1655 sk = g_io_channel_unix_get_fd(server->channel);
1657 err = sendto(sk, request, req->request_len, MSG_NOSIGNAL,
1658 server->server_addr, server->server_addr_len);
1660 debug("Cannot send message to server %s sock %d "
1661 "protocol %d (%s/%d)",
1662 server->server, sk, server->protocol,
1663 strerror(errno), errno);
1669 /* If we have more than one dot, we don't add domains */
1670 dot = strchr(lookup, '.');
1671 if (dot && dot != lookup + strlen(lookup) - 1)
1674 if (server->domains && server->domains->data)
1675 req->append_domain = true;
1677 for (list = server->domains; list; list = list->next) {
1679 unsigned char alt[1024];
1680 struct domain_hdr *hdr = (void *) &alt;
1681 int altlen, domlen, offset;
1683 domain = list->data;
1688 offset = protocol_offset(server->protocol);
1692 domlen = strlen(domain) + 1;
1696 alt[offset] = req->altid & 0xff;
1697 alt[offset + 1] = req->altid >> 8;
1699 memcpy(alt + offset + 2, request + offset + 2, 10);
1700 hdr->qdcount = htons(1);
1702 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1709 memcpy(alt + offset + altlen,
1710 request + offset + altlen - domlen,
1711 req->request_len - altlen - offset + domlen);
1713 if (server->protocol == IPPROTO_TCP) {
1714 int req_len = req->request_len + domlen - 2;
1716 alt[0] = (req_len >> 8) & 0xff;
1717 alt[1] = req_len & 0xff;
1720 debug("req %p dstid 0x%04x altid 0x%04x", req, req->dstid,
1723 err = send(sk, alt, req->request_len + domlen, MSG_NOSIGNAL);
1733 static char *convert_label(char *start, char *end, char *ptr, char *uptr,
1734 int remaining_len, int *used_comp, int *used_uncomp)
1737 char name[NS_MAXLABEL];
1739 pos = dn_expand((u_char *)start, (u_char *)end, (u_char *)ptr,
1742 debug("uncompress error [%d/%s]", errno, strerror(errno));
1747 * We need to compress back the name so that we get back to internal
1748 * label presentation.
1750 comp_pos = dn_comp(name, (u_char *)uptr, remaining_len, NULL, NULL);
1752 debug("compress error [%d/%s]", errno, strerror(errno));
1757 *used_uncomp = comp_pos;
1765 static char *uncompress(int16_t field_count, char *start, char *end,
1766 char *ptr, char *uncompressed, int uncomp_len,
1767 char **uncompressed_ptr)
1769 char *uptr = *uncompressed_ptr; /* position in result buffer */
1771 debug("count %d ptr %p end %p uptr %p", field_count, ptr, end, uptr);
1773 while (field_count-- > 0 && ptr < end) {
1774 int dlen; /* data field length */
1775 int ulen; /* uncompress length */
1776 int pos; /* position in compressed string */
1777 char name[NS_MAXLABEL]; /* tmp label */
1778 uint16_t dns_type, dns_class;
1781 if (!convert_label(start, end, ptr, name, NS_MAXLABEL,
1786 * Copy the uncompressed resource record, type, class and \0 to
1790 ulen = strlen(name);
1791 strncpy(uptr, name, uncomp_len - (uptr - uncompressed));
1793 debug("pos %d ulen %d left %d name %s", pos, ulen,
1794 (int)(uncomp_len - (uptr - uncompressed)), uptr);
1802 * We copy also the fixed portion of the result (type, class,
1803 * ttl, address length and the address)
1805 memcpy(uptr, ptr, NS_RRFIXEDSZ);
1807 dns_type = uptr[0] << 8 | uptr[1];
1808 dns_class = uptr[2] << 8 | uptr[3];
1810 if (dns_class != ns_c_in)
1813 ptr += NS_RRFIXEDSZ;
1814 uptr += NS_RRFIXEDSZ;
1817 * Then the variable portion of the result (data length).
1818 * Typically this portion is also compressed
1819 * so we need to uncompress it also when necessary.
1821 if (dns_type == ns_t_cname) {
1822 if (!convert_label(start, end, ptr, uptr,
1823 uncomp_len - (uptr - uncompressed),
1827 uptr[-2] = comp_pos << 8;
1828 uptr[-1] = comp_pos & 0xff;
1833 } else if (dns_type == ns_t_a || dns_type == ns_t_aaaa) {
1834 dlen = uptr[-2] << 8 | uptr[-1];
1836 if (ptr + dlen > end) {
1837 debug("data len %d too long", dlen);
1841 memcpy(uptr, ptr, dlen);
1845 } else if (dns_type == ns_t_soa) {
1849 /* Primary name server expansion */
1850 if (!convert_label(start, end, ptr, uptr,
1851 uncomp_len - (uptr - uncompressed),
1855 total_len += comp_pos;
1856 len_ptr = &uptr[-2];
1860 /* Responsible authority's mailbox */
1861 if (!convert_label(start, end, ptr, uptr,
1862 uncomp_len - (uptr - uncompressed),
1866 total_len += comp_pos;
1871 * Copy rest of the soa fields (serial number,
1872 * refresh interval, retry interval, expiration
1873 * limit and minimum ttl). They are 20 bytes long.
1875 memcpy(uptr, ptr, 20);
1881 * Finally fix the length of the data part
1883 len_ptr[0] = total_len << 8;
1884 len_ptr[1] = total_len & 0xff;
1887 *uncompressed_ptr = uptr;
1896 static int strip_domains(char *name, char *answers, int maxlen)
1899 int name_len = strlen(name);
1900 char *ptr, *start = answers, *end = answers + maxlen;
1902 while (maxlen > 0) {
1903 ptr = strstr(answers, name);
1905 char *domain = ptr + name_len;
1908 int domain_len = strlen(domain);
1910 memmove(answers + name_len,
1911 domain + domain_len,
1912 end - (domain + domain_len));
1915 maxlen -= domain_len;
1919 answers += strlen(answers) + 1;
1920 answers += 2 + 2 + 4; /* skip type, class and ttl fields */
1922 data_len = answers[0] << 8 | answers[1];
1923 answers += 2; /* skip the length field */
1925 if (answers + data_len > end)
1928 answers += data_len;
1929 maxlen -= answers - ptr;
1935 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1936 struct server_data *data)
1938 struct domain_hdr *hdr;
1939 struct request_data *req;
1940 int dns_id, sk, err, offset = protocol_offset(protocol);
1945 hdr = (void *)(reply + offset);
1946 dns_id = reply[offset] | reply[offset + 1] << 8;
1948 debug("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1950 req = find_request(dns_id);
1954 debug("req %p dstid 0x%04x altid 0x%04x rcode %d",
1955 req, req->dstid, req->altid, hdr->rcode);
1957 reply[offset] = req->srcid & 0xff;
1958 reply[offset + 1] = req->srcid >> 8;
1962 if (hdr->rcode == ns_r_noerror || !req->resp) {
1963 unsigned char *new_reply = NULL;
1966 * If the domain name was append
1967 * remove it before forwarding the reply.
1968 * If there were more than one question, then this
1969 * domain name ripping can be hairy so avoid that
1970 * and bail out in that that case.
1972 * The reason we are doing this magic is that if the
1973 * user's DNS client tries to resolv hostname without
1974 * domain part, it also expects to get the result without
1975 * a domain name part.
1977 if (req->append_domain && ntohs(hdr->qdcount) == 1) {
1978 uint16_t domain_len = 0;
1979 uint16_t header_len;
1980 uint16_t dns_type, dns_class;
1981 uint8_t host_len, dns_type_pos;
1982 char uncompressed[NS_MAXDNAME], *uptr;
1983 char *ptr, *eom = (char *)reply + reply_len;
1986 * ptr points to the first char of the hostname.
1987 * ->hostname.domain.net
1989 header_len = offset + sizeof(struct domain_hdr);
1990 ptr = (char *)reply + header_len;
1994 domain_len = strnlen(ptr + 1 + host_len,
1995 reply_len - header_len);
1998 * If the query type is anything other than A or AAAA,
1999 * then bail out and pass the message as is.
2000 * We only want to deal with IPv4 or IPv6 addresses.
2002 dns_type_pos = host_len + 1 + domain_len + 1;
2004 dns_type = ptr[dns_type_pos] << 8 |
2005 ptr[dns_type_pos + 1];
2006 dns_class = ptr[dns_type_pos + 2] << 8 |
2007 ptr[dns_type_pos + 3];
2008 if (dns_type != ns_t_a && dns_type != ns_t_aaaa &&
2009 dns_class != ns_c_in) {
2010 debug("Pass msg dns type %d class %d",
2011 dns_type, dns_class);
2016 * Remove the domain name and replace it by the end
2017 * of reply. Check if the domain is really there
2018 * before trying to copy the data. We also need to
2019 * uncompress the answers if necessary.
2020 * The domain_len can be 0 because if the original
2021 * query did not contain a domain name, then we are
2022 * sending two packets, first without the domain name
2023 * and the second packet with domain name.
2024 * The append_domain is set to true even if we sent
2025 * the first packet without domain name. In this
2026 * case we end up in this branch.
2028 if (domain_len > 0) {
2029 int len = host_len + 1;
2030 int new_len, fixed_len;
2034 * First copy host (without domain name) into
2037 uptr = &uncompressed[0];
2038 memcpy(uptr, ptr, len);
2040 uptr[len] = '\0'; /* host termination */
2044 * Copy type and class fields of the question.
2046 ptr += len + domain_len + 1;
2047 memcpy(uptr, ptr, NS_QFIXEDSZ);
2050 * ptr points to answers after this
2053 uptr += NS_QFIXEDSZ;
2055 fixed_len = answers - uncompressed;
2058 * We then uncompress the result to buffer
2059 * so that we can rip off the domain name
2060 * part from the question. First answers,
2061 * then name server (authority) information,
2062 * and finally additional record info.
2065 ptr = uncompress(ntohs(hdr->ancount),
2066 (char *)reply + offset, eom,
2067 ptr, uncompressed, NS_MAXDNAME,
2072 ptr = uncompress(ntohs(hdr->nscount),
2073 (char *)reply + offset, eom,
2074 ptr, uncompressed, NS_MAXDNAME,
2079 ptr = uncompress(ntohs(hdr->arcount),
2080 (char *)reply + offset, eom,
2081 ptr, uncompressed, NS_MAXDNAME,
2087 * The uncompressed buffer now contains almost
2088 * valid response. Final step is to get rid of
2089 * the domain name because at least glibc
2090 * gethostbyname() implementation does extra
2091 * checks and expects to find an answer without
2092 * domain name if we asked a query without
2093 * domain part. Note that glibc getaddrinfo()
2094 * works differently and accepts FQDN in answer
2096 new_len = strip_domains(uncompressed, answers,
2099 debug("Corrupted packet");
2104 * Because we have now uncompressed the answers
2105 * we might have to create a bigger buffer to
2106 * hold all that data.
2109 reply_len = header_len + new_len + fixed_len;
2111 new_reply = g_try_malloc(reply_len);
2115 memcpy(new_reply, reply, header_len);
2116 memcpy(new_reply + header_len, uncompressed,
2117 new_len + fixed_len);
2127 req->resp = g_try_malloc(reply_len);
2131 memcpy(req->resp, reply, reply_len);
2132 req->resplen = reply_len;
2134 cache_update(data, reply, reply_len);
2140 if (req->numresp < req->numserv) {
2141 if (hdr->rcode > ns_r_noerror) {
2143 } else if (hdr->ancount == 0 && req->append_domain) {
2148 request_list = g_slist_remove(request_list, req);
2150 if (protocol == IPPROTO_UDP) {
2151 sk = get_req_udp_socket(req);
2156 err = sendto(sk, req->resp, req->resplen, 0,
2157 &req->sa, req->sa_len);
2159 sk = req->client_sk;
2160 err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
2164 debug("Cannot send msg, sk %d proto %d errno %d/%s", sk,
2165 protocol, errno, strerror(errno));
2167 debug("proto %d sent %d bytes to %d", protocol, err, sk);
2169 destroy_request_data(req);
2174 static void server_destroy_socket(struct server_data *data)
2176 debug("index %d server %s proto %d", data->index,
2177 data->server, data->protocol);
2179 if (data->watch > 0) {
2180 g_source_remove(data->watch);
2184 if (data->timeout > 0) {
2185 g_source_remove(data->timeout);
2189 if (data->channel) {
2190 g_io_channel_shutdown(data->channel, TRUE, NULL);
2191 g_io_channel_unref(data->channel);
2192 data->channel = NULL;
2195 g_free(data->incoming_reply);
2196 data->incoming_reply = NULL;
2199 static void destroy_server(struct server_data *server)
2201 debug("index %d server %s sock %d", server->index, server->server,
2203 g_io_channel_unix_get_fd(server->channel): -1);
2205 server_list = g_slist_remove(server_list, server);
2206 server_destroy_socket(server);
2208 if (server->protocol == IPPROTO_UDP && server->enabled)
2209 debug("Removing DNS server %s", server->server);
2211 g_free(server->server);
2212 g_list_free_full(server->domains, g_free);
2213 g_free(server->server_addr);
2216 * We do not remove cache right away but delay it few seconds.
2217 * The idea is that when IPv6 DNS server is added via RDNSS, it has a
2218 * lifetime. When the lifetime expires we decrease the refcount so it
2219 * is possible that the cache is then removed. Because a new DNS server
2220 * is usually created almost immediately we would then loose the cache
2221 * without any good reason. The small delay allows the new RDNSS to
2222 * create a new DNS server instance and the refcount does not go to 0.
2224 if (cache && !cache_timer)
2225 cache_timer = g_timeout_add_seconds(3, try_remove_cache, NULL);
2230 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
2233 unsigned char buf[4096];
2235 struct server_data *data = user_data;
2237 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2238 connman_error("Error with UDP server %s", data->server);
2239 server_destroy_socket(data);
2243 sk = g_io_channel_unix_get_fd(channel);
2245 len = recv(sk, buf, sizeof(buf), 0);
2248 forward_dns_reply(buf, len, IPPROTO_UDP, data);
2253 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
2257 struct server_data *server = user_data;
2259 sk = g_io_channel_unix_get_fd(channel);
2263 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2266 debug("TCP server channel closed, sk %d", sk);
2269 * Discard any partial response which is buffered; better
2270 * to get a proper response from a working server.
2272 g_free(server->incoming_reply);
2273 server->incoming_reply = NULL;
2275 list = request_list;
2277 struct request_data *req = list->data;
2278 struct domain_hdr *hdr;
2281 if (req->protocol == IPPROTO_UDP)
2288 * If we're not waiting for any further response
2289 * from another name server, then we send an error
2290 * response to the client.
2292 if (req->numserv && --(req->numserv))
2295 hdr = (void *) (req->request + 2);
2296 hdr->id = req->srcid;
2297 send_response(req->client_sk, req->request,
2298 req->request_len, NULL, 0, IPPROTO_TCP);
2300 request_list = g_slist_remove(request_list, req);
2303 destroy_server(server);
2308 if ((condition & G_IO_OUT) && !server->connected) {
2311 bool no_request_sent = true;
2312 struct server_data *udp_server;
2314 udp_server = find_server(server->index, server->server,
2317 for (domains = udp_server->domains; domains;
2318 domains = domains->next) {
2319 char *dom = domains->data;
2321 debug("Adding domain %s to %s",
2322 dom, server->server);
2324 server->domains = g_list_append(server->domains,
2329 server->connected = true;
2330 server_list = g_slist_append(server_list, server);
2332 if (server->timeout > 0) {
2333 g_source_remove(server->timeout);
2334 server->timeout = 0;
2337 for (list = request_list; list; ) {
2338 struct request_data *req = list->data;
2341 if (req->protocol == IPPROTO_UDP) {
2346 debug("Sending req %s over TCP", (char *)req->name);
2348 status = ns_resolv(server, req,
2349 req->request, req->name);
2352 * A cached result was sent,
2353 * so the request can be released
2356 request_list = g_slist_remove(request_list, req);
2357 destroy_request_data(req);
2366 no_request_sent = false;
2368 if (req->timeout > 0)
2369 g_source_remove(req->timeout);
2371 req->timeout = g_timeout_add_seconds(30,
2372 request_timeout, req);
2376 if (no_request_sent) {
2377 destroy_server(server);
2381 } else if (condition & G_IO_IN) {
2382 struct partial_reply *reply = server->incoming_reply;
2386 unsigned char reply_len_buf[2];
2389 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
2392 } else if (bytes_recv < 0) {
2393 if (errno == EAGAIN || errno == EWOULDBLOCK)
2396 connman_error("DNS proxy error %s",
2399 } else if (bytes_recv < 2)
2402 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
2405 debug("TCP reply %d bytes from %d", reply_len, sk);
2407 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
2411 reply->len = reply_len;
2412 reply->received = 0;
2414 server->incoming_reply = reply;
2417 while (reply->received < reply->len) {
2418 bytes_recv = recv(sk, reply->buf + reply->received,
2419 reply->len - reply->received, 0);
2421 connman_error("DNS proxy TCP disconnect");
2423 } else if (bytes_recv < 0) {
2424 if (errno == EAGAIN || errno == EWOULDBLOCK)
2427 connman_error("DNS proxy error %s",
2431 reply->received += bytes_recv;
2434 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
2438 server->incoming_reply = NULL;
2440 destroy_server(server);
2448 static gboolean tcp_idle_timeout(gpointer user_data)
2450 struct server_data *server = user_data;
2457 destroy_server(server);
2462 static int server_create_socket(struct server_data *data)
2467 debug("index %d server %s proto %d", data->index,
2468 data->server, data->protocol);
2470 sk = socket(data->server_addr->sa_family,
2471 data->protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM,
2475 connman_error("Failed to create server %s socket",
2477 server_destroy_socket(data);
2483 interface = connman_inet_ifname(data->index);
2485 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2487 strlen(interface) + 1) < 0) {
2489 connman_error("Failed to bind server %s "
2491 data->server, interface);
2493 server_destroy_socket(data);
2500 data->channel = g_io_channel_unix_new(sk);
2501 if (!data->channel) {
2502 connman_error("Failed to create server %s channel",
2505 server_destroy_socket(data);
2509 g_io_channel_set_close_on_unref(data->channel, TRUE);
2511 if (data->protocol == IPPROTO_TCP) {
2512 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
2513 data->watch = g_io_add_watch(data->channel,
2514 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
2515 tcp_server_event, data);
2516 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
2519 data->watch = g_io_add_watch(data->channel,
2520 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2521 udp_server_event, data);
2523 if (connect(sk, data->server_addr, data->server_addr_len) < 0) {
2526 if ((data->protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
2527 data->protocol == IPPROTO_UDP) {
2529 connman_error("Failed to connect to server %s",
2531 server_destroy_socket(data);
2541 static void enable_fallback(bool enable)
2545 for (list = server_list; list; list = list->next) {
2546 struct server_data *data = list->data;
2548 if (data->index != -1)
2552 DBG("Enabling fallback DNS server %s", data->server);
2554 DBG("Disabling fallback DNS server %s", data->server);
2556 data->enabled = enable;
2560 static struct server_data *create_server(int index,
2561 const char *domain, const char *server,
2564 struct server_data *data;
2565 struct addrinfo hints, *rp;
2568 DBG("index %d server %s", index, server);
2570 data = g_try_new0(struct server_data, 1);
2572 connman_error("Failed to allocate server %s data", server);
2576 data->index = index;
2578 data->domains = g_list_append(data->domains, g_strdup(domain));
2579 data->server = g_strdup(server);
2580 data->protocol = protocol;
2582 memset(&hints, 0, sizeof(hints));
2586 hints.ai_socktype = SOCK_DGRAM;
2590 hints.ai_socktype = SOCK_STREAM;
2594 destroy_server(data);
2597 hints.ai_family = AF_UNSPEC;
2598 hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
2600 ret = getaddrinfo(data->server, "53", &hints, &rp);
2602 connman_error("Failed to parse server %s address: %s\n",
2603 data->server, gai_strerror(ret));
2604 destroy_server(data);
2608 /* Do not blindly copy this code elsewhere; it doesn't loop over the
2609 results using ->ai_next as it should. That's OK in *this* case
2610 because it was a numeric lookup; we *know* there's only one. */
2612 data->server_addr_len = rp->ai_addrlen;
2614 switch (rp->ai_family) {
2616 data->server_addr = (struct sockaddr *)
2617 g_try_new0(struct sockaddr_in, 1);
2620 data->server_addr = (struct sockaddr *)
2621 g_try_new0(struct sockaddr_in6, 1);
2624 connman_error("Wrong address family %d", rp->ai_family);
2627 if (!data->server_addr) {
2629 destroy_server(data);
2632 memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
2635 if (server_create_socket(data) != 0) {
2636 destroy_server(data);
2640 if (protocol == IPPROTO_UDP) {
2641 if (__connman_service_index_is_default(data->index) ||
2642 __connman_service_index_is_split_routing(
2644 data->enabled = true;
2645 DBG("Adding DNS server %s", data->server);
2647 enable_fallback(false);
2650 server_list = g_slist_append(server_list, data);
2656 static bool resolv(struct request_data *req,
2657 gpointer request, gpointer name)
2661 for (list = server_list; list; list = list->next) {
2662 struct server_data *data = list->data;
2664 if (data->protocol == IPPROTO_TCP) {
2665 DBG("server %s ignored proto TCP", data->server);
2669 debug("server %s enabled %d", data->server, data->enabled);
2674 if (!data->channel && data->protocol == IPPROTO_UDP) {
2675 if (server_create_socket(data) < 0) {
2676 DBG("socket creation failed while resolving");
2681 if (ns_resolv(data, req, request, name) > 0)
2688 static void update_domain(int index, const char *domain, bool append)
2692 DBG("index %d domain %s", index, domain);
2697 for (list = server_list; list; list = list->next) {
2698 struct server_data *data = list->data;
2701 bool dom_found = false;
2703 if (data->index < 0)
2706 if (data->index != index)
2709 for (dom_list = data->domains; dom_list;
2710 dom_list = dom_list->next) {
2711 dom = dom_list->data;
2713 if (g_str_equal(dom, domain)) {
2719 if (!dom_found && append) {
2721 g_list_append(data->domains, g_strdup(domain));
2722 } else if (dom_found && !append) {
2724 g_list_remove(data->domains, dom);
2730 static void append_domain(int index, const char *domain)
2732 update_domain(index, domain, true);
2735 static void remove_domain(int index, const char *domain)
2737 update_domain(index, domain, false);
2740 static void flush_requests(struct server_data *server)
2744 list = request_list;
2746 struct request_data *req = list->data;
2750 if (ns_resolv(server, req, req->request, req->name)) {
2752 * A cached result was sent,
2753 * so the request can be released
2756 g_slist_remove(request_list, req);
2757 destroy_request_data(req);
2761 if (req->timeout > 0)
2762 g_source_remove(req->timeout);
2764 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2768 int __connman_dnsproxy_append(int index, const char *domain,
2771 struct server_data *data;
2773 DBG("index %d server %s", index, server);
2775 if (!server && !domain)
2779 append_domain(index, domain);
2784 if (g_str_equal(server, "127.0.0.1"))
2787 if (g_str_equal(server, "::1"))
2790 data = find_server(index, server, IPPROTO_UDP);
2792 append_domain(index, domain);
2796 data = create_server(index, domain, server, IPPROTO_UDP);
2800 flush_requests(data);
2805 static void remove_server(int index, const char *domain,
2806 const char *server, int protocol)
2808 struct server_data *data;
2811 data = find_server(index, server, protocol);
2815 destroy_server(data);
2817 for (list = server_list; list; list = list->next) {
2818 struct server_data *data = list->data;
2820 if (data->index != -1 && data->enabled == true)
2824 enable_fallback(true);
2827 int __connman_dnsproxy_remove(int index, const char *domain,
2830 DBG("index %d server %s", index, server);
2832 if (!server && !domain)
2836 remove_domain(index, domain);
2841 if (g_str_equal(server, "127.0.0.1"))
2844 if (g_str_equal(server, "::1"))
2847 remove_server(index, domain, server, IPPROTO_UDP);
2848 remove_server(index, domain, server, IPPROTO_TCP);
2853 static void dnsproxy_offline_mode(bool enabled)
2857 DBG("enabled %d", enabled);
2859 for (list = server_list; list; list = list->next) {
2860 struct server_data *data = list->data;
2863 DBG("Enabling DNS server %s", data->server);
2864 data->enabled = true;
2868 DBG("Disabling DNS server %s", data->server);
2869 data->enabled = false;
2875 static void dnsproxy_default_changed(struct connman_service *service)
2877 bool server_enabled = false;
2881 DBG("service %p", service);
2883 /* DNS has changed, invalidate the cache */
2887 /* When no services are active, then disable DNS proxying */
2888 dnsproxy_offline_mode(true);
2892 index = __connman_service_get_index(service);
2896 for (list = server_list; list; list = list->next) {
2897 struct server_data *data = list->data;
2899 if (data->index == index) {
2900 DBG("Enabling DNS server %s", data->server);
2901 data->enabled = true;
2902 server_enabled = true;
2904 DBG("Disabling DNS server %s", data->server);
2905 data->enabled = false;
2909 if (!server_enabled)
2910 enable_fallback(true);
2915 static void dnsproxy_service_state_changed(struct connman_service *service,
2916 enum connman_service_state state)
2922 case CONNMAN_SERVICE_STATE_DISCONNECT:
2923 case CONNMAN_SERVICE_STATE_IDLE:
2925 case CONNMAN_SERVICE_STATE_ASSOCIATION:
2926 case CONNMAN_SERVICE_STATE_CONFIGURATION:
2927 case CONNMAN_SERVICE_STATE_FAILURE:
2928 case CONNMAN_SERVICE_STATE_ONLINE:
2929 case CONNMAN_SERVICE_STATE_READY:
2930 case CONNMAN_SERVICE_STATE_UNKNOWN:
2934 index = __connman_service_get_index(service);
2938 struct server_data *data = list->data;
2940 /* Get next before the list is changed by destroy_server() */
2943 if (data->index == index) {
2944 DBG("removing server data of index %d", index);
2945 destroy_server(data);
2950 static const struct connman_notifier dnsproxy_notifier = {
2952 .default_changed = dnsproxy_default_changed,
2953 .offline_mode = dnsproxy_offline_mode,
2954 .service_state_changed = dnsproxy_service_state_changed,
2957 static const unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2959 static int parse_request(unsigned char *buf, size_t len,
2960 char *name, unsigned int size)
2962 struct domain_hdr *hdr = (void *) buf;
2963 uint16_t qdcount = ntohs(hdr->qdcount);
2964 uint16_t ancount = ntohs(hdr->ancount);
2965 uint16_t nscount = ntohs(hdr->nscount);
2966 uint16_t arcount = ntohs(hdr->arcount);
2968 unsigned int remain, used = 0;
2970 if (len < sizeof(*hdr) + sizeof(struct qtype_qclass) ||
2971 hdr->qr || qdcount != 1 || ancount || nscount) {
2972 DBG("Dropped DNS request qr %d with len %zd qdcount %d "
2973 "ancount %d nscount %d", hdr->qr, len, qdcount, ancount,
2982 debug("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2983 hdr->id, hdr->qr, hdr->opcode,
2988 ptr = buf + sizeof(struct domain_hdr);
2989 remain = len - sizeof(struct domain_hdr);
2991 while (remain > 0) {
2992 uint8_t label_len = *ptr;
2994 if (label_len == 0x00) {
2996 struct qtype_qclass *q =
2997 (struct qtype_qclass *)(ptr + 1);
2999 if (remain < sizeof(*q)) {
3000 DBG("Dropped malformed DNS query");
3004 class = ntohs(q->qclass);
3005 if (class != 1 && class != 255) {
3006 DBG("Dropped non-IN DNS class %d", class);
3010 ptr += sizeof(*q) + 1;
3011 remain -= (sizeof(*q) + 1);
3015 if (used + label_len + 1 > size)
3018 strncat(name, (char *) (ptr + 1), label_len);
3021 used += label_len + 1;
3023 ptr += label_len + 1;
3024 remain -= label_len + 1;
3027 if (arcount && remain >= sizeof(struct domain_rr) + 1 && !ptr[0] &&
3028 ptr[1] == opt_edns0_type[0] && ptr[2] == opt_edns0_type[1]) {
3029 struct domain_rr *edns0 = (struct domain_rr *)(ptr + 1);
3031 DBG("EDNS0 buffer size %u", ntohs(edns0->class));
3032 } else if (!arcount && remain) {
3033 DBG("DNS request with %d garbage bytes", remain);
3036 debug("query %s", name);
3041 static void client_reset(struct tcp_partial_client_data *client)
3046 if (client->channel) {
3047 debug("client %d closing",
3048 g_io_channel_unix_get_fd(client->channel));
3050 g_io_channel_unref(client->channel);
3051 client->channel = NULL;
3054 if (client->watch > 0) {
3055 g_source_remove(client->watch);
3059 if (client->timeout > 0) {
3060 g_source_remove(client->timeout);
3061 client->timeout = 0;
3064 g_free(client->buf);
3067 client->buf_end = 0;
3070 static unsigned int get_msg_len(unsigned char *buf)
3072 return buf[0]<<8 | buf[1];
3075 static bool read_tcp_data(struct tcp_partial_client_data *client,
3076 void *client_addr, socklen_t client_addr_len,
3079 char query[TCP_MAX_BUF_LEN];
3080 struct request_data *req;
3082 unsigned int msg_len;
3084 bool waiting_for_connect = false;
3086 struct cache_entry *entry;
3088 client_sk = g_io_channel_unix_get_fd(client->channel);
3090 if (read_len == 0) {
3091 debug("client %d closed, pending %d bytes",
3092 client_sk, client->buf_end);
3093 g_hash_table_remove(partial_tcp_req_table,
3094 GINT_TO_POINTER(client_sk));
3098 debug("client %d received %d bytes", client_sk, read_len);
3100 client->buf_end += read_len;
3102 if (client->buf_end < 2)
3105 msg_len = get_msg_len(client->buf);
3106 if (msg_len > TCP_MAX_BUF_LEN) {
3107 debug("client %d sent too much data %d", client_sk, msg_len);
3108 g_hash_table_remove(partial_tcp_req_table,
3109 GINT_TO_POINTER(client_sk));
3114 debug("client %d msg len %d end %d past end %d", client_sk, msg_len,
3115 client->buf_end, client->buf_end - (msg_len + 2));
3117 if (client->buf_end < (msg_len + 2)) {
3118 debug("client %d still missing %d bytes",
3120 msg_len + 2 - client->buf_end);
3124 debug("client %d all data %d received", client_sk, msg_len);
3126 err = parse_request(client->buf + 2, msg_len,
3127 query, sizeof(query));
3128 if (err < 0 || (g_slist_length(server_list) == 0)) {
3129 send_response(client_sk, client->buf, msg_len + 2,
3130 NULL, 0, IPPROTO_TCP);
3134 req = g_try_new0(struct request_data, 1);
3138 memcpy(&req->sa, client_addr, client_addr_len);
3139 req->sa_len = client_addr_len;
3140 req->client_sk = client_sk;
3141 req->protocol = IPPROTO_TCP;
3142 req->family = client->family;
3144 req->srcid = client->buf[2] | (client->buf[3] << 8);
3145 req->dstid = get_id();
3146 req->altid = get_id();
3147 req->request_len = msg_len + 2;
3149 client->buf[2] = req->dstid & 0xff;
3150 client->buf[3] = req->dstid >> 8;
3153 req->ifdata = client->ifdata;
3154 req->append_domain = false;
3157 * Check if the answer is found in the cache before
3158 * creating sockets to the server.
3160 entry = cache_check(client->buf, &qtype, IPPROTO_TCP);
3163 struct cache_data *data;
3165 debug("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
3172 ttl_left = data->valid_until - time(NULL);
3175 send_cached_response(client_sk, data->data,
3176 data->data_len, NULL, 0, IPPROTO_TCP,
3177 req->srcid, data->answers, ttl_left);
3182 debug("data missing, ignoring cache for this query");
3185 for (list = server_list; list; list = list->next) {
3186 struct server_data *data = list->data;
3188 if (data->protocol != IPPROTO_UDP || !data->enabled)
3191 if (!create_server(data->index, NULL, data->server,
3195 waiting_for_connect = true;
3198 if (!waiting_for_connect) {
3199 /* No server is waiting for connect */
3200 send_response(client_sk, client->buf,
3201 req->request_len, NULL, 0, IPPROTO_TCP);
3207 * The server is not connected yet.
3208 * Copy the relevant buffers.
3209 * The request will actually be sent once we're
3210 * properly connected over TCP to the nameserver.
3212 req->request = g_try_malloc0(req->request_len);
3213 if (!req->request) {
3214 send_response(client_sk, client->buf,
3215 req->request_len, NULL, 0, IPPROTO_TCP);
3219 memcpy(req->request, client->buf, req->request_len);
3221 req->name = g_try_malloc0(sizeof(query));
3223 send_response(client_sk, client->buf,
3224 req->request_len, NULL, 0, IPPROTO_TCP);
3225 g_free(req->request);
3229 memcpy(req->name, query, sizeof(query));
3231 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
3233 request_list = g_slist_append(request_list, req);
3236 if (client->buf_end > (msg_len + 2)) {
3237 debug("client %d buf %p -> %p end %d len %d new %d",
3239 client->buf + msg_len + 2,
3240 client->buf, client->buf_end,
3241 TCP_MAX_BUF_LEN - client->buf_end,
3242 client->buf_end - (msg_len + 2));
3243 memmove(client->buf, client->buf + msg_len + 2,
3244 TCP_MAX_BUF_LEN - client->buf_end);
3245 client->buf_end = client->buf_end - (msg_len + 2);
3248 * If we have a full message waiting, just read it
3251 msg_len = get_msg_len(client->buf);
3252 if ((msg_len + 2) == client->buf_end) {
3253 debug("client %d reading another %d bytes", client_sk,
3258 debug("client %d clearing reading buffer", client_sk);
3260 client->buf_end = 0;
3261 memset(client->buf, 0, TCP_MAX_BUF_LEN);
3264 * We received all the packets from client so we must also
3265 * remove the timeout handler here otherwise we might get
3266 * timeout while waiting the results from server.
3268 g_source_remove(client->timeout);
3269 client->timeout = 0;
3275 static gboolean tcp_client_event(GIOChannel *channel, GIOCondition condition,
3278 struct tcp_partial_client_data *client = user_data;
3279 struct sockaddr_in6 client_addr6;
3280 socklen_t client_addr6_len = sizeof(client_addr6);
3281 struct sockaddr_in client_addr4;
3282 socklen_t client_addr4_len = sizeof(client_addr4);
3284 socklen_t *client_addr_len;
3287 client_sk = g_io_channel_unix_get_fd(channel);
3289 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3290 g_hash_table_remove(partial_tcp_req_table,
3291 GINT_TO_POINTER(client_sk));
3293 connman_error("Error with TCP client %d channel", client_sk);
3297 switch (client->family) {
3299 client_addr = &client_addr4;
3300 client_addr_len = &client_addr4_len;
3303 client_addr = &client_addr6;
3304 client_addr_len = &client_addr6_len;
3307 g_hash_table_remove(partial_tcp_req_table,
3308 GINT_TO_POINTER(client_sk));
3309 connman_error("client %p corrupted", client);
3313 len = recvfrom(client_sk, client->buf + client->buf_end,
3314 TCP_MAX_BUF_LEN - client->buf_end, 0,
3315 client_addr, client_addr_len);
3317 if (errno == EAGAIN || errno == EWOULDBLOCK)
3320 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3322 g_hash_table_remove(partial_tcp_req_table,
3323 GINT_TO_POINTER(client_sk));
3327 return read_tcp_data(client, client_addr, *client_addr_len, len);
3330 static gboolean client_timeout(gpointer user_data)
3332 struct tcp_partial_client_data *client = user_data;
3335 sock = g_io_channel_unix_get_fd(client->channel);
3337 debug("client %d timeout pending %d bytes", sock, client->buf_end);
3339 g_hash_table_remove(partial_tcp_req_table, GINT_TO_POINTER(sock));
3344 static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
3345 struct listener_data *ifdata, int family,
3346 guint *listener_watch)
3348 int sk, client_sk, len;
3349 unsigned int msg_len;
3350 struct tcp_partial_client_data *client;
3351 struct sockaddr_in6 client_addr6;
3352 socklen_t client_addr6_len = sizeof(client_addr6);
3353 struct sockaddr_in client_addr4;
3354 socklen_t client_addr4_len = sizeof(client_addr4);
3356 socklen_t *client_addr_len;
3360 debug("condition 0x%02x channel %p ifdata %p family %d",
3361 condition, channel, ifdata, family);
3363 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3364 if (*listener_watch > 0)
3365 g_source_remove(*listener_watch);
3366 *listener_watch = 0;
3368 connman_error("Error with TCP listener channel");
3373 sk = g_io_channel_unix_get_fd(channel);
3375 if (family == AF_INET) {
3376 client_addr = &client_addr4;
3377 client_addr_len = &client_addr4_len;
3379 client_addr = &client_addr6;
3380 client_addr_len = &client_addr6_len;
3383 tv.tv_sec = tv.tv_usec = 0;
3385 FD_SET(sk, &readfds);
3387 select(sk + 1, &readfds, NULL, NULL, &tv);
3388 if (FD_ISSET(sk, &readfds)) {
3389 client_sk = accept(sk, client_addr, client_addr_len);
3390 debug("client %d accepted", client_sk);
3392 debug("No data to read from master %d, waiting.", sk);
3396 if (client_sk < 0) {
3397 connman_error("Accept failure on TCP listener");
3398 *listener_watch = 0;
3402 fcntl(client_sk, F_SETFL, O_NONBLOCK);
3404 client = g_hash_table_lookup(partial_tcp_req_table,
3405 GINT_TO_POINTER(client_sk));
3407 client = g_try_new0(struct tcp_partial_client_data, 1);
3413 g_hash_table_insert(partial_tcp_req_table,
3414 GINT_TO_POINTER(client_sk),
3417 client->channel = g_io_channel_unix_new(client_sk);
3418 g_io_channel_set_close_on_unref(client->channel, TRUE);
3420 client->watch = g_io_add_watch(client->channel,
3421 G_IO_IN, tcp_client_event,
3424 client->ifdata = ifdata;
3426 debug("client %d created %p", client_sk, client);
3428 debug("client %d already exists %p", client_sk, client);
3432 client->buf = g_try_malloc(TCP_MAX_BUF_LEN);
3436 memset(client->buf, 0, TCP_MAX_BUF_LEN);
3437 client->buf_end = 0;
3438 client->family = family;
3440 if (client->timeout == 0)
3441 client->timeout = g_timeout_add_seconds(2, client_timeout,
3445 * Check how much data there is. If all is there, then we can
3446 * proceed normally, otherwise read the bits until everything
3447 * is received or timeout occurs.
3449 len = recv(client_sk, client->buf, TCP_MAX_BUF_LEN, 0);
3451 if (errno == EAGAIN || errno == EWOULDBLOCK) {
3452 debug("client %d no data to read, waiting", client_sk);
3456 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3458 g_hash_table_remove(partial_tcp_req_table,
3459 GINT_TO_POINTER(client_sk));
3464 debug("client %d not enough data to read, waiting", client_sk);
3465 client->buf_end += len;
3469 msg_len = get_msg_len(client->buf);
3470 if (msg_len > TCP_MAX_BUF_LEN) {
3471 debug("client %d invalid message length %u ignoring packet",
3472 client_sk, msg_len);
3473 g_hash_table_remove(partial_tcp_req_table,
3474 GINT_TO_POINTER(client_sk));
3479 * The packet length bytes do not contain the total message length,
3480 * that is the reason to -2 below.
3482 if (msg_len != (unsigned int)(len - 2)) {
3483 debug("client %d sent %d bytes but expecting %u pending %d",
3484 client_sk, len, msg_len + 2, msg_len + 2 - len);
3486 client->buf_end += len;
3490 return read_tcp_data(client, client_addr, *client_addr_len, len);
3493 static gboolean tcp4_listener_event(GIOChannel *channel, GIOCondition condition,
3496 struct listener_data *ifdata = user_data;
3498 return tcp_listener_event(channel, condition, ifdata, AF_INET,
3499 &ifdata->tcp4_listener_watch);
3502 static gboolean tcp6_listener_event(GIOChannel *channel, GIOCondition condition,
3505 struct listener_data *ifdata = user_data;
3507 return tcp_listener_event(channel, condition, user_data, AF_INET6,
3508 &ifdata->tcp6_listener_watch);
3511 static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
3512 struct listener_data *ifdata, int family,
3513 guint *listener_watch)
3515 unsigned char buf[768];
3517 struct request_data *req;
3518 struct sockaddr_in6 client_addr6;
3519 socklen_t client_addr6_len = sizeof(client_addr6);
3520 struct sockaddr_in client_addr4;
3521 socklen_t client_addr4_len = sizeof(client_addr4);
3523 socklen_t *client_addr_len;
3526 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3527 connman_error("Error with UDP listener channel");
3528 *listener_watch = 0;
3532 sk = g_io_channel_unix_get_fd(channel);
3534 if (family == AF_INET) {
3535 client_addr = &client_addr4;
3536 client_addr_len = &client_addr4_len;
3538 client_addr = &client_addr6;
3539 client_addr_len = &client_addr6_len;
3542 memset(client_addr, 0, *client_addr_len);
3543 len = recvfrom(sk, buf, sizeof(buf), 0, client_addr, client_addr_len);
3547 debug("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
3549 err = parse_request(buf, len, query, sizeof(query));
3550 if (err < 0 || (g_slist_length(server_list) == 0)) {
3551 send_response(sk, buf, len, client_addr,
3552 *client_addr_len, IPPROTO_UDP);
3556 req = g_try_new0(struct request_data, 1);
3560 memcpy(&req->sa, client_addr, *client_addr_len);
3561 req->sa_len = *client_addr_len;
3563 req->protocol = IPPROTO_UDP;
3564 req->family = family;
3566 req->srcid = buf[0] | (buf[1] << 8);
3567 req->dstid = get_id();
3568 req->altid = get_id();
3569 req->request_len = len;
3571 buf[0] = req->dstid & 0xff;
3572 buf[1] = req->dstid >> 8;
3575 req->ifdata = ifdata;
3576 req->append_domain = false;
3578 if (resolv(req, buf, query)) {
3579 /* a cached result was sent, so the request can be released */
3584 req->name = g_strdup(query);
3585 req->request = g_malloc(len);
3586 memcpy(req->request, buf, len);
3587 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
3588 request_list = g_slist_append(request_list, req);
3593 static gboolean udp4_listener_event(GIOChannel *channel, GIOCondition condition,
3596 struct listener_data *ifdata = user_data;
3598 return udp_listener_event(channel, condition, ifdata, AF_INET,
3599 &ifdata->udp4_listener_watch);
3602 static gboolean udp6_listener_event(GIOChannel *channel, GIOCondition condition,
3605 struct listener_data *ifdata = user_data;
3607 return udp_listener_event(channel, condition, user_data, AF_INET6,
3608 &ifdata->udp6_listener_watch);
3611 static GIOChannel *get_listener(int family, int protocol, int index)
3613 GIOChannel *channel;
3617 struct sockaddr_in6 sin6;
3618 struct sockaddr_in sin;
3624 debug("family %d protocol %d index %d", family, protocol, index);
3629 type = SOCK_DGRAM | SOCK_CLOEXEC;
3634 type = SOCK_STREAM | SOCK_CLOEXEC;
3641 sk = socket(family, type, protocol);
3642 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
3643 connman_error("No IPv6 support");
3648 connman_error("Failed to create %s listener socket", proto);
3652 interface = connman_inet_ifname(index);
3653 if (!interface || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
3655 strlen(interface) + 1) < 0) {
3656 connman_error("Failed to bind %s listener interface "
3658 proto, family == AF_INET ? "IPv4" : "IPv6",
3659 -errno, strerror(errno));
3666 if (family == AF_INET6) {
3667 memset(&s.sin6, 0, sizeof(s.sin6));
3668 s.sin6.sin6_family = AF_INET6;
3669 s.sin6.sin6_port = htons(53);
3670 slen = sizeof(s.sin6);
3672 if (__connman_inet_get_interface_address(index,
3674 &s.sin6.sin6_addr) < 0) {
3675 /* So we could not find suitable IPv6 address for
3676 * the interface. This could happen if we have
3677 * disabled IPv6 for the interface.
3683 } else if (family == AF_INET) {
3684 memset(&s.sin, 0, sizeof(s.sin));
3685 s.sin.sin_family = AF_INET;
3686 s.sin.sin_port = htons(53);
3687 slen = sizeof(s.sin);
3689 if (__connman_inet_get_interface_address(index,
3691 &s.sin.sin_addr) < 0) {
3700 if (bind(sk, &s.sa, slen) < 0) {
3701 connman_error("Failed to bind %s listener socket", proto);
3706 if (protocol == IPPROTO_TCP) {
3708 if (listen(sk, 10) < 0) {
3709 connman_error("Failed to listen on TCP socket %d/%s",
3710 -errno, strerror(errno));
3715 fcntl(sk, F_SETFL, O_NONBLOCK);
3718 channel = g_io_channel_unix_new(sk);
3720 connman_error("Failed to create %s listener channel", proto);
3725 g_io_channel_set_close_on_unref(channel, TRUE);
3730 #define UDP_IPv4_FAILED 0x01
3731 #define TCP_IPv4_FAILED 0x02
3732 #define UDP_IPv6_FAILED 0x04
3733 #define TCP_IPv6_FAILED 0x08
3734 #define UDP_FAILED (UDP_IPv4_FAILED | UDP_IPv6_FAILED)
3735 #define TCP_FAILED (TCP_IPv4_FAILED | TCP_IPv6_FAILED)
3736 #define IPv6_FAILED (UDP_IPv6_FAILED | TCP_IPv6_FAILED)
3737 #define IPv4_FAILED (UDP_IPv4_FAILED | TCP_IPv4_FAILED)
3739 static int create_dns_listener(int protocol, struct listener_data *ifdata)
3743 if (protocol == IPPROTO_TCP) {
3744 ifdata->tcp4_listener_channel = get_listener(AF_INET, protocol,
3746 if (ifdata->tcp4_listener_channel)
3747 ifdata->tcp4_listener_watch =
3748 g_io_add_watch(ifdata->tcp4_listener_channel,
3749 G_IO_IN, tcp4_listener_event,
3752 ret |= TCP_IPv4_FAILED;
3754 ifdata->tcp6_listener_channel = get_listener(AF_INET6, protocol,
3756 if (ifdata->tcp6_listener_channel)
3757 ifdata->tcp6_listener_watch =
3758 g_io_add_watch(ifdata->tcp6_listener_channel,
3759 G_IO_IN, tcp6_listener_event,
3762 ret |= TCP_IPv6_FAILED;
3764 ifdata->udp4_listener_channel = get_listener(AF_INET, protocol,
3766 if (ifdata->udp4_listener_channel)
3767 ifdata->udp4_listener_watch =
3768 g_io_add_watch(ifdata->udp4_listener_channel,
3769 G_IO_IN, udp4_listener_event,
3772 ret |= UDP_IPv4_FAILED;
3774 ifdata->udp6_listener_channel = get_listener(AF_INET6, protocol,
3776 if (ifdata->udp6_listener_channel)
3777 ifdata->udp6_listener_watch =
3778 g_io_add_watch(ifdata->udp6_listener_channel,
3779 G_IO_IN, udp6_listener_event,
3782 ret |= UDP_IPv6_FAILED;
3788 static void destroy_udp_listener(struct listener_data *ifdata)
3790 DBG("index %d", ifdata->index);
3792 if (ifdata->udp4_listener_watch > 0)
3793 g_source_remove(ifdata->udp4_listener_watch);
3795 if (ifdata->udp6_listener_watch > 0)
3796 g_source_remove(ifdata->udp6_listener_watch);
3798 if (ifdata->udp4_listener_channel)
3799 g_io_channel_unref(ifdata->udp4_listener_channel);
3800 if (ifdata->udp6_listener_channel)
3801 g_io_channel_unref(ifdata->udp6_listener_channel);
3804 static void destroy_tcp_listener(struct listener_data *ifdata)
3806 DBG("index %d", ifdata->index);
3808 if (ifdata->tcp4_listener_watch > 0)
3809 g_source_remove(ifdata->tcp4_listener_watch);
3810 if (ifdata->tcp6_listener_watch > 0)
3811 g_source_remove(ifdata->tcp6_listener_watch);
3813 if (ifdata->tcp4_listener_channel)
3814 g_io_channel_unref(ifdata->tcp4_listener_channel);
3815 if (ifdata->tcp6_listener_channel)
3816 g_io_channel_unref(ifdata->tcp6_listener_channel);
3819 static int create_listener(struct listener_data *ifdata)
3823 err = create_dns_listener(IPPROTO_UDP, ifdata);
3824 if ((err & UDP_FAILED) == UDP_FAILED)
3827 err |= create_dns_listener(IPPROTO_TCP, ifdata);
3828 if ((err & TCP_FAILED) == TCP_FAILED) {
3829 destroy_udp_listener(ifdata);
3833 index = connman_inet_ifindex("lo");
3834 if (ifdata->index == index) {
3835 if ((err & IPv6_FAILED) != IPv6_FAILED)
3836 __connman_resolvfile_append(index, NULL, "::1");
3838 if ((err & IPv4_FAILED) != IPv4_FAILED)
3839 __connman_resolvfile_append(index, NULL, "127.0.0.1");
3845 static void destroy_listener(struct listener_data *ifdata)
3850 index = connman_inet_ifindex("lo");
3851 if (ifdata->index == index) {
3852 __connman_resolvfile_remove(index, NULL, "127.0.0.1");
3853 __connman_resolvfile_remove(index, NULL, "::1");
3856 for (list = request_list; list; list = list->next) {
3857 struct request_data *req = list->data;
3859 debug("Dropping request (id 0x%04x -> 0x%04x)",
3860 req->srcid, req->dstid);
3861 destroy_request_data(req);
3865 g_slist_free(request_list);
3866 request_list = NULL;
3868 destroy_tcp_listener(ifdata);
3869 destroy_udp_listener(ifdata);
3872 int __connman_dnsproxy_add_listener(int index)
3874 struct listener_data *ifdata;
3877 DBG("index %d", index);
3882 if (!listener_table)
3885 if (g_hash_table_lookup(listener_table, GINT_TO_POINTER(index)))
3888 ifdata = g_try_new0(struct listener_data, 1);
3892 ifdata->index = index;
3893 ifdata->udp4_listener_channel = NULL;
3894 ifdata->udp4_listener_watch = 0;
3895 ifdata->tcp4_listener_channel = NULL;
3896 ifdata->tcp4_listener_watch = 0;
3897 ifdata->udp6_listener_channel = NULL;
3898 ifdata->udp6_listener_watch = 0;
3899 ifdata->tcp6_listener_channel = NULL;
3900 ifdata->tcp6_listener_watch = 0;
3902 err = create_listener(ifdata);
3904 connman_error("Couldn't create listener for index %d err %d",
3909 g_hash_table_insert(listener_table, GINT_TO_POINTER(ifdata->index),
3914 void __connman_dnsproxy_remove_listener(int index)
3916 struct listener_data *ifdata;
3918 DBG("index %d", index);
3920 if (!listener_table)
3923 ifdata = g_hash_table_lookup(listener_table, GINT_TO_POINTER(index));
3927 destroy_listener(ifdata);
3929 g_hash_table_remove(listener_table, GINT_TO_POINTER(index));
3932 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
3934 int index = GPOINTER_TO_INT(key);
3935 struct listener_data *ifdata = value;
3937 DBG("index %d", index);
3939 destroy_listener(ifdata);
3942 static void free_partial_reqs(gpointer value)
3944 struct tcp_partial_client_data *data = value;
3950 int __connman_dnsproxy_init(void)
3956 listener_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
3959 partial_tcp_req_table = g_hash_table_new_full(g_direct_hash,
3964 index = connman_inet_ifindex("lo");
3965 err = __connman_dnsproxy_add_listener(index);
3969 err = connman_notifier_register(&dnsproxy_notifier);
3976 __connman_dnsproxy_remove_listener(index);
3977 g_hash_table_destroy(listener_table);
3978 g_hash_table_destroy(partial_tcp_req_table);
3983 int __connman_dnsproxy_set_mdns(int index, bool enabled)
3988 void __connman_dnsproxy_cleanup(void)
3993 g_source_remove(cache_timer);
3998 g_hash_table_destroy(cache);
4002 connman_notifier_unregister(&dnsproxy_notifier);
4004 g_hash_table_foreach(listener_table, remove_listener, NULL);
4006 g_hash_table_destroy(listener_table);
4008 g_hash_table_destroy(partial_tcp_req_table);
4011 g_resolv_unref(ipv4_resolve);
4013 g_resolv_unref(ipv6_resolve);