fill descr struct on new()

This commit is contained in:
Markus Teich 2016-08-12 14:38:10 +02:00
parent 155d0a2370
commit 6483328c48
3 changed files with 110 additions and 62 deletions

119
brandt.c
View File

@ -59,44 +59,62 @@ start_auction (void *arg)
struct BRANDT_Auction * struct BRANDT_Auction *
BRANDT_new (BRANDT_CbBroadcast broadcast, BRANDT_new (BRANDT_CbBroadcast broadcast,
BRANDT_CbResult result, BRANDT_CbResult result,
void *auction_closure, void *auction_closure,
void **auction_data, void **auction_data,
size_t *auction_data_len, size_t *auction_data_len,
struct GNUNET_TIME_Absolute time_start, struct GNUNET_TIME_Absolute time_start,
struct GNUNET_TIME_Relative time_round, struct GNUNET_TIME_Relative time_round,
uint16_t num_prices, void *description,
uint16_t m, uint32_t description_len,
int outcome_public) uint16_t num_prices,
uint16_t m,
int outcome_public)
{ {
struct BRANDT_Auction *ret; struct BRANDT_Auction *ret = GNUNET_new (struct BRANDT_Auction);
struct BRANDT_DescrP *desc = GNUNET_new (struct BRANDT_DescrP);
struct GNUNET_TIME_Relative until_start;
struct GNUNET_HashContext *hc = GNUNET_CRYPTO_hash_context_start ();
ret = GNUNET_new (struct BRANDT_Auction); desc->time_start = GNUNET_TIME_absolute_hton (time_start);
desc->time_round = GNUNET_TIME_relative_hton (time_round);
desc->description_len = htonl (description_len);
desc->k = htons (num_prices);
desc->m = htons (m);
desc->outcome_public = htons (outcome_public);
GNUNET_CRYPTO_hash_context_read (hc,
&desc->time_start,
sizeof (*desc) - sizeof (desc->hash));
GNUNET_CRYPTO_hash_context_read (hc,
description,
description_len);
GNUNET_CRYPTO_hash_context_finish (hc, &desc->hash);
ret->time_start = time_start; ret->time_start = time_start;
ret->time_round = time_round; ret->time_round = time_round;
ret->k = num_prices; ret->k = num_prices;
ret->m = m; ret->m = m;
ret->outcome_public = outcome_public; ret->outcome_public = outcome_public;
ret->cur_round = msg_join;
ret->round_progress = gcry_mpi_new (256);
/* we are the seller */ /* we are the seller */
ret->seller_mode = 1; ret->seller_mode = 1;
/* interface with application */ /* callback interface with application */
ret->closure = auction_closure; ret->closure = auction_closure;
ret->bcast = broadcast; ret->bcast = broadcast;
ret->result = result; ret->result = result;
ret->cur_round = msg_join; until_start = GNUNET_TIME_absolute_get_remaining (time_start);
ret->round_progress = gcry_mpi_new (256);
/* \todo: store returned task somewhere to cancel it on shutdown */ /* \todo: store returned task somewhere to cancel it on shutdown */
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (time_start), ret->task = GNUNET_SCHEDULER_add_delayed (until_start,
&start_auction, &start_auction,
ret); ret);
*auction_data_len = sizeof (struct BRANDT_DescrP);
*auction_data = desc;
return ret; return ret;
} }
@ -109,53 +127,59 @@ BRANDT_join (BRANDT_CbBroadcast broadcast,
const void *auction_data, const void *auction_data,
size_t auction_data_len) size_t auction_data_len)
{ {
struct BRANDT_Auction *ret; struct BRANDT_Auction *ret = GNUNET_new (struct BRANDT_Auction);
struct BRANDT_DescrP *desc = (struct BRANDT_DescrP *)auction_data; struct BRANDT_DescrP *desc = (struct BRANDT_DescrP *)auction_data;
ret = GNUNET_new (struct BRANDT_Auction); ret->time_start = GNUNET_TIME_absolute_ntoh (desc->time_start);
ret->time_round = GNUNET_TIME_relative_ntoh (desc->time_round);
ret->time_start = GNUNET_TIME_absolute_ntoh(desc->time_start); ret->k = ntohs (desc->k);
ret->time_round = GNUNET_TIME_relative_ntoh(desc->time_round); ret->m = ntohs (desc->m);
ret->outcome_public = ntohs (desc->outcome_public);
ret->k = ntohs(desc->k); ret->cur_round = msg_join;
ret->m = ntohs(desc->m); ret->round_progress = gcry_mpi_new (256);
ret->outcome_public = ntohs(desc->outcome_public);
/* we are the seller */ /* we are the seller */
ret->seller_mode = 0; ret->seller_mode = 0;
/* interface with application */ /* callback interface with application */
ret->closure = auction_closure; ret->closure = auction_closure;
ret->bcast = broadcast; ret->bcast = broadcast;
ret->ucast = unicast; ret->ucast = unicast;
ret->result = result; ret->result = result;
ret->cur_round = msg_join;
ret->round_progress = gcry_mpi_new (256);
return ret; return ret;
} }
void
BRANDT_destroy (struct BRANDT_Auction *auction)
{
GNUNET_SCHEDULER_cancel (auction->task);
}
static void static void
advance_round (struct BRANDT_Auction *auction, enum auction_type atype, enum outcome_type outcome) advance_round (struct BRANDT_Auction *ad,
enum auction_type atype,
enum outcome_type outcome)
{ {
unsigned char *buf; unsigned char *buf;
size_t buflen; size_t buflen;
/* if we got the current round message from all participants, advance to /* if we got the current round message from all participants, advance to
* next round */ * next round */
for (uint16_t i = 0; i < auction->n; i++) for (uint16_t i = 0; i < ad->n; i++)
if (!gcry_mpi_test_bit (auction->round_progress, i)) if (!gcry_mpi_test_bit (ad->round_progress, i))
return; return;
gcry_mpi_clear_highbit (auction->round_progress, 0); gcry_mpi_clear_highbit (ad->round_progress, 0);
if (msg_last == ++(auction->cur_round)) if (msg_last == ++(ad->cur_round))
{ {
/** \todo: finish */
} }
if (!handler_out[atype][outcome][auction->cur_round] || if (!handler_out[atype][outcome][ad->cur_round] ||
!(buf = handler_out[atype][outcome][auction->cur_round](auction, &buflen))) !(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen)))
{ {
/** \todo */ /** \todo */
weprintf ("wow fail out"); weprintf ("wow fail out");
@ -165,10 +189,10 @@ advance_round (struct BRANDT_Auction *auction, enum auction_type atype, enum out
/** \todo: add msgtype header in the handler_out functions */ /** \todo: add msgtype header in the handler_out functions */
/* last message only sent to seller, others are broadcasted */ /* last message only sent to seller, others are broadcasted */
if (msg_decrypt == auction->cur_round) if (msg_decrypt == ad->cur_round)
auction->ucast (auction->closure, buf, buflen); ad->ucast (ad->closure, buf, buflen);
else else
auction->bcast (auction->closure, buf, buflen); ad->bcast (ad->closure, buf, buflen);
} }
@ -178,10 +202,10 @@ BRANDT_got_message (struct BRANDT_Auction *auction,
const unsigned char *msg, const unsigned char *msg,
size_t msg_len) size_t msg_len)
{ {
uint16_t mtype = *(uint16_t *)msg; uint16_t mtype = *(uint16_t *)msg;
enum auction_type atype; enum auction_type atype;
enum outcome_type outcome; enum outcome_type outcome;
enum rounds round = auction->cur_round; enum rounds round = auction->cur_round;
atype = auction->m > 0 ? auction_mPlusFirstPrice : auction_firstPrice; atype = auction->m > 0 ? auction_mPlusFirstPrice : auction_firstPrice;
outcome = auction->outcome_public ? outcome_public : outcome_private; outcome = auction->outcome_public ? outcome_public : outcome_private;
@ -207,5 +231,6 @@ BRANDT_got_message (struct BRANDT_Auction *auction,
} }
gcry_mpi_set_bit (auction->round_progress, sender); gcry_mpi_set_bit (auction->round_progress, sender);
/** \todo: seller_mode and new task for round timing */
advance_round (auction, atype, outcome); advance_round (auction, atype, outcome);
} }

