2 * @brief #include <sys/socket.h> with portability workarounds.
4 /* Copyright (C) 2012,2013,2014,2018,2019,2023 Olly Betts
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.
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.
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
21 #ifndef XAPIAN_INCLUDED_SAFESYSSOCKET_H
22 #define XAPIAN_INCLUDED_SAFESYSSOCKET_H
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>
28 # include <sys/socket.h>
30 # include "safewinsock2.h"
34 # include <type_traits>
35 # if defined SOCK_CLOEXEC
36 static_assert(!SOCK_CLOEXEC
, "__WIN32__ doesn't support SOCK_CLOEXEC");
38 # define SOCK_CLOEXEC 0
40 static_assert(std::is_unsigned_v
<SOCKET
>, "SOCKET is unsigned");
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.
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).
54 // We check this assumption rather than quietly mangling the value.
55 if (rare(sock
> SOCKET(0x7fffffff) && sock
!= INVALID_SOCKET
)) {
57 sock
= INVALID_SOCKET
;
58 // "Too many open sockets" seems the most appropriate error to fake.
59 WSASetLastError(WSAEMFILE
);
64 inline int socket_(int domain
, int type
, int protocol
) {
65 return xapian_convert_socket_to_int_(socket(domain
, type
, protocol
));
71 # define socket(D,T,P) socket_(D,T,P)
73 inline int accept_(int sockfd
, struct sockaddr
* addr
, SOCKLEN_T
* addrlen
) {
74 return xapian_convert_socket_to_int_(accept(sockfd
, addr
, addrlen
));
80 # define accept(S,A,L) accept_(S,A,L)
82 // Winsock2 effectively uses `int` instead of `socklen_t` everywhere except for
83 // in `struct addrinfo` where it uses `size_t`. This results in an integer
84 // truncation warning when calling `bind()` or `connect()` on an address looked
85 // up with `getaddrinfo()`. To solve this we add overloads which take `size_t`
86 // for `addrlen` and cast it to `SOCKLEN_T`.
88 inline int bind(SOCKET s
, const struct sockaddr
* addr
, size_t addrlen
) {
89 return bind(s
, addr
, SOCKLEN_T(addrlen
));
92 inline int connect(SOCKET s
, const struct sockaddr
* addr
, size_t addrlen
) {
93 return connect(s
, addr
, SOCKLEN_T(addrlen
));
96 #elif !defined SOCK_CLOEXEC
97 # define SOCK_CLOEXEC 0
99 // On Linux at least, sometimes SOCK_CLOEXEC is defined but the kernel doesn't
100 // handle it in socket() or socketpair():
104 inline int socket_(int domain
, int type
, int protocol
) {
105 // Usually type is passed a constant, so we'll collapse to one branch or
106 // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
107 if (type
& SOCK_CLOEXEC
) {
108 int save_errno
= errno
;
109 int r
= socket(domain
, type
, protocol
);
110 if (r
< 0 && errno
== EINVAL
) {
112 r
= socket(domain
, type
&~ SOCK_CLOEXEC
, protocol
);
116 return socket(domain
, type
, protocol
);
120 inline int socketpair_(int domain
, int type
, int protocol
, int *sv
) {
121 // Usually type is passed a constant, so we'll collapse to one branch or
122 // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
123 if (type
& SOCK_CLOEXEC
) {
124 int save_errno
= errno
;
125 int r
= socketpair(domain
, type
, protocol
, sv
);
126 if (r
!= 0 && errno
== EINVAL
) {
128 r
= socketpair(domain
, type
&~ SOCK_CLOEXEC
, protocol
, sv
);
132 return socketpair(domain
, type
, protocol
, sv
);
139 # define socket(D,T,P) socket_(D,T,P)
143 # define socketpair(D,T,P,S) socketpair_(D,T,P,S)
146 #endif // XAPIAN_INCLUDED_SAFESYSSOCKET_H