Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate booststrap-dht with cmake #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
*.lai
*.la
*.a

# CMake build directory
build/
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 2.8)
project(bootstrap-dht)

set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/)

set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Werror ${CMAKE_CXX_FLAGS}")

find_package(Boost REQUIRED COMPONENTS system)
find_package(Threads REQUIRED)

set(SOURCES main.cpp lazy_bdecode.cpp)
set(HEADERS bencode.hpp lazy_entry.hpp)

include_directories(
${CMAKE_BINARY_DIR}/include
${Boost_INCLUDE_DIRS})

add_executable(bootstrap-dht ${SOURCES} ${HEADERS})

target_link_libraries(bootstrap-dht
${Boost_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT})
4 changes: 2 additions & 2 deletions lazy_bdecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ namespace libtorrent
for (int i = 0; i < int(m_size); ++i)
{
lazy_dict_entry& e = m_data.dict[i];
if (name.size() != e.val.m_begin - e.name) continue;
if (int(name.size()) != e.val.m_begin - e.name) continue;
if (std::equal(name.begin(), name.end(), e.name))
return &e.val;
}
Expand Down Expand Up @@ -533,7 +533,7 @@ namespace libtorrent
line_len += 4;
break;
}

if (line_len > limit) return -1;
return line_len;
}
Expand Down
55 changes: 30 additions & 25 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ struct hash<address_v4::bytes_type> : hash<uint32_t>
}

/*

The way the DHT bootstrapping works is by storing all
nodes in one circular buffer of (IP, port, node-id)-triplets.
In this circular buffer there are two cursors, one read
cursor and one write cursor.
When a find_nodes request comes in, we return the next
few nodes (or so) under the read cursor and progresses it.

We remember the node that asked in a separate queue.
At a later time we ping it. If it responds, we
add it at the write cursor and progresses it.
Expand All @@ -125,7 +125,7 @@ struct hash<address_v4::bytes_type> : hash<uint32_t>


[1]: http://libtorrent.org/dht_sec.html

*/

std::atomic<uint64_t> incoming_queries;
Expand Down Expand Up @@ -243,7 +243,7 @@ struct ping_queue_t
bool need_ping(queued_node_t* out)
{
if (m_queue.empty()) return false;

time_point now = steady_clock::now();
if (m_queue.front().expire > now)
return false;
Expand All @@ -267,14 +267,14 @@ struct ping_queue_t
if (m_ips.count(ep)) return;

// don't let the queue get too big
if (m_queue.size() > ping_queue_size) return;
if (int(m_queue.size()) > ping_queue_size) return;

// as the size approaches the limit, increasingly reject
// new nodes, to distribute nodes we ping more evenly
// over time
++m_round_robin;
m_round_robin &= 0xff;
if (m_round_robin < m_queue.size() * 256 / ping_queue_size)
if (m_round_robin < int(m_queue.size()) * 256 / ping_queue_size)
return;

queued_node_t e;
Expand Down Expand Up @@ -349,10 +349,10 @@ struct node_buffer_t

ret.resize(nodes_in_response * sizeof(node_entry_t));

if (m_read_cursor == m_buffer.size())
if (m_read_cursor == int(m_buffer.size()))
m_read_cursor = 0;
if (m_read_cursor <= m_buffer.size() - nodes_in_response)

if (m_read_cursor <= int(m_buffer.size()) - nodes_in_response)
{
memcpy(&ret[0], &m_buffer[m_read_cursor], sizeof(node_entry_t) * nodes_in_response);
m_read_cursor += nodes_in_response;
Expand All @@ -370,7 +370,7 @@ struct node_buffer_t
m_read_cursor = slice2;
return ret;
}

void insert_node(udp::endpoint const& ep, char const* node_id)
{
node_entry_t e;
Expand Down Expand Up @@ -408,7 +408,7 @@ struct node_buffer_t
m_last_write_loop = now;
}

if (m_buffer.size() < m_current_max_size)
if (int(m_buffer.size()) < m_current_max_size)
{
m_buffer.push_back(e);
m_ips.insert(e.ip);
Expand Down Expand Up @@ -484,7 +484,7 @@ bool compare_id_prefix(char const* id1, char const* id2)
void generate_id(address const& ip_, boost::uint32_t r, char* id)
{
boost::uint8_t* ip = 0;

const static boost::uint8_t mask[] = { 0x03, 0x0f, 0x3f, 0xff };

address_v4::bytes_type b4;
Expand Down Expand Up @@ -515,7 +515,7 @@ void generate_id(address const& ip_, boost::uint32_t r, char* id)
void generate_id_sha1(address const& ip_, boost::uint32_t r, char* id)
{
boost::uint8_t* ip = 0;

const static boost::uint8_t mask[] = { 0x01, 0x07, 0x1f, 0x7f };

address_v4::bytes_type b4;
Expand Down Expand Up @@ -841,7 +841,7 @@ void router_thread(int threadid, udp::socket& sock)
int num_nodes = nodes.size() / sizeof(node_entry_t);
if (num_nodes < nodes_in_response && last_nodes.size() > 0)
{
// fill in with lower quality nodes, since
// fill in with lower quality nodes, since
nodes.resize((num_nodes + last_nodes.size()) * sizeof(node_entry_t));

// this is just to be able to copy the entire ringbuffer in
Expand Down Expand Up @@ -977,12 +977,15 @@ int main(int argc, char* argv[])
return 1;
}
num_threads = atoi(argv[i]);
if (num_threads > std::thread::hardware_concurrency())
if (num_threads <= 0)
{
num_threads = 1;
}
else if (unsigned(num_threads) > std::thread::hardware_concurrency())
{
fprintf(stderr, "WARNING: using more threads (%d) than cores (%d)\n"
, num_threads, std::thread::hardware_concurrency());
}
if (num_threads <= 0) num_threads = 1;
}
else if (strcmp(argv[i], "--nodes") == 0)
{
Expand All @@ -992,13 +995,14 @@ int main(int argc, char* argv[])
fprintf(stderr, "--nodes expects an integer argument\n");
return 1;
}
node_buffer_size = atoi(argv[i]);
if (node_buffer_size <= 1000)
int size = atoi(argv[i]);
if (size <= 1000)
{
node_buffer_size = 1000;
size = 1000;
fprintf(stderr, "WARNING: node buffer suspiciously small, using %d\n"
, node_buffer_size);
, size);
}
node_buffer_size = size;
}
else if (strcmp(argv[i], "--ping-queue") == 0)
{
Expand All @@ -1008,13 +1012,14 @@ int main(int argc, char* argv[])
fprintf(stderr, "--ping-queue expects an integer argument\n");
return 1;
}
ping_queue_size = atoi(argv[i]);
if (ping_queue_size < 10)
int size = atoi(argv[i]);
if (size < 10)
{
ping_queue_size = 10;
size = 10;
fprintf(stderr, "WARNING: ping queue suspiciously small, using %d\n"
, ping_queue_size);
, size);
}
ping_queue_size = size;
}
else if (strcmp(argv[i], "--no-verify-id") == 0)
{
Expand Down Expand Up @@ -1087,7 +1092,7 @@ int main(int argc, char* argv[])
fprintf(stderr, "bind: (%d) %s\n", ec.value(), ec.message().c_str());
return 1;
}

// set send and receive buffers relatively large
boost::asio::socket_base::receive_buffer_size recv_size(512 * 1024);
sock.set_option(recv_size);
Expand Down