Skip to content

Commit

Permalink
#26 Fix client on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
securesocketfunneling committed Sep 13, 2016
1 parent 7a82a45 commit cde82c8
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/core/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class SSFClient
BaseUserServicePtr p_user_service, boost::system::error_code ec);

private:
NetworkSocketPtr p_socket_;

AsyncEngine async_engine_;

NetworkSocketPtr p_socket_;

Demux fiber_demux_;

std::vector<BaseUserServicePtr> user_services_;
Expand Down
16 changes: 6 additions & 10 deletions src/core/client/client.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ SSFClient<N, T>::SSFClient(std::vector<BaseUserServicePtr> user_services,
ClientCallback callback)
: T<typename N::socket>(
boost::bind(&SSFClient<N, T>::DoSSFStart, this, _1, _2)),
p_socket_(nullptr),
async_engine_(),
p_socket_(nullptr),
fiber_demux_(async_engine_.get_io_service()),
user_services_(user_services),
services_config_(services_config),
Expand Down Expand Up @@ -77,15 +77,16 @@ void SSFClient<N, T>::Stop() {
return;
}

fiber_demux_.close();

if (p_socket_.get() != nullptr) {
boost::system::error_code close_ec;
p_socket_->shutdown(boost::asio::socket_base::shutdown_both, close_ec);
p_socket_->close(close_ec);
p_socket_.reset();
}

fiber_demux_.close();

async_engine_.Stop();
p_socket_.reset();
}

template <class N, template <class> class T>
Expand All @@ -96,18 +97,13 @@ boost::asio::io_service& SSFClient<N, T>::get_io_service() {
template <class N, template <class> class T>
void SSFClient<N, T>::NetworkToTransport(const boost::system::error_code& ec) {
if (!ec) {
this->DoSSFInitiate(std::move(p_socket_));
this->DoSSFInitiate(p_socket_);
return;
}

SSF_LOG(kLogError) << "client: error when connecting to server: "
<< ec.message();

if (p_socket_) {
boost::system::error_code close_ec;
p_socket_->close(close_ec);
}

Notify(ssf::services::initialisation::NETWORK, nullptr, ec);
}

Expand Down
23 changes: 23 additions & 0 deletions src/framework/ssf/layer/data_link/basic_circuit_socket_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class basic_CircuitSocket_service
boost::system::error_code open(implementation_type& impl,
const protocol_type& protocol,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

return impl.p_next_layer_socket->open(typename protocol_type::next_layer_protocol(),
ec);
}
Expand All @@ -87,6 +93,10 @@ class basic_CircuitSocket_service
}

bool is_open(const implementation_type& impl) const {
if (!impl.p_next_layer_socket) {
return false;
}

return impl.p_next_layer_socket->is_open();
}

Expand Down Expand Up @@ -114,6 +124,12 @@ class basic_CircuitSocket_service

boost::system::error_code close(implementation_type& impl,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

return impl.p_next_layer_socket->close(ec);
}

Expand All @@ -123,6 +139,11 @@ class basic_CircuitSocket_service

bool at_mark(const implementation_type& impl,
boost::system::error_code& ec) const {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return false;
}
return impl.p_next_layer_socket->at_mark(ec);
}

Expand All @@ -140,6 +161,8 @@ class basic_CircuitSocket_service
boost::system::error_code cancel(implementation_type& impl,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

Expand Down
52 changes: 46 additions & 6 deletions src/framework/ssf/layer/proxy/basic_proxy_acceptor_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base<
}

bool is_open(const implementation_type& impl) const {
if (!impl.p_next_layer_acceptor) {
return false;
}

return impl.p_next_layer_acceptor->is_open();
}

Expand All @@ -113,6 +117,12 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base<

boost::system::error_code close(implementation_type& impl,
boost::system::error_code& ec) {
if (!impl.p_next_layer_acceptor) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

return impl.p_next_layer_acceptor->close(ec);
}

Expand All @@ -124,22 +134,38 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base<
template <typename SettableSocketOption>
boost::system::error_code set_option(implementation_type& impl,
const SettableSocketOption& option,
boost::system::error_code& ec) {
if (impl.p_next_layer_acceptor) {
return impl.p_next_layer_acceptor->set_option(option, ec);
boost::system::error_code& ec) {
if (!impl.p_next_layer_acceptor) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}
return ec;

return impl.p_next_layer_acceptor->set_option(option, ec);

}

boost::system::error_code bind(implementation_type& impl,
const endpoint_type& endpoint,
boost::system::error_code& ec) {
if (!impl.p_next_layer_acceptor) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

impl.p_local_endpoint = std::make_shared<endpoint_type>(endpoint);
return impl.p_next_layer_acceptor->bind(endpoint.next_layer_endpoint(), ec);
}

