0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 18:18:56 +02:00
construct/ircd/net.cc

2287 lines
48 KiB
C++
Raw Normal View History

2017-09-30 08:04:41 +02:00
/*
* 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>
2018-01-04 22:34:07 +01:00
/// Internal pimpl wrapping an instance of boost's resolver service. This
/// class is a singleton with the instance as a static member of
/// ircd::net::resolve. This service requires a valid ircd::ios which is not
/// available during static initialization; instead it is tied to net::init.
struct ircd::net::resolver
:std::unique_ptr<ip::tcp::resolver>
{
resolver() = default;
~resolver() noexcept = default;
};
2017-09-30 08:04:41 +02:00
///////////////////////////////////////////////////////////////////////////////
//
2017-10-19 10:30:19 +02:00
// net/net.h
2017-09-30 08:04:41 +02:00
//
2018-01-04 22:34:07 +01:00
/// Network subsystem log facility with dedicated SNOMASK.
struct ircd::log::log
ircd::net::log
{
"net", 'N'
};
/// Network subsystem initialization
2017-09-30 08:04:41 +02:00
ircd::net::init::init()
{
2018-01-04 22:34:07 +01:00
assert(ircd::ios);
resolve::resolver.reset(new ip::tcp::resolver{*ircd::ios});
sslv23_client.set_verify_mode(asio::ssl::verify_peer);
sslv23_client.set_default_verify_paths();
2017-09-30 08:04:41 +02:00
}
/// Network subsystem shutdown
2017-09-30 08:04:41 +02:00
ircd::net::init::~init()
{
2018-01-04 22:34:07 +01:00
resolve::resolver.reset(nullptr);
2017-09-30 08:04:41 +02:00
}
///////////////////////////////////////////////////////////////////////////////
//
// net/sockpub.h
//
bool
ircd::net::disconnect(socket &socket)
noexcept
{
return disconnect(socket, dc::SSL_NOTIFY);
}
2018-01-02 01:16:39 +01:00
bool
ircd::net::disconnect(socket &socket,
const dc &type)
noexcept try
{
2018-01-02 01:16:39 +01:00
socket.disconnect(type);
return true;
}
catch(const std::exception &e)
{
/*
log::error("socket(%p): disconnect: type: %d: %s",
this,
int(type),
e.what());
*/
return false;
}
/// Attempt to connect and ssl handshake; future.
///
ircd::ctx::future<std::shared_ptr<ircd::net::socket>>
ircd::net::open(const connopts &opts)
{
ctx::promise<std::shared_ptr<socket>> p;
ctx::future<std::shared_ptr<socket>> f(p);
auto s{std::make_shared<socket>()};
open(*s, opts, [s, p(std::move(p))]
(std::exception_ptr eptr)
mutable
{
if(eptr)
p.set_exception(std::move(eptr));
else
p.set_value(s);
});
return f;
}
/// Attempt to connect and ssl handshake; asynchronous, callback when done.
///
void
ircd::net::open(socket &socket,
const connopts &opts,
std::function<void (std::exception_ptr)> handler)
{
auto complete{[&socket, &opts, handler(std::move(handler))]
(std::exception_ptr eptr)
{
if(eptr)
disconnect(socket, dc::RST);
handler(std::move(eptr));
}};
auto connector{[&socket, &opts, complete(std::move(complete))]
(std::exception_ptr eptr, const ipport &ipport)
{
if(eptr)
return complete(std::move(eptr));
const auto ep{make_endpoint(ipport)};
socket.connect(ep, opts, [&socket, complete(std::move(complete))]
(const error_code &ec)
{
complete(make_eptr(ec));
});
}};
if(!opts.ipport)
resolve(opts.hostport, std::move(connector));
else
connector({}, opts.ipport);
2017-09-30 08:04:41 +02:00
}
void
ircd::net::flush(socket &socket)
2017-09-30 08:04:41 +02:00
{
if(nodelay(socket))
return;
nodelay(socket, true);
nodelay(socket, false);
2017-09-30 08:04:41 +02:00
}
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,
2018-01-02 01:16:39 +01:00
const ilist<const_buffer> &bufs)
2017-09-30 08:04:41 +02:00
{
const size_t wrote(socket.write(bufs));
assert(wrote == size(bufs));
return wrote;
}
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);
}
//
// Active observers
//
2018-01-02 01:16:39 +01:00
ircd::const_raw_buffer
ircd::net::peer_cert_der(const mutable_raw_buffer &buf,
const socket &socket)
{
const SSL &ssl(socket);
2018-01-05 06:59:39 +01:00
const X509 &cert{openssl::peer_cert(ssl)};
2018-01-02 01:16:39 +01:00
return openssl::i2d(buf, cert);
}
ircd::net::ipport
ircd::net::remote_ipport(const socket &socket)
2018-01-02 01:16:39 +01:00
noexcept try
{
const auto &ep(socket.remote());
2018-01-02 01:16:39 +01:00
return make_ipport(ep);
}
catch(...)
{
return {};
}
ircd::net::ipport
ircd::net::local_ipport(const socket &socket)
2018-01-02 01:16:39 +01:00
noexcept try
{
const auto &ep(socket.local());
2018-01-02 01:16:39 +01:00
return make_ipport(ep);
}
catch(...)
{
return {};
}
2017-09-30 08:04:41 +02:00
size_t
ircd::net::available(const socket &socket)
2018-01-02 01:16:39 +01:00
noexcept
2017-09-30 08:04:41 +02:00
{
const ip::tcp::socket &sd(socket);
2018-01-02 01:16:39 +01:00
boost::system::error_code ec;
return sd.available(ec);
}
size_t
ircd::net::readable(const socket &socket)
{
ip::tcp::socket &sd(const_cast<net::socket &>(socket));
ip::tcp::socket::bytes_readable command{true};
sd.io_control(command);
return command.get();
}
2018-01-02 01:16:39 +01:00
bool
ircd::net::connected(const socket &socket)
noexcept try
{
const ip::tcp::socket &sd(socket);
return sd.is_open();
}
catch(...)
{
return false;
}
//
// Options
//
/// Construct sockopts with the current options from socket argument
ircd::net::sockopts::sockopts(const socket &socket)
:blocking{net::blocking(socket)}
,nodelay{net::nodelay(socket)}
,keepalive{net::keepalive(socket)}
,linger{net::linger(socket)}
,read_bufsz{ssize_t(net::read_bufsz(socket))}
,write_bufsz{ssize_t(net::write_bufsz(socket))}
,read_lowat{ssize_t(net::read_lowat(socket))}
,write_lowat{ssize_t(net::write_lowat(socket))}
{
}
/// Updates the socket with provided options. Defaulted / -1'ed options are
/// ignored for updating.
void
ircd::net::set(socket &socket,
const sockopts &opts)
{
if(opts.blocking != opts.IGN)
net::blocking(socket, opts.blocking);
if(opts.nodelay != opts.IGN)
net::nodelay(socket, opts.nodelay);
if(opts.keepalive != opts.IGN)
net::keepalive(socket, opts.keepalive);
if(opts.linger != opts.IGN)
net::linger(socket, opts.linger);
if(opts.read_bufsz != opts.IGN)
net::read_bufsz(socket, opts.read_bufsz);
if(opts.write_bufsz != opts.IGN)
net::write_bufsz(socket, opts.write_bufsz);
if(opts.read_lowat != opts.IGN)
net::read_lowat(socket, opts.read_lowat);
if(opts.write_lowat != opts.IGN)
net::write_lowat(socket, opts.write_lowat);
}
void
ircd::net::write_lowat(socket &socket,
const size_t &bytes)
{
assert(bytes <= std::numeric_limits<int>::max());
ip::tcp::socket::send_low_watermark option
{
int(bytes)
};
ip::tcp::socket &sd(socket);
sd.set_option(option);
}
void
ircd::net::read_lowat(socket &socket,
const size_t &bytes)
{
assert(bytes <= std::numeric_limits<int>::max());
ip::tcp::socket::receive_low_watermark option
{
int(bytes)
};
ip::tcp::socket &sd(socket);
sd.set_option(option);
}
void
ircd::net::write_bufsz(socket &socket,
const size_t &bytes)
{
assert(bytes <= std::numeric_limits<int>::max());
ip::tcp::socket::send_buffer_size option
{
int(bytes)
};
ip::tcp::socket &sd(socket);
sd.set_option(option);
}
void
ircd::net::read_bufsz(socket &socket,
const size_t &bytes)
{
assert(bytes <= std::numeric_limits<int>::max());
ip::tcp::socket::receive_buffer_size option
{
int(bytes)
};
ip::tcp::socket &sd(socket);
sd.set_option(option);
}
void
ircd::net::linger(socket &socket,
const time_t &t)
{
assert(t >= std::numeric_limits<int>::min());
assert(t <= std::numeric_limits<int>::max());
ip::tcp::socket::linger option
{
t >= 0, // ON / OFF boolean
t >= 0? int(t) : 0 // Uses 0 when OFF
};
ip::tcp::socket &sd(socket);
sd.set_option(option);
}
void
ircd::net::keepalive(socket &socket,
const bool &b)
{
ip::tcp::socket::keep_alive option{b};
ip::tcp::socket &sd(socket);
sd.set_option(option);
}
void
ircd::net::nodelay(socket &socket,
const bool &b)
{
ip::tcp::no_delay option{b};
ip::tcp::socket &sd(socket);
sd.set_option(option);
}
void
ircd::net::blocking(socket &socket,
const bool &b)
{
ip::tcp::socket &sd(socket);
sd.non_blocking(!b);
}
size_t
ircd::net::write_lowat(const socket &socket)
{
const ip::tcp::socket &sd(socket);
ip::tcp::socket::send_low_watermark option{};
sd.get_option(option);
return option.value();
}
size_t
ircd::net::read_lowat(const socket &socket)
{
const ip::tcp::socket &sd(socket);
ip::tcp::socket::receive_low_watermark option{};
sd.get_option(option);
return option.value();
}
size_t
ircd::net::write_bufsz(const socket &socket)
{
const ip::tcp::socket &sd(socket);
ip::tcp::socket::send_buffer_size option{};
sd.get_option(option);
return option.value();
}
size_t
ircd::net::read_bufsz(const socket &socket)
{
const ip::tcp::socket &sd(socket);
ip::tcp::socket::receive_buffer_size option{};
sd.get_option(option);
return option.value();
}
time_t
ircd::net::linger(const socket &socket)
{
const ip::tcp::socket &sd(socket);
ip::tcp::socket::linger option;
sd.get_option(option);
return option.enabled()? option.timeout() : -1;
}
bool
ircd::net::keepalive(const socket &socket)
{
const ip::tcp::socket &sd(socket);
ip::tcp::socket::keep_alive option;
sd.get_option(option);
return option.value();
}
bool
ircd::net::nodelay(const socket &socket)
{
const ip::tcp::socket &sd(socket);
ip::tcp::no_delay option;
sd.get_option(option);
return option.value();
}
bool
ircd::net::blocking(const socket &socket)
2018-01-02 01:16:39 +01:00
{
const ip::tcp::socket &sd(socket);
return !sd.non_blocking();
2017-09-30 08:04:41 +02:00
}
2017-10-19 10:30:19 +02:00
///////////////////////////////////////////////////////////////////////////////
//
// net/listener.h
//
2017-09-30 08:04:41 +02:00
struct ircd::net::listener::acceptor
:std::enable_shared_from_this<struct ircd::net::listener::acceptor>
2017-09-30 08:04:41 +02:00
{
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;
size_t accepting {0};
size_t handshaking {0};
bool interrupting {false};
ctx::dock joining;
2017-09-30 08:04:41 +02:00
explicit operator std::string() const;
void configure(const json::object &opts);
// Handshake stack
bool handshake_error(const error_code &ec, socket &);
void handshake(const error_code &ec, std::shared_ptr<socket>, std::weak_ptr<acceptor>) noexcept;
2017-09-30 08:04:41 +02:00
// Acceptance stack
bool accept_error(const error_code &ec, socket &);
void accept(const error_code &ec, std::shared_ptr<socket>, std::weak_ptr<acceptor>) noexcept;
2017-09-30 08:04:41 +02:00
// Accept next
void next();
// Acceptor shutdown
bool interrupt() noexcept;
void join() noexcept;
2017-09-30 08:04:41 +02:00
acceptor(const json::object &opts);
~acceptor() noexcept;
};
//
// 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_shared<struct acceptor>(opts)}
{
// Starts the first asynchronous accept. This has to be done out here after
// the acceptor's shared object is constructed.
acceptor->next();
}
/// Cancels all pending accepts and handshakes and waits (yields ircd::ctx)
/// until report.
///
ircd::net::listener::~listener()
noexcept
{
if(acceptor)
acceptor->join();
}
void
ircd::net::listener::acceptor::join()
noexcept try
{
interrupt();
joining.wait([this]
{
return !accepting && !handshaking;
});
}
catch(const std::exception &e)
{
log.error("acceptor(%p): join: %s",
this,
e.what());
}
bool
ircd::net::listener::acceptor::interrupt()
noexcept try
{
a.cancel();
interrupting = true;
return true;
}
catch(const boost::system::system_error &e)
{
log.error("acceptor(%p): interrupt: %s",
this,
string(e));
return false;
}
2017-09-30 08:04:41 +02:00
//
// 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
{
//boost::asio::ip::tcp::socket::max_connections <-- linkage failed?
opts.get<size_t>("backlog", SOMAXCONN) //TODO: XXX
2017-09-30 08:04:41 +02:00
}
,ssl
{
asio::ssl::context::method::sslv23_server
}
,ep
{
ip::address::from_string(unquote(opts.get("host", "127.0.0.1"s))),
opts.at<uint16_t>("port")
2017-09-30 08:04:41 +02:00
}
,a
{
*ircd::ios
}
{
static const auto &max_connections
{
//boost::asio::ip::tcp::socket::max_connections <-- linkage failed?
SOMAXCONN //TODO: XXX
};
static const ip::tcp::acceptor::reuse_address reuse_address
{
true
};
2017-09-30 08:04:41 +02:00
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, max connections: %zu)",
2017-09-30 08:04:41 +02:00
std::string(*this),
backlog,
max_connections);
2017-09-30 08:04:41 +02:00
}
catch(const boost::system::system_error &e)
{
throw error("listener: %s", e.what());
}
ircd::net::listener::acceptor::~acceptor()
noexcept
{
}
/// Sets the next asynchronous handler to start the next accept sequence.
/// Each call to next() sets one handler which handles the connect for one
/// socket. After the connect, an asynchronous SSL handshake handler is set
/// for the socket, and next() is called again to setup for the next socket
/// too.
2017-09-30 08:04:41 +02:00
void
ircd::net::listener::acceptor::next()
try
{
auto sock(std::make_shared<ircd::socket>(ssl));
/*
log.debug("%s: socket(%p) is the next socket to accept",
2017-09-30 08:04:41 +02:00
std::string(*this),
sock.get());
*/
ip::tcp::socket &sd(*sock);
a.async_accept(sd, std::bind(&acceptor::accept, this, ph::_1, sock, weak_from(*this)));
++accepting;
2017-09-30 08:04:41 +02:00
}
catch(const std::exception &e)
{
log.critical("%s: %s",
std::string(*this),
e.what());
if(ircd::debugmode)
throw;
}
/// Callback for a socket connected. This handler then invokes the
/// asynchronous SSL handshake sequence.
///
2017-09-30 08:04:41 +02:00
void
ircd::net::listener::acceptor::accept(const error_code &ec,
const std::shared_ptr<socket> sock,
const std::weak_ptr<acceptor> a)
2017-09-30 08:04:41 +02:00
noexcept try
{
if(unlikely(a.expired()))
2017-09-30 08:04:41 +02:00
return;
--accepting;
const unwind::nominal next{[this]
{
this->next();
}};
const unwind::exceptional drop{[&sock]
{
assert(bool(sock));
disconnect(*sock, dc::RST);
}};
assert(bool(sock));
if(unlikely(accept_error(ec, *sock)))
{
disconnect(*sock, dc::RST);
return;
}
log.debug("%s: socket(%p) accepted %s",
2017-09-30 08:04:41 +02:00
std::string(*this),
sock.get(),
2017-09-30 08:04:41 +02:00
string(sock->remote()));
static const socket::handshake_type handshake_type
2017-09-30 08:04:41 +02:00
{
socket::handshake_type::server
};
auto handshake
{
std::bind(&acceptor::handshake, this, ph::_1, sock, a)
2017-09-30 08:04:41 +02:00
};
sock->ssl.async_handshake(handshake_type, std::move(handshake));
++handshaking;
}
catch(const ctx::interrupted &e)
{
log.debug("%s: acceptor interrupted socket(%p): %s",
std::string(*this),
sock.get(),
string(ec));
joining.notify_all();
2017-09-30 08:04:41 +02:00
}
catch(const std::exception &e)
{
log.error("%s: socket(%p): in accept(): [%s]: %s",
2017-09-30 08:04:41 +02:00
std::string(*this),
sock.get(),
connected(*sock)? string(sock->remote()) : "<gone>",
2017-09-30 08:04:41 +02:00
e.what());
}
/// Error handler for the accept socket callback. This handler determines
/// whether or not the handler should return or continue processing the
/// result.
///
2017-09-30 08:04:41 +02:00
bool
ircd::net::listener::acceptor::accept_error(const error_code &ec,
socket &sock)
2017-09-30 08:04:41 +02:00
{
using namespace boost::system::errc;
2017-12-29 23:53:39 +01:00
using boost::system::system_category;
if(unlikely(interrupting))
throw ctx::interrupted();
if(likely(ec == success))
return false;
2017-12-29 23:53:39 +01:00
if(ec.category() == system_category()) switch(ec.value())
2017-09-30 08:04:41 +02:00
{
case operation_canceled:
return false;
2017-09-30 08:04:41 +02:00
default:
break;
2017-09-30 08:04:41 +02:00
}
throw boost::system::system_error(ec);
2017-09-30 08:04:41 +02:00
}
void
ircd::net::listener::acceptor::handshake(const error_code &ec,
const std::shared_ptr<socket> sock,
const std::weak_ptr<acceptor> a)
2017-09-30 08:04:41 +02:00
noexcept try
{
if(unlikely(a.expired()))
return;
--handshaking;
assert(bool(sock));
const unwind::exceptional drop{[&sock]
{
disconnect(*sock, dc::RST);
}};
if(unlikely(handshake_error(ec, *sock)))
{
disconnect(*sock, dc::RST);
2017-09-30 08:04:41 +02:00
return;
}
2017-09-30 08:04:41 +02:00
log.debug("%s socket(%p): SSL handshook %s",
2017-09-30 08:04:41 +02:00
std::string(*this),
sock.get(),
2017-09-30 08:04:41 +02:00
string(sock->remote()));
add_client(sock);
}
catch(const ctx::interrupted &e)
{
log.debug("%s: SSL handshake interrupted socket(%p): %s",
std::string(*this),
sock.get(),
string(ec));
joining.notify_all();
}
2017-09-30 08:04:41 +02:00
catch(const std::exception &e)
{
log.error("%s: socket(%p): in handshake(): [%s]: %s",
2017-09-30 08:04:41 +02:00
std::string(*this),
sock.get(),
connected(*sock)? string(sock->remote()) : "<gone>",
2017-09-30 08:04:41 +02:00
e.what());
}
/// Error handler for the SSL handshake callback. This handler determines
/// whether or not the handler should return or continue processing the
/// result.
///
2017-09-30 08:04:41 +02:00
bool
ircd::net::listener::acceptor::handshake_error(const error_code &ec,
socket &sock)
2017-09-30 08:04:41 +02:00
{
2017-12-29 23:53:39 +01:00
using boost::system::system_category;
using namespace boost::system::errc;
if(unlikely(interrupting))
throw ctx::interrupted();
if(likely(ec == success))
return false;
2017-12-29 23:53:39 +01:00
if(ec.category() == system_category()) switch(ec.value())
2017-09-30 08:04:41 +02:00
{
case operation_canceled:
return false;
2017-09-30 08:04:41 +02:00
default:
break;
2017-09-30 08:04:41 +02:00
}
throw boost::system::system_error(ec);
2017-09-30 08:04:41 +02:00
}
void
ircd::net::listener::acceptor::configure(const json::object &opts)
{
log.debug("%s preparing listener socket configuration...",
std::string(*this));
2018-01-01 10:42:00 +01:00
/*
2017-09-30 08:04:41 +02:00
ssl.set_options
(
//ssl.default_workarounds
2017-09-30 08:04:41 +02:00
//| ssl.no_tlsv1
//| ssl.no_tlsv1_1
//| ssl.no_tlsv1_2
2017-09-30 08:04:41 +02:00
//| ssl.no_sslv2
//| ssl.no_sslv3
2018-01-01 10:42:00 +01:00
//| ssl.single_dh_use
2017-09-30 08:04:41 +02:00
);
2018-01-01 10:42:00 +01:00
*/
2017-09-30 08:04:41 +02:00
//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"])
};
if(!fs::exists(filename))
throw error("%s: SSL certificate chain file @ `%s' not found",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
ssl.use_certificate_chain_file(filename);
log.info("%s using certificate chain file '%s'",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
}
if(opts.has("ssl_certificate_file_pem"))
{
const std::string filename
{
unquote(opts["ssl_certificate_file_pem"])
};
if(!fs::exists(filename))
throw error("%s: SSL certificate pem file @ `%s' not found",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
ssl.use_certificate_file(filename, asio::ssl::context::pem);
log.info("%s using certificate file '%s'",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
}
if(opts.has("ssl_private_key_file_pem"))
{
const std::string filename
{
unquote(opts["ssl_private_key_file_pem"])
};
if(!fs::exists(filename))
throw error("%s: SSL private key file @ `%s' not found",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
ssl.use_private_key_file(filename, asio::ssl::context::pem);
log.info("%s using private key file '%s'",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
}
if(opts.has("ssl_tmp_dh_file"))
{
const std::string filename
{
unquote(opts["ssl_tmp_dh_file"])
};
if(!fs::exists(filename))
throw error("%s: SSL tmp dh file @ `%s' not found",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
ssl.use_tmp_dh_file(filename);
log.info("%s using tmp dh file '%s'",
std::string(*this),
filename);
2017-09-30 08:04:41 +02:00
}
}
ircd::net::listener::acceptor::operator std::string()
const
{
return fmt::snstringf
2017-09-30 08:04:41 +02:00
{
256, "'%s' @ [%s]:%u", name, string(ep.address()), ep.port()
2017-09-30 08:04:41 +02:00
};
}
///////////////////////////////////////////////////////////////////////////////
//
2017-10-19 10:30:19 +02:00
// net/socket.h
2017-09-30 08:04:41 +02:00
//
boost::asio::ssl::context
ircd::net::sslv23_client
{
boost::asio::ssl::context::method::sslv23_client
};
//
// socket
2017-09-30 08:04:41 +02:00
//
ircd::net::socket::socket(asio::ssl::context &ssl,
boost::asio::io_service *const &ios)
:sd
{
*ios
}
,ssl
2017-09-30 08:04:41 +02:00
{
this->sd, ssl
2017-09-30 08:04:41 +02:00
}
,timer
2017-09-30 08:04:41 +02:00
{
*ios
}
{
}
/// The dtor asserts that the socket is not open/connected requiring a
/// an SSL close_notify. There's no more room for async callbacks via
/// shared_ptr after this dtor.
2017-09-30 08:04:41 +02:00
ircd::net::socket::~socket()
noexcept try
{
if(unlikely(RB_DEBUG_LEVEL && connected(*this)))
log.critical("Failed to ensure socket(%p) is disconnected from %s before dtor.",
this,
string(remote()));
assert(!connected(*this));
}
catch(const std::exception &e)
2017-09-30 08:04:41 +02:00
{
log.critical("socket(%p): close: %s", this, e.what());
return;
2017-09-30 08:04:41 +02:00
}
void
ircd::net::socket::connect(const endpoint &ep,
const connopts &opts,
handler callback)
2017-09-30 08:04:41 +02:00
{
2017-10-25 19:02:45 +02:00
log.debug("socket(%p) attempting connect to remote: %s for the next %ld$ms",
this,
string(ep),
opts.connect_timeout.count());
2017-10-25 19:02:45 +02:00
auto connect_handler
{
std::bind(&socket::handle_connect, this, weak_from(*this), std::cref(opts), std::move(callback), ph::_1)
};
sd.async_connect(ep, std::move(connect_handler));
set_timeout(opts.connect_timeout);
}
void
ircd::net::socket::handshake(const connopts &opts,
handler callback)
{
log.debug("socket(%p) performing handshake with %s for '%s' for the next %ld$ms",
this,
string(remote()),
opts.common_name,
opts.handshake_timeout.count());
auto handshake_handler
{
std::bind(&socket::handle_handshake, this, weak_from(*this), std::cref(opts), std::move(callback), ph::_1)
};
auto verify_handler
{
std::bind(&socket::handle_verify, this, ph::_1, ph::_2, std::cref(opts))
};
ssl.set_verify_callback(std::move(verify_handler));
ssl.async_handshake(handshake_type::client, std::move(handshake_handler));
set_timeout(opts.handshake_timeout);
}
bool
2017-09-30 08:04:41 +02:00
ircd::net::socket::disconnect(const dc &type)
try
2017-09-30 08:04:41 +02:00
{
if(timer.expires_from_now() > 0ms)
timer.cancel();
if(sd.is_open())
log.debug("socket(%p): disconnect: %s type:%d user: in:%zu out:%zu",
2017-10-25 19:02:45 +02:00
(const void *)this,
ircd::string(remote_ipport(*this)),
uint(type),
in.bytes,
out.bytes);
2017-09-30 08:04:41 +02:00
if(sd.is_open()) switch(type)
{
default:
case dc::RST:
sd.close();
return true;
case dc::FIN:
sd.shutdown(ip::tcp::socket::shutdown_both);
return true;
case dc::FIN_SEND:
sd.shutdown(ip::tcp::socket::shutdown_send);
return true;
case dc::FIN_RECV:
sd.shutdown(ip::tcp::socket::shutdown_receive);
return true;
2017-09-30 08:04:41 +02:00
case dc::SSL_NOTIFY_YIELD: if(likely(ctx::current))
{
const life_guard<socket> lg{*this};
const scope_timeout ts{*this, 8s};
ssl.async_shutdown(yield_context{to_asio{}});
error_code ec;
sd.close(ec);
if(ec)
log.error("socket(%p): close: %s: %s",
this,
string(ec));
return true;
}
2017-09-30 08:04:41 +02:00
case dc::SSL_NOTIFY:
{
ssl.async_shutdown([s(shared_from_this())]
(error_code ec)
noexcept
2017-09-30 08:04:41 +02:00
{
if(!s->timedout)
s->cancel_timeout();
if(ec)
log.warning("socket(%p): SSL_NOTIFY: %s: %s",
2017-10-25 19:02:45 +02:00
s.get(),
string(ec));
if(!s->sd.is_open())
return;
s->sd.close(ec);
2017-09-30 08:04:41 +02:00
if(ec)
log.warning("socket(%p): after SSL_NOTIFY: %s: %s",
2017-10-25 19:02:45 +02:00
s.get(),
string(ec));
2017-09-30 08:04:41 +02:00
});
set_timeout(8s);
return true;
2017-09-30 08:04:41 +02:00
}
}
else return false;
2017-09-30 08:04:41 +02:00
}
catch(const boost::system::system_error &e)
{
2017-10-25 19:02:45 +02:00
log.warning("socket(%p): disconnect: type: %d: %s",
(const void *)this,
uint(type),
e.what());
if(sd.is_open())
{
boost::system::error_code ec;
sd.close(ec);
if(ec)
log.warning("socket(%p): after disconnect: %s: %s",
this,
string(ec));
}
throw;
}
2017-09-30 08:04:41 +02:00
bool
2017-09-30 08:04:41 +02:00
ircd::net::socket::cancel()
noexcept
2017-09-30 08:04:41 +02:00
{
2018-01-01 10:42:00 +01:00
static const auto good{[](const auto &ec)
{
return ec == boost::system::errc::success;
}};
boost::system::error_code ec[2];
sd.cancel(ec[0]);
timer.cancel(ec[1]);
2018-01-01 10:42:00 +01:00
return std::all_of(begin(ec), end(ec), good);
2017-09-30 08:04:41 +02:00
}
/// Asynchronous callback when the socket is ready
///
/// Overload for operator() without a timeout. see: operator()
///
void
2018-01-01 10:42:00 +01:00
ircd::net::socket::operator()(const wait_type &type,
handler h)
2017-09-30 08:04:41 +02:00
{
2018-01-01 10:42:00 +01:00
operator()(type, milliseconds(-1), std::move(h));
2017-09-30 08:04:41 +02:00
}
/// Asynchronous callback when the socket is ready
///
2018-01-01 10:42:00 +01:00
/// This function calls back the handler when the socket is ready
/// for the operation of the specified type.
2017-09-30 08:04:41 +02:00
///
void
2018-01-01 10:42:00 +01:00
ircd::net::socket::operator()(const wait_type &type,
const milliseconds &timeout,
2017-09-30 08:04:41 +02:00
handler callback)
{
2018-01-01 10:42:00 +01:00
auto handle
2017-09-30 08:04:41 +02:00
{
2018-01-01 10:42:00 +01:00
std::bind(&socket::handle, this, weak_from(*this), std::move(callback), ph::_1)
2017-09-30 08:04:41 +02:00
};
assert(connected(*this));
2018-01-01 10:42:00 +01:00
switch(type)
{
case wait_type::wait_error:
case wait_type::wait_write:
{
sd.async_wait(type, std::move(handle));
break;
}
// There might be a bug in boost on linux which is only reproducible
// when serving a large number of assets: a ready status for the socket
// is not indicated when it ought to be, at random. This is fixed below
// by doing it the old way (pre boost-1.66 sd.async_wait()) with the
// proper peek.
case wait_type::wait_read:
{
static const auto flags{ip::tcp::socket::message_peek};
sd.async_receive(buffer::null_buffers, flags, std::move(handle));
break;
}
}
// Commit to timeout here in case exception was thrown earlier.
set_timeout(timeout);
2017-09-30 08:04:41 +02:00
}
void
ircd::net::socket::handle(const std::weak_ptr<socket> wp,
const handler callback,
2018-01-01 10:42:00 +01:00
const error_code &ec)
noexcept try
2017-09-30 08:04:41 +02:00
{
2018-01-01 10:42:00 +01:00
using namespace boost::system::errc;
using boost::system::system_category;
// After life_guard is constructed it is safe to use *this in this frame.
const life_guard<socket> s{wp};
2018-01-01 10:42:00 +01:00
log.debug("socket(%p): handle: (%s)",
this,
string(ec));
2017-09-30 08:04:41 +02:00
if(!timedout)
cancel_timeout();
2017-09-30 08:04:41 +02:00
2018-01-01 10:42:00 +01:00
if(ec.category() == system_category()) switch(ec.value())
2017-09-30 08:04:41 +02:00
{
// We expose a timeout condition to the user, but hide
// other cancellations from invoking the callback.
2018-01-01 10:42:00 +01:00
case operation_canceled:
if(timedout)
break;
return;
// This is a condition which we hide from the user.
case bad_file_descriptor:
return;
// Everything else is passed up to the user.
default:
break;
2017-09-30 08:04:41 +02:00
}
call_user(callback, ec);
}
catch(const std::bad_weak_ptr &e)
{
// 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.
2017-10-25 19:02:45 +02:00
log.warning("socket(%p): belated callback to handler... (%s)",
this,
e.what());
assert(0);
}
catch(const std::exception &e)
{
2018-01-01 10:42:00 +01:00
log.critical("socket(%p): handle: %s",
this,
e.what());
assert(0);
2017-09-30 08:04:41 +02:00
}
void
ircd::net::socket::handle_timeout(const std::weak_ptr<socket> wp,
const error_code &ec)
noexcept try
{
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:
{
assert(timedout == false);
timedout = true;
sd.cancel();
break;
}
// A cancelation means there was no timeout.
case operation_canceled:
{
assert(ec.category() == boost::system::system_category());
assert(timedout == false);
timedout = false;
break;
}
// All other errors are unexpected, logged and ignored here.
default:
throw boost::system::system_error(ec);
}
}
catch(const boost::system::system_error &e)
{
log.critical("socket(%p): handle_timeout: unexpected: %s\n",
(const void *)this,
e.what());
assert(0);
}
catch(const std::exception &e)
{
log.error("socket(%p): handle timeout: %s",
(const void *)this,
e.what());
}
void
ircd::net::socket::handle_connect(std::weak_ptr<socket> wp,
const connopts &opts,
handler callback,
const error_code &ec)
noexcept try
{
const life_guard<socket> s{wp};
assert(!timedout || ec == boost::system::errc::operation_canceled);
log.debug("socket(%p) connect to remote: %s from local: %s: %s",
this,
string(remote_ipport(*this)),
string(local_ipport(*this)),
string(ec));
// The timer was set by socket::connect() and may need to be canceled.
if(!timedout)
cancel_timeout();
// A connect error; abort here by calling the user back with error.
if(ec)
return call_user(callback, ec);
// Try to set the user's socket options now; if something fails
// we can invoke their callback with the error.
if(opts.sopts) try
{
set(*this, *opts.sopts);
}
catch(const boost::system::system_error &e)
{
return call_user(callback, e.code());
}
// The user can opt out of performing the handshake here.
if(!opts.handshake)
return call_user(callback, ec);
handshake(opts, std::move(callback));
}
catch(const std::bad_weak_ptr &e)
{
log.warning("socket(%p): belated callback to handle_connect... (%s)",
this,
e.what());
assert(0);
}
catch(const std::exception &e)
{
log.critical("socket(%p): handle_connect: %s",
this,
e.what());
assert(0);
}
void
ircd::net::socket::handle_handshake(std::weak_ptr<socket> wp,
const connopts &opts,
handler callback,
const error_code &ec)
noexcept try
{
const life_guard<socket> s{wp};
assert(!timedout || ec == boost::system::errc::operation_canceled);
log.debug("socket(%p) handshake from local: %s to remote: %s: %s",
this,
string(local_ipport(*this)),
string(remote_ipport(*this)),
string(ec));
// The timer was set by socket::handshake() and may need to be canceled.
if(!timedout)
cancel_timeout();
// This is the end of the asynchronous call chain; the user is called
// back with or without error here.
call_user(callback, ec);
}
catch(const std::bad_weak_ptr &e)
{
log.warning("socket(%p): belated callback to handle_handshake... (%s)",
this,
e.what());
assert(0);
}
catch(const std::exception &e)
{
log.critical("socket(%p): handle_handshake: %s",
this,
e.what());
assert(0);
}
bool
ircd::net::socket::handle_verify(const bool valid,
asio::ssl::verify_context &vc,
const connopts &opts)
noexcept try
{
// `valid` indicates whether or not there's an anomaly with the
// certificate; if so, it is usually enumerated by the `switch()`
// statement below. If `valid` is false, this function can return
// true to continue but it appears this function will be called a
// second time with `valid=true`.
//
// TODO: XXX: This behavior must be confirmed since we return true
// TODO: XXX: early on recoverable errors and skip other checks
// TODO: XXX: expecting a second call..
//
// The user can set this option to bypass verification.
if(!opts.verify_certificate)
return true;
// X509_STORE_CTX &
assert(vc.native_handle());
const auto &stctx{*vc.native_handle()};
const auto &cert{openssl::current_cert(stctx)};
const auto required_common_name
{
opts.common_name? opts.common_name : opts.hostport.host
};
const auto reject{[&stctx, &required_common_name]
{
throw inauthentic
{
"%s #%ld: %s",
required_common_name,
openssl::get_error(stctx),
openssl::get_error_string(stctx)
};
}};
if(!valid)
{
char buf[256];
log.warning("verify: %s /CN=%s :%s",
required_common_name,
openssl::subject_common_name(buf, cert),
openssl::get_error_string(stctx));
}
if(!valid) switch(openssl::get_error(stctx))
{
case X509_V_OK:
assert(0);
default:
reject();
break;
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
assert(openssl::get_error_depth(stctx) == 0);
if(opts.allow_self_signed)
return true;
reject();
break;
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
if(opts.allow_self_chain)
return true;
reject();
break;
}
if(opts.verify_common_name)
{
//TODO: this object makes an std::string
boost::asio::ssl::rfc2818_verification verifier
{
std::string(required_common_name)
};
if(!verifier(true, vc))
{
char buf[256];
throw inauthentic
{
"/CN=%s does not match target host %s :%s",
openssl::subject_common_name(buf, cert),
required_common_name,
openssl::get_error_string(stctx)
};
}
}
return true;
}
catch(const inauthentic &e)
{
log.error("Certificate rejected: %s", e.what());
return false;
}
catch(const std::exception &e)
{
log.critical("Certificate error: %s", e.what());
return false;
}
2018-01-01 10:42:00 +01:00
void
ircd::net::socket::call_user(const handler &callback,
const error_code &ec)
noexcept try
{
callback(ec);
}
catch(const std::exception &e)
{
log.critical("socket(%p): async handler: unhandled exception: %s",
this,
e.what());
}
boost::asio::ip::tcp::endpoint
ircd::net::socket::local()
const
{
return sd.local_endpoint();
}
boost::asio::ip::tcp::endpoint
ircd::net::socket::remote()
const
{
return sd.remote_endpoint();
}
ircd::milliseconds
ircd::net::socket::cancel_timeout()
noexcept
{
const auto ret
{
timer.expires_from_now()
};
boost::system::error_code ec;
timer.cancel(ec);
assert(!ec);
return duration_cast<milliseconds>(ret);
}
2017-09-30 08:04:41 +02:00
void
ircd::net::socket::set_timeout(const milliseconds &t)
{
cancel_timeout();
2017-09-30 08:04:41 +02:00
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)
2017-09-30 08:04:41 +02:00
{
cancel_timeout();
2017-09-30 08:04:41 +02:00
if(t < milliseconds(0))
return;
timer.expires_from_now(t);
timer.async_wait(std::move(h));
}
bool
ircd::net::socket::has_timeout()
const noexcept
{
return !timedout && timer.expires_from_now() != milliseconds{0};
}
ircd::net::socket::operator
SSL &()
{
assert(ssl.native_handle());
return *ssl.native_handle();
}
ircd::net::socket::operator
const SSL &()
const
{
using type = typename std::remove_const<decltype(socket::ssl)>::type;
auto &ssl(const_cast<type &>(this->ssl));
assert(ssl.native_handle());
return *ssl.native_handle();
}
//
// socket::io
//
ircd::net::socket::io::io(struct socket &sock,
struct stat &stat,
const std::function<size_t ()> &closure)
:io
{
sock, stat, closure()
}
{}
ircd::net::socket::io::io(struct socket &sock,
struct stat &stat,
const size_t &bytes)
:sock{sock}
,stat{stat}
,bytes{bytes}
{
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);
}
ircd::net::socket::scope_timeout::scope_timeout(socket &socket,
const milliseconds &timeout,
socket::handler handler)
:s{&socket}
{
socket.set_timeout(timeout, std::move(handler));
}
ircd::net::socket::scope_timeout::scope_timeout(scope_timeout &&other)
noexcept
:s{std::move(other.s)}
{
other.s = nullptr;
}
ircd::net::socket::scope_timeout &
ircd::net::socket::scope_timeout::operator=(scope_timeout &&other)
noexcept
{
this->~scope_timeout();
s = std::move(other.s);
return *this;
}
ircd::net::socket::scope_timeout::~scope_timeout()
noexcept
{
cancel();
}
bool
ircd::net::socket::scope_timeout::cancel()
noexcept try
{
if(!this->s)
return false;
auto *const s{this->s};
this->s = nullptr;
s->cancel_timeout();
return true;
}
catch(const std::exception &e)
{
log.error("socket(%p) scope_timeout::cancel: %s",
(const void *)s,
e.what());
return false;
}
bool
ircd::net::socket::scope_timeout::release()
{
const auto s{this->s};
this->s = nullptr;
return s != nullptr;
}
//
// socket::xfer
//
ircd::net::socket::xfer::xfer(const error_code &error_code,
const size_t &bytes)
:eptr{make_eptr(error_code)}
,bytes{bytes}
{
}
///////////////////////////////////////////////////////////////////////////////
//
2017-12-30 06:42:39 +01:00
// net/resolve.h
//
namespace ircd::net
{
// Internal resolve base (requires boost syms)
using resolve_callback = std::function<void (std::exception_ptr, ip::tcp::resolver::results_type)>;
void _resolve(const hostport &, ip::tcp::resolver::flags, resolve_callback);
void _resolve(const ipport &, resolve_callback);
}
2018-01-04 22:34:07 +01:00
/// Singleton instance of the public interface ircd::net::resolve
decltype(ircd::net::resolve)
ircd::net::resolve
{
2018-01-04 22:34:07 +01:00
};
2018-01-04 22:34:07 +01:00
/// Singleton instance of the internal boost resolver wrapper.
decltype(ircd::net::resolve::resolver)
ircd::net::resolve::resolver
{
2018-01-04 22:34:07 +01:00
};
2018-01-04 22:34:07 +01:00
/// Resolve a numerical address to a hostname string. This is a PTR record
/// query or 'reverse DNS' lookup.
ircd::ctx::future<std::string>
ircd::net::resolve::operator()(const ipport &ipport)
{
2018-01-04 22:34:07 +01:00
ctx::promise<std::string> p;
ctx::future<std::string> ret{p};
operator()(ipport, [p(std::move(p))]
(std::exception_ptr eptr, std::string ptr)
mutable
{
if(eptr)
p.set_exception(std::move(eptr));
else
2018-01-04 22:34:07 +01:00
p.set_value(ptr);
});
2018-01-04 22:34:07 +01:00
return ret;
}
2018-01-04 22:34:07 +01:00
/// Resolve a hostname (with service name/portnum) to a numerical address. This
/// is an A or AAAA query (with automatic SRV query) returning a single result.
ircd::ctx::future<ircd::net::ipport>
ircd::net::resolve::operator()(const hostport &hostport)
{
2018-01-04 22:34:07 +01:00
ctx::promise<ipport> p;
ctx::future<ipport> ret{p};
operator()(hostport, [p(std::move(p))]
(std::exception_ptr eptr, const ipport &ip)
mutable
{
if(eptr)
p.set_exception(std::move(eptr));
else
2018-01-04 22:34:07 +01:00
p.set_value(ip);
});
2018-01-04 22:34:07 +01:00
return ret;
}
2018-01-04 22:34:07 +01:00
/// Lower-level PTR query (i.e "reverse DNS") with asynchronous callback
/// interface.
void
ircd::net::resolve::operator()(const ipport &ipport,
callback_reverse callback)
{
_resolve(ipport, [callback(std::move(callback))]
(std::exception_ptr eptr, ip::tcp::resolver::results_type results)
{
if(eptr)
return callback(std::move(eptr), {});
if(results.empty())
return callback({}, {});
assert(results.size() <= 1);
const auto &result(*begin(results));
callback({}, result.host_name());
});
}
2018-01-04 22:34:07 +01:00
/// Lower-level A or AAAA query (with automatic SRV query) with asynchronous
/// callback interface. This returns only one result.
void
ircd::net::resolve::operator()(const hostport &hostport,
callback_one callback)
{
2018-01-04 22:34:07 +01:00
static const ip::tcp::resolver::flags flags{};
_resolve(hostport, flags, [callback(std::move(callback))]
(std::exception_ptr eptr, ip::tcp::resolver::results_type results)
{
if(eptr)
return callback(std::move(eptr), {});
2018-01-04 22:34:07 +01:00
if(results.empty())
return callback(std::make_exception_ptr(nxdomain{}), {});
2018-01-04 22:34:07 +01:00
const auto &result(*begin(results));
callback(std::move(eptr), make_ipport(result));
});
}
2018-01-04 22:34:07 +01:00
/// Lower-level A+AAAA query (with automatic SRV query). This returns a vector
/// of all results in the callback.
void
ircd::net::resolve::operator()(const hostport &hostport,
callback_many callback)
{
static const ip::tcp::resolver::flags flags{};
_resolve(hostport, flags, [callback(std::move(callback))]
(std::exception_ptr eptr, ip::tcp::resolver::results_type results)
{
if(eptr)
return callback(std::move(eptr), {});
2018-01-04 22:34:07 +01:00
std::vector<ipport> vector(results.size());
std::transform(begin(results), end(results), begin(vector), []
(const auto &entry)
{
return make_ipport(entry.endpoint());
});
2018-01-04 22:34:07 +01:00
callback(std::move(eptr), std::move(vector));
});
}
/// Internal A/AAAA record resolver function
void
ircd::net::_resolve(const hostport &hostport,
ip::tcp::resolver::flags flags,
resolve_callback callback)
{
// Trivial host string
const string_view &host
{
hostport.host
};
// Determine if the port is a string or requires a lex_cast to one.
char portbuf[8];
const string_view &port
{
hostport.portnum? lex_cast(hostport.portnum, portbuf) : hostport.port
};
// Determine if the port is numeric and hint to avoid name lookup if so.
if(hostport.portnum || ctype<std::isdigit>(hostport.port) == -1)
flags |= ip::tcp::resolver::numeric_service;
// This base handler will provide exception guarantees for the entire stack.
// It may invoke callback twice in the case when callback throws unhandled,
// but the latter invocation will always have an the eptr set.
2018-01-04 22:34:07 +01:00
assert(bool(ircd::net::resolve::resolver));
resolve::resolver->async_resolve(host, port, flags, [callback(std::move(callback))]
(const error_code &ec, ip::tcp::resolver::results_type results)
noexcept
{
if(ec)
{
callback(std::make_exception_ptr(boost::system::system_error{ec}), std::move(results));
}
else try
{
callback({}, std::move(results));
}
catch(...)
{
callback(std::make_exception_ptr(std::current_exception()), {});
}
});
}
/// Internal PTR record resolver function
void
ircd::net::_resolve(const ipport &ipport,
resolve_callback callback)
{
2018-01-04 22:34:07 +01:00
assert(bool(ircd::net::resolve::resolver));
resolve::resolver->async_resolve(make_endpoint(ipport), [callback(std::move(callback))]
(const error_code &ec, ip::tcp::resolver::results_type results)
noexcept
{
if(ec)
{
callback(std::make_exception_ptr(boost::system::system_error{ec}), std::move(results));
}
else try
{
callback({}, std::move(results));
}
catch(...)
{
callback(std::make_exception_ptr(std::current_exception()), {});
}
});
}
2017-12-30 06:42:39 +01:00
///////////////////////////////////////////////////////////////////////////////
//
// net/remote.h
//
//
// host / port utils
//
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const uint32_t &ip)
{
const auto len
{
ip::address_v4{ip}.to_string().copy(data(buf), size(buf))
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const uint128_t &ip)
{
const auto &pun
{
reinterpret_cast<const uint8_t (&)[16]>(ip)
};
const auto &punpun
{
reinterpret_cast<const std::array<uint8_t, 16> &>(pun)
};
const auto len
{
ip::address_v6{punpun}.to_string().copy(data(buf), size(buf))
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const hostport &hp)
{
const auto len
{
fmt::sprintf
{
buf, "%s:%s",
hp.host,
hp.portnum? lex_cast(hp.portnum) : hp.port
}
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const ipport &ipp)
{
const auto len
{
is_v4(ipp)?
fmt::sprintf(buf, "%s:%u",
ip::address_v4{host4(ipp)}.to_string(),
port(ipp)):
is_v6(ipp)?
fmt::sprintf(buf, "%s:%u",
ip::address_v6{std::get<ipp.IP>(ipp)}.to_string(),
port(ipp)):
0
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const remote &remote)
{
const auto &ipp
{
static_cast<const ipport &>(remote)
};
if(!ipp && !remote.hostname)
{
const auto len{strlcpy(data(buf), "0.0.0.0", size(buf))};
return { data(buf), size_t(len) };
}
else if(!ipp)
{
const auto len{strlcpy(data(buf), remote.hostname, size(buf))};
return { data(buf), size_t(len) };
}
else return string(buf, ipp);
}
//
// remote
//
ircd::net::remote::remote(const hostport &hostport)
:ipport
{
resolve(hostport)
}
,hostname
{
hostport.host
}
{
}
2017-12-30 06:42:39 +01:00
std::ostream &
ircd::net::operator<<(std::ostream &s, const remote &t)
{
char buf[256];
s << string(buf, t);
return s;
}
//
// ipport
//
std::ostream &
ircd::net::operator<<(std::ostream &s, const ipport &t)
{
char buf[256];
s << string(buf, t);
return s;
}
ircd::net::ipport
ircd::net::make_ipport(const boost::asio::ip::tcp::endpoint &ep)
{
return ipport
{
ep.address(), ep.port()
};
}
boost::asio::ip::tcp::endpoint
ircd::net::make_endpoint(const ipport &ipport)
{
return
{
is_v6(ipport)? ip::tcp::endpoint
{
asio::ip::address_v6 { std::get<ipport.IP>(ipport) }, port(ipport)
}
: ip::tcp::endpoint
{
asio::ip::address_v4 { host4(ipport) }, port(ipport)
},
};
}
ircd::net::ipport::ipport(const boost::asio::ip::address &address,
const uint16_t &port)
{
std::get<TYPE>(*this) = address.is_v6();
if(is_v6(*this))
{
std::get<IP>(*this) = address.to_v6().to_bytes();
std::reverse(std::get<IP>(*this).begin(), std::get<IP>(*this).end());
}
else host4(*this) = address.to_v4().to_ulong();
net::port(*this) = port;
}
//
// hostport
//
std::ostream &
ircd::net::operator<<(std::ostream &s, const hostport &t)
{
char buf[256];
s << string(buf, t);
return s;
}
2017-12-30 06:45:15 +01:00
///////////////////////////////////////////////////////////////////////////////
//
// net/asio.h
//
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(256, char{});
const auto addr{string(net::addr(ep))};
const auto data{const_cast<char *>(ret.data())};
ret.resize(snprintf(data, ret.size(), "%s:%u", addr.c_str(), port(ep)));
return ret;
}
std::string
ircd::net::host(const ip::tcp::endpoint &ep)
{
return string(addr(ep));
}
boost::asio::ip::address
ircd::net::addr(const ip::tcp::endpoint &ep)
{
return ep.address();
}
uint16_t
ircd::net::port(const ip::tcp::endpoint &ep)
{
return ep.port();
}
///////////////////////////////////////////////////////////////////////////////
//
// asio.h
//
std::exception_ptr
ircd::make_eptr(const boost::system::error_code &ec)
{
return bool(ec)? std::make_exception_ptr(boost::system::system_error(ec)):
std::exception_ptr{};
}
2017-12-30 06:45:15 +01:00
std::string
ircd::string(const boost::system::system_error &e)
2017-12-30 06:45:15 +01:00
{
return string(e.code());
}
std::string
ircd::string(const boost::system::error_code &ec)
2017-12-30 06:45:15 +01:00
{
std::string ret(128, char{});
ret.resize(string(mutable_buffer{ret}, ec).size());
return ret;
}
ircd::string_view
ircd::string(const mutable_buffer &buf,
const boost::system::system_error &e)
2017-12-30 06:45:15 +01:00
{
return string(buf, e.code());
}
ircd::string_view
ircd::string(const mutable_buffer &buf,
const boost::system::error_code &ec)
2017-12-30 06:45:15 +01:00
{
const auto len
{
fmt::sprintf
{
buf, "%s: %s", ec.category().name(), ec.message()
}
};
return { data(buf), size_t(len) };
}
2017-09-30 08:04:41 +02:00
///////////////////////////////////////////////////////////////////////////////
//
// buffer.h - provide definition for the null buffers and asio conversion
//
const ircd::buffer::mutable_buffer
ircd::buffer::null_buffer
{
2018-01-01 23:27:11 +01:00
nullptr, nullptr
2017-09-30 08:04:41 +02:00
};
const ircd::ilist<ircd::buffer::mutable_buffer>
ircd::buffer::null_buffers
{{
2018-01-01 23:27:11 +01:00
null_buffer
2017-09-30 08:04:41 +02:00
}};
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)
};
}