2 This file is part of GNUnet.
3 Copyright (C) 2013-2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file scalarproduct/gnunet-service-scalarproduct-ecc_bob.c
22 * @brief scalarproduct service implementation
23 * @author Christian M. Fuchs
24 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_cadet_service.h"
32 #include "gnunet_applications.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_scalarproduct_service.h"
35 #include "gnunet_set_service.h"
36 #include "scalarproduct.h"
37 #include "gnunet-service-scalarproduct-ecc.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-bob", __VA_ARGS__)
43 * An encrypted element key-value pair.
48 * Key used to identify matching pairs of values to multiply.
49 * Points into an existing data structure, to avoid copying
50 * and doubling memory use.
52 const struct GNUNET_HashCode *key;
55 * Value represented (a).
62 * A scalarproduct session which tracks an offer for a
63 * multiplication service by a local client.
65 struct BobServiceSession
69 * The client this request is related to.
71 struct GNUNET_SERVICE_Client *client;
74 * Client message queue.
76 struct GNUNET_MQ_Handle *client_mq;
79 * All non-0-value'd elements transmitted to us.
81 struct GNUNET_CONTAINER_MultiHashMap *intersected_elements;
84 * Set of elements for which we will be conducting an intersection.
85 * The resulting elements are then used for computing the scalar product.
87 struct GNUNET_SET_Handle *intersection_set;
90 * Set of elements for which will conduction an intersection.
91 * the resulting elements are then used for computing the scalar product.
93 struct GNUNET_SET_OperationHandle *intersection_op;
98 struct GNUNET_CADET_Port *port;
103 struct MpiElement *sorted_elements;
106 * Product of the g_i^{b_i}
108 gcry_mpi_point_t prod_g_i_b_i;
111 * Product of the h_i^{b_i}
113 gcry_mpi_point_t prod_h_i_b_i;
116 * How many elements will be supplied in total from the client.
121 * Already transferred elements (received) for multipart
122 * messages from client. Always less than @e total.
124 uint32_t client_received_element_count;
127 * How many elements actually are used for the scalar product.
128 * Size of the arrays in @e r and @e r_prime. Also sometimes
129 * used as an index into the arrays during construction.
131 uint32_t used_element_count;
134 * Counts the number of values received from Alice by us.
135 * Always less than @e used_element_count.
137 uint32_t cadet_received_element_count;
140 * State of this session. In
141 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
142 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
143 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
145 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
148 * Are we already in #destroy_service_session()?
155 struct GNUNET_CADET_Channel *channel;
158 * Originator's peer identity. (Only for diagnostics.)
160 struct GNUNET_PeerIdentity peer;
163 * (hopefully) unique transaction ID
165 struct GNUNET_HashCode session_id;
168 * The message queue for this channel.
170 struct GNUNET_MQ_Handle *cadet_mq;
176 * GNUnet configuration handle
178 static const struct GNUNET_CONFIGURATION_Handle *cfg;
181 * Handle to the CADET service.
183 static struct GNUNET_CADET_Handle *my_cadet;
186 * Context for DLOG operations on a curve.
188 static struct GNUNET_CRYPTO_EccDlogContext *edc;
192 * Callback used to free the elements in the map.
195 * @param key key of the element
196 * @param value the value to free
199 free_element_cb (void *cls,
200 const struct GNUNET_HashCode *key,
203 struct GNUNET_SCALARPRODUCT_Element *element = value;
205 GNUNET_free (element);
211 * Destroy session state, we are done with it.
213 * @param session the session to free elements from
216 destroy_service_session (struct BobServiceSession *s)
220 if (GNUNET_YES == s->in_destroy)
222 s->in_destroy = GNUNET_YES;
223 if (NULL != s->client)
225 struct GNUNET_SERVICE_Client *c = s->client;
228 GNUNET_SERVICE_client_drop (c);
230 if (NULL != s->intersected_elements)
232 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
235 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
236 s->intersected_elements = NULL;
238 if (NULL != s->intersection_op)
240 GNUNET_SET_operation_cancel (s->intersection_op);
241 s->intersection_op = NULL;
243 if (NULL != s->intersection_set)
245 GNUNET_SET_destroy (s->intersection_set);
246 s->intersection_set = NULL;
248 if (NULL != s->sorted_elements)
250 for (i=0;i<s->used_element_count;i++)
251 gcry_mpi_release (s->sorted_elements[i].value);
252 GNUNET_free (s->sorted_elements);
253 s->sorted_elements = NULL;
255 if (NULL != s->prod_g_i_b_i)
257 gcry_mpi_point_release (s->prod_g_i_b_i);
258 s->prod_g_i_b_i = NULL;
260 if (NULL != s->prod_h_i_b_i)
262 gcry_mpi_point_release (s->prod_h_i_b_i);
263 s->prod_h_i_b_i = NULL;
267 GNUNET_CADET_close_port (s->port);
270 if (NULL != s->channel)
272 GNUNET_CADET_channel_destroy (s->channel);
280 * Notify the client that the session has succeeded or failed. This
281 * message gets sent to Bob's client if the operation completed or
282 * Alice disconnected.
284 * @param session the associated client session to fail or succeed
287 prepare_client_end_notification (struct BobServiceSession *session)
289 struct ClientResponseMessage *msg;
290 struct GNUNET_MQ_Envelope *e;
292 if (NULL == session->client_mq)
293 return; /* no client left to be notified */
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Sending session-end notification with status %d to client for session %s\n",
297 GNUNET_h2s (&session->session_id));
298 e = GNUNET_MQ_msg (msg,
299 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
301 msg->product_length = htonl (0);
302 msg->status = htonl (session->status);
303 GNUNET_MQ_send (session->client_mq,
309 * Function called whenever a channel is destroyed. Should clean up
310 * any associated state.
312 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
314 * @param cls the `struct BobServiceSession`
315 * @param channel connection to the other end (henceforth invalid)
316 * @param channel_ctx place where local state associated
317 * with the channel is stored
320 cb_channel_destruction (void *cls,
321 const struct GNUNET_CADET_Channel *channel)
323 struct BobServiceSession *s = cls;
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Peer disconnected, terminating session %s with peer %s\n",
327 GNUNET_h2s (&s->session_id),
328 GNUNET_i2s (&s->peer));
330 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
332 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
333 prepare_client_end_notification (s);
335 destroy_service_session (s);
340 * MQ finished giving our last message to CADET, now notify
341 * the client that we are finished.
344 bob_cadet_done_cb (void *cls)
346 struct BobServiceSession *session = cls;
348 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS;
349 prepare_client_end_notification (session);
354 * Bob generates the response message to be sent to Alice.
356 * @param s the associated requesting session with Alice
359 transmit_bobs_cryptodata_message (struct BobServiceSession *s)
361 struct EccBobCryptodataMessage *msg;
362 struct GNUNET_MQ_Envelope *e;
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Sending response to Alice\n");
366 e = GNUNET_MQ_msg (msg,
367 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA);
368 msg->contained_element_count = htonl (2);
369 if (NULL != s->prod_g_i_b_i)
370 GNUNET_CRYPTO_ecc_point_to_bin (edc,
373 if (NULL != s->prod_h_i_b_i)
374 GNUNET_CRYPTO_ecc_point_to_bin (edc,
377 GNUNET_MQ_notify_sent (e,
380 GNUNET_MQ_send (s->cadet_mq,
386 * Iterator to copy over messages from the hash map
387 * into an array for sorting.
389 * @param cls the `struct BobServiceSession *`
390 * @param key the key (unused)
391 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
392 * TODO: code duplication with Alice!
395 copy_element_cb (void *cls,
396 const struct GNUNET_HashCode *key,
399 struct BobServiceSession *s = cls;
400 struct GNUNET_SCALARPRODUCT_Element *e = value;
404 mval = gcry_mpi_new (0);
405 val = (int64_t) GNUNET_ntohll (e->value);
407 gcry_mpi_sub_ui (mval, mval, -val);
409 gcry_mpi_add_ui (mval, mval, val);
410 s->sorted_elements [s->used_element_count].value = mval;
411 s->sorted_elements [s->used_element_count].key = &e->key;
412 s->used_element_count++;
418 * Compare two `struct MpiValue`s by key for sorting.
420 * @param a pointer to first `struct MpiValue *`
421 * @param b pointer to first `struct MpiValue *`
422 * @return -1 for a < b, 0 for a=b, 1 for a > b.
423 * TODO: code duplication with Alice!
426 element_cmp (const void *a,
429 const struct MpiElement *ma = a;
430 const struct MpiElement *mb = b;
432 return GNUNET_CRYPTO_hash_cmp (ma->key,
438 * Check a multipart-chunk of a request from another service to
439 * calculate a scalarproduct with us.
441 * @param cls closure (set from #GNUNET_CADET_connect)
442 * @param msg the actual message
443 * @return #GNUNET_OK to keep the connection open,
444 * #GNUNET_SYSERR to close it (signal serious error)
447 check_alices_cryptodata_message (void *cls,
448 const struct EccAliceCryptodataMessage *msg)
450 struct BobServiceSession *s = cls;
451 uint32_t contained_elements;
456 msize = ntohs (msg->header.size);
457 if (msize <= sizeof (struct EccAliceCryptodataMessage))
460 return GNUNET_SYSERR;
462 contained_elements = ntohl (msg->contained_element_count);
463 /* Our intersection may still be ongoing, but this is nevertheless
464 an upper bound on the required array size */
465 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
466 msg_length = sizeof (struct EccAliceCryptodataMessage)
467 + contained_elements * sizeof (struct GNUNET_CRYPTO_EccPoint) * 2;
468 if ( (msize != msg_length) ||
469 (0 == contained_elements) ||
470 (contained_elements > UINT16_MAX) ||
471 (max < contained_elements + s->cadet_received_element_count) )
474 return GNUNET_SYSERR;
481 * Handle a multipart-chunk of a request from another service to
482 * calculate a scalarproduct with us.
484 * @param cls closure (set from #GNUNET_CADET_connect)
485 * @param msg the actual message
488 handle_alices_cryptodata_message (void *cls,
489 const struct EccAliceCryptodataMessage *msg)
491 struct BobServiceSession *s = cls;
492 const struct GNUNET_CRYPTO_EccPoint *payload;
493 uint32_t contained_elements;
496 const struct MpiElement *b_i;
497 gcry_mpi_point_t tmp;
498 gcry_mpi_point_t g_i;
499 gcry_mpi_point_t h_i;
500 gcry_mpi_point_t g_i_b_i;
501 gcry_mpi_point_t h_i_b_i;
503 contained_elements = ntohl (msg->contained_element_count);
504 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
505 /* sort our vector for the computation */
506 if (NULL == s->sorted_elements)
509 = GNUNET_new_array (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements),
511 s->used_element_count = 0;
512 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
515 qsort (s->sorted_elements,
516 s->used_element_count,
517 sizeof (struct MpiElement),
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Received %u crypto values from Alice\n",
523 (unsigned int) contained_elements);
524 payload = (const struct GNUNET_CRYPTO_EccPoint *) &msg[1];
526 for (i=0;i<contained_elements;i++)
528 b_i = &s->sorted_elements[i + s->cadet_received_element_count];
529 g_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
531 g_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
534 gcry_mpi_point_release (g_i);
535 h_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
536 &payload[i * 2 + 1]);
537 h_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
540 gcry_mpi_point_release (h_i);
541 if (0 == i + s->cadet_received_element_count)
543 /* first iteration, nothing to add */
544 s->prod_g_i_b_i = g_i_b_i;
545 s->prod_h_i_b_i = h_i_b_i;
549 /* further iterations, cummulate resulting value */
550 tmp = GNUNET_CRYPTO_ecc_add (edc,
553 gcry_mpi_point_release (s->prod_g_i_b_i);
554 gcry_mpi_point_release (g_i_b_i);
555 s->prod_g_i_b_i = tmp;
556 tmp = GNUNET_CRYPTO_ecc_add (edc,
559 gcry_mpi_point_release (s->prod_h_i_b_i);
560 gcry_mpi_point_release (h_i_b_i);
561 s->prod_h_i_b_i = tmp;
564 s->cadet_received_element_count += contained_elements;
565 if ( (s->cadet_received_element_count == max) &&
566 (NULL == s->intersection_op) )
568 /* intersection has finished also on our side, and
569 we got the full set, so we can proceed with the
571 transmit_bobs_cryptodata_message (s);
573 GNUNET_CADET_receive_done (s->channel);
578 * Callback for set operation results. Called for each element
579 * that needs to be removed from the result set.
581 * @param cls closure with the `struct BobServiceSession`
582 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
583 * @param status what has happened with the set intersection?
586 cb_intersection_element_removed (void *cls,
587 const struct GNUNET_SET_Element *element,
588 enum GNUNET_SET_Status status)
590 struct BobServiceSession *s = cls;
591 struct GNUNET_SCALARPRODUCT_Element *se;
595 case GNUNET_SET_STATUS_OK:
596 /* this element has been removed from the set */
597 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
599 GNUNET_assert (NULL != se);
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Removed element with key %s and value %lld\n",
602 GNUNET_h2s (&se->key),
603 (long long) GNUNET_ntohll (se->value));
604 GNUNET_assert (GNUNET_YES ==
605 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
610 case GNUNET_SET_STATUS_DONE:
611 s->intersection_op = NULL;
612 GNUNET_break (NULL == s->intersection_set);
613 GNUNET_CADET_receive_done (s->channel);
614 LOG (GNUNET_ERROR_TYPE_DEBUG,
615 "Finished intersection, %d items remain\n",
616 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements));
617 if (s->client_received_element_count ==
618 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements))
620 /* CADET transmission from Alice is also already done,
621 start with our own reply */
622 transmit_bobs_cryptodata_message (s);
625 case GNUNET_SET_STATUS_HALF_DONE:
626 /* unexpected for intersection */
629 case GNUNET_SET_STATUS_FAILURE:
630 /* unhandled status code */
631 LOG (GNUNET_ERROR_TYPE_DEBUG,
632 "Set intersection failed!\n");
633 s->intersection_op = NULL;
634 if (NULL != s->intersection_set)
636 GNUNET_SET_destroy (s->intersection_set);
637 s->intersection_set = NULL;
639 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
640 prepare_client_end_notification (s);
650 * We've paired up a client session with an incoming CADET request.
651 * Initiate set intersection work.
653 * @param s client session to start intersection for
656 start_intersection (struct BobServiceSession *s)
658 struct GNUNET_HashCode set_sid;
660 GNUNET_CRYPTO_hash (&s->session_id,
661 sizeof (struct GNUNET_HashCode),
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Got session with key %s and %u elements, starting intersection.\n",
665 GNUNET_h2s (&s->session_id),
666 (unsigned int) s->total);
669 = GNUNET_SET_prepare (&s->peer,
672 GNUNET_SET_RESULT_REMOVED,
673 &cb_intersection_element_removed,
676 GNUNET_SET_commit (s->intersection_op,
677 s->intersection_set))
680 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
681 prepare_client_end_notification (s);
684 GNUNET_SET_destroy (s->intersection_set);
685 s->intersection_set = NULL;
690 * Handle a request from Alice to calculate a scalarproduct with us (Bob).
692 * @param cls closure (set from #GNUNET_CADET_connect)
693 * @param msg the actual message
696 handle_alices_computation_request (void *cls,
697 const struct EccServiceRequestMessage *msg)
699 struct BobServiceSession *s = cls;
701 s->session_id = msg->session_id; // ??
702 if (s->client_received_element_count < s->total)
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Alice ready, still waiting for Bob client data!\n");
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Both ready, launching intersection!\n");
710 start_intersection (s);
715 * Function called for inbound channels on Bob's end. Does some
716 * preliminary initialization, more happens after we get Alice's first
719 * @param cls our `struct BobServiceSession`
720 * @param channel new handle to the channel
721 * @param initiator peer that started the channel
722 * @return session associated with the channel
725 cb_channel_incoming (void *cls,
726 struct GNUNET_CADET_Channel *channel,
727 const struct GNUNET_PeerIdentity *initiator)
729 struct BobServiceSession *s = cls;
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "New incoming channel from peer %s.\n",
733 GNUNET_i2s (initiator));
734 GNUNET_CADET_close_port (s->port);
736 s->peer = *initiator;
737 s->channel = channel;
738 s->cadet_mq = GNUNET_CADET_get_mq (s->channel);
744 * We're receiving additional set data. Check it is well-formed.
746 * @param cls identification of the client
747 * @param msg the actual message
748 * @return #GNUNET_OK if @a msg is well-formed
751 check_bob_client_message_multipart (void *cls,
752 const struct ComputationBobCryptodataMultipartMessage *msg)
754 struct BobServiceSession *s = cls;
755 uint32_t contained_count;
758 msize = ntohs (msg->header.size);
759 contained_count = ntohl (msg->element_count_contained);
760 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
761 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
762 (0 == contained_count) ||
763 (UINT16_MAX < contained_count) ||
764 (s->total == s->client_received_element_count) ||
765 (s->total < s->client_received_element_count + contained_count) )
768 return GNUNET_SYSERR;
775 * We're receiving additional set data. Add it to our
776 * set and if we are done, initiate the transaction.
778 * @param cls identification of the client
779 * @param msg the actual message
782 handle_bob_client_message_multipart (void *cls,
783 const struct ComputationBobCryptodataMultipartMessage *msg)
785 struct BobServiceSession *s = cls;
786 uint32_t contained_count;
787 const struct GNUNET_SCALARPRODUCT_Element *elements;
788 struct GNUNET_SET_Element set_elem;
789 struct GNUNET_SCALARPRODUCT_Element *elem;
791 contained_count = ntohl (msg->element_count_contained);
792 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
793 for (uint32_t i = 0; i < contained_count; i++)
795 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
798 sizeof (struct GNUNET_SCALARPRODUCT_Element));
800 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
803 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
809 set_elem.data = &elem->key;
810 set_elem.size = sizeof (elem->key);
811 set_elem.element_type = 0;
812 GNUNET_SET_add_element (s->intersection_set,
816 s->client_received_element_count += contained_count;
817 GNUNET_SERVICE_client_continue (s->client);
818 if (s->total != s->client_received_element_count)
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Request still partial, waiting for more client data!\n");
825 if (NULL == s->channel)
827 /* no Alice waiting for this request, wait for Alice */
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Client ready, still waiting for Alice!\n");
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Both ready, launching intersection!\n");
834 start_intersection (s);
839 * Handler for Bob's a client request message. Check @a msg is
842 * @param cls identification of the client
843 * @param msg the actual message
844 * @return #GNUNET_OK if @a msg is well-formed
847 check_bob_client_message (void *cls,
848 const struct BobComputationMessage *msg)
850 struct BobServiceSession *s = cls;
851 uint32_t contained_count;
852 uint32_t total_count;
855 if (GNUNET_SCALARPRODUCT_STATUS_INIT != s->status)
858 return GNUNET_SYSERR;
860 msize = ntohs (msg->header.size);
861 total_count = ntohl (msg->element_count_total);
862 contained_count = ntohl (msg->element_count_contained);
863 if ( (0 == total_count) ||
864 (0 == contained_count) ||
865 (UINT16_MAX < contained_count) ||
866 (msize != (sizeof (struct BobComputationMessage) +
867 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
870 return GNUNET_SYSERR;
877 * Handler for Bob's a client request message. Bob is in the response
878 * role, keep the values + session and waiting for a matching session
879 * or process a waiting request from Alice.
881 * @param cls identification of the client
882 * @param msg the actual message
885 handle_bob_client_message (void *cls,
886 const struct BobComputationMessage *msg)
888 struct BobServiceSession *s = cls;
889 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
890 GNUNET_MQ_hd_fixed_size (alices_computation_request,
891 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION,
892 struct EccServiceRequestMessage,
894 GNUNET_MQ_hd_var_size (alices_cryptodata_message,
895 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA,
896 struct EccAliceCryptodataMessage,
898 GNUNET_MQ_handler_end ()
900 uint32_t contained_count;
901 uint32_t total_count;
902 const struct GNUNET_SCALARPRODUCT_Element *elements;
903 struct GNUNET_SET_Element set_elem;
904 struct GNUNET_SCALARPRODUCT_Element *elem;
906 total_count = ntohl (msg->element_count_total);
907 contained_count = ntohl (msg->element_count_contained);
909 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
910 s->total = total_count;
911 s->client_received_element_count = contained_count;
912 s->session_id = msg->session_key;
913 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
914 s->intersected_elements
915 = GNUNET_CONTAINER_multihashmap_create (s->total,
918 = GNUNET_SET_create (cfg,
919 GNUNET_SET_OPERATION_INTERSECTION);
920 for (uint32_t i = 0; i < contained_count; i++)
922 if (0 == GNUNET_ntohll (elements[i].value))
924 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
927 sizeof (struct GNUNET_SCALARPRODUCT_Element));
929 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
932 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
938 set_elem.data = &elem->key;
939 set_elem.size = sizeof (elem->key);
940 set_elem.element_type = 0;
941 GNUNET_SET_add_element (s->intersection_set,
944 s->used_element_count++;
946 GNUNET_SERVICE_client_continue (s->client);
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Received client request, opening port %s!\n",
949 GNUNET_h2s (&msg->session_key));
950 s->port = GNUNET_CADET_open_porT (my_cadet,
952 &cb_channel_incoming,
955 &cb_channel_destruction,
960 GNUNET_SERVICE_client_drop (s->client);
967 * Task run during shutdown.
972 shutdown_task (void *cls)
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 "Shutting down, initiating cleanup.\n");
976 // FIXME: we have to cut our connections to CADET first!
977 if (NULL != my_cadet)
979 GNUNET_CADET_disconnect (my_cadet);
984 GNUNET_CRYPTO_ecc_dlog_release (edc);
991 * A client connected.
993 * Setup the associated data structure.
995 * @param cls closure, NULL
996 * @param client identification of the client
997 * @param mq message queue to communicate with @a client
998 * @return our `struct BobServiceSession`
1001 client_connect_cb (void *cls,
1002 struct GNUNET_SERVICE_Client *client,
1003 struct GNUNET_MQ_Handle *mq)
1005 struct BobServiceSession *s;
1007 s = GNUNET_new (struct BobServiceSession);
1015 * A client disconnected.
1017 * Remove the associated session(s), release data structures
1018 * and cancel pending outgoing transmissions to the client.
1020 * @param cls closure, NULL
1021 * @param client identification of the client
1022 * @param app_cls our `struct BobServiceSession`
1025 client_disconnect_cb (void *cls,
1026 struct GNUNET_SERVICE_Client *client,
1029 struct BobServiceSession *s = app_cls;
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Client disconnected from us.\n");
1034 destroy_service_session (s);
1039 * Initialization of the program and message handlers
1041 * @param cls closure
1042 * @param c configuration to use
1043 * @param service the initialized service
1047 const struct GNUNET_CONFIGURATION_Handle *c,
1048 struct GNUNET_SERVICE_Handle *service)
1051 /* We don't really do DLOG, so we can setup with very minimal resources */
1052 edc = GNUNET_CRYPTO_ecc_dlog_prepare (4 /* max value */,
1054 my_cadet = GNUNET_CADET_connecT (cfg);
1055 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1057 if (NULL == my_cadet)
1059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1060 _("Connect to CADET failed\n"));
1061 GNUNET_SCHEDULER_shutdown ();
1068 * Define "main" method using service macro.
1071 ("scalarproduct-bob",
1072 GNUNET_SERVICE_OPTION_NONE,
1075 &client_disconnect_cb,
1077 GNUNET_MQ_hd_var_size (bob_client_message,
1078 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1079 struct BobComputationMessage,
1081 GNUNET_MQ_hd_var_size (bob_client_message_multipart,
1082 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB,
1083 struct ComputationBobCryptodataMultipartMessage,
1085 GNUNET_MQ_handler_end ());
1088 /* end of gnunet-service-scalarproduct-ecc_bob.c */