bug
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn-dns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file vpn/gnunet-daemon-vpn-dns.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include <platform.h>
27 #include <gnunet_common.h>
28 #include <gnunet_client_lib.h>
29 #include <gnunet_os_lib.h>
30 #include <gnunet_mesh_service.h>
31 #include <gnunet_protocols.h>
32 #include <gnunet_server_lib.h>
33 #include <gnunet_container_lib.h>
34 #include <block_dns.h>
35
36 #include "gnunet-daemon-vpn-dns.h"
37 #include "gnunet-daemon-vpn.h"
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-service-dns-p.h"
40 #include "gnunet-vpn-packet.h"
41
42 struct query_packet_list *head;
43 struct query_packet_list *tail;
44 struct GNUNET_CLIENT_Connection *dns_connection;
45 unsigned char restart_hijack;
46 struct answer_packet_list *answer_proc_head;
47 struct answer_packet_list *answer_proc_tail;
48
49 struct GNUNET_CLIENT_TransmitHandle *dns_transmit_handle;
50
51 /**
52  * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages
53  * to the service-dns
54  * {{{
55  */
56 size_t
57 send_query (void *cls __attribute__ ((unused)), size_t size, void *buf)
58 {
59   size_t len;
60   dns_transmit_handle = NULL;
61
62   /*
63    * Send the rehijack-message
64    */
65   if (restart_hijack == 1)
66   {
67     restart_hijack = 0;
68     /*
69      * The message is just a header
70      */
71     GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= size);
72     struct GNUNET_MessageHeader *hdr = buf;
73
74     len = sizeof (struct GNUNET_MessageHeader);
75     hdr->size = htons (len);
76     hdr->type = htons (GNUNET_MESSAGE_TYPE_REHIJACK);
77   }
78   else if (head != NULL)
79   {
80     struct query_packet_list *query = head;
81
82     len = ntohs (query->pkt.hdr.size);
83
84     GNUNET_assert (len <= size);
85
86     memcpy (buf, &query->pkt.hdr, len);
87
88     GNUNET_CONTAINER_DLL_remove (head, tail, query);
89
90     GNUNET_free (query);
91   }
92   else
93   {
94     GNUNET_break (0);
95     len = 0;
96   }
97
98   /*
99    * Check whether more data is to be sent
100    */
101   if (head != NULL)
102   {
103     dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection,
104                                          ntohs (head->pkt.hdr.size),
105                                          GNUNET_TIME_UNIT_FOREVER_REL,
106                                          GNUNET_YES, &send_query, NULL);
107   }
108   else if (restart_hijack == 1)
109   {
110     dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection,
111                                          sizeof (struct GNUNET_MessageHeader),
112                                          GNUNET_TIME_UNIT_FOREVER_REL,
113                                          GNUNET_YES, &send_query, NULL);
114   }
115
116   return len;
117 }
118
119 /* }}} */
120
121
122 /**
123  * Connect to the service-dns
124  */
125 void
126 connect_to_service_dns (void *cls
127                         __attribute__ ((unused)),
128                         const struct GNUNET_SCHEDULER_TaskContext *tc)
129 {
130   conn_task = GNUNET_SCHEDULER_NO_TASK;
131   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
132     return;
133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
134   GNUNET_assert (dns_connection == NULL);
135   dns_connection = GNUNET_CLIENT_connect ("dns", cfg);
136   /* This would most likely be a misconfiguration */
137   GNUNET_assert (NULL != dns_connection);
138   GNUNET_CLIENT_receive (dns_connection, &dns_answer_handler, NULL,
139                          GNUNET_TIME_UNIT_FOREVER_REL);
140
141   /* We might not yet be connected. Yay, mps. */
142   if (NULL == dns_connection)
143     return;
144
145   /* If a packet is already in the list, schedule to send it */
146   if (dns_transmit_handle == NULL && head != NULL)
147     dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection,
148                                          ntohs (head->pkt.hdr.size),
149                                          GNUNET_TIME_UNIT_FOREVER_REL,
150                                          GNUNET_YES, &send_query, NULL);
151   else if (dns_transmit_handle == NULL && restart_hijack == 1)
152   {
153     dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection,
154                                          sizeof (struct GNUNET_MessageHeader),
155                                          GNUNET_TIME_UNIT_FOREVER_REL,
156                                          GNUNET_YES, &send_query, NULL);
157   }
158 }
159
160 /**
161  * This receives packets from the service-dns and schedules process_answer to
162  * handle it
163  */
164 void
165 dns_answer_handler (void *cls
166                     __attribute__ ((unused)),
167                     const struct GNUNET_MessageHeader *msg)
168 {
169   /* the service disconnected, reconnect after short wait */
170   if (msg == NULL)
171   {
172     if (dns_transmit_handle != NULL)
173       GNUNET_CLIENT_notify_transmit_ready_cancel(dns_transmit_handle);
174     dns_transmit_handle = NULL;
175     GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
176     dns_connection = NULL;
177     conn_task =
178         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
179                                       &connect_to_service_dns, NULL);
180     return;
181   }
182
183   /* the service did something strange, reconnect immediately */
184   if (msg->type != htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS))
185   {
186     GNUNET_break (0);
187     GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
188     dns_connection = NULL;
189     conn_task = GNUNET_SCHEDULER_add_now (&connect_to_service_dns, NULL);
190     return;
191   }
192   void *pkt = GNUNET_malloc (ntohs (msg->size));
193
194   memcpy (pkt, msg, ntohs (msg->size));
195
196   GNUNET_SCHEDULER_add_now (process_answer, pkt);
197   GNUNET_CLIENT_receive (dns_connection, &dns_answer_handler, NULL,
198                          GNUNET_TIME_UNIT_FOREVER_REL);
199 }