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 et 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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2023-08-16 22:53:06 +00:00
|
|
|
#include "libc/intrin/fds.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/intrin/describeflags.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sock/internal.h"
|
|
|
|
#include "libc/sock/sock.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/sock/syscall_fd.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves socket setting.
|
|
|
|
*
|
|
|
|
* @param level can be SOL_SOCKET, IPPROTO_TCP, etc.
|
|
|
|
* @param optname can be SO_{REUSE{PORT,ADDR},KEEPALIVE,etc.} etc.
|
|
|
|
* @return 0 on success, or -1 w/ errno
|
|
|
|
* @error ENOPROTOOPT for unknown (level,optname)
|
2022-07-17 09:40:39 +00:00
|
|
|
* @error EINVAL if `out_optlen` is invalid somehow
|
|
|
|
* @error ENOTSOCK if `fd` is valid but not a socket
|
|
|
|
* @error EBADF if `fd` isn't valid
|
|
|
|
* @error EFAULT if optval memory isn't valid
|
2020-06-15 14:18:57 +00:00
|
|
|
* @see libc/sysv/consts.sh for tuning catalogue
|
|
|
|
* @see setsockopt()
|
|
|
|
*/
|
|
|
|
int getsockopt(int fd, int level, int optname, void *out_opt_optval,
|
|
|
|
uint32_t *out_optlen) {
|
2022-03-23 13:31:55 +00:00
|
|
|
int rc;
|
2022-04-24 16:59:22 +00:00
|
|
|
|
2022-07-17 09:40:39 +00:00
|
|
|
if (level == -1 || !optname) {
|
2023-08-16 22:53:06 +00:00
|
|
|
rc = enoprotoopt(); // see libc/sysv/consts.sh
|
|
|
|
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
|
|
|
|
rc = enotsock();
|
2022-03-23 13:31:55 +00:00
|
|
|
} else if (!IsWindows()) {
|
|
|
|
rc = sys_getsockopt(fd, level, optname, out_opt_optval, out_optlen);
|
2023-08-16 22:53:06 +00:00
|
|
|
} else if (!__isfdopen(fd)) {
|
|
|
|
rc = ebadf();
|
2020-11-28 20:01:51 +00:00
|
|
|
} else if (__isfdkind(fd, kFdSocket)) {
|
2022-03-23 13:31:55 +00:00
|
|
|
rc = sys_getsockopt_nt(&g_fds.p[fd], level, optname, out_opt_optval,
|
|
|
|
out_optlen);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2023-08-16 22:53:06 +00:00
|
|
|
rc = enotsock();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-24 16:59:22 +00:00
|
|
|
|
2023-11-29 11:45:54 +00:00
|
|
|
#if SYSDEBUG
|
2022-04-24 16:59:22 +00:00
|
|
|
if (out_opt_optval && out_optlen && rc != -1) {
|
|
|
|
STRACE("getsockopt(%d, %s, %s, [%#.*hhs], [%d]) → %d% lm", fd,
|
|
|
|
DescribeSockLevel(level), DescribeSockOptname(level, optname),
|
|
|
|
*out_optlen, out_opt_optval, *out_optlen, rc);
|
|
|
|
} else {
|
|
|
|
STRACE("getsockopt(%d, %s, %s, %p, %p) → %d% lm", fd,
|
|
|
|
DescribeSockLevel(level), DescribeSockOptname(level, optname),
|
|
|
|
out_opt_optval, out_optlen, rc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-03-23 13:31:55 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|