# libtorrent todo-list

5 important 9 relevant 12 feasible 46 notes

relevance 4 | ../src/session_impl.cpp:675 | in order to support SSL over uTP, the utp_socket manager either needs to be able to receive packets on multiple ports, or we need to peek into the first few bytes the payload stream of a socket to determine whether or not it's an SSL connection. (The former is simpler but won't do as well with NATs)
## in order to support SSL over uTP, the utp_socket manager either needs to be able to receive packets on multiple ports, or we need to peek into the first few bytes the payload stream of a socket to determine whether or not it's an SSL connection. (The former is simpler but won't do as well with NATs) #### ../src/session_impl.cpp:675
    		, m_optimistic_unchoke_time_scaler(0)
    		, m_disconnect_time_scaler(90)
    		, m_auto_scrape_time_scaler(180)
    		, m_next_explicit_cache_torrent(0)
    		, m_cache_rotation_timer(0)
    		, m_peak_up_rate(0)
    		, m_peak_down_rate(0)
    		, m_created(time_now_hires())
    		, m_last_tick(m_created)
    		, m_last_second_tick(m_created - milliseconds(900))
    		, m_last_disk_performance_warning(min_time())
    		, m_last_disk_queue_performance_warning(min_time())
    		, m_last_choke(m_created)
    		, m_next_rss_update(min_time())
    #ifndef TORRENT_DISABLE_DHT
    		, m_dht_announce_timer(m_io_service)
    		, m_dht_interval_update_torrents(0)
    #endif
    		, m_external_udp_port(0)
    		, m_udp_socket(m_io_service, m_half_open)
    		, m_utp_socket_manager(m_settings, m_udp_socket
    			, boost::bind(&session_impl::incoming_connection, this, _1))
    		, m_boost_connections(0)
    		, m_timer(m_io_service)
    		, m_lsd_announce_timer(m_io_service)
    		, m_host_resolver(m_io_service)
    		, m_current_connect_attempts(0)
    		, m_tick_residual(0)
    		, m_non_filtered_torrents(0)
    #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
    		, m_logpath(".")
    #endif
    #ifndef TORRENT_DISABLE_GEO_IP
    		, m_asnum_db(0)
    		, m_country_db(0)
    #endif
    		, m_total_failed_bytes(0)
    		, m_total_redundant_bytes(0)
    		, m_pending_auto_manage(false)
    		, m_need_auto_manage(false)
    		, m_abort(false)
    		, m_paused(false)
    		, m_incoming_connection(false)
    #if TORRENT_USE_ASSERTS && defined BOOST_HAS_PTHREADS
    		, m_network_thread(0)
    #endif
    	{
    #if TORRENT_USE_ASSERTS
    		m_posting_torrent_updates = false;
    #endif
relevance 3 | ../src/pe_crypto.cpp:228 | clean this up using destructors instead
## clean this up using destructors instead #### ../src/pe_crypto.cpp:228
    		if (e != 0) { ret = 1; goto get_out; }
    		e = gcry_mpi_scan(&secret, GCRYMPI_FMT_USG, (unsigned char const*)m_dh_local_secret
    			, sizeof(m_dh_local_secret), 0);
    		if (e != 0) { ret = 1; goto get_out; }

    		gcry_mpi_powm(remote_key, remote_key, secret, prime);

    		// remote_key is now the shared secret
    		e = gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*)m_dh_shared_secret
    			, sizeof(m_dh_shared_secret), &written, remote_key);
    		if (e != 0) { ret = 1; goto get_out; }

    		if (written < 96)
    		{
    			memmove(m_dh_shared_secret, m_dh_shared_secret
    				+ (sizeof(m_dh_shared_secret) - written), written);
    			memset(m_dh_shared_secret, 0, sizeof(m_dh_shared_secret) - written);
    		}

    get_out:
    		if (prime) gcry_mpi_release(prime);
    		if (remote_key) gcry_mpi_release(remote_key);
    		if (secret) gcry_mpi_release(secret);

    #elif defined TORRENT_USE_OPENSSL

    		BIGNUM* prime = 0;
    		BIGNUM* secret = 0;
    		BIGNUM* remote_key = 0;
    		BN_CTX* ctx = 0;
    		int size;

    		prime = BN_bin2bn(dh_prime, sizeof(dh_prime), 0);
    		if (prime == 0) { ret = 1; goto get_out; }
    		secret = BN_bin2bn((unsigned char*)m_dh_local_secret, sizeof(m_dh_local_secret), 0);
    		if (secret == 0) { ret = 1; goto get_out; }
    		remote_key = BN_bin2bn((unsigned char*)remote_pubkey, 96, 0);
    		if (remote_key == 0) { ret = 1; goto get_out; }

    		ctx = BN_CTX_new();
    		if (ctx == 0) { ret = 1; goto get_out; }
    		BN_mod_exp(remote_key, remote_key, secret, prime, ctx);
    		BN_CTX_free(ctx);

    		// remote_key is now the shared secret
    		size = BN_num_bytes(remote_key);
    		memset(m_dh_shared_secret, 0, sizeof(m_dh_shared_secret) - size);
    		BN_bn2bin(remote_key, (unsigned char*)m_dh_shared_secret + sizeof(m_dh_shared_secret) - size);

    get_out:
    		BN_free(remote_key);
relevance 3 | ../src/torrent.cpp:6425 | if peer is a really good peer, maybe we shouldn't disconnect it perhaps this logic should be disabled if we have too many idle peers (with some definition of idle)
## if peer is a really good peer, maybe we shouldn't disconnect it perhaps this logic should be disabled if we have too many idle peers (with some definition of idle) #### ../src/torrent.cpp:6425
    			return false;
    		}
    		TORRENT_ASSERT(m_connections.find(p) == m_connections.end());
    		m_connections.insert(p);
    #ifdef TORRENT_DEBUG
    		error_code ec;
    		TORRENT_ASSERT(p->remote() == p->get_socket()->remote_endpoint(ec) || ec);
    #endif

    		TORRENT_ASSERT(p->peer_info_struct() != NULL);

    		// we need to do this after we've added the peer to the policy
    		// since that's when the peer is assigned its peer_info object,
    		// which holds the rank
    		if (maybe_replace_peer)
    		{
    			// now, find the lowest rank peer and disconnect that
    			// if it's lower rank than the incoming connection
    			peer_connection* peer = find_lowest_ranking_peer();

    			if (peer && peer->peer_rank() < p->peer_rank())
    			{
    #if defined TORRENT_LOGGING
    				debug_log("CLOSING CONNECTION \"%s\" peer list full (low peer rank) "
    					"connections: %d limit: %d"
    					, print_endpoint(peer->remote()).c_str()
    					, int(m_connections.size())
    					, m_max_connections);
    #endif
    				peer->disconnect(errors::too_many_connections);
    				p->peer_disconnected_other();
    			}
    			else
    			{
    #if defined TORRENT_LOGGING
    				debug_log("CLOSING CONNECTION \"%s\" peer list full (low peer rank) "
    					"connections: %d limit: %d"
    					, print_endpoint(p->remote()).c_str()
    					, int(m_connections.size())
    					, m_max_connections);
    #endif
    				p->disconnect(errors::too_many_connections);
    				// we have to do this here because from the peer's point of
    				// it wasn't really attached to the torrent, but we do need
    				// to let policy know we're removing it
    				remove_peer(p);
    				return false;
    			}
    		}

    #if TORRENT_USE_INVARIANT_CHECKS
