2016-11-29 16:23:38 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
// definition file if you need low level access to this socket API. The
|
|
|
|
// client.h still offers higher level access to sockets without requiring
|
|
|
|
// boost headers; please check that for satisfaction before including this.
|
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
|
|
|
{
|
|
|
|
using asio::steady_timer;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct socket;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
extern asio::ssl::context sslv23_client;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
std::shared_ptr<socket> connect(const ip::tcp::endpoint &remote, const milliseconds &timeout);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-03-13 22:07:58 +01:00
|
|
|
|
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;
|
2017-03-10 22:23:01 +01:00
|
|
|
struct scope_timeout;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
struct stat
|
|
|
|
{
|
|
|
|
size_t bytes {0};
|
|
|
|
size_t calls {0};
|
|
|
|
};
|
|
|
|
|
|
|
|
using message_flags = boost::asio::socket_base::message_flags;
|
|
|
|
using handshake_type = asio::ssl::stream<ip::tcp::socket>::handshake_type;
|
|
|
|
using handler = std::function<void (const error_code &) noexcept>;
|
2017-10-19 12:55:24 +02:00
|
|
|
using xfer_handler = std::function<void (const error_code &, const size_t &) noexcept>;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
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
|
|
|
steady_timer timer;
|
|
|
|
stat in, out;
|
2017-11-01 23:51:24 +01:00
|
|
|
bool timedout {false};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 22:47:15 +02:00
|
|
|
void call_user(const handler &, const error_code &) noexcept;
|
|
|
|
bool handle_error(const error_code &ec);
|
2017-10-19 10:01:07 +02:00
|
|
|
void handle_timeout(std::weak_ptr<socket> wp, const error_code &ec) noexcept;
|
2017-08-23 22:47:15 +02:00
|
|
|
void handle(std::weak_ptr<socket>, handler, const error_code &, const 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
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
ip::tcp::endpoint remote() const; // getpeername(); throws if not conn
|
|
|
|
ip::tcp::endpoint local() const; // getsockname(); throws if not conn/bound
|
2017-10-19 12:55:24 +02:00
|
|
|
bool connected() const noexcept; // false on any sock errs
|
|
|
|
size_t available() const; // throws on errors; use friend variant for noex..
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// low level read suite
|
|
|
|
template<class iov> auto read_some(const iov &, xfer_handler);
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov> auto read_some(const iov &, error_code &);
|
|
|
|
template<class iov> auto read_some(const iov &);
|
2017-10-19 12:55:24 +02:00
|
|
|
template<class iov> auto read(const iov &, xfer_handler);
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov> auto read(const iov &, error_code &);
|
|
|
|
template<class iov> auto read(const iov &);
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// low level write suite
|
|
|
|
template<class iov> auto write_some(const iov &, xfer_handler);
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov> auto write_some(const iov &, error_code &);
|
|
|
|
template<class iov> auto write_some(const iov &);
|
2017-10-19 12:55:24 +02:00
|
|
|
template<class iov> auto write(const iov &, xfer_handler);
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov> auto write(const iov &, error_code &);
|
|
|
|
template<class iov> auto write(const iov &);
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// Timer for this socket
|
2017-09-12 18:28:41 +02:00
|
|
|
void set_timeout(const milliseconds &, handler);
|
|
|
|
void set_timeout(const milliseconds &);
|
2017-10-19 12:55:24 +02:00
|
|
|
error_code cancel_timeout() noexcept;
|
2017-09-12 18:28:41 +02:00
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// Asynchronous callback when socket ready for read
|
2016-11-29 16:23:38 +01:00
|
|
|
void operator()(const milliseconds &timeout, handler);
|
|
|
|
void operator()(handler);
|
2017-10-19 10:01:07 +02:00
|
|
|
bool cancel() noexcept;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
// Connect to host; synchronous (yield) and asynchronous (callback) variants
|
|
|
|
void connect(const ip::tcp::endpoint &ep, const milliseconds &timeout, handler callback);
|
2017-10-25 18:37:37 +02:00
|
|
|
void connect(const ip::tcp::endpoint &ep, const milliseconds &timeout = 30000ms);
|
|
|
|
void connect(const net::remote &, const milliseconds &timeout = 30000ms);
|
2017-10-27 00:36:31 +02:00
|
|
|
bool disconnect(const dc &type);
|
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;
|
2016-11-29 16:23:38 +01:00
|
|
|
~socket() noexcept;
|
|
|
|
};
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
class ircd::net::socket::scope_timeout
|
2017-03-10 22:23:01 +01:00
|
|
|
{
|
2017-10-19 12:55:24 +02:00
|
|
|
socket *s {nullptr};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-10 22:23:01 +01:00
|
|
|
public:
|
2017-10-19 12:55:24 +02:00
|
|
|
bool cancel() noexcept; // invoke timer.cancel() before dtor
|
|
|
|
bool release(); // cancels the cancel;
|
|
|
|
|
|
|
|
scope_timeout(socket &, const milliseconds &timeout, socket::handler handler);
|
2017-03-10 22:23:01 +01:00
|
|
|
scope_timeout(socket &, const milliseconds &timeout);
|
2017-10-19 12:55:24 +02:00
|
|
|
scope_timeout() = default;
|
|
|
|
scope_timeout(scope_timeout &&) noexcept;
|
2017-03-10 22:23:01 +01:00
|
|
|
scope_timeout(const scope_timeout &) = delete;
|
2017-10-19 12:55:24 +02:00
|
|
|
scope_timeout &operator=(scope_timeout &&) noexcept;
|
2017-03-10 22:23:01 +01:00
|
|
|
scope_timeout &operator=(const scope_timeout &) = delete;
|
|
|
|
~scope_timeout() noexcept;
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
class ircd::net::socket::io
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
struct socket &sock;
|
|
|
|
struct stat &stat;
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
public:
|
|
|
|
operator size_t() const;
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
io(struct socket &, struct stat &, const size_t &bytes);
|
2016-11-29 16:23:38 +01:00
|
|
|
io(struct socket &, struct stat &, const std::function<size_t ()> &closure);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::write(const iov &bufs)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
return io(*this, out, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
return async_write(ssl, bufs, asio::transfer_all(), yield_context{to_asio{}});
|
2017-03-13 22:07:58 +01:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::write(const iov &bufs,
|
|
|
|
error_code &ec)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
return io(*this, out, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
return async_write(ssl, bufs, asio::transfer_all(), yield_context{to_asio{}}[ec]);
|
2017-03-13 22:07:58 +01:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
template<class iov>
|
|
|
|
auto
|
|
|
|
ircd::net::socket::write(const iov &bufs,
|
|
|
|
xfer_handler handler)
|
|
|
|
{
|
|
|
|
async_write(ssl, bufs, asio::transfer_all(), [this, handler(std::move(handler))]
|
|
|
|
(const error_code &ec, const size_t &bytes)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
io{*this, out, bytes};
|
|
|
|
handler(ec, bytes);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::write_some(const iov &bufs)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
return io(*this, out, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
return ssl.async_write_some(bufs, yield_context{to_asio{}});
|
2017-03-13 22:07:58 +01:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::write_some(const iov &bufs,
|
|
|
|
error_code &ec)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
return io(*this, out, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
return ssl.async_write_some(bufs, yield_context{to_asio{}}[ec]);
|
2017-03-13 22:07:58 +01:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
template<class iov>
|
|
|
|
auto
|
|
|
|
ircd::net::socket::write_some(const iov &bufs,
|
|
|
|
xfer_handler handler)
|
|
|
|
{
|
|
|
|
ssl.async_write_some(bufs, [this, handler(std::move(handler))]
|
|
|
|
(const error_code &ec, const size_t &bytes)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
io{*this, out, bytes};
|
|
|
|
handler(ec, bytes);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::read(const iov &bufs)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return io(*this, in, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
const size_t ret(async_read(ssl, bufs, yield_context{to_asio{}}));
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw boost::system::system_error(boost::asio::error::eof);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::read(const iov &bufs,
|
|
|
|
error_code &ec)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return io(*this, in, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
return async_read(ssl, bufs, yield_context{to_asio{}}[ec]);
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
template<class iov>
|
|
|
|
auto
|
|
|
|
ircd::net::socket::read(const iov &bufs,
|
|
|
|
xfer_handler handler)
|
|
|
|
{
|
|
|
|
async_read(ssl, bufs, [this, handler(std::move(handler))]
|
|
|
|
(const error_code &ec, const size_t &bytes)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
io{*this, in, bytes};
|
|
|
|
handler(ec, bytes);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::read_some(const iov &bufs)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return io(*this, in, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
const size_t ret(ssl.async_read_some(bufs, yield_context{to_asio{}}));
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw boost::system::system_error(boost::asio::error::eof);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class iov>
|
|
|
|
auto
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::read_some(const iov &bufs,
|
|
|
|
error_code &ec)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return io(*this, in, [&]
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
return ssl.async_read_some(bufs, yield_context{to_asio{}}[ec]);
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
|
|
|
}
|
2017-10-19 12:55:24 +02:00
|
|
|
|
|
|
|
template<class iov>
|
|
|
|
auto
|
|
|
|
ircd::net::socket::read_some(const iov &bufs,
|
|
|
|
xfer_handler handler)
|
|
|
|
{
|
|
|
|
ssl.async_read_some(bufs, [this, handler(std::move(handler))]
|
|
|
|
(const error_code &ec, const size_t &bytes)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
io{*this, in, bytes};
|
|
|
|
handler(ec, bytes);
|
|
|
|
});
|
|
|
|
}
|