+/**
+ * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
+ */
+static void
+receive_query(void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message) {
+ struct query_packet* pkt = (struct query_packet*)message;
+ struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
+ struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
+
+ query_states[dns->s.id].valid = GNUNET_YES;
+ query_states[dns->s.id].client = client;
+ query_states[dns->s.id].local_ip = pkt->orig_from;
+ query_states[dns->s.id].local_port = pkt->src_port;
+ query_states[dns->s.id].remote_ip = pkt->orig_to;
+ query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
+ query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
+ memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
+
+ /* The query is for a .gnunet-address */
+ if (pdns->queries[0]->namelen > 9 &&
+ 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
+ GNUNET_HashCode key;
+ GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
+
+ GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+ "Getting with key %08x, len is %d\n",
+ *((unsigned int*)&key),
+ pdns->queries[0]->namelen);
+
+ struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
+ cls->id = dns->s.id;
+
+ cls->handle = GNUNET_DHT_get_start(dht,
+ GNUNET_TIME_UNIT_MINUTES,
+ GNUNET_BLOCK_TYPE_DNS,
+ &key,
+ DEFAULT_GET_REPLICATION,
+ GNUNET_DHT_RO_NONE,
+ NULL,
+ 0,
+ NULL,
+ 0,
+ receive_dht,
+ cls);
+
+ goto outfree;
+ }
+
+ /* The query is for a PTR of a previosly resolved virtual IP */
+ if (htons(pdns->queries[0]->qtype) == 12 &&
+ pdns->queries[0]->namelen > 19 &&
+ 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 19), ".4.3.2.1.ip6.arpa.", 19))
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
+
+ GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
+
+ goto out;
+ }
+
+ /* The query should be sent to the network */
+
+ struct sockaddr_in dest;
+ memset(&dest, 0, sizeof dest);
+ dest.sin_port = htons(53);
+ dest.sin_addr.s_addr = pkt->orig_to;
+
+ GNUNET_NETWORK_socket_sendto(dnsout,
+ dns,
+ ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
+ (struct sockaddr*) &dest,
+ sizeof dest);
+
+outfree:
+ free_parsed_dns_packet(pdns);
+ pdns = NULL;
+out:
+ GNUNET_SERVER_receive_done(client, GNUNET_OK);
+}
+
+/**
+ * Read a response-packet of the UDP-Socket
+ */
+static void
+read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
+ unsigned char buf[65536];
+ struct dns_pkt* dns = (struct dns_pkt*)buf;
+
+ if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
+ return;
+
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof addr);
+ socklen_t addrlen = sizeof addr;
+
+ int r;
+ r = GNUNET_NETWORK_socket_recvfrom(dnsout,
+ buf,
+ 65536,
+ (struct sockaddr*)&addr,
+ &addrlen);
+
+ /* if (r < 0) FIXME */
+
+ if (query_states[dns->s.id].valid == GNUNET_YES) {
+ query_states[dns->s.id].valid = GNUNET_NO;
+
+ size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
+ struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
+ answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
+ answer->pkt.hdr.size = htons(len);
+ answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
+ answer->pkt.from = addr.sin_addr.s_addr;
+ answer->pkt.to = query_states[dns->s.id].local_ip;
+ answer->pkt.dst_port = query_states[dns->s.id].local_port;
+ memcpy(answer->pkt.data, buf, r);
+
+ GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
+
+ 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);
+ }
+
+ GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
+ dnsout,
+ &read_response,
+ NULL);