5 * Copyright (C) 2007-2009 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/log.h>
48 struct sockaddr_in sin;
53 static GSList *server_list = NULL;
54 static GSList *request_list = NULL;
56 static GIOChannel *listener_channel = NULL;
57 static guint listener_watch = 0;
59 static struct request_data *find_request(guint16 id)
63 for (list = request_list; list; list = list->next) {
64 struct request_data *data = list->data;
73 static struct server_data *find_server(const char *interface,
78 DBG("interface %s server %s", interface, server);
80 for (list = server_list; list; list = list->next) {
81 struct server_data *data = list->data;
83 if (data->interface == NULL || data->server == NULL)
86 if (g_str_equal(data->interface, interface) == TRUE &&
87 g_str_equal(data->server, server) == TRUE)
94 static gboolean server_event(GIOChannel *channel, GIOCondition condition,
97 struct request_data *req;
98 unsigned char buf[768];
101 sk = g_io_channel_unix_get_fd(channel);
103 len = recv(sk, buf, sizeof(buf), 0);
107 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
109 req = find_request(buf[0] | buf[1] << 8);
113 request_list = g_slist_remove(request_list, req);
115 sk = g_io_channel_unix_get_fd(listener_channel);
117 err = sendto(sk, buf, len, 0, (struct sockaddr *) &req->sin, req->len);
124 static struct server_data *create_server(const char *interface,
127 struct server_data *data;
128 struct sockaddr_in sin;
131 DBG("interface %s server %s", interface, server);
133 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
135 connman_error("Failed to create server %s socket", server);
139 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
140 interface, strlen(interface) + 1) < 0) {
141 connman_error("Failed to bind server %s to interface %s",
147 memset(&sin, 0, sizeof(sin));
148 sin.sin_family = AF_INET;
149 sin.sin_port = htons(53);
150 sin.sin_addr.s_addr = inet_addr(server);
152 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
153 connman_error("Failed to connect server %s", server);
158 data = g_try_new0(struct server_data, 1);
160 connman_error("Failed to allocate server %s data", server);
165 data->channel = g_io_channel_unix_new(sk);
166 if (data->channel == NULL) {
167 connman_error("Failed to create server %s channel", server);
173 g_io_channel_set_close_on_unref(data->channel, TRUE);
175 data->watch = g_io_add_watch(data->channel, G_IO_IN,
178 data->interface = g_strdup(interface);
179 data->server = g_strdup(server);
184 static void destroy_server(struct server_data *data)
186 DBG("interface %s server %s", data->interface, data->server);
189 g_source_remove(data->watch);
191 g_io_channel_unref(data->channel);
193 g_free(data->interface);
194 g_free(data->server);
198 static int dnsproxy_append(const char *interface, const char *domain,
201 struct server_data *data;
203 DBG("interface %s server %s", interface, server);
205 data = create_server(interface, server);
209 server_list = g_slist_append(server_list, data);
214 static int dnsproxy_remove(const char *interface, const char *domain,
217 struct server_data *data;
219 DBG("interface %s server %s", interface, server);
221 data = find_server(interface, server);
225 server_list = g_slist_remove(server_list, data);
227 destroy_server(data);
232 static struct connman_resolver dnsproxy_resolver = {
234 .priority = CONNMAN_RESOLVER_PRIORITY_HIGH,
235 .append = dnsproxy_append,
236 .remove = dnsproxy_remove,
239 #if __BYTE_ORDER == __LITTLE_ENDIAN
254 } __attribute__ ((packed));
255 #elif __BYTE_ORDER == __BIG_ENDIAN
270 } __attribute__ ((packed));
272 #error "Unknown byte order"
275 static void parse_request(unsigned char *buf, int len)
277 struct domain_hdr *hdr = (void *) buf;
278 uint16_t qdcount = ntohs(hdr->qdcount);
281 unsigned int remain, used = 0;
286 DBG("id 0x%04x qr %d opcode %d qdcount %d",
287 hdr->id, hdr->qr, hdr->opcode, qdcount);
289 if (hdr->qr != 0 || qdcount != 1)
292 memset(name, 0, sizeof(name));
303 if (used + len + 1 > sizeof(name))
306 strncat(name, (char *) (ptr + 1), len);
315 DBG("domain name %s", name);
318 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
322 unsigned char buf[768];
323 struct request_data *req;
324 struct sockaddr_in sin;
325 socklen_t size = sizeof(sin);
328 sk = g_io_channel_unix_get_fd(channel);
330 memset(&sin, 0, sizeof(sin));
331 len = recvfrom(sk, buf, sizeof(buf), 0,
332 (struct sockaddr *) &sin, &size);
336 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
338 parse_request(buf, len);
340 if (g_slist_length(server_list) == 0)
343 req = find_request(buf[0] | (buf[1] << 8));
345 req = g_try_new0(struct request_data, 1);
349 memcpy(&req->sin, &sin, sizeof(sin));
351 req->id = buf[0] | (buf[1] << 8);
353 request_list = g_slist_append(request_list, req);
355 memcpy(&req->sin, &sin, sizeof(sin));
359 for (list = server_list; list; list = list->next) {
360 struct server_data *data = list->data;
362 sk = g_io_channel_unix_get_fd(data->channel);
364 err = send(sk, buf, len, 0);
370 static int create_listener(void)
372 const char *ifname = "lo";
373 struct sockaddr_in sin;
378 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
380 connman_error("Failed to create listener socket");
384 //setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
385 //setsockopt(sk, SOL_IP, IP_PKTINFO, &opt, sizeof(opt));
387 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
388 ifname, strlen(ifname) + 1) < 0) {
389 connman_error("Failed to bind listener interface");
394 memset(&sin, 0, sizeof(sin));
395 sin.sin_family = AF_INET;
396 sin.sin_port = htons(53);
397 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
398 //sin.sin_addr.s_addr = INADDR_ANY;
400 if (bind(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
401 connman_error("Failed to bind listener socket");
406 listener_channel = g_io_channel_unix_new(sk);
407 if (listener_channel == NULL) {
408 connman_error("Failed to create listener channel");
413 g_io_channel_set_close_on_unref(listener_channel, TRUE);
415 listener_watch = g_io_add_watch(listener_channel, G_IO_IN,
416 listener_event, NULL);
421 static void destroy_listener(void)
427 if (listener_watch > 0)
428 g_source_remove(listener_watch);
430 for (list = request_list; list; list = list->next) {
431 struct request_data *data = list->data;
433 DBG("Dropping request (id 0x%04x)", data->id);
439 g_slist_free(request_list);
442 g_io_channel_unref(listener_channel);
445 static int dnsproxy_init(void)
449 err = create_listener();
453 err = connman_resolver_register(&dnsproxy_resolver);
460 static void dnsproxy_exit(void)
464 connman_resolver_unregister(&dnsproxy_resolver);
467 CONNMAN_PLUGIN_DEFINE(dnsproxy, "DNS proxy resolver plugin", VERSION,
468 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dnsproxy_init, dnsproxy_exit)