2 This file is part of GNUnet.
3 Copyright (C) 2013-2015 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 * An incoming session from CADET.
64 struct CadetIncomingSession;
68 * A scalarproduct session which tracks an offer for a
69 * multiplication service by a local client.
71 struct BobServiceSession
75 * (hopefully) unique transaction ID
77 struct GNUNET_HashCode session_id;
80 * The client this request is related to.
82 struct GNUNET_SERVICE_Client *client;
85 * Client message queue.
87 struct GNUNET_MQ_Handle *client_mq;
90 * All non-0-value'd elements transmitted to us.
92 struct GNUNET_CONTAINER_MultiHashMap *intersected_elements;
95 * Set of elements for which we will be conducting an intersection.
96 * The resulting elements are then used for computing the scalar product.
98 struct GNUNET_SET_Handle *intersection_set;
101 * Set of elements for which will conduction an intersection.
102 * the resulting elements are then used for computing the scalar product.
104 struct GNUNET_SET_OperationHandle *intersection_op;
109 struct GNUNET_CADET_Port *port;
114 struct MpiElement *sorted_elements;
117 * Product of the g_i^{b_i}
119 gcry_mpi_point_t prod_g_i_b_i;
122 * Product of the h_i^{b_i}
124 gcry_mpi_point_t prod_h_i_b_i;
127 * Handle for our associated incoming CADET session, or NULL
128 * if we have not gotten one yet.
130 struct CadetIncomingSession *cadet;
133 * How many elements will be supplied in total from the client.
138 * Already transferred elements (received) for multipart
139 * messages from client. Always less than @e total.
141 uint32_t client_received_element_count;
144 * How many elements actually are used for the scalar product.
145 * Size of the arrays in @e r and @e r_prime. Also sometimes
146 * used as an index into the arrays during construction.
148 uint32_t used_element_count;
151 * Counts the number of values received from Alice by us.
152 * Always less than @e used_element_count.
154 uint32_t cadet_received_element_count;
157 * State of this session. In
158 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
159 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
160 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
162 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
165 * Are we already in #destroy_service_session()?
173 * An incoming session from CADET.
175 struct CadetIncomingSession
179 * Associated client session, or NULL.
181 struct BobServiceSession *s;
186 struct GNUNET_CADET_Channel *channel;
189 * Originator's peer identity. (Only for diagnostics.)
191 struct GNUNET_PeerIdentity peer;
194 * (hopefully) unique transaction ID
196 struct GNUNET_HashCode session_id;
199 * The message queue for this channel.
201 struct GNUNET_MQ_Handle *cadet_mq;
204 * Has this CADET session been added to the map yet?
205 * #GNUNET_YES if so, in which case @e session_id is
211 * Are we already in #destroy_cadet_session()?
219 * GNUnet configuration handle
221 static const struct GNUNET_CONFIGURATION_Handle *cfg;
224 * Map of `struct BobServiceSession`, by session keys.
226 static struct GNUNET_CONTAINER_MultiHashMap *client_sessions;
229 * Map of `struct CadetIncomingSession`, by session keys.
231 static struct GNUNET_CONTAINER_MultiHashMap *cadet_sessions;
234 * Handle to the CADET service.
236 static struct GNUNET_CADET_Handle *my_cadet;
239 * Context for DLOG operations on a curve.
241 static struct GNUNET_CRYPTO_EccDlogContext *edc;
245 * Finds a not terminated client session in the respective map based on
248 * @param key the session key we want to search for
249 * @return the matching session, or NULL for none
251 static struct BobServiceSession *
252 find_matching_client_session (const struct GNUNET_HashCode *key)
254 return GNUNET_CONTAINER_multihashmap_get (client_sessions,
260 * Finds a CADET session in the respective map based on session key.
262 * @param key the session key we want to search for
263 * @return the matching session, or NULL for none
265 static struct CadetIncomingSession *
266 find_matching_cadet_session (const struct GNUNET_HashCode *key)
268 return GNUNET_CONTAINER_multihashmap_get (cadet_sessions,
274 * Callback used to free the elements in the map.
277 * @param key key of the element
278 * @param value the value to free
281 free_element_cb (void *cls,
282 const struct GNUNET_HashCode *key,
285 struct GNUNET_SCALARPRODUCT_Element *element = value;
287 GNUNET_free (element);
293 * Destroy session state, we are done with it.
295 * @param session the session to free elements from
298 destroy_cadet_session (struct CadetIncomingSession *s);
302 * Destroy session state, we are done with it.
304 * @param session the session to free elements from
307 destroy_service_session (struct BobServiceSession *s)
309 struct CadetIncomingSession *in;
312 if (GNUNET_YES == s->in_destroy)
314 s->in_destroy = GNUNET_YES;
315 if (NULL != (in = s->cadet))
318 destroy_cadet_session (in);
320 if (NULL != s->client)
322 struct GNUNET_SERVICE_Client *c = s->client;
325 GNUNET_SERVICE_client_drop (c);
327 GNUNET_assert (GNUNET_YES ==
328 GNUNET_CONTAINER_multihashmap_remove (client_sessions,
331 if (NULL != s->intersected_elements)
333 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
336 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
337 s->intersected_elements = NULL;
339 if (NULL != s->intersection_op)
341 GNUNET_SET_operation_cancel (s->intersection_op);
342 s->intersection_op = NULL;
344 if (NULL != s->intersection_set)
346 GNUNET_SET_destroy (s->intersection_set);
347 s->intersection_set = NULL;
349 if (NULL != s->sorted_elements)
351 for (i=0;i<s->used_element_count;i++)
352 gcry_mpi_release (s->sorted_elements[i].value);
353 GNUNET_free (s->sorted_elements);
354 s->sorted_elements = NULL;
356 if (NULL != s->prod_g_i_b_i)
358 gcry_mpi_point_release (s->prod_g_i_b_i);
359 s->prod_g_i_b_i = NULL;
361 if (NULL != s->prod_h_i_b_i)
363 gcry_mpi_point_release (s->prod_h_i_b_i);
364 s->prod_h_i_b_i = NULL;
366 GNUNET_CADET_close_port (s->port);
372 * Destroy incoming CADET session state, we are done with it.
374 * @param in the session to free elements from
377 destroy_cadet_session (struct CadetIncomingSession *in)
379 struct BobServiceSession *s;
381 if (GNUNET_YES == in->in_destroy)
383 in->in_destroy = GNUNET_YES;
384 if (NULL != (s = in->s))
387 destroy_service_session (s);
389 if (GNUNET_YES == in->in_map)
391 GNUNET_assert (GNUNET_YES ==
392 GNUNET_CONTAINER_multihashmap_remove (cadet_sessions,
395 in->in_map = GNUNET_NO;
397 if (NULL != in->cadet_mq)
399 GNUNET_MQ_destroy (in->cadet_mq);
402 if (NULL != in->channel)
404 GNUNET_CADET_channel_destroy (in->channel);
412 * Notify the client that the session has succeeded or failed. This
413 * message gets sent to Bob's client if the operation completed or
414 * Alice disconnected.
416 * @param session the associated client session to fail or succeed
419 prepare_client_end_notification (struct BobServiceSession *session)
421 struct ClientResponseMessage *msg;
422 struct GNUNET_MQ_Envelope *e;
424 if (NULL == session->client_mq)
425 return; /* no client left to be notified */
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Sending session-end notification with status %d to client for session %s\n",
429 GNUNET_h2s (&session->session_id));
430 e = GNUNET_MQ_msg (msg,
431 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
433 msg->product_length = htonl (0);
434 msg->status = htonl (session->status);
435 GNUNET_MQ_send (session->client_mq,
441 * Function called whenever a channel is destroyed. Should clean up
442 * any associated state.
444 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
446 * @param cls closure (set from #GNUNET_CADET_connect())
447 * @param channel connection to the other end (henceforth invalid)
448 * @param channel_ctx place where local state associated
449 * with the channel is stored
452 cb_channel_destruction (void *cls,
453 const struct GNUNET_CADET_Channel *channel,
456 struct CadetIncomingSession *in = channel_ctx;
457 struct BobServiceSession *s;
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "Peer disconnected, terminating session %s with peer %s\n",
461 GNUNET_h2s (&in->session_id),
462 GNUNET_i2s (&in->peer));
463 if (NULL != in->cadet_mq)
465 GNUNET_MQ_destroy (in->cadet_mq);
469 if (NULL != (s = in->s))
471 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
473 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
474 prepare_client_end_notification (s);
477 destroy_cadet_session (in);
482 * MQ finished giving our last message to CADET, now notify
483 * the client that we are finished.
486 bob_cadet_done_cb (void *cls)
488 struct BobServiceSession *session = cls;
490 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS;
491 prepare_client_end_notification (session);
496 * Bob generates the response message to be sent to Alice.
498 * @param s the associated requesting session with Alice
501 transmit_bobs_cryptodata_message (struct BobServiceSession *s)
503 struct EccBobCryptodataMessage *msg;
504 struct GNUNET_MQ_Envelope *e;
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Sending response to Alice\n");
508 e = GNUNET_MQ_msg (msg,
509 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA);
510 msg->contained_element_count = htonl (2);
511 if (NULL != s->prod_g_i_b_i)
512 GNUNET_CRYPTO_ecc_point_to_bin (edc,
515 if (NULL != s->prod_h_i_b_i)
516 GNUNET_CRYPTO_ecc_point_to_bin (edc,
519 GNUNET_MQ_notify_sent (e,
522 GNUNET_MQ_send (s->cadet->cadet_mq,
528 * Iterator to copy over messages from the hash map
529 * into an array for sorting.
531 * @param cls the `struct BobServiceSession *`
532 * @param key the key (unused)
533 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
534 * TODO: code duplication with Alice!
537 copy_element_cb (void *cls,
538 const struct GNUNET_HashCode *key,
541 struct BobServiceSession *s = cls;
542 struct GNUNET_SCALARPRODUCT_Element *e = value;
546 mval = gcry_mpi_new (0);
547 val = (int64_t) GNUNET_ntohll (e->value);
549 gcry_mpi_sub_ui (mval, mval, -val);
551 gcry_mpi_add_ui (mval, mval, val);
552 s->sorted_elements [s->used_element_count].value = mval;
553 s->sorted_elements [s->used_element_count].key = &e->key;
554 s->used_element_count++;
560 * Compare two `struct MpiValue`s by key for sorting.
562 * @param a pointer to first `struct MpiValue *`
563 * @param b pointer to first `struct MpiValue *`
564 * @return -1 for a < b, 0 for a=b, 1 for a > b.
565 * TODO: code duplication with Alice!
568 element_cmp (const void *a,
571 const struct MpiElement *ma = a;
572 const struct MpiElement *mb = b;
574 return GNUNET_CRYPTO_hash_cmp (ma->key,
580 * Handle a multipart-chunk of a request from another service to
581 * calculate a scalarproduct with us.
583 * @param cls closure (set from #GNUNET_CADET_connect)
584 * @param channel connection to the other end
585 * @param channel_ctx place to store local state associated with the @a channel
586 * @param message the actual message
587 * @return #GNUNET_OK to keep the connection open,
588 * #GNUNET_SYSERR to close it (signal serious error)
591 handle_alices_cryptodata_message (void *cls,
592 struct GNUNET_CADET_Channel *channel,
594 const struct GNUNET_MessageHeader *message)
596 struct CadetIncomingSession *in = *channel_ctx;
597 struct BobServiceSession *s;
598 const struct EccAliceCryptodataMessage *msg;
599 const struct GNUNET_CRYPTO_EccPoint *payload;
600 uint32_t contained_elements;
605 const struct MpiElement *b_i;
606 gcry_mpi_point_t tmp;
607 gcry_mpi_point_t g_i;
608 gcry_mpi_point_t h_i;
609 gcry_mpi_point_t g_i_b_i;
610 gcry_mpi_point_t h_i_b_i;
616 return GNUNET_SYSERR;
622 return GNUNET_SYSERR;
624 /* sort our vector for the computation */
625 if (NULL == s->sorted_elements)
628 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
629 sizeof (struct MpiElement));
630 s->used_element_count = 0;
631 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
634 qsort (s->sorted_elements,
635 s->used_element_count,
636 sizeof (struct MpiElement),
641 msize = ntohs (message->size);
642 if (msize <= sizeof (struct EccAliceCryptodataMessage))
645 return GNUNET_SYSERR;
647 msg = (const struct EccAliceCryptodataMessage *) message;
648 contained_elements = ntohl (msg->contained_element_count);
649 /* Our intersection may still be ongoing, but this is nevertheless
650 an upper bound on the required array size */
651 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
652 msg_length = sizeof (struct EccAliceCryptodataMessage)
653 + contained_elements * sizeof (struct GNUNET_CRYPTO_EccPoint) * 2;
654 if ( (msize != msg_length) ||
655 (0 == contained_elements) ||
656 (contained_elements > UINT16_MAX) ||
657 (max < contained_elements + s->cadet_received_element_count) )
660 return GNUNET_SYSERR;
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Received %u crypto values from Alice\n",
664 (unsigned int) contained_elements);
665 payload = (const struct GNUNET_CRYPTO_EccPoint *) &msg[1];
667 for (i=0;i<contained_elements;i++)
669 b_i = &s->sorted_elements[i + s->cadet_received_element_count];
670 g_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
672 g_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
675 gcry_mpi_point_release (g_i);
676 h_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
677 &payload[i * 2 + 1]);
678 h_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
681 gcry_mpi_point_release (h_i);
682 if (0 == i + s->cadet_received_element_count)
684 /* first iteration, nothing to add */
685 s->prod_g_i_b_i = g_i_b_i;
686 s->prod_h_i_b_i = h_i_b_i;
690 /* further iterations, cummulate resulting value */
691 tmp = GNUNET_CRYPTO_ecc_add (edc,
694 gcry_mpi_point_release (s->prod_g_i_b_i);
695 gcry_mpi_point_release (g_i_b_i);
696 s->prod_g_i_b_i = tmp;
697 tmp = GNUNET_CRYPTO_ecc_add (edc,
700 gcry_mpi_point_release (s->prod_h_i_b_i);
701 gcry_mpi_point_release (h_i_b_i);
702 s->prod_h_i_b_i = tmp;
705 s->cadet_received_element_count += contained_elements;
706 if ( (s->cadet_received_element_count == max) &&
707 (NULL == s->intersection_op) )
709 /* intersection has finished also on our side, and
710 we got the full set, so we can proceed with the
712 transmit_bobs_cryptodata_message (s);
714 GNUNET_CADET_receive_done (s->cadet->channel);
720 * Callback for set operation results. Called for each element
721 * that needs to be removed from the result set.
723 * @param cls closure with the `struct BobServiceSession`
724 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
725 * @param status what has happened with the set intersection?
728 cb_intersection_element_removed (void *cls,
729 const struct GNUNET_SET_Element *element,
730 enum GNUNET_SET_Status status)
732 struct BobServiceSession *s = cls;
733 struct GNUNET_SCALARPRODUCT_Element *se;
737 case GNUNET_SET_STATUS_OK:
738 /* this element has been removed from the set */
739 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
741 GNUNET_assert (NULL != se);
742 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "Removed element with key %s and value %lld\n",
744 GNUNET_h2s (&se->key),
745 (long long) GNUNET_ntohll (se->value));
746 GNUNET_assert (GNUNET_YES ==
747 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
752 case GNUNET_SET_STATUS_DONE:
753 s->intersection_op = NULL;
754 GNUNET_break (NULL == s->intersection_set);
755 GNUNET_CADET_receive_done (s->cadet->channel);
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "Finished intersection, %d items remain\n",
758 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements));
759 if (s->client_received_element_count ==
760 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements))
762 /* CADET transmission from Alice is also already done,
763 start with our own reply */
764 transmit_bobs_cryptodata_message (s);
767 case GNUNET_SET_STATUS_HALF_DONE:
768 /* unexpected for intersection */
771 case GNUNET_SET_STATUS_FAILURE:
772 /* unhandled status code */
773 LOG (GNUNET_ERROR_TYPE_DEBUG,
774 "Set intersection failed!\n");
775 s->intersection_op = NULL;
776 if (NULL != s->intersection_set)
778 GNUNET_SET_destroy (s->intersection_set);
779 s->intersection_set = NULL;
781 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
782 prepare_client_end_notification (s);
792 * We've paired up a client session with an incoming CADET request.
793 * Initiate set intersection work.
795 * @param s client session to start intersection for
798 start_intersection (struct BobServiceSession *s)
800 struct GNUNET_HashCode set_sid;
802 GNUNET_CRYPTO_hash (&s->session_id,
803 sizeof (struct GNUNET_HashCode),
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Got session with key %s and %u elements, starting intersection.\n",
807 GNUNET_h2s (&s->session_id),
808 (unsigned int) s->total);
811 = GNUNET_SET_prepare (&s->cadet->peer,
814 GNUNET_SET_RESULT_REMOVED,
815 &cb_intersection_element_removed,
818 GNUNET_SET_commit (s->intersection_op,
819 s->intersection_set))
822 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
823 prepare_client_end_notification (s);
826 GNUNET_SET_destroy (s->intersection_set);
827 s->intersection_set = NULL;
832 * Handle a request from Alice to calculate a scalarproduct with us (Bob).
834 * @param cls closure (set from #GNUNET_CADET_connect)
835 * @param channel connection to the other end
836 * @param channel_ctx place to store the `struct CadetIncomingSession *`
837 * @param message the actual message
838 * @return #GNUNET_OK to keep the connection open,
839 * #GNUNET_SYSERR to close it (signal serious error)
842 handle_alices_computation_request (void *cls,
843 struct GNUNET_CADET_Channel *channel,
845 const struct GNUNET_MessageHeader *message)
847 struct CadetIncomingSession *in = *channel_ctx;
848 struct BobServiceSession *s;
849 const struct EccServiceRequestMessage *msg;
851 msg = (const struct EccServiceRequestMessage *) message;
852 if (GNUNET_YES == in->in_map)
855 return GNUNET_SYSERR;
857 if (NULL != find_matching_cadet_session (&msg->session_id))
859 /* not unique, got one like this already */
861 return GNUNET_SYSERR;
863 in->session_id = msg->session_id;
864 GNUNET_assert (GNUNET_YES ==
865 GNUNET_CONTAINER_multihashmap_put (cadet_sessions,
868 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
869 s = find_matching_client_session (&in->session_id);
872 /* no client waiting for this request, wait for client */
875 GNUNET_assert (NULL == s->cadet);
879 if (s->client_received_element_count == s->total)
880 start_intersection (s);
886 * Function called for inbound channels on Bob's end. Does some
887 * preliminary initialization, more happens after we get Alice's first
891 * @param channel new handle to the channel
892 * @param initiator peer that started the channel
894 * @param options unused
895 * @return session associated with the channel
898 cb_channel_incoming (void *cls,
899 struct GNUNET_CADET_Channel *channel,
900 const struct GNUNET_PeerIdentity *initiator,
901 const struct GNUNET_HashCode *port,
902 enum GNUNET_CADET_ChannelOption options)
904 struct CadetIncomingSession *in;
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "New incoming channel from peer %s.\n",
908 GNUNET_i2s (initiator));
909 in = GNUNET_new (struct CadetIncomingSession);
910 in->peer = *initiator;
911 in->channel = channel;
912 in->cadet_mq = GNUNET_CADET_mq_create (in->channel);
918 * We're receiving additional set data. Check it is well-formed.
920 * @param cls identification of the client
921 * @param msg the actual message
922 * @return #GNUNET_OK if @a msg is well-formed
925 check_bob_client_message_multipart (void *cls,
926 const struct ComputationBobCryptodataMultipartMessage *msg)
928 struct BobServiceSession *s = cls;
929 uint32_t contained_count;
932 msize = ntohs (msg->header.size);
933 contained_count = ntohl (msg->element_count_contained);
934 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
935 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
936 (0 == contained_count) ||
937 (UINT16_MAX < contained_count) ||
938 (s->total == s->client_received_element_count) ||
939 (s->total < s->client_received_element_count + contained_count) )
942 return GNUNET_SYSERR;
949 * We're receiving additional set data. Add it to our
950 * set and if we are done, initiate the transaction.
952 * @param cls identification of the client
953 * @param msg the actual message
956 handle_bob_client_message_multipart (void *cls,
957 const struct ComputationBobCryptodataMultipartMessage *msg)
959 struct BobServiceSession *s = cls;
960 uint32_t contained_count;
961 const struct GNUNET_SCALARPRODUCT_Element *elements;
962 struct GNUNET_SET_Element set_elem;
963 struct GNUNET_SCALARPRODUCT_Element *elem;
965 contained_count = ntohl (msg->element_count_contained);
966 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
967 for (uint32_t i = 0; i < contained_count; i++)
969 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
972 sizeof (struct GNUNET_SCALARPRODUCT_Element));
974 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
977 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
983 set_elem.data = &elem->key;
984 set_elem.size = sizeof (elem->key);
985 set_elem.element_type = 0;
986 GNUNET_SET_add_element (s->intersection_set,
990 s->client_received_element_count += contained_count;
991 GNUNET_SERVICE_client_continue (s->client);
992 if (s->total != s->client_received_element_count)
997 if (NULL == s->cadet)
999 /* no Alice waiting for this request, wait for Alice */
1002 start_intersection (s);
1007 * Handler for Bob's a client request message. Check @a msg is
1010 * @param cls identification of the client
1011 * @param msg the actual message
1012 * @return #GNUNET_OK if @a msg is well-formed
1015 check_bob_client_message (void *cls,
1016 const struct BobComputationMessage *msg)
1018 struct BobServiceSession *s = cls;
1019 uint32_t contained_count;
1020 uint32_t total_count;
1023 if (GNUNET_SCALARPRODUCT_STATUS_INIT != s->status)
1026 return GNUNET_SYSERR;
1028 msize = ntohs (msg->header.size);
1029 total_count = ntohl (msg->element_count_total);
1030 contained_count = ntohl (msg->element_count_contained);
1031 if ( (0 == total_count) ||
1032 (0 == contained_count) ||
1033 (UINT16_MAX < contained_count) ||
1034 (msize != (sizeof (struct BobComputationMessage) +
1035 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1037 GNUNET_break_op (0);
1038 return GNUNET_SYSERR;
1040 if (NULL != find_matching_client_session (&msg->session_key))
1043 return GNUNET_SYSERR;
1050 * Handler for Bob's a client request message. Bob is in the response
1051 * role, keep the values + session and waiting for a matching session
1052 * or process a waiting request from Alice.
1054 * @param cls identification of the client
1055 * @param msg the actual message
1058 handle_bob_client_message (void *cls,
1059 const struct BobComputationMessage *msg)
1061 struct BobServiceSession *s = cls;
1062 struct CadetIncomingSession *in;
1063 uint32_t contained_count;
1064 uint32_t total_count;
1065 const struct GNUNET_SCALARPRODUCT_Element *elements;
1066 struct GNUNET_SET_Element set_elem;
1067 struct GNUNET_SCALARPRODUCT_Element *elem;
1069 total_count = ntohl (msg->element_count_total);
1070 contained_count = ntohl (msg->element_count_contained);
1072 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1073 s->total = total_count;
1074 s->client_received_element_count = contained_count;
1075 s->session_id = msg->session_key;
1076 s->port = GNUNET_CADET_open_port (my_cadet,
1078 &cb_channel_incoming,
1080 if (NULL == s->port)
1083 GNUNET_SERVICE_client_drop (s->client);
1086 GNUNET_break (GNUNET_YES ==
1087 GNUNET_CONTAINER_multihashmap_put (client_sessions,
1090 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1091 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1092 s->intersected_elements
1093 = GNUNET_CONTAINER_multihashmap_create (s->total,
1096 = GNUNET_SET_create (cfg,
1097 GNUNET_SET_OPERATION_INTERSECTION);
1098 for (uint32_t i = 0; i < contained_count; i++)
1100 if (0 == GNUNET_ntohll (elements[i].value))
1102 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1103 GNUNET_memcpy (elem,
1105 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1106 if (GNUNET_SYSERR ==
1107 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1110 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1116 set_elem.data = &elem->key;
1117 set_elem.size = sizeof (elem->key);
1118 set_elem.element_type = 0;
1119 GNUNET_SET_add_element (s->intersection_set,
1122 s->used_element_count++;
1124 GNUNET_SERVICE_client_continue (s->client);
1125 if (s->total != s->client_received_element_count)
1130 in = find_matching_cadet_session (&s->session_id);
1133 /* nothing yet, wait for Alice */
1136 GNUNET_assert (NULL == in->s);
1140 start_intersection (s);
1145 * Task run during shutdown.
1150 shutdown_task (void *cls)
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Shutting down, initiating cleanup.\n");
1154 // FIXME: we have to cut our connections to CADET first!
1155 if (NULL != my_cadet)
1157 GNUNET_CADET_disconnect (my_cadet);
1162 GNUNET_CRYPTO_ecc_dlog_release (edc);
1165 GNUNET_CONTAINER_multihashmap_destroy (client_sessions);
1166 client_sessions = NULL;
1167 GNUNET_CONTAINER_multihashmap_destroy (cadet_sessions);
1168 cadet_sessions = NULL;
1173 * A client connected.
1175 * Setup the associated data structure.
1177 * @param cls closure, NULL
1178 * @param client identification of the client
1179 * @param mq message queue to communicate with @a client
1180 * @return our `struct BobServiceSession`
1183 client_connect_cb (void *cls,
1184 struct GNUNET_SERVICE_Client *client,
1185 struct GNUNET_MQ_Handle *mq)
1187 struct BobServiceSession *s;
1189 s = GNUNET_new (struct BobServiceSession);
1197 * A client disconnected.
1199 * Remove the associated session(s), release data structures
1200 * and cancel pending outgoing transmissions to the client.
1202 * @param cls closure, NULL
1203 * @param client identification of the client
1204 * @param app_cls our `struct BobServiceSession`
1207 client_disconnect_cb (void *cls,
1208 struct GNUNET_SERVICE_Client *client,
1211 struct BobServiceSession *s = app_cls;
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "Client disconnected from us.\n");
1216 destroy_service_session (s);
1221 * Initialization of the program and message handlers
1223 * @param cls closure
1224 * @param c configuration to use
1225 * @param service the initialized service
1229 const struct GNUNET_CONFIGURATION_Handle *c,
1230 struct GNUNET_SERVICE_Handle *service)
1232 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1233 { &handle_alices_computation_request,
1234 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION,
1235 sizeof (struct EccServiceRequestMessage) },
1236 { &handle_alices_cryptodata_message,
1237 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA,
1243 /* We don't really do DLOG, so we can setup with very minimal resources */
1244 edc = GNUNET_CRYPTO_ecc_dlog_prepare (4 /* max value */,
1246 client_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1248 cadet_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1250 my_cadet = GNUNET_CADET_connect (cfg,
1252 &cb_channel_destruction,
1254 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1256 if (NULL == my_cadet)
1258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1259 _("Connect to CADET failed\n"));
1260 GNUNET_SCHEDULER_shutdown ();
1267 * Define "main" method using service macro.
1270 ("scalarproduct-bob",
1271 GNUNET_SERVICE_OPTION_NONE,
1274 &client_disconnect_cb,
1276 GNUNET_MQ_hd_var_size (bob_client_message,
1277 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1278 struct BobComputationMessage,
1280 GNUNET_MQ_hd_var_size (bob_client_message_multipart,
1281 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB,
1282 struct ComputationBobCryptodataMultipartMessage,
1284 GNUNET_MQ_handler_end ());
1287 /* end of gnunet-service-scalarproduct-ecc_bob.c */