1 /** @file socket_utils.cc
2 * @brief Socket handling utilities.
4 /* Copyright (C) 2006,2007,2008,2015 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 <arpa/inet.h>
32 #include "stringutils.h"
36 # include "msvcignoreinvalidparam.h"
39 /// Convert an fd (which might be a socket) to a WIN32 HANDLE.
40 extern HANDLE
fd_to_handle(int fd
) {
41 MSVCIgnoreInvalidParameter invalid_handle_value_is_ok
;
42 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
43 if (handle
!= INVALID_HANDLE_VALUE
) return handle
;
44 // On WIN32, a socket fd isn't the same as a non-socket fd - in fact it's
46 return reinterpret_cast<HANDLE
>(fd
);
49 /// Close an fd, which might be a socket.
50 extern void close_fd_or_socket(int fd
) {
51 MSVCIgnoreInvalidParameter invalid_fd_value_is_ok
;
52 if (close(fd
) == -1 && errno
== EBADF
) {
53 // Bad file descriptor - probably because the fd is actually
62 set_socket_timeouts(int fd
, double timeout
)
66 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
70 RealTime::to_timeval(timeout
, &t
);
72 // Just to be different, it's a DWORD counting in milliseconds.
74 if (usual(timeout
< numeric_limits
<DWORD
>::max() / 1000))
77 t
= numeric_limits
<DWORD
>::max();
80 (void)setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
,
81 reinterpret_cast<char*>(&t
), sizeof(t
));
84 (void)setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
,
85 reinterpret_cast<char*>(&t
), sizeof(t
));
90 // SO_SNDTIMEO and SO_RCVTIMEO may be ignored even if they exist, so set
91 // SO_KEEPALIVE anyway if it exists, as it will cause stuck connections to
92 // time out eventually (though it may take ~2 hours).
99 (void)setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
,
100 reinterpret_cast<char*>(&flag
), sizeof(flag
));
106 pretty_ip6(const void* p
, char* buf
)
108 const sockaddr_in
* a
= reinterpret_cast<const sockaddr_in
*>(p
);
109 const sockaddr_in6
* a6
= reinterpret_cast<const sockaddr_in6
*>(p
);
110 sa_family_t af
= a6
->sin6_family
;
112 if (af
== AF_INET6
) {
113 port
= a6
->sin6_port
;
114 } else if (af
== AF_INET
) {
121 // Under __WIN32__, inet_ntop()'s second parameter isn't const for some
122 // reason. We don't currently use inet_ntop() there, but allow for a
123 // non-const second parameter in case it's more widespread.
124 void* src
= const_cast<void*>(p
);
125 const char* r
= inet_ntop(af
, src
, buf
, PRETTY_IP6_LEN
);
129 // inet_ntop() isn't always available, at least with mingw.
130 // WSAAddressToString() supports both IPv4 and IPv6, so just use that.
131 DWORD size
= PRETTY_IP6_LEN
;
132 if (WSAAddressToString(reinterpret_cast<sockaddr
*>(&remote_address
),
133 sizeof(remote_address
), NULL
, buf
, &size
) != 0) {
139 if (startswith(r
, "::ffff:") || startswith(r
, "::FFFF:")) {
140 if (strchr(r
+ 7, '.')) {
146 memmove(buf
, r
, strlen(r
) + 1);