1 /*
2  * ----------------------------------------------------------------
3  * Defines
4  * ----------------------------------------------------------------
5  * Copyright (C) 1997-2009 Jonas Kvinge
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * $Id: defines.h 54 2009-03-18 18:23:29Z jonasio $
21  *
22  */
23 
24 #ifdef DEFINES_H
25 #warning "defines.h already included."
26 #else
27 #define DEFINES_H
28 
29 struct hostent;
30 
31 /* DEFINES */
32 
33 #ifndef TRUE
34   #define TRUE			1
35 #endif
36 #ifndef FALSE
37   #define FALSE			0
38 #endif
39 
40 #define SUCCESS			0
41 #define ERROR			-1
42 
43 #define FOREVER			-1
44 #define NEVER			0
45 #define NONE			-1
46 
47 #define FD_NONE			-1
48 
49 #define HOST_UNRESOLVED		"<unresolved>"
50 
51 #define UNKNOWNUSER		"<unknown>"
52 #define UNKNOWNHOST		"<unknown>"
53 
54 #define SERVER_PORT_MIN		1
55 #define SERVER_PORT_MAX		1023
56 
57 #define CLIENT_PORT_MIN		1024
58 #define CLIENT_PORT_MAX		0xFFFF
59 
60 #define PASSWD_FILE		"/etc/passwd"
61 #define GROUP_FILE		"/etc/group"
62 
63 #define STDINMSGLEN		512
64 #define DATESTAMPLEN		12
65 #define TIMESTAMPLEN		10
66 #define DTSTAMPLEN		21
67 #define HOURLEN			2
68 #define MINLEN			2
69 #define SECLEN			2
70 #define STRDURATIONLEN		200
71 #define FILELEN			1024
72 #define USERLEN			40
73 #define PASSLEN			40
74 #define HOSTLEN			254
75 #define RECVBUFFERLEN		2048
76 #define SENDBUFFERLEN		2048
77 #define LINELEN			4000
78 #define TEXTLEN			150
79 #define SPACE10			"          "
80 #define SPACE15			"               "
81 #define SPACE20			"                    "
82 #define ONESECOND		1
83 #define ONEMINUTE		60
84 #define ONEHOUR			3600
85 #define ONEDAY			86400
86 #define FILECHARS		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.!@{}[]~:,"
87 #define STRHOSTCHARS		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.?*"
88 #define FILECHARSILLEGAL	"/\?%*:|\"<>"
89 
90 #define FAKELOOP for (FakeLoop = TRUE ; FakeLoop == TRUE ; FakeLoop = FALSE)
91 #define FOREVERLOOP while(TRUE)
92 
93 #ifndef __FILE__
94   #define __FILE__ "unknown"
95 #endif
96 #ifndef __LINE__
97   #define __LINE__ 0
98 #endif
99 #if 0
100   #define __FUNCTION__ "unknown"
101 #endif
102 
103 #ifndef INADDR_NONE
104   #define INADDR_NONE 0xffffffff
105 #endif
106 
107 #ifndef MSG_NOSIGNAL
108   #define MSG_NOSIGNAL 0
109 #endif
110 
111 #define PRINT_SIZE_T		signed long int
112 #define PRINT_PID_T		unsigned long int
113 #define PRINT_TIME_T		signed long int
114 #define PRINT_UID_T		unsigned long int
115 #define PRINT_GID_T		unsigned long int
116 
117 #if WIN32
118   #define LINEFEED "\r\n"
119 #else
120   #define LINEFEED "\n"
121 #endif
122 
123 #if !HAVE_SIZE_T
124   typedef int size_t;
125 #endif
126 
127 #if !HAVE_PID_T
128   typedef int pid_t;
129 #endif
130 
131 #if !HAVE_UID_T
132   typedef long uid_t;
133 #endif
134 
135 #if !HAVE_GID_T
136   typedef long gid_t;
137 #endif
138 
139 #if !HAVE_SOCKLEN_T
140   typedef size_t socklen_t;
141 #endif
142 
143 #if !HAVE_SETEUID
144   #define seteuid setuid
145 #endif
146 #if !HAVE_SETEGID
147   #define setegid setgid
148 #endif
149 
150 #ifndef accept_addrlen_type
151 #if OS_BSD || OS_LINUX || OS_WIN32 || WIN32
152   #define accept_addrlen_type socklen_t
153   #define getsockopt_optlen_type socklen_t
154   #define getsockname_namelen_type socklen_t
155   #define getpeername_namelen_type socklen_t
156 #elif OS_SOLARIS
157   #define accept_addrlen_type socklen_t
158   #define getsockopt_optlen_type int
159   #define getsockname_namelen_type socklen_t
160   #define getpeername_namelen_type socklen_t
161 #elif OS_AIX
162   #define accept_addrlen_type int
163   #define getsockopt_optlen_type int
164   #define getsockname_namelen_type socklen_t
165   #define getpeername_namelen_type socklen_t
166 #elif OS_HPUNIX
167   #define accept_addrlen_type socklen_t
168   #define getsockopt_optlen_type socklen_t
169   #define getsockname_namelen_type socklen_t
170   #define getpeername_namelen_type socklen_t
171 #elif OS_OSF1
172   #define accept_addrlen_type int
173   #define getsockopt_optlen_type int
174   #define getsockname_namelen_type int
175   #define getpeername_namelen_type int
176 #else
177   #define accept_addrlen_type socklen_t
178   #define getsockopt_optlen_type socklen_t
179   #define getsockname_namelen_type socklen_t
180   #define getpeername_namelen_type socklen_t
181 #endif
182 #endif
183 
184 #if HAVE_SNPRINTF
185   #if !HAVE_DECL_SNPRINTF || OS_OSF1
186     int snprintf(char *str, size_t size, const char *format, ...);
187   #endif
188 #else
189   #warning "This system does not have snprintf(). Using sprintf() for snprintf()!"
190   #define snprintf(S, Z, F, ...) sprintf(S, F, ...)
191 #endif
192 
193 #if HAVE_VSNPRINTF
194   #if !HAVE_DECL_VSNPRINTF || OS_OSF1
195     int vsnprintf(char *str, size_t size, const char *format, va_list ap);
196   #endif
197 #else
198   #warning "This system does not have vsnprintf(). Using vsprintf() for vsnprintf()!"
199   #define vsnprintf(S, Z, F, ...) vsprintf(S, F, ...)
200 #endif
201 
202 #if !HAVE_INET_ATON
203   #if HAVE_INET_PTON
204     #define inet_aton(C, A) inet_pton(AF_INET, C, A)
205   #else
206     #define inet_aton nl_net_aton
207   #endif
208 #endif
209 
210 #ifndef NBLOCK_SYSV
211   #ifndef NBLOCK_BSD
212     #ifndef NBLOCK_POSIX
213       #define NBLOCK_POSIX 1
214     #endif
215   #endif
216 #endif
217 
218 #if ARES
219   #if defined(ARES_VERSION)
220     #if ARES_VERSION >= 0x010500
221       #define HAVE_CARES_CALLBACK_TIMEOUTS 1
222     #endif
223   #endif
224   #if HAVE_ARES_VERSION_H
225     #define res_strerror(X) ares_strerror(X)
226   #else
227     #define res_strerror(X) ares_strerror(X, NULL)
228   #endif
229 #else
230   typedef void (*ares_host_callback)(void *arg, int status,  int timeouts, struct hostent *hostent);
231   #define res_strerror(X) strerror(X)
232 #endif
233 
234 #if ARES && defined(MAIN_C) && !HAVE_ARES_CANCELQUERY
235   #warning "Your version of ares does not have ares_cancelquery()."
236 #endif
237 
238 /* MACROS */
239 
240 #undef FREE
241 #define FREE(x) \
242                 free((x)); \
243                 (x) = NULL
244 
245 #ifndef DEBUGPRINT
246   #if DEBUG
247     #define DEBUGPRINT(X, ...) if (X & BITMASK_DEBUG_OPTIONS) debugprint(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
248   #else
249     #define DEBUGPRINT(X, ...)
250   #endif
251 #endif
252 
253 #define Seconds(x)		(((x) / ONESECOND) % ONEMINUTE)
254 #define Minutes(x)		(((x) / ONEMINUTE) % ONEMINUTE)
255 #define Hours(x)		(((x) / ONEHOUR) % 24)
256 #define Days(x)			(((x) / ONEDAY))
257 #define OneOrMore(x)		(((x) == 1) ? "" : "s")
258 
259 #define StrMovePastToken(S, T)	\
260 				S = strchr(S, T); \
261 				if (S != NULL) { \
262 				  *S = '\0'; \
263 				  ++S; \
264 				  while (S[0] == T) { ++S; } \
265 				}
266 
267 #define StrPrintErrorIfNull(S, F, L, M) \
268 				if ((S == NULL) || (S[0] == '\0')) { \
269 				  sysprint(BITMASK_ERROR, M, F, L); \
270 				  continue; \
271 				} \
272 				while ((S[0] == ' ') || (S[0] == '\t')) { ++S; }
273 
274 /* VARAIBLES */
275 
276 #if defined(LIBRARY)
277   #define NOW time(NULL)
278 #else
279   #if defined(MAIN_C) || defined(MKPASSWD_C)
280     time_t NOW = 0;
281     unsigned short int FakeLoop = TRUE;
282   #else
283     extern time_t NOW;
284     extern unsigned short int FakeLoop;
285   #endif
286 #endif
287 
288 
289 /* AERRNO DEFINES */
290 
291 #define AESUCCESS					0
292 #define AEINVALID					1
293 #define AEEXISTS					2
294 #define AEMALLOC					3
295 #define AENOMATCH					4
296 #define AENORESOURCE					5
297 #define AEINPROGRESS					6
298 
299 
300 /* HOST DEFINES */
301 
302 #define HOSTRESOLVING					1
303 #define HOSTTOIP					2
304 #define HOSTIPTONAME					4
305 #define HOSTNAMETOIP					8
306 #define HOSTRESOLVED					16
307 
308 
309 /* HOST MACROS */
310 
311 #define Host_SetResolving(x)				((x) |= HOSTRESOLVING)
312 #define Host_SetHostToIP(x)				((x) |= HOSTTOIP)
313 #define Host_SetHostIPToName(x)				((x) |= HOSTIPTONAME)
314 #define Host_SetHostNameToIP(x)				((x) |= HOSTNAMETOIP)
315 #define Host_SetResolved(x)				((x) |= HOSTRESOLVED)
316 
317 #define Host_ClearResolving(x)				((x) &= ~HOSTRESOLVING)
318 #define Host_ClearHostToIP(x)				((x) &= ~HOSTTOIP)
319 #define Host_ClearHostIPToName(x)			((x) &= ~HOSTIPTONAME)
320 #define Host_ClearHostNameToIP(x)			((x) &= ~HOSTNAMETOIP)
321 #define Host_ClearResolved(x)				((x) &= ~HOSTRESOLVED)
322 
323 #define Host_IsResolving(x)				((x) & HOSTRESOLVING)
324 #define Host_IsHostToIP(x)				((x) & HOSTTOIP)
325 #define Host_IsHostIPToName(x)				((x) & HOSTIPTONAME)
326 #define Host_IsHostNameToIP(x)				((x) & HOSTNAMETOIP)
327 #define Host_IsResolved(x)				((x) & HOSTRESOLVED)
328 
329 #endif
330