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);
219 connman_error("Failed to send DNS response: %s",
225 static gboolean request_timeout(gpointer user_data)
227 struct request_data *req = user_data;
228 struct listener_data *ifdata;
230 DBG("id 0x%04x", req->srcid);
235 ifdata = req->ifdata;
237 request_list = g_slist_remove(request_list, req);
240 if (req->resplen > 0 && req->resp != NULL) {
243 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
245 err = sendto(sk, req->resp, req->resplen, 0,
246 &req->sa, req->sa_len);
249 } else if (req->request && req->numserv == 0) {
250 struct domain_hdr *hdr;
252 if (req->protocol == IPPROTO_TCP) {
253 hdr = (void *) (req->request + 2);
254 hdr->id = req->srcid;
255 send_response(req->client_sk, req->request,
256 req->request_len, NULL, 0, IPPROTO_TCP);
258 } else if (req->protocol == IPPROTO_UDP) {
261 hdr = (void *) (req->request);
262 hdr->id = req->srcid;
263 sk = g_io_channel_unix_get_fd(
264 ifdata->udp_listener_channel);
265 send_response(sk, req->request, req->request_len,
266 &req->sa, req->sa_len, IPPROTO_UDP);
276 static int append_query(unsigned char *buf, unsigned int size,
277 const char *query, const char *domain)
279 unsigned char *ptr = buf;
282 DBG("query %s domain %s", query, domain);
284 offset = (char *) query;
285 while (offset != NULL) {
288 tmp = strchr(offset, '.');
290 if (strlen(offset) == 0)
292 *ptr = strlen(offset);
293 memcpy(ptr + 1, offset, strlen(offset));
294 ptr += strlen(offset) + 1;
299 memcpy(ptr + 1, offset, tmp - offset);
300 ptr += tmp - offset + 1;
305 offset = (char *) domain;
306 while (offset != NULL) {
309 tmp = strchr(offset, '.');
311 if (strlen(offset) == 0)
313 *ptr = strlen(offset);
314 memcpy(ptr + 1, offset, strlen(offset));
315 ptr += strlen(offset) + 1;
320 memcpy(ptr + 1, offset, tmp - offset);
321 ptr += tmp - offset + 1;
331 static int ns_resolv(struct server_data *server, struct request_data *req,
332 gpointer request, gpointer name)
336 char *dot, *lookup = (char *) name;
338 sk = g_io_channel_unix_get_fd(server->channel);
340 err = send(sk, request, req->request_len, 0);
344 /* If we have more than one dot, we don't add domains */
345 dot = strchr(lookup, '.');
346 if (dot != NULL && dot != lookup + strlen(lookup) - 1)
349 for (list = server->domains; list; list = list->next) {
351 unsigned char alt[1024];
352 struct domain_hdr *hdr = (void *) &alt;
353 int altlen, domlen, offset;
360 offset = protocol_offset(server->protocol);
364 domlen = strlen(domain) + 1;
368 alt[offset] = req->altid & 0xff;
369 alt[offset + 1] = req->altid >> 8;
371 memcpy(alt + offset + 2, request + offset + 2, 10);
372 hdr->qdcount = htons(1);
374 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
381 memcpy(alt + offset + altlen,
382 request + offset + altlen - domlen,
383 req->request_len - altlen + domlen);
385 if (server->protocol == IPPROTO_TCP) {
386 int req_len = req->request_len + domlen - 1;
388 alt[0] = (req_len >> 8) & 0xff;
389 alt[1] = req_len & 0xff;
392 err = send(sk, alt, req->request_len + domlen + 1, 0);
402 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol)
404 struct domain_hdr *hdr;
405 struct request_data *req;
406 int dns_id, sk, err, offset = protocol_offset(protocol);
407 struct listener_data *ifdata;
412 hdr = (void *)(reply + offset);
413 dns_id = reply[offset] | reply[offset + 1] << 8;
415 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
417 req = find_request(dns_id);
421 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
423 ifdata = req->ifdata;
425 reply[offset] = req->srcid & 0xff;
426 reply[offset + 1] = req->srcid >> 8;
430 if (hdr->rcode == 0 || req->resp == NULL) {
434 req->resp = g_try_malloc(reply_len);
435 if (req->resp == NULL)
438 memcpy(req->resp, reply, reply_len);
439 req->resplen = reply_len;
442 if (hdr->rcode > 0 && req->numresp < req->numserv)
445 if (req->timeout > 0)
446 g_source_remove(req->timeout);
448 request_list = g_slist_remove(request_list, req);
450 if (protocol == IPPROTO_UDP) {
451 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
452 err = sendto(sk, req->resp, req->resplen, 0,
453 &req->sa, req->sa_len);
456 err = send(sk, req->resp, req->resplen, 0);
466 static void destroy_server(struct server_data *server)
470 DBG("interface %s server %s", server->interface, server->server);
472 server_list = g_slist_remove(server_list, server);
474 if (server->watch > 0)
475 g_source_remove(server->watch);
477 if (server->timeout > 0)
478 g_source_remove(server->timeout);
480 g_io_channel_unref(server->channel);
482 if (server->protocol == IPPROTO_UDP)
483 connman_info("Removing DNS server %s", server->server);
485 g_free(server->incoming_reply);
486 g_free(server->server);
487 for (list = server->domains; list; list = list->next) {
488 char *domain = list->data;
490 server->domains = g_list_remove(server->domains, domain);
493 g_free(server->interface);
497 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
500 unsigned char buf[4096];
503 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
504 struct server_data *data = user_data;
506 connman_error("Error with UDP server %s", data->server);
511 sk = g_io_channel_unix_get_fd(channel);
513 len = recv(sk, buf, sizeof(buf), 0);
517 err = forward_dns_reply(buf, len, IPPROTO_UDP);
524 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
528 struct server_data *server = user_data;
530 sk = g_io_channel_unix_get_fd(channel);
534 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
537 DBG("TCP server channel closed");
540 * Discard any partial response which is buffered; better
541 * to get a proper response from a working server.
543 g_free(server->incoming_reply);
544 server->incoming_reply = NULL;
546 for (list = request_list; list; list = list->next) {
547 struct request_data *req = list->data;
548 struct domain_hdr *hdr;
550 if (req->protocol == IPPROTO_UDP)
553 if (req->request == NULL)
557 * If we're not waiting for any further response
558 * from another name server, then we send an error
559 * response to the client.
561 if (req->numserv && --(req->numserv))
564 hdr = (void *) (req->request + 2);
565 hdr->id = req->srcid;
566 send_response(req->client_sk, req->request,
567 req->request_len, NULL, 0, IPPROTO_TCP);
569 request_list = g_slist_remove(request_list, req);
572 destroy_server(server);
577 if ((condition & G_IO_OUT) && !server->connected) {
580 struct server_data *udp_server;
582 udp_server = find_server(server->interface, server->server,
584 if (udp_server != NULL) {
585 for (domains = udp_server->domains; domains;
586 domains = domains->next) {
587 char *dom = domains->data;
589 DBG("Adding domain %s to %s",
590 dom, server->server);
592 server->domains = g_list_append(server->domains,
597 server->connected = TRUE;
598 server_list = g_slist_append(server_list, server);
600 if (server->timeout > 0) {
601 g_source_remove(server->timeout);
605 for (list = request_list; list; list = list->next) {
606 struct request_data *req = list->data;
608 if (req->protocol == IPPROTO_UDP)
611 DBG("Sending req %s over TCP", (char *)req->name);
613 if (req->timeout > 0)
614 g_source_remove(req->timeout);
616 req->timeout = g_timeout_add_seconds(30,
617 request_timeout, req);
618 ns_resolv(server, req, req->request, req->name);
621 } else if (condition & G_IO_IN) {
622 struct partial_reply *reply = server->incoming_reply;
626 unsigned char reply_len_buf[2];
629 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
632 } else if (bytes_recv < 0) {
633 if (errno == EAGAIN || errno == EWOULDBLOCK)
636 connman_error("DNS proxy error %s",
639 } else if (bytes_recv < 2)
642 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
645 DBG("TCP reply %d bytes", reply_len);
647 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
651 reply->len = reply_len;
654 server->incoming_reply = reply;
657 while (reply->received < reply->len) {
658 bytes_recv = recv(sk, reply->buf + reply->received,
659 reply->len - reply->received, 0);
661 connman_error("DNS proxy TCP disconnect");
663 } else if (bytes_recv < 0) {
664 if (errno == EAGAIN || errno == EWOULDBLOCK)
667 connman_error("DNS proxy error %s",
671 reply->received += bytes_recv;
674 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP);
677 server->incoming_reply = NULL;
679 destroy_server(server);
687 static gboolean tcp_idle_timeout(gpointer user_data)
689 struct server_data *server = user_data;
696 destroy_server(server);
701 static struct server_data *create_server(const char *interface,
702 const char *domain, const char *server,
705 struct addrinfo hints, *rp;
706 struct server_data *data;
709 DBG("interface %s server %s", interface, server);
711 memset(&hints, 0, sizeof(hints));
715 hints.ai_socktype = SOCK_DGRAM;
719 hints.ai_socktype = SOCK_STREAM;
725 hints.ai_family = AF_UNSPEC;
726 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
728 ret = getaddrinfo(server, "53", &hints, &rp);
730 connman_error("Failed to parse server %s address: %s\n",
731 server, gai_strerror(ret));
734 /* Do not blindly copy this code elsewhere; it doesn't loop over the
735 results using ->ai_next as it should. That's OK in *this* case
736 because it was a numeric lookup; we *know* there's only one. */
738 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
740 connman_error("Failed to create server %s socket", server);
745 if (interface != NULL) {
746 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
747 interface, strlen(interface) + 1) < 0) {
748 connman_error("Failed to bind server %s "
757 data = g_try_new0(struct server_data, 1);
759 connman_error("Failed to allocate server %s data", server);
765 data->channel = g_io_channel_unix_new(sk);
766 if (data->channel == NULL) {
767 connman_error("Failed to create server %s channel", server);
774 g_io_channel_set_close_on_unref(data->channel, TRUE);
776 if (protocol == IPPROTO_TCP) {
777 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
778 data->watch = g_io_add_watch(data->channel,
779 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
780 tcp_server_event, data);
781 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
784 data->watch = g_io_add_watch(data->channel,
785 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
786 udp_server_event, data);
788 data->interface = g_strdup(interface);
790 data->domains = g_list_append(data->domains, g_strdup(domain));
791 data->server = g_strdup(server);
792 data->protocol = protocol;
794 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
797 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
798 protocol == IPPROTO_UDP) {
801 connman_error("Failed to connect to server %s", server);
803 g_source_remove(data->watch);
804 if (data->timeout > 0)
805 g_source_remove(data->timeout);
807 g_io_channel_unref(data->channel);
810 g_free(data->server);
811 g_free(data->interface);
812 for (list = data->domains; list; list = list->next) {
813 char *domain = list->data;
815 data->domains = g_list_remove(data->domains,
824 if (protocol == IPPROTO_UDP) {
825 /* Enable new servers by default */
826 data->enabled = TRUE;
827 connman_info("Adding DNS server %s", data->server);
829 server_list = g_slist_append(server_list, data);
837 static gboolean resolv(struct request_data *req,
838 gpointer request, gpointer name)
842 for (list = server_list; list; list = list->next) {
843 struct server_data *data = list->data;
845 DBG("server %s enabled %d", data->server, data->enabled);
847 if (data->enabled == FALSE)
850 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
851 data->watch = g_io_add_watch(data->channel,
852 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
853 udp_server_event, data);
855 if (ns_resolv(data, req, request, name) < 0)
862 static void append_domain(const char *interface, const char *domain)
866 DBG("interface %s domain %s", interface, domain);
871 for (list = server_list; list; list = list->next) {
872 struct server_data *data = list->data;
875 gboolean dom_found = FALSE;
877 if (data->interface == NULL)
880 if (g_str_equal(data->interface, interface) == FALSE)
883 for (dom_list = data->domains; dom_list;
884 dom_list = dom_list->next) {
885 dom = dom_list->data;
887 if (g_str_equal(dom, domain)) {
893 if (dom_found == FALSE) {
895 g_list_append(data->domains, g_strdup(domain));
900 int __connman_dnsproxy_append(const char *interface, const char *domain,
903 struct server_data *data;
905 DBG("interface %s server %s", interface, server);
907 if (server == NULL && domain == NULL)
910 if (server == NULL) {
911 append_domain(interface, domain);
916 if (g_str_equal(server, "127.0.0.1") == TRUE)
919 data = find_server(interface, server, IPPROTO_UDP);
921 append_domain(interface, domain);
925 data = create_server(interface, domain, server, IPPROTO_UDP);
932 static void remove_server(const char *interface, const char *domain,
933 const char *server, int protocol)
935 struct server_data *data;
937 data = find_server(interface, server, protocol);
941 destroy_server(data);
944 int __connman_dnsproxy_remove(const char *interface, const char *domain,
947 DBG("interface %s server %s", interface, server);
952 if (g_str_equal(server, "127.0.0.1") == TRUE)
955 remove_server(interface, domain, server, IPPROTO_UDP);
956 remove_server(interface, domain, server, IPPROTO_TCP);
961 void __connman_dnsproxy_flush(void)
965 list = request_pending_list;
967 struct request_data *req = list->data;
971 request_pending_list =
972 g_slist_remove(request_pending_list, req);
973 resolv(req, req->request, req->name);
974 g_free(req->request);
979 static void dnsproxy_offline_mode(connman_bool_t enabled)
983 DBG("enabled %d", enabled);
985 for (list = server_list; list; list = list->next) {
986 struct server_data *data = list->data;
988 if (enabled == FALSE) {
989 connman_info("Enabling DNS server %s", data->server);
990 data->enabled = TRUE;
992 connman_info("Disabling DNS server %s", data->server);
993 data->enabled = FALSE;
998 static void dnsproxy_default_changed(struct connman_service *service)
1003 DBG("service %p", service);
1005 if (service == NULL) {
1006 /* When no services are active, then disable DNS proxying */
1007 dnsproxy_offline_mode(TRUE);
1011 interface = connman_service_get_interface(service);
1012 if (interface == NULL)
1015 for (list = server_list; list; list = list->next) {
1016 struct server_data *data = list->data;
1018 if (g_strcmp0(data->interface, interface) == 0) {
1019 connman_info("Enabling DNS server %s", data->server);
1020 data->enabled = TRUE;
1022 connman_info("Disabling DNS server %s", data->server);
1023 data->enabled = FALSE;
1030 static struct connman_notifier dnsproxy_notifier = {
1032 .default_changed = dnsproxy_default_changed,
1033 .offline_mode = dnsproxy_offline_mode,
1036 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
1038 static int parse_request(unsigned char *buf, int len,
1039 char *name, unsigned int size)
1041 struct domain_hdr *hdr = (void *) buf;
1042 uint16_t qdcount = ntohs(hdr->qdcount);
1043 uint16_t arcount = ntohs(hdr->arcount);
1045 char *last_label = NULL;
1046 unsigned int remain, used = 0;
1051 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
1052 hdr->id, hdr->qr, hdr->opcode,
1055 if (hdr->qr != 0 || qdcount != 1)
1058 memset(name, 0, size);
1060 ptr = buf + sizeof(struct domain_hdr);
1061 remain = len - sizeof(struct domain_hdr);
1063 while (remain > 0) {
1067 last_label = (char *) (ptr + 1);
1071 if (used + len + 1 > size)
1074 strncat(name, (char *) (ptr + 1), len);
1083 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
1084 !memcmp(last_label + 5, opt_edns0_type, 2)) {
1085 uint16_t edns0_bufsize;
1087 edns0_bufsize = last_label[7] << 8 | last_label[8];
1089 DBG("EDNS0 buffer size %u", edns0_bufsize);
1091 /* This is an evil hack until full TCP support has been
1094 * Somtimes the EDNS0 request gets send with a too-small
1095 * buffer size. Since glibc doesn't seem to crash when it
1096 * gets a response biffer then it requested, just bump
1097 * the buffer size up to 4KiB.
1099 if (edns0_bufsize < 0x1000) {
1100 last_label[7] = 0x10;
1101 last_label[8] = 0x00;
1105 DBG("query %s", name);
1110 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
1113 unsigned char buf[768];
1115 struct request_data *req;
1116 struct server_data *server;
1117 int sk, client_sk, len, err;
1118 struct sockaddr_in6 client_addr;
1119 socklen_t client_addr_len = sizeof(client_addr);
1121 struct listener_data *ifdata = user_data;
1123 DBG("condition 0x%x", condition);
1125 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1126 if (ifdata->tcp_listener_watch > 0)
1127 g_source_remove(ifdata->tcp_listener_watch);
1128 ifdata->tcp_listener_watch = 0;
1130 connman_error("Error with TCP listener channel");
1135 sk = g_io_channel_unix_get_fd(channel);
1137 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
1138 if (client_sk < 0) {
1139 connman_error("Accept failure on TCP listener");
1140 ifdata->tcp_listener_watch = 0;
1144 len = recv(client_sk, buf, sizeof(buf), 0);
1148 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
1150 err = parse_request(buf + 2, len - 2, query, sizeof(query));
1151 if (err < 0 || (g_slist_length(server_list) == 0)) {
1152 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
1156 req = g_try_new0(struct request_data, 1);
1160 memcpy(&req->sa, &client_addr, client_addr_len);
1161 req->sa_len = client_addr_len;
1162 req->client_sk = client_sk;
1163 req->protocol = IPPROTO_TCP;
1166 if (request_id == 0x0000 || request_id == 0xffff)
1169 req->srcid = buf[2] | (buf[3] << 8);
1170 req->dstid = request_id;
1171 req->altid = request_id + 1;
1172 req->request_len = len;
1174 buf[2] = req->dstid & 0xff;
1175 buf[3] = req->dstid >> 8;
1178 req->ifdata = (struct listener_data *) ifdata;
1179 request_list = g_slist_append(request_list, req);
1181 for (list = server_list; list; list = list->next) {
1182 struct server_data *data = list->data;
1185 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
1188 server = create_server(data->interface, NULL,
1189 data->server, IPPROTO_TCP);
1192 * If server is NULL, we're not connected yet.
1193 * Copy the relevant buffers and continue with
1194 * the next nameserver.
1195 * The request will actually be sent once we're
1196 * properly connected over TCP to this nameserver.
1198 if (server == NULL) {
1199 req->request = g_try_malloc0(req->request_len);
1200 if (req->request == NULL)
1203 memcpy(req->request, buf, req->request_len);
1205 req->name = g_try_malloc0(sizeof(query));
1206 if (req->name == NULL) {
1207 g_free(req->request);
1210 memcpy(req->name, query, sizeof(query));
1215 if (req->timeout > 0)
1216 g_source_remove(req->timeout);
1218 for (domains = data->domains; domains;
1219 domains = domains->next) {
1220 char *dom = domains->data;
1222 DBG("Adding domain %s to %s", dom, server->server);
1224 server->domains = g_list_append(server->domains,
1228 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
1229 ns_resolv(server, req, buf, query);
1235 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
1238 unsigned char buf[768];
1240 struct request_data *req;
1241 struct sockaddr_in6 client_addr;
1242 socklen_t client_addr_len = sizeof(client_addr);
1244 struct listener_data *ifdata = user_data;
1246 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1247 connman_error("Error with UDP listener channel");
1248 ifdata->udp_listener_watch = 0;
1252 sk = g_io_channel_unix_get_fd(channel);
1254 memset(&client_addr, 0, client_addr_len);
1255 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
1260 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
1262 err = parse_request(buf, len, query, sizeof(query));
1263 if (err < 0 || (g_slist_length(server_list) == 0)) {
1264 send_response(sk, buf, len, (void *)&client_addr,
1265 client_addr_len, IPPROTO_UDP);
1269 req = g_try_new0(struct request_data, 1);
1273 memcpy(&req->sa, &client_addr, client_addr_len);
1274 req->sa_len = client_addr_len;
1276 req->protocol = IPPROTO_UDP;
1279 if (request_id == 0x0000 || request_id == 0xffff)
1282 req->srcid = buf[0] | (buf[1] << 8);
1283 req->dstid = request_id;
1284 req->altid = request_id + 1;
1285 req->request_len = len;
1287 buf[0] = req->dstid & 0xff;
1288 buf[1] = req->dstid >> 8;
1291 req->ifdata = (struct listener_data *) ifdata;
1292 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
1293 request_list = g_slist_append(request_list, req);
1295 return resolv(req, buf, query);
1298 static int create_dns_listener(int protocol, struct listener_data *ifdata)
1300 GIOChannel *channel;
1304 struct sockaddr_in6 sin6;
1305 struct sockaddr_in sin;
1308 int sk, type, v6only = 0;
1309 int family = AF_INET6;
1312 DBG("interface %s", ifdata->ifname);
1329 sk = socket(family, type, protocol);
1330 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
1331 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
1333 sk = socket(family, type, protocol);
1336 connman_error("Failed to create %s listener socket", proto);
1340 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1342 strlen(ifdata->ifname) + 1) < 0) {
1343 connman_error("Failed to bind %s listener interface", proto);
1347 /* Ensure it accepts Legacy IP connections too */
1348 if (family == AF_INET6 &&
1349 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
1350 &v6only, sizeof(v6only)) < 0) {
1351 connman_error("Failed to clear V6ONLY on %s listener socket",
1357 if (family == AF_INET) {
1358 memset(&s.sin, 0, sizeof(s.sin));
1359 s.sin.sin_family = AF_INET;
1360 s.sin.sin_port = htons(53);
1361 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
1362 slen = sizeof(s.sin);
1364 memset(&s.sin6, 0, sizeof(s.sin6));
1365 s.sin6.sin6_family = AF_INET6;
1366 s.sin6.sin6_port = htons(53);
1367 s.sin6.sin6_addr = in6addr_any;
1368 slen = sizeof(s.sin6);
1371 if (bind(sk, &s.sa, slen) < 0) {
1372 connman_error("Failed to bind %s listener socket", proto);
1377 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
1378 connman_error("Failed to listen on TCP socket");
1383 channel = g_io_channel_unix_new(sk);
1384 if (channel == NULL) {
1385 connman_error("Failed to create %s listener channel", proto);
1390 g_io_channel_set_close_on_unref(channel, TRUE);
1392 if (protocol == IPPROTO_TCP) {
1393 ifdata->tcp_listener_channel = channel;
1394 ifdata->tcp_listener_watch = g_io_add_watch(channel,
1395 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
1397 ifdata->udp_listener_channel = channel;
1398 ifdata->udp_listener_watch = g_io_add_watch(channel,
1399 G_IO_IN, udp_listener_event, (gpointer) ifdata);
1405 static void destroy_udp_listener(struct listener_data *ifdata)
1407 DBG("interface %s", ifdata->ifname);
1409 if (ifdata->udp_listener_watch > 0)
1410 g_source_remove(ifdata->udp_listener_watch);
1412 g_io_channel_unref(ifdata->udp_listener_channel);
1415 static void destroy_tcp_listener(struct listener_data *ifdata)
1417 DBG("interface %s", ifdata->ifname);
1419 if (ifdata->tcp_listener_watch > 0)
1420 g_source_remove(ifdata->tcp_listener_watch);
1422 g_io_channel_unref(ifdata->tcp_listener_channel);
1425 static int create_listener(struct listener_data *ifdata)
1429 err = create_dns_listener(IPPROTO_UDP, ifdata);
1433 err = create_dns_listener(IPPROTO_TCP, ifdata);
1435 destroy_udp_listener(ifdata);
1439 if (g_strcmp0(ifdata->ifname, "lo") == 0)
1440 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
1445 static void destroy_listener(struct listener_data *ifdata)
1449 if (g_strcmp0(ifdata->ifname, "lo") == 0)
1450 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
1452 for (list = request_pending_list; list; list = list->next) {
1453 struct request_data *req = list->data;
1455 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
1456 req->srcid, req->dstid);
1459 g_free(req->request);
1465 g_slist_free(request_pending_list);
1466 request_pending_list = NULL;
1468 for (list = request_list; list; list = list->next) {
1469 struct request_data *req = list->data;
1471 DBG("Dropping request (id 0x%04x -> 0x%04x)",
1472 req->srcid, req->dstid);
1475 g_free(req->request);
1481 g_slist_free(request_list);
1482 request_list = NULL;
1484 destroy_tcp_listener(ifdata);
1485 destroy_udp_listener(ifdata);
1488 int __connman_dnsproxy_add_listener(const char *interface)
1490 struct listener_data *ifdata;
1493 DBG("interface %s", interface);
1495 if (g_hash_table_lookup(listener_table, interface) != NULL)
1498 ifdata = g_try_new0(struct listener_data, 1);
1502 ifdata->ifname = g_strdup(interface);
1503 ifdata->udp_listener_channel = NULL;
1504 ifdata->udp_listener_watch = 0;
1505 ifdata->tcp_listener_channel = NULL;
1506 ifdata->tcp_listener_watch = 0;
1508 err = create_listener(ifdata);
1510 connman_error("Couldn't create listener for %s err %d",
1512 g_free(ifdata->ifname);
1516 g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
1520 void __connman_dnsproxy_remove_listener(const char *interface)
1522 struct listener_data *ifdata;
1524 DBG("interface %s", interface);
1526 ifdata = g_hash_table_lookup(listener_table, interface);
1530 destroy_listener(ifdata);
1532 g_hash_table_remove(listener_table, interface);
1535 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
1537 __connman_dnsproxy_remove_listener(key);
1540 int __connman_dnsproxy_init(void)
1546 listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1548 err = __connman_dnsproxy_add_listener("lo");
1552 err = connman_notifier_register(&dnsproxy_notifier);
1559 __connman_dnsproxy_remove_listener("lo");
1560 g_hash_table_destroy(listener_table);
1565 void __connman_dnsproxy_cleanup(void)
1569 connman_notifier_unregister(&dnsproxy_notifier);
1571 g_hash_table_foreach(listener_table, remove_listener, NULL);
1573 g_hash_table_destroy(listener_table);