77d9000469f532cd65be29b49eab4f50f8b84449
[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
61   dns_transmit_handle = NULL;
62
63   /*
64    * Send the rehijack-message
65    */
66   if (restart_hijack == 1)
67   {
68     restart_hijack = 0;
69     /*
70      * The message is just a header
71      */
72     GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= size);
73     struct GNUNET_MessageHeader *hdr = buf;
74
75     len = sizeof (struct GNUNET_MessageHeader);
76     hdr->size = htons (len);
77     hdr->type = htons (GNUNET_MESSAGE_TYPE_REHIJACK);
78   }
79   else if (head != NULL)
80   {
81     struct query_packet_list *query = head;
82
83     len = ntohs (query->pkt.hdr.size);
84
85     GNUNET_assert (len <= size);
86
87     memcpy (buf, &query->pkt.hdr, len);
88
89     GNUNET_CONTAINER_DLL_remove (head, tail, query);
90
91     GNUNET_free (query);
92   }
93   else
94   {
95     GNUNET_break (0);
96     len = 0;
97   }
98
99   /*
100    * Check whether more data is to be sent
101    */
102   if (head != NULL)
103   {
104     dns_transmit_handle =
105         GNUNET_CLIENT_notify_transmit_ready (dns_connection,
106                                              ntohs (head->pkt.hdr.size),
107                                              GNUNET_TIME_UNIT_FOREVER_REL,
108                                              GNUNET_YES, &send_query, NULL);
109   }
110   else if (restart_hijack == 1)
111   {
112     dns_transmit_handle =
113         GNUNET_CLIENT_notify_transmit_ready (dns_connection,
114                                              sizeof (struct
115                                                      GNUNET_MessageHeader),
116                                              GNUNET_TIME_UNIT_FOREVER_REL,
117                                              GNUNET_YES, &send_query, NULL);
118   }
119
120   return len;
121 }
122
123 /* }}} */
124
125
126 /**
127  * Connect to the service-dns
128  */
129 void
130 connect_to_service_dns (void *cls
131                         __attribute__ ((unused)),
132                         const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   conn_task = GNUNET_SCHEDULER_NO_TASK;
135   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
136     return;
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
138   GNUNET_assert (dns_connection == NULL);
139   dns_connection = GNUNET_CLIENT_connect ("dns", cfg);
140   /* This would most likely be a misconfiguration */
141   GNUNET_assert (NULL != dns_connection);
142   GNUNET_CLIENT_receive (dns_connection, &dns_answer_handler, NULL,
143                          GNUNET_TIME_UNIT_FOREVER_REL);
144
145   /* We might not yet be connected. Yay, mps. */
146   if (NULL == dns_connection)
147     return;
148
149   /* If a packet is already in the list, schedule to send it */
150   if (dns_transmit_handle == NULL && head != NULL)
151     dns_transmit_handle =
152         GNUNET_CLIENT_notify_transmit_ready (dns_connection,
153                                              ntohs (head->pkt.hdr.size),
154                                              GNUNET_TIME_UNIT_FOREVER_REL,
155                                              GNUNET_YES, &send_query, NULL);
156   else if (dns_transmit_handle == NULL && restart_hijack == 1)
157   {
158     dns_transmit_handle =
159         GNUNET_CLIENT_notify_transmit_ready (dns_connection,
160                                              sizeof (struct
161                                                      GNUNET_MessageHeader),
162                                              GNUNET_TIME_UNIT_FOREVER_REL,
163                                              GNUNET_YES, &send_query, NULL);
164   }
165 }
166
167 /**
168  * This receives packets from the service-dns and schedules process_answer to
169  * handle it
170  */
171 void
172 dns_answer_handler (void *cls
173                     __attribute__ ((unused)),
174                     const struct GNUNET_MessageHeader *msg)
175 {
176   /* the service disconnected, reconnect after short wait */
177   if (msg == NULL)
178   {
179     if (dns_transmit_handle != NULL)
180       GNUNET_CLIENT_notify_transmit_ready_cancel (dns_transmit_handle);
181     dns_transmit_handle = NULL;
182     GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
183     dns_connection = NULL;
184     conn_task =
185         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
186                                       &connect_to_service_dns, NULL);
187     return;
188   }
189
190   /* the service did something strange, reconnect immediately */
191   if (msg->type != htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS))
192   {
193     GNUNET_break (0);
194     GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
195     dns_connection = NULL;
196     conn_task = GNUNET_SCHEDULER_add_now (&connect_to_service_dns, NULL);
197     return;
198   }
199   void *pkt = GNUNET_malloc (ntohs (msg->size));
200
201   memcpy (pkt, msg, ntohs (msg->size));
202
203   GNUNET_SCHEDULER_add_now (process_answer, pkt);
204   GNUNET_CLIENT_receive (dns_connection, &dns_answer_handler, NULL,
205                          GNUNET_TIME_UNIT_FOREVER_REL);
206 }