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