2022-04-08 03:30:04 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2022-04-08 03:30:04 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
#include "libc/assert.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-09-15 04:29:50 +00:00
|
|
|
#include "libc/intrin/describentoverlapped.internal.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2023-11-11 06:55:33 +00:00
|
|
|
#include "libc/intrin/likely.h"
|
2022-09-15 04:29:50 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-04-08 03:30:04 +00:00
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/nt/winsock.h"
|
2023-11-11 06:55:33 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
#include "libc/sock/internal.h"
|
2022-04-08 03:30:04 +00:00
|
|
|
|
2022-06-26 01:17:31 +00:00
|
|
|
__msabi extern typeof(WSARecv) *const __imp_WSARecv;
|
2022-04-08 03:30:04 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-26 01:17:31 +00:00
|
|
|
* Receives data from Windows socket.
|
|
|
|
* @return 0 on success, or -1 on failure
|
2022-04-08 03:30:04 +00:00
|
|
|
*/
|
2022-06-26 01:17:31 +00:00
|
|
|
textwindows int WSARecv(
|
|
|
|
uint64_t s, const struct NtIovec *inout_lpBuffers, uint32_t dwBufferCount,
|
|
|
|
uint32_t *opt_out_lpNumberOfBytesRecvd, uint32_t *inout_lpFlags,
|
|
|
|
struct NtOverlapped *opt_inout_lpOverlapped,
|
|
|
|
const NtWsaOverlappedCompletionRoutine opt_lpCompletionRoutine) {
|
|
|
|
int rc;
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
if (opt_inout_lpOverlapped) {
|
|
|
|
// Use NULL for this parameter if the lpOverlapped parameter is not
|
|
|
|
// NULL to avoid potentially erroneous results. This parameter can
|
|
|
|
// be NULL only if the lpOverlapped parameter is not NULL.
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(!opt_out_lpNumberOfBytesRecvd);
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
}
|
2023-12-09 04:03:03 +00:00
|
|
|
#if SYSDEBUG && _NTTRACE
|
2022-06-26 01:17:31 +00:00
|
|
|
uint32_t NumberOfBytesRecvd;
|
|
|
|
if (opt_out_lpNumberOfBytesRecvd) {
|
|
|
|
NumberOfBytesRecvd = *opt_out_lpNumberOfBytesRecvd;
|
|
|
|
}
|
|
|
|
rc = __imp_WSARecv(s, inout_lpBuffers, dwBufferCount, &NumberOfBytesRecvd,
|
|
|
|
inout_lpFlags, opt_inout_lpOverlapped,
|
|
|
|
opt_lpCompletionRoutine);
|
|
|
|
if (opt_out_lpNumberOfBytesRecvd) {
|
|
|
|
*opt_out_lpNumberOfBytesRecvd = NumberOfBytesRecvd;
|
|
|
|
}
|
2022-11-02 05:36:03 +00:00
|
|
|
if (UNLIKELY(__strace > 0) && strace_enabled(0) > 0) {
|
2022-06-26 01:17:31 +00:00
|
|
|
kprintf(STRACE_PROLOGUE "WSARecv(%lu, [", s);
|
|
|
|
DescribeIovNt(inout_lpBuffers, dwBufferCount,
|
|
|
|
rc != -1 ? NumberOfBytesRecvd : 0);
|
2022-09-15 04:29:50 +00:00
|
|
|
kprintf("], %u, [%'u], %p, %s, %p) → %d% lm\n", dwBufferCount,
|
|
|
|
NumberOfBytesRecvd, inout_lpFlags,
|
|
|
|
DescribeNtOverlapped(opt_inout_lpOverlapped),
|
2022-06-26 01:17:31 +00:00
|
|
|
opt_lpCompletionRoutine, rc);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
rc = __imp_WSARecv(s, inout_lpBuffers, dwBufferCount,
|
|
|
|
opt_out_lpNumberOfBytesRecvd, inout_lpFlags,
|
|
|
|
opt_inout_lpOverlapped, opt_lpCompletionRoutine);
|
|
|
|
#endif
|
|
|
|
return rc;
|
2022-04-08 03:30:04 +00:00
|
|
|
}
|