Store mappings into the hashmap
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.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.c
23  * @brief 
24  * @author Philipp Toelke
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-vpn-helper-p.h"
31 #include "gnunet-vpn-packet.h"
32 #include "gnunet-vpn-pretty-print.h"
33 #include "gnunet_common.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet-service-dns-p.h"
37 #include "gnunet_client_lib.h"
38 #include "gnunet_container_lib.h"
39 #include "block_dns.h"
40
41 /**
42  * Final status code.
43  */
44 static int ret;
45
46 /**
47  * The configuration to use
48  */
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50
51 /**
52  * PipeHandle to receive data from the helper
53  */
54 static struct GNUNET_DISK_PipeHandle* helper_in;
55
56 /**
57  * PipeHandle to send data to the helper
58  */
59 static struct GNUNET_DISK_PipeHandle* helper_out;
60
61 /**
62  * FileHandle to receive data from the helper
63  */
64 static const struct GNUNET_DISK_FileHandle* fh_from_helper;
65
66 /**
67  * FileHandle to send data to the helper
68  */
69 static const struct GNUNET_DISK_FileHandle* fh_to_helper;
70
71 /**
72  * The Message-Tokenizer that tokenizes the messages comming from the helper
73  */
74 static struct GNUNET_SERVER_MessageStreamTokenizer* mst;
75
76 /**
77  * The connection to the service-dns
78  */
79 static struct GNUNET_CLIENT_Connection *dns_connection;
80
81 /**
82  * A flag to show that the service-dns has to rehijack the outbound dns-packets
83  *
84  * This gets set when the helper restarts as the routing-tables are flushed when
85  * the interface vanishes.
86  */
87 static unsigned char restart_hijack;
88
89 /**
90  * The process id of the helper
91  */
92 static struct GNUNET_OS_Process *helper_proc;
93
94 /**
95  * a list of outgoing dns-query-packets
96  */
97 static struct query_packet_list *head;
98
99 /**
100  * The last element of the list of outgoing dns-query-packets
101  */
102 static struct query_packet_list *tail;
103
104 /**
105  * A list of processed dns-responses.
106  *
107  * "processed" means that the packet is complete and can be sent out via udp
108  * directly
109  */
110 static struct answer_packet_list *answer_proc_head;
111
112 /**
113  * The last element of the list of processed dns-responses.
114  */
115 static struct answer_packet_list *answer_proc_tail;
116
117 /**
118  * The hashmap containing the mappings from ipv6-addresses to gnunet-descriptors
119  */
120 static struct GNUNET_CONTAINER_MultiHashMap* hashmap;
121
122 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
123 static void dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg);
124
125 /**
126  * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages
127  * to the service-dns
128  */
129 static size_t
130 send_query(void* cls, size_t size, void* buf) {
131     size_t len;
132     /*
133      * Send the rehijack-message
134      */
135     if (restart_hijack == 1)
136       {
137         restart_hijack = 0;
138         /*
139          * The message is just a header
140          */
141         GNUNET_assert(sizeof(struct GNUNET_MessageHeader) <= size);
142         struct GNUNET_MessageHeader* hdr = buf;
143         len = sizeof(struct GNUNET_MessageHeader);
144         hdr->size = htons(len);
145         hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK);
146       }
147     else
148       {
149         struct query_packet_list* query = head;
150         len = ntohs(query->pkt.hdr.size);
151
152         GNUNET_assert(len <= size);
153
154         memcpy(buf, &query->pkt.hdr, len);
155
156         GNUNET_CONTAINER_DLL_remove (head, tail, query);
157
158         GNUNET_free(query);
159       }
160
161     /*
162      * Check whether more data is to be sent
163      */
164     if (head != NULL)
165       {
166         GNUNET_CLIENT_notify_transmit_ready(dns_connection, ntohs(head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
167       }
168     else if (restart_hijack == 1)
169       {
170         GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
171       }
172
173     return len;
174 }
175
176 /**
177  * Function scheduled as very last function, cleans up after us
178  */
179 static void
180 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
181     GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
182
183     /* stop the helper */
184     if (helper_proc != NULL)
185       {
186         GNUNET_OS_process_kill (helper_proc, SIGTERM);
187         GNUNET_OS_process_wait (helper_proc);
188         GNUNET_OS_process_close (helper_proc);
189         helper_proc = NULL;
190       }
191
192     /* close the connection to the service-dns */
193     if (dns_connection != NULL)
194       {
195         GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
196         dns_connection = NULL;
197       }
198 }
199
200 /**
201  * Start the helper-process
202  */
203 static void
204 start_helper_and_schedule(void *cls,
205                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
206     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
207       return;
208
209     helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
210     helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
211
212     if (helper_in == NULL || helper_out == NULL) return;
213
214     helper_proc = GNUNET_OS_start_process(helper_in, helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
215
216     fh_from_helper = GNUNET_DISK_pipe_handle (helper_out, GNUNET_DISK_PIPE_END_READ);
217     fh_to_helper = GNUNET_DISK_pipe_handle (helper_in, GNUNET_DISK_PIPE_END_WRITE);
218
219     GNUNET_DISK_pipe_close_end(helper_out, GNUNET_DISK_PIPE_END_WRITE);
220     GNUNET_DISK_pipe_close_end(helper_in, GNUNET_DISK_PIPE_END_READ);
221
222     /* Tell the dns-service to rehijack the dns-port
223      * The routing-table gets flushed if an interface disappears.
224      */
225     restart_hijack = 1;
226     GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
227
228     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL);
229 }
230
231 /**
232  * Restart the helper-process
233  */
234 static void
235 restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
236     // Kill the helper
237     GNUNET_OS_process_kill (helper_proc, SIGKILL);
238     GNUNET_OS_process_wait (helper_proc);
239     GNUNET_OS_process_close (helper_proc);
240     helper_proc = NULL;
241
242     GNUNET_DISK_pipe_close(helper_in);
243     GNUNET_DISK_pipe_close(helper_out);
244
245     /* Restart the helper */
246     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, start_helper_and_schedule, NULL);
247 }
248
249 /**
250  * Read from the helper-process
251  */
252 static void
253 helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
254     /* no message can be bigger then 64k */
255     char buf[65535];
256
257     if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
258       return;
259
260     int t = GNUNET_DISK_file_read(fh_from_helper, &buf, 65535);
261
262     /* On read-error, restart the helper */
263     if (t<=0) {
264         GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n");
265         GNUNET_SCHEDULER_add_now(restart_helper, cls);
266         return;
267     }
268
269     /* FIXME */ GNUNET_SERVER_mst_receive(mst, NULL, buf, t, 0, 0);
270
271     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL);
272 }
273
274 /**
275  * Calculate the checksum of an IPv4-Header
276  */
277 static uint16_t
278 calculate_ip_checksum(uint16_t* hdr, short len) {
279     uint32_t sum = 0;
280     for(; len >= 2; len -= 2)
281       sum += *(hdr++);
282     if (len == 1)
283       sum += *((unsigned char*)hdr);
284
285     sum = (sum >> 16) + (sum & 0xFFFF);
286
287     return ~sum;
288 }
289
290 /**
291  * Send an dns-answer-packet to the helper
292  */
293 static void
294 helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
295     if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
296       return;
297
298     struct answer_packet_list* ans = answer_proc_head;
299     size_t len = ntohs(ans->pkt.hdr.size);
300
301     GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
302
303     size_t data_len = len - sizeof(struct answer_packet) + 1;
304     size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
305     size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
306
307     struct ip_udp_dns* pkt = alloca(pkt_len);
308     memset(pkt, 0, pkt_len);
309
310     /* set the gnunet-header */
311     pkt->shdr.size = htons(pkt_len);
312     pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
313
314     /* set the tun-header (no flags and ethertype of IPv4) */
315     pkt->tun.flags = 0;
316     pkt->tun.type = htons(0x0800);
317
318     /* set the ip-header */
319     pkt->ip_hdr.version = 4;
320     pkt->ip_hdr.hdr_lngth = 5;
321     pkt->ip_hdr.diff_serv = 0;
322     pkt->ip_hdr.tot_lngth = htons(net_len);
323     pkt->ip_hdr.ident = 0;
324     pkt->ip_hdr.flags = 0;
325     pkt->ip_hdr.frag_off = 0;
326     pkt->ip_hdr.ttl = 255;
327     pkt->ip_hdr.proto = 0x11; /* UDP */
328     pkt->ip_hdr.chks = 0; /* Will be calculated later*/
329     pkt->ip_hdr.sadr = ans->pkt.from;
330     pkt->ip_hdr.dadr = ans->pkt.to;
331
332     pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
333
334     /* set the udp-header */
335     pkt->udp_dns.udp_hdr.spt = htons(53);
336     pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
337     pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
338     pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
339
340     memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
341
342     GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
343     GNUNET_free(ans);
344
345     /* FIXME */ GNUNET_DISK_file_write(fh_to_helper, pkt, pkt_len);
346
347     /* if more packets are available, reschedule */
348     if (answer_proc_head != NULL)
349       GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
350                                        fh_to_helper,
351                                        &helper_write,
352                                        NULL);
353 }
354
355 /**
356  * @return GNUNET_YES if a mapping exists
357  */
358 static int
359 address_mapping_exists(unsigned char addr[]) {
360     GNUNET_HashCode* key = alloca(sizeof(GNUNET_HashCode));
361     memset(key, 0, sizeof(GNUNET_HashCode));
362     memcpy(key, addr, 16);
363
364     return GNUNET_CONTAINER_multihashmap_contains(hashmap, key);
365 }
366
367 static void
368 send_icmp_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
369     struct ip6_icmp* request = cls;
370
371     struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
372     memset(response, 0, ntohs(request->shdr.size));
373
374     response->shdr.size = request->shdr.size;
375     response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
376
377     response->tun.flags = 0;
378     response->tun.type = htons(0x86dd);
379
380     response->ip6_hdr.hoplmt = 255;
381     response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
382     response->ip6_hdr.nxthdr = 0x3a;
383     response->ip6_hdr.version = 6;
384     memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
385     memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
386
387     response->icmp_hdr.code = 0;
388     response->icmp_hdr.type = 0x81;
389
390     /* Magic, more Magic! */
391     response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
392
393     /* Copy the rest of the packet */
394     memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
395
396     /* FIXME */ GNUNET_DISK_file_write(fh_to_helper, response, ntohs(response->shdr.size));
397
398     GNUNET_free(request);
399 }
400
401 /**
402  * Receive packets from the helper-process
403  */
404 static void
405 message_token(void *cls,
406               void *client,
407               const struct GNUNET_MessageHeader *message) {
408     GNUNET_assert(ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
409
410     struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
411
412     /* ethertype is ipv6 */
413     if (ntohs(pkt_tun->tun.type) == 0x86dd)
414       {
415         struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
416         GNUNET_assert(pkt6->ip6_hdr.version == 6);
417         struct ip6_tcp *pkt6_tcp;
418         struct ip6_udp *pkt6_udp;
419         struct ip6_icmp *pkt6_icmp;
420
421         pkt_printf(pkt6);
422         switch(pkt6->ip6_hdr.nxthdr)
423           {
424           case 0x06:
425             pkt6_tcp = (struct ip6_tcp*)pkt6;
426             pkt_printf_ip6tcp(pkt6_tcp);
427             break;
428           case 0x11:
429             pkt6_udp = (struct ip6_udp*)pkt6;
430             pkt_printf_ip6udp(pkt6_udp);
431             if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
432                 pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
433             }
434             break;
435           case 0x3a:
436             /* ICMPv6 */
437             pkt6_icmp = (struct ip6_icmp*)pkt6;
438             /* If this packet is an icmp-echo-request and a mapping exists, answer */
439             if (pkt6_icmp->icmp_hdr.type == 0x80 && address_mapping_exists(pkt6->ip6_hdr.dadr))
440               {
441                 pkt6_icmp = GNUNET_malloc(ntohs(pkt6->shdr.size));
442                 memcpy(pkt6_icmp, pkt6, ntohs(pkt6->shdr.size));
443                 GNUNET_SCHEDULER_add_now(&send_icmp_response, pkt6_icmp);
444               }
445             break;
446           }
447       }
448     /* ethertype is ipv4 */
449     else if (ntohs(pkt_tun->tun.type) == 0x0800)
450       {
451         struct ip_pkt *pkt = (struct ip_pkt*) message;
452         struct ip_udp *udp = (struct ip_udp*) message;
453         GNUNET_assert(pkt->ip_hdr.version == 4);
454
455         /* Send dns-packets to the service-dns */
456         if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 )
457           {
458             /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
459             size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9;
460
461             struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
462             query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
463             query->pkt.hdr.size = htons(len);
464             query->pkt.orig_to = pkt->ip_hdr.dadr;
465             query->pkt.orig_from = pkt->ip_hdr.sadr;
466             query->pkt.src_port = udp->udp_hdr.spt;
467             memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
468
469             GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, query);
470
471             if (dns_connection != NULL)
472               GNUNET_CLIENT_notify_transmit_ready(dns_connection,
473                                                   len,
474                                                   GNUNET_TIME_UNIT_FOREVER_REL,
475                                                   GNUNET_YES,
476                                                   &send_query,
477                                                   NULL);
478           }
479       }
480 }
481
482 /**
483  * Connect to the service-dns
484  */
485 static void
486 connect_to_service_dns (void *cls,
487                         const struct GNUNET_SCHEDULER_TaskContext *tc) {
488     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
489       return;
490     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
491     GNUNET_assert (dns_connection == NULL);
492     dns_connection = GNUNET_CLIENT_connect ("dns", cfg);
493     GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
494
495     /* If a packet is already in the list, schedule to send it */
496     if (head != NULL)
497       GNUNET_CLIENT_notify_transmit_ready(dns_connection,
498                                           ntohs(head->pkt.hdr.size),
499                                           GNUNET_TIME_UNIT_FOREVER_REL,
500                                           GNUNET_YES,
501                                           &send_query,
502                                           NULL);
503     else if (restart_hijack == 1)
504       {
505         GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
506       }
507 }
508
509 /**
510  * Create a new Address from an answer-packet
511  * {{{
512  */
513 void
514 new_ip6addr(char* buf, struct answer_packet* pkt) {
515         memcpy(buf, (int[]){htons(0x1234)}, 2);
516         memcpy(buf+2, &pkt->service_descr.service_descriptor, 6);
517         memcpy(buf+8, &pkt->service_descr.peer, 8);
518 }
519 /*}}}*/
520
521 /**
522  * This gets scheduled with cls pointing to an answer_packet and does everything
523  * needed in order to send it to the helper.
524  *
525  * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
526  * doing nothing for "real" services.
527  */
528 static void
529 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
530     struct answer_packet* pkt = cls;
531     struct answer_packet_list* list;
532
533     /* This answer is about a .gnunet-service
534      *
535      * It contains an almost complete DNS-Response, we have to fill in the ip
536      * at the offset pkt->addroffset
537      */
538     //FIXME htons?
539     if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
540       {
541         pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
542
543         GNUNET_HashCode key;
544         memset(&key, 0, sizeof(GNUNET_HashCode));
545         new_ip6addr((char*)&key, pkt);
546
547         struct GNUNET_vpn_service_descriptor* value = GNUNET_malloc(sizeof(struct GNUNET_vpn_service_descriptor));
548         memcpy(value, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
549
550         if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(hashmap,
551                                                            &key,
552                                                            value,
553                                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
554           {
555             GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not store to hashmap\n");
556           }
557
558         memcpy(((char*)pkt)+ntohs(pkt->addroffset), &key, 16);
559
560           /*FIXME:
561            * -save DNS_Record into hashmap, pointed to by ip
562            * -regularily walk through hashmap, deleting old entries
563            *  when is an entry old?
564            *  have a last-used field
565            *  don't remove if last-used "recent", ask dht again if record expired
566            */
567
568         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
569
570         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
571       }
572     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
573       {
574         unsigned short offset = ntohs(pkt->addroffset);
575
576         unsigned short namelen = htons(22); /* calculate the length of the answer */
577         char name[22] = {13, 'p', 'h', 'i', 'l', 'i', 'p', 'p', 't', 'o', 'e', 'l', 'k', 'e', 6, 'g', 'n', 'u', 'n', 'e', 't', 0};
578
579         list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
580
581         struct answer_packet* rpkt = &list->pkt;
582
583         memcpy(rpkt, pkt, offset);
584
585         rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
586         rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
587
588         memcpy(((char*)rpkt)+offset, &namelen, 2);
589         memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
590
591       }
592     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
593       {
594         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
595         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
596       }
597     else
598       {
599         GNUNET_break(0);
600         GNUNET_free(pkt);
601         return;
602       }
603
604     GNUNET_free(pkt);
605
606     GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
607
608     GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_to_helper, &helper_write, NULL);
609
610     return;
611 }
612
613 /**
614  * This receives packets from the service-dns and schedules process_answer to
615  * handle it
616  */
617 static void
618 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg) {
619     /* the service disconnected, reconnect after short wait */
620     if (msg == NULL)
621       {
622         GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
623         dns_connection = NULL;
624         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
625                                       &connect_to_service_dns,
626                                       NULL);
627         return;
628       }
629
630     /* the service did something strange, reconnect immediately */
631     if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS))
632       {
633         GNUNET_break (0);
634         GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
635         dns_connection = NULL;
636         GNUNET_SCHEDULER_add_now (&connect_to_service_dns,
637                                   NULL);
638         return;
639       }
640     void *pkt = GNUNET_malloc(ntohs(msg->size));
641
642     memcpy(pkt, msg, ntohs(msg->size));
643
644     GNUNET_SCHEDULER_add_now(process_answer, pkt);
645     GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
646 }
647
648 /**
649  * Main function that will be run by the scheduler.
650  *
651  * @param cls closure
652  * @param args remaining command-line arguments
653  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
654  * @param cfg configuration
655  */
656 static void
657 run (void *cls,
658      char *const *args,
659      const char *cfgfile,
660      const struct GNUNET_CONFIGURATION_Handle *cfg_) {
661     mst = GNUNET_SERVER_mst_create(&message_token, NULL);
662     cfg = cfg_;
663     restart_hijack = 0;
664     hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
665     GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
666     GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
667     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
668 }
669
670 /**
671  * The main function to obtain template from gnunetd.
672  *
673  * @param argc number of arguments from the command line
674  * @param argv command line arguments
675  * @return 0 ok, 1 on error
676  */
677 int
678 main (int argc, char *const *argv) {
679     static const struct GNUNET_GETOPT_CommandLineOption options[] = {
680         GNUNET_GETOPT_OPTION_END
681     };
682
683     return (GNUNET_OK ==
684             GNUNET_PROGRAM_run (argc,
685                                 argv,
686                                 "gnunet-daemon-vpn",
687                                 gettext_noop ("help text"),
688                                 options, &run, NULL)) ? ret : 1;
689 }
690
691 /* end of gnunet-daemon-vpn.c */