View File

@ -153,21 +153,27 @@ BRANDT_join (BRANDT_CbBroadcast broadcast,
* black-box pointer, do NOT dereference/change it or the data it points to! * black-box pointer, do NOT dereference/change it or the data it points to!
*/ */
struct BRANDT_Auction * struct BRANDT_Auction *
BRANDT_new (BRANDT_CbBroadcast broadcast, BRANDT_new (BRANDT_CbBroadcast broadcast,
BRANDT_CbResult result, BRANDT_CbResult result,
void *auction_closure, void *auction_closure,
void **auction_data, void **auction_data,
size_t *auction_data_len, size_t *auction_data_len,
struct GNUNET_TIME_Absolute time_start, struct GNUNET_TIME_Absolute time_start,
struct GNUNET_TIME_Relative time_round, struct GNUNET_TIME_Relative time_round,
uint16_t num_prices, void *description,
uint16_t m, uint32_t description_len,
int outcome_public); uint16_t num_prices,
uint16_t m,
int outcome_public);
/** \todo */ /**
* Clean up this auction on shutdown.
*
* @param[in] auction The pointer returned by BRANDT_join() or BRANDT_new().
* \todo: implement (de)serialization */
void void
BRANDT_free (); BRANDT_destroy (struct BRANDT_Auction *auction);
/** /**

View File

@ -55,12 +55,20 @@ enum outcome_type {
GNUNET_NETWORK_STRUCT_BEGIN GNUNET_NETWORK_STRUCT_BEGIN
/** /**
* This struct describes an auction and has to be followed by #description_len * This struct describes an auction and is always linked to a description buffer
* bytes of arbitrary data where the description of the item to be sold is * of #description_len bytes of arbitrary data where the description of the item
* stored. All fields are stored in network byte order. * to be sold is stored. This buffer should also contain information linking the
* auction to the payment system (which exact prices do the k possibilities
* refer to, payment system seller identity, ). All fields are stored in
* network byte order.
* *
* \todo: align to a multiple of 64bit */ * \todo: align to a multiple of 64bit
* \todo: versionsnummer */
struct BRANDT_DescrP { struct BRANDT_DescrP {
/** Hash code over the remaining elements of this struct followed by the
* description buffer of #description_len bytes */
struct GNUNET_HashCode hash GNUNET_PACKED;
/** Starting time of the auction. Bidders have to join the auction via /** Starting time of the auction. Bidders have to join the auction via
* BRANDT_join until this time */ * BRANDT_join until this time */
struct GNUNET_TIME_AbsoluteNBO time_start; struct GNUNET_TIME_AbsoluteNBO time_start;
@ -68,9 +76,12 @@ struct BRANDT_DescrP {
/** The maximum duration the participants have to complete each round. */ /** The maximum duration the participants have to complete each round. */
struct GNUNET_TIME_RelativeNBO time_round; struct GNUNET_TIME_RelativeNBO time_round;
/** The length of the description in bytes directly following this struct */ /** The length of the description in bytes */
uint32_t description_len GNUNET_PACKED; uint32_t description_len GNUNET_PACKED;
/** reserved for future use */
uint32_t reserved1 GNUNET_PACKED;
/** The amount of possible prices */ /** The amount of possible prices */
uint16_t k GNUNET_PACKED; uint16_t k GNUNET_PACKED;
@ -81,6 +92,9 @@ struct BRANDT_DescrP {
/** Outcome type. 0 means private outcome, everything else means public /** Outcome type. 0 means private outcome, everything else means public
* outcome. */ * outcome. */
uint16_t outcome_public GNUNET_PACKED; uint16_t outcome_public GNUNET_PACKED;
/** reserved for future use */
uint16_t reserved2 GNUNET_PACKED;
}; };
GNUNET_NETWORK_STRUCT_END GNUNET_NETWORK_STRUCT_END
@ -102,6 +116,9 @@ struct BRANDT_Auction {
* outcome. */ * outcome. */
uint16_t outcome_public; uint16_t outcome_public;
/** Link to the next delayed task (auction start trigger, round trigger) */
struct GNUNET_SCHEDULER_Task *task;
void *closure; /** auction closure given by the user */ void *closure; /** auction closure given by the user */
BRANDT_CbBroadcast bcast; /** broadcast callback */ BRANDT_CbBroadcast bcast; /** broadcast callback */