TEST(ban, add) { test_core pr_core; cryptonote::t_cryptonote_protocol_handler<test_core> cprotocol(pr_core, NULL); Server server(cprotocol); cprotocol.set_p2p_endpoint(&server); // starts empty ASSERT_TRUE(server.get_blocked_hosts().empty()); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); // add an IP ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_TRUE(server.get_blocked_hosts().size() == 1); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); // add the same, should not change ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_TRUE(server.get_blocked_hosts().size() == 1); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); // remove an unblocked IP, should not change ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5))); ASSERT_TRUE(server.get_blocked_hosts().size() == 1); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); // remove the IP, ends up empty ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_TRUE(server.get_blocked_hosts().size() == 0); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); // remove the IP from an empty list, still empty ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_TRUE(server.get_blocked_hosts().size() == 0); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); // add two for known amounts of time, they're both blocked ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1)); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,5), 3)); ASSERT_TRUE(server.get_blocked_hosts().size() == 2); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5))); // these tests would need to call is_remote_ip_allowed, which is private #if 0 // after two seconds, the first IP is unblocked, but not the second yet sleep(2); ASSERT_TRUE(server.get_blocked_hosts().size() == 1); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); // after two more seconds, the second IP is also unblocked sleep(2); ASSERT_TRUE(server.get_blocked_hosts().size() == 0); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); #endif // add an IP again, then re-ban for longer, then shorter time_t t; ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 2)); ASSERT_TRUE(server.get_blocked_hosts().size() == 1); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); ASSERT_TRUE(t >= 1); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 9)); ASSERT_TRUE(server.get_blocked_hosts().size() == 1); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); ASSERT_TRUE(t >= 8); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 5)); ASSERT_TRUE(server.get_blocked_hosts().size() == 1); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); ASSERT_TRUE(t >= 4); }
/* * Function: wilddog_receive * Description: wilddog receive function, it use the interface in wiced platform. * Input: socketId: The socket id. * addr: The pointer of Wilddog_Address_T * buf: The pointer of the send buffer * bufLen: The length of the send buffer. * timeout: The max timeout in recv process. * Output: N/A * Return: If success, return the number of bytes received; else return -1. */ int wilddog_receive ( int socketId, Wilddog_Address_T* addr_in, void* buf, s32 bufLen, s32 timeout ) { wiced_udp_socket_t* socket = (wiced_udp_socket_t*) socketId; wiced_packet_t* receive = NULL; u16 totalLeft = 0; u16 total = 0; uint16_t pos = 0; uint8_t* rxData; uint16_t fragLeft = 0; wiced_ip_address_t recieve_ip_addr; uint16_t receive_port; wiced_result_t result; result = wiced_udp_receive( socket, &receive, timeout ); if ( result == WICED_SUCCESS ) { if(NULL == receive) { wilddog_debug_level(WD_DEBUG_ERROR, "receive is NULL!"); return -1; } wiced_udp_packet_get_info( receive, &recieve_ip_addr, &receive_port ); if ( !recieve_ip_addr.ip.v4 == MAKE_IPV4_ADDRESS(addr_in->ip[0], \ addr_in->ip[1], addr_in->ip[2], addr_in->ip[3]) ) { wilddog_debug_level(WD_DEBUG_ERROR, "addr error!\n" ); wiced_packet_delete( receive ); return -1; } do { if ( wiced_packet_get_data( receive, 0, (uint8_t**) &rxData, \ &fragLeft, &totalLeft ) != WICED_SUCCESS ) { wilddog_debug_level(WD_DEBUG_ERROR, \ "get data from packet error \r\n" ); wiced_packet_delete( receive ); return -1; } else { if ( pos + fragLeft > bufLen ) { /* too large, drop*/ wiced_packet_delete( receive ); wilddog_debug_level(WD_DEBUG_ERROR, \ "too large receive end , get %d bytes\n", pos ); return 0; } if ( total == 0 ) { /*only get first data, next fragment's total is wrong*/ total = totalLeft; } memcpy( (uint8_t *) ( buf + pos ), rxData, fragLeft ); pos += fragLeft; fragLeft = 0; } if ( pos == total ) { /*end*/ #if WILDDOG_SELFTEST { performtest_getWaitSessionQueryTime(); performtest_getWaitRecvTime(); } #endif wilddog_debug_level(WD_DEBUG_LOG, "received %d data!", pos); wiced_packet_delete( receive ); return pos; } else if ( pos < total ) { /*get next fragment*/ wilddog_debug_level(WD_DEBUG_LOG, "more than one packet!"); if ( WICED_SUCCESS != _packet_get_next_fragment( receive, \ &receive ) ) { wilddog_debug_level(WD_DEBUG_ERROR, \ "get next fragment err! %d lost!", total); if(receive) wiced_packet_delete( receive ); return -1; } if ( NULL == receive ) { wilddog_debug_level(WD_DEBUG_ERROR, \ "get next fragment err! %d lost!", total); return -1; } } else { wiced_packet_delete( receive ); return -1; } } while ( pos < total ); } else { wilddog_debug_level(WD_DEBUG_LOG, "result = %d",result); } /*if(receive) wiced_packet_delete( receive );*/ return 0; }