mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 20:29:18 +00:00
LibCore: Implement System::socketpair on Windows
This commit is contained in:
parent
886a8fca86
commit
4ae3522b10
Notes:
github-actions[bot]
2025-02-14 16:40:14 +00:00
Author: https://github.com/stasoid
Commit: 4ae3522b10
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2952
Reviewed-by: https://github.com/ADKaster ✅
6 changed files with 124 additions and 3 deletions
|
@ -12,7 +12,9 @@ set(SOURCES
|
||||||
)
|
)
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
list(APPEND SOURCES SystemWindows.cpp)
|
list(APPEND SOURCES
|
||||||
|
SocketpairWindows.cpp
|
||||||
|
SystemWindows.cpp)
|
||||||
else()
|
else()
|
||||||
list(APPEND SOURCES System.cpp)
|
list(APPEND SOURCES System.cpp)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -16,6 +16,7 @@ typedef char CHAR;
|
||||||
typedef unsigned char UCHAR;
|
typedef unsigned char UCHAR;
|
||||||
typedef const CHAR* PCSTR;
|
typedef const CHAR* PCSTR;
|
||||||
typedef USHORT ADDRESS_FAMILY;
|
typedef USHORT ADDRESS_FAMILY;
|
||||||
|
typedef int socklen_t;
|
||||||
|
|
||||||
#define WINAPI_FAMILY_PARTITION(x) 1
|
#define WINAPI_FAMILY_PARTITION(x) 1
|
||||||
#define FAR
|
#define FAR
|
||||||
|
|
101
Libraries/LibCore/SocketpairWindows.cpp
Normal file
101
Libraries/LibCore/SocketpairWindows.cpp
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
/* https://github.com/ncm/selectable-socketpair
|
||||||
|
Copyright 2007, 2010 by Nathan C. Myers <ncm@cantrip.org>
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
The name of the author must not be used to endorse or promote products
|
||||||
|
derived from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibCore/SocketAddressWindows.h>
|
||||||
|
|
||||||
|
#include <AK/Windows.h>
|
||||||
|
|
||||||
|
namespace Core::System {
|
||||||
|
|
||||||
|
int windows_socketpair(SOCKET socks[2], int make_overlapped)
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
struct sockaddr_in inaddr;
|
||||||
|
struct sockaddr addr;
|
||||||
|
} a;
|
||||||
|
SOCKET listener;
|
||||||
|
int e;
|
||||||
|
socklen_t addrlen = sizeof(a.inaddr);
|
||||||
|
DWORD flags = (make_overlapped ? WSA_FLAG_OVERLAPPED : 0);
|
||||||
|
int reuse = 1;
|
||||||
|
|
||||||
|
if (socks == 0) {
|
||||||
|
WSASetLastError(WSAEINVAL);
|
||||||
|
return SOCKET_ERROR;
|
||||||
|
}
|
||||||
|
socks[0] = socks[1] = -1;
|
||||||
|
|
||||||
|
listener = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if (listener == INVALID_SOCKET)
|
||||||
|
return SOCKET_ERROR;
|
||||||
|
|
||||||
|
memset(&a, 0, sizeof(a));
|
||||||
|
a.inaddr.sin_family = AF_INET;
|
||||||
|
a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||||
|
a.inaddr.sin_port = 0;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
if (::setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, (socklen_t)sizeof(reuse)) == -1)
|
||||||
|
break;
|
||||||
|
if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
|
||||||
|
break;
|
||||||
|
|
||||||
|
memset(&a, 0, sizeof(a));
|
||||||
|
if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR)
|
||||||
|
break;
|
||||||
|
// win32 getsockname may only set the port number, p=0.0005.
|
||||||
|
// ( http://msdn.microsoft.com/library/ms738543.aspx ):
|
||||||
|
a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||||
|
a.inaddr.sin_family = AF_INET;
|
||||||
|
|
||||||
|
if (::listen(listener, 1) == SOCKET_ERROR)
|
||||||
|
break;
|
||||||
|
|
||||||
|
socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
|
||||||
|
if (socks[0] == INVALID_SOCKET)
|
||||||
|
break;
|
||||||
|
if (connect(socks[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
|
||||||
|
break;
|
||||||
|
|
||||||
|
socks[1] = ::accept(listener, NULL, NULL);
|
||||||
|
if (socks[1] == INVALID_SOCKET)
|
||||||
|
break;
|
||||||
|
|
||||||
|
closesocket(listener);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
e = WSAGetLastError();
|
||||||
|
closesocket(listener);
|
||||||
|
closesocket(socks[0]);
|
||||||
|
closesocket(socks[1]);
|
||||||
|
WSASetLastError(e);
|
||||||
|
socks[0] = socks[1] = -1;
|
||||||
|
return SOCKET_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -32,11 +32,11 @@
|
||||||
# include <termios.h>
|
# include <termios.h>
|
||||||
# include <utime.h>
|
# include <utime.h>
|
||||||
#else
|
#else
|
||||||
|
# include "SocketAddressWindows.h"
|
||||||
# define O_CLOEXEC O_NOINHERIT
|
# define O_CLOEXEC O_NOINHERIT
|
||||||
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
||||||
# define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
# define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
||||||
using sighandler_t = void (*)(int);
|
using sighandler_t = void (*)(int);
|
||||||
using socklen_t = int;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID) && !defined(AK_OS_WINDOWS)
|
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID) && !defined(AK_OS_WINDOWS)
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
||||||
* Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
|
* Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
|
||||||
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
||||||
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
|
* Copyright (c) 2024-2025, stasoid <stasoid@yahoo.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
namespace Core::System {
|
namespace Core::System {
|
||||||
|
|
||||||
|
int windows_socketpair(SOCKET socks[2], int make_overlapped);
|
||||||
|
|
||||||
static void invalid_parameter_handler(wchar_t const*, wchar_t const*, wchar_t const*, unsigned int, uintptr_t)
|
static void invalid_parameter_handler(wchar_t const*, wchar_t const*, wchar_t const*, unsigned int, uintptr_t)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -225,6 +227,20 @@ bool is_socket(int handle)
|
||||||
return GetFileType(to_handle(handle)) == FILE_TYPE_PIPE;
|
return GetFileType(to_handle(handle)) == FILE_TYPE_PIPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> socketpair(int domain, int type, int protocol, int sv[2])
|
||||||
|
{
|
||||||
|
if (domain != AF_LOCAL || type != SOCK_STREAM || protocol != 0)
|
||||||
|
return Error::from_string_literal("Unsupported argument value");
|
||||||
|
|
||||||
|
SOCKET socks[2] = {};
|
||||||
|
if (windows_socketpair(socks, true))
|
||||||
|
return Error::from_windows_error();
|
||||||
|
|
||||||
|
sv[0] = socks[0];
|
||||||
|
sv[1] = socks[1];
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<void> sleep_ms(u32 milliseconds)
|
ErrorOr<void> sleep_ms(u32 milliseconds)
|
||||||
{
|
{
|
||||||
Sleep(milliseconds);
|
Sleep(milliseconds);
|
||||||
|
|
|
@ -23,6 +23,7 @@ GOOD_LICENSE_HEADER_PATTERN = re.compile(
|
||||||
LICENSE_HEADER_CHECK_EXCLUDES = {
|
LICENSE_HEADER_CHECK_EXCLUDES = {
|
||||||
'AK/Checked.h',
|
'AK/Checked.h',
|
||||||
'AK/Function.h',
|
'AK/Function.h',
|
||||||
|
'Libraries/LibCore/SocketpairWindows.cpp',
|
||||||
}
|
}
|
||||||
|
|
||||||
# We check that "#pragma once" is present
|
# We check that "#pragma once" is present
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue