5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
40 #if __BYTE_ORDER == __LITTLE_ENDIAN
55 } __attribute__ ((packed));
56 #elif __BYTE_ORDER == __BIG_ENDIAN
71 } __attribute__ ((packed));
73 #error "Unknown byte order"
76 struct partial_reply {
92 struct partial_reply *incoming_reply;
97 struct sockaddr_in6 __sin6; /* Only for the length */
115 struct listener_data *ifdata;
118 struct listener_data {
120 GIOChannel *udp_listener_channel;
121 guint udp_listener_watch;
122 GIOChannel *tcp_listener_channel;
123 guint tcp_listener_watch;
126 static GSList *server_list = NULL;
127 static GSList *request_list = NULL;
128 static GSList *request_pending_list = NULL;
129 static guint16 request_id = 0x0000;
130 static GHashTable *listener_table = NULL;
132 static int protocol_offset(int protocol)
147 static struct request_data *find_request(guint16 id)
151 for (list = request_list; list; list = list->next) {
152 struct request_data *req = list->data;
154 if (req->dstid == id || req->altid == id)
161 static struct server_data *find_server(const char *interface,
167 DBG("interface %s server %s", interface, server);
169 for (list = server_list; list; list = list->next) {
170 struct server_data *data = list->data;
172 if (interface == NULL && data->interface == NULL &&
173 g_str_equal(data->server, server) == TRUE &&
174 data->protocol == protocol)
177 if (interface == NULL ||
178 data->interface == NULL || data->server == NULL)
181 if (g_str_equal(data->interface, interface) == TRUE &&
182 g_str_equal(data->server, server) == TRUE &&
183 data->protocol == protocol)
191 static void send_response(int sk, unsigned char *buf, int len,
192 const struct sockaddr *to, socklen_t tolen,
195 struct domain_hdr *hdr;
196 int err, offset = protocol_offset(protocol);
206 hdr = (void *) (buf + offset);
208 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
217 err = sendto(sk, buf, len, 0, to, tolen);
220 static gboolean request_timeout(gpointer user_data)
222 struct request_data *req = user_data;
223 struct listener_data *ifdata;
225 DBG("id 0x%04x", req->srcid);
230 ifdata = req->ifdata;
232 request_list = g_slist_remove(request_list, req);
235 if (req->resplen > 0 && req->resp != NULL) {
238 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
240 err = sendto(sk, req->resp, req->resplen, 0,
241 &req->sa, req->sa_len);
242 } else if (req->request && req->numserv == 0) {
243 struct domain_hdr *hdr;
245 if (req->protocol == IPPROTO_TCP) {
246 hdr = (void *) (req->request + 2);
247 hdr->id = req->srcid;
248 send_response(req->client_sk, req->request,
249 req->request_len, NULL, 0, IPPROTO_TCP);
251 } else if (req->protocol == IPPROTO_UDP) {
254 hdr = (void *) (req->request);
255 hdr->id = req->srcid;
256 sk = g_io_channel_unix_get_fd(
257 ifdata->udp_listener_channel);
258 send_response(sk, req->request, req->request_len,
259 &req->sa, req->sa_len, IPPROTO_UDP);
269 static int append_query(unsigned char *buf, unsigned int size,
270 const char *query, const char *domain)
272 unsigned char *ptr = buf;
275 DBG("query %s domain %s", query, domain);
277 offset = (char *) query;
278 while (offset != NULL) {
281 tmp = strchr(offset, '.');
283 if (strlen(offset) == 0)
285 *ptr = strlen(offset);
286 memcpy(ptr + 1, offset, strlen(offset));
287 ptr += strlen(offset) + 1;
292 memcpy(ptr + 1, offset, tmp - offset);
293 ptr += tmp - offset + 1;
298 offset = (char *) domain;
299 while (offset != NULL) {
302 tmp = strchr(offset, '.');
304 if (strlen(offset) == 0)
306 *ptr = strlen(offset);
307 memcpy(ptr + 1, offset, strlen(offset));
308 ptr += strlen(offset) + 1;
313 memcpy(ptr + 1, offset, tmp - offset);
314 ptr += tmp - offset + 1;
324 static int ns_resolv(struct server_data *server, struct request_data *req,
325 gpointer request, gpointer name)
329 char *dot, *lookup = (char *) name;
331 sk = g_io_channel_unix_get_fd(server->channel);
333 err = send(sk, request, req->request_len, 0);
337 /* If we have more than one dot, we don't add domains */
338 dot = strchr(lookup, '.');
339 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
342 for (list = server->domains; list; list = list->next) {
344 unsigned char alt[1024];
345 struct domain_hdr *hdr = (void *) &alt;
346 int altlen, domlen, offset;
353 offset = protocol_offset(server->protocol);
357 domlen = strlen(domain) + 1;
361 alt[offset] = req->altid & 0xff;
362 alt[offset + 1] = req->altid >> 8;
364 memcpy(alt + offset + 2, request + offset + 2, 10);
365 hdr->qdcount = htons(1);
367 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
374 memcpy(alt + offset + altlen,
375 request + offset + altlen - domlen,
376 req->request_len - altlen + domlen);
378 if (server->protocol == IPPROTO_TCP) {
379 int req_len = req->request_len + domlen - 1;
381 alt[0] = (req_len >> 8) & 0xff;
382 alt[1] = req_len & 0xff;
385 err = send(sk, alt, req->request_len + domlen + 1, 0);
393 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol)
395 struct domain_hdr *hdr;
396 struct request_data *req;
397 int dns_id, sk, err, offset = protocol_offset(protocol);
398 struct listener_data *ifdata;
403 hdr = (void *)(reply + offset);
404 dns_id = reply[offset] | reply[offset + 1] << 8;
406 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
408 req = find_request(dns_id);
412 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
414 ifdata = req->ifdata;
416 reply[offset] = req->srcid & 0xff;
417 reply[offset + 1] = req->srcid >> 8;
421 if (hdr->rcode == 0 || req->resp == NULL) {
425 req->resp = g_try_malloc(reply_len);
426 if (req->resp == NULL)
429 memcpy(req->resp, reply, reply_len);
430 req->resplen = reply_len;
433 if (hdr->rcode > 0 && req->numresp < req->numserv)
436 if (req->timeout > 0)
437 g_source_remove(req->timeout);
439 request_list = g_slist_remove(request_list, req);
441 if (protocol == IPPROTO_UDP) {
442 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
443 err = sendto(sk, req->resp, req->resplen, 0,
444 &req->sa, req->sa_len);
447 err = send(sk, req->resp, req->resplen, 0);
457 static void destroy_server(struct server_data *server)
461 DBG("interface %s server %s", server->interface, server->server);
463 server_list = g_slist_remove(server_list, server);
465 if (server->watch > 0)
466 g_source_remove(server->watch);
468 if (server->timeout > 0)
469 g_source_remove(server->timeout);
471 g_io_channel_unref(server->channel);
473 if (server->protocol == IPPROTO_UDP)
474 connman_info("Removing DNS server %s", server->server);
476 g_free(server->incoming_reply);
477 g_free(server->server);
478 for (list = server->domains; list; list = list->next) {
479 char *domain = list->data;
481 server->domains = g_list_remove(server->domains, domain);
484 g_free(server->interface);
488 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
491 unsigned char buf[4096];
494 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
495 struct server_data *data = user_data;
497 connman_error("Error with UDP server %s", data->server);
502 sk = g_io_channel_unix_get_fd(channel);
504 len = recv(sk, buf, sizeof(buf), 0);
508 err = forward_dns_reply(buf, len, IPPROTO_UDP);
513 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
517 struct server_data *server = user_data;
519 sk = g_io_channel_unix_get_fd(channel);
523 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
526 DBG("TCP server channel closed");
529 * Discard any partial response which is buffered; better
530 * to get a proper response from a working server.
532 g_free(server->incoming_reply);
533 server->incoming_reply = NULL;
535 for (list = request_list; list; list = list->next) {
536 struct request_data *req = list->data;
537 struct domain_hdr *hdr;
539 if (req->protocol == IPPROTO_UDP)
542 if (req->request == NULL)
546 * If we're not waiting for any further response
547 * from another name server, then we send an error
548 * response to the client.
550 if (req->numserv && --(req->numserv))
553 hdr = (void *) (req->request + 2);
554 hdr->id = req->srcid;
555 send_response(req->client_sk, req->request,
556 req->request_len, NULL, 0, IPPROTO_TCP);
558 request_list = g_slist_remove(request_list, req);
561 destroy_server(server);
566 if ((condition & G_IO_OUT) && !server->connected) {
569 struct server_data *udp_server;
571 udp_server = find_server(server->interface, server->server,
573 if (udp_server != NULL) {
574 for (domains = udp_server->domains; domains;
575 domains = domains->next) {
576 char *dom = domains->data;
578 DBG("Adding domain %s to %s",
579 dom, server->server);
581 server->domains = g_list_append(server->domains,
586 server->connected = TRUE;
587 server_list = g_slist_append(server_list, server);
589 if (server->timeout > 0) {
590 g_source_remove(server->timeout);
594 for (list = request_list; list; list = list->next) {
595 struct request_data *req = list->data;
597 if (req->protocol == IPPROTO_UDP)
600 DBG("Sending req %s over TCP", (char *)req->name);
602 if (req->timeout > 0)
603 g_source_remove(req->timeout);
605 req->timeout = g_timeout_add_seconds(30,
606 request_timeout, req);
607 ns_resolv(server, req, req->request, req->name);
610 } else if (condition & G_IO_IN) {
611 struct partial_reply *reply = server->incoming_reply;
615 unsigned char reply_len_buf[2];
618 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
621 } else if (bytes_recv < 0) {
622 if (errno == EAGAIN || errno == EWOULDBLOCK)
625 connman_error("DNS proxy error %s",
628 } else if (bytes_recv < 2)
631 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
634 DBG("TCP reply %d bytes", reply_len);
636 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
640 reply->len = reply_len;
643 server->incoming_reply = reply;
646 while (reply->received < reply->len) {
647 bytes_recv = recv(sk, reply->buf + reply->received,
648 reply->len - reply->received, 0);
650 connman_error("DNS proxy TCP disconnect");
652 } else if (bytes_recv < 0) {
653 if (errno == EAGAIN || errno == EWOULDBLOCK)
656 connman_error("DNS proxy error %s",
660 reply->received += bytes_recv;
663 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP);
666 server->incoming_reply = NULL;
668 destroy_server(server);
676 static gboolean tcp_idle_timeout(gpointer user_data)
678 struct server_data *server = user_data;
685 destroy_server(server);
690 static struct server_data *create_server(const char *interface,
691 const char *domain, const char *server,
694 struct addrinfo hints, *rp;
695 struct server_data *data;
698 DBG("interface %s server %s", interface, server);
700 memset(&hints, 0, sizeof(hints));
704 hints.ai_socktype = SOCK_DGRAM;
708 hints.ai_socktype = SOCK_STREAM;
714 hints.ai_family = AF_UNSPEC;
715 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
717 ret = getaddrinfo(server, "53", &hints, &rp);
719 connman_error("Failed to parse server %s address: %s\n",
720 server, gai_strerror(ret));
723 /* Do not blindly copy this code elsewhere; it doesn't loop over the
724 results using ->ai_next as it should. That's OK in *this* case
725 because it was a numeric lookup; we *know* there's only one. */
727 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
729 connman_error("Failed to create server %s socket", server);
734 if (interface != NULL) {
735 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
736 interface, strlen(interface) + 1) < 0) {
737 connman_error("Failed to bind server %s "
746 data = g_try_new0(struct server_data, 1);
748 connman_error("Failed to allocate server %s data", server);
754 data->channel = g_io_channel_unix_new(sk);
755 if (data->channel == NULL) {
756 connman_error("Failed to create server %s channel", server);
763 g_io_channel_set_close_on_unref(data->channel, TRUE);
765 if (protocol == IPPROTO_TCP) {
766 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
767 data->watch = g_io_add_watch(data->channel,
768 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
769 tcp_server_event, data);
770 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
773 data->watch = g_io_add_watch(data->channel,
774 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
775 udp_server_event, data);
777 data->interface = g_strdup(interface);
779 data->domains = g_list_append(data->domains, g_strdup(domain));
780 data->server = g_strdup(server);
781 data->protocol = protocol;
783 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
786 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
787 protocol == IPPROTO_UDP) {
790 connman_error("Failed to connect to server %s", server);
792 g_source_remove(data->watch);
793 if (data->timeout > 0)
794 g_source_remove(data->timeout);
796 g_io_channel_unref(data->channel);
799 g_free(data->server);
800 g_free(data->interface);
801 for (list = data->domains; list; list = list->next) {
802 char *domain = list->data;
804 data->domains = g_list_remove(data->domains,
813 if (protocol == IPPROTO_UDP) {
814 /* Enable new servers by default */
815 data->enabled = TRUE;
816 connman_info("Adding DNS server %s", data->server);
818 server_list = g_slist_append(server_list, data);
826 static gboolean resolv(struct request_data *req,
827 gpointer request, gpointer name)
831 for (list = server_list; list; list = list->next) {
832 struct server_data *data = list->data;
834 DBG("server %s enabled %d", data->server, data->enabled);
836 if (data->enabled == FALSE)
839 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
840 data->watch = g_io_add_watch(data->channel,
841 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
842 udp_server_event, data);
844 if (ns_resolv(data, req, request, name) < 0)
851 static void append_domain(const char *interface, const char *domain)
855 DBG("interface %s domain %s", interface, domain);
860 for (list = server_list; list; list = list->next) {
861 struct server_data *data = list->data;
864 gboolean dom_found = FALSE;
866 if (data->interface == NULL)
869 if (g_str_equal(data->interface, interface) == FALSE)
872 for (dom_list = data->domains; dom_list;
873 dom_list = dom_list->next) {
874 dom = dom_list->data;
876 if (g_str_equal(dom, domain)) {
882 if (dom_found == FALSE) {
884 g_list_append(data->domains, g_strdup(domain));
889 int __connman_dnsproxy_append(const char *interface, const char *domain,
892 struct server_data *data;
894 DBG("interface %s server %s", interface, server);
896 if (server == NULL && domain == NULL)
899 if (server == NULL) {
900 append_domain(interface, domain);
905 if (g_str_equal(server, "127.0.0.1") == TRUE)
908 data = find_server(interface, server, IPPROTO_UDP);
910 append_domain(interface, domain);
914 data = create_server(interface, domain, server, IPPROTO_UDP);
921 static void remove_server(const char *interface, const char *domain,
922 const char *server, int protocol)
924 struct server_data *data;
926 data = find_server(interface, server, protocol);
930 destroy_server(data);
933 int __connman_dnsproxy_remove(const char *interface, const char *domain,
936 DBG("interface %s server %s", interface, server);
941 if (g_str_equal(server, "127.0.0.1") == TRUE)
944 remove_server(interface, domain, server, IPPROTO_UDP);
945 remove_server(interface, domain, server, IPPROTO_TCP);
950 void __connman_dnsproxy_flush(void)
954 list = request_pending_list;
956 struct request_data *req = list->data;
960 request_pending_list =
961 g_slist_remove(request_pending_list, req);
962 resolv(req, req->request, req->name);
963 g_free(req->request);
968 static void dnsproxy_offline_mode(connman_bool_t enabled)
972 DBG("enabled %d", enabled);
974 for (list = server_list; list; list = list->next) {
975 struct server_data *data = list->data;
977 if (enabled == FALSE) {
978 connman_info("Enabling DNS server %s", data->server);
979 data->enabled = TRUE;
981 connman_info("Disabling DNS server %s", data->server);
982 data->enabled = FALSE;
987 static void dnsproxy_default_changed(struct connman_service *service)
992 DBG("service %p", service);
994 if (service == NULL) {
995 /* When no services are active, then disable DNS proxying */
996 dnsproxy_offline_mode(TRUE);
1000 interface = connman_service_get_interface(service);
1001 if (interface == NULL)
1004 for (list = server_list; list; list = list->next) {
1005 struct server_data *data = list->data;
1007 if (g_strcmp0(data->interface, interface) == 0) {
1008 connman_info("Enabling DNS server %s", data->server);
1009 data->enabled = TRUE;
1011 connman_info("Disabling DNS server %s", data->server);
1012 data->enabled = FALSE;
1019 static struct connman_notifier dnsproxy_notifier = {
1021 .default_changed = dnsproxy_default_changed,
1022 .offline_mode = dnsproxy_offline_mode,
1025 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
1027 static int parse_request(unsigned char *buf, int len,
1028 char *name, unsigned int size)
1030 struct domain_hdr *hdr = (void *) buf;
1031 uint16_t qdcount = ntohs(hdr->qdcount);
1032 uint16_t arcount = ntohs(hdr->arcount);
1034 char *last_label = NULL;
1035 unsigned int remain, used = 0;
1040 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
1041 hdr->id, hdr->qr, hdr->opcode,
1044 if (hdr->qr != 0 || qdcount != 1)
1047 memset(name, 0, size);
1049 ptr = buf + sizeof(struct domain_hdr);
1050 remain = len - sizeof(struct domain_hdr);
1052 while (remain > 0) {
1056 last_label = (char *) (ptr + 1);
1060 if (used + len + 1 > size)
1063 strncat(name, (char *) (ptr + 1), len);
1072 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
1073 !memcmp(last_label + 5, opt_edns0_type, 2)) {
1074 uint16_t edns0_bufsize;
1076 edns0_bufsize = last_label[7] << 8 | last_label[8];
1078 DBG("EDNS0 buffer size %u", edns0_bufsize);
1080 /* This is an evil hack until full TCP support has been
1083 * Somtimes the EDNS0 request gets send with a too-small
1084 * buffer size. Since glibc doesn't seem to crash when it
1085 * gets a response biffer then it requested, just bump
1086 * the buffer size up to 4KiB.
1088 if (edns0_bufsize < 0x1000) {
1089 last_label[7] = 0x10;
1090 last_label[8] = 0x00;
1094 DBG("query %s", name);
1099 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
1102 unsigned char buf[768];
1104 struct request_data *req;
1105 struct server_data *server;
1106 int sk, client_sk, len, err;
1107 struct sockaddr_in6 client_addr;
1108 socklen_t client_addr_len = sizeof(client_addr);
1110 struct listener_data *ifdata = user_data;
1112 DBG("condition 0x%x", condition);
1114 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1115 if (ifdata->tcp_listener_watch > 0)
1116 g_source_remove(ifdata->tcp_listener_watch);
1117 ifdata->tcp_listener_watch = 0;
1119 connman_error("Error with TCP listener channel");
1124 sk = g_io_channel_unix_get_fd(channel);
1126 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
1127 if (client_sk < 0) {
1128 connman_error("Accept failure on TCP listener");
1129 ifdata->tcp_listener_watch = 0;
1133 len = recv(client_sk, buf, sizeof(buf), 0);
1137 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
1139 err = parse_request(buf + 2, len - 2, query, sizeof(query));
1140 if (err < 0 || (g_slist_length(server_list) == 0)) {
1141 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
1145 req = g_try_new0(struct request_data, 1);
1149 memcpy(&req->sa, &client_addr, client_addr_len);
1150 req->sa_len = client_addr_len;
1151 req->client_sk = client_sk;
1152 req->protocol = IPPROTO_TCP;
1155 if (request_id == 0x0000 || request_id == 0xffff)
1158 req->srcid = buf[2] | (buf[3] << 8);
1159 req->dstid = request_id;
1160 req->altid = request_id + 1;
1161 req->request_len = len;
1163 buf[2] = req->dstid & 0xff;
1164 buf[3] = req->dstid >> 8;
1167 req->ifdata = (struct listener_data *) ifdata;
1168 request_list = g_slist_append(request_list, req);
1170 for (list = server_list; list; list = list->next) {
1171 struct server_data *data = list->data;
1174 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
1177 server = create_server(data->interface, NULL,
1178 data->server, IPPROTO_TCP);
1181 * If server is NULL, we're not connected yet.
1182 * Copy the relevant buffers and continue with
1183 * the next nameserver.
1184 * The request will actually be sent once we're
1185 * properly connected over TCP to this nameserver.
1187 if (server == NULL) {
1188 req->request = g_try_malloc0(req->request_len);
1189 if (req->request == NULL)
1192 memcpy(req->request, buf, req->request_len);
1194 req->name = g_try_malloc0(sizeof(query));
1195 if (req->name == NULL) {
1196 g_free(req->request);
1199 memcpy(req->name, query, sizeof(query));
1204 if (req->timeout > 0)
1205 g_source_remove(req->timeout);
1207 for (domains = data->domains; domains;
1208 domains = domains->next) {
1209 char *dom = domains->data;
1211 DBG("Adding domain %s to %s", dom, server->server);
1213 server->domains = g_list_append(server->domains,
1217 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
1218 ns_resolv(server, req, buf, query);
1224 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
1227 unsigned char buf[768];
1229 struct request_data *req;
1230 struct sockaddr_in6 client_addr;
1231 socklen_t client_addr_len = sizeof(client_addr);
1233 struct listener_data *ifdata = user_data;
1235 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1236 connman_error("Error with UDP listener channel");
1237 ifdata->udp_listener_watch = 0;
1241 sk = g_io_channel_unix_get_fd(channel);
1243 memset(&client_addr, 0, client_addr_len);
1244 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
1249 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
1251 err = parse_request(buf, len, query, sizeof(query));
1252 if (err < 0 || (g_slist_length(server_list) == 0)) {
1253 send_response(sk, buf, len, (void *)&client_addr,
1254 client_addr_len, IPPROTO_UDP);
1258 req = g_try_new0(struct request_data, 1);
1262 memcpy(&req->sa, &client_addr, client_addr_len);
1263 req->sa_len = client_addr_len;
1265 req->protocol = IPPROTO_UDP;
1268 if (request_id == 0x0000 || request_id == 0xffff)
1271 req->srcid = buf[0] | (buf[1] << 8);
1272 req->dstid = request_id;
1273 req->altid = request_id + 1;
1274 req->request_len = len;
1276 buf[0] = req->dstid & 0xff;
1277 buf[1] = req->dstid >> 8;
1280 req->ifdata = (struct listener_data *) ifdata;
1281 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
1282 request_list = g_slist_append(request_list, req);
1284 return resolv(req, buf, query);
1287 static int create_dns_listener(int protocol, const char *ifname)
1289 GIOChannel *channel;
1293 struct sockaddr_in6 sin6;
1294 struct sockaddr_in sin;
1297 int sk, type, v6only = 0;
1298 int family = AF_INET6;
1299 struct listener_data *ifdata;
1301 DBG("interface %s", ifname);
1303 ifdata = g_hash_table_lookup(listener_table, ifname);
1322 sk = socket(family, type, protocol);
1323 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
1324 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
1326 sk = socket(family, type, protocol);
1329 connman_error("Failed to create %s listener socket", proto);
1333 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1334 ifname, strlen(ifname) + 1) < 0) {
1335 connman_error("Failed to bind %s listener interface", proto);
1339 /* Ensure it accepts Legacy IP connections too */
1340 if (family == AF_INET6 &&
1341 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
1342 &v6only, sizeof(v6only)) < 0) {
1343 connman_error("Failed to clear V6ONLY on %s listener socket",
1349 if (family == AF_INET) {
1350 memset(&s.sin, 0, sizeof(s.sin));
1351 s.sin.sin_family = AF_INET;
1352 s.sin.sin_port = htons(53);
1353 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
1354 slen = sizeof(s.sin);
1356 memset(&s.sin6, 0, sizeof(s.sin6));
1357 s.sin6.sin6_family = AF_INET6;
1358 s.sin6.sin6_port = htons(53);
1359 s.sin6.sin6_addr = in6addr_any;
1360 slen = sizeof(s.sin6);
1363 if (bind(sk, &s.sa, slen) < 0) {
1364 connman_error("Failed to bind %s listener socket", proto);
1369 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
1370 connman_error("Failed to listen on TCP socket");
1375 channel = g_io_channel_unix_new(sk);
1376 if (channel == NULL) {
1377 connman_error("Failed to create %s listener channel", proto);
1382 g_io_channel_set_close_on_unref(channel, TRUE);
1384 if (protocol == IPPROTO_TCP) {
1385 ifdata->tcp_listener_channel = channel;
1386 ifdata->tcp_listener_watch = g_io_add_watch(channel,
1387 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
1389 ifdata->udp_listener_channel = channel;
1390 ifdata->udp_listener_watch = g_io_add_watch(channel,
1391 G_IO_IN, udp_listener_event, (gpointer) ifdata);
1397 static void destroy_udp_listener(const char *interface)
1399 struct listener_data *ifdata;
1401 DBG("interface %s", interface);
1403 ifdata = g_hash_table_lookup(listener_table, interface);
1407 if (ifdata->udp_listener_watch > 0)
1408 g_source_remove(ifdata->udp_listener_watch);
1410 g_io_channel_unref(ifdata->udp_listener_channel);
1413 static void destroy_tcp_listener(const char *interface)
1415 struct listener_data *ifdata;
1417 DBG("interface %s", interface);
1419 ifdata = g_hash_table_lookup(listener_table, interface);
1423 if (ifdata->tcp_listener_watch > 0)
1424 g_source_remove(ifdata->tcp_listener_watch);
1426 g_io_channel_unref(ifdata->tcp_listener_channel);
1429 static int create_listener(const char *interface)
1433 err = create_dns_listener(IPPROTO_UDP, interface);
1437 err = create_dns_listener(IPPROTO_TCP, interface);
1439 destroy_udp_listener(interface);
1443 if (g_strcmp0(interface, "lo") == 0)
1444 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
1449 static void destroy_listener(const char *interface)
1453 if (interface == NULL)
1456 if (g_strcmp0(interface, "lo") == 0)
1457 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
1459 for (list = request_pending_list; list; list = list->next) {
1460 struct request_data *req = list->data;
1462 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
1463 req->srcid, req->dstid);
1466 g_free(req->request);
1472 g_slist_free(request_pending_list);
1473 request_pending_list = NULL;
1475 for (list = request_list; list; list = list->next) {
1476 struct request_data *req = list->data;
1478 DBG("Dropping request (id 0x%04x -> 0x%04x)",
1479 req->srcid, req->dstid);
1482 g_free(req->request);
1488 g_slist_free(request_list);
1489 request_list = NULL;
1491 destroy_tcp_listener(interface);
1492 destroy_udp_listener(interface);
1495 int __connman_dnsproxy_add_listener(const char *interface)
1497 struct listener_data *ifdata;
1500 DBG("interface %s", interface);
1502 if (g_hash_table_lookup(listener_table, interface) != NULL)
1505 ifdata = g_try_new0(struct listener_data, 1);
1509 ifdata->ifname = g_strdup(interface);
1510 ifdata->udp_listener_channel = NULL;
1511 ifdata->udp_listener_watch = 0;
1512 ifdata->tcp_listener_channel = NULL;
1513 ifdata->tcp_listener_watch = 0;
1514 g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
1516 err = create_listener(interface);
1522 void __connman_dnsproxy_remove_listener(const char *interface)
1524 DBG("interface %s", interface);
1526 destroy_listener(interface);
1528 g_hash_table_remove(listener_table, interface);
1531 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
1533 __connman_dnsproxy_remove_listener(key);
1536 int __connman_dnsproxy_init(void)
1542 listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1544 err = __connman_dnsproxy_add_listener("lo");
1548 err = connman_notifier_register(&dnsproxy_notifier);
1555 __connman_dnsproxy_remove_listener("lo");
1556 g_hash_table_destroy(listener_table);
1561 void __connman_dnsproxy_cleanup(void)
1565 connman_notifier_unregister(&dnsproxy_notifier);
1567 g_hash_table_foreach(listener_table, remove_listener, NULL);
1569 g_hash_table_destroy(listener_table);