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>
33 #define CONNMAN_API_SUBJECT_TO_CHANGE
34 #include <connman/plugin.h>
35 #include <connman/resolver.h>
36 #include <connman/notifier.h>
37 #include <connman/ondemand.h>
38 #include <connman/log.h>
42 #if __BYTE_ORDER == __LITTLE_ENDIAN
57 } __attribute__ ((packed));
58 #elif __BYTE_ORDER == __BIG_ENDIAN
73 } __attribute__ ((packed));
75 #error "Unknown byte order"
91 struct sockaddr_in sin;
109 static GSList *server_list = NULL;
110 static GSList *request_list = NULL;
111 static GSList *request_pending_list = NULL;
112 static guint16 request_id = 0x0000;
114 static GIOChannel *udp_listener_channel = NULL;
115 static guint udp_listener_watch = 0;
116 static GIOChannel *tcp_listener_channel = NULL;
117 static guint tcp_listener_watch = 0;
119 static int protocol_offset(int protocol)
134 static struct request_data *find_request(guint16 id)
138 for (list = request_list; list; list = list->next) {
139 struct request_data *req = list->data;
141 if (req->dstid == id || req->altid == id)
148 static struct server_data *find_server(const char *interface,
149 const char *domain, const char *server,
154 DBG("interface %s server %s", interface, server);
156 for (list = server_list; list; list = list->next) {
157 struct server_data *data = list->data;
159 if (data->interface == NULL || data->server == NULL)
162 if (g_str_equal(data->interface, interface) == TRUE &&
163 g_str_equal(data->server, server) == TRUE &&
164 data->protocol == protocol) {
165 if (domain == NULL) {
166 if (data->domain == NULL)
171 if (g_str_equal(data->domain, domain) == TRUE)
180 static void send_response(int sk, unsigned char *buf, int len,
181 const struct sockaddr *to, socklen_t tolen,
184 struct domain_hdr *hdr;
185 int err, offset = protocol_offset(protocol);
195 hdr = (void*) (buf + offset);
197 DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
206 err = sendto(sk, buf, len, 0, to, tolen);
209 static gboolean request_timeout(gpointer user_data)
211 struct request_data *req = user_data;
213 DBG("id 0x%04x", req->srcid);
218 request_list = g_slist_remove(request_list, req);
221 if (req->resplen > 0 && req->resp != NULL) {
224 sk = g_io_channel_unix_get_fd(udp_listener_channel);
226 err = sendto(sk, req->resp, req->resplen, 0,
227 (struct sockaddr *) &req->sin, req->len);
228 } else if (req->request && req->numserv == 0) {
229 struct domain_hdr *hdr;
231 if (req->protocol == IPPROTO_TCP) {
232 hdr = (void *) (req->request + 2);
233 hdr->id = req->srcid;
234 send_response(req->client_sk, req->request,
235 req->request_len, NULL, 0, IPPROTO_TCP);
237 } else if (req->protocol == IPPROTO_UDP) {
240 hdr = (void *) (req->request);
241 hdr->id = req->srcid;
242 sk = g_io_channel_unix_get_fd(udp_listener_channel);
243 send_response(sk, req->request, req->request_len,
244 (struct sockaddr *)&req->sin,
245 sizeof(req->sin), 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)
315 sk = g_io_channel_unix_get_fd(server->channel);
317 err = send(sk, request, req->request_len, 0);
321 if (server->domain != NULL && server->protocol == IPPROTO_UDP) {
322 unsigned char alt[1024];
323 struct domain_hdr *hdr = (void *) &alt;
326 domlen = strlen(server->domain) + 1;
330 alt[0] = req->altid & 0xff;
331 alt[1] = req->altid >> 8;
333 memcpy(alt + 2, request + 2, 10);
334 hdr->qdcount = htons(1);
336 altlen = append_query(alt + 12, sizeof(alt) - 12,
337 name, server->domain);
343 memcpy(alt + altlen, request + altlen - domlen,
344 req->request_len - altlen + domlen);
346 err = send(sk, alt, req->request_len + domlen + 1, 0);
354 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol)
356 struct domain_hdr *hdr;
357 struct request_data *req;
358 int dns_id, sk, err, offset = protocol_offset(protocol);
363 hdr = (void *)(reply + offset);
364 dns_id = reply[offset] | reply[offset + 1] << 8;
366 DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
368 req = find_request(dns_id);
372 DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
374 reply[offset] = req->srcid & 0xff;
375 reply[offset + 1] = req->srcid >> 8;
379 if (hdr->rcode == 0 || req->resp == NULL) {
383 req->resp = g_try_malloc(reply_len);
384 if (req->resp == NULL)
387 memcpy(req->resp, reply, reply_len);
388 req->resplen = reply_len;
391 if (hdr->rcode > 0 && req->numresp < req->numserv)
394 if (req->timeout > 0)
395 g_source_remove(req->timeout);
397 request_list = g_slist_remove(request_list, req);
399 if (protocol == IPPROTO_UDP) {
400 sk = g_io_channel_unix_get_fd(udp_listener_channel);
401 err = sendto(sk, req->resp, req->resplen, 0,
402 (struct sockaddr *) &req->sin, req->len);
405 err = send(sk, req->resp, req->resplen, 0);
415 static void destroy_server(struct server_data *server)
417 DBG("interface %s server %s", server->interface, server->server);
419 server_list = g_slist_remove(server_list, server);
421 if (server->watch > 0)
422 g_source_remove(server->watch);
424 if (server->timeout > 0)
425 g_source_remove(server->timeout);
427 g_io_channel_unref(server->channel);
429 if (server->protocol == IPPROTO_UDP)
430 connman_info("Removing DNS server %s", server->server);
432 g_free(server->server);
433 g_free(server->domain);
434 g_free(server->interface);
438 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
441 struct server_data *data = user_data;
442 unsigned char buf[4096];
445 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
446 connman_error("Error with server channel");
451 sk = g_io_channel_unix_get_fd(channel);
453 len = recv(sk, buf, sizeof(buf), 0);
457 err = forward_dns_reply(buf, len, IPPROTO_UDP);
462 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
466 struct server_data *server = user_data;
468 sk = g_io_channel_unix_get_fd(channel);
472 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
475 DBG("TCP server channel closed");
477 for (list = request_list; list; list = list->next) {
478 struct request_data *req = list->data;
479 struct domain_hdr *hdr;
481 if (req->protocol == IPPROTO_UDP)
484 if (req->request == NULL)
488 * If we're not waiting for any further response
489 * from another name server, then we send an error
490 * response to the client.
492 if (req->numserv && --(req->numserv))
495 hdr = (void *) (req->request + 2);
496 hdr->id = req->srcid;
497 send_response(req->client_sk, req->request,
498 req->request_len, NULL, 0, IPPROTO_TCP);
500 request_list = g_slist_remove(request_list, req);
503 destroy_server(server);
508 if ((condition & G_IO_OUT) && !server->connected) {
511 server->connected = TRUE;
512 server_list = g_slist_append(server_list, server);
514 if (server->timeout > 0) {
515 g_source_remove(server->timeout);
519 for (list = request_list; list; list = list->next) {
520 struct request_data *req = list->data;
522 if (req->protocol == IPPROTO_UDP)
525 DBG("Sending req %s over TCP", (char *)req->name);
527 if (req->timeout > 0)
528 g_source_remove(req->timeout);
530 req->timeout = g_timeout_add_seconds(30,
531 request_timeout, req);
532 ns_resolv(server, req, req->request, req->name);
535 } else if (condition & G_IO_IN) {
536 int len, bytes_recv, total_bytes_recv;
537 unsigned char reply_len_buf[2];
539 unsigned char *reply;
541 len = recv(sk, reply_len_buf, 2, 0);
545 reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
547 DBG("TCP reply %d bytes", reply_len);
549 reply = g_try_malloc(reply_len + 2);
553 reply[0] = reply_len_buf[0];
554 reply[1] = reply_len_buf[1];
556 total_bytes_recv = bytes_recv = 0;
557 while (total_bytes_recv < reply_len) {
558 bytes_recv = recv(sk, reply + 2, reply_len, 0);
562 total_bytes_recv += bytes_recv;
565 forward_dns_reply(reply, reply_len + 2, IPPROTO_TCP);
569 destroy_server(server);
577 static gboolean tcp_idle_timeout(gpointer user_data)
579 struct server_data *server = user_data;
586 destroy_server(server);
591 static struct server_data *create_server(const char *interface,
592 const char *domain, const char *server,
595 struct server_data *data;
596 struct sockaddr_in sin;
599 DBG("interface %s server %s", interface, server);
614 data = find_server(interface, domain, server, protocol);
617 g_source_remove(data->watch);
618 data->watch = g_io_add_watch(data->channel,
619 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
620 tcp_server_event, data);
624 sk = socket(AF_INET, type, protocol);
626 connman_error("Failed to create server %s socket", server);
630 if (interface != NULL) {
631 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
632 interface, strlen(interface) + 1) < 0) {
633 connman_error("Failed to bind server %s "
641 data = g_try_new0(struct server_data, 1);
643 connman_error("Failed to allocate server %s data", server);
648 data->channel = g_io_channel_unix_new(sk);
649 if (data->channel == NULL) {
650 connman_error("Failed to create server %s channel", server);
656 g_io_channel_set_close_on_unref(data->channel, TRUE);
658 if (protocol == IPPROTO_TCP) {
659 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
660 data->watch = g_io_add_watch(data->channel,
661 G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
662 tcp_server_event, data);
663 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
666 data->watch = g_io_add_watch(data->channel, G_IO_IN,
667 udp_server_event, data);
669 data->interface = g_strdup(interface);
670 data->domain = g_strdup(domain);
671 data->server = g_strdup(server);
672 data->protocol = protocol;
674 memset(&sin, 0, sizeof(sin));
675 sin.sin_family = AF_INET;
676 sin.sin_port = htons(53);
677 sin.sin_addr.s_addr = inet_addr(server);
679 ret = connect(sk, (struct sockaddr *) &sin, sizeof(sin));
681 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
682 protocol == IPPROTO_UDP) {
683 connman_error("Failed to connect to server %s", server);
690 if (protocol == IPPROTO_UDP) {
691 /* Enable new servers by default */
692 data->enabled = TRUE;
693 connman_info("Adding DNS server %s", data->server);
695 server_list = g_slist_append(server_list, data);
703 static gboolean resolv(struct request_data *req,
704 gpointer request, gpointer name)
708 for (list = server_list; list; list = list->next) {
709 struct server_data *data = list->data;
711 DBG("server %s domain %s enabled %d",
712 data->server, data->domain, data->enabled);
714 if (data->enabled == FALSE)
717 if (ns_resolv(data, req, request, name) < 0)
724 static int dnsproxy_append(const char *interface, const char *domain,
727 struct server_data *data;
729 DBG("interface %s server %s", interface, server);
731 if (g_str_equal(server, "127.0.0.1") == TRUE)
734 data = create_server(interface, domain, server, IPPROTO_UDP);
741 static void remove_server(const char *interface, const char *domain,
742 const char *server, int protocol)
744 struct server_data *data;
746 data = find_server(interface, domain, server, protocol);
750 destroy_server(data);
753 static int dnsproxy_remove(const char *interface, const char *domain,
756 DBG("interface %s server %s", interface, server);
758 if (g_str_equal(server, "127.0.0.1") == TRUE)
761 remove_server(interface, domain, server, IPPROTO_UDP);
762 remove_server(interface, domain, server, IPPROTO_TCP);
767 static void dnsproxy_flush(void)
771 list = request_pending_list;
773 struct request_data *req = list->data;
777 request_pending_list =
778 g_slist_remove(request_pending_list, req);
779 resolv(req, req->request, req->name);
780 g_free(req->request);
785 static struct connman_resolver dnsproxy_resolver = {
787 .priority = CONNMAN_RESOLVER_PRIORITY_HIGH,
788 .append = dnsproxy_append,
789 .remove = dnsproxy_remove,
790 .flush = dnsproxy_flush,
793 static void dnsproxy_offline_mode(connman_bool_t enabled)
797 DBG("enabled %d", enabled);
799 for (list = server_list; list; list = list->next) {
800 struct server_data *data = list->data;
802 if (enabled == FALSE) {
803 connman_info("Enabling DNS server %s", data->server);
804 data->enabled = TRUE;
806 connman_info("Disabling DNS server %s", data->server);
807 data->enabled = FALSE;
812 static void dnsproxy_default_changed(struct connman_service *service)
817 DBG("service %p", service);
819 if (service == NULL) {
820 /* When no services are active, then disable DNS proxying */
821 dnsproxy_offline_mode(TRUE);
825 interface = connman_service_get_interface(service);
826 if (interface == NULL)
829 for (list = server_list; list; list = list->next) {
830 struct server_data *data = list->data;
832 if (g_strcmp0(data->interface, interface) == 0) {
833 connman_info("Enabling DNS server %s", data->server);
834 data->enabled = TRUE;
836 connman_info("Disabling DNS server %s", data->server);
837 data->enabled = FALSE;
844 static struct connman_notifier dnsproxy_notifier = {
846 .default_changed = dnsproxy_default_changed,
847 .offline_mode = dnsproxy_offline_mode,
850 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
852 static int parse_request(unsigned char *buf, int len,
853 char *name, unsigned int size)
855 struct domain_hdr *hdr = (void *) buf;
856 uint16_t qdcount = ntohs(hdr->qdcount);
857 uint16_t arcount = ntohs(hdr->arcount);
859 char *last_label = NULL;
860 unsigned int remain, used = 0;
865 DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
866 hdr->id, hdr->qr, hdr->opcode,
869 if (hdr->qr != 0 || qdcount != 1)
872 memset(name, 0, size);
874 ptr = buf + sizeof(struct domain_hdr);
875 remain = len - sizeof(struct domain_hdr);
881 last_label = (char *) (ptr + 1);
885 if (used + len + 1 > size)
888 strncat(name, (char *) (ptr + 1), len);
897 if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
898 !memcmp(last_label + 5, opt_edns0_type, 2)) {
899 uint16_t edns0_bufsize;
901 edns0_bufsize = last_label[7] << 8 | last_label[8];
903 DBG("EDNS0 buffer size %u", edns0_bufsize);
905 /* This is an evil hack until full TCP support has been
908 * Somtimes the EDNS0 request gets send with a too-small
909 * buffer size. Since glibc doesn't seem to crash when it
910 * gets a response biffer then it requested, just bump
911 * the buffer size up to 4KiB.
913 if (edns0_bufsize < 0x1000) {
914 last_label[7] = 0x10;
915 last_label[8] = 0x00;
919 DBG("query %s", name);
924 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
927 unsigned char buf[768];
929 struct request_data *req;
930 struct server_data *server;
931 int sk, client_sk, len, err;
932 struct sockaddr client_addr;
933 socklen_t client_addr_len;
936 DBG("condition 0x%x", condition);
938 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
939 if (tcp_listener_watch > 0)
940 g_source_remove(tcp_listener_watch);
941 tcp_listener_watch = 0;
943 connman_error("Error with TCP listener channel");
948 sk = g_io_channel_unix_get_fd(channel);
950 client_addr_len = sizeof(struct sockaddr);
951 client_sk = accept(sk, &client_addr, &client_addr_len);
953 connman_error("Accept failure on TCP listener");
954 tcp_listener_watch = 0;
958 len = recv(client_sk, buf, sizeof(buf), 0);
962 DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
964 err = parse_request(buf + 2, len - 2, query, sizeof(query));
965 if (err < 0 || (g_slist_length(server_list) == 0 &&
966 connman_ondemand_connected())) {
967 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
971 req = g_try_new0(struct request_data, 1);
975 memcpy(&req->sin, (struct sockaddr_in *)&client_addr, sizeof(req->sin));
976 req->client_sk = client_sk;
977 req->protocol = IPPROTO_TCP;
978 req->len = client_addr_len;
981 if (request_id == 0x0000 || request_id == 0xffff)
984 req->srcid = buf[2] | (buf[3] << 8);
985 req->dstid = request_id;
986 req->altid = request_id + 1;
987 req->request_len = len;
989 buf[2] = req->dstid & 0xff;
990 buf[3] = req->dstid >> 8;
993 request_list = g_slist_append(request_list, req);
995 for (list = server_list; list; list = list->next) {
996 struct server_data *data = list->data;
998 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
1001 server = create_server(data->interface, data->domain,
1002 data->server, IPPROTO_TCP);
1005 * If server is NULL, we're not connected yet.
1006 * Copy the relevant buffers and continue with
1007 * the next nameserver.
1008 * The request will actually be sent once we're
1009 * properly connected over TCP to this nameserver.
1011 if (server == NULL) {
1012 req->request = g_try_malloc0(req->request_len);
1013 if (req->request == NULL)
1016 memcpy(req->request, buf, req->request_len);
1018 req->name = g_try_malloc0(sizeof(query));
1019 if (req->name == NULL) {
1020 g_free(req->request);
1023 memcpy(req->name, query, sizeof(query));
1028 if (req->timeout > 0)
1029 g_source_remove(req->timeout);
1031 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
1032 ns_resolv(server, req, buf, query);
1038 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
1041 unsigned char buf[768];
1043 struct request_data *req;
1044 struct sockaddr_in sin;
1045 socklen_t size = sizeof(sin);
1048 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1049 connman_error("Error with UDP listener channel");
1050 udp_listener_watch = 0;
1054 sk = g_io_channel_unix_get_fd(channel);
1056 memset(&sin, 0, sizeof(sin));
1057 len = recvfrom(sk, buf, sizeof(buf), 0,
1058 (struct sockaddr *) &sin, &size);
1062 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
1064 err = parse_request(buf, len, query, sizeof(query));
1065 if (err < 0 || (g_slist_length(server_list) == 0 &&
1066 connman_ondemand_connected())) {
1067 send_response(sk, buf, len, (struct sockaddr *) &sin, size,
1072 req = g_try_new0(struct request_data, 1);
1076 memcpy(&req->sin, &sin, sizeof(sin));
1078 req->protocol = IPPROTO_UDP;
1082 if (request_id == 0x0000 || request_id == 0xffff)
1085 req->srcid = buf[0] | (buf[1] << 8);
1086 req->dstid = request_id;
1087 req->altid = request_id + 1;
1088 req->request_len = len;
1090 buf[0] = req->dstid & 0xff;
1091 buf[1] = req->dstid >> 8;
1093 if (!connman_ondemand_connected()) {
1094 DBG("Starting on demand connection");
1096 * We're not connected, let's queue the request and start
1097 * an on-demand connection.
1099 req->request = g_try_malloc0(req->request_len);
1100 if (req->request == NULL)
1103 memcpy(req->request, buf, req->request_len);
1105 req->name = g_try_malloc0(sizeof(query));
1106 if (req->name == NULL) {
1107 g_free(req->request);
1110 memcpy(req->name, query, sizeof(query));
1112 request_pending_list = g_slist_append(request_pending_list,
1115 connman_ondemand_start("", 300);
1122 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
1123 request_list = g_slist_append(request_list, req);
1125 return resolv(req, buf, query);
1128 static int create_dns_listener(int protocol)
1130 GIOChannel *channel;
1131 const char *ifname = "lo", *proto;
1132 struct sockaddr_in sin;
1152 sk = socket(AF_INET, type, protocol);
1154 connman_error("Failed to create %s listener socket", proto);
1158 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1159 ifname, strlen(ifname) + 1) < 0) {
1160 connman_error("Failed to bind %s listener interface", proto);
1165 memset(&sin, 0, sizeof(sin));
1166 sin.sin_family = AF_INET;
1167 sin.sin_port = htons(53);
1168 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
1169 sin.sin_addr.s_addr = htonl(INADDR_ANY);
1171 if (bind(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
1172 connman_error("Failed to bind %s listener socket", proto);
1177 if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
1178 connman_error("Failed to listen on TCP socket");
1183 channel = g_io_channel_unix_new(sk);
1184 if (channel == NULL) {
1185 connman_error("Failed to create %s listener channel", proto);
1190 g_io_channel_set_close_on_unref(channel, TRUE);
1192 if (protocol == IPPROTO_TCP) {
1193 tcp_listener_channel = channel;
1194 tcp_listener_watch = g_io_add_watch(channel,
1195 G_IO_IN, tcp_listener_event, NULL);
1197 udp_listener_channel = channel;
1198 udp_listener_watch = g_io_add_watch(channel,
1199 G_IO_IN, udp_listener_event, NULL);
1205 static void destroy_udp_listener(void)
1209 if (udp_listener_watch > 0)
1210 g_source_remove(udp_listener_watch);
1212 g_io_channel_unref(udp_listener_channel);
1215 static void destroy_tcp_listener(void)
1219 if (tcp_listener_watch > 0)
1220 g_source_remove(tcp_listener_watch);
1222 g_io_channel_unref(tcp_listener_channel);
1225 static int create_listener(void)
1229 err = create_dns_listener(IPPROTO_UDP);
1233 err = create_dns_listener(IPPROTO_TCP);
1235 destroy_udp_listener();
1239 connman_resolver_append("lo", NULL, "127.0.0.1");
1244 static void destroy_listener(void)
1248 connman_resolver_remove_all("lo");
1250 for (list = request_pending_list; list; list = list->next) {
1251 struct request_data *req = list->data;
1253 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
1254 req->srcid, req->dstid);
1257 g_free(req->request);
1263 g_slist_free(request_pending_list);
1264 request_pending_list = NULL;
1266 for (list = request_list; list; list = list->next) {
1267 struct request_data *req = list->data;
1269 DBG("Dropping request (id 0x%04x -> 0x%04x)",
1270 req->srcid, req->dstid);
1273 g_free(req->request);
1279 g_slist_free(request_list);
1280 request_list = NULL;
1282 destroy_tcp_listener();
1283 destroy_udp_listener();
1286 static int dnsproxy_init(void)
1290 err = create_listener();
1294 err = connman_resolver_register(&dnsproxy_resolver);
1298 err = connman_notifier_register(&dnsproxy_notifier);
1305 connman_resolver_unregister(&dnsproxy_resolver);
1313 static void dnsproxy_exit(void)
1315 connman_notifier_unregister(&dnsproxy_notifier);
1317 connman_resolver_unregister(&dnsproxy_resolver);
1322 CONNMAN_PLUGIN_DEFINE(dnsproxy, "DNS proxy resolver plugin", VERSION,
1323 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dnsproxy_init, dnsproxy_exit)