void load_dht_state(lt::session& s) { FILE* f = fopen(".dht", "rb"); if (f == NULL) return; fseek(f, 0, SEEK_END); int size = ftell(f); fseek(f, 0, SEEK_SET); if (size > 0) { std::vector<char> state; state.resize(size); fread(&state[0], 1, state.size(), f); bdecode_node e; error_code ec; bdecode(&state[0], &state[0] + state.size(), e, ec); if (ec) fprintf(stderr, "failed to parse .dht file: (%d) %s\n" , ec.value(), ec.message().c_str()); else { printf("load dht state from .dht\n"); s.load_state(e); } } fclose(f); }
void bootstrap_session(std::vector<dht_network*> networks, lt::session& ses) { lt::dht_settings sett; sett.ignore_dark_internet = false; ses.set_dht_settings(sett); lt::entry state; for (auto dht : networks) { // bootstrap off of 8 of the nodes auto router_nodes = dht->router_nodes(); char const* nodes_key; if (router_nodes.front().address().is_v6()) nodes_key = "nodes6"; else nodes_key = "nodes"; lt::entry::list_type& nodes = state["dht state"][nodes_key].list(); for (auto const& n : router_nodes) { std::string node; std::back_insert_iterator<std::string> out(node); lt::detail::write_endpoint(n, out); nodes.push_back(lt::entry(node)); } } std::vector<char> buf; lt::bencode(std::back_inserter(buf), state); lt::bdecode_node e; lt::error_code ec; lt::bdecode(&buf[0], &buf[0] + buf.size(), e, ec); ses.load_state(e); lt::settings_pack pack; pack.set_bool(lt::settings_pack::enable_dht, true); ses.apply_settings(pack); }