34 # include <arpa/inet.h>
35 # include <netinet/in.h>
42 extern HANDLE fd_to_handle(
int fd) {
44 HANDLE handle = (HANDLE)_get_osfhandle(fd);
45 if (handle != INVALID_HANDLE_VALUE)
return handle;
53 return reinterpret_cast<HANDLE
>(intptr_t(fd));
59 if (
close(fd) == -1 && errno == EBADF) {
73 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
81 if (
usual(timeout < numeric_limits<DWORD>::max() / 1000))
84 t = numeric_limits<DWORD>::max();
87 (void)setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
88 reinterpret_cast<char*
>(&t),
sizeof(t));
91 (void)setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
92 reinterpret_cast<char*
>(&t),
sizeof(t));
106 (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
107 reinterpret_cast<char*
>(&flag),
sizeof(flag));
115 const sockaddr* sa =
reinterpret_cast<const sockaddr*
>(
p);
116 auto af = sa->sa_family;
121 if (af == AF_INET6) {
122 auto sa6 =
reinterpret_cast<const sockaddr_in6*
>(
p);
123 port = sa6->sin6_port;
125 src = &sa6->sin6_addr;
127 }
else if (af == AF_INET) {
128 auto sa4 =
reinterpret_cast<const sockaddr_in*
>(
p);
129 port = sa4->sin_port;
131 src = &sa4->sin_addr;
147 DWORD in_size = (af == AF_INET6 ?
148 sizeof(
struct sockaddr_in6) :
149 sizeof(struct sockaddr_in));
151 if (WSAAddressToString(const_cast<struct sockaddr*>(sa),
152 in_size, NULL, buf, &size) != 0) {
159 if (strchr(r + 7,
'.')) {
165 memmove(buf, r, strlen(r) + 1);
if(!(properties &BACKEND))
Work around MSVC's unhelpful non-standard invalid parameter handling.
void to_timeval(double t, struct timeval *tv)
Fill in struct timeval from number of seconds in a double.
Functions for handling a time or time interval in a double.
include <sys/socket.h> with portability workarounds.
void set_socket_timeouts(int fd, double timeout)
Attempt to set socket-level timeouts.
int pretty_ip6(const void *p, char *buf)
Socket handling utilities.
void close_fd_or_socket(int fd)
constexpr size_t PRETTY_IP6_LEN
Various handy string-related helpers.
bool startswith(std::string_view s, char pfx)