+/**
+ * Receive a block from the dht.
+ */
+static void
+receive_dht(void *cls,
+ struct GNUNET_TIME_Absolute exp,
+ const GNUNET_HashCode *key,
+ const struct GNUNET_PeerIdentity *const *get_path,
+ const struct GNUNET_PeerIdentity *const *put_path,
+ enum GNUNET_BLOCK_Type type,
+ size_t size,
+ const void *data) {
+
+ unsigned short id = ((struct receive_dht_cls*)cls)->id;
+ struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
+ GNUNET_free(cls);
+
+ GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
+
+ /* If no query with this id is pending, ignore the block */
+ if (query_states[id].valid != GNUNET_YES) return;
+ query_states[id].valid = GNUNET_NO;
+
+ const struct GNUNET_DNS_Record* rec = data;
+ 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));
+
+ size_t len = sizeof(struct answer_packet) - 1 \
+ + sizeof(struct dns_static) \
+ + query_states[id].namelen \
+ + sizeof(struct dns_query_line) \
+ + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
+ + sizeof(struct dns_record_line) - 1 \
+ + 16; /* To hold the IPv6-Address */
+
+ struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
+ memset(answer, 0, 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_SERVICE;
+
+ GNUNET_CRYPTO_hash(&rec->peer,
+ sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
+ &answer->pkt.service_descr.peer);
+
+ memcpy(&answer->pkt.service_descr.service_descriptor,
+ &rec->service_descriptor,
+ sizeof(GNUNET_HashCode));
+ memcpy(&answer->pkt.service_descr.service_type,
+ &rec->service_type,
+ sizeof(answer->pkt.service_descr.service_type));
+ memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
+
+ answer->pkt.from = query_states[id].remote_ip;
+
+ answer->pkt.to = query_states[id].local_ip;
+ answer->pkt.dst_port = query_states[id].local_port;
+
+ struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
+
+ dpkt->s.id = id;
+ dpkt->s.aa = 1;
+ dpkt->s.qr = 1;
+ dpkt->s.ra = 1;
+ dpkt->s.qdcount = htons(1);
+ dpkt->s.ancount = htons(1);
+
+ memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
+ GNUNET_free(query_states[id].name);
+
+ struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
+ dque->type = htons(28); /* AAAA */
+ dque->class = htons(1); /* IN */
+
+ char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
+ memcpy(anname, (char[]){0xc0, 0x0c}, 2);
+
+ struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
+ drec_data->type = htons(28); /* AAAA */
+ drec_data->class = htons(1); /* IN */
+ drec_data->ttl = htonl(3600); /* FIXME: read from block */
+ drec_data->data_len = htons(16);
+
+ /* Calculate at which offset in the packet the IPv6-Address belongs, it is
+ * filled in by the daemon-vpn */
+ answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
+
+ GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
+
+ GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
+ len,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ &send_answer,
+ query_states[id].client);
+
+ GNUNET_DHT_get_stop(handle);
+}
+
+/**
+ * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
+ */
+static void
+rehijack(void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message) {
+ unhijack(dnsoutport);
+ GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
+
+ GNUNET_SERVER_receive_done(client, GNUNET_OK);
+}
+
+/**
+ * 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);
+}
+
+