Use the configures IP-addresses correctly
[oweals/gnunet.git] / src / vpn / gnunet-service-dns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file vpn/gnunet-service-dns.c
23  * @author Philipp Toelke
24  */
25 #include "platform.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_service_lib.h"
28 #include "gnunet_network_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-service-dns-p.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet-vpn-packet.h"
33 #include "gnunet-vpn-pretty-print.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet-dns-parser.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_block_lib.h"
38 #include "block_dns.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_signatures.h"
41
42 /**
43  * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
44  * sent through gnunet. The port of this socket will not be hijacked.
45  */
46 static struct GNUNET_NETWORK_Handle *dnsout;
47
48 /**
49  * The port bound to the socket dnsout
50  */
51 static unsigned short dnsoutport;
52
53 /**
54  * A handle to the DHT-Service
55  */
56 static struct GNUNET_DHT_Handle *dht;
57
58 /**
59  * The configuration to use
60  */
61 static const struct GNUNET_CONFIGURATION_Handle *cfg;
62
63 /**
64  * A list of DNS-Responses that have to be sent to the requesting client
65  */
66 static struct answer_packet_list *head;
67
68 /**
69  * The tail of the list of DNS-responses
70  */
71 static struct answer_packet_list *tail;
72
73 /**
74  * A structure containing a mapping from network-byte-ordered DNS-id to
75  * some information needed to handle this query
76  *
77  * It currently allocates at least
78  * (1 + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
79  * = 1.7 MiB on 64 bit.
80  * = 1.2 MiB on 32 bit.
81  */
82 static struct {
83     unsigned valid:1;
84     struct GNUNET_SERVER_Client* client;
85     unsigned local_ip:32;
86     unsigned remote_ip:32;
87     unsigned local_port:16;
88     char* name;
89     unsigned namelen:8;
90 } query_states[65536];
91
92 /**
93  * A struct used to give more than one value as
94  * closure to receive_dht
95  */
96 struct receive_dht_cls {
97     unsigned short id;
98     struct GNUNET_DHT_GetHandle* handle;
99 };
100
101 /**
102  * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
103  */
104 static void
105 hijack(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
106     char port_s[6];
107     char* virt_dns;
108
109     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "VIRTDNS", &virt_dns))
110       {
111         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'VIRTDNS' in configuration!\n");
112         exit(1);
113       }
114
115     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
116     snprintf(port_s, 6, "%d", dnsoutport);
117     GNUNET_OS_process_close (GNUNET_OS_start_process(NULL,
118                                                      NULL,
119                                                      "gnunet-helper-hijack-dns",
120                                                      "gnunet-hijack-dns",
121                                                      port_s,
122                                                      virt_dns,
123                                                      NULL));
124 }
125
126 /**
127  * Delete the hijacking-routes
128  */
129 static void
130 unhijack(unsigned short port) {
131     char port_s[6];
132     char* virt_dns;
133
134     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "VIRTDNS", &virt_dns))
135       {
136         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'VIRTDNS' in configuration!\n");
137         exit(1);
138       }
139
140     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
141     snprintf(port_s, 6, "%d", port);
142     GNUNET_OS_start_process(NULL,
143                             NULL,
144                             "gnunet-helper-hijack-dns",
145                             "gnunet-hijack-dns",
146                             "-d",
147                             port_s,
148                             virt_dns,
149                             NULL);
150 }
151
152 /**
153  * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
154  * system.
155  */
156 static size_t
157 send_answer(void* cls, size_t size, void* buf) {
158     struct answer_packet_list* query = head;
159     size_t 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     /* When more data is to be sent, reschedule */
170     if (head != NULL)
171       GNUNET_SERVER_notify_transmit_ready(cls,
172                                           ntohs(head->pkt.hdr.size),
173                                           GNUNET_TIME_UNIT_FOREVER_REL,
174                                           &send_answer,
175                                           cls);
176
177     return len;
178 }
179
180 static void
181 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
182     struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
183
184     unsigned short id = pdns->s.id;
185
186     if (query_states[id].valid != GNUNET_YES) return;
187     query_states[id].valid = GNUNET_NO;
188
189     GNUNET_assert(query_states[id].namelen == 74);
190
191     size_t len = sizeof(struct answer_packet) - 1 \
192                  + sizeof(struct dns_static) \
193                  + 74 /* this is the length of a reverse ipv6-lookup */ \
194                  + sizeof(struct dns_query_line) \
195                  + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
196                  + sizeof(struct dns_record_line) - 1 \
197                  - 2 /* We do not know the lenght of the answer yet*/ \
198                  - 2 /* No idea why... */ ;
199
200     struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
201     memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
202
203     answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
204     answer->pkt.hdr.size = htons(len);
205     answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
206
207     answer->pkt.from = query_states[id].remote_ip;
208
209     answer->pkt.to = query_states[id].local_ip;
210     answer->pkt.dst_port = query_states[id].local_port;
211
212     struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
213
214     dpkt->s.id = id;
215     dpkt->s.aa = 1;
216     dpkt->s.qr = 1;
217     dpkt->s.ra = 1;
218     dpkt->s.qdcount = htons(1);
219     dpkt->s.ancount = htons(1);
220
221     memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
222     GNUNET_free(query_states[id].name);
223
224     struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
225     dque->type = htons(12); /* PTR */
226     dque->class = htons(1); /* IN */
227
228     char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
229     memcpy(anname, (char[]){0xc0, 0x0c}, 2);
230
231     struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
232     drec_data->type = htons(12); /* AAAA */
233     drec_data->class = htons(1); /* IN */
234     drec_data->ttl = htonl(3600); /* FIXME: read from block */
235
236     /* Calculate at which offset in the packet the length of the name and the
237      * name, it is filled in by the daemon-vpn */
238     answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
239
240     GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
241
242     GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
243                                         len,
244                                         GNUNET_TIME_UNIT_FOREVER_REL,
245                                         &send_answer,
246                                         query_states[id].client);
247
248     /*
249      * build
250      * complete dns-packet with empty name in the answer
251      * provide offsett of the name
252      */
253 }
254
255 /**
256  * Receive a block from the dht.
257  */
258 static void
259 receive_dht(void *cls,
260             struct GNUNET_TIME_Absolute exp,
261             const GNUNET_HashCode *key,
262             const struct GNUNET_PeerIdentity *const *get_path,
263             const struct GNUNET_PeerIdentity *const *put_path,
264             enum GNUNET_BLOCK_Type type,
265             size_t size,
266             const void *data) {
267
268     unsigned short id = ((struct receive_dht_cls*)cls)->id;
269     struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
270     GNUNET_free(cls);
271
272     GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
273
274     /* If no query with this id is pending, ignore the block */
275     if (query_states[id].valid != GNUNET_YES) return;
276     query_states[id].valid = GNUNET_NO;
277
278     const struct GNUNET_DNS_Record* rec = data;
279     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
280                "Got block of size %d, peer: %08x, desc: %08x\n",
281                size,
282                *((unsigned int*)&rec->peer),
283                *((unsigned int*)&rec->service_descriptor));
284
285     size_t len = sizeof(struct answer_packet) - 1 \
286                  + sizeof(struct dns_static) \
287                  + query_states[id].namelen \
288                  + sizeof(struct dns_query_line) \
289                  + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
290                  + sizeof(struct dns_record_line) - 1 \
291                  + 16; /* To hold the IPv6-Address */
292
293     struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
294     memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
295
296     answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
297     answer->pkt.hdr.size = htons(len);
298     answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
299
300     GNUNET_CRYPTO_hash(&rec->peer,
301                        sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
302                        &answer->pkt.service_descr.peer);
303
304     memcpy(&answer->pkt.service_descr.service_descriptor,
305            &rec->service_descriptor,
306            sizeof(GNUNET_HashCode));
307     memcpy(&answer->pkt.service_descr.service_type,
308            &rec->service_type,
309            sizeof(answer->pkt.service_descr.service_type));
310     memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
311
312     answer->pkt.from = query_states[id].remote_ip;
313
314     answer->pkt.to = query_states[id].local_ip;
315     answer->pkt.dst_port = query_states[id].local_port;
316
317     struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
318
319     dpkt->s.id = id;
320     dpkt->s.aa = 1;
321     dpkt->s.qr = 1;
322     dpkt->s.ra = 1;
323     dpkt->s.qdcount = htons(1);
324     dpkt->s.ancount = htons(1);
325
326     memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
327     GNUNET_free(query_states[id].name);
328
329     struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
330     dque->type = htons(28); /* AAAA */
331     dque->class = htons(1); /* IN */
332
333     char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
334     memcpy(anname, (char[]){0xc0, 0x0c}, 2);
335
336     struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
337     drec_data->type = htons(28); /* AAAA */
338     drec_data->class = htons(1); /* IN */
339     drec_data->ttl = htonl(3600); /* FIXME: read from block */
340     drec_data->data_len = htons(16);
341
342     /* Calculate at which offset in the packet the IPv6-Address belongs, it is
343      * filled in by the daemon-vpn */
344     answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
345
346     GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
347
348     GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
349                                         len,
350                                         GNUNET_TIME_UNIT_FOREVER_REL,
351                                         &send_answer,
352                                         query_states[id].client);
353
354     GNUNET_DHT_get_stop(handle);
355 }
356
357 /**
358  * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
359  */
360 static void
361 rehijack(void *cls,
362          struct GNUNET_SERVER_Client *client,
363          const struct GNUNET_MessageHeader *message) {
364     unhijack(dnsoutport);
365     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
366
367     GNUNET_SERVER_receive_done(client, GNUNET_OK);
368 }
369
370 /**
371  * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
372  */
373 static void
374 receive_query(void *cls,
375               struct GNUNET_SERVER_Client *client,
376               const struct GNUNET_MessageHeader *message) {
377     struct query_packet* pkt = (struct query_packet*)message;
378     struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
379     struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
380
381     query_states[dns->s.id].valid = GNUNET_YES;
382     query_states[dns->s.id].client = client;
383     query_states[dns->s.id].local_ip = pkt->orig_from;
384     query_states[dns->s.id].local_port = pkt->src_port;
385     query_states[dns->s.id].remote_ip = pkt->orig_to;
386     query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
387     query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
388     memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
389
390     /* The query is for a .gnunet-address */
391     if (pdns->queries[0]->namelen > 9 &&
392         0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
393       {
394         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
395         GNUNET_HashCode key;
396         GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
397
398         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
399                    "Getting with key %08x, len is %d\n",
400                    *((unsigned int*)&key),
401                    pdns->queries[0]->namelen);
402
403         struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
404         cls->id = dns->s.id;
405
406         cls->handle = GNUNET_DHT_get_start(dht,
407                                            GNUNET_TIME_UNIT_MINUTES,
408                                            GNUNET_BLOCK_TYPE_DNS,
409                                            &key,
410                                            DEFAULT_GET_REPLICATION,
411                                            GNUNET_DHT_RO_NONE,
412                                            NULL,
413                                            0,
414                                            NULL,
415                                            0,
416                                            receive_dht,
417                                            cls);
418
419         goto outfree;
420       }
421
422     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
423     /* The query is for a PTR of a previosly resolved virtual IP */
424     if (htons(pdns->queries[0]->qtype) == 12 &&
425         74 == pdns->queries[0]->namelen)
426       {
427         char* ipv6addr;
428         char ipv6[16];
429         char ipv6rev[74] = "X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.ip6.arpa.";
430         unsigned int i;
431         unsigned long long ipv6prefix;
432         unsigned int comparelen;
433
434         GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
435         inet_pton (AF_INET6, ipv6addr, ipv6);
436         GNUNET_free(ipv6addr);
437
438         GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
439         GNUNET_assert(ipv6prefix < 127);
440         ipv6prefix = (ipv6prefix + 7)/8;
441
442         for (i = ipv6prefix; i < 16; i++)
443           ipv6[i] = 0;
444
445         for (i = 0; i < 16; i++)
446           {
447             unsigned char c1 = ipv6[i] >> 4;
448             unsigned char c2 = ipv6[i] & 0xf;
449
450             if (c1 <= 9)
451               ipv6rev[62-(4*i)] = c1 + '0';
452             else
453               ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
454
455             if (c2 <= 9)
456               ipv6rev[62-((4*i)+2)] = c2 + '0';
457             else
458               ipv6rev[62-((4*i)+2)] = c2 + 87;
459           }
460         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
461         comparelen = 10 + 4*ipv6prefix;
462         if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
463                         ipv6rev + (74 - comparelen),
464                         comparelen))
465           {
466             GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
467
468             GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
469
470             goto out;
471           }
472       }
473
474     /* The query should be sent to the network */
475
476     struct sockaddr_in dest;
477     memset(&dest, 0, sizeof dest);
478     dest.sin_port = htons(53);
479     dest.sin_addr.s_addr = pkt->orig_to;
480
481     GNUNET_NETWORK_socket_sendto(dnsout,
482                                  dns,
483                                  ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
484                                  (struct sockaddr*) &dest,
485                                  sizeof dest);
486
487 outfree:
488     free_parsed_dns_packet(pdns);
489     pdns = NULL;
490 out:
491     GNUNET_SERVER_receive_done(client, GNUNET_OK);
492 }
493
494 /**
495  * Read a response-packet of the UDP-Socket
496  */
497 static void
498 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
499     unsigned char buf[65536];
500     struct dns_pkt* dns = (struct dns_pkt*)buf;
501
502     if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
503       return;
504
505     struct sockaddr_in addr;
506     memset(&addr, 0, sizeof addr);
507     socklen_t addrlen = sizeof addr;
508
509     int r;
510     r = GNUNET_NETWORK_socket_recvfrom(dnsout,
511                                        buf,
512                                        65536,
513                                        (struct sockaddr*)&addr,
514                                        &addrlen);
515
516     /* if (r < 0) FIXME */
517
518     if (query_states[dns->s.id].valid == GNUNET_YES) {
519         query_states[dns->s.id].valid = GNUNET_NO;
520
521         size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
522         struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
523         answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
524         answer->pkt.hdr.size = htons(len);
525         answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
526         answer->pkt.from = addr.sin_addr.s_addr;
527         answer->pkt.to = query_states[dns->s.id].local_ip;
528         answer->pkt.dst_port = query_states[dns->s.id].local_port;
529         memcpy(answer->pkt.data, buf, r);
530
531         GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
532
533         GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client,
534                                             len,
535                                             GNUNET_TIME_UNIT_FOREVER_REL,
536                                             &send_answer,
537                                             query_states[dns->s.id].client);
538     }
539
540     GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
541                                   dnsout,
542                                   &read_response,
543                                   NULL);
544 }
545
546
547 /**
548  * Task run during shutdown.
549  *
550  * @param cls unused
551  * @param tc unused
552  */
553 static void
554 cleanup_task (void *cls,
555               const struct GNUNET_SCHEDULER_TaskContext *tc)
556 {
557   unhijack(dnsoutport);
558   GNUNET_DHT_disconnect(dht);
559 }
560
561 /**
562  * Publish a DNS-record in the DHT. This is up to now just for testing.
563  */
564 static void
565 publish_name (void *cls,
566               const struct GNUNET_SCHEDULER_TaskContext *tc) {
567     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
568       return;
569
570     char* name = "philipptoelke.gnunet.";
571     size_t size = sizeof(struct GNUNET_DNS_Record);
572     struct GNUNET_DNS_Record data;
573     memset(&data, 0, size);
574
575     data.purpose.size = htonl(size - sizeof(struct GNUNET_CRYPTO_RsaSignature));
576     data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
577
578     GNUNET_CRYPTO_hash(name, strlen(name)+1, &data.service_descriptor);
579
580     data.service_type = htonl(GNUNET_DNS_SERVICE_TYPE_UDP);
581     data.ports = htons(69);
582
583     char* keyfile;
584     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, "GNUNETD",
585                                                              "HOSTKEY", &keyfile))
586       {
587         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
588         if (keyfile != NULL) GNUNET_free(keyfile);
589         return;
590       }
591
592     struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file(keyfile);
593     GNUNET_free(keyfile);
594     GNUNET_assert(my_private_key != NULL);
595
596     GNUNET_CRYPTO_rsa_key_get_public(my_private_key, &data.peer);
597
598     data.expiration_time = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS);
599
600   /* Sign the block */
601     if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign(my_private_key,
602                                             &data.purpose,
603                                             &data.signature))
604       {
605         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
606         return;
607       }
608     GNUNET_CRYPTO_rsa_key_free(my_private_key);
609
610     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
611                "Putting with key %08x, size = %d\n",
612                *((unsigned int*)&data.service_descriptor),
613                size);
614
615     GNUNET_DHT_put(dht,
616                    &data.service_descriptor,
617                    DEFAULT_PUT_REPLICATION,
618                    GNUNET_DHT_RO_NONE,
619                    GNUNET_BLOCK_TYPE_DNS,
620                    size,
621                    (char*)&data,
622                    GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
623                    GNUNET_TIME_UNIT_MINUTES,
624                    NULL,
625                    NULL);
626
627     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
628                                   publish_name,
629                                   NULL);
630 }
631
632 /**
633  * @param cls closure
634  * @param server the initialized server
635  * @param cfg_ configuration to use
636  */
637 static void
638 run (void *cls,
639      struct GNUNET_SERVER_Handle *server,
640      const struct GNUNET_CONFIGURATION_Handle *cfg_)
641 {
642   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
643       /* callback, cls, type, size */
644         {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
645         {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, sizeof(struct GNUNET_MessageHeader)},
646         {NULL, NULL, 0, 0}
647   };
648
649   cfg = cfg_;
650
651   unsigned int i;
652   for (i = 0; i < 65536; i++) {
653       query_states[i].valid = GNUNET_NO;
654   }
655
656   dht = GNUNET_DHT_connect(cfg, 1024);
657
658   struct sockaddr_in addr;
659
660   dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
661   if (dnsout == NULL)
662     return;
663   memset(&addr, 0, sizeof(struct sockaddr_in));
664
665   int err = GNUNET_NETWORK_socket_bind (dnsout,
666                                         (struct sockaddr*)&addr,
667                                         sizeof(struct sockaddr_in));
668
669   if (err != GNUNET_YES) {
670       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
671       return;
672   }
673
674   /* Read the port we bound to */
675   socklen_t addrlen = sizeof(struct sockaddr_in);
676   err = getsockname(GNUNET_NETWORK_get_fd(dnsout),
677                     (struct sockaddr*) &addr,
678                     &addrlen);
679
680   dnsoutport = htons(addr.sin_port);
681
682   GNUNET_SCHEDULER_add_now (publish_name, NULL);
683
684   GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, dnsout, &read_response, NULL);
685
686   GNUNET_SERVER_add_handlers (server, handlers);
687   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
688                                 &cleanup_task,
689                                 cls);
690 }
691
692 /**
693  * The main function for the dns service.
694  *
695  * @param argc number of arguments from the command line
696  * @param argv command line arguments
697  * @return 0 ok, 1 on error
698  */
699 int
700 main (int argc, char *const *argv)
701 {
702   return (GNUNET_OK ==
703           GNUNET_SERVICE_run (argc,
704                               argv,
705                               "dns",
706                               GNUNET_SERVICE_OPTION_NONE,
707                               &run, NULL)) ? 0 : 1;
708 }