Пример #1
0
		bool bind(endpoint_type& endpoint)
		{
			return socket_ops::bind(
				_socket,
				endpoint.data(),
				endpoint.size());
		}
Пример #2
0
 void listen(endpoint_type const & endpoint, boost::system::error_code & ec, bool reuse_addr = true)
 {
     if (endpoint.protocol() == protocol_type::v4())
         v4_type::listen(endpoint, ec, reuse_addr);
     if (endpoint.protocol() == protocol_type::v6())
         v6_type::listen(endpoint, ec, reuse_addr);
 }
 // Connect the socket to the specified endpoint.
 asio::error_code connect(implementation_type& impl,
     const endpoint_type& peer_endpoint, asio::error_code& ec)
 {
   socket_ops::sync_connect(impl.socket_,
       peer_endpoint.data(), peer_endpoint.size(), ec);
   return ec;
 }
Пример #4
0
    explicit
    socket(endpoint_type endpoint) {
        typename endpoint_type::protocol_type protocol = endpoint.protocol();

        m_fd = ::socket(protocol.family(), protocol.type(), protocol.protocol());

        if(m_fd == -1) {
            throw std::system_error(errno, std::system_category(), "unable to create a socket");
        }

        medium_type::configure(m_fd);

        if(::connect(m_fd, endpoint.data(), endpoint.size()) != 0) {
            auto ec = std::error_code(errno, std::system_category());

            ::close(m_fd);

            throw std::system_error(
                ec,
                cocaine::format("unable to connect a socket to '%s'", endpoint)
            );
        }

        ::fcntl(m_fd, F_SETFD, FD_CLOEXEC);
        ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
    }
Пример #5
0
  /// Bind the fiber acceptor to the specified local endpoint.
  boost::system::error_code bind(implementation_type& impl,
                                 const endpoint_type& endpoint,
                                 boost::system::error_code& ec) {
    impl->p_fib_demux = &(endpoint.demux());
    impl->p_fib_demux->bind(endpoint.port(), impl, ec);

    return ec;
  }
Пример #6
0
    static inline
    void
    pack(msgpack::packer<Stream>& target, const endpoint_type& source) {
        const std::string address = source.address().to_string();
        const unsigned short port = source.port();

        type_traits<tuple_type>::pack(target, tuple_type(address, port));
    }
  size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
      const endpoint_type& destination, socket_base::message_flags flags,
      asio::error_code& ec)
  {
    buffer_sequence_adapter<asio::const_buffer,
        ConstBufferSequence> bufs(buffers);

    return socket_ops::sync_sendto(impl.socket_, impl.state_,
        bufs.buffers(), bufs.count(), flags,
        destination.data(), destination.size(), ec);
  }
Пример #8
0
    static inline
    void
    unpack(const msgpack::object& source, endpoint_type& target) {
        std::string address;
        unsigned short port;

        type_traits<tuple_type>::unpack(source, std::move(std::tie(address, port)));

        target.address(boost::asio::ip::address::from_string(address));
        target.port(port);
    }
Пример #9
0
    std::string address() const {
        if (endpoint_.address().is_v6()) {
            std::string result;
            result.push_back('[');
            result.append(endpoint().address().to_string());
            result.push_back(']');
            return result;
        }

        return endpoint_.address().to_string();
    }
Пример #10
0
  // Resolve an endpoint to a list of entries.
  iterator_type resolve(implementation_type&,
      const endpoint_type& endpoint, asio::error_code& ec)
  {
    char host_name[NI_MAXHOST];
    char service_name[NI_MAXSERV];
    socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
        host_name, NI_MAXHOST, service_name, NI_MAXSERV,
        endpoint.protocol().type(), ec);

    return ec ? iterator_type() : iterator_type::create(
        endpoint, host_name, service_name);
  }
Пример #11
0
		ssize_t sendto(
			endpoint_type& to,
			void* buffer,
			size_t len
		)
		{
			return detail::socket_ops::sync_sendto(
				_socket,
				buffer,
				len,
				0,
				to.data(),
				to.size());
		}
Пример #12
0
 virtual request_t build_request(endpoint_type ep, error_code & ec)
 {
     request_t rc = request_t();
     if(!ep.address().is_v4())
     {
         ec = error_code(boost::asio::error::address_family_not_supported);
         return rc;
     }
     rc.detail.version = 4;
     rc.detail.command = 1;
     rc.detail.destination_port = ::htons(ep.port());
     rc.detail.destination_address = ep.address().to_v4().to_bytes();
     rc.detail.end_marker = 0;
     return rc;
 }
