#include "block_gns.h"
#include "gns.h"
+#define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
+
/* Ignore for now not used anyway and probably never will */
#define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
#define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
* @param rh the pending gns query context
* @param name the name to query record
*/
-void
+static void
resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
{
uint32_t xquery;
- struct GNUNET_TIME_Relative timeout;
GNUNET_HashCode name_hash;
GNUNET_HashCode lookup_key;
struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
"starting dht lookup for %s with key: %s\n",
name, (char*)&lookup_key_string);
- timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
-
xquery = htonl(rh->query->type);
//FIXME how long to wait for results?
- rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
+ rh->get_handle = GNUNET_DHT_get_start(dht_handle,
+ DHT_LOOKUP_TIMEOUT,
GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
&lookup_key,
5, //Replication level FIXME
GNUNET_DHT_RO_NONE,
- &xquery, //xquery FIXME is this bad?
+ &xquery,
sizeof(xquery),
&process_name_dht_result,
rh);
size_t xquery_size, const void *reply_block,
size_t reply_block_size)
{
- if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
- return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
- if (reply_block_size == 0)
- return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
-
char* name;
GNUNET_HashCode pkey_hash;
GNUNET_HashCode query_key;
struct GNSNameRecordBlock *nrb;
struct GNSRecordBlock *rb;
uint32_t rd_count;
-
+ unsigned int record_match;
+
+ if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
+ return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
+ if (reply_block_size == 0)
+ return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
+
nrb = (struct GNSNameRecordBlock *)reply_block;
name = (char*)&nrb[1];
GNUNET_CRYPTO_hash(&nrb->public_key,
if (0 != GNUNET_CRYPTO_hash_cmp(query, &query_key))
return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
+ record_match = 0;
rd_count = ntohl(nrb->rd_count);
-
- struct GNUNET_NAMESTORE_RecordData rd[rd_count];
- int i = 0;
- int record_match = 0;
- uint32_t record_xquery = ntohl(*((uint32_t*)xquery));
- rb = (struct GNSRecordBlock*)(&name[strlen(name) + 1]);
-
- for (i=0; i<rd_count; i++)
{
- rd[i].record_type = ntohl(rb->type);
- rd[i].expiration =
- GNUNET_TIME_absolute_ntoh(rb->expiration);
- rd[i].data_size = ntohl(rb->data_length);
- rd[i].flags = ntohl(rb->flags);
- rd[i].data = (char*)&rb[1];
- rb = (struct GNSRecordBlock *)((char*)&rb[1] + rd[i].data_size);
-
- if (xquery_size == 0)
- continue;
-
- if (rd[i].record_type == record_xquery)
+ struct GNUNET_NAMESTORE_RecordData rd[rd_count];
+ unsigned int i;
+ uint32_t record_xquery = ntohl(*((uint32_t*)xquery));
+
+ rb = (struct GNSRecordBlock*)(&name[strlen(name) + 1]);
+ for (i=0; i<rd_count; i++)
{
- record_match++;
+ rd[i].record_type = ntohl(rb->type);
+ rd[i].expiration =
+ GNUNET_TIME_absolute_ntoh(rb->expiration);
+ rd[i].data_size = ntohl(rb->data_length);
+ rd[i].flags = ntohl(rb->flags);
+ rd[i].data = (char*)&rb[1];
+ rb = (struct GNSRecordBlock *)((char*)&rb[1] + rd[i].data_size);
+
+ if (xquery_size == 0)
+ continue;
+
+ if (rd[i].record_type == record_xquery)
+ record_match++;
}
- }
-
+ }
/*if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&nrb->public_key,
name,
*/
struct GNUNET_CRYPTO_RsaSignature signature;
- /**
- * What is being signed and why?
- */
- struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
-
/* number of records that follow */
uint32_t rd_count GNUNET_PACKED;
#define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT 441
#define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP 442
-size_t
-GNUNET_NAMESTORE_records_serialize (char ** dest,
- unsigned int rd_count,
- const struct GNUNET_NAMESTORE_RecordData *rd);
-
-int
-GNUNET_NAMESTORE_records_deserialize ( struct GNUNET_NAMESTORE_RecordData **dest, char *src, size_t len);
-
-void
-GNUNET_NAMESTORE_records_free (unsigned int rd_count, struct GNUNET_NAMESTORE_RecordData *rd);
-
GNUNET_NETWORK_STRUCT_BEGIN
/**
#define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
#define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
+
+
+/**
+ * Internal format of a record in the serialized form.
+ */
+struct NetworkRecord
+{
+
+ /**
+ * Expiration time for the DNS record.
+ */
+ struct GNUNET_TIME_AbsoluteNBO expiration;
+
+ /**
+ * Number of bytes in 'data', network byte order.
+ */
+ uint32_t data_size;
+
+ /**
+ * Type of the GNS/DNS record, network byte order.
+ */
+ uint32_t record_type;
+
+ /**
+ * Flags for the record, network byte order.
+ */
+ uint32_t flags;
+
+};
+
+/**
+ * Calculate how many bytes we will need to serialize the given
+ * records.
+ */
+size_t
+GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
+ const struct GNUNET_NAMESTORE_RecordData *rd)
+{
+ unsigned int i;
+ size_t ret;
+
+ ret = sizeof (struct NetworkRecord) * rd_count;
+ for (i=0;i<rd_count;i++)
+ {
+ GNUNET_assert (ret + rd[i].data_size >= ret);
+ ret += rd[i].data_size;
+ }
+ return ret;
+}
+
+
+/**
+ * Serialize the given records to the given destination buffer.
+ */
+ssize_t
+GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
+ const struct GNUNET_NAMESTORE_RecordData *rd,
+ size_t dest_size,
+ char *dest)
+{
+ struct NetworkRecord rec;
+ unsigned int i;
+ size_t off;
+
+ off = 0;
+ for (i=0;i<rd_count;i++)
+ {
+ rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
+ rec.data_size = htonl ((uint32_t) rd[i].data_size);
+ rec.record_type = htonl (rd[i].record_type);
+ rec.flags = htonl (rd[i].flags);
+ if (off + sizeof (rec) > dest_size)
+ return -1;
+ memcpy (&dest[off], &rec, sizeof (rec));
+ off += sizeof (rec);
+ if (off + rd[i].data_size > dest_size)
+ return -1;
+ memcpy (&dest[off], rd[i].data, rd[i].data_size);
+ off += rd[i].data_size;
+ }
+ return off;
+}
+
+
+/**
+ * @param rd_count expected number of records in 'src'
+ * @param dest array of 'rd_count' entries for storing record data;
+ * 'data' values in 'dest' will point into 'src' and will thus
+ * become invalid if 'src' is modified
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_NAMESTORE_records_deserialize (size_t len,
+ const char *src,
+ unsigned int rd_count,
+ struct GNUNET_NAMESTORE_RecordData *dest)
+{
+ struct NetworkRecord rec;
+ unsigned int i;
+ size_t off;
+
+ off = 0;
+ for (i=0;i<rd_count;i++)
+ {
+ if (off + sizeof (rec) > len)
+ return GNUNET_SYSERR;
+ memcpy (&rec, &src[off], sizeof (rec));
+ dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
+ dest[i].data_size = ntohl ((uint32_t) rec.data_size);
+ dest[i].record_type = ntohl (rec.record_type);
+ dest[i].flags = ntohl (rec.flags);
+ off += sizeof (rec);
+
+ if (off + sizeof (dest[i].data_size) > len)
+ return GNUNET_SYSERR;
+ dest[i].data = &src[off];
+ off += dest[i].data_size;
+ }
+ return GNUNET_OK;
+}
+
+
+
+#if 0
+
/**
* Serialize an array of GNUNET_NAMESTORE_RecordData *rd to transmit over the
* network
return elements;
}
+#endif
+
/* end of namestore_api.c */