diff options
author | Christian Fuchs <christian.fuchs@cfuchs.net> | 2013-10-14 12:35:46 +0000 |
---|---|---|
committer | Christian Fuchs <christian.fuchs@cfuchs.net> | 2013-10-14 12:35:46 +0000 |
commit | e9dda4933275bc7e260981a8757ba8803c585907 (patch) | |
tree | 818e7238fdea0ae46bb1f84e876d6c5e896d968a /src/scalarproduct | |
parent | a806156660a4212ad2a0df4bf9cb9982e23b798a (diff) | |
download | gnunet-e9dda4933275bc7e260981a8757ba8803c585907.tar.gz gnunet-e9dda4933275bc7e260981a8757ba8803c585907.zip |
second part of function comment doxygenization
more formatting work
Diffstat (limited to 'src/scalarproduct')
-rw-r--r-- | src/scalarproduct/gnunet-service-scalarproduct.c | 598 |
1 files changed, 303 insertions, 295 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct.c b/src/scalarproduct/gnunet-service-scalarproduct.c index c767ffb86..d78f92d40 100644 --- a/src/scalarproduct/gnunet-service-scalarproduct.c +++ b/src/scalarproduct/gnunet-service-scalarproduct.c | |||
@@ -39,31 +39,34 @@ | |||
39 | // Service Structure Definitions | 39 | // Service Structure Definitions |
40 | /////////////////////////////////////////////////////////////////////////////// | 40 | /////////////////////////////////////////////////////////////////////////////// |
41 | 41 | ||
42 | |||
42 | /** | 43 | /** |
43 | * state a session can be in | 44 | * state a session can be in |
44 | */ | 45 | */ |
45 | enum SessionState | 46 | enum SessionState |
46 | { | 47 | { |
47 | CLIENT_REQUEST_RECEIVED, | 48 | CLIENT_REQUEST_RECEIVED, |
48 | WAITING_FOR_BOBS_CONNECT, | 49 | WAITING_FOR_BOBS_CONNECT, |
49 | CLIENT_RESPONSE_RECEIVED, | 50 | CLIENT_RESPONSE_RECEIVED, |
50 | WAITING_FOR_SERVICE_REQUEST, | 51 | WAITING_FOR_SERVICE_REQUEST, |
51 | WAITING_FOR_MULTIPART_TRANSMISSION, | 52 | WAITING_FOR_MULTIPART_TRANSMISSION, |
52 | WAITING_FOR_SERVICE_RESPONSE, | 53 | WAITING_FOR_SERVICE_RESPONSE, |
53 | SERVICE_REQUEST_RECEIVED, | 54 | SERVICE_REQUEST_RECEIVED, |
54 | SERVICE_RESPONSE_RECEIVED, | 55 | SERVICE_RESPONSE_RECEIVED, |
55 | FINALIZED | 56 | FINALIZED |
56 | }; | 57 | }; |
57 | 58 | ||
59 | |||
58 | /** | 60 | /** |
59 | * role a peer in a session can assume | 61 | * role a peer in a session can assume |
60 | */ | 62 | */ |
61 | enum PeerRole | 63 | enum PeerRole |
62 | { | 64 | { |
63 | ALICE, | 65 | ALICE, |
64 | BOB | 66 | BOB |
65 | }; | 67 | }; |
66 | 68 | ||
69 | |||
67 | /** | 70 | /** |
68 | * A scalarproduct session which tracks: | 71 | * A scalarproduct session which tracks: |
69 | * | 72 | * |
@@ -73,146 +76,146 @@ enum PeerRole | |||
73 | */ | 76 | */ |
74 | struct ServiceSession | 77 | struct ServiceSession |
75 | { | 78 | { |
76 | /** | 79 | /** |
77 | * the role this peer has | 80 | * the role this peer has |
78 | */ | 81 | */ |
79 | enum PeerRole role; | 82 | enum PeerRole role; |
80 | 83 | ||
81 | /** | 84 | /** |
82 | * session information is kept in a DLL | 85 | * session information is kept in a DLL |
83 | */ | 86 | */ |
84 | struct ServiceSession *next; | 87 | struct ServiceSession *next; |
85 | 88 | ||
86 | /** | 89 | /** |
87 | * session information is kept in a DLL | 90 | * session information is kept in a DLL |
88 | */ | 91 | */ |
89 | struct ServiceSession *prev; | 92 | struct ServiceSession *prev; |
90 | 93 | ||
91 | /** | 94 | /** |
92 | * (hopefully) unique transaction ID | 95 | * (hopefully) unique transaction ID |
93 | */ | 96 | */ |
94 | struct GNUNET_HashCode key; | 97 | struct GNUNET_HashCode key; |
95 | 98 | ||
96 | /** | 99 | /** |
97 | * state of the session | 100 | * state of the session |
98 | */ | 101 | */ |
99 | enum SessionState state; | 102 | enum SessionState state; |
100 | 103 | ||
101 | /** | 104 | /** |
102 | * Alice or Bob's peerID | 105 | * Alice or Bob's peerID |
103 | */ | 106 | */ |
104 | struct GNUNET_PeerIdentity peer; | 107 | struct GNUNET_PeerIdentity peer; |
105 | 108 | ||
106 | /** | 109 | /** |
107 | * the client this request is related to | 110 | * the client this request is related to |
108 | */ | 111 | */ |
109 | struct GNUNET_SERVER_Client * client; | 112 | struct GNUNET_SERVER_Client * client; |
110 | 113 | ||
111 | /** | 114 | /** |
112 | * The message to send | 115 | * The message to send |
113 | */ | 116 | */ |
114 | struct GNUNET_MessageHeader * msg; | 117 | struct GNUNET_MessageHeader * msg; |
115 | 118 | ||
116 | /** | 119 | /** |
117 | * how many elements we were supplied with from the client | 120 | * how many elements we were supplied with from the client |
118 | */ | 121 | */ |
119 | uint32_t total; | 122 | uint32_t total; |
120 | 123 | ||
121 | /** | 124 | /** |
122 | * how many elements actually are used after applying the mask | 125 | * how many elements actually are used after applying the mask |
123 | */ | 126 | */ |
124 | uint32_t used; | 127 | uint32_t used; |
125 | 128 | ||
126 | /** | 129 | /** |
127 | * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for | 130 | * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for |
128 | */ | 131 | */ |
129 | uint32_t transferred; | 132 | uint32_t transferred; |
130 | 133 | ||
131 | /** | 134 | /** |
132 | * index of the last transferred element for multipart messages | 135 | * index of the last transferred element for multipart messages |
133 | */ | 136 | */ |
134 | uint32_t last_processed; | 137 | uint32_t last_processed; |
135 | 138 | ||
136 | /** | 139 | /** |
137 | * how many bytes the mask is long. | 140 | * how many bytes the mask is long. |
138 | * just for convenience so we don't have to re-re-re calculate it each time | 141 | * just for convenience so we don't have to re-re-re calculate it each time |
139 | */ | 142 | */ |
140 | uint32_t mask_length; | 143 | uint32_t mask_length; |
141 | 144 | ||
142 | /** | 145 | /** |
143 | * all the vector elements we received | 146 | * all the vector elements we received |
144 | */ | 147 | */ |
145 | int32_t * vector; | 148 | int32_t * vector; |
146 | 149 | ||
147 | /** | 150 | /** |
148 | * mask of which elements to check | 151 | * mask of which elements to check |
149 | */ | 152 | */ |
150 | unsigned char * mask; | 153 | unsigned char * mask; |
151 | 154 | ||
152 | /** | 155 | /** |
153 | * Public key of the remote service, only used by bob | 156 | * Public key of the remote service, only used by bob |
154 | */ | 157 | */ |
155 | gcry_sexp_t remote_pubkey; | 158 | gcry_sexp_t remote_pubkey; |
156 | 159 | ||
157 | /** | 160 | /** |
158 | * E(ai)(Bob) or ai(Alice) after applying the mask | 161 | * E(ai)(Bob) or ai(Alice) after applying the mask |
159 | */ | 162 | */ |
160 | gcry_mpi_t * a; | 163 | gcry_mpi_t * a; |
161 | 164 | ||
162 | /** | 165 | /** |
163 | * Bob's permutation p of R | 166 | * Bob's permutation p of R |
164 | */ | 167 | */ |
165 | gcry_mpi_t * r; | 168 | gcry_mpi_t * r; |
166 | 169 | ||
167 | /** | 170 | /** |
168 | * Bob's permutation q of R | 171 | * Bob's permutation q of R |
169 | */ | 172 | */ |
170 | gcry_mpi_t * r_prime; | 173 | gcry_mpi_t * r_prime; |
171 | 174 | ||
172 | /** | 175 | /** |
173 | * Bob's s | 176 | * Bob's s |
174 | */ | 177 | */ |
175 | gcry_mpi_t s; | 178 | gcry_mpi_t s; |
176 | 179 | ||
177 | /** | 180 | /** |
178 | * Bob's s' | 181 | * Bob's s' |
179 | */ | 182 | */ |
180 | gcry_mpi_t s_prime; | 183 | gcry_mpi_t s_prime; |
181 | 184 | ||
182 | /** | 185 | /** |
183 | * Bobs matching response session from the client | 186 | * Bobs matching response session from the client |
184 | */ | 187 | */ |
185 | struct ServiceSession * response; | 188 | struct ServiceSession * response; |
186 | 189 | ||
187 | /** | 190 | /** |
188 | * The computed scalar | 191 | * The computed scalar |
189 | */ | 192 | */ |
190 | gcry_mpi_t product; | 193 | gcry_mpi_t product; |
191 | 194 | ||
192 | /** | 195 | /** |
193 | * My transmit handle for the current message to a alice/bob | 196 | * My transmit handle for the current message to a alice/bob |
194 | */ | 197 | */ |
195 | struct GNUNET_MESH_TransmitHandle * service_transmit_handle; | 198 | struct GNUNET_MESH_TransmitHandle * service_transmit_handle; |
196 | 199 | ||
197 | /** | 200 | /** |
198 | * My transmit handle for the current message to the client | 201 | * My transmit handle for the current message to the client |
199 | */ | 202 | */ |
200 | struct GNUNET_SERVER_TransmitHandle * client_transmit_handle; | 203 | struct GNUNET_SERVER_TransmitHandle * client_transmit_handle; |
201 | 204 | ||
202 | /** | 205 | /** |
203 | * tunnel-handle associated with our mesh handle | 206 | * tunnel-handle associated with our mesh handle |
204 | */ | 207 | */ |
205 | struct GNUNET_MESH_Tunnel * tunnel; | 208 | struct GNUNET_MESH_Tunnel * tunnel; |
206 | 209 | ||
207 | /** | 210 | /** |
208 | * Handle to a task that sends a msg to the our client | 211 | * Handle to a task that sends a msg to the our client |
209 | */ | 212 | */ |
210 | GNUNET_SCHEDULER_TaskIdentifier client_notification_task; | 213 | GNUNET_SCHEDULER_TaskIdentifier client_notification_task; |
211 | 214 | ||
212 | /** | 215 | /** |
213 | * Handle to a task that sends a msg to the our peer | 216 | * Handle to a task that sends a msg to the our peer |
214 | */ | 217 | */ |
215 | GNUNET_SCHEDULER_TaskIdentifier service_request_task; | 218 | GNUNET_SCHEDULER_TaskIdentifier service_request_task; |
216 | }; | 219 | }; |
217 | 220 | ||
218 | /////////////////////////////////////////////////////////////////////////////// | 221 | /////////////////////////////////////////////////////////////////////////////// |
@@ -543,6 +546,7 @@ compute_square_sum (gcry_mpi_t * vector, uint32_t length) | |||
543 | return sum; | 546 | return sum; |
544 | } | 547 | } |
545 | 548 | ||
549 | |||
546 | /** | 550 | /** |
547 | * Primitive callback for copying over a message, as they | 551 | * Primitive callback for copying over a message, as they |
548 | * usually are too complex to be handled in the callback itself. | 552 | * usually are too complex to be handled in the callback itself. |
@@ -561,12 +565,11 @@ do_send_message (void *cls, size_t size, void *buf) | |||
561 | 565 | ||
562 | GNUNET_assert (buf); | 566 | GNUNET_assert (buf); |
563 | 567 | ||
564 | if (ntohs (session->msg->size) != size) | 568 | if (ntohs (session->msg->size) != size) { |
565 | { | ||
566 | GNUNET_break (0); | 569 | GNUNET_break (0); |
567 | return 0; | 570 | return 0; |
568 | } | 571 | } |
569 | 572 | ||
570 | type = ntohs (session->msg->type); | 573 | type = ntohs (session->msg->type); |
571 | memcpy (buf, session->msg, size); | 574 | memcpy (buf, session->msg, size); |
572 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 575 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
@@ -597,12 +600,13 @@ do_send_message (void *cls, size_t size, void *buf) | |||
597 | break; | 600 | break; |
598 | 601 | ||
599 | default: | 602 | default: |
600 | GNUNET_assert(0); | 603 | GNUNET_assert (0); |
601 | } | 604 | } |
602 | 605 | ||
603 | return size; | 606 | return size; |
604 | } | 607 | } |
605 | 608 | ||
609 | |||
606 | /** | 610 | /** |
607 | * initializes a new vector with fresh MPI values (=0) of a given length | 611 | * initializes a new vector with fresh MPI values (=0) of a given length |
608 | * | 612 | * |
@@ -620,6 +624,7 @@ initialize_mpi_vector (uint32_t length) | |||
620 | return output; | 624 | return output; |
621 | } | 625 | } |
622 | 626 | ||
627 | |||
623 | /** | 628 | /** |
624 | * permutes an MPI vector according to the given permutation vector | 629 | * permutes an MPI vector according to the given permutation vector |
625 | * | 630 | * |
@@ -698,21 +703,21 @@ free_session_variables (struct ServiceSession * session) | |||
698 | { | 703 | { |
699 | unsigned int i; | 704 | unsigned int i; |
700 | 705 | ||
701 | if (session->a){ | 706 | if (session->a) { |
702 | for (i = 0; i < session->used; i++) | 707 | for (i = 0; i < session->used; i++) |
703 | if (session->a[i]) gcry_mpi_release (session->a[i]); | 708 | if (session->a[i]) gcry_mpi_release (session->a[i]); |
704 | GNUNET_free (session->a); | 709 | GNUNET_free (session->a); |
705 | } | 710 | } |
706 | GNUNET_free_non_null (session->mask); | 711 | GNUNET_free_non_null (session->mask); |
707 | if (session->r){ | 712 | if (session->r) { |
708 | for (i = 0; i < session->used; i++) | 713 | for (i = 0; i < session->used; i++) |
709 | if (session->r[i]) gcry_mpi_release (session->r[i]); | 714 | if (session->r[i]) gcry_mpi_release (session->r[i]); |
710 | GNUNET_free(session->r); | 715 | GNUNET_free (session->r); |
711 | } | 716 | } |
712 | if (session->r_prime){ | 717 | if (session->r_prime) { |
713 | for (i = 0; i < session->used; i++) | 718 | for (i = 0; i < session->used; i++) |
714 | if (session->r_prime[i]) gcry_mpi_release (session->r_prime[i]); | 719 | if (session->r_prime[i]) gcry_mpi_release (session->r_prime[i]); |
715 | GNUNET_free(session->r_prime); | 720 | GNUNET_free (session->r_prime); |
716 | } | 721 | } |
717 | if (session->s) | 722 | if (session->s) |
718 | gcry_mpi_release (session->s); | 723 | gcry_mpi_release (session->s); |
@@ -730,6 +735,7 @@ free_session_variables (struct ServiceSession * session) | |||
730 | // Event and Message Handlers | 735 | // Event and Message Handlers |
731 | /////////////////////////////////////////////////////////////////////////////// | 736 | /////////////////////////////////////////////////////////////////////////////// |
732 | 737 | ||
738 | |||
733 | /** | 739 | /** |
734 | * A client disconnected. | 740 | * A client disconnected. |
735 | * | 741 | * |
@@ -830,7 +836,100 @@ prepare_client_end_notification (void * cls, | |||
830 | 836 | ||
831 | 837 | ||
832 | /** | 838 | /** |
833 | * Send a multi part chunk of a service response from bob to alice. | 839 | * prepare the response we will send to alice or bobs' clients. |
840 | * in Bobs case the product will be NULL. | ||
841 | * | ||
842 | * @param cls the session associated with our client. | ||
843 | * @param tc the task context handed to us by the scheduler, unused | ||
844 | */ | ||
845 | static void | ||
846 | prepare_client_response (void *cls, | ||
847 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
848 | { | ||
849 | struct ServiceSession * session = cls; | ||
850 | struct GNUNET_SCALARPRODUCT_client_response * msg; | ||
851 | unsigned char * product_exported = NULL; | ||
852 | size_t product_length = 0; | ||
853 | uint32_t msg_length = 0; | ||
854 | int8_t range = -1; | ||
855 | gcry_error_t rc; | ||
856 | int sign; | ||
857 | |||
858 | session->client_notification_task = GNUNET_SCHEDULER_NO_TASK; | ||
859 | |||
860 | if (session->product) { | ||
861 | gcry_mpi_t value = gcry_mpi_new (0); | ||
862 | |||
863 | sign = gcry_mpi_cmp_ui (session->product, 0); | ||
864 | // libgcrypt can not handle a print of a negative number | ||
865 | // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */ | ||
866 | if (0 > sign) { | ||
867 | gcry_mpi_sub (value, value, session->product); | ||
868 | } | ||
869 | else if (0 < sign) { | ||
870 | range = 1; | ||
871 | gcry_mpi_add (value, value, session->product); | ||
872 | } | ||
873 | else | ||
874 | range = 0; | ||
875 | |||
876 | gcry_mpi_release (session->product); | ||
877 | session->product = NULL; | ||
878 | |||
879 | // get representation as string | ||
880 | if (range | ||
881 | && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD, | ||
882 | &product_exported, | ||
883 | &product_length, | ||
884 | value)))) { | ||
885 | LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); | ||
886 | product_length = 0; | ||
887 | range = -1; // signal error with product-length = 0 and range = -1 | ||
888 | } | ||
889 | gcry_mpi_release (value); | ||
890 | } | ||
891 | |||
892 | msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) +product_length; | ||
893 | msg = GNUNET_malloc (msg_length); | ||
894 | memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode)); | ||
895 | memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity)); | ||
896 | if (product_exported != NULL) { | ||
897 | memcpy (&msg[1], product_exported, product_length); | ||
898 | GNUNET_free (product_exported); | ||
899 | } | ||
900 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT); | ||
901 | msg->header.size = htons (msg_length); | ||
902 | msg->range = range; | ||
903 | msg->product_length = htonl (product_length); | ||
904 | |||
905 | session->msg = (struct GNUNET_MessageHeader *) msg; | ||
906 | //transmit this message to our client | ||
907 | session->client_transmit_handle = | ||
908 | GNUNET_SERVER_notify_transmit_ready (session->client, | ||
909 | msg_length, | ||
910 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
911 | &do_send_message, | ||
912 | session); | ||
913 | if (NULL == session->client_transmit_handle) { | ||
914 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
915 | _ ("Could not send message to client (%p)!\n"), | ||
916 | session->client); | ||
917 | session->client = NULL; | ||
918 | // callback was not called! | ||
919 | GNUNET_free (msg); | ||
920 | session->msg = NULL; | ||
921 | } | ||
922 | else | ||
923 | // gracefully sent message, just terminate session structure | ||
924 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
925 | _ ("Sent result to client (%p), this session (%s) has ended!\n"), | ||
926 | session->client, | ||
927 | GNUNET_h2s (&session->key)); | ||
928 | } | ||
929 | |||
930 | |||
931 | /** | ||
932 | * Send a multipart chunk of a service response from bob to alice. | ||
834 | * This element only contains the two permutations of R, R'. | 933 | * This element only contains the two permutations of R, R'. |
835 | * | 934 | * |
836 | * @param cls the associated service session | 935 | * @param cls the associated service session |
@@ -915,6 +1014,7 @@ prepare_service_response_multipart (void *cls) | |||
915 | session->state = FINALIZED; | 1014 | session->state = FINALIZED; |
916 | } | 1015 | } |
917 | 1016 | ||
1017 | |||
918 | /** | 1018 | /** |
919 | * Bob executes: | 1019 | * Bob executes: |
920 | * generates the response message to be sent to alice after computing | 1020 | * generates the response message to be sent to alice after computing |
@@ -927,8 +1027,8 @@ prepare_service_response_multipart (void *cls) | |||
927 | * @param s S: $S := E_A(sum (r_i + b_i)^2)$ | 1027 | * @param s S: $S := E_A(sum (r_i + b_i)^2)$ |
928 | * @param s_prime S': $S' := E_A(sum r_i^2)$ | 1028 | * @param s_prime S': $S' := E_A(sum r_i^2)$ |
929 | * @param session the associated requesting session with alice | 1029 | * @param session the associated requesting session with alice |
930 | * @return GNUNET_NO if we could not send our message | 1030 | * @return #GNUNET_NO if we could not send our message |
931 | * GNUNET_OK if the operation succeeded | 1031 | * #GNUNET_OK if the operation succeeded |
932 | */ | 1032 | */ |
933 | static int | 1033 | static int |
934 | prepare_service_response (gcry_mpi_t s, | 1034 | prepare_service_response (gcry_mpi_t s, |
@@ -1045,6 +1145,7 @@ prepare_service_response (gcry_mpi_t s, | |||
1045 | return GNUNET_OK; | 1145 | return GNUNET_OK; |
1046 | } | 1146 | } |
1047 | 1147 | ||
1148 | |||
1048 | /** | 1149 | /** |
1049 | * executed by bob: | 1150 | * executed by bob: |
1050 | * compute the values | 1151 | * compute the values |
@@ -1359,13 +1460,12 @@ prepare_service_request_multipart (void *cls) | |||
1359 | session->state = WAITING_FOR_SERVICE_RESPONSE; | 1460 | session->state = WAITING_FOR_SERVICE_RESPONSE; |
1360 | } | 1461 | } |
1361 | 1462 | ||
1463 | |||
1362 | /** | 1464 | /** |
1363 | * Executed by Alice, fills in a service-request message and sends it to the given peer | 1465 | * Executed by Alice, fills in a service-request message and sends it to the given peer |
1364 | * | 1466 | * |
1365 | * @param session the session associated with this request, then also holds the CORE-handle | 1467 | * @param cls the session associated with this request |
1366 | * @return #GNUNET_SYSERR if we could not send the message | 1468 | * @param tc task context handed over by scheduler, unsued |
1367 | * #GNUNET_NO if the message was too large | ||
1368 | * #GNUNET_OK if we sent it | ||
1369 | */ | 1469 | */ |
1370 | static void | 1470 | static void |
1371 | prepare_service_request (void *cls, | 1471 | prepare_service_request (void *cls, |
@@ -1488,6 +1588,7 @@ prepare_service_request (void *cls, | |||
1488 | session->state = WAITING_FOR_SERVICE_RESPONSE; | 1588 | session->state = WAITING_FOR_SERVICE_RESPONSE; |
1489 | } | 1589 | } |
1490 | 1590 | ||
1591 | |||
1491 | /** | 1592 | /** |
1492 | * Handler for a client request message. | 1593 | * Handler for a client request message. |
1493 | * Can either be type A or B | 1594 | * Can either be type A or B |
@@ -1675,9 +1776,8 @@ handle_client_request (void *cls, | |||
1675 | * @param cls closure | 1776 | * @param cls closure |
1676 | * @param tunnel new handle to the tunnel | 1777 | * @param tunnel new handle to the tunnel |
1677 | * @param initiator peer that started the tunnel | 1778 | * @param initiator peer that started the tunnel |
1678 | * @param atsi performance information for the tunnel | 1779 | * @param port unused |
1679 | * @return initial tunnel context for the tunnel | 1780 | * @return session associated with the tunnel |
1680 | * (can be NULL -- that's not an error) | ||
1681 | */ | 1781 | */ |
1682 | static void * | 1782 | static void * |
1683 | tunnel_incoming_handler (void *cls, | 1783 | tunnel_incoming_handler (void *cls, |
@@ -1840,96 +1940,6 @@ compute_scalar_product (struct ServiceSession * session) | |||
1840 | return p; | 1940 | return p; |
1841 | } | 1941 | } |
1842 | 1942 | ||
1843 | /** | ||
1844 | * prepare the response we will send to alice or bobs' clients. | ||
1845 | * in Bobs case the product will be NULL. | ||
1846 | * | ||
1847 | * @param session the session associated with our client. | ||
1848 | */ | ||
1849 | static void | ||
1850 | prepare_client_response (void *cls, | ||
1851 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
1852 | { | ||
1853 | struct ServiceSession * session = cls; | ||
1854 | struct GNUNET_SCALARPRODUCT_client_response * msg; | ||
1855 | unsigned char * product_exported = NULL; | ||
1856 | size_t product_length = 0; | ||
1857 | uint32_t msg_length = 0; | ||
1858 | int8_t range = -1; | ||
1859 | gcry_error_t rc; | ||
1860 | int sign; | ||
1861 | |||
1862 | session->client_notification_task = GNUNET_SCHEDULER_NO_TASK; | ||
1863 | |||
1864 | if (session->product) { | ||
1865 | gcry_mpi_t value = gcry_mpi_new (0); | ||
1866 | |||
1867 | sign = gcry_mpi_cmp_ui (session->product, 0); | ||
1868 | // libgcrypt can not handle a print of a negative number | ||
1869 | // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */ | ||
1870 | if (0 > sign) { | ||
1871 | gcry_mpi_sub (value, value, session->product); | ||
1872 | } | ||
1873 | else if (0 < sign) { | ||
1874 | range = 1; | ||
1875 | gcry_mpi_add (value, value, session->product); | ||
1876 | } | ||
1877 | else | ||
1878 | range = 0; | ||
1879 | |||
1880 | gcry_mpi_release (session->product); | ||
1881 | session->product = NULL; | ||
1882 | |||
1883 | // get representation as string | ||
1884 | if (range | ||
1885 | && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD, | ||
1886 | &product_exported, | ||
1887 | &product_length, | ||
1888 | value)))) { | ||
1889 | LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); | ||
1890 | product_length = 0; | ||
1891 | range = -1; // signal error with product-length = 0 and range = -1 | ||
1892 | } | ||
1893 | gcry_mpi_release (value); | ||
1894 | } | ||
1895 | |||
1896 | msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) +product_length; | ||
1897 | msg = GNUNET_malloc (msg_length); | ||
1898 | memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode)); | ||
1899 | memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity)); | ||
1900 | if (product_exported != NULL) { | ||
1901 | memcpy (&msg[1], product_exported, product_length); | ||
1902 | GNUNET_free (product_exported); | ||
1903 | } | ||
1904 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT); | ||
1905 | msg->header.size = htons (msg_length); | ||
1906 | msg->range = range; | ||
1907 | msg->product_length = htonl (product_length); | ||
1908 | |||
1909 | session->msg = (struct GNUNET_MessageHeader *) msg; | ||
1910 | //transmit this message to our client | ||
1911 | session->client_transmit_handle = | ||
1912 | GNUNET_SERVER_notify_transmit_ready (session->client, | ||
1913 | msg_length, | ||
1914 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
1915 | &do_send_message, | ||
1916 | session); | ||
1917 | if (NULL == session->client_transmit_handle) { | ||
1918 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1919 | _ ("Could not send message to client (%p)!\n"), | ||
1920 | session->client); | ||
1921 | session->client = NULL; | ||
1922 | // callback was not called! | ||
1923 | GNUNET_free (msg); | ||
1924 | session->msg = NULL; | ||
1925 | } | ||
1926 | else | ||
1927 | // gracefully sent message, just terminate session structure | ||
1928 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1929 | _ ("Sent result to client (%p), this session (%s) has ended!\n"), | ||
1930 | session->client, | ||
1931 | GNUNET_h2s (&session->key)); | ||
1932 | } | ||
1933 | 1943 | ||
1934 | /** | 1944 | /** |
1935 | * Handle a multipart-chunk of a request from another service to calculate a scalarproduct with us. | 1945 | * Handle a multipart-chunk of a request from another service to calculate a scalarproduct with us. |
@@ -1937,9 +1947,7 @@ prepare_client_response (void *cls, | |||
1937 | * @param cls closure (set from #GNUNET_MESH_connect) | 1947 | * @param cls closure (set from #GNUNET_MESH_connect) |
1938 | * @param tunnel connection to the other end | 1948 | * @param tunnel connection to the other end |
1939 | * @param tunnel_ctx place to store local state associated with the tunnel | 1949 | * @param tunnel_ctx place to store local state associated with the tunnel |
1940 | * @param sender who sent the message | ||
1941 | * @param message the actual message | 1950 | * @param message the actual message |
1942 | * @param atsi performance data for the connection | ||
1943 | * @return #GNUNET_OK to keep the connection open, | 1951 | * @return #GNUNET_OK to keep the connection open, |
1944 | * #GNUNET_SYSERR to close it (signal serious error) | 1952 | * #GNUNET_SYSERR to close it (signal serious error) |
1945 | */ | 1953 | */ |
@@ -1952,12 +1960,12 @@ handle_service_request_multipart (void *cls, | |||
1952 | struct ServiceSession * session; | 1960 | struct ServiceSession * session; |
1953 | const struct GNUNET_SCALARPRODUCT_multipart_message * msg = (const struct GNUNET_SCALARPRODUCT_multipart_message *) message; | 1961 | const struct GNUNET_SCALARPRODUCT_multipart_message * msg = (const struct GNUNET_SCALARPRODUCT_multipart_message *) message; |
1954 | uint32_t used_elements; | 1962 | uint32_t used_elements; |
1955 | uint32_t contained_elements=0; | 1963 | uint32_t contained_elements = 0; |
1956 | uint32_t msg_length; | 1964 | uint32_t msg_length; |
1957 | unsigned char * current; | 1965 | unsigned char * current; |
1958 | gcry_error_t rc; | 1966 | gcry_error_t rc; |
1959 | int32_t i = -1; | 1967 | int32_t i = -1; |
1960 | 1968 | ||
1961 | // are we in the correct state? | 1969 | // are we in the correct state? |
1962 | session = (struct ServiceSession *) * tunnel_ctx; | 1970 | session = (struct ServiceSession *) * tunnel_ctx; |
1963 | if ((BOB != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != session->state)) { | 1971 | if ((BOB != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != session->state)) { |
@@ -1970,28 +1978,28 @@ handle_service_request_multipart (void *cls, | |||
1970 | used_elements = session->used; | 1978 | used_elements = session->used; |
1971 | contained_elements = ntohl (msg->multipart_element_count); | 1979 | contained_elements = ntohl (msg->multipart_element_count); |
1972 | msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message) | 1980 | msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message) |
1973 | + contained_elements * PAILLIER_ELEMENT_LENGTH; | 1981 | +contained_elements * PAILLIER_ELEMENT_LENGTH; |
1974 | //sanity check | 1982 | //sanity check |
1975 | if (( ntohs (msg->header.size) != msg_length) | 1983 | if ((ntohs (msg->header.size) != msg_length) |
1976 | || (used_elements < contained_elements + session->transferred)) { | 1984 | || (used_elements < contained_elements + session->transferred)) { |
1977 | goto except; | 1985 | goto except; |
1978 | } | 1986 | } |
1979 | current = (unsigned char *) &msg[1]; | 1987 | current = (unsigned char *) &msg[1]; |
1980 | if (contained_elements != 0) { | 1988 | if (contained_elements != 0) { |
1981 | // Convert each vector element to MPI_value | 1989 | // Convert each vector element to MPI_value |
1982 | for (i = session->transferred; i < session->transferred+contained_elements; i++) { | 1990 | for (i = session->transferred; i < session->transferred + contained_elements; i++) { |
1983 | size_t read = 0; | 1991 | size_t read = 0; |
1984 | if (0 != (rc = gcry_mpi_scan (&session->a[i], | 1992 | if (0 != (rc = gcry_mpi_scan (&session->a[i], |
1985 | GCRYMPI_FMT_USG, | 1993 | GCRYMPI_FMT_USG, |
1986 | ¤t[i * PAILLIER_ELEMENT_LENGTH], | 1994 | ¤t[i * PAILLIER_ELEMENT_LENGTH], |
1987 | PAILLIER_ELEMENT_LENGTH, | 1995 | PAILLIER_ELEMENT_LENGTH, |
1988 | &read))) { | 1996 | &read))) { |
1989 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | 1997 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); |
1990 | goto except; | 1998 | goto except; |
1991 | } | 1999 | } |
1992 | } | 2000 | } |
1993 | session->transferred+=contained_elements; | 2001 | session->transferred += contained_elements; |
1994 | 2002 | ||
1995 | if (session->transferred == used_elements) { | 2003 | if (session->transferred == used_elements) { |
1996 | // single part finished | 2004 | // single part finished |
1997 | session->state = SERVICE_REQUEST_RECEIVED; | 2005 | session->state = SERVICE_REQUEST_RECEIVED; |
@@ -2005,11 +2013,11 @@ handle_service_request_multipart (void *cls, | |||
2005 | else | 2013 | else |
2006 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key)); | 2014 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key)); |
2007 | } | 2015 | } |
2008 | else{ | 2016 | else { |
2009 | // multipart message | 2017 | // multipart message |
2010 | } | 2018 | } |
2011 | } | 2019 | } |
2012 | 2020 | ||
2013 | return GNUNET_OK; | 2021 | return GNUNET_OK; |
2014 | except: | 2022 | except: |
2015 | // and notify our client-session that we could not complete the session | 2023 | // and notify our client-session that we could not complete the session |
@@ -2024,15 +2032,14 @@ except: | |||
2024 | return GNUNET_SYSERR; | 2032 | return GNUNET_SYSERR; |
2025 | } | 2033 | } |
2026 | 2034 | ||
2035 | |||
2027 | /** | 2036 | /** |
2028 | * Handle a request from another service to calculate a scalarproduct with us. | 2037 | * Handle a request from another service to calculate a scalarproduct with us. |
2029 | * | 2038 | * |
2030 | * @param cls closure (set from #GNUNET_MESH_connect) | 2039 | * @param cls closure (set from #GNUNET_MESH_connect) |
2031 | * @param tunnel connection to the other end | 2040 | * @param tunnel connection to the other end |
2032 | * @param tunnel_ctx place to store local state associated with the tunnel | 2041 | * @param tunnel_ctx place to store local state associated with the tunnel |
2033 | * @param sender who sent the message | ||
2034 | * @param message the actual message | 2042 | * @param message the actual message |
2035 | * @param atsi performance data for the connection | ||
2036 | * @return #GNUNET_OK to keep the connection open, | 2043 | * @return #GNUNET_OK to keep the connection open, |
2037 | * #GNUNET_SYSERR to close it (signal serious error) | 2044 | * #GNUNET_SYSERR to close it (signal serious error) |
2038 | */ | 2045 | */ |
@@ -2128,17 +2135,17 @@ handle_service_request (void *cls, | |||
2128 | &needed_state, NULL); | 2135 | &needed_state, NULL); |
2129 | 2136 | ||
2130 | session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements); | 2137 | session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements); |
2131 | session->state = WAITING_FOR_MULTIPART_TRANSMISSION; | 2138 | session->state = WAITING_FOR_MULTIPART_TRANSMISSION; |
2132 | GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session); | 2139 | GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session); |
2133 | if (contained_elements != 0) { | 2140 | if (contained_elements != 0) { |
2134 | // Convert each vector element to MPI_value | 2141 | // Convert each vector element to MPI_value |
2135 | for (i = 0; i < contained_elements; i++) { | 2142 | for (i = 0; i < contained_elements; i++) { |
2136 | size_t read = 0; | 2143 | size_t read = 0; |
2137 | if (0 != (rc = gcry_mpi_scan (&session->a[i], | 2144 | if (0 != (rc = gcry_mpi_scan (&session->a[i], |
2138 | GCRYMPI_FMT_USG, | 2145 | GCRYMPI_FMT_USG, |
2139 | ¤t[i * PAILLIER_ELEMENT_LENGTH], | 2146 | ¤t[i * PAILLIER_ELEMENT_LENGTH], |
2140 | PAILLIER_ELEMENT_LENGTH, | 2147 | PAILLIER_ELEMENT_LENGTH, |
2141 | &read))) { | 2148 | &read))) { |
2142 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | 2149 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); |
2143 | goto invalid_msg; | 2150 | goto invalid_msg; |
2144 | } | 2151 | } |
@@ -2156,7 +2163,7 @@ handle_service_request (void *cls, | |||
2156 | else | 2163 | else |
2157 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key)); | 2164 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key)); |
2158 | } | 2165 | } |
2159 | else{ | 2166 | else { |
2160 | // multipart message | 2167 | // multipart message |
2161 | } | 2168 | } |
2162 | } | 2169 | } |
@@ -2175,15 +2182,14 @@ invalid_msg: | |||
2175 | return GNUNET_SYSERR; | 2182 | return GNUNET_SYSERR; |
2176 | } | 2183 | } |
2177 | 2184 | ||
2185 | |||
2178 | /** | 2186 | /** |
2179 | * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with. | 2187 | * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with. |
2180 | * | 2188 | * |
2181 | * @param cls closure (set from #GNUNET_MESH_connect) | 2189 | * @param cls closure (set from #GNUNET_MESH_connect) |
2182 | * @param tunnel connection to the other end | 2190 | * @param tunnel connection to the other end |
2183 | * @param tunnel_ctx place to store local state associated with the tunnel | 2191 | * @param tunnel_ctx place to store local state associated with the tunnel |
2184 | * @param sender who sent the message | ||
2185 | * @param message the actual message | 2192 | * @param message the actual message |
2186 | * @param atsi performance data for the connection | ||
2187 | * @return #GNUNET_OK to keep the connection open, | 2193 | * @return #GNUNET_OK to keep the connection open, |
2188 | * #GNUNET_SYSERR to close it (signal serious error) | 2194 | * #GNUNET_SYSERR to close it (signal serious error) |
2189 | */ | 2195 | */ |
@@ -2198,7 +2204,7 @@ handle_service_response_multipart (void *cls, | |||
2198 | unsigned char * current; | 2204 | unsigned char * current; |
2199 | size_t read; | 2205 | size_t read; |
2200 | size_t i; | 2206 | size_t i; |
2201 | uint32_t contained=0; | 2207 | uint32_t contained = 0; |
2202 | size_t msg_size; | 2208 | size_t msg_size; |
2203 | int rc; | 2209 | int rc; |
2204 | 2210 | ||
@@ -2241,7 +2247,7 @@ handle_service_response_multipart (void *cls, | |||
2241 | session->state = SERVICE_RESPONSE_RECEIVED; | 2247 | session->state = SERVICE_RESPONSE_RECEIVED; |
2242 | session->product = compute_scalar_product (session); | 2248 | session->product = compute_scalar_product (session); |
2243 | return GNUNET_SYSERR; // terminate the tunnel right away, we are done here! | 2249 | return GNUNET_SYSERR; // terminate the tunnel right away, we are done here! |
2244 | 2250 | ||
2245 | invalid_msg: | 2251 | invalid_msg: |
2246 | GNUNET_break_op (0); | 2252 | GNUNET_break_op (0); |
2247 | free_session_variables (session); | 2253 | free_session_variables (session); |
@@ -2249,24 +2255,23 @@ invalid_msg: | |||
2249 | session->tunnel = NULL; | 2255 | session->tunnel = NULL; |
2250 | // send message with product to client | 2256 | // send message with product to client |
2251 | if (ALICE == session->role) | 2257 | if (ALICE == session->role) |
2252 | session->client_notification_task = | 2258 | session->client_notification_task = |
2253 | GNUNET_SCHEDULER_add_now (&prepare_client_response, | 2259 | GNUNET_SCHEDULER_add_now (&prepare_client_response, |
2254 | session); | 2260 | session); |
2255 | // the tunnel has done its job, terminate our connection and the tunnel | 2261 | // the tunnel has done its job, terminate our connection and the tunnel |
2256 | // the peer will be notified that the tunnel was destroyed via tunnel_destruction_handler | 2262 | // the peer will be notified that the tunnel was destroyed via tunnel_destruction_handler |
2257 | // just close the connection, as recommended by Christian | 2263 | // just close the connection, as recommended by Christian |
2258 | return GNUNET_SYSERR; | 2264 | return GNUNET_SYSERR; |
2259 | } | 2265 | } |
2260 | 2266 | ||
2267 | |||
2261 | /** | 2268 | /** |
2262 | * Handle a response we got from another service we wanted to calculate a scalarproduct with. | 2269 | * Handle a response we got from another service we wanted to calculate a scalarproduct with. |
2263 | * | 2270 | * |
2264 | * @param cls closure (set from #GNUNET_MESH_connect) | 2271 | * @param cls closure (set from #GNUNET_MESH_connect) |
2265 | * @param tunnel connection to the other end | 2272 | * @param tunnel connection to the other end |
2266 | * @param tunnel_ctx place to store local state associated with the tunnel | 2273 | * @param tunnel_ctx place to store local state associated with the tunnel |
2267 | * @param sender who sent the message | ||
2268 | * @param message the actual message | 2274 | * @param message the actual message |
2269 | * @param atsi performance data for the connection | ||
2270 | * @return #GNUNET_OK to keep the connection open, | 2275 | * @return #GNUNET_OK to keep the connection open, |
2271 | * #GNUNET_SYSERR to close it (we are done) | 2276 | * #GNUNET_SYSERR to close it (we are done) |
2272 | */ | 2277 | */ |
@@ -2281,7 +2286,7 @@ handle_service_response (void *cls, | |||
2281 | unsigned char * current; | 2286 | unsigned char * current; |
2282 | size_t read; | 2287 | size_t read; |
2283 | size_t i; | 2288 | size_t i; |
2284 | uint32_t contained=0; | 2289 | uint32_t contained = 0; |
2285 | size_t msg_size; | 2290 | size_t msg_size; |
2286 | int rc; | 2291 | int rc; |
2287 | 2292 | ||
@@ -2339,7 +2344,7 @@ handle_service_response (void *cls, | |||
2339 | } | 2344 | } |
2340 | if (session->transferred != session->used) | 2345 | if (session->transferred != session->used) |
2341 | return GNUNET_OK; //wait for the other multipart chunks | 2346 | return GNUNET_OK; //wait for the other multipart chunks |
2342 | 2347 | ||
2343 | session->state = SERVICE_RESPONSE_RECEIVED; | 2348 | session->state = SERVICE_RESPONSE_RECEIVED; |
2344 | session->product = compute_scalar_product (session); | 2349 | session->product = compute_scalar_product (session); |
2345 | return GNUNET_SYSERR; // terminate the tunnel right away, we are done here! | 2350 | return GNUNET_SYSERR; // terminate the tunnel right away, we are done here! |
@@ -2360,6 +2365,7 @@ invalid_msg: | |||
2360 | return GNUNET_SYSERR; | 2365 | return GNUNET_SYSERR; |
2361 | } | 2366 | } |
2362 | 2367 | ||
2368 | |||
2363 | /** | 2369 | /** |
2364 | * Task run during shutdown. | 2370 | * Task run during shutdown. |
2365 | * | 2371 | * |
@@ -2406,6 +2412,7 @@ shutdown_task (void *cls, | |||
2406 | } | 2412 | } |
2407 | } | 2413 | } |
2408 | 2414 | ||
2415 | |||
2409 | /** | 2416 | /** |
2410 | * Initialization of the program and message handlers | 2417 | * Initialization of the program and message handlers |
2411 | * | 2418 | * |
@@ -2460,6 +2467,7 @@ run (void *cls, | |||
2460 | NULL); | 2467 | NULL); |
2461 | } | 2468 | } |
2462 | 2469 | ||
2470 | |||
2463 | /** | 2471 | /** |
2464 | * The main function for the scalarproduct service. | 2472 | * The main function for the scalarproduct service. |
2465 | * | 2473 | * |