xapian-core  1.4.26
socket_utils.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2015 Olly Betts
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <config.h>
22 #include "socket_utils.h"
23 
24 #include <limits>
25 
26 #include "realtime.h"
27 #include "safesyssocket.h"
28 
29 using namespace std;
30 
31 #ifdef __WIN32__
32 # include <io.h>
33 # include "msvcignoreinvalidparam.h"
34 # include <cerrno>
35 
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  //
44  // We need to convert to intptr_t first to suppress a compiler warning here
45  // about casting an integer to a wider pointer type which is a reasonable
46  // warning in general, but we check that the value isn't truncated before
47  // we cast the HANDLE to int (see common/safesyssocket.h).
48  return reinterpret_cast<HANDLE>(intptr_t(fd));
49 }
50 
52 extern void close_fd_or_socket(int fd) {
53  MSVCIgnoreInvalidParameter invalid_fd_value_is_ok;
54  if (close(fd) == -1 && errno == EBADF) {
55  // Bad file descriptor - probably because the fd is actually
56  // a socket.
57  closesocket(fd);
58  }
59 }
60 
61 #endif
62 
63 void
65 {
66  (void)fd;
67  (void)timeout;
68 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
69  {
70 # ifndef __WIN32__
71  struct timeval t;
72  RealTime::to_timeval(timeout, &t);
73 # else
74  // Just to be different, it's a DWORD counting in milliseconds.
75  DWORD t;
76  if (usual(timeout < numeric_limits<DWORD>::max() / 1000))
77  t = timeout * 1000;
78  else
79  t = numeric_limits<DWORD>::max();
80 # endif
81 # ifdef SO_SNDTIMEO
82  (void)setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
83  reinterpret_cast<char*>(&t), sizeof(t));
84 # endif
85 # ifdef SO_RCVTIMEO
86  (void)setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
87  reinterpret_cast<char*>(&t), sizeof(t));
88 # endif
89  }
90 #endif
91 #ifdef SO_KEEPALIVE
92  // SO_SNDTIMEO and SO_RCVTIMEO may be ignored even if they exist, so set
93  // SO_KEEPALIVE anyway if it exists, as it will cause stuck connections to
94  // time out eventually (though it may take ~2 hours).
95  {
96 # ifndef __WIN32__
97  int flag = 1;
98 # else
99  DWORD flag = 1;
100 # endif
101  (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
102  reinterpret_cast<char*>(&flag), sizeof(flag));
103  }
104 #endif
105 }
int close(FD &fd)
Definition: fd.h:63
void set_socket_timeouts(int fd, double timeout)
Attempt to set socket-level timeouts.
Definition: socket_utils.cc:64
unsigned timeout
A timeout value in milliseconds.
Definition: types.h:100
Work around MSVC&#39;s unhelpful non-standard invalid parameter handling.
#define usual(COND)
Definition: config.h:576
include <sys/socket.h> with portability workarounds.
STL namespace.
void close_fd_or_socket(int fd)
Definition: socket_utils.h:38
Socket handling utilities.
void to_timeval(double t, struct timeval *tv)
Fill in struct timeval from number of seconds in a double.
Definition: realtime.h:110
Functions for handling a time or time interval in a double.