Sirikata::Network::Address convertEndpointToAddress(const boost::asio::ip::tcp::endpoint&ep) { std::ostringstream address; address<<ep.address(); std::ostringstream port; port<<ep.port(); return Address (address.str(),port.str()); }
FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme) : m_scheme(scheme) { m_isV6 = endpoint.address().is_v6(); m_host = endpoint.address().to_string(); m_port = boost::lexical_cast<std::string>(endpoint.port()); }
void ServicePort::handle(Acceptor_ptr acceptor, boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error) { if(!error) { if(m_services.empty()) { #ifdef __DEBUG_NET__ std::clog << "[Error - ServerPort::handle] No services running!" << std::endl; #endif return; } boost::system::error_code error; const boost::asio::ip::tcp::endpoint ip = socket->remote_endpoint(error); uint32_t remoteIp = 0; if(!error) remoteIp = htonl(ip.address().to_v4().to_ulong()); Connection_ptr connection; if(remoteIp && ConnectionManager::getInstance()->acceptConnection(remoteIp) && (connection = ConnectionManager::getInstance()->createConnection( socket, m_io_service, shared_from_this()))) { if(m_services.front()->isSingleSocket()) connection->handle(m_services.front()->makeProtocol(connection)); else connection->accept(); } else if(socket->is_open()) { boost::system::error_code error; socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error); socket->close(error); delete socket; } #ifdef __DEBUG_NET_DETAIL__ std::clog << "handle - OK" << std::endl; #endif accept(acceptor); } else if(error != boost::asio::error::operation_aborted) { PRINT_ASIO_ERROR("Handling"); close(); if(!m_pendingStart) { m_pendingStart = true; Scheduler::getInstance().addEvent(createSchedulerTask(5000, boost::bind( &ServicePort::service, boost::weak_ptr<ServicePort>(shared_from_this()), m_acceptors[acceptor], m_serverPort))); } } #ifdef __DEBUG_NET__ else std::clog << "[Error - ServerPort::handle] Operation aborted." << std::endl; #endif }
void node::on_proxy( const std::uint16_t & tid, const boost::asio::ip::tcp::endpoint & ep, const std::string & value ) { stack_impl_.on_proxy( tid, ep.address().to_string().c_str(), ep.port(), value ); }
int Connection::getIp() { boost::system::error_code error; const boost::asio::ip::tcp::endpoint ip = m_socket.remote_endpoint(error); if(!error) return boost::asio::detail::socket_ops::host_to_network_long(ip.address().to_v4().to_ulong()); g_logger.error("Getting remote ip"); return 0; }
uint32_t Connection::getIP() const { //ip is expressed in network byte order boost::system::error_code error; const boost::asio::ip::tcp::endpoint ip = m_socket->remote_endpoint(error); if(!error) return htonl(ip.address().to_v4().to_ulong()); return 0; }
void TcpConnectionImpl::connect(boost::asio::ip::tcp::endpoint& endpoint) { if(m_isConnected) return; if(m_isClosing) return; m_endPoint = endpoint; std::cout << "Trying to connect to port " << endpoint.port() << " @ " << endpoint.address().to_string() << std::endl; // try to connect, then call handle_connect m_socket.async_connect(endpoint, boost::bind(&TcpConnectionImpl::handle_connect, this, boost::asio::placeholders::error)); }
void ServicePort::onAccept(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error) { if(!error) { if(m_services.empty()) { #ifdef __DEBUG_NET__ std::cout << "Error: [ServerPort::accept] No services running!" << std::endl; #endif return; } boost::system::error_code error; const boost::asio::ip::tcp::endpoint endpoint = socket->remote_endpoint(error); uint32_t remote_ip = 0; if(!error) remote_ip = htonl(endpoint.address().to_v4().to_ulong()); if(remote_ip != 0/* && g_bans.acceptConnection(remote_ip)*/) { Connection_ptr connection = ConnectionManager::getInstance()->createConnection(socket, m_io_service, shared_from_this()); if(m_services.front()->is_single_socket()) connection->acceptConnection(m_services.front()->make_protocol(connection)); else connection->acceptConnection(); } else if(socket->is_open()) { boost::system::error_code error; socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error); socket->close(error); delete socket; } accept(); } else if(error != boost::asio::error::operation_aborted) { if(!m_pendingStart) { close(); m_pendingStart = true; g_scheduler.addEvent(createSchedulerTask(15000, boost::bind(&ServicePort::openAcceptor, boost::weak_ptr<ServicePort>(shared_from_this()), m_serverPort))); } } else { #ifdef __DEBUG_NET__ std::cout << "Error: [ServicePort::onAccept] Operation aborted." << std::endl; #endif } }
uint32_t Connection::getIP() const { //Ip is expressed in network byte order boost::system::error_code error; const boost::asio::ip::tcp::endpoint endpoint = m_socket.remote_endpoint(error); if(!error) return htonl(endpoint.address().to_v4().to_ulong()); else { PRINT_ASIO_ERROR("Getting remote ip"); return 0; } }
uint32_t Connection::getIP() { std::lock_guard<std::recursive_mutex> lockClass(connectionLock); // IP-address is expressed in network byte order boost::system::error_code error; const boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(error); if (error) { return 0; } return htonl(endpoint.address().to_v4().to_ulong()); }
VideoDataHandler(InputStream * is, OutputStream * os, boost::asio::ip::tcp::endpoint e): _db(org::esb::hive::DatabaseService::getDatabase()) { _ep=e; _os=os; _is=is; _oos = new io::ObjectOutputStream(os); _ois = new io::ObjectInputStream(is); _own_id = e.address().to_string(); _own_id += ":"; _own_id += StringUtil::toString(e.port()); shutdown = false; LOGDEBUG("endpoint:" << e); }
void ServicePort::handle(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error) { if(!error) { if(m_services.empty()) { return; } boost::system::error_code error; const boost::asio::ip::tcp::endpoint ip = socket->remote_endpoint(error); uint32_t remoteIp = 0; if(!error) remoteIp = htonl(ip.address().to_v4().to_ulong()); Connection_ptr connection; if(remoteIp && ConnectionManager::getInstance()->acceptConnection(remoteIp) && (connection = ConnectionManager::getInstance()->createConnection( socket, m_io_service, shared_from_this()))) { if(m_services.front()->isSingleSocket()) connection->handle(m_services.front()->makeProtocol(connection)); else connection->accept(); } else if(socket->is_open()) { boost::system::error_code error; socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error); socket->close(error); delete socket; } accept(); } else if(error != boost::asio::error::operation_aborted) { close(); if(!m_pendingStart) { m_pendingStart = true; server.scheduler().addTask(SchedulerTask::create(Milliseconds(5000), std::bind( &ServicePort::onOpen, std::weak_ptr<ServicePort>(shared_from_this()), m_serverPort))); } } }
void handle_read(const boost::system::error_code& ec, std::size_t sz, const boost::asio::ip::tcp::endpoint& endpoint, const string& request, int rpe, boost::shared_ptr<Socket> socket, boost::shared_ptr<boost::asio::streambuf> buf) { ycout << endpoint.address() << "<< \r\n" << boost::make_iterator_range(boost::asio::buffers_begin(buf->data()), boost::asio::buffers_begin(buf->data()) + sz) << "--"; make_request<Socket>(socket->get_io_service(), endpoint, request, rpe); }
void Socket::connect(const ao::ip::tcp::endpoint & e) { socket_.open(e.protocol()); socket_.connect(e); std::cout << "Connected to '" << e << "'" << std::endl; }
/// Handle completion of a read operation. void handleRead(const boost::system::error_code& error, std::size_t bytes_transferred) { if (!error){ readBytes += bytes_transferred; readBuff.has_written(bytes_transferred); onReadMsg(readBuff, writeBuff); readBuff.discard_all(); reading = false; if (writeBuff.readable_bytes()>0){ //std::cout << "going to send " << writeBuff.readable_bytes() << std::endl; //std::cout << writeBuff.as_string() << std::endl; LOG_DEBUG << "going to send " << writeBuff.readable_bytes(); { size_t out_put_size = 256; if (out_put_size > writeBuff.readable_bytes()){ out_put_size = writeBuff.readable_bytes(); } LOG_DEBUG << writeBuff.as_string(0,out_put_size); } startWrite(); } else{ startRead(); } } else{ if (error == boost::asio::error::misc_errors::eof){ LOG_INFO << " Client disconnectd:" << remoteAddr.address() << " " << remoteAddr.port(); } else{ LOG_ERR << remoteAddr.address() << " " << remoteAddr.port() << ",error =" << error.value() << ",msg=" << error.message(); } } reading = false; }
Server::Server(ip::tcp::endpoint endpoint, Server::ConnectionCtor ctor) : acceptor(io_service), image_cache(new ImageCache(0x100)), task_pool(new TaskPool()), ctor(ctor), stop(false) { new_connection.reset(ctor(io_service, image_cache, task_pool)); acceptor.open(endpoint.protocol()); acceptor.set_option(ip::tcp::acceptor::reuse_address(true)); acceptor.bind(endpoint); acceptor.listen(); acceptor.async_accept(new_connection->Socket(), boost::bind(&Server::handle_accept, shared_from_this(), boost::asio::placeholders::error)); }
////////////////////////////////////////////////////////////////////////// /// Pushes to Lua the data of the endpoint void Resolver::EndpointToLua(lua_State* L, const boost::asio::ip::tcp::endpoint& endpoint) { const boost::asio::ip::address& address = endpoint.address(); std::string s = endpoint.address().to_string(); lua_newtable(L); lua_pushstring(L, address.to_string().c_str()); lua_setfield(L, -2, "address"); lua_pushnumber(L, endpoint.port()); lua_setfield(L, -2, "port"); if(endpoint.address().is_v6()) { lua_pushnumber(L, 6); } else if(endpoint.address().is_v4()) { lua_pushnumber(L, 4); } else { lua_pushliteral(L, "unknown"); } lua_setfield(L, -2, "family"); }
/// Handle completion of a write operation. void handleWrite(const boost::system::error_code& error, std::size_t bytes_transferred) { if (!error){ // //std::cout << "send done,bytes_transferred:" << writeBuff.readable_bytes() << bytes_transferred << std::endl; LOG_DEBUG << "send done,bytes_transferred:" << bytes_transferred; BOOST_ASSERT(bytes_transferred == writeBuff.readable_bytes()); writeBytes += bytes_transferred; writeBuff.discard_all(); writing = false; startRead(); } else{ LOG_ERR << remoteAddr.address() << " " << remoteAddr.port() << ",error =" << error.value() << ",msg=" << error.message(); } writing = false; }
void ServicePort::onAccept(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error) { if (!error) { if (m_services.empty()) { return; } boost::system::error_code socketError; const boost::asio::ip::tcp::endpoint endpoint = socket->remote_endpoint(socketError); uint32_t remote_ip = 0; if (!socketError) { remote_ip = htonl(endpoint.address().to_v4().to_ulong()); } if (remote_ip != 0 && g_bans.acceptConnection(remote_ip)) { Connection_ptr connection = ConnectionManager::getInstance()->createConnection(socket, m_io_service, shared_from_this()); Service_ptr service = m_services.front(); if (service->is_single_socket()) { connection->acceptConnection(service->make_protocol(connection)); } else { connection->acceptConnection(); } } else if (socket->is_open()) { socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, socketError); socket->close(socketError); delete socket; } accept(); } else if (error != boost::asio::error::operation_aborted) { if (!m_pendingStart) { close(); m_pendingStart = true; g_scheduler.addEvent(createSchedulerTask(15000, boost::bind(&ServicePort::openAcceptor, boost::weak_ptr<ServicePort>(shared_from_this()), m_serverPort))); } } }
PrivateData( int portNumber ) : m_success(false), m_endpoint(tcp::v4(), portNumber), m_service(), m_acceptor( m_service ), m_thread() { m_acceptor.open( m_endpoint.protocol() ); m_acceptor.set_option( boost::asio::ip::tcp::acceptor::reuse_address(true)); m_acceptor.bind( m_endpoint ); m_acceptor.listen(); m_success = true; }
server(boost::asio::io_service & io_service, const boost::asio::ip::tcp::endpoint & endpoint) : io_service_(io_service) , acceptor_(io_service_, endpoint) { std::cout << esc(MAKE_GREEN) << LOG_HEADER << "Echo server is listening on port: " << endpoint.port() << esc(RESET_COLOR) << std::endl; start_accept(); }
bool TargetedCertificateManager::isValidTarget(boost::asio::ip::tcp::endpoint &endpoint, bool wildcardOK) { boost::asio::ip::address address = endpoint.address(); std::list<Certificate*>::iterator i = certificates.begin(); std::list<Certificate*>::iterator end = certificates.end(); for ( ; i != end; i++) { if ((*i)->isValidTarget(address, wildcardOK)) return true; } return false; }
void http_server_base::bind_listen(boost::system::error_code & ec, boost::asio::ip::tcp::endpoint ep) { accept_opt->open(ep.protocol(), ec); if (ec) return; accept_opt->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true), ec); if (ec) return; accept_opt->bind(ep, ec); if (ec) return; accept_opt->listen(boost::asio::socket_base::max_connections, ec); }
void CommandServer::createTcpEndpoint() { std::stringstream streamForPort; streamForPort << m_options.port; Ip::tcp::resolver resolver(m_tcpAcceptor.get_io_service()); // TODO: support ipv6 Ip::tcp::resolver::query query(Ip::tcp::v4(), m_options.host, streamForPort.str(), Ip::resolver_query_base::numeric_service); Ip::tcp::endpoint endpoint = *resolver.resolve(query); m_tcpAcceptor.open(endpoint.protocol()); m_tcpAcceptor.set_option(Ip::tcp::acceptor::reuse_address(true)); m_tcpAcceptor.bind(endpoint); m_tcpAcceptor.listen(); LOG(info) << "Listening on " << endpoint; startAcceptOnTcp(); }
void Server::start_accept_connections_on(boost::asio::ip::tcp::endpoint endpoint) { using namespace boost::asio; // Acceptors used to listen for incoming connections over ip::tcp. IpTcpConnectionAcceptor_ptr acceptor( new IpTcpConnectionAcceptor(io_service_) ); // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). acceptor->open( endpoint.protocol() ); acceptor->set_option( ip::tcp::acceptor::reuse_address(true) ); acceptor->bind(endpoint); acceptor->listen(); // The next connection to be accepted. ConnectionIpTcp_ptr next_connection( new ConnectionIpTcp(io_service_, request_handler_) ); acceptor->async_accept( next_connection->socket(), boost::bind(&Server::handle_accept, this, acceptor, next_connection, placeholders::error) ); }
void TargetedCertificateManager::getCertificateForTarget(boost::asio::ip::tcp::endpoint &endpoint, bool wildcardOK, X509 *serverCertificate, Certificate **cert, std::list<Certificate*> **chainList) { boost::asio::ip::address address = endpoint.address(); *chainList = &(this->chainList); // *chain = this->chain; std::list<Certificate*>::iterator i = certificates.begin(); std::list<Certificate*>::iterator end = certificates.end(); for ( ; i != end; i++) { if ((*i)->isValidTarget(address, wildcardOK)) { *cert = (*i); return; } } *cert = NULL; return; }
/** * @return the ip address of the endpoint endpoint. */ std::string ip_address(const boost::asio::ip::tcp::endpoint &endpoint) { return endpoint.address().to_string() + ":" + boost::lexical_cast<std::string>(endpoint.port()); }
void bind( boost::asio::ip::tcp::endpoint const & ep , boost::system::error_code &ec) { if(event_channel_) { HPX_IBVERBS_THROWS_IF(ec, boost::asio::error::already_connected); } else { event_channel_ = rdma_create_event_channel(); if(!event_channel_) { int verrno = errno; close(ec); boost::system::error_code err(verrno, boost::system::system_category()); HPX_IBVERBS_THROWS_IF( ec , err ); return; } set_nonblocking(event_channel_->fd, ec); if(ec) { close(ec); return; } int ret = 0; ret = rdma_create_id(event_channel_, &listener_, NULL, RDMA_PS_TCP); if(ret) { int verrno = errno; close(ec); boost::system::error_code err(verrno, boost::system::system_category()); HPX_IBVERBS_THROWS_IF( ec , err ); return; } std::string host = ep.address().to_string(); std::string port = boost::lexical_cast<std::string>(ep.port()); addrinfo *addr; getaddrinfo(host.c_str(), port.c_str(), NULL, &addr); ret = rdma_bind_addr(listener_, addr->ai_addr); freeaddrinfo(addr); if(ret) { int verrno = errno; close(ec); boost::system::error_code err(verrno, boost::system::system_category()); HPX_IBVERBS_THROWS_IF( ec , err ); return; } ret = rdma_listen(listener_, 10); /* backlog = 10 is arbitrary */ if(ret) { int verrno = errno; close(ec); boost::system::error_code err(verrno, boost::system::system_category()); HPX_IBVERBS_THROWS_IF( ec , err ); return; } HPX_IBVERBS_RESET_EC(ec); } }
std::int16_t incentive::calculate_score( const boost::asio::ip::tcp::endpoint & ep ) { std::int16_t ret = -1; auto index = utility::find_block_index_by_height( globals::instance().best_block_height() ); if (index) { const auto & hash_block = index->get_block_hash(); /** * Get the node endpoint. */ auto node_ep = ep.address().to_string() + ":" + std::to_string(ep.port()) ; /** * Hash the endpoint. */ auto digest1 = hash::sha256d( reinterpret_cast<const std::uint8_t *>(node_ep.data()), node_ep.size() ); /** * Hash the hash of the block. */ auto digest2 = hash::sha256d( hash_block.digest(), sha256::digest_length ); auto hash2 = sha256::from_digest(&digest2[0]); auto digest3 = hash::sha256d( &digest2[0], &digest2[0] + digest2.size(), &digest1[0], &digest1[0] + digest1.size() ); auto hash3 = sha256::from_digest(&digest3[0]); if (hash3 > hash2) { ret = static_cast<std::int16_t> ( (hash3 - hash2).to_uint64()) ; } else { ret = static_cast<std::int16_t> ( (hash2 - hash3).to_uint64()) ; } } return ret; }
EndPoint::EndPoint(boost::asio::ip::tcp::endpoint& bEndpoint): _hostName(), _port(static_cast<unsigned short>(bEndpoint.port())), _address(new Address(bEndpoint)) {}