* Number of IBFs in a strata estimator.
*/
#define SE_STRATA_COUNT 32
+
/**
* Size of the IBFs in the strata estimator.
*/
#define SE_IBF_SIZE 80
+
/**
* The hash num parameter for the difference digests and strata estimators.
*/
* In the ultimate phase, we wait until
* our demands are satisfied and then
* quit (sending another DONE message). */
- PHASE_DONE,
+ PHASE_DONE
};
/**
* Extra state required for efficient set union.
*/
- struct SetState
+struct SetState
{
/**
* The strata estimator is only generated once for
*
* @param op the union operation
* @param size size of the ibf to create
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/
-static void
+static int
prepare_ibf (struct Operation *op,
uint32_t size)
{
if (NULL != op->state->local_ibf)
ibf_destroy (op->state->local_ibf);
op->state->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
+ if (NULL == op->state->local_ibf)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to allocate local IBF\n");
+ return GNUNET_SYSERR;
+ }
GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element,
&prepare_ibf_iterator,
op);
+ return GNUNET_OK;
}
*
* @param op the union operation
* @param ibf_order order of the ibf to send, size=2^order
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/
-static void
+static int
send_ibf (struct Operation *op,
uint16_t ibf_order)
{
unsigned int buckets_sent = 0;
struct InvertibleBloomFilter *ibf;
- prepare_ibf (op, 1<<ibf_order);
+ if (GNUNET_OK !=
+ prepare_ibf (op, 1<<ibf_order))
+ {
+ /* allocation failed */
+ return GNUNET_SYSERR;
+ }
LOG (GNUNET_ERROR_TYPE_DEBUG,
"sending ibf of size %u\n",
/* The other peer must decode the IBF, so
* we're passive. */
op->state->phase = PHASE_INVENTORY_PASSIVE;
+ return GNUNET_OK;
}
static void
send_strata_estimator (struct Operation *op)
{
+ const struct StrataEstimator *se = op->state->se;
struct GNUNET_MQ_Envelope *ev;
struct GNUNET_MessageHeader *strata_msg;
-
+ char *buf;
+ size_t len;
+ uint16_t type;
+
+ buf = GNUNET_malloc (se->strata_count * IBF_BUCKET_SIZE * se->ibf_size);
+ len = strata_estimator_write (op->state->se,
+ buf);
+ if (len < se->strata_count * IBF_BUCKET_SIZE * se->ibf_size)
+ type = GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SEC;
+ else
+ type = GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE;
ev = GNUNET_MQ_msg_header_extra (strata_msg,
- SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
- GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE);
- strata_estimator_write (op->state->se, &strata_msg[1]);
+ len,
+ type);
+ memcpy (&strata_msg[1],
+ buf,
+ len);
+ GNUNET_free (buf);
GNUNET_MQ_send (op->mq,
ev);
op->state->phase = PHASE_EXPECT_IBF;
*
* @param cls the union operation
* @param mh the message
+ * @param is_compressed #GNUNET_YES if the estimator is compressed
* @return #GNUNET_SYSERR if the tunnel should be disconnected,
* #GNUNET_OK otherwise
*/
static int
handle_p2p_strata_estimator (void *cls,
- const struct GNUNET_MessageHeader *mh)
+ const struct GNUNET_MessageHeader *mh,
+ int is_compressed)
{
struct Operation *op = cls;
struct StrataEstimator *remote_se;
int diff;
+ size_t len;
if (op->state->phase != PHASE_EXPECT_SE)
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
- if (ntohs (mh->size) !=
- SE_STRATA_COUNT * SE_IBF_SIZE * IBF_BUCKET_SIZE +
- sizeof (struct GNUNET_MessageHeader))
+ len = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
+ if ( (GNUNET_NO == is_compressed) &&
+ (len !=
+ SE_STRATA_COUNT * SE_IBF_SIZE * IBF_BUCKET_SIZE) )
{
fail_union_operation (op);
GNUNET_break (0);
remote_se = strata_estimator_create (SE_STRATA_COUNT,
SE_IBF_SIZE,
SE_IBF_HASH_NUM);
- strata_estimator_read (&mh[1], remote_se);
+ if (NULL == remote_se)
+ {
+ /* insufficient resources, fail */
+ fail_union_operation (op);
+ return GNUNET_SYSERR;
+ }
+ if (GNUNET_OK !=
+ strata_estimator_read (&mh[1],
+ len,
+ is_compressed,
+ remote_se))
+ {
+ /* decompression failed */
+ fail_union_operation (op);
+ return GNUNET_SYSERR;
+ }
GNUNET_assert (NULL != op->state->se);
diff = strata_estimator_difference (remote_se,
op->state->se);
"got se diff=%d, using ibf size %d\n",
diff,
1<<get_order_from_difference (diff));
- send_ibf (op,
- get_order_from_difference (diff));
+ if (GNUNET_OK !=
+ send_ibf (op,
+ get_order_from_difference (diff)))
+ {
+ /* Internal error, best we can do is shut the connection */
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to send IBF, closing connection\n");
+ fail_union_operation (op);
+ return GNUNET_SYSERR;
+ }
return GNUNET_OK;
}
* send the appropriate offers and inquiries.
*
* @param op union operation
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/
-static void
+static int
decode_and_send (struct Operation *op)
{
struct IBF_Key key;
GNUNET_assert (PHASE_INVENTORY_ACTIVE == op->state->phase);
- prepare_ibf (op, op->state->remote_ibf->size);
+ if (GNUNET_OK !=
+ prepare_ibf (op, op->state->remote_ibf->size))
+ {
+ /* allocation failed */
+ return GNUNET_SYSERR;
+ }
diff_ibf = ibf_dup (op->state->local_ibf);
ibf_subtract (diff_ibf, op->state->remote_ibf);
"# of IBF retries",
1,
GNUNET_NO);
- send_ibf (op, next_order);
+ if (GNUNET_OK !=
+ send_ibf (op, next_order))
+ {
+ /* Internal error, best we can do is shut the connection */
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to send IBF, closing connection\n");
+ fail_union_operation (op);
+ ibf_destroy (diff_ibf);
+ return GNUNET_SYSERR;
+ }
}
else
{
// XXX: Send the whole set, element-by-element
LOG (GNUNET_ERROR_TYPE_ERROR,
"set union failed: reached ibf limit\n");
+ fail_union_operation (op);
+ ibf_destroy (diff_ibf);
+ return GNUNET_SYSERR;
}
break;
}
}
}
ibf_destroy (diff_ibf);
+ return GNUNET_OK;
}
"Creating new ibf of size %u\n",
1 << msg->order);
op->state->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
+ if (NULL == op->state->remote_ibf)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to parse remote IBF, closing connection\n");
+ fail_union_operation (op);
+ return GNUNET_SYSERR;
+ }
op->state->ibf_buckets_received = 0;
if (0 != ntohs (msg->offset))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"received full ibf\n");
op->state->phase = PHASE_INVENTORY_ACTIVE;
- decode_and_send (op);
+ if (GNUNET_OK !=
+ decode_and_send (op))
+ {
+ /* Internal error, best we can do is shut down */
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to decode IBF, closing connection\n");
+ return GNUNET_SYSERR;
+ }
}
return GNUNET_OK;
}
fail_union_operation (op);
return;
}
-
if (ntohs (mh->size) < sizeof (struct GNUNET_SET_ElementMessage))
{
GNUNET_break_op (0);
return;
}
- emsg = (struct GNUNET_SET_ElementMessage *) mh;
+ emsg = (const struct GNUNET_SET_ElementMessage *) mh;
element_size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ElementMessage);
ee = GNUNET_malloc (sizeof (struct ElementEntry) + element_size);
ee->remote = GNUNET_YES;
GNUNET_SET_element_hash (&ee->element, &ee->element_hash);
- if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (op->state->demanded_hashes, &ee->element_hash, NULL))
+ if (GNUNET_NO ==
+ GNUNET_CONTAINER_multihashmap_remove (op->state->demanded_hashes,
+ &ee->element_hash,
+ NULL))
{
/* We got something we didn't demand, since it's not in our map. */
GNUNET_break_op (0);
}
-
+/**
+ * FIXME
+ */
static void
handle_p2p_demand (void *cls,
- const struct GNUNET_MessageHeader *mh)
+ const struct GNUNET_MessageHeader *mh)
{
struct Operation *op = cls;
struct ElementEntry *ee;
struct ElementEntry *ee;
struct GNUNET_MessageHeader *demands;
struct GNUNET_MQ_Envelope *ev;
- ee = GNUNET_CONTAINER_multihashmap_get (op->spec->set->content->elements, hash);
+
+ ee = GNUNET_CONTAINER_multihashmap_get (op->spec->set->content->elements,
+ hash);
if (NULL != ee)
if (GNUNET_YES == _GSS_is_element_of_operation (ee, op))
continue;
- if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (op->state->demanded_hashes, hash))
+ if (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_contains (op->state->demanded_hashes,
+ hash))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Skipped sending duplicate demand\n");
LOG (GNUNET_ERROR_TYPE_DEBUG,
"[OP %x] Requesting element (hash %s)\n",
(void *) op, GNUNET_h2s (hash));
- ev = GNUNET_MQ_msg_header_extra (demands, sizeof (struct GNUNET_HashCode), GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DEMAND);
+ ev = GNUNET_MQ_msg_header_extra (demands,
+ sizeof (struct GNUNET_HashCode),
+ GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DEMAND);
*(struct GNUNET_HashCode *) &demands[1] = *hash;
GNUNET_MQ_send (op->mq, ev);
}
* We maintain one strata estimator per set and then manipulate it over the
* lifetime of the set, as recreating a strata estimator would be expensive.
*
- * @return the newly created set
+ * @return the newly created set, NULL on error
*/
static struct SetState *
union_set_create (void)
set_state = GNUNET_new (struct SetState);
set_state->se = strata_estimator_create (SE_STRATA_COUNT,
SE_IBF_SIZE, SE_IBF_HASH_NUM);
+ if (NULL == set_state->se)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to allocate strata estimator\n");
+ GNUNET_free (set_state);
+ return NULL;
+ }
return set_state;
}
case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF:
return handle_p2p_ibf (op, mh);
case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE:
- return handle_p2p_strata_estimator (op, mh);
+ return handle_p2p_strata_estimator (op, mh, GNUNET_NO);
+ case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SEC:
+ return handle_p2p_strata_estimator (op, mh, GNUNET_YES);
case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS:
handle_p2p_elements (op, mh);
break;
* @file set/gnunet-service-set_union_strata_estimator.c
* @brief invertible bloom filter
* @author Florian Dold
+ * @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet-service-set_union_strata_estimator.h"
+/**
+ * Should we try compressing the strata estimator? This will
+ * break compatibility with the 0.10.1-network.
+ */
+#define FAIL_10_1_COMPATIBILTIY 0
+
+
/**
* Write the given strata estimator to the buffer.
*
* @param se strata estimator to serialize
- * @param buf buffer to write to, must be of appropriate size
+ * @param[out] buf buffer to write to, must be of appropriate size
+ * @return number of bytes written to @a buf
*/
-void
+size_t
strata_estimator_write (const struct StrataEstimator *se,
void *buf)
{
GNUNET_assert (NULL != se);
for (i = 0; i < se->strata_count; i++)
{
- ibf_write_slice (se->strata[i], 0, se->ibf_size, buf);
+ ibf_write_slice (se->strata[i],
+ 0,
+ se->ibf_size,
+ buf);
buf += se->ibf_size * IBF_BUCKET_SIZE;
}
+ osize = se->ibf_size * IBF_BUCKET_SIZE * se->strata_count;
+#if FAIL_10_1_COMPATIBILTIY
+ {
+ size_t osize;
+ char *cbuf;
+ size_t nsize;
+
+ if (GNUNET_YES ==
+ GNUNET_try_compression (buf,
+ osize,
+ &cbuf,
+ &nsize))
+ {
+ memcpy (buf, cbuf, nsize);
+ osize = nsize;
+ GNUNET_free (cbuf);
+ }
+ }
+#endif
+ return osize;
}
* estimator. The strata estimator must already be allocated.
*
* @param buf buffer to read from
- * @param se strata estimator to write to
+ * @param buf_len number of bytes in @a buf
+ * @param is_compressed is the data compressed?
+ * @param[out] se strata estimator to write to
+ * @return #GNUNET_OK on success
*/
-void
+int
strata_estimator_read (const void *buf,
+ size_t buf_len,
+ int is_compressed,
struct StrataEstimator *se)
{
unsigned int i;
+ size_t osize;
+ char *dbuf;
+ dbuf = NULL;
+ if (GNUNET_YES == is_compressed)
+ {
+ osize = se->ibf_size * IBF_BUCKET_SIZE * se->strata_count;
+ dbuf = GNUNET_decompress (buf,
+ buf_len,
+ osize);
+ if (NULL == dbuf)
+ {
+ GNUNET_break_op (0); /* bad compressed input data */
+ return GNUNET_SYSERR;
+ }
+ buf = dbuf;
+ buf_len = osize;
+ }
+
+ if (buf_len != se->strata_count * se->ibf_size * IBF_BUCKET_SIZE)
+ {
+ GNUNET_break (0); /* very odd error */
+ GNUNET_free_non_null (dbuf);
+ return GNUNET_SYSERR;
+ }
for (i = 0; i < se->strata_count; i++)
{
ibf_read_slice (buf, 0, se->ibf_size, se->strata[i]);
buf += se->ibf_size * IBF_BUCKET_SIZE;
}
+ GNUNET_free_non_null (dbuf);
+ return GNUNET_OK;
}
* @param strata_count number of stratas, that is, number of ibfs in the estimator
* @param ibf_size size of each ibf stratum
* @param ibf_hashnum hashnum parameter of each ibf
- * @return a freshly allocated, empty strata estimator
+ * @return a freshly allocated, empty strata estimator, NULL on error
*/
struct StrataEstimator *
strata_estimator_create (unsigned int strata_count,
{
struct StrataEstimator *se;
unsigned int i;
+ unsigned int j;
- /* fixme: allocate everything in one chunk */
se = GNUNET_new (struct StrataEstimator);
se->strata_count = strata_count;
se->ibf_size = ibf_size;
- se->strata = GNUNET_malloc (sizeof (struct InvertibleBloomFilter *) * strata_count);
+ se->strata = GNUNET_new_array (strata_count,
+ struct InvertibleBloomFilter *);
for (i = 0; i < strata_count; i++)
+ {
se->strata[i] = ibf_create (ibf_size, ibf_hashnum);
+ if (NULL == se->strata[i])
+ {
+ for (j = 0; j < i; j++)
+ ibf_destroy (se->strata[i]);
+ GNUNET_free (se);
+ return NULL;
+ }
+ }
return se;
}
GNUNET_free (se->strata);
GNUNET_free (se);
}
-
* @param buf buffer to write the data to
*/
void
-ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, uint32_t count, void *buf);
+ibf_write_slice (const struct InvertibleBloomFilter *ibf,
+ uint32_t start,
+ uint32_t count,
+ void *buf);
/**
* @param buf pointer to the buffer to read from
* @param start which bucket to start at
* @param count how many buckets to read
- * @param ibf the ibf to read from
+ * @param ibf the ibf to write to
*/
void
-ibf_read_slice (const void *buf, uint32_t start, uint32_t count, struct InvertibleBloomFilter *ibf);
+ibf_read_slice (const void *buf,
+ uint32_t start,
+ uint32_t count,
+ struct InvertibleBloomFilter *ibf);
/**
*
* @param size number of IBF buckets
* @param hash_num number of buckets one element is hashed in, usually 3 or 4
- * @return the newly created invertible bloom filter
+ * @return the newly created invertible bloom filter, NULL on error
*/
struct InvertibleBloomFilter *
ibf_create (uint32_t size, uint8_t hash_num);
* @param ibf2 IBF that will be subtracted from ibf1
*/
void
-ibf_subtract (struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2);
+ibf_subtract (struct InvertibleBloomFilter *ibf1,
+ const struct InvertibleBloomFilter *ibf2);
/**
* A negative sign indicates that the element was recovered
* resides in an IBF that was previously subtracted from.
* @param ret_id receives the hash code of the decoded element, if successful
- * @return GNUNET_YES if decoding an element was successful,
- * GNUNET_NO if the IBF is empty,
- * GNUNET_SYSERR if the decoding has failed
+ * @return #GNUNET_YES if decoding an element was successful,
+ * #GNUNET_NO if the IBF is empty,
+ * #GNUNET_SYSERR if the decoding has failed
*/
int
-ibf_decode (struct InvertibleBloomFilter *ibf, int *ret_side, struct IBF_Key *ret_id);
+ibf_decode (struct InvertibleBloomFilter *ibf,
+ int *ret_side,
+ struct IBF_Key *ret_id);
/**
#endif
#endif
-