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) {
55 if (
rare(sock > SOCKET(0x7fffffff) && sock != INVALID_SOCKET)) {
57 sock = INVALID_SOCKET;
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 #elif !defined SOCK_CLOEXEC 83 # define SOCK_CLOEXEC 0 90 inline int socket_(
int domain,
int type,
int protocol) {
94 int save_errno = errno;
95 int r = socket(domain, type, protocol);
96 if (r < 0 && errno == EINVAL) {
98 r = socket(domain, type &~ SOCK_CLOEXEC, protocol);
102 return socket(domain, type, protocol);
106 inline int socketpair_(
int domain,
int type,
int protocol,
int *sv) {
110 int save_errno = errno;
111 int r = socketpair(domain, type, protocol, sv);
112 if (r != 0 && errno == EINVAL) {
114 r = socketpair(domain, type &~ SOCK_CLOEXEC, protocol, sv);
118 return socketpair(domain, type, protocol, sv);
125 # define socket(D,T,P) socket_(D,T,P) 129 # define socketpair(D,T,P,S) socketpair_(D,T,P,S) 132 #endif // XAPIAN_INCLUDED_SAFESYSSOCKET_H
include <winsock2.h> but working around problems.