boost::system::error_code listen(implementation_type& impl, int backlog,
boost::system::error_code& ec) {
boost::system::error_code& ec) {
if (!impl.p_next_layer_acceptor) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

return impl.p_next_layer_acceptor->listen(backlog, ec);
}

Expand All @@ -149,7 +175,13 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base<
boost::asio::basic_socket<Protocol1, SocketService>& peer,
endpoint_type* p_peer_endpoint, boost::system::error_code& ec,
typename std::enable_if<boost::thread_detail::is_convertible<
protocol_type, Protocol1>::value>::type* = 0) {
protocol_type, Protocol1>::value>::type* = 0) {
if (!impl.p_next_layer_acceptor) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

auto& peer_impl = peer.native_handle();
peer_impl.p_remote_endpoint =
std::make_shared<typename Protocol1::endpoint>();
Expand Down Expand Up @@ -183,6 +215,14 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base<
void(boost::system::error_code)>
init(std::forward<AcceptHandler>(handler));

if (!impl.p_next_layer_acceptor) {
io::PostHandler(
this->get_io_service(), init.handler,
boost::system::error_code(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category()));
return init.result.get();
}

auto& peer_impl = peer.native_handle();
peer_impl.p_local_endpoint =
std::make_shared<typename Protocol1::endpoint>();
Expand Down
70 changes: 66 additions & 4 deletions src/framework/ssf/layer/proxy/basic_proxy_socket_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base<
}

bool is_open(const implementation_type& impl) const {
if (!impl.p_next_layer_socket) {
return false;
}

return impl.p_next_layer_socket->is_open();
}

Expand Down Expand Up @@ -113,6 +117,11 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base<

boost::system::error_code close(implementation_type& impl,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::broken_pipe, ssf::error::get_ssf_category());
return ec;
}

return impl.p_next_layer_socket->close(ec);
}

Expand All @@ -122,11 +131,22 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base<

boost::system::error_code cancel(implementation_type& impl,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return ec;
}

return impl.p_next_layer_socket->cancel(ec);
}

bool at_mark(const implementation_type& impl,
boost::system::error_code& ec) const {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return false;
}
return impl.p_next_layer_socket->at_mark(ec);
}

Expand Down Expand Up @@ -187,6 +207,12 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base<
const ConstBufferSequence& buffers,
boost::asio::socket_base::message_flags flags,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::bad_file_descriptor,
ssf::error::get_ssf_category());
return 0;
}

return impl.p_next_layer_socket->send(buffers, flags, ec);
}

Expand All @@ -196,15 +222,33 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base<
async_send(implementation_type& impl, const ConstBufferSequence& buffers,
boost::asio::socket_base::message_flags flags,
WriteHandler&& handler) {
return impl.p_next_layer_socket->async_send(
buffers, std::forward<WriteHandler>(handler));
boost::asio::detail::async_result_init<
WriteHandler, void(boost::system::error_code, std::size_t)>
init(std::forward<WriteHandler>(handler));

if (!impl.p_next_layer_socket) {
io::PostHandler(this->get_io_service(), init.handler,
boost::system::error_code(ssf::error::broken_pipe,
ssf::error::get_ssf_category()),
0);
return init.result.get();
}

impl.p_next_layer_socket->async_send(buffers, init.handler);

return init.result.get();
}

template <typename MutableBufferSequence>
std::size_t receive(implementation_type& impl,
const MutableBufferSequence& buffers,
boost::asio::socket_base::message_flags flags,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::broken_pipe, ssf::error::get_ssf_category());
return 0;
}

return impl.p_next_layer_socket->receive(buffers, flags, ec);
}

Expand All @@ -215,13 +259,31 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base<
const MutableBufferSequence& buffers,
boost::asio::socket_base::message_flags flags,
ReadHandler&& handler) {
return impl.p_next_layer_socket->async_receive(
buffers, std::forward<ReadHandler>(handler));
boost::asio::detail::async_result_init<
ReadHandler, void(boost::system::error_code, std::size_t)>
init(std::forward<ReadHandler>(handler));

if (!impl.p_next_layer_socket) {
io::PostHandler(this->get_io_service(), init.handler,
boost::system::error_code(ssf::error::broken_pipe,
ssf::error::get_ssf_category()),
0);
return init.result.get();
}

impl.p_next_layer_socket->async_receive(buffers, init.handler);

return init.result.get();
}

boost::system::error_code shutdown(
implementation_type& impl, boost::asio::socket_base::shutdown_type what,
boost::system::error_code& ec) {
if (!impl.p_next_layer_socket) {
ec.assign(ssf::error::broken_pipe, ssf::error::get_ssf_category());
return ec;
}

impl.p_next_layer_socket->shutdown(what, ec);

return ec;
Expand Down

0 comments on commit cde82c8

Please sign in to comment.