Remote Call Framework 3.3
Unix/BsdSockets.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2022, Delta V Software. All rights reserved.
6 // https://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF under GPL terms.
12 //
13 // Version: 3.3
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
18 #ifndef INCLUDE_UTIL_PLATFORM_OS_UNIX_BSDSOCKETS_HPP
19 #define INCLUDE_UTIL_PLATFORM_OS_UNIX_BSDSOCKETS_HPP
20 
21 #if defined(sun) || defined(__sun) || defined(__sun__)
22 #define BSD_COMP // Needs to be defined in order to use FIONBIO in ioctl()
23 #define MSG_NOSIGNAL 0 // MSG_NOSIGNAL flag for send() not implemented on Solaris
24 #endif
25 
26 #if defined(__MACH__) && defined(__APPLE__)
27 
28 // Incompatibilites in the 10.2.8 and 10.3.9 headers
29 #ifndef _BSD_SOCKLEN_T_
30 #define _BSD_SOCKLEN_T_ int32_t
31 #endif
32 
33 #if defined( __GNUC__ ) && !defined(MSG_NOSIGNAL)
34 #define MSG_NOSIGNAL 0
35 #endif
36 
37 #endif // defined(__MACH__) && defined(__APPLE__)
38 
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <sys/uio.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <netdb.h>
50 #include <signal.h>
51 
52 #include <cerrno>
53 
54 #include <string>
55 #include <string.h>
56 
57 #ifndef __UNIX__
58 #define __UNIX__
59 #endif
60 
61 // O_BINARY is not defined in Unix (all files are "binary" anyway)
62 #ifndef O_BINARY
63 #define O_BINARY 0
64 #endif
65 
66 #ifndef SOCKET
67 #define SOCKET int
68 #endif
69 
70 #ifndef INVALID_SOCKET
71 #define INVALID_SOCKET -1
72 #endif
73 
74 // Solaris doesn't define INADDR_NONE, for some reason
75 #ifndef INADDR_NONE
76 #define INADDR_NONE ((unsigned long) -1)
77 #endif
78 
79 namespace Platform {
80 
81  namespace OS {
82 
83  inline std::string GetErrorString(int err) { return std::string( strerror(err) ); }
84 
85  inline std::string GetErrorString() { return GetErrorString( errno ); }
86 
87  namespace BsdSockets {
88 
89  typedef ::socklen_t socklen_t;
90 
91  inline int recv(int fd, char *buf, int len, int flags)
92  {
93  return ::recv(fd, buf, len, flags);
94  }
95 
96  inline int send(int fd, const char *buf, int len, int flags)
97  {
98  return ::send(fd, buf, len, flags | MSG_NOSIGNAL);
99  }
100 
101  inline int sendto(int fd, const char *buf, int len, int flags, const sockaddr *to, int tolen)
102  {
103  return ::sendto(fd, buf, len, flags, to, tolen);
104  }
105 
106  inline int recvfrom(int fd, char *buf, int len, int flags, sockaddr *from, int *fromlen)
107  {
108  if (from)
109  {
110  socklen_t fromlen_ = *fromlen;
111  int ret = ::recvfrom(fd, buf, len, flags, from, &fromlen_);
112  *fromlen = fromlen_;
113  return ret;
114  }
115  else
116  {
117  return ::recvfrom(fd, buf, len, flags, NULL, NULL);
118  }
119  }
120 
121  inline int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
122  {
123  return ::select( nfds, readfds, writefds, exceptfds, const_cast<struct timeval *>(timeout) );
124  }
125 
126  inline int accept(int fd, sockaddr *addr, int *addrlen)
127  {
128  socklen_t addrlen_ = *addrlen;
129  int ret = ::accept(fd, addr, &addrlen_);
130  *addrlen = addrlen_;
131  return ret;
132  }
133 
134  inline int connect(int fd, const sockaddr *name, int namelen)
135  {
136  return ::connect(fd, name, namelen);
137  }
138 
139  inline int closesocket(int fd)
140  {
141  return ::close(fd);
142  }
143 
144  inline void setblocking(SOCKET fd, bool bBlocking)
145  {
146  int arg = bBlocking ? 0 : 1;
147  ::ioctl(fd, FIONBIO, &arg);
148  }
149 
150  inline int GetLastError()
151  {
152  return errno;
153  }
154 
155 #if (defined(__MACH__) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__ANDROID__)
156 
157  inline void disableBrokenPipeSignals()
158  {
159  signal(SIGPIPE, SIG_IGN);
160  }
161 
162 #else
163 
164  inline void disableBrokenPipeSignals()
165  {
166  signal(SIGPIPE, SIG_IGN);
167  }
168 
169 #endif
170 
171  static const int ERR_EWOULDBLOCK = EWOULDBLOCK;
172  static const int ERR_EINPROGRESS = EINPROGRESS;
173  static const int ERR_ECONNRESET = ECONNRESET;
174  static const int ERR_ECONNABORTED = ECONNABORTED;
175  static const int ERR_ECONNREFUSED = ECONNREFUSED;
176  static const int ERR_EMSGSIZE = EMSGSIZE;
177  static const int ERR_EADDRINUSE = EADDRINUSE;
178 
179  } //namespace BsdSockets
180 
181  } // namespace OS;
182 
183 } // namespace Platform
184 
185 #endif
Definition: Unix/BsdSockets.hpp:79