xapian-core  1.4.26
safesyssocket.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2012,2013,2014,2018,2019 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (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 #ifndef XAPIAN_INCLUDED_SAFESYSSOCKET_H
22 #define XAPIAN_INCLUDED_SAFESYSSOCKET_H
23 
24 // Some older BSDs require sys/types.h to be included first. Also seems to
25 // be needed for some mingw versions.
26 #include <sys/types.h>
27 #ifndef __WIN32__
28 # include <sys/socket.h>
29 #else
30 # include "safewinsock2.h"
31 #endif
32 
33 #ifdef __WIN32__
34 # include <type_traits>
35 # if defined SOCK_CLOEXEC
36 static_assert(!SOCK_CLOEXEC, "__WIN32__ doesn't support SOCK_CLOEXEC");
37 # endif
38 # define SOCK_CLOEXEC 0
39 
40 static_assert(std::is_unsigned<SOCKET>::value, "SOCKET is unsigned");
41 
42 inline int xapian_convert_socket_to_int_(SOCKET sock) {
43  // Winsock2 function socket() and accept() return the unsigned type SOCKET,
44  // which is a 32-bit type for WIN32 and a 64-bit type for WIN64.
45  //
46  // It seems we can always safely assign SOCKET to an int and treat the
47  // result like a file descriptor (with < 0 signalling invalid). Failure is
48  // indicated by INVALID_SOCKET which will cast to -1 as an int, and it
49  // seems in practice that valid values all fit in 31-bits (and that we're
50  // not the only code to assume this since it makes it much easier to write
51  // code that deals with BSD sockets and winsock2's bastardised version of
52  // them) so Microsoft are unlikely to arbitrarily change that).
53  //
54  // We check this assumption rather than quietly mangling the value.
55  if (rare(sock > SOCKET(0x7fffffff) && sock != INVALID_SOCKET)) {
56  closesocket(sock);
57  sock = INVALID_SOCKET;
58  // "Too many open sockets" seems the most appropriate error to fake.
59  WSASetLastError(WSAEMFILE);
60  }
61  return int(sock);
62 }
63 
64 inline int socket_(int domain, int type, int protocol) {
65  return xapian_convert_socket_to_int_(socket(domain, type, protocol));
66 }
67 
68 # ifdef socket
69 # undef socket
70 # endif
71 # define socket(D,T,P) socket_(D,T,P)
72 
73 inline int accept_(int sockfd, struct sockaddr* addr, SOCKLEN_T* addrlen) {
74  return xapian_convert_socket_to_int_(accept(sockfd, addr, addrlen));
75 }
76 
77 # ifdef accept
78 # undef accept
79 # endif
80 # define accept(S,A,L) accept_(S,A,L)
81 
82 #elif !defined SOCK_CLOEXEC
83 # define SOCK_CLOEXEC 0
84 #else
85 // On Linux at least, sometimes SOCK_CLOEXEC is defined but the kernel doesn't
86 // handle it in socket() or socketpair():
87 
88 # include <cerrno>
89 
90 inline int socket_(int domain, int type, int protocol) {
91  // Usually type is passed a constant, so we'll collapse to one branch or
92  // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
93  if (type & SOCK_CLOEXEC) {
94  int save_errno = errno;
95  int r = socket(domain, type, protocol);
96  if (r < 0 && errno == EINVAL) {
97  errno = save_errno;
98  r = socket(domain, type &~ SOCK_CLOEXEC, protocol);
99  }
100  return r;
101  } else {
102  return socket(domain, type, protocol);
103  }
104 }
105 
106 inline int socketpair_(int domain, int type, int protocol, int *sv) {
107  // Usually type is passed a constant, so we'll collapse to one branch or
108  // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
109  if (type & SOCK_CLOEXEC) {
110  int save_errno = errno;
111  int r = socketpair(domain, type, protocol, sv);
112  if (r != 0 && errno == EINVAL) {
113  errno = save_errno;
114  r = socketpair(domain, type &~ SOCK_CLOEXEC, protocol, sv);
115  }
116  return r;
117  } else {
118  return socketpair(domain, type, protocol, sv);
119  }
120 }
121 
122 # ifdef socket
123 # undef socket
124 # endif
125 # define socket(D,T,P) socket_(D,T,P)
126 # ifdef socketpair
127 # undef socketpair
128 # endif
129 # define socketpair(D,T,P,S) socketpair_(D,T,P,S)
130 #endif
131 
132 #endif // XAPIAN_INCLUDED_SAFESYSSOCKET_H
#define SOCKLEN_T
Definition: config.h:371
#define rare(COND)
Definition: config.h:575
#define SOCK_CLOEXEC
Definition: safesyssocket.h:83
include <winsock2.h> but working around problems.