2 * @brief Socket handling utilities.
4 /* Copyright (C) 2006,2007,2008,2015,2018,2023 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "socket_utils.h"
27 #include "safesyssocket.h"
31 #include "stringutils.h"
34 # include <arpa/inet.h>
35 # include <netinet/in.h>
38 # include "msvcignoreinvalidparam.h"
41 /// Convert an fd (which might be a socket) to a WIN32 HANDLE.
42 extern HANDLE
fd_to_handle(int fd
) {
43 MSVCIgnoreInvalidParameter invalid_handle_value_is_ok
;
44 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
45 if (handle
!= INVALID_HANDLE_VALUE
) return handle
;
46 // On WIN32, a socket fd isn't the same as a non-socket fd - in fact it's
49 // We need to convert to intptr_t first to suppress a compiler warning here
50 // about casting an integer to a wider pointer type which is a reasonable
51 // warning in general, but we check that the value isn't truncated before
52 // we cast the HANDLE to int (see common/safesyssocket.h).
53 return reinterpret_cast<HANDLE
>(intptr_t(fd
));
56 /// Close an fd, which might be a socket.
57 extern void close_fd_or_socket(int fd
) {
58 MSVCIgnoreInvalidParameter invalid_fd_value_is_ok
;
59 if (close(fd
) == -1 && errno
== EBADF
) {
60 // Bad file descriptor - probably because the fd is actually
69 set_socket_timeouts(int fd
, double timeout
)
73 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
77 RealTime::to_timeval(timeout
, &t
);
79 // Just to be different, it's a DWORD counting in milliseconds.
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
));
97 // SO_SNDTIMEO and SO_RCVTIMEO may be ignored even if they exist, so set
98 // SO_KEEPALIVE anyway if it exists, as it will cause stuck connections to
99 // time out eventually (though it may take ~2 hours).
106 (void)setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
,
107 reinterpret_cast<char*>(&flag
), sizeof(flag
));
113 pretty_ip6(const void* p
, char* buf
)
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
;
138 const char* r
= inet_ntop(af
, src
, buf
, PRETTY_IP6_LEN
);
142 // inet_ntop() isn't always available (at least with mingw) but
143 // WSAAddressToString() supports both IPv4 and IPv6, so just use that.
145 // WSAAddressToString() has a non-const first parameter so we have to cast
147 DWORD in_size
= (af
== AF_INET6
?
148 sizeof(struct sockaddr_in6
) :
149 sizeof(struct sockaddr_in
));
150 DWORD size
= PRETTY_IP6_LEN
;
151 if (WSAAddressToString(const_cast<struct sockaddr
*>(sa
),
152 in_size
, NULL
, buf
, &size
) != 0) {
158 if (startswith(r
, "::ffff:") || startswith(r
, "::FFFF:")) {
159 if (strchr(r
+ 7, '.')) {
165 memmove(buf
, r
, strlen(r
) + 1);