26#ifndef MYSQL_HARNESS_NET_TS_IMPL_SOCKET_H_
27#define MYSQL_HARNESS_NET_TS_IMPL_SOCKET_H_
34#include <system_error>
44#include <netinet/in.h>
46#include <sys/socket.h>
90 unsigned long cmd,
void *data) {
94 reinterpret_cast<unsigned long *
>(data))) {
117 return (*res & O_NONBLOCK) != 0;
124 unsigned long nonblocking{on};
134 if (
flags & O_NONBLOCK)
return {};
137 if (!(
flags & O_NONBLOCK))
return {};
138 flags &= ~O_NONBLOCK;
160 const void *optval, socklen_t optlen) {
163 reinterpret_cast<const char *
>(optval), optlen);
179 reinterpret_cast<char *
>(optval), optlen);
200 void *
buf,
size_t buf_len,
205 auto bytes_transferred =
::recv(
207 std::min(
static_cast<size_t>(std::numeric_limits<int>::max()), buf_len),
210 auto bytes_transferred =
217 return {
static_cast<size_t>(bytes_transferred)};
221 void *data,
size_t data_len) {
231 return {
static_cast<size_t>(bytes_transferred)};
238 DWORD bytes_transferred;
239 DWORD _flags =
flags.to_ulong();
244 &bytes_transferred, &_flags,
254 ssize_t bytes_transferred =
261 return {
static_cast<size_t>(bytes_transferred)};
274 const void *
buf,
size_t buf_len,
279 auto bytes_transferred =
::send(
281 std::min(
static_cast<size_t>(std::numeric_limits<int>::max()), buf_len),
285 auto bytes_transferred =
292 return {
static_cast<size_t>(bytes_transferred)};
306 return {
static_cast<size_t>(bytes_transferred)};
313 DWORD bytes_transferred;
314 DWORD _flags =
flags.to_ulong();
316 &bytes_transferred, _flags,
326 ssize_t bytes_transferred =
333 return {
static_cast<size_t>(bytes_transferred)};
369 socklen_t *addr_len) {
382 socklen_t *addr_len,
int flags = 0) {
383#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
405 socklen_t len =
static_cast<socklen_t
>(*addr_len);
407 socklen_t len = *addr_len;
415 *addr_len =
static_cast<size_t>(len);
426 socklen_t len =
static_cast<socklen_t
>(*addr_len);
428 socklen_t len = *addr_len;
436 *addr_len =
static_cast<size_t>(len);
454 std::array<native_handle_type, 2> fds;
456 if (0 != ::
socketpair(family, sock_type, protocol, fds.data())) {
460 return std::make_pair(fds[0], fds[1]);
465 auto listener = listener_res.value();
469 struct sockaddr_storage ss {};
470 size_t ss_len =
sizeof(ss);
473 listener,
reinterpret_cast<sockaddr *
>(&ss), &ss_len);
475 if (ss.ss_family == AF_UNIX) {
476 struct sockaddr_un *su =
reinterpret_cast<sockaddr_un *
>(&ss);
479 DeleteFile(su->sun_path);
495 struct sockaddr_in sa {};
496 size_t sa_len =
sizeof(sa);
498 sa.sin_family = family;
507 struct sockaddr_un sa {};
508 size_t sa_len =
sizeof(sa);
510 sa.sun_family = family;
512 DWORD path_len = GetTempPath(UNIX_PATH_MAX, sa.sun_path);
515 if (path_len >= UNIX_PATH_MAX - 9) path_len = 0;
518 QueryPerformanceCounter(&ticks);
520 snprintf(sa.sun_path + path_len, UNIX_PATH_MAX - path_len,
521 "%" PRIx64
"-%lu.sok", ticks.QuadPart, GetCurrentProcessId());
541 auto first_fd = first_res.value();
545 auto remote_sa_res = [](
auto sock_handle)
547 struct sockaddr_storage ss {};
548 size_t ss_len =
sizeof(ss);
551 sock_handle,
reinterpret_cast<sockaddr *
>(&ss), &ss_len);
555 if (ss.ss_family == AF_INET) {
556 auto *sa =
reinterpret_cast<sockaddr_in *
>(&ss);
565 const auto remote_sa = *remote_sa_res;
568 first_fd,
reinterpret_cast<const sockaddr *
>(&remote_sa),
575 first_fd_guard.commit();
577 auto second_fd = second_res.value();
579 return std::make_pair(first_fd, second_fd);
586 size_t len,
int flags) {
587 ssize_t written = ::splice(fd_in,
nullptr, fd_out,
nullptr, len,
flags);
606 return splice(fd_in, fd_out, len,
flags);
621 return splice(fd_in, fd_out, len,
flags);
647 std::array<impl::poll::poll_fd, 1> fds{{
671 WORD wVersionRequested = MAKEWORD(2, 2);
673 if (
int err = WSAStartup(wVersionRequested, &wsaData)) {
Definition: scope_guard.h:28
Definition: expected.h:284
error_type
Definition: error.h:36
static int flags[50]
Definition: hp_test1.cc:40
#define closesocket(A)
Definition: my_io.h:178
static MYSQL * sock
Definition: mysqlcheck.cc:57
Definition: buf0block_hint.cc:30
Definition: http_server_component.cc:34
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:927
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
stdx::expected< int, std::error_code > fcntl(file_handle_type fd, const FileControlOption &cmd)
Definition: file.h:125
int file_handle_type
Definition: file.h:53
stdx::expected< size_t, std::error_code > poll(poll_fd *fds, size_t num_fds, std::chrono::milliseconds timeout)
Definition: poll.h:53
stdx::expected< size_t, error_type > sendmsg(native_handle_type native_handle, msghdr_base &msg, message_flags flags)
Definition: socket.h:310
stdx::expected< void, error_type > listen(native_handle_type native_handle, int backlog)
Definition: socket.h:149
stdx::expected< void, error_type > getsockname(native_handle_type native_handle, struct sockaddr *addr, size_t *addr_len)
Definition: socket.h:402
std::error_code make_error_code(int errcode)
make proper std::error_code for socket errno's
Definition: socket_error.h:96
stdx::expected< size_t, error_type > read(native_handle_type native_handle, void *data, size_t data_len)
Definition: socket.h:220
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
stdx::expected< void, std::error_code > init()
Definition: socket.h:669
stdx::expected< std::pair< native_handle_type, native_handle_type >, error_type > socketpair(int family, int sock_type, int protocol)
socketpair().
Definition: socket.h:452
stdx::expected< void, error_type > bind(native_handle_type native_handle, const struct sockaddr *addr, size_t addr_len)
wrap bind() in a portable way.
Definition: socket.h:339
stdx::expected< size_t, error_type > send(native_handle_type native_handle, const void *buf, size_t buf_len, message_flags flags)
wrap send() in a portable way.
Definition: socket.h:273
std::error_code last_error_code()
get last std::error_code for socket-errors.
Definition: socket_error.h:107
stdx::expected< void, error_type > ioctl(native_handle_type native_handle, unsigned long cmd, void *data)
Definition: socket.h:89
stdx::expected< size_t, error_type > recvmsg(native_handle_type native_handle, msghdr_base &msg, message_flags flags)
Definition: socket.h:235
stdx::expected< size_t, error_type > splice_to_pipe(native_handle_type fd_in, impl::file::file_handle_type fd_out, size_t len, int flags)
Definition: socket.h:602
stdx::expected< void, error_type > getpeername(native_handle_type native_handle, struct sockaddr *addr, size_t *addr_len)
Definition: socket.h:423
stdx::expected< native_handle_type, error_type > accept(native_handle_type native_handle, struct sockaddr *addr, socklen_t *addr_len)
wrap accept() in a portable way.
Definition: socket.h:367
stdx::expected< size_t, error_type > recv(native_handle_type native_handle, void *buf, size_t buf_len, message_flags flags)
wrap recv() in a portable way.
Definition: socket.h:199
stdx::expected< void, std::error_code > close(native_handle_type native_handle)
Definition: socket.h:75
stdx::expected< native_handle_type, error_type > accept4(native_handle_type native_handle, struct sockaddr *addr, socklen_t *addr_len, int flags=0)
Definition: socket.h:380
stdx::expected< size_t, error_type > write(native_handle_type native_handle, const void *data, size_t data_len)
Definition: socket.h:295
wait_type
Definition: socket_constants.h:86
stdx::expected< void, error_type > setsockopt(native_handle_type native_handle, int level, int optname, const void *optval, socklen_t optlen)
Definition: socket.h:158
stdx::expected< bool, error_type > native_non_blocking(native_handle_type native_handle)
Definition: socket.h:106
::msghdr msghdr_base
Definition: socket_constants.h:60
stdx::expected< void, error_type > connect(native_handle_type native_handle, const struct sockaddr *addr, size_t addr_len)
wrap connect() in a portable way.
Definition: socket.h:353
std::bitset< 31 > message_flags
Definition: socket_constants.h:64
stdx::expected< void, error_type > shutdown(native_handle_type fd, int how)
Definition: socket.h:659
constexpr const int kSocketError
Definition: socket_constants.h:50
int native_handle_type
Definition: socket_constants.h:51
constexpr const native_handle_type kInvalidSocket
Definition: socket_constants.h:52
stdx::expected< void, error_type > wait(native_handle_type fd, wait_type wt)
Definition: socket.h:632
stdx::expected< void, error_type > getsockopt(native_handle_type native_handle, int level, int optname, void *optval, socklen_t *optlen)
Definition: socket.h:174
stdx::expected< size_t, error_type > splice_from_pipe(impl::file::file_handle_type fd_in, native_handle_type fd_out, size_t len, int flags)
Definition: socket.h:617
native_handle_type native_handle()
Definition: process.h:56
unexpected(E) -> unexpected< E >
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
#define INADDR_LOOPBACK
Definition: types.h:94