Support: quest -f cjk_ngram
[xapian.git] / xapian-core / common / socket_utils.cc
blobbaeb504cfad93be0cbb052e44f54d62a91f705f5
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 #ifdef __WIN32__
32 # include "safeerrno.h"
34 # include <io.h>
35 // __STDC_SECURE_LIB__ doesn't appear to be publicly documented, but appears
36 // to be a good idea. We cribbed this test from the python sources - see, for
37 // example, http://svn.python.org/view?rev=47223&view=rev
38 # if defined _MSC_VER && _MSC_VER >= 1400 && defined __STDC_SECURE_LIB__
39 # include <cstdlib> // For _set_invalid_parameter_handler(), etc.
40 # include <crtdbg.h> // For _CrtSetReportMode, etc.
42 /** A dummy invalid parameter handler which ignores the error. */
43 static void dummy_handler(const wchar_t*,
44 const wchar_t*,
45 const wchar_t*,
46 unsigned int,
47 uintptr_t)
51 // Recent versions of MSVC call an "_invalid_parameter_handler" if a
52 // CRT function receives an invalid parameter. However, there are cases
53 // where this is totally reasonable. To avoid the application dying,
54 // you just need to instantiate the MSVCIgnoreInvalidParameter class in
55 // the scope where you want MSVC to ignore invalid parameters.
56 class MSVCIgnoreInvalidParameter {
57 _invalid_parameter_handler old_handler;
58 int old_report_mode;
60 public:
61 MSVCIgnoreInvalidParameter() {
62 // Install a dummy handler to avoid the program dying.
63 old_handler = _set_invalid_parameter_handler(dummy_handler);
64 // Make sure that no dialog boxes appear.
65 old_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0);
68 ~MSVCIgnoreInvalidParameter() {
69 // Restore the previous settings.
70 _set_invalid_parameter_handler(old_handler);
71 _CrtSetReportMode(_CRT_ASSERT, old_report_mode);
74 # else
75 // Mingw seems to be free of this insanity, so for this and older MSVC versions
76 // define a dummy class to allow MSVCIgnoreInvalidParameter to be used
77 // unconditionally.
78 struct MSVCIgnoreInvalidParameter {
79 // Provide an explicit constructor so this isn't a POD struct - this seems
80 // to prevent GCC warning about an unused variable whenever we instantiate
81 // this class.
82 MSVCIgnoreInvalidParameter() { }
84 # endif
86 /// Convert an fd (which might be a socket) to a WIN32 HANDLE.
87 extern HANDLE fd_to_handle(int fd) {
88 MSVCIgnoreInvalidParameter invalid_handle_value_is_ok;
89 HANDLE handle = (HANDLE)_get_osfhandle(fd);
90 if (handle != INVALID_HANDLE_VALUE) return handle;
91 // On WIN32, a socket fd isn't the same as a non-socket fd - in fact it's
92 // already a HANDLE!
93 return reinterpret_cast<HANDLE>(fd);
96 /// Close an fd, which might be a socket.
97 extern void close_fd_or_socket(int fd) {
98 MSVCIgnoreInvalidParameter invalid_fd_value_is_ok;
99 if (close(fd) == -1 && errno == EBADF) {
100 // Bad file descriptor - probably because the fd is actually
101 // a socket.
102 closesocket(fd);
106 #endif
108 void
109 set_socket_timeouts(int fd, double timeout)
111 (void)fd;
112 (void)timeout;
113 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
115 # ifndef __WIN32__
116 struct timeval t;
117 RealTime::to_timeval(timeout, &t);
118 # else
119 // Just to be different, it's a DWORD counting in milliseconds.
120 DWORD t;
121 if (usual(timeout < numeric_limits<DWORD>::max() / 1000))
122 t = timeout * 1000;
123 else
124 t = numeric_limits<DWORD>::max();
125 # endif
126 # ifdef SO_SNDTIMEO
127 (void)setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
128 reinterpret_cast<char*>(&t), sizeof(t));
129 # endif
130 # ifdef SO_RCVTIMEO
131 (void)setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
132 reinterpret_cast<char*>(&t), sizeof(t));
133 # endif
135 #endif
136 #ifdef SO_KEEPALIVE
137 // SO_SNDTIMEO and SO_RCVTIMEO may be ignored even if they exist, so set
138 // SO_KEEPALIVE anyway if it exists, as it will cause stuck connections to
139 // time out eventually (though it may take ~2 hours).
141 # ifndef __WIN32__
142 int flag = 1;
143 # else
144 DWORD flag = 1;
145 # endif
146 (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
147 reinterpret_cast<char*>(&flag), sizeof(flag));
149 #endif