19 #ifndef INCLUDE_UTIL_PLATFORM_OS_UNIX_BSDSOCKETS_HPP 20 #define INCLUDE_UTIL_PLATFORM_OS_UNIX_BSDSOCKETS_HPP 22 #if defined(sun) || defined(__sun) || defined(__sun__) 23 #define BSD_COMP // Needs to be defined in order to use FIONBIO in ioctl() 24 #define MSG_NOSIGNAL 0 // MSG_NOSIGNAL flag for send() not implemented on Solaris 27 #if defined(__MACH__) && defined(__APPLE__) 30 #ifndef _BSD_SOCKLEN_T_ 31 #define _BSD_SOCKLEN_T_ int32_t 34 #if defined( __GNUC__ ) && !defined(MSG_NOSIGNAL) 35 #define MSG_NOSIGNAL 0 38 #endif // defined(__MACH__) && defined(__APPLE__) 45 #include <sys/types.h> 46 #include <sys/socket.h> 47 #include <sys/ioctl.h> 48 #include <netinet/in.h> 49 #include <arpa/inet.h> 71 #ifndef INVALID_SOCKET 72 #define INVALID_SOCKET -1 77 #define INADDR_NONE ((unsigned long) -1) 84 inline std::string GetErrorString(
int err) {
return std::string( strerror(err) ); }
86 inline std::string GetErrorString() {
return GetErrorString( errno ); }
88 namespace BsdSockets {
90 typedef ::socklen_t socklen_t;
92 inline int recv(
int fd,
char *buf,
int len,
int flags)
94 return ::recv(fd, buf, len, flags);
97 inline int send(
int fd,
const char *buf,
int len,
int flags)
99 return ::send(fd, buf, len, flags | MSG_NOSIGNAL);
102 inline int sendto(
int fd,
const char *buf,
int len,
int flags,
const sockaddr *to,
int tolen)
104 return ::sendto(fd, buf, len, flags, to, tolen);
107 inline int recvfrom(
int fd,
char *buf,
int len,
int flags, sockaddr *from,
int *fromlen)
111 socklen_t fromlen_ = *fromlen;
112 int ret = ::recvfrom(fd, buf, len, flags, from, &fromlen_);
118 return ::recvfrom(fd, buf, len, flags, NULL, NULL);
122 inline int select(
int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
const struct timeval *timeout)
124 return ::select( nfds, readfds, writefds, exceptfds, const_cast<struct timeval *>(timeout) );
127 inline int accept(
int fd, sockaddr *addr,
int *addrlen)
129 socklen_t addrlen_ = *addrlen;
130 int ret = ::accept(fd, addr, &addrlen_);
135 inline int connect(
int fd,
const sockaddr *name,
int namelen)
137 return ::connect(fd, name, namelen);
140 inline int closesocket(
int fd)
145 inline void setblocking(SOCKET fd,
bool bBlocking)
147 int arg = bBlocking ? 0 : 1;
148 ::ioctl(fd, FIONBIO, &arg);
151 inline int GetLastError()
156 #if (defined(__MACH__) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__ANDROID__) 158 inline void disableBrokenPipeSignals()
160 signal(SIGPIPE, SIG_IGN);
165 inline void disableBrokenPipeSignals()
172 static const int ERR_EWOULDBLOCK = EWOULDBLOCK;
173 static const int ERR_EINPROGRESS = EINPROGRESS;
174 static const int ERR_ECONNRESET = ECONNRESET;
175 static const int ERR_ECONNABORTED = ECONNABORTED;
176 static const int ERR_ECONNREFUSED = ECONNREFUSED;
177 static const int ERR_EMSGSIZE = EMSGSIZE;
178 static const int ERR_EADDRINUSE = EADDRINUSE;