relevance 3 | ../src/web_peer_connection.cpp:580 | just make this peer not have the pieces associated with the file we just requested. Only when it doesn't have any of the file do the following
## just make this peer not have the pieces associated with the file we just requested. Only when it doesn't have any of the file do the following #### ../src/web_peer_connection.cpp:580
    			{
    				++m_num_responses;

    				if (m_parser.connection_close())
    				{
    					incoming_choke();
    					if (m_num_responses == 1)
    						m_web->supports_keepalive = false;
    				}

    #ifdef TORRENT_VERBOSE_LOGGING
    				peer_log("*** STATUS: %d %s", m_parser.status_code(), m_parser.message().c_str());
    				std::multimap<std::string, std::string> const& headers = m_parser.headers();
    				for (std::multimap<std::string, std::string>::const_iterator i = headers.begin()
    					, end(headers.end()); i != end; ++i)
    					peer_log("   %s: %s", i->first.c_str(), i->second.c_str());
    #endif
    				// if the status code is not one of the accepted ones, abort
    				if (!is_ok_status(m_parser.status_code()))
    				{
    					int retry_time = atoi(m_parser.header("retry-after").c_str());
    					if (retry_time <= 0) retry_time = m_ses.settings().urlseed_wait_retry;
    					// temporarily unavailable, retry later
    					t->retry_web_seed(this, retry_time);
    					std::string error_msg = to_string(m_parser.status_code()).elems
    						+ (" " + m_parser.message());
    					if (m_ses.m_alerts.should_post<url_seed_alert>())
    					{
    						m_ses.m_alerts.post_alert(url_seed_alert(t->get_handle(), m_url
    							, error_msg));
    					}
    					m_statistics.received_bytes(0, bytes_transferred);
    					disconnect(error_code(m_parser.status_code(), get_http_category()), 1);
    #ifdef TORRENT_DEBUG
    					TORRENT_ASSERT(m_statistics.last_payload_downloaded()
    						+ m_statistics.last_protocol_downloaded()
    						== dl_target);
    #endif
    					return;
    				}
    				if (is_redirect(m_parser.status_code()))
    				{
    					// this means we got a redirection request
    					// look for the location header
    					std::string location = m_parser.header("location");
    					m_statistics.received_bytes(0, bytes_transferred);

    					if (location.empty())
    					{
    						// we should not try this server again.
    						t->remove_web_seed(this);
relevance 3 | ../src/kademlia/get_item.cpp:220 | we don't support CAS errors here! we need a custom observer
## we don't support CAS errors here! we need a custom observer #### ../src/kademlia/get_item.cpp:220
    	TORRENT_LOG(node) << "sending put [ v: \"" << m_data.value()
    		<< "\" seq: " << (m_data.is_mutable() ? m_data.seq() : -1)
    		<< " nodes: " << v.size() << " ]" ;
    #endif

    	// create a dummy traversal_algorithm
    	boost::intrusive_ptr<traversal_algorithm> algo(
    		new traversal_algorithm(m_node, (node_id::min)()));

    	// store on the first k nodes
    	for (std::vector<std::pair<node_entry, std::string> >::const_iterator i = v.begin()
    		, end(v.end()); i != end; ++i)
    	{
    #ifdef TORRENT_DHT_VERBOSE_LOGGING
    		TORRENT_LOG(node) << "  put-distance: " << (160 - distance_exp(m_target, i->first.id));
    #endif

    		void* ptr = m_node.m_rpc.allocate_observer();
    		if (ptr == 0) return;

    		observer_ptr o(new (ptr) announce_observer(algo, i->first.ep(), i->first.id));
    #if TORRENT_USE_ASSERTS
    		o->m_in_constructor = false;
    #endif
    		entry e;
    		e["y"] = "q";
    		e["q"] = "put";
    		entry& a = e["a"];
    		a["v"] = m_data.value();
    		a["token"] = i->second;
    		if (m_data.is_mutable())
    		{
    			a["k"] = std::string(m_data.pk().data(), item_pk_len);
    			a["seq"] = m_data.seq();
    			a["sig"] = std::string(m_data.sig().data(), item_sig_len);
    			if (!m_data.salt().empty())
    			{
    				a["salt"] = m_data.salt();
    			}
    		}
    		m_node.m_rpc.invoke(e, i->first.ep(), o);
    	}
    }

    void get_item_observer::reply(msg const& m)
    {
    	char const* pk = NULL;
    	char const* sig = NULL;
    	boost::uint64_t seq = 0;

    	lazy_entry const* r = m.message.dict_find_dict("r");
relevance 3 | ../src/kademlia/routing_table.cpp:875 | move the lowest priority nodes to the replacement bucket
## move the lowest priority nodes to the replacement bucket #### ../src/kademlia/routing_table.cpp:875
    	bucket_t& b = m_buckets[bucket_index].live_nodes;
    	bucket_t& rb = m_buckets[bucket_index].replacements;

    	// move any node whose (160 - distane_exp(m_id, id)) >= (i - m_buckets.begin())
    	// to the new bucket
    	int new_bucket_size = bucket_limit(bucket_index + 1);
    	for (bucket_t::iterator j = b.begin(); j != b.end();)
    	{
    		if (distance_exp(m_id, j->id) >= 159 - bucket_index)
    		{
    			++j;
    			continue;
    		}
    		// this entry belongs in the new bucket
    		new_bucket.push_back(*j);
    		j = b.erase(j);
    	}

    	if (b.size() > bucket_size_limit)
    	{
    		for (bucket_t::iterator i = b.begin() + bucket_size_limit
    			, end(b.end()); i != end; ++i)
    		{
    			rb.push_back(*i);
    		}

    		b.resize(bucket_size_limit);
    	}

    	// split the replacement bucket as well. If the live bucket
    	// is not full anymore, also move the replacement entries
    	// into the main bucket
    	for (bucket_t::iterator j = rb.begin(); j != rb.end();)
    	{
    		if (distance_exp(m_id, j->id) >= 159 - bucket_index)
    		{
    			if (int(b.size()) >= bucket_size_limit)
    			{
    				++j;
    				continue;
    			}
    			b.push_back(*j);
    		}
    		else
    		{
    			// this entry belongs in the new bucket
    			if (int(new_bucket.size()) < new_bucket_size)
    				new_bucket.push_back(*j);
    			else
    				new_replacement_bucket.push_back(*j);
    		}
relevance 2 | ../src/file_storage.cpp:485 | it would be nice if file_entry::filehash could be taken into account as well, and if the file_storage object could actually hold copies of filehash
## it would be nice if file_entry::filehash could be taken into account as well, and if the file_storage object could actually hold copies of filehash #### ../src/file_storage.cpp:485
    		e.executable_attribute = (flags & attribute_executable) != 0;
    		if ((flags & attribute_symlink) && m_symlinks.size() < internal_file_entry::not_a_symlink - 1)
    		{
    			e.symlink_attribute = 1;
    			e.symlink_index = m_symlinks.size();
    			m_symlinks.push_back(symlink_path);
    		}
    		else
    			e.symlink_attribute = 0;

    		if (mtime)
    		{
    			if (m_mtime.size() < m_files.size()) m_mtime.resize(m_files.size());
    			m_mtime[m_files.size() - 1] = mtime;
    		}

    		update_path_index(e);
    		m_total_size += size;
    	}

    	void file_storage::add_file(file_entry const& ent, char const* filehash)
    	{
    		TORRENT_ASSERT_PRECOND(ent.size >= 0);
    		if (!has_parent_path(ent.path))
    		{
    			// you have already added at least one file with a
    			// path to the file (branch_path), which means that
    			// all the other files need to be in the same top
    			// directory as the first file.
    			TORRENT_ASSERT_PRECOND(m_files.empty());
    			m_name = ent.path;
    		}
    		else
    		{
    			if (m_files.empty())
    				m_name = split_path(ent.path).c_str();
    		}
    		internal_file_entry ife(ent);
    		int file_index = m_files.size();
    		m_files.push_back(ife);
    		internal_file_entry& e = m_files.back();
    		e.offset = m_total_size;
    		m_total_size += e.size;
    		if (filehash)
    		{
    			if (m_file_hashes.size() < m_files.size()) m_file_hashes.resize(m_files.size());
    			m_file_hashes[m_files.size() - 1] = filehash;
    		}
    		if (!ent.symlink_path.empty() && m_symlinks.size() < internal_file_entry::not_a_symlink - 1)
    		{
    			e.symlink_index = m_symlinks.size();
relevance 2 | ../src/torrent.cpp:678 | post alert
## post alert #### ../src/torrent.cpp:678
    		state_updated();

    		set_state(torrent_status::downloading);

    		init();
    	}

    #endif // if 0

    	void torrent::leave_seed_mode(bool skip_checking)
    	{
    		if (!m_seed_mode) return;

    		if (!skip_checking)
    		{
    			// this means the user promised we had all the
    			// files, but it turned out we didn't. This is
    			// an error.

    #if defined TORRENT_ERROR_LOGGING
    			debug_log("*** FAILED SEED MODE, rechecking");
    #endif
    		}

    #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
    		debug_log("*** LEAVING SEED MODE (%s)", m_seed_mode ? "as seed" : "as non-seed");
    #endif
    		m_seed_mode = false;
    		// seed is false if we turned out not
    		// to be a seed after all
    		if (!skip_checking)
    		{
    			set_state(torrent_status::downloading);
    			force_recheck();
    		}
    		m_num_verified = 0;
    		m_verified.clear();

    		m_policy.recalculate_connect_candidates();
    	}

    	void torrent::verified(int piece)
    	{
    		TORRENT_ASSERT(piece < int(m_verified.size()));
    		TORRENT_ASSERT(piece >= 0);
    		TORRENT_ASSERT(m_verified.get_bit(piece) == false);
    		++m_num_verified;
    		m_verified.set_bit(piece);
    	}
relevance 2 | ../src/web_peer_connection.cpp:634 | create a mapping of file-index to redirection URLs. Use that to form URLs instead. Support to reconnect to a new server without destructing this peer_connection
## create a mapping of file-index to redirection URLs. Use that to form URLs instead. Support to reconnect to a new server without destructing this peer_connection #### ../src/web_peer_connection.cpp:634
    						m_web = NULL;
    						disconnect(errors::missing_location, 2);
    #ifdef TORRENT_DEBUG
    						TORRENT_ASSERT(m_statistics.last_payload_downloaded()
    							+ m_statistics.last_protocol_downloaded()
    							== dl_target);
    #endif
    						return;
    					}

    					bool single_file_request = false;
    					if (!m_path.empty() && m_path[m_path.size() - 1] != '/')
    						single_file_request = true;

    					// add the redirected url and remove the current one
    					if (!single_file_request)
    					{
    						TORRENT_ASSERT(!m_file_requests.empty());
    						int file_index = m_file_requests.front();

    						torrent_info const& info = t->torrent_file();
    						std::string path = info.orig_files().file_path(file_index);
    #ifdef TORRENT_WINDOWS
    						convert_path_to_posix(path);
    #endif
    						path = escape_path(path.c_str(), path.length());
    						size_t i = location.rfind(path);
    						if (i == std::string::npos)
    						{
    							t->remove_web_seed(this);
    							m_web = NULL;
    							disconnect(errors::invalid_redirection, 2);
    #ifdef TORRENT_DEBUG
    							TORRENT_ASSERT(m_statistics.last_payload_downloaded()
    								+ m_statistics.last_protocol_downloaded()
    								== dl_target);
    #endif
    							return;
    						}
    						location.resize(i);
    					}
    					else
    					{
    						location = resolve_redirect_location(m_url, location);
    					}
    					t->add_web_seed(location, web_seed_entry::url_seed
    						, m_external_auth, m_extra_headers);
    					t->remove_web_seed(this);
    					m_web = NULL;
    					disconnect(errors::redirecting, 2);
    #ifdef TORRENT_DEBUG
relevance 2 | ../src/kademlia/node.cpp:66 | make this configurable in dht_settings
## make this configurable in dht_settings #### ../src/kademlia/node.cpp:66
    #include "libtorrent/kademlia/rpc_manager.hpp"
    #include "libtorrent/kademlia/routing_table.hpp"
    #include "libtorrent/kademlia/node.hpp"
    #include "libtorrent/kademlia/dht_observer.hpp"

    #include "libtorrent/kademlia/refresh.hpp"
    #include "libtorrent/kademlia/get_peers.hpp"
    #include "libtorrent/kademlia/get_item.hpp"

    #ifdef TORRENT_USE_VALGRIND
    #include <valgrind/memcheck.h>
    #endif

    namespace libtorrent { namespace dht
    {

    void incoming_error(entry& e, char const* msg, int error_code = 203);

    using detail::write_endpoint;

    enum { announce_interval = 30 };

    #ifdef TORRENT_DHT_VERBOSE_LOGGING
    TORRENT_DEFINE_LOG(node)

    extern int g_failed_announces;
    extern int g_announces;

    #endif

    // remove peers that have timed out
    void purge_peers(std::set<peer_entry>& peers)
    {
    	for (std::set<peer_entry>::iterator i = peers.begin()
    		  , end(peers.end()); i != end;)
    	{
    		// the peer has timed out
    		if (i->added + minutes(int(announce_interval * 1.5f)) < time_now())
    		{
    #ifdef TORRENT_DHT_VERBOSE_LOGGING
    			TORRENT_LOG(node) << "peer timed out at: " << i->addr;
    #endif
    			peers.erase(i++);
    		}
    		else
    			++i;
    	}
    }

    void nop() {}
relevance 2 | ../src/kademlia/node.cpp:483 | it would be nice to have a bias towards node-id prefixes that are missing in the bucket
## it would be nice to have a bias towards node-id prefixes that are missing in the bucket #### ../src/kademlia/node.cpp:483
    	// this shouldn't happen
    	TORRENT_ASSERT(m_id != ne->id);
    	if (ne->id == m_id) return;

    	int bucket = 159 - distance_exp(m_id, ne->id);
    	TORRENT_ASSERT(bucket < 160);
    	send_single_refresh(ne->ep(), bucket, ne->id);
    }

    void node_impl::send_single_refresh(udp::endpoint const& ep, int bucket
    	, node_id const& id)
    {
    	TORRENT_ASSERT(id != m_id);
    	void* ptr = m_rpc.allocate_observer();
    	if (ptr == 0) return;

    	TORRENT_ASSERT(bucket >= 0);
    	TORRENT_ASSERT(bucket <= 159);

    	// generate a random node_id within the given bucket
    	node_id mask = generate_prefix_mask(bucket + 1);
    	node_id target = generate_secret_id() & ~mask;
    	target |= m_id & mask;

    	// create a dummy traversal_algorithm
    	// this is unfortunately necessary for the observer
    	// to free itself from the pool when it's being released
    	boost::intrusive_ptr<traversal_algorithm> algo(
    		new traversal_algorithm(*this, (node_id::min)()));
    	observer_ptr o(new (ptr) ping_observer(algo, ep, id));
    #if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
    	o->m_in_constructor = false;
    #endif
    	entry e;
    	e["y"] = "q";
    	entry& a = e["a"];

    	// use get_peers instead of find_node. We'll get nodes in the response
    	// either way.
    	e["q"] = "get_peers";
    	a["info_hash"] = target.to_string();

    //	e["q"] = "find_node";
    //	a["target"] = target.to_string();
    	m_rpc.invoke(e, ep, o);
    }

    time_duration node_impl::connection_timeout()
    {
    	time_duration d = m_rpc.tick();
    	ptime now(time_now());
relevance 2 | ../src/kademlia/node.cpp:900 | find_node should write directly to the response entry
## find_node should write directly to the response entry #### ../src/kademlia/node.cpp:900
    		{
    			TORRENT_LOG(node) << " values: " << reply["values"].list().size();
    		}
    #endif
    	}
    	else if (strcmp(query, "find_node") == 0)
    	{
    		key_desc_t msg_desc[] = {
    			{"target", lazy_entry::string_t, 20, 0},
    		};

    		lazy_entry const* msg_keys[1];
    		if (!verify_message(arg_ent, msg_desc, msg_keys, 1, error_string, sizeof(error_string)))
    		{
    			incoming_error(e, error_string);
    			return;
    		}

    		sha1_hash target(msg_keys[0]->string_ptr());

    		nodes_t n;
    		m_table.find_node(target, n, 0);
    		write_nodes_entry(reply, n);
    	}
    	else if (strcmp(query, "announce_peer") == 0)
    	{
    		key_desc_t msg_desc[] = {
    			{"info_hash", lazy_entry::string_t, 20, 0},
    			{"port", lazy_entry::int_t, 0, 0},
    			{"token", lazy_entry::string_t, 0, 0},
    			{"n", lazy_entry::string_t, 0, key_desc_t::optional},
    			{"seed", lazy_entry::int_t, 0, key_desc_t::optional},
    			{"implied_port", lazy_entry::int_t, 0, key_desc_t::optional},
    		};

    		lazy_entry const* msg_keys[6];
    		if (!verify_message(arg_ent, msg_desc, msg_keys, 6, error_string, sizeof(error_string)))
    		{
    #ifdef TORRENT_DHT_VERBOSE_LOGGING
    			++g_failed_announces;
    #endif
    			incoming_error(e, error_string);
    			return;
    		}

    		int port = int(msg_keys[1]->int_value());

    		// is the announcer asking to ignore the explicit
    		// listen port and instead use the source port of the packet?
    		if (msg_keys[5] && msg_keys[5]->int_value() != 0)
    			port = m.addr.port();
relevance 2 | ../src/kademlia/node_id.cpp:136 | this could be optimized if SSE 4.2 is available. It could also be optimized given that we have a fixed length
## this could be optimized if SSE 4.2 is available. It could also be optimized given that we have a fixed length #### ../src/kademlia/node_id.cpp:136
    		b6 = ip_.to_v6().to_bytes();
    		ip = &b6[0];
    		num_octets = 8;
    		mask = v6mask;
    	}
    	else
    #endif
    	{
    		b4 = ip_.to_v4().to_bytes();
    		ip = &b4[0];
    		num_octets = 4;
    		mask = v4mask;
    	}

    	for (int i = 0; i < num_octets; ++i)
    		ip[i] &= mask[i];

    	ip[0] |= (r & 0x7) << 5;

    	// this is the crc32c (Castagnoli) polynomial
    	boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc;
    	crc.process_block(ip, ip + num_octets);
    	boost::uint32_t c = crc.checksum();
    	node_id id;

    	id[0] = (c >> 24) & 0xff;
    	id[1] = (c >> 16) & 0xff;
    	id[2] = ((c >> 8) & 0xf8) | (random() & 0x7);

    	for (int i = 3; i < 19; ++i) id[i] = random() & 0xff;
    	id[19] = r & 0xff;

    	return id;
    }

    static boost::uint32_t secret = 0;

    void make_id_secret(node_id& in)
    {
    	if (secret == 0) secret = (random() % 0xfffffffe) + 1;

    	boost::uint32_t rand = random();

    	// generate the last 4 bytes as a "signature" of the previous 4 bytes. This
    	// lets us verify whether a hash came from this function or not in the future.
    	hasher h((char*)&secret, 4);
    	h.update((char*)&rand, 4);
    	sha1_hash secret_hash = h.final();
    	memcpy(&in[20-4], &secret_hash[0], 4);
    	memcpy(&in[20-8], &rand, 4);
    }
relevance 2 | ../include/libtorrent/peer_connection.hpp:1162 | rename this target queue size
## rename this target queue size #### ../include/libtorrent/peer_connection.hpp:1162
    		// in the session_settings structure. But it may be lowered
    		// if the peer is known to require a smaller limit (like BitComet).
    		// or if the extended handshake sets a limit.
    		// web seeds also has a limit on the queue size.
    		int m_max_out_request_queue;

    		// when using the BitTyrant choker, this is our
    		// estimated reciprocation rate. i.e. the rate
    		// we need to send to this peer for it to unchoke
    		// us
    		int m_est_reciprocation_rate;

    		// estimated round trip time to this peer
    		// based on the time from when async_connect
    		// was called to when on_connection_complete
    		// was called. The rtt is specified in milliseconds
    		boost::uint16_t m_rtt;

    		// the number of request we should queue up
    		// at the remote end.
    		boost::uint16_t m_desired_queue_size;

    #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
    		// in case the session settings is set
    		// to resolve countries, this is set to
    		// the two character country code this
    		// peer resides in.
    		char m_country[2];
    #endif

    		// if set to non-zero, this peer will always prefer
    		// to request entire n pieces, rather than blocks.
    		// where n is the value of this variable.
    		// if it is 0, the download rate limit setting
    		// will be used to determine if whole pieces
    		// are preferred.
    		boost::uint8_t m_prefer_whole_pieces;

    		// if this is true, the disconnection
    		// timestamp is not updated when the connection
    		// is closed. This means the time until we can
    		// reconnect to this peer is shorter, and likely
    		// immediate.
    		bool m_fast_reconnect:1;

    		// is true if it was we that connected to the peer
    		// and false if we got an incoming connection
    		// could be considered: true = local, false = remote
    		bool m_outgoing:1;

    		// if this is set to true, the peer will not
relevance 2 | ../include/libtorrent/session.hpp:215 | the ip filter should probably be saved here too
## the ip filter should probably be saved here too #### ../include/libtorrent/session.hpp:215
    			TORRENT_LOGPATH_ARG_DEFAULT)
    		{
    			TORRENT_CFG();
    			TORRENT_ASSERT(listen_port_range.first > 0);
    			TORRENT_ASSERT(listen_port_range.first <= listen_port_range.second);
    			init(listen_port_range, listen_interface, print, alert_mask);
    #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
    			set_log_path(logpath);
    #endif
    			start(flags);
    		}

    		// The destructor of session will notify all trackers that our torrents
    		// have been shut down. If some trackers are down, they will time out.
    		// All this before the destructor of session returns. So, it's advised
    		// that any kind of interface (such as windows) are closed before
    		// destructing the session object. Because it can take a few second for
    		// it to finish. The timeout can be set with ``set_settings()``.
    		~session();

    		// flags that determines which aspects of the session should be
    		// saved when calling save_state().
    		enum save_state_flags_t
    		{
    			// saves settings (i.e. the session_settings)
    			save_settings =     0x001,

    			// saves dht_settings
    			save_dht_settings = 0x002,

    			// saves dht state such as nodes and node-id, possibly accelerating
    			// joining the DHT if provided at next session startup.
    			save_dht_state =    0x004,

    			// save proxy_settings
    			save_proxy =        0x008,

    			// save i2p_proxy settings
    			save_i2p_proxy =    0x010,

    			// save pe_settings
    			save_encryption_settings = 0x020,

    			// internal
    			save_as_map =       0x040,

    			// saves RSS feeds
    			save_feeds =        0x080

    #ifndef TORRENT_NO_DEPRECATE
relevance 1 | ../src/http_seed_connection.cpp:115 | in chunked encoding mode, this assert won't hold. the chunk headers should be subtracted from the receive_buffer_size
## in chunked encoding mode, this assert won't hold. the chunk headers should be subtracted from the receive_buffer_size #### ../src/http_seed_connection.cpp:115
    	boost::optional<piece_block_progress>
    	http_seed_connection::downloading_piece_progress() const
    	{
    		if (m_requests.empty())
    			return boost::optional<piece_block_progress>();

    		boost::shared_ptr<torrent> t = associated_torrent().lock();
    		TORRENT_ASSERT(t);

    		piece_block_progress ret;

    		peer_request const& pr = m_requests.front();
    		ret.piece_index = pr.piece;
    		if (!m_parser.header_finished())
    		{
    			ret.bytes_downloaded = 0;
    		}
    		else
    		{
    			int receive_buffer_size = receive_buffer().left() - m_parser.body_start();
    			TORRENT_ASSERT_VAL(receive_buffer_size <= t->block_size(), receive_buffer_size);
    			ret.bytes_downloaded = t->block_size() - receive_buffer_size;
    		}
    		// this is used to make sure that the block_index stays within
    		// bounds. If the entire piece is downloaded, the block_index
    		// would otherwise point to one past the end
    		int correction = ret.bytes_downloaded ? -1 : 0;
    		ret.block_index = (pr.start + ret.bytes_downloaded + correction) / t->block_size();
    		ret.full_block_bytes = t->block_size();
    		const int last_piece = t->torrent_file().num_pieces() - 1;
    		if (ret.piece_index == last_piece && ret.block_index
    			== t->torrent_file().piece_size(last_piece) / t->block_size())
    			ret.full_block_bytes = t->torrent_file().piece_size(last_piece) % t->block_size();
    		return ret;
    	}

    	void http_seed_connection::write_request(peer_request const& r)
    	{
    		INVARIANT_CHECK;

    		boost::shared_ptr<torrent> t = associated_torrent().lock();
    		TORRENT_ASSERT(t);

    		TORRENT_ASSERT(t->valid_metadata());
    		// http_seeds don't support requesting more than one piece
    		// at a time
    		TORRENT_ASSERT(r.length <= t->torrent_file().piece_size(r.piece));

    		std::string request;
    		request.reserve(400);
relevance 1 | ../src/peer_connection.cpp:2620 | peers should really be corked/uncorked outside of all completed disk operations
## peers should really be corked/uncorked outside of all completed disk operations #### ../src/peer_connection.cpp:2620
    		}

    		if (is_disconnecting()) return;

    #ifdef TORRENT_STATS
    		++m_ses.m_incoming_piece_picks;
    #endif
    		request_a_block(*t, *this);
    		send_block_requests();
    	}

    	void peer_connection::on_disk_write_complete(int ret, disk_io_job const& j
    		, peer_request p, boost::shared_ptr<torrent> t)
    	{
    #ifdef TORRENT_STATS
    		++m_ses.m_num_messages[aux::session_impl::on_disk_write_counter];
    #endif
    		TORRENT_ASSERT(m_ses.is_network_thread());

    		// flush send buffer at the end of this scope
    		cork _c(*this);

    		INVARIANT_CHECK;

    		m_outstanding_writing_bytes -= p.length;
    		TORRENT_ASSERT(m_outstanding_writing_bytes >= 0);

    #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
    //		(*m_ses.m_logger) << time_now_string() << " *** DISK_WRITE_COMPLETE [ p: "
    //			<< p.piece << " o: " << p.start << " ]\n";
    #endif

    		if (!t)
    		{
    			disconnect(j.error);
    			return;
    		}

    		// in case the outstanding bytes just dropped down
    		// to allow to receive more data
    		setup_receive(read_async);

    		piece_block block_finished(p.piece, p.start / t->block_size());

    		if (ret == -1)
    		{
    			// handle_disk_error may disconnect us
    			t->handle_disk_error(j, this);
    			return;
    		}
relevance 1 | ../src/session_impl.cpp:5630 | report the proper address of the router as the source IP of this understanding of our external address, instead of the empty address
## report the proper address of the router as the source IP of this understanding of our external address, instead of the empty address #### ../src/session_impl.cpp:5630
    	void session_impl::on_port_mapping(int mapping, address const& ip, int port
    		, error_code const& ec, int map_transport)
    	{
    		TORRENT_ASSERT(is_network_thread());

    		TORRENT_ASSERT(map_transport >= 0 && map_transport <= 1);

    		if (mapping == m_udp_mapping[map_transport] && port != 0)
    		{
    			m_external_udp_port = port;
    			if (m_alerts.should_post<portmap_alert>())
    				m_alerts.post_alert(portmap_alert(mapping, port
    					, map_transport));
    			return;
    		}

    		if (mapping == m_tcp_mapping[map_transport] && port != 0)
    		{
    			if (ip != address())
    			{
    				set_external_address(ip, source_router, address());
    			}

    			if (!m_listen_sockets.empty()) {
    				m_listen_sockets.front().external_address = ip;
    				m_listen_sockets.front().external_port = port;
    			}
    			if (m_alerts.should_post<portmap_alert>())
    				m_alerts.post_alert(portmap_alert(mapping, port
    					, map_transport));
    			return;
    		}

    		if (ec)
    		{
    			if (m_alerts.should_post<portmap_error_alert>())
    				m_alerts.post_alert(portmap_error_alert(mapping
    					, map_transport, ec));
    		}
    		else
    		{
    			if (m_alerts.should_post<portmap_alert>())
    				m_alerts.post_alert(portmap_alert(mapping, port
    					, map_transport));
    		}
    	}

    	session_status session_impl::status() const
    	{
    //		INVARIANT_CHECK;
    		TORRENT_ASSERT(is_network_thread());
relevance 1 | ../src/session_impl.cpp:6419 | we only need to do this if our global IPv4 address has changed since the DHT (currently) only supports IPv4. Since restarting the DHT is kind of expensive, it would be nice to not do it unnecessarily
## we only need to do this if our global IPv4 address has changed since the DHT (currently) only supports IPv4. Since restarting the DHT is kind of expensive, it would be nice to not do it unnecessarily #### ../src/session_impl.cpp:6419
    #endif

    		if (!m_external_ip.cast_vote(ip, source_type, source)) return;

    #if defined TORRENT_VERBOSE_LOGGING
    		session_log("  external IP updated");
    #endif

    		if (m_alerts.should_post<external_ip_alert>())
    			m_alerts.post_alert(external_ip_alert(ip));

    		for (torrent_map::iterator i = m_torrents.begin()
    			, end(m_torrents.end()); i != end; ++i)
    		{
    			i->second->new_external_ip();
    		}

    		// since we have a new external IP now, we need to
    		// restart the DHT with a new node ID
    #ifndef TORRENT_DISABLE_DHT
    		if (m_dht)
    		{
    			entry s = m_dht->state();
    			int cur_state = 0;
    			int prev_state = 0;
    			entry* nodes1 = s.find_key("nodes");
    			if (nodes1 && nodes1->type() == entry::list_t) cur_state = nodes1->list().size();
    			entry* nodes2 = m_dht_state.find_key("nodes");
    			if (nodes2 && nodes2->type() == entry::list_t) prev_state = nodes2->list().size();
    			if (cur_state > prev_state) m_dht_state = s;
    			std::vector<dht::node_id> torrents;
    			m_dht->get_announces(&torrents);
    			start_dht(m_dht_state);

    			// force re-announce any torrent that had a DHT announce in flight
    			for (std::vector<dht::node_id>::iterator i = torrents.begin()
    				, end(torrents.end()); i != end; ++i)
    			{
    				boost::weak_ptr<torrent> wt = find_torrent(*i);
    				boost::shared_ptr<torrent> t = wt.lock();
    				if (!t) continue;

    				prioritize_dht(t);
    			}
    		}
    #endif
    	}

    	void session_impl::free_disk_buffer(char* buf)
    	{
    		m_disk_thread.free_buffer(buf);
relevance 1 | ../src/torrent.cpp:1077 | make this depend on the error and on the filesystem the files are being downloaded to. If the error is no_space_left_on_device and the filesystem doesn't support sparse files, only zero the priorities of the pieces that are at the tails of all files, leaving everything up to the highest written piece in each file
## make this depend on the error and on the filesystem the files are being downloaded to. If the error is no_space_left_on_device and the filesystem doesn't support sparse files, only zero the priorities of the pieces that are at the tails of all files, leaving everything up to the highest written piece in each file #### ../src/torrent.cpp:1077
    			if (c) c->disconnect(errors::no_memory);
    			return;
    		}

    		// notify the user of the error
    		if (alerts().should_post<file_error_alert>())
    			alerts().post_alert(file_error_alert(j.error_file, get_handle(), j.error));

    		// put the torrent in an error-state
    		set_error(j.error, j.error_file);

    		if (j.action == disk_io_job::write
    			&& (j.error == boost::system::errc::read_only_file_system
    			|| j.error == boost::system::errc::permission_denied
    			|| j.error == boost::system::errc::operation_not_permitted
    			|| j.error == boost::system::errc::no_space_on_device
    			|| j.error == boost::system::errc::file_too_large))
    		{
    			// if we failed to write, stop downloading and just
    			// keep seeding.
    			set_upload_mode(true);
    			return;
    		}

    		// if the error appears to be more serious than a full disk, just pause the torrent
    		pause();
    	}

    	void torrent::on_disk_read_complete(int ret, disk_io_job const& j
    		, peer_request r, read_piece_struct* rp)
    	{
    		TORRENT_ASSERT(m_ses.is_network_thread());

    		disk_buffer_holder buffer(m_ses, j.buffer);

    		--rp->blocks_left;
    		if (ret != r.length)
    		{
    			rp->fail = true;
    			rp->error = j.error;
    			handle_disk_error(j);
    		}
    		else
    		{
    			std::memcpy(rp->piece_data.get() + r.start, j.buffer, r.length);
    		}

    		if (rp->blocks_left == 0)
    		{
    			int size = m_torrent_file->piece_size(r.piece);
    			if (rp->fail)
relevance 1 | ../src/torrent.cpp:5675 | save the send_stats state instead of throwing them away it may pose an issue when downgrading though
## save the send_stats state instead of throwing them away it may pose an issue when downgrading though #### ../src/torrent.cpp:5675
    					for (int k = 0; k < bits; ++k)
    						v |= (i->info[j*8+k].state == piece_picker::block_info::state_finished)
    						? (1 << k) : 0;
    					bitmask.append(1, v);
    					TORRENT_ASSERT(bits == 8 || j == num_bitmask_bytes - 1);
    				}
    				piece_struct["bitmask"] = bitmask;
    				// push the struct onto the unfinished-piece list
    				up.push_back(piece_struct);
    			}
    		}

    		// save trackers
    		entry::list_type& tr_list = ret["trackers"].list();
    		tr_list.push_back(entry::list_type());
    		int tier = 0;
    		for (std::vector<announce_entry>::const_iterator i = m_trackers.begin()
    			, end(m_trackers.end()); i != end; ++i)
    		{
    			// don't save trackers we can't trust
    			if (i->send_stats == false) continue;
    			if (i->tier == tier)
    			{
    				tr_list.back().list().push_back(i->url);
    			}
    			else
    			{
    				tr_list.push_back(entry::list_t);
    				tr_list.back().list().push_back(i->url);
    				tier = i->tier;
    			}
    		}

    		// save web seeds
    		if (!m_web_seeds.empty())
    		{
    			entry::list_type& url_list = ret["url-list"].list();
    			entry::list_type& httpseed_list = ret["httpseeds"].list();
    			for (std::list<web_seed_entry>::const_iterator i = m_web_seeds.begin()
    				, end(m_web_seeds.end()); i != end; ++i)
    			{
    				if (i->type == web_seed_entry::url_seed)
    					url_list.push_back(i->url);
    				else if (i->type == web_seed_entry::http_seed)
    					httpseed_list.push_back(i->url);
    			}
    		}

    		// write have bitmask
    		// the pieces string has one byte per piece. Each
    		// byte is a bitmask representing different properties
relevance 1 | ../src/torrent.cpp:6614 | should disconnect all peers that have the pieces we have not just seeds. It would be pretty expensive to check all pieces for all peers though
## should disconnect all peers that have the pieces we have not just seeds. It would be pretty expensive to check all pieces for all peers though #### ../src/torrent.cpp:6614
    		TORRENT_ASSERT(m_state != torrent_status::finished && m_state != torrent_status::seeding);

    		set_state(torrent_status::finished);
    		set_queue_position(-1);

    		// we have to call completed() before we start
    		// disconnecting peers, since there's an assert
    		// to make sure we're cleared the piece picker
    		if (is_seed()) completed();

    		send_upload_only();

    		state_updated();

    		if (m_completed_time == 0)
    			m_completed_time = time(0);

    		// disconnect all seeds
    		if (settings().close_redundant_connections)
    		{
    			std::vector<peer_connection*> seeds;
    			for (peer_iterator i = m_connections.begin();
    				i != m_connections.end(); ++i)
    			{
    				peer_connection* p = *i;
    				TORRENT_ASSERT(p->associated_torrent().lock().get() == this);
    				if (p->upload_only())
    				{
    #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
    					p->peer_log("*** SEED, CLOSING CONNECTION");
    #endif
    					seeds.push_back(p);
    				}
    			}
    			std::for_each(seeds.begin(), seeds.end()
    				, boost::bind(&peer_connection::disconnect, _1, errors::torrent_finished, 0));
    		}

    		if (m_abort) return;

    		m_policy.recalculate_connect_candidates();

    		if (m_storage)
    		{
    			// we need to keep the object alive during this operation
    			m_storage->async_release_files(
    				boost::bind(&torrent::on_files_released, shared_from_this(), _1, _2));
    		}

    		// this torrent just completed downloads, which means it will fall
    		// under a different limit with the auto-manager. Make sure we
relevance 1 | ../src/torrent_info.cpp:164 | we might save constructing a std::string if this would take a char const* instead
## we might save constructing a std::string if this would take a char const* instead #### ../src/torrent_info.cpp:164
    			// encode codepoint into utf-8
    			cp = &codepoint;
    			UTF8 sequence[5];
    			UTF8* start = sequence;
    			res = ConvertUTF32toUTF8((const UTF32**)&cp, cp + 1, &start, start + 5, lenientConversion);
    			TORRENT_ASSERT(res == conversionOK);

    			for (int i = 0; i < start - sequence; ++i)
    				tmp_path += (char)sequence[i];
    		}

    		// the encoding was not valid utf-8
    		// save the original encoding and replace the
    		// commonly used path with the correctly
    		// encoded string
    		if (!valid_encoding) target = tmp_path;
    		return valid_encoding;
    	}

    	bool valid_path_element(std::string const& element)
    	{
    		if (element.empty()
    			|| element == "." || element == ".."
    			|| element[0] == '/' || element[0] == '\\'
    			|| element[element.size()-1] == ':')
    			return false;
    		return true;
    	}

    	TORRENT_EXTRA_EXPORT void trim_path_element(std::string& element)
    	{
    		const int max_path_len = TORRENT_MAX_PATH;

    		// on windows, the max path is expressed in
    		// unicode characters, not bytes
    #if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING
    		std::wstring path_element;
    		utf8_wchar(element, path_element);
    		if (path_element.size() > max_path_len)
    		{
    			// truncate filenames that are too long. But keep extensions!
    			std::wstring ext;
    			wchar_t const* ext1 = wcsrchr(path_element.c_str(), '.');
    			if (ext1 != NULL) ext = ext1;

    			if (ext.size() > 15)
    			{
    				path_element.resize(max_path_len);
    			}
    			else
relevance 1 | ../src/torrent_info.cpp:388 | this logic should be a separate step done once the torrent is loaded, and the original filenames should be preserved!
## this logic should be a separate step done once the torrent is loaded, and the original filenames should be preserved! #### ../src/torrent_info.cpp:388
    			while (*s1 != 0 || *s2 != 0)
    			{
    				c1 = to_lower(*s1);
    				c2 = to_lower(*s2);
    				if (c1 < c2) return true;
    				if (c1 > c2) return false;
    				++s1;
    				++s2;
    			}
    			return false;
    		}
    	};

    	bool extract_files(lazy_entry const& list, file_storage& target
    		, std::string const& root_dir, ptrdiff_t info_ptr_diff)
    	{
    		if (list.type() != lazy_entry::list_t) return false;
    		target.reserve(list.list_size());

    		std::set<std::string, string_less_no_case> files;

    		for (int i = 0, end(list.list_size()); i < end; ++i)
    		{
    			lazy_entry const* file_hash = 0;
    			time_t mtime = 0;
    			file_entry e;
    			lazy_entry const* fee = 0;
    			if (!extract_single_file(*list.list_at(i), e, root_dir
    				, &file_hash, &fee, &mtime))
    				return false;

    			// as long as this file already exists
    			// increase the counter
    			int cnt = 0;
    			if (!files.insert(e.path).second)
    			{
    				std::string base = remove_extension(e.path);
    				std::string ext = extension(e.path);
    				do
    				{
    					++cnt;
    					char new_ext[50];
    					snprintf(new_ext, sizeof(new_ext), ".%d%s", cnt, ext.c_str());
    					e.path = base + new_ext;
    				} while (!files.insert(e.path).second);
    			}
    			target.add_file(e, file_hash ? file_hash->string_ptr() + info_ptr_diff : 0);

    			// This is a memory optimization! Instead of having
    			// each entry keep a string for its filename, make it
relevance 1 | ../src/torrent_info.cpp:424 | once the filename renaming is removed from here this check can be removed as well
## once the filename renaming is removed from here this check can be removed as well #### ../src/torrent_info.cpp:424
    			// increase the counter
    			int cnt = 0;
    			if (!files.insert(e.path).second)
    			{
    				std::string base = remove_extension(e.path);
    				std::string ext = extension(e.path);
    				do
    				{
    					++cnt;
    					char new_ext[50];
    					snprintf(new_ext, sizeof(new_ext), ".%d%s", cnt, ext.c_str());
    					e.path = base + new_ext;
    				} while (!files.insert(e.path).second);
    			}
    			target.add_file(e, file_hash ? file_hash->string_ptr() + info_ptr_diff : 0);

    			// This is a memory optimization! Instead of having
    			// each entry keep a string for its filename, make it
    			// simply point into the info-section buffer
    			int last_index = target.num_files() - 1;
    			if (fee && target.file_name(last_index) == fee->string_value())
    			{
    				// this string pointer does not necessarily point into
    				// the m_info_section buffer.
    				char const* str_ptr = fee->string_ptr() + info_ptr_diff;
    				target.rename_file_borrow(last_index, str_ptr, fee->string_length());
    			}
    		}
    		return true;
    	}

    	int merkle_get_parent(int tree_node)
    	{
    		// node 0 doesn't have a parent
    		TORRENT_ASSERT(tree_node > 0);
    		return (tree_node - 1) / 2;
    	}

    	int merkle_get_sibling(int tree_node)
    	{
    		// node 0 doesn't have a sibling
    		TORRENT_ASSERT(tree_node > 0);
    		// even numbers have their sibling to the left
    		// odd numbers have their sibling to the right
    		return tree_node + (tree_node&1?1:-1);
    	}

    	int merkle_num_nodes(int leafs)
    	{
    		TORRENT_ASSERT(leafs > 0);
    		return (leafs << 1) - 1;
relevance 1 | ../include/libtorrent/ip_voter.hpp:124 | instead, have one instance per possible subnet, global IPv4, global IPv6, loopback, 192.168.x.x, 10.x.x.x, etc.
## instead, have one instance per possible subnet, global IPv4, global IPv6, loopback, 192.168.x.x, 10.x.x.x, etc. #### ../include/libtorrent/ip_voter.hpp:124
    		// away all the votes and started from scratch, in case
    		// our IP has changed
    		ptime m_last_rotate;
    	};

    	// this keeps track of multiple external IPs (for now, just IPv6 and IPv4, but
    	// it could be extended to deal with loopback and local network addresses as well)
    	struct TORRENT_EXTRA_EXPORT external_ip
    	{
    		// returns true if a different IP is the top vote now
    		// i.e. we changed our idea of what our external IP is
    		bool cast_vote(address const& ip, int source_type, address const& source);

    		// the external IP as it would be observed from `ip`
    		address external_address(address const& ip) const;

    	private:

    		// for now, assume one external IPv4 and one external IPv6 address
    		// 0 = IPv4 1 = IPv6
    		ip_voter m_vote_group[2];
    	};

    }

    #endif
