xapian-core  2.0.0
socket_utils.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2015,2018,2023 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, see
18  * <https://www.gnu.org/licenses/>.
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 #include "stringutils.h"
32 
33 #ifndef __WIN32__
34 # include <arpa/inet.h>
35 # include <netinet/in.h>
36 #else
37 # include <io.h>
38 # include "msvcignoreinvalidparam.h"
39 # include <cerrno>
40 
42 extern HANDLE fd_to_handle(int fd) {
43  MSVCIgnoreInvalidParameter invalid_handle_value_is_ok;
44  HANDLE handle = (HANDLE)_get_osfhandle(fd);
45  if (handle != INVALID_HANDLE_VALUE) return handle;
46  // On WIN32, a socket fd isn't the same as a non-socket fd - in fact it's
47  // already a HANDLE!
48  //
49  // We need to convert to intptr_t first to suppress a compiler warning here
50  // about casting an integer to a wider pointer type which is a reasonable
51  // warning in general, but we check that the value isn't truncated before
52  // we cast the HANDLE to int (see common/safesyssocket.h).
53  return reinterpret_cast<HANDLE>(intptr_t(fd));
54 }
55 
57 extern void close_fd_or_socket(int fd) {
58  MSVCIgnoreInvalidParameter invalid_fd_value_is_ok;
59  if (close(fd) == -1 && errno == EBADF) {
60  // Bad file descriptor - probably because the fd is actually
61  // a socket.
62  closesocket(fd);
63  }
64 }
65 
66 #endif
67 
68 void
69 set_socket_timeouts(int fd, double timeout)
70 {
71  (void)fd;
72  (void)timeout;
73 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
74  {
75 # ifndef __WIN32__
76  struct timeval t;
77  RealTime::to_timeval(timeout, &t);
78 # else
79  // Just to be different, it's a DWORD counting in milliseconds.
80  DWORD t;
81  if (usual(timeout < numeric_limits<DWORD>::max() / 1000))
82  t = timeout * 1000;
83  else
84  t = numeric_limits<DWORD>::max();
85 # endif
86 # ifdef SO_SNDTIMEO
87  (void)setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
88  reinterpret_cast<char*>(&t), sizeof(t));
89 # endif
90 # ifdef SO_RCVTIMEO
91  (void)setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
92  reinterpret_cast<char*>(&t), sizeof(t));
93 # endif
94  }
95 #endif
96 #ifdef SO_KEEPALIVE
97  // SO_SNDTIMEO and SO_RCVTIMEO may be ignored even if they exist, so set
98  // SO_KEEPALIVE anyway if it exists, as it will cause stuck connections to
99  // time out eventually (though it may take ~2 hours).
100  {
101 # ifndef __WIN32__
102  int flag = 1;
103 # else
104  DWORD flag = 1;
105 # endif
106  (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
107  reinterpret_cast<char*>(&flag), sizeof(flag));
108  }
109 #endif
110 }
111 
112 int
113 pretty_ip6(const void* p, char* buf)
114 {
115  const sockaddr* sa = reinterpret_cast<const sockaddr*>(p);
116  auto af = sa->sa_family;
117  int port;
118 #ifndef __WIN32__
119  const void* src;
120 #endif
121  if (af == AF_INET6) {
122  auto sa6 = reinterpret_cast<const sockaddr_in6*>(p);
123  port = sa6->sin6_port;
124 #ifndef __WIN32__
125  src = &sa6->sin6_addr;
126 #endif
127  } else if (af == AF_INET) {
128  auto sa4 = reinterpret_cast<const sockaddr_in*>(p);
129  port = sa4->sin_port;
130 #ifndef __WIN32__
131  src = &sa4->sin_addr;
132 #endif
133  } else {
134  return -1;
135  }
136 
137 #ifndef __WIN32__
138  const char* r = inet_ntop(af, src, buf, PRETTY_IP6_LEN);
139  if (!r)
140  return -1;
141 #else
142  // inet_ntop() isn't always available (at least with mingw) but
143  // WSAAddressToString() supports both IPv4 and IPv6, so just use that.
144  //
145  // WSAAddressToString() has a non-const first parameter so we have to cast
146  // away const.
147  DWORD in_size = (af == AF_INET6 ?
148  sizeof(struct sockaddr_in6) :
149  sizeof(struct sockaddr_in));
150  DWORD size = PRETTY_IP6_LEN;
151  if (WSAAddressToString(const_cast<struct sockaddr*>(sa),
152  in_size, NULL, buf, &size) != 0) {
153  return -1;
154  }
155  const char* r = buf;
156 #endif
157 
158  if (startswith(r, "::ffff:") || startswith(r, "::FFFF:")) {
159  if (strchr(r + 7, '.')) {
160  r += 7;
161  }
162  }
163 
164  if (r != buf)
165  memmove(buf, r, strlen(r) + 1);
166 
167  return port;
168 }
if(!(properties &BACKEND))
Definition: api_collated.h:3
#define usual(COND)
Definition: config.h:608
PositionList * p
int close(FD &fd)
Definition: fd.h:63
Work around MSVC's unhelpful non-standard invalid parameter handling.
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.
include <sys/socket.h> with portability workarounds.
void set_socket_timeouts(int fd, double timeout)
Attempt to set socket-level timeouts.
Definition: socket_utils.cc:69
int pretty_ip6(const void *p, char *buf)
Socket handling utilities.
void close_fd_or_socket(int fd)
Definition: socket_utils.h:119
constexpr size_t PRETTY_IP6_LEN
Definition: socket_utils.h:137
Various handy string-related helpers.
bool startswith(std::string_view s, char pfx)
Definition: stringutils.h:56