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