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.c655
1 files changed, 318 insertions, 337 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index 5a75ac55a..21963ee42 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -68,6 +68,7 @@ static struct GNUNET_STATISTICS_Handle *stats;
68 */ 68 */
69static struct GNUNET_PeerIdentity own_identity; 69static struct GNUNET_PeerIdentity own_identity;
70 70
71static int in_shutdown = GNUNET_NO;
71 72
72/** 73/**
73 * @brief Port used for cadet. 74 * @brief Port used for cadet.
@@ -97,11 +98,6 @@ static struct GNUNET_HashCode port;
97#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))
98 99
99/** 100/**
100 * Set a channel flag of given channel context.
101 */
102#define set_channel_flag(channel_flags, mask) ((*channel_flags) |= (mask))
103
104/**
105 * Get channel flag of given channel context. 101 * Get channel flag of given channel context.
106 */ 102 */
107#define check_channel_flag_set(channel_flags, mask)\ 103#define check_channel_flag_set(channel_flags, mask)\
@@ -164,6 +160,11 @@ struct PendingMessage
164}; 160};
165 161
166/** 162/**
163 * @brief Context for a channel
164 */
165struct ChannelCtx;
166
167/**
167 * Struct used to keep track of other peer's status 168 * Struct used to keep track of other peer's status
168 * 169 *
169 * This is stored in a multipeermap. 170 * This is stored in a multipeermap.
@@ -181,22 +182,12 @@ struct PeerContext
181 /** 182 /**
182 * Channel open to client. 183 * Channel open to client.
183 */ 184 */
184 struct GNUNET_CADET_Channel *send_channel; 185 struct ChannelCtx *send_channel_ctx;
185
186 /**
187 * Flags to the sending channel
188 */
189 uint32_t *send_channel_flags;
190 186
191 /** 187 /**
192 * Channel open from client. 188 * Channel open from client.
193 */ 189 */
194 struct GNUNET_CADET_Channel *recv_channel; // unneeded? 190 struct ChannelCtx *recv_channel_ctx;
195
196 /**
197 * Flags to the receiving channel
198 */
199 uint32_t *recv_channel_flags;
200 191
201 /** 192 /**
202 * Array of pending operations on this peer. 193 * Array of pending operations on this peer.
@@ -242,6 +233,11 @@ struct PeerContext
242 struct PendingMessage *pending_messages_tail; 233 struct PendingMessage *pending_messages_tail;
243 234
244 /** 235 /**
236 * @brief Task to destroy this context.
237 */
238 struct GNUNET_SCHEDULER_Task *destruction_task;
239
240 /**
245 * This is pobably followed by 'statistical' data (when we first saw 241 * This is pobably followed by 'statistical' data (when we first saw
246 * it, how did we get its ID, how many pushes (in a timeinterval), 242 * it, how did we get its ID, how many pushes (in a timeinterval),
247 * ...) 243 * ...)
@@ -265,6 +261,33 @@ struct PeersIteratorCls
265}; 261};
266 262
267/** 263/**
264 * @brief Context for a channel
265 */
266struct ChannelCtx
267{
268 /**
269 * @brief Meant to be used in a DLL
270 */
271 struct ChannelCtx *next;
272 struct ChannelCtx *prev;
273
274 /**
275 * @brief The channel itself
276 */
277 struct GNUNET_CADET_Channel *channel;
278
279 /**
280 * @brief The peer context associated with the channel
281 */
282 struct PeerContext *peer_ctx;
283
284 /**
285 * @brief Scheduled task that will destroy this context
286 */
287 struct GNUNET_SCHEDULER_Task *destruction_task;
288};
289
290/**
268 * @brief Hashmap of valid peers. 291 * @brief Hashmap of valid peers.
269 */ 292 */
270static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers; 293static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
@@ -332,8 +355,6 @@ create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
332 355
333 ctx = GNUNET_new (struct PeerContext); 356 ctx = GNUNET_new (struct PeerContext);
334 ctx->peer_id = *peer; 357 ctx->peer_id = *peer;
335 ctx->send_channel_flags = GNUNET_new (uint32_t);
336 ctx->recv_channel_flags = GNUNET_new (uint32_t);
337 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx, 358 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 359 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
339 GNUNET_assert (GNUNET_OK == ret); 360 GNUNET_assert (GNUNET_OK == ret);
@@ -387,8 +408,8 @@ Peers_check_connected (const struct GNUNET_PeerIdentity *peer)
387 /* Get the context */ 408 /* Get the context */
388 peer_ctx = get_peer_ctx (peer); 409 peer_ctx = get_peer_ctx (peer);
389 /* If we have no channel to this peer we don't know whether it's online */ 410 /* If we have no channel to this peer we don't know whether it's online */
390 if ( (NULL == peer_ctx->send_channel) && 411 if ( (NULL == peer_ctx->send_channel_ctx) &&
391 (NULL == peer_ctx->recv_channel) ) 412 (NULL == peer_ctx->recv_channel_ctx) )
392 { 413 {
393 Peers_unset_peer_flag (peer, Peers_ONLINE); 414 Peers_unset_peer_flag (peer, Peers_ONLINE);
394 return GNUNET_NO; 415 return GNUNET_NO;
@@ -575,6 +596,24 @@ handle_peer_pull_reply (void *cls,
575 596
576/* End declaration of handlers */ 597/* End declaration of handlers */
577 598
599/**
600 * @brief Allocate memory for a new channel context and insert it into DLL
601 *
602 * @param peer_ctx context of the according peer
603 *
604 * @return The channel context
605 */
606static struct ChannelCtx *
607add_channel_ctx (struct PeerContext *peer_ctx);
608
609/**
610 * @brief Remove the channel context from the DLL and free the memory.
611 *
612 * @param channel_ctx The channel context.
613 */
614static void
615remove_channel_ctx (struct ChannelCtx *channel_ctx);
616
578 617
579/** 618/**
580 * @brief Get the channel of a peer. If not existing, create. 619 * @brief Get the channel of a peer. If not existing, create.
@@ -610,16 +649,17 @@ get_channel (const struct GNUNET_PeerIdentity *peer)
610 649
611 650
612 peer_ctx = get_peer_ctx (peer); 651 peer_ctx = get_peer_ctx (peer);
613 if (NULL == peer_ctx->send_channel) 652 if (NULL == peer_ctx->send_channel_ctx)
614 { 653 {
615 LOG (GNUNET_ERROR_TYPE_DEBUG, 654 LOG (GNUNET_ERROR_TYPE_DEBUG,
616 "Trying to establish channel to peer %s\n", 655 "Trying to establish channel to peer %s\n",
617 GNUNET_i2s (peer)); 656 GNUNET_i2s (peer));
618 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity); 657 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity);
619 *ctx_peer = *peer; 658 *ctx_peer = *peer;
620 peer_ctx->send_channel = 659 peer_ctx->send_channel_ctx = add_channel_ctx (peer_ctx);
660 peer_ctx->send_channel_ctx->channel =
621 GNUNET_CADET_channel_create (cadet_handle, 661 GNUNET_CADET_channel_create (cadet_handle,
622 (struct GNUNET_PeerIdentity *) ctx_peer, /* context */ 662 peer_ctx->send_channel_ctx, /* context */
623 peer, 663 peer,
624 &port, 664 &port,
625 GNUNET_CADET_OPTION_RELIABLE, 665 GNUNET_CADET_OPTION_RELIABLE,
@@ -627,8 +667,9 @@ get_channel (const struct GNUNET_PeerIdentity *peer)
627 cleanup_destroyed_channel, /* Disconnect handler */ 667 cleanup_destroyed_channel, /* Disconnect handler */
628 cadet_handlers); 668 cadet_handlers);
629 } 669 }
630 GNUNET_assert (NULL != peer_ctx->send_channel); 670 GNUNET_assert (NULL != peer_ctx->send_channel_ctx);
631 return peer_ctx->send_channel; 671 GNUNET_assert (NULL != peer_ctx->send_channel_ctx->channel);
672 return peer_ctx->send_channel_ctx->channel;
632} 673}
633 674
634 675
@@ -1045,12 +1086,10 @@ restore_valid_peers ()
1045 */ 1086 */
1046void 1087void
1047Peers_initialise (char* fn_valid_peers, 1088Peers_initialise (char* fn_valid_peers,
1048 struct GNUNET_CADET_Handle *cadet_h, 1089 struct GNUNET_CADET_Handle *cadet_h)
1049 const struct GNUNET_PeerIdentity *own_id)
1050{ 1090{
1051 filename_valid_peers = GNUNET_strdup (fn_valid_peers); 1091 filename_valid_peers = GNUNET_strdup (fn_valid_peers);
1052 cadet_handle = cadet_h; 1092 cadet_handle = cadet_h;
1053 own_identity = *own_id;
1054 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 1093 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1055 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 1094 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1056 restore_valid_peers (); 1095 restore_valid_peers ();
@@ -1136,14 +1175,12 @@ Peers_get_valid_peers (PeersIterator iterator,
1136 * @param peer the new #GNUNET_PeerIdentity 1175 * @param peer the new #GNUNET_PeerIdentity
1137 * 1176 *
1138 * @return #GNUNET_YES if peer was inserted 1177 * @return #GNUNET_YES if peer was inserted
1139 * #GNUNET_NO otherwise (if peer was already known or 1178 * #GNUNET_NO otherwise
1140 * peer was #own_identity)
1141 */ 1179 */
1142int 1180int
1143Peers_insert_peer (const struct GNUNET_PeerIdentity *peer) 1181Peers_insert_peer (const struct GNUNET_PeerIdentity *peer)
1144{ 1182{
1145 if ( (GNUNET_YES == Peers_check_peer_known (peer)) || 1183 if (GNUNET_YES == Peers_check_peer_known (peer))
1146 (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity)) )
1147 { 1184 {
1148 return GNUNET_NO; /* We already know this peer - nothing to do */ 1185 return GNUNET_NO; /* We already know this peer - nothing to do */
1149 } 1186 }
@@ -1161,8 +1198,7 @@ Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFl
1161 * 1198 *
1162 * @param peer the peer whose liveliness is to be checked 1199 * @param peer the peer whose liveliness is to be checked
1163 * @return #GNUNET_YES if peer had to be inserted 1200 * @return #GNUNET_YES if peer had to be inserted
1164 * #GNUNET_NO otherwise (if peer was already known or 1201 * #GNUNET_NO otherwise
1165 * peer was #own_identity)
1166 */ 1202 */
1167int 1203int
1168Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer) 1204Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
@@ -1170,13 +1206,10 @@ Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
1170 struct PeerContext *peer_ctx; 1206 struct PeerContext *peer_ctx;
1171 int ret; 1207 int ret;
1172 1208
1173 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1174 {
1175 return GNUNET_NO;
1176 }
1177 ret = Peers_insert_peer (peer); 1209 ret = Peers_insert_peer (peer);
1178 peer_ctx = get_peer_ctx (peer); 1210 peer_ctx = get_peer_ctx (peer);
1179 if (GNUNET_NO == Peers_check_peer_flag (peer, Peers_ONLINE)) 1211 if ( (GNUNET_NO == Peers_check_peer_flag (peer, Peers_ONLINE)) &&
1212 (NULL == peer_ctx->liveliness_check_pending) )
1180 { 1213 {
1181 check_peer_live (peer_ctx); 1214 check_peer_live (peer_ctx);
1182 } 1215 }
@@ -1208,7 +1241,7 @@ Peers_check_removable (const struct GNUNET_PeerIdentity *peer)
1208 } 1241 }
1209 1242
1210 peer_ctx = get_peer_ctx (peer); 1243 peer_ctx = get_peer_ctx (peer);
1211 if ( (NULL != peer_ctx->recv_channel) || 1244 if ( (NULL != peer_ctx->recv_channel_ctx) ||
1212 (NULL != peer_ctx->pending_messages_head) || 1245 (NULL != peer_ctx->pending_messages_head) ||
1213 (GNUNET_NO == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)) ) 1246 (GNUNET_NO == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)) )
1214 { 1247 {
@@ -1224,6 +1257,46 @@ Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1224int 1257int
1225Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags); 1258Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags);
1226 1259
1260static void
1261destroy_peer (void *cls)
1262{
1263 struct PeerContext *peer_ctx = cls;
1264
1265 GNUNET_assert (NULL != peer_ctx);
1266 peer_ctx->destruction_task = NULL;
1267 Peers_remove_peer (&peer_ctx->peer_id);
1268}
1269
1270static void
1271destroy_channel (void *cls);
1272
1273
1274static void
1275schedule_channel_destruction (struct ChannelCtx *channel_ctx)
1276{
1277 GNUNET_assert (NULL != channel_ctx);
1278 if (NULL != channel_ctx->destruction_task &&
1279 GNUNET_NO == in_shutdown)
1280 {
1281 channel_ctx->destruction_task =
1282 GNUNET_SCHEDULER_add_now (destroy_channel, channel_ctx);
1283 }
1284}
1285
1286
1287static void
1288schedule_peer_destruction (struct PeerContext *peer_ctx)
1289{
1290 GNUNET_assert (NULL != peer_ctx);
1291 if (NULL != peer_ctx->destruction_task &&
1292 GNUNET_NO == in_shutdown)
1293 {
1294 peer_ctx->destruction_task =
1295 GNUNET_SCHEDULER_add_now (destroy_peer, peer_ctx);
1296 }
1297}
1298
1299
1227/** 1300/**
1228 * @brief Remove peer 1301 * @brief Remove peer
1229 * 1302 *
@@ -1235,7 +1308,8 @@ int
1235Peers_remove_peer (const struct GNUNET_PeerIdentity *peer) 1308Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1236{ 1309{
1237 struct PeerContext *peer_ctx; 1310 struct PeerContext *peer_ctx;
1238 uint32_t *channel_flag; 1311
1312 GNUNET_assert (NULL != peer_map);
1239 1313
1240 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) 1314 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1241 { 1315 {
@@ -1249,7 +1323,12 @@ Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1249 GNUNET_i2s (&peer_ctx->peer_id)); 1323 GNUNET_i2s (&peer_ctx->peer_id));
1250 Peers_unset_peer_flag (peer, Peers_ONLINE); 1324 Peers_unset_peer_flag (peer, Peers_ONLINE);
1251 1325
1326 /* Clear list of pending operations */
1327 // TODO this probably leaks memory
1328 // ('only' the cls to the function. Not sure what to do with it)
1252 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0); 1329 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
1330
1331 /* Remove all pending messages */
1253 while (NULL != peer_ctx->pending_messages_head) 1332 while (NULL != peer_ctx->pending_messages_head)
1254 { 1333 {
1255 LOG (GNUNET_ERROR_TYPE_DEBUG, 1334 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1261,10 +1340,12 @@ Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1261 peer_ctx->liveliness_check_pending, 1340 peer_ctx->liveliness_check_pending,
1262 sizeof (struct PendingMessage))) ) 1341 sizeof (struct PendingMessage))) )
1263 { 1342 {
1343 // TODO this may leak memory
1264 peer_ctx->liveliness_check_pending = NULL; 1344 peer_ctx->liveliness_check_pending = NULL;
1265 } 1345 }
1266 remove_pending_message (peer_ctx->pending_messages_head, GNUNET_YES); 1346 remove_pending_message (peer_ctx->pending_messages_head, GNUNET_YES);
1267 } 1347 }
1348
1268 /* If we are still waiting for notification whether this peer is live 1349 /* If we are still waiting for notification whether this peer is live
1269 * cancel the according task */ 1350 * cancel the according task */
1270 if (NULL != peer_ctx->liveliness_check_pending) 1351 if (NULL != peer_ctx->liveliness_check_pending)
@@ -1277,28 +1358,40 @@ Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1277 remove_pending_message (peer_ctx->liveliness_check_pending, GNUNET_YES); 1358 remove_pending_message (peer_ctx->liveliness_check_pending, GNUNET_YES);
1278 peer_ctx->liveliness_check_pending = NULL; 1359 peer_ctx->liveliness_check_pending = NULL;
1279 } 1360 }
1280 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING); 1361
1281 if (NULL != peer_ctx->send_channel && 1362
1282 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING)) 1363 /* Do we still have to wait for destruction of channels
1364 * or issue the destruction? */
1365 if (NULL != peer_ctx->send_channel_ctx &&
1366 NULL != peer_ctx->send_channel_ctx->destruction_task
1367 )
1283 { 1368 {
1284 LOG (GNUNET_ERROR_TYPE_DEBUG, 1369 schedule_peer_destruction (peer_ctx);
1285 "Destroying send channel\n"); 1370 return GNUNET_NO;
1286 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1287 peer_ctx->send_channel = NULL;
1288 peer_ctx->mq = NULL;
1289 } 1371 }
1290 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING); 1372 if (NULL != peer_ctx->recv_channel_ctx &&
1291 if (NULL != peer_ctx->recv_channel && 1373 NULL != peer_ctx->recv_channel_ctx->destruction_task)
1292 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING))
1293 { 1374 {
1294 LOG (GNUNET_ERROR_TYPE_DEBUG, 1375 schedule_peer_destruction (peer_ctx);
1295 "Destroying recv channel\n"); 1376 return GNUNET_NO;
1296 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel); 1377 }
1297 peer_ctx->recv_channel = NULL; 1378 if (NULL != peer_ctx->recv_channel_ctx)
1379 {
1380 schedule_channel_destruction (peer_ctx->recv_channel_ctx);
1381 schedule_peer_destruction (peer_ctx);
1382 return GNUNET_NO;
1383 }
1384 if (NULL != peer_ctx->send_channel_ctx)
1385 {
1386 schedule_channel_destruction (peer_ctx->send_channel_ctx);
1387 schedule_peer_destruction (peer_ctx);
1388 return GNUNET_NO;
1298 } 1389 }
1299 1390
1300 GNUNET_free (peer_ctx->send_channel_flags); 1391 if (NULL != peer_ctx->destruction_task)
1301 GNUNET_free (peer_ctx->recv_channel_flags); 1392 {
1393 GNUNET_SCHEDULER_cancel (peer_ctx->destruction_task);
1394 }
1302 1395
1303 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, &peer_ctx->peer_id)) 1396 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, &peer_ctx->peer_id))
1304 { 1397 {
@@ -1308,7 +1401,6 @@ Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1308 return GNUNET_YES; 1401 return GNUNET_YES;
1309} 1402}
1310 1403
1311
1312/** 1404/**
1313 * @brief set flags on a given peer. 1405 * @brief set flags on a given peer.
1314 * 1406 *
@@ -1364,77 +1456,6 @@ Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFl
1364 return check_peer_flag_set (peer_ctx, flags); 1456 return check_peer_flag_set (peer_ctx, flags);
1365} 1457}
1366 1458
1367
1368/**
1369 * @brief set flags on a given channel.
1370 *
1371 * @param channel the channel to set flags on
1372 * @param flags the flags
1373 */
1374void
1375Peers_set_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1376{
1377 set_channel_flag (channel_flags, flags);
1378}
1379
1380
1381/**
1382 * @brief unset flags on a given channel.
1383 *
1384 * @param channel the channel to unset flags on
1385 * @param flags the flags
1386 */
1387void
1388Peers_unset_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1389{
1390 unset_channel_flag (channel_flags, flags);
1391}
1392
1393
1394/**
1395 * @brief Check whether flags on a channel are set.
1396 *
1397 * @param channel the channel to check the flag of
1398 * @param flags the flags to check
1399 *
1400 * @return #GNUNET_YES if all given flags are set
1401 * #GNUNET_NO otherwise
1402 */
1403int
1404Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1405{
1406 return check_channel_flag_set (channel_flags, flags);
1407}
1408
1409/**
1410 * @brief Get the flags for the channel in @a role for @a peer.
1411 *
1412 * @param peer Peer to get the channel flags for.
1413 * @param role Role of channel to get flags for
1414 *
1415 * @return The flags.
1416 */
1417uint32_t *
1418Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1419 enum Peers_ChannelRole role)
1420{
1421 const struct PeerContext *peer_ctx;
1422
1423 peer_ctx = get_peer_ctx (peer);
1424 if (Peers_CHANNEL_ROLE_SENDING == role)
1425 {
1426 return peer_ctx->send_channel_flags;
1427 }
1428 else if (Peers_CHANNEL_ROLE_RECEIVING == role)
1429 {
1430 return peer_ctx->recv_channel_flags;
1431 }
1432 else
1433 {
1434 GNUNET_assert (0);
1435 }
1436}
1437
1438/** 1459/**
1439 * @brief Check whether we have information about the given peer. 1460 * @brief Check whether we have information about the given peer.
1440 * 1461 *
@@ -1505,7 +1526,7 @@ Peers_check_peer_send_intention (const struct GNUNET_PeerIdentity *peer)
1505 const struct PeerContext *peer_ctx; 1526 const struct PeerContext *peer_ctx;
1506 1527
1507 peer_ctx = get_peer_ctx (peer); 1528 peer_ctx = get_peer_ctx (peer);
1508 if (NULL != peer_ctx->recv_channel) 1529 if (NULL != peer_ctx->recv_channel_ctx)
1509 { 1530 {
1510 return GNUNET_YES; 1531 return GNUNET_YES;
1511 } 1532 }
@@ -1530,6 +1551,7 @@ Peers_handle_inbound_channel (void *cls,
1530{ 1551{
1531 struct PeerContext *peer_ctx; 1552 struct PeerContext *peer_ctx;
1532 struct GNUNET_PeerIdentity *ctx_peer; 1553 struct GNUNET_PeerIdentity *ctx_peer;
1554 struct ChannelCtx *channel_ctx;
1533 1555
1534 LOG (GNUNET_ERROR_TYPE_DEBUG, 1556 LOG (GNUNET_ERROR_TYPE_DEBUG,
1535 "New channel was established to us (Peer %s).\n", 1557 "New channel was established to us (Peer %s).\n",
@@ -1540,19 +1562,22 @@ Peers_handle_inbound_channel (void *cls,
1540 set_peer_live (peer_ctx); 1562 set_peer_live (peer_ctx);
1541 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity); 1563 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity);
1542 *ctx_peer = *initiator; 1564 *ctx_peer = *initiator;
1565 channel_ctx = add_channel_ctx (peer_ctx);
1566 channel_ctx->channel = channel;
1543 /* We only accept one incoming channel per peer */ 1567 /* We only accept one incoming channel per peer */
1544 if (GNUNET_YES == Peers_check_peer_send_intention (initiator)) 1568 if (GNUNET_YES == Peers_check_peer_send_intention (initiator))
1545 { 1569 {
1546 set_channel_flag (peer_ctx->recv_channel_flags, 1570 LOG (GNUNET_ERROR_TYPE_WARNING,
1547 Peers_CHANNEL_ESTABLISHED_TWICE); 1571 "Already got one receive channel. Destroying old one.\n");
1548 //GNUNET_CADET_channel_destroy (channel); 1572 GNUNET_break_op (0);
1549 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel); 1573 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel_ctx->channel);
1550 peer_ctx->recv_channel = channel; 1574 remove_channel_ctx (peer_ctx->recv_channel_ctx);
1575 peer_ctx->recv_channel_ctx = channel_ctx;
1551 /* return the channel context */ 1576 /* return the channel context */
1552 return ctx_peer; 1577 return channel_ctx;
1553 } 1578 }
1554 peer_ctx->recv_channel = channel; 1579 peer_ctx->recv_channel_ctx = channel_ctx;
1555 return ctx_peer; 1580 return channel_ctx;
1556} 1581}
1557 1582
1558 1583
@@ -1574,7 +1599,7 @@ Peers_check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer)
1574 return GNUNET_NO; 1599 return GNUNET_NO;
1575 } 1600 }
1576 peer_ctx = get_peer_ctx (peer); 1601 peer_ctx = get_peer_ctx (peer);
1577 if (NULL == peer_ctx->send_channel) 1602 if (NULL == peer_ctx->send_channel_ctx)
1578 { 1603 {
1579 return GNUNET_NO; 1604 return GNUNET_NO;
1580 } 1605 }
@@ -1607,12 +1632,14 @@ Peers_check_channel_role (const struct GNUNET_PeerIdentity *peer,
1607 } 1632 }
1608 peer_ctx = get_peer_ctx (peer); 1633 peer_ctx = get_peer_ctx (peer);
1609 if ( (Peers_CHANNEL_ROLE_SENDING == role) && 1634 if ( (Peers_CHANNEL_ROLE_SENDING == role) &&
1610 (channel == peer_ctx->send_channel) ) 1635 (NULL != peer_ctx->send_channel_ctx) &&
1636 (channel == peer_ctx->send_channel_ctx->channel) )
1611 { 1637 {
1612 return GNUNET_YES; 1638 return GNUNET_YES;
1613 } 1639 }
1614 if ( (Peers_CHANNEL_ROLE_RECEIVING == role) && 1640 if ( (Peers_CHANNEL_ROLE_RECEIVING == role) &&
1615 (channel == peer_ctx->recv_channel) ) 1641 (NULL != peer_ctx->recv_channel_ctx) &&
1642 (channel == peer_ctx->recv_channel_ctx->channel) )
1616 { 1643 {
1617 return GNUNET_YES; 1644 return GNUNET_YES;
1618 } 1645 }
@@ -1642,18 +1669,29 @@ Peers_destroy_sending_channel (const struct GNUNET_PeerIdentity *peer)
1642 return GNUNET_NO; 1669 return GNUNET_NO;
1643 } 1670 }
1644 peer_ctx = get_peer_ctx (peer); 1671 peer_ctx = get_peer_ctx (peer);
1645 if (NULL != peer_ctx->send_channel) 1672 if (NULL != peer_ctx->send_channel_ctx)
1646 { 1673 {
1647 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_CLEAN); 1674 schedule_channel_destruction (peer_ctx->send_channel_ctx);
1648 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1649 peer_ctx->send_channel = NULL;
1650 peer_ctx->mq = NULL;
1651 (void) Peers_check_connected (peer); 1675 (void) Peers_check_connected (peer);
1652 return GNUNET_YES; 1676 return GNUNET_YES;
1653 } 1677 }
1654 return GNUNET_NO; 1678 return GNUNET_NO;
1655} 1679}
1656 1680
1681static void
1682destroy_channel (void *cls)
1683{
1684 struct ChannelCtx *channel_ctx = cls;
1685 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
1686
1687 GNUNET_assert (channel_ctx == peer_ctx->send_channel_ctx ||
1688 channel_ctx == peer_ctx->recv_channel_ctx);
1689
1690 channel_ctx->destruction_task = NULL;
1691 GNUNET_CADET_channel_destroy (channel_ctx->channel);
1692 remove_channel_ctx (peer_ctx->send_channel_ctx);
1693}
1694
1657/** 1695/**
1658 * This is called when a channel is destroyed. 1696 * This is called when a channel is destroyed.
1659 * 1697 *
@@ -1664,9 +1702,9 @@ void
1664Peers_cleanup_destroyed_channel (void *cls, 1702Peers_cleanup_destroyed_channel (void *cls,
1665 const struct GNUNET_CADET_Channel *channel) 1703 const struct GNUNET_CADET_Channel *channel)
1666{ 1704{
1667 struct GNUNET_PeerIdentity *peer = cls; 1705 struct ChannelCtx *channel_ctx = cls;
1668 struct PeerContext *peer_ctx; 1706 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
1669 uint32_t *channel_flag; 1707 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
1670 1708
1671 if (GNUNET_NO == Peers_check_peer_known (peer)) 1709 if (GNUNET_NO == Peers_check_peer_known (peer))
1672 {/* We don't want to implicitly create a context that we're about to kill */ 1710 {/* We don't want to implicitly create a context that we're about to kill */
@@ -1675,71 +1713,34 @@ Peers_cleanup_destroyed_channel (void *cls,
1675 GNUNET_i2s (peer)); 1713 GNUNET_i2s (peer));
1676 return; 1714 return;
1677 } 1715 }
1678 peer_ctx = get_peer_ctx (peer);
1679 1716
1680 /* If our peer issued the destruction of the channel, the #Peers_TO_DESTROY 1717 /* If our peer issued the destruction of the channel, the #Peers_TO_DESTROY
1681 * flag will be set. In this case simply make sure that the channels are 1718 * flag will be set. In this case simply make sure that the channels are
1682 * cleaned. */ 1719 * cleaned. */
1683 /* FIXME This distinction seems to be redundant */ 1720 /* The distinction seems to be redundant */
1684 if (Peers_check_peer_flag (peer, Peers_TO_DESTROY)) 1721 LOG (GNUNET_ERROR_TYPE_DEBUG,
1685 {/* We initiatad the destruction of this particular peer */ 1722 "Peer is NOT in the process of being destroyed\n");
1723 if ( (NULL != peer_ctx->send_channel_ctx) &&
1724 (channel == peer_ctx->send_channel_ctx->channel) )
1725 { /* Something (but us) killd the channel - clean up peer */
1686 LOG (GNUNET_ERROR_TYPE_DEBUG, 1726 LOG (GNUNET_ERROR_TYPE_DEBUG,
1687 "Peer is in the process of being destroyed\n"); 1727 "send channel (%s) was destroyed - cleaning up\n",
1688 if (channel == peer_ctx->send_channel) 1728 GNUNET_i2s (peer));
1689 { 1729 remove_channel_ctx (peer_ctx->send_channel_ctx);
1690 peer_ctx->send_channel = NULL;
1691 peer_ctx->mq = NULL;
1692 }
1693 else if (channel == peer_ctx->recv_channel)
1694 {
1695 peer_ctx->recv_channel = NULL;
1696 }
1697
1698 if (NULL != peer_ctx->send_channel)
1699 {
1700 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1701 channel_flag = Peers_get_channel_flag (&peer_ctx->peer_id, Peers_CHANNEL_ROLE_SENDING);
1702 Peers_set_channel_flag (channel_flag, Peers_CHANNEL_DESTROING);
1703 peer_ctx->send_channel = NULL;
1704 peer_ctx->mq = NULL;
1705 }
1706 if (NULL != peer_ctx->recv_channel)
1707 {
1708 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1709 channel_flag = Peers_get_channel_flag (&peer_ctx->peer_id, Peers_CHANNEL_ROLE_RECEIVING);
1710 Peers_set_channel_flag (channel_flag, Peers_CHANNEL_DESTROING);
1711 peer_ctx->recv_channel = NULL;
1712 }
1713 /* Set the #Peers_ONLINE flag accordingly */
1714 (void) Peers_check_connected (peer);
1715 return;
1716 } 1730 }
1717 1731 else if ( (NULL != peer_ctx->recv_channel_ctx) &&
1718 else 1732 (channel == peer_ctx->recv_channel_ctx->channel) )
1719 { /* We did not initiate the destruction of this peer */ 1733 { /* Other peer doesn't want to send us messages anymore */
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, 1734 LOG (GNUNET_ERROR_TYPE_DEBUG,
1721 "Peer is NOT in the process of being destroyed\n"); 1735 "Peer %s destroyed recv channel - cleaning up channel\n",
1722 if (channel == peer_ctx->send_channel) 1736 GNUNET_i2s (peer));
1723 { /* Something (but us) killd the channel - clean up peer */ 1737 remove_channel_ctx (peer_ctx->send_channel_ctx);
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, 1738 }
1725 "send channel (%s) was destroyed - cleaning up\n", 1739 else
1726 GNUNET_i2s (peer)); 1740 {
1727 peer_ctx->send_channel = NULL; 1741 LOG (GNUNET_ERROR_TYPE_WARNING,
1728 peer_ctx->mq = NULL; 1742 "unknown channel (%s) was destroyed\n",
1729 } 1743 GNUNET_i2s (peer));
1730 else if (channel == peer_ctx->recv_channel)
1731 { /* Other peer doesn't want to send us messages anymore */
1732 LOG (GNUNET_ERROR_TYPE_DEBUG,
1733 "Peer %s destroyed recv channel - cleaning up channel\n",
1734 GNUNET_i2s (peer));
1735 peer_ctx->recv_channel = NULL;
1736 }
1737 else
1738 {
1739 LOG (GNUNET_ERROR_TYPE_WARNING,
1740 "unknown channel (%s) was destroyed\n",
1741 GNUNET_i2s (peer));
1742 }
1743 } 1744 }
1744 (void) Peers_check_connected (peer); 1745 (void) Peers_check_connected (peer);
1745} 1746}
@@ -1791,10 +1792,6 @@ Peers_schedule_operation (const struct GNUNET_PeerIdentity *peer,
1791 struct PeerPendingOp pending_op; 1792 struct PeerPendingOp pending_op;
1792 struct PeerContext *peer_ctx; 1793 struct PeerContext *peer_ctx;
1793 1794
1794 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1795 {
1796 return GNUNET_NO;
1797 }
1798 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer)); 1795 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1799 1796
1800 //TODO if LIVE/ONLINE execute immediately 1797 //TODO if LIVE/ONLINE execute immediately
@@ -1828,7 +1825,7 @@ Peers_get_recv_channel (const struct GNUNET_PeerIdentity *peer)
1828 1825
1829 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer)); 1826 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1830 peer_ctx = get_peer_ctx (peer); 1827 peer_ctx = get_peer_ctx (peer);
1831 return peer_ctx->recv_channel; 1828 return peer_ctx->recv_channel_ctx->channel;
1832} 1829}
1833/*********************************************************************** 1830/***********************************************************************
1834 * /Old gnunet-service-rps_peers.c 1831 * /Old gnunet-service-rps_peers.c
@@ -2489,6 +2486,9 @@ send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
2489 2486
2490 Peers_send_message (peer_id, ev, "PULL REPLY"); 2487 Peers_send_message (peer_id, ev, "PULL REPLY");
2491 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO); 2488 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO);
2489 // TODO check with send intention: as send_channel is used/opened we indicate
2490 // a sending intention without intending it.
2491 // -> clean peer afterwards?
2492} 2492}
2493 2493
2494 2494
@@ -2621,7 +2621,7 @@ remove_peer (const struct GNUNET_PeerIdentity *peer)
2621 CustomPeerMap_remove_peer (push_map, peer); 2621 CustomPeerMap_remove_peer (push_map, peer);
2622 RPS_sampler_reinitialise_by_value (prot_sampler, peer); 2622 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
2623 RPS_sampler_reinitialise_by_value (client_sampler, peer); 2623 RPS_sampler_reinitialise_by_value (client_sampler, peer);
2624 Peers_remove_peer (peer); 2624 schedule_peer_destruction (get_peer_ctx (peer));
2625} 2625}
2626 2626
2627 2627
@@ -2665,6 +2665,58 @@ clean_peer (const struct GNUNET_PeerIdentity *peer)
2665} 2665}
2666 2666
2667/** 2667/**
2668 * @brief Allocate memory for a new channel context and insert it into DLL
2669 *
2670 * @param peer_ctx context of the according peer
2671 *
2672 * @return The channel context
2673 */
2674static struct ChannelCtx *
2675add_channel_ctx (struct PeerContext *peer_ctx)
2676{
2677 struct ChannelCtx *channel_ctx;
2678 channel_ctx = GNUNET_new (struct ChannelCtx);
2679 channel_ctx->peer_ctx = peer_ctx;
2680 return channel_ctx;
2681}
2682
2683/**
2684 * @brief Remove the channel context from the DLL and free the memory.
2685 *
2686 * @param channel_ctx The channel context.
2687 */
2688static void
2689remove_channel_ctx (struct ChannelCtx *channel_ctx)
2690{
2691 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
2692 if (NULL != channel_ctx->destruction_task)
2693 {
2694 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task);
2695 }
2696 GNUNET_free (channel_ctx);
2697
2698 if (channel_ctx == peer_ctx->send_channel_ctx)
2699 {
2700 peer_ctx->send_channel_ctx = NULL;
2701 peer_ctx->mq = NULL;
2702 }
2703 else if (channel_ctx == peer_ctx->recv_channel_ctx)
2704 {
2705 peer_ctx->recv_channel_ctx = NULL;
2706 }
2707 else
2708 {
2709 LOG (GNUNET_ERROR_TYPE_ERROR,
2710 "Trying to remove channel_ctx that is not associated with a peer\n");
2711 LOG (GNUNET_ERROR_TYPE_ERROR,
2712 "\trecv: %p\n", peer_ctx->recv_channel_ctx);
2713 LOG (GNUNET_ERROR_TYPE_ERROR,
2714 "\tsend: %p\n", peer_ctx->send_channel_ctx);
2715 GNUNET_assert (0);
2716 }
2717}
2718
2719/**
2668 * @brief This is called when a channel is destroyed. 2720 * @brief This is called when a channel is destroyed.
2669 * 2721 *
2670 * Removes peer completely from our knowledge if the send_channel was destroyed 2722 * Removes peer completely from our knowledge if the send_channel was destroyed
@@ -2680,8 +2732,8 @@ static void
2680cleanup_destroyed_channel (void *cls, 2732cleanup_destroyed_channel (void *cls,
2681 const struct GNUNET_CADET_Channel *channel) 2733 const struct GNUNET_CADET_Channel *channel)
2682{ 2734{
2683 struct GNUNET_PeerIdentity *peer = cls; 2735 struct ChannelCtx *channel_ctx = cls;
2684 uint32_t *channel_flag; 2736 struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
2685 struct PeerContext *peer_ctx; 2737 struct PeerContext *peer_ctx;
2686 2738
2687 GNUNET_assert (NULL != peer); 2739 GNUNET_assert (NULL != peer);
@@ -2691,94 +2743,26 @@ cleanup_destroyed_channel (void *cls,
2691 LOG (GNUNET_ERROR_TYPE_WARNING, 2743 LOG (GNUNET_ERROR_TYPE_WARNING,
2692 "channel (%s) without associated context was destroyed\n", 2744 "channel (%s) without associated context was destroyed\n",
2693 GNUNET_i2s (peer)); 2745 GNUNET_i2s (peer));
2694 GNUNET_free (peer); 2746 remove_channel_ctx (channel_ctx);
2695 return; 2747 return;
2696 } 2748 }
2697 2749
2698 peer_ctx = get_peer_ctx (peer); 2750 peer_ctx = get_peer_ctx (peer);
2699 if (GNUNET_YES == Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
2700 {
2701 LOG (GNUNET_ERROR_TYPE_DEBUG,
2702 "Callback on destruction of recv-channel was called (%s)\n",
2703 GNUNET_i2s (peer));
2704 set_channel_flag (peer_ctx->recv_channel_flags, Peers_CHANNEL_DESTROING);
2705 } else if (GNUNET_YES == Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
2706 {
2707 LOG (GNUNET_ERROR_TYPE_DEBUG,
2708 "Callback on destruction of send-channel was called (%s)\n",
2709 GNUNET_i2s (peer));
2710 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_DESTROING);
2711 } else {
2712 LOG (GNUNET_ERROR_TYPE_ERROR,
2713 "Channel to be destroyed has is neither sending nor receiving role\n");
2714 }
2715 2751
2716 if (GNUNET_YES == Peers_check_peer_flag (peer, Peers_TO_DESTROY)) 2752 // What should be done here:
2717 { /* We are in the middle of removing that peer from our knowledge. In this 2753 // * cleanup everything related to the channel
2718 case simply make sure that the channels are cleaned. */ 2754 // * memory
2719 Peers_cleanup_destroyed_channel (cls, channel); 2755 // * remove peer if necessary
2720 to_file (file_name_view_log,
2721 "-%s\t(cleanup channel, ourself)",
2722 GNUNET_i2s_full (peer));
2723 GNUNET_free (peer);
2724 return;
2725 }
2726 2756
2727 if (GNUNET_YES == 2757 if (peer_ctx->recv_channel_ctx == channel_ctx)
2728 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING)) 2758 {
2729 { /* Channel used for sending was destroyed */ 2759 remove_channel_ctx (channel_ctx);
2730 /* Possible causes of channel destruction:
2731 * - ourselves -> cleaning send channel -> clean context
2732 * - other peer -> peer probably went down -> remove
2733 */
2734 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
2735 if (GNUNET_YES == Peers_check_channel_flag (channel_flag, Peers_CHANNEL_CLEAN))
2736 { /* We are about to clean the sending channel. Clean the respective
2737 * context */
2738 Peers_cleanup_destroyed_channel (cls, channel);
2739 GNUNET_free (peer);
2740 return;
2741 }
2742 else
2743 { /* Other peer destroyed our sending channel that it is supposed to keep
2744 * open. It probably went down. Remove it from our knowledge. */
2745 Peers_cleanup_destroyed_channel (cls, channel);
2746 remove_peer (peer);
2747 GNUNET_free (peer);
2748 return;
2749 }
2750 }
2751 else if (GNUNET_YES ==
2752 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
2753 { /* Channel used for receiving was destroyed */
2754 /* Possible causes of channel destruction:
2755 * - ourselves -> peer tried to establish channel twice -> clean context
2756 * - other peer -> peer doesn't want to send us data -> clean
2757 */
2758 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
2759 if (GNUNET_YES ==
2760 Peers_check_channel_flag (channel_flag, Peers_CHANNEL_ESTABLISHED_TWICE))
2761 { /* Other peer tried to establish a channel to us twice. We do not accept
2762 * that. Clean the context. */
2763 Peers_cleanup_destroyed_channel (cls, channel);
2764 GNUNET_free (peer);
2765 return;
2766 }
2767 else
2768 { /* Other peer doesn't want to send us data anymore. We are free to clean
2769 * it. */
2770 Peers_cleanup_destroyed_channel (cls, channel);
2771 clean_peer (peer);
2772 GNUNET_free (peer);
2773 return;
2774 }
2775 } 2760 }
2776 else 2761 else if (peer_ctx->send_channel_ctx == channel_ctx)
2777 { 2762 {
2778 LOG (GNUNET_ERROR_TYPE_WARNING, 2763 remove_channel_ctx (channel_ctx);
2779 "Destroyed channel is neither sending nor receiving channel\n"); 2764 remove_peer (&peer_ctx->peer_id);
2780 } 2765 }
2781 GNUNET_free (peer);
2782} 2766}
2783 2767
2784/*********************************************************************** 2768/***********************************************************************
@@ -3037,8 +3021,6 @@ handle_client_seed (void *cls,
3037 3021
3038 num_peers = ntohl (msg->num_peers); 3022 num_peers = ntohl (msg->num_peers);
3039 peers = (struct GNUNET_PeerIdentity *) &msg[1]; 3023 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3040 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
3041 //GNUNET_memcpy (peers, &msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
3042 3024
3043 LOG (GNUNET_ERROR_TYPE_DEBUG, 3025 LOG (GNUNET_ERROR_TYPE_DEBUG,
3044 "Client seeded peers:\n"); 3026 "Client seeded peers:\n");
@@ -3053,9 +3035,6 @@ handle_client_seed (void *cls,
3053 3035
3054 got_peer (&peers[i]); 3036 got_peer (&peers[i]);
3055 } 3037 }
3056
3057 ////GNUNET_free (peers);
3058
3059 GNUNET_SERVICE_client_continue (cli_ctx->client); 3038 GNUNET_SERVICE_client_continue (cli_ctx->client);
3060} 3039}
3061 3040
@@ -3173,11 +3152,12 @@ static void
3173handle_peer_check (void *cls, 3152handle_peer_check (void *cls,
3174 const struct GNUNET_MessageHeader *msg) 3153 const struct GNUNET_MessageHeader *msg)
3175{ 3154{
3176 const struct GNUNET_PeerIdentity *peer = cls; 3155 const struct ChannelCtx *channel_ctx = cls;
3156 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3177 LOG (GNUNET_ERROR_TYPE_DEBUG, 3157 LOG (GNUNET_ERROR_TYPE_DEBUG,
3178 "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer)); 3158 "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer));
3179 3159
3180 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer)); 3160 GNUNET_CADET_receive_done (channel_ctx->channel);
3181} 3161}
3182 3162
3183/** 3163/**
@@ -3193,7 +3173,8 @@ static void
3193handle_peer_push (void *cls, 3173handle_peer_push (void *cls,
3194 const struct GNUNET_MessageHeader *msg) 3174 const struct GNUNET_MessageHeader *msg)
3195{ 3175{
3196 const struct GNUNET_PeerIdentity *peer = cls; 3176 const struct ChannelCtx *channel_ctx = cls;
3177 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3197 3178
3198 // (check the proof of work (?)) 3179 // (check the proof of work (?))
3199 3180
@@ -3238,7 +3219,7 @@ handle_peer_push (void *cls,
3238 CustomPeerMap_put (push_map, peer); 3219 CustomPeerMap_put (push_map, peer);
3239 3220
3240 GNUNET_break_op (Peers_check_peer_known (peer)); 3221 GNUNET_break_op (Peers_check_peer_known (peer));
3241 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer)); 3222 GNUNET_CADET_receive_done (channel_ctx->channel);
3242} 3223}
3243 3224
3244 3225
@@ -3254,7 +3235,8 @@ static void
3254handle_peer_pull_request (void *cls, 3235handle_peer_pull_request (void *cls,
3255 const struct GNUNET_MessageHeader *msg) 3236 const struct GNUNET_MessageHeader *msg)
3256{ 3237{
3257 struct GNUNET_PeerIdentity *peer = cls; 3238 const struct ChannelCtx *channel_ctx = cls;
3239 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3258 const struct GNUNET_PeerIdentity *view_array; 3240 const struct GNUNET_PeerIdentity *view_array;
3259 3241
3260 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer)); 3242 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
@@ -3277,7 +3259,7 @@ handle_peer_pull_request (void *cls,
3277 #endif /* ENABLE_MALICIOUS */ 3259 #endif /* ENABLE_MALICIOUS */
3278 3260
3279 GNUNET_break_op (Peers_check_peer_known (peer)); 3261 GNUNET_break_op (Peers_check_peer_known (peer));
3280 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer)); 3262 GNUNET_CADET_receive_done (channel_ctx->channel);
3281 view_array = View_get_as_array (); 3263 view_array = View_get_as_array ();
3282 send_pull_reply (peer, view_array, View_size ()); 3264 send_pull_reply (peer, view_array, View_size ());
3283} 3265}
@@ -3317,7 +3299,8 @@ check_peer_pull_reply (void *cls,
3317 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING)) 3299 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
3318 { 3300 {
3319 LOG (GNUNET_ERROR_TYPE_WARNING, 3301 LOG (GNUNET_ERROR_TYPE_WARNING,
3320 "Received a pull reply from a peer we didn't request one from!\n"); 3302 "Received a pull reply from a peer (%s) we didn't request one from!\n",
3303 GNUNET_i2s (sender));
3321 GNUNET_break_op (0); 3304 GNUNET_break_op (0);
3322 return GNUNET_SYSERR; 3305 return GNUNET_SYSERR;
3323 } 3306 }
@@ -3334,8 +3317,9 @@ static void
3334handle_peer_pull_reply (void *cls, 3317handle_peer_pull_reply (void *cls,
3335 const struct GNUNET_RPS_P2P_PullReplyMessage *msg) 3318 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3336{ 3319{
3320 const struct ChannelCtx *channel_ctx = cls;
3321 const struct GNUNET_PeerIdentity *sender = &channel_ctx->peer_ctx->peer_id;
3337 const struct GNUNET_PeerIdentity *peers; 3322 const struct GNUNET_PeerIdentity *peers;
3338 struct GNUNET_PeerIdentity *sender = cls;
3339 uint32_t i; 3323 uint32_t i;
3340#ifdef ENABLE_MALICIOUS 3324#ifdef ENABLE_MALICIOUS
3341 struct AttackedPeer *tmp_att_peer; 3325 struct AttackedPeer *tmp_att_peer;
@@ -3373,9 +3357,7 @@ handle_peer_pull_reply (void *cls,
3373 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set, 3357 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3374 &peers[i]) 3358 &peers[i])
3375 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set, 3359 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
3376 &peers[i]) 3360 &peers[i]))
3377 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
3378 &own_identity))
3379 { 3361 {
3380 tmp_att_peer = GNUNET_new (struct AttackedPeer); 3362 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3381 tmp_att_peer->peer_id = peers[i]; 3363 tmp_att_peer->peer_id = peers[i];
@@ -3387,21 +3369,17 @@ handle_peer_pull_reply (void *cls,
3387 continue; 3369 continue;
3388 } 3370 }
3389 #endif /* ENABLE_MALICIOUS */ 3371 #endif /* ENABLE_MALICIOUS */
3390 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, 3372 /* Make sure we 'know' about this peer */
3391 &peers[i])) 3373 (void) Peers_insert_peer (&peers[i]);
3392 {
3393 /* Make sure we 'know' about this peer */
3394 (void) Peers_insert_peer (&peers[i]);
3395 3374
3396 if (GNUNET_YES == Peers_check_peer_valid (&peers[i])) 3375 if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
3397 { 3376 {
3398 CustomPeerMap_put (pull_map, &peers[i]); 3377 CustomPeerMap_put (pull_map, &peers[i]);
3399 } 3378 }
3400 else 3379 else
3401 { 3380 {
3402 Peers_schedule_operation (&peers[i], insert_in_pull_map); 3381 Peers_schedule_operation (&peers[i], insert_in_pull_map);
3403 (void) Peers_issue_peer_liveliness_check (&peers[i]); 3382 (void) Peers_issue_peer_liveliness_check (&peers[i]);
3404 }
3405 } 3383 }
3406 } 3384 }
3407 3385
@@ -3409,7 +3387,7 @@ handle_peer_pull_reply (void *cls,
3409 clean_peer (sender); 3387 clean_peer (sender);
3410 3388
3411 GNUNET_break_op (Peers_check_peer_known (sender)); 3389 GNUNET_break_op (Peers_check_peer_known (sender));
3412 GNUNET_CADET_receive_done (Peers_get_recv_channel (sender)); 3390 GNUNET_CADET_receive_done (channel_ctx->channel);
3413} 3391}
3414 3392
3415 3393
@@ -3836,10 +3814,8 @@ do_round (void *cls)
3836 for (i = 0; i < a_peers; i++) 3814 for (i = 0; i < a_peers; i++)
3837 { 3815 {
3838 peer = view_array[permut[i]]; 3816 peer = view_array[permut[i]];
3839 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO 3817 // FIXME if this fails schedule/loop this for later
3840 { // FIXME if this fails schedule/loop this for later 3818 send_push (&peer);
3841 send_push (&peer);
3842 }
3843 } 3819 }
3844 3820
3845 /* Send PULL requests */ 3821 /* Send PULL requests */
@@ -3857,8 +3833,7 @@ do_round (void *cls)
3857 for (i = first_border; i < second_border; i++) 3833 for (i = first_border; i < second_border; i++)
3858 { 3834 {
3859 peer = view_array[permut[i]]; 3835 peer = view_array[permut[i]];
3860 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) && 3836 if ( GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING))
3861 GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
3862 { // FIXME if this fails schedule/loop this for later 3837 { // FIXME if this fails schedule/loop this for later
3863 send_pull_request (&peer); 3838 send_pull_request (&peer);
3864 } 3839 }
@@ -3955,7 +3930,6 @@ do_round (void *cls)
3955 "-%s", 3930 "-%s",
3956 GNUNET_i2s_full (&peers_to_clean[i])); 3931 GNUNET_i2s_full (&peers_to_clean[i]));
3957 clean_peer (&peers_to_clean[i]); 3932 clean_peer (&peers_to_clean[i]);
3958 //peer_destroy_channel_send (sender);
3959 } 3933 }
3960 3934
3961 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0); 3935 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
@@ -4011,7 +3985,6 @@ do_round (void *cls)
4011 GNUNET_i2s (update_peer)); 3985 GNUNET_i2s (update_peer));
4012 insert_in_sampler (NULL, update_peer); 3986 insert_in_sampler (NULL, update_peer);
4013 clean_peer (update_peer); /* This cleans only if it is not in the view */ 3987 clean_peer (update_peer); /* This cleans only if it is not in the view */
4014 //peer_destroy_channel_send (sender);
4015 } 3988 }
4016 3989
4017 for (i = 0; i < CustomPeerMap_size (pull_map); i++) 3990 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
@@ -4022,7 +3995,6 @@ do_round (void *cls)
4022 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i)); 3995 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
4023 /* This cleans only if it is not in the view */ 3996 /* This cleans only if it is not in the view */
4024 clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i)); 3997 clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
4025 //peer_destroy_channel_send (sender);
4026 } 3998 }
4027 3999
4028 4000
@@ -4125,6 +4097,8 @@ shutdown_task (void *cls)
4125 struct ClientContext *client_ctx; 4097 struct ClientContext *client_ctx;
4126 struct ReplyCls *reply_cls; 4098 struct ReplyCls *reply_cls;
4127 4099
4100 in_shutdown = GNUNET_YES;
4101
4128 LOG (GNUNET_ERROR_TYPE_DEBUG, 4102 LOG (GNUNET_ERROR_TYPE_DEBUG,
4129 "RPS is going down\n"); 4103 "RPS is going down\n");
4130 4104
@@ -4369,10 +4343,17 @@ run (void *cls,
4369 NULL, /* WindowSize handler */ 4343 NULL, /* WindowSize handler */
4370 cleanup_destroyed_channel, /* Disconnect handler */ 4344 cleanup_destroyed_channel, /* Disconnect handler */
4371 cadet_handlers); 4345 cadet_handlers);
4346 if (NULL == cadet_port)
4347 {
4348 LOG (GNUNET_ERROR_TYPE_ERROR,
4349 "Cadet port `%s' is already in use.\n",
4350 GNUNET_APPLICATION_PORT_RPS);
4351 GNUNET_assert (0);
4352 }
4372 4353
4373 4354
4374 peerinfo_handle = GNUNET_PEERINFO_connect (cfg); 4355 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
4375 Peers_initialise (fn_valid_peers, cadet_handle, &own_identity); 4356 Peers_initialise (fn_valid_peers, cadet_handle);
4376 GNUNET_free (fn_valid_peers); 4357 GNUNET_free (fn_valid_peers);
4377 4358
4378 /* Initialise sampler */ 4359 /* Initialise sampler */