core: Add SOCK_CLOEXEC to socket()
[framework/connectivity/connman.git] / src / dnsproxy.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netdb.h>
35
36 #include <glib.h>
37
38 #include "connman.h"
39
40 #if __BYTE_ORDER == __LITTLE_ENDIAN
41 struct domain_hdr {
42         uint16_t id;
43         uint8_t rd:1;
44         uint8_t tc:1;
45         uint8_t aa:1;
46         uint8_t opcode:4;
47         uint8_t qr:1;
48         uint8_t rcode:4;
49         uint8_t z:3;
50         uint8_t ra:1;
51         uint16_t qdcount;
52         uint16_t ancount;
53         uint16_t nscount;
54         uint16_t arcount;
55 } __attribute__ ((packed));
56 #elif __BYTE_ORDER == __BIG_ENDIAN
57 struct domain_hdr {
58         uint16_t id;
59         uint8_t qr:1;
60         uint8_t opcode:4;
61         uint8_t aa:1;
62         uint8_t tc:1;
63         uint8_t rd:1;
64         uint8_t ra:1;
65         uint8_t z:3;
66         uint8_t rcode:4;
67         uint16_t qdcount;
68         uint16_t ancount;
69         uint16_t nscount;
70         uint16_t arcount;
71 } __attribute__ ((packed));
72 #else
73 #error "Unknown byte order"
74 #endif
75
76 struct partial_reply {
77         uint16_t len;
78         uint16_t received;
79         unsigned char buf[];
80 };
81
82 struct server_data {
83         char *interface;
84         GList *domains;
85         char *server;
86         int protocol;
87         GIOChannel *channel;
88         guint watch;
89         guint timeout;
90         gboolean enabled;
91         gboolean connected;
92         struct partial_reply *incoming_reply;
93 };
94
95 struct request_data {
96         union {
97                 struct sockaddr_in6 __sin6; /* Only for the length */
98                 struct sockaddr sa;
99         };
100         socklen_t sa_len;
101         int client_sk;
102         int protocol;
103         guint16 srcid;
104         guint16 dstid;
105         guint16 altid;
106         guint timeout;
107         guint watch;
108         guint numserv;
109         guint numresp;
110         gpointer request;
111         gsize request_len;
112         gpointer name;
113         gpointer resp;
114         gsize resplen;
115         struct listener_data *ifdata;
116         gboolean append_domain;
117 };
118
119 struct listener_data {
120         char *ifname;
121         GIOChannel *udp_listener_channel;
122         guint udp_listener_watch;
123         GIOChannel *tcp_listener_channel;
124         guint tcp_listener_watch;
125 };
126
127 static GSList *server_list = NULL;
128 static GSList *request_list = NULL;
129 static GSList *request_pending_list = NULL;
130 static guint16 request_id = 0x0000;
131 static GHashTable *listener_table = NULL;
132
133 static int protocol_offset(int protocol)
134 {
135         switch (protocol) {
136         case IPPROTO_UDP:
137                 return 0;
138
139         case IPPROTO_TCP:
140                 return 2;
141
142         default:
143                 return -EINVAL;
144         }
145
146 }
147
148 static struct request_data *find_request(guint16 id)
149 {
150         GSList *list;
151
152         for (list = request_list; list; list = list->next) {
153                 struct request_data *req = list->data;
154
155                 if (req->dstid == id || req->altid == id)
156                         return req;
157         }
158
159         return NULL;
160 }
161
162 static struct server_data *find_server(const char *interface,
163                                         const char *server,
164                                                 int protocol)
165 {
166         GSList *list;
167
168         DBG("interface %s server %s", interface, server);
169
170         for (list = server_list; list; list = list->next) {
171                 struct server_data *data = list->data;
172
173                 if (interface == NULL && data->interface == NULL &&
174                                 g_str_equal(data->server, server) == TRUE &&
175                                 data->protocol == protocol)
176                         return data;
177
178                 if (interface == NULL ||
179                                 data->interface == NULL || data->server == NULL)
180                         continue;
181
182                 if (g_str_equal(data->interface, interface) == TRUE &&
183                                 g_str_equal(data->server, server) == TRUE &&
184                                 data->protocol == protocol)
185                         return data;
186         }
187
188         return NULL;
189 }
190
191
192 static void send_response(int sk, unsigned char *buf, int len,
193                                 const struct sockaddr *to, socklen_t tolen,
194                                 int protocol)
195 {
196         struct domain_hdr *hdr;
197         int err, offset = protocol_offset(protocol);
198
199         DBG("");
200
201         if (offset < 0)
202                 return;
203
204         if (len < 12)
205                 return;
206
207         hdr = (void *) (buf + offset);
208
209         DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
210
211         hdr->qr = 1;
212         hdr->rcode = 2;
213
214         hdr->ancount = 0;
215         hdr->nscount = 0;
216         hdr->arcount = 0;
217
218         err = sendto(sk, buf, len, 0, to, tolen);
219         if (err < 0) {
220                 connman_error("Failed to send DNS response: %s",
221                                 strerror(errno));
222                 return;
223         }
224 }
225
226 static gboolean request_timeout(gpointer user_data)
227 {
228         struct request_data *req = user_data;
229         struct listener_data *ifdata;
230
231         DBG("id 0x%04x", req->srcid);
232
233         if (req == NULL)
234                 return FALSE;
235
236         ifdata = req->ifdata;
237
238         request_list = g_slist_remove(request_list, req);
239         req->numserv--;
240
241         if (req->resplen > 0 && req->resp != NULL) {
242                 int sk, err;
243
244                 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
245
246                 err = sendto(sk, req->resp, req->resplen, 0,
247                                                 &req->sa, req->sa_len);
248                 if (err < 0)
249                         return FALSE;
250         } else if (req->request && req->numserv == 0) {
251                 struct domain_hdr *hdr;
252
253                 if (req->protocol == IPPROTO_TCP) {
254                         hdr = (void *) (req->request + 2);
255                         hdr->id = req->srcid;
256                         send_response(req->client_sk, req->request,
257                                 req->request_len, NULL, 0, IPPROTO_TCP);
258
259                 } else if (req->protocol == IPPROTO_UDP) {
260                         int sk;
261
262                         hdr = (void *) (req->request);
263                         hdr->id = req->srcid;
264                         sk = g_io_channel_unix_get_fd(
265                                                 ifdata->udp_listener_channel);
266                         send_response(sk, req->request, req->request_len,
267                                         &req->sa, req->sa_len, IPPROTO_UDP);
268                 }
269         }
270
271         g_free(req->resp);
272         g_free(req);
273
274         return FALSE;
275 }
276
277 static int append_query(unsigned char *buf, unsigned int size,
278                                 const char *query, const char *domain)
279 {
280         unsigned char *ptr = buf;
281         char *offset;
282
283         DBG("query %s domain %s", query, domain);
284
285         offset = (char *) query;
286         while (offset != NULL) {
287                 char *tmp;
288
289                 tmp = strchr(offset, '.');
290                 if (tmp == NULL) {
291                         if (strlen(offset) == 0)
292                                 break;
293                         *ptr = strlen(offset);
294                         memcpy(ptr + 1, offset, strlen(offset));
295                         ptr += strlen(offset) + 1;
296                         break;
297                 }
298
299                 *ptr = tmp - offset;
300                 memcpy(ptr + 1, offset, tmp - offset);
301                 ptr += tmp - offset + 1;
302
303                 offset = tmp + 1;
304         }
305
306         offset = (char *) domain;
307         while (offset != NULL) {
308                 char *tmp;
309
310                 tmp = strchr(offset, '.');
311                 if (tmp == NULL) {
312                         if (strlen(offset) == 0)
313                                 break;
314                         *ptr = strlen(offset);
315                         memcpy(ptr + 1, offset, strlen(offset));
316                         ptr += strlen(offset) + 1;
317                         break;
318                 }
319
320                 *ptr = tmp - offset;
321                 memcpy(ptr + 1, offset, tmp - offset);
322                 ptr += tmp - offset + 1;
323
324                 offset = tmp + 1;
325         }
326
327         *ptr++ = 0x00;
328
329         return ptr - buf;
330 }
331
332 static int ns_resolv(struct server_data *server, struct request_data *req,
333                                 gpointer request, gpointer name)
334 {
335         GList *list;
336         int sk, err;
337         char *dot, *lookup = (char *) name;
338
339         sk = g_io_channel_unix_get_fd(server->channel);
340
341         err = send(sk, request, req->request_len, 0);
342
343         req->numserv++;
344
345         /* If we have more than one dot, we don't add domains */
346         dot = strchr(lookup, '.');
347         if (dot != NULL && dot != lookup + strlen(lookup) - 1)
348                 return 0;
349
350         if (server->domains != NULL && server->domains->data != NULL)
351                 req->append_domain = TRUE;
352
353         for (list = server->domains; list; list = list->next) {
354                 char *domain;
355                 unsigned char alt[1024];
356                 struct domain_hdr *hdr = (void *) &alt;
357                 int altlen, domlen, offset;
358
359                 domain = list->data;
360
361                 if (domain == NULL)
362                         continue;
363
364                 offset = protocol_offset(server->protocol);
365                 if (offset < 0)
366                         return offset;
367
368                 domlen = strlen(domain) + 1;
369                 if (domlen < 5)
370                         return -EINVAL;
371
372                 alt[offset] = req->altid & 0xff;
373                 alt[offset + 1] = req->altid >> 8;
374
375                 memcpy(alt + offset + 2, request + offset + 2, 10);
376                 hdr->qdcount = htons(1);
377
378                 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
379                                         name, domain);
380                 if (altlen < 0)
381                         return -EINVAL;
382
383                 altlen += 12;
384
385                 memcpy(alt + offset + altlen,
386                         request + offset + altlen - domlen,
387                                 req->request_len - altlen + domlen);
388
389                 if (server->protocol == IPPROTO_TCP) {
390                         int req_len = req->request_len + domlen - 1;
391
392                         alt[0] = (req_len >> 8) & 0xff;
393                         alt[1] = req_len & 0xff;
394                 }
395
396                 err = send(sk, alt, req->request_len + domlen + 1, 0);
397                 if (err < 0)
398                         return -EIO;
399
400                 req->numserv++;
401         }
402
403         return 0;
404 }
405
406 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol)
407 {
408         struct domain_hdr *hdr;
409         struct request_data *req;
410         int dns_id, sk, err, offset = protocol_offset(protocol);
411         struct listener_data *ifdata;
412
413         if (offset < 0)
414                 return offset;
415
416         hdr = (void *)(reply + offset);
417         dns_id = reply[offset] | reply[offset + 1] << 8;
418
419         DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
420
421         req = find_request(dns_id);
422         if (req == NULL)
423                 return -EINVAL;
424
425         DBG("id 0x%04x rcode %d", hdr->id, hdr->rcode);
426
427         ifdata = req->ifdata;
428
429         reply[offset] = req->srcid & 0xff;
430         reply[offset + 1] = req->srcid >> 8;
431
432         req->numresp++;
433
434         if (hdr->rcode == 0 || req->resp == NULL) {
435
436                 /*
437                  * If the domain name was append
438                  * remove it before forwarding the reply.
439                  */
440                 if (req->append_domain == TRUE) {
441                         unsigned char *ptr;
442                         uint8_t host_len;
443                         unsigned int domain_len;
444
445                         /*
446                          * ptr points to the first char of the hostname.
447                          * ->hostname.domain.net
448                          */
449                         ptr = reply + offset + sizeof(struct domain_hdr);
450                         host_len = *ptr;
451                         domain_len = strlen((const char *)ptr) - host_len - 1;
452
453                         /*
454                          * remove the domain name and replaced it by the end
455                          * of reply.
456                          */
457                         memmove(ptr + host_len + 1,
458                                 ptr + host_len + domain_len + 1,
459                                 reply_len - (ptr - reply + domain_len));
460
461                         reply_len = reply_len - domain_len;
462                 }
463
464                 g_free(req->resp);
465                 req->resplen = 0;
466
467                 req->resp = g_try_malloc(reply_len);
468                 if (req->resp == NULL)
469                         return -ENOMEM;
470
471                 memcpy(req->resp, reply, reply_len);
472                 req->resplen = reply_len;
473         }
474
475         if (hdr->rcode > 0 && req->numresp < req->numserv)
476                 return -EINVAL;
477
478         if (req->timeout > 0)
479                 g_source_remove(req->timeout);
480
481         request_list = g_slist_remove(request_list, req);
482
483         if (protocol == IPPROTO_UDP) {
484                 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
485                 err = sendto(sk, req->resp, req->resplen, 0,
486                              &req->sa, req->sa_len);
487         } else {
488                 sk = req->client_sk;
489                 err = send(sk, req->resp, req->resplen, 0);
490                 close(sk);
491         }
492
493         g_free(req->resp);
494         g_free(req);
495
496         return err;
497 }
498
499 static void destroy_server(struct server_data *server)
500 {
501         GList *list;
502
503         DBG("interface %s server %s", server->interface, server->server);
504
505         server_list = g_slist_remove(server_list, server);
506
507         if (server->watch > 0)
508                 g_source_remove(server->watch);
509
510         if (server->timeout > 0)
511                 g_source_remove(server->timeout);
512
513         g_io_channel_unref(server->channel);
514
515         if (server->protocol == IPPROTO_UDP)
516                 connman_info("Removing DNS server %s", server->server);
517
518         g_free(server->incoming_reply);
519         g_free(server->server);
520         for (list = server->domains; list; list = list->next) {
521                 char *domain = list->data;
522
523                 server->domains = g_list_remove(server->domains, domain);
524                 g_free(domain);
525         }
526         g_free(server->interface);
527         g_free(server);
528 }
529
530 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
531                                                         gpointer user_data)
532 {
533         unsigned char buf[4096];
534         int sk, err, len;
535
536         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
537                 struct server_data *data = user_data;
538
539                 connman_error("Error with UDP server %s", data->server);
540                 data->watch = 0;
541                 return FALSE;
542         }
543
544         sk = g_io_channel_unix_get_fd(channel);
545
546         len = recv(sk, buf, sizeof(buf), 0);
547         if (len < 12)
548                 return TRUE;
549
550         err = forward_dns_reply(buf, len, IPPROTO_UDP);
551         if (err < 0)
552                 return TRUE;
553
554         return TRUE;
555 }
556
557 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
558                                                         gpointer user_data)
559 {
560         int sk;
561         struct server_data *server = user_data;
562
563         sk = g_io_channel_unix_get_fd(channel);
564         if (sk == 0)
565                 return FALSE;
566
567         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
568                 GSList *list;
569 hangup:
570                 DBG("TCP server channel closed");
571
572                 /*
573                  * Discard any partial response which is buffered; better
574                  * to get a proper response from a working server.
575                  */
576                 g_free(server->incoming_reply);
577                 server->incoming_reply = NULL;
578
579                 for (list = request_list; list; list = list->next) {
580                         struct request_data *req = list->data;
581                         struct domain_hdr *hdr;
582
583                         if (req->protocol == IPPROTO_UDP)
584                                 continue;
585
586                         if (req->request == NULL)
587                                 continue;
588
589                         /*
590                          * If we're not waiting for any further response
591                          * from another name server, then we send an error
592                          * response to the client.
593                          */
594                         if (req->numserv && --(req->numserv))
595                                 continue;
596
597                         hdr = (void *) (req->request + 2);
598                         hdr->id = req->srcid;
599                         send_response(req->client_sk, req->request,
600                                 req->request_len, NULL, 0, IPPROTO_TCP);
601
602                         request_list = g_slist_remove(request_list, req);
603                 }
604
605                 destroy_server(server);
606
607                 return FALSE;
608         }
609
610         if ((condition & G_IO_OUT) && !server->connected) {
611                 GSList *list;
612                 GList *domains;
613                 struct server_data *udp_server;
614
615                 udp_server = find_server(server->interface, server->server,
616                                                                 IPPROTO_UDP);
617                 if (udp_server != NULL) {
618                         for (domains = udp_server->domains; domains;
619                                                 domains = domains->next) {
620                                 char *dom = domains->data;
621
622                                 DBG("Adding domain %s to %s",
623                                                 dom, server->server);
624
625                                 server->domains = g_list_append(server->domains,
626                                                                 g_strdup(dom));
627                         }
628                 }
629
630                 server->connected = TRUE;
631                 server_list = g_slist_append(server_list, server);
632
633                 if (server->timeout > 0) {
634                         g_source_remove(server->timeout);
635                         server->timeout = 0;
636                 }
637
638                 for (list = request_list; list; list = list->next) {
639                         struct request_data *req = list->data;
640
641                         if (req->protocol == IPPROTO_UDP)
642                                 continue;
643
644                         DBG("Sending req %s over TCP", (char *)req->name);
645
646                         if (req->timeout > 0)
647                                 g_source_remove(req->timeout);
648
649                         req->timeout = g_timeout_add_seconds(30,
650                                                 request_timeout, req);
651                         ns_resolv(server, req, req->request, req->name);
652                 }
653
654         } else if (condition & G_IO_IN) {
655                 struct partial_reply *reply = server->incoming_reply;
656                 int bytes_recv;
657
658                 if (!reply) {
659                         unsigned char reply_len_buf[2];
660                         uint16_t reply_len;
661
662                         bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
663                         if (!bytes_recv) {
664                                 goto hangup;
665                         } else if (bytes_recv < 0) {
666                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
667                                         return TRUE;
668
669                                 connman_error("DNS proxy error %s",
670                                                 strerror(errno));
671                                 goto hangup;
672                         } else if (bytes_recv < 2)
673                                 return TRUE;
674
675                         reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
676                         reply_len += 2;
677
678                         DBG("TCP reply %d bytes", reply_len);
679
680                         reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
681                         if (!reply)
682                                 return TRUE;
683
684                         reply->len = reply_len;
685                         reply->received = 0;
686
687                         server->incoming_reply = reply;
688                 }
689
690                 while (reply->received < reply->len) {
691                         bytes_recv = recv(sk, reply->buf + reply->received,
692                                         reply->len - reply->received, 0);
693                         if (!bytes_recv) {
694                                 connman_error("DNS proxy TCP disconnect");
695                                 break;
696                         } else if (bytes_recv < 0) {
697                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
698                                         return TRUE;
699
700                                 connman_error("DNS proxy error %s",
701                                                 strerror(errno));
702                                 break;
703                         }
704                         reply->received += bytes_recv;
705                 }
706
707                 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP);
708
709                 g_free(reply);
710                 server->incoming_reply = NULL;
711
712                 destroy_server(server);
713
714                 return FALSE;
715         }
716
717         return TRUE;
718 }
719
720 static gboolean tcp_idle_timeout(gpointer user_data)
721 {
722         struct server_data *server = user_data;
723
724         DBG("");
725
726         if (server == NULL)
727                 return FALSE;
728
729         destroy_server(server);
730
731         return FALSE;
732 }
733
734 static struct server_data *create_server(const char *interface,
735                                         const char *domain, const char *server,
736                                         int protocol)
737 {
738         struct addrinfo hints, *rp;
739         struct server_data *data;
740         int sk, ret;
741
742         DBG("interface %s server %s", interface, server);
743
744         memset(&hints, 0, sizeof(hints));
745
746         switch (protocol) {
747         case IPPROTO_UDP:
748                 hints.ai_socktype = SOCK_DGRAM;
749                 break;
750
751         case IPPROTO_TCP:
752                 hints.ai_socktype = SOCK_STREAM;
753                 break;
754
755         default:
756                 return NULL;
757         }
758         hints.ai_family = AF_UNSPEC;
759         hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
760
761         ret = getaddrinfo(server, "53", &hints, &rp);
762         if (ret) {
763                 connman_error("Failed to parse server %s address: %s\n",
764                               server, gai_strerror(ret));
765                 return NULL;
766         }
767         /* Do not blindly copy this code elsewhere; it doesn't loop over the
768            results using ->ai_next as it should. That's OK in *this* case
769            because it was a numeric lookup; we *know* there's only one. */
770
771         sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
772         if (sk < 0) {
773                 connman_error("Failed to create server %s socket", server);
774                 freeaddrinfo(rp);
775                 return NULL;
776         }
777
778         if (interface != NULL) {
779                 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
780                                 interface, strlen(interface) + 1) < 0) {
781                         connman_error("Failed to bind server %s "
782                                                 "to interface %s",
783                                                         server, interface);
784                         freeaddrinfo(rp);
785                         close(sk);
786                         return NULL;
787                 }
788         }
789
790         data = g_try_new0(struct server_data, 1);
791         if (data == NULL) {
792                 connman_error("Failed to allocate server %s data", server);
793                 freeaddrinfo(rp);
794                 close(sk);
795                 return NULL;
796         }
797
798         data->channel = g_io_channel_unix_new(sk);
799         if (data->channel == NULL) {
800                 connman_error("Failed to create server %s channel", server);
801                 freeaddrinfo(rp);
802                 close(sk);
803                 g_free(data);
804                 return NULL;
805         }
806
807         g_io_channel_set_close_on_unref(data->channel, TRUE);
808
809         if (protocol == IPPROTO_TCP) {
810                 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
811                 data->watch = g_io_add_watch(data->channel,
812                         G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
813                                                 tcp_server_event, data);
814                 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
815                                                                 data);
816         } else
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);
820
821         data->interface = g_strdup(interface);
822         if (domain)
823                 data->domains = g_list_append(data->domains, g_strdup(domain));
824         data->server = g_strdup(server);
825         data->protocol = protocol;
826
827         ret = connect(sk, rp->ai_addr, rp->ai_addrlen);
828         freeaddrinfo(rp);
829         if (ret < 0) {
830                 if ((protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
831                                 protocol == IPPROTO_UDP) {
832                         GList *list;
833
834                         connman_error("Failed to connect to server %s", server);
835                         if (data->watch > 0)
836                                 g_source_remove(data->watch);
837                         if (data->timeout > 0)
838                                 g_source_remove(data->timeout);
839
840                         g_io_channel_unref(data->channel);
841                         close(sk);
842
843                         g_free(data->server);
844                         g_free(data->interface);
845                         for (list = data->domains; list; list = list->next) {
846                                 char *domain = list->data;
847
848                                 data->domains = g_list_remove(data->domains,
849                                                                         domain);
850                                 g_free(domain);
851                         }
852                         g_free(data);
853                         return NULL;
854                 }
855         }
856
857         if (protocol == IPPROTO_UDP) {
858                 /* Enable new servers by default */
859                 data->enabled = TRUE;
860                 connman_info("Adding DNS server %s", data->server);
861
862                 server_list = g_slist_append(server_list, data);
863
864                 return data;
865         }
866
867         return NULL;
868 }
869
870 static gboolean resolv(struct request_data *req,
871                                 gpointer request, gpointer name)
872 {
873         GSList *list;
874
875         for (list = server_list; list; list = list->next) {
876                 struct server_data *data = list->data;
877
878                 DBG("server %s enabled %d", data->server, data->enabled);
879
880                 if (data->enabled == FALSE)
881                         continue;
882
883                 if (data->watch == 0 && data->protocol == IPPROTO_UDP)
884                         data->watch = g_io_add_watch(data->channel,
885                                 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
886                                                 udp_server_event, data);
887
888                 if (ns_resolv(data, req, request, name) < 0)
889                         continue;
890         }
891
892         return TRUE;
893 }
894
895 static void append_domain(const char *interface, const char *domain)
896 {
897         GSList *list;
898
899         DBG("interface %s domain %s", interface, domain);
900
901         if (domain == NULL)
902                 return;
903
904         for (list = server_list; list; list = list->next) {
905                 struct server_data *data = list->data;
906                 GList *dom_list;
907                 char *dom;
908                 gboolean dom_found = FALSE;
909
910                 if (data->interface == NULL)
911                         continue;
912
913                 if (g_str_equal(data->interface, interface) == FALSE)
914                         continue;
915
916                 for (dom_list = data->domains; dom_list;
917                                 dom_list = dom_list->next) {
918                         dom = dom_list->data;
919
920                         if (g_str_equal(dom, domain)) {
921                                 dom_found = TRUE;
922                                 break;
923                         }
924                 }
925
926                 if (dom_found == FALSE) {
927                         data->domains =
928                                 g_list_append(data->domains, g_strdup(domain));
929                 }
930         }
931 }
932
933 int __connman_dnsproxy_append(const char *interface, const char *domain,
934                                                         const char *server)
935 {
936         struct server_data *data;
937
938         DBG("interface %s server %s", interface, server);
939
940         if (server == NULL && domain == NULL)
941                 return -EINVAL;
942
943         if (server == NULL) {
944                 append_domain(interface, domain);
945
946                 return 0;
947         }
948
949         if (g_str_equal(server, "127.0.0.1") == TRUE)
950                 return -ENODEV;
951
952         data = find_server(interface, server, IPPROTO_UDP);
953         if (data != NULL) {
954                 append_domain(interface, domain);
955                 return 0;
956         }
957
958         data = create_server(interface, domain, server, IPPROTO_UDP);
959         if (data == NULL)
960                 return -EIO;
961
962         return 0;
963 }
964
965 static void remove_server(const char *interface, const char *domain,
966                         const char *server, int protocol)
967 {
968         struct server_data *data;
969
970         data = find_server(interface, server, protocol);
971         if (data == NULL)
972                 return;
973
974         destroy_server(data);
975 }
976
977 int __connman_dnsproxy_remove(const char *interface, const char *domain,
978                                                         const char *server)
979 {
980         DBG("interface %s server %s", interface, server);
981
982         if (server == NULL)
983                 return -EINVAL;
984
985         if (g_str_equal(server, "127.0.0.1") == TRUE)
986                 return -ENODEV;
987
988         remove_server(interface, domain, server, IPPROTO_UDP);
989         remove_server(interface, domain, server, IPPROTO_TCP);
990
991         return 0;
992 }
993
994 void __connman_dnsproxy_flush(void)
995 {
996         GSList *list;
997
998         list = request_pending_list;
999         while (list) {
1000                 struct request_data *req = list->data;
1001
1002                 list = list->next;
1003
1004                 request_pending_list =
1005                                 g_slist_remove(request_pending_list, req);
1006                 resolv(req, req->request, req->name);
1007                 g_free(req->request);
1008                 g_free(req->name);
1009         }
1010 }
1011
1012 static void dnsproxy_offline_mode(connman_bool_t enabled)
1013 {
1014         GSList *list;
1015
1016         DBG("enabled %d", enabled);
1017
1018         for (list = server_list; list; list = list->next) {
1019                 struct server_data *data = list->data;
1020
1021                 if (enabled == FALSE) {
1022                         connman_info("Enabling DNS server %s", data->server);
1023                         data->enabled = TRUE;
1024                 } else {
1025                         connman_info("Disabling DNS server %s", data->server);
1026                         data->enabled = FALSE;
1027                 }
1028         }
1029 }
1030
1031 static void dnsproxy_default_changed(struct connman_service *service)
1032 {
1033         GSList *list;
1034         char *interface;
1035
1036         DBG("service %p", service);
1037
1038         if (service == NULL) {
1039                 /* When no services are active, then disable DNS proxying */
1040                 dnsproxy_offline_mode(TRUE);
1041                 return;
1042         }
1043
1044         interface = connman_service_get_interface(service);
1045         if (interface == NULL)
1046                 return;
1047
1048         for (list = server_list; list; list = list->next) {
1049                 struct server_data *data = list->data;
1050
1051                 if (g_strcmp0(data->interface, interface) == 0) {
1052                         connman_info("Enabling DNS server %s", data->server);
1053                         data->enabled = TRUE;
1054                 } else {
1055                         connman_info("Disabling DNS server %s", data->server);
1056                         data->enabled = FALSE;
1057                 }
1058         }
1059
1060         g_free(interface);
1061 }
1062
1063 static struct connman_notifier dnsproxy_notifier = {
1064         .name                   = "dnsproxy",
1065         .default_changed        = dnsproxy_default_changed,
1066         .offline_mode           = dnsproxy_offline_mode,
1067 };
1068
1069 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
1070
1071 static int parse_request(unsigned char *buf, int len,
1072                                         char *name, unsigned int size)
1073 {
1074         struct domain_hdr *hdr = (void *) buf;
1075         uint16_t qdcount = ntohs(hdr->qdcount);
1076         uint16_t arcount = ntohs(hdr->arcount);
1077         unsigned char *ptr;
1078         char *last_label = NULL;
1079         unsigned int remain, used = 0;
1080
1081         if (len < 12)
1082                 return -EINVAL;
1083
1084         DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
1085                                         hdr->id, hdr->qr, hdr->opcode,
1086                                                         qdcount, arcount);
1087
1088         if (hdr->qr != 0 || qdcount != 1)
1089                 return -EINVAL;
1090
1091         memset(name, 0, size);
1092
1093         ptr = buf + sizeof(struct domain_hdr);
1094         remain = len - sizeof(struct domain_hdr);
1095
1096         while (remain > 0) {
1097                 uint8_t len = *ptr;
1098
1099                 if (len == 0x00) {
1100                         last_label = (char *) (ptr + 1);
1101                         break;
1102                 }
1103
1104                 if (used + len + 1 > size)
1105                         return -ENOBUFS;
1106
1107                 strncat(name, (char *) (ptr + 1), len);
1108                 strcat(name, ".");
1109
1110                 used += len + 1;
1111
1112                 ptr += len + 1;
1113                 remain -= len + 1;
1114         }
1115
1116         if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
1117                                 !memcmp(last_label + 5, opt_edns0_type, 2)) {
1118                 uint16_t edns0_bufsize;
1119
1120                 edns0_bufsize = last_label[7] << 8 | last_label[8];
1121
1122                 DBG("EDNS0 buffer size %u", edns0_bufsize);
1123
1124                 /* This is an evil hack until full TCP support has been
1125                  * implemented.
1126                  *
1127                  * Somtimes the EDNS0 request gets send with a too-small
1128                  * buffer size. Since glibc doesn't seem to crash when it
1129                  * gets a response biffer then it requested, just bump
1130                  * the buffer size up to 4KiB.
1131                  */
1132                 if (edns0_bufsize < 0x1000) {
1133                         last_label[7] = 0x10;
1134                         last_label[8] = 0x00;
1135                 }
1136         }
1137
1138         DBG("query %s", name);
1139
1140         return 0;
1141 }
1142
1143 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
1144                                                         gpointer user_data)
1145 {
1146         unsigned char buf[768];
1147         char query[512];
1148         struct request_data *req;
1149         struct server_data *server;
1150         int sk, client_sk, len, err;
1151         struct sockaddr_in6 client_addr;
1152         socklen_t client_addr_len = sizeof(client_addr);
1153         GSList *list;
1154         struct listener_data *ifdata = user_data;
1155
1156         DBG("condition 0x%x", condition);
1157
1158         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1159                 if (ifdata->tcp_listener_watch > 0)
1160                         g_source_remove(ifdata->tcp_listener_watch);
1161                 ifdata->tcp_listener_watch = 0;
1162
1163                 connman_error("Error with TCP listener channel");
1164
1165                 return FALSE;
1166         }
1167
1168         sk = g_io_channel_unix_get_fd(channel);
1169
1170         client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
1171         if (client_sk < 0) {
1172                 connman_error("Accept failure on TCP listener");
1173                 ifdata->tcp_listener_watch = 0;
1174                 return FALSE;
1175         }
1176
1177         len = recv(client_sk, buf, sizeof(buf), 0);
1178         if (len < 2)
1179                 return TRUE;
1180
1181         DBG("Received %d bytes (id 0x%04x)", len, buf[2] | buf[3] << 8);
1182
1183         err = parse_request(buf + 2, len - 2, query, sizeof(query));
1184         if (err < 0 || (g_slist_length(server_list) == 0)) {
1185                 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
1186                 return TRUE;
1187         }
1188
1189         req = g_try_new0(struct request_data, 1);
1190         if (req == NULL)
1191                 return TRUE;
1192
1193         memcpy(&req->sa, &client_addr, client_addr_len);
1194         req->sa_len = client_addr_len;
1195         req->client_sk = client_sk;
1196         req->protocol = IPPROTO_TCP;
1197
1198         request_id += 2;
1199         if (request_id == 0x0000 || request_id == 0xffff)
1200                 request_id += 2;
1201
1202         req->srcid = buf[2] | (buf[3] << 8);
1203         req->dstid = request_id;
1204         req->altid = request_id + 1;
1205         req->request_len = len;
1206
1207         buf[2] = req->dstid & 0xff;
1208         buf[3] = req->dstid >> 8;
1209
1210         req->numserv = 0;
1211         req->ifdata = (struct listener_data *) ifdata;
1212         req->append_domain = FALSE;
1213         request_list = g_slist_append(request_list, req);
1214
1215         for (list = server_list; list; list = list->next) {
1216                 struct server_data *data = list->data;
1217                 GList *domains;
1218
1219                 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
1220                         continue;
1221
1222                 server = create_server(data->interface, NULL,
1223                                         data->server, IPPROTO_TCP);
1224
1225                 /*
1226                  * If server is NULL, we're not connected yet.
1227                  * Copy the relevant buffers and continue with
1228                  * the next nameserver.
1229                  * The request will actually be sent once we're
1230                  * properly connected over TCP to this nameserver.
1231                  */
1232                 if (server == NULL) {
1233                         req->request = g_try_malloc0(req->request_len);
1234                         if (req->request == NULL)
1235                                 return TRUE;
1236
1237                         memcpy(req->request, buf, req->request_len);
1238
1239                         req->name = g_try_malloc0(sizeof(query));
1240                         if (req->name == NULL) {
1241                                 g_free(req->request);
1242                                 return TRUE;
1243                         }
1244                         memcpy(req->name, query, sizeof(query));
1245
1246                         continue;
1247                 }
1248
1249                 if (req->timeout > 0)
1250                         g_source_remove(req->timeout);
1251
1252                 for (domains = data->domains; domains;
1253                                 domains = domains->next) {
1254                         char *dom = domains->data;
1255
1256                         DBG("Adding domain %s to %s", dom, server->server);
1257
1258                         server->domains = g_list_append(server->domains,
1259                                                 g_strdup(dom));
1260                 }
1261
1262                 req->timeout = g_timeout_add_seconds(30, request_timeout, req);
1263                 ns_resolv(server, req, buf, query);
1264         }
1265
1266         return TRUE;
1267 }
1268
1269 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
1270                                                         gpointer user_data)
1271 {
1272         unsigned char buf[768];
1273         char query[512];
1274         struct request_data *req;
1275         struct sockaddr_in6 client_addr;
1276         socklen_t client_addr_len = sizeof(client_addr);
1277         int sk, err, len;
1278         struct listener_data *ifdata = user_data;
1279
1280         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1281                 connman_error("Error with UDP listener channel");
1282                 ifdata->udp_listener_watch = 0;
1283                 return FALSE;
1284         }
1285
1286         sk = g_io_channel_unix_get_fd(channel);
1287
1288         memset(&client_addr, 0, client_addr_len);
1289         len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
1290                        &client_addr_len);
1291         if (len < 2)
1292                 return TRUE;
1293
1294         DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
1295
1296         err = parse_request(buf, len, query, sizeof(query));
1297         if (err < 0 || (g_slist_length(server_list) == 0)) {
1298                 send_response(sk, buf, len, (void *)&client_addr,
1299                                 client_addr_len, IPPROTO_UDP);
1300                 return TRUE;
1301         }
1302
1303         req = g_try_new0(struct request_data, 1);
1304         if (req == NULL)
1305                 return TRUE;
1306
1307         memcpy(&req->sa, &client_addr, client_addr_len);
1308         req->sa_len = client_addr_len;
1309         req->client_sk = 0;
1310         req->protocol = IPPROTO_UDP;
1311
1312         request_id += 2;
1313         if (request_id == 0x0000 || request_id == 0xffff)
1314                 request_id += 2;
1315
1316         req->srcid = buf[0] | (buf[1] << 8);
1317         req->dstid = request_id;
1318         req->altid = request_id + 1;
1319         req->request_len = len;
1320
1321         buf[0] = req->dstid & 0xff;
1322         buf[1] = req->dstid >> 8;
1323
1324         req->numserv = 0;
1325         req->ifdata = (struct listener_data *) ifdata;
1326         req->timeout = g_timeout_add_seconds(5, request_timeout, req);
1327         req->append_domain = FALSE;
1328         request_list = g_slist_append(request_list, req);
1329
1330         return resolv(req, buf, query);
1331 }
1332
1333 static int create_dns_listener(int protocol, struct listener_data *ifdata)
1334 {
1335         GIOChannel *channel;
1336         const char *proto;
1337         union {
1338                 struct sockaddr sa;
1339                 struct sockaddr_in6 sin6;
1340                 struct sockaddr_in sin;
1341         } s;
1342         socklen_t slen;
1343         int sk, type, v6only = 0;
1344         int family = AF_INET6;
1345
1346
1347         DBG("interface %s", ifdata->ifname);
1348
1349         switch (protocol) {
1350         case IPPROTO_UDP:
1351                 proto = "UDP";
1352                 type = SOCK_DGRAM | SOCK_CLOEXEC;
1353                 break;
1354
1355         case IPPROTO_TCP:
1356                 proto = "TCP";
1357                 type = SOCK_STREAM | SOCK_CLOEXEC;
1358                 break;
1359
1360         default:
1361                 return -EINVAL;
1362         }
1363
1364         sk = socket(family, type, protocol);
1365         if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
1366                 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
1367                 family = AF_INET;
1368                 sk = socket(family, type, protocol);
1369         }
1370         if (sk < 0) {
1371                 connman_error("Failed to create %s listener socket", proto);
1372                 return -EIO;
1373         }
1374
1375         if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1376                                         ifdata->ifname,
1377                                         strlen(ifdata->ifname) + 1) < 0) {
1378                 connman_error("Failed to bind %s listener interface", proto);
1379                 close(sk);
1380                 return -EIO;
1381         }
1382         /* Ensure it accepts Legacy IP connections too */
1383         if (family == AF_INET6 &&
1384                         setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
1385                                         &v6only, sizeof(v6only)) < 0) {
1386                 connman_error("Failed to clear V6ONLY on %s listener socket",
1387                               proto);
1388                 close(sk);
1389                 return -EIO;
1390         }
1391
1392         if (family == AF_INET) {
1393                 memset(&s.sin, 0, sizeof(s.sin));
1394                 s.sin.sin_family = AF_INET;
1395                 s.sin.sin_port = htons(53);
1396                 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
1397                 slen = sizeof(s.sin);
1398         } else {
1399                 memset(&s.sin6, 0, sizeof(s.sin6));
1400                 s.sin6.sin6_family = AF_INET6;
1401                 s.sin6.sin6_port = htons(53);
1402                 s.sin6.sin6_addr = in6addr_any;
1403                 slen = sizeof(s.sin6);
1404         }
1405
1406         if (bind(sk, &s.sa, slen) < 0) {
1407                 connman_error("Failed to bind %s listener socket", proto);
1408                 close(sk);
1409                 return -EIO;
1410         }
1411
1412         if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
1413                 connman_error("Failed to listen on TCP socket");
1414                 close(sk);
1415                 return -EIO;
1416         }
1417
1418         channel = g_io_channel_unix_new(sk);
1419         if (channel == NULL) {
1420                 connman_error("Failed to create %s listener channel", proto);
1421                 close(sk);
1422                 return -EIO;
1423         }
1424
1425         g_io_channel_set_close_on_unref(channel, TRUE);
1426
1427         if (protocol == IPPROTO_TCP) {
1428                 ifdata->tcp_listener_channel = channel;
1429                 ifdata->tcp_listener_watch = g_io_add_watch(channel,
1430                                 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
1431         } else {
1432                 ifdata->udp_listener_channel = channel;
1433                 ifdata->udp_listener_watch = g_io_add_watch(channel,
1434                                 G_IO_IN, udp_listener_event, (gpointer) ifdata);
1435         }
1436
1437         return 0;
1438 }
1439
1440 static void destroy_udp_listener(struct listener_data *ifdata)
1441 {
1442         DBG("interface %s", ifdata->ifname);
1443
1444         if (ifdata->udp_listener_watch > 0)
1445                 g_source_remove(ifdata->udp_listener_watch);
1446
1447         g_io_channel_unref(ifdata->udp_listener_channel);
1448 }
1449
1450 static void destroy_tcp_listener(struct listener_data *ifdata)
1451 {
1452         DBG("interface %s", ifdata->ifname);
1453
1454         if (ifdata->tcp_listener_watch > 0)
1455                 g_source_remove(ifdata->tcp_listener_watch);
1456
1457         g_io_channel_unref(ifdata->tcp_listener_channel);
1458 }
1459
1460 static int create_listener(struct listener_data *ifdata)
1461 {
1462         int err;
1463
1464         err = create_dns_listener(IPPROTO_UDP, ifdata);
1465         if (err < 0)
1466                 return err;
1467
1468         err = create_dns_listener(IPPROTO_TCP, ifdata);
1469         if (err < 0) {
1470                 destroy_udp_listener(ifdata);
1471                 return err;
1472         }
1473
1474         if (g_strcmp0(ifdata->ifname, "lo") == 0)
1475                 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
1476
1477         return 0;
1478 }
1479
1480 static void destroy_listener(struct listener_data *ifdata)
1481 {
1482         GSList *list;
1483
1484         if (g_strcmp0(ifdata->ifname, "lo") == 0)
1485                 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
1486
1487         for (list = request_pending_list; list; list = list->next) {
1488                 struct request_data *req = list->data;
1489
1490                 DBG("Dropping pending request (id 0x%04x -> 0x%04x)",
1491                                                 req->srcid, req->dstid);
1492
1493                 g_free(req->resp);
1494                 g_free(req->request);
1495                 g_free(req->name);
1496                 g_free(req);
1497                 list->data = NULL;
1498         }
1499
1500         g_slist_free(request_pending_list);
1501         request_pending_list = NULL;
1502
1503         for (list = request_list; list; list = list->next) {
1504                 struct request_data *req = list->data;
1505
1506                 DBG("Dropping request (id 0x%04x -> 0x%04x)",
1507                                                 req->srcid, req->dstid);
1508
1509                 g_free(req->resp);
1510                 g_free(req->request);
1511                 g_free(req->name);
1512                 g_free(req);
1513                 list->data = NULL;
1514         }
1515
1516         g_slist_free(request_list);
1517         request_list = NULL;
1518
1519         destroy_tcp_listener(ifdata);
1520         destroy_udp_listener(ifdata);
1521 }
1522
1523 int __connman_dnsproxy_add_listener(const char *interface)
1524 {
1525         struct listener_data *ifdata;
1526         int err;
1527
1528         DBG("interface %s", interface);
1529
1530         if (g_hash_table_lookup(listener_table, interface) != NULL)
1531                 return 0;
1532
1533         ifdata = g_try_new0(struct listener_data, 1);
1534         if (ifdata == NULL)
1535                 return -ENOMEM;
1536
1537         ifdata->ifname = g_strdup(interface);
1538         ifdata->udp_listener_channel = NULL;
1539         ifdata->udp_listener_watch = 0;
1540         ifdata->tcp_listener_channel = NULL;
1541         ifdata->tcp_listener_watch = 0;
1542
1543         err = create_listener(ifdata);
1544         if (err < 0) {
1545                 connman_error("Couldn't create listener for %s err %d",
1546                                 interface, err);
1547                 g_free(ifdata->ifname);
1548                 g_free(ifdata);
1549                 return err;
1550         }
1551         g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
1552         return 0;
1553 }
1554
1555 void __connman_dnsproxy_remove_listener(const char *interface)
1556 {
1557         struct listener_data *ifdata;
1558
1559         DBG("interface %s", interface);
1560
1561         ifdata = g_hash_table_lookup(listener_table, interface);
1562         if (ifdata == NULL)
1563                 return;
1564
1565         destroy_listener(ifdata);
1566
1567         g_hash_table_remove(listener_table, interface);
1568 }
1569
1570 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
1571 {
1572         __connman_dnsproxy_remove_listener(key);
1573 }
1574
1575 int __connman_dnsproxy_init(void)
1576 {
1577         int err;
1578
1579         DBG("");
1580
1581         listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1582                                                         g_free, g_free);
1583         err = __connman_dnsproxy_add_listener("lo");
1584         if (err < 0)
1585                 return err;
1586
1587         err = connman_notifier_register(&dnsproxy_notifier);
1588         if (err < 0)
1589                 goto destroy;
1590
1591         return 0;
1592
1593 destroy:
1594         __connman_dnsproxy_remove_listener("lo");
1595         g_hash_table_destroy(listener_table);
1596
1597         return err;
1598 }
1599
1600 void __connman_dnsproxy_cleanup(void)
1601 {
1602         DBG("");
1603
1604         connman_notifier_unregister(&dnsproxy_notifier);
1605
1606         g_hash_table_foreach(listener_table, remove_listener, NULL);
1607
1608         g_hash_table_destroy(listener_table);
1609 }