reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
compat.hxx
Go to the documentation of this file.
1#if !defined(RESIP_COMPAT_HXX)
2#define RESIP_COMPAT_HXX
3
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#else
7#define GPERF_SIZE_TYPE size_t
8#endif
9
10
17#if defined(__INTEL_COMPILER ) && defined( __OPTIMIZE__ )
18# undef __OPTIMIZE__ // weird intel bug with ntohs and htons macros
19#endif
20
34#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
35# define RESIP_LAMBDA_CAPTURE_ALL_AND_THIS =, this
36#else
37# define RESIP_LAMBDA_CAPTURE_ALL_AND_THIS =
38#endif
39
40//#if defined(HAVE_SYS_INT_TYPES_H)
41//#include <sys/int_types.h>
42//#endif
43
44#include <cstring>
45#include <string>
46
47#ifndef WIN32
48# include <netdb.h>
49# include <sys/types.h>
50# include <sys/time.h>
51# include <sys/socket.h>
52# include <sys/select.h>
53# include <netinet/in.h>
54# include <arpa/inet.h>
55# include <unistd.h>
56# include <pthread.h>
57# include <limits.h>
58#endif
59
60#ifdef WIN32
61#ifndef WIN32_LEAN_AND_MEAN
62#define WIN32_LEAN_AND_MEAN
63#endif
64# include <windows.h>
65# include <winsock2.h>
66# include <cstdint>
67#undef WIN32_LEAN_AND_MEAN
68# include <errno.h>
69# include <io.h>
70#ifdef UNDER_CE
71#include "wince/WceCompat.hxx"
72#endif // UNDER_CE
73
74#if defined(_MSC_VER) && _MSC_VER < 1900
75#include <stdio.h>
76#ifndef snprintf
77#define snprintf c99_snprintf
78
79inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
80{
81 int count = -1;
82
83 if (size != 0)
84 count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
85 if (count == -1)
86 count = _vscprintf(format, ap);
87
88 return count;
89}
90
91inline int c99_snprintf(char* str, size_t size, const char* format, ...)
92{
93 int count;
94 va_list ap;
95
97 count = c99_vsnprintf(str, size, format, ap);
98 va_end(ap);
99
100 return count;
101}
102#endif
103#endif // _MSC_VER
104
105#endif
106
107#define RESIP_MAX_SOCKADDR_SIZE 28
108
109#if defined(__APPLE__)
110 // .amr. If you get linker or type conflicts around UInt32, then use this define
111# if defined(RESIP_APPLE_USE_SYSTEM_TYPES)
112# include <TargetConditionals.h>
113# include <CoreServices/CoreServices.h>
114# endif
115# if !defined(MAC_OS_X_VERSION_MIN_REQUIRED) || MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_2
116 // you don't include the SDK or you're running 10.3 or above
117 // note: this will fail on 10.2 if you don't include the SDK
118# include <arpa/nameser_compat.h>
119# else
120 // you include the SDK and you're running Mac OS 10.2 or below
121 typedef int socklen_t;
122# endif
123# ifdef __MWERKS__ /* this is a <limits.h> bug filed with Apple, Radar# 3657629. */
124# ifndef __SCHAR_MAX__
125# define __SCHAR_MAX__ 127
126# endif
127# endif
128#endif
129
130#if defined(__SUNPRO_CC)
131# if defined(_TIME_T)
132 using std::time_t;
133# endif
134# include <time.h>
135# include <memory.h>
136# include <string.h>
137#endif
138
139#if !defined(T_NAPTR)
140# define T_NAPTR 35
141#endif
142
143#if !defined(T_SRV)
144# define T_SRV 33
145#endif
146
147#if !defined(T_AAAA)
148# define T_AAAA 28
149#endif
150
151#if !defined(T_A)
152# define T_A 1
153#endif
154
155#if defined( TARGET_OS_IPHONE )
156// TARGET_OS_IPHONE can be 0 or 1, so must also check the value
157#if TARGET_OS_IPHONE
158#define REQUIRE_SO_NOSIGPIPE
159#endif
160#endif
161
162//typedef struct { unsigned char octet[16]; } UInt128;
163
164namespace resip
165{
166
167#if defined(WIN32) || defined(__QNX__)
168#ifndef strcasecmp
169# define strcasecmp(a,b) _stricmp(a,b)
170#endif
171#ifndef strncasecmp
172# define strncasecmp(a,b,c) _strnicmp(a,b,c)
173#endif
174#endif
175
176#if defined(__QNX__) || defined(__sun) || defined(WIN32)
177 typedef unsigned int u_int32_t;
178#endif
179
180template<typename _Tp>
181inline const _Tp&
182resipMin(const _Tp& __a, const _Tp& __b)
183{
184 if (__b < __a)
185 {
186 return __b;
187 }
188
189 return __a;
190}
191
192template<typename _Tp>
193inline const _Tp&
194resipMax(const _Tp& __a, const _Tp& __b)
195{
196 if (__a < __b)
197 {
198 return __b;
199 }
200
201 return __a;
202}
203
204template<typename _Tp1, typename _Tp2>
205inline const _Tp1
206resipIntDiv(const _Tp1& __a, const _Tp2& __b)
207{
208 // .bwc. Divide-round-nearest without using any floating-point.
209 if(__a%__b > __b/2)
210 {
211 return __a/__b+1;
212 }
213
214 return __a/__b;
215}
216
217#if defined(WORDS_BIGENDIAN) || defined(_BIG_ENDIAN) || defined( __BIG_ENDIAN__ ) || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || defined(RESIP_BIG_ENDIAN)
218
219inline uint64_t
220ntoh64(const uint64_t input)
221{
222 return input;
223}
224
225inline uint64_t
226hton64(const uint64_t input)
227{
228 return input;
229}
230
231#else
232
233inline uint64_t
234ntoh64(const uint64_t input)
235{
236 uint64_t rval;
237 uint8_t *data = (uint8_t *)&rval;
238
239 data[0] = (uint8_t)((input >> 56) & 0xFF);
240 data[1] = (uint8_t)((input >> 48) & 0xFF);
241 data[2] = (uint8_t)((input >> 40) & 0xFF);
242 data[3] = (uint8_t)((input >> 32) & 0xFF);
243 data[4] = (uint8_t)((input >> 24) & 0xFF);
244 data[5] = (uint8_t)((input >> 16) & 0xFF);
245 data[6] = (uint8_t)((input >> 8) & 0xFF);
246 data[7] = (uint8_t)((input >> 0) & 0xFF);
247
248 return rval;
249}
250
251inline uint64_t
252hton64(const uint64_t input)
253{
254 return (ntoh64(input));
255}
256
257#endif
258
259#ifndef _MSC_VER
260// strerror_r has two incompatible signatures depending on feature-test macros
261// and the platform's libc. The GNU variant returns char* (the message, which
262// may not be 'buf'); the XSI/POSIX variant returns int (0 on success) and
263// writes the message into 'buf'. Let overload resolution pick the right one.
264inline const char* strErrorResult(char* result, char* /*buf*/)
265{
266 return result; // GNU variant
267}
268inline const char* strErrorResult(int result, char* buf)
269{
270 return (result == 0) ? buf : "Unknown error"; // XSI/POSIX variant
271}
272#endif
273
274inline std::string strError(int err)
275{
276 char buf[256];
277#ifdef _MSC_VER
278 strerror_s(buf, sizeof(buf), err);
279 return buf;
280#else
281 return strErrorResult(strerror_r(err, buf, sizeof(buf)), buf);
282#endif
283}
284
285}
286
287//template "levels; ie REASONABLE and COMPLETE
288//reasonable allows most things such as partial template specialization,
289//etc...like most compilers and VC++2003+.
290//COMPLETE would allow template metaprogramming, template< template< > > tricks,
291//etc...REASONABLE should always be defined when COMPLETE is defined.
292
293//#if !defined(__SUNPRO_CC) && !defined(__INTEL_COMPILER)
294#if !defined(__INTEL_COMPILER)
295# define REASONABLE_TEMPLATES
296#endif
297
298// .bwc. This is the only place we check for USE_IPV6 in a header file. This
299// code has no effect if USE_IPV6 is not set, so this should only kick in when
300// we're building the resip libs. If someone sets USE_IPV6 while building
301// against the resip libs, no resip header file will care.
302#ifdef USE_IPV6
303#ifndef IPPROTO_IPV6
304#if(_WIN32_WINNT >= 0x0501) // Some versions of the windows SDK define IPPROTO_IPV6 differently - always enable IP v6 if USE_IPV6 and _WIN32_WINNT >= 0x0501
305#define IPPROTO_IPV6 ::IPPROTO_IPV6
306#else
307#ifdef _MSC_VER
308#define __STR2__(x) #x
309#define __STR1__(x) __STR2__(x)
310#define __LOC__ __FILE__ "("__STR1__(__LINE__)"): "
311#pragma message (__LOC__ " IPv6 support requested, but IPPROTO_V6 undefined; this platform does not appear to support IPv6 ")
312#else
313#warning IPv6 support requested, but IPPROTO_IPV6 undefined; this platform does not appear to support IPv6
314#endif
315// .bwc. Don't do this; someone might have defined it for their own code.
316// #undef USE_IPV6
317#endif
318#endif
319#endif
320
321#ifndef IPPROTO_SCTP
322#define IPPROTO_SCTP 132
323#endif
324
325// !bwc! Some poking around seems to indicate that icc supports gcc's function
326// attributes, at least as far back as version 8. I have no idea what support is
327// like prior to that. As for SUNPRO, it uses gcc's frontend, so I would expect
328// gnu-c function attributes to work, but does it define __GNUC__?
329#if defined(__GNUC__) || (__INTEL_COMPILER > 800)
330#define RESIP_DEPRECATED(x) x __attribute__ ((deprecated))
331#elif defined(_MSC_VER)
332#define RESIP_DEPRECATED(x) __declspec(deprecated) x
333#else
334#define RESIP_DEPRECATED(x) x
335#endif
336
337// These used to live in resipfaststreams.hxx and may only be used there
338#if (defined(WIN32) || defined(_WIN32_WCE))
339
340#if (defined(_MSC_VER) && _MSC_VER >= 1400 )
341#define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) _snprintf_s(buffer,sizeofBuffer,_TRUNCATE,format,var1)
342#define LTOA(value,string,sizeofstring,radix) _ltoa_s(value,string,sizeofstring,radix)
343#define ULTOA(value,string,sizeofstring,radix) _ultoa_s(value,string,sizeofstring,radix)
344#define I64TOA(value,string,sizeofstring,radix) _i64toa_s(value,string,sizeofstring,radix)
345#define UI64TOA(value,string,sizeofstring,radix) _ui64toa_s(value,string,sizeofstring,radix)
346#define GCVT(val,num,buffer,buffersize) _gcvt_s(buffer,buffersize,val,num)
347#else
348#define _TRUNCATE -1
349#define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) _snprintf(buffer,count,format,var1)
350#define LTOA(value,string,sizeofstring,radix) _ltoa(value,string,radix)
351#define ULTOA(value,string,sizeofstring,radix) _ultoa(value,string,radix)
352#define I64TOA(value,string,sizeofstring,radix) _i64toa(value,string,radix)
353#define UI64TOA(value,string,sizeofstring,radix) _ui64toa(value,string,radix)
354#define GCVT(val,sigdigits,buffer,buffersize) _gcvt(val,sigdigits,buffer)
355#endif
356
357#else //non-windows
358#define _TRUNCATE -1
359#define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) snprintf(buffer,sizeofBuffer,format,var1)
360#define LTOA(l,buffer,bufferlen,radix) SNPRINTF_1(buffer,bufferlen,bufferlen,"%li",l)
361#define ULTOA(ul,buffer,bufferlen,radix) SNPRINTF_1(buffer,bufferlen,bufferlen,"%lu",ul)
362#define I64TOA(value,string,sizeofstring,radix) SNPRINTF_1(string,sizeofstring,sizeofstring,"%lli",value)
363#define UI64TOA(value,string,sizeofstring,radix) SNPRINTF_1(string,sizeofstring,sizeofstring,"%llu",value)
364#define GCVT(f,sigdigits,buffer,bufferlen) SNPRINTF_1(buffer,bufferlen,bufferlen,"%f",f)
365#define _CVTBUFSIZE 309+40
366#endif
367
368#endif
369
370/* ====================================================================
371 * The Vovida Software License, Version 1.0
372 *
373 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
374 *
375 * Redistribution and use in source and binary forms, with or without
376 * modification, are permitted provided that the following conditions
377 * are met:
378 *
379 * 1. Redistributions of source code must retain the above copyright
380 * notice, this list of conditions and the following disclaimer.
381 *
382 * 2. Redistributions in binary form must reproduce the above copyright
383 * notice, this list of conditions and the following disclaimer in
384 * the documentation and/or other materials provided with the
385 * distribution.
386 *
387 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
388 * and "Vovida Open Communication Application Library (VOCAL)" must
389 * not be used to endorse or promote products derived from this
390 * software without prior written permission. For written
391 * permission, please contact vocal@vovida.org.
392 *
393 * 4. Products derived from this software may not be called "VOCAL", nor
394 * may "VOCAL" appear in their name, without prior written
395 * permission of Vovida Networks, Inc.
396 *
397 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
398 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
399 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
400 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
401 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
402 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
403 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
404 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
405 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
406 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
407 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
408 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
409 * DAMAGE.
410 *
411 * ====================================================================
412 */
#define _TRUNCATE
Definition compat.hxx:358
dcm! – add UnusedChecking(_enum) below;
const _Tp & resipMin(const _Tp &__a, const _Tp &__b)
Definition compat.hxx:182
const char * strErrorResult(char *result, char *)
Definition compat.hxx:264
const _Tp1 resipIntDiv(const _Tp1 &__a, const _Tp2 &__b)
Definition compat.hxx:206
uint64_t ntoh64(const uint64_t input)
Definition compat.hxx:234
unsigned long long uint64_t
Definition Stun.hxx:39
uint64_t hton64(const uint64_t input)
Definition compat.hxx:252
std::string strError(int err)
Definition compat.hxx:274
const _Tp & resipMax(const _Tp &__a, const _Tp &__b)
Definition compat.hxx:194
unsigned char uint8_t
Definition Stun.hxx:29
unsigned __int64 uint64_t
Definition stdint.h:95
void list()
Definition userAdmin.cxx:66