/[resiprocate]/main/rutil/Socket.hxx
ViewVC logotype

Contents of /main/rutil/Socket.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11235 - (show annotations) (download)
Mon Sep 15 17:20:11 2014 UTC (5 years, 2 months ago) by dpocock
File MIME type: text/plain
File size: 8876 byte(s)
resip/stack: TcpBaseTransport: generalize support for SO_NOSIGPIPE
1 #if !defined(RESIP_SOCKET_HXX)
2 #define RESIP_SOCKET_HXX
3
4 #include <cassert>
5 #include <errno.h>
6 #include <algorithm>
7
8 #ifdef WIN32
9 #include <winsock2.h>
10 #include <stdlib.h>
11 #include <io.h>
12 #include <WS2TCPIP.H>
13 #endif
14
15 #include "compat.hxx"
16 #include "rutil/TransportType.hxx"
17
18 /**
19 @file
20 @brief Handles cross-platform sockets compatibility.
21 */
22
23 #ifdef WIN32
24
25 /** With VS 2010 Berkeley errno constants have been redefined and are now different than WSAGetLastError() WSA prefixed constants.
26 @see http://msdn.microsoft.com/en-us/library/ms737828(VS.85).aspx, https://connect.microsoft.com/VisualStudio/feedback/details/509380/errno-h-socket-return-codes-now-inconsistent-with-wsagetlasterror?wa=wsignin1.0
27 @see <winsock2.h>
28 The recommended fix is to use WSAGetLastError() and with the WSAXXX constants. One way to do this would be to create RESIP_XXX socket constants that would correclty use WSAXXX constants
29 on windows without using the Berkeley style constants. For now just re-assign the Berkeley style constants as WSA constants.
30 */
31 #if defined(_MSC_VER) && (_MSC_VER >= 1600)
32 #pragma warning (push)
33 #pragma warning (disable: 4005 )
34 #endif
35 typedef int socklen_t;
36
37 //this list taken from <winsock2.h>, see #if 0 removed block.
38 #define EWOULDBLOCK WSAEWOULDBLOCK
39 #define EINPROGRESS WSAEINPROGRESS
40 #define EALREADY WSAEALREADY
41 #define ENOTSOCK WSAENOTSOCK
42 #define EDESTADDRREQ WSAEDESTADDRREQ
43 #define EMSGSIZE WSAEMSGSIZE
44 #define EPROTOTYPE WSAEPROTOTYPE
45 #define ENOPROTOOPT WSAENOPROTOOPT
46 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
47 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
48 #define EOPNOTSUPP WSAEOPNOTSUPP
49 #define EPFNOSUPPORT WSAEPFNOSUPPORT
50 #define EAFNOSUPPORT WSAEAFNOSUPPORT
51 #define EADDRINUSE WSAEADDRINUSE
52 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
53 #define ENETDOWN WSAENETDOWN
54 #define ENETUNREACH WSAENETUNREACH
55 #define ENETRESET WSAENETRESET
56 #define ECONNABORTED WSAECONNABORTED
57 #define ECONNRESET WSAECONNRESET
58 #define ENOBUFS WSAENOBUFS
59 #define EISCONN WSAEISCONN
60 #define ENOTCONN WSAENOTCONN
61 #define ESHUTDOWN WSAESHUTDOWN
62 #define ETOOMANYREFS WSAETOOMANYREFS
63 #define ETIMEDOUT WSAETIMEDOUT
64 #define ECONNREFUSED WSAECONNREFUSED
65 #define ELOOP WSAELOOP
66 //#define ENAMETOOLONG WSAENAMETOOLONG
67 #define EHOSTDOWN WSAEHOSTDOWN
68 #define EHOSTUNREACH WSAEHOSTUNREACH
69 //#define ENOTEMPTY WSAENOTEMPTY
70 #define EPROCLIM WSAEPROCLIM
71 #define EUSERS WSAEUSERS
72 #define EDQUOT WSAEDQUOT
73 #define ESTALE WSAESTALE
74 #define EREMOTE WSAEREMOTE
75
76 #if defined(_MSC_VER) && (_MSC_VER >= 1600)
77 #pragma warning (pop)
78 #endif
79
80 #else
81
82 #define WSANOTINITIALISED EPROTONOSUPPORT
83
84 #endif
85
86 namespace resip
87 {
88
89 /// set up network - does nothing in unix but needed for windows
90 void
91 initNetwork();
92
93 #ifndef WIN32
94 typedef int Socket; // Un*x fds are signed
95 #define INVALID_SOCKET (-1)
96 #define SOCKET_ERROR (-1)
97 inline int getErrno() { return errno; }
98 #else
99 typedef SOCKET Socket; // Windows defines as *unsigned* something
100 // INVALID_SOCKET is defined by Windows as ~0 (since Windows socket unsigned)
101 // SOCKET_ERROR is defined by Windows as -1 (most func retvals are signed)
102 inline int getErrno() { return WSAGetLastError(); }
103 #endif
104
105 //c function pointer because of ares
106 extern "C" {
107 typedef void(*AfterSocketCreationFuncPtr)(Socket s, int transportType, const char* file, int line);
108 }
109
110 bool makeSocketNonBlocking(Socket fd);
111 bool makeSocketBlocking(Socket fd);
112 bool configureConnectedSocket(Socket fd);
113 int closeSocket( Socket fd );
114 int getSocketError(Socket fd); // getsockopt(SOCK_SOCKET,SO_ERROR)
115 int increaseLimitFds(unsigned int targetFds);
116 int setSocketRcvBufLen(Socket fd, int buflen); // setsockopt(SO_RCVBUF)
117
118
119 /**
120 @brief Object-oriented wrapper for your platform's file-descriptor set.
121 */
122 class FdSet
123 {
124 public:
125 FdSet() : size(0), numReady(0)
126 {
127 FD_ZERO(&read);
128 FD_ZERO(&write);
129 FD_ZERO(&except);
130 }
131
132 int select(struct timeval& tv)
133 {
134 return numReady = ::select(size, &read, &write, &except, &tv);
135 }
136
137 int selectMilliSeconds(unsigned long ms)
138 {
139 struct timeval tv;
140 tv.tv_sec = (ms/1000);
141 tv.tv_usec = (ms%1000)*1000;
142 return select(tv);
143 }
144
145 bool readyToRead(Socket fd)
146 {
147 return (FD_ISSET(fd, &read) != 0);
148 }
149
150 bool readyToWrite(Socket fd)
151 {
152 return (FD_ISSET(fd, &write) != 0);
153 }
154
155 bool hasException(Socket fd)
156 {
157 return (FD_ISSET(fd,&except) != 0);
158 }
159
160 void setRead(Socket fd)
161 {
162 assert( FD_SETSIZE >= 8 );
163 #ifndef WIN32 // windows fd are not int's and don't start at 0 - this won't work in windows
164 assert( fd < (int)FD_SETSIZE ); // redefineing FD_SETSIZE will not work
165 #else
166 assert(read.fd_count < FD_SETSIZE); // Ensure there is room to add new FD
167 #endif
168 FD_SET(fd, &read);
169 size = ( int(fd+1) > size) ? int(fd+1) : size;
170 }
171
172 void setWrite(Socket fd)
173 {
174 #ifndef WIN32 // windows fd are not int's and don't start at 0 - this won't work in windows
175 assert( fd < (int)FD_SETSIZE ); // redefinitn FD_SETSIZE will not work
176 #else
177 assert(write.fd_count < FD_SETSIZE); // Ensure there is room to add new FD
178 #endif
179 FD_SET(fd, &write);
180 size = ( int(fd+1) > size) ? int(fd+1) : size;
181 }
182
183 void setExcept(Socket fd)
184 {
185 #ifndef WIN32 // windows fd are not int's and don't start at 0 - this won't work in windows
186 assert( fd < (int)FD_SETSIZE ); // redefinitn FD_SETSIZE will not work
187 #else
188 assert(except.fd_count < FD_SETSIZE); // Ensure there is room to add new FD
189 #endif
190 FD_SET(fd,&except);
191 size = ( int(fd+1) > size) ? int(fd+1) : size;
192 }
193
194
195 void clear(Socket fd)
196 {
197 FD_CLR(fd, &read);
198 FD_CLR(fd, &write);
199 FD_CLR(fd, &except);
200 }
201
202 void reset()
203 {
204 size = 0;
205 numReady = 0;
206 FD_ZERO(&read);
207 FD_ZERO(&write);
208 FD_ZERO(&except);
209 }
210
211 // Make this stuff public for async dns/ares to use
212 fd_set read;
213 fd_set write;
214 fd_set except;
215 int size;
216 int numReady; // set after each select call
217 };
218
219
220 }
221
222 #endif
223
224 /* ====================================================================
225 * The Vovida Software License, Version 1.0
226 *
227 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
228 *
229 * Redistribution and use in source and binary forms, with or without
230 * modification, are permitted provided that the following conditions
231 * are met:
232 *
233 * 1. Redistributions of source code must retain the above copyright
234 * notice, this list of conditions and the following disclaimer.
235 *
236 * 2. Redistributions in binary form must reproduce the above copyright
237 * notice, this list of conditions and the following disclaimer in
238 * the documentation and/or other materials provided with the
239 * distribution.
240 *
241 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
242 * and "Vovida Open Communication Application Library (VOCAL)" must
243 * not be used to endorse or promote products derived from this
244 * software without prior written permission. For written
245 * permission, please contact vocal@vovida.org.
246 *
247 * 4. Products derived from this software may not be called "VOCAL", nor
248 * may "VOCAL" appear in their name, without prior written
249 * permission of Vovida Networks, Inc.
250 *
251 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
252 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
253 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
254 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
255 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
256 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
257 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
258 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
259 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
260 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
262 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
263 * DAMAGE.
264 *
265 * ====================================================================
266 *
267 * This software consists of voluntary contributions made by Vovida
268 * Networks, Inc. and many individuals on behalf of Vovida Networks,
269 * Inc. For more information on Vovida Networks, Inc., please see
270 * <http://www.vovida.org/>.
271 *
272 * vi: set shiftwidth=3 expandtab:
273 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27