2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file para.h global paraslash definitions */
14 #include <time.h> /* time(), localtime() */
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <sys/un.h> /* needed by create_pf_socket */
27 #include "gcc-compat.h"
29 /** used in various contexts */
32 /** compute the minimum of \a a and \a b */
33 #define MIN(a,b) ((a) < (b) ? (a) : (b))
34 /** compute the maximum of \a a and \a b */
35 #define MAX(a,b) ((a) > (b) ? (a) : (b))
36 /** compute the absolute value of \a a */
37 #define ABS(a) ((a) > 0 ? (a) : -(a))
39 /** debug loglevel, gets really noisy */
41 /** still noisy, but won't fill your disk */
43 /** normal, but significant event */
45 /** unexpected event that can be handled */
47 /** unhandled error condition */
49 /** system might be unreliable */
51 /** last message before exit */
54 /** Log messages with lower priority than that will not be compiled in. */
55 #define COMPILE_TIME_LOGLEVEL 0
58 #if DEBUG > COMPILE_TIME_LOGLEVEL
59 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
61 #define DEBUG_LOG(...) do {;} while (0)
64 #if INFO > COMPILE_TIME_LOGLEVEL
65 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
67 #define INFO_LOG(...) do {;} while (0)
70 #if NOTICE > COMPILE_TIME_LOGLEVEL
71 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
73 #define NOTICE_LOG(...) do {;} while (0)
76 #if WARNING > COMPILE_TIME_LOGLEVEL
77 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
79 #define WARNING_LOG(...) do {;} while (0)
82 #if ERROR > COMPILE_TIME_LOGLEVEL
83 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
85 #define ERROR_LOG(...) do {;} while (0)
88 #if CRIT > COMPILE_TIME_LOGLEVEL
89 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
91 #define CRIT_LOG(...) do {;} while (0)
94 #if EMERG > COMPILE_TIME_LOGLEVEL
95 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
97 #define EMERG_LOG(...)
102 * define a standard log function that always writes to stderr
104 * \param loglevel_barrier If the loglevel of the current message
105 * is less than that, the message is going to be ignored.
108 #define INIT_STDERR_LOGGING(loglevel_barrier) \
109 __printf_2_3 void __log(int ll, const char* fmt,...) \
112 if (ll < loglevel_barrier) \
114 va_start(argp, fmt); \
115 vfprintf(stderr, fmt, argp); \
119 /** version text used by various commands if -V switch was given */
120 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \
121 "Copyright (C) 2008 Andre Noll\n" \
122 "This is free software with ABSOLUTELY NO WARRANTY." \
123 " See COPYING for details.\n" \
124 "Written by Andre Noll.\n" \
125 "Report bugs to <maan@systemlinux.org>.\n"
127 /** print out \p VERSION_TEXT and exit if version flag was given */
128 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
129 if (_args_info_struct.version_given) { \
130 printf("%s", VERSION_TEXT(_prefix)); \
131 exit(EXIT_SUCCESS); \
133 /** sent by para_server for commands that expect a data file */
134 #define AWAITING_DATA_MSG "\nAwaiting Data."
135 /** sent by para_server if authentication was successful */
136 #define PROCEED_MSG "\nProceed.\n"
137 /** length of the \p PROCEED_MSG string */
138 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
139 /** sent by para_client to indicate the end of the command line */
140 #define EOC_MSG "\nEnd of Command."
141 /** sent by para_client, followed by the decrypted challenge number */
142 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
145 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
148 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
149 long unsigned tv2ms(const struct timeval*);
150 void d2tv(double, struct timeval*);
151 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
152 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
153 void tv_divide(const unsigned long divisor, const struct timeval *tv,
154 struct timeval *result);
155 int tv_convex_combination(const long a, const struct timeval *tv1,
156 const long b, const struct timeval *tv2,
157 struct timeval *result);
158 void ms2tv(const long unsigned n, struct timeval *tv);
159 void compute_chunk_time(long unsigned chunk_num,
160 struct timeval *chunk_tv, struct timeval *stream_start,
161 struct timeval *result);
163 __printf_2_3 void __log(int, const char*, ...);
166 * Write a log message to a dynamically allocated string.
168 * \param fmt Usual format string.
169 * \param p Result pointer.
172 #define VSPRINTF(fmt, p) \
176 p = para_malloc(size); \
179 /* Try to print in the allocated space. */ \
181 n = vsnprintf(p, size, fmt, ap); \
183 /* If that worked, return the string. */ \
184 if (n > -1 && n < size) \
186 /* Else try again with more space. */ \
187 if (n > -1) /* glibc 2.1 */ \
188 size = n + 1; /* precisely what is needed */ \
189 else /* glibc 2.0 */ \
190 size *= 2; /* twice the old size */ \
191 p = para_realloc(p, size); \
196 * Return a random non-negative integer in an interval.
198 * \param max Determines maximal possible return value.
200 * \return An integer between zero and \p max - 1, inclusively.
202 static inline long int para_random(unsigned max)
204 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
207 /** Round up x to a multiple of y */
208 #define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y))
210 /** Get the size of an array */
211 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
214 * Wrapper for isspace.
218 * The values should be cast to an unsigned char first, then to int.
219 * Why? Because the isdigit (as do all other is/to functions/macros)
220 * expect a number from 0 upto and including 255 as their (int) argument.
221 * Because char is signed on most systems, casting it to int immediately
222 * gives the functions an argument between -128 and 127 (inclusive),
223 * which they will use as an array index, and which will thus fail
224 * horribly for characters which have their most significant bit set.
226 #define para_isspace(c) isspace((int)(unsigned char)(c))