Cleanup socket code

This commit is contained in:
Justine Tunney 2021-02-05 23:45:34 -08:00
parent a91ba89d85
commit e06c90fafc
13 changed files with 108 additions and 94 deletions

View file

@ -17,36 +17,50 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/mem/mem.h"
#include "libc/nt/files.h"
#include "libc/nt/struct/pollfd.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/yoink.inc"
#include "libc/sysv/consts/fio.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/sock.h"
#include "libc/sysv/errfuns.h"
textwindows int sys_accept_nt(struct Fd *fd, void *addr, uint32_t *addrsize,
int flags) {
int flags) {
int64_t h;
int client;
uint32_t yes;
int client, oflags;
struct SockFd *sockfd, *sockfd2;
sockfd = (struct SockFd *)fd->extra;
for (;;) {
if (!WSAPoll(&(struct sys_pollfd_nt){fd->handle, POLLIN}, 1, 1000)) continue;
if (!WSAPoll(&(struct sys_pollfd_nt){fd->handle, POLLIN}, 1, 1000))
continue;
if ((client = __reservefd()) == -1) return -1;
if ((h = WSAAccept(fd->handle, addr, (int32_t *)addrsize, 0, 0)) != -1) {
oflags = 0;
if (flags & SOCK_CLOEXEC) oflags |= O_CLOEXEC;
if (flags & SOCK_NONBLOCK) oflags |= O_NONBLOCK;
if (flags & SOCK_NONBLOCK) {
yes = 1;
if (__sys_ioctlsocket_nt(g_fds.p[client].handle, FIONBIO, &yes) == -1) {
if (__sys_ioctlsocket_nt(g_fds.p[client].handle, FIONBIO,
(uint32_t[]){1}) == -1) {
__winsockerr();
__sys_closesocket_nt(g_fds.p[client].handle);
__releasefd(client);
return __winsockerr();
return -1;
}
}
sockfd2 = calloc(1, sizeof(struct SockFd));
sockfd2->family = sockfd->family;
sockfd2->type = sockfd->type;
sockfd2->protocol = sockfd->protocol;
sockfd2->event = WSACreateEvent();
g_fds.p[client].kind = kFdSocket;
g_fds.p[client].flags = flags;
g_fds.p[client].flags = oflags;
g_fds.p[client].handle = h;
g_fds.p[client].extra = (uintptr_t)sockfd2;
return client;
} else {
__releasefd(client);

View file

@ -16,21 +16,22 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.h"
#include "libc/calls/internal.h"
#include "libc/nt/winsock.h"
#include "libc/mem/mem.h"
#include "libc/sock/internal.h"
#include "libc/sock/yoink.inc"
#include "libc/sysv/errfuns.h"
textwindows int sys_closesocket_nt(int fd) {
int rc;
if (!__isfdkind(fd, kFdSocket)) return ebadf();
if (__sys_closesocket_nt(g_fds.p[fd].handle) != -1) {
rc = 0;
/**
* Closes socket on Windows.
*
* This function should only be called by close().
*/
textwindows int sys_closesocket_nt(struct Fd *fd) {
struct SockFd *sockfd;
sockfd = (struct SockFd *)fd->extra;
WSACloseEvent(sockfd->event);
free(sockfd);
if (__sys_closesocket_nt(fd->handle) != -1) {
return 0;
} else {
rc = __winsockerr();
return __winsockerr();
}
__removefd(fd);
return rc;
}

View file

@ -2,6 +2,7 @@
#define COSMOPOLITAN_LIBC_SOCK_INTERNAL_H_
#include "libc/bits/bits.h"
#include "libc/calls/internal.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/nt/winsock.h"
#include "libc/sock/select.h"
#include "libc/sock/sock.h"
@ -45,6 +46,22 @@ struct msghdr_bsd {
uint32_t msg_flags; /* « different type */
};
struct SockFd {
int family;
int type;
int protocol;
int64_t event;
bool32 (*AcceptEx)(int64_t sListenSocket, int64_t sAcceptSocket,
void *out_lpOutputBuffer /*[recvlen+local+remoteaddrlen]*/,
uint32_t dwReceiveDataLength,
uint32_t dwLocalAddressLength,
uint32_t dwRemoteAddressLength,
uint32_t *out_lpdwBytesReceived,
struct NtOverlapped *inout_lpOverlapped) __msabi;
};
hidden extern int64_t __iocp;
errno_t __dos2errno(uint32_t);
int32_t __sys_accept(int32_t, void *, uint32_t *, int) nodiscard hidden;
@ -85,7 +102,7 @@ int sys_listen_nt(struct Fd *, int) hidden;
int sys_connect_nt(struct Fd *, const void *, uint32_t) hidden;
int sys_bind_nt(struct Fd *, const void *, uint32_t);
int sys_accept_nt(struct Fd *, void *, uint32_t *, int) hidden;
int sys_closesocket_nt(int) hidden;
int sys_closesocket_nt(struct Fd *) hidden;
int sys_socket_nt(int, int, int) hidden;
int sys_select_nt(int, fd_set *, fd_set *, fd_set *, struct timeval *) hidden;
int sys_shutdown_nt(struct Fd *, int) hidden;

View file

@ -45,14 +45,14 @@ ssize_t recvfrom(int fd, void *buf, size_t size, uint32_t flags,
ssize_t got;
if (!IsWindows()) {
got = sys_recvfrom(fd, buf, size, flags, opt_out_srcaddr,
opt_inout_srcaddrsize);
opt_inout_srcaddrsize);
if (opt_out_srcaddr && IsBsd() && got != -1) {
sockaddr2linux(opt_out_srcaddr);
}
return got;
} else if (__isfdkind(fd, kFdSocket)) {
return sys_recvfrom_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1, flags,
opt_out_srcaddr, opt_inout_srcaddrsize);
return sys_recvfrom_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1,
flags, opt_out_srcaddr, opt_inout_srcaddrsize);
} else {
return ebadf();
}

View file

@ -59,7 +59,7 @@ ssize_t sendto(int fd, const void *buf, size_t size, uint32_t flags,
}
} else if (__isfdkind(fd, kFdSocket)) {
return sys_sendto_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1, flags,
opt_addr, addrsize);
opt_addr, addrsize);
} else {
return ebadf();
}

View file

@ -40,6 +40,14 @@ struct sockaddr_in { /* Linux+NT ABI */
uint8_t sin_zero[8];
};
struct sockaddr_storage {
union {
uint16_t ss_family;
intptr_t __ss_align;
char __ss_storage[128];
};
};
struct pollfd {
int32_t fd;
int16_t events;

View file

@ -17,32 +17,43 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/mem/mem.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/yoink.inc"
#include "libc/sysv/consts/fio.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sock.h"
#define CLOEXEC 0x00080000
#define NONBLOCK 0x00000800
textwindows int sys_socket_nt(int family, int type, int protocol) {
int fd;
uint32_t yes;
int64_t h;
struct SockFd *sockfd;
int fd, oflags, truetype;
if ((fd = __reservefd()) == -1) return -1;
if ((g_fds.p[fd].handle = WSASocket(family, type & ~(CLOEXEC | NONBLOCK),
protocol, NULL, 0, 0)) != -1) {
if (type & NONBLOCK) {
yes = 1;
if (__sys_ioctlsocket_nt(g_fds.p[fd].handle, FIONBIO, &yes) == -1) {
__sys_closesocket_nt(g_fds.p[fd].handle);
truetype = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
if ((h = WSASocket(family, truetype, protocol, NULL, 0, 0)) != -1) {
oflags = 0;
if (type & SOCK_CLOEXEC) oflags |= O_CLOEXEC;
if (type & SOCK_NONBLOCK) oflags |= O_NONBLOCK;
if (type & SOCK_NONBLOCK) {
if (__sys_ioctlsocket_nt(h, FIONBIO, (uint32_t[1]){1}) == -1) {
__sys_closesocket_nt(h);
__releasefd(fd);
return __winsockerr();
}
}
sockfd = calloc(1, sizeof(struct SockFd));
sockfd->family = family;
sockfd->type = truetype;
sockfd->protocol = protocol;
sockfd->event = WSACreateEvent();
g_fds.p[fd].kind = kFdSocket;
g_fds.p[fd].flags = type & (CLOEXEC | NONBLOCK);
g_fds.p[fd].flags = oflags;
g_fds.p[fd].handle = h;
g_fds.p[fd].extra = (uintptr_t)sockfd;
return fd;
} else {
__releasefd(fd);
return __winsockerr();
}
}