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

@ -1,113 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 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.
*/
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/intrin/atomic.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "libc/thread/thread2.h"
int cpu_count;
void SetUpOnce(void) {
cpu_count = __get_cpu_count();
}
////////////////////////////////////////////////////////////////////////////////
// AFFINITY TEST
TEST(sched_getcpu, affinity_test) {
if (IsXnu())
return;
if (IsNetbsd())
return;
if (IsOpenbsd())
return;
SPAWN(fork);
int n = cpu_count;
for (int i = 0; i < n; ++i) {
cpu_set_t affinity;
CPU_ZERO(&affinity);
CPU_SET(i, &affinity);
ASSERT_EQ(
0, pthread_setaffinity_np(pthread_self(), sizeof(affinity), &affinity));
EXPECT_EQ(i, sched_getcpu());
}
EXITS(0);
}
////////////////////////////////////////////////////////////////////////////////
// KLUDGE TEST
#define THREADS 2
#define ITERATIONS 100000
int g_hits[256];
atomic_int g_sync;
int call_sched_getcpu(void) {
int res = sched_getcpu();
ASSERT_NE(-1, res);
ASSERT_GE(res, 0);
ASSERT_LT(res, cpu_count);
return res;
}
void *worker(void *arg) {
int ith = (long)arg;
int nth = THREADS;
for (int i = 0; i < ITERATIONS; ++i) {
// help execution of threads be interleaved
int sync = atomic_fetch_add(&g_sync, 1);
if (sync % nth == ith) {
g_hits[call_sched_getcpu() % ARRAYLEN(g_hits)]++;
}
}
return 0;
}
TEST(sched_getcpu, kludge_test) {
#ifdef __x86_64__
if (IsXnu())
return;
#endif
if (IsNetbsd())
return;
if (IsOpenbsd())
return;
if (cpu_count < THREADS)
return;
pthread_t th[THREADS];
for (int i = 0; i < THREADS; ++i)
ASSERT_EQ(0, pthread_create(th + i, 0, worker, (void *)(long)i));
for (int i = 0; i < THREADS; ++i)
ASSERT_EQ(0, pthread_join(th[i], 0));
int hit = 0;
for (int i = 0; i < ARRAYLEN(g_hits); ++i)
hit += !!g_hits[i];
ASSERT_GE(hit, THREADS);
}

View file

