diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/crypto_random.c | 84 | ||||
-rw-r--r-- | src/util/getopt.c | 192 | ||||
-rw-r--r-- | src/util/getopt_helpers.c | 440 | ||||
-rw-r--r-- | src/util/gnunet-config.c | 195 | ||||
-rw-r--r-- | src/util/gnunet-scrypt.c | 213 | ||||
-rw-r--r-- | src/util/gnunet-uri.c | 84 | ||||
-rw-r--r-- | src/util/test_common_logging_dummy.c | 59 | ||||
-rw-r--r-- | src/util/tun.c | 163 |
8 files changed, 732 insertions, 698 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c index 8bb5f0587..71eaab87a 100644 --- a/src/util/crypto_random.c +++ b/src/util/crypto_random.c | |||
@@ -28,22 +28,23 @@ | |||
28 | #include "gnunet_crypto_lib.h" | 28 | #include "gnunet_crypto_lib.h" |
29 | #include <gcrypt.h> | 29 | #include <gcrypt.h> |
30 | 30 | ||
31 | #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__) | 31 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__) |
32 | 32 | ||
33 | #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-random", syscall) | 33 | #define LOG_STRERROR(kind, syscall) \ |
34 | GNUNET_log_from_strerror (kind, "util-crypto-random", syscall) | ||
34 | 35 | ||
35 | 36 | ||
36 | /* TODO: ndurner, move this to plibc? */ | 37 | /* TODO: ndurner, move this to plibc? */ |
37 | /* The code is derived from glibc, obviously */ | 38 | /* The code is derived from glibc, obviously */ |
38 | #if !HAVE_RANDOM || !HAVE_SRANDOM | 39 | #if ! HAVE_RANDOM || ! HAVE_SRANDOM |
39 | #ifdef RANDOM | 40 | #ifdef RANDOM |
40 | #undef RANDOM | 41 | #undef RANDOM |
41 | #endif | 42 | #endif |
42 | #ifdef SRANDOM | 43 | #ifdef SRANDOM |
43 | #undef SRANDOM | 44 | #undef SRANDOM |
44 | #endif | 45 | #endif |
45 | #define RANDOM() glibc_weak_rand32() | 46 | #define RANDOM() glibc_weak_rand32 () |
46 | #define SRANDOM(s) glibc_weak_srand32(s) | 47 | #define SRANDOM(s) glibc_weak_srand32 (s) |
47 | #if defined(RAND_MAX) | 48 | #if defined(RAND_MAX) |
48 | #undef RAND_MAX | 49 | #undef RAND_MAX |
49 | #endif | 50 | #endif |
@@ -105,17 +106,12 @@ GNUNET_CRYPTO_seed_weak_random (int32_t seed) | |||
105 | * @param length buffer length | 106 | * @param length buffer length |
106 | */ | 107 | */ |
107 | void | 108 | void |
108 | GNUNET_CRYPTO_zero_keys (void *buffer, | 109 | GNUNET_CRYPTO_zero_keys (void *buffer, size_t length) |
109 | size_t length) | ||
110 | { | 110 | { |
111 | #if HAVE_MEMSET_S | 111 | #if HAVE_MEMSET_S |
112 | memset_s (buffer, | 112 | memset_s (buffer, length, 0, length); |
113 | length, | ||
114 | 0, | ||
115 | length); | ||
116 | #elif HAVE_EXPLICIT_BZERO | 113 | #elif HAVE_EXPLICIT_BZERO |
117 | explicit_bzero (buffer, | 114 | explicit_bzero (buffer, length); |
118 | length); | ||
119 | #else | 115 | #else |
120 | volatile unsigned char *p = buffer; | 116 | volatile unsigned char *p = buffer; |
121 | while (length--) | 117 | while (length--) |
@@ -175,8 +171,7 @@ GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, | |||
175 | * @return a random value in the interval [0,i[. | 171 | * @return a random value in the interval [0,i[. |
176 | */ | 172 | */ |
177 | uint32_t | 173 | uint32_t |
178 | GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, | 174 | GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i) |
179 | uint32_t i) | ||
180 | { | 175 | { |
181 | #ifdef gcry_fast_random_poll | 176 | #ifdef gcry_fast_random_poll |
182 | static unsigned int invokeCount; | 177 | static unsigned int invokeCount; |
@@ -197,18 +192,17 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, | |||
197 | ul = UINT32_MAX - (UINT32_MAX % i); | 192 | ul = UINT32_MAX - (UINT32_MAX % i); |
198 | do | 193 | do |
199 | { | 194 | { |
200 | gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t), | 195 | gcry_randomize ((unsigned char *) &ret, |
196 | sizeof (uint32_t), | ||
201 | GCRY_STRONG_RANDOM); | 197 | GCRY_STRONG_RANDOM); |
202 | } | 198 | } while (ret >= ul); |
203 | while (ret >= ul); | ||
204 | return ret % i; | 199 | return ret % i; |
205 | case GNUNET_CRYPTO_QUALITY_NONCE: | 200 | case GNUNET_CRYPTO_QUALITY_NONCE: |
206 | ul = UINT32_MAX - (UINT32_MAX % i); | 201 | ul = UINT32_MAX - (UINT32_MAX % i); |
207 | do | 202 | do |
208 | { | 203 | { |
209 | gcry_create_nonce (&ret, sizeof (ret)); | 204 | gcry_create_nonce (&ret, sizeof (ret)); |
210 | } | 205 | } while (ret >= ul); |
211 | while (ret >= ul); | ||
212 | return ret % i; | 206 | return ret % i; |
213 | case GNUNET_CRYPTO_QUALITY_WEAK: | 207 | case GNUNET_CRYPTO_QUALITY_WEAK: |
214 | ret = i * get_weak_random (); | 208 | ret = i * get_weak_random (); |
@@ -231,8 +225,7 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, | |||
231 | * @return the permutation array (allocated from heap) | 225 | * @return the permutation array (allocated from heap) |
232 | */ | 226 | */ |
233 | unsigned int * | 227 | unsigned int * |
234 | GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, | 228 | GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n) |
235 | unsigned int n) | ||
236 | { | 229 | { |
237 | unsigned int *ret; | 230 | unsigned int *ret; |
238 | unsigned int i; | 231 | unsigned int i; |
@@ -262,8 +255,7 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, | |||
262 | * @return random 64-bit number | 255 | * @return random 64-bit number |
263 | */ | 256 | */ |
264 | uint64_t | 257 | uint64_t |
265 | GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, | 258 | GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max) |
266 | uint64_t max) | ||
267 | { | 259 | { |
268 | uint64_t ret; | 260 | uint64_t ret; |
269 | uint64_t ul; | 261 | uint64_t ul; |
@@ -275,18 +267,17 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, | |||
275 | ul = UINT64_MAX - (UINT64_MAX % max); | 267 | ul = UINT64_MAX - (UINT64_MAX % max); |
276 | do | 268 | do |
277 | { | 269 | { |
278 | gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t), | 270 | gcry_randomize ((unsigned char *) &ret, |
271 | sizeof (uint64_t), | ||
279 | GCRY_STRONG_RANDOM); | 272 | GCRY_STRONG_RANDOM); |
280 | } | 273 | } while (ret >= ul); |
281 | while (ret >= ul); | ||
282 | return ret % max; | 274 | return ret % max; |
283 | case GNUNET_CRYPTO_QUALITY_NONCE: | 275 | case GNUNET_CRYPTO_QUALITY_NONCE: |
284 | ul = UINT64_MAX - (UINT64_MAX % max); | 276 | ul = UINT64_MAX - (UINT64_MAX % max); |
285 | do | 277 | do |
286 | { | 278 | { |
287 | gcry_create_nonce (&ret, sizeof (ret)); | 279 | gcry_create_nonce (&ret, sizeof (ret)); |
288 | } | 280 | } while (ret >= ul); |
289 | while (ret >= ul); | ||
290 | 281 | ||
291 | return ret % max; | 282 | return ret % max; |
292 | case GNUNET_CRYPTO_QUALITY_WEAK: | 283 | case GNUNET_CRYPTO_QUALITY_WEAK: |
@@ -319,6 +310,7 @@ w_malloc (size_t n) | |||
319 | static int | 310 | static int |
320 | w_check (const void *p) | 311 | w_check (const void *p) |
321 | { | 312 | { |
313 | (void) p; | ||
322 | return 0; /* not secure memory */ | 314 | return 0; /* not secure memory */ |
323 | } | 315 | } |
324 | 316 | ||
@@ -326,50 +318,45 @@ w_check (const void *p) | |||
326 | /** | 318 | /** |
327 | * Initialize libgcrypt. | 319 | * Initialize libgcrypt. |
328 | */ | 320 | */ |
329 | void __attribute__ ((constructor)) | 321 | void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init () |
330 | GNUNET_CRYPTO_random_init () | ||
331 | { | 322 | { |
332 | gcry_error_t rc; | 323 | gcry_error_t rc; |
333 | 324 | ||
334 | if (! gcry_check_version (NEED_LIBGCRYPT_VERSION)) | 325 | if (! gcry_check_version (NEED_LIBGCRYPT_VERSION)) |
335 | { | 326 | { |
336 | FPRINTF (stderr, | 327 | FPRINTF ( |
337 | _("libgcrypt has not the expected version (version %s is required).\n"), | 328 | stderr, |
338 | NEED_LIBGCRYPT_VERSION); | 329 | _ ("libgcrypt has not the expected version (version %s is required).\n"), |
330 | NEED_LIBGCRYPT_VERSION); | ||
339 | GNUNET_assert (0); | 331 | GNUNET_assert (0); |
340 | } | 332 | } |
341 | /* set custom allocators */ | 333 | /* set custom allocators */ |
342 | gcry_set_allocation_handler (&w_malloc, | 334 | gcry_set_allocation_handler (&w_malloc, &w_malloc, &w_check, &realloc, &free); |
343 | &w_malloc, | ||
344 | &w_check, | ||
345 | &realloc, | ||
346 | &free); | ||
347 | /* Disable use of secure memory */ | 335 | /* Disable use of secure memory */ |
348 | if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0))) | 336 | if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0))) |
349 | FPRINTF (stderr, | 337 | FPRINTF (stderr, |
350 | "Failed to set libgcrypt option %s: %s\n", | 338 | "Failed to set libgcrypt option %s: %s\n", |
351 | "DISABLE_SECMEM", | 339 | "DISABLE_SECMEM", |
352 | gcry_strerror (rc)); | 340 | gcry_strerror (rc)); |
353 | /* Otherwise gnunet-ecc takes forever to complete, besides | 341 | /* Otherwise gnunet-ecc takes forever to complete, besides |
354 | we are fine with "just" using GCRY_STRONG_RANDOM */ | 342 | we are fine with "just" using GCRY_STRONG_RANDOM */ |
355 | if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0))) | 343 | if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0))) |
356 | FPRINTF (stderr, | 344 | FPRINTF (stderr, |
357 | "Failed to set libgcrypt option %s: %s\n", | 345 | "Failed to set libgcrypt option %s: %s\n", |
358 | "ENABLE_QUICK_RANDOM", | 346 | "ENABLE_QUICK_RANDOM", |
359 | gcry_strerror (rc)); | 347 | gcry_strerror (rc)); |
360 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); | 348 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); |
361 | gcry_fast_random_poll (); | 349 | gcry_fast_random_poll (); |
362 | GNUNET_CRYPTO_seed_weak_random (time (NULL) ^ | 350 | GNUNET_CRYPTO_seed_weak_random ( |
363 | GNUNET_CRYPTO_random_u32 | 351 | time (NULL) ^ |
364 | (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX)); | 352 | GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX)); |
365 | } | 353 | } |
366 | 354 | ||
367 | 355 | ||
368 | /** | 356 | /** |
369 | * Nicely shut down libgcrypt. | 357 | * Nicely shut down libgcrypt. |
370 | */ | 358 | */ |
371 | void __attribute__ ((destructor)) | 359 | void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini () |
372 | GNUNET_CRYPTO_random_fini () | ||
373 | { | 360 | { |
374 | gcry_set_progress_handler (NULL, NULL); | 361 | gcry_set_progress_handler (NULL, NULL); |
375 | #ifdef GCRYCTL_CLOSE_RANDOM_DEVICE | 362 | #ifdef GCRYCTL_CLOSE_RANDOM_DEVICE |
@@ -378,5 +365,4 @@ GNUNET_CRYPTO_random_fini () | |||
378 | } | 365 | } |
379 | 366 | ||
380 | 367 | ||
381 | |||
382 | /* end of crypto_random.c */ | 368 | /* end of crypto_random.c */ |
diff --git a/src/util/getopt.c b/src/util/getopt.c index 5e8571ee5..048f52ee0 100644 --- a/src/util/getopt.c +++ b/src/util/getopt.c | |||
@@ -47,14 +47,15 @@ Copyright (C) 2006, 2017 Christian Grothoff | |||
47 | #endif | 47 | #endif |
48 | #endif | 48 | #endif |
49 | 49 | ||
50 | #define LOG(kind,...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) | 50 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) |
51 | 51 | ||
52 | #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-getopt", syscall) | 52 | #define LOG_STRERROR(kind, syscall) \ |
53 | GNUNET_log_from_strerror (kind, "util-getopt", syscall) | ||
53 | 54 | ||
54 | #if defined (WIN32) && !defined (__CYGWIN32__) | 55 | #if defined(WIN32) && ! defined(__CYGWIN32__) |
55 | /* It's not Unix, really. See? Capital letters. */ | 56 | /* It's not Unix, really. See? Capital letters. */ |
56 | #include <windows.h> | 57 | #include <windows.h> |
57 | #define getpid() GetCurrentProcessId() | 58 | #define getpid() GetCurrentProcessId () |
58 | #endif | 59 | #endif |
59 | 60 | ||
60 | #ifndef _ | 61 | #ifndef _ |
@@ -62,9 +63,9 @@ Copyright (C) 2006, 2017 Christian Grothoff | |||
62 | When compiling libc, the _ macro is predefined. */ | 63 | When compiling libc, the _ macro is predefined. */ |
63 | #ifdef HAVE_LIBINTL_H | 64 | #ifdef HAVE_LIBINTL_H |
64 | #include <libintl.h> | 65 | #include <libintl.h> |
65 | #define _(msgid) gettext (msgid) | 66 | #define _(msgid) gettext (msgid) |
66 | #else | 67 | #else |
67 | #define _(msgid) (msgid) | 68 | #define _(msgid) (msgid) |
68 | #endif | 69 | #endif |
69 | #endif | 70 | #endif |
70 | 71 | ||
@@ -176,21 +177,18 @@ static char *nextchar; | |||
176 | of the value of `ordering'. In the case of RETURN_IN_ORDER, only | 177 | of the value of `ordering'. In the case of RETURN_IN_ORDER, only |
177 | `--' can cause `getopt' to return -1 with `GNoptind' != ARGC. */ | 178 | `--' can cause `getopt' to return -1 with `GNoptind' != ARGC. */ |
178 | 179 | ||
179 | static enum | 180 | static enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering; |
180 | { | ||
181 | REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER | ||
182 | } ordering; | ||
183 | 181 | ||
184 | /* Value of POSIXLY_CORRECT environment variable. */ | 182 | /* Value of POSIXLY_CORRECT environment variable. */ |
185 | static char *posixly_correct; | 183 | static char *posixly_correct; |
186 | 184 | ||
187 | #ifdef __GNU_LIBRARY__ | 185 | #ifdef __GNU_LIBRARY__ |
188 | /* We want to avoid inclusion of string.h with non-GNU libraries | 186 | /* We want to avoid inclusion of string.h with non-GNU libraries |
189 | because there are many ways it can cause trouble. | 187 | because there are many ways it can cause trouble. |
190 | On some systems, it contains special magic macros that don't work | 188 | On some systems, it contains special magic macros that don't work |
191 | in GCC. */ | 189 | in GCC. */ |
192 | #include <string.h> | 190 | #include <string.h> |
193 | #define my_index strchr | 191 | #define my_index strchr |
194 | #else | 192 | #else |
195 | 193 | ||
196 | /* Avoid depending on library functions or files | 194 | /* Avoid depending on library functions or files |
@@ -200,8 +198,7 @@ char * | |||
200 | getenv (); | 198 | getenv (); |
201 | 199 | ||
202 | static char * | 200 | static char * |
203 | my_index (const char *str, | 201 | my_index (const char *str, int chr) |
204 | int chr) | ||
205 | { | 202 | { |
206 | while (*str) | 203 | while (*str) |
207 | { | 204 | { |
@@ -217,7 +214,7 @@ my_index (const char *str, | |||
217 | #ifdef __GNUC__ | 214 | #ifdef __GNUC__ |
218 | /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. | 215 | /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. |
219 | That was relevant to code that was here before. */ | 216 | That was relevant to code that was here before. */ |
220 | #if !defined (__STDC__) || !__STDC__ | 217 | #if ! defined(__STDC__) || ! __STDC__ |
221 | /* gcc with -traditional declares the built-in strlen to return int, | 218 | /* gcc with -traditional declares the built-in strlen to return int, |
222 | and has done so at least since version 2.4.5. -- rms. */ | 219 | and has done so at least since version 2.4.5. -- rms. */ |
223 | extern int | 220 | extern int |
@@ -247,7 +244,7 @@ static int last_nonopt; | |||
247 | `first_nonopt' and `last_nonopt' are relocated so that they describe | 244 | `first_nonopt' and `last_nonopt' are relocated so that they describe |
248 | the new indices of the non-options in ARGV after they are moved. */ | 245 | the new indices of the non-options in ARGV after they are moved. */ |
249 | 246 | ||
250 | #if defined (__STDC__) && __STDC__ | 247 | #if defined(__STDC__) && __STDC__ |
251 | static void | 248 | static void |
252 | exchange (char **); | 249 | exchange (char **); |
253 | #endif | 250 | #endif |
@@ -311,14 +308,12 @@ exchange (char **argv) | |||
311 | 308 | ||
312 | /* Initialize the internal data when the first call is made. */ | 309 | /* Initialize the internal data when the first call is made. */ |
313 | 310 | ||
314 | #if defined (__STDC__) && __STDC__ | 311 | #if defined(__STDC__) && __STDC__ |
315 | static const char * | 312 | static const char * |
316 | _getopt_initialize (int, char *const *, const char *); | 313 | _getopt_initialize (int, char *const *, const char *); |
317 | #endif | 314 | #endif |
318 | static const char * | 315 | static const char * |
319 | _getopt_initialize (int argc, | 316 | _getopt_initialize (int argc, char *const *argv, const char *optstring) |
320 | char *const *argv, | ||
321 | const char *optstring) | ||
322 | { | 317 | { |
323 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 | 318 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 |
324 | * is the program name); the sequence of previously skipped | 319 | * is the program name); the sequence of previously skipped |
@@ -407,8 +402,11 @@ _getopt_initialize (int argc, | |||
407 | long-named options. */ | 402 | long-named options. */ |
408 | 403 | ||
409 | static int | 404 | static int |
410 | GN_getopt_internal (int argc, char *const *argv, const char *optstring, | 405 | GN_getopt_internal (int argc, |
411 | const struct GNoption *longopts, int *longind, | 406 | char *const *argv, |
407 | const char *optstring, | ||
408 | const struct GNoption *longopts, | ||
409 | int *longind, | ||
412 | int long_only) | 410 | int long_only) |
413 | { | 411 | { |
414 | static int __getopt_initialized = 0; | 412 | static int __getopt_initialized = 0; |
@@ -416,10 +414,10 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
416 | 414 | ||
417 | GNoptarg = NULL; | 415 | GNoptarg = NULL; |
418 | 416 | ||
419 | if (GNoptind == 0 || !__getopt_initialized) | 417 | if (GNoptind == 0 || ! __getopt_initialized) |
420 | { | 418 | { |
421 | if (GNoptind == 0) | 419 | if (GNoptind == 0) |
422 | GNoptind = 1; /* Don't scan ARGV[0], the program name. */ | 420 | GNoptind = 1; /* Don't scan ARGV[0], the program name. */ |
423 | optstring = _getopt_initialize (argc, argv, optstring); | 421 | optstring = _getopt_initialize (argc, argv, optstring); |
424 | __getopt_initialized = 1; | 422 | __getopt_initialized = 1; |
425 | } | 423 | } |
@@ -463,7 +461,7 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
463 | * Skip it like a null option, | 461 | * Skip it like a null option, |
464 | * then exchange with previous non-options as if it were an option, | 462 | * then exchange with previous non-options as if it were an option, |
465 | * then skip everything else like a non-option. */ | 463 | * then skip everything else like a non-option. */ |
466 | if (GNoptind != argc && !strcmp (argv[GNoptind], "--")) | 464 | if (GNoptind != argc && ! strcmp (argv[GNoptind], "--")) |
467 | { | 465 | { |
468 | GNoptind++; | 466 | GNoptind++; |
469 | 467 | ||
@@ -503,7 +501,7 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
503 | * Skip the initial punctuation. */ | 501 | * Skip the initial punctuation. */ |
504 | 502 | ||
505 | nextchar = | 503 | nextchar = |
506 | (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-')); | 504 | (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-')); |
507 | } | 505 | } |
508 | 506 | ||
509 | /* Decode the current option-ARGV-element. */ | 507 | /* Decode the current option-ARGV-element. */ |
@@ -524,7 +522,7 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
524 | if (longopts != NULL && | 522 | if (longopts != NULL && |
525 | (argv[GNoptind][1] == '-' || | 523 | (argv[GNoptind][1] == '-' || |
526 | (long_only && | 524 | (long_only && |
527 | (argv[GNoptind][2] || !my_index (optstring, argv[GNoptind][1]))))) | 525 | (argv[GNoptind][2] || ! my_index (optstring, argv[GNoptind][1]))))) |
528 | { | 526 | { |
529 | char *nameend; | 527 | char *nameend; |
530 | const struct GNoption *p; | 528 | const struct GNoption *p; |
@@ -535,12 +533,12 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
535 | int option_index; | 533 | int option_index; |
536 | 534 | ||
537 | for (nameend = nextchar; *nameend && *nameend != '='; nameend++) | 535 | for (nameend = nextchar; *nameend && *nameend != '='; nameend++) |
538 | /* Do nothing. */ ; | 536 | /* Do nothing. */; |
539 | 537 | ||
540 | /* Test all long options for either exact match | 538 | /* Test all long options for either exact match |
541 | * or abbreviated matches. */ | 539 | * or abbreviated matches. */ |
542 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 540 | for (p = longopts, option_index = 0; p->name; p++, option_index++) |
543 | if (!strncmp (p->name, nextchar, nameend - nextchar)) | 541 | if (! strncmp (p->name, nextchar, nameend - nextchar)) |
544 | { | 542 | { |
545 | if ((unsigned int) (nameend - nextchar) == | 543 | if ((unsigned int) (nameend - nextchar) == |
546 | (unsigned int) strlen (p->name)) | 544 | (unsigned int) strlen (p->name)) |
@@ -562,10 +560,12 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
562 | ambig = 1; | 560 | ambig = 1; |
563 | } | 561 | } |
564 | 562 | ||
565 | if (ambig && !exact) | 563 | if (ambig && ! exact) |
566 | { | 564 | { |
567 | if (GNopterr) | 565 | if (GNopterr) |
568 | FPRINTF (stderr, _("%s: option `%s' is ambiguous\n"), argv[0], | 566 | FPRINTF (stderr, |
567 | _ ("%s: option `%s' is ambiguous\n"), | ||
568 | argv[0], | ||
569 | argv[GNoptind]); | 569 | argv[GNoptind]); |
570 | nextchar += strlen (nextchar); | 570 | nextchar += strlen (nextchar); |
571 | GNoptind++; | 571 | GNoptind++; |
@@ -589,13 +589,16 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
589 | if (argv[GNoptind - 1][1] == '-') | 589 | if (argv[GNoptind - 1][1] == '-') |
590 | /* --option */ | 590 | /* --option */ |
591 | FPRINTF (stderr, | 591 | FPRINTF (stderr, |
592 | _("%s: option `--%s' does not allow an argument\n"), | 592 | _ ("%s: option `--%s' does not allow an argument\n"), |
593 | argv[0], pfound->name); | 593 | argv[0], |
594 | pfound->name); | ||
594 | else | 595 | else |
595 | /* +option or -option */ | 596 | /* +option or -option */ |
596 | FPRINTF (stderr, | 597 | FPRINTF (stderr, |
597 | _("%s: option `%c%s' does not allow an argument\n"), | 598 | _ ("%s: option `%c%s' does not allow an argument\n"), |
598 | argv[0], argv[GNoptind - 1][0], pfound->name); | 599 | argv[0], |
600 | argv[GNoptind - 1][0], | ||
601 | pfound->name); | ||
599 | } | 602 | } |
600 | nextchar += strlen (nextchar); | 603 | nextchar += strlen (nextchar); |
601 | return '?'; | 604 | return '?'; |
@@ -611,8 +614,10 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
611 | { | 614 | { |
612 | if (GNopterr) | 615 | if (GNopterr) |
613 | { | 616 | { |
614 | FPRINTF (stderr, _("%s: option `%s' requires an argument\n"), | 617 | FPRINTF (stderr, |
615 | argv[0], argv[GNoptind - 1]); | 618 | _ ("%s: option `%s' requires an argument\n"), |
619 | argv[0], | ||
620 | argv[GNoptind - 1]); | ||
616 | } | 621 | } |
617 | nextchar += strlen (nextchar); | 622 | nextchar += strlen (nextchar); |
618 | return (optstring[0] == ':') ? ':' : '?'; | 623 | return (optstring[0] == ':') ? ':' : '?'; |
@@ -633,19 +638,24 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
633 | * or the option starts with '--' or is not a valid short | 638 | * or the option starts with '--' or is not a valid short |
634 | * option, then it's an error. | 639 | * option, then it's an error. |
635 | * Otherwise interpret it as a short option. */ | 640 | * Otherwise interpret it as a short option. */ |
636 | if (!long_only || argv[GNoptind][1] == '-' || | 641 | if (! long_only || argv[GNoptind][1] == '-' || |
637 | my_index (optstring, *nextchar) == NULL) | 642 | my_index (optstring, *nextchar) == NULL) |
638 | { | 643 | { |
639 | if (GNopterr) | 644 | if (GNopterr) |
640 | { | 645 | { |
641 | if (argv[GNoptind][1] == '-') | 646 | if (argv[GNoptind][1] == '-') |
642 | /* --option */ | 647 | /* --option */ |
643 | FPRINTF (stderr, _("%s: unrecognized option `--%s'\n"), argv[0], | 648 | FPRINTF (stderr, |
649 | _ ("%s: unrecognized option `--%s'\n"), | ||
650 | argv[0], | ||
644 | nextchar); | 651 | nextchar); |
645 | else | 652 | else |
646 | /* +option or -option */ | 653 | /* +option or -option */ |
647 | FPRINTF (stderr, _("%s: unrecognized option `%c%s'\n"), argv[0], | 654 | FPRINTF (stderr, |
648 | argv[GNoptind][0], nextchar); | 655 | _ ("%s: unrecognized option `%c%s'\n"), |
656 | argv[0], | ||
657 | argv[GNoptind][0], | ||
658 | nextchar); | ||
649 | } | 659 | } |
650 | nextchar = (char *) ""; | 660 | nextchar = (char *) ""; |
651 | GNoptind++; | 661 | GNoptind++; |
@@ -669,9 +679,9 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
669 | { | 679 | { |
670 | if (posixly_correct) | 680 | if (posixly_correct) |
671 | /* 1003.2 specifies the format of this message. */ | 681 | /* 1003.2 specifies the format of this message. */ |
672 | FPRINTF (stderr, _("%s: illegal option -- %c\n"), argv[0], c); | 682 | FPRINTF (stderr, _ ("%s: illegal option -- %c\n"), argv[0], c); |
673 | else | 683 | else |
674 | FPRINTF (stderr, _("%s: invalid option -- %c\n"), argv[0], c); | 684 | FPRINTF (stderr, _ ("%s: invalid option -- %c\n"), argv[0], c); |
675 | } | 685 | } |
676 | return '?'; | 686 | return '?'; |
677 | } | 687 | } |
@@ -699,8 +709,10 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
699 | if (GNopterr) | 709 | if (GNopterr) |
700 | { | 710 | { |
701 | /* 1003.2 specifies the format of this message. */ | 711 | /* 1003.2 specifies the format of this message. */ |
702 | FPRINTF (stderr, _("%s: option requires an argument -- %c\n"), | 712 | FPRINTF (stderr, |
703 | argv[0], c); | 713 | _ ("%s: option requires an argument -- %c\n"), |
714 | argv[0], | ||
715 | c); | ||
704 | } | 716 | } |
705 | if (optstring[0] == ':') | 717 | if (optstring[0] == ':') |
706 | c = ':'; | 718 | c = ':'; |
@@ -718,13 +730,13 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
718 | 730 | ||
719 | for (nextchar = nameend = GNoptarg; *nameend && *nameend != '='; | 731 | for (nextchar = nameend = GNoptarg; *nameend && *nameend != '='; |
720 | nameend++) | 732 | nameend++) |
721 | /* Do nothing. */ ; | 733 | /* Do nothing. */; |
722 | 734 | ||
723 | /* Test all long options for either exact match | 735 | /* Test all long options for either exact match |
724 | * or abbreviated matches. */ | 736 | * or abbreviated matches. */ |
725 | if (longopts != NULL) | 737 | if (longopts != NULL) |
726 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 738 | for (p = longopts, option_index = 0; p->name; p++, option_index++) |
727 | if (!strncmp (p->name, nextchar, nameend - nextchar)) | 739 | if (! strncmp (p->name, nextchar, nameend - nextchar)) |
728 | { | 740 | { |
729 | if ((unsigned int) (nameend - nextchar) == strlen (p->name)) | 741 | if ((unsigned int) (nameend - nextchar) == strlen (p->name)) |
730 | { | 742 | { |
@@ -744,10 +756,12 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
744 | /* Second or later nonexact match found. */ | 756 | /* Second or later nonexact match found. */ |
745 | ambig = 1; | 757 | ambig = 1; |
746 | } | 758 | } |
747 | if (ambig && !exact) | 759 | if (ambig && ! exact) |
748 | { | 760 | { |
749 | if (GNopterr) | 761 | if (GNopterr) |
750 | FPRINTF (stderr, _("%s: option `-W %s' is ambiguous\n"), argv[0], | 762 | FPRINTF (stderr, |
763 | _ ("%s: option `-W %s' is ambiguous\n"), | ||
764 | argv[0], | ||
751 | argv[GNoptind]); | 765 | argv[GNoptind]); |
752 | nextchar += strlen (nextchar); | 766 | nextchar += strlen (nextchar); |
753 | GNoptind++; | 767 | GNoptind++; |
@@ -765,7 +779,10 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
765 | else | 779 | else |
766 | { | 780 | { |
767 | if (GNopterr) | 781 | if (GNopterr) |
768 | FPRINTF (stderr, _("%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name); | 782 | FPRINTF (stderr, |
783 | _ ("%s: option `-W %s' does not allow an argument\n"), | ||
784 | argv[0], | ||
785 | pfound->name); | ||
769 | 786 | ||
770 | nextchar += strlen (nextchar); | 787 | nextchar += strlen (nextchar); |
771 | return '?'; | 788 | return '?'; |
@@ -778,8 +795,10 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
778 | else | 795 | else |
779 | { | 796 | { |
780 | if (GNopterr) | 797 | if (GNopterr) |
781 | FPRINTF (stderr, _("%s: option `%s' requires an argument\n"), | 798 | FPRINTF (stderr, |
782 | argv[0], argv[GNoptind - 1]); | 799 | _ ("%s: option `%s' requires an argument\n"), |
800 | argv[0], | ||
801 | argv[GNoptind - 1]); | ||
783 | nextchar += strlen (nextchar); | 802 | nextchar += strlen (nextchar); |
784 | return optstring[0] == ':' ? ':' : '?'; | 803 | return optstring[0] == ':' ? ':' : '?'; |
785 | } | 804 | } |
@@ -795,7 +814,7 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
795 | return pfound->val; | 814 | return pfound->val; |
796 | } | 815 | } |
797 | nextchar = NULL; | 816 | nextchar = NULL; |
798 | return 'W'; /* Let the application handle it. */ | 817 | return 'W'; /* Let the application handle it. */ |
799 | } | 818 | } |
800 | if (temp[1] == ':') | 819 | if (temp[1] == ':') |
801 | { | 820 | { |
@@ -826,8 +845,10 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, | |||
826 | if (GNopterr) | 845 | if (GNopterr) |
827 | { | 846 | { |
828 | /* 1003.2 specifies the format of this message. */ | 847 | /* 1003.2 specifies the format of this message. */ |
829 | FPRINTF (stderr, _("%s: option requires an argument -- %c\n"), | 848 | FPRINTF (stderr, |
830 | argv[0], c); | 849 | _ ("%s: option requires an argument -- %c\n"), |
850 | argv[0], | ||
851 | c); | ||
831 | } | 852 | } |
832 | if (optstring[0] == ':') | 853 | if (optstring[0] == ':') |
833 | c = ':'; | 854 | c = ':'; |
@@ -880,8 +901,9 @@ GNUNET_GETOPT_run (const char *binaryOptions, | |||
880 | char *shorts; | 901 | char *shorts; |
881 | int spos; | 902 | int spos; |
882 | int cont; | 903 | int cont; |
883 | int c; | ||
884 | uint8_t *seen; | 904 | uint8_t *seen; |
905 | unsigned int optmatch = 0; | ||
906 | const char *have_exclusive = NULL; | ||
885 | 907 | ||
886 | GNUNET_assert (argc > 0); | 908 | GNUNET_assert (argc > 0); |
887 | GNoptind = 0; | 909 | GNoptind = 0; |
@@ -890,12 +912,13 @@ GNUNET_GETOPT_run (const char *binaryOptions, | |||
890 | clpc.allOptions = allOptions; | 912 | clpc.allOptions = allOptions; |
891 | clpc.argv = argv; | 913 | clpc.argv = argv; |
892 | clpc.argc = argc; | 914 | clpc.argc = argc; |
893 | for (count = 0; NULL != allOptions[count].name; count++) ; | 915 | for (count = 0; NULL != allOptions[count].name; count++) |
916 | ; | ||
894 | 917 | ||
895 | long_options = GNUNET_new_array (count + 1, | 918 | /* transform our option representation into the format |
896 | struct GNoption); | 919 | used by the GNU getopt copylib */ |
897 | seen = GNUNET_new_array (count, | 920 | long_options = GNUNET_new_array (count + 1, struct GNoption); |
898 | uint8_t); | 921 | seen = GNUNET_new_array (count, uint8_t); |
899 | shorts = GNUNET_malloc (count * 2 + 1); | 922 | shorts = GNUNET_malloc (count * 2 + 1); |
900 | spos = 0; | 923 | spos = 0; |
901 | for (unsigned i = 0; i < count; i++) | 924 | for (unsigned i = 0; i < count; i++) |
@@ -916,53 +939,68 @@ GNUNET_GETOPT_run (const char *binaryOptions, | |||
916 | cont = GNUNET_OK; | 939 | cont = GNUNET_OK; |
917 | 940 | ||
918 | /* main getopt loop */ | 941 | /* main getopt loop */ |
919 | while (GNUNET_OK == cont) | 942 | while (1) |
920 | { | 943 | { |
921 | int option_index = 0; | 944 | int option_index = 0; |
922 | unsigned int i; | 945 | unsigned int i; |
946 | int c; | ||
923 | 947 | ||
924 | c = GNgetopt_long (argc, argv, | 948 | c = GNgetopt_long (argc, argv, shorts, long_options, &option_index); |
925 | shorts, | ||
926 | long_options, | ||
927 | &option_index); | ||
928 | if (c == GNUNET_SYSERR) | 949 | if (c == GNUNET_SYSERR) |
929 | break; /* No more flags to process */ | 950 | break; /* No more flags to process */ |
930 | 951 | ||
952 | /* Check which of our program's options was given by the user */ | ||
931 | for (i = 0; i < count; i++) | 953 | for (i = 0; i < count; i++) |
932 | { | 954 | { |
933 | clpc.currentArgument = GNoptind - 1; | 955 | clpc.currentArgument = GNoptind - 1; |
934 | if ((char) c == allOptions[i].shortName) | 956 | if ((char) c == allOptions[i].shortName) |
935 | { | 957 | { |
936 | cont = allOptions[i].processor (&clpc, | 958 | optmatch++; |
937 | allOptions[i].scls, | 959 | if (allOptions[i].option_exclusive) |
938 | allOptions[i].name, | 960 | have_exclusive = allOptions[i].name; |
939 | GNoptarg); | 961 | if (GNUNET_OK == cont) |
962 | { | ||
963 | /* parse the option using the option-specific processor */ | ||
964 | cont = allOptions[i].processor (&clpc, | ||
965 | allOptions[i].scls, | ||
966 | allOptions[i].name, | ||
967 | GNoptarg); | ||
968 | } | ||
940 | seen[i] = 1; | 969 | seen[i] = 1; |
941 | break; | 970 | break; |
942 | } | 971 | } |
943 | } | 972 | } |
944 | if (i == count) | 973 | if (i == count) |
945 | { | 974 | { |
946 | FPRINTF (stderr, | 975 | FPRINTF (stderr, _ ("Use %s to get a list of options.\n"), "--help"); |
947 | _("Use %s to get a list of options.\n"), | ||
948 | "--help"); | ||
949 | cont = GNUNET_SYSERR; | 976 | cont = GNUNET_SYSERR; |
950 | } | 977 | } |
951 | } | 978 | } |
952 | GNUNET_free (shorts); | 979 | GNUNET_free (shorts); |
953 | GNUNET_free (long_options); | 980 | GNUNET_free (long_options); |
954 | 981 | ||
982 | /* check that if any option that was marked as exclusive | ||
983 | is the only option that was provided */ | ||
984 | if ((NULL != have_exclusive) && (optmatch > 1)) | ||
985 | { | ||
986 | FPRINTF (stderr, | ||
987 | _ ("Option `%s' can't be used with other options.\n"), | ||
988 | have_exclusive); | ||
989 | cont = GNUNET_SYSERR; | ||
990 | } | ||
955 | if (GNUNET_YES == cont) | 991 | if (GNUNET_YES == cont) |
956 | { | 992 | { |
993 | /* check that all mandatory options are present */ | ||
957 | for (count = 0; NULL != allOptions[count].name; count++) | 994 | for (count = 0; NULL != allOptions[count].name; count++) |
958 | if ( (0 == seen[count]) && | 995 | { |
959 | (allOptions[count].option_mandatory) ) | 996 | if ((0 == seen[count]) && (allOptions[count].option_mandatory)) |
960 | { | 997 | { |
961 | FPRINTF (stderr, | 998 | FPRINTF (stderr, |
962 | _("Missing mandatory option `%s'.\n"), | 999 | _ ("Missing mandatory option `%s'.\n"), |
963 | allOptions[count].name); | 1000 | allOptions[count].name); |
964 | cont = GNUNET_SYSERR; | 1001 | cont = GNUNET_SYSERR; |
965 | } | 1002 | } |
1003 | } | ||
966 | } | 1004 | } |
967 | GNUNET_free (seen); | 1005 | GNUNET_free (seen); |
968 | 1006 | ||
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index 77032e501..05856beee 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c | |||
@@ -26,7 +26,7 @@ | |||
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | 28 | ||
29 | #define LOG(kind,...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) | 29 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) |
30 | 30 | ||
31 | 31 | ||
32 | /** | 32 | /** |
@@ -48,9 +48,7 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
48 | 48 | ||
49 | (void) option; | 49 | (void) option; |
50 | (void) value; | 50 | (void) value; |
51 | printf ("%s v%s\n", | 51 | printf ("%s v%s\n", ctx->binaryName, version); |
52 | ctx->binaryName, | ||
53 | version); | ||
54 | return GNUNET_NO; | 52 | return GNUNET_NO; |
55 | } | 53 | } |
56 | 54 | ||
@@ -64,13 +62,13 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
64 | struct GNUNET_GETOPT_CommandLineOption | 62 | struct GNUNET_GETOPT_CommandLineOption |
65 | GNUNET_GETOPT_option_version (const char *version) | 63 | GNUNET_GETOPT_option_version (const char *version) |
66 | { | 64 | { |
67 | struct GNUNET_GETOPT_CommandLineOption clo = { | 65 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'v', |
68 | .shortName = 'v', | 66 | .name = "version", |
69 | .name = "version", | 67 | .description = gettext_noop ( |
70 | .description = gettext_noop("print the version number"), | 68 | "print the version number"), |
71 | .processor = &print_version, | 69 | .option_exclusive = 1, |
72 | .scls = (void *) version | 70 | .processor = &print_version, |
73 | }; | 71 | .scls = (void *) version}; |
74 | return clo; | 72 | return clo; |
75 | } | 73 | } |
76 | 74 | ||
@@ -110,10 +108,9 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
110 | (void) value; | 108 | (void) value; |
111 | if (NULL != about) | 109 | if (NULL != about) |
112 | { | 110 | { |
113 | printf ("%s\n%s\n", | 111 | printf ("%s\n%s\n", ctx->binaryOptions, gettext (about)); |
114 | ctx->binaryOptions, | 112 | printf (_ ( |
115 | gettext (about)); | 113 | "Arguments mandatory for long options are also mandatory for short options.\n")); |
116 | printf (_("Arguments mandatory for long options are also mandatory for short options.\n")); | ||
117 | } | 114 | } |
118 | i = 0; | 115 | i = 0; |
119 | opt = ctx->allOptions; | 116 | opt = ctx->allOptions; |
@@ -146,7 +143,7 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
146 | trans = ""; | 143 | trans = ""; |
147 | ml = strlen (trans); | 144 | ml = strlen (trans); |
148 | p = 0; | 145 | p = 0; |
149 | OUTER: | 146 | OUTER: |
150 | while (ml - p > 78 - slen) | 147 | while (ml - p > 78 - slen) |
151 | { | 148 | { |
152 | for (j = p + 78 - slen; j > (int) p; j--) | 149 | for (j = p + 78 - slen; j > (int) p; j--) |
@@ -198,13 +195,13 @@ OUTER: | |||
198 | struct GNUNET_GETOPT_CommandLineOption | 195 | struct GNUNET_GETOPT_CommandLineOption |
199 | GNUNET_GETOPT_option_help (const char *about) | 196 | GNUNET_GETOPT_option_help (const char *about) |
200 | { | 197 | { |
201 | struct GNUNET_GETOPT_CommandLineOption clo = { | 198 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'h', |
202 | .shortName = 'h', | 199 | .name = "help", |
203 | .name = "help", | 200 | .description = gettext_noop ( |
204 | .description = gettext_noop("print this help"), | 201 | "print this help"), |
205 | .processor = format_help, | 202 | .option_exclusive = 1, |
206 | .scls = (void *) about | 203 | .processor = format_help, |
207 | }; | 204 | .scls = (void *) about}; |
208 | 205 | ||
209 | return clo; | 206 | return clo; |
210 | } | 207 | } |
@@ -251,17 +248,15 @@ increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
251 | */ | 248 | */ |
252 | struct GNUNET_GETOPT_CommandLineOption | 249 | struct GNUNET_GETOPT_CommandLineOption |
253 | GNUNET_GETOPT_option_increment_uint (char shortName, | 250 | GNUNET_GETOPT_option_increment_uint (char shortName, |
254 | const char *name, | 251 | const char *name, |
255 | const char *description, | 252 | const char *description, |
256 | unsigned int *val) | 253 | unsigned int *val) |
257 | { | 254 | { |
258 | struct GNUNET_GETOPT_CommandLineOption clo = { | 255 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
259 | .shortName = shortName, | 256 | .name = name, |
260 | .name = name, | 257 | .description = description, |
261 | .description = description, | 258 | .processor = &increment_value, |
262 | .processor = &increment_value, | 259 | .scls = (void *) val}; |
263 | .scls = (void *) val | ||
264 | }; | ||
265 | 260 | ||
266 | return clo; | 261 | return clo; |
267 | } | 262 | } |
@@ -276,13 +271,12 @@ GNUNET_GETOPT_option_increment_uint (char shortName, | |||
276 | struct GNUNET_GETOPT_CommandLineOption | 271 | struct GNUNET_GETOPT_CommandLineOption |
277 | GNUNET_GETOPT_option_verbose (unsigned int *level) | 272 | GNUNET_GETOPT_option_verbose (unsigned int *level) |
278 | { | 273 | { |
279 | struct GNUNET_GETOPT_CommandLineOption clo = { | 274 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'V', |
280 | .shortName = 'V', | 275 | .name = "verbose", |
281 | .name = "verbose", | 276 | .description = |
282 | .description = gettext_noop("be verbose"), | 277 | gettext_noop ("be verbose"), |
283 | .processor = &increment_value, | 278 | .processor = &increment_value, |
284 | .scls = (void *) level | 279 | .scls = (void *) level}; |
285 | }; | ||
286 | 280 | ||
287 | return clo; | 281 | return clo; |
288 | } | 282 | } |
@@ -330,17 +324,15 @@ set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
330 | */ | 324 | */ |
331 | struct GNUNET_GETOPT_CommandLineOption | 325 | struct GNUNET_GETOPT_CommandLineOption |
332 | GNUNET_GETOPT_option_flag (char shortName, | 326 | GNUNET_GETOPT_option_flag (char shortName, |
333 | const char *name, | 327 | const char *name, |
334 | const char *description, | 328 | const char *description, |
335 | int *val) | 329 | int *val) |
336 | { | 330 | { |
337 | struct GNUNET_GETOPT_CommandLineOption clo = { | 331 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
338 | .shortName = shortName, | 332 | .name = name, |
339 | .name = name, | 333 | .description = description, |
340 | .description = description, | 334 | .processor = &set_one, |
341 | .processor = &set_one, | 335 | .scls = (void *) val}; |
342 | .scls = (void *) val | ||
343 | }; | ||
344 | 336 | ||
345 | return clo; | 337 | return clo; |
346 | } | 338 | } |
@@ -393,15 +385,13 @@ GNUNET_GETOPT_option_string (char shortName, | |||
393 | const char *description, | 385 | const char *description, |
394 | char **str) | 386 | char **str) |
395 | { | 387 | { |
396 | struct GNUNET_GETOPT_CommandLineOption clo = { | 388 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
397 | .shortName = shortName, | 389 | .name = name, |
398 | .name = name, | 390 | .argumentHelp = argumentHelp, |
399 | .argumentHelp = argumentHelp, | 391 | .description = description, |
400 | .description = description, | 392 | .require_argument = 1, |
401 | .require_argument = 1, | 393 | .processor = &set_string, |
402 | .processor = &set_string, | 394 | .scls = (void *) str}; |
403 | .scls = (void *) str | ||
404 | }; | ||
405 | 395 | ||
406 | return clo; | 396 | return clo; |
407 | } | 397 | } |
@@ -416,15 +406,14 @@ GNUNET_GETOPT_option_string (char shortName, | |||
416 | struct GNUNET_GETOPT_CommandLineOption | 406 | struct GNUNET_GETOPT_CommandLineOption |
417 | GNUNET_GETOPT_option_loglevel (char **level) | 407 | GNUNET_GETOPT_option_loglevel (char **level) |
418 | { | 408 | { |
419 | struct GNUNET_GETOPT_CommandLineOption clo = { | 409 | struct GNUNET_GETOPT_CommandLineOption clo = |
420 | .shortName = 'L', | 410 | {.shortName = 'L', |
421 | .name = "log", | 411 | .name = "log", |
422 | .argumentHelp = "LOGLEVEL", | 412 | .argumentHelp = "LOGLEVEL", |
423 | .description = gettext_noop("configure logging to use LOGLEVEL"), | 413 | .description = gettext_noop ("configure logging to use LOGLEVEL"), |
424 | .require_argument = 1, | 414 | .require_argument = 1, |
425 | .processor = &set_string, | 415 | .processor = &set_string, |
426 | .scls = (void *) level | 416 | .scls = (void *) level}; |
427 | }; | ||
428 | 417 | ||
429 | return clo; | 418 | return clo; |
430 | } | 419 | } |
@@ -469,20 +458,18 @@ set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
469 | */ | 458 | */ |
470 | struct GNUNET_GETOPT_CommandLineOption | 459 | struct GNUNET_GETOPT_CommandLineOption |
471 | GNUNET_GETOPT_option_filename (char shortName, | 460 | GNUNET_GETOPT_option_filename (char shortName, |
472 | const char *name, | 461 | const char *name, |
473 | const char *argumentHelp, | 462 | const char *argumentHelp, |
474 | const char *description, | 463 | const char *description, |
475 | char **str) | 464 | char **str) |
476 | { | 465 | { |
477 | struct GNUNET_GETOPT_CommandLineOption clo = { | 466 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
478 | .shortName = shortName, | 467 | .name = name, |
479 | .name = name, | 468 | .argumentHelp = argumentHelp, |
480 | .argumentHelp = argumentHelp, | 469 | .description = description, |
481 | .description = description, | 470 | .require_argument = 1, |
482 | .require_argument = 1, | 471 | .processor = &set_filename, |
483 | .processor = &set_filename, | 472 | .scls = (void *) str}; |
484 | .scls = (void *) str | ||
485 | }; | ||
486 | 473 | ||
487 | return clo; | 474 | return clo; |
488 | } | 475 | } |
@@ -496,15 +483,15 @@ GNUNET_GETOPT_option_filename (char shortName, | |||
496 | struct GNUNET_GETOPT_CommandLineOption | 483 | struct GNUNET_GETOPT_CommandLineOption |
497 | GNUNET_GETOPT_option_logfile (char **logfn) | 484 | GNUNET_GETOPT_option_logfile (char **logfn) |
498 | { | 485 | { |
499 | struct GNUNET_GETOPT_CommandLineOption clo = { | 486 | struct GNUNET_GETOPT_CommandLineOption clo = |
500 | .shortName = 'l', | 487 | {.shortName = 'l', |
501 | .name = "logfile", | 488 | .name = "logfile", |
502 | .argumentHelp = "FILENAME", | 489 | .argumentHelp = "FILENAME", |
503 | .description = gettext_noop ("configure logging to write logs to FILENAME"), | 490 | .description = |
504 | .require_argument = 1, | 491 | gettext_noop ("configure logging to write logs to FILENAME"), |
505 | .processor = &set_filename, | 492 | .require_argument = 1, |
506 | .scls = (void *) logfn | 493 | .processor = &set_filename, |
507 | }; | 494 | .scls = (void *) logfn}; |
508 | 495 | ||
509 | return clo; | 496 | return clo; |
510 | } | 497 | } |
@@ -518,15 +505,14 @@ GNUNET_GETOPT_option_logfile (char **logfn) | |||
518 | struct GNUNET_GETOPT_CommandLineOption | 505 | struct GNUNET_GETOPT_CommandLineOption |
519 | GNUNET_GETOPT_option_cfgfile (char **fn) | 506 | GNUNET_GETOPT_option_cfgfile (char **fn) |
520 | { | 507 | { |
521 | struct GNUNET_GETOPT_CommandLineOption clo = { | 508 | struct GNUNET_GETOPT_CommandLineOption clo = |
522 | .shortName = 'c', | 509 | {.shortName = 'c', |
523 | .name = "config", | 510 | .name = "config", |
524 | .argumentHelp = "FILENAME", | 511 | .argumentHelp = "FILENAME", |
525 | .description = gettext_noop("use configuration file FILENAME"), | 512 | .description = gettext_noop ("use configuration file FILENAME"), |
526 | .require_argument = 1, | 513 | .require_argument = 1, |
527 | .processor = &set_filename, | 514 | .processor = &set_filename, |
528 | .scls = (void *) fn | 515 | .scls = (void *) fn}; |
529 | }; | ||
530 | 516 | ||
531 | return clo; | 517 | return clo; |
532 | } | 518 | } |
@@ -555,13 +541,10 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
555 | char dummy[2]; | 541 | char dummy[2]; |
556 | 542 | ||
557 | (void) ctx; | 543 | (void) ctx; |
558 | if (1 != SSCANF (value, | 544 | if (1 != SSCANF (value, "%llu%1s", val, dummy)) |
559 | "%llu%1s", | ||
560 | val, | ||
561 | dummy)) | ||
562 | { | 545 | { |
563 | FPRINTF (stderr, | 546 | FPRINTF (stderr, |
564 | _("You must pass a number to the `%s' option.\n"), | 547 | _ ("You must pass a number to the `%s' option.\n"), |
565 | option); | 548 | option); |
566 | return GNUNET_SYSERR; | 549 | return GNUNET_SYSERR; |
567 | } | 550 | } |
@@ -580,20 +563,18 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
580 | */ | 563 | */ |
581 | struct GNUNET_GETOPT_CommandLineOption | 564 | struct GNUNET_GETOPT_CommandLineOption |
582 | GNUNET_GETOPT_option_ulong (char shortName, | 565 | GNUNET_GETOPT_option_ulong (char shortName, |
583 | const char *name, | 566 | const char *name, |
584 | const char *argumentHelp, | 567 | const char *argumentHelp, |
585 | const char *description, | 568 | const char *description, |
586 | unsigned long long *val) | 569 | unsigned long long *val) |
587 | { | 570 | { |
588 | struct GNUNET_GETOPT_CommandLineOption clo = { | 571 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
589 | .shortName = shortName, | 572 | .name = name, |
590 | .name = name, | 573 | .argumentHelp = argumentHelp, |
591 | .argumentHelp = argumentHelp, | 574 | .description = description, |
592 | .description = description, | 575 | .require_argument = 1, |
593 | .require_argument = 1, | 576 | .processor = &set_ulong, |
594 | .processor = &set_ulong, | 577 | .scls = (void *) val}; |
595 | .scls = (void *) val | ||
596 | }; | ||
597 | 578 | ||
598 | return clo; | 579 | return clo; |
599 | } | 580 | } |
@@ -619,14 +600,12 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
619 | const char *value) | 600 | const char *value) |
620 | { | 601 | { |
621 | struct GNUNET_TIME_Relative *val = scls; | 602 | struct GNUNET_TIME_Relative *val = scls; |
622 | 603 | ||
623 | (void) ctx; | 604 | (void) ctx; |
624 | if (GNUNET_OK != | 605 | if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val)) |
625 | GNUNET_STRINGS_fancy_time_to_relative (value, | ||
626 | val)) | ||
627 | { | 606 | { |
628 | FPRINTF (stderr, | 607 | FPRINTF (stderr, |
629 | _("You must pass relative time to the `%s' option.\n"), | 608 | _ ("You must pass relative time to the `%s' option.\n"), |
630 | option); | 609 | option); |
631 | return GNUNET_SYSERR; | 610 | return GNUNET_SYSERR; |
632 | } | 611 | } |
@@ -646,20 +625,18 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
646 | */ | 625 | */ |
647 | struct GNUNET_GETOPT_CommandLineOption | 626 | struct GNUNET_GETOPT_CommandLineOption |
648 | GNUNET_GETOPT_option_relative_time (char shortName, | 627 | GNUNET_GETOPT_option_relative_time (char shortName, |
649 | const char *name, | 628 | const char *name, |
650 | const char *argumentHelp, | 629 | const char *argumentHelp, |
651 | const char *description, | 630 | const char *description, |
652 | struct GNUNET_TIME_Relative *val) | 631 | struct GNUNET_TIME_Relative *val) |
653 | { | 632 | { |
654 | struct GNUNET_GETOPT_CommandLineOption clo = { | 633 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
655 | .shortName = shortName, | 634 | .name = name, |
656 | .name = name, | 635 | .argumentHelp = argumentHelp, |
657 | .argumentHelp = argumentHelp, | 636 | .description = description, |
658 | .description = description, | 637 | .require_argument = 1, |
659 | .require_argument = 1, | 638 | .processor = &set_relative_time, |
660 | .processor = &set_relative_time, | 639 | .scls = (void *) val}; |
661 | .scls = (void *) val | ||
662 | }; | ||
663 | 640 | ||
664 | return clo; | 641 | return clo; |
665 | } | 642 | } |
@@ -687,12 +664,10 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
687 | struct GNUNET_TIME_Absolute *val = scls; | 664 | struct GNUNET_TIME_Absolute *val = scls; |
688 | 665 | ||
689 | (void) ctx; | 666 | (void) ctx; |
690 | if (GNUNET_OK != | 667 | if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val)) |
691 | GNUNET_STRINGS_fancy_time_to_absolute (value, | ||
692 | val)) | ||
693 | { | 668 | { |
694 | FPRINTF (stderr, | 669 | FPRINTF (stderr, |
695 | _("You must pass absolute time to the `%s' option.\n"), | 670 | _ ("You must pass absolute time to the `%s' option.\n"), |
696 | option); | 671 | option); |
697 | return GNUNET_SYSERR; | 672 | return GNUNET_SYSERR; |
698 | } | 673 | } |
@@ -712,20 +687,18 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
712 | */ | 687 | */ |
713 | struct GNUNET_GETOPT_CommandLineOption | 688 | struct GNUNET_GETOPT_CommandLineOption |
714 | GNUNET_GETOPT_option_absolute_time (char shortName, | 689 | GNUNET_GETOPT_option_absolute_time (char shortName, |
715 | const char *name, | 690 | const char *name, |
716 | const char *argumentHelp, | 691 | const char *argumentHelp, |
717 | const char *description, | 692 | const char *description, |
718 | struct GNUNET_TIME_Absolute *val) | 693 | struct GNUNET_TIME_Absolute *val) |
719 | { | 694 | { |
720 | struct GNUNET_GETOPT_CommandLineOption clo = { | 695 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
721 | .shortName = shortName, | 696 | .name = name, |
722 | .name = name, | 697 | .argumentHelp = argumentHelp, |
723 | .argumentHelp = argumentHelp, | 698 | .description = description, |
724 | .description = description, | 699 | .require_argument = 1, |
725 | .require_argument = 1, | 700 | .processor = &set_absolute_time, |
726 | .processor = &set_absolute_time, | 701 | .scls = (void *) val}; |
727 | .scls = (void *) val | ||
728 | }; | ||
729 | 702 | ||
730 | return clo; | 703 | return clo; |
731 | } | 704 | } |
@@ -754,20 +727,18 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
754 | char dummy[2]; | 727 | char dummy[2]; |
755 | 728 | ||
756 | (void) ctx; | 729 | (void) ctx; |
757 | if('-' == *value) | 730 | if ('-' == *value) |
758 | { | 731 | { |
759 | FPRINTF (stderr, | 732 | FPRINTF (stderr, |
760 | _("Your input for the '%s' option has to be a non negative number \n"), | 733 | _ ( |
761 | option); | 734 | "Your input for the '%s' option has to be a non negative number \n"), |
762 | return GNUNET_SYSERR; | 735 | option); |
736 | return GNUNET_SYSERR; | ||
763 | } | 737 | } |
764 | if (1 != SSCANF (value, | 738 | if (1 != SSCANF (value, "%u%1s", val, dummy)) |
765 | "%u%1s", | ||
766 | val, | ||
767 | dummy)) | ||
768 | { | 739 | { |
769 | FPRINTF (stderr, | 740 | FPRINTF (stderr, |
770 | _("You must pass a number to the `%s' option.\n"), | 741 | _ ("You must pass a number to the `%s' option.\n"), |
771 | option); | 742 | option); |
772 | return GNUNET_SYSERR; | 743 | return GNUNET_SYSERR; |
773 | } | 744 | } |
@@ -786,26 +757,23 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
786 | */ | 757 | */ |
787 | struct GNUNET_GETOPT_CommandLineOption | 758 | struct GNUNET_GETOPT_CommandLineOption |
788 | GNUNET_GETOPT_option_uint (char shortName, | 759 | GNUNET_GETOPT_option_uint (char shortName, |
789 | const char *name, | 760 | const char *name, |
790 | const char *argumentHelp, | 761 | const char *argumentHelp, |
791 | const char *description, | 762 | const char *description, |
792 | unsigned int *val) | 763 | unsigned int *val) |
793 | { | 764 | { |
794 | struct GNUNET_GETOPT_CommandLineOption clo = { | 765 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
795 | .shortName = shortName, | 766 | .name = name, |
796 | .name = name, | 767 | .argumentHelp = argumentHelp, |
797 | .argumentHelp = argumentHelp, | 768 | .description = description, |
798 | .description = description, | 769 | .require_argument = 1, |
799 | .require_argument = 1, | 770 | .processor = &set_uint, |
800 | .processor = &set_uint, | 771 | .scls = (void *) val}; |
801 | .scls = (void *) val | ||
802 | }; | ||
803 | 772 | ||
804 | return clo; | 773 | return clo; |
805 | } | 774 | } |
806 | 775 | ||
807 | 776 | ||
808 | |||
809 | /** | 777 | /** |
810 | * Set an option of type 'uint16_t' from the command line. | 778 | * Set an option of type 'uint16_t' from the command line. |
811 | * A pointer to this function should be passed as part of the | 779 | * A pointer to this function should be passed as part of the |
@@ -821,30 +789,27 @@ GNUNET_GETOPT_option_uint (char shortName, | |||
821 | */ | 789 | */ |
822 | static int | 790 | static int |
823 | set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 791 | set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
824 | void *scls, | 792 | void *scls, |
825 | const char *option, | 793 | const char *option, |
826 | const char *value) | 794 | const char *value) |
827 | { | 795 | { |
828 | uint16_t *val = scls; | 796 | uint16_t *val = scls; |
829 | unsigned int v; | 797 | unsigned int v; |
830 | char dummy[2]; | 798 | char dummy[2]; |
831 | 799 | ||
832 | (void) ctx; | 800 | (void) ctx; |
833 | if (1 != SSCANF (value, | 801 | if (1 != SSCANF (value, "%u%1s", &v, dummy)) |
834 | "%u%1s", | ||
835 | &v, | ||
836 | dummy)) | ||
837 | { | 802 | { |
838 | FPRINTF (stderr, | 803 | FPRINTF (stderr, |
839 | _("You must pass a number to the `%s' option.\n"), | 804 | _ ("You must pass a number to the `%s' option.\n"), |
840 | option); | 805 | option); |
841 | return GNUNET_SYSERR; | 806 | return GNUNET_SYSERR; |
842 | } | 807 | } |
843 | if (v > UINT16_MAX) | 808 | if (v > UINT16_MAX) |
844 | { | 809 | { |
845 | FPRINTF (stderr, | 810 | FPRINTF (stderr, |
846 | _("You must pass a number below %u to the `%s' option.\n"), | 811 | _ ("You must pass a number below %u to the `%s' option.\n"), |
847 | (unsigned int) UINT16_MAX, | 812 | (unsigned int) UINT16_MAX, |
848 | option); | 813 | option); |
849 | return GNUNET_SYSERR; | 814 | return GNUNET_SYSERR; |
850 | } | 815 | } |
@@ -864,20 +829,18 @@ set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
864 | */ | 829 | */ |
865 | struct GNUNET_GETOPT_CommandLineOption | 830 | struct GNUNET_GETOPT_CommandLineOption |
866 | GNUNET_GETOPT_option_uint16 (char shortName, | 831 | GNUNET_GETOPT_option_uint16 (char shortName, |
867 | const char *name, | 832 | const char *name, |
868 | const char *argumentHelp, | 833 | const char *argumentHelp, |
869 | const char *description, | 834 | const char *description, |
870 | uint16_t *val) | 835 | uint16_t *val) |
871 | { | 836 | { |
872 | struct GNUNET_GETOPT_CommandLineOption clo = { | 837 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
873 | .shortName = shortName, | 838 | .name = name, |
874 | .name = name, | 839 | .argumentHelp = argumentHelp, |
875 | .argumentHelp = argumentHelp, | 840 | .description = description, |
876 | .description = description, | 841 | .require_argument = 1, |
877 | .require_argument = 1, | 842 | .processor = &set_uint16, |
878 | .processor = &set_uint16, | 843 | .scls = (void *) val}; |
879 | .scls = (void *) val | ||
880 | }; | ||
881 | 844 | ||
882 | return clo; | 845 | return clo; |
883 | } | 846 | } |
@@ -922,15 +885,16 @@ set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
922 | struct Base32Context *bc = scls; | 885 | struct Base32Context *bc = scls; |
923 | 886 | ||
924 | (void) ctx; | 887 | (void) ctx; |
925 | if (GNUNET_OK != | 888 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (value, |
926 | GNUNET_STRINGS_string_to_data (value, | 889 | strlen (value), |
927 | strlen (value), | 890 | bc->val, |
928 | bc->val, | 891 | bc->val_size)) |
929 | bc->val_size)) | ||
930 | { | 892 | { |
931 | fprintf (stderr, | 893 | fprintf ( |
932 | _("Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), | 894 | stderr, |
933 | option); | 895 | _ ( |
896 | "Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), | ||
897 | option); | ||
934 | return GNUNET_SYSERR; | 898 | return GNUNET_SYSERR; |
935 | } | 899 | } |
936 | return GNUNET_OK; | 900 | return GNUNET_OK; |
@@ -963,23 +927,21 @@ free_bc (void *cls) | |||
963 | */ | 927 | */ |
964 | struct GNUNET_GETOPT_CommandLineOption | 928 | struct GNUNET_GETOPT_CommandLineOption |
965 | GNUNET_GETOPT_option_base32_fixed_size (char shortName, | 929 | GNUNET_GETOPT_option_base32_fixed_size (char shortName, |
966 | const char *name, | 930 | const char *name, |
967 | const char *argumentHelp, | 931 | const char *argumentHelp, |
968 | const char *description, | 932 | const char *description, |
969 | void *val, | 933 | void *val, |
970 | size_t val_size) | 934 | size_t val_size) |
971 | { | 935 | { |
972 | struct Base32Context *bc = GNUNET_new (struct Base32Context); | 936 | struct Base32Context *bc = GNUNET_new (struct Base32Context); |
973 | struct GNUNET_GETOPT_CommandLineOption clo = { | 937 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
974 | .shortName = shortName, | 938 | .name = name, |
975 | .name = name, | 939 | .argumentHelp = argumentHelp, |
976 | .argumentHelp = argumentHelp, | 940 | .description = description, |
977 | .description = description, | 941 | .require_argument = 1, |
978 | .require_argument = 1, | 942 | .processor = &set_base32, |
979 | .processor = &set_base32, | 943 | .cleaner = &free_bc, |
980 | .cleaner = &free_bc, | 944 | .scls = (void *) bc}; |
981 | .scls = (void *) bc | ||
982 | }; | ||
983 | 945 | ||
984 | bc->val = val; | 946 | bc->val = val; |
985 | bc->val_size = val_size; | 947 | bc->val_size = val_size; |
@@ -1001,4 +963,18 @@ GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt) | |||
1001 | } | 963 | } |
1002 | 964 | ||
1003 | 965 | ||
966 | /** | ||
967 | * Make the given option mutually exclusive with other options. | ||
968 | * | ||
969 | * @param opt option to modify | ||
970 | * @return @a opt with the exclusive flag set. | ||
971 | */ | ||
972 | struct GNUNET_GETOPT_CommandLineOption | ||
973 | GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt) | ||
974 | { | ||
975 | opt.option_exclusive = 1; | ||
976 | return opt; | ||
977 | } | ||
978 | |||
979 | |||
1004 | /* end of getopt_helpers.c */ | 980 | /* end of getopt_helpers.c */ |
diff --git a/src/util/gnunet-config.c b/src/util/gnunet-config.c index f700428a2..3b9b64d4e 100644 --- a/src/util/gnunet-config.c +++ b/src/util/gnunet-config.c | |||
@@ -43,6 +43,13 @@ static char *option; | |||
43 | static char *value; | 43 | static char *value; |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * Backend to check if the respective plugin is | ||
47 | * loadable. NULL if no check is to be performed. | ||
48 | * The value is the "basename" of the plugin to load. | ||
49 | */ | ||
50 | static char *backend_check; | ||
51 | |||
52 | /** | ||
46 | * Treat option as a filename. | 53 | * Treat option as a filename. |
47 | */ | 54 | */ |
48 | static int is_filename; | 55 | static int is_filename; |
@@ -55,7 +62,7 @@ static int list_sections; | |||
55 | /** | 62 | /** |
56 | * Return value from 'main'. | 63 | * Return value from 'main'. |
57 | */ | 64 | */ |
58 | static int ret; | 65 | static int global_ret; |
59 | 66 | ||
60 | /** | 67 | /** |
61 | * Should we generate a configuration file that is clean and | 68 | * Should we generate a configuration file that is clean and |
@@ -63,6 +70,7 @@ static int ret; | |||
63 | */ | 70 | */ |
64 | static int rewrite; | 71 | static int rewrite; |
65 | 72 | ||
73 | |||
66 | /** | 74 | /** |
67 | * Print each option in a given section. | 75 | * Print each option in a given section. |
68 | * | 76 | * |
@@ -95,18 +103,12 @@ print_option (void *cls, | |||
95 | fn = value_fn; | 103 | fn = value_fn; |
96 | else | 104 | else |
97 | GNUNET_free (value_fn); | 105 | GNUNET_free (value_fn); |
98 | fprintf (stdout, | 106 | fprintf (stdout, "%s = %s\n", option, fn); |
99 | "%s = %s\n", | ||
100 | option, | ||
101 | fn); | ||
102 | GNUNET_free (fn); | 107 | GNUNET_free (fn); |
103 | } | 108 | } |
104 | else | 109 | else |
105 | { | 110 | { |
106 | fprintf (stdout, | 111 | fprintf (stdout, "%s = %s\n", option, value); |
107 | "%s = %s\n", | ||
108 | option, | ||
109 | value); | ||
110 | } | 112 | } |
111 | } | 113 | } |
112 | 114 | ||
@@ -118,13 +120,10 @@ print_option (void *cls, | |||
118 | * @param section a section in the configuration file | 120 | * @param section a section in the configuration file |
119 | */ | 121 | */ |
120 | static void | 122 | static void |
121 | print_section_name (void *cls, | 123 | print_section_name (void *cls, const char *section) |
122 | const char *section) | ||
123 | { | 124 | { |
124 | (void) cls; | 125 | (void) cls; |
125 | fprintf (stdout, | 126 | fprintf (stdout, "%s\n", section); |
126 | "%s\n", | ||
127 | section); | ||
128 | } | 127 | } |
129 | 128 | ||
130 | 129 | ||
@@ -149,45 +148,48 @@ run (void *cls, | |||
149 | 148 | ||
150 | (void) cls; | 149 | (void) cls; |
151 | (void) args; | 150 | (void) args; |
151 | if (NULL != backend_check) | ||
152 | { | ||
153 | char *name; | ||
154 | |||
155 | GNUNET_asprintf (&name, "libgnunet_plugin_%s", backend_check); | ||
156 | global_ret = (GNUNET_OK == GNUNET_PLUGIN_test (name)) ? 0 : 77; | ||
157 | GNUNET_free (name); | ||
158 | return; | ||
159 | } | ||
152 | if (rewrite) | 160 | if (rewrite) |
153 | { | 161 | { |
154 | struct GNUNET_CONFIGURATION_Handle *def; | 162 | struct GNUNET_CONFIGURATION_Handle *def; |
155 | 163 | ||
156 | def = GNUNET_CONFIGURATION_create (); | 164 | def = GNUNET_CONFIGURATION_create (); |
157 | if (GNUNET_OK != | 165 | if (GNUNET_OK != GNUNET_CONFIGURATION_load (def, NULL)) |
158 | GNUNET_CONFIGURATION_load (def, NULL)) | ||
159 | { | 166 | { |
160 | fprintf (stderr, | 167 | fprintf (stderr, _ ("failed to load configuration defaults")); |
161 | _("failed to load configuration defaults")); | 168 | global_ret = 1; |
162 | ret = 1; | ||
163 | return; | 169 | return; |
164 | } | 170 | } |
165 | diff = GNUNET_CONFIGURATION_get_diff (def, | 171 | diff = GNUNET_CONFIGURATION_get_diff (def, cfg); |
166 | cfg); | ||
167 | cfg = diff; | 172 | cfg = diff; |
168 | } | 173 | } |
169 | if ( ((! rewrite) && (NULL == section)) || list_sections) | 174 | if (((! rewrite) && (NULL == section)) || list_sections) |
170 | { | 175 | { |
171 | if (! list_sections) | 176 | if (! list_sections) |
172 | { | 177 | { |
173 | fprintf (stderr, | 178 | fprintf (stderr, |
174 | _("%s or %s argument is required\n"), | 179 | _ ("%s or %s argument is required\n"), |
175 | "--section", | 180 | "--section", |
176 | "--list-sections"); | 181 | "--list-sections"); |
177 | ret = 1; | 182 | global_ret = 1; |
178 | } | 183 | } |
179 | else | 184 | else |
180 | { | 185 | { |
181 | fprintf (stderr, | 186 | fprintf (stderr, _ ("The following sections are available:\n")); |
182 | _("The following sections are available:\n")); | 187 | GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL); |
183 | GNUNET_CONFIGURATION_iterate_sections (cfg, | ||
184 | &print_section_name, | ||
185 | NULL); | ||
186 | } | 188 | } |
187 | goto cleanup; | 189 | goto cleanup; |
188 | } | 190 | } |
189 | 191 | ||
190 | if ( (NULL != section) && (NULL == value) ) | 192 | if ((NULL != section) && (NULL == value)) |
191 | { | 193 | { |
192 | if (NULL == option) | 194 | if (NULL == option) |
193 | { | 195 | { |
@@ -200,27 +202,25 @@ run (void *cls, | |||
200 | { | 202 | { |
201 | if (is_filename) | 203 | if (is_filename) |
202 | { | 204 | { |
203 | if (GNUNET_OK != | 205 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, |
204 | GNUNET_CONFIGURATION_get_value_filename (cfg, | 206 | section, |
205 | section, | 207 | option, |
206 | option, | 208 | &value)) |
207 | &value)) | ||
208 | { | 209 | { |
209 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 210 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); |
210 | section, option); | 211 | global_ret = 3; |
211 | ret = 3; | ||
212 | goto cleanup; | 212 | goto cleanup; |
213 | } | 213 | } |
214 | } | 214 | } |
215 | else | 215 | else |
216 | { | 216 | { |
217 | if (GNUNET_OK != | 217 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, |
218 | GNUNET_CONFIGURATION_get_value_string (cfg, section, | 218 | section, |
219 | option, &value)) | 219 | option, |
220 | &value)) | ||
220 | { | 221 | { |
221 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 222 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); |
222 | section, option); | 223 | global_ret = 3; |
223 | ret = 3; | ||
224 | goto cleanup; | 224 | goto cleanup; |
225 | } | 225 | } |
226 | } | 226 | } |
@@ -231,15 +231,12 @@ run (void *cls, | |||
231 | { | 231 | { |
232 | if (NULL == option) | 232 | if (NULL == option) |
233 | { | 233 | { |
234 | fprintf (stderr, _("--option argument required to set value\n")); | 234 | fprintf (stderr, _ ("--option argument required to set value\n")); |
235 | ret = 1; | 235 | global_ret = 1; |
236 | goto cleanup; | 236 | goto cleanup; |
237 | } | 237 | } |
238 | out = GNUNET_CONFIGURATION_dup (cfg); | 238 | out = GNUNET_CONFIGURATION_dup (cfg); |
239 | GNUNET_CONFIGURATION_set_value_string (out, | 239 | GNUNET_CONFIGURATION_set_value_string (out, section, option, value); |
240 | section, | ||
241 | option, | ||
242 | value); | ||
243 | } | 240 | } |
244 | cfg_fn = NULL; | 241 | cfg_fn = NULL; |
245 | if (NULL == cfgfile) | 242 | if (NULL == cfgfile) |
@@ -255,12 +252,11 @@ run (void *cls, | |||
255 | cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file); | 252 | cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file); |
256 | cfgfile = cfg_fn; | 253 | cfgfile = cfg_fn; |
257 | } | 254 | } |
258 | if ( (NULL != diff) || (NULL != out) ) | 255 | if ((NULL != diff) || (NULL != out)) |
259 | { | 256 | { |
260 | if (GNUNET_OK != | 257 | if (GNUNET_OK != |
261 | GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, | 258 | GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, cfgfile)) |
262 | cfgfile)) | 259 | global_ret = 2; |
263 | ret = 2; | ||
264 | } | 260 | } |
265 | GNUNET_free_non_null (cfg_fn); | 261 | GNUNET_free_non_null (cfg_fn); |
266 | if (NULL != out) | 262 | if (NULL != out) |
@@ -279,52 +275,65 @@ cleanup: | |||
279 | * @return 0 ok, 1 on error | 275 | * @return 0 ok, 1 on error |
280 | */ | 276 | */ |
281 | int | 277 | int |
282 | main (int argc, | 278 | main (int argc, char *const *argv) |
283 | char *const *argv) | ||
284 | { | 279 | { |
285 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 280 | struct GNUNET_GETOPT_CommandLineOption options[] = |
286 | GNUNET_GETOPT_option_flag ('f', | 281 | {GNUNET_GETOPT_option_flag ( |
287 | "filename", | 282 | 'f', |
288 | gettext_noop ("interpret option value as a filename (with $-expansion)"), | 283 | "filename", |
289 | &is_filename), | 284 | gettext_noop ("interpret option value as a filename (with $-expansion)"), |
290 | GNUNET_GETOPT_option_string ('s', | 285 | &is_filename), |
291 | "section", | 286 | GNUNET_GETOPT_option_exclusive (GNUNET_GETOPT_option_string ( |
292 | "SECTION", | 287 | 'b', |
293 | gettext_noop ("name of the section to access"), | 288 | "supported-backend", |
294 | §ion), | 289 | "BACKEND", |
295 | GNUNET_GETOPT_option_string ('o', | 290 | gettext_noop ( |
296 | "option", | 291 | "test if the current installation supports the specified BACKEND"), |
297 | "OPTION", | 292 | &backend_check)), |
298 | gettext_noop ("name of the option to access"), | 293 | GNUNET_GETOPT_option_string ('s', |
299 | &option), | 294 | "section", |
300 | GNUNET_GETOPT_option_string ('V', | 295 | "SECTION", |
301 | "value", | 296 | gettext_noop ( |
302 | "VALUE", | 297 | "name of the section to access"), |
303 | gettext_noop ("value to set"), | 298 | §ion), |
304 | &value), | 299 | GNUNET_GETOPT_option_string ('o', |
305 | GNUNET_GETOPT_option_flag ('S', | 300 | "option", |
306 | "list-sections", | 301 | "OPTION", |
307 | gettext_noop ("print available configuration sections"), | 302 | gettext_noop ("name of the option to access"), |
308 | &list_sections), | 303 | &option), |
309 | GNUNET_GETOPT_option_flag ('w', | 304 | GNUNET_GETOPT_option_string ('V', |
310 | "rewrite", | 305 | "value", |
311 | gettext_noop ("write configuration file that only contains delta to defaults"), | 306 | "VALUE", |
312 | &rewrite), | 307 | gettext_noop ("value to set"), |
313 | GNUNET_GETOPT_OPTION_END | 308 | &value), |
314 | }; | 309 | GNUNET_GETOPT_option_flag ('S', |
315 | if (GNUNET_OK != | 310 | "list-sections", |
316 | GNUNET_STRINGS_get_utf8_args (argc, argv, | 311 | gettext_noop ( |
317 | &argc, &argv)) | 312 | "print available configuration sections"), |
313 | &list_sections), | ||
314 | GNUNET_GETOPT_option_flag ( | ||
315 | 'w', | ||
316 | "rewrite", | ||
317 | gettext_noop ( | ||
318 | "write configuration file that only contains delta to defaults"), | ||
319 | &rewrite), | ||
320 | GNUNET_GETOPT_OPTION_END}; | ||
321 | int ret; | ||
322 | |||
323 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
318 | return 2; | 324 | return 2; |
319 | 325 | ||
320 | ret = (GNUNET_OK == | 326 | ret = |
321 | GNUNET_PROGRAM_run (argc, | 327 | GNUNET_PROGRAM_run (argc, |
322 | argv, | 328 | argv, |
323 | "gnunet-config [OPTIONS]", | 329 | "gnunet-config [OPTIONS]", |
324 | gettext_noop ("Manipulate GNUnet configuration files"), | 330 | gettext_noop ("Manipulate GNUnet configuration files"), |
325 | options, | 331 | options, |
326 | &run, NULL)) ? 0 : ret; | 332 | &run, |
327 | GNUNET_free ((void*) argv); | 333 | NULL); |
334 | GNUNET_free ((void *) argv); | ||
335 | if (GNUNET_OK == ret) | ||
336 | return global_ret; | ||
328 | return ret; | 337 | return ret; |
329 | } | 338 | } |
330 | 339 | ||
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c index 5e15eb16a..f4149a398 100644 --- a/src/util/gnunet-scrypt.c +++ b/src/util/gnunet-scrypt.c | |||
@@ -57,15 +57,13 @@ static char *pwfn; | |||
57 | static void | 57 | static void |
58 | shutdown_task (void *cls) | 58 | shutdown_task (void *cls) |
59 | { | 59 | { |
60 | if (sizeof (proof) != | 60 | (void) cls; |
61 | GNUNET_DISK_fn_write (pwfn, | 61 | if (sizeof (proof) != GNUNET_DISK_fn_write (pwfn, |
62 | &proof, | 62 | &proof, |
63 | sizeof (proof), | 63 | sizeof (proof), |
64 | GNUNET_DISK_PERM_USER_READ | | 64 | GNUNET_DISK_PERM_USER_READ | |
65 | GNUNET_DISK_PERM_USER_WRITE)) | 65 | GNUNET_DISK_PERM_USER_WRITE)) |
66 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | 66 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", pwfn); |
67 | "write", | ||
68 | pwfn); | ||
69 | } | 67 | } |
70 | 68 | ||
71 | 69 | ||
@@ -77,17 +75,18 @@ shutdown_task (void *cls) | |||
77 | * @param result where to write the resulting hash | 75 | * @param result where to write the resulting hash |
78 | */ | 76 | */ |
79 | static void | 77 | static void |
80 | pow_hash (const void *buf, | 78 | pow_hash (const void *buf, size_t buf_len, struct GNUNET_HashCode *result) |
81 | size_t buf_len, | ||
82 | struct GNUNET_HashCode *result) | ||
83 | { | 79 | { |
84 | GNUNET_break (0 == | 80 | GNUNET_break ( |
85 | gcry_kdf_derive (buf, buf_len, | 81 | 0 == gcry_kdf_derive (buf, |
86 | GCRY_KDF_SCRYPT, | 82 | buf_len, |
87 | 1 /* subalgo */, | 83 | GCRY_KDF_SCRYPT, |
88 | "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"), | 84 | 1 /* subalgo */, |
89 | 2 /* iterations; keep cost of individual op small */, | 85 | "gnunet-proof-of-work", |
90 | sizeof (struct GNUNET_HashCode), result)); | 86 | strlen ("gnunet-proof-of-work"), |
87 | 2 /* iterations; keep cost of individual op small */, | ||
88 | sizeof (struct GNUNET_HashCode), | ||
89 | result)); | ||
91 | } | 90 | } |
92 | 91 | ||
93 | 92 | ||
@@ -118,7 +117,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash) | |||
118 | static void | 117 | static void |
119 | find_proof (void *cls) | 118 | find_proof (void *cls) |
120 | { | 119 | { |
121 | #define ROUND_SIZE 10 | 120 | #define ROUND_SIZE 10 |
122 | uint64_t counter; | 121 | uint64_t counter; |
123 | char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + | 122 | char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + |
124 | sizeof (uint64_t)] GNUNET_ALIGN; | 123 | sizeof (uint64_t)] GNUNET_ALIGN; |
@@ -127,12 +126,14 @@ find_proof (void *cls) | |||
127 | struct GNUNET_TIME_Absolute timestamp; | 126 | struct GNUNET_TIME_Absolute timestamp; |
128 | struct GNUNET_TIME_Relative elapsed; | 127 | struct GNUNET_TIME_Relative elapsed; |
129 | 128 | ||
129 | (void) cls; | ||
130 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 130 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
131 | "Got Proof of Work %llu\n", | 131 | "Got Proof of Work %llu\n", |
132 | (unsigned long long) proof); | 132 | (unsigned long long) proof); |
133 | proof_task = NULL; | 133 | proof_task = NULL; |
134 | GNUNET_memcpy (&buf[sizeof (uint64_t)], &pub, | 134 | GNUNET_memcpy (&buf[sizeof (uint64_t)], |
135 | sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); | 135 | &pub, |
136 | sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); | ||
136 | i = 0; | 137 | i = 0; |
137 | counter = proof; | 138 | counter = proof; |
138 | timestamp = GNUNET_TIME_absolute_get (); | 139 | timestamp = GNUNET_TIME_absolute_get (); |
@@ -143,7 +144,8 @@ find_proof (void *cls) | |||
143 | if (nse_work_required <= count_leading_zeroes (&result)) | 144 | if (nse_work_required <= count_leading_zeroes (&result)) |
144 | { | 145 | { |
145 | proof = counter; | 146 | proof = counter; |
146 | FPRINTF (stdout, "Proof of work found: %llu!\n", | 147 | FPRINTF (stdout, |
148 | "Proof of work found: %llu!\n", | ||
147 | (unsigned long long) proof); | 149 | (unsigned long long) proof); |
148 | GNUNET_SCHEDULER_shutdown (); | 150 | GNUNET_SCHEDULER_shutdown (); |
149 | return; | 151 | return; |
@@ -159,7 +161,8 @@ find_proof (void *cls) | |||
159 | GNUNET_STRINGS_relative_time_to_string (elapsed, 0)); | 161 | GNUNET_STRINGS_relative_time_to_string (elapsed, 0)); |
160 | if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE)) | 162 | if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE)) |
161 | { | 163 | { |
162 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n", | 164 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
165 | "Testing proofs currently at %llu\n", | ||
163 | (unsigned long long) counter); | 166 | (unsigned long long) counter); |
164 | /* remember progress every 100 rounds */ | 167 | /* remember progress every 100 rounds */ |
165 | proof = counter; | 168 | proof = counter; |
@@ -171,8 +174,9 @@ find_proof (void *cls) | |||
171 | } | 174 | } |
172 | proof_task = | 175 | proof_task = |
173 | GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay, | 176 | GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay, |
174 | GNUNET_SCHEDULER_PRIORITY_IDLE, | 177 | GNUNET_SCHEDULER_PRIORITY_IDLE, |
175 | &find_proof, NULL); | 178 | &find_proof, |
179 | NULL); | ||
176 | } | 180 | } |
177 | 181 | ||
178 | 182 | ||
@@ -193,54 +197,46 @@ run (void *cls, | |||
193 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; | 197 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; |
194 | char *pids; | 198 | char *pids; |
195 | 199 | ||
200 | (void) cls; | ||
201 | (void) args; | ||
202 | (void) cfgfile; | ||
196 | cfg = config; | 203 | cfg = config; |
197 | /* load proof of work */ | 204 | /* load proof of work */ |
198 | if (NULL == pwfn) | 205 | if (NULL == pwfn) |
199 | { | 206 | { |
200 | if (GNUNET_OK != | 207 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, |
201 | GNUNET_CONFIGURATION_get_value_filename (cfg, | 208 | "NSE", |
202 | "NSE", | 209 | "PROOFFILE", |
203 | "PROOFFILE", | 210 | &pwfn)) |
204 | &pwfn)) | ||
205 | { | 211 | { |
206 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 212 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE"); |
207 | "NSE", | ||
208 | "PROOFFILE"); | ||
209 | GNUNET_SCHEDULER_shutdown (); | 213 | GNUNET_SCHEDULER_shutdown (); |
210 | return; | 214 | return; |
211 | } | 215 | } |
212 | } | 216 | } |
213 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 217 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn); |
214 | "Proof of Work file: %s\n", | 218 | if ((GNUNET_YES != GNUNET_DISK_file_test (pwfn)) || |
215 | pwfn); | 219 | (sizeof (proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof)))) |
216 | if ( (GNUNET_YES != GNUNET_DISK_file_test (pwfn)) || | ||
217 | (sizeof (proof) != | ||
218 | GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof)))) | ||
219 | proof = 0; | 220 | proof = 0; |
220 | 221 | ||
221 | /* load private key */ | 222 | /* load private key */ |
222 | if (NULL == pkfn) | 223 | if (NULL == pkfn) |
223 | { | 224 | { |
224 | if (GNUNET_OK != | 225 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, |
225 | GNUNET_CONFIGURATION_get_value_filename (cfg, | 226 | "PEER", |
226 | "PEER", | 227 | "PRIVATE_KEY", |
227 | "PRIVATE_KEY", | 228 | &pkfn)) |
228 | &pkfn)) | ||
229 | { | 229 | { |
230 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 230 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, |
231 | "PEER", | 231 | "PEER", |
232 | "PRIVATE_KEY"); | 232 | "PRIVATE_KEY"); |
233 | return; | 233 | return; |
234 | } | 234 | } |
235 | } | 235 | } |
236 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 236 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn); |
237 | "Private Key file: %s\n", | ||
238 | pkfn); | ||
239 | if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn))) | 237 | if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn))) |
240 | { | 238 | { |
241 | FPRINTF (stderr, | 239 | FPRINTF (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn); |
242 | _("Loading hostkey from `%s' failed.\n"), | ||
243 | pkfn); | ||
244 | GNUNET_free (pkfn); | 240 | GNUNET_free (pkfn); |
245 | return; | 241 | return; |
246 | } | 242 | } |
@@ -248,32 +244,27 @@ run (void *cls, | |||
248 | GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub); | 244 | GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub); |
249 | GNUNET_free (pk); | 245 | GNUNET_free (pk); |
250 | pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); | 246 | pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); |
251 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 247 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids); |
252 | "Peer ID: %s\n", | ||
253 | pids); | ||
254 | GNUNET_free (pids); | 248 | GNUNET_free (pids); |
255 | 249 | ||
256 | /* get target bit amount */ | 250 | /* get target bit amount */ |
257 | if (0 == nse_work_required) | 251 | if (0 == nse_work_required) |
258 | { | 252 | { |
259 | if (GNUNET_OK != | 253 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, |
260 | GNUNET_CONFIGURATION_get_value_number (cfg, | 254 | "NSE", |
261 | "NSE", | 255 | "WORKBITS", |
262 | "WORKBITS", | 256 | &nse_work_required)) |
263 | &nse_work_required)) | ||
264 | { | 257 | { |
265 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 258 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS"); |
266 | "NSE", | ||
267 | "WORKBITS"); | ||
268 | GNUNET_SCHEDULER_shutdown (); | 259 | GNUNET_SCHEDULER_shutdown (); |
269 | return; | 260 | return; |
270 | } | 261 | } |
271 | if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8) | 262 | if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8) |
272 | { | 263 | { |
273 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, | 264 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, |
274 | "NSE", | 265 | "NSE", |
275 | "WORKBITS", | 266 | "WORKBITS", |
276 | _("Value is too large.\n")); | 267 | _ ("Value is too large.\n")); |
277 | GNUNET_SCHEDULER_shutdown (); | 268 | GNUNET_SCHEDULER_shutdown (); |
278 | return; | 269 | return; |
279 | } | 270 | } |
@@ -283,19 +274,16 @@ run (void *cls, | |||
283 | return; | 274 | return; |
284 | } | 275 | } |
285 | } | 276 | } |
286 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 277 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required); |
287 | "Bits: %llu\n", | ||
288 | nse_work_required); | ||
289 | 278 | ||
290 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 279 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
291 | "Delay between tries: %s\n", | 280 | "Delay between tries: %s\n", |
292 | GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1)); | 281 | GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1)); |
293 | proof_task = | 282 | proof_task = |
294 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, | 283 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, |
295 | &find_proof, | 284 | &find_proof, |
296 | NULL); | 285 | NULL); |
297 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | 286 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); |
298 | NULL); | ||
299 | } | 287 | } |
300 | 288 | ||
301 | 289 | ||
@@ -307,46 +295,51 @@ run (void *cls, | |||
307 | * @return 0 ok, 1 on error | 295 | * @return 0 ok, 1 on error |
308 | */ | 296 | */ |
309 | int | 297 | int |
310 | main (int argc, | 298 | main (int argc, char *const *argv) |
311 | char *const *argv) | ||
312 | { | 299 | { |
313 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 300 | struct GNUNET_GETOPT_CommandLineOption options[] = |
314 | GNUNET_GETOPT_option_ulong ('b', | 301 | {GNUNET_GETOPT_option_ulong ( |
315 | "bits", | 302 | 'b', |
316 | "BITS", | 303 | "bits", |
317 | gettext_noop ("number of bits to require for the proof of work"), | 304 | "BITS", |
318 | &nse_work_required), | 305 | gettext_noop ("number of bits to require for the proof of work"), |
319 | GNUNET_GETOPT_option_filename ('k', | 306 | &nse_work_required), |
320 | "keyfile", | 307 | GNUNET_GETOPT_option_filename ( |
321 | "FILE", | 308 | 'k', |
322 | gettext_noop ("file with private key, otherwise default is used"), | 309 | "keyfile", |
323 | &pkfn), | 310 | "FILE", |
324 | GNUNET_GETOPT_option_filename ('o', | 311 | gettext_noop ("file with private key, otherwise default is used"), |
325 | "outfile", | 312 | &pkfn), |
326 | "FILE", | 313 | GNUNET_GETOPT_option_filename ( |
327 | gettext_noop ("file with proof of work, otherwise default is used"), | 314 | 'o', |
328 | &pwfn), | 315 | "outfile", |
329 | GNUNET_GETOPT_option_relative_time ('t', | 316 | "FILE", |
330 | "timeout", | 317 | gettext_noop ("file with proof of work, otherwise default is used"), |
331 | "TIME", | 318 | &pwfn), |
332 | gettext_noop ("time to wait between calculations"), | 319 | GNUNET_GETOPT_option_relative_time ('t', |
333 | &proof_find_delay), | 320 | "timeout", |
334 | GNUNET_GETOPT_OPTION_END | 321 | "TIME", |
335 | }; | 322 | gettext_noop ( |
323 | "time to wait between calculations"), | ||
324 | &proof_find_delay), | ||
325 | GNUNET_GETOPT_OPTION_END}; | ||
336 | int ret; | 326 | int ret; |
337 | 327 | ||
338 | if (GNUNET_OK != | 328 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) |
339 | GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
340 | return 2; | 329 | return 2; |
341 | 330 | ||
342 | ret = (GNUNET_OK == | 331 | ret = |
343 | GNUNET_PROGRAM_run (argc, argv, | 332 | (GNUNET_OK == |
344 | "gnunet-scrypt [OPTIONS] prooffile", | 333 | GNUNET_PROGRAM_run (argc, |
345 | gettext_noop ("Manipulate GNUnet proof of work files"), | 334 | argv, |
346 | options, | 335 | "gnunet-scrypt [OPTIONS] prooffile", |
347 | &run, | 336 | gettext_noop ("Manipulate GNUnet proof of work files"), |
348 | NULL)) ? 0 : 1; | 337 | options, |
349 | GNUNET_free ((void*) argv); | 338 | &run, |
339 | NULL)) | ||
340 | ? 0 | ||
341 | : 1; | ||
342 | GNUNET_free ((void *) argv); | ||
350 | GNUNET_free_non_null (pwfn); | 343 | GNUNET_free_non_null (pwfn); |
351 | return ret; | 344 | return ret; |
352 | } | 345 | } |
diff --git a/src/util/gnunet-uri.c b/src/util/gnunet-uri.c index 58f9e331c..33ff7b1e6 100644 --- a/src/util/gnunet-uri.c +++ b/src/util/gnunet-uri.c | |||
@@ -53,9 +53,9 @@ maint_child_death (void *cls) | |||
53 | { | 53 | { |
54 | enum GNUNET_OS_ProcessStatusType type; | 54 | enum GNUNET_OS_ProcessStatusType type; |
55 | 55 | ||
56 | if ( (GNUNET_OK != | 56 | (void) cls; |
57 | GNUNET_OS_process_status (p, &type, &exit_code)) || | 57 | if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) || |
58 | (type != GNUNET_OS_PROCESS_EXITED) ) | 58 | (type != GNUNET_OS_PROCESS_EXITED)) |
59 | GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG)); | 59 | GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG)); |
60 | GNUNET_OS_process_destroy (p); | 60 | GNUNET_OS_process_destroy (p); |
61 | } | 61 | } |
@@ -70,56 +70,60 @@ maint_child_death (void *cls) | |||
70 | * @param cfg configuration | 70 | * @param cfg configuration |
71 | */ | 71 | */ |
72 | static void | 72 | static void |
73 | run (void *cls, char *const *args, const char *cfgfile, | 73 | run (void *cls, |
74 | char *const *args, | ||
75 | const char *cfgfile, | ||
74 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 76 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
75 | { | 77 | { |
76 | const char *uri; | 78 | const char *uri; |
77 | const char *slash; | 79 | const char *slash; |
78 | char *subsystem; | 80 | char *subsystem; |
79 | char *program; | 81 | char *program; |
80 | struct GNUNET_SCHEDULER_Task * rt; | 82 | struct GNUNET_SCHEDULER_Task *rt; |
81 | 83 | ||
84 | (void) cls; | ||
85 | (void) cfgfile; | ||
82 | if (NULL == (uri = args[0])) | 86 | if (NULL == (uri = args[0])) |
83 | { | 87 | { |
84 | fprintf (stderr, | 88 | fprintf (stderr, _ ("No URI specified on command line\n")); |
85 | _("No URI specified on command line\n")); | ||
86 | return; | 89 | return; |
87 | } | 90 | } |
88 | if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://"))) | 91 | if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://"))) |
89 | { | 92 | { |
90 | fprintf (stderr, | 93 | fprintf (stderr, |
91 | _("Invalid URI: does not start with `%s'\n"), | 94 | _ ("Invalid URI: does not start with `%s'\n"), |
92 | "gnunet://"); | 95 | "gnunet://"); |
93 | return; | 96 | return; |
94 | } | 97 | } |
95 | uri += strlen ("gnunet://"); | 98 | uri += strlen ("gnunet://"); |
96 | if (NULL == (slash = strchr (uri, '/'))) | 99 | if (NULL == (slash = strchr (uri, '/'))) |
97 | { | 100 | { |
98 | fprintf (stderr, _("Invalid URI: fails to specify subsystem\n")); | 101 | fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n")); |
99 | return; | 102 | return; |
100 | } | 103 | } |
101 | subsystem = GNUNET_strndup (uri, slash - uri); | 104 | subsystem = GNUNET_strndup (uri, slash - uri); |
102 | if (GNUNET_OK != | 105 | if (GNUNET_OK != |
103 | GNUNET_CONFIGURATION_get_value_string (cfg, | 106 | GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program)) |
104 | "uri", | ||
105 | subsystem, | ||
106 | &program)) | ||
107 | { | 107 | { |
108 | fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem); | 108 | fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem); |
109 | GNUNET_free (subsystem); | 109 | GNUNET_free (subsystem); |
110 | return; | 110 | return; |
111 | } | 111 | } |
112 | GNUNET_free (subsystem); | 112 | GNUNET_free (subsystem); |
113 | rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 113 | rt = GNUNET_SCHEDULER_add_read_file ( |
114 | GNUNET_DISK_pipe_handle (sigpipe, | 114 | GNUNET_TIME_UNIT_FOREVER_REL, |
115 | GNUNET_DISK_PIPE_END_READ), | 115 | GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ), |
116 | &maint_child_death, NULL); | 116 | &maint_child_death, |
117 | p = GNUNET_OS_start_process (GNUNET_NO, 0, | 117 | NULL); |
118 | NULL, NULL, NULL, | 118 | p = GNUNET_OS_start_process (GNUNET_NO, |
119 | program, | 119 | 0, |
120 | program, | 120 | NULL, |
121 | args[0], | 121 | NULL, |
122 | NULL); | 122 | NULL, |
123 | program, | ||
124 | program, | ||
125 | args[0], | ||
126 | NULL); | ||
123 | GNUNET_free (program); | 127 | GNUNET_free (program); |
124 | if (NULL == p) | 128 | if (NULL == p) |
125 | GNUNET_SCHEDULER_cancel (rt); | 129 | GNUNET_SCHEDULER_cancel (rt); |
@@ -134,13 +138,15 @@ static void | |||
134 | sighandler_child_death () | 138 | sighandler_child_death () |
135 | { | 139 | { |
136 | static char c; | 140 | static char c; |
137 | int old_errno = errno; /* back-up errno */ | 141 | int old_errno = errno; /* back-up errno */ |
138 | 142 | ||
139 | GNUNET_break (1 == | 143 | GNUNET_break ( |
140 | GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle | 144 | 1 == |
141 | (sigpipe, GNUNET_DISK_PIPE_END_WRITE), | 145 | GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe, |
142 | &c, sizeof (c))); | 146 | GNUNET_DISK_PIPE_END_WRITE), |
143 | errno = old_errno; /* restore errno */ | 147 | &c, |
148 | sizeof (c))); | ||
149 | errno = old_errno; /* restore errno */ | ||
144 | } | 150 | } |
145 | 151 | ||
146 | 152 | ||
@@ -155,8 +161,7 @@ int | |||
155 | main (int argc, char *const *argv) | 161 | main (int argc, char *const *argv) |
156 | { | 162 | { |
157 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 163 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
158 | GNUNET_GETOPT_OPTION_END | 164 | GNUNET_GETOPT_OPTION_END}; |
159 | }; | ||
160 | struct GNUNET_SIGNAL_Context *shc_chld; | 165 | struct GNUNET_SIGNAL_Context *shc_chld; |
161 | int ret; | 166 | int ret; |
162 | 167 | ||
@@ -166,9 +171,14 @@ main (int argc, char *const *argv) | |||
166 | GNUNET_assert (sigpipe != NULL); | 171 | GNUNET_assert (sigpipe != NULL); |
167 | shc_chld = | 172 | shc_chld = |
168 | GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); | 173 | GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); |
169 | ret = GNUNET_PROGRAM_run (argc, argv, "gnunet-uri URI", | 174 | ret = GNUNET_PROGRAM_run (argc, |
170 | gettext_noop ("Perform default-actions for GNUnet URIs"), | 175 | argv, |
171 | options, &run, NULL); | 176 | "gnunet-uri URI", |
177 | gettext_noop ( | ||
178 | "Perform default-actions for GNUnet URIs"), | ||
179 | options, | ||
180 | &run, | ||
181 | NULL); | ||
172 | GNUNET_SIGNAL_handler_uninstall (shc_chld); | 182 | GNUNET_SIGNAL_handler_uninstall (shc_chld); |
173 | shc_chld = NULL; | 183 | shc_chld = NULL; |
174 | GNUNET_DISK_pipe_close (sigpipe); | 184 | GNUNET_DISK_pipe_close (sigpipe); |
diff --git a/src/util/test_common_logging_dummy.c b/src/util/test_common_logging_dummy.c index b8f2bef91..5c3709206 100644 --- a/src/util/test_common_logging_dummy.c +++ b/src/util/test_common_logging_dummy.c | |||
@@ -35,12 +35,20 @@ | |||
35 | * This must be long enough for us to not to mistake skipped log call | 35 | * This must be long enough for us to not to mistake skipped log call |
36 | * on a slow machine for a non-skipped one. | 36 | * on a slow machine for a non-skipped one. |
37 | */ | 37 | */ |
38 | #define OUTPUT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 1000) | 38 | #define OUTPUT_DELAY \ |
39 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 1000) | ||
39 | 40 | ||
40 | static void | 41 | static void |
41 | my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component, | 42 | my_log (void *ctx, |
42 | const char *date, const char *msg) | 43 | enum GNUNET_ErrorType kind, |
44 | const char *component, | ||
45 | const char *date, | ||
46 | const char *msg) | ||
43 | { | 47 | { |
48 | (void) ctx; | ||
49 | (void) kind; | ||
50 | (void) component; | ||
51 | (void) date; | ||
44 | if (strncmp ("test-common-logging-dummy", component, 25) != 0) | 52 | if (strncmp ("test-common-logging-dummy", component, 25) != 0) |
45 | return; | 53 | return; |
46 | FPRINTF (stdout, "%s", msg); | 54 | FPRINTF (stdout, "%s", msg); |
@@ -48,7 +56,7 @@ my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component, | |||
48 | } | 56 | } |
49 | 57 | ||
50 | 58 | ||
51 | #if !defined(GNUNET_CULL_LOGGING) | 59 | #if ! defined(GNUNET_CULL_LOGGING) |
52 | static int | 60 | static int |
53 | expensive_func () | 61 | expensive_func () |
54 | { | 62 | { |
@@ -57,27 +65,36 @@ expensive_func () | |||
57 | #endif | 65 | #endif |
58 | 66 | ||
59 | 67 | ||
60 | #define pr(kind,lvl) {\ | 68 | #define pr(kind, lvl) \ |
61 | struct GNUNET_TIME_Absolute t1, t2;\ | 69 | { \ |
62 | t1 = GNUNET_TIME_absolute_get ();\ | 70 | struct GNUNET_TIME_Absolute t1, t2; \ |
63 | GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\ | 71 | t1 = GNUNET_TIME_absolute_get (); \ |
64 | t2 = GNUNET_TIME_absolute_get ();\ | 72 | GNUNET_log (kind, "L%s %d\n", lvl, expensive_func ()); \ |
65 | printf ("1%s %llu\n", lvl,\ | 73 | t2 = GNUNET_TIME_absolute_get (); \ |
66 | (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value_us); \ | 74 | printf ("1%s %llu\n", \ |
67 | } | 75 | lvl, \ |
76 | (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2) \ | ||
77 | .rel_value_us); \ | ||
78 | } | ||
79 | |||
80 | #define pr2(kind, lvl) \ | ||
81 | { \ | ||
82 | struct GNUNET_TIME_Absolute t1, t2; \ | ||
83 | t1 = GNUNET_TIME_absolute_get (); \ | ||
84 | GNUNET_log (kind, "L%s %d\n", lvl, expensive_func ()); \ | ||
85 | t2 = GNUNET_TIME_absolute_get (); \ | ||
86 | printf ("2%s %llu\n", \ | ||
87 | lvl, \ | ||
88 | (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2) \ | ||
89 | .rel_value_us); \ | ||
90 | } | ||
68 | 91 | ||
69 | #define pr2(kind,lvl) {\ | ||
70 | struct GNUNET_TIME_Absolute t1, t2;\ | ||
71 | t1 = GNUNET_TIME_absolute_get ();\ | ||
72 | GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\ | ||
73 | t2 = GNUNET_TIME_absolute_get ();\ | ||
74 | printf ("2%s %llu\n", lvl,\ | ||
75 | (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value_us); \ | ||
76 | } | ||
77 | 92 | ||
78 | int | 93 | int |
79 | main (int argc, char *argv[]) | 94 | main (int argc, char *argv[]) |
80 | { | 95 | { |
96 | (void) argc; | ||
97 | (void) argv; | ||
81 | /* We set up logging with NULL level - will be overrided by | 98 | /* We set up logging with NULL level - will be overrided by |
82 | * GNUNET_LOG or GNUNET_FORCE_LOG at runtime. | 99 | * GNUNET_LOG or GNUNET_FORCE_LOG at runtime. |
83 | */ | 100 | */ |
@@ -97,6 +114,6 @@ main (int argc, char *argv[]) | |||
97 | pr2 (GNUNET_ERROR_TYPE_INFO, "INFO"); | 114 | pr2 (GNUNET_ERROR_TYPE_INFO, "INFO"); |
98 | pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG"); | 115 | pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG"); |
99 | return 0; | 116 | return 0; |
100 | } /* end of main */ | 117 | } /* end of main */ |
101 | 118 | ||
102 | /* end of test_common_logging_dummy.c */ | 119 | /* end of test_common_logging_dummy.c */ |
diff --git a/src/util/tun.c b/src/util/tun.c index 97fbaa83b..e3643d0c6 100644 --- a/src/util/tun.c +++ b/src/util/tun.c | |||
@@ -44,24 +44,27 @@ | |||
44 | */ | 44 | */ |
45 | void | 45 | void |
46 | GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, | 46 | GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, |
47 | uint8_t protocol, | 47 | uint8_t protocol, |
48 | uint16_t payload_length, | 48 | uint16_t payload_length, |
49 | const struct in_addr *src, | 49 | const struct in_addr *src, |
50 | const struct in_addr *dst) | 50 | const struct in_addr *dst) |
51 | { | 51 | { |
52 | GNUNET_assert (20 == sizeof (struct GNUNET_TUN_IPv4Header)); | 52 | GNUNET_assert (20 == sizeof (struct GNUNET_TUN_IPv4Header)); |
53 | GNUNET_assert (payload_length <= UINT16_MAX - sizeof (struct GNUNET_TUN_IPv4Header)); | 53 | GNUNET_assert (payload_length <= |
54 | UINT16_MAX - sizeof (struct GNUNET_TUN_IPv4Header)); | ||
54 | memset (ip, 0, sizeof (struct GNUNET_TUN_IPv4Header)); | 55 | memset (ip, 0, sizeof (struct GNUNET_TUN_IPv4Header)); |
55 | ip->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; | 56 | ip->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; |
56 | ip->version = 4; | 57 | ip->version = 4; |
57 | ip->total_length = htons (sizeof (struct GNUNET_TUN_IPv4Header) + payload_length); | 58 | ip->total_length = |
58 | ip->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | 59 | htons (sizeof (struct GNUNET_TUN_IPv4Header) + payload_length); |
59 | 65536); | 60 | ip->identification = |
61 | (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 65536); | ||
60 | ip->ttl = FRESH_TTL; | 62 | ip->ttl = FRESH_TTL; |
61 | ip->protocol = protocol; | 63 | ip->protocol = protocol; |
62 | ip->source_address = *src; | 64 | ip->source_address = *src; |
63 | ip->destination_address = *dst; | 65 | ip->destination_address = *dst; |
64 | ip->checksum = GNUNET_CRYPTO_crc16_n (ip, sizeof (struct GNUNET_TUN_IPv4Header)); | 66 | ip->checksum = |
67 | GNUNET_CRYPTO_crc16_n (ip, sizeof (struct GNUNET_TUN_IPv4Header)); | ||
65 | } | 68 | } |
66 | 69 | ||
67 | 70 | ||
@@ -76,13 +79,14 @@ GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, | |||
76 | */ | 79 | */ |
77 | void | 80 | void |
78 | GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip, | 81 | GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip, |
79 | uint8_t protocol, | 82 | uint8_t protocol, |
80 | uint16_t payload_length, | 83 | uint16_t payload_length, |
81 | const struct in6_addr *src, | 84 | const struct in6_addr *src, |
82 | const struct in6_addr *dst) | 85 | const struct in6_addr *dst) |
83 | { | 86 | { |
84 | GNUNET_assert (40 == sizeof (struct GNUNET_TUN_IPv6Header)); | 87 | GNUNET_assert (40 == sizeof (struct GNUNET_TUN_IPv6Header)); |
85 | GNUNET_assert (payload_length <= UINT16_MAX - sizeof (struct GNUNET_TUN_IPv6Header)); | 88 | GNUNET_assert (payload_length <= |
89 | UINT16_MAX - sizeof (struct GNUNET_TUN_IPv6Header)); | ||
86 | memset (ip, 0, sizeof (struct GNUNET_TUN_IPv6Header)); | 90 | memset (ip, 0, sizeof (struct GNUNET_TUN_IPv6Header)); |
87 | ip->version = 6; | 91 | ip->version = 6; |
88 | ip->next_header = protocol; | 92 | ip->next_header = protocol; |
@@ -103,27 +107,29 @@ GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip, | |||
103 | */ | 107 | */ |
104 | void | 108 | void |
105 | GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, | 109 | GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, |
106 | struct GNUNET_TUN_TcpHeader *tcp, | 110 | struct GNUNET_TUN_TcpHeader *tcp, |
107 | const void *payload, | 111 | const void *payload, |
108 | uint16_t payload_length) | 112 | uint16_t payload_length) |
109 | { | 113 | { |
110 | uint32_t sum; | 114 | uint32_t sum; |
111 | uint16_t tmp; | 115 | uint16_t tmp; |
112 | 116 | ||
113 | GNUNET_assert (20 == sizeof (struct GNUNET_TUN_TcpHeader)); | 117 | GNUNET_assert (20 == sizeof (struct GNUNET_TUN_TcpHeader)); |
114 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_IPv4Header) + sizeof (struct GNUNET_TUN_TcpHeader) == | 118 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_IPv4Header) + |
115 | ntohs (ip->total_length)); | 119 | sizeof (struct GNUNET_TUN_TcpHeader) == |
120 | ntohs (ip->total_length)); | ||
116 | GNUNET_assert (IPPROTO_TCP == ip->protocol); | 121 | GNUNET_assert (IPPROTO_TCP == ip->protocol); |
117 | 122 | ||
118 | tcp->crc = 0; | 123 | tcp->crc = 0; |
119 | sum = GNUNET_CRYPTO_crc16_step (0, | 124 | sum = GNUNET_CRYPTO_crc16_step (0, |
120 | &ip->source_address, | 125 | &ip->source_address, |
121 | sizeof (struct in_addr) * 2); | 126 | sizeof (struct in_addr) * 2); |
122 | tmp = htons (IPPROTO_TCP); | 127 | tmp = htons (IPPROTO_TCP); |
123 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_t)); | 128 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_t)); |
124 | tmp = htons (payload_length + sizeof (struct GNUNET_TUN_TcpHeader)); | 129 | tmp = htons (payload_length + sizeof (struct GNUNET_TUN_TcpHeader)); |
125 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_t)); | 130 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_t)); |
126 | sum = GNUNET_CRYPTO_crc16_step (sum, tcp, sizeof (struct GNUNET_TUN_TcpHeader)); | 131 | sum = |
132 | GNUNET_CRYPTO_crc16_step (sum, tcp, sizeof (struct GNUNET_TUN_TcpHeader)); | ||
127 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); | 133 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); |
128 | tcp->crc = GNUNET_CRYPTO_crc16_finish (sum); | 134 | tcp->crc = GNUNET_CRYPTO_crc16_finish (sum); |
129 | } | 135 | } |
@@ -139,25 +145,27 @@ GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, | |||
139 | */ | 145 | */ |
140 | void | 146 | void |
141 | GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, | 147 | GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, |
142 | struct GNUNET_TUN_TcpHeader *tcp, | 148 | struct GNUNET_TUN_TcpHeader *tcp, |
143 | const void *payload, | 149 | const void *payload, |
144 | uint16_t payload_length) | 150 | uint16_t payload_length) |
145 | { | 151 | { |
146 | uint32_t sum; | 152 | uint32_t sum; |
147 | uint32_t tmp; | 153 | uint32_t tmp; |
148 | 154 | ||
149 | GNUNET_assert (20 == sizeof (struct GNUNET_TUN_TcpHeader)); | 155 | GNUNET_assert (20 == sizeof (struct GNUNET_TUN_TcpHeader)); |
150 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_TcpHeader) == | 156 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_TcpHeader) == |
151 | ntohs (ip->payload_length)); | 157 | ntohs (ip->payload_length)); |
152 | GNUNET_assert (IPPROTO_TCP == ip->next_header); | 158 | GNUNET_assert (IPPROTO_TCP == ip->next_header); |
153 | tcp->crc = 0; | 159 | tcp->crc = 0; |
154 | sum = GNUNET_CRYPTO_crc16_step (0, &ip->source_address, 2 * sizeof (struct in6_addr)); | 160 | sum = GNUNET_CRYPTO_crc16_step (0, |
161 | &ip->source_address, | ||
162 | 2 * sizeof (struct in6_addr)); | ||
155 | tmp = htonl (sizeof (struct GNUNET_TUN_TcpHeader) + payload_length); | 163 | tmp = htonl (sizeof (struct GNUNET_TUN_TcpHeader) + payload_length); |
156 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); | 164 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); |
157 | tmp = htonl (IPPROTO_TCP); | 165 | tmp = htonl (IPPROTO_TCP); |
158 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); | 166 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); |
159 | sum = GNUNET_CRYPTO_crc16_step (sum, tcp, | 167 | sum = |
160 | sizeof (struct GNUNET_TUN_TcpHeader)); | 168 | GNUNET_CRYPTO_crc16_step (sum, tcp, sizeof (struct GNUNET_TUN_TcpHeader)); |
161 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); | 169 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); |
162 | tcp->crc = GNUNET_CRYPTO_crc16_finish (sum); | 170 | tcp->crc = GNUNET_CRYPTO_crc16_finish (sum); |
163 | } | 171 | } |
@@ -173,36 +181,31 @@ GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, | |||
173 | */ | 181 | */ |
174 | void | 182 | void |
175 | GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, | 183 | GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, |
176 | struct GNUNET_TUN_UdpHeader *udp, | 184 | struct GNUNET_TUN_UdpHeader *udp, |
177 | const void *payload, | 185 | const void *payload, |
178 | uint16_t payload_length) | 186 | uint16_t payload_length) |
179 | { | 187 | { |
180 | uint32_t sum; | 188 | uint32_t sum; |
181 | uint16_t tmp; | 189 | uint16_t tmp; |
182 | 190 | ||
183 | GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader)); | 191 | GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader)); |
184 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_IPv4Header) + sizeof (struct GNUNET_TUN_UdpHeader) == | 192 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_IPv4Header) + |
185 | ntohs (ip->total_length)); | 193 | sizeof (struct GNUNET_TUN_UdpHeader) == |
194 | ntohs (ip->total_length)); | ||
186 | GNUNET_assert (IPPROTO_UDP == ip->protocol); | 195 | GNUNET_assert (IPPROTO_UDP == ip->protocol); |
187 | 196 | ||
188 | udp->crc = 0; /* technically optional, but we calculate it anyway, just to be sure */ | 197 | udp->crc = |
198 | 0; /* technically optional, but we calculate it anyway, just to be sure */ | ||
189 | sum = GNUNET_CRYPTO_crc16_step (0, | 199 | sum = GNUNET_CRYPTO_crc16_step (0, |
190 | &ip->source_address, | 200 | &ip->source_address, |
191 | sizeof (struct in_addr) * 2); | 201 | sizeof (struct in_addr) * 2); |
192 | tmp = htons (IPPROTO_UDP); | 202 | tmp = htons (IPPROTO_UDP); |
193 | sum = GNUNET_CRYPTO_crc16_step (sum, | 203 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_t)); |
194 | &tmp, | ||
195 | sizeof (uint16_t)); | ||
196 | tmp = htons (sizeof (struct GNUNET_TUN_UdpHeader) + payload_length); | 204 | tmp = htons (sizeof (struct GNUNET_TUN_UdpHeader) + payload_length); |
197 | sum = GNUNET_CRYPTO_crc16_step (sum, | 205 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_t)); |
198 | &tmp, | 206 | sum = |
199 | sizeof (uint16_t)); | 207 | GNUNET_CRYPTO_crc16_step (sum, udp, sizeof (struct GNUNET_TUN_UdpHeader)); |
200 | sum = GNUNET_CRYPTO_crc16_step (sum, | 208 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); |
201 | udp, | ||
202 | sizeof (struct GNUNET_TUN_UdpHeader)); | ||
203 | sum = GNUNET_CRYPTO_crc16_step (sum, | ||
204 | payload, | ||
205 | payload_length); | ||
206 | udp->crc = GNUNET_CRYPTO_crc16_finish (sum); | 209 | udp->crc = GNUNET_CRYPTO_crc16_finish (sum); |
207 | } | 210 | } |
208 | 211 | ||
@@ -217,28 +220,30 @@ GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, | |||
217 | */ | 220 | */ |
218 | void | 221 | void |
219 | GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, | 222 | GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, |
220 | struct GNUNET_TUN_UdpHeader *udp, | 223 | struct GNUNET_TUN_UdpHeader *udp, |
221 | const void *payload, | 224 | const void *payload, |
222 | uint16_t payload_length) | 225 | uint16_t payload_length) |
223 | { | 226 | { |
224 | uint32_t sum; | 227 | uint32_t sum; |
225 | uint32_t tmp; | 228 | uint32_t tmp; |
226 | 229 | ||
227 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_UdpHeader) == | 230 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_UdpHeader) == |
228 | ntohs (ip->payload_length)); | 231 | ntohs (ip->payload_length)); |
229 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_UdpHeader) == | 232 | GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_UdpHeader) == |
230 | ntohs (udp->len)); | 233 | ntohs (udp->len)); |
231 | GNUNET_assert (IPPROTO_UDP == ip->next_header); | 234 | GNUNET_assert (IPPROTO_UDP == ip->next_header); |
232 | 235 | ||
233 | udp->crc = 0; | 236 | udp->crc = 0; |
234 | sum = GNUNET_CRYPTO_crc16_step (0, | 237 | sum = GNUNET_CRYPTO_crc16_step (0, |
235 | &ip->source_address, | 238 | &ip->source_address, |
236 | sizeof (struct in6_addr) * 2); | 239 | sizeof (struct in6_addr) * 2); |
237 | tmp = htons (sizeof (struct GNUNET_TUN_UdpHeader) + payload_length); /* aka udp->len */ | 240 | tmp = htons (sizeof (struct GNUNET_TUN_UdpHeader) + |
241 | payload_length); /* aka udp->len */ | ||
238 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); | 242 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); |
239 | tmp = htons (ip->next_header); | 243 | tmp = htons (ip->next_header); |
240 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); | 244 | sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t)); |
241 | sum = GNUNET_CRYPTO_crc16_step (sum, udp, sizeof (struct GNUNET_TUN_UdpHeader)); | 245 | sum = |
246 | GNUNET_CRYPTO_crc16_step (sum, udp, sizeof (struct GNUNET_TUN_UdpHeader)); | ||
242 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); | 247 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); |
243 | udp->crc = GNUNET_CRYPTO_crc16_finish (sum); | 248 | udp->crc = GNUNET_CRYPTO_crc16_finish (sum); |
244 | } | 249 | } |
@@ -253,16 +258,15 @@ GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, | |||
253 | */ | 258 | */ |
254 | void | 259 | void |
255 | GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp, | 260 | GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp, |
256 | const void *payload, | 261 | const void *payload, |
257 | uint16_t payload_length) | 262 | uint16_t payload_length) |
258 | { | 263 | { |
259 | uint32_t sum; | 264 | uint32_t sum; |
260 | 265 | ||
261 | GNUNET_assert (8 == sizeof (struct GNUNET_TUN_IcmpHeader)); | 266 | GNUNET_assert (8 == sizeof (struct GNUNET_TUN_IcmpHeader)); |
262 | icmp->crc = 0; | 267 | icmp->crc = 0; |
263 | sum = GNUNET_CRYPTO_crc16_step (0, | 268 | sum = |
264 | icmp, | 269 | GNUNET_CRYPTO_crc16_step (0, icmp, sizeof (struct GNUNET_TUN_IcmpHeader)); |
265 | sizeof (struct GNUNET_TUN_IcmpHeader)); | ||
266 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); | 270 | sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); |
267 | icmp->crc = GNUNET_CRYPTO_crc16_finish (sum); | 271 | icmp->crc = GNUNET_CRYPTO_crc16_finish (sum); |
268 | } | 272 | } |
@@ -286,21 +290,22 @@ GNUNET_TUN_sockaddr_cmp (const struct sockaddr *sa, | |||
286 | 290 | ||
287 | switch (sa->sa_family) | 291 | switch (sa->sa_family) |
288 | { | 292 | { |
289 | case AF_INET: | 293 | case AF_INET: { |
290 | { | 294 | const struct sockaddr_in *sa4 = (const struct sockaddr_in *) sa; |
291 | const struct sockaddr_in *sa4 = (const struct sockaddr_in *) sa; | 295 | const struct sockaddr_in *sb4 = (const struct sockaddr_in *) sb; |
292 | const struct sockaddr_in *sb4 = (const struct sockaddr_in *) sb; | 296 | if ((include_port) && (sa4->sin_port != sb4->sin_port)) |
293 | return (sa4->sin_addr.s_addr == sb4->sin_addr.s_addr); | 297 | return GNUNET_NO; |
294 | } | 298 | return (sa4->sin_addr.s_addr == sb4->sin_addr.s_addr); |
295 | case AF_INET6: | 299 | } |
296 | { | 300 | case AF_INET6: { |
297 | const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *) sa; | 301 | const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *) sa; |
298 | const struct sockaddr_in6 *sb6 = (const struct sockaddr_in6 *) sb; | 302 | const struct sockaddr_in6 *sb6 = (const struct sockaddr_in6 *) sb; |
299 | 303 | ||
300 | return (0 == memcmp(&sa6->sin6_addr, | 304 | if ((include_port) && (sa6->sin6_port != sb6->sin6_port)) |
301 | &sb6->sin6_addr, | 305 | return GNUNET_NO; |
302 | sizeof (struct in6_addr))); | 306 | return ( |
303 | } | 307 | 0 == memcmp (&sa6->sin6_addr, &sb6->sin6_addr, sizeof (struct in6_addr))); |
308 | } | ||
304 | default: | 309 | default: |
305 | GNUNET_break (0); | 310 | GNUNET_break (0); |
306 | return GNUNET_SYSERR; | 311 | return GNUNET_SYSERR; |