relevance 1 | ../include/libtorrent/web_peer_connection.hpp:130 | if we make this be a disk_buffer_holder instead we would save a copy sometimes use allocate_disk_receive_buffer and release_disk_receive_buffer
## if we make this be a disk_buffer_holder instead we would save a copy sometimes use allocate_disk_receive_buffer and release_disk_receive_buffer #### ../include/libtorrent/web_peer_connection.hpp:130
    		// returns the block currently being
    		// downloaded. And the progress of that
    		// block. If the peer isn't downloading
    		// a piece for the moment, the boost::optional
    		// will be invalid.
    		boost::optional<piece_block_progress> downloading_piece_progress() const;

    		void handle_padfile(buffer::const_interval& recv_buffer);

    		// this has one entry per http-request
    		// (might be more than the bt requests)
    		std::deque<int> m_file_requests;

    		std::string m_url;

    		web_seed_entry* m_web;

    		// this is used for intermediate storage of pieces
    		// that are received in more than one HTTP response
    		std::vector<char> m_piece;

    		// the number of bytes received in the current HTTP
    		// response. used to know where in the buffer the
    		// next response starts
    		size_type m_received_body;

    		// position in the current range response
    		size_type m_range_pos;

    		// the position in the current block
    		int m_block_pos;

    		// this is the offset inside the current receive
    		// buffer where the next chunk header will be.
    		// this is updated for each chunk header that's
    		// parsed. It does not necessarily point to a valid
    		// offset in the receive buffer, if we haven't received
    		// it yet. This offset never includes the HTTP header
    		size_type m_chunk_pos;

    		// this is the number of bytes we've already received
    		// from the next chunk header we're waiting for
    		int m_partial_chunk_header;

    		// the number of responses we've received so far on
    		// this connection
    		int m_num_responses;
    	};
    }