@ -121,12 +121,6 @@ TEST(connect, nonblocking) {
ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP));
ASSERT_SYS(EINPROGRESS, -1,
connect(3, (struct sockaddr *)&addr, sizeof(addr)));
if (!IsLinux() && !IsNetbsd() && !IsXnu()) {
// this doens't work on linux and netbsd
// on MacOS this can EISCONN before accept() is called
ASSERT_SYS(EALREADY, -1,
connect(3, (struct sockaddr *)&addr, sizeof(addr)));
}
ASSERT_SYS(EAGAIN, -1, read(3, buf, 16));
*sem = 1;
{ // wait until connected

View file

@ -0,0 +1,260 @@
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
char buffer[1024];
fd_set wset;
int listenfd, connfd, sockfd;
int s, error;
pid_t pid;
socklen_t len;
struct sockaddr_in serv_addr, cli_addr;
uint16_t port;
printf("\n");
/* Create listening socket */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
perror("socket() failed");
exit(1);
}
/* Initialize server address */
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
serv_addr.sin_port = htons(0);
/* Bind socket */
if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("bind");
exit(2);
}
/* Get the assigned port number */
len = sizeof(serv_addr);
if (getsockname(listenfd, (struct sockaddr *)&serv_addr, &len) < 0) {
perror("getsockname");
exit(3);
}
port = ntohs(serv_addr.sin_port);
/* Listen on the socket */
if (listen(listenfd, 1) < 0) {
perror("listen");
exit(4);
}
/* Fork a child process */
pid = fork();
if (pid < 0) {
perror("fork");
exit(5);
} else if (pid == 0) {
/* Child process: acts as the client */
close(listenfd); /* Close the listening socket in the child */
/* Create socket */
sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (sockfd < 0) {
perror("socket");
exit(6);
}
/* Initialize server address */
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* 127.0.0.1 */
serv_addr.sin_port = htons(port); /* Assigned port */
/* Try calling read() before connection is established */
s = read(sockfd, buffer, sizeof(buffer));
if (s < 0) {
if (errno == ENOTCONN) {
printf("read #1 enotconn\n");
/* good */
} else {
perror("read #1");
exit(6);
}
} else {
printf("read #1 succeeded\n");
exit(6);
}
#if 0
/* Try calling read() before connection is established */
s = write(sockfd, buffer, sizeof(buffer));
if (s < 0) {
if (errno == ENOTCONN) {
/* good */
} else {
perror("write");
}
} else {
printf("Wrote %d bytes: %.*s\n", s, s, buffer);
}
#endif
/* Attempt to connect */
s = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (s == 0) {
printf("connect #1 success\n");
} else if (s < 0 && errno == EINPROGRESS) {
printf("connect #1 einprogress\n");
} else {
perror("connect #1");
exit(10);
}
/* Try calling read() before connection is established */
s = read(sockfd, buffer, sizeof(buffer));
if (s < 0) {
if (errno == EAGAIN) {
printf("read #2 eagain\n");
} else {
perror("read #2");
exit(10);
}
} else {
printf("read #2 succeeded\n");
exit(10);
}
/* Try calling connect() again to trigger EALREADY */
s = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (!s) {
printf("connect #2 succeeded\n");
} else if (s < 0 && errno == EALREADY) {
printf("connect #2 ealready\n");
} else if (s < 0 && errno == EISCONN) {
printf("connect #2 eisconn\n");
} else if (s < 0) {
perror("connect #2");
exit(11);
}
/* Try calling connect() again to trigger EALREADY */
s = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (!s) {
printf("connect #3 succeeded\n");
} else if (errno == EALREADY) {
printf("connect #3 ealready\n");
} else if (errno == EISCONN) {
printf("connect #3 eisconn\n");
} else {
perror("connect");
exit(11);
}
/* Try calling read() before connection is established */
s = read(sockfd, buffer, sizeof(buffer));
if (s < 0) {
if (errno == EAGAIN) {
/* good */
} else {
perror("read");
}
} else {
printf("Read %d bytes: %.*s\n", s, s, buffer);
}
/* Use select() to wait for the socket to be writable */
FD_ZERO(&wset);
FD_SET(sockfd, &wset);
s = select(sockfd + 1, NULL, &wset, NULL, 0);
if (s == 0) {
printf("not possible\n");
exit(11);
} else if (s < 0) {
perror("select");
exit(12);
}
/* Check if socket is writable */
if (FD_ISSET(sockfd, &wset)) {
/* Check for error */
len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
exit(13);
if (error) {
printf("connection failed after select(): %s\n", strerror(error));
exit(14);
}
} else {
exit(16);
}
/* Try calling connect() again to trigger EISCONN */
s = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (!s) {
printf("connect #4 succeeded\n");
} else if (s < 0 && errno == EISCONN) {
printf("connect #4 eisconn\n");
} else if (s < 0) {
exit(17);
}
if (close(sockfd))
exit(15);
exit(0);
} else {
/* Accept connection */
len = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr *)&cli_addr, &len);
if (connfd < 0) {
close(listenfd);
wait(NULL);
exit(18);
}
/* Read data from client */
s = read(connfd, buffer, sizeof(buffer));
if (s < 0) {
exit(51);
} else if (!s) {
/* got close */
} else {
exit(50);
}
/* Close connected socket */
if (close(connfd)) {
close(listenfd);
wait(NULL);
exit(19);
}
/* Close listening socket */
if (close(listenfd)) {
wait(NULL);
exit(20);
}
/* Wait for child process to finish */
int status;
if (waitpid(pid, &status, 0) < 0)
exit(21);
printf("\n");
if (WIFEXITED(status)) {
exit(WEXITSTATUS(status)); /* Return child's exit status */
} else {
exit(22);
}
}
exit(23);
}

View file

@ -0,0 +1,79 @@
#include <arpa/inet.h>
#include <cosmo.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
int listenfd, connfd;
struct sockaddr_in serv_addr;
struct timeval timeout;
socklen_t len;
struct sockaddr_in cli_addr;
// only linux really does this
if (!IsLinux())
return 0;
// create listening socket
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
perror("socket");
exit(1);
}
// initialize server address
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
serv_addr.sin_port = htons(0);
// bind socket
if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("bind");
close(listenfd);
exit(2);
}
// listen on the socket
if (listen(listenfd, 5) < 0) {
perror("listen");
close(listenfd);
exit(3);
}
// accept for 200ms
timeout.tv_sec = 0;
timeout.tv_usec = 200e3;
if (setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof(timeout))) {
perror("setsockopt");
close(listenfd);
exit(4);
}
// Accept connection
len = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr *)&cli_addr, &len);
if (connfd < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* printf("accept() timed out\n"); */
} else {
perror("accept");
}
} else {
printf("Connection accepted from client.\n");
// Close connected socket
close(connfd);
}
// Close listening socket
close(listenfd);
}