aboutsummaryrefslogtreecommitdiff
path: root/src/psyc/psyc_api.c
diff options
context:
space:
mode:
authorGabor X Toth <*@tg-x.net>2014-05-26 22:16:05 +0000
committerGabor X Toth <*@tg-x.net>2014-05-26 22:16:05 +0000
commit83495695331fcfa8824d7a6d407b82cfcfc8b13c (patch)
tree2d2a7717d81890f5142da9561bccf9e164b1deee /src/psyc/psyc_api.c
parent1cfab01aaea932539c39dcb2118ec4d6c6d44381 (diff)
downloadgnunet-83495695331fcfa8824d7a6d407b82cfcfc8b13c.tar.gz
gnunet-83495695331fcfa8824d7a6d407b82cfcfc8b13c.zip
psyc, multicast: join decision, tests
Diffstat (limited to 'src/psyc/psyc_api.c')
-rw-r--r--src/psyc/psyc_api.c147
1 files changed, 86 insertions, 61 deletions
diff --git a/src/psyc/psyc_api.c b/src/psyc/psyc_api.c
index ee49a584f..7ec9d21b7 100644
--- a/src/psyc/psyc_api.c
+++ b/src/psyc/psyc_api.c
@@ -198,9 +198,9 @@ struct GNUNET_PSYC_Master
198 GNUNET_PSYC_MasterStartCallback start_cb; 198 GNUNET_PSYC_MasterStartCallback start_cb;
199 199
200 /** 200 /**
201 * Join handler callback. 201 * Join request callback.
202 */ 202 */
203 GNUNET_PSYC_JoinCallback join_cb; 203 GNUNET_PSYC_JoinRequestCallback join_req_cb;
204}; 204};
205 205
206 206
@@ -211,14 +211,16 @@ struct GNUNET_PSYC_Slave
211{ 211{
212 struct GNUNET_PSYC_Channel ch; 212 struct GNUNET_PSYC_Channel ch;
213 213
214 GNUNET_PSYC_SlaveJoinCallback join_cb; 214 GNUNET_PSYC_SlaveConnectCallback connect_cb;
215
216 GNUNET_PSYC_JoinDecisionCallback join_dcsn_cb;
215}; 217};
216 218
217 219
218/** 220/**
219 * Handle that identifies a join request. 221 * Handle that identifies a join request.
220 * 222 *
221 * Used to match calls to #GNUNET_PSYC_JoinCallback to the 223 * Used to match calls to #GNUNET_PSYC_JoinRequestCallback to the
222 * corresponding calls to GNUNET_PSYC_join_decision(). 224 * corresponding calls to GNUNET_PSYC_join_decision().
223 */ 225 */
224struct GNUNET_PSYC_JoinHandle 226struct GNUNET_PSYC_JoinHandle
@@ -922,7 +924,22 @@ handle_psyc_join_request (struct GNUNET_PSYC_Master *mst,
922 jh->mst = mst; 924 jh->mst = mst;
923 jh->slave_key = req->slave_key; 925 jh->slave_key = req->slave_key;
924 926
925 mst->join_cb (mst->ch.cb_cls, &req->slave_key, msg, jh); 927 if (NULL != mst->join_req_cb)
928 mst->join_req_cb (mst->ch.cb_cls, &req->slave_key, msg, jh);
929}
930
931
932static void
933handle_psyc_join_decision (struct GNUNET_PSYC_Slave *slv,
934 const struct SlaveJoinDecision *dcsn)
935{
936 struct GNUNET_PSYC_MessageHeader *msg = NULL;
937 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*msg))
938 msg = (struct GNUNET_PSYC_MessageHeader *) &dcsn[1];
939
940 struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh));
941 if (NULL != slv->join_dcsn_cb)
942 slv->join_dcsn_cb (slv->ch.cb_cls, ntohl (dcsn->is_admitted), msg);
926} 943}
927 944
928 945
@@ -971,6 +988,9 @@ message_handler (void *cls,
971 case GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST: 988 case GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST:
972 size_min = sizeof (struct MasterJoinRequest); 989 size_min = sizeof (struct MasterJoinRequest);
973 break; 990 break;
991 case GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION:
992 size_min = sizeof (struct SlaveJoinDecision);
993 break;
974 default: 994 default:
975 GNUNET_break_op (0); 995 GNUNET_break_op (0);
976 return; 996 return;
@@ -995,8 +1015,8 @@ message_handler (void *cls,
995 case GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK: 1015 case GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK:
996 { 1016 {
997 struct CountersResult *cres = (struct CountersResult *) msg; 1017 struct CountersResult *cres = (struct CountersResult *) msg;
998 if (NULL != slv->join_cb) 1018 if (NULL != slv->connect_cb)
999 slv->join_cb (ch->cb_cls, GNUNET_ntohll (cres->max_message_id)); 1019 slv->connect_cb (ch->cb_cls, GNUNET_ntohll (cres->max_message_id));
1000 break; 1020 break;
1001 } 1021 }
1002 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK: 1022 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK:
@@ -1013,6 +1033,11 @@ message_handler (void *cls,
1013 handle_psyc_join_request ((struct GNUNET_PSYC_Master *) ch, 1033 handle_psyc_join_request ((struct GNUNET_PSYC_Master *) ch,
1014 (const struct MasterJoinRequest *) msg); 1034 (const struct MasterJoinRequest *) msg);
1015 break; 1035 break;
1036
1037 case GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION:
1038 handle_psyc_join_decision ((struct GNUNET_PSYC_Slave *) ch,
1039 (const struct SlaveJoinDecision *) msg);
1040 break;
1016 } 1041 }
1017 1042
1018 if (NULL != ch->client) 1043 if (NULL != ch->client)
@@ -1175,20 +1200,20 @@ disconnect (void *c)
1175 * or part messages, the respective methods must call other PSYC functions to 1200 * or part messages, the respective methods must call other PSYC functions to
1176 * inform PSYC about the meaning of the respective events. 1201 * inform PSYC about the meaning of the respective events.
1177 * 1202 *
1178 * @param cfg Configuration to use (to connect to PSYC service). 1203 * @param cfg Configuration to use (to connect to PSYC service).
1179 * @param channel_key ECC key that will be used to sign messages for this 1204 * @param channel_key ECC key that will be used to sign messages for this
1180 * PSYC session. The public key is used to identify the PSYC channel. 1205 * PSYC session. The public key is used to identify the PSYC channel.
1181 * Note that end-users will usually not use the private key directly, but 1206 * Note that end-users will usually not use the private key directly, but
1182 * rather look it up in GNS for places managed by other users, or select 1207 * rather look it up in GNS for places managed by other users, or select
1183 * a file with the private key(s) when setting up their own channels 1208 * a file with the private key(s) when setting up their own channels
1184 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper 1209 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
1185 * one in the future. 1210 * one in the future.
1186 * @param policy Channel policy specifying join and history restrictions. 1211 * @param policy Channel policy specifying join and history restrictions.
1187 * Used to automate join decisions. 1212 * Used to automate join decisions.
1188 * @param message_cb Function to invoke on message parts received from slaves. 1213 * @param message_cb Function to invoke on message parts received from slaves.
1189 * @param join_cb Function to invoke when a peer wants to join. 1214 * @param join_request_cb Function to invoke when a slave wants to join.
1190 * @param master_started_cb Function to invoke after the channel master started. 1215 * @param master_start_cb Function to invoke after the channel master started.
1191 * @param cls Closure for @a master_started_cb and @a join_cb. 1216 * @param cls Closure for @a method and @a join_cb.
1192 * 1217 *
1193 * @return Handle for the channel master, NULL on error. 1218 * @return Handle for the channel master, NULL on error.
1194 */ 1219 */
@@ -1196,9 +1221,9 @@ struct GNUNET_PSYC_Master *
1196GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 1221GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1197 const struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key, 1222 const struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key,
1198 enum GNUNET_PSYC_Policy policy, 1223 enum GNUNET_PSYC_Policy policy,
1224 GNUNET_PSYC_MasterStartCallback start_cb,
1225 GNUNET_PSYC_JoinRequestCallback join_request_cb,
1199 GNUNET_PSYC_MessageCallback message_cb, 1226 GNUNET_PSYC_MessageCallback message_cb,
1200 GNUNET_PSYC_JoinCallback join_cb,
1201 GNUNET_PSYC_MasterStartCallback master_started_cb,
1202 void *cls) 1227 void *cls)
1203{ 1228{
1204 struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst)); 1229 struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst));
@@ -1210,8 +1235,8 @@ GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1210 req->channel_key = *channel_key; 1235 req->channel_key = *channel_key;
1211 req->policy = policy; 1236 req->policy = policy;
1212 1237
1213 mst->start_cb = master_started_cb; 1238 mst->start_cb = start_cb;
1214 mst->join_cb = join_cb; 1239 mst->join_req_cb = join_request_cb;
1215 ch->message_cb = message_cb; 1240 ch->message_cb = message_cb;
1216 ch->cb_cls = cls; 1241 ch->cb_cls = cls;
1217 ch->cfg = cfg; 1242 ch->cfg = cfg;
@@ -1244,8 +1269,9 @@ GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *master)
1244 * #GNUNET_PSYC_JoinCallback. 1269 * #GNUNET_PSYC_JoinCallback.
1245 * 1270 *
1246 * @param jh Join request handle. 1271 * @param jh Join request handle.
1247 * @param is_admitted #GNUNET_YES if joining is approved, 1272 * @param is_admitted #GNUNET_YES if the join is approved,
1248 * #GNUNET_NO if it is disapproved. 1273 * #GNUNET_NO if it is disapproved,
1274 * #GNUNET_SYSERR if we cannot answer the request.
1249 * @param relay_count Number of relays given. 1275 * @param relay_count Number of relays given.
1250 * @param relays Array of suggested peers that might be useful relays to use 1276 * @param relays Array of suggested peers that might be useful relays to use
1251 * when joining the multicast group (essentially a list of peers that 1277 * when joining the multicast group (essentially a list of peers that
@@ -1254,48 +1280,42 @@ GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *master)
1254 * be the multicast origin) is a good candidate for building the 1280 * be the multicast origin) is a good candidate for building the
1255 * multicast tree. Note that it is unnecessary to specify our own 1281 * multicast tree. Note that it is unnecessary to specify our own
1256 * peer identity in this array. 1282 * peer identity in this array.
1257 * @param method_name Method name for the message transmitted with the response. 1283 * @param join_resp Application-dependent join response message.
1258 * @param env Environment containing transient variables for the message, or NULL. 1284 *
1259 * @param data Data of the message. 1285 * @return #GNUNET_OK on success,
1260 * @param data_size Size of @a data. 1286 * #GNUNET_SYSERR if the message is too large.
1261 */ 1287 */
1262void 1288int
1263GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh, 1289GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
1264 int is_admitted, 1290 int is_admitted,
1265 uint32_t relay_count, 1291 uint32_t relay_count,
1266 const struct GNUNET_PeerIdentity *relays, 1292 const struct GNUNET_PeerIdentity *relays,
1267 const char *method_name, 1293 const struct GNUNET_PSYC_MessageHeader *join_resp)
1268 const struct GNUNET_ENV_Environment *env,
1269 const void *data,
1270 size_t data_size)
1271{ 1294{
1272 struct GNUNET_PSYC_Channel *ch = &jh->mst->ch; 1295 struct GNUNET_PSYC_Channel *ch = &jh->mst->ch;
1273
1274 struct MasterJoinDecision *dcsn; 1296 struct MasterJoinDecision *dcsn;
1275 struct GNUNET_PSYC_MessageHeader *pmsg = NULL; 1297 uint16_t join_resp_size
1276 uint16_t pmsg_size = 0; 1298 = (NULL != join_resp) ? ntohs (join_resp->header.size) : 0;
1277/* FIXME:
1278 sizeof (*pmsg)
1279 + sizeof (struct GNUNET_PSYC_MessageMethod)
1280 + vars_size
1281 + sizeof (struct GNUNET_MessageHeader) + data_size
1282 + sizeof (struct GNUNET_MessageHeader);
1283*/
1284 uint16_t relay_size = relay_count * sizeof (*relays); 1299 uint16_t relay_size = relay_count * sizeof (*relays);
1285 struct MessageQueue * 1300
1286 mq = GNUNET_malloc (sizeof (*mq) + sizeof (*dcsn) + relay_size + pmsg_size); 1301 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
1302 < sizeof (*dcsn) + relay_size + join_resp_size)
1303 return GNUNET_SYSERR;
1304
1305 struct MessageQueue *mq = GNUNET_malloc (sizeof (*mq) + sizeof (*dcsn)
1306 + relay_size + join_resp_size);
1287 dcsn = (struct MasterJoinDecision *) &mq[1]; 1307 dcsn = (struct MasterJoinDecision *) &mq[1];
1288 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + pmsg_size); 1308 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
1289 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION); 1309 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1290 dcsn->is_admitted = (GNUNET_YES == is_admitted) ? GNUNET_YES : GNUNET_NO; 1310 dcsn->is_admitted = htonl (is_admitted);
1291 dcsn->slave_key = jh->slave_key; 1311 dcsn->slave_key = jh->slave_key;
1292 1312
1293 /* FIXME: add message parts to pmsg */ 1313 if (0 < join_resp_size)
1294 if (0 < pmsg_size) 1314 memcpy (&dcsn[1], join_resp, join_resp_size);
1295 memcpy (&dcsn[1], pmsg, pmsg_size);
1296 1315
1297 GNUNET_CONTAINER_DLL_insert_tail (ch->tmit_head, ch->tmit_tail, mq); 1316 GNUNET_CONTAINER_DLL_insert_tail (ch->tmit_head, ch->tmit_tail, mq);
1298 transmit_next (ch); 1317 transmit_next (ch);
1318 return GNUNET_OK;
1299} 1319}
1300 1320
1301 1321
@@ -1359,24 +1379,27 @@ GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *th)
1359 * notification on failure (as the channel may simply take days to approve, 1379 * notification on failure (as the channel may simply take days to approve,
1360 * and disapproval is simply being ignored). 1380 * and disapproval is simply being ignored).
1361 * 1381 *
1362 * @param cfg Configuration to use. 1382 * @param cfg Configuration to use.
1363 * @param channel_key ECC public key that identifies the channel we wish to join. 1383 * @param channel_key ECC public key that identifies the channel we wish to join.
1364 * @param slave_key ECC private-public key pair that identifies the slave, and 1384 * @param slave_key ECC private-public key pair that identifies the slave, and
1365 * used by multicast to sign the join request and subsequent unicast 1385 * used by multicast to sign the join request and subsequent unicast
1366 * requests sent to the master. 1386 * requests sent to the master.
1367 * @param origin Peer identity of the origin. 1387 * @param origin Peer identity of the origin.
1368 * @param relay_count Number of peers in the @a relays array. 1388 * @param relay_count Number of peers in the @a relays array.
1369 * @param relays Peer identities of members of the multicast group, which serve 1389 * @param relays Peer identities of members of the multicast group, which serve
1370 * as relays and used to join the group at. 1390 * as relays and used to join the group at.
1371 * @param message_cb Function to invoke on message parts received from the 1391 * @param message_cb Function to invoke on message parts received from the
1372 * channel, typically at least contains method handlers for @e join and 1392 * channel, typically at least contains method handlers for @e join and
1373 * @e part. 1393 * @e part.
1374 * @param slave_joined_cb Function invoked once we have joined the channel. 1394 * @param slave_connect_cb Function invoked once we have connected to the
1375 * @param cls Closure for @a message_cb and @a slave_joined_cb. 1395 * PSYC service.
1376 * @param method_name Method name for the join request. 1396 * @param join_decision_cb Function invoked once we have received a join
1377 * @param env Environment containing transient variables for the request, or NULL. 1397 * decision.
1378 * @param data Payload for the join message. 1398 * @param cls Closure for @a message_cb and @a slave_joined_cb.
1379 * @param data_size Number of bytes in @a data. 1399 * @param method_name Method name for the join request.
1400 * @param env Environment containing transient variables for the request, or NULL.
1401 * @param data Payload for the join message.
1402 * @param data_size Number of bytes in @a data.
1380 * 1403 *
1381 * @return Handle for the slave, NULL on error. 1404 * @return Handle for the slave, NULL on error.
1382 */ 1405 */
@@ -1388,7 +1411,8 @@ GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
1388 uint32_t relay_count, 1411 uint32_t relay_count,
1389 const struct GNUNET_PeerIdentity *relays, 1412 const struct GNUNET_PeerIdentity *relays,
1390 GNUNET_PSYC_MessageCallback message_cb, 1413 GNUNET_PSYC_MessageCallback message_cb,
1391 GNUNET_PSYC_SlaveJoinCallback slave_joined_cb, 1414 GNUNET_PSYC_SlaveConnectCallback connect_cb,
1415 GNUNET_PSYC_JoinDecisionCallback join_decision_cb,
1392 void *cls, 1416 void *cls,
1393 const char *method_name, 1417 const char *method_name,
1394 const struct GNUNET_ENV_Environment *env, 1418 const struct GNUNET_ENV_Environment *env,
@@ -1408,7 +1432,8 @@ GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
1408 req->relay_count = htonl (relay_count); 1432 req->relay_count = htonl (relay_count);
1409 memcpy (&req[1], relays, relay_count * sizeof (*relays)); 1433 memcpy (&req[1], relays, relay_count * sizeof (*relays));
1410 1434
1411 slv->join_cb = slave_joined_cb; 1435 slv->connect_cb = connect_cb;
1436 slv->join_dcsn_cb = join_decision_cb;
1412 ch->message_cb = message_cb; 1437 ch->message_cb = message_cb;
1413 ch->cb_cls = cls; 1438 ch->cb_cls = cls;
1414 1439