From: "Richard W.M. Jones" Subject: [PATCH v3] ws2_32: Very minimal support for AF_UNIX. Message-Id: <20200908171240.4182912-2-rjones@redhat.com> Date: Tue, 8 Sep 2020 18:12:40 +0100 In-Reply-To: <20200908171240.4182912-1-rjones@redhat.com> References: <20200908171240.4182912-1-rjones@redhat.com> This is the most minimal support for AF_UNIX. Windows 10 has had AF_UNIX support for a while: https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ Signed-off-by: Richard W.M. Jones --- dlls/ws2_32/socket.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ include/Makefile.in | 1 + include/afunix.h | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/dlls/ws2_32/socket.c b/dlls/ws2_32/socket.c index b7a570043c..968414d8eb 100644 --- a/dlls/ws2_32/socket.c +++ b/dlls/ws2_32/socket.c @@ -132,6 +132,8 @@ # include #endif +#include + #define NONAMELESSUNION #define NONAMELESSSTRUCT #include "ntstatus.h" @@ -151,6 +153,7 @@ #include "wshisotp.h" #include "mstcpip.h" #include "af_irda.h" +#include "afunix.h" #include "winnt.h" #define USE_WC_PREFIX /* For CMSG_DATA */ #include "iphlpapi.h" @@ -745,6 +748,7 @@ static const int ws_ipv6_map[][2] = static const int ws_af_map[][2] = { MAP_OPTION( AF_UNSPEC ), + MAP_OPTION( AF_UNIX ), MAP_OPTION( AF_INET ), MAP_OPTION( AF_INET6 ), #ifdef HAS_IPX @@ -1802,6 +1806,7 @@ static inline BOOL supported_pf(int pf) { switch (pf) { + case WS_AF_UNIX: case WS_AF_INET: case WS_AF_INET6: return TRUE; @@ -1900,6 +1905,36 @@ static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsadd memcpy(&uin->sin_addr,&win->sin_addr,4); /* 4 bytes = 32 address bits */ break; } + case WS_AF_UNIX: { + struct sockaddr_un* un = (struct sockaddr_un *)uaddr; + const struct WS_sockaddr_un* wun = (const struct WS_sockaddr_un*)wsaddr; + int num; + LPWSTR win_path; + char *unix_path; + + if (wsaddrlensun_family = AF_UNIX; + /* Note that in Win32 sun_path is "a null-terminated UTF-8 + * file system path". We have to translate to a Unix path. + */ + num = MultiByteToWideChar(CP_UTF8, 0, wun->sun_path, -1, NULL, 0); + win_path = HeapAlloc(GetProcessHeap(), 0, num); + if (win_path == NULL) + return 0; + MultiByteToWideChar(CP_UTF8, 0, wun->sun_path, -1, win_path, num); + unix_path = wine_get_unix_file_name(win_path); + heap_free(win_path); + if (!unix_path || strlen(unix_path) > sizeof(un->sun_path)) { + heap_free(unix_path); + return 0; + } + memcpy(&un->sun_path, unix_path, strlen(unix_path)); + heap_free(unix_path); + break; + } #ifdef HAS_IRDA case WS_AF_IRDA: { struct sockaddr_irda *uin = (struct sockaddr_irda *)uaddr; @@ -1984,6 +2019,12 @@ static BOOL is_sockaddr_bound(const struct sockaddr *uaddr, int uaddrlen) const struct sockaddr_in *in = (const struct sockaddr_in*) uaddr; return in->sin_port || memcmp(&in->sin_addr, &emptyAddr.sin_addr, sizeof(struct in_addr)); } + case AF_UNIX: + { + static const struct sockaddr_un emptyAddr; + const struct sockaddr_un *un = (const struct sockaddr_un*) uaddr; + return memcmp(&un->sun_path, &emptyAddr.sun_path, sizeof(un->sun_path)); + } case AF_UNSPEC: return FALSE; default: @@ -2108,6 +2149,21 @@ static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, struct WS_sockaddr* ws *wsaddrlen = sizeof(struct WS_sockaddr_in); return 0; } + case AF_UNIX: { + const struct sockaddr_un* un = (const struct sockaddr_un*)uaddr; + struct WS_sockaddr_un* wun = (struct WS_sockaddr_un*)wsaddr; + + if (*wsaddrlen < sizeof(struct WS_sockaddr_un)) + return -1; + wun->sun_family = WS_AF_INET; + /* XXX We might want to translate the Unix path to an NT path + * here, or maybe not depending on whether we expect the + * caller would need to open this path or simply display it. + */ + memcpy(&wun->sun_path, &un->sun_path, sizeof(wun->sun_path)); + *wsaddrlen = sizeof(struct WS_sockaddr_un); + return 0; + } case AF_UNSPEC: { memset(wsaddr,0,*wsaddrlen); return 0; diff --git a/include/Makefile.in b/include/Makefile.in index 49b174ed31..3513b88c23 100644 --- a/include/Makefile.in +++ b/include/Makefile.in @@ -11,6 +11,7 @@ SOURCES = \ adshlp.h \ advpub.h \ af_irda.h \ + afunix.h \ amaudio.h \ amsi.idl \ amstream.idl \ diff --git a/include/afunix.h b/include/afunix.h new file mode 100644 index 0000000000..4266e5f84b --- /dev/null +++ b/include/afunix.h @@ -0,0 +1,43 @@ +/* AFUNIX.H + * + * Copyright (C) the Wine project + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef __WINE_AFUNIX_H +#define __WINE_AFUNIX_H + +#define __WINE_UNIX_PATH_MAX 108 + +#ifdef USE_WS_PREFIX + +struct WS_sockaddr_un +{ + ADDRESS_FAMILY sun_family; + char sun_path[__WINE_UNIX_PATH_MAX]; +}; + +#else + +struct sockaddr_un +{ + ADDRESS_FAMILY sun_family; + char sun_path[__WINE_UNIX_PATH_MAX]; +}; + +#endif + +#endif /* __WINE_AFUNIX_H */ -- 2.27.0