xapian-core  1.4.29
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  // https://learn.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication
47  // says that only the lower 32 bits are significant and one can
48  // truncate to 32 bits or sign-extend to 64 bits.
49  //
50  // We make a slight additional assumption that valid socket values fit
51  // in 31 bits (with INVALID_SOCKET being all bits set), which seems to
52  // be true in practice. This means we can cast SOCKET to int and treat
53  // the result like a file descriptor (and INVALID_SOCKET casts to -1
54  // which matches file descriptor semantics).
55  //
56  // Ours is not the only code to assume this since it makes it much easier
57  // to write code that deals with BSD sockets and winsock2's bastardised
58  // version of them) so hopefully Microsoft are unlikely to arbitrarily
59  // change this.
60  //
61  // We check this assumption rather than quietly mangling the value.
62  if (rare(sock > SOCKET(0x7fffffff) && sock != INVALID_SOCKET)) {
63  closesocket(sock);
64  sock = INVALID_SOCKET;
65  // "Too many open sockets" seems the most appropriate error to fake.
66  WSASetLastError(WSAEMFILE);
67  }
68  return int(sock);
69 }
70 
71 inline int socket_(int domain, int type, int protocol) {
72  return xapian_convert_socket_to_int_(socket(domain, type, protocol));
73 }
74 
75 # ifdef socket
76 # undef socket
77 # endif
78 # define socket(D,T,P) socket_(D,T,P)
79 
80 inline int accept_(int sockfd, struct sockaddr* addr, SOCKLEN_T* addrlen) {
81  return xapian_convert_socket_to_int_(accept(sockfd, addr, addrlen));
82 }
83 
84 # ifdef accept
85 # undef accept
86 # endif
87 # define accept(S,A,L) accept_(S,A,L)
88 
89 #elif !defined SOCK_CLOEXEC
90 # define SOCK_CLOEXEC 0
91 #else
92 // On Linux at least, sometimes SOCK_CLOEXEC is defined but the kernel doesn't
93 // handle it in socket() or socketpair():
94 
95 # include <cerrno>
96 
97 inline int socket_(int domain, int type, int protocol) {
98  // Usually type is passed a constant, so we'll collapse to one branch or
99  // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
100  if (type & SOCK_CLOEXEC) {
101  int save_errno = errno;
102  int r = socket(domain, type, protocol);
103  if (r < 0 && errno == EINVAL) {
104  errno = save_errno;
105  r = socket(domain, type &~ SOCK_CLOEXEC, protocol);
106  }
107  return r;
108  } else {
109  return socket(domain, type, protocol);
110  }
111 }
112 
113 inline int socketpair_(int domain, int type, int protocol, int *sv) {
114  // Usually type is passed a constant, so we'll collapse to one branch or
115  // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
116  if (type & SOCK_CLOEXEC) {
117  int save_errno = errno;
118  int r = socketpair(domain, type, protocol, sv);
119  if (r != 0 && errno == EINVAL) {
120  errno = save_errno;
121  r = socketpair(domain, type &~ SOCK_CLOEXEC, protocol, sv);
122  }
123  return r;
124  } else {
125  return socketpair(domain, type, protocol, sv);
126  }
127 }
128 
129 # ifdef socket
130 # undef socket
131 # endif
132 # define socket(D,T,P) socket_(D,T,P)
133 # ifdef socketpair
134 # undef socketpair
135 # endif
136 # define socketpair(D,T,P,S) socketpair_(D,T,P,S)
137 #endif
138 
139 #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:90
include <winsock2.h> but working around problems.