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>
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-10-19 10:30:19 +02:00
|
|
|
// net/net.h
|
2017-09-30 08:04:41 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
ip::tcp::resolver *resolver;
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
struct ircd::log::log
|
|
|
|
ircd::net::log
|
|
|
|
{
|
|
|
|
"net", 'N'
|
|
|
|
};
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
/// Network subsystem initialization
|
|
|
|
///
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::init::init()
|
|
|
|
{
|
|
|
|
net::resolver = new ip::tcp::resolver{*ircd::ios};
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
/// Network subsystem shutdown
|
|
|
|
///
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::init::~init()
|
|
|
|
{
|
|
|
|
assert(net::resolver);
|
|
|
|
delete net::resolver;
|
|
|
|
net::resolver = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
//
|
|
|
|
// socket (public)
|
|
|
|
//
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-19 10:30:19 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/listener.h
|
|
|
|
//
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
struct ircd::net::listener::acceptor
|
2017-11-01 23:51:24 +01:00
|
|
|
: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;
|
2017-11-01 23:51:24 +01:00
|
|
|
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
|
2017-11-01 23:51:24 +01:00
|
|
|
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
|
2017-11-01 23:51:24 +01:00
|
|
|
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();
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
// Acceptor shutdown
|
|
|
|
bool interrupt() noexcept;
|
|
|
|
void join() noexcept;
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
acceptor(const json::object &opts);
|
|
|
|
~acceptor() noexcept;
|
|
|
|
};
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
{
|
2017-10-25 18:37:37 +02:00
|
|
|
//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.get<uint16_t>("port", 6667)
|
|
|
|
}
|
|
|
|
,a
|
|
|
|
{
|
|
|
|
*ircd::ios
|
|
|
|
}
|
|
|
|
{
|
2017-10-25 18:37:37 +02:00
|
|
|
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);
|
2017-10-25 18:37:37 +02:00
|
|
|
log.debug("%s listening (backlog: %lu, max connections: %zu)",
|
2017-09-30 08:04:41 +02:00
|
|
|
std::string(*this),
|
2017-10-25 18:37:37 +02:00
|
|
|
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
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
/// 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));
|
2017-10-25 18:37:37 +02:00
|
|
|
log.debug("%s: socket(%p) is the next socket to accept",
|
2017-09-30 08:04:41 +02:00
|
|
|
std::string(*this),
|
|
|
|
sock.get());
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
ip::tcp::socket &sd(*sock);
|
2017-11-01 23:51:24 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
/// 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,
|
2017-11-01 23:51:24 +01:00
|
|
|
const std::shared_ptr<socket> sock,
|
|
|
|
const std::weak_ptr<acceptor> a)
|
2017-09-30 08:04:41 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
if(unlikely(a.expired()))
|
2017-09-30 08:04:41 +02:00
|
|
|
return;
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
--accepting;
|
|
|
|
const unwind::nominal next{[this]
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
|
|
|
this->next();
|
|
|
|
}};
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
log.debug("%s: socket(%p) accepted %s",
|
2017-09-30 08:04:41 +02:00
|
|
|
std::string(*this),
|
2017-10-25 18:37:37 +02:00
|
|
|
sock.get(),
|
2017-09-30 08:04:41 +02:00
|
|
|
string(sock->remote()));
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
//ip::tcp::socket &sd(*sock);
|
|
|
|
|
2017-10-19 10:03:00 +02:00
|
|
|
//static const asio::socket_base::keep_alive keep_alive(true);
|
2017-10-25 18:37:37 +02:00
|
|
|
//sd.set_option(keep_alive);
|
2017-10-19 10:03:00 +02:00
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
//static const asio::socket_base::linger linger{true, 10};
|
|
|
|
//sd.set_option(linger);
|
2017-10-19 10:03:00 +02:00
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
//sd.non_blocking(false);
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
static const socket::handshake_type handshake_type
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
socket::handshake_type::server
|
|
|
|
};
|
|
|
|
|
|
|
|
auto handshake
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
std::bind(&acceptor::handshake, this, ph::_1, sock, a)
|
2017-09-30 08:04:41 +02:00
|
|
|
};
|
|
|
|
|
2017-10-19 10:03:00 +02:00
|
|
|
sock->ssl.async_handshake(handshake_type, std::move(handshake));
|
2017-11-01 23:51:24 +01:00
|
|
|
++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)
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
log.error("%s: socket(%p): in accept(): [%s]: %s",
|
2017-09-30 08:04:41 +02:00
|
|
|
std::string(*this),
|
|
|
|
sock.get(),
|
2017-11-01 23:51:24 +01:00
|
|
|
sock->connected()? string(sock->remote()) : "<gone>",
|
2017-09-30 08:04:41 +02:00
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
/// 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
|
2017-11-01 23:51:24 +01:00
|
|
|
ircd::net::listener::acceptor::accept_error(const error_code &ec,
|
|
|
|
socket &sock)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-11-06 21:26:47 +01:00
|
|
|
using namespace boost::system::errc;
|
2017-10-27 00:36:31 +02:00
|
|
|
using boost::system::get_system_category;
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
if(unlikely(interrupting))
|
|
|
|
throw ctx::interrupted();
|
|
|
|
|
2017-11-06 21:26:47 +01:00
|
|
|
if(likely(ec == success))
|
|
|
|
return false;
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
if(ec.category() == get_system_category()) switch(ec.value())
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
case operation_canceled:
|
2017-11-01 23:51:24 +01:00
|
|
|
return false;
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
default:
|
2017-10-27 00:36:31 +02:00
|
|
|
break;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-10-27 00:36:31 +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,
|
2017-11-01 23:51:24 +01:00
|
|
|
const std::shared_ptr<socket> sock,
|
|
|
|
const std::weak_ptr<acceptor> a)
|
2017-09-30 08:04:41 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
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-11-01 23:51:24 +01:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
log.debug("%s socket(%p): SSL handshook %s",
|
2017-09-30 08:04:41 +02:00
|
|
|
std::string(*this),
|
2017-10-25 18:37:37 +02:00
|
|
|
sock.get(),
|
2017-09-30 08:04:41 +02:00
|
|
|
string(sock->remote()));
|
|
|
|
|
|
|
|
add_client(sock);
|
|
|
|
}
|
2017-11-01 23:51:24 +01:00
|
|
|
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)
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
log.error("%s: socket(%p): in handshake(): [%s]: %s",
|
2017-09-30 08:04:41 +02:00
|
|
|
std::string(*this),
|
|
|
|
sock.get(),
|
2017-10-19 10:01:07 +02:00
|
|
|
sock->connected()? string(sock->remote()) : "<gone>",
|
2017-09-30 08:04:41 +02:00
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
/// 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
|
2017-11-01 23:51:24 +01:00
|
|
|
ircd::net::listener::acceptor::handshake_error(const error_code &ec,
|
|
|
|
socket &sock)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
using boost::system::get_system_category;
|
2017-11-06 21:26:47 +01:00
|
|
|
using namespace boost::system::errc;
|
2017-10-27 00:36:31 +02:00
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
if(unlikely(interrupting))
|
|
|
|
throw ctx::interrupted();
|
|
|
|
|
2017-11-06 21:26:47 +01:00
|
|
|
if(likely(ec == success))
|
|
|
|
return false;
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
if(ec.category() == get_system_category()) switch(ec.value())
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
case operation_canceled:
|
2017-11-01 23:51:24 +01:00
|
|
|
return false;
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
default:
|
2017-10-27 00:36:31 +02:00
|
|
|
break;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-10-27 00:36:31 +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));
|
|
|
|
|
|
|
|
ssl.set_options
|
|
|
|
(
|
2017-10-19 10:03:00 +02:00
|
|
|
//ssl.default_workarounds
|
2017-09-30 08:04:41 +02:00
|
|
|
//| ssl.no_tlsv1
|
|
|
|
//| ssl.no_tlsv1_1
|
2017-10-19 10:03:00 +02:00
|
|
|
//| ssl.no_tlsv1_2
|
2017-09-30 08:04:41 +02:00
|
|
|
//| ssl.no_sslv2
|
2017-10-19 10:03:00 +02:00
|
|
|
//| ssl.no_sslv3
|
|
|
|
ssl.single_dh_use
|
2017-09-30 08:04:41 +02:00
|
|
|
);
|
|
|
|
|
2017-10-19 10:03:00 +02: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"])
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
return fmt::snstringf
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-11-01 23:51:24 +01: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
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::net::hostport
|
|
|
|
ircd::net::local_hostport(const socket &socket)
|
2017-10-25 18:37:37 +02:00
|
|
|
noexcept try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
const auto &ep(socket.local());
|
2017-10-25 18:37:37 +02:00
|
|
|
return { host(ep), port(ep) };
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return { std::string{}, 0 };
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::hostport
|
|
|
|
ircd::net::remote_hostport(const socket &socket)
|
2017-10-25 18:37:37 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
const auto &ep(socket.remote());
|
|
|
|
return { host(ep), port(ep) };
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return { std::string{}, 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport
|
|
|
|
ircd::net::local_ipport(const socket &socket)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
const auto &ep(socket.local());
|
|
|
|
const auto &a(addr(ep));
|
|
|
|
|
|
|
|
ipport ret;
|
|
|
|
if(a.is_v6())
|
|
|
|
{
|
|
|
|
std::get<ret.IP>(ret) = a.to_v6().to_bytes();
|
|
|
|
std::reverse(std::get<ret.IP>(ret).begin(), std::get<ret.IP>(ret).end());
|
|
|
|
}
|
|
|
|
else host4(ret) = a.to_v4().to_ulong();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport
|
|
|
|
ircd::net::remote_ipport(const socket &socket)
|
|
|
|
noexcept try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
const auto &ep(socket.remote());
|
2017-10-25 18:37:37 +02:00
|
|
|
const auto &a(addr(ep));
|
|
|
|
|
|
|
|
ipport ret;
|
|
|
|
if(a.is_v6())
|
|
|
|
{
|
|
|
|
std::get<ret.IP>(ret) = a.to_v6().to_bytes();
|
|
|
|
std::reverse(std::get<ret.IP>(ret).begin(), std::get<ret.IP>(ret).end());
|
|
|
|
}
|
|
|
|
else host4(ret) = a.to_v4().to_ulong();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::net::available(const socket &s)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
const ip::tcp::socket &sd(s);
|
|
|
|
return sd.available(ec);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::connected(const socket &s)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return s.connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// socket::io
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::socket::io::io(struct socket &sock,
|
|
|
|
struct stat &stat,
|
|
|
|
const std::function<size_t ()> &closure)
|
2017-10-19 12:55:24 +02:00
|
|
|
:io
|
|
|
|
{
|
|
|
|
sock, stat, closure()
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::net::socket::io::io(struct socket &sock,
|
|
|
|
struct stat &stat,
|
|
|
|
const size_t &bytes)
|
2017-09-30 08:04:41 +02:00
|
|
|
:sock{sock}
|
|
|
|
,stat{stat}
|
2017-10-19 12:55:24 +02:00
|
|
|
,bytes{bytes}
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
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}
|
|
|
|
{
|
2017-10-25 18:37:37 +02:00
|
|
|
socket.set_timeout(timeout);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::socket::scope_timeout::scope_timeout(socket &socket,
|
|
|
|
const milliseconds &timeout,
|
2017-10-19 12:55:24 +02:00
|
|
|
socket::handler handler)
|
2017-09-30 08:04:41 +02:00
|
|
|
:s{&socket}
|
|
|
|
{
|
2017-10-19 12:55:24 +02:00
|
|
|
socket.set_timeout(timeout, std::move(handler));
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
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)
|
2017-09-30 08:04:41 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2017-10-19 12:55:24 +02:00
|
|
|
this->~scope_timeout();
|
|
|
|
s = std::move(other.s);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::socket::scope_timeout::~scope_timeout()
|
2017-11-01 23:51:24 +01:00
|
|
|
noexcept
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
cancel();
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::socket::scope_timeout::cancel()
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
if(!this->s)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto *const s{this->s};
|
|
|
|
this->s = nullptr;
|
2017-11-01 23:51:24 +01:00
|
|
|
s->cancel_timeout();
|
2017-10-19 12:55:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-10-25 19:02:45 +02:00
|
|
|
log.error("socket(%p) scope_timeout::cancel: %s",
|
|
|
|
(const void *)s,
|
|
|
|
e.what());
|
2017-10-19 12:55:24 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::socket::scope_timeout::release()
|
|
|
|
{
|
|
|
|
const auto s{this->s};
|
|
|
|
this->s = nullptr;
|
|
|
|
return s != nullptr;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
std::shared_ptr<ircd::net::socket>
|
|
|
|
ircd::net::connect(const net::remote &remote,
|
|
|
|
const milliseconds &timeout)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
const asio::ip::tcp::endpoint ep
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
is_v6(remote)? asio::ip::tcp::endpoint
|
|
|
|
{
|
|
|
|
asio::ip::address_v6 { std::get<remote.IP>(remote) }, port(remote)
|
|
|
|
}
|
|
|
|
: asio::ip::tcp::endpoint
|
|
|
|
{
|
|
|
|
asio::ip::address_v4 { host4(remote) }, port(remote)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return connect(ep, timeout);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-10-27 00:36:31 +02:00
|
|
|
|
|
|
|
std::shared_ptr<ircd::net::socket>
|
|
|
|
ircd::net::connect(const ip::tcp::endpoint &remote,
|
|
|
|
const milliseconds &timeout)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
const auto ret(std::make_shared<socket>());
|
|
|
|
ret->connect(remote, timeout);
|
|
|
|
return ret;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
bool
|
|
|
|
ircd::net::disconnect(socket &socket,
|
|
|
|
const dc &type)
|
|
|
|
noexcept try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
socket.disconnect(type);
|
|
|
|
return true;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-10-27 00:36:31 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
log::error("socket(%p): disconnect: type: %d: %s",
|
|
|
|
this,
|
|
|
|
int(type),
|
|
|
|
e.what());
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// socket
|
|
|
|
//
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
ircd::net::socket::socket(asio::ssl::context &ssl,
|
|
|
|
boost::asio::io_service *const &ios)
|
2017-10-25 18:37:37 +02:00
|
|
|
:sd
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-25 18:37:37 +02:00
|
|
|
*ios
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-10-25 18:37:37 +02:00
|
|
|
,ssl
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-25 18:37:37 +02:00
|
|
|
this->sd, ssl
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
,timer
|
|
|
|
{
|
|
|
|
*ios
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
/// 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()
|
2017-10-16 06:28:40 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
if(unlikely(RB_DEBUG_LEVEL && connected()))
|
|
|
|
log.critical("Failed to ensure socket(%p) is disconnected from %s before dtor.",
|
|
|
|
this,
|
|
|
|
string(remote()));
|
|
|
|
|
|
|
|
assert(!connected());
|
2017-10-16 06:28:40 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
log.critical("socket(%p): close: %s", this, e.what());
|
2017-10-16 06:28:40 +02:00
|
|
|
return;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
/// Attempt to connect and ssl handshake remote; yields ircd::ctx; throws timeout
|
|
|
|
///
|
2017-09-30 08:04:41 +02:00
|
|
|
void
|
|
|
|
ircd::net::socket::connect(const ip::tcp::endpoint &ep,
|
|
|
|
const milliseconds &timeout)
|
2017-10-25 19:02:45 +02:00
|
|
|
try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
const life_guard<socket> lg{*this};
|
|
|
|
const scope_timeout ts{*this, timeout};
|
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),
|
|
|
|
timeout.count());
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
sd.async_connect(ep, yield_context{to_asio{}});
|
2017-10-25 19:02:45 +02:00
|
|
|
log.debug("socket(%p) connected to remote: %s from local: %s; performing handshake...",
|
|
|
|
this,
|
|
|
|
string(ep),
|
|
|
|
string(local()));
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ssl.async_handshake(socket::handshake_type::client, yield_context{to_asio{}});
|
2017-10-25 19:02:45 +02:00
|
|
|
log.debug("socket(%p) secure session with %s from local: %s established.",
|
|
|
|
this,
|
|
|
|
string(ep),
|
|
|
|
string(local()));
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log.debug("socket(%p) failed to connect to remote %s: %s",
|
|
|
|
this,
|
|
|
|
string(ep),
|
|
|
|
e.what());
|
2017-10-27 00:36:31 +02:00
|
|
|
|
|
|
|
disconnect(dc::RST);
|
|
|
|
throw;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
/// Attempt to connect and ssl handshake remote; yields ircd::ctx; throws timeout
|
|
|
|
///
|
|
|
|
void
|
|
|
|
ircd::net::socket::connect(const net::remote &remote,
|
|
|
|
const milliseconds &timeout)
|
|
|
|
{
|
|
|
|
const ip::tcp::endpoint ep
|
|
|
|
{
|
|
|
|
is_v6(remote)? asio::ip::tcp::endpoint
|
|
|
|
{
|
|
|
|
asio::ip::address_v6 { std::get<remote.IP>(remote) }, port(remote)
|
|
|
|
}
|
|
|
|
: asio::ip::tcp::endpoint
|
|
|
|
{
|
|
|
|
asio::ip::address_v4 { host4(remote) }, port(remote)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this->connect(ep, timeout);
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
/// Attempt to connect and ssl handshake; asynchronous, callback when done.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
ircd::net::socket::connect(const ip::tcp::endpoint &ep,
|
|
|
|
const milliseconds &timeout,
|
|
|
|
handler callback)
|
|
|
|
{
|
|
|
|
auto handshake_handler{[this, callback(std::move(callback))]
|
|
|
|
(const error_code &ec)
|
|
|
|
noexcept
|
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
if(timedout)
|
|
|
|
assert(ec == boost::system::errc::operation_canceled);
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
if(!timedout)
|
|
|
|
cancel_timeout();
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
callback(ec);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
log.critical("socket(%p): connect: unhandled exception from user callback: %s",
|
|
|
|
(const void *)this,
|
|
|
|
e.what());
|
2017-10-27 00:36:31 +02:00
|
|
|
}
|
2017-10-19 12:55:24 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
auto connect_handler{[this, handshake_handler(std::move(handshake_handler))]
|
|
|
|
(const error_code &ec)
|
|
|
|
noexcept
|
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
// Even though the branch on ec below should cancel the timeout on
|
|
|
|
// error, the timeout still needs to be canceled if else anything bad
|
|
|
|
// happens in the remainder of this frame too.
|
|
|
|
const unwind::exceptional cancels{[this]
|
|
|
|
{
|
|
|
|
cancel_timeout();
|
|
|
|
}};
|
|
|
|
|
|
|
|
// A connect error
|
2017-10-19 12:55:24 +02:00
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
handshake_handler(ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const auto handshake{socket::handshake_type::client};
|
|
|
|
ssl.async_handshake(handshake, std::move(handshake_handler));
|
|
|
|
}};
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
set_timeout(timeout);
|
2017-11-01 23:51:24 +01:00
|
|
|
sd.async_connect(ep, std::move(connect_handler));
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
bool
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::disconnect(const dc &type)
|
2017-10-25 18:37:37 +02:00
|
|
|
try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
if(timer.expires_from_now() > 0ms)
|
|
|
|
timer.cancel();
|
|
|
|
|
2017-10-16 06:28:40 +02:00
|
|
|
if(sd.is_open())
|
2017-10-25 19:02:45 +02:00
|
|
|
log.debug("socket(%p): disconnect: %s type: %d",
|
|
|
|
(const void *)this,
|
|
|
|
string(remote()),
|
|
|
|
uint(type));
|
2017-10-16 06:28:40 +02:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
if(sd.is_open()) switch(type)
|
|
|
|
{
|
|
|
|
default:
|
2017-10-19 10:02:30 +02:00
|
|
|
case dc::RST:
|
|
|
|
sd.close();
|
2017-10-27 00:36:31 +02:00
|
|
|
return true;
|
2017-10-19 10:02:30 +02:00
|
|
|
|
|
|
|
case dc::FIN:
|
|
|
|
sd.shutdown(ip::tcp::socket::shutdown_both);
|
2017-10-27 00:36:31 +02:00
|
|
|
return true;
|
2017-10-19 10:02:30 +02:00
|
|
|
|
|
|
|
case dc::FIN_SEND:
|
|
|
|
sd.shutdown(ip::tcp::socket::shutdown_send);
|
2017-10-27 00:36:31 +02:00
|
|
|
return true;
|
2017-10-19 10:02:30 +02:00
|
|
|
|
|
|
|
case dc::FIN_RECV:
|
|
|
|
sd.shutdown(ip::tcp::socket::shutdown_receive);
|
2017-10-27 00:36:31 +02:00
|
|
|
return true;
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
case dc::SSL_NOTIFY_YIELD: if(likely(ctx::current))
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
const life_guard<socket> lg{*this};
|
|
|
|
const scope_timeout ts{*this, 8s};
|
2017-10-25 18:37:37 +02:00
|
|
|
ssl.async_shutdown(yield_context{to_asio{}});
|
2017-11-01 23:51:24 +01:00
|
|
|
error_code ec;
|
|
|
|
sd.close(ec);
|
|
|
|
if(ec)
|
|
|
|
log.error("socket(%p): close: %s: %s",
|
|
|
|
this,
|
|
|
|
string(ec));
|
2017-10-27 00:36:31 +02:00
|
|
|
return true;
|
2017-10-25 18:37:37 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
case dc::SSL_NOTIFY:
|
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
set_timeout(8s);
|
2017-10-19 10:02:30 +02:00
|
|
|
ssl.async_shutdown([s(shared_from_this())]
|
2017-11-01 23:51:24 +01:00
|
|
|
(error_code ec)
|
|
|
|
noexcept
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
if(!s->timedout)
|
|
|
|
s->cancel_timeout();
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
if(ec)
|
2017-10-27 00:36:31 +02:00
|
|
|
log.warning("socket(%p): SSL_NOTIFY: %s: %s",
|
2017-10-25 19:02:45 +02:00
|
|
|
s.get(),
|
2017-11-01 23:51:24 +01:00
|
|
|
string(ec));
|
2017-10-27 00:36:31 +02:00
|
|
|
|
|
|
|
if(!s->sd.is_open())
|
2017-10-25 18:37:37 +02:00
|
|
|
return;
|
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
s->sd.close(ec);
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
if(ec)
|
2017-10-27 00:36:31 +02:00
|
|
|
log.warning("socket(%p): after SSL_NOTIFY: %s: %s",
|
2017-10-25 19:02:45 +02:00
|
|
|
s.get(),
|
2017-11-01 23:51:24 +01:00
|
|
|
string(ec));
|
2017-09-30 08:04:41 +02:00
|
|
|
});
|
2017-10-27 00:36:31 +02:00
|
|
|
return true;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 00:36:31 +02:00
|
|
|
else return false;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-10-25 18:37:37 +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());
|
2017-10-27 00:36:31 +02:00
|
|
|
|
|
|
|
if(!sd.is_open())
|
|
|
|
throw;
|
|
|
|
|
|
|
|
boost::system::error_code ec;
|
|
|
|
sd.close(ec);
|
|
|
|
|
|
|
|
if(ec)
|
|
|
|
log.warning("socket(%p): after disconnect: %s: %s",
|
|
|
|
this,
|
2017-11-01 23:51:24 +01:00
|
|
|
string(ec));
|
2017-10-25 18:37:37 +02:00
|
|
|
throw;
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2017-10-19 10:01:07 +02:00
|
|
|
bool
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::cancel()
|
2017-10-19 10:01:07 +02:00
|
|
|
noexcept
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-19 10:01:07 +02:00
|
|
|
boost::system::error_code ec[2];
|
2017-10-25 18:37:37 +02:00
|
|
|
sd.cancel(ec[0]);
|
|
|
|
timer.cancel(ec[1]);
|
2017-10-19 10:01:07 +02:00
|
|
|
|
|
|
|
return std::all_of(begin(ec), end(ec), [](const auto &ec)
|
|
|
|
{
|
|
|
|
return ec == boost::system::errc::success;
|
|
|
|
});
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
ircd::net::socket::operator()(const milliseconds &timeout,
|
|
|
|
handler callback)
|
|
|
|
{
|
|
|
|
static const auto flags
|
|
|
|
{
|
|
|
|
ip::tcp::socket::message_peek
|
|
|
|
};
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
static char buffer[0];
|
2017-09-30 08:04:41 +02:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
assert(connected());
|
2017-09-30 08:04:41 +02:00
|
|
|
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)
|
2017-10-19 10:01:07 +02:00
|
|
|
noexcept try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-19 10:01:07 +02:00
|
|
|
const life_guard<socket> s{wp};
|
2017-11-01 23:51:24 +01:00
|
|
|
log.debug("socket(%p): %zu bytes: %s: %s",
|
|
|
|
this,
|
|
|
|
bytes,
|
|
|
|
string(ec));
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
// 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)
|
2017-11-01 23:51:24 +01:00
|
|
|
cancel_timeout();
|
2017-09-30 08:04:41 +02:00
|
|
|
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))
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
log.error("socket(%p): %s",
|
|
|
|
this,
|
|
|
|
string(ec));
|
2017-09-30 08:04:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
call_user(callback, ec);
|
|
|
|
}
|
2017-10-19 10:01:07 +02:00
|
|
|
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());
|
2017-10-19 10:01:07 +02:00
|
|
|
assert(0);
|
|
|
|
}
|
2017-11-01 23:51:24 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
log.error("socket(%p): handle: %s %s",
|
|
|
|
this,
|
|
|
|
string(ec));
|
|
|
|
assert(0);
|
|
|
|
}
|
2017-10-19 10:01:07 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-10-25 19:02:45 +02:00
|
|
|
log.error("socket(%p): handle: %s",
|
|
|
|
this,
|
|
|
|
e.what());
|
2017-10-19 10:01:07 +02:00
|
|
|
assert(0);
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::socket::call_user(const handler &callback,
|
|
|
|
const error_code &ec)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
callback(ec);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
log.critical("socket(%p): async handler: unhandled exception: %s",
|
|
|
|
this,
|
|
|
|
e.what());
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::socket::handle_error(const error_code &ec)
|
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
2017-10-27 00:36:31 +02:00
|
|
|
using boost::system::get_system_category;
|
|
|
|
using boost::asio::error::get_ssl_category;
|
|
|
|
using boost::asio::error::get_misc_category;
|
|
|
|
|
2017-11-06 21:26:47 +01:00
|
|
|
if(likely(ec == success))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
log.warning("socket(%p): handle error: %s: %s",
|
|
|
|
this,
|
|
|
|
string(ec));
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
if(ec.category() == get_system_category()) switch(ec.value())
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
// 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 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;
|
|
|
|
}
|
2017-10-27 00:36:31 +02:00
|
|
|
else if(ec.category() == get_misc_category()) switch(ec.value())
|
|
|
|
{
|
|
|
|
// This indicates the remote closed the socket, we still
|
|
|
|
// pass this up to the user so they can know that too.
|
|
|
|
case boost::asio::error::eof:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(ec.category() == get_ssl_category()) switch(ec.value())
|
|
|
|
{
|
|
|
|
// Docs say this means we read less bytes off the socket than desired.
|
|
|
|
case SSL_R_SHORT_READ:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return true;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::socket::handle_timeout(const std::weak_ptr<socket> wp,
|
|
|
|
const error_code &ec)
|
2017-10-19 10:01:07 +02:00
|
|
|
noexcept try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
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:
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2017-10-19 10:01:07 +02:00
|
|
|
sd.cancel();
|
2017-11-01 23:51:24 +01:00
|
|
|
assert(timedout == false);
|
2017-10-25 18:37:37 +02:00
|
|
|
timedout = true;
|
2017-09-30 08:04:41 +02:00
|
|
|
break;
|
2017-10-25 18:37:37 +02:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
// A cancelation means there was no timeout.
|
|
|
|
case operation_canceled:
|
2017-11-01 23:51:24 +01:00
|
|
|
{
|
2017-10-27 00:36:31 +02:00
|
|
|
assert(ec.category() == boost::system::get_system_category());
|
2017-11-01 23:51:24 +01:00
|
|
|
assert(timedout == false);
|
2017-09-30 08:04:41 +02:00
|
|
|
timedout = false;
|
|
|
|
break;
|
2017-11-01 23:51:24 +01:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
// All other errors are unexpected, logged and ignored here.
|
|
|
|
default:
|
2017-10-19 10:01:07 +02:00
|
|
|
throw boost::system::system_error(ec);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-19 10:01:07 +02:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2017-10-25 19:02:45 +02:00
|
|
|
log.error("socket(%p): handle_timeout: unexpected: %s\n",
|
|
|
|
(const void *)this,
|
2017-10-27 00:36:31 +02:00
|
|
|
e.what());
|
2017-10-19 10:01:07 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-10-25 19:02:45 +02:00
|
|
|
log.error("socket(%p): handle timeout: %s",
|
|
|
|
(const void *)this,
|
|
|
|
e.what());
|
2017-10-19 10:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::available()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return sd.available();
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::socket::connected()
|
|
|
|
const noexcept try
|
|
|
|
{
|
|
|
|
return sd.is_open();
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:55:24 +02:00
|
|
|
ircd::net::error_code
|
|
|
|
ircd::net::socket::cancel_timeout()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
2017-11-01 23:51:24 +01:00
|
|
|
timedout = false;
|
2017-10-19 12:55:24 +02:00
|
|
|
timer.cancel(ec);
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
void
|
|
|
|
ircd::net::socket::set_timeout(const milliseconds &t)
|
|
|
|
{
|
2017-11-01 23:51:24 +01: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::bind(&socket::handle_timeout, this, weak_from(*this), ph::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::socket::set_timeout(const milliseconds &t,
|
2017-10-19 12:55:24 +02:00
|
|
|
handler h)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-11-01 23:51:24 +01: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));
|
|
|
|
}
|
|
|
|
|
2017-11-01 23:51:24 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return string(e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
std::string ret(128, char{});
|
|
|
|
ret.resize(string(mutable_buffer{ret}, ec).size());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &buf,
|
|
|
|
const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return string(buf, e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &buf,
|
|
|
|
const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
const auto len
|
|
|
|
{
|
|
|
|
fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s: %s", ec.category().name(), ec.message()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return { data(buf), size_t(len) };
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/remote.h
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// host / port utils
|
|
|
|
//
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const hostport &t)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
s << string(t, buf);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const ipport &t)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
s << string(t, buf);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const remote &t)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
s << string(t, buf);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
template<class T> std::string _string(const T &t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
std::string
|
|
|
|
ircd::net::_string(const T &t)
|
|
|
|
{
|
|
|
|
std::string ret(256, char{});
|
|
|
|
ret.resize(net::string(t, mutable_buffer{ret}).size());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const uint32_t &t)
|
|
|
|
{
|
|
|
|
return _string(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const uint128_t &t)
|
|
|
|
{
|
|
|
|
return _string(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const hostport &t)
|
|
|
|
{
|
|
|
|
return _string(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const ipport &t)
|
|
|
|
{
|
|
|
|
return _string(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const remote &t)
|
|
|
|
{
|
|
|
|
return _string(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const uint32_t &ip,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
const auto len
|
|
|
|
{
|
|
|
|
fmt::sprintf(buf, "%s:%u", ip::address_v4{ip}.to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
return { data(buf), size_t(len) };
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const uint128_t &ip,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
fmt::sprintf(buf, "%s:%u", ip::address_v6{punpun}.to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
return { data(buf), size_t(len) };
|
|
|
|
}
|
|
|
|
|
|
|
|
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) };
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const ipport &ipp,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
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 remote &remote,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
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(ipp, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// remote
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::remote::remote(hostport hp)
|
|
|
|
:remote{std::move(hp.first), hp.second}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::remote::remote(const string_view &host,
|
|
|
|
const uint16_t &port)
|
|
|
|
:remote
|
|
|
|
{
|
|
|
|
std::string(host), std::string(lex_cast(port))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::remote::remote(const string_view &host,
|
|
|
|
const string_view &port)
|
|
|
|
:remote
|
|
|
|
{
|
|
|
|
std::string(host), std::string(port)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::remote::remote(std::string host,
|
|
|
|
const uint16_t &port)
|
|
|
|
:ipport{host, port}
|
|
|
|
,hostname{std::move(host)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::remote::remote(std::string host,
|
|
|
|
const std::string &port)
|
|
|
|
:ipport{host, port}
|
|
|
|
,hostname{std::move(host)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::remote::remote(const ipport &ipp)
|
|
|
|
:ipport{ipp}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// ipport
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const hostport &hp)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
hp.first, std::string(lex_cast(port(hp)))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const string_view &host,
|
|
|
|
const uint16_t &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
std::string(host), std::string(lex_cast(port))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const string_view &host,
|
|
|
|
const string_view &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
std::string(host), std::string(port)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const std::string &host,
|
|
|
|
const uint16_t &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
host, std::string{lex_cast(port)}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const std::string &host,
|
|
|
|
const std::string &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
uint32_t{0},
|
|
|
|
lex_cast<uint16_t>(port)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assert(resolver);
|
|
|
|
const ip::tcp::resolver::query query
|
|
|
|
{
|
|
|
|
host, 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);
|
|
|
|
|
|
|
|
const ip::tcp::endpoint &ep
|
|
|
|
{
|
|
|
|
*epit
|
|
|
|
};
|
|
|
|
|
|
|
|
const asio::ip::address &address
|
|
|
|
{
|
|
|
|
ep.address()
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2017-10-25 19:02:45 +02:00
|
|
|
log.debug("resolved remote %s:%u => %s %s",
|
|
|
|
host,
|
|
|
|
net::port(*this),
|
|
|
|
is_v6(*this)? "IP6" : "IP4",
|
|
|
|
string(*this));
|
2017-10-25 18:37:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// hostport
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::hostport::hostport(std::string s,
|
|
|
|
const uint16_t &port)
|
|
|
|
try
|
|
|
|
:std::pair<std::string, uint16_t>
|
|
|
|
{
|
|
|
|
std::move(s), port
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(port != 8448)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//TODO: ipv6
|
|
|
|
const auto port_suffix
|
|
|
|
{
|
|
|
|
rsplit(first, ':').second
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!port_suffix.empty() && port_suffix != "8448")
|
|
|
|
second = lex_cast<uint16_t>(port_suffix);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw net::invalid_argument
|
|
|
|
{
|
|
|
|
"Supplied host name and/or port number: ", e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|