aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct/scalarproduct_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalarproduct/scalarproduct_api.c')
-rw-r--r--src/scalarproduct/scalarproduct_api.c192
1 files changed, 95 insertions, 97 deletions
diff --git a/src/scalarproduct/scalarproduct_api.c b/src/scalarproduct/scalarproduct_api.c
index eea17c8f6..cae045d62 100644
--- a/src/scalarproduct/scalarproduct_api.c
+++ b/src/scalarproduct/scalarproduct_api.c
@@ -124,96 +124,9 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
124 124
125 125
126/** 126/**
127 * Handles the STATUS received from the service for a response, does 127 * Called when a response is received from the service. After basic
128 * not contain a payload. Called when we participate as "Bob" via 128 * check, the handler in `h->response_proc` is called. This functions
129 * #GNUNET_SCALARPRODUCT_accept_computation(). 129 * handles the response to the client which used the API.
130 *
131 * @param h our Handle
132 * @param msg the response received
133 * @param status the condition the request was terminated with (eg: disconnect)
134 */
135static void
136process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
137 const struct ClientResponseMessage *msg,
138 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
139{
140 if (NULL != h->cont_status)
141 h->cont_status (h->cont_cls,
142 status);
143 GNUNET_SCALARPRODUCT_cancel (h);
144}
145
146
147/**
148 * Handles the RESULT received from the service for a request, should
149 * contain a result MPI value. Called when we participate as "Alice" via
150 * #GNUNET_SCALARPRODUCT_start_computation().
151 *
152 * @param h our Handle
153 * @param msg Pointer to the response received
154 * @param status the condition the request was terminated with (eg: disconnect)
155 */
156static void
157process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
158 const struct ClientResponseMessage *msg,
159 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
160{
161 size_t product_len;
162 gcry_mpi_t result = NULL;
163 gcry_error_t rc;
164 gcry_mpi_t num;
165 size_t rsize;
166
167 if ( (GNUNET_SCALARPRODUCT_Status_Success == status) &&
168 ( (NULL == msg) ||
169 ( (ntohs (msg->header.size) - sizeof (struct ClientResponseMessage)
170 != (product_len = ntohl (msg->product_length))) ) ) )
171 {
172 GNUNET_break (0);
173 status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
174 }
175 if (GNUNET_SCALARPRODUCT_Status_Success == status)
176 {
177 result = gcry_mpi_new (0);
178
179 if (0 < product_len)
180 {
181 rsize = 0;
182 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
183 &msg[1],
184 product_len,
185 &rsize)))
186 {
187 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
188 "gcry_mpi_scan",
189 rc);
190 gcry_mpi_release (result);
191 result = NULL;
192 status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
193 }
194 else
195 {
196 if (0 < ntohl (msg->range))
197 gcry_mpi_add (result, result, num);
198 else if (0 > ntohl (msg->range))
199 gcry_mpi_sub (result, result, num);
200 gcry_mpi_release (num);
201 }
202 }
203 }
204 h->cont_datum (h->cont_cls,
205 status,
206 result);
207 if (NULL != result)
208 gcry_mpi_release (result);
209 GNUNET_SCALARPRODUCT_cancel (h);
210}
211
212
213/**
214 * Called when a response is received from the service. After basic check, the
215 * handler in qe->response_proc is called. This functions handles the response
216 * to the client which used the API.
217 * 130 *
218 * @param cls Pointer to the Master Context 131 * @param cls Pointer to the Master Context
219 * @param msg Pointer to the data received in response 132 * @param msg Pointer to the data received in response
@@ -224,6 +137,7 @@ receive_cb (void *cls,
224{ 137{
225 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls; 138 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
226 const struct ClientResponseMessage *message; 139 const struct ClientResponseMessage *message;
140 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
227 141
228 if (NULL == msg) 142 if (NULL == msg)
229 { 143 {
@@ -231,28 +145,31 @@ receive_cb (void *cls,
231 "Disconnected from SCALARPRODUCT service.\n"); 145 "Disconnected from SCALARPRODUCT service.\n");
232 h->response_proc (h, 146 h->response_proc (h,
233 NULL, 147 NULL,
234 GNUNET_SCALARPRODUCT_Status_ServiceDisconnected); 148 GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
235 return; 149 return;
236 } 150 }
237 if (ntohs (msg->size) != sizeof (struct ClientResponseMessage)) 151 if (ntohs (msg->size) < sizeof (struct ClientResponseMessage))
238 { 152 {
239 GNUNET_break (0); 153 GNUNET_break (0);
240 h->response_proc (h, 154 h->response_proc (h,
241 NULL, 155 NULL,
242 GNUNET_SCALARPRODUCT_Status_InvalidResponse); 156 GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE);
243 return; 157 return;
244 } 158 }
245 message = (const struct ClientResponseMessage *) msg; 159 message = (const struct ClientResponseMessage *) msg;
246 if (GNUNET_SYSERR == ntohl (message->status)) 160 if (ntohs (msg->size) !=
161 ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
247 { 162 {
163 GNUNET_break (0);
248 h->response_proc (h, 164 h->response_proc (h,
249 NULL, 165 NULL,
250 GNUNET_SCALARPRODUCT_Status_Failure); 166 GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE);
251 return; 167 return;
252 } 168 }
169 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
253 h->response_proc (h, 170 h->response_proc (h,
254 message, 171 message,
255 GNUNET_SCALARPRODUCT_Status_Success); 172 status);
256} 173}
257 174
258 175
@@ -282,7 +199,7 @@ do_send_message (void *cls,
282 "Failed to transmit request to SCALARPRODUCT.\n"); 199 "Failed to transmit request to SCALARPRODUCT.\n");
283 /* notify caller about the error, done here */ 200 /* notify caller about the error, done here */
284 h->response_proc (h, NULL, 201 h->response_proc (h, NULL,
285 GNUNET_SCALARPRODUCT_Status_Failure); 202 GNUNET_SCALARPRODUCT_STATUS_FAILURE);
286 return 0; 203 return 0;
287 } 204 }
288 ret = ntohs (h->msg->size); 205 ret = ntohs (h->msg->size);
@@ -330,6 +247,27 @@ do_send_message (void *cls,
330 247
331 248
332/** 249/**
250 * Handles the STATUS received from the service for a response, does
251 * not contain a payload. Called when we participate as "Bob" via
252 * #GNUNET_SCALARPRODUCT_accept_computation().
253 *
254 * @param h our Handle
255 * @param msg the response received
256 * @param status the condition the request was terminated with (eg: disconnect)
257 */
258static void
259process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
260 const struct ClientResponseMessage *msg,
261 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
262{
263 if (NULL != h->cont_status)
264 h->cont_status (h->cont_cls,
265 status);
266 GNUNET_SCALARPRODUCT_cancel (h);
267}
268
269
270/**
333 * Used by Bob's client to cooperate with Alice, 271 * Used by Bob's client to cooperate with Alice,
334 * 272 *
335 * @param cfg the gnunet configuration handle 273 * @param cfg the gnunet configuration handle
@@ -410,6 +348,66 @@ GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handl
410 348
411 349
412/** 350/**
351 * Handles the RESULT received from the service for a request, should
352 * contain a result MPI value. Called when we participate as "Alice" via
353 * #GNUNET_SCALARPRODUCT_start_computation().
354 *
355 * @param h our Handle
356 * @param msg Pointer to the response received
357 * @param status the condition the request was terminated with (eg: disconnect)
358 */
359static void
360process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
361 const struct ClientResponseMessage *msg,
362 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
363{
364 uint32_t product_len;
365 gcry_mpi_t result = NULL;
366 gcry_error_t rc;
367 gcry_mpi_t num;
368 size_t rsize;
369
370 if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
371 {
372 result = gcry_mpi_new (0);
373
374 product_len = ntohl (msg->product_length);
375 if (0 < product_len)
376 {
377 rsize = 0;
378 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
379 &msg[1],
380 product_len,
381 &rsize)))
382 {
383 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
384 "gcry_mpi_scan",
385 rc);
386 gcry_mpi_release (result);
387 result = NULL;
388 status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
389 }
390 else
391 {
392 if (0 < ntohl (msg->range))
393 gcry_mpi_add (result, result, num);
394 else if (0 > ntohl (msg->range))
395 gcry_mpi_sub (result, result, num);
396 gcry_mpi_release (num);
397 }
398 }
399 }
400 if (NULL != h->cont_datum)
401 h->cont_datum (h->cont_cls,
402 status,
403 result);
404 if (NULL != result)
405 gcry_mpi_release (result);
406 GNUNET_SCALARPRODUCT_cancel (h);
407}
408
409
410/**
413 * Request by Alice's client for computing a scalar product 411 * Request by Alice's client for computing a scalar product
414 * 412 *
415 * @param cfg the gnunet configuration handle 413 * @param cfg the gnunet configuration handle