2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
#include <ircd/asio.h>
|
|
|
|
|
2018-03-13 02:45:21 +01:00
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
ctx::dock dock;
|
|
|
|
|
|
|
|
void wait_close_sockets();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::wait_close_sockets()
|
|
|
|
{
|
2018-05-12 06:25:50 +02:00
|
|
|
while(socket::instances)
|
2018-03-27 01:52:14 +02:00
|
|
|
if(!dock.wait_for(seconds(2)))
|
2018-09-30 02:28:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "Waiting for %zu sockets to destruct", socket::instances
|
|
|
|
};
|
2018-03-13 02:45:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:37:13 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Network subsystem initialization
|
|
|
|
ircd::net::init::init()
|
|
|
|
{
|
|
|
|
sslv23_client.set_verify_mode(asio::ssl::verify_peer);
|
|
|
|
sslv23_client.set_default_verify_paths();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Network subsystem shutdown
|
|
|
|
ircd::net::init::~init()
|
2018-04-29 00:31:07 +02:00
|
|
|
noexcept
|
2018-01-28 18:37:13 +01:00
|
|
|
{
|
2018-03-13 02:45:21 +01:00
|
|
|
wait_close_sockets();
|
2018-01-28 18:37:13 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2017-10-25 18:37:37 +02:00
|
|
|
struct ircd::log::log
|
|
|
|
ircd::net::log
|
|
|
|
{
|
|
|
|
"net", 'N'
|
|
|
|
};
|
|
|
|
|
2018-05-11 05:57:59 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::peer_cert_der_sha256_b64(const mutable_buffer &buf,
|
|
|
|
const socket &socket)
|
|
|
|
{
|
|
|
|
thread_local char shabuf[sha256::digest_size];
|
|
|
|
|
|
|
|
const auto hash
|
|
|
|
{
|
|
|
|
peer_cert_der_sha256(shabuf, socket)
|
|
|
|
};
|
|
|
|
|
|
|
|
return b64encode_unpadded(buf, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::net::peer_cert_der_sha256(const mutable_buffer &buf,
|
|
|
|
const socket &socket)
|
|
|
|
{
|
|
|
|
thread_local char derbuf[16384];
|
|
|
|
|
|
|
|
sha256
|
|
|
|
{
|
|
|
|
buf, peer_cert_der(derbuf, socket)
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
data(buf), sha256::digest_size
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::net::peer_cert_der(const mutable_buffer &buf,
|
2018-01-07 06:34:02 +01:00
|
|
|
const socket &socket)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
const SSL &ssl(socket);
|
2018-05-11 05:57:59 +02:00
|
|
|
const X509 &cert
|
|
|
|
{
|
|
|
|
openssl::peer_cert(ssl)
|
|
|
|
};
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
return openssl::i2d(buf, cert);
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 02:18:31 +02:00
|
|
|
std::pair<size_t, size_t>
|
|
|
|
ircd::net::calls(const socket &socket)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
socket.in.calls, socket.out.calls
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<size_t, size_t>
|
|
|
|
ircd::net::bytes(const socket &socket)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
socket.in.bytes, socket.out.bytes
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:59:36 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::loghead(const socket &socket)
|
|
|
|
{
|
|
|
|
thread_local char buf[512];
|
|
|
|
return loghead(buf, socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::loghead(const mutable_buffer &out,
|
|
|
|
const socket &socket)
|
|
|
|
{
|
|
|
|
thread_local char buf[2][128];
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "socket:%lu local[%s] remote[%s]",
|
2018-12-12 18:05:15 +01:00
|
|
|
id(socket),
|
2018-12-12 17:59:36 +01:00
|
|
|
string(buf[0], local_ipport(socket)),
|
|
|
|
string(buf[1], remote_ipport(socket)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::ipport
|
|
|
|
ircd::net::remote_ipport(const socket &socket)
|
2018-01-02 01:16:39 +01:00
|
|
|
noexcept try
|
2017-11-16 02:27:36 +01:00
|
|
|
{
|
2018-01-25 03:09:58 +01:00
|
|
|
const auto &ep(socket.remote());
|
2018-01-07 06:34:02 +01:00
|
|
|
return make_ipport(ep);
|
2018-01-02 01:16:39 +01:00
|
|
|
}
|
2018-01-07 06:34:02 +01:00
|
|
|
catch(...)
|
2018-01-02 01:16:39 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return {};
|
2017-11-16 02:27:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::ipport
|
|
|
|
ircd::net::local_ipport(const socket &socket)
|
|
|
|
noexcept try
|
2018-01-05 00:14:13 +01:00
|
|
|
{
|
2018-01-25 03:09:58 +01:00
|
|
|
const auto &ep(socket.local());
|
2018-01-07 06:34:02 +01:00
|
|
|
return make_ipport(ep);
|
2018-01-05 00:14:13 +01:00
|
|
|
}
|
2018-01-07 06:34:02 +01:00
|
|
|
catch(...)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return {};
|
|
|
|
}
|
2018-01-05 00:14:13 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
size_t
|
|
|
|
ircd::net::available(const socket &socket)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const ip::tcp::socket &sd(socket);
|
|
|
|
boost::system::error_code ec;
|
|
|
|
return sd.available(ec);
|
|
|
|
}
|
2017-11-16 02:27:36 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
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();
|
|
|
|
}
|
2017-11-16 02:27:36 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
bool
|
2018-02-27 07:49:44 +01:00
|
|
|
ircd::net::opened(const socket &socket)
|
2018-01-07 06:34:02 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
const ip::tcp::socket &sd(socket);
|
|
|
|
return sd.is_open();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return false;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 02:15:45 +02:00
|
|
|
const uint64_t &
|
|
|
|
ircd::net::id(const socket &socket)
|
|
|
|
{
|
|
|
|
return socket.id;
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/write.h
|
|
|
|
//
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
void
|
|
|
|
ircd::net::flush(socket &socket)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-06 02:01:37 +01:00
|
|
|
if(nodelay(socket))
|
|
|
|
return;
|
|
|
|
|
|
|
|
nodelay(socket, true);
|
|
|
|
nodelay(socket, false);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Yields ircd::ctx until all buffers are sent.
|
|
|
|
///
|
|
|
|
/// This is blocking behavior; use this if the following are true:
|
|
|
|
///
|
|
|
|
/// * You put a timer on the socket so if the remote slows us down the data
|
|
|
|
/// will not occupy the daemon's memory for a long time. Remember, *all* of
|
|
|
|
/// the data will be sitting in memory even after some of it was ack'ed by
|
|
|
|
/// the remote.
|
|
|
|
///
|
|
|
|
/// * You are willing to dedicate the ircd::ctx to sending all the data to
|
|
|
|
/// the remote. The ircd::ctx will be yielding until everything is sent.
|
|
|
|
///
|
2017-09-30 08:04:41 +02:00
|
|
|
size_t
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::write_all(socket &socket,
|
|
|
|
const vector_view<const const_buffer> &buffers)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return socket.write_all(buffers);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 10:46:50 +01:00
|
|
|
/// Yields ircd::ctx until at least some buffers are sent.
|
|
|
|
///
|
|
|
|
/// This is blocking behavior; use this if the following are true:
|
|
|
|
///
|
|
|
|
/// * You put a timer on the socket so if the remote slows us down the data
|
|
|
|
/// will not occupy the daemon's memory for a long time.
|
|
|
|
///
|
|
|
|
/// * You are willing to dedicate the ircd::ctx to sending the data to
|
|
|
|
/// the remote. The ircd::ctx will be yielding until the kernel has at least
|
|
|
|
/// some space to consume at least something from the supplied buffers.
|
|
|
|
///
|
|
|
|
size_t
|
|
|
|
ircd::net::write_few(socket &socket,
|
|
|
|
const vector_view<const const_buffer> &buffers)
|
|
|
|
{
|
|
|
|
return socket.write_few(buffers);
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Writes as much as possible until one of the following is true:
|
|
|
|
///
|
|
|
|
/// * The kernel buffer for the socket is full.
|
|
|
|
/// * The user buffer is exhausted.
|
|
|
|
///
|
|
|
|
/// This is non-blocking behavior. No yielding will take place; no timer is
|
|
|
|
/// needed. Multiple syscalls will be composed to fulfill the above points.
|
|
|
|
///
|
2017-09-30 08:04:41 +02:00
|
|
|
size_t
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::write_any(socket &socket,
|
|
|
|
const vector_view<const const_buffer> &buffers)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return socket.write_any(buffers);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Writes one "unit" of data or less; never more. The size of that unit
|
|
|
|
/// is determined by the system. Less may be written if one of the following
|
|
|
|
/// is true:
|
|
|
|
///
|
|
|
|
/// * The kernel buffer for the socket is full.
|
|
|
|
/// * The user buffer is exhausted.
|
|
|
|
///
|
|
|
|
/// If neither are true, more can be written using additional calls;
|
|
|
|
/// alternatively, use other variants of write_ for that.
|
|
|
|
///
|
|
|
|
/// This is non-blocking behavior. No yielding will take place; no timer is
|
|
|
|
/// needed. Only one syscall will occur.
|
|
|
|
///
|
2017-09-30 08:04:41 +02:00
|
|
|
size_t
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::write_one(socket &socket,
|
|
|
|
const vector_view<const const_buffer> &buffers)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return socket.write_one(buffers);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/read.h
|
|
|
|
//
|
|
|
|
|
2018-01-12 03:38:29 +01:00
|
|
|
/// Yields ircd::ctx until len bytes have been received and discarded from the
|
|
|
|
/// socket.
|
|
|
|
///
|
|
|
|
size_t
|
|
|
|
ircd::net::discard_all(socket &socket,
|
|
|
|
const size_t &len)
|
|
|
|
{
|
|
|
|
static char buffer[512] alignas(16);
|
|
|
|
|
|
|
|
size_t remain{len}; while(remain)
|
|
|
|
{
|
|
|
|
const mutable_buffer mb
|
|
|
|
{
|
|
|
|
buffer, std::min(remain, sizeof(buffer))
|
|
|
|
};
|
|
|
|
|
|
|
|
__builtin_prefetch(data(mb), 1, 0); // 1 = write, 0 = no cache
|
2018-01-14 04:19:29 +01:00
|
|
|
remain -= read_all(socket, mb);
|
2018-01-12 03:38:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-01-14 05:17:17 +01:00
|
|
|
/// Non-blocking discard of up to len bytes. The amount of bytes discarded
|
|
|
|
/// is returned. Zero is only returned if len==0 because the EAGAIN is
|
|
|
|
/// thrown. If any bytes have been discarded any EAGAIN encountered in
|
|
|
|
/// this function's internal loop is not thrown, but used to exit the loop.
|
|
|
|
///
|
|
|
|
size_t
|
|
|
|
ircd::net::discard_any(socket &socket,
|
|
|
|
const size_t &len)
|
|
|
|
{
|
|
|
|
static char buffer[512] alignas(16);
|
|
|
|
|
|
|
|
size_t remain{len}; while(remain) try
|
|
|
|
{
|
|
|
|
const mutable_buffer mb
|
|
|
|
{
|
|
|
|
buffer, std::min(remain, sizeof(buffer))
|
|
|
|
};
|
|
|
|
|
|
|
|
__builtin_prefetch(data(mb), 1, 0); // 1 = write, 0 = no cache
|
|
|
|
remain -= read_one(socket, mb);
|
|
|
|
}
|
2018-11-09 06:18:39 +01:00
|
|
|
catch(const std::system_error &e)
|
2018-01-14 05:17:17 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
if(e.code() == std::errc::resource_unavailable_try_again)
|
2018-01-14 05:17:17 +01:00
|
|
|
if(remain <= len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len - remain;
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Yields ircd::ctx until buffers are full.
|
|
|
|
///
|
|
|
|
/// Use this only if the following are true:
|
|
|
|
///
|
|
|
|
/// * You know the remote has made a guarantee to send you a specific amount
|
|
|
|
/// of data.
|
|
|
|
///
|
|
|
|
/// * You put a timer on the socket so that if the remote runs short this
|
|
|
|
/// call doesn't hang the ircd::ctx forever, otherwise it will until cancel.
|
|
|
|
///
|
|
|
|
/// * You are willing to dedicate the ircd::ctx to just this operation for
|
|
|
|
/// that amount of time.
|
|
|
|
///
|
2018-01-06 02:01:37 +01:00
|
|
|
size_t
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::read_all(socket &socket,
|
|
|
|
const vector_view<const mutable_buffer> &buffers)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return socket.read_all(buffers);
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Yields ircd::ctx until remote has sent at least one frame. The buffers may
|
|
|
|
/// be filled with any amount of data depending on what has accumulated.
|
|
|
|
///
|
|
|
|
/// Use this if the following are true:
|
|
|
|
///
|
|
|
|
/// * You know there is data to be read; you can do this asynchronously with
|
|
|
|
/// other features of the socket. Otherwise this will hang the ircd::ctx.
|
|
|
|
///
|
|
|
|
/// * You are willing to dedicate the ircd::ctx to just this operation,
|
|
|
|
/// which is non-blocking if data is known to be available, but may be
|
|
|
|
/// blocking if this call is made in the blind.
|
|
|
|
///
|
2018-01-06 02:01:37 +01:00
|
|
|
size_t
|
2018-01-14 10:46:50 +01:00
|
|
|
ircd::net::read_few(socket &socket,
|
|
|
|
const vector_view<const mutable_buffer> &buffers)
|
|
|
|
{
|
|
|
|
return socket.read_few(buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads as much as possible. Non-blocking behavior.
|
|
|
|
///
|
|
|
|
/// This is intended for lowest-level/custom control and not preferred by
|
|
|
|
/// default for most users on an ircd::ctx.
|
|
|
|
///
|
|
|
|
size_t
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::read_any(socket &socket,
|
|
|
|
const vector_view<const mutable_buffer> &buffers)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return socket.read_any(buffers);
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-08 21:41:22 +01:00
|
|
|
/// Reads one message or less in a single syscall. Non-blocking behavior.
|
|
|
|
///
|
|
|
|
/// This is intended for lowest-level/custom control and not preferred by
|
2018-01-14 10:46:50 +01:00
|
|
|
/// default for most users on an ircd::ctx.
|
2018-01-08 21:41:22 +01:00
|
|
|
///
|
|
|
|
size_t
|
|
|
|
ircd::net::read_one(socket &socket,
|
|
|
|
const vector_view<const mutable_buffer> &buffers)
|
|
|
|
{
|
|
|
|
return socket.read_one(buffers);
|
|
|
|
}
|
2018-01-07 06:34:02 +01:00
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/wait.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::wait_opts
|
2018-10-03 07:37:07 +02:00
|
|
|
const ircd::net::wait_opts_default;
|
2018-01-08 22:25:13 +01:00
|
|
|
|
|
|
|
/// Wait for socket to become "ready" using a ctx::future.
|
|
|
|
ircd::ctx::future<void>
|
|
|
|
ircd::net::wait(use_future_t,
|
|
|
|
socket &socket,
|
|
|
|
const wait_opts &wait_opts)
|
|
|
|
{
|
|
|
|
ctx::promise<void> p;
|
|
|
|
ctx::future<void> f{p};
|
|
|
|
wait(socket, wait_opts, [p(std::move(p))]
|
|
|
|
(std::exception_ptr eptr)
|
|
|
|
mutable
|
|
|
|
{
|
|
|
|
if(eptr)
|
|
|
|
p.set_exception(std::move(eptr));
|
|
|
|
else
|
|
|
|
p.set_value();
|
|
|
|
});
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for socket to become "ready"; yields ircd::ctx returning code.
|
2018-11-09 06:18:39 +01:00
|
|
|
std::error_code
|
2018-01-08 22:25:13 +01:00
|
|
|
ircd::net::wait(nothrow_t,
|
|
|
|
socket &socket,
|
|
|
|
const wait_opts &wait_opts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
wait(socket, wait_opts);
|
|
|
|
return {};
|
|
|
|
}
|
2018-11-09 06:18:39 +01:00
|
|
|
catch(const std::system_error &e)
|
2018-01-08 22:25:13 +01:00
|
|
|
{
|
|
|
|
return e.code();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for socket to become "ready"; yields ircd::ctx; throws errors.
|
|
|
|
void
|
|
|
|
ircd::net::wait(socket &socket,
|
|
|
|
const wait_opts &wait_opts)
|
|
|
|
{
|
|
|
|
socket.wait(wait_opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for socket to become "ready"; callback with exception_ptr
|
|
|
|
void
|
|
|
|
ircd::net::wait(socket &socket,
|
|
|
|
const wait_opts &wait_opts,
|
|
|
|
wait_callback_eptr callback)
|
|
|
|
{
|
|
|
|
socket.wait(wait_opts, std::move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::wait(socket &socket,
|
|
|
|
const wait_opts &wait_opts,
|
|
|
|
wait_callback_ec callback)
|
|
|
|
{
|
|
|
|
socket.wait(wait_opts, std::move(callback));
|
|
|
|
}
|
|
|
|
|
2018-01-09 02:18:25 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::reflect(const ready &type)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case ready::ANY: return "ANY"_sv;
|
|
|
|
case ready::READ: return "READ"_sv;
|
|
|
|
case ready::WRITE: return "WRITE"_sv;
|
|
|
|
case ready::ERROR: return "ERROR"_sv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "????"_sv;
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-06 02:01:37 +01:00
|
|
|
//
|
2018-01-07 06:34:02 +01:00
|
|
|
// net/close.h
|
2018-01-06 02:01:37 +01:00
|
|
|
//
|
|
|
|
|
2018-06-01 13:08:20 +02:00
|
|
|
decltype(ircd::net::close_opts::default_timeout)
|
|
|
|
ircd::net::close_opts::default_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.close.timeout" },
|
|
|
|
{ "default", 7500L },
|
|
|
|
};
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Static instance of default close options.
|
|
|
|
ircd::net::close_opts
|
|
|
|
const ircd::net::close_opts_default
|
2018-01-02 01:16:39 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
};
|
2018-01-02 01:16:39 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Static helper callback which may be passed to the callback-based overload
|
|
|
|
/// of close(). This callback does nothing.
|
|
|
|
ircd::net::close_callback
|
|
|
|
const ircd::net::close_ignore{[]
|
|
|
|
(std::exception_ptr eptr)
|
2018-01-02 01:16:39 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
return;
|
|
|
|
}};
|
2018-01-02 01:16:39 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::ctx::future<void>
|
|
|
|
ircd::net::close(socket &socket,
|
|
|
|
const close_opts &opts)
|
2018-01-02 01:16:39 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
ctx::promise<void> p;
|
|
|
|
ctx::future<void> f(p);
|
|
|
|
close(socket, opts, [p(std::move(p))]
|
|
|
|
(std::exception_ptr eptr)
|
|
|
|
mutable
|
|
|
|
{
|
|
|
|
if(eptr)
|
|
|
|
p.set_exception(std::move(eptr));
|
|
|
|
else
|
|
|
|
p.set_value();
|
|
|
|
});
|
|
|
|
|
|
|
|
return f;
|
2018-01-02 01:16:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
void
|
|
|
|
ircd::net::close(socket &socket,
|
|
|
|
const close_opts &opts,
|
|
|
|
close_callback callback)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
socket.disconnect(opts, std::move(callback));
|
2018-01-02 01:16:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/open.h
|
|
|
|
//
|
|
|
|
|
2018-06-01 13:05:19 +02:00
|
|
|
decltype(ircd::net::open_opts::default_connect_timeout)
|
|
|
|
ircd::net::open_opts::default_connect_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.open.connect_timeout" },
|
|
|
|
{ "default", 7500L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::open_opts::default_handshake_timeout)
|
|
|
|
ircd::net::open_opts::default_handshake_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.open.handshake_timeout" },
|
|
|
|
{ "default", 7500L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::open_opts::default_verify_certificate)
|
|
|
|
ircd::net::open_opts::default_verify_certificate
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.open.verify_certificate" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::open_opts::default_allow_self_signed)
|
|
|
|
ircd::net::open_opts::default_allow_self_signed
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.open.allow_self_signed" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2018-10-04 23:56:08 +02:00
|
|
|
decltype(ircd::net::open_opts::default_allow_self_chain)
|
|
|
|
ircd::net::open_opts::default_allow_self_chain
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.open.allow_self_chain" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2018-06-01 13:05:19 +02:00
|
|
|
decltype(ircd::net::open_opts::default_allow_expired)
|
|
|
|
ircd::net::open_opts::default_allow_expired
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.open.allow_expired" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Open new socket with future-based report.
|
|
|
|
///
|
|
|
|
ircd::ctx::future<std::shared_ptr<ircd::net::socket>>
|
|
|
|
ircd::net::open(const open_opts &opts)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
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;
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Open existing socket with callback-based report.
|
|
|
|
///
|
|
|
|
std::shared_ptr<ircd::net::socket>
|
|
|
|
ircd::net::open(const open_opts &opts,
|
|
|
|
open_callback handler)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
auto s{std::make_shared<socket>()};
|
|
|
|
open(*s, opts, std::move(handler));
|
|
|
|
return s;
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
/// Open existing socket with callback-based report.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
ircd::net::open(socket &socket,
|
|
|
|
const open_opts &opts,
|
|
|
|
open_callback handler)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-01-07 23:37:33 +01:00
|
|
|
auto complete{[s(shared_from(socket)), handler(std::move(handler))]
|
2018-01-07 06:34:02 +01:00
|
|
|
(std::exception_ptr eptr)
|
|
|
|
{
|
2018-03-10 22:42:55 +01:00
|
|
|
if(eptr && !s->fini)
|
2018-01-07 06:34:02 +01:00
|
|
|
close(*s, dc::RST);
|
|
|
|
|
|
|
|
handler(std::move(eptr));
|
|
|
|
}};
|
|
|
|
|
2018-01-07 23:37:33 +01:00
|
|
|
auto connector{[&socket, opts, complete(std::move(complete))]
|
2018-05-02 03:40:03 +02:00
|
|
|
(std::exception_ptr eptr, const hostport &hp, const ipport &ipport)
|
2018-01-07 06:34:02 +01:00
|
|
|
{
|
|
|
|
if(eptr)
|
|
|
|
return complete(std::move(eptr));
|
|
|
|
|
|
|
|
const auto ep{make_endpoint(ipport)};
|
|
|
|
socket.connect(ep, opts, std::move(complete));
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(!opts.ipport)
|
2018-10-01 20:52:59 +02:00
|
|
|
dns::resolve(opts.hostport, std::move(connector));
|
2018-01-07 06:34:02 +01:00
|
|
|
else
|
2018-05-02 03:40:03 +02:00
|
|
|
connector({}, opts.hostport, opts.ipport);
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-06 02:01:37 +01:00
|
|
|
//
|
2018-01-07 06:34:02 +01:00
|
|
|
// net/sopts.h
|
2018-01-06 02:01:37 +01:00
|
|
|
//
|
|
|
|
|
2018-01-07 11:02:41 +01:00
|
|
|
/// Construct sock_opts with the current options from socket argument
|
|
|
|
ircd::net::sock_opts::sock_opts(const socket &socket)
|
2018-01-06 02:01:37 +01:00
|
|
|
: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,
|
2018-01-07 11:02:41 +01:00
|
|
|
const sock_opts &opts)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2018-01-11 12:22:17 +01:00
|
|
|
t >= 0, // ON / OFF boolean
|
2018-01-06 02:01:37 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
/// Toggles the behavior of non-async asio calls.
|
|
|
|
///
|
|
|
|
/// This option affects very little in practice and only sets a flag in
|
|
|
|
/// userspace in asio, not an actual ioctl(). Specifically:
|
|
|
|
///
|
|
|
|
/// * All sockets are already set by asio to FIONBIO=1 no matter what, thus
|
|
|
|
/// nothing really blocks the event loop ever by default unless you try hard.
|
|
|
|
///
|
|
|
|
/// * All asio::async_ and sd.async_ and ssl.async_ calls will always do what
|
|
|
|
/// the synchronous/blocking alternative would have accomplished but using
|
|
|
|
/// the async methodology. i.e if a buffer is full you will always wait
|
|
|
|
/// asynchronously: async_write() will wait for everything, async_write_some()
|
|
|
|
/// will wait for something, etc -- but there will never be true non-blocking
|
|
|
|
/// _effective behavior_ from these calls.
|
|
|
|
///
|
|
|
|
/// * All asio non-async calls conduct blocking by (on linux) poll()'ing the
|
|
|
|
/// socket to get a real kernel-blocking operation out of it (this is the
|
|
|
|
/// try-hard part).
|
|
|
|
///
|
|
|
|
/// This flag only controls the behavior of the last bullet. In practice,
|
|
|
|
/// in this project there is never a reason to ever set this to true,
|
|
|
|
/// however, sockets do get constructed by asio in blocking mode by default
|
|
|
|
/// so we mostly use this function to set it to non-blocking.
|
|
|
|
///
|
2018-01-06 02:01:37 +01:00
|
|
|
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
|
|
|
{
|
2018-01-06 02:01:37 +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
|
|
|
|
//
|
|
|
|
|
2018-12-09 00:17:13 +01:00
|
|
|
/// Option to indicate if any listener sockets should be allowed to bind. If
|
|
|
|
/// false then no listeners should bind. This is only effective on startup
|
|
|
|
/// unless a conf item updated function is implemented here.
|
|
|
|
decltype(ircd::net::listen)
|
|
|
|
ircd::net::listen
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.listen" },
|
|
|
|
{ "default", true },
|
2018-12-09 01:15:22 +01:00
|
|
|
{ "persist", false },
|
2018-12-09 00:17:13 +01:00
|
|
|
};
|
|
|
|
|
2018-07-08 06:33:23 +02:00
|
|
|
//
|
|
|
|
// listener
|
|
|
|
//
|
|
|
|
|
2018-08-16 09:55:23 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const listener &a)
|
|
|
|
{
|
|
|
|
s << *a.acceptor;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// listener::listener
|
|
|
|
//
|
|
|
|
|
2018-03-23 21:10:00 +01:00
|
|
|
ircd::net::listener::listener(const string_view &name,
|
2018-07-07 03:38:08 +02:00
|
|
|
const std::string &opts,
|
2018-09-02 07:00:38 +02:00
|
|
|
callback cb,
|
|
|
|
proffer pcb)
|
2018-03-23 21:10:00 +01:00
|
|
|
:listener
|
|
|
|
{
|
2018-09-02 07:00:38 +02:00
|
|
|
name, json::object{opts}, std::move(cb), std::move(pcb)
|
2018-03-23 21:10:00 +01:00
|
|
|
}
|
2017-11-01 23:51:24 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-23 21:10:00 +01:00
|
|
|
ircd::net::listener::listener(const string_view &name,
|
2018-07-07 03:38:08 +02:00
|
|
|
const json::object &opts,
|
2018-09-02 07:00:38 +02:00
|
|
|
callback cb,
|
|
|
|
proffer pcb)
|
2018-03-23 21:10:00 +01:00
|
|
|
:acceptor
|
|
|
|
{
|
2019-01-18 18:42:44 +01:00
|
|
|
std::make_shared<struct acceptor>(*this, name, opts, std::move(cb), std::move(pcb))
|
2018-03-23 21:10:00 +01:00
|
|
|
}
|
2017-11-01 23:51:24 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancels all pending accepts and handshakes and waits (yields ircd::ctx)
|
|
|
|
/// until report.
|
|
|
|
///
|
|
|
|
ircd::net::listener::~listener()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(acceptor)
|
|
|
|
acceptor->join();
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:42:44 +01:00
|
|
|
bool
|
|
|
|
ircd::net::listener::start()
|
|
|
|
{
|
|
|
|
return acceptor && !acceptor->handle_set?
|
|
|
|
acceptor->set_handle():
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2018-08-31 04:15:28 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::listener::name()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(acceptor);
|
|
|
|
return acceptor->name;
|
|
|
|
}
|
|
|
|
|
2018-08-30 00:50:55 +02:00
|
|
|
ircd::net::listener::operator
|
|
|
|
ircd::json::object()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(acceptor);
|
|
|
|
return acceptor->opts;
|
|
|
|
}
|
|
|
|
|
2018-07-08 06:33:23 +02:00
|
|
|
//
|
|
|
|
// listener_udp
|
|
|
|
//
|
|
|
|
|
2018-08-16 09:55:23 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const listener_udp &a)
|
|
|
|
{
|
|
|
|
s << *a.acceptor;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// listener_udp::listener_udp
|
|
|
|
//
|
|
|
|
|
2018-07-08 06:33:23 +02:00
|
|
|
ircd::net::listener_udp::listener_udp(const string_view &name,
|
|
|
|
const std::string &opts)
|
|
|
|
:listener_udp
|
|
|
|
{
|
|
|
|
name, json::object{opts}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::listener_udp::listener_udp(const string_view &name,
|
|
|
|
const json::object &opts)
|
|
|
|
:acceptor
|
|
|
|
{
|
|
|
|
std::make_unique<struct acceptor>(name, opts)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::listener_udp::~listener_udp()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(acceptor)
|
|
|
|
acceptor->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::listener_udp::datagram &
|
|
|
|
ircd::net::listener_udp::operator()(datagram &datagram)
|
|
|
|
{
|
|
|
|
assert(acceptor);
|
|
|
|
return acceptor->operator()(datagram);
|
|
|
|
}
|
|
|
|
|
2018-08-31 04:15:28 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::listener_udp::name()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(acceptor);
|
|
|
|
return acceptor->name;
|
|
|
|
}
|
|
|
|
|
2018-08-30 00:50:55 +02:00
|
|
|
ircd::net::listener_udp::operator
|
|
|
|
ircd::json::object()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(acceptor);
|
|
|
|
return acceptor->opts;
|
|
|
|
}
|
|
|
|
|
2018-01-19 15:55:48 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-09-30 08:04:41 +02:00
|
|
|
//
|
2018-01-19 15:55:48 +01:00
|
|
|
// net/acceptor.h
|
2017-09-30 08:04:41 +02:00
|
|
|
//
|
|
|
|
|
2018-09-30 02:40:45 +02:00
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
thread_local char logheadbuf[512];
|
|
|
|
}
|
|
|
|
|
2018-07-08 06:33:23 +02:00
|
|
|
//
|
|
|
|
// listener::acceptor
|
|
|
|
//
|
|
|
|
|
2018-09-30 02:40:45 +02:00
|
|
|
decltype(ircd::net::listener::acceptor::log)
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::listener::acceptor::log
|
|
|
|
{
|
|
|
|
"listener"
|
|
|
|
};
|
|
|
|
|
2018-03-09 21:22:10 +01:00
|
|
|
decltype(ircd::net::listener::acceptor::timeout)
|
|
|
|
ircd::net::listener::acceptor::timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.acceptor.timeout" },
|
2018-05-15 03:32:19 +02:00
|
|
|
{ "default", 12000L },
|
2018-03-09 21:22:10 +01:00
|
|
|
};
|
|
|
|
|
2018-08-16 09:55:23 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const struct listener::acceptor &a)
|
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
thread_local char addrbuf[128];
|
|
|
|
s << "'" << a.name << "' @ [" << string(addrbuf, a.ep.address()) << "]:" << a.ep.port();
|
2018-08-16 09:55:23 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-07-08 06:33:23 +02:00
|
|
|
//
|
|
|
|
// listener::acceptor::acceptor
|
|
|
|
//
|
|
|
|
|
2019-01-18 18:42:44 +01:00
|
|
|
ircd::net::listener::acceptor::acceptor(net::listener &listener,
|
|
|
|
const string_view &name,
|
2018-07-07 03:38:08 +02:00
|
|
|
const json::object &opts,
|
2018-09-02 07:00:38 +02:00
|
|
|
listener::callback cb,
|
|
|
|
listener::proffer pcb)
|
2017-09-30 08:04:41 +02:00
|
|
|
try
|
2019-01-18 18:42:44 +01:00
|
|
|
:listener
|
|
|
|
{
|
|
|
|
&listener
|
|
|
|
}
|
|
|
|
,name
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-03-23 21:10:00 +01:00
|
|
|
name
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2018-08-30 00:50:55 +02:00
|
|
|
,opts
|
|
|
|
{
|
|
|
|
opts
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
,backlog
|
|
|
|
{
|
2018-03-23 21:10:00 +01:00
|
|
|
//TODO: XXX
|
2017-10-25 18:37:37 +02:00
|
|
|
//boost::asio::ip::tcp::socket::max_connections <-- linkage failed?
|
2018-03-23 21:10:00 +01:00
|
|
|
std::min(opts.get<uint>("backlog", SOMAXCONN), uint(SOMAXCONN))
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2018-07-07 03:38:08 +02:00
|
|
|
,cb
|
|
|
|
{
|
|
|
|
std::move(cb)
|
|
|
|
}
|
2018-09-02 07:00:38 +02:00
|
|
|
,pcb
|
|
|
|
{
|
|
|
|
std::move(pcb)
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
,ssl
|
|
|
|
{
|
|
|
|
asio::ssl::context::method::sslv23_server
|
|
|
|
}
|
|
|
|
,ep
|
|
|
|
{
|
2018-07-01 11:13:29 +02:00
|
|
|
ip::address::from_string(unquote(opts.get("host", "0.0.0.0"s))),
|
2018-03-23 21:10:00 +01:00
|
|
|
opts.get<uint16_t>("port", 8448L)
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
,a
|
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
ios::get()
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
{
|
2017-10-25 18:37:37 +02:00
|
|
|
static const auto &max_connections
|
|
|
|
{
|
2018-03-23 21:10:00 +01:00
|
|
|
//TODO: XXX
|
2017-10-25 18:37:37 +02:00
|
|
|
//boost::asio::ip::tcp::socket::max_connections <-- linkage failed?
|
2018-03-23 21:10:00 +01:00
|
|
|
std::min(opts.get<uint>("max_connections", SOMAXCONN), uint(SOMAXCONN))
|
2017-10-25 18:37:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const ip::tcp::acceptor::reuse_address reuse_address
|
|
|
|
{
|
|
|
|
true
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
configure(opts);
|
|
|
|
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
log, "%s configured listener SSL", string(logheadbuf, *this)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
a.open(ep.protocol());
|
|
|
|
a.set_option(reuse_address);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
log, "%s opened listener socket", string(logheadbuf, *this)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
a.bind(ep);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
log, "%s bound listener socket", string(logheadbuf, *this)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
a.listen(backlog);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s listening (backlog: %lu, max connections: %zu)",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-09-30 02:28:11 +02:00
|
|
|
backlog,
|
|
|
|
max_connections
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
throw_system_error(e);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::listener::acceptor::~acceptor()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-19 15:55:48 +01:00
|
|
|
void
|
|
|
|
ircd::net::listener::acceptor::join()
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
interrupt();
|
|
|
|
joining.wait([this]
|
|
|
|
{
|
|
|
|
return !accepting && !handshaking;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-02 07:11:09 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "acceptor(%p) join: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-19 15:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::listener::acceptor::interrupt()
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
interrupting = true;
|
2018-07-07 03:40:58 +02:00
|
|
|
a.cancel();
|
2018-01-19 15:55:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-09-02 07:11:09 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "acceptor(%p) interrupt: %s",
|
|
|
|
this,
|
|
|
|
string(e)
|
|
|
|
};
|
2018-01-19 15:55:48 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
2019-01-18 18:42:44 +01:00
|
|
|
/// for the socket.
|
|
|
|
bool
|
|
|
|
ircd::net::listener::acceptor::set_handle()
|
2017-09-30 08:04:41 +02:00
|
|
|
try
|
|
|
|
{
|
2019-01-18 18:42:44 +01:00
|
|
|
assert(!handle_set);
|
|
|
|
handle_set = true;
|
|
|
|
auto sock
|
|
|
|
{
|
|
|
|
std::make_shared<ircd::socket>(ssl)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
2019-01-18 18:42:44 +01:00
|
|
|
|
2018-01-12 03:50:34 +01:00
|
|
|
++accepting;
|
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)));
|
2019-01-18 18:42:44 +01:00
|
|
|
return true;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-01-25 03:09:58 +01:00
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
"%s: %s", string(logheadbuf, *this), e.what()
|
2018-01-25 03:09:58 +01:00
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
assert(bool(sock));
|
2019-01-18 18:42:44 +01:00
|
|
|
assert(handle_set);
|
|
|
|
assert(accepting > 0);
|
|
|
|
|
|
|
|
handle_set = false;
|
|
|
|
--accepting;
|
2018-09-02 07:11:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "%s: accepted(%zu) %s %s",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-09-02 07:11:09 +02:00
|
|
|
accepting,
|
2018-12-31 21:57:32 +01:00
|
|
|
loghead(*sock),
|
2018-09-02 07:11:09 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2018-01-12 03:50:34 +01:00
|
|
|
|
|
|
|
if(!check_accept_error(ec, *sock))
|
|
|
|
return;
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2018-09-02 07:00:38 +02:00
|
|
|
// Call the proffer-callback if available. This allows the application
|
|
|
|
// to check whether to allow or deny this remote before the handshake.
|
2019-01-18 18:42:44 +01:00
|
|
|
if(pcb && !pcb(*listener, remote_ipport(*sock)))
|
2018-09-02 07:00:38 +02:00
|
|
|
{
|
|
|
|
net::close(*sock, dc::RST, close_ignore);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
// Toggles the behavior of non-async functions; see func comment
|
|
|
|
blocking(*sock, false);
|
|
|
|
|
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-11-01 23:51:24 +01:00
|
|
|
++handshaking;
|
2018-03-09 21:22:10 +01:00
|
|
|
sock->set_timeout(milliseconds(timeout));
|
2018-01-12 03:50:34 +01:00
|
|
|
sock->ssl.async_handshake(handshake_type, std::move(handshake));
|
2017-11-01 23:51:24 +01:00
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
2019-01-04 21:44:29 +01:00
|
|
|
assert(bool(sock));
|
2018-09-02 07:11:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "%s: acceptor interrupted %s %s",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-12-31 21:57:32 +01:00
|
|
|
loghead(*sock),
|
2018-09-02 07:11:09 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2017-11-01 23:51:24 +01:00
|
|
|
|
2019-01-04 21:44:29 +01:00
|
|
|
error_code ec_;
|
|
|
|
sock->sd.close(ec_);
|
|
|
|
assert(!ec_);
|
2017-11-01 23:51:24 +01:00
|
|
|
joining.notify_all();
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2018-11-15 03:57:42 +01:00
|
|
|
catch(const std::system_error &e)
|
2018-04-13 06:52:04 +02:00
|
|
|
{
|
2019-01-04 21:44:29 +01:00
|
|
|
assert(bool(sock));
|
2018-09-02 07:11:09 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "%s: %s in accept(): %s",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-12-31 21:57:32 +01:00
|
|
|
loghead(*sock),
|
2019-01-04 21:44:29 +01:00
|
|
|
e.what()
|
2018-09-02 07:11:09 +02:00
|
|
|
};
|
2019-01-04 21:44:29 +01:00
|
|
|
|
|
|
|
error_code ec_;
|
|
|
|
sock->sd.close(ec_);
|
|
|
|
assert(!ec_);
|
2018-04-13 06:52:04 +02:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-01-04 21:44:29 +01:00
|
|
|
assert(bool(sock));
|
2018-09-02 07:11:09 +02:00
|
|
|
log::error
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "%s: %s in accept(): %s",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-12-31 21:57:32 +01:00
|
|
|
loghead(*sock),
|
2018-09-02 07:11:09 +02:00
|
|
|
e.what()
|
|
|
|
};
|
2019-01-04 21:44:29 +01:00
|
|
|
|
|
|
|
error_code ec_;
|
|
|
|
sock->sd.close(ec_);
|
|
|
|
assert(!ec_);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
///
|
2018-01-12 03:50:34 +01:00
|
|
|
bool
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::listener::acceptor::check_accept_error(const error_code &ec,
|
|
|
|
socket &sock)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2017-10-27 00:36:31 +02:00
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
if(unlikely(interrupting))
|
|
|
|
throw ctx::interrupted();
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(likely(!ec))
|
2018-01-12 03:50:34 +01:00
|
|
|
return true;
|
2017-11-06 21:26:47 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(system_category(ec)) switch(ec.value())
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
case int(errc::operation_canceled):
|
2018-01-12 03:50:34 +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
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
throw_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;
|
2018-01-07 06:34:02 +01:00
|
|
|
assert(bool(sock));
|
2019-03-05 20:34:55 +01:00
|
|
|
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
const auto ¤t_cipher
|
|
|
|
{
|
|
|
|
openssl::current_cipher(*sock)
|
|
|
|
};
|
|
|
|
|
2018-09-02 07:11:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-03-05 20:34:55 +01:00
|
|
|
log, "%s handshook(%zu) cipher:%s %s",
|
2018-12-12 18:05:15 +01:00
|
|
|
loghead(*sock),
|
2018-09-02 07:11:09 +02:00
|
|
|
handshaking,
|
2019-03-05 20:34:55 +01:00
|
|
|
current_cipher?
|
|
|
|
openssl::name(*current_cipher):
|
|
|
|
"<NO CIPHER>"_sv,
|
2018-09-02 07:11:09 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2019-03-05 20:34:55 +01:00
|
|
|
#endif
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2018-01-12 03:50:34 +01:00
|
|
|
check_handshake_error(ec, *sock);
|
|
|
|
sock->cancel_timeout();
|
2018-07-07 03:38:08 +02:00
|
|
|
assert(bool(cb));
|
2019-01-18 18:42:44 +01:00
|
|
|
cb(*listener, sock);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-11-01 23:51:24 +01:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
2019-01-04 21:44:29 +01:00
|
|
|
assert(bool(sock));
|
2018-09-02 07:11:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "%s: SSL handshake interrupted %s %s",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-12-31 21:57:32 +01:00
|
|
|
loghead(*sock),
|
2018-09-02 07:11:09 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2017-11-01 23:51:24 +01:00
|
|
|
|
2019-01-04 21:44:29 +01:00
|
|
|
close(*sock, dc::RST, close_ignore);
|
2017-11-01 23:51:24 +01:00
|
|
|
joining.notify_all();
|
|
|
|
}
|
2018-11-15 03:57:42 +01:00
|
|
|
catch(const std::system_error &e)
|
2018-01-24 23:32:05 +01:00
|
|
|
{
|
2019-01-04 21:44:29 +01:00
|
|
|
assert(bool(sock));
|
2018-09-02 07:11:09 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "%s: %s in handshake(): %s",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-12-31 21:57:32 +01:00
|
|
|
loghead(*sock),
|
2019-01-04 21:44:29 +01:00
|
|
|
e.what()
|
2018-09-02 07:11:09 +02:00
|
|
|
};
|
2019-01-04 21:44:29 +01:00
|
|
|
|
|
|
|
close(*sock, dc::RST, close_ignore);
|
2018-01-24 23:32:05 +01:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-01-04 21:44:29 +01:00
|
|
|
assert(bool(sock));
|
2018-09-02 07:11:09 +02:00
|
|
|
log::error
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "%s: %s in handshake(): %s",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-12-31 21:57:32 +01:00
|
|
|
loghead(*sock),
|
2018-09-02 07:11:09 +02:00
|
|
|
e.what()
|
|
|
|
};
|
2019-01-04 21:44:29 +01:00
|
|
|
|
|
|
|
close(*sock, dc::RST, close_ignore);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
///
|
2018-01-07 06:34:02 +01:00
|
|
|
void
|
|
|
|
ircd::net::listener::acceptor::check_handshake_error(const error_code &ec,
|
|
|
|
socket &sock)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2017-10-27 00:36:31 +02:00
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
if(unlikely(interrupting))
|
|
|
|
throw ctx::interrupted();
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(likely(system_category(ec))) switch(ec.value())
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
case 0:
|
2018-01-24 23:32:05 +01:00
|
|
|
return;
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
case int(errc::operation_canceled):
|
2018-01-24 23:32:05 +01:00
|
|
|
if(sock.timedout)
|
2018-11-09 06:18:39 +01:00
|
|
|
throw_system_error(errc::timed_out);
|
2018-01-24 23:32:05 +01:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
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
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
throw_system_error(ec);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::listener::acceptor::configure(const json::object &opts)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
log, "%s preparing listener socket configuration...", string(logheadbuf, *this)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
2018-01-11 12:22:17 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ssl.set_options
|
|
|
|
(
|
2018-01-11 12:22:17 +01:00
|
|
|
0
|
|
|
|
//| ssl.default_workarounds
|
2019-03-05 01:48:05 +01:00
|
|
|
| ssl.no_tlsv1
|
2017-09-30 08:04:41 +02:00
|
|
|
//| 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
|
2018-01-11 12:22:17 +01:00
|
|
|
//| ssl.single_dh_use
|
2017-09-30 08:04:41 +02:00
|
|
|
);
|
2018-01-11 12:22:17 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
//TODO: XXX
|
|
|
|
ssl.set_password_callback([this]
|
|
|
|
(const auto &size, const auto &purpose)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
log, "%s asking for password with purpose '%s' (size: %zu)",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-09-30 02:28:11 +02:00
|
|
|
purpose,
|
|
|
|
size
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
//XXX: TODO
|
2018-09-30 02:28:11 +02:00
|
|
|
assert(0);
|
2017-09-30 08:04:41 +02:00
|
|
|
return "foobar";
|
|
|
|
});
|
|
|
|
|
2019-03-05 01:38:39 +01:00
|
|
|
if(!empty(unquote(opts["certificate_chain_path"])))
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
const std::string filename
|
|
|
|
{
|
2018-03-28 21:42:51 +02:00
|
|
|
unquote(opts["certificate_chain_path"])
|
2017-09-30 08:04:41 +02:00
|
|
|
};
|
|
|
|
|
2017-11-26 20:56:31 +01:00
|
|
|
if(!fs::exists(filename))
|
2018-03-28 21:42:51 +02:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"%s: SSL certificate chain file @ `%s' not found",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-03-28 21:42:51 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-11-26 20:56:31 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ssl.use_certificate_chain_file(filename);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s using certificate chain file '%s'",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-09-30 02:28:11 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 01:38:39 +01:00
|
|
|
if(!empty(unquote(opts["certificate_pem_path"])))
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
const std::string filename
|
|
|
|
{
|
2018-07-01 11:16:00 +02:00
|
|
|
unquote(opts.get("certificate_pem_path", name + ".crt"))
|
2017-09-30 08:04:41 +02:00
|
|
|
};
|
|
|
|
|
2017-11-26 20:56:31 +01:00
|
|
|
if(!fs::exists(filename))
|
2018-03-28 21:42:51 +02:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"%s: SSL certificate pem file @ `%s' not found",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-03-28 21:42:51 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-11-26 20:56:31 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ssl.use_certificate_file(filename, asio::ssl::context::pem);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s using certificate file '%s'",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-09-30 02:28:11 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 01:38:39 +01:00
|
|
|
if(!empty(unquote(opts["private_key_pem_path"])))
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
const std::string filename
|
|
|
|
{
|
2018-07-01 11:16:00 +02:00
|
|
|
unquote(opts.get("private_key_pem_path", name + ".crt.key"))
|
2017-09-30 08:04:41 +02:00
|
|
|
};
|
|
|
|
|
2017-11-26 20:56:31 +01:00
|
|
|
if(!fs::exists(filename))
|
2018-03-28 21:42:51 +02:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"%s: SSL private key file @ `%s' not found",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-03-28 21:42:51 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-11-26 20:56:31 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ssl.use_private_key_file(filename, asio::ssl::context::pem);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s using private key file '%s'",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-09-30 02:28:11 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 01:38:39 +01:00
|
|
|
if(!empty(unquote(opts["tmp_dh_path"])))
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
const std::string filename
|
|
|
|
{
|
2018-07-01 11:16:00 +02:00
|
|
|
unquote(opts.at("tmp_dh_path"))
|
2017-09-30 08:04:41 +02:00
|
|
|
};
|
|
|
|
|
2017-11-26 20:56:31 +01:00
|
|
|
if(!fs::exists(filename))
|
2018-03-28 21:42:51 +02:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"%s: SSL tmp dh file @ `%s' not found",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-03-28 21:42:51 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-11-26 20:56:31 +01:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ssl.use_tmp_dh_file(filename);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s using tmp dh file '%s'",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-09-30 02:28:11 +02:00
|
|
|
filename
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2019-03-05 01:38:39 +01:00
|
|
|
else if(!empty(unquote(opts["tmp_dh"])))
|
2018-08-30 01:16:35 +02:00
|
|
|
{
|
|
|
|
const const_buffer buf
|
|
|
|
{
|
|
|
|
unquote(opts.at("tmp_dh"))
|
|
|
|
};
|
|
|
|
|
|
|
|
ssl.use_tmp_dh(buf);
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s using DH params supplied in options (%zu bytes)",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this),
|
2018-08-30 01:16:35 +02:00
|
|
|
size(buf)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const const_buffer &buf
|
|
|
|
{
|
|
|
|
openssl::rfc3526_dh_params_pem
|
|
|
|
};
|
|
|
|
|
|
|
|
ssl.use_tmp_dh(buf);
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s using pre-supplied rfc3526 DH parameters.",
|
2018-09-30 02:40:45 +02:00
|
|
|
string(logheadbuf, *this)
|
2018-08-30 01:16:35 +02:00
|
|
|
};
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 06:33:23 +02:00
|
|
|
//
|
|
|
|
// listener_udp::acceptor
|
|
|
|
//
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const struct listener_udp::acceptor &a)
|
|
|
|
{
|
|
|
|
s << "'" << a.name << "' @ [" << string(a.ep.address()) << "]:" << a.ep.port();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// listener_udp::acceptor::acceptor
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::listener_udp::acceptor::acceptor(const string_view &name,
|
|
|
|
const json::object &opts)
|
|
|
|
try
|
|
|
|
:name
|
|
|
|
{
|
|
|
|
name
|
|
|
|
}
|
|
|
|
,opts
|
|
|
|
{
|
|
|
|
opts
|
|
|
|
}
|
|
|
|
,ep
|
|
|
|
{
|
|
|
|
ip::address::from_string(unquote(opts.get("host", "0.0.0.0"s))),
|
|
|
|
opts.get<uint16_t>("port", 8448L)
|
|
|
|
}
|
|
|
|
,a
|
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
ios::get()
|
2018-07-08 06:33:23 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
static const ip::udp::socket::reuse_address reuse_address
|
|
|
|
{
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
a.open(ep.protocol());
|
|
|
|
a.set_option(reuse_address);
|
|
|
|
log::debug
|
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
log, "%s opened listener socket", string(logheadbuf, *this)
|
2018-07-08 06:33:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
a.bind(ep);
|
|
|
|
log::debug
|
|
|
|
{
|
2018-09-30 02:40:45 +02:00
|
|
|
log, "%s bound listener socket", string(logheadbuf, *this)
|
2018-07-08 06:33:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
throw_system_error(e);
|
2018-07-08 06:33:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::listener_udp::acceptor::~acceptor()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::listener_udp::acceptor::join()
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
interrupt();
|
|
|
|
joining.wait([this]
|
|
|
|
{
|
|
|
|
return waiting == 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "acceptor(%p) join: %s", this, e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::listener_udp::acceptor::interrupt()
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
a.cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "acceptor(%p) interrupt: %s", this, string(e)
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::listener_udp::datagram &
|
|
|
|
ircd::net::listener_udp::acceptor::operator()(datagram &datagram)
|
|
|
|
{
|
|
|
|
assert(ctx::current);
|
|
|
|
|
2018-11-30 04:16:05 +01:00
|
|
|
const auto flags
|
|
|
|
{
|
|
|
|
this->flags(datagram.flag)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto interruption{[this]
|
2018-12-23 01:21:27 +01:00
|
|
|
(ctx::ctx *const &)
|
2018-11-30 04:16:05 +01:00
|
|
|
{
|
|
|
|
this->interrupt();
|
|
|
|
}};
|
|
|
|
|
2019-02-28 22:33:21 +01:00
|
|
|
const scope_count waiting
|
2018-07-08 06:33:23 +02:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
this->waiting
|
|
|
|
};
|
2018-07-08 06:33:23 +02:00
|
|
|
|
|
|
|
ip::udp::endpoint ep;
|
2018-12-23 05:55:52 +01:00
|
|
|
size_t rlen; continuation
|
2018-07-08 06:33:23 +02:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
continuation::asio_predicate, interruption, [this, &rlen, &datagram, &ep, &flags]
|
|
|
|
(auto &yield)
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
rlen = a.async_receive_from(datagram.mbufs, ep, flags, yield);
|
|
|
|
}
|
2018-07-08 06:33:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
datagram.remote = make_ipport(ep);
|
2018-07-08 07:10:29 +02:00
|
|
|
datagram.mbuf = {data(datagram.mbuf), rlen};
|
2018-07-08 06:33:23 +02:00
|
|
|
return datagram;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::asio::ip::udp::socket::message_flags
|
|
|
|
ircd::net::listener_udp::acceptor::flags(const flag &flag)
|
|
|
|
{
|
|
|
|
ip::udp::socket::message_flags ret{0};
|
|
|
|
|
|
|
|
if(flag & flag::PEEK)
|
|
|
|
ret |= ip::udp::socket::message_peek;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:10:29 +02:00
|
|
|
//
|
|
|
|
// listener_udp::datagram
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::listener_udp::datagram::datagram(const const_buffer &buf,
|
|
|
|
const ipport &remote,
|
|
|
|
const enum flag &flag)
|
|
|
|
:cbuf{buf}
|
|
|
|
,cbufs{&cbuf, 1}
|
|
|
|
,remote{remote}
|
|
|
|
,flag{flag}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::net::listener_udp::datagram::datagram(const mutable_buffer &buf,
|
|
|
|
const enum flag &flag)
|
|
|
|
:mbuf{buf}
|
|
|
|
,mbufs{&mbuf, 1}
|
|
|
|
,flag{flag}
|
|
|
|
{}
|
|
|
|
|
2018-04-16 00:15:26 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/scope_timeout.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::scope_timeout::scope_timeout(socket &socket,
|
|
|
|
const milliseconds &timeout)
|
|
|
|
:s
|
|
|
|
{
|
|
|
|
timeout < 0ms? nullptr : &socket
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(timeout < 0ms)
|
|
|
|
return;
|
|
|
|
|
|
|
|
socket.set_timeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::scope_timeout::scope_timeout(socket &socket,
|
|
|
|
const milliseconds &timeout,
|
|
|
|
handler callback)
|
|
|
|
:s
|
|
|
|
{
|
|
|
|
timeout < 0ms? nullptr : &socket
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(timeout < 0ms)
|
|
|
|
return;
|
|
|
|
|
|
|
|
socket.set_timeout(timeout, [callback(std::move(callback))]
|
|
|
|
(const error_code &ec)
|
|
|
|
{
|
2018-04-16 00:55:25 +02:00
|
|
|
const bool &timed_out{!ec}; // success = timeout
|
2018-04-16 00:15:26 +02:00
|
|
|
callback(timed_out);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::scope_timeout::scope_timeout(scope_timeout &&other)
|
|
|
|
noexcept
|
|
|
|
:s{std::move(other.s)}
|
|
|
|
{
|
|
|
|
other.s = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::scope_timeout &
|
|
|
|
ircd::net::scope_timeout::operator=(scope_timeout &&other)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~scope_timeout();
|
|
|
|
s = std::move(other.s);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::scope_timeout::~scope_timeout()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::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)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "socket(%p) scope_timeout::cancel: %s",
|
|
|
|
(const void *)s,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-04-16 00:15:26 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::scope_timeout::release()
|
|
|
|
{
|
|
|
|
const auto s{this->s};
|
|
|
|
this->s = nullptr;
|
|
|
|
return s != nullptr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-03-13 02:45:21 +01:00
|
|
|
decltype(ircd::net::socket::count)
|
|
|
|
ircd::net::socket::count
|
|
|
|
{};
|
|
|
|
|
|
|
|
decltype(ircd::net::socket::instances)
|
|
|
|
ircd::net::socket::instances
|
|
|
|
{};
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
//
|
2018-01-06 02:01:37 +01:00
|
|
|
// socket
|
2017-09-30 08:04:41 +02:00
|
|
|
//
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
ircd::net::socket::socket(asio::ssl::context &ssl,
|
2018-10-17 14:12:10 +02:00
|
|
|
boost::asio::io_service &ios)
|
2018-01-06 02:01:37 +01:00
|
|
|
:sd
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
ios
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|
2018-01-06 02:01:37 +01:00
|
|
|
,ssl
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-06 02:01:37 +01:00
|
|
|
this->sd, ssl
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2018-01-06 02:01:37 +01:00
|
|
|
,timer
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
ios
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
{
|
2018-03-13 02:45:21 +01:00
|
|
|
++instances;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2018-04-12 21:52:14 +02:00
|
|
|
assert(instances > 0);
|
2018-03-13 02:45:21 +01:00
|
|
|
if(unlikely(--instances == 0))
|
|
|
|
net::dock.notify_all();
|
|
|
|
|
2018-02-27 07:49:44 +01:00
|
|
|
if(unlikely(RB_DEBUG_LEVEL && opened(*this)))
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-01-25 03:09:58 +01:00
|
|
|
{
|
|
|
|
"Failed to ensure socket(%p) is disconnected from %s before dtor.",
|
|
|
|
this,
|
|
|
|
string(remote())
|
|
|
|
};
|
2017-10-16 06:28:40 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) close: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:28:40 +02:00
|
|
|
return;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-06 02:01:37 +01:00
|
|
|
ircd::net::socket::connect(const endpoint &ep,
|
2018-01-07 06:34:02 +01:00
|
|
|
const open_opts &opts,
|
|
|
|
eptr_handler callback)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "socket:%lu attempting connect remote[%s] to:%ld$ms",
|
|
|
|
this->id,
|
2018-09-30 02:28:11 +02:00
|
|
|
string(ep),
|
|
|
|
opts.connect_timeout.count()
|
|
|
|
};
|
2017-10-25 19:02:45 +02:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
auto connect_handler
|
2018-01-04 23:20:30 +01:00
|
|
|
{
|
2018-01-07 23:37:33 +01:00
|
|
|
std::bind(&socket::handle_connect, this, weak_from(*this), opts, std::move(callback), ph::_1)
|
2018-01-04 23:20:30 +01:00
|
|
|
};
|
2017-10-19 12:55:24 +02:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
set_timeout(opts.connect_timeout);
|
2018-01-12 03:48:00 +01:00
|
|
|
sd.async_connect(ep, std::move(connect_handler));
|
2018-01-04 23:20:30 +01:00
|
|
|
}
|
2017-10-19 12:55:24 +02:00
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
void
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::socket::handshake(const open_opts &opts,
|
|
|
|
eptr_handler callback)
|
2018-01-04 23:20:30 +01:00
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-12 18:05:15 +01:00
|
|
|
log, "%s handshaking for '%s' to:%ld$ms",
|
|
|
|
loghead(*this),
|
2018-09-30 02:28:11 +02:00
|
|
|
common_name(opts),
|
|
|
|
opts.handshake_timeout.count()
|
|
|
|
};
|
2017-10-19 12:55:24 +02:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
auto handshake_handler
|
2018-01-05 08:14:21 +01:00
|
|
|
{
|
2018-01-07 23:37:33 +01:00
|
|
|
std::bind(&socket::handle_handshake, this, weak_from(*this), std::move(callback), ph::_1)
|
2018-01-05 08:14:21 +01:00
|
|
|
};
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
auto verify_handler
|
2018-01-05 08:14:21 +01:00
|
|
|
{
|
2018-01-07 23:37:33 +01:00
|
|
|
std::bind(&socket::handle_verify, this, ph::_1, ph::_2, opts)
|
2018-01-05 08:14:21 +01:00
|
|
|
};
|
|
|
|
|
2018-01-12 03:50:34 +01:00
|
|
|
set_timeout(opts.handshake_timeout);
|
2018-01-06 02:01:37 +01:00
|
|
|
ssl.set_verify_callback(std::move(verify_handler));
|
|
|
|
ssl.async_handshake(handshake_type::client, std::move(handshake_handler));
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
void
|
|
|
|
ircd::net::socket::disconnect(const close_opts &opts,
|
|
|
|
eptr_handler callback)
|
2017-10-25 18:37:37 +02:00
|
|
|
try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
if(!sd.is_open())
|
|
|
|
{
|
|
|
|
call_user(callback, {});
|
|
|
|
return;
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2018-08-28 20:48:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-12 18:05:15 +01:00
|
|
|
log, "%s disconnect type:%d user: in:%zu out:%zu",
|
|
|
|
loghead(*this),
|
2018-08-28 20:48:09 +02:00
|
|
|
uint(opts.type),
|
|
|
|
in.bytes,
|
|
|
|
out.bytes
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
2018-03-10 00:55:15 +01:00
|
|
|
assert(!fini);
|
|
|
|
fini = true;
|
2018-01-24 01:22:25 +01:00
|
|
|
cancel();
|
2018-03-10 00:55:15 +01:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
if(opts.sopts)
|
|
|
|
set(*this, *opts.sopts);
|
2017-10-16 06:28:40 +02:00
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
switch(opts.type)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2017-10-19 10:02:30 +02:00
|
|
|
case dc::RST:
|
|
|
|
sd.close();
|
2018-01-07 06:34:02 +01:00
|
|
|
break;
|
2017-10-19 10:02:30 +02:00
|
|
|
|
|
|
|
case dc::FIN:
|
|
|
|
sd.shutdown(ip::tcp::socket::shutdown_both);
|
2018-01-07 06:34:02 +01:00
|
|
|
break;
|
2017-10-19 10:02:30 +02:00
|
|
|
|
|
|
|
case dc::FIN_SEND:
|
|
|
|
sd.shutdown(ip::tcp::socket::shutdown_send);
|
2018-01-07 06:34:02 +01:00
|
|
|
break;
|
2017-10-19 10:02:30 +02:00
|
|
|
|
|
|
|
case dc::FIN_RECV:
|
|
|
|
sd.shutdown(ip::tcp::socket::shutdown_receive);
|
2018-01-07 06:34:02 +01:00
|
|
|
break;
|
2017-10-25 18:37:37 +02:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
case dc::SSL_NOTIFY:
|
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
auto disconnect_handler
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-07 06:34:02 +01:00
|
|
|
std::bind(&socket::handle_disconnect, this, shared_from(*this), std::move(callback), ph::_1)
|
|
|
|
};
|
|
|
|
|
|
|
|
set_timeout(opts.timeout);
|
2018-01-12 03:50:34 +01:00
|
|
|
ssl.async_shutdown(std::move(disconnect_handler));
|
2018-01-07 06:34:02 +01:00
|
|
|
return;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
call_user(callback, {});
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2017-10-25 18:37:37 +02:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-08-28 20:48:09 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "socket:%lu disconnect type:%d :%s",
|
|
|
|
this->id,
|
2018-08-28 20:48:09 +02:00
|
|
|
uint(opts.type),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
call_user(callback, make_error_code(e));
|
2018-01-07 06:34:02 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-01-25 03:09:58 +01:00
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
"socket:%lu disconnect: type: %d: %s",
|
|
|
|
this->id,
|
2018-01-25 03:09:58 +01:00
|
|
|
uint(opts.type),
|
|
|
|
e.what()
|
|
|
|
};
|
2017-10-25 18:37:37 +02:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2018-01-12 03:39:24 +01:00
|
|
|
void
|
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
|
|
|
{
|
2018-02-07 07:22:54 +01:00
|
|
|
cancel_timeout();
|
2018-09-25 07:01:25 +02:00
|
|
|
|
2018-01-12 03:39:24 +01:00
|
|
|
boost::system::error_code ec;
|
|
|
|
sd.cancel(ec);
|
2018-11-09 06:18:39 +01:00
|
|
|
if(likely(!ec))
|
2018-09-25 07:01:25 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
log::dwarning
|
|
|
|
{
|
2018-12-31 21:57:32 +01:00
|
|
|
log, "socket:%lu cancel :%s",
|
|
|
|
this->id,
|
2018-09-25 07:01:25 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-08 22:25:13 +01:00
|
|
|
ircd::net::socket::wait(const wait_opts &opts,
|
|
|
|
wait_callback_eptr callback)
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-08 22:25:13 +01:00
|
|
|
wait(opts, [callback(std::move(callback))]
|
|
|
|
(const error_code &ec)
|
|
|
|
{
|
2018-03-08 18:27:32 +01:00
|
|
|
if(likely(!ec))
|
|
|
|
return callback(std::exception_ptr{});
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
callback(make_system_eptr(ec));
|
2018-01-08 22:25:13 +01:00
|
|
|
});
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronous callback when the socket is ready
|
|
|
|
///
|
2018-01-14 02:53:59 +01:00
|
|
|
/// Overload for operator() without a timeout. see: operator()
|
2017-09-30 08:04:41 +02:00
|
|
|
///
|
|
|
|
void
|
2018-01-14 02:53:59 +01:00
|
|
|
ircd::net::socket::wait(const wait_opts &opts)
|
2018-01-20 12:14:14 +01:00
|
|
|
try
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-04-16 01:12:50 +02:00
|
|
|
const auto interruption{[this]
|
2018-12-23 01:21:27 +01:00
|
|
|
(ctx::ctx *const &)
|
2018-04-16 01:12:50 +02:00
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
2018-01-14 02:53:59 +01:00
|
|
|
const scope_timeout timeout
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
2018-01-14 02:53:59 +01:00
|
|
|
*this, opts.timeout
|
2017-09-30 08:04:41 +02:00
|
|
|
};
|
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
switch(opts.type)
|
2018-01-01 10:42:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
case ready::READ: continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this]
|
|
|
|
(auto &yield)
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
sd.async_wait(wait_type::wait_read, yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
2018-01-08 22:25:13 +01:00
|
|
|
|
2018-12-23 05:55:52 +01:00
|
|
|
case ready::WRITE: continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this]
|
|
|
|
(auto &yield)
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
sd.async_wait(wait_type::wait_write, yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
2018-01-01 10:42:00 +01:00
|
|
|
|
2018-12-23 05:55:52 +01:00
|
|
|
case ready::ERROR: continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this]
|
|
|
|
(auto &yield)
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
sd.async_wait(wait_type::wait_error, yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
2018-01-08 22:25:13 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw ircd::not_implemented{};
|
2018-01-01 10:42:00 +01:00
|
|
|
}
|
2018-01-08 22:25:13 +01:00
|
|
|
}
|
2018-01-20 12:14:14 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
if(e.code() == boost::system::errc::operation_canceled && timedout)
|
|
|
|
throw_system_error(std::errc::timed_out);
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
throw_system_error(e);
|
2018-01-20 12:14:14 +01:00
|
|
|
}
|
2018-01-08 22:25:13 +01:00
|
|
|
|
|
|
|
/// Asynchronous callback when the socket is ready
|
|
|
|
///
|
2018-01-14 02:53:59 +01:00
|
|
|
/// This function calls back the handler when the socket is ready
|
|
|
|
/// for the operation of the specified type.
|
2018-01-08 22:25:13 +01:00
|
|
|
///
|
|
|
|
void
|
2018-01-14 02:53:59 +01:00
|
|
|
ircd::net::socket::wait(const wait_opts &opts,
|
|
|
|
wait_callback_ec callback)
|
2018-11-09 06:18:39 +01:00
|
|
|
try
|
2018-01-08 22:25:13 +01:00
|
|
|
{
|
2018-01-14 02:53:59 +01:00
|
|
|
set_timeout(opts.timeout);
|
|
|
|
const unwind::exceptional unset{[this]
|
|
|
|
{
|
|
|
|
cancel_timeout();
|
|
|
|
}};
|
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
switch(opts.type)
|
|
|
|
{
|
2018-01-09 02:18:25 +01:00
|
|
|
case ready::ERROR:
|
2018-03-11 19:23:50 +01:00
|
|
|
{
|
|
|
|
auto handle
|
|
|
|
{
|
|
|
|
std::bind(&socket::handle_ready, this, weak_from(*this), opts.type, std::move(callback), ph::_1, 0UL)
|
|
|
|
};
|
|
|
|
|
2018-01-14 02:53:59 +01:00
|
|
|
sd.async_wait(wait_type::wait_error, std::move(handle));
|
2018-01-08 22:25:13 +01:00
|
|
|
break;
|
2018-03-11 19:23:50 +01:00
|
|
|
}
|
2018-01-08 22:25:13 +01:00
|
|
|
|
2018-01-09 02:18:25 +01:00
|
|
|
case ready::WRITE:
|
2018-03-11 19:23:50 +01:00
|
|
|
{
|
|
|
|
auto handle
|
|
|
|
{
|
|
|
|
std::bind(&socket::handle_ready, this, weak_from(*this), opts.type, std::move(callback), ph::_1, 0UL)
|
|
|
|
};
|
|
|
|
|
2018-01-14 02:53:59 +01:00
|
|
|
sd.async_wait(wait_type::wait_write, std::move(handle));
|
2018-01-08 22:25:13 +01:00
|
|
|
break;
|
2018-03-11 19:23:50 +01:00
|
|
|
}
|
2018-01-08 22:25:13 +01:00
|
|
|
|
2018-01-09 02:18:25 +01:00
|
|
|
case ready::READ:
|
2018-01-14 02:53:59 +01:00
|
|
|
{
|
|
|
|
static char buf[1] alignas(16);
|
|
|
|
static const ilist<mutable_buffer> bufs{buf};
|
|
|
|
__builtin_prefetch(buf, 1, 0); // 1 = write, 0 = no cache
|
|
|
|
|
2018-03-11 19:23:50 +01:00
|
|
|
auto handle
|
|
|
|
{
|
|
|
|
std::bind(&socket::handle_ready, this, weak_from(*this), opts.type, std::move(callback), ph::_1, ph::_2)
|
|
|
|
};
|
|
|
|
|
2018-01-14 02:53:59 +01:00
|
|
|
// The problem here is that waiting on the sd doesn't account for bytes
|
|
|
|
// read into SSL that we didn't consume yet. If something is stuck in
|
|
|
|
// those userspace buffers, the socket won't know about it and perform
|
|
|
|
// the wait. ASIO should fix this by adding a ssl::stream.wait() method
|
|
|
|
// which will bail out immediately in this case before passing up to the
|
|
|
|
// real socket wait.
|
2018-03-18 07:19:33 +01:00
|
|
|
if(SSL_peek(ssl.native_handle(), buf, sizeof(buf)) >= ssize_t(sizeof(buf)))
|
2018-01-14 02:53:59 +01:00
|
|
|
{
|
2018-03-18 07:19:33 +01:00
|
|
|
ircd::post([handle(std::move(handle))]
|
|
|
|
{
|
|
|
|
handle(error_code{}, 1UL);
|
|
|
|
});
|
|
|
|
|
2018-01-14 02:53:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The problem here is that the wait operation gives ec=success on both a
|
|
|
|
// socket error and when data is actually available. We then have to check
|
|
|
|
// using a non-blocking peek in the handler. By doing it this way here we
|
|
|
|
// just get the error in the handler's ec.
|
|
|
|
sd.async_receive(bufs, sd.message_peek, std::move(handle));
|
|
|
|
//sd.async_wait(wait_type::wait_read, std::move(handle));
|
|
|
|
break;
|
|
|
|
}
|
2018-01-08 22:25:13 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw ircd::not_implemented{};
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2018-11-09 06:18:39 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
throw_system_error(e);
|
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
void
|
2018-01-09 02:18:25 +01:00
|
|
|
ircd::net::socket::handle_ready(const std::weak_ptr<socket> wp,
|
|
|
|
const net::ready type,
|
|
|
|
const ec_handler callback,
|
2018-03-11 19:23:50 +01:00
|
|
|
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
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-01-01 10:42:00 +01:00
|
|
|
|
2017-11-16 02:31:42 +01:00
|
|
|
// After life_guard is constructed it is safe to use *this in this frame.
|
2017-10-19 10:01:07 +02:00
|
|
|
const life_guard<socket> s{wp};
|
2018-01-12 03:50:34 +01:00
|
|
|
|
2018-11-09 09:29:31 +01:00
|
|
|
if(!timedout && !is(ec, errc::operation_canceled) && !fini)
|
2018-02-27 16:12:04 +01:00
|
|
|
cancel_timeout();
|
2018-03-10 21:52:23 +01:00
|
|
|
|
2018-11-09 09:29:31 +01:00
|
|
|
if(timedout && is(ec, errc::operation_canceled))
|
2018-11-09 06:18:39 +01:00
|
|
|
ec = make_error_code(errc::timed_out);
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2018-02-28 02:20:31 +01:00
|
|
|
if(unlikely(!ec && !sd.is_open()))
|
2018-11-09 06:18:39 +01:00
|
|
|
ec = make_error_code(errc::bad_file_descriptor);
|
2018-01-24 23:12:38 +01:00
|
|
|
|
2018-03-11 19:23:50 +01:00
|
|
|
if(type == ready::READ && !ec && bytes == 0)
|
2018-11-09 06:18:39 +01:00
|
|
|
ec = error_code{asio::error::eof, asio::error::get_misc_category()};
|
2018-03-11 19:23:50 +01:00
|
|
|
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-12 18:05:15 +01:00
|
|
|
log, "%s ready %s %s avail:%zu:%zu:%d",
|
|
|
|
loghead(*this),
|
2018-09-30 02:28:11 +02:00
|
|
|
reflect(type),
|
|
|
|
string(ec),
|
|
|
|
type == ready::READ? bytes : 0UL,
|
|
|
|
type == ready::READ? available(*this) : 0UL,
|
|
|
|
SSL_pending(ssl.native_handle())
|
|
|
|
};
|
2017-09-30 08:04:41 +02:00
|
|
|
|
|
|
|
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.
|
2018-09-30 02:28:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "socket(%p) belated callback to handler... (%s)",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
2017-10-19 10:01:07 +02:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) handle: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
2017-10-27 00:36:31 +02:00
|
|
|
assert(0);
|
2018-01-07 06:34:02 +01:00
|
|
|
call_user(callback, ec);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
void
|
|
|
|
ircd::net::socket::handle_timeout(const std::weak_ptr<socket> wp,
|
2018-01-08 12:03:50 +01:00
|
|
|
ec_handler callback,
|
2018-01-20 12:14:14 +01:00
|
|
|
error_code ec)
|
2018-01-06 02:01:37 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
2018-01-20 16:01:14 +01:00
|
|
|
if(unlikely(wp.expired()))
|
|
|
|
return;
|
|
|
|
|
2018-04-16 00:55:25 +02:00
|
|
|
// We increment our end of the timer semaphore. If the count is still
|
|
|
|
// behind the other end of the semaphore, this callback was sitting in
|
|
|
|
// the ios queue while the timer was given a new task; any effects here
|
|
|
|
// will be erroneously bleeding into the next timeout. However the callback
|
|
|
|
// is still invoked to satisfy the user's expectation for receiving it.
|
|
|
|
assert(timer_sem[0] < timer_sem[1]);
|
|
|
|
if(++timer_sem[0] == timer_sem[1] && timer_set) switch(ec.value())
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
// A 'success' for this handler means there was a timeout on the socket
|
2018-11-09 06:18:39 +01:00
|
|
|
case 0:
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
assert(timedout == false);
|
|
|
|
timedout = true;
|
|
|
|
sd.cancel();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A cancelation means there was no timeout.
|
2018-11-09 06:18:39 +01:00
|
|
|
case int(std::errc::operation_canceled):
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
assert(ec.category() == std::system_category());
|
2018-01-06 02:01:37 +01:00
|
|
|
assert(timedout == false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All other errors are unexpected, logged and ignored here.
|
2019-01-14 00:50:04 +01:00
|
|
|
default: throw panic
|
2018-01-08 12:03:50 +01:00
|
|
|
{
|
2018-05-10 06:12:01 +02:00
|
|
|
"socket(%p): unexpected: %s\n", (const void *)this, string(ec)
|
2018-01-25 03:09:58 +01:00
|
|
|
};
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
2018-11-09 06:18:39 +01:00
|
|
|
else ec = make_error_code(std::errc::operation_canceled);
|
2018-01-08 12:03:50 +01:00
|
|
|
|
|
|
|
if(callback)
|
|
|
|
call_user(callback, ec);
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
2018-03-11 22:12:39 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-03-11 22:12:39 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
const auto &ec_(e.code());
|
|
|
|
if(system_category(ec_)) switch(ec_.value())
|
2018-03-11 22:12:39 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
case int(errc::bad_file_descriptor):
|
|
|
|
{
|
2018-03-11 22:12:39 +01:00
|
|
|
if(fini)
|
|
|
|
break;
|
|
|
|
|
2019-02-16 01:47:00 +01:00
|
|
|
[[fallthrough]];
|
2018-11-09 06:18:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 22:12:39 +01:00
|
|
|
default:
|
2018-11-09 06:18:39 +01:00
|
|
|
{
|
2018-03-11 22:12:39 +01:00
|
|
|
assert(0);
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) handle timeout: %s",
|
|
|
|
(const void *)this,
|
|
|
|
string(e)
|
|
|
|
};
|
2018-11-09 06:18:39 +01:00
|
|
|
|
2018-03-11 22:12:39 +01:00
|
|
|
break;
|
2018-11-09 06:18:39 +01:00
|
|
|
}
|
2018-03-11 22:12:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(callback)
|
2018-11-09 06:18:39 +01:00
|
|
|
call_user(callback, ec_);
|
2018-03-11 22:12:39 +01:00
|
|
|
}
|
2018-01-08 12:03:50 +01:00
|
|
|
catch(const std::exception &e)
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) handle timeout: %s",
|
|
|
|
(const void *)this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-20 16:01:14 +01:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
assert(0);
|
2018-01-08 12:03:50 +01:00
|
|
|
if(callback)
|
|
|
|
call_user(callback, ec);
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
void
|
|
|
|
ircd::net::socket::handle_connect(std::weak_ptr<socket> wp,
|
2018-01-07 23:37:33 +01:00
|
|
|
const open_opts opts,
|
2018-01-07 06:34:02 +01:00
|
|
|
eptr_handler callback,
|
2018-01-20 12:14:14 +01:00
|
|
|
error_code ec)
|
2018-01-04 23:20:30 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
const life_guard<socket> s{wp};
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-12 18:05:15 +01:00
|
|
|
log, "%s connect %s",
|
|
|
|
loghead(*this),
|
2018-09-30 02:28:11 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2018-01-04 23:20:30 +01:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
// The timer was set by socket::connect() and may need to be canceled.
|
2018-11-09 09:29:31 +01:00
|
|
|
if(!timedout && !is(ec, errc::operation_canceled) && !fini)
|
2018-02-28 02:20:31 +01:00
|
|
|
cancel_timeout();
|
2018-03-10 21:52:23 +01:00
|
|
|
|
2018-11-09 09:29:31 +01:00
|
|
|
if(timedout && is(ec, errc::operation_canceled))
|
2018-11-09 06:18:39 +01:00
|
|
|
ec = make_error_code(errc::timed_out);
|
2018-01-06 02:01:37 +01:00
|
|
|
|
|
|
|
// A connect error; abort here by calling the user back with error.
|
2018-01-04 23:20:30 +01:00
|
|
|
if(ec)
|
2018-01-06 02:01:37 +01:00
|
|
|
return call_user(callback, ec);
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
// Toggles the behavior of non-async functions; see func comment
|
|
|
|
blocking(*this, false);
|
|
|
|
|
|
|
|
// Try to set the user's socket options now; if something fails we can
|
|
|
|
// invoke their callback with the error from the exception handler.
|
|
|
|
if(opts.sopts)
|
2018-01-06 02:01:37 +01:00
|
|
|
set(*this, *opts.sopts);
|
|
|
|
|
|
|
|
// The user can opt out of performing the handshake here.
|
|
|
|
if(!opts.handshake)
|
|
|
|
return call_user(callback, ec);
|
2018-01-04 23:20:30 +01:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
handshake(opts, std::move(callback));
|
2018-01-04 23:20:30 +01:00
|
|
|
}
|
2018-01-20 16:01:14 +01:00
|
|
|
catch(const std::bad_weak_ptr &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "socket(%p) belated callback to handle_connect... (%s)",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-20 16:01:14 +01:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
}
|
2018-01-04 23:20:30 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) handle_connect: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
call_user(callback, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::socket::handle_disconnect(std::shared_ptr<socket> s,
|
|
|
|
eptr_handler callback,
|
2018-01-16 04:46:23 +01:00
|
|
|
error_code ec)
|
2018-01-07 06:34:02 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2018-03-10 22:42:55 +01:00
|
|
|
assert(fini);
|
2018-11-09 06:18:39 +01:00
|
|
|
if(!timedout && ec != errc::operation_canceled)
|
2018-02-28 02:20:31 +01:00
|
|
|
cancel_timeout();
|
2018-03-10 21:52:23 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(timedout && ec == errc::operation_canceled)
|
|
|
|
ec = make_error_code(errc::timed_out);
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-12 18:05:15 +01:00
|
|
|
log, "%s disconnect %s",
|
|
|
|
loghead(*this),
|
2018-09-30 02:28:11 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
2018-01-16 04:46:23 +01:00
|
|
|
// This ignores EOF and turns it into a success to alleviate user concern.
|
|
|
|
if(ec.category() == asio::error::get_misc_category())
|
|
|
|
if(ec.value() == asio::error::eof)
|
|
|
|
ec = error_code{};
|
|
|
|
|
2018-01-11 12:23:40 +01:00
|
|
|
sd.close();
|
2018-01-07 06:34:02 +01:00
|
|
|
call_user(callback, ec);
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "socket(%p) disconnect: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
call_user(callback, e.code());
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) disconnect: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
assert(0);
|
2018-01-07 06:34:02 +01:00
|
|
|
call_user(callback, ec);
|
2018-01-04 23:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::socket::handle_handshake(std::weak_ptr<socket> wp,
|
2018-01-07 06:34:02 +01:00
|
|
|
eptr_handler callback,
|
2018-01-20 12:14:14 +01:00
|
|
|
error_code ec)
|
2018-01-04 23:20:30 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
const life_guard<socket> s{wp};
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(!timedout && ec != errc::operation_canceled && !fini)
|
2018-02-28 02:20:31 +01:00
|
|
|
cancel_timeout();
|
2018-03-10 21:52:23 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(timedout && ec == errc::operation_canceled)
|
|
|
|
ec = make_error_code(errc::timed_out);
|
2018-01-20 12:14:14 +01:00
|
|
|
|
2019-03-05 20:34:55 +01:00
|
|
|
#ifdef RB_DEBUG
|
|
|
|
const auto ¤t_cipher
|
|
|
|
{
|
|
|
|
openssl::current_cipher(*this)
|
|
|
|
};
|
|
|
|
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-03-05 20:34:55 +01:00
|
|
|
log, "%s handshake cipher:%s %s",
|
2018-12-12 18:05:15 +01:00
|
|
|
loghead(*this),
|
2019-03-05 20:34:55 +01:00
|
|
|
current_cipher?
|
|
|
|
openssl::name(*current_cipher):
|
|
|
|
"<NO CIPHER>"_sv,
|
2018-09-30 02:28:11 +02:00
|
|
|
string(ec)
|
|
|
|
};
|
2019-03-05 20:34:55 +01:00
|
|
|
#endif
|
2018-01-04 23:20:30 +01:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
// This is the end of the asynchronous call chain; the user is called
|
|
|
|
// back with or without error here.
|
2018-01-04 23:20:30 +01:00
|
|
|
call_user(callback, ec);
|
|
|
|
}
|
2018-01-07 06:34:02 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "socket(%p) after handshake: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
call_user(callback, e.code());
|
|
|
|
}
|
2018-01-04 23:20:30 +01:00
|
|
|
catch(const std::bad_weak_ptr &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "socket(%p) belated callback to handle_handshake... (%s)",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-20 16:01:14 +01:00
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) handle_handshake: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-20 16:01:14 +01:00
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
assert(0);
|
2018-01-07 06:34:02 +01:00
|
|
|
call_user(callback, ec);
|
2018-01-04 23:20:30 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 08:14:21 +01:00
|
|
|
bool
|
|
|
|
ircd::net::socket::handle_verify(const bool valid,
|
|
|
|
asio::ssl::verify_context &vc,
|
2018-01-07 06:34:02 +01:00
|
|
|
const open_opts &opts)
|
2018-01-05 08:14:21 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
2018-01-06 02:01:37 +01:00
|
|
|
// `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 &
|
2018-01-05 08:14:21 +01:00
|
|
|
assert(vc.native_handle());
|
|
|
|
const auto &stctx{*vc.native_handle()};
|
2018-01-06 02:01:37 +01:00
|
|
|
const auto &cert{openssl::current_cert(stctx)};
|
2018-01-08 02:45:41 +01:00
|
|
|
const auto reject{[&stctx, &opts]
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
throw inauthentic
|
|
|
|
{
|
|
|
|
"%s #%ld: %s",
|
2018-01-08 02:45:41 +01:00
|
|
|
common_name(opts),
|
2018-01-06 02:01:37 +01:00
|
|
|
openssl::get_error(stctx),
|
|
|
|
openssl::get_error_string(stctx)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(!valid)
|
|
|
|
{
|
2018-01-13 00:32:58 +01:00
|
|
|
thread_local char buf[4_KiB];
|
2018-01-12 03:41:27 +01:00
|
|
|
const critical_assertion ca;
|
2018-09-30 02:28:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "verify[%s]: %s :%s",
|
|
|
|
common_name(opts),
|
|
|
|
openssl::get_error_string(stctx),
|
|
|
|
openssl::print_subject(buf, cert)
|
|
|
|
};
|
2018-01-06 02:01:37 +01:00
|
|
|
}
|
2018-01-05 08:14:21 +01:00
|
|
|
|
2018-01-22 09:31:33 +01:00
|
|
|
const auto err
|
|
|
|
{
|
|
|
|
openssl::get_error(stctx)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!valid) switch(err)
|
2018-01-05 08:14:21 +01:00
|
|
|
{
|
|
|
|
case X509_V_OK:
|
2018-01-06 02:01:37 +01:00
|
|
|
assert(0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
reject();
|
2018-01-05 08:14:21 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
|
|
|
|
assert(openssl::get_error_depth(stctx) == 0);
|
2018-01-06 02:01:37 +01:00
|
|
|
if(opts.allow_self_signed)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
reject();
|
2018-01-05 08:14:21 +01:00
|
|
|
break;
|
|
|
|
|
2018-10-04 23:56:08 +02:00
|
|
|
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
|
|
|
|
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
|
|
|
|
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
|
2018-01-05 08:14:21 +01:00
|
|
|
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
|
2019-02-07 10:51:51 +01:00
|
|
|
if(opts.allow_self_signed || opts.allow_self_chain)
|
2018-01-06 02:01:37 +01:00
|
|
|
return true;
|
2018-01-05 08:14:21 +01:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
reject();
|
2018-01-05 08:14:21 +01:00
|
|
|
break;
|
2018-02-11 03:36:23 +01:00
|
|
|
|
|
|
|
case X509_V_ERR_CERT_HAS_EXPIRED:
|
|
|
|
if(opts.allow_expired)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
reject();
|
|
|
|
break;
|
2018-01-05 08:14:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 09:31:33 +01:00
|
|
|
const bool verify_common_name
|
|
|
|
{
|
|
|
|
opts.verify_common_name &&
|
|
|
|
(opts.verify_self_signed_common_name && err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(verify_common_name)
|
2018-01-05 08:14:21 +01:00
|
|
|
{
|
2018-01-08 02:45:41 +01:00
|
|
|
if(unlikely(empty(common_name(opts))))
|
|
|
|
throw inauthentic
|
|
|
|
{
|
|
|
|
"No common name specified in connection options"
|
|
|
|
};
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
//TODO: this object makes an std::string
|
|
|
|
boost::asio::ssl::rfc2818_verification verifier
|
|
|
|
{
|
2018-01-08 02:45:41 +01:00
|
|
|
std::string(common_name(opts))
|
2018-01-06 02:01:37 +01:00
|
|
|
};
|
2018-01-05 08:14:21 +01:00
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
if(!verifier(true, vc))
|
|
|
|
{
|
2018-11-30 02:56:32 +01:00
|
|
|
thread_local char buf[rfc1035::NAME_BUF_SIZE];
|
2018-01-12 03:41:27 +01:00
|
|
|
const critical_assertion ca;
|
2018-01-06 02:01:37 +01:00
|
|
|
throw inauthentic
|
|
|
|
{
|
|
|
|
"/CN=%s does not match target host %s :%s",
|
|
|
|
openssl::subject_common_name(buf, cert),
|
2018-01-08 02:45:41 +01:00
|
|
|
common_name(opts),
|
2018-01-06 02:01:37 +01:00
|
|
|
openssl::get_error_string(stctx)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 08:14:21 +01:00
|
|
|
|
2018-01-12 03:41:27 +01:00
|
|
|
{
|
2018-01-13 00:32:58 +01:00
|
|
|
thread_local char buf[4_KiB];
|
2018-01-12 03:41:27 +01:00
|
|
|
const critical_assertion ca;
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "verify[%s]: %s",
|
|
|
|
common_name(opts),
|
|
|
|
openssl::print_subject(buf, cert)
|
|
|
|
};
|
2018-01-12 03:41:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const inauthentic &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Certificate rejected: %s", e.what()
|
|
|
|
};
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
return false;
|
2018-01-05 08:14:21 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "Certificate error: %s", e.what()
|
|
|
|
};
|
|
|
|
|
2018-01-05 08:14:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-01 10:42:00 +01:00
|
|
|
void
|
2018-01-07 06:34:02 +01:00
|
|
|
ircd::net::socket::call_user(const ec_handler &callback,
|
2018-01-01 10:42:00 +01:00
|
|
|
const error_code &ec)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
callback(ec);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) async handler: unhandled exception: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-23 20:40:19 +01:00
|
|
|
|
|
|
|
close(*this, dc::RST, close_ignore);
|
2018-01-01 10:42:00 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 06:34:02 +01:00
|
|
|
void
|
|
|
|
ircd::net::socket::call_user(const eptr_handler &callback,
|
|
|
|
const error_code &ec)
|
|
|
|
noexcept try
|
|
|
|
{
|
2018-03-08 18:27:32 +01:00
|
|
|
if(likely(!ec))
|
|
|
|
return callback(std::exception_ptr{});
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
callback(make_system_eptr(ec));
|
2018-01-07 06:34:02 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-30 02:28:11 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) async handler: unhandled exception: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 23:20:30 +01:00
|
|
|
ircd::milliseconds
|
2017-10-19 12:55:24 +02:00
|
|
|
ircd::net::socket::cancel_timeout()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-02-07 07:22:54 +01:00
|
|
|
const auto exp
|
2018-01-04 23:20:30 +01:00
|
|
|
{
|
|
|
|
timer.expires_from_now()
|
|
|
|
};
|
|
|
|
|
2018-02-07 07:22:54 +01:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
duration_cast<milliseconds>(exp)
|
|
|
|
};
|
|
|
|
|
2018-04-16 00:55:25 +02:00
|
|
|
timer_set = false;
|
2018-03-09 23:20:01 +01:00
|
|
|
timedout = false;
|
2017-10-19 12:55:24 +02:00
|
|
|
boost::system::error_code ec;
|
|
|
|
timer.cancel(ec);
|
2018-01-04 23:20:30 +01:00
|
|
|
assert(!ec);
|
2018-02-07 07:22:54 +01:00
|
|
|
return ret;
|
2017-10-19 12:55:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
void
|
|
|
|
ircd::net::socket::set_timeout(const milliseconds &t)
|
|
|
|
{
|
2018-01-08 12:03:50 +01:00
|
|
|
set_timeout(t, nullptr);
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::socket::set_timeout(const milliseconds &t,
|
2018-01-08 12:03:50 +01:00
|
|
|
ec_handler callback)
|
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;
|
|
|
|
|
2018-01-08 12:03:50 +01:00
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&socket::handle_timeout, this, weak_from(*this), std::move(callback), ph::_1)
|
|
|
|
};
|
|
|
|
|
2018-04-16 00:55:25 +02:00
|
|
|
// The sending-side of the semaphore is incremented here to invalidate any
|
|
|
|
// pending/queued callbacks to handle_timeout as to not conflict now. The
|
|
|
|
// required companion boolean timer_set is also lit here.
|
|
|
|
assert(timer_sem[0] <= timer_sem[1]);
|
|
|
|
assert(timer_set == false);
|
|
|
|
assert(timedout == false);
|
|
|
|
++timer_sem[1];
|
|
|
|
timer_set = true;
|
2017-09-30 08:04:41 +02:00
|
|
|
timer.expires_from_now(t);
|
2018-01-08 12:03:50 +01:00
|
|
|
timer.async_wait(std::move(handler));
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
|
|
|
|
2018-02-27 07:49:44 +01: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-11-16 02:27:36 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:37:37 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2018-01-28 18:37:13 +01:00
|
|
|
// net/dns.h
|
2017-10-25 18:37:37 +02:00
|
|
|
//
|
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
/// Linkage for default opts
|
|
|
|
decltype(ircd::net::dns::opts_default)
|
2018-10-01 20:52:59 +02:00
|
|
|
ircd::net::dns::opts_default;
|
2017-10-25 18:37:37 +02:00
|
|
|
|
2018-04-15 01:20:21 +02:00
|
|
|
decltype(ircd::net::dns::prefetch_ipport)
|
|
|
|
ircd::net::dns::prefetch_ipport{[]
|
2018-05-02 03:40:03 +02:00
|
|
|
(std::exception_ptr, const auto &hostport, const auto &record)
|
2018-04-15 01:20:21 +02:00
|
|
|
{
|
|
|
|
// Do nothing; cache already updated if necessary
|
|
|
|
}};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::prefetch_SRV)
|
|
|
|
ircd::net::dns::prefetch_SRV{[]
|
2018-05-02 03:40:03 +02:00
|
|
|
(std::exception_ptr, const auto &hostport, const auto &record)
|
2018-04-15 01:20:21 +02:00
|
|
|
{
|
|
|
|
// Do nothing; cache already updated if necessary
|
|
|
|
}};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::prefetch_A)
|
|
|
|
ircd::net::dns::prefetch_A{[]
|
2018-05-02 03:40:03 +02:00
|
|
|
(std::exception_ptr, const auto &hostport, const auto &record)
|
2018-04-15 01:20:21 +02:00
|
|
|
{
|
|
|
|
// Do nothing; cache already updated if necessary
|
|
|
|
}};
|
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
/// Convenience composition with a single ipport callback. This is the result of
|
|
|
|
/// an automatic chain of queries such as SRV and A/AAAA based on the input and
|
|
|
|
/// intermediate results.
|
|
|
|
void
|
2018-10-01 20:52:59 +02:00
|
|
|
ircd::net::dns::resolve(const hostport &hp,
|
2018-10-01 21:46:35 +02:00
|
|
|
const opts &op,
|
|
|
|
callback_ipport_one cb)
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2018-10-03 23:18:27 +02:00
|
|
|
using prototype = void (const hostport &, opts, callback_ipport_one);
|
2018-05-10 06:12:01 +02:00
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
static mods::import<prototype> function
|
2018-02-07 05:58:13 +01:00
|
|
|
{
|
2018-10-01 21:46:35 +02:00
|
|
|
"s_dns", "_resolve_ipport"
|
|
|
|
};
|
2018-04-29 01:46:42 +02:00
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
function(hp, op, std::move(cb));
|
2017-10-25 18:37:37 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
/// Convenience callback with a single SRV record which was selected from
|
|
|
|
/// the vector with stochastic respect for weighting and priority.
|
2018-01-04 22:34:07 +01:00
|
|
|
void
|
2018-10-01 20:52:59 +02:00
|
|
|
ircd::net::dns::resolve(const hostport &hp,
|
2018-10-01 21:46:35 +02:00
|
|
|
const opts &op,
|
|
|
|
callback_SRV_one cb)
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2018-10-03 23:18:27 +02:00
|
|
|
using prototype = void (const hostport &, opts, callback_SRV_one);
|
2017-10-25 18:37:37 +02:00
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"s_dns", "_resolve__SRV"
|
|
|
|
};
|
2018-02-07 05:58:13 +01:00
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
function(hp, op, std::move(cb));
|
2017-10-25 18:37:37 +02:00
|
|
|
}
|
2017-12-29 23:55:26 +01:00
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
/// Convenience callback with a single A record which was selected from
|
|
|
|
/// the vector randomly.
|
2018-01-04 22:34:07 +01:00
|
|
|
void
|
2018-10-01 20:52:59 +02:00
|
|
|
ircd::net::dns::resolve(const hostport &hp,
|
2018-10-01 21:46:35 +02:00
|
|
|
const opts &op,
|
|
|
|
callback_A_one cb)
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2018-10-03 23:18:27 +02:00
|
|
|
using prototype = void (const hostport &, opts, callback_A_one);
|
2018-02-03 22:15:55 +01:00
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"s_dns", "_resolve__A"
|
|
|
|
};
|
2018-02-07 05:58:13 +01:00
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
function(hp, op, std::move(cb));
|
2018-01-28 23:19:35 +01:00
|
|
|
}
|
2017-12-29 23:55:26 +01:00
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
/// Fundamental callback with a vector of abstract resource records.
|
2018-01-28 23:19:35 +01:00
|
|
|
void
|
2018-10-01 20:52:59 +02:00
|
|
|
ircd::net::dns::resolve(const hostport &hp,
|
|
|
|
const opts &op,
|
|
|
|
callback cb)
|
2018-01-28 23:19:35 +01:00
|
|
|
{
|
2018-10-01 21:46:35 +02:00
|
|
|
using prototype = void (const hostport &, const opts &, callback);
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
static mods::import<prototype> function
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2018-10-01 21:46:35 +02:00
|
|
|
"s_dns", "_resolve__"
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
function(hp, op, std::move(cb));
|
2017-12-29 23:55:26 +01:00
|
|
|
}
|
2017-10-25 18:37:37 +02:00
|
|
|
|
2018-04-29 02:39:08 +02:00
|
|
|
/// Really assumptional and hacky right now. We're just assuming the SRV
|
|
|
|
/// key is the first two elements of a dot-delimited string which start
|
|
|
|
/// with underscores. If that isn't good enough in the future this will rot
|
|
|
|
/// and become a regression hazard.
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::dns::unmake_SRV_key(const string_view &key)
|
|
|
|
{
|
|
|
|
if(token_count(key, '.') < 3)
|
|
|
|
return key;
|
|
|
|
|
|
|
|
if(!startswith(token(key, '.', 0), '_'))
|
|
|
|
return key;
|
|
|
|
|
|
|
|
if(!startswith(token(key, '.', 1), '_'))
|
|
|
|
return key;
|
|
|
|
|
|
|
|
return tokens_after(key, '.', 1);
|
|
|
|
}
|
|
|
|
|
2018-04-15 01:39:13 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::dns::make_SRV_key(const mutable_buffer &out,
|
|
|
|
const hostport &hp,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
if(!opts.srv)
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "_%s._%s.%s", service(hp), opts.proto, host(hp)
|
|
|
|
};
|
|
|
|
else
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "%s%s", opts.srv, host(hp)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// cache
|
|
|
|
//
|
|
|
|
|
2018-04-29 01:25:20 +02:00
|
|
|
ircd::rfc1035::record *
|
2018-04-15 01:39:13 +02:00
|
|
|
ircd::net::dns::cache::put_error(const rfc1035::question &question,
|
|
|
|
const uint &code)
|
2018-10-01 04:01:06 +02:00
|
|
|
try
|
2018-04-15 01:39:13 +02:00
|
|
|
{
|
2018-10-01 04:01:06 +02:00
|
|
|
using prototype = rfc1035::record *(const rfc1035::question &, const uint &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
2018-04-15 01:39:13 +02:00
|
|
|
{
|
2018-10-01 21:46:35 +02:00
|
|
|
"s_dns", "_put_error"
|
2018-04-15 01:39:13 +02:00
|
|
|
};
|
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
return function(question, code);
|
|
|
|
}
|
|
|
|
catch(const mods::unavailable &e)
|
|
|
|
{
|
|
|
|
log::dwarning
|
2018-04-15 01:39:13 +02:00
|
|
|
{
|
2018-10-01 04:01:06 +02:00
|
|
|
log, "Failed to put error for '%s' in DNS cache :%s",
|
|
|
|
question.name,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-04-15 01:39:13 +02:00
|
|
|
|
2018-04-29 01:25:20 +02:00
|
|
|
return nullptr;
|
2018-04-15 01:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::rfc1035::record *
|
|
|
|
ircd::net::dns::cache::put(const rfc1035::question &question,
|
|
|
|
const rfc1035::answer &answer)
|
2018-10-01 04:01:06 +02:00
|
|
|
try
|
2018-04-15 01:39:13 +02:00
|
|
|
{
|
2018-10-01 04:01:06 +02:00
|
|
|
using prototype = rfc1035::record *(const rfc1035::question &, const rfc1035::answer &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
2018-04-15 01:39:13 +02:00
|
|
|
{
|
2018-10-01 21:46:35 +02:00
|
|
|
"s_dns", "_put"
|
2018-04-15 01:39:13 +02:00
|
|
|
};
|
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
return function(question, answer);
|
|
|
|
}
|
|
|
|
catch(const mods::unavailable &e)
|
|
|
|
{
|
|
|
|
log::dwarning
|
2018-04-15 01:39:13 +02:00
|
|
|
{
|
2018-10-01 04:01:06 +02:00
|
|
|
log, "Failed to put '%s' in DNS cache :%s",
|
|
|
|
question.name,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-04-15 01:39:13 +02:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
return nullptr;
|
2018-04-15 01:39:13 +02:00
|
|
|
}
|
|
|
|
|
2018-03-02 08:55:10 +01:00
|
|
|
/// This function has an opportunity to respond from the DNS cache. If it
|
|
|
|
/// returns true, that indicates it responded by calling back the user and
|
|
|
|
/// nothing further should be done for them. If it returns false, that
|
|
|
|
/// indicates it did not respond and to proceed normally. The response can
|
|
|
|
/// be of a cached successful result, or a cached error. Both will return
|
|
|
|
/// true.
|
2018-03-02 08:08:22 +01:00
|
|
|
bool
|
2018-04-15 01:39:13 +02:00
|
|
|
ircd::net::dns::cache::get(const hostport &hp,
|
2018-10-01 04:01:06 +02:00
|
|
|
const opts &o,
|
2018-04-15 01:39:13 +02:00
|
|
|
const callback &cb)
|
2018-10-01 04:01:06 +02:00
|
|
|
try
|
2018-03-02 08:08:22 +01:00
|
|
|
{
|
2018-10-01 04:01:06 +02:00
|
|
|
using prototype = bool (const hostport &, const opts &, const callback &);
|
2018-03-02 08:55:10 +01:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-02 08:08:22 +01:00
|
|
|
{
|
2018-10-01 21:46:35 +02:00
|
|
|
"s_dns", "_get"
|
2018-10-01 04:01:06 +02:00
|
|
|
};
|
2018-03-02 08:08:22 +01:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
return function(hp, o, cb);
|
|
|
|
}
|
|
|
|
catch(const mods::unavailable &e)
|
|
|
|
{
|
|
|
|
thread_local char buf[128];
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "Failed to get '%s' from DNS cache :%s",
|
|
|
|
string(buf, hp),
|
|
|
|
e.what()
|
|
|
|
};
|
2018-03-02 08:55:10 +01:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-15 02:31:28 +02:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
bool
|
|
|
|
ircd::net::dns::cache::for_each(const string_view &type,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
return for_each(rfc1035::qtype.at(type), closure);
|
|
|
|
}
|
2018-03-02 08:08:22 +01:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
bool
|
|
|
|
ircd::net::dns::cache::for_each(const uint16_t &type,
|
|
|
|
const closure &c)
|
|
|
|
{
|
|
|
|
using prototype = bool (const uint16_t &, const closure &);
|
2018-03-02 08:08:22 +01:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2018-10-01 21:46:35 +02:00
|
|
|
"s_dns", "_for_each"
|
2018-10-01 04:01:06 +02:00
|
|
|
};
|
2018-03-02 08:55:10 +01:00
|
|
|
|
2018-10-01 04:01:06 +02:00
|
|
|
return function(type, c);
|
2018-03-02 08:08:22 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-12-30 06:42:39 +01:00
|
|
|
//
|
2018-01-07 22:48:03 +01:00
|
|
|
// net/ipport.h
|
2017-12-30 06:42:39 +01:00
|
|
|
//
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const ipport &t)
|
|
|
|
{
|
2018-01-12 03:41:27 +01:00
|
|
|
thread_local char buf[256];
|
|
|
|
const critical_assertion ca;
|
2018-05-02 21:12:19 +02:00
|
|
|
s << net::string(buf, t);
|
2018-01-07 22:48:03 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-12-30 06:42:39 +01:00
|
|
|
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 ipport &ipp)
|
|
|
|
{
|
|
|
|
const auto len
|
|
|
|
{
|
2018-05-02 21:12:19 +02:00
|
|
|
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",
|
2019-03-01 00:22:21 +01:00
|
|
|
ip::address_v6{std::get<ipp.IP>(ipp).byte}.to_string(),
|
2018-05-02 21:12:19 +02:00
|
|
|
port(ipp)
|
|
|
|
}:
|
2017-12-30 06:42:39 +01:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
return { data(buf), size_t(len) };
|
|
|
|
}
|
|
|
|
|
2018-01-28 23:18:47 +01:00
|
|
|
ircd::net::ipport
|
|
|
|
ircd::net::make_ipport(const boost::asio::ip::udp::endpoint &ep)
|
|
|
|
{
|
|
|
|
return ipport
|
|
|
|
{
|
|
|
|
ep.address(), ep.port()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-30 06:42:39 +01:00
|
|
|
ircd::net::ipport
|
|
|
|
ircd::net::make_ipport(const boost::asio::ip::tcp::endpoint &ep)
|
|
|
|
{
|
|
|
|
return ipport
|
|
|
|
{
|
|
|
|
ep.address(), ep.port()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-28 23:18:47 +01:00
|
|
|
boost::asio::ip::udp::endpoint
|
|
|
|
ircd::net::make_endpoint_udp(const ipport &ipport)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
is_v6(ipport)? ip::udp::endpoint
|
|
|
|
{
|
2019-03-01 00:22:21 +01:00
|
|
|
asio::ip::address_v6 { std::get<ipport.IP>(ipport).byte }, port(ipport)
|
2018-01-28 23:18:47 +01:00
|
|
|
}
|
|
|
|
: ip::udp::endpoint
|
|
|
|
{
|
|
|
|
asio::ip::address_v4 { host4(ipport) }, port(ipport)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-30 06:42:39 +01:00
|
|
|
boost::asio::ip::tcp::endpoint
|
|
|
|
ircd::net::make_endpoint(const ipport &ipport)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
is_v6(ipport)? ip::tcp::endpoint
|
|
|
|
{
|
2019-03-01 00:22:21 +01:00
|
|
|
asio::ip::address_v6 { std::get<ipport.IP>(ipport).byte }, port(ipport)
|
2017-12-30 06:42:39 +01:00
|
|
|
}
|
|
|
|
: ip::tcp::endpoint
|
|
|
|
{
|
|
|
|
asio::ip::address_v4 { host4(ipport) }, port(ipport)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-30 01:46:02 +02:00
|
|
|
bool
|
|
|
|
ircd::net::ipport::cmp_ip::operator()(const ipport &a, const ipport &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(is_v4(a) && is_v6(b))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(is_v6(a) && is_v4(b))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert((is_v4(a) && is_v4(b)) || (is_v6(a) && is_v6(b)));
|
2019-03-01 00:22:21 +01:00
|
|
|
return std::get<a.IP>(a).byte < std::get<b.IP>(b).byte;
|
2018-09-30 01:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::ipport::cmp_port::operator()(const ipport &a, const ipport &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::get<a.PORT>(a) < std::get<b.PORT>(b);
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
//
|
2018-09-30 01:31:27 +02:00
|
|
|
// ipport::ipport
|
2018-01-07 22:48:03 +01:00
|
|
|
//
|
|
|
|
|
2018-01-08 02:44:49 +01:00
|
|
|
ircd::net::ipport::ipport(const string_view &ip,
|
|
|
|
const string_view &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
ip, lex_cast<uint16_t>(port)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const string_view &ip,
|
|
|
|
const uint16_t &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
asio::ip::make_address(ip), port
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-03 20:06:16 +01:00
|
|
|
ircd::net::ipport::ipport(const rfc1035::record::A &rr,
|
|
|
|
const uint16_t &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
rr.ip4, port
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const rfc1035::record::AAAA &rr,
|
|
|
|
const uint16_t &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
rr.ip6, port
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-30 06:42:39 +01:00
|
|
|
ircd::net::ipport::ipport(const boost::asio::ip::address &address,
|
|
|
|
const uint16_t &port)
|
|
|
|
{
|
|
|
|
std::get<TYPE>(*this) = address.is_v6();
|
2018-01-08 02:44:49 +01:00
|
|
|
std::get<PORT>(*this) = port;
|
2017-12-30 06:42:39 +01:00
|
|
|
|
|
|
|
if(is_v6(*this))
|
|
|
|
{
|
2019-03-01 00:22:21 +01:00
|
|
|
std::get<IP>(*this).byte = address.to_v6().to_bytes();
|
|
|
|
std::reverse(std::get<IP>(*this).byte.begin(), std::get<IP>(*this).byte.end());
|
2017-12-30 06:42:39 +01:00
|
|
|
}
|
|
|
|
else host4(*this) = address.to_v4().to_ulong();
|
|
|
|
}
|
|
|
|
|
2018-09-30 01:31:27 +02:00
|
|
|
ircd::net::ipport::ipport(const uint32_t &ip,
|
|
|
|
const uint16_t &p)
|
|
|
|
{
|
|
|
|
std::get<TYPE>(*this) = false;
|
|
|
|
host6(*this) = 0;
|
|
|
|
host4(*this) = ip;
|
|
|
|
port(*this) = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::ipport::ipport(const uint128_t &ip,
|
|
|
|
const uint16_t &p)
|
|
|
|
{
|
|
|
|
std::get<TYPE>(*this) = true;
|
|
|
|
host6(*this) = ip;
|
|
|
|
port(*this) = p;
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-10-25 18:37:37 +02:00
|
|
|
//
|
2018-01-07 22:48:03 +01:00
|
|
|
// net/hostport.h
|
2017-10-25 18:37:37 +02:00
|
|
|
//
|
|
|
|
|
2018-05-20 03:25:11 +02:00
|
|
|
decltype(ircd::net::canon_port)
|
|
|
|
ircd::net::canon_port
|
|
|
|
{
|
|
|
|
8448
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::canon_service)
|
|
|
|
ircd::net::canon_service
|
|
|
|
{
|
|
|
|
"matrix"
|
|
|
|
};
|
|
|
|
|
2017-12-29 23:55:26 +01:00
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const hostport &t)
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2018-01-12 03:41:27 +01:00
|
|
|
thread_local char buf[256];
|
|
|
|
const critical_assertion ca;
|
2017-12-29 23:55:26 +01:00
|
|
|
s << string(buf, t);
|
|
|
|
return s;
|
2017-10-25 18:37:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-14 00:49:41 +01:00
|
|
|
std::string
|
|
|
|
ircd::net::canonize(const hostport &hp,
|
|
|
|
const uint16_t &port)
|
|
|
|
{
|
|
|
|
const size_t len
|
|
|
|
{
|
2018-05-20 03:25:11 +02:00
|
|
|
size(host(hp)) + 1 + 5 + 1 // optimistic ':' + portnum
|
2018-03-14 00:49:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return ircd::string(len, [&hp, &port]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return canonize(buf, hp, port);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::canonize(const mutable_buffer &buf,
|
|
|
|
const hostport &hp,
|
|
|
|
const uint16_t &port)
|
|
|
|
{
|
|
|
|
if(net::port(hp) == 0 || net::port(hp) == port)
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s", host(hp)
|
|
|
|
};
|
|
|
|
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s:%u", host(hp), net::port(hp)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &buf,
|
|
|
|
const hostport &hp)
|
|
|
|
{
|
2018-02-03 20:38:46 +01:00
|
|
|
if(empty(service(hp)))
|
|
|
|
return fmt::sprintf
|
2018-01-07 22:48:03 +01:00
|
|
|
{
|
2018-01-10 10:20:16 +01:00
|
|
|
buf, "%s:%u", host(hp), port(hp)
|
2018-02-03 20:38:46 +01:00
|
|
|
};
|
2018-01-07 22:48:03 +01:00
|
|
|
|
2018-02-03 20:38:46 +01:00
|
|
|
if(port(hp) == 0)
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s (%s)", host(hp), service(hp)
|
|
|
|
};
|
|
|
|
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s:%u (%s)", host(hp), port(hp), service(hp)
|
|
|
|
};
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-01-10 10:20:16 +01:00
|
|
|
std::string ret(128, char{});
|
2017-12-30 06:45:15 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|