0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd: Add net; various asio related.

This commit is contained in:
Jason Volk 2017-09-29 23:04:41 -07:00
parent 6e6d92a6ed
commit 84d7746165
12 changed files with 1198 additions and 1078 deletions

View file

@ -43,6 +43,19 @@
#include <boost/asio/spawn.hpp>
#include <boost/asio/io_service.hpp>
struct ircd::strand
:asio::io_service::strand
{
using handler = std::function<void ()>;
operator const asio::io_service &() const;
operator asio::io_service &();
strand(asio::io_service &ios)
:asio::io_service::strand{ios}
{}
};
///
/// The following IRCd headers are not included in the main stdinc.h list of
/// includes because they require boost directly or symbols which we cannot
@ -52,3 +65,16 @@
#include <ircd/ctx/continuation.h>
#include <ircd/socket.h>
inline ircd::strand::operator
asio::io_service &()
{
return get_io_service();
}
inline ircd::strand::operator
const asio::io_service &()
const
{
return const_cast<strand *>(this)->get_io_service();
}

View file

@ -29,18 +29,15 @@
namespace ircd
{
IRCD_EXCEPTION(ircd::error, client_error)
IRCD_EXCEPTION(client_error, broken_pipe)
IRCD_EXCEPTION(client_error, disconnected)
struct socket;
struct client;
const char *write(client &, const char *&start, const char *const &stop);
char *read(client &, char *&start, char *const &stop);
string_view readline(client &, char *&start, char *const &stop);
http::response::write_closure write_closure(client &);
parse::read_closure read_closure(client &);
std::shared_ptr<client> add_client(std::shared_ptr<socket>); // Creates a client.
}
@ -48,9 +45,8 @@ struct ircd::client
:std::enable_shared_from_this<client>
{
struct init;
using list = std::list<client *>;
using host_port_pair = std::pair<std::string, uint16_t>;
using host_port = IRCD_WEAK_T(host_port_pair);
static list clients;
@ -62,7 +58,7 @@ struct ircd::client
public:
client(std::shared_ptr<socket>);
client(const host_port &, const seconds &timeout = 5s);
client(const hostport &, const seconds &timeout = 5s);
client();
client(client &&) = delete;
client(const client &) = delete;
@ -70,11 +66,8 @@ struct ircd::client
client &operator=(const client &) = delete;
virtual ~client() noexcept;
friend host_port remote_addr(const client &);
friend host_port local_addr(const client &);
friend std::string string(const host_port &);
friend const auto &host(const host_port &);
friend const auto &port(const host_port &);
friend hostport remote(const client &);
friend hostport local(const client &);
};
struct ircd::client::init
@ -82,15 +75,3 @@ struct ircd::client::init
init();
~init() noexcept;
};
inline const auto &
ircd::port(const client::host_port &host_port)
{
return host_port.second;
}
inline const auto &
ircd::host(const client::host_port &host_port)
{
return host_port.first;
}

View file