relevance 0 | ../src/bt_peer_connection.cpp:647 | this could be optimized using knuth morris pratt
## this could be optimized using knuth morris pratt #### ../src/bt_peer_connection.cpp:647
    		{
    			disconnect(errors::no_memory);
    			return;
    		}

    #ifdef TORRENT_VERBOSE_LOGGING
    		peer_log(" computed RC4 keys");
    #endif
    	}

    	int bt_peer_connection::get_syncoffset(char const* src, int src_size,
    		char const* target, int target_size) const
    	{
    		TORRENT_ASSERT(target_size >= src_size);
    		TORRENT_ASSERT(src_size > 0);
    		TORRENT_ASSERT(src);
    		TORRENT_ASSERT(target);

    		int traverse_limit = target_size - src_size;

    		for (int i = 0; i < traverse_limit; ++i)
    		{
    			char const* target_ptr = target + i;
    			if (std::equal(src, src+src_size, target_ptr))
    				return i;
    		}

    //	    // Partial sync
    // 		for (int i = 0; i < target_size; ++i)
    // 		{
    // 			// first is iterator in src[] at which mismatch occurs
    // 			// second is iterator in target[] at which mismatch occurs
    // 			std::pair<const char*, const char*> ret;
    // 			int src_sync_size;
    //  			if (i > traverse_limit) // partial sync test
    //  			{
    //  				ret = std::mismatch(src, src + src_size - (i - traverse_limit), &target[i]);
    //  				src_sync_size = ret.first - src;
    //  				if (src_sync_size == (src_size - (i - traverse_limit)))
    //  					return i;
    //  			}
    //  			else // complete sync test
    // 			{
    // 				ret = std::mismatch(src, src + src_size, &target[i]);
    // 				src_sync_size = ret.first - src;
    // 				if (src_sync_size == src_size)
    // 					return i;
    // 			}
    // 		}

            // no complete sync
relevance 0 | ../src/bt_peer_connection.cpp:2132 | if we're finished, send upload_only message
## if we're finished, send upload_only message #### ../src/bt_peer_connection.cpp:2132
    			if (msg[5 + k / 8] & (0x80 >> (k % 8))) bitfield_string[k] = '1';
    			else bitfield_string[k] = '0';
    		}
    		peer_log("==> BITFIELD [ %s ]", bitfield_string.c_str());
    #endif
    #if TORRENT_USE_ASSERTS
    		m_sent_bitfield = true;
    #endif

    		send_buffer(msg, packet_size);

    		if (num_lazy_pieces > 0)
    		{
    			for (int i = 0; i < num_lazy_pieces; ++i)
    			{
    #ifdef TORRENT_VERBOSE_LOGGING
    				peer_log("==> HAVE    [ piece: %d ]", lazy_pieces[i]);
    #endif
    				write_have(lazy_pieces[i]);
    			}
    		}

    		if (m_supports_fast)
    			send_allowed_set();
    	}

    #ifndef TORRENT_DISABLE_EXTENSIONS
    	void bt_peer_connection::write_extensions()
    	{
    		INVARIANT_CHECK;

    		TORRENT_ASSERT(m_supports_extensions);
    		TORRENT_ASSERT(m_sent_handshake);

    		entry handshake;
    		entry::dictionary_type& m = handshake["m"].dict();

    		// if we're using a proxy, our listen port won't be useful
    		// anyway.
    		if (!m_ses.settings().force_proxy && is_outgoing())
    			handshake["p"] = m_ses.listen_port();

    		// only send the port in case we bade the connection
    		// on incoming connections the other end already knows
    		// our listen port
    		if (!m_ses.m_settings.anonymous_mode)
    		{
    			handshake["v"] = m_ses.settings().handshake_client_version.empty()
    				? m_ses.settings().user_agent : m_ses.settings().handshake_client_version;
    		}
relevance 0 | ../src/bt_peer_connection.cpp:3378 | move the erasing into the loop above remove all payload ranges that has been sent
## move the erasing into the loop above remove all payload ranges that has been sent #### ../src/bt_peer_connection.cpp:3378
    			for (std::vector<range>::iterator i = m_payloads.begin();
    				i != m_payloads.end(); ++i)
    			{
    				i->start -= bytes_transferred;
    				if (i->start < 0)
    				{
    					if (i->start + i->length <= 0)
    					{
    						amount_payload += i->length;
    					}
    					else
    					{
    						amount_payload += -i->start;
    						i->length -= -i->start;
    						i->start = 0;
    					}
    				}
    			}
    		}

    		m_payloads.erase(
    			std::remove_if(m_payloads.begin(), m_payloads.end(), range_below_zero)
    			, m_payloads.end());

    		TORRENT_ASSERT(amount_payload <= (int)bytes_transferred);
    		m_statistics.sent_bytes(amount_payload, bytes_transferred - amount_payload);

    		if (amount_payload > 0)
    		{
    			boost::shared_ptr<torrent> t = associated_torrent().lock();
    			TORRENT_ASSERT(t);
    			if (t) t->update_last_upload();
    		}
    	}

    #if TORRENT_USE_INVARIANT_CHECKS
    	void bt_peer_connection::check_invariant() const
    	{
    		boost::shared_ptr<torrent> t = associated_torrent().lock();

    #ifndef TORRENT_DISABLE_ENCRYPTION
    		TORRENT_ASSERT( (bool(m_state != read_pe_dhkey) || m_dh_key_exchange.get())
    				|| !is_outgoing());

    		TORRENT_ASSERT(!m_rc4_encrypted || m_enc_handler.get());
    #endif
    		if (!in_handshake())
    		{
    			TORRENT_ASSERT(m_sent_handshake);
    		}
relevance 0 | ../src/enum_net.cpp:271 | get the MTU (and other interesting metrics) from the rt_msghdr instead
## get the MTU (and other interesting metrics) from the rt_msghdr instead #### ../src/enum_net.cpp:271
    #undef ROUNDUP
    		}

    		sa = rti_info[RTAX_GATEWAY];
    		if (sa == 0
    			|| rti_info[RTAX_DST] == 0
    			|| rti_info[RTAX_NETMASK] == 0
    			|| (sa->sa_family != AF_INET
    #if TORRENT_USE_IPV6
    				&& sa->sa_family != AF_INET6
    #endif
    				))
    			return false;

    		rt_info->gateway = sockaddr_to_address(rti_info[RTAX_GATEWAY]);
    		rt_info->destination = sockaddr_to_address(rti_info[RTAX_DST]);
    		rt_info->netmask = sockaddr_to_address(rti_info[RTAX_NETMASK]
    			, rt_info->destination.is_v4() ? AF_INET : AF_INET6);
    		if_indextoname(rtm->rtm_index, rt_info->name);

    		ifreq req;
    		memset(&req, 0, sizeof(req));
    		if_indextoname(rtm->rtm_index, req.ifr_name);
    		// ignore errors here. This is best-effort
    		ioctl(s, SIOCGIFMTU, &req);
    		rt_info->mtu = req.ifr_mtu;

    		return true;
    	}
    #endif

    #if TORRENT_USE_IFADDRS
    	bool iface_from_ifaddrs(ifaddrs *ifa, ip_interface &rv, error_code& ec)
    	{
    		int family = ifa->ifa_addr->sa_family;

    		if (family != AF_INET
    #if TORRENT_USE_IPV6
    			&& family != AF_INET6
    #endif
    		)
    		{
    			return false;
    		}

    		strncpy(rv.name, ifa->ifa_name, sizeof(rv.name));
    		rv.name[sizeof(rv.name)-1] = 0;

    		// determine address
    		rv.interface_address = sockaddr_to_address(ifa->ifa_addr);
    		// determine netmask
relevance 0 | ../src/file.cpp:1406 | is there any way to pre-fetch data from a file on windows?
## is there any way to pre-fetch data from a file on windows? #### ../src/file.cpp:1406
    	void file::init_file()
    	{
    		if (m_page_size != 0) return;

    		m_page_size = page_size();
    	}

    #endif

    	void file::hint_read(size_type file_offset, int len)
    	{
    #if defined POSIX_FADV_WILLNEED
    		posix_fadvise(m_fd, file_offset, len, POSIX_FADV_WILLNEED);
    #elif defined F_RDADVISE
    		radvisory r;
    		r.ra_offset = file_offset;
    		r.ra_count = len;
    		fcntl(m_fd, F_RDADVISE, &r);
    #else
    #endif
    	}

    	size_type file::readv(size_type file_offset, iovec_t const* bufs, int num_bufs, error_code& ec)
    	{
    #ifdef TORRENT_WINDOWS
    		if (m_file_handle == INVALID_HANDLE_VALUE)
    		{
    			ec = error_code(ERROR_INVALID_HANDLE, get_system_category());
    			return -1;
    		}
    #else
    		if (m_fd == -1)
    		{
    			ec = error_code(EBADF, get_system_category());
    			return -1;
    		}
    #endif
    		TORRENT_ASSERT((m_open_mode & rw_mask) == read_only || (m_open_mode & rw_mask) == read_write);
    		TORRENT_ASSERT(bufs);
    		TORRENT_ASSERT(num_bufs > 0);
    		TORRENT_ASSERT(is_open());

    #if defined TORRENT_WINDOWS || defined TORRENT_LINUX || defined TORRENT_DEBUG
    		// make sure m_page_size is initialized
    		init_file();
    #endif

    #ifdef TORRENT_DEBUG
    		if (m_open_mode & no_buffer)
    		{
relevance 0 | ../src/http_tracker_connection.cpp:97 | support authentication (i.e. user name and password) in the URL
## support authentication (i.e. user name and password) in the URL #### ../src/http_tracker_connection.cpp:97
    		, aux::session_impl const& ses
    		, proxy_settings const& ps
    		, std::string const& auth
    #if TORRENT_USE_I2P
    		, i2p_connection* i2p_conn
    #endif
    		)
    		: tracker_connection(man, req, ios, c)
    		, m_man(man)
    		, m_ses(ses)
    		, m_ps(ps)
    		, m_cc(cc)
    		, m_ios(ios)
    #if TORRENT_USE_I2P
    		, m_i2p_conn(i2p_conn)
    #endif
    	{}

    	void http_tracker_connection::start()
    	{
    		std::string url = tracker_req().url;

    		if (tracker_req().kind == tracker_request::scrape_request)
    		{
    			// find and replace "announce" with "scrape"
    			// in request

    			std::size_t pos = url.find("announce");
    			if (pos == std::string::npos)
    			{
    				tracker_connection::fail(error_code(errors::scrape_not_available));
    				return;
    			}
    			url.replace(pos, 8, "scrape");
    		}

    #if TORRENT_USE_I2P
    		bool i2p = is_i2p_url(url);
    #else
    		static const bool i2p = false;
    #endif

    		session_settings const& settings = m_ses.settings();

    		// if request-string already contains
    		// some parameters, append an ampersand instead
    		// of a question mark
    		size_t arguments_start = url.find('?');
    		if (arguments_start != std::string::npos)
    			url += "&";
    		else
relevance 0 | ../src/i2p_stream.cpp:225 | move this to proxy_base and use it in all proxies
## move this to proxy_base and use it in all proxies #### ../src/i2p_stream.cpp:225
    	i2p_stream::i2p_stream(io_service& io_service)
    		: proxy_base(io_service)
    		, m_id(0)
    		, m_command(cmd_create_session)
    		, m_state(0)
    	{
    #if TORRENT_USE_ASSERTS
    		m_magic = 0x1337;
    #endif
    	}

    	i2p_stream::~i2p_stream()
    	{
    #if TORRENT_USE_ASSERTS
    		TORRENT_ASSERT(m_magic == 0x1337);
    		m_magic = 0;
    #endif
    	}

    	bool i2p_stream::handle_error(error_code const& e, boost::shared_ptr<handler_type> const& h)
    	{
    		TORRENT_ASSERT(m_magic == 0x1337);
    		if (!e) return false;
    //		fprintf(stderr, "i2p error \"%s\"\n", e.message().c_str());
    		(*h)(e);
    		return true;
    	}

    	void i2p_stream::do_connect(error_code const& e, tcp::resolver::iterator i
    		, boost::shared_ptr<handler_type> h)
    	{
    		TORRENT_ASSERT(m_magic == 0x1337);
    		if (e || i == tcp::resolver::iterator())
    		{
    			(*h)(e);
    			error_code ec;
    			close(ec);
    			return;
    		}

    #if defined TORRENT_ASIO_DEBUGGING
    		add_outstanding_async("i2p_stream::connected");
    #endif
    		m_sock.async_connect(i->endpoint(), boost::bind(
    			&i2p_stream::connected, this, _1, h));
    	}

    	void i2p_stream::connected(error_code const& e, boost::shared_ptr<handler_type> h)
    	{
    		TORRENT_ASSERT(m_magic == 0x1337);
relevance 0 | ../src/packet_buffer.cpp:176 | use compare_less_wrap for this comparison as well
## use compare_less_wrap for this comparison as well #### ../src/packet_buffer.cpp:176
    		while (new_size < size)
    			new_size <<= 1;

    		void** new_storage = (void**)malloc(sizeof(void*) * new_size);

    		for (index_type i = 0; i < new_size; ++i)
    			new_storage[i] = 0;

    		for (index_type i = m_first; i < (m_first + m_capacity); ++i)
    			new_storage[i & (new_size - 1)] = m_storage[i & (m_capacity - 1)];

    		free(m_storage);

    		m_storage = new_storage;
    		m_capacity = new_size;
    	}

    	void* packet_buffer::remove(index_type idx)
    	{
    		INVARIANT_CHECK;
    		if (idx >= m_first + m_capacity)
    			return 0;

    		if (compare_less_wrap(idx, m_first, 0xffff))
    			return 0;

    		const int mask = (m_capacity - 1);
    		void* old_value = m_storage[idx & mask];
    		m_storage[idx & mask] = 0;

    		if (old_value)
    		{
    			--m_size;
    			if (m_size == 0) m_last = m_first;
    		}

    		if (idx == m_first && m_size != 0)
    		{
    			++m_first;
    			for (boost::uint32_t i = 0; i < m_capacity; ++i, ++m_first)
    				if (m_storage[m_first & mask]) break;
    			m_first &= 0xffff;
    		}

    		if (((idx + 1) & 0xffff) == m_last && m_size != 0)
    		{
    			--m_last;
    			for (boost::uint32_t i = 0; i < m_capacity; ++i, --m_last)
    				if (m_storage[m_last & mask]) break;
    			++m_last;
    			m_last &= 0xffff;
relevance 0 | ../src/peer_connection.cpp:997 | this should only be peers we're trying to download from
## this should only be peers we're trying to download from #### ../src/peer_connection.cpp:997
    	time_duration peer_connection::download_queue_time(int extra_bytes) const
    	{
    		boost::shared_ptr<torrent> t = m_torrent.lock();
    		TORRENT_ASSERT(t);

    		int rate = 0;

    		// if we haven't received any data recently, the current download rate
    		// is not representative
    		if (time_now() - m_last_piece > seconds(30) && m_download_rate_peak > 0)
    		{
    			rate = m_download_rate_peak;
    		}
    		else if (time_now() - m_last_unchoked < seconds(5)
    			&& m_statistics.total_payload_upload() < 2 * 0x4000)
    		{
    			// if we're have only been unchoked for a short period of time,
    			// we don't know what rate we can get from this peer. Instead of assuming
    			// the lowest possible rate, assume the average.

    			int peers_with_requests = m_ses.num_connections();
    			// avoid division by 0
    			if (peers_with_requests == 0) peers_with_requests = 1;

    			rate = m_ses.m_stat.transfer_rate(stat::download_payload) / peers_with_requests;
    		}
    		else
    		{
    			// current download rate in bytes per seconds
    			rate = m_statistics.transfer_rate(stat::download_payload);
    		}

    		// avoid division by zero
    		if (rate < 50) rate = 50;

    		// average of current rate and peak
    //		rate = (rate + m_download_rate_peak) / 2;

    		return milliseconds((m_outstanding_bytes
    			+ m_queued_time_critical * t->block_size() * 1000) / rate);
    	}

    	void peer_connection::add_stat(size_type downloaded, size_type uploaded)
    	{
    		m_statistics.add_stat(downloaded, uploaded);
    	}

    	bitfield const& peer_connection::get_bitfield() const
    	{
    		return m_have_piece;
    	}
relevance 0 | ../src/peer_connection.cpp:2783 | this might need something more so that once we have the metadata we can construct a full bitfield
## this might need something more so that once we have the metadata we can construct a full bitfield #### ../src/peer_connection.cpp:2783
    #ifdef TORRENT_VERBOSE_LOGGING
    		peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info);
    #endif

    		t->get_policy().set_seed(m_peer_info, true);
    		m_upload_only = true;
    		m_bitfield_received = true;

    		// if we don't have metadata yet
    		// just remember the bitmask
    		// don't update the piecepicker
    		// (since it doesn't exist yet)
    		if (!t->ready_for_connections())
    		{
    			// assume seeds are interesting when we
    			// don't even have the metadata
    			t->get_policy().peer_is_interesting(*this);

    			disconnect_if_redundant();
    			return;
    		}

    		TORRENT_ASSERT(!m_have_piece.empty());
    		m_have_piece.set_all();
    		m_num_pieces = m_have_piece.size();

    		t->peer_has_all(this);

    		// if we're finished, we're not interested
    		if (t->is_upload_only()) send_not_interested();
    		else t->get_policy().peer_is_interesting(*this);

    		disconnect_if_redundant();
    	}

    	// -----------------------------
    	// --------- HAVE NONE ---------
    	// -----------------------------

    	void peer_connection::incoming_have_none()
    	{
    		INVARIANT_CHECK;

    #ifdef TORRENT_VERBOSE_LOGGING
    		peer_log("<== HAVE_NONE");
    #endif

    		boost::shared_ptr<torrent> t = m_torrent.lock();
    		TORRENT_ASSERT(t);
relevance 0 | ../src/peer_connection.cpp:2914 | sort the allowed fast set in priority order
## sort the allowed fast set in priority order #### ../src/peer_connection.cpp:2914
    		// this piece index later
    		m_allowed_fast.push_back(index);

    		// if the peer has the piece and we want
    		// to download it, request it
    		if (int(m_have_piece.size()) > index
    			&& m_have_piece[index]
    			&& t->valid_metadata()
    			&& t->has_picker()
    			&& t->picker().piece_priority(index) > 0)
    		{
    			t->get_policy().peer_is_interesting(*this);
    		}
    	}

    	std::vector<int> const& peer_connection::allowed_fast()
    	{
    		boost::shared_ptr<torrent> t = m_torrent.lock();
    		TORRENT_ASSERT(t);

    		return m_allowed_fast;
    	}

    	bool peer_connection::can_request_time_critical() const
    	{
    		if (has_peer_choked() || !is_interesting()) return false;
    		if ((int)m_download_queue.size() + (int)m_request_queue.size()
    			> m_desired_queue_size * 2) return false;
    		if (on_parole()) return false;
    		if (m_disconnecting) return false;
    		boost::shared_ptr<torrent> t = m_torrent.lock();
    		TORRENT_ASSERT(t);
    		if (t->upload_mode()) return false;

    		// ignore snubbed peers, since they're not likely to return pieces in a
    		// timely manner anyway
    		if (m_snubbed) return false;
    		return true;
    	}

    	bool peer_connection::make_time_critical(piece_block const& block)
    	{
    		std::vector<pending_block>::iterator rit = std::find_if(m_request_queue.begin()
    			, m_request_queue.end(), has_block(block));
    		if (rit == m_request_queue.end()) return false;
    #if TORRENT_USE_ASSERTS
    		boost::shared_ptr<torrent> t = m_torrent.lock();
    		TORRENT_ASSERT(t);
    		TORRENT_ASSERT(t->has_picker());
    		TORRENT_ASSERT(t->picker().is_requested(block));
    #endif
relevance 0 | ../src/peer_connection.cpp:4636 | peers should really be corked/uncorked outside of all completed disk operations
## peers should really be corked/uncorked outside of all completed disk operations #### ../src/peer_connection.cpp:4636
    				// this means we're in seed mode and we haven't yet
    				// verified this piece (r.piece)
    				t->filesystem().async_read_and_hash(r, boost::bind(&peer_connection::on_disk_read_complete
    					, self(), _1, _2, r), cache.second);
    				t->verified(r.piece);
    			}

    			m_reading_bytes += r.length;

    			m_requests.erase(m_requests.begin());
    			sent_a_piece = true;
    		}

    		if (t->share_mode() && sent_a_piece)
    			t->recalc_share_mode();
    	}

    	void peer_connection::on_disk_read_complete(int ret, disk_io_job const& j, peer_request r)
    	{
    		// flush send buffer at the end of this scope
    		cork _c(*this);

    #ifdef TORRENT_STATS
    		++m_ses.m_num_messages[aux::session_impl::on_disk_read_counter];
    #endif
    		TORRENT_ASSERT(m_ses.is_network_thread());

    		m_reading_bytes -= r.length;

    		disk_buffer_holder buffer(m_ses, j.buffer);
    #if TORRENT_DISK_STATS
    		if (j.buffer) m_ses.m_disk_thread.rename_buffer(j.buffer, "received send buffer");
    #endif

    		boost::shared_ptr<torrent> t = m_torrent.lock();
    		if (!t)
    		{
    			disconnect(j.error);
    			return;
    		}

    		if (ret != r.length)
    		{
    			if (ret == -3)
    			{
    #if defined TORRENT_VERBOSE_LOGGING
    				peer_log("==> REJECT_PIECE [ piece: %d s: %d l: %d ]"
    					, r.piece , r.start , r.length);
    #endif
    				write_reject_request(r);
    				if (t->seed_mode()) t->leave_seed_mode(false);
relevance 0 | ../src/policy.cpp:883 | only allow _one_ connection to use this override at a time
## only allow _one_ connection to use this override at a time #### ../src/policy.cpp:883
    				" external: " << external.external_address(m_peers[candidate]->address()) <<
    				" t: " << (session_time - m_peers[candidate]->last_connected) <<
    				" ]\n";
    		}
    #endif

    		if (candidate == -1) return m_peers.end();
    		return m_peers.begin() + candidate;
    	}

    	bool policy::new_connection(peer_connection& c, int session_time)
    	{
    		TORRENT_ASSERT(!c.is_outgoing());

    		INVARIANT_CHECK;

    		// if the connection comes from the tracker,
    		// it's probably just a NAT-check. Ignore the
    		// num connections constraint then.

    		error_code ec;
    		TORRENT_ASSERT(c.remote() == c.get_socket()->remote_endpoint(ec) || ec);
    		TORRENT_ASSERT(!m_torrent->is_paused());

    #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
    		if (c.remote().address() == m_torrent->current_tracker().address())
    		{
    			m_torrent->debug_log("overriding connection limit for tracker NAT-check");
    		}
    #endif

    		iterator iter;
    		peer* i = 0;

    		bool found = false;
    		if (m_torrent->settings().allow_multiple_connections_per_ip)
    		{
    			tcp::endpoint remote = c.remote();
    			std::pair<iterator, iterator> range = find_peers(remote.address());
    			iter = std::find_if(range.first, range.second, match_peer_endpoint(remote));

    			if (iter != range.second)
    			{
    				TORRENT_ASSERT((*iter)->in_use);
    				found = true;
    			}
    		}
    		else
    		{
    			iter = std::lower_bound(
    				m_peers.begin(), m_peers.end()
relevance 0 | ../src/session_impl.cpp:1793 | recalculate all connect candidates for all torrents
## recalculate all connect candidates for all torrents #### ../src/session_impl.cpp:1793
    		// #error closing the udp socket here means that
    		// the uTP connections cannot be closed gracefully
    		m_udp_socket.close();
    		m_external_udp_port = 0;

    		m_undead_peers.clear();

    #ifndef TORRENT_DISABLE_GEO_IP
    		if (m_asnum_db) GeoIP_delete(m_asnum_db);
    		if (m_country_db) GeoIP_delete(m_country_db);
    		m_asnum_db = 0;
    		m_country_db = 0;
    #endif

    		m_disk_thread.abort();
    	}

    	void session_impl::set_port_filter(port_filter const& f)
    	{
    		m_port_filter = f;
    	}

    	void session_impl::set_ip_filter(ip_filter const& f)
    	{
    		INVARIANT_CHECK;

    		m_ip_filter = f;

    		// Close connections whose endpoint is filtered
    		// by the new ip-filter
    		for (torrent_map::iterator i = m_torrents.begin()
    			, end(m_torrents.end()); i != end; ++i)
    			i->second->ip_filter_updated();
    	}

    	ip_filter const& session_impl::get_ip_filter() const
    	{
    		return m_ip_filter;
    	}

    	void session_impl::update_disk_thread_settings()
    	{
    		disk_io_job j;
    		j.buffer = (char*)new session_settings(m_settings);
    		j.action = disk_io_job::update_settings;
    		m_disk_thread.add_job(j);
    	}

    	template <class Socket>
    	void static set_socket_buffer_size(Socket& s, session_settings const& sett, error_code& ec)
    	{
relevance 0 | ../src/session_impl.cpp:3301 | have a separate list for these connections, instead of having to loop through all of them
## have a separate list for these connections, instead of having to loop through all of them #### ../src/session_impl.cpp:3301
    		// --------------------------------------------------------------
    		if (!m_paused) m_auto_manage_time_scaler--;
    		if (m_auto_manage_time_scaler < 0)
    		{
    			m_auto_manage_time_scaler = settings().auto_manage_interval;
    			recalculate_auto_managed_torrents();
    		}

    		// --------------------------------------------------------------
    		// check for incoming connections that might have timed out
    		// --------------------------------------------------------------

    		for (connection_map::iterator i = m_connections.begin();
    			i != m_connections.end();)
    		{
    			peer_connection* p = (*i).get();
    			++i;
    			// ignore connections that already have a torrent, since they
    			// are ticked through the torrents' second_tick
    			if (!p->associated_torrent().expired()) continue;
    			if (m_last_tick - p->connected_time() > seconds(m_settings.handshake_timeout))
    				p->disconnect(errors::timed_out);
    		}

    		// --------------------------------------------------------------
    		// second_tick every torrent
    		// --------------------------------------------------------------

    		int congested_torrents = 0;
    		int uncongested_torrents = 0;

    		// count the number of seeding torrents vs. downloading
    		// torrents we are running
    		int num_seeds = 0;
    		int num_downloads = 0;

    		// count the number of peers of downloading torrents
    		int num_downloads_peers = 0;

    		torrent_map::iterator least_recently_scraped = m_torrents.end();
    		int num_paused_auto_managed = 0;

    		int num_checking = 0;
    		int num_queued = 0;

    #if TORRENT_DEBUG_STREAMING > 0
    		printf("\033[2J\033[0;0H");
    #endif

    		for (torrent_map::iterator i = m_torrents.begin();
    			i != m_torrents.end();)
relevance 0 | ../src/session_impl.cpp:4385 | allow extensions to sort torrents for queuing
## allow extensions to sort torrents for queuing #### ../src/session_impl.cpp:4385
    			if (t->is_auto_managed() && !t->has_error())
    			{
    				TORRENT_ASSERT(t->m_resume_data_loaded || !t->valid_metadata());
    				// this torrent is auto managed, add it to
    				// the list (depending on if it's a seed or not)
    				if (t->is_finished())
    					seeds.push_back(t);
    				else
    					downloaders.push_back(t);
    			}
    			else if (!t->is_paused())
    			{
    				TORRENT_ASSERT(t->m_resume_data_loaded || !t->valid_metadata());
    				--hard_limit;
    			}
    		}

    		bool handled_by_extension = false;

    #ifndef TORRENT_DISABLE_EXTENSIONS
    #endif

    		if (!handled_by_extension)
    		{
    			std::sort(downloaders.begin(), downloaders.end()
    				, boost::bind(&torrent::sequence_number, _1) < boost::bind(&torrent::sequence_number, _2));

    			std::sort(seeds.begin(), seeds.end()
    				, boost::bind(&torrent::seed_rank, _1, boost::ref(m_settings))
    				> boost::bind(&torrent::seed_rank, _2, boost::ref(m_settings)));
    		}

    		if (settings().auto_manage_prefer_seeds)
    		{
    			auto_manage_torrents(seeds, dht_limit, tracker_limit, lsd_limit
    				, hard_limit, num_seeds);
    			auto_manage_torrents(downloaders, dht_limit, tracker_limit, lsd_limit
    				, hard_limit, num_downloaders);
    		}
    		else
    		{
    			auto_manage_torrents(downloaders, dht_limit, tracker_limit, lsd_limit
    				, hard_limit, num_downloaders);
    			auto_manage_torrents(seeds, dht_limit, tracker_limit, lsd_limit
    				, hard_limit, num_seeds);
    		}
    	}

    	void session_impl::recalculate_optimistic_unchoke_slots()
    	{
    		TORRENT_ASSERT(is_network_thread());
relevance 0 | ../src/session_impl.cpp:4550 | use a lower limit than m_settings.connections_limit to allocate the to 10% or so of connection slots for incoming connections
## use a lower limit than m_settings.connections_limit to allocate the to 10% or so of connection slots for incoming connections #### ../src/session_impl.cpp:4550
    		{
    			if (m_boost_connections > max_connections)
    			{
    				m_boost_connections -= max_connections;
    				max_connections = 0;
    			}
    			else
    			{
    				max_connections -= m_boost_connections;
    				m_boost_connections = 0;
    			}
    		}

    		// this logic is here to smooth out the number of new connection
    		// attempts over time, to prevent connecting a large number of
    		// sockets, wait 10 seconds, and then try again
    		int limit = (std::min)(m_settings.connections_limit - num_connections(), free_slots);
    		if (m_settings.smooth_connects && max_connections > (limit+1) / 2)
    			max_connections = (limit+1) / 2;

    		if (!m_torrents.empty()
    			&& free_slots > -m_half_open.limit()
    			&& num_connections() < m_settings.connections_limit
    			&& !m_abort
    			&& m_settings.connection_speed > 0
    			&& max_connections > 0)
    		{
    			// this is the maximum number of connections we will
    			// attempt this tick
    //			int average_peers = 0;
    //			if (num_downloads > 0)
    //				average_peers = num_downloads_peers / num_downloads;

    			if (m_next_connect_torrent == m_torrents.end())
    				m_next_connect_torrent = m_torrents.begin();

    			int steps_since_last_connect = 0;
    			int num_torrents = int(m_torrents.size());
    			for (;;)
    			{
    				torrent& t = *m_next_connect_torrent->second;
    				if (t.want_more_peers())
    				{
    					TORRENT_ASSERT(t.allows_peers());
    					// have a bias to give more connection attempts
    					// to downloading torrents than seed, and even
    					// more to downloading torrents with less than
    					// average number of connections
    					int num_attempts = 1;
    					if (!t.is_finished() && m_num_active_downloading > 0)
    					{
relevance 0 | ../src/session_impl.cpp:4584 | make this bias configurable
## make this bias configurable #### ../src/session_impl.cpp:4584
relevance 0 | ../src/session_impl.cpp:4585 | also take average_peers into account, to create a bias for downloading torrents with < average peers
## also take average_peers into account, to create a bias for downloading torrents with < average peers #### ../src/session_impl.cpp:4585
    //				average_peers = num_downloads_peers / num_downloads;

    			if (m_next_connect_torrent == m_torrents.end())
    				m_next_connect_torrent = m_torrents.begin();

    			int steps_since_last_connect = 0;
    			int num_torrents = int(m_torrents.size());
    			for (;;)
    			{
    				torrent& t = *m_next_connect_torrent->second;
    				if (t.want_more_peers())
    				{
    					TORRENT_ASSERT(t.allows_peers());
    					// have a bias to give more connection attempts
    					// to downloading torrents than seed, and even
    					// more to downloading torrents with less than
    					// average number of connections
    					int num_attempts = 1;
    					if (!t.is_finished() && m_num_active_downloading > 0)
    					{
    						num_attempts += m_num_active_finished / m_num_active_downloading;
    					}
    					while (m_current_connect_attempts < num_attempts)
    					{
    						TORRENT_TRY
    						{
    							++m_current_connect_attempts;
    							if (t.try_connect_peer())
    							{
    								--max_connections;
    								--free_slots;
    								steps_since_last_connect = 0;
    #ifdef TORRENT_STATS
    								++m_connection_attempts;
    #endif
    							}
    						}
    						TORRENT_CATCH(std::bad_alloc&)
    						{
    							// we ran out of memory trying to connect to a peer
    							// lower the global limit to the number of peers
    							// we already have
    							m_settings.connections_limit = num_connections();
    							if (m_settings.connections_limit < 2) m_settings.connections_limit = 2;
    						}
    						if (!t.want_more_peers()) break;
    						if (free_slots <= -m_half_open.limit()) return;
    						if (max_connections == 0) return;
    						if (num_connections() >= m_settings.connections_limit) return;
    					}
    				}
relevance 0 | ../src/session_impl.cpp:4695 | post a message to have this happen immediately instead of waiting for the next tick
## post a message to have this happen immediately instead of waiting for the next tick #### ../src/session_impl.cpp:4695
    					{
    						// we've unchoked this peer, and it hasn't reciprocated
    						// we may want to increase our estimated reciprocation rate
    						p->increase_est_reciprocation_rate();
    					}
    				}
    			}

    			if (!p->is_peer_interested()
    				|| p->is_disconnecting()
    				|| p->is_connecting())
    			{
    				// this peer is not unchokable. So, if it's unchoked
    				// already, make sure to choke it.
    				if (p->is_choked()) continue;
    				if (pi && pi->optimistically_unchoked)
    				{
    					pi->optimistically_unchoked = false;
    					// force a new optimistic unchoke
    					m_optimistic_unchoke_time_scaler = 0;
    				}
    				t->choke_peer(*p);
    				continue;
    			}
    			peers.push_back(p.get());
    		}

    		if (m_settings.choking_algorithm == session_settings::rate_based_choker)
    		{
    			m_allowed_upload_slots = 0;
    			std::sort(peers.begin(), peers.end()
    				, boost::bind(&peer_connection::upload_rate_compare, _1, _2));

    #ifdef TORRENT_DEBUG
    			for (std::vector<peer_connection*>::const_iterator i = peers.begin()
    				, end(peers.end()), prev(peers.end()); i != end; ++i)
    			{
    				if (prev != end)
    				{
    					boost::shared_ptr<torrent> t1 = (*prev)->associated_torrent().lock();
    					TORRENT_ASSERT(t1);
    					boost::shared_ptr<torrent> t2 = (*i)->associated_torrent().lock();
    					TORRENT_ASSERT(t2);
    					TORRENT_ASSERT((*prev)->uploaded_in_last_round() * 1000
    						* (1 + t1->priority()) / total_milliseconds(unchoke_interval)
    						>= (*i)->uploaded_in_last_round() * 1000
    						* (1 + t2->priority()) / total_milliseconds(unchoke_interval));
    				}
    				prev = i;
    			}
    #endif
relevance 0 | ../src/session_impl.cpp:4729 | make configurable
## make configurable #### ../src/session_impl.cpp:4729
    #ifdef TORRENT_DEBUG
    			for (std::vector<peer_connection*>::const_iterator i = peers.begin()
    				, end(peers.end()), prev(peers.end()); i != end; ++i)
    			{
    				if (prev != end)
    				{
    					boost::shared_ptr<torrent> t1 = (*prev)->associated_torrent().lock();
    					TORRENT_ASSERT(t1);
    					boost::shared_ptr<torrent> t2 = (*i)->associated_torrent().lock();
    					TORRENT_ASSERT(t2);
    					TORRENT_ASSERT((*prev)->uploaded_in_last_round() * 1000
    						* (1 + t1->priority()) / total_milliseconds(unchoke_interval)
    						>= (*i)->uploaded_in_last_round() * 1000
    						* (1 + t2->priority()) / total_milliseconds(unchoke_interval));
    				}
    				prev = i;
    			}
    #endif

    			int rate_threshold = 1024;

    			for (std::vector<peer_connection*>::const_iterator i = peers.begin()
    				, end(peers.end()); i != end; ++i)
    			{
    				peer_connection const& p = **i;
    				int rate = int(p.uploaded_in_last_round()
    					* 1000 / total_milliseconds(unchoke_interval));

    				if (rate < rate_threshold) break;

    				++m_allowed_upload_slots;
relevance 0 | ../src/session_impl.cpp:4743 | make configurable
## make configurable #### ../src/session_impl.cpp:4743
    						>= (*i)->uploaded_in_last_round() * 1000
    						* (1 + t2->priority()) / total_milliseconds(unchoke_interval));
    				}
    				prev = i;
    			}
    #endif

    			int rate_threshold = 1024;

    			for (std::vector<peer_connection*>::const_iterator i = peers.begin()
    				, end(peers.end()); i != end; ++i)
    			{
    				peer_connection const& p = **i;
    				int rate = int(p.uploaded_in_last_round()
    					* 1000 / total_milliseconds(unchoke_interval));

    				if (rate < rate_threshold) break;

    				++m_allowed_upload_slots;

    				rate_threshold += 1024;
    			}
    			// allow one optimistic unchoke
    			++m_allowed_upload_slots;
    		}

    		if (m_settings.choking_algorithm == session_settings::bittyrant_choker)
    		{
    			// if we're using the bittyrant choker, sort peers by their return
    			// on investment. i.e. download rate / upload rate
    			std::sort(peers.begin(), peers.end()
    				, boost::bind(&peer_connection::bittyrant_unchoke_compare, _1, _2));
    		}
    		else
    		{
    			// sorts the peers that are eligible for unchoke by download rate and secondary
    			// by total upload. The reason for this is, if all torrents are being seeded,
    			// the download rate will be 0, and the peers we have sent the least to should
    			// be unchoked
    			std::sort(peers.begin(), peers.end()
    				, boost::bind(&peer_connection::unchoke_compare, _1, _2));
    		}

    		// auto unchoke
    		int upload_limit = m_bandwidth_channel[peer_connection::upload_channel]->throttle();
    		if (m_settings.choking_algorithm == session_settings::auto_expand_choker
    			&& upload_limit > 0)
    		{
    			// if our current upload rate is less than 90% of our
    			// limit AND most torrents are not "congested", i.e.
    			// they are not holding back because of a per-torrent
relevance 0 | ../src/storage.cpp:322 | if the read fails, set error and exit immediately
## if the read fails, set error and exit immediately #### ../src/storage.cpp:322
    			if (m_storage->disk_pool()) block_size = m_storage->disk_pool()->block_size();
    			int size = slot_size;
    			int num_blocks = (size + block_size - 1) / block_size;

    			// when we optimize for speed we allocate all the buffers we
    			// need for the rest of the piece, and read it all in one call
    			// and then hash it. When optimizing for memory usage, we read
    			// one block at a time and hash it. This ends up only using a
    			// single buffer
    			if (m_storage->settings().optimize_hashing_for_speed)
    			{
    				file::iovec_t* bufs = TORRENT_ALLOCA(file::iovec_t, num_blocks);
    				for (int i = 0; i < num_blocks; ++i)
    				{
    					bufs[i].iov_base = m_storage->disk_pool()->allocate_buffer("hash temp");
    					bufs[i].iov_len = (std::min)(block_size, size);
    					size -= bufs[i].iov_len;
    				}
    				// deliberately pass in 0 as flags, to disable random_access
    				num_read = m_storage->readv(bufs, slot, ph.offset, num_blocks, 0);

    				for (int i = 0; i < num_blocks; ++i)
    				{
    					if (small_hash && small_piece_size <= block_size)
    					{
    						ph.h.update((char const*)bufs[i].iov_base, small_piece_size);
    						*small_hash = hasher(ph.h).final();
    						small_hash = 0; // avoid this case again
    						if (int(bufs[i].iov_len) > small_piece_size)
    							ph.h.update((char const*)bufs[i].iov_base + small_piece_size
    								, bufs[i].iov_len - small_piece_size);
    					}
    					else
    					{
    						ph.h.update((char const*)bufs[i].iov_base, bufs[i].iov_len);
    						small_piece_size -= bufs[i].iov_len;
    					}
    					ph.offset += bufs[i].iov_len;
    					m_storage->disk_pool()->free_buffer((char*)bufs[i].iov_base);
    				}
    			}
    			else
    			{
    				file::iovec_t buf;
    				disk_buffer_holder holder(*m_storage->disk_pool()
    					, m_storage->disk_pool()->allocate_buffer("hash temp"));
    				buf.iov_base = holder.get();
    				for (int i = 0; i < num_blocks; ++i)
    				{
    					buf.iov_len = (std::min)(block_size, size);
    					// deliberately pass in 0 as flags, to disable random_access
relevance 0 | ../src/storage.cpp:356 | if the read fails, set error and exit immediately
## if the read fails, set error and exit immediately #### ../src/storage.cpp:356
    					{
    						ph.h.update((char const*)bufs[i].iov_base, bufs[i].iov_len);
    						small_piece_size -= bufs[i].iov_len;
    					}
    					ph.offset += bufs[i].iov_len;
    					m_storage->disk_pool()->free_buffer((char*)bufs[i].iov_base);
    				}
    			}
    			else
    			{
    				file::iovec_t buf;
    				disk_buffer_holder holder(*m_storage->disk_pool()
    					, m_storage->disk_pool()->allocate_buffer("hash temp"));
    				buf.iov_base = holder.get();
    				for (int i = 0; i < num_blocks; ++i)
    				{
    					buf.iov_len = (std::min)(block_size, size);
    					// deliberately pass in 0 as flags, to disable random_access
    					int ret = m_storage->readv(&buf, slot, ph.offset, 1, 0);
    					if (ret > 0) num_read += ret;

    					if (small_hash && small_piece_size <= block_size)
    					{
    						if (small_piece_size > 0) ph.h.update((char const*)buf.iov_base, small_piece_size);
    						*small_hash = hasher(ph.h).final();
    						small_hash = 0; // avoid this case again
    						if (int(buf.iov_len) > small_piece_size)
    							ph.h.update((char const*)buf.iov_base + small_piece_size
    								, buf.iov_len - small_piece_size);
    					}
    					else
    					{
    						ph.h.update((char const*)buf.iov_base, buf.iov_len);
    						small_piece_size -= buf.iov_len;
    					}

    					ph.offset += buf.iov_len;
    					size -= buf.iov_len;
    				}
    			}
    			if (error()) return 0;
    		}
    		return num_read;
    	}

    	default_storage::default_storage(file_storage const& fs, file_storage const* mapped, std::string const& path
    		, file_pool& fp, std::vector<boost::uint8_t> const& file_prio)
    		: m_files(fs)
    		, m_file_priority(file_prio)
    		, m_pool(fp)
    		, m_page_size(page_size())
relevance 0 | ../src/storage.cpp:645 | make this more generic to not just work if files have been renamed, but also if they have been merged into a single file for instance maybe use the same format as .torrent files and reuse some code from torrent_info
## make this more generic to not just work if files have been renamed, but also if they have been merged into a single file for instance maybe use the same format as .torrent files and reuse some code from torrent_info #### ../src/storage.cpp:645
    		for (;;)
    		{
    			if (file_offset < files().file_size(file_index))
    				break;

    			file_offset -= files().file_size(file_index);
    			++file_index;
    			TORRENT_ASSERT(file_index != files().num_files());
    		}

    		error_code ec;
    		boost::intrusive_ptr<file> file_handle = open_file(file_index, file::read_only, ec);
    		if (!file_handle || ec) return slot;

    		size_type data_start = file_handle->sparse_end(file_offset);
    		return int((data_start + files().piece_length() - 1) / files().piece_length());
    	}

    	bool default_storage::verify_resume_data(lazy_entry const& rd, error_code& error)
    	{
    		lazy_entry const* mapped_files = rd.dict_find_list("mapped_files");
    		if (mapped_files && mapped_files->list_size() == m_files.num_files())
    		{
    			m_mapped_files.reset(new file_storage(m_files));
    			for (int i = 0; i < m_files.num_files(); ++i)
    			{
    				std::string new_filename = mapped_files->list_string_value_at(i);
    				if (new_filename.empty()) continue;
    				m_mapped_files->rename_file(i, new_filename);
    			}
    		}

    		lazy_entry const* file_priority = rd.dict_find_list("file_priority");
    		if (file_priority && file_priority->list_size()
    			== files().num_files())
    		{
    			m_file_priority.resize(file_priority->list_size());
    			for (int i = 0; i < file_priority->list_size(); ++i)
    				m_file_priority[i] = boost::uint8_t(file_priority->list_int_value_at(i, 1));
    		}

    		std::vector<std::pair<size_type, std::time_t> > file_sizes;
    		lazy_entry const* file_sizes_ent = rd.dict_find_list("file sizes");
    		if (file_sizes_ent == 0)
    		{
    			error = errors::missing_file_sizes;
    			return false;
    		}

    		for (int i = 0; i < file_sizes_ent->list_size(); ++i)
    		{
relevance 0 | ../src/storage.cpp:1280 | what if file_base is used to merge several virtual files into a single physical file? We should probably disable this if file_base is used. This is not a widely used feature though
## what if file_base is used to merge several virtual files into a single physical file? We should probably disable this if file_base is used. This is not a widely used feature though #### ../src/storage.cpp:1280
    			int bytes_transferred = 0;
    			// if the file is opened in no_buffer mode, and the
    			// read is unaligned, we need to fall back on a slow
    			// special read that reads aligned buffers and copies
    			// it into the one supplied
    			size_type adjusted_offset = files().file_base(file_index) + file_offset;
    			if ((file_handle->open_mode() & file::no_buffer)
    				&& ((adjusted_offset & (file_handle->pos_alignment()-1)) != 0
    				|| (uintptr_t(tmp_bufs->iov_base) & (file_handle->buf_alignment()-1)) != 0))
    			{
    				bytes_transferred = (int)(this->*op.unaligned_op)(file_handle, adjusted_offset
    					, tmp_bufs, num_tmp_bufs, ec);
    				if ((op.mode & file::rw_mask) != file::read_only
    					&& adjusted_offset + bytes_transferred >= files().file_size(file_index)
    					&& (file_handle->pos_alignment() > 0 || file_handle->size_alignment() > 0))
    				{
    					// we were writing, and we just wrote the last block of the file
    					// we likely wrote a bit too much, since we're restricted to
    					// a specific alignment for writes. Make sure to truncate the size

    					file_handle->set_size(files().file_size(file_index), ec);
    				}
    			}
    			else
    			{
    				bytes_transferred = (int)((*file_handle).*op.regular_op)(adjusted_offset
    					, tmp_bufs, num_tmp_bufs, ec);
    				TORRENT_ASSERT(bytes_transferred <= bufs_size(tmp_bufs, num_tmp_bufs));
    			}
    			file_offset = 0;

    			if (ec)
    			{
    				set_error(files().file_path(file_index, m_save_path), ec);
    				return -1;
    			}

    			if (file_bytes_left != bytes_transferred)
    				return bytes_transferred;

    			advance_bufs(current_buf, bytes_transferred);
    			TORRENT_ASSERT(count_bufs(current_buf, bytes_left - file_bytes_left) <= num_bufs);
    		}
    		return size;
    	}

    	// these functions are inefficient, but should be fairly uncommon. The read
    	// case happens if unaligned files are opened in no_buffer mode or if clients
    	// makes unaligned requests (and the disk cache is disabled or fully utilized
    	// for write cache).
relevance 0 | ../src/torrent.cpp:1279 | is verify_peer_cert called once per certificate in the chain, and this function just tells us which depth we're at right now? If so, the comment makes sense. any certificate that isn't the leaf (i.e. the one presented by the peer) should be accepted automatically, given preverified is true. The leaf certificate need to be verified to make sure its DN matches the info-hash
## is verify_peer_cert called once per certificate in the chain, and this function just tells us which depth we're at right now? If so, the comment makes sense. any certificate that isn't the leaf (i.e. the one presented by the peer) should be accepted automatically, given preverified is true. The leaf certificate need to be verified to make sure its DN matches the info-hash #### ../src/torrent.cpp:1279
    			if (pp) p->add_extension(pp);
    		}

    		// if files are checked for this torrent, call the extension
    		// to let it initialize itself
    		if (m_connections_initialized)
    			tp->on_files_checked();
    	}

    #endif

    #ifdef TORRENT_USE_OPENSSL

    #if BOOST_VERSION >= 104700
    	bool torrent::verify_peer_cert(bool preverified, boost::asio::ssl::verify_context& ctx)
    	{
    		// if the cert wasn't signed by the correct CA, fail the verification
    		if (!preverified) return false;

    		// we're only interested in checking the certificate at the end of the chain.
    		int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
    		if (depth > 0) return true;

    		X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());

    		// Go through the alternate names in the certificate looking for matching DNS entries
    		GENERAL_NAMES* gens = static_cast<GENERAL_NAMES*>(
    			X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0));

    #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
    		std::string names;
    		bool match = false;
    #endif
    		for (int i = 0; i < sk_GENERAL_NAME_num(gens); ++i)
    		{
    			GENERAL_NAME* gen = sk_GENERAL_NAME_value(gens, i);
    			if (gen->type != GEN_DNS) continue;
    			ASN1_IA5STRING* domain = gen->d.dNSName;
    			if (domain->type != V_ASN1_IA5STRING || !domain->data || !domain->length) continue;
    			const char* torrent_name = reinterpret_cast<const char*>(domain->data);
    			std::size_t name_length = domain->length;

    #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
    			if (i > 1) names += " | n: ";
    			names.append(torrent_name, name_length);
    #endif
    			if (strncmp(torrent_name, "*", name_length) == 0
    				|| strncmp(torrent_name, m_torrent_file->name().c_str(), name_length) == 0)
    			{
    #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
    				match = true;
relevance 0 | ../src/torrent.cpp:5391 | make this more generic to not just work if files have been renamed, but also if they have been merged into a single file for instance maybe use the same format as .torrent files and reuse some code from torrent_info The mapped_files needs to be read both in the network thread and in the disk thread, since they both have their own mapped files structures which are kept in sync
## make this more generic to not just work if files have been renamed, but also if they have been merged into a single file for instance maybe use the same format as .torrent files and reuse some code from torrent_info The mapped_files needs to be read both in the network thread and in the disk thread, since they both have their own mapped files structures which are kept in sync #### ../src/torrent.cpp:5391
    				m_save_path = p;
    #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
    				debug_log("loaded resume data: save-path: %s", m_save_path.c_str());
    #endif
    			}
    		}

    		m_url = rd.dict_find_string_value("url");
    		m_uuid = rd.dict_find_string_value("uuid");
    		m_source_feed_url = rd.dict_find_string_value("feed");

    		if (!m_uuid.empty() || !m_url.empty())
    		{
    			boost::shared_ptr<torrent> me(shared_from_this());

    			// insert this torrent in the uuid index
    			m_ses.m_uuids.insert(std::make_pair(m_uuid.empty()
    				? m_url : m_uuid, me));
    		}

    		lazy_entry const* mapped_files = rd.dict_find_list("mapped_files");
    		if (mapped_files && mapped_files->list_size() == m_torrent_file->num_files())
    		{
    			for (int i = 0; i < m_torrent_file->num_files(); ++i)
    			{
    				std::string new_filename = mapped_files->list_string_value_at(i);
    				if (new_filename.empty()) continue;
    				m_torrent_file->rename_file(i, new_filename);
    			}
    		}

    		m_added_time = rd.dict_find_int_value("added_time", m_added_time);
    		m_completed_time = rd.dict_find_int_value("completed_time", m_completed_time);
    		if (m_completed_time != 0 && m_completed_time < m_added_time)
    			m_completed_time = m_added_time;

    		if (!m_override_resume_data)
    		{
    			lazy_entry const* file_priority = rd.dict_find_list("file_priority");
    			if (file_priority)
    			{
    				const int num_files = (std::min)(file_priority->list_size()
    					, m_torrent_file->num_files());
    				for (int i = 0; i < num_files; ++i)
    				{
    					m_file_priority[i] = file_priority->list_int_value_at(i, 1);
    					// this is suspicious, leave seed mode
    					if (m_file_priority[i] == 0) m_seed_mode = false;
    				}
    				update_piece_priorities();
    			}
relevance 0 | ../src/torrent.cpp:5507 | if this is a merkle torrent and we can't restore the tree, we need to wipe all the bits in the have array, but not necessarily we might want to do a full check to see if we have all the pieces. This is low priority since almost no one uses merkle torrents
## if this is a merkle torrent and we can't restore the tree, we need to wipe all the bits in the have array, but not necessarily we might want to do a full check to see if we have all the pieces. This is low priority since almost no one uses merkle torrents #### ../src/torrent.cpp:5507
    				add_web_seed(url, web_seed_entry::http_seed);
    			}
    		}

    		if (m_torrent_file->is_merkle_torrent())
    		{
    			lazy_entry const* mt = rd.dict_find_string("merkle tree");
    			if (mt)
    			{
    				std::vector<sha1_hash> tree;
    				tree.resize(m_torrent_file->merkle_tree().size());
    				std::memcpy(&tree[0], mt->string_ptr()
    					, (std::min)(mt->string_length(), int(tree.size()) * 20));
    				if (mt->string_length() < int(tree.size()) * 20)
    					std::memset(&tree[0] + mt->string_length() / 20, 0
    						, tree.size() - mt->string_length() / 20);
    				m_torrent_file->set_merkle_tree(tree);
    			}
    			else
    			{
    				TORRENT_ASSERT(false);
    			}
    		}

    		if (m_seed_mode)
    		{
    			// some sanity checking. Maybe we shouldn't be in seed mode anymore
    			lazy_entry const* pieces = rd.dict_find("pieces");
    			if (pieces && pieces->type() == lazy_entry::string_t
    				&& int(pieces->string_length()) == m_torrent_file->num_pieces())
    			{
    				char const* pieces_str = pieces->string_ptr();
    				for (int i = 0, end(pieces->string_length()); i < end; ++i)
    				{
    					// being in seed mode and missing a piece is not compatible.
    					// Leave seed mode if that happens
    					if ((pieces_str[i] & 1)) continue;
    					m_seed_mode = false;
    					break;
    				}
    			}

    			lazy_entry const* piece_priority = m_resume_entry.dict_find_string("piece_priority");
    			if (piece_priority && piece_priority->string_length()
    				== m_torrent_file->num_pieces())
    			{
    				char const* p = piece_priority->string_ptr();
    				for (int i = 0; i < piece_priority->string_length(); ++i)
    				{
    					if (p[i] > 0) continue;
    					m_seed_mode = false;
relevance 0 | ../src/torrent.cpp:5731 | make this more generic to not just work if files have been renamed, but also if they have been merged into a single file for instance. using file_base
## make this more generic to not just work if files have been renamed, but also if they have been merged into a single file for instance. using file_base #### ../src/torrent.cpp:5731
    		entry::string_type& pieces = ret["pieces"].string();
    		pieces.resize(m_torrent_file->num_pieces());
    		if (is_seed())
    		{
    			std::memset(&pieces[0], 1, pieces.size());
    		}
    		else if (has_picker())
    		{
    			for (int i = 0, end(pieces.size()); i < end; ++i)
    				pieces[i] = m_picker->have_piece(i) ? 1 : 0;
    		}

    		if (m_seed_mode)
    		{
    			TORRENT_ASSERT(m_verified.size() == pieces.size());
    			for (int i = 0, end(pieces.size()); i < end; ++i)
    				pieces[i] |= m_verified[i] ? 2 : 0;
    		}

    		// write renamed files
    		if (&m_torrent_file->files() != &m_torrent_file->orig_files()
    			&& m_torrent_file->files().num_files() == m_torrent_file->orig_files().num_files())
    		{
    			entry::list_type& fl = ret["mapped_files"].list();
    			file_storage const& fs = m_torrent_file->files();
    			for (int i = 0; i < fs.num_files(); ++i)
    			{
    				fl.push_back(fs.file_path(i));
    			}
    		}

    		// write local peers

    		std::back_insert_iterator<entry::string_type> peers(ret["peers"].string());
    		std::back_insert_iterator<entry::string_type> banned_peers(ret["banned_peers"].string());
    #if TORRENT_USE_IPV6
    		std::back_insert_iterator<entry::string_type> peers6(ret["peers6"].string());
    		std::back_insert_iterator<entry::string_type> banned_peers6(ret["banned_peers6"].string());
    #endif

    		// failcount is a 5 bit value
    		int max_failcount = (std::min)(settings().max_failcount, 31);

    		int num_saved_peers = 0;

    		for (policy::const_iterator i = m_policy.begin_peer()
    			, end(m_policy.end_peer()); i != end; ++i)
    		{
    			error_code ec;
    			policy::peer const* p = *i;
    			address addr = p->address();
relevance 0 | ../src/torrent.cpp:8441 | go through the pieces we have and count the total number of downloaders we have. Only count peers that are interested in us since some peers might not send have messages for pieces we have it num_interested == 0, we need to pick a new piece
## go through the pieces we have and count the total number of downloaders we have. Only count peers that are interested in us since some peers might not send have messages for pieces we have it num_interested == 0, we need to pick a new piece #### ../src/torrent.cpp:8441
    			}

    			rarest_pieces.clear();
    			rarest_rarity = pp.peer_count;
    			rarest_pieces.push_back(i);
    		}

    		if (prio_updated)
    			m_policy.recalculate_connect_candidates();

    		// now, rarest_pieces is a list of all pieces that are the rarest ones.
    		// and rarest_rarity is the number of peers that have the rarest pieces

    		// if there's only a single peer that doesn't have the rarest piece
    		// it's impossible for us to download one piece and upload it
    		// twice. i.e. we cannot get a positive share ratio
    		if (num_peers - rarest_rarity < settings().share_mode_target) return;

    		// we might be able to do better than a share ratio of 2 if there are
    		// enough downloaders of the pieces we already have.

    		// now, pick one of the rarest pieces to download
    		int pick = random() % rarest_pieces.size();
    		bool was_finished = is_finished();
    		m_picker->set_piece_priority(rarest_pieces[pick], 1);
    		update_peer_interest(was_finished);

    		m_policy.recalculate_connect_candidates();
    	}

    	void torrent::refresh_explicit_cache(int cache_size)
    	{
    		TORRENT_ASSERT(m_ses.is_network_thread());
    		if (!ready_for_connections()) return;

    		if (m_abort) return;
    		TORRENT_ASSERT(m_storage);

    		// rotate the cached pieces

    		// add blocks_per_piece / 2 in order to round to closest whole piece
    		int blocks_per_piece = m_torrent_file->piece_length() / block_size();
    		int num_cache_pieces = (cache_size + blocks_per_piece / 2) / blocks_per_piece;
    		if (num_cache_pieces > m_torrent_file->num_pieces())
    			num_cache_pieces = m_torrent_file->num_pieces();

    		std::vector<int> avail_vec;
    		if (has_picker())
    		{
    			m_picker->get_availability(avail_vec);
    		}
relevance 0 | ../src/torrent.cpp:9090 | instead of resorting the whole list, insert the peers directly into the right place
## instead of resorting the whole list, insert the peers directly into the right place #### ../src/torrent.cpp:9090
    				printf("timed out [average-piece-time: %d ms ]\n"
    					, m_average_piece_time);
    #endif
    			}

    			// pick all blocks for this piece. the peers list is kept up to date
    			// and sorted. when we issue a request to a peer, its download queue
    			// time will increase and it may need to be bumped in the peers list,
    			// since it's ordered by download queue time
    			pick_time_critical_block(peers, ignore_peers
    				, peers_with_requests
    				, pi, &*i, m_picker.get()
    				, blocks_in_piece, timed_out);

    			// put back the peers we ignored into the peer list for the next piece
    			if (!ignore_peers.empty())
    			{
    				peers.insert(peers.begin(), ignore_peers.begin(), ignore_peers.end());
    				ignore_peers.clear();

    				std::sort(peers.begin(), peers.end()
    					, boost::bind(&peer_connection::download_queue_time, _1, 16*1024)
    					< boost::bind(&peer_connection::download_queue_time, _2, 16*1024));
    			}

    			// if this peer's download time exceeds 2 seconds, we're done.
    			// We don't want to build unreasonably long request queues
    			if (!peers.empty() && peers[0]->download_queue_time() > milliseconds(2000))
    				break;
    		}

    		// commit all the time critical requests
    		for (std::set<peer_connection*>::iterator i = peers_with_requests.begin()
    			, end(peers_with_requests.end()); i != end; ++i)
    		{
    			(*i)->send_block_requests();
    		}
    	}

    	std::set<std::string> torrent::web_seeds(web_seed_entry::type_t type) const
    	{
    		TORRENT_ASSERT(m_ses.is_network_thread());
    		std::set<std::string> ret;
    		for (std::list<web_seed_entry>::const_iterator i = m_web_seeds.begin()
    			, end(m_web_seeds.end()); i != end; ++i)
    		{
    			if (i->peer_info.banned) continue;
    			if (i->type != type) continue;
    			ret.insert(i->url);
    		}
    		return ret;
relevance 0 | ../src/udp_socket.cpp:294 | it would be nice to detect this on posix systems also
## it would be nice to detect this on posix systems also #### ../src/udp_socket.cpp:294
    		--m_v6_outstanding;
    	}
    	else
    #endif
    	{
    		TORRENT_ASSERT(m_v4_outstanding > 0);
    		--m_v4_outstanding;
    	}

    	if (ec == asio::error::operation_aborted) return;
    	if (m_abort) return;

    	CHECK_MAGIC;

    	for (;;)
    	{
    		error_code ec;
    		udp::endpoint ep;
    		size_t bytes_transferred = s->receive_from(asio::buffer(m_buf, m_buf_size), ep, 0, ec);

    #ifdef TORRENT_WINDOWS
    		if ((ec == error_code(ERROR_MORE_DATA, get_system_category())
    			|| ec == error_code(WSAEMSGSIZE, get_system_category()))
    			&& m_buf_size < 65536)
    		{
    			// if this function fails to allocate memory, m_buf_size
    			// is set to 0. In that case, don't issue the async_read().
    			set_buf_size(m_buf_size * 2);
    			if (m_buf_size == 0) return;
    			continue;
    		}
    #endif

    		if (ec == asio::error::would_block || ec == asio::error::try_again) break;
    		on_read_impl(s, ep, ec, bytes_transferred);
    	}
    	call_drained_handler();
    	setup_read(s);
    }

    void udp_socket::call_handler(error_code const& ec, udp::endpoint const& ep, char const* buf, int size)
    {
    	m_observers_locked = true;
    	for (std::vector<udp_socket_observer*>::iterator i = m_observers.begin();
    		i != m_observers.end();)
    	{
    		bool ret = false;
    		TORRENT_TRY {
    			ret = (*i)->incoming_packet(ec, ep, buf, size);
    		} TORRENT_CATCH (std::exception&) {}
    		if (*i == NULL) i = m_observers.erase(i);
relevance 0 | ../src/udp_tracker_connection.cpp:553 | it would be more efficient to not use a string here. however, the problem is that some trackers will respond with actual strings. For example i2p trackers
## it would be more efficient to not use a string here. however, the problem is that some trackers will respond with actual strings. For example i2p trackers #### ../src/udp_tracker_connection.cpp:553
    		}

    		boost::shared_ptr<request_callback> cb = requester();
    #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
    		if (cb)
    		{
    			boost::shared_ptr<request_callback> cb = requester();
    			cb->debug_log("<== UDP_TRACKER_RESPONSE [ url: %s ]", tracker_req().url.c_str());
    		}
    #endif

    		if (!cb)
    		{
    			close();
    			return true;
    		}

    		std::vector<peer_entry> peer_list;
    		for (int i = 0; i < num_peers; ++i)
    		{
    			peer_entry e;
    			char ip_string[100];
    			unsigned int a = detail::read_uint8(buf);
    			unsigned int b = detail::read_uint8(buf);
    			unsigned int c = detail::read_uint8(buf);
    			unsigned int d = detail::read_uint8(buf);
    			snprintf(ip_string, 100, "%u.%u.%u.%u", a, b, c, d);
    			e.ip = ip_string;
    			e.port = detail::read_uint16(buf);
    			e.pid.clear();
    			peer_list.push_back(e);
    		}

    		std::list<address> ip_list;
    		for (std::list<tcp::endpoint>::const_iterator i = m_endpoints.begin()
    			, end(m_endpoints.end()); i != end; ++i)
    		{
    			ip_list.push_back(i->address());
    		}

    		cb->tracker_response(tracker_req(), m_target.address(), ip_list
    			, peer_list, interval, min_interval, complete, incomplete, 0, address(), "" /*trackerid*/);

    		close();
    		return true;
    	}

    	bool udp_tracker_connection::on_scrape_response(char const* buf, int size)
    	{
    		restart_read_timeout();
    		int action = detail::read_int32(buf);
relevance 0 | ../src/upnp.cpp:72 | listen_interface is not used. It's meant to bind the broadcast socket
## listen_interface is not used. It's meant to bind the broadcast socket #### ../src/upnp.cpp:72
    #include <asio/ip/multicast.hpp>
    #else
    #include <boost/asio/ip/host_name.hpp>
    #include <boost/asio/ip/multicast.hpp>
    #endif
    #include <cstdlib>

    namespace libtorrent {

    namespace upnp_errors
    {
    	boost::system::error_code make_error_code(error_code_enum e)
    	{
    		return error_code(e, get_upnp_category());
    	}

    } // upnp_errors namespace

    static error_code ec;

    upnp::upnp(io_service& ios, connection_queue& cc
    	, address const& listen_interface, std::string const& user_agent
    	, portmap_callback_t const& cb, log_callback_t const& lcb
    	, bool ignore_nonrouters, void* state)
    	: m_user_agent(user_agent)
    	, m_callback(cb)
    	, m_log_callback(lcb)
    	, m_retry_count(0)
    	, m_io_service(ios)
    	, m_socket(udp::endpoint(address_v4::from_string("239.255.255.250", ec), 1900)
    		, boost::bind(&upnp::on_reply, self(), _1, _2, _3))
    	, m_broadcast_timer(ios)
    	, m_refresh_timer(ios)
    	, m_map_timer(ios)
    	, m_disabled(false)
    	, m_closing(false)
    	, m_ignore_non_routers(ignore_nonrouters)
    	, m_cc(cc)
    {
    	TORRENT_ASSERT(cb);

    	error_code ec;
    	m_socket.open(ios, ec);

    	if (state)
    	{
    		upnp_state_t* s = (upnp_state_t*)state;
    		m_devices.swap(s->devices);
    		m_mappings.swap(s->mappings);
    		delete s;
    	}
relevance 0 | ../src/utp_stream.cpp:1621 | this loop may not be very efficient
## this loop may not be very efficient #### ../src/utp_stream.cpp:1621
    };

    // sends a packet, pulls data from the write buffer (if there's any)
    // if ack is true, we need to send a packet regardless of if there's
    // any data. Returns true if we could send more data (i.e. call
    // send_pkt() again)
    // returns true if there is more space for payload in our
    // congestion window, false if there is no more space.
    bool utp_socket_impl::send_pkt(int flags)
    {
    #ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
    	INVARIANT_CHECK;
    #endif

    	bool force = (flags & pkt_ack) || (flags & pkt_fin);

    //	TORRENT_ASSERT(m_state != UTP_STATE_FIN_SENT || (flags & pkt_ack));

    	// first see if we need to resend any packets

    	for (int i = (m_acked_seq_nr + 1) & ACK_MASK; i != m_seq_nr; i = (i + 1) & ACK_MASK)
    	{
    		packet* p = (packet*)m_outbuf.at(i);
    		if (!p) continue;
    		if (!p->need_resend) continue;
    		if (!resend_packet(p))
    		{
    			// we couldn't resend the packet. It probably doesn't
    			// fit in our cwnd. If force is set, we need to continue
    			// to send our packet anyway, if we don't have force set,
    			// we might as well return
    			if (!force) return false;
    			// resend_packet might have failed
    			if (m_state == UTP_STATE_ERROR_WAIT || m_state == UTP_STATE_DELETE) return false;
    			break;
    		}

    		// don't fast-resend this packet
    		if (m_fast_resend_seq_nr == i)
    			m_fast_resend_seq_nr = (m_fast_resend_seq_nr + 1) & ACK_MASK;
    	}

    	int sack = 0;
    	if (m_inbuf.size())
    	{
    		// the SACK bitfield should ideally fit all
    		// the pieces we have successfully received
    		sack = (m_inbuf.span() + 7) / 8;
    		if (sack > 32) sack = 32;
    	}
relevance 0 | ../src/kademlia/dht_tracker.cpp:438 | ideally this function would be called when the put completes
## ideally this function would be called when the put completes #### ../src/kademlia/dht_tracker.cpp:438
    		// since it controls whether we re-put the content
    		TORRENT_ASSERT(!it.is_mutable());
    		f(it);
    		return false;
    	}

    	bool get_mutable_item_callback(item& it, boost::function<void(item const&)> f)
    	{
    		// the reason to wrap here is to control the return value
    		// since it controls whether we re-put the content
    		TORRENT_ASSERT(it.is_mutable());
    		f(it);
    		return false;
    	}

    	bool put_immutable_item_callback(item& it, boost::function<void()> f
    		, entry data)
    	{
    		TORRENT_ASSERT(!it.is_mutable());
    		it.assign(data);
    		f();
    		return true;
    	}

    	bool put_mutable_item_callback(item& it, boost::function<void(item&)> cb)
    	{
    		cb(it);
    		return true;
    	}

    	void dht_tracker::get_item(sha1_hash const& target
    		, boost::function<void(item const&)> cb)
    	{
    		m_dht.get_item(target, boost::bind(&get_immutable_item_callback, _1, cb));
    	}

    	// key is a 32-byte binary string, the public key to look up.
    	// the salt is optional
    	void dht_tracker::get_item(char const* key
    		, boost::function<void(item const&)> cb
    		, std::string salt)
    	{
    		m_dht.get_item(key, salt, boost::bind(&get_mutable_item_callback, _1, cb));
    	}

    	void dht_tracker::put_item(entry data
    		, boost::function<void()> cb)
    	{
    		std::string flat_data;
    		bencode(std::back_inserter(flat_data), data);
    		sha1_hash target = item_target_id(
relevance 0 | ../include/libtorrent/config.hpp:294 | Make this count Unicode characters instead of bytes on windows
## Make this count Unicode characters instead of bytes on windows #### ../include/libtorrent/config.hpp:294
    #define TORRENT_USE_SYSCTL 1
    #define TORRENT_USE_MLOCK 0
    #define TORRENT_USE_IPV6 0
    #define TORRENT_ICONV_ARG (const char**)
    #define TORRENT_USE_WRITEV 0
    #define TORRENT_USE_READV 0

    #else

    #ifdef _MSC_VER
    #pragma message ( "unknown OS, assuming BSD" )
    #else
    #warning "unknown OS, assuming BSD"
    #endif

    #define TORRENT_BSD
    #endif

    // on windows, NAME_MAX refers to Unicode characters
    // on linux it refers to bytes (utf-8 encoded)

    // windows
    #if defined FILENAME_MAX
    #define TORRENT_MAX_PATH FILENAME_MAX

    // beos
    #elif defined B_PATH_NAME_LENGTH
    #define TORRENT_MAX_PATH B_PATH_NAME_LENGTH

    // solaris
    #elif defined MAXPATH
    #define TORRENT_MAX_PATH MAXPATH

    // none of the above
    #else
    // this is the maximum number of characters in a
    // path element / filename on windows and also on many filesystems commonly used
    // on linux
    #define TORRENT_MAX_PATH 255

    #ifdef _MSC_VER
    #pragma message ( "unknown platform, assuming the longest path is 255" )
    #else
    #warning "unknown platform, assuming the longest path is 255"
    #endif

    #endif

    #if (defined _MSC_VER && _MSC_VER < 1900) && !defined TORRENT_MINGW

    #include <stdarg.h>
relevance 0 | ../include/libtorrent/peer_connection.hpp:723 | make this private
## make this private #### ../include/libtorrent/peer_connection.hpp:723
    		void set_soft_packet_size(int size) { m_soft_packet_size = size; }

    		// if allow_encrypted is false, and the torrent 'ih' turns out
    		// to be an encrypted torrent (AES-256 encrypted) the peer will
    		// be disconnected. This is to prevent non-encrypted peers to
    		// attach to an encrypted torrent
    		void attach_to_torrent(sha1_hash const& ih, bool allow_encrypted);

    		bool verify_piece(peer_request const& p) const;

    		void update_desired_queue_size();

    		void set_timeout(int s) { m_timeout = s; }

    		boost::intrusive_ptr<peer_connection> self()
    		{
    			TORRENT_ASSERT(!m_in_constructor);
    			return boost::intrusive_ptr<peer_connection>(this);
    		}

    	public:

    		// upload and download channel state
    		// enum from peer_info::bw_state
    		boost::uint8_t m_channel_state[2];

    	private:

    		// is true if we learn the incoming connections listening
    		// during the extended handshake
    		bool m_received_listen_port:1;

    		// this is set to true when a have_all
    		// message is received. This information
    		// is used to fill the bitmask in init()
    		bool m_have_all:1;

    		// other side says that it's interested in downloading
    		// from us.
    		bool m_peer_interested:1;

    		// the other side has told us that it won't send anymore
    		// data to us for a while
    		bool m_peer_choked:1;

    		// the peer has pieces we are interested in
    		bool m_interesting:1;

    		// we have choked the upload to the peer
    		bool m_choked:1;
relevance 0 | ../include/libtorrent/peer_connection.hpp:810 | make these private as well
## make these private as well #### ../include/libtorrent/peer_connection.hpp:810
    		bool m_corked:1;

    		// set to true if this peer has metadata, and false
    		// otherwise.
    		bool m_has_metadata:1;

    		// this is set to true if this peer was accepted exceeding
    		// the connection limit. It means it has to disconnect
    		// itself, or some other peer, as soon as it's completed
    		// the handshake. We need to wait for the handshake in
    		// order to know which torrent it belongs to, to know which
    		// other peers to compare it to.
    		bool m_exceeded_limit:1;

    		// this is slow-start at the bittorrent layer. It affects how we increase
    		// desired queue size (i.e. the number of outstanding requests we keep).
    		// While the underlying transport protocol is in slow-start, the number of
    		// outstanding requests need to increase at the same pace to keep up.
    		bool m_slow_start:1;

    	protected:

    		// number of bytes this peer can send and receive
    		int m_quota[2];

    		// statistics about upload and download speeds
    		// and total amount of uploads and downloads for
    		// this peer
    		stat m_statistics;

    		// a back reference to the session
    		// the peer belongs to.
    		aux::session_impl& m_ses;

    #ifndef TORRENT_DISABLE_EXTENSIONS
    		typedef std::list<boost::shared_ptr<peer_plugin> > extension_list_t;
    		extension_list_t m_extensions;
    #endif

    		// called from the main loop when this connection has any
    		// work to do.
    		void on_send_data(error_code const& error
    			, std::size_t bytes_transferred);
    		void on_receive_data(error_code const& error
    			, std::size_t bytes_transferred);

    		// the average rate of receiving complete piece messages
    		sliding_average<20> m_piece_rate;
    		sliding_average<20> m_send_rate;

    	private:
relevance 0 | ../include/libtorrent/proxy_base.hpp:166 | it would be nice to remember the bind port and bind once we know where the proxy is m_sock.bind(endpoint, ec);
## it would be nice to remember the bind port and bind once we know where the proxy is m_sock.bind(endpoint, ec); #### ../include/libtorrent/proxy_base.hpp:166
    	{
    		return m_sock.get_option(opt, ec);
    	}

    #ifndef BOOST_NO_EXCEPTIONS
    	void bind(endpoint_type const& endpoint)
    	{
    //		m_sock.bind(endpoint);
    	}
    #endif

    	void bind(endpoint_type const& endpoint, error_code& ec)
    	{
    		// the reason why we ignore binds here is because we don't
    		// (necessarily) yet know what address family the proxy
    		// will resolve to, and binding to the wrong one would
    		// break our connection attempt later. The caller here
    		// doesn't necessarily know that we're proxying, so this
    		// bind address is based on the final endpoint, not the
    		// proxy.
    	}

    #ifndef BOOST_NO_EXCEPTIONS
    	void open(protocol_type const& p)
    	{
    //		m_sock.open(p);
    	}
    #endif

    	void open(protocol_type const& p, error_code& ec)
    	{
    		// we need to ignore this for the same reason as stated
    		// for ignoring bind()
    //		m_sock.open(p, ec);
    	}

    #ifndef BOOST_NO_EXCEPTIONS
    	void close()
    	{
    		m_remote_endpoint = endpoint_type();
    		m_sock.close();
    		m_resolver.cancel();
    	}
    #endif

    	void close(error_code& ec)
    	{
    		m_remote_endpoint = endpoint_type();
    		m_sock.close(ec);
    		m_resolver.cancel();
    	}
relevance 0 | ../include/libtorrent/stat.hpp:113 | this is 4 bytes of padding!
## this is 4 bytes of padding! #### ../include/libtorrent/stat.hpp:113
    		int counter() const { return m_counter; }

    		void clear()
    		{
    			m_counter = 0;
    			m_5_sec_average = 0;
    			m_30_sec_average = 0;
    			m_total_counter = 0;
    		}

    	private:

    		// the accumulator for this second.
    		int m_counter;

    		// sliding average
    		int m_5_sec_average;
    		int m_30_sec_average;

    		// total counters
    		size_type m_total_counter;
    	};

    	class TORRENT_EXTRA_EXPORT stat
    	{
    	friend class invariant_access;
    	public:
    		void operator+=(const stat& s)
    		{
    			for (int i = 0; i < num_channels; ++i)
    				m_stat[i] += s.m_stat[i];
    		}

    		void sent_syn(bool ipv6)
    		{
    #ifndef TORRENT_DISABLE_FULL_STATS
    			m_stat[upload_ip_protocol].add(ipv6 ? 60 : 40);
    #endif
    		}

    		void received_synack(bool ipv6)
    		{
    #ifndef TORRENT_DISABLE_FULL_STATS
    			// we received SYN-ACK and also sent ACK back
    			m_stat[download_ip_protocol].add(ipv6 ? 60 : 40);
    			m_stat[upload_ip_protocol].add(ipv6 ? 60 : 40);
    #endif
    		}
relevance 0 | ../include/libtorrent/torrent_info.hpp:123 | include the number of peers received from this tracker, at last announce
## include the number of peers received from this tracker, at last announce #### ../include/libtorrent/torrent_info.hpp:123
    		// if this tracker failed the last time it was contacted
    		// this error code specifies what error occurred
    		error_code last_error;

    		// returns the number of seconds to the next announce on
    		// this tracker. ``min_announce_in()`` returns the number of seconds until we are
    		// allowed to force another tracker update with this tracker.
    		//
    		// If the last time this tracker was contacted failed, ``last_error`` is the error
    		// code describing what error occurred.
    		int next_announce_in() const;
    		int min_announce_in() const;

    		// the time of next tracker announce
    		ptime next_announce;

    		// no announces before this time
    		ptime min_announce;

    		// these are either -1 or the scrape information this tracker last responded with. *incomplete* is
    		// the current number of downloaders in the swarm, *complete* is the current number
    		// of seeds in the swarm and *downloaded* is the cumulative number of completed
    		// downloads of this torrent, since the beginning of time (from this tracker's point
    		// of view).

    		// if this tracker has returned scrape data, these fields are filled
    		// in with valid numbers. Otherwise they are set to -1.
    		// the number of current downloaders
    		int scrape_incomplete;
    		int scrape_complete;
    		int scrape_downloaded;

    		// the tier this tracker belongs to
    		boost::uint8_t tier;

    		// the max number of failures to announce to this tracker in
    		// a row, before this tracker is not used anymore. 0 means unlimited
    		boost::uint8_t fail_limit;

    		// the number of times in a row we have failed to announce to this
    		// tracker.
    		boost::uint8_t fails:7;

    		// true while we're waiting for a response from the tracker.
    		bool updating:1;

    		// flags for the source bitmask, each indicating where
    		// we heard about this tracker
    		enum tracker_source
relevance 0 | ../include/libtorrent/upnp.hpp:112 | support using the windows API for UPnP operations as well
## support using the windows API for UPnP operations as well #### ../include/libtorrent/upnp.hpp:112
    			external_port_must_be_wildcard = 727
    		};

    		// hidden
    		TORRENT_EXPORT boost::system::error_code make_error_code(error_code_enum e);
    	}

    	// the boost.system error category for UPnP errors
    	TORRENT_EXPORT boost::system::error_category& get_upnp_category();

    // int: port-mapping index
    // address: external address as queried from router
    // int: external port
    // std::string: error message
    // an empty string as error means success
    // a port-mapping index of -1 means it's
    // an informational log message
    typedef boost::function<void(int, address, int, error_code const&)> portmap_callback_t;
    typedef boost::function<void(char const*)> log_callback_t;

    class TORRENT_EXTRA_EXPORT upnp : public intrusive_ptr_base<upnp>
    {
    public:
    	upnp(io_service& ios, connection_queue& cc
    		, address const& listen_interface, std::string const& user_agent
    		, portmap_callback_t const& cb, log_callback_t const& lcb
    		, bool ignore_nonrouters, void* state = 0);
    	~upnp();

    	void* drain_state();

    	enum protocol_type { none = 0, udp = 1, tcp = 2 };

    	// Attempts to add a port mapping for the specified protocol. Valid protocols are
    	// ``upnp::tcp`` and ``upnp::udp`` for the UPnP class and ``natpmp::tcp`` and
    	// ``natpmp::udp`` for the NAT-PMP class.
    	//
    	// ``external_port`` is the port on the external address that will be mapped. This
    	// is a hint, you are not guaranteed that this port will be available, and it may
    	// end up being something else. In the portmap_alert_ notification, the actual
    	// external port is reported.
    	//
    	// ``local_port`` is the port in the local machine that the mapping should forward
    	// to.
    	//
    	// The return value is an index that identifies this port mapping. This is used
    	// to refer to mappings that fails or succeeds in the portmap_error_alert_ and
    	// portmap_alert_ respectively. If The mapping fails immediately, the return value
    	// is -1, which means failure. There will not be any error alert notification for
    	// mappings that fail with a -1 return value.
    	int add_mapping(protocol_type p, int external_port, int local_port);
relevance 0 | ../include/libtorrent/utp_stream.hpp:378 | implement blocking write. Low priority since it's not used (yet)
## implement blocking write. Low priority since it's not used (yet) #### ../include/libtorrent/utp_stream.hpp:378
    		for (typename Mutable_Buffers::const_iterator i = buffers.begin()
    			, end(buffers.end()); i != end; ++i)
    		{
    			using asio::buffer_cast;
    			using asio::buffer_size;
    			add_read_buffer(buffer_cast<void*>(*i), buffer_size(*i));
    #if TORRENT_USE_ASSERTS
    			buf_size += buffer_size(*i);
    #endif
    		}
    		std::size_t ret = read_some(true);
    		TORRENT_ASSERT(ret <= buf_size);
    		TORRENT_ASSERT(ret > 0);
    		return ret;
    	}

    	template <class Const_Buffers>
    	std::size_t write_some(Const_Buffers const& buffers, error_code& ec)
    	{
    		TORRENT_ASSERT(false && "not implemented!");
    		return 0;
    	}

    #ifndef BOOST_NO_EXCEPTIONS
    	template <class Mutable_Buffers>
    	std::size_t read_some(Mutable_Buffers const& buffers)
    	{
    		error_code ec;
    		std::size_t ret = read_some(buffers, ec);
    		if (ec)
    			boost::throw_exception(boost::system::system_error(ec));
    		return ret;
    	}

    	template <class Const_Buffers>
    	std::size_t write_some(Const_Buffers const& buffers)
    	{
    		error_code ec;
    		std::size_t ret = write_some(buffers, ec);
    		if (ec)
    			boost::throw_exception(boost::system::system_error(ec));
    		return ret;
    	}
    #endif

    	template <class Handler>
    	void async_write_some(boost::asio::null_buffers const& buffers, Handler const& handler)
    	{
    		TORRENT_ASSERT(false);
    	}
relevance 0 | ../include/libtorrent/kademlia/item.hpp:61 | since this is a public function, it should probably be moved out of this header and into one with other public functions.
## since this is a public function, it should probably be moved out of this header and into one with other public functions. #### ../include/libtorrent/kademlia/item.hpp:61
    #include <boost/array.hpp>

    namespace libtorrent { namespace dht
    {

    // calculate the target hash for an immutable item.
    sha1_hash TORRENT_EXTRA_EXPORT item_target_id(
    	std::pair<char const*, int> v);

    // calculate the target hash for a mutable item.
    sha1_hash TORRENT_EXTRA_EXPORT item_target_id(std::pair<char const*, int> salt
    	, char const* pk);

    bool TORRENT_EXTRA_EXPORT verify_mutable_item(
    	std::pair<char const*, int> v
    	, std::pair<char const*, int> salt
    	, boost::uint64_t seq
    	, char const* pk
    	, char const* sig);

    // given a byte range ``v`` and an optional byte range ``salt``, a
    // sequence number, public key ``pk`` (must be 32 bytes) and a secret key
    // ``sk`` (must be 64 bytes), this function produces a signature which
    // is written into a 64 byte buffer pointed to by ``sig``. The caller
    // is responsible for allocating the destination buffer that's passed in
    // as the ``sig`` argument. Typically it would be allocated on the stack.
    void TORRENT_EXPORT sign_mutable_item(
    	std::pair<char const*, int> v
    	, std::pair<char const*, int> salt
    	, boost::uint64_t seq
    	, char const* pk
    	, char const* sk
    	, char* sig);

    enum
    {
    	item_pk_len = 32,
    	item_sk_len = 64,
    	item_sig_len = 64
    };

    class TORRENT_EXTRA_EXPORT item
    {
    public:
    	item() : m_seq(0), m_mutable(false)  {}
    	item(char const* pk, std::string const& salt);
    	item(entry const& v) { assign(v); }
    	item(entry const& v
    		, std::pair<char const*, int> salt
    		, boost::uint64_t seq, char const* pk, char const* sk);