2 * @brief #include <sys/socket.h> with portability workarounds.
4 /* Copyright (C) 2012,2013,2014,2018,2019 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
25 // Some older BSDs require sys/types.h to be included first.
26 # include <sys/types.h>
27 # include <sys/socket.h>
29 # include "safewinsock2.h"
33 # include <type_traits>
34 # include "xapian/error.h"
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
<SOCKET
>::value
, "SOCKET is unsigned");
42 inline int socket_(int domain
, int type
, int protocol
) {
43 // Winsock2's socket() returns the unsigned type SOCKET, which is a 32-bit
44 // type for WIN32 and a 64-bit type for WIN64.
46 // It seems we can always safely assign SOCKET to an int: failure is indicated
47 // by INVALID_SOCKET which will cast to -1 as an int, and it seems in
48 // practice that valid values all fit in 31-bits (and that we're not the
49 // only code to assume this since it makes it much easier to write code
50 // that deals with BSD sockets and winsock2's bastardised version of them)
51 // so Microsoft are unlikely to arbitrarily change that).
53 // But we should check and throw an exception rather than quietly mangling
55 SOCKET sock
= socket(domain
, type
, protocol
);
56 if (rare(sock
> SOCKET(0x7fffffff) && sock
!= INVALID_SOCKET
)) {
57 throw Xapian::NetworkError("socket() returned value > INT_MAX");
65 # define socket(D,T,P) socket_(D,T,P)
67 #elif !defined SOCK_CLOEXEC
68 # define SOCK_CLOEXEC 0
70 // On Linux at least, sometimes SOCK_CLOEXEC is defined but the kernel doesn't
71 // handle it in socket() or socketpair():
75 inline int socket_(int domain
, int type
, int protocol
) {
76 // Usually type is passed a constant, so we'll collapse to one branch or
77 // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
78 if (type
& SOCK_CLOEXEC
) {
79 int save_errno
= errno
;
80 int r
= socket(domain
, type
, protocol
);
81 if (r
< 0 && errno
== EINVAL
) {
83 r
= socket(domain
, type
&~ SOCK_CLOEXEC
, protocol
);
87 return socket(domain
, type
, protocol
);
91 inline int socketpair_(int domain
, int type
, int protocol
, int *sv
) {
92 // Usually type is passed a constant, so we'll collapse to one branch or
93 // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
94 if (type
& SOCK_CLOEXEC
) {
95 int save_errno
= errno
;
96 int r
= socketpair(domain
, type
, protocol
, sv
);
97 if (r
!= 0 && errno
== EINVAL
) {
99 r
= socketpair(domain
, type
&~ SOCK_CLOEXEC
, protocol
, sv
);
103 return socketpair(domain
, type
, protocol
, sv
);
110 # define socket(D,T,P) socket_(D,T,P)
114 # define socketpair(D,T,P,S) socketpair_(D,T,P,S)
117 #endif // XAPIAN_INCLUDED_SAFESYSSOCKET_H