2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file vpn/gnunet-service-dns.c
23 * @author Philipp Toelke
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"
43 struct GNUNET_SCHEDULER_Handle *sched;
45 struct GNUNET_NETWORK_Handle *dnsout;
47 struct GNUNET_DHT_Handle *dht;
49 unsigned short dnsoutport;
51 const struct GNUNET_CONFIGURATION_Handle *cfg;
53 struct answer_packet_list *head;
54 struct answer_packet_list *tail;
56 static struct dns_cls mycls;
58 struct dns_query_id_state {
60 struct GNUNET_SERVER_Client* client;
62 unsigned remote_ip:32;
63 unsigned local_port:16;
65 static struct dns_query_id_state query_states[65536]; /* This is < 1.5MiB */
67 void hijack(unsigned short port) {
70 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", port);
71 snprintf(port_s, 6, "%d", port);
72 GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", port_s, NULL);
75 void unhijack(unsigned short port) {
78 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
79 snprintf(port_s, 6, "%d", port);
80 GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
83 size_t send_answer(void* cls, size_t size, void* buf);
85 struct receive_dht_cls {
87 struct GNUNET_DHT_GetHandle* handle;
90 void receive_dht(void *cls,
91 struct GNUNET_TIME_Absolute exp,
92 const GNUNET_HashCode *key,
93 const struct GNUNET_PeerIdentity *const *get_path,
94 const struct GNUNET_PeerIdentity *const *put_path,
95 enum GNUNET_BLOCK_Type type,
99 unsigned short id = ((struct receive_dht_cls*)cls)->id;
100 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
103 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
105 if (query_states[id].valid != GNUNET_YES) return;
106 query_states[id].valid = GNUNET_NO;
108 const struct GNUNET_DNS_Record* rec = data;
109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got block of size %d, peer: %08x, desc: %08x\n", size, *((unsigned int*)&rec->peer), *((unsigned int*)&rec->service_descriptor));
111 size_t len = sizeof(struct answer_packet) + size - 1; /* 1 for the unsigned char data[1]; */
112 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
113 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
114 answer->pkt.hdr.size = htons(len);
115 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
117 answer->pkt.from = query_states[id].remote_ip;
119 answer->pkt.to = query_states[id].local_ip;
120 answer->pkt.dst_port = query_states[id].local_port;
124 memcpy(answer->pkt.data, data, size);
126 GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
128 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
130 GNUNET_TIME_UNIT_FOREVER_REL,
132 query_states[id].client);
134 GNUNET_DHT_get_stop(handle);
138 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
140 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
142 struct query_packet* pkt = (struct query_packet*)message;
143 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
144 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
146 query_states[dns->s.id].valid = GNUNET_YES;
147 query_states[dns->s.id].client = client;
148 query_states[dns->s.id].local_ip = pkt->orig_from;
149 query_states[dns->s.id].local_port = pkt->src_port;
150 query_states[dns->s.id].remote_ip = pkt->orig_to;
152 if (pdns->queries[0]->namelen > 9 &&
153 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9)) {
155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
157 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n", *((unsigned int*)&key), pdns->queries[0]->namelen);
161 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
164 cls->handle = GNUNET_DHT_get_start(mycls.dht,
165 GNUNET_TIME_UNIT_MINUTES,
166 GNUNET_BLOCK_TYPE_DNS,
179 /* The query should be sent to the network */
182 struct sockaddr_in dest;
183 memset(&dest, 0, sizeof dest);
184 dest.sin_port = htons(53);
185 dest.sin_addr.s_addr = pkt->orig_to;
187 /* int r = */ GNUNET_NETWORK_socket_sendto(mycls.dnsout, dns, ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1, (struct sockaddr*) &dest, sizeof dest);
190 GNUNET_SERVER_receive_done(client, GNUNET_OK);
193 size_t send_answer(void* cls, size_t size, void* buf) {
194 struct answer_packet_list* query = mycls.head;
195 size_t len = ntohs(query->pkt.hdr.size);
197 GNUNET_assert(len <= size);
199 memcpy(buf, &query->pkt.hdr, len);
201 GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
205 if (mycls.head != NULL) {
206 GNUNET_SERVER_notify_transmit_ready(cls, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, cls);
212 static void read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
213 unsigned char buf[65536];
214 struct dns_pkt* dns = (struct dns_pkt*)buf;
216 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
219 struct sockaddr_in addr;
220 memset(&addr, 0, sizeof addr);
221 unsigned int addrlen = sizeof addr;
224 r = GNUNET_NETWORK_socket_recvfrom(mycls.dnsout, buf, 65536, (struct sockaddr*)&addr, &addrlen);
226 /* if (r < 0) TODO */
228 if (query_states[dns->s.id].valid == GNUNET_YES) {
229 query_states[dns->s.id].valid = GNUNET_NO;
231 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
232 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
233 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
234 answer->pkt.hdr.size = htons(len);
235 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
236 answer->pkt.from = addr.sin_addr.s_addr;
237 answer->pkt.to = query_states[dns->s.id].local_ip;
238 answer->pkt.dst_port = query_states[dns->s.id].local_port;
239 memcpy(answer->pkt.data, buf, r);
241 GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
243 /* struct GNUNET_CONNECTION_TransmitHandle* th = */ GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client, len, GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, query_states[dns->s.id].client);
246 GNUNET_SCHEDULER_add_read_net(mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
251 * Task run during shutdown.
257 cleanup_task (void *cls,
258 const struct GNUNET_SCHEDULER_TaskContext *tc)
260 unhijack(mycls.dnsoutport);
261 GNUNET_DHT_disconnect(mycls.dht);
265 publish_name (void *cls,
266 const struct GNUNET_SCHEDULER_TaskContext *tc)
268 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
271 char* name = "philipptoelke.gnunet.";
272 size_t size = sizeof(struct GNUNET_DNS_Record);
273 struct GNUNET_DNS_Record data;
274 memset(&data, 0, size);
276 data.purpose.size = htonl(size - sizeof(struct GNUNET_CRYPTO_RsaSignature));
277 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
279 GNUNET_CRYPTO_hash(name, strlen(name)+1, &data.service_descriptor);
282 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(mycls.cfg, "GNUNETD",
283 "HOSTKEY", &keyfile))
285 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
286 if (keyfile != NULL) GNUNET_free(keyfile);
290 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file(keyfile);
291 GNUNET_free(keyfile);
293 GNUNET_CRYPTO_rsa_key_get_public(my_private_key, &data.peer);
295 data.expiration_time = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS);
300 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign(my_private_key,
304 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
307 GNUNET_free(my_private_key);
309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x\n", *((unsigned int*)&data.service_descriptor));
311 GNUNET_DHT_put(mycls.dht,
312 &data.service_descriptor,
314 GNUNET_BLOCK_TYPE_DNS,
317 GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
318 GNUNET_TIME_UNIT_MINUTES,
322 GNUNET_SCHEDULER_add_delayed (mycls.sched, GNUNET_TIME_UNIT_HOURS, publish_name, NULL);
327 * @param sched scheduler to use
328 * @param server the initialized server
329 * @param cfg configuration to use
333 struct GNUNET_SCHEDULER_Handle *sched,
334 struct GNUNET_SERVER_Handle *server,
335 const struct GNUNET_CONFIGURATION_Handle *cfg)
337 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
338 /* callback, cls, type, size */
339 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
347 for (i = 0; i < 65536; i++) {
348 query_states[i].valid = GNUNET_NO;
352 mycls.dht = GNUNET_DHT_connect(sched, cfg, 1024);
354 struct sockaddr_in addr;
357 mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
358 if (mycls.dnsout == NULL)
360 memset(&addr, 0, sizeof(struct sockaddr_in));
362 int err = GNUNET_NETWORK_socket_bind (mycls.dnsout,
363 (struct sockaddr*)&addr,
364 sizeof(struct sockaddr_in));
366 if (err != GNUNET_YES) {
367 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
370 socklen_t addrlen = sizeof(struct sockaddr_in);
371 err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout),
372 (struct sockaddr*) &addr,
375 mycls.dnsoutport = htons(addr.sin_port);
377 hijack(htons(addr.sin_port));
379 GNUNET_SCHEDULER_add_now (mycls.sched, publish_name, NULL);
381 GNUNET_SCHEDULER_add_read_net(sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
383 GNUNET_SERVER_add_handlers (server, handlers);
384 GNUNET_SCHEDULER_add_delayed (sched,
385 GNUNET_TIME_UNIT_FOREVER_REL,
391 * The main function for the dns service.
393 * @param argc number of arguments from the command line
394 * @param argv command line arguments
395 * @return 0 ok, 1 on error
398 main (int argc, char *const *argv)
401 GNUNET_SERVICE_run (argc,
404 GNUNET_SERVICE_OPTION_NONE,
405 &run, NULL)) ? 0 : 1;