reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
Socket.hxx
Go to the documentation of this file.
1#if !defined(RESIP_SOCKET_HXX)
2#define RESIP_SOCKET_HXX
3
4#include "rutil/ResipAssert.h"
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"
17
23#ifdef WIN32
24
31#if defined(_MSC_VER) && (_MSC_VER >= 1600)
32#pragma warning (push)
33#pragma warning (disable: 4005 )
34#endif
35typedef 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
86namespace resip
87{
88
90void
92
93#ifndef WIN32
94typedef int Socket; // Un*x fds are signed
95#define INVALID_SOCKET (-1)
96#define SOCKET_ERROR (-1)
97inline int getErrno() { return errno; }
98#else
99typedef 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)
102inline int getErrno() { return WSAGetLastError(); }
103#endif
104
105//c function pointer because of ares
106extern "C" {
107typedef void(*AfterSocketCreationFuncPtr)(Socket s, int transportType, const char* file, int line);
108}
109
113int closeSocket( Socket fd );
114int getSocketError(Socket fd); // getsockopt(SOCK_SOCKET,SO_ERROR)
115int increaseLimitFds(unsigned int targetFds);
116int setSocketRcvBufLen(Socket fd, int buflen); // setsockopt(SO_RCVBUF)
117
140int setSocketDscp(Socket fd, int dscp, IpVersion version); // setsockopt(IP_TOS / IPV6_TCLASS)
141
149int getSocketTos(Socket fd, IpVersion version); // getsockopt(IP_TOS / IPV6_TCLASS)
150
155int getSocketDscp(Socket fd, IpVersion version);
156
157
161class FdSet
162{
163public:
164 FdSet() : size(0), numReady(0)
165 {
166 FD_ZERO(&read);
167 FD_ZERO(&write);
168 FD_ZERO(&except);
169 }
170
171 int select()
172 {
173 return numReady = ::select(size, &read, &write, &except, NULL);
174 }
175
176 int select(struct timeval& tv)
177 {
178 return numReady = ::select(size, &read, &write, &except, &tv);
179 }
180
181 int selectMilliSeconds(unsigned long ms)
182 {
183 struct timeval tv;
184 tv.tv_sec = (ms/1000);
185 tv.tv_usec = (ms%1000)*1000;
186 return select(tv);
187 }
188
190 {
191 return (FD_ISSET(fd, &read) != 0);
192 }
193
195 {
196 return (FD_ISSET(fd, &write) != 0);
197 }
198
200 {
201 return (FD_ISSET(fd,&except) != 0);
202 }
203
205 {
206 resip_assert( FD_SETSIZE >= 8 );
207#ifndef WIN32 // windows fd are not int's and don't start at 0 - this won't work in windows
208 resip_assert( fd < (int)FD_SETSIZE ); // redefineing FD_SETSIZE will not work
209#else
210 resip_assert(read.fd_count < FD_SETSIZE); // Ensure there is room to add new FD
211#endif
212 FD_SET(fd, &read);
213 size = ( int(fd+1) > size) ? int(fd+1) : size;
214 }
215
217 {
218#ifndef WIN32 // windows fd are not int's and don't start at 0 - this won't work in windows
219 resip_assert( fd < (int)FD_SETSIZE ); // redefinitn FD_SETSIZE will not work
220#else
221 resip_assert(write.fd_count < FD_SETSIZE); // Ensure there is room to add new FD
222#endif
223 FD_SET(fd, &write);
224 size = ( int(fd+1) > size) ? int(fd+1) : size;
225 }
226
228 {
229#ifndef WIN32 // windows fd are not int's and don't start at 0 - this won't work in windows
230 resip_assert( fd < (int)FD_SETSIZE ); // redefinitn FD_SETSIZE will not work
231#else
232 resip_assert(except.fd_count < FD_SETSIZE); // Ensure there is room to add new FD
233#endif
234 FD_SET(fd,&except);
235 size = ( int(fd+1) > size) ? int(fd+1) : size;
236 }
237
238
239 void clear(Socket fd)
240 {
241 FD_CLR(fd, &read);
242 FD_CLR(fd, &write);
243 FD_CLR(fd, &except);
244 }
245
246 void reset()
247 {
248 size = 0;
249 numReady = 0;
250 FD_ZERO(&read);
251 FD_ZERO(&write);
252 FD_ZERO(&except);
253 }
254
255 // Make this stuff public for async dns/ares to use
259 int size;
260 int numReady; // set after each select call
261};
262
263
264}
265
266#endif
267
268/* ====================================================================
269 * The Vovida Software License, Version 1.0
270 *
271 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
272 *
273 * Redistribution and use in source and binary forms, with or without
274 * modification, are permitted provided that the following conditions
275 * are met:
276 *
277 * 1. Redistributions of source code must retain the above copyright
278 * notice, this list of conditions and the following disclaimer.
279 *
280 * 2. Redistributions in binary form must reproduce the above copyright
281 * notice, this list of conditions and the following disclaimer in
282 * the documentation and/or other materials provided with the
283 * distribution.
284 *
285 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
286 * and "Vovida Open Communication Application Library (VOCAL)" must
287 * not be used to endorse or promote products derived from this
288 * software without prior written permission. For written
289 * permission, please contact vocal@vovida.org.
290 *
291 * 4. Products derived from this software may not be called "VOCAL", nor
292 * may "VOCAL" appear in their name, without prior written
293 * permission of Vovida Networks, Inc.
294 *
295 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
296 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
297 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
298 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
299 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
300 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
301 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
302 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
303 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
304 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
306 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
307 * DAMAGE.
308 *
309 * ====================================================================
310 *
311 * This software consists of voluntary contributions made by Vovida
312 * Networks, Inc. and many individuals on behalf of Vovida Networks,
313 * Inc. For more information on Vovida Networks, Inc., please see
314 * <http://www.vovida.org/>.
315 *
316 * vi: set shiftwidth=3 expandtab:
317 */
#define resip_assert(x)
This file needs to work for both C and C++ code.
int Socket
Object-oriented wrapper for your platform's file-descriptor set.
Definition Socket.hxx:162
fd_set except
Definition Socket.hxx:258
void clear(Socket fd)
Definition Socket.hxx:239
fd_set read
Definition Socket.hxx:256
bool readyToWrite(Socket fd)
Definition Socket.hxx:194
bool readyToRead(Socket fd)
Definition Socket.hxx:189
void setRead(Socket fd)
Definition Socket.hxx:204
void reset()
Definition Socket.hxx:246
void setExcept(Socket fd)
Definition Socket.hxx:227
int selectMilliSeconds(unsigned long ms)
Definition Socket.hxx:181
fd_set write
Definition Socket.hxx:257
bool hasException(Socket fd)
Definition Socket.hxx:199
int select(struct timeval &tv)
Definition Socket.hxx:176
int select()
Definition Socket.hxx:171
void setWrite(Socket fd)
Definition Socket.hxx:216
This file is used to handle compatibility fixes/tweaks so reSIProcate can function on multiple platfo...
dcm! – add UnusedChecking(_enum) below;
bool makeSocketBlocking(Socket fd)
Definition Socket.cxx:43
void(* AfterSocketCreationFuncPtr)(Socket s, int transportType, const char *file, int line)
Definition Socket.hxx:107
int getSocketError(Socket fd)
Definition Socket.cxx:148
int getSocketDscp(Socket fd, IpVersion version)
The DSCP class of {fd}, which is getSocketTos() without the ECN bits.
Definition Socket.cxx:342
int increaseLimitFds(unsigned int targetFds)
Returns negative on error, or number of (positive) allowed fds.
Definition Socket.cxx:161
bool configureConnectedSocket(Socket fd)
Definition Socket.cxx:66
void initNetwork()
set up network - does nothing in unix but needed for windows
Definition Socket.cxx:83
int Socket
Definition Socket.hxx:94
int closeSocket(Socket fd)
Definition Socket.cxx:134
int setSocketDscp(Socket fd, int dscp, IpVersion version)
Sets the DSCP class of {fd}, in the IPv4 ToS byte or the IPv6 traffic class, and reads it back.
Definition Socket.cxx:292
int setSocketRcvBufLen(Socket fd, int buflen)
Definition Socket.cxx:244
int getErrno()
Definition Socket.hxx:97
int getSocketTos(Socket fd, IpVersion version)
Reads the IPv4 ToS byte, or the IPv6 traffic class, of {fd}.
Definition Socket.cxx:322
IpVersion
An enumeration of IP versions.
bool makeSocketNonBlocking(Socket fd)
Definition Socket.cxx:21
void list()
Definition userAdmin.cxx:66