2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
#pragma once
|
2017-09-30 08:04:41 +02:00
|
|
|
#define HAVE_IRCD_NET_SOCKET_H
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// This file is not included with the IRCd standard include stack because
|
|
|
|
// it requires symbols we can't forward declare without boost headers. It
|
|
|
|
// is part of the <ircd/asio.h> stack which can be included in your
|
2018-01-06 02:01:37 +01:00
|
|
|
// definition file if you need low level access to this socket API.
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
namespace ircd::net
|
2017-08-28 23:51:22 +02:00
|
|
|
{
|
2018-01-05 08:14:21 +01:00
|
|
|
extern asio::ssl::context sslv23_client;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2018-01-22 09:31:53 +01:00
|
|
|
/// Internal socket interface
|
|
|
|
///
|
2017-09-30 08:04:41 +02:00
|
|
|
struct ircd::net::socket
|
|
|
|
:std::enable_shared_from_this<ircd::net::socket>
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-10-19 12:55:24 +02:00
|
|
|
struct io;
|
2016-11-29 16:23:38 +01:00
|
|
|
struct stat;
|
2018-01-01 10:42:00 +01:00
|
|
|
struct xfer;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-01 10:42:00 +01:00
|
|
|
using endpoint = ip::tcp::endpoint;
|
|
|
|
using wait_type = ip::tcp::socket::wait_type;
|
|
|
|
using message_flags = asio::socket_base::message_flags;
|
|
|
|
using handshake_type = asio::ssl::stream<ip::tcp::socket>::handshake_type;
|
2018-01-08 12:03:50 +01:00
|
|
|
using ec_handler = std::function<void (const error_code &)>;
|
|
|
|
using eptr_handler = std::function<void (std::exception_ptr)>;
|
2018-01-01 10:42:00 +01:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
struct stat
|
|
|
|
{
|
|
|
|
size_t bytes {0};
|
|
|
|
size_t calls {0};
|
|
|
|
};
|
|
|
|
|
2018-03-13 02:45:21 +01:00
|
|
|
static uint64_t count; // monotonic
|
|
|
|
static uint64_t instances; // current socket count
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
ip::tcp::socket sd;
|
|
|
|
asio::ssl::stream<ip::tcp::socket &> ssl;
|
2016-11-29 16:23:38 +01:00
|
|
|
stat in, out;
|
2018-04-16 00:55:25 +02:00
|
|
|
steady_timer timer;
|
|
|
|
uint64_t timer_sem[2] {0}; // handler, sender
|
|
|
|
bool timer_set {false}; // boolean lockout
|
2017-11-01 23:51:24 +01:00
|
|
|
bool timedout {false};
|
2018-03-10 00:55:15 +01:00
|
|
|
bool fini {false};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
void call_user(const eptr_handler &, const error_code &) noexcept;
|
|
|
|
void call_user(const ec_handler &, const error_code &) noexcept;
|
|
|
|
bool handle_verify(bool, asio::ssl::verify_context &, const open_opts &) noexcept;
|
2018-01-16 04:46:23 +01:00
|
|
|
void handle_disconnect(std::shared_ptr<socket>, eptr_handler, error_code) noexcept;
|
2018-01-20 12:14:14 +01:00
|
|
|
void handle_handshake(std::weak_ptr<socket>, eptr_handler, error_code) noexcept;
|
|
|
|
void handle_connect(std::weak_ptr<socket>, open_opts, eptr_handler, error_code) noexcept;
|
|
|
|
void handle_timeout(std::weak_ptr<socket>, ec_handler, error_code) noexcept;
|
2018-03-11 19:23:50 +01:00
|
|
|
void handle_ready(std::weak_ptr<socket>, ready, ec_handler, error_code, size_t) noexcept;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
operator const ip::tcp::socket &() const { return sd; }
|
|
|
|
operator ip::tcp::socket &() { return sd; }
|
2017-11-16 02:27:36 +01:00
|
|
|
operator const SSL &() const;
|
|
|
|
operator SSL &();
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-01 10:42:00 +01:00
|
|
|
endpoint remote() const; // getpeername(); throws if not conn
|
|
|
|
endpoint local() const; // getsockname(); throws if not conn/bound
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// Timer for this socket
|
2018-01-07 06:34:02 +01:00
|
|
|
void set_timeout(const milliseconds &, ec_handler);
|
2017-09-12 18:28:41 +02:00
|
|
|
void set_timeout(const milliseconds &);
|
2018-01-04 23:20:30 +01:00
|
|
|
milliseconds cancel_timeout() noexcept;
|
2017-09-12 18:28:41 +02:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
// low level write suite
|
2018-01-08 12:04:15 +01:00
|
|
|
template<class iov> size_t write_one(iov&&); // non-blocking
|
|
|
|
template<class iov> size_t write_any(iov&&); // non-blocking
|
2018-01-14 10:46:50 +01:00
|
|
|
template<class iov> size_t write_few(iov&&); // yielding
|
2018-01-08 12:04:15 +01:00
|
|
|
template<class iov> size_t write_all(iov&&); // yielding
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
// low level read suite
|
2018-01-08 12:04:15 +01:00
|
|
|
template<class iov> size_t read_one(iov&&); // non-blocking
|
2018-01-14 10:46:50 +01:00
|
|
|
template<class iov> size_t read_any(iov&&); // non-blocking
|
|
|
|
template<class iov> size_t read_few(iov&&); // yielding
|
2018-01-08 12:04:15 +01:00
|
|
|
template<class iov> size_t read_all(iov&&); // yielding
|
2018-01-07 06:34:02 +01:00
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
// low level wait suite
|
|
|
|
void wait(const wait_opts &);
|
|
|
|
void wait(const wait_opts &, wait_callback_ec);
|
|
|
|
void wait(const wait_opts &, wait_callback_eptr);
|
2018-01-12 03:39:24 +01:00
|
|
|
void cancel() noexcept;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
// Alias to wait()
|
|
|
|
template<class... args> auto operator()(args&&...);
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
void disconnect(const close_opts &, eptr_handler);
|
|
|
|
void handshake(const open_opts &, eptr_handler);
|
|
|
|
void connect(const endpoint &, const open_opts &, eptr_handler);
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
socket(asio::ssl::context &ssl = sslv23_client,
|
|
|
|
boost::asio::io_service *const &ios = ircd::ios);
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// Socket cannot be copied or moved; must be constructed as shared ptr
|
2017-08-23 22:47:15 +02:00
|
|
|
socket(socket &&) = delete;
|
|
|
|
socket(const socket &) = delete;
|
2018-04-12 21:52:14 +02:00
|
|
|
socket &operator=(socket &&) = delete;
|
|
|
|
socket &operator=(const socket &) = delete;
|
2016-11-29 16:23:38 +01:00
|
|
|
~socket() noexcept;
|
|
|
|
};
|
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
template<class... args>
|
|
|
|
auto
|
|
|
|
ircd::net::socket::operator()(args&&... a)
|
|
|
|
{
|
|
|
|
return this->wait(std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Yields ircd::ctx until buffers are full.
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov>
|
2018-01-07 06:34:02 +01:00
|
|
|
size_t
|
|
|
|
ircd::net::socket::read_all(iov&& bufs)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
static const auto completion
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
asio::transfer_all()
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-04-16 01:12:50 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &) noexcept
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
const size_t ret
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2018-04-16 01:12:50 +02:00
|
|
|
asio::async_read(ssl, std::forward<iov>(bufs), completion, yield_context
|
|
|
|
{
|
|
|
|
to_asio{interruption}
|
|
|
|
})
|
2018-01-07 06:34:02 +01:00
|
|
|
};
|
2017-10-19 12:55:24 +02:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
if(!ret)
|
|
|
|
throw boost::system::system_error
|
|
|
|
{
|
|
|
|
boost::asio::error::eof
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
in.bytes += ret;
|
|
|
|
++in.calls;
|
|
|
|
return ret;
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Yields ircd::ctx until remote has sent at least some data.
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov>
|
2018-01-07 06:34:02 +01:00
|
|
|
size_t
|
2018-01-14 10:46:50 +01:00
|
|
|
ircd::net::socket::read_few(iov&& bufs)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-04-16 01:12:50 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &) noexcept
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
const size_t ret
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-04-16 01:12:50 +02:00
|
|
|
ssl.async_read_some(std::forward<iov>(bufs), yield_context
|
|
|
|
{
|
|
|
|
to_asio{interruption}
|
|
|
|
})
|
2018-01-07 06:34:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw boost::system::system_error
|
2018-01-01 10:42:00 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
boost::asio::error::eof
|
2018-01-01 10:42:00 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
in.bytes += ret;
|
2018-01-08 12:04:15 +01:00
|
|
|
++in.calls;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-14 10:46:50 +01:00
|
|
|
/// Non-blocking; as much as possible without blocking
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::read_any(iov&& bufs)
|
|
|
|
{
|
|
|
|
assert(!blocking(*this));
|
|
|
|
static const auto completion
|
|
|
|
{
|
|
|
|
asio::transfer_all()
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
asio::read(ssl, std::forward<iov>(bufs), completion)
|
|
|
|
};
|
|
|
|
|
|
|
|
in.bytes += ret;
|
|
|
|
++in.calls;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:04:15 +01:00
|
|
|
/// Non-blocking; One system call only; never throws eof;
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::read_one(iov&& bufs)
|
|
|
|
{
|
|
|
|
assert(!blocking(*this));
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
ssl.read_some(std::forward<iov>(bufs))
|
|
|
|
};
|
|
|
|
|
|
|
|
in.bytes += ret;
|
2018-01-07 06:34:02 +01:00
|
|
|
++in.calls;
|
|
|
|
return ret;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Yields ircd::ctx until all buffers are sent.
|
2017-10-19 12:55:24 +02:00
|
|
|
template<class iov>
|
2018-01-07 06:34:02 +01:00
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_all(iov&& bufs)
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
static const auto completion
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
asio::transfer_all()
|
|
|
|
};
|
|
|
|
|
2018-04-16 01:12:50 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &) noexcept
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
const size_t ret
|
|
|
|
{
|
2018-04-16 01:12:50 +02:00
|
|
|
asio::async_write(ssl, std::forward<iov>(bufs), completion, yield_context
|
|
|
|
{
|
|
|
|
to_asio{interruption}
|
|
|
|
})
|
2018-01-07 06:34:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
out.bytes += ret;
|
|
|
|
++out.calls;
|
|
|
|
return ret;
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 10:46:50 +01:00
|
|
|
/// Yields ircd::ctx until one or more bytes are sent.
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_few(iov&& bufs)
|
|
|
|
{
|
2018-04-16 01:12:50 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &) noexcept
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
2018-01-14 10:46:50 +01:00
|
|
|
const size_t ret
|
|
|
|
{
|
2018-04-16 01:12:50 +02:00
|
|
|
ssl.async_write_some(std::forward<iov>(bufs), yield_context
|
|
|
|
{
|
|
|
|
to_asio{interruption}
|
|
|
|
})
|
2018-01-14 10:46:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
out.bytes += ret;
|
|
|
|
++out.calls;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Non-blocking; writes as much as possible without blocking
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov>
|
2018-01-07 06:34:02 +01:00
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_any(iov&& bufs)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
static const auto completion
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
asio::transfer_all()
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
assert(!blocking(*this));
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
asio::write(ssl, std::forward<iov>(bufs), completion)
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
out.bytes += ret;
|
|
|
|
++out.calls;
|
|
|
|
return ret;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2017-10-19 12:55:24 +02:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Non-blocking; Writes one "unit" of data or less; never more.
|
2017-10-19 12:55:24 +02:00
|
|
|
template<class iov>
|
2018-01-07 06:34:02 +01:00
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_one(iov&& bufs)
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
assert(!blocking(*this));
|
|
|
|
const size_t ret
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
ssl.write_some(std::forward<iov>(bufs))
|
|
|
|
};
|
|
|
|
|
|
|
|
out.bytes += ret;
|
|
|
|
++out.calls;
|
|
|
|
return ret;
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|