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

Contents of /main/rutil/compat.hxx

Parent Directory Parent Directory | Revision Log Revision Log


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

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