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__) || \
406 socklen_t len =
static_cast<socklen_t
>(*addr_len);
408 socklen_t len = *addr_len;
416 *addr_len =
static_cast<size_t>(len);
427 socklen_t len =
static_cast<socklen_t
>(*addr_len);
429 socklen_t len = *addr_len;
437 *addr_len =
static_cast<size_t>(len);
455 std::array<native_handle_type, 2> fds;
457 if (0 != ::
socketpair(family, sock_type, protocol, fds.data())) {
461 return std::make_pair(fds[0], fds[1]);
466 auto listener = listener_res.value();
470 struct sockaddr_storage ss {};
471 size_t ss_len =
sizeof(ss);
474 listener,
reinterpret_cast<sockaddr *
>(&ss), &ss_len);
476 if (ss.ss_family == AF_UNIX) {
477 struct sockaddr_un *su =
reinterpret_cast<sockaddr_un *
>(&ss);
480 DeleteFile(su->sun_path);
496 struct sockaddr_in sa {};
497 size_t sa_len =
sizeof(sa);
499 sa.sin_family = family;
508 struct sockaddr_un sa {};
509 size_t sa_len =
sizeof(sa);
511 sa.sun_family = family;
513 DWORD path_len = GetTempPath(UNIX_PATH_MAX, sa.sun_path);
516 if (path_len >= UNIX_PATH_MAX - 9) path_len = 0;
519 QueryPerformanceCounter(&ticks);
521 snprintf(sa.sun_path + path_len, UNIX_PATH_MAX - path_len,
522 "%" PRIx64
"-%lu.sok", ticks.QuadPart, GetCurrentProcessId());
542 auto first_fd = first_res.value();
546 auto remote_sa_res = [](
auto sock_handle)
548 struct sockaddr_storage ss {};
549 size_t ss_len =
sizeof(ss);
552 sock_handle,
reinterpret_cast<sockaddr *
>(&ss), &ss_len);
556 if (ss.ss_family == AF_INET) {
557 auto *sa =
reinterpret_cast<sockaddr_in *
>(&ss);
566 const auto remote_sa = *remote_sa_res;
569 first_fd,
reinterpret_cast<const sockaddr *
>(&remote_sa),
576 first_fd_guard.commit();
578 auto second_fd = second_res.value();
580 return std::make_pair(first_fd, second_fd);
587 size_t len,
int flags) {
588 ssize_t written = ::splice(fd_in,
nullptr, fd_out,
nullptr, len,
flags);
608 return splice(fd_in, fd_out, len,
flags);
624 return splice(fd_in, fd_out, len,
flags);
651 std::array<impl::poll::poll_fd, 1> fds{{
675 WORD wVersionRequested = MAKEWORD(2, 2);
677 if (
int err = WSAStartup(wVersionRequested, &wsaData)) {
Definition: scope_guard.h:28
Definition: expected.h:944
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:56
Definition: buf0block_hint.cc:30
Definition: authentication.cc:36
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:910
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:403
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:673
stdx::expected< std::pair< native_handle_type, native_handle_type >, error_type > socketpair(int family, int sock_type, int protocol)
socketpair().
Definition: socket.h:453
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:604
stdx::expected< void, error_type > getpeername(native_handle_type native_handle, struct sockaddr *addr, size_t *addr_len)
Definition: socket.h:424
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:663
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:636
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:620
native_handle_type native_handle()
Definition: process.h:56
constexpr auto make_unexpected(E &&e) -> unexpected< std::decay_t< E > >
Definition: expected.h:125
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
#define INADDR_LOOPBACK
Definition: types.h:94