Clean up more WIN32 APIs

This commit is contained in:
Justine Tunney 2023-07-30 16:00:58 -07:00
parent 44b88d659d
commit 929478c524
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
60 changed files with 74 additions and 962 deletions

View file

@ -21,6 +21,7 @@
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/struct/sockaddr.h"
@ -30,6 +31,8 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sock.h"
__msabi extern typeof(__sys_closesocket_nt) *const __imp_closesocket;
union AcceptExAddr {
struct sockaddr_storage addr;
char buf[sizeof(struct sockaddr_storage) + 16];
@ -70,7 +73,7 @@ textwindows int sys_accept_nt(struct Fd *fd, struct sockaddr_storage *addr,
if (__wsablock(fd, &overlapped, &completion_flags, kSigOpRestartable,
sockfd->rcvtimeo) == -1) {
WSACloseEvent(overlapped.hEvent);
__sys_closesocket_nt(handle);
__imp_closesocket(handle);
free(sockfd2);
return -1;
}

View file

@ -28,7 +28,7 @@ __msabi extern typeof(__sys_bind_nt) *const __imp_bind;
textwindows int sys_bind_nt(struct Fd *fd, const void *addr,
uint32_t addrsize) {
unassert(fd->kind == kFdSocket);
if (__sys_bind_nt(fd->handle, addr, addrsize) != -1) {
if (__imp_bind(fd->handle, addr, addrsize) != -1) {
return 0;
} else {
return __winsockerr();

View file

@ -22,6 +22,7 @@
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/errors.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/sock.h"
@ -30,10 +31,14 @@
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/errfuns.h"
__msabi extern typeof(__sys_getsockname_nt) *const __imp_getsockname;
__msabi extern typeof(__sys_getpeername_nt) *const __imp_getpeername;
static int __getsockpeername(int fd, struct sockaddr *out_addr,
uint32_t *inout_addrsize, const char *name,
int impl_sysv(int, void *, uint32_t *),
int impl_win32(uint64_t, void *, uint32_t *)) {
int (*__msabi impl_win32)(uint64_t, void *,
uint32_t *)) {
int rc;
struct sockaddr_storage ss = {0};
uint32_t size = sizeof(ss);
@ -41,8 +46,7 @@ static int __getsockpeername(int fd, struct sockaddr *out_addr,
if (IsWindows()) {
if (__isfdkind(fd, kFdSocket)) {
if ((rc = impl_win32(g_fds.p[fd].handle, &ss, &size))) {
if (impl_win32 == __sys_getsockname_nt &&
WSAGetLastError() == WSAEINVAL) {
if (impl_win32 == __imp_getsockname && WSAGetLastError() == WSAEINVAL) {
// The socket has not been bound to an address with bind, or
// ADDR_ANY is specified in bind but connection has not yet
// occurred. -MSDN
@ -78,7 +82,7 @@ static int __getsockpeername(int fd, struct sockaddr *out_addr,
*/
int getsockname(int fd, struct sockaddr *out_addr, uint32_t *inout_addrsize) {
return __getsockpeername(fd, out_addr, inout_addrsize, "getsockname",
__sys_getsockname, __sys_getsockname_nt);
__sys_getsockname, __imp_getsockname);
}
/**
@ -88,5 +92,5 @@ int getsockname(int fd, struct sockaddr *out_addr, uint32_t *inout_addrsize) {
*/
int getpeername(int fd, struct sockaddr *out_addr, uint32_t *inout_addrsize) {
return __getsockpeername(fd, out_addr, inout_addrsize, "getpeername",
__sys_getpeername, __sys_getpeername_nt);
__sys_getpeername, __imp_getpeername);
}

View file

@ -24,6 +24,7 @@
#include "libc/mem/mem.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/iphlpapi.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/yoink.inc"
@ -35,6 +36,8 @@
#include "libc/sysv/consts/sock.h"
#include "libc/sysv/consts/sol.h"
__msabi extern typeof(__sys_setsockopt_nt) *const __imp_setsockopt;
/*
* ioctl(SIOCGIFCONFIG) for Windows need to access the following
* functions through weak reference. This ensure those symbols are not
@ -59,7 +62,7 @@ textwindows int sys_socket_nt(int family, int type, int protocol) {
// pydoc of this file third_party/python/Lib/test/support/__init__.py
// this needs to happen right after socket is called or it won't work
if (family == AF_INET || family == AF_INET6) {
unassert(__sys_setsockopt_nt(h, SOL_SOCKET, -5, &yes, 4) != -1);
unassert(__imp_setsockopt(h, SOL_SOCKET, -5, &yes, 4) != -1);
}
oflags = O_RDWR;