#include "gnunet_os_lib.h"
#include "gnunet-service-dns-p.h"
#include "gnunet_protocols.h"
+#include "gnunet_applications.h"
#include "gnunet-vpn-packet.h"
-#include "gnunet-vpn-pretty-print.h"
#include "gnunet_container_lib.h"
#include "gnunet-dns-parser.h"
#include "gnunet_dht_service.h"
#include "gnunet_block_lib.h"
#include "block_dns.h"
#include "gnunet_crypto_lib.h"
+#include "gnunet_mesh_service.h"
#include "gnunet_signatures.h"
+struct GNUNET_MESH_Handle *mesh_handle;
+
/**
* The UDP-Socket through which DNS-Resolves will be sent if they are not to be
* sent through gnunet. The port of this socket will not be hijacked.
*/
static const struct GNUNET_CONFIGURATION_Handle *cfg;
+/**
+ * The handle to the service-configuration
+ */
+static struct GNUNET_CONFIGURATION_Handle *servicecfg;
+
/**
* A list of DNS-Responses that have to be sent to the requesting client
*/
static struct answer_packet_list *tail;
/**
- * A structure containing a mapping from network-byte-ordered DNS-id to
+ * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
* some information needed to handle this query
*
* It currently allocates at least
static struct {
unsigned valid:1;
struct GNUNET_SERVER_Client* client;
- unsigned local_ip:32;
- unsigned remote_ip:32;
- unsigned local_port:16;
+ uint32_t local_ip;
+ uint32_t remote_ip;
+ uint16_t local_port;
char* name;
- unsigned namelen:8;
-} query_states[65536];
+ uint8_t namelen;
+} query_states[UINT16_MAX];
/**
* A struct used to give more than one value as
* closure to receive_dht
*/
struct receive_dht_cls {
- unsigned short id;
+ uint16_t id;
struct GNUNET_DHT_GetHandle* handle;
};
return len;
}
+struct tunnel_cls {
+ struct GNUNET_MESH_Tunnel *tunnel;
+ struct GNUNET_MessageHeader hdr;
+ struct dns_pkt dns;
+};
+
+struct tunnel_cls *remote_pending[UINT16_MAX];
+
+
+static size_t
+mesh_send (void *cls, size_t size, void *buf)
+{
+ struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
+
+ GNUNET_assert(cls_->hdr.size <= size);
+
+ size = cls_->hdr.size;
+ cls_->hdr.size = htons(cls_->hdr.size);
+
+ memcpy(buf, &cls_->hdr, size);
+ return size;
+}
+
+
+void mesh_connect (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi) {
+ GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %x\n", *((unsigned long*)peer));
+ struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
+
+ GNUNET_MESH_notify_transmit_ready(cls_->tunnel,
+ GNUNET_YES,
+ 42,
+ GNUNET_TIME_UNIT_MINUTES,
+ NULL,
+ cls_->hdr.size,
+ mesh_send,
+ cls);
+}
+
+
+static void
+send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ return;
+
+ struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
+
+ cls_->tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
+ GNUNET_TIME_UNIT_HOURS,
+ GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
+ mesh_connect,
+ NULL,
+ cls_);
+
+ remote_pending[cls_->dns.s.id] = cls_;
+}
+
+static int
+receive_mesh_query (void *cls,
+ struct GNUNET_MESH_Tunnel *tunnel,
+ void **ctx,
+ const struct GNUNET_PeerIdentity *sender,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_TRANSPORT_ATS_Information *atsi)
+{
+ return GNUNET_SYSERR;
+}
+
+static int
+receive_mesh_answer (void *cls,
+ struct GNUNET_MESH_Tunnel *tunnel,
+ void **ctx,
+ const struct GNUNET_PeerIdentity *sender,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_TRANSPORT_ATS_Information *atsi)
+{
+ /* TODo: size check */
+ struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
+
+ /* They sent us a packet we were not waiting for */
+ if (remote_pending[dns->s.id] == NULL
+ || remote_pending[dns->s.id]->tunnel != tunnel)
+ return GNUNET_SYSERR;
+ if (query_states[dns->s.id].valid != GNUNET_YES)
+ return GNUNET_SYSERR;
+ query_states[dns->s.id].valid = GNUNET_NO;
+
+ size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[dns->s.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_REMOTE;
+
+ struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
+
+ if (ntohs(pdns->s.ancount) < 1)
+ {
+ free_parsed_dns_packet(pdns);
+ return GNUNET_OK;
+ }
+
+ answer->pkt.addrsize = pdns->answers[0]->data_len;
+ memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len));
+
+ answer->pkt.from = query_states[dns->s.id].remote_ip;
+
+ answer->pkt.to = query_states[dns->s.id].local_ip;
+ answer->pkt.dst_port = query_states[dns->s.id].local_port;
+
+ struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
+
+ dpkt->s.id = dns->s.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[dns->s.id].name,
+ query_states[dns->s.id].namelen);
+ GNUNET_free (query_states[dns->s.id].name);
+
+ struct dns_query_line *dque =
+ (struct dns_query_line *) (dpkt->data +
+ (query_states[dns->s.id].namelen));
+ dque->type = htons (28); /* AAAA */
+ dque->class = htons (1); /* IN */
+
+ char *anname =
+ (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
+ sizeof (struct dns_query_line));
+ memcpy (anname, "\xc0\x0c", 2);
+
+ struct dns_record_line *drec_data =
+ (struct dns_record_line *) (dpkt->data +
+ (query_states[dns->s.id].namelen) +
+ sizeof (struct dns_query_line) + 2);
+ drec_data->type = htons (28); /* AAAA */
+ drec_data->class = htons (1); /* IN */
+
+ drec_data->ttl = pdns->answers[0]->ttl;
+ 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[dns->s.id].client,
+ len,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ &send_answer,
+ query_states[dns->s.id].client);
+
+ free_parsed_dns_packet(pdns);
+ return GNUNET_OK;
+}
+
+
static void
send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ return;
+
struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
unsigned short id = pdns->s.id;
+ free_parsed_dns_packet(pdns);
+
if (query_states[id].valid != GNUNET_YES) return;
query_states[id].valid = GNUNET_NO;
+ sizeof(struct dns_query_line) \
+ 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
+ sizeof(struct dns_record_line) - 1 \
- - 2 /* We do not know the lenght of the answer yet*/ \
- - 2 /* No idea why... */ ;
+ - 2 /* We do not know the lenght of the answer yet*/;
struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
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);
+ memcpy(anname, "\xc0\x0c", 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(12); /* AAAA */
drec_data->class = htons(1); /* IN */
- drec_data->ttl = htonl(3600); /* FIXME: read from block */
+ /* FIXME: read the TTL from block:
+ * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
+ *
+ * But how to get the seconds out of this?
+ */
+ drec_data->ttl = htonl(3600);
/* Calculate at which offset in the packet the length of the name and the
* name, it is filled in by the daemon-vpn */
GNUNET_TIME_UNIT_FOREVER_REL,
&send_answer,
query_states[id].client);
-
- /*
- * build
- * complete dns-packet with empty name in the answer
- * provide offsett of the name
- */
}
/**
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);
+ memcpy(anname, "\xc0\x0c", 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 */
+
+ /* FIXME: read the TTL from block:
+ * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
+ *
+ * But how to get the seconds out of this?
+ */
+ drec_data->ttl = htonl(3600);
drec_data->data_len = htons(16);
/* Calculate at which offset in the packet the IPv6-Address belongs, it is
}
GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
- /* The query is for a PTR of a previosly resolved virtual IP */
+
+ /* This is a PTR-Query. Check if it is for "our" network */
if (htons(pdns->queries[0]->qtype) == 12 &&
74 == pdns->queries[0]->namelen)
{
}
}
+ char* virt_dns;
+ int virt_dns_bytes;
+ if (GNUNET_SYSERR ==
+ GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
+ &virt_dns))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "No entry 'VIRTDNS' in configuration!\n");
+ exit (1);
+ }
+
+ if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
+ exit(1);
+ }
+
+ GNUNET_free(virt_dns);
+
+ if (virt_dns_bytes == pkt->orig_to)
+ {
+ /* This is a packet that was sent directly to the virtual dns-server
+ *
+ * This means we have to send this query over gnunet
+ */
+
+ size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1);
+ struct tunnel_cls *cls_ = GNUNET_malloc(size);
+ cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);
+
+ cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS);
+
+ memcpy(&cls_->dns, dns, cls_->hdr.size);
+ GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);
+
+ goto out;
+ }
+
+
/* The query should be sent to the network */
struct sockaddr_in dest;
GNUNET_SERVER_receive_done(client, GNUNET_OK);
}
+static void read_response (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+static void
+open_port ()
+{
+ struct sockaddr_in addr;
+
+ dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
+ if (dnsout == NULL)
+ return;
+ memset (&addr, 0, sizeof (struct sockaddr_in));
+
+ int err = GNUNET_NETWORK_socket_bind (dnsout,
+ (struct sockaddr *) &addr,
+ sizeof (struct sockaddr_in));
+
+ if (err != GNUNET_YES)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Could not bind a port, exiting\n");
+ return;
+ }
+
+ /* Read the port we bound to */
+ socklen_t addrlen = sizeof (struct sockaddr_in);
+ err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
+ (struct sockaddr *) &addr, &addrlen);
+
+ dnsoutport = htons (addr.sin_port);
+
+ GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
+ &read_response, NULL);
+}
+
/**
* 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;
+ struct sockaddr_in addr;
+ socklen_t addrlen = sizeof (addr);
+ int r;
+ int len;
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);
+#ifndef MINGW
+ if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout),
+ FIONREAD, &len))
+ {
+ unhijack(dnsoutport);
+ open_port();
+ GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
+ return;
+ }
+#else
+ /* port the code above? */
+ len = 65536;
+#endif
+ {
+ unsigned char buf[len];
+ struct dns_pkt* dns = (struct dns_pkt*)buf;
+
+ r = GNUNET_NETWORK_socket_recvfrom (dnsout,
+ buf,
+ sizeof (buf),
+ (struct sockaddr*)&addr,
+ &addrlen);
+
+ if (r < 0)
+ {
+ unhijack(dnsoutport);
+ open_port();
+ GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
+ return;
+ }
+
+ 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,
}
/**
- * Publish a DNS-record in the DHT. This is up to now just for testing.
+ * @brief Create a port-map from udp and tcp redirects
+ *
+ * @param udp_redirects
+ * @param tcp_redirects
+ *
+ * @return
*/
-static void
-publish_name (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc) {
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
+uint64_t
+get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
+{
+ uint64_t ret = 0;
+ char* cpy, *hostname, *redirect;
+ int local_port, count = 0;
+
+ if (NULL != udp_redirects)
+ {
+ cpy = GNUNET_strdup (udp_redirects);
+ for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
+ {
+ if (NULL == (hostname = strstr (redirect, ":")))
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
+ continue;
+ }
+ hostname[0] = '\0';
+ local_port = atoi (redirect);
+ if (!((local_port > 0) && (local_port < 65536)))
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
+
+ ret |= (0xFFFF & htons(local_port));
+ ret <<= 16;
+ count ++;
+
+ if(count > 4)
+ {
+ ret = 0;
+ goto out;
+ }
+ }
+ GNUNET_free(cpy);
+ cpy = NULL;
+ }
- char* name = "philipptoelke.gnunet.";
- size_t size = sizeof(struct GNUNET_DNS_Record);
- struct GNUNET_DNS_Record data;
- memset(&data, 0, size);
+ if (NULL != tcp_redirects)
+ {
+ cpy = GNUNET_strdup (tcp_redirects);
+ for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
+ {
+ if (NULL == (hostname = strstr (redirect, ":")))
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
+ continue;
+ }
+ hostname[0] = '\0';
+ local_port = atoi (redirect);
+ if (!((local_port > 0) && (local_port < 65536)))
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
+
+ ret |= (0xFFFF & htons(local_port));
+ ret <<= 16;
+ count ++;
+
+ if(count > 4)
+ {
+ ret = 0;
+ goto out;
+ }
+ }
+ GNUNET_free(cpy);
+ cpy = NULL;
+ }
- data.purpose.size = htonl(size - sizeof(struct GNUNET_CRYPTO_RsaSignature));
- data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
+out:
+ if (NULL != cpy)
+ GNUNET_free(cpy);
+ return ret;
+}
- GNUNET_CRYPTO_hash(name, strlen(name)+1, &data.service_descriptor);
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n", *((unsigned long long*)&data.service_descriptor));
+void
+publish_name (const char *name, uint64_t ports, uint32_t service_type,
+ struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
+{
+ size_t size = sizeof (struct GNUNET_DNS_Record);
+ struct GNUNET_DNS_Record data;
+ memset (&data, 0, size);
- data.service_type = htonl(GNUNET_DNS_SERVICE_TYPE_UDP);
- data.ports = htons(69);
+ data.purpose.size =
+ htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
+ data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
- char* keyfile;
- if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, "GNUNETD",
- "HOSTKEY", &keyfile))
- {
- GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
- if (keyfile != NULL) GNUNET_free(keyfile);
- return;
- }
+ GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
+ *((unsigned long long *) &data.service_descriptor));
- struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file(keyfile);
- GNUNET_free(keyfile);
- GNUNET_assert(my_private_key != NULL);
+ data.service_type = service_type;
+ data.ports = ports;
- GNUNET_CRYPTO_rsa_key_get_public(my_private_key, &data.peer);
+ GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
- data.expiration_time = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS);
+ data.expiration_time =
+ GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
/* Sign the block */
- if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign(my_private_key,
- &data.purpose,
- &data.signature))
+ if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
+ &data.purpose, &data.signature))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
+ return;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Putting with key %08x, size = %d\n",
+ *((unsigned int *) &data.service_descriptor), size);
+
+ GNUNET_DHT_put (dht,
+ &data.service_descriptor,
+ DEFAULT_PUT_REPLICATION,
+ GNUNET_DHT_RO_NONE,
+ GNUNET_BLOCK_TYPE_DNS,
+ size,
+ (char *) &data,
+ GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
+ GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
+}
+
+/**
+ * @brief Publishes the record defined by the section section
+ *
+ * @param cls closure
+ * @param section the current section
+ */
+void
+publish_iterate (void *cls, const char *section)
+{
+ char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
+ *keyfile;
+
+ GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
+ "UDP_REDIRECTS", &udp_redirects);
+ GNUNET_CONFIGURATION_get_value_string (servicecfg, section, "TCP_REDIRECTS",
+ &tcp_redirects);
+
+ if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
+ "HOSTKEY",
+ &keyfile))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
+ if (keyfile != NULL)
+ GNUNET_free (keyfile);
+ return;
+ }
+
+ struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
+ GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
+ GNUNET_free (keyfile);
+ GNUNET_assert (my_private_key != NULL);
+
+ uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
+ uint32_t service_type = 0;
+
+ if (NULL != udp_redirects)
+ service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
+
+ if (NULL != tcp_redirects)
+ service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
+
+ service_type = htonl (service_type);
+
+
+ publish_name (section, ports, service_type, my_private_key);
+
+ GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
+ "ALTERNATIVE_NAMES",
+ &alternative_names);
+ for (alternative_name = strtok (alternative_names, " ");
+ alternative_name != NULL; alternative_name = strtok (NULL, " "))
+ {
+ char *altname =
+ alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
+ strcpy (altname, alternative_name);
+ strcpy (altname + strlen (alternative_name) + 1, section);
+ altname[strlen (alternative_name)] = '.';
+
+ publish_name (altname, ports, service_type, my_private_key);
+ }
+
+ GNUNET_free_non_null(alternative_names);
+ GNUNET_CRYPTO_rsa_key_free (my_private_key);
+ GNUNET_free_non_null (udp_redirects);
+ GNUNET_free_non_null (tcp_redirects);
+}
+
+/**
+ * Publish a DNS-record in the DHT. This is up to now just for testing.
+ */
+static void
+publish_names (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc) {
+ char *services;
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ return;
+
+ if (NULL != servicecfg)
+ GNUNET_CONFIGURATION_destroy(servicecfg);
+
+ GNUNET_CONFIGURATION_get_value_filename(cfg, "dns", "SERVICES", &services);
+
+ servicecfg = GNUNET_CONFIGURATION_create();
+ if (GNUNET_OK == GNUNET_CONFIGURATION_parse(servicecfg, services))
{
- GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
- return;
+ GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
+ GNUNET_CONFIGURATION_iterate_sections(servicecfg, publish_iterate, NULL);
}
- GNUNET_CRYPTO_rsa_key_free(my_private_key);
-
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
- "Putting with key %08x, size = %d\n",
- *((unsigned int*)&data.service_descriptor),
- size);
-
- GNUNET_DHT_put(dht,
- &data.service_descriptor,
- DEFAULT_PUT_REPLICATION,
- GNUNET_DHT_RO_NONE,
- GNUNET_BLOCK_TYPE_DNS,
- size,
- (char*)&data,
- GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
- GNUNET_TIME_UNIT_MINUTES,
- NULL,
- NULL);
+ if (NULL != services)
+ GNUNET_free(services);
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
- publish_name,
+ publish_names,
NULL);
}
const struct GNUNET_CONFIGURATION_Handle *cfg_)
{
static const struct GNUNET_SERVER_MessageHandler handlers[] = {
- /* callback, cls, type, size */
- {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
- {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, sizeof(struct GNUNET_MessageHeader)},
- {NULL, NULL, 0, 0}
+ /* callback, cls, type, size */
+ {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
+ {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
+ sizeof (struct GNUNET_MessageHeader)},
+ {NULL, NULL, 0, 0}
+ };
+
+ static struct GNUNET_MESH_MessageHandler *mesh_handlers;
+
+ if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg_, "dns", "PROVIDE_EXIT"))
+ mesh_handlers = (struct GNUNET_MESH_MessageHandler[]) {
+ {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
+ {NULL, 0, 0}
+ };
+ else
+ mesh_handlers = (struct GNUNET_MESH_MessageHandler[]) {
+ {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
+ {NULL, 0, 0}
+ };
+
+ const static GNUNET_MESH_ApplicationType apptypes[] =
+ { GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
+ GNUNET_APPLICATION_TYPE_END
};
+ mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
+
cfg = cfg_;
unsigned int i;
- for (i = 0; i < 65536; i++) {
+ for (i = 0; i < 65536; i++)
+ {
query_states[i].valid = GNUNET_NO;
- }
-
- dht = GNUNET_DHT_connect(cfg, 1024);
-
- struct sockaddr_in addr;
-
- dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
- if (dnsout == NULL)
- return;
- memset(&addr, 0, sizeof(struct sockaddr_in));
-
- int err = GNUNET_NETWORK_socket_bind (dnsout,
- (struct sockaddr*)&addr,
- sizeof(struct sockaddr_in));
-
- if (err != GNUNET_YES) {
- GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
- return;
- }
-
- /* Read the port we bound to */
- socklen_t addrlen = sizeof(struct sockaddr_in);
- err = getsockname(GNUNET_NETWORK_get_fd(dnsout),
- (struct sockaddr*) &addr,
- &addrlen);
+ }
- dnsoutport = htons(addr.sin_port);
+ dht = GNUNET_DHT_connect (cfg, 1024);
- GNUNET_SCHEDULER_add_now (publish_name, NULL);
+ open_port ();
- GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, dnsout, &read_response, NULL);
+ GNUNET_SCHEDULER_add_now (publish_names, NULL);
GNUNET_SERVER_add_handlers (server, handlers);
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
- &cleanup_task,
- cls);
+ &cleanup_task, cls);
}
/**