ee992acc8aff5b667f81a59d0b694552c8ab3f92
[adu.git] / adu.h
1 /*
2 * Copyright (C) 1997-2008 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 <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/wait.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h> /* time(), localtime() */
15 #include <unistd.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <ctype.h>
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 */
24 #include <string.h>
25 #include <assert.h>
26 #include "gcc-compat.h"
27
28 /** used in various contexts */
29 #define MAXLINE 255
30
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))
37
38 /** debug loglevel, gets really noisy */
39 #define DEBUG 1
40 /** still noisy, but won't fill your disk */
41 #define INFO 2
42 /** normal, but significant event */
43 #define NOTICE 3
44 /** unexpected event that can be handled */
45 #define WARNING 4
46 /** unhandled error condition */
47 #define ERROR 5
48 /** system might be unreliable */
49 #define CRIT 6
50 /** last message before exit */
51 #define EMERG 7
52
53 /** Log messages with lower priority than that will not be compiled in. */
54 #define COMPILE_TIME_LOGLEVEL 0
55
56 /** \cond */
57 #if DEBUG > COMPILE_TIME_LOGLEVEL
58 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
59 #else
60 #define DEBUG_LOG(...) do {;} while (0)
61 #endif
62
63 #if INFO > COMPILE_TIME_LOGLEVEL
64 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
65 #else
66 #define INFO_LOG(...) do {;} while (0)
67 #endif
68
69 #if NOTICE > COMPILE_TIME_LOGLEVEL
70 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
71 #else
72 #define NOTICE_LOG(...) do {;} while (0)
73 #endif
74
75 #if WARNING > COMPILE_TIME_LOGLEVEL
76 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
77 #else
78 #define WARNING_LOG(...) do {;} while (0)
79 #endif
80
81 #if ERROR > COMPILE_TIME_LOGLEVEL
82 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
83 #else
84 #define ERROR_LOG(...) do {;} while (0)
85 #endif
86
87 #if CRIT > COMPILE_TIME_LOGLEVEL
88 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
89 #else
90 #define CRIT_LOG(...) do {;} while (0)
91 #endif
92
93 #if EMERG > COMPILE_TIME_LOGLEVEL
94 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
95 #else
96 #define EMERG_LOG(...)
97 #endif
98 /** \endcond */
99
100 /**
101 * define a standard log function that always writes to stderr
102 *
103 * \param loglevel_barrier If the loglevel of the current message
104 * is less than that, the message is going to be ignored.
105 *
106 */
107 #define INIT_STDERR_LOGGING(loglevel_barrier) \
108 __printf_2_3 void __log(int ll, const char* fmt,...) \
109 { \
110 va_list argp; \
111 if (ll < loglevel_barrier) \
112 return; \
113 va_start(argp, fmt); \
114 vfprintf(stderr, fmt, argp); \
115 va_end(argp); \
116 }
117
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"
125
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); \
131 }
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:"
142
143 /* exec */
144 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
145
146 /* time */
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);
161
162 __printf_2_3 void __log(int, const char*, ...);
163
164 /**
165 * Write a log message to a dynamically allocated string.
166 *
167 * \param fmt Usual format string.
168 * \param p Result pointer.
169 *
170 * \sa printf(3). */
171 #define VSPRINTF(fmt, p) \
172 { \
173 int n; \
174 size_t size = 100; \
175 p = para_malloc(size); \
176 while (1) { \
177 va_list ap; \
178 /* Try to print in the allocated space. */ \
179 va_start(ap, fmt); \
180 n = vsnprintf(p, size, fmt, ap); \
181 va_end(ap); \
182 /* If that worked, return the string. */ \
183 if (n > -1 && n < size) \
184 break; \
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); \
191 } \
192 }
193
194 /**
195 * Return a random non-negative integer in an interval.
196 *
197 * \param max Determines maximal possible return value.
198 *
199 * \return An integer between zero and \p max - 1, inclusively.
200 */
201 static inline long int para_random(unsigned max)
202 {
203 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
204 }
205
206 /** Round up x to a multiple of y */
207 #define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y))
208
209 /** Get the size of an array */
210 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
211
212 /**
213 * Wrapper for isspace.
214 * NetBSD needs this.
215 */
216 /*
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.
224 */
225 #define para_isspace(c) isspace((int)(unsigned char)(c))