aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct/gnunet-service-scalarproduct_alice.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-09-20 10:20:46 +0000
committerChristian Grothoff <christian@grothoff.org>2016-09-20 10:20:46 +0000
commit7e835e78aa82869715c9aee96a73ba40aad3f39f (patch)
tree000078e5b3f0b48a8f473df30b187a170b7c29b3 /src/scalarproduct/gnunet-service-scalarproduct_alice.c
parenta9f53345421cddea37be81e59d6fb56a9f095d6c (diff)
downloadgnunet-7e835e78aa82869715c9aee96a73ba40aad3f39f.tar.gz
gnunet-7e835e78aa82869715c9aee96a73ba40aad3f39f.zip
convert non-ecc scalarproducts to new service API
Diffstat (limited to 'src/scalarproduct/gnunet-service-scalarproduct_alice.c')
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct_alice.c274
1 files changed, 136 insertions, 138 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct_alice.c b/src/scalarproduct/gnunet-service-scalarproduct_alice.c
index 51bfcb80f..45d1f4e2c 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct_alice.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct_alice.c
@@ -77,7 +77,7 @@ struct AliceServiceSession
77 /** 77 /**
78 * The client this request is related to. 78 * The client this request is related to.
79 */ 79 */
80 struct GNUNET_SERVER_Client *client; 80 struct GNUNET_SERVICE_Client *client;
81 81
82 /** 82 /**
83 * The message queue for the client. 83 * The message queue for the client.
@@ -248,11 +248,6 @@ destroy_service_session (struct AliceServiceSession *s)
248 if (GNUNET_YES == s->in_destroy) 248 if (GNUNET_YES == s->in_destroy)
249 return; 249 return;
250 s->in_destroy = GNUNET_YES; 250 s->in_destroy = GNUNET_YES;
251 if (NULL != s->client_mq)
252 {
253 GNUNET_MQ_destroy (s->client_mq);
254 s->client_mq = NULL;
255 }
256 if (NULL != s->cadet_mq) 251 if (NULL != s->cadet_mq)
257 { 252 {
258 GNUNET_MQ_destroy (s->cadet_mq); 253 GNUNET_MQ_destroy (s->cadet_mq);
@@ -260,10 +255,10 @@ destroy_service_session (struct AliceServiceSession *s)
260 } 255 }
261 if (NULL != s->client) 256 if (NULL != s->client)
262 { 257 {
263 GNUNET_SERVER_client_set_user_context (s->client, 258 struct GNUNET_SERVICE_Client *c = s->client;
264 NULL); 259
265 GNUNET_SERVER_client_disconnect (s->client);
266 s->client = NULL; 260 s->client = NULL;
261 GNUNET_SERVICE_client_drop (c);
267 } 262 }
268 if (NULL != s->channel) 263 if (NULL != s->channel)
269 { 264 {
@@ -1115,48 +1110,23 @@ client_request_complete_alice (struct AliceServiceSession *s)
1115 1110
1116 1111
1117/** 1112/**
1118 * We're receiving additional set data. Add it to our 1113 * We're receiving additional set data. Check if
1119 * set and if we are done, initiate the transaction. 1114 * @a msg is well-formed.
1120 * 1115 *
1121 * @param cls closure 1116 * @param cls client identification of the client
1122 * @param client identification of the client 1117 * @param msg the actual message
1123 * @param message the actual message 1118 * @return #GNUNET_OK if @a msg is well-formed
1124 */ 1119 */
1125static void 1120static int
1126GSS_handle_alice_client_message_multipart (void *cls, 1121check_alice_client_message_multipart (void *cls,
1127 struct GNUNET_SERVER_Client *client, 1122 const struct ComputationBobCryptodataMultipartMessage *msg)
1128 const struct GNUNET_MessageHeader *message)
1129{ 1123{
1130 const struct ComputationBobCryptodataMultipartMessage * msg; 1124 struct AliceServiceSession *s = cls;
1131 struct AliceServiceSession *s;
1132 uint32_t contained_count; 1125 uint32_t contained_count;
1133 const struct GNUNET_SCALARPRODUCT_Element *elements;
1134 uint32_t i;
1135 uint16_t msize; 1126 uint16_t msize;
1136 struct GNUNET_SET_Element set_elem;
1137 struct GNUNET_SCALARPRODUCT_Element *elem;
1138 1127
1139 s = GNUNET_SERVER_client_get_user_context (client, 1128 msize = ntohs (msg->header.size);
1140 struct AliceServiceSession);
1141 if (NULL == s)
1142 {
1143 /* session needs to already exist */
1144 GNUNET_break (0);
1145 GNUNET_SERVER_receive_done (client,
1146 GNUNET_SYSERR);
1147 return;
1148 }
1149 msize = ntohs (message->size);
1150 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
1151 {
1152 GNUNET_break (0);
1153 GNUNET_SERVER_receive_done (client,
1154 GNUNET_SYSERR);
1155 return;
1156 }
1157 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
1158 contained_count = ntohl (msg->element_count_contained); 1129 contained_count = ntohl (msg->element_count_contained);
1159
1160 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) + 1130 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
1161 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) || 1131 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
1162 (0 == contained_count) || 1132 (0 == contained_count) ||
@@ -1164,18 +1134,38 @@ GSS_handle_alice_client_message_multipart (void *cls,
1164 (s->total < s->client_received_element_count + contained_count) ) 1134 (s->total < s->client_received_element_count + contained_count) )
1165 { 1135 {
1166 GNUNET_break_op (0); 1136 GNUNET_break_op (0);
1167 GNUNET_SERVER_receive_done (client, 1137 return GNUNET_SYSERR;
1168 GNUNET_SYSERR);
1169 return;
1170 } 1138 }
1139 return GNUNET_OK;
1140}
1141
1142
1143/**
1144 * We're receiving additional set data. Add it to our
1145 * set and if we are done, initiate the transaction.
1146 *
1147 * @param cls client identification of the client
1148 * @param msg the actual message
1149 */
1150static void
1151handle_alice_client_message_multipart (void *cls,
1152 const struct ComputationBobCryptodataMultipartMessage *msg)
1153{
1154 struct AliceServiceSession *s = cls;
1155 uint32_t contained_count;
1156 const struct GNUNET_SCALARPRODUCT_Element *elements;
1157 struct GNUNET_SET_Element set_elem;
1158 struct GNUNET_SCALARPRODUCT_Element *elem;
1159
1160 contained_count = ntohl (msg->element_count_contained);
1171 s->client_received_element_count += contained_count; 1161 s->client_received_element_count += contained_count;
1172 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1]; 1162 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1173 for (i = 0; i < contained_count; i++) 1163 for (uint32_t i = 0; i < contained_count; i++)
1174 { 1164 {
1175 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element); 1165 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1176 GNUNET_memcpy (elem, 1166 GNUNET_memcpy (elem,
1177 &elements[i], 1167 &elements[i],
1178 sizeof (struct GNUNET_SCALARPRODUCT_Element)); 1168 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1179 if (GNUNET_SYSERR == 1169 if (GNUNET_SYSERR ==
1180 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements, 1170 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1181 &elem->key, 1171 &elem->key,
@@ -1194,8 +1184,7 @@ GSS_handle_alice_client_message_multipart (void *cls,
1194 NULL, NULL); 1184 NULL, NULL);
1195 s->used_element_count++; 1185 s->used_element_count++;
1196 } 1186 }
1197 GNUNET_SERVER_receive_done (client, 1187 GNUNET_SERVICE_client_continue (s->client);
1198 GNUNET_OK);
1199 if (s->total != s->client_received_element_count) 1188 if (s->total != s->client_received_element_count)
1200 { 1189 {
1201 /* more to come */ 1190 /* more to come */
@@ -1207,45 +1196,29 @@ GSS_handle_alice_client_message_multipart (void *cls,
1207 1196
1208/** 1197/**
1209 * Handler for Alice's client request message. 1198 * Handler for Alice's client request message.
1210 * We are doing request-initiation to compute a scalar product with a peer. 1199 * Check that @a msg is well-formed.
1211 * 1200 *
1212 * @param cls closure 1201 * @param cls identification of the client
1213 * @param client identification of the client 1202 * @param msg the actual message
1214 * @param message the actual message 1203 * @return #GNUNET_OK if @a msg is well-formed
1215 */ 1204 */
1216static void 1205static int
1217GSS_handle_alice_client_message (void *cls, 1206check_alice_client_message (void *cls,
1218 struct GNUNET_SERVER_Client *client, 1207 const struct AliceComputationMessage *msg)
1219 const struct GNUNET_MessageHeader *message)
1220{ 1208{
1221 const struct AliceComputationMessage *msg; 1209 struct AliceServiceSession *s = cls;
1222 struct AliceServiceSession *s;
1223 uint32_t contained_count;
1224 uint32_t total_count;
1225 const struct GNUNET_SCALARPRODUCT_Element *elements;
1226 uint32_t i;
1227 uint16_t msize; 1210 uint16_t msize;
1228 struct GNUNET_SET_Element set_elem; 1211 uint32_t total_count;
1229 struct GNUNET_SCALARPRODUCT_Element *elem; 1212 uint32_t contained_count;
1230 1213
1231 s = GNUNET_SERVER_client_get_user_context (client, 1214 if (NULL != s->intersected_elements)
1232 struct AliceServiceSession);
1233 if (NULL != s)
1234 { 1215 {
1235 /* only one concurrent session per client connection allowed, 1216 /* only one concurrent session per client connection allowed,
1236 simplifies logic a lot... */ 1217 simplifies logic a lot... */
1237 GNUNET_break (0); 1218 GNUNET_break (0);
1238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1219 return GNUNET_SYSERR;
1239 return;
1240 }
1241 msize = ntohs (message->size);
1242 if (msize < sizeof (struct AliceComputationMessage))
1243 {
1244 GNUNET_break (0);
1245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1246 return;
1247 } 1220 }
1248 msg = (const struct AliceComputationMessage *) message; 1221 msize = ntohs (msg->header.size);
1249 total_count = ntohl (msg->element_count_total); 1222 total_count = ntohl (msg->element_count_total);
1250 contained_count = ntohl (msg->element_count_contained); 1223 contained_count = ntohl (msg->element_count_contained);
1251 if ( (0 == total_count) || 1224 if ( (0 == total_count) ||
@@ -1254,15 +1227,34 @@ GSS_handle_alice_client_message (void *cls,
1254 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ) 1227 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1255 { 1228 {
1256 GNUNET_break_op (0); 1229 GNUNET_break_op (0);
1257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1230 return GNUNET_SYSERR;
1258 return;
1259 } 1231 }
1232 return GNUNET_OK;
1233}
1260 1234
1261 s = GNUNET_new (struct AliceServiceSession); 1235
1236/**
1237 * Handler for Alice's client request message.
1238 * We are doing request-initiation to compute a scalar product with a peer.
1239 *
1240 * @param cls identification of the client
1241 * @param msg the actual message
1242 */
1243static void
1244handle_alice_client_message (void *cls,
1245 const struct AliceComputationMessage *msg)
1246{
1247 struct AliceServiceSession *s = cls;
1248 uint32_t contained_count;
1249 uint32_t total_count;
1250 const struct GNUNET_SCALARPRODUCT_Element *elements;
1251 struct GNUNET_SET_Element set_elem;
1252 struct GNUNET_SCALARPRODUCT_Element *elem;
1253
1254 total_count = ntohl (msg->element_count_total);
1255 contained_count = ntohl (msg->element_count_contained);
1262 s->peer = msg->peer; 1256 s->peer = msg->peer;
1263 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE; 1257 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1264 s->client = client;
1265 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1266 s->total = total_count; 1258 s->total = total_count;
1267 s->client_received_element_count = contained_count; 1259 s->client_received_element_count = contained_count;
1268 s->session_id = msg->session_key; 1260 s->session_id = msg->session_key;
@@ -1271,7 +1263,8 @@ GSS_handle_alice_client_message (void *cls,
1271 GNUNET_YES); 1263 GNUNET_YES);
1272 s->intersection_set = GNUNET_SET_create (cfg, 1264 s->intersection_set = GNUNET_SET_create (cfg,
1273 GNUNET_SET_OPERATION_INTERSECTION); 1265 GNUNET_SET_OPERATION_INTERSECTION);
1274 for (i = 0; i < contained_count; i++) 1266
1267 for (uint32_t i = 0; i < contained_count; i++)
1275 { 1268 {
1276 if (0 == GNUNET_ntohll (elements[i].value)) 1269 if (0 == GNUNET_ntohll (elements[i].value))
1277 continue; 1270 continue;
@@ -1298,10 +1291,7 @@ GSS_handle_alice_client_message (void *cls,
1298 NULL, NULL); 1291 NULL, NULL);
1299 s->used_element_count++; 1292 s->used_element_count++;
1300 } 1293 }
1301 GNUNET_SERVER_client_set_user_context (client, 1294 GNUNET_SERVICE_client_continue (s->client);
1302 s);
1303 GNUNET_SERVER_receive_done (client,
1304 GNUNET_OK);
1305 if (s->total != s->client_received_element_count) 1295 if (s->total != s->client_received_element_count)
1306 { 1296 {
1307 /* wait for multipart msg */ 1297 /* wait for multipart msg */
@@ -1331,6 +1321,30 @@ shutdown_task (void *cls)
1331 1321
1332 1322
1333/** 1323/**
1324 * A client connected.
1325 *
1326 * Setup the associated data structure.
1327 *
1328 * @param cls closure, NULL
1329 * @param client identification of the client
1330 * @param mq message queue to communicate with @a client
1331 * @return our `struct AliceServiceSession`
1332 */
1333static void *
1334client_connect_cb (void *cls,
1335 struct GNUNET_SERVICE_Client *client,
1336 struct GNUNET_MQ_Handle *mq)
1337{
1338 struct AliceServiceSession *s;
1339
1340 s = GNUNET_new (struct AliceServiceSession);
1341 s->client = client;
1342 s->client_mq = mq;
1343 return s;
1344}
1345
1346
1347/**
1334 * A client disconnected. 1348 * A client disconnected.
1335 * 1349 *
1336 * Remove the associated session(s), release data structures 1350 * Remove the associated session(s), release data structures
@@ -1338,25 +1352,20 @@ shutdown_task (void *cls)
1338 * 1352 *
1339 * @param cls closure, NULL 1353 * @param cls closure, NULL
1340 * @param client identification of the client 1354 * @param client identification of the client
1355 * @param app_cls our `struct AliceServiceSession`
1341 */ 1356 */
1342static void 1357static void
1343handle_client_disconnect (void *cls, 1358client_disconnect_cb (void *cls,
1344 struct GNUNET_SERVER_Client *client) 1359 struct GNUNET_SERVICE_Client *client,
1360 void *app_cls)
1345{ 1361{
1346 struct AliceServiceSession *s; 1362 struct AliceServiceSession *s = app_cls;
1347 1363
1348 if (NULL == client)
1349 return;
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Client %p disconnected from us.\n", 1365 "Client %p disconnected from us.\n",
1352 client); 1366 client);
1353 s = GNUNET_SERVER_client_get_user_context (client,
1354 struct AliceServiceSession);
1355 if (NULL == s)
1356 return;
1357 s->client = NULL; 1367 s->client = NULL;
1358 GNUNET_SERVER_client_set_user_context (client, 1368 s->client_mq = NULL;
1359 NULL);
1360 destroy_service_session (s); 1369 destroy_service_session (s);
1361} 1370}
1362 1371
@@ -1365,13 +1374,13 @@ handle_client_disconnect (void *cls,
1365 * Initialization of the program and message handlers 1374 * Initialization of the program and message handlers
1366 * 1375 *
1367 * @param cls closure 1376 * @param cls closure
1368 * @param server the initialized server
1369 * @param c configuration to use 1377 * @param c configuration to use
1378 * @param service the initialized service
1370 */ 1379 */
1371static void 1380static void
1372run (void *cls, 1381run (void *cls,
1373 struct GNUNET_SERVER_Handle *server, 1382 const struct GNUNET_CONFIGURATION_Handle *c,
1374 const struct GNUNET_CONFIGURATION_Handle *c) 1383 struct GNUNET_SERVICE_Handle *service)
1375{ 1384{
1376 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = { 1385 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1377 { &handle_bobs_cryptodata_message, 1386 { &handle_bobs_cryptodata_message,
@@ -1382,15 +1391,6 @@ run (void *cls,
1382 0}, 1391 0},
1383 { NULL, 0, 0} 1392 { NULL, 0, 0}
1384 }; 1393 };
1385 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1386 { &GSS_handle_alice_client_message, NULL,
1387 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1388 0},
1389 { &GSS_handle_alice_client_message_multipart, NULL,
1390 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE,
1391 0},
1392 { NULL, NULL, 0, 0}
1393 };
1394 1394
1395 cfg = c; 1395 cfg = c;
1396 /* 1396 /*
@@ -1403,14 +1403,12 @@ run (void *cls,
1403 1403
1404 GNUNET_CRYPTO_paillier_create (&my_pubkey, 1404 GNUNET_CRYPTO_paillier_create (&my_pubkey,
1405 &my_privkey); 1405 &my_privkey);
1406 GNUNET_SERVER_add_handlers (server, 1406 my_cadet = GNUNET_CADET_connect (cfg,
1407 server_handlers); 1407 NULL,
1408 GNUNET_SERVER_disconnect_notify (server,
1409 &handle_client_disconnect,
1410 NULL);
1411 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1412 &cb_channel_destruction, 1408 &cb_channel_destruction,
1413 cadet_handlers); 1409 cadet_handlers);
1410 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1411 NULL);
1414 if (NULL == my_cadet) 1412 if (NULL == my_cadet)
1415 { 1413 {
1416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1418,28 +1416,28 @@ run (void *cls,
1418 GNUNET_SCHEDULER_shutdown (); 1416 GNUNET_SCHEDULER_shutdown ();
1419 return; 1417 return;
1420 } 1418 }
1421 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1422 NULL);
1423
1424} 1419}
1425 1420
1426 1421
1427/** 1422/**
1428 * The main function for the scalarproduct service. 1423 * Define "main" method using service macro.
1429 *
1430 * @param argc number of arguments from the command line
1431 * @param argv command line arguments
1432 * @return 0 ok, 1 on error
1433 */ 1424 */
1434int 1425GNUNET_SERVICE_MAIN
1435main (int argc, 1426("scalarproduct-alice",
1436 char *const *argv) 1427 GNUNET_SERVICE_OPTION_NONE,
1437{ 1428 &run,
1438 return (GNUNET_OK == 1429 &client_connect_cb,
1439 GNUNET_SERVICE_run (argc, argv, 1430 &client_disconnect_cb,
1440 "scalarproduct-alice", 1431 NULL,
1441 GNUNET_SERVICE_OPTION_NONE, 1432 GNUNET_MQ_hd_var_size (alice_client_message,
1442 &run, NULL)) ? 0 : 1; 1433 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1443} 1434 struct AliceComputationMessage,
1435 NULL),
1436 GNUNET_MQ_hd_var_size (alice_client_message_multipart,
1437 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE,
1438 struct ComputationBobCryptodataMultipartMessage,
1439 NULL),
1440 GNUNET_MQ_handler_end ());
1441
1444 1442
1445/* end of gnunet-service-scalarproduct_alice.c */ 1443/* end of gnunet-service-scalarproduct_alice.c */