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
29 #include <arpa/inet.h>
30 #include <netinet/in.h>
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/plugin.h>
34 #include <connman/resolver.h>
35 #include <connman/log.h>
47 struct sockaddr_in sin;
52 static GSList *server_list = NULL;
53 static GSList *request_list = NULL;
55 static GIOChannel *listener_channel = NULL;
56 static guint listener_watch = 0;
58 static struct request_data *find_request(guint16 id)
62 for (list = request_list; list; list = list->next) {
63 struct request_data *data = list->data;
72 static struct server_data *find_server(const char *interface,
77 DBG("interface %s server %s", interface, server);
79 for (list = server_list; list; list = list->next) {
80 struct server_data *data = list->data;
82 if (data->interface == NULL || data->server == NULL)
85 if (g_str_equal(data->interface, interface) == TRUE &&
86 g_str_equal(data->server, server) == TRUE)
93 static gboolean server_event(GIOChannel *channel, GIOCondition condition,
96 struct request_data *req;
97 unsigned char buf[768];
100 sk = g_io_channel_unix_get_fd(channel);
102 len = recv(sk, buf, sizeof(buf), 0);
106 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
108 req = find_request(buf[0] | buf[1] << 8);
112 request_list = g_slist_remove(request_list, req);
114 sk = g_io_channel_unix_get_fd(listener_channel);
116 err = sendto(sk, buf, len, 0, (struct sockaddr *) &req->sin, req->len);
123 static struct server_data *create_server(const char *interface,
126 struct server_data *data;
127 struct sockaddr_in sin;
130 DBG("interface %s server %s", interface, server);
132 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
134 connman_error("Failed to create server %s socket", server);
138 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
139 interface, strlen(interface) + 1) < 0) {
140 connman_error("Failed to bind server %s to interface %s",
146 memset(&sin, 0, sizeof(sin));
147 sin.sin_family = AF_INET;
148 sin.sin_port = htons(53);
149 sin.sin_addr.s_addr = inet_addr(server);
151 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
152 connman_error("Failed to connect server %s", server);
157 data = g_try_new0(struct server_data, 1);
159 connman_error("Failed to allocate server %s data", server);
164 data->channel = g_io_channel_unix_new(sk);
165 if (data->channel == NULL) {
166 connman_error("Failed to create server %s channel", server);
172 g_io_channel_set_close_on_unref(data->channel, TRUE);
174 data->watch = g_io_add_watch(data->channel, G_IO_IN,
177 data->interface = g_strdup(interface);
178 data->server = g_strdup(server);
183 static void destroy_server(struct server_data *data)
185 DBG("interface %s server %s", data->interface, data->server);
188 g_source_remove(data->watch);
190 g_io_channel_unref(data->channel);
192 g_free(data->interface);
193 g_free(data->server);
197 static int dnsproxy_append(const char *interface, const char *domain,
200 struct server_data *data;
202 DBG("interface %s server %s", interface, server);
204 data = create_server(interface, server);
208 server_list = g_slist_append(server_list, data);
213 static int dnsproxy_remove(const char *interface, const char *domain,
216 struct server_data *data;
218 DBG("interface %s server %s", interface, server);
220 data = find_server(interface, server);
224 server_list = g_slist_remove(server_list, data);
226 destroy_server(data);
231 static struct connman_resolver dnsproxy_resolver = {
233 .priority = CONNMAN_RESOLVER_PRIORITY_HIGH,
234 .append = dnsproxy_append,
235 .remove = dnsproxy_remove,
238 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
242 unsigned char buf[768];
243 struct request_data *req;
244 struct sockaddr_in sin;
245 socklen_t size = sizeof(sin);
248 sk = g_io_channel_unix_get_fd(channel);
250 memset(&sin, 0, sizeof(sin));
251 len = recvfrom(sk, buf, sizeof(buf), 0,
252 (struct sockaddr *) &sin, &size);
256 DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
258 if (g_slist_length(server_list) == 0)
261 req = find_request(buf[0] | (buf[1] << 8));
263 req = g_try_new0(struct request_data, 1);
267 memcpy(&req->sin, &sin, sizeof(sin));
269 req->id = buf[0] | (buf[1] << 8);
271 request_list = g_slist_append(request_list, req);
273 memcpy(&req->sin, &sin, sizeof(sin));
277 for (list = server_list; list; list = list->next) {
278 struct server_data *data = list->data;
280 sk = g_io_channel_unix_get_fd(data->channel);
282 err = send(sk, buf, len, 0);
288 static int create_listener(void)
290 const char *ifname = "lo";
291 struct sockaddr_in sin;
296 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
298 connman_error("Failed to create listener socket");
302 //setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
303 //setsockopt(sk, SOL_IP, IP_PKTINFO, &opt, sizeof(opt));
305 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
306 ifname, strlen(ifname) + 1) < 0) {
307 connman_error("Failed to bind listener interface");
312 memset(&sin, 0, sizeof(sin));
313 sin.sin_family = AF_INET;
314 sin.sin_port = htons(53);
315 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
316 //sin.sin_addr.s_addr = INADDR_ANY;
318 if (bind(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
319 connman_error("Failed to bind listener socket");
324 listener_channel = g_io_channel_unix_new(sk);
325 if (listener_channel == NULL) {
326 connman_error("Failed to create listener channel");
331 g_io_channel_set_close_on_unref(listener_channel, TRUE);
333 listener_watch = g_io_add_watch(listener_channel, G_IO_IN,
334 listener_event, NULL);
339 static void destroy_listener(void)
345 if (listener_watch > 0)
346 g_source_remove(listener_watch);
348 for (list = request_list; list; list = list->next) {
349 struct request_data *data = list->data;
351 DBG("Dropping request (id 0x%04x)", data->id);
357 g_slist_free(request_list);
360 g_io_channel_unref(listener_channel);
363 static int dnsproxy_init(void)
367 err = create_listener();
371 err = connman_resolver_register(&dnsproxy_resolver);
378 static void dnsproxy_exit(void)
382 connman_resolver_unregister(&dnsproxy_resolver);
385 CONNMAN_PLUGIN_DEFINE(dnsproxy, "DNS proxy resolver plugin", VERSION,
386 dnsproxy_init, dnsproxy_exit)