diff options
author | Markus Teich <markus.teich@stusta.mhn.de> | 2016-08-12 14:38:10 +0200 |
---|---|---|
committer | Markus Teich <markus.teich@stusta.mhn.de> | 2016-08-12 15:22:57 +0200 |
commit | 6483328c4805b92390032ac45f55c50be7b6f103 (patch) | |
tree | 2a9fb3d125a0f850046973af0c061e5f3a7e2acb | |
parent | 155d0a2370721f1ba50759c25daa27b37c2234c7 (diff) | |
download | libbrandt-6483328c4805b92390032ac45f55c50be7b6f103.tar.gz libbrandt-6483328c4805b92390032ac45f55c50be7b6f103.zip |
fill descr struct on new()
-rw-r--r-- | brandt.c | 123 | ||||
-rw-r--r-- | brandt.h | 26 | ||||
-rw-r--r-- | internals.h | 27 |
3 files changed, 112 insertions, 64 deletions
@@ -59,44 +59,62 @@ start_auction (void *arg) | |||
59 | 59 | ||
60 | 60 | ||
61 | struct BRANDT_Auction * | 61 | struct BRANDT_Auction * |
62 | BRANDT_new (BRANDT_CbBroadcast broadcast, | 62 | BRANDT_new (BRANDT_CbBroadcast broadcast, |
63 | BRANDT_CbResult result, | 63 | BRANDT_CbResult result, |
64 | void *auction_closure, | 64 | void *auction_closure, |
65 | void **auction_data, | 65 | void **auction_data, |
66 | size_t *auction_data_len, | 66 | size_t *auction_data_len, |
67 | struct GNUNET_TIME_Absolute time_start, | 67 | struct GNUNET_TIME_Absolute time_start, |
68 | struct GNUNET_TIME_Relative time_round, | 68 | struct GNUNET_TIME_Relative time_round, |
69 | uint16_t num_prices, | 69 | void *description, |
70 | uint16_t m, | 70 | uint32_t description_len, |
71 | int outcome_public) | 71 | uint16_t num_prices, |
72 | uint16_t m, | ||
73 | int outcome_public) | ||
72 | { | 74 | { |
73 | struct BRANDT_Auction *ret; | 75 | struct BRANDT_Auction *ret = GNUNET_new (struct BRANDT_Auction); |
74 | 76 | struct BRANDT_DescrP *desc = GNUNET_new (struct BRANDT_DescrP); | |
75 | ret = GNUNET_new (struct BRANDT_Auction); | 77 | struct GNUNET_TIME_Relative until_start; |
78 | struct GNUNET_HashContext *hc = GNUNET_CRYPTO_hash_context_start (); | ||
79 | |||
80 | desc->time_start = GNUNET_TIME_absolute_hton (time_start); | ||
81 | desc->time_round = GNUNET_TIME_relative_hton (time_round); | ||
82 | desc->description_len = htonl (description_len); | ||
83 | desc->k = htons (num_prices); | ||
84 | desc->m = htons (m); | ||
85 | desc->outcome_public = htons (outcome_public); | ||
86 | GNUNET_CRYPTO_hash_context_read (hc, | ||
87 | &desc->time_start, | ||
88 | sizeof (*desc) - sizeof (desc->hash)); | ||
89 | GNUNET_CRYPTO_hash_context_read (hc, | ||
90 | description, | ||
91 | description_len); | ||
92 | GNUNET_CRYPTO_hash_context_finish (hc, &desc->hash); | ||
76 | 93 | ||
77 | ret->time_start = time_start; | 94 | ret->time_start = time_start; |
78 | ret->time_round = time_round; | 95 | ret->time_round = time_round; |
79 | |||
80 | ret->k = num_prices; | 96 | ret->k = num_prices; |
81 | ret->m = m; | 97 | ret->m = m; |
82 | ret->outcome_public = outcome_public; | 98 | ret->outcome_public = outcome_public; |
99 | ret->cur_round = msg_join; | ||
100 | ret->round_progress = gcry_mpi_new (256); | ||
83 | 101 | ||
84 | /* we are the seller */ | 102 | /* we are the seller */ |
85 | ret->seller_mode = 1; | 103 | ret->seller_mode = 1; |
86 | 104 | ||
87 | /* interface with application */ | 105 | /* callback interface with application */ |
88 | ret->closure = auction_closure; | 106 | ret->closure = auction_closure; |
89 | ret->bcast = broadcast; | 107 | ret->bcast = broadcast; |
90 | ret->result = result; | 108 | ret->result = result; |
91 | 109 | ||
92 | ret->cur_round = msg_join; | 110 | until_start = GNUNET_TIME_absolute_get_remaining (time_start); |
93 | ret->round_progress = gcry_mpi_new (256); | ||
94 | |||
95 | /* \todo: store returned task somewhere to cancel it on shutdown */ | 111 | /* \todo: store returned task somewhere to cancel it on shutdown */ |
96 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (time_start), | 112 | ret->task = GNUNET_SCHEDULER_add_delayed (until_start, |
97 | &start_auction, | 113 | &start_auction, |
98 | ret); | 114 | ret); |
99 | 115 | ||
116 | *auction_data_len = sizeof (struct BRANDT_DescrP); | ||
117 | *auction_data = desc; | ||
100 | return ret; | 118 | return ret; |
101 | } | 119 | } |
102 | 120 | ||
@@ -109,53 +127,59 @@ BRANDT_join (BRANDT_CbBroadcast broadcast, | |||
109 | const void *auction_data, | 127 | const void *auction_data, |
110 | size_t auction_data_len) | 128 | size_t auction_data_len) |
111 | { | 129 | { |
112 | struct BRANDT_Auction *ret; | 130 | struct BRANDT_Auction *ret = GNUNET_new (struct BRANDT_Auction); |
113 | struct BRANDT_DescrP *desc = (struct BRANDT_DescrP *)auction_data; | 131 | struct BRANDT_DescrP *desc = (struct BRANDT_DescrP *)auction_data; |
114 | 132 | ||
115 | ret = GNUNET_new (struct BRANDT_Auction); | 133 | ret->time_start = GNUNET_TIME_absolute_ntoh (desc->time_start); |
116 | 134 | ret->time_round = GNUNET_TIME_relative_ntoh (desc->time_round); | |
117 | ret->time_start = GNUNET_TIME_absolute_ntoh(desc->time_start); | 135 | ret->k = ntohs (desc->k); |
118 | ret->time_round = GNUNET_TIME_relative_ntoh(desc->time_round); | 136 | ret->m = ntohs (desc->m); |
119 | 137 | ret->outcome_public = ntohs (desc->outcome_public); | |
120 | ret->k = ntohs(desc->k); | 138 | ret->cur_round = msg_join; |
121 | ret->m = ntohs(desc->m); | 139 | ret->round_progress = gcry_mpi_new (256); |
122 | ret->outcome_public = ntohs(desc->outcome_public); | ||
123 | 140 | ||
124 | /* we are the seller */ | 141 | /* we are the seller */ |
125 | ret->seller_mode = 0; | 142 | ret->seller_mode = 0; |
126 | 143 | ||
127 | /* interface with application */ | 144 | /* callback interface with application */ |
128 | ret->closure = auction_closure; | 145 | ret->closure = auction_closure; |
129 | ret->bcast = broadcast; | 146 | ret->bcast = broadcast; |
130 | ret->ucast = unicast; | 147 | ret->ucast = unicast; |
131 | ret->result = result; | 148 | ret->result = result; |
132 | 149 | ||
133 | ret->cur_round = msg_join; | ||
134 | ret->round_progress = gcry_mpi_new (256); | ||
135 | |||
136 | return ret; | 150 | return ret; |
137 | } | 151 | } |
138 | 152 | ||
139 | 153 | ||
154 | void | ||
155 | BRANDT_destroy (struct BRANDT_Auction *auction) | ||
156 | { | ||
157 | GNUNET_SCHEDULER_cancel (auction->task); | ||
158 | } | ||
159 | |||
160 | |||
140 | static void | 161 | static void |
141 | advance_round (struct BRANDT_Auction *auction, enum auction_type atype, enum outcome_type outcome) | 162 | advance_round (struct BRANDT_Auction *ad, |
163 | enum auction_type atype, | ||
164 | enum outcome_type outcome) | ||
142 | { | 165 | { |
143 | unsigned char *buf; | 166 | unsigned char *buf; |
144 | size_t buflen; | 167 | size_t buflen; |
145 | 168 | ||
146 | /* if we got the current round message from all participants, advance to | 169 | /* if we got the current round message from all participants, advance to |
147 | * next round */ | 170 | * next round */ |
148 | for (uint16_t i = 0; i < auction->n; i++) | 171 | for (uint16_t i = 0; i < ad->n; i++) |
149 | if (!gcry_mpi_test_bit (auction->round_progress, i)) | 172 | if (!gcry_mpi_test_bit (ad->round_progress, i)) |
150 | return; | 173 | return; |
151 | 174 | ||
152 | gcry_mpi_clear_highbit (auction->round_progress, 0); | 175 | gcry_mpi_clear_highbit (ad->round_progress, 0); |
153 | if (msg_last == ++(auction->cur_round)) | 176 | if (msg_last == ++(ad->cur_round)) |
154 | { | 177 | { |
178 | /** \todo: finish */ | ||
155 | } | 179 | } |
156 | 180 | ||
157 | if (!handler_out[atype][outcome][auction->cur_round] || | 181 | if (!handler_out[atype][outcome][ad->cur_round] || |
158 | !(buf = handler_out[atype][outcome][auction->cur_round](auction, &buflen))) | 182 | !(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen))) |
159 | { | 183 | { |
160 | /** \todo */ | 184 | /** \todo */ |
161 | weprintf ("wow fail out"); | 185 | weprintf ("wow fail out"); |
@@ -165,10 +189,10 @@ advance_round (struct BRANDT_Auction *auction, enum auction_type atype, enum out | |||
165 | /** \todo: add msgtype header in the handler_out functions */ | 189 | /** \todo: add msgtype header in the handler_out functions */ |
166 | 190 | ||
167 | /* last message only sent to seller, others are broadcasted */ | 191 | /* last message only sent to seller, others are broadcasted */ |
168 | if (msg_decrypt == auction->cur_round) | 192 | if (msg_decrypt == ad->cur_round) |
169 | auction->ucast (auction->closure, buf, buflen); | 193 | ad->ucast (ad->closure, buf, buflen); |
170 | else | 194 | else |
171 | auction->bcast (auction->closure, buf, buflen); | 195 | ad->bcast (ad->closure, buf, buflen); |
172 | } | 196 | } |
173 | 197 | ||
174 | 198 | ||
@@ -178,10 +202,10 @@ BRANDT_got_message (struct BRANDT_Auction *auction, | |||
178 | const unsigned char *msg, | 202 | const unsigned char *msg, |
179 | size_t msg_len) | 203 | size_t msg_len) |
180 | { | 204 | { |
181 | uint16_t mtype = *(uint16_t *)msg; | 205 | uint16_t mtype = *(uint16_t *)msg; |
182 | enum auction_type atype; | 206 | enum auction_type atype; |
183 | enum outcome_type outcome; | 207 | enum outcome_type outcome; |
184 | enum rounds round = auction->cur_round; | 208 | enum rounds round = auction->cur_round; |
185 | 209 | ||
186 | atype = auction->m > 0 ? auction_mPlusFirstPrice : auction_firstPrice; | 210 | atype = auction->m > 0 ? auction_mPlusFirstPrice : auction_firstPrice; |
187 | outcome = auction->outcome_public ? outcome_public : outcome_private; | 211 | outcome = auction->outcome_public ? outcome_public : outcome_private; |
@@ -207,5 +231,6 @@ BRANDT_got_message (struct BRANDT_Auction *auction, | |||
207 | } | 231 | } |
208 | gcry_mpi_set_bit (auction->round_progress, sender); | 232 | gcry_mpi_set_bit (auction->round_progress, sender); |
209 | 233 | ||
234 | /** \todo: seller_mode and new task for round timing */ | ||
210 | advance_round (auction, atype, outcome); | 235 | advance_round (auction, atype, outcome); |
211 | } | 236 | } |
@@ -153,21 +153,27 @@ BRANDT_join (BRANDT_CbBroadcast broadcast, | |||
153 | * black-box pointer, do NOT dereference/change it or the data it points to! | 153 | * black-box pointer, do NOT dereference/change it or the data it points to! |
154 | */ | 154 | */ |
155 | struct BRANDT_Auction * | 155 | struct BRANDT_Auction * |
156 | BRANDT_new (BRANDT_CbBroadcast broadcast, | 156 | BRANDT_new (BRANDT_CbBroadcast broadcast, |
157 | BRANDT_CbResult result, | 157 | BRANDT_CbResult result, |
158 | void *auction_closure, | 158 | void *auction_closure, |
159 | void **auction_data, | 159 | void **auction_data, |
160 | size_t *auction_data_len, | 160 | size_t *auction_data_len, |
161 | struct GNUNET_TIME_Absolute time_start, | 161 | struct GNUNET_TIME_Absolute time_start, |
162 | struct GNUNET_TIME_Relative time_round, | 162 | struct GNUNET_TIME_Relative time_round, |
163 | uint16_t num_prices, | 163 | void *description, |
164 | uint16_t m, | 164 | uint32_t description_len, |
165 | int outcome_public); | 165 | uint16_t num_prices, |
166 | uint16_t m, | ||
167 | int outcome_public); | ||
166 | 168 | ||
167 | 169 | ||
168 | /** \todo */ | 170 | /** |
171 | * Clean up this auction on shutdown. | ||
172 | * | ||
173 | * @param[in] auction The pointer returned by BRANDT_join() or BRANDT_new(). | ||
174 | * \todo: implement (de)serialization */ | ||
169 | void | 175 | void |
170 | BRANDT_free (); | 176 | BRANDT_destroy (struct BRANDT_Auction *auction); |
171 | 177 | ||
172 | 178 | ||
173 | /** | 179 | /** |
diff --git a/internals.h b/internals.h index acb4845..fa2ea87 100644 --- a/internals.h +++ b/internals.h | |||
@@ -55,12 +55,20 @@ enum outcome_type { | |||
55 | GNUNET_NETWORK_STRUCT_BEGIN | 55 | GNUNET_NETWORK_STRUCT_BEGIN |
56 | 56 | ||
57 | /** | 57 | /** |
58 | * This struct describes an auction and has to be followed by #description_len | 58 | * This struct describes an auction and is always linked to a description buffer |
59 | * bytes of arbitrary data where the description of the item to be sold is | 59 | * of #description_len bytes of arbitrary data where the description of the item |
60 | * stored. All fields are stored in network byte order. | 60 | * to be sold is stored. This buffer should also contain information linking the |
61 | * auction to the payment system (which exact prices do the k possibilities | ||
62 | * refer to, payment system seller identity, …). All fields are stored in | ||
63 | * network byte order. | ||
61 | * | 64 | * |
62 | * \todo: align to a multiple of 64bit */ | 65 | * \todo: align to a multiple of 64bit |
66 | * \todo: versionsnummer */ | ||
63 | struct BRANDT_DescrP { | 67 | struct BRANDT_DescrP { |
68 | /** Hash code over the remaining elements of this struct followed by the | ||
69 | * description buffer of #description_len bytes */ | ||
70 | struct GNUNET_HashCode hash GNUNET_PACKED; | ||
71 | |||
64 | /** Starting time of the auction. Bidders have to join the auction via | 72 | /** Starting time of the auction. Bidders have to join the auction via |
65 | * BRANDT_join until this time */ | 73 | * BRANDT_join until this time */ |
66 | struct GNUNET_TIME_AbsoluteNBO time_start; | 74 | struct GNUNET_TIME_AbsoluteNBO time_start; |
@@ -68,9 +76,12 @@ struct BRANDT_DescrP { | |||
68 | /** The maximum duration the participants have to complete each round. */ | 76 | /** The maximum duration the participants have to complete each round. */ |
69 | struct GNUNET_TIME_RelativeNBO time_round; | 77 | struct GNUNET_TIME_RelativeNBO time_round; |
70 | 78 | ||
71 | /** The length of the description in bytes directly following this struct */ | 79 | /** The length of the description in bytes */ |
72 | uint32_t description_len GNUNET_PACKED; | 80 | uint32_t description_len GNUNET_PACKED; |
73 | 81 | ||
82 | /** reserved for future use */ | ||
83 | uint32_t reserved1 GNUNET_PACKED; | ||
84 | |||
74 | /** The amount of possible prices */ | 85 | /** The amount of possible prices */ |
75 | uint16_t k GNUNET_PACKED; | 86 | uint16_t k GNUNET_PACKED; |
76 | 87 | ||
@@ -81,6 +92,9 @@ struct BRANDT_DescrP { | |||
81 | /** Outcome type. 0 means private outcome, everything else means public | 92 | /** Outcome type. 0 means private outcome, everything else means public |
82 | * outcome. */ | 93 | * outcome. */ |
83 | uint16_t outcome_public GNUNET_PACKED; | 94 | uint16_t outcome_public GNUNET_PACKED; |
95 | |||
96 | /** reserved for future use */ | ||
97 | uint16_t reserved2 GNUNET_PACKED; | ||
84 | }; | 98 | }; |
85 | 99 | ||
86 | GNUNET_NETWORK_STRUCT_END | 100 | GNUNET_NETWORK_STRUCT_END |
@@ -102,6 +116,9 @@ struct BRANDT_Auction { | |||
102 | * outcome. */ | 116 | * outcome. */ |
103 | uint16_t outcome_public; | 117 | uint16_t outcome_public; |
104 | 118 | ||
119 | /** Link to the next delayed task (auction start trigger, round trigger) */ | ||
120 | struct GNUNET_SCHEDULER_Task *task; | ||
121 | |||
105 | void *closure; /** auction closure given by the user */ | 122 | void *closure; /** auction closure given by the user */ |
106 | 123 | ||
107 | BRANDT_CbBroadcast bcast; /** broadcast callback */ | 124 | BRANDT_CbBroadcast bcast; /** broadcast callback */ |