2 * Copyright (C) 1997-2013 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 */
29 #include "gcc-compat.h"
31 /** used in various contexts */
34 /** Compute the minimum of \a x and \a y. */
35 #define PARA_MIN(x, y) ({ \
36 typeof(x) _min1 = (x); \
37 typeof(y) _min2 = (y); \
38 (void) (&_min1 == &_min2); \
39 _min1 < _min2 ? _min1 : _min2; })
41 /** Compute the maximum of \a x and \a y. */
42 #define PARA_MAX(x, y) ({ \
43 typeof(x) _max1 = (x); \
44 typeof(y) _max2 = (y); \
45 (void) (&_max1 == &_max2); \
46 _max1 < _max2 ? _max2 : _max1; })
48 /** Compute the absolute value of \a x. */
49 #define PARA_ABS(x) ({ \
54 extern __printf_2_3 void (*para_log)(int, const char*, ...);
56 * Define a standard log function that always writes to stderr.
58 * \param funcname The name of the function to be defined.
60 * \param loglevel_barrier If the loglevel of the current message
61 * is less than that, the message is going to be ignored.
64 #define DEFINE_STDERR_LOGGER(funcname, loglevel_barrier) \
65 __printf_2_3 void funcname(int ll, const char* fmt,...) \
68 if (ll < loglevel_barrier) \
70 va_start(argp, fmt); \
71 vfprintf(stderr, fmt, argp); \
75 * Define the standard log function and activate it.
77 * \param loglevel_barrier See \ref DEFINE_STDERR_LOGGER.
79 #define INIT_STDERR_LOGGING(loglevel_barrier) \
80 DEFINE_STDERR_LOGGER(stderr_log, loglevel_barrier); \
81 __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
83 /** Sent by para_client to initiate the authentication procedure. */
84 #define AUTH_REQUEST_MSG "auth rsa "
87 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
90 int tv_diff(const struct timeval *b, const struct timeval *a,
91 struct timeval *diff);
92 long unsigned tv2ms(const struct timeval *tv);
93 void d2tv(double x, struct timeval *tv);
94 void tv_add(const struct timeval *a, const struct timeval *b,
96 void tv_scale(const unsigned long mult, const struct timeval *tv,
97 struct timeval *result);
98 void tv_divide(const unsigned long divisor, const struct timeval *tv,
99 struct timeval *result);
100 int tv_convex_combination(const long a, const struct timeval *tv1,
101 const long b, const struct timeval *tv2,
102 struct timeval *result);
103 void ms2tv(long unsigned n, struct timeval *tv);
104 void compute_chunk_time(long unsigned chunk_num,
105 struct timeval *chunk_tv, struct timeval *stream_start,
106 struct timeval *result);
107 struct timeval *clock_get_realtime(struct timeval *tv);
109 /** The enum of all status items. */
110 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
111 extern const char *status_item_list[];
112 /** Loop over each status item. */
113 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
114 int for_each_stat_item(char *item_buf, size_t num_bytes,
115 int (*item_handler)(int, char *));
119 * Return a random non-negative integer in an interval.
121 * \param max Determines maximal possible return value.
123 * \return An integer between zero and \p max - 1, inclusively.
125 _static_inline_ long int para_random(unsigned max)
127 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
131 * Simple sanity check for I/O vectors.
133 * \param iov Pointer to the I/O vector to check.
135 * \return True if \a iov points to a non-empty buffer.
137 _static_inline_ bool iov_valid(const struct iovec *iov)
139 return iov && iov->iov_len > 0 && iov->iov_base;
142 /** Round up x to next multiple of y. */
143 #define ROUND_UP(x, y) ({ \
144 const typeof(y) _divisor = y; \
145 ((x) + _divisor - 1) / _divisor * _divisor; })
147 /** Round down x to multiple of y. */
148 #define ROUND_DOWN(x, y) ({ \
149 const typeof(y) _divisor = y; \
150 (x) / _divisor * _divisor; })
152 /** Divide and round up to next integer. */
153 #define DIV_ROUND_UP(x, y) ({ \
154 typeof(y) _divisor = y; \
155 ((x) + _divisor - 1) / _divisor; })
158 * Assert a build-time dependency, as an expression.
160 * \param cond The compile-time condition which must be true.
162 * Compilation will fail if the condition isn't true, or can't be evaluated by
163 * the compiler. This can be used in an expression: its value is "0".
167 #define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2 * !(cond)]) - 1)
169 /** &a[0] degrades to a pointer: a different type from an array */
170 #define _array_size_chk(arr) EXPR_BUILD_ASSERT(\
171 !__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0])))
172 /** Get the size of an array */
173 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
176 * Wrapper for isspace.
180 * The values should be cast to an unsigned char first, then to int.
181 * Why? Because the isdigit (as do all other is/to functions/macros)
182 * expect a number from 0 upto and including 255 as their (int) argument.
183 * Because char is signed on most systems, casting it to int immediately
184 * gives the functions an argument between -128 and 127 (inclusive),
185 * which they will use as an array index, and which will thus fail
186 * horribly for characters which have their most significant bit set.
188 #define para_isspace(c) isspace((int)(unsigned char)(c))
190 /** Data that indicates an eof-condition for a fec-encoded stream. */
191 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
192 "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
193 /** The number of bytes of the \a FEC_EOF_PACKET. */
194 #define FEC_EOF_PACKET_LEN 32
196 /** Used to avoid a shortcoming in vim's syntax highlighting. */
197 #define EMBRACE(...) { __VA_ARGS__}
199 /** A nice cup of STFU for Mr gcc. */
200 #define do_nothing do {/* nothing */} while (0)
203 * The sample formats supported by paraslash.
205 * It may be determined by one of the following sources:
207 * 1. The decoding filter (para_audiod only). In this case, it is always
208 * \p SF_S16_LE which is the canonical format used within decoders.
210 * 2. The wav header (para_write only).
212 * 3. The --format option of para_write.
214 #define SAMPLE_FORMATS \
215 SAMPLE_FORMAT(SF_S8, "8 bit signed"), \
216 SAMPLE_FORMAT(SF_U8, "8 bit unsigned"), \
217 SAMPLE_FORMAT(SF_S16_LE, "16 bit signed, little endian"), \
218 SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
219 SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
220 SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
222 /** \cond sample_format */
223 #define SAMPLE_FORMAT(a, b) a
224 enum sample_format {SAMPLE_FORMATS};
226 #define SAMPLE_FORMAT(a, b) b
227 /** \endcond sample_format */
229 /** Debug loglevel, gets really noisy. */
231 /** Still noisy, but won't fill your disk. */
233 /** Normal, but significant event. */
235 /** Unexpected event that can be handled. */
237 /** Unhandled error condition. */
239 /** System might be unreliable. */
241 /** Last message before exit. */
243 /** Number of all loglevels. */
244 #define NUM_LOGLEVELS 7
246 /** Log messages with lower priority than that will not be compiled in. */
247 #define COMPILE_TIME_LOGLEVEL 0
250 #if LL_DEBUG >= COMPILE_TIME_LOGLEVEL
251 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
253 #define PARA_DEBUG_LOG(...) do {;} while (0)
256 #if LL_INFO >= COMPILE_TIME_LOGLEVEL
257 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
259 #define PARA_INFO_LOG(...) do {;} while (0)
262 #if LL_NOTICE >= COMPILE_TIME_LOGLEVEL
263 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
265 #define PARA_NOTICE_LOG(...) do {;} while (0)
268 #if LL_WARNING >= COMPILE_TIME_LOGLEVEL
269 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
271 #define PARA_WARNING_LOG(...) do {;} while (0)
274 #if LL_ERROR >= COMPILE_TIME_LOGLEVEL
275 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
277 #define PARA_ERROR_LOG(...) do {;} while (0)
280 #if LL_CRIT >= COMPILE_TIME_LOGLEVEL
281 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
283 #define PARA_CRIT_LOG(...) do {;} while (0)
286 #if LL_EMERG >= COMPILE_TIME_LOGLEVEL
287 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
289 #define PARA_EMERG_LOG(...)