recv.h: Cosmetics.
[paraslash.git] / para.h
1 /*
2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file para.h global paraslash definitions */
8
9 #include "config.h"
10
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/wait.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h> /* time(), localtime() */
17 #include <unistd.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <ctype.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25 #include <sys/un.h> /* needed by create_pf_socket */
26 #include <string.h>
27 #include <assert.h>
28 #include "gcc-compat.h"
29
30 /** used in various contexts */
31 #define MAXLINE 255
32
33 /** compute the minimum of \a a and \a b */
34 #define PARA_MIN(a,b) ((a) < (b) ? (a) : (b))
35 /** compute the maximum of \a a and \a b */
36 #define PARA_MAX(a,b) ((a) > (b) ? (a) : (b))
37 /** compute the absolute value of \a a */
38 #define PARA_ABS(a) ((a) > 0 ? (a) : -(a))
39
40 /** debug loglevel, gets really noisy */
41 #define DEBUG 1
42 /** still noisy, but won't fill your disk */
43 #define INFO 2
44 /** normal, but significant event */
45 #define NOTICE 3
46 /** unexpected event that can be handled */
47 #define WARNING 4
48 /** unhandled error condition */
49 #define ERROR 5
50 /** system might be unreliable */
51 #define CRIT 6
52 /** last message before exit */
53 #define EMERG 7
54
55 /** Log messages with lower priority than that will not be compiled in. */
56 #define COMPILE_TIME_LOGLEVEL 0
57
58 /** \cond */
59 #if DEBUG > COMPILE_TIME_LOGLEVEL
60 #define PARA_DEBUG_LOG(f,...) para_log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
61 #else
62 #define PARA_DEBUG_LOG(...) do {;} while (0)
63 #endif
64
65 #if INFO > COMPILE_TIME_LOGLEVEL
66 #define PARA_INFO_LOG(f,...) para_log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
67 #else
68 #define PARA_INFO_LOG(...) do {;} while (0)
69 #endif
70
71 #if NOTICE > COMPILE_TIME_LOGLEVEL
72 #define PARA_NOTICE_LOG(f,...) para_log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
73 #else
74 #define PARA_NOTICE_LOG(...) do {;} while (0)
75 #endif
76
77 #if WARNING > COMPILE_TIME_LOGLEVEL
78 #define PARA_WARNING_LOG(f,...) para_log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
79 #else
80 #define PARA_WARNING_LOG(...) do {;} while (0)
81 #endif
82
83 #if ERROR > COMPILE_TIME_LOGLEVEL
84 #define PARA_ERROR_LOG(f,...) para_log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
85 #else
86 #define PARA_ERROR_LOG(...) do {;} while (0)
87 #endif
88
89 #if CRIT > COMPILE_TIME_LOGLEVEL
90 #define PARA_CRIT_LOG(f,...) para_log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
91 #else
92 #define PARA_CRIT_LOG(...) do {;} while (0)
93 #endif
94
95 #if EMERG > COMPILE_TIME_LOGLEVEL
96 #define PARA_EMERG_LOG(f,...) para_log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
97 #else
98 #define PARA_EMERG_LOG(...)
99 #endif
100 /** \endcond */
101
102 /**
103 * define a standard log function that always writes to stderr
104 *
105 * \param loglevel_barrier If the loglevel of the current message
106 * is less than that, the message is going to be ignored.
107 *
108 */
109 #define INIT_STDERR_LOGGING(loglevel_barrier) \
110 __printf_2_3 void para_log(int ll, const char* fmt,...) \
111 { \
112 va_list argp; \
113 if (ll < loglevel_barrier) \
114 return; \
115 va_start(argp, fmt); \
116 vfprintf(stderr, fmt, argp); \
117 va_end(argp); \
118 }
119
120 /** version text used by various commands if -V switch was given */
121 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \
122 "Copyright (C) 2008 Andre Noll\n" \
123 "This is free software with ABSOLUTELY NO WARRANTY." \
124 " See COPYING for details.\n" \
125 "Written by Andre Noll.\n" \
126 "Report bugs to <maan@systemlinux.org>.\n"
127
128 /** print out \p VERSION_TEXT and exit if version flag was given */
129 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
130 if (_args_info_struct.version_given) { \
131 printf("%s", VERSION_TEXT(_prefix)); \
132 exit(EXIT_SUCCESS); \
133 }
134 /** sent by para_server for commands that expect a data file */
135 #define AWAITING_DATA_MSG "\nAwaiting Data."
136 /** sent by para_server if authentication was successful */
137 #define PROCEED_MSG "\nProceed.\n"
138 /** length of the \p PROCEED_MSG string */
139 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
140 /** sent by para_client to indicate the end of the command line */
141 #define EOC_MSG "\nEnd of Command."
142 /** sent by para_client, followed by the decrypted challenge number */
143 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
144
145 /* exec */
146 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
147
148 /* time */
149 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
150 long unsigned tv2ms(const struct timeval*);
151 void d2tv(double, struct timeval*);
152 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
153 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
154 void tv_divide(const unsigned long divisor, const struct timeval *tv,
155 struct timeval *result);
156 int tv_convex_combination(const long a, const struct timeval *tv1,
157 const long b, const struct timeval *tv2,
158 struct timeval *result);
159 void ms2tv(const long unsigned n, struct timeval *tv);
160 void compute_chunk_time(long unsigned chunk_num,
161 struct timeval *chunk_tv, struct timeval *stream_start,
162 struct timeval *result);
163
164 /** The enum of all status items. */
165 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
166 extern const char *status_item_list[];
167 /** Loop over each status item. */
168 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
169 int stat_item_valid(const char *item);
170 int stat_line_valid(const char *);
171 void stat_client_write(const char *msg, int itemnum);
172 int stat_client_add(int fd, long unsigned mask);
173
174 __printf_2_3 void para_log(int, const char*, ...);
175
176 /**
177 * Write a log message to a dynamically allocated string.
178 *
179 * \param fmt Usual format string.
180 * \param p Result pointer.
181 *
182 * \sa printf(3). */
183 #define PARA_VSPRINTF(fmt, p) \
184 { \
185 int n; \
186 size_t size = 100; \
187 p = para_malloc(size); \
188 while (1) { \
189 va_list ap; \
190 /* Try to print in the allocated space. */ \
191 va_start(ap, fmt); \
192 n = vsnprintf(p, size, fmt, ap); \
193 va_end(ap); \
194 /* If that worked, return the string. */ \
195 if (n > -1 && n < size) \
196 break; \
197 /* Else try again with more space. */ \
198 if (n > -1) /* glibc 2.1 */ \
199 size = n + 1; /* precisely what is needed */ \
200 else /* glibc 2.0 */ \
201 size *= 2; /* twice the old size */ \
202 p = para_realloc(p, size); \
203 } \
204 }
205
206 /**
207 * Return a random non-negative integer in an interval.
208 *
209 * \param max Determines maximal possible return value.
210 *
211 * \return An integer between zero and \p max - 1, inclusively.
212 */
213 static inline long int para_random(unsigned max)
214 {
215 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
216 }
217
218 /** Round up x to a multiple of y */
219 #define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y))
220
221 /** Get the size of an array */
222 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
223
224 /**
225 * Wrapper for isspace.
226 * NetBSD needs this.
227 */
228 /*
229 * The values should be cast to an unsigned char first, then to int.
230 * Why? Because the isdigit (as do all other is/to functions/macros)
231 * expect a number from 0 upto and including 255 as their (int) argument.
232 * Because char is signed on most systems, casting it to int immediately
233 * gives the functions an argument between -128 and 127 (inclusive),
234 * which they will use as an array index, and which will thus fail
235 * horribly for characters which have their most significant bit set.
236 */
237 #define para_isspace(c) isspace((int)(unsigned char)(c))