2 * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file para.h global paraslash definitions */
16 #include <time.h> /* time(), localtime() */
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 */
28 #include "gcc-compat.h"
30 /** used in various contexts */
33 /** Compute the minimum of \a x and \a y. */
34 #define PARA_MIN(x, y) ({ \
35 typeof(x) _min1 = (x); \
36 typeof(y) _min2 = (y); \
37 (void) (&_min1 == &_min2); \
38 _min1 < _min2 ? _min1 : _min2; })
40 /** Compute the maximum of \a x and \a y. */
41 #define PARA_MAX(x, y) ({ \
42 typeof(x) _max1 = (x); \
43 typeof(y) _max2 = (y); \
44 (void) (&_max1 == &_max2); \
45 _max1 < _max2 ? _max2 : _max1; })
47 /** Compute the absolute value of \a x. */
48 #define PARA_ABS(x) ({ \
53 * Define a standard log function that always writes to stderr.
55 * \param loglevel_barrier If the loglevel of the current message
56 * is less than that, the message is going to be ignored.
59 #define INIT_STDERR_LOGGING(loglevel_barrier) \
60 __printf_2_3 void para_log(int ll, const char* fmt,...) \
63 if (ll < loglevel_barrier) \
65 va_start(argp, fmt); \
66 vfprintf(stderr, fmt, argp); \
70 /** Sent by para_client to initiate the authentication procedure. */
71 #define AUTH_REQUEST_MSG "auth rsa "
72 /** Sent by para_server for commands that expect a data file. */
73 #define AWAITING_DATA_MSG "\nAwaiting Data."
74 /** Sent by para_server if authentication was successful. */
75 #define PROCEED_MSG "Proceed."
76 /** Length of the \p PROCEED_MSG string. */
77 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
78 /** Sent by para_client to indicate the end of the command line. */
79 #define EOC_MSG "\nEnd of Command."
82 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
85 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
86 long unsigned tv2ms(const struct timeval*);
87 void d2tv(double, struct timeval*);
88 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
89 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
90 void tv_divide(const unsigned long divisor, const struct timeval *tv,
91 struct timeval *result);
92 int tv_convex_combination(const long a, const struct timeval *tv1,
93 const long b, const struct timeval *tv2,
94 struct timeval *result);
95 void ms2tv(const long unsigned n, struct timeval *tv);
96 void compute_chunk_time(long unsigned chunk_num,
97 struct timeval *chunk_tv, struct timeval *stream_start,
98 struct timeval *result);
100 /** The enum of all status items. */
101 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
102 extern const char *status_item_list[];
103 /** Loop over each status item. */
104 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
105 int for_each_stat_item(char *item_buf, size_t num_bytes,
106 int (*item_handler)(int, char *));
108 __printf_2_3 void para_log(int, const char*, ...);
111 * Write a log message to a dynamically allocated string.
113 * \param fmt Usual format string.
114 * \param p Result pointer.
117 #define PARA_VSPRINTF(fmt, p) \
121 p = para_malloc(size); \
124 /* Try to print in the allocated space. */ \
126 n = vsnprintf(p, size, fmt, ap); \
128 /* If that worked, return the string. */ \
129 if (n > -1 && n < size) \
131 /* Else try again with more space. */ \
132 if (n > -1) /* glibc 2.1 */ \
133 size = n + 1; /* precisely what is needed */ \
134 else /* glibc 2.0 */ \
135 size *= 2; /* twice the old size */ \
136 p = para_realloc(p, size); \
141 * Return a random non-negative integer in an interval.
143 * \param max Determines maximal possible return value.
145 * \return An integer between zero and \p max - 1, inclusively.
147 _static_inline_ long int para_random(unsigned max)
149 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
152 /** Round up x to next multiple of y. */
153 #define ROUND_UP(x, y) ({ \
154 const typeof(y) _divisor = y; \
155 ((x) + _divisor - 1) / _divisor * _divisor; })
157 /** Round down x to multiple of y. */
158 #define ROUND_DOWN(x, y) ({ \
159 const typeof(y) _divisor = y; \
160 (x) / _divisor * _divisor; })
162 /** Divide and round up to next integer. */
163 #define DIV_ROUND_UP(x, y) ({ \
164 typeof(y) _divisor = y; \
165 ((x) + _divisor - 1) / _divisor; })
168 * Assert a build-time dependency, as an expression.
170 * \param cond The compile-time condition which must be true.
172 * Compilation will fail if the condition isn't true, or can't be evaluated by
173 * the compiler. This can be used in an expression: its value is "0".
177 #define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2 * !(cond)]) - 1)
179 /** &a[0] degrades to a pointer: a different type from an array */
180 #define _array_size_chk(arr) EXPR_BUILD_ASSERT(\
181 !__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0])))
182 /** Get the size of an array */
183 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
186 * Wrapper for isspace.
190 * The values should be cast to an unsigned char first, then to int.
191 * Why? Because the isdigit (as do all other is/to functions/macros)
192 * expect a number from 0 upto and including 255 as their (int) argument.
193 * Because char is signed on most systems, casting it to int immediately
194 * gives the functions an argument between -128 and 127 (inclusive),
195 * which they will use as an array index, and which will thus fail
196 * horribly for characters which have their most significant bit set.
198 #define para_isspace(c) isspace((int)(unsigned char)(c))
200 /** Data that indicates an eof-condition for a fec-encoded stream. */
201 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
202 "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
203 /** The number of bytes of the \a FEC_EOF_PACKET. */
204 #define FEC_EOF_PACKET_LEN 32
206 /** Used to avoid a shortcoming in vim's syntax highlighting. */
207 #define EMBRACE(...) { __VA_ARGS__}
209 /** A nice cup of STFU for Mr gcc. */
210 #define do_nothing do {/* nothing */} while (0)
213 * The sample formats supported by paraslash.
215 * It may be determined by one of the following sources:
217 * 1. The decoding filter (para_audiod only). In this case, it is always
218 * \p SF_S16_LE which is the canonical format used within decoders.
220 * 2. The wav header (para_write only).
222 * 3. The --format option of para_write.
224 #define SAMPLE_FORMATS \
225 SAMPLE_FORMAT(SF_S8, "8 bit signed"), \
226 SAMPLE_FORMAT(SF_U8, "8 bit unsigned"), \
227 SAMPLE_FORMAT(SF_S16_LE, "16 bit signed, little endian"), \
228 SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
229 SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
230 SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
233 #define SAMPLE_FORMAT(a, b) a
234 enum sample_format {SAMPLE_FORMATS};
236 #define SAMPLE_FORMAT(a, b) b
239 /** Debug loglevel, gets really noisy. */
241 /** Still noisy, but won't fill your disk. */
243 /** Normal, but significant event. */
245 /** Unexpected event that can be handled. */
247 /** Unhandled error condition. */
249 /** System might be unreliable. */
251 /** Last message before exit. */
253 /** Number of all loglevels. */
254 #define NUM_LOGLEVELS 7
256 /** Log messages with lower priority than that will not be compiled in. */
257 #define COMPILE_TIME_LOGLEVEL 0
260 #if LL_DEBUG >= COMPILE_TIME_LOGLEVEL
261 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
263 #define PARA_DEBUG_LOG(...) do {;} while (0)
266 #if LL_INFO >= COMPILE_TIME_LOGLEVEL
267 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
269 #define PARA_INFO_LOG(...) do {;} while (0)
272 #if LL_NOTICE >= COMPILE_TIME_LOGLEVEL
273 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
275 #define PARA_NOTICE_LOG(...) do {;} while (0)
278 #if LL_WARNING >= COMPILE_TIME_LOGLEVEL
279 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
281 #define PARA_WARNING_LOG(...) do {;} while (0)
284 #if LL_ERROR >= COMPILE_TIME_LOGLEVEL
285 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
287 #define PARA_ERROR_LOG(...) do {;} while (0)
290 #if LL_CRIT >= COMPILE_TIME_LOGLEVEL
291 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
293 #define PARA_CRIT_LOG(...) do {;} while (0)
296 #if LL_EMERG >= COMPILE_TIME_LOGLEVEL
297 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
299 #define PARA_EMERG_LOG(...)