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
|
|
|
|
2018-03-13 02:45:21 +01:00
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
ctx::dock dock;
|
2019-10-06 02:02:50 +02:00
|
|
|
std::optional<dns::init> _dns_;
|
2018-03-13 02:45:21 +01:00
|
|
|
|
2019-03-26 00:31:04 +01:00
|
|
|
static void init_ipv6();
|
|
|
|
static void wait_close_sockets();
|
2018-03-13 02:45:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-26 00:31:04 +01:00
|
|
|
void
|
|
|
|
ircd::net::init_ipv6()
|
|
|
|
{
|
|
|
|
if(!enable_ipv6)
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "IPv6 is disabled by the configuration."
|
|
|
|
" Not checking for usable interfaces."
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!addrs::has_usable_ipv6_interface())
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "No usable IPv6 interfaces detected."
|
|
|
|
};
|
|
|
|
|
|
|
|
enable_ipv6.set("false");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "Detected usable IPv6 interfaces."
|
|
|
|
" Server will query AAAA records and attempt IPv6 connections. If this"
|
|
|
|
" is an error please set ircd.net.enable_ipv6 to false or start with -no6."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:37:13 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Network subsystem initialization
|
|
|
|
ircd::net::init::init()
|
|
|
|
{
|
2019-03-26 00:31:04 +01:00
|
|
|
init_ipv6();
|
2018-01-28 18:37:13 +01:00
|
|
|
sslv23_client.set_verify_mode(asio::ssl::verify_peer);
|
|
|
|
sslv23_client.set_default_verify_paths();
|
2019-10-06 02:02:50 +02:00
|
|
|
_dns_.emplace();
|
2018-01-28 18:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Network subsystem shutdown
|
|
|
|
ircd::net::init::~init()
|
2018-04-29 00:31:07 +02:00
|
|
|
noexcept
|
2018-01-28 18:37:13 +01:00
|
|
|
{
|
2019-10-06 02:02:50 +02:00
|
|
|
_dns_.reset();
|
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
|
|
|
//
|
|
|
|
|
2019-09-13 20:49:08 +02:00
|
|
|
decltype(ircd::net::eof)
|
|
|
|
ircd::net::eof
|
2017-10-25 18:37:37 +02:00
|
|
|
{
|
2019-09-13 20:49:08 +02:00
|
|
|
make_error_code(boost::system::error_code
|
|
|
|
{
|
|
|
|
boost::asio::error::eof,
|
|
|
|
boost::asio::error::get_misc_category()
|
|
|
|
})
|
2017-10-25 18:37:37 +02:00
|
|
|
};
|
|
|
|
|
2019-03-25 23:16:24 +01:00
|
|
|
decltype(ircd::net::enable_ipv6)
|
|
|
|
ircd::net::enable_ipv6
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.enable_ipv6" },
|
|
|
|
{ "default", true },
|
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
2019-09-13 20:49:08 +02:00
|
|
|
/// Network subsystem log facility
|
|
|
|
decltype(ircd::net::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)
|
|
|
|
};
|
|
|
|
|
2020-08-10 12:51:09 +02:00
|
|
|
return b64::encode_unpadded(buf, hash);
|
2018-05-11 05:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-07-19 02:13:00 +02:00
|
|
|
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
|
|
|
{
|
2019-04-16 07:46:24 +02:00
|
|
|
if(!opened(socket))
|
|
|
|
return {};
|
|
|
|
|
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
|
|
|
{
|
2019-04-16 07:46:24 +02:00
|
|
|
if(!opened(socket))
|
|
|
|
return {};
|
|
|
|
|
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)
|
|
|
|
{
|
2020-01-06 01:36:57 +01:00
|
|
|
thread_local char buffer[512];
|
2018-01-12 03:38:29 +01:00
|
|
|
|
|
|
|
size_t remain{len}; while(remain)
|
|
|
|
{
|
|
|
|
const mutable_buffer mb
|
|
|
|
{
|
|
|
|
buffer, std::min(remain, sizeof(buffer))
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
{
|
2020-01-06 01:36:57 +01:00
|
|
|
thread_local char buffer[512];
|
2018-01-14 05:17:17 +01:00
|
|
|
|
2019-04-10 05:22:06 +02:00
|
|
|
size_t remain{len}; while(remain)
|
2018-01-14 05:17:17 +01:00
|
|
|
{
|
|
|
|
const mutable_buffer mb
|
|
|
|
{
|
|
|
|
buffer, std::min(remain, sizeof(buffer))
|
|
|
|
};
|
|
|
|
|
2019-04-10 05:22:06 +02:00
|
|
|
size_t read;
|
|
|
|
if(!(read = read_one(socket, mb)))
|
|
|
|
break;
|
2018-01-14 05:17:17 +01:00
|
|
|
|
2019-04-10 05:22:06 +02:00
|
|
|
remain -= read;
|
2018-01-14 05:17:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-12 22:27:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/check.h
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::check(socket &socket,
|
|
|
|
const ready &type)
|
|
|
|
{
|
2019-07-27 22:17:34 +02:00
|
|
|
const error_code ec
|
|
|
|
{
|
|
|
|
check(std::nothrow, socket, type)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(likely(!ec))
|
|
|
|
return;
|
|
|
|
|
|
|
|
throw_system_error(ec);
|
|
|
|
__builtin_unreachable();
|
2019-03-12 22:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::net::check(std::nothrow_t,
|
|
|
|
socket &socket,
|
|
|
|
const ready &type)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return socket.check(std::nothrow, type);
|
|
|
|
}
|
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/wait.h
|
|
|
|
//
|
|
|
|
|
2019-06-23 01:11:41 +02:00
|
|
|
decltype(ircd::net::wait_opts_default)
|
|
|
|
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)
|
2019-09-18 01:37:27 +02:00
|
|
|
close(*s, dc::RST, close_ignore);
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
handler(std::move(eptr));
|
|
|
|
}};
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
const dns::callback_ipport 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)
|
2019-03-22 02:24:36 +01:00
|
|
|
dns::resolve(opts.hostport, dns::opts_default, 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)
|
2019-03-25 00:20:05 +01:00
|
|
|
:v6only{net::v6only(socket)}
|
|
|
|
,blocking{net::blocking(socket)}
|
2018-01-06 02:01:37 +01:00
|
|
|
,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
|
|
|
{
|
2019-03-25 00:20:05 +01:00
|
|
|
if(opts.v6only != opts.IGN)
|
|
|
|
net::v6only(socket, opts.v6only);
|
|
|
|
|
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());
|
2019-03-25 00:21:01 +01:00
|
|
|
const ip::tcp::socket::send_low_watermark option
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
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());
|
2019-03-25 00:21:01 +01:00
|
|
|
const ip::tcp::socket::receive_low_watermark option
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
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());
|
2019-03-25 00:21:01 +01:00
|
|
|
const ip::tcp::socket::send_buffer_size option
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
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());
|
2019-03-25 00:21:01 +01:00
|
|
|
const ip::tcp::socket::receive_buffer_size option
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
|
|
|
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());
|
2019-03-25 00:21:01 +01:00
|
|
|
const ip::tcp::socket::linger option
|
2018-01-06 02:01:37 +01:00
|
|
|
{
|
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)
|
|
|
|
{
|
2019-03-25 00:21:01 +01:00
|
|
|
const ip::tcp::socket::keep_alive option{b};
|
2018-01-06 02:01:37 +01:00
|
|
|
ip::tcp::socket &sd(socket);
|
|
|
|
sd.set_option(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::nodelay(socket &socket,
|
|
|
|
const bool &b)
|
|
|
|
{
|
2019-03-25 00:21:01 +01:00
|
|
|
const ip::tcp::no_delay option{b};
|
2018-01-06 02:01:37 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-25 00:20:05 +01:00
|
|
|
void
|
|
|
|
ircd::net::v6only(socket &socket,
|
|
|
|
const bool &b)
|
|
|
|
{
|
|
|
|
const ip::v6_only option{b};
|
|
|
|
ip::tcp::socket &sd(socket);
|
|
|
|
sd.set_option(option);
|
|
|
|
}
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-25 00:20:05 +01:00
|
|
|
bool
|
|
|
|
ircd::net::v6only(const socket &socket)
|
|
|
|
{
|
|
|
|
const ip::tcp::socket &sd(socket);
|
|
|
|
ip::v6_only option;
|
|
|
|
sd.get_option(option);
|
|
|
|
return option.value();
|
|
|
|
}
|
|
|
|
|
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);
|
2019-04-17 02:32:09 +02:00
|
|
|
other.s = nullptr;
|
2018-04-16 00:15:26 +02:00
|
|
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) scope_timeout::cancel :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
(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
|
|
|
//
|
|
|
|
|
2019-04-16 04:54:31 +02:00
|
|
|
decltype(ircd::net::ssl_curve_list)
|
|
|
|
ircd::net::ssl_curve_list
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.ssl.curve.list" },
|
|
|
|
{ "default", string_view{} },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::ssl_cipher_list)
|
|
|
|
ircd::net::ssl_cipher_list
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.ssl.cipher.list" },
|
|
|
|
{ "default", string_view{} },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::ssl_cipher_blacklist)
|
|
|
|
ircd::net::ssl_cipher_blacklist
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.ssl.cipher.blacklist" },
|
|
|
|
{ "default", string_view{} },
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{};
|
|
|
|
|
2019-07-11 22:11:38 +02:00
|
|
|
decltype(ircd::net::socket::total_bytes_in)
|
|
|
|
ircd::net::socket::total_bytes_in
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.socket.in.total.bytes" },
|
|
|
|
{ "desc", "The total number of bytes received by all sockets" },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::socket::total_bytes_out)
|
|
|
|
ircd::net::socket::total_bytes_out
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.socket.out.total.bytes" },
|
|
|
|
{ "desc", "The total number of bytes received by all sockets" },
|
|
|
|
};
|
2019-07-08 10:22:16 +02:00
|
|
|
|
2019-07-11 22:11:38 +02:00
|
|
|
decltype(ircd::net::socket::total_calls_in)
|
|
|
|
ircd::net::socket::total_calls_in
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.socket.in.total.calls" },
|
|
|
|
{ "desc", "The total number of read operations on all sockets" },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::socket::total_calls_out)
|
|
|
|
ircd::net::socket::total_calls_out
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.socket.out.total.calls" },
|
|
|
|
{ "desc", "The total number of write operations on all sockets" },
|
|
|
|
};
|
2019-07-08 10:22:16 +02:00
|
|
|
|
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
|
|
|
//
|
|
|
|
|
2020-02-27 19:11:59 +01:00
|
|
|
ircd::net::socket::socket(asio::ssl::context &ssl)
|
2018-01-06 02:01:37 +01:00
|
|
|
:sd
|
2017-10-19 12:55:24 +02:00
|
|
|
{
|
2020-02-27 19:11:59 +01:00
|
|
|
ios::get()
|
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
|
|
|
{
|
2020-02-27 19:11:59 +01:00
|
|
|
ios::get()
|
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();
|
|
|
|
|
2019-09-18 01:37:36 +02:00
|
|
|
if(unlikely(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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) close :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:28:40 +02:00
|
|
|
return;
|
2017-09-30 08:04:41 +02:00
|
|
|
}
|
2019-09-18 01:37:36 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "socket(%p) close: unexpected",
|
|
|
|
this,
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::terminate();
|
|
|
|
}
|
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
|
|
|
|
2019-03-27 01:29:50 +01:00
|
|
|
static ios::descriptor desc
|
|
|
|
{
|
|
|
|
"ircd::net::socket connect"
|
|
|
|
};
|
|
|
|
|
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);
|
2019-03-27 01:29:50 +01:00
|
|
|
sd.async_connect(ep, ios::handle(desc, 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
|
|
|
{
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
|
|
|
assert(sd.is_open());
|
2019-04-17 06:27:01 +02:00
|
|
|
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-03-13 21:44:41 +01:00
|
|
|
log, "%s handshaking to '%s' for '%s' to:%ld$ms",
|
2018-12-12 18:05:15 +01:00
|
|
|
loghead(*this),
|
2019-03-13 21:44:41 +01:00
|
|
|
opts.send_sni?
|
|
|
|
server_name(opts):
|
|
|
|
"<no sni>"_sv,
|
2018-09-30 02:28:11 +02:00
|
|
|
common_name(opts),
|
|
|
|
opts.handshake_timeout.count()
|
|
|
|
};
|
2017-10-19 12:55:24 +02:00
|
|
|
|
2019-03-27 01:29:50 +01:00
|
|
|
static ios::descriptor desc
|
|
|
|
{
|
|
|
|
"ircd::net::socket handshake"
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-04-17 04:38:07 +02:00
|
|
|
assert(!fini);
|
2018-01-12 03:50:34 +01:00
|
|
|
set_timeout(opts.handshake_timeout);
|
2019-03-13 21:44:41 +01:00
|
|
|
|
2019-04-15 22:08:04 +02:00
|
|
|
if(opts.send_sni && server_name(opts))
|
2019-03-13 21:44:41 +01:00
|
|
|
openssl::server_name(*this, server_name(opts));
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
ssl.set_verify_callback(std::move(verify_handler));
|
2019-03-27 01:29:50 +01:00
|
|
|
ssl.async_handshake(handshake_type::client, ios::handle(desc, 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
|
|
|
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
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
|
|
|
|
2019-09-15 03:22:18 +02:00
|
|
|
cancel();
|
2018-03-10 00:55:15 +01:00
|
|
|
assert(!fini);
|
|
|
|
fini = true;
|
|
|
|
|
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:
|
|
|
|
{
|
2019-03-27 01:29:50 +01:00
|
|
|
static ios::descriptor desc
|
|
|
|
{
|
|
|
|
"ircd::net::socket shutdown"
|
|
|
|
};
|
|
|
|
|
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);
|
2019-03-27 01:29:50 +01:00
|
|
|
ssl.async_shutdown(ios::handle(desc, 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
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
"socket:%lu disconnect: type: %d :%s",
|
2018-12-31 21:57:32 +01:00
|
|
|
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
|
|
|
|
2019-09-14 20:41:44 +02:00
|
|
|
bool
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::net::socket::cancel()
|
2017-10-19 10:01:07 +02:00
|
|
|
noexcept
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
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);
|
2019-09-14 20:41:44 +02:00
|
|
|
if(unlikely(ec))
|
2018-09-25 07:01:25 +02:00
|
|
|
{
|
2019-09-14 20:41:44 +02:00
|
|
|
thread_local char ecbuf[64];
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "socket:%lu cancel :%s",
|
|
|
|
this->id,
|
|
|
|
string(ecbuf, ec)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return !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
|
|
|
{
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
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
|
|
|
{
|
2019-09-13 20:57:43 +02:00
|
|
|
static ios::descriptor desc[4]
|
|
|
|
{
|
|
|
|
{ "ircd::net::socket::wait ready::ANY" },
|
|
|
|
{ "ircd::net::socket::wait ready::READ" },
|
|
|
|
{ "ircd::net::socket::wait ready::WRITE" },
|
|
|
|
{ "ircd::net::socket::wait ready::ERROR" },
|
|
|
|
};
|
|
|
|
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
2018-01-14 02:53:59 +01:00
|
|
|
set_timeout(opts.timeout);
|
2019-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional unset{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
|
|
|
cancel_timeout();
|
|
|
|
}};
|
|
|
|
|
2019-09-13 20:57:43 +02:00
|
|
|
auto handle
|
2018-01-08 22:25:13 +01:00
|
|
|
{
|
2019-09-13 21:30:05 +02:00
|
|
|
std::bind(&socket::handle_ready, this, weak_from(*this), opts.type, std::move(callback), ph::_1)
|
2019-09-13 20:57:43 +02:00
|
|
|
};
|
2018-01-08 22:25:13 +01:00
|
|
|
|
2019-09-13 20:57:43 +02:00
|
|
|
switch(opts.type)
|
|
|
|
{
|
2018-01-09 02:18:25 +01:00
|
|
|
case ready::READ:
|
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.
|
2019-09-13 20:49:49 +02:00
|
|
|
static char buf[64];
|
2019-09-13 20:57:43 +02:00
|
|
|
static const ilist<mutable_buffer> bufs{buf};
|
2019-09-13 20:49:49 +02:00
|
|
|
if(SSL_peek(ssl.native_handle(), buf, sizeof(buf)) > 0)
|
2018-01-14 02:53:59 +01:00
|
|
|
{
|
2019-09-13 20:57:43 +02:00
|
|
|
ircd::post(desc[1], [handle(std::move(handle))]
|
2018-03-18 07:19:33 +01:00
|
|
|
{
|
2019-09-13 21:30:05 +02:00
|
|
|
handle(error_code{});
|
2018-03-18 07:19:33 +01:00
|
|
|
});
|
|
|
|
|
2019-09-13 21:30:05 +02:00
|
|
|
return;
|
2018-01-14 02:53:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-09-13 21:30:05 +02:00
|
|
|
//sd.async_wait(bufs, sd.message_peek, ios::handle(desc[1], [handle(std::move(handle))]
|
|
|
|
sd.async_receive(bufs, sd.message_peek, ios::handle(desc[1], [handle(std::move(handle))]
|
|
|
|
(const auto &ec, const size_t bytes)
|
|
|
|
{
|
|
|
|
handle
|
|
|
|
(
|
|
|
|
!ec && bytes?
|
|
|
|
error_code{}:
|
|
|
|
!ec && !bytes?
|
|
|
|
net::eof:
|
|
|
|
make_error_code(ec)
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
|
|
|
|
return;
|
2018-01-14 02:53:59 +01:00
|
|
|
}
|
2018-01-08 22:25:13 +01:00
|
|
|
|
2019-09-13 20:57:43 +02:00
|
|
|
case ready::WRITE:
|
|
|
|
{
|
|
|
|
sd.async_wait(wait_type::wait_write, ios::handle(desc[2], std::move(handle)));
|
2019-09-13 21:30:05 +02:00
|
|
|
return;
|
2019-09-13 20:57:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case ready::ERROR:
|
|
|
|
{
|
|
|
|
sd.async_wait(wait_type::wait_error, ios::handle(desc[3], std::move(handle)));
|
2019-09-13 21:30:05 +02:00
|
|
|
return;
|
2019-09-13 20:57:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default: throw ircd::not_implemented{};
|
2018-01-08 22:25:13 +01:00
|
|
|
}
|
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
|
|
|
|
2019-03-12 22:27:42 +01:00
|
|
|
std::error_code
|
|
|
|
ircd::net::socket::check(std::nothrow_t,
|
|
|
|
const ready &type)
|
|
|
|
noexcept
|
|
|
|
{
|
2019-09-13 20:49:49 +02:00
|
|
|
static char buf[64];
|
|
|
|
static const ilist<mutable_buffer> bufs
|
2019-03-12 22:27:42 +01:00
|
|
|
{
|
2019-09-13 20:49:08 +02:00
|
|
|
buf
|
2019-03-12 22:27:42 +01:00
|
|
|
};
|
|
|
|
|
2019-09-15 03:22:34 +02:00
|
|
|
if(!sd.is_open())
|
2019-07-27 23:42:42 +02:00
|
|
|
return make_error_code(std::errc::bad_file_descriptor);
|
|
|
|
|
2019-09-15 03:22:34 +02:00
|
|
|
if(fini)
|
|
|
|
return make_error_code(std::errc::not_connected);
|
|
|
|
|
2019-03-12 22:27:42 +01:00
|
|
|
std::error_code ret;
|
2019-09-13 20:49:49 +02:00
|
|
|
if(SSL_peek(ssl.native_handle(), buf, sizeof(buf)) > 0)
|
2019-03-12 22:27:42 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
assert(!blocking(*this));
|
|
|
|
boost::system::error_code ec;
|
2019-09-13 20:49:49 +02:00
|
|
|
if(sd.receive(bufs, sd.message_peek, ec) > 0)
|
2019-03-12 22:27:42 +01:00
|
|
|
{
|
|
|
|
assert(!ec.value());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ec.value())
|
|
|
|
ret = make_error_code(ec);
|
|
|
|
else
|
|
|
|
ret = eof;
|
|
|
|
|
|
|
|
if(ret == std::errc::resource_unavailable_try_again)
|
|
|
|
ret = {};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-04 01:28:00 +02:00
|
|
|
/// Yields ircd::ctx until buffers are full.
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::read_all(iov&& bufs)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto completion
|
|
|
|
{
|
|
|
|
asio::transfer_all()
|
|
|
|
};
|
|
|
|
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
2019-05-04 01:28:00 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &)
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
|
|
|
size_t ret; continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this, &ret, &bufs]
|
|
|
|
(auto &yield)
|
|
|
|
{
|
|
|
|
ret = asio::async_read(ssl, std::forward<iov>(bufs), completion, yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw std::system_error
|
|
|
|
{
|
2019-09-13 20:49:08 +02:00
|
|
|
eof
|
2019-05-04 01:28:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
++in.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
in.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_in;
|
|
|
|
total_bytes_in += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
throw_system_error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Yields ircd::ctx until remote has sent at least some data.
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::read_few(iov&& bufs)
|
|
|
|
try
|
|
|
|
{
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
2019-05-04 01:28:00 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &)
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
|
|
|
size_t ret; continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this, &ret, &bufs]
|
|
|
|
(auto &yield)
|
|
|
|
{
|
|
|
|
ret = ssl.async_read_some(std::forward<iov>(bufs), yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw std::system_error
|
|
|
|
{
|
2019-09-13 20:49:08 +02:00
|
|
|
eof
|
2019-05-04 01:28:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
++in.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
in.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_in;
|
|
|
|
total_bytes_in += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
throw_system_error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Non-blocking; as much as possible without blocking
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::read_any(iov&& bufs)
|
|
|
|
{
|
|
|
|
static const auto completion
|
|
|
|
{
|
|
|
|
asio::transfer_all()
|
|
|
|
};
|
|
|
|
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
|
|
|
assert(!blocking(*this));
|
2019-05-04 01:28:00 +02:00
|
|
|
boost::system::error_code ec;
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
asio::read(ssl, std::forward<iov>(bufs), completion, ec)
|
|
|
|
};
|
|
|
|
|
|
|
|
++in.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
in.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_in;
|
|
|
|
total_bytes_in += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
|
|
|
|
if(likely(!ec))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if(ec == boost::system::errc::resource_unavailable_try_again)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
throw_system_error(ec);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Non-blocking; One system call only; never throws eof;
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::read_one(iov&& bufs)
|
|
|
|
{
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
2019-05-04 01:28:00 +02:00
|
|
|
assert(!blocking(*this));
|
|
|
|
boost::system::error_code ec;
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
ssl.read_some(std::forward<iov>(bufs), ec)
|
|
|
|
};
|
|
|
|
|
|
|
|
++in.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
in.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_in;
|
|
|
|
total_bytes_in += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
|
|
|
|
if(likely(!ec))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if(ec == boost::system::errc::resource_unavailable_try_again)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
throw_system_error(ec);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Yields ircd::ctx until all buffers are sent.
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_all(iov&& bufs)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto completion
|
|
|
|
{
|
|
|
|
asio::transfer_all()
|
|
|
|
};
|
|
|
|
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
|
|
|
assert(!blocking(*this));
|
2019-05-04 01:28:00 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &)
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
|
|
|
size_t ret; continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this, &ret, &bufs]
|
|
|
|
(auto &yield)
|
|
|
|
{
|
|
|
|
ret = asio::async_write(ssl, std::forward<iov>(bufs), completion, yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
++out.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
out.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_out;
|
|
|
|
total_bytes_out += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
throw_system_error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Yields ircd::ctx until one or more bytes are sent.
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_few(iov&& bufs)
|
|
|
|
try
|
|
|
|
{
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
|
|
|
assert(!blocking(*this));
|
2019-05-04 01:28:00 +02:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &)
|
|
|
|
{
|
|
|
|
this->cancel();
|
|
|
|
}};
|
|
|
|
|
|
|
|
size_t ret; continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this, &ret, &bufs]
|
|
|
|
(auto &yield)
|
|
|
|
{
|
|
|
|
ret = ssl.async_write_some(std::forward<iov>(bufs), yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
++out.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
out.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_out;
|
|
|
|
total_bytes_out += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
throw_system_error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Non-blocking; writes as much as possible without blocking
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_any(iov&& bufs)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto completion
|
|
|
|
{
|
|
|
|
asio::transfer_all()
|
|
|
|
};
|
|
|
|
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
2019-05-04 01:28:00 +02:00
|
|
|
assert(!blocking(*this));
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
asio::write(ssl, std::forward<iov>(bufs), completion)
|
|
|
|
};
|
|
|
|
|
|
|
|
++out.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
out.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_out;
|
|
|
|
total_bytes_out += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
throw_system_error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Non-blocking; Writes one "unit" of data or less; never more.
|
|
|
|
template<class iov>
|
|
|
|
size_t
|
|
|
|
ircd::net::socket::write_one(iov&& bufs)
|
|
|
|
try
|
|
|
|
{
|
2019-09-15 03:22:18 +02:00
|
|
|
assert(!fini);
|
2019-05-04 01:28:00 +02:00
|
|
|
assert(!blocking(*this));
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
ssl.write_some(std::forward<iov>(bufs))
|
|
|
|
};
|
|
|
|
|
|
|
|
++out.calls;
|
2019-07-08 10:22:16 +02:00
|
|
|
out.bytes += ret;
|
2019-07-11 22:11:38 +02:00
|
|
|
++total_calls_out;
|
|
|
|
total_bytes_out += ret;
|
2019-05-04 01:28:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
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,
|
2019-09-13 21:30:05 +02:00
|
|
|
error_code ec)
|
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
|
|
|
|
2019-09-15 03:22:34 +02:00
|
|
|
if(unlikely(!ec && fini))
|
|
|
|
ec = make_error_code(errc::not_connected);
|
|
|
|
|
2019-04-25 01:26:59 +02:00
|
|
|
#ifdef IRCD_DEBUG_NET_SOCKET_READY
|
2019-09-09 23:59:22 +02:00
|
|
|
const auto has_pending
|
|
|
|
{
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
SSL_has_pending(ssl.native_handle())
|
|
|
|
#else
|
|
|
|
0
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-04-16 07:22:14 +02:00
|
|
|
thread_local char ecbuf[64];
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-09-09 23:59:22 +02:00
|
|
|
log, "%s ready %s %s avail:%zu:%zu:%d:%d",
|
2018-12-12 18:05:15 +01:00
|
|
|
loghead(*this),
|
2018-09-30 02:28:11 +02:00
|
|
|
reflect(type),
|
2019-04-16 07:22:14 +02:00
|
|
|
string(ecbuf, ec),
|
2018-09-30 02:28:11 +02:00
|
|
|
type == ready::READ? bytes : 0UL,
|
|
|
|
type == ready::READ? available(*this) : 0UL,
|
2019-09-09 23:59:22 +02:00
|
|
|
has_pending,
|
|
|
|
SSL_pending(ssl.native_handle()),
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
2019-04-25 01:26:59 +02:00
|
|
|
#endif
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) handle :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
"socket(%p): unexpected :%s",
|
|
|
|
(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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) handle timeout :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
(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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) handle timeout :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
(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,
|
2019-04-17 06:27:01 +02: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};
|
2019-08-03 07:34:12 +02:00
|
|
|
thread_local char ecbuf[64], epbuf[128];
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-08-03 07:34:12 +02:00
|
|
|
log, "%s connect to %s :%s",
|
2018-12-12 18:05:15 +01:00
|
|
|
loghead(*this),
|
2019-08-03 07:34:12 +02:00
|
|
|
string(epbuf, opts.ipport),
|
2019-04-16 07:22:14 +02:00
|
|
|
string(ecbuf, ec)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
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
|
|
|
|
2019-04-17 04:23:30 +02:00
|
|
|
if(!ec && opts.handshake && fini)
|
|
|
|
ec = make_error_code(errc::operation_canceled);
|
|
|
|
|
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
|
|
|
// Try to set the user's socket options now; if something fails we can
|
|
|
|
// invoke their callback with the error from the exception handler.
|
2019-04-17 04:38:07 +02:00
|
|
|
if(opts.sopts && !fini)
|
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
|
|
|
|
2019-04-17 04:38:07 +02:00
|
|
|
assert(!fini);
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) handle_connect :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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
|
|
|
|
2019-04-16 07:22:14 +02:00
|
|
|
thread_local char ecbuf[64];
|
2018-09-30 02:28:11 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-12 18:05:15 +01:00
|
|
|
log, "%s disconnect %s",
|
|
|
|
loghead(*this),
|
2019-04-16 07:22:14 +02:00
|
|
|
string(ecbuf, ec)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
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.
|
2019-09-13 20:49:08 +02:00
|
|
|
if(ec == eof)
|
|
|
|
ec = error_code{};
|
2018-01-16 04:46:23 +01:00
|
|
|
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) disconnect :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) disconnect :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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
|
2019-04-17 04:37:42 +02:00
|
|
|
const auto *const current_cipher
|
2019-03-05 20:34:55 +01:00
|
|
|
{
|
2019-04-17 04:37:42 +02:00
|
|
|
!ec?
|
|
|
|
openssl::current_cipher(*this):
|
|
|
|
nullptr
|
2019-03-05 20:34:55 +01:00
|
|
|
};
|
|
|
|
|
2019-04-16 07:22:14 +02:00
|
|
|
thread_local char ecbuf[64];
|
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,
|
2019-04-16 07:22:14 +02:00
|
|
|
string(ecbuf, ec)
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
2019-03-05 20:34:55 +01:00
|
|
|
#endif
|
2018-01-04 23:20:30 +01:00
|
|
|
|
2019-04-12 14:49:22 +02:00
|
|
|
// Toggles the behavior of non-async functions; see func comment
|
2019-04-17 04:38:07 +02:00
|
|
|
if(!ec)
|
|
|
|
blocking(*this, false);
|
2019-04-12 14:49:22 +02: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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) after handshake :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-07 06:34:02 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) handle_handshake :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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
|
2019-04-17 04:20:39 +02:00
|
|
|
// true to still continue.
|
|
|
|
|
|
|
|
// Socket ordered to shut down. We abort the verification here
|
|
|
|
// to allow the open_opts out of scope with the user.
|
|
|
|
if(fini || !sd.is_open())
|
|
|
|
return false;
|
2018-01-06 02:01:37 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2019-04-17 06:11:27 +02:00
|
|
|
thread_local char buf[16_KiB];
|
2018-01-12 03:41:27 +01:00
|
|
|
const critical_assertion ca;
|
2018-09-30 02:28:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "verify[%s] :%s :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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();
|
2019-04-17 04:17:41 +02:00
|
|
|
__builtin_unreachable();
|
2018-01-05 08:14:21 +01:00
|
|
|
|
|
|
|
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();
|
2019-04-17 04:17:41 +02:00
|
|
|
__builtin_unreachable();
|
2018-01-05 08:14:21 +01:00
|
|
|
|
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();
|
2019-04-17 04:17:41 +02:00
|
|
|
__builtin_unreachable();
|
2018-02-11 03:36:23 +01:00
|
|
|
|
|
|
|
case X509_V_ERR_CERT_HAS_EXPIRED:
|
|
|
|
if(opts.allow_expired)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
reject();
|
2019-04-17 04:17:41 +02:00
|
|
|
__builtin_unreachable();
|
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
|
|
|
{
|
2019-04-17 06:11:39 +02:00
|
|
|
if(unlikely(!common_name(opts)))
|
2018-01-08 02:45:41 +01:00
|
|
|
throw inauthentic
|
|
|
|
{
|
|
|
|
"No common name specified in connection options"
|
|
|
|
};
|
|
|
|
|
2018-01-06 02:01:37 +01:00
|
|
|
boost::asio::ssl::rfc2818_verification verifier
|
|
|
|
{
|
2019-04-17 06:11:39 +02:00
|
|
|
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))
|
|
|
|
{
|
2019-05-27 01:53:08 +02:00
|
|
|
thread_local char buf[rfc1035::NAME_BUFSIZE];
|
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
|
|
|
|
2019-04-17 06:11:27 +02:00
|
|
|
#ifdef RB_DEBUG
|
|
|
|
thread_local char buf[16_KiB];
|
|
|
|
const critical_assertion ca;
|
|
|
|
log::debug
|
2018-01-12 03:41:27 +01:00
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "verify[%s] %s",
|
2019-04-17 06:11:27 +02:00
|
|
|
common_name(opts),
|
|
|
|
openssl::print_subject(buf, cert)
|
|
|
|
};
|
|
|
|
#endif
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "Certificate rejected :%s", e.what()
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "Certificate error :%s", e.what()
|
2018-09-30 02:28:11 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) async handler: unhandled exception :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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
|
|
|
|
{
|
2020-03-17 00:51:30 +01:00
|
|
|
log, "socket(%p) async handler: unhandled exception :%s",
|
2018-09-30 02:28:11 +02:00
|
|
|
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()
|
|
|
|
};
|
|
|
|
|
2019-10-09 03:14:10 +02:00
|
|
|
const milliseconds ret
|
2018-02-07 07:22:54 +01:00
|
|
|
{
|
2019-10-09 03:14:10 +02:00
|
|
|
exp.total_milliseconds()
|
2018-02-07 07:22:54 +01:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2019-03-27 01:29:50 +01:00
|
|
|
static ios::descriptor descriptor
|
|
|
|
{
|
|
|
|
"ircd::net::socket timer"
|
|
|
|
};
|
|
|
|
|
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;
|
2019-10-09 03:14:10 +02:00
|
|
|
const boost::posix_time::milliseconds pt
|
|
|
|
{
|
|
|
|
t.count()
|
|
|
|
};
|
|
|
|
|
|
|
|
timer.expires_from_now(pt);
|
2019-03-27 01:29:50 +01:00
|
|
|
timer.async_wait(ios::handle(descriptor, 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();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
thread_local char buf[128];
|
2018-01-12 03:41:27 +01:00
|
|
|
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 ipport &ipp)
|
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
mutable_buffer out{buf};
|
2019-03-25 20:15:07 +01:00
|
|
|
const bool has_port(port(ipp));
|
2019-08-05 01:41:08 +02:00
|
|
|
const bool need_bracket
|
|
|
|
{
|
|
|
|
has_port && is_v6(ipp) && !is_null(ipp)
|
|
|
|
};
|
2019-03-25 20:15:07 +01:00
|
|
|
|
|
|
|
if(need_bracket)
|
2020-04-14 07:46:11 +02:00
|
|
|
consume(out, copy(out, '['));
|
2019-03-25 20:15:07 +01:00
|
|
|
|
2019-04-16 07:46:24 +02:00
|
|
|
if(ipp)
|
2019-06-23 08:28:24 +02:00
|
|
|
consume(out, size(string(out, std::get<ipport::IP>(ipp))));
|
2019-03-25 20:15:07 +01:00
|
|
|
|
|
|
|
if(need_bracket)
|
2020-04-14 07:46:11 +02:00
|
|
|
consume(out, copy(out, ']'));
|
2019-03-25 20:15:07 +01:00
|
|
|
|
|
|
|
if(has_port)
|
|
|
|
{
|
2020-04-14 07:46:11 +02:00
|
|
|
consume(out, copy(out, ':'));
|
2019-03-25 20:15:07 +01:00
|
|
|
consume(out, size(lex_cast(port(ipp), out)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
data(buf), data(out)
|
|
|
|
};
|
2017-12-30 06:42:39 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
make_address(std::get<ipport::IP>(ipport)), port(ipport)
|
2018-01-28 23:18:47 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-30 06:42:39 +01:00
|
|
|
boost::asio::ip::tcp::endpoint
|
|
|
|
ircd::net::make_endpoint(const ipport &ipport)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
make_address(std::get<ipport::IP>(ipport)), port(ipport)
|
2017-12-30 06:42:39 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
//
|
|
|
|
// cmp
|
|
|
|
//
|
|
|
|
|
2018-09-30 01:46:02 +02:00
|
|
|
bool
|
|
|
|
ircd::net::ipport::cmp_ip::operator()(const ipport &a, const ipport &b)
|
|
|
|
const
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return ipaddr::cmp()(std::get<ipport::IP>(a), std::get<ipport::IP>(b));
|
2018-09-30 01:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::ipport::cmp_port::operator()(const ipport &a, const ipport &b)
|
|
|
|
const
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return std::get<ipport::PORT>(a) < std::get<ipport::PORT>(b);
|
2018-09-30 01:46:02 +02:00
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-07 22:48:03 +01:00
|
|
|
//
|
2019-03-25 02:27:49 +01:00
|
|
|
// net/ipaddr.h
|
2018-01-07 22:48:03 +01:00
|
|
|
//
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
boost::asio::ip::address
|
|
|
|
ircd::net::make_address(const ipaddr &ipaddr)
|
2018-01-08 02:44:49 +01:00
|
|
|
{
|
2019-08-05 01:41:08 +02:00
|
|
|
return is_v4(ipaddr)?
|
|
|
|
ip::address(make_address(ipaddr.v4)):
|
|
|
|
ip::address(make_address(ipaddr.v6));
|
2018-01-08 02:44:49 +01:00
|
|
|
}
|
2019-03-25 02:27:49 +01:00
|
|
|
|
|
|
|
boost::asio::ip::address
|
|
|
|
ircd::net::make_address(const string_view &ip)
|
|
|
|
try
|
2018-01-08 02:44:49 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
return
|
|
|
|
ip && ip == "*"?
|
|
|
|
boost::asio::ip::address_v6::any():
|
|
|
|
ip?
|
|
|
|
boost::asio::ip::make_address(ip):
|
|
|
|
boost::asio::ip::address{};
|
2018-01-08 02:44:49 +01:00
|
|
|
}
|
2019-03-25 02:27:49 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
2018-01-08 02:44:49 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
throw_system_error(e);
|
2018-01-08 02:44:49 +01:00
|
|
|
}
|
2019-03-25 02:27:49 +01:00
|
|
|
|
|
|
|
boost::asio::ip::address_v4
|
|
|
|
ircd::net::make_address(const uint32_t &ip)
|
2018-01-08 02:44:49 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
return ip::address_v4{ip};
|
2018-01-08 02:44:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
boost::asio::ip::address_v6
|
|
|
|
ircd::net::make_address(const uint128_t &ip)
|
2018-02-03 20:06:16 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
const auto &pun
|
|
|
|
{
|
|
|
|
reinterpret_cast<const uint8_t (&)[16]>(ip)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto punpun
|
|
|
|
{
|
|
|
|
reinterpret_cast<const std::array<uint8_t, 16> &>(pun)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::reverse(begin(punpun), end(punpun));
|
|
|
|
return ip::address_v6{punpun};
|
2018-02-03 20:06:16 +01:00
|
|
|
}
|
2019-03-25 02:27:49 +01:00
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::net::operator<<(std::ostream &s, const ipaddr &ipa)
|
2018-02-03 20:06:16 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
thread_local char buf[128];
|
|
|
|
const critical_assertion ca;
|
|
|
|
s << net::string(buf, ipa);
|
|
|
|
return s;
|
2018-02-03 20:06:16 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &buf,
|
|
|
|
const ipaddr &ipaddr)
|
2018-02-03 20:06:16 +01:00
|
|
|
{
|
2019-08-05 01:41:08 +02:00
|
|
|
return is_v4(ipaddr)?
|
|
|
|
string_ip4(buf, ipaddr.v4):
|
|
|
|
string_ip6(buf, ipaddr.v6);
|
2018-02-03 20:06:16 +01:00
|
|
|
}
|
2019-03-25 02:27:49 +01:00
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string_ip4(const mutable_buffer &buf,
|
|
|
|
const uint32_t &ip)
|
2018-02-03 20:06:16 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
return string(buf, make_address(ip));
|
2018-02-03 20:06:16 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string_ip6(const mutable_buffer &buf,
|
|
|
|
const uint128_t &ip)
|
2017-12-30 06:42:39 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
return string(buf, make_address(ip));
|
|
|
|
}
|
2017-12-30 06:42:39 +01:00
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
bool
|
|
|
|
ircd::net::is_loop(const ipaddr &ipaddr)
|
|
|
|
{
|
2019-08-05 01:41:08 +02:00
|
|
|
return is_v4(ipaddr)?
|
|
|
|
make_address(ipaddr.v4).is_loopback():
|
|
|
|
make_address(ipaddr.v6).is_loopback();
|
2018-09-30 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:46:50 +01:00
|
|
|
//
|
2019-03-25 02:27:49 +01:00
|
|
|
// ipaddr::ipaddr
|
2019-03-08 20:46:50 +01:00
|
|
|
//
|
|
|
|
|
2019-08-05 01:41:08 +02:00
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
SIZEOF_LONG_LONG >= 8,
|
|
|
|
"8 byte integer literals are required."
|
|
|
|
);
|
|
|
|
|
|
|
|
decltype(ircd::net::ipaddr::v4_max)
|
|
|
|
ircd::net::ipaddr::v4_min
|
|
|
|
{
|
|
|
|
0x0000ffff00000000ULL
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::ipaddr::v4_max)
|
|
|
|
ircd::net::ipaddr::v4_max
|
|
|
|
{
|
|
|
|
v4_min +
|
|
|
|
0x00000000ffffffffULL
|
|
|
|
};
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::net::ipaddr::ipaddr(const string_view &ip)
|
|
|
|
:ipaddr
|
2019-03-24 23:59:46 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
make_address(ip)
|
2019-03-24 23:59:46 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::net::ipaddr::ipaddr(const rfc1035::record::A &rr)
|
|
|
|
:ipaddr
|
|
|
|
{
|
|
|
|
rr.ip4
|
|
|
|
}
|
2019-03-18 20:39:21 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::net::ipaddr::ipaddr(const rfc1035::record::AAAA &rr)
|
|
|
|
:ipaddr
|
2019-03-08 20:46:50 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
rr.ip6
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2019-03-08 20:46:50 +01:00
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::net::ipaddr::ipaddr(const uint32_t &ip)
|
|
|
|
:ipaddr
|
|
|
|
{
|
|
|
|
make_address(ip)
|
|
|
|
}
|
|
|
|
{
|
2019-03-08 20:46:50 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::net::ipaddr::ipaddr(const uint128_t &ip)
|
|
|
|
:ipaddr
|
2019-03-08 20:46:50 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
make_address(ip)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2019-03-08 20:46:50 +01:00
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::net::ipaddr::ipaddr(const asio::ip::address &address)
|
|
|
|
{
|
|
|
|
const auto address_
|
2019-03-08 20:46:50 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
address.is_v6()?
|
|
|
|
address.to_v6():
|
|
|
|
make_address_v6(ip::v4_mapped, address.to_v4())
|
2019-03-08 20:46:50 +01:00
|
|
|
};
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
byte = address_.to_bytes();
|
|
|
|
std::reverse(byte.begin(), byte.end());
|
|
|
|
}
|
2019-03-08 20:46:50 +01:00
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
//
|
|
|
|
// ipaddr::ipaddr
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::ipaddr::cmp::operator()(const ipaddr &a, const ipaddr &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return a.byte < b.byte;
|
2019-03-08 20:46:50 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
|
|
|
|
2020-02-28 19:18:13 +01:00
|
|
|
/// Creates a host:service or host:port pair from the single string literally
|
|
|
|
/// containing the colon deliminated values. If the suffix is a port number
|
|
|
|
/// then the behavior for the port number constructor applies; if a service
|
2020-03-06 19:42:39 +01:00
|
|
|
/// string then the service constructor applies.
|
2020-02-28 19:18:13 +01:00
|
|
|
ircd::net::hostport::hostport(const string_view &amalgam)
|
|
|
|
:host
|
|
|
|
{
|
|
|
|
rfc3986::host(amalgam)
|
|
|
|
}
|
|
|
|
,port
|
|
|
|
{
|
|
|
|
rfc3986::port(amalgam)
|
|
|
|
}
|
|
|
|
{
|
2020-03-05 18:48:17 +01:00
|
|
|
// When the amalgam has no port || a valid integer port
|
|
|
|
if(amalgam == host || port)
|
2020-02-28 19:18:13 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// When the port is actually a service string
|
2020-03-05 18:48:17 +01:00
|
|
|
this->service = rsplit(amalgam, ':').second;
|
2020-02-28 19:18:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::hostport::hostport(const string_view &amalgam,
|
|
|
|
verbatim_t)
|
|
|
|
:host
|
|
|
|
{
|
|
|
|
rfc3986::host(amalgam)
|
|
|
|
}
|
|
|
|
,service
|
|
|
|
{
|
|
|
|
amalgam != host && !rfc3986::port(amalgam)?
|
|
|
|
rsplit(amalgam, ':').second:
|
|
|
|
string_view{}
|
|
|
|
}
|
|
|
|
,port
|
|
|
|
{
|
|
|
|
rfc3986::port(amalgam)
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
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
|
|
|
{
|
2020-03-05 02:32:32 +01:00
|
|
|
thread_local char buf[rfc3986::DOMAIN_BUFSIZE * 2];
|
2018-01-12 03:41:27 +01:00
|
|
|
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
|
2020-03-06 01:34:13 +01:00
|
|
|
ircd::net::canonize(const hostport &hostport)
|
2018-03-14 00:49:41 +01:00
|
|
|
{
|
|
|
|
const size_t len
|
|
|
|
{
|
2020-03-06 01:34:13 +01:00
|
|
|
size(host(hostport)) // host
|
|
|
|
+ 1 + size(service(hostport)) // ':' + service
|
|
|
|
+ 1 + 5 + 1 // ':' + portnum (optimistic)
|
2018-03-14 00:49:41 +01:00
|
|
|
};
|
|
|
|
|
2020-03-06 01:34:13 +01:00
|
|
|
return ircd::string(len, [&hostport]
|
2018-03-14 00:49:41 +01:00
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
2020-03-06 01:34:13 +01:00
|
|
|
return canonize(buf, hostport);
|
2018-03-14 00:49:41 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::canonize(const mutable_buffer &buf,
|
2020-03-06 01:34:13 +01:00
|
|
|
const hostport &hostport)
|
2018-03-14 00:49:41 +01:00
|
|
|
{
|
2020-03-06 01:34:13 +01:00
|
|
|
thread_local char tlbuf[2][rfc3986::DOMAIN_BUFSIZE * 2];
|
2019-08-03 06:27:57 +02:00
|
|
|
|
2020-03-06 19:29:23 +01:00
|
|
|
assert(service(hostport) || port(hostport));
|
|
|
|
if(unlikely(!service(hostport) && !port(hostport)))
|
2020-03-06 01:34:13 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Missing service suffix in hostname:service string.",
|
|
|
|
};
|
|
|
|
|
|
|
|
if(port(hostport))
|
2018-03-14 00:49:41 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2020-03-06 19:29:23 +01:00
|
|
|
buf, "%s:%u",
|
2020-03-06 01:34:13 +01:00
|
|
|
tolower(tlbuf[0], host(hostport)),
|
|
|
|
port(hostport),
|
2018-03-14 00:49:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2020-03-06 01:34:13 +01:00
|
|
|
buf, "%s:%s",
|
|
|
|
tolower(tlbuf[0], host(hostport)),
|
|
|
|
tolower(tlbuf[1], service(hostport)),
|
2018-03-14 00:49:41 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &buf,
|
|
|
|
const hostport &hp)
|
|
|
|
{
|
2020-03-05 02:32:32 +01:00
|
|
|
thread_local char tlbuf[2][rfc3986::DOMAIN_BUFSIZE * 2];
|
2019-08-03 06:27:57 +02:00
|
|
|
|
2019-03-19 01:39:21 +01:00
|
|
|
if(empty(service(hp)) && port(hp) == 0)
|
2018-02-03 20:38:46 +01:00
|
|
|
return fmt::sprintf
|
2018-01-07 22:48:03 +01:00
|
|
|
{
|
2019-08-03 06:27:57 +02:00
|
|
|
buf, "%s",
|
|
|
|
tolower(tlbuf[0], host(hp)),
|
2018-02-03 20:38:46 +01:00
|
|
|
};
|
2018-01-07 22:48:03 +01:00
|
|
|
|
2019-03-19 01:39:21 +01:00
|
|
|
if(empty(service(hp)) && port(hp) != 0)
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2019-08-03 06:27:57 +02:00
|
|
|
buf, "%s:%u",
|
|
|
|
tolower(tlbuf[0], host(hp)),
|
|
|
|
port(hp)
|
2018-02-03 20:38:46 +01:00
|
|
|
};
|
|
|
|
|
2020-03-07 23:20:51 +01:00
|
|
|
if(!empty(service(hp)) && port(hp) == 0)
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s:%s",
|
|
|
|
tolower(tlbuf[0], host(hp)),
|
|
|
|
tolower(tlbuf[1], service(hp)),
|
|
|
|
};
|
|
|
|
|
2018-02-03 20:38:46 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2019-08-03 06:27:57 +02:00
|
|
|
buf, "%s:%u (%s)",
|
|
|
|
tolower(tlbuf[0], host(hp)),
|
|
|
|
port(hp),
|
|
|
|
tolower(tlbuf[1], service(hp)),
|
2018-02-03 20:38:46 +01:00
|
|
|
};
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-30 06:45:15 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// net/asio.h
|
|
|
|
//
|
|
|
|
|
|
|
|
std::string
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::net::string(const ip::tcp::endpoint &ep)
|
2017-12-30 06:45:15 +01:00
|
|
|
{
|
2019-03-25 20:15:07 +01:00
|
|
|
return string(make_ipport(ep));
|
2017-12-30 06:45:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &buf,
|
|
|
|
const ip::tcp::endpoint &ep)
|
2017-12-30 06:45:15 +01:00
|
|
|
{
|
2019-03-25 20:15:07 +01:00
|
|
|
return string(buf, make_ipport(ep));
|
2017-12-30 06:45:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
std::string
|
|
|
|
ircd::net::string(const ip::address &addr)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
addr.is_v4()?
|
|
|
|
string(addr.to_v4()):
|
|
|
|
string(addr.to_v6());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const ip::address_v4 &addr)
|
|
|
|
{
|
|
|
|
return util::string(16, [&addr]
|
|
|
|
(const mutable_buffer &out)
|
|
|
|
{
|
|
|
|
return string(out, addr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::net::string(const ip::address_v6 &addr)
|
|
|
|
{
|
|
|
|
return addr.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &out,
|
|
|
|
const ip::address &addr)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
addr.is_v4()?
|
|
|
|
string(out, addr.to_v4()):
|
|
|
|
string(out, addr.to_v6());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &out,
|
|
|
|
const ip::address_v4 &addr)
|
|
|
|
{
|
|
|
|
const uint32_t a(addr.to_ulong());
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "%u.%u.%u.%u",
|
|
|
|
(a & 0xFF000000U) >> 24,
|
|
|
|
(a & 0x00FF0000U) >> 16,
|
|
|
|
(a & 0x0000FF00U) >> 8,
|
|
|
|
(a & 0x000000FFU) >> 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::string(const mutable_buffer &out,
|
|
|
|
const ip::address_v6 &addr)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
data(out), string(addr).copy(data(out), size(out))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|