1 /*---------------------------------------------------------------------------*/
2 /*                                                                           */
3 /* Host.h - Basic header file to provide cross-platform solutions via        */
4 /*                   macros, conditional compilation, etc.                   */
5 /*                                                                           */
6 /* Author : Mark Carrier (mark@carrierlabs.com)                              */
7 /*                                                                           */
8 /*---------------------------------------------------------------------------*/
9 /* Copyright (c) 2007 CarrierLabs, LLC.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  *
23  * 3. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * 4. The name "CarrierLabs" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    mark@carrierlabs.com.
30  *
31  * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
32  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL MARK CARRIER OR
35  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42  * OF THE POSSIBILITY OF SUCH DAMAGE.
43  *----------------------------------------------------------------------------*/
44 #ifndef __HOST_H__
45 #define __HOST_H__
46 
47 #include <limits.h>
48 
49 #ifdef __cplusplus
50 extern "C"
51 {
52 #endif
53 
54 /*---------------------------------------------------------------------------*/
55 /*                                                                           */
56 /* Type Definition Macros                                                    */
57 /*                                                                           */
58 /*---------------------------------------------------------------------------*/
59 #ifndef __WORDSIZE
60 /* Assume 32 */
61 #define __WORDSIZE 32
62 #endif
63 
64 #if defined(_LINUX) || defined(_DARWIN) || defined(_BSD)
65 	typedef unsigned char uint8;
66 	typedef char int8;
67 	typedef unsigned short uint16;
68 	typedef short int16;
69 	typedef unsigned int uint32;
70 	typedef int int32;
71 	typedef int SOCKET;
72 #endif
73 
74 #ifdef WIN32
75 	struct iovec
76 	{
77 		void *iov_base;
78 		size_t iov_len;
79 	};
80 
81 	typedef unsigned char uint8;
82 	typedef char int8;
83 	typedef unsigned short uint16;
84 	typedef short int16;
85 	typedef unsigned int uint32;
86 	typedef int int32;
87 #endif
88 
89 #ifdef WIN32
90 	typedef int socklen_t;
91 #endif
92 
93 #if defined(WIN32)
94 	typedef unsigned long long int uint64;
95 	typedef long long int int64;
96 #elif (__WORDSIZE == 32)
97 __extension__ typedef long long int int64;
98 __extension__ typedef unsigned long long int uint64;
99 #elif (__WORDSIZE == 64)
100 typedef unsigned long int uint64;
101 typedef long int int64;
102 #endif
103 
104 #ifdef WIN32
105 
106 #ifndef UINT8_MAX
107 #define UINT8_MAX (UCHAR_MAX)
108 #endif
109 #ifndef UINT16_MAX
110 #define UINT16_MAX (USHRT_MAX)
111 #endif
112 #ifndef UINT32_MAX
113 #define UINT32_MAX (ULONG_MAX)
114 #endif
115 
116 #if __WORDSIZE == 64
117 #define SIZE_MAX (18446744073709551615UL)
118 #else
119 #ifndef SIZE_MAX
120 #define SIZE_MAX (4294967295U)
121 #endif
122 #endif
123 #endif
124 
125 #if defined(WIN32)
126 #define ssize_t size_t
127 #endif
128 
129 #ifndef TRUE
130 #define TRUE 1
131 #endif
132 
133 #ifndef FALSE
134 #define FALSE 0
135 #endif
136 
137 #ifndef htonll
138 #ifdef _BIG_ENDIAN
139 #define htonll(x) (x)
140 #define ntohll(x) (x)
141 #else
142 #define htonll(x) ((((uint64)htonl(x)) << 32) + htonl(x >> 32))
143 #define ntohll(x) ((((uint64)ntohl(x)) << 32) + ntohl(x >> 32))
144 #endif
145 #endif
146 
147 /*---------------------------------------------------------------------------*/
148 /*                                                                           */
149 /* Socket Macros                                                             */
150 /*                                                                           */
151 /*---------------------------------------------------------------------------*/
152 #ifdef WIN32
153 #define SHUT_RD 0
154 #define SHUT_WR 1
155 #define SHUT_RDWR 2
156 #define ACCEPT(a, b, c) accept(a, b, c)
157 #define CONNECT(a, b, c) connect(a, b, c)
158 #define CLOSE(a) closesocket(a)
159 #define READ(a, b, c) read(a, b, c)
160 #define RECV(a, b, c, d) recv(a, (char *)b, c, d)
161 #define RECVFROM(a, b, c, d, e, f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, (int *)f)
162 #define RECV_FLAGS MSG_WAITALL
163 #define SELECT(a, b, c, d, e) select((int32)a, b, c, d, e)
164 #define SEND(a, b, c, d) send(a, (const char *)b, (int)c, d)
165 #define SENDTO(a, b, c, d, e, f) sendto(a, (const char *)b, (int)c, d, e, f)
166 #define SEND_FLAGS 0
167 #define SENDFILE(a, b, c, d) sendfile(a, b, c, d)
168 #define SET_SOCKET_ERROR(x, y) errno = y
169 #define SOCKET_ERROR_INTERUPT EINTR
170 #define SOCKET_ERROR_TIMEDOUT EAGAIN
171 #define WRITE(a, b, c) write(a, b, c)
172 #define WRITEV(a, b, c) Writev(b, c)
173 #define GETSOCKOPT(a, b, c, d, e) getsockopt(a, b, c, (char *)d, (int *)e)
174 #define SETSOCKOPT(a, b, c, d, e) setsockopt(a, b, c, (char *)d, (int)e)
175 #define GETHOSTBYNAME(a) gethostbyname(a)
176 #endif
177 
178 #if defined(_LINUX) || defined(_DARWIN) || defined(_BSD)
179 #define ACCEPT(a, b, c) accept(a, b, c)
180 #define CONNECT(a, b, c) connect(a, b, c)
181 #define CLOSE(a) close(a)
182 #define READ(a, b, c) read(a, b, c)
183 #define RECV(a, b, c, d) recv(a, (void *)b, c, d)
184 #define RECVFROM(a, b, c, d, e, f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, f)
185 #define RECV_FLAGS MSG_WAITALL
186 #define SELECT(a, b, c, d, e) select(a, b, c, d, e)
187 #define SEND(a, b, c, d) send(a, (const int8 *)b, c, d)
188 #define SENDTO(a, b, c, d, e, f) sendto(a, (const int8 *)b, c, d, e, f)
189 #define SEND_FLAGS 0
190 #define SENDFILE(a, b, c, d) sendfile(a, b, c, d)
191 #define SET_SOCKET_ERROR(x, y) errno = y
192 #define SOCKET_ERROR_INTERUPT EINTR
193 #define SOCKET_ERROR_TIMEDOUT EAGAIN
194 #define WRITE(a, b, c) write(a, b, c)
195 #define WRITEV(a, b, c) writev(a, b, c)
196 #define GETSOCKOPT(a, b, c, d, e) getsockopt((int)a, (int)b, (int)c, (void *)d, (socklen_t *)e)
197 #define SETSOCKOPT(a, b, c, d, e) setsockopt((int)a, (int)b, (int)c, (const void *)d, (int)e)
198 #define GETHOSTBYNAME(a) gethostbyname(a)
199 #endif
200 
201 /*---------------------------------------------------------------------------*/
202 /*                                                                           */
203 /* File Macros                                                               */
204 /*                                                                           */
205 /*---------------------------------------------------------------------------*/
206 #define STRUCT_STAT struct stat
207 #define LSTAT(x, y) lstat(x, y)
208 #define FILE_HANDLE FILE *
209 #define CLEARERR(x) clearerr(x)
210 #define FCLOSE(x) fclose(x)
211 #define FEOF(x) feof(x)
212 #define FERROR(x) ferror(x)
213 #define FFLUSH(x) fflush(x)
214 #define FILENO(s) fileno(s)
215 #define FOPEN(x, y) fopen(x, y)
216 	//#define FREAD(a,b,c,d)      fread(a, b, c, d)
217 #define FSTAT(s, st) fstat(FILENO(s), st)
218 	//#define FWRITE(a,b,c,d)     fwrite(a, b, c, d)
219 #define STAT_BLK_SIZE(x) ((x).st_blksize)
220 
221 /*---------------------------------------------------------------------------*/
222 /*                                                                           */
223 /* Misc Macros                                                               */
224 /*                                                                           */
225 /*---------------------------------------------------------------------------*/
226 #if defined(WIN32)
227 #define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x)
228 #else
229 #define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)
230 #endif
231 
232 #if defined(WIN32)
233 #define STRTOULL(x) _atoi64(x)
234 #else
235 #define STRTOULL(x) strtoull(x, NULL, 10)
236 #endif
237 
238 #if defined(WIN32)
239 #define SNPRINTF _snprintf
240 #define PRINTF printf
241 #define VPRINTF vprintf
242 #define FPRINTF fprintf
243 #else
244 #define SNPRINTF snprintf
245 #define PRINTF printf
246 #define VPRINTF vprintf
247 #define FPRINTF fprintf
248 #endif
249 
250 #ifdef __cplusplus
251 }
252 #endif
253 
254 #endif /* __HOST_H__ */
255