14cd07f7aa748b2e1306999c97b59f1a95ae63e0
[oweals/gnunet.git] / src / vpn / gnunet-service-dns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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-service-dns.c
23  * @author Philipp Tölke
24  */
25 #include "platform.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_service_lib.h"
28 #include "gnunet_network_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-service-dns-p.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet-vpn-packet.h"
33 #include "gnunet-vpn-pretty-print.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet-dns-parser.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_block_lib.h"
38 #include "gnunet_block_dns.h"
39
40 struct dns_cls {
41         struct GNUNET_SCHEDULER_Handle *sched;
42
43         struct GNUNET_NETWORK_Handle *dnsout;
44
45         struct GNUNET_DHT_Handle *dht;
46
47         unsigned short dnsoutport;
48
49         struct answer_packet_list *head;
50         struct answer_packet_list *tail;
51 };
52 static struct dns_cls mycls;
53
54 struct dns_query_id_state {
55         unsigned valid:1;
56         struct GNUNET_SERVER_Client* client;
57         unsigned local_ip:32;
58         unsigned local_port:16;
59 };
60 static struct dns_query_id_state query_states[65536]; /* This is < 1MiB */
61
62 void hijack(unsigned short port) {
63         char port_s[6];
64
65         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", port);
66         snprintf(port_s, 6, "%d", port);
67         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", port_s, NULL);
68 }
69
70 void unhijack(unsigned short port) {
71         char port_s[6];
72
73         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
74         snprintf(port_s, 6, "%d", port);
75         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
76 }
77
78 void receive_dht(void *cls,
79                  struct GNUNET_TIME_Absolute exp,
80                  const GNUNET_HashCode *key,
81                  const struct GNUNET_PeerIdentity *const *get_path,
82                  const struct GNUNET_PeerIdentity *const *put_path,
83                  enum GNUNET_BLOCK_Type type,
84                  size_t size,
85                  const void *data)
86 {
87   GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
88   const struct GNUNET_DNS_Record* rec = data;
89   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got block of size %s, peer: %08x, desc: %08x\n", size, *((unsigned int*)&rec->peer), *((unsigned int*)&rec->service_descriptor));
90 }
91
92 /**
93  * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
94  */
95 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
96 {
97         struct query_packet* pkt = (struct query_packet*)message;
98         struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
99         struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
100
101         if (pdns->queries[0]->namelen > 9 &&
102             0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9)) {
103             GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
104             GNUNET_HashCode key;
105             GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
106             GNUNET_DHT_get_start(mycls.dht,
107                                  GNUNET_TIME_UNIT_MINUTES,
108                                  GNUNET_BLOCK_TYPE_DNS,
109                                  &key,
110                                  GNUNET_DHT_RO_NONE,
111                                  NULL,
112                                  0,
113                                  NULL,
114                                  0,
115                                  receive_dht,
116                                  NULL);
117             goto out;
118         }
119
120         GNUNET_free(pdns);
121
122         struct sockaddr_in dest;
123         memset(&dest, 0, sizeof dest);
124         dest.sin_port = htons(53);
125         dest.sin_addr.s_addr = pkt->orig_to;
126
127         query_states[dns->s.id].valid = 1;
128         query_states[dns->s.id].client = client;
129         query_states[dns->s.id].local_ip = pkt->orig_from;
130         query_states[dns->s.id].local_port = pkt->src_port;
131
132         /* int r = */ GNUNET_NETWORK_socket_sendto(mycls.dnsout, dns, ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1, (struct sockaddr*) &dest, sizeof dest);
133
134 out:
135         GNUNET_SERVER_receive_done(client, GNUNET_OK);
136 }
137
138 size_t send_answer(void* cls, size_t size, void* buf) {
139         struct answer_packet_list* query = mycls.head;
140         size_t len = ntohs(query->pkt.hdr.size);
141
142         GNUNET_assert(len <= size);
143
144         memcpy(buf, &query->pkt.hdr, len);
145
146         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
147
148         GNUNET_free(query);
149
150         if (mycls.head != NULL) {
151                 GNUNET_SERVER_notify_transmit_ready(cls, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, cls);
152         }
153
154         return len;
155 }
156
157 static void read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
158         unsigned char buf[65536];
159         struct dns_pkt* dns = (struct dns_pkt*)buf;
160
161         if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
162                 return;
163
164         struct sockaddr_in addr;
165         memset(&addr, 0, sizeof addr);
166         unsigned int addrlen = sizeof addr;
167
168         int r;
169         r = GNUNET_NETWORK_socket_recvfrom(mycls.dnsout, buf, 65536, (struct sockaddr*)&addr, &addrlen);
170
171         /* if (r < 0) TODO */
172
173         if (query_states[dns->s.id].valid == 1) {
174                 query_states[dns->s.id].valid = 0;
175
176                 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
177                 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
178                 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
179                 answer->pkt.hdr.size = htons(len);
180                 answer->pkt.from = addr.sin_addr.s_addr;
181                 answer->pkt.to = query_states[dns->s.id].local_ip;
182                 answer->pkt.dst_port = query_states[dns->s.id].local_port;
183                 memcpy(answer->pkt.data, buf, r);
184
185                 GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
186
187                 /* struct GNUNET_CONNECTION_TransmitHandle* th = */ GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client, len, GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, query_states[dns->s.id].client);
188         }
189
190         GNUNET_SCHEDULER_add_read_net(mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
191 }
192
193
194 /**
195  * Task run during shutdown.
196  *
197  * @param cls unused
198  * @param tc unused
199  */
200 static void
201 cleanup_task (void *cls,
202               const struct GNUNET_SCHEDULER_TaskContext *tc)
203 {
204         unhijack(mycls.dnsoutport);
205         GNUNET_DHT_disconnect(mycls.dht);
206 }
207
208 static void
209 publish_name (void *cls,
210              const struct GNUNET_SCHEDULER_TaskContext *tc)
211 {
212   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
213     return;
214
215   char* name = "philipptoelke.gnunet";
216   size_t size = sizeof(struct GNUNET_DNS_Record) + strlen(name) - 1;
217   struct GNUNET_DNS_Record *data = alloca(size);
218   memset(data, 0, size);
219   memcpy(data->name, name, strlen(name));
220   data->namelen = strlen(name);
221   *((unsigned int*)&data->service_descriptor) = 0x11223344;
222   *((unsigned int*)&data->peer) = 0x55667788;
223
224   GNUNET_HashCode key;
225   GNUNET_CRYPTO_hash(name, strlen(name), &key);
226   GNUNET_DHT_put(mycls.dht,
227                       &key,
228                       GNUNET_DHT_RO_NONE,
229                       GNUNET_BLOCK_TYPE_DNS,
230                       size,
231                       (char*)data,
232                       GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
233                       GNUNET_TIME_UNIT_MINUTES,
234                       NULL,
235                       NULL);
236
237   GNUNET_SCHEDULER_add_delayed (mycls.sched, GNUNET_TIME_UNIT_MINUTES, publish_name, NULL);
238 }
239
240 /**
241  * @param cls closure
242  * @param sched scheduler to use
243  * @param server the initialized server
244  * @param cfg configuration to use
245  */
246 static void
247 run (void *cls,
248      struct GNUNET_SCHEDULER_Handle *sched,
249      struct GNUNET_SERVER_Handle *server,
250      const struct GNUNET_CONFIGURATION_Handle *cfg)
251 {
252   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
253           /* callback, cls, type, size */
254     {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
255     {NULL, NULL, 0, 0}
256   };
257
258   {
259   int i;
260   for (i = 0; i < 65536; i++) {
261     query_states[i].valid = 0;
262   }
263   }
264
265   mycls.dht = GNUNET_DHT_connect(sched, cfg, 1024);
266
267   struct sockaddr_in addr;
268
269   mycls.sched = sched;
270   mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
271   if (mycls.dnsout == NULL) 
272     return;
273   memset(&addr, 0, sizeof(struct sockaddr_in));
274
275   int err = GNUNET_NETWORK_socket_bind (mycls.dnsout,
276                                         (struct sockaddr*)&addr, 
277                                         sizeof(struct sockaddr_in));
278
279   if (err != GNUNET_YES) {
280         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
281         return;
282   }
283   socklen_t addrlen = sizeof(struct sockaddr_in);
284   err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout),
285                     (struct sockaddr*) &addr, 
286                     &addrlen);
287
288   mycls.dnsoutport = htons(addr.sin_port);
289
290   hijack(htons(addr.sin_port));
291
292   GNUNET_SCHEDULER_add_now (mycls.sched, publish_name, NULL);
293
294         GNUNET_SCHEDULER_add_read_net(sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
295
296   GNUNET_SERVER_add_handlers (server, handlers);
297   GNUNET_SCHEDULER_add_delayed (sched,
298                   GNUNET_TIME_UNIT_FOREVER_REL,
299                   &cleanup_task,
300                   cls);
301 }
302
303 /**
304  * The main function for the dns service.
305  *
306  * @param argc number of arguments from the command line
307  * @param argv command line arguments
308  * @return 0 ok, 1 on error
309  */
310 int
311 main (int argc, char *const *argv)
312 {
313   return (GNUNET_OK ==
314           GNUNET_SERVICE_run (argc,
315                               argv,
316                               "dns",
317                               GNUNET_SERVICE_OPTION_NONE,
318                               &run, NULL)) ? 0 : 1;
319 }