aboutsummaryrefslogtreecommitdiff
path: root/src/rps/gnunet-service-rps.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/gnunet-service-rps.c')
-rw-r--r--src/rps/gnunet-service-rps.c828
1 files changed, 324 insertions, 504 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index 6b0ecc58c..1f155b14f 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -84,7 +84,7 @@ static struct GNUNET_HashCode port;
84/** 84/**
85 * Set a peer flag of given peer context. 85 * Set a peer flag of given peer context.
86 */ 86 */
87#define set_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask)) 87#define SET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask))
88 88
89/** 89/**
90 * Get peer flag of given peer context. 90 * Get peer flag of given peer context.
@@ -95,7 +95,7 @@ static struct GNUNET_HashCode port;
95/** 95/**
96 * Unset flag of given peer context. 96 * Unset flag of given peer context.
97 */ 97 */
98#define unset_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask)) 98#define UNSET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask))
99 99
100/** 100/**
101 * Get channel flag of given channel context. 101 * Get channel flag of given channel context.
@@ -233,11 +233,6 @@ struct PeerContext
233 struct PendingMessage *pending_messages_tail; 233 struct PendingMessage *pending_messages_tail;
234 234
235 /** 235 /**
236 * @brief Task to destroy this context.
237 */
238 struct GNUNET_SCHEDULER_Task *destruction_task;
239
240 /**
241 * This is pobably followed by 'statistical' data (when we first saw 236 * This is pobably followed by 'statistical' data (when we first saw
242 * it, how did we get its ID, how many pushes (in a timeinterval), 237 * it, how did we get its ID, how many pushes (in a timeinterval),
243 * ...) 238 * ...)
@@ -282,7 +277,9 @@ struct ChannelCtx
282 struct PeerContext *peer_ctx; 277 struct PeerContext *peer_ctx;
283 278
284 /** 279 /**
285 * @brief Scheduled task that will destroy this context 280 * @brief When channel destruction needs to be delayed (because it is called
281 * from within the cadet routine of another channel destruction) this task
282 * refers to the respective _SCHEDULER_Task.
286 */ 283 */
287 struct GNUNET_SCHEDULER_Task *destruction_task; 284 struct GNUNET_SCHEDULER_Task *destruction_task;
288}; 285};
@@ -314,7 +311,6 @@ static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
314static struct GNUNET_CADET_Handle *cadet_handle; 311static struct GNUNET_CADET_Handle *cadet_handle;
315 312
316 313
317
318/** 314/**
319 * @brief Get the #PeerContext associated with a peer 315 * @brief Get the #PeerContext associated with a peer
320 * 316 *
@@ -335,8 +331,28 @@ get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
335 return ctx; 331 return ctx;
336} 332}
337 333
338int 334/**
339Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer); 335 * @brief Check whether we have information about the given peer.
336 *
337 * FIXME probably deprecated. Make this the new _online.
338 *
339 * @param peer peer in question
340 *
341 * @return #GNUNET_YES if peer is known
342 * #GNUNET_NO if peer is not knwon
343 */
344static int
345check_peer_known (const struct GNUNET_PeerIdentity *peer)
346{
347 if (NULL != peer_map)
348 {
349 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
350 } else
351 {
352 return GNUNET_NO;
353 }
354}
355
340 356
341/** 357/**
342 * @brief Create a new #PeerContext and insert it into the peer map 358 * @brief Create a new #PeerContext and insert it into the peer map
@@ -351,7 +367,7 @@ create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
351 struct PeerContext *ctx; 367 struct PeerContext *ctx;
352 int ret; 368 int ret;
353 369
354 GNUNET_assert (GNUNET_NO == Peers_check_peer_known (peer)); 370 GNUNET_assert (GNUNET_NO == check_peer_known (peer));
355 371
356 ctx = GNUNET_new (struct PeerContext); 372 ctx = GNUNET_new (struct PeerContext);
357 ctx->peer_id = *peer; 373 ctx->peer_id = *peer;
@@ -372,18 +388,13 @@ create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
372static struct PeerContext * 388static struct PeerContext *
373create_or_get_peer_ctx (const struct GNUNET_PeerIdentity *peer) 389create_or_get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
374{ 390{
375 if (GNUNET_NO == Peers_check_peer_known (peer)) 391 if (GNUNET_NO == check_peer_known (peer))
376 { 392 {
377 return create_peer_ctx (peer); 393 return create_peer_ctx (peer);
378 } 394 }
379 return get_peer_ctx (peer); 395 return get_peer_ctx (peer);
380} 396}
381 397
382void
383Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
384
385void
386Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
387 398
388/** 399/**
389 * @brief Check whether we have a connection to this @a peer 400 * @brief Check whether we have a connection to this @a peer
@@ -395,13 +406,13 @@ Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlag
395 * @return #GNUNET_YES if we are connected 406 * @return #GNUNET_YES if we are connected
396 * #GNUNET_NO otherwise 407 * #GNUNET_NO otherwise
397 */ 408 */
398int 409static int
399Peers_check_connected (const struct GNUNET_PeerIdentity *peer) 410check_connected (const struct GNUNET_PeerIdentity *peer)
400{ 411{
401 const struct PeerContext *peer_ctx; 412 struct PeerContext *peer_ctx;
402 413
403 /* If we don't know about this peer we don't know whether it's online */ 414 /* If we don't know about this peer we don't know whether it's online */
404 if (GNUNET_NO == Peers_check_peer_known (peer)) 415 if (GNUNET_NO == check_peer_known (peer))
405 { 416 {
406 return GNUNET_NO; 417 return GNUNET_NO;
407 } 418 }
@@ -411,11 +422,11 @@ Peers_check_connected (const struct GNUNET_PeerIdentity *peer)
411 if ( (NULL == peer_ctx->send_channel_ctx) && 422 if ( (NULL == peer_ctx->send_channel_ctx) &&
412 (NULL == peer_ctx->recv_channel_ctx) ) 423 (NULL == peer_ctx->recv_channel_ctx) )
413 { 424 {
414 Peers_unset_peer_flag (peer, Peers_ONLINE); 425 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE);
415 return GNUNET_NO; 426 return GNUNET_NO;
416 } 427 }
417 /* Otherwise (if we have a channel, we know that it's online */ 428 /* Otherwise (if we have a channel, we know that it's online */
418 Peers_set_peer_flag (peer, Peers_ONLINE); 429 SET_PEER_FLAG (peer_ctx, Peers_ONLINE);
419 return GNUNET_YES; 430 return GNUNET_YES;
420} 431}
421 432
@@ -559,7 +570,7 @@ set_peer_live (struct PeerContext *peer_ctx)
559 } 570 }
560 571
561 (void) add_valid_peer (peer); 572 (void) add_valid_peer (peer);
562 set_peer_flag (peer_ctx, Peers_ONLINE); 573 SET_PEER_FLAG (peer_ctx, Peers_ONLINE);
563 574
564 /* Call pending operations */ 575 /* Call pending operations */
565 for (i = 0; i < peer_ctx->num_pending_ops; i++) 576 for (i = 0; i < peer_ctx->num_pending_ops; i++)
@@ -604,15 +615,37 @@ handle_peer_pull_reply (void *cls,
604 * @return The channel context 615 * @return The channel context
605 */ 616 */
606static struct ChannelCtx * 617static struct ChannelCtx *
607add_channel_ctx (struct PeerContext *peer_ctx); 618add_channel_ctx (struct PeerContext *peer_ctx)
619{
620 struct ChannelCtx *channel_ctx;
621 channel_ctx = GNUNET_new (struct ChannelCtx);
622 channel_ctx->peer_ctx = peer_ctx;
623 return channel_ctx;
624}
625
608 626
609/** 627/**
610 * @brief Remove the channel context from the DLL and free the memory. 628 * @brief Free memory and NULL pointers.
611 * 629 *
612 * @param channel_ctx The channel context. 630 * @param channel_ctx The channel context.
613 */ 631 */
614static void 632static void
615remove_channel_ctx (struct ChannelCtx *channel_ctx); 633remove_channel_ctx (struct ChannelCtx *channel_ctx)
634{
635 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
636
637 if (channel_ctx == peer_ctx->send_channel_ctx)
638 {
639 GNUNET_free (channel_ctx);
640 peer_ctx->send_channel_ctx = NULL;
641 peer_ctx->mq = NULL;
642 }
643 else if (channel_ctx == peer_ctx->recv_channel_ctx)
644 {
645 GNUNET_free (channel_ctx);
646 peer_ctx->recv_channel_ctx = NULL;
647 }
648}
616 649
617 650
618/** 651/**
@@ -664,7 +697,7 @@ get_channel (const struct GNUNET_PeerIdentity *peer)
664 &port, 697 &port,
665 GNUNET_CADET_OPTION_RELIABLE, 698 GNUNET_CADET_OPTION_RELIABLE,
666 NULL, /* WindowSize handler */ 699 NULL, /* WindowSize handler */
667 cleanup_destroyed_channel, /* Disconnect handler */ 700 &cleanup_destroyed_channel, /* Disconnect handler */
668 cadet_handlers); 701 cadet_handlers);
669 } 702 }
670 GNUNET_assert (NULL != peer_ctx->send_channel_ctx); 703 GNUNET_assert (NULL != peer_ctx->send_channel_ctx);
@@ -823,8 +856,155 @@ check_operation_scheduled (const struct GNUNET_PeerIdentity *peer,
823 return GNUNET_NO; 856 return GNUNET_NO;
824} 857}
825 858
859
860/**
861 * @brief Callback for scheduler to destroy a channel
862 *
863 * @param cls Context of the channel
864 */
865static void
866destroy_channel (struct ChannelCtx *channel_ctx)
867{
868 struct GNUNET_CADET_Channel *channel;
869
870 if (NULL != channel_ctx->destruction_task)
871 {
872 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task);
873 channel_ctx->destruction_task = NULL;
874 }
875 GNUNET_assert (channel_ctx->channel != NULL);
876 channel = channel_ctx->channel;
877 channel_ctx->channel = NULL;
878 GNUNET_CADET_channel_destroy (channel);
879 remove_channel_ctx (channel_ctx);
880}
881
882
883/**
884 * @brief Destroy a cadet channel.
885 *
886 * This satisfies the function signature of #GNUNET_SCHEDULER_TaskCallback.
887 *
888 * @param cls
889 */
890static void
891destroy_channel_cb (void *cls)
892{
893 struct ChannelCtx *channel_ctx = cls;
894
895 channel_ctx->destruction_task = NULL;
896 destroy_channel (channel_ctx);
897}
898
899
900/**
901 * @brief Schedule the destruction of a channel for immediately afterwards.
902 *
903 * In case a channel is to be destroyed from within the callback to the
904 * destruction of another channel (send channel), we cannot call
905 * GNUNET_CADET_channel_destroy directly, but need to use this scheduling
906 * construction.
907 *
908 * @param channel_ctx channel to be destroyed.
909 */
910static void
911schedule_channel_destruction (struct ChannelCtx *channel_ctx)
912{
913 GNUNET_assert (NULL ==
914 channel_ctx->destruction_task);
915 GNUNET_assert (NULL !=
916 channel_ctx->channel);
917 channel_ctx->destruction_task =
918 GNUNET_SCHEDULER_add_now (&destroy_channel_cb,
919 channel_ctx);
920}
921
922
923/**
924 * @brief Remove peer
925 *
926 * @param peer the peer to clean
927 * @return #GNUNET_YES if peer was removed
928 * #GNUNET_NO otherwise
929 */
826static int 930static int
827Peers_remove_peer (const struct GNUNET_PeerIdentity *peer); 931destroy_peer (struct PeerContext *peer_ctx)
932{
933 GNUNET_assert (NULL != peer_ctx);
934 GNUNET_assert (NULL != peer_map);
935 if (GNUNET_NO ==
936 GNUNET_CONTAINER_multipeermap_contains (peer_map,
937 &peer_ctx->peer_id))
938 {
939 return GNUNET_NO;
940 }
941 SET_PEER_FLAG (peer_ctx, Peers_TO_DESTROY);
942 LOG (GNUNET_ERROR_TYPE_DEBUG,
943 "Going to remove peer %s\n",
944 GNUNET_i2s (&peer_ctx->peer_id));
945 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE);
946
947 /* Clear list of pending operations */
948 // TODO this probably leaks memory
949 // ('only' the cls to the function. Not sure what to do with it)
950 GNUNET_array_grow (peer_ctx->pending_ops,
951 peer_ctx->num_pending_ops,
952 0);
953 /* Remove all pending messages */
954 while (NULL != peer_ctx->pending_messages_head)
955 {
956 LOG (GNUNET_ERROR_TYPE_DEBUG,
957 "Removing unsent %s\n",
958 peer_ctx->pending_messages_head->type);
959 /* Cancle pending message, too */
960 if ( (NULL != peer_ctx->liveliness_check_pending) &&
961 (0 == memcmp (peer_ctx->pending_messages_head,
962 peer_ctx->liveliness_check_pending,
963 sizeof (struct PendingMessage))) )
964 {
965 // TODO this may leak memory
966 peer_ctx->liveliness_check_pending = NULL;
967 }
968 remove_pending_message (peer_ctx->pending_messages_head,
969 GNUNET_YES);
970 }
971
972 /* If we are still waiting for notification whether this peer is live
973 * cancel the according task */
974 if (NULL != peer_ctx->liveliness_check_pending)
975 {
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Removing pending liveliness check for peer %s\n",
978 GNUNET_i2s (&peer_ctx->peer_id));
979 // TODO wait until cadet sets mq->cancel_impl
980 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
981 remove_pending_message (peer_ctx->liveliness_check_pending,
982 GNUNET_YES);
983 peer_ctx->liveliness_check_pending = NULL;
984 }
985
986 if (NULL != peer_ctx->send_channel_ctx)
987 {
988 /* This is possibly called from within channel destruction */
989 schedule_channel_destruction (peer_ctx->send_channel_ctx);
990 }
991 if (NULL != peer_ctx->recv_channel_ctx)
992 {
993 /* This is possibly called from within channel destruction */
994 schedule_channel_destruction (peer_ctx->recv_channel_ctx);
995 }
996
997 if (GNUNET_YES !=
998 GNUNET_CONTAINER_multipeermap_remove_all (peer_map,
999 &peer_ctx->peer_id))
1000 {
1001 LOG (GNUNET_ERROR_TYPE_WARNING,
1002 "removing peer from peer_map failed\n");
1003 }
1004 GNUNET_free (peer_ctx);
1005 return GNUNET_YES;
1006}
1007
828 1008
829/** 1009/**
830 * Iterator over hash map entries. Deletes all contexts of peers. 1010 * Iterator over hash map entries. Deletes all contexts of peers.
@@ -840,7 +1020,7 @@ peermap_clear_iterator (void *cls,
840 const struct GNUNET_PeerIdentity *key, 1020 const struct GNUNET_PeerIdentity *key,
841 void *value) 1021 void *value)
842{ 1022{
843 Peers_remove_peer (key); 1023 destroy_peer (get_peer_ctx (key));
844 return GNUNET_YES; 1024 return GNUNET_YES;
845} 1025}
846 1026
@@ -1084,8 +1264,8 @@ restore_valid_peers ()
1084 * @param cadet_h cadet handle 1264 * @param cadet_h cadet handle
1085 * @param own_id own peer identity 1265 * @param own_id own peer identity
1086 */ 1266 */
1087void 1267static void
1088Peers_initialise (char* fn_valid_peers, 1268initialise_peers (char* fn_valid_peers,
1089 struct GNUNET_CADET_Handle *cadet_h) 1269 struct GNUNET_CADET_Handle *cadet_h)
1090{ 1270{
1091 filename_valid_peers = GNUNET_strdup (fn_valid_peers); 1271 filename_valid_peers = GNUNET_strdup (fn_valid_peers);
@@ -1097,7 +1277,7 @@ Peers_initialise (char* fn_valid_peers,
1097 1277
1098 1278
1099/** 1279/**
1100 * @brief Delete storage of peers that was created with #Peers_initialise () 1280 * @brief Delete storage of peers that was created with #initialise_peers ()
1101 */ 1281 */
1102static void 1282static void
1103peers_terminate () 1283peers_terminate ()
@@ -1150,9 +1330,9 @@ valid_peer_iterator (void *cls,
1150 * @return the number of key value pairs processed, 1330 * @return the number of key value pairs processed,
1151 * #GNUNET_SYSERR if it aborted iteration 1331 * #GNUNET_SYSERR if it aborted iteration
1152 */ 1332 */
1153int 1333static int
1154Peers_get_valid_peers (PeersIterator iterator, 1334get_valid_peers (PeersIterator iterator,
1155 void *it_cls) 1335 void *it_cls)
1156{ 1336{
1157 struct PeersIteratorCls *cls; 1337 struct PeersIteratorCls *cls;
1158 int ret; 1338 int ret;
@@ -1179,10 +1359,10 @@ Peers_get_valid_peers (PeersIterator iterator,
1179 * @return #GNUNET_YES if peer was inserted 1359 * @return #GNUNET_YES if peer was inserted
1180 * #GNUNET_NO otherwise 1360 * #GNUNET_NO otherwise
1181 */ 1361 */
1182int 1362static int
1183Peers_insert_peer (const struct GNUNET_PeerIdentity *peer) 1363insert_peer (const struct GNUNET_PeerIdentity *peer)
1184{ 1364{
1185 if (GNUNET_YES == Peers_check_peer_known (peer)) 1365 if (GNUNET_YES == check_peer_known (peer))
1186 { 1366 {
1187 return GNUNET_NO; /* We already know this peer - nothing to do */ 1367 return GNUNET_NO; /* We already know this peer - nothing to do */
1188 } 1368 }
@@ -1190,8 +1370,30 @@ Peers_insert_peer (const struct GNUNET_PeerIdentity *peer)
1190 return GNUNET_YES; 1370 return GNUNET_YES;
1191} 1371}
1192 1372
1193int 1373
1194Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags); 1374/**
1375 * @brief Check whether flags on a peer are set.
1376 *
1377 * @param peer the peer to check the flag of
1378 * @param flags the flags to check
1379 *
1380 * @return #GNUNET_SYSERR if peer is not known
1381 * #GNUNET_YES if all given flags are set
1382 * #GNUNET_NO otherwise
1383 */
1384static int
1385check_peer_flag (const struct GNUNET_PeerIdentity *peer,
1386 enum Peers_PeerFlags flags)
1387{
1388 struct PeerContext *peer_ctx;
1389
1390 if (GNUNET_NO == check_peer_known (peer))
1391 {
1392 return GNUNET_SYSERR;
1393 }
1394 peer_ctx = get_peer_ctx (peer);
1395 return check_peer_flag_set (peer_ctx, flags);
1396}
1195 1397
1196/** 1398/**
1197 * @brief Try connecting to a peer to see whether it is online 1399 * @brief Try connecting to a peer to see whether it is online
@@ -1202,15 +1404,15 @@ Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFl
1202 * @return #GNUNET_YES if peer had to be inserted 1404 * @return #GNUNET_YES if peer had to be inserted
1203 * #GNUNET_NO otherwise 1405 * #GNUNET_NO otherwise
1204 */ 1406 */
1205int 1407static int
1206Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer) 1408issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
1207{ 1409{
1208 struct PeerContext *peer_ctx; 1410 struct PeerContext *peer_ctx;
1209 int ret; 1411 int ret;
1210 1412
1211 ret = Peers_insert_peer (peer); 1413 ret = insert_peer (peer);
1212 peer_ctx = get_peer_ctx (peer); 1414 peer_ctx = get_peer_ctx (peer);
1213 if ( (GNUNET_NO == Peers_check_peer_flag (peer, Peers_ONLINE)) && 1415 if ( (GNUNET_NO == check_peer_flag (peer, Peers_ONLINE)) &&
1214 (NULL == peer_ctx->liveliness_check_pending) ) 1416 (NULL == peer_ctx->liveliness_check_pending) )
1215 { 1417 {
1216 check_peer_live (peer_ctx); 1418 check_peer_live (peer_ctx);
@@ -1232,8 +1434,8 @@ Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
1232 * #GNUNET_NO if peer is NOT removable 1434 * #GNUNET_NO if peer is NOT removable
1233 * #GNUNET_SYSERR if peer is not known 1435 * #GNUNET_SYSERR if peer is not known
1234 */ 1436 */
1235int 1437static int
1236Peers_check_removable (const struct GNUNET_PeerIdentity *peer) 1438check_removable (const struct GNUNET_PeerIdentity *peer)
1237{ 1439{
1238 struct PeerContext *peer_ctx; 1440 struct PeerContext *peer_ctx;
1239 1441
@@ -1252,262 +1454,6 @@ Peers_check_removable (const struct GNUNET_PeerIdentity *peer)
1252 return GNUNET_YES; 1454 return GNUNET_YES;
1253} 1455}
1254 1456
1255uint32_t *
1256Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1257 enum Peers_ChannelRole role);
1258
1259int
1260Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags);
1261
1262/**
1263 * @brief Callback for the scheduler to destroy the knowledge of a peer.
1264 *
1265 * @param cls Context of the peer
1266 */
1267static void
1268destroy_peer (void *cls)
1269{
1270 struct PeerContext *peer_ctx = cls;
1271
1272 GNUNET_assert (NULL != peer_ctx);
1273 peer_ctx->destruction_task = NULL;
1274 Peers_remove_peer (&peer_ctx->peer_id);
1275}
1276
1277
1278static void
1279destroy_channel (void *cls);
1280
1281
1282/**
1283 * @brief Schedule the destruction of the given channel.
1284 *
1285 * Do so only if it was not already scheduled and not during shutdown.
1286 *
1287 * @param channel_ctx The context of the channel to destroy.
1288 */
1289static void
1290schedule_channel_destruction (struct ChannelCtx *channel_ctx)
1291{
1292 GNUNET_assert (NULL != channel_ctx);
1293 if (NULL != channel_ctx->destruction_task &&
1294 GNUNET_NO == in_shutdown)
1295 {
1296 channel_ctx->destruction_task =
1297 GNUNET_SCHEDULER_add_now (&destroy_channel,
1298 channel_ctx);
1299 }
1300}
1301
1302
1303/**
1304 * @brief Schedule the destruction of the given peer.
1305 *
1306 * Do so only if it was not already scheduled and not during shutdown.
1307 *
1308 * @param peer_ctx The context of the peer to destroy.
1309 */
1310static void
1311schedule_peer_destruction (struct PeerContext *peer_ctx)
1312{
1313 GNUNET_assert (NULL != peer_ctx);
1314 if (NULL != peer_ctx->destruction_task &&
1315 GNUNET_NO == in_shutdown)
1316 {
1317 peer_ctx->destruction_task =
1318 GNUNET_SCHEDULER_add_now (&destroy_peer,
1319 peer_ctx);
1320 }
1321}
1322
1323
1324/**
1325 * @brief Remove peer
1326 *
1327 * @param peer the peer to clean
1328 * @return #GNUNET_YES if peer was removed
1329 * #GNUNET_NO otherwise
1330 */
1331static int
1332Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1333{
1334 struct PeerContext *peer_ctx;
1335
1336 GNUNET_assert (NULL != peer_map);
1337 if (GNUNET_NO ==
1338 GNUNET_CONTAINER_multipeermap_contains (peer_map,
1339 peer))
1340 {
1341 return GNUNET_NO;
1342 }
1343 peer_ctx = get_peer_ctx (peer);
1344 set_peer_flag (peer_ctx, Peers_TO_DESTROY);
1345 LOG (GNUNET_ERROR_TYPE_DEBUG,
1346 "Going to remove peer %s\n",
1347 GNUNET_i2s (&peer_ctx->peer_id));
1348 Peers_unset_peer_flag (peer, Peers_ONLINE);
1349
1350 /* Clear list of pending operations */
1351 // TODO this probably leaks memory
1352 // ('only' the cls to the function. Not sure what to do with it)
1353 GNUNET_array_grow (peer_ctx->pending_ops,
1354 peer_ctx->num_pending_ops,
1355 0);
1356 /* Remove all pending messages */
1357 while (NULL != peer_ctx->pending_messages_head)
1358 {
1359 LOG (GNUNET_ERROR_TYPE_DEBUG,
1360 "Removing unsent %s\n",
1361 peer_ctx->pending_messages_head->type);
1362 /* Cancle pending message, too */
1363 if ( (NULL != peer_ctx->liveliness_check_pending) &&
1364 (0 == memcmp (peer_ctx->pending_messages_head,
1365 peer_ctx->liveliness_check_pending,
1366 sizeof (struct PendingMessage))) )
1367 {
1368 // TODO this may leak memory
1369 peer_ctx->liveliness_check_pending = NULL;
1370 }
1371 remove_pending_message (peer_ctx->pending_messages_head,
1372 GNUNET_YES);
1373 }
1374
1375 /* If we are still waiting for notification whether this peer is live
1376 * cancel the according task */
1377 if (NULL != peer_ctx->liveliness_check_pending)
1378 {
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Removing pending liveliness check for peer %s\n",
1381 GNUNET_i2s (&peer_ctx->peer_id));
1382 // TODO wait until cadet sets mq->cancel_impl
1383 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
1384 remove_pending_message (peer_ctx->liveliness_check_pending,
1385 GNUNET_YES);
1386 peer_ctx->liveliness_check_pending = NULL;
1387 }
1388
1389
1390 /* Do we still have to wait for destruction of channels
1391 * or issue the destruction? */
1392 if (NULL != peer_ctx->send_channel_ctx &&
1393 NULL != peer_ctx->send_channel_ctx->destruction_task)
1394 {
1395 schedule_peer_destruction (peer_ctx);
1396 return GNUNET_NO;
1397 }
1398 if (NULL != peer_ctx->recv_channel_ctx &&
1399 NULL != peer_ctx->recv_channel_ctx->destruction_task)
1400 {
1401 schedule_peer_destruction (peer_ctx);
1402 return GNUNET_NO;
1403 }
1404 if (NULL != peer_ctx->recv_channel_ctx)
1405 {
1406 schedule_channel_destruction (peer_ctx->recv_channel_ctx);
1407 schedule_peer_destruction (peer_ctx);
1408 return GNUNET_NO;
1409 }
1410 if (NULL != peer_ctx->send_channel_ctx)
1411 {
1412 schedule_channel_destruction (peer_ctx->send_channel_ctx);
1413 schedule_peer_destruction (peer_ctx);
1414 return GNUNET_NO;
1415 }
1416
1417 if (NULL != peer_ctx->destruction_task)
1418 {
1419 GNUNET_SCHEDULER_cancel (peer_ctx->destruction_task);
1420 }
1421
1422 if (GNUNET_YES !=
1423 GNUNET_CONTAINER_multipeermap_remove_all (peer_map,
1424 &peer_ctx->peer_id))
1425 {
1426 LOG (GNUNET_ERROR_TYPE_WARNING,
1427 "removing peer from peer_map failed\n");
1428 }
1429 GNUNET_free (peer_ctx);
1430 return GNUNET_YES;
1431}
1432
1433
1434/**
1435 * @brief set flags on a given peer.
1436 *
1437 * @param peer the peer to set flags on
1438 * @param flags the flags
1439 */
1440void
1441Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1442{
1443 struct PeerContext *peer_ctx;
1444
1445 peer_ctx = get_peer_ctx (peer);
1446 set_peer_flag (peer_ctx, flags);
1447}
1448
1449
1450/**
1451 * @brief unset flags on a given peer.
1452 *
1453 * @param peer the peer to unset flags on
1454 * @param flags the flags
1455 */
1456void
1457Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1458{
1459 struct PeerContext *peer_ctx;
1460
1461 peer_ctx = get_peer_ctx (peer);
1462 unset_peer_flag (peer_ctx, flags);
1463}
1464
1465
1466/**
1467 * @brief Check whether flags on a peer are set.
1468 *
1469 * @param peer the peer to check the flag of
1470 * @param flags the flags to check
1471 *
1472 * @return #GNUNET_SYSERR if peer is not known
1473 * #GNUNET_YES if all given flags are set
1474 * #GNUNET_NO otherwise
1475 */
1476int
1477Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1478{
1479 struct PeerContext *peer_ctx;
1480
1481 if (GNUNET_NO == Peers_check_peer_known (peer))
1482 {
1483 return GNUNET_SYSERR;
1484 }
1485 peer_ctx = get_peer_ctx (peer);
1486 return check_peer_flag_set (peer_ctx, flags);
1487}
1488
1489/**
1490 * @brief Check whether we have information about the given peer.
1491 *
1492 * FIXME probably deprecated. Make this the new _online.
1493 *
1494 * @param peer peer in question
1495 *
1496 * @return #GNUNET_YES if peer is known
1497 * #GNUNET_NO if peer is not knwon
1498 */
1499int
1500Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer)
1501{
1502 if (NULL != peer_map)
1503 {
1504 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
1505 } else
1506 {
1507 return GNUNET_NO;
1508 }
1509}
1510
1511 1457
1512/** 1458/**
1513 * @brief Check whether @a peer is actually a peer. 1459 * @brief Check whether @a peer is actually a peer.
@@ -1519,8 +1465,8 @@ Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer)
1519 * @return #GNUNET_YES if peer is valid 1465 * @return #GNUNET_YES if peer is valid
1520 * #GNUNET_NO if peer is not valid 1466 * #GNUNET_NO if peer is not valid
1521 */ 1467 */
1522int 1468static int
1523Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer) 1469check_peer_valid (const struct GNUNET_PeerIdentity *peer)
1524{ 1470{
1525 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer); 1471 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer);
1526} 1472}
@@ -1533,10 +1479,10 @@ Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer)
1533 * 1479 *
1534 * @param peer the peer to establish channel to 1480 * @param peer the peer to establish channel to
1535 */ 1481 */
1536void 1482static void
1537Peers_indicate_sending_intention (const struct GNUNET_PeerIdentity *peer) 1483indicate_sending_intention (const struct GNUNET_PeerIdentity *peer)
1538{ 1484{
1539 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer)); 1485 GNUNET_assert (GNUNET_YES == check_peer_known (peer));
1540 (void) get_channel (peer); 1486 (void) get_channel (peer);
1541} 1487}
1542 1488
@@ -1550,8 +1496,8 @@ Peers_indicate_sending_intention (const struct GNUNET_PeerIdentity *peer)
1550 * @return #GNUNET_YES if peer has the intention to send 1496 * @return #GNUNET_YES if peer has the intention to send
1551 * #GNUNET_NO otherwise 1497 * #GNUNET_NO otherwise
1552 */ 1498 */
1553int 1499static int
1554Peers_check_peer_send_intention (const struct GNUNET_PeerIdentity *peer) 1500check_peer_send_intention (const struct GNUNET_PeerIdentity *peer)
1555{ 1501{
1556 const struct PeerContext *peer_ctx; 1502 const struct PeerContext *peer_ctx;
1557 1503
@@ -1574,10 +1520,10 @@ Peers_check_peer_send_intention (const struct GNUNET_PeerIdentity *peer)
1574 * @return initial channel context for the channel 1520 * @return initial channel context for the channel
1575 * (can be NULL -- that's not an error) 1521 * (can be NULL -- that's not an error)
1576 */ 1522 */
1577void * 1523static void *
1578Peers_handle_inbound_channel (void *cls, 1524handle_inbound_channel (void *cls,
1579 struct GNUNET_CADET_Channel *channel, 1525 struct GNUNET_CADET_Channel *channel,
1580 const struct GNUNET_PeerIdentity *initiator) 1526 const struct GNUNET_PeerIdentity *initiator)
1581{ 1527{
1582 struct PeerContext *peer_ctx; 1528 struct PeerContext *peer_ctx;
1583 struct GNUNET_PeerIdentity *ctx_peer; 1529 struct GNUNET_PeerIdentity *ctx_peer;
@@ -1595,14 +1541,12 @@ Peers_handle_inbound_channel (void *cls,
1595 channel_ctx = add_channel_ctx (peer_ctx); 1541 channel_ctx = add_channel_ctx (peer_ctx);
1596 channel_ctx->channel = channel; 1542 channel_ctx->channel = channel;
1597 /* We only accept one incoming channel per peer */ 1543 /* We only accept one incoming channel per peer */
1598 if (GNUNET_YES == Peers_check_peer_send_intention (initiator)) 1544 if (GNUNET_YES == check_peer_send_intention (initiator))
1599 { 1545 {
1600 LOG (GNUNET_ERROR_TYPE_WARNING, 1546 LOG (GNUNET_ERROR_TYPE_WARNING,
1601 "Already got one receive channel. Destroying old one.\n"); 1547 "Already got one receive channel. Destroying old one.\n");
1602 GNUNET_break_op (0); 1548 GNUNET_break_op (0);
1603 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel_ctx->channel); 1549 destroy_channel (peer_ctx->recv_channel_ctx);
1604 peer_ctx->recv_channel_ctx->channel = NULL;
1605 remove_channel_ctx (peer_ctx->recv_channel_ctx);
1606 peer_ctx->recv_channel_ctx = channel_ctx; 1550 peer_ctx->recv_channel_ctx = channel_ctx;
1607 /* return the channel context */ 1551 /* return the channel context */
1608 return channel_ctx; 1552 return channel_ctx;
@@ -1620,12 +1564,12 @@ Peers_handle_inbound_channel (void *cls,
1620 * @return #GNUNET_YES if a sending channel towards that peer exists 1564 * @return #GNUNET_YES if a sending channel towards that peer exists
1621 * #GNUNET_NO otherwise 1565 * #GNUNET_NO otherwise
1622 */ 1566 */
1623int 1567static int
1624Peers_check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer) 1568check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer)
1625{ 1569{
1626 struct PeerContext *peer_ctx; 1570 struct PeerContext *peer_ctx;
1627 1571
1628 if (GNUNET_NO == Peers_check_peer_known (peer)) 1572 if (GNUNET_NO == check_peer_known (peer))
1629 { /* If no such peer exists, there is no channel */ 1573 { /* If no such peer exists, there is no channel */
1630 return GNUNET_NO; 1574 return GNUNET_NO;
1631 } 1575 }
@@ -1639,46 +1583,6 @@ Peers_check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer)
1639 1583
1640 1584
1641/** 1585/**
1642 * @brief check whether the given channel is the sending channel of the given
1643 * peer
1644 *
1645 * @param peer the peer in question
1646 * @param channel the channel to check for
1647 * @param role either #Peers_CHANNEL_ROLE_SENDING, or
1648 * #Peers_CHANNEL_ROLE_RECEIVING
1649 *
1650 * @return #GNUNET_YES if the given chennel is the sending channel of the peer
1651 * #GNUNET_NO otherwise
1652 */
1653int
1654Peers_check_channel_role (const struct GNUNET_PeerIdentity *peer,
1655 const struct GNUNET_CADET_Channel *channel,
1656 enum Peers_ChannelRole role)
1657{
1658 const struct PeerContext *peer_ctx;
1659
1660 if (GNUNET_NO == Peers_check_peer_known (peer))
1661 {
1662 return GNUNET_NO;
1663 }
1664 peer_ctx = get_peer_ctx (peer);
1665 if ( (Peers_CHANNEL_ROLE_SENDING == role) &&
1666 (NULL != peer_ctx->send_channel_ctx) &&
1667 (channel == peer_ctx->send_channel_ctx->channel) )
1668 {
1669 return GNUNET_YES;
1670 }
1671 if ( (Peers_CHANNEL_ROLE_RECEIVING == role) &&
1672 (NULL != peer_ctx->recv_channel_ctx) &&
1673 (channel == peer_ctx->recv_channel_ctx->channel) )
1674 {
1675 return GNUNET_YES;
1676 }
1677 return GNUNET_NO;
1678}
1679
1680
1681/**
1682 * @brief Destroy the send channel of a peer e.g. stop indicating a sending 1586 * @brief Destroy the send channel of a peer e.g. stop indicating a sending
1683 * intention to another peer 1587 * intention to another peer
1684 * 1588 *
@@ -1690,46 +1594,26 @@ Peers_check_channel_role (const struct GNUNET_PeerIdentity *peer,
1690 * @return #GNUNET_YES if channel was destroyed 1594 * @return #GNUNET_YES if channel was destroyed
1691 * #GNUNET_NO otherwise 1595 * #GNUNET_NO otherwise
1692 */ 1596 */
1693int 1597static int
1694Peers_destroy_sending_channel (const struct GNUNET_PeerIdentity *peer) 1598destroy_sending_channel (const struct GNUNET_PeerIdentity *peer)
1695{ 1599{
1696 struct PeerContext *peer_ctx; 1600 struct PeerContext *peer_ctx;
1697 1601
1698 if (GNUNET_NO == Peers_check_peer_known (peer)) 1602 if (GNUNET_NO == check_peer_known (peer))
1699 { 1603 {
1700 return GNUNET_NO; 1604 return GNUNET_NO;
1701 } 1605 }
1702 peer_ctx = get_peer_ctx (peer); 1606 peer_ctx = get_peer_ctx (peer);
1703 if (NULL != peer_ctx->send_channel_ctx) 1607 if (NULL != peer_ctx->send_channel_ctx)
1704 { 1608 {
1705 schedule_channel_destruction (peer_ctx->send_channel_ctx); 1609 destroy_channel (peer_ctx->send_channel_ctx);
1706 (void) Peers_check_connected (peer); 1610 (void) check_connected (peer);
1707 return GNUNET_YES; 1611 return GNUNET_YES;
1708 } 1612 }
1709 return GNUNET_NO; 1613 return GNUNET_NO;
1710} 1614}
1711 1615
1712/** 1616/**
1713 * @brief Callback for scheduler to destroy a channel
1714 *
1715 * @param cls Context of the channel
1716 */
1717static void
1718destroy_channel (void *cls)
1719{
1720 struct ChannelCtx *channel_ctx = cls;
1721 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
1722
1723 GNUNET_assert (channel_ctx == peer_ctx->send_channel_ctx ||
1724 channel_ctx == peer_ctx->recv_channel_ctx);
1725
1726 channel_ctx->destruction_task = NULL;
1727 GNUNET_CADET_channel_destroy (channel_ctx->channel);
1728 remove_channel_ctx (peer_ctx->send_channel_ctx);
1729}
1730
1731
1732/**
1733 * @brief Send a message to another peer. 1617 * @brief Send a message to another peer.
1734 * 1618 *
1735 * Keeps track about pending messages so they can be properly removed when the 1619 * Keeps track about pending messages so they can be properly removed when the
@@ -1739,10 +1623,10 @@ destroy_channel (void *cls)
1739 * @param ev envelope of the message 1623 * @param ev envelope of the message
1740 * @param type type of the message 1624 * @param type type of the message
1741 */ 1625 */
1742void 1626static void
1743Peers_send_message (const struct GNUNET_PeerIdentity *peer, 1627send_message (const struct GNUNET_PeerIdentity *peer,
1744 struct GNUNET_MQ_Envelope *ev, 1628 struct GNUNET_MQ_Envelope *ev,
1745 const char *type) 1629 const char *type)
1746{ 1630{
1747 struct PendingMessage *pending_msg; 1631 struct PendingMessage *pending_msg;
1748 struct GNUNET_MQ_Handle *mq; 1632 struct GNUNET_MQ_Handle *mq;
@@ -1769,14 +1653,14 @@ Peers_send_message (const struct GNUNET_PeerIdentity *peer,
1769 * @return #GNUNET_YES if the operation was scheduled 1653 * @return #GNUNET_YES if the operation was scheduled
1770 * #GNUNET_NO otherwise 1654 * #GNUNET_NO otherwise
1771 */ 1655 */
1772int 1656static int
1773Peers_schedule_operation (const struct GNUNET_PeerIdentity *peer, 1657schedule_operation (const struct GNUNET_PeerIdentity *peer,
1774 const PeerOp peer_op) 1658 const PeerOp peer_op)
1775{ 1659{
1776 struct PeerPendingOp pending_op; 1660 struct PeerPendingOp pending_op;
1777 struct PeerContext *peer_ctx; 1661 struct PeerContext *peer_ctx;
1778 1662
1779 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer)); 1663 GNUNET_assert (GNUNET_YES == check_peer_known (peer));
1780 1664
1781 //TODO if LIVE/ONLINE execute immediately 1665 //TODO if LIVE/ONLINE execute immediately
1782 1666
@@ -1793,24 +1677,6 @@ Peers_schedule_operation (const struct GNUNET_PeerIdentity *peer,
1793 return GNUNET_NO; 1677 return GNUNET_NO;
1794} 1678}
1795 1679
1796/**
1797 * @brief Get the recv_channel of @a peer.
1798 * Needed to correctly handle (call #GNUNET_CADET_receive_done()) incoming
1799 * messages.
1800 *
1801 * @param peer The peer to get the recv_channel from.
1802 *
1803 * @return The recv_channel.
1804 */
1805struct GNUNET_CADET_Channel *
1806Peers_get_recv_channel (const struct GNUNET_PeerIdentity *peer)
1807{
1808 struct PeerContext *peer_ctx;
1809
1810 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1811 peer_ctx = get_peer_ctx (peer);
1812 return peer_ctx->recv_channel_ctx->channel;
1813}
1814/*********************************************************************** 1680/***********************************************************************
1815 * /Old gnunet-service-rps_peers.c 1681 * /Old gnunet-service-rps_peers.c
1816***********************************************************************/ 1682***********************************************************************/
@@ -2266,16 +2132,16 @@ insert_in_view (const struct GNUNET_PeerIdentity *peer)
2266{ 2132{
2267 int online; 2133 int online;
2268 2134
2269 online = Peers_check_peer_flag (peer, Peers_ONLINE); 2135 online = check_peer_flag (peer, Peers_ONLINE);
2270 if ( (GNUNET_NO == online) || 2136 if ( (GNUNET_NO == online) ||
2271 (GNUNET_SYSERR == online) ) /* peer is not even known */ 2137 (GNUNET_SYSERR == online) ) /* peer is not even known */
2272 { 2138 {
2273 (void) Peers_issue_peer_liveliness_check (peer); 2139 (void) issue_peer_liveliness_check (peer);
2274 (void) Peers_schedule_operation (peer, insert_in_view_op); 2140 (void) schedule_operation (peer, insert_in_view_op);
2275 return GNUNET_NO; 2141 return GNUNET_NO;
2276 } 2142 }
2277 /* Open channel towards peer to keep connection open */ 2143 /* Open channel towards peer to keep connection open */
2278 Peers_indicate_sending_intention (peer); 2144 indicate_sending_intention (peer);
2279 return View_put (peer); 2145 return View_put (peer);
2280} 2146}
2281 2147
@@ -2468,7 +2334,7 @@ send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
2468 GNUNET_memcpy (&out_msg[1], peer_ids, 2334 GNUNET_memcpy (&out_msg[1], peer_ids,
2469 send_size * sizeof (struct GNUNET_PeerIdentity)); 2335 send_size * sizeof (struct GNUNET_PeerIdentity));
2470 2336
2471 Peers_send_message (peer_id, ev, "PULL REPLY"); 2337 send_message (peer_id, ev, "PULL REPLY");
2472 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO); 2338 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO);
2473 // TODO check with send intention: as send_channel is used/opened we indicate 2339 // TODO check with send intention: as send_channel is used/opened we indicate
2474 // a sending intention without intending it. 2340 // a sending intention without intending it.
@@ -2519,10 +2385,10 @@ insert_in_sampler (void *cls,
2519 if (0 < RPS_sampler_count_id (prot_sampler, peer)) 2385 if (0 < RPS_sampler_count_id (prot_sampler, peer))
2520 { 2386 {
2521 /* Make sure we 'know' about this peer */ 2387 /* Make sure we 'know' about this peer */
2522 (void) Peers_issue_peer_liveliness_check (peer); 2388 (void) issue_peer_liveliness_check (peer);
2523 /* Establish a channel towards that peer to indicate we are going to send 2389 /* Establish a channel towards that peer to indicate we are going to send
2524 * messages to it */ 2390 * messages to it */
2525 //Peers_indicate_sending_intention (peer); 2391 //indicate_sending_intention (peer);
2526 } 2392 }
2527 #ifdef TO_FILE 2393 #ifdef TO_FILE
2528 num_observed_peers++; 2394 num_observed_peers++;
@@ -2554,10 +2420,10 @@ static void
2554got_peer (const struct GNUNET_PeerIdentity *peer) 2420got_peer (const struct GNUNET_PeerIdentity *peer)
2555{ 2421{
2556 /* If we did not know this peer already, insert it into sampler and view */ 2422 /* If we did not know this peer already, insert it into sampler and view */
2557 if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer)) 2423 if (GNUNET_YES == issue_peer_liveliness_check (peer))
2558 { 2424 {
2559 Peers_schedule_operation (peer, insert_in_sampler); 2425 schedule_operation (peer, insert_in_sampler);
2560 Peers_schedule_operation (peer, insert_in_view_op); 2426 schedule_operation (peer, insert_in_view_op);
2561 } 2427 }
2562} 2428}
2563 2429
@@ -2572,17 +2438,17 @@ static int
2572check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer) 2438check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer)
2573{ 2439{
2574 /* struct GNUNET_CADET_Channel *channel; */ 2440 /* struct GNUNET_CADET_Channel *channel; */
2575 if (GNUNET_NO == Peers_check_peer_known (peer)) 2441 if (GNUNET_NO == check_peer_known (peer))
2576 { 2442 {
2577 return GNUNET_NO; 2443 return GNUNET_NO;
2578 } 2444 }
2579 if (GNUNET_YES == Peers_check_sending_channel_exists (peer)) 2445 if (GNUNET_YES == check_sending_channel_exists (peer))
2580 { 2446 {
2581 if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) || 2447 if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) ||
2582 (GNUNET_YES == View_contains_peer (peer)) || 2448 (GNUNET_YES == View_contains_peer (peer)) ||
2583 (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) || 2449 (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) ||
2584 (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) || 2450 (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) ||
2585 (GNUNET_YES == Peers_check_peer_flag (peer, Peers_PULL_REPLY_PENDING))) 2451 (GNUNET_YES == check_peer_flag (peer, Peers_PULL_REPLY_PENDING)))
2586 { /* If we want to keep the connection to peer open */ 2452 { /* If we want to keep the connection to peer open */
2587 return GNUNET_YES; 2453 return GNUNET_YES;
2588 } 2454 }
@@ -2605,7 +2471,7 @@ remove_peer (const struct GNUNET_PeerIdentity *peer)
2605 CustomPeerMap_remove_peer (push_map, peer); 2471 CustomPeerMap_remove_peer (push_map, peer);
2606 RPS_sampler_reinitialise_by_value (prot_sampler, peer); 2472 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
2607 RPS_sampler_reinitialise_by_value (client_sampler, peer); 2473 RPS_sampler_reinitialise_by_value (client_sampler, peer);
2608 schedule_peer_destruction (get_peer_ctx (peer)); 2474 destroy_peer (get_peer_ctx (peer));
2609} 2475}
2610 2476
2611 2477
@@ -2626,19 +2492,19 @@ clean_peer (const struct GNUNET_PeerIdentity *peer)
2626 GNUNET_i2s (peer)); 2492 GNUNET_i2s (peer));
2627 #ifdef ENABLE_MALICIOUS 2493 #ifdef ENABLE_MALICIOUS
2628 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer)) 2494 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2629 (void) Peers_destroy_sending_channel (peer); 2495 (void) destroy_sending_channel (peer);
2630 #else /* ENABLE_MALICIOUS */ 2496 #else /* ENABLE_MALICIOUS */
2631 (void) Peers_destroy_sending_channel (peer); 2497 (void) destroy_sending_channel (peer);
2632 #endif /* ENABLE_MALICIOUS */ 2498 #endif /* ENABLE_MALICIOUS */
2633 } 2499 }
2634 2500
2635 if ( (GNUNET_NO == Peers_check_peer_send_intention (peer)) && 2501 if ( (GNUNET_NO == check_peer_send_intention (peer)) &&
2636 (GNUNET_NO == View_contains_peer (peer)) && 2502 (GNUNET_NO == View_contains_peer (peer)) &&
2637 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) && 2503 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2638 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) && 2504 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2639 (0 == RPS_sampler_count_id (prot_sampler, peer)) && 2505 (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2640 (0 == RPS_sampler_count_id (client_sampler, peer)) && 2506 (0 == RPS_sampler_count_id (client_sampler, peer)) &&
2641 (GNUNET_NO != Peers_check_removable (peer)) ) 2507 (GNUNET_NO != check_removable (peer)) )
2642 { /* We can safely remove this peer */ 2508 { /* We can safely remove this peer */
2643 LOG (GNUNET_ERROR_TYPE_DEBUG, 2509 LOG (GNUNET_ERROR_TYPE_DEBUG,
2644 "Going to remove peer %s\n", 2510 "Going to remove peer %s\n",
@@ -2648,53 +2514,6 @@ clean_peer (const struct GNUNET_PeerIdentity *peer)
2648 } 2514 }
2649} 2515}
2650 2516
2651/**
2652 * @brief Allocate memory for a new channel context and insert it into DLL
2653 *
2654 * @param peer_ctx context of the according peer
2655 *
2656 * @return The channel context
2657 */
2658static struct ChannelCtx *
2659add_channel_ctx (struct PeerContext *peer_ctx)
2660{
2661 struct ChannelCtx *channel_ctx;
2662 channel_ctx = GNUNET_new (struct ChannelCtx);
2663 channel_ctx->peer_ctx = peer_ctx;
2664 return channel_ctx;
2665}
2666
2667
2668/**
2669 * @brief Remove the channel context from the DLL and free the memory.
2670 *
2671 * @param channel_ctx The channel context.
2672 */
2673static void
2674remove_channel_ctx (struct ChannelCtx *channel_ctx)
2675{
2676 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
2677
2678 if (NULL != channel_ctx->destruction_task)
2679 {
2680 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task);
2681 }
2682 GNUNET_free (channel_ctx);
2683 if (channel_ctx == peer_ctx->send_channel_ctx)
2684 {
2685 peer_ctx->send_channel_ctx = NULL;
2686 peer_ctx->mq = NULL;
2687 }
2688 else if (channel_ctx == peer_ctx->recv_channel_ctx)
2689 {
2690 peer_ctx->recv_channel_ctx = NULL;
2691 }
2692 else
2693 {
2694 GNUNET_assert (0);
2695 }
2696}
2697
2698 2517
2699/** 2518/**
2700 * @brief This is called when a channel is destroyed. 2519 * @brief This is called when a channel is destroyed.
@@ -2719,7 +2538,7 @@ cleanup_destroyed_channel (void *cls,
2719 // * cleanup everything related to the channel 2538 // * cleanup everything related to the channel
2720 // * memory 2539 // * memory
2721 // * remove peer if necessary 2540 // * remove peer if necessary
2722 2541 channel_ctx->channel = NULL;
2723 if (peer_ctx->recv_channel_ctx == channel_ctx) 2542 if (peer_ctx->recv_channel_ctx == channel_ctx)
2724 { 2543 {
2725 remove_channel_ctx (channel_ctx); 2544 remove_channel_ctx (channel_ctx);
@@ -3184,7 +3003,7 @@ handle_peer_push (void *cls,
3184 /* Add the sending peer to the push_map */ 3003 /* Add the sending peer to the push_map */
3185 CustomPeerMap_put (push_map, peer); 3004 CustomPeerMap_put (push_map, peer);
3186 3005
3187 GNUNET_break_op (Peers_check_peer_known (peer)); 3006 GNUNET_break_op (check_peer_known (peer));
3188 GNUNET_CADET_receive_done (channel_ctx->channel); 3007 GNUNET_CADET_receive_done (channel_ctx->channel);
3189} 3008}
3190 3009
@@ -3224,7 +3043,7 @@ handle_peer_pull_request (void *cls,
3224 } 3043 }
3225 #endif /* ENABLE_MALICIOUS */ 3044 #endif /* ENABLE_MALICIOUS */
3226 3045
3227 GNUNET_break_op (Peers_check_peer_known (peer)); 3046 GNUNET_break_op (check_peer_known (peer));
3228 GNUNET_CADET_receive_done (channel_ctx->channel); 3047 GNUNET_CADET_receive_done (channel_ctx->channel);
3229 view_array = View_get_as_array (); 3048 view_array = View_get_as_array ();
3230 send_pull_reply (peer, view_array, View_size ()); 3049 send_pull_reply (peer, view_array, View_size ());
@@ -3262,7 +3081,7 @@ check_peer_pull_reply (void *cls,
3262 return GNUNET_SYSERR; 3081 return GNUNET_SYSERR;
3263 } 3082 }
3264 3083
3265 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING)) 3084 if (GNUNET_YES != check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
3266 { 3085 {
3267 LOG (GNUNET_ERROR_TYPE_WARNING, 3086 LOG (GNUNET_ERROR_TYPE_WARNING,
3268 "Received a pull reply from a peer (%s) we didn't request one from!\n", 3087 "Received a pull reply from a peer (%s) we didn't request one from!\n",
@@ -3336,23 +3155,23 @@ handle_peer_pull_reply (void *cls,
3336 } 3155 }
3337 #endif /* ENABLE_MALICIOUS */ 3156 #endif /* ENABLE_MALICIOUS */
3338 /* Make sure we 'know' about this peer */ 3157 /* Make sure we 'know' about this peer */
3339 (void) Peers_insert_peer (&peers[i]); 3158 (void) insert_peer (&peers[i]);
3340 3159
3341 if (GNUNET_YES == Peers_check_peer_valid (&peers[i])) 3160 if (GNUNET_YES == check_peer_valid (&peers[i]))
3342 { 3161 {
3343 CustomPeerMap_put (pull_map, &peers[i]); 3162 CustomPeerMap_put (pull_map, &peers[i]);
3344 } 3163 }
3345 else 3164 else
3346 { 3165 {
3347 Peers_schedule_operation (&peers[i], insert_in_pull_map); 3166 schedule_operation (&peers[i], insert_in_pull_map);
3348 (void) Peers_issue_peer_liveliness_check (&peers[i]); 3167 (void) issue_peer_liveliness_check (&peers[i]);
3349 } 3168 }
3350 } 3169 }
3351 3170
3352 Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING); 3171 UNSET_PEER_FLAG (get_peer_ctx (sender), Peers_PULL_REPLY_PENDING);
3353 clean_peer (sender); 3172 clean_peer (sender);
3354 3173
3355 GNUNET_break_op (Peers_check_peer_known (sender)); 3174 GNUNET_break_op (check_peer_known (sender));
3356 GNUNET_CADET_receive_done (channel_ctx->channel); 3175 GNUNET_CADET_receive_done (channel_ctx->channel);
3357} 3176}
3358 3177
@@ -3416,16 +3235,16 @@ send_pull_request (const struct GNUNET_PeerIdentity *peer)
3416{ 3235{
3417 struct GNUNET_MQ_Envelope *ev; 3236 struct GNUNET_MQ_Envelope *ev;
3418 3237
3419 GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer, 3238 GNUNET_assert (GNUNET_NO == check_peer_flag (peer,
3420 Peers_PULL_REPLY_PENDING)); 3239 Peers_PULL_REPLY_PENDING));
3421 Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING); 3240 SET_PEER_FLAG (get_peer_ctx (peer), Peers_PULL_REPLY_PENDING);
3422 3241
3423 LOG (GNUNET_ERROR_TYPE_DEBUG, 3242 LOG (GNUNET_ERROR_TYPE_DEBUG,
3424 "Going to send PULL REQUEST to peer %s.\n", 3243 "Going to send PULL REQUEST to peer %s.\n",
3425 GNUNET_i2s (peer)); 3244 GNUNET_i2s (peer));
3426 3245
3427 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST); 3246 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
3428 Peers_send_message (peer, ev, "PULL REQUEST"); 3247 send_message (peer, ev, "PULL REQUEST");
3429 GNUNET_STATISTICS_update(stats, "# pull request send issued", 1, GNUNET_NO); 3248 GNUNET_STATISTICS_update(stats, "# pull request send issued", 1, GNUNET_NO);
3430} 3249}
3431 3250
@@ -3445,7 +3264,7 @@ send_push (const struct GNUNET_PeerIdentity *peer_id)
3445 GNUNET_i2s (peer_id)); 3264 GNUNET_i2s (peer_id));
3446 3265
3447 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH); 3266 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
3448 Peers_send_message (peer_id, ev, "PUSH"); 3267 send_message (peer_id, ev, "PUSH");
3449 GNUNET_STATISTICS_update(stats, "# push send issued", 1, GNUNET_NO); 3268 GNUNET_STATISTICS_update(stats, "# push send issued", 1, GNUNET_NO);
3450} 3269}
3451 3270
@@ -3569,9 +3388,9 @@ handle_client_act_malicious (void *cls,
3569 &msg->attacked_peer, 3388 &msg->attacked_peer,
3570 sizeof (struct GNUNET_PeerIdentity)); 3389 sizeof (struct GNUNET_PeerIdentity));
3571 /* Set the flag of the attacked peer to valid to avoid problems */ 3390 /* Set the flag of the attacked peer to valid to avoid problems */
3572 if (GNUNET_NO == Peers_check_peer_known (&attacked_peer)) 3391 if (GNUNET_NO == check_peer_known (&attacked_peer))
3573 { 3392 {
3574 (void) Peers_issue_peer_liveliness_check (&attacked_peer); 3393 (void) issue_peer_liveliness_check (&attacked_peer);
3575 } 3394 }
3576 3395
3577 LOG (GNUNET_ERROR_TYPE_DEBUG, 3396 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -3661,8 +3480,8 @@ do_mal_round (void *cls)
3661 * Send as many pushes to the attacked peer as possible 3480 * Send as many pushes to the attacked peer as possible
3662 * That is one push per round as it will ignore more. 3481 * That is one push per round as it will ignore more.
3663 */ 3482 */
3664 (void) Peers_issue_peer_liveliness_check (&attacked_peer); 3483 (void) issue_peer_liveliness_check (&attacked_peer);
3665 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE)) 3484 if (GNUNET_YES == check_peer_flag (&attacked_peer, Peers_ONLINE))
3666 send_push (&attacked_peer); 3485 send_push (&attacked_peer);
3667 } 3486 }
3668 3487
@@ -3671,10 +3490,10 @@ do_mal_round (void *cls)
3671 { /* Combined attack */ 3490 { /* Combined attack */
3672 3491
3673 /* Send PUSH to attacked peers */ 3492 /* Send PUSH to attacked peers */
3674 if (GNUNET_YES == Peers_check_peer_known (&attacked_peer)) 3493 if (GNUNET_YES == check_peer_known (&attacked_peer))
3675 { 3494 {
3676 (void) Peers_issue_peer_liveliness_check (&attacked_peer); 3495 (void) issue_peer_liveliness_check (&attacked_peer);
3677 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE)) 3496 if (GNUNET_YES == check_peer_flag (&attacked_peer, Peers_ONLINE))
3678 { 3497 {
3679 LOG (GNUNET_ERROR_TYPE_DEBUG, 3498 LOG (GNUNET_ERROR_TYPE_DEBUG,
3680 "Goding to send push to attacked peer (%s)\n", 3499 "Goding to send push to attacked peer (%s)\n",
@@ -3682,7 +3501,7 @@ do_mal_round (void *cls)
3682 send_push (&attacked_peer); 3501 send_push (&attacked_peer);
3683 } 3502 }
3684 } 3503 }
3685 (void) Peers_issue_peer_liveliness_check (&attacked_peer); 3504 (void) issue_peer_liveliness_check (&attacked_peer);
3686 3505
3687 /* The maximum of pushes we're going to send this round */ 3506 /* The maximum of pushes we're going to send this round */
3688 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1, 3507 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
@@ -3799,7 +3618,7 @@ do_round (void *cls)
3799 for (i = first_border; i < second_border; i++) 3618 for (i = first_border; i < second_border; i++)
3800 { 3619 {
3801 peer = view_array[permut[i]]; 3620 peer = view_array[permut[i]];
3802 if ( GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) 3621 if ( GNUNET_NO == check_peer_flag (&peer, Peers_PULL_REPLY_PENDING))
3803 { // FIXME if this fails schedule/loop this for later 3622 { // FIXME if this fails schedule/loop this for later
3804 send_pull_request (&peer); 3623 send_pull_request (&peer);
3805 } 3624 }
@@ -4098,13 +3917,14 @@ shutdown_task (void *cls)
4098 do_round_task = NULL; 3917 do_round_task = NULL;
4099 } 3918 }
4100 3919
4101 Peers_terminate (); 3920 peers_terminate ();
4102 3921
4103 GNUNET_NSE_disconnect (nse); 3922 GNUNET_NSE_disconnect (nse);
4104 RPS_sampler_destroy (prot_sampler); 3923 RPS_sampler_destroy (prot_sampler);
4105 RPS_sampler_destroy (client_sampler); 3924 RPS_sampler_destroy (client_sampler);
4106 GNUNET_CADET_close_port (cadet_port); 3925 GNUNET_CADET_close_port (cadet_port);
4107 GNUNET_CADET_disconnect (cadet_handle); 3926 GNUNET_CADET_disconnect (cadet_handle);
3927 cadet_handle = NULL;
4108 View_destroy (); 3928 View_destroy ();
4109 CustomPeerMap_destroy (push_map); 3929 CustomPeerMap_destroy (push_map);
4110 CustomPeerMap_destroy (pull_map); 3930 CustomPeerMap_destroy (pull_map);
@@ -4315,7 +4135,7 @@ run (void *cls,
4315 &port); 4135 &port);
4316 cadet_port = GNUNET_CADET_open_port (cadet_handle, 4136 cadet_port = GNUNET_CADET_open_port (cadet_handle,
4317 &port, 4137 &port,
4318 &Peers_handle_inbound_channel, /* Connect handler */ 4138 &handle_inbound_channel, /* Connect handler */
4319 NULL, /* cls */ 4139 NULL, /* cls */
4320 NULL, /* WindowSize handler */ 4140 NULL, /* WindowSize handler */
4321 &cleanup_destroyed_channel, /* Disconnect handler */ 4141 &cleanup_destroyed_channel, /* Disconnect handler */
@@ -4330,7 +4150,7 @@ run (void *cls,
4330 4150
4331 4151
4332 peerinfo_handle = GNUNET_PEERINFO_connect (cfg); 4152 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
4333 Peers_initialise (fn_valid_peers, cadet_handle); 4153 initialise_peers (fn_valid_peers, cadet_handle);
4334 GNUNET_free (fn_valid_peers); 4154 GNUNET_free (fn_valid_peers);
4335 4155
4336 /* Initialise sampler */ 4156 /* Initialise sampler */
@@ -4353,7 +4173,7 @@ run (void *cls,
4353 // TODO send push/pull to each of those peers? 4173 // TODO send push/pull to each of those peers?
4354 // TODO read stored valid peers from last run 4174 // TODO read stored valid peers from last run
4355 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n"); 4175 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
4356 Peers_get_valid_peers (valid_peers_iterator, NULL); 4176 get_valid_peers (valid_peers_iterator, NULL);
4357 4177
4358 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg, 4178 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
4359 GNUNET_NO, 4179 GNUNET_NO,