summaryrefslogtreecommitdiff
path: root/src/scalarproduct
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-09-20 09:55:33 +0000
committerChristian Grothoff <christian@grothoff.org>2016-09-20 09:55:33 +0000
commitc284a3bb84a91678707bbae3c93106bd4e84226e (patch)
tree94c1e6c346d391511d14390486df820bb3557b3c /src/scalarproduct
parentdca2051799b683ced76973359950dbc2e624d56e (diff)
downloadgnunet-c284a3bb84a91678707bbae3c93106bd4e84226e.tar.gz
gnunet-c284a3bb84a91678707bbae3c93106bd4e84226e.zip
converting scalarproduct ecc-bob to new service api
Diffstat (limited to 'src/scalarproduct')
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c294
1 files changed, 142 insertions, 152 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c b/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c
index 7942c0b3d..f873b0e05 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c
@@ -79,7 +79,7 @@ struct BobServiceSession
79 /** 79 /**
80 * The client this request is related to. 80 * The client this request is related to.
81 */ 81 */
82 struct GNUNET_SERVER_Client *client; 82 struct GNUNET_SERVICE_Client *client;
83 83
84 /** 84 /**
85 * Client message queue. 85 * Client message queue.
@@ -241,7 +241,6 @@ static struct GNUNET_CADET_Handle *my_cadet;
241static struct GNUNET_CRYPTO_EccDlogContext *edc; 241static struct GNUNET_CRYPTO_EccDlogContext *edc;
242 242
243 243
244
245/** 244/**
246 * Finds a not terminated client session in the respective map based on 245 * Finds a not terminated client session in the respective map based on
247 * session key. 246 * session key.
@@ -318,14 +317,9 @@ destroy_service_session (struct BobServiceSession *s)
318 s->cadet = NULL; 317 s->cadet = NULL;
319 destroy_cadet_session (in); 318 destroy_cadet_session (in);
320 } 319 }
321 if (NULL != s->client_mq)
322 {
323 GNUNET_MQ_destroy (s->client_mq);
324 s->client_mq = NULL;
325 }
326 if (NULL != s->client) 320 if (NULL != s->client)
327 { 321 {
328 GNUNET_SERVER_client_disconnect (s->client); 322 GNUNET_SERVICE_client_drop (s->client);
329 s->client = NULL; 323 s->client = NULL;
330 } 324 }
331 GNUNET_assert (GNUNET_YES == 325 GNUNET_assert (GNUNET_YES ==
@@ -919,48 +913,22 @@ cb_channel_incoming (void *cls,
919 913
920 914
921/** 915/**
922 * We're receiving additional set data. Add it to our 916 * We're receiving additional set data. Check it is well-formed.
923 * set and if we are done, initiate the transaction.
924 * 917 *
925 * @param cls closure 918 * @param cls identification of the client
926 * @param client identification of the client 919 * @param msg the actual message
927 * @param message the actual message 920 * @return #GNUNET_OK if @a msg is well-formed
928 */ 921 */
929static void 922static int
930GSS_handle_bob_client_message_multipart (void *cls, 923check_bob_client_message_multipart (void *cls,
931 struct GNUNET_SERVER_Client *client, 924 const struct ComputationBobCryptodataMultipartMessage *msg)
932 const struct GNUNET_MessageHeader *message)
933{ 925{
934 const struct ComputationBobCryptodataMultipartMessage * msg; 926 struct BobServiceSession *s = cls;
935 struct BobServiceSession *s;
936 uint32_t contained_count; 927 uint32_t contained_count;
937 const struct GNUNET_SCALARPRODUCT_Element *elements;
938 uint32_t i;
939 uint16_t msize; 928 uint16_t msize;
940 struct GNUNET_SET_Element set_elem;
941 struct GNUNET_SCALARPRODUCT_Element *elem;
942 929
943 s = GNUNET_SERVER_client_get_user_context (client, 930 msize = ntohs (msg->header.size);
944 struct BobServiceSession);
945 if (NULL == s)
946 {
947 /* session needs to already exist */
948 GNUNET_break (0);
949 GNUNET_SERVER_receive_done (client,
950 GNUNET_SYSERR);
951 return;
952 }
953 msize = ntohs (message->size);
954 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
955 {
956 GNUNET_break (0);
957 GNUNET_SERVER_receive_done (client,
958 GNUNET_SYSERR);
959 return;
960 }
961 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
962 contained_count = ntohl (msg->element_count_contained); 931 contained_count = ntohl (msg->element_count_contained);
963
964 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) + 932 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
965 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) || 933 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
966 (0 == contained_count) || 934 (0 == contained_count) ||
@@ -968,18 +936,38 @@ GSS_handle_bob_client_message_multipart (void *cls,
968 (s->total == s->client_received_element_count) || 936 (s->total == s->client_received_element_count) ||
969 (s->total < s->client_received_element_count + contained_count) ) 937 (s->total < s->client_received_element_count + contained_count) )
970 { 938 {
971 GNUNET_break_op (0); 939 GNUNET_break (0);
972 GNUNET_SERVER_receive_done (client, 940 return GNUNET_SYSERR;
973 GNUNET_SYSERR);
974 return;
975 } 941 }
942 return GNUNET_OK;
943}
944
945
946/**
947 * We're receiving additional set data. Add it to our
948 * set and if we are done, initiate the transaction.
949 *
950 * @param cls identification of the client
951 * @param msg the actual message
952 */
953static void
954handle_bob_client_message_multipart (void *cls,
955 const struct ComputationBobCryptodataMultipartMessage *msg)
956{
957 struct BobServiceSession *s = cls;
958 uint32_t contained_count;
959 const struct GNUNET_SCALARPRODUCT_Element *elements;
960 struct GNUNET_SET_Element set_elem;
961 struct GNUNET_SCALARPRODUCT_Element *elem;
962
963 contained_count = ntohl (msg->element_count_contained);
976 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1]; 964 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
977 for (i = 0; i < contained_count; i++) 965 for (uint32_t i = 0; i < contained_count; i++)
978 { 966 {
979 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element); 967 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
980 GNUNET_memcpy (elem, 968 GNUNET_memcpy (elem,
981 &elements[i], 969 &elements[i],
982 sizeof (struct GNUNET_SCALARPRODUCT_Element)); 970 sizeof (struct GNUNET_SCALARPRODUCT_Element));
983 if (GNUNET_SYSERR == 971 if (GNUNET_SYSERR ==
984 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements, 972 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
985 &elem->key, 973 &elem->key,
@@ -998,8 +986,7 @@ GSS_handle_bob_client_message_multipart (void *cls,
998 NULL, NULL); 986 NULL, NULL);
999 } 987 }
1000 s->client_received_element_count += contained_count; 988 s->client_received_element_count += contained_count;
1001 GNUNET_SERVER_receive_done (client, 989 GNUNET_SERVICE_client_continue (s->client);
1002 GNUNET_OK);
1003 if (s->total != s->client_received_element_count) 990 if (s->total != s->client_received_element_count)
1004 { 991 {
1005 /* more to come */ 992 /* more to come */
@@ -1015,50 +1002,28 @@ GSS_handle_bob_client_message_multipart (void *cls,
1015 1002
1016 1003
1017/** 1004/**
1018 * Handler for Bob's a client request message. Bob is in the response 1005 * Handler for Bob's a client request message. Check @a msg is
1019 * role, keep the values + session and waiting for a matching session 1006 * well-formed.
1020 * or process a waiting request from Alice.
1021 * 1007 *
1022 * @param cls closure 1008 * @param cls identification of the client
1023 * @param client identification of the client 1009 * @param msg the actual message
1024 * @param message the actual message 1010 * @return #GNUNET_OK if @a msg is well-formed
1025 */ 1011 */
1026static void 1012static int
1027GSS_handle_bob_client_message (void *cls, 1013check_bob_client_message (void *cls,
1028 struct GNUNET_SERVER_Client *client, 1014 const struct BobComputationMessage *msg)
1029 const struct GNUNET_MessageHeader *message)
1030{ 1015{
1031 const struct BobComputationMessage *msg; 1016 struct BobServiceSession *s = cls;
1032 struct BobServiceSession *s;
1033 struct CadetIncomingSession *in;
1034 uint32_t contained_count; 1017 uint32_t contained_count;
1035 uint32_t total_count; 1018 uint32_t total_count;
1036 const struct GNUNET_SCALARPRODUCT_Element *elements;
1037 uint32_t i;
1038 struct GNUNET_SET_Element set_elem;
1039 struct GNUNET_SCALARPRODUCT_Element *elem;
1040 uint16_t msize; 1019 uint16_t msize;
1041 1020
1042 s = GNUNET_SERVER_client_get_user_context (client, 1021 if (GNUNET_SCALARPRODUCT_STATUS_INIT != s->status)
1043 struct BobServiceSession);
1044 if (NULL != s)
1045 {
1046 /* only one concurrent session per client connection allowed,
1047 simplifies logic a lot... */
1048 GNUNET_break (0);
1049 GNUNET_SERVER_receive_done (client,
1050 GNUNET_SYSERR);
1051 return;
1052 }
1053 msize = ntohs (message->size);
1054 if (msize < sizeof (struct BobComputationMessage))
1055 { 1022 {
1056 GNUNET_break (0); 1023 GNUNET_break (0);
1057 GNUNET_SERVER_receive_done (client, 1024 return GNUNET_SYSERR;
1058 GNUNET_SYSERR);
1059 return;
1060 } 1025 }
1061 msg = (const struct BobComputationMessage *) message; 1026 msize = ntohs (msg->header.size);
1062 total_count = ntohl (msg->element_count_total); 1027 total_count = ntohl (msg->element_count_total);
1063 contained_count = ntohl (msg->element_count_contained); 1028 contained_count = ntohl (msg->element_count_contained);
1064 if ( (0 == total_count) || 1029 if ( (0 == total_count) ||
@@ -1068,22 +1033,41 @@ GSS_handle_bob_client_message (void *cls,
1068 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ) 1033 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1069 { 1034 {
1070 GNUNET_break_op (0); 1035 GNUNET_break_op (0);
1071 GNUNET_SERVER_receive_done (client, 1036 return GNUNET_SYSERR;
1072 GNUNET_SYSERR);
1073 return;
1074 } 1037 }
1075 if (NULL != find_matching_client_session (&msg->session_key)) 1038 if (NULL != find_matching_client_session (&msg->session_key))
1076 { 1039 {
1077 GNUNET_break (0); 1040 GNUNET_break (0);
1078 GNUNET_SERVER_receive_done (client, 1041 return GNUNET_SYSERR;
1079 GNUNET_SYSERR);
1080 return;
1081 } 1042 }
1043 return GNUNET_OK;
1044}
1045
1046
1047/**
1048 * Handler for Bob's a client request message. Bob is in the response
1049 * role, keep the values + session and waiting for a matching session
1050 * or process a waiting request from Alice.
1051 *
1052 * @param cls identification of the client
1053 * @param msg the actual message
1054 */
1055static void
1056handle_bob_client_message (void *cls,
1057 const struct BobComputationMessage *msg)
1058{
1059 struct BobServiceSession *s = cls;
1060 struct CadetIncomingSession *in;
1061 uint32_t contained_count;
1062 uint32_t total_count;
1063 const struct GNUNET_SCALARPRODUCT_Element *elements;
1064 struct GNUNET_SET_Element set_elem;
1065 struct GNUNET_SCALARPRODUCT_Element *elem;
1066
1067 total_count = ntohl (msg->element_count_total);
1068 contained_count = ntohl (msg->element_count_contained);
1082 1069
1083 s = GNUNET_new (struct BobServiceSession);
1084 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE; 1070 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1085 s->client = client;
1086 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1087 s->total = total_count; 1071 s->total = total_count;
1088 s->client_received_element_count = contained_count; 1072 s->client_received_element_count = contained_count;
1089 s->session_id = msg->session_key; 1073 s->session_id = msg->session_key;
@@ -1094,9 +1078,7 @@ GSS_handle_bob_client_message (void *cls,
1094 if (NULL == s->port) 1078 if (NULL == s->port)
1095 { 1079 {
1096 GNUNET_break (0); 1080 GNUNET_break (0);
1097 GNUNET_SERVER_receive_done (client, 1081 GNUNET_SERVICE_client_drop (s->client);
1098 GNUNET_SYSERR);
1099 GNUNET_free (s);
1100 return; 1082 return;
1101 } 1083 }
1102 GNUNET_break (GNUNET_YES == 1084 GNUNET_break (GNUNET_YES ==
@@ -1105,11 +1087,13 @@ GSS_handle_bob_client_message (void *cls,
1105 s, 1087 s,
1106 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 1088 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1107 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1]; 1089 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1108 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total, 1090 s->intersected_elements
1109 GNUNET_YES); 1091 = GNUNET_CONTAINER_multihashmap_create (s->total,
1110 s->intersection_set = GNUNET_SET_create (cfg, 1092 GNUNET_YES);
1111 GNUNET_SET_OPERATION_INTERSECTION); 1093 s->intersection_set
1112 for (i = 0; i < contained_count; i++) 1094 = GNUNET_SET_create (cfg,
1095 GNUNET_SET_OPERATION_INTERSECTION);
1096 for (uint32_t i = 0; i < contained_count; i++)
1113 { 1097 {
1114 if (0 == GNUNET_ntohll (elements[i].value)) 1098 if (0 == GNUNET_ntohll (elements[i].value))
1115 continue; 1099 continue;
@@ -1135,10 +1119,7 @@ GSS_handle_bob_client_message (void *cls,
1135 NULL, NULL); 1119 NULL, NULL);
1136 s->used_element_count++; 1120 s->used_element_count++;
1137 } 1121 }
1138 GNUNET_SERVER_client_set_user_context (client, 1122 GNUNET_SERVICE_client_continue (s->client);
1139 s);
1140 GNUNET_SERVER_receive_done (client,
1141 GNUNET_YES);
1142 if (s->total != s->client_received_element_count) 1123 if (s->total != s->client_received_element_count)
1143 { 1124 {
1144 /* multipart msg */ 1125 /* multipart msg */
@@ -1187,6 +1168,30 @@ shutdown_task (void *cls)
1187 1168
1188 1169
1189/** 1170/**
1171 * A client connected.
1172 *
1173 * Setup the associated data structure.
1174 *
1175 * @param cls closure, NULL
1176 * @param client identification of the client
1177 * @param mq message queue to communicate with @a client
1178 * @return our `struct BobServiceSession`
1179 */
1180static void *
1181client_connect_cb (void *cls,
1182 struct GNUNET_SERVICE_Client *client,
1183 struct GNUNET_MQ_Handle *mq)
1184{
1185 struct BobServiceSession *s;
1186
1187 s = GNUNET_new (struct BobServiceSession);
1188 s->client = client;
1189 s->client_mq = mq;
1190 return s;
1191}
1192
1193
1194/**
1190 * A client disconnected. 1195 * A client disconnected.
1191 * 1196 *
1192 * Remove the associated session(s), release data structures 1197 * Remove the associated session(s), release data structures
@@ -1194,21 +1199,17 @@ shutdown_task (void *cls)
1194 * 1199 *
1195 * @param cls closure, NULL 1200 * @param cls closure, NULL
1196 * @param client identification of the client 1201 * @param client identification of the client
1202 * @param app_cls our `struct BobServiceSession`
1197 */ 1203 */
1198static void 1204static void
1199handle_client_disconnect (void *cls, 1205client_disconnect_cb (void *cls,
1200 struct GNUNET_SERVER_Client *client) 1206 struct GNUNET_SERVICE_Client *client,
1207 void *app_cls)
1201{ 1208{
1202 struct BobServiceSession *s; 1209 struct BobServiceSession *s = app_cls;
1203 1210
1204 if (NULL == client)
1205 return;
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Client disconnected from us.\n"); 1212 "Client disconnected from us.\n");
1208 s = GNUNET_SERVER_client_get_user_context (client,
1209 struct BobServiceSession);
1210 if (NULL == s)
1211 return;
1212 s->client = NULL; 1213 s->client = NULL;
1213 destroy_service_session (s); 1214 destroy_service_session (s);
1214} 1215}
@@ -1218,23 +1219,14 @@ handle_client_disconnect (void *cls,
1218 * Initialization of the program and message handlers 1219 * Initialization of the program and message handlers
1219 * 1220 *
1220 * @param cls closure 1221 * @param cls closure
1221 * @param server the initialized server
1222 * @param c configuration to use 1222 * @param c configuration to use
1223 * @param service the initialized service
1223 */ 1224 */
1224static void 1225static void
1225run (void *cls, 1226run (void *cls,
1226 struct GNUNET_SERVER_Handle *server, 1227 const struct GNUNET_CONFIGURATION_Handle *c,
1227 const struct GNUNET_CONFIGURATION_Handle *c) 1228 struct GNUNET_SERVICE_Handle *service)
1228{ 1229{
1229 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1230 { &GSS_handle_bob_client_message, NULL,
1231 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1232 0},
1233 { &GSS_handle_bob_client_message_multipart, NULL,
1234 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB,
1235 0},
1236 { NULL, NULL, 0, 0}
1237 };
1238 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = { 1230 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1239 { &handle_alices_computation_request, 1231 { &handle_alices_computation_request,
1240 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION, 1232 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION,
@@ -1249,19 +1241,16 @@ run (void *cls,
1249 /* We don't really do DLOG, so we can setup with very minimal resources */ 1241 /* We don't really do DLOG, so we can setup with very minimal resources */
1250 edc = GNUNET_CRYPTO_ecc_dlog_prepare (4 /* max value */, 1242 edc = GNUNET_CRYPTO_ecc_dlog_prepare (4 /* max value */,
1251 2 /* RAM */); 1243 2 /* RAM */);
1252
1253 GNUNET_SERVER_add_handlers (server,
1254 server_handlers);
1255 GNUNET_SERVER_disconnect_notify (server,
1256 &handle_client_disconnect,
1257 NULL);
1258 client_sessions = GNUNET_CONTAINER_multihashmap_create (128, 1244 client_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1259 GNUNET_YES); 1245 GNUNET_YES);
1260 cadet_sessions = GNUNET_CONTAINER_multihashmap_create (128, 1246 cadet_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1261 GNUNET_YES); 1247 GNUNET_YES);
1262 my_cadet = GNUNET_CADET_connect (cfg, NULL, 1248 my_cadet = GNUNET_CADET_connect (cfg,
1249 NULL,
1263 &cb_channel_destruction, 1250 &cb_channel_destruction,
1264 cadet_handlers); 1251 cadet_handlers);
1252 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1253 NULL);
1265 if (NULL == my_cadet) 1254 if (NULL == my_cadet)
1266 { 1255 {
1267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1269,27 +1258,28 @@ run (void *cls,
1269 GNUNET_SCHEDULER_shutdown (); 1258 GNUNET_SCHEDULER_shutdown ();
1270 return; 1259 return;
1271 } 1260 }
1272 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1273 NULL);
1274} 1261}
1275 1262
1276 1263
1277/** 1264/**
1278 * The main function for the scalarproduct service. 1265 * Define "main" method using service macro.
1279 *
1280 * @param argc number of arguments from the command line
1281 * @param argv command line arguments
1282 * @return 0 ok, 1 on error
1283 */ 1266 */
1284int 1267GNUNET_SERVICE_MAIN
1285main (int argc, 1268("scalarproduct-bob",
1286 char *const *argv) 1269 GNUNET_SERVICE_OPTION_NONE,
1287{ 1270 &run,
1288 return (GNUNET_OK == 1271 &client_connect_cb,
1289 GNUNET_SERVICE_run (argc, argv, 1272 &client_disconnect_cb,
1290 "scalarproduct-bob", 1273 NULL,
1291 GNUNET_SERVICE_OPTION_NONE, 1274 GNUNET_MQ_hd_var_size (bob_client_message,
1292 &run, NULL)) ? 0 : 1; 1275 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1293} 1276 struct BobComputationMessage,
1277 NULL),
1278GNUNET_MQ_hd_var_size (bob_client_message_multipart,
1279 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB,
1280 struct ComputationBobCryptodataMultipartMessage,
1281 NULL),
1282 GNUNET_MQ_handler_end ());
1283
1294 1284
1295/* end of gnunet-service-scalarproduct-ecc_bob.c */ 1285/* end of gnunet-service-scalarproduct-ecc_bob.c */