1
0
mirror of synced 2025-04-19 00:24:02 +03:00

Add AF_UNIX support on windows (#2115)

Signed-off-by: Piotr Stankiewicz <piotr.stankiewicz@docker.com>
This commit is contained in:
Piotr 2025-03-25 00:14:24 +01:00 committed by GitHub
parent 65ce51aed7
commit 72b35befb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -187,6 +187,7 @@ using ssize_t = long;
#include <io.h> #include <io.h>
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <afunix.h>
#ifndef WSA_FLAG_NO_HANDLE_INHERIT #ifndef WSA_FLAG_NO_HANDLE_INHERIT
#define WSA_FLAG_NO_HANDLE_INHERIT 0x80 #define WSA_FLAG_NO_HANDLE_INHERIT 0x80
@ -3538,7 +3539,6 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
hints.ai_flags = socket_flags; hints.ai_flags = socket_flags;
} }
#ifndef _WIN32
if (hints.ai_family == AF_UNIX) { if (hints.ai_family == AF_UNIX) {
const auto addrlen = host.length(); const auto addrlen = host.length();
if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; }
@ -3562,11 +3562,19 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
sizeof(addr) - sizeof(addr.sun_path) + addrlen); sizeof(addr) - sizeof(addr.sun_path) + addrlen);
#ifndef SOCK_CLOEXEC #ifndef SOCK_CLOEXEC
#ifndef _WIN32
fcntl(sock, F_SETFD, FD_CLOEXEC); fcntl(sock, F_SETFD, FD_CLOEXEC);
#endif
#endif #endif
if (socket_options) { socket_options(sock); } if (socket_options) { socket_options(sock); }
#ifdef _WIN32
// Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so avoid
// setting default_socket_options.
detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
#endif
bool dummy; bool dummy;
if (!bind_or_connect(sock, hints, dummy)) { if (!bind_or_connect(sock, hints, dummy)) {
close_socket(sock); close_socket(sock);
@ -3575,7 +3583,6 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
} }
return sock; return sock;
} }
#endif
auto service = std::to_string(port); auto service = std::to_string(port);

View File

@ -70,7 +70,6 @@ static void read_file(const std::string &path, std::string &out) {
fs.read(&out[0], static_cast<std::streamsize>(size)); fs.read(&out[0], static_cast<std::streamsize>(size));
} }
#ifndef _WIN32
class UnixSocketTest : public ::testing::Test { class UnixSocketTest : public ::testing::Test {
protected: protected:
void TearDown() override { std::remove(pathname_.c_str()); } void TearDown() override { std::remove(pathname_.c_str()); }
@ -167,6 +166,7 @@ TEST_F(UnixSocketTest, abstract) {
} }
#endif #endif
#ifndef _WIN32
TEST(SocketStream, wait_writable_UNIX) { TEST(SocketStream, wait_writable_UNIX) {
int fds[2]; int fds[2];
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));