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 */
26 #include "gcc-compat.h"
28 /** used in various contexts */
31 /** compute the minimum of \a a and \a b */
32 #define MIN(a,b) ((a) < (b) ? (a) : (b))
33 /** compute the maximum of \a a and \a b */
34 #define MAX(a,b) ((a) > (b) ? (a) : (b))
35 /** compute the absolute value of \a a */
36 #define ABS(a) ((a) > 0 ? (a) : -(a))
38 /** debug loglevel, gets really noisy */
40 /** still noisy, but won't fill your disk */
42 /** normal, but significant event */
44 /** unexpected event that can be handled */
46 /** unhandled error condition */
48 /** system might be unreliable */
50 /** last message before exit */
53 /** Log messages with lower priority than that will not be compiled in. */
54 #define COMPILE_TIME_LOGLEVEL 0
57 #if DEBUG > COMPILE_TIME_LOGLEVEL
58 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
60 #define DEBUG_LOG(...) do {;} while (0)
63 #if INFO > COMPILE_TIME_LOGLEVEL
64 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
66 #define INFO_LOG(...) do {;} while (0)
69 #if NOTICE > COMPILE_TIME_LOGLEVEL
70 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
72 #define NOTICE_LOG(...) do {;} while (0)
75 #if WARNING > COMPILE_TIME_LOGLEVEL
76 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
78 #define WARNING_LOG(...) do {;} while (0)
81 #if ERROR > COMPILE_TIME_LOGLEVEL
82 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
84 #define ERROR_LOG(...) do {;} while (0)
87 #if CRIT > COMPILE_TIME_LOGLEVEL
88 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
90 #define CRIT_LOG(...) do {;} while (0)
93 #if EMERG > COMPILE_TIME_LOGLEVEL
94 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
96 #define EMERG_LOG(...)
101 * define a standard log function that always writes to stderr
103 * \param loglevel_barrier If the loglevel of the current message
104 * is less than that, the message is going to be ignored.
107 #define INIT_STDERR_LOGGING(loglevel_barrier) \
108 __printf_2_3 void __log(int ll, const char* fmt,...) \
111 if (ll < loglevel_barrier) \
113 va_start(argp, fmt); \
114 vfprintf(stderr, fmt, argp); \
118 /** version text used by various commands if -V switch was given */
119 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \
120 "Copyright (C) 2008 Andre Noll\n" \
121 "This is free software with ABSOLUTELY NO WARRANTY." \
122 " See COPYING for details.\n" \
123 "Written by Andre Noll.\n" \
124 "Report bugs to <maan@systemlinux.org>.\n"
126 /** print out \p VERSION_TEXT and exit if version flag was given */
127 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
128 if (_args_info_struct.version_given) { \
129 printf("%s", VERSION_TEXT(_prefix)); \
130 exit(EXIT_SUCCESS); \
132 /** sent by para_server for commands that expect a data file */
133 #define AWAITING_DATA_MSG "\nAwaiting Data."
134 /** sent by para_server if authentication was successful */
135 #define PROCEED_MSG "\nProceed.\n"
136 /** length of the \p PROCEED_MSG string */
137 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
138 /** sent by para_client to indicate the end of the command line */
139 #define EOC_MSG "\nEnd of Command."
140 /** sent by para_client, followed by the decrypted challenge number */
141 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
144 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
147 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
148 long unsigned tv2ms(const struct timeval*);
149 void d2tv(double, struct timeval*);
150 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
151 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
152 void tv_divide(const unsigned long divisor, const struct timeval *tv,
153 struct timeval *result);
154 int tv_convex_combination(const long a, const struct timeval *tv1,
155 const long b, const struct timeval *tv2,
156 struct timeval *result);
157 void ms2tv(const long unsigned n, struct timeval *tv);
158 void compute_chunk_time(long unsigned chunk_num,
159 struct timeval *chunk_tv, struct timeval *stream_start,
160 struct timeval *result);
162 __printf_2_3 void __log(int, const char*, ...);
165 * Write a log message to a dynamically allocated string.
167 * \param fmt Usual format string.
168 * \param p Result pointer.
171 #define VSPRINTF(fmt, p) \
175 p = para_malloc(size); \
178 /* Try to print in the allocated space. */ \
180 n = vsnprintf(p, size, fmt, ap); \
182 /* If that worked, return the string. */ \
183 if (n > -1 && n < size) \
185 /* Else try again with more space. */ \
186 if (n > -1) /* glibc 2.1 */ \
187 size = n + 1; /* precisely what is needed */ \
188 else /* glibc 2.0 */ \
189 size *= 2; /* twice the old size */ \
190 p = para_realloc(p, size); \
195 * Return a random non-negative integer in an interval.
197 * \param max Determines maximal possible return value.
199 * \return An integer between zero and \p max - 1, inclusively.
201 static inline long int para_random(unsigned max)
203 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
206 /** Round up x to a multiple of y */
207 #define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y))
209 /** Get the size of an array */
210 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
213 * Wrapper for isspace.
217 * The values should be cast to an unsigned char first, then to int.
218 * Why? Because the isdigit (as do all other is/to functions/macros)
219 * expect a number from 0 upto and including 255 as their (int) argument.
220 * Because char is signed on most systems, casting it to int immediately
221 * gives the functions an argument between -128 and 127 (inclusive),
222 * which they will use as an array index, and which will thus fail
223 * horribly for characters which have their most significant bit set.
225 #define para_isspace(c) isspace((int)(unsigned char)(c))