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