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 */
117 static connman_bool_t dnsproxy_enabled = TRUE;
118 static GSList *server_list = NULL;
119 static GSList *request_list = NULL;
120 static GSList *request_pending_list = NULL;
121 static guint16 request_id = 0x0000;
123 static GIOChannel *udp_listener_channel = NULL;
124 static guint udp_listener_watch = 0;
125 static GIOChannel *tcp_listener_channel = NULL;
126 static guint tcp_listener_watch = 0;
128 static int protocol_offset(int protocol)
143 static struct request_data *find_request(guint16 id)
147 for (list = request_list; list; list = list->next) {
148 struct request_data *req = list->data;
150 if (req->dstid == id || req->altid == id)
157 static struct server_data *find_server(const char *interface,
163 DBG("interface %s server %s", interface, server);
165 for (list = server_list; list; list = list->next) {
166 struct server_data *data = list->data;
168 if (data->interface == NULL || data->server == NULL)
171 if (g_str_equal(data->interface, interface) == TRUE &&
172 g_str_equal(data->server, server) == TRUE &&
173 data->protocol == protocol)
181 static void send_response(int sk, unsigned char *buf, int len,
182 const struct sockaddr *to, socklen_t tolen,
185 struct domain_hdr *hdr;
186 int err, offset = protocol_offset(protocol);
196 hdr = (void *) (buf + offset);
198 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
207 err = sendto(sk, buf, len, 0, to, tolen);
210 static gboolean request_timeout(gpointer user_data)
212 struct request_data *req = user_data;
214 DBG("id 0x%04x", req->srcid);
219 request_list = g_slist_remove(request_list, req);
222 if (req->resplen > 0 && req->resp != NULL) {
225 sk = g_io_channel_unix_get_fd(udp_listener_channel);
227 err = sendto(sk, req->resp, req->resplen, 0,
228 &req->sa, req->sa_len);
229 } else if (req->request && req->numserv == 0) {
230 struct domain_hdr *hdr;
232 if (req->protocol == IPPROTO_TCP) {
233 hdr = (void *) (req->request + 2);
234 hdr->id = req->srcid;
235 send_response(req->client_sk, req->request,
236 req->request_len, NULL, 0, IPPROTO_TCP);
238 } else if (req->protocol == IPPROTO_UDP) {
241 hdr = (void *) (req->request);
242 hdr->id = req->srcid;
243 sk = g_io_channel_unix_get_fd(udp_listener_channel);
244 send_response(sk, req->request, req->request_len,
245 &req->sa, req->sa_len, IPPROTO_UDP);
255 static int append_query(unsigned char *buf, unsigned int size,
256 const char *query, const char *domain)
258 unsigned char *ptr = buf;
261 DBG("query %s domain %s", query, domain);
263 offset = (char *) query;
264 while (offset != NULL) {
267 tmp = strchr(offset, '.');
269 if (strlen(offset) == 0)
271 *ptr = strlen(offset);
272 memcpy(ptr + 1, offset, strlen(offset));
273 ptr += strlen(offset) + 1;
278 memcpy(ptr + 1, offset, tmp - offset);
279 ptr += tmp - offset + 1;
284 offset = (char *) domain;
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;
310 static int ns_resolv(struct server_data *server, struct request_data *req,
311 gpointer request, gpointer name)
316 sk = g_io_channel_unix_get_fd(server->channel);
318 err = send(sk, request, req->request_len, 0);
322 for (list = server->domains; list; list = list->next) {
324 unsigned char alt[1024];
325 struct domain_hdr *hdr = (void *) &alt;
326 int altlen, domlen, offset;
333 offset = protocol_offset(server->protocol);
337 domlen = strlen(domain) + 1;
341 alt[offset] = req->altid & 0xff;
342 alt[offset + 1] = req->altid >> 8;
344 memcpy(alt + offset + 2, request + offset + 2, 10);
345 hdr->qdcount = htons(1);
347 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
354 memcpy(alt + offset + altlen,
355 request + offset + altlen - domlen,
356 req->request_len - altlen + domlen);
358 if (server->protocol == IPPROTO_TCP) {
359 int req_len = req->request_len + domlen - 1;
361 alt[0] = (req_len >> 8) & 0xff;
362 alt[1] = req_len & 0xff;
365 err = send(sk, alt, req->request_len + domlen + 1, 0);
373 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol)
375 struct domain_hdr *hdr;
376 struct request_data *req;
377 int dns_id, sk, err, offset = protocol_offset(protocol);
382 hdr = (void *)(reply + offset);
383 dns_id = reply[offset] | reply[offset + 1] << 8;
385 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
387 req = find_request(dns_id);
391 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
393 reply[offset] = req->srcid & 0xff;
394 reply[offset + 1] = req->srcid >> 8;
398 if (hdr->rcode == 0 || req->resp == NULL) {
402 req->resp = g_try_malloc(reply_len);
403 if (req->resp == NULL)
406 memcpy(req->resp, reply, reply_len);
407 req->resplen = reply_len;
410 if (hdr->rcode > 0 && req->numresp < req->numserv)
413 if (req->timeout > 0)
414 g_source_remove(req->timeout);
416 request_list = g_slist_remove(request_list, req);
418 if (protocol == IPPROTO_UDP) {
419 sk = g_io_channel_unix_get_fd(udp_listener_channel);
420 err = sendto(sk, req->resp, req->resplen, 0,
421 &req->sa, req->sa_len);
424 err = send(sk, req->resp, req->resplen, 0);
434 static void destroy_server(struct server_data *server)
438 DBG("interface %s server %s", server->interface, server->server);
440 server_list = g_slist_remove(server_list, server);
442 if (server->watch > 0)
443 g_source_remove(server->watch);
445 if (server->timeout > 0)
446 g_source_remove(server->timeout);
448 g_io_channel_unref(server->channel);
450 if (server->protocol == IPPROTO_UDP)
451 connman_info("Removing DNS server %s", server->server);
453 g_free(server->incoming_reply);
454 g_free(server->server);
455 for (list = server->domains; list; list = list->next) {
456 char *domain = list->data;
458 server->domains = g_list_remove(server->domains, domain);
461 g_free(server->interface);
465 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
468 unsigned char buf[4096];
471 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
472 struct server_data *data = user_data;
474 connman_error("Error with UDP server %s", data->server);
479 sk = g_io_channel_unix_get_fd(channel);
481 len = recv(sk, buf, sizeof(buf), 0);
485 err = forward_dns_reply(buf, len, IPPROTO_UDP);
490 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
494 struct server_data *server = user_data;
496 sk = g_io_channel_unix_get_fd(channel);
500 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
503 DBG("TCP server channel closed");
506 * Discard any partial response which is buffered; better
507 * to get a proper response from a working server.
509 g_free(server->incoming_reply);
510 server->incoming_reply = NULL;
512 for (list = request_list; list; list = list->next) {
513 struct request_data *req = list->data;
514 struct domain_hdr *hdr;
516 if (req->protocol == IPPROTO_UDP)
519 if (req->request == NULL)
523 * If we're not waiting for any further response
524 * from another name server, then we send an error
525 * response to the client.
527 if (req->numserv && --(req->numserv))
530 hdr = (void *) (req->request + 2);
531 hdr->id = req->srcid;
532 send_response(req->client_sk, req->request,
533 req->request_len, NULL, 0, IPPROTO_TCP);
535 request_list = g_slist_remove(request_list, req);
538 destroy_server(server);
543 if ((condition & G_IO_OUT) && !server->connected) {
546 struct server_data *udp_server;
548 udp_server = find_server(server->interface, server->server,
550 if (udp_server != NULL) {
551 for (domains = udp_server->domains; domains;
552 domains = domains->next) {
553 char *dom = domains->data;
555 DBG("Adding domain %s to %s",
556 dom, server->server);
558 server->domains = g_list_append(server->domains,
563 server->connected = TRUE;
564 server_list = g_slist_append(server_list, server);
566 if (server->timeout > 0) {
567 g_source_remove(server->timeout);
571 for (list = request_list; list; list = list->next) {
572 struct request_data *req = list->data;
574 if (req->protocol == IPPROTO_UDP)
577 DBG("Sending req %s over TCP", (char *)req->name);
579 if (req->timeout > 0)
580 g_source_remove(req->timeout);
582 req->timeout = g_timeout_add_seconds(30,
583 request_timeout, req);
584 ns_resolv(server, req, req->request, req->name);
587 } else if (condition & G_IO_IN) {
588 struct partial_reply *reply = server->incoming_reply;
592 unsigned char reply_len_buf[2];
595 bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
598 } else if (bytes_recv < 0) {
599 if (errno == EAGAIN || errno == EWOULDBLOCK)
602 connman_error("DNS proxy error %s",
605 } else if (bytes_recv < 2)
608 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
611 DBG("TCP reply %d bytes", reply_len);
613 reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
617 reply->len = reply_len;
620 server->incoming_reply = reply;
623 while (reply->received < reply->len) {
624 bytes_recv = recv(sk, reply->buf + reply->received,
625 reply->len - reply->received, 0);
627 connman_error("DNS proxy TCP disconnect");
629 } else if (bytes_recv < 0) {
630 if (errno == EAGAIN || errno == EWOULDBLOCK)
633 connman_error("DNS proxy error %s",
637 reply->received += bytes_recv;
640 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP);
643 server->incoming_reply = NULL;
645 destroy_server(server);
653 static gboolean tcp_idle_timeout(gpointer user_data)
655 struct server_data *server = user_data;
662 destroy_server(server);
667 static struct server_data *create_server(const char *interface,
668 const char *domain, const char *server,
671 struct addrinfo hints, *rp;
672 struct server_data *data;
675 DBG("interface %s server %s", interface, server);
677 memset(&hints, 0, sizeof(hints));
681 hints.ai_socktype = SOCK_DGRAM;
685 hints.ai_socktype = SOCK_STREAM;
691 hints.ai_family = AF_UNSPEC;
692 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
694 ret = getaddrinfo(server, "53", &hints, &rp);
696 connman_error("Failed to parse server %s address: %s\n",
697 server, gai_strerror(ret));
700 /* Do not blindly copy this code elsewhere; it doesn't loop over the
701 results using ->ai_next as it should. That's OK in *this* case
702 because it was a numeric lookup; we *know* there's only one. */
704 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
706 connman_error("Failed to create server %s socket", server);
711 if (interface != NULL) {
712 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
713 interface, strlen(interface) + 1) < 0) {
714 connman_error("Failed to bind server %s "
723 data = g_try_new0(struct server_data, 1);
725 connman_error("Failed to allocate server %s data", server);
731 data->channel = g_io_channel_unix_new(sk);
732 if (data->channel == NULL) {
733 connman_error("Failed to create server %s channel", server);
740 g_io_channel_set_close_on_unref(data->channel, TRUE);
742 if (protocol == IPPROTO_TCP) {
743 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
744 data->watch = g_io_add_watch(data->channel,
745 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
746 tcp_server_event, data);
747 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
750 data->watch = g_io_add_watch(data->channel,
751 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
752 udp_server_event, data);
754 data->interface = g_strdup(interface);
756 data->domains = g_list_append(data->domains, g_strdup(domain));
757 data->server = g_strdup(server);
758 data->protocol = protocol;
760 ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
763 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
764 protocol == IPPROTO_UDP) {
767 connman_error("Failed to connect to server %s", server);
769 g_source_remove(data->watch);
770 if (data->timeout > 0)
771 g_source_remove(data->timeout);
773 g_io_channel_unref(data->channel);
776 g_free(data->server);
777 g_free(data->interface);
778 for (list = data->domains; list; list = list->next) {
779 char *domain = list->data;
781 data->domains = g_list_remove(data->domains,
790 if (protocol == IPPROTO_UDP) {
791 /* Enable new servers by default */
792 data->enabled = TRUE;
793 connman_info("Adding DNS server %s", data->server);
795 server_list = g_slist_append(server_list, data);
803 static gboolean resolv(struct request_data *req,
804 gpointer request, gpointer name)
808 for (list = server_list; list; list = list->next) {
809 struct server_data *data = list->data;
811 DBG("server %s enabled %d", data->server, data->enabled);
813 if (data->enabled == FALSE)
816 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
817 data->watch = g_io_add_watch(data->channel,
818 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
819 udp_server_event, data);
821 if (ns_resolv(data, req, request, name) < 0)
828 static void append_domain(const char *interface, const char *domain)
832 DBG("interface %s domain %s", interface, domain);
834 for (list = server_list; list; list = list->next) {
835 struct server_data *data = list->data;
838 gboolean dom_found = FALSE;
840 if (data->interface == NULL)
843 if (g_str_equal(data->interface, interface) == FALSE)
846 for (dom_list = data->domains; dom_list;
847 dom_list = dom_list->next) {
848 dom = dom_list->data;
850 if (g_str_equal(dom, domain)) {
856 if (dom_found == FALSE) {
858 g_list_append(data->domains, g_strdup(domain));
863 int __connman_dnsproxy_append(const char *interface, const char *domain,
866 struct server_data *data;
868 DBG("interface %s server %s", interface, server);
870 if (server == NULL && domain == NULL)
873 if (server == NULL) {
874 append_domain(interface, domain);
879 if (g_str_equal(server, "127.0.0.1") == TRUE)
882 data = find_server(interface, server, IPPROTO_UDP);
884 append_domain(interface, domain);
888 data = create_server(interface, domain, server, IPPROTO_UDP);
895 static void remove_server(const char *interface, const char *domain,
896 const char *server, int protocol)
898 struct server_data *data;
900 data = find_server(interface, server, protocol);
904 destroy_server(data);
907 int __connman_dnsproxy_remove(const char *interface, const char *domain,
910 DBG("interface %s server %s", interface, server);
915 if (g_str_equal(server, "127.0.0.1") == TRUE)
918 remove_server(interface, domain, server, IPPROTO_UDP);
919 remove_server(interface, domain, server, IPPROTO_TCP);
924 void __connman_dnsproxy_flush(void)
928 list = request_pending_list;
930 struct request_data *req = list->data;
934 request_pending_list =
935 g_slist_remove(request_pending_list, req);
936 resolv(req, req->request, req->name);
937 g_free(req->request);
942 static void dnsproxy_offline_mode(connman_bool_t enabled)
946 DBG("enabled %d", enabled);
948 for (list = server_list; list; list = list->next) {
949 struct server_data *data = list->data;
951 if (enabled == FALSE) {
952 connman_info("Enabling DNS server %s", data->server);
953 data->enabled = TRUE;
955 connman_info("Disabling DNS server %s", data->server);
956 data->enabled = FALSE;
961 static void dnsproxy_default_changed(struct connman_service *service)
966 DBG("service %p", service);
968 if (service == NULL) {
969 /* When no services are active, then disable DNS proxying */
970 dnsproxy_offline_mode(TRUE);
974 interface = connman_service_get_interface(service);
975 if (interface == NULL)
978 for (list = server_list; list; list = list->next) {
979 struct server_data *data = list->data;
981 if (g_strcmp0(data->interface, interface) == 0) {
982 connman_info("Enabling DNS server %s", data->server);
983 data->enabled = TRUE;
985 connman_info("Disabling DNS server %s", data->server);
986 data->enabled = FALSE;
993 static struct connman_notifier dnsproxy_notifier = {
995 .default_changed = dnsproxy_default_changed,
996 .offline_mode = dnsproxy_offline_mode,
999 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
1001 static int parse_request(unsigned char *buf, int len,
1002 char *name, unsigned int size)
1004 struct domain_hdr *hdr = (void *) buf;
1005 uint16_t qdcount = ntohs(hdr->qdcount);
1006 uint16_t arcount = ntohs(hdr->arcount);
1008 char *last_label = NULL;
1009 unsigned int remain, used = 0;
1014 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
1015 hdr->id, hdr->qr, hdr->opcode,
1018 if (hdr->qr != 0 || qdcount != 1)
1021 memset(name, 0, size);
1023 ptr = buf + sizeof(struct domain_hdr);
1024 remain = len - sizeof(struct domain_hdr);
1026 while (remain > 0) {
1030 last_label = (char *) (ptr + 1);
1034 if (used + len + 1 > size)
1037 strncat(name, (char *) (ptr + 1), len);
1046 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
1047 !memcmp(last_label + 5, opt_edns0_type, 2)) {
1048 uint16_t edns0_bufsize;
1050 edns0_bufsize = last_label[7] << 8 | last_label[8];
1052 DBG("EDNS0 buffer size %u", edns0_bufsize);
1054 /* This is an evil hack until full TCP support has been
1057 * Somtimes the EDNS0 request gets send with a too-small
1058 * buffer size. Since glibc doesn't seem to crash when it
1059 * gets a response biffer then it requested, just bump
1060 * the buffer size up to 4KiB.
1062 if (edns0_bufsize < 0x1000) {
1063 last_label[7] = 0x10;
1064 last_label[8] = 0x00;
1068 DBG("query %s", name);
1073 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
1076 unsigned char buf[768];
1078 struct request_data *req;
1079 struct server_data *server;
1080 int sk, client_sk, len, err;
1081 struct sockaddr_in6 client_addr;
1082 socklen_t client_addr_len = sizeof(client_addr);
1085 DBG("condition 0x%x", condition);
1087 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1088 if (tcp_listener_watch > 0)
1089 g_source_remove(tcp_listener_watch);
1090 tcp_listener_watch = 0;
1092 connman_error("Error with TCP listener channel");
1097 sk = g_io_channel_unix_get_fd(channel);
1099 client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
1100 if (client_sk < 0) {
1101 connman_error("Accept failure on TCP listener");
1102 tcp_listener_watch = 0;
1106 len = recv(client_sk, buf, sizeof(buf), 0);
1110 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
1112 err = parse_request(buf + 2, len - 2, query, sizeof(query));
1113 if (err < 0 || (g_slist_length(server_list) == 0)) {
1114 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
1118 req = g_try_new0(struct request_data, 1);
1122 memcpy(&req->sa, &client_addr, client_addr_len);
1123 req->sa_len = client_addr_len;
1124 req->client_sk = client_sk;
1125 req->protocol = IPPROTO_TCP;
1128 if (request_id == 0x0000 || request_id == 0xffff)
1131 req->srcid = buf[2] | (buf[3] << 8);
1132 req->dstid = request_id;
1133 req->altid = request_id + 1;
1134 req->request_len = len;
1136 buf[2] = req->dstid & 0xff;
1137 buf[3] = req->dstid >> 8;
1140 request_list = g_slist_append(request_list, req);
1142 for (list = server_list; list; list = list->next) {
1143 struct server_data *data = list->data;
1146 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
1149 server = create_server(data->interface, NULL,
1150 data->server, IPPROTO_TCP);
1153 * If server is NULL, we're not connected yet.
1154 * Copy the relevant buffers and continue with
1155 * the next nameserver.
1156 * The request will actually be sent once we're
1157 * properly connected over TCP to this nameserver.
1159 if (server == NULL) {
1160 req->request = g_try_malloc0(req->request_len);
1161 if (req->request == NULL)
1164 memcpy(req->request, buf, req->request_len);
1166 req->name = g_try_malloc0(sizeof(query));
1167 if (req->name == NULL) {
1168 g_free(req->request);
1171 memcpy(req->name, query, sizeof(query));
1176 if (req->timeout > 0)
1177 g_source_remove(req->timeout);
1179 for (domains = data->domains; domains;
1180 domains = domains->next) {
1181 char *dom = domains->data;
1183 DBG("Adding domain %s to %s", dom, server->server);
1185 server->domains = g_list_append(server->domains,
1189 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
1190 ns_resolv(server, req, buf, query);
1196 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
1199 unsigned char buf[768];
1201 struct request_data *req;
1202 struct sockaddr_in6 client_addr;
1203 socklen_t client_addr_len = sizeof(client_addr);
1206 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1207 connman_error("Error with UDP listener channel");
1208 udp_listener_watch = 0;
1212 sk = g_io_channel_unix_get_fd(channel);
1214 memset(&client_addr, 0, client_addr_len);
1215 len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
1220 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
1222 err = parse_request(buf, len, query, sizeof(query));
1223 if (err < 0 || (g_slist_length(server_list) == 0)) {
1224 send_response(sk, buf, len, (void *)&client_addr,
1225 client_addr_len, IPPROTO_UDP);
1229 req = g_try_new0(struct request_data, 1);
1233 memcpy(&req->sa, &client_addr, client_addr_len);
1234 req->sa_len = client_addr_len;
1236 req->protocol = IPPROTO_UDP;
1239 if (request_id == 0x0000 || request_id == 0xffff)
1242 req->srcid = buf[0] | (buf[1] << 8);
1243 req->dstid = request_id;
1244 req->altid = request_id + 1;
1245 req->request_len = len;
1247 buf[0] = req->dstid & 0xff;
1248 buf[1] = req->dstid >> 8;
1251 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
1252 request_list = g_slist_append(request_list, req);
1254 return resolv(req, buf, query);
1257 static int create_dns_listener(int protocol)
1259 GIOChannel *channel;
1260 const char *ifname = "lo", *proto;
1263 struct sockaddr_in6 sin6;
1264 struct sockaddr_in sin;
1267 int sk, type, v6only = 0;
1268 int family = AF_INET6;
1287 sk = socket(family, type, protocol);
1288 if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
1289 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
1291 sk = socket(family, type, protocol);
1294 connman_error("Failed to create %s listener socket", proto);
1298 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1299 ifname, strlen(ifname) + 1) < 0) {
1300 connman_error("Failed to bind %s listener interface", proto);
1304 /* Ensure it accepts Legacy IP connections too */
1305 if (family == AF_INET6 &&
1306 setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
1307 &v6only, sizeof(v6only)) < 0) {
1308 connman_error("Failed to clear V6ONLY on %s listener socket",
1314 if (family == AF_INET) {
1315 memset(&s.sin, 0, sizeof(s.sin));
1316 s.sin.sin_family = AF_INET;
1317 s.sin.sin_port = htons(53);
1318 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
1319 slen = sizeof(s.sin);
1321 memset(&s.sin6, 0, sizeof(s.sin6));
1322 s.sin6.sin6_family = AF_INET6;
1323 s.sin6.sin6_port = htons(53);
1324 s.sin6.sin6_addr = in6addr_any;
1325 slen = sizeof(s.sin6);
1328 if (bind(sk, &s.sa, slen) < 0) {
1329 connman_error("Failed to bind %s listener socket", proto);
1334 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
1335 connman_error("Failed to listen on TCP socket");
1340 channel = g_io_channel_unix_new(sk);
1341 if (channel == NULL) {
1342 connman_error("Failed to create %s listener channel", proto);
1347 g_io_channel_set_close_on_unref(channel, TRUE);
1349 if (protocol == IPPROTO_TCP) {
1350 tcp_listener_channel = channel;
1351 tcp_listener_watch = g_io_add_watch(channel,
1352 G_IO_IN, tcp_listener_event, NULL);
1354 udp_listener_channel = channel;
1355 udp_listener_watch = g_io_add_watch(channel,
1356 G_IO_IN, udp_listener_event, NULL);
1362 static void destroy_udp_listener(void)
1366 if (udp_listener_watch > 0)
1367 g_source_remove(udp_listener_watch);
1369 g_io_channel_unref(udp_listener_channel);
1372 static void destroy_tcp_listener(void)
1376 if (tcp_listener_watch > 0)
1377 g_source_remove(tcp_listener_watch);
1379 g_io_channel_unref(tcp_listener_channel);
1382 static int create_listener(void)
1386 err = create_dns_listener(IPPROTO_UDP);
1390 err = create_dns_listener(IPPROTO_TCP);
1392 destroy_udp_listener();
1396 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
1401 static void destroy_listener(void)
1405 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
1407 for (list = request_pending_list; list; list = list->next) {
1408 struct request_data *req = list->data;
1410 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
1411 req->srcid, req->dstid);
1414 g_free(req->request);
1420 g_slist_free(request_pending_list);
1421 request_pending_list = NULL;
1423 for (list = request_list; list; list = list->next) {
1424 struct request_data *req = list->data;
1426 DBG("Dropping request (id 0x%04x -> 0x%04x)",
1427 req->srcid, req->dstid);
1430 g_free(req->request);
1436 g_slist_free(request_list);
1437 request_list = NULL;
1439 destroy_tcp_listener();
1440 destroy_udp_listener();
1443 int __connman_dnsproxy_init(connman_bool_t dnsproxy)
1447 DBG("dnsproxy %d", dnsproxy);
1449 dnsproxy_enabled = dnsproxy;
1450 if (dnsproxy_enabled == FALSE)
1453 err = create_listener();
1457 err = connman_notifier_register(&dnsproxy_notifier);
1469 void __connman_dnsproxy_cleanup(void)
1473 if (dnsproxy_enabled == FALSE)
1476 connman_notifier_unregister(&dnsproxy_notifier);