From 9b607c899dd2b88c5940134b71dea05a76e04f77 Mon Sep 17 00:00:00 2001 From: halx99 Date: Thu, 4 Nov 2021 23:10:12 +0800 Subject: [PATCH] Fix udp disconnect ipv6 for windows --- thirdparty/yasio/xxsocket.cpp | 11 +++++------ thirdparty/yasio/xxsocket.hpp | 6 +++--- thirdparty/yasio/yasio.cpp | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/thirdparty/yasio/xxsocket.cpp b/thirdparty/yasio/xxsocket.cpp index eed9f4bcb8..b925165d8c 100644 --- a/thirdparty/yasio/xxsocket.cpp +++ b/thirdparty/yasio/xxsocket.cpp @@ -286,8 +286,6 @@ int xxsocket::getipsv(void) void xxsocket::traverse_local_address(std::function handler) { - int family = AF_UNSPEC; - bool done = false; /* Only windows support use getaddrinfo to get local ip address(not loopback or linklocal), Because nullptr same as "localhost": always return loopback address and at unix/linux the gethostname always return "localhost" @@ -596,11 +594,12 @@ int xxsocket::connect_n(socket_native_type s, const endpoint& ep) int xxsocket::disconnect() const { return xxsocket::disconnect(this->fd); } int xxsocket::disconnect(socket_native_type s) { +#if defined(_WIN32) + sockaddr_storage addr_unspec{0}; + return ::connect(s, (sockaddr*)&addr_unspec, sizeof(addr_unspec)); +#else sockaddr addr_unspec{0}; addr_unspec.sa_family = AF_UNSPEC; -#if defined(_WIN32) - return ::connect(s, &addr_unspec, sizeof(addr_unspec)); -#else int ret, error; for (;;) { @@ -615,7 +614,7 @@ int xxsocket::disconnect(socket_native_type s) * The udp socket will be success disconnected by kernel function: `sodisconnect(upic_socket.c)`, then in the kernel, will continue try to * connect with new sockaddr, but will failed with follow errno: * a. EINVAL: addrlen mismatch - * b. ENOSUPPORT: family mismatch + * b. EAFNOSUPPORT: family mismatch * So, we just simply ignore them for the disconnect behavior. */ return (error == EAFNOSUPPORT || error == EINVAL) ? 0 : -1; diff --git a/thirdparty/yasio/xxsocket.hpp b/thirdparty/yasio/xxsocket.hpp index 3cd9338890..ad8c406895 100644 --- a/thirdparty/yasio/xxsocket.hpp +++ b/thirdparty/yasio/xxsocket.hpp @@ -266,7 +266,7 @@ public: if (rbracket && rbracket[1] == ':') { auto zone_value = parse_in6_zone(ip, addr_part, sizeof(addr_part), rbracket); - as_in6(ip, atoi(rbracket + 2), zone_value); + as_in6(ip, static_cast(atoi(rbracket + 2)), zone_value); } } else @@ -277,7 +277,7 @@ public: auto n = colon - str_ep; memcpy(addr_part, str_ep, n); addr_part[n] = '\0'; - as_in4(addr_part, atoi(colon + 1)); + as_in4(addr_part, static_cast(atoi(colon + 1))); } } return *this; @@ -373,7 +373,7 @@ public: void zeroset() { ::memset(this, 0x0, sizeof(*this)); } - void af(int v) { sa_.sa_family = v; } + void af(int v) { sa_.sa_family = static_cast(v); } int af() const { return sa_.sa_family; } void ip(const char* addr) diff --git a/thirdparty/yasio/yasio.cpp b/thirdparty/yasio/yasio.cpp index cef8e250b5..08c3bad67f 100644 --- a/thirdparty/yasio/yasio.cpp +++ b/thirdparty/yasio/yasio.cpp @@ -1477,7 +1477,7 @@ void io_service::do_ssl_handshake(io_channel* ctx) } #endif #if defined(YASIO_HAVE_CARES) -void io_service::ares_getaddrinfo_cb(void* arg, int status, int timeouts, ares_addrinfo* answerlist) +void io_service::ares_getaddrinfo_cb(void* arg, int status, int /*timeouts*/, ares_addrinfo* answerlist) { auto ctx = (io_channel*)arg; auto& current_service = ctx->get_service(); @@ -1565,7 +1565,6 @@ void io_service::config_ares_name_servers() int status = ::ares_get_servers_ports(ares_, &name_servers); if (status == ARES_SUCCESS) { - int count = 0; for (auto ns = name_servers; ns != nullptr; ns = ns->next) if (endpoint{ns->family, &ns->addr, static_cast(ns->udp_port)}.format_to(nscsv, endpoint::fmt_default | endpoint::fmt_no_local)) nscsv.push_back(','); @@ -1792,6 +1791,7 @@ void io_service::notify_connect_succeed(transport_handle_t t) auto ctx = t->ctx_; auto& s = t->socket_; this->transports_.push_back(t); + YASIO__UNUSED_PARAM(s); YASIO_KLOGV("[index: %d] sndbuf=%d, rcvbuf=%d", ctx->index_, s->get_optval(SOL_SOCKET, SO_SNDBUF), s->get_optval(SOL_SOCKET, SO_RCVBUF)); YASIO_KLOGD("[index: %d] the connection #%u(%p) <%s> --> <%s> is established.", ctx->index_, t->id_, t, t->local_endpoint().to_string().c_str(), t->remote_endpoint().to_string().c_str());