aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulius Bünger <buenger@mytum.de>2015-03-26 11:20:42 +0000
committerJulius Bünger <buenger@mytum.de>2015-03-26 11:20:42 +0000
commit5e11eb124cc16bc65d42a47076e13d3b0147b838 (patch)
treeebdc89fe55bc6fffbed1c9c512b067f1290d4287 /src
parentc7ed5f29afe7a4e4294657239238170bfd9bc934 (diff)
downloadgnunet-5e11eb124cc16bc65d42a47076e13d3b0147b838.tar.gz
gnunet-5e11eb124cc16bc65d42a47076e13d3b0147b838.zip
-mal peer handle pull request
Diffstat (limited to 'src')
-rw-r--r--src/rps/gnunet-service-rps.c195
1 files changed, 133 insertions, 62 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index e4f7b3c4d..9e3a1a6c8 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -372,6 +372,12 @@ uint32_t mal_type = 0;
372static struct GNUNET_PeerIdentity *mal_peers = NULL; 372static struct GNUNET_PeerIdentity *mal_peers = NULL;
373 373
374/** 374/**
375 * Hashmap of malicious peers used as set.
376 * Used to more efficiently check whether we know that peer.
377 */
378static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
379
380/**
375 * Number of other malicious peers 381 * Number of other malicious peers
376 */ 382 */
377static uint32_t num_mal_peers = 0; 383static uint32_t num_mal_peers = 0;
@@ -407,6 +413,12 @@ static struct AttackedPeer *att_peers_tail = NULL;
407static struct AttackedPeer *att_peer_index = NULL; 413static struct AttackedPeer *att_peer_index = NULL;
408 414
409/** 415/**
416 * Hashmap of attacked peers used as set.
417 * Used to more efficiently check whether we know that peer.
418 */
419static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
420
421/**
410 * Number of attacked peers 422 * Number of attacked peers
411 */ 423 */
412static uint32_t num_attacked_peers = 0; 424static uint32_t num_attacked_peers = 0;
@@ -1032,6 +1044,82 @@ est_request_rate()
1032} 1044}
1033 1045
1034 1046
1047/**
1048 * Add all peers in @a peer_array to @peer_map used as set.
1049 *
1050 * @param peer_array array containing the peers
1051 * @param num_peers number of peers in @peer_array
1052 * @param peer_map the peermap to use as set
1053 */
1054static void
1055add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1056 unsigned int num_peers,
1057 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1058{
1059 unsigned int i;
1060 if (NULL == peer_map)
1061 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers,
1062 GNUNET_NO);
1063 for (i = 0 ; i < num_peers ; i++)
1064 {
1065 GNUNET_CONTAINER_multipeermap_put (peer_map,
1066 &peer_array[i],
1067 NULL,
1068 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1069 }
1070}
1071
1072
1073/**
1074 * Send a PULL REPLY to @a peer_id
1075 *
1076 * @param peer_id the peer to send the reply to.
1077 * @param peer_ids the peers to send to @a peer_id
1078 * @param num_peer_ids the number of peers to send to @a peer_id
1079 */
1080static void
1081send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1082 const struct GNUNET_PeerIdentity *peer_ids,
1083 unsigned int num_peer_ids)
1084{
1085 uint32_t send_size;
1086 struct GNUNET_MQ_Handle *mq;
1087 struct GNUNET_MQ_Envelope *ev;
1088 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1089
1090 /* Compute actual size */
1091 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1092 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1093
1094 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1095 /* Compute number of peers to send
1096 * If too long, simply truncate */
1097 // TODO select random ones via permutation
1098 // or even better: do good protocol design
1099 send_size =
1100 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1101 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1102 sizeof (struct GNUNET_PeerIdentity);
1103 else
1104 send_size = num_peer_ids;
1105
1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 "PULL REQUEST from peer %s received, going to send %u peers\n",
1108 GNUNET_i2s (peer_id), send_size);
1109
1110 mq = get_mq (peer_map, peer_id);
1111
1112 ev = GNUNET_MQ_msg_extra (out_msg,
1113 send_size * sizeof (struct GNUNET_PeerIdentity),
1114 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1115 out_msg->num_peers = htonl (send_size);
1116 memcpy (&out_msg[1], peer_ids,
1117 send_size * sizeof (struct GNUNET_PeerIdentity));
1118
1119 GNUNET_MQ_send (mq, ev);
1120}
1121
1122
1035/*********************************************************************** 1123/***********************************************************************
1036 * /Util functions 1124 * /Util functions
1037***********************************************************************/ 1125***********************************************************************/
@@ -1243,8 +1331,10 @@ handle_peer_push (void *cls,
1243 1331
1244 // (check the proof of work) 1332 // (check the proof of work)
1245 1333
1246 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER); 1334 peer = (const struct GNUNET_PeerIdentity *)
1335 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1247 // FIXME wait for cadet to change this function 1336 // FIXME wait for cadet to change this function
1337
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer)); 1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1249 1339
1250 #ifdef ENABLE_MALICIOUS 1340 #ifdef ENABLE_MALICIOUS
@@ -1254,8 +1344,16 @@ handle_peer_push (void *cls,
1254 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity)); 1344 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1255 if (1 == mal_type) 1345 if (1 == mal_type)
1256 { /* Try to maximise representation */ 1346 { /* Try to maximise representation */
1257 // TODO Check whether we already have that peer 1347 if (NULL == att_peer_set)
1258 GNUNET_CONTAINER_DLL_insert (att_peers_head, att_peers_tail, tmp_att_peer); 1348 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1349 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1350 peer))
1351 {
1352 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1353 att_peers_tail,
1354 tmp_att_peer);
1355 add_peer_array_to_set (peer, 1, att_peer_set);
1356 }
1259 return GNUNET_OK; 1357 return GNUNET_OK;
1260 } 1358 }
1261 1359
@@ -1292,47 +1390,30 @@ handle_peer_pull_request (void *cls,
1292 const struct GNUNET_MessageHeader *msg) 1390 const struct GNUNET_MessageHeader *msg)
1293{ 1391{
1294 struct GNUNET_PeerIdentity *peer; 1392 struct GNUNET_PeerIdentity *peer;
1295 uint32_t send_size;
1296 struct GNUNET_MQ_Handle *mq;
1297 struct GNUNET_MQ_Envelope *ev;
1298 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1299
1300 1393
1301 peer = (struct GNUNET_PeerIdentity *) 1394 peer = (struct GNUNET_PeerIdentity *)
1302 GNUNET_CADET_channel_get_info (channel, 1395 GNUNET_CADET_channel_get_info (channel,
1303 GNUNET_CADET_OPTION_PEER); 1396 GNUNET_CADET_OPTION_PEER);
1304 // FIXME wait for cadet to change this function 1397 // FIXME wait for cadet to change this function
1305 1398
1306 /* Compute actual size */ 1399 #ifdef ENABLE_MALICIOUS
1307 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) + 1400 if (1 == mal_type)
1308 gossip_list_size * sizeof (struct GNUNET_PeerIdentity); 1401 { /* Try to maximise representation */
1309 1402 send_pull_reply (peer, mal_peers, num_mal_peers);
1310 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size) 1403 return GNUNET_OK;
1311 /* Compute number of peers to send 1404 }
1312 * If too long, simply truncate */
1313 // TODO select random ones via permutation
1314 send_size =
1315 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1316 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1317 sizeof (struct GNUNET_PeerIdentity);
1318 else
1319 send_size = gossip_list_size;
1320
1321 LOG (GNUNET_ERROR_TYPE_DEBUG,
1322 "PULL REQUEST from peer %s received, going to send %u peers\n",
1323 GNUNET_i2s (peer), send_size);
1324
1325 mq = get_mq (peer_map, peer);
1326 1405
1327 ev = GNUNET_MQ_msg_extra (out_msg, 1406 else if (2 == mal_type)
1328 send_size * sizeof (struct GNUNET_PeerIdentity), 1407 { /* Try to partition network */
1329 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY); 1408 if (GNUNET_YES == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1330 //out_msg->num_peers = htonl (gossip_list_size); 1409 {
1331 out_msg->num_peers = htonl (send_size); 1410 send_pull_reply (peer, mal_peers, num_mal_peers);
1332 memcpy (&out_msg[1], gossip_list, 1411 }
1333 send_size * sizeof (struct GNUNET_PeerIdentity)); 1412 return GNUNET_OK;
1413 }
1414 #endif /* ENABLE_MALICIOUS */
1334 1415
1335 GNUNET_MQ_send (mq, ev); 1416 send_pull_reply (peer, gossip_list, gossip_list_size);
1336 1417
1337 return GNUNET_OK; 1418 return GNUNET_OK;
1338} 1419}
@@ -1575,6 +1656,7 @@ handle_client_act_malicious (void *cls,
1575 if (1 == mal_type) 1656 if (1 == mal_type)
1576 { /* Try to maximise representation */ 1657 { /* Try to maximise representation */
1577 /* Add other malicious peers to those we already know */ 1658 /* Add other malicious peers to those we already know */
1659
1578 num_mal_peers_sent = ntohl (in_msg->num_peers); 1660 num_mal_peers_sent = ntohl (in_msg->num_peers);
1579 num_mal_peers_old = num_mal_peers; 1661 num_mal_peers_old = num_mal_peers;
1580 GNUNET_array_grow (mal_peers, 1662 GNUNET_array_grow (mal_peers,
@@ -1584,10 +1666,16 @@ handle_client_act_malicious (void *cls,
1584 peers, 1666 peers,
1585 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity)); 1667 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1586 1668
1669 /* Add all mal peers to mal_peer_set */
1670 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1671 num_mal_peers_sent,
1672 mal_peer_set);
1673
1587 /* Substitute do_round () with do_mal_round () */ 1674 /* Substitute do_round () with do_mal_round () */
1588 GNUNET_SCHEDULER_cancel (do_round_task); 1675 GNUNET_SCHEDULER_cancel (do_round_task);
1589 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL); 1676 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1590 } 1677 }
1678
1591 else if (2 == mal_type) 1679 else if (2 == mal_type)
1592 { /* Try to partition the network */ 1680 { /* Try to partition the network */
1593 /* Add other malicious peers to those we already know */ 1681 /* Add other malicious peers to those we already know */
@@ -1600,6 +1688,11 @@ handle_client_act_malicious (void *cls,
1600 peers, 1688 peers,
1601 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity)); 1689 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1602 1690
1691 /* Add all mal peers to mal_peer_set */
1692 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1693 num_mal_peers_sent,
1694 mal_peer_set);
1695
1603 /* Store the one attacked peer */ 1696 /* Store the one attacked peer */
1604 memcpy (&attacked_peer, 1697 memcpy (&attacked_peer,
1605 &peers[num_mal_peers_sent], 1698 &peers[num_mal_peers_sent],
@@ -1641,7 +1734,6 @@ do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1641 uint32_t i; 1734 uint32_t i;
1642 struct GNUNET_TIME_Relative time_next_round; 1735 struct GNUNET_TIME_Relative time_next_round;
1643 struct AttackedPeer *tmp_att_peer; 1736 struct AttackedPeer *tmp_att_peer;
1644 struct AttackedPeer *att_stop_peer;
1645 1737
1646 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n"); 1738 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1647 1739
@@ -1654,47 +1746,22 @@ do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1654 num_attacked_peers), 1746 num_attacked_peers),
1655 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE); 1747 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1656 1748
1657
1658 /* Send PUSHes to attacked peers */ 1749 /* Send PUSHes to attacked peers */
1659
1660 /* If we see this peer again while iterating over peers
1661 * we can stop iterating, as peers will ignore multiple
1662 * pushes from one peer in one round */
1663 if (att_peers_head == att_peer_index)
1664 att_stop_peer = att_peers_tail;
1665 else
1666 att_peers_tail = att_peer_index->prev;
1667
1668 for (i = 0 ; i < num_pushes ; i++) 1750 for (i = 0 ; i < num_pushes ; i++)
1669 { 1751 {
1670 if (att_peers_tail == att_peer_index) 1752 if (att_peers_tail == att_peer_index)
1671 att_peer_index = att_peers_head; 1753 att_peer_index = att_peers_head;
1672 else if (att_stop_peer == att_peer_index)
1673 break;
1674 else 1754 else
1675 att_peer_index = att_peer_index->next; 1755 att_peer_index = att_peer_index->next;
1676 1756
1677 send_push (&att_peer_index->peer_id); 1757 send_push (&att_peer_index->peer_id);
1678 } 1758 }
1679 1759
1680
1681 /* Send PULLs to some peers to learn about additional peers to attack */ 1760 /* Send PULLs to some peers to learn about additional peers to attack */
1682
1683 /* If we see this peer again while iterating over peers
1684 * we can stop iterating, as peers will ignore multiple
1685 * pushes from one peer in one round */
1686 tmp_att_peer = att_peer_index;
1687 if (att_peers_head == att_peer_index)
1688 att_stop_peer = att_peers_tail;
1689 else
1690 att_peers_tail = att_peer_index->prev;
1691
1692 for (i = 0 ; i < num_pushes * alpha ; i++) 1761 for (i = 0 ; i < num_pushes * alpha ; i++)
1693 { 1762 {
1694 if (att_peers_tail == tmp_att_peer) 1763 if (att_peers_tail == tmp_att_peer)
1695 tmp_att_peer = att_peers_head; 1764 tmp_att_peer = att_peers_head;
1696 else if (att_stop_peer == att_peer_index)
1697 break;
1698 else 1765 else
1699 att_peer_index = tmp_att_peer->next; 1766 att_peer_index = tmp_att_peer->next;
1700 1767
@@ -2084,6 +2151,10 @@ shutdown_task (void *cls,
2084 GNUNET_array_grow (pull_list, pull_list_size, 0); 2151 GNUNET_array_grow (pull_list, pull_list_size, 0);
2085 #ifdef ENABLE_MALICIOUS 2152 #ifdef ENABLE_MALICIOUS
2086 GNUNET_array_grow (mal_peers, num_mal_peers, 0); 2153 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2154 if (NULL != mal_peer_set)
2155 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2156 if (NULL != att_peer_set)
2157 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2087 // TODO empty attacked_peers DLL 2158 // TODO empty attacked_peers DLL
2088 #endif /* ENABLE_MALICIOUS */ 2159 #endif /* ENABLE_MALICIOUS */
2089} 2160}