2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/ServerSocketFd.h"
|
|
|
|
|
|
|
|
#include "td/utils/port/config.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
#include "td/utils/port/detail/WineventPoll.h"
|
|
|
|
#include "td/utils/SpinLock.h"
|
|
|
|
#include "td/utils/VectorQueue.h"
|
|
|
|
#endif
|
|
|
|
|
2018-09-10 03:08:15 +02:00
|
|
|
#include <atomic>
|
2018-09-07 02:41:21 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace detail {
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
class ServerSocketFdImpl : private IOCP::Callback {
|
|
|
|
public:
|
2018-09-07 02:41:21 +02:00
|
|
|
ServerSocketFdImpl(NativeFd fd, int socket_family) : info_(std::move(fd)), socket_family_(socket_family) {
|
2018-08-13 19:15:09 +02:00
|
|
|
VLOG(fd) << get_native_fd().io_handle() << " create ServerSocketFd";
|
|
|
|
IOCP::get()->subscribe(get_native_fd(), this);
|
|
|
|
notify_iocp_read();
|
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
notify_iocp_close();
|
|
|
|
}
|
|
|
|
PollableFdInfo &get_poll_info() {
|
2018-09-07 02:41:21 +02:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
const PollableFdInfo &get_poll_info() const {
|
2018-09-07 02:41:21 +02:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
const NativeFd &get_native_fd() const {
|
2018-09-07 02:41:21 +02:00
|
|
|
return info_.native_fd();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Result<SocketFd> accept() {
|
|
|
|
auto lock = lock_.lock();
|
|
|
|
if (accepted_.empty()) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
return Status::Error(-1, "Operation would block");
|
|
|
|
}
|
|
|
|
return accepted_.pop();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Status get_pending_error() {
|
|
|
|
Status res;
|
|
|
|
{
|
|
|
|
auto lock = lock_.lock();
|
|
|
|
if (!pending_errors_.empty()) {
|
|
|
|
res = pending_errors_.pop();
|
|
|
|
}
|
|
|
|
if (res.is_ok()) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
private:
|
2018-09-07 02:41:21 +02:00
|
|
|
PollableFdInfo info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
SpinLock lock_;
|
|
|
|
VectorQueue<SocketFd> accepted_;
|
|
|
|
VectorQueue<Status> pending_errors_;
|
|
|
|
static constexpr size_t MAX_ADDR_SIZE = sizeof(sockaddr_in6) + 16;
|
|
|
|
char addr_buf_[MAX_ADDR_SIZE * 2];
|
|
|
|
|
|
|
|
bool close_flag_{false};
|
|
|
|
std::atomic<int> refcnt_{1};
|
|
|
|
bool is_read_active_{false};
|
|
|
|
OVERLAPPED read_overlapped_;
|
|
|
|
|
|
|
|
char close_overlapped_;
|
|
|
|
|
|
|
|
NativeFd accept_socket_;
|
|
|
|
int socket_family_;
|
|
|
|
|
|
|
|
void on_close() {
|
|
|
|
close_flag_ = true;
|
2018-09-07 02:41:21 +02:00
|
|
|
info_.set_native_fd({});
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
void on_read() {
|
|
|
|
VLOG(fd) << get_native_fd().io_handle() << " on_read";
|
|
|
|
if (is_read_active_) {
|
|
|
|
is_read_active_ = false;
|
|
|
|
auto r_socket = SocketFd::from_native_fd(std::move(accept_socket_));
|
|
|
|
VLOG(fd) << get_native_fd().io_handle() << " finish accept";
|
|
|
|
if (r_socket.is_error()) {
|
|
|
|
return on_error(r_socket.move_as_error());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto lock = lock_.lock();
|
|
|
|
accepted_.push(r_socket.move_as_ok());
|
|
|
|
}
|
|
|
|
get_poll_info().add_flags_from_poll(PollFlags::Read());
|
|
|
|
}
|
|
|
|
loop_read();
|
|
|
|
}
|
|
|
|
void loop_read() {
|
|
|
|
CHECK(!is_read_active_);
|
|
|
|
accept_socket_ = NativeFd(socket(socket_family_, SOCK_STREAM, 0));
|
|
|
|
std::memset(&read_overlapped_, 0, sizeof(read_overlapped_));
|
|
|
|
VLOG(fd) << get_native_fd().io_handle() << " start accept";
|
|
|
|
auto status = AcceptEx(get_native_fd().socket(), accept_socket_.socket(), addr_buf_, 0, MAX_ADDR_SIZE,
|
|
|
|
MAX_ADDR_SIZE, nullptr, &read_overlapped_);
|
|
|
|
if (check_status(status, "accent")) {
|
|
|
|
inc_refcnt();
|
|
|
|
is_read_active_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool check_status(DWORD status, Slice message) {
|
|
|
|
if (status == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
auto last_error = WSAGetLastError();
|
2018-08-13 19:15:09 +02:00
|
|
|
if (last_error == ERROR_IO_PENDING) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
on_error(OS_SOCKET_ERROR(message));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool dec_refcnt() {
|
|
|
|
if (--refcnt_ == 0) {
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void inc_refcnt() {
|
|
|
|
CHECK(refcnt_ != 0);
|
|
|
|
refcnt_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) {
|
|
|
|
{
|
|
|
|
auto lock = lock_.lock();
|
|
|
|
pending_errors_.push(std::move(status));
|
|
|
|
}
|
|
|
|
get_poll_info().add_flags_from_poll(PollFlags::Error());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_iocp(Result<size_t> r_size, OVERLAPPED *overlapped) override {
|
|
|
|
// called from other thread
|
|
|
|
if (dec_refcnt() || close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (r_size.is_error()) {
|
|
|
|
return on_error(r_size.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlapped == nullptr) {
|
|
|
|
return on_read();
|
|
|
|
}
|
|
|
|
if (overlapped == &read_overlapped_) {
|
|
|
|
return on_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlapped == reinterpret_cast<OVERLAPPED *>(&close_overlapped_)) {
|
|
|
|
return on_close();
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
void notify_iocp_read() {
|
|
|
|
VLOG(fd) << get_native_fd().io_handle() << " notify_read";
|
|
|
|
inc_refcnt();
|
|
|
|
IOCP::get()->post(0, this, nullptr);
|
|
|
|
}
|
|
|
|
void notify_iocp_close() {
|
|
|
|
VLOG(fd) << get_native_fd().io_handle() << " notify_close";
|
|
|
|
IOCP::get()->post(0, this, reinterpret_cast<OVERLAPPED *>(&close_overlapped_));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void ServerSocketFdImplDeleter::operator()(ServerSocketFdImpl *impl) {
|
|
|
|
impl->close();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
#elif TD_PORT_POSIX
|
|
|
|
class ServerSocketFdImpl {
|
|
|
|
public:
|
2018-09-07 02:41:21 +02:00
|
|
|
explicit ServerSocketFdImpl(NativeFd fd) : info_(std::move(fd)) {
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
PollableFdInfo &get_poll_info() {
|
2018-09-07 02:41:21 +02:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
const PollableFdInfo &get_poll_info() const {
|
2018-09-07 02:41:21 +02:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
const NativeFd &get_native_fd() const {
|
2018-09-07 02:41:21 +02:00
|
|
|
return info_.native_fd();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
Result<SocketFd> accept() {
|
|
|
|
sockaddr_storage addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
int native_fd = get_native_fd().fd();
|
2018-09-10 01:16:42 +02:00
|
|
|
int r_fd = detail::skip_eintr([&] { return ::accept(native_fd, reinterpret_cast<sockaddr *>(&addr), &addr_len); });
|
2018-08-13 19:15:09 +02:00
|
|
|
auto accept_errno = errno;
|
|
|
|
if (r_fd >= 0) {
|
|
|
|
return SocketFd::from_native_fd(NativeFd(r_fd));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
if (accept_errno == EAGAIN
|
2018-12-31 20:04:05 +01:00
|
|
|
#if EAGAIN != EWOULDBLOCK
|
2018-08-13 19:15:09 +02:00
|
|
|
|| accept_errno == EWOULDBLOCK
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
return Status::Error(-1, "Operation would block");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto error = Status::PosixError(accept_errno, PSLICE() << "Accept from [fd = " << native_fd << "] has failed");
|
|
|
|
switch (accept_errno) {
|
|
|
|
case EBADF:
|
|
|
|
case EFAULT:
|
|
|
|
case EINVAL:
|
|
|
|
case ENOTSOCK:
|
|
|
|
case EOPNOTSUPP:
|
|
|
|
LOG(FATAL) << error;
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << error;
|
|
|
|
// fallthrough
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
|
|
|
case ECONNABORTED: //???
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
get_poll_info().add_flags(PollFlags::Close());
|
|
|
|
return std::move(error);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
Status get_pending_error() {
|
|
|
|
if (get_poll_info().get_flags().has_pending_error()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
TRY_STATUS(detail::get_socket_pending_error(get_native_fd()));
|
|
|
|
get_poll_info().clear_flags(PollFlags::Error());
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-07 02:41:21 +02:00
|
|
|
PollableFdInfo info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
};
|
|
|
|
void ServerSocketFdImplDeleter::operator()(ServerSocketFdImpl *impl) {
|
|
|
|
delete impl;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
ServerSocketFd::ServerSocketFd() = default;
|
|
|
|
ServerSocketFd::ServerSocketFd(ServerSocketFd &&) = default;
|
|
|
|
ServerSocketFd &ServerSocketFd::operator=(ServerSocketFd &&) = default;
|
|
|
|
ServerSocketFd::~ServerSocketFd() = default;
|
|
|
|
ServerSocketFd::ServerSocketFd(std::unique_ptr<detail::ServerSocketFdImpl> impl) : impl_(impl.release()) {
|
|
|
|
}
|
|
|
|
PollableFdInfo &ServerSocketFd::get_poll_info() {
|
|
|
|
return impl_->get_poll_info();
|
|
|
|
}
|
|
|
|
|
|
|
|
const PollableFdInfo &ServerSocketFd::get_poll_info() const {
|
|
|
|
return impl_->get_poll_info();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status ServerSocketFd::get_pending_error() {
|
|
|
|
return impl_->get_pending_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
const NativeFd &ServerSocketFd::get_native_fd() const {
|
|
|
|
return impl_->get_native_fd();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<SocketFd> ServerSocketFd::accept() {
|
|
|
|
return impl_->accept();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerSocketFd::close() {
|
2018-08-13 19:15:09 +02:00
|
|
|
impl_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ServerSocketFd::empty() const {
|
2018-08-13 19:15:09 +02:00
|
|
|
return !impl_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Result<ServerSocketFd> ServerSocketFd::open(int32 port, CSlice addr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
IPAddress address;
|
|
|
|
TRY_STATUS(address.init_ipv4_port(addr, port));
|
2018-08-13 19:15:09 +02:00
|
|
|
NativeFd fd{socket(address.get_address_family(), SOCK_STREAM, 0)};
|
|
|
|
if (!fd) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return OS_SOCKET_ERROR("Failed to create a socket");
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:08:12 +02:00
|
|
|
TRY_STATUS(fd.set_is_blocking(false));
|
2018-08-13 19:15:09 +02:00
|
|
|
auto sock = fd.socket();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
linger ling = {0, 0};
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
int flags = 1;
|
|
|
|
#ifdef SO_REUSEPORT
|
2018-08-13 19:15:09 +02:00
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
BOOL flags = TRUE;
|
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_LINGER, reinterpret_cast<const char *>(&ling), sizeof(ling));
|
|
|
|
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
int e_bind = bind(sock, address.get_sockaddr(), static_cast<socklen_t>(address.get_sockaddr_len()));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (e_bind != 0) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to bind a socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: magic constant
|
2018-08-13 19:15:09 +02:00
|
|
|
int e_listen = listen(sock, 8192);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (e_listen != 0) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to listen on a socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
2018-08-13 19:15:09 +02:00
|
|
|
auto impl = std::make_unique<detail::ServerSocketFdImpl>(std::move(fd));
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-08-13 19:15:09 +02:00
|
|
|
auto impl = std::make_unique<detail::ServerSocketFdImpl>(std::move(fd), address.get_address_family());
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
return ServerSocketFd(std::move(impl));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|