Fix testcase unsupportedcheck1 for --disable-backend-remote
[xapian.git] / xapian-core / common / socket_utils.h
blobfdc3b7400093027a17b05572e15be5627f7954ec
1 /** @file
2 * @brief Socket handling utilities.
3 */
4 /* Copyright (C) 2006-2023 Olly Betts
5 * Copyright (C) 2008 Lemur Consulting Ltd
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef XAPIAN_INCLUDED_SOCKET_UTILS_H
23 #define XAPIAN_INCLUDED_SOCKET_UTILS_H
25 #include "safenetdb.h"
26 #include "safeunistd.h"
28 #include <cerrno>
30 #ifdef __WIN32__
32 # include "safewinsock2.h"
34 # include <xapian/error.h>
36 /// Convert an fd (which might be a socket) to a WIN32 HANDLE.
37 extern HANDLE fd_to_handle(int fd);
39 /// Close an fd, which might be a socket.
40 extern void close_fd_or_socket(int fd);
42 /** Class to initialise winsock and keep it initialised while we use it.
44 * We need to get WinSock initialised before we use it, and make it clean up
45 * after we've finished using it. This class performs this initialisation when
46 * constructed and cleans up when destructed. Multiple instances of the class
47 * may be instantiated - windows keeps a count of the number of times that
48 * WSAStartup has been successfully called and only performs the actual cleanup
49 * when WSACleanup has been called the same number of times.
51 * Simply ensure that an instance of this class is initialised whenever we're
52 * doing socket handling. This class can be used as a mixin class (just
53 * inherit from it) or instantiated as a class member or local variable).
55 struct WinsockInitializer {
56 WinsockInitializer() {
57 WSADATA wsadata;
58 int wsaerror = WSAStartup(MAKEWORD(2, 2), &wsadata);
59 // FIXME - should we check the returned information in wsadata to check
60 // that we have a version of winsock which is recent enough for us?
62 if (wsaerror != 0) {
63 throw Xapian::NetworkError("Failed to initialize winsock", wsaerror);
67 ~WinsockInitializer() {
68 WSACleanup();
72 /** Get the errno value of the last error to occur due to a socket operation.
74 * This is specific to the calling thread.
76 * This is needed because some platforms (Windows) separate errors due to
77 * socket operations from other errors. On platforms which don't do this,
78 * the return value will be the value of errno.
80 inline int socket_errno() {
81 int wsa_err = WSAGetLastError();
82 switch (wsa_err) {
83 # ifdef EADDRINUSE
84 case WSAEADDRINUSE: return EADDRINUSE;
85 # endif
86 # ifdef ETIMEDOUT
87 case WSAETIMEDOUT: return ETIMEDOUT;
88 # endif
89 # ifdef EINPROGRESS
90 case WSAEINPROGRESS: return EINPROGRESS;
91 # endif
92 default: return wsa_err;
96 /* Newer compilers define these, in which case we map to those already defined
97 * values in socket_errno() above.
99 # ifndef EADDRINUSE
100 # define EADDRINUSE WSAEADDRINUSE
101 # endif
102 # ifndef ETIMEDOUT
103 # define ETIMEDOUT WSAETIMEDOUT
104 # endif
105 # ifndef EINPROGRESS
106 # define EINPROGRESS WSAEINPROGRESS
107 # endif
109 // We must call closesocket() (instead of just close()) under __WIN32__ or
110 // else the socket remains in the CLOSE_WAIT state.
111 # define CLOSESOCKET(S) closesocket(S)
113 #else
115 // For INET_ADDRSTRLEN and INET6_ADDRSTRLEN.
116 #include <arpa/inet.h>
118 // There's no distinction between sockets and other fds on UNIX.
119 inline void close_fd_or_socket(int fd) { close(fd); }
121 inline int socket_errno() { return errno; }
123 # define CLOSESOCKET(S) close(S)
125 #endif
127 /** Attempt to set socket-level timeouts.
129 * These aren't supported by all platforms, and some platforms allow them to
130 * set but ignore them, so we can't easily report failure.
132 * Also sets SO_KEEPALIVE (if supported), which should ensure a stuck
133 * connection will eventually time out, though it may take up to ~2 hours.
135 void set_socket_timeouts(int fd, double timeout);
137 constexpr size_t PRETTY_IP6_LEN =
138 (INET6_ADDRSTRLEN > INET_ADDRSTRLEN ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN);
140 int pretty_ip6(const void* p, char* buf);
142 #endif // XAPIAN_INCLUDED_SOCKET_UTILS_H