@ -1,6 +1,6 @@
/*
* Copyright (C) 2016 Charybdis Development Team
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 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
@ -19,22 +19,23 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace ircd
#pragma once
#define HAVE_IRCD_IOS_H
/// Forward declarations for boost::asio because it is not included here.
///
/// Boost headers are not exposed to our users unless explicitly included by a
/// definition file. Other libircd headers may extend this namespace with more
/// forward declarations.
namespace boost::asio
{
struct listener;
struct io_service; // Allow a reference to an ios to be passed to ircd
}
struct ircd::listener
namespace ircd
{
struct acceptor;
namespace asio = boost::asio; // Alias so that asio:: can be used
IRCD_EXCEPTION(ircd::error, error)
private:
std::unique_ptr<struct acceptor> acceptor;
public:
listener(const json::object &options);
listener(const std::string &options);
~listener() noexcept;
};
extern asio::io_service *ios; // The user's io_service;
struct strand extern *strand; // IRCd's strand.
}

99
include/ircd/net.h Normal file
View file

@ -0,0 +1,99 @@
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 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
#define HAVE_IRCD_NET_H
namespace ircd::net
{
IRCD_EXCEPTION(ircd::error, error)
IRCD_EXCEPTION(error, broken_pipe)
IRCD_EXCEPTION(error, disconnected)
struct init;
struct socket;
struct listener;
using hostport_pair = std::pair<std::string, uint16_t>;
using hostport = IRCD_WEAK_T(hostport_pair);
const auto &host(const hostport &);
const auto &port(const hostport &);
std::string string(const hostport &);
string_view string(const hostport &, const mutable_buffer &buf);
bool connected(const socket &) noexcept;
hostport local_hostport(const socket &);
hostport remote_hostport(const socket &);
size_t available(const socket &);
size_t write(socket &, const ilist<const_buffer> &); // write_all
size_t write(socket &, const iov<const_buffer> &); // write_all
size_t write(socket &, const const_buffer &); // write_all
size_t write(socket &, iov<const_buffer> &); // write_some
size_t read(socket &, const ilist<mutable_buffer> &); // read_all
size_t read(socket &, const iov<mutable_buffer> &); // read_all
size_t read(socket &, const mutable_buffer &); // read_all
size_t read(socket &, iov<mutable_buffer> &); // read_some
}
namespace ircd
{
using net::socket;
using net::hostport;
using net::host;
using net::port;
using net::string;
}
struct ircd::net::listener
{
struct acceptor;
IRCD_EXCEPTION(ircd::error, error)
private:
std::unique_ptr<struct acceptor> acceptor;
public:
listener(const json::object &options);
listener(const std::string &options);
~listener() noexcept;
};
struct ircd::net::init
{
init();
~init() noexcept;
};
inline const auto &
ircd::net::port(const hostport &hostport)
{
return hostport.second;
}
inline const auto &
ircd::net::host(const hostport &hostport)
{
return hostport.first;
}

View file

@ -20,7 +20,7 @@
*/
#pragma once
#define HAVE_IRCD_CLIENT_SOCKET_H
#define HAVE_IRCD_NET_SOCKET_H
/// This file is not included with the IRCd standard include stack because
/// it requires symbols we can't forward declare without boost headers. It
@ -29,9 +29,8 @@
/// client.h still offers higher level access to sockets without requiring
/// boost headers; please check that for satisfaction before including this.
namespace ircd
namespace ircd::net
{
namespace asio = boost::asio;
namespace ip = asio::ip;
using boost::system::error_code;
@ -44,28 +43,24 @@ namespace ircd
extern asio::ssl::context sslv23_client;
std::string string(const ip::address &);
std::string string(const ip::tcp::endpoint &);
ip::address address(const ip::tcp::endpoint &);
std::string hostaddr(const ip::tcp::endpoint &);
uint16_t port(const ip::tcp::endpoint &);
size_t write(socket &, const ilist<const_buffer> &); // write_all
size_t write(socket &, const iov<const_buffer> &); // write_all
size_t write(socket &, const const_buffer &); // write_all
size_t write(socket &, iov<const_buffer> &); // write_some
size_t read(socket &, const ilist<mutable_buffer> &); // read_all
size_t read(socket &, const iov<mutable_buffer> &); // read_all
size_t read(socket &, const mutable_buffer &); // read_all
size_t read(socket &, iov<mutable_buffer> &); // read_some
size_t available(const socket &);
bool connected(const socket &) noexcept;
}
struct ircd::socket
:std::enable_shared_from_this<ircd::socket>
namespace ircd
{
using net::error_code;
using net::string;
using net::address;
using net::hostaddr;
using net::port;
}
struct ircd::net::socket
:std::enable_shared_from_this<ircd::net::socket>
{
struct init;
struct stat;
struct scope_timeout;
struct io;
@ -149,7 +144,7 @@ struct ircd::socket
~socket() noexcept;
};
class ircd::socket::scope_timeout
class ircd::net::socket::scope_timeout
{
socket *s;
@ -161,7 +156,7 @@ class ircd::socket::scope_timeout
~scope_timeout() noexcept;
};
class ircd::socket::io
class ircd::net::socket::io
{
struct socket &sock;
struct stat &stat;
@ -173,17 +168,9 @@ class ircd::socket::io
io(struct socket &, struct stat &, const std::function<size_t ()> &closure);
};
struct ircd::socket::init
{
std::unique_ptr<ip::tcp::resolver> resolver;
init();
~init() noexcept;
};
template<class iov>
auto
ircd::socket::write(const iov &bufs)
ircd::net::socket::write(const iov &bufs)
{
return io(*this, out, [&]
{
@ -193,8 +180,8 @@ ircd::socket::write(const iov &bufs)
template<class iov>
auto
ircd::socket::write(const iov &bufs,
error_code &ec)
ircd::net::socket::write(const iov &bufs,
error_code &ec)
{
return io(*this, out, [&]
{
@ -204,7 +191,7 @@ ircd::socket::write(const iov &bufs,
template<class iov>
auto
ircd::socket::write_some(const iov &bufs)
ircd::net::socket::write_some(const iov &bufs)
{
return io(*this, out, [&]
{
@ -214,8 +201,8 @@ ircd::socket::write_some(const iov &bufs)
template<class iov>
auto
ircd::socket::write_some(const iov &bufs,
error_code &ec)
ircd::net::socket::write_some(const iov &bufs,
error_code &ec)
{
return io(*this, out, [&]
{
@ -225,7 +212,7 @@ ircd::socket::write_some(const iov &bufs,
template<class iov>
auto
ircd::socket::read(const iov &bufs)
ircd::net::socket::read(const iov &bufs)
{
return io(*this, in, [&]
{
@ -240,8 +227,8 @@ ircd::socket::read(const iov &bufs)
template<class iov>
auto
ircd::socket::read(const iov &bufs,
error_code &ec)
ircd::net::socket::read(const iov &bufs,
error_code &ec)
{
return io(*this, in, [&]
{
@ -251,7 +238,7 @@ ircd::socket::read(const iov &bufs,
template<class iov>
auto
ircd::socket::read_some(const iov &bufs)
ircd::net::socket::read_some(const iov &bufs)
{
return io(*this, in, [&]
{
@ -266,8 +253,8 @@ ircd::socket::read_some(const iov &bufs)
template<class iov>
auto
ircd::socket::read_some(const iov &bufs,
error_code &ec)
ircd::net::socket::read_some(const iov &bufs,
error_code &ec)
{
return io(*this, in, [&]
{

View file

@ -140,31 +140,6 @@ namespace std
using experimental::string_view;
}
///////////////////////////////////////////////////////////////////////////////
//
// Forward declarations from third party namespaces not included here
//
/// Forward declarations for boost because it is not included here.
///
/// libircd does not include third party headers along with its own headers
/// for the public interface, only standard library headers. boost is only
/// included in specific definition files where we use its functionality.
/// This is a major improvement in project compile time.
namespace boost
{
}
/// Forward declarations for boost::asio because it is not included here.
///
/// Boost headers are not exposed to our users unless explicitly included by a
/// definition file. Other libircd headers may extend this namespace with more
/// forward declarations.
namespace boost::asio
{
struct io_service; // Allow a reference to an ios to be passed to ircd
}
///////////////////////////////////////////////////////////////////////////////
//
// Some items imported into our namespace.
@ -181,7 +156,6 @@ namespace ircd
using std::const_pointer_cast;
using ostream = std::ostream;
namespace ph = std::placeholders;
namespace asio = boost::asio;
using namespace std::string_literals;
using namespace std::literals::chrono_literals;
template<class... T> using ilist = std::initializer_list<T...>;
@ -195,10 +169,8 @@ namespace ircd
// Unsorted section
namespace ircd
{
extern boost::asio::io_service *ios;
constexpr size_t BUFSIZE { 512 };
struct socket;
struct client;
std::string demangle(const std::string &symbol);
@ -227,15 +199,16 @@ namespace ircd
#include "http.h"
#include "fmt.h"
#include "fs.h"
#include "ios.h"
#include "ctx.h"
#include "logger.h"
#include "db.h"
#include "js.h"
#include "client.h"
#include "mods.h"
#include "listen.h"
#include "net.h"
#include "m.h"
#include "resource.h"
#include "client.h"
template<class T>
std::string

View file

@ -66,16 +66,15 @@ libircd_la_SOURCES = \
ircd.cc \
json.cc \
lexical.cc \
listen.cc \
locale.cc \
logger.cc \
matrix.cc \
mods.cc \
net.cc \
openssl.cc \
parse.cc \
resource.cc \
rfc1459.cc \
socket.cc \
###
if JS

View file

@ -57,7 +57,7 @@ client::list client::clients;
bool handle_ec_timeout(client &);
bool handle_ec_eof(client &);
bool handle_ec_success(client &);
bool handle_ec(client &, const error_code &);
bool handle_ec(client &, const net::error_code &);
void async_recv_next(std::shared_ptr<client>, const milliseconds &timeout);
void async_recv_next(std::shared_ptr<client>);
@ -144,26 +144,22 @@ ircd::write(client &client,
return base;
}
ircd::client::host_port
ircd::local_addr(const client &client)
ircd::hostport
ircd::local(const client &client)
{
if(!client.sock)
return { "0.0.0.0"s, 0 };
const auto &sock(*client.sock);
const auto &ep(sock.local());
return { hostaddr(ep), port(ep) };
return net::local_hostport(*client.sock);
}
ircd::client::host_port
ircd::remote_addr(const client &client)
ircd::hostport
ircd::remote(const client &client)
{
if(!client.sock)
return { "0.0.0.0"s, 0 };
const auto &sock(*client.sock);
const auto &ep(sock.remote());
return { hostaddr(ep), port(ep) };
return net::remote_hostport(*client.sock);
}
ircd::http::response::write_closure
@ -220,7 +216,7 @@ ircd::client::client()
{
}
ircd::client::client(const host_port &host_port,
ircd::client::client(const hostport &host_port,
const seconds &timeout)
:client
{
@ -297,7 +293,7 @@ catch(const boost::system::system_error &e)
catch(const std::exception &e)
{
log::error("client[%s] [500 Internal Error]: %s",
string(remote_addr(*this)),
string(remote(*this)),
e.what());
if(ircd::debugmode)
@ -313,7 +309,7 @@ try
{
client.request_timer = ircd::timer{};
client.sock->set_timeout(request_timeout, [&client]
(const error_code &ec)
(const net::error_code &ec)
{
if(!ec)
client.sock->cancel();
@ -334,7 +330,7 @@ try
catch(const http::error &e)
{
log::debug("client[%s] HTTP %s in %ld$us %s",
string(remote_addr(client)),
string(remote(client)),
e.what(),
client.request_timer.at<microseconds>().count(),
e.content);
@ -354,7 +350,7 @@ ircd::handle_request(client &client,
const http::request::head &head)
{
log::debug("client[%s] HTTP %s `%s' (content-length: %zu)",
string(remote_addr(client)),
string(remote(client)),
head.method,
head.path,
head.content_length);
@ -376,8 +372,8 @@ ircd::add_client(std::shared_ptr<socket> s)
};
log::debug("client[%s] CONNECTED local[%s]",
string(remote_addr(*client)),
string(local_addr(*client)));
string(remote(*client)),
string(local(*client)));
async_recv_next(client, async_timeout);
return client;
@ -440,7 +436,7 @@ ircd::async_recv_next(std::shared_ptr<client> client,
// This call returns immediately so we no longer block the current context and
// its stack while waiting for activity on idle connections between requests.
sock(timeout, [client(std::move(client)), timeout](const error_code &ec)
sock(timeout, [client(std::move(client)), timeout](const net::error_code &ec)
noexcept
{
// Right here this handler is executing on the main stack (not in any
@ -468,7 +464,7 @@ ircd::async_recv_next(std::shared_ptr<client> client,
bool
ircd::handle_ec(client &client,
const error_code &ec)
const net::error_code &ec)
{
using namespace boost::system::errc;
using boost::asio::error::eof;
@ -493,7 +489,7 @@ ircd::handle_ec_eof(client &client)
try
{
log::debug("client[%s]: EOF",
string(remote_addr(client)));
string(remote(client)));
client.sock->disconnect(socket::FIN_RECV);
return false;
@ -513,7 +509,7 @@ try
{
auto &sock(*client.sock);
log::debug("client[%s]: disconnecting after inactivity timeout",
string(remote_addr(client)));
string(remote(client)));
sock.disconnect();
return false;
@ -526,13 +522,3 @@ catch(const std::exception &e)
return false;
}
std::string
ircd::string(const client::host_port &pair)
{
std::string ret(64, '\0');
ret.resize(snprintf(&ret.front(), ret.size(), "%s:%u",
pair.first.c_str(),
pair.second));
return ret;
}

View file

@ -33,11 +33,13 @@ namespace ircd
const enum runlevel &runlevel{_runlevel};
runlevel_handler runlevel_changed;
boost::asio::io_service *ios; // user's io service
struct strand *strand; // our main strand
ctx::ctx *main_context;
bool debugmode;
void set_runlevel(const enum runlevel &);
void init_rlimit();
void at_main_exit() noexcept;
void main();
}
@ -61,11 +63,15 @@ void
ircd::init(boost::asio::io_service &ios,
const std::string &configfile,
runlevel_handler runlevel_changed)
try
{
assert(runlevel == runlevel::STOPPED);
ircd::ios = &ios;
init_rlimit();
ircd::ios = &ios;
ircd::strand = new struct strand(ios);
// The log is available, but it is console-only until conf opens files.
log::init();
log::mark("START");
@ -94,6 +100,12 @@ ircd::init(boost::asio::io_service &ios,
ircd::set_runlevel(runlevel::READY);
}
catch(const std::exception &e)
{
delete strand;
strand = nullptr;
throw;
}
///
/// Notifies IRCd to shutdown. A shutdown will occur asynchronously and this
@ -146,7 +158,11 @@ try
// When this function completes, subsystems are done shutting down and IRCd
// transitions to STOPPED.
const unwind stopped{std::bind(&ircd::set_runlevel, runlevel::STOPPED)};
const unwind stopped{[]
{
at_main_exit();
set_runlevel(runlevel::STOPPED);
}};
// These objects are the init()'s and fini()'s for each subsystem.
// Appearing here ties their life to the main context. Initialization can
@ -154,7 +170,7 @@ try
// more appropriate.
ctx::ole::init _ole_; // Thread OffLoad Engine
socket::init _socket_; // Socket/Networking
net::init _net_; // Networking
client::init _client_; // Client related
db::init _db_; // RocksDB
js::init _js_; // SpiderMonkey
@ -185,6 +201,17 @@ catch(const std::exception &e)
std::terminate();
}
void
ircd::at_main_exit()
noexcept
{
strand->post([]
{
delete strand;
strand = nullptr;
});
}
void
ircd::set_runlevel(const enum runlevel &new_runlevel)
try

View file

@ -1,368 +0,0 @@
/*
* 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.
*/
#include <ircd/asio.h>
namespace ircd {
struct listener::acceptor
{
using error_code = boost::system::error_code;
static log::log log;
std::string name;
size_t backlog;
asio::ssl::context ssl;
ip::tcp::endpoint ep;
ip::tcp::acceptor a;
explicit operator std::string() const;
void configure(const json::object &opts);
// Handshake stack
bool handshake_error(const error_code &ec);
void handshake(const error_code &ec, std::shared_ptr<socket>) noexcept;
// Acceptance stack
bool accept_error(const error_code &ec);
void accept(const error_code &ec, std::shared_ptr<socket>) noexcept;
// Accept next
void next();
acceptor(const json::object &opts);
~acceptor() noexcept;
};
} // namespace ircd
///////////////////////////////////////////////////////////////////////////////
//
// ircd::listener
//
ircd::listener::listener(const std::string &opts)
:listener{json::object{opts}}
{
}
ircd::listener::listener(const json::object &opts)
:acceptor{std::make_unique<struct acceptor>(opts)}
{
}
ircd::listener::~listener()
noexcept
{
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd::listener::acceptor
//
ircd::log::log
ircd::listener::acceptor::log
{
"listener"
};
ircd::listener::acceptor::acceptor(const json::object &opts)
try
:name
{
unquote(opts.get("name", "IRCd (ssl)"s))
}
,backlog
{
opts.get<size_t>("backlog", asio::socket_base::max_connections - 2)
}
,ssl
{
asio::ssl::context::method::sslv23_server
}
,ep
{
ip::address::from_string(unquote(opts.get("host", "127.0.0.1"s))),
opts.get<uint16_t>("port", 6667)
}
,a
{
*ircd::ios
}
{
static const ip::tcp::acceptor::reuse_address reuse_address{true};
configure(opts);
log.debug("%s configured listener SSL",
std::string(*this));
a.open(ep.protocol());
a.set_option(reuse_address);
log.debug("%s opened listener socket",
std::string(*this));
a.bind(ep);
log.debug("%s bound listener socket",
std::string(*this));
a.listen(backlog);
log.debug("%s listening (backlog: %lu)",
std::string(*this),
backlog);
next();
}
catch(const boost::system::system_error &e)
{
throw error("listener: %s", e.what());
}
ircd::listener::acceptor::~acceptor()
noexcept
{
a.cancel();
}
void
ircd::listener::acceptor::next()
try
{
auto sock(std::make_shared<ircd::socket>(ssl));
log.debug("%s: listening with next socket(%p)",
std::string(*this),
sock.get());
// The context blocks here until the next client is connected.
auto accept(std::bind(&acceptor::accept, this, ph::_1, sock));
a.async_accept(sock->sd, accept);
}
catch(const std::exception &e)
{
log.critical("%s: %s",
std::string(*this),
e.what());
if(ircd::debugmode)
throw;
}
void
ircd::listener::acceptor::accept(const error_code &ec,
const std::shared_ptr<socket> sock)
noexcept try
{
if(accept_error(ec))
return;
log.debug("%s: accepted %s",
std::string(*this),
string(sock->remote()));
/*
static const asio::socket_base::keep_alive keep_alive(true);
static const asio::socket_base::linger linger(true, 30);
sock->sd.set_option(keep_alive);
sock->sd.set_option(linger);
sock->sd.non_blocking(true);
*/
static const auto handshake_type
{
socket::handshake_type::server
};
auto handshake
{
std::bind(&acceptor::handshake, this, ph::_1, sock)
};
sock->ssl.async_handshake(handshake_type, handshake);
}
catch(const std::exception &e)
{
log.error("%s: in accept(): socket(%p): %s",
std::string(*this),
sock.get(),
e.what());
next();
}
bool
ircd::listener::acceptor::accept_error(const error_code &ec)
{
switch(ec.value())
{
using namespace boost::system::errc;
case success:
return false;
case operation_canceled:
return true;
default:
throw boost::system::system_error(ec);
}
}
void
ircd::listener::acceptor::handshake(const error_code &ec,
const std::shared_ptr<socket> sock)
noexcept try
{
if(handshake_error(ec))
return;
log.debug("%s SSL handshook %s",
std::string(*this),
string(sock->remote()));
add_client(sock);
next();
}
catch(const std::exception &e)
{
log.error("%s: in handshake(): socket(%p)[%s]: %s",
std::string(*this),
sock.get(),
string(sock->remote()),
e.what());
}
bool
ircd::listener::acceptor::handshake_error(const error_code &ec)
{
switch(ec.value())
{
using namespace boost::system::errc;
case success:
return false;
case operation_canceled:
return true;
default:
throw boost::system::system_error(ec);
}
}
void
ircd::listener::acceptor::configure(const json::object &opts)
{
log.debug("%s preparing listener socket configuration...",
std::string(*this));
ssl.set_options
(
ssl.default_workarounds
//| ssl.no_tlsv1
//| ssl.no_tlsv1_1
| ssl.no_tlsv1_2
//| ssl.no_sslv2
| ssl.no_sslv3
//| ssl.single_dh_use
);
//TODO: XXX
ssl.set_password_callback([this]
(const auto &size, const auto &purpose)
{
log.debug("%s asking for password with purpose '%s' (size: %zu)",
std::string(*this),
purpose,
size);
//XXX: TODO
return "foobar";
});
if(opts.has("ssl_certificate_chain_file"))
{
const std::string filename
{
unquote(opts["ssl_certificate_chain_file"])
};
ssl.use_certificate_chain_file(filename);
log.info("%s using certificate chain file '%s'",
std::string(*this),
filename);
}
if(opts.has("ssl_certificate_file_pem"))
{
const std::string filename
{
unquote(opts["ssl_certificate_file_pem"])
};
ssl.use_certificate_file(filename, asio::ssl::context::pem);
log.info("%s using certificate file '%s'",
std::string(*this),
filename);
}
if(opts.has("ssl_private_key_file_pem"))
{
const std::string filename
{
unquote(opts["ssl_private_key_file_pem"])
};
ssl.use_private_key_file(filename, asio::ssl::context::pem);
log.info("%s using private key file '%s'",
std::string(*this),
filename);
}
if(opts.has("ssl_tmp_dh_file"))
{
const std::string filename
{
unquote(opts["ssl_tmp_dh_file"])
};
ssl.use_tmp_dh_file(filename);
log.info("%s using tmp dh file '%s'",
std::string(*this),
filename);
}
}
ircd::listener::acceptor::operator std::string()
const
{
std::string ret(256, char());
const auto length
{
fmt::snprintf(&ret.front(), ret.size(), "'%s' @ [%s]:%u",
name,
string(ep.address()),
ep.port())
};
ret.resize(length);
return ret;
}

970
ircd/net.cc Normal file
View file

@ -0,0 +1,970 @@
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 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.
*/
#include <ircd/asio.h>
///////////////////////////////////////////////////////////////////////////////
//
// net.h
//
namespace ircd::net
{
ip::tcp::resolver *resolver;
}
ircd::net::init::init()
{
net::resolver = new ip::tcp::resolver{*ircd::ios};
}
ircd::net::init::~init()
{
assert(net::resolver);
delete net::resolver;
net::resolver = nullptr;
}
//
// net.h
//
std::string
ircd::net::string(const hostport &pair)
{
std::string ret;
ret.resize(256, char{});
ret.resize(string(pair, mutable_buffer{ret}).size());
return ret;
}
ircd::string_view
ircd::net::string(const hostport &pair,
const mutable_buffer &buf)
{
const auto len
{
fmt::sprintf(buf, "%s:%u", pair.first, pair.second)
};
return { data(buf), size_t(len) };
}
size_t
ircd::net::read(socket &socket,
iov<mutable_buffer> &bufs)
{
const size_t read(socket.read_some(bufs));
const size_t consumed(buffer::consume(bufs, read));
assert(read == consumed);
return read;
}
size_t
ircd::net::read(socket &socket,
const iov<mutable_buffer> &bufs)
{
return socket.read(bufs);
}
size_t
ircd::net::read(socket &socket,
const mutable_buffer &buf)
{
const ilist<mutable_buffer> bufs{buf};
return socket.read(bufs);
}
size_t
ircd::net::write(socket &socket,
iov<const_buffer> &bufs)
{
const size_t wrote(socket.write_some(bufs));
const size_t consumed(consume(bufs, wrote));
assert(wrote == consumed);
return consumed;
}
size_t
ircd::net::write(socket &socket,
const iov<const_buffer> &bufs)
{
const size_t wrote(socket.write(bufs));
assert(wrote == size(bufs));
return wrote;
}
size_t
ircd::net::write(socket &socket,
const const_buffer &buf)
{
const ilist<const_buffer> bufs{buf};
const size_t wrote(socket.write(bufs));
assert(wrote == size(bufs));
return wrote;
}
size_t
ircd::net::write(socket &socket,
const ilist<const_buffer> &bufs)
{
const size_t wrote(socket.write(bufs));
assert(wrote == size(bufs));
return wrote;
}
//
// ircd::net::listener
//
ircd::net::listener::listener(const std::string &opts)
:listener{json::object{opts}}
{
}
ircd::net::listener::listener(const json::object &opts)
:acceptor{std::make_unique<struct acceptor>(opts)}
{
}
ircd::net::listener::~listener()
noexcept
{
}
//
// ircd::net::listener::acceptor
//
struct ircd::net::listener::acceptor
{
using error_code = boost::system::error_code;
static log::log log;
std::string name;
size_t backlog;
asio::ssl::context ssl;
ip::tcp::endpoint ep;
ip::tcp::acceptor a;
explicit operator std::string() const;
void configure(const json::object &opts);
// Handshake stack
bool handshake_error(const error_code &ec);
void handshake(const error_code &ec, std::shared_ptr<socket>) noexcept;
// Acceptance stack
bool accept_error(const error_code &ec);
void accept(const error_code &ec, std::shared_ptr<socket>) noexcept;
// Accept next
void next();
acceptor(const json::object &opts);
~acceptor() noexcept;
};
//
// ircd::net::listener::acceptor
//
ircd::log::log
ircd::net::listener::acceptor::log
{
"listener"
};
ircd::net::listener::acceptor::acceptor(const json::object &opts)
try
:name
{
unquote(opts.get("name", "IRCd (ssl)"s))
}
,backlog
{
opts.get<size_t>("backlog", asio::socket_base::max_connections - 2)
}
,ssl
{
asio::ssl::context::method::sslv23_server
}
,ep
{
ip::address::from_string(unquote(opts.get("host", "127.0.0.1"s))),
opts.get<uint16_t>("port", 6667)
}
,a
{
*ircd::ios
}
{
static const ip::tcp::acceptor::reuse_address reuse_address{true};
configure(opts);
log.debug("%s configured listener SSL",
std::string(*this));
a.open(ep.protocol());
a.set_option(reuse_address);
log.debug("%s opened listener socket",
std::string(*this));
a.bind(ep);
log.debug("%s bound listener socket",
std::string(*this));
a.listen(backlog);
log.debug("%s listening (backlog: %lu)",
std::string(*this),
backlog);
next();
}
catch(const boost::system::system_error &e)
{
throw error("listener: %s", e.what());
}
ircd::net::listener::acceptor::~acceptor()
noexcept
{
a.cancel();
}
void
ircd::net::listener::acceptor::next()
try
{
auto sock(std::make_shared<ircd::socket>(ssl));
log.debug("%s: listening with next socket(%p)",
std::string(*this),
sock.get());
// The context blocks here until the next client is connected.
auto accept(std::bind(&acceptor::accept, this, ph::_1, sock));
a.async_accept(sock->sd, accept);
}
catch(const std::exception &e)
{
log.critical("%s: %s",
std::string(*this),
e.what());
if(ircd::debugmode)
throw;
}
void
ircd::net::listener::acceptor::accept(const error_code &ec,
const std::shared_ptr<socket> sock)
noexcept try
{
if(accept_error(ec))
return;
log.debug("%s: accepted %s",
std::string(*this),
string(sock->remote()));
/*
static const asio::socket_base::keep_alive keep_alive(true);
static const asio::socket_base::linger linger(true, 30);
sock->sd.set_option(keep_alive);
sock->sd.set_option(linger);
sock->sd.non_blocking(true);
*/
static const auto handshake_type
{
socket::handshake_type::server
};
auto handshake
{
std::bind(&acceptor::handshake, this, ph::_1, sock)
};
sock->ssl.async_handshake(handshake_type, handshake);
}
catch(const std::exception &e)
{
log.error("%s: in accept(): socket(%p): %s",
std::string(*this),
sock.get(),
e.what());
next();
}
bool
ircd::net::listener::acceptor::accept_error(const error_code &ec)
{
switch(ec.value())
{
using namespace boost::system::errc;
case success:
return false;
case operation_canceled:
return true;
default:
throw boost::system::system_error(ec);
}
}
void
ircd::net::listener::acceptor::handshake(const error_code &ec,
const std::shared_ptr<socket> sock)
noexcept try
{
if(handshake_error(ec))
return;
log.debug("%s SSL handshook %s",
std::string(*this),
string(sock->remote()));
add_client(sock);
next();
}
catch(const std::exception &e)
{
log.error("%s: in handshake(): socket(%p)[%s]: %s",
std::string(*this),
sock.get(),
string(sock->remote()),
e.what());
}
bool
ircd::net::listener::acceptor::handshake_error(const error_code &ec)
{
switch(ec.value())
{
using namespace boost::system::errc;
case success:
return false;
case operation_canceled:
return true;
default:
throw boost::system::system_error(ec);
}
}
void
ircd::net::listener::acceptor::configure(const json::object &opts)
{
log.debug("%s preparing listener socket configuration...",
std::string(*this));
ssl.set_options
(
ssl.default_workarounds
//| ssl.no_tlsv1
//| ssl.no_tlsv1_1
| ssl.no_tlsv1_2
//| ssl.no_sslv2
| ssl.no_sslv3
//| ssl.single_dh_use
);
//TODO: XXX
ssl.set_password_callback([this]
(const auto &size, const auto &purpose)
{
log.debug("%s asking for password with purpose '%s' (size: %zu)",
std::string(*this),
purpose,
size);
//XXX: TODO
return "foobar";
});
if(opts.has("ssl_certificate_chain_file"))
{
const std::string filename
{
unquote(opts["ssl_certificate_chain_file"])
};
ssl.use_certificate_chain_file(filename);
log.info("%s using certificate chain file '%s'",
std::string(*this),
filename);
}
if(opts.has("ssl_certificate_file_pem"))
{
const std::string filename
{
unquote(opts["ssl_certificate_file_pem"])
};
ssl.use_certificate_file(filename, asio::ssl::context::pem);
log.info("%s using certificate file '%s'",
std::string(*this),
filename);
}
if(opts.has("ssl_private_key_file_pem"))
{
const std::string filename
{
unquote(opts["ssl_private_key_file_pem"])
};
ssl.use_private_key_file(filename, asio::ssl::context::pem);
log.info("%s using private key file '%s'",
std::string(*this),
filename);
}
if(opts.has("ssl_tmp_dh_file"))
{
const std::string filename
{
unquote(opts["ssl_tmp_dh_file"])
};
ssl.use_tmp_dh_file(filename);
log.info("%s using tmp dh file '%s'",
std::string(*this),
filename);
}
}
ircd::net::listener::acceptor::operator std::string()
const
{
std::string ret;
ret.resize(256, char{});
const auto length
{
fmt::sprintf(mutable_buffer{ret}, "'%s' @ [%s]:%u",
name,
string(ep.address()),
ep.port())
};
ret.resize(length);
return ret;
}
///////////////////////////////////////////////////////////////////////////////
//
// socket.h
//
boost::asio::ssl::context
ircd::net::sslv23_client
{
boost::asio::ssl::context::method::sslv23_client
};
size_t
ircd::net::available(const socket &s)
{
return s.sd.available();
}
ircd::net::hostport
ircd::net::local_hostport(const socket &socket)
{
const auto &ep(socket.local());
return { hostaddr(ep), port(ep) };
}
ircd::net::hostport
ircd::net::remote_hostport(const socket &socket)
{
const auto &ep(socket.remote());
return { hostaddr(ep), port(ep) };
}
bool
ircd::net::connected(const socket &s)
noexcept
{
return s.connected();
}
uint16_t
ircd::net::port(const ip::tcp::endpoint &ep)
{
return ep.port();
}
std::string
ircd::net::hostaddr(const ip::tcp::endpoint &ep)
{
return string(address(ep));
}
std::string
ircd::net::string(const ip::address &addr)
{
return addr.to_string();
}
std::string
ircd::net::string(const ip::tcp::endpoint &ep)
{
std::string ret;
ret.resize(256, char{});
ret.resize(fmt::sprintf(mutable_buffer{ret}, "%s:%u", string(address(ep)), port(ep)));
return ret;
}
boost::asio::ip::address
ircd::net::address(const ip::tcp::endpoint &ep)
{
return ep.address();
}
//
// socket::io
//
ircd::net::socket::io::io(struct socket &sock,
struct stat &stat,
const std::function<size_t ()> &closure)
:sock{sock}
,stat{stat}
,bytes{closure()}
{
stat.bytes += bytes;
stat.calls++;
}
ircd::net::socket::io::operator size_t()
const
{
return bytes;
}
//
// socket::scope_timeout
//
ircd::net::socket::scope_timeout::scope_timeout(socket &socket,
const milliseconds &timeout)
:s{&socket}
{
socket.set_timeout(timeout, [&socket]
(const error_code &ec)
{
if(!ec)
socket.sd.cancel();
});
}
ircd::net::socket::scope_timeout::scope_timeout(socket &socket,
const milliseconds &timeout,
const socket::handler &handler)
:s{&socket}
{
socket.set_timeout(timeout, handler);
}
ircd::net::socket::scope_timeout::~scope_timeout()
noexcept
{
s->timer.cancel();
}
//
// socket
//
ircd::net::socket::socket(const std::string &host,
const uint16_t &port,
const milliseconds &timeout,
asio::ssl::context &ssl,
boost::asio::io_service *const &ios)
:socket
{
[&host, &port]() -> ip::tcp::endpoint
{
assert(resolver);
const ip::tcp::resolver::query query(host, string(lex_cast(port)));
auto epit(resolver->async_resolve(query, yield_context{to_asio{}}));
static const ip::tcp::resolver::iterator end;
if(epit == end)
throw nxdomain("host '%s' not found", host.data());
return *epit;
}(),
timeout,
ssl,
ios
}
{
}
ircd::net::socket::socket(const ip::tcp::endpoint &remote,
const milliseconds &timeout,
asio::ssl::context &ssl,
boost::asio::io_service *const &ios)
:socket{ssl, ios}
{
connect(remote, timeout);
}
ircd::net::socket::socket(asio::ssl::context &ssl,
boost::asio::io_service *const &ios)
:ssl
{
*ios, ssl
}
,sd
{
this->ssl.next_layer()
}
,timer
{
*ios
}
,timedout
{
false
}
{
}
ircd::net::socket::~socket()
noexcept
{
}
void
ircd::net::socket::connect(const ip::tcp::endpoint &ep,
const milliseconds &timeout)
{
const scope_timeout ts(*this, timeout);
sd.async_connect(ep, yield_context{to_asio{}});
ssl.async_handshake(socket::handshake_type::client, yield_context{to_asio{}});
}
void
ircd::net::socket::disconnect(const dc &type)
{
if(timer.expires_from_now() > 0ms)
timer.cancel();
if(sd.is_open()) switch(type)
{
default:
case dc::RST: sd.close(); break;
case dc::FIN: sd.shutdown(ip::tcp::socket::shutdown_both); break;
case dc::FIN_SEND: sd.shutdown(ip::tcp::socket::shutdown_send); break;
case dc::FIN_RECV: sd.shutdown(ip::tcp::socket::shutdown_receive); break;
case dc::SSL_NOTIFY:
{
ssl.async_shutdown([socket(shared_from_this())]
(boost::system::error_code ec)
{
if(!ec)
socket->sd.close(ec);
if(ec)
log::warning("socket(%p): disconnect(): %s",
socket.get(),
ec.message());
});
break;
}
case dc::SSL_NOTIFY_YIELD:
{
ssl.async_shutdown(yield_context{to_asio{}});
sd.close();
break;
}
}
}
void
ircd::net::socket::cancel()
{
timer.cancel();
sd.cancel();
}
/// Asynchronous callback when the socket is ready
///
/// Overload for operator() without a timeout. see: operator()
///
void
ircd::net::socket::operator()(handler h)
{
operator()(milliseconds(-1), std::move(h));
}
/// Asynchronous callback when the socket is ready
///
/// This function calls back the handler when the socket has received
/// something and is ready to be read from.
///
/// The purpose here is to allow waiting for data from the socket without
/// blocking any context and using any stack space whatsoever, i.e full
/// asynchronous mode.
///
/// boost::asio has no direct way to accomplish this because the buffer size
/// must be positive so we use a little trick to read a single byte with
/// MSG_PEEK as our indication. This is done directly on the socket and
/// not through the SSL cipher, but we don't want this byte anyway. This
/// isn't such a great trick.
///
void
ircd::net::socket::operator()(const milliseconds &timeout,
handler callback)
{
static const auto flags
{
ip::tcp::socket::message_peek
};
static char buffer[1];
static const asio::mutable_buffers_1 buffers
{
buffer, sizeof(buffer)
};
auto handler
{
std::bind(&socket::handle, this, weak_from(*this), std::move(callback), ph::_1, ph::_2)
};
set_timeout(timeout);
sd.async_receive(buffers, flags, std::move(handler));
}
void
ircd::net::socket::handle(const std::weak_ptr<socket> wp,
const handler callback,
const error_code &ec,
const size_t &bytes)
noexcept
{
// This handler may still be registered with asio after the socket destructs, so
// the weak_ptr will indicate that fact. However, this is never intended and is
// a debug assertion which should be corrected.
if(unlikely(wp.expired()))
{
log::warning("socket(%p): belated callback to handler...", this);
assert(0);
return;
}
// This handler and the timeout handler are responsible for canceling each other
// when one or the other is entered. If the timeout handler has already fired for
// a timeout on the socket, `timedout` will be `true` and this handler will be
// entered with an `operation_canceled` error.
if(!timedout)
timer.cancel();
else
assert(ec == boost::system::errc::operation_canceled);
// We can handle a few errors at this level which don't ever need to invoke the
// user's callback. Otherwise they are passed up.
if(!handle_error(ec))
{
log::debug("socket(%p): %s", this, ec.message());
return;
}
call_user(callback, ec);
}
void
ircd::net::socket::call_user(const handler &callback,
const error_code &ec)
noexcept try
{
callback(ec);
}
catch(const std::exception &e)
{
log::error("socket(%p): async handler: unhandled user exception: %s",
this,
e.what());
if(ircd::debugmode)
std::terminate();
}
bool
ircd::net::socket::handle_error(const error_code &ec)
{
using namespace boost::system::errc;
switch(ec.value())
{
// A success is not an error; can call the user handler
case success:
return true;
// A cancel is triggered either by the timeout handler or by
// a request to shutdown/close the socket. We only call the user's
// handler for a timeout, otherwise this is hidden from the user.
case operation_canceled:
return timedout;
// This indicates the remote closed the socket, we still
// pass this up to the user so they can handle it.
case boost::asio::error::eof:
return true;
// This is a condition which we hide from the user.
case bad_file_descriptor:
return false;
// Everything else is passed up to the user.
default:
return true;
}
}
void
ircd::net::socket::handle_timeout(const std::weak_ptr<socket> wp,
const error_code &ec)
{
using namespace boost::system::errc;
if(!wp.expired()) switch(ec.value())
{
// A 'success' for this handler means there was a timeout on the socket
case success:
timedout = true;
cancel();
break;
// A cancelation means there was no timeout.
case operation_canceled:
timedout = false;
break;
// All other errors are unexpected, logged and ignored here.
default:
log::error("socket::handle_timeout(): unexpected: %s\n",
ec.message());
break;
}
}
bool
ircd::net::socket::connected()
const noexcept try
{
return sd.is_open();
}
catch(const boost::system::system_error &e)
{
return false;
}
void
ircd::net::socket::set_timeout(const milliseconds &t)
{
if(t < milliseconds(0))
return;
timer.expires_from_now(t);
timer.async_wait(std::bind(&socket::handle_timeout, this, weak_from(*this), ph::_1));
}
void
ircd::net::socket::set_timeout(const milliseconds &t,
handler h)
{
if(t < milliseconds(0))
return;
timer.expires_from_now(t);
timer.async_wait(std::move(h));
}
///////////////////////////////////////////////////////////////////////////////
//
// buffer.h - provide definition for the null buffers and asio conversion
//
const ircd::buffer::mutable_buffer
ircd::buffer::null_buffer
{
nullptr, nullptr
};
const ircd::ilist<ircd::buffer::mutable_buffer>
ircd::buffer::null_buffers
{{
null_buffer
}};
ircd::buffer::mutable_buffer::operator
boost::asio::mutable_buffer()
const
{
return boost::asio::mutable_buffer
{
data(*this), size(*this)
};
}
ircd::buffer::const_buffer::operator
boost::asio::const_buffer()
const
{
return boost::asio::const_buffer
{
data(*this), size(*this)
};
}
ircd::buffer::mutable_raw_buffer::operator
boost::asio::mutable_buffer()
const
{
return boost::asio::mutable_buffer
{
data(*this), size(*this)
};
}
ircd::buffer::const_raw_buffer::operator
boost::asio::const_buffer()
const
{
return boost::asio::const_buffer
{
data(*this), size(*this)
};
}

View file

@ -1,561 +0,0 @@
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 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.
*/
#include <ircd/asio.h>
///////////////////////////////////////////////////////////////////////////////
//
// socket.h
//
boost::asio::ssl::context
ircd::sslv23_client
{
boost::asio::ssl::context::method::sslv23_client
};
namespace ircd
{
ip::tcp::resolver *resolver;
}
ircd::socket::init::init()
:resolver
{
std::make_unique<ip::tcp::resolver>(*ios)
}
{
ircd::resolver = resolver.get();
}
ircd::socket::init::~init()
{
ircd::resolver = nullptr;
}
ircd::socket::scope_timeout::scope_timeout(socket &socket,
const milliseconds &timeout)
:s{&socket}
{
socket.set_timeout(timeout, [&socket]
(const error_code &ec)
{
if(!ec)
socket.sd.cancel();
});
}
ircd::socket::scope_timeout::scope_timeout(socket &socket,
const milliseconds &timeout,
const socket::handler &handler)
:s{&socket}
{
socket.set_timeout(timeout, handler);
}
ircd::socket::scope_timeout::~scope_timeout()
noexcept
{
s->timer.cancel();
}
ircd::socket::socket(const std::string &host,
const uint16_t &port,
const milliseconds &timeout,
asio::ssl::context &ssl,
boost::asio::io_service *const &ios)
:socket
{
[&host, &port]() -> ip::tcp::endpoint
{
assert(resolver);
const ip::tcp::resolver::query query(host, string(lex_cast(port)));
auto epit(resolver->async_resolve(query, yield_context{to_asio{}}));
static const ip::tcp::resolver::iterator end;
if(epit == end)
throw nxdomain("host '%s' not found", host.data());
return *epit;
}(),
timeout,
ssl,
ios
}
{
}
ircd::socket::socket(const ip::tcp::endpoint &remote,
const milliseconds &timeout,
asio::ssl::context &ssl,
boost::asio::io_service *const &ios)
:socket{ssl, ios}
{
connect(remote, timeout);
}
ircd::socket::socket(asio::ssl::context &ssl,
boost::asio::io_service *const &ios)
:ssl
{
*ios, ssl
}
,sd
{
this->ssl.next_layer()
}
,timer
{
*ios
}
,timedout
{
false
}
{
}
ircd::socket::~socket()
noexcept
{
}
void
ircd::socket::connect(const ip::tcp::endpoint &ep,
const milliseconds &timeout)
{
const scope_timeout ts(*this, timeout);
sd.async_connect(ep, yield_context{to_asio{}});
ssl.async_handshake(socket::handshake_type::client, yield_context{to_asio{}});
}
void
ircd::socket::disconnect(const dc &type)
{
if(timer.expires_from_now() > 0ms)
timer.cancel();
if(sd.is_open()) switch(type)
{
default:
case dc::RST: sd.close(); break;
case dc::FIN: sd.shutdown(ip::tcp::socket::shutdown_both); break;
case dc::FIN_SEND: sd.shutdown(ip::tcp::socket::shutdown_send); break;
case dc::FIN_RECV: sd.shutdown(ip::tcp::socket::shutdown_receive); break;
case dc::SSL_NOTIFY:
{
ssl.async_shutdown([socket(shared_from_this())]
(boost::system::error_code ec)
{
if(!ec)
socket->sd.close(ec);
if(ec)
log::warning("socket(%p): disconnect(): %s",
socket.get(),
ec.message());
});
break;
}
case dc::SSL_NOTIFY_YIELD:
{
ssl.async_shutdown(yield_context{to_asio{}});
sd.close();
break;
}
}
}
void
ircd::socket::cancel()
{
timer.cancel();
sd.cancel();
}
/// Asynchronous callback when the socket is ready
///
/// Overload for operator() without a timeout. see: operator()
///
void
ircd::socket::operator()(handler h)
{
operator()(milliseconds(-1), std::move(h));
}
/// Asynchronous callback when the socket is ready
///
/// This function calls back the handler when the socket has received
/// something and is ready to be read from.
///
/// The purpose here is to allow waiting for data from the socket without
/// blocking any context and using any stack space whatsoever, i.e full
/// asynchronous mode.
///
/// boost::asio has no direct way to accomplish this because the buffer size
/// must be positive so we use a little trick to read a single byte with
/// MSG_PEEK as our indication. This is done directly on the socket and
/// not through the SSL cipher, but we don't want this byte anyway. This
/// isn't such a great trick.
///
void
ircd::socket::operator()(const milliseconds &timeout,
handler callback)
{
static const auto flags
{
ip::tcp::socket::message_peek
};
static char buffer[1];
static const asio::mutable_buffers_1 buffers
{
buffer, sizeof(buffer)
};
auto handler
{
std::bind(&socket::handle, this, weak_from(*this), std::move(callback), ph::_1, ph::_2)
};
set_timeout(timeout);
sd.async_receive(buffers, flags, std::move(handler));
}
void
ircd::socket::handle(const std::weak_ptr<socket> wp,
const handler callback,
const error_code &ec,
const size_t &bytes)
noexcept
{
// This handler may still be registered with asio after the socket destructs, so
// the weak_ptr will indicate that fact. However, this is never intended and is
// a debug assertion which should be corrected.
if(unlikely(wp.expired()))
{
log::warning("socket(%p): belated callback to handler...", this);
assert(0);
return;
}
// This handler and the timeout handler are responsible for canceling each other
// when one or the other is entered. If the timeout handler has already fired for
// a timeout on the socket, `timedout` will be `true` and this handler will be
// entered with an `operation_canceled` error.
if(!timedout)
timer.cancel();
else
assert(ec == boost::system::errc::operation_canceled);
// We can handle a few errors at this level which don't ever need to invoke the
// user's callback. Otherwise they are passed up.
if(!handle_error(ec))
{
log::debug("socket(%p): %s", this, ec.message());
return;
}
call_user(callback, ec);
}
void
ircd::socket::call_user(const handler &callback,
const error_code &ec)
noexcept try
{
callback(ec);
}
catch(const std::exception &e)
{
log::error("socket(%p): async handler: unhandled user exception: %s",
this,
e.what());
if(ircd::debugmode)
std::terminate();
}
bool
ircd::socket::handle_error(const error_code &ec)
{
using namespace boost::system::errc;
switch(ec.value())
{
// A success is not an error; can call the user handler
case success:
return true;
// A cancel is triggered either by the timeout handler or by
// a request to shutdown/close the socket. We only call the user's
// handler for a timeout, otherwise this is hidden from the user.
case operation_canceled:
return timedout;
// This indicates the remote closed the socket, we still
// pass this up to the user so they can handle it.
case boost::asio::error::eof:
return true;
// This is a condition which we hide from the user.
case bad_file_descriptor:
return false;
// Everything else is passed up to the user.
default:
return true;
}
}
void
ircd::socket::handle_timeout(const std::weak_ptr<socket> wp,
const error_code &ec)
{
using namespace boost::system::errc;
if(!wp.expired()) switch(ec.value())
{
// A 'success' for this handler means there was a timeout on the socket
case success:
timedout = true;
cancel();
break;
// A cancelation means there was no timeout.
case operation_canceled:
timedout = false;
break;
// All other errors are unexpected, logged and ignored here.
default:
log::error("socket::handle_timeout(): unexpected: %s\n",
ec.message());
break;
}
}
size_t
ircd::available(const socket &s)
{
return s.sd.available();
}
bool
ircd::connected(const socket &s)
noexcept
{
return s.connected();
}
uint16_t
ircd::port(const ip::tcp::endpoint &ep)
{
return ep.port();
}
std::string
ircd::hostaddr(const ip::tcp::endpoint &ep)
{
return string(address(ep));
}
std::string
ircd::string(const ip::address &addr)
{
return addr.to_string();
}
boost::asio::ip::address
ircd::address(const ip::tcp::endpoint &ep)
{
return ep.address();
}
size_t
ircd::read(socket &socket,
iov<mutable_buffer> &bufs)
{
const size_t read(socket.read_some(bufs));
const size_t consumed(buffer::consume(bufs, read));
assert(read == consumed);
return read;
}
size_t
ircd::read(socket &socket,
const iov<mutable_buffer> &bufs)
{
return socket.read(bufs);
}
size_t
ircd::read(socket &socket,
const mutable_buffer &buf)
{
const ilist<mutable_buffer> bufs{buf};
return socket.read(bufs);
}
size_t
ircd::write(socket &socket,
iov<const_buffer> &bufs)
{
const size_t wrote(socket.write_some(bufs));
const size_t consumed(consume(bufs, wrote));
assert(wrote == consumed);
return consumed;
}
size_t
ircd::write(socket &socket,
const iov<const_buffer> &bufs)
{
const size_t wrote(socket.write(bufs));
assert(wrote == size(bufs));
return wrote;
}
size_t
ircd::write(socket &socket,
const const_buffer &buf)
{
const ilist<const_buffer> bufs{buf};
const size_t wrote(socket.write(bufs));
assert(wrote == size(bufs));
return wrote;
}
size_t
ircd::write(socket &socket,
const ilist<const_buffer> &bufs)
{
const size_t wrote(socket.write(bufs));
assert(wrote == size(bufs));
return wrote;
}
bool
ircd::socket::connected()
const noexcept try
{
return sd.is_open();
}
catch(const boost::system::system_error &e)
{
return false;
}
void
ircd::socket::set_timeout(const milliseconds &t)
{
if(t < milliseconds(0))
return;
timer.expires_from_now(t);
timer.async_wait(std::bind(&socket::handle_timeout, this, weak_from(*this), ph::_1));
}
void
ircd::socket::set_timeout(const milliseconds &t,
handler h)
{
if(t < milliseconds(0))
return;
timer.expires_from_now(t);
timer.async_wait(std::move(h));
}
ircd::socket::io::io(struct socket &sock,
struct stat &stat,
const std::function<size_t ()> &closure)
:sock{sock}
,stat{stat}
,bytes{closure()}
{
stat.bytes += bytes;
stat.calls++;
}
ircd::socket::io::operator size_t()
const
{
return bytes;
}
///////////////////////////////////////////////////////////////////////////////
//
// buffer.h - provide definition for the null buffers and asio conversion
//
const ircd::buffer::mutable_buffer
ircd::buffer::null_buffer
{
nullptr, nullptr
};
const ircd::ilist<ircd::buffer::mutable_buffer>
ircd::buffer::null_buffers
{{
null_buffer
}};
ircd::buffer::mutable_buffer::operator
boost::asio::mutable_buffer()
const
{
return boost::asio::mutable_buffer
{
data(*this), size(*this)
};
}
ircd::buffer::const_buffer::operator
boost::asio::const_buffer()
const
{
return boost::asio::const_buffer
{
data(*this), size(*this)
};
}
ircd::buffer::mutable_raw_buffer::operator
boost::asio::mutable_buffer()
const
{
return boost::asio::mutable_buffer
{
data(*this), size(*this)
};
}
ircd::buffer::const_raw_buffer::operator
boost::asio::const_buffer()
const
{
return boost::asio::const_buffer
{
data(*this), size(*this)
};
}