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"
82 struct partial_reply {
92 struct sockaddr *server_addr;
93 socklen_t server_addr_len;
100 struct partial_reply *incoming_reply;
103 struct request_data {
105 struct sockaddr_in6 __sin6; /* Only for the length */
124 struct listener_data *ifdata;
128 struct listener_data {
131 GIOChannel *udp4_listener_channel;
132 GIOChannel *tcp4_listener_channel;
133 guint udp4_listener_watch;
134 guint tcp4_listener_watch;
136 GIOChannel *udp6_listener_channel;
137 GIOChannel *tcp6_listener_channel;
138 guint udp6_listener_watch;
139 guint tcp6_listener_watch;
143 * The TCP client requires some extra handling as we need to
144 * be prepared to receive also partial DNS requests.
146 struct tcp_partial_client_data {
148 struct listener_data *ifdata;
152 unsigned int buf_end;
163 unsigned int data_len;
164 unsigned char *data; /* contains DNS header + body */
171 struct cache_data *ipv4;
172 struct cache_data *ipv6;
175 struct domain_question {
178 } __attribute__ ((packed));
185 } __attribute__ ((packed));
188 * Max length of the DNS TCP packet.
190 #define TCP_MAX_BUF_LEN 4096
193 * We limit how long the cached DNS entry stays in the cache.
194 * By default the TTL (time-to-live) of the DNS response is used
195 * when setting the cache entry life time. The value is in seconds.
197 #define MAX_CACHE_TTL (60 * 30)
199 * Also limit the other end, cache at least for 30 seconds.
201 #define MIN_CACHE_TTL (30)
204 * We limit the cache size to some sane value so that cached data does
205 * not occupy too much memory. Each cached entry occupies on average
206 * about 100 bytes memory (depending on DNS name length).
207 * Example: caching www.connman.net uses 97 bytes memory.
208 * The value is the max amount of cached DNS responses (count).
210 #define MAX_CACHE_SIZE 256
212 static int cache_size;
213 static GHashTable *cache;
214 static int cache_refcount;
215 static GSList *server_list = NULL;
216 static GSList *request_list = NULL;
217 static GHashTable *listener_table = NULL;
218 static time_t next_refresh;
219 static GHashTable *partial_tcp_req_table;
220 static guint cache_timer = 0;
222 static guint16 get_id(void)
226 __connman_util_get_random(&rand);
231 static int protocol_offset(int protocol)
247 * There is a power and efficiency benefit to have entries
248 * in our cache expire at the same time. To this extend,
249 * we round down the cache valid time to common boundaries.
251 static time_t round_down_ttl(time_t end_time, int ttl)
256 /* Less than 5 minutes, round to 10 second boundary */
258 end_time = end_time / 10;
259 end_time = end_time * 10;
260 } else { /* 5 or more minutes, round to 30 seconds */
261 end_time = end_time / 30;
262 end_time = end_time * 30;
267 static struct request_data *find_request(guint16 id)
271 for (list = request_list; list; list = list->next) {
272 struct request_data *req = list->data;
274 if (req->dstid == id || req->altid == id)
281 static struct server_data *find_server(int index,
287 debug("index %d server %s proto %d", index, server, protocol);
289 for (list = server_list; list; list = list->next) {
290 struct server_data *data = list->data;
292 if (index < 0 && data->index < 0 &&
293 g_str_equal(data->server, server) &&
294 data->protocol == protocol)
298 data->index < 0 || !data->server)
301 if (data->index == index &&
302 g_str_equal(data->server, server) &&
303 data->protocol == protocol)
310 /* we can keep using the same resolve's */
311 static GResolv *ipv4_resolve;
312 static GResolv *ipv6_resolve;
314 static void dummy_resolve_func(GResolvResultStatus status,
315 char **results, gpointer user_data)
320 * Refresh a DNS entry, but also age the hit count a bit */
321 static void refresh_dns_entry(struct cache_entry *entry, char *name)
326 ipv4_resolve = g_resolv_new(0);
327 g_resolv_set_address_family(ipv4_resolve, AF_INET);
328 g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
332 ipv6_resolve = g_resolv_new(0);
333 g_resolv_set_address_family(ipv6_resolve, AF_INET6);
334 g_resolv_add_nameserver(ipv6_resolve, "::1", 53, 0);
338 debug("Refreshing A record for %s", name);
339 g_resolv_lookup_hostname(ipv4_resolve, name,
340 dummy_resolve_func, NULL);
345 debug("Refreshing AAAA record for %s", name);
346 g_resolv_lookup_hostname(ipv6_resolve, name,
347 dummy_resolve_func, NULL);
356 static int dns_name_length(unsigned char *buf)
358 if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
360 return strlen((char *)buf) + 1;
363 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
369 /* skip the header */
373 /* skip the query, which is a name and 2 16 bit words */
374 l = dns_name_length(c);
380 /* now we get the answer records */
384 l = dns_name_length(c);
389 /* then type + class, 2 bytes each */
395 /* now the 4 byte TTL field */
396 c[0] = new_ttl >> 24 & 0xff;
397 c[1] = new_ttl >> 16 & 0xff;
398 c[2] = new_ttl >> 8 & 0xff;
399 c[3] = new_ttl & 0xff;
405 /* now the 2 byte rdlen field */
406 w = c[0] << 8 | c[1];
412 static void send_cached_response(int sk, unsigned char *buf, int len,
413 const struct sockaddr *to, socklen_t tolen,
414 int protocol, int id, uint16_t answers, int ttl)
416 struct domain_hdr *hdr;
417 unsigned char *ptr = buf;
418 int err, offset, dns_len, adj_len = len - 2;
421 * The cached packet contains always the TCP offset (two bytes)
422 * so skip them for UDP.
433 dns_len = ptr[0] * 256 + ptr[1];
442 hdr = (void *) (ptr + offset);
446 hdr->rcode = ns_r_noerror;
447 hdr->ancount = htons(answers);
451 /* if this is a negative reply, we are authorative */
455 update_cached_ttl((unsigned char *)hdr, adj_len, ttl);
457 debug("sk %d id 0x%04x answers %d ptr %p length %d dns %d",
458 sk, hdr->id, answers, ptr, len, dns_len);
460 err = sendto(sk, ptr, len, MSG_NOSIGNAL, to, tolen);
462 connman_error("Cannot send cached DNS response: %s",
467 if (err != len || (dns_len != (len - 2) && protocol == IPPROTO_TCP) ||
468 (dns_len != len && protocol == IPPROTO_UDP))
469 debug("Packet length mismatch, sent %d wanted %d dns %d",
473 static void send_response(int sk, unsigned char *buf, int len,
474 const struct sockaddr *to, socklen_t tolen,
477 struct domain_hdr *hdr;
478 int err, offset = protocol_offset(protocol);
488 hdr = (void *) (buf + offset);
490 debug("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
493 hdr->rcode = ns_r_servfail;
499 err = sendto(sk, buf, len, MSG_NOSIGNAL, to, tolen);
501 connman_error("Failed to send DNS response to %d: %s",
502 sk, strerror(errno));
507 static int get_req_udp_socket(struct request_data *req)
511 if (req->family == AF_INET)
512 channel = req->ifdata->udp4_listener_channel;
514 channel = req->ifdata->udp6_listener_channel;
519 return g_io_channel_unix_get_fd(channel);
522 static void destroy_request_data(struct request_data *req)
524 if (req->timeout > 0)
525 g_source_remove(req->timeout);
528 g_free(req->request);
533 static gboolean request_timeout(gpointer user_data)
535 struct request_data *req = user_data;
542 debug("id 0x%04x", req->srcid);
544 request_list = g_slist_remove(request_list, req);
546 if (req->protocol == IPPROTO_UDP) {
547 sk = get_req_udp_socket(req);
549 } else if (req->protocol == IPPROTO_TCP) {
555 if (req->resplen > 0 && req->resp) {
557 * Here we have received at least one reply (probably telling
558 * "not found" result), so send that back to client instead
559 * of more fatal server failed error.
562 sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
565 } else if (req->request) {
567 * There was not reply from server at all.
569 struct domain_hdr *hdr;
571 hdr = (void *)(req->request + protocol_offset(req->protocol));
572 hdr->id = req->srcid;
575 send_response(sk, req->request, req->request_len,
576 sa, req->sa_len, req->protocol);
580 * We cannot leave TCP client hanging so just kick it out
581 * if we get a request timeout from server.
583 if (req->protocol == IPPROTO_TCP) {
584 debug("client %d removed", req->client_sk);
585 g_hash_table_remove(partial_tcp_req_table,
586 GINT_TO_POINTER(req->client_sk));
591 destroy_request_data(req);
596 static int append_query(unsigned char *buf, unsigned int size,
597 const char *query, const char *domain)
599 unsigned char *ptr = buf;
602 debug("query %s domain %s", query, domain);
607 tmp = strchr(query, '.');
613 memcpy(ptr + 1, query, len);
619 memcpy(ptr + 1, query, tmp - query);
620 ptr += tmp - query + 1;
628 tmp = strchr(domain, '.');
630 len = strlen(domain);
634 memcpy(ptr + 1, domain, len);
640 memcpy(ptr + 1, domain, tmp - domain);
641 ptr += tmp - domain + 1;
651 static bool cache_check_is_valid(struct cache_data *data,
657 if (data->cache_until < current_time)
664 * remove stale cached entries so that they can be refreshed
666 static void cache_enforce_validity(struct cache_entry *entry)
668 time_t current_time = time(NULL);
670 if (!cache_check_is_valid(entry->ipv4, current_time)
672 debug("cache timeout \"%s\" type A", entry->key);
673 g_free(entry->ipv4->data);
679 if (!cache_check_is_valid(entry->ipv6, current_time)
681 debug("cache timeout \"%s\" type AAAA", entry->key);
682 g_free(entry->ipv6->data);
688 static uint16_t cache_check_validity(char *question, uint16_t type,
689 struct cache_entry *entry)
691 time_t current_time = time(NULL);
692 bool want_refresh = false;
695 * if we have a popular entry, we want a refresh instead of
696 * total destruction of the entry.
701 cache_enforce_validity(entry);
705 if (!cache_check_is_valid(entry->ipv4, current_time)) {
706 debug("cache %s \"%s\" type A", entry->ipv4 ?
707 "timeout" : "entry missing", question);
710 entry->want_refresh = true;
713 * We do not remove cache entry if there is still
714 * valid IPv6 entry found in the cache.
716 if (!cache_check_is_valid(entry->ipv6, current_time) && !want_refresh) {
717 g_hash_table_remove(cache, question);
724 if (!cache_check_is_valid(entry->ipv6, current_time)) {
725 debug("cache %s \"%s\" type AAAA", entry->ipv6 ?
726 "timeout" : "entry missing", question);
729 entry->want_refresh = true;
731 if (!cache_check_is_valid(entry->ipv4, current_time) && !want_refresh) {
732 g_hash_table_remove(cache, question);
742 static void cache_element_destroy(gpointer value)
744 struct cache_entry *entry = value;
750 g_free(entry->ipv4->data);
755 g_free(entry->ipv6->data);
762 if (--cache_size < 0)
766 static gboolean try_remove_cache(gpointer user_data)
770 if (__sync_fetch_and_sub(&cache_refcount, 1) == 1) {
771 debug("No cache users, removing it.");
773 g_hash_table_destroy(cache);
780 static void create_cache(void)
782 if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
783 cache = g_hash_table_new_full(g_str_hash,
786 cache_element_destroy);
789 static struct cache_entry *cache_check(gpointer request, int *qtype, int proto)
792 struct cache_entry *entry;
793 struct domain_question *q;
795 int offset, proto_offset;
800 proto_offset = protocol_offset(proto);
801 if (proto_offset < 0)
804 question = request + proto_offset + 12;
806 offset = strlen(question) + 1;
807 q = (void *) (question + offset);
808 type = ntohs(q->type);
810 /* We only cache either A (1) or AAAA (28) requests */
811 if (type != 1 && type != 28)
819 entry = g_hash_table_lookup(cache, question);
823 type = cache_check_validity(question, type, entry);
832 * Get a label/name from DNS resource record. The function decompresses the
833 * label if necessary. The function does not convert the name to presentation
834 * form. This means that the result string will contain label lengths instead
835 * of dots between labels. We intentionally do not want to convert to dotted
836 * format so that we can cache the wire format string directly.
838 static int get_name(int counter,
839 unsigned char *pkt, unsigned char *start, unsigned char *max,
840 unsigned char *output, int output_max, int *output_len,
841 unsigned char **end, char *name, size_t max_name, int *name_len)
845 /* Limit recursion to 10 (this means up to 10 labels in domain name) */
851 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
852 uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
854 if (offset >= max - pkt)
860 return get_name(counter + 1, pkt, pkt + offset, max,
861 output, output_max, output_len, end,
862 name, max_name, name_len);
864 unsigned label_len = *p;
866 if (pkt + label_len > max)
869 if (*output_len > output_max)
872 if ((*name_len + 1 + label_len + 1) > max_name)
876 * We need the original name in order to check
877 * if this answer is the correct one.
879 name[(*name_len)++] = label_len;
880 memcpy(name + *name_len, p + 1, label_len + 1);
881 *name_len += label_len;
883 /* We compress the result */
884 output[0] = NS_CMPRSFLGS;
901 static int parse_rr(unsigned char *buf, unsigned char *start,
903 unsigned char *response, unsigned int *response_size,
904 uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
906 char *name, size_t max_name)
908 struct domain_rr *rr;
910 int name_len = 0, output_len = 0, max_rsp = *response_size;
912 err = get_name(0, buf, start, max, response, max_rsp,
913 &output_len, end, name, max_name, &name_len);
919 if ((unsigned int) offset > *response_size)
922 rr = (void *) (*end);
927 *type = ntohs(rr->type);
928 *class = ntohs(rr->class);
929 *ttl = ntohl(rr->ttl);
930 *rdlen = ntohs(rr->rdlen);
935 memcpy(response + offset, *end, sizeof(struct domain_rr));
937 offset += sizeof(struct domain_rr);
938 *end += sizeof(struct domain_rr);
940 if ((unsigned int) (offset + *rdlen) > *response_size)
943 memcpy(response + offset, *end, *rdlen);
947 *response_size = offset + *rdlen;
952 static bool check_alias(GSList *aliases, char *name)
957 for (list = aliases; list; list = list->next) {
958 int len = strlen((char *)list->data);
959 if (strncmp((char *)list->data, name, len) == 0)
967 static int parse_response(unsigned char *buf, int buflen,
968 char *question, int qlen,
969 uint16_t *type, uint16_t *class, int *ttl,
970 unsigned char *response, unsigned int *response_len,
973 struct domain_hdr *hdr = (void *) buf;
974 struct domain_question *q;
976 uint16_t qdcount = ntohs(hdr->qdcount);
977 uint16_t ancount = ntohs(hdr->ancount);
979 uint16_t qtype, qclass;
980 unsigned char *next = NULL;
981 unsigned int maxlen = *response_len;
982 GSList *aliases = NULL, *list;
983 char name[NS_MAXDNAME + 1];
988 debug("qr %d qdcount %d", hdr->qr, qdcount);
990 /* We currently only cache responses where question count is 1 */
991 if (hdr->qr != 1 || qdcount != 1)
994 ptr = buf + sizeof(struct domain_hdr);
996 strncpy(question, (char *) ptr, qlen);
997 qlen = strlen(question);
998 ptr += qlen + 1; /* skip \0 */
1001 qtype = ntohs(q->type);
1003 /* We cache only A and AAAA records */
1004 if (qtype != 1 && qtype != 28)
1007 qclass = ntohs(q->class);
1009 ptr += 2 + 2; /* ptr points now to answers */
1015 memset(name, 0, sizeof(name));
1018 * We have a bunch of answers (like A, AAAA, CNAME etc) to
1019 * A or AAAA question. We traverse the answers and parse the
1020 * resource records. Only A and AAAA records are cached, all
1021 * the other records in answers are skipped.
1023 for (i = 0; i < ancount; i++) {
1025 * Get one address at a time to this buffer.
1026 * The max size of the answer is
1027 * 2 (pointer) + 2 (type) + 2 (class) +
1028 * 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
1029 * for A or AAAA record.
1030 * For CNAME the size can be bigger.
1032 unsigned char rsp[NS_MAXCDNAME];
1033 unsigned int rsp_len = sizeof(rsp) - 1;
1036 memset(rsp, 0, sizeof(rsp));
1038 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
1039 type, class, ttl, &rdlen, &next, name,
1047 * Now rsp contains compressed or uncompressed resource
1048 * record. Next we check if this record answers the question.
1049 * The name var contains the uncompressed label.
1050 * One tricky bit is the CNAME records as they alias
1051 * the name we might be interested in.
1055 * Go to next answer if the class is not the one we are
1058 if (*class != qclass) {
1065 * Try to resolve aliases also, type is CNAME(5).
1066 * This is important as otherwise the aliased names would not
1067 * be cached at all as the cache would not contain the aliased
1070 * If any CNAME is found in DNS packet, then we cache the alias
1071 * IP address instead of the question (as the server
1072 * said that question has only an alias).
1073 * This means in practice that if e.g., ipv6.google.com is
1074 * queried, DNS server returns CNAME of that name which is
1075 * ipv6.l.google.com. We then cache the address of the CNAME
1076 * but return the question name to client. So the alias
1077 * status of the name is not saved in cache and thus not
1078 * returned to the client. We do not return DNS packets from
1079 * cache to client saying that ipv6.google.com is an alias to
1080 * ipv6.l.google.com but we return instead a DNS packet that
1081 * says ipv6.google.com has address xxx which is in fact the
1082 * address of ipv6.l.google.com. For caching purposes this
1083 * should not cause any issues.
1085 if (*type == 5 && strncmp(question, name, qlen) == 0) {
1087 * So now the alias answered the question. This is
1088 * not very useful from caching point of view as
1089 * the following A or AAAA records will not match the
1090 * question. We need to find the real A/AAAA record
1091 * of the alias and cache that.
1093 unsigned char *end = NULL;
1094 int name_len = 0, output_len = 0;
1096 memset(rsp, 0, sizeof(rsp));
1097 rsp_len = sizeof(rsp) - 1;
1100 * Alias is in rdata part of the message,
1101 * and next-rdlen points to it. So we need to get
1102 * the real name of the alias.
1104 ret = get_name(0, buf, next - rdlen, buf + buflen,
1105 rsp, rsp_len, &output_len, &end,
1106 name, sizeof(name) - 1, &name_len);
1108 /* just ignore the error at this point */
1115 * We should now have the alias of the entry we might
1116 * want to cache. Just remember it for a while.
1117 * We check the alias list when we have parsed the
1120 aliases = g_slist_prepend(aliases, g_strdup(name));
1127 if (*type == qtype) {
1129 * We found correct type (A or AAAA)
1131 if (check_alias(aliases, name) ||
1132 (!aliases && strncmp(question, name,
1135 * We found an alias or the name of the rr
1136 * matches the question. If so, we append
1137 * the compressed label to the cache.
1138 * The end result is a response buffer that
1139 * will contain one or more cached and
1140 * compressed resource records.
1142 if (*response_len + rsp_len > maxlen) {
1146 memcpy(response + *response_len, rsp, rsp_len);
1147 *response_len += rsp_len;
1158 for (list = aliases; list; list = list->next)
1160 g_slist_free(aliases);
1165 struct cache_timeout {
1166 time_t current_time;
1171 static gboolean cache_check_entry(gpointer key, gpointer value,
1174 struct cache_timeout *data = user_data;
1175 struct cache_entry *entry = value;
1178 /* Scale the number of hits by half as part of cache aging */
1183 * If either IPv4 or IPv6 cached entry has expired, we
1184 * remove both from the cache.
1187 if (entry->ipv4 && entry->ipv4->timeout > 0) {
1188 max_timeout = entry->ipv4->cache_until;
1189 if (max_timeout > data->max_timeout)
1190 data->max_timeout = max_timeout;
1192 if (entry->ipv4->cache_until < data->current_time)
1196 if (entry->ipv6 && entry->ipv6->timeout > 0) {
1197 max_timeout = entry->ipv6->cache_until;
1198 if (max_timeout > data->max_timeout)
1199 data->max_timeout = max_timeout;
1201 if (entry->ipv6->cache_until < data->current_time)
1206 * if we're asked to try harder, also remove entries that have
1209 if (data->try_harder && entry->hits < 4)
1215 static void cache_cleanup(void)
1217 static int max_timeout;
1218 struct cache_timeout data;
1221 data.current_time = time(NULL);
1222 data.max_timeout = 0;
1223 data.try_harder = 0;
1226 * In the first pass, we only remove entries that have timed out.
1227 * We use a cache of the first time to expire to do this only
1228 * when it makes sense.
1230 if (max_timeout <= data.current_time) {
1231 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1234 debug("removed %d in the first pass", count);
1237 * In the second pass, if the first pass turned up blank,
1238 * we also expire entries with a low hit count,
1239 * while aging the hit count at the same time.
1241 data.try_harder = 1;
1243 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1248 * If we could not remove anything, then remember
1249 * what is the max timeout and do nothing if we
1250 * have not yet reached it. This will prevent
1251 * constant traversal of the cache if it is full.
1253 max_timeout = data.max_timeout;
1258 static gboolean cache_invalidate_entry(gpointer key, gpointer value,
1261 struct cache_entry *entry = value;
1263 /* first, delete any expired elements */
1264 cache_enforce_validity(entry);
1266 /* if anything is not expired, mark the entry for refresh */
1267 if (entry->hits > 0 && (entry->ipv4 || entry->ipv6))
1268 entry->want_refresh = true;
1270 /* delete the cached data */
1272 g_free(entry->ipv4->data);
1273 g_free(entry->ipv4);
1278 g_free(entry->ipv6->data);
1279 g_free(entry->ipv6);
1283 /* keep the entry if we want it refreshed, delete it otherwise */
1284 if (entry->want_refresh)
1291 * cache_invalidate is called from places where the DNS landscape
1292 * has changed, say because connections are added or we entered a VPN.
1293 * The logic is to wipe all cache data, but mark all non-expired
1294 * parts of the cache for refresh rather than deleting the whole cache.
1296 static void cache_invalidate(void)
1298 debug("Invalidating the DNS cache %p", cache);
1303 g_hash_table_foreach_remove(cache, cache_invalidate_entry, NULL);
1306 static void cache_refresh_entry(struct cache_entry *entry)
1309 cache_enforce_validity(entry);
1311 if (entry->hits > 2 && !entry->ipv4)
1312 entry->want_refresh = true;
1313 if (entry->hits > 2 && !entry->ipv6)
1314 entry->want_refresh = true;
1316 if (entry->want_refresh) {
1318 char dns_name[NS_MAXDNAME + 1];
1319 entry->want_refresh = false;
1321 /* turn a DNS name into a hostname with dots */
1322 strncpy(dns_name, entry->key, NS_MAXDNAME);
1330 debug("Refreshing %s\n", dns_name);
1331 /* then refresh the hostname */
1332 refresh_dns_entry(entry, &dns_name[1]);
1336 static void cache_refresh_iterator(gpointer key, gpointer value,
1339 struct cache_entry *entry = value;
1341 cache_refresh_entry(entry);
1344 static void cache_refresh(void)
1349 g_hash_table_foreach(cache, cache_refresh_iterator, NULL);
1352 static int reply_query_type(unsigned char *msg, int len)
1358 /* skip the header */
1359 c = msg + sizeof(struct domain_hdr);
1360 len -= sizeof(struct domain_hdr);
1365 /* now the query, which is a name and 2 16 bit words */
1366 l = dns_name_length(c);
1368 type = c[0] << 8 | c[1];
1373 static int cache_update(struct server_data *srv, unsigned char *msg,
1374 unsigned int msg_len)
1376 int offset = protocol_offset(srv->protocol);
1377 int err, qlen, ttl = 0;
1378 uint16_t answers = 0, type = 0, class = 0;
1379 struct domain_hdr *hdr = (void *)(msg + offset);
1380 struct domain_question *q;
1381 struct cache_entry *entry;
1382 struct cache_data *data;
1383 char question[NS_MAXDNAME + 1];
1384 unsigned char response[NS_MAXDNAME + 1];
1386 unsigned int rsplen;
1387 bool new_entry = true;
1388 time_t current_time;
1390 if (cache_size >= MAX_CACHE_SIZE) {
1392 if (cache_size >= MAX_CACHE_SIZE)
1396 current_time = time(NULL);
1398 /* don't do a cache refresh more than twice a minute */
1399 if (next_refresh < current_time) {
1401 next_refresh = current_time + 30;
1407 debug("offset %d hdr %p msg %p rcode %d", offset, hdr, msg, hdr->rcode);
1409 /* Continue only if response code is 0 (=ok) */
1410 if (hdr->rcode != ns_r_noerror)
1416 rsplen = sizeof(response) - 1;
1417 question[sizeof(question) - 1] = '\0';
1419 err = parse_response(msg + offset, msg_len - offset,
1420 question, sizeof(question) - 1,
1421 &type, &class, &ttl,
1422 response, &rsplen, &answers);
1425 * special case: if we do a ipv6 lookup and get no result
1426 * for a record that's already in our ipv4 cache.. we want
1427 * to cache the negative response.
1429 if ((err == -ENOMSG || err == -ENOBUFS) &&
1430 reply_query_type(msg + offset,
1431 msg_len - offset) == 28) {
1432 entry = g_hash_table_lookup(cache, question);
1433 if (entry && entry->ipv4 && !entry->ipv6) {
1434 int cache_offset = 0;
1436 data = g_try_new(struct cache_data, 1);
1439 data->inserted = entry->ipv4->inserted;
1441 data->answers = ntohs(hdr->ancount);
1442 data->timeout = entry->ipv4->timeout;
1443 if (srv->protocol == IPPROTO_UDP)
1445 data->data_len = msg_len + cache_offset;
1446 data->data = ptr = g_malloc(data->data_len);
1447 ptr[0] = (data->data_len - 2) / 256;
1448 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1449 if (srv->protocol == IPPROTO_UDP)
1451 data->valid_until = entry->ipv4->valid_until;
1452 data->cache_until = entry->ipv4->cache_until;
1453 memcpy(ptr, msg, msg_len);
1456 * we will get a "hit" when we serve the response
1460 if (entry->hits < 0)
1466 if (err < 0 || ttl == 0)
1469 qlen = strlen(question);
1472 * If the cache contains already data, check if the
1473 * type of the cached data is the same and do not add
1474 * to cache if data is already there.
1475 * This is needed so that we can cache both A and AAAA
1476 * records for the same name.
1478 entry = g_hash_table_lookup(cache, question);
1480 entry = g_try_new(struct cache_entry, 1);
1484 data = g_try_new(struct cache_data, 1);
1490 entry->key = g_strdup(question);
1491 entry->ipv4 = entry->ipv6 = NULL;
1492 entry->want_refresh = false;
1500 if (type == 1 && entry->ipv4)
1503 if (type == 28 && entry->ipv6)
1506 data = g_try_new(struct cache_data, 1);
1516 * compensate for the hit we'll get for serving
1517 * the response out of the cache
1520 if (entry->hits < 0)
1526 if (ttl < MIN_CACHE_TTL)
1527 ttl = MIN_CACHE_TTL;
1529 data->inserted = current_time;
1531 data->answers = answers;
1532 data->timeout = ttl;
1534 * The "2" in start of the length is the TCP offset. We allocate it
1535 * here even for UDP packet because it simplifies the sending
1538 data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
1539 data->data = ptr = g_malloc(data->data_len);
1540 data->valid_until = current_time + ttl;
1543 * Restrict the cached DNS record TTL to some sane value
1544 * in order to prevent data staying in the cache too long.
1546 if (ttl > MAX_CACHE_TTL)
1547 ttl = MAX_CACHE_TTL;
1549 data->cache_until = round_down_ttl(current_time + ttl, ttl);
1559 * We cache the two extra bytes at the start of the message
1560 * in a TCP packet. When sending UDP packet, we skip the first
1561 * two bytes. This way we do not need to know the format
1562 * (UDP/TCP) of the cached message.
1564 if (srv->protocol == IPPROTO_UDP)
1565 memcpy(ptr + 2, msg, offset + 12);
1567 memcpy(ptr, msg, offset + 12);
1569 ptr[0] = (data->data_len - 2) / 256;
1570 ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1571 if (srv->protocol == IPPROTO_UDP)
1574 memcpy(ptr + offset + 12, question, qlen + 1); /* copy also the \0 */
1576 q = (void *) (ptr + offset + 12 + qlen + 1);
1577 q->type = htons(type);
1578 q->class = htons(class);
1579 memcpy(ptr + offset + 12 + qlen + 1 + sizeof(struct domain_question),
1583 g_hash_table_replace(cache, entry->key, entry);
1587 debug("cache %d %squestion \"%s\" type %d ttl %d size %zd packet %u "
1589 cache_size, new_entry ? "new " : "old ",
1590 question, type, ttl,
1591 sizeof(*entry) + sizeof(*data) + data->data_len + qlen,
1593 srv->protocol == IPPROTO_TCP ?
1594 (unsigned int)(data->data[0] * 256 + data->data[1]) :
1600 static int ns_resolv(struct server_data *server, struct request_data *req,
1601 gpointer request, gpointer name)
1604 int sk, err, type = 0;
1605 char *dot, *lookup = (char *) name;
1606 struct cache_entry *entry;
1608 entry = cache_check(request, &type, req->protocol);
1611 struct cache_data *data;
1613 debug("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1620 ttl_left = data->valid_until - time(NULL);
1624 if (data && req->protocol == IPPROTO_TCP) {
1625 send_cached_response(req->client_sk, data->data,
1626 data->data_len, NULL, 0, IPPROTO_TCP,
1627 req->srcid, data->answers, ttl_left);
1631 if (data && req->protocol == IPPROTO_UDP) {
1632 int udp_sk = get_req_udp_socket(req);
1637 send_cached_response(udp_sk, data->data,
1638 data->data_len, &req->sa, req->sa_len,
1639 IPPROTO_UDP, req->srcid, data->answers,
1645 sk = g_io_channel_unix_get_fd(server->channel);
1647 err = sendto(sk, request, req->request_len, MSG_NOSIGNAL,
1648 server->server_addr, server->server_addr_len);
1650 debug("Cannot send message to server %s sock %d "
1651 "protocol %d (%s/%d)",
1652 server->server, sk, server->protocol,
1653 strerror(errno), errno);
1659 /* If we have more than one dot, we don't add domains */
1660 dot = strchr(lookup, '.');
1661 if (dot && dot != lookup + strlen(lookup) - 1)
1664 if (server->domains && server->domains->data)
1665 req->append_domain = true;
1667 for (list = server->domains; list; list = list->next) {
1669 unsigned char alt[1024];
1670 struct domain_hdr *hdr = (void *) &alt;
1671 int altlen, domlen, offset;
1673 domain = list->data;
1678 offset = protocol_offset(server->protocol);
1682 domlen = strlen(domain) + 1;
1686 alt[offset] = req->altid & 0xff;
1687 alt[offset + 1] = req->altid >> 8;
1689 memcpy(alt + offset + 2, request + offset + 2, 10);
1690 hdr->qdcount = htons(1);
1692 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1699 memcpy(alt + offset + altlen,
1700 request + offset + altlen - domlen,
1701 req->request_len - altlen - offset + domlen);
1703 if (server->protocol == IPPROTO_TCP) {
1704 int req_len = req->request_len + domlen - 2;
1706 alt[0] = (req_len >> 8) & 0xff;
1707 alt[1] = req_len & 0xff;
1710 debug("req %p dstid 0x%04x altid 0x%04x", req, req->dstid,
1713 err = send(sk, alt, req->request_len + domlen, MSG_NOSIGNAL);
1723 static char *convert_label(char *start, char *end, char *ptr, char *uptr,
1724 int remaining_len, int *used_comp, int *used_uncomp)
1727 char name[NS_MAXLABEL];
1729 pos = dn_expand((u_char *)start, (u_char *)end, (u_char *)ptr,
1732 debug("uncompress error [%d/%s]", errno, strerror(errno));
1737 * We need to compress back the name so that we get back to internal
1738 * label presentation.
1740 comp_pos = dn_comp(name, (u_char *)uptr, remaining_len, NULL, NULL);
1742 debug("compress error [%d/%s]", errno, strerror(errno));
1747 *used_uncomp = comp_pos;
1755 static char *uncompress(int16_t field_count, char *start, char *end,
1756 char *ptr, char *uncompressed, int uncomp_len,
1757 char **uncompressed_ptr)
1759 char *uptr = *uncompressed_ptr; /* position in result buffer */
1761 debug("count %d ptr %p end %p uptr %p", field_count, ptr, end, uptr);
1763 while (field_count-- > 0 && ptr < end) {
1764 int dlen; /* data field length */
1765 int ulen; /* uncompress length */
1766 int pos; /* position in compressed string */
1767 char name[NS_MAXLABEL]; /* tmp label */
1768 uint16_t dns_type, dns_class;
1771 if (!convert_label(start, end, ptr, name, NS_MAXLABEL,
1776 * Copy the uncompressed resource record, type, class and \0 to
1780 ulen = strlen(name);
1781 strncpy(uptr, name, uncomp_len - (uptr - uncompressed));
1783 debug("pos %d ulen %d left %d name %s", pos, ulen,
1784 (int)(uncomp_len - (uptr - uncompressed)), uptr);
1792 * We copy also the fixed portion of the result (type, class,
1793 * ttl, address length and the address)
1795 memcpy(uptr, ptr, NS_RRFIXEDSZ);
1797 dns_type = uptr[0] << 8 | uptr[1];
1798 dns_class = uptr[2] << 8 | uptr[3];
1800 if (dns_class != ns_c_in)
1803 ptr += NS_RRFIXEDSZ;
1804 uptr += NS_RRFIXEDSZ;
1807 * Then the variable portion of the result (data length).
1808 * Typically this portion is also compressed
1809 * so we need to uncompress it also when necessary.
1811 if (dns_type == ns_t_cname) {
1812 if (!convert_label(start, end, ptr, uptr,
1813 uncomp_len - (uptr - uncompressed),
1817 uptr[-2] = comp_pos << 8;
1818 uptr[-1] = comp_pos & 0xff;
1823 } else if (dns_type == ns_t_a || dns_type == ns_t_aaaa) {
1824 dlen = uptr[-2] << 8 | uptr[-1];
1826 if (ptr + dlen > end) {
1827 debug("data len %d too long", dlen);
1831 memcpy(uptr, ptr, dlen);
1835 } else if (dns_type == ns_t_soa) {
1839 /* Primary name server expansion */
1840 if (!convert_label(start, end, ptr, uptr,
1841 uncomp_len - (uptr - uncompressed),
1845 total_len += comp_pos;
1846 len_ptr = &uptr[-2];
1850 /* Responsible authority's mailbox */
1851 if (!convert_label(start, end, ptr, uptr,
1852 uncomp_len - (uptr - uncompressed),
1856 total_len += comp_pos;
1861 * Copy rest of the soa fields (serial number,
1862 * refresh interval, retry interval, expiration
1863 * limit and minimum ttl). They are 20 bytes long.
1865 memcpy(uptr, ptr, 20);
1871 * Finally fix the length of the data part
1873 len_ptr[0] = total_len << 8;
1874 len_ptr[1] = total_len & 0xff;
1877 *uncompressed_ptr = uptr;
1886 static int strip_domains(char *name, char *answers, int maxlen)
1889 int name_len = strlen(name);
1890 char *ptr, *start = answers, *end = answers + maxlen;
1892 while (maxlen > 0) {
1893 ptr = strstr(answers, name);
1895 char *domain = ptr + name_len;
1898 int domain_len = strlen(domain);
1900 memmove(answers + name_len,
1901 domain + domain_len,
1902 end - (domain + domain_len));
1905 maxlen -= domain_len;
1909 answers += strlen(answers) + 1;
1910 answers += 2 + 2 + 4; /* skip type, class and ttl fields */
1912 data_len = answers[0] << 8 | answers[1];
1913 answers += 2; /* skip the length field */
1915 if (answers + data_len > end)
1918 answers += data_len;
1919 maxlen -= answers - ptr;
1925 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1926 struct server_data *data)
1928 struct domain_hdr *hdr;
1929 struct request_data *req;
1930 int dns_id, sk, err, offset = protocol_offset(protocol);
1935 hdr = (void *)(reply + offset);
1936 dns_id = reply[offset] | reply[offset + 1] << 8;
1938 debug("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1940 req = find_request(dns_id);
1944 debug("req %p dstid 0x%04x altid 0x%04x rcode %d",
1945 req, req->dstid, req->altid, hdr->rcode);
1947 reply[offset] = req->srcid & 0xff;
1948 reply[offset + 1] = req->srcid >> 8;
1952 if (hdr->rcode == ns_r_noerror || !req->resp) {
1953 unsigned char *new_reply = NULL;
1956 * If the domain name was append
1957 * remove it before forwarding the reply.
1958 * If there were more than one question, then this
1959 * domain name ripping can be hairy so avoid that
1960 * and bail out in that that case.
1962 * The reason we are doing this magic is that if the
1963 * user's DNS client tries to resolv hostname without
1964 * domain part, it also expects to get the result without
1965 * a domain name part.
1967 if (req->append_domain && ntohs(hdr->qdcount) == 1) {
1968 uint16_t domain_len = 0;
1969 uint16_t header_len;
1970 uint16_t dns_type, dns_class;
1971 uint8_t host_len, dns_type_pos;
1972 char uncompressed[NS_MAXDNAME], *uptr;
1973 char *ptr, *eom = (char *)reply + reply_len;
1976 * ptr points to the first char of the hostname.
1977 * ->hostname.domain.net
1979 header_len = offset + sizeof(struct domain_hdr);
1980 ptr = (char *)reply + header_len;
1984 domain_len = strnlen(ptr + 1 + host_len,
1985 reply_len - header_len);
1988 * If the query type is anything other than A or AAAA,
1989 * then bail out and pass the message as is.
1990 * We only want to deal with IPv4 or IPv6 addresses.
1992 dns_type_pos = host_len + 1 + domain_len + 1;
1994 dns_type = ptr[dns_type_pos] << 8 |
1995 ptr[dns_type_pos + 1];
1996 dns_class = ptr[dns_type_pos + 2] << 8 |
1997 ptr[dns_type_pos + 3];
1998 if (dns_type != ns_t_a && dns_type != ns_t_aaaa &&
1999 dns_class != ns_c_in) {
2000 debug("Pass msg dns type %d class %d",
2001 dns_type, dns_class);
2006 * Remove the domain name and replace it by the end
2007 * of reply. Check if the domain is really there
2008 * before trying to copy the data. We also need to
2009 * uncompress the answers if necessary.
2010 * The domain_len can be 0 because if the original
2011 * query did not contain a domain name, then we are
2012 * sending two packets, first without the domain name
2013 * and the second packet with domain name.
2014 * The append_domain is set to true even if we sent
2015 * the first packet without domain name. In this
2016 * case we end up in this branch.
2018 if (domain_len > 0) {
2019 int len = host_len + 1;
2020 int new_len, fixed_len;
2024 * First copy host (without domain name) into
2027 uptr = &uncompressed[0];
2028 memcpy(uptr, ptr, len);
2030 uptr[len] = '\0'; /* host termination */
2034 * Copy type and class fields of the question.
2036 ptr += len + domain_len + 1;
2037 memcpy(uptr, ptr, NS_QFIXEDSZ);
2040 * ptr points to answers after this
2043 uptr += NS_QFIXEDSZ;
2045 fixed_len = answers - uncompressed;
2048 * We then uncompress the result to buffer
2049 * so that we can rip off the domain name
2050 * part from the question. First answers,
2051 * then name server (authority) information,
2052 * and finally additional record info.
2055 ptr = uncompress(ntohs(hdr->ancount),
2056 (char *)reply + offset, eom,
2057 ptr, uncompressed, NS_MAXDNAME,
2062 ptr = uncompress(ntohs(hdr->nscount),
2063 (char *)reply + offset, eom,
2064 ptr, uncompressed, NS_MAXDNAME,
2069 ptr = uncompress(ntohs(hdr->arcount),
2070 (char *)reply + offset, eom,
2071 ptr, uncompressed, NS_MAXDNAME,
2077 * The uncompressed buffer now contains almost
2078 * valid response. Final step is to get rid of
2079 * the domain name because at least glibc
2080 * gethostbyname() implementation does extra
2081 * checks and expects to find an answer without
2082 * domain name if we asked a query without
2083 * domain part. Note that glibc getaddrinfo()
2084 * works differently and accepts FQDN in answer
2086 new_len = strip_domains(uncompressed, answers,
2089 debug("Corrupted packet");
2094 * Because we have now uncompressed the answers
2095 * we might have to create a bigger buffer to
2096 * hold all that data.
2099 reply_len = header_len + new_len + fixed_len;
2101 new_reply = g_try_malloc(reply_len);
2105 memcpy(new_reply, reply, header_len);
2106 memcpy(new_reply + header_len, uncompressed,
2107 new_len + fixed_len);
2117 req->resp = g_try_malloc(reply_len);
2121 memcpy(req->resp, reply, reply_len);
2122 req->resplen = reply_len;
2124 cache_update(data, reply, reply_len);
2130 if (req->numresp < req->numserv) {
2131 if (hdr->rcode > ns_r_noerror) {
2133 } else if (hdr->ancount == 0 && req->append_domain) {
2138 request_list = g_slist_remove(request_list, req);
2140 if (protocol == IPPROTO_UDP) {
2141 sk = get_req_udp_socket(req);
2146 err = sendto(sk, req->resp, req->resplen, 0,
2147 &req->sa, req->sa_len);
2149 sk = req->client_sk;
2150 err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
2154 debug("Cannot send msg, sk %d proto %d errno %d/%s", sk,
2155 protocol, errno, strerror(errno));
2157 debug("proto %d sent %d bytes to %d", protocol, err, sk);
2159 destroy_request_data(req);
2164 static void server_destroy_socket(struct server_data *data)
2166 debug("index %d server %s proto %d", data->index,
2167 data->server, data->protocol);
2169 if (data->watch > 0) {
2170 g_source_remove(data->watch);
2174 if (data->timeout > 0) {
2175 g_source_remove(data->timeout);
2179 if (data->channel) {
2180 g_io_channel_shutdown(data->channel, TRUE, NULL);
2181 g_io_channel_unref(data->channel);
2182 data->channel = NULL;
2185 g_free(data->incoming_reply);
2186 data->incoming_reply = NULL;
2189 static void destroy_server(struct server_data *server)
2191 debug("index %d server %s sock %d", server->index, server->server,
2193 g_io_channel_unix_get_fd(server->channel): -1);
2195 server_list = g_slist_remove(server_list, server);
2196 server_destroy_socket(server);
2198 if (server->protocol == IPPROTO_UDP && server->enabled)
2199 debug("Removing DNS server %s", server->server);
2201 g_free(server->server);
2202 g_list_free_full(server->domains, g_free);
2203 g_free(server->server_addr);
2206 * We do not remove cache right away but delay it few seconds.
2207 * The idea is that when IPv6 DNS server is added via RDNSS, it has a
2208 * lifetime. When the lifetime expires we decrease the refcount so it
2209 * is possible that the cache is then removed. Because a new DNS server
2210 * is usually created almost immediately we would then loose the cache
2211 * without any good reason. The small delay allows the new RDNSS to
2212 * create a new DNS server instance and the refcount does not go to 0.
2214 if (cache && !cache_timer)
2215 cache_timer = g_timeout_add_seconds(3, try_remove_cache, NULL);
2220 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
2223 unsigned char buf[4096];
2225 struct server_data *data = user_data;
2227 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2228 connman_error("Error with UDP server %s", data->server);
2229 server_destroy_socket(data);
2233 sk = g_io_channel_unix_get_fd(channel);
2235 len = recv(sk, buf, sizeof(buf), 0);
2239 err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
2246 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
2250 struct server_data *server = user_data;
2252 sk = g_io_channel_unix_get_fd(channel);
2256 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2259 debug("TCP server channel closed, sk %d", sk);
2262 * Discard any partial response which is buffered; better
2263 * to get a proper response from a working server.
2265 g_free(server->incoming_reply);
2266 server->incoming_reply = NULL;
2268 list = request_list;
2270 struct request_data *req = list->data;
2271 struct domain_hdr *hdr;
2274 if (req->protocol == IPPROTO_UDP)
2281 * If we're not waiting for any further response
2282 * from another name server, then we send an error
2283 * response to the client.
2285 if (req->numserv && --(req->numserv))
2288 hdr = (void *) (req->request + 2);
2289 hdr->id = req->srcid;
2290 send_response(req->client_sk, req->request,
2291 req->request_len, NULL, 0, IPPROTO_TCP);
2293 request_list = g_slist_remove(request_list, req);
2296 destroy_server(server);
2301 if ((condition & G_IO_OUT) && !server->connected) {
2304 bool no_request_sent = true;
2305 struct server_data *udp_server;
2307 udp_server = find_server(server->index, server->server,
2310 for (domains = udp_server->domains; domains;
2311 domains = domains->next) {
2312 char *dom = domains->data;
2314 debug("Adding domain %s to %s",
2315 dom, server->server);
2317 server->domains = g_list_append(server->domains,
2322 server->connected = true;
2323 server_list = g_slist_append(server_list, server);
2325 if (server->timeout > 0) {
2326 g_source_remove(server->timeout);
2327 server->timeout = 0;
2330 for (list = request_list; list; ) {
2331 struct request_data *req = list->data;
2334 if (req->protocol == IPPROTO_UDP) {
2339 debug("Sending req %s over TCP", (char *)req->name);
2341 status = ns_resolv(server, req,
2342 req->request, req->name);
2345 * A cached result was sent,
2346 * so the request can be released
2349 request_list = g_slist_remove(request_list, req);
2350 destroy_request_data(req);
2359 no_request_sent = false;
2361 if (req->timeout > 0)
2362 g_source_remove(req->timeout);
2364 req->timeout = g_timeout_add_seconds(30,
2365 request_timeout, req);
2369 if (no_request_sent) {
2370 destroy_server(server);
2374 } else if (condition & G_IO_IN) {
2375 struct partial_reply *reply = server->incoming_reply;
2379 unsigned char reply_len_buf[2];
2382 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
2385 } else if (bytes_recv < 0) {
2386 if (errno == EAGAIN || errno == EWOULDBLOCK)
2389 connman_error("DNS proxy error %s",
2392 } else if (bytes_recv < 2)
2395 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
2398 debug("TCP reply %d bytes from %d", reply_len, sk);
2400 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
2404 reply->len = reply_len;
2405 reply->received = 0;
2407 server->incoming_reply = reply;
2410 while (reply->received < reply->len) {
2411 bytes_recv = recv(sk, reply->buf + reply->received,
2412 reply->len - reply->received, 0);
2414 connman_error("DNS proxy TCP disconnect");
2416 } else if (bytes_recv < 0) {
2417 if (errno == EAGAIN || errno == EWOULDBLOCK)
2420 connman_error("DNS proxy error %s",
2424 reply->received += bytes_recv;
2427 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
2431 server->incoming_reply = NULL;
2433 destroy_server(server);
2441 static gboolean tcp_idle_timeout(gpointer user_data)
2443 struct server_data *server = user_data;
2450 destroy_server(server);
2455 static int server_create_socket(struct server_data *data)
2460 debug("index %d server %s proto %d", data->index,
2461 data->server, data->protocol);
2463 sk = socket(data->server_addr->sa_family,
2464 data->protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM,
2468 connman_error("Failed to create server %s socket",
2470 server_destroy_socket(data);
2476 interface = connman_inet_ifname(data->index);
2478 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2480 strlen(interface) + 1) < 0) {
2482 connman_error("Failed to bind server %s "
2484 data->server, interface);
2486 server_destroy_socket(data);
2493 data->channel = g_io_channel_unix_new(sk);
2494 if (!data->channel) {
2495 connman_error("Failed to create server %s channel",
2498 server_destroy_socket(data);
2502 g_io_channel_set_close_on_unref(data->channel, TRUE);
2504 if (data->protocol == IPPROTO_TCP) {
2505 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
2506 data->watch = g_io_add_watch(data->channel,
2507 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
2508 tcp_server_event, data);
2509 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
2512 data->watch = g_io_add_watch(data->channel,
2513 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2514 udp_server_event, data);
2516 if (connect(sk, data->server_addr, data->server_addr_len) < 0) {
2519 if ((data->protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
2520 data->protocol == IPPROTO_UDP) {
2522 connman_error("Failed to connect to server %s",
2524 server_destroy_socket(data);
2534 static void enable_fallback(bool enable)
2538 for (list = server_list; list; list = list->next) {
2539 struct server_data *data = list->data;
2541 if (data->index != -1)
2545 DBG("Enabling fallback DNS server %s", data->server);
2547 DBG("Disabling fallback DNS server %s", data->server);
2549 data->enabled = enable;
2553 static struct server_data *create_server(int index,
2554 const char *domain, const char *server,
2557 struct server_data *data;
2558 struct addrinfo hints, *rp;
2561 DBG("index %d server %s", index, server);
2563 data = g_try_new0(struct server_data, 1);
2565 connman_error("Failed to allocate server %s data", server);
2569 data->index = index;
2571 data->domains = g_list_append(data->domains, g_strdup(domain));
2572 data->server = g_strdup(server);
2573 data->protocol = protocol;
2575 memset(&hints, 0, sizeof(hints));
2579 hints.ai_socktype = SOCK_DGRAM;
2583 hints.ai_socktype = SOCK_STREAM;
2587 destroy_server(data);
2590 hints.ai_family = AF_UNSPEC;
2591 hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
2593 ret = getaddrinfo(data->server, "53", &hints, &rp);
2595 connman_error("Failed to parse server %s address: %s\n",
2596 data->server, gai_strerror(ret));
2597 destroy_server(data);
2601 /* Do not blindly copy this code elsewhere; it doesn't loop over the
2602 results using ->ai_next as it should. That's OK in *this* case
2603 because it was a numeric lookup; we *know* there's only one. */
2605 data->server_addr_len = rp->ai_addrlen;
2607 switch (rp->ai_family) {
2609 data->server_addr = (struct sockaddr *)
2610 g_try_new0(struct sockaddr_in, 1);
2613 data->server_addr = (struct sockaddr *)
2614 g_try_new0(struct sockaddr_in6, 1);
2617 connman_error("Wrong address family %d", rp->ai_family);
2620 if (!data->server_addr) {
2622 destroy_server(data);
2625 memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
2628 if (server_create_socket(data) != 0) {
2629 destroy_server(data);
2633 if (protocol == IPPROTO_UDP) {
2634 if (__connman_service_index_is_default(data->index) ||
2635 __connman_service_index_is_split_routing(
2637 data->enabled = true;
2638 DBG("Adding DNS server %s", data->server);
2640 enable_fallback(false);
2643 server_list = g_slist_append(server_list, data);
2649 static bool resolv(struct request_data *req,
2650 gpointer request, gpointer name)
2654 for (list = server_list; list; list = list->next) {
2655 struct server_data *data = list->data;
2657 if (data->protocol == IPPROTO_TCP) {
2658 DBG("server %s ignored proto TCP", data->server);
2662 debug("server %s enabled %d", data->server, data->enabled);
2667 if (!data->channel && data->protocol == IPPROTO_UDP) {
2668 if (server_create_socket(data) < 0) {
2669 DBG("socket creation failed while resolving");
2674 if (ns_resolv(data, req, request, name) > 0)
2681 static void update_domain(int index, const char *domain, bool append)
2685 DBG("index %d domain %s", index, domain);
2690 for (list = server_list; list; list = list->next) {
2691 struct server_data *data = list->data;
2694 bool dom_found = false;
2696 if (data->index < 0)
2699 if (data->index != index)
2702 for (dom_list = data->domains; dom_list;
2703 dom_list = dom_list->next) {
2704 dom = dom_list->data;
2706 if (g_str_equal(dom, domain)) {
2712 if (!dom_found && append) {
2714 g_list_append(data->domains, g_strdup(domain));
2715 } else if (dom_found && !append) {
2717 g_list_remove(data->domains, dom);
2723 static void append_domain(int index, const char *domain)
2725 update_domain(index, domain, true);
2728 static void remove_domain(int index, const char *domain)
2730 update_domain(index, domain, false);
2733 static void flush_requests(struct server_data *server)
2737 list = request_list;
2739 struct request_data *req = list->data;
2743 if (ns_resolv(server, req, req->request, req->name)) {
2745 * A cached result was sent,
2746 * so the request can be released
2749 g_slist_remove(request_list, req);
2750 destroy_request_data(req);
2754 if (req->timeout > 0)
2755 g_source_remove(req->timeout);
2757 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2761 int __connman_dnsproxy_append(int index, const char *domain,
2764 struct server_data *data;
2766 DBG("index %d server %s", index, server);
2768 if (!server && !domain)
2772 append_domain(index, domain);
2777 if (g_str_equal(server, "127.0.0.1"))
2780 if (g_str_equal(server, "::1"))
2783 data = find_server(index, server, IPPROTO_UDP);
2785 append_domain(index, domain);
2789 data = create_server(index, domain, server, IPPROTO_UDP);
2793 flush_requests(data);
2798 static void remove_server(int index, const char *domain,
2799 const char *server, int protocol)
2801 struct server_data *data;
2804 data = find_server(index, server, protocol);
2808 destroy_server(data);
2810 for (list = server_list; list; list = list->next) {
2811 struct server_data *data = list->data;
2813 if (data->index != -1 && data->enabled == true)
2817 enable_fallback(true);
2820 int __connman_dnsproxy_remove(int index, const char *domain,
2823 DBG("index %d server %s", index, server);
2825 if (!server && !domain)
2829 remove_domain(index, domain);
2834 if (g_str_equal(server, "127.0.0.1"))
2837 if (g_str_equal(server, "::1"))
2840 remove_server(index, domain, server, IPPROTO_UDP);
2841 remove_server(index, domain, server, IPPROTO_TCP);
2846 static void dnsproxy_offline_mode(bool enabled)
2850 DBG("enabled %d", enabled);
2852 for (list = server_list; list; list = list->next) {
2853 struct server_data *data = list->data;
2856 DBG("Enabling DNS server %s", data->server);
2857 data->enabled = true;
2861 DBG("Disabling DNS server %s", data->server);
2862 data->enabled = false;
2868 static void dnsproxy_default_changed(struct connman_service *service)
2870 bool server_enabled = false;
2874 DBG("service %p", service);
2876 /* DNS has changed, invalidate the cache */
2880 /* When no services are active, then disable DNS proxying */
2881 dnsproxy_offline_mode(true);
2885 index = __connman_service_get_index(service);
2889 for (list = server_list; list; list = list->next) {
2890 struct server_data *data = list->data;
2892 if (data->index == index) {
2893 DBG("Enabling DNS server %s", data->server);
2894 data->enabled = true;
2895 server_enabled = true;
2897 DBG("Disabling DNS server %s", data->server);
2898 data->enabled = false;
2902 if (!server_enabled)
2903 enable_fallback(true);
2908 static struct connman_notifier dnsproxy_notifier = {
2910 .default_changed = dnsproxy_default_changed,
2911 .offline_mode = dnsproxy_offline_mode,
2914 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2916 static int parse_request(unsigned char *buf, int len,
2917 char *name, unsigned int size)
2919 struct domain_hdr *hdr = (void *) buf;
2920 uint16_t qdcount = ntohs(hdr->qdcount);
2921 uint16_t arcount = ntohs(hdr->arcount);
2923 char *last_label = NULL;
2924 unsigned int remain, used = 0;
2929 debug("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2930 hdr->id, hdr->qr, hdr->opcode,
2933 if (hdr->qr != 0 || qdcount != 1)
2938 ptr = buf + sizeof(struct domain_hdr);
2939 remain = len - sizeof(struct domain_hdr);
2941 while (remain > 0) {
2942 uint8_t label_len = *ptr;
2944 if (label_len == 0x00) {
2945 last_label = (char *) (ptr + 1);
2949 if (used + label_len + 1 > size)
2952 strncat(name, (char *) (ptr + 1), label_len);
2955 used += label_len + 1;
2957 ptr += label_len + 1;
2958 remain -= label_len + 1;
2961 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
2962 !memcmp(last_label + 5, opt_edns0_type, 2)) {
2963 uint16_t edns0_bufsize;
2965 edns0_bufsize = last_label[7] << 8 | last_label[8];
2967 debug("EDNS0 buffer size %u", edns0_bufsize);
2969 /* This is an evil hack until full TCP support has been
2972 * Somtimes the EDNS0 request gets send with a too-small
2973 * buffer size. Since glibc doesn't seem to crash when it
2974 * gets a response biffer then it requested, just bump
2975 * the buffer size up to 4KiB.
2977 if (edns0_bufsize < 0x1000) {
2978 last_label[7] = 0x10;
2979 last_label[8] = 0x00;
2983 debug("query %s", name);
2988 static void client_reset(struct tcp_partial_client_data *client)
2993 if (client->channel) {
2994 debug("client %d closing",
2995 g_io_channel_unix_get_fd(client->channel));
2997 g_io_channel_unref(client->channel);
2998 client->channel = NULL;
3001 if (client->watch > 0) {
3002 g_source_remove(client->watch);
3006 if (client->timeout > 0) {
3007 g_source_remove(client->timeout);
3008 client->timeout = 0;
3011 g_free(client->buf);
3014 client->buf_end = 0;
3017 static unsigned int get_msg_len(unsigned char *buf)
3019 return buf[0]<<8 | buf[1];
3022 static bool read_tcp_data(struct tcp_partial_client_data *client,
3023 void *client_addr, socklen_t client_addr_len,
3026 char query[TCP_MAX_BUF_LEN];
3027 struct request_data *req;
3029 unsigned int msg_len;
3031 bool waiting_for_connect = false;
3033 struct cache_entry *entry;
3035 client_sk = g_io_channel_unix_get_fd(client->channel);
3037 if (read_len == 0) {
3038 debug("client %d closed, pending %d bytes",
3039 client_sk, client->buf_end);
3040 g_hash_table_remove(partial_tcp_req_table,
3041 GINT_TO_POINTER(client_sk));
3045 debug("client %d received %d bytes", client_sk, read_len);
3047 client->buf_end += read_len;
3049 if (client->buf_end < 2)
3052 msg_len = get_msg_len(client->buf);
3053 if (msg_len > TCP_MAX_BUF_LEN) {
3054 debug("client %d sent too much data %d", client_sk, msg_len);
3055 g_hash_table_remove(partial_tcp_req_table,
3056 GINT_TO_POINTER(client_sk));
3061 debug("client %d msg len %d end %d past end %d", client_sk, msg_len,
3062 client->buf_end, client->buf_end - (msg_len + 2));
3064 if (client->buf_end < (msg_len + 2)) {
3065 debug("client %d still missing %d bytes",
3067 msg_len + 2 - client->buf_end);
3071 debug("client %d all data %d received", client_sk, msg_len);
3073 err = parse_request(client->buf + 2, msg_len,
3074 query, sizeof(query));
3075 if (err < 0 || (g_slist_length(server_list) == 0)) {
3076 send_response(client_sk, client->buf, msg_len + 2,
3077 NULL, 0, IPPROTO_TCP);
3081 req = g_try_new0(struct request_data, 1);
3085 memcpy(&req->sa, client_addr, client_addr_len);
3086 req->sa_len = client_addr_len;
3087 req->client_sk = client_sk;
3088 req->protocol = IPPROTO_TCP;
3089 req->family = client->family;
3091 req->srcid = client->buf[2] | (client->buf[3] << 8);
3092 req->dstid = get_id();
3093 req->altid = get_id();
3094 req->request_len = msg_len + 2;
3096 client->buf[2] = req->dstid & 0xff;
3097 client->buf[3] = req->dstid >> 8;
3100 req->ifdata = client->ifdata;
3101 req->append_domain = false;
3104 * Check if the answer is found in the cache before
3105 * creating sockets to the server.
3107 entry = cache_check(client->buf, &qtype, IPPROTO_TCP);
3110 struct cache_data *data;
3112 debug("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
3119 ttl_left = data->valid_until - time(NULL);
3122 send_cached_response(client_sk, data->data,
3123 data->data_len, NULL, 0, IPPROTO_TCP,
3124 req->srcid, data->answers, ttl_left);
3129 debug("data missing, ignoring cache for this query");
3132 for (list = server_list; list; list = list->next) {
3133 struct server_data *data = list->data;
3135 if (data->protocol != IPPROTO_UDP || !data->enabled)
3138 if (!create_server(data->index, NULL, data->server,
3142 waiting_for_connect = true;
3145 if (!waiting_for_connect) {
3146 /* No server is waiting for connect */
3147 send_response(client_sk, client->buf,
3148 req->request_len, NULL, 0, IPPROTO_TCP);
3154 * The server is not connected yet.
3155 * Copy the relevant buffers.
3156 * The request will actually be sent once we're
3157 * properly connected over TCP to the nameserver.
3159 req->request = g_try_malloc0(req->request_len);
3160 if (!req->request) {
3161 send_response(client_sk, client->buf,
3162 req->request_len, NULL, 0, IPPROTO_TCP);
3166 memcpy(req->request, client->buf, req->request_len);
3168 req->name = g_try_malloc0(sizeof(query));
3170 send_response(client_sk, client->buf,
3171 req->request_len, NULL, 0, IPPROTO_TCP);
3172 g_free(req->request);
3176 memcpy(req->name, query, sizeof(query));
3178 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
3180 request_list = g_slist_append(request_list, req);
3183 if (client->buf_end > (msg_len + 2)) {
3184 debug("client %d buf %p -> %p end %d len %d new %d",
3186 client->buf + msg_len + 2,
3187 client->buf, client->buf_end,
3188 TCP_MAX_BUF_LEN - client->buf_end,
3189 client->buf_end - (msg_len + 2));
3190 memmove(client->buf, client->buf + msg_len + 2,
3191 TCP_MAX_BUF_LEN - client->buf_end);
3192 client->buf_end = client->buf_end - (msg_len + 2);
3195 * If we have a full message waiting, just read it
3198 msg_len = get_msg_len(client->buf);
3199 if ((msg_len + 2) == client->buf_end) {
3200 debug("client %d reading another %d bytes", client_sk,
3205 debug("client %d clearing reading buffer", client_sk);
3207 client->buf_end = 0;
3208 memset(client->buf, 0, TCP_MAX_BUF_LEN);
3211 * We received all the packets from client so we must also
3212 * remove the timeout handler here otherwise we might get
3213 * timeout while waiting the results from server.
3215 g_source_remove(client->timeout);
3216 client->timeout = 0;
3222 static gboolean tcp_client_event(GIOChannel *channel, GIOCondition condition,
3225 struct tcp_partial_client_data *client = user_data;
3226 struct sockaddr_in6 client_addr6;
3227 socklen_t client_addr6_len = sizeof(client_addr6);
3228 struct sockaddr_in client_addr4;
3229 socklen_t client_addr4_len = sizeof(client_addr4);
3231 socklen_t *client_addr_len;
3234 client_sk = g_io_channel_unix_get_fd(channel);
3236 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3237 g_hash_table_remove(partial_tcp_req_table,
3238 GINT_TO_POINTER(client_sk));
3240 connman_error("Error with TCP client %d channel", client_sk);
3244 switch (client->family) {
3246 client_addr = &client_addr4;
3247 client_addr_len = &client_addr4_len;
3250 client_addr = &client_addr6;
3251 client_addr_len = &client_addr6_len;
3254 g_hash_table_remove(partial_tcp_req_table,
3255 GINT_TO_POINTER(client_sk));
3256 connman_error("client %p corrupted", client);
3260 len = recvfrom(client_sk, client->buf + client->buf_end,
3261 TCP_MAX_BUF_LEN - client->buf_end, 0,
3262 client_addr, client_addr_len);
3264 if (errno == EAGAIN || errno == EWOULDBLOCK)
3267 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3269 g_hash_table_remove(partial_tcp_req_table,
3270 GINT_TO_POINTER(client_sk));
3274 return read_tcp_data(client, client_addr, *client_addr_len, len);
3277 static gboolean client_timeout(gpointer user_data)
3279 struct tcp_partial_client_data *client = user_data;
3282 sock = g_io_channel_unix_get_fd(client->channel);
3284 debug("client %d timeout pending %d bytes", sock, client->buf_end);
3286 g_hash_table_remove(partial_tcp_req_table, GINT_TO_POINTER(sock));
3291 static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
3292 struct listener_data *ifdata, int family,
3293 guint *listener_watch)
3295 int sk, client_sk, len;
3296 unsigned int msg_len;
3297 struct tcp_partial_client_data *client;
3298 struct sockaddr_in6 client_addr6;
3299 socklen_t client_addr6_len = sizeof(client_addr6);
3300 struct sockaddr_in client_addr4;
3301 socklen_t client_addr4_len = sizeof(client_addr4);
3303 socklen_t *client_addr_len;
3307 debug("condition 0x%02x channel %p ifdata %p family %d",
3308 condition, channel, ifdata, family);
3310 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3311 if (*listener_watch > 0)
3312 g_source_remove(*listener_watch);
3313 *listener_watch = 0;
3315 connman_error("Error with TCP listener channel");
3320 sk = g_io_channel_unix_get_fd(channel);
3322 if (family == AF_INET) {
3323 client_addr = &client_addr4;
3324 client_addr_len = &client_addr4_len;
3326 client_addr = &client_addr6;
3327 client_addr_len = &client_addr6_len;
3330 tv.tv_sec = tv.tv_usec = 0;
3332 FD_SET(sk, &readfds);
3334 select(sk + 1, &readfds, NULL, NULL, &tv);
3335 if (FD_ISSET(sk, &readfds)) {
3336 client_sk = accept(sk, client_addr, client_addr_len);
3337 debug("client %d accepted", client_sk);
3339 debug("No data to read from master %d, waiting.", sk);
3343 if (client_sk < 0) {
3344 connman_error("Accept failure on TCP listener");
3345 *listener_watch = 0;
3349 fcntl(client_sk, F_SETFL, O_NONBLOCK);
3351 client = g_hash_table_lookup(partial_tcp_req_table,
3352 GINT_TO_POINTER(client_sk));
3354 client = g_try_new0(struct tcp_partial_client_data, 1);
3360 g_hash_table_insert(partial_tcp_req_table,
3361 GINT_TO_POINTER(client_sk),
3364 client->channel = g_io_channel_unix_new(client_sk);
3365 g_io_channel_set_close_on_unref(client->channel, TRUE);
3367 client->watch = g_io_add_watch(client->channel,
3368 G_IO_IN, tcp_client_event,
3371 client->ifdata = ifdata;
3373 debug("client %d created %p", client_sk, client);
3375 debug("client %d already exists %p", client_sk, client);
3379 client->buf = g_try_malloc(TCP_MAX_BUF_LEN);
3383 memset(client->buf, 0, TCP_MAX_BUF_LEN);
3384 client->buf_end = 0;
3385 client->family = family;
3387 if (client->timeout == 0)
3388 client->timeout = g_timeout_add_seconds(2, client_timeout,
3392 * Check how much data there is. If all is there, then we can
3393 * proceed normally, otherwise read the bits until everything
3394 * is received or timeout occurs.
3396 len = recv(client_sk, client->buf, TCP_MAX_BUF_LEN, 0);
3398 if (errno == EAGAIN || errno == EWOULDBLOCK) {
3399 debug("client %d no data to read, waiting", client_sk);
3403 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3405 g_hash_table_remove(partial_tcp_req_table,
3406 GINT_TO_POINTER(client_sk));
3411 debug("client %d not enough data to read, waiting", client_sk);
3412 client->buf_end += len;
3416 msg_len = get_msg_len(client->buf);
3417 if (msg_len > TCP_MAX_BUF_LEN) {
3418 debug("client %d invalid message length %u ignoring packet",
3419 client_sk, msg_len);
3420 g_hash_table_remove(partial_tcp_req_table,
3421 GINT_TO_POINTER(client_sk));
3426 * The packet length bytes do not contain the total message length,
3427 * that is the reason to -2 below.
3429 if (msg_len != (unsigned int)(len - 2)) {
3430 debug("client %d sent %d bytes but expecting %u pending %d",
3431 client_sk, len, msg_len + 2, msg_len + 2 - len);
3433 client->buf_end += len;
3437 return read_tcp_data(client, client_addr, *client_addr_len, len);
3440 static gboolean tcp4_listener_event(GIOChannel *channel, GIOCondition condition,
3443 struct listener_data *ifdata = user_data;
3445 return tcp_listener_event(channel, condition, ifdata, AF_INET,
3446 &ifdata->tcp4_listener_watch);
3449 static gboolean tcp6_listener_event(GIOChannel *channel, GIOCondition condition,
3452 struct listener_data *ifdata = user_data;
3454 return tcp_listener_event(channel, condition, user_data, AF_INET6,
3455 &ifdata->tcp6_listener_watch);
3458 static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
3459 struct listener_data *ifdata, int family,
3460 guint *listener_watch)
3462 unsigned char buf[768];
3464 struct request_data *req;
3465 struct sockaddr_in6 client_addr6;
3466 socklen_t client_addr6_len = sizeof(client_addr6);
3467 struct sockaddr_in client_addr4;
3468 socklen_t client_addr4_len = sizeof(client_addr4);
3470 socklen_t *client_addr_len;
3473 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3474 connman_error("Error with UDP listener channel");
3475 *listener_watch = 0;
3479 sk = g_io_channel_unix_get_fd(channel);
3481 if (family == AF_INET) {
3482 client_addr = &client_addr4;
3483 client_addr_len = &client_addr4_len;
3485 client_addr = &client_addr6;
3486 client_addr_len = &client_addr6_len;
3489 memset(client_addr, 0, *client_addr_len);
3490 len = recvfrom(sk, buf, sizeof(buf), 0, client_addr, client_addr_len);
3494 debug("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
3496 err = parse_request(buf, len, query, sizeof(query));
3497 if (err < 0 || (g_slist_length(server_list) == 0)) {
3498 send_response(sk, buf, len, client_addr,
3499 *client_addr_len, IPPROTO_UDP);
3503 req = g_try_new0(struct request_data, 1);
3507 memcpy(&req->sa, client_addr, *client_addr_len);
3508 req->sa_len = *client_addr_len;
3510 req->protocol = IPPROTO_UDP;
3511 req->family = family;
3513 req->srcid = buf[0] | (buf[1] << 8);
3514 req->dstid = get_id();
3515 req->altid = get_id();
3516 req->request_len = len;
3518 buf[0] = req->dstid & 0xff;
3519 buf[1] = req->dstid >> 8;
3522 req->ifdata = ifdata;
3523 req->append_domain = false;
3525 if (resolv(req, buf, query)) {
3526 /* a cached result was sent, so the request can be released */
3531 req->name = g_strdup(query);
3532 req->request = g_malloc(len);
3533 memcpy(req->request, buf, len);
3534 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
3535 request_list = g_slist_append(request_list, req);
3540 static gboolean udp4_listener_event(GIOChannel *channel, GIOCondition condition,
3543 struct listener_data *ifdata = user_data;
3545 return udp_listener_event(channel, condition, ifdata, AF_INET,
3546 &ifdata->udp4_listener_watch);
3549 static gboolean udp6_listener_event(GIOChannel *channel, GIOCondition condition,
3552 struct listener_data *ifdata = user_data;
3554 return udp_listener_event(channel, condition, user_data, AF_INET6,
3555 &ifdata->udp6_listener_watch);
3558 static GIOChannel *get_listener(int family, int protocol, int index)
3560 GIOChannel *channel;
3564 struct sockaddr_in6 sin6;
3565 struct sockaddr_in sin;
3571 debug("family %d protocol %d index %d", family, protocol, index);
3576 type = SOCK_DGRAM | SOCK_CLOEXEC;
3581 type = SOCK_STREAM | SOCK_CLOEXEC;
3588 sk = socket(family, type, protocol);
3589 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
3590 connman_error("No IPv6 support");
3595 connman_error("Failed to create %s listener socket", proto);
3599 interface = connman_inet_ifname(index);
3600 if (!interface || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
3602 strlen(interface) + 1) < 0) {
3603 connman_error("Failed to bind %s listener interface "
3605 proto, family == AF_INET ? "IPv4" : "IPv6",
3606 -errno, strerror(errno));
3613 if (family == AF_INET6) {
3614 memset(&s.sin6, 0, sizeof(s.sin6));
3615 s.sin6.sin6_family = AF_INET6;
3616 s.sin6.sin6_port = htons(53);
3617 slen = sizeof(s.sin6);
3619 if (__connman_inet_get_interface_address(index,
3621 &s.sin6.sin6_addr) < 0) {
3622 /* So we could not find suitable IPv6 address for
3623 * the interface. This could happen if we have
3624 * disabled IPv6 for the interface.
3630 } else if (family == AF_INET) {
3631 memset(&s.sin, 0, sizeof(s.sin));
3632 s.sin.sin_family = AF_INET;
3633 s.sin.sin_port = htons(53);
3634 slen = sizeof(s.sin);
3636 if (__connman_inet_get_interface_address(index,
3638 &s.sin.sin_addr) < 0) {
3647 if (bind(sk, &s.sa, slen) < 0) {
3648 connman_error("Failed to bind %s listener socket", proto);
3653 if (protocol == IPPROTO_TCP) {
3655 if (listen(sk, 10) < 0) {
3656 connman_error("Failed to listen on TCP socket %d/%s",
3657 -errno, strerror(errno));
3662 fcntl(sk, F_SETFL, O_NONBLOCK);
3665 channel = g_io_channel_unix_new(sk);
3667 connman_error("Failed to create %s listener channel", proto);
3672 g_io_channel_set_close_on_unref(channel, TRUE);
3677 #define UDP_IPv4_FAILED 0x01
3678 #define TCP_IPv4_FAILED 0x02
3679 #define UDP_IPv6_FAILED 0x04
3680 #define TCP_IPv6_FAILED 0x08
3681 #define UDP_FAILED (UDP_IPv4_FAILED | UDP_IPv6_FAILED)
3682 #define TCP_FAILED (TCP_IPv4_FAILED | TCP_IPv6_FAILED)
3683 #define IPv6_FAILED (UDP_IPv6_FAILED | TCP_IPv6_FAILED)
3684 #define IPv4_FAILED (UDP_IPv4_FAILED | TCP_IPv4_FAILED)
3686 static int create_dns_listener(int protocol, struct listener_data *ifdata)
3690 if (protocol == IPPROTO_TCP) {
3691 ifdata->tcp4_listener_channel = get_listener(AF_INET, protocol,
3693 if (ifdata->tcp4_listener_channel)
3694 ifdata->tcp4_listener_watch =
3695 g_io_add_watch(ifdata->tcp4_listener_channel,
3696 G_IO_IN, tcp4_listener_event,
3699 ret |= TCP_IPv4_FAILED;
3701 ifdata->tcp6_listener_channel = get_listener(AF_INET6, protocol,
3703 if (ifdata->tcp6_listener_channel)
3704 ifdata->tcp6_listener_watch =
3705 g_io_add_watch(ifdata->tcp6_listener_channel,
3706 G_IO_IN, tcp6_listener_event,
3709 ret |= TCP_IPv6_FAILED;
3711 ifdata->udp4_listener_channel = get_listener(AF_INET, protocol,
3713 if (ifdata->udp4_listener_channel)
3714 ifdata->udp4_listener_watch =
3715 g_io_add_watch(ifdata->udp4_listener_channel,
3716 G_IO_IN, udp4_listener_event,
3719 ret |= UDP_IPv4_FAILED;
3721 ifdata->udp6_listener_channel = get_listener(AF_INET6, protocol,
3723 if (ifdata->udp6_listener_channel)
3724 ifdata->udp6_listener_watch =
3725 g_io_add_watch(ifdata->udp6_listener_channel,
3726 G_IO_IN, udp6_listener_event,
3729 ret |= UDP_IPv6_FAILED;
3735 static void destroy_udp_listener(struct listener_data *ifdata)
3737 DBG("index %d", ifdata->index);
3739 if (ifdata->udp4_listener_watch > 0)
3740 g_source_remove(ifdata->udp4_listener_watch);
3742 if (ifdata->udp6_listener_watch > 0)
3743 g_source_remove(ifdata->udp6_listener_watch);
3745 if (ifdata->udp4_listener_channel)
3746 g_io_channel_unref(ifdata->udp4_listener_channel);
3747 if (ifdata->udp6_listener_channel)
3748 g_io_channel_unref(ifdata->udp6_listener_channel);
3751 static void destroy_tcp_listener(struct listener_data *ifdata)
3753 DBG("index %d", ifdata->index);
3755 if (ifdata->tcp4_listener_watch > 0)
3756 g_source_remove(ifdata->tcp4_listener_watch);
3757 if (ifdata->tcp6_listener_watch > 0)
3758 g_source_remove(ifdata->tcp6_listener_watch);
3760 if (ifdata->tcp4_listener_channel)
3761 g_io_channel_unref(ifdata->tcp4_listener_channel);
3762 if (ifdata->tcp6_listener_channel)
3763 g_io_channel_unref(ifdata->tcp6_listener_channel);
3766 static int create_listener(struct listener_data *ifdata)
3770 err = create_dns_listener(IPPROTO_UDP, ifdata);
3771 if ((err & UDP_FAILED) == UDP_FAILED)
3774 err |= create_dns_listener(IPPROTO_TCP, ifdata);
3775 if ((err & TCP_FAILED) == TCP_FAILED) {
3776 destroy_udp_listener(ifdata);
3780 index = connman_inet_ifindex("lo");
3781 if (ifdata->index == index) {
3782 if ((err & IPv6_FAILED) != IPv6_FAILED)
3783 __connman_resolvfile_append(index, NULL, "::1");
3785 if ((err & IPv4_FAILED) != IPv4_FAILED)
3786 __connman_resolvfile_append(index, NULL, "127.0.0.1");
3792 static void destroy_listener(struct listener_data *ifdata)
3797 index = connman_inet_ifindex("lo");
3798 if (ifdata->index == index) {
3799 __connman_resolvfile_remove(index, NULL, "127.0.0.1");
3800 __connman_resolvfile_remove(index, NULL, "::1");
3803 for (list = request_list; list; list = list->next) {
3804 struct request_data *req = list->data;
3806 debug("Dropping request (id 0x%04x -> 0x%04x)",
3807 req->srcid, req->dstid);
3808 destroy_request_data(req);
3812 g_slist_free(request_list);
3813 request_list = NULL;
3815 destroy_tcp_listener(ifdata);
3816 destroy_udp_listener(ifdata);
3819 int __connman_dnsproxy_add_listener(int index)
3821 struct listener_data *ifdata;
3824 DBG("index %d", index);
3829 if (!listener_table)
3832 if (g_hash_table_lookup(listener_table, GINT_TO_POINTER(index)))
3835 ifdata = g_try_new0(struct listener_data, 1);
3839 ifdata->index = index;
3840 ifdata->udp4_listener_channel = NULL;
3841 ifdata->udp4_listener_watch = 0;
3842 ifdata->tcp4_listener_channel = NULL;
3843 ifdata->tcp4_listener_watch = 0;
3844 ifdata->udp6_listener_channel = NULL;
3845 ifdata->udp6_listener_watch = 0;
3846 ifdata->tcp6_listener_channel = NULL;
3847 ifdata->tcp6_listener_watch = 0;
3849 err = create_listener(ifdata);
3851 connman_error("Couldn't create listener for index %d err %d",
3856 g_hash_table_insert(listener_table, GINT_TO_POINTER(ifdata->index),
3861 void __connman_dnsproxy_remove_listener(int index)
3863 struct listener_data *ifdata;
3865 DBG("index %d", index);
3867 if (!listener_table)
3870 ifdata = g_hash_table_lookup(listener_table, GINT_TO_POINTER(index));
3874 destroy_listener(ifdata);
3876 g_hash_table_remove(listener_table, GINT_TO_POINTER(index));
3879 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
3881 int index = GPOINTER_TO_INT(key);
3882 struct listener_data *ifdata = value;
3884 DBG("index %d", index);
3886 destroy_listener(ifdata);
3889 static void free_partial_reqs(gpointer value)
3891 struct tcp_partial_client_data *data = value;
3897 int __connman_dnsproxy_init(void)
3903 listener_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
3906 partial_tcp_req_table = g_hash_table_new_full(g_direct_hash,
3911 index = connman_inet_ifindex("lo");
3912 err = __connman_dnsproxy_add_listener(index);
3916 err = connman_notifier_register(&dnsproxy_notifier);
3923 __connman_dnsproxy_remove_listener(index);
3924 g_hash_table_destroy(listener_table);
3925 g_hash_table_destroy(partial_tcp_req_table);
3930 void __connman_dnsproxy_cleanup(void)
3935 g_source_remove(cache_timer);
3940 g_hash_table_destroy(cache);
3944 connman_notifier_unregister(&dnsproxy_notifier);
3946 g_hash_table_foreach(listener_table, remove_listener, NULL);
3948 g_hash_table_destroy(listener_table);
3950 g_hash_table_destroy(partial_tcp_req_table);