aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/bio.c133
-rw-r--r--src/util/common_allocation.c199
-rw-r--r--src/util/common_logging.c21
-rw-r--r--src/util/configuration.c451
-rw-r--r--src/util/container_bloomfilter.c205
-rw-r--r--src/util/crypto_ecc_setup.c247
-rw-r--r--src/util/crypto_random.c10
-rw-r--r--src/util/disk.c589
-rw-r--r--src/util/dnsstub.c168
-rw-r--r--src/util/getopt.c32
-rw-r--r--src/util/getopt_helpers.c22
-rw-r--r--src/util/gnunet-ecc.c208
-rw-r--r--src/util/gnunet-resolver.c4
-rw-r--r--src/util/gnunet-scrypt.c4
-rw-r--r--src/util/helper.c221
-rw-r--r--src/util/os_installation.c64
-rw-r--r--src/util/os_priority.c604
-rw-r--r--src/util/perf_crypto_asymmetric.c2
-rw-r--r--src/util/perf_crypto_ecc_dlog.c2
-rw-r--r--src/util/plugin.c41
-rw-r--r--src/util/program.c20
-rw-r--r--src/util/service.c856
-rw-r--r--src/util/strings.c693
-rw-r--r--src/util/test_common_allocation.c2
-rw-r--r--src/util/test_common_logging.c8
-rw-r--r--src/util/test_common_logging_dummy.c2
-rw-r--r--src/util/test_common_logging_runtime_loglevels.c6
-rw-r--r--src/util/test_configuration.c14
-rw-r--r--src/util/test_container_bloomfilter.c30
-rw-r--r--src/util/test_crypto_ecc_dlog.c2
-rw-r--r--src/util/test_crypto_ecdh_eddsa.c2
-rw-r--r--src/util/test_crypto_ecdhe.c2
-rw-r--r--src/util/test_crypto_ecdsa.c18
-rw-r--r--src/util/test_crypto_eddsa.c20
-rw-r--r--src/util/test_crypto_hash.c6
-rw-r--r--src/util/test_crypto_random.c2
-rw-r--r--src/util/test_disk.c16
-rw-r--r--src/util/test_getopt.c119
-rw-r--r--src/util/test_peer.c4
-rw-r--r--src/util/test_resolver_api.c4
-rw-r--r--src/util/test_scheduler.c2
-rw-r--r--src/util/test_scheduler_delay.c14
-rw-r--r--src/util/test_service.c2
-rw-r--r--src/util/test_strings.c2
-rw-r--r--src/util/time.c125
45 files changed, 2364 insertions, 2834 deletions
diff --git a/src/util/bio.c b/src/util/bio.c
index def9b02e1..1df249e40 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -25,7 +25,7 @@
25#include "platform.h" 25#include "platform.h"
26#include "gnunet_util_lib.h" 26#include "gnunet_util_lib.h"
27 27
28#define LOG(kind,...) GNUNET_log_from (kind, "util-bio",__VA_ARGS__) 28#define LOG(kind, ...) GNUNET_log_from (kind, "util-bio", __VA_ARGS__)
29 29
30#ifndef PATH_MAX 30#ifndef PATH_MAX
31/** 31/**
@@ -116,8 +116,7 @@ GNUNET_BIO_read_open (const char *fn)
116 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 116 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
117 */ 117 */
118int 118int
119GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, 119GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
120 char **emsg)
121{ 120{
122 int err; 121 int err;
123 122
@@ -144,7 +143,8 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
144int 143int
145GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, 144GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
146 const char *what, 145 const char *what,
147 void *result, size_t len) 146 void *result,
147 size_t len)
148{ 148{
149 char *dst = result; 149 char *dst = result;
150 size_t min; 150 size_t min;
@@ -162,39 +162,34 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
162 { 162 {
163 if (min > len - pos) 163 if (min > len - pos)
164 min = len - pos; 164 min = len - pos;
165 GNUNET_memcpy (&dst[pos], 165 GNUNET_memcpy (&dst[pos], &h->buffer[h->pos], min);
166 &h->buffer[h->pos],
167 min);
168 h->pos += min; 166 h->pos += min;
169 pos += min; 167 pos += min;
170 } 168 }
171 if (pos == len) 169 if (pos == len)
172 return GNUNET_OK; /* done! */ 170 return GNUNET_OK; /* done! */
173 GNUNET_assert (((off_t) h->have) == h->pos); 171 GNUNET_assert (((off_t) h->have) == h->pos);
174 /* fill buffer */ 172 /* fill buffer */
175 ret = GNUNET_DISK_file_read (h->fd, 173 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
176 h->buffer,
177 h->size);
178 if (-1 == ret) 174 if (-1 == ret)
179 { 175 {
180 GNUNET_asprintf (&h->emsg, 176 GNUNET_asprintf (&h->emsg,
181 _("Error reading `%s': %s"), 177 _ ("Error reading `%s': %s"),
182 what, 178 what,
183 STRERROR (errno)); 179 strerror (errno));
184 return GNUNET_SYSERR; 180 return GNUNET_SYSERR;
185 } 181 }
186 if (0 == ret) 182 if (0 == ret)
187 { 183 {
188 GNUNET_asprintf (&h->emsg, 184 GNUNET_asprintf (&h->emsg,
189 _("Error reading `%s': %s"), 185 _ ("Error reading `%s': %s"),
190 what, 186 what,
191 _("End of file")); 187 _ ("End of file"));
192 return GNUNET_SYSERR; 188 return GNUNET_SYSERR;
193 } 189 }
194 h->pos = 0; 190 h->pos = 0;
195 h->have = ret; 191 h->have = ret;
196 } 192 } while (pos < len); /* should always be true */
197 while (pos < len); /* should always be true */
198 return GNUNET_OK; 193 return GNUNET_OK;
199} 194}
200 195
@@ -245,7 +240,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
245 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) 240 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
246 { 241 {
247 GNUNET_free_non_null (h->emsg); 242 GNUNET_free_non_null (h->emsg);
248 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what); 243 GNUNET_asprintf (&h->emsg, _ ("Error reading length of string `%s'"), what);
249 return GNUNET_SYSERR; 244 return GNUNET_SYSERR;
250 } 245 }
251 if (0 == big) 246 if (0 == big)
@@ -255,8 +250,11 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
255 } 250 }
256 if (big > max_length) 251 if (big > max_length)
257 { 252 {
258 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"), 253 GNUNET_asprintf (&h->emsg,
259 what, big, max_length); 254 _ ("String `%s' longer than allowed (%u > %u)"),
255 what,
256 big,
257 max_length);
260 return GNUNET_SYSERR; 258 return GNUNET_SYSERR;
261 } 259 }
262 buf = GNUNET_malloc (big); 260 buf = GNUNET_malloc (big);
@@ -291,9 +289,7 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
291 char *buf; 289 char *buf;
292 struct GNUNET_CONTAINER_MetaData *meta; 290 struct GNUNET_CONTAINER_MetaData *meta;
293 291
294 if (GNUNET_OK != 292 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, (int32_t *) &size))
295 GNUNET_BIO_read_int32 (h,
296 (int32_t *) & size))
297 return GNUNET_SYSERR; 293 return GNUNET_SYSERR;
298 if (size == 0) 294 if (size == 0)
299 { 295 {
@@ -303,30 +299,23 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
303 if (size > MAX_META_DATA) 299 if (size > MAX_META_DATA)
304 { 300 {
305 GNUNET_asprintf (&h->emsg, 301 GNUNET_asprintf (&h->emsg,
306 _("Serialized metadata `%s' larger than allowed (%u>%u)"), 302 _ ("Serialized metadata `%s' larger than allowed (%u>%u)"),
307 what, 303 what,
308 size, 304 size,
309 MAX_META_DATA); 305 MAX_META_DATA);
310 return GNUNET_SYSERR; 306 return GNUNET_SYSERR;
311 } 307 }
312 buf = GNUNET_malloc (size); 308 buf = GNUNET_malloc (size);
313 if (GNUNET_OK != 309 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
314 GNUNET_BIO_read (h,
315 what,
316 buf,
317 size))
318 { 310 {
319 GNUNET_free (buf); 311 GNUNET_free (buf);
320 return GNUNET_SYSERR; 312 return GNUNET_SYSERR;
321 } 313 }
322 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, 314 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
323 size);
324 if (NULL == meta) 315 if (NULL == meta)
325 { 316 {
326 GNUNET_free (buf); 317 GNUNET_free (buf);
327 GNUNET_asprintf (&h->emsg, 318 GNUNET_asprintf (&h->emsg, _ ("Metadata `%s' failed to deserialize"), what);
328 _("Metadata `%s' failed to deserialize"),
329 what);
330 return GNUNET_SYSERR; 319 return GNUNET_SYSERR;
331 } 320 }
332 GNUNET_free (buf); 321 GNUNET_free (buf);
@@ -346,18 +335,13 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
346 */ 335 */
347int 336int
348GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, 337GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
349 const char *file, 338 const char *file,
350 int line, 339 int line,
351 int32_t * i) 340 int32_t *i)
352{ 341{
353 int32_t big; 342 int32_t big;
354 343
355 if (GNUNET_OK != 344 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
356 GNUNET_BIO_read_fn (h,
357 file,
358 line,
359 &big,
360 sizeof (int32_t)))
361 return GNUNET_SYSERR; 345 return GNUNET_SYSERR;
362 *i = ntohl (big); 346 *i = ntohl (big);
363 return GNUNET_OK; 347 return GNUNET_OK;
@@ -381,12 +365,7 @@ GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
381{ 365{
382 int64_t big; 366 int64_t big;
383 367
384 if (GNUNET_OK != 368 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
385 GNUNET_BIO_read_fn (h,
386 file,
387 line,
388 &big,
389 sizeof (int64_t)))
390 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
391 *i = GNUNET_ntohll (big); 370 *i = GNUNET_ntohll (big);
392 return GNUNET_OK; 371 return GNUNET_OK;
@@ -432,11 +411,12 @@ GNUNET_BIO_write_open (const char *fn)
432 struct GNUNET_DISK_FileHandle *fd; 411 struct GNUNET_DISK_FileHandle *fd;
433 struct GNUNET_BIO_WriteHandle *h; 412 struct GNUNET_BIO_WriteHandle *h;
434 413
435 fd = GNUNET_DISK_file_open (fn, 414 fd =
436 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE 415 GNUNET_DISK_file_open (fn,
437 | GNUNET_DISK_OPEN_CREATE, 416 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE |
438 GNUNET_DISK_PERM_USER_READ | 417 GNUNET_DISK_OPEN_CREATE,
439 GNUNET_DISK_PERM_USER_WRITE); 418 GNUNET_DISK_PERM_USER_READ |
419 GNUNET_DISK_PERM_USER_WRITE);
440 if (NULL == fd) 420 if (NULL == fd)
441 return NULL; 421 return NULL;
442 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); 422 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
@@ -459,8 +439,7 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
459 int ret; 439 int ret;
460 440
461 ret = GNUNET_SYSERR; 441 ret = GNUNET_SYSERR;
462 if ( (NULL != h->fd) && 442 if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))))
463 (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
464 GNUNET_DISK_file_close (h->fd); 443 GNUNET_DISK_file_close (h->fd);
465 GNUNET_free (h); 444 GNUNET_free (h);
466 return ret; 445 return ret;
@@ -479,14 +458,12 @@ GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
479{ 458{
480 ssize_t ret; 459 ssize_t ret;
481 460
482 ret = GNUNET_DISK_file_write (h->fd, 461 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
483 h->buffer,
484 h->have);
485 if (ret != (ssize_t) h->have) 462 if (ret != (ssize_t) h->have)
486 { 463 {
487 GNUNET_DISK_file_close (h->fd); 464 GNUNET_DISK_file_close (h->fd);
488 h->fd = NULL; 465 h->fd = NULL;
489 return GNUNET_SYSERR; /* error */ 466 return GNUNET_SYSERR; /* error */
490 } 467 }
491 h->have = 0; 468 h->have = 0;
492 return GNUNET_OK; 469 return GNUNET_OK;
@@ -503,7 +480,7 @@ GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
503 */ 480 */
504int 481int
505GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, 482GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
506 const void *buffer, 483 const void *buffer,
507 size_t n) 484 size_t n)
508{ 485{
509 const char *src = buffer; 486 const char *src = buffer;
@@ -519,18 +496,15 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
519 min = h->size - h->have; 496 min = h->size - h->have;
520 if (min > n - pos) 497 if (min > n - pos)
521 min = n - pos; 498 min = n - pos;
522 GNUNET_memcpy (&h->buffer[h->have], 499 GNUNET_memcpy (&h->buffer[h->have], &src[pos], min);
523 &src[pos],
524 min);
525 pos += min; 500 pos += min;
526 h->have += min; 501 h->have += min;
527 if (pos == n) 502 if (pos == n)
528 return GNUNET_OK; /* done */ 503 return GNUNET_OK; /* done */
529 GNUNET_assert (h->have == h->size); 504 GNUNET_assert (h->have == h->size);
530 if (GNUNET_OK != GNUNET_BIO_flush (h)) 505 if (GNUNET_OK != GNUNET_BIO_flush (h))
531 return GNUNET_SYSERR; /* error */ 506 return GNUNET_SYSERR; /* error */
532 } 507 } while (pos < n); /* should always be true */
533 while (pos < n); /* should always be true */
534 GNUNET_break (0); 508 GNUNET_break (0);
535 return GNUNET_OK; 509 return GNUNET_OK;
536} 510}
@@ -544,8 +518,7 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
544 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 518 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
545 */ 519 */
546int 520int
547GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, 521GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
548 const char *s)
549{ 522{
550 uint32_t slen; 523 uint32_t slen;
551 524
@@ -575,9 +548,11 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
575 if (m == NULL) 548 if (m == NULL)
576 return GNUNET_BIO_write_int32 (h, 0); 549 return GNUNET_BIO_write_int32 (h, 0);
577 buf = NULL; 550 buf = NULL;
578 size = 551 size = GNUNET_CONTAINER_meta_data_serialize (
579 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, 552 m,
580 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); 553 &buf,
554 MAX_META_DATA,
555 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
581 if (size == -1) 556 if (size == -1)
582 { 557 {
583 GNUNET_free (buf); 558 GNUNET_free (buf);
@@ -602,8 +577,7 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
602 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 577 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
603 */ 578 */
604int 579int
605GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, 580GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
606 int32_t i)
607{ 581{
608 int32_t big; 582 int32_t big;
609 583
@@ -620,8 +594,7 @@ GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
620 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 594 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
621 */ 595 */
622int 596int
623GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, 597GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
624 int64_t i)
625{ 598{
626 int64_t big; 599 int64_t big;
627 600
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c
index 20333ce56..94890a7f4 100644
--- a/src/util/common_allocation.c
+++ b/src/util/common_allocation.c
@@ -32,9 +32,11 @@
32#include <malloc/malloc.h> 32#include <malloc/malloc.h>
33#endif 33#endif
34 34
35#define LOG(kind,...) GNUNET_log_from (kind, "util-common-allocation",__VA_ARGS__) 35#define LOG(kind, ...) \
36 GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)
36 37
37#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-common-allocation", syscall) 38#define LOG_STRERROR(kind, syscall) \
39 GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
38 40
39#ifndef INT_MAX 41#ifndef INT_MAX
40#define INT_MAX 0x7FFFFFFF 42#define INT_MAX 0x7FFFFFFF
@@ -61,24 +63,17 @@ static LONG mem_used = 0;
61 * @return pointer to size bytes of memory 63 * @return pointer to size bytes of memory
62 */ 64 */
63void * 65void *
64GNUNET_xmalloc_ (size_t size, 66GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
65 const char *filename,
66 int linenumber)
67{ 67{
68 void *ret; 68 void *ret;
69 69
70 /* As a security precaution, we generally do not allow very large 70 /* As a security precaution, we generally do not allow very large
71 * allocations using the default 'GNUNET_malloc()' macro */ 71 * allocations using the default 'GNUNET_malloc()' macro */
72 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, 72 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
73 filename, 73 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
74 linenumber);
75 ret = GNUNET_xmalloc_unchecked_ (size,
76 filename,
77 linenumber);
78 if (NULL == ret) 74 if (NULL == ret)
79 { 75 {
80 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 76 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
81 "malloc");
82 GNUNET_assert (0); 77 GNUNET_assert (0);
83 } 78 }
84 return ret; 79 return ret;
@@ -101,21 +96,22 @@ GNUNET_xmalloc_ (size_t size,
101 */ 96 */
102void ** 97void **
103GNUNET_xnew_array_2d_ (size_t n, 98GNUNET_xnew_array_2d_ (size_t n,
104 size_t m, 99 size_t m,
105 size_t elementSize, 100 size_t elementSize,
106 const char *filename, 101 const char *filename,
107 int linenumber) 102 int linenumber)
108{ 103{
109 /* use char pointer internally to avoid void pointer arithmetic warnings */ 104 /* use char pointer internally to avoid void pointer arithmetic warnings */
110 char **ret = GNUNET_xmalloc_ (n * sizeof (void *) + /* 1. dim header */ 105 char **ret = GNUNET_xmalloc_ (n * sizeof (void *) + /* 1. dim header */
111 n * m * elementSize, /* element data */ 106 n * m * elementSize, /* element data */
112 filename, linenumber); 107 filename,
113 108 linenumber);
114 for (size_t i = 0; i < n; i++) 109
115 ret[i] = (char *)ret + /* base address */ 110 for (size_t i = 0; i < n; i++)
116 n * sizeof (void *) + /* skip 1. dim header */ 111 ret[i] = (char *) ret + /* base address */
117 i * m * elementSize; /* skip to 2. dim row header */ 112 n * sizeof (void *) + /* skip 1. dim header */
118 return (void **)ret; 113 i * m * elementSize; /* skip to 2. dim row header */
114 return (void **) ret;
119} 115}
120 116
121 117
@@ -135,29 +131,34 @@ GNUNET_xnew_array_2d_ (size_t n,
135 * @return allocated memory, never NULL 131 * @return allocated memory, never NULL
136 */ 132 */
137void *** 133void ***
138GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize, 134GNUNET_xnew_array_3d_ (size_t n,
139 const char *filename, int linenumber) 135 size_t m,
136 size_t o,
137 size_t elementSize,
138 const char *filename,
139 int linenumber)
140{ 140{
141 /* use char pointer internally to avoid void pointer arithmetic warnings */ 141 /* use char pointer internally to avoid void pointer arithmetic warnings */
142 char ***ret = GNUNET_xmalloc_ (n * sizeof (void **) + /* 1. dim header */ 142 char ***ret = GNUNET_xmalloc_ (n * sizeof (void **) + /* 1. dim header */
143 n * m * sizeof (void *) + /* 2. dim header */ 143 n * m * sizeof (void *) + /* 2. dim header */
144 n * m * o * elementSize, /* element data */ 144 n * m * o * elementSize, /* element data */
145 filename, linenumber); 145 filename,
146 146 linenumber);
147 for (size_t i = 0; i < n; i++) 147
148 { 148 for (size_t i = 0; i < n; i++)
149 /* need to cast to (char *) temporarily for byte level acuracy */ 149 {
150 ret[i] = (char **)((char *)ret + /* base address */ 150 /* need to cast to (char *) temporarily for byte level acuracy */
151 n * sizeof (void **) + /* skip 1. dim header */ 151 ret[i] = (char **) ((char *) ret + /* base address */
152 i * m * sizeof (void *)); /* skip to 2. dim header */ 152 n * sizeof (void **) + /* skip 1. dim header */
153 for (size_t j = 0; j < m; j++) 153 i * m * sizeof (void *)); /* skip to 2. dim header */
154 ret[i][j] = (char *)ret + /* base address */ 154 for (size_t j = 0; j < m; j++)
155 n * sizeof (void **) + /* skip 1. dim header */ 155 ret[i][j] = (char *) ret + /* base address */
156 n * m * sizeof (void *) + /* skip 2. dim header */ 156 n * sizeof (void **) + /* skip 1. dim header */
157 i * m * o * elementSize + /* skip to 2. dim part */ 157 n * m * sizeof (void *) + /* skip 2. dim header */
158 j * o * elementSize; /* skip to 3. dim row data */ 158 i * m * o * elementSize + /* skip to 2. dim part */
159 } 159 j * o * elementSize; /* skip to 3. dim row data */
160 return (void ***)ret; 160 }
161 return (void ***) ret;
161} 162}
162 163
163 164
@@ -174,8 +175,8 @@ GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
174 */ 175 */
175void * 176void *
176GNUNET_xmemdup_ (const void *buf, 177GNUNET_xmemdup_ (const void *buf,
177 size_t size, 178 size_t size,
178 const char *filename, 179 const char *filename,
179 int linenumber) 180 int linenumber)
180{ 181{
181 void *ret; 182 void *ret;
@@ -215,9 +216,7 @@ GNUNET_xmemdup_ (const void *buf,
215 * @return pointer to size bytes of memory, NULL if we do not have enough memory 216 * @return pointer to size bytes of memory, NULL if we do not have enough memory
216 */ 217 */
217void * 218void *
218GNUNET_xmalloc_unchecked_ (size_t size, 219GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
219 const char *filename,
220 int linenumber)
221{ 220{
222 void *result; 221 void *result;
223 222
@@ -256,10 +255,7 @@ GNUNET_xmalloc_unchecked_ (size_t size,
256 * @return pointer to size bytes of memory 255 * @return pointer to size bytes of memory
257 */ 256 */
258void * 257void *
259GNUNET_xrealloc_ (void *ptr, 258GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
260 size_t n,
261 const char *filename,
262 int linenumber)
263{ 259{
264 (void) filename; 260 (void) filename;
265 (void) linenumber; 261 (void) linenumber;
@@ -272,8 +268,7 @@ GNUNET_xrealloc_ (void *ptr,
272 ptr = realloc (ptr, n); 268 ptr = realloc (ptr, n);
273 if ((NULL == ptr) && (n > 0)) 269 if ((NULL == ptr) && (n > 0))
274 { 270 {
275 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 271 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
276 "realloc");
277 GNUNET_assert (0); 272 GNUNET_assert (0);
278 } 273 }
279#ifdef W32_MEM_LIMIT 274#ifdef W32_MEM_LIMIT
@@ -283,10 +278,10 @@ GNUNET_xrealloc_ (void *ptr,
283} 278}
284 279
285 280
286# if __BYTE_ORDER == __LITTLE_ENDIAN 281#if __BYTE_ORDER == __LITTLE_ENDIAN
287#define BAADFOOD_STR "\x0D\xF0\xAD\xBA" 282#define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
288#endif 283#endif
289# if __BYTE_ORDER == __BIG_ENDIAN 284#if __BYTE_ORDER == __BIG_ENDIAN
290#define BAADFOOD_STR "\xBA\xAD\xF0\x0D" 285#define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
291#endif 286#endif
292 287
@@ -311,13 +306,9 @@ GNUNET_xrealloc_ (void *ptr,
311 * @param linenumber where in the code was the call to GNUNET_free 306 * @param linenumber where in the code was the call to GNUNET_free
312 */ 307 */
313void 308void
314GNUNET_xfree_ (void *ptr, 309GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
315 const char *filename,
316 int linenumber)
317{ 310{
318 GNUNET_assert_at (NULL != ptr, 311 GNUNET_assert_at (NULL != ptr, filename, linenumber);
319 filename,
320 linenumber);
321#ifdef W32_MEM_LIMIT 312#ifdef W32_MEM_LIMIT
322 ptr = &((size_t *) ptr)[-1]; 313 ptr = &((size_t *) ptr)[-1];
323 mem_used -= *((size_t *) ptr); 314 mem_used -= *((size_t *) ptr);
@@ -330,9 +321,9 @@ GNUNET_xfree_ (void *ptr,
330 size_t s = M_SIZE (ptr); 321 size_t s = M_SIZE (ptr);
331 size_t i; 322 size_t i;
332 323
333 for (i=0;i<s/8;i++) 324 for (i = 0; i < s / 8; i++)
334 base[i] = baadfood; 325 base[i] = baadfood;
335 GNUNET_memcpy (&base[s/8], &baadfood, s % 8); 326 GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
336 } 327 }
337#endif 328#endif
338#endif 329#endif
@@ -349,31 +340,22 @@ GNUNET_xfree_ (void *ptr,
349 * @return `strdup(@a str)` 340 * @return `strdup(@a str)`
350 */ 341 */
351char * 342char *
352GNUNET_xstrdup_ (const char *str, 343GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
353 const char *filename,
354 int linenumber)
355{ 344{
356 char *res; 345 char *res;
357 size_t slen; 346 size_t slen;
358 347
359 GNUNET_assert_at (str != NULL, 348 GNUNET_assert_at (str != NULL, filename, linenumber);
360 filename,
361 linenumber);
362 slen = strlen (str) + 1; 349 slen = strlen (str) + 1;
363 res = GNUNET_xmalloc_ (slen, 350 res = GNUNET_xmalloc_ (slen, filename, linenumber);
364 filename, 351 GNUNET_memcpy (res, str, slen);
365 linenumber);
366 GNUNET_memcpy (res,
367 str,
368 slen);
369 return res; 352 return res;
370} 353}
371 354
372 355
373#if ! HAVE_STRNLEN 356#if ! HAVE_STRNLEN
374static size_t 357static size_t
375strnlen (const char *s, 358strnlen (const char *s, size_t n)
376 size_t n)
377{ 359{
378 const char *e; 360 const char *e;
379 361
@@ -396,22 +378,17 @@ strnlen (const char *s,
396 */ 378 */
397char * 379char *
398GNUNET_xstrndup_ (const char *str, 380GNUNET_xstrndup_ (const char *str,
399 size_t len, 381 size_t len,
400 const char *filename, 382 const char *filename,
401 int linenumber) 383 int linenumber)
402{ 384{
403 char *res; 385 char *res;
404 386
405 if (0 == len) 387 if (0 == len)
406 return GNUNET_strdup (""); 388 return GNUNET_strdup ("");
407 GNUNET_assert_at (NULL != str, 389 GNUNET_assert_at (NULL != str, filename, linenumber);
408 filename, 390 len = strnlen (str, len);
409 linenumber); 391 res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
410 len = strnlen (str,
411 len);
412 res = GNUNET_xmalloc_ (len + 1,
413 filename,
414 linenumber);
415 GNUNET_memcpy (res, str, len); 392 GNUNET_memcpy (res, str, len);
416 /* res[len] = '\0'; 'malloc' zeros out anyway */ 393 /* res[len] = '\0'; 'malloc' zeros out anyway */
417 return res; 394 return res;
@@ -432,11 +409,11 @@ GNUNET_xstrndup_ (const char *str,
432 */ 409 */
433void 410void
434GNUNET_xgrow_ (void **old, 411GNUNET_xgrow_ (void **old,
435 size_t elementSize, 412 size_t elementSize,
436 unsigned int *oldCount, 413 unsigned int *oldCount,
437 unsigned int newCount, 414 unsigned int newCount,
438 const char *filename, 415 const char *filename,
439 int linenumber) 416 int linenumber)
440{ 417{
441 void *tmp; 418 void *tmp;
442 size_t size; 419 size_t size;
@@ -452,7 +429,7 @@ GNUNET_xgrow_ (void **old,
452 tmp = GNUNET_xmalloc_ (size, filename, linenumber); 429 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
453 if (NULL != *old) 430 if (NULL != *old)
454 { 431 {
455 GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount)); 432 GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN (*oldCount, newCount));
456 } 433 }
457 } 434 }
458 435
@@ -474,20 +451,18 @@ GNUNET_xgrow_ (void **old,
474 * @return number of bytes in `*@a buf`, excluding 0-termination 451 * @return number of bytes in `*@a buf`, excluding 0-termination
475 */ 452 */
476int 453int
477GNUNET_asprintf (char **buf, 454GNUNET_asprintf (char **buf, const char *format, ...)
478 const char *format,
479 ...)
480{ 455{
481 int ret; 456 int ret;
482 va_list args; 457 va_list args;
483 458
484 va_start (args, format); 459 va_start (args, format);
485 ret = VSNPRINTF (NULL, 0, format, args); 460 ret = vsnprintf (NULL, 0, format, args);
486 va_end (args); 461 va_end (args);
487 GNUNET_assert (ret >= 0); 462 GNUNET_assert (ret >= 0);
488 *buf = GNUNET_malloc (ret + 1); 463 *buf = GNUNET_malloc (ret + 1);
489 va_start (args, format); 464 va_start (args, format);
490 ret = VSPRINTF (*buf, format, args); 465 ret = vsprintf (*buf, format, args);
491 va_end (args); 466 va_end (args);
492 return ret; 467 return ret;
493} 468}
@@ -503,21 +478,15 @@ GNUNET_asprintf (char **buf,
503 * @return number of bytes written to buf or negative value on error 478 * @return number of bytes written to buf or negative value on error
504 */ 479 */
505int 480int
506GNUNET_snprintf (char *buf, 481GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
507 size_t size,
508 const char *format, ...)
509{ 482{
510 int ret; 483 int ret;
511 va_list args; 484 va_list args;
512 485
513 va_start (args, format); 486 va_start (args, format);
514 ret = VSNPRINTF (buf, 487 ret = vsnprintf (buf, size, format, args);
515 size,
516 format,
517 args);
518 va_end (args); 488 va_end (args);
519 GNUNET_assert ( (ret >= 0) && 489 GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
520 (((size_t) ret) < size) );
521 return ret; 490 return ret;
522} 491}
523 492
@@ -537,9 +506,7 @@ GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
537 msize = ntohs (msg->size); 506 msize = ntohs (msg->size);
538 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader)); 507 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
539 ret = GNUNET_malloc (msize); 508 ret = GNUNET_malloc (msize);
540 GNUNET_memcpy (ret, 509 GNUNET_memcpy (ret, msg, msize);
541 msg,
542 msize);
543 return ret; 510 return ret;
544} 511}
545 512
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index 19bbbdb81..d3666ae7d 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -107,8 +107,7 @@ static __thread struct GNUNET_AsyncScopeSave current_async_scope;
107 * Note that this message maybe truncated to the first BULK_TRACK_SIZE 107 * Note that this message maybe truncated to the first BULK_TRACK_SIZE
108 * characters, in which case it is NOT 0-terminated! 108 * characters, in which case it is NOT 0-terminated!
109 */ 109 */
110static GNUNET_THREAD_LOCAL char last_bulk[BULK_TRACK_SIZE] 110static GNUNET_THREAD_LOCAL char last_bulk[BULK_TRACK_SIZE] __nonstring;
111 __nonstring;
112 111
113/** 112/**
114 * Type of the last bulk message. 113 * Type of the last bulk message.
@@ -325,7 +324,7 @@ log_rotate (const char *new_name)
325 { 324 {
326 /* Note: can't log errors during logging (recursion!), so this 325 /* Note: can't log errors during logging (recursion!), so this
327 operation MUST silently fail... */ 326 operation MUST silently fail... */
328 (void) UNLINK (discard); 327 (void) unlink (discard);
329 GNUNET_free (discard); 328 GNUNET_free (discard);
330 } 329 }
331 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name); 330 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name);
@@ -377,14 +376,14 @@ setup_log_file (const struct tm *tm)
377 fprintf (stderr, 376 fprintf (stderr,
378 "Failed to create directory for `%s': %s\n", 377 "Failed to create directory for `%s': %s\n",
379 fn, 378 fn,
380 STRERROR (errno)); 379 strerror (errno));
381 return GNUNET_SYSERR; 380 return GNUNET_SYSERR;
382 } 381 }
383#if WINDOWS 382#if WINDOWS
384 altlog_fd = 383 altlog_fd =
385 OPEN (fn, O_APPEND | O_BINARY | O_WRONLY | O_CREAT, _S_IREAD | _S_IWRITE); 384 open (fn, O_APPEND | O_BINARY | O_WRONLY | O_CREAT, _S_IREAD | _S_IWRITE);
386#else 385#else
387 altlog_fd = OPEN (fn, 386 altlog_fd = open (fn,
388 O_APPEND | O_WRONLY | O_CREAT, 387 O_APPEND | O_WRONLY | O_CREAT,
389 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 388 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
390#endif 389#endif
@@ -841,7 +840,7 @@ output_message (enum GNUNET_ErrorType kind,
841 * this way if the output is going to logfiles or robots 840 * this way if the output is going to logfiles or robots
842 * instead. 841 * instead.
843 */ 842 */
844 FPRINTF (GNUNET_stderr, "* %s", msg); 843 fprintf (GNUNET_stderr, "* %s", msg);
845 } 844 }
846 else if (GNUNET_YES == current_async_scope.have_scope) 845 else if (GNUNET_YES == current_async_scope.have_scope)
847 { 846 {
@@ -857,7 +856,7 @@ output_message (enum GNUNET_ErrorType kind,
857 GNUNET_assert (NULL != end); 856 GNUNET_assert (NULL != end);
858 *end = '\0'; 857 *end = '\0';
859 skip_log = 0; 858 skip_log = 0;
860 FPRINTF (GNUNET_stderr, 859 fprintf (GNUNET_stderr,
861 "%s %s(%s) %s %s", 860 "%s %s(%s) %s %s",
862 datestr, 861 datestr,
863 comp, 862 comp,
@@ -867,7 +866,7 @@ output_message (enum GNUNET_ErrorType kind,
867 } 866 }
868 else 867 else
869 { 868 {
870 FPRINTF (GNUNET_stderr, 869 fprintf (GNUNET_stderr,
871 "%s %s %s %s", 870 "%s %s %s %s",
872 datestr, 871 datestr,
873 comp, 872 comp,
@@ -991,7 +990,7 @@ mylog (enum GNUNET_ErrorType kind,
991 va_list vacp; 990 va_list vacp;
992 991
993 va_copy (vacp, va); 992 va_copy (vacp, va);
994 size = VSNPRINTF (NULL, 0, message, vacp) + 1; 993 size = vsnprintf (NULL, 0, message, vacp) + 1;
995 GNUNET_assert (0 != size); 994 GNUNET_assert (0 != size);
996 va_end (vacp); 995 va_end (vacp);
997 memset (date, 0, DATE_STR_SIZE); 996 memset (date, 0, DATE_STR_SIZE);
@@ -1065,7 +1064,7 @@ mylog (enum GNUNET_ErrorType kind,
1065 abort (); 1064 abort ();
1066 } 1065 }
1067#endif 1066#endif
1068 VSNPRINTF (buf, size, message, va); 1067 vsnprintf (buf, size, message, va);
1069#if ! (defined(GNUNET_CULL_LOGGING) || TALER_WALLET_ONLY) 1068#if ! (defined(GNUNET_CULL_LOGGING) || TALER_WALLET_ONLY)
1070 if (NULL != tmptr) 1069 if (NULL != tmptr)
1071 (void) setup_log_file (tmptr); 1070 (void) setup_log_file (tmptr);
diff --git a/src/util/configuration.c b/src/util/configuration.c
index a38438ba8..f327071de 100644
--- a/src/util/configuration.c
+++ b/src/util/configuration.c
@@ -30,9 +30,10 @@
30#include "gnunet_configuration_lib.h" 30#include "gnunet_configuration_lib.h"
31#include "gnunet_disk_lib.h" 31#include "gnunet_disk_lib.h"
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 34
35#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) 35#define LOG_STRERROR_FILE(kind, syscall, filename) \
36 GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36 37
37/** 38/**
38 * @brief configuration entry 39 * @brief configuration entry
@@ -95,7 +96,6 @@ struct GNUNET_CONFIGURATION_Handle
95 * #GNUNET_SYSERR on error (i.e. last save failed) 96 * #GNUNET_SYSERR on error (i.e. last save failed)
96 */ 97 */
97 int dirty; 98 int dirty;
98
99}; 99};
100 100
101 101
@@ -152,23 +152,20 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
152 */ 152 */
153int 153int
154GNUNET_CONFIGURATION_parse_and_run (const char *filename, 154GNUNET_CONFIGURATION_parse_and_run (const char *filename,
155 GNUNET_CONFIGURATION_Callback cb, 155 GNUNET_CONFIGURATION_Callback cb,
156 void *cb_cls) 156 void *cb_cls)
157{ 157{
158 struct GNUNET_CONFIGURATION_Handle *cfg; 158 struct GNUNET_CONFIGURATION_Handle *cfg;
159 int ret; 159 int ret;
160 160
161 cfg = GNUNET_CONFIGURATION_create (); 161 cfg = GNUNET_CONFIGURATION_create ();
162 if (GNUNET_OK != 162 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, filename))
163 GNUNET_CONFIGURATION_load (cfg,
164 filename))
165 { 163 {
166 GNUNET_break (0); 164 GNUNET_break (0);
167 GNUNET_CONFIGURATION_destroy (cfg); 165 GNUNET_CONFIGURATION_destroy (cfg);
168 return GNUNET_SYSERR; 166 return GNUNET_SYSERR;
169 } 167 }
170 ret = cb (cb_cls, 168 ret = cb (cb_cls, cfg);
171 cfg);
172 GNUNET_CONFIGURATION_destroy (cfg); 169 GNUNET_CONFIGURATION_destroy (cfg);
173 return ret; 170 return ret;
174} 171}
@@ -187,9 +184,9 @@ GNUNET_CONFIGURATION_parse_and_run (const char *filename,
187 */ 184 */
188int 185int
189GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg, 186GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
190 const char *mem, 187 const char *mem,
191 size_t size, 188 size_t size,
192 const char *basedir) 189 const char *basedir)
193{ 190{
194 char *line; 191 char *line;
195 char *line_orig; 192 char *line_orig;
@@ -224,7 +221,8 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
224 } 221 }
225 else 222 else
226 { 223 {
227 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes])); 224 line_orig =
225 GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
228 r_bytes += line_size + 1; 226 r_bytes += line_size + 1;
229 } 227 }
230 line = line_orig; 228 line = line_orig;
@@ -246,51 +244,46 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
246 continue; 244 continue;
247 245
248 /* remove tailing whitespace */ 246 /* remove tailing whitespace */
249 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--) 247 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));
248 i--)
250 line[i] = '\0'; 249 line[i] = '\0';
251 250
252 /* remove leading whitespace */ 251 /* remove leading whitespace */
253 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++); 252 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++)
253 ;
254 254
255 /* ignore comments */ 255 /* ignore comments */
256 if ( ('#' == line[0]) || ('%' == line[0]) ) 256 if (('#' == line[0]) || ('%' == line[0]))
257 continue; 257 continue;
258 258
259 /* handle special "@INLINE@" directive */ 259 /* handle special "@INLINE@" directive */
260 if (0 == strncasecmp (line, 260 if (0 == strncasecmp (line, "@INLINE@ ", strlen ("@INLINE@ ")))
261 "@INLINE@ ",
262 strlen ("@INLINE@ ")))
263 { 261 {
264 /* @INLINE@ value */ 262 /* @INLINE@ value */
265 value = &line[strlen ("@INLINE@ ")]; 263 value = &line[strlen ("@INLINE@ ")];
266 if (NULL != basedir) 264 if (NULL != basedir)
267 { 265 {
268 char *fn; 266 char *fn;
269 267
270 GNUNET_asprintf (&fn, 268 GNUNET_asprintf (&fn, "%s/%s", basedir, value);
271 "%s/%s", 269 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, fn))
272 basedir, 270 {
273 value); 271 GNUNET_free (fn);
274 if (GNUNET_OK != 272 ret = GNUNET_SYSERR; /* failed to parse included config */
275 GNUNET_CONFIGURATION_parse (cfg, 273 break;
276 fn)) 274 }
277 { 275 GNUNET_free (fn);
278 GNUNET_free (fn);
279 ret = GNUNET_SYSERR; /* failed to parse included config */
280 break;
281 }
282 GNUNET_free (fn);
283 } 276 }
284 else 277 else
285 { 278 {
286 LOG (GNUNET_ERROR_TYPE_DEBUG, 279 LOG (GNUNET_ERROR_TYPE_DEBUG,
287 "Ignoring parsing @INLINE@ configurations, not allowed!\n"); 280 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
288 ret = GNUNET_SYSERR; 281 ret = GNUNET_SYSERR;
289 break; 282 break;
290 } 283 }
291 continue; 284 continue;
292 } 285 }
293 if ( ('[' == line[0]) && (']' == line[line_size - 1]) ) 286 if (('[' == line[0]) && (']' == line[line_size - 1]))
294 { 287 {
295 /* [value] */ 288 /* [value] */
296 line[line_size - 1] = '\0'; 289 line[line_size - 1] = '\0';
@@ -304,23 +297,25 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
304 /* tag = value */ 297 /* tag = value */
305 tag = GNUNET_strndup (line, eq - line); 298 tag = GNUNET_strndup (line, eq - line);
306 /* remove tailing whitespace */ 299 /* remove tailing whitespace */
307 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--) 300 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));
308 tag[i] = '\0'; 301 i--)
302 tag[i] = '\0';
309 303
310 /* Strip whitespace */ 304 /* Strip whitespace */
311 value = eq + 1; 305 value = eq + 1;
312 while (isspace ((unsigned char) value[0])) 306 while (isspace ((unsigned char) value[0]))
313 value++; 307 value++;
314 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--) 308 for (i = strlen (value) - 1;
315 value[i] = '\0'; 309 (i >= 1) && (isspace ((unsigned char) value[i]));
310 i--)
311 value[i] = '\0';
316 312
317 /* remove quotes */ 313 /* remove quotes */
318 i = 0; 314 i = 0;
319 if ( ('"' == value[0]) && 315 if (('"' == value[0]) && ('"' == value[strlen (value) - 1]))
320 ('"' == value[strlen (value) - 1]) )
321 { 316 {
322 value[strlen (value) - 1] = '\0'; 317 value[strlen (value) - 1] = '\0';
323 value++; 318 value++;
324 } 319 }
325 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]); 320 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
326 GNUNET_free (tag); 321 GNUNET_free (tag);
@@ -328,14 +323,14 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
328 } 323 }
329 /* parse error */ 324 /* parse error */
330 LOG (GNUNET_ERROR_TYPE_WARNING, 325 LOG (GNUNET_ERROR_TYPE_WARNING,
331 _("Syntax error while deserializing in line %u\n"), 326 _ ("Syntax error while deserializing in line %u\n"),
332 nr); 327 nr);
333 ret = GNUNET_SYSERR; 328 ret = GNUNET_SYSERR;
334 break; 329 break;
335 } 330 }
336 GNUNET_free_non_null (line_orig); 331 GNUNET_free_non_null (line_orig);
337 GNUNET_free (section); 332 GNUNET_free (section);
338 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) ); 333 GNUNET_assert ((GNUNET_OK != ret) || (r_bytes == size));
339 return ret; 334 return ret;
340} 335}
341 336
@@ -362,55 +357,40 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
362 ssize_t sret; 357 ssize_t sret;
363 358
364 fn = GNUNET_STRINGS_filename_expand (filename); 359 fn = GNUNET_STRINGS_filename_expand (filename);
365 LOG (GNUNET_ERROR_TYPE_DEBUG, 360 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", fn);
366 "Asked to parse config file `%s'\n",
367 fn);
368 if (NULL == fn) 361 if (NULL == fn)
369 return GNUNET_SYSERR; 362 return GNUNET_SYSERR;
370 dirty = cfg->dirty; /* back up value! */ 363 dirty = cfg->dirty; /* back up value! */
371 if (GNUNET_SYSERR == 364 if (GNUNET_SYSERR ==
372 GNUNET_DISK_file_size (fn, 365 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
373 &fs64,
374 GNUNET_YES,
375 GNUNET_YES))
376 { 366 {
377 LOG (GNUNET_ERROR_TYPE_WARNING, 367 LOG (GNUNET_ERROR_TYPE_WARNING,
378 "Error while determining the file size of `%s'\n", 368 "Error while determining the file size of `%s'\n",
379 fn); 369 fn);
380 GNUNET_free (fn); 370 GNUNET_free (fn);
381 return GNUNET_SYSERR; 371 return GNUNET_SYSERR;
382 } 372 }
383 if (fs64 > SIZE_MAX) 373 if (fs64 > SIZE_MAX)
384 { 374 {
385 GNUNET_break (0); /* File size is more than the heap size */ 375 GNUNET_break (0); /* File size is more than the heap size */
386 GNUNET_free (fn); 376 GNUNET_free (fn);
387 return GNUNET_SYSERR; 377 return GNUNET_SYSERR;
388 } 378 }
389 fs = fs64; 379 fs = fs64;
390 mem = GNUNET_malloc (fs); 380 mem = GNUNET_malloc (fs);
391 sret = GNUNET_DISK_fn_read (fn, 381 sret = GNUNET_DISK_fn_read (fn, mem, fs);
392 mem, 382 if ((sret < 0) || (fs != (size_t) sret))
393 fs);
394 if ( (sret < 0) ||
395 (fs != (size_t) sret) )
396 { 383 {
397 LOG (GNUNET_ERROR_TYPE_WARNING, 384 LOG (GNUNET_ERROR_TYPE_WARNING, _ ("Error while reading file `%s'\n"), fn);
398 _("Error while reading file `%s'\n"),
399 fn);
400 GNUNET_free (fn); 385 GNUNET_free (fn);
401 GNUNET_free (mem); 386 GNUNET_free (mem);
402 return GNUNET_SYSERR; 387 return GNUNET_SYSERR;
403 } 388 }
404 LOG (GNUNET_ERROR_TYPE_DEBUG, 389 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
405 "Deserializing contents of file `%s'\n",
406 fn);
407 endsep = strrchr (fn, (int) '/'); 390 endsep = strrchr (fn, (int) '/');
408 if (NULL != endsep) 391 if (NULL != endsep)
409 *endsep = '\0'; 392 *endsep = '\0';
410 ret = GNUNET_CONFIGURATION_deserialize (cfg, 393 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, fn);
411 mem,
412 fs,
413 fn);
414 GNUNET_free (fn); 394 GNUNET_free (fn);
415 GNUNET_free (mem); 395 GNUNET_free (mem);
416 /* restore dirty flag - anything we set in the meantime 396 /* restore dirty flag - anything we set in the meantime
@@ -444,7 +424,7 @@ GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
444 */ 424 */
445char * 425char *
446GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg, 426GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
447 size_t *size) 427 size_t *size)
448{ 428{
449 struct ConfigSection *sec; 429 struct ConfigSection *sec;
450 struct ConfigEntry *ent; 430 struct ConfigEntry *ent;
@@ -466,16 +446,16 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
466 { 446 {
467 if (NULL != ent->val) 447 if (NULL != ent->val)
468 { 448 {
469 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */ 449 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
470 pos = ent->val; 450 pos = ent->val;
471 while (NULL != (pos = strstr (pos, "\n"))) 451 while (NULL != (pos = strstr (pos, "\n")))
472 { 452 {
473 m_size++; 453 m_size++;
474 pos++; 454 pos++;
475 } 455 }
476 /* For each key = value pair we need to add 4 characters (2 456 /* For each key = value pair we need to add 4 characters (2
477 spaces and 1 equal-to character and 1 new line) */ 457 spaces and 1 equal-to character and 1 new line) */
478 m_size += strlen (ent->key) + strlen (ent->val) + 4; 458 m_size += strlen (ent->key) + strlen (ent->val) + 4;
479 } 459 }
480 } 460 }
481 /* A new line after section end */ 461 /* A new line after section end */
@@ -498,23 +478,23 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
498 { 478 {
499 if (NULL != ent->val) 479 if (NULL != ent->val)
500 { 480 {
501 val = GNUNET_malloc (strlen (ent->val) * 2 + 1); 481 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
502 strcpy (val, ent->val); 482 strcpy (val, ent->val);
503 while (NULL != (pos = strstr (val, "\n"))) 483 while (NULL != (pos = strstr (val, "\n")))
504 { 484 {
505 memmove (&pos[2], &pos[1], strlen (&pos[1])); 485 memmove (&pos[2], &pos[1], strlen (&pos[1]));
506 pos[0] = '\\'; 486 pos[0] = '\\';
507 pos[1] = 'n'; 487 pos[1] = 'n';
508 } 488 }
509 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val); 489 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
510 GNUNET_free (val); 490 GNUNET_free (val);
511 GNUNET_memcpy (mem + c_size, cbuf, len); 491 GNUNET_memcpy (mem + c_size, cbuf, len);
512 c_size += len; 492 c_size += len;
513 GNUNET_free (cbuf); 493 GNUNET_free (cbuf);
514 } 494 }
515 } 495 }
516 GNUNET_memcpy (mem + c_size, "\n", 1); 496 GNUNET_memcpy (mem + c_size, "\n", 1);
517 c_size ++; 497 c_size++;
518 sec = sec->next; 498 sec = sec->next;
519 } 499 }
520 GNUNET_assert (c_size == m_size); 500 GNUNET_assert (c_size == m_size);
@@ -548,25 +528,26 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
548 return GNUNET_SYSERR; 528 return GNUNET_SYSERR;
549 } 529 }
550 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size); 530 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
551 sret = GNUNET_DISK_fn_write (fn, cfg_buf, size, 531 sret = GNUNET_DISK_fn_write (fn,
552 GNUNET_DISK_PERM_USER_READ 532 cfg_buf,
553 | GNUNET_DISK_PERM_USER_WRITE 533 size,
554 | GNUNET_DISK_PERM_GROUP_READ 534 GNUNET_DISK_PERM_USER_READ |
555 | GNUNET_DISK_PERM_GROUP_WRITE); 535 GNUNET_DISK_PERM_USER_WRITE |
556 if ( (sret < 0) || 536 GNUNET_DISK_PERM_GROUP_READ |
557 (size != (size_t) sret) ) 537 GNUNET_DISK_PERM_GROUP_WRITE);
538 if ((sret < 0) || (size != (size_t) sret))
558 { 539 {
559 GNUNET_free (fn); 540 GNUNET_free (fn);
560 GNUNET_free (cfg_buf); 541 GNUNET_free (cfg_buf);
561 LOG (GNUNET_ERROR_TYPE_WARNING, 542 LOG (GNUNET_ERROR_TYPE_WARNING,
562 "Writing configuration to file `%s' failed\n", 543 "Writing configuration to file `%s' failed\n",
563 filename); 544 filename);
564 cfg->dirty = GNUNET_SYSERR; /* last write failed */ 545 cfg->dirty = GNUNET_SYSERR; /* last write failed */
565 return GNUNET_SYSERR; 546 return GNUNET_SYSERR;
566 } 547 }
567 GNUNET_free (fn); 548 GNUNET_free (fn);
568 GNUNET_free (cfg_buf); 549 GNUNET_free (cfg_buf);
569 cfg->dirty = GNUNET_NO; /* last write succeeded */ 550 cfg->dirty = GNUNET_NO; /* last write succeeded */
570 return GNUNET_OK; 551 return GNUNET_OK;
571} 552}
572 553
@@ -589,7 +570,7 @@ GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
589 for (spos = cfg->sections; NULL != spos; spos = spos->next) 570 for (spos = cfg->sections; NULL != spos; spos = spos->next)
590 for (epos = spos->entries; NULL != epos; epos = epos->next) 571 for (epos = spos->entries; NULL != epos; epos = epos->next)
591 if (NULL != epos->val) 572 if (NULL != epos->val)
592 iter (iter_cls, spos->name, epos->key, epos->val); 573 iter (iter_cls, spos->name, epos->key, epos->val);
593} 574}
594 575
595 576
@@ -602,11 +583,11 @@ GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
602 * @param iter_cls closure for @a iter 583 * @param iter_cls closure for @a iter
603 */ 584 */
604void 585void
605GNUNET_CONFIGURATION_iterate_section_values (const struct 586GNUNET_CONFIGURATION_iterate_section_values (
606 GNUNET_CONFIGURATION_Handle *cfg, 587 const struct GNUNET_CONFIGURATION_Handle *cfg,
607 const char *section, 588 const char *section,
608 GNUNET_CONFIGURATION_Iterator iter, 589 GNUNET_CONFIGURATION_Iterator iter,
609 void *iter_cls) 590 void *iter_cls)
610{ 591{
611 struct ConfigSection *spos; 592 struct ConfigSection *spos;
612 struct ConfigEntry *epos; 593 struct ConfigEntry *epos;
@@ -630,9 +611,10 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct
630 * @param iter_cls closure for @a iter 611 * @param iter_cls closure for @a iter
631 */ 612 */
632void 613void
633GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg, 614GNUNET_CONFIGURATION_iterate_sections (
634 GNUNET_CONFIGURATION_Section_Iterator iter, 615 const struct GNUNET_CONFIGURATION_Handle *cfg,
635 void *iter_cls) 616 GNUNET_CONFIGURATION_Section_Iterator iter,
617 void *iter_cls)
636{ 618{
637 struct ConfigSection *spos; 619 struct ConfigSection *spos;
638 struct ConfigSection *next; 620 struct ConfigSection *next;
@@ -757,8 +739,8 @@ find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
757 */ 739 */
758static struct ConfigEntry * 740static struct ConfigEntry *
759find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg, 741find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
760 const char *section, 742 const char *section,
761 const char *key) 743 const char *key)
762{ 744{
763 struct ConfigSection *sec; 745 struct ConfigSection *sec;
764 struct ConfigEntry *pos; 746 struct ConfigEntry *pos;
@@ -786,15 +768,14 @@ static void
786compare_entries (void *cls, 768compare_entries (void *cls,
787 const char *section, 769 const char *section,
788 const char *option, 770 const char *option,
789 const char *value) 771 const char *value)
790{ 772{
791 struct DiffHandle *dh = cls; 773 struct DiffHandle *dh = cls;
792 struct ConfigEntry *entNew; 774 struct ConfigEntry *entNew;
793 775
794 entNew = find_entry (dh->cfg_default, section, option); 776 entNew = find_entry (dh->cfg_default, section, option);
795 if ( (NULL != entNew) && 777 if ((NULL != entNew) && (NULL != entNew->val) &&
796 (NULL != entNew->val) && 778 (0 == strcmp (entNew->val, value)))
797 (0 == strcmp (entNew->val, value)) )
798 return; 779 return;
799 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value); 780 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
800} 781}
@@ -808,8 +789,9 @@ compare_entries (void *cls,
808 * @return configuration with only the differences, never NULL 789 * @return configuration with only the differences, never NULL
809 */ 790 */
810struct GNUNET_CONFIGURATION_Handle * 791struct GNUNET_CONFIGURATION_Handle *
811GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default, 792GNUNET_CONFIGURATION_get_diff (
812 const struct GNUNET_CONFIGURATION_Handle *cfg_new) 793 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
794 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
813{ 795{
814 struct DiffHandle diffHandle; 796 struct DiffHandle diffHandle;
815 797
@@ -829,10 +811,10 @@ GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_def
829 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 811 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
830 */ 812 */
831int 813int
832GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle 814GNUNET_CONFIGURATION_write_diffs (
833 *cfg_default, 815 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
834 const struct GNUNET_CONFIGURATION_Handle 816 const struct GNUNET_CONFIGURATION_Handle *cfg_new,
835 *cfg_new, const char *filename) 817 const char *filename)
836{ 818{
837 int ret; 819 int ret;
838 struct GNUNET_CONFIGURATION_Handle *diff; 820 struct GNUNET_CONFIGURATION_Handle *diff;
@@ -854,7 +836,8 @@ GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
854 */ 836 */
855void 837void
856GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg, 838GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
857 const char *section, const char *option, 839 const char *section,
840 const char *option,
858 const char *value) 841 const char *value)
859{ 842{
860 struct ConfigSection *sec; 843 struct ConfigSection *sec;
@@ -904,19 +887,13 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
904void 887void
905GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg, 888GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
906 const char *section, 889 const char *section,
907 const char *option, 890 const char *option,
908 unsigned long long number) 891 unsigned long long number)
909{ 892{
910 char s[64]; 893 char s[64];
911 894
912 GNUNET_snprintf (s, 895 GNUNET_snprintf (s, 64, "%llu", number);
913 64, 896 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
914 "%llu",
915 number);
916 GNUNET_CONFIGURATION_set_value_string (cfg,
917 section,
918 option,
919 s);
920} 897}
921 898
922 899
@@ -930,10 +907,11 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
930 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 907 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
931 */ 908 */
932int 909int
933GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg, 910GNUNET_CONFIGURATION_get_value_number (
934 const char *section, 911 const struct GNUNET_CONFIGURATION_Handle *cfg,
935 const char *option, 912 const char *section,
936 unsigned long long *number) 913 const char *option,
914 unsigned long long *number)
937{ 915{
938 struct ConfigEntry *e; 916 struct ConfigEntry *e;
939 char dummy[2]; 917 char dummy[2];
@@ -942,10 +920,7 @@ GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
942 return GNUNET_SYSERR; 920 return GNUNET_SYSERR;
943 if (NULL == e->val) 921 if (NULL == e->val)
944 return GNUNET_SYSERR; 922 return GNUNET_SYSERR;
945 if (1 != SSCANF (e->val, 923 if (1 != sscanf (e->val, "%llu%1s", number, dummy))
946 "%llu%1s",
947 number,
948 dummy))
949 return GNUNET_SYSERR; 924 return GNUNET_SYSERR;
950 return GNUNET_OK; 925 return GNUNET_OK;
951} 926}
@@ -961,10 +936,11 @@ GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
961 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 936 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
962 */ 937 */
963int 938int
964GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle *cfg, 939GNUNET_CONFIGURATION_get_value_float (
965 const char *section, 940 const struct GNUNET_CONFIGURATION_Handle *cfg,
966 const char *option, 941 const char *section,
967 float *number) 942 const char *option,
943 float *number)
968{ 944{
969 struct ConfigEntry *e; 945 struct ConfigEntry *e;
970 char dummy[2]; 946 char dummy[2];
@@ -973,16 +949,12 @@ GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
973 return GNUNET_SYSERR; 949 return GNUNET_SYSERR;
974 if (NULL == e->val) 950 if (NULL == e->val)
975 return GNUNET_SYSERR; 951 return GNUNET_SYSERR;
976 if (1 != SSCANF (e->val, 952 if (1 != sscanf (e->val, "%f%1s", number, dummy))
977 "%f%1s",
978 number,
979 dummy))
980 return GNUNET_SYSERR; 953 return GNUNET_SYSERR;
981 return GNUNET_OK; 954 return GNUNET_OK;
982} 955}
983 956
984 957
985
986/** 958/**
987 * Get a configuration value that should be a relative time. 959 * Get a configuration value that should be a relative time.
988 * 960 *
@@ -993,27 +965,25 @@ GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
993 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 965 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
994 */ 966 */
995int 967int
996GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg, 968GNUNET_CONFIGURATION_get_value_time (
997 const char *section, 969 const struct GNUNET_CONFIGURATION_Handle *cfg,
998 const char *option, 970 const char *section,
999 struct GNUNET_TIME_Relative *time) 971 const char *option,
972 struct GNUNET_TIME_Relative *time)
1000{ 973{
1001 struct ConfigEntry *e; 974 struct ConfigEntry *e;
1002 int ret; 975 int ret;
1003 976
1004 if (NULL == (e = find_entry (cfg, 977 if (NULL == (e = find_entry (cfg, section, option)))
1005 section,
1006 option)))
1007 return GNUNET_SYSERR; 978 return GNUNET_SYSERR;
1008 if (NULL == e->val) 979 if (NULL == e->val)
1009 return GNUNET_SYSERR; 980 return GNUNET_SYSERR;
1010 ret = GNUNET_STRINGS_fancy_time_to_relative (e->val, 981 ret = GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
1011 time);
1012 if (GNUNET_OK != ret) 982 if (GNUNET_OK != ret)
1013 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 983 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1014 section, 984 section,
1015 option, 985 option,
1016 _("Not a valid relative time specification")); 986 _ ("Not a valid relative time specification"));
1017 return ret; 987 return ret;
1018} 988}
1019 989
@@ -1028,10 +998,11 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *c
1028 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 998 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1029 */ 999 */
1030int 1000int
1031GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg, 1001GNUNET_CONFIGURATION_get_value_size (
1032 const char *section, 1002 const struct GNUNET_CONFIGURATION_Handle *cfg,
1033 const char *option, 1003 const char *section,
1034 unsigned long long *size) 1004 const char *option,
1005 unsigned long long *size)
1035{ 1006{
1036 struct ConfigEntry *e; 1007 struct ConfigEntry *e;
1037 1008
@@ -1054,15 +1025,15 @@ GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *c
1054 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1025 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1055 */ 1026 */
1056int 1027int
1057GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg, 1028GNUNET_CONFIGURATION_get_value_string (
1058 const char *section, 1029 const struct GNUNET_CONFIGURATION_Handle *cfg,
1059 const char *option, 1030 const char *section,
1060 char **value) 1031 const char *option,
1032 char **value)
1061{ 1033{
1062 struct ConfigEntry *e; 1034 struct ConfigEntry *e;
1063 1035
1064 if ( (NULL == (e = find_entry (cfg, section, option))) || 1036 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1065 (NULL == e->val) )
1066 { 1037 {
1067 *value = NULL; 1038 *value = NULL;
1068 return GNUNET_SYSERR; 1039 return GNUNET_SYSERR;
@@ -1085,11 +1056,12 @@ GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
1085 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1056 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1086 */ 1057 */
1087int 1058int
1088GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg, 1059GNUNET_CONFIGURATION_get_value_choice (
1089 const char *section, 1060 const struct GNUNET_CONFIGURATION_Handle *cfg,
1090 const char *option, 1061 const char *section,
1091 const char *const *choices, 1062 const char *option,
1092 const char **value) 1063 const char *const *choices,
1064 const char **value)
1093{ 1065{
1094 struct ConfigEntry *e; 1066 struct ConfigEntry *e;
1095 unsigned int i; 1067 unsigned int i;
@@ -1102,8 +1074,8 @@ GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
1102 if (NULL == choices[i]) 1074 if (NULL == choices[i])
1103 { 1075 {
1104 LOG (GNUNET_ERROR_TYPE_ERROR, 1076 LOG (GNUNET_ERROR_TYPE_ERROR,
1105 _("Configuration value '%s' for '%s'" 1077 _ ("Configuration value '%s' for '%s'"
1106 " in section '%s' is not in set of legal choices\n"), 1078 " in section '%s' is not in set of legal choices\n"),
1107 e->val, 1079 e->val,
1108 option, 1080 option,
1109 section); 1081 section);
@@ -1138,10 +1110,8 @@ GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1138 size_t data_size; 1110 size_t data_size;
1139 1111
1140 if (GNUNET_OK != 1112 if (GNUNET_OK !=
1141 (res = GNUNET_CONFIGURATION_get_value_string (cfg, 1113 (res =
1142 section, 1114 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &enc)))
1143 option,
1144 &enc)))
1145 return res; 1115 return res;
1146 data_size = (strlen (enc) * 5) / 8; 1116 data_size = (strlen (enc) * 5) / 8;
1147 if (data_size != buf_size) 1117 if (data_size != buf_size)
@@ -1150,9 +1120,7 @@ GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1150 return GNUNET_SYSERR; 1120 return GNUNET_SYSERR;
1151 } 1121 }
1152 if (GNUNET_OK != 1122 if (GNUNET_OK !=
1153 GNUNET_STRINGS_string_to_data (enc, 1123 GNUNET_STRINGS_string_to_data (enc, strlen (enc), buf, buf_size))
1154 strlen (enc),
1155 buf, buf_size))
1156 { 1124 {
1157 GNUNET_free (enc); 1125 GNUNET_free (enc);
1158 return GNUNET_SYSERR; 1126 return GNUNET_SYSERR;
@@ -1221,17 +1189,15 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1221 if (depth > 128) 1189 if (depth > 128)
1222 { 1190 {
1223 LOG (GNUNET_ERROR_TYPE_WARNING, 1191 LOG (GNUNET_ERROR_TYPE_WARNING,
1224 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"), 1192 _ (
1193 "Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1225 orig); 1194 orig);
1226 return orig; 1195 return orig;
1227 } 1196 }
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, 1197 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1229 "Asked to $-expand %s\n",
1230 orig);
1231 if ('$' != orig[0]) 1198 if ('$' != orig[0])
1232 { 1199 {
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, 1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1234 "Doesn't start with $ - not expanding\n");
1235 return orig; 1201 return orig;
1236 } 1202 }
1237 erased_char = 0; 1203 erased_char = 0;
@@ -1254,7 +1220,7 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1254 break; 1220 break;
1255 case '\0': 1221 case '\0':
1256 LOG (GNUNET_ERROR_TYPE_WARNING, 1222 LOG (GNUNET_ERROR_TYPE_WARNING,
1257 _("Missing closing `%s' in option `%s'\n"), 1223 _ ("Missing closing `%s' in option `%s'\n"),
1258 "}", 1224 "}",
1259 orig); 1225 orig);
1260 return orig; 1226 return orig;
@@ -1271,8 +1237,7 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1271 { 1237 {
1272 *def = '\0'; 1238 *def = '\0';
1273 def++; 1239 def++;
1274 if ( ('-' == *def) || 1240 if (('-' == *def) || ('=' == *def))
1275 ('=' == *def) )
1276 def++; 1241 def++;
1277 def = GNUNET_strdup (def); 1242 def = GNUNET_strdup (def);
1278 } 1243 }
@@ -1282,10 +1247,8 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1282 start = &orig[1]; 1247 start = &orig[1];
1283 def = NULL; 1248 def = NULL;
1284 i = 0; 1249 i = 0;
1285 while ( (orig[i] != '/') && 1250 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0') &&
1286 (orig[i] != '\\') && 1251 (orig[i] != ' '))
1287 (orig[i] != '\0') &&
1288 (orig[i] != ' ') )
1289 i++; 1252 i++;
1290 if (orig[i] == '\0') 1253 if (orig[i] == '\0')
1291 { 1254 {
@@ -1305,10 +1268,7 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1305 post, 1268 post,
1306 def); 1269 def);
1307 if (GNUNET_OK != 1270 if (GNUNET_OK !=
1308 GNUNET_CONFIGURATION_get_value_string (cfg, 1271 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", start, &prefix))
1309 "PATHS",
1310 start,
1311 &prefix))
1312 { 1272 {
1313 if (NULL == (env = getenv (start))) 1273 if (NULL == (env = getenv (start)))
1314 { 1274 {
@@ -1322,15 +1282,17 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1322 if (erased_pos) 1282 if (erased_pos)
1323 *erased_pos = erased_char; 1283 *erased_pos = erased_char;
1324 LOG (GNUNET_ERROR_TYPE_WARNING, 1284 LOG (GNUNET_ERROR_TYPE_WARNING,
1325 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"), 1285 _ (
1326 start, orig); 1286 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1287 start,
1288 orig);
1327 GNUNET_free (start); 1289 GNUNET_free (start);
1328 return orig; 1290 return orig;
1329 } 1291 }
1330 prefix = GNUNET_strdup (env); 1292 prefix = GNUNET_strdup (env);
1331 } 1293 }
1332 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix); 1294 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1333 if ( (erased_pos) && ('}' != erased_char) ) 1295 if ((erased_pos) && ('}' != erased_char))
1334 { 1296 {
1335 len = strlen (prefix) + 1; 1297 len = strlen (prefix) + 1;
1336 prefix = GNUNET_realloc (prefix, len + 1); 1298 prefix = GNUNET_realloc (prefix, len + 1);
@@ -1364,8 +1326,9 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1364 * @return $-expanded string 1326 * @return $-expanded string
1365 */ 1327 */
1366char * 1328char *
1367GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, 1329GNUNET_CONFIGURATION_expand_dollar (
1368 char *orig) 1330 const struct GNUNET_CONFIGURATION_Handle *cfg,
1331 char *orig)
1369{ 1332{
1370 char *dup; 1333 char *dup;
1371 size_t i; 1334 size_t i;
@@ -1397,18 +1360,18 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cf
1397 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1360 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1398 */ 1361 */
1399int 1362int
1400GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, 1363GNUNET_CONFIGURATION_get_value_filename (
1401 const char *section, 1364 const struct GNUNET_CONFIGURATION_Handle *cfg,
1402 const char *option, 1365 const char *section,
1403 char **value) 1366 const char *option,
1367 char **value)
1404{ 1368{
1405 char *tmp; 1369 char *tmp;
1406 1370
1407 if (GNUNET_OK != 1371 if (GNUNET_OK !=
1408 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp)) 1372 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1409 { 1373 {
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, 1374 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1411 "Failed to retrieve filename\n");
1412 *value = NULL; 1375 *value = NULL;
1413 return GNUNET_SYSERR; 1376 return GNUNET_SYSERR;
1414 } 1377 }
@@ -1431,16 +1394,17 @@ GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handl
1431 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR 1394 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1432 */ 1395 */
1433int 1396int
1434GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg, 1397GNUNET_CONFIGURATION_get_value_yesno (
1435 const char *section, 1398 const struct GNUNET_CONFIGURATION_Handle *cfg,
1436 const char *option) 1399 const char *section,
1400 const char *option)
1437{ 1401{
1438 static const char *yesno[] = { "YES", "NO", NULL }; 1402 static const char *yesno[] = {"YES", "NO", NULL};
1439 const char *val; 1403 const char *val;
1440 int ret; 1404 int ret;
1441 1405
1442 ret = 1406 ret =
1443 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val); 1407 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1444 if (ret == GNUNET_SYSERR) 1408 if (ret == GNUNET_SYSERR)
1445 return ret; 1409 return ret;
1446 if (val == yesno[0]) 1410 if (val == yesno[0])
@@ -1460,11 +1424,12 @@ GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *
1460 * @return number of filenames iterated over, -1 on error 1424 * @return number of filenames iterated over, -1 on error
1461 */ 1425 */
1462int 1426int
1463GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg, 1427GNUNET_CONFIGURATION_iterate_value_filenames (
1464 const char *section, 1428 const struct GNUNET_CONFIGURATION_Handle *cfg,
1465 const char *option, 1429 const char *section,
1466 GNUNET_FileNameCallback cb, 1430 const char *option,
1467 void *cb_cls) 1431 GNUNET_FileNameCallback cb,
1432 void *cb_cls)
1468{ 1433{
1469 char *list; 1434 char *list;
1470 char *pos; 1435 char *pos;
@@ -1590,20 +1555,23 @@ test_match (void *cls, const char *fn)
1590 * #GNUNET_SYSERR on error 1555 * #GNUNET_SYSERR on error
1591 */ 1556 */
1592int 1557int
1593GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg, 1558GNUNET_CONFIGURATION_append_value_filename (
1594 const char *section, 1559 struct GNUNET_CONFIGURATION_Handle *cfg,
1595 const char *option, 1560 const char *section,
1596 const char *value) 1561 const char *option,
1562 const char *value)
1597{ 1563{
1598 char *escaped; 1564 char *escaped;
1599 char *old; 1565 char *old;
1600 char *nw; 1566 char *nw;
1601 1567
1602 if (GNUNET_SYSERR == 1568 if (GNUNET_SYSERR ==
1603 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option, 1569 GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1570 section,
1571 option,
1604 &test_match, 1572 &test_match,
1605 (void *) value)) 1573 (void *) value))
1606 return GNUNET_NO; /* already exists */ 1574 return GNUNET_NO; /* already exists */
1607 if (GNUNET_OK != 1575 if (GNUNET_OK !=
1608 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old)) 1576 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1609 old = GNUNET_strdup (""); 1577 old = GNUNET_strdup ("");
@@ -1634,10 +1602,11 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *
1634 * #GNUNET_SYSERR on error 1602 * #GNUNET_SYSERR on error
1635 */ 1603 */
1636int 1604int
1637GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle 1605GNUNET_CONFIGURATION_remove_value_filename (
1638 *cfg, const char *section, 1606 struct GNUNET_CONFIGURATION_Handle *cfg,
1639 const char *option, 1607 const char *section,
1640 const char *value) 1608 const char *option,
1609 const char *value)
1641{ 1610{
1642 char *list; 1611 char *list;
1643 char *pos; 1612 char *pos;
@@ -1719,16 +1688,14 @@ static int
1719parse_configuration_file (void *cls, const char *filename) 1688parse_configuration_file (void *cls, const char *filename)
1720{ 1689{
1721 struct GNUNET_CONFIGURATION_Handle *cfg = cls; 1690 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1722 char * ext; 1691 char *ext;
1723 int ret; 1692 int ret;
1724 1693
1725 /* Examine file extension */ 1694 /* Examine file extension */
1726 ext = strrchr (filename, '.'); 1695 ext = strrchr (filename, '.');
1727 if ((NULL == ext) || (0 != strcmp (ext, ".conf"))) 1696 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1728 { 1697 {
1729 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1730 "Skipping file `%s'\n",
1731 filename);
1732 return GNUNET_OK; 1699 return GNUNET_OK;
1733 } 1700 }
1734 1701
@@ -1747,13 +1714,11 @@ parse_configuration_file (void *cls, const char *filename)
1747 */ 1714 */
1748int 1715int
1749GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg, 1716GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1750 const char *defaults_d) 1717 const char *defaults_d)
1751{ 1718{
1752 if (GNUNET_SYSERR == 1719 if (GNUNET_SYSERR ==
1753 GNUNET_DISK_directory_scan (defaults_d, 1720 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1754 &parse_configuration_file, 1721 return GNUNET_SYSERR; /* no configuration at all found */
1755 cfg))
1756 return GNUNET_SYSERR; /* no configuration at all found */
1757 return GNUNET_OK; 1722 return GNUNET_OK;
1758} 1723}
1759 1724
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index a41c0cf8a..fe7d86421 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -42,11 +42,17 @@
42#include "platform.h" 42#include "platform.h"
43#include "gnunet_util_lib.h" 43#include "gnunet_util_lib.h"
44 44
45#define LOG(kind,...) GNUNET_log_from (kind, "util-container-bloomfilter", __VA_ARGS__) 45#define LOG(kind, ...) \
46 GNUNET_log_from (kind, "util-container-bloomfilter", __VA_ARGS__)
46 47
47#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-container-bloomfilter", syscall) 48#define LOG_strerror(kind, syscall) \
49 GNUNET_log_from_strerror (kind, "util-container-bloomfilter", syscall)
48 50
49#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-container-bloomfilter", syscall, filename) 51#define LOG_strerror_FILE(kind, syscall, filename) \
52 GNUNET_log_from_strerror_file (kind, \
53 "util-container-bloomfilter", \
54 syscall, \
55 filename)
50 56
51struct GNUNET_CONTAINER_BloomFilter 57struct GNUNET_CONTAINER_BloomFilter
52{ 58{
@@ -75,7 +81,6 @@ struct GNUNET_CONTAINER_BloomFilter
75 * Size of bitArray in bytes 81 * Size of bitArray in bytes
76 */ 82 */
77 size_t bitArraySize; 83 size_t bitArraySize;
78
79}; 84};
80 85
81 86
@@ -86,8 +91,8 @@ struct GNUNET_CONTAINER_BloomFilter
86 * @return addresses set per element in the bf 91 * @return addresses set per element in the bf
87 */ 92 */
88size_t 93size_t
89GNUNET_CONTAINER_bloomfilter_get_element_addresses (const struct GNUNET_CONTAINER_BloomFilter 94GNUNET_CONTAINER_bloomfilter_get_element_addresses (
90 *bf) 95 const struct GNUNET_CONTAINER_BloomFilter *bf)
91{ 96{
92 if (bf == NULL) 97 if (bf == NULL)
93 return 0; 98 return 0;
@@ -102,8 +107,8 @@ GNUNET_CONTAINER_bloomfilter_get_element_addresses (const struct GNUNET_CONTAINE
102 * @return number of bytes used for the data of the bloom filter 107 * @return number of bytes used for the data of the bloom filter
103 */ 108 */
104size_t 109size_t
105GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter 110GNUNET_CONTAINER_bloomfilter_get_size (
106 *bf) 111 const struct GNUNET_CONTAINER_BloomFilter *bf)
107{ 112{
108 if (bf == NULL) 113 if (bf == NULL)
109 return 0; 114 return 0;
@@ -118,10 +123,11 @@ GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
118 * @return copy of the bf 123 * @return copy of the bf
119 */ 124 */
120struct GNUNET_CONTAINER_BloomFilter * 125struct GNUNET_CONTAINER_BloomFilter *
121GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter 126GNUNET_CONTAINER_bloomfilter_copy (
122 *bf) 127 const struct GNUNET_CONTAINER_BloomFilter *bf)
123{ 128{
124 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize, 129 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray,
130 bf->bitArraySize,
125 bf->addressesPerElement); 131 bf->addressesPerElement);
126} 132}
127 133
@@ -193,7 +199,8 @@ testBit (char *bitArray, unsigned int bitIdx)
193 * @param fh A file to keep the 4 bit address usage counters in 199 * @param fh A file to keep the 4 bit address usage counters in
194 */ 200 */
195static void 201static void
196incrementBit (char *bitArray, unsigned int bitIdx, 202incrementBit (char *bitArray,
203 unsigned int bitIdx,
197 const struct GNUNET_DISK_FileHandle *fh) 204 const struct GNUNET_DISK_FileHandle *fh)
198{ 205{
199 off_t fileSlot; 206 off_t fileSlot;
@@ -241,7 +248,8 @@ incrementBit (char *bitArray, unsigned int bitIdx,
241 * @param fh A file to keep the 4bit address usage counters in 248 * @param fh A file to keep the 4bit address usage counters in
242 */ 249 */
243static void 250static void
244decrementBit (char *bitArray, unsigned int bitIdx, 251decrementBit (char *bitArray,
252 unsigned int bitIdx,
245 const struct GNUNET_DISK_FileHandle *fh) 253 const struct GNUNET_DISK_FileHandle *fh)
246{ 254{
247 off_t fileslot; 255 off_t fileslot;
@@ -251,15 +259,16 @@ decrementBit (char *bitArray, unsigned int bitIdx,
251 unsigned int targetLoc; 259 unsigned int targetLoc;
252 260
253 if (GNUNET_DISK_handle_invalid (fh)) 261 if (GNUNET_DISK_handle_invalid (fh))
254 return; /* cannot decrement! */ 262 return; /* cannot decrement! */
255 /* Each char slot in the counter file holds two 4 bit counters */ 263 /* Each char slot in the counter file holds two 4 bit counters */
256 fileslot = bitIdx / 2; 264 fileslot = bitIdx / 2;
257 targetLoc = bitIdx % 2; 265 targetLoc = bitIdx % 2;
258 if (GNUNET_SYSERR == GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET)) 266 if (GNUNET_SYSERR ==
259 { 267 GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
260 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek"); 268 {
261 return; 269 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
262 } 270 return;
271 }
263 if (1 != GNUNET_DISK_file_read (fh, &value, 1)) 272 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
264 value = 0; 273 value = 0;
265 low = value & 0xF; 274 low = value & 0xF;
@@ -285,11 +294,12 @@ decrementBit (char *bitArray, unsigned int bitIdx,
285 } 294 }
286 } 295 }
287 value = ((high << 4) | low); 296 value = ((high << 4) | low);
288 if (GNUNET_SYSERR == GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET)) 297 if (GNUNET_SYSERR ==
289 { 298 GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
290 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek"); 299 {
291 return; 300 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
292 } 301 return;
302 }
293 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1)); 303 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
294} 304}
295 305
@@ -319,13 +329,13 @@ make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size)
319 { 329 {
320 res = GNUNET_DISK_file_write (fh, buffer, sizeof (buffer)); 330 res = GNUNET_DISK_file_write (fh, buffer, sizeof (buffer));
321 if (res >= 0) 331 if (res >= 0)
322 bytesleft -= res; 332 bytesleft -= res;
323 } 333 }
324 else 334 else
325 { 335 {
326 res = GNUNET_DISK_file_write (fh, buffer, bytesleft); 336 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
327 if (res >= 0) 337 if (res >= 0)
328 bytesleft -= res; 338 bytesleft -= res;
329 } 339 }
330 if (GNUNET_SYSERR == res) 340 if (GNUNET_SYSERR == res)
331 return GNUNET_SYSERR; 341 return GNUNET_SYSERR;
@@ -346,7 +356,7 @@ make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size)
346 * @return GNUNET_YES to continue, GNUNET_NO to stop early 356 * @return GNUNET_YES to continue, GNUNET_NO to stop early
347 */ 357 */
348typedef int (*BitIterator) (void *cls, 358typedef int (*BitIterator) (void *cls,
349 const struct GNUNET_CONTAINER_BloomFilter * bf, 359 const struct GNUNET_CONTAINER_BloomFilter *bf,
350 unsigned int bit); 360 unsigned int bit);
351 361
352 362
@@ -361,7 +371,9 @@ typedef int (*BitIterator) (void *cls,
361 */ 371 */
362static void 372static void
363iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf, 373iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
364 BitIterator callback, void *arg, const struct GNUNET_HashCode *key) 374 BitIterator callback,
375 void *arg,
376 const struct GNUNET_HashCode *key)
365{ 377{
366 struct GNUNET_HashCode tmp[2]; 378 struct GNUNET_HashCode tmp[2];
367 int bitCount; 379 int bitCount;
@@ -378,9 +390,10 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
378 while (slot < (sizeof (struct GNUNET_HashCode) / sizeof (uint32_t))) 390 while (slot < (sizeof (struct GNUNET_HashCode) / sizeof (uint32_t)))
379 { 391 {
380 if (GNUNET_YES != 392 if (GNUNET_YES !=
381 callback (arg, bf, 393 callback (arg,
382 ntohl ((((uint32_t *) & tmp[round & 1])[slot])) % 394 bf,
383 ((bf->bitArraySize * 8LL)))) 395 ntohl ((((uint32_t *) &tmp[round & 1])[slot])) %
396 ((bf->bitArraySize * 8LL))))
384 return; 397 return;
385 slot++; 398 slot++;
386 bitCount--; 399 bitCount--;
@@ -389,7 +402,8 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
389 } 402 }
390 if (bitCount > 0) 403 if (bitCount > 0)
391 { 404 {
392 GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (struct GNUNET_HashCode), 405 GNUNET_CRYPTO_hash (&tmp[round & 1],
406 sizeof (struct GNUNET_HashCode),
393 &tmp[(round + 1) & 1]); 407 &tmp[(round + 1) & 1]);
394 round++; 408 round++;
395 slot = 0; 409 slot = 0;
@@ -407,7 +421,8 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
407 * @return GNUNET_YES 421 * @return GNUNET_YES
408 */ 422 */
409static int 423static int
410incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, 424incrementBitCallback (void *cls,
425 const struct GNUNET_CONTAINER_BloomFilter *bf,
411 unsigned int bit) 426 unsigned int bit)
412{ 427{
413 struct GNUNET_CONTAINER_BloomFilter *b = cls; 428 struct GNUNET_CONTAINER_BloomFilter *b = cls;
@@ -426,7 +441,8 @@ incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
426 * @return GNUNET_YES 441 * @return GNUNET_YES
427 */ 442 */
428static int 443static int
429decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, 444decrementBitCallback (void *cls,
445 const struct GNUNET_CONTAINER_BloomFilter *bf,
430 unsigned int bit) 446 unsigned int bit)
431{ 447{
432 struct GNUNET_CONTAINER_BloomFilter *b = cls; 448 struct GNUNET_CONTAINER_BloomFilter *b = cls;
@@ -445,7 +461,8 @@ decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
445 * @return YES if the bit is set, NO if not 461 * @return YES if the bit is set, NO if not
446 */ 462 */
447static int 463static int
448testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, 464testBitCallback (void *cls,
465 const struct GNUNET_CONTAINER_BloomFilter *bf,
449 unsigned int bit) 466 unsigned int bit)
450{ 467{
451 int *arg = cls; 468 int *arg = cls;
@@ -472,7 +489,8 @@ testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
472 * @return the bloomfilter 489 * @return the bloomfilter
473 */ 490 */
474struct GNUNET_CONTAINER_BloomFilter * 491struct GNUNET_CONTAINER_BloomFilter *
475GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, 492GNUNET_CONTAINER_bloomfilter_load (const char *filename,
493 size_t size,
476 unsigned int k) 494 unsigned int k)
477{ 495{
478 struct GNUNET_CONTAINER_BloomFilter *bf; 496 struct GNUNET_CONTAINER_BloomFilter *bf;
@@ -489,25 +507,22 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
489 if (size < BUFFSIZE) 507 if (size < BUFFSIZE)
490 size = BUFFSIZE; 508 size = BUFFSIZE;
491 ui = 1; 509 ui = 1;
492 while ( (ui < size) && 510 while ((ui < size) && (ui * 2 > ui))
493 (ui * 2 > ui) )
494 ui *= 2; 511 ui *= 2;
495 size = ui; /* make sure it's a power of 2 */ 512 size = ui; /* make sure it's a power of 2 */
496 513
497 bf = GNUNET_new (struct GNUNET_CONTAINER_BloomFilter); 514 bf = GNUNET_new (struct GNUNET_CONTAINER_BloomFilter);
498 /* Try to open a bloomfilter file */ 515 /* Try to open a bloomfilter file */
499 if (GNUNET_YES == GNUNET_DISK_file_test (filename)) 516 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
500 bf->fh = 517 bf->fh = GNUNET_DISK_file_open (filename,
501 GNUNET_DISK_file_open (filename, 518 GNUNET_DISK_OPEN_READWRITE,
502 GNUNET_DISK_OPEN_READWRITE, 519 GNUNET_DISK_PERM_USER_READ |
503 GNUNET_DISK_PERM_USER_READ | 520 GNUNET_DISK_PERM_USER_WRITE);
504 GNUNET_DISK_PERM_USER_WRITE);
505 if (NULL != bf->fh) 521 if (NULL != bf->fh)
506 { 522 {
507 /* file existed, try to read it! */ 523 /* file existed, try to read it! */
508 must_read = GNUNET_YES; 524 must_read = GNUNET_YES;
509 if (GNUNET_OK != 525 if (GNUNET_OK != GNUNET_DISK_file_handle_size (bf->fh, &fsize))
510 GNUNET_DISK_file_handle_size (bf->fh, &fsize))
511 { 526 {
512 GNUNET_DISK_file_close (bf->fh); 527 GNUNET_DISK_file_close (bf->fh);
513 GNUNET_free (bf); 528 GNUNET_free (bf);
@@ -516,22 +531,22 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
516 if (0 == fsize) 531 if (0 == fsize)
517 { 532 {
518 /* found existing empty file, just overwrite */ 533 /* found existing empty file, just overwrite */
519 if (GNUNET_OK != 534 if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
520 make_empty_file (bf->fh, size * 4LL))
521 { 535 {
522 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 536 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
523 "write"); 537 GNUNET_DISK_file_close (bf->fh);
524 GNUNET_DISK_file_close (bf->fh); 538 GNUNET_free (bf);
525 GNUNET_free (bf); 539 return NULL;
526 return NULL;
527 } 540 }
528 } 541 }
529 else if (fsize != ((off_t) size) * 4LL) 542 else if (fsize != ((off_t) size) * 4LL)
530 { 543 {
531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 544 GNUNET_log (
532 _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"), 545 GNUNET_ERROR_TYPE_ERROR,
533 (unsigned long long) (size * 4LL), 546 _ (
534 (unsigned long long) fsize); 547 "Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
548 (unsigned long long) (size * 4LL),
549 (unsigned long long) fsize);
535 GNUNET_DISK_file_close (bf->fh); 550 GNUNET_DISK_file_close (bf->fh);
536 GNUNET_free (bf); 551 GNUNET_free (bf);
537 return NULL; 552 return NULL;
@@ -541,21 +556,19 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
541 { 556 {
542 /* file did not exist, don't read, just create */ 557 /* file did not exist, don't read, just create */
543 must_read = GNUNET_NO; 558 must_read = GNUNET_NO;
544 bf->fh = 559 bf->fh = GNUNET_DISK_file_open (filename,
545 GNUNET_DISK_file_open (filename, 560 GNUNET_DISK_OPEN_CREATE |
546 GNUNET_DISK_OPEN_CREATE | 561 GNUNET_DISK_OPEN_READWRITE,
547 GNUNET_DISK_OPEN_READWRITE, 562 GNUNET_DISK_PERM_USER_READ |
548 GNUNET_DISK_PERM_USER_READ | 563 GNUNET_DISK_PERM_USER_WRITE);
549 GNUNET_DISK_PERM_USER_WRITE);
550 if (NULL == bf->fh) 564 if (NULL == bf->fh)
551 { 565 {
552 GNUNET_free (bf); 566 GNUNET_free (bf);
553 return NULL; 567 return NULL;
554 } 568 }
555 if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL)) 569 if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
556 { 570 {
557 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 571 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
558 "write");
559 GNUNET_DISK_file_close (bf->fh); 572 GNUNET_DISK_file_close (bf->fh);
560 GNUNET_free (bf); 573 GNUNET_free (bf);
561 return NULL; 574 return NULL;
@@ -583,14 +596,10 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
583 { 596 {
584 int res; 597 int res;
585 598
586 res = GNUNET_DISK_file_read (bf->fh, 599 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
587 rbuff,
588 BUFFSIZE);
589 if (res == -1) 600 if (res == -1)
590 { 601 {
591 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 602 LOG_strerror_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
592 "read",
593 bf->filename);
594 GNUNET_free (rbuff); 603 GNUNET_free (rbuff);
595 GNUNET_free (bf->filename); 604 GNUNET_free (bf->filename);
596 GNUNET_DISK_file_close (bf->fh); 605 GNUNET_DISK_file_close (bf->fh);
@@ -598,7 +607,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
598 return NULL; 607 return NULL;
599 } 608 }
600 if (res == 0) 609 if (res == 0)
601 break; /* is ok! we just did not use that many bits yet */ 610 break; /* is ok! we just did not use that many bits yet */
602 for (i = 0; i < res; i++) 611 for (i = 0; i < res; i++)
603 { 612 {
604 if ((rbuff[i] & 0x0F) != 0) 613 if ((rbuff[i] & 0x0F) != 0)
@@ -608,7 +617,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
608 } 617 }
609 if (res < BUFFSIZE) 618 if (res < BUFFSIZE)
610 break; 619 break;
611 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */ 620 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
612 } 621 }
613 GNUNET_free (rbuff); 622 GNUNET_free (rbuff);
614 return bf; 623 return bf;
@@ -629,7 +638,8 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
629 * @return the bloomfilter 638 * @return the bloomfilter
630 */ 639 */
631struct GNUNET_CONTAINER_BloomFilter * 640struct GNUNET_CONTAINER_BloomFilter *
632GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size, 641GNUNET_CONTAINER_bloomfilter_init (const char *data,
642 size_t size,
633 unsigned int k) 643 unsigned int k)
634{ 644{
635 struct GNUNET_CONTAINER_BloomFilter *bf; 645 struct GNUNET_CONTAINER_BloomFilter *bf;
@@ -663,9 +673,10 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
663 * @return #GNUNET_SYSERR if the data array is not big enough 673 * @return #GNUNET_SYSERR if the data array is not big enough
664 */ 674 */
665int 675int
666GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct 676GNUNET_CONTAINER_bloomfilter_get_raw_data (
667 GNUNET_CONTAINER_BloomFilter *bf, 677 const struct GNUNET_CONTAINER_BloomFilter *bf,
668 char *data, size_t size) 678 char *data,
679 size_t size)
669{ 680{
670 if (NULL == bf) 681 if (NULL == bf)
671 return GNUNET_SYSERR; 682 return GNUNET_SYSERR;
@@ -721,8 +732,9 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
721 * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not 732 * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not
722 */ 733 */
723int 734int
724GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf, 735GNUNET_CONTAINER_bloomfilter_test (
725 const struct GNUNET_HashCode * e) 736 const struct GNUNET_CONTAINER_BloomFilter *bf,
737 const struct GNUNET_HashCode *e)
726{ 738{
727 int res; 739 int res;
728 740
@@ -742,7 +754,7 @@ GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf
742 */ 754 */
743void 755void
744GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf, 756GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
745 const struct GNUNET_HashCode * e) 757 const struct GNUNET_HashCode *e)
746{ 758{
747 if (NULL == bf) 759 if (NULL == bf)
748 return; 760 return;
@@ -763,7 +775,7 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
763int 775int
764GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf, 776GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
765 const char *data, 777 const char *data,
766 size_t size) 778 size_t size)
767{ 779{
768 unsigned int i; 780 unsigned int i;
769 unsigned int n; 781 unsigned int n;
@@ -796,8 +808,9 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
796 * @return #GNUNET_OK on success 808 * @return #GNUNET_OK on success
797 */ 809 */
798int 810int
799GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf, 811GNUNET_CONTAINER_bloomfilter_or2 (
800 const struct GNUNET_CONTAINER_BloomFilter *to_or) 812 struct GNUNET_CONTAINER_BloomFilter *bf,
813 const struct GNUNET_CONTAINER_BloomFilter *to_or)
801{ 814{
802 unsigned int i; 815 unsigned int i;
803 unsigned int n; 816 unsigned int n;
@@ -839,10 +852,7 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
839 return; 852 return;
840 if (NULL == bf->filename) 853 if (NULL == bf->filename)
841 return; 854 return;
842 iterateBits (bf, 855 iterateBits (bf, &decrementBitCallback, bf, e);
843 &decrementBitCallback,
844 bf,
845 e);
846} 856}
847 857
848/** 858/**
@@ -860,7 +870,7 @@ void
860GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, 870GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
861 GNUNET_CONTAINER_HashCodeIterator iterator, 871 GNUNET_CONTAINER_HashCodeIterator iterator,
862 void *iterator_cls, 872 void *iterator_cls,
863 size_t size, 873 size_t size,
864 unsigned int k) 874 unsigned int k)
865{ 875{
866 struct GNUNET_HashCode hc; 876 struct GNUNET_HashCode hc;
@@ -870,17 +880,14 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
870 i = 1; 880 i = 1;
871 while (i < size) 881 while (i < size)
872 i *= 2; 882 i *= 2;
873 size = i; /* make sure it's a power of 2 */ 883 size = i; /* make sure it's a power of 2 */
874 bf->addressesPerElement = k; 884 bf->addressesPerElement = k;
875 bf->bitArraySize = size; 885 bf->bitArraySize = size;
876 bf->bitArray = GNUNET_malloc (size); 886 bf->bitArray = GNUNET_malloc (size);
877 if (NULL != bf->filename) 887 if (NULL != bf->filename)
878 make_empty_file (bf->fh, 888 make_empty_file (bf->fh, bf->bitArraySize * 4LL);
879 bf->bitArraySize * 4LL); 889 while (GNUNET_YES == iterator (iterator_cls, &hc))
880 while (GNUNET_YES == iterator (iterator_cls, 890 GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
881 &hc))
882 GNUNET_CONTAINER_bloomfilter_add (bf,
883 &hc);
884} 891}
885 892
886/* end of container_bloomfilter.c */ 893/* end of container_bloomfilter.c */
diff --git a/src/util/crypto_ecc_setup.c b/src/util/crypto_ecc_setup.c
index 1031b302e..c556d805d 100644
--- a/src/util/crypto_ecc_setup.c
+++ b/src/util/crypto_ecc_setup.c
@@ -27,18 +27,29 @@
27#include <gcrypt.h> 27#include <gcrypt.h>
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
31 31
32#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall) 32#define LOG_STRERROR(kind, syscall) \
33 GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
33 34
34#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename) 35#define LOG_STRERROR_FILE(kind, syscall, filename) \
36 GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
35 37
36/** 38/**
37 * Log an error message at log-level 'level' that indicates 39 * Log an error message at log-level 'level' that indicates
38 * a failure of the command 'cmd' with the message given 40 * a failure of the command 'cmd' with the message given
39 * by gcry_strerror(rc). 41 * by gcry_strerror(rc).
40 */ 42 */
41#define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0) 43#define LOG_GCRY(level, cmd, rc) \
44 do \
45 { \
46 LOG (level, \
47 _ ("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, \
49 __FILE__, \
50 __LINE__, \
51 gcry_strerror (rc)); \
52 } while (0)
42 53
43 54
44/** 55/**
@@ -86,11 +97,12 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
86 return NULL; 97 return NULL;
87 while (GNUNET_YES != GNUNET_DISK_file_test (filename)) 98 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
88 { 99 {
89 fd = GNUNET_DISK_file_open (filename, 100 fd =
90 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE 101 GNUNET_DISK_file_open (filename,
91 | GNUNET_DISK_OPEN_FAILIFEXISTS, 102 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
92 GNUNET_DISK_PERM_USER_READ | 103 GNUNET_DISK_OPEN_FAILIFEXISTS,
93 GNUNET_DISK_PERM_USER_WRITE); 104 GNUNET_DISK_PERM_USER_READ |
105 GNUNET_DISK_PERM_USER_WRITE);
94 if (NULL == fd) 106 if (NULL == fd)
95 { 107 {
96 if (EEXIST == errno) 108 if (EEXIST == errno)
@@ -98,10 +110,10 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
98 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 110 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
99 { 111 {
100 /* must exist but not be accessible, fail for good! */ 112 /* must exist but not be accessible, fail for good! */
101 if (0 != ACCESS (filename, R_OK)) 113 if (0 != access (filename, R_OK))
102 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename); 114 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
103 else 115 else
104 GNUNET_break (0); /* what is going on!? */ 116 GNUNET_break (0); /* what is going on!? */
105 return NULL; 117 return NULL;
106 } 118 }
107 continue; 119 continue;
@@ -111,7 +123,8 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
111 } 123 }
112 cnt = 0; 124 cnt = 0;
113 while (GNUNET_YES != 125 while (GNUNET_YES !=
114 GNUNET_DISK_file_lock (fd, 0, 126 GNUNET_DISK_file_lock (fd,
127 0,
115 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey), 128 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
116 GNUNET_YES)) 129 GNUNET_YES))
117 { 130 {
@@ -120,27 +133,29 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
120 { 133 {
121 ec = errno; 134 ec = errno;
122 LOG (GNUNET_ERROR_TYPE_ERROR, 135 LOG (GNUNET_ERROR_TYPE_ERROR,
123 _("Could not acquire lock on file `%s': %s...\n"), 136 _ ("Could not acquire lock on file `%s': %s...\n"),
124 filename, 137 filename,
125 STRERROR (ec)); 138 strerror (ec));
126 } 139 }
127 } 140 }
128 LOG (GNUNET_ERROR_TYPE_INFO, 141 LOG (GNUNET_ERROR_TYPE_INFO,
129 _("Creating a new private key. This may take a while.\n")); 142 _ ("Creating a new private key. This may take a while.\n"));
130 priv = GNUNET_CRYPTO_eddsa_key_create (); 143 priv = GNUNET_CRYPTO_eddsa_key_create ();
131 GNUNET_assert (NULL != priv); 144 GNUNET_assert (NULL != priv);
132 GNUNET_assert (sizeof (*priv) == 145 GNUNET_assert (sizeof (*priv) ==
133 GNUNET_DISK_file_write (fd, priv, sizeof (*priv))); 146 GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
134 GNUNET_DISK_file_sync (fd); 147 GNUNET_DISK_file_sync (fd);
135 if (GNUNET_YES != 148 if (GNUNET_YES !=
136 GNUNET_DISK_file_unlock (fd, 0, 149 GNUNET_DISK_file_unlock (fd,
150 0,
137 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))) 151 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
138 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 152 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
139 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); 153 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
140 return priv; 154 return priv;
141 } 155 }
142 /* key file exists already, read it! */ 156 /* key file exists already, read it! */
143 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 157 fd = GNUNET_DISK_file_open (filename,
158 GNUNET_DISK_OPEN_READ,
144 GNUNET_DISK_PERM_NONE); 159 GNUNET_DISK_PERM_NONE);
145 if (NULL == fd) 160 if (NULL == fd)
146 { 161 {
@@ -151,7 +166,8 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
151 while (1) 166 while (1)
152 { 167 {
153 if (GNUNET_YES != 168 if (GNUNET_YES !=
154 GNUNET_DISK_file_lock (fd, 0, 169 GNUNET_DISK_file_lock (fd,
170 0,
155 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey), 171 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
156 GNUNET_NO)) 172 GNUNET_NO))
157 { 173 {
@@ -159,11 +175,13 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
159 { 175 {
160 ec = errno; 176 ec = errno;
161 LOG (GNUNET_ERROR_TYPE_ERROR, 177 LOG (GNUNET_ERROR_TYPE_ERROR,
162 _("Could not acquire lock on file `%s': %s...\n"), filename, 178 _ ("Could not acquire lock on file `%s': %s...\n"),
163 STRERROR (ec)); 179 filename,
164 LOG (GNUNET_ERROR_TYPE_ERROR, 180 strerror (ec));
165 _ 181 LOG (
166 ("This may be ok if someone is currently generating a private key.\n")); 182 GNUNET_ERROR_TYPE_ERROR,
183 _ (
184 "This may be ok if someone is currently generating a private key.\n"));
167 } 185 }
168 short_wait (); 186 short_wait ();
169 continue; 187 continue;
@@ -173,57 +191,56 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
173 /* eh, what!? File we opened is now gone!? */ 191 /* eh, what!? File we opened is now gone!? */
174 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename); 192 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
175 if (GNUNET_YES != 193 if (GNUNET_YES !=
176 GNUNET_DISK_file_unlock (fd, 0, 194 GNUNET_DISK_file_unlock (fd,
177 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))) 195 0,
196 sizeof (
197 struct GNUNET_CRYPTO_EddsaPrivateKey)))
178 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 198 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
179 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd)); 199 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
180 200
181 return NULL; 201 return NULL;
182 } 202 }
183 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) 203 if (GNUNET_OK !=
204 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
184 fs = 0; 205 fs = 0;
185 if (fs < sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)) 206 if (fs < sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))
186 { 207 {
187 /* maybe we got the read lock before the key generating 208 /* maybe we got the read lock before the key generating
188 * process had a chance to get the write lock; give it up! */ 209 * process had a chance to get the write lock; give it up! */
189 if (GNUNET_YES != 210 if (GNUNET_YES !=
190 GNUNET_DISK_file_unlock (fd, 0, 211 GNUNET_DISK_file_unlock (fd,
191 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))) 212 0,
213 sizeof (
214 struct GNUNET_CRYPTO_EddsaPrivateKey)))
192 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 215 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
193 if (0 == ++cnt % 10) 216 if (0 == ++cnt % 10)
194 { 217 {
195 LOG (GNUNET_ERROR_TYPE_ERROR, 218 LOG (GNUNET_ERROR_TYPE_ERROR,
196 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"), 219 _ (
220 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
197 filename, 221 filename,
198 (unsigned int) fs, 222 (unsigned int) fs,
199 (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 223 (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
200 LOG (GNUNET_ERROR_TYPE_ERROR, 224 LOG (GNUNET_ERROR_TYPE_ERROR,
201 _("This may be ok if someone is currently generating a key.\n")); 225 _ ("This may be ok if someone is currently generating a key.\n"));
202 } 226 }
203 short_wait (); /* wait a bit longer! */ 227 short_wait (); /* wait a bit longer! */
204 continue; 228 continue;
205 } 229 }
206 break; 230 break;
207 } 231 }
208 fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey); 232 fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey);
209 priv = GNUNET_malloc (fs); 233 priv = GNUNET_malloc (fs);
210 sret = GNUNET_DISK_file_read (fd, 234 sret = GNUNET_DISK_file_read (fd, priv, fs);
211 priv, 235 GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
212 fs);
213 GNUNET_assert ( (sret >= 0) &&
214 (fs == (size_t) sret) );
215 if (GNUNET_YES != 236 if (GNUNET_YES !=
216 GNUNET_DISK_file_unlock (fd, 237 GNUNET_DISK_file_unlock (fd,
217 0, 238 0,
218 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))) 239 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
219 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 240 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
220 "fcntl", 241 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
221 filename);
222 GNUNET_assert (GNUNET_YES ==
223 GNUNET_DISK_file_close (fd));
224#if CRYPTO_BUG 242#if CRYPTO_BUG
225 if (GNUNET_OK != 243 if (GNUNET_OK != check_eddsa_key (priv))
226 check_eddsa_key (priv))
227 { 244 {
228 GNUNET_break (0); 245 GNUNET_break (0);
229 GNUNET_free (priv); 246 GNUNET_free (priv);
@@ -258,16 +275,16 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
258 uint64_t fs; 275 uint64_t fs;
259 ssize_t sret; 276 ssize_t sret;
260 277
261 if (GNUNET_SYSERR == 278 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
262 GNUNET_DISK_directory_create_for_file (filename))
263 return NULL; 279 return NULL;
264 while (GNUNET_YES != GNUNET_DISK_file_test (filename)) 280 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
265 { 281 {
266 fd = GNUNET_DISK_file_open (filename, 282 fd =
267 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE 283 GNUNET_DISK_file_open (filename,
268 | GNUNET_DISK_OPEN_FAILIFEXISTS, 284 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
269 GNUNET_DISK_PERM_USER_READ | 285 GNUNET_DISK_OPEN_FAILIFEXISTS,
270 GNUNET_DISK_PERM_USER_WRITE); 286 GNUNET_DISK_PERM_USER_READ |
287 GNUNET_DISK_PERM_USER_WRITE);
271 if (NULL == fd) 288 if (NULL == fd)
272 { 289 {
273 if (EEXIST == errno) 290 if (EEXIST == errno)
@@ -275,25 +292,21 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
275 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 292 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
276 { 293 {
277 /* must exist but not be accessible, fail for good! */ 294 /* must exist but not be accessible, fail for good! */
278 if (0 != ACCESS (filename, R_OK)) 295 if (0 != access (filename, R_OK))
279 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, 296 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
280 "access",
281 filename);
282 else 297 else
283 GNUNET_break (0); /* what is going on!? */ 298 GNUNET_break (0); /* what is going on!? */
284 return NULL; 299 return NULL;
285 } 300 }
286 continue; 301 continue;
287 } 302 }
288 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, 303 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
289 "open",
290 filename);
291 return NULL; 304 return NULL;
292 } 305 }
293 cnt = 0; 306 cnt = 0;
294 while (GNUNET_YES != 307 while (GNUNET_YES !=
295 GNUNET_DISK_file_lock (fd, 308 GNUNET_DISK_file_lock (fd,
296 0, 309 0,
297 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 310 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
298 GNUNET_YES)) 311 GNUNET_YES))
299 { 312 {
@@ -302,46 +315,41 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
302 { 315 {
303 ec = errno; 316 ec = errno;
304 LOG (GNUNET_ERROR_TYPE_ERROR, 317 LOG (GNUNET_ERROR_TYPE_ERROR,
305 _("Could not acquire lock on file `%s': %s...\n"), 318 _ ("Could not acquire lock on file `%s': %s...\n"),
306 filename, 319 filename,
307 STRERROR (ec)); 320 strerror (ec));
308 } 321 }
309 } 322 }
310 LOG (GNUNET_ERROR_TYPE_INFO, 323 LOG (GNUNET_ERROR_TYPE_INFO,
311 _("Creating a new private key. This may take a while.\n")); 324 _ ("Creating a new private key. This may take a while.\n"));
312 priv = GNUNET_CRYPTO_ecdsa_key_create (); 325 priv = GNUNET_CRYPTO_ecdsa_key_create ();
313 GNUNET_assert (NULL != priv); 326 GNUNET_assert (NULL != priv);
314 GNUNET_assert (sizeof (*priv) == 327 GNUNET_assert (sizeof (*priv) ==
315 GNUNET_DISK_file_write (fd, 328 GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
316 priv,
317 sizeof (*priv)));
318 GNUNET_DISK_file_sync (fd); 329 GNUNET_DISK_file_sync (fd);
319 if (GNUNET_YES != 330 if (GNUNET_YES !=
320 GNUNET_DISK_file_unlock (fd, 0, 331 GNUNET_DISK_file_unlock (fd,
332 0,
321 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 333 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
322 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 334 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
323 "fcntl", 335 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
324 filename);
325 GNUNET_assert (GNUNET_YES ==
326 GNUNET_DISK_file_close (fd));
327 return priv; 336 return priv;
328 } 337 }
329 /* key file exists already, read it! */ 338 /* key file exists already, read it! */
330 fd = GNUNET_DISK_file_open (filename, 339 fd = GNUNET_DISK_file_open (filename,
331 GNUNET_DISK_OPEN_READ, 340 GNUNET_DISK_OPEN_READ,
332 GNUNET_DISK_PERM_NONE); 341 GNUNET_DISK_PERM_NONE);
333 if (NULL == fd) 342 if (NULL == fd)
334 { 343 {
335 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, 344 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
336 "open",
337 filename);
338 return NULL; 345 return NULL;
339 } 346 }
340 cnt = 0; 347 cnt = 0;
341 while (1) 348 while (1)
342 { 349 {
343 if (GNUNET_YES != 350 if (GNUNET_YES !=
344 GNUNET_DISK_file_lock (fd, 0, 351 GNUNET_DISK_file_lock (fd,
352 0,
345 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 353 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
346 GNUNET_NO)) 354 GNUNET_NO))
347 { 355 {
@@ -349,78 +357,70 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
349 { 357 {
350 ec = errno; 358 ec = errno;
351 LOG (GNUNET_ERROR_TYPE_ERROR, 359 LOG (GNUNET_ERROR_TYPE_ERROR,
352 _("Could not acquire lock on file `%s': %s...\n"), 360 _ ("Could not acquire lock on file `%s': %s...\n"),
353 filename, 361 filename,
354 STRERROR (ec)); 362 strerror (ec));
355 LOG (GNUNET_ERROR_TYPE_ERROR, 363 LOG (
356 _("This may be ok if someone is currently generating a private key.\n")); 364 GNUNET_ERROR_TYPE_ERROR,
365 _ (
366 "This may be ok if someone is currently generating a private key.\n"));
357 } 367 }
358 short_wait (); 368 short_wait ();
359 continue; 369 continue;
360 } 370 }
361 if (GNUNET_YES != 371 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
362 GNUNET_DISK_file_test (filename))
363 { 372 {
364 /* eh, what!? File we opened is now gone!? */ 373 /* eh, what!? File we opened is now gone!? */
365 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 374 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
366 "stat",
367 filename);
368 if (GNUNET_YES != 375 if (GNUNET_YES !=
369 GNUNET_DISK_file_unlock (fd, 0, 376 GNUNET_DISK_file_unlock (fd,
370 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 377 0,
371 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 378 sizeof (
372 "fcntl", 379 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
373 filename); 380 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
374 GNUNET_assert (GNUNET_OK == 381 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
375 GNUNET_DISK_file_close (fd));
376 382
377 return NULL; 383 return NULL;
378 } 384 }
379 if (GNUNET_OK != 385 if (GNUNET_OK !=
380 GNUNET_DISK_file_size (filename, 386 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
381 &fs,
382 GNUNET_YES,
383 GNUNET_YES))
384 fs = 0; 387 fs = 0;
385 if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) 388 if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
386 { 389 {
387 /* maybe we got the read lock before the key generating 390 /* maybe we got the read lock before the key generating
388 * process had a chance to get the write lock; give it up! */ 391 * process had a chance to get the write lock; give it up! */
389 if (GNUNET_YES != 392 if (GNUNET_YES !=
390 GNUNET_DISK_file_unlock (fd, 0, 393 GNUNET_DISK_file_unlock (fd,
391 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 394 0,
392 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 395 sizeof (
393 "fcntl", 396 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
394 filename); 397 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
395 if (0 == ++cnt % 10) 398 if (0 == ++cnt % 10)
396 { 399 {
397 LOG (GNUNET_ERROR_TYPE_ERROR, 400 LOG (GNUNET_ERROR_TYPE_ERROR,
398 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"), 401 _ (
399 filename, (unsigned int) fs, 402 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
403 filename,
404 (unsigned int) fs,
400 (unsigned int) sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 405 (unsigned int) sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
401 LOG (GNUNET_ERROR_TYPE_ERROR, 406 LOG (GNUNET_ERROR_TYPE_ERROR,
402 _("This may be ok if someone is currently generating a key.\n")); 407 _ ("This may be ok if someone is currently generating a key.\n"));
403 } 408 }
404 short_wait (); /* wait a bit longer! */ 409 short_wait (); /* wait a bit longer! */
405 continue; 410 continue;
406 } 411 }
407 break; 412 break;
408 } 413 }
409 fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 414 fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
410 priv = GNUNET_malloc (fs); 415 priv = GNUNET_malloc (fs);
411 sret = GNUNET_DISK_file_read (fd, 416 sret = GNUNET_DISK_file_read (fd, priv, fs);
412 priv, 417 GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
413 fs);
414 GNUNET_assert ( (sret >= 0) &&
415 (fs == (size_t) sret) );
416 if (GNUNET_YES != 418 if (GNUNET_YES !=
417 GNUNET_DISK_file_unlock (fd, 0, 419 GNUNET_DISK_file_unlock (fd,
420 0,
418 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 421 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
419 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 422 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
420 "fcntl", 423 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
421 filename);
422 GNUNET_assert (GNUNET_YES ==
423 GNUNET_DISK_file_close (fd));
424 return priv; 424 return priv;
425} 425}
426 426
@@ -434,7 +434,8 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
434 * permission denied) 434 * permission denied)
435 */ 435 */
436struct GNUNET_CRYPTO_EddsaPrivateKey * 436struct GNUNET_CRYPTO_EddsaPrivateKey *
437GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg) 437GNUNET_CRYPTO_eddsa_key_create_from_configuration (
438 const struct GNUNET_CONFIGURATION_Handle *cfg)
438{ 439{
439 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 440 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
440 char *fn; 441 char *fn;
@@ -465,7 +466,7 @@ GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
465 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg))) 466 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
466 { 467 {
467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
468 _("Could not load peer's private key\n")); 469 _ ("Could not load peer's private key\n"));
469 return GNUNET_SYSERR; 470 return GNUNET_SYSERR;
470 } 471 }
471 GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key); 472 GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key);
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index 71eaab87a..58cab082e 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -80,7 +80,7 @@ glibc_weak_rand32 ()
80static double 80static double
81get_weak_random () 81get_weak_random ()
82{ 82{
83 return ((double) RANDOM () / RAND_MAX); 83 return ((double) random () / RAND_MAX);
84} 84}
85 85
86 86
@@ -93,7 +93,7 @@ get_weak_random ()
93void 93void
94GNUNET_CRYPTO_seed_weak_random (int32_t seed) 94GNUNET_CRYPTO_seed_weak_random (int32_t seed)
95{ 95{
96 SRANDOM (seed); 96 srandom (seed);
97} 97}
98 98
99 99
@@ -324,7 +324,7 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
324 324
325 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION)) 325 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
326 { 326 {
327 FPRINTF ( 327 fprintf (
328 stderr, 328 stderr,
329 _ ("libgcrypt has not the expected version (version %s is required).\n"), 329 _ ("libgcrypt has not the expected version (version %s is required).\n"),
330 NEED_LIBGCRYPT_VERSION); 330 NEED_LIBGCRYPT_VERSION);
@@ -334,14 +334,14 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
334 gcry_set_allocation_handler (&w_malloc, &w_malloc, &w_check, &realloc, &free); 334 gcry_set_allocation_handler (&w_malloc, &w_malloc, &w_check, &realloc, &free);
335 /* Disable use of secure memory */ 335 /* Disable use of secure memory */
336 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0))) 336 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
337 FPRINTF (stderr, 337 fprintf (stderr,
338 "Failed to set libgcrypt option %s: %s\n", 338 "Failed to set libgcrypt option %s: %s\n",
339 "DISABLE_SECMEM", 339 "DISABLE_SECMEM",
340 gcry_strerror (rc)); 340 gcry_strerror (rc));
341 /* Otherwise gnunet-ecc takes forever to complete, besides 341 /* Otherwise gnunet-ecc takes forever to complete, besides
342 we are fine with "just" using GCRY_STRONG_RANDOM */ 342 we are fine with "just" using GCRY_STRONG_RANDOM */
343 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0))) 343 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
344 FPRINTF (stderr, 344 fprintf (stderr,
345 "Failed to set libgcrypt option %s: %s\n", 345 "Failed to set libgcrypt option %s: %s\n",
346 "ENABLE_QUICK_RANDOM", 346 "ENABLE_QUICK_RANDOM",
347 gcry_strerror (rc)); 347 gcry_strerror (rc));
diff --git a/src/util/disk.c b/src/util/disk.c
index f395a375e..c1f24e4c8 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -28,11 +28,13 @@
28#include "gnunet_strings_lib.h" 28#include "gnunet_strings_lib.h"
29#include "gnunet_disk_lib.h" 29#include "gnunet_disk_lib.h"
30 30
31#define LOG(kind,...) GNUNET_log_from (kind, "util-disk", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-disk", __VA_ARGS__)
32 32
33#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-disk", syscall) 33#define LOG_STRERROR(kind, syscall) \
34 GNUNET_log_from_strerror (kind, "util-disk", syscall)
34 35
35#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename) 36#define LOG_STRERROR_FILE(kind, syscall, filename) \
37 GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename)
36 38
37/** 39/**
38 * Block size for IO for copying files. 40 * Block size for IO for copying files.
@@ -54,9 +56,9 @@
54#endif 56#endif
55 57
56#ifndef S_ISLNK 58#ifndef S_ISLNK
57#define _IFMT 0170000 /* type of file */ 59#define _IFMT 0170000 /* type of file */
58#define _IFLNK 0120000 /* symbolic link */ 60#define _IFLNK 0120000 /* symbolic link */
59#define S_ISLNK(m) (((m)&_IFMT) == _IFLNK) 61#define S_ISLNK(m) (((m) &_IFMT) == _IFLNK)
60#endif 62#endif
61 63
62 64
@@ -147,7 +149,8 @@ getSizeRec (void *cls, const char *fn)
147{ 149{
148 struct GetFileSizeData *gfsd = cls; 150 struct GetFileSizeData *gfsd = cls;
149 151
150#if defined (HAVE_STAT64) && !(defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) 152#if defined(HAVE_STAT64) && \
153 ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
151 STRUCT_STAT64 buf; 154 STRUCT_STAT64 buf;
152 155
153 if (0 != STAT64 (fn, &buf)) 156 if (0 != STAT64 (fn, &buf))
@@ -158,7 +161,7 @@ getSizeRec (void *cls, const char *fn)
158#else 161#else
159 struct stat buf; 162 struct stat buf;
160 163
161 if (0 != STAT (fn, &buf)) 164 if (0 != stat (fn, &buf))
162 { 165 {
163 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn); 166 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn);
164 return GNUNET_SYSERR; 167 return GNUNET_SYSERR;
@@ -169,10 +172,10 @@ getSizeRec (void *cls, const char *fn)
169 errno = EISDIR; 172 errno = EISDIR;
170 return GNUNET_SYSERR; 173 return GNUNET_SYSERR;
171 } 174 }
172 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) 175 if ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
173 gfsd->total += buf.st_size; 176 gfsd->total += buf.st_size;
174 if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) && 177 if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) &&
175 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) 178 ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
176 { 179 {
177 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) 180 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
178 return GNUNET_SYSERR; 181 return GNUNET_SYSERR;
@@ -191,9 +194,9 @@ int
191GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) 194GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
192{ 195{
193#ifdef MINGW 196#ifdef MINGW
194 return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO; 197 return ((! h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO;
195#else 198#else
196 return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; 199 return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
197#endif 200#endif
198} 201}
199 202
@@ -205,14 +208,13 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
205 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 208 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
206 */ 209 */
207int 210int
208GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, 211GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size)
209 off_t *size)
210{ 212{
211#if WINDOWS 213#if WINDOWS
212 BOOL b; 214 BOOL b;
213 LARGE_INTEGER li; 215 LARGE_INTEGER li;
214 b = GetFileSizeEx (fh->h, &li); 216 b = GetFileSizeEx (fh->h, &li);
215 if (!b) 217 if (! b)
216 { 218 {
217 SetErrnoFromWinError (GetLastError ()); 219 SetErrnoFromWinError (GetLastError ());
218 return GNUNET_SYSERR; 220 return GNUNET_SYSERR;
@@ -221,7 +223,7 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
221#else 223#else
222 struct stat sbuf; 224 struct stat sbuf;
223 225
224 if (0 != FSTAT (fh->fd, &sbuf)) 226 if (0 != fstat (fh->fd, &sbuf))
225 return GNUNET_SYSERR; 227 return GNUNET_SYSERR;
226 *size = sbuf.st_size; 228 *size = sbuf.st_size;
227#endif 229#endif
@@ -239,7 +241,7 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
239 */ 241 */
240off_t 242off_t
241GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, 243GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
242 off_t offset, 244 off_t offset,
243 enum GNUNET_DISK_Seek whence) 245 enum GNUNET_DISK_Seek whence)
244{ 246{
245 if (h == NULL) 247 if (h == NULL)
@@ -253,7 +255,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
253 LARGE_INTEGER new_pos; 255 LARGE_INTEGER new_pos;
254 BOOL b; 256 BOOL b;
255 257
256 static DWORD t[] = { FILE_BEGIN, FILE_CURRENT, FILE_END }; 258 static DWORD t[] = {FILE_BEGIN, FILE_CURRENT, FILE_END};
257 li.QuadPart = offset; 259 li.QuadPart = offset;
258 260
259 b = SetFilePointerEx (h->h, li, &new_pos, t[whence]); 261 b = SetFilePointerEx (h->h, li, &new_pos, t[whence]);
@@ -264,7 +266,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
264 } 266 }
265 return (off_t) new_pos.QuadPart; 267 return (off_t) new_pos.QuadPart;
266#else 268#else
267 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END }; 269 static int t[] = {SEEK_SET, SEEK_CUR, SEEK_END};
268 270
269 return lseek (h->fd, offset, t[whence]); 271 return lseek (h->fd, offset, t[whence]);
270#endif 272#endif
@@ -287,9 +289,9 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
287 */ 289 */
288int 290int
289GNUNET_DISK_file_size (const char *filename, 291GNUNET_DISK_file_size (const char *filename,
290 uint64_t * size, 292 uint64_t *size,
291 int include_symbolic_links, 293 int include_symbolic_links,
292 int single_file_mode) 294 int single_file_mode)
293{ 295{
294 struct GetFileSizeData gfsd; 296 struct GetFileSizeData gfsd;
295 int ret; 297 int ret;
@@ -320,8 +322,9 @@ GNUNET_DISK_file_size (const char *filename,
320 * @return #GNUNET_OK on success 322 * @return #GNUNET_OK on success
321 */ 323 */
322int 324int
323GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, 325GNUNET_DISK_file_get_identifiers (const char *filename,
324 uint64_t * ino) 326 uint64_t *dev,
327 uint64_t *ino)
325{ 328{
326#if WINDOWS 329#if WINDOWS
327 { 330 {
@@ -337,12 +340,13 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
337 return GNUNET_SYSERR; 340 return GNUNET_SYSERR;
338 succ = GetFileInformationByHandle (fh->h, &info); 341 succ = GetFileInformationByHandle (fh->h, &info);
339 GNUNET_DISK_file_close (fh); 342 GNUNET_DISK_file_close (fh);
340 if (!succ) 343 if (! succ)
341 { 344 {
342 return GNUNET_SYSERR; 345 return GNUNET_SYSERR;
343 } 346 }
344 *dev = info.dwVolumeSerialNumber; 347 *dev = info.dwVolumeSerialNumber;
345 *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | info.nFileIndexLow); 348 *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) |
349 info.nFileIndexLow);
346 } 350 }
347#else /* !WINDOWS */ 351#else /* !WINDOWS */
348#if HAVE_STAT 352#if HAVE_STAT
@@ -376,8 +380,8 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
376 { 380 {
377 return GNUNET_SYSERR; 381 return GNUNET_SYSERR;
378 } 382 }
379 *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || 383 *dev =
380 ((uint64_t) fbuf.f_fsid.val[1]); 384 ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]);
381 } 385 }
382#else 386#else
383 *dev = 0; 387 *dev = 0;
@@ -402,9 +406,9 @@ mktemp_name (const char *t)
402 406
403 if ((t[0] != '/') && (t[0] != '\\') 407 if ((t[0] != '/') && (t[0] != '\\')
404#if WINDOWS 408#if WINDOWS
405 && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) 409 && ! (isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':'))
406#endif 410#endif
407 ) 411 )
408 { 412 {
409 /* FIXME: This uses system codepage on W32, not UTF-8 */ 413 /* FIXME: This uses system codepage on W32, not UTF-8 */
410 tmpdir = getenv ("TMPDIR"); 414 tmpdir = getenv ("TMPDIR");
@@ -506,11 +510,10 @@ GNUNET_DISK_fix_permissions (const char *fn,
506 else if (GNUNET_YES == require_gid_match) 510 else if (GNUNET_YES == require_gid_match)
507 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP; 511 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
508 else 512 else
509 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; 513 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH |
514 S_IWOTH | S_IXOTH;
510 if (0 != chmod (fn, mode)) 515 if (0 != chmod (fn, mode))
511 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 516 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn);
512 "chmod",
513 fn);
514} 517}
515 518
516#endif 519#endif
@@ -565,15 +568,10 @@ GNUNET_DISK_file_backup (const char *fil)
565 num = 0; 568 num = 0;
566 do 569 do
567 { 570 {
568 GNUNET_snprintf (target, slen, 571 GNUNET_snprintf (target, slen, "%s.%u~", fil, num++);
569 "%s.%u~",
570 fil,
571 num++);
572 } while (0 == access (target, F_OK)); 572 } while (0 == access (target, F_OK));
573 if (0 != rename (fil, target)) 573 if (0 != rename (fil, target))
574 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 574 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "rename", fil);
575 "rename",
576 fil);
577 GNUNET_free (target); 575 GNUNET_free (target);
578} 576}
579 577
@@ -606,7 +604,7 @@ GNUNET_DISK_mktemp (const char *t)
606 return NULL; 604 return NULL;
607 } 605 }
608 umask (omask); 606 umask (omask);
609 if (0 != CLOSE (fd)) 607 if (0 != close (fd))
610 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn); 608 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
611 return fn; 609 return fn;
612} 610}
@@ -625,29 +623,29 @@ GNUNET_DISK_mktemp (const char *t)
625 * does not exist or stat'ed 623 * does not exist or stat'ed
626 */ 624 */
627int 625int
628GNUNET_DISK_directory_test (const char *fil, 626GNUNET_DISK_directory_test (const char *fil, int is_readable)
629 int is_readable)
630{ 627{
631 struct stat filestat; 628 struct stat filestat;
632 int ret; 629 int ret;
633 630
634 ret = STAT (fil, &filestat); 631 ret = stat (fil, &filestat);
635 if (ret != 0) 632 if (ret != 0)
636 { 633 {
637 if (errno != ENOENT) 634 if (errno != ENOENT)
638 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); 635 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
639 return GNUNET_SYSERR; 636 return GNUNET_SYSERR;
640 } 637 }
641 if (!S_ISDIR (filestat.st_mode)) 638 if (! S_ISDIR (filestat.st_mode))
642 { 639 {
643 LOG (GNUNET_ERROR_TYPE_INFO, 640 LOG (GNUNET_ERROR_TYPE_INFO,
644 "A file already exits with the same name %s\n", fil); 641 "A file already exits with the same name %s\n",
642 fil);
645 return GNUNET_NO; 643 return GNUNET_NO;
646 } 644 }
647 if (GNUNET_YES == is_readable) 645 if (GNUNET_YES == is_readable)
648 ret = ACCESS (fil, R_OK | X_OK); 646 ret = access (fil, R_OK | X_OK);
649 else 647 else
650 ret = ACCESS (fil, X_OK); 648 ret = access (fil, X_OK);
651 if (ret < 0) 649 if (ret < 0)
652 { 650 {
653 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); 651 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
@@ -676,7 +674,7 @@ GNUNET_DISK_file_test (const char *fil)
676 if (rdir == NULL) 674 if (rdir == NULL)
677 return GNUNET_SYSERR; 675 return GNUNET_SYSERR;
678 676
679 ret = STAT (rdir, &filestat); 677 ret = stat (rdir, &filestat);
680 if (ret != 0) 678 if (ret != 0)
681 { 679 {
682 if (errno != ENOENT) 680 if (errno != ENOENT)
@@ -688,12 +686,12 @@ GNUNET_DISK_file_test (const char *fil)
688 GNUNET_free (rdir); 686 GNUNET_free (rdir);
689 return GNUNET_NO; 687 return GNUNET_NO;
690 } 688 }
691 if (!S_ISREG (filestat.st_mode)) 689 if (! S_ISREG (filestat.st_mode))
692 { 690 {
693 GNUNET_free (rdir); 691 GNUNET_free (rdir);
694 return GNUNET_NO; 692 return GNUNET_NO;
695 } 693 }
696 if (ACCESS (rdir, F_OK) < 0) 694 if (access (rdir, F_OK) < 0)
697 { 695 {
698 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir); 696 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
699 GNUNET_free (rdir); 697 GNUNET_free (rdir);
@@ -728,7 +726,7 @@ GNUNET_DISK_directory_create (const char *dir)
728 726
729 len = strlen (rdir); 727 len = strlen (rdir);
730#ifndef MINGW 728#ifndef MINGW
731 pos = 1; /* skip heading '/' */ 729 pos = 1; /* skip heading '/' */
732#else 730#else
733 /* Local or Network path? */ 731 /* Local or Network path? */
734 if (strncmp (rdir, "\\\\", 2) == 0) 732 if (strncmp (rdir, "\\\\", 2) == 0)
@@ -746,7 +744,7 @@ GNUNET_DISK_directory_create (const char *dir)
746 } 744 }
747 else 745 else
748 { 746 {
749 pos = 3; /* strlen("C:\\") */ 747 pos = 3; /* strlen("C:\\") */
750 } 748 }
751#endif 749#endif
752 /* Check which low level directories already exist */ 750 /* Check which low level directories already exist */
@@ -796,11 +794,13 @@ GNUNET_DISK_directory_create (const char *dir)
796 if (GNUNET_SYSERR == ret) 794 if (GNUNET_SYSERR == ret)
797 { 795 {
798#ifndef MINGW 796#ifndef MINGW
799 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ 797 ret = mkdir (rdir,
798 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH |
799 S_IXOTH); /* 755 */
800#else 800#else
801 wchar_t wrdir[MAX_PATH + 1]; 801 wchar_t wrdir[MAX_PATH + 1];
802 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(rdir, wrdir)) 802 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv (rdir, wrdir))
803 ret = !CreateDirectoryW (wrdir, NULL); 803 ret = ! CreateDirectoryW (wrdir, NULL);
804 else 804 else
805 ret = 1; 805 ret = 1;
806#endif 806#endif
@@ -843,7 +843,7 @@ GNUNET_DISK_directory_create_for_file (const char *filename)
843 errno = EINVAL; 843 errno = EINVAL;
844 return GNUNET_SYSERR; 844 return GNUNET_SYSERR;
845 } 845 }
846 if (0 == ACCESS (rdir, W_OK)) 846 if (0 == access (rdir, W_OK))
847 { 847 {
848 GNUNET_free (rdir); 848 GNUNET_free (rdir);
849 return GNUNET_OK; 849 return GNUNET_OK;
@@ -860,7 +860,7 @@ GNUNET_DISK_directory_create_for_file (const char *filename)
860 rdir = GNUNET_strdup ("/"); 860 rdir = GNUNET_strdup ("/");
861 } 861 }
862 ret = GNUNET_DISK_directory_create (rdir); 862 ret = GNUNET_DISK_directory_create (rdir);
863 if ((GNUNET_OK == ret) && (0 != ACCESS (rdir, W_OK))) 863 if ((GNUNET_OK == ret) && (0 != access (rdir, W_OK)))
864 ret = GNUNET_NO; 864 ret = GNUNET_NO;
865 eno = errno; 865 eno = errno;
866 GNUNET_free (rdir); 866 GNUNET_free (rdir);
@@ -893,7 +893,7 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
893 893
894 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) 894 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE)
895 { 895 {
896 if (!ReadFile (h->h, result, len, &bytes_read, NULL)) 896 if (! ReadFile (h->h, result, len, &bytes_read, NULL))
897 { 897 {
898 SetErrnoFromWinError (GetLastError ()); 898 SetErrnoFromWinError (GetLastError ());
899 return GNUNET_SYSERR; 899 return GNUNET_SYSERR;
@@ -901,7 +901,7 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
901 } 901 }
902 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) 902 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
903 { 903 {
904 if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) 904 if (! ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead))
905 { 905 {
906 if (GetLastError () != ERROR_IO_PENDING) 906 if (GetLastError () != ERROR_IO_PENDING)
907 { 907 {
@@ -939,8 +939,8 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
939 */ 939 */
940ssize_t 940ssize_t
941GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, 941GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
942 void *result, 942 void *result,
943 size_t len) 943 size_t len)
944{ 944{
945 if (NULL == h) 945 if (NULL == h)
946 { 946 {
@@ -953,7 +953,7 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
953 953
954 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) 954 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE)
955 { 955 {
956 if (!ReadFile (h->h, result, len, &bytes_read, NULL)) 956 if (! ReadFile (h->h, result, len, &bytes_read, NULL))
957 { 957 {
958 SetErrnoFromWinError (GetLastError ()); 958 SetErrnoFromWinError (GetLastError ());
959 return GNUNET_SYSERR; 959 return GNUNET_SYSERR;
@@ -961,26 +961,25 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
961 } 961 }
962 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) 962 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
963 { 963 {
964 if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) 964 if (! ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead))
965 { 965 {
966 if (GetLastError () != ERROR_IO_PENDING) 966 if (GetLastError () != ERROR_IO_PENDING)
967 { 967 {
968 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ()); 968 LOG (GNUNET_ERROR_TYPE_DEBUG,
969 "Error reading from pipe: %u\n",
970 GetLastError ());
969 SetErrnoFromWinError (GetLastError ()); 971 SetErrnoFromWinError (GetLastError ());
970 return GNUNET_SYSERR; 972 return GNUNET_SYSERR;
971 } 973 }
972 else 974 else
973 { 975 {
974 LOG (GNUNET_ERROR_TYPE_DEBUG, 976 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadFile() queued a read, cancelling\n");
975 "ReadFile() queued a read, cancelling\n");
976 CancelIo (h->h); 977 CancelIo (h->h);
977 errno = EAGAIN; 978 errno = EAGAIN;
978 return GNUNET_SYSERR; 979 return GNUNET_SYSERR;
979 } 980 }
980 } 981 }
981 LOG (GNUNET_ERROR_TYPE_DEBUG, 982 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytes_read);
982 "Read %u bytes\n",
983 bytes_read);
984 } 983 }
985 else 984 else
986 { 985 {
@@ -997,11 +996,11 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
997 (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK); 996 (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
998 ret = read (h->fd, result, len); 997 ret = read (h->fd, result, len);
999 if (0 == (flags & O_NONBLOCK)) 998 if (0 == (flags & O_NONBLOCK))
1000 { 999 {
1001 int eno = errno; 1000 int eno = errno;
1002 (void) fcntl (h->fd, F_SETFL, flags); 1001 (void) fcntl (h->fd, F_SETFL, flags);
1003 errno = eno; 1002 errno = eno;
1004 } 1003 }
1005 return ret; 1004 return ret;
1006#endif 1005#endif
1007} 1006}
@@ -1016,17 +1015,13 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
1016 * @return number of bytes read, #GNUNET_SYSERR on failure 1015 * @return number of bytes read, #GNUNET_SYSERR on failure
1017 */ 1016 */
1018ssize_t 1017ssize_t
1019GNUNET_DISK_fn_read (const char *fn, 1018GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
1020 void *result,
1021 size_t len)
1022{ 1019{
1023 struct GNUNET_DISK_FileHandle *fh; 1020 struct GNUNET_DISK_FileHandle *fh;
1024 ssize_t ret; 1021 ssize_t ret;
1025 int eno; 1022 int eno;
1026 1023
1027 fh = GNUNET_DISK_file_open (fn, 1024 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
1028 GNUNET_DISK_OPEN_READ,
1029 GNUNET_DISK_PERM_NONE);
1030 if (NULL == fh) 1025 if (NULL == fh)
1031 return GNUNET_SYSERR; 1026 return GNUNET_SYSERR;
1032 ret = GNUNET_DISK_file_read (fh, result, len); 1027 ret = GNUNET_DISK_file_read (fh, result, len);
@@ -1046,9 +1041,9 @@ GNUNET_DISK_fn_read (const char *fn,
1046 * @return number of bytes written on success, #GNUNET_SYSERR on error 1041 * @return number of bytes written on success, #GNUNET_SYSERR on error
1047 */ 1042 */
1048ssize_t 1043ssize_t
1049GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, 1044GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
1050 const void *buffer, 1045 const void *buffer,
1051 size_t n) 1046 size_t n)
1052{ 1047{
1053 if (NULL == h) 1048 if (NULL == h)
1054 { 1049 {
@@ -1061,7 +1056,7 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
1061 1056
1062 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) 1057 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1063 { 1058 {
1064 if (!WriteFile (h->h, buffer, n, &bytes_written, NULL)) 1059 if (! WriteFile (h->h, buffer, n, &bytes_written, NULL))
1065 { 1060 {
1066 SetErrnoFromWinError (GetLastError ()); 1061 SetErrnoFromWinError (GetLastError ());
1067 return GNUNET_SYSERR; 1062 return GNUNET_SYSERR;
@@ -1070,46 +1065,50 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
1070 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) 1065 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1071 { 1066 {
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); 1067 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n);
1073 if (!WriteFile (h->h, buffer, n, &bytes_written, h->oOverlapWrite)) 1068 if (! WriteFile (h->h, buffer, n, &bytes_written, h->oOverlapWrite))
1074 { 1069 {
1075 if (GetLastError () != ERROR_IO_PENDING) 1070 if (GetLastError () != ERROR_IO_PENDING)
1076 { 1071 {
1077 SetErrnoFromWinError (GetLastError ()); 1072 SetErrnoFromWinError (GetLastError ());
1078 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", 1073 LOG (GNUNET_ERROR_TYPE_DEBUG,
1079 GetLastError ()); 1074 "Error writing to pipe: %u\n",
1075 GetLastError ());
1080 return GNUNET_SYSERR; 1076 return GNUNET_SYSERR;
1081 } 1077 }
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); 1078 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
1083 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytes_written, TRUE)) 1079 if (! GetOverlappedResult (h->h, h->oOverlapWrite, &bytes_written, TRUE))
1084 { 1080 {
1085 SetErrnoFromWinError (GetLastError ()); 1081 SetErrnoFromWinError (GetLastError ());
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, 1082 LOG (GNUNET_ERROR_TYPE_DEBUG,
1087 "Error getting overlapped result while writing to pipe: %u\n", 1083 "Error getting overlapped result while writing to pipe: %u\n",
1088 GetLastError ()); 1084 GetLastError ());
1089 return GNUNET_SYSERR; 1085 return GNUNET_SYSERR;
1090 } 1086 }
1091 } 1087 }
1092 else 1088 else
1093 { 1089 {
1094 DWORD ovr; 1090 DWORD ovr;
1095 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE)) 1091 if (! GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE))
1096 { 1092 {
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, 1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1098 "Error getting control overlapped result while writing to pipe: %u\n", 1094 "Error getting control overlapped result while writing to pipe: %u\n",
1099 GetLastError ()); 1095 GetLastError ());
1100 } 1096 }
1101 else 1097 else
1102 { 1098 {
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, 1099 LOG (GNUNET_ERROR_TYPE_DEBUG,
1104 "Wrote %u bytes (ovr says %u), picking the greatest\n", 1100 "Wrote %u bytes (ovr says %u), picking the greatest\n",
1105 bytes_written, ovr); 1101 bytes_written,
1102 ovr);
1106 } 1103 }
1107 } 1104 }
1108 if (bytes_written == 0) 1105 if (bytes_written == 0)
1109 { 1106 {
1110 if (n > 0) 1107 if (n > 0)
1111 { 1108 {
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytes_written); 1109 LOG (GNUNET_ERROR_TYPE_DEBUG,
1110 "Wrote %u bytes, returning -1 with EAGAIN\n",
1111 bytes_written);
1113 errno = EAGAIN; 1112 errno = EAGAIN;
1114 return GNUNET_SYSERR; 1113 return GNUNET_SYSERR;
1115 } 1114 }
@@ -1136,7 +1135,7 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
1136 * @return number of bytes written on success, #GNUNET_SYSERR on error 1135 * @return number of bytes written on success, #GNUNET_SYSERR on error
1137 */ 1136 */
1138ssize_t 1137ssize_t
1139GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h, 1138GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
1140 const void *buffer, 1139 const void *buffer,
1141 size_t n) 1140 size_t n)
1142{ 1141{
@@ -1150,28 +1149,28 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
1150 DWORD bytes_written; 1149 DWORD bytes_written;
1151 /* We do a non-overlapped write, which is as blocking as it gets */ 1150 /* We do a non-overlapped write, which is as blocking as it gets */
1152 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n); 1151 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n);
1153 if (!WriteFile (h->h, buffer, n, &bytes_written, NULL)) 1152 if (! WriteFile (h->h, buffer, n, &bytes_written, NULL))
1154 { 1153 {
1155 SetErrnoFromWinError (GetLastError ()); 1154 SetErrnoFromWinError (GetLastError ());
1156 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", 1155 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 GetLastError ()); 1156 "Error writing to pipe: %u\n",
1157 GetLastError ());
1158 return GNUNET_SYSERR; 1158 return GNUNET_SYSERR;
1159 } 1159 }
1160 if (bytes_written == 0 && n > 0) 1160 if (bytes_written == 0 && n > 0)
1161 { 1161 {
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n"); 1162 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n");
1163 WaitForSingleObject (h->h, INFINITE); 1163 WaitForSingleObject (h->h, INFINITE);
1164 if (!WriteFile (h->h, buffer, n, &bytes_written, NULL)) 1164 if (! WriteFile (h->h, buffer, n, &bytes_written, NULL))
1165 { 1165 {
1166 SetErrnoFromWinError (GetLastError ()); 1166 SetErrnoFromWinError (GetLastError ());
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", 1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 GetLastError ()); 1168 "Error writing to pipe: %u\n",
1169 GetLastError ());
1169 return GNUNET_SYSERR; 1170 return GNUNET_SYSERR;
1170 } 1171 }
1171 } 1172 }
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, 1173 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written);
1173 "Wrote %u bytes\n",
1174 bytes_written);
1175 return bytes_written; 1174 return bytes_written;
1176#else 1175#else
1177 int flags; 1176 int flags;
@@ -1208,9 +1207,11 @@ GNUNET_DISK_fn_write (const char *fn,
1208 struct GNUNET_DISK_FileHandle *fh; 1207 struct GNUNET_DISK_FileHandle *fh;
1209 ssize_t ret; 1208 ssize_t ret;
1210 1209
1211 fh = GNUNET_DISK_file_open (fn, 1210 fh =
1212 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE 1211 GNUNET_DISK_file_open (fn,
1213 | GNUNET_DISK_OPEN_CREATE, mode); 1212 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE |
1213 GNUNET_DISK_OPEN_CREATE,
1214 mode);
1214 if (! fh) 1215 if (! fh)
1215 return GNUNET_SYSERR; 1216 return GNUNET_SYSERR;
1216 ret = GNUNET_DISK_file_write (fh, buffer, n); 1217 ret = GNUNET_DISK_file_write (fh, buffer, n);
@@ -1248,45 +1249,39 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1248 dname = GNUNET_STRINGS_filename_expand (dir_name); 1249 dname = GNUNET_STRINGS_filename_expand (dir_name);
1249 if (NULL == dname) 1250 if (NULL == dname)
1250 return GNUNET_SYSERR; 1251 return GNUNET_SYSERR;
1251 while ( (strlen (dname) > 0) && 1252 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
1252 (dname[strlen (dname) - 1] == DIR_SEPARATOR) )
1253 dname[strlen (dname) - 1] = '\0'; 1253 dname[strlen (dname) - 1] = '\0';
1254 if (0 != STAT (dname, &istat)) 1254 if (0 != stat (dname, &istat))
1255 { 1255 {
1256 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 1256 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
1257 "stat",
1258 dname);
1259 GNUNET_free (dname); 1257 GNUNET_free (dname);
1260 return GNUNET_SYSERR; 1258 return GNUNET_SYSERR;
1261 } 1259 }
1262 if (! S_ISDIR (istat.st_mode)) 1260 if (! S_ISDIR (istat.st_mode))
1263 { 1261 {
1264 LOG (GNUNET_ERROR_TYPE_WARNING, 1262 LOG (GNUNET_ERROR_TYPE_WARNING,
1265 _("Expected `%s' to be a directory!\n"), 1263 _ ("Expected `%s' to be a directory!\n"),
1266 dir_name); 1264 dir_name);
1267 GNUNET_free (dname); 1265 GNUNET_free (dname);
1268 return GNUNET_SYSERR; 1266 return GNUNET_SYSERR;
1269 } 1267 }
1270 errno = 0; 1268 errno = 0;
1271 dinfo = OPENDIR (dname); 1269 dinfo = opendir (dname);
1272 if ( (EACCES == errno) || 1270 if ((EACCES == errno) || (NULL == dinfo))
1273 (NULL == dinfo) )
1274 { 1271 {
1275 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 1272 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
1276 "opendir",
1277 dname);
1278 if (NULL != dinfo) 1273 if (NULL != dinfo)
1279 CLOSEDIR (dinfo); 1274 closedir (dinfo);
1280 GNUNET_free (dname); 1275 GNUNET_free (dname);
1281 return GNUNET_SYSERR; 1276 return GNUNET_SYSERR;
1282 } 1277 }
1283 name_len = 256; 1278 name_len = 256;
1284 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1; 1279 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
1285 name = GNUNET_malloc (n_size); 1280 name = GNUNET_malloc (n_size);
1286 while (NULL != (finfo = READDIR (dinfo))) 1281 while (NULL != (finfo = readdir (dinfo)))
1287 { 1282 {
1288 if ( (0 == strcmp (finfo->d_name, ".")) || 1283 if ((0 == strcmp (finfo->d_name, ".")) ||
1289 (0 == strcmp (finfo->d_name, "..")) ) 1284 (0 == strcmp (finfo->d_name, "..")))
1290 continue; 1285 continue;
1291 if (NULL != callback) 1286 if (NULL != callback)
1292 { 1287 {
@@ -1304,16 +1299,14 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1304 n_size, 1299 n_size,
1305 "%s%s%s", 1300 "%s%s%s",
1306 dname, 1301 dname,
1307 (0 == strcmp (dname, 1302 (0 == strcmp (dname, DIR_SEPARATOR_STR))
1308 DIR_SEPARATOR_STR)) 1303 ? ""
1309 ? "" 1304 : DIR_SEPARATOR_STR,
1310 : DIR_SEPARATOR_STR,
1311 finfo->d_name); 1305 finfo->d_name);
1312 ret = callback (callback_cls, 1306 ret = callback (callback_cls, name);
1313 name);
1314 if (GNUNET_OK != ret) 1307 if (GNUNET_OK != ret)
1315 { 1308 {
1316 CLOSEDIR (dinfo); 1309 closedir (dinfo);
1317 GNUNET_free (name); 1310 GNUNET_free (name);
1318 GNUNET_free (dname); 1311 GNUNET_free (dname);
1319 if (GNUNET_NO == ret) 1312 if (GNUNET_NO == ret)
@@ -1323,7 +1316,7 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1323 } 1316 }
1324 count++; 1317 count++;
1325 } 1318 }
1326 CLOSEDIR (dinfo); 1319 closedir (dinfo);
1327 GNUNET_free (name); 1320 GNUNET_free (name);
1328 GNUNET_free (dname); 1321 GNUNET_free (dname);
1329 return count; 1322 return count;
@@ -1339,8 +1332,7 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1339 * @return #GNUNET_OK 1332 * @return #GNUNET_OK
1340 */ 1333 */
1341static int 1334static int
1342remove_helper (void *unused, 1335remove_helper (void *unused, const char *fn)
1343 const char *fn)
1344{ 1336{
1345 (void) unused; 1337 (void) unused;
1346 (void) GNUNET_DISK_directory_remove (fn); 1338 (void) GNUNET_DISK_directory_remove (fn);
@@ -1365,35 +1357,26 @@ GNUNET_DISK_directory_remove (const char *filename)
1365 GNUNET_break (0); 1357 GNUNET_break (0);
1366 return GNUNET_SYSERR; 1358 return GNUNET_SYSERR;
1367 } 1359 }
1368 if (0 != LSTAT (filename, &istat)) 1360 if (0 != lstat (filename, &istat))
1369 return GNUNET_NO; /* file may not exist... */ 1361 return GNUNET_NO; /* file may not exist... */
1370 (void) CHMOD (filename, 1362 (void) chmod (filename, S_IWUSR | S_IRUSR | S_IXUSR);
1371 S_IWUSR | S_IRUSR | S_IXUSR); 1363 if (0 == unlink (filename))
1372 if (0 == UNLINK (filename))
1373 return GNUNET_OK; 1364 return GNUNET_OK;
1374 if ( (errno != EISDIR) && 1365 if ((errno != EISDIR) &&
1375 /* EISDIR is not sufficient in all cases, e.g. 1366 /* EISDIR is not sufficient in all cases, e.g.
1376 * sticky /tmp directory may result in EPERM on BSD. 1367 * sticky /tmp directory may result in EPERM on BSD.
1377 * So we also explicitly check "isDirectory" */ 1368 * So we also explicitly check "isDirectory" */
1378 (GNUNET_YES != 1369 (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES)))
1379 GNUNET_DISK_directory_test (filename,
1380 GNUNET_YES)) )
1381 { 1370 {
1382 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 1371 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1383 "rmdir",
1384 filename);
1385 return GNUNET_SYSERR; 1372 return GNUNET_SYSERR;
1386 } 1373 }
1387 if (GNUNET_SYSERR == 1374 if (GNUNET_SYSERR ==
1388 GNUNET_DISK_directory_scan (filename, 1375 GNUNET_DISK_directory_scan (filename, &remove_helper, NULL))
1389 &remove_helper,
1390 NULL))
1391 return GNUNET_SYSERR; 1376 return GNUNET_SYSERR;
1392 if (0 != RMDIR (filename)) 1377 if (0 != rmdir (filename))
1393 { 1378 {
1394 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 1379 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1395 "rmdir",
1396 filename);
1397 return GNUNET_SYSERR; 1380 return GNUNET_SYSERR;
1398 } 1381 }
1399 return GNUNET_OK; 1382 return GNUNET_OK;
@@ -1408,8 +1391,7 @@ GNUNET_DISK_directory_remove (const char *filename)
1408 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1391 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1409 */ 1392 */
1410int 1393int
1411GNUNET_DISK_file_copy (const char *src, 1394GNUNET_DISK_file_copy (const char *src, const char *dst)
1412 const char *dst)
1413{ 1395{
1414 char *buf; 1396 char *buf;
1415 uint64_t pos; 1397 uint64_t pos;
@@ -1419,41 +1401,30 @@ GNUNET_DISK_file_copy (const char *src,
1419 struct GNUNET_DISK_FileHandle *in; 1401 struct GNUNET_DISK_FileHandle *in;
1420 struct GNUNET_DISK_FileHandle *out; 1402 struct GNUNET_DISK_FileHandle *out;
1421 1403
1422 if (GNUNET_OK != 1404 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
1423 GNUNET_DISK_file_size (src, 1405 {
1424 &size, 1406 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "stat", src);
1425 GNUNET_YES,
1426 GNUNET_YES))
1427 {
1428 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1429 "stat",
1430 src);
1431 return GNUNET_SYSERR; 1407 return GNUNET_SYSERR;
1432 } 1408 }
1433 pos = 0; 1409 pos = 0;
1434 in = GNUNET_DISK_file_open (src, 1410 in =
1435 GNUNET_DISK_OPEN_READ, 1411 GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
1436 GNUNET_DISK_PERM_NONE);
1437 if (! in) 1412 if (! in)
1438 { 1413 {
1439 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 1414 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", src);
1440 "open",
1441 src);
1442 return GNUNET_SYSERR; 1415 return GNUNET_SYSERR;
1443 } 1416 }
1444 out = 1417 out =
1445 GNUNET_DISK_file_open (dst, 1418 GNUNET_DISK_file_open (dst,
1446 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 1419 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
1447 GNUNET_DISK_OPEN_FAILIFEXISTS, 1420 GNUNET_DISK_OPEN_FAILIFEXISTS,
1448 GNUNET_DISK_PERM_USER_READ | 1421 GNUNET_DISK_PERM_USER_READ |
1449 GNUNET_DISK_PERM_USER_WRITE | 1422 GNUNET_DISK_PERM_USER_WRITE |
1450 GNUNET_DISK_PERM_GROUP_READ | 1423 GNUNET_DISK_PERM_GROUP_READ |
1451 GNUNET_DISK_PERM_GROUP_WRITE); 1424 GNUNET_DISK_PERM_GROUP_WRITE);
1452 if (!out) 1425 if (! out)
1453 { 1426 {
1454 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 1427 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", dst);
1455 "open",
1456 dst);
1457 GNUNET_DISK_file_close (in); 1428 GNUNET_DISK_file_close (in);
1458 return GNUNET_SYSERR; 1429 return GNUNET_SYSERR;
1459 } 1430 }
@@ -1463,17 +1434,11 @@ GNUNET_DISK_file_copy (const char *src,
1463 len = COPY_BLK_SIZE; 1434 len = COPY_BLK_SIZE;
1464 if (len > size - pos) 1435 if (len > size - pos)
1465 len = size - pos; 1436 len = size - pos;
1466 sret = GNUNET_DISK_file_read (in, 1437 sret = GNUNET_DISK_file_read (in, buf, len);
1467 buf, 1438 if ((sret < 0) || (len != (size_t) sret))
1468 len);
1469 if ( (sret < 0) ||
1470 (len != (size_t) sret) )
1471 goto FAIL; 1439 goto FAIL;
1472 sret = GNUNET_DISK_file_write (out, 1440 sret = GNUNET_DISK_file_write (out, buf, len);
1473 buf, 1441 if ((sret < 0) || (len != (size_t) sret))
1474 len);
1475 if ( (sret < 0) ||
1476 (len != (size_t) sret) )
1477 goto FAIL; 1442 goto FAIL;
1478 pos += len; 1443 pos += len;
1479 } 1444 }
@@ -1503,8 +1468,7 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1503 { 1468 {
1504 c = *idx; 1469 c = *idx;
1505 1470
1506 if (c == '/' || c == '\\' || c == ':' || 1471 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' ||
1507 c == '*' || c == '?' || c == '"' ||
1508 c == '<' || c == '>' || c == '|') 1472 c == '<' || c == '>' || c == '|')
1509 { 1473 {
1510 *idx = '_'; 1474 *idx = '_';
@@ -1513,7 +1477,6 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1513} 1477}
1514 1478
1515 1479
1516
1517/** 1480/**
1518 * @brief Change owner of a file 1481 * @brief Change owner of a file
1519 * 1482 *
@@ -1522,8 +1485,7 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1522 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 1485 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1523 */ 1486 */
1524int 1487int
1525GNUNET_DISK_file_change_owner (const char *filename, 1488GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1526 const char *user)
1527{ 1489{
1528#ifndef MINGW 1490#ifndef MINGW
1529 struct passwd *pws; 1491 struct passwd *pws;
@@ -1532,18 +1494,14 @@ GNUNET_DISK_file_change_owner (const char *filename,
1532 if (NULL == pws) 1494 if (NULL == pws)
1533 { 1495 {
1534 LOG (GNUNET_ERROR_TYPE_ERROR, 1496 LOG (GNUNET_ERROR_TYPE_ERROR,
1535 _("Cannot obtain information about user `%s': %s\n"), 1497 _ ("Cannot obtain information about user `%s': %s\n"),
1536 user, 1498 user,
1537 STRERROR (errno)); 1499 strerror (errno));
1538 return GNUNET_SYSERR; 1500 return GNUNET_SYSERR;
1539 } 1501 }
1540 if (0 != chown (filename, 1502 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1541 pws->pw_uid,
1542 pws->pw_gid))
1543 { 1503 {
1544 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 1504 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1545 "chown",
1546 filename);
1547 return GNUNET_SYSERR; 1505 return GNUNET_SYSERR;
1548 } 1506 }
1549#endif 1507#endif
@@ -1590,12 +1548,18 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
1590 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); 1548 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1591 1549
1592 memset (&o, 0, sizeof (OVERLAPPED)); 1550 memset (&o, 0, sizeof (OVERLAPPED));
1593 o.Offset = (DWORD) (lock_start & 0xFFFFFFFF);; 1551 o.Offset = (DWORD) (lock_start & 0xFFFFFFFF);
1594 o.OffsetHigh = (DWORD) (((lock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); 1552 ;
1595 1553 o.OffsetHigh =
1596 if (!LockFileEx 1554 (DWORD) (((lock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1597 (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY, 1555
1598 0, diff_low, diff_high, &o)) 1556 if (! LockFileEx (fh->h,
1557 (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) |
1558 LOCKFILE_FAIL_IMMEDIATELY,
1559 0,
1560 diff_low,
1561 diff_high,
1562 &o))
1599 { 1563 {
1600 SetErrnoFromWinError (GetLastError ()); 1564 SetErrnoFromWinError (GetLastError ());
1601 return GNUNET_SYSERR; 1565 return GNUNET_SYSERR;
@@ -1643,10 +1607,12 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
1643 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); 1607 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1644 1608
1645 memset (&o, 0, sizeof (OVERLAPPED)); 1609 memset (&o, 0, sizeof (OVERLAPPED));
1646 o.Offset = (DWORD) (unlock_start & 0xFFFFFFFF);; 1610 o.Offset = (DWORD) (unlock_start & 0xFFFFFFFF);
1647 o.OffsetHigh = (DWORD) (((unlock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); 1611 ;
1612 o.OffsetHigh = (DWORD) (
1613 ((unlock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1648 1614
1649 if (!UnlockFileEx (fh->h, 0, diff_low, diff_high, &o)) 1615 if (! UnlockFileEx (fh->h, 0, diff_low, diff_high, &o))
1650 { 1616 {
1651 SetErrnoFromWinError (GetLastError ()); 1617 SetErrnoFromWinError (GetLastError ());
1652 return GNUNET_SYSERR; 1618 return GNUNET_SYSERR;
@@ -1694,7 +1660,7 @@ GNUNET_DISK_file_open (const char *fn,
1694#ifndef MINGW 1660#ifndef MINGW
1695 mode = 0; 1661 mode = 0;
1696 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) 1662 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1697 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ 1663 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1698 else if (flags & GNUNET_DISK_OPEN_READ) 1664 else if (flags & GNUNET_DISK_OPEN_READ)
1699 oflags = O_RDONLY; 1665 oflags = O_RDONLY;
1700 else if (flags & GNUNET_DISK_OPEN_WRITE) 1666 else if (flags & GNUNET_DISK_OPEN_WRITE)
@@ -1706,26 +1672,28 @@ GNUNET_DISK_file_open (const char *fn,
1706 return NULL; 1672 return NULL;
1707 } 1673 }
1708 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) 1674 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1709 oflags |= (O_CREAT | O_EXCL); 1675 oflags |= (O_CREAT | O_EXCL);
1710 if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1676 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1711 oflags |= O_TRUNC; 1677 oflags |= O_TRUNC;
1712 if (flags & GNUNET_DISK_OPEN_APPEND) 1678 if (flags & GNUNET_DISK_OPEN_APPEND)
1713 oflags |= O_APPEND; 1679 oflags |= O_APPEND;
1714 if(GNUNET_NO == GNUNET_DISK_file_test(fn)) 1680 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
1715 { 1681 {
1716 if (flags & GNUNET_DISK_OPEN_CREATE ) 1682 if (flags & GNUNET_DISK_OPEN_CREATE)
1717 { 1683 {
1718 (void) GNUNET_DISK_directory_create_for_file (expfn); 1684 (void) GNUNET_DISK_directory_create_for_file (expfn);
1719 oflags |= O_CREAT; 1685 oflags |= O_CREAT;
1720 mode = translate_unix_perms (perm); 1686 mode = translate_unix_perms (perm);
1721 } 1687 }
1722 } 1688 }
1723 1689
1724 fd = open (expfn, oflags 1690 fd = open (expfn,
1691 oflags
1725#if O_CLOEXEC 1692#if O_CLOEXEC
1726 | O_CLOEXEC 1693 | O_CLOEXEC
1727#endif 1694#endif
1728 | O_LARGEFILE, mode); 1695 | O_LARGEFILE,
1696 mode);
1729 if (fd == -1) 1697 if (fd == -1)
1730 { 1698 {
1731 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) 1699 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
@@ -1767,10 +1735,14 @@ GNUNET_DISK_file_open (const char *fn,
1767 disp = OPEN_EXISTING; 1735 disp = OPEN_EXISTING;
1768 } 1736 }
1769 1737
1770 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(expfn, wexpfn)) 1738 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv (expfn, wexpfn))
1771 h = CreateFileW (wexpfn, access, 1739 h = CreateFileW (wexpfn,
1772 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 1740 access,
1773 disp, FILE_ATTRIBUTE_NORMAL, NULL); 1741 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
1742 NULL,
1743 disp,
1744 FILE_ATTRIBUTE_NORMAL,
1745 NULL);
1774 else 1746 else
1775 h = INVALID_HANDLE_VALUE; 1747 h = INVALID_HANDLE_VALUE;
1776 if (h == INVALID_HANDLE_VALUE) 1748 if (h == INVALID_HANDLE_VALUE)
@@ -1844,7 +1816,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1844 } 1816 }
1845 if (h->oOverlapWrite) 1817 if (h->oOverlapWrite)
1846 { 1818 {
1847 if (!CloseHandle (h->oOverlapWrite->hEvent)) 1819 if (! CloseHandle (h->oOverlapWrite->hEvent))
1848 { 1820 {
1849 SetErrnoFromWinError (GetLastError ()); 1821 SetErrnoFromWinError (GetLastError ());
1850 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); 1822 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
@@ -1888,8 +1860,8 @@ GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh)
1888 ftype = GNUNET_DISK_HANLDE_TYPE_PIPE; 1860 ftype = GNUNET_DISK_HANLDE_TYPE_PIPE;
1889 break; 1861 break;
1890 case FILE_TYPE_UNKNOWN: 1862 case FILE_TYPE_UNKNOWN:
1891 if ( (GetLastError () == NO_ERROR) || 1863 if ((GetLastError () == NO_ERROR) ||
1892 (GetLastError () == ERROR_INVALID_HANDLE) ) 1864 (GetLastError () == ERROR_INVALID_HANDLE))
1893 { 1865 {
1894 if (0 != ResetEvent (osfh)) 1866 if (0 != ResetEvent (osfh))
1895 ftype = GNUNET_DISK_HANLDE_TYPE_EVENT; 1867 ftype = GNUNET_DISK_HANLDE_TYPE_EVENT;
@@ -1938,8 +1910,7 @@ GNUNET_DISK_get_handle_from_int_fd (int fno)
1938{ 1910{
1939 struct GNUNET_DISK_FileHandle *fh; 1911 struct GNUNET_DISK_FileHandle *fh;
1940 1912
1941 if ( (((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && 1913 if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno))
1942 (EBADF == errno) )
1943 return NULL; /* invalid FD */ 1914 return NULL; /* invalid FD */
1944 1915
1945#ifndef WINDOWS 1916#ifndef WINDOWS
@@ -2019,7 +1990,8 @@ struct GNUNET_DISK_MapHandle
2019void * 1990void *
2020GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, 1991GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
2021 struct GNUNET_DISK_MapHandle **m, 1992 struct GNUNET_DISK_MapHandle **m,
2022 enum GNUNET_DISK_MapType access, size_t len) 1993 enum GNUNET_DISK_MapType access,
1994 size_t len)
2023{ 1995{
2024 if (NULL == h) 1996 if (NULL == h)
2025 { 1997 {
@@ -2062,7 +2034,7 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
2062 } 2034 }
2063 2035
2064 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); 2036 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
2065 if (!(*m)->addr) 2037 if (! (*m)->addr)
2066 { 2038 {
2067 SetErrnoFromWinError (GetLastError ()); 2039 SetErrnoFromWinError (GetLastError ());
2068 CloseHandle ((*m)->h); 2040 CloseHandle ((*m)->h);
@@ -2111,7 +2083,7 @@ GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
2111 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; 2083 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR;
2112 if (ret != GNUNET_OK) 2084 if (ret != GNUNET_OK)
2113 SetErrnoFromWinError (GetLastError ()); 2085 SetErrnoFromWinError (GetLastError ());
2114 if (!CloseHandle (h->h) && (ret == GNUNET_OK)) 2086 if (! CloseHandle (h->h) && (ret == GNUNET_OK))
2115 { 2087 {
2116 ret = GNUNET_SYSERR; 2088 ret = GNUNET_SYSERR;
2117 SetErrnoFromWinError (GetLastError ()); 2089 SetErrnoFromWinError (GetLastError ());
@@ -2155,7 +2127,7 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
2155 2127
2156#if WINDOWS 2128#if WINDOWS
2157#ifndef PIPE_BUF 2129#ifndef PIPE_BUF
2158#define PIPE_BUF 512 2130#define PIPE_BUF 512
2159#endif 2131#endif
2160/* Copyright Bob Byrnes <byrnes <at> curl.com> 2132/* Copyright Bob Byrnes <byrnes <at> curl.com>
2161 http://permalink.gmane.org/gmane.os.cygwin.patches/2121 2133 http://permalink.gmane.org/gmane.os.cygwin.patches/2121
@@ -2168,9 +2140,12 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
2168 Note that the return value is either NO_ERROR or GetLastError, 2140 Note that the return value is either NO_ERROR or GetLastError,
2169 unlike CreatePipe, which returns a bool for success or failure. */ 2141 unlike CreatePipe, which returns a bool for success or failure. */
2170static int 2142static int
2171create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, 2143create_selectable_pipe (PHANDLE read_pipe_ptr,
2172 LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize, 2144 PHANDLE write_pipe_ptr,
2173 DWORD dwReadMode, DWORD dwWriteMode) 2145 LPSECURITY_ATTRIBUTES sa_ptr,
2146 DWORD psize,
2147 DWORD dwReadMode,
2148 DWORD dwWriteMode)
2174{ 2149{
2175 /* Default to error. */ 2150 /* Default to error. */
2176 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; 2151 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE;
@@ -2191,10 +2166,15 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2191 { 2166 {
2192 static volatile LONG pipe_unique_id; 2167 static volatile LONG pipe_unique_id;
2193 2168
2194 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", 2169 snprintf (pipename,
2195 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); 2170 sizeof pipename,
2196 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n", 2171 "\\\\.\\pipe\\gnunet-%d-%ld",
2197 pipename, psize); 2172 getpid (),
2173 InterlockedIncrement ((LONG *) &pipe_unique_id));
2174 LOG (GNUNET_ERROR_TYPE_DEBUG,
2175 "CreateNamedPipe: name = %s, size = %lu\n",
2176 pipename,
2177 psize);
2198 /* Use CreateNamedPipe instead of CreatePipe, because the latter 2178 /* Use CreateNamedPipe instead of CreatePipe, because the latter
2199 * returns a write handle that does not permit FILE_READ_ATTRIBUTES 2179 * returns a write handle that does not permit FILE_READ_ATTRIBUTES
2200 * access, on versions of win32 earlier than WinXP SP2. 2180 * access, on versions of win32 earlier than WinXP SP2.
@@ -2203,10 +2183,15 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2203 * It's important to only allow a single instance, to ensure that 2183 * It's important to only allow a single instance, to ensure that
2204 * the pipe was not created earlier by some other process, even if 2184 * the pipe was not created earlier by some other process, even if
2205 * the pid has been reused. */ 2185 * the pid has been reused. */
2206 read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */ 2186 read_pipe = CreateNamedPipeA (pipename,
2207 psize, /* output buffer size */ 2187 PIPE_ACCESS_INBOUND |
2208 psize, /* input buffer size */ 2188 FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode,
2209 NMPWAIT_USE_DEFAULT_WAIT, sa_ptr); 2189 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
2190 1, /* max instances */
2191 psize, /* output buffer size */
2192 psize, /* input buffer size */
2193 NMPWAIT_USE_DEFAULT_WAIT,
2194 sa_ptr);
2210 2195
2211 if (read_pipe != INVALID_HANDLE_VALUE) 2196 if (read_pipe != INVALID_HANDLE_VALUE)
2212 { 2197 {
@@ -2233,10 +2218,12 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2233 * Return an anonymous pipe as the best approximation. */ 2218 * Return an anonymous pipe as the best approximation. */
2234 LOG (GNUNET_ERROR_TYPE_DEBUG, 2219 LOG (GNUNET_ERROR_TYPE_DEBUG,
2235 "CreateNamedPipe not implemented, resorting to " 2220 "CreateNamedPipe not implemented, resorting to "
2236 "CreatePipe: size = %lu\n", psize); 2221 "CreatePipe: size = %lu\n",
2222 psize);
2237 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) 2223 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize))
2238 { 2224 {
2239 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p, write handle = %p\n", 2225 LOG (GNUNET_ERROR_TYPE_DEBUG,
2226 "pipe read handle = %p, write handle = %p\n",
2240 *read_pipe_ptr, 2227 *read_pipe_ptr,
2241 *write_pipe_ptr); 2228 *write_pipe_ptr);
2242 return GNUNET_OK; 2229 return GNUNET_OK;
@@ -2254,8 +2241,12 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2254 2241
2255 /* Open the named pipe for writing. 2242 /* Open the named pipe for writing.
2256 * Be sure to permit FILE_READ_ATTRIBUTES access. */ 2243 * Be sure to permit FILE_READ_ATTRIBUTES access. */
2257 write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */ 2244 write_pipe = CreateFileA (pipename,
2258 sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */ 2245 GENERIC_WRITE | FILE_READ_ATTRIBUTES,
2246 0, /* share mode */
2247 sa_ptr,
2248 OPEN_EXISTING,
2249 dwWriteMode, /* flags and attributes */
2259 0); /* handle to template file */ 2250 0); /* handle to template file */
2260 2251
2261 if (write_pipe == INVALID_HANDLE_VALUE) 2252 if (write_pipe == INVALID_HANDLE_VALUE)
@@ -2287,9 +2278,9 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2287 */ 2278 */
2288struct GNUNET_DISK_PipeHandle * 2279struct GNUNET_DISK_PipeHandle *
2289GNUNET_DISK_pipe (int blocking_read, 2280GNUNET_DISK_pipe (int blocking_read,
2290 int blocking_write, 2281 int blocking_write,
2291 int inherit_read, 2282 int inherit_read,
2292 int inherit_write) 2283 int inherit_write)
2293{ 2284{
2294#ifndef MINGW 2285#ifndef MINGW
2295 int fd[2]; 2286 int fd[2];
@@ -2302,14 +2293,11 @@ GNUNET_DISK_pipe (int blocking_read,
2302 if (ret == -1) 2293 if (ret == -1)
2303 { 2294 {
2304 eno = errno; 2295 eno = errno;
2305 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 2296 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
2306 "pipe");
2307 errno = eno; 2297 errno = eno;
2308 return NULL; 2298 return NULL;
2309 } 2299 }
2310 return GNUNET_DISK_pipe_from_fd (blocking_read, 2300 return GNUNET_DISK_pipe_from_fd (blocking_read, blocking_write, fd);
2311 blocking_write,
2312 fd);
2313#else 2301#else
2314 struct GNUNET_DISK_PipeHandle *p; 2302 struct GNUNET_DISK_PipeHandle *p;
2315 BOOL ret; 2303 BOOL ret;
@@ -2329,11 +2317,13 @@ GNUNET_DISK_pipe (int blocking_read,
2329 * Pipes are not seekable, and need no offsets, which is 2317 * Pipes are not seekable, and need no offsets, which is
2330 * probably why it works for them. 2318 * probably why it works for them.
2331 */ 2319 */
2332 ret = 2320 ret = create_selectable_pipe (&p->fd[0]->h,
2333 create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, 2321 &p->fd[1]->h,
2334 FILE_FLAG_OVERLAPPED, 2322 NULL,
2335 FILE_FLAG_OVERLAPPED); 2323 0,
2336 if (!ret) 2324 FILE_FLAG_OVERLAPPED,
2325 FILE_FLAG_OVERLAPPED);
2326 if (! ret)
2337 { 2327 {
2338 SetErrnoFromWinError (GetLastError ()); 2328 SetErrnoFromWinError (GetLastError ());
2339 save_errno = errno; 2329 save_errno = errno;
@@ -2343,9 +2333,13 @@ GNUNET_DISK_pipe (int blocking_read,
2343 errno = save_errno; 2333 errno = save_errno;
2344 return NULL; 2334 return NULL;
2345 } 2335 }
2346 if (!DuplicateHandle 2336 if (! DuplicateHandle (GetCurrentProcess (),
2347 (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 0, 2337 p->fd[0]->h,
2348 inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) 2338 GetCurrentProcess (),
2339 &tmp_handle,
2340 0,
2341 inherit_read == GNUNET_YES ? TRUE : FALSE,
2342 DUPLICATE_SAME_ACCESS))
2349 { 2343 {
2350 SetErrnoFromWinError (GetLastError ()); 2344 SetErrnoFromWinError (GetLastError ());
2351 save_errno = errno; 2345 save_errno = errno;
@@ -2360,9 +2354,13 @@ GNUNET_DISK_pipe (int blocking_read,
2360 CloseHandle (p->fd[0]->h); 2354 CloseHandle (p->fd[0]->h);
2361 p->fd[0]->h = tmp_handle; 2355 p->fd[0]->h = tmp_handle;
2362 2356
2363 if (!DuplicateHandle 2357 if (! DuplicateHandle (GetCurrentProcess (),
2364 (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 0, 2358 p->fd[1]->h,
2365 inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) 2359 GetCurrentProcess (),
2360 &tmp_handle,
2361 0,
2362 inherit_write == GNUNET_YES ? TRUE : FALSE,
2363 DUPLICATE_SAME_ACCESS))
2366 { 2364 {
2367 SetErrnoFromWinError (GetLastError ()); 2365 SetErrnoFromWinError (GetLastError ());
2368 save_errno = errno; 2366 save_errno = errno;
@@ -2423,14 +2421,14 @@ GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
2423 { 2421 {
2424 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle); 2422 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2425 p->fd[0]->fd = fd[0]; 2423 p->fd[0]->fd = fd[0];
2426 if (!blocking_read) 2424 if (! blocking_read)
2427 { 2425 {
2428 flags = fcntl (fd[0], F_GETFL); 2426 flags = fcntl (fd[0], F_GETFL);
2429 flags |= O_NONBLOCK; 2427 flags |= O_NONBLOCK;
2430 if (0 > fcntl (fd[0], F_SETFL, flags)) 2428 if (0 > fcntl (fd[0], F_SETFL, flags))
2431 { 2429 {
2432 ret = -1; 2430 ret = -1;
2433 eno = errno; 2431 eno = errno;
2434 } 2432 }
2435 } 2433 }
2436 flags = fcntl (fd[0], F_GETFD); 2434 flags = fcntl (fd[0], F_GETFD);
@@ -2446,14 +2444,14 @@ GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
2446 { 2444 {
2447 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle); 2445 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2448 p->fd[1]->fd = fd[1]; 2446 p->fd[1]->fd = fd[1];
2449 if (!blocking_write) 2447 if (! blocking_write)
2450 { 2448 {
2451 flags = fcntl (fd[1], F_GETFL); 2449 flags = fcntl (fd[1], F_GETFL);
2452 flags |= O_NONBLOCK; 2450 flags |= O_NONBLOCK;
2453 if (0 > fcntl (fd[1], F_SETFL, flags)) 2451 if (0 > fcntl (fd[1], F_SETFL, flags))
2454 { 2452 {
2455 ret = -1; 2453 ret = -1;
2456 eno = errno; 2454 eno = errno;
2457 } 2455 }
2458 } 2456 }
2459 flags = fcntl (fd[1], F_GETFD); 2457 flags = fcntl (fd[1], F_GETFD);
@@ -2663,7 +2661,8 @@ GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
2663 */ 2661 */
2664int 2662int
2665GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, 2663GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
2666 void *dst, size_t dst_len) 2664 void *dst,
2665 size_t dst_len)
2667{ 2666{
2668 if (NULL == fh) 2667 if (NULL == fh)
2669 return GNUNET_SYSERR; 2668 return GNUNET_SYSERR;
@@ -2689,29 +2688,20 @@ GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
2689 * @return #GNUNET_OK on success 2688 * @return #GNUNET_OK on success
2690 */ 2689 */
2691static int 2690static int
2692purge_cfg_dir (void *cls, 2691purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
2693 const struct GNUNET_CONFIGURATION_Handle *cfg)
2694{ 2692{
2695 const char *option = cls; 2693 const char *option = cls;
2696 char *tmpname; 2694 char *tmpname;
2697 2695
2698 if (GNUNET_OK != 2696 if (GNUNET_OK !=
2699 GNUNET_CONFIGURATION_get_value_filename (cfg, 2697 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname))
2700 "PATHS", 2698 {
2701 option, 2699 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PATHS", option);
2702 &tmpname))
2703 {
2704 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2705 "PATHS",
2706 option);
2707 return GNUNET_NO; 2700 return GNUNET_NO;
2708 } 2701 }
2709 if (GNUNET_SYSERR == 2702 if (GNUNET_SYSERR == GNUNET_DISK_directory_remove (tmpname))
2710 GNUNET_DISK_directory_remove (tmpname))
2711 { 2703 {
2712 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 2704 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "remove", tmpname);
2713 "remove",
2714 tmpname);
2715 GNUNET_free (tmpname); 2705 GNUNET_free (tmpname);
2716 return GNUNET_OK; 2706 return GNUNET_OK;
2717 } 2707 }
@@ -2728,13 +2718,12 @@ purge_cfg_dir (void *cls,
2728 * @param option option with the dir name to purge 2718 * @param option option with the dir name to purge
2729 */ 2719 */
2730void 2720void
2731GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, 2721GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, const char *option)
2732 const char *option)
2733{ 2722{
2734 GNUNET_break (GNUNET_OK == 2723 GNUNET_break (GNUNET_OK ==
2735 GNUNET_CONFIGURATION_parse_and_run (cfg_filename, 2724 GNUNET_CONFIGURATION_parse_and_run (cfg_filename,
2736 &purge_cfg_dir, 2725 &purge_cfg_dir,
2737 (void *) option)); 2726 (void *) option));
2738} 2727}
2739 2728
2740 2729
diff --git a/src/util/dnsstub.c b/src/util/dnsstub.c
index a16a9a7dd..aa0c2191a 100644
--- a/src/util/dnsstub.c
+++ b/src/util/dnsstub.c
@@ -28,7 +28,8 @@
28/** 28/**
29 * Timeout for retrying DNS queries. 29 * Timeout for retrying DNS queries.
30 */ 30 */
31#define DNS_RETRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250) 31#define DNS_RETRANSMIT_DELAY \
32 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
32 33
33 34
34/** 35/**
@@ -92,7 +93,6 @@ struct GNUNET_DNSSTUB_RequestSocket
92 * Number of bytes in @a request. 93 * Number of bytes in @a request.
93 */ 94 */
94 size_t request_len; 95 size_t request_len;
95
96}; 96};
97 97
98 98
@@ -149,7 +149,6 @@ struct GNUNET_DNSSTUB_Context
149 * Length of @e sockets array. 149 * Length of @e sockets array.
150 */ 150 */
151 unsigned int num_sockets; 151 unsigned int num_sockets;
152
153}; 152};
154 153
155 154
@@ -223,13 +222,11 @@ open_socket (int af)
223 return NULL; 222 return NULL;
224 } 223 }
225 sa->sa_family = af; 224 sa->sa_family = af;
226 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret, 225 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret, sa, alen))
227 sa,
228 alen))
229 { 226 {
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 _("Could not bind to any port: %s\n"), 228 _ ("Could not bind to any port: %s\n"),
232 STRERROR (errno)); 229 strerror (errno));
233 GNUNET_NETWORK_socket_close (ret); 230 GNUNET_NETWORK_socket_close (ret);
234 return NULL; 231 return NULL;
235 } 232 }
@@ -249,7 +246,7 @@ get_request_socket (struct GNUNET_DNSSTUB_Context *ctx)
249{ 246{
250 struct GNUNET_DNSSTUB_RequestSocket *rs; 247 struct GNUNET_DNSSTUB_RequestSocket *rs;
251 248
252 for (unsigned int i=0;i<256;i++) 249 for (unsigned int i = 0; i < 256; i++)
253 { 250 {
254 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 251 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
255 ctx->num_sockets)]; 252 ctx->num_sockets)];
@@ -259,9 +256,7 @@ get_request_socket (struct GNUNET_DNSSTUB_Context *ctx)
259 if (NULL != rs->rc) 256 if (NULL != rs->rc)
260 { 257 {
261 /* signal "failure" */ 258 /* signal "failure" */
262 rs->rc (rs->rc_cls, 259 rs->rc (rs->rc_cls, NULL, 0);
263 NULL,
264 0);
265 rs->rc = NULL; 260 rs->rc = NULL;
266 } 261 }
267 if (NULL != rs->read_task) 262 if (NULL != rs->read_task)
@@ -294,16 +289,14 @@ get_request_socket (struct GNUNET_DNSSTUB_Context *ctx)
294 */ 289 */
295static int 290static int
296do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs, 291do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
297 struct GNUNET_NETWORK_Handle *dnsout) 292 struct GNUNET_NETWORK_Handle *dnsout)
298{ 293{
299 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx; 294 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx;
300 ssize_t r; 295 ssize_t r;
301 int len; 296 int len;
302 297
303#ifndef MINGW 298#ifndef MINGW
304 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), 299 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
305 FIONREAD,
306 &len))
307 { 300 {
308 /* conservative choice: */ 301 /* conservative choice: */
309 len = UINT16_MAX; 302 len = UINT16_MAX;
@@ -312,9 +305,7 @@ do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
312 /* port the code above? */ 305 /* port the code above? */
313 len = UINT16_MAX; 306 len = UINT16_MAX;
314#endif 307#endif
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving %d byte DNS reply\n", len);
316 "Receiving %d byte DNS reply\n",
317 len);
318 { 309 {
319 unsigned char buf[len] GNUNET_ALIGN; 310 unsigned char buf[len] GNUNET_ALIGN;
320 int found; 311 int found;
@@ -323,18 +314,15 @@ do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
323 struct GNUNET_TUN_DnsHeader *dns; 314 struct GNUNET_TUN_DnsHeader *dns;
324 315
325 addrlen = sizeof (addr); 316 addrlen = sizeof (addr);
326 memset (&addr, 317 memset (&addr, 0, sizeof (addr));
327 0,
328 sizeof (addr));
329 r = GNUNET_NETWORK_socket_recvfrom (dnsout, 318 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
330 buf, 319 buf,
331 sizeof (buf), 320 sizeof (buf),
332 (struct sockaddr*) &addr, 321 (struct sockaddr *) &addr,
333 &addrlen); 322 &addrlen);
334 if (-1 == r) 323 if (-1 == r)
335 { 324 {
336 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 325 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
337 "recvfrom");
338 GNUNET_NETWORK_socket_close (dnsout); 326 GNUNET_NETWORK_socket_close (dnsout);
339 return GNUNET_SYSERR; 327 return GNUNET_SYSERR;
340 } 328 }
@@ -343,8 +331,7 @@ do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
343 { 331 {
344 if (0 == memcmp (&addr, 332 if (0 == memcmp (&addr,
345 &ds->ss, 333 &ds->ss,
346 GNUNET_MIN (sizeof (struct sockaddr_storage), 334 GNUNET_MIN (sizeof (struct sockaddr_storage), addrlen)))
347 addrlen)))
348 { 335 {
349 found = GNUNET_YES; 336 found = GNUNET_YES;
350 break; 337 break;
@@ -353,26 +340,24 @@ do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
353 if (GNUNET_NO == found) 340 if (GNUNET_NO == found)
354 { 341 {
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Received DNS response from server we never asked (ignored)"); 343 "Received DNS response from server we never asked (ignored)");
357 return GNUNET_NO; 344 return GNUNET_NO;
358 } 345 }
359 if (sizeof (struct GNUNET_TUN_DnsHeader) > (size_t) r) 346 if (sizeof (struct GNUNET_TUN_DnsHeader) > (size_t) r)
360 { 347 {
361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
362 _("Received DNS response that is too small (%u bytes)"), 349 _ ("Received DNS response that is too small (%u bytes)"),
363 (unsigned int) r); 350 (unsigned int) r);
364 return GNUNET_NO; 351 return GNUNET_NO;
365 } 352 }
366 dns = (struct GNUNET_TUN_DnsHeader *) buf; 353 dns = (struct GNUNET_TUN_DnsHeader *) buf;
367 if (NULL == rs->rc) 354 if (NULL == rs->rc)
368 { 355 {
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Request timeout or cancelled; ignoring reply\n"); 357 "Request timeout or cancelled; ignoring reply\n");
371 return GNUNET_NO; 358 return GNUNET_NO;
372 } 359 }
373 rs->rc (rs->rc_cls, 360 rs->rc (rs->rc_cls, dns, r);
374 dns,
375 r);
376 } 361 }
377 return GNUNET_OK; 362 return GNUNET_OK;
378} 363}
@@ -401,17 +386,16 @@ schedule_read (struct GNUNET_DNSSTUB_RequestSocket *rs)
401 GNUNET_SCHEDULER_cancel (rs->read_task); 386 GNUNET_SCHEDULER_cancel (rs->read_task);
402 rset = GNUNET_NETWORK_fdset_create (); 387 rset = GNUNET_NETWORK_fdset_create ();
403 if (NULL != rs->dnsout4) 388 if (NULL != rs->dnsout4)
404 GNUNET_NETWORK_fdset_set (rset, 389 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
405 rs->dnsout4);
406 if (NULL != rs->dnsout6) 390 if (NULL != rs->dnsout6)
407 GNUNET_NETWORK_fdset_set (rset, 391 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
408 rs->dnsout6); 392 rs->read_task =
409 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 393 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
410 GNUNET_TIME_UNIT_FOREVER_REL, 394 GNUNET_TIME_UNIT_FOREVER_REL,
411 rset, 395 rset,
412 NULL, 396 NULL,
413 &read_response, 397 &read_response,
414 rs); 398 rs);
415 GNUNET_NETWORK_fdset_destroy (rset); 399 GNUNET_NETWORK_fdset_destroy (rset);
416} 400}
417 401
@@ -430,19 +414,13 @@ read_response (void *cls)
430 rs->read_task = NULL; 414 rs->read_task = NULL;
431 tc = GNUNET_SCHEDULER_get_task_context (); 415 tc = GNUNET_SCHEDULER_get_task_context ();
432 /* read and process ready sockets */ 416 /* read and process ready sockets */
433 if ( (NULL != rs->dnsout4) && 417 if ((NULL != rs->dnsout4) &&
434 (GNUNET_NETWORK_fdset_isset (tc->read_ready, 418 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
435 rs->dnsout4)) && 419 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout4)))
436 (GNUNET_SYSERR ==
437 do_dns_read (rs,
438 rs->dnsout4)) )
439 rs->dnsout4 = NULL; 420 rs->dnsout4 = NULL;
440 if ( (NULL != rs->dnsout6) && 421 if ((NULL != rs->dnsout6) &&
441 (GNUNET_NETWORK_fdset_isset (tc->read_ready, 422 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
442 rs->dnsout6)) && 423 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout6)))
443 (GNUNET_SYSERR ==
444 do_dns_read (rs,
445 rs->dnsout6)) )
446 rs->dnsout6 = NULL; 424 rs->dnsout6 = NULL;
447 /* re-schedule read task */ 425 /* re-schedule read task */
448 schedule_read (rs); 426 schedule_read (rs);
@@ -465,9 +443,8 @@ transmit_query (void *cls)
465 struct DnsServer *ds; 443 struct DnsServer *ds;
466 struct GNUNET_NETWORK_Handle *dnsout; 444 struct GNUNET_NETWORK_Handle *dnsout;
467 445
468 rs->retry_task = GNUNET_SCHEDULER_add_delayed (ctx->retry_freq, 446 rs->retry_task =
469 &transmit_query, 447 GNUNET_SCHEDULER_add_delayed (ctx->retry_freq, &transmit_query, rs);
470 rs);
471 ds = rs->ds_pos; 448 ds = rs->ds_pos;
472 rs->ds_pos = ds->next; 449 rs->ds_pos = ds->next;
473 if (NULL == rs->ds_pos) 450 if (NULL == rs->ds_pos)
@@ -499,22 +476,19 @@ transmit_query (void *cls)
499 "Unable to use configure DNS server, skipping\n"); 476 "Unable to use configure DNS server, skipping\n");
500 return; 477 return;
501 } 478 }
502 if (GNUNET_SYSERR == 479 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_sendto (dnsout,
503 GNUNET_NETWORK_socket_sendto (dnsout, 480 rs->request,
504 rs->request, 481 rs->request_len,
505 rs->request_len, 482 sa,
506 sa, 483 salen))
507 salen))
508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
509 _("Failed to send DNS request to %s: %s\n"), 485 _ ("Failed to send DNS request to %s: %s\n"),
510 GNUNET_a2s (sa, 486 GNUNET_a2s (sa, salen),
511 salen), 487 strerror (errno));
512 STRERROR (errno));
513 else 488 else
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 _("Sent DNS request to %s\n"), 490 _ ("Sent DNS request to %s\n"),
516 GNUNET_a2s (sa, 491 GNUNET_a2s (sa, salen));
517 salen));
518 schedule_read (rs); 492 schedule_read (rs);
519} 493}
520 494
@@ -531,10 +505,10 @@ transmit_query (void *cls)
531 */ 505 */
532struct GNUNET_DNSSTUB_RequestSocket * 506struct GNUNET_DNSSTUB_RequestSocket *
533GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx, 507GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx,
534 const void *request, 508 const void *request,
535 size_t request_len, 509 size_t request_len,
536 GNUNET_DNSSTUB_ResultCallback rc, 510 GNUNET_DNSSTUB_ResultCallback rc,
537 void *rc_cls) 511 void *rc_cls)
538{ 512{
539 struct GNUNET_DNSSTUB_RequestSocket *rs; 513 struct GNUNET_DNSSTUB_RequestSocket *rs;
540 514
@@ -553,11 +527,9 @@ GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx,
553 rs->ds_pos = ctx->dns_head; 527 rs->ds_pos = ctx->dns_head;
554 rs->rc = rc; 528 rs->rc = rc;
555 rs->rc_cls = rc_cls; 529 rs->rc_cls = rc_cls;
556 rs->request = GNUNET_memdup (request, 530 rs->request = GNUNET_memdup (request, request_len);
557 request_len);
558 rs->request_len = request_len; 531 rs->request_len = request_len;
559 rs->retry_task = GNUNET_SCHEDULER_add_now (&transmit_query, 532 rs->retry_task = GNUNET_SCHEDULER_add_now (&transmit_query, rs);
560 rs);
561 return rs; 533 return rs;
562} 534}
563 535
@@ -603,8 +575,8 @@ GNUNET_DNSSTUB_start (unsigned int num_sockets)
603 } 575 }
604 ctx = GNUNET_new (struct GNUNET_DNSSTUB_Context); 576 ctx = GNUNET_new (struct GNUNET_DNSSTUB_Context);
605 ctx->num_sockets = num_sockets; 577 ctx->num_sockets = num_sockets;
606 ctx->sockets = GNUNET_new_array (num_sockets, 578 ctx->sockets =
607 struct GNUNET_DNSSTUB_RequestSocket); 579 GNUNET_new_array (num_sockets, struct GNUNET_DNSSTUB_RequestSocket);
608 ctx->retry_freq = DNS_RETRANSMIT_DELAY; 580 ctx->retry_freq = DNS_RETRANSMIT_DELAY;
609 return ctx; 581 return ctx;
610} 582}
@@ -627,9 +599,7 @@ GNUNET_DNSSTUB_add_dns_ip (struct GNUNET_DNSSTUB_Context *ctx,
627 struct in6_addr i6; 599 struct in6_addr i6;
628 600
629 ds = GNUNET_new (struct DnsServer); 601 ds = GNUNET_new (struct DnsServer);
630 if (1 == inet_pton (AF_INET, 602 if (1 == inet_pton (AF_INET, dns_ip, &i4))
631 dns_ip,
632 &i4))
633 { 603 {
634 struct sockaddr_in *s4 = (struct sockaddr_in *) &ds->ss; 604 struct sockaddr_in *s4 = (struct sockaddr_in *) &ds->ss;
635 605
@@ -640,9 +610,7 @@ GNUNET_DNSSTUB_add_dns_ip (struct GNUNET_DNSSTUB_Context *ctx,
640 s4->sin_len = (u_char) sizeof (struct sockaddr_in); 610 s4->sin_len = (u_char) sizeof (struct sockaddr_in);
641#endif 611#endif
642 } 612 }
643 else if (1 == inet_pton (AF_INET6, 613 else if (1 == inet_pton (AF_INET6, dns_ip, &i6))
644 dns_ip,
645 &i6))
646 { 614 {
647 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &ds->ss; 615 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &ds->ss;
648 616
@@ -661,9 +629,7 @@ GNUNET_DNSSTUB_add_dns_ip (struct GNUNET_DNSSTUB_Context *ctx,
661 GNUNET_free (ds); 629 GNUNET_free (ds);
662 return GNUNET_SYSERR; 630 return GNUNET_SYSERR;
663 } 631 }
664 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, 632 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, ctx->dns_tail, ds);
665 ctx->dns_tail,
666 ds);
667 return GNUNET_OK; 633 return GNUNET_OK;
668} 634}
669 635
@@ -686,23 +652,17 @@ GNUNET_DNSSTUB_add_dns_sa (struct GNUNET_DNSSTUB_Context *ctx,
686 switch (sa->sa_family) 652 switch (sa->sa_family)
687 { 653 {
688 case AF_INET: 654 case AF_INET:
689 GNUNET_memcpy (&ds->ss, 655 GNUNET_memcpy (&ds->ss, sa, sizeof (struct sockaddr_in));
690 sa,
691 sizeof (struct sockaddr_in));
692 break; 656 break;
693 case AF_INET6: 657 case AF_INET6:
694 GNUNET_memcpy (&ds->ss, 658 GNUNET_memcpy (&ds->ss, sa, sizeof (struct sockaddr_in6));
695 sa,
696 sizeof (struct sockaddr_in6));
697 break; 659 break;
698 default: 660 default:
699 GNUNET_break (0); 661 GNUNET_break (0);
700 GNUNET_free (ds); 662 GNUNET_free (ds);
701 return GNUNET_SYSERR; 663 return GNUNET_SYSERR;
702 } 664 }
703 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, 665 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, ctx->dns_tail, ds);
704 ctx->dns_tail,
705 ds);
706 return GNUNET_OK; 666 return GNUNET_OK;
707} 667}
708 668
@@ -734,12 +694,10 @@ GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
734 694
735 while (NULL != (ds = ctx->dns_head)) 695 while (NULL != (ds = ctx->dns_head))
736 { 696 {
737 GNUNET_CONTAINER_DLL_remove (ctx->dns_head, 697 GNUNET_CONTAINER_DLL_remove (ctx->dns_head, ctx->dns_tail, ds);
738 ctx->dns_tail,
739 ds);
740 GNUNET_free (ds); 698 GNUNET_free (ds);
741 } 699 }
742 for (unsigned int i=0;i<ctx->num_sockets;i++) 700 for (unsigned int i = 0; i < ctx->num_sockets; i++)
743 cleanup_rs (&ctx->sockets[i]); 701 cleanup_rs (&ctx->sockets[i]);
744 GNUNET_free (ctx->sockets); 702 GNUNET_free (ctx->sockets);
745 GNUNET_free (ctx); 703 GNUNET_free (ctx);
diff --git a/src/util/getopt.c b/src/util/getopt.c
index 048f52ee0..678e2a61f 100644
--- a/src/util/getopt.c
+++ b/src/util/getopt.c
@@ -563,7 +563,7 @@ GN_getopt_internal (int argc,
563 if (ambig && ! exact) 563 if (ambig && ! exact)
564 { 564 {
565 if (GNopterr) 565 if (GNopterr)
566 FPRINTF (stderr, 566 fprintf (stderr,
567 _ ("%s: option `%s' is ambiguous\n"), 567 _ ("%s: option `%s' is ambiguous\n"),
568 argv[0], 568 argv[0],
569 argv[GNoptind]); 569 argv[GNoptind]);
@@ -588,13 +588,13 @@ GN_getopt_internal (int argc,
588 { 588 {
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], 593 argv[0],
594 pfound->name); 594 pfound->name);
595 else 595 else
596 /* +option or -option */ 596 /* +option or -option */
597 FPRINTF (stderr, 597 fprintf (stderr,
598 _ ("%s: option `%c%s' does not allow an argument\n"), 598 _ ("%s: option `%c%s' does not allow an argument\n"),
599 argv[0], 599 argv[0],
600 argv[GNoptind - 1][0], 600 argv[GNoptind - 1][0],
@@ -614,7 +614,7 @@ GN_getopt_internal (int argc,
614 { 614 {
615 if (GNopterr) 615 if (GNopterr)
616 { 616 {
617 FPRINTF (stderr, 617 fprintf (stderr,
618 _ ("%s: option `%s' requires an argument\n"), 618 _ ("%s: option `%s' requires an argument\n"),
619 argv[0], 619 argv[0],
620 argv[GNoptind - 1]); 620 argv[GNoptind - 1]);
@@ -645,13 +645,13 @@ GN_getopt_internal (int argc,
645 { 645 {
646 if (argv[GNoptind][1] == '-') 646 if (argv[GNoptind][1] == '-')
647 /* --option */ 647 /* --option */
648 FPRINTF (stderr, 648 fprintf (stderr,
649 _ ("%s: unrecognized option `--%s'\n"), 649 _ ("%s: unrecognized option `--%s'\n"),
650 argv[0], 650 argv[0],
651 nextchar); 651 nextchar);
652 else 652 else
653 /* +option or -option */ 653 /* +option or -option */
654 FPRINTF (stderr, 654 fprintf (stderr,
655 _ ("%s: unrecognized option `%c%s'\n"), 655 _ ("%s: unrecognized option `%c%s'\n"),
656 argv[0], 656 argv[0],
657 argv[GNoptind][0], 657 argv[GNoptind][0],
@@ -679,9 +679,9 @@ GN_getopt_internal (int argc,
679 { 679 {
680 if (posixly_correct) 680 if (posixly_correct)
681 /* 1003.2 specifies the format of this message. */ 681 /* 1003.2 specifies the format of this message. */
682 FPRINTF (stderr, _ ("%s: illegal option -- %c\n"), argv[0], c); 682 fprintf (stderr, _ ("%s: illegal option -- %c\n"), argv[0], c);
683 else 683 else
684 FPRINTF (stderr, _ ("%s: invalid option -- %c\n"), argv[0], c); 684 fprintf (stderr, _ ("%s: invalid option -- %c\n"), argv[0], c);
685 } 685 }
686 return '?'; 686 return '?';
687 } 687 }
@@ -709,7 +709,7 @@ GN_getopt_internal (int argc,
709 if (GNopterr) 709 if (GNopterr)
710 { 710 {
711 /* 1003.2 specifies the format of this message. */ 711 /* 1003.2 specifies the format of this message. */
712 FPRINTF (stderr, 712 fprintf (stderr,
713 _ ("%s: option requires an argument -- %c\n"), 713 _ ("%s: option requires an argument -- %c\n"),
714 argv[0], 714 argv[0],
715 c); 715 c);
@@ -759,7 +759,7 @@ GN_getopt_internal (int argc,
759 if (ambig && ! exact) 759 if (ambig && ! exact)
760 { 760 {
761 if (GNopterr) 761 if (GNopterr)
762 FPRINTF (stderr, 762 fprintf (stderr,
763 _ ("%s: option `-W %s' is ambiguous\n"), 763 _ ("%s: option `-W %s' is ambiguous\n"),
764 argv[0], 764 argv[0],
765 argv[GNoptind]); 765 argv[GNoptind]);
@@ -779,7 +779,7 @@ GN_getopt_internal (int argc,
779 else 779 else
780 { 780 {
781 if (GNopterr) 781 if (GNopterr)
782 FPRINTF (stderr, 782 fprintf (stderr,
783 _ ("%s: option `-W %s' does not allow an argument\n"), 783 _ ("%s: option `-W %s' does not allow an argument\n"),
784 argv[0], 784 argv[0],
785 pfound->name); 785 pfound->name);
@@ -795,7 +795,7 @@ GN_getopt_internal (int argc,
795 else 795 else
796 { 796 {
797 if (GNopterr) 797 if (GNopterr)
798 FPRINTF (stderr, 798 fprintf (stderr,
799 _ ("%s: option `%s' requires an argument\n"), 799 _ ("%s: option `%s' requires an argument\n"),
800 argv[0], 800 argv[0],
801 argv[GNoptind - 1]); 801 argv[GNoptind - 1]);
@@ -845,7 +845,7 @@ GN_getopt_internal (int argc,
845 if (GNopterr) 845 if (GNopterr)
846 { 846 {
847 /* 1003.2 specifies the format of this message. */ 847 /* 1003.2 specifies the format of this message. */
848 FPRINTF (stderr, 848 fprintf (stderr,
849 _ ("%s: option requires an argument -- %c\n"), 849 _ ("%s: option requires an argument -- %c\n"),
850 argv[0], 850 argv[0],
851 c); 851 c);
@@ -972,7 +972,7 @@ GNUNET_GETOPT_run (const char *binaryOptions,
972 } 972 }
973 if (i == count) 973 if (i == count)
974 { 974 {
975 FPRINTF (stderr, _ ("Use %s to get a list of options.\n"), "--help"); 975 fprintf (stderr, _ ("Use %s to get a list of options.\n"), "--help");
976 cont = GNUNET_SYSERR; 976 cont = GNUNET_SYSERR;
977 } 977 }
978 } 978 }
@@ -983,7 +983,7 @@ GNUNET_GETOPT_run (const char *binaryOptions,
983 is the only option that was provided */ 983 is the only option that was provided */
984 if ((NULL != have_exclusive) && (optmatch > 1)) 984 if ((NULL != have_exclusive) && (optmatch > 1))
985 { 985 {
986 FPRINTF (stderr, 986 fprintf (stderr,
987 _ ("Option `%s' can't be used with other options.\n"), 987 _ ("Option `%s' can't be used with other options.\n"),
988 have_exclusive); 988 have_exclusive);
989 cont = GNUNET_SYSERR; 989 cont = GNUNET_SYSERR;
@@ -995,7 +995,7 @@ GNUNET_GETOPT_run (const char *binaryOptions,
995 { 995 {
996 if ((0 == seen[count]) && (allOptions[count].option_mandatory)) 996 if ((0 == seen[count]) && (allOptions[count].option_mandatory))
997 { 997 {
998 FPRINTF (stderr, 998 fprintf (stderr,
999 _ ("Missing mandatory option `%s'.\n"), 999 _ ("Missing mandatory option `%s'.\n"),
1000 allOptions[count].name); 1000 allOptions[count].name);
1001 cont = GNUNET_SYSERR; 1001 cont = GNUNET_SYSERR;
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index 80d1037bb..f53441508 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -184,7 +184,7 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
184 184
185 if (0 != pd->is_gnu) 185 if (0 != pd->is_gnu)
186 printf ("General help using GNU software: http://www.gnu.org/gethelp/\n"); 186 printf ("General help using GNU software: http://www.gnu.org/gethelp/\n");
187 187
188 return GNUNET_NO; 188 return GNUNET_NO;
189} 189}
190 190
@@ -544,9 +544,9 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
544 char dummy[2]; 544 char dummy[2];
545 545
546 (void) ctx; 546 (void) ctx;
547 if (1 != SSCANF (value, "%llu%1s", val, dummy)) 547 if (1 != sscanf (value, "%llu%1s", val, dummy))
548 { 548 {
549 FPRINTF (stderr, 549 fprintf (stderr,
550 _ ("You must pass a number to the `%s' option.\n"), 550 _ ("You must pass a number to the `%s' option.\n"),
551 option); 551 option);
552 return GNUNET_SYSERR; 552 return GNUNET_SYSERR;
@@ -607,7 +607,7 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
607 (void) ctx; 607 (void) ctx;
608 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val)) 608 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val))
609 { 609 {
610 FPRINTF (stderr, 610 fprintf (stderr,
611 _ ("You must pass relative time to the `%s' option.\n"), 611 _ ("You must pass relative time to the `%s' option.\n"),
612 option); 612 option);
613 return GNUNET_SYSERR; 613 return GNUNET_SYSERR;
@@ -669,7 +669,7 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
669 (void) ctx; 669 (void) ctx;
670 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val)) 670 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val))
671 { 671 {
672 FPRINTF (stderr, 672 fprintf (stderr,
673 _ ("You must pass absolute time to the `%s' option.\n"), 673 _ ("You must pass absolute time to the `%s' option.\n"),
674 option); 674 option);
675 return GNUNET_SYSERR; 675 return GNUNET_SYSERR;
@@ -732,15 +732,15 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
732 (void) ctx; 732 (void) ctx;
733 if ('-' == *value) 733 if ('-' == *value)
734 { 734 {
735 FPRINTF (stderr, 735 fprintf (stderr,
736 _ ( 736 _ (
737 "Your input for the '%s' option has to be a non negative number \n"), 737 "Your input for the '%s' option has to be a non negative number \n"),
738 option); 738 option);
739 return GNUNET_SYSERR; 739 return GNUNET_SYSERR;
740 } 740 }
741 if (1 != SSCANF (value, "%u%1s", val, dummy)) 741 if (1 != sscanf (value, "%u%1s", val, dummy))
742 { 742 {
743 FPRINTF (stderr, 743 fprintf (stderr,
744 _ ("You must pass a number to the `%s' option.\n"), 744 _ ("You must pass a number to the `%s' option.\n"),
745 option); 745 option);
746 return GNUNET_SYSERR; 746 return GNUNET_SYSERR;
@@ -801,16 +801,16 @@ set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
801 char dummy[2]; 801 char dummy[2];
802 802
803 (void) ctx; 803 (void) ctx;
804 if (1 != SSCANF (value, "%u%1s", &v, dummy)) 804 if (1 != sscanf (value, "%u%1s", &v, dummy))
805 { 805 {
806 FPRINTF (stderr, 806 fprintf (stderr,
807 _ ("You must pass a number to the `%s' option.\n"), 807 _ ("You must pass a number to the `%s' option.\n"),
808 option); 808 option);
809 return GNUNET_SYSERR; 809 return GNUNET_SYSERR;
810 } 810 }
811 if (v > UINT16_MAX) 811 if (v > UINT16_MAX)
812 { 812 {
813 FPRINTF (stderr, 813 fprintf (stderr,
814 _ ("You must pass a number below %u to the `%s' option.\n"), 814 _ ("You must pass a number below %u to the `%s' option.\n"),
815 (unsigned int) UINT16_MAX, 815 (unsigned int) UINT16_MAX,
816 option); 816 option);
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c
index d2253337c..e7a170b81 100644
--- a/src/util/gnunet-ecc.c
+++ b/src/util/gnunet-ecc.c
@@ -31,7 +31,7 @@
31/** 31/**
32 * Number of characters a Base32-encoded public key requires. 32 * Number of characters a Base32-encoded public key requires.
33 */ 33 */
34#define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)*8/5+1 34#define KEY_STR_LEN sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) * 8 / 5 + 1
35 35
36/** 36/**
37 * Flag for listing public key. 37 * Flag for listing public key.
@@ -91,7 +91,7 @@ create_keys (const char *fn, const char *prefix)
91 91
92 if (NULL == (f = fopen (fn, "w+"))) 92 if (NULL == (f = fopen (fn, "w+")))
93 { 93 {
94 fprintf (stderr, _("Failed to open `%s': %s\n"), fn, STRERROR (errno)); 94 fprintf (stderr, _ ("Failed to open `%s': %s\n"), fn, strerror (errno));
95 return; 95 return;
96 } 96 }
97 if (NULL != prefix) 97 if (NULL != prefix)
@@ -116,7 +116,7 @@ create_keys (const char *fn, const char *prefix)
116 * rest = 5%8 = 5 (bits) 116 * rest = 5%8 = 5 (bits)
117 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000 117 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
118 */ 118 */
119 mask = ~ ((int)pow (2, 8 - rest) - 1); 119 mask = ~((int) pow (2, 8 - rest) - 1);
120 target_byte = ((unsigned char *) &target_pub)[n] & mask; 120 target_byte = ((unsigned char *) &target_pub)[n] & mask;
121 } 121 }
122 else 122 else
@@ -126,21 +126,15 @@ create_keys (const char *fn, const char *prefix)
126 } 126 }
127 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub); 127 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
128 fprintf (stderr, 128 fprintf (stderr,
129 _("Generating %u keys like %s, please wait"), 129 _ ("Generating %u keys like %s, please wait"),
130 make_keys, 130 make_keys,
131 s); 131 s);
132 GNUNET_free (s); 132 GNUNET_free (s);
133 fprintf (stderr, 133 fprintf (stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int) n, mask);
134 "\nattempt %s [%u, %X]\n",
135 vanity,
136 (unsigned int) n,
137 mask);
138 } 134 }
139 else 135 else
140 { 136 {
141 fprintf (stderr, 137 fprintf (stderr, _ ("Generating %u keys, please wait"), make_keys);
142 _("Generating %u keys, please wait"),
143 make_keys);
144 /* Just so old (debian) versions of GCC calm down with the warnings. */ 138 /* Just so old (debian) versions of GCC calm down with the warnings. */
145 n = rest = target_byte = mask = 0; 139 n = rest = target_byte = mask = 0;
146 } 140 }
@@ -150,8 +144,8 @@ create_keys (const char *fn, const char *prefix)
150 fprintf (stderr, "."); 144 fprintf (stderr, ".");
151 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ())) 145 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
152 { 146 {
153 GNUNET_break (0); 147 GNUNET_break (0);
154 break; 148 break;
155 } 149 }
156 if (NULL != prefix) 150 if (NULL != prefix)
157 { 151 {
@@ -176,38 +170,32 @@ create_keys (const char *fn, const char *prefix)
176 } 170 }
177 } 171 }
178 if (GNUNET_TESTING_HOSTKEYFILESIZE != 172 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
179 fwrite (pk, 1, 173 fwrite (pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f))
180 GNUNET_TESTING_HOSTKEYFILESIZE, f))
181 { 174 {
182 fprintf (stderr, 175 fprintf (stderr,
183 _("\nFailed to write to `%s': %s\n"), 176 _ ("\nFailed to write to `%s': %s\n"),
184 fn, 177 fn,
185 STRERROR (errno)); 178 strerror (errno));
186 GNUNET_free (pk); 179 GNUNET_free (pk);
187 break; 180 break;
188 } 181 }
189 GNUNET_free (pk); 182 GNUNET_free (pk);
190 } 183 }
191 if (UINT_MAX == make_keys) 184 if (UINT_MAX == make_keys)
192 fprintf (stderr, 185 fprintf (stderr, _ ("\nFinished!\n"));
193 _("\nFinished!\n"));
194 else 186 else
195 fprintf (stderr, 187 fprintf (stderr, _ ("\nError, %u keys not generated\n"), make_keys);
196 _("\nError, %u keys not generated\n"),
197 make_keys);
198 fclose (f); 188 fclose (f);
199} 189}
200 190
201 191
202static void 192static void
203print_hex (const char *msg, 193print_hex (const char *msg, const void *buf, size_t size)
204 const void *buf,
205 size_t size)
206{ 194{
207 printf ("%s: ", msg); 195 printf ("%s: ", msg);
208 for (size_t i = 0; i < size; i++) 196 for (size_t i = 0; i < size; i++)
209 { 197 {
210 printf ("%02hhx", ((const uint8_t *)buf)[i]); 198 printf ("%02hhx", ((const uint8_t *) buf)[i]);
211 } 199 }
212 printf ("\n"); 200 printf ("\n");
213} 201}
@@ -230,7 +218,8 @@ print_examples_ecdh ()
230 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1); 218 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
231 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2); 219 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
232 220
233 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128)); 221 GNUNET_assert (NULL !=
222 GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
234 printf ("ECDHE key 1:\n"); 223 printf ("ECDHE key 1:\n");
235 printf ("private: %s\n", buf); 224 printf ("private: %s\n", buf);
236 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1); 225 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
@@ -238,7 +227,8 @@ print_examples_ecdh ()
238 printf ("public: %s\n", buf); 227 printf ("public: %s\n", buf);
239 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1); 228 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
240 229
241 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128)); 230 GNUNET_assert (NULL !=
231 GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
242 printf ("ECDHE key 2:\n"); 232 printf ("ECDHE key 2:\n");
243 printf ("private: %s\n", buf); 233 printf ("private: %s\n", buf);
244 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2); 234 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
@@ -246,7 +236,8 @@ print_examples_ecdh ()
246 printf ("public: %s\n", buf); 236 printf ("public: %s\n", buf);
247 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2); 237 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
248 238
249 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash)); 239 GNUNET_assert (GNUNET_OK ==
240 GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
250 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128)); 241 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
251 printf ("ECDH shared secret: %s\n", buf); 242 printf ("ECDH shared secret: %s\n", buf);
252 243
@@ -282,56 +273,39 @@ print_key (const char *filename)
282 unsigned int c; 273 unsigned int c;
283 ssize_t sret; 274 ssize_t sret;
284 275
285 if (GNUNET_YES != 276 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
286 GNUNET_DISK_file_test (filename))
287 { 277 {
288 fprintf (stderr, 278 fprintf (stderr, _ ("Hostkeys file `%s' not found\n"), filename);
289 _("Hostkeys file `%s' not found\n"),
290 filename);
291 return; 279 return;
292 } 280 }
293 281
294 /* Check hostkey file size, read entire thing into memory */ 282 /* Check hostkey file size, read entire thing into memory */
295 if (GNUNET_OK != 283 if (GNUNET_OK !=
296 GNUNET_DISK_file_size (filename, 284 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
297 &fs,
298 GNUNET_YES,
299 GNUNET_YES))
300 fs = 0; 285 fs = 0;
301 if (0 == fs) 286 if (0 == fs)
302 { 287 {
303 fprintf (stderr, 288 fprintf (stderr, _ ("Hostkeys file `%s' is empty\n"), filename);
304 _("Hostkeys file `%s' is empty\n"), 289 return; /* File is empty */
305 filename);
306 return; /* File is empty */
307 } 290 }
308 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE)) 291 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
309 { 292 {
310 fprintf (stderr, 293 fprintf (stderr, _ ("Incorrect hostkey file format: %s\n"), filename);
311 _("Incorrect hostkey file format: %s\n"),
312 filename);
313 return; 294 return;
314 } 295 }
315 fd = GNUNET_DISK_file_open (filename, 296 fd = GNUNET_DISK_file_open (filename,
316 GNUNET_DISK_OPEN_READ, 297 GNUNET_DISK_OPEN_READ,
317 GNUNET_DISK_PERM_NONE); 298 GNUNET_DISK_PERM_NONE);
318 if (NULL == fd) 299 if (NULL == fd)
319 { 300 {
320 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 301 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
321 "open",
322 filename);
323 return; 302 return;
324 } 303 }
325 hostkeys_data = GNUNET_malloc (fs); 304 hostkeys_data = GNUNET_malloc (fs);
326 sret = GNUNET_DISK_file_read (fd, 305 sret = GNUNET_DISK_file_read (fd, hostkeys_data, fs);
327 hostkeys_data, 306 if ((sret < 0) || (fs != (size_t) sret))
328 fs);
329 if ( (sret < 0) ||
330 (fs != (size_t) sret) )
331 { 307 {
332 fprintf (stderr, 308 fprintf (stderr, _ ("Could not read hostkey file: %s\n"), filename);
333 _("Could not read hostkey file: %s\n"),
334 filename);
335 GNUNET_free (hostkeys_data); 309 GNUNET_free (hostkeys_data);
336 GNUNET_DISK_file_close (fd); 310 GNUNET_DISK_file_close (fd);
337 return; 311 return;
@@ -344,8 +318,8 @@ print_key (const char *filename)
344 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++) 318 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
345 { 319 {
346 GNUNET_memcpy (&private_key, 320 GNUNET_memcpy (&private_key,
347 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE), 321 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
348 GNUNET_TESTING_HOSTKEYFILESIZE); 322 GNUNET_TESTING_HOSTKEYFILESIZE);
349 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key); 323 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
350 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key); 324 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
351 if (NULL != hostkey_str) 325 if (NULL != hostkey_str)
@@ -377,7 +351,7 @@ run (void *cls,
377 (void) cls; 351 (void) cls;
378 (void) cfgfile; 352 (void) cfgfile;
379 (void) cfg; 353 (void) cfg;
380 354
381 if (print_examples_flag) 355 if (print_examples_flag)
382 { 356 {
383 print_examples (); 357 print_examples ();
@@ -385,9 +359,7 @@ run (void *cls,
385 } 359 }
386 if (NULL == args[0]) 360 if (NULL == args[0])
387 { 361 {
388 FPRINTF (stderr, 362 fprintf (stderr, "%s", _ ("No hostkey file specified on command line\n"));
389 "%s",
390 _("No hostkey file specified on command line\n"));
391 return; 363 return;
392 } 364 }
393 if (list_keys) 365 if (list_keys)
@@ -407,12 +379,12 @@ run (void *cls,
407 struct GNUNET_CRYPTO_EddsaPrivateKey pk; 379 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
408 struct GNUNET_CRYPTO_EddsaPublicKey pub; 380 struct GNUNET_CRYPTO_EddsaPublicKey pub;
409 381
410 keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ, 382 keyfile = GNUNET_DISK_file_open (args[0],
383 GNUNET_DISK_OPEN_READ,
411 GNUNET_DISK_PERM_NONE); 384 GNUNET_DISK_PERM_NONE);
412 if (NULL == keyfile) 385 if (NULL == keyfile)
413 return; 386 return;
414 while (sizeof (pk) == 387 while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
415 GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
416 { 388 {
417 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub); 389 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
418 if (print_public_key_hex) 390 if (print_public_key_hex)
@@ -422,19 +394,18 @@ run (void *cls,
422 else if (print_public_key) 394 else if (print_public_key)
423 { 395 {
424 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); 396 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
425 FPRINTF (stdout, "%s\n", str); 397 fprintf (stdout, "%s\n", str);
426 GNUNET_free (str); 398 GNUNET_free (str);
427 } 399 }
428 else if (print_private_key) 400 else if (print_private_key)
429 { 401 {
430 str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk); 402 str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk);
431 FPRINTF (stdout, "%s\n", str); 403 fprintf (stdout, "%s\n", str);
432 GNUNET_free (str); 404 GNUNET_free (str);
433 } 405 }
434 } 406 }
435 GNUNET_DISK_file_close (keyfile); 407 GNUNET_DISK_file_close (keyfile);
436 } 408 }
437
438} 409}
439 410
440 411
@@ -446,59 +417,66 @@ run (void *cls,
446 * @return 0 ok, 1 on error 417 * @return 0 ok, 1 on error
447 */ 418 */
448int 419int
449main (int argc, 420main (int argc, char *const *argv)
450 char *const *argv)
451{ 421{
452 struct GNUNET_GETOPT_CommandLineOption options[] = { 422 struct GNUNET_GETOPT_CommandLineOption options[] =
453 GNUNET_GETOPT_option_flag ('i', 423 {GNUNET_GETOPT_option_flag ('i',
454 "iterate", 424 "iterate",
455 gettext_noop ("list keys included in a file (for testing)"), 425 gettext_noop (
456 &list_keys), 426 "list keys included in a file (for testing)"),
457 GNUNET_GETOPT_option_uint ('e', 427 &list_keys),
458 "end=", 428 GNUNET_GETOPT_option_uint (
459 "COUNT", 429 'e',
460 gettext_noop ("number of keys to list included in a file (for testing)"), 430 "end=",
461 &list_keys_count), 431 "COUNT",
462 GNUNET_GETOPT_option_uint ('g', 432 gettext_noop ("number of keys to list included in a file (for testing)"),
463 "generate-keys", 433 &list_keys_count),
464 "COUNT", 434 GNUNET_GETOPT_option_uint (
465 gettext_noop ("create COUNT public-private key pairs (for testing)"), 435 'g',
466 &make_keys), 436 "generate-keys",
467 GNUNET_GETOPT_option_flag ('p', 437 "COUNT",
468 "print-public-key", 438 gettext_noop ("create COUNT public-private key pairs (for testing)"),
469 gettext_noop ("print the public key in ASCII format"), 439 &make_keys),
470 &print_public_key), 440 GNUNET_GETOPT_option_flag ('p',
471 GNUNET_GETOPT_option_flag ('P', 441 "print-public-key",
472 "print-private-key", 442 gettext_noop (
473 gettext_noop ("print the private key in ASCII format"), 443 "print the public key in ASCII format"),
474 &print_private_key), 444 &print_public_key),
475 GNUNET_GETOPT_option_flag ('x', 445 GNUNET_GETOPT_option_flag ('P',
476 "print-hex", 446 "print-private-key",
477 gettext_noop ("print the public key in HEX format"), 447 gettext_noop (
478 &print_public_key_hex), 448 "print the private key in ASCII format"),
479 GNUNET_GETOPT_option_flag ('E', 449 &print_private_key),
480 "examples", 450 GNUNET_GETOPT_option_flag ('x',
481 gettext_noop ("print examples of ECC operations (used for compatibility testing)"), 451 "print-hex",
482 &print_examples_flag), 452 gettext_noop (
483 GNUNET_GETOPT_OPTION_END 453 "print the public key in HEX format"),
484 }; 454 &print_public_key_hex),
455 GNUNET_GETOPT_option_flag (
456 'E',
457 "examples",
458 gettext_noop (
459 "print examples of ECC operations (used for compatibility testing)"),
460 &print_examples_flag),
461 GNUNET_GETOPT_OPTION_END};
485 int ret; 462 int ret;
486 463
487 list_keys_count = UINT32_MAX; 464 list_keys_count = UINT32_MAX;
488 if (GNUNET_OK != 465 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
489 GNUNET_STRINGS_get_utf8_args (argc, argv,
490 &argc, &argv))
491 return 2; 466 return 2;
492 467
493 ret = (GNUNET_OK == 468 ret = (GNUNET_OK ==
494 GNUNET_PROGRAM_run (argc, 469 GNUNET_PROGRAM_run (argc,
495 argv, 470 argv,
496 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]", 471 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
497 gettext_noop ("Manipulate GNUnet private ECC key files"), 472 gettext_noop (
498 options, 473 "Manipulate GNUnet private ECC key files"),
474 options,
499 &run, 475 &run,
500 NULL)) ? 0 : 1; 476 NULL))
501 GNUNET_free ((void*) argv); 477 ? 0
478 : 1;
479 GNUNET_free ((void *) argv);
502 return ret; 480 return ret;
503} 481}
504 482
diff --git a/src/util/gnunet-resolver.c b/src/util/gnunet-resolver.c
index e5d58b4b6..3aba5fb1e 100644
--- a/src/util/gnunet-resolver.c
+++ b/src/util/gnunet-resolver.c
@@ -49,7 +49,7 @@ print_hostname (void *cls,
49 (void) cls; 49 (void) cls;
50 if (NULL == hostname) 50 if (NULL == hostname)
51 return; 51 return;
52 FPRINTF (stdout, 52 fprintf (stdout,
53 "%s\n", 53 "%s\n",
54 hostname); 54 hostname);
55} 55}
@@ -70,7 +70,7 @@ print_sockaddr (void *cls,
70 (void) cls; 70 (void) cls;
71 if (NULL == addr) 71 if (NULL == addr)
72 return; 72 return;
73 FPRINTF (stdout, 73 fprintf (stdout,
74 "%s\n", 74 "%s\n",
75 GNUNET_a2s (addr, 75 GNUNET_a2s (addr,
76 addrlen)); 76 addrlen));
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c
index f4149a398..6f9690c99 100644
--- a/src/util/gnunet-scrypt.c
+++ b/src/util/gnunet-scrypt.c
@@ -144,7 +144,7 @@ find_proof (void *cls)
144 if (nse_work_required <= count_leading_zeroes (&result)) 144 if (nse_work_required <= count_leading_zeroes (&result))
145 { 145 {
146 proof = counter; 146 proof = counter;
147 FPRINTF (stdout, 147 fprintf (stdout,
148 "Proof of work found: %llu!\n", 148 "Proof of work found: %llu!\n",
149 (unsigned long long) proof); 149 (unsigned long long) proof);
150 GNUNET_SCHEDULER_shutdown (); 150 GNUNET_SCHEDULER_shutdown ();
@@ -236,7 +236,7 @@ run (void *cls,
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn); 236 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
237 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn))) 237 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
238 { 238 {
239 FPRINTF (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn); 239 fprintf (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn);
240 GNUNET_free (pkfn); 240 GNUNET_free (pkfn);
241 return; 241 return;
242 } 242 }
diff --git a/src/util/helper.c b/src/util/helper.c
index 7eb2ce6d9..aa452d22b 100644
--- a/src/util/helper.c
+++ b/src/util/helper.c
@@ -70,7 +70,6 @@ struct GNUNET_HELPER_SendHandle
70 * Current write position. 70 * Current write position.
71 */ 71 */
72 unsigned int wpos; 72 unsigned int wpos;
73
74}; 73};
75 74
76 75
@@ -177,17 +176,14 @@ struct GNUNET_HELPER_Handle
177 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error 176 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
178 */ 177 */
179int 178int
180GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, 179GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill)
181 int soft_kill)
182{ 180{
183 struct GNUNET_HELPER_SendHandle *sh; 181 struct GNUNET_HELPER_SendHandle *sh;
184 int ret; 182 int ret;
185 183
186 while (NULL != (sh = h->sh_head)) 184 while (NULL != (sh = h->sh_head))
187 { 185 {
188 GNUNET_CONTAINER_DLL_remove (h->sh_head, 186 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
189 h->sh_tail,
190 sh);
191 if (NULL != sh->cont) 187 if (NULL != sh->cont)
192 sh->cont (sh->cont_cls, GNUNET_NO); 188 sh->cont (sh->cont_cls, GNUNET_NO);
193 GNUNET_free (sh); 189 GNUNET_free (sh);
@@ -264,19 +260,14 @@ GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
264 } 260 }
265 while (NULL != (sh = h->sh_head)) 261 while (NULL != (sh = h->sh_head))
266 { 262 {
267 GNUNET_CONTAINER_DLL_remove (h->sh_head, 263 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
268 h->sh_tail,
269 sh);
270 if (NULL != sh->cont) 264 if (NULL != sh->cont)
271 sh->cont (sh->cont_cls, GNUNET_NO); 265 sh->cont (sh->cont_cls, GNUNET_NO);
272 GNUNET_free (sh); 266 GNUNET_free (sh);
273 } 267 }
274 /* purge MST buffer */ 268 /* purge MST buffer */
275 if (NULL != h->mst) 269 if (NULL != h->mst)
276 (void) GNUNET_MST_from_buffer (h->mst, 270 (void) GNUNET_MST_from_buffer (h->mst, NULL, 0, GNUNET_YES, GNUNET_NO);
277 NULL, 0,
278 GNUNET_YES,
279 GNUNET_NO);
280 return ret; 271 return ret;
281} 272}
282 273
@@ -289,8 +280,7 @@ GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
289 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper 280 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
290 */ 281 */
291static void 282static void
292stop_helper (struct GNUNET_HELPER_Handle *h, 283stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
293 int soft_kill)
294{ 284{
295 if (NULL != h->restart_task) 285 if (NULL != h->restart_task)
296 { 286 {
@@ -332,9 +322,9 @@ helper_read (void *cls)
332 { 322 {
333 /* On read-error, restart the helper */ 323 /* On read-error, restart the helper */
334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
335 _("Error reading from `%s': %s\n"), 325 _ ("Error reading from `%s': %s\n"),
336 h->binary_name, 326 h->binary_name,
337 STRERROR (errno)); 327 strerror (errno));
338 if (NULL != h->exp_cb) 328 if (NULL != h->exp_cb)
339 { 329 {
340 h->exp_cb (h->cb_cls); 330 h->exp_cb (h->cb_cls);
@@ -343,9 +333,11 @@ helper_read (void *cls)
343 } 333 }
344 stop_helper (h, GNUNET_NO); 334 stop_helper (h, GNUNET_NO);
345 /* Restart the helper */ 335 /* Restart the helper */
346 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 336 h->restart_task = GNUNET_SCHEDULER_add_delayed (
347 h->retry_back_off), 337 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
348 &restart_task, h); 338 h->retry_back_off),
339 &restart_task,
340 h);
349 return; 341 return;
350 } 342 }
351 if (0 == t) 343 if (0 == t)
@@ -353,8 +345,8 @@ helper_read (void *cls)
353 /* this happens if the helper is shut down via a 345 /* this happens if the helper is shut down via a
354 signal, so it is not a "hard" error */ 346 signal, so it is not a "hard" error */
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Got 0 bytes from helper `%s' (EOF)\n", 348 "Got 0 bytes from helper `%s' (EOF)\n",
357 h->binary_name); 349 h->binary_name);
358 if (NULL != h->exp_cb) 350 if (NULL != h->exp_cb)
359 { 351 {
360 h->exp_cb (h->cb_cls); 352 h->exp_cb (h->cb_cls);
@@ -363,28 +355,27 @@ helper_read (void *cls)
363 } 355 }
364 stop_helper (h, GNUNET_NO); 356 stop_helper (h, GNUNET_NO);
365 /* Restart the helper */ 357 /* Restart the helper */
366 h->restart_task 358 h->restart_task = GNUNET_SCHEDULER_add_delayed (
367 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 359 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
368 h->retry_back_off), 360 h->retry_back_off),
369 &restart_task, h); 361 &restart_task,
362 h);
370 return; 363 return;
371 } 364 }
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Got %u bytes from helper `%s'\n", 366 "Got %u bytes from helper `%s'\n",
374 (unsigned int) t, 367 (unsigned int) t,
375 h->binary_name); 368 h->binary_name);
376 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 369 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
377 h->fh_from_helper, 370 h->fh_from_helper,
378 &helper_read, h); 371 &helper_read,
372 h);
379 if (GNUNET_SYSERR == 373 if (GNUNET_SYSERR ==
380 GNUNET_MST_from_buffer (h->mst, 374 GNUNET_MST_from_buffer (h->mst, buf, t, GNUNET_NO, GNUNET_NO))
381 buf, t,
382 GNUNET_NO,
383 GNUNET_NO))
384 { 375 {
385 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
386 _("Failed to parse inbound message from helper `%s'\n"), 377 _ ("Failed to parse inbound message from helper `%s'\n"),
387 h->binary_name); 378 h->binary_name);
388 if (NULL != h->exp_cb) 379 if (NULL != h->exp_cb)
389 { 380 {
390 h->exp_cb (h->cb_cls); 381 h->exp_cb (h->cb_cls);
@@ -393,9 +384,11 @@ helper_read (void *cls)
393 } 384 }
394 stop_helper (h, GNUNET_NO); 385 stop_helper (h, GNUNET_NO);
395 /* Restart the helper */ 386 /* Restart the helper */
396 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 387 h->restart_task = GNUNET_SCHEDULER_add_delayed (
397 h->retry_back_off), 388 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
398 &restart_task, h); 389 h->retry_back_off),
390 &restart_task,
391 h);
399 return; 392 return;
400 } 393 }
401} 394}
@@ -409,46 +402,53 @@ helper_read (void *cls)
409static void 402static void
410start_helper (struct GNUNET_HELPER_Handle *h) 403start_helper (struct GNUNET_HELPER_Handle *h)
411{ 404{
412 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 405 h->helper_in =
413 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 406 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
414 if ( (h->helper_in == NULL) || (h->helper_out == NULL)) 407 h->helper_out =
408 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
409 if ((h->helper_in == NULL) || (h->helper_out == NULL))
415 { 410 {
416 /* out of file descriptors? try again later... */ 411 /* out of file descriptors? try again later... */
417 stop_helper (h, GNUNET_NO); 412 stop_helper (h, GNUNET_NO);
418 h->restart_task = 413 h->restart_task = GNUNET_SCHEDULER_add_delayed (
419 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 414 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
420 h->retry_back_off), 415 h->retry_back_off),
421 &restart_task, h); 416 &restart_task,
417 h);
422 return; 418 return;
423 } 419 }
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Starting HELPER process `%s'\n", 421 "Starting HELPER process `%s'\n",
426 h->binary_name); 422 h->binary_name);
427 h->fh_from_helper = 423 h->fh_from_helper =
428 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ); 424 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
429 h->fh_to_helper = 425 h->fh_to_helper =
430 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE); 426 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
431 h->helper_proc = 427 h->helper_proc = GNUNET_OS_start_process_vap (h->with_control_pipe,
432 GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR, 428 GNUNET_OS_INHERIT_STD_ERR,
433 h->helper_in, h->helper_out, NULL, 429 h->helper_in,
434 h->binary_name, 430 h->helper_out,
435 h->binary_argv); 431 NULL,
432 h->binary_name,
433 h->binary_argv);
436 if (NULL == h->helper_proc) 434 if (NULL == h->helper_proc)
437 { 435 {
438 /* failed to start process? try again later... */ 436 /* failed to start process? try again later... */
439 stop_helper (h, GNUNET_NO); 437 stop_helper (h, GNUNET_NO);
440 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 438 h->restart_task = GNUNET_SCHEDULER_add_delayed (
441 h->retry_back_off), 439 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
442 &restart_task, h); 440 h->retry_back_off),
441 &restart_task,
442 h);
443 return; 443 return;
444 } 444 }
445 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE); 445 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
446 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ); 446 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
447 if (NULL != h->mst) 447 if (NULL != h->mst)
448 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 448 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
449 h->fh_from_helper, 449 h->fh_from_helper,
450 &helper_read, 450 &helper_read,
451 h); 451 h);
452} 452}
453 453
454 454
@@ -460,7 +460,7 @@ start_helper (struct GNUNET_HELPER_Handle *h)
460static void 460static void
461restart_task (void *cls) 461restart_task (void *cls)
462{ 462{
463 struct GNUNET_HELPER_Handle*h = cls; 463 struct GNUNET_HELPER_Handle *h = cls;
464 464
465 h->restart_task = NULL; 465 h->restart_task = NULL;
466 h->retry_back_off++; 466 h->retry_back_off++;
@@ -489,11 +489,11 @@ restart_task (void *cls)
489 */ 489 */
490struct GNUNET_HELPER_Handle * 490struct GNUNET_HELPER_Handle *
491GNUNET_HELPER_start (int with_control_pipe, 491GNUNET_HELPER_start (int with_control_pipe,
492 const char *binary_name, 492 const char *binary_name,
493 char *const binary_argv[], 493 char *const binary_argv[],
494 GNUNET_MessageTokenizerCallback cb, 494 GNUNET_MessageTokenizerCallback cb,
495 GNUNET_HELPER_ExceptionCallback exp_cb, 495 GNUNET_HELPER_ExceptionCallback exp_cb,
496 void *cb_cls) 496 void *cb_cls)
497{ 497{
498 struct GNUNET_HELPER_Handle *h; 498 struct GNUNET_HELPER_Handle *h;
499 unsigned int c; 499 unsigned int c;
@@ -505,15 +505,15 @@ GNUNET_HELPER_start (int with_control_pipe,
505 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name); 505 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
506 else 506 else
507 h->binary_name = GNUNET_strdup (binary_name); 507 h->binary_name = GNUNET_strdup (binary_name);
508 for (c = 0; NULL != binary_argv[c]; c++); 508 for (c = 0; NULL != binary_argv[c]; c++)
509 ;
509 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1)); 510 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
510 for (c = 0; NULL != binary_argv[c]; c++) 511 for (c = 0; NULL != binary_argv[c]; c++)
511 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]); 512 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
512 h->binary_argv[c] = NULL; 513 h->binary_argv[c] = NULL;
513 h->cb_cls = cb_cls; 514 h->cb_cls = cb_cls;
514 if (NULL != cb) 515 if (NULL != cb)
515 h->mst = GNUNET_MST_create (cb, 516 h->mst = GNUNET_MST_create (cb, h->cb_cls);
516 h->cb_cls);
517 h->exp_cb = exp_cb; 517 h->exp_cb = exp_cb;
518 h->retry_back_off = 0; 518 h->retry_back_off = 0;
519 start_helper (h); 519 start_helper (h);
@@ -541,9 +541,7 @@ GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
541 GNUNET_assert (NULL == h->restart_task); 541 GNUNET_assert (NULL == h->restart_task);
542 while (NULL != (sh = h->sh_head)) 542 while (NULL != (sh = h->sh_head))
543 { 543 {
544 GNUNET_CONTAINER_DLL_remove (h->sh_head, 544 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
545 h->sh_tail,
546 sh);
547 if (NULL != sh->cont) 545 if (NULL != sh->cont)
548 sh->cont (sh->cont_cls, GNUNET_SYSERR); 546 sh->cont (sh->cont_cls, GNUNET_SYSERR);
549 GNUNET_free (sh); 547 GNUNET_free (sh);
@@ -566,8 +564,7 @@ GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
566 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper 564 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
567 */ 565 */
568void 566void
569GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, 567GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill)
570 int soft_kill)
571{ 568{
572 h->exp_cb = NULL; 569 h->exp_cb = NULL;
573 stop_helper (h, soft_kill); 570 stop_helper (h, soft_kill);
@@ -591,21 +588,20 @@ helper_write (void *cls)
591 h->write_task = NULL; 588 h->write_task = NULL;
592 if (NULL == (sh = h->sh_head)) 589 if (NULL == (sh = h->sh_head))
593 { 590 {
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
595 "Helper write had no work!\n");
596 return; /* how did this happen? */ 592 return; /* how did this happen? */
597 } 593 }
598 buf = (const char*) sh->msg; 594 buf = (const char *) sh->msg;
599 t = GNUNET_DISK_file_write (h->fh_to_helper, 595 t = GNUNET_DISK_file_write (h->fh_to_helper,
600 &buf[sh->wpos], 596 &buf[sh->wpos],
601 ntohs (sh->msg->size) - sh->wpos); 597 ntohs (sh->msg->size) - sh->wpos);
602 if (-1 == t) 598 if (-1 == t)
603 { 599 {
604 /* On write-error, restart the helper */ 600 /* On write-error, restart the helper */
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 601 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 _("Error writing to `%s': %s\n"), 602 _ ("Error writing to `%s': %s\n"),
607 h->binary_name, 603 h->binary_name,
608 STRERROR (errno)); 604 strerror (errno));
609 if (NULL != h->exp_cb) 605 if (NULL != h->exp_cb)
610 { 606 {
611 h->exp_cb (h->cb_cls); 607 h->exp_cb (h->cb_cls);
@@ -613,33 +609,34 @@ helper_write (void *cls)
613 return; 609 return;
614 } 610 }
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Stopping and restarting helper task!\n"); 612 "Stopping and restarting helper task!\n");
617 stop_helper (h, GNUNET_NO); 613 stop_helper (h, GNUNET_NO);
618 /* Restart the helper */ 614 /* Restart the helper */
619 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 615 h->restart_task = GNUNET_SCHEDULER_add_delayed (
620 h->retry_back_off), 616 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
621 &restart_task, h); 617 h->retry_back_off),
618 &restart_task,
619 h);
622 return; 620 return;
623 } 621 }
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Transmitted %u bytes to %s\n", 623 "Transmitted %u bytes to %s\n",
626 (unsigned int) t, 624 (unsigned int) t,
627 h->binary_name); 625 h->binary_name);
628 sh->wpos += t; 626 sh->wpos += t;
629 if (sh->wpos == ntohs (sh->msg->size)) 627 if (sh->wpos == ntohs (sh->msg->size))
630 { 628 {
631 GNUNET_CONTAINER_DLL_remove (h->sh_head, 629 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
632 h->sh_tail,
633 sh);
634 if (NULL != sh->cont) 630 if (NULL != sh->cont)
635 sh->cont (sh->cont_cls, GNUNET_YES); 631 sh->cont (sh->cont_cls, GNUNET_YES);
636 GNUNET_free (sh); 632 GNUNET_free (sh);
637 } 633 }
638 if (NULL != h->sh_head) 634 if (NULL != h->sh_head)
639 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, 635 h->write_task =
640 h->fh_to_helper, 636 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
641 &helper_write, 637 h->fh_to_helper,
642 h); 638 &helper_write,
639 h);
643} 640}
644 641
645 642
@@ -658,34 +655,32 @@ helper_write (void *cls)
658 */ 655 */
659struct GNUNET_HELPER_SendHandle * 656struct GNUNET_HELPER_SendHandle *
660GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h, 657GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
661 const struct GNUNET_MessageHeader *msg, 658 const struct GNUNET_MessageHeader *msg,
662 int can_drop, 659 int can_drop,
663 GNUNET_HELPER_Continuation cont, 660 GNUNET_HELPER_Continuation cont,
664 void *cont_cls) 661 void *cont_cls)
665{ 662{
666 struct GNUNET_HELPER_SendHandle *sh; 663 struct GNUNET_HELPER_SendHandle *sh;
667 uint16_t mlen; 664 uint16_t mlen;
668 665
669 if (NULL == h->fh_to_helper) 666 if (NULL == h->fh_to_helper)
670 return NULL; 667 return NULL;
671 if ( (GNUNET_YES == can_drop) && 668 if ((GNUNET_YES == can_drop) && (NULL != h->sh_head))
672 (NULL != h->sh_head) )
673 return NULL; 669 return NULL;
674 mlen = ntohs (msg->size); 670 mlen = ntohs (msg->size);
675 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen); 671 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
676 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1]; 672 sh->msg = (const struct GNUNET_MessageHeader *) &sh[1];
677 GNUNET_memcpy (&sh[1], msg, mlen); 673 GNUNET_memcpy (&sh[1], msg, mlen);
678 sh->h = h; 674 sh->h = h;
679 sh->cont = cont; 675 sh->cont = cont;
680 sh->cont_cls = cont_cls; 676 sh->cont_cls = cont_cls;
681 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head, 677 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head, h->sh_tail, sh);
682 h->sh_tail,
683 sh);
684 if (NULL == h->write_task) 678 if (NULL == h->write_task)
685 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, 679 h->write_task =
686 h->fh_to_helper, 680 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
687 &helper_write, 681 h->fh_to_helper,
688 h); 682 &helper_write,
683 h);
689 684
690 return sh; 685 return sh;
691} 686}
diff --git a/src/util/os_installation.c b/src/util/os_installation.c
index 8dacd431d..3e5a99811 100644
--- a/src/util/os_installation.c
+++ b/src/util/os_installation.c
@@ -107,7 +107,7 @@ GNUNET_OS_project_data_get ()
107 { 107 {
108 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); 108 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
109 if (NULL != path) 109 if (NULL != path)
110 BINDTEXTDOMAIN (PACKAGE, path); 110 bindtextdomain (PACKAGE, path);
111 GNUNET_free (path); 111 GNUNET_free (path);
112 gettextinit = 1; 112 gettextinit = 1;
113 } 113 }
@@ -127,7 +127,7 @@ GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd)
127 { 127 {
128 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); 128 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
129 if (NULL != path) 129 if (NULL != path)
130 BINDTEXTDOMAIN (PACKAGE, path); 130 bindtextdomain (PACKAGE, path);
131 GNUNET_free (path); 131 GNUNET_free (path);
132 gettextinit = 1; 132 gettextinit = 1;
133 } 133 }
@@ -152,7 +152,7 @@ get_path_from_proc_maps ()
152 char *lgu; 152 char *lgu;
153 153
154 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ()); 154 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
155 if (NULL == (f = FOPEN (fn, "r"))) 155 if (NULL == (f = fopen (fn, "r")))
156 return NULL; 156 return NULL;
157 while (NULL != fgets (line, sizeof (line), f)) 157 while (NULL != fgets (line, sizeof (line), f))
158 { 158 {
@@ -162,11 +162,11 @@ get_path_from_proc_maps ()
162 (NULL != (lgu = strstr (dir, current_pd->libname)))) 162 (NULL != (lgu = strstr (dir, current_pd->libname))))
163 { 163 {
164 lgu[0] = '\0'; 164 lgu[0] = '\0';
165 FCLOSE (f); 165 fclose (f);
166 return GNUNET_strdup (dir); 166 return GNUNET_strdup (dir);
167 } 167 }
168 } 168 }
169 FCLOSE (f); 169 fclose (f);
170 return NULL; 170 return NULL;
171} 171}
172 172
@@ -666,7 +666,7 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
666 { 666 {
667 GNUNET_asprintf (&dirname, 667 GNUNET_asprintf (&dirname,
668 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR 668 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
669 "%s" DIR_SEPARATOR_STR, 669 "%s" DIR_SEPARATOR_STR,
670 current_pd->project_dirname); 670 current_pd->project_dirname);
671 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname); 671 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
672 } 672 }
@@ -674,7 +674,7 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
674 { 674 {
675 GNUNET_asprintf (&dirname, 675 GNUNET_asprintf (&dirname,
676 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR 676 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
677 "%s" DIR_SEPARATOR_STR, 677 "%s" DIR_SEPARATOR_STR,
678 current_pd->project_dirname); 678 current_pd->project_dirname);
679 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname); 679 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
680 } 680 }
@@ -696,22 +696,22 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
696 case GNUNET_OS_IPK_DATADIR: 696 case GNUNET_OS_IPK_DATADIR:
697 GNUNET_asprintf (&dirname, 697 GNUNET_asprintf (&dirname,
698 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR 698 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
699 "%s" DIR_SEPARATOR_STR, 699 "%s" DIR_SEPARATOR_STR,
700 current_pd->project_dirname); 700 current_pd->project_dirname);
701 break; 701 break;
702 case GNUNET_OS_IPK_LOCALEDIR: 702 case GNUNET_OS_IPK_LOCALEDIR:
703 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR 703 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
704 "locale" DIR_SEPARATOR_STR); 704 "locale" DIR_SEPARATOR_STR);
705 break; 705 break;
706 case GNUNET_OS_IPK_ICONDIR: 706 case GNUNET_OS_IPK_ICONDIR:
707 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR 707 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
708 "icons" DIR_SEPARATOR_STR); 708 "icons" DIR_SEPARATOR_STR);
709 break; 709 break;
710 case GNUNET_OS_IPK_DOCDIR: 710 case GNUNET_OS_IPK_DOCDIR:
711 GNUNET_asprintf (&dirname, 711 GNUNET_asprintf (&dirname,
712 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR 712 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
713 "doc" DIR_SEPARATOR_STR 713 "doc" DIR_SEPARATOR_STR
714 "%s" DIR_SEPARATOR_STR, 714 "%s" DIR_SEPARATOR_STR,
715 current_pd->project_dirname); 715 current_pd->project_dirname);
716 break; 716 break;
717 case GNUNET_OS_IPK_LIBEXECDIR: 717 case GNUNET_OS_IPK_LIBEXECDIR:
@@ -719,7 +719,7 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
719 { 719 {
720 GNUNET_asprintf (&dirname, 720 GNUNET_asprintf (&dirname,
721 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR 721 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
722 "libexec" DIR_SEPARATOR_STR, 722 "libexec" DIR_SEPARATOR_STR,
723 current_pd->project_dirname); 723 current_pd->project_dirname);
724 GNUNET_asprintf (&tmp, 724 GNUNET_asprintf (&tmp,
725 "%s%s%s%s", 725 "%s%s%s%s",
@@ -740,8 +740,8 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
740 { 740 {
741 GNUNET_asprintf (&dirname, 741 GNUNET_asprintf (&dirname,
742 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR 742 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
743 "%s" DIR_SEPARATOR_STR 743 "%s" DIR_SEPARATOR_STR
744 "libexec" DIR_SEPARATOR_STR, 744 "libexec" DIR_SEPARATOR_STR,
745 current_pd->project_dirname); 745 current_pd->project_dirname);
746 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname); 746 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
747 } 747 }
@@ -749,8 +749,8 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
749 { 749 {
750 GNUNET_asprintf (&dirname, 750 GNUNET_asprintf (&dirname,
751 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR 751 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
752 "%s" DIR_SEPARATOR_STR 752 "%s" DIR_SEPARATOR_STR
753 "libexec" DIR_SEPARATOR_STR, 753 "libexec" DIR_SEPARATOR_STR,
754 current_pd->project_dirname); 754 current_pd->project_dirname);
755 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname); 755 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
756 } 756 }
@@ -766,7 +766,7 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
766 } 766 }
767 GNUNET_asprintf (&dirname, 767 GNUNET_asprintf (&dirname,
768 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR 768 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
769 "libexec" DIR_SEPARATOR_STR, 769 "libexec" DIR_SEPARATOR_STR,
770 current_pd->project_dirname); 770 current_pd->project_dirname);
771 break; 771 break;
772 default: 772 default:
@@ -844,14 +844,14 @@ GNUNET_OS_get_suid_binary_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
844 "PATHS", 844 "PATHS",
845 "SUID_BINARY_PATH", 845 "SUID_BINARY_PATH",
846 &path); 846 &path);
847 if ((NULL == path)||(0 == strlen (path))) 847 if ((NULL == path) || (0 == strlen (path)))
848 return GNUNET_OS_get_libexec_binary_path (progname); 848 return GNUNET_OS_get_libexec_binary_path (progname);
849 path_len = strlen (path); 849 path_len = strlen (path);
850 GNUNET_asprintf (&binary, 850 GNUNET_asprintf (&binary,
851 "%s%s%s", 851 "%s%s%s",
852 path, 852 path,
853 (path[path_len - 1] == DIR_SEPARATOR) ? "" 853 (path[path_len - 1] == DIR_SEPARATOR) ? ""
854 : DIR_SEPARATOR_STR, 854 : DIR_SEPARATOR_STR,
855 progname); 855 progname);
856 cache = path; 856 cache = path;
857 return binary; 857 return binary;
@@ -926,7 +926,7 @@ GNUNET_OS_check_helper_binary (const char *binary,
926 binary); 926 binary);
927 return GNUNET_SYSERR; 927 return GNUNET_SYSERR;
928 } 928 }
929 if (0 != ACCESS (p, X_OK)) 929 if (0 != access (p, X_OK))
930 { 930 {
931 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p); 931 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p);
932 GNUNET_free (p); 932 GNUNET_free (p);
@@ -940,7 +940,7 @@ GNUNET_OS_check_helper_binary (const char *binary,
940 return GNUNET_YES; 940 return GNUNET_YES;
941 } 941 }
942#endif 942#endif
943 if (0 != STAT (p, &statbuf)) 943 if (0 != stat (p, &statbuf))
944 { 944 {
945 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p); 945 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p);
946 GNUNET_free (p); 946 GNUNET_free (p);
@@ -973,16 +973,16 @@ GNUNET_OS_check_helper_binary (const char *binary,
973 973
974 // Start the child process. 974 // Start the child process.
975 if (! (CreateProcess ( 975 if (! (CreateProcess (
976 p, // current windows (2k3 and up can handle / instead of \ in paths)) 976 p, // current windows (2k3 and up can handle / instead of \ in paths))
977 parameters, // execute dryrun/priviliege checking mode 977 parameters, // execute dryrun/priviliege checking mode
978 NULL, // Process handle not inheritable 978 NULL, // Process handle not inheritable
979 NULL, // Thread handle not inheritable 979 NULL, // Thread handle not inheritable
980 FALSE, // Set handle inheritance to FALSE 980 FALSE, // Set handle inheritance to FALSE
981 CREATE_DEFAULT_ERROR_MODE, // No creation flags 981 CREATE_DEFAULT_ERROR_MODE, // No creation flags
982 NULL, // Use parent's environment block 982 NULL, // Use parent's environment block
983 NULL, // Use parent's starting directory 983 NULL, // Use parent's starting directory
984 &start, // Pointer to STARTUPINFO structure 984 &start, // Pointer to STARTUPINFO structure
985 &proc) // Pointer to PROCESS_INFORMATION structure 985 &proc) // Pointer to PROCESS_INFORMATION structure
986 )) 986 ))
987 { 987 {
988 LOG (GNUNET_ERROR_TYPE_ERROR, 988 LOG (GNUNET_ERROR_TYPE_ERROR,
diff --git a/src/util/os_priority.c b/src/util/os_priority.c
index 03d4bde9e..17baa0df8 100644
--- a/src/util/os_priority.c
+++ b/src/util/os_priority.c
@@ -29,11 +29,13 @@
29#include "disk.h" 29#include "disk.h"
30#include <unistr.h> 30#include <unistr.h>
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)
33 33
34#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-os-priority", syscall) 34#define LOG_STRERROR(kind, syscall) \
35 GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
35 36
36#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename) 37#define LOG_STRERROR_FILE(kind, syscall, filename) \
38 GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
37 39
38#define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE" 40#define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
39 41
@@ -107,16 +109,12 @@ parent_control_handler (void *cls)
107 ssize_t ret; 109 ssize_t ret;
108 110
109 pch = NULL; 111 pch = NULL;
110 ret = GNUNET_DISK_file_read (control_pipe, 112 ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig));
111 &sig,
112 sizeof (sig));
113 if (sizeof (sig) != ret) 113 if (sizeof (sig) != ret)
114 { 114 {
115 if (-1 == ret) 115 if (-1 == ret)
116 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 116 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
117 "GNUNET_DISK_file_read"); 117 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
118 LOG (GNUNET_ERROR_TYPE_DEBUG,
119 "Closing control pipe\n");
120 GNUNET_DISK_file_close (control_pipe); 118 GNUNET_DISK_file_close (control_pipe);
121 control_pipe = NULL; 119 control_pipe = NULL;
122 GNUNET_SCHEDULER_cancel (spch); 120 GNUNET_SCHEDULER_cancel (spch);
@@ -124,16 +122,15 @@ parent_control_handler (void *cls)
124 return; 122 return;
125 } 123 }
126 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE); 124 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
127 GNUNET_assert ( (NULL == pipe_fd) || 125 GNUNET_assert ((NULL == pipe_fd) || (strlen (pipe_fd) <= 0));
128 (strlen (pipe_fd) <= 0) );
129 LOG (GNUNET_ERROR_TYPE_DEBUG, 126 LOG (GNUNET_ERROR_TYPE_DEBUG,
130 "Got control code %d from parent via pipe %s\n", 127 "Got control code %d from parent via pipe %s\n",
131 sig, 128 sig,
132 pipe_fd); 129 pipe_fd);
133 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 130 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
134 control_pipe, 131 control_pipe,
135 &parent_control_handler, 132 &parent_control_handler,
136 control_pipe); 133 control_pipe);
137 GNUNET_SIGNAL_raise ((int) sig); 134 GNUNET_SIGNAL_raise ((int) sig);
138} 135}
139 136
@@ -162,10 +159,10 @@ GNUNET_OS_install_parent_control_handler (void *cls)
162 return; 159 return;
163 } 160 }
164 env_buf = getenv (GNUNET_OS_CONTROL_PIPE); 161 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
165 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) ) 162 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
166 { 163 {
167 LOG (GNUNET_ERROR_TYPE_DEBUG, 164 LOG (GNUNET_ERROR_TYPE_DEBUG,
168 "Not installing a handler because $%s is empty\n", 165 "Not installing a handler because $%s is empty\n",
169 GNUNET_OS_CONTROL_PIPE); 166 GNUNET_OS_CONTROL_PIPE);
170 putenv (GNUNET_OS_CONTROL_PIPE "="); 167 putenv (GNUNET_OS_CONTROL_PIPE "=");
171 return; 168 return;
@@ -174,35 +171,32 @@ GNUNET_OS_install_parent_control_handler (void *cls)
174 pipe_fd = strtoull (env_buf, &env_buf_end, 16); 171 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
175 if ((0 != errno) || (env_buf == env_buf_end)) 172 if ((0 != errno) || (env_buf == env_buf_end))
176 { 173 {
177 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 174 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf);
178 "strtoull",
179 env_buf);
180 putenv (GNUNET_OS_CONTROL_PIPE "="); 175 putenv (GNUNET_OS_CONTROL_PIPE "=");
181 return; 176 return;
182 } 177 }
183#if !defined (WINDOWS) 178#if ! defined(WINDOWS)
184 if (pipe_fd >= FD_SETSIZE) 179 if (pipe_fd >= FD_SETSIZE)
185#else 180#else
186 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd)) 181 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd)) &&
187 && (0 != GetLastError ())) 182 (0 != GetLastError ()))
188#endif 183#endif
189 { 184 {
190 LOG (GNUNET_ERROR_TYPE_ERROR, 185 LOG (GNUNET_ERROR_TYPE_ERROR,
191 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n", 186 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
192 env_buf); 187 env_buf);
193 putenv (GNUNET_OS_CONTROL_PIPE "="); 188 putenv (GNUNET_OS_CONTROL_PIPE "=");
194 return; 189 return;
195 } 190 }
196#if WINDOWS 191#if WINDOWS
197 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd); 192 control_pipe =
193 GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
198#else 194#else
199 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd); 195 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
200#endif 196#endif
201 if (NULL == control_pipe) 197 if (NULL == control_pipe)
202 { 198 {
203 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 199 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
204 "open",
205 env_buf);
206 putenv (GNUNET_OS_CONTROL_PIPE "="); 200 putenv (GNUNET_OS_CONTROL_PIPE "=");
207 return; 201 return;
208 } 202 }
@@ -210,11 +204,10 @@ GNUNET_OS_install_parent_control_handler (void *cls)
210 "Adding parent control handler pipe `%s' to the scheduler\n", 204 "Adding parent control handler pipe `%s' to the scheduler\n",
211 env_buf); 205 env_buf);
212 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 206 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
213 control_pipe, 207 control_pipe,
214 &parent_control_handler, 208 &parent_control_handler,
215 control_pipe);
216 spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch,
217 control_pipe); 209 control_pipe);
210 spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch, control_pipe);
218 putenv (GNUNET_OS_CONTROL_PIPE "="); 211 putenv (GNUNET_OS_CONTROL_PIPE "=");
219} 212}
220 213
@@ -248,8 +241,7 @@ GNUNET_OS_process_current ()
248 * @return 0 on success, -1 on error 241 * @return 0 on success, -1 on error
249 */ 242 */
250int 243int
251GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, 244GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
252 int sig)
253{ 245{
254 int ret; 246 int ret;
255 char csig; 247 char csig;
@@ -258,19 +250,17 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
258 if (NULL != proc->control_pipe) 250 if (NULL != proc->control_pipe)
259 { 251 {
260 LOG (GNUNET_ERROR_TYPE_DEBUG, 252 LOG (GNUNET_ERROR_TYPE_DEBUG,
261 "Sending signal %d to pid: %u via pipe\n", 253 "Sending signal %d to pid: %u via pipe\n",
262 sig, 254 sig,
263 proc->pid); 255 proc->pid);
264 ret = GNUNET_DISK_file_write (proc->control_pipe, 256 ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof (csig));
265 &csig,
266 sizeof (csig));
267 if (sizeof (csig) == ret) 257 if (sizeof (csig) == ret)
268 return 0; 258 return 0;
269 } 259 }
270 /* pipe failed or non-existent, try other methods */ 260 /* pipe failed or non-existent, try other methods */
271 switch (sig) 261 switch (sig)
272 { 262 {
273#if !defined (WINDOWS) 263#if ! defined(WINDOWS)
274 case SIGHUP: 264 case SIGHUP:
275#endif 265#endif
276 case SIGINT: 266 case SIGINT:
@@ -279,62 +269,62 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
279#if (SIGTERM != GNUNET_TERM_SIG) 269#if (SIGTERM != GNUNET_TERM_SIG)
280 case GNUNET_TERM_SIG: 270 case GNUNET_TERM_SIG:
281#endif 271#endif
282#if defined(WINDOWS) && !defined(__CYGWIN__) 272#if defined(WINDOWS) && ! defined(__CYGWIN__)
273 {
274 DWORD exitcode;
275 int must_kill = GNUNET_YES;
276 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
277 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
278 if (GNUNET_YES == must_kill)
283 { 279 {
284 DWORD exitcode; 280 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
285 int must_kill = GNUNET_YES;
286 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
287 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
288 if (GNUNET_YES == must_kill)
289 { 281 {
290 if (0 == SafeTerminateProcess (proc->handle, 0, 0)) 282 DWORD error_code = GetLastError ();
283 if ((error_code != WAIT_TIMEOUT) &&
284 (error_code != ERROR_PROCESS_ABORTED))
291 { 285 {
292 DWORD error_code = GetLastError (); 286 LOG ((error_code == ERROR_ACCESS_DENIED) ? GNUNET_ERROR_TYPE_INFO
293 if ( (error_code != WAIT_TIMEOUT) && 287 : GNUNET_ERROR_TYPE_WARNING,
294 (error_code != ERROR_PROCESS_ABORTED) ) 288 "SafeTermiateProcess failed with code %lu\n",
295 { 289 error_code);
296 LOG ((error_code == ERROR_ACCESS_DENIED) ? 290 /* The problem here is that a process that is already dying
297 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
298 "SafeTermiateProcess failed with code %lu\n",
299 error_code);
300 /* The problem here is that a process that is already dying
301 * might cause SafeTerminateProcess to fail with 291 * might cause SafeTerminateProcess to fail with
302 * ERROR_ACCESS_DENIED, but the process WILL die eventually. 292 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
303 * If we really had a permissions problem, hanging up (which 293 * If we really had a permissions problem, hanging up (which
304 * is what will happen in process_wait() in that case) is 294 * is what will happen in process_wait() in that case) is
305 * a valid option. 295 * a valid option.
306 */ 296 */
307 if (ERROR_ACCESS_DENIED == error_code) 297 if (ERROR_ACCESS_DENIED == error_code)
308 { 298 {
309 errno = 0; 299 errno = 0;
310 } 300 }
311 else 301 else
312 { 302 {
313 SetErrnoFromWinError (error_code); 303 SetErrnoFromWinError (error_code);
314 return -1; 304 return -1;
315 }
316 } 305 }
317 } 306 }
318 } 307 }
319 } 308 }
309 }
320 return 0; 310 return 0;
321#else 311#else
322 LOG (GNUNET_ERROR_TYPE_DEBUG, 312 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Sending signal %d to pid: %u via system call\n", 313 "Sending signal %d to pid: %u via system call\n",
324 sig, 314 sig,
325 proc->pid); 315 proc->pid);
326 return PLIBC_KILL (proc->pid, sig); 316 return kill (proc->pid, sig);
327#endif 317#endif
328 default: 318 default:
329#if defined (WINDOWS) 319#if defined(WINDOWS)
330 errno = EINVAL; 320 errno = EINVAL;
331 return -1; 321 return -1;
332#else 322#else
333 LOG (GNUNET_ERROR_TYPE_DEBUG, 323 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Sending signal %d to pid: %u via system call\n", 324 "Sending signal %d to pid: %u via system call\n",
335 sig, 325 sig,
336 proc->pid); 326 proc->pid);
337 return PLIBC_KILL (proc->pid, sig); 327 return kill (proc->pid, sig);
338#endif 328#endif
339 } 329 }
340} 330}
@@ -348,7 +338,7 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
348 * @return the current process id 338 * @return the current process id
349 */ 339 */
350pid_t 340pid_t
351GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc) 341GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc)
352{ 342{
353 return proc->pid; 343 return proc->pid;
354} 344}
@@ -365,7 +355,7 @@ GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
365{ 355{
366 if (NULL != proc->control_pipe) 356 if (NULL != proc->control_pipe)
367 GNUNET_DISK_file_close (proc->control_pipe); 357 GNUNET_DISK_file_close (proc->control_pipe);
368#if defined (WINDOWS) 358#if defined(WINDOWS)
369 if (NULL != proc->handle) 359 if (NULL != proc->handle)
370 CloseHandle (proc->handle); 360 CloseHandle (proc->handle);
371#endif 361#endif
@@ -425,7 +415,8 @@ CreateCustomEnvTable (char **vars)
425 win32_env_table = GetEnvironmentStringsA (); 415 win32_env_table = GetEnvironmentStringsA ();
426 if (NULL == win32_env_table) 416 if (NULL == win32_env_table)
427 return NULL; 417 return NULL;
428 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ; 418 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++)
419 ;
429 n_var = c; 420 n_var = c;
430 index = GNUNET_malloc (sizeof (char *) * n_var); 421 index = GNUNET_malloc (sizeof (char *) * n_var);
431 for (c = 0; c < n_var; c++) 422 for (c = 0; c < n_var; c++)
@@ -448,7 +439,7 @@ CreateCustomEnvTable (char **vars)
448 break; 439 break;
449 } 440 }
450 } 441 }
451 if (!found) 442 if (! found)
452 tablesize += len + 1; 443 tablesize += len + 1;
453 ptr += len + 1; 444 ptr += len + 1;
454 } 445 }
@@ -476,7 +467,7 @@ CreateCustomEnvTable (char **vars)
476 break; 467 break;
477 } 468 }
478 } 469 }
479 if (!found) 470 if (! found)
480 { 471 {
481 strcpy (result_ptr, ptr); 472 strcpy (result_ptr, ptr);
482 result_ptr += len + 1; 473 result_ptr += len + 1;
@@ -519,17 +510,14 @@ CreateCustomEnvTable (char **vars)
519 * @param flags open flags (O_RDONLY, O_WRONLY) 510 * @param flags open flags (O_RDONLY, O_WRONLY)
520 */ 511 */
521static void 512static void
522open_dev_null (int target_fd, 513open_dev_null (int target_fd, int flags)
523 int flags)
524{ 514{
525 int fd; 515 int fd;
526 516
527 fd = open ("/dev/null", flags); 517 fd = open ("/dev/null", flags);
528 if (-1 == fd) 518 if (-1 == fd)
529 { 519 {
530 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 520 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
531 "open",
532 "/dev/null");
533 return; 521 return;
534 } 522 }
535 if (fd == target_fd) 523 if (fd == target_fd)
@@ -565,12 +553,12 @@ open_dev_null (int target_fd,
565static struct GNUNET_OS_Process * 553static struct GNUNET_OS_Process *
566start_process (int pipe_control, 554start_process (int pipe_control,
567 enum GNUNET_OS_InheritStdioFlags std_inheritance, 555 enum GNUNET_OS_InheritStdioFlags std_inheritance,
568 struct GNUNET_DISK_PipeHandle *pipe_stdin, 556 struct GNUNET_DISK_PipeHandle *pipe_stdin,
569 struct GNUNET_DISK_PipeHandle *pipe_stdout, 557 struct GNUNET_DISK_PipeHandle *pipe_stdout,
570 struct GNUNET_DISK_PipeHandle *pipe_stderr, 558 struct GNUNET_DISK_PipeHandle *pipe_stderr,
571 const SOCKTYPE *lsocks, 559 const SOCKTYPE *lsocks,
572 const char *filename, 560 const char *filename,
573 char *const argv[]) 561 char *const argv[])
574{ 562{
575#ifndef MINGW 563#ifndef MINGW
576 pid_t ret; 564 pid_t ret;
@@ -601,22 +589,19 @@ start_process (int pipe_control,
601 struct GNUNET_DISK_PipeHandle *childpipe; 589 struct GNUNET_DISK_PipeHandle *childpipe;
602 int dup_childpipe_read_fd = -1; 590 int dup_childpipe_read_fd = -1;
603 591
604 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, 592 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
605 GNUNET_YES, GNUNET_NO);
606 if (NULL == childpipe) 593 if (NULL == childpipe)
607 return NULL; 594 return NULL;
608 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, 595 childpipe_read =
609 GNUNET_DISK_PIPE_END_READ); 596 GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
610 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, 597 childpipe_write =
611 GNUNET_DISK_PIPE_END_WRITE); 598 GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
612 GNUNET_DISK_pipe_close (childpipe); 599 GNUNET_DISK_pipe_close (childpipe);
613 if ( (NULL == childpipe_read) || 600 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
614 (NULL == childpipe_write) || 601 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
615 (GNUNET_OK != 602 &childpipe_read_fd,
616 GNUNET_DISK_internal_file_handle_ (childpipe_read, 603 sizeof (int))) ||
617 &childpipe_read_fd, 604 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
618 sizeof (int))) ||
619 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
620 { 605 {
621 if (NULL != childpipe_read) 606 if (NULL != childpipe_read)
622 GNUNET_DISK_file_close (childpipe_read); 607 GNUNET_DISK_file_close (childpipe_read);
@@ -636,39 +621,48 @@ start_process (int pipe_control,
636 } 621 }
637 if (NULL != pipe_stdin) 622 if (NULL != pipe_stdin)
638 { 623 {
639 GNUNET_assert (GNUNET_OK == 624 GNUNET_assert (
640 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 625 GNUNET_OK ==
641 (pipe_stdin, GNUNET_DISK_PIPE_END_READ), 626 GNUNET_DISK_internal_file_handle_ (
642 &fd_stdin_read, sizeof (int))); 627 GNUNET_DISK_pipe_handle (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
643 GNUNET_assert (GNUNET_OK == 628 &fd_stdin_read,
644 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 629 sizeof (int)));
645 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), 630 GNUNET_assert (
646 &fd_stdin_write, sizeof (int))); 631 GNUNET_OK ==
632 GNUNET_DISK_internal_file_handle_ (
633 GNUNET_DISK_pipe_handle (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
634 &fd_stdin_write,
635 sizeof (int)));
647 } 636 }
648 if (NULL != pipe_stdout) 637 if (NULL != pipe_stdout)
649 { 638 {
650 GNUNET_assert (GNUNET_OK == 639 GNUNET_assert (
651 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 640 GNUNET_OK ==
652 (pipe_stdout, 641 GNUNET_DISK_internal_file_handle_ (
653 GNUNET_DISK_PIPE_END_WRITE), 642 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE),
654 &fd_stdout_write, sizeof (int))); 643 &fd_stdout_write,
655 GNUNET_assert (GNUNET_OK == 644 sizeof (int)));
656 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 645 GNUNET_assert (
657 (pipe_stdout, GNUNET_DISK_PIPE_END_READ), 646 GNUNET_OK ==
658 &fd_stdout_read, sizeof (int))); 647 GNUNET_DISK_internal_file_handle_ (
648 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
649 &fd_stdout_read,
650 sizeof (int)));
659 } 651 }
660 if (NULL != pipe_stderr) 652 if (NULL != pipe_stderr)
661 { 653 {
662 GNUNET_assert (GNUNET_OK == 654 GNUNET_assert (
663 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 655 GNUNET_OK ==
664 (pipe_stderr, 656 GNUNET_DISK_internal_file_handle_ (
665 GNUNET_DISK_PIPE_END_READ), 657 GNUNET_DISK_pipe_handle (pipe_stderr, GNUNET_DISK_PIPE_END_READ),
666 &fd_stderr_read, sizeof (int))); 658 &fd_stderr_read,
667 GNUNET_assert (GNUNET_OK == 659 sizeof (int)));
668 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 660 GNUNET_assert (
669 (pipe_stderr, 661 GNUNET_OK ==
670 GNUNET_DISK_PIPE_END_WRITE), 662 GNUNET_DISK_internal_file_handle_ (
671 &fd_stderr_write, sizeof (int))); 663 GNUNET_DISK_pipe_handle (pipe_stderr, GNUNET_DISK_PIPE_END_WRITE),
664 &fd_stderr_write,
665 sizeof (int)));
672 } 666 }
673 lscp = NULL; 667 lscp = NULL;
674 ls = 0; 668 ls = 0;
@@ -819,7 +813,7 @@ start_process (int pipe_control,
819 int argcount = 0; 813 int argcount = 0;
820 struct GNUNET_OS_Process *gnunet_proc; 814 struct GNUNET_OS_Process *gnunet_proc;
821 char path[MAX_PATH + 1]; 815 char path[MAX_PATH + 1];
822 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; 816 char *our_env[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
823 char *env_block = NULL; 817 char *env_block = NULL;
824 char *pathbuf; 818 char *pathbuf;
825 DWORD pathbuf_len; 819 DWORD pathbuf_len;
@@ -849,7 +843,8 @@ start_process (int pipe_control,
849 DWORD error_code; 843 DWORD error_code;
850 DWORD create_no_window; 844 DWORD create_no_window;
851 845
852 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL)) 846 if (GNUNET_SYSERR ==
847 GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
853 return NULL; /* not executable */ 848 return NULL; /* not executable */
854 849
855 /* Search in prefix dir (hopefully - the directory from which 850 /* Search in prefix dir (hopefully - the directory from which
@@ -861,9 +856,8 @@ start_process (int pipe_control,
861 856
862 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0); 857 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
863 858
864 alloc_len = 859 alloc_len = pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
865 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + 860 strlen (libdir);
866 strlen (libdir);
867 861
868 pathbuf = GNUNET_malloc (alloc_len * sizeof (char)); 862 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
869 863
@@ -877,21 +871,23 @@ start_process (int pipe_control,
877 if (alloc_len != pathbuf_len - 1) 871 if (alloc_len != pathbuf_len - 1)
878 { 872 {
879 GNUNET_free (pathbuf); 873 GNUNET_free (pathbuf);
880 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */ 874 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
881 return NULL; 875 return NULL;
882 } 876 }
883 877
884 cmdlen = strlen (filename); 878 cmdlen = strlen (filename);
885 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) ) 879 if ((cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")))
886 GNUNET_asprintf (&non_const_filename, "%s.exe", filename); 880 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
887 else 881 else
888 GNUNET_asprintf (&non_const_filename, "%s", filename); 882 GNUNET_asprintf (&non_const_filename, "%s", filename);
889 883
890 /* It could be in POSIX form, convert it to a DOS path early on */ 884 /* It could be in POSIX form, convert it to a DOS path early on */
891 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path))) 885 if (ERROR_SUCCESS !=
886 (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
892 { 887 {
893 SetErrnoFromWinError (lRet); 888 SetErrnoFromWinError (lRet);
894 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path", 889 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
890 "plibc_conv_to_win_path",
895 non_const_filename); 891 non_const_filename);
896 GNUNET_free (non_const_filename); 892 GNUNET_free (non_const_filename);
897 GNUNET_free (pathbuf); 893 GNUNET_free (pathbuf);
@@ -899,7 +895,7 @@ start_process (int pipe_control,
899 } 895 }
900 GNUNET_free (non_const_filename); 896 GNUNET_free (non_const_filename);
901 non_const_filename = GNUNET_strdup (win_path); 897 non_const_filename = GNUNET_strdup (win_path);
902 /* Check that this is the full path. If it isn't, search. */ 898 /* Check that this is the full path. If it isn't, search. */
903 /* FIXME: convert it to wchar_t and use SearchPathW? 899 /* FIXME: convert it to wchar_t and use SearchPathW?
904 * Remember: arguments to _start_process() are technically in UTF-8... 900 * Remember: arguments to _start_process() are technically in UTF-8...
905 */ 901 */
@@ -907,22 +903,27 @@ start_process (int pipe_control,
907 { 903 {
908 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename); 904 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
909 LOG (GNUNET_ERROR_TYPE_DEBUG, 905 LOG (GNUNET_ERROR_TYPE_DEBUG,
910 "Using path `%s' as-is. PATH is %s\n", path, ptr); 906 "Using path `%s' as-is. PATH is %s\n",
907 path,
908 ptr);
911 } 909 }
912 else if (!SearchPathA 910 else if (! SearchPathA (pathbuf,
913 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), 911 non_const_filename,
914 path, NULL)) 912 NULL,
913 sizeof (path) / sizeof (char),
914 path,
915 NULL))
915 { 916 {
916 SetErrnoFromWinError (GetLastError ()); 917 SetErrnoFromWinError (GetLastError ());
917 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath", 918 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
919 "SearchPath",
918 non_const_filename); 920 non_const_filename);
919 GNUNET_free (non_const_filename); 921 GNUNET_free (non_const_filename);
920 GNUNET_free (pathbuf); 922 GNUNET_free (pathbuf);
921 return NULL; 923 return NULL;
922 } 924 }
923 else 925 else
924 LOG (GNUNET_ERROR_TYPE_DEBUG, 926 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' in PATH `%s'\n", path, pathbuf);
925 "Found `%s' in PATH `%s'\n", path, pathbuf);
926 GNUNET_free (pathbuf); 927 GNUNET_free (pathbuf);
927 GNUNET_free (non_const_filename); 928 GNUNET_free (non_const_filename);
928 929
@@ -966,8 +967,11 @@ start_process (int pipe_control,
966 while (*arg) 967 while (*arg)
967 { 968 {
968 char arg_last_char = (*arg)[strlen (*arg) - 1]; 969 char arg_last_char = (*arg)[strlen (*arg) - 1];
969 idx += sprintf (idx, "\"%s%s\"%s", *arg, 970 idx += sprintf (idx,
970 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : ""); 971 "\"%s%s\"%s",
972 *arg,
973 arg_last_char == '\\' ? "\\" : "",
974 *(arg + 1) ? " " : "");
971 arg++; 975 arg++;
972 } 976 }
973 977
@@ -984,9 +988,10 @@ start_process (int pipe_control,
984 GetHandleInformation (stdih, &stdif); 988 GetHandleInformation (stdih, &stdif);
985 if (pipe_stdin != NULL) 989 if (pipe_stdin != NULL)
986 { 990 {
987 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 991 GNUNET_DISK_internal_file_handle_ (
988 (pipe_stdin, GNUNET_DISK_PIPE_END_READ), 992 GNUNET_DISK_pipe_handle (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
989 &stdin_handle, sizeof (HANDLE)); 993 &stdin_handle,
994 sizeof (HANDLE));
990 start.hStdInput = stdin_handle; 995 start.hStdInput = stdin_handle;
991 } 996 }
992 else if (stdih) 997 else if (stdih)
@@ -1006,10 +1011,10 @@ start_process (int pipe_control,
1006 GetHandleInformation (stdoh, &stdof); 1011 GetHandleInformation (stdoh, &stdof);
1007 if (NULL != pipe_stdout) 1012 if (NULL != pipe_stdout)
1008 { 1013 {
1009 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 1014 GNUNET_DISK_internal_file_handle_ (
1010 (pipe_stdout, 1015 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE),
1011 GNUNET_DISK_PIPE_END_WRITE), 1016 &stdout_handle,
1012 &stdout_handle, sizeof (HANDLE)); 1017 sizeof (HANDLE));
1013 start.hStdOutput = stdout_handle; 1018 start.hStdOutput = stdout_handle;
1014 } 1019 }
1015 else if (stdoh) 1020 else if (stdoh)
@@ -1043,12 +1048,15 @@ start_process (int pipe_control,
1043 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO); 1048 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
1044 if (NULL == childpipe) 1049 if (NULL == childpipe)
1045 return NULL; 1050 return NULL;
1046 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ); 1051 childpipe_read =
1047 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE); 1052 GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
1053 childpipe_write =
1054 GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
1048 GNUNET_DISK_pipe_close (childpipe); 1055 GNUNET_DISK_pipe_close (childpipe);
1049 if ((NULL == childpipe_read) || (NULL == childpipe_write) || 1056 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
1050 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read, 1057 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
1051 &childpipe_read_handle, sizeof (HANDLE)))) 1058 &childpipe_read_handle,
1059 sizeof (HANDLE))))
1052 { 1060 {
1053 if (childpipe_read) 1061 if (childpipe_read)
1054 GNUNET_DISK_file_close (childpipe_read); 1062 GNUNET_DISK_file_close (childpipe_read);
@@ -1070,7 +1078,8 @@ start_process (int pipe_control,
1070 1078
1071 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET) 1079 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1072 { 1080 {
1073 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 1081 lsocks_pipe =
1082 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1074 1083
1075 if (lsocks_pipe == NULL) 1084 if (lsocks_pipe == NULL)
1076 { 1085 {
@@ -1083,13 +1092,15 @@ start_process (int pipe_control,
1083 } 1092 }
1084 return NULL; 1093 return NULL;
1085 } 1094 }
1086 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe, 1095 lsocks_write_fd =
1087 GNUNET_DISK_PIPE_END_WRITE); 1096 GNUNET_DISK_pipe_handle (lsocks_pipe, GNUNET_DISK_PIPE_END_WRITE);
1088 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd, 1097 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1089 &lsocks_write, sizeof (HANDLE)); 1098 &lsocks_write,
1090 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 1099 sizeof (HANDLE));
1091 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ), 1100 GNUNET_DISK_internal_file_handle_ (
1092 &lsocks_read, sizeof (HANDLE)); 1101 GNUNET_DISK_pipe_handle (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1102 &lsocks_read,
1103 sizeof (HANDLE));
1093 } 1104 }
1094 else 1105 else
1095 { 1106 {
@@ -1103,7 +1114,7 @@ start_process (int pipe_control,
1103 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE); 1114 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1104 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle); 1115 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1105 } 1116 }
1106 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET)) 1117 if ((lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1107 { 1118 {
1108 /*This will tell the child that we're going to send lsocks over the pipe*/ 1119 /*This will tell the child that we're going to send lsocks over the pipe*/
1109 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS"); 1120 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
@@ -1115,10 +1126,14 @@ start_process (int pipe_control,
1115 GNUNET_free_non_null (our_env[--env_off]); 1126 GNUNET_free_non_null (our_env[--env_off]);
1116 1127
1117 wpath_len = 0; 1128 wpath_len = 0;
1118 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len))) 1129 if (NULL ==
1130 (wpath =
1131 u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1119 { 1132 {
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, 1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1121 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno); 1134 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
1135 path,
1136 errno);
1122 GNUNET_free (env_block); 1137 GNUNET_free (env_block);
1123 GNUNET_free (cmd); 1138 GNUNET_free (cmd);
1124 if (lsocks_pipe) 1139 if (lsocks_pipe)
@@ -1132,7 +1147,8 @@ start_process (int pipe_control,
1132 } 1147 }
1133 1148
1134 wcmd_len = 0; 1149 wcmd_len = 0;
1135 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len))) 1150 if (NULL ==
1151 (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1136 { 1152 {
1137 LOG (GNUNET_ERROR_TYPE_DEBUG, 1153 LOG (GNUNET_ERROR_TYPE_DEBUG,
1138 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", 1154 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
@@ -1153,15 +1169,29 @@ start_process (int pipe_control,
1153 1169
1154 create_no_window = 0; 1170 create_no_window = 0;
1155 { 1171 {
1156 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 1172 HANDLE console_input = CreateFile ("CONIN$",
1173 GENERIC_READ,
1174 FILE_SHARE_READ | FILE_SHARE_WRITE,
1175 NULL,
1176 OPEN_EXISTING,
1177 0,
1178 NULL);
1157 if (INVALID_HANDLE_VALUE == console_input) 1179 if (INVALID_HANDLE_VALUE == console_input)
1158 create_no_window = CREATE_NO_WINDOW; 1180 create_no_window = CREATE_NO_WINDOW;
1159 else 1181 else
1160 CloseHandle (console_input); 1182 CloseHandle (console_input);
1161 } 1183 }
1162 1184
1163 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES, 1185 bresult = CreateProcessW (wpath,
1164 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc); 1186 wcmd,
1187 NULL,
1188 NULL,
1189 GNUNET_YES,
1190 create_no_window | CREATE_SUSPENDED,
1191 env_block,
1192 NULL,
1193 &start,
1194 &proc);
1165 error_code = GetLastError (); 1195 error_code = GetLastError ();
1166 1196
1167 if ((NULL == pipe_stdin) && (stdih)) 1197 if ((NULL == pipe_stdin) && (stdih))
@@ -1174,7 +1204,7 @@ start_process (int pipe_control,
1174 if (stdeh) 1204 if (stdeh)
1175 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef); 1205 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1176 1206
1177 if (!bresult) 1207 if (! bresult)
1178 LOG (GNUNET_ERROR_TYPE_ERROR, 1208 LOG (GNUNET_ERROR_TYPE_ERROR,
1179 "CreateProcess(%s, %s) failed: %lu\n", 1209 "CreateProcess(%s, %s) failed: %lu\n",
1180 path, 1210 path,
@@ -1190,7 +1220,7 @@ start_process (int pipe_control,
1190 GNUNET_DISK_file_close (childpipe_read); 1220 GNUNET_DISK_file_close (childpipe_read);
1191 } 1221 }
1192 1222
1193 if (!bresult) 1223 if (! bresult)
1194 { 1224 {
1195 if (GNUNET_YES == pipe_control) 1225 if (GNUNET_YES == pipe_control)
1196 { 1226 {
@@ -1212,7 +1242,7 @@ start_process (int pipe_control,
1212 ResumeThread (proc.hThread); 1242 ResumeThread (proc.hThread);
1213 CloseHandle (proc.hThread); 1243 CloseHandle (proc.hThread);
1214 1244
1215 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) ) 1245 if ((NULL == lsocks) || (INVALID_SOCKET == lsocks[0]))
1216 return gnunet_proc; 1246 return gnunet_proc;
1217 1247
1218 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ); 1248 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
@@ -1227,25 +1257,29 @@ start_process (int pipe_control,
1227 unsigned int i; 1257 unsigned int i;
1228 1258
1229 /* Tell the number of sockets */ 1259 /* Tell the number of sockets */
1230 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++); 1260 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++)
1261 ;
1231 1262
1232 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count)); 1263 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1233 if (sizeof (count) != wrote) 1264 if (sizeof (count) != wrote)
1234 { 1265 {
1235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1236 "Failed to write %u count bytes to the child: %lu\n", 1267 "Failed to write %u count bytes to the child: %lu\n",
1237 sizeof (count), GetLastError ()); 1268 sizeof (count),
1269 GetLastError ());
1238 break; 1270 break;
1239 } 1271 }
1240 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++) 1272 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1241 { 1273 {
1242 WSAPROTOCOL_INFOA pi; 1274 WSAPROTOCOL_INFOA pi;
1243 /* Get a socket duplication info */ 1275 /* Get a socket duplication info */
1244 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi)) 1276 if (SOCKET_ERROR ==
1277 WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1245 { 1278 {
1246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1247 "Failed to duplicate an socket[%u]: %lu\n", i, 1280 "Failed to duplicate an socket[%u]: %lu\n",
1248 GetLastError ()); 1281 i,
1282 GetLastError ());
1249 break; 1283 break;
1250 } 1284 }
1251 /* Synchronous I/O is not nice, but we can't schedule this: 1285 /* Synchronous I/O is not nice, but we can't schedule this:
@@ -1261,8 +1295,10 @@ start_process (int pipe_control,
1261 if (sizeof (size) != wrote) 1295 if (sizeof (size) != wrote)
1262 { 1296 {
1263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1264 "Failed to write %u size[%u] bytes to the child: %lu\n", 1298 "Failed to write %u size[%u] bytes to the child: %lu\n",
1265 sizeof (size), i, GetLastError ()); 1299 sizeof (size),
1300 i,
1301 GetLastError ());
1266 break; 1302 break;
1267 } 1303 }
1268 /* Finally! Send the data */ 1304 /* Finally! Send the data */
@@ -1270,8 +1306,10 @@ start_process (int pipe_control,
1270 if (sizeof (pi) != wrote) 1306 if (sizeof (pi) != wrote)
1271 { 1307 {
1272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 "Failed to write %u socket[%u] bytes to the child: %lu\n", 1309 "Failed to write %u socket[%u] bytes to the child: %lu\n",
1274 sizeof (pi), i, GetLastError ()); 1310 sizeof (pi),
1311 i,
1312 GetLastError ());
1275 break; 1313 break;
1276 } 1314 }
1277 } 1315 }
@@ -1282,8 +1320,7 @@ start_process (int pipe_control,
1282 */ 1320 */
1283 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count)); 1321 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1284 fail = 0; 1322 fail = 0;
1285 } 1323 } while (fail);
1286 while (fail);
1287 1324
1288 GNUNET_DISK_file_sync (lsocks_write_fd); 1325 GNUNET_DISK_file_sync (lsocks_write_fd);
1289 GNUNET_DISK_pipe_close (lsocks_pipe); 1326 GNUNET_DISK_pipe_close (lsocks_pipe);
@@ -1320,20 +1357,20 @@ start_process (int pipe_control,
1320struct GNUNET_OS_Process * 1357struct GNUNET_OS_Process *
1321GNUNET_OS_start_process_vap (int pipe_control, 1358GNUNET_OS_start_process_vap (int pipe_control,
1322 enum GNUNET_OS_InheritStdioFlags std_inheritance, 1359 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1323 struct GNUNET_DISK_PipeHandle *pipe_stdin, 1360 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1324 struct GNUNET_DISK_PipeHandle *pipe_stdout, 1361 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1325 struct GNUNET_DISK_PipeHandle *pipe_stderr, 1362 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1326 const char *filename, 1363 const char *filename,
1327 char *const argv[]) 1364 char *const argv[])
1328{ 1365{
1329 return start_process (pipe_control, 1366 return start_process (pipe_control,
1330 std_inheritance, 1367 std_inheritance,
1331 pipe_stdin, 1368 pipe_stdin,
1332 pipe_stdout, 1369 pipe_stdout,
1333 pipe_stderr, 1370 pipe_stderr,
1334 NULL, 1371 NULL,
1335 filename, 1372 filename,
1336 argv); 1373 argv);
1337} 1374}
1338 1375
1339 1376
@@ -1352,10 +1389,11 @@ GNUNET_OS_start_process_vap (int pipe_control,
1352struct GNUNET_OS_Process * 1389struct GNUNET_OS_Process *
1353GNUNET_OS_start_process_va (int pipe_control, 1390GNUNET_OS_start_process_va (int pipe_control,
1354 enum GNUNET_OS_InheritStdioFlags std_inheritance, 1391 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1355 struct GNUNET_DISK_PipeHandle *pipe_stdin, 1392 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1356 struct GNUNET_DISK_PipeHandle *pipe_stdout, 1393 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1357 struct GNUNET_DISK_PipeHandle *pipe_stderr, 1394 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1358 const char *filename, va_list va) 1395 const char *filename,
1396 va_list va)
1359{ 1397{
1360 struct GNUNET_OS_Process *ret; 1398 struct GNUNET_OS_Process *ret;
1361 va_list ap; 1399 va_list ap;
@@ -1375,11 +1413,11 @@ GNUNET_OS_start_process_va (int pipe_control,
1375 va_end (ap); 1413 va_end (ap);
1376 ret = GNUNET_OS_start_process_vap (pipe_control, 1414 ret = GNUNET_OS_start_process_vap (pipe_control,
1377 std_inheritance, 1415 std_inheritance,
1378 pipe_stdin, 1416 pipe_stdin,
1379 pipe_stdout, 1417 pipe_stdout,
1380 pipe_stderr, 1418 pipe_stderr,
1381 filename, 1419 filename,
1382 argv); 1420 argv);
1383 GNUNET_free (argv); 1421 GNUNET_free (argv);
1384 return ret; 1422 return ret;
1385} 1423}
@@ -1399,10 +1437,11 @@ GNUNET_OS_start_process_va (int pipe_control,
1399struct GNUNET_OS_Process * 1437struct GNUNET_OS_Process *
1400GNUNET_OS_start_process (int pipe_control, 1438GNUNET_OS_start_process (int pipe_control,
1401 enum GNUNET_OS_InheritStdioFlags std_inheritance, 1439 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1402 struct GNUNET_DISK_PipeHandle *pipe_stdin, 1440 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1403 struct GNUNET_DISK_PipeHandle *pipe_stdout, 1441 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1404 struct GNUNET_DISK_PipeHandle *pipe_stderr, 1442 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1405 const char *filename, ...) 1443 const char *filename,
1444 ...)
1406{ 1445{
1407 struct GNUNET_OS_Process *ret; 1446 struct GNUNET_OS_Process *ret;
1408 va_list ap; 1447 va_list ap;
@@ -1411,7 +1450,7 @@ GNUNET_OS_start_process (int pipe_control,
1411 ret = GNUNET_OS_start_process_va (pipe_control, 1450 ret = GNUNET_OS_start_process_va (pipe_control,
1412 std_inheritance, 1451 std_inheritance,
1413 pipe_stdin, 1452 pipe_stdin,
1414 pipe_stdout, 1453 pipe_stdout,
1415 pipe_stderr, 1454 pipe_stderr,
1416 filename, 1455 filename,
1417 ap); 1456 ap);
@@ -1437,18 +1476,18 @@ GNUNET_OS_start_process (int pipe_control,
1437struct GNUNET_OS_Process * 1476struct GNUNET_OS_Process *
1438GNUNET_OS_start_process_v (int pipe_control, 1477GNUNET_OS_start_process_v (int pipe_control,
1439 enum GNUNET_OS_InheritStdioFlags std_inheritance, 1478 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1440 const SOCKTYPE *lsocks, 1479 const SOCKTYPE *lsocks,
1441 const char *filename, 1480 const char *filename,
1442 char *const argv[]) 1481 char *const argv[])
1443{ 1482{
1444 return start_process (pipe_control, 1483 return start_process (pipe_control,
1445 std_inheritance, 1484 std_inheritance,
1446 NULL,
1447 NULL,
1448 NULL, 1485 NULL,
1449 lsocks, 1486 NULL,
1450 filename, 1487 NULL,
1451 argv); 1488 lsocks,
1489 filename,
1490 argv);
1452} 1491}
1453 1492
1454 1493
@@ -1473,8 +1512,9 @@ GNUNET_OS_start_process_v (int pipe_control,
1473struct GNUNET_OS_Process * 1512struct GNUNET_OS_Process *
1474GNUNET_OS_start_process_s (int pipe_control, 1513GNUNET_OS_start_process_s (int pipe_control,
1475 unsigned int std_inheritance, 1514 unsigned int std_inheritance,
1476 const SOCKTYPE * lsocks, 1515 const SOCKTYPE *lsocks,
1477 const char *filename, ...) 1516 const char *filename,
1517 ...)
1478{ 1518{
1479 va_list ap; 1519 va_list ap;
1480 char **argv; 1520 char **argv;
@@ -1502,29 +1542,28 @@ GNUNET_OS_start_process_s (int pipe_control,
1502 { 1542 {
1503 if ('"' == *rpos) 1543 if ('"' == *rpos)
1504 { 1544 {
1505 if (1 == quote_on) 1545 if (1 == quote_on)
1506 quote_on = 0; 1546 quote_on = 0;
1507 else 1547 else
1508 quote_on = 1; 1548 quote_on = 1;
1509 } 1549 }
1510 if ( (' ' == *rpos) && (0 == quote_on) ) 1550 if ((' ' == *rpos) && (0 == quote_on))
1511 { 1551 {
1512 if (NULL != last) 1552 if (NULL != last)
1513 argv_size++; 1553 argv_size++;
1514 last = NULL; 1554 last = NULL;
1515 rpos++; 1555 rpos++;
1516 while (' ' == *rpos) 1556 while (' ' == *rpos)
1517 rpos++; 1557 rpos++;
1518 } 1558 }
1519 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=? 1559 if ((NULL == last) && ('\0' != *rpos)) // FIXME: == or !=?
1520 last = rpos; 1560 last = rpos;
1521 if ('\0' != *rpos) 1561 if ('\0' != *rpos)
1522 rpos++; 1562 rpos++;
1523 } 1563 }
1524 if (NULL != last) 1564 if (NULL != last)
1525 argv_size++; 1565 argv_size++;
1526 } 1566 } while (NULL != (arg = (va_arg (ap, const char *))));
1527 while (NULL != (arg = (va_arg (ap, const char*))));
1528 va_end (ap); 1567 va_end (ap);
1529 1568
1530 argv = GNUNET_malloc (argv_size * sizeof (char *)); 1569 argv = GNUNET_malloc (argv_size * sizeof (char *));
@@ -1541,47 +1580,49 @@ GNUNET_OS_start_process_s (int pipe_control,
1541 { 1580 {
1542 if ('"' == *pos) 1581 if ('"' == *pos)
1543 { 1582 {
1544 if (1 == quote_on) 1583 if (1 == quote_on)
1545 quote_on = 0; 1584 quote_on = 0;
1546 else 1585 else
1547 quote_on = 1; 1586 quote_on = 1;
1548 } 1587 }
1549 if ( (' ' == *pos) && (0 == quote_on) ) 1588 if ((' ' == *pos) && (0 == quote_on))
1550 { 1589 {
1551 *pos = '\0'; 1590 *pos = '\0';
1552 if (NULL != last) 1591 if (NULL != last)
1553 argv[argv_size++] = GNUNET_strdup (last); 1592 argv[argv_size++] = GNUNET_strdup (last);
1554 last = NULL; 1593 last = NULL;
1555 pos++; 1594 pos++;
1556 while (' ' == *pos) 1595 while (' ' == *pos)
1557 pos++; 1596 pos++;
1558 } 1597 }
1559 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=? 1598 if ((NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1560 last = pos; 1599 last = pos;
1561 if ('\0' != *pos) 1600 if ('\0' != *pos)
1562 pos++; 1601 pos++;
1563 } 1602 }
1564 if (NULL != last) 1603 if (NULL != last)
1565 argv[argv_size++] = GNUNET_strdup (last); 1604 argv[argv_size++] = GNUNET_strdup (last);
1566 last = NULL; 1605 last = NULL;
1567 GNUNET_free (cp); 1606 GNUNET_free (cp);
1568 } 1607 } while (NULL != (arg = (va_arg (ap, const char *))));
1569 while (NULL != (arg = (va_arg (ap, const char*))));
1570 va_end (ap); 1608 va_end (ap);
1571 argv[argv_size] = NULL; 1609 argv[argv_size] = NULL;
1572 1610
1573 for(i = 0; i < argv_size; i++) 1611 for (i = 0; i < argv_size; i++)
1574 { 1612 {
1575 len = strlen (argv[i]); 1613 len = strlen (argv[i]);
1576 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"')) 1614 if ((argv[i][0] == '"') && (argv[i][len - 1] == '"'))
1577 { 1615 {
1578 memmove (&argv[i][0], &argv[i][1], len - 2); 1616 memmove (&argv[i][0], &argv[i][1], len - 2);
1579 argv[i][len-2] = '\0'; 1617 argv[i][len - 2] = '\0';
1580 } 1618 }
1581 } 1619 }
1582 binary_path = argv[0]; 1620 binary_path = argv[0];
1583 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks, 1621 proc = GNUNET_OS_start_process_v (pipe_control,
1584 binary_path, argv); 1622 std_inheritance,
1623 lsocks,
1624 binary_path,
1625 argv);
1585 while (argv_size > 0) 1626 while (argv_size > 0)
1586 GNUNET_free (argv[--argv_size]); 1627 GNUNET_free (argv[--argv_size]);
1587 GNUNET_free (argv); 1628 GNUNET_free (argv);
@@ -1613,8 +1654,7 @@ process_status (struct GNUNET_OS_Process *proc,
1613 ret = waitpid (proc->pid, &status, options); 1654 ret = waitpid (proc->pid, &status, options);
1614 if (ret < 0) 1655 if (ret < 0)
1615 { 1656 {
1616 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 1657 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1617 "waitpid");
1618 return GNUNET_SYSERR; 1658 return GNUNET_SYSERR;
1619 } 1659 }
1620 if (0 == ret) 1660 if (0 == ret)
@@ -1657,7 +1697,7 @@ process_status (struct GNUNET_OS_Process *proc,
1657 } 1697 }
1658#else 1698#else
1659#ifndef WNOHANG 1699#ifndef WNOHANG
1660#define WNOHANG 42 /* just a flag for W32, purely internal at this point */ 1700#define WNOHANG 42 /* just a flag for W32, purely internal at this point */
1661#endif 1701#endif
1662 1702
1663 HANDLE h; 1703 HANDLE h;
@@ -1669,7 +1709,8 @@ process_status (struct GNUNET_OS_Process *proc,
1669 { 1709 {
1670 LOG (GNUNET_ERROR_TYPE_WARNING, 1710 LOG (GNUNET_ERROR_TYPE_WARNING,
1671 "Invalid process information {%d, %08X}\n", 1711 "Invalid process information {%d, %08X}\n",
1672 ret, h); 1712 ret,
1713 h);
1673 return GNUNET_SYSERR; 1714 return GNUNET_SYSERR;
1674 } 1715 }
1675 if (h == NULL) 1716 if (h == NULL)
@@ -1720,10 +1761,7 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1720 enum GNUNET_OS_ProcessStatusType *type, 1761 enum GNUNET_OS_ProcessStatusType *type,
1721 unsigned long *code) 1762 unsigned long *code)
1722{ 1763{
1723 return process_status (proc, 1764 return process_status (proc, type, code, WNOHANG);
1724 type,
1725 code,
1726 WNOHANG);
1727} 1765}
1728 1766
1729 1767
@@ -1741,10 +1779,7 @@ GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
1741 enum GNUNET_OS_ProcessStatusType *type, 1779 enum GNUNET_OS_ProcessStatusType *type,
1742 unsigned long *code) 1780 unsigned long *code)
1743{ 1781{
1744 return process_status (proc, 1782 return process_status (proc, type, code, 0);
1745 type,
1746 code,
1747 0);
1748} 1783}
1749 1784
1750 1785
@@ -1765,12 +1800,11 @@ GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1765 pid_t pid = proc->pid; 1800 pid_t pid = proc->pid;
1766 pid_t ret; 1801 pid_t ret;
1767 1802
1768 while ( (pid != (ret = waitpid (pid, NULL, 0))) && 1803 while ((pid != (ret = waitpid (pid, NULL, 0))) && (EINTR == errno))
1769 (EINTR == errno) ) ; 1804 ;
1770 if (pid != ret) 1805 if (pid != ret)
1771 { 1806 {
1772 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 1807 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1773 "waitpid");
1774 return GNUNET_SYSERR; 1808 return GNUNET_SYSERR;
1775 } 1809 }
1776 return GNUNET_OK; 1810 return GNUNET_OK;
@@ -1782,7 +1816,8 @@ GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1782 { 1816 {
1783 LOG (GNUNET_ERROR_TYPE_WARNING, 1817 LOG (GNUNET_ERROR_TYPE_WARNING,
1784 "Invalid process information {%d, %08X}\n", 1818 "Invalid process information {%d, %08X}\n",
1785 proc->pid, h); 1819 proc->pid,
1820 h);
1786 return GNUNET_SYSERR; 1821 return GNUNET_SYSERR;
1787 } 1822 }
1788 if (NULL == h) 1823 if (NULL == h)
@@ -1890,9 +1925,7 @@ cmd_read (void *cls)
1890 1925
1891 cmd->rtask = NULL; 1926 cmd->rtask = NULL;
1892 tc = GNUNET_SCHEDULER_get_task_context (); 1927 tc = GNUNET_SCHEDULER_get_task_context ();
1893 if (GNUNET_YES != 1928 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1894 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
1895 cmd->r))
1896 { 1929 {
1897 /* timeout */ 1930 /* timeout */
1898 proc = cmd->proc; 1931 proc = cmd->proc;
@@ -1901,8 +1934,8 @@ cmd_read (void *cls)
1901 return; 1934 return;
1902 } 1935 }
1903 ret = GNUNET_DISK_file_read (cmd->r, 1936 ret = GNUNET_DISK_file_read (cmd->r,
1904 &cmd->buf[cmd->off], 1937 &cmd->buf[cmd->off],
1905 sizeof (cmd->buf) - cmd->off); 1938 sizeof (cmd->buf) - cmd->off);
1906 if (ret <= 0) 1939 if (ret <= 0)
1907 { 1940 {
1908 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf))) 1941 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
@@ -1925,11 +1958,12 @@ cmd_read (void *cls)
1925 cmd->off -= (end + 1 - cmd->buf); 1958 cmd->off -= (end + 1 - cmd->buf);
1926 end = memchr (cmd->buf, '\n', cmd->off); 1959 end = memchr (cmd->buf, '\n', cmd->off);
1927 } 1960 }
1928 cmd->rtask 1961 cmd->rtask =
1929 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining 1962 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining (
1930 (cmd->timeout), 1963 cmd->timeout),
1931 cmd->r, 1964 cmd->r,
1932 &cmd_read, cmd); 1965 &cmd_read,
1966 cmd);
1933} 1967}
1934 1968
1935 1969
@@ -1956,15 +1990,13 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1956 struct GNUNET_DISK_PipeHandle *opipe; 1990 struct GNUNET_DISK_PipeHandle *opipe;
1957 va_list ap; 1991 va_list ap;
1958 1992
1959 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, 1993 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1960 GNUNET_NO, GNUNET_YES);
1961 if (NULL == opipe) 1994 if (NULL == opipe)
1962 return NULL; 1995 return NULL;
1963 va_start (ap, binary); 1996 va_start (ap, binary);
1964 /* redirect stdout, don't inherit stderr/stdin */ 1997 /* redirect stdout, don't inherit stderr/stdin */
1965 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL, 1998 eip =
1966 opipe, NULL, binary, 1999 GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL, opipe, NULL, binary, ap);
1967 ap);
1968 va_end (ap); 2000 va_end (ap);
1969 if (NULL == eip) 2001 if (NULL == eip)
1970 { 2002 {
@@ -1978,12 +2010,8 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1978 cmd->opipe = opipe; 2010 cmd->opipe = opipe;
1979 cmd->proc = proc; 2011 cmd->proc = proc;
1980 cmd->proc_cls = proc_cls; 2012 cmd->proc_cls = proc_cls;
1981 cmd->r = GNUNET_DISK_pipe_handle (opipe, 2013 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1982 GNUNET_DISK_PIPE_END_READ); 2014 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1983 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
1984 cmd->r,
1985 &cmd_read,
1986 cmd);
1987 return cmd; 2015 return cmd;
1988} 2016}
1989 2017
diff --git a/src/util/perf_crypto_asymmetric.c b/src/util/perf_crypto_asymmetric.c
index a2cb3a6ce..f42706bda 100644
--- a/src/util/perf_crypto_asymmetric.c
+++ b/src/util/perf_crypto_asymmetric.c
@@ -49,7 +49,7 @@ log_duration (const char *cryptosystem,
49 sprintf (s, "%6s %15s", cryptosystem, description); 49 sprintf (s, "%6s %15s", cryptosystem, description);
50 t = GNUNET_TIME_absolute_get_duration (start); 50 t = GNUNET_TIME_absolute_get_duration (start);
51 t = GNUNET_TIME_relative_divide (t, l); 51 t = GNUNET_TIME_relative_divide (t, l);
52 FPRINTF (stdout, 52 fprintf (stdout,
53 "%s: %10s\n", 53 "%s: %10s\n",
54 s, 54 s,
55 GNUNET_STRINGS_relative_time_to_string (t, 55 GNUNET_STRINGS_relative_time_to_string (t,
diff --git a/src/util/perf_crypto_ecc_dlog.c b/src/util/perf_crypto_ecc_dlog.c
index 59fa08d3a..2729241e2 100644
--- a/src/util/perf_crypto_ecc_dlog.c
+++ b/src/util/perf_crypto_ecc_dlog.c
@@ -130,7 +130,7 @@ main (int argc, char *argv[])
130 130
131 if (! gcry_check_version ("1.6.0")) 131 if (! gcry_check_version ("1.6.0"))
132 { 132 {
133 FPRINTF (stderr, 133 fprintf (stderr,
134 _ 134 _
135 ("libgcrypt has not the expected version (version %s is required).\n"), 135 ("libgcrypt has not the expected version (version %s is required).\n"),
136 "1.6.0"); 136 "1.6.0");
diff --git a/src/util/plugin.c b/src/util/plugin.c
index 4b922340d..19169f070 100644
--- a/src/util/plugin.c
+++ b/src/util/plugin.c
@@ -28,7 +28,7 @@
28#include <ltdl.h> 28#include <ltdl.h>
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30 30
31#define LOG(kind,...) GNUNET_log_from (kind, "util-plugin", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-plugin", __VA_ARGS__)
32 32
33/** 33/**
34 * Linked list of active plugins. 34 * Linked list of active plugins.
@@ -82,8 +82,8 @@ plugin_init ()
82 err = lt_dlinit (); 82 err = lt_dlinit ();
83 if (err > 0) 83 if (err > 0)
84 { 84 {
85 FPRINTF (stderr, 85 fprintf (stderr,
86 _("Initialization of plugin mechanism failed: %s!\n"), 86 _ ("Initialization of plugin mechanism failed: %s!\n"),
87 lt_dlerror ()); 87 lt_dlerror ());
88 return; 88 return;
89 } 89 }
@@ -133,24 +133,21 @@ plugin_fini ()
133 * @return NULL if the symbol was not found 133 * @return NULL if the symbol was not found
134 */ 134 */
135static GNUNET_PLUGIN_Callback 135static GNUNET_PLUGIN_Callback
136resolve_function (struct PluginList *plug, 136resolve_function (struct PluginList *plug, const char *name)
137 const char *name)
138{ 137{
139 char *initName; 138 char *initName;
140 void *mptr; 139 void *mptr;
141 140
142 GNUNET_asprintf (&initName, 141 GNUNET_asprintf (&initName, "_%s_%s", plug->name, name);
143 "_%s_%s",
144 plug->name,
145 name);
146 mptr = lt_dlsym (plug->handle, &initName[1]); 142 mptr = lt_dlsym (plug->handle, &initName[1]);
147 if (NULL == mptr) 143 if (NULL == mptr)
148 mptr = lt_dlsym (plug->handle, initName); 144 mptr = lt_dlsym (plug->handle, initName);
149 if (NULL == mptr) 145 if (NULL == mptr)
150 LOG (GNUNET_ERROR_TYPE_ERROR, 146 LOG (GNUNET_ERROR_TYPE_ERROR,
151 _("`%s' failed to resolve method '%s' with error: %s\n"), 147 _ ("`%s' failed to resolve method '%s' with error: %s\n"),
152 "lt_dlsym", 148 "lt_dlsym",
153 &initName[1], lt_dlerror ()); 149 &initName[1],
150 lt_dlerror ());
154 GNUNET_free (initName); 151 GNUNET_free (initName);
155 return mptr; 152 return mptr;
156} 153}
@@ -214,7 +211,7 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
214 GNUNET_PLUGIN_Callback init; 211 GNUNET_PLUGIN_Callback init;
215 void *ret; 212 void *ret;
216 213
217 if (!initialized) 214 if (! initialized)
218 { 215 {
219 initialized = GNUNET_YES; 216 initialized = GNUNET_YES;
220 plugin_init (); 217 plugin_init ();
@@ -223,9 +220,10 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
223 if (libhandle == NULL) 220 if (libhandle == NULL)
224 { 221 {
225 LOG (GNUNET_ERROR_TYPE_ERROR, 222 LOG (GNUNET_ERROR_TYPE_ERROR,
226 _("`%s' failed for library `%s' with error: %s\n"), 223 _ ("`%s' failed for library `%s' with error: %s\n"),
227 "lt_dlopenext", 224 "lt_dlopenext",
228 library_name, lt_dlerror ()); 225 library_name,
226 lt_dlerror ());
229 return NULL; 227 return NULL;
230 } 228 }
231 plug = GNUNET_new (struct PluginList); 229 plug = GNUNET_new (struct PluginList);
@@ -255,8 +253,7 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
255 * @return whatever the shutdown function returned 253 * @return whatever the shutdown function returned
256 */ 254 */
257void * 255void *
258GNUNET_PLUGIN_unload (const char *library_name, 256GNUNET_PLUGIN_unload (const char *library_name, void *arg)
259 void *arg)
260{ 257{
261 struct PluginList *pos; 258 struct PluginList *pos;
262 struct PluginList *prev; 259 struct PluginList *prev;
@@ -345,9 +342,9 @@ find_libraries (void *cls, const char *filename)
345 libname = slashpos + 1; 342 libname = slashpos + 1;
346 n = strlen (libname); 343 n = strlen (libname);
347 if (0 != strncmp (lac->basename, libname, strlen (lac->basename))) 344 if (0 != strncmp (lac->basename, libname, strlen (lac->basename)))
348 return GNUNET_OK; /* wrong name */ 345 return GNUNET_OK; /* wrong name */
349 if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la"))) 346 if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la")))
350 return GNUNET_OK; /* .la file */ 347 return GNUNET_OK; /* .la file */
351 basename = GNUNET_strdup (libname); 348 basename = GNUNET_strdup (libname);
352 if (NULL != (dot = strstr (basename, "."))) 349 if (NULL != (dot = strstr (basename, ".")))
353 *dot = '\0'; 350 *dot = '\0';
@@ -372,8 +369,10 @@ find_libraries (void *cls, const char *filename)
372 * @param cb_cls closure for @a cb 369 * @param cb_cls closure for @a cb
373 */ 370 */
374void 371void
375GNUNET_PLUGIN_load_all (const char *basename, void *arg, 372GNUNET_PLUGIN_load_all (const char *basename,
376 GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls) 373 void *arg,
374 GNUNET_PLUGIN_LoaderCallback cb,
375 void *cb_cls)
377{ 376{
378 struct LoadAllContext lac; 377 struct LoadAllContext lac;
379 char *path; 378 char *path;
@@ -382,7 +381,7 @@ GNUNET_PLUGIN_load_all (const char *basename, void *arg,
382 if (NULL == path) 381 if (NULL == path)
383 { 382 {
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 _("Could not determine plugin installation path.\n")); 384 _ ("Could not determine plugin installation path.\n"));
386 return; 385 return;
387 } 386 }
388 lac.basename = basename; 387 lac.basename = basename;
diff --git a/src/util/program.c b/src/util/program.c
index 73beb8d57..77f4ea3ae 100644
--- a/src/util/program.c
+++ b/src/util/program.c
@@ -201,18 +201,18 @@ GNUNET_PROGRAM_run2 (int argc,
201 /* prepare */ 201 /* prepare */
202#if ENABLE_NLS 202#if ENABLE_NLS
203 if (NULL != pd->gettext_domain) 203 if (NULL != pd->gettext_domain)
204 {
205 setlocale (LC_ALL, "");
206 path = (NULL == pd->gettext_path)
207 ? GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR)
208 : GNUNET_strdup (pd->gettext_path);
209 if (NULL != path)
204 { 210 {
205 setlocale (LC_ALL, ""); 211 bindtextdomain (pd->gettext_domain, path);
206 path = (NULL == pd->gettext_path) 212 GNUNET_free (path);
207 ? GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR)
208 : GNUNET_strdup (pd->gettext_path);
209 if (NULL != path)
210 {
211 BINDTEXTDOMAIN (pd->gettext_domain, path);
212 GNUNET_free (path);
213 }
214 textdomain (pd->gettext_domain);
215 } 213 }
214 textdomain (pd->gettext_domain);
215 }
216#endif 216#endif
217 cnt = 0; 217 cnt = 0;
218 while (NULL != options[cnt].name) 218 while (NULL != options[cnt].name)
diff --git a/src/util/service.c b/src/util/service.c
index 73a73cbea..fba5a2f20 100644
--- a/src/util/service.c
+++ b/src/util/service.c
@@ -37,14 +37,13 @@
37#endif 37#endif
38 38
39 39
40#define LOG(kind,...) GNUNET_log_from (kind, "util-service", __VA_ARGS__) 40#define LOG(kind, ...) GNUNET_log_from (kind, "util-service", __VA_ARGS__)
41 41
42#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, \ 42#define LOG_STRERROR(kind, syscall) \
43 "util-service", \ 43 GNUNET_log_from_strerror (kind, "util-service", syscall)
44 syscall)
45 44
46#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file ( \ 45#define LOG_STRERROR_FILE(kind, syscall, filename) \
47 kind, "util-service", syscall, filename) 46 GNUNET_log_from_strerror_file (kind, "util-service", syscall, filename)
48 47
49 48
50/** 49/**
@@ -77,7 +76,6 @@ struct ServiceListenContext
77 * Task scheduled to do the listening. 76 * Task scheduled to do the listening.
78 */ 77 */
79 struct GNUNET_SCHEDULER_Task *listen_task; 78 struct GNUNET_SCHEDULER_Task *listen_task;
80
81}; 79};
82 80
83 81
@@ -363,8 +361,7 @@ struct GNUNET_SERVICE_Client
363static int 361static int
364have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh) 362have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
365{ 363{
366 for (struct GNUNET_SERVICE_Client *client = sh->clients_head; 364 for (struct GNUNET_SERVICE_Client *client = sh->clients_head; NULL != client;
367 NULL != client;
368 client = client->next) 365 client = client->next)
369 { 366 {
370 if (client->is_monitor) 367 if (client->is_monitor)
@@ -383,8 +380,7 @@ have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
383 * @param sr reason for suspending accepting connections 380 * @param sr reason for suspending accepting connections
384 */ 381 */
385static void 382static void
386do_suspend (struct GNUNET_SERVICE_Handle *sh, 383do_suspend (struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
387 enum SuspendReason sr)
388{ 384{
389 struct ServiceListenContext *slc; 385 struct ServiceListenContext *slc;
390 386
@@ -426,8 +422,7 @@ service_shutdown (void *cls)
426 break; 422 break;
427 case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN: 423 case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN:
428 if (0 == (sh->suspend_state & SUSPEND_STATE_SHUTDOWN)) 424 if (0 == (sh->suspend_state & SUSPEND_STATE_SHUTDOWN))
429 do_suspend (sh, 425 do_suspend (sh, SUSPEND_STATE_SHUTDOWN);
430 SUSPEND_STATE_SHUTDOWN);
431 if (GNUNET_NO == have_non_monitor_clients (sh)) 426 if (GNUNET_NO == have_non_monitor_clients (sh))
432 GNUNET_SERVICE_shutdown (sh); 427 GNUNET_SERVICE_shutdown (sh);
433 break; 428 break;
@@ -451,8 +446,7 @@ check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
451 if (NULL == list) 446 if (NULL == list)
452 return GNUNET_NO; 447 return GNUNET_NO;
453 i = 0; 448 i = 0;
454 while ( (0 != list[i].network.s_addr) || 449 while ((0 != list[i].network.s_addr) || (0 != list[i].netmask.s_addr))
455 (0 != list[i].netmask.s_addr) )
456 { 450 {
457 if ((add->s_addr & list[i].netmask.s_addr) == 451 if ((add->s_addr & list[i].netmask.s_addr) ==
458 (list[i].network.s_addr & list[i].netmask.s_addr)) 452 (list[i].network.s_addr & list[i].netmask.s_addr))
@@ -480,7 +474,7 @@ check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
480 if (NULL == list) 474 if (NULL == list)
481 return GNUNET_NO; 475 return GNUNET_NO;
482 i = 0; 476 i = 0;
483 NEXT: 477NEXT:
484 while (0 != GNUNET_is_zero (&list[i].network)) 478 while (0 != GNUNET_is_zero (&list[i].network))
485 { 479 {
486 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++) 480 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
@@ -518,22 +512,17 @@ do_send (void *cls)
518 client->send_task = NULL; 512 client->send_task = NULL;
519 buf = (const char *) client->msg; 513 buf = (const char *) client->msg;
520 left = ntohs (client->msg->size) - client->msg_pos; 514 left = ntohs (client->msg->size) - client->msg_pos;
521 ret = GNUNET_NETWORK_socket_send (client->sock, 515 ret = GNUNET_NETWORK_socket_send (client->sock, &buf[client->msg_pos], left);
522 &buf[client->msg_pos],
523 left);
524 GNUNET_assert (ret <= (ssize_t) left); 516 GNUNET_assert (ret <= (ssize_t) left);
525 if (0 == ret) 517 if (0 == ret)
526 { 518 {
527 LOG (GNUNET_ERROR_TYPE_DEBUG, 519 LOG (GNUNET_ERROR_TYPE_DEBUG, "no data send");
528 "no data send"); 520 GNUNET_MQ_inject_error (client->mq, GNUNET_MQ_ERROR_WRITE);
529 GNUNET_MQ_inject_error (client->mq,
530 GNUNET_MQ_ERROR_WRITE);
531 return; 521 return;
532 } 522 }
533 if (-1 == ret) 523 if (-1 == ret)
534 { 524 {
535 if ( (EAGAIN == errno) || 525 if ((EAGAIN == errno) || (EINTR == errno))
536 (EINTR == errno) )
537 { 526 {
538 /* ignore */ 527 /* ignore */
539 ret = 0; 528 ret = 0;
@@ -541,13 +530,11 @@ do_send (void *cls)
541 else 530 else
542 { 531 {
543 if (EPIPE != errno) 532 if (EPIPE != errno)
544 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 533 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
545 "send");
546 LOG (GNUNET_ERROR_TYPE_DEBUG, 534 LOG (GNUNET_ERROR_TYPE_DEBUG,
547 "socket send returned with error code %i", 535 "socket send returned with error code %i",
548 errno); 536 errno);
549 GNUNET_MQ_inject_error (client->mq, 537 GNUNET_MQ_inject_error (client->mq, GNUNET_MQ_ERROR_WRITE);
550 GNUNET_MQ_ERROR_WRITE);
551 return; 538 return;
552 } 539 }
553 } 540 }
@@ -559,11 +546,11 @@ do_send (void *cls)
559 if (left > (size_t) ret) 546 if (left > (size_t) ret)
560 { 547 {
561 GNUNET_assert (NULL == client->drop_task); 548 GNUNET_assert (NULL == client->drop_task);
562 client->send_task 549 client->send_task =
563 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, 550 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
564 client->sock, 551 client->sock,
565 &do_send, 552 &do_send,
566 client); 553 client);
567 return; 554 return;
568 } 555 }
569 GNUNET_MQ_impl_send_continue (client->mq); 556 GNUNET_MQ_impl_send_continue (client->mq);
@@ -595,11 +582,11 @@ service_mq_send (struct GNUNET_MQ_Handle *mq,
595 ntohs (msg->size)); 582 ntohs (msg->size));
596 client->msg = msg; 583 client->msg = msg;
597 client->msg_pos = 0; 584 client->msg_pos = 0;
598 client->send_task 585 client->send_task =
599 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, 586 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
600 client->sock, 587 client->sock,
601 &do_send, 588 &do_send,
602 client); 589 client);
603} 590}
604 591
605 592
@@ -610,8 +597,7 @@ service_mq_send (struct GNUNET_MQ_Handle *mq,
610 * @param impl_state state specific to the implementation 597 * @param impl_state state specific to the implementation
611 */ 598 */
612static void 599static void
613service_mq_cancel (struct GNUNET_MQ_Handle *mq, 600service_mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
614 void *impl_state)
615{ 601{
616 struct GNUNET_SERVICE_Client *client = impl_state; 602 struct GNUNET_SERVICE_Client *client = impl_state;
617 603
@@ -633,14 +619,12 @@ service_mq_cancel (struct GNUNET_MQ_Handle *mq,
633 * @param error error code 619 * @param error error code
634 */ 620 */
635static void 621static void
636service_mq_error_handler (void *cls, 622service_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
637 enum GNUNET_MQ_Error error)
638{ 623{
639 struct GNUNET_SERVICE_Client *client = cls; 624 struct GNUNET_SERVICE_Client *client = cls;
640 struct GNUNET_SERVICE_Handle *sh = client->sh; 625 struct GNUNET_SERVICE_Handle *sh = client->sh;
641 626
642 if ( (GNUNET_MQ_ERROR_NO_MATCH == error) && 627 if ((GNUNET_MQ_ERROR_NO_MATCH == error) && (GNUNET_NO == sh->require_found))
643 (GNUNET_NO == sh->require_found) )
644 { 628 {
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "No handler for message of type %u found\n", 630 "No handler for message of type %u found\n",
@@ -662,18 +646,20 @@ warn_no_client_continue (void *cls)
662{ 646{
663 struct GNUNET_SERVICE_Client *client = cls; 647 struct GNUNET_SERVICE_Client *client = cls;
664 648
665 GNUNET_break (0 != client->warn_type); /* type should never be 0 here, as we don't use 0 */ 649 GNUNET_break (
666 client->warn_task 650 0 !=
667 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 651 client->warn_type); /* type should never be 0 here, as we don't use 0 */
668 &warn_no_client_continue, 652 client->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
669 client); 653 &warn_no_client_continue,
670 LOG (GNUNET_ERROR_TYPE_WARNING, 654 client);
671 _ ( 655 LOG (
672 "Processing code for message of type %u did not call `GNUNET_SERVICE_client_continue' after %s\n"), 656 GNUNET_ERROR_TYPE_WARNING,
673 (unsigned int) client->warn_type, 657 _ (
674 GNUNET_STRINGS_relative_time_to_string ( 658 "Processing code for message of type %u did not call `GNUNET_SERVICE_client_continue' after %s\n"),
675 GNUNET_TIME_absolute_get_duration (client->warn_start), 659 (unsigned int) client->warn_type,
676 GNUNET_YES)); 660 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (
661 client->warn_start),
662 GNUNET_YES));
677} 663}
678 664
679 665
@@ -689,8 +675,7 @@ warn_no_client_continue (void *cls)
689 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the client was dropped 675 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the client was dropped
690 */ 676 */
691static int 677static int
692service_client_mst_cb (void *cls, 678service_client_mst_cb (void *cls, const struct GNUNET_MessageHeader *message)
693 const struct GNUNET_MessageHeader *message)
694{ 679{
695 struct GNUNET_SERVICE_Client *client = cls; 680 struct GNUNET_SERVICE_Client *client = cls;
696 681
@@ -703,12 +688,10 @@ service_client_mst_cb (void *cls,
703 client->warn_type = ntohs (message->type); 688 client->warn_type = ntohs (message->type);
704 client->warn_start = GNUNET_TIME_absolute_get (); 689 client->warn_start = GNUNET_TIME_absolute_get ();
705 GNUNET_assert (NULL == client->warn_task); 690 GNUNET_assert (NULL == client->warn_task);
706 client->warn_task 691 client->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
707 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 692 &warn_no_client_continue,
708 &warn_no_client_continue, 693 client);
709 client); 694 GNUNET_MQ_inject_message (client->mq, message);
710 GNUNET_MQ_inject_message (client->mq,
711 message);
712 if (NULL != client->drop_task) 695 if (NULL != client->drop_task)
713 return GNUNET_SYSERR; 696 return GNUNET_SYSERR;
714 return GNUNET_OK; 697 return GNUNET_OK;
@@ -728,10 +711,7 @@ service_client_recv (void *cls)
728 int ret; 711 int ret;
729 712
730 client->recv_task = NULL; 713 client->recv_task = NULL;
731 ret = GNUNET_MST_read (client->mst, 714 ret = GNUNET_MST_read (client->mst, client->sock, GNUNET_NO, GNUNET_YES);
732 client->sock,
733 GNUNET_NO,
734 GNUNET_YES);
735 if (GNUNET_SYSERR == ret) 715 if (GNUNET_SYSERR == ret)
736 { 716 {
737 /* client closed connection (or IO error) */ 717 /* client closed connection (or IO error) */
@@ -751,11 +731,11 @@ service_client_recv (void *cls)
751 if (NULL != client->recv_task) 731 if (NULL != client->recv_task)
752 return; 732 return;
753 /* MST needs more data, re-schedule read job */ 733 /* MST needs more data, re-schedule read job */
754 client->recv_task 734 client->recv_task =
755 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 735 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
756 client->sock, 736 client->sock,
757 &service_client_recv, 737 &service_client_recv,
758 client); 738 client);
759} 739}
760 740
761 741
@@ -773,9 +753,7 @@ start_client (struct GNUNET_SERVICE_Handle *sh,
773 struct GNUNET_SERVICE_Client *client; 753 struct GNUNET_SERVICE_Client *client;
774 754
775 client = GNUNET_new (struct GNUNET_SERVICE_Client); 755 client = GNUNET_new (struct GNUNET_SERVICE_Client);
776 GNUNET_CONTAINER_DLL_insert (sh->clients_head, 756 GNUNET_CONTAINER_DLL_insert (sh->clients_head, sh->clients_tail, client);
777 sh->clients_tail,
778 client);
779 client->sh = sh; 757 client->sh = sh;
780 client->sock = csock; 758 client->sock = csock;
781 client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send, 759 client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send,
@@ -785,19 +763,15 @@ start_client (struct GNUNET_SERVICE_Handle *sh,
785 sh->handlers, 763 sh->handlers,
786 &service_mq_error_handler, 764 &service_mq_error_handler,
787 client); 765 client);
788 client->mst = GNUNET_MST_create (&service_client_mst_cb, 766 client->mst = GNUNET_MST_create (&service_client_mst_cb, client);
789 client);
790 if (NULL != sh->connect_cb) 767 if (NULL != sh->connect_cb)
791 client->user_context = sh->connect_cb (sh->cb_cls, 768 client->user_context = sh->connect_cb (sh->cb_cls, client, client->mq);
792 client, 769 GNUNET_MQ_set_handlers_closure (client->mq, client->user_context);
793 client->mq); 770 client->recv_task =
794 GNUNET_MQ_set_handlers_closure (client->mq, 771 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
795 client->user_context); 772 client->sock,
796 client->recv_task 773 &service_client_recv,
797 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 774 client);
798 client->sock,
799 &service_client_recv,
800 client);
801} 775}
802 776
803 777
@@ -830,11 +804,9 @@ accept_client (void *cls)
830 if (NULL == sock) 804 if (NULL == sock)
831 { 805 {
832 if (EMFILE == errno) 806 if (EMFILE == errno)
833 do_suspend (sh, 807 do_suspend (sh, SUSPEND_STATE_EMFILE);
834 SUSPEND_STATE_EMFILE);
835 else if (EAGAIN != errno) 808 else if (EAGAIN != errno)
836 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 809 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
837 "accept");
838 break; 810 break;
839 } 811 }
840 switch (sa.ss_family) 812 switch (sa.ss_family)
@@ -842,26 +814,22 @@ accept_client (void *cls)
842 case AF_INET: 814 case AF_INET:
843 GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); 815 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
844 v4 = (const struct sockaddr_in *) &sa; 816 v4 = (const struct sockaddr_in *) &sa;
845 ok = ( ( (NULL == sh->v4_allowed) || 817 ok = (((NULL == sh->v4_allowed) ||
846 (check_ipv4_listed (sh->v4_allowed, 818 (check_ipv4_listed (sh->v4_allowed, &v4->sin_addr))) &&
847 &v4->sin_addr))) && 819 ((NULL == sh->v4_denied) ||
848 ( (NULL == sh->v4_denied) || 820 (! check_ipv4_listed (sh->v4_denied, &v4->sin_addr))));
849 (! check_ipv4_listed (sh->v4_denied,
850 &v4->sin_addr)) ) );
851 break; 821 break;
852 case AF_INET6: 822 case AF_INET6:
853 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); 823 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
854 v6 = (const struct sockaddr_in6 *) &sa; 824 v6 = (const struct sockaddr_in6 *) &sa;
855 ok = ( ( (NULL == sh->v6_allowed) || 825 ok = (((NULL == sh->v6_allowed) ||
856 (check_ipv6_listed (sh->v6_allowed, 826 (check_ipv6_listed (sh->v6_allowed, &v6->sin6_addr))) &&
857 &v6->sin6_addr))) && 827 ((NULL == sh->v6_denied) ||
858 ( (NULL == sh->v6_denied) || 828 (! check_ipv6_listed (sh->v6_denied, &v6->sin6_addr))));
859 (! check_ipv6_listed (sh->v6_denied,
860 &v6->sin6_addr)) ) );
861 break; 829 break;
862#ifndef WINDOWS 830#ifndef WINDOWS
863 case AF_UNIX: 831 case AF_UNIX:
864 ok = GNUNET_OK; /* controlled using file-system ACL now */ 832 ok = GNUNET_OK; /* controlled using file-system ACL now */
865 break; 833 break;
866#endif 834#endif
867 default: 835 default:
@@ -874,26 +842,22 @@ accept_client (void *cls)
874 { 842 {
875 LOG (GNUNET_ERROR_TYPE_DEBUG, 843 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "Service rejected incoming connection from %s due to policy.\n", 844 "Service rejected incoming connection from %s due to policy.\n",
877 GNUNET_a2s ((const struct sockaddr *) &sa, 845 GNUNET_a2s ((const struct sockaddr *) &sa, addrlen));
878 addrlen)); 846 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
879 GNUNET_break (GNUNET_OK ==
880 GNUNET_NETWORK_socket_close (sock));
881 continue; 847 continue;
882 } 848 }
883 LOG (GNUNET_ERROR_TYPE_DEBUG, 849 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "Service accepted incoming connection from %s.\n", 850 "Service accepted incoming connection from %s.\n",
885 GNUNET_a2s ((const struct sockaddr *) &sa, 851 GNUNET_a2s ((const struct sockaddr *) &sa, addrlen));
886 addrlen)); 852 start_client (slc->sh, sock);
887 start_client (slc->sh,
888 sock);
889 } 853 }
890 if (0 != sh->suspend_state) 854 if (0 != sh->suspend_state)
891 return; 855 return;
892 slc->listen_task 856 slc->listen_task =
893 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 857 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
894 slc->listen_socket, 858 slc->listen_socket,
895 &accept_client, 859 &accept_client,
896 slc); 860 slc);
897} 861}
898 862
899 863
@@ -905,24 +869,22 @@ accept_client (void *cls)
905 * or #SUSPEND_STATE_NONE on first startup 869 * or #SUSPEND_STATE_NONE on first startup
906 */ 870 */
907static void 871static void
908do_resume (struct GNUNET_SERVICE_Handle *sh, 872do_resume (struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
909 enum SuspendReason sr)
910{ 873{
911 struct ServiceListenContext *slc; 874 struct ServiceListenContext *slc;
912 875
913 GNUNET_assert ( (SUSPEND_STATE_NONE == sr) || 876 GNUNET_assert ((SUSPEND_STATE_NONE == sr) || (0 != (sh->suspend_state & sr)));
914 (0 != (sh->suspend_state & sr)) );
915 sh->suspend_state -= sr; 877 sh->suspend_state -= sr;
916 if (SUSPEND_STATE_NONE != sh->suspend_state) 878 if (SUSPEND_STATE_NONE != sh->suspend_state)
917 return; 879 return;
918 for (slc = sh->slc_head; NULL != slc; slc = slc->next) 880 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
919 { 881 {
920 GNUNET_assert (NULL == slc->listen_task); 882 GNUNET_assert (NULL == slc->listen_task);
921 slc->listen_task 883 slc->listen_task =
922 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 884 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
923 slc->listen_socket, 885 slc->listen_socket,
924 &accept_client, 886 &accept_client,
925 slc); 887 slc);
926 } 888 }
927} 889}
928 890
@@ -940,22 +902,18 @@ service_main (void *cls)
940 struct GNUNET_SERVICE_Handle *sh = cls; 902 struct GNUNET_SERVICE_Handle *sh = cls;
941 903
942 if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN != sh->options) 904 if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN != sh->options)
943 GNUNET_SCHEDULER_add_shutdown (&service_shutdown, 905 GNUNET_SCHEDULER_add_shutdown (&service_shutdown, sh);
944 sh); 906 do_resume (sh, SUSPEND_STATE_NONE);
945 do_resume (sh,
946 SUSPEND_STATE_NONE);
947 907
948 if (-1 != sh->ready_confirm_fd) 908 if (-1 != sh->ready_confirm_fd)
949 { 909 {
950 GNUNET_break (1 == WRITE (sh->ready_confirm_fd, ".", 1)); 910 GNUNET_break (1 == write (sh->ready_confirm_fd, ".", 1));
951 GNUNET_break (0 == CLOSE (sh->ready_confirm_fd)); 911 GNUNET_break (0 == close (sh->ready_confirm_fd));
952 sh->ready_confirm_fd = -1; 912 sh->ready_confirm_fd = -1;
953 } 913 }
954 914
955 if (NULL != sh->service_init_cb) 915 if (NULL != sh->service_init_cb)
956 sh->service_init_cb (sh->cb_cls, 916 sh->service_init_cb (sh->cb_cls, sh->cfg, sh);
957 sh->cfg,
958 sh);
959} 917}
960 918
961 919
@@ -975,9 +933,7 @@ process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
975{ 933{
976 char *opt; 934 char *opt;
977 935
978 if (! GNUNET_CONFIGURATION_have_value (sh->cfg, 936 if (! GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
979 sh->service_name,
980 option))
981 { 937 {
982 *ret = NULL; 938 *ret = NULL;
983 return GNUNET_OK; 939 return GNUNET_OK;
@@ -1018,9 +974,7 @@ process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
1018{ 974{
1019 char *opt; 975 char *opt;
1020 976
1021 if (! GNUNET_CONFIGURATION_have_value (sh->cfg, 977 if (! GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
1022 sh->service_name,
1023 option))
1024 { 978 {
1025 *ret = NULL; 979 *ret = NULL;
1026 return GNUNET_OK; 980 return GNUNET_OK;
@@ -1066,9 +1020,7 @@ add_unixpath (struct sockaddr **saddrs,
1066 1020
1067 un = GNUNET_new (struct sockaddr_un); 1021 un = GNUNET_new (struct sockaddr_un);
1068 un->sun_family = AF_UNIX; 1022 un->sun_family = AF_UNIX;
1069 GNUNET_strlcpy (un->sun_path, 1023 GNUNET_strlcpy (un->sun_path, unixpath, sizeof (un->sun_path));
1070 unixpath,
1071 sizeof (un->sun_path));
1072#ifdef LINUX 1024#ifdef LINUX
1073 if (GNUNET_YES == abstract) 1025 if (GNUNET_YES == abstract)
1074 un->sun_path[0] = '\0'; 1026 un->sun_path[0] = '\0';
@@ -1132,24 +1084,18 @@ get_server_addresses (const char *service_name,
1132 *addr_lens = NULL; 1084 *addr_lens = NULL;
1133 desc = NULL; 1085 desc = NULL;
1134 disablev6 = GNUNET_NO; 1086 disablev6 = GNUNET_NO;
1135 if ( (GNUNET_NO == 1087 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1136 GNUNET_NETWORK_test_pf (PF_INET6)) || 1088 (GNUNET_YES ==
1137 (GNUNET_YES == 1089 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
1138 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1139 service_name,
1140 "DISABLEV6") ) )
1141 disablev6 = GNUNET_YES; 1090 disablev6 = GNUNET_YES;
1142 1091
1143 port = 0; 1092 port = 0;
1144 if (GNUNET_CONFIGURATION_have_value (cfg, 1093 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
1145 service_name,
1146 "PORT"))
1147 { 1094 {
1148 if (GNUNET_OK != 1095 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
1149 GNUNET_CONFIGURATION_get_value_number (cfg, 1096 service_name,
1150 service_name, 1097 "PORT",
1151 "PORT", 1098 &port))
1152 &port))
1153 { 1099 {
1154 LOG (GNUNET_ERROR_TYPE_ERROR, 1100 LOG (GNUNET_ERROR_TYPE_ERROR,
1155 _ ("Require valid port number for service `%s' in configuration!\n"), 1101 _ ("Require valid port number for service `%s' in configuration!\n"),
@@ -1164,9 +1110,7 @@ get_server_addresses (const char *service_name,
1164 } 1110 }
1165 } 1111 }
1166 1112
1167 if (GNUNET_CONFIGURATION_have_value (cfg, 1113 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
1168 service_name,
1169 "BINDTO"))
1170 { 1114 {
1171 GNUNET_break (GNUNET_OK == 1115 GNUNET_break (GNUNET_OK ==
1172 GNUNET_CONFIGURATION_get_value_string (cfg, 1116 GNUNET_CONFIGURATION_get_value_string (cfg,
@@ -1181,14 +1125,11 @@ get_server_addresses (const char *service_name,
1181 abstract = GNUNET_NO; 1125 abstract = GNUNET_NO;
1182#ifdef AF_UNIX 1126#ifdef AF_UNIX
1183 if ((GNUNET_YES == 1127 if ((GNUNET_YES ==
1184 GNUNET_CONFIGURATION_have_value (cfg, 1128 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
1185 service_name, 1129 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
1186 "UNIXPATH")) && 1130 service_name,
1187 (GNUNET_OK == 1131 "UNIXPATH",
1188 GNUNET_CONFIGURATION_get_value_filename (cfg, 1132 &unixpath)) &&
1189 service_name,
1190 "UNIXPATH",
1191 &unixpath)) &&
1192 (0 < strlen (unixpath))) 1133 (0 < strlen (unixpath)))
1193 { 1134 {
1194 /* probe UNIX support */ 1135 /* probe UNIX support */
@@ -1201,9 +1142,7 @@ get_server_addresses (const char *service_name,
1201 unixpath, 1142 unixpath,
1202 (unsigned long long) sizeof (s_un.sun_path)); 1143 (unsigned long long) sizeof (s_un.sun_path));
1203 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath); 1144 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
1204 LOG (GNUNET_ERROR_TYPE_INFO, 1145 LOG (GNUNET_ERROR_TYPE_INFO, _ ("Using `%s' instead\n"), unixpath);
1205 _ ("Using `%s' instead\n"),
1206 unixpath);
1207 } 1146 }
1208#ifdef LINUX 1147#ifdef LINUX
1209 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1148 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
@@ -1212,27 +1151,19 @@ get_server_addresses (const char *service_name,
1212 if (GNUNET_SYSERR == abstract) 1151 if (GNUNET_SYSERR == abstract)
1213 abstract = GNUNET_NO; 1152 abstract = GNUNET_NO;
1214#endif 1153#endif
1215 if ( (GNUNET_YES != abstract) && 1154 if ((GNUNET_YES != abstract) &&
1216 (GNUNET_OK != 1155 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (unixpath)))
1217 GNUNET_DISK_directory_create_for_file (unixpath)) ) 1156 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", unixpath);
1218 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1219 "mkdir",
1220 unixpath);
1221 } 1157 }
1222 if (NULL != unixpath) 1158 if (NULL != unixpath)
1223 { 1159 {
1224 desc = GNUNET_NETWORK_socket_create (AF_UNIX, 1160 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
1225 SOCK_STREAM,
1226 0);
1227 if (NULL == desc) 1161 if (NULL == desc)
1228 { 1162 {
1229 if ((ENOBUFS == errno) || 1163 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1230 (ENOMEM == errno) ||
1231 (ENFILE == errno) ||
1232 (EACCES == errno)) 1164 (EACCES == errno))
1233 { 1165 {
1234 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1166 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
1235 "socket");
1236 GNUNET_free_non_null (hostname); 1167 GNUNET_free_non_null (hostname);
1237 GNUNET_free (unixpath); 1168 GNUNET_free (unixpath);
1238 return GNUNET_SYSERR; 1169 return GNUNET_SYSERR;
@@ -1241,14 +1172,13 @@ get_server_addresses (const char *service_name,
1241 _ ( 1172 _ (
1242 "Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"), 1173 "Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
1243 service_name, 1174 service_name,
1244 STRERROR (errno)); 1175 strerror (errno));
1245 GNUNET_free (unixpath); 1176 GNUNET_free (unixpath);
1246 unixpath = NULL; 1177 unixpath = NULL;
1247 } 1178 }
1248 else 1179 else
1249 { 1180 {
1250 GNUNET_break (GNUNET_OK == 1181 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1251 GNUNET_NETWORK_socket_close (desc));
1252 desc = NULL; 1182 desc = NULL;
1253 } 1183 }
1254 } 1184 }
@@ -1265,14 +1195,9 @@ get_server_addresses (const char *service_name,
1265 } 1195 }
1266 if (0 == port) 1196 if (0 == port)
1267 { 1197 {
1268 saddrs = GNUNET_new_array (2, 1198 saddrs = GNUNET_new_array (2, struct sockaddr *);
1269 struct sockaddr *); 1199 saddrlens = GNUNET_new_array (2, socklen_t);
1270 saddrlens = GNUNET_new_array (2, 1200 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1271 socklen_t);
1272 add_unixpath (saddrs,
1273 saddrlens,
1274 unixpath,
1275 abstract);
1276 GNUNET_free_non_null (unixpath); 1201 GNUNET_free_non_null (unixpath);
1277 GNUNET_free_non_null (hostname); 1202 GNUNET_free_non_null (hostname);
1278 *addrs = saddrs; 1203 *addrs = saddrs;
@@ -1286,16 +1211,11 @@ get_server_addresses (const char *service_name,
1286 "Resolving `%s' since that is where `%s' will bind to.\n", 1211 "Resolving `%s' since that is where `%s' will bind to.\n",
1287 hostname, 1212 hostname,
1288 service_name); 1213 service_name);
1289 memset (&hints, 1214 memset (&hints, 0, sizeof (struct addrinfo));
1290 0,
1291 sizeof (struct addrinfo));
1292 if (disablev6) 1215 if (disablev6)
1293 hints.ai_family = AF_INET; 1216 hints.ai_family = AF_INET;
1294 hints.ai_protocol = IPPROTO_TCP; 1217 hints.ai_protocol = IPPROTO_TCP;
1295 if ((0 != (ret = getaddrinfo (hostname, 1218 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1296 NULL,
1297 &hints,
1298 &res))) ||
1299 (NULL == res)) 1219 (NULL == res))
1300 { 1220 {
1301 LOG (GNUNET_ERROR_TYPE_ERROR, 1221 LOG (GNUNET_ERROR_TYPE_ERROR,
@@ -1311,8 +1231,7 @@ get_server_addresses (const char *service_name,
1311 while (NULL != (pos = next)) 1231 while (NULL != (pos = next))
1312 { 1232 {
1313 next = pos->ai_next; 1233 next = pos->ai_next;
1314 if ( (disablev6) && 1234 if ((disablev6) && (pos->ai_family == AF_INET6))
1315 (pos->ai_family == AF_INET6) )
1316 continue; 1235 continue;
1317 i++; 1236 i++;
1318 } 1237 }
@@ -1330,45 +1249,34 @@ get_server_addresses (const char *service_name,
1330 resi = i; 1249 resi = i;
1331 if (NULL != unixpath) 1250 if (NULL != unixpath)
1332 resi++; 1251 resi++;
1333 saddrs = GNUNET_new_array (resi + 1, 1252 saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
1334 struct sockaddr *); 1253 saddrlens = GNUNET_new_array (resi + 1, socklen_t);
1335 saddrlens = GNUNET_new_array (resi + 1,
1336 socklen_t);
1337 i = 0; 1254 i = 0;
1338 if (NULL != unixpath) 1255 if (NULL != unixpath)
1339 { 1256 {
1340 add_unixpath (saddrs, 1257 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1341 saddrlens,
1342 unixpath,
1343 abstract);
1344 i++; 1258 i++;
1345 } 1259 }
1346 next = res; 1260 next = res;
1347 while (NULL != (pos = next)) 1261 while (NULL != (pos = next))
1348 { 1262 {
1349 next = pos->ai_next; 1263 next = pos->ai_next;
1350 if ( (disablev6) && 1264 if ((disablev6) && (AF_INET6 == pos->ai_family))
1351 (AF_INET6 == pos->ai_family) )
1352 continue; 1265 continue;
1353 if ( (IPPROTO_TCP != pos->ai_protocol) && 1266 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1354 (0 != pos->ai_protocol) ) 1267 continue; /* not TCP */
1355 continue; /* not TCP */ 1268 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1356 if ( (SOCK_STREAM != pos->ai_socktype) && 1269 continue; /* huh? */
1357 (0 != pos->ai_socktype) )
1358 continue; /* huh? */
1359 LOG (GNUNET_ERROR_TYPE_DEBUG, 1270 LOG (GNUNET_ERROR_TYPE_DEBUG,
1360 "Service `%s' will bind to `%s'\n", 1271 "Service `%s' will bind to `%s'\n",
1361 service_name, 1272 service_name,
1362 GNUNET_a2s (pos->ai_addr, 1273 GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1363 pos->ai_addrlen));
1364 if (AF_INET == pos->ai_family) 1274 if (AF_INET == pos->ai_family)
1365 { 1275 {
1366 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen); 1276 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1367 saddrlens[i] = pos->ai_addrlen; 1277 saddrlens[i] = pos->ai_addrlen;
1368 saddrs[i] = GNUNET_malloc (saddrlens[i]); 1278 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1369 GNUNET_memcpy (saddrs[i], 1279 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1370 pos->ai_addr,
1371 saddrlens[i]);
1372 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); 1280 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1373 } 1281 }
1374 else 1282 else
@@ -1377,9 +1285,7 @@ get_server_addresses (const char *service_name,
1377 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen); 1285 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1378 saddrlens[i] = pos->ai_addrlen; 1286 saddrlens[i] = pos->ai_addrlen;
1379 saddrs[i] = GNUNET_malloc (saddrlens[i]); 1287 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1380 GNUNET_memcpy (saddrs[i], 1288 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1381 pos->ai_addr,
1382 saddrlens[i]);
1383 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port); 1289 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1384 } 1290 }
1385 i++; 1291 i++;
@@ -1398,16 +1304,11 @@ get_server_addresses (const char *service_name,
1398 if (NULL != unixpath) 1304 if (NULL != unixpath)
1399 resi++; 1305 resi++;
1400 i = 0; 1306 i = 0;
1401 saddrs = GNUNET_new_array (resi + 1, 1307 saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
1402 struct sockaddr *); 1308 saddrlens = GNUNET_new_array (resi + 1, socklen_t);
1403 saddrlens = GNUNET_new_array (resi + 1,
1404 socklen_t);
1405 if (NULL != unixpath) 1309 if (NULL != unixpath)
1406 { 1310 {
1407 add_unixpath (saddrs, 1311 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1408 saddrlens,
1409 unixpath,
1410 abstract);
1411 i++; 1312 i++;
1412 } 1313 }
1413 saddrlens[i] = sizeof (struct sockaddr_in); 1314 saddrlens[i] = sizeof (struct sockaddr_in);
@@ -1424,17 +1325,12 @@ get_server_addresses (const char *service_name,
1424 resi = 2; 1325 resi = 2;
1425 if (NULL != unixpath) 1326 if (NULL != unixpath)
1426 resi++; 1327 resi++;
1427 saddrs = GNUNET_new_array (resi + 1, 1328 saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
1428 struct sockaddr *); 1329 saddrlens = GNUNET_new_array (resi + 1, socklen_t);
1429 saddrlens = GNUNET_new_array (resi + 1,
1430 socklen_t);
1431 i = 0; 1330 i = 0;
1432 if (NULL != unixpath) 1331 if (NULL != unixpath)
1433 { 1332 {
1434 add_unixpath (saddrs, 1333 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1435 saddrlens,
1436 unixpath,
1437 abstract);
1438 i++; 1334 i++;
1439 } 1335 }
1440 saddrlens[i] = sizeof (struct sockaddr_in6); 1336 saddrlens[i] = sizeof (struct sockaddr_in6);
@@ -1480,18 +1376,14 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
1480 HANDLE lsocks_pipe; 1376 HANDLE lsocks_pipe;
1481 1377
1482 env_buf = getenv ("GNUNET_OS_READ_LSOCKS"); 1378 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1483 if ( (NULL == env_buf) || 1379 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1484 (strlen (env_buf) <= 0) )
1485 return NULL; 1380 return NULL;
1486 /* Using W32 API directly here, because this pipe will 1381 /* Using W32 API directly here, because this pipe will
1487 * never be used outside of this function, and it's just too much of a bother 1382 * never be used outside of this function, and it's just too much of a bother
1488 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks) 1383 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1489 */ 1384 */
1490 lsocks_pipe = (HANDLE) strtoul (env_buf, 1385 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1491 NULL, 1386 if ((0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1492 10);
1493 if ( (0 == lsocks_pipe) ||
1494 (INVALID_HANDLE_VALUE == lsocks_pipe))
1495 return NULL; 1387 return NULL;
1496 fail = 1; 1388 fail = 1;
1497 do 1389 do
@@ -1500,17 +1392,10 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
1500 int fail2; 1392 int fail2;
1501 DWORD rd; 1393 DWORD rd;
1502 1394
1503 ret = ReadFile (lsocks_pipe, 1395 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1504 &count, 1396 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1505 sizeof (count),
1506 &rd,
1507 NULL);
1508 if ( (0 == ret) ||
1509 (sizeof (count) != rd) ||
1510 (0 == count) )
1511 break; 1397 break;
1512 lsocks = GNUNET_new_array (count + 1, 1398 lsocks = GNUNET_new_array (count + 1, struct GNUNET_NETWORK_Handle *);
1513 struct GNUNET_NETWORK_Handle *);
1514 1399
1515 fail2 = 1; 1400 fail2 = 1;
1516 for (i = 0; i < count; i++) 1401 for (i = 0; i < count; i++)
@@ -1519,22 +1404,11 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
1519 uint64_t size; 1404 uint64_t size;
1520 SOCKET s; 1405 SOCKET s;
1521 1406
1522 ret = ReadFile (lsocks_pipe, 1407 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1523 &size, 1408 if ((0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size))
1524 sizeof (size),
1525 &rd,
1526 NULL);
1527 if ( (0 == ret) ||
1528 (sizeof (size) != rd) ||
1529 (sizeof (pi) != size) )
1530 break; 1409 break;
1531 ret = ReadFile (lsocks_pipe, 1410 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1532 &pi, 1411 if ((0 == ret) || (sizeof (pi) != rd))
1533 sizeof (pi),
1534 &rd,
1535 NULL);
1536 if ( (0 == ret) ||
1537 (sizeof (pi) != rd))
1538 break; 1412 break;
1539 s = WSASocketA (pi.iAddressFamily, 1413 s = WSASocketA (pi.iAddressFamily,
1540 pi.iSocketType, 1414 pi.iSocketType,
@@ -1552,8 +1426,7 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
1552 break; 1426 break;
1553 lsocks[count] = NULL; 1427 lsocks[count] = NULL;
1554 fail = 0; 1428 fail = 0;
1555 } 1429 } while (fail);
1556 while (fail);
1557 CloseHandle (lsocks_pipe); 1430 CloseHandle (lsocks_pipe);
1558 1431
1559 if (fail) 1432 if (fail)
@@ -1561,8 +1434,7 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
1561 LOG (GNUNET_ERROR_TYPE_ERROR, 1434 LOG (GNUNET_ERROR_TYPE_ERROR,
1562 _ ("Could not access a pre-bound socket, will try to bind myself\n")); 1435 _ ("Could not access a pre-bound socket, will try to bind myself\n"));
1563 for (i = 0; (i < count) && (NULL != lsocks[i]); i++) 1436 for (i = 0; (i < count) && (NULL != lsocks[i]); i++)
1564 GNUNET_break (GNUNET_OK == 1437 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (lsocks[i]));
1565 GNUNET_NETWORK_socket_close (lsocks[i]));
1566 GNUNET_free (lsocks); 1438 GNUNET_free (lsocks);
1567 return NULL; 1439 return NULL;
1568 } 1440 }
@@ -1579,8 +1451,7 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
1579 * @return NULL on error, otherwise the listen socket 1451 * @return NULL on error, otherwise the listen socket
1580 */ 1452 */
1581static struct GNUNET_NETWORK_Handle * 1453static struct GNUNET_NETWORK_Handle *
1582open_listen_socket (const struct sockaddr *server_addr, 1454open_listen_socket (const struct sockaddr *server_addr, socklen_t socklen)
1583 socklen_t socklen)
1584{ 1455{
1585 struct GNUNET_NETWORK_Handle *sock; 1456 struct GNUNET_NETWORK_Handle *sock;
1586 uint16_t port; 1457 uint16_t port;
@@ -1602,20 +1473,15 @@ open_listen_socket (const struct sockaddr *server_addr,
1602 port = 0; 1473 port = 0;
1603 break; 1474 break;
1604 } 1475 }
1605 sock = GNUNET_NETWORK_socket_create (server_addr->sa_family, 1476 sock = GNUNET_NETWORK_socket_create (server_addr->sa_family, SOCK_STREAM, 0);
1606 SOCK_STREAM,
1607 0);
1608 if (NULL == sock) 1477 if (NULL == sock)
1609 { 1478 {
1610 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1479 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
1611 "socket");
1612 errno = 0; 1480 errno = 0;
1613 return NULL; 1481 return NULL;
1614 } 1482 }
1615 /* bind the socket */ 1483 /* bind the socket */
1616 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, 1484 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, server_addr, socklen))
1617 server_addr,
1618 socklen))
1619 { 1485 {
1620 eno = errno; 1486 eno = errno;
1621 if (EADDRINUSE != errno) 1487 if (EADDRINUSE != errno)
@@ -1631,8 +1497,7 @@ open_listen_socket (const struct sockaddr *server_addr,
1631 port, 1497 port,
1632 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6"); 1498 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1633 else 1499 else
1634 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1500 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
1635 "bind");
1636 eno = 0; 1501 eno = 0;
1637 } 1502 }
1638 else 1503 else
@@ -1640,7 +1505,8 @@ open_listen_socket (const struct sockaddr *server_addr,
1640 if (0 != port) 1505 if (0 != port)
1641 LOG (GNUNET_ERROR_TYPE_WARNING, 1506 LOG (GNUNET_ERROR_TYPE_WARNING,
1642 _ ("`%s' failed for port %d (%s): address already in use\n"), 1507 _ ("`%s' failed for port %d (%s): address already in use\n"),
1643 "bind", port, 1508 "bind",
1509 port,
1644 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6"); 1510 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1645 else if (AF_UNIX == server_addr->sa_family) 1511 else if (AF_UNIX == server_addr->sa_family)
1646 { 1512 {
@@ -1650,18 +1516,14 @@ open_listen_socket (const struct sockaddr *server_addr,
1650 GNUNET_a2s (server_addr, socklen)); 1516 GNUNET_a2s (server_addr, socklen));
1651 } 1517 }
1652 } 1518 }
1653 GNUNET_break (GNUNET_OK == 1519 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1654 GNUNET_NETWORK_socket_close (sock));
1655 errno = eno; 1520 errno = eno;
1656 return NULL; 1521 return NULL;
1657 } 1522 }
1658 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 1523 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
1659 5))
1660 { 1524 {
1661 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1525 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
1662 "listen"); 1526 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1663 GNUNET_break (GNUNET_OK ==
1664 GNUNET_NETWORK_socket_close (sock));
1665 errno = 0; 1527 errno = 0;
1666 return NULL; 1528 return NULL;
1667 } 1529 }
@@ -1701,16 +1563,12 @@ setup_service (struct GNUNET_SERVICE_Handle *sh)
1701 char dummy[2]; 1563 char dummy[2];
1702#endif 1564#endif
1703 1565
1704 if (GNUNET_CONFIGURATION_have_value 1566 if (GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, "TOLERANT"))
1705 (sh->cfg,
1706 sh->service_name,
1707 "TOLERANT"))
1708 { 1567 {
1709 if (GNUNET_SYSERR == 1568 if (GNUNET_SYSERR ==
1710 (tolerant = 1569 (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1711 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg, 1570 sh->service_name,
1712 sh->service_name, 1571 "TOLERANT")))
1713 "TOLERANT")))
1714 { 1572 {
1715 LOG (GNUNET_ERROR_TYPE_ERROR, 1573 LOG (GNUNET_ERROR_TYPE_ERROR,
1716 _ ("Specified value for `%s' of service `%s' is invalid\n"), 1574 _ ("Specified value for `%s' of service `%s' is invalid\n"),
@@ -1725,25 +1583,16 @@ setup_service (struct GNUNET_SERVICE_Handle *sh)
1725 lsocks = NULL; 1583 lsocks = NULL;
1726#ifndef MINGW 1584#ifndef MINGW
1727 errno = 0; 1585 errno = 0;
1728 if ( (NULL != (nfds = getenv ("LISTEN_FDS"))) && 1586 if ((NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1729 (1 == SSCANF (nfds, 1587 (1 == sscanf (nfds, "%u%1s", &cnt, dummy)) && (cnt > 0) &&
1730 "%u%1s", 1588 (cnt < FD_SETSIZE) && (cnt + 4 < FD_SETSIZE))
1731 &cnt,
1732 dummy)) &&
1733 (cnt > 0) &&
1734 (cnt < FD_SETSIZE) &&
1735 (cnt + 4 < FD_SETSIZE) )
1736 { 1589 {
1737 lsocks = GNUNET_new_array (cnt + 1, 1590 lsocks = GNUNET_new_array (cnt + 1, struct GNUNET_NETWORK_Handle *);
1738 struct GNUNET_NETWORK_Handle *);
1739 while (0 < cnt--) 1591 while (0 < cnt--)
1740 { 1592 {
1741 flags = fcntl (3 + cnt, 1593 flags = fcntl (3 + cnt, F_GETFD);
1742 F_GETFD); 1594 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1743 if ( (flags < 0) || 1595 (NULL == (lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1744 (0 != (flags & FD_CLOEXEC)) ||
1745 (NULL ==
1746 (lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1747 { 1596 {
1748 LOG (GNUNET_ERROR_TYPE_ERROR, 1597 LOG (GNUNET_ERROR_TYPE_ERROR,
1749 _ ( 1598 _ (
@@ -1780,9 +1629,7 @@ setup_service (struct GNUNET_SERVICE_Handle *sh)
1780 slc = GNUNET_new (struct ServiceListenContext); 1629 slc = GNUNET_new (struct ServiceListenContext);
1781 slc->sh = sh; 1630 slc->sh = sh;
1782 slc->listen_socket = *ls; 1631 slc->listen_socket = *ls;
1783 GNUNET_CONTAINER_DLL_insert (sh->slc_head, 1632 GNUNET_CONTAINER_DLL_insert (sh->slc_head, sh->slc_tail, slc);
1784 sh->slc_tail,
1785 slc);
1786 } 1633 }
1787 GNUNET_free (lsocks); 1634 GNUNET_free (lsocks);
1788 } 1635 }
@@ -1792,10 +1639,7 @@ setup_service (struct GNUNET_SERVICE_Handle *sh)
1792 socklen_t *addrlens; 1639 socklen_t *addrlens;
1793 int num; 1640 int num;
1794 1641
1795 num = get_server_addresses (sh->service_name, 1642 num = get_server_addresses (sh->service_name, sh->cfg, &addrs, &addrlens);
1796 sh->cfg,
1797 &addrs,
1798 &addrlens);
1799 if (GNUNET_SYSERR == num) 1643 if (GNUNET_SYSERR == num)
1800 return GNUNET_SYSERR; 1644 return GNUNET_SYSERR;
1801 1645
@@ -1805,54 +1649,40 @@ setup_service (struct GNUNET_SERVICE_Handle *sh)
1805 1649
1806 slc = GNUNET_new (struct ServiceListenContext); 1650 slc = GNUNET_new (struct ServiceListenContext);
1807 slc->sh = sh; 1651 slc->sh = sh;
1808 slc->listen_socket = open_listen_socket (addrs[i], 1652 slc->listen_socket = open_listen_socket (addrs[i], addrlens[i]);
1809 addrlens[i]);
1810 GNUNET_free (addrs[i]); 1653 GNUNET_free (addrs[i]);
1811 if (NULL == slc->listen_socket) 1654 if (NULL == slc->listen_socket)
1812 { 1655 {
1813 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 1656 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1814 "bind");
1815 GNUNET_free (slc); 1657 GNUNET_free (slc);
1816 continue; 1658 continue;
1817 } 1659 }
1818 GNUNET_CONTAINER_DLL_insert (sh->slc_head, 1660 GNUNET_CONTAINER_DLL_insert (sh->slc_head, sh->slc_tail, slc);
1819 sh->slc_tail,
1820 slc);
1821 } 1661 }
1822 GNUNET_free_non_null (addrlens); 1662 GNUNET_free_non_null (addrlens);
1823 GNUNET_free_non_null (addrs); 1663 GNUNET_free_non_null (addrs);
1824 if ( (0 != num) && 1664 if ((0 != num) && (NULL == sh->slc_head))
1825 (NULL == sh->slc_head) )
1826 { 1665 {
1827 /* All attempts to bind failed, hard failure */ 1666 /* All attempts to bind failed, hard failure */
1828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1667 GNUNET_log (
1829 _ ( 1668 GNUNET_ERROR_TYPE_ERROR,
1830 "Could not bind to any of the ports I was supposed to, refusing to run!\n")); 1669 _ (
1670 "Could not bind to any of the ports I was supposed to, refusing to run!\n"));
1831 return GNUNET_SYSERR; 1671 return GNUNET_SYSERR;
1832 } 1672 }
1833 } 1673 }
1834 1674
1835 sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES; 1675 sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1836 sh->match_uid 1676 sh->match_uid = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1837 = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg, 1677 sh->service_name,
1838 sh->service_name, 1678 "UNIX_MATCH_UID");
1839 "UNIX_MATCH_UID"); 1679 sh->match_gid = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1840 sh->match_gid 1680 sh->service_name,
1841 = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg, 1681 "UNIX_MATCH_GID");
1842 sh->service_name, 1682 process_acl4 (&sh->v4_denied, sh, "REJECT_FROM");
1843 "UNIX_MATCH_GID"); 1683 process_acl4 (&sh->v4_allowed, sh, "ACCEPT_FROM");
1844 process_acl4 (&sh->v4_denied, 1684 process_acl6 (&sh->v6_denied, sh, "REJECT_FROM6");
1845 sh, 1685 process_acl6 (&sh->v6_allowed, sh, "ACCEPT_FROM6");
1846 "REJECT_FROM");
1847 process_acl4 (&sh->v4_allowed,
1848 sh,
1849 "ACCEPT_FROM");
1850 process_acl6 (&sh->v6_denied,
1851 sh,
1852 "REJECT_FROM6");
1853 process_acl6 (&sh->v6_allowed,
1854 sh,
1855 "ACCEPT_FROM6");
1856 return GNUNET_OK; 1686 return GNUNET_OK;
1857} 1687}
1858 1688
@@ -1869,11 +1699,10 @@ get_user_name (struct GNUNET_SERVICE_Handle *sh)
1869{ 1699{
1870 char *un; 1700 char *un;
1871 1701
1872 if (GNUNET_OK != 1702 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1873 GNUNET_CONFIGURATION_get_value_filename (sh->cfg, 1703 sh->service_name,
1874 sh->service_name, 1704 "USERNAME",
1875 "USERNAME", 1705 &un))
1876 &un))
1877 return NULL; 1706 return NULL;
1878 return un; 1707 return un;
1879} 1708}
@@ -1891,7 +1720,7 @@ set_user_id (struct GNUNET_SERVICE_Handle *sh)
1891 char *user; 1720 char *user;
1892 1721
1893 if (NULL == (user = get_user_name (sh))) 1722 if (NULL == (user = get_user_name (sh)))
1894 return GNUNET_OK; /* keep */ 1723 return GNUNET_OK; /* keep */
1895#ifndef MINGW 1724#ifndef MINGW
1896 struct passwd *pws; 1725 struct passwd *pws;
1897 1726
@@ -1902,28 +1731,23 @@ set_user_id (struct GNUNET_SERVICE_Handle *sh)
1902 LOG (GNUNET_ERROR_TYPE_ERROR, 1731 LOG (GNUNET_ERROR_TYPE_ERROR,
1903 _ ("Cannot obtain information about user `%s': %s\n"), 1732 _ ("Cannot obtain information about user `%s': %s\n"),
1904 user, 1733 user,
1905 errno == 0 ? _ ("No such user") : STRERROR (errno)); 1734 errno == 0 ? _ ("No such user") : strerror (errno));
1906 GNUNET_free (user); 1735 GNUNET_free (user);
1907 return GNUNET_SYSERR; 1736 return GNUNET_SYSERR;
1908 } 1737 }
1909 if ( (0 != setgid (pws->pw_gid)) || 1738 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1910 (0 != setegid (pws->pw_gid)) ||
1911#if HAVE_INITGROUPS 1739#if HAVE_INITGROUPS
1912 (0 != initgroups (user, 1740 (0 != initgroups (user, pws->pw_gid)) ||
1913 pws->pw_gid)) ||
1914#endif 1741#endif
1915 (0 != setuid (pws->pw_uid)) || 1742 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1916 (0 != seteuid (pws->pw_uid)))
1917 { 1743 {
1918 if ((0 != setregid (pws->pw_gid, 1744 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1919 pws->pw_gid)) || 1745 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1920 (0 != setreuid (pws->pw_uid,
1921 pws->pw_uid)))
1922 { 1746 {
1923 LOG (GNUNET_ERROR_TYPE_ERROR, 1747 LOG (GNUNET_ERROR_TYPE_ERROR,
1924 _ ("Cannot change user/group to `%s': %s\n"), 1748 _ ("Cannot change user/group to `%s': %s\n"),
1925 user, 1749 user,
1926 STRERROR (errno)); 1750 strerror (errno));
1927 GNUNET_free (user); 1751 GNUNET_free (user);
1928 return GNUNET_SYSERR; 1752 return GNUNET_SYSERR;
1929 } 1753 }
@@ -1946,11 +1770,10 @@ get_pid_file_name (struct GNUNET_SERVICE_Handle *sh)
1946{ 1770{
1947 char *pif; 1771 char *pif;
1948 1772
1949 if (GNUNET_OK != 1773 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1950 GNUNET_CONFIGURATION_get_value_filename (sh->cfg, 1774 sh->service_name,
1951 sh->service_name, 1775 "PIDFILE",
1952 "PIDFILE", 1776 &pif))
1953 &pif))
1954 return NULL; 1777 return NULL;
1955 return pif; 1778 return pif;
1956} 1779}
@@ -1967,11 +1790,9 @@ pid_file_delete (struct GNUNET_SERVICE_Handle *sh)
1967 char *pif = get_pid_file_name (sh); 1790 char *pif = get_pid_file_name (sh);
1968 1791
1969 if (NULL == pif) 1792 if (NULL == pif)
1970 return; /* no PID file */ 1793 return; /* no PID file */
1971 if (0 != UNLINK (pif)) 1794 if (0 != unlink (pif))
1972 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 1795 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1973 "unlink",
1974 pif);
1975 GNUNET_free (pif); 1796 GNUNET_free (pif);
1976} 1797}
1977 1798
@@ -1990,17 +1811,15 @@ detach_terminal (struct GNUNET_SERVICE_Handle *sh)
1990 int nullfd; 1811 int nullfd;
1991 int filedes[2]; 1812 int filedes[2];
1992 1813
1993 if (0 != PIPE (filedes)) 1814 if (0 != pipe (filedes))
1994 { 1815 {
1995 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1816 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1996 "pipe");
1997 return GNUNET_SYSERR; 1817 return GNUNET_SYSERR;
1998 } 1818 }
1999 pid = fork (); 1819 pid = fork ();
2000 if (pid < 0) 1820 if (pid < 0)
2001 { 1821 {
2002 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1822 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
2003 "fork");
2004 return GNUNET_SYSERR; 1823 return GNUNET_SYSERR;
2005 } 1824 }
2006 if (0 != pid) 1825 if (0 != pid)
@@ -2008,13 +1827,10 @@ detach_terminal (struct GNUNET_SERVICE_Handle *sh)
2008 /* Parent */ 1827 /* Parent */
2009 char c; 1828 char c;
2010 1829
2011 GNUNET_break (0 == CLOSE (filedes[1])); 1830 GNUNET_break (0 == close (filedes[1]));
2012 c = 'X'; 1831 c = 'X';
2013 if (1 != READ (filedes[0], 1832 if (1 != read (filedes[0], &c, sizeof (char)))
2014 &c, 1833 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
2015 sizeof (char)))
2016 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2017 "read");
2018 fflush (stdout); 1834 fflush (stdout);
2019 switch (c) 1835 switch (c)
2020 { 1836 {
@@ -2033,30 +1849,26 @@ detach_terminal (struct GNUNET_SERVICE_Handle *sh)
2033 _ ("Service process failed to report status\n")); 1849 _ ("Service process failed to report status\n"));
2034 break; 1850 break;
2035 } 1851 }
2036 exit (1); /* child reported error */ 1852 exit (1); /* child reported error */
2037 } 1853 }
2038 GNUNET_break (0 == CLOSE (0)); 1854 GNUNET_break (0 == close (0));
2039 GNUNET_break (0 == CLOSE (1)); 1855 GNUNET_break (0 == close (1));
2040 GNUNET_break (0 == CLOSE (filedes[0])); 1856 GNUNET_break (0 == close (filedes[0]));
2041 nullfd = OPEN ("/dev/null", 1857 nullfd = open ("/dev/null", O_RDWR | O_APPEND);
2042 O_RDWR | O_APPEND);
2043 if (nullfd < 0) 1858 if (nullfd < 0)
2044 return GNUNET_SYSERR; 1859 return GNUNET_SYSERR;
2045 /* set stdin/stdout to /dev/null */ 1860 /* set stdin/stdout to /dev/null */
2046 if ( (dup2 (nullfd, 0) < 0) || 1861 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
2047 (dup2 (nullfd, 1) < 0) )
2048 { 1862 {
2049 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1863 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
2050 "dup2"); 1864 (void) close (nullfd);
2051 (void) CLOSE (nullfd);
2052 return GNUNET_SYSERR; 1865 return GNUNET_SYSERR;
2053 } 1866 }
2054 (void) CLOSE (nullfd); 1867 (void) close (nullfd);
2055 /* Detach from controlling terminal */ 1868 /* Detach from controlling terminal */
2056 pid = setsid (); 1869 pid = setsid ();
2057 if (-1 == pid) 1870 if (-1 == pid)
2058 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1871 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
2059 "setsid");
2060 sh->ready_confirm_fd = filedes[1]; 1872 sh->ready_confirm_fd = filedes[1];
2061#else 1873#else
2062 /* FIXME: we probably need to do something else 1874 /* FIXME: we probably need to do something else
@@ -2084,9 +1896,7 @@ teardown_service (struct GNUNET_SERVICE_Handle *sh)
2084 GNUNET_free_non_null (sh->v6_allowed); 1896 GNUNET_free_non_null (sh->v6_allowed);
2085 while (NULL != (slc = sh->slc_head)) 1897 while (NULL != (slc = sh->slc_head))
2086 { 1898 {
2087 GNUNET_CONTAINER_DLL_remove (sh->slc_head, 1899 GNUNET_CONTAINER_DLL_remove (sh->slc_head, sh->slc_tail, slc);
2088 sh->slc_tail,
2089 slc);
2090 if (NULL != slc->listen_task) 1900 if (NULL != slc->listen_task)
2091 GNUNET_SCHEDULER_cancel (slc->listen_task); 1901 GNUNET_SCHEDULER_cancel (slc->listen_task);
2092 GNUNET_break (GNUNET_OK == 1902 GNUNET_break (GNUNET_OK ==
@@ -2103,8 +1913,7 @@ teardown_service (struct GNUNET_SERVICE_Handle *sh)
2103 * @param msg AGPL request 1913 * @param msg AGPL request
2104 */ 1914 */
2105static void 1915static void
2106return_agpl (void *cls, 1916return_agpl (void *cls, const struct GNUNET_MessageHeader *msg)
2107 const struct GNUNET_MessageHeader *msg)
2108{ 1917{
2109 struct GNUNET_SERVICE_Client *client = cls; 1918 struct GNUNET_SERVICE_Client *client = cls;
2110 struct GNUNET_MQ_Handle *mq; 1919 struct GNUNET_MQ_Handle *mq;
@@ -2114,15 +1923,10 @@ return_agpl (void *cls,
2114 1923
2115 (void) msg; 1924 (void) msg;
2116 slen = strlen (GNUNET_AGPL_URL) + 1; 1925 slen = strlen (GNUNET_AGPL_URL) + 1;
2117 env = GNUNET_MQ_msg_extra (res, 1926 env = GNUNET_MQ_msg_extra (res, GNUNET_MESSAGE_TYPE_RESPONSE_AGPL, slen);
2118 GNUNET_MESSAGE_TYPE_RESPONSE_AGPL, 1927 memcpy (&res[1], GNUNET_AGPL_URL, slen);
2119 slen);
2120 memcpy (&res[1],
2121 GNUNET_AGPL_URL,
2122 slen);
2123 mq = GNUNET_SERVICE_client_get_mq (client); 1928 mq = GNUNET_SERVICE_client_get_mq (client);
2124 GNUNET_MQ_send (mq, 1929 GNUNET_MQ_send (mq, env);
2125 env);
2126 GNUNET_SERVICE_client_continue (client); 1930 GNUNET_SERVICE_client_continue (client);
2127} 1931}
2128 1932
@@ -2179,17 +1983,14 @@ GNUNET_SERVICE_start (const char *service_name,
2179 sh->connect_cb = connect_cb; 1983 sh->connect_cb = connect_cb;
2180 sh->disconnect_cb = disconnect_cb; 1984 sh->disconnect_cb = disconnect_cb;
2181 sh->cb_cls = cls; 1985 sh->cb_cls = cls;
2182 sh->handlers = GNUNET_MQ_copy_handlers2 (handlers, 1986 sh->handlers = GNUNET_MQ_copy_handlers2 (handlers, &return_agpl, NULL);
2183 &return_agpl,
2184 NULL);
2185 if (GNUNET_OK != setup_service (sh)) 1987 if (GNUNET_OK != setup_service (sh))
2186 { 1988 {
2187 GNUNET_free_non_null (sh->handlers); 1989 GNUNET_free_non_null (sh->handlers);
2188 GNUNET_free (sh); 1990 GNUNET_free (sh);
2189 return NULL; 1991 return NULL;
2190 } 1992 }
2191 do_resume (sh, 1993 do_resume (sh, SUSPEND_STATE_NONE);
2192 SUSPEND_STATE_NONE);
2193 return sh; 1994 return sh;
2194} 1995}
2195 1996
@@ -2279,24 +2080,21 @@ GNUNET_SERVICE_run_ (int argc,
2279 int ret; 2080 int ret;
2280 int err; 2081 int err;
2281 2082
2282 struct GNUNET_GETOPT_CommandLineOption service_options[] = { 2083 struct GNUNET_GETOPT_CommandLineOption service_options[] =
2283 GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename), 2084 {GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
2284 GNUNET_GETOPT_option_flag ('d', 2085 GNUNET_GETOPT_option_flag ('d',
2285 "daemonize", 2086 "daemonize",
2286 gettext_noop ( 2087 gettext_noop (
2287 "do daemonize (detach from terminal)"), 2088 "do daemonize (detach from terminal)"),
2288 &do_daemonize), 2089 &do_daemonize),
2289 GNUNET_GETOPT_option_help (NULL), 2090 GNUNET_GETOPT_option_help (NULL),
2290 GNUNET_GETOPT_option_loglevel (&loglev), 2091 GNUNET_GETOPT_option_loglevel (&loglev),
2291 GNUNET_GETOPT_option_logfile (&logfile), 2092 GNUNET_GETOPT_option_logfile (&logfile),
2292 GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION), 2093 GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
2293 GNUNET_GETOPT_OPTION_END 2094 GNUNET_GETOPT_OPTION_END};
2294 };
2295 2095
2296 err = 1; 2096 err = 1;
2297 memset (&sh, 2097 memset (&sh, 0, sizeof (sh));
2298 0,
2299 sizeof (sh));
2300 xdg = getenv ("XDG_CONFIG_HOME"); 2098 xdg = getenv ("XDG_CONFIG_HOME");
2301 if (NULL != xdg) 2099 if (NULL != xdg)
2302 GNUNET_asprintf (&cfg_filename, 2100 GNUNET_asprintf (&cfg_filename,
@@ -2305,8 +2103,8 @@ GNUNET_SERVICE_run_ (int argc,
2305 DIR_SEPARATOR_STR, 2103 DIR_SEPARATOR_STR,
2306 GNUNET_OS_project_data_get ()->config_file); 2104 GNUNET_OS_project_data_get ()->config_file);
2307 else 2105 else
2308 cfg_filename = GNUNET_strdup ( 2106 cfg_filename =
2309 GNUNET_OS_project_data_get ()->user_config_file); 2107 GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
2310 sh.ready_confirm_fd = -1; 2108 sh.ready_confirm_fd = -1;
2311 sh.options = options; 2109 sh.options = options;
2312 sh.cfg = cfg = GNUNET_CONFIGURATION_create (); 2110 sh.cfg = cfg = GNUNET_CONFIGURATION_create ();
@@ -2322,10 +2120,7 @@ GNUNET_SERVICE_run_ (int argc,
2322 logfile = NULL; 2120 logfile = NULL;
2323 opt_cfg_filename = NULL; 2121 opt_cfg_filename = NULL;
2324 do_daemonize = 0; 2122 do_daemonize = 0;
2325 ret = GNUNET_GETOPT_run (service_name, 2123 ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
2326 service_options,
2327 argc,
2328 argv);
2329 if (GNUNET_SYSERR == ret) 2124 if (GNUNET_SYSERR == ret)
2330 goto shutdown; 2125 goto shutdown;
2331 if (GNUNET_NO == ret) 2126 if (GNUNET_NO == ret)
@@ -2333,20 +2128,15 @@ GNUNET_SERVICE_run_ (int argc,
2333 err = 0; 2128 err = 0;
2334 goto shutdown; 2129 goto shutdown;
2335 } 2130 }
2336 if (GNUNET_OK != GNUNET_log_setup (service_name, 2131 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
2337 loglev,
2338 logfile))
2339 { 2132 {
2340 GNUNET_break (0); 2133 GNUNET_break (0);
2341 goto shutdown; 2134 goto shutdown;
2342 } 2135 }
2343 if (NULL != opt_cfg_filename) 2136 if (NULL != opt_cfg_filename)
2344 { 2137 {
2345 if ( (GNUNET_YES != 2138 if ((GNUNET_YES != GNUNET_DISK_file_test (opt_cfg_filename)) ||
2346 GNUNET_DISK_file_test (opt_cfg_filename)) || 2139 (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, opt_cfg_filename)))
2347 (GNUNET_SYSERR ==
2348 GNUNET_CONFIGURATION_load (cfg,
2349 opt_cfg_filename)) )
2350 { 2140 {
2351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2352 _ ("Malformed configuration file `%s', exit ...\n"), 2142 _ ("Malformed configuration file `%s', exit ...\n"),
@@ -2356,12 +2146,9 @@ GNUNET_SERVICE_run_ (int argc,
2356 } 2146 }
2357 else 2147 else
2358 { 2148 {
2359 if (GNUNET_YES == 2149 if (GNUNET_YES == GNUNET_DISK_file_test (cfg_filename))
2360 GNUNET_DISK_file_test (cfg_filename))
2361 { 2150 {
2362 if (GNUNET_SYSERR == 2151 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cfg_filename))
2363 GNUNET_CONFIGURATION_load (cfg,
2364 cfg_filename))
2365 { 2152 {
2366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2367 _ ("Malformed configuration file `%s', exit ...\n"), 2154 _ ("Malformed configuration file `%s', exit ...\n"),
@@ -2371,9 +2158,7 @@ GNUNET_SERVICE_run_ (int argc,
2371 } 2158 }
2372 else 2159 else
2373 { 2160 {
2374 if (GNUNET_SYSERR == 2161 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
2375 GNUNET_CONFIGURATION_load (cfg,
2376 NULL))
2377 { 2162 {
2378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2379 _ ("Malformed configuration, exit ...\n")); 2164 _ ("Malformed configuration, exit ...\n"));
@@ -2383,8 +2168,7 @@ GNUNET_SERVICE_run_ (int argc,
2383 } 2168 }
2384 if (GNUNET_OK != setup_service (&sh)) 2169 if (GNUNET_OK != setup_service (&sh))
2385 goto shutdown; 2170 goto shutdown;
2386 if ( (1 == do_daemonize) && 2171 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sh)))
2387 (GNUNET_OK != detach_terminal (&sh)) )
2388 { 2172 {
2389 GNUNET_break (0); 2173 GNUNET_break (0);
2390 goto shutdown; 2174 goto shutdown;
@@ -2395,64 +2179,51 @@ GNUNET_SERVICE_run_ (int argc,
2395 "Service `%s' runs with configuration from `%s'\n", 2179 "Service `%s' runs with configuration from `%s'\n",
2396 service_name, 2180 service_name,
2397 (NULL != opt_cfg_filename) ? opt_cfg_filename : cfg_filename); 2181 (NULL != opt_cfg_filename) ? opt_cfg_filename : cfg_filename);
2398 if ((GNUNET_OK == 2182 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (sh.cfg,
2399 GNUNET_CONFIGURATION_get_value_number (sh.cfg, 2183 "TESTING",
2400 "TESTING", 2184 "SKEW_OFFSET",
2401 "SKEW_OFFSET", 2185 &skew_offset)) &&
2402 &skew_offset)) && 2186 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (sh.cfg,
2403 (GNUNET_OK == 2187 "TESTING",
2404 GNUNET_CONFIGURATION_get_value_number (sh.cfg, 2188 "SKEW_VARIANCE",
2405 "TESTING", 2189 &skew_variance)))
2406 "SKEW_VARIANCE",
2407 &skew_variance)))
2408 { 2190 {
2409 clock_offset = skew_offset - skew_variance; 2191 clock_offset = skew_offset - skew_variance;
2410 GNUNET_TIME_set_offset (clock_offset); 2192 GNUNET_TIME_set_offset (clock_offset);
2411 LOG (GNUNET_ERROR_TYPE_DEBUG, 2193 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
2412 "Skewing clock by %dll ms\n",
2413 clock_offset);
2414 } 2194 }
2415 GNUNET_RESOLVER_connect (sh.cfg); 2195 GNUNET_RESOLVER_connect (sh.cfg);
2416 2196
2417 /* actually run service */ 2197 /* actually run service */
2418 err = 0; 2198 err = 0;
2419 GNUNET_SCHEDULER_run (&service_main, 2199 GNUNET_SCHEDULER_run (&service_main, &sh);
2420 &sh);
2421 /* shutdown */ 2200 /* shutdown */
2422 if (1 == do_daemonize) 2201 if (1 == do_daemonize)
2423 pid_file_delete (&sh); 2202 pid_file_delete (&sh);
2424 2203
2425 shutdown: 2204shutdown:
2426 if (-1 != sh.ready_confirm_fd) 2205 if (-1 != sh.ready_confirm_fd)
2427 { 2206 {
2428 if (1 != WRITE (sh.ready_confirm_fd, 2207 if (1 != write (sh.ready_confirm_fd, err ? "I" : "S", 1))
2429 err ? "I" : "S", 2208 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
2430 1)) 2209 GNUNET_break (0 == close (sh.ready_confirm_fd));
2431 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2432 "write");
2433 GNUNET_break (0 == CLOSE (sh.ready_confirm_fd));
2434 } 2210 }
2435#if HAVE_MALLINFO 2211#if HAVE_MALLINFO
2436 { 2212 {
2437 char *counter; 2213 char *counter;
2438 2214
2439 if ( (GNUNET_YES == 2215 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (sh.cfg,
2440 GNUNET_CONFIGURATION_have_value (sh.cfg, 2216 service_name,
2441 service_name, 2217 "GAUGER_HEAP")) &&
2442 "GAUGER_HEAP")) && 2218 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (sh.cfg,
2443 (GNUNET_OK == 2219 service_name,
2444 GNUNET_CONFIGURATION_get_value_string (sh.cfg, 2220 "GAUGER_HEAP",
2445 service_name, 2221 &counter)))
2446 "GAUGER_HEAP",
2447 &counter)) )
2448 { 2222 {
2449 struct mallinfo mi; 2223 struct mallinfo mi;
2450 2224
2451 mi = mallinfo (); 2225 mi = mallinfo ();
2452 GAUGER (service_name, 2226 GAUGER (service_name, counter, mi.usmblks, "blocks");
2453 counter,
2454 mi.usmblks,
2455 "blocks");
2456 GNUNET_free (counter); 2227 GNUNET_free (counter);
2457 } 2228 }
2458 } 2229 }
@@ -2479,8 +2250,7 @@ GNUNET_SERVICE_run_ (int argc,
2479void 2250void
2480GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh) 2251GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
2481{ 2252{
2482 do_suspend (sh, 2253 do_suspend (sh, SUSPEND_STATE_APP);
2483 SUSPEND_STATE_APP);
2484} 2254}
2485 2255
2486 2256
@@ -2492,8 +2262,7 @@ GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
2492void 2262void
2493GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh) 2263GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
2494{ 2264{
2495 do_resume (sh, 2265 do_resume (sh, SUSPEND_STATE_APP);
2496 SUSPEND_STATE_APP);
2497} 2266}
2498 2267
2499 2268
@@ -2511,8 +2280,7 @@ resume_client_receive (void *cls)
2511 2280
2512 c->recv_task = NULL; 2281 c->recv_task = NULL;
2513 /* first, check if there is still something in the buffer */ 2282 /* first, check if there is still something in the buffer */
2514 ret = GNUNET_MST_next (c->mst, 2283 ret = GNUNET_MST_next (c->mst, GNUNET_YES);
2515 GNUNET_YES);
2516 if (GNUNET_SYSERR == ret) 2284 if (GNUNET_SYSERR == ret)
2517 { 2285 {
2518 if (NULL == c->drop_task) 2286 if (NULL == c->drop_task)
@@ -2527,11 +2295,10 @@ resume_client_receive (void *cls)
2527 /* need to receive more data from the network first */ 2295 /* need to receive more data from the network first */
2528 if (NULL != c->recv_task) 2296 if (NULL != c->recv_task)
2529 return; 2297 return;
2530 c->recv_task 2298 c->recv_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2531 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 2299 c->sock,
2532 c->sock, 2300 &service_client_recv,
2533 &service_client_recv, 2301 c);
2534 c);
2535} 2302}
2536 2303
2537 2304
@@ -2553,9 +2320,7 @@ GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
2553 GNUNET_SCHEDULER_cancel (c->warn_task); 2320 GNUNET_SCHEDULER_cancel (c->warn_task);
2554 c->warn_task = NULL; 2321 c->warn_task = NULL;
2555 } 2322 }
2556 c->recv_task 2323 c->recv_task = GNUNET_SCHEDULER_add_now (&resume_client_receive, c);
2557 = GNUNET_SCHEDULER_add_now (&resume_client_receive,
2558 c);
2559} 2324}
2560 2325
2561 2326
@@ -2598,20 +2363,18 @@ finish_client_drop (void *cls)
2598 GNUNET_MQ_destroy (c->mq); 2363 GNUNET_MQ_destroy (c->mq);
2599 if (GNUNET_NO == c->persist) 2364 if (GNUNET_NO == c->persist)
2600 { 2365 {
2601 GNUNET_break (GNUNET_OK == 2366 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (c->sock));
2602 GNUNET_NETWORK_socket_close (c->sock)); 2367 if ((0 != (SUSPEND_STATE_EMFILE & sh->suspend_state)) &&
2603 if ( (0 != (SUSPEND_STATE_EMFILE & sh->suspend_state)) && 2368 (0 == (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)))
2604 (0 == (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)) ) 2369 do_resume (sh, SUSPEND_STATE_EMFILE);
2605 do_resume (sh,
2606 SUSPEND_STATE_EMFILE);
2607 } 2370 }
2608 else 2371 else
2609 { 2372 {
2610 GNUNET_NETWORK_socket_free_memory_only_ (c->sock); 2373 GNUNET_NETWORK_socket_free_memory_only_ (c->sock);
2611 } 2374 }
2612 GNUNET_free (c); 2375 GNUNET_free (c);
2613 if ( (0 != (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)) && 2376 if ((0 != (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)) &&
2614 (GNUNET_NO == have_non_monitor_clients (sh)) ) 2377 (GNUNET_NO == have_non_monitor_clients (sh)))
2615 GNUNET_SERVICE_shutdown (sh); 2378 GNUNET_SERVICE_shutdown (sh);
2616} 2379}
2617 2380
@@ -2640,8 +2403,7 @@ GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
2640 void *backtrace_array[MAX_TRACE_DEPTH]; 2403 void *backtrace_array[MAX_TRACE_DEPTH];
2641 int num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH); 2404 int num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
2642 char **backtrace_strings = 2405 char **backtrace_strings =
2643 backtrace_symbols (backtrace_array, 2406 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
2644 t->num_backtrace_strings);
2645 for (unsigned int i = 0; i < num_backtrace_strings; i++) 2407 for (unsigned int i = 0; i < num_backtrace_strings; i++)
2646 LOG (GNUNET_ERROR_TYPE_DEBUG, 2408 LOG (GNUNET_ERROR_TYPE_DEBUG,
2647 "client drop trace %u: %s\n", 2409 "client drop trace %u: %s\n",
@@ -2655,13 +2417,9 @@ GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
2655 GNUNET_assert (0); 2417 GNUNET_assert (0);
2656 return; 2418 return;
2657 } 2419 }
2658 GNUNET_CONTAINER_DLL_remove (sh->clients_head, 2420 GNUNET_CONTAINER_DLL_remove (sh->clients_head, sh->clients_tail, c);
2659 sh->clients_tail,
2660 c);
2661 if (NULL != sh->disconnect_cb) 2421 if (NULL != sh->disconnect_cb)
2662 sh->disconnect_cb (sh->cb_cls, 2422 sh->disconnect_cb (sh->cb_cls, c, c->user_context);
2663 c,
2664 c->user_context);
2665 if (NULL != c->warn_task) 2423 if (NULL != c->warn_task)
2666 { 2424 {
2667 GNUNET_SCHEDULER_cancel (c->warn_task); 2425 GNUNET_SCHEDULER_cancel (c->warn_task);
@@ -2677,8 +2435,7 @@ GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
2677 GNUNET_SCHEDULER_cancel (c->send_task); 2435 GNUNET_SCHEDULER_cancel (c->send_task);
2678 c->send_task = NULL; 2436 c->send_task = NULL;
2679 } 2437 }
2680 c->drop_task = GNUNET_SCHEDULER_add_now (&finish_client_drop, 2438 c->drop_task = GNUNET_SCHEDULER_add_now (&finish_client_drop, c);
2681 c);
2682} 2439}
2683 2440
2684 2441
@@ -2693,8 +2450,7 @@ GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
2693 struct GNUNET_SERVICE_Client *client; 2450 struct GNUNET_SERVICE_Client *client;
2694 2451
2695 if (0 == (sh->suspend_state & SUSPEND_STATE_SHUTDOWN)) 2452 if (0 == (sh->suspend_state & SUSPEND_STATE_SHUTDOWN))
2696 do_suspend (sh, 2453 do_suspend (sh, SUSPEND_STATE_SHUTDOWN);
2697 SUSPEND_STATE_SHUTDOWN);
2698 while (NULL != (client = sh->clients_head)) 2454 while (NULL != (client = sh->clients_head))
2699 GNUNET_SERVICE_client_drop (client); 2455 GNUNET_SERVICE_client_drop (client);
2700} 2456}
@@ -2716,8 +2472,8 @@ void
2716GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c) 2472GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
2717{ 2473{
2718 c->is_monitor = GNUNET_YES; 2474 c->is_monitor = GNUNET_YES;
2719 if ( ((0 != (SUSPEND_STATE_SHUTDOWN & c->sh->suspend_state))&& 2475 if (((0 != (SUSPEND_STATE_SHUTDOWN & c->sh->suspend_state)) &&
2720 (GNUNET_NO == have_non_monitor_clients (c->sh)) ) ) 2476 (GNUNET_NO == have_non_monitor_clients (c->sh))))
2721 GNUNET_SERVICE_shutdown (c->sh); 2477 GNUNET_SERVICE_shutdown (c->sh);
2722} 2478}
2723 2479
diff --git a/src/util/strings.c b/src/util/strings.c
index 8cd591288..ef9fdd693 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -34,9 +34,10 @@
34#include <unistr.h> 34#include <unistr.h>
35#include <uniconv.h> 35#include <uniconv.h>
36 36
37#define LOG(kind,...) GNUNET_log_from (kind, "util-strings", __VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log_from (kind, "util-strings", __VA_ARGS__)
38 38
39#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-strings", syscall) 39#define LOG_STRERROR(kind, syscall) \
40 GNUNET_log_from_strerror (kind, "util-strings", syscall)
40 41
41 42
42/** 43/**
@@ -105,15 +106,13 @@ GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids,
105 106
106 off = 0; 107 off = 0;
107 buf = GNUNET_malloc (plen); 108 buf = GNUNET_malloc (plen);
108 for (unsigned int i = 0; 109 for (unsigned int i = 0; i < num_pids; i++)
109 i < num_pids;
110 i++)
111 { 110 {
112 off += GNUNET_snprintf (&buf[off], 111 off += GNUNET_snprintf (&buf[off],
113 plen - off, 112 plen - off,
114 "%s%s", 113 "%s%s",
115 GNUNET_i2s (&pids[i]), 114 GNUNET_i2s (&pids[i]),
116 (i == num_pids -1) ? "" : "-"); 115 (i == num_pids - 1) ? "" : "-");
117 } 116 }
118 return buf; 117 return buf;
119} 118}
@@ -133,8 +132,10 @@ GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids,
133 * in the buffer, or 0 on error. 132 * in the buffer, or 0 on error.
134 */ 133 */
135unsigned int 134unsigned int
136GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, 135GNUNET_STRINGS_buffer_tokenize (const char *buffer,
137 unsigned int count, ...) 136 size_t size,
137 unsigned int count,
138 ...)
138{ 139{
139 unsigned int start; 140 unsigned int start;
140 unsigned int needed; 141 unsigned int needed;
@@ -153,10 +154,10 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
153 if (needed == size) 154 if (needed == size)
154 { 155 {
155 va_end (ap); 156 va_end (ap);
156 return 0; /* error */ 157 return 0; /* error */
157 } 158 }
158 *r = &buffer[start]; 159 *r = &buffer[start];
159 needed++; /* skip 0-termination */ 160 needed++; /* skip 0-termination */
160 count--; 161 count--;
161 } 162 }
162 va_end (ap); 163 va_end (ap);
@@ -173,7 +174,7 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
173char * 174char *
174GNUNET_STRINGS_byte_size_fancy (unsigned long long size) 175GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
175{ 176{
176 const char *unit = _( /* size unit */ "b"); 177 const char *unit = _ (/* size unit */ "b");
177 char *ret; 178 char *ret;
178 179
179 if (size > 5 * 1024) 180 if (size > 5 * 1024)
@@ -215,7 +216,7 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
215 * null byte 216 * null byte
216 */ 217 */
217size_t 218size_t
218GNUNET_strlcpy(char *dst, const char *src, size_t n) 219GNUNET_strlcpy (char *dst, const char *src, size_t n)
219{ 220{
220 size_t slen; 221 size_t slen;
221 222
@@ -257,8 +258,8 @@ struct ConversionTable
257 */ 258 */
258static int 259static int
259convert_with_table (const char *input, 260convert_with_table (const char *input,
260 const struct ConversionTable *table, 261 const struct ConversionTable *table,
261 unsigned long long *output) 262 unsigned long long *output)
262{ 263{
263 unsigned long long ret; 264 unsigned long long ret;
264 char *in; 265 char *in;
@@ -290,7 +291,7 @@ convert_with_table (const char *input,
290 if ((0 != errno) || (endptr == tok)) 291 if ((0 != errno) || (endptr == tok))
291 { 292 {
292 GNUNET_free (in); 293 GNUNET_free (in);
293 return GNUNET_SYSERR; /* expected number */ 294 return GNUNET_SYSERR; /* expected number */
294 } 295 }
295 if ('\0' == endptr[0]) 296 if ('\0' == endptr[0])
296 break; /* next tok */ 297 break; /* next tok */
@@ -318,26 +319,22 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
318 unsigned long long *size) 319 unsigned long long *size)
319{ 320{
320 static const struct ConversionTable table[] = 321 static const struct ConversionTable table[] =
321 { 322 {{"B", 1},
322 { "B", 1}, 323 {"KiB", 1024},
323 { "KiB", 1024}, 324 {"kB", 1000},
324 { "kB", 1000}, 325 {"MiB", 1024 * 1024},
325 { "MiB", 1024 * 1024}, 326 {"MB", 1000 * 1000},
326 { "MB", 1000 * 1000}, 327 {"GiB", 1024 * 1024 * 1024},
327 { "GiB", 1024 * 1024 * 1024}, 328 {"GB", 1000 * 1000 * 1000},
328 { "GB", 1000 * 1000 * 1000}, 329 {"TiB", 1024LL * 1024LL * 1024LL * 1024LL},
329 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL}, 330 {"TB", 1000LL * 1000LL * 1000LL * 1024LL},
330 { "TB", 1000LL * 1000LL * 1000LL * 1024LL}, 331 {"PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
331 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, 332 {"PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
332 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL}, 333 {"EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
333 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, 334 {"EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
334 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL}, 335 {NULL, 0}};
335 { NULL, 0} 336
336 }; 337 return convert_with_table (fancy_size, table, size);
337
338 return convert_with_table (fancy_size,
339 table,
340 size);
341} 338}
342 339
343 340
@@ -354,31 +351,29 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
354 struct GNUNET_TIME_Relative *rtime) 351 struct GNUNET_TIME_Relative *rtime)
355{ 352{
356 static const struct ConversionTable table[] = 353 static const struct ConversionTable table[] =
357 { 354 {{"us", 1},
358 { "us", 1}, 355 {"ms", 1000},
359 { "ms", 1000 }, 356 {"s", 1000 * 1000LL},
360 { "s", 1000 * 1000LL }, 357 {"second", 1000 * 1000LL},
361 { "second", 1000 * 1000LL }, 358 {"seconds", 1000 * 1000LL},
362 { "seconds", 1000 * 1000LL }, 359 {"\"", 1000 * 1000LL},
363 { "\"", 1000 * 1000LL }, 360 {"m", 60 * 1000 * 1000LL},
364 { "m", 60 * 1000 * 1000LL}, 361 {"min", 60 * 1000 * 1000LL},
365 { "min", 60 * 1000 * 1000LL}, 362 {"minute", 60 * 1000 * 1000LL},
366 { "minute", 60 * 1000 * 1000LL}, 363 {"minutes", 60 * 1000 * 1000LL},
367 { "minutes", 60 * 1000 * 1000LL}, 364 {"'", 60 * 1000 * 1000LL},
368 { "'", 60 * 1000 * 1000LL}, 365 {"h", 60 * 60 * 1000 * 1000LL},
369 { "h", 60 * 60 * 1000 * 1000LL}, 366 {"hour", 60 * 60 * 1000 * 1000LL},
370 { "hour", 60 * 60 * 1000 * 1000LL}, 367 {"hours", 60 * 60 * 1000 * 1000LL},
371 { "hours", 60 * 60 * 1000 * 1000LL}, 368 {"d", 24 * 60 * 60 * 1000LL * 1000LL},
372 { "d", 24 * 60 * 60 * 1000LL * 1000LL}, 369 {"day", 24 * 60 * 60 * 1000LL * 1000LL},
373 { "day", 24 * 60 * 60 * 1000LL * 1000LL}, 370 {"days", 24 * 60 * 60 * 1000LL * 1000LL},
374 { "days", 24 * 60 * 60 * 1000LL * 1000LL}, 371 {"week", 7 * 24 * 60 * 60 * 1000LL * 1000LL},
375 { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, 372 {"weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL},
376 { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, 373 {"year", 31536000000000LL /* year */},
377 { "year", 31536000000000LL /* year */ }, 374 {"years", 31536000000000LL /* year */},
378 { "years", 31536000000000LL /* year */ }, 375 {"a", 31536000000000LL /* year */},
379 { "a", 31536000000000LL /* year */ }, 376 {NULL, 0}};
380 { NULL, 0}
381 };
382 int ret; 377 int ret;
383 unsigned long long val; 378 unsigned long long val;
384 379
@@ -387,9 +382,7 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
387 *rtime = GNUNET_TIME_UNIT_FOREVER_REL; 382 *rtime = GNUNET_TIME_UNIT_FOREVER_REL;
388 return GNUNET_OK; 383 return GNUNET_OK;
389 } 384 }
390 ret = convert_with_table (fancy_time, 385 ret = convert_with_table (fancy_time, table, &val);
391 table,
392 &val);
393 rtime->rel_value_us = (uint64_t) val; 386 rtime->rel_value_us = (uint64_t) val;
394 return ret; 387 return ret;
395} 388}
@@ -412,24 +405,23 @@ GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
412 time_t t; 405 time_t t;
413 const char *eos; 406 const char *eos;
414 407
415 if (0 == strcasecmp ("end of time", 408 if (0 == strcasecmp ("end of time", fancy_time))
416 fancy_time))
417 { 409 {
418 *atime = GNUNET_TIME_UNIT_FOREVER_ABS; 410 *atime = GNUNET_TIME_UNIT_FOREVER_ABS;
419 return GNUNET_OK; 411 return GNUNET_OK;
420 } 412 }
421 eos = &fancy_time[strlen (fancy_time)]; 413 eos = &fancy_time[strlen (fancy_time)];
422 memset (&tv, 0, sizeof (tv)); 414 memset (&tv, 0, sizeof (tv));
423 if ( (eos != strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) && 415 if ((eos != strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) &&
424 (eos != strptime (fancy_time, "%c", &tv)) && 416 (eos != strptime (fancy_time, "%c", &tv)) &&
425 (eos != strptime (fancy_time, "%Ec", &tv)) && 417 (eos != strptime (fancy_time, "%Ec", &tv)) &&
426 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && 418 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) &&
427 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) && 419 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) &&
428 (eos != strptime (fancy_time, "%x", &tv)) && 420 (eos != strptime (fancy_time, "%x", &tv)) &&
429 (eos != strptime (fancy_time, "%Ex", &tv)) && 421 (eos != strptime (fancy_time, "%Ex", &tv)) &&
430 (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) && 422 (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) &&
431 (eos != strptime (fancy_time, "%Y-%m", &tv)) && 423 (eos != strptime (fancy_time, "%Y-%m", &tv)) &&
432 (eos != strptime (fancy_time, "%Y", &tv)) ) 424 (eos != strptime (fancy_time, "%Y", &tv)))
433 return GNUNET_SYSERR; 425 return GNUNET_SYSERR;
434 t = mktime (&tv); 426 t = mktime (&tv);
435 atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL); 427 atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL);
@@ -452,9 +444,9 @@ GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
452 */ 444 */
453char * 445char *
454GNUNET_STRINGS_conv (const char *input, 446GNUNET_STRINGS_conv (const char *input,
455 size_t len, 447 size_t len,
456 const char *input_charset, 448 const char *input_charset,
457 const char *output_charset) 449 const char *output_charset)
458{ 450{
459 char *ret; 451 char *ret;
460 uint8_t *u8_string; 452 uint8_t *u8_string;
@@ -463,10 +455,12 @@ GNUNET_STRINGS_conv (const char *input,
463 size_t encoded_string_length; 455 size_t encoded_string_length;
464 456
465 u8_string = u8_conv_from_encoding (input_charset, 457 u8_string = u8_conv_from_encoding (input_charset,
466 iconveh_error, 458 iconveh_error,
467 input, len, 459 input,
468 NULL, NULL, 460 len,
469 &u8_string_length); 461 NULL,
462 NULL,
463 &u8_string_length);
470 if (NULL == u8_string) 464 if (NULL == u8_string)
471 { 465 {
472 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); 466 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
@@ -480,10 +474,13 @@ GNUNET_STRINGS_conv (const char *input,
480 free (u8_string); 474 free (u8_string);
481 return ret; 475 return ret;
482 } 476 }
483 encoded_string = u8_conv_to_encoding (output_charset, iconveh_error, 477 encoded_string = u8_conv_to_encoding (output_charset,
484 u8_string, u8_string_length, 478 iconveh_error,
485 NULL, NULL, 479 u8_string,
486 &encoded_string_length); 480 u8_string_length,
481 NULL,
482 NULL,
483 &encoded_string_length);
487 free (u8_string); 484 free (u8_string);
488 if (NULL == encoded_string) 485 if (NULL == encoded_string)
489 { 486 {
@@ -495,10 +492,11 @@ GNUNET_STRINGS_conv (const char *input,
495 ret[encoded_string_length] = '\0'; 492 ret[encoded_string_length] = '\0';
496 free (encoded_string); 493 free (encoded_string);
497 return ret; 494 return ret;
498 fail: 495fail:
499 LOG (GNUNET_ERROR_TYPE_WARNING, 496 LOG (GNUNET_ERROR_TYPE_WARNING,
500 _("Character sets requested were `%s'->`%s'\n"), 497 _ ("Character sets requested were `%s'->`%s'\n"),
501 "UTF-8", output_charset); 498 "UTF-8",
499 output_charset);
502 ret = GNUNET_malloc (len + 1); 500 ret = GNUNET_malloc (len + 1);
503 GNUNET_memcpy (ret, input, len); 501 GNUNET_memcpy (ret, input, len);
504 ret[len] = '\0'; 502 ret[len] = '\0';
@@ -519,9 +517,7 @@ GNUNET_STRINGS_conv (const char *input,
519 * string is returned. 517 * string is returned.
520 */ 518 */
521char * 519char *
522GNUNET_STRINGS_to_utf8 (const char *input, 520GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
523 size_t len,
524 const char *charset)
525{ 521{
526 return GNUNET_STRINGS_conv (input, len, charset, "UTF-8"); 522 return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
527} 523}
@@ -539,9 +535,7 @@ GNUNET_STRINGS_to_utf8 (const char *input,
539 * string is returned. 535 * string is returned.
540 */ 536 */
541char * 537char *
542GNUNET_STRINGS_from_utf8 (const char *input, 538GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
543 size_t len,
544 const char *charset)
545{ 539{
546 return GNUNET_STRINGS_conv (input, len, "UTF-8", charset); 540 return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
547} 541}
@@ -555,17 +549,20 @@ GNUNET_STRINGS_from_utf8 (const char *input,
555 * @param output output buffer 549 * @param output output buffer
556 */ 550 */
557void 551void
558GNUNET_STRINGS_utf8_tolower (const char *input, 552GNUNET_STRINGS_utf8_tolower (const char *input, char *output)
559 char *output)
560{ 553{
561 uint8_t *tmp_in; 554 uint8_t *tmp_in;
562 size_t len; 555 size_t len;
563 556
564 tmp_in = u8_tolower ((uint8_t*)input, strlen ((char *) input), 557 tmp_in = u8_tolower ((uint8_t *) input,
565 NULL, UNINORM_NFD, NULL, &len); 558 strlen ((char *) input),
566 GNUNET_memcpy(output, tmp_in, len); 559 NULL,
560 UNINORM_NFD,
561 NULL,
562 &len);
563 GNUNET_memcpy (output, tmp_in, len);
567 output[len] = '\0'; 564 output[len] = '\0';
568 free(tmp_in); 565 free (tmp_in);
569} 566}
570 567
571 568
@@ -577,14 +574,17 @@ GNUNET_STRINGS_utf8_tolower (const char *input,
577 * @param output output buffer 574 * @param output output buffer
578 */ 575 */
579void 576void
580GNUNET_STRINGS_utf8_toupper(const char *input, 577GNUNET_STRINGS_utf8_toupper (const char *input, char *output)
581 char *output)
582{ 578{
583 uint8_t *tmp_in; 579 uint8_t *tmp_in;
584 size_t len; 580 size_t len;
585 581
586 tmp_in = u8_toupper ((uint8_t*)input, strlen ((char *) input), 582 tmp_in = u8_toupper ((uint8_t *) input,
587 NULL, UNINORM_NFD, NULL, &len); 583 strlen ((char *) input),
584 NULL,
585 UNINORM_NFD,
586 NULL,
587 &len);
588 GNUNET_memcpy (output, tmp_in, len); 588 GNUNET_memcpy (output, tmp_in, len);
589 output[len] = '\0'; 589 output[len] = '\0';
590 free (tmp_in); 590 free (tmp_in);
@@ -624,7 +624,7 @@ GNUNET_STRINGS_filename_expand (const char *fil)
624 if (fm == NULL) 624 if (fm == NULL)
625 { 625 {
626 LOG (GNUNET_ERROR_TYPE_WARNING, 626 LOG (GNUNET_ERROR_TYPE_WARNING,
627 _("Failed to expand `$HOME': environment variable `HOME' not set")); 627 _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
628 return NULL; 628 return NULL;
629 } 629 }
630 fm = GNUNET_strdup (fm); 630 fm = GNUNET_strdup (fm);
@@ -660,20 +660,20 @@ GNUNET_STRINGS_filename_expand (const char *fil)
660 } 660 }
661 if (fm == NULL) 661 if (fm == NULL)
662 { 662 {
663 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 663 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
664 "getcwd"); 664 buffer = getenv ("PWD"); /* alternative */
665 buffer = getenv ("PWD"); /* alternative */
666 if (buffer != NULL) 665 if (buffer != NULL)
667 fm = GNUNET_strdup (buffer); 666 fm = GNUNET_strdup (buffer);
668 } 667 }
669 if (fm == NULL) 668 if (fm == NULL)
670 fm = GNUNET_strdup ("./"); /* give up */ 669 fm = GNUNET_strdup ("./"); /* give up */
671 } 670 }
672 GNUNET_asprintf (&buffer, 671 GNUNET_asprintf (&buffer,
673 "%s%s%s", 672 "%s%s%s",
674 fm, 673 fm,
675 (fm[strlen (fm) - 1] == 674 (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
676 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr); 675 : DIR_SEPARATOR_STR,
676 fil_ptr);
677 GNUNET_free (fm); 677 GNUNET_free (fm);
678 return buffer; 678 return buffer;
679#else 679#else
@@ -682,29 +682,22 @@ GNUNET_STRINGS_filename_expand (const char *fil)
682 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS) 682 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
683 { 683 {
684 SetErrnoFromWinError (lRet); 684 SetErrnoFromWinError (lRet);
685 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 685 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
686 "plibc_conv_to_win_path");
687 return NULL; 686 return NULL;
688 } 687 }
689 /* is the path relative? */ 688 /* is the path relative? */
690 if ( (0 != strncmp (fn + 1, ":\\", 2)) && 689 if ((0 != strncmp (fn + 1, ":\\", 2)) && (0 != strncmp (fn, "\\\\", 2)))
691 (0 != strncmp (fn, "\\\\", 2)) )
692 { 690 {
693 char szCurDir[MAX_PATH + 1]; 691 char szCurDir[MAX_PATH + 1];
694 692
695 lRet = GetCurrentDirectory (MAX_PATH + 1, 693 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
696 szCurDir);
697 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1)) 694 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
698 { 695 {
699 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW); 696 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
700 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 697 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
701 "GetCurrentDirectory");
702 return NULL; 698 return NULL;
703 } 699 }
704 GNUNET_asprintf (&buffer, 700 GNUNET_asprintf (&buffer, "%s\\%s", szCurDir, fn);
705 "%s\\%s",
706 szCurDir,
707 fn);
708 GNUNET_free (fn); 701 GNUNET_free (fn);
709 fn = buffer; 702 fn = buffer;
710 } 703 }
@@ -725,56 +718,46 @@ GNUNET_STRINGS_filename_expand (const char *fil)
725 */ 718 */
726const char * 719const char *
727GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, 720GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
728 int do_round) 721 int do_round)
729{ 722{
730 static char buf[128]; 723 static char buf[128];
731 const char *unit = _( /* time unit */ "µs"); 724 const char *unit = _ (/* time unit */ "µs");
732 uint64_t dval = delta.rel_value_us; 725 uint64_t dval = delta.rel_value_us;
733 726
734 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) 727 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us)
735 return _("forever"); 728 return _ ("forever");
736 if (0 == delta.rel_value_us) 729 if (0 == delta.rel_value_us)
737 return _("0 ms"); 730 return _ ("0 ms");
738 if ( ( (GNUNET_YES == do_round) && 731 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
739 (dval > 5 * 1000) ) ||
740 (0 == (dval % 1000) ))
741 { 732 {
742 dval = dval / 1000; 733 dval = dval / 1000;
743 unit = _( /* time unit */ "ms"); 734 unit = _ (/* time unit */ "ms");
744 if ( ( (GNUNET_YES == do_round) && 735 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
745 (dval > 5 * 1000) ) ||
746 (0 == (dval % 1000) ))
747 { 736 {
748 dval = dval / 1000; 737 dval = dval / 1000;
749 unit = _( /* time unit */ "s"); 738 unit = _ (/* time unit */ "s");
750 if ( ( (GNUNET_YES == do_round) && 739 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
751 (dval > 5 * 60) ) ||
752 (0 == (dval % 60) ) )
753 { 740 {
754 dval = dval / 60; 741 dval = dval / 60;
755 unit = _( /* time unit */ "m"); 742 unit = _ (/* time unit */ "m");
756 if ( ( (GNUNET_YES == do_round) && 743 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
757 (dval > 5 * 60) ) || 744 {
758 (0 == (dval % 60) )) 745 dval = dval / 60;
759 { 746 unit = _ (/* time unit */ "h");
760 dval = dval / 60; 747 if (((GNUNET_YES == do_round) && (dval > 5 * 24)) ||
761 unit = _( /* time unit */ "h"); 748 (0 == (dval % 24)))
762 if ( ( (GNUNET_YES == do_round) && 749 {
763 (dval > 5 * 24) ) || 750 dval = dval / 24;
764 (0 == (dval % 24)) ) 751 if (1 == dval)
765 { 752 unit = _ (/* time unit */ "day");
766 dval = dval / 24; 753 else
767 if (1 == dval) 754 unit = _ (/* time unit */ "days");
768 unit = _( /* time unit */ "day"); 755 }
769 else 756 }
770 unit = _( /* time unit */ "days");
771 }
772 }
773 } 757 }
774 } 758 }
775 } 759 }
776 GNUNET_snprintf (buf, sizeof (buf), 760 GNUNET_snprintf (buf, sizeof (buf), "%llu %s", dval, unit);
777 "%llu %s", dval, unit);
778 return buf; 761 return buf;
779} 762}
780 763
@@ -796,7 +779,7 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
796 struct tm *tp; 779 struct tm *tp;
797 780
798 if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 781 if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
799 return _("end of time"); 782 return _ ("end of time");
800 tt = t.abs_value_us / 1000LL / 1000LL; 783 tt = t.abs_value_us / 1000LL / 1000LL;
801 tp = localtime (&tt); 784 tp = localtime (&tt);
802 /* This is hacky, but i don't know a way to detect libc character encoding. 785 /* This is hacky, but i don't know a way to detect libc character encoding.
@@ -813,12 +796,16 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
813 uint8_t *conved; 796 uint8_t *conved;
814 size_t ssize; 797 size_t ssize;
815 798
816 wcsftime (wbuf, sizeof (wbuf) / sizeof (wchar_t), 799 wcsftime (wbuf,
817 L"%a %b %d %H:%M:%S %Y", tp); 800 sizeof (wbuf) / sizeof (wchar_t),
801 L"%a %b %d %H:%M:%S %Y",
802 tp);
818 803
819 ssize = sizeof (buf); 804 ssize = sizeof (buf);
820 conved = u16_to_u8 (wbuf, sizeof (wbuf) / sizeof (wchar_t), 805 conved = u16_to_u8 (wbuf,
821 (uint8_t *) buf, &ssize); 806 sizeof (wbuf) / sizeof (wchar_t),
807 (uint8_t *) buf,
808 &ssize);
822 if (conved != (uint8_t *) buf) 809 if (conved != (uint8_t *) buf)
823 { 810 {
824 GNUNET_strlcpy (buf, (char *) conved, sizeof (buf)); 811 GNUNET_strlcpy (buf, (char *) conved, sizeof (buf));
@@ -847,8 +834,7 @@ GNUNET_STRINGS_get_short_name (const char *filename)
847 const char *short_fn = filename; 834 const char *short_fn = filename;
848 const char *ss; 835 const char *ss;
849 836
850 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) 837 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
851 && (ss[1] != '\0'))
852 short_fn = 1 + ss; 838 short_fn = 1 + ss;
853 return short_fn; 839 return short_fn;
854} 840}
@@ -890,7 +876,7 @@ getValue__ (unsigned char a)
890 return a - '0'; 876 return a - '0';
891 if ((a >= 'a') && (a <= 'z')) 877 if ((a >= 'a') && (a <= 'z'))
892 a = toupper (a); 878 a = toupper (a);
893 /* return (a - 'a' + 10); */ 879 /* return (a - 'a' + 10); */
894 dec = 0; 880 dec = 0;
895 if ((a >= 'A') && (a <= 'Z')) 881 if ((a >= 'A') && (a <= 'Z'))
896 { 882 {
@@ -950,12 +936,12 @@ GNUNET_STRINGS_data_to_string (const void *data,
950 { 936 {
951 if ((rpos < size) && (vbit < 5)) 937 if ((rpos < size) && (vbit < 5))
952 { 938 {
953 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */ 939 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
954 vbit += 8; 940 vbit += 8;
955 } 941 }
956 if (vbit < 5) 942 if (vbit < 5)
957 { 943 {
958 bits <<= (5 - vbit); /* zero-padding */ 944 bits <<= (5 - vbit); /* zero-padding */
959 GNUNET_assert (vbit == ((size * 8) % 5)); 945 GNUNET_assert (vbit == ((size * 8) % 5));
960 vbit = 5; 946 vbit = 5;
961 } 947 }
@@ -985,8 +971,7 @@ GNUNET_STRINGS_data_to_string (const void *data,
985 * @return freshly allocated, null-terminated string 971 * @return freshly allocated, null-terminated string
986 */ 972 */
987char * 973char *
988GNUNET_STRINGS_data_to_string_alloc (const void *buf, 974GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size)
989 size_t size)
990{ 975{
991 char *str_buf; 976 char *str_buf;
992 size_t len = size * 8; 977 size_t len = size * 8;
@@ -1018,8 +1003,10 @@ GNUNET_STRINGS_data_to_string_alloc (const void *buf,
1018 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding 1003 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
1019 */ 1004 */
1020int 1005int
1021GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, 1006GNUNET_STRINGS_string_to_data (const char *enc,
1022 void *out, size_t out_size) 1007 size_t enclen,
1008 void *out,
1009 size_t out_size)
1023{ 1010{
1024 unsigned int rpos; 1011 unsigned int rpos;
1025 unsigned int wpos; 1012 unsigned int wpos;
@@ -1073,8 +1060,7 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
1073 vbit -= 8; 1060 vbit -= 8;
1074 } 1061 }
1075 } 1062 }
1076 if ( (0 != rpos) || 1063 if ((0 != rpos) || (0 != vbit))
1077 (0 != vbit) )
1078 return GNUNET_SYSERR; 1064 return GNUNET_SYSERR;
1079 return GNUNET_OK; 1065 return GNUNET_OK;
1080} 1066}
@@ -1098,8 +1084,8 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
1098 */ 1084 */
1099int 1085int
1100GNUNET_STRINGS_parse_uri (const char *path, 1086GNUNET_STRINGS_parse_uri (const char *path,
1101 char **scheme_part, 1087 char **scheme_part,
1102 const char **path_part) 1088 const char **path_part)
1103{ 1089{
1104 size_t len; 1090 size_t len;
1105 size_t i; 1091 size_t i;
@@ -1107,19 +1093,20 @@ GNUNET_STRINGS_parse_uri (const char *path,
1107 int pp_state = 0; 1093 int pp_state = 0;
1108 const char *post_scheme_part = NULL; 1094 const char *post_scheme_part = NULL;
1109 len = strlen (path); 1095 len = strlen (path);
1110 for (end = 0, i = 0; !end && i < len; i++) 1096 for (end = 0, i = 0; ! end && i < len; i++)
1111 { 1097 {
1112 switch (pp_state) 1098 switch (pp_state)
1113 { 1099 {
1114 case 0: 1100 case 0:
1115 if ( (path[i] == ':') && (i > 0) ) 1101 if ((path[i] == ':') && (i > 0))
1116 { 1102 {
1117 pp_state += 1; 1103 pp_state += 1;
1118 continue; 1104 continue;
1119 } 1105 }
1120 if (!((path[i] >= 'A' && path[i] <= 'Z') || (path[i] >= 'a' && path[i] <= 'z') 1106 if (! ((path[i] >= 'A' && path[i] <= 'Z') ||
1121 || (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || path[i] == '-' 1107 (path[i] >= 'a' && path[i] <= 'z') ||
1122 || (path[i] == '.'))) 1108 (path[i] >= '0' && path[i] <= '9') || path[i] == '+' ||
1109 path[i] == '-' || (path[i] == '.')))
1123 end = 1; 1110 end = 1;
1124 break; 1111 break;
1125 case 1: 1112 case 1:
@@ -1178,7 +1165,7 @@ GNUNET_STRINGS_path_is_absolute (const char *filename,
1178#endif 1165#endif
1179 const char *post_scheme_path; 1166 const char *post_scheme_path;
1180 int is_uri; 1167 int is_uri;
1181 char * uri; 1168 char *uri;
1182 /* consider POSIX paths to be absolute too, even on W32, 1169 /* consider POSIX paths to be absolute too, even on W32,
1183 * as plibc expansion will fix them for us. 1170 * as plibc expansion will fix them for us.
1184 */ 1171 */
@@ -1196,14 +1183,17 @@ GNUNET_STRINGS_path_is_absolute (const char *filename,
1196 else 1183 else
1197 GNUNET_free_non_null (uri); 1184 GNUNET_free_non_null (uri);
1198#if WINDOWS 1185#if WINDOWS
1199 len = strlen(post_scheme_path); 1186 len = strlen (post_scheme_path);
1200 /* Special check for file:///c:/blah 1187 /* Special check for file:///c:/blah
1201 * We want to parse 'c:/', not '/c:/' 1188 * We want to parse 'c:/', not '/c:/'
1202 */ 1189 */
1203 if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':') 1190 if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':')
1204 post_scheme_path = &post_scheme_path[1]; 1191 post_scheme_path = &post_scheme_path[1];
1205#endif 1192#endif
1206 return GNUNET_STRINGS_path_is_absolute (post_scheme_path, GNUNET_NO, NULL, NULL); 1193 return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1194 GNUNET_NO,
1195 NULL,
1196 NULL);
1207 } 1197 }
1208 } 1198 }
1209 else 1199 else
@@ -1214,18 +1204,18 @@ GNUNET_STRINGS_path_is_absolute (const char *filename,
1214#if WINDOWS 1204#if WINDOWS
1215 len = strlen (filename); 1205 len = strlen (filename);
1216 if (len >= 3 && 1206 if (len >= 3 &&
1217 ((filename[0] >= 'A' && filename[0] <= 'Z') 1207 ((filename[0] >= 'A' && filename[0] <= 'Z') ||
1218 || (filename[0] >= 'a' && filename[0] <= 'z')) 1208 (filename[0] >= 'a' && filename[0] <= 'z')) &&
1219 && filename[1] == ':' && (filename[2] == '/' || filename[2] == '\\')) 1209 filename[1] == ':' && (filename[2] == '/' || filename[2] == '\\'))
1220 return GNUNET_YES; 1210 return GNUNET_YES;
1221#endif 1211#endif
1222 return GNUNET_NO; 1212 return GNUNET_NO;
1223} 1213}
1224 1214
1225#if MINGW 1215#if MINGW
1226#define _IFMT 0170000 /* type of file */ 1216#define _IFMT 0170000 /* type of file */
1227#define _IFLNK 0120000 /* symbolic link */ 1217#define _IFLNK 0120000 /* symbolic link */
1228#define S_ISLNK(m) (((m)&_IFMT) == _IFLNK) 1218#define S_ISLNK(m) (((m) &_IFMT) == _IFLNK)
1229#endif 1219#endif
1230 1220
1231 1221
@@ -1239,19 +1229,19 @@ GNUNET_STRINGS_path_is_absolute (const char *filename,
1239 */ 1229 */
1240int 1230int
1241GNUNET_STRINGS_check_filename (const char *filename, 1231GNUNET_STRINGS_check_filename (const char *filename,
1242 enum GNUNET_STRINGS_FilenameCheck checks) 1232 enum GNUNET_STRINGS_FilenameCheck checks)
1243{ 1233{
1244 struct stat st; 1234 struct stat st;
1245 if ( (NULL == filename) || (filename[0] == '\0') ) 1235 if ((NULL == filename) || (filename[0] == '\0'))
1246 return GNUNET_SYSERR; 1236 return GNUNET_SYSERR;
1247 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) 1237 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1248 if (!GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL)) 1238 if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
1249 return GNUNET_NO; 1239 return GNUNET_NO;
1250 if (0 != (checks & (GNUNET_STRINGS_CHECK_EXISTS 1240 if (0 != (checks &
1251 | GNUNET_STRINGS_CHECK_IS_DIRECTORY 1241 (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY |
1252 | GNUNET_STRINGS_CHECK_IS_LINK))) 1242 GNUNET_STRINGS_CHECK_IS_LINK)))
1253 { 1243 {
1254 if (0 != STAT (filename, &st)) 1244 if (0 != stat (filename, &st))
1255 { 1245 {
1256 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) 1246 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1257 return GNUNET_NO; 1247 return GNUNET_NO;
@@ -1260,10 +1250,10 @@ GNUNET_STRINGS_check_filename (const char *filename,
1260 } 1250 }
1261 } 1251 }
1262 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) 1252 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1263 if (!S_ISDIR (st.st_mode)) 1253 if (! S_ISDIR (st.st_mode))
1264 return GNUNET_NO; 1254 return GNUNET_NO;
1265 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) 1255 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1266 if (!S_ISLNK (st.st_mode)) 1256 if (! S_ISLNK (st.st_mode))
1267 return GNUNET_NO; 1257 return GNUNET_NO;
1268 return GNUNET_YES; 1258 return GNUNET_YES;
1269} 1259}
@@ -1283,8 +1273,8 @@ GNUNET_STRINGS_check_filename (const char *filename,
1283 */ 1273 */
1284int 1274int
1285GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, 1275GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1286 uint16_t addrlen, 1276 uint16_t addrlen,
1287 struct sockaddr_in6 *r_buf) 1277 struct sockaddr_in6 *r_buf)
1288{ 1278{
1289 char zbuf[addrlen + 1]; 1279 char zbuf[addrlen + 1];
1290 int ret; 1280 int ret;
@@ -1298,7 +1288,7 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1298 if ('[' != zbuf[0]) 1288 if ('[' != zbuf[0])
1299 { 1289 {
1300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1301 _("IPv6 address did not start with `['\n")); 1291 _ ("IPv6 address did not start with `['\n"));
1302 return GNUNET_SYSERR; 1292 return GNUNET_SYSERR;
1303 } 1293 }
1304 zbuf[addrlen] = '\0'; 1294 zbuf[addrlen] = '\0';
@@ -1306,34 +1296,33 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1306 if (NULL == port_colon) 1296 if (NULL == port_colon)
1307 { 1297 {
1308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1309 _("IPv6 address did contain ':' to separate port number\n")); 1299 _ ("IPv6 address did contain ':' to separate port number\n"));
1310 return GNUNET_SYSERR; 1300 return GNUNET_SYSERR;
1311 } 1301 }
1312 if (']' != *(port_colon - 1)) 1302 if (']' != *(port_colon - 1))
1313 { 1303 {
1314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1304 GNUNET_log (
1315 _("IPv6 address did contain ']' before ':' to separate port number\n")); 1305 GNUNET_ERROR_TYPE_WARNING,
1306 _ ("IPv6 address did contain ']' before ':' to separate port number\n"));
1316 return GNUNET_SYSERR; 1307 return GNUNET_SYSERR;
1317 } 1308 }
1318 ret = SSCANF (port_colon, 1309 ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1319 ":%u%1s", 1310 if ((1 != ret) || (port > 65535))
1320 &port,
1321 dummy);
1322 if ( (1 != ret) || (port > 65535) )
1323 { 1311 {
1324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1312 GNUNET_log (
1325 _("IPv6 address did contain a valid port number after the last ':'\n")); 1313 GNUNET_ERROR_TYPE_WARNING,
1314 _ ("IPv6 address did contain a valid port number after the last ':'\n"));
1326 return GNUNET_SYSERR; 1315 return GNUNET_SYSERR;
1327 } 1316 }
1328 *(port_colon-1) = '\0'; 1317 *(port_colon - 1) = '\0';
1329 memset (r_buf, 0, sizeof (struct sockaddr_in6)); 1318 memset (r_buf, 0, sizeof (struct sockaddr_in6));
1330 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr); 1319 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1331 if (ret <= 0) 1320 if (ret <= 0)
1332 { 1321 {
1333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1334 _("Invalid IPv6 address `%s': %s\n"), 1323 _ ("Invalid IPv6 address `%s': %s\n"),
1335 &zbuf[1], 1324 &zbuf[1],
1336 STRERROR (errno)); 1325 strerror (errno));
1337 return GNUNET_SYSERR; 1326 return GNUNET_SYSERR;
1338 } 1327 }
1339 r_buf->sin6_port = htons (port); 1328 r_buf->sin6_port = htons (port);
@@ -1358,8 +1347,8 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1358 */ 1347 */
1359int 1348int
1360GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, 1349GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
1361 uint16_t addrlen, 1350 uint16_t addrlen,
1362 struct sockaddr_in *r_buf) 1351 struct sockaddr_in *r_buf)
1363{ 1352{
1364 unsigned int temps[4]; 1353 unsigned int temps[4];
1365 unsigned int port; 1354 unsigned int port;
@@ -1368,14 +1357,14 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
1368 1357
1369 if (addrlen < 9) 1358 if (addrlen < 9)
1370 return GNUNET_SYSERR; 1359 return GNUNET_SYSERR;
1371 cnt = SSCANF (zt_addr, 1360 cnt = sscanf (zt_addr,
1372 "%u.%u.%u.%u:%u%1s", 1361 "%u.%u.%u.%u:%u%1s",
1373 &temps[0], 1362 &temps[0],
1374 &temps[1], 1363 &temps[1],
1375 &temps[2], 1364 &temps[2],
1376 &temps[3], 1365 &temps[3],
1377 &port, 1366 &port,
1378 dummy); 1367 dummy);
1379 if (5 != cnt) 1368 if (5 != cnt)
1380 return GNUNET_SYSERR; 1369 return GNUNET_SYSERR;
1381 for (cnt = 0; cnt < 4; cnt++) 1370 for (cnt = 0; cnt < 4; cnt++)
@@ -1385,8 +1374,8 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
1385 return GNUNET_SYSERR; 1374 return GNUNET_SYSERR;
1386 r_buf->sin_family = AF_INET; 1375 r_buf->sin_family = AF_INET;
1387 r_buf->sin_port = htons (port); 1376 r_buf->sin_port = htons (port);
1388 r_buf->sin_addr.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + 1377 r_buf->sin_addr.s_addr =
1389 (temps[2] << 8) + temps[3]); 1378 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1390#if HAVE_SOCKADDR_IN_SIN_LEN 1379#if HAVE_SOCKADDR_IN_SIN_LEN
1391 r_buf->sin_len = (u_char) sizeof (struct sockaddr_in); 1380 r_buf->sin_len = (u_char) sizeof (struct sockaddr_in);
1392#endif 1381#endif
@@ -1407,8 +1396,8 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
1407 */ 1396 */
1408int 1397int
1409GNUNET_STRINGS_to_address_ip (const char *addr, 1398GNUNET_STRINGS_to_address_ip (const char *addr,
1410 uint16_t addrlen, 1399 uint16_t addrlen,
1411 struct sockaddr_storage *r_buf) 1400 struct sockaddr_storage *r_buf)
1412{ 1401{
1413 if (addr[0] == '[') 1402 if (addr[0] == '[')
1414 return GNUNET_STRINGS_to_address_ipv6 (addr, 1403 return GNUNET_STRINGS_to_address_ipv6 (addr,
@@ -1431,8 +1420,8 @@ GNUNET_STRINGS_to_address_ip (const char *addr,
1431 */ 1420 */
1432size_t 1421size_t
1433GNUNET_STRINGS_parse_socket_addr (const char *addr, 1422GNUNET_STRINGS_parse_socket_addr (const char *addr,
1434 uint8_t *af, 1423 uint8_t *af,
1435 struct sockaddr **sa) 1424 struct sockaddr **sa)
1436{ 1425{
1437 char *cp = GNUNET_strdup (addr); 1426 char *cp = GNUNET_strdup (addr);
1438 1427
@@ -1442,9 +1431,9 @@ GNUNET_STRINGS_parse_socket_addr (const char *addr,
1442 /* IPv6 */ 1431 /* IPv6 */
1443 *sa = GNUNET_malloc (sizeof (struct sockaddr_in6)); 1432 *sa = GNUNET_malloc (sizeof (struct sockaddr_in6));
1444 if (GNUNET_OK != 1433 if (GNUNET_OK !=
1445 GNUNET_STRINGS_to_address_ipv6 (cp, 1434 GNUNET_STRINGS_to_address_ipv6 (cp,
1446 strlen (cp), 1435 strlen (cp),
1447 (struct sockaddr_in6 *) *sa)) 1436 (struct sockaddr_in6 *) *sa))
1448 { 1437 {
1449 GNUNET_free (*sa); 1438 GNUNET_free (*sa);
1450 *sa = NULL; 1439 *sa = NULL;
@@ -1460,9 +1449,9 @@ GNUNET_STRINGS_parse_socket_addr (const char *addr,
1460 /* IPv4 */ 1449 /* IPv4 */
1461 *sa = GNUNET_malloc (sizeof (struct sockaddr_in)); 1450 *sa = GNUNET_malloc (sizeof (struct sockaddr_in));
1462 if (GNUNET_OK != 1451 if (GNUNET_OK !=
1463 GNUNET_STRINGS_to_address_ipv4 (cp, 1452 GNUNET_STRINGS_to_address_ipv4 (cp,
1464 strlen (cp), 1453 strlen (cp),
1465 (struct sockaddr_in *) *sa)) 1454 (struct sockaddr_in *) *sa))
1466 { 1455 {
1467 GNUNET_free (*sa); 1456 GNUNET_free (*sa);
1468 *sa = NULL; 1457 *sa = NULL;
@@ -1481,8 +1470,7 @@ GNUNET_STRINGS_parse_socket_addr (const char *addr,
1481 * freed with a single call to GNUNET_free(); 1470 * freed with a single call to GNUNET_free();
1482 */ 1471 */
1483static char *const * 1472static char *const *
1484_make_continuous_arg_copy (int argc, 1473_make_continuous_arg_copy (int argc, char *const *argv)
1485 char *const *argv)
1486{ 1474{
1487 size_t argvsize = 0; 1475 size_t argvsize = 0;
1488 int i; 1476 int i;
@@ -1519,9 +1507,9 @@ _make_continuous_arg_copy (int argc,
1519 */ 1507 */
1520int 1508int
1521GNUNET_STRINGS_get_utf8_args (int argc, 1509GNUNET_STRINGS_get_utf8_args (int argc,
1522 char *const *argv, 1510 char *const *argv,
1523 int *u8argc, 1511 int *u8argc,
1524 char *const **u8argv) 1512 char *const **u8argv)
1525{ 1513{
1526#if WINDOWS 1514#if WINDOWS
1527 wchar_t *wcmd; 1515 wchar_t *wcmd;
@@ -1543,7 +1531,8 @@ GNUNET_STRINGS_get_utf8_args (int argc,
1543 { 1531 {
1544 size_t strl; 1532 size_t strl;
1545 /* Hopefully it will allocate us NUL-terminated strings... */ 1533 /* Hopefully it will allocate us NUL-terminated strings... */
1546 split_u8argv[i] = (char *) u16_to_u8 (wargv[i], wcslen (wargv[i]) + 1, NULL, &strl); 1534 split_u8argv[i] =
1535 (char *) u16_to_u8 (wargv[i], wcslen (wargv[i]) + 1, NULL, &strl);
1547 if (NULL == split_u8argv[i]) 1536 if (NULL == split_u8argv[i])
1548 { 1537 {
1549 int j; 1538 int j;
@@ -1563,7 +1552,8 @@ GNUNET_STRINGS_get_utf8_args (int argc,
1563 free (split_u8argv); 1552 free (split_u8argv);
1564 return GNUNET_OK; 1553 return GNUNET_OK;
1565#else 1554#else
1566 char *const *new_argv = (char *const *) _make_continuous_arg_copy (argc, argv); 1555 char *const *new_argv =
1556 (char *const *) _make_continuous_arg_copy (argc, argv);
1567 *u8argv = new_argv; 1557 *u8argv = new_argv;
1568 *u8argc = argc; 1558 *u8argc = argc;
1569 return GNUNET_OK; 1559 return GNUNET_OK;
@@ -1595,35 +1585,22 @@ parse_port_policy (const char *port_policy,
1595 pp->negate_portrange = GNUNET_YES; 1585 pp->negate_portrange = GNUNET_YES;
1596 pos++; 1586 pos++;
1597 } 1587 }
1598 if (2 == sscanf (pos, 1588 if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol))
1599 "%u-%u%1s",
1600 &s,
1601 &e,
1602 eol))
1603 { 1589 {
1604 if ( (0 == s) || 1590 if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF))
1605 (s > 0xFFFF) ||
1606 (e < s) ||
1607 (e > 0xFFFF) )
1608 { 1591 {
1609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1592 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1610 _("Port not in range\n"));
1611 return GNUNET_SYSERR; 1593 return GNUNET_SYSERR;
1612 } 1594 }
1613 pp->start_port = (uint16_t) s; 1595 pp->start_port = (uint16_t) s;
1614 pp->end_port = (uint16_t) e; 1596 pp->end_port = (uint16_t) e;
1615 return GNUNET_OK; 1597 return GNUNET_OK;
1616 } 1598 }
1617 if (1 == sscanf (pos, 1599 if (1 == sscanf (pos, "%u%1s", &s, eol))
1618 "%u%1s",
1619 &s,
1620 eol))
1621 { 1600 {
1622 if ( (0 == s) || 1601 if ((0 == s) || (s > 0xFFFF))
1623 (s > 0xFFFF) )
1624 { 1602 {
1625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1626 _("Port not in range\n"));
1627 return GNUNET_SYSERR; 1604 return GNUNET_SYSERR;
1628 } 1605 }
1629 1606
@@ -1632,7 +1609,7 @@ parse_port_policy (const char *port_policy,
1632 return GNUNET_OK; 1609 return GNUNET_OK;
1633 } 1610 }
1634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1635 _("Malformed port policy `%s'\n"), 1612 _ ("Malformed port policy `%s'\n"),
1636 port_policy); 1613 port_policy);
1637 return GNUNET_SYSERR; 1614 return GNUNET_SYSERR;
1638} 1615}
@@ -1666,7 +1643,7 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1666 int end; 1643 int end;
1667 char *routeList; 1644 char *routeList;
1668 char dummy[2]; 1645 char dummy[2];
1669 1646
1670 if (NULL == routeListX) 1647 if (NULL == routeListX)
1671 return NULL; 1648 return NULL;
1672 len = strlen (routeListX); 1649 len = strlen (routeListX);
@@ -1677,14 +1654,14 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1677 for (i = 0; i < len; i++) 1654 for (i = 0; i < len; i++)
1678 if (routeList[i] == ';') 1655 if (routeList[i] == ';')
1679 count++; 1656 count++;
1680 result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv4NetworkPolicy) * (count + 1)); 1657 result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv4NetworkPolicy) *
1658 (count + 1));
1681 i = 0; 1659 i = 0;
1682 pos = 0; 1660 pos = 0;
1683 while (i < count) 1661 while (i < count)
1684 { 1662 {
1685 for (colon = pos; ':' != routeList[colon]; colon++) 1663 for (colon = pos; ':' != routeList[colon]; colon++)
1686 if ( (';' == routeList[colon]) || 1664 if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1687 ('\0' == routeList[colon]) )
1688 break; 1665 break;
1689 for (end = colon; ';' != routeList[end]; end++) 1666 for (end = colon; ';' != routeList[end]; end++)
1690 if ('\0' == routeList[end]) 1667 if ('\0' == routeList[end])
@@ -1695,76 +1672,70 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1695 if (':' == routeList[colon]) 1672 if (':' == routeList[colon])
1696 { 1673 {
1697 routeList[colon] = '\0'; 1674 routeList[colon] = '\0';
1698 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], 1675 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1699 &result[i].pp))
1700 break; 1676 break;
1701 } 1677 }
1702 cnt = 1678 cnt = sscanf (&routeList[pos],
1703 SSCANF (&routeList[pos], 1679 "%u.%u.%u.%u/%u.%u.%u.%u%1s",
1704 "%u.%u.%u.%u/%u.%u.%u.%u%1s", 1680 &temps[0],
1705 &temps[0], 1681 &temps[1],
1706 &temps[1], 1682 &temps[2],
1707 &temps[2], 1683 &temps[3],
1708 &temps[3], 1684 &temps[4],
1709 &temps[4], 1685 &temps[5],
1710 &temps[5], 1686 &temps[6],
1711 &temps[6], 1687 &temps[7],
1712 &temps[7], 1688 dummy);
1713 dummy);
1714 if (8 == cnt) 1689 if (8 == cnt)
1715 { 1690 {
1716 for (j = 0; j < 8; j++) 1691 for (j = 0; j < 8; j++)
1717 if (temps[j] > 0xFF) 1692 if (temps[j] > 0xFF)
1718 { 1693 {
1719 LOG (GNUNET_ERROR_TYPE_WARNING, 1694 LOG (GNUNET_ERROR_TYPE_WARNING,
1720 _("Invalid format for IP: `%s'\n"), 1695 _ ("Invalid format for IP: `%s'\n"),
1721 &routeList[pos]); 1696 &routeList[pos]);
1722 GNUNET_free (result); 1697 GNUNET_free (result);
1723 GNUNET_free (routeList); 1698 GNUNET_free (routeList);
1724 return NULL; 1699 return NULL;
1725 } 1700 }
1726 result[i].network.s_addr = 1701 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) +
1727 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + 1702 (temps[2] << 8) + temps[3]);
1728 temps[3]); 1703 result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16) +
1729 result[i].netmask.s_addr = 1704 (temps[6] << 8) + temps[7]);
1730 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
1731 temps[7]);
1732 pos = end + 1; 1705 pos = end + 1;
1733 i++; 1706 i++;
1734 continue; 1707 continue;
1735 } 1708 }
1736 /* try second notation */ 1709 /* try second notation */
1737 cnt = 1710 cnt = sscanf (&routeList[pos],
1738 SSCANF (&routeList[pos], 1711 "%u.%u.%u.%u/%u%1s",
1739 "%u.%u.%u.%u/%u%1s", 1712 &temps[0],
1740 &temps[0], 1713 &temps[1],
1741 &temps[1], 1714 &temps[2],
1742 &temps[2], 1715 &temps[3],
1743 &temps[3], 1716 &slash,
1744 &slash, 1717 dummy);
1745 dummy);
1746 if (5 == cnt) 1718 if (5 == cnt)
1747 { 1719 {
1748 for (j = 0; j < 4; j++) 1720 for (j = 0; j < 4; j++)
1749 if (temps[j] > 0xFF) 1721 if (temps[j] > 0xFF)
1750 { 1722 {
1751 LOG (GNUNET_ERROR_TYPE_WARNING, 1723 LOG (GNUNET_ERROR_TYPE_WARNING,
1752 _("Invalid format for IP: `%s'\n"), 1724 _ ("Invalid format for IP: `%s'\n"),
1753 &routeList[pos]); 1725 &routeList[pos]);
1754 GNUNET_free (result); 1726 GNUNET_free (result);
1755 GNUNET_free (routeList); 1727 GNUNET_free (routeList);
1756 return NULL; 1728 return NULL;
1757 } 1729 }
1758 result[i].network.s_addr = 1730 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) +
1759 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + 1731 (temps[2] << 8) + temps[3]);
1760 temps[3]);
1761 if ((slash <= 32) && (slash >= 0)) 1732 if ((slash <= 32) && (slash >= 0))
1762 { 1733 {
1763 result[i].netmask.s_addr = 0; 1734 result[i].netmask.s_addr = 0;
1764 while (slash > 0) 1735 while (slash > 0)
1765 { 1736 {
1766 result[i].netmask.s_addr = 1737 result[i].netmask.s_addr =
1767 (result[i].netmask.s_addr >> 1) + 0x80000000; 1738 (result[i].netmask.s_addr >> 1) + 0x80000000;
1768 slash--; 1739 slash--;
1769 } 1740 }
1770 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); 1741 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
@@ -1775,38 +1746,36 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1775 else 1746 else
1776 { 1747 {
1777 LOG (GNUNET_ERROR_TYPE_WARNING, 1748 LOG (GNUNET_ERROR_TYPE_WARNING,
1778 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), 1749 _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1779 slash); 1750 slash);
1780 GNUNET_free (result); 1751 GNUNET_free (result);
1781 GNUNET_free (routeList); 1752 GNUNET_free (routeList);
1782 return NULL; /* error */ 1753 return NULL; /* error */
1783 } 1754 }
1784 } 1755 }
1785 /* try third notation */ 1756 /* try third notation */
1786 slash = 32; 1757 slash = 32;
1787 cnt = 1758 cnt = sscanf (&routeList[pos],
1788 SSCANF (&routeList[pos], 1759 "%u.%u.%u.%u%1s",
1789 "%u.%u.%u.%u%1s", 1760 &temps[0],
1790 &temps[0], 1761 &temps[1],
1791 &temps[1], 1762 &temps[2],
1792 &temps[2], 1763 &temps[3],
1793 &temps[3], 1764 dummy);
1794 dummy);
1795 if (4 == cnt) 1765 if (4 == cnt)
1796 { 1766 {
1797 for (j = 0; j < 4; j++) 1767 for (j = 0; j < 4; j++)
1798 if (temps[j] > 0xFF) 1768 if (temps[j] > 0xFF)
1799 { 1769 {
1800 LOG (GNUNET_ERROR_TYPE_WARNING, 1770 LOG (GNUNET_ERROR_TYPE_WARNING,
1801 _("Invalid format for IP: `%s'\n"), 1771 _ ("Invalid format for IP: `%s'\n"),
1802 &routeList[pos]); 1772 &routeList[pos]);
1803 GNUNET_free (result); 1773 GNUNET_free (result);
1804 GNUNET_free (routeList); 1774 GNUNET_free (routeList);
1805 return NULL; 1775 return NULL;
1806 } 1776 }
1807 result[i].network.s_addr = 1777 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) +
1808 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + 1778 (temps[2] << 8) + temps[3]);
1809 temps[3]);
1810 result[i].netmask.s_addr = 0; 1779 result[i].netmask.s_addr = 0;
1811 while (slash > 0) 1780 while (slash > 0)
1812 { 1781 {
@@ -1819,23 +1788,23 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1819 continue; 1788 continue;
1820 } 1789 }
1821 LOG (GNUNET_ERROR_TYPE_WARNING, 1790 LOG (GNUNET_ERROR_TYPE_WARNING,
1822 _("Invalid format for IP: `%s'\n"), 1791 _ ("Invalid format for IP: `%s'\n"),
1823 &routeList[pos]); 1792 &routeList[pos]);
1824 GNUNET_free (result); 1793 GNUNET_free (result);
1825 GNUNET_free (routeList); 1794 GNUNET_free (routeList);
1826 return NULL; /* error */ 1795 return NULL; /* error */
1827 } 1796 }
1828 if (pos < strlen (routeList)) 1797 if (pos < strlen (routeList))
1829 { 1798 {
1830 LOG (GNUNET_ERROR_TYPE_WARNING, 1799 LOG (GNUNET_ERROR_TYPE_WARNING,
1831 _("Invalid format: `%s'\n"), 1800 _ ("Invalid format: `%s'\n"),
1832 &routeListX[pos]); 1801 &routeListX[pos]);
1833 GNUNET_free (result); 1802 GNUNET_free (result);
1834 GNUNET_free (routeList); 1803 GNUNET_free (routeList);
1835 return NULL; /* oops */ 1804 return NULL; /* oops */
1836 } 1805 }
1837 GNUNET_free (routeList); 1806 GNUNET_free (routeList);
1838 return result; /* ok */ 1807 return result; /* ok */
1839} 1808}
1840 1809
1841 1810
@@ -1867,7 +1836,7 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1867 int save; 1836 int save;
1868 int colon; 1837 int colon;
1869 char dummy[2]; 1838 char dummy[2];
1870 1839
1871 if (NULL == routeListX) 1840 if (NULL == routeListX)
1872 return NULL; 1841 return NULL;
1873 len = strlen (routeListX); 1842 len = strlen (routeListX);
@@ -1881,13 +1850,14 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1881 if (';' != routeList[len - 1]) 1850 if (';' != routeList[len - 1])
1882 { 1851 {
1883 LOG (GNUNET_ERROR_TYPE_WARNING, 1852 LOG (GNUNET_ERROR_TYPE_WARNING,
1884 _("Invalid network notation (does not end with ';': `%s')\n"), 1853 _ ("Invalid network notation (does not end with ';': `%s')\n"),
1885 routeList); 1854 routeList);
1886 GNUNET_free (routeList); 1855 GNUNET_free (routeList);
1887 return NULL; 1856 return NULL;
1888 } 1857 }
1889 1858
1890 result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv6NetworkPolicy) * (count + 1)); 1859 result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv6NetworkPolicy) *
1860 (count + 1));
1891 i = 0; 1861 i = 0;
1892 pos = 0; 1862 pos = 0;
1893 while (i < count) 1863 while (i < count)
@@ -1901,9 +1871,7 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1901 1871
1902 if (slash < start) 1872 if (slash < start)
1903 { 1873 {
1904 memset (&result[i].netmask, 1874 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
1905 0xFF,
1906 sizeof (struct in6_addr));
1907 slash = pos; 1875 slash = pos;
1908 } 1876 }
1909 else 1877 else
@@ -1915,8 +1883,8 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1915 if (':' == routeList[colon]) 1883 if (':' == routeList[colon])
1916 { 1884 {
1917 routeList[colon] = '\0'; 1885 routeList[colon] = '\0';
1918 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], 1886 if (GNUNET_OK !=
1919 &result[i].pp)) 1887 parse_port_policy (&routeList[colon + 1], &result[i].pp))
1920 { 1888 {
1921 GNUNET_free (result); 1889 GNUNET_free (result);
1922 GNUNET_free (routeList); 1890 GNUNET_free (routeList);
@@ -1927,15 +1895,12 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1927 if (ret <= 0) 1895 if (ret <= 0)
1928 { 1896 {
1929 save = errno; 1897 save = errno;
1930 if ( (1 != SSCANF (&routeList[slash + 1], 1898 if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1931 "%u%1s", 1899 (bits > 128))
1932 &bits,
1933 dummy)) ||
1934 (bits > 128) )
1935 { 1900 {
1936 if (0 == ret) 1901 if (0 == ret)
1937 LOG (GNUNET_ERROR_TYPE_WARNING, 1902 LOG (GNUNET_ERROR_TYPE_WARNING,
1938 _("Wrong format `%s' for netmask\n"), 1903 _ ("Wrong format `%s' for netmask\n"),
1939 &routeList[slash + 1]); 1904 &routeList[slash + 1]);
1940 else 1905 else
1941 { 1906 {
@@ -1955,7 +1920,7 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1955 while (bits > 0) 1920 while (bits > 0)
1956 { 1921 {
1957 result[i].netmask.s6_addr[off] = 1922 result[i].netmask.s6_addr[off] =
1958 (result[i].netmask.s6_addr[off] >> 1) + 0x80; 1923 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1959 bits--; 1924 bits--;
1960 } 1925 }
1961 } 1926 }
@@ -1966,11 +1931,10 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1966 { 1931 {
1967 if (0 == ret) 1932 if (0 == ret)
1968 LOG (GNUNET_ERROR_TYPE_WARNING, 1933 LOG (GNUNET_ERROR_TYPE_WARNING,
1969 _("Wrong format `%s' for network\n"), 1934 _ ("Wrong format `%s' for network\n"),
1970 &routeList[slash + 1]); 1935 &routeList[slash + 1]);
1971 else 1936 else
1972 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, 1937 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
1973 "inet_pton");
1974 GNUNET_free (result); 1938 GNUNET_free (result);
1975 GNUNET_free (routeList); 1939 GNUNET_free (routeList);
1976 return NULL; 1940 return NULL;
@@ -1983,12 +1947,12 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1983} 1947}
1984 1948
1985 1949
1986
1987/** ******************** Base64 encoding ***********/ 1950/** ******************** Base64 encoding ***********/
1988 1951
1989#define FILLCHAR '=' 1952#define FILLCHAR '='
1990static char *cvt = 1953static char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1991 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; 1954 "abcdefghijklmnopqrstuvwxyz"
1955 "0123456789+/";
1992 1956
1993 1957
1994/** 1958/**
@@ -2001,9 +1965,7 @@ static char *cvt =
2001 * @return the size of the output 1965 * @return the size of the output
2002 */ 1966 */
2003size_t 1967size_t
2004GNUNET_STRINGS_base64_encode (const void *in, 1968GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
2005 size_t len,
2006 char **output)
2007{ 1969{
2008 const char *data = in; 1970 const char *data = in;
2009 size_t ret; 1971 size_t ret;
@@ -2047,11 +2009,14 @@ GNUNET_STRINGS_base64_encode (const void *in,
2047 return ret; 2009 return ret;
2048} 2010}
2049 2011
2050#define cvtfind(a)( (((a) >= 'A')&&((a) <= 'Z'))? (a)-'A'\ 2012#define cvtfind(a) \
2051 :(((a)>='a')&&((a)<='z')) ? (a)-'a'+26\ 2013 ((((a) >= 'A') && ((a) <= 'Z')) \
2052 :(((a)>='0')&&((a)<='9')) ? (a)-'0'+52\ 2014 ? (a) - 'A' \
2053 :((a) == '+') ? 62\ 2015 : (((a) >= 'a') && ((a) <= 'z')) \
2054 :((a) == '/') ? 63 : -1) 2016 ? (a) - 'a' + 26 \
2017 : (((a) >= '0') && ((a) <= '9')) \
2018 ? (a) - '0' + 52 \
2019 : ((a) == '+') ? 62 : ((a) == '/') ? 63 : -1)
2055 2020
2056 2021
2057/** 2022/**
@@ -2064,18 +2029,20 @@ GNUNET_STRINGS_base64_encode (const void *in,
2064 * @return the size of the output 2029 * @return the size of the output
2065 */ 2030 */
2066size_t 2031size_t
2067GNUNET_STRINGS_base64_decode (const char *data, 2032GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **out)
2068 size_t len,
2069 void **out)
2070{ 2033{
2071 char *output; 2034 char *output;
2072 size_t ret = 0; 2035 size_t ret = 0;
2073 2036
2074#define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ 2037#define CHECK_CRLF \
2075 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, "ignoring CR/LF\n"); \ 2038 while (data[i] == '\r' || data[i] == '\n') \
2076 i++; \ 2039 { \
2077 if (i >= len) goto END; \ 2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
2078 } 2041 "ignoring CR/LF\n"); \
2042 i++; \
2043 if (i >= len) \
2044 goto END; \
2045 }
2079 2046
2080 output = GNUNET_malloc ((len * 3 / 4) + 8); 2047 output = GNUNET_malloc ((len * 3 / 4) + 8);
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
diff --git a/src/util/test_common_allocation.c b/src/util/test_common_allocation.c
index 476934b3f..fcc453d6b 100644
--- a/src/util/test_common_allocation.c
+++ b/src/util/test_common_allocation.c
@@ -144,7 +144,7 @@ main (int argc, char *argv[])
144 NULL); 144 NULL);
145 ret = check (); 145 ret = check ();
146 if (ret != 0) 146 if (ret != 0)
147 FPRINTF (stderr, 147 fprintf (stderr,
148 "ERROR %d.\n", 148 "ERROR %d.\n",
149 ret); 149 ret);
150 return ret; 150 return ret;
diff --git a/src/util/test_common_logging.c b/src/util/test_common_logging.c
index 396bf3966..6ae50b36b 100644
--- a/src/util/test_common_logging.c
+++ b/src/util/test_common_logging.c
@@ -44,7 +44,7 @@ main (int argc, char *argv[])
44 unsigned int logs = 0; 44 unsigned int logs = 0;
45 45
46 if (0 != putenv ("GNUNET_FORCE_LOG=")) 46 if (0 != putenv ("GNUNET_FORCE_LOG="))
47 FPRINTF (stderr, "Failed to putenv: %s\n", strerror (errno)); 47 fprintf (stderr, "Failed to putenv: %s\n", strerror (errno));
48 GNUNET_log_setup ("test-common-logging", "DEBUG", "/dev/null"); 48 GNUNET_log_setup ("test-common-logging", "DEBUG", "/dev/null");
49 GNUNET_logger_add (&my_log, &logs); 49 GNUNET_logger_add (&my_log, &logs);
50 GNUNET_logger_add (&my_log, &logs); 50 GNUNET_logger_add (&my_log, &logs);
@@ -60,7 +60,7 @@ main (int argc, char *argv[])
60 GNUNET_logger_remove (&my_log, &logs); 60 GNUNET_logger_remove (&my_log, &logs);
61 if (logs != 4) 61 if (logs != 4)
62 { 62 {
63 FPRINTF (stdout, "Expected 4 log calls, got %u\n", logs); 63 fprintf (stdout, "Expected 4 log calls, got %u\n", logs);
64 failureCount++; 64 failureCount++;
65 } 65 }
66 GNUNET_break (0 == 66 GNUNET_break (0 ==
@@ -84,13 +84,13 @@ main (int argc, char *argv[])
84 GNUNET_logger_remove (&my_log, &logs); 84 GNUNET_logger_remove (&my_log, &logs);
85 if (logs != 1) 85 if (logs != 1)
86 { 86 {
87 FPRINTF (stdout, "Expected 1 log call, got %u\n", logs); 87 fprintf (stdout, "Expected 1 log call, got %u\n", logs);
88 failureCount++; 88 failureCount++;
89 } 89 }
90 90
91 if (failureCount != 0) 91 if (failureCount != 0)
92 { 92 {
93 FPRINTF (stdout, "%u TESTS FAILED!\n", failureCount); 93 fprintf (stdout, "%u TESTS FAILED!\n", failureCount);
94 return -1; 94 return -1;
95 } 95 }
96 return 0; 96 return 0;
diff --git a/src/util/test_common_logging_dummy.c b/src/util/test_common_logging_dummy.c
index 5c3709206..d3c6e3452 100644
--- a/src/util/test_common_logging_dummy.c
+++ b/src/util/test_common_logging_dummy.c
@@ -51,7 +51,7 @@ my_log (void *ctx,
51 (void) date; 51 (void) date;
52 if (strncmp ("test-common-logging-dummy", component, 25) != 0) 52 if (strncmp ("test-common-logging-dummy", component, 25) != 0)
53 return; 53 return;
54 FPRINTF (stdout, "%s", msg); 54 fprintf (stdout, "%s", msg);
55 fflush (stdout); 55 fflush (stdout);
56} 56}
57 57
diff --git a/src/util/test_common_logging_runtime_loglevels.c b/src/util/test_common_logging_runtime_loglevels.c
index 7c72acf7d..8da808760 100644
--- a/src/util/test_common_logging_runtime_loglevels.c
+++ b/src/util/test_common_logging_runtime_loglevels.c
@@ -237,7 +237,7 @@ read_call (void *cls)
237 buf_ptr += rd; 237 buf_ptr += rd;
238 bytes += rd; 238 bytes += rd;
239#if VERBOSE 239#if VERBOSE
240 FPRINTF (stderr, "got %d bytes, reading more\n", rd); 240 fprintf (stderr, "got %d bytes, reading more\n", rd);
241#endif 241#endif
242 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 242 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
243 stdout_read_handle, 243 stdout_read_handle,
@@ -247,7 +247,7 @@ read_call (void *cls)
247 } 247 }
248 248
249#if VERBOSE 249#if VERBOSE
250 FPRINTF (stderr, "bytes is %d:%s\n", bytes, buf); 250 fprintf (stderr, "bytes is %d:%s\n", bytes, buf);
251#endif 251#endif
252 252
253 /* +------CHILD OUTPUT-- 253 /* +------CHILD OUTPUT--
@@ -334,7 +334,7 @@ read_call (void *cls)
334 &bytes, &delays[7], level))) 334 &bytes, &delays[7], level)))
335 { 335 {
336 if (bytes == LOG_BUFFER_SIZE) 336 if (bytes == LOG_BUFFER_SIZE)
337 FPRINTF (stderr, "%s", "Ran out of buffer space!\n"); 337 fprintf (stderr, "%s", "Ran out of buffer space!\n");
338 GNUNET_break (0); 338 GNUNET_break (0);
339 ok = 2; 339 ok = 2;
340 GNUNET_SCHEDULER_cancel (die_task); 340 GNUNET_SCHEDULER_cancel (die_task);
diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c
index 96f13eefe..67e532f40 100644
--- a/src/util/test_configuration.c
+++ b/src/util/test_configuration.c
@@ -236,14 +236,14 @@ checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
236 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData); 236 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
237 if (1 == (ret = cbData.status)) 237 if (1 == (ret = cbData.status))
238 { 238 {
239 FPRINTF (stderr, "%s", 239 fprintf (stderr, "%s",
240 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n"); 240 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
241 goto housekeeping; 241 goto housekeeping;
242 } 242 }
243 cbData.cfgDiffs = cfg; 243 cbData.cfgDiffs = cfg;
244 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData); 244 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
245 if ((ret = cbData.status) == 1) 245 if ((ret = cbData.status) == 1)
246 FPRINTF (stderr, "%s", 246 fprintf (stderr, "%s",
247 "Incorrect Configuration Diffs: Data may be missing in diffs\n"); 247 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
248 248
249housekeeping: 249housekeeping:
@@ -273,7 +273,7 @@ testConfig ()
273 return 1; 273 return 1;
274 if (0 != strcmp ("b", c)) 274 if (0 != strcmp ("b", c))
275 { 275 {
276 FPRINTF (stderr, "Got `%s'\n", c); 276 fprintf (stderr, "Got `%s'\n", c);
277 GNUNET_free (c); 277 GNUNET_free (c);
278 return 2; 278 return 2;
279 } 279 }
@@ -466,7 +466,7 @@ main (int argc, char *argv[])
466 if (GNUNET_OK != 466 if (GNUNET_OK !=
467 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf")) 467 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
468 { 468 {
469 FPRINTF (stderr, "%s", "Failed to parse configuration file\n"); 469 fprintf (stderr, "%s", "Failed to parse configuration file\n");
470 GNUNET_CONFIGURATION_destroy (cfg); 470 GNUNET_CONFIGURATION_destroy (cfg);
471 return 1; 471 return 1;
472 } 472 }
@@ -480,12 +480,12 @@ main (int argc, char *argv[])
480 480
481 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf")) 481 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
482 { 482 {
483 FPRINTF (stderr, "%s", "Failed to write configuration file\n"); 483 fprintf (stderr, "%s", "Failed to write configuration file\n");
484 GNUNET_CONFIGURATION_destroy (cfg); 484 GNUNET_CONFIGURATION_destroy (cfg);
485 return 1; 485 return 1;
486 } 486 }
487 GNUNET_CONFIGURATION_destroy (cfg); 487 GNUNET_CONFIGURATION_destroy (cfg);
488 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf")); 488 GNUNET_assert (0 == unlink ("/tmp/gnunet-test.conf"));
489 489
490 cfg = GNUNET_CONFIGURATION_create (); 490 cfg = GNUNET_CONFIGURATION_create ();
491 if (GNUNET_OK != 491 if (GNUNET_OK !=
@@ -542,7 +542,7 @@ main (int argc, char *argv[])
542error: 542error:
543 if (failureCount != 0) 543 if (failureCount != 0)
544 { 544 {
545 FPRINTF (stderr, "Test failed: %u\n", failureCount); 545 fprintf (stderr, "Test failed: %u\n", failureCount);
546 return 1; 546 return 1;
547 } 547 }
548 return 0; 548 return 0;
diff --git a/src/util/test_container_bloomfilter.c b/src/util/test_container_bloomfilter.c
index 42849af97..043eacafd 100644
--- a/src/util/test_container_bloomfilter.c
+++ b/src/util/test_container_bloomfilter.c
@@ -35,13 +35,13 @@
35 * Generate a random hashcode. 35 * Generate a random hashcode.
36 */ 36 */
37static void 37static void
38nextHC (struct GNUNET_HashCode * hc) 38nextHC (struct GNUNET_HashCode *hc)
39{ 39{
40 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, hc); 40 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, hc);
41} 41}
42 42
43static int 43static int
44add_iterator (void *cls, struct GNUNET_HashCode * next) 44add_iterator (void *cls, struct GNUNET_HashCode *next)
45{ 45{
46 int *ret = cls; 46 int *ret = cls;
47 struct GNUNET_HashCode pos; 47 struct GNUNET_HashCode pos;
@@ -68,8 +68,8 @@ main (int argc, char *argv[])
68 68
69 GNUNET_log_setup ("test-container-bloomfilter", "WARNING", NULL); 69 GNUNET_log_setup ("test-container-bloomfilter", "WARNING", NULL);
70 GNUNET_CRYPTO_seed_weak_random (1); 70 GNUNET_CRYPTO_seed_weak_random (1);
71 if (0 == STAT (TESTFILE, &sbuf)) 71 if (0 == stat (TESTFILE, &sbuf))
72 if (0 != UNLINK (TESTFILE)) 72 if (0 != unlink (TESTFILE))
73 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", TESTFILE); 73 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", TESTFILE);
74 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K); 74 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K);
75 75
@@ -88,7 +88,9 @@ main (int argc, char *argv[])
88 } 88 }
89 if (ok1 != 200) 89 if (ok1 != 200)
90 { 90 {
91 printf ("Got %d elements out of" "200 expected after insertion.\n", ok1); 91 printf ("Got %d elements out of"
92 "200 expected after insertion.\n",
93 ok1);
92 GNUNET_CONTAINER_bloomfilter_free (bf); 94 GNUNET_CONTAINER_bloomfilter_free (bf);
93 return -1; 95 return -1;
94 } 96 }
@@ -118,7 +120,9 @@ main (int argc, char *argv[])
118 } 120 }
119 if (ok1 != 200) 121 if (ok1 != 200)
120 { 122 {
121 printf ("Got %d elements out of 200 " "expected after reloading.\n", ok1); 123 printf ("Got %d elements out of 200 "
124 "expected after reloading.\n",
125 ok1);
122 GNUNET_CONTAINER_bloomfilter_free (bf); 126 GNUNET_CONTAINER_bloomfilter_free (bf);
123 GNUNET_CONTAINER_bloomfilter_free (bfi); 127 GNUNET_CONTAINER_bloomfilter_free (bfi);
124 return -1; 128 return -1;
@@ -126,7 +130,8 @@ main (int argc, char *argv[])
126 130
127 if (ok2 != 200) 131 if (ok2 != 200)
128 { 132 {
129 printf ("Got %d elements out of 200 " "expected after initialization.\n", 133 printf ("Got %d elements out of 200 "
134 "expected after initialization.\n",
130 ok2); 135 ok2);
131 GNUNET_CONTAINER_bloomfilter_free (bf); 136 GNUNET_CONTAINER_bloomfilter_free (bf);
132 GNUNET_CONTAINER_bloomfilter_free (bfi); 137 GNUNET_CONTAINER_bloomfilter_free (bfi);
@@ -157,7 +162,8 @@ main (int argc, char *argv[])
157 if (ok1 != 100) 162 if (ok1 != 100)
158 { 163 {
159 printf ("Expected 100 elements in loaded filter" 164 printf ("Expected 100 elements in loaded filter"
160 " after adding 200 and deleting 100, got %d\n", ok1); 165 " after adding 200 and deleting 100, got %d\n",
166 ok1);
161 GNUNET_CONTAINER_bloomfilter_free (bf); 167 GNUNET_CONTAINER_bloomfilter_free (bf);
162 GNUNET_CONTAINER_bloomfilter_free (bfi); 168 GNUNET_CONTAINER_bloomfilter_free (bfi);
163 return -1; 169 return -1;
@@ -220,7 +226,8 @@ main (int argc, char *argv[])
220 if (ok1 != 20) 226 if (ok1 != 20)
221 { 227 {
222 printf ("Expected 20 elements in resized file-backed filter" 228 printf ("Expected 20 elements in resized file-backed filter"
223 " after adding 20, got %d\n", ok1); 229 " after adding 20, got %d\n",
230 ok1);
224 GNUNET_CONTAINER_bloomfilter_free (bf); 231 GNUNET_CONTAINER_bloomfilter_free (bf);
225 GNUNET_CONTAINER_bloomfilter_free (bfi); 232 GNUNET_CONTAINER_bloomfilter_free (bfi);
226 return -1; 233 return -1;
@@ -228,7 +235,8 @@ main (int argc, char *argv[])
228 if (ok2 != 20) 235 if (ok2 != 20)
229 { 236 {
230 printf ("Expected 20 elements in resized filter" 237 printf ("Expected 20 elements in resized filter"
231 " after adding 20, got %d\n", ok2); 238 " after adding 20, got %d\n",
239 ok2);
232 GNUNET_CONTAINER_bloomfilter_free (bf); 240 GNUNET_CONTAINER_bloomfilter_free (bf);
233 GNUNET_CONTAINER_bloomfilter_free (bfi); 241 GNUNET_CONTAINER_bloomfilter_free (bfi);
234 return -1; 242 return -1;
@@ -238,6 +246,6 @@ main (int argc, char *argv[])
238 GNUNET_CONTAINER_bloomfilter_free (bf); 246 GNUNET_CONTAINER_bloomfilter_free (bf);
239 GNUNET_CONTAINER_bloomfilter_free (bfi); 247 GNUNET_CONTAINER_bloomfilter_free (bfi);
240 248
241 GNUNET_break (0 == UNLINK (TESTFILE)); 249 GNUNET_break (0 == unlink (TESTFILE));
242 return 0; 250 return 0;
243} 251}
diff --git a/src/util/test_crypto_ecc_dlog.c b/src/util/test_crypto_ecc_dlog.c
index 0b0b8d57c..c0828c354 100644
--- a/src/util/test_crypto_ecc_dlog.c
+++ b/src/util/test_crypto_ecc_dlog.c
@@ -172,7 +172,7 @@ main (int argc, char *argv[])
172 172
173 if (! gcry_check_version ("1.6.0")) 173 if (! gcry_check_version ("1.6.0"))
174 { 174 {
175 FPRINTF (stderr, 175 fprintf (stderr,
176 _ 176 _
177 ("libgcrypt has not the expected version (version %s is required).\n"), 177 ("libgcrypt has not the expected version (version %s is required).\n"),
178 "1.6.0"); 178 "1.6.0");
diff --git a/src/util/test_crypto_ecdh_eddsa.c b/src/util/test_crypto_ecdh_eddsa.c
index 721f831d0..00f85033e 100644
--- a/src/util/test_crypto_ecdh_eddsa.c
+++ b/src/util/test_crypto_ecdh_eddsa.c
@@ -74,7 +74,7 @@ main (int argc, char *argv[])
74{ 74{
75 if (! gcry_check_version ("1.6.0")) 75 if (! gcry_check_version ("1.6.0"))
76 { 76 {
77 FPRINTF (stderr, 77 fprintf (stderr,
78 _("libgcrypt has not the expected version (version %s is required).\n"), 78 _("libgcrypt has not the expected version (version %s is required).\n"),
79 "1.6.0"); 79 "1.6.0");
80 return 0; 80 return 0;
diff --git a/src/util/test_crypto_ecdhe.c b/src/util/test_crypto_ecdhe.c
index c4392ca5a..71fe9b570 100644
--- a/src/util/test_crypto_ecdhe.c
+++ b/src/util/test_crypto_ecdhe.c
@@ -40,7 +40,7 @@ main (int argc, char *argv[])
40 40
41 if (! gcry_check_version ("1.6.0")) 41 if (! gcry_check_version ("1.6.0"))
42 { 42 {
43 FPRINTF (stderr, 43 fprintf (stderr,
44 _ 44 _
45 ("libgcrypt has not the expected version (version %s is required).\n"), 45 ("libgcrypt has not the expected version (version %s is required).\n"),
46 "1.6.0"); 46 "1.6.0");
diff --git a/src/util/test_crypto_ecdsa.c b/src/util/test_crypto_ecdsa.c
index 8fa5eb486..00cc72af4 100644
--- a/src/util/test_crypto_ecdsa.c
+++ b/src/util/test_crypto_ecdsa.c
@@ -46,7 +46,7 @@ testSignVerify ()
46 struct GNUNET_TIME_Absolute start; 46 struct GNUNET_TIME_Absolute start;
47 int ok = GNUNET_OK; 47 int ok = GNUNET_OK;
48 48
49 FPRINTF (stderr, "%s", "W"); 49 fprintf (stderr, "%s", "W");
50 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey); 50 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
51 start = GNUNET_TIME_absolute_get (); 51 start = GNUNET_TIME_absolute_get ();
52 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); 52 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
@@ -54,10 +54,10 @@ testSignVerify ()
54 54
55 for (i = 0; i < ITER; i++) 55 for (i = 0; i < ITER; i++)
56 { 56 {
57 FPRINTF (stderr, "%s", "."); fflush (stderr); 57 fprintf (stderr, "%s", "."); fflush (stderr);
58 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig)) 58 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
59 { 59 {
60 FPRINTF (stderr, 60 fprintf (stderr,
61 "%s", 61 "%s",
62 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n"); 62 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
63 ok = GNUNET_SYSERR; 63 ok = GNUNET_SYSERR;
@@ -103,7 +103,7 @@ testDeriveSignVerify ()
103 103
104 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig)) 104 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig))
105 { 105 {
106 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n"); 106 fprintf (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
107 GNUNET_free (dpriv); 107 GNUNET_free (dpriv);
108 return GNUNET_SYSERR; 108 return GNUNET_SYSERR;
109 } 109 }
@@ -151,15 +151,15 @@ testSignPerformance ()
151 151
152 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); 152 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
153 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); 153 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
154 FPRINTF (stderr, "%s", "W"); 154 fprintf (stderr, "%s", "W");
155 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey); 155 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
156 start = GNUNET_TIME_absolute_get (); 156 start = GNUNET_TIME_absolute_get ();
157 for (i = 0; i < ITER; i++) 157 for (i = 0; i < ITER; i++)
158 { 158 {
159 FPRINTF (stderr, "%s", "."); fflush (stderr); 159 fprintf (stderr, "%s", "."); fflush (stderr);
160 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig)) 160 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
161 { 161 {
162 FPRINTF (stderr, "%s", 162 fprintf (stderr, "%s",
163 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n"); 163 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
164 ok = GNUNET_SYSERR; 164 ok = GNUNET_SYSERR;
165 continue; 165 continue;
@@ -180,7 +180,7 @@ perf_keygen ()
180 struct GNUNET_CRYPTO_EcdsaPrivateKey *pk; 180 struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
181 int i; 181 int i;
182 182
183 FPRINTF (stderr, "%s", "W"); 183 fprintf (stderr, "%s", "W");
184 start = GNUNET_TIME_absolute_get (); 184 start = GNUNET_TIME_absolute_get ();
185 for (i=0;i<10;i++) 185 for (i=0;i<10;i++)
186 { 186 {
@@ -203,7 +203,7 @@ main (int argc, char *argv[])
203 203
204 if (! gcry_check_version ("1.6.0")) 204 if (! gcry_check_version ("1.6.0"))
205 { 205 {
206 FPRINTF (stderr, 206 fprintf (stderr,
207 _ 207 _
208 ("libgcrypt has not the expected version (version %s is required).\n"), 208 ("libgcrypt has not the expected version (version %s is required).\n"),
209 "1.6.0"); 209 "1.6.0");
diff --git a/src/util/test_crypto_eddsa.c b/src/util/test_crypto_eddsa.c
index e738f7e6c..acb5fbdf5 100644
--- a/src/util/test_crypto_eddsa.c
+++ b/src/util/test_crypto_eddsa.c
@@ -48,7 +48,7 @@ testSignVerify ()
48 struct GNUNET_TIME_Absolute start; 48 struct GNUNET_TIME_Absolute start;
49 int ok = GNUNET_OK; 49 int ok = GNUNET_OK;
50 50
51 FPRINTF (stderr, "%s", "W"); 51 fprintf (stderr, "%s", "W");
52 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey); 52 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
53 start = GNUNET_TIME_absolute_get (); 53 start = GNUNET_TIME_absolute_get ();
54 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); 54 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
@@ -56,10 +56,10 @@ testSignVerify ()
56 56
57 for (i = 0; i < ITER; i++) 57 for (i = 0; i < ITER; i++)
58 { 58 {
59 FPRINTF (stderr, "%s", "."); fflush (stderr); 59 fprintf (stderr, "%s", "."); fflush (stderr);
60 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig)) 60 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
61 { 61 {
62 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n"); 62 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
63 ok = GNUNET_SYSERR; 63 ok = GNUNET_SYSERR;
64 continue; 64 continue;
65 } 65 }
@@ -99,15 +99,15 @@ testSignPerformance ()
99 99
100 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); 100 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
101 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); 101 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
102 FPRINTF (stderr, "%s", "W"); 102 fprintf (stderr, "%s", "W");
103 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey); 103 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
104 start = GNUNET_TIME_absolute_get (); 104 start = GNUNET_TIME_absolute_get ();
105 for (i = 0; i < ITER; i++) 105 for (i = 0; i < ITER; i++)
106 { 106 {
107 FPRINTF (stderr, "%s", "."); fflush (stderr); 107 fprintf (stderr, "%s", "."); fflush (stderr);
108 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig)) 108 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
109 { 109 {
110 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n"); 110 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
111 ok = GNUNET_SYSERR; 111 ok = GNUNET_SYSERR;
112 continue; 112 continue;
113 } 113 }
@@ -135,7 +135,7 @@ testCreateFromFile ()
135 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2); 135 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
136 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1))); 136 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
137 GNUNET_free (key); 137 GNUNET_free (key);
138 GNUNET_assert (0 == UNLINK (KEYFILE)); 138 GNUNET_assert (0 == unlink (KEYFILE));
139 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE); 139 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
140 GNUNET_assert (NULL != key); 140 GNUNET_assert (NULL != key);
141 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2); 141 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
@@ -152,7 +152,7 @@ perf_keygen ()
152 struct GNUNET_CRYPTO_EddsaPrivateKey *pk; 152 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
153 int i; 153 int i;
154 154
155 FPRINTF (stderr, "%s", "W"); 155 fprintf (stderr, "%s", "W");
156 start = GNUNET_TIME_absolute_get (); 156 start = GNUNET_TIME_absolute_get ();
157 for (i=0;i<10;i++) 157 for (i=0;i<10;i++)
158 { 158 {
@@ -175,7 +175,7 @@ main (int argc, char *argv[])
175 175
176 if (! gcry_check_version ("1.6.0")) 176 if (! gcry_check_version ("1.6.0"))
177 { 177 {
178 FPRINTF (stderr, 178 fprintf (stderr,
179 _("libgcrypt has not the expected version (version %s is required).\n"), 179 _("libgcrypt has not the expected version (version %s is required).\n"),
180 "1.6.0"); 180 "1.6.0");
181 return 0; 181 return 0;
@@ -193,7 +193,7 @@ main (int argc, char *argv[])
193 GNUNET_free (key); 193 GNUNET_free (key);
194 if (GNUNET_OK != testCreateFromFile ()) 194 if (GNUNET_OK != testCreateFromFile ())
195 failure_count++; 195 failure_count++;
196 GNUNET_assert (0 == UNLINK (KEYFILE)); 196 GNUNET_assert (0 == unlink (KEYFILE));
197 perf_keygen (); 197 perf_keygen ();
198 198
199 if (0 != failure_count) 199 if (0 != failure_count)
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c
index 52e24718d..e08348e53 100644
--- a/src/util/test_crypto_hash.c
+++ b/src/util/test_crypto_hash.c
@@ -130,12 +130,12 @@ testFileHash ()
130 130
131 memset (block, 42, sizeof (block) / 2); 131 memset (block, 42, sizeof (block) / 2);
132 memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2); 132 memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2);
133 GNUNET_assert (NULL != (f = FOPEN (FILENAME, "w+"))); 133 GNUNET_assert (NULL != (f = fopen (FILENAME, "w+")));
134 GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f)); 134 GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f));
135 GNUNET_break (0 == FCLOSE (f)); 135 GNUNET_break (0 == fclose (f));
136 ret = 1; 136 ret = 1;
137 GNUNET_SCHEDULER_run (&file_hasher, &ret); 137 GNUNET_SCHEDULER_run (&file_hasher, &ret);
138 GNUNET_break (0 == UNLINK (FILENAME)); 138 GNUNET_break (0 == unlink (FILENAME));
139 return ret; 139 return ret;
140} 140}
141 141
diff --git a/src/util/test_crypto_random.c b/src/util/test_crypto_random.c
index dec58086a..6386c79db 100644
--- a/src/util/test_crypto_random.c
+++ b/src/util/test_crypto_random.c
@@ -42,7 +42,7 @@ test (enum GNUNET_CRYPTO_Quality mode)
42 b2 = GNUNET_CRYPTO_random_permute (mode, 1024); 42 b2 = GNUNET_CRYPTO_random_permute (mode, 1024);
43 if (0 == memcmp (b2, buf, sizeof (buf))) 43 if (0 == memcmp (b2, buf, sizeof (buf)))
44 { 44 {
45 FPRINTF (stderr, "%s", "!"); 45 fprintf (stderr, "%s", "!");
46 GNUNET_free (b2); 46 GNUNET_free (b2);
47 continue; 47 continue;
48 } 48 }
diff --git a/src/util/test_disk.c b/src/util/test_disk.c
index 267d4d4f9..5746aa96e 100644
--- a/src/util/test_disk.c
+++ b/src/util/test_disk.c
@@ -45,13 +45,13 @@ testReadWrite ()
45 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1); 45 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
46 if (ret < 0) 46 if (ret < 0)
47 { 47 {
48 FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile"); 48 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
49 return 1; 49 return 1;
50 } 50 }
51 tmp[ret] = '\0'; 51 tmp[ret] = '\0';
52 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) 52 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
53 { 53 {
54 FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, 54 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
55 TESTSTRING, ".testfile"); 55 TESTSTRING, ".testfile");
56 return 1; 56 return 1;
57 } 57 }
@@ -60,20 +60,20 @@ testReadWrite ()
60 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1); 60 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
61 if (ret < 0) 61 if (ret < 0)
62 { 62 {
63 FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n", 63 fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
64 ".testfile2"); 64 ".testfile2");
65 return 1; 65 return 1;
66 } 66 }
67 tmp[ret] = '\0'; 67 tmp[ret] = '\0';
68 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) 68 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
69 { 69 {
70 FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, 70 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
71 TESTSTRING, ".testfile2"); 71 TESTSTRING, ".testfile2");
72 return 1; 72 return 1;
73 } 73 }
74 74
75 GNUNET_break (0 == UNLINK (".testfile")); 75 GNUNET_break (0 == unlink (".testfile"));
76 GNUNET_break (0 == UNLINK (".testfile2")); 76 GNUNET_break (0 == unlink (".testfile2"));
77 if (GNUNET_NO != GNUNET_DISK_file_test (".testfile")) 77 if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
78 return 1; 78 return 1;
79 79
@@ -99,7 +99,7 @@ testOpenClose ()
99 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO, GNUNET_YES)); 99 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO, GNUNET_YES));
100 if (size != 5) 100 if (size != 5)
101 return 1; 101 return 1;
102 GNUNET_break (0 == UNLINK (".testfile")); 102 GNUNET_break (0 == unlink (".testfile"));
103 103
104 return 0; 104 return 0;
105} 105}
@@ -281,7 +281,7 @@ main (int argc, char *argv[])
281 failureCount += testDirMani (); 281 failureCount += testDirMani ();
282 if (0 != failureCount) 282 if (0 != failureCount)
283 { 283 {
284 FPRINTF (stderr, 284 fprintf (stderr,
285 "\n%u TESTS FAILED!\n", 285 "\n%u TESTS FAILED!\n",
286 failureCount); 286 failureCount);
287 return -1; 287 return -1;
diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c
index 4294117a6..d9dd7121f 100644
--- a/src/util/test_getopt.c
+++ b/src/util/test_getopt.c
@@ -28,13 +28,9 @@
28static int 28static int
29testMinimal () 29testMinimal ()
30{ 30{
31 char *const emptyargv[] = { 31 char *const emptyargv[] = {"test", NULL};
32 "test",
33 NULL
34 };
35 const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = { 32 const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
36 GNUNET_GETOPT_OPTION_END 33 GNUNET_GETOPT_OPTION_END};
37 };
38 34
39 if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv)) 35 if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
40 return 1; 36 return 1;
@@ -46,19 +42,11 @@ testMinimal ()
46static int 42static int
47testVerbose () 43testVerbose ()
48{ 44{
49 char *const myargv[] = { 45 char *const myargv[] = {"test", "-V", "-V", "more", NULL};
50 "test",
51 "-V",
52 "-V",
53 "more",
54 NULL
55 };
56 unsigned int vflags = 0; 46 unsigned int vflags = 0;
57 47
58 const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = { 48 const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] =
59 GNUNET_GETOPT_option_verbose (&vflags), 49 {GNUNET_GETOPT_option_verbose (&vflags), GNUNET_GETOPT_OPTION_END};
60 GNUNET_GETOPT_OPTION_END
61 };
62 50
63 if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv)) 51 if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
64 { 52 {
@@ -77,15 +65,10 @@ testVerbose ()
77static int 65static int
78testVersion () 66testVersion ()
79{ 67{
80 char *const myargv[] = { 68 char *const myargv[] = {"test_getopt", "-v", NULL};
81 "test_getopt", 69 const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] =
82 "-v", 70 {GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
83 NULL 71 GNUNET_GETOPT_OPTION_END};
84 };
85 const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = {
86 GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
87 GNUNET_GETOPT_OPTION_END
88 };
89 72
90 if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv)) 73 if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
91 { 74 {
@@ -99,15 +82,9 @@ testVersion ()
99static int 82static int
100testAbout () 83testAbout ()
101{ 84{
102 char *const myargv[] = { 85 char *const myargv[] = {"test_getopt", "-h", NULL};
103 "test_getopt", 86 const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] =
104 "-h", 87 {GNUNET_GETOPT_option_help ("Testing"), GNUNET_GETOPT_OPTION_END};
105 NULL
106 };
107 const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = {
108 GNUNET_GETOPT_option_help ("Testing"),
109 GNUNET_GETOPT_OPTION_END
110 };
111 88
112 if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv)) 89 if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
113 { 90 {
@@ -121,31 +98,23 @@ testAbout ()
121static int 98static int
122testLogOpts () 99testLogOpts ()
123{ 100{
124 char *const myargv[] = { 101 char *const myargv[] =
125 "test_getopt", 102 {"test_getopt", "-l", "filename", "-L", "WARNING", NULL};
126 "-l", "filename",
127 "-L", "WARNING",
128 NULL
129 };
130 char *level = GNUNET_strdup ("stuff"); 103 char *level = GNUNET_strdup ("stuff");
131 char *fn = NULL; 104 char *fn = NULL;
132 105
133 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = { 106 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
134 GNUNET_GETOPT_option_logfile (&fn), 107 {GNUNET_GETOPT_option_logfile (&fn),
135 GNUNET_GETOPT_option_loglevel (&level), 108 GNUNET_GETOPT_option_loglevel (&level),
136 GNUNET_GETOPT_OPTION_END 109 GNUNET_GETOPT_OPTION_END};
137 };
138 110
139 if (5 != GNUNET_GETOPT_run ("test_getopt", 111 if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
140 logoptionlist,
141 5, myargv))
142 { 112 {
143 GNUNET_break (0); 113 GNUNET_break (0);
144 return 1; 114 return 1;
145 } 115 }
146 GNUNET_assert (NULL != fn); 116 GNUNET_assert (NULL != fn);
147 if ( (0 != strcmp (level, "WARNING")) || 117 if ((0 != strcmp (level, "WARNING")) || (NULL == strstr (fn, "/filename")))
148 (NULL == strstr (fn, "/filename")) )
149 { 118 {
150 GNUNET_break (0); 119 GNUNET_break (0);
151 GNUNET_free (level); 120 GNUNET_free (level);
@@ -161,47 +130,23 @@ testLogOpts ()
161static int 130static int
162testFlagNum () 131testFlagNum ()
163{ 132{
164 char *const myargv[] = { 133 char *const myargv[] = {"test_getopt", "-f", "-n", "42", "-N", "42", NULL};
165 "test_getopt",
166 "-f",
167 "-n", "42",
168 "-N", "42",
169 NULL
170 };
171 int flag = 0; 134 int flag = 0;
172 unsigned int num = 0; 135 unsigned int num = 0;
173 unsigned long long lnum = 0; 136 unsigned long long lnum = 0;
174 137
175 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = { 138 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
176 GNUNET_GETOPT_option_flag ('f', 139 {GNUNET_GETOPT_option_flag ('f', "--flag", "helptext", &flag),
177 "--flag", 140 GNUNET_GETOPT_option_uint ('n', "--num", "ARG", "helptext", &num),
178 "helptext", 141 GNUNET_GETOPT_option_ulong ('N', "--lnum", "ARG", "helptext", &lnum),
179 &flag), 142 GNUNET_GETOPT_OPTION_END};
180 GNUNET_GETOPT_option_uint ('n', 143
181 "--num", 144 if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
182 "ARG",
183 "helptext",
184 &num),
185 GNUNET_GETOPT_option_ulong ('N',
186 "--lnum",
187 "ARG",
188 "helptext",
189 &lnum),
190 GNUNET_GETOPT_OPTION_END
191 };
192
193 if (6 !=
194 GNUNET_GETOPT_run ("test_getopt",
195 logoptionlist,
196 6,
197 myargv))
198 { 145 {
199 GNUNET_break (0); 146 GNUNET_break (0);
200 return 1; 147 return 1;
201 } 148 }
202 if ( (1 != flag) || 149 if ((1 != flag) || (42 != num) || (42 != lnum))
203 (42 != num) ||
204 (42 != lnum))
205 { 150 {
206 GNUNET_break (0); 151 GNUNET_break (0);
207 return 1; 152 return 1;
@@ -215,12 +160,10 @@ main (int argc, char *argv[])
215{ 160{
216 int errCnt = 0; 161 int errCnt = 0;
217 162
218 GNUNET_log_setup ("test_getopt", 163 GNUNET_log_setup ("test_getopt", "WARNING", NULL);
219 "WARNING",
220 NULL);
221 /* suppress output from -h, -v options */ 164 /* suppress output from -h, -v options */
222#ifndef MINGW 165#ifndef MINGW
223 GNUNET_break (0 == CLOSE (1)); 166 GNUNET_break (0 == close (1));
224#endif 167#endif
225 if (0 != testMinimal ()) 168 if (0 != testMinimal ())
226 errCnt++; 169 errCnt++;
diff --git a/src/util/test_peer.c b/src/util/test_peer.c
index 248836b4c..b059f2cf3 100644
--- a/src/util/test_peer.c
+++ b/src/util/test_peer.c
@@ -65,7 +65,7 @@ check ()
65 pid = GNUNET_PEER_intern (&pidArr[i]); 65 pid = GNUNET_PEER_intern (&pidArr[i]);
66 if (pid != (i + 1)) 66 if (pid != (i + 1))
67 { 67 {
68 FPRINTF (stderr, "%s", "Unexpected Peer ID returned by intern function\n"); 68 fprintf (stderr, "%s", "Unexpected Peer ID returned by intern function\n");
69 return 1; 69 return 1;
70 } 70 }
71 } 71 }
@@ -76,7 +76,7 @@ check ()
76 pid = GNUNET_PEER_intern (&pidArr[i]); 76 pid = GNUNET_PEER_intern (&pidArr[i]);
77 if (pid != (i + 1)) 77 if (pid != (i + 1))
78 { 78 {
79 FPRINTF (stderr, "%s", "Unexpected Peer ID returned by intern function\n"); 79 fprintf (stderr, "%s", "Unexpected Peer ID returned by intern function\n");
80 return 1; 80 return 1;
81 } 81 }
82 } 82 }
diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c
index 686301694..f4184aa84 100644
--- a/src/util/test_resolver_api.c
+++ b/src/util/test_resolver_api.c
@@ -229,7 +229,7 @@ run (void *cls, char *const *args, const char *cfgfile,
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 _("gethostbyname() could not lookup IP address: %s\n"), 230 _("gethostbyname() could not lookup IP address: %s\n"),
231 hstrerror (h_errno)); 231 hstrerror (h_errno));
232 FPRINTF (stderr, 232 fprintf (stderr,
233 "%s", 233 "%s",
234 "System seems to be off-line, will not run all DNS tests\n"); 234 "System seems to be off-line, will not run all DNS tests\n");
235 *ok = 0; /* mark test as passing anyway */ 235 *ok = 0; /* mark test as passing anyway */
@@ -373,7 +373,7 @@ main (int argc, char *argv[])
373 GNUNET_OS_process_destroy (proc); 373 GNUNET_OS_process_destroy (proc);
374 proc = NULL; 374 proc = NULL;
375 if (0 != ok) 375 if (0 != ok)
376 FPRINTF (stderr, "Missed some resolutions: %u\n", ok); 376 fprintf (stderr, "Missed some resolutions: %u\n", ok);
377 return ok; 377 return ok;
378} 378}
379 379
diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c
index c38af8a0a..234a2aae1 100644
--- a/src/util/test_scheduler.c
+++ b/src/util/test_scheduler.c
@@ -224,7 +224,7 @@ taskSig (void *cls)
224 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), 224 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
225 &taskNeverRun, 225 &taskNeverRun,
226 NULL); 226 NULL);
227 GNUNET_break (0 == PLIBC_KILL (getpid (), 227 GNUNET_break (0 == kill (getpid (),
228 GNUNET_TERM_SIG)); 228 GNUNET_TERM_SIG));
229} 229}
230 230
diff --git a/src/util/test_scheduler_delay.c b/src/util/test_scheduler_delay.c
index 6c25531f2..1ec86c647 100644
--- a/src/util/test_scheduler_delay.c
+++ b/src/util/test_scheduler_delay.c
@@ -55,10 +55,10 @@ test_task (void *cls)
55 target = 55 target =
56 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply 56 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
57 (GNUNET_TIME_UNIT_MICROSECONDS, i)); 57 (GNUNET_TIME_UNIT_MICROSECONDS, i));
58 FPRINTF (stderr, "%s", "."); 58 fprintf (stderr, "%s", ".");
59 if (i > MAXV) 59 if (i > MAXV)
60 { 60 {
61 FPRINTF (stderr, "%s", "\n"); 61 fprintf (stderr, "%s", "\n");
62 return; 62 return;
63 } 63 }
64 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 64 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
@@ -77,17 +77,17 @@ main (int argc, char *argv[])
77 NULL); 77 NULL);
78 target = GNUNET_TIME_absolute_get (); 78 target = GNUNET_TIME_absolute_get ();
79 GNUNET_SCHEDULER_run (&test_task, NULL); 79 GNUNET_SCHEDULER_run (&test_task, NULL);
80 FPRINTF (stdout, 80 fprintf (stdout,
81 "Sleep precision: %llu microseconds (average delta). ", 81 "Sleep precision: %llu microseconds (average delta). ",
82 cumDelta / (MAXV / INCR)); 82 cumDelta / (MAXV / INCR));
83 if (cumDelta <= 500 * MAXV / INCR) 83 if (cumDelta <= 500 * MAXV / INCR)
84 FPRINTF (stdout, "%s", "Timer precision is excellent.\n"); 84 fprintf (stdout, "%s", "Timer precision is excellent.\n");
85 else if (cumDelta <= 5000 * MAXV / INCR) /* 5 ms average deviation */ 85 else if (cumDelta <= 5000 * MAXV / INCR) /* 5 ms average deviation */
86 FPRINTF (stdout, "%s", "Timer precision is good.\n"); 86 fprintf (stdout, "%s", "Timer precision is good.\n");
87 else if (cumDelta > 25000 * MAXV / INCR) 87 else if (cumDelta > 25000 * MAXV / INCR)
88 FPRINTF (stdout, "%s", "Timer precision is awful.\n"); 88 fprintf (stdout, "%s", "Timer precision is awful.\n");
89 else 89 else
90 FPRINTF (stdout, "%s", "Timer precision is acceptable.\n"); 90 fprintf (stdout, "%s", "Timer precision is acceptable.\n");
91 return 0; 91 return 0;
92} 92}
93 93
diff --git a/src/util/test_service.c b/src/util/test_service.c
index 72bd1f371..dc8bc59c1 100644
--- a/src/util/test_service.c
+++ b/src/util/test_service.c
@@ -222,7 +222,7 @@ main (int argc,
222 "socket"); 222 "socket");
223 return 1; 223 return 1;
224 } 224 }
225 FPRINTF (stderr, 225 fprintf (stderr,
226 "IPv6 support seems to not be available (%s), not testing it!\n", 226 "IPv6 support seems to not be available (%s), not testing it!\n",
227 strerror (errno)); 227 strerror (errno));
228 } 228 }
diff --git a/src/util/test_strings.c b/src/util/test_strings.c
index 652c3be23..9e4e808ef 100644
--- a/src/util/test_strings.c
+++ b/src/util/test_strings.c
@@ -93,7 +93,7 @@ main (int argc, char *argv[])
93 * however, the "0:05 19" should always be there; hence: */ 93 * however, the "0:05 19" should always be there; hence: */
94 if (NULL == strstr (bc, "0:05 19")) 94 if (NULL == strstr (bc, "0:05 19"))
95 { 95 {
96 FPRINTF (stderr, "Got %s\n", bc); 96 fprintf (stderr, "Got %s\n", bc);
97 GNUNET_break (0); 97 GNUNET_break (0);
98 return 1; 98 return 1;
99 } 99 }
diff --git a/src/util/time.c b/src/util/time.c
index 758921718..85a781aff 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -37,7 +37,7 @@
37#endif 37#endif
38#endif 38#endif
39 39
40#define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__) 40#define LOG(kind, ...) GNUNET_log_from (kind, "util-time", __VA_ARGS__)
41 41
42/** 42/**
43 * Variable used to simulate clock skew. Used for testing, never in production. 43 * Variable used to simulate clock skew. Used for testing, never in production.
@@ -120,10 +120,10 @@ GNUNET_TIME_absolute_get ()
120 struct GNUNET_TIME_Absolute ret; 120 struct GNUNET_TIME_Absolute ret;
121 struct timeval tv; 121 struct timeval tv;
122 122
123 GETTIMEOFDAY (&tv, NULL); 123 gettimeofday (&tv, NULL);
124 ret.abs_value_us = 124 ret.abs_value_us = (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) +
125 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) + 125 ((uint64_t) tv.tv_usec)) +
126 ((uint64_t) tv.tv_usec)) + timestamp_offset; 126 timestamp_offset;
127 return ret; 127 return ret;
128} 128}
129 129
@@ -158,7 +158,7 @@ GNUNET_TIME_absolute_get_zero_ ()
158struct GNUNET_TIME_Relative 158struct GNUNET_TIME_Relative
159GNUNET_TIME_relative_get_unit_ () 159GNUNET_TIME_relative_get_unit_ ()
160{ 160{
161 static struct GNUNET_TIME_Relative one = { 1 }; 161 static struct GNUNET_TIME_Relative one = {1};
162 162
163 return one; 163 return one;
164} 164}
@@ -170,7 +170,7 @@ GNUNET_TIME_relative_get_unit_ ()
170struct GNUNET_TIME_Relative 170struct GNUNET_TIME_Relative
171GNUNET_TIME_relative_get_millisecond_ () 171GNUNET_TIME_relative_get_millisecond_ ()
172{ 172{
173 static struct GNUNET_TIME_Relative one = { 1000 }; 173 static struct GNUNET_TIME_Relative one = {1000};
174 174
175 return one; 175 return one;
176} 176}
@@ -182,7 +182,7 @@ GNUNET_TIME_relative_get_millisecond_ ()
182struct GNUNET_TIME_Relative 182struct GNUNET_TIME_Relative
183GNUNET_TIME_relative_get_second_ () 183GNUNET_TIME_relative_get_second_ ()
184{ 184{
185 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL }; 185 static struct GNUNET_TIME_Relative one = {1000 * 1000LL};
186 186
187 return one; 187 return one;
188} 188}
@@ -194,7 +194,7 @@ GNUNET_TIME_relative_get_second_ ()
194struct GNUNET_TIME_Relative 194struct GNUNET_TIME_Relative
195GNUNET_TIME_relative_get_minute_ () 195GNUNET_TIME_relative_get_minute_ ()
196{ 196{
197 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL }; 197 static struct GNUNET_TIME_Relative one = {60 * 1000 * 1000LL};
198 198
199 return one; 199 return one;
200} 200}
@@ -206,7 +206,7 @@ GNUNET_TIME_relative_get_minute_ ()
206struct GNUNET_TIME_Relative 206struct GNUNET_TIME_Relative
207GNUNET_TIME_relative_get_hour_ () 207GNUNET_TIME_relative_get_hour_ ()
208{ 208{
209 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL }; 209 static struct GNUNET_TIME_Relative one = {60 * 60 * 1000 * 1000LL};
210 210
211 return one; 211 return one;
212} 212}
@@ -218,7 +218,7 @@ GNUNET_TIME_relative_get_hour_ ()
218struct GNUNET_TIME_Relative 218struct GNUNET_TIME_Relative
219GNUNET_TIME_relative_get_forever_ () 219GNUNET_TIME_relative_get_forever_ ()
220{ 220{
221 static struct GNUNET_TIME_Relative forever = { UINT64_MAX }; 221 static struct GNUNET_TIME_Relative forever = {UINT64_MAX};
222 222
223 return forever; 223 return forever;
224} 224}
@@ -230,7 +230,7 @@ GNUNET_TIME_relative_get_forever_ ()
230struct GNUNET_TIME_Absolute 230struct GNUNET_TIME_Absolute
231GNUNET_TIME_absolute_get_forever_ () 231GNUNET_TIME_absolute_get_forever_ ()
232{ 232{
233 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX }; 233 static struct GNUNET_TIME_Absolute forever = {UINT64_MAX};
234 return forever; 234 return forever;
235} 235}
236 236
@@ -252,7 +252,7 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
252 252
253 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us) 253 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
254 { 254 {
255 GNUNET_break (0); /* overflow... */ 255 GNUNET_break (0); /* overflow... */
256 return GNUNET_TIME_UNIT_FOREVER_ABS; 256 return GNUNET_TIME_UNIT_FOREVER_ABS;
257 } 257 }
258 ret.abs_value_us = rel.rel_value_us + now.abs_value_us; 258 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
@@ -290,7 +290,6 @@ GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
290} 290}
291 291
292 292
293
294/** 293/**
295 * Return the minimum of two relative time values. 294 * Return the minimum of two relative time values.
296 * 295 *
@@ -395,7 +394,8 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
395{ 394{
396 struct GNUNET_TIME_Absolute ret; 395 struct GNUNET_TIME_Absolute ret;
397 396
398 if ((start.abs_value_us == UINT64_MAX) || (duration.rel_value_us == UINT64_MAX)) 397 if ((start.abs_value_us == UINT64_MAX) ||
398 (duration.rel_value_us == UINT64_MAX))
399 return GNUNET_TIME_UNIT_FOREVER_ABS; 399 return GNUNET_TIME_UNIT_FOREVER_ABS;
400 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us) 400 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
401 { 401 {
@@ -462,8 +462,7 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
462 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 462 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
463 */ 463 */
464struct GNUNET_TIME_Relative 464struct GNUNET_TIME_Relative
465relative_multiply_double (struct GNUNET_TIME_Relative rel, 465relative_multiply_double (struct GNUNET_TIME_Relative rel, double factor)
466 double factor)
467{ 466{
468 struct GNUNET_TIME_Relative out; 467 struct GNUNET_TIME_Relative out;
469 double m; 468 double m;
@@ -546,7 +545,8 @@ GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
546 * assuming it continues at the same speed 545 * assuming it continues at the same speed
547 */ 546 */
548struct GNUNET_TIME_Relative 547struct GNUNET_TIME_Relative
549GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished, 548GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
549 uint64_t finished,
550 uint64_t total) 550 uint64_t total)
551{ 551{
552 struct GNUNET_TIME_Relative dur; 552 struct GNUNET_TIME_Relative dur;
@@ -673,7 +673,6 @@ GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
673 673
674 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__); 674 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
675 return ret; 675 return ret;
676
677} 676}
678 677
679 678
@@ -706,12 +705,11 @@ GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
706 struct tm *t; 705 struct tm *t;
707 time_t tp; 706 time_t tp;
708 707
709 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */ 708 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
710 t = gmtime (&tp); 709 t = gmtime (&tp);
711 if (t == NULL) 710 if (t == NULL)
712 return 0; 711 return 0;
713 return t->tm_year + 1900; 712 return t->tm_year + 1900;
714
715} 713}
716 714
717 715
@@ -740,8 +738,8 @@ GNUNET_TIME_year_to_time (unsigned int year)
740 t.tm_wday = 1; 738 t.tm_wday = 1;
741 t.tm_yday = 1; 739 t.tm_yday = 1;
742 tp = mktime (&t); 740 tp = mktime (&t);
743 GNUNET_break (tp != (time_t) - 1); 741 GNUNET_break (tp != (time_t) -1);
744 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */ 742 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */
745 return ret; 743 return ret;
746} 744}
747 745
@@ -756,16 +754,16 @@ GNUNET_TIME_year_to_time (unsigned int year)
756 * @return the next backoff time 754 * @return the next backoff time
757 */ 755 */
758struct GNUNET_TIME_Relative 756struct GNUNET_TIME_Relative
759GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold) 757GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt,
758 struct GNUNET_TIME_Relative threshold)
760{ 759{
761 double r = (rand() % 500) / 1000.0; 760 double r = (rand () % 500) / 1000.0;
762 struct GNUNET_TIME_Relative t; 761 struct GNUNET_TIME_Relative t;
763 762
764 t = relative_multiply_double (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, 763 t = relative_multiply_double (
765 rt), 764 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, rt),
766 2 + r); 765 2 + r);
767 return GNUNET_TIME_relative_min (threshold, 766 return GNUNET_TIME_relative_min (threshold, t);
768 t);
769} 767}
770 768
771 769
@@ -778,10 +776,9 @@ GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIM
778struct GNUNET_TIME_Relative 776struct GNUNET_TIME_Relative
779GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r) 777GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r)
780{ 778{
781 double d = ((rand() % 1001) - 500) / 1000.0; 779 double d = ((rand () % 1001) - 500) / 1000.0;
782 780
783 return relative_multiply_double (r, 781 return relative_multiply_double (r, d);
784 d);
785} 782}
786 783
787 784
@@ -804,7 +801,8 @@ GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r)
804 * @return monotonically increasing time 801 * @return monotonically increasing time
805 */ 802 */
806struct GNUNET_TIME_Absolute 803struct GNUNET_TIME_Absolute
807GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cfg) 804GNUNET_TIME_absolute_get_monotonic (
805 const struct GNUNET_CONFIGURATION_Handle *cfg)
808{ 806{
809 static const struct GNUNET_CONFIGURATION_Handle *last_cfg; 807 static const struct GNUNET_CONFIGURATION_Handle *last_cfg;
810 static struct GNUNET_TIME_Absolute last_time; 808 static struct GNUNET_TIME_Absolute last_time;
@@ -825,23 +823,26 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
825 map = NULL; 823 map = NULL;
826 824
827 last_cfg = cfg; 825 last_cfg = cfg;
828 if ( (NULL != cfg) && 826 if ((NULL != cfg) &&
829 (GNUNET_OK == 827 (GNUNET_OK ==
830 GNUNET_CONFIGURATION_get_value_filename (cfg, 828 GNUNET_CONFIGURATION_get_value_filename (cfg,
831 "util", 829 "util",
832 "MONOTONIC_TIME_FILENAME", 830 "MONOTONIC_TIME_FILENAME",
833 &filename)) ) 831 &filename)))
834 { 832 {
835 struct GNUNET_DISK_FileHandle *fh; 833 struct GNUNET_DISK_FileHandle *fh;
836 834
837 fh = GNUNET_DISK_file_open (filename, 835 fh = GNUNET_DISK_file_open (filename,
838 GNUNET_DISK_OPEN_READWRITE | GNUNET_DISK_OPEN_CREATE, 836 GNUNET_DISK_OPEN_READWRITE |
839 GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_WRITE | 837 GNUNET_DISK_OPEN_CREATE,
840 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_GROUP_READ); 838 GNUNET_DISK_PERM_USER_WRITE |
839 GNUNET_DISK_PERM_GROUP_WRITE |
840 GNUNET_DISK_PERM_USER_READ |
841 GNUNET_DISK_PERM_GROUP_READ);
841 if (NULL == fh) 842 if (NULL == fh)
842 { 843 {
843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 844 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
844 _("Failed to map `%s', cannot assure monotonic time!\n"), 845 _ ("Failed to map `%s', cannot assure monotonic time!\n"),
845 filename); 846 filename);
846 } 847 }
847 else 848 else
@@ -849,18 +850,13 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
849 off_t size; 850 off_t size;
850 851
851 size = 0; 852 size = 0;
852 GNUNET_break (GNUNET_OK == 853 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &size));
853 GNUNET_DISK_file_handle_size (fh,
854 &size));
855 if (size < (off_t) sizeof (*map)) 854 if (size < (off_t) sizeof (*map))
856 { 855 {
857 struct GNUNET_TIME_AbsoluteNBO o; 856 struct GNUNET_TIME_AbsoluteNBO o;
858 857
859 o = GNUNET_TIME_absolute_hton (now); 858 o = GNUNET_TIME_absolute_hton (now);
860 if (sizeof (o) != 859 if (sizeof (o) != GNUNET_DISK_file_write (fh, &o, sizeof (o)))
861 GNUNET_DISK_file_write (fh,
862 &o,
863 sizeof (o)))
864 size = 0; 860 size = 0;
865 else 861 else
866 size = sizeof (o); 862 size = sizeof (o);
@@ -873,14 +869,17 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
873 sizeof (*map)); 869 sizeof (*map));
874 if (NULL == map) 870 if (NULL == map)
875 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
876 _("Failed to map `%s', cannot assure monotonic time!\n"), 872 _ (
873 "Failed to map `%s', cannot assure monotonic time!\n"),
877 filename); 874 filename);
878 } 875 }
879 else 876 else
880 { 877 {
881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 878 GNUNET_log (
882 _("Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"), 879 GNUNET_ERROR_TYPE_WARNING,
883 filename); 880 _ (
881 "Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"),
882 filename);
884 } 883 }
885 } 884 }
886 GNUNET_DISK_file_close (fh); 885 GNUNET_DISK_file_close (fh);
@@ -900,11 +899,11 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
900#else 899#else
901 mt.abs_value_us__ = atomic_load (map); 900 mt.abs_value_us__ = atomic_load (map);
902#endif 901#endif
903 last_time = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), 902 last_time =
904 last_time); 903 GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), last_time);
905 } 904 }
906 if (now.abs_value_us <= last_time.abs_value_us) 905 if (now.abs_value_us <= last_time.abs_value_us)
907 now.abs_value_us = last_time.abs_value_us+1; 906 now.abs_value_us = last_time.abs_value_us + 1;
908 last_time = now; 907 last_time = now;
909 if (NULL != map) 908 if (NULL != map)
910 { 909 {
@@ -913,11 +912,10 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
913#if __GNUC__ 912#if __GNUC__
914 (void) __sync_lock_test_and_set (map, val); 913 (void) __sync_lock_test_and_set (map, val);
915#else 914#else
916 *map = val; /* godspeed, pray this is atomic */ 915 *map = val; /* godspeed, pray this is atomic */
917#endif 916#endif
918#else 917#else
919 atomic_store (map, 918 atomic_store (map, val);
920 val);
921#endif 919#endif
922 } 920 }
923 return now; 921 return now;
@@ -927,8 +925,7 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
927/** 925/**
928 * Destructor 926 * Destructor
929 */ 927 */
930void __attribute__ ((destructor)) 928void __attribute__ ((destructor)) GNUNET_util_time_fini ()
931GNUNET_util_time_fini ()
932{ 929{
933 (void) GNUNET_TIME_absolute_get_monotonic (NULL); 930 (void) GNUNET_TIME_absolute_get_monotonic (NULL);
934} 931}