Add accept() wrapper
[xapian.git] / xapian-core / common / safesyssocket.h
blobf5e00c469eabd6434e2f3aac20690651aa463560
1 /** @file
2 * @brief #include <sys/socket.h> with portability workarounds.
3 */
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
24 #ifndef __WIN32__
25 // Some older BSDs require sys/types.h to be included first.
26 # include <sys/types.h>
27 # include <sys/socket.h>
28 #else
29 # include "safewinsock2.h"
30 #endif
32 #ifdef __WIN32__
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");
37 # endif
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
54 // the value.
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");
59 return int(sock);
62 # ifdef socket
63 # undef socket
64 # endif
65 # define socket(D,T,P) socket_(D,T,P)
67 inline int accept_(int sockfd, struct sockaddr* addr, SOCKLEN_T* addrlen) {
68 // Winsock2's accept() returns the unsigned type SOCKET, which is a 32-bit
69 // type for WIN32 and a 64-bit type for WIN64.
71 // It seems we can always safely assign SOCKET to an int: failure is indicated
72 // by INVALID_SOCKET which will cast to -1 as an int, and it seems in
73 // practice that valid values all fit in 31-bits (and that we're not the
74 // only code to assume this since it makes it much easier to write code
75 // that deals with BSD sockets and winsock2's bastardised version of them)
76 // so Microsoft are unlikely to arbitrarily change that).
78 // But we should check and throw an exception rather than quietly mangling
79 // the value.
80 SOCKET sock = accept(sockfd, addr, addrlen);
81 if (rare(sock > SOCKET(0x7fffffff) && sock != INVALID_SOCKET)) {
82 throw Xapian::NetworkError("accept() returned value > INT_MAX");
84 return int(sock);
87 # ifdef accept
88 # undef accept
89 # endif
90 # define accept(S,A,L) accept_(S,A,L)
92 #elif !defined SOCK_CLOEXEC
93 # define SOCK_CLOEXEC 0
94 #else
95 // On Linux at least, sometimes SOCK_CLOEXEC is defined but the kernel doesn't
96 // handle it in socket() or socketpair():
98 # include <cerrno>
100 inline int socket_(int domain, int type, int protocol) {
101 // Usually type is passed a constant, so we'll collapse to one branch or
102 // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
103 if (type & SOCK_CLOEXEC) {
104 int save_errno = errno;
105 int r = socket(domain, type, protocol);
106 if (r < 0 && errno == EINVAL) {
107 errno = save_errno;
108 r = socket(domain, type &~ SOCK_CLOEXEC, protocol);
110 return r;
111 } else {
112 return socket(domain, type, protocol);
116 inline int socketpair_(int domain, int type, int protocol, int *sv) {
117 // Usually type is passed a constant, so we'll collapse to one branch or
118 // the other here. The case where SOCK_CLOEXEC == 0 is handled suitably.
119 if (type & SOCK_CLOEXEC) {
120 int save_errno = errno;
121 int r = socketpair(domain, type, protocol, sv);
122 if (r != 0 && errno == EINVAL) {
123 errno = save_errno;
124 r = socketpair(domain, type &~ SOCK_CLOEXEC, protocol, sv);
126 return r;
127 } else {
128 return socketpair(domain, type, protocol, sv);
132 # ifdef socket
133 # undef socket
134 # endif
135 # define socket(D,T,P) socket_(D,T,P)
136 # ifdef socketpair
137 # undef socketpair
138 # endif
139 # define socketpair(D,T,P,S) socketpair_(D,T,P,S)
140 #endif
142 #endif // XAPIAN_INCLUDED_SAFESYSSOCKET_H