]> git.tuebingen.mpg.de Git - paraslash.git/blob - para.h
client: Remove sb-compatibility code.
[paraslash.git] / para.h
1 /*
2  * Copyright (C) 1997-2013 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 <stdbool.h>
29 #include "gcc-compat.h"
30
31 /** used in various contexts */
32 #define MAXLINE 255
33
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; })
40
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; })
47
48 /** Compute the absolute value of \a x. */
49 #define PARA_ABS(x) ({ \
50         typeof(x) _x = (x); \
51         _x > 0? _x : -_x; })
52
53
54 extern __printf_2_3 void (*para_log)(int, const char*, ...);
55 /**
56  * Define a standard log function that always writes to stderr.
57  *
58  * \param funcname The name of the function to be defined.
59  *
60  * \param loglevel_barrier If the loglevel of the current message
61  * is less than that, the message is going to be ignored.
62  *
63  */
64 #define DEFINE_STDERR_LOGGER(funcname, loglevel_barrier) \
65         __printf_2_3 void funcname(int ll, const char* fmt,...) \
66         { \
67                 va_list argp; \
68                 if (ll < loglevel_barrier) \
69                         return; \
70                 va_start(argp, fmt); \
71                 vfprintf(stderr, fmt, argp); \
72                 va_end(argp); \
73         }
74 /**
75  * Define the standard log function and activate it.
76  *
77  * \param loglevel_barrier See \ref DEFINE_STDERR_LOGGER.
78  */
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;
82
83 /** Sent by para_client to initiate the authentication procedure. */
84 #define AUTH_REQUEST_MSG "auth rsa "
85
86 /* exec */
87 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
88
89 /* time */
90 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
91 long unsigned tv2ms(const struct timeval*);
92 void d2tv(double, struct timeval*);
93 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
94 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
95 void tv_divide(const unsigned long divisor, const struct timeval *tv,
96         struct timeval *result);
97 int tv_convex_combination(const long a, const struct timeval *tv1,
98                 const long b, const struct timeval *tv2,
99                 struct timeval *result);
100 void ms2tv(const long unsigned n, struct timeval *tv);
101 void compute_chunk_time(long unsigned chunk_num,
102                 struct timeval *chunk_tv, struct timeval *stream_start,
103                 struct timeval *result);
104
105 /** The enum of all status items. */
106 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
107 extern const char *status_item_list[];
108 /** Loop over each status item. */
109 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
110 int for_each_stat_item(char *item_buf, size_t num_bytes,
111         int (*item_handler)(int, char *));
112
113
114 /**
115  * Return a random non-negative integer in an interval.
116  *
117  * \param max Determines maximal possible return value.
118  *
119  * \return An integer between zero and \p max - 1, inclusively.
120  */
121 _static_inline_ long int para_random(unsigned max)
122 {
123         return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
124 }
125
126 /**
127  * Simple sanity check for I/O vectors.
128  *
129  * \param iov Pointer to the I/O vector to check.
130  *
131  * \return True if \a iov points to a non-empty buffer.
132  */
133 _static_inline_ bool iov_valid(const struct iovec *iov)
134 {
135         return iov && iov->iov_len > 0 && iov->iov_base;
136 }
137
138 /** Round up x to next multiple of y. */
139 #define ROUND_UP(x, y) ({ \
140         const typeof(y) _divisor = y; \
141         ((x) + _divisor - 1) / _divisor * _divisor; })
142
143 /** Round down x to multiple of y. */
144 #define ROUND_DOWN(x, y) ({ \
145         const typeof(y) _divisor = y; \
146         (x) / _divisor * _divisor; })
147
148 /** Divide and round up to next integer. */
149 #define DIV_ROUND_UP(x, y) ({ \
150         typeof(y) _divisor = y; \
151         ((x) + _divisor - 1) / _divisor; })
152
153 /**
154  * Assert a build-time dependency, as an expression.
155  *
156  * \param cond The compile-time condition which must be true.
157  *
158  * Compilation will fail if the condition isn't true, or can't be evaluated by
159  * the compiler. This can be used in an expression: its value is "0".
160  *
161  * Taken from ccan.
162  */
163 #define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2 * !(cond)]) - 1)
164
165 /** &a[0] degrades to a pointer: a different type from an array */
166 #define _array_size_chk(arr) EXPR_BUILD_ASSERT(\
167         !__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0])))
168 /** Get the size of an array */
169 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
170
171 /**
172  * Wrapper for isspace.
173  * NetBSD needs this.
174  */
175 /*
176  * The values should be cast to an unsigned char first, then to int.
177  * Why? Because the isdigit (as do all other is/to functions/macros)
178  * expect a number from 0 upto and including 255 as their (int) argument.
179  * Because char is signed on most systems, casting it to int immediately
180  * gives the functions an argument between -128 and 127 (inclusive),
181  * which they will use as an array index, and which will thus fail
182  * horribly for characters which have their most significant bit set.
183  */
184 #define para_isspace(c) isspace((int)(unsigned char)(c))
185
186 /** Data that indicates an eof-condition for a fec-encoded stream. */
187 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
188         "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
189 /** The number of bytes of the \a FEC_EOF_PACKET. */
190 #define FEC_EOF_PACKET_LEN 32
191
192 /** Used to avoid a shortcoming in vim's syntax highlighting. */
193 #define EMBRACE(...) { __VA_ARGS__}
194
195 /** A nice cup of STFU for Mr gcc. */
196 #define do_nothing do {/* nothing */} while (0)
197
198 /**
199  * The sample formats supported by paraslash.
200  *
201  * It may be determined by one of the following sources:
202  *
203  *      1. The decoding filter (para_audiod only). In this case, it is always
204  *      \p SF_S16_LE which is the canonical format used within decoders.
205  *
206  *      2. The wav header (para_write only).
207  *
208  *      3. The --format option of para_write.
209  */
210 #define SAMPLE_FORMATS \
211         SAMPLE_FORMAT(SF_S8, "8 bit signed"), \
212         SAMPLE_FORMAT(SF_U8, "8 bit unsigned"), \
213         SAMPLE_FORMAT(SF_S16_LE, "16 bit signed, little endian"), \
214         SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
215         SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
216         SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
217
218 /** \cond sample_format */
219 #define SAMPLE_FORMAT(a, b) a
220 enum sample_format {SAMPLE_FORMATS};
221 #undef SAMPLE_FORMAT
222 #define SAMPLE_FORMAT(a, b) b
223 /** \endcond sample_format */
224
225 /** Debug loglevel, gets really noisy. */
226 #define LL_DEBUG 0
227 /** Still noisy, but won't fill your disk. */
228 #define LL_INFO  1
229 /** Normal, but significant event. */
230 #define LL_NOTICE 2
231 /** Unexpected event that can be handled. */
232 #define LL_WARNING 3
233 /** Unhandled error condition. */
234 #define LL_ERROR 4
235 /** System might be unreliable. */
236 #define LL_CRIT 5
237 /** Last message before exit. */
238 #define LL_EMERG 6
239 /** Number of all loglevels. */
240 #define NUM_LOGLEVELS 7
241
242 /** Log messages with lower priority than that will not be compiled in. */
243 #define COMPILE_TIME_LOGLEVEL 0
244
245 /** \cond log */
246 #if LL_DEBUG >= COMPILE_TIME_LOGLEVEL
247 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
248 #else
249 #define PARA_DEBUG_LOG(...) do {;} while (0)
250 #endif
251
252 #if LL_INFO >= COMPILE_TIME_LOGLEVEL
253 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
254 #else
255 #define PARA_INFO_LOG(...) do {;} while (0)
256 #endif
257
258 #if LL_NOTICE >= COMPILE_TIME_LOGLEVEL
259 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
260 #else
261 #define PARA_NOTICE_LOG(...) do {;} while (0)
262 #endif
263
264 #if LL_WARNING >= COMPILE_TIME_LOGLEVEL
265 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ##  __VA_ARGS__)
266 #else
267 #define PARA_WARNING_LOG(...) do {;} while (0)
268 #endif
269
270 #if LL_ERROR >= COMPILE_TIME_LOGLEVEL
271 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
272 #else
273 #define PARA_ERROR_LOG(...) do {;} while (0)
274 #endif
275
276 #if LL_CRIT >= COMPILE_TIME_LOGLEVEL
277 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
278 #else
279 #define PARA_CRIT_LOG(...) do {;} while (0)
280 #endif
281
282 #if LL_EMERG >= COMPILE_TIME_LOGLEVEL
283 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
284 #else
285 #define PARA_EMERG_LOG(...)
286 #endif
287 /** \endcond log */