2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/assert.h"
|
2022-04-24 16:59:22 +00:00
|
|
|
#include "libc/calls/struct/timeval.h"
|
2022-04-25 15:30:14 +00:00
|
|
|
#include "libc/nt/struct/linger.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/winsock.h"
|
|
|
|
#include "libc/sock/internal.h"
|
2022-04-25 15:30:14 +00:00
|
|
|
#include "libc/sock/sock.h"
|
2022-08-06 10:51:50 +00:00
|
|
|
#include "libc/sock/struct/linger.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/sock/syscall_fd.internal.h"
|
2022-04-24 16:59:22 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/so.h"
|
|
|
|
#include "libc/sysv/consts/sol.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-02-04 03:35:29 +00:00
|
|
|
textwindows int sys_getsockopt_nt(struct Fd *fd, int level, int optname,
|
2022-04-24 16:59:22 +00:00
|
|
|
void *out_opt_optval,
|
|
|
|
uint32_t *inout_optlen) {
|
|
|
|
uint64_t ms;
|
|
|
|
uint32_t in_optlen;
|
2022-06-26 01:17:31 +00:00
|
|
|
struct SockFd *sockfd;
|
2022-04-25 15:30:14 +00:00
|
|
|
struct linger_nt linger;
|
2023-07-26 20:54:49 +00:00
|
|
|
npassert(fd->kind == kFdSocket);
|
2022-06-26 01:17:31 +00:00
|
|
|
sockfd = (struct SockFd *)fd->extra;
|
2022-04-24 16:59:22 +00:00
|
|
|
|
|
|
|
if (out_opt_optval && inout_optlen) {
|
|
|
|
in_optlen = *inout_optlen;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2022-04-24 16:59:22 +00:00
|
|
|
in_optlen = 0;
|
|
|
|
}
|
|
|
|
|
2022-06-26 01:17:31 +00:00
|
|
|
if (level == SOL_SOCKET &&
|
|
|
|
(optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
|
|
|
|
if (in_optlen >= sizeof(struct timeval)) {
|
|
|
|
if (optname == SO_RCVTIMEO) {
|
|
|
|
ms = sockfd->rcvtimeo;
|
|
|
|
} else {
|
|
|
|
ms = sockfd->sndtimeo;
|
|
|
|
}
|
|
|
|
((struct timeval *)out_opt_optval)->tv_sec = ms / 1000;
|
|
|
|
((struct timeval *)out_opt_optval)->tv_usec = ms % 1000 * 1000;
|
|
|
|
*inout_optlen = sizeof(struct timeval);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return einval();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:59:22 +00:00
|
|
|
// TODO(jart): Use WSAIoctl?
|
|
|
|
if (__sys_getsockopt_nt(fd->handle, level, optname, out_opt_optval,
|
|
|
|
inout_optlen) == -1) {
|
2020-11-28 20:01:51 +00:00
|
|
|
return __winsockerr();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-24 16:59:22 +00:00
|
|
|
|
|
|
|
if (level == SOL_SOCKET) {
|
2022-06-26 01:17:31 +00:00
|
|
|
if (optname == SO_LINGER && in_optlen == sizeof(struct linger)) {
|
2022-04-25 15:30:14 +00:00
|
|
|
linger = *(struct linger_nt *)out_opt_optval;
|
|
|
|
((struct linger *)out_opt_optval)->l_onoff = !!linger.l_onoff;
|
|
|
|
((struct linger *)out_opt_optval)->l_linger = linger.l_linger;
|
|
|
|
*inout_optlen = sizeof(struct linger);
|
2022-04-24 16:59:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_optlen == 4 && *inout_optlen == 1) {
|
|
|
|
// handle cases like this
|
|
|
|
// getsockopt(8, SOL_TCP, TCP_FASTOPEN, [u"☺"], [1]) → 0
|
|
|
|
int32_t wut = *(signed char *)out_opt_optval;
|
|
|
|
memcpy(out_opt_optval, &wut, 4);
|
|
|
|
*inout_optlen = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|