Support IPv6 for remote backend and replication
[xapian.git] / xapian-core / common / socket_utils.cc
blobeff5253641caf97ee8e6f488cde7ab256c1d7e0f
1 /** @file socket_utils.cc
2 * @brief Socket handling utilities.
3 */
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
21 #include <config.h>
22 #include "socket_utils.h"
24 #include <limits>
26 #include "realtime.h"
27 #include "safesyssocket.h"
29 using namespace std;
31 #include <arpa/inet.h>
32 #include "stringutils.h"
34 #ifdef __WIN32__
35 # include <io.h>
36 # include "msvcignoreinvalidparam.h"
37 # include <cerrno>
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
45 // already a HANDLE!
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
54 // a socket.
55 closesocket(fd);
59 #endif
61 void
62 set_socket_timeouts(int fd, double timeout)
64 (void)fd;
65 (void)timeout;
66 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
68 # ifndef __WIN32__
69 struct timeval t;
70 RealTime::to_timeval(timeout, &t);
71 # else
72 // Just to be different, it's a DWORD counting in milliseconds.
73 DWORD t;
74 if (usual(timeout < numeric_limits<DWORD>::max() / 1000))
75 t = timeout * 1000;
76 else
77 t = numeric_limits<DWORD>::max();
78 # endif
79 # ifdef SO_SNDTIMEO
80 (void)setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
81 reinterpret_cast<char*>(&t), sizeof(t));
82 # endif
83 # ifdef SO_RCVTIMEO
84 (void)setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
85 reinterpret_cast<char*>(&t), sizeof(t));
86 # endif
88 #endif
89 #ifdef SO_KEEPALIVE
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).
94 # ifndef __WIN32__
95 int flag = 1;
96 # else
97 DWORD flag = 1;
98 # endif
99 (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
100 reinterpret_cast<char*>(&flag), sizeof(flag));
102 #endif
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;
111 in_port_t port;
112 if (af == AF_INET6) {
113 port = a6->sin6_port;
114 } else if (af == AF_INET) {
115 port = a->sin_port;
116 } else {
117 return -1;
120 #ifndef __WIN32__
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);
126 if (!r)
127 return -1;
128 #else
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) {
134 return -1;
136 const char* r = buf;
137 #endif
139 if (startswith(r, "::ffff:") || startswith(r, "::FFFF:")) {
140 if (strchr(r + 7, '.')) {
141 r += 7;
145 if (r != buf)
146 memmove(buf, r, strlen(r) + 1);
148 return port;