2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file scalarproduct/gnunet-service-scalarproduct.c
23 * @brief scalarproduct service implementation
24 * @author Christian M. Fuchs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_mesh_service.h"
31 #include "gnunet_applications.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_scalarproduct_service.h"
34 #include "gnunet_scalarproduct.h"
35 #include "scalarproduct.h"
37 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct", __VA_ARGS__)
40 * Log an error message at log-level 'level' that indicates
41 * a failure of the command 'cmd' with the message given
42 * by gcry_strerror(rc).
44 #define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0)
46 ///////////////////////////////////////////////////////////////////////////////
47 // Service Structure Definitions
48 ///////////////////////////////////////////////////////////////////////////////
51 * state a session can be in
55 WAITING_FOR_BOBS_CONNECT,
56 MESSAGE_FROM_RESPONDING_CLIENT_RECEIVED,
57 WAITING_FOR_RESPONSE_FROM_SERVICE,
58 REQUEST_FROM_SERVICE_RECEIVED,
63 * role a peer in a session can assume
73 * A scalarproduct session which tracks:
75 * a request form the client to our final response.
77 * a request from a service to us(service).
82 * the role this peer has
87 * session information is kept in a DLL
89 struct ServiceSession *next;
92 * session information is kept in a DLL
94 struct ServiceSession *prev;
97 * (hopefully) unique transaction ID
99 struct GNUNET_HashCode key;
102 * state of the session
104 enum SessionState state;
107 * Alice or Bob's peerID
109 struct GNUNET_PeerIdentity peer;
112 * the client this request is related to
114 struct GNUNET_SERVER_Client * client;
117 * how many elements we were supplied with from the client
119 uint16_t element_count;
122 * how many elements actually are used after applying the mask
124 uint16_t used_element_count;
127 * how many bytes the mask is long.
128 * just for convenience so we don't have to re-re-re calculate it each time
130 uint16_t mask_length;
133 * all the vector elements we received
138 * mask of which elements to check
140 unsigned char * mask;
143 * Public key of the remote service, only used by bob
145 gcry_sexp_t remote_pubkey;
148 * E(ai)(Bob) or ai(Alice) after applying the mask
153 * The computed scalar
158 * My transmit handle for the current message to a alice/bob
160 struct GNUNET_MESH_TransmitHandle * service_transmit_handle;
163 * My transmit handle for the current message to the client
165 struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
168 * tunnel-handle associated with our mesh handle
170 struct GNUNET_MESH_Tunnel * tunnel;
175 * We need to do a minimum of bookkeeping to maintain track of our transmit handles.
176 * each msg is associated with a session and handle. using this information we can determine which msg was sent.
181 * The handle used to transmit with this request
183 void ** transmit_handle;
186 * The message to send
188 struct GNUNET_MessageHeader * msg;
191 ///////////////////////////////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////////////////////////////
197 * Handle to the core service (NULL until we've connected to it).
199 static struct GNUNET_MESH_Handle *my_mesh;
202 * The identity of this host.
204 static struct GNUNET_PeerIdentity me;
207 * Service's own public key represented as string
209 static unsigned char * my_pubkey_external;
212 * Service's own public key represented as string
214 static uint16_t my_pubkey_external_length = 0;
219 static gcry_mpi_t my_n;
222 * Service's own n^2 (kept for performance)
224 static gcry_mpi_t my_nsquare;
227 * Service's own public exponent
229 static gcry_mpi_t my_g;
232 * Service's own private multiplier
234 static gcry_mpi_t my_mu;
237 * Service's own private exponent
239 static gcry_mpi_t my_lambda;
242 * Service's offset for values that could possibly be negative but are plaintext for encryption.
244 static gcry_mpi_t my_offset;
247 * Head of our double linked list for client-requests sent to us.
248 * for all of these elements we calculate a scalar product with a remote peer
249 * split between service->service and client->service for simplicity
251 static struct ServiceSession * from_client_head;
253 * Tail of our double linked list for client-requests sent to us.
254 * for all of these elements we calculate a scalar product with a remote peer
255 * split between service->service and client->service for simplicity
257 static struct ServiceSession * from_client_tail;
260 * Head of our double linked list for service-requests sent to us.
261 * for all of these elements we help the requesting service in calculating a scalar product
262 * split between service->service and client->service for simplicity
264 static struct ServiceSession * from_service_head;
267 * Tail of our double linked list for service-requests sent to us.
268 * for all of these elements we help the requesting service in calculating a scalar product
269 * split between service->service and client->service for simplicity
271 static struct ServiceSession * from_service_tail;
274 * Certain events (callbacks for server & mesh operations) must not be queued after shutdown.
276 static int do_shutdown;
278 ///////////////////////////////////////////////////////////////////////////////
280 ///////////////////////////////////////////////////////////////////////////////
283 * Generates an Paillier private/public keyset and extracts the values using libgrcypt only
288 gcry_sexp_t gen_parms;
290 gcry_sexp_t tmp_sexp;
299 // we can still use the RSA keygen for generating p,q,n, but using e is pointless.
300 GNUNET_assert (0 == gcry_sexp_build (&gen_parms, &erroff,
301 "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))",
304 GNUNET_assert (0 == gcry_pk_genkey (&key, gen_parms));
305 gcry_sexp_release (gen_parms);
307 // get n and d of our publickey as MPI
308 tmp_sexp = gcry_sexp_find_token (key, "n", 0);
309 GNUNET_assert (tmp_sexp);
310 my_n = gcry_sexp_nth_mpi (tmp_sexp, 1, GCRYMPI_FMT_USG);
311 gcry_sexp_release (tmp_sexp);
312 tmp_sexp = gcry_sexp_find_token (key, "p", 0);
313 GNUNET_assert (tmp_sexp);
314 p = gcry_sexp_nth_mpi (tmp_sexp, 1, GCRYMPI_FMT_USG);
315 gcry_sexp_release (tmp_sexp);
316 tmp_sexp = gcry_sexp_find_token (key, "q", 0);
317 GNUNET_assert (tmp_sexp);
318 q = gcry_sexp_nth_mpi (tmp_sexp, 1, GCRYMPI_FMT_USG);
319 gcry_sexp_release (key);
321 tmp1 = gcry_mpi_new (0);
322 tmp2 = gcry_mpi_new (0);
323 gcd = gcry_mpi_new (0);
324 my_g = gcry_mpi_new (0);
325 my_mu = gcry_mpi_new (0);
326 my_nsquare = gcry_mpi_new (0);
327 my_lambda = gcry_mpi_new (0);
330 // lambda = \frac{(p-1)*(q-1)}{gcd(p-1,q-1)}
331 gcry_mpi_sub_ui (tmp1, p, 1);
332 gcry_mpi_sub_ui (tmp2, q, 1);
333 gcry_mpi_gcd (gcd, tmp1, tmp2);
334 gcry_mpi_set (my_lambda, tmp1);
335 gcry_mpi_mul (my_lambda, my_lambda, tmp2);
336 gcry_mpi_div (my_lambda, NULL, my_lambda, gcd, 0);
339 gcry_mpi_mul (my_nsquare, my_n, my_n);
345 gcry_mpi_randomize (my_g, KEYBITS * 2, GCRY_WEAK_RANDOM);
346 // g must be smaller than n^2
347 if (0 >= gcry_mpi_cmp (my_g, my_nsquare))
350 // g must have gcd == 1 with n^2
351 gcry_mpi_gcd (gcd, my_g, my_nsquare);
353 while (gcry_mpi_cmp_ui (gcd, 1));
355 // is this a valid g?
356 // if so, gcd(((g^lambda mod n^2)-1 )/n, n) = 1
357 gcry_mpi_powm (tmp1, my_g, my_lambda, my_nsquare);
358 gcry_mpi_sub_ui (tmp1, tmp1, 1);
359 gcry_mpi_div (tmp1, NULL, tmp1, my_n, 0);
360 gcry_mpi_gcd (gcd, tmp1, my_n);
362 while (gcry_mpi_cmp_ui (gcd, 1));
364 // calculate our mu based on g and n.
365 // mu = (((g^lambda mod n^2)-1 )/n)^-1 mod n
366 gcry_mpi_invm (my_mu, tmp1, my_n);
368 GNUNET_assert (0 == gcry_sexp_build (&key, &erroff,
369 "(public-key (paillier (n %M)(g %M)))",
372 // get the length of this sexpression
373 my_pubkey_external_length = gcry_sexp_sprint (key,
378 GNUNET_assert (my_pubkey_external_length > 0);
379 my_pubkey_external = GNUNET_malloc (my_pubkey_external_length);
381 // convert the sexpression to canonical format
382 gcry_sexp_sprint (key,
385 my_pubkey_external_length);
387 gcry_sexp_release (key);
389 // offset has to be sufficiently small to allow computation of:
390 // m1+m2 mod n == (S + a) + (S + b) mod n,
391 // if we have more complex operations, this factor needs to be lowered
392 my_offset = gcry_mpi_new(KEYBITS/3);
393 gcry_mpi_set_bit(my_offset, KEYBITS/3);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Generated key set with key length %d bits.\n"), KEYBITS);
400 * If target != size, move target bytes to the
401 * end of the size-sized buffer and zero out the
402 * first target-size bytes.
404 * @param buf original buffer
405 * @param size number of bytes in the buffer
406 * @param target target size of the buffer
409 adjust (unsigned char *buf, size_t size, size_t target)
413 memmove (&buf[target - size], buf, size);
414 memset (buf, 0, target - size);
420 * encrypts an element using the paillier crypto system
422 * @param c ciphertext (output)
424 * @param g the public base
425 * @param n the module from which which r is chosen (Z*_n)
426 * @param n_square the module for encryption, for performance reasons.
429 encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mpi_t n_square)
433 GNUNET_assert (tmp = gcry_mpi_new (0));
435 while (0 >= gcry_mpi_cmp_ui (tmp, 1))
437 gcry_mpi_randomize (tmp, KEYBITS / 3, GCRY_WEAK_RANDOM);
438 // r must be 1 < r < n
441 gcry_mpi_powm (c, g, m, n_square);
442 gcry_mpi_powm (tmp, tmp, n, n_square);
443 gcry_mpi_mulm (c, tmp, c, n_square);
445 gcry_mpi_release (tmp);
449 * decrypts an element using the paillier crypto system
451 * @param m plaintext (output)
452 * @param c the ciphertext
453 * @param mu the modifier to correct encryption
454 * @param lambda the private exponent
455 * @param n the outer module for decryption
456 * @param n_square the inner module for decryption
459 decrypt_element (gcry_mpi_t m, gcry_mpi_t c, gcry_mpi_t mu, gcry_mpi_t lambda, gcry_mpi_t n, gcry_mpi_t n_square)
461 gcry_mpi_powm (m, c, lambda, n_square);
462 gcry_mpi_sub_ui (m, m, 1);
463 gcry_mpi_div (m, NULL, m, n, 0);
464 gcry_mpi_mulm (m, m, mu, n);
469 * computes the square sum over a vector of a given length.
471 * @param vector the vector to encrypt
472 * @param length the length of the vector
473 * @return an MPI value containing the calculated sum, never NULL
476 compute_square_sum (gcry_mpi_t * vector, uint16_t length)
482 GNUNET_assert (sum = gcry_mpi_new (0));
483 GNUNET_assert (elem = gcry_mpi_new (0));
485 // calculare E(sum (ai ^ 2), publickey)
486 for (i = 0; i < length; i++)
488 gcry_mpi_mul (elem, vector[i], vector[i]);
489 gcry_mpi_add (sum, sum, elem);
491 gcry_mpi_release (elem);
498 * Primitive callback for copying over a message, as they
499 * usually are too complex to be handled in the callback itself.
500 * clears a session-callback, if a session was handed over and the transmit handle was stored
502 * @param cls the message object
503 * @param size the size of the buffer we got
504 * @param buf the buffer to copy the message to
505 * @return 0 if we couldn't copy, else the size copied over
508 do_send_message (void *cls, size_t size, void *buf)
510 struct MessageObject * info = cls;
511 struct GNUNET_MessageHeader * msg;
514 GNUNET_assert (info);
519 if (ntohs (msg->size) == size)
521 memcpy (buf, msg, size);
525 // reset the transmit handle, if necessary
526 if (info->transmit_handle)
527 *info->transmit_handle = NULL;
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "Sent a message of type %hu.\n",
539 * initializes a new vector with fresh MPI values (=0) of a given length
541 * @param length of the vector to create
542 * @return the initialized vector, never NULL
545 initialize_mpi_vector (uint16_t length)
548 gcry_mpi_t * output = GNUNET_malloc (sizeof (gcry_mpi_t) * length);
550 for (i = 0; i < length; i++)
551 GNUNET_assert (NULL != (output[i] = gcry_mpi_new (0)));
557 * permutes an MPI vector according to the given permutation vector
559 * @param vector the vector to permuted
560 * @param perm the permutation to use
561 * @param length the length of the vectors
562 * @return the permuted vector (same as input), never NULL
565 permute_vector (gcry_mpi_t * vector,
569 gcry_mpi_t tmp[length];
572 GNUNET_assert (length > 0);
575 memcpy (tmp, vector, length * sizeof (gcry_mpi_t));
577 // permute vector according to given
578 for (i = 0; i < length; i++)
579 vector[i] = tmp[perm[i]];
586 * Populate a vector with random integer values and convert them to
588 * @param length the length of the vector we must generate
589 * @return an array of MPI values with random values
592 generate_random_vector (uint16_t length)
594 gcry_mpi_t * random_vector;
598 random_vector = initialize_mpi_vector (length);
599 for (i = 0; i < length; i++)
601 value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
603 // long to gcry_mpi_t
605 gcry_mpi_sub_ui (random_vector[i],
609 random_vector[i] = gcry_mpi_set_ui (random_vector[i], value);
612 return random_vector;
617 * Finds a not terminated client/service session in the
618 * given DLL based on session key, element count and state.
620 * @param tail - the tail of the DLL
621 * @param my - the session to compare it to
622 * @return a pointer to a matching session,
625 static struct ServiceSession *
626 find_matching_session (struct ServiceSession * tail,
627 const struct GNUNET_HashCode * key,
628 uint16_t element_count,
629 enum SessionState * state,
630 const struct GNUNET_PeerIdentity * peerid)
632 struct ServiceSession * curr;
634 for (curr = tail; NULL != curr; curr = curr->prev)
636 // if the key matches, and the element_count is same
637 if ((!memcmp (&curr->key, key, sizeof (struct GNUNET_HashCode)))
638 && (curr->element_count == element_count))
640 // if incoming state is NULL OR is same as state of the queued request
641 if ((NULL == state) || (curr->state == *state))
643 // if peerid is NULL OR same as the peer Id in the queued request
645 || (!memcmp (&curr->peer, peerid, sizeof (struct GNUNET_PeerIdentity))))
646 // matches and is not an already terminated session
657 destroy_tunnel (void *cls,
658 const struct GNUNET_SCHEDULER_TaskContext *tc)
660 struct ServiceSession * session = cls;
664 GNUNET_MESH_tunnel_destroy (session->tunnel);
665 session->tunnel = NULL;
667 session->service_transmit_handle = NULL;
668 // we need to set this to NULL so there is no problem with double-cancel later on.
673 free_session (struct ServiceSession * session)
677 if (FINALIZED != session->state)
681 for (i = 0; i < session->used_element_count; i++)
682 gcry_mpi_release (session->a[i]);
684 GNUNET_free (session->a);
686 if (session->product)
687 gcry_mpi_release (session->product);
689 if (session->remote_pubkey)
690 gcry_sexp_release (session->remote_pubkey);
692 GNUNET_free_non_null (session->vector);
695 GNUNET_free (session);
697 ///////////////////////////////////////////////////////////////////////////////
698 // Event and Message Handlers
699 ///////////////////////////////////////////////////////////////////////////////
703 * A client disconnected.
705 * Remove the associated session(s), release datastructures
706 * and cancel pending outgoing transmissions to the client.
707 * if the session has not yet completed, we also cancel Alice's request to Bob.
709 * @param cls closure, NULL
710 * @param client identification of the client
713 handle_client_disconnect (void *cls,
714 struct GNUNET_SERVER_Client
717 struct ServiceSession * elem;
718 struct ServiceSession * next;
720 // start from the tail, old stuff will be there...
721 for (elem = from_client_head; NULL != elem; elem = next)
724 if (elem->client != client)
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Client (%p) disconnected from us.\n"), client);
728 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, elem);
730 if (!(elem->role == BOB && elem->state == FINALIZED))
732 //we MUST terminate any client message underway
733 if (elem->service_transmit_handle && elem->tunnel)
734 GNUNET_MESH_notify_transmit_ready_cancel (elem->service_transmit_handle);
735 if (elem->tunnel && elem->state == WAITING_FOR_RESPONSE_FROM_SERVICE)
736 destroy_tunnel (elem, NULL);
744 * Notify the client that the session has succeeded or failed completely.
745 * This message gets sent to
746 * * alice's client if bob disconnected or to
747 * * bob's client if the operation completed or alice disconnected
749 * @param client_session the associated client session
750 * @return GNUNET_NO, if we could not notify the client
751 * GNUNET_YES if we notified it.
754 prepare_client_end_notification (void * cls,
755 const struct GNUNET_SCHEDULER_TaskContext * tc)
757 struct ServiceSession * session = cls;
758 struct GNUNET_SCALARPRODUCT_client_response * msg;
759 struct MessageObject * msg_obj;
761 msg = GNUNET_new (struct GNUNET_SCALARPRODUCT_client_response);
762 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
763 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
764 memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
765 msg->header.size = htons (sizeof (struct GNUNET_SCALARPRODUCT_client_response));
766 // 0 size and the first char in the product is 0, which should never be zero if encoding is used.
767 msg->product_length = htonl (0);
769 msg_obj = GNUNET_new (struct MessageObject);
770 msg_obj->msg = &msg->header;
771 msg_obj->transmit_handle = NULL; // do not reset the transmit handle, please
773 //transmit this message to our client
774 session->client_transmit_handle =
775 GNUNET_SERVER_notify_transmit_ready (session->client,
776 sizeof (struct GNUNET_SCALARPRODUCT_client_response),
777 GNUNET_TIME_UNIT_FOREVER_REL,
782 // if we could not even queue our request, something is wrong
783 if ( ! session->client_transmit_handle)
786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)! This is OK if it was disconnected beforehand already.\n"), session->client);
787 // usually gets freed by do_send_message
788 GNUNET_free (msg_obj);
792 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Sending session-end notification to client (%p) for session %s\n"), &session->client, GNUNET_h2s (&session->key));
794 free_session(session);
800 * generates the response message to be sent to alice after computing
801 * the values (1), (2), S and S'
802 * (1)[]: $E_A(a_{pi(i)}) times E_A(- r_{pi(i)} - b_{pi(i)}) &= E_A(a_{pi(i)} - r_{pi(i)} - b_{pi(i)})$
803 * (2)[]: $E_A(a_{pi'(i)}) times E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
804 * S: $S := E_A(sum (r_i + b_i)^2)$
805 * S': $S' := E_A(sum r_i^2)$
807 * @param r (1)[]: $E_A(a_{pi(i)}) times E_A(- r_{pi(i)} - b_{pi(i)}) &= E_A(a_{pi(i)} - r_{pi(i)} - b_{pi(i)})$
808 * @param r_prime (2)[]: $E_A(a_{pi'(i)}) times E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
809 * @param s S: $S := E_A(sum (r_i + b_i)^2)$
810 * @param s_prime S': $S' := E_A(sum r_i^2)$
811 * @param request the associated requesting session with alice
812 * @param response the associated responder session with bob's client
813 * @return GNUNET_SYSERR if the function was called with NULL parameters or if there was an error
814 * GNUNET_NO if we could not send our message
815 * GNUNET_OK if the operation succeeded
818 prepare_service_response (gcry_mpi_t * r,
819 gcry_mpi_t * r_prime,
822 struct ServiceSession * request,
823 struct ServiceSession * response)
825 struct GNUNET_SCALARPRODUCT_service_response * msg;
826 uint16_t msg_length = 0;
827 unsigned char * current = NULL;
828 unsigned char * element_exported = NULL;
829 size_t element_length = 0;
832 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
833 + 2 * request->used_element_count * PAILLIER_ELEMENT_LENGTH // kp, kq
834 + 2 * PAILLIER_ELEMENT_LENGTH; // s, stick
836 msg = GNUNET_malloc (msg_length);
838 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE);
839 msg->header.size = htons (msg_length);
840 msg->element_count = htons (request->element_count);
841 msg->used_element_count = htons (request->used_element_count);
842 memcpy (&msg->key, &request->key, sizeof (struct GNUNET_HashCode));
843 current = (unsigned char *) &msg[1];
845 // 4 times the same logics with slight variations.
846 // doesn't really justify having 2 functions for that
847 // so i put it into blocks to enhance readability
850 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
851 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
852 element_exported, PAILLIER_ELEMENT_LENGTH,
855 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
856 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
857 GNUNET_free (element_exported);
858 current += PAILLIER_ELEMENT_LENGTH;
863 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
864 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
865 element_exported, PAILLIER_ELEMENT_LENGTH,
868 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
869 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
870 GNUNET_free (element_exported);
871 current += PAILLIER_ELEMENT_LENGTH;
875 for (i = 0; i < request->used_element_count; i++)
877 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
878 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
879 element_exported, PAILLIER_ELEMENT_LENGTH,
882 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
883 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
884 GNUNET_free (element_exported);
885 current += PAILLIER_ELEMENT_LENGTH;
890 for (i = 0; i < request->used_element_count; i++)
892 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
893 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
894 element_exported, PAILLIER_ELEMENT_LENGTH,
897 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
898 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
899 GNUNET_free (element_exported);
900 current += PAILLIER_ELEMENT_LENGTH;
903 if (GNUNET_SERVER_MAX_MESSAGE_SIZE >= msg_length)
905 struct MessageObject * msg_obj;
907 msg_obj = GNUNET_new (struct MessageObject);
908 msg_obj->msg = (struct GNUNET_MessageHeader *) msg;
909 msg_obj->transmit_handle = (void *) &request->service_transmit_handle; //and reset the transmit handle
910 request->service_transmit_handle =
911 GNUNET_MESH_notify_transmit_ready (request->tunnel,
913 GNUNET_TIME_UNIT_FOREVER_REL,
914 &request->peer, //must be specified, we are a slave/participant/non-owner
918 // we don't care if it could be send or not. either way, the session is over for us.
919 request->state = FINALIZED;
920 response->state = FINALIZED;
924 // TODO FEATURE: fallback to fragmentation, in case the message is too long
925 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!)\n"));
928 //disconnect our client
929 if ( ! request->service_transmit_handle)
931 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
932 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, response);
933 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
934 &prepare_client_end_notification,
945 * (1)[]: $E_A(a_{\pi(i)}) \otimes E_A(- r_{\pi(i)} - b_{\pi(i)}) &= E_A(a_{\pi(i)} - r_{\pi(i)} - b_{\pi(i)})$
946 * (2)[]: $E_A(a_{\pi'(i)}) \otimes E_A(- r_{\pi'(i)}) &= E_A(a_{\pi'(i)} - r_{\pi'(i)})$
947 * S: $S := E_A(\sum (r_i + b_i)^2)$
948 * S': $S' := E_A(\sum r_i^2)$
950 * @param request the requesting session + bob's requesting peer
951 * @param response the responding session + bob's client handle
952 * @return GNUNET_SYSERR if the computation failed
953 * GNUNET_OK if everything went well.
956 compute_service_response (struct ServiceSession * request,
957 struct ServiceSession * response)
961 int ret = GNUNET_SYSERR;
965 gcry_mpi_t * rand = NULL;
966 gcry_mpi_t * r = NULL;
967 gcry_mpi_t * r_prime = NULL;
970 gcry_mpi_t * a_pi_prime;
972 gcry_mpi_t * rand_pi;
973 gcry_mpi_t * rand_pi_prime;
975 gcry_mpi_t s_prime = NULL;
976 gcry_mpi_t remote_n = NULL;
977 gcry_mpi_t remote_nsquare;
978 gcry_mpi_t remote_g = NULL;
982 count = request->used_element_count;
984 b = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
985 a_pi = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
986 b_pi = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
987 a_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
988 rand_pi = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
989 rand_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
991 // convert responder session to from long to mpi
992 for (i = 0, j = 0; i < response->element_count && j < count; i++)
994 if (request->mask[i / 8] & (1 << (i % 8)))
996 value = response->vector[i] >= 0 ? response->vector[i] : -response->vector[i];
997 // long to gcry_mpi_t
998 if (0 > response->vector[i])
1000 b[j] = gcry_mpi_new (0);
1001 gcry_mpi_sub_ui (b[j], b[j], value);
1005 b[j] = gcry_mpi_set_ui (NULL, value);
1010 GNUNET_free (response->vector);
1011 response->vector = NULL;
1013 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "n", 0);
1016 GNUNET_break_op (0);
1017 gcry_sexp_release (request->remote_pubkey);
1018 request->remote_pubkey = NULL;
1021 remote_n = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
1025 gcry_sexp_release (tmp_exp);
1028 remote_nsquare = gcry_mpi_new (KEYBITS + 1);
1029 gcry_mpi_mul (remote_nsquare, remote_n, remote_n);
1030 gcry_sexp_release (tmp_exp);
1031 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "g", 0);
1032 gcry_sexp_release (request->remote_pubkey);
1033 request->remote_pubkey = NULL;
1036 GNUNET_break_op (0);
1037 gcry_mpi_release (remote_n);
1040 remote_g = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
1044 gcry_mpi_release (remote_n);
1045 gcry_sexp_release (tmp_exp);
1048 gcry_sexp_release (tmp_exp);
1050 // generate r, p and q
1051 rand = generate_random_vector (count);
1052 p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
1053 q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
1054 //initialize the result vectors
1055 r = initialize_mpi_vector (count);
1056 r_prime = initialize_mpi_vector (count);
1058 // copy the REFERNCES of a, b and r into aq and bq. we will not change
1059 // those values, thus we can work with the references
1060 memcpy (a_pi, request->a, sizeof (gcry_mpi_t) * count);
1061 memcpy (a_pi_prime, request->a, sizeof (gcry_mpi_t) * count);
1062 memcpy (b_pi, b, sizeof (gcry_mpi_t) * count);
1063 memcpy (rand_pi, rand, sizeof (gcry_mpi_t) * count);
1064 memcpy (rand_pi_prime, rand, sizeof (gcry_mpi_t) * count);
1066 // generate p and q permutations for a, b and r
1067 GNUNET_assert (permute_vector (a_pi, p, count));
1068 GNUNET_assert (permute_vector (b_pi, p, count));
1069 GNUNET_assert (permute_vector (rand_pi, p, count));
1070 GNUNET_assert (permute_vector (a_pi_prime, q, count));
1071 GNUNET_assert (permute_vector (rand_pi_prime, q, count));
1073 // encrypt the element
1074 // for the sake of readability I decided to have dedicated permutation
1075 // vectors, which get rid of all the lookups in p/q.
1076 // however, ap/aq are not absolutely necessary but are just abstraction
1077 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
1078 for (i = 0; i < count; i++)
1080 // E(S - r_pi - b_pi)
1081 gcry_mpi_sub (r[i], my_offset, rand_pi[i]);
1082 gcry_mpi_sub (r[i], r[i], b_pi[i]);
1083 encrypt_element (r[i], r[i], remote_g, remote_n, remote_nsquare);
1085 // E(S - r_pi - b_pi) * E(S + a_pi) == E(2*S + a - r - b)
1086 gcry_mpi_mulm (r[i], r[i], a_pi[i], remote_nsquare);
1090 GNUNET_free (rand_pi);
1092 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
1093 for (i = 0; i < count; i++)
1096 gcry_mpi_sub (r_prime[i], my_offset, rand_pi_prime[i]);
1097 encrypt_element (r_prime[i], r_prime[i], remote_g, remote_n, remote_nsquare);
1099 // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
1100 gcry_mpi_mulm (r_prime[i], r_prime[i], a_pi_prime[i], remote_nsquare);
1102 GNUNET_free (a_pi_prime);
1103 GNUNET_free (rand_pi_prime);
1105 // Calculate S' = E(SUM( r_i^2 ))
1106 s_prime = compute_square_sum (rand, count);
1107 encrypt_element (s_prime, s_prime, remote_g, remote_n, remote_nsquare);
1109 // Calculate S = E(SUM( (r_i + b_i)^2 ))
1110 for (i = 0; i < count; i++)
1112 gcry_mpi_add (rand[i], rand[i], b[i]);
1114 s = compute_square_sum (rand, count);
1115 encrypt_element (s, s, remote_g, remote_n, remote_nsquare);
1116 gcry_mpi_release (remote_n);
1117 gcry_mpi_release (remote_g);
1118 gcry_mpi_release (remote_nsquare);
1120 // release r and tmp
1121 for (i = 0; i < count; i++)
1122 // rp, rq, aq, ap, bp, bq are released along with a, r, b respectively, (a and b are handled at except:)
1123 gcry_mpi_release (rand[i]);
1125 // copy the Kp[], Kq[], S and Stick into a new message
1126 if (GNUNET_YES != prepare_service_response (r, r_prime, s, s_prime, request, response))
1127 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to communicate with `%s', scalar product calculation aborted.\n"),
1128 GNUNET_i2s (&request->peer));
1132 for (i = 0; i < count; i++)
1134 gcry_mpi_release (r_prime[i]);
1135 gcry_mpi_release (r[i]);
1138 gcry_mpi_release (s);
1139 gcry_mpi_release (s_prime);
1142 for (i = 0; i < count; i++)
1144 gcry_mpi_release (b[i]);
1145 gcry_mpi_release (request->a[i]);
1149 GNUNET_free (request->a);
1157 * Executed by Alice, fills in a service-request message and sends it to the given peer
1159 * @param session the session associated with this request, then also holds the CORE-handle
1160 * @return #GNUNET_SYSERR if we could not send the message
1161 * #GNUNET_NO if the message was too large
1162 * #GNUNET_OK if we sent it
1165 prepare_service_request (void *cls,
1166 const struct GNUNET_PeerIdentity * peer,
1167 const struct GNUNET_ATS_Information * atsi)
1169 struct ServiceSession * session = cls;
1170 unsigned char * current;
1171 struct GNUNET_SCALARPRODUCT_service_request * msg;
1172 struct MessageObject * msg_obj;
1175 uint16_t msg_length;
1176 size_t element_length = 0; //gets initialized by gcry_mpi_print, but the compiler doesn't know that
1180 GNUNET_assert (NULL != cls);
1181 GNUNET_assert (NULL != peer);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to peer (%s)!\n"), GNUNET_i2s (peer));
1184 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1185 + session->used_element_count * PAILLIER_ELEMENT_LENGTH
1186 + session->mask_length
1187 + my_pubkey_external_length;
1189 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1190 + session->used_element_count * PAILLIER_ELEMENT_LENGTH
1191 + session->mask_length
1192 + my_pubkey_external_length)
1194 // TODO FEATURE: fallback to fragmentation, in case the message is too long
1195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!\n"));
1196 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
1197 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1198 &prepare_client_end_notification,
1202 msg = GNUNET_malloc (msg_length);
1204 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB);
1205 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
1206 msg->mask_length = htons (session->mask_length);
1207 msg->pk_length = htons (my_pubkey_external_length);
1208 msg->used_element_count = htons (session->used_element_count);
1209 msg->element_count = htons (session->element_count);
1210 msg->header.size = htons (msg_length);
1212 // fill in the payload
1213 current = (unsigned char *) &msg[1];
1214 // copy over the mask
1215 memcpy (current, session->mask, session->mask_length);
1216 // copy over our public key
1217 current += session->mask_length;
1218 memcpy (current, my_pubkey_external, my_pubkey_external_length);
1219 current += my_pubkey_external_length;
1221 // now copy over the element vector
1222 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * session->used_element_count);
1223 a = gcry_mpi_new (KEYBITS * 2);
1224 // encrypt our vector and generate string representations
1225 for (i = 0, j = 0; i < session->element_count; i++)
1227 // if this is a used element...
1228 if (session->mask[i / 8] & 1 << (i % 8))
1230 unsigned char * element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
1231 value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i];
1233 a = gcry_mpi_set_ui (a, 0);
1234 // long to gcry_mpi_t
1235 if (session->vector[i] < 0)
1236 gcry_mpi_sub_ui (a, a, value);
1238 gcry_mpi_add_ui (a, a, value);
1240 session->a[j++] = gcry_mpi_set (NULL, a);
1241 gcry_mpi_add (a, a, my_offset);
1242 encrypt_element (a, a, my_g, my_n, my_nsquare);
1244 // get representation as string
1245 // we always supply some value, so gcry_mpi_print fails only if it can't reserve memory
1246 GNUNET_assert ( ! gcry_mpi_print (GCRYMPI_FMT_USG,
1247 element_exported, PAILLIER_ELEMENT_LENGTH,
1251 // move buffer content to the end of the buffer so it can easily be read by libgcrypt. also this now has fixed size
1252 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
1254 // copy over to the message
1255 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
1256 current += PAILLIER_ELEMENT_LENGTH;
1259 gcry_mpi_release (a);
1261 msg_obj = GNUNET_new (struct MessageObject);
1262 msg_obj->msg = (struct GNUNET_MessageHeader *) msg;
1263 msg_obj->transmit_handle = (void *) &session->service_transmit_handle; //and reset the transmit handle
1264 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transmitting service request.\n"));
1266 //transmit via mesh messaging
1267 session->state = WAITING_FOR_RESPONSE_FROM_SERVICE;
1268 session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->tunnel, GNUNET_YES,
1269 GNUNET_TIME_UNIT_FOREVER_REL,
1270 peer, //multicast to all targets, maybe useful in the future
1274 if ( ! session->service_transmit_handle)
1276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not send mutlicast message to tunnel!\n"));
1277 GNUNET_free (msg_obj);
1279 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
1280 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1281 &prepare_client_end_notification,
1288 * Method called whenever a peer has disconnected from the tunnel.
1289 * Implementations of this callback must NOT call
1290 * #GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
1291 * to run in some other task later. However, calling
1292 * #GNUNET_MESH_notify_transmit_ready_cancel is allowed.
1294 * @param cls closure
1295 * @param peer peer identity the tunnel stopped working with
1298 tunnel_peer_disconnect_handler (void *cls, const struct GNUNET_PeerIdentity * peer)
1300 // as we have only one peer connected in each session, just remove the session and say good bye
1301 struct ServiceSession * session = cls;
1302 struct ServiceSession * curr;
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Peer (%s) disconnected from our tunnel!\n",
1308 if ((session->role == ALICE) && (FINALIZED != session->state) && ( ! do_shutdown))
1310 for (curr = from_client_head; NULL != curr; curr = curr->next)
1311 if (curr == session)
1313 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
1316 // FIXME: dangling tasks, code duplication, use-after-free, fun...
1317 GNUNET_SCHEDULER_add_now (&destroy_tunnel,
1319 // if this happened before we received the answer, we must terminate the session
1320 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
1327 * Handler for a client request message.
1328 * Can either be type A or B
1329 * A: request-initiation to compute a scalar product with a peer
1330 * B: response role, keep the values + session and wait for a matching session or process a waiting request
1332 * @param cls closure
1333 * @param client identification of the client
1334 * @param message the actual message
1337 handle_client_request (void *cls,
1338 struct GNUNET_SERVER_Client *client,
1339 const struct GNUNET_MessageHeader *message)
1341 const struct GNUNET_SCALARPRODUCT_client_request * msg = (const struct GNUNET_SCALARPRODUCT_client_request *) message;
1342 struct ServiceSession * session;
1343 uint16_t element_count;
1344 uint16_t mask_length;
1349 GNUNET_assert (message);
1351 //we need at least a peer and one message id to compare
1352 if (sizeof (struct GNUNET_SCALARPRODUCT_client_request) > ntohs (msg->header.size))
1354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1355 _ ("Too short message received from client!\n"));
1356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1360 msg_type = ntohs (msg->header.type);
1361 element_count = ntohs (msg->element_count);
1362 mask_length = ntohs (msg->mask_length);
1364 //sanity check: is the message as long as the message_count fields suggests?
1365 if (( ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t) + mask_length))
1366 || (0 == element_count))
1368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1369 _ ("Invalid message received from client, session information incorrect!\n"));
1370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1374 // do we have a duplicate session here already?
1375 if (NULL != find_matching_session (from_client_tail,
1380 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Duplicate session information received, cannot create new session with key `%s'\n"), GNUNET_h2s (&msg->key));
1381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1385 session = GNUNET_new (struct ServiceSession);
1386 session->client = client;
1387 session->element_count = element_count;
1388 session->mask_length = mask_length;
1389 // get our transaction key
1390 memcpy (&session->key, &msg->key, sizeof (struct GNUNET_HashCode));
1391 //allocate memory for vector and encrypted vector
1392 session->vector = GNUNET_malloc (sizeof (int32_t) * element_count);
1393 vector = (int32_t *) & msg[1];
1395 if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type)
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Got client-request-session with key %s, preparing tunnel to remote service.\n"), GNUNET_h2s (&session->key));
1399 session->role = ALICE;
1401 session->mask = GNUNET_malloc (mask_length);
1402 memcpy (session->mask, &vector[element_count], mask_length);
1404 // copy over the elements
1405 session->used_element_count = 0;
1406 for (i = 0; i < element_count; i++)
1408 session->vector[i] = ntohl (vector[i]);
1409 if (session->vector[i] == 0)
1410 session->mask[i / 8] &= ~(1 << (i % 8));
1411 if (session->mask[i / 8] & (1 << (i % 8)))
1412 session->used_element_count++;
1415 if ( ! session->used_element_count)
1417 GNUNET_break_op (0);
1418 GNUNET_free (session->vector);
1419 GNUNET_free (session->a);
1420 GNUNET_free (session);
1421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1424 //session with ourself makes no sense!
1425 if ( ! memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
1428 GNUNET_free (session->vector);
1429 GNUNET_free (session->a);
1430 GNUNET_free (session);
1431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1435 memcpy (&session->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
1436 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Creating new tunnel to for session with key %s.\n"), GNUNET_h2s (&session->key));
1437 GNUNET_CONTAINER_DLL_insert (from_client_head, from_client_tail, session);
1438 session->tunnel = GNUNET_MESH_tunnel_create (my_mesh, session,
1439 prepare_service_request,
1440 tunnel_peer_disconnect_handler,
1442 if ( ! session->tunnel)
1445 GNUNET_free (session->vector);
1446 GNUNET_free (session->a);
1447 GNUNET_free (session);
1448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1451 GNUNET_MESH_peer_request_connect_add (session->tunnel, &session->peer);
1452 GNUNET_SERVER_receive_done (client, GNUNET_YES);
1453 session->state = WAITING_FOR_BOBS_CONNECT;
1457 struct ServiceSession * requesting_session;
1458 enum SessionState needed_state = REQUEST_FROM_SERVICE_RECEIVED;
1460 session->role = BOB;
1461 session->mask = NULL;
1462 // copy over the elements
1463 session->used_element_count = element_count;
1464 for (i = 0; i < element_count; i++)
1465 session->vector[i] = ntohl (vector[i]);
1466 session->state = MESSAGE_FROM_RESPONDING_CLIENT_RECEIVED;
1468 GNUNET_CONTAINER_DLL_insert (from_client_head, from_client_tail, session);
1469 GNUNET_SERVER_receive_done (client, GNUNET_YES);
1470 //check if service queue contains a matching request
1471 requesting_session = find_matching_session (from_service_tail,
1473 session->element_count,
1474 &needed_state, NULL);
1475 if (NULL != requesting_session)
1477 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s and a matching service-request-session set, processing.\n"), GNUNET_h2s (&session->key));
1478 if (GNUNET_OK != compute_service_response (requesting_session, session))
1480 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
1481 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1482 &prepare_client_end_notification,
1487 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s but NO matching service-request-session set, queuing element for later use.\n"), GNUNET_h2s (&session->key));
1488 // no matching session exists yet, store the response
1489 // for later processing by handle_service_request()
1495 * Function called for inbound tunnels.
1497 * @param cls closure
1498 * @param tunnel new handle to the tunnel
1499 * @param initiator peer that started the tunnel
1500 * @param atsi performance information for the tunnel
1501 * @return initial tunnel context for the tunnel
1502 * (can be NULL -- that's not an error)
1505 tunnel_incoming_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
1506 const struct GNUNET_PeerIdentity *initiator,
1507 const struct GNUNET_ATS_Information *atsi)
1510 struct ServiceSession * c = GNUNET_new (struct ServiceSession);
1512 memcpy (&c->peer, initiator, sizeof (struct GNUNET_PeerIdentity));
1520 * Function called whenever an inbound tunnel is destroyed. Should clean up
1521 * any associated state.
1523 * @param cls closure (set from #GNUNET_MESH_connect)
1524 * @param tunnel connection to the other end (henceforth invalid)
1525 * @param tunnel_ctx place where local state associated
1526 * with the tunnel is stored (our 'struct TunnelState')
1529 tunnel_destruction_handler (void *cls,
1530 const struct GNUNET_MESH_Tunnel *tunnel,
1533 struct ServiceSession * service_session = tunnel_ctx;
1534 struct ServiceSession * client_session;
1535 struct ServiceSession * curr;
1537 GNUNET_assert (service_session);
1538 if (!memcmp (&service_session->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Tunnel destroyed, terminating session with peer (%s)\n"), GNUNET_i2s (&service_session->peer));
1541 // remove the session, unless it has already been dequeued, but somehow still active
1542 // this could bug without the IF in case the queue is empty and the service session was the only one know to the service
1543 for (curr = from_service_head; NULL != curr; curr = curr->next)
1544 if (curr == service_session)
1546 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, curr);
1549 // there is a client waiting for this service session, terminate it, too!
1550 // i assume the tupel of key and element count is unique. if it was not the rest of the code would not work either.
1551 client_session = find_matching_session (from_client_tail,
1552 &service_session->key,
1553 service_session->element_count,
1555 free_session (service_session);
1557 // the client has to check if it was waiting for a result
1558 // or if it was a responder, no point in adding more statefulness
1559 if (client_session && ( ! do_shutdown))
1561 // remove the session, we just found it in the queue, so it must be there
1562 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, client_session);
1563 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1564 &prepare_client_end_notification,
1571 * Compute our scalar product, done by Alice
1573 * @param session - the session associated with this computation
1574 * @param kp - (1) from the protocol definition:
1575 * $E_A(a_{\pi(i)}) \otimes E_A(- r_{\pi(i)} - b_{\pi(i)}) &= E_A(a_{\pi(i)} - r_{\pi(i)} - b_{\pi(i)})$
1576 * @param kq - (2) from the protocol definition:
1577 * $E_A(a_{\pi'(i)}) \otimes E_A(- r_{\pi'(i)}) &= E_A(a_{\pi'(i)} - r_{\pi'(i)})$
1578 * @param s - S from the protocol definition:
1579 * $S := E_A(\sum (r_i + b_i)^2)$
1580 * @param stick - S' from the protocol definition:
1581 * $S' := E_A(\sum r_i^2)$
1582 * @return product as MPI, never NULL
1585 compute_scalar_product (struct ServiceSession * session,
1586 gcry_mpi_t * r, gcry_mpi_t * r_prime, gcry_mpi_t s, gcry_mpi_t s_prime)
1597 count = session->used_element_count;
1598 tmp = gcry_mpi_new (KEYBITS);
1599 // due to the introduced static offset S, we now also have to remove this
1600 // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each,
1601 // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi)
1602 for (i = 0; i < count; i++)
1604 decrypt_element (r[i], r[i], my_mu, my_lambda, my_n, my_nsquare);
1605 gcry_mpi_sub(r[i],r[i],my_offset);
1606 gcry_mpi_sub(r[i],r[i],my_offset);
1607 decrypt_element (r_prime[i], r_prime[i], my_mu, my_lambda, my_n, my_nsquare);
1608 gcry_mpi_sub(r_prime[i],r_prime[i],my_offset);
1609 gcry_mpi_sub(r_prime[i],r_prime[i],my_offset);
1612 // calculate t = sum(ai)
1613 t = compute_square_sum (session->a, count);
1616 u = gcry_mpi_new (0);
1617 tmp = compute_square_sum (r, count);
1618 gcry_mpi_sub (u, u, tmp);
1619 gcry_mpi_release (tmp);
1622 utick = gcry_mpi_new (0);
1623 tmp = compute_square_sum (r_prime, count);
1624 gcry_mpi_sub (utick, utick, tmp);
1626 GNUNET_assert (p = gcry_mpi_new (0));
1627 GNUNET_assert (ptick = gcry_mpi_new (0));
1630 decrypt_element (s, s, my_mu, my_lambda, my_n, my_nsquare);
1631 decrypt_element (s_prime, s_prime, my_mu, my_lambda, my_n, my_nsquare);
1634 gcry_mpi_add (p, s, t);
1635 gcry_mpi_add (p, p, u);
1638 gcry_mpi_add (ptick, s_prime, t);
1639 gcry_mpi_add (ptick, ptick, utick);
1641 gcry_mpi_release (t);
1642 gcry_mpi_release (u);
1643 gcry_mpi_release (utick);
1646 gcry_mpi_sub (p, p, ptick);
1647 gcry_mpi_release (ptick);
1648 tmp = gcry_mpi_set_ui (tmp, 2);
1649 gcry_mpi_div (p, NULL, p, tmp, 0);
1651 gcry_mpi_release (tmp);
1652 for (i = 0; i < count; i++)
1653 gcry_mpi_release (session->a[i]);
1654 GNUNET_free (session->a);
1662 * prepare the response we will send to alice or bobs' clients.
1663 * in Bobs case the product will be NULL.
1665 * @param session the session associated with our client.
1668 prepare_client_response (void *cls,
1669 const struct GNUNET_SCHEDULER_TaskContext *tc)
1671 struct ServiceSession * session = cls;
1672 struct GNUNET_SCALARPRODUCT_client_response * msg;
1673 unsigned char * product_exported = NULL;
1674 size_t product_length = 0;
1675 uint16_t msg_length = 0;
1676 struct MessageObject * msg_obj;
1680 if (session->product)
1682 gcry_mpi_t value = gcry_mpi_new(0);
1684 sign = gcry_mpi_cmp_ui(session->product, 0);
1685 // libgcrypt can not handle a print of a negative number
1688 gcry_mpi_sub(value, value, session->product);
1692 gcry_mpi_add(value, value, session->product);
1695 // get representation as string
1696 // unfortunately libgcrypt is too stupid to implement print-support in
1697 // signed GCRYMPI_FMT_STD format, and simply asserts in that case.
1698 // here is the associated sourcecode:
1699 // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */
1701 GNUNET_assert ( ! gcry_mpi_aprint (GCRYMPI_FMT_USG, // FIXME: just log (& survive!)
1706 gcry_mpi_release (session->product);
1707 session->product = NULL;
1710 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) + product_length;
1711 msg = GNUNET_malloc (msg_length);
1712 memcpy (&msg[1], product_exported, product_length);
1713 GNUNET_free_non_null (product_exported);
1714 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
1715 msg->header.size = htons (msg_length);
1717 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
1718 memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
1719 msg->product_length = htonl (product_length);
1721 msg_obj = GNUNET_new (struct MessageObject);
1722 msg_obj->msg = (struct GNUNET_MessageHeader *) msg;
1723 msg_obj->transmit_handle = NULL; // don't reset the transmit handle
1725 //transmit this message to our client
1726 session->client_transmit_handle = // FIXME: use after free possibility during shutdown
1727 GNUNET_SERVER_notify_transmit_ready (session->client,
1729 GNUNET_TIME_UNIT_FOREVER_REL,
1732 if ( ! session->client_transmit_handle)
1734 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)! This probably is OK if the client disconnected before us.\n"), session->client);
1735 session->client = NULL;
1736 // callback was not called!
1737 GNUNET_free (msg_obj);
1741 // gracefully sent message, just terminate session structure
1742 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Sent result to client (%p), this session (%s) has ended!\n"), session->client, GNUNET_h2s (&session->key));
1743 free_session (session);
1748 * Handle a request from another service to calculate a scalarproduct with us.
1750 * @param cls closure (set from #GNUNET_MESH_connect)
1751 * @param tunnel connection to the other end
1752 * @param tunnel_ctx place to store local state associated with the tunnel
1753 * @param sender who sent the message
1754 * @param message the actual message
1755 * @param atsi performance data for the connection
1756 * @return #GNUNET_OK to keep the connection open,
1757 * #GNUNET_SYSERR to close it (signal serious error)
1760 handle_service_request (void *cls,
1761 struct GNUNET_MESH_Tunnel * tunnel,
1763 const struct GNUNET_PeerIdentity * sender,
1764 const struct GNUNET_MessageHeader * message,
1765 const struct GNUNET_ATS_Information * atsi)
1767 struct ServiceSession * session;
1768 const struct GNUNET_SCALARPRODUCT_service_request * msg = (const struct GNUNET_SCALARPRODUCT_service_request *) message;
1769 uint16_t mask_length;
1771 uint16_t used_elements;
1772 uint16_t element_count;
1773 uint16_t msg_length;
1774 unsigned char * current;
1775 struct ServiceSession * responder_session;
1777 enum SessionState needed_state;
1779 session = (struct ServiceSession *) * tunnel_ctx;
1780 // is this tunnel already in use?
1781 if ( (session->next) || (from_service_head == session))
1783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Got a service request over a tunnel that is already in use, ignoring!\n"));
1784 return GNUNET_SYSERR;
1786 // Check if message was sent by me, which would be bad!
1787 if ( ! memcmp (sender, &me, sizeof (struct GNUNET_PeerIdentity)))
1790 GNUNET_free (session);
1791 return GNUNET_SYSERR;
1793 // this protocol can at best be 1:N, but never M:N!
1794 // Check if the sender is not the peer, I am connected to, which would be bad!
1795 if (memcmp (sender, &session->peer, sizeof (struct GNUNET_PeerIdentity)))
1798 GNUNET_free (session);
1799 return GNUNET_SYSERR;
1802 //we need at least a peer and one message id to compare
1803 if (ntohs (msg->header.size) < sizeof (struct GNUNET_SCALARPRODUCT_service_request))
1805 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Too short message received from peer!\n"));
1806 GNUNET_free (session);
1807 return GNUNET_SYSERR;
1809 mask_length = ntohs (msg->mask_length);
1810 pk_length = ntohs (msg->pk_length);
1811 used_elements = ntohs (msg->used_element_count);
1812 element_count = ntohs (msg->element_count);
1813 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1814 + mask_length + pk_length + used_elements * PAILLIER_ELEMENT_LENGTH;
1816 //sanity check: is the message as long as the message_count fields suggests?
1817 if ((ntohs (msg->header.size) != msg_length) || (element_count < used_elements)
1818 || (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0)))
1821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Invalid message received from peer, message count does not match message length!\n"));
1822 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Used elements: %hu\nElement Count: %hu\nExpected Mask Length: %hu\nCalculated Masklength: %d\n"), used_elements, element_count, mask_length, (element_count / 8 + (element_count % 8 ? 1 : 0)));
1823 GNUNET_free (session);
1824 return GNUNET_SYSERR;
1826 if (find_matching_session (from_service_tail,
1832 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"), (const char *) &(msg->key));
1833 GNUNET_free (session);
1834 return GNUNET_SYSERR;
1837 memcpy (&session->peer, sender, sizeof (struct GNUNET_PeerIdentity));
1838 session->state = REQUEST_FROM_SERVICE_RECEIVED;
1839 session->element_count = ntohs (msg->element_count);
1840 session->used_element_count = used_elements;
1841 session->tunnel = tunnel;
1844 memcpy (&session->key, &msg->key, sizeof (struct GNUNET_HashCode));
1845 current = (unsigned char *) &msg[1];
1846 //preserve the mask, we will need that later on
1847 session->mask = GNUNET_malloc (mask_length);
1848 memcpy (session->mask, current, mask_length);
1850 current += mask_length;
1852 //convert the publickey to sexp
1853 if (gcry_sexp_new (&session->remote_pubkey, current, pk_length, 1))
1855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate remote public key to sexpression!\n"));
1856 GNUNET_free (session->mask);
1857 GNUNET_free (session);
1858 return GNUNET_SYSERR;
1861 current += pk_length;
1863 //check if service queue contains a matching request
1864 needed_state = MESSAGE_FROM_RESPONDING_CLIENT_RECEIVED;
1865 responder_session = find_matching_session (from_client_tail,
1867 session->element_count,
1868 &needed_state, NULL);
1870 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
1872 if (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1875 + used_elements * PAILLIER_ELEMENT_LENGTH)
1877 gcry_error_t ret = 0;
1878 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
1879 // Convert each vector element to MPI_value
1880 for (i = 0; i < used_elements; i++)
1884 ret = gcry_mpi_scan (&session->a[i],
1886 ¤t[i * PAILLIER_ELEMENT_LENGTH],
1887 PAILLIER_ELEMENT_LENGTH,
1891 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate E[a%d] to MPI!\n%s/%s\n"),
1892 i, gcry_strsource (ret), gcry_strerror (ret));
1896 GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session);
1897 if (responder_session)
1899 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key));
1900 if (GNUNET_OK != compute_service_response (session, responder_session))
1902 //something went wrong, remove it again...
1903 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
1908 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
1913 // TODO FEATURE: fallback to fragmentation, in case the message is too long
1914 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!\n"));
1918 for (i = 0; i < used_elements; i++)
1920 gcry_mpi_release (session->a[i]);
1921 gcry_sexp_release (session->remote_pubkey);
1922 session->remote_pubkey = NULL;
1923 GNUNET_free_non_null (session->a);
1925 free_session (session);
1926 // and notify our client-session that we could not complete the session
1927 if (responder_session)
1929 // we just found the responder session in this queue
1930 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, responder_session);
1931 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1932 &prepare_client_end_notification,
1935 return GNUNET_SYSERR;
1940 * Handle a response we got from another service we wanted to calculate a scalarproduct with.
1942 * @param cls closure (set from #GNUNET_MESH_connect)
1943 * @param tunnel connection to the other end
1944 * @param tunnel_ctx place to store local state associated with the tunnel
1945 * @param sender who sent the message
1946 * @param message the actual message
1947 * @param atsi performance data for the connection
1948 * @return #GNUNET_OK to keep the connection open,
1949 * #GNUNET_SYSERR to close it (signal serious error)
1952 handle_service_response (void *cls,
1953 struct GNUNET_MESH_Tunnel * tunnel,
1955 const struct GNUNET_PeerIdentity * sender,
1956 const struct GNUNET_MessageHeader * message,
1957 const struct GNUNET_ATS_Information * atsi)
1960 struct ServiceSession * session;
1961 struct GNUNET_SCALARPRODUCT_service_response * msg = (struct GNUNET_SCALARPRODUCT_service_response *) message;
1962 unsigned char * current;
1964 gcry_mpi_t s = NULL;
1965 gcry_mpi_t s_prime = NULL;
1968 uint16_t used_element_count;
1970 gcry_mpi_t * r = NULL;
1971 gcry_mpi_t * r_prime = NULL;
1974 GNUNET_assert (NULL != message);
1975 GNUNET_assert (NULL != sender);
1976 GNUNET_assert (NULL != tunnel_ctx);
1977 session = (struct ServiceSession *) * tunnel_ctx;
1978 GNUNET_assert (NULL != session);
1979 count = session->used_element_count;
1980 session->product = NULL;
1982 if (memcmp (&session->peer, sender, sizeof (struct GNUNET_PeerIdentity)))
1984 GNUNET_break_op (0);
1987 //we need at least a peer and one message id to compare
1988 if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) > ntohs (msg->header.size))
1990 GNUNET_break_op (0);
1993 used_element_count = ntohs (msg->used_element_count);
1994 msg_size = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
1995 + 2 * used_element_count * PAILLIER_ELEMENT_LENGTH
1996 + 2 * PAILLIER_ELEMENT_LENGTH;
1997 //sanity check: is the message as long as the message_count fields suggests?
1998 if ((ntohs (msg->header.size) != msg_size) || (count != used_element_count))
2000 GNUNET_break_op (0);
2005 current = (unsigned char *) &msg[1];
2006 if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG, current,
2007 PAILLIER_ELEMENT_LENGTH, &read)))
2009 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2010 GNUNET_break_op (0);
2013 current += PAILLIER_ELEMENT_LENGTH;
2015 if (0 != (rc = gcry_mpi_scan (&s_prime, GCRYMPI_FMT_USG, current,
2016 PAILLIER_ELEMENT_LENGTH, &read)))
2018 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2019 GNUNET_break_op (0);
2022 current += PAILLIER_ELEMENT_LENGTH;
2024 r = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
2025 // Convert each kp[] to its MPI_value
2026 for (i = 0; i < count; i++)
2028 if (0 != (rc = gcry_mpi_scan (&r[i], GCRYMPI_FMT_USG, current,
2029 PAILLIER_ELEMENT_LENGTH, &read)))
2031 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2032 GNUNET_break_op (0);
2035 current += PAILLIER_ELEMENT_LENGTH;
2039 r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
2040 // Convert each kq[] to its MPI_value
2041 for (i = 0; i < count; i++)
2043 if (0 != (rc = gcry_mpi_scan (&r_prime[i], GCRYMPI_FMT_USG, current,
2044 PAILLIER_ELEMENT_LENGTH, &read)))
2046 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2047 GNUNET_break_op (0);
2050 current += PAILLIER_ELEMENT_LENGTH;
2053 session->product = compute_scalar_product (session, r, r_prime, s, s_prime);
2057 gcry_mpi_release (s);
2059 gcry_mpi_release (s_prime);
2060 for (i = 0; r && i < count; i++)
2061 if (r[i]) gcry_mpi_release (r[i]);
2062 for (i = 0; r_prime && i < count; i++)
2063 if (r_prime[i]) gcry_mpi_release (r_prime[i]);
2064 GNUNET_free_non_null (r);
2065 GNUNET_free_non_null (r_prime);
2067 session->state = FINALIZED;
2068 // the tunnel has done its job, terminate our connection and the tunnel
2069 // the peer will be notified that the tunnel was destroyed via tunnel_destruction_handler
2070 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
2071 GNUNET_SCHEDULER_add_now (&destroy_tunnel, session); // FIXME: use after free!
2072 // send message with product to client
2073 /* session->current_task = */ GNUNET_SCHEDULER_add_now (&prepare_client_response, session); // FIXME: dangling task!
2075 // if success: terminate the session gracefully, else terminate with error
2080 * Task run during shutdown.
2086 shutdown_task (void *cls,
2087 const struct GNUNET_SCHEDULER_TaskContext *tc)
2089 struct ServiceSession * curr;
2090 struct ServiceSession * next;
2091 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Shutting down, initiating cleanup.\n"));
2093 do_shutdown = GNUNET_YES;
2094 // terminate all owned open tunnels.
2095 for (curr = from_client_head; NULL != curr; curr = next)
2098 if (FINALIZED != curr->state)
2100 destroy_tunnel (curr, NULL);
2101 curr->state = FINALIZED;
2106 GNUNET_MESH_disconnect (my_mesh);
2113 * Initialization of the program and message handlers
2115 * @param cls closure
2116 * @param server the initialized server
2117 * @param c configuration to use
2121 struct GNUNET_SERVER_Handle *server,
2122 const struct GNUNET_CONFIGURATION_Handle *c)
2124 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
2125 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE, 0},
2126 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB, 0},
2129 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
2130 { &handle_service_request, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB, 0},
2131 { &handle_service_response, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE, 0},
2134 static GNUNET_MESH_ApplicationType mesh_types[] = {
2135 GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
2136 GNUNET_APPLICATION_TYPE_END
2139 //generate private/public key set
2140 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Generating Paillier-Keyset.\n"));
2142 // register server callbacks and disconnect handler
2143 GNUNET_SERVER_add_handlers (server, server_handlers);
2144 GNUNET_SERVER_disconnect_notify (server,
2145 &handle_client_disconnect,
2147 GNUNET_break (GNUNET_OK ==
2148 GNUNET_CRYPTO_get_host_identity (c,
2150 my_mesh = GNUNET_MESH_connect (c, NULL,
2151 &tunnel_incoming_handler,
2152 &tunnel_destruction_handler,
2153 mesh_handlers, mesh_types);
2156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n"));
2157 GNUNET_SCHEDULER_shutdown ();
2160 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Mesh initialized\n"));
2161 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2168 * The main function for the scalarproduct service.
2170 * @param argc number of arguments from the command line
2171 * @param argv command line arguments
2172 * @return 0 ok, 1 on error
2175 main (int argc, char *const *argv)
2177 return (GNUNET_OK ==
2178 GNUNET_SERVICE_run (argc, argv,
2180 GNUNET_SERVICE_OPTION_NONE,
2181 &run, NULL)) ? 0 : 1;
2184 /* end of gnunet-service-ext.c */