handle_connect(): Don't send anything to non-authorized clients.
[paraslash.git] / para.h
1 /*
2  * Copyright (C) 1997-2011 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 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; })
39
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; })
46
47 /** Compute the absolute value of \a x. */
48 #define PARA_ABS(x) ({ \
49         typeof(x) _x = (x); \
50         _x > 0? _x : -_x; })
51
52 /**
53  * Define a standard log function that always writes to stderr.
54  *
55  * \param loglevel_barrier If the loglevel of the current message
56  * is less than that, the message is going to be ignored.
57  *
58  */
59 #define INIT_STDERR_LOGGING(loglevel_barrier) \
60         __printf_2_3 void para_log(int ll, const char* fmt,...) \
61         { \
62                 va_list argp; \
63                 if (ll < loglevel_barrier) \
64                         return; \
65                 va_start(argp, fmt); \
66                 vfprintf(stderr, fmt, argp); \
67                 va_end(argp); \
68         }
69
70 /** Version text used by various commands if -V switch was given. */
71 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION \
72         " (" GIT_VERSION ": " CODENAME ")" "\n" \
73         "Copyright (C) 2011 Andre Noll\n" \
74         "This is free software with ABSOLUTELY NO WARRANTY." \
75         " See COPYING for details.\n" \
76         "Written by Andre Noll.\n" \
77         "Report bugs to <maan@systemlinux.org>.\n"
78
79 /** Print out \p VERSION_TEXT and exit if version flag was given. */
80 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
81         if (_args_info_struct.version_given) { \
82                 printf("%s", VERSION_TEXT(_prefix)); \
83                 exit(EXIT_SUCCESS); \
84         }
85
86 /** Sent by para_client to initiate the authentication procedure. */
87 #define AUTH_REQUEST_MSG "auth rsa "
88 /** Sent by para_server for commands that expect a data file. */
89 #define AWAITING_DATA_MSG "\nAwaiting Data."
90 /** Sent by para_server if authentication was successful. */
91 #define PROCEED_MSG "Proceed."
92 /** Length of the \p PROCEED_MSG string. */
93 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
94 /** Sent by para_client to indicate the end of the command line. */
95 #define EOC_MSG "\nEnd of Command."
96
97 /* exec */
98 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
99
100 /* time */
101 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
102 long unsigned tv2ms(const struct timeval*);
103 void d2tv(double, struct timeval*);
104 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
105 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
106 void tv_divide(const unsigned long divisor, const struct timeval *tv,
107         struct timeval *result);
108 int tv_convex_combination(const long a, const struct timeval *tv1,
109                 const long b, const struct timeval *tv2,
110                 struct timeval *result);
111 void ms2tv(const long unsigned n, struct timeval *tv);
112 void compute_chunk_time(long unsigned chunk_num,
113                 struct timeval *chunk_tv, struct timeval *stream_start,
114                 struct timeval *result);
115
116 /** The enum of all status items. */
117 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
118 extern const char *status_item_list[];
119 /** Loop over each status item. */
120 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
121 int for_each_stat_item(char *item_buf, size_t num_bytes,
122         int (*item_handler)(int, char *));
123
124 __printf_2_3 void para_log(int, const char*, ...);
125
126 /**
127  * Write a log message to a dynamically allocated string.
128  *
129  * \param fmt Usual format string.
130  * \param p Result pointer.
131  *
132  * \sa printf(3). */
133 #define PARA_VSPRINTF(fmt, p) \
134 { \
135         int n; \
136         size_t size = 100; \
137         p = para_malloc(size); \
138         while (1) { \
139                 va_list ap; \
140                 /* Try to print in the allocated space. */ \
141                 va_start(ap, fmt); \
142                 n = vsnprintf(p, size, fmt, ap); \
143                 va_end(ap); \
144                 /* If that worked, return the string. */ \
145                 if (n > -1 && n < size) \
146                         break; \
147                 /* Else try again with more space. */ \
148                 if (n > -1) /* glibc 2.1 */ \
149                         size = n + 1; /* precisely what is needed */ \
150                 else /* glibc 2.0 */ \
151                         size *= 2; /* twice the old size */ \
152                 p = para_realloc(p, size); \
153         } \
154 }
155
156 /**
157  *  Return a random non-negative integer in an interval.
158  *
159  * \param max Determines maximal possible return value.
160  *
161  * \return An integer between zero and \p max - 1, inclusively.
162  */
163 _static_inline_ long int para_random(unsigned max)
164 {
165         return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
166 }
167
168 /** Round up x to next multiple of y. */
169 #define ROUND_UP(x, y) ({ \
170         const typeof(y) _divisor = y; \
171         ((x) + _divisor - 1) / _divisor * _divisor; })
172
173 /** Round down x to multiple of y. */
174 #define ROUND_DOWN(x, y) ({ \
175         const typeof(y) _divisor = y; \
176         (x) / _divisor * _divisor; })
177
178 /** Divide and round up to next integer. */
179 #define DIV_ROUND_UP(x, y) ({ \
180         typeof(y) _divisor = y; \
181         ((x) + _divisor - 1) / _divisor; })
182
183 /** Get the size of an array */
184 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
185
186 /**
187  * Wrapper for isspace.
188  * NetBSD needs this.
189  */
190 /*
191  * The values should be cast to an unsigned char first, then to int.
192  * Why? Because the isdigit (as do all other is/to functions/macros)
193  * expect a number from 0 upto and including 255 as their (int) argument.
194  * Because char is signed on most systems, casting it to int immediately
195  * gives the functions an argument between -128 and 127 (inclusive),
196  * which they will use as an array index, and which will thus fail
197  * horribly for characters which have their most significant bit set.
198  */
199 #define para_isspace(c) isspace((int)(unsigned char)(c))
200
201 /** Data that indicates an eof-condition for a fec-encoded stream. */
202 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
203         "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
204 #define FEC_EOF_PACKET_LEN 32
205
206 /** Used to avoid a shortcoming in vim's syntax highlighting. */
207 #define EMBRACE(...) { __VA_ARGS__}
208
209 /**
210  * The sample formats supported by paraslash.
211  *
212  * It may be determined by one of the following sources:
213  *
214  *      1. The decoding filter (para_audiod only). In this case, it is always
215  *      \p SF_S16_LE which is the canonical format used within decoders.
216  *
217  *      2. The wav header (para_write only).
218  *
219  *      3. The --format option of para_write.
220  */
221 #define SAMPLE_FORMATS \
222         SAMPLE_FORMAT(SF_S8, "8 bit signed"), \
223         SAMPLE_FORMAT(SF_U8, "8 bit unsigned"), \
224         SAMPLE_FORMAT(SF_S16_LE, "16 bit signed, little endian"), \
225         SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
226         SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
227         SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
228
229 #define SAMPLE_FORMAT(a, b) a
230 enum sample_format {SAMPLE_FORMATS};
231 #undef SAMPLE_FORMAT
232 #define SAMPLE_FORMAT(a, b) b
233
234 /** Debug loglevel, gets really noisy. */
235 #define LL_DEBUG 0
236 /** Still noisy, but won't fill your disk. */
237 #define LL_INFO  1
238 /** Normal, but significant event. */
239 #define LL_NOTICE 2
240 /** Unexpected event that can be handled. */
241 #define LL_WARNING 3
242 /** Unhandled error condition. */
243 #define LL_ERROR 4
244 /** System might be unreliable. */
245 #define LL_CRIT 5
246 /** Last message before exit. */
247 #define LL_EMERG 6
248 /** Number of all loglevels. */
249 #define NUM_LOGLEVELS 7
250
251 /** Log messages with lower priority than that will not be compiled in. */
252 #define COMPILE_TIME_LOGLEVEL 0
253
254 /** \cond */
255 #if LL_DEBUG >= COMPILE_TIME_LOGLEVEL
256 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
257 #else
258 #define PARA_DEBUG_LOG(...) do {;} while (0)
259 #endif
260
261 #if LL_INFO >= COMPILE_TIME_LOGLEVEL
262 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
263 #else
264 #define PARA_INFO_LOG(...) do {;} while (0)
265 #endif
266
267 #if LL_NOTICE >= COMPILE_TIME_LOGLEVEL
268 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
269 #else
270 #define PARA_NOTICE_LOG(...) do {;} while (0)
271 #endif
272
273 #if LL_WARNING >= COMPILE_TIME_LOGLEVEL
274 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ##  __VA_ARGS__)
275 #else
276 #define PARA_WARNING_LOG(...) do {;} while (0)
277 #endif
278
279 #if LL_ERROR >= COMPILE_TIME_LOGLEVEL
280 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
281 #else
282 #define PARA_ERROR_LOG(...) do {;} while (0)
283 #endif
284
285 #if LL_CRIT >= COMPILE_TIME_LOGLEVEL
286 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
287 #else
288 #define PARA_CRIT_LOG(...) do {;} while (0)
289 #endif
290
291 #if LL_EMERG >= COMPILE_TIME_LOGLEVEL
292 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
293 #else
294 #define PARA_EMERG_LOG(...)
295 #endif
296 /** \endcond */