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