1 /** @file
2  * @brief #include <sys/socket.h> with portability workarounds.
3  */
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 #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
31 
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
39 
40 static_assert(std::is_unsigned<SOCKET>::value, "SOCKET is unsigned");
41 
socket_(int domain,int type,int protocol)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.
45     //
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).
52     //
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");
58     }
59     return int(sock);
60 }
61 
62 # ifdef socket
63 #  undef socket
64 # endif
65 # define socket(D,T,P) socket_(D,T,P)
66 
67 #elif !defined SOCK_CLOEXEC
68 # define SOCK_CLOEXEC 0
69 #else
70 // On Linux at least, sometimes SOCK_CLOEXEC is defined but the kernel doesn't
71 // handle it in socket() or socketpair():
72 
73 # include <cerrno>
74 
socket_(int domain,int type,int protocol)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) {
82 	    errno = save_errno;
83 	    r = socket(domain, type &~ SOCK_CLOEXEC, protocol);
84 	}
85 	return r;
86     } else {
87 	return socket(domain, type, protocol);
88     }
89 }
90 
socketpair_(int domain,int type,int protocol,int * sv)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) {
98 	    errno = save_errno;
99 	    r = socketpair(domain, type &~ SOCK_CLOEXEC, protocol, sv);
100 	}
101 	return r;
102     } else {
103 	return socketpair(domain, type, protocol, sv);
104     }
105 }
106 
107 # ifdef socket
108 #  undef socket
109 # endif
110 # define socket(D,T,P) socket_(D,T,P)
111 # ifdef socketpair
112 #  undef socketpair
113 # endif
114 # define socketpair(D,T,P,S) socketpair_(D,T,P,S)
115 #endif
116 
117 #endif // XAPIAN_INCLUDED_SAFESYSSOCKET_H
118