Rewrite Windows connect()

Our old code wasn't working with projects like Qt that call connect() in
O_NONBLOCK mode multiple times. This change overhauls connect() to use a
simpler WSAConnect() API and follows the same pattern as cosmo accept().
This change also reduces the binary footprint of read(), which no longer
needs to depend on our enormous clock_gettime() function.
This commit is contained in:
Justine Tunney 2024-09-12 23:01:20 -07:00
parent 5469202ea8
commit e142124730
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
25 changed files with 556 additions and 277 deletions

View file

@ -18,15 +18,12 @@
*/
#include "libc/calls/internal.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/nt/errors.h"
#include "libc/nt/struct/pollfd.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/struct/sockaddr.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/consts/fio.h"
#include "libc/sysv/consts/o.h"
@ -38,8 +35,6 @@
#define POLL_INTERVAL_MS 10
__msabi extern typeof(__sys_setsockopt_nt) *const __imp_setsockopt;
__msabi extern typeof(__sys_closesocket_nt) *const __imp_closesocket;
__msabi extern typeof(__sys_ioctlsocket_nt) *const __imp_ioctlsocket;
textwindows static int sys_accept_nt_impl(struct Fd *f,
@ -61,7 +56,6 @@ textwindows static int sys_accept_nt_impl(struct Fd *f,
for (;;) {
// perform non-blocking accept
// we assume listen() put f->handle in non-blocking mode
int32_t addrsize = sizeof(*addr);
struct sockaddr *paddr = (struct sockaddr *)addr;
if ((handle = WSAAccept(f->handle, paddr, &addrsize, 0, 0)) != -1)
@ -76,7 +70,7 @@ textwindows static int sys_accept_nt_impl(struct Fd *f,
return -1;
}
// we're done if user wants non-blocking
// check for non-blocking
if (f->flags & O_NONBLOCK)
return eagain();
@ -91,13 +85,6 @@ textwindows static int sys_accept_nt_impl(struct Fd *f,
return __winsockerr();
}
// inherit properties of listening socket
// errors ignored as if f->handle was created before forking
// this fails with WSAENOTSOCK, see
// https://github.com/jart/cosmopolitan/issues/1174
__imp_setsockopt(handle, SOL_SOCKET, kNtSoUpdateAcceptContext, &f->handle,
sizeof(f->handle));
// create file descriptor for new socket
// don't inherit the file open mode bits
int oflags = 0;