Update for 1.4.18
[xapian.git] / xapian-core / common / socket_utils.cc
blobe6a97a32a641c3b262bc2a7d3a751aa12f38f733
1 /** @file
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 <io.h>
33 # include "msvcignoreinvalidparam.h"
34 # include <cerrno>
36 /// Convert an fd (which might be a socket) to a WIN32 HANDLE.
37 extern HANDLE fd_to_handle(int fd) {
38 MSVCIgnoreInvalidParameter invalid_handle_value_is_ok;
39 HANDLE handle = (HANDLE)_get_osfhandle(fd);
40 if (handle != INVALID_HANDLE_VALUE) return handle;
41 // On WIN32, a socket fd isn't the same as a non-socket fd - in fact it's
42 // already a HANDLE!
43 return reinterpret_cast<HANDLE>(fd);
46 /// Close an fd, which might be a socket.
47 extern void close_fd_or_socket(int fd) {
48 MSVCIgnoreInvalidParameter invalid_fd_value_is_ok;
49 if (close(fd) == -1 && errno == EBADF) {
50 // Bad file descriptor - probably because the fd is actually
51 // a socket.
52 closesocket(fd);
56 #endif
58 void
59 set_socket_timeouts(int fd, double timeout)
61 (void)fd;
62 (void)timeout;
63 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
65 # ifndef __WIN32__
66 struct timeval t;
67 RealTime::to_timeval(timeout, &t);
68 # else
69 // Just to be different, it's a DWORD counting in milliseconds.
70 DWORD t;
71 if (usual(timeout < numeric_limits<DWORD>::max() / 1000))
72 t = timeout * 1000;
73 else
74 t = numeric_limits<DWORD>::max();
75 # endif
76 # ifdef SO_SNDTIMEO
77 (void)setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
78 reinterpret_cast<char*>(&t), sizeof(t));
79 # endif
80 # ifdef SO_RCVTIMEO
81 (void)setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
82 reinterpret_cast<char*>(&t), sizeof(t));
83 # endif
85 #endif
86 #ifdef SO_KEEPALIVE
87 // SO_SNDTIMEO and SO_RCVTIMEO may be ignored even if they exist, so set
88 // SO_KEEPALIVE anyway if it exists, as it will cause stuck connections to
89 // time out eventually (though it may take ~2 hours).
91 # ifndef __WIN32__
92 int flag = 1;
93 # else
94 DWORD flag = 1;
95 # endif
96 (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
97 reinterpret_cast<char*>(&flag), sizeof(flag));
99 #endif