ipv6_peer::ipv6_peer( tcp::endpoint const& ep, bool c, int src ) : torrent_peer(ep.port(), c, src) , addr(ep.address().to_v6().to_bytes()) { is_v6_addr = true; #if TORRENT_USE_I2P is_i2p_addr = false; #endif }
void set_peer(tcp::endpoint const& ep) { #if TORRENT_USE_IPV6 is_v6_addr = ep.address().is_v6(); if (is_v6_addr) addr.v6 = ep.address().to_v6().to_bytes(); else #endif addr.v4 = ep.address().to_v4().to_bytes(); port = ep.port(); }
void seedArm_service_impl::acceptWait(const std::string& addr, const std::string& port , size_t workerThreadsCount , const size_t keepAliveMilliseconds_GlobalValue) { if (0 == workerThreadsCount){ workerThreadsCount = boost::thread::hardware_concurrency(); } //////////////////////////////////////////////// // set common keepAliveTime connection_impl::KEEP_ALIVE_TIME_MS = keepAliveMilliseconds_GlobalValue; using TCP = boost::asio::ip::tcp; //////////////////////////////////////////////// // ready accpetor TCP::resolver resolver(_ios); TCP::resolver::query quary(addr, port); TCP::endpoint endpoint = *resolver.resolve(quary); _acceptor.open(endpoint.protocol()); _acceptor.set_option(TCP::no_delay(true)); _acceptor.set_option(TCP::acceptor::reuse_address(true)); _acceptor.bind(endpoint); _acceptor.listen(); _acceptConn = connection::ptr(new connection_impl(this, _ios), connection_impl::destruct); _acceptor.async_accept(_acceptConn->socket(), _strand.wrap( boost::bind(&seedArm_service_impl::accept, this, boost::asio::placeholders::error))); //////////////////////////////////////////////// // ready worker for (size_t i = 0; i < workerThreadsCount; ++i) { boost::shared_ptr<boost::thread> thread(new boost::thread( boost::bind(&boost::asio::io_service::run, &_ios))); s_threads.push_back(thread); } //////////////////////////////////////////////// // ready expireTimer _prevTime = boost::chrono::system_clock::now(); _updater.expires_from_now(boost::posix_time::milliseconds(UPDATE_TIME_MS)); _updater.async_wait(_strand.wrap( boost::bind(&seedArm_service_impl::update, this, boost::asio::placeholders::error))); }
ipv4_peer::ipv4_peer( tcp::endpoint const& ep, bool c, int src ) : torrent_peer(ep.port(), c, src) , addr(ep.address().to_v4()) { #if TORRENT_USE_IPV6 is_v6_addr = false; #endif #if TORRENT_USE_I2P is_i2p_addr = false; #endif }
listener( boost::asio::io_context& ioc, tcp::endpoint endpoint) : acceptor_(ioc) , socket_(ioc) { boost::system::error_code ec; // Open the acceptor acceptor_.open(endpoint.protocol(), ec); if(ec) { fail(ec, "open"); return; } // Bind to the server address acceptor_.bind(endpoint, ec); if(ec) { fail(ec, "bind"); return; } // Start listening for connections acceptor_.listen( boost::asio::socket_base::max_listen_connections, ec); if(ec) { fail(ec, "listen"); return; } }
bool TajoSyncClient::connect(const tcp::endpoint &endpoint, std::string &errmsg) { boost::system::error_code ec; pImpl_->socket_.open(endpoint.protocol(), ec); if( !ec ) { pImpl_->socket_.set_option(tcp::no_delay(true), ec); if( !ec ) { pImpl_->socket_.connect(endpoint, ec); } } if( !ec ) { pImpl_->state_ = TajoClientState::CONNECTED; return true; } else { errmsg = ec.message(); return false; } }
void Server::Start(tcp::endpoint endpoint) { _acceptor.open(endpoint.protocol()); _acceptor.set_option(tcp::acceptor::reuse_address(true)); _acceptor.bind(endpoint); _acceptor.listen(); _StartAccept(); sLog.Info(LOG_STRATUM, "Stratum server started"); }
bool was_introduced_by(peer_plugin const* pp, tcp::endpoint const& ep) { ut_pex_peer_plugin const* p = static_cast<ut_pex_peer_plugin const*>(pp); #if TORRENT_USE_IPV6 if (ep.address().is_v4()) { #endif ut_pex_peer_plugin::peers4_t::value_type v(ep.address().to_v4().to_bytes(), ep.port()); ut_pex_peer_plugin::peers4_t::const_iterator i = std::lower_bound(p->m_peers.begin(), p->m_peers.end(), v); return i != p->m_peers.end() && *i == v; #if TORRENT_USE_IPV6 } else { ut_pex_peer_plugin::peers6_t::value_type v(ep.address().to_v6().to_bytes(), ep.port()); ut_pex_peer_plugin::peers6_t::const_iterator i = std::lower_bound(p->m_peers6.begin(), p->m_peers6.end(), v); return i != p->m_peers6.end() && *i == v; } #endif }
void run(unsigned sleepMillis) { std::cout << "in GUIMessageSenderThread: run " << sleepMillis << "ms" << std::endl; while (running_) { if (!connected_) { std::cout << "In GUIMessageSenderThread trying to connect to: " << host_ << " " << port_ << std::endl; //tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_); //tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_); endpoint_.address(boost::asio::ip::address::from_string(host_)); endpoint_.port(port_); tcp::acceptor acceptor(*io_service_, endpoint_); getClientConnection(acceptor); try { sendMessages(sleepMillis); acceptor.close(); stream_.clear(); stream_.close(); } catch (std::exception& e) { std::cerr << "GUIMessageSenderThread Error in connection: " << e.what() << std::endl; } connected_ = false; //boost::asio::deadline_timer timer(*io_service_,boost::posix_time::milliseconds(1000)); //timer.expires_from_now(boost::posix_time::milliseconds(1000)); //timer.wait(); } } std::cout << "done GUIMessageSenderThread: run " << sleepMillis << "ms" << std::endl; }
inline std::string print_endpoint(tcp::endpoint const& ep) { error_code ec; std::string ret; address const& addr = ep.address(); #if TORRENT_USE_IPV6 if (addr.is_v6()) { ret += '['; ret += addr.to_string(ec); ret += ']'; ret += ':'; ret += to_string(ep.port()).elems; } else #endif { ret += addr.to_string(ec); ret += ':'; ret += to_string(ep.port()).elems; } return ret; }
void worker::add_endpoint(tcp::endpoint& ep, bs::error_code& ec) { try { auto acceptor = tcp::acceptor(m_iosvc); acceptor.open(ep.protocol()); acceptor.set_option(tcp::acceptor::reuse_address(true)); acceptor.bind(ep); int backlog = options::opts["server.backlog"].as<int>(); if (backlog == 0) { backlog = ba::socket_base::max_connections; } acceptor.listen(backlog); m_acceptors.push_back(std::move(acceptor)); } catch (bs::system_error& e) { ec = e.code(); } }
/** Open a listening port. @param ep The address and port to bind to. @param ec Set to the error, if any occurred. */ void open(tcp::endpoint const& ep, error_code& ec) { acceptor_.open(ep.protocol(), ec); if(ec) return fail("open", ec); acceptor_.set_option( boost::asio::socket_base::reuse_address{true}); acceptor_.bind(ep, ec); if(ec) return fail("bind", ec); acceptor_.listen( boost::asio::socket_base::max_connections, ec); if(ec) return fail("listen", ec); do_accept(); }
listener( boost::asio::io_context& ioc, ssl::context& ctx, tcp::endpoint endpoint) : ctx_(ctx) , acceptor_(ioc) , socket_(ioc) { boost::system::error_code ec; // Open the acceptor acceptor_.open(endpoint.protocol(), ec); if(ec) { fail(ec, "open"); return; } // Allow address reuse acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec); if(ec) { fail(ec, "set_option"); return; } // Bind to the server address acceptor_.bind(endpoint, ec); if(ec) { fail(ec, "bind"); return; } // Start listening for connections acceptor_.listen( boost::asio::socket_base::max_listen_connections, ec); if(ec) { fail(ec, "listen"); return; } }
static bool resolve_address_tcp(io_service &io, size_t chan, std::string host, unsigned short port, tcp::endpoint &ep) { bool result = false; tcp::resolver resolver(io); error_code ec; tcp::resolver::query query(host, ""); std::for_each(resolver.resolve(query, ec), tcp::resolver::iterator(), [&](const tcp::endpoint & q_ep) { ep = q_ep; ep.port(port); result = true; logDebug(PFXd "host %s resolved as %s", chan, host.c_str(), to_string_ss(ep).c_str()); }); if (ec) { logWarn(PFXd "resolve error: %s", chan, ec.message().c_str()); result = false; } return result; }
bool has_peer(peer_list const& p, tcp::endpoint const& ep) { auto const its = p.find_peers(ep.address()); return its.first != its.second; }
// 1. if the IP addresses are identical, hash the ports in 16 bit network-order // binary representation, ordered lowest first. // 2. if the IPs are in the same /24, hash the IPs ordered, lowest first. // 3. if the IPs are in the ame /16, mask the IPs by 0xffffff55, hash them // ordered, lowest first. // 4. if IPs are not in the same /16, mask the IPs by 0xffff5555, hash them // ordered, lowest first. // // * for IPv6 peers, just use the first 64 bits and widen the masks. // like this: 0xffff5555 -> 0xffffffff55555555 // the lower 64 bits are always unmasked // // * for IPv6 addresses, compare /32 and /48 instead of /16 and /24 // // * the two IP addresses that are used to calculate the rank must // always be of the same address family // // * all IP addresses are in network byte order when hashed boost::uint32_t peer_priority(tcp::endpoint e1, tcp::endpoint e2) { TORRENT_ASSERT(e1.address().is_v4() == e2.address().is_v4()); using std::swap; boost::uint32_t ret; if (e1.address() == e2.address()) { if (e1.port() > e2.port()) swap(e1, e2); boost::uint32_t p; reinterpret_cast<boost::uint16_t*>(&p)[0] = htons(e1.port()); reinterpret_cast<boost::uint16_t*>(&p)[1] = htons(e2.port()); ret = crc32c_32(p); } #if TORRENT_USE_IPV6 else if (e1.address().is_v6()) { const static boost::uint8_t v6mask[][8] = { { 0xff, 0xff, 0xff, 0xff, 0x55, 0x55, 0x55, 0x55 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x55, 0x55 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; if (e1 > e2) swap(e1, e2); address_v6::bytes_type b1 = e1.address().to_v6().to_bytes(); address_v6::bytes_type b2 = e2.address().to_v6().to_bytes(); int mask = memcmp(&b1[0], &b2[0], 4) ? 0 : memcmp(&b1[0], &b2[0], 6) ? 1 : 2; apply_mask(&b1[0], v6mask[mask], 8); apply_mask(&b2[0], v6mask[mask], 8); boost::uint64_t addrbuf[4]; memcpy(&addrbuf[0], &b1[0], 16); memcpy(&addrbuf[2], &b2[0], 16); ret = crc32c(addrbuf, 4); } #endif else { const static boost::uint8_t v4mask[][4] = { { 0xff, 0xff, 0x55, 0x55 }, { 0xff, 0xff, 0xff, 0x55 }, { 0xff, 0xff, 0xff, 0xff } }; if (e1 > e2) swap(e1, e2); address_v4::bytes_type b1 = e1.address().to_v4().to_bytes(); address_v4::bytes_type b2 = e2.address().to_v4().to_bytes(); int mask = memcmp(&b1[0], &b2[0], 2) ? 0 : memcmp(&b1[0], &b2[0], 3) ? 1 : 2; apply_mask(&b1[0], v4mask[mask], 4); apply_mask(&b2[0], v4mask[mask], 4); boost::uint64_t addrbuf; memcpy(&addrbuf, &b1[0], 4); memcpy(reinterpret_cast<char*>(&addrbuf) + 4, &b2[0], 4); ret = crc32c(&addrbuf, 1); } return ret; }
tuple endpoint_to_tuple(tcp::endpoint const& ep) { return boost::python::make_tuple(ep.address().to_string(), ep.port()); }
// 1. if the IP addresses are identical, hash the ports in 16 bit network-order // binary representation, ordered lowest first. // 2. if the IPs are in the same /24, hash the IPs ordered, lowest first. // 3. if the IPs are in the ame /16, mask the IPs by 0xffffff55, hash them // ordered, lowest first. // 4. if IPs are not in the same /16, mask the IPs by 0xffff5555, hash them // ordered, lowest first. // // * for IPv6 peers, just use the first 64 bits and widen the masks. // like this: 0xffff5555 -> 0xffffffff55555555 // the lower 64 bits are always unmasked // // * for IPv6 addresses, compare /32 and /48 instead of /16 and /24 // // * the two IP addresses that are used to calculate the rank must // always be of the same address family // // * all IP addresses are in network byte order when hashed std::uint32_t peer_priority(tcp::endpoint e1, tcp::endpoint e2) { TORRENT_ASSERT(e1.address().is_v4() == e2.address().is_v4()); using std::swap; std::uint32_t ret; if (e1.address() == e2.address()) { if (e1.port() > e2.port()) swap(e1, e2); std::uint32_t p; #if defined BOOST_BIG_ENDIAN p = e1.port() << 16; p |= e2.port(); #elif defined BOOST_LITTLE_ENDIAN p = aux::host_to_network(e2.port()) << 16; p |= aux::host_to_network(e1.port()); #else #error unsupported endianness #endif ret = crc32c_32(p); } #if TORRENT_USE_IPV6 else if (e1.address().is_v6()) { static const std::uint8_t v6mask[][8] = { { 0xff, 0xff, 0xff, 0xff, 0x55, 0x55, 0x55, 0x55 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x55, 0x55 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; if (e1 > e2) swap(e1, e2); address_v6::bytes_type b1 = e1.address().to_v6().to_bytes(); address_v6::bytes_type b2 = e2.address().to_v6().to_bytes(); int mask = memcmp(&b1[0], &b2[0], 4) ? 0 : memcmp(&b1[0], &b2[0], 6) ? 1 : 2; apply_mask(&b1[0], v6mask[mask], 8); apply_mask(&b2[0], v6mask[mask], 8); std::uint64_t addrbuf[4]; memcpy(&addrbuf[0], &b1[0], 16); memcpy(&addrbuf[2], &b2[0], 16); ret = crc32c(addrbuf, 4); } #endif else { static const std::uint8_t v4mask[][4] = { { 0xff, 0xff, 0x55, 0x55 }, { 0xff, 0xff, 0xff, 0x55 }, { 0xff, 0xff, 0xff, 0xff } }; if (e1 > e2) swap(e1, e2); address_v4::bytes_type b1 = e1.address().to_v4().to_bytes(); address_v4::bytes_type b2 = e2.address().to_v4().to_bytes(); int mask = memcmp(&b1[0], &b2[0], 2) ? 0 : memcmp(&b1[0], &b2[0], 3) ? 1 : 2; apply_mask(&b1[0], v4mask[mask], 4); apply_mask(&b2[0], v4mask[mask], 4); std::uint64_t addrbuf; memcpy(&addrbuf, &b1[0], 4); memcpy(reinterpret_cast<char*>(&addrbuf) + 4, &b2[0], 4); ret = crc32c(&addrbuf, 1); } return ret; }