+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * include/compat/win32/sys/socket.h - Win32 emulation of sys/socket.h |
| 7 | */ |
| 8 | |
| 9 | #ifndef OPENAVX_COMPAT_WIN32_SYS_SOCKET_H |
| 10 | #define OPENAVX_COMPAT_WIN32_SYS_SOCKET_H |
| 11 | |
| 12 | #ifdef _WIN32 |
| 13 | #ifndef WIN32_LEAN_AND_MEAN |
| 14 | #define WIN32_LEAN_AND_MEAN |
| 15 | #endif |
| 16 | #include <winsock2.h> |
| 17 | #include <ws2tcpip.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #ifndef AF_UNIX |
| 21 | #define AF_UNIX 1 |
| 22 | #endif |
| 23 | |
| 24 | #ifndef SHUT_RDWR |
| 25 | #define SHUT_RDWR SD_BOTH |
| 26 | #endif |
| 27 | #ifndef SHUT_RD |
| 28 | #define SHUT_RD SD_RECEIVE |
| 29 | #endif |
| 30 | #ifndef SHUT_WR |
| 31 | #define SHUT_WR SD_SEND |
| 32 | #endif |
| 33 | |
| 34 | static inline int socketpair(int domain, int type, int protocol, int sv[2]) { |
| 35 | (void)domain; (void)type; (void)protocol; |
| 36 | SOCKET listener = socket(AF_INET, SOCK_STREAM, 0); |
| 37 | if (listener == INVALID_SOCKET) return -1; |
| 38 | struct sockaddr_in sin; |
| 39 | memset(&sin, 0, sizeof(sin)); |
| 40 | sin.sin_family = AF_INET; |
| 41 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 42 | sin.sin_port = 0; |
| 43 | if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) != 0) { |
| 44 | closesocket(listener); |
| 45 | return -1; |
| 46 | } |
| 47 | int len = sizeof(sin); |
| 48 | if (getsockname(listener, (struct sockaddr*)&sin, &len) != 0 || listen(listener, 1) != 0) { |
| 49 | closesocket(listener); |
| 50 | return -1; |
| 51 | } |
| 52 | SOCKET c = socket(AF_INET, SOCK_STREAM, 0); |
| 53 | if (c == INVALID_SOCKET) { |
| 54 | closesocket(listener); |
| 55 | return -1; |
| 56 | } |
| 57 | if (connect(c, (struct sockaddr*)&sin, sizeof(sin)) != 0) { |
| 58 | closesocket(c); |
| 59 | closesocket(listener); |
| 60 | return -1; |
| 61 | } |
| 62 | SOCKET a = accept(listener, NULL, NULL); |
| 63 | closesocket(listener); |
| 64 | if (a == INVALID_SOCKET) { |
| 65 | closesocket(c); |
| 66 | return -1; |
| 67 | } |
| 68 | sv[0] = (int)c; |
| 69 | sv[1] = (int)a; |
| 70 | return 0; |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | #endif /* OPENAVX_COMPAT_WIN32_SYS_SOCKET_H */ |