Пример #13
0
		ssize_t recvfrom(
			endpoint_type& from,
			void* buffer,
			size_t len
		)
		{
			socklen_t size = from.size();
			return detail::socket_ops::sync_recvfrom(
				_socket,
				buffer,
				len,
				0,
				from.data(),
				&size);
		}
Пример #14
0
	void async_connect(endpoint_type const& endpoint, Handler const& handler)
	{
		// make sure we don't try to connect to INADDR_ANY. binding is fine,
		// and using a hostname is fine on SOCKS version 5.
		TORRENT_ASSERT(m_command == socks5_bind
			|| endpoint.address() != address()
			|| (!m_dst_name.empty() && m_version == 5));

		m_remote_endpoint = endpoint;

		// the connect is split up in the following steps:
		// 1. resolve name of proxy server
		// 2. connect to proxy server
		// 3. if version == 5:
		//   3.1 send SOCKS5 authentication method message
		//   3.2 read SOCKS5 authentication response
		//   3.3 send username+password
		// 4. send SOCKS command message

		// to avoid unnecessary copying of the handler,
		// store it in a shaed_ptr
		boost::shared_ptr<handler_type> h(new handler_type(handler));

		ADD_OUTSTANDING_ASYNC("socks5_stream::name_lookup");
		tcp::resolver::query q(m_hostname, to_string(m_port).elems);
		m_resolver.async_resolve(q, boost::bind(
			&socks5_stream::name_lookup, this, _1, _2, h));
	}
Пример #15
0
  void async_connect(implementation_type& impl,
      const endpoint_type& peer_endpoint, Handler& handler)
  {
    // Allocate and construct an operation to wrap the handler.
    typedef win_iocp_socket_connect_op<Handler> op;
    typename op::ptr p = { asio::detail::addressof(handler),
      op::ptr::allocate(handler), 0 };
    p.p = new (p.v) op(impl.socket_, handler);

    ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
          &impl, impl.socket_, "async_connect"));

    start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(),
        peer_endpoint.data(), static_cast<int>(peer_endpoint.size()), p.p);
    p.v = p.p = 0;
  }
Пример #16
0
    boost::system::error_code
    bind
        ( endpoint_type const& e )
    {
        // Only fixed port is handled right now.
        if ( e.port() != FIXED_PORT )
            return make_error_code( boost::system::errc::invalid_argument );

        // Generate our local address.
        if ( e.address().is_v4() )
            local_endpoint( generate_unique_ipv4_endpoint( e.port() ) );
        else
            local_endpoint( generate_unique_ipv6_endpoint( e.port() ) );

        add_route_to_socket( local_endpoint(), this );
        return boost::system::error_code();
    }
  size_t receive_from(implementation_type& impl,
      const MutableBufferSequence& buffers,
      endpoint_type& sender_endpoint, socket_base::message_flags flags,
      asio::error_code& ec)
  {
    buffer_sequence_adapter<asio::mutable_buffer,
        MutableBufferSequence> bufs(buffers);

    std::size_t addr_len = sender_endpoint.capacity();
    std::size_t bytes_recvd = socket_ops::sync_recvfrom(
        impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
        flags, sender_endpoint.data(), &addr_len, ec);

    if (!ec)
      sender_endpoint.resize(addr_len);

    return bytes_recvd;
  }
  void async_connect(implementation_type& impl,
      const endpoint_type& peer_endpoint, Handler& handler)
  {
    bool is_continuation =
      asio_handler_cont_helpers::is_continuation(handler);

    // Allocate and construct an operation to wrap the handler.
    typedef reactive_socket_connect_op<Handler> op;
    typename op::ptr p = { asio::detail::addressof(handler),
      op::ptr::allocate(handler), 0 };
    p.p = new (p.v) op(impl.socket_, handler);

    ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect"));

    start_connect_op(impl, p.p, is_continuation,
        peer_endpoint.data(), peer_endpoint.size());
    p.v = p.p = 0;
  }
Пример #19
0
 /**
  * This function is used to connect a socket to the specified remote endpoint.
  * The function call will block until the connection is successfully made or
  * an error occurs.
  *
  * The socket is automatically opened if it is not already open. If the
  * connect fails, and the socket was automatically opened, the socket is
  * not returned to the closed state.
  *
  * @param peer_endpoint The remote endpoint to which the socket will be
  * connected.
  *
  * @throws boost::system::system_error Thrown on failure.
  *
  * @par Example
  * @code
  * boost::asio::ip::tcp::socket socket(io_service);
  * boost::asio::ip::tcp::endpoint endpoint(
  *     boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  * socket.connect(endpoint);
  * @endcode
  */
 void connect(const endpoint_type& peer_endpoint)
 {
   boost::system::error_code ec;
   if (!is_open())
   {
     this->service.open(this->implementation, peer_endpoint.protocol(), ec);
     boost::asio::detail::throw_error(ec);
   }
   this->service.connect(this->implementation, peer_endpoint, ec);
   boost::asio::detail::throw_error(ec);
 }
Пример #20
0
 /**
  * This function is used to connect a socket to the specified remote endpoint.
  * The function call will block until the connection is successfully made or
  * an error occurs.
  *
  * The socket is automatically opened if it is not already open. If the
  * connect fails, and the socket was automatically opened, the socket is
  * not returned to the closed state.
  *
  * @param peer_endpoint The remote endpoint to which the socket will be
  * connected.
  *
  * @throws asio::system_error Thrown on failure.
  *
  * @par Example
  * @code
  * asio::ip::tcp::socket socket(io_service);
  * asio::ip::tcp::endpoint endpoint(
  *     asio::ip::address::from_string("1.2.3.4"), 12345);
  * socket.connect(endpoint);
  * @endcode
  */
 void connect(const endpoint_type& peer_endpoint)
 {
   asio::error_code ec;
   if (!is_open())
   {
     this->get_service().open(this->get_implementation(),
         peer_endpoint.protocol(), ec);
     asio::detail::throw_error(ec, "connect");
   }
   this->get_service().connect(this->get_implementation(), peer_endpoint, ec);
   asio::detail::throw_error(ec, "connect");
 }
Пример #21
0
  void async_send_to(implementation_type& impl,
      const ConstBufferSequence& buffers, const endpoint_type& destination,
      socket_base::message_flags flags, Handler& handler)
  {
    // Allocate and construct an operation to wrap the handler.
    typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
    typename op::ptr p = { asio::detail::addressof(handler),
      op::ptr::allocate(handler), 0 };
    p.p = new (p.v) op(impl.cancel_token_, buffers, handler);

    ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
          &impl, impl.socket_, "async_send_to"));

    buffer_sequence_adapter<asio::const_buffer,
        ConstBufferSequence> bufs(buffers);

    start_send_to_op(impl, bufs.buffers(), bufs.count(),
        destination.data(), static_cast<int>(destination.size()),
        flags, p.p);
    p.v = p.p = 0;
  }
  /// Remove an endpoint to the set used by the acceptor.
  boost::system::error_code bind_remove(implementation_type& impl,
      const endpoint_type& endpoint, boost::system::error_code& ec)
  {
    if (!is_open(impl))
    {
      ec = boost::asio::error::bad_descriptor;
      return ec;
    }

    boost::asio_sctp::detail::sctp_socket_ops::bind_remove(native(impl), endpoint.data(), endpoint.size(), ec);
    return ec;
  }
Пример #23
0
  /**
   * This function is used to connect a socket to the specified remote endpoint.
   * The function call will block until the connection is successfully made or
   * an error occurs.
   *
   * The socket is automatically opened if it is not already open. If the
   * connect fails, and the socket was automatically opened, the socket is
   * not returned to the closed state.
   *
   * @param peer_endpoint The remote endpoint to which the socket will be
   * connected.
   *
   * @param ec Set to indicate what error occurred, if any.
   *
   * @par Example
   * @code
   * boost::asio::ip::tcp::socket socket(io_service);
   * boost::asio::ip::tcp::endpoint endpoint(
   *     boost::asio::ip::address::from_string("1.2.3.4"), 12345);
   * boost::system::error_code ec;
   * socket.connect(endpoint, ec);
   * if (ec)
   * {
   *   // An error occurred.
   * }
   * @endcode
   */
  boost::system::error_code connect(const endpoint_type& peer_endpoint,
      boost::system::error_code& ec)
  {
    if (!is_open())
    {
      if (this->service.open(this->implementation,
            peer_endpoint.protocol(), ec))
      {
        return ec;
      }
    }

    return this->service.connect(this->implementation, peer_endpoint, ec);
  }
Пример #24
0
  /**
   * This function is used to connect a socket to the specified remote endpoint.
   * The function call will block until the connection is successfully made or
   * an error occurs.
   *
   * The socket is automatically opened if it is not already open. If the
   * connect fails, and the socket was automatically opened, the socket is
   * not returned to the closed state.
   *
   * @param peer_endpoint The remote endpoint to which the socket will be
   * connected.
   *
   * @param ec Set to indicate what error occurred, if any.
   *
   * @par Example
   * @code
   * asio::ip::tcp::socket socket(io_service);
   * asio::ip::tcp::endpoint endpoint(
   *     asio::ip::address::from_string("1.2.3.4"), 12345);
   * asio::error_code ec;
   * socket.connect(endpoint, ec);
   * if (ec)
   * {
   *   // An error occurred.
   * }
   * @endcode
   */
  asio::error_code connect(const endpoint_type& peer_endpoint,
      asio::error_code& ec)
  {
    if (!is_open())
    {
      if (this->get_service().open(this->get_implementation(),
            peer_endpoint.protocol(), ec))
      {
        return ec;
      }
    }

    return this->get_service().connect(
        this->get_implementation(), peer_endpoint, ec);
  }
Пример #25
0
 http_sync_server(endpoint_type const& ep,
         std::string const& root)
     : sock_(ios_)
     , acceptor_(ios_)
     , root_(root)
 {
     acceptor_.open(ep.protocol());
     acceptor_.bind(ep);
     acceptor_.listen(
         boost::asio::socket_base::max_connections);
     acceptor_.async_accept(sock_,
         std::bind(&http_sync_server::on_accept, this,
             beast::asio::placeholders::error));
     thread_ = std::thread{[&]{ ios_.run(); }};
 }
Пример #26
0
  void async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler)
  {
    if (!is_open())
    {
      boost::system::error_code ec;
      if (this->service.open(this->implementation,
            peer_endpoint.protocol(), ec))
      {
        this->get_io_service().post(
            boost::asio::detail::bind_handler(handler, ec));
        return;
      }
    }

    this->service.async_connect(this->implementation, peer_endpoint, handler);
  }
Пример #27
0
	void async_connect(endpoint_type const& endpoint, Handler const& handler)
	{
		if (!endpoint.address().is_v4())
		{
			m_io_service.post(boost::bind<void>(handler, boost::asio::error::operation_not_supported, 0));
			return;
		}

		if (m_impl == 0)
		{
			m_io_service.post(boost::bind<void>(handler, boost::asio::error::not_connected, 0));
			return;
		}

		m_connect_handler = handler;
		do_connect(endpoint);
	}
  void async_connect(implementation_type& impl,
      const endpoint_type& peer_endpoint, Handler& handler)
  {
    bool is_continuation =
      networking_ts_handler_cont_helpers::is_continuation(handler);

    // Allocate and construct an operation to wrap the handler.
    typedef winrt_socket_connect_op<Handler> op;
    typename op::ptr p = { std::experimental::net::detail::addressof(handler),
      op::ptr::allocate(handler), 0 };
    p.p = new (p.v) op(handler);

    NET_TS_HANDLER_CREATION((io_context_.context(),
          *p.p, "socket", &impl, 0, "async_connect"));

    start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
    p.v = p.p = 0;
  }
Пример #29
0
  void async_connect(implementation_type& impl,
      const endpoint_type& peer_endpoint, Handler& handler)
  {
    bool is_continuation =
      boost_asio_handler_cont_helpers::is_continuation(handler);

    // Allocate and construct an operation to wrap the handler.
    typedef winrt_socket_connect_op<Handler> op;
    typename op::ptr p = { boost::asio::detail::addressof(handler),
      boost_asio_handler_alloc_helpers::allocate(
        sizeof(op), handler), 0 };
    p.p = new (p.v) op(handler);

    BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect"));

    start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
    p.v = p.p = 0;
  }
Пример #30
0
 /**
  * This constructor creates an acceptor and automatically opens it to listen
  * for new connections on the specified endpoint.
  *
  * @param io_service The io_service object that the acceptor will use to
  * dispatch handlers for any asynchronous operations performed on the
  * acceptor.
  *
  * @param endpoint An endpoint on the local machine on which the acceptor
  * will listen for new connections.
  *
  * @param reuse_addr Whether the constructor should set the socket option
  * socket_base::reuse_address.
  *
  * @throws asio::system_error Thrown on failure.
  *
  * @note This constructor is equivalent to the following code:
  * @code
  * basic_socket_acceptor<Protocol> acceptor(io_service);
  * acceptor.open(endpoint.protocol());
  * if (reuse_addr)
  *   acceptor.set_option(socket_base::reuse_address(true));
  * acceptor.bind(endpoint);
  * acceptor.listen(listen_backlog);
  * @endcode
  */
 basic_socket_acceptor(asio::io_service& io_service,
     const endpoint_type& endpoint, bool reuse_addr = true)
   : basic_io_object<SocketAcceptorService>(io_service)
 {
   asio::error_code ec;
   this->service.open(this->implementation, endpoint.protocol(), ec);
   asio::detail::throw_error(ec);
   if (reuse_addr)
   {
     this->service.set_option(this->implementation,
         socket_base::reuse_address(true), ec);
     asio::detail::throw_error(ec);
   }
   this->service.bind(this->implementation, endpoint, ec);
   asio::detail::throw_error(ec);
   this->service.listen(this->implementation,
       socket_base::max_connections, ec);
   asio::detail::throw_error(ec);
 }