xapian-core  1.4.22
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  return reinterpret_cast<HANDLE>(fd);
44 }
45 
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);
53  }
54 }
55 
56 #endif
57 
58 void
60 {
61  (void)fd;
62  (void)timeout;
63 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
64  {
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
84  }
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).
90  {
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));
98  }
99 #endif
100 }
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:59
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:574
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.