21 #ifndef XAPIAN_INCLUDED_SAFESYSSOCKET_H
22 #define XAPIAN_INCLUDED_SAFESYSSOCKET_H
26 #include <sys/types.h>
28 # include <sys/socket.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<SOCKET>::value,
"SOCKET is unsigned");
42 inline int xapian_convert_socket_to_int_(SOCKET sock) {
62 if (
rare(sock > SOCKET(0x7fffffff) && sock != INVALID_SOCKET)) {
64 sock = INVALID_SOCKET;
66 WSASetLastError(WSAEMFILE);
71 inline int socket_(
int domain,
int type,
int protocol) {
72 return xapian_convert_socket_to_int_(socket(domain, type, protocol));
78 # define socket(D,T,P) socket_(D,T,P)
80 inline int accept_(
int sockfd,
struct sockaddr* addr,
SOCKLEN_T* addrlen) {
81 return xapian_convert_socket_to_int_(accept(sockfd, addr, addrlen));
87 # define accept(S,A,L) accept_(S,A,L)
89 #elif !defined SOCK_CLOEXEC
90 # define SOCK_CLOEXEC 0
97 inline int socket_(
int domain,
int type,
int protocol) {
101 int save_errno = errno;
102 int r = socket(domain, type, protocol);
103 if (r < 0 && errno == EINVAL) {
109 return socket(domain, type, protocol);
113 inline int socketpair_(
int domain,
int type,
int protocol,
int *sv) {
117 int save_errno = errno;
118 int r = socketpair(domain, type, protocol, sv);
119 if (r != 0 && errno == EINVAL) {
121 r = socketpair(domain, type &~
SOCK_CLOEXEC, protocol, sv);
125 return socketpair(domain, type, protocol, sv);
132 # define socket(D,T,P) socket_(D,T,P)
136 # define socketpair(D,T,P,S) socketpair_(D,T,P,S)
include <winsock2.h> but working around problems.