From d9d94d0e53d26af75ec8241383d166544ebd79f3 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 11 Oct 2011 09:43:04 +0000 Subject: converting to GNUNET_LOG_from* --- src/util/bandwidth.c | 184 +-- src/util/bio.c | 255 ++-- src/util/client.c | 822 ++++++------ src/util/common_allocation.c | 60 +- src/util/common_endian.c | 2 + src/util/common_logging.c | 405 +++--- src/util/configuration.c | 769 ++++++------ src/util/connection.c | 1086 ++++++++-------- src/util/container_bloomfilter.c | 263 ++-- src/util/container_heap.c | 202 +-- src/util/container_meta_data.c | 794 ++++++------ src/util/container_multihashmap.c | 237 ++-- src/util/container_slist.c | 74 +- src/util/crypto_aes.c | 79 +- src/util/crypto_crc.c | 14 +- src/util/crypto_hash.c | 217 ++-- src/util/crypto_hkdf.c | 120 +- src/util/crypto_kdf.c | 16 +- src/util/crypto_ksk.c | 517 ++++---- src/util/crypto_random.c | 203 +-- src/util/crypto_rsa.c | 765 ++++++------ src/util/disk.c | 1454 +++++++++++----------- src/util/getopt.c | 1022 +++++++-------- src/util/getopt_helpers.c | 173 +-- src/util/gnunet-config-diff.c | 8 +- src/util/gnunet-resolver.c | 6 +- src/util/gnunet-service-resolver.c | 411 +++--- src/util/load.c | 48 +- src/util/network.c | 1137 ++++++++--------- src/util/os_installation.c | 289 ++--- src/util/os_network.c | 369 +++--- src/util/os_priority.c | 1132 ++++++++--------- src/util/peer.c | 104 +- src/util/perf_crypto_hash.c | 20 +- src/util/plugin.c | 118 +- src/util/program.c | 112 +- src/util/pseudonym.c | 282 +++-- src/util/resolver_api.c | 602 ++++----- src/util/scheduler.c | 110 +- src/util/server.c | 816 ++++++------ src/util/server_mst.c | 278 +++-- src/util/server_nc.c | 221 ++-- src/util/server_tc.c | 79 +- src/util/service.c | 1433 ++++++++++----------- src/util/signal.c | 19 +- src/util/strings.c | 291 ++--- src/util/test_bio.c | 55 +- src/util/test_client.c | 56 +- src/util/test_common_allocation.c | 24 +- src/util/test_common_logging.c | 49 +- src/util/test_common_logging_dummy.c | 4 +- src/util/test_common_logging_runtime_loglevels.c | 334 ++--- src/util/test_configuration.c | 477 +++---- src/util/test_connection.c | 64 +- src/util/test_connection_addressing.c | 69 +- src/util/test_connection_receive_cancel.c | 22 +- src/util/test_connection_timeout.c | 43 +- src/util/test_connection_timeout_no_connect.c | 16 +- src/util/test_connection_transmit_cancel.c | 15 +- src/util/test_container_bloomfilter.c | 206 +-- src/util/test_container_heap.c | 14 +- src/util/test_container_meta_data.c | 256 ++-- src/util/test_container_multihashmap.c | 29 +- src/util/test_container_slist.c | 95 +- src/util/test_crypto_aes.c | 120 +- src/util/test_crypto_aes_weak.c | 127 +- src/util/test_crypto_hash.c | 13 +- src/util/test_crypto_hkdf.c | 94 +- src/util/test_crypto_ksk.c | 192 +-- src/util/test_crypto_random.c | 18 +- src/util/test_crypto_rsa.c | 210 ++-- src/util/test_disk.c | 101 +- src/util/test_getopt.c | 68 +- src/util/test_os_network.c | 8 +- src/util/test_os_priority.c | 12 +- src/util/test_os_start_process.c | 78 +- src/util/test_peer.c | 46 +- src/util/test_program.c | 18 +- src/util/test_pseudonym.c | 46 +- src/util/test_resolver_api.c | 267 ++-- src/util/test_scheduler.c | 16 +- src/util/test_scheduler_delay.c | 20 +- src/util/test_server.c | 43 +- src/util/test_server_disconnect.c | 21 +- src/util/test_server_with_client.c | 58 +- src/util/test_server_with_client_unix.c | 52 +- src/util/test_service.c | 77 +- src/util/test_strings.c | 24 +- src/util/test_time.c | 52 +- src/util/time.c | 64 +- src/util/winproc.c | 230 ++-- 91 files changed, 10941 insertions(+), 10580 deletions(-) (limited to 'src/util') diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c index c0ac54054..ceac4d87b 100644 --- a/src/util/bandwidth.c +++ b/src/util/bandwidth.c @@ -29,6 +29,8 @@ #define DEBUG_BANDWIDTH GNUNET_EXTRA_LOGGING +#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) + /** * Create a new bandwidth value. * @@ -41,8 +43,8 @@ GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second) struct GNUNET_BANDWIDTH_Value32NBO ret; #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing bandwidth of %u Bps\n", - (unsigned int) bytes_per_second); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Initializing bandwidth of %u Bps\n", + (unsigned int) bytes_per_second); #endif ret.value__ = htonl (bytes_per_second); return ret; @@ -58,11 +60,11 @@ GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second) */ struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, - struct GNUNET_BANDWIDTH_Value32NBO b2) + struct GNUNET_BANDWIDTH_Value32NBO b2) { return - GNUNET_BANDWIDTH_value_init (GNUNET_MIN - (ntohl (b1.value__), ntohl (b2.value__))); + GNUNET_BANDWIDTH_value_init (GNUNET_MIN + (ntohl (b1.value__), ntohl (b2.value__))); } @@ -76,18 +78,18 @@ GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, */ uint64_t GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO - bps, - struct GNUNET_TIME_Relative - deadline) + bps, + struct GNUNET_TIME_Relative + deadline) { uint64_t b; b = ntohl (bps.value__); #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Bandwidth has %llu bytes available until deadline in %llums\n", - (unsigned long long) ((b * deadline.rel_value + 500LL) / 1000LL), - deadline.rel_value); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Bandwidth has %llu bytes available until deadline in %llums\n", + (unsigned long long) ((b * deadline.rel_value + 500LL) / 1000LL), + deadline.rel_value); #endif return (b * deadline.rel_value + 500LL) / 1000LL; } @@ -103,25 +105,25 @@ GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO */ struct GNUNET_TIME_Relative GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps, - uint64_t size) + uint64_t size) { uint64_t b; struct GNUNET_TIME_Relative ret; b = ntohl (bps.value__); if (b == 0) - { + { #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Bandwidth suggests delay of infinity (zero bandwidth)\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Bandwidth suggests delay of infinity (zero bandwidth)\n"); #endif - return GNUNET_TIME_UNIT_FOREVER_REL; - } + return GNUNET_TIME_UNIT_FOREVER_REL; + } ret.rel_value = size * 1000LL / b; #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n", - (unsigned long long) ret.rel_value, (unsigned long long) size); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n", + (unsigned long long) ret.rel_value, (unsigned long long) size); #endif return ret; } @@ -143,18 +145,18 @@ GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps, */ void GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av, - struct GNUNET_BANDWIDTH_Value32NBO - bytes_per_second_limit, uint32_t max_carry_s) + struct GNUNET_BANDWIDTH_Value32NBO + bytes_per_second_limit, uint32_t max_carry_s) { av->consumption_since_last_update__ = 0; av->last_update__ = GNUNET_TIME_absolute_get (); av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__); av->max_carry_s__ = max_carry_s; #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p initialized with %u Bps and max carry %u\n", av, - (unsigned int) av->available_bytes_per_s__, - (unsigned int) max_carry_s); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tracker %p initialized with %u Bps and max carry %u\n", av, + (unsigned int) av->available_bytes_per_s__, + (unsigned int) max_carry_s); #endif } @@ -177,26 +179,26 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av) now = GNUNET_TIME_absolute_get (); delta_time = now.abs_value - av->last_update__.abs_value; delta_avail = - (delta_time * ((unsigned long long) av->available_bytes_per_s__) + - 500LL) / 1000LL; + (delta_time * ((unsigned long long) av->available_bytes_per_s__) + + 500LL) / 1000LL; av->consumption_since_last_update__ -= delta_avail; av->last_update__ = now; if (av->consumption_since_last_update__ < 0) - { - left_bytes = -av->consumption_since_last_update__; - max_carry = av->available_bytes_per_s__ * av->max_carry_s__; - if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE) - max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE; - if (max_carry > left_bytes) - av->consumption_since_last_update__ = -left_bytes; - else - av->consumption_since_last_update__ = -max_carry; - } + { + left_bytes = -av->consumption_since_last_update__; + max_carry = av->available_bytes_per_s__ * av->max_carry_s__; + if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE) + max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE; + if (max_carry > left_bytes) + av->consumption_since_last_update__ = -left_bytes; + else + av->consumption_since_last_update__ = -max_carry; + } #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p updated, have %u Bps, last update was %llu ms ago\n", - av, (unsigned int) av->available_bytes_per_s__, - (unsigned long long) delta_time); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tracker %p updated, have %u Bps, last update was %llu ms ago\n", + av, (unsigned int) av->available_bytes_per_s__, + (unsigned long long) delta_time); #endif } @@ -215,38 +217,38 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av) */ int GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, - ssize_t size) + ssize_t size) { int64_t nc; #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av, - (int) size); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av, + (int) size); #endif if (size > 0) - { - nc = av->consumption_since_last_update__ + size; - if (nc < av->consumption_since_last_update__) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - av->consumption_since_last_update__ = nc; - update_tracker (av); - if (av->consumption_since_last_update__ > 0) { + nc = av->consumption_since_last_update__ + size; + if (nc < av->consumption_since_last_update__) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + av->consumption_since_last_update__ = nc; + update_tracker (av); + if (av->consumption_since_last_update__ > 0) + { #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p consumption %llu bytes above limit\n", av, - (unsigned long long) av->consumption_since_last_update__); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tracker %p consumption %llu bytes above limit\n", av, + (unsigned long long) av->consumption_since_last_update__); #endif - return GNUNET_YES; + return GNUNET_YES; + } } - } else - { - av->consumption_since_last_update__ += size; - } + { + av->consumption_since_last_update__ += size; + } return GNUNET_NO; } @@ -262,35 +264,35 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, */ struct GNUNET_TIME_Relative GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av, - size_t size) + size_t size) { struct GNUNET_TIME_Relative ret; int64_t bytes_needed; if (av->available_bytes_per_s__ == 0) - { + { #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av); #endif - return GNUNET_TIME_UNIT_FOREVER_REL; - } + return GNUNET_TIME_UNIT_FOREVER_REL; + } update_tracker (av); bytes_needed = size + av->consumption_since_last_update__; if (bytes_needed <= 0) - { + { #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p delay for %u bytes is zero\n", av, - (unsigned int) size); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tracker %p delay for %u bytes is zero\n", av, + (unsigned int) size); #endif - return GNUNET_TIME_UNIT_ZERO; - } + return GNUNET_TIME_UNIT_ZERO; + } ret.rel_value = - 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__; + 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__; #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p delay for %u bytes is %llu ms\n", av, - (unsigned int) size, (unsigned long long) ret.rel_value); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tracker %p delay for %u bytes is %llu ms\n", av, + (unsigned int) size, (unsigned long long) ret.rel_value); #endif return ret; } @@ -313,14 +315,14 @@ GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av) update_tracker (av); bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__); avail = - GNUNET_BANDWIDTH_value_get_available_until (bps, - GNUNET_TIME_absolute_get_duration - (av->last_update__)); + GNUNET_BANDWIDTH_value_get_available_until (bps, + GNUNET_TIME_absolute_get_duration + (av->last_update__)); used = av->consumption_since_last_update__; #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p available bandwidth is %lld bytes\n", av, - (long long) (int64_t) (avail - used)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tracker %p available bandwidth is %lld bytes\n", av, + (long long) (int64_t) (avail - used)); #endif return (int64_t) (avail - used); } @@ -334,23 +336,23 @@ GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av) */ void GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av, - struct GNUNET_BANDWIDTH_Value32NBO - bytes_per_second_limit) + struct GNUNET_BANDWIDTH_Value32NBO + bytes_per_second_limit) { uint32_t old_limit; uint32_t new_limit; new_limit = ntohl (bytes_per_second_limit.value__); #if DEBUG_BANDWIDTH - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p bandwidth changed to %u Bps\n", av, - (unsigned int) new_limit); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tracker %p bandwidth changed to %u Bps\n", av, + (unsigned int) new_limit); #endif update_tracker (av); old_limit = av->available_bytes_per_s__; av->available_bytes_per_s__ = new_limit; if (old_limit > new_limit) - update_tracker (av); /* maximum excess might be less now */ + update_tracker (av); /* maximum excess might be less now */ } diff --git a/src/util/bio.c b/src/util/bio.c index 404b7aa3b..9c2b9d0dd 100644 --- a/src/util/bio.c +++ b/src/util/bio.c @@ -26,6 +26,8 @@ #include "gnunet_bio_lib.h" #include "gnunet_disk_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) + #define BIO_BUFFER_SIZE 65536 #define MAX_META_DATA (1024 * 1024) @@ -56,7 +58,8 @@ GNUNET_BIO_read_open (const char *fn) struct GNUNET_DISK_FileHandle *fd; struct GNUNET_BIO_ReadHandle *h; - fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); + fd = + GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); if (NULL == fd) return NULL; h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); @@ -102,7 +105,7 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) */ int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, - void *result, size_t len) + void *result, size_t len) { char *dst = result; size_t min; @@ -113,38 +116,38 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, return GNUNET_SYSERR; pos = 0; do - { - /* first, use buffer */ - min = h->have - h->pos; - if (min > 0) - { - if (min > len - pos) - min = len - pos; - memcpy (&dst[pos], &h->buffer[h->pos], min); - h->pos += min; - pos += min; - } - if (pos == len) - return GNUNET_OK; /* done! */ - GNUNET_assert (h->have == h->pos); - /* fill buffer */ - ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); - if (ret == -1) - { - GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, - STRERROR (errno)); - return GNUNET_SYSERR; - } - if (ret == 0) { - GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, - _("End of file")); - return GNUNET_SYSERR; + /* first, use buffer */ + min = h->have - h->pos; + if (min > 0) + { + if (min > len - pos) + min = len - pos; + memcpy (&dst[pos], &h->buffer[h->pos], min); + h->pos += min; + pos += min; + } + if (pos == len) + return GNUNET_OK; /* done! */ + GNUNET_assert (h->have == h->pos); + /* fill buffer */ + ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); + if (ret == -1) + { + GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, + STRERROR (errno)); + return GNUNET_SYSERR; + } + if (ret == 0) + { + GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, + _("End of file")); + return GNUNET_SYSERR; + } + h->pos = 0; + h->have = ret; } - h->pos = 0; - h->have = ret; - } - while (pos < len); /* should always be true */ + while (pos < len); /* should always be true */ return GNUNET_OK; } @@ -160,8 +163,8 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, * @return GNUNET_OK on success, GNUNET_SYSERR on failure */ int -GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line, - void *result, size_t len) +GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, + int line, void *result, size_t len) { char what[1024]; @@ -182,39 +185,41 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line, */ int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, - char **result, size_t maxLen) + char **result, size_t maxLen) { char *buf; uint32_t big; if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) - { - GNUNET_free_non_null (h->emsg); - GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what); - return GNUNET_SYSERR; - } + { + GNUNET_free_non_null (h->emsg); + GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), + what); + return GNUNET_SYSERR; + } if (big == 0) - { - *result = NULL; - return GNUNET_OK; - } + { + *result = NULL; + return GNUNET_OK; + } if (big > maxLen) - { - GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"), - what, big, maxLen); - return GNUNET_SYSERR; - } + { + GNUNET_asprintf (&h->emsg, + _("String `%s' longer than allowed (%u > %u)"), what, + big, maxLen); + return GNUNET_SYSERR; + } buf = GNUNET_malloc (big); *result = buf; buf[--big] = '\0'; if (big == 0) return GNUNET_OK; if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) - { - GNUNET_free (buf); - *result = NULL; - return GNUNET_SYSERR; - } + { + GNUNET_free (buf); + *result = NULL; + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -229,7 +234,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, */ int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, - struct GNUNET_CONTAINER_MetaData **result) + struct GNUNET_CONTAINER_MetaData **result) { uint32_t size; char *buf; @@ -238,30 +243,32 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK) return GNUNET_SYSERR; if (size == 0) - { - *result = NULL; - return GNUNET_OK; - } + { + *result = NULL; + return GNUNET_OK; + } if (size > MAX_META_DATA) - { - GNUNET_asprintf (&h->emsg, - _("Serialized metadata `%s' larger than allowed (%u>%u)"), - what, size, MAX_META_DATA); - return GNUNET_SYSERR; - } + { + GNUNET_asprintf (&h->emsg, + _ + ("Serialized metadata `%s' larger than allowed (%u>%u)"), + what, size, MAX_META_DATA); + return GNUNET_SYSERR; + } buf = GNUNET_malloc (size); if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) - { - GNUNET_free (buf); - return GNUNET_SYSERR; - } + { + GNUNET_free (buf); + return GNUNET_SYSERR; + } meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); if (meta == NULL) - { - GNUNET_free (buf); - GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what); - return GNUNET_SYSERR; - } + { + GNUNET_free (buf); + GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), + what); + return GNUNET_SYSERR; + } GNUNET_free (buf); *result = meta; return GNUNET_OK; @@ -279,7 +286,7 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, */ int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, - int line, int32_t * i) + int line, int32_t * i) { int32_t big; @@ -301,7 +308,7 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, */ int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file, - int line, int64_t * i) + int line, int64_t * i) { int64_t big; @@ -337,13 +344,15 @@ GNUNET_BIO_write_open (const char *fn) struct GNUNET_BIO_WriteHandle *h; fd = GNUNET_DISK_file_open (fn, - GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE - | GNUNET_DISK_OPEN_CREATE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_OPEN_WRITE | + GNUNET_DISK_OPEN_TRUNCATE | + GNUNET_DISK_OPEN_CREATE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); if (NULL == fd) return NULL; - h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); + h = + GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); h->buffer = (char *) &h[1]; h->size = BIO_BUFFER_SIZE; h->fd = fd; @@ -365,18 +374,18 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) int ret; if (NULL == h->fd) - { - ret = GNUNET_SYSERR; - } - else - { - wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); - if (wrt == h->have) - ret = GNUNET_OK; - else + { ret = GNUNET_SYSERR; - GNUNET_DISK_file_close (h->fd); - } + } + else + { + wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); + if (wrt == h->have) + ret = GNUNET_OK; + else + ret = GNUNET_SYSERR; + GNUNET_DISK_file_close (h->fd); + } GNUNET_free (h); return ret; } @@ -392,7 +401,7 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) */ int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, - size_t n) + size_t n) { const char *src = buffer; size_t min; @@ -403,27 +412,27 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, return GNUNET_SYSERR; pos = 0; do - { - /* first, just use buffer */ - min = h->size - h->have; - if (min > n - pos) - min = n - pos; - memcpy (&h->buffer[h->have], &src[pos], min); - pos += min; - h->have += min; - if (pos == n) - return GNUNET_OK; /* done */ - GNUNET_assert (h->have == h->size); - ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size); - if (ret != h->size) { - GNUNET_DISK_file_close (h->fd); - h->fd = NULL; - return GNUNET_SYSERR; /* error */ + /* first, just use buffer */ + min = h->size - h->have; + if (min > n - pos) + min = n - pos; + memcpy (&h->buffer[h->have], &src[pos], min); + pos += min; + h->have += min; + if (pos == n) + return GNUNET_OK; /* done */ + GNUNET_assert (h->have == h->size); + ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size); + if (ret != h->size) + { + GNUNET_DISK_file_close (h->fd); + h->fd = NULL; + return GNUNET_SYSERR; /* error */ + } + h->have = 0; } - h->have = 0; - } - while (pos < n); /* should always be true */ + while (pos < n); /* should always be true */ GNUNET_break (0); return GNUNET_OK; } @@ -459,7 +468,7 @@ GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s) */ int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, - const struct GNUNET_CONTAINER_MetaData *m) + const struct GNUNET_CONTAINER_MetaData *m) { ssize_t size; char *buf; @@ -468,19 +477,19 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, return GNUNET_BIO_write_int32 (h, 0); buf = NULL; size = - GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, - GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); + GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, + GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); if (size == -1) - { - GNUNET_free (buf); - return GNUNET_SYSERR; - } + { + GNUNET_free (buf); + return GNUNET_SYSERR; + } if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || (GNUNET_OK != GNUNET_BIO_write (h, buf, size))) - { - GNUNET_free (buf); - return GNUNET_SYSERR; - } + { + GNUNET_free (buf); + return GNUNET_SYSERR; + } GNUNET_free (buf); return GNUNET_OK; } diff --git a/src/util/client.c b/src/util/client.c index 5193f751f..5d5387ef9 100644 --- a/src/util/client.c +++ b/src/util/client.c @@ -43,6 +43,8 @@ */ #define MAX_ATTEMPTS 50 +#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) + /** * Handle for a transmission request. */ @@ -262,7 +264,8 @@ struct GNUNET_CLIENT_Connection */ static struct GNUNET_CONNECTION_Handle * do_connect (const char *service_name, - const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt) + const struct GNUNET_CONFIGURATION_Handle *cfg, + unsigned int attempt) { struct GNUNET_CONNECTION_Handle *sock; char *hostname; @@ -272,79 +275,80 @@ do_connect (const char *service_name, sock = NULL; #if AF_UNIX if (0 == (attempt % 2)) - { - /* on even rounds, try UNIX */ - unixpath = NULL; - if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */ { - sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); - if (sock != NULL) - { + /* on even rounds, try UNIX */ + unixpath = NULL; + if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */ + { + sock = + GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); + if (sock != NULL) + { #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", - unixpath); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", + unixpath); #endif - GNUNET_free (unixpath); - return sock; - } + GNUNET_free (unixpath); + return sock; + } + } + GNUNET_free_non_null (unixpath); } - GNUNET_free_non_null (unixpath); - } #endif if ((GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) - || (port > 65535) || - (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME", - &hostname))) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ - ("Could not determine valid hostname and port for service `%s' from configuration.\n"), - service_name); - return NULL; - } + GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", + &port)) || (port > 65535) + || (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_string (cfg, service_name, + "HOSTNAME", &hostname))) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _ + ("Could not determine valid hostname and port for service `%s' from configuration.\n"), + service_name); + return NULL; + } if (0 == strlen (hostname)) - { - GNUNET_free (hostname); - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Need a non-empty hostname for service `%s'.\n"), - service_name); - return NULL; - } - if (port == 0) - { -#if AF_UNIX - if (0 != (attempt % 2)) { - /* try UNIX */ - unixpath = NULL; - if ((GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", - &unixpath)) && - (0 < strlen (unixpath))) - { - sock = - GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); - if (sock != NULL) - { - GNUNET_free (unixpath); - GNUNET_free (hostname); - return sock; - } - } - GNUNET_free_non_null (unixpath); + GNUNET_free (hostname); + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Need a non-empty hostname for service `%s'.\n"), service_name); + return NULL; } + if (port == 0) + { +#if AF_UNIX + if (0 != (attempt % 2)) + { + /* try UNIX */ + unixpath = NULL; + if ((GNUNET_OK == + GNUNET_CONFIGURATION_get_value_string (cfg, service_name, + "UNIXPATH", &unixpath)) + && (0 < strlen (unixpath))) + { + sock = + GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, + unixpath); + if (sock != NULL) + { + GNUNET_free (unixpath); + GNUNET_free (hostname); + return sock; + } + } + GNUNET_free_non_null (unixpath); + } #endif #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n", - service_name); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n", + service_name); #endif - GNUNET_free (hostname); - return NULL; - } + GNUNET_free (hostname); + return NULL; + } sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port); GNUNET_free (hostname); @@ -361,7 +365,7 @@ do_connect (const char *service_name, */ struct GNUNET_CLIENT_Connection * GNUNET_CLIENT_connect (const char *service_name, - const struct GNUNET_CONFIGURATION_Handle *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_CLIENT_Connection *ret; struct GNUNET_CONNECTION_Handle *sock; @@ -385,7 +389,7 @@ GNUNET_CLIENT_connect (const char *service_name, */ void GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h, - int do_ignore) + int do_ignore) { h->ignore_shutdown = do_ignore; if (h->sock != NULL) @@ -410,33 +414,33 @@ GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h, */ void GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock, - int finish_pending_write) + int finish_pending_write) { if (sock->in_receive == GNUNET_YES) - { - GNUNET_CONNECTION_receive_cancel (sock->sock); - sock->in_receive = GNUNET_NO; - } + { + GNUNET_CONNECTION_receive_cancel (sock->sock); + sock->in_receive = GNUNET_NO; + } if (sock->th != NULL) - { - GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th); - sock->th = NULL; - } + { + GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th); + sock->th = NULL; + } if (NULL != sock->sock) - { - GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write); - sock->sock = NULL; - } + { + GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write); + sock->sock = NULL; + } if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (sock->receive_task); - sock->receive_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (sock->receive_task); + sock->receive_task = GNUNET_SCHEDULER_NO_TASK; + } if (sock->tag != NULL) - { - GNUNET_free (sock->tag); - sock->tag = NULL; - } + { + GNUNET_free (sock->tag); + sock->tag = NULL; + } sock->receiver_handler = NULL; GNUNET_array_grow (sock->received_buf, sock->received_size, 0); GNUNET_free (sock->service_name); @@ -453,7 +457,7 @@ check_complete (struct GNUNET_CLIENT_Connection *conn) if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) && (conn->received_pos >= ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)-> - size))) + size))) conn->msg_complete = GNUNET_YES; } @@ -472,7 +476,7 @@ check_complete (struct GNUNET_CLIENT_Connection *conn) */ static void receive_helper (void *cls, const void *buf, size_t available, - const struct sockaddr *addr, socklen_t addrlen, int errCode) + const struct sockaddr *addr, socklen_t addrlen, int errCode) { struct GNUNET_CLIENT_Connection *conn = cls; struct GNUNET_TIME_Relative remaining; @@ -482,22 +486,22 @@ receive_helper (void *cls, const void *buf, size_t available, GNUNET_assert (conn->msg_complete == GNUNET_NO); conn->in_receive = GNUNET_NO; if ((available == 0) || (conn->sock == NULL) || (errCode != 0)) - { - /* signal timeout! */ + { + /* signal timeout! */ #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n", - (unsigned int) available, - conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n", + (unsigned int) available, + conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode)); #endif - if (NULL != (receive_handler = conn->receiver_handler)) - { - receive_handler_cls = conn->receiver_handler_cls; - conn->receiver_handler = NULL; - receive_handler (receive_handler_cls, NULL); + if (NULL != (receive_handler = conn->receiver_handler)) + { + receive_handler_cls = conn->receiver_handler_cls; + conn->receiver_handler = NULL; + receive_handler (receive_handler_cls, NULL); + } + return; } - return; - } /* FIXME: optimize for common fast case where buf contains the * entire message and we need no copying... */ @@ -506,22 +510,22 @@ receive_helper (void *cls, const void *buf, size_t available, /* slow path: append to array */ if (conn->received_size < conn->received_pos + available) GNUNET_array_grow (conn->received_buf, conn->received_size, - conn->received_pos + available); + conn->received_pos + available); memcpy (&conn->received_buf[conn->received_pos], buf, available); conn->received_pos += available; check_complete (conn); /* check for timeout */ remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout); if (remaining.rel_value == 0) - { - /* signal timeout! */ - if (NULL != conn->receiver_handler) - conn->receiver_handler (conn->receiver_handler_cls, NULL); - return; - } + { + /* signal timeout! */ + if (NULL != conn->receiver_handler) + conn->receiver_handler (conn->receiver_handler_cls, NULL); + return; + } /* back to receive -- either for more data or to call callback! */ GNUNET_CLIENT_receive (conn, conn->receiver_handler, - conn->receiver_handler_cls, remaining); + conn->receiver_handler_cls, remaining); } @@ -537,23 +541,23 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) struct GNUNET_CLIENT_Connection *sock = cls; GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler; const struct GNUNET_MessageHeader *cmsg = - (const struct GNUNET_MessageHeader *) sock->received_buf; + (const struct GNUNET_MessageHeader *) sock->received_buf; void *handler_cls = sock->receiver_handler_cls; uint16_t msize = ntohs (cmsg->size); char mbuf[msize]; struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf; #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received message of type %u and size %u\n", ntohs (cmsg->type), - msize); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Received message of type %u and size %u\n", ntohs (cmsg->type), + msize); #endif sock->receive_task = GNUNET_SCHEDULER_NO_TASK; GNUNET_assert (GNUNET_YES == sock->msg_complete); GNUNET_assert (sock->received_pos >= msize); memcpy (msg, cmsg, msize); memmove (sock->received_buf, &sock->received_buf[msize], - sock->received_pos - msize); + sock->received_pos - msize); sock->received_pos -= msize; sock->msg_complete = GNUNET_NO; sock->receiver_handler = NULL; @@ -573,35 +577,36 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ void GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock, - GNUNET_CLIENT_MessageHandler handler, void *handler_cls, - struct GNUNET_TIME_Relative timeout) + GNUNET_CLIENT_MessageHandler handler, + void *handler_cls, struct GNUNET_TIME_Relative timeout) { if (sock->sock == NULL) - { - /* already disconnected, fail instantly! */ - GNUNET_break (0); /* this should not happen in well-written code! */ - if (NULL != handler) - handler (handler_cls, NULL); - return; - } + { + /* already disconnected, fail instantly! */ + GNUNET_break (0); /* this should not happen in well-written code! */ + if (NULL != handler) + handler (handler_cls, NULL); + return; + } sock->receiver_handler = handler; sock->receiver_handler_cls = handler_cls; sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); if (GNUNET_YES == sock->msg_complete) - { - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task); - sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock); - } + { + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task); + sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock); + } else - { - GNUNET_assert (sock->in_receive == GNUNET_NO); - sock->in_receive = GNUNET_YES; + { + GNUNET_assert (sock->in_receive == GNUNET_NO); + sock->in_receive = GNUNET_YES; #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n"); #endif - GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, - timeout, &receive_helper, sock); - } + GNUNET_CONNECTION_receive (sock->sock, + GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, timeout, + &receive_helper, sock); + } } @@ -612,7 +617,7 @@ static void service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls) { GNUNET_SCHEDULER_add_continuation (task, task_cls, - GNUNET_SCHEDULER_REASON_TIMEOUT); + GNUNET_SCHEDULER_REASON_TIMEOUT); } @@ -631,18 +636,18 @@ confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg) * detail in the future, for example, is this the * correct service? FIXME! */ if (msg != NULL) - { + { #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received confirmation that service is running.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Received confirmation that service is running.\n"); #endif - GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); - } + GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls, + GNUNET_SCHEDULER_REASON_PREREQ_DONE); + } else - { - service_test_error (conn->test_cb, conn->test_cb_cls); - } + { + service_test_error (conn->test_cb, conn->test_cb_cls); + } GNUNET_CLIENT_disconnect (conn, GNUNET_NO); } @@ -663,24 +668,23 @@ write_test (void *cls, size_t size, void *buf) struct GNUNET_MessageHeader *msg; if (size < sizeof (struct GNUNET_MessageHeader)) - { + { #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _("Failure to transmit TEST request.\n")); + LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n")); #endif - service_test_error (conn->test_cb, conn->test_cb_cls); - GNUNET_CLIENT_disconnect (conn, GNUNET_NO); - return 0; /* client disconnected */ - } + service_test_error (conn->test_cb, conn->test_cb_cls); + GNUNET_CLIENT_disconnect (conn, GNUNET_NO); + return 0; /* client disconnected */ + } #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST"); #endif msg = (struct GNUNET_MessageHeader *) buf; msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); GNUNET_CLIENT_receive (conn, &confirm_handler, conn, - GNUNET_TIME_absolute_get_remaining - (conn->test_deadline)); + GNUNET_TIME_absolute_get_remaining + (conn->test_deadline)); return sizeof (struct GNUNET_MessageHeader); } @@ -700,9 +704,9 @@ write_test (void *cls, size_t size, void *buf) */ void GNUNET_CLIENT_service_test (const char *service, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_TIME_Relative timeout, - GNUNET_SCHEDULER_Task task, void *task_cls) + const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_TIME_Relative timeout, + GNUNET_SCHEDULER_Task task, void *task_cls) { char *hostname; unsigned long long port; @@ -710,8 +714,8 @@ GNUNET_CLIENT_service_test (const char *service, struct GNUNET_CLIENT_Connection *conn; #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n", - service); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n", + service); #endif #ifdef AF_UNIX { @@ -721,49 +725,50 @@ GNUNET_CLIENT_service_test (const char *service, char *unixpath; unixpath = NULL; - if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */ - { - if (strlen (unixpath) >= sizeof (s_un.sun_path)) + if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */ { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("UNIXPATH `%s' too long, maximum length is %llu\n"), - unixpath, sizeof (s_un.sun_path)); - } - else - { - sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); - if (sock != NULL) - { - memset (&s_un, 0, sizeof (s_un)); - s_un.sun_family = AF_UNIX; - slen = strlen (unixpath) + 1; - if (slen >= sizeof (s_un.sun_path)) - slen = sizeof (s_un.sun_path) - 1; - memcpy (s_un.sun_path, unixpath, slen); - s_un.sun_path[slen] = '\0'; - slen = sizeof (struct sockaddr_un); + if (strlen (unixpath) >= sizeof (s_un.sun_path)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("UNIXPATH `%s' too long, maximum length is %llu\n"), + unixpath, sizeof (s_un.sun_path)); + } + else + { + sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); + if (sock != NULL) + { + memset (&s_un, 0, sizeof (s_un)); + s_un.sun_family = AF_UNIX; + slen = strlen (unixpath) + 1; + if (slen >= sizeof (s_un.sun_path)) + slen = sizeof (s_un.sun_path) - 1; + memcpy (s_un.sun_path, unixpath, slen); + s_un.sun_path[slen] = '\0'; + slen = sizeof (struct sockaddr_un); #if LINUX - s_un.sun_path[0] = '\0'; + s_un.sun_path[0] = '\0'; #endif #if HAVE_SOCKADDR_IN_SIN_LEN - s_un.sun_len = (u_char) slen; + s_un.sun_len = (u_char) slen; #endif - if (GNUNET_OK != - GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un, - slen)) - { - /* failed to bind => service must be running */ - GNUNET_free (unixpath); - (void) GNUNET_NETWORK_socket_close (sock); - GNUNET_SCHEDULER_add_continuation (task, task_cls, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); - return; - } - (void) GNUNET_NETWORK_socket_close (sock); - } - /* let's try IP */ + if (GNUNET_OK != + GNUNET_NETWORK_socket_bind (sock, + (const struct sockaddr *) + &s_un, slen)) + { + /* failed to bind => service must be running */ + GNUNET_free (unixpath); + (void) GNUNET_NETWORK_socket_close (sock); + GNUNET_SCHEDULER_add_continuation (task, task_cls, + GNUNET_SCHEDULER_REASON_PREREQ_DONE); + return; + } + (void) GNUNET_NETWORK_socket_close (sock); + } + /* let's try IP */ + } } - } GNUNET_free_non_null (unixpath); } #endif @@ -774,121 +779,124 @@ GNUNET_CLIENT_service_test (const char *service, (port > 65535) || (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME", - &hostname))) - { - /* UNIXPATH failed (if possible) AND IP failed => error */ - service_test_error (task, task_cls); - return; - } + &hostname))) + { + /* UNIXPATH failed (if possible) AND IP failed => error */ + service_test_error (task, task_cls); + return; + } if (0 == strcmp ("localhost", hostname) #if !LINUX && 0 #endif - ) - { - /* can test using 'bind' */ - struct sockaddr_in s_in; + ) + { + /* can test using 'bind' */ + struct sockaddr_in s_in; - memset (&s_in, 0, sizeof (s_in)); + memset (&s_in, 0, sizeof (s_in)); #if HAVE_SOCKADDR_IN_SIN_LEN - s_in.sin_len = sizeof (struct sockaddr_in); + s_in.sin_len = sizeof (struct sockaddr_in); #endif - s_in.sin_family = AF_INET; - s_in.sin_port = htons (port); + s_in.sin_family = AF_INET; + s_in.sin_port = htons (port); - sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); - if (sock != NULL) - { - if (GNUNET_OK != - GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in, - sizeof (s_in))) - { - /* failed to bind => service must be running */ - GNUNET_free (hostname); - (void) GNUNET_NETWORK_socket_close (sock); - GNUNET_SCHEDULER_add_continuation (task, task_cls, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); - return; - } - (void) GNUNET_NETWORK_socket_close (sock); + sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); + if (sock != NULL) + { + if (GNUNET_OK != + GNUNET_NETWORK_socket_bind (sock, + (const struct sockaddr *) &s_in, + sizeof (s_in))) + { + /* failed to bind => service must be running */ + GNUNET_free (hostname); + (void) GNUNET_NETWORK_socket_close (sock); + GNUNET_SCHEDULER_add_continuation (task, task_cls, + GNUNET_SCHEDULER_REASON_PREREQ_DONE); + return; + } + (void) GNUNET_NETWORK_socket_close (sock); + } } - } if (0 == strcmp ("ip6-localhost", hostname) #if !LINUX && 0 #endif - ) - { - /* can test using 'bind' */ - struct sockaddr_in6 s_in6; + ) + { + /* can test using 'bind' */ + struct sockaddr_in6 s_in6; - memset (&s_in6, 0, sizeof (s_in6)); + memset (&s_in6, 0, sizeof (s_in6)); #if HAVE_SOCKADDR_IN_SIN_LEN - s_in6.sin6_len = sizeof (struct sockaddr_in6); + s_in6.sin6_len = sizeof (struct sockaddr_in6); #endif - s_in6.sin6_family = AF_INET6; - s_in6.sin6_port = htons (port); + s_in6.sin6_family = AF_INET6; + s_in6.sin6_port = htons (port); - sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0); - if (sock != NULL) - { - if (GNUNET_OK != - GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6, - sizeof (s_in6))) - { - /* failed to bind => service must be running */ - GNUNET_free (hostname); - (void) GNUNET_NETWORK_socket_close (sock); - GNUNET_SCHEDULER_add_continuation (task, task_cls, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); - return; - } - (void) GNUNET_NETWORK_socket_close (sock); + sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0); + if (sock != NULL) + { + if (GNUNET_OK != + GNUNET_NETWORK_socket_bind (sock, + (const struct sockaddr *) &s_in6, + sizeof (s_in6))) + { + /* failed to bind => service must be running */ + GNUNET_free (hostname); + (void) GNUNET_NETWORK_socket_close (sock); + GNUNET_SCHEDULER_add_continuation (task, task_cls, + GNUNET_SCHEDULER_REASON_PREREQ_DONE); + return; + } + (void) GNUNET_NETWORK_socket_close (sock); + } } - } if (((0 == strcmp ("localhost", hostname)) || (0 == strcmp ("ip6-localhost", hostname))) #if !LINUX && 0 #endif - ) - { - /* all binds succeeded => claim service not running right now */ - GNUNET_free_non_null (hostname); - service_test_error (task, task_cls); - return; - } + ) + { + /* all binds succeeded => claim service not running right now */ + GNUNET_free_non_null (hostname); + service_test_error (task, task_cls); + return; + } GNUNET_free_non_null (hostname); /* non-localhost, try 'connect' method */ conn = GNUNET_CLIENT_connect (service, cfg); if (conn == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Could not connect to service `%s', must not be running.\n"), - service); - service_test_error (task, task_cls); - return; - } + { + LOG (GNUNET_ERROR_TYPE_INFO, + _("Could not connect to service `%s', must not be running.\n"), + service); + service_test_error (task, task_cls); + return; + } conn->test_cb = task; conn->test_cb_cls = task_cls; conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout); if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn, - sizeof (struct GNUNET_MessageHeader), - timeout, GNUNET_YES, &write_test, - conn)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failure to transmit request to service `%s'\n"), service); - service_test_error (task, task_cls); - GNUNET_CLIENT_disconnect (conn, GNUNET_NO); - return; - } + sizeof (struct + GNUNET_MessageHeader), + timeout, GNUNET_YES, &write_test, + conn)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Failure to transmit request to service `%s'\n"), service); + service_test_error (task, task_cls); + GNUNET_CLIENT_disconnect (conn, GNUNET_NO); + return; + } } @@ -902,8 +910,7 @@ GNUNET_CLIENT_service_test (const char *service, * @param buf where to write them * @return number of bytes written to buf */ -static size_t -client_notify (void *cls, size_t size, void *buf); +static size_t client_notify (void *cls, size_t size, void *buf); /** @@ -914,59 +921,60 @@ client_notify (void *cls, size_t size, void *buf); * @param tc unused */ static void -client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +client_delayed_retry (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_CLIENT_TransmitHandle *th = cls; struct GNUNET_TIME_Relative delay; th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - { + { #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmission failed due to shutdown.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n"); #endif - th->sock->th = NULL; - th->notify (th->notify_cls, 0, NULL); - GNUNET_free (th); - return; - } + th->sock->th = NULL; + th->notify (th->notify_cls, 0, NULL); + GNUNET_free (th); + return; + } th->sock->sock = - do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++); + do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++); if (NULL == th->sock->sock) - { - /* could happen if we're out of sockets */ - delay = - GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining - (th->timeout), th->sock->back_off); - th->sock->back_off = - GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply - (th->sock->back_off, 2), - GNUNET_TIME_UNIT_SECONDS); + { + /* could happen if we're out of sockets */ + delay = + GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining + (th->timeout), th->sock->back_off); + th->sock->back_off = + GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply + (th->sock->back_off, 2), + GNUNET_TIME_UNIT_SECONDS); #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmission failed %u times, trying again in %llums.\n", - MAX_ATTEMPTS - th->attempts_left, - (unsigned long long) delay.rel_value); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmission failed %u times, trying again in %llums.\n", + MAX_ATTEMPTS - th->attempts_left, + (unsigned long long) delay.rel_value); #endif - th->reconnect_task = - GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); - return; - } - GNUNET_CONNECTION_ignore_shutdown (th->sock->sock, th->sock->ignore_shutdown); + th->reconnect_task = + GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); + return; + } + GNUNET_CONNECTION_ignore_shutdown (th->sock->sock, + th->sock->ignore_shutdown); th->th = - GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size, - GNUNET_TIME_absolute_get_remaining - (th->timeout), &client_notify, - th); + GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size, + GNUNET_TIME_absolute_get_remaining + (th->timeout), &client_notify, + th); if (th->th == NULL) - { - GNUNET_break (0); - th->sock->th = NULL; - th->notify (th->notify_cls, 0, NULL); - GNUNET_free (th); - return; - } + { + GNUNET_break (0); + th->sock->th = NULL; + th->notify (th->notify_cls, 0, NULL); + GNUNET_free (th); + return; + } } @@ -989,47 +997,47 @@ client_notify (void *cls, size_t size, void *buf) th->th = NULL; th->sock->th = NULL; if (buf == NULL) - { - delay = GNUNET_TIME_absolute_get_remaining (th->timeout); - delay.rel_value /= 2; - if ((0 != - (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) || - (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) || - (delay.rel_value < 1)) { + delay = GNUNET_TIME_absolute_get_remaining (th->timeout); + delay.rel_value /= 2; + if ((0 != + (GNUNET_SCHEDULER_REASON_SHUTDOWN & + GNUNET_SCHEDULER_get_reason ())) || (GNUNET_YES != th->auto_retry) + || (0 == --th->attempts_left) || (delay.rel_value < 1)) + { #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmission failed %u times, giving up.\n", - MAX_ATTEMPTS - th->attempts_left); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmission failed %u times, giving up.\n", + MAX_ATTEMPTS - th->attempts_left); #endif - GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL)); - GNUNET_free (th); - return 0; - } - /* auto-retry */ + GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL)); + GNUNET_free (th); + return 0; + } + /* auto-retry */ #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Failed to connect to `%s', automatically trying again.\n", - th->sock->service_name); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Failed to connect to `%s', automatically trying again.\n", + th->sock->service_name); #endif - GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO); - th->sock->sock = NULL; - delay = GNUNET_TIME_relative_min (delay, th->sock->back_off); - th->sock->back_off = - GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply - (th->sock->back_off, 2), - GNUNET_TIME_UNIT_SECONDS); + GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO); + th->sock->sock = NULL; + delay = GNUNET_TIME_relative_min (delay, th->sock->back_off); + th->sock->back_off = + GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply + (th->sock->back_off, 2), + GNUNET_TIME_UNIT_SECONDS); #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmission failed %u times, trying again in %llums.\n", - MAX_ATTEMPTS - th->attempts_left, - (unsigned long long) delay.rel_value); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmission failed %u times, trying again in %llums.\n", + MAX_ATTEMPTS - th->attempts_left, + (unsigned long long) delay.rel_value); #endif - th->sock->th = th; - th->reconnect_task = - GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); - return 0; - } + th->sock->th = th; + th->reconnect_task = + GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); + return 0; + } GNUNET_assert (size >= th->size); ret = th->notify (th->notify_cls, size, buf); GNUNET_free (th); @@ -1058,21 +1066,21 @@ client_notify (void *cls, size_t size, void *buf) */ struct GNUNET_CLIENT_TransmitHandle * GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, - size_t size, - struct GNUNET_TIME_Relative timeout, - int auto_retry, - GNUNET_CONNECTION_TransmitReadyNotify - notify, void *notify_cls) + size_t size, + struct GNUNET_TIME_Relative timeout, + int auto_retry, + GNUNET_CONNECTION_TransmitReadyNotify + notify, void *notify_cls) { struct GNUNET_CLIENT_TransmitHandle *th; if (NULL != sock->th) - { - /* If this breaks, you most likley called this function twice without waiting - * for completion or canceling the request */ - GNUNET_break (0); - return NULL; - } + { + /* If this breaks, you most likley called this function twice without waiting + * for completion or canceling the request */ + GNUNET_break (0); + return NULL; + } th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle)); th->sock = sock; th->size = size; @@ -1083,25 +1091,25 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, th->attempts_left = MAX_ATTEMPTS; sock->th = th; if (sock->sock == NULL) - { - th->reconnect_task = - GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry, - th); + { + th->reconnect_task = + GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry, + th); - } + } else - { - th->th = - GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout, - &client_notify, th); - if (NULL == th->th) { - GNUNET_break (0); - GNUNET_free (th); - sock->th = NULL; - return NULL; + th->th = + GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout, + &client_notify, th); + if (NULL == th->th) + { + GNUNET_break (0); + GNUNET_free (th); + sock->th = NULL; + return NULL; + } } - } return th; } @@ -1112,20 +1120,20 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, * @param th handle from the original request. */ void -GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle - *th) +GNUNET_CLIENT_notify_transmit_ready_cancel (struct + GNUNET_CLIENT_TransmitHandle *th) { if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_assert (NULL == th->th); - GNUNET_SCHEDULER_cancel (th->reconnect_task); - th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_assert (NULL == th->th); + GNUNET_SCHEDULER_cancel (th->reconnect_task); + th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; + } else - { - GNUNET_assert (NULL != th->th); - GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th); - } + { + GNUNET_assert (NULL != th->th); + GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th); + } th->sock->th = NULL; GNUNET_free (th); } @@ -1151,21 +1159,21 @@ transmit_for_response (void *cls, size_t size, void *buf) tc->sock->tag = NULL; msize = ntohs (tc->hdr->size); if (NULL == buf) - { + { #if DEBUG_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _ - ("Could not submit request, not expecting to receive a response.\n")); + LOG (GNUNET_ERROR_TYPE_DEBUG, + _ + ("Could not submit request, not expecting to receive a response.\n")); #endif - if (NULL != tc->rn) - tc->rn (tc->rn_cls, NULL); - GNUNET_free (tc); - return 0; - } + if (NULL != tc->rn) + tc->rn (tc->rn_cls, NULL); + GNUNET_free (tc); + return 0; + } GNUNET_assert (size >= msize); memcpy (buf, tc->hdr, msize); GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls, - GNUNET_TIME_absolute_get_remaining (tc->timeout)); + GNUNET_TIME_absolute_get_remaining (tc->timeout)); GNUNET_free (tc); return msize; } @@ -1193,12 +1201,14 @@ transmit_for_response (void *cls, size_t size, void *buf) * is already pending */ int -GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, - const struct GNUNET_MessageHeader *hdr, - struct GNUNET_TIME_Relative timeout, - int auto_retry, - GNUNET_CLIENT_MessageHandler rn, - void *rn_cls) +GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection + *sock, + const struct GNUNET_MessageHeader + *hdr, + struct GNUNET_TIME_Relative timeout, + int auto_retry, + GNUNET_CLIENT_MessageHandler rn, + void *rn_cls) { struct TransmitGetResponseContext *tc; uint16_t msize; @@ -1216,12 +1226,12 @@ GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, tc->rn_cls = rn_cls; if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry, - &transmit_for_response, tc)) - { - GNUNET_break (0); - GNUNET_free (tc); - return GNUNET_SYSERR; - } + &transmit_for_response, tc)) + { + GNUNET_break (0); + GNUNET_free (tc); + return GNUNET_SYSERR; + } sock->tag = tc; return GNUNET_OK; } diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c index 98c89d86a..31da5829b 100644 --- a/src/util/common_allocation.c +++ b/src/util/common_allocation.c @@ -27,6 +27,10 @@ #include "platform.h" #include "gnunet_common.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + #ifndef INT_MAX #define INT_MAX 0x7FFFFFFF #endif @@ -61,10 +65,10 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber) GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber); if (ret == NULL) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); - abort (); - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc"); + abort (); + } return ret; } @@ -82,7 +86,7 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber) */ void * GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, - int linenumber) + int linenumber) { void *ret; @@ -97,10 +101,10 @@ GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, GNUNET_assert_at (size < INT_MAX, filename, linenumber); ret = malloc (size); if (ret == NULL) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); - abort (); - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc"); + abort (); + } #ifdef W32_MEM_LIMIT *((size_t *) ret) = size; ret = &((size_t *) ret)[1]; @@ -168,10 +172,10 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber) #endif ptr = realloc (ptr, n); if ((NULL == ptr) && (n > 0)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc"); - abort (); - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc"); + abort (); + } #ifdef W32_MEM_LIMIT ptr = &((size_t *) ptr)[1]; #endif @@ -229,7 +233,7 @@ GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber) */ char * GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, - int linenumber) + int linenumber) { char *res; @@ -256,7 +260,7 @@ GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, */ void GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, - unsigned int newCount, const char *filename, int linenumber) + unsigned int newCount, const char *filename, int linenumber) { void *tmp; size_t size; @@ -264,22 +268,22 @@ GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber); size = newCount * elementSize; if (size == 0) - { - tmp = NULL; - } + { + tmp = NULL; + } else - { - tmp = GNUNET_xmalloc_ (size, filename, linenumber); - memset (tmp, 0, size); /* client code should not rely on this, though... */ - if (*oldCount > newCount) - *oldCount = newCount; /* shrink is also allowed! */ - memcpy (tmp, *old, elementSize * (*oldCount)); - } + { + tmp = GNUNET_xmalloc_ (size, filename, linenumber); + memset (tmp, 0, size); /* client code should not rely on this, though... */ + if (*oldCount > newCount) + *oldCount = newCount; /* shrink is also allowed! */ + memcpy (tmp, *old, elementSize * (*oldCount)); + } if (*old != NULL) - { - GNUNET_xfree_ (*old, filename, linenumber); - } + { + GNUNET_xfree_ (*old, filename, linenumber); + } *old = tmp; *oldCount = newCount; } diff --git a/src/util/common_endian.c b/src/util/common_endian.c index 223144c40..5d7aa50d5 100644 --- a/src/util/common_endian.c +++ b/src/util/common_endian.c @@ -27,6 +27,8 @@ #include "platform.h" #include "gnunet_common.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) + unsigned long long GNUNET_ntohll (unsigned long long n) { diff --git a/src/util/common_logging.c b/src/util/common_logging.c index ccee8e028..fd32036d0 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c @@ -251,6 +251,7 @@ get_type (const char *log) return GNUNET_ERROR_TYPE_NONE; return GNUNET_ERROR_TYPE_INVALID; } + #if !defined(GNUNET_CULL_LOGGING) /** * Utility function - reallocates logdefs array to be twice as large. @@ -258,8 +259,8 @@ get_type (const char *log) static void resize_logdefs () { - logdefs_size = (logdefs_size + 1) * 2; - logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef)); + logdefs_size = (logdefs_size + 1) * 2; + logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef)); } /** @@ -274,7 +275,8 @@ resize_logdefs () * @param force see struct LogDef */ static void -add_definition (char *component, char *file, char *function, int from_line, int to_line, int level, int force) +add_definition (char *component, char *file, char *function, int from_line, + int to_line, int level, int force) { if (logdefs_size == logdefs_len) resize_logdefs (); @@ -283,10 +285,10 @@ add_definition (char *component, char *file, char *function, int from_line, int if (strlen (component) > 0 && component[0] != '*') n.component = strdup (component); if (strlen (file) > 0 && file[0] != '*') - { - n.file = strdup (file); - n.strlen_file = strlen (file); - } + { + n.file = strdup (file); + n.strlen_file = strlen (file); + } if (strlen (function) > 0 && function[0] != '*') n.function = strdup (function); n.from_line = from_line; @@ -310,8 +312,9 @@ add_definition (char *component, char *file, char *function, int from_line, int * @param line line at which the call is made, usually __LINE__ * @return 0 to disallow the call, 1 to allow it */ -int -GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file, const char *function, int line) +int +GNUNET_get_log_call_status (int caller_level, const char *comp, + const char *file, const char *function, int line) { struct LogDef *ld; int i; @@ -332,21 +335,20 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file force_only = min_level >= 0; strlen_file = strlen (file); for (i = 0; i < logdefs_len; i++) - { - ld = &logdefs[i]; - if ((!force_only || ld->force) && - (line >= ld->from_line && line <= ld->to_line) && - (ld->component == NULL || strcmp (comp, ld->component) == 0) && - (ld->file == NULL || - (ld->strlen_file <= strlen_file && - strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) && - (ld->function == NULL || strcmp (function, ld->function) == 0) - ) { - /* We're finished */ - return caller_level <= ld->level; + ld = &logdefs[i]; + if ((!force_only || ld->force) && + (line >= ld->from_line && line <= ld->to_line) && + (ld->component == NULL || strcmp (comp, ld->component) == 0) && + (ld->file == NULL || + (ld->strlen_file <= strlen_file && + strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) && + (ld->function == NULL || strcmp (function, ld->function) == 0)) + { + /* We're finished */ + return caller_level <= ld->level; + } } - } /* No matches - use global level, if defined */ if (min_level >= 0) return caller_level <= min_level; @@ -404,83 +406,85 @@ parse_definitions (const char *constname, int force) from_line = 0; to_line = INT_MAX; for (p = def, state = 0, start = def; keep_looking; p++) - { - switch (p[0]) { - case ';': /* found a field separator */ - p[0] = '\0'; - switch (state) - { - case 0: /* within a component name */ - comp = start; - break; - case 1: /* within a file name */ - file = start; - break; - case 2: /* within a function name */ - /* after a file name there must be a function name */ - function = start; - break; - case 3: /* within a from-to line range */ - if (strlen (start) > 0) - { - errno = 0; - from_line = strtol (start, &t, 10); - if (errno != 0 || from_line < 0) - { - free (def); - return counter; - } - if (t < p && t[0] == '-') - { - errno = 0; - start = t + 1; - to_line = strtol (start, &t, 10); - if (errno != 0 || to_line < 0 || t != p) - { - free (def); - return counter; - } - } - else /* one number means "match this line only" */ - to_line = from_line; - } - else /* default to 0-max */ - { - from_line = 0; - to_line = INT_MAX; - } - break; - } - start = p + 1; - state += 1; - break; - case '\0': /* found EOL */ - keep_looking = 0; - /* fall through to '/' */ - case '/': /* found a definition separator */ - switch (state) - { - case 4: /* within a log level */ - p[0] = '\0'; - state = 0; - level = get_type ((const char *) start); - if (level == GNUNET_ERROR_TYPE_INVALID || level == GNUNET_ERROR_TYPE_UNSPECIFIED) - { - free (def); - return counter; - } - add_definition (comp, file, function, from_line, to_line, level, force); - counter += 1; - start = p + 1; - break; - default: - break; - } - default: - break; + switch (p[0]) + { + case ';': /* found a field separator */ + p[0] = '\0'; + switch (state) + { + case 0: /* within a component name */ + comp = start; + break; + case 1: /* within a file name */ + file = start; + break; + case 2: /* within a function name */ + /* after a file name there must be a function name */ + function = start; + break; + case 3: /* within a from-to line range */ + if (strlen (start) > 0) + { + errno = 0; + from_line = strtol (start, &t, 10); + if (errno != 0 || from_line < 0) + { + free (def); + return counter; + } + if (t < p && t[0] == '-') + { + errno = 0; + start = t + 1; + to_line = strtol (start, &t, 10); + if (errno != 0 || to_line < 0 || t != p) + { + free (def); + return counter; + } + } + else /* one number means "match this line only" */ + to_line = from_line; + } + else /* default to 0-max */ + { + from_line = 0; + to_line = INT_MAX; + } + break; + } + start = p + 1; + state += 1; + break; + case '\0': /* found EOL */ + keep_looking = 0; + /* fall through to '/' */ + case '/': /* found a definition separator */ + switch (state) + { + case 4: /* within a log level */ + p[0] = '\0'; + state = 0; + level = get_type ((const char *) start); + if (level == GNUNET_ERROR_TYPE_INVALID + || level == GNUNET_ERROR_TYPE_UNSPECIFIED) + { + free (def); + return counter; + } + add_definition (comp, file, function, from_line, to_line, level, + force); + counter += 1; + start = p + 1; + break; + default: + break; + } + default: + break; + } } - } free (def); return counter; } @@ -495,7 +499,8 @@ parse_all_definitions () parse_definitions ("GNUNET_LOG", 0); gnunet_log_parsed = GNUNET_YES; if (gnunet_force_log_parsed == GNUNET_NO) - gnunet_force_log_present = parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; + gnunet_force_log_present = + parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; gnunet_force_log_parsed = GNUNET_YES; } #endif @@ -539,15 +544,16 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile) dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); altlog = FOPEN (fn, "a"); if (altlog == NULL) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); - if (dirwarn) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failed to create or access directory for log file `%s'\n"), - fn); - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); + if (dirwarn) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _ + ("Failed to create or access directory for log file `%s'\n"), + fn); + GNUNET_free (fn); + return GNUNET_SYSERR; + } GNUNET_free (fn); if (GNUNET_stderr != NULL) fclose (GNUNET_stderr); @@ -588,11 +594,11 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls) prev = NULL; pos = loggers; while ((pos != NULL) && - ((pos->logger != logger) || (pos->logger_cls != logger_cls))) - { - prev = pos; - pos = pos->next; - } + ((pos->logger != logger) || (pos->logger_cls != logger_cls))) + { + prev = pos; + pos = pos->next; + } GNUNET_assert (pos != NULL); if (prev == NULL) loggers = pos->next; @@ -612,22 +618,22 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls) */ static void output_message (enum GNUNET_ErrorType kind, const char *comp, - const char *datestr, const char *msg) + const char *datestr, const char *msg) { struct CustomLogger *pos; if (GNUNET_stderr != NULL) - { - fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, - GNUNET_error_type_to_string (kind), msg); - fflush (GNUNET_stderr); - } + { + fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, + GNUNET_error_type_to_string (kind), msg); + fflush (GNUNET_stderr); + } pos = loggers; while (pos != NULL) - { - pos->logger (pos->logger_cls, kind, comp, datestr, msg); - pos = pos->next; - } + { + pos->logger (pos->logger_cls, kind, comp, datestr, msg); + pos = pos->next; + } } @@ -653,15 +659,16 @@ flush_bulk (const char *datestr) else if (last != last_bulk) last--; if (last[0] == '\n') - { - rev = 1; - last[0] = '\0'; - } - ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration - (last_bulk_time)); + { + rev = 1; + last[0] = '\0'; + } + ft = + GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration + (last_bulk_time)); snprintf (msg, sizeof (msg), - _("Message `%.*s' repeated %u times in the last %s\n"), - BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); + _("Message `%.*s' repeated %u times in the last %s\n"), + BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); GNUNET_free (ft); if (rev == 1) last[0] = '\n'; @@ -681,14 +688,14 @@ void GNUNET_log_skip (unsigned int n, int check_reset) { if (n == 0) - { - int ok; - - ok = (0 == skip_log); - skip_log = 0; - if (check_reset) - GNUNET_assert (ok); - } + { + int ok; + + ok = (0 == skip_log); + skip_log = 0; + if (check_reset) + GNUNET_assert (ok); + } else skip_log += n; } @@ -720,40 +727,42 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message, va_end (vacp); buf = malloc (size); if (buf == NULL) - return; /* oops */ + return; /* oops */ VSNPRINTF (buf, size, message, va); time (&timetmp); memset (date, 0, DATE_STR_SIZE); tmptr = localtime (&timetmp); gettimeofday (&timeofday, NULL); if (NULL != tmptr) - { + { #ifdef WINDOWS - LARGE_INTEGER pc; - - pc.QuadPart = 0; - QueryPerformanceCounter (&pc); - strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); - snprintf (date, sizeof (date), date2, - (long long) (pc.QuadPart / - (performance_frequency.QuadPart / 1000))); + LARGE_INTEGER pc; + + pc.QuadPart = 0; + QueryPerformanceCounter (&pc); + strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); + snprintf (date, sizeof (date), date2, + (long long) (pc.QuadPart / + (performance_frequency.QuadPart / 1000))); #else - strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); - snprintf (date, sizeof (date), date2, timeofday.tv_usec); + strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); + snprintf (date, sizeof (date), date2, timeofday.tv_usec); #endif - } + } else strcpy (date, "localtime error"); - if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && (last_bulk_time.abs_value != 0) + if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) + && (last_bulk_time.abs_value != 0) && (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) - { - last_bulk_repeat++; - if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > - BULK_DELAY_THRESHOLD) || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) - flush_bulk (date); - free (buf); - return; - } + { + last_bulk_repeat++; + if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > + BULK_DELAY_THRESHOLD) + || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) + flush_bulk (date); + free (buf); + return; + } flush_bulk (date); strncpy (last_bulk, buf, sizeof (last_bulk)); last_bulk_repeat = 0; @@ -794,7 +803,7 @@ GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...) */ void GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp, - const char *message, ...) + const char *message, ...) { va_list va; char comp_w_pid[128]; @@ -912,44 +921,44 @@ GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen) if (addr == NULL) return _("unknown address"); switch (addr->sa_family) - { - case AF_INET: - if (addrlen != sizeof (struct sockaddr_in)) - return ""; - v4 = (const struct sockaddr_in *) addr; - inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); - if (0 == ntohs (v4->sin_port)) + { + case AF_INET: + if (addrlen != sizeof (struct sockaddr_in)) + return ""; + v4 = (const struct sockaddr_in *) addr; + inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); + if (0 == ntohs (v4->sin_port)) + return buf; + strcat (buf, ":"); + GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port)); + strcat (buf, b2); + return buf; + case AF_INET6: + if (addrlen != sizeof (struct sockaddr_in6)) + return ""; + v6 = (const struct sockaddr_in6 *) addr; + buf[0] = '['; + inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN); + if (0 == ntohs (v6->sin6_port)) + return &buf[1]; + strcat (buf, "]:"); + GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port)); + strcat (buf, b2); return buf; - strcat (buf, ":"); - GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port)); - strcat (buf, b2); - return buf; - case AF_INET6: - if (addrlen != sizeof (struct sockaddr_in6)) - return ""; - v6 = (const struct sockaddr_in6 *) addr; - buf[0] = '['; - inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN); - if (0 == ntohs (v6->sin6_port)) - return &buf[1]; - strcat (buf, "]:"); - GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port)); - strcat (buf, b2); - return buf; - case AF_UNIX: - if (addrlen <= sizeof (sa_family_t)) - return ""; - un = (const struct sockaddr_un *) addr; - off = 0; - if (un->sun_path[0] == '\0') - off++; - snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "", - (int) (addrlen - sizeof (sa_family_t) - 1 - off), - &un->sun_path[off]); - return buf; - default: - return _("invalid address"); - } + case AF_UNIX: + if (addrlen <= sizeof (sa_family_t)) + return ""; + un = (const struct sockaddr_un *) addr; + off = 0; + if (un->sun_path[0] == '\0') + off++; + snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "", + (int) (addrlen - sizeof (sa_family_t) - 1 - off), + &un->sun_path[off]); + return buf; + default: + return _("invalid address"); + } } diff --git a/src/util/configuration.c b/src/util/configuration.c index c69042175..adfd4ea5a 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c @@ -33,6 +33,9 @@ #include "gnunet_os_lib.h" #include "gnunet_strings_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) /** * @brief configuration entry @@ -149,7 +152,7 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg) */ int GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *filename) + const char *filename) { int dirty; char line[256]; @@ -166,87 +169,89 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, fn = GNUNET_STRINGS_filename_expand (filename); if (fn == NULL) return GNUNET_SYSERR; - dirty = cfg->dirty; /* back up value! */ + dirty = cfg->dirty; /* back up value! */ if (NULL == (fp = FOPEN (fn, "r"))) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); + GNUNET_free (fn); + return GNUNET_SYSERR; + } GNUNET_free (fn); ret = GNUNET_OK; section = GNUNET_strdup (""); memset (line, 0, 256); nr = 0; while (NULL != fgets (line, 255, fp)) - { - nr++; - for (i = 0; i < 255; i++) - if (line[i] == '\t') - line[i] = ' '; - if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r') - continue; - emptyline = 1; - for (i = 0; (i < 255 && line[i] != 0); i++) - if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r') - emptyline = 0; - if (emptyline == 1) - continue; - /* remove tailing whitespace */ - for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i])); - i--) - line[i] = '\0'; - if (1 == sscanf (line, "@INLINE@ %191[^\n]", value)) - { - /* @INLINE@ value */ - if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value)) - ret = GNUNET_SYSERR; /* failed to parse included config */ - } - else if (1 == sscanf (line, "[%99[^]]]", value)) - { - /* [value] */ - GNUNET_free (section); - section = GNUNET_strdup (value); - } - else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value)) - { - /* tag = value */ - /* Strip LF */ - i = strlen (value) - 1; - while ((i >= 0) && (isspace ((unsigned char) value[i]))) - value[i--] = '\0'; - /* remove quotes */ - i = 0; - if (value[0] == '"') - { - i = 1; - while ((value[i] != '\0') && (value[i] != '"')) - i++; - if (value[i] == '"') - { - value[i] = '\0'; - i = 1; - } - else - i = 0; - } - GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]); - } - else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag)) - { - /* tag = */ - GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, ""); - } - else { - /* parse error */ - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Syntax error in configuration file `%s' at line %u.\n"), - filename, nr); - ret = GNUNET_SYSERR; - break; + nr++; + for (i = 0; i < 255; i++) + if (line[i] == '\t') + line[i] = ' '; + if (line[0] == '\n' || line[0] == '#' || line[0] == '%' + || line[0] == '\r') + continue; + emptyline = 1; + for (i = 0; (i < 255 && line[i] != 0); i++) + if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r') + emptyline = 0; + if (emptyline == 1) + continue; + /* remove tailing whitespace */ + for (i = strlen (line) - 1; + (i >= 0) && (isspace ((unsigned char) line[i])); i--) + line[i] = '\0'; + if (1 == sscanf (line, "@INLINE@ %191[^\n]", value)) + { + /* @INLINE@ value */ + if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value)) + ret = GNUNET_SYSERR; /* failed to parse included config */ + } + else if (1 == sscanf (line, "[%99[^]]]", value)) + { + /* [value] */ + GNUNET_free (section); + section = GNUNET_strdup (value); + } + else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value)) + { + /* tag = value */ + /* Strip LF */ + i = strlen (value) - 1; + while ((i >= 0) && (isspace ((unsigned char) value[i]))) + value[i--] = '\0'; + /* remove quotes */ + i = 0; + if (value[0] == '"') + { + i = 1; + while ((value[i] != '\0') && (value[i] != '"')) + i++; + if (value[i] == '"') + { + value[i] = '\0'; + i = 1; + } + else + i = 0; + } + GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, + &value[i]); + } + else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag)) + { + /* tag = */ + GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, ""); + } + else + { + /* parse error */ + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Syntax error in configuration file `%s' at line %u.\n"), + filename, nr); + ret = GNUNET_SYSERR; + break; + } } - } GNUNET_assert (0 == fclose (fp)); /* restore dirty flag - anything we set in the meantime * came from disk */ @@ -279,7 +284,7 @@ GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg) */ int GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *filename) + const char *filename) { struct ConfigSection *sec; struct ConfigEntry *ent; @@ -293,67 +298,67 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, if (fn == NULL) return GNUNET_SYSERR; if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)) - { - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + GNUNET_free (fn); + return GNUNET_SYSERR; + } if (NULL == (fp = FOPEN (fn, "w"))) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); + GNUNET_free (fn); + return GNUNET_SYSERR; + } GNUNET_free (fn); error = 0; sec = cfg->sections; while (sec != NULL) - { - if (0 > fprintf (fp, "[%s]\n", sec->name)) - { - error = 1; - break; - } - ent = sec->entries; - while (ent != NULL) - { - if (ent->val != NULL) - { - val = GNUNET_malloc (strlen (ent->val) * 2 + 1); - strcpy (val, ent->val); - while (NULL != (pos = strstr (val, "\n"))) - { - memmove (&pos[2], &pos[1], strlen (&pos[1])); - pos[0] = '\\'; - pos[1] = 'n'; - } - if (0 > fprintf (fp, "%s = %s\n", ent->key, val)) - { - error = 1; - GNUNET_free (val); - break; - } - GNUNET_free (val); - } - ent = ent->next; - } - if (error != 0) - break; - if (0 > fprintf (fp, "\n")) { - error = 1; - break; + if (0 > fprintf (fp, "[%s]\n", sec->name)) + { + error = 1; + break; + } + ent = sec->entries; + while (ent != NULL) + { + if (ent->val != NULL) + { + val = GNUNET_malloc (strlen (ent->val) * 2 + 1); + strcpy (val, ent->val); + while (NULL != (pos = strstr (val, "\n"))) + { + memmove (&pos[2], &pos[1], strlen (&pos[1])); + pos[0] = '\\'; + pos[1] = 'n'; + } + if (0 > fprintf (fp, "%s = %s\n", ent->key, val)) + { + error = 1; + GNUNET_free (val); + break; + } + GNUNET_free (val); + } + ent = ent->next; + } + if (error != 0) + break; + if (0 > fprintf (fp, "\n")) + { + error = 1; + break; + } + sec = sec->next; } - sec = sec->next; - } if (error != 0) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename); GNUNET_assert (0 == fclose (fp)); if (error != 0) - { - cfg->dirty = GNUNET_SYSERR; /* last write failed */ - return GNUNET_SYSERR; - } - cfg->dirty = GNUNET_NO; /* last write succeeded */ + { + cfg->dirty = GNUNET_SYSERR; /* last write failed */ + return GNUNET_SYSERR; + } + cfg->dirty = GNUNET_NO; /* last write succeeded */ return GNUNET_OK; } @@ -367,23 +372,23 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, */ void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_CONFIGURATION_Iterator iter, - void *iter_cls) + GNUNET_CONFIGURATION_Iterator iter, + void *iter_cls) { struct ConfigSection *spos; struct ConfigEntry *epos; spos = cfg->sections; while (spos != NULL) - { - epos = spos->entries; - while (epos != NULL) { - iter (iter_cls, spos->name, epos->key, epos->val); - epos = epos->next; + epos = spos->entries; + while (epos != NULL) + { + iter (iter_cls, spos->name, epos->key, epos->val); + epos = epos->next; + } + spos = spos->next; } - spos = spos->next; - } } @@ -397,10 +402,10 @@ GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, */ void GNUNET_CONFIGURATION_iterate_section_values (const struct - GNUNET_CONFIGURATION_Handle *cfg, - const char *section, - GNUNET_CONFIGURATION_Iterator iter, - void *iter_cls) + GNUNET_CONFIGURATION_Handle *cfg, + const char *section, + GNUNET_CONFIGURATION_Iterator + iter, void *iter_cls) { struct ConfigSection *spos; struct ConfigEntry *epos; @@ -414,10 +419,10 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct epos = spos->entries; while (epos != NULL) - { - iter (iter_cls, spos->name, epos->key, epos->val); - epos = epos->next; - } + { + iter (iter_cls, spos->name, epos->key, epos->val); + epos = epos->next; + } } @@ -429,21 +434,21 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct * @param iter_cls closure for iter */ void -GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle - *cfg, - GNUNET_CONFIGURATION_Section_Iterator - iter, void *iter_cls) +GNUNET_CONFIGURATION_iterate_sections (const struct + GNUNET_CONFIGURATION_Handle *cfg, + GNUNET_CONFIGURATION_Section_Iterator + iter, void *iter_cls) { struct ConfigSection *spos; struct ConfigSection *next; next = cfg->sections; while (next != NULL) - { - spos = next; - next = spos->next; - iter (iter_cls, spos->name); - } + { + spos = next; + next = spos->next; + iter (iter_cls, spos->name); + } } /** @@ -454,7 +459,7 @@ GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle */ void GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *section) + const char *section) { struct ConfigSection *spos; struct ConfigSection *prev; @@ -463,28 +468,28 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, prev = NULL; spos = cfg->sections; while (spos != NULL) - { - if (0 == strcmp (section, spos->name)) { - if (prev == NULL) - cfg->sections = spos->next; - else - prev->next = spos->next; - while (NULL != (ent = spos->entries)) - { - spos->entries = ent->next; - GNUNET_free (ent->key); - GNUNET_free_non_null (ent->val); - GNUNET_free (ent); - cfg->dirty = GNUNET_YES; - } - GNUNET_free (spos->name); - GNUNET_free (spos); - return; + if (0 == strcmp (section, spos->name)) + { + if (prev == NULL) + cfg->sections = spos->next; + else + prev->next = spos->next; + while (NULL != (ent = spos->entries)) + { + spos->entries = ent->next; + GNUNET_free (ent->key); + GNUNET_free_non_null (ent->val); + GNUNET_free (ent); + cfg->dirty = GNUNET_YES; + } + GNUNET_free (spos->name); + GNUNET_free (spos); + return; + } + prev = spos; + spos = spos->next; } - prev = spos; - spos = spos->next; - } } @@ -499,7 +504,7 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, */ static void copy_entry (void *cls, const char *section, const char *option, - const char *value) + const char *value) { struct GNUNET_CONFIGURATION_Handle *dst = cls; @@ -532,7 +537,8 @@ GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg) * @return matching entry, NULL if not found */ static struct ConfigSection * -findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section) +findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *section) { struct ConfigSection *pos; @@ -553,7 +559,7 @@ findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section) */ static struct ConfigEntry * findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, - const char *key) + const char *key) { struct ConfigSection *sec; struct ConfigEntry *pos; @@ -580,7 +586,7 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, */ static void compareEntries (void *cls, const char *section, const char *option, - const char *value) + const char *value) { struct DiffHandle *dh = cls; struct ConfigEntry *entNew; @@ -601,9 +607,9 @@ compareEntries (void *cls, const char *section, const char *option, */ int GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle - *cfgDefault, - const struct GNUNET_CONFIGURATION_Handle - *cfgNew, const char *filename) + *cfgDefault, + const struct GNUNET_CONFIGURATION_Handle + *cfgNew, const char *filename) { int ret; struct DiffHandle diffHandle; @@ -626,28 +632,28 @@ GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle * @param value value to set */ void -GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *section, const char *option, - const char *value) +GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle + *cfg, const char *section, + const char *option, const char *value) { struct ConfigSection *sec; struct ConfigEntry *e; e = findEntry (cfg, section, option); if (e != NULL) - { - GNUNET_free_non_null (e->val); - e->val = GNUNET_strdup (value); - return; - } + { + GNUNET_free_non_null (e->val); + e->val = GNUNET_strdup (value); + return; + } sec = findSection (cfg, section); if (sec == NULL) - { - sec = GNUNET_malloc (sizeof (struct ConfigSection)); - sec->name = GNUNET_strdup (section); - sec->next = cfg->sections; - cfg->sections = sec; - } + { + sec = GNUNET_malloc (sizeof (struct ConfigSection)); + sec->name = GNUNET_strdup (section); + sec->next = cfg->sections; + cfg->sections = sec; + } e = GNUNET_malloc (sizeof (struct ConfigEntry)); e->key = GNUNET_strdup (option); e->val = GNUNET_strdup (value); @@ -665,9 +671,10 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg, * @param number value to set */ void -GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *section, const char *option, - unsigned long long number) +GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle + *cfg, const char *section, + const char *option, + unsigned long long number) { char s[64]; @@ -686,10 +693,11 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg, * @return GNUNET_OK on success, GNUNET_SYSERR on error */ int -GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle - *cfg, const char *section, - const char *option, - unsigned long long *number) +GNUNET_CONFIGURATION_get_value_number (const struct + GNUNET_CONFIGURATION_Handle *cfg, + const char *section, + const char *option, + unsigned long long *number) { struct ConfigEntry *e; @@ -713,9 +721,9 @@ GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle */ int GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle - *cfg, const char *section, - const char *option, - struct GNUNET_TIME_Relative *time) + *cfg, const char *section, + const char *option, + struct GNUNET_TIME_Relative *time) { struct ConfigEntry *e; unsigned long long num; @@ -725,10 +733,10 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle return GNUNET_SYSERR; if ((0 == strcasecmp (e->val, "infinity")) || (0 == strcasecmp (e->val, "forever"))) - { - *time = GNUNET_TIME_UNIT_FOREVER_REL; - return GNUNET_OK; - } + { + *time = GNUNET_TIME_UNIT_FOREVER_REL; + return GNUNET_OK; + } if (1 != SSCANF (e->val, "%llu", &num)) return GNUNET_SYSERR; time->rel_value = (uint64_t) num; @@ -747,18 +755,19 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle * @return GNUNET_OK on success, GNUNET_SYSERR on error */ int -GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle - *cfg, const char *section, - const char *option, char **value) +GNUNET_CONFIGURATION_get_value_string (const struct + GNUNET_CONFIGURATION_Handle *cfg, + const char *section, + const char *option, char **value) { struct ConfigEntry *e; e = findEntry (cfg, section, option); if ((e == NULL) || (e->val == NULL)) - { - *value = NULL; - return GNUNET_SYSERR; - } + { + *value = NULL; + return GNUNET_SYSERR; + } *value = GNUNET_strdup (e->val); return GNUNET_OK; } @@ -777,10 +786,12 @@ GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle * @return GNUNET_OK on success, GNUNET_SYSERR on error */ int -GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle - *cfg, const char *section, - const char *option, const char **choices, - const char **value) +GNUNET_CONFIGURATION_get_value_choice (const struct + GNUNET_CONFIGURATION_Handle *cfg, + const char *section, + const char *option, + const char **choices, + const char **value) { struct ConfigEntry *e; int i; @@ -790,19 +801,19 @@ GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle return GNUNET_SYSERR; i = 0; while (choices[i] != NULL) - { - if (0 == strcasecmp (choices[i], e->val)) - break; - i++; - } + { + if (0 == strcasecmp (choices[i], e->val)) + break; + i++; + } if (choices[i] == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Configuration value '%s' for '%s'" - " in section '%s' is not in set of legal choices\n"), e->val, - option, section); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Configuration value '%s' for '%s'" + " in section '%s' is not in set of legal choices\n"), e->val, + option, section); + return GNUNET_SYSERR; + } *value = choices[i]; return GNUNET_OK; } @@ -816,8 +827,9 @@ GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle * @return GNUNET_YES if so, GNUNET_NO if not. */ int -GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *section, const char *option) +GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle + *cfg, const char *section, + const char *option) { struct ConfigEntry *e; @@ -838,7 +850,7 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, */ char * GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle - *cfg, char *orig) + *cfg, char *orig) { int i; char *prefix; @@ -852,24 +864,25 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0')) i++; if (orig[i] == '\0') - { - post = ""; - } + { + post = ""; + } else - { - orig[i] = '\0'; - post = &orig[i + 1]; - } + { + orig[i] = '\0'; + post = &orig[i + 1]; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix)) - { - if (NULL == (env = getenv (&orig[1]))) + GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], + &prefix)) { - orig[i] = DIR_SEPARATOR; - return orig; + if (NULL == (env = getenv (&orig[1]))) + { + orig[i] = DIR_SEPARATOR; + return orig; + } + prefix = GNUNET_strdup (env); } - prefix = GNUNET_strdup (env); - } result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2); strcpy (result, prefix); if ((strlen (prefix) == 0) || @@ -894,18 +907,18 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle */ int GNUNET_CONFIGURATION_get_value_filename (const struct - GNUNET_CONFIGURATION_Handle *cfg, - const char *section, - const char *option, char **value) + GNUNET_CONFIGURATION_Handle *cfg, + const char *section, + const char *option, char **value) { char *tmp; if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp)) - { - *value = NULL; - return GNUNET_SYSERR; - } + { + *value = NULL; + return GNUNET_SYSERR; + } tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp); *value = GNUNET_STRINGS_filename_expand (tmp); GNUNET_free (tmp); @@ -926,15 +939,15 @@ GNUNET_CONFIGURATION_get_value_filename (const struct */ int GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle - *cfg, const char *section, - const char *option) + *cfg, const char *section, + const char *option) { static const char *yesno[] = { "YES", "NO", NULL }; const char *val; int ret; ret = - GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val); + GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val); if (ret == GNUNET_SYSERR) return ret; if (val == yesno[0]) @@ -955,11 +968,11 @@ GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle */ int GNUNET_CONFIGURATION_iterate_value_filenames (const struct - GNUNET_CONFIGURATION_Handle *cfg, - const char *section, - const char *option, - GNUNET_FileNameCallback cb, - void *cb_cls) + GNUNET_CONFIGURATION_Handle + *cfg, const char *section, + const char *option, + GNUNET_FileNameCallback cb, + void *cb_cls) { char *list; char *pos; @@ -974,46 +987,46 @@ GNUNET_CONFIGURATION_iterate_value_filenames (const struct ret = 0; pos = list; while (1) - { - while (pos[0] == ' ') - pos++; - if (strlen (pos) == 0) - break; - end = pos + 1; - while ((end[0] != ' ') && (end[0] != '\0')) { - if (end[0] == '\\') - { - switch (end[1]) - { - case '\\': - case ' ': - memmove (end, &end[1], strlen (&end[1]) + 1); - case '\0': - /* illegal, but just keep it */ - break; - default: - /* illegal, but just ignore that there was a '/' */ - break; - } - } - end++; + while (pos[0] == ' ') + pos++; + if (strlen (pos) == 0) + break; + end = pos + 1; + while ((end[0] != ' ') && (end[0] != '\0')) + { + if (end[0] == '\\') + { + switch (end[1]) + { + case '\\': + case ' ': + memmove (end, &end[1], strlen (&end[1]) + 1); + case '\0': + /* illegal, but just keep it */ + break; + default: + /* illegal, but just ignore that there was a '/' */ + break; + } + } + end++; + } + old = end[0]; + end[0] = '\0'; + if (strlen (pos) > 0) + { + ret++; + if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos))) + { + ret = GNUNET_SYSERR; + break; + } + } + if (old == '\0') + break; + pos = end + 1; } - old = end[0]; - end[0] = '\0'; - if (strlen (pos) > 0) - { - ret++; - if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos))) - { - ret = GNUNET_SYSERR; - break; - } - } - if (old == '\0') - break; - pos = end + 1; - } GNUNET_free (list); return ret; } @@ -1037,21 +1050,21 @@ escape_name (const char *value) rpos = value; wpos = escaped; while (rpos[0] != '\0') - { - switch (rpos[0]) { - case '\\': - case ' ': - wpos[0] = '\\'; - wpos[1] = rpos[0]; - wpos += 2; - break; - default: - wpos[0] = rpos[0]; - wpos++; + switch (rpos[0]) + { + case '\\': + case ' ': + wpos[0] = '\\'; + wpos[1] = rpos[0]; + wpos += 2; + break; + default: + wpos[0] = rpos[0]; + wpos++; + } + rpos++; } - rpos++; - } return escaped; } @@ -1086,9 +1099,9 @@ test_match (void *cls, const char *fn) */ int GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle - *cfg, const char *section, - const char *option, - const char *value) + *cfg, const char *section, + const char *option, + const char *value) { char *escaped; char *old; @@ -1096,9 +1109,9 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle if (GNUNET_SYSERR == GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option, - &test_match, - (void *) value)) - return GNUNET_NO; /* already exists */ + &test_match, + (void *) value)) + return GNUNET_NO; /* already exists */ if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old)) old = GNUNET_strdup (""); @@ -1130,9 +1143,9 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle */ int GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle - *cfg, const char *section, - const char *option, - const char *value) + *cfg, const char *section, + const char *option, + const char *value) { char *list; char *pos; @@ -1146,55 +1159,55 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle match = escape_name (value); pos = list; while (1) - { - while (pos[0] == ' ') - pos++; - if (strlen (pos) == 0) - break; - end = pos + 1; - while ((end[0] != ' ') && (end[0] != '\0')) - { - if (end[0] == '\\') - { - switch (end[1]) - { - case '\\': - case ' ': - end++; - break; - case '\0': - /* illegal, but just keep it */ - break; - default: - /* illegal, but just ignore that there was a '/' */ - break; - } - } - end++; - } - old = end[0]; - end[0] = '\0'; - if (0 == strcmp (pos, match)) { - if (old != '\0') - memmove (pos, &end[1], strlen (&end[1]) + 1); - else - { - if (pos != list) - pos[-1] = '\0'; - else - pos[0] = '\0'; - } - GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list); - GNUNET_free (list); - GNUNET_free (match); - return GNUNET_OK; + while (pos[0] == ' ') + pos++; + if (strlen (pos) == 0) + break; + end = pos + 1; + while ((end[0] != ' ') && (end[0] != '\0')) + { + if (end[0] == '\\') + { + switch (end[1]) + { + case '\\': + case ' ': + end++; + break; + case '\0': + /* illegal, but just keep it */ + break; + default: + /* illegal, but just ignore that there was a '/' */ + break; + } + } + end++; + } + old = end[0]; + end[0] = '\0'; + if (0 == strcmp (pos, match)) + { + if (old != '\0') + memmove (pos, &end[1], strlen (&end[1]) + 1); + else + { + if (pos != list) + pos[-1] = '\0'; + else + pos[0] = '\0'; + } + GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list); + GNUNET_free (list); + GNUNET_free (match); + return GNUNET_OK; + } + if (old == '\0') + break; + end[0] = old; + pos = end + 1; } - if (old == '\0') - break; - end[0] = old; - pos = end + 1; - } GNUNET_free (list); GNUNET_free (match); return GNUNET_NO; @@ -1211,7 +1224,7 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle */ int GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *filename) + const char *filename) { char *baseconfig; char *ipath; @@ -1221,21 +1234,21 @@ GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, return GNUNET_SYSERR; baseconfig = NULL; GNUNET_asprintf (&baseconfig, "%s%s%s", ipath, DIR_SEPARATOR_STR, - "defaults.conf"); + "defaults.conf"); GNUNET_free (ipath); if ((GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, baseconfig)) || (!((filename == NULL) || - (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename))))) - { - GNUNET_free (baseconfig); - return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR; - } + (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename))))) + { + GNUNET_free (baseconfig); + return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR; + } GNUNET_free (baseconfig); if (((GNUNET_YES != - GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) && + GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) && (filename != NULL)) GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", - filename); + filename); if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) && (GNUNET_YES == diff --git a/src/util/connection.c b/src/util/connection.c index 6b2cf75e0..4beafa3f3 100644 --- a/src/util/connection.c +++ b/src/util/connection.c @@ -41,6 +41,10 @@ #define DEBUG_CONNECTION GNUNET_EXTRA_LOGGING +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + /** * Possible functions to call after connect failed or succeeded. */ @@ -329,7 +333,8 @@ GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock) * @return the boxed socket handle */ struct GNUNET_CONNECTION_Handle * -GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket) +GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle + *osSocket) { struct GNUNET_CONNECTION_Handle *ret; @@ -352,8 +357,8 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket) */ struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, - void *access_cls, - struct GNUNET_NETWORK_Handle *lsock) + void *access_cls, + struct GNUNET_NETWORK_Handle *lsock) { struct GNUNET_CONNECTION_Handle *ret; char addr[128]; @@ -374,94 +379,94 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, addrlen = sizeof (addr); sock = - GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen); + GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen); if (NULL == sock) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept"); - return NULL; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept"); + return NULL; + } if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t))) - { - GNUNET_break (0); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - return NULL; - } + { + GNUNET_break (0); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); + return NULL; + } sa = (struct sockaddr *) addr; v6 = (struct sockaddr_in6 *) addr; if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr))) - { - /* convert to V4 address */ - v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); - memset (v4, 0, sizeof (struct sockaddr_in)); - v4->sin_family = AF_INET; + { + /* convert to V4 address */ + v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); + memset (v4, 0, sizeof (struct sockaddr_in)); + v4->sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - v4->sin_len = (u_char) sizeof (struct sockaddr_in); + v4->sin_len = (u_char) sizeof (struct sockaddr_in); #endif - memcpy (&v4->sin_addr, - &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) - - sizeof (struct in_addr)], - sizeof (struct in_addr)); - v4->sin_port = v6->sin6_port; - uaddr = v4; - addrlen = sizeof (struct sockaddr_in); - } + memcpy (&v4->sin_addr, + &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) - + sizeof (struct in_addr)], + sizeof (struct in_addr)); + v4->sin_port = v6->sin6_port; + uaddr = v4; + addrlen = sizeof (struct sockaddr_in); + } else - { - uaddr = GNUNET_malloc (addrlen); - memcpy (uaddr, addr, addrlen); - } + { + uaddr = GNUNET_malloc (addrlen); + memcpy (uaddr, addr, addrlen); + } gcp = NULL; gc.uid = 0; gc.gid = 0; if (sa->sa_family == AF_UNIX) - { + { #if HAVE_GETPEEREID - /* most BSDs */ - if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid)) - gcp = &gc; + /* most BSDs */ + if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid)) + gcp = &gc; #else #ifdef SO_PEERCRED - /* largely traditional GNU/Linux */ - olen = sizeof (uc); - if ((0 == - getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc, - &olen)) && (olen == sizeof (uc))) - { - gc.uid = uc.uid; - gc.gid = uc.gid; - gcp = &gc; - } + /* largely traditional GNU/Linux */ + olen = sizeof (uc); + if ((0 == + getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, + &uc, &olen)) && (olen == sizeof (uc))) + { + gc.uid = uc.uid; + gc.gid = uc.gid; + gcp = &gc; + } #else #if HAVE_GETPEERUCRED - /* this is for Solaris 10 */ - ucred_t *uc; - - uc = NULL; - if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc)) - { - gc.uid = ucred_geteuid (uc); - gc.gid = ucred_getegid (uc); - gcp = &gc; - } - ucred_free (uc); + /* this is for Solaris 10 */ + ucred_t *uc; + + uc = NULL; + if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc)) + { + gc.uid = ucred_geteuid (uc); + gc.gid = ucred_getegid (uc); + gcp = &gc; + } + ucred_free (uc); #endif #endif #endif - } + } if ((access != NULL) && (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen)))) - { - if (aret == GNUNET_NO) - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"), - GNUNET_a2s (uaddr, addrlen)); - GNUNET_break (GNUNET_OK == - GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR)); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - GNUNET_free (uaddr); - return NULL; - } + { + if (aret == GNUNET_NO) + LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"), + GNUNET_a2s (uaddr, addrlen)); + GNUNET_break (GNUNET_OK == + GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR)); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); + GNUNET_free (uaddr); + return NULL; + } ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); @@ -469,8 +474,8 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, ret->addrlen = addrlen; ret->sock = sock; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"), - GNUNET_a2s (uaddr, addrlen), ret); + LOG (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"), + GNUNET_a2s (uaddr, addrlen), ret); #endif return ret; } @@ -485,7 +490,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, */ int GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock, - void **addr, size_t * addrlen) + void **addr, size_t * addrlen) { if ((sock->addr == NULL) || (sock->addrlen == 0)) return GNUNET_NO; @@ -518,7 +523,8 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); * @param tc unused */ static void -destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +destroy_continuation (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_CONNECTION_Handle *sock = cls; GNUNET_CONNECTION_TransmitReadyNotify notify; @@ -527,83 +533,83 @@ destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sock->destroy_task = GNUNET_SCHEDULER_NO_TASK; GNUNET_assert (sock->dns_active == NULL); if (0 != (sock->ccs & COCO_TRANSMIT_READY)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Destroy waits for CCS-TR to be done (%p)\n", sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Destroy waits for CCS-TR to be done (%p)\n", sock); #endif - sock->ccs |= COCO_DESTROY_CONTINUATION; - return; - } + sock->ccs |= COCO_DESTROY_CONTINUATION; + return; + } if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Destroy waits for write_task to be done (%p)\n", sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Destroy waits for write_task to be done (%p)\n", sock); #endif - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); - sock->destroy_task = - GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation, - sock); - return; - } + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); + sock->destroy_task = + GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation, + sock); + return; + } if (0 != (sock->ccs & COCO_RECEIVE_AGAIN)) - { - sock->ccs |= COCO_DESTROY_CONTINUATION; - return; - } + { + sock->ccs |= COCO_DESTROY_CONTINUATION; + return; + } if (sock->sock != NULL) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock); #endif - if (sock->persist != GNUNET_YES) - { - if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) - && (errno != ENOTCONN) && (errno != ECONNRESET)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown"); + if (sock->persist != GNUNET_YES) + { + if ((GNUNET_YES != + GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) + && (errno != ENOTCONN) && (errno != ECONNRESET)) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown"); + } } - } if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); - sock->destroy_task = - GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation, - sock); - return; - } + { + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); + sock->destroy_task = + GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation, + sock); + return; + } #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock); #endif while (NULL != (pos = sock->ap_head)) - { - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); - GNUNET_SCHEDULER_cancel (pos->task); - GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos); - GNUNET_free (pos); - } + { + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); + GNUNET_SCHEDULER_cancel (pos->task); + GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos); + GNUNET_free (pos); + } GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); GNUNET_assert (sock->ccs == COCO_NONE); if (NULL != (notify = sock->nth.notify_ready)) - { - sock->nth.notify_ready = NULL; - notify (sock->nth.notify_ready_cls, 0, NULL); - } + { + sock->nth.notify_ready = NULL; + notify (sock->nth.notify_ready_cls, 0, NULL); + } if (sock->sock != NULL) - { - if (sock->persist != GNUNET_YES) - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); - else - GNUNET_free (sock->sock); /* at least no memory leak (we deliberately - * leak the socket in this special case) ... */ - } + { + if (sock->persist != GNUNET_YES) + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); + else + GNUNET_free (sock->sock); /* at least no memory leak (we deliberately + * leak the socket in this special case) ... */ + } GNUNET_free_non_null (sock->addr); GNUNET_free_non_null (sock->hostname); GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", - sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", sock); #endif GNUNET_free (sock->write_buffer); GNUNET_free (sock); @@ -632,13 +638,13 @@ static void connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) { #if DEBUG_CONNECTION - GNUNET_log ((0 != - strncmp (h->hostname, "localhost:", - 10)) ? GNUNET_ERROR_TYPE_INFO : - GNUNET_ERROR_TYPE_WARNING, - _ - ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), - h->hostname, h->port); + LOG ((0 != + strncmp (h->hostname, "localhost:", + 10)) ? GNUNET_ERROR_TYPE_INFO : + GNUNET_ERROR_TYPE_WARNING, + _ + ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), + h->hostname, h->port); #endif /* connect failed / timed out */ GNUNET_break (h->ap_head == NULL); @@ -648,40 +654,39 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) /* trigger jobs that used to wait on "connect_task" */ if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connect_fail_continuation triggers receive_again (%p)\n", h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "connect_fail_continuation triggers receive_again (%p)\n", h); #endif - h->ccs -= COCO_RECEIVE_AGAIN; - h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); - } + h->ccs -= COCO_RECEIVE_AGAIN; + h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); + } if (0 != (h->ccs & COCO_TRANSMIT_READY)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n", - h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n", + h); #endif - GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); - GNUNET_SCHEDULER_cancel (h->nth.timeout_task); - h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; - h->ccs -= COCO_TRANSMIT_READY; - GNUNET_assert (h->nth.notify_ready != NULL); - GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); - h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h); - } + GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); + GNUNET_SCHEDULER_cancel (h->nth.timeout_task); + h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; + h->ccs -= COCO_TRANSMIT_READY; + GNUNET_assert (h->nth.notify_ready != NULL); + GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); + h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h); + } if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connect_fail_continuation runs destroy_continuation (%p)\n", - h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "connect_fail_continuation runs destroy_continuation (%p)\n", h); #endif - h->ccs -= COCO_DESTROY_CONTINUATION; - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); - h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); - } + h->ccs -= COCO_DESTROY_CONTINUATION; + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); + h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); + } } @@ -694,48 +699,48 @@ static void connect_success_continuation (struct GNUNET_CONNECTION_Handle *h) { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n", - GNUNET_a2s (h->addr, h->addrlen), h); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n", + GNUNET_a2s (h->addr, h->addrlen), h); #endif /* trigger jobs that waited for the connection */ if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connect_success_continuation runs receive_again (%p)\n", h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "connect_success_continuation runs receive_again (%p)\n", h); #endif - h->ccs -= COCO_RECEIVE_AGAIN; - h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); - } + h->ccs -= COCO_RECEIVE_AGAIN; + h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); + } if (0 != (h->ccs & COCO_TRANSMIT_READY)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", - h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", + h); #endif - GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); - GNUNET_SCHEDULER_cancel (h->nth.timeout_task); - h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; - h->ccs -= COCO_TRANSMIT_READY; - GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); - GNUNET_assert (h->nth.notify_ready != NULL); - h->write_task = - GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining - (h->nth.transmit_timeout), h->sock, - &transmit_ready, h); - } + GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); + GNUNET_SCHEDULER_cancel (h->nth.timeout_task); + h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; + h->ccs -= COCO_TRANSMIT_READY; + GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); + GNUNET_assert (h->nth.notify_ready != NULL); + h->write_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining + (h->nth.transmit_timeout), h->sock, + &transmit_ready, h); + } if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connect_success_continuation runs destroy_continuation (%p)\n", - h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "connect_success_continuation runs destroy_continuation (%p)\n", + h); #endif - h->ccs -= COCO_DESTROY_CONTINUATION; - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); - h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); - } + h->ccs -= COCO_DESTROY_CONTINUATION; + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); + h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); + } } @@ -748,7 +753,7 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h) */ static void connect_probe_continuation (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct AddressProbe *ap = cls; struct GNUNET_CONNECTION_Handle *h = ap->h; @@ -763,15 +768,15 @@ connect_probe_continuation (void *cls, error = 0; if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || (GNUNET_OK != - GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error, - &len)) || (error != 0)) - { - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); - GNUNET_free (ap); - if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO)) - connect_fail_continuation (h); - return; - } + GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, + &error, &len)) || (error != 0)) + { + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); + GNUNET_free (ap); + if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO)) + connect_fail_continuation (h); + return; + } GNUNET_assert (h->sock == NULL); h->sock = ap->sock; GNUNET_assert (h->addr == NULL); @@ -781,12 +786,12 @@ connect_probe_continuation (void *cls, GNUNET_free (ap); /* cancel all other attempts */ while (NULL != (pos = h->ap_head)) - { - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); - GNUNET_SCHEDULER_cancel (pos->task); - GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos); - GNUNET_free (pos); - } + { + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); + GNUNET_SCHEDULER_cancel (pos->task); + GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos); + GNUNET_free (pos); + } connect_success_continuation (h); } @@ -801,28 +806,27 @@ connect_probe_continuation (void *cls, */ static void try_connect_using_address (void *cls, const struct sockaddr *addr, - socklen_t addrlen) + socklen_t addrlen) { struct GNUNET_CONNECTION_Handle *h = cls; struct AddressProbe *ap; struct GNUNET_TIME_Relative delay; if (addr == NULL) - { - h->dns_active = NULL; - if ( (NULL == h->ap_head) && - (NULL == h->sock) ) - connect_fail_continuation (h); - return; - } + { + h->dns_active = NULL; + if ((NULL == h->ap_head) && (NULL == h->sock)) + connect_fail_continuation (h); + return; + } if (h->sock != NULL) - return; /* already connected */ + return; /* already connected */ GNUNET_assert (h->addr == NULL); /* try to connect */ #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, - h->port, GNUNET_a2s (addr, addrlen), h->port); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, + h->port, GNUNET_a2s (addr, addrlen), h->port); #endif ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen); ap->addr = (const struct sockaddr *) &ap[1]; @@ -831,58 +835,59 @@ try_connect_using_address (void *cls, const struct sockaddr *addr, ap->h = h; switch (ap->addr->sa_family) - { - case AF_INET: - ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port); - break; - case AF_INET6: - ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port); - break; - default: - GNUNET_break (0); - GNUNET_free (ap); - return; /* not supported by us */ - } - ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0); + { + case AF_INET: + ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port); + break; + case AF_INET6: + ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port); + break; + default: + GNUNET_break (0); + GNUNET_free (ap); + return; /* not supported by us */ + } + ap->sock = + GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0); if (ap->sock == NULL) - { - GNUNET_free (ap); - return; /* not supported by OS */ - } + { + GNUNET_free (ap); + return; /* not supported by OS */ + } #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), - GNUNET_a2s (ap->addr, ap->addrlen), h); + LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), + GNUNET_a2s (ap->addr, ap->addrlen), h); #endif if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && (errno != EINPROGRESS)) - { - /* maybe refused / unsupported address, try next */ - GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); + { + /* maybe refused / unsupported address, try next */ + LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); #if 0 - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Failed to connect to `%s' (%p)\n"), - GNUNET_a2s (ap->addr, ap->addrlen), h); + LOG (GNUNET_ERROR_TYPE_INFO, + _("Failed to connect to `%s' (%p)\n"), + GNUNET_a2s (ap->addr, ap->addrlen), h); #endif - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); - GNUNET_free (ap); - return; - } + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); + GNUNET_free (ap); + return; + } GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap); delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT; if (h->nth.notify_ready != NULL) delay = - GNUNET_TIME_relative_min (delay, - GNUNET_TIME_absolute_get_remaining (h-> - nth.transmit_timeout)); + GNUNET_TIME_relative_min (delay, + GNUNET_TIME_absolute_get_remaining (h-> + nth.transmit_timeout)); if (h->receiver != NULL) delay = - GNUNET_TIME_relative_min (delay, - GNUNET_TIME_absolute_get_remaining - (h->receive_timeout)); + GNUNET_TIME_relative_min (delay, + GNUNET_TIME_absolute_get_remaining + (h->receive_timeout)); ap->task = - GNUNET_SCHEDULER_add_write_net (delay, ap->sock, - &connect_probe_continuation, ap); + GNUNET_SCHEDULER_add_write_net (delay, ap->sock, + &connect_probe_continuation, ap); } @@ -897,13 +902,13 @@ try_connect_using_address (void *cls, const struct sockaddr *addr, * @return the socket handle */ struct GNUNET_CONNECTION_Handle * -GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle - *cfg, const char *hostname, - uint16_t port) +GNUNET_CONNECTION_create_from_connect (const struct + GNUNET_CONFIGURATION_Handle *cfg, + const char *hostname, uint16_t port) { struct GNUNET_CONNECTION_Handle *ret; - - GNUNET_assert (0 < strlen (hostname)); /* sanity check */ + + GNUNET_assert (0 < strlen (hostname)); /* sanity check */ ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); ret->cfg = cfg; ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; @@ -911,9 +916,9 @@ GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle ret->port = port; ret->hostname = GNUNET_strdup (hostname); ret->dns_active = - GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC, - GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT, - &try_connect_using_address, ret); + GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC, + GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT, + &try_connect_using_address, ret); return ret; } @@ -929,15 +934,15 @@ GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle */ struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct - GNUNET_CONFIGURATION_Handle - *cfg, const char *unixpath) + GNUNET_CONFIGURATION_Handle + *cfg, const char *unixpath) { #ifdef AF_UNIX struct GNUNET_CONNECTION_Handle *ret; struct sockaddr_un *un; size_t slen; - GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ + GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ un = GNUNET_malloc (sizeof (struct sockaddr_un)); un->sun_family = AF_UNIX; slen = strlen (unixpath); @@ -962,20 +967,20 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct ret->addrlen = slen; ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); if (NULL == ret->sock) - { - GNUNET_free (ret->addr); - GNUNET_free (ret->write_buffer); - GNUNET_free (ret); - return NULL; - } + { + GNUNET_free (ret->addr); + GNUNET_free (ret->write_buffer); + GNUNET_free (ret); + return NULL; + } if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen)) - { - /* Just return; we expect everything to work eventually so don't fail HARD */ - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock)); - ret->sock = NULL; - return ret; - } + { + /* Just return; we expect everything to work eventually so don't fail HARD */ + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock)); + ret->sock = NULL; + return ret; + } connect_success_continuation (ret); return ret; #else @@ -996,8 +1001,8 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct */ struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_sockaddr (int af_family, - const struct sockaddr *serv_addr, - socklen_t addrlen) + const struct sockaddr *serv_addr, + socklen_t addrlen) { struct GNUNET_NETWORK_Handle *s; struct GNUNET_CONNECTION_Handle *ret; @@ -1005,29 +1010,29 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family, s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); if (s == NULL) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, - "socket"); - return NULL; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, + "socket"); + return NULL; + } if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && (errno != EINPROGRESS)) - { - /* maybe refused / unsupported address, try next */ - GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr, - addrlen)); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); - return NULL; - } + { + /* maybe refused / unsupported address, try next */ + LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); + LOG (GNUNET_ERROR_TYPE_INFO, + _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr, + addrlen)); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); + return NULL; + } ret = GNUNET_CONNECTION_create_from_existing (s); ret->addr = GNUNET_malloc (addrlen); memcpy (ret->addr, serv_addr, addrlen); ret->addrlen = addrlen; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), - GNUNET_a2s (serv_addr, addrlen), ret); + LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), + GNUNET_a2s (serv_addr, addrlen), ret); #endif return ret; } @@ -1045,7 +1050,7 @@ int GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock) { if ((sock->ap_head != NULL) || (sock->dns_active != NULL)) - return GNUNET_YES; /* still trying to connect */ + return GNUNET_YES; /* still trying to connect */ return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES; } @@ -1065,23 +1070,23 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock) */ void GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock, - int finish_pending_write) + int finish_pending_write) { if (GNUNET_NO == finish_pending_write) - { - if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) { - GNUNET_SCHEDULER_cancel (sock->write_task); - sock->write_task = GNUNET_SCHEDULER_NO_TASK; - sock->write_buffer_off = 0; + if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) + { + GNUNET_SCHEDULER_cancel (sock->write_task); + sock->write_task = GNUNET_SCHEDULER_NO_TASK; + sock->write_buffer_off = 0; + } + sock->nth.notify_ready = NULL; } - sock->nth.notify_ready = NULL; - } if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL)) - { - GNUNET_RESOLVER_request_cancel (sock->dns_active); - sock->dns_active = NULL; - } + { + GNUNET_RESOLVER_request_cancel (sock->dns_active); + sock->dns_active = NULL; + } GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock); @@ -1097,8 +1102,8 @@ signal_timeout (struct GNUNET_CONNECTION_Handle *sh) GNUNET_CONNECTION_Receiver receiver; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Network signals time out to receiver (%p)!\n", sh); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Network signals time out to receiver (%p)!\n", sh); #endif GNUNET_assert (NULL != (receiver = sh->receiver)); sh->receiver = NULL; @@ -1136,63 +1141,62 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sh->read_task = GNUNET_SCHEDULER_NO_TASK; if ((GNUNET_YES == sh->ignore_shutdown) && (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) - { - /* ignore shutdown request, go again immediately */ + { + /* ignore shutdown request, go again immediately */ #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Ignoring shutdown signal per configuration\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Ignoring shutdown signal per configuration\n"); #endif - sh->read_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining - (sh->receive_timeout), sh->sock, - &receive_ready, sh); - return; - } + sh->read_task = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining + (sh->receive_timeout), sh->sock, + &receive_ready, sh); + return; + } now = GNUNET_TIME_absolute_get (); if ((now.abs_value > sh->receive_timeout.abs_value) || (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) || (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) - { + { #if DEBUG_CONNECTION - if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Receive from `%s' encounters error: time out by %llums... (%p)\n", - GNUNET_a2s (sh->addr, sh->addrlen), - GNUNET_TIME_absolute_get_duration (sh->receive_timeout). - rel_value, sh); + if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Receive from `%s' encounters error: time out by %llums... (%p)\n", + GNUNET_a2s (sh->addr, sh->addrlen), + GNUNET_TIME_absolute_get_duration (sh->receive_timeout). + rel_value, sh); #endif - signal_timeout (sh); - return; - } + signal_timeout (sh); + return; + } if (sh->sock == NULL) - { - /* connect failed for good */ + { + /* connect failed for good */ #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Receive encounters error, socket closed... (%p)\n", sh); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Receive encounters error, socket closed... (%p)\n", sh); #endif - signal_error (sh, ECONNREFUSED); - return; - } + signal_error (sh, ECONNREFUSED); + return; + } GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock)); RETRY: ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max); if (ret == -1) - { - if (errno == EINTR) - goto RETRY; + { + if (errno == EINTR) + goto RETRY; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", - STRERROR (errno)); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", + STRERROR (errno)); #endif - signal_error (sh, errno); - return; - } + signal_error (sh, errno); + return; + } #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "receive_ready read %u/%u bytes from `%s' (%p)!\n", - (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen), - sh); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "receive_ready read %u/%u bytes from `%s' (%p)!\n", + (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh); #endif GNUNET_assert (NULL != (receiver = sh->receiver)); sh->receiver = NULL; @@ -1218,32 +1222,32 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sh->read_task = GNUNET_SCHEDULER_NO_TASK; if (sh->sock == NULL) - { - /* not connected and no longer trying */ + { + /* not connected and no longer trying */ #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Receive encounters error, socket closed (%p)...\n", sh); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Receive encounters error, socket closed (%p)...\n", sh); #endif - signal_error (sh, ECONNREFUSED); - return; - } + signal_error (sh, ECONNREFUSED); + return; + } now = GNUNET_TIME_absolute_get (); if ((now.abs_value > sh->receive_timeout.abs_value) || (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Receive encounters error: time out (%p)...\n", sh); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Receive encounters error: time out (%p)...\n", sh); #endif - signal_timeout (sh); - return; - } + signal_timeout (sh); + return; + } GNUNET_assert (sh->sock != NULL); /* connect succeeded, wait for data! */ sh->read_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining - (sh->receive_timeout), sh->sock, - &receive_ready, sh); + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining + (sh->receive_timeout), sh->sock, + &receive_ready, sh); } @@ -1262,31 +1266,31 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ void GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max, - struct GNUNET_TIME_Relative timeout, - GNUNET_CONNECTION_Receiver receiver, - void *receiver_cls) + struct GNUNET_TIME_Relative timeout, + GNUNET_CONNECTION_Receiver receiver, + void *receiver_cls) { struct GNUNET_SCHEDULER_TaskContext tc; GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) && - (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) && - (sock->receiver == NULL)); + (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) && + (sock->receiver == NULL)); sock->receiver = receiver; sock->receiver_cls = receiver_cls; sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); sock->max = max; if (sock->sock != NULL) - { - memset (&tc, 0, sizeof (tc)); - tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; - receive_again (sock, &tc); - return; - } + { + memset (&tc, 0, sizeof (tc)); + tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; + receive_again (sock, &tc); + return; + } if ((sock->dns_active == NULL) && (sock->ap_head == NULL)) - { - receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); - return; - } + { + receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); + return; + } sock->ccs += COCO_RECEIVE_AGAIN; } @@ -1299,7 +1303,7 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max, */ void GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock, - int do_ignore) + int do_ignore) { sock->ignore_shutdown = do_ignore; } @@ -1317,15 +1321,15 @@ void * GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock) { if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task)); - sock->read_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task)); + sock->read_task = GNUNET_SCHEDULER_NO_TASK; + } else - { - GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN)); - sock->ccs -= COCO_RECEIVE_AGAIN; - } + { + GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN)); + sock->ccs -= COCO_RECEIVE_AGAIN; + } sock->receiver = NULL; return sock->receiver_cls; } @@ -1356,18 +1360,18 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock) return GNUNET_NO; sock->nth.notify_ready = NULL; if (sock->write_buffer_size - sock->write_buffer_off < size) - { - /* need to compact */ - memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos], - used); - sock->write_buffer_off -= sock->write_buffer_pos; - sock->write_buffer_pos = 0; - } + { + /* need to compact */ + memmove (sock->write_buffer, + &sock->write_buffer[sock->write_buffer_pos], used); + sock->write_buffer_off -= sock->write_buffer_pos; + sock->write_buffer_pos = 0; + } avail = sock->write_buffer_size - sock->write_buffer_off; GNUNET_assert (avail >= size); size = - notify (sock->nth.notify_ready_cls, avail, - &sock->write_buffer[sock->write_buffer_off]); + notify (sock->nth.notify_ready_cls, avail, + &sock->write_buffer[sock->write_buffer_off]); GNUNET_assert (size <= avail); sock->write_buffer_off += size; return GNUNET_YES; @@ -1392,17 +1396,17 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CONNECTION_TransmitReadyNotify notify; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock); #endif sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", - sock->hostname, sock->port, GNUNET_a2s (sock->addr, - sock->addrlen), sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", + sock->hostname, sock->port, GNUNET_a2s (sock->addr, + sock->addrlen), sock); #endif GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY)); - sock->ccs -= COCO_TRANSMIT_READY; /* remove request */ + sock->ccs -= COCO_TRANSMIT_READY; /* remove request */ notify = sock->nth.notify_ready; sock->nth.notify_ready = NULL; notify (sock->nth.notify_ready_cls, 0, NULL); @@ -1425,9 +1429,9 @@ connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CONNECTION_TransmitReadyNotify notify; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmission request of size %u fails (%s/%u), connection failed (%p).\n", - sock->nth.notify_size, sock->hostname, sock->port, sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmission request of size %u fails (%s/%u), connection failed (%p).\n", + sock->nth.notify_size, sock->hostname, sock->port, sock); #endif sock->write_task = GNUNET_SCHEDULER_NO_TASK; notify = sock->nth.notify_ready; @@ -1447,20 +1451,20 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock) GNUNET_CONNECTION_TransmitReadyNotify notify; if (NULL != sock->sock) - { - GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); - sock->sock = NULL; - } + { + GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); + sock->sock = NULL; + } if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (sock->read_task); - sock->read_task = GNUNET_SCHEDULER_NO_TASK; - signal_timeout (sock); - return; - } + { + GNUNET_SCHEDULER_cancel (sock->read_task); + sock->read_task = GNUNET_SCHEDULER_NO_TASK; + signal_timeout (sock); + return; + } if (sock->nth.notify_ready == NULL) - return; /* nobody to tell about it */ + return; /* nobody to tell about it */ notify = sock->nth.notify_ready; sock->nth.notify_ready = NULL; notify (sock->nth.notify_ready_cls, 0, NULL); @@ -1484,129 +1488,129 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) size_t have; #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock); #endif GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK); sock->write_task = GNUNET_SCHEDULER_NO_TASK; GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - { - if (sock->ignore_shutdown == GNUNET_YES) - goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ + { + if (sock->ignore_shutdown == GNUNET_YES) + goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmit to `%s' fails, shutdown happened (%p).\n", - GNUNET_a2s (sock->addr, sock->addrlen), sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmit to `%s' fails, shutdown happened (%p).\n", + GNUNET_a2s (sock->addr, sock->addrlen), sock); #endif - notify = sock->nth.notify_ready; - if (NULL != notify) - { - sock->nth.notify_ready = NULL; - notify (sock->nth.notify_ready_cls, 0, NULL); + notify = sock->nth.notify_ready; + if (NULL != notify) + { + sock->nth.notify_ready = NULL; + notify (sock->nth.notify_ready_cls, 0, NULL); + } + return; } - return; - } if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmit to `%s' fails, time out reached (%p).\n", - GNUNET_a2s (sock->addr, sock->addrlen), sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmit to `%s' fails, time out reached (%p).\n", + GNUNET_a2s (sock->addr, sock->addrlen), sock); #endif - notify = sock->nth.notify_ready; - GNUNET_assert (NULL != notify); - sock->nth.notify_ready = NULL; - notify (sock->nth.notify_ready_cls, 0, NULL); - return; - } + notify = sock->nth.notify_ready; + GNUNET_assert (NULL != notify); + sock->nth.notify_ready = NULL; + notify (sock->nth.notify_ready_cls, 0, NULL); + return; + } GNUNET_assert (NULL != sock->sock); if (tc->write_ready == NULL) - { - /* special circumstances (in particular, - * PREREQ_DONE after connect): not yet ready to write, - * but no "fatal" error either. Hence retry. */ - goto SCHEDULE_WRITE; - } + { + /* special circumstances (in particular, + * PREREQ_DONE after connect): not yet ready to write, + * but no "fatal" error either. Hence retry. */ + goto SCHEDULE_WRITE; + } if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _ - ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"), - sock); + LOG (GNUNET_ERROR_TYPE_INFO, + _ + ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"), + sock); #endif - transmit_error (sock); - return; /* connect failed for good, we're finished */ - } + transmit_error (sock); + return; /* connect failed for good, we're finished */ + } GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos); if ((sock->nth.notify_ready != NULL) && (sock->write_buffer_size < sock->nth.notify_size)) - { - sock->write_buffer = - GNUNET_realloc (sock->write_buffer, sock->nth.notify_size); - sock->write_buffer_size = sock->nth.notify_size; - } + { + sock->write_buffer = + GNUNET_realloc (sock->write_buffer, sock->nth.notify_size); + sock->write_buffer_size = sock->nth.notify_size; + } process_notify (sock); have = sock->write_buffer_off - sock->write_buffer_pos; if (have == 0) - { - /* no data ready for writing, terminate write loop */ - return; - } + { + /* no data ready for writing, terminate write loop */ + return; + } GNUNET_assert (have <= sock->write_buffer_size); GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size); GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size); RETRY: ret = - GNUNET_NETWORK_socket_send (sock->sock, - &sock->write_buffer[sock->write_buffer_pos], - have); + GNUNET_NETWORK_socket_send (sock->sock, + &sock->write_buffer[sock->write_buffer_pos], + have); if (ret == -1) - { - if (errno == EINTR) - goto RETRY; + { + if (errno == EINTR) + goto RETRY; #if 0 - int en = errno; + int en = errno; - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"), - GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en)); + LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"), + GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en)); #endif #if DEBUG_CONNECTION - GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send"); + LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send"); #endif - transmit_error (sock); - return; - } + transmit_error (sock); + return; + } #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", - (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), - sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", + (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), + sock); #endif sock->write_buffer_pos += ret; if (sock->write_buffer_pos == sock->write_buffer_off) - { - /* transmitted all pending data */ - sock->write_buffer_pos = 0; - sock->write_buffer_off = 0; - } + { + /* transmitted all pending data */ + sock->write_buffer_pos = 0; + sock->write_buffer_off = 0; + } if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready)) - return; /* all data sent! */ + return; /* all data sent! */ /* not done writing, schedule more */ SCHEDULE_WRITE: #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Re-scheduling transmit_ready (more to do) (%p).\n", sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Re-scheduling transmit_ready (more to do) (%p).\n", sock); #endif have = sock->write_buffer_off - sock->write_buffer_pos; GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0)); if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) sock->write_task = - GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == - NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : - GNUNET_TIME_absolute_get_remaining - (sock->nth.transmit_timeout), - sock->sock, &transmit_ready, sock); + GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == + NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : + GNUNET_TIME_absolute_get_remaining + (sock->nth.transmit_timeout), + sock->sock, &transmit_ready, sock); } @@ -1625,17 +1629,17 @@ SCHEDULE_WRITE: * NULL if we are already going to notify someone else (busy) */ struct GNUNET_CONNECTION_TransmitHandle * -GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock, - size_t size, - struct GNUNET_TIME_Relative timeout, - GNUNET_CONNECTION_TransmitReadyNotify - notify, void *notify_cls) +GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle + *sock, size_t size, + struct GNUNET_TIME_Relative timeout, + GNUNET_CONNECTION_TransmitReadyNotify + notify, void *notify_cls) { if (sock->nth.notify_ready != NULL) - { - GNUNET_assert (0); - return NULL; - } + { + GNUNET_assert (0); + return NULL; + } GNUNET_assert (notify != NULL); GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size); @@ -1649,36 +1653,36 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock, GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task); if ((sock->sock == NULL) && (sock->ap_head == NULL) && (sock->dns_active == NULL)) - { - if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) - GNUNET_SCHEDULER_cancel (sock->write_task); - sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock); - return &sock->nth; - } + { + if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) + GNUNET_SCHEDULER_cancel (sock->write_task); + sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock); + return &sock->nth; + } if (GNUNET_SCHEDULER_NO_TASK != sock->write_task) return &sock->nth; if (sock->sock != NULL) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", - sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", + sock); #endif - sock->write_task = - GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining - (sock->nth.transmit_timeout), - sock->sock, &transmit_ready, sock); - } + sock->write_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining + (sock->nth.transmit_timeout), + sock->sock, &transmit_ready, sock); + } else - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", - sock); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", + sock); #endif - sock->ccs |= COCO_TRANSMIT_READY; - sock->nth.timeout_task = - GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock); - } + sock->ccs |= COCO_TRANSMIT_READY; + sock->nth.timeout_task = + GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock); + } return &sock->nth; } @@ -1690,28 +1694,28 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock, */ void GNUNET_CONNECTION_notify_transmit_ready_cancel (struct - GNUNET_CONNECTION_TransmitHandle - *th) + GNUNET_CONNECTION_TransmitHandle + *th) { GNUNET_assert (th->notify_ready != NULL); if (0 != (th->sh->ccs & COCO_TRANSMIT_READY)) - { + { #if DEBUG_CONNECTION - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th); #endif - GNUNET_SCHEDULER_cancel (th->timeout_task); - th->timeout_task = GNUNET_SCHEDULER_NO_TASK; - th->sh->ccs -= COCO_TRANSMIT_READY; - } + GNUNET_SCHEDULER_cancel (th->timeout_task); + th->timeout_task = GNUNET_SCHEDULER_NO_TASK; + th->sh->ccs -= COCO_TRANSMIT_READY; + } else - { - if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK) { - GNUNET_SCHEDULER_cancel (th->sh->write_task); - th->sh->write_task = GNUNET_SCHEDULER_NO_TASK; + if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK) + { + GNUNET_SCHEDULER_cancel (th->sh->write_task); + th->sh->write_task = GNUNET_SCHEDULER_NO_TASK; + } } - } th->notify_ready = NULL; } diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c index b7be764b5..a33b619ff 100644 --- a/src/util/container_bloomfilter.c +++ b/src/util/container_bloomfilter.c @@ -44,6 +44,12 @@ #include "gnunet_container_lib.h" #include "gnunet_disk_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + struct GNUNET_CONTAINER_BloomFilter { @@ -83,8 +89,8 @@ struct GNUNET_CONTAINER_BloomFilter * @return number of bytes used for the data of the bloom filter */ size_t -GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter - *bf) +GNUNET_CONTAINER_bloomfilter_get_size (const struct + GNUNET_CONTAINER_BloomFilter *bf) { if (bf == NULL) return 0; @@ -100,10 +106,10 @@ GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter */ struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter - *bf) + *bf) { return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize, - bf->addressesPerElement); + bf->addressesPerElement); } @@ -175,7 +181,7 @@ testBit (char *bitArray, unsigned int bitIdx) */ static void incrementBit (char *bitArray, unsigned int bitIdx, - const struct GNUNET_DISK_FileHandle *fh) + const struct GNUNET_DISK_FileHandle *fh) { off_t fileSlot; unsigned char value; @@ -191,25 +197,25 @@ incrementBit (char *bitArray, unsigned int bitIdx, targetLoc = bitIdx % 2; GNUNET_assert (fileSlot == - GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); + GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); if (1 != GNUNET_DISK_file_read (fh, &value, 1)) value = 0; low = value & 0xF; high = (value & (~0xF)) >> 4; if (targetLoc == 0) - { - if (low < 0xF) - low++; - } + { + if (low < 0xF) + low++; + } else - { - if (high < 0xF) - high++; - } + { + if (high < 0xF) + high++; + } value = ((high << 4) | low); GNUNET_assert (fileSlot == - GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); + GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1)); } @@ -223,7 +229,7 @@ incrementBit (char *bitArray, unsigned int bitIdx, */ static void decrementBit (char *bitArray, unsigned int bitIdx, - const struct GNUNET_DISK_FileHandle *fh) + const struct GNUNET_DISK_FileHandle *fh) { off_t fileSlot; unsigned char value; @@ -232,7 +238,7 @@ decrementBit (char *bitArray, unsigned int bitIdx, unsigned int targetLoc; if (GNUNET_DISK_handle_invalid (fh)) - return; /* cannot decrement! */ + return; /* cannot decrement! */ /* Each char slot in the counter file holds two 4 bit counters */ fileSlot = bitIdx / 2; targetLoc = bitIdx % 2; @@ -244,23 +250,23 @@ decrementBit (char *bitArray, unsigned int bitIdx, /* decrement, but once we have reached the max, never go back! */ if (targetLoc == 0) - { - if ((low > 0) && (low < 0xF)) - low--; - if (low == 0) { - clearBit (bitArray, bitIdx); + if ((low > 0) && (low < 0xF)) + low--; + if (low == 0) + { + clearBit (bitArray, bitIdx); + } } - } else - { - if ((high > 0) && (high < 0xF)) - high--; - if (high == 0) { - clearBit (bitArray, bitIdx); + if ((high > 0) && (high < 0xF)) + high--; + if (high == 0) + { + clearBit (bitArray, bitIdx); + } } - } value = ((high << 4) | low); GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET); GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1)); @@ -289,19 +295,19 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size) GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET); while (bytesleft > 0) - { - if (bytesleft > BUFFSIZE) { - res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE); - bytesleft -= BUFFSIZE; + if (bytesleft > BUFFSIZE) + { + res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE); + bytesleft -= BUFFSIZE; + } + else + { + res = GNUNET_DISK_file_write (fh, buffer, bytesleft); + bytesleft = 0; + } + GNUNET_assert (res != GNUNET_SYSERR); } - else - { - res = GNUNET_DISK_file_write (fh, buffer, bytesleft); - bytesleft = 0; - } - GNUNET_assert (res != GNUNET_SYSERR); - } GNUNET_free (buffer); return GNUNET_OK; } @@ -334,7 +340,7 @@ typedef int (*BitIterator) (void *cls, */ static void iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf, - BitIterator callback, void *arg, const GNUNET_HashCode * key) + BitIterator callback, void *arg, const GNUNET_HashCode * key) { GNUNET_HashCode tmp[2]; int bitCount; @@ -345,27 +351,27 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf, tmp[0] = *key; round = 0; while (bitCount > 0) - { - while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t))) - { - if (GNUNET_YES != - callback (arg, bf, - (((uint32_t *) & tmp[round & 1])[slot]) & - ((bf->bitArraySize * 8) - 1))) - return; - slot++; - bitCount--; - if (bitCount == 0) - break; - } - if (bitCount > 0) { - GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode), - &tmp[(round + 1) & 1]); - round++; - slot = 0; + while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t))) + { + if (GNUNET_YES != + callback (arg, bf, + (((uint32_t *) & tmp[round & 1])[slot]) & + ((bf->bitArraySize * 8) - 1))) + return; + slot++; + bitCount--; + if (bitCount == 0) + break; + } + if (bitCount > 0) + { + GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode), + &tmp[(round + 1) & 1]); + round++; + slot = 0; + } } - } } @@ -378,8 +384,9 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf, * @return GNUNET_YES */ static int -incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, - unsigned int bit) +incrementBitCallback (void *cls, + const struct GNUNET_CONTAINER_BloomFilter *bf, + unsigned int bit) { struct GNUNET_CONTAINER_BloomFilter *b = cls; @@ -397,8 +404,9 @@ incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, * @return GNUNET_YES */ static int -decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, - unsigned int bit) +decrementBitCallback (void *cls, + const struct GNUNET_CONTAINER_BloomFilter *bf, + unsigned int bit) { struct GNUNET_CONTAINER_BloomFilter *b = cls; @@ -417,15 +425,15 @@ decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, */ static int testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, - unsigned int bit) + unsigned int bit) { int *arg = cls; if (GNUNET_NO == testBit (bf->bitArray, bit)) - { - *arg = GNUNET_NO; - return GNUNET_NO; - } + { + *arg = GNUNET_NO; + return GNUNET_NO; + } return GNUNET_YES; } @@ -443,7 +451,7 @@ testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, */ struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, - unsigned int k) + unsigned int k) { struct GNUNET_CONTAINER_BloomFilter *bf; char *rbuff; @@ -459,32 +467,32 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, ui = 1; while (ui < size) ui *= 2; - size = ui; /* make sure it's a power of 2 */ + size = ui; /* make sure it's a power of 2 */ bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); /* Try to open a bloomfilter file */ bf->fh = - GNUNET_DISK_file_open (filename, - GNUNET_DISK_OPEN_READWRITE | - GNUNET_DISK_OPEN_CREATE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_file_open (filename, + GNUNET_DISK_OPEN_READWRITE | + GNUNET_DISK_OPEN_CREATE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); if (NULL == bf->fh) - { - GNUNET_free (bf); - return NULL; - } + { + GNUNET_free (bf); + return NULL; + } bf->filename = GNUNET_strdup (filename); /* Alloc block */ bf->bitArray = GNUNET_malloc_large (size); if (bf->bitArray == NULL) - { - if (bf->fh != NULL) - GNUNET_DISK_file_close (bf->fh); - GNUNET_free (bf->filename); - GNUNET_free (bf); - return NULL; - } + { + if (bf->fh != NULL) + GNUNET_DISK_file_close (bf->fh); + GNUNET_free (bf->filename); + GNUNET_free (bf); + return NULL; + } bf->bitArraySize = size; bf->addressesPerElement = k; memset (bf->bitArray, 0, bf->bitArraySize); @@ -493,28 +501,27 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, rbuff = GNUNET_malloc (BUFFSIZE); pos = 0; while (pos < size * 8) - { - int res; - - res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE); - if (res == -1) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", - bf->filename); - } - if (res == 0) - break; /* is ok! we just did not use that many bits yet */ - for (i = 0; i < res; i++) { - if ((rbuff[i] & 0x0F) != 0) - setBit (bf->bitArray, pos + i * 2); - if ((rbuff[i] & 0xF0) != 0) - setBit (bf->bitArray, pos + i * 2 + 1); + int res; + + res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE); + if (res == -1) + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename); + } + if (res == 0) + break; /* is ok! we just did not use that many bits yet */ + for (i = 0; i < res; i++) + { + if ((rbuff[i] & 0x0F) != 0) + setBit (bf->bitArray, pos + i * 2); + if ((rbuff[i] & 0xF0) != 0) + setBit (bf->bitArray, pos + i * 2 + 1); + } + if (res < BUFFSIZE) + break; + pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */ } - if (res < BUFFSIZE) - break; - pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */ - } GNUNET_free (rbuff); return bf; } @@ -535,7 +542,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, */ struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size, - unsigned int k) + unsigned int k) { struct GNUNET_CONTAINER_BloomFilter *bf; size_t ui; @@ -546,19 +553,19 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size, while (ui < size) ui *= 2; if (size != ui) - { - GNUNET_break (0); - return NULL; - } + { + GNUNET_break (0); + return NULL; + } bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); bf->filename = NULL; bf->fh = NULL; bf->bitArray = GNUNET_malloc_large (size); if (bf->bitArray == NULL) - { - GNUNET_free (bf); - return NULL; - } + { + GNUNET_free (bf); + return NULL; + } bf->bitArraySize = size; bf->addressesPerElement = k; if (data != NULL) @@ -580,8 +587,8 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size, */ int GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct - GNUNET_CONTAINER_BloomFilter *bf, - char *data, size_t size) + GNUNET_CONTAINER_BloomFilter *bf, + char *data, size_t size) { if (NULL == bf) return GNUNET_SYSERR; @@ -638,7 +645,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf) */ int GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter - *bf, const GNUNET_HashCode * e) + *bf, const GNUNET_HashCode * e) { int res; @@ -658,7 +665,7 @@ GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter */ void GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf, - const GNUNET_HashCode * e) + const GNUNET_HashCode * e) { if (NULL == bf) return; @@ -678,7 +685,7 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf, */ int GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf, - const char *data, size_t size) + const char *data, size_t size) { unsigned int i; unsigned int n; @@ -712,8 +719,8 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf, */ int GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf, - const struct GNUNET_CONTAINER_BloomFilter - *to_or, size_t size) + const struct GNUNET_CONTAINER_BloomFilter + *to_or, size_t size) { unsigned int i; unsigned int n; @@ -743,7 +750,7 @@ GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf, */ void GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf, - const GNUNET_HashCode * e) + const GNUNET_HashCode * e) { if (NULL == bf) return; @@ -765,9 +772,9 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf, */ void GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, - GNUNET_HashCodeIterator iterator, - void *iterator_cls, size_t size, - unsigned int k) + GNUNET_HashCodeIterator iterator, + void *iterator_cls, size_t size, + unsigned int k) { GNUNET_HashCode hc; unsigned int i; @@ -776,7 +783,7 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, i = 1; while (i < size) i *= 2; - size = i; /* make sure it's a power of 2 */ + size = i; /* make sure it's a power of 2 */ bf->bitArraySize = size; bf->bitArray = GNUNET_malloc (size); diff --git a/src/util/container_heap.c b/src/util/container_heap.c index cd4d7909a..051b85a25 100644 --- a/src/util/container_heap.c +++ b/src/util/container_heap.c @@ -28,6 +28,7 @@ #include "platform.h" #include "gnunet_util_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) #define DEBUG 0 @@ -115,10 +116,10 @@ check (const struct GNUNET_CONTAINER_HeapNode *node) if (NULL == node) return; GNUNET_assert (node->tree_size == - ((node->left_child == - NULL) ? 0 : 1 + node->left_child->tree_size) + - ((node->right_child == - NULL) ? 0 : 1 + node->right_child->tree_size)); + ((node->left_child == + NULL) ? 0 : 1 + node->left_child->tree_size) + + ((node->right_child == + NULL) ? 0 : 1 + node->right_child->tree_size)); check (node->left_child); check (node->right_child); } @@ -197,7 +198,7 @@ GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap) */ GNUNET_CONTAINER_HeapCostType GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode - *node) + * node) { return node->cost; } @@ -213,8 +214,8 @@ GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode */ static int node_iterator (const struct GNUNET_CONTAINER_Heap *heap, - struct GNUNET_CONTAINER_HeapNode *node, - GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls) + struct GNUNET_CONTAINER_HeapNode *node, + GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls) { if (node == NULL) return GNUNET_YES; @@ -237,8 +238,8 @@ node_iterator (const struct GNUNET_CONTAINER_Heap *heap, */ void GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap, - GNUNET_CONTAINER_HeapIterator iterator, - void *iterator_cls) + GNUNET_CONTAINER_HeapIterator iterator, + void *iterator_cls) { (void) node_iterator (heap, heap->root, iterator, iterator_cls); } @@ -268,9 +269,9 @@ GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap) pos = heap->root; element = pos->element; heap->walk_pos = - (0 == - GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, - 2)) ? pos->right_child : pos->left_child; + (0 == + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, + 2)) ? pos->right_child : pos->left_child; return element; } @@ -285,51 +286,51 @@ GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap) */ static void insert_node (struct GNUNET_CONTAINER_Heap *heap, - struct GNUNET_CONTAINER_HeapNode *pos, - struct GNUNET_CONTAINER_HeapNode *node) + struct GNUNET_CONTAINER_HeapNode *pos, + struct GNUNET_CONTAINER_HeapNode *node) { struct GNUNET_CONTAINER_HeapNode *parent; GNUNET_assert (node->parent == NULL); while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >= - node->cost) - : (pos->cost <= node->cost)) - { - /* node is descendent of pos */ - pos->tree_size += (1 + node->tree_size); - if (pos->left_child == NULL) + node->cost) + : (pos->cost <= node->cost)) { - pos->left_child = node; - node->parent = pos; - return; + /* node is descendent of pos */ + pos->tree_size += (1 + node->tree_size); + if (pos->left_child == NULL) + { + pos->left_child = node; + node->parent = pos; + return; + } + if (pos->right_child == NULL) + { + pos->right_child = node; + node->parent = pos; + return; + } + /* keep it balanced by descending into smaller subtree */ + if (pos->left_child->tree_size < pos->right_child->tree_size) + pos = pos->left_child; + else + pos = pos->right_child; } - if (pos->right_child == NULL) - { - pos->right_child = node; - node->parent = pos; - return; - } - /* keep it balanced by descending into smaller subtree */ - if (pos->left_child->tree_size < pos->right_child->tree_size) - pos = pos->left_child; - else - pos = pos->right_child; - } /* make 'node' parent of 'pos' */ parent = pos->parent; pos->parent = NULL; node->parent = parent; if (NULL == parent) - { - heap->root = node; - } + { + heap->root = node; + } else - { - if (parent->left_child == pos) - parent->left_child = node; - else - parent->right_child = node; - } + { + if (parent->left_child == pos) + parent->left_child = node; + else + parent->right_child = node; + } /* insert 'pos' below 'node' */ insert_node (heap, node, pos); CHECK (pos); @@ -345,8 +346,9 @@ insert_node (struct GNUNET_CONTAINER_Heap *heap, * @return node for the new element */ struct GNUNET_CONTAINER_HeapNode * -GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element, - GNUNET_CONTAINER_HeapCostType cost) +GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, + void *element, + GNUNET_CONTAINER_HeapCostType cost) { struct GNUNET_CONTAINER_HeapNode *node; @@ -382,27 +384,27 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap) heap->size--; ret = root->element; if (root->left_child == NULL) - { - heap->root = root->right_child; - if (root->right_child != NULL) - root->right_child->parent = NULL; - } + { + heap->root = root->right_child; + if (root->right_child != NULL) + root->right_child->parent = NULL; + } else if (root->right_child == NULL) - { - heap->root = root->left_child; - root->left_child->parent = NULL; - } + { + heap->root = root->left_child; + root->left_child->parent = NULL; + } else - { - root->left_child->parent = NULL; - root->right_child->parent = NULL; - heap->root = root->left_child; - insert_node (heap, heap->root, root->right_child); - } + { + root->left_child->parent = NULL; + root->right_child->parent = NULL; + heap->root = root->left_child; + insert_node (heap, heap->root, root->right_child); + } GNUNET_free (root); #if DEBUG GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || - (heap->size == heap->root->tree_size + 1)); + (heap->size == heap->root->tree_size + 1)); CHECK (heap->root); #endif return ret; @@ -434,43 +436,43 @@ remove_node (struct GNUNET_CONTAINER_HeapNode *node) /* unlink 'node' itself and insert children in its place */ if (node->parent == NULL) - { - if (node->left_child != NULL) { - heap->root = node->left_child; - node->left_child->parent = NULL; - if (node->right_child != NULL) - { - node->right_child->parent = NULL; - insert_node (heap, heap->root, node->right_child); - } + if (node->left_child != NULL) + { + heap->root = node->left_child; + node->left_child->parent = NULL; + if (node->right_child != NULL) + { + node->right_child->parent = NULL; + insert_node (heap, heap->root, node->right_child); + } + } + else + { + heap->root = node->right_child; + if (node->right_child != NULL) + node->right_child->parent = NULL; + } } - else - { - heap->root = node->right_child; - if (node->right_child != NULL) - node->right_child->parent = NULL; - } - } else - { - if (node->parent->left_child == node) - node->parent->left_child = NULL; - else - node->parent->right_child = NULL; - if (node->left_child != NULL) { - node->left_child->parent = NULL; - node->parent->tree_size -= (1 + node->left_child->tree_size); - insert_node (heap, node->parent, node->left_child); - } - if (node->right_child != NULL) - { - node->right_child->parent = NULL; - node->parent->tree_size -= (1 + node->right_child->tree_size); - insert_node (heap, node->parent, node->right_child); + if (node->parent->left_child == node) + node->parent->left_child = NULL; + else + node->parent->right_child = NULL; + if (node->left_child != NULL) + { + node->left_child->parent = NULL; + node->parent->tree_size -= (1 + node->left_child->tree_size); + insert_node (heap, node->parent, node->left_child); + } + if (node->right_child != NULL) + { + node->right_child->parent = NULL; + node->parent->tree_size -= (1 + node->right_child->tree_size); + insert_node (heap, node->parent, node->right_child); + } } - } node->parent = NULL; node->left_child = NULL; node->right_child = NULL; @@ -504,7 +506,7 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node) #if DEBUG CHECK (heap->root); GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || - (heap->size == heap->root->tree_size + 1)); + (heap->size == heap->root->tree_size + 1)); #endif return ret; } @@ -519,19 +521,19 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node) */ void GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap, - struct GNUNET_CONTAINER_HeapNode *node, - GNUNET_CONTAINER_HeapCostType new_cost) + struct GNUNET_CONTAINER_HeapNode *node, + GNUNET_CONTAINER_HeapCostType new_cost) { #if DEBUG GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || - (heap->size == heap->root->tree_size + 1)); + (heap->size == heap->root->tree_size + 1)); CHECK (heap->root); #endif remove_node (node); #if DEBUG CHECK (heap->root); GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) || - (heap->size == heap->root->tree_size + 2)); + (heap->size == heap->root->tree_size + 2)); #endif node->cost = new_cost; if (heap->root == NULL) @@ -541,7 +543,7 @@ GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap, #if DEBUG CHECK (heap->root); GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || - (heap->size == heap->root->tree_size + 1)); + (heap->size == heap->root->tree_size + 1)); #endif } diff --git a/src/util/container_meta_data.c b/src/util/container_meta_data.c index 16173cca9..1ea77f63a 100644 --- a/src/util/container_meta_data.c +++ b/src/util/container_meta_data.c @@ -32,6 +32,8 @@ #include #include +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Meta data item. */ @@ -160,10 +162,10 @@ GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md) if (md == NULL) return; while (NULL != (item = md->items)) - { - md->items = item->next; - meta_item_free (item); - } + { + md->items = item->next; + meta_item_free (item); + } GNUNET_free_non_null (md->sbuf); GNUNET_free (md); } @@ -182,10 +184,10 @@ GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md) if (md == NULL) return; while (NULL != (item = md->items)) - { - md->items = item->next; - meta_item_free (item); - } + { + md->items = item->next; + meta_item_free (item); + } GNUNET_free_non_null (md->sbuf); memset (md, 0, sizeof (struct GNUNET_CONTAINER_MetaData)); } @@ -204,9 +206,9 @@ GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md) */ int GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData - *md1, - const struct GNUNET_CONTAINER_MetaData - *md2) + *md1, + const struct GNUNET_CONTAINER_MetaData + *md2) { struct MetaItem *i; struct MetaItem *j; @@ -219,24 +221,24 @@ GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData i = md1->items; while (NULL != i) - { - found = GNUNET_NO; - j = md2->items; - while (NULL != j) { - if ((i->type == j->type) && (i->format == j->format) && - (i->data_size == j->data_size) && - (0 == memcmp (i->data, j->data, i->data_size))) - { - found = GNUNET_YES; - break; - } - j = j->next; + found = GNUNET_NO; + j = md2->items; + while (NULL != j) + { + if ((i->type == j->type) && (i->format == j->format) && + (i->data_size == j->data_size) && + (0 == memcmp (i->data, j->data, i->data_size))) + { + found = GNUNET_YES; + break; + } + j = j->next; + } + if (found == GNUNET_NO) + return GNUNET_NO; + i = i->next; } - if (found == GNUNET_NO) - return GNUNET_NO; - i = i->next; - } return GNUNET_YES; } @@ -261,11 +263,11 @@ GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData */ int GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md, - const char *plugin_name, - enum EXTRACTOR_MetaType type, - enum EXTRACTOR_MetaFormat format, - const char *data_mime_type, const char *data, - size_t data_len) + const char *plugin_name, + enum EXTRACTOR_MetaType type, + enum EXTRACTOR_MetaFormat format, + const char *data_mime_type, + const char *data, size_t data_len) { struct MetaItem *prev; struct MetaItem *pos; @@ -275,28 +277,28 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md, prev = NULL; pos = md->items; while (NULL != pos) - { - if (pos->data_size < data_len) - break; - if ((pos->type == type) && (pos->data_size == data_len) && - (0 == memcmp (pos->data, data, data_len))) { - if ((pos->mime_type == NULL) && (data_mime_type != NULL)) - { - pos->mime_type = GNUNET_strdup (data_mime_type); - invalidate_sbuf (md); - } - if ((pos->format == EXTRACTOR_METAFORMAT_C_STRING) && - (format == EXTRACTOR_METAFORMAT_UTF8)) - { - pos->format = EXTRACTOR_METAFORMAT_UTF8; - invalidate_sbuf (md); - } - return GNUNET_SYSERR; + if (pos->data_size < data_len) + break; + if ((pos->type == type) && (pos->data_size == data_len) && + (0 == memcmp (pos->data, data, data_len))) + { + if ((pos->mime_type == NULL) && (data_mime_type != NULL)) + { + pos->mime_type = GNUNET_strdup (data_mime_type); + invalidate_sbuf (md); + } + if ((pos->format == EXTRACTOR_METAFORMAT_C_STRING) && + (format == EXTRACTOR_METAFORMAT_UTF8)) + { + pos->format = EXTRACTOR_METAFORMAT_UTF8; + invalidate_sbuf (md); + } + return GNUNET_SYSERR; + } + prev = pos; + pos = pos->next; } - prev = pos; - pos = pos->next; - } md->item_count++; i = GNUNET_malloc (sizeof (struct MetaItem)); i->type = type; @@ -308,23 +310,23 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md, else prev->next = i; i->mime_type = - (data_mime_type == NULL) ? NULL : GNUNET_strdup (data_mime_type); + (data_mime_type == NULL) ? NULL : GNUNET_strdup (data_mime_type); i->plugin_name = (plugin_name == NULL) ? NULL : GNUNET_strdup (plugin_name); i->data = GNUNET_malloc (data_len); memcpy (i->data, data, data_len); /* change OS native dir separators to unix '/' and others to '_' */ if (type == EXTRACTOR_METATYPE_FILENAME) - { - p = i->data; - while ((*p != '\0') && (p < i->data + data_len)) { - if (*p == DIR_SEPARATOR) - *p = '/'; - else if (*p == '\\') - *p = '_'; - p++; + p = i->data; + while ((*p != '\0') && (p < i->data + data_len)) + { + if (*p == DIR_SEPARATOR) + *p = '/'; + else if (*p == '\\') + *p = '_'; + p++; + } } - } invalidate_sbuf (md); return GNUNET_OK; } @@ -347,14 +349,14 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md, * @return 0 (to continue) */ static int -merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, - enum EXTRACTOR_MetaFormat format, const char *data_mime_type, - const char *data, size_t data_len) +merge_helper (void *cls, const char *plugin_name, + enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, + const char *data_mime_type, const char *data, size_t data_len) { struct GNUNET_CONTAINER_MetaData *md = cls; (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format, - data_mime_type, data, data_len); + data_mime_type, data, data_len); return 0; } @@ -368,7 +370,7 @@ merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, */ void GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md, - const struct GNUNET_CONTAINER_MetaData *in) + const struct GNUNET_CONTAINER_MetaData *in) { GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md); } @@ -386,8 +388,8 @@ GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md, */ int GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md, - enum EXTRACTOR_MetaType type, - const char *data, size_t data_len) + enum EXTRACTOR_MetaType type, + const char *data, size_t data_len) { struct MetaItem *pos; struct MetaItem *prev; @@ -395,24 +397,24 @@ GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md, prev = NULL; pos = md->items; while (NULL != pos) - { - if ((pos->type == type) && - ((data == NULL) || - ((pos->data_size == data_len) && - (0 == memcmp (pos->data, data, data_len))))) { - if (prev == NULL) - md->items = pos->next; - else - prev->next = pos->next; - meta_item_free (pos); - md->item_count--; - invalidate_sbuf (md); - return GNUNET_OK; + if ((pos->type == type) && + ((data == NULL) || + ((pos->data_size == data_len) && + (0 == memcmp (pos->data, data, data_len))))) + { + if (prev == NULL) + md->items = pos->next; + else + prev->next = pos->next; + meta_item_free (pos); + md->item_count--; + invalidate_sbuf (md); + return GNUNET_OK; + } + prev = pos; + pos = pos->next; } - prev = pos; - pos = pos->next; - } return GNUNET_SYSERR; } @@ -425,19 +427,20 @@ GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md, */ void GNUNET_CONTAINER_meta_data_add_publication_date (struct - GNUNET_CONTAINER_MetaData *md) + GNUNET_CONTAINER_MetaData + *md) { char *dat; struct GNUNET_TIME_Absolute t; t = GNUNET_TIME_absolute_get (); GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_METATYPE_PUBLICATION_DATE, - NULL, 0); + NULL, 0); dat = GNUNET_STRINGS_absolute_time_to_string (t); GNUNET_CONTAINER_meta_data_insert (md, "", - EXTRACTOR_METATYPE_PUBLICATION_DATE, - EXTRACTOR_METAFORMAT_UTF8, "text/plain", - dat, strlen (dat) + 1); + EXTRACTOR_METATYPE_PUBLICATION_DATE, + EXTRACTOR_METAFORMAT_UTF8, "text/plain", + dat, strlen (dat) + 1); GNUNET_free (dat); } @@ -451,9 +454,9 @@ GNUNET_CONTAINER_meta_data_add_publication_date (struct * @return number of entries */ int -GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md, - EXTRACTOR_MetaDataProcessor iter, - void *iter_cls) +GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData + *md, EXTRACTOR_MetaDataProcessor iter, + void *iter_cls) { struct MetaItem *pos; @@ -463,13 +466,13 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md, return md->item_count; pos = md->items; while (NULL != pos) - { - if (0 != - iter (iter_cls, pos->plugin_name, pos->type, pos->format, - pos->mime_type, pos->data, pos->data_size)) - return md->item_count; - pos = pos->next; - } + { + if (0 != + iter (iter_cls, pos->plugin_name, pos->type, pos->format, + pos->mime_type, pos->data, pos->data_size)) + return md->item_count; + pos = pos->next; + } return md->item_count; } @@ -486,7 +489,7 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md, */ char * GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData - *md, enum EXTRACTOR_MetaType type) + *md, enum EXTRACTOR_MetaType type) { struct MetaItem *pos; @@ -494,13 +497,13 @@ GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData return NULL; pos = md->items; while (NULL != pos) - { - if ((type == pos->type) && - ((pos->format == EXTRACTOR_METAFORMAT_UTF8) || - (pos->format == EXTRACTOR_METAFORMAT_C_STRING))) - return GNUNET_strdup (pos->data); - pos = pos->next; - } + { + if ((type == pos->type) && + ((pos->format == EXTRACTOR_METAFORMAT_UTF8) || + (pos->format == EXTRACTOR_METAFORMAT_C_STRING))) + return GNUNET_strdup (pos->data); + pos = pos->next; + } return NULL; } @@ -518,8 +521,8 @@ GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData */ char * GNUNET_CONTAINER_meta_data_get_first_by_types (const struct - GNUNET_CONTAINER_MetaData *md, - ...) + GNUNET_CONTAINER_MetaData *md, + ...) { char *ret; va_list args; @@ -530,15 +533,15 @@ GNUNET_CONTAINER_meta_data_get_first_by_types (const struct ret = NULL; va_start (args, md); while (1) - { - type = va_arg (args, enum EXTRACTOR_MetaType); - - if (type == -1) - break; - ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type); - if (ret != NULL) - break; - } + { + type = va_arg (args, enum EXTRACTOR_MetaType); + + if (type == -1) + break; + ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type); + if (ret != NULL) + break; + } va_end (args); return ret; } @@ -553,8 +556,9 @@ GNUNET_CONTAINER_meta_data_get_first_by_types (const struct * @return number of bytes in thumbnail, 0 if not available */ size_t -GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData - * md, unsigned char **thumb) +GNUNET_CONTAINER_meta_data_get_thumbnail (const struct + GNUNET_CONTAINER_MetaData * md, + unsigned char **thumb) { struct MetaItem *pos; struct MetaItem *match; @@ -564,19 +568,19 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData match = NULL; pos = md->items; while (NULL != pos) - { - if ((NULL != pos->mime_type) && - (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) && - (pos->format == EXTRACTOR_METAFORMAT_BINARY)) { - if (match == NULL) - match = pos; - else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) && - (pos->type == EXTRACTOR_METATYPE_THUMBNAIL)) - match = pos; + if ((NULL != pos->mime_type) && + (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) && + (pos->format == EXTRACTOR_METAFORMAT_BINARY)) + { + if (match == NULL) + match = pos; + else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) && + (pos->type == EXTRACTOR_METATYPE_THUMBNAIL)) + match = pos; + } + pos = pos->next; } - pos = pos->next; - } if ((match == NULL) || (match->data_size == 0)) return 0; *thumb = GNUNET_malloc (match->data_size); @@ -593,7 +597,7 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData */ struct GNUNET_CONTAINER_MetaData * GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData - *md) + *md) { struct GNUNET_CONTAINER_MetaData *ret; struct MetaItem *pos; @@ -603,12 +607,12 @@ GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData ret = GNUNET_CONTAINER_meta_data_create (); pos = md->items; while (NULL != pos) - { - GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type, - pos->format, pos->mime_type, pos->data, - pos->data_size); - pos = pos->next; - } + { + GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type, + pos->format, pos->mime_type, + pos->data, pos->data_size); + pos = pos->next; + } return ret; } @@ -629,7 +633,7 @@ GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData */ static int try_compression (const char *data, size_t oldSize, char **result, - size_t * newSize) + size_t * newSize) { char *tmp; uLongf dlen; @@ -644,14 +648,14 @@ try_compression (const char *data, size_t oldSize, char **result, tmp = GNUNET_malloc (dlen); if (Z_OK == compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9)) - { - if (dlen < oldSize) { - *result = tmp; - *newSize = dlen; - return GNUNET_YES; + if (dlen < oldSize) + { + *result = tmp; + *newSize = dlen; + return GNUNET_YES; + } } - } GNUNET_free (tmp); return GNUNET_NO; } @@ -752,10 +756,10 @@ struct MetaDataEntry */ ssize_t GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData - *md, char **target, size_t max, - enum - GNUNET_CONTAINER_MetaDataSerializationOptions - opt) + *md, char **target, size_t max, + enum + GNUNET_CONTAINER_MetaDataSerializationOptions + opt) { struct GNUNET_CONTAINER_MetaData *vmd; struct MetaItem *pos; @@ -777,79 +781,79 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData int comp; if (max < sizeof (struct MetaDataHeader)) - return GNUNET_SYSERR; /* far too small */ + return GNUNET_SYSERR; /* far too small */ if (md == NULL) return 0; if (md->sbuf != NULL) - { - /* try to use serialization cache */ - if (md->sbuf_size <= max) { - if (NULL == *target) - *target = GNUNET_malloc (md->sbuf_size); - memcpy (*target, md->sbuf, md->sbuf_size); - return md->sbuf_size; + /* try to use serialization cache */ + if (md->sbuf_size <= max) + { + if (NULL == *target) + *target = GNUNET_malloc (md->sbuf_size); + memcpy (*target, md->sbuf, md->sbuf_size); + return md->sbuf_size; + } + if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART)) + return GNUNET_SYSERR; /* can say that this will fail */ + /* need to compute a partial serialization, sbuf useless ... */ } - if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART)) - return GNUNET_SYSERR; /* can say that this will fail */ - /* need to compute a partial serialization, sbuf useless ... */ - } dst = NULL; msize = 0; pos = md->items; while (NULL != pos) - { - msize += sizeof (struct MetaDataEntry); - msize += pos->data_size; - if (pos->plugin_name != NULL) - msize += strlen (pos->plugin_name) + 1; - if (pos->mime_type != NULL) - msize += strlen (pos->mime_type) + 1; - pos = pos->next; - } + { + msize += sizeof (struct MetaDataEntry); + msize += pos->data_size; + if (pos->plugin_name != NULL) + msize += strlen (pos->plugin_name) + 1; + if (pos->mime_type != NULL) + msize += strlen (pos->mime_type) + 1; + pos = pos->next; + } size = (size_t) msize; if (size != msize) - { - GNUNET_break (0); /* integer overflow */ - return GNUNET_SYSERR; - } + { + GNUNET_break (0); /* integer overflow */ + return GNUNET_SYSERR; + } if (size >= GNUNET_MAX_MALLOC_CHECKED) - { - /* too large to be processed */ - return GNUNET_SYSERR; - } + { + /* too large to be processed */ + return GNUNET_SYSERR; + } ent = GNUNET_malloc (size); mdata = (char *) &ent[md->item_count]; off = size - (md->item_count * sizeof (struct MetaDataEntry)); i = 0; pos = md->items; while (NULL != pos) - { - ent[i].type = htonl ((uint32_t) pos->type); - ent[i].format = htonl ((uint32_t) pos->format); - ent[i].data_size = htonl ((uint32_t) pos->data_size); - if (pos->plugin_name == NULL) - plen = 0; - else - plen = strlen (pos->plugin_name) + 1; - ent[i].plugin_name_len = htonl ((uint32_t) plen); - if (pos->mime_type == NULL) - mlen = 0; - else - mlen = strlen (pos->mime_type) + 1; - ent[i].mime_type_len = htonl ((uint32_t) mlen); - off -= pos->data_size; - memcpy (&mdata[off], pos->data, pos->data_size); - off -= plen; - if (pos->plugin_name != NULL) - memcpy (&mdata[off], pos->plugin_name, plen); - off -= mlen; - if (pos->mime_type != NULL) - memcpy (&mdata[off], pos->mime_type, mlen); - i++; - pos = pos->next; - } + { + ent[i].type = htonl ((uint32_t) pos->type); + ent[i].format = htonl ((uint32_t) pos->format); + ent[i].data_size = htonl ((uint32_t) pos->data_size); + if (pos->plugin_name == NULL) + plen = 0; + else + plen = strlen (pos->plugin_name) + 1; + ent[i].plugin_name_len = htonl ((uint32_t) plen); + if (pos->mime_type == NULL) + mlen = 0; + else + mlen = strlen (pos->mime_type) + 1; + ent[i].mime_type_len = htonl ((uint32_t) mlen); + off -= pos->data_size; + memcpy (&mdata[off], pos->data, pos->data_size); + off -= plen; + if (pos->plugin_name != NULL) + memcpy (&mdata[off], pos->plugin_name, plen); + off -= mlen; + if (pos->mime_type != NULL) + memcpy (&mdata[off], pos->mime_type, mlen); + i++; + pos = pos->next; + } GNUNET_assert (off == 0); clen = 0; @@ -858,95 +862,95 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData i = 0; pos = md->items; while (pos != NULL) - { - comp = GNUNET_NO; - if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS)) - comp = try_compression ((const char *) &ent[i], left, &cdata, &clen); - - if ((md->sbuf == NULL) && (i == 0)) - { - /* fill 'sbuf'; this "modifies" md, but since this is only - * an internal cache we will cast away the 'const' instead - * of making the API look strange. */ - vmd = (struct GNUNET_CONTAINER_MetaData *) md; - hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader)); - hdr->size = htonl (left); - hdr->entries = htonl (md->item_count); - if (GNUNET_YES == comp) - { - GNUNET_assert (clen < left); - hdr->version = htonl (2 | HEADER_COMPRESSED); - memcpy (&hdr[1], cdata, clen); - vmd->sbuf_size = clen + sizeof (struct MetaDataHeader); - } - else - { - hdr->version = htonl (2); - memcpy (&hdr[1], &ent[0], left); - vmd->sbuf_size = left + sizeof (struct MetaDataHeader); - } - vmd->sbuf = (char *) hdr; - } - - if (((left + sizeof (struct MetaDataHeader)) <= max) || - ((comp == GNUNET_YES) && (clen <= max))) - { - /* success, this now fits! */ - if (GNUNET_YES == comp) - { - if (dst == NULL) - dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader)); - hdr = (struct MetaDataHeader *) dst; - hdr->version = htonl (2 | HEADER_COMPRESSED); - hdr->size = htonl (left); - hdr->entries = htonl (md->item_count - i); - memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen); - GNUNET_free (cdata); - GNUNET_free (ent); - rlen = clen + sizeof (struct MetaDataHeader); - } - else - { - if (dst == NULL) - dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader)); - hdr = (struct MetaDataHeader *) dst; - hdr->version = htonl (2); - hdr->entries = htonl (md->item_count - i); - hdr->size = htonl (left); - memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left); - GNUNET_free (ent); - rlen = left + sizeof (struct MetaDataHeader); - } - if (NULL != *target) - { - memcpy (*target, dst, clen + sizeof (struct MetaDataHeader)); - GNUNET_free (dst); - } - else - { - *target = dst; - } - return rlen; - } - - if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART)) { - /* does not fit! */ - GNUNET_free (ent); - return GNUNET_SYSERR; + comp = GNUNET_NO; + if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS)) + comp = try_compression ((const char *) &ent[i], left, &cdata, &clen); + + if ((md->sbuf == NULL) && (i == 0)) + { + /* fill 'sbuf'; this "modifies" md, but since this is only + * an internal cache we will cast away the 'const' instead + * of making the API look strange. */ + vmd = (struct GNUNET_CONTAINER_MetaData *) md; + hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader)); + hdr->size = htonl (left); + hdr->entries = htonl (md->item_count); + if (GNUNET_YES == comp) + { + GNUNET_assert (clen < left); + hdr->version = htonl (2 | HEADER_COMPRESSED); + memcpy (&hdr[1], cdata, clen); + vmd->sbuf_size = clen + sizeof (struct MetaDataHeader); + } + else + { + hdr->version = htonl (2); + memcpy (&hdr[1], &ent[0], left); + vmd->sbuf_size = left + sizeof (struct MetaDataHeader); + } + vmd->sbuf = (char *) hdr; + } + + if (((left + sizeof (struct MetaDataHeader)) <= max) || + ((comp == GNUNET_YES) && (clen <= max))) + { + /* success, this now fits! */ + if (GNUNET_YES == comp) + { + if (dst == NULL) + dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader)); + hdr = (struct MetaDataHeader *) dst; + hdr->version = htonl (2 | HEADER_COMPRESSED); + hdr->size = htonl (left); + hdr->entries = htonl (md->item_count - i); + memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen); + GNUNET_free (cdata); + GNUNET_free (ent); + rlen = clen + sizeof (struct MetaDataHeader); + } + else + { + if (dst == NULL) + dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader)); + hdr = (struct MetaDataHeader *) dst; + hdr->version = htonl (2); + hdr->entries = htonl (md->item_count - i); + hdr->size = htonl (left); + memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left); + GNUNET_free (ent); + rlen = left + sizeof (struct MetaDataHeader); + } + if (NULL != *target) + { + memcpy (*target, dst, clen + sizeof (struct MetaDataHeader)); + GNUNET_free (dst); + } + else + { + *target = dst; + } + return rlen; + } + + if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART)) + { + /* does not fit! */ + GNUNET_free (ent); + return GNUNET_SYSERR; + } + + /* next iteration: ignore the corresponding meta data at the + * end and try again without it */ + left -= sizeof (struct MetaDataEntry); + left -= pos->data_size; + if (pos->plugin_name != NULL) + left -= strlen (pos->plugin_name) + 1; + if (pos->mime_type != NULL) + left -= strlen (pos->mime_type) + 1; + pos = pos->next; + i++; } - - /* next iteration: ignore the corresponding meta data at the - * end and try again without it */ - left -= sizeof (struct MetaDataEntry); - left -= pos->data_size; - if (pos->plugin_name != NULL) - left -= strlen (pos->plugin_name) + 1; - if (pos->mime_type != NULL) - left -= strlen (pos->mime_type) + 1; - pos = pos->next; - i++; - } GNUNET_free (ent); /* nothing fit, only write header! */ @@ -968,7 +972,7 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData */ ssize_t GNUNET_CONTAINER_meta_data_get_serialized_size (const struct - GNUNET_CONTAINER_MetaData *md) + GNUNET_CONTAINER_MetaData *md) { ssize_t ret; char *ptr; @@ -977,8 +981,8 @@ GNUNET_CONTAINER_meta_data_get_serialized_size (const struct return md->sbuf_size; ptr = NULL; ret = - GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED, - GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); + GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED, + GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); if (ret != -1) GNUNET_free (ptr); return ret; @@ -1005,14 +1009,14 @@ decompress (const char *input, size_t inputSize, size_t outputSize) output = GNUNET_malloc (olen); if (Z_OK == uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize)) - { - return output; - } + { + return output; + } else - { - GNUNET_free (output); - return NULL; - } + { + GNUNET_free (output); + return NULL; + } } @@ -1054,121 +1058,121 @@ GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size) compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0; if (version == 1) - return NULL; /* null pointer */ + return NULL; /* null pointer */ if (version != 2) - { - GNUNET_break_op (0); /* unsupported version */ - return NULL; - } + { + GNUNET_break_op (0); /* unsupported version */ + return NULL; + } ic = ntohl (hdr.entries); dataSize = ntohl (hdr.size); if ((sizeof (struct MetaDataEntry) * ic) > dataSize) - { - GNUNET_break_op (0); - return NULL; - } - - if (compressed) - { - if (dataSize >= GNUNET_MAX_MALLOC_CHECKED) { - /* make sure we don't blow our memory limit because of a mal-formed - * message... */ GNUNET_break_op (0); return NULL; } - data = - decompress ((const char *) &input[sizeof (struct MetaDataHeader)], - size - sizeof (struct MetaDataHeader), dataSize); - if (data == NULL) + + if (compressed) { - GNUNET_break_op (0); - return NULL; + if (dataSize >= GNUNET_MAX_MALLOC_CHECKED) + { + /* make sure we don't blow our memory limit because of a mal-formed + * message... */ + GNUNET_break_op (0); + return NULL; + } + data = + decompress ((const char *) &input[sizeof (struct MetaDataHeader)], + size - sizeof (struct MetaDataHeader), dataSize); + if (data == NULL) + { + GNUNET_break_op (0); + return NULL; + } + cdata = data; } - cdata = data; - } else - { - data = NULL; - cdata = (const char *) &input[sizeof (struct MetaDataHeader)]; - if (dataSize != size - sizeof (struct MetaDataHeader)) { - GNUNET_break_op (0); - return NULL; + data = NULL; + cdata = (const char *) &input[sizeof (struct MetaDataHeader)]; + if (dataSize != size - sizeof (struct MetaDataHeader)) + { + GNUNET_break_op (0); + return NULL; + } } - } md = GNUNET_CONTAINER_meta_data_create (); left = dataSize - ic * sizeof (struct MetaDataEntry); mdata = &cdata[ic * sizeof (struct MetaDataEntry)]; for (i = 0; i < ic; i++) - { - memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)], - sizeof (struct MetaDataEntry)); - format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format); - if ((format != EXTRACTOR_METAFORMAT_UTF8) && - (format != EXTRACTOR_METAFORMAT_C_STRING) && - (format != EXTRACTOR_METAFORMAT_BINARY)) - { - GNUNET_break_op (0); - break; - } - dlen = ntohl (ent.data_size); - plen = ntohl (ent.plugin_name_len); - mlen = ntohl (ent.mime_type_len); - if (dlen > left) - { - GNUNET_break_op (0); - break; - } - left -= dlen; - meta_data = &mdata[left]; - if ((format == EXTRACTOR_METAFORMAT_UTF8) || - (format == EXTRACTOR_METAFORMAT_C_STRING)) - { - if ((dlen == 0) || (mdata[left + dlen - 1] != '\0')) - { - GNUNET_break_op (0); - break; - } - } - if (plen > left) - { - GNUNET_break_op (0); - break; - } - left -= plen; - if ((plen > 0) && (mdata[left + plen - 1] != '\0')) { - GNUNET_break_op (0); - break; - } - if (plen == 0) - plugin_name = NULL; - else - plugin_name = &mdata[left]; - - if (mlen > left) - { - GNUNET_break_op (0); - break; - } - left -= mlen; - if ((mlen > 0) && (mdata[left + mlen - 1] != '\0')) - { - GNUNET_break_op (0); - break; + memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)], + sizeof (struct MetaDataEntry)); + format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format); + if ((format != EXTRACTOR_METAFORMAT_UTF8) && + (format != EXTRACTOR_METAFORMAT_C_STRING) && + (format != EXTRACTOR_METAFORMAT_BINARY)) + { + GNUNET_break_op (0); + break; + } + dlen = ntohl (ent.data_size); + plen = ntohl (ent.plugin_name_len); + mlen = ntohl (ent.mime_type_len); + if (dlen > left) + { + GNUNET_break_op (0); + break; + } + left -= dlen; + meta_data = &mdata[left]; + if ((format == EXTRACTOR_METAFORMAT_UTF8) || + (format == EXTRACTOR_METAFORMAT_C_STRING)) + { + if ((dlen == 0) || (mdata[left + dlen - 1] != '\0')) + { + GNUNET_break_op (0); + break; + } + } + if (plen > left) + { + GNUNET_break_op (0); + break; + } + left -= plen; + if ((plen > 0) && (mdata[left + plen - 1] != '\0')) + { + GNUNET_break_op (0); + break; + } + if (plen == 0) + plugin_name = NULL; + else + plugin_name = &mdata[left]; + + if (mlen > left) + { + GNUNET_break_op (0); + break; + } + left -= mlen; + if ((mlen > 0) && (mdata[left + mlen - 1] != '\0')) + { + GNUNET_break_op (0); + break; + } + if (mlen == 0) + mime_type = NULL; + else + mime_type = &mdata[left]; + GNUNET_CONTAINER_meta_data_insert (md, plugin_name, + (enum EXTRACTOR_MetaType) + ntohl (ent.type), format, mime_type, + meta_data, dlen); } - if (mlen == 0) - mime_type = NULL; - else - mime_type = &mdata[left]; - GNUNET_CONTAINER_meta_data_insert (md, plugin_name, - (enum EXTRACTOR_MetaType) - ntohl (ent.type), format, mime_type, - meta_data, dlen); - } GNUNET_free_non_null (data); return md; } diff --git a/src/util/container_multihashmap.c b/src/util/container_multihashmap.c index 974d6c7f7..60d4a43d5 100644 --- a/src/util/container_multihashmap.c +++ b/src/util/container_multihashmap.c @@ -28,6 +28,8 @@ #include "gnunet_container_lib.h" #include "gnunet_crypto_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * An entry in the hash map. */ @@ -101,19 +103,19 @@ GNUNET_CONTAINER_multihashmap_create (unsigned int len) */ void GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap - *map) + *map) { unsigned int i; struct MapEntry *e; for (i = 0; i < map->map_length; i++) - { - while (NULL != (e = map->map[i])) { - map->map[i] = e->next; - GNUNET_free (e); + while (NULL != (e = map->map[i])) + { + map->map[i] = e->next; + GNUNET_free (e); + } } - } GNUNET_free (map->map); GNUNET_free (map); } @@ -128,7 +130,7 @@ GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap */ static unsigned int idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m, - const GNUNET_HashCode * key) + const GNUNET_HashCode * key) { GNUNET_assert (m != NULL); return (*(unsigned int *) key) % m->map_length; @@ -143,7 +145,7 @@ idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m, */ unsigned int GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap - *map) + *map) { return map->size; } @@ -161,17 +163,17 @@ GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap */ void * GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap - *map, const GNUNET_HashCode * key) + *map, const GNUNET_HashCode * key) { struct MapEntry *e; e = map->map[idx_of (map, key)]; while (e != NULL) - { - if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) - return e->value; - e = e->next; - } + { + if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) + return e->value; + e = e->next; + } return NULL; } @@ -187,9 +189,9 @@ GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap */ int GNUNET_CONTAINER_multihashmap_iterate (const struct - GNUNET_CONTAINER_MultiHashMap *map, - GNUNET_CONTAINER_HashMapIterator it, - void *it_cls) + GNUNET_CONTAINER_MultiHashMap *map, + GNUNET_CONTAINER_HashMapIterator it, + void *it_cls) { int count; unsigned int i; @@ -200,20 +202,20 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct count = 0; GNUNET_assert (map != NULL); for (i = 0; i < map->map_length; i++) - { - n = map->map[i]; - while (NULL != (e = n)) { - n = e->next; - if (NULL != it) - { - kc = e->key; - if (GNUNET_OK != it (it_cls, &kc, e->value)) - return GNUNET_SYSERR; - } - count++; + n = map->map[i]; + while (NULL != (e = n)) + { + n = e->next; + if (NULL != it) + { + kc = e->key; + if (GNUNET_OK != it (it_cls, &kc, e->value)) + return GNUNET_SYSERR; + } + count++; + } } - } return count; } @@ -230,8 +232,9 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct * is not in the map */ int -GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, - const GNUNET_HashCode * key, void *value) +GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap + *map, const GNUNET_HashCode * key, + void *value) { struct MapEntry *e; struct MapEntry *p; @@ -241,21 +244,21 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, p = NULL; e = map->map[i]; while (e != NULL) - { - if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && - (value == e->value)) { - if (p == NULL) - map->map[i] = e->next; - else - p->next = e->next; - GNUNET_free (e); - map->size--; - return GNUNET_YES; + if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && + (value == e->value)) + { + if (p == NULL) + map->map[i] = e->next; + else + p->next = e->next; + GNUNET_free (e); + map->size--; + return GNUNET_YES; + } + p = e; + e = e->next; } - p = e; - e = e->next; - } return GNUNET_NO; } @@ -270,7 +273,7 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, */ int GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap - *map, const GNUNET_HashCode * key) + *map, const GNUNET_HashCode * key) { struct MapEntry *e; struct MapEntry *p; @@ -282,27 +285,27 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap p = NULL; e = map->map[i]; while (e != NULL) - { - if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) { - if (p == NULL) - map->map[i] = e->next; - else - p->next = e->next; - GNUNET_free (e); - map->size--; - if (p == NULL) - e = map->map[i]; + if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) + { + if (p == NULL) + map->map[i] = e->next; + else + p->next = e->next; + GNUNET_free (e); + map->size--; + if (p == NULL) + e = map->map[i]; + else + e = p->next; + ret++; + } else - e = p->next; - ret++; - } - else - { - p = e; - e = e->next; + { + p = e; + e = e->next; + } } - } return ret; } @@ -318,18 +321,18 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap */ int GNUNET_CONTAINER_multihashmap_contains (const struct - GNUNET_CONTAINER_MultiHashMap *map, - const GNUNET_HashCode * key) + GNUNET_CONTAINER_MultiHashMap *map, + const GNUNET_HashCode * key) { struct MapEntry *e; e = map->map[idx_of (map, key)]; while (e != NULL) - { - if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) - return GNUNET_YES; - e = e->next; - } + { + if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) + return GNUNET_YES; + e = e->next; + } return GNUNET_NO; } @@ -346,20 +349,21 @@ GNUNET_CONTAINER_multihashmap_contains (const struct */ int GNUNET_CONTAINER_multihashmap_contains_value (const struct - GNUNET_CONTAINER_MultiHashMap - *map, const GNUNET_HashCode * key, - const void *value) + GNUNET_CONTAINER_MultiHashMap + *map, + const GNUNET_HashCode * key, + const void *value) { struct MapEntry *e; e = map->map[idx_of (map, key)]; while (e != NULL) - { - if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && - (e->value == value)) - return GNUNET_YES; - e = e->next; - } + { + if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && + (e->value == value)) + return GNUNET_YES; + e = e->next; + } return GNUNET_NO; } @@ -387,15 +391,15 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map) map->map_length = new_len; map->map = new_map; for (i = 0; i < old_len; i++) - { - while (NULL != (e = old_map[i])) { - old_map[i] = e->next; - idx = idx_of (map, &e->key); - e->next = new_map[idx]; - new_map[idx] = e; + while (NULL != (e = old_map[i])) + { + old_map[i] = e->next; + idx = idx_of (map, &e->key); + e->next = new_map[idx]; + new_map[idx] = e; + } } - } GNUNET_free (old_map); } @@ -414,8 +418,9 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map) */ int GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, - const GNUNET_HashCode * key, void *value, - enum GNUNET_CONTAINER_MultiHashMapOption opt) + const GNUNET_HashCode * key, void *value, + enum GNUNET_CONTAINER_MultiHashMapOption + opt) { struct MapEntry *e; unsigned int i; @@ -423,25 +428,25 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, i = idx_of (map, key); if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) - { - e = map->map[i]; - while (e != NULL) { - if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) - { - if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) - return GNUNET_SYSERR; - e->value = value; - return GNUNET_NO; - } - e = e->next; + e = map->map[i]; + while (e != NULL) + { + if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) + { + if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) + return GNUNET_SYSERR; + e->value = value; + return GNUNET_NO; + } + e = e->next; + } } - } if (map->size / 3 >= map->map_length / 4) - { - grow (map); - i = idx_of (map, key); - } + { + grow (map); + i = idx_of (map, key); + } e = GNUNET_malloc (sizeof (struct MapEntry)); e->key = *key; e->value = value; @@ -464,10 +469,10 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, */ int GNUNET_CONTAINER_multihashmap_get_multiple (const struct - GNUNET_CONTAINER_MultiHashMap *map, - const GNUNET_HashCode * key, - GNUNET_CONTAINER_HashMapIterator it, - void *it_cls) + GNUNET_CONTAINER_MultiHashMap + *map, const GNUNET_HashCode * key, + GNUNET_CONTAINER_HashMapIterator + it, void *it_cls) { int count; struct MapEntry *e; @@ -476,14 +481,14 @@ GNUNET_CONTAINER_multihashmap_get_multiple (const struct count = 0; n = map->map[idx_of (map, key)]; while (NULL != (e = n)) - { - n = e->next; - if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode))) - continue; - if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value))) - return GNUNET_SYSERR; - count++; - } + { + n = e->next; + if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode))) + continue; + if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value))) + return GNUNET_SYSERR; + count++; + } return count; } diff --git a/src/util/container_slist.c b/src/util/container_slist.c index 1dd0344e4..144d352ce 100644 --- a/src/util/container_slist.c +++ b/src/util/container_slist.c @@ -27,6 +27,8 @@ #include "platform.h" #include "gnunet_container_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Element in our linked list. */ @@ -108,21 +110,21 @@ struct GNUNET_CONTAINER_SList_Iterator */ static struct GNUNET_CONTAINER_SList_Elem * create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf, - size_t len) + size_t len) { struct GNUNET_CONTAINER_SList_Elem *e; if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT) - { - e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len); - memcpy (&e[1], buf, len); - e->elem = (void *) &e[1]; - } + { + e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len); + memcpy (&e[1], buf, len); + e->elem = (void *) &e[1]; + } else - { - e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem)); - e->elem = (void *) buf; - } + { + e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem)); + e->elem = (void *) buf; + } e->disp = disp; e->len = len; return e; @@ -138,8 +140,8 @@ create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf, */ void GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, - enum GNUNET_CONTAINER_SListDisposition disp, - const void *buf, size_t len) + enum GNUNET_CONTAINER_SListDisposition disp, + const void *buf, size_t len) { struct GNUNET_CONTAINER_SList_Elem *e; @@ -160,8 +162,8 @@ GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, */ void GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l, - enum GNUNET_CONTAINER_SListDisposition disp, - const void *buf, size_t len) + enum GNUNET_CONTAINER_SListDisposition disp, + const void *buf, size_t len) { struct GNUNET_CONTAINER_SList_Elem *e; @@ -182,7 +184,7 @@ GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l, */ void GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, - struct GNUNET_CONTAINER_SList *src) + struct GNUNET_CONTAINER_SList *src) { struct GNUNET_CONTAINER_SList_Iterator *i; @@ -190,14 +192,15 @@ GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, GNUNET_CONTAINER_slist_end (i) != GNUNET_YES; GNUNET_CONTAINER_slist_next (i)) - { - GNUNET_CONTAINER_slist_add (dst, - (i->elem->disp == - GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ? - GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC : - GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - i->elem->elem, i->elem->len); - } + { + GNUNET_CONTAINER_slist_add (dst, + (i->elem->disp == + GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) + ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC + : + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + i->elem->elem, i->elem->len); + } GNUNET_CONTAINER_slist_iter_destroy (i); } @@ -254,13 +257,13 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l) e = l->head; while (e != NULL) - { - n = e->next; - if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC) - GNUNET_free (e->elem); - GNUNET_free (e); - e = n; - } + { + n = e->next; + if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC) + GNUNET_free (e->elem); + GNUNET_free (e); + e = n; + } l->head = NULL; l->tail = NULL; l->length = 0; @@ -276,7 +279,7 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l) */ int GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, - const void *buf, size_t len) + const void *buf, size_t len) { struct GNUNET_CONTAINER_SList_Elem *e; @@ -333,8 +336,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i) */ void GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before, - enum GNUNET_CONTAINER_SListDisposition disp, - const void *buf, size_t len) + enum GNUNET_CONTAINER_SListDisposition disp, + const void *buf, size_t len) { struct GNUNET_CONTAINER_SList_Elem *e; @@ -387,7 +390,7 @@ GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i) */ void * GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, - size_t * len) + size_t * len) { if (len) *len = i->elem->len; @@ -399,7 +402,8 @@ GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, * @param i iterator */ void -GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i) +GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator + *i) { GNUNET_free (i); } diff --git a/src/util/crypto_aes.c b/src/util/crypto_aes.c index a984e17fc..9e194094c 100644 --- a/src/util/crypto_aes.c +++ b/src/util/crypto_aes.c @@ -30,6 +30,8 @@ #include "gnunet_crypto_lib.h" #include +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Create a new SessionKey (for AES-256). */ @@ -37,9 +39,9 @@ void GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key) { gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH, - GCRY_STRONG_RANDOM); + GCRY_STRONG_RANDOM); key->crc32 = - htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH)); + htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH)); } /** @@ -49,7 +51,7 @@ GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key) */ int GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey - *key) + *key) { uint32_t crc; @@ -74,28 +76,29 @@ GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey */ ssize_t GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len, - const struct GNUNET_CRYPTO_AesSessionKey * - sessionkey, - const struct GNUNET_CRYPTO_AesInitializationVector * - iv, void *result) + const struct GNUNET_CRYPTO_AesSessionKey * + sessionkey, + const struct GNUNET_CRYPTO_AesInitializationVector + * iv, void *result) { gcry_cipher_hd_t handle; int rc; if (sessionkey->crc32 != - htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) - { - GNUNET_break (0); - return -1; - } + htonl (GNUNET_CRYPTO_crc32_n + (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) + { + GNUNET_break (0); + return -1; + } GNUNET_assert (0 == - gcry_cipher_open (&handle, GCRY_CIPHER_AES256, - GCRY_CIPHER_MODE_CFB, 0)); + gcry_cipher_open (&handle, GCRY_CIPHER_AES256, + GCRY_CIPHER_MODE_CFB, 0)); rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); rc = gcry_cipher_setiv (handle, iv, - sizeof (struct - GNUNET_CRYPTO_AesInitializationVector)); + sizeof (struct + GNUNET_CRYPTO_AesInitializationVector)); GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len)); gcry_cipher_close (handle); @@ -115,30 +118,32 @@ GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len, */ ssize_t GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size, - const struct GNUNET_CRYPTO_AesSessionKey * - sessionkey, - const struct GNUNET_CRYPTO_AesInitializationVector * - iv, void *result) + const struct GNUNET_CRYPTO_AesSessionKey * + sessionkey, + const struct GNUNET_CRYPTO_AesInitializationVector + * iv, void *result) { gcry_cipher_hd_t handle; int rc; if (sessionkey->crc32 != - htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) - { - GNUNET_break (0); - return -1; - } + htonl (GNUNET_CRYPTO_crc32_n + (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) + { + GNUNET_break (0); + return -1; + } GNUNET_assert (0 == - gcry_cipher_open (&handle, GCRY_CIPHER_AES256, - GCRY_CIPHER_MODE_CFB, 0)); + gcry_cipher_open (&handle, GCRY_CIPHER_AES256, + GCRY_CIPHER_MODE_CFB, 0)); rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); rc = gcry_cipher_setiv (handle, iv, - sizeof (struct - GNUNET_CRYPTO_AesInitializationVector)); + sizeof (struct + GNUNET_CRYPTO_AesInitializationVector)); GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); - GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size)); + GNUNET_assert (0 == + gcry_cipher_decrypt (handle, result, size, block, size)); gcry_cipher_close (handle); return size; } @@ -153,8 +158,8 @@ GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size, */ void GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv, - const struct GNUNET_CRYPTO_AesSessionKey *skey, - const void *salt, size_t salt_len, ...) + const struct GNUNET_CRYPTO_AesSessionKey *skey, + const void *salt, size_t salt_len, ...) { va_list argp; @@ -172,12 +177,14 @@ GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv, * @param argp pairs of void * & size_t for context chunks, terminated by NULL */ void -GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv, - const struct GNUNET_CRYPTO_AesSessionKey *skey, - const void *salt, size_t salt_len, va_list argp) +GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector + *iv, + const struct GNUNET_CRYPTO_AesSessionKey *skey, + const void *salt, size_t salt_len, + va_list argp) { GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key, - sizeof (skey->key), argp); + sizeof (skey->key), argp); } /* end of crypto_aes.c */ diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c index 543bc4c65..5920ddc93 100644 --- a/src/util/crypto_crc.c +++ b/src/util/crypto_crc.c @@ -32,6 +32,8 @@ #include "gnunet_common.h" #include "gnunet_crypto_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /* Avoid wasting space on 8-byte longs. */ #if UINT_MAX >= 0xffffffff typedef unsigned int uLong; @@ -64,12 +66,12 @@ crc_init () once = 1; crc_table[0] = 0; for (i = 128; i; i >>= 1) - { - h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0); - /* h is now crc_table[i] */ - for (j = 0; j < 256; j += 2 * i) - crc_table[i + j] = crc_table[j] ^ h; - } + { + h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0); + /* h is now crc_table[i] */ + for (j = 0; j < 256; j += 2 * i) + crc_table[i + j] = crc_table[j] ^ h; + } } /* diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index c253bbf2f..b8c9a8082 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c @@ -36,6 +36,9 @@ #include "gnunet_disk_lib.h" #include +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) /** * Hash block of given size. @@ -116,14 +119,14 @@ struct GNUNET_CRYPTO_FileHashContext */ static void file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc, - const GNUNET_HashCode * res) + const GNUNET_HashCode * res) { fhc->callback (fhc->callback_cls, res); GNUNET_free (fhc->filename); if (!GNUNET_DISK_handle_invalid (fhc->fh)) GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh)); gcry_md_close (fhc->md); - GNUNET_free (fhc); /* also frees fhc->buffer */ + GNUNET_free (fhc); /* also frees fhc->buffer */ } @@ -146,19 +149,19 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) if (fhc->fsize - fhc->offset < delta) delta = fhc->fsize - fhc->offset; if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename); - file_hash_finish (fhc, NULL); - return; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename); + file_hash_finish (fhc, NULL); + return; + } gcry_md_write (fhc->md, fhc->buffer, delta); fhc->offset += delta; if (fhc->offset == fhc->fsize) - { - res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512); - file_hash_finish (fhc, res); - return; - } + { + res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512); + file_hash_finish (fhc, res); + return; + } fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc); } @@ -175,43 +178,43 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ struct GNUNET_CRYPTO_FileHashContext * GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority, - const char *filename, size_t blocksize, - GNUNET_CRYPTO_HashCompletedCallback callback, - void *callback_cls) + const char *filename, size_t blocksize, + GNUNET_CRYPTO_HashCompletedCallback callback, + void *callback_cls) { struct GNUNET_CRYPTO_FileHashContext *fhc; GNUNET_assert (blocksize > 0); fhc = - GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize); + GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize); fhc->callback = callback; fhc->callback_cls = callback_cls; fhc->buffer = (unsigned char *) &fhc[1]; fhc->filename = GNUNET_strdup (filename); if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0)) - { - GNUNET_break (0); - GNUNET_free (fhc); - return NULL; - } + { + GNUNET_break (0); + GNUNET_free (fhc); + return NULL; + } fhc->bsize = blocksize; if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO)) - { - GNUNET_free (fhc->filename); - GNUNET_free (fhc); - return NULL; - } + { + GNUNET_free (fhc->filename); + GNUNET_free (fhc); + return NULL; + } fhc->fh = - GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, - GNUNET_DISK_PERM_NONE); + GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, + GNUNET_DISK_PERM_NONE); if (!fhc->fh) - { - GNUNET_free (fhc->filename); - GNUNET_free (fhc); - return NULL; - } + { + GNUNET_free (fhc->filename); + GNUNET_free (fhc); + return NULL; + } fhc->task = - GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc); + GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc); return fhc; } @@ -257,7 +260,7 @@ getValue__ (unsigned char a) */ void GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block, - struct GNUNET_CRYPTO_HashAsciiEncoded *result) + struct GNUNET_CRYPTO_HashAsciiEncoded *result) { /** * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters) @@ -275,22 +278,23 @@ GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block, rpos = 0; bits = 0; while ((rpos < sizeof (GNUNET_HashCode)) || (vbit > 0)) - { - if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5)) - { - bits = (bits << 8) | ((unsigned char *) block)[rpos++]; /* eat 8 more bits */ - vbit += 8; - } - if (vbit < 5) { - bits <<= (5 - vbit); /* zero-padding */ - GNUNET_assert (vbit == 2); /* padding by 3: 512+3 mod 5 == 0 */ - vbit = 5; + if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5)) + { + bits = (bits << 8) | ((unsigned char *) block)[rpos++]; /* eat 8 more bits */ + vbit += 8; + } + if (vbit < 5) + { + bits <<= (5 - vbit); /* zero-padding */ + GNUNET_assert (vbit == 2); /* padding by 3: 512+3 mod 5 == 0 */ + vbit = 5; + } + GNUNET_assert (wpos < + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1); + result->encoding[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; + vbit -= 5; } - GNUNET_assert (wpos < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1); - result->encoding[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; - vbit -= 5; - } GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1); GNUNET_assert (vbit == 0); result->encoding[wpos] = '\0'; @@ -314,22 +318,22 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result) if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1) return GNUNET_SYSERR; - vbit = 2; /* padding! */ + vbit = 2; /* padding! */ wpos = sizeof (GNUNET_HashCode); rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1; bits = getValue__ (enc[--rpos]) >> 3; while (wpos > 0) - { - GNUNET_assert (rpos > 0); - bits = (getValue__ (enc[--rpos]) << vbit) | bits; - vbit += 5; - if (vbit >= 8) { - ((unsigned char *) result)[--wpos] = (unsigned char) bits; - bits >>= 8; - vbit -= 8; + GNUNET_assert (rpos > 0); + bits = (getValue__ (enc[--rpos]) << vbit) | bits; + vbit += 5; + if (vbit >= 8) + { + ((unsigned char *) result)[--wpos] = (unsigned char) bits; + bits >>= 8; + vbit -= 8; + } } - } GNUNET_assert (rpos == 0); GNUNET_assert (vbit == 0); return GNUNET_OK; @@ -348,7 +352,7 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result) */ unsigned int GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, - const GNUNET_HashCode * b) + const GNUNET_HashCode * b) { unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16; unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16; @@ -358,7 +362,7 @@ GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, void GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, - GNUNET_HashCode * result) + GNUNET_HashCode * result) { int i; @@ -368,33 +372,37 @@ GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, void GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, - const GNUNET_HashCode * b, - GNUNET_HashCode * result) + const GNUNET_HashCode * b, + GNUNET_HashCode * result) { int i; - for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) + for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; + i--) result->bits[i] = b->bits[i] - a->bits[i]; } void GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, - const GNUNET_HashCode * delta, GNUNET_HashCode * result) + const GNUNET_HashCode * delta, + GNUNET_HashCode * result) { int i; - for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) + for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; + i--) result->bits[i] = delta->bits[i] + a->bits[i]; } void GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, - GNUNET_HashCode * result) + GNUNET_HashCode * result) { int i; - for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) + for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; + i--) result->bits[i] = a->bits[i] ^ b->bits[i]; } @@ -404,17 +412,18 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, */ void GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, - struct GNUNET_CRYPTO_AesSessionKey *skey, - struct GNUNET_CRYPTO_AesInitializationVector *iv) + struct GNUNET_CRYPTO_AesSessionKey *skey, + struct GNUNET_CRYPTO_AesInitializationVector + *iv) { GNUNET_assert (sizeof (GNUNET_HashCode) >= - GNUNET_CRYPTO_AES_KEY_LENGTH + - sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); + GNUNET_CRYPTO_AES_KEY_LENGTH + + sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH); skey->crc32 = - htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH)); + htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH)); memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH], - sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); + sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); } @@ -445,13 +454,13 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit) */ unsigned int GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first, - const GNUNET_HashCode * second) + const GNUNET_HashCode * second) { unsigned int i; for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++) if (GNUNET_CRYPTO_hash_get_bit (first, i) != - GNUNET_CRYPTO_hash_get_bit (second, i)) + GNUNET_CRYPTO_hash_get_bit (second, i)) return i; return sizeof (GNUNET_HashCode) * 8; } @@ -463,7 +472,8 @@ GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first, * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. */ int -GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2) +GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, + const GNUNET_HashCode * h2) { unsigned int *i1; unsigned int *i2; @@ -471,13 +481,14 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2) i1 = (unsigned int *) h1; i2 = (unsigned int *) h2; - for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) - { - if (i1[i] > i2[i]) - return 1; - if (i1[i] < i2[i]) - return -1; - } + for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; + i--) + { + if (i1[i] > i2[i]) + return 1; + if (i1[i] < i2[i]) + return -1; + } return 0; } @@ -489,22 +500,22 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2) */ int GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, - const GNUNET_HashCode * h2, - const GNUNET_HashCode * target) + const GNUNET_HashCode * h2, + const GNUNET_HashCode * target) { int i; unsigned int d1; unsigned int d2; for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--) - { - d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; - d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; - if (d1 > d2) - return 1; - else if (d1 < d2) - return -1; - } + { + d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; + d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; + if (d1 > d2) + return 1; + else if (d1 < d2) + return -1; + } return 0; } @@ -519,8 +530,8 @@ GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, */ void GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key, - const struct GNUNET_CRYPTO_AesSessionKey *rkey, - const void *salt, size_t salt_len, ...) + const struct GNUNET_CRYPTO_AesSessionKey *rkey, + const void *salt, size_t salt_len, ...) { va_list argp; @@ -540,12 +551,12 @@ GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key, */ void GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, - const struct GNUNET_CRYPTO_AesSessionKey *rkey, - const void *salt, size_t salt_len, - va_list argp) + const struct GNUNET_CRYPTO_AesSessionKey + *rkey, const void *salt, size_t salt_len, + va_list argp) { GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key, - sizeof (rkey->key), argp); + sizeof (rkey->key), argp); } @@ -559,14 +570,14 @@ GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, */ void GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key, - const void *plaintext, size_t plaintext_len, - GNUNET_HashCode * hmac) + const void *plaintext, size_t plaintext_len, + GNUNET_HashCode * hmac) { gcry_md_hd_t md; const unsigned char *mc; GNUNET_assert (GPG_ERR_NO_ERROR == - gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC)); + gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC)); gcry_md_setkey (md, key->key, sizeof (key->key)); gcry_md_write (md, plaintext, plaintext_len); mc = gcry_md_read (md, GCRY_MD_SHA512); diff --git a/src/util/crypto_hkdf.c b/src/util/crypto_hkdf.c index 0654b51f6..6af76ba68 100644 --- a/src/util/crypto_hkdf.c +++ b/src/util/crypto_hkdf.c @@ -36,6 +36,8 @@ * - Matthias Wachs (08.10.2010) */ +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Set this to 0 if you compile this code outside of GNUnet. */ @@ -72,7 +74,7 @@ */ static const void * doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf, - size_t buf_len) + size_t buf_len) { gcry_md_setkey (mac, key, key_len); gcry_md_write (mac, buf, buf_len); @@ -92,7 +94,7 @@ doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf, */ static int getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm, - size_t skm_len, void *prk) + size_t skm_len, void *prk) { const void *ret; @@ -113,9 +115,9 @@ dump (const char *src, const void *p, unsigned int l) printf ("\n%s: ", src); for (i = 0; i < l; i++) - { - printf ("%2x", (int) ((const unsigned char *) p)[i]); - } + { + printf ("%2x", (int) ((const unsigned char *) p)[i]); + } printf ("\n"); } #endif @@ -135,9 +137,9 @@ dump (const char *src, const void *p, unsigned int l) * @return GNUNET_YES on success */ int -GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo, - const void *xts, size_t xts_len, const void *skm, - size_t skm_len, va_list argp) +GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, + int prf_algo, const void *xts, size_t xts_len, + const void *skm, size_t skm_len, va_list argp) { const void *hc; unsigned long i, t, d; @@ -156,16 +158,16 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo, return GNUNET_SYSERR; if (gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR) - { - gcry_md_close (xtr); - return GNUNET_SYSERR; - } + { + gcry_md_close (xtr); + return GNUNET_SYSERR; + } va_copy (args, argp); ctx_len = 0; while (NULL != va_arg (args, void *)) - ctx_len += va_arg (args, size_t); + ctx_len += va_arg (args, size_t); va_end (args); @@ -189,65 +191,65 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo, dst = plain + k; va_copy (args, argp); while ((ctx = va_arg (args, void *))) - { - size_t len; + { + size_t len; - len = va_arg (args, size_t); - memcpy (dst, ctx, len); - dst += len; - } + len = va_arg (args, size_t); + memcpy (dst, ctx, len); + dst += len; + } va_end (args); if (t > 0) - { - memset (plain + k + ctx_len, 1, 1); + { + memset (plain + k + ctx_len, 1, 1); #if DEBUG_HKDF - dump ("K(1)", plain, plain_len); + dump ("K(1)", plain, plain_len); #endif - hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1); - if (hc == NULL) - goto hkdf_error; - memcpy (result, hc, k); - result += k; - } + hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1); + if (hc == NULL) + goto hkdf_error; + memcpy (result, hc, k); + result += k; + } /* K(i+1) */ for (i = 1; i < t; i++) - { - memcpy (plain, result - k, k); - memset (plain + k + ctx_len, i + 1, 1); - gcry_md_reset (prf); + { + memcpy (plain, result - k, k); + memset (plain + k + ctx_len, i + 1, 1); + gcry_md_reset (prf); #if DEBUG_HKDF - dump ("K(i+1)", plain, plain_len); + dump ("K(i+1)", plain, plain_len); #endif - hc = doHMAC (prf, prk, xtr_len, plain, plain_len); - if (hc == NULL) - goto hkdf_error; - memcpy (result, hc, k); - result += k; - } + hc = doHMAC (prf, prk, xtr_len, plain, plain_len); + if (hc == NULL) + goto hkdf_error; + memcpy (result, hc, k); + result += k; + } /* K(t):d */ if (d > 0) - { - if (t > 0) { - memcpy (plain, result - k, k); - i++; - } - memset (plain + k + ctx_len, i, 1); - gcry_md_reset (prf); + if (t > 0) + { + memcpy (plain, result - k, k); + i++; + } + memset (plain + k + ctx_len, i, 1); + gcry_md_reset (prf); #if DEBUG_HKDF - dump ("K(t):d", plain, plain_len); + dump ("K(t):d", plain, plain_len); #endif - if (t > 0) - hc = doHMAC (prf, prk, xtr_len, plain, plain_len); - else - hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k); - if (hc == NULL) - goto hkdf_error; - memcpy (result, hc, d); - } + if (t > 0) + hc = doHMAC (prf, prk, xtr_len, plain, plain_len); + else + hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k); + if (hc == NULL) + goto hkdf_error; + memcpy (result, hc, d); + } #if DEBUG_HKDF dump ("result", result - k, out_len); #endif @@ -279,16 +281,16 @@ hkdf_ok: */ int GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo, - const void *xts, size_t xts_len, const void *skm, - size_t skm_len, ...) + const void *xts, size_t xts_len, const void *skm, + size_t skm_len, ...) { va_list argp; int ret; va_start (argp, skm_len); ret = - GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len, - skm, skm_len, argp); + GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len, + skm, skm_len, argp); va_end (argp); return ret; diff --git a/src/util/crypto_kdf.c b/src/util/crypto_kdf.c index eff4e6fd6..d8fcbd866 100644 --- a/src/util/crypto_kdf.c +++ b/src/util/crypto_kdf.c @@ -29,6 +29,8 @@ #include "platform.h" #include "gnunet_crypto_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * @brief Derive key * @param result buffer for the derived key, allocated by caller @@ -42,8 +44,8 @@ */ int GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts, - size_t xts_len, const void *skm, size_t skm_len, - va_list argp) + size_t xts_len, const void *skm, size_t skm_len, + va_list argp) { /* * "Finally, we point out to a particularly advantageous instantiation using @@ -57,8 +59,9 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts, * http://eprint.iacr.org/2010/264 */ - return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512, GCRY_MD_SHA256, - xts, xts_len, skm, skm_len, argp); + return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512, + GCRY_MD_SHA256, xts, xts_len, skm, skm_len, + argp); } /** @@ -74,13 +77,14 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts, */ int GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts, - size_t xts_len, const void *skm, size_t skm_len, ...) + size_t xts_len, const void *skm, size_t skm_len, ...) { va_list argp; int ret; va_start (argp, skm_len); - ret = GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp); + ret = + GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp); va_end (argp); return ret; diff --git a/src/util/crypto_ksk.c b/src/util/crypto_ksk.c index bec87c803..3d40ce27b 100644 --- a/src/util/crypto_ksk.c +++ b/src/util/crypto_ksk.c @@ -39,22 +39,24 @@ #include #include +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Log an error message at log-level 'level' that indicates * a failure of the command 'cmd' with the message given * by gcry_strerror(rc). */ -#define LOG_GCRY(level, cmd, rc) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0); +#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); typedef struct { - gcry_mpi_t n; /* public modulus */ - gcry_mpi_t e; /* public exponent */ - gcry_mpi_t d; /* exponent */ - gcry_mpi_t p; /* prime p. */ - gcry_mpi_t q; /* prime q. */ - gcry_mpi_t u; /* inverse of p mod q. */ + gcry_mpi_t n; /* public modulus */ + gcry_mpi_t e; /* public exponent */ + gcry_mpi_t d; /* exponent */ + gcry_mpi_t p; /* prime p. */ + gcry_mpi_t q; /* prime q. */ + gcry_mpi_t u; /* inverse of p mod q. */ } KBlock_secret_key; /** @@ -82,23 +84,23 @@ mpz_randomize (gcry_mpi_t n, unsigned int nbits, GNUNET_HashCode * rnd) tmp = *rnd; for (i = 0; i < cnt; i++) - { - int j; - - if (i > 0) - GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), &tmp); - for (j = 0; j < sizeof (GNUNET_HashCode) / sizeof (uint32_t); j++) { + int j; + + if (i > 0) + GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), &tmp); + for (j = 0; j < sizeof (GNUNET_HashCode) / sizeof (uint32_t); j++) + { #if HAVE_GCRY_MPI_LSHIFT - gcry_mpi_lshift (n, n, sizeof (uint32_t) * 8); + gcry_mpi_lshift (n, n, sizeof (uint32_t) * 8); #else - gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); - gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); + gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); + gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); #endif - gcry_mpi_add_ui (n, n, ntohl (((uint32_t *) & tmp)[j])); + gcry_mpi_add_ui (n, n, ntohl (((uint32_t *) & tmp)[j])); + } + hc = tmp; } - hc = tmp; - } GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), rnd); i = gcry_mpi_get_nbits (n); while (i > nbits) @@ -112,10 +114,10 @@ mpz_trailing_zeroes (gcry_mpi_t n) cnt = gcry_mpi_get_nbits (n); for (idx = 0; idx < cnt; idx++) - { - if (gcry_mpi_test_bit (n, idx) == 0) - return idx; - } + { + if (gcry_mpi_test_bit (n, idx) == 0) + return idx; + } return ULONG_MAX; } @@ -162,31 +164,31 @@ is_prime (gcry_mpi_t n, int steps, GNUNET_HashCode * hc) mpz_tdiv_q_2exp (q, q, k); for (i = 0; i < steps; i++) - { - if (!i) - { - gcry_mpi_set_ui (x, 2); - } - else { - mpz_randomize (x, nbits - 1, hc); - GNUNET_assert (gcry_mpi_cmp (x, nminus1) < 0); - GNUNET_assert (gcry_mpi_cmp_ui (x, 1) > 0); + if (!i) + { + gcry_mpi_set_ui (x, 2); + } + else + { + mpz_randomize (x, nbits - 1, hc); + GNUNET_assert (gcry_mpi_cmp (x, nminus1) < 0); + GNUNET_assert (gcry_mpi_cmp_ui (x, 1) > 0); + } + gcry_mpi_powm (y, x, q, n); + if (gcry_mpi_cmp_ui (y, 1) && gcry_mpi_cmp (y, nminus1)) + { + for (j = 1; j < k && gcry_mpi_cmp (y, nminus1); j++) + { + gcry_mpi_powm (y, y, a2, n); + if (!gcry_mpi_cmp_ui (y, 1)) + goto leave; /* Not a prime. */ + } + if (gcry_mpi_cmp (y, nminus1)) + goto leave; /* Not a prime. */ + } } - gcry_mpi_powm (y, x, q, n); - if (gcry_mpi_cmp_ui (y, 1) && gcry_mpi_cmp (y, nminus1)) - { - for (j = 1; j < k && gcry_mpi_cmp (y, nminus1); j++) - { - gcry_mpi_powm (y, y, a2, n); - if (!gcry_mpi_cmp_ui (y, 1)) - goto leave; /* Not a prime. */ - } - if (gcry_mpi_cmp (y, nminus1)) - goto leave; /* Not a prime. */ - } - } - rc = 1; /* May be a prime. */ + rc = 1; /* May be a prime. */ leave: gcry_mpi_release (x); @@ -208,10 +210,10 @@ static void adjust (unsigned char *buf, size_t size, size_t target) { if (size < target) - { - memmove (&buf[target - size], buf, size); - memset (buf, 0, target - size); - } + { + memmove (&buf[target - size], buf, size); + memset (buf, 0, target - size); + } } @@ -324,70 +326,70 @@ gen_prime (gcry_mpi_t * ptest, unsigned int nbits, GNUNET_HashCode * hc) tmp = gcry_mpi_new (0); sp = gcry_mpi_new (0); while (1) - { - /* generate a random number */ - mpz_randomize (prime, nbits, hc); - /* Set high order bit to 1, set low order bit to 1. If we are - * generating a secret prime we are most probably doing that - * for RSA, to make sure that the modulus does have the - * requested key size we set the 2 high order bits. */ - gcry_mpi_set_bit (prime, nbits - 1); - gcry_mpi_set_bit (prime, nbits - 2); - gcry_mpi_set_bit (prime, 0); - - /* Calculate all remainders. */ - for (i = 0; i < no_of_small_prime_numbers; i++) - { - size_t written; - - gcry_mpi_set_ui (sp, small_prime_numbers[i]); - gcry_mpi_div (NULL, tmp, prime, sp, -1); - mods[i] = 0; - written = sizeof (unsigned int); - GNUNET_assert (0 == - gcry_mpi_print (GCRYMPI_FMT_USG, - (unsigned char *) &mods[i], written, - &written, tmp)); - adjust ((unsigned char *) &mods[i], written, sizeof (unsigned int)); - mods[i] = ntohl (mods[i]); - } - /* Now try some primes starting with prime. */ - for (step = 0; step < 20000; step += 2) { - /* Check against all the small primes we have in mods. */ + /* generate a random number */ + mpz_randomize (prime, nbits, hc); + /* Set high order bit to 1, set low order bit to 1. If we are + * generating a secret prime we are most probably doing that + * for RSA, to make sure that the modulus does have the + * requested key size we set the 2 high order bits. */ + gcry_mpi_set_bit (prime, nbits - 1); + gcry_mpi_set_bit (prime, nbits - 2); + gcry_mpi_set_bit (prime, 0); + + /* Calculate all remainders. */ for (i = 0; i < no_of_small_prime_numbers; i++) - { - uint16_t x = small_prime_numbers[i]; - - while (mods[i] + step >= x) - mods[i] -= x; - if (!(mods[i] + step)) - break; - } - if (i < no_of_small_prime_numbers) - continue; /* Found a multiple of an already known prime. */ - - gcry_mpi_add_ui (*ptest, prime, step); - if (!gcry_mpi_test_bit (*ptest, nbits - 2)) - break; - - /* Do a fast Fermat test now. */ - gcry_mpi_sub_ui (pminus1, *ptest, 1); - gcry_mpi_powm (result, val_2, pminus1, *ptest); - if ((!gcry_mpi_cmp_ui (result, 1)) && (is_prime (*ptest, 5, hc))) - { - /* Got it. */ - gcry_mpi_release (sp); - gcry_mpi_release (tmp); - gcry_mpi_release (val_2); - gcry_mpi_release (val_3); - gcry_mpi_release (result); - gcry_mpi_release (pminus1); - gcry_mpi_release (prime); - return; - } + { + size_t written; + + gcry_mpi_set_ui (sp, small_prime_numbers[i]); + gcry_mpi_div (NULL, tmp, prime, sp, -1); + mods[i] = 0; + written = sizeof (unsigned int); + GNUNET_assert (0 == + gcry_mpi_print (GCRYMPI_FMT_USG, + (unsigned char *) &mods[i], written, + &written, tmp)); + adjust ((unsigned char *) &mods[i], written, sizeof (unsigned int)); + mods[i] = ntohl (mods[i]); + } + /* Now try some primes starting with prime. */ + for (step = 0; step < 20000; step += 2) + { + /* Check against all the small primes we have in mods. */ + for (i = 0; i < no_of_small_prime_numbers; i++) + { + uint16_t x = small_prime_numbers[i]; + + while (mods[i] + step >= x) + mods[i] -= x; + if (!(mods[i] + step)) + break; + } + if (i < no_of_small_prime_numbers) + continue; /* Found a multiple of an already known prime. */ + + gcry_mpi_add_ui (*ptest, prime, step); + if (!gcry_mpi_test_bit (*ptest, nbits - 2)) + break; + + /* Do a fast Fermat test now. */ + gcry_mpi_sub_ui (pminus1, *ptest, 1); + gcry_mpi_powm (result, val_2, pminus1, *ptest); + if ((!gcry_mpi_cmp_ui (result, 1)) && (is_prime (*ptest, 5, hc))) + { + /* Got it. */ + gcry_mpi_release (sp); + gcry_mpi_release (tmp); + gcry_mpi_release (val_2); + gcry_mpi_release (val_3); + gcry_mpi_release (result); + gcry_mpi_release (pminus1); + gcry_mpi_release (prime); + return; + } + } } - } } /** @@ -397,11 +399,11 @@ gen_prime (gcry_mpi_t * ptest, unsigned int nbits, GNUNET_HashCode * hc) * @param hc the HC to use for PRNG (modified!) */ static void -generate_kblock_key (KBlock_secret_key *sk, unsigned int nbits, - GNUNET_HashCode * hc) +generate_kblock_key (KBlock_secret_key * sk, unsigned int nbits, + GNUNET_HashCode * hc) { gcry_mpi_t t1, t2; - gcry_mpi_t phi; /* helper: (p-1)(q-1) */ + gcry_mpi_t phi; /* helper: (p-1)(q-1) */ gcry_mpi_t g; gcry_mpi_t f; @@ -423,36 +425,36 @@ generate_kblock_key (KBlock_secret_key *sk, unsigned int nbits, f = gcry_mpi_new (0); do - { - do { - gcry_mpi_release (sk->p); - gcry_mpi_release (sk->q); - gen_prime (&sk->p, nbits / 2, hc); - gen_prime (&sk->q, nbits / 2, hc); - - if (gcry_mpi_cmp (sk->p, sk->q) > 0) /* p shall be smaller than q (for calc of u) */ - gcry_mpi_swap (sk->p, sk->q); - /* calculate the modulus */ - gcry_mpi_mul (sk->n, sk->p, sk->q); + do + { + gcry_mpi_release (sk->p); + gcry_mpi_release (sk->q); + gen_prime (&sk->p, nbits / 2, hc); + gen_prime (&sk->q, nbits / 2, hc); + + if (gcry_mpi_cmp (sk->p, sk->q) > 0) /* p shall be smaller than q (for calc of u) */ + gcry_mpi_swap (sk->p, sk->q); + /* calculate the modulus */ + gcry_mpi_mul (sk->n, sk->p, sk->q); + } + while (gcry_mpi_get_nbits (sk->n) != nbits); + + /* calculate Euler totient: phi = (p-1)(q-1) */ + gcry_mpi_sub_ui (t1, sk->p, 1); + gcry_mpi_sub_ui (t2, sk->q, 1); + gcry_mpi_mul (phi, t1, t2); + gcry_mpi_gcd (g, t1, t2); + gcry_mpi_div (f, NULL, phi, g, 0); + while (0 == gcry_mpi_gcd (t1, sk->e, phi)) + { /* (while gcd is not 1) */ + gcry_mpi_add_ui (sk->e, sk->e, 2); + } + + /* calculate the secret key d = e^1 mod phi */ } - while (gcry_mpi_get_nbits (sk->n) != nbits); - - /* calculate Euler totient: phi = (p-1)(q-1) */ - gcry_mpi_sub_ui (t1, sk->p, 1); - gcry_mpi_sub_ui (t2, sk->q, 1); - gcry_mpi_mul (phi, t1, t2); - gcry_mpi_gcd (g, t1, t2); - gcry_mpi_div (f, NULL, phi, g, 0); - while (0 == gcry_mpi_gcd (t1, sk->e, phi)) - { /* (while gcd is not 1) */ - gcry_mpi_add_ui (sk->e, sk->e, 2); - } - - /* calculate the secret key d = e^1 mod phi */ - } while ((0 == gcry_mpi_invm (sk->d, sk->e, f)) || - (0 == gcry_mpi_invm (sk->u, sk->p, sk->q))); + (0 == gcry_mpi_invm (sk->u, sk->p, sk->q))); gcry_mpi_release (t1); gcry_mpi_release (t2); @@ -471,13 +473,13 @@ struct KskRsaPrivateKeyBinaryEncoded * Total size of the structure, in bytes, in big-endian! */ uint16_t len GNUNET_PACKED; - uint16_t sizen GNUNET_PACKED; /* in big-endian! */ - uint16_t sizee GNUNET_PACKED; /* in big-endian! */ - uint16_t sized GNUNET_PACKED; /* in big-endian! */ - uint16_t sizep GNUNET_PACKED; /* in big-endian! */ - uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ - uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ - uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ + uint16_t sizen GNUNET_PACKED; /* in big-endian! */ + uint16_t sizee GNUNET_PACKED; /* in big-endian! */ + uint16_t sized GNUNET_PACKED; /* in big-endian! */ + uint16_t sizep GNUNET_PACKED; /* in big-endian! */ + uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ + uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ + uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ /* followed by the actual values */ }; @@ -499,16 +501,16 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc) size_t size; hx = *hc; - generate_kblock_key (&sk, 1024, /* at least 10x as fast than 2048 bits - * -- we simply cannot afford 2048 bits - * even on modern hardware, and especially - * not since clearly a dictionary attack - * will still be much cheaper - * than breaking a 1024 bit RSA key. - * If an adversary can spend the time to - * break a 1024 bit RSA key just to forge - * a signature -- SO BE IT. [ CG, 6/2005 ] */ - &hx); + generate_kblock_key (&sk, 1024, /* at least 10x as fast than 2048 bits + * -- we simply cannot afford 2048 bits + * even on modern hardware, and especially + * not since clearly a dictionary attack + * will still be much cheaper + * than breaking a 1024 bit RSA key. + * If an adversary can spend the time to + * break a 1024 bit RSA key just to forge + * a signature -- SO BE IT. [ CG, 6/2005 ] */ + &hx); pkv[0] = &sk.n; pkv[1] = &sk.e; pkv[2] = &sk.d; @@ -517,10 +519,10 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc) pkv[5] = &sk.u; size = sizeof (struct KskRsaPrivateKeyBinaryEncoded); for (i = 0; i < 6; i++) - { - gcry_mpi_aprint (GCRYMPI_FMT_STD, &pbu[i], &sizes[i], *pkv[i]); - size += sizes[i]; - } + { + gcry_mpi_aprint (GCRYMPI_FMT_STD, &pbu[i], &sizes[i], *pkv[i]); + size += sizes[i]; + } GNUNET_assert (size < 65536); retval = GNUNET_malloc (size); retval->len = htons (size); @@ -545,10 +547,10 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc) retval->sizedmq1 = htons (0); memcpy (&((char *) &retval[1])[i], pbu[5], sizes[5]); for (i = 0; i < 6; i++) - { - gcry_mpi_release (*pkv[i]); - free (pbu[i]); - } + { + gcry_mpi_release (*pkv[i]); + free (pbu[i]); + } return retval; } @@ -570,122 +572,123 @@ ksk_decode_key (const struct KskRsaPrivateKeyBinaryEncoded *encoding) pos = 0; size = ntohs (encoding->sizen); rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); + &((const unsigned char *) (&encoding[1]))[pos], size, + &size); pos += ntohs (encoding->sizen); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + return NULL; + } size = ntohs (encoding->sizee); rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); + &((const unsigned char *) (&encoding[1]))[pos], size, + &size); pos += ntohs (encoding->sizee); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + return NULL; + } size = ntohs (encoding->sized); rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); + &((const unsigned char *) (&encoding[1]))[pos], size, + &size); pos += ntohs (encoding->sized); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - gcry_mpi_release (e); - return NULL; - } - /* swap p and q! */ - size = ntohs (encoding->sizep); - if (size > 0) - { - rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); - pos += ntohs (encoding->sizep); - if (rc) { LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); gcry_mpi_release (n); gcry_mpi_release (e); - gcry_mpi_release (d); return NULL; } - } + /* swap p and q! */ + size = ntohs (encoding->sizep); + if (size > 0) + { + rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG, + &((const unsigned char *) (&encoding[1]))[pos], + size, &size); + pos += ntohs (encoding->sizep); + if (rc) + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + gcry_mpi_release (e); + gcry_mpi_release (d); + return NULL; + } + } else q = NULL; size = ntohs (encoding->sizeq); if (size > 0) - { - rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); - pos += ntohs (encoding->sizeq); - if (rc) { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - gcry_mpi_release (e); - gcry_mpi_release (d); - if (q != NULL) - gcry_mpi_release (q); - return NULL; + rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG, + &((const unsigned char *) (&encoding[1]))[pos], + size, &size); + pos += ntohs (encoding->sizeq); + if (rc) + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + gcry_mpi_release (e); + gcry_mpi_release (d); + if (q != NULL) + gcry_mpi_release (q); + return NULL; + } } - } else p = NULL; pos += ntohs (encoding->sizedmp1); pos += ntohs (encoding->sizedmq1); size = - ntohs (encoding->len) - sizeof (struct KskRsaPrivateKeyBinaryEncoded) - - pos; + ntohs (encoding->len) - sizeof (struct KskRsaPrivateKeyBinaryEncoded) - + pos; if (size > 0) - { - rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); - if (rc) { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - gcry_mpi_release (e); - gcry_mpi_release (d); - if (p != NULL) - gcry_mpi_release (p); - if (q != NULL) - gcry_mpi_release (q); - return NULL; + rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG, + &((const unsigned char *) (&encoding[1]))[pos], + size, &size); + if (rc) + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + gcry_mpi_release (e); + gcry_mpi_release (d); + if (p != NULL) + gcry_mpi_release (p); + if (q != NULL) + gcry_mpi_release (q); + return NULL; + } } - } else u = NULL; if ((p != NULL) && (q != NULL) && (u != NULL)) - { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))", - n, e, d, p, q, u); - } - else - { - if ((p != NULL) && (q != NULL)) { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))", - n, e, d, p, q); + rc = gcry_sexp_build (&res, &size, /* erroff */ + "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))", + n, e, d, p, q, u); } - else + else { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, d); + if ((p != NULL) && (q != NULL)) + { + rc = gcry_sexp_build (&res, &size, /* erroff */ + "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))", + n, e, d, p, q); + } + else + { + rc = gcry_sexp_build (&res, &size, /* erroff */ + "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, + d); + } } - } gcry_mpi_release (n); gcry_mpi_release (e); gcry_mpi_release (d); @@ -700,10 +703,10 @@ ksk_decode_key (const struct KskRsaPrivateKeyBinaryEncoded *encoding) LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); #if EXTRA_CHECKS if (gcry_pk_testkey (res)) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); + return NULL; + } #endif ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); ret->sexp = res; @@ -733,13 +736,13 @@ GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc) unsigned int i; for (i = 0; i < cacheSize; i++) - { - if (0 == memcmp (hc, &cache[i]->hc, sizeof (GNUNET_HashCode))) { - ret = ksk_decode_key (cache[i]->pke); - return ret; + if (0 == memcmp (hc, &cache[i]->hc, sizeof (GNUNET_HashCode))) + { + ret = ksk_decode_key (cache[i]->pke); + return ret; + } } - } line = GNUNET_malloc (sizeof (struct KBlockKeyCacheLine)); line->hc = *hc; @@ -755,10 +758,10 @@ void __attribute__ ((destructor)) GNUNET_CRYPTO_ksk_fini () unsigned int i; for (i = 0; i < cacheSize; i++) - { - GNUNET_free (cache[i]->pke); - GNUNET_free (cache[i]); - } + { + GNUNET_free (cache[i]->pke); + GNUNET_free (cache[i]); + } GNUNET_array_grow (cache, cacheSize, 0); } diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c index 445f99a62..7fb2d1428 100644 --- a/src/util/crypto_random.c +++ b/src/util/crypto_random.c @@ -30,6 +30,10 @@ #include "gnunet_os_lib.h" #include +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + /** * Create a cryptographically weak pseudo-random number in the interval of 0 to 1. * @@ -61,37 +65,37 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i) GNUNET_assert (i > 0); switch (mode) - { - case GNUNET_CRYPTO_QUALITY_STRONG: - /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ + { + case GNUNET_CRYPTO_QUALITY_STRONG: + /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ #ifdef gcry_fast_random_poll - if ((invokeCount++ % 256) == 0) - gcry_fast_random_poll (); + if ((invokeCount++ % 256) == 0) + gcry_fast_random_poll (); #endif - ul = UINT32_MAX - (UINT32_MAX % i); - do - { - gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t), - GCRY_STRONG_RANDOM); + ul = UINT32_MAX - (UINT32_MAX % i); + do + { + gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t), + GCRY_STRONG_RANDOM); + } + while (ret >= ul); + return ret % i; + case GNUNET_CRYPTO_QUALITY_NONCE: + ul = UINT32_MAX - (UINT32_MAX % i); + do + { + gcry_create_nonce (&ret, sizeof (ret)); + } + while (ret >= ul); + return ret % i; + case GNUNET_CRYPTO_QUALITY_WEAK: + ret = i * weak_random (); + if (ret >= i) + ret = i - 1; + return ret; + default: + GNUNET_assert (0); } - while (ret >= ul); - return ret % i; - case GNUNET_CRYPTO_QUALITY_NONCE: - ul = UINT32_MAX - (UINT32_MAX % i); - do - { - gcry_create_nonce (&ret, sizeof (ret)); - } - while (ret >= ul); - return ret % i; - case GNUNET_CRYPTO_QUALITY_WEAK: - ret = i * weak_random (); - if (ret >= i) - ret = i - 1; - return ret; - default: - GNUNET_assert (0); - } return 0; } @@ -117,12 +121,12 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n) for (i = 0; i < n; i++) ret[i] = i; for (i = n - 1; i > 0; i--) - { - x = GNUNET_CRYPTO_random_u32 (mode, i + 1); - tmp = ret[x]; - ret[x] = ret[i]; - ret[i] = tmp; - } + { + x = GNUNET_CRYPTO_random_u32 (mode, i + 1); + tmp = ret[x]; + ret[x] = ret[i]; + ret[i] = tmp; + } return ret; } @@ -142,33 +146,33 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max) GNUNET_assert (max > 0); switch (mode) - { - case GNUNET_CRYPTO_QUALITY_STRONG: - ul = UINT64_MAX - (UINT64_MAX % max); - do - { - gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t), - GCRY_STRONG_RANDOM); - } - while (ret >= ul); - return ret % max; - case GNUNET_CRYPTO_QUALITY_NONCE: - ul = UINT64_MAX - (UINT64_MAX % max); - do { - gcry_create_nonce (&ret, sizeof (ret)); + case GNUNET_CRYPTO_QUALITY_STRONG: + ul = UINT64_MAX - (UINT64_MAX % max); + do + { + gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t), + GCRY_STRONG_RANDOM); + } + while (ret >= ul); + return ret % max; + case GNUNET_CRYPTO_QUALITY_NONCE: + ul = UINT64_MAX - (UINT64_MAX % max); + do + { + gcry_create_nonce (&ret, sizeof (ret)); + } + while (ret >= ul); + + return ret % max; + case GNUNET_CRYPTO_QUALITY_WEAK: + ret = max * weak_random (); + if (ret >= max) + ret = max - 1; + return ret; + default: + GNUNET_assert (0); } - while (ret >= ul); - - return ret % max; - case GNUNET_CRYPTO_QUALITY_WEAK: - ret = max * weak_random (); - if (ret >= max) - ret = max - 1; - return ret; - default: - GNUNET_assert (0); - } return 0; } @@ -196,7 +200,7 @@ static struct GNUNET_OS_Process *genproc; */ static void entropy_generator (void *cls, const char *what, int printchar, int current, - int total) + int total) { unsigned long code; enum GNUNET_OS_ProcessStatusType type; @@ -205,39 +209,39 @@ entropy_generator (void *cls, const char *what, int printchar, int current, if (0 != strcmp (what, "need_entropy")) return; if (current == total) - { - if (genproc != NULL) { + if (genproc != NULL) + { + if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill"); + GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); + GNUNET_OS_process_close (genproc); + genproc = NULL; + } + return; + } + if (genproc != NULL) + { + ret = GNUNET_OS_process_status (genproc, &type, &code); + if (ret == GNUNET_NO) + return; /* still running */ + if (ret == GNUNET_SYSERR) + { + GNUNET_break (0); + return; + } if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill"); GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); GNUNET_OS_process_close (genproc); genproc = NULL; } - return; - } - if (genproc != NULL) - { - ret = GNUNET_OS_process_status (genproc, &type, &code); - if (ret == GNUNET_NO) - return; /* still running */ - if (ret == GNUNET_SYSERR) - { - GNUNET_break (0); - return; - } - if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); - GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); - GNUNET_OS_process_close (genproc); - genproc = NULL; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Starting `%s' process to generate entropy\n"), "find"); + LOG (GNUNET_ERROR_TYPE_INFO, + _("Starting `%s' process to generate entropy\n"), "find"); genproc = - GNUNET_OS_start_process (NULL, NULL, "sh", "sh", "-c", - "exec find / -mount -type f -exec cp {} /dev/null \\; 2>/dev/null", - NULL); + GNUNET_OS_start_process (NULL, NULL, "sh", "sh", "-c", + "exec find / -mount -type f -exec cp {} /dev/null \\; 2>/dev/null", + NULL); } @@ -245,11 +249,11 @@ static void killfind () { if (genproc != NULL) - { - GNUNET_OS_process_kill (genproc, SIGKILL); - GNUNET_OS_process_close (genproc); - genproc = NULL; - } + { + GNUNET_OS_process_kill (genproc, SIGKILL); + GNUNET_OS_process_close (genproc); + genproc = NULL; + } } @@ -257,20 +261,21 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init () { gcry_control (GCRYCTL_DISABLE_SECMEM, 0); if (!gcry_check_version (GCRYPT_VERSION)) - { - fprintf (stderr, - _ - ("libgcrypt has not the expected version (version %s is required).\n"), - GCRYPT_VERSION); - abort (); - } + { + fprintf (stderr, + _ + ("libgcrypt has not the expected version (version %s is required).\n"), + GCRYPT_VERSION); + abort (); + } #ifdef gcry_fast_random_poll gcry_fast_random_poll (); #endif gcry_set_progress_handler (&entropy_generator, NULL); atexit (&killfind); SRANDOM (time (NULL) ^ - GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX)); + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, + UINT32_MAX)); } diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c index 46778a842..7f0c81692 100644 --- a/src/util/crypto_rsa.c +++ b/src/util/crypto_rsa.c @@ -37,6 +37,12 @@ #include "gnunet_crypto_lib.h" #include "gnunet_disk_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + /** * The private information of an RSA key pair. * NOTE: this must match the definition in crypto_ksk.c @@ -60,13 +66,13 @@ struct RsaPrivateKeyBinaryEncoded * Total size of the structure, in bytes, in big-endian! */ uint16_t len GNUNET_PACKED; - uint16_t sizen GNUNET_PACKED; /* in big-endian! */ - uint16_t sizee GNUNET_PACKED; /* in big-endian! */ - uint16_t sized GNUNET_PACKED; /* in big-endian! */ - uint16_t sizep GNUNET_PACKED; /* in big-endian! */ - uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ - uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ - uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ + uint16_t sizen GNUNET_PACKED; /* in big-endian! */ + uint16_t sizee GNUNET_PACKED; /* in big-endian! */ + uint16_t sized GNUNET_PACKED; /* in big-endian! */ + uint16_t sizep GNUNET_PACKED; /* in big-endian! */ + uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ + uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ + uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ /* followed by the actual values */ }; @@ -81,7 +87,7 @@ struct RsaPrivateKeyBinaryEncoded * a failure of the command 'cmd' with the message given * by gcry_strerror(rc). */ -#define LOG_GCRY(level, cmd, rc) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0); +#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); /** * If target != size, move target bytes to the @@ -92,10 +98,10 @@ static void adjust (unsigned char *buf, size_t size, size_t target) { if (size < target) - { - memmove (&buf[target - size], buf, size); - memset (buf, 0, target - size); - } + { + memmove (&buf[target - size], buf, size); + memset (buf, 0, target - size); + } } /** @@ -109,9 +115,9 @@ GNUNET_CRYPTO_rsa_key_create () gcry_sexp_t s_keyparam; GNUNET_assert (0 == - gcry_sexp_build (&s_keyparam, NULL, - "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))", - HOSTKEY_LEN)); + gcry_sexp_build (&s_keyparam, NULL, + "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))", + HOSTKEY_LEN)); GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam)); gcry_sexp_release (s_keyparam); #if EXTRA_CHECKS @@ -134,7 +140,7 @@ GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey) static int key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname, - const char *elems) + const char *elems) { gcry_sexp_t list, l2; const char *s; @@ -142,44 +148,44 @@ key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname, list = gcry_sexp_find_token (sexp, topname, 0); if (!list) - { - return 1; - } + { + return 1; + } l2 = gcry_sexp_cadr (list); gcry_sexp_release (list); list = l2; if (!list) - { - return 2; - } + { + return 2; + } idx = 0; for (s = elems; *s; s++, idx++) - { - l2 = gcry_sexp_find_token (list, s, 1); - if (!l2) - { - for (i = 0; i < idx; i++) - { - gcry_free (array[i]); - array[i] = NULL; - } - gcry_sexp_release (list); - return 3; /* required parameter not found */ - } - array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG); - gcry_sexp_release (l2); - if (!array[idx]) { - for (i = 0; i < idx; i++) - { - gcry_free (array[i]); - array[i] = NULL; - } - gcry_sexp_release (list); - return 4; /* required parameter is invalid */ + l2 = gcry_sexp_find_token (list, s, 1); + if (!l2) + { + for (i = 0; i < idx; i++) + { + gcry_free (array[i]); + array[i] = NULL; + } + gcry_sexp_release (list); + return 3; /* required parameter not found */ + } + array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG); + gcry_sexp_release (l2); + if (!array[idx]) + { + for (i = 0; i < idx; i++) + { + gcry_free (array[i]); + array[i] = NULL; + } + gcry_sexp_release (list); + return 4; /* required parameter is invalid */ + } } - } gcry_sexp_release (list); return 0; } @@ -191,9 +197,10 @@ key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname, */ void GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey - *priv, - struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded - *pub) + *priv, + struct + GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded + *pub) { gcry_mpi_t skey[2]; size_t size; @@ -206,24 +213,25 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey rc = key_from_sexp (skey, priv->sexp, "rsa", "ne"); GNUNET_assert (0 == rc); pub->len = - htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) - - sizeof (pub->padding)); + htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) - + sizeof (pub->padding)); pub->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); pub->padding = 0; size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; GNUNET_assert (0 == - gcry_mpi_print (GCRYMPI_FMT_USG, &pub->key[0], size, &size, - skey[0])); + gcry_mpi_print (GCRYMPI_FMT_USG, &pub->key[0], size, &size, + skey[0])); adjust (&pub->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); - size = GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; + size = + GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; GNUNET_assert (0 == - gcry_mpi_print (GCRYMPI_FMT_USG, - &pub->key - [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size, - &size, skey[1])); + gcry_mpi_print (GCRYMPI_FMT_USG, + &pub->key + [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], + size, &size, skey[1])); adjust (&pub->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size, - GNUNET_CRYPTO_RSA_KEY_LENGTH - - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); + GNUNET_CRYPTO_RSA_KEY_LENGTH - + GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); gcry_mpi_release (skey[0]); gcry_mpi_release (skey[1]); } @@ -237,7 +245,7 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey */ static struct GNUNET_CRYPTO_RsaPrivateKey * public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded - *publicKey) + *publicKey) { struct GNUNET_CRYPTO_RsaPrivateKey *ret; gcry_sexp_t result; @@ -251,36 +259,38 @@ public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded (ntohs (publicKey->len) != sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) - sizeof (publicKey->padding))) - { - GNUNET_break (0); - return NULL; - } + { + GNUNET_break (0); + return NULL; + } size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, &publicKey->key[0], size, &size); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - return NULL; - } - size = GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; - rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, - &publicKey->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], - size, &size); + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + return NULL; + } + size = + GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; + rc = + gcry_mpi_scan (&e, GCRYMPI_FMT_USG, + &publicKey->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], + size, &size); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + return NULL; + } rc = gcry_sexp_build (&result, &erroff, "(public-key(rsa(n %m)(e %m)))", n, - e); + e); gcry_mpi_release (n); gcry_mpi_release (e); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */ - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */ + return NULL; + } ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); ret->sexp = result; return ret; @@ -306,10 +316,10 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey) #if EXTRA_CHECKS if (gcry_pk_testkey (hostkey->sexp)) - { - GNUNET_break (0); - return NULL; - } + { + GNUNET_break (0); + return NULL; + } #endif memset (pkv, 0, sizeof (gcry_mpi_t) * 6); @@ -327,21 +337,21 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey) GNUNET_assert (0 == rc); size = sizeof (struct RsaPrivateKeyBinaryEncoded); for (i = 0; i < 6; i++) - { - if (pkv[i] != NULL) { - GNUNET_assert (0 == - gcry_mpi_aprint (GCRYMPI_FMT_USG, - (unsigned char **) &pbu[i], &sizes[i], - pkv[i])); - size += sizes[i]; + if (pkv[i] != NULL) + { + GNUNET_assert (0 == + gcry_mpi_aprint (GCRYMPI_FMT_USG, + (unsigned char **) &pbu[i], + &sizes[i], pkv[i])); + size += sizes[i]; + } + else + { + pbu[i] = NULL; + sizes[i] = 0; + } } - else - { - pbu[i] = NULL; - sizes[i] = 0; - } - } GNUNET_assert (size < 65536); retval = GNUNET_malloc (size); retval->len = htons (size); @@ -366,12 +376,12 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey) retval->sizedmq1 = htons (0); memcpy (&((char *) (&retval[1]))[i], pbu[5], sizes[5]); for (i = 0; i < 6; i++) - { - if (pkv[i] != NULL) - gcry_mpi_release (pkv[i]); - if (pbu[i] != NULL) - free (pbu[i]); - } + { + if (pkv[i] != NULL) + gcry_mpi_release (pkv[i]); + if (pbu[i] != NULL) + free (pbu[i]); + } return retval; } @@ -387,7 +397,7 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len) { struct GNUNET_CRYPTO_RsaPrivateKey *ret; const struct RsaPrivateKeyBinaryEncoded *encoding = - (const struct RsaPrivateKeyBinaryEncoded *) buf; + (const struct RsaPrivateKeyBinaryEncoded *) buf; gcry_sexp_t res; gcry_mpi_t n, e, d, p, q, u; int rc; @@ -402,121 +412,122 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len) pos = 0; size = ntohs (encoding->sizen); rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); + &((const unsigned char *) (&encoding[1]))[pos], size, + &size); pos += ntohs (encoding->sizen); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + return NULL; + } size = ntohs (encoding->sizee); rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); + &((const unsigned char *) (&encoding[1]))[pos], size, + &size); pos += ntohs (encoding->sizee); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + return NULL; + } size = ntohs (encoding->sized); rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); + &((const unsigned char *) (&encoding[1]))[pos], size, + &size); pos += ntohs (encoding->sized); if (rc) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - gcry_mpi_release (e); - return NULL; - } - /* swap p and q! */ - size = ntohs (encoding->sizep); - if (size > 0) - { - rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); - pos += ntohs (encoding->sizep); - if (rc) { LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); gcry_mpi_release (n); gcry_mpi_release (e); - gcry_mpi_release (d); return NULL; } - } + /* swap p and q! */ + size = ntohs (encoding->sizep); + if (size > 0) + { + rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG, + &((const unsigned char *) (&encoding[1]))[pos], + size, &size); + pos += ntohs (encoding->sizep); + if (rc) + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + gcry_mpi_release (e); + gcry_mpi_release (d); + return NULL; + } + } else q = NULL; size = ntohs (encoding->sizeq); if (size > 0) - { - rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); - pos += ntohs (encoding->sizeq); - if (rc) { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - gcry_mpi_release (e); - gcry_mpi_release (d); - if (q != NULL) - gcry_mpi_release (q); - return NULL; + rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG, + &((const unsigned char *) (&encoding[1]))[pos], + size, &size); + pos += ntohs (encoding->sizeq); + if (rc) + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + gcry_mpi_release (e); + gcry_mpi_release (d); + if (q != NULL) + gcry_mpi_release (q); + return NULL; + } } - } else p = NULL; pos += ntohs (encoding->sizedmp1); pos += ntohs (encoding->sizedmq1); size = - ntohs (encoding->len) - sizeof (struct RsaPrivateKeyBinaryEncoded) - pos; + ntohs (encoding->len) - sizeof (struct RsaPrivateKeyBinaryEncoded) - pos; if (size > 0) - { - rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG, - &((const unsigned char *) (&encoding[1]))[pos], size, - &size); - if (rc) { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); - gcry_mpi_release (n); - gcry_mpi_release (e); - gcry_mpi_release (d); - if (p != NULL) - gcry_mpi_release (p); - if (q != NULL) - gcry_mpi_release (q); - return NULL; + rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG, + &((const unsigned char *) (&encoding[1]))[pos], + size, &size); + if (rc) + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); + gcry_mpi_release (n); + gcry_mpi_release (e); + gcry_mpi_release (d); + if (p != NULL) + gcry_mpi_release (p); + if (q != NULL) + gcry_mpi_release (q); + return NULL; + } } - } else u = NULL; if ((p != NULL) && (q != NULL) && (u != NULL)) - { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))", - n, e, d, p, q, u); - } - else - { - if ((p != NULL) && (q != NULL)) { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))", - n, e, d, p, q); + rc = gcry_sexp_build (&res, &size, /* erroff */ + "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))", + n, e, d, p, q, u); } - else + else { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, d); + if ((p != NULL) && (q != NULL)) + { + rc = gcry_sexp_build (&res, &size, /* erroff */ + "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))", + n, e, d, p, q); + } + else + { + rc = gcry_sexp_build (&res, &size, /* erroff */ + "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, + d); + } } - } gcry_mpi_release (n); gcry_mpi_release (e); gcry_mpi_release (d); @@ -531,10 +542,10 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len) LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); #if EXTRA_CHECKS if (gcry_pk_testkey (res)) - { - LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); - return NULL; - } + { + LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); + return NULL; + } #endif ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); ret->sexp = res; @@ -571,167 +582,172 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename)) return NULL; while (GNUNET_YES != GNUNET_DISK_file_test (filename)) - { - fd = GNUNET_DISK_file_open (filename, - GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE - | GNUNET_DISK_OPEN_FAILIFEXISTS, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); - if (NULL == fd) - { - if (errno == EEXIST) - { - if (GNUNET_YES != GNUNET_DISK_file_test (filename)) - { - /* must exist but not be accessible, fail for good! */ - if (0 != ACCESS (filename, R_OK)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", - filename); - else - GNUNET_break (0); /* what is going on!? */ - return NULL; - } - continue; - } - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); - return NULL; - } - cnt = 0; - - while (GNUNET_YES != - GNUNET_DISK_file_lock (fd, 0, - sizeof (struct RsaPrivateKeyBinaryEncoded), - GNUNET_YES)) { - sleep (1); - if (0 == ++cnt % 10) - { - ec = errno; - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not aquire lock on file `%s': %s...\n"), filename, - STRERROR (ec)); - } + fd = GNUNET_DISK_file_open (filename, + GNUNET_DISK_OPEN_WRITE | + GNUNET_DISK_OPEN_CREATE | + GNUNET_DISK_OPEN_FAILIFEXISTS, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); + if (NULL == fd) + { + if (errno == EEXIST) + { + if (GNUNET_YES != GNUNET_DISK_file_test (filename)) + { + /* must exist but not be accessible, fail for good! */ + if (0 != ACCESS (filename, R_OK)) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", + filename); + else + GNUNET_break (0); /* what is going on!? */ + return NULL; + } + continue; + } + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); + return NULL; + } + cnt = 0; + + while (GNUNET_YES != + GNUNET_DISK_file_lock (fd, 0, + sizeof (struct + RsaPrivateKeyBinaryEncoded), + GNUNET_YES)) + { + sleep (1); + if (0 == ++cnt % 10) + { + ec = errno; + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Could not aquire lock on file `%s': %s...\n"), filename, + STRERROR (ec)); + } + } + LOG (GNUNET_ERROR_TYPE_INFO, + _("Creating a new private key. This may take a while.\n")); + ret = GNUNET_CRYPTO_rsa_key_create (); + GNUNET_assert (ret != NULL); + enc = rsa_encode_key (ret); + GNUNET_assert (enc != NULL); + GNUNET_assert (ntohs (enc->len) == + GNUNET_DISK_file_write (fd, enc, ntohs (enc->len))); + GNUNET_free (enc); + + GNUNET_DISK_file_sync (fd); + if (GNUNET_YES != + GNUNET_DISK_file_unlock (fd, 0, + sizeof (struct + RsaPrivateKeyBinaryEncoded))) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); + GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); + GNUNET_CRYPTO_rsa_key_get_public (ret, &pub); + GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey); + LOG (GNUNET_ERROR_TYPE_INFO, + _("I am host `%s'. Stored new private key in `%s'.\n"), + GNUNET_i2s (&pid), filename); + return ret; } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Creating a new private key. This may take a while.\n")); - ret = GNUNET_CRYPTO_rsa_key_create (); - GNUNET_assert (ret != NULL); - enc = rsa_encode_key (ret); - GNUNET_assert (enc != NULL); - GNUNET_assert (ntohs (enc->len) == - GNUNET_DISK_file_write (fd, enc, ntohs (enc->len))); - GNUNET_free (enc); - - GNUNET_DISK_file_sync (fd); - if (GNUNET_YES != - GNUNET_DISK_file_unlock (fd, 0, - sizeof (struct RsaPrivateKeyBinaryEncoded))) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); - GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); - GNUNET_CRYPTO_rsa_key_get_public (ret, &pub); - GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("I am host `%s'. Stored new private key in `%s'.\n"), - GNUNET_i2s (&pid), filename); - return ret; - } /* hostkey file exists already, read it! */ fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, - GNUNET_DISK_PERM_NONE); + GNUNET_DISK_PERM_NONE); if (NULL == fd) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); - return NULL; - } - cnt = 0; - while (1) - { - if (GNUNET_YES != - GNUNET_DISK_file_lock (fd, 0, - sizeof (struct RsaPrivateKeyBinaryEncoded), - GNUNET_NO)) { - if (0 == ++cnt % 60) - { - ec = errno; - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not aquire lock on file `%s': %s...\n"), filename, - STRERROR (ec)); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("This may be ok if someone is currently generating a hostkey.\n")); - } - sleep (1); - continue; - } - if (GNUNET_YES != GNUNET_DISK_file_test (filename)) - { - /* eh, what!? File we opened is now gone!? */ - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename); - if (GNUNET_YES != - GNUNET_DISK_file_unlock (fd, 0, - sizeof (struct RsaPrivateKeyBinaryEncoded))) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); - GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd)); - + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); return NULL; } - if (GNUNET_YES != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES)) - fs = 0; - if (fs < sizeof (struct RsaPrivateKeyBinaryEncoded)) + cnt = 0; + while (1) { - /* maybe we got the read lock before the hostkey generating - * process had a chance to get the write lock; give it up! */ if (GNUNET_YES != - GNUNET_DISK_file_unlock (fd, 0, - sizeof (struct RsaPrivateKeyBinaryEncoded))) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); - if (0 == ++cnt % 10) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("When trying to read hostkey file `%s' I found %u bytes but I need at least %u.\n"), - filename, (unsigned int) fs, - (unsigned int) sizeof (struct RsaPrivateKeyBinaryEncoded)); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("This may be ok if someone is currently generating a hostkey.\n")); - } - sleep (2); /* wait a bit longer! */ - continue; + GNUNET_DISK_file_lock (fd, 0, + sizeof (struct RsaPrivateKeyBinaryEncoded), + GNUNET_NO)) + { + if (0 == ++cnt % 60) + { + ec = errno; + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Could not aquire lock on file `%s': %s...\n"), filename, + STRERROR (ec)); + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("This may be ok if someone is currently generating a hostkey.\n")); + } + sleep (1); + continue; + } + if (GNUNET_YES != GNUNET_DISK_file_test (filename)) + { + /* eh, what!? File we opened is now gone!? */ + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename); + if (GNUNET_YES != + GNUNET_DISK_file_unlock (fd, 0, + sizeof (struct + RsaPrivateKeyBinaryEncoded))) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); + GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd)); + + return NULL; + } + if (GNUNET_YES != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES)) + fs = 0; + if (fs < sizeof (struct RsaPrivateKeyBinaryEncoded)) + { + /* maybe we got the read lock before the hostkey generating + * process had a chance to get the write lock; give it up! */ + if (GNUNET_YES != + GNUNET_DISK_file_unlock (fd, 0, + sizeof (struct + RsaPrivateKeyBinaryEncoded))) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); + if (0 == ++cnt % 10) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("When trying to read hostkey file `%s' I found %u bytes but I need at least %u.\n"), + filename, (unsigned int) fs, + (unsigned int) sizeof (struct RsaPrivateKeyBinaryEncoded)); + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("This may be ok if someone is currently generating a hostkey.\n")); + } + sleep (2); /* wait a bit longer! */ + continue; + } + break; } - break; - } enc = GNUNET_malloc (fs); GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs)); len = ntohs (enc->len); ret = NULL; if ((len != fs) || (NULL == (ret = GNUNET_CRYPTO_rsa_decode_key ((char *) enc, len)))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("File `%s' does not contain a valid private key. Deleting it.\n"), - filename); - if (0 != UNLINK (filename)) { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename); + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("File `%s' does not contain a valid private key. Deleting it.\n"), + filename); + if (0 != UNLINK (filename)) + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename); + } } - } GNUNET_free (enc); if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, - sizeof (struct RsaPrivateKeyBinaryEncoded))) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); + sizeof (struct RsaPrivateKeyBinaryEncoded))) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); if (ret != NULL) - { - GNUNET_CRYPTO_rsa_key_get_public (ret, &pub); - GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("I am host `%s'. Read private key from `%s'.\n"), - GNUNET_i2s (&pid), filename); - } + { + GNUNET_CRYPTO_rsa_key_get_public (ret, &pub); + GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey); + LOG (GNUNET_ERROR_TYPE_INFO, + _("I am host `%s'. Read private key from `%s'.\n"), + GNUNET_i2s (&pid), filename); + } return ret; } @@ -748,9 +764,9 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) */ int GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size, - const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded - *publicKey, - struct GNUNET_CRYPTO_RsaEncryptedData *target) + const struct + GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey, + struct GNUNET_CRYPTO_RsaEncryptedData *target) { gcry_sexp_t result; gcry_sexp_t data; @@ -766,10 +782,10 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size, return GNUNET_SYSERR; isize = size; GNUNET_assert (0 == - gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize)); + gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize)); GNUNET_assert (0 == - gcry_sexp_build (&data, &erroff, - "(data (flags pkcs1)(value %m))", val)); + gcry_sexp_build (&data, &erroff, + "(data (flags pkcs1)(value %m))", val)); gcry_mpi_release (val); GNUNET_assert (0 == gcry_pk_encrypt (&result, data, pubkey->sexp)); gcry_sexp_release (data); @@ -779,11 +795,11 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size, gcry_sexp_release (result); isize = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData); GNUNET_assert (0 == - gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) target, - isize, &isize, rval)); + gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) target, + isize, &isize, rval)); gcry_mpi_release (rval); adjust (&target->encoding[0], isize, - sizeof (struct GNUNET_CRYPTO_RsaEncryptedData)); + sizeof (struct GNUNET_CRYPTO_RsaEncryptedData)); return GNUNET_OK; } @@ -799,8 +815,8 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size, */ ssize_t GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key, - const struct GNUNET_CRYPTO_RsaEncryptedData * block, - void *result, size_t max) + const struct GNUNET_CRYPTO_RsaEncryptedData * + block, void *result, size_t max) { gcry_sexp_t resultsexp; gcry_sexp_t data; @@ -815,21 +831,22 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key, #endif size = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData); GNUNET_assert (0 == - gcry_mpi_scan (&val, GCRYMPI_FMT_USG, &block->encoding[0], - size, &size)); + gcry_mpi_scan (&val, GCRYMPI_FMT_USG, &block->encoding[0], + size, &size)); GNUNET_assert (0 == - gcry_sexp_build (&data, &erroff, "(enc-val(flags)(rsa(a %m)))", - val)); + gcry_sexp_build (&data, &erroff, + "(enc-val(flags)(rsa(a %m)))", val)); gcry_mpi_release (val); GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp)); gcry_sexp_release (data); /* resultsexp has format "(value %m)" */ GNUNET_assert (NULL != - (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG))); + (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG))); gcry_sexp_release (resultsexp); tmp = GNUNET_malloc (max + HOSTKEY_LEN / 8); size = max + HOSTKEY_LEN / 8; - GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val)); + GNUNET_assert (0 == + gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val)); gcry_mpi_release (val); endp = tmp; endp += (size - max); @@ -850,8 +867,8 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key, */ int GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key, - const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose, - struct GNUNET_CRYPTO_RsaSignature *sig) + const struct GNUNET_CRYPTO_RsaSignaturePurpose + *purpose, struct GNUNET_CRYPTO_RsaSignature *sig) { gcry_sexp_t result; gcry_sexp_t data; @@ -867,10 +884,10 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key, buff = GNUNET_malloc (bufSize); memcpy (buff, FORMATSTRING, bufSize); memcpy (&buff - [bufSize - - strlen - ("0123456789012345678901234567890123456789012345678901234567890123))") - - 1], &hc, sizeof (GNUNET_HashCode)); + [bufSize - + strlen + ("0123456789012345678901234567890123456789012345678901234567890123))") + - 1], &hc, sizeof (GNUNET_HashCode)); GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0)); GNUNET_free (buff); GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp)); @@ -879,8 +896,8 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key, gcry_sexp_release (result); ssize = sizeof (struct GNUNET_CRYPTO_RsaSignature); GNUNET_assert (0 == - gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) sig, ssize, - &ssize, rval)); + gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) sig, + ssize, &ssize, rval)); gcry_mpi_release (rval); adjust (sig->sig, ssize, sizeof (struct GNUNET_CRYPTO_RsaSignature)); return GNUNET_OK; @@ -898,11 +915,11 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key, */ int GNUNET_CRYPTO_rsa_verify (uint32_t purpose, - const struct GNUNET_CRYPTO_RsaSignaturePurpose - *validate, - const struct GNUNET_CRYPTO_RsaSignature *sig, - const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded - *publicKey) + const struct GNUNET_CRYPTO_RsaSignaturePurpose + *validate, + const struct GNUNET_CRYPTO_RsaSignature *sig, + const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded + *publicKey) { gcry_sexp_t data; gcry_sexp_t sigdata; @@ -916,44 +933,44 @@ GNUNET_CRYPTO_rsa_verify (uint32_t purpose, int rc; if (purpose != ntohl (validate->purpose)) - return GNUNET_SYSERR; /* purpose mismatch */ + return GNUNET_SYSERR; /* purpose mismatch */ GNUNET_CRYPTO_hash (validate, ntohl (validate->size), &hc); size = sizeof (struct GNUNET_CRYPTO_RsaSignature); GNUNET_assert (0 == - gcry_mpi_scan (&val, GCRYMPI_FMT_USG, - (const unsigned char *) sig, size, &size)); + gcry_mpi_scan (&val, GCRYMPI_FMT_USG, + (const unsigned char *) sig, size, &size)); GNUNET_assert (0 == - gcry_sexp_build (&sigdata, &erroff, "(sig-val(rsa(s %m)))", - val)); + gcry_sexp_build (&sigdata, &erroff, "(sig-val(rsa(s %m)))", + val)); gcry_mpi_release (val); bufSize = strlen (FORMATSTRING) + 1; buff = GNUNET_malloc (bufSize); memcpy (buff, FORMATSTRING, bufSize); memcpy (&buff - [strlen (FORMATSTRING) - - strlen - ("0123456789012345678901234567890123456789012345678901234567890123))")], - &hc, sizeof (GNUNET_HashCode)); + [strlen (FORMATSTRING) - + strlen + ("0123456789012345678901234567890123456789012345678901234567890123))")], + &hc, sizeof (GNUNET_HashCode)); GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0)); GNUNET_free (buff); hostkey = public2PrivateKey (publicKey); if (hostkey == NULL) - { - gcry_sexp_release (data); - gcry_sexp_release (sigdata); - return GNUNET_SYSERR; - } + { + gcry_sexp_release (data); + gcry_sexp_release (sigdata); + return GNUNET_SYSERR; + } rc = gcry_pk_verify (sigdata, data, hostkey->sexp); GNUNET_CRYPTO_rsa_key_free (hostkey); gcry_sexp_release (data); gcry_sexp_release (sigdata); if (rc) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("RSA signature verification failed at %s:%d: %s\n"), __FILE__, - __LINE__, gcry_strerror (rc)); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("RSA signature verification failed at %s:%d: %s\n"), __FILE__, + __LINE__, gcry_strerror (rc)); + return GNUNET_SYSERR; + } return GNUNET_OK; } diff --git a/src/util/disk.c b/src/util/disk.c index bb507bbff..2021d6493 100644 --- a/src/util/disk.c +++ b/src/util/disk.c @@ -34,6 +34,12 @@ #include "gnunet_crypto_lib.h" #include "disk.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + #define DEBUG_NPIPE GNUNET_EXTRA_LOGGING #define DEBUG_PIPE GNUNET_EXTRA_LOGGING @@ -61,8 +67,8 @@ #define PIPE_BUF 512 ULONG PipeSerialNumber; #endif -#define _IFMT 0170000 /* type of file */ -#define _IFLNK 0120000 /* symbolic link */ +#define _IFMT 0170000 /* type of file */ +#define _IFLNK 0120000 /* symbolic link */ #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK) #else #error PORT-ME: need to port statfs (how much space is left on the drive?) @@ -159,25 +165,25 @@ getSizeRec (void *cls, const char *fn) #ifdef HAVE_STAT64 if (0 != STAT64 (fn, &buf)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat64", fn); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat64", fn); + return GNUNET_SYSERR; + } #else if (0 != STAT (fn, &buf)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fn); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fn); + return GNUNET_SYSERR; + } #endif if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) gfsd->total += buf.st_size; if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) && ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) - { - if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) - return GNUNET_SYSERR; - } + { + if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -209,13 +215,13 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) */ off_t GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset, - enum GNUNET_DISK_Seek whence) + enum GNUNET_DISK_Seek whence) { if (h == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #ifdef MINGW DWORD ret; @@ -226,10 +232,10 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset, ret = SetFilePointer (h->h, offset, NULL, t[whence]); if (ret == INVALID_SET_FILE_POINTER) - { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; - } + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } return ret; #else static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET, @@ -255,7 +261,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset, */ int GNUNET_DISK_file_size (const char *filename, uint64_t * size, - int includeSymLinks) + int includeSymLinks) { struct GetFileSizeData gfsd; int ret; @@ -286,29 +292,29 @@ GNUNET_DISK_file_size (const char *filename, uint64_t * size, */ int GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, - uint64_t * ino) + uint64_t * ino) { #if LINUX struct stat sbuf; struct statvfs fbuf; if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf))) - { - *dev = (uint64_t) fbuf.f_fsid; - *ino = (uint64_t) sbuf.st_ino; - return GNUNET_OK; - } + { + *dev = (uint64_t) fbuf.f_fsid; + *ino = (uint64_t) sbuf.st_ino; + return GNUNET_OK; + } #elif SOMEBSD struct stat sbuf; struct statfs fbuf; if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf))) - { - *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || - ((uint64_t) fbuf.f_fsid.val[1]); - *ino = (uint64_t) sbuf.st_ino; - return GNUNET_OK; - } + { + *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || + ((uint64_t) fbuf.f_fsid.val[1]); + *ino = (uint64_t) sbuf.st_ino; + return GNUNET_OK; + } #elif WINDOWS // FIXME NILS: test this struct GNUNET_DISK_FileHandle *fh; @@ -321,11 +327,11 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, succ = GetFileInformationByHandle (fh->h, &info); GNUNET_DISK_file_close (fh); if (succ) - { - *dev = info.dwVolumeSerialNumber; - *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow); - return GNUNET_OK; - } + { + *dev = info.dwVolumeSerialNumber; + *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow); + return GNUNET_OK; + } else return GNUNET_SYSERR; @@ -357,37 +363,37 @@ GNUNET_DISK_mktemp (const char *t) #if WINDOWS && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) #endif - ) - { - tmpdir = getenv ("TMPDIR"); - tmpdir = tmpdir ? tmpdir : "/tmp"; - GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); - } + ) + { + tmpdir = getenv ("TMPDIR"); + tmpdir = tmpdir ? tmpdir : "/tmp"; + GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); + } else - { - GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX"); - } + { + GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX"); + } #ifdef MINGW fn = (char *) GNUNET_malloc (MAX_PATH + 1); if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn)) - { - GNUNET_free (fn); - GNUNET_free (tmpl); - return NULL; - } + { + GNUNET_free (fn); + GNUNET_free (tmpl); + return NULL; + } GNUNET_free (tmpl); #else fn = tmpl; #endif fd = mkstemp (fn); if (fd == -1) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); - GNUNET_free (fn); - return NULL; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); + GNUNET_free (fn); + return NULL; + } if (0 != CLOSE (fd)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn); return fn; } @@ -406,10 +412,10 @@ GNUNET_DISK_get_blocks_available (const char *part) struct statvfs buf; if (0 != statvfs (part, &buf)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); - return -1; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part); + return -1; + } return buf.f_bavail; #elif MINGW DWORD dwDummy; @@ -424,22 +430,22 @@ GNUNET_DISK_get_blocks_available (const char *part) GNUNET_free (path); szDrive[3] = 0; if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("`%s' failed for drive `%s': %u\n"), "GetDiskFreeSpace", - szDrive, GetLastError ()); + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("`%s' failed for drive `%s': %u\n"), "GetDiskFreeSpace", + szDrive, GetLastError ()); - return -1; - } + return -1; + } return dwBlocks; #else struct statfs s; if (0 != statfs (part, &s)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); - return -1; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part); + return -1; + } return s.f_bavail; #endif } @@ -463,21 +469,21 @@ GNUNET_DISK_directory_test (const char *fil) ret = STAT (fil, &filestat); if (ret != 0) - { - if (errno != ENOENT) { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil); - return GNUNET_SYSERR; + if (errno != ENOENT) + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); + return GNUNET_SYSERR; + } + return GNUNET_NO; } - return GNUNET_NO; - } if (!S_ISDIR (filestat.st_mode)) return GNUNET_NO; if (ACCESS (fil, R_OK | X_OK) < 0) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); + return GNUNET_SYSERR; + } return GNUNET_YES; } @@ -502,27 +508,27 @@ GNUNET_DISK_file_test (const char *fil) ret = STAT (rdir, &filestat); if (ret != 0) - { - if (errno != ENOENT) { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); + if (errno != ENOENT) + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); + GNUNET_free (rdir); + return GNUNET_SYSERR; + } GNUNET_free (rdir); - return GNUNET_SYSERR; + return GNUNET_NO; } - GNUNET_free (rdir); - return GNUNET_NO; - } if (!S_ISREG (filestat.st_mode)) - { - GNUNET_free (rdir); - return GNUNET_NO; - } + { + GNUNET_free (rdir); + return GNUNET_NO; + } if (ACCESS (rdir, R_OK) < 0) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir); - GNUNET_free (rdir); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir); + GNUNET_free (rdir); + return GNUNET_SYSERR; + } GNUNET_free (rdir); return GNUNET_YES; } @@ -547,56 +553,56 @@ GNUNET_DISK_directory_create (const char *dir) len = strlen (rdir); #ifndef MINGW - pos = 1; /* skip heading '/' */ + pos = 1; /* skip heading '/' */ #else /* Local or Network path? */ if (strncmp (rdir, "\\\\", 2) == 0) - { - pos = 2; - while (rdir[pos]) { - if (rdir[pos] == '\\') - { - pos++; - break; - } - pos++; + pos = 2; + while (rdir[pos]) + { + if (rdir[pos] == '\\') + { + pos++; + break; + } + pos++; + } } - } else - { - pos = 3; /* strlen("C:\\") */ - } + { + pos = 3; /* strlen("C:\\") */ + } #endif while (pos <= len) - { - if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) { - rdir[pos] = '\0'; - ret = GNUNET_DISK_directory_test (rdir); - if (ret == GNUNET_SYSERR) - { - GNUNET_free (rdir); - return GNUNET_SYSERR; - } - if (ret == GNUNET_NO) - { + if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) + { + rdir[pos] = '\0'; + ret = GNUNET_DISK_directory_test (rdir); + if (ret == GNUNET_SYSERR) + { + GNUNET_free (rdir); + return GNUNET_SYSERR; + } + if (ret == GNUNET_NO) + { #ifndef MINGW - ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ + ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ #else - ret = mkdir (rdir); + ret = mkdir (rdir); #endif - if ((ret != 0) && (errno != EEXIST)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); - GNUNET_free (rdir); - return GNUNET_SYSERR; - } - } - rdir[pos] = DIR_SEPARATOR; + if ((ret != 0) && (errno != EEXIST)) + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); + GNUNET_free (rdir); + return GNUNET_SYSERR; + } + } + rdir[pos] = DIR_SEPARATOR; + } + pos++; } - pos++; - } GNUNET_free (rdir); return GNUNET_OK; } @@ -643,37 +649,37 @@ GNUNET_DISK_directory_create_for_file (const char *filename) */ ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, - size_t len) + size_t len) { if (h == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #ifdef MINGW DWORD bytesRead; if (h->type != GNUNET_PIPE) - { - if (!ReadFile (h->h, result, len, &bytesRead, NULL)) { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; + if (!ReadFile (h->h, result, len, &bytesRead, NULL)) + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } } - } else - { - if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead)) { - if (GetLastError () != ERROR_IO_PENDING) - { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; - } + if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead)) + { + if (GetLastError () != ERROR_IO_PENDING) + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } + } + GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); } - GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); - } return bytesRead; #else return read (h->fd, result, len); @@ -695,7 +701,8 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len) struct GNUNET_DISK_FileHandle *fh; ssize_t ret; - fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); + fh = + GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); if (!fh) return GNUNET_SYSERR; ret = GNUNET_DISK_file_read (fh, result, len); @@ -714,46 +721,46 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len) */ ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, - const void *buffer, size_t n) + const void *buffer, size_t n) { if (h == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #ifdef MINGW DWORD bytesWritten; if (h->type != GNUNET_PIPE) - { - if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; + if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } } - } else - { + { #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write\n"); #endif - if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite)) - { - if (GetLastError () != ERROR_IO_PENDING) - { - SetErrnoFromWinError (GetLastError ()); + if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite)) + { + if (GetLastError () != ERROR_IO_PENDING) + { + SetErrnoFromWinError (GetLastError ()); #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n"); #endif - return GNUNET_SYSERR; - } - } + return GNUNET_SYSERR; + } + } #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); #endif - GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE); - } + GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE); + } return bytesWritten; #else return write (h->fd, buffer, n); @@ -772,14 +779,15 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, */ ssize_t GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n, - enum GNUNET_DISK_AccessPermissions mode) + enum GNUNET_DISK_AccessPermissions mode) { struct GNUNET_DISK_FileHandle *fh; ssize_t ret; fh = GNUNET_DISK_file_open (fn, - GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE - | GNUNET_DISK_OPEN_CREATE, mode); + GNUNET_DISK_OPEN_WRITE | + GNUNET_DISK_OPEN_TRUNCATE | + GNUNET_DISK_OPEN_CREATE, mode); if (!fh) return GNUNET_SYSERR; ret = GNUNET_DISK_file_write (fh, buffer, n); @@ -799,8 +807,8 @@ GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n, */ int GNUNET_DISK_directory_scan (const char *dirName, - GNUNET_FileNameCallback callback, - void *callback_cls) + GNUNET_FileNameCallback callback, + void *callback_cls) { DIR *dinfo; struct dirent *finfo; @@ -818,61 +826,61 @@ GNUNET_DISK_directory_scan (const char *dirName, while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) dname[strlen (dname) - 1] = '\0'; if (0 != STAT (dname, &istat)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname); - GNUNET_free (dname); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname); + GNUNET_free (dname); + return GNUNET_SYSERR; + } if (!S_ISDIR (istat.st_mode)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Expected `%s' to be a directory!\n"), dirName); - GNUNET_free (dname); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Expected `%s' to be a directory!\n"), dirName); + GNUNET_free (dname); + return GNUNET_SYSERR; + } errno = 0; dinfo = OPENDIR (dname); if ((errno == EACCES) || (dinfo == NULL)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); - if (dinfo != NULL) - closedir (dinfo); - GNUNET_free (dname); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); + if (dinfo != NULL) + closedir (dinfo); + GNUNET_free (dname); + return GNUNET_SYSERR; + } name_len = 256; n_size = strlen (dname) + name_len + 2; name = GNUNET_malloc (n_size); while ((finfo = readdir (dinfo)) != NULL) - { - if ((0 == strcmp (finfo->d_name, ".")) || - (0 == strcmp (finfo->d_name, ".."))) - continue; - if (callback != NULL) { - if (name_len < strlen (finfo->d_name)) - { - GNUNET_free (name); - name_len = strlen (finfo->d_name); - n_size = strlen (dname) + name_len + 2; - name = GNUNET_malloc (n_size); - } - /* dname can end in "/" only if dname == "/"; - * if dname does not end in "/", we need to add - * a "/" (otherwise, we must not!) */ - GNUNET_snprintf (name, n_size, "%s%s%s", dname, - (strcmp (dname, DIR_SEPARATOR_STR) == - 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); - if (GNUNET_OK != callback (callback_cls, name)) - { - closedir (dinfo); - GNUNET_free (name); - GNUNET_free (dname); - return GNUNET_SYSERR; - } + if ((0 == strcmp (finfo->d_name, ".")) || + (0 == strcmp (finfo->d_name, ".."))) + continue; + if (callback != NULL) + { + if (name_len < strlen (finfo->d_name)) + { + GNUNET_free (name); + name_len = strlen (finfo->d_name); + n_size = strlen (dname) + name_len + 2; + name = GNUNET_malloc (n_size); + } + /* dname can end in "/" only if dname == "/"; + * if dname does not end in "/", we need to add + * a "/" (otherwise, we must not!) */ + GNUNET_snprintf (name, n_size, "%s%s%s", dname, + (strcmp (dname, DIR_SEPARATOR_STR) == + 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); + if (GNUNET_OK != callback (callback_cls, name)) + { + closedir (dinfo); + GNUNET_free (name); + GNUNET_free (dname); + return GNUNET_SYSERR; + } + } + count++; } - count++; - } closedir (dinfo); GNUNET_free (name); GNUNET_free (dname); @@ -924,7 +932,7 @@ struct GNUNET_DISK_DirectoryIterator */ static void directory_iterator_task (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_DISK_DirectoryIterator *iter = cls; char *name; @@ -949,35 +957,35 @@ directory_iterator_task (void *cls, * GNUNET_SYSERR if abort was YES */ int -GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter, - int can) +GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator + *iter, int can) { struct dirent *finfo; GNUNET_assert (iter->next_name == NULL); if (can == GNUNET_YES) - { - closedir (iter->directory); - GNUNET_free (iter->dirname); - GNUNET_free (iter); - return GNUNET_SYSERR; - } + { + closedir (iter->directory); + GNUNET_free (iter->dirname); + GNUNET_free (iter); + return GNUNET_SYSERR; + } while (NULL != (finfo = readdir (iter->directory))) - { - if ((0 == strcmp (finfo->d_name, ".")) || - (0 == strcmp (finfo->d_name, ".."))) - continue; - GNUNET_asprintf (&iter->next_name, "%s%s%s", iter->dirname, - DIR_SEPARATOR_STR, finfo->d_name); - break; - } + { + if ((0 == strcmp (finfo->d_name, ".")) || + (0 == strcmp (finfo->d_name, ".."))) + continue; + GNUNET_asprintf (&iter->next_name, "%s%s%s", iter->dirname, + DIR_SEPARATOR_STR, finfo->d_name); + break; + } if (finfo == NULL) - { - GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES); - return GNUNET_NO; - } - GNUNET_SCHEDULER_add_with_priority (iter->priority, &directory_iterator_task, - iter); + { + GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES); + return GNUNET_NO; + } + GNUNET_SCHEDULER_add_with_priority (iter->priority, + &directory_iterator_task, iter); return GNUNET_YES; } @@ -995,9 +1003,9 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter, */ void GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio, - const char *dirName, - GNUNET_DISK_DirectoryIteratorCallback - callback, void *callback_cls) + const char *dirName, + GNUNET_DISK_DirectoryIteratorCallback + callback, void *callback_cls) { struct GNUNET_DISK_DirectoryIterator *di; @@ -1006,11 +1014,11 @@ GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio, di->callback_cls = callback_cls; di->directory = OPENDIR (dirName); if (di->directory == NULL) - { - GNUNET_free (di); - callback (callback_cls, NULL, NULL, NULL); - return; - } + { + GNUNET_free (di); + callback (callback_cls, NULL, NULL, NULL); + return; + } di->dirname = GNUNET_strdup (dirName); di->priority = prio; GNUNET_DISK_directory_iterator_next (di, GNUNET_NO); @@ -1047,7 +1055,7 @@ GNUNET_DISK_directory_remove (const char *fileName) struct stat istat; if (0 != LSTAT (fileName, &istat)) - return GNUNET_NO; /* file may not exist... */ + return GNUNET_NO; /* file may not exist... */ CHMOD (fileName, S_IWUSR | S_IRUSR | S_IXUSR); if (UNLINK (fileName) == 0) return GNUNET_OK; @@ -1056,18 +1064,18 @@ GNUNET_DISK_directory_remove (const char *fileName) * sticky /tmp directory may result in EPERM on BSD. * So we also explicitly check "isDirectory" */ (GNUNET_YES != GNUNET_DISK_directory_test (fileName))) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); + return GNUNET_SYSERR; + } if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL)) return GNUNET_SYSERR; if (0 != RMDIR (fileName)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -1093,34 +1101,34 @@ GNUNET_DISK_file_copy (const char *src, const char *dst) return GNUNET_SYSERR; pos = 0; in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, - GNUNET_DISK_PERM_NONE); + GNUNET_DISK_PERM_NONE); if (!in) return GNUNET_SYSERR; out = - GNUNET_DISK_file_open (dst, - GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | - GNUNET_DISK_OPEN_FAILIFEXISTS, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE | - GNUNET_DISK_PERM_GROUP_READ | - GNUNET_DISK_PERM_GROUP_WRITE); + GNUNET_DISK_file_open (dst, + GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | + GNUNET_DISK_OPEN_FAILIFEXISTS, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE | + GNUNET_DISK_PERM_GROUP_READ | + GNUNET_DISK_PERM_GROUP_WRITE); if (!out) - { - GNUNET_DISK_file_close (in); - return GNUNET_SYSERR; - } + { + GNUNET_DISK_file_close (in); + return GNUNET_SYSERR; + } buf = GNUNET_malloc (COPY_BLK_SIZE); while (pos < size) - { - len = COPY_BLK_SIZE; - if (len > size - pos) - len = size - pos; - if (len != GNUNET_DISK_file_read (in, buf, len)) - goto FAIL; - if (len != GNUNET_DISK_file_write (out, buf, len)) - goto FAIL; - pos += len; - } + { + len = COPY_BLK_SIZE; + if (len > size - pos) + len = size - pos; + if (len != GNUNET_DISK_file_read (in, buf, len)) + goto FAIL; + if (len != GNUNET_DISK_file_write (out, buf, len)) + goto FAIL; + pos += len; + } GNUNET_free (buf); GNUNET_DISK_file_close (in); GNUNET_DISK_file_close (out); @@ -1145,17 +1153,17 @@ GNUNET_DISK_filename_canonicalize (char *fn) idx = fn; while (*idx) - { - c = *idx; - - if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || - c == '<' || c == '>' || c == '|') { - *idx = '_'; - } + c = *idx; + + if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' + || c == '"' || c == '<' || c == '>' || c == '|') + { + *idx = '_'; + } - idx++; - } + idx++; + } } @@ -1175,14 +1183,14 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user) pws = getpwnam (user); if (pws == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Cannot obtain information about user `%s': %s\n"), user, - STRERROR (errno)); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Cannot obtain information about user `%s': %s\n"), user, + STRERROR (errno)); + return GNUNET_SYSERR; + } if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename); #endif return GNUNET_OK; } @@ -1198,13 +1206,13 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user) */ int GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart, - off_t lockEnd, int excl) + off_t lockEnd, int excl) { if (fh == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #ifndef MINGW struct flock fl; @@ -1223,12 +1231,13 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart, o.Offset = lockStart; if (!LockFileEx - (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY, - 0, lockEnd - lockStart, 0, &o)) - { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; - } + (fh->h, + (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY, 0, + lockEnd - lockStart, 0, &o)) + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } return GNUNET_OK; #endif @@ -1244,13 +1253,13 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart, */ int GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, - off_t unlockEnd) + off_t unlockEnd) { if (fh == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #ifndef MINGW struct flock fl; @@ -1269,10 +1278,10 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, o.Offset = unlockStart; if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o)) - { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; - } + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } return GNUNET_OK; #endif @@ -1293,7 +1302,7 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, */ struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, - enum GNUNET_DISK_AccessPermissions perm) + enum GNUNET_DISK_AccessPermissions perm) { char *expfn; struct GNUNET_DISK_FileHandle *ret; @@ -1314,17 +1323,17 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, #ifndef MINGW mode = 0; if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) - oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ + oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ else if (flags & GNUNET_DISK_OPEN_READ) oflags = O_RDONLY; else if (flags & GNUNET_DISK_OPEN_WRITE) oflags = O_WRONLY; else - { - GNUNET_break (0); - GNUNET_free (expfn); - return NULL; - } + { + GNUNET_break (0); + GNUNET_free (expfn); + return NULL; + } if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) oflags |= (O_CREAT | O_EXCL); if (flags & GNUNET_DISK_OPEN_TRUNCATE) @@ -1332,22 +1341,22 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, if (flags & GNUNET_DISK_OPEN_APPEND) oflags |= O_APPEND; if (flags & GNUNET_DISK_OPEN_CREATE) - { - (void) GNUNET_DISK_directory_create_for_file (expfn); - oflags |= O_CREAT; - mode = translate_unix_perms (perm); - } + { + (void) GNUNET_DISK_directory_create_for_file (expfn); + oflags |= O_CREAT; + mode = translate_unix_perms (perm); + } fd = open (expfn, oflags | O_LARGEFILE, mode); if (fd == -1) - { - if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn); - else - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_DEBUG, "open", expfn); - GNUNET_free (expfn); - return NULL; - } + { + if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn); + else + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn); + GNUNET_free (expfn); + return NULL; + } #else access = 0; disp = OPEN_ALWAYS; @@ -1360,48 +1369,48 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, access = FILE_WRITE_DATA; if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) - { - disp = CREATE_NEW; - } + { + disp = CREATE_NEW; + } else if (flags & GNUNET_DISK_OPEN_CREATE) - { - (void) GNUNET_DISK_directory_create_for_file (expfn); - if (flags & GNUNET_DISK_OPEN_TRUNCATE) - disp = CREATE_ALWAYS; - else - disp = OPEN_ALWAYS; - } + { + (void) GNUNET_DISK_directory_create_for_file (expfn); + if (flags & GNUNET_DISK_OPEN_TRUNCATE) + disp = CREATE_ALWAYS; + else + disp = OPEN_ALWAYS; + } else if (flags & GNUNET_DISK_OPEN_TRUNCATE) - { - disp = TRUNCATE_EXISTING; - } + { + disp = TRUNCATE_EXISTING; + } else - { - disp = OPEN_EXISTING; - } + { + disp = OPEN_EXISTING; + } /* TODO: access priviledges? */ h = CreateFile (expfn, access, - FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, - disp, FILE_ATTRIBUTE_NORMAL, NULL); + FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL); if (h == INVALID_HANDLE_VALUE) - { - SetErrnoFromWinError (GetLastError ()); - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn); - GNUNET_free (expfn); - return NULL; - } - - if (flags & GNUNET_DISK_OPEN_APPEND) - if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER) { SetErrnoFromWinError (GetLastError ()); - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", - expfn); - CloseHandle (h); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn); GNUNET_free (expfn); return NULL; } + + if (flags & GNUNET_DISK_OPEN_APPEND) + if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER) + { + SetErrnoFromWinError (GetLastError ()); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", + expfn); + CloseHandle (h); + GNUNET_free (expfn); + return NULL; + } #endif ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle)); @@ -1425,28 +1434,28 @@ int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) { if (h == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #if MINGW if (!CloseHandle (h->h)) - { - SetErrnoFromWinError (GetLastError ()); - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); - GNUNET_free (h->oOverlapRead); - GNUNET_free (h->oOverlapWrite); - GNUNET_free (h); - return GNUNET_SYSERR; - } + { + SetErrnoFromWinError (GetLastError ()); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); + GNUNET_free (h->oOverlapRead); + GNUNET_free (h->oOverlapWrite); + GNUNET_free (h); + return GNUNET_SYSERR; + } #else if (close (h->fd) != 0) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); - GNUNET_free (h); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); + GNUNET_free (h); + return GNUNET_SYSERR; + } #endif GNUNET_free (h); return GNUNET_OK; @@ -1469,7 +1478,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) */ char * GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *serviceName, ...) + const char *serviceName, ...) { const char *c; char *pfx; @@ -1478,44 +1487,45 @@ GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int needed; if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME", &pfx)) + GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME", + &pfx)) return NULL; if (pfx == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("No `%s' specified for service `%s' in configuration.\n"), - "HOME", serviceName); - return NULL; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("No `%s' specified for service `%s' in configuration.\n"), + "HOME", serviceName); + return NULL; + } needed = strlen (pfx) + 2; if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\')) needed++; va_start (ap, serviceName); while (1) - { - c = va_arg (ap, const char *); + { + c = va_arg (ap, const char *); - if (c == NULL) - break; - needed += strlen (c); - if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) - needed++; - } + if (c == NULL) + break; + needed += strlen (c); + if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) + needed++; + } va_end (ap); ret = GNUNET_malloc (needed); strcpy (ret, pfx); GNUNET_free (pfx); va_start (ap, serviceName); while (1) - { - c = va_arg (ap, const char *); + { + c = va_arg (ap, const char *); - if (c == NULL) - break; - if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) - strcat (ret, DIR_SEPARATOR_STR); - strcat (ret, c); - } + if (c == NULL) + break; + if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) + strcat (ret, DIR_SEPARATOR_STR); + strcat (ret, c); + } va_end (ap); if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\')) (void) GNUNET_DISK_directory_create_for_file (ret); @@ -1564,56 +1574,56 @@ struct GNUNET_DISK_MapHandle */ void * GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, - struct GNUNET_DISK_MapHandle **m, - enum GNUNET_DISK_MapType access, size_t len) + struct GNUNET_DISK_MapHandle **m, + enum GNUNET_DISK_MapType access, size_t len) { if (h == NULL) - { - errno = EINVAL; - return NULL; - } + { + errno = EINVAL; + return NULL; + } #ifdef MINGW DWORD mapAccess, protect; if ((access & GNUNET_DISK_MAP_TYPE_READ) && (access & GNUNET_DISK_MAP_TYPE_WRITE)) - { - protect = PAGE_READWRITE; - mapAccess = FILE_MAP_ALL_ACCESS; - } + { + protect = PAGE_READWRITE; + mapAccess = FILE_MAP_ALL_ACCESS; + } else if (access & GNUNET_DISK_MAP_TYPE_READ) - { - protect = PAGE_READONLY; - mapAccess = FILE_MAP_READ; - } + { + protect = PAGE_READONLY; + mapAccess = FILE_MAP_READ; + } else if (access & GNUNET_DISK_MAP_TYPE_WRITE) - { - protect = PAGE_READWRITE; - mapAccess = FILE_MAP_WRITE; - } + { + protect = PAGE_READWRITE; + mapAccess = FILE_MAP_WRITE; + } else - { - GNUNET_break (0); - return NULL; - } + { + GNUNET_break (0); + return NULL; + } *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle)); (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL); if ((*m)->h == INVALID_HANDLE_VALUE) - { - SetErrnoFromWinError (GetLastError ()); - GNUNET_free (*m); - return NULL; - } + { + SetErrnoFromWinError (GetLastError ()); + GNUNET_free (*m); + return NULL; + } (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); if (!(*m)->addr) - { - SetErrnoFromWinError (GetLastError ()); - CloseHandle ((*m)->h); - GNUNET_free (*m); - } + { + SetErrnoFromWinError (GetLastError ()); + CloseHandle ((*m)->h); + GNUNET_free (*m); + } return (*m)->addr; #else @@ -1628,10 +1638,10 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0); GNUNET_assert (NULL != (*m)->addr); if (MAP_FAILED == (*m)->addr) - { - GNUNET_free (*m); - return NULL; - } + { + GNUNET_free (*m); + return NULL; + } (*m)->len = len; return (*m)->addr; #endif @@ -1648,20 +1658,20 @@ GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h) int ret; if (h == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #ifdef MINGW ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; if (ret != GNUNET_OK) SetErrnoFromWinError (GetLastError ()); if (!CloseHandle (h->h) && (ret == GNUNET_OK)) - { - ret = GNUNET_SYSERR; - SetErrnoFromWinError (GetLastError ()); - } + { + ret = GNUNET_SYSERR; + SetErrnoFromWinError (GetLastError ()); + } #else ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; #endif @@ -1679,10 +1689,10 @@ int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) { if (h == NULL) - { - errno = EINVAL; - return GNUNET_SYSERR; - } + { + errno = EINVAL; + return GNUNET_SYSERR; + } #ifdef MINGW int ret; @@ -1711,8 +1721,8 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) unlike CreatePipe, which returns a bool for success or failure. */ static int create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, - LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize, - DWORD dwReadMode, DWORD dwWriteMode) + LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize, + DWORD dwReadMode, DWORD dwWriteMode) { /* Default to error. */ *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; @@ -1729,106 +1739,105 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, * Retrying will probably never be necessary, but we want * to be as robust as possible. */ while (1) - { - static volatile LONG pipe_unique_id; + { + static volatile LONG pipe_unique_id; - snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", - getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); + snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", + getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize); #endif - /* Use CreateNamedPipe instead of CreatePipe, because the latter - * returns a write handle that does not permit FILE_READ_ATTRIBUTES - * access, on versions of win32 earlier than WinXP SP2. - * CreatePipe also stupidly creates a full duplex pipe, which is - * a waste, since only a single direction is actually used. - * It's important to only allow a single instance, to ensure that - * the pipe was not created earlier by some other process, even if - * the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE - * because that is only available for Win2k SP2 and WinXP. */ - read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */ - psize, /* output buffer size */ - psize, /* input buffer size */ - NMPWAIT_USE_DEFAULT_WAIT, sa_ptr); - - if (read_pipe != INVALID_HANDLE_VALUE) - { + /* Use CreateNamedPipe instead of CreatePipe, because the latter + * returns a write handle that does not permit FILE_READ_ATTRIBUTES + * access, on versions of win32 earlier than WinXP SP2. + * CreatePipe also stupidly creates a full duplex pipe, which is + * a waste, since only a single direction is actually used. + * It's important to only allow a single instance, to ensure that + * the pipe was not created earlier by some other process, even if + * the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE + * because that is only available for Win2k SP2 and WinXP. */ + read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */ + psize, /* output buffer size */ + psize, /* input buffer size */ + NMPWAIT_USE_DEFAULT_WAIT, sa_ptr); + + if (read_pipe != INVALID_HANDLE_VALUE) + { #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", - read_pipe); + LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe); #endif - break; - } + break; + } - DWORD err = GetLastError (); + DWORD err = GetLastError (); - switch (err) - { - case ERROR_PIPE_BUSY: - /* The pipe is already open with compatible parameters. - * Pick a new name and retry. */ + switch (err) + { + case ERROR_PIPE_BUSY: + /* The pipe is already open with compatible parameters. + * Pick a new name and retry. */ #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); #endif - continue; - case ERROR_ACCESS_DENIED: - /* The pipe is already open with incompatible parameters. - * Pick a new name and retry. */ + continue; + case ERROR_ACCESS_DENIED: + /* The pipe is already open with incompatible parameters. + * Pick a new name and retry. */ #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); #endif - continue; - case ERROR_CALL_NOT_IMPLEMENTED: - /* We are on an older Win9x platform without named pipes. - * Return an anonymous pipe as the best approximation. */ + continue; + case ERROR_CALL_NOT_IMPLEMENTED: + /* We are on an older Win9x platform without named pipes. + * Return an anonymous pipe as the best approximation. */ #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "CreateNamedPipe not implemented, resorting to " - "CreatePipe: size = %lu\n", psize); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "CreateNamedPipe not implemented, resorting to " + "CreatePipe: size = %lu\n", psize); #endif - if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) - { + if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) + { #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", - *read_pipe_ptr); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", - *write_pipe_ptr); + LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", + *read_pipe_ptr); + LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", + *write_pipe_ptr); #endif - return GNUNET_OK; - } - err = GetLastError (); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); - return err; - default: - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); - return err; + return GNUNET_OK; + } + err = GetLastError (); + LOG (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); + return err; + default: + LOG (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); + return err; + } + /* NOTREACHED */ } - /* NOTREACHED */ - } #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); + LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); #endif /* Open the named pipe for writing. * Be sure to permit FILE_READ_ATTRIBUTES access. */ - write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */ - sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */ - 0); /* handle to template file */ + write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */ + sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */ + 0); /* handle to template file */ if (write_pipe == INVALID_HANDLE_VALUE) - { - /* Failure. */ - DWORD err = GetLastError (); + { + /* Failure. */ + DWORD err = GetLastError (); #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); + LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); #endif - CloseHandle (read_pipe); - return err; - } + CloseHandle (read_pipe); + return err; + } #if DEBUG_PIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); + LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); #endif /* Success. */ *read_pipe_ptr = read_pipe; @@ -1853,7 +1862,7 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) struct GNUNET_DISK_FileHandle *fds; p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + - 2 * sizeof (struct GNUNET_DISK_FileHandle)); + 2 * sizeof (struct GNUNET_DISK_FileHandle)); fds = (struct GNUNET_DISK_FileHandle *) &p[1]; p->fd[0] = &fds[0]; p->fd[1] = &fds[1]; @@ -1865,13 +1874,13 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) ret = pipe (fd); if (ret == -1) - { - eno = errno; - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); - GNUNET_free (p); - errno = eno; - return NULL; - } + { + eno = errno; + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe"); + GNUNET_free (p); + errno = eno; + return NULL; + } p->fd[0]->fd = fd[0]; p->fd[1]->fd = fd[1]; ret = 0; @@ -1895,62 +1904,62 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) if (0 > fcntl (fd[1], F_SETFD, flags)) ret = -1; if (ret == -1) - { - eno = errno; - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fcntl"); - GNUNET_break (0 == close (p->fd[0]->fd)); - GNUNET_break (0 == close (p->fd[1]->fd)); - GNUNET_free (p); - errno = eno; - return NULL; - } + { + eno = errno; + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl"); + GNUNET_break (0 == close (p->fd[0]->fd)); + GNUNET_break (0 == close (p->fd[1]->fd)); + GNUNET_free (p); + errno = eno; + return NULL; + } #else BOOL ret; HANDLE tmp_handle; ret = - create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, - FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED); + create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, + FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED); if (!ret) - { - GNUNET_free (p); - SetErrnoFromWinError (GetLastError ()); - return NULL; - } + { + GNUNET_free (p); + SetErrnoFromWinError (GetLastError ()); + return NULL; + } if (!DuplicateHandle - (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 0, - inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) - { - SetErrnoFromWinError (GetLastError ()); - CloseHandle (p->fd[0]->h); - CloseHandle (p->fd[1]->h); - GNUNET_free (p); - return NULL; - } + (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, + 0, inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) + { + SetErrnoFromWinError (GetLastError ()); + CloseHandle (p->fd[0]->h); + CloseHandle (p->fd[1]->h); + GNUNET_free (p); + return NULL; + } CloseHandle (p->fd[0]->h); p->fd[0]->h = tmp_handle; if (!DuplicateHandle - (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 0, - inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) - { - SetErrnoFromWinError (GetLastError ()); - CloseHandle (p->fd[0]->h); - CloseHandle (p->fd[1]->h); - GNUNET_free (p); - return NULL; - } + (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, + 0, inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) + { + SetErrnoFromWinError (GetLastError ()); + CloseHandle (p->fd[0]->h); + CloseHandle (p->fd[1]->h); + GNUNET_free (p); + return NULL; + } CloseHandle (p->fd[1]->h); p->fd[1]->h = tmp_handle; if (!blocking) - { - DWORD mode; - - mode = PIPE_NOWAIT; - SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); - SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); - /* this always fails on Windows 95, so we don't care about error handling */ - } + { + DWORD mode; + + mode = PIPE_NOWAIT; + SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); + SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); + /* this always fails on Windows 95, so we don't care about error handling */ + } p->fd[0]->type = GNUNET_PIPE; p->fd[1]->type = GNUNET_PIPE; @@ -1979,51 +1988,51 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) */ int GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, - enum GNUNET_DISK_PipeEnd end) + enum GNUNET_DISK_PipeEnd end) { int ret = GNUNET_OK; int save; #ifdef MINGW if (end == GNUNET_DISK_PIPE_END_READ) - { - if (!CloseHandle (p->fd[0]->h)) { - SetErrnoFromWinError (GetLastError ()); - ret = GNUNET_SYSERR; + if (!CloseHandle (p->fd[0]->h)) + { + SetErrnoFromWinError (GetLastError ()); + ret = GNUNET_SYSERR; + } + p->fd[0]->h = INVALID_HANDLE_VALUE; } - p->fd[0]->h = INVALID_HANDLE_VALUE; - } else if (end == GNUNET_DISK_PIPE_END_WRITE) - { - if (!CloseHandle (p->fd[1]->h)) { - SetErrnoFromWinError (GetLastError ()); - ret = GNUNET_SYSERR; + if (!CloseHandle (p->fd[1]->h)) + { + SetErrnoFromWinError (GetLastError ()); + ret = GNUNET_SYSERR; + } + p->fd[1]->h = INVALID_HANDLE_VALUE; } - p->fd[1]->h = INVALID_HANDLE_VALUE; - } save = errno; #else save = 0; if (end == GNUNET_DISK_PIPE_END_READ) - { - if (0 != close (p->fd[0]->fd)) { - ret = GNUNET_SYSERR; - save = errno; + if (0 != close (p->fd[0]->fd)) + { + ret = GNUNET_SYSERR; + save = errno; + } + p->fd[0]->fd = -1; } - p->fd[0]->fd = -1; - } else if (end == GNUNET_DISK_PIPE_END_WRITE) - { - if (0 != close (p->fd[1]->fd)) { - ret = GNUNET_SYSERR; - save = errno; + if (0 != close (p->fd[1]->fd)) + { + ret = GNUNET_SYSERR; + save = errno; + } + p->fd[1]->fd = -1; } - p->fd[1]->fd = -1; - } #endif errno = save; return ret; @@ -2043,35 +2052,35 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) #ifdef MINGW if (!CloseHandle (p->fd[0]->h)) - { - SetErrnoFromWinError (GetLastError ()); - ret = GNUNET_SYSERR; - } + { + SetErrnoFromWinError (GetLastError ()); + ret = GNUNET_SYSERR; + } if (!CloseHandle (p->fd[1]->h)) - { - SetErrnoFromWinError (GetLastError ()); - ret = GNUNET_SYSERR; - } + { + SetErrnoFromWinError (GetLastError ()); + ret = GNUNET_SYSERR; + } save = errno; #else save = 0; if (p->fd[0]->fd != -1) - { - if (0 != close (p->fd[0]->fd)) { - ret = GNUNET_SYSERR; - save = errno; + if (0 != close (p->fd[0]->fd)) + { + ret = GNUNET_SYSERR; + save = errno; + } } - } if (p->fd[1]->fd != -1) - { - if (0 != close (p->fd[1]->fd)) { - ret = GNUNET_SYSERR; - save = errno; + if (0 != close (p->fd[1]->fd)) + { + ret = GNUNET_SYSERR; + save = errno; + } } - } #endif GNUNET_free (p); errno = save; @@ -2088,7 +2097,7 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) */ struct GNUNET_DISK_FileHandle * GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags, - enum GNUNET_DISK_AccessPermissions perm) + enum GNUNET_DISK_AccessPermissions perm) { #ifdef MINGW struct GNUNET_DISK_FileHandle *ret; @@ -2108,70 +2117,71 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags, openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE; while (h == NULL) - { - DWORD error_code; - - name = NULL; - if (*fn != NULL) { - GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn); + DWORD error_code; + + name = NULL; + if (*fn != NULL) + { + GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn); #if DEBUG_NPIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Trying to create an instance of named pipe `%s'\n", name); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Trying to create an instance of named pipe `%s'\n", name); #endif - h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, - NULL); - } - else - { - GNUNET_asprintf (fn, "\\\\.\\pipe\\gnunet-%llu", - GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, - UINT64_MAX)); + h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, + 0, NULL); + } + else + { + GNUNET_asprintf (fn, "\\\\.\\pipe\\gnunet-%llu", + GNUNET_CRYPTO_random_u64 + (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX)); #if DEBUG_NPIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Trying to create unique named pipe `%s'\n", *fn); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Trying to create unique named pipe `%s'\n", *fn); #endif - h = CreateNamedPipe (*fn, - openMode | FILE_FLAG_OVERLAPPED | - FILE_FLAG_FIRST_PIPE_INSTANCE, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, - NULL); - } - error_code = GetLastError (); - if (name) - GNUNET_free (name); - /* don't re-set name to NULL yet */ - if (h == INVALID_HANDLE_VALUE) - { - SetErrnoFromWinError (error_code); + h = CreateNamedPipe (*fn, + openMode | FILE_FLAG_OVERLAPPED | + FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, + 0, NULL); + } + error_code = GetLastError (); + if (name) + GNUNET_free (name); + /* don't re-set name to NULL yet */ + if (h == INVALID_HANDLE_VALUE) + { + SetErrnoFromWinError (error_code); #if DEBUG_NPIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Pipe creation have failed because of %d, errno is %d\n", - error_code, errno); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Pipe creation have failed because of %d, errno is %d\n", + error_code, errno); #endif - if (name == NULL) - { + if (name == NULL) + { #if DEBUG_NPIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Pipe was to be unique, considering re-creation\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Pipe was to be unique, considering re-creation\n"); #endif - GNUNET_free (*fn); - *fn = NULL; - if (error_code != ERROR_ACCESS_DENIED && error_code != ERROR_PIPE_BUSY) - { - return NULL; - } + GNUNET_free (*fn); + *fn = NULL; + if (error_code != ERROR_ACCESS_DENIED + && error_code != ERROR_PIPE_BUSY) + { + return NULL; + } #if DEBUG_NPIPE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Pipe name was not unique, trying again\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Pipe name was not unique, trying again\n"); #endif - h = NULL; - } - else - return NULL; + h = NULL; + } + else + return NULL; + } } - } errno = 0; ret = GNUNET_malloc (sizeof (*ret)); @@ -2187,22 +2197,22 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags, return ret; #else if (*fn == NULL) - { - char dir[] = "/tmp/gnunet-pipe-XXXXXX"; - - if (mkdtemp (dir) == NULL) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "mkdtemp"); - return NULL; + char dir[] = "/tmp/gnunet-pipe-XXXXXX"; + + if (mkdtemp (dir) == NULL) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "mkdtemp"); + return NULL; + } + GNUNET_asprintf (fn, "%s/child-control", dir); } - GNUNET_asprintf (fn, "%s/child-control", dir); - } if (mkfifo (*fn, translate_unix_perms (perm)) == -1) - { - if ((errno != EEXIST) || (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))) - return NULL; - } + { + if ((errno != EEXIST) || (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))) + return NULL; + } flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS); return GNUNET_DISK_file_open (*fn, flags, perm); @@ -2220,7 +2230,7 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags, */ struct GNUNET_DISK_FileHandle * GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, - enum GNUNET_DISK_AccessPermissions perm) + enum GNUNET_DISK_AccessPermissions perm) { #ifdef MINGW struct GNUNET_DISK_FileHandle *ret; @@ -2236,12 +2246,12 @@ GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, openMode = GENERIC_WRITE; h = CreateFile (fn, openMode, 0, NULL, OPEN_EXISTING, - FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL); + FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL); if (h == INVALID_HANDLE_VALUE) - { - SetErrnoFromWinError (GetLastError ()); - return NULL; - } + { + SetErrnoFromWinError (GetLastError ()); + return NULL; + } ret = GNUNET_malloc (sizeof (*ret)); ret->h = h; @@ -2273,10 +2283,10 @@ GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe) ret = CloseHandle (pipe->h); if (!ret) - { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; - } + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } else return GNUNET_OK; #endif @@ -2292,17 +2302,17 @@ GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe) */ const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, - enum GNUNET_DISK_PipeEnd n) + enum GNUNET_DISK_PipeEnd n) { switch (n) - { - case GNUNET_DISK_PIPE_END_READ: - case GNUNET_DISK_PIPE_END_WRITE: - return p->fd[n]; - default: - GNUNET_break (0); - return NULL; - } + { + case GNUNET_DISK_PIPE_END_READ: + case GNUNET_DISK_PIPE_END_WRITE: + return p->fd[n]; + default: + GNUNET_break (0); + return NULL; + } } @@ -2316,7 +2326,7 @@ GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, */ int GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, - void *dst, size_t dst_len) + void *dst, size_t dst_len) { #ifdef MINGW if (dst_len < sizeof (HANDLE)) diff --git a/src/util/getopt.c b/src/util/getopt.c index d48183b2a..4a36678b2 100644 --- a/src/util/getopt.c +++ b/src/util/getopt.c @@ -49,6 +49,10 @@ Copyright (C) 2006 Christian Grothoff #endif #endif +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + #if defined (WIN32) && !defined (__CYGWIN32__) /* It's not Unix, really. See? Capital letters. */ #include @@ -194,20 +198,19 @@ static char *posixly_correct; /* Avoid depending on library functions or files whose names are inconsistent. */ -char * -getenv (); +char *getenv (); static char * my_index (str, chr) - const char *str; - int chr; + const char *str; + int chr; { while (*str) - { - if (*str == chr) - return (char *) str; - str++; - } + { + if (*str == chr) + return (char *) str; + str++; + } return 0; } @@ -219,8 +222,7 @@ my_index (str, chr) #if !defined (__STDC__) || !__STDC__ /* gcc with -traditional declares the built-in strlen to return int, and has done so at least since version 2.4.5. -- rms. */ -extern int -strlen (const char *); +extern int strlen (const char *); #endif /* not __STDC__ */ #endif /* __GNUC__ */ @@ -254,7 +256,7 @@ extern pid_t __libc_pid; is valid for the getopt call we must make sure that the ARGV passed to getopt is that one passed to the process. */ static void - __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv) + __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv) { /* XXX This is no good solution. We should rather copy the args so * that we can compare them later. But we must not use malloc(3). */ @@ -285,13 +287,12 @@ text_set_element (__libc_subinit, store_args_and_env); the new indices of the non-options in ARGV after they are moved. */ #if defined (__STDC__) && __STDC__ -static void -exchange (char **); +static void exchange (char **); #endif static void exchange (argv) - char **argv; + char **argv; { int bottom = first_nonopt; int middle = last_nonopt; @@ -308,61 +309,61 @@ exchange (argv) * string can work normally. Our top argument must be in the range * of the string. */ if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) - { - /* We must extend the array. The user plays games with us and - * presents new arguments. */ - char *new_str = malloc (top + 1); - - if (new_str == NULL) - nonoption_flags_len = nonoption_flags_max_len = 0; - else { - memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); - memset (&new_str[nonoption_flags_max_len], '\0', - top + 1 - nonoption_flags_max_len); - nonoption_flags_max_len = top + 1; - __getopt_nonoption_flags = new_str; + /* We must extend the array. The user plays games with us and + * presents new arguments. */ + char *new_str = malloc (top + 1); + + if (new_str == NULL) + nonoption_flags_len = nonoption_flags_max_len = 0; + else + { + memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); + memset (&new_str[nonoption_flags_max_len], '\0', + top + 1 - nonoption_flags_max_len); + nonoption_flags_max_len = top + 1; + __getopt_nonoption_flags = new_str; + } } - } #endif while (top > middle && middle > bottom) - { - if (top - middle > middle - bottom) - { - /* Bottom segment is the short one. */ - int len = middle - bottom; - register int i; - - /* Swap it with the top part of the top segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[top - (middle - bottom) + i]; - argv[top - (middle - bottom) + i] = tem; - SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); - } - /* Exclude the moved bottom segment from further swapping. */ - top -= len; - } - else { - /* Top segment is the short one. */ - int len = top - middle; - register int i; - - /* Swap it with the bottom part of the bottom segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[middle + i]; - argv[middle + i] = tem; - SWAP_FLAGS (bottom + i, middle + i); - } - /* Exclude the moved top segment from further swapping. */ - bottom += len; + if (top - middle > middle - bottom) + { + /* Bottom segment is the short one. */ + int len = middle - bottom; + register int i; + + /* Swap it with the top part of the top segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[top - (middle - bottom) + i]; + argv[top - (middle - bottom) + i] = tem; + SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); + } + /* Exclude the moved bottom segment from further swapping. */ + top -= len; + } + else + { + /* Top segment is the short one. */ + int len = top - middle; + register int i; + + /* Swap it with the bottom part of the bottom segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[middle + i]; + argv[middle + i] = tem; + SWAP_FLAGS (bottom + i, middle + i); + } + /* Exclude the moved top segment from further swapping. */ + bottom += len; + } } - } /* Update records for the slots the non-options now occupy. */ @@ -373,14 +374,13 @@ exchange (argv) /* Initialize the internal data when the first call is made. */ #if defined (__STDC__) && __STDC__ -static const char * -_getopt_initialize (int, char *const *, const char *); +static const char *_getopt_initialize (int, char *const *, const char *); #endif static const char * _getopt_initialize (argc, argv, optstring) - int argc; - char *const *argv; - const char *optstring; + int argc; + char *const *argv; + const char *optstring; { /* Start processing options with ARGV-element 1 (since ARGV-element 0 * is the program name); the sequence of previously skipped @@ -395,48 +395,50 @@ _getopt_initialize (argc, argv, optstring) /* Determine how to handle the ordering of options and nonoptions. */ if (optstring[0] == '-') - { - ordering = RETURN_IN_ORDER; - ++optstring; - } + { + ordering = RETURN_IN_ORDER; + ++optstring; + } else if (optstring[0] == '+') - { - ordering = REQUIRE_ORDER; - ++optstring; - } + { + ordering = REQUIRE_ORDER; + ++optstring; + } else if (posixly_correct != NULL) ordering = REQUIRE_ORDER; else ordering = PERMUTE; #ifdef _LIBC - if (posixly_correct == NULL && argc == original_argc && argv == original_argv) - { - if (nonoption_flags_max_len == 0) + if (posixly_correct == NULL && argc == original_argc + && argv == original_argv) { - if (__getopt_nonoption_flags == NULL || - __getopt_nonoption_flags[0] == '\0') - nonoption_flags_max_len = -1; - else - { - const char *orig_str = __getopt_nonoption_flags; - int len = nonoption_flags_max_len = strlen (orig_str); - - if (nonoption_flags_max_len < argc) - nonoption_flags_max_len = argc; - __getopt_nonoption_flags = (char *) malloc (nonoption_flags_max_len); - if (__getopt_nonoption_flags == NULL) - nonoption_flags_max_len = -1; - else - { - memcpy (__getopt_nonoption_flags, orig_str, len); - memset (&__getopt_nonoption_flags[len], '\0', - nonoption_flags_max_len - len); - } - } + if (nonoption_flags_max_len == 0) + { + if (__getopt_nonoption_flags == NULL || + __getopt_nonoption_flags[0] == '\0') + nonoption_flags_max_len = -1; + else + { + const char *orig_str = __getopt_nonoption_flags; + int len = nonoption_flags_max_len = strlen (orig_str); + + if (nonoption_flags_max_len < argc) + nonoption_flags_max_len = argc; + __getopt_nonoption_flags = + (char *) malloc (nonoption_flags_max_len); + if (__getopt_nonoption_flags == NULL) + nonoption_flags_max_len = -1; + else + { + memcpy (__getopt_nonoption_flags, orig_str, len); + memset (&__getopt_nonoption_flags[len], '\0', + nonoption_flags_max_len - len); + } + } + } + nonoption_flags_len = nonoption_flags_max_len; } - nonoption_flags_len = nonoption_flags_max_len; - } else nonoption_flags_len = 0; #endif @@ -502,8 +504,8 @@ _getopt_initialize (argc, argv, optstring) static int GN_getopt_internal (int argc, char *const *argv, const char *optstring, - const struct GNoption *longopts, int *longind, - int long_only) + const struct GNoption *longopts, int *longind, + int long_only) { static int __getopt_initialized = 0; static int GNopterr = 1; @@ -511,12 +513,12 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, GNoptarg = NULL; if (GNoptind == 0 || !__getopt_initialized) - { - if (GNoptind == 0) - GNoptind = 1; /* Don't scan ARGV[0], the program name. */ - optstring = _getopt_initialize (argc, argv, optstring); - __getopt_initialized = 1; - } + { + if (GNoptind == 0) + GNoptind = 1; /* Don't scan ARGV[0], the program name. */ + optstring = _getopt_initialize (argc, argv, optstring); + __getopt_initialized = 1; + } /* Test whether ARGV[GNoptind] points to a non-option argument. * Either it does not have option syntax, or there is an environment flag @@ -531,81 +533,81 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, #endif if (nextchar == NULL || *nextchar == '\0') - { - /* Advance to the next ARGV-element. */ - - /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been - * moved back by the user (who may also have changed the arguments). */ - if (last_nonopt > GNoptind) - last_nonopt = GNoptind; - if (first_nonopt > GNoptind) - first_nonopt = GNoptind; - - if (ordering == PERMUTE) - { - /* If we have just processed some options following some non-options, - * exchange them so that the options come first. */ - - if (first_nonopt != last_nonopt && last_nonopt != GNoptind) - exchange ((char **) argv); - else if (last_nonopt != GNoptind) - first_nonopt = GNoptind; - - /* Skip any additional non-options - * and extend the range of non-options previously skipped. */ - - while (GNoptind < argc && NONOPTION_P) - GNoptind++; - last_nonopt = GNoptind; - } - - /* The special ARGV-element `--' means premature end of options. - * Skip it like a null option, - * then exchange with previous non-options as if it were an option, - * then skip everything else like a non-option. */ - if (GNoptind != argc && !strcmp (argv[GNoptind], "--")) { - GNoptind++; - - if (first_nonopt != last_nonopt && last_nonopt != GNoptind) - exchange ((char **) argv); - else if (first_nonopt == last_nonopt) - first_nonopt = GNoptind; - last_nonopt = argc; - - GNoptind = argc; + /* Advance to the next ARGV-element. */ + + /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been + * moved back by the user (who may also have changed the arguments). */ + if (last_nonopt > GNoptind) + last_nonopt = GNoptind; + if (first_nonopt > GNoptind) + first_nonopt = GNoptind; + + if (ordering == PERMUTE) + { + /* If we have just processed some options following some non-options, + * exchange them so that the options come first. */ + + if (first_nonopt != last_nonopt && last_nonopt != GNoptind) + exchange ((char **) argv); + else if (last_nonopt != GNoptind) + first_nonopt = GNoptind; + + /* Skip any additional non-options + * and extend the range of non-options previously skipped. */ + + while (GNoptind < argc && NONOPTION_P) + GNoptind++; + last_nonopt = GNoptind; + } + + /* The special ARGV-element `--' means premature end of options. + * Skip it like a null option, + * then exchange with previous non-options as if it were an option, + * then skip everything else like a non-option. */ + if (GNoptind != argc && !strcmp (argv[GNoptind], "--")) + { + GNoptind++; + + if (first_nonopt != last_nonopt && last_nonopt != GNoptind) + exchange ((char **) argv); + else if (first_nonopt == last_nonopt) + first_nonopt = GNoptind; + last_nonopt = argc; + + GNoptind = argc; + } + + /* If we have done all the ARGV-elements, stop the scan + * and back over any non-options that we skipped and permuted. */ + + if (GNoptind == argc) + { + /* Set the next-arg-index to point at the non-options + * that we previously skipped, so the caller will digest them. */ + if (first_nonopt != last_nonopt) + GNoptind = first_nonopt; + return -1; + } + + /* If we have come to a non-option and did not permute it, + * either stop the scan or describe it to the caller and pass it by. */ + + if (NONOPTION_P) + { + if (ordering == REQUIRE_ORDER) + return -1; + GNoptarg = argv[GNoptind++]; + return 1; + } + + /* We have found another option-ARGV-element. + * Skip the initial punctuation. */ + + nextchar = + (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-')); } - /* If we have done all the ARGV-elements, stop the scan - * and back over any non-options that we skipped and permuted. */ - - if (GNoptind == argc) - { - /* Set the next-arg-index to point at the non-options - * that we previously skipped, so the caller will digest them. */ - if (first_nonopt != last_nonopt) - GNoptind = first_nonopt; - return -1; - } - - /* If we have come to a non-option and did not permute it, - * either stop the scan or describe it to the caller and pass it by. */ - - if (NONOPTION_P) - { - if (ordering == REQUIRE_ORDER) - return -1; - GNoptarg = argv[GNoptind++]; - return 1; - } - - /* We have found another option-ARGV-element. - * Skip the initial punctuation. */ - - nextchar = - (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-')); - } - /* Decode the current option-ARGV-element. */ /* Check whether the ARGV-element is a long option. @@ -624,134 +626,138 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, if (longopts != NULL && (argv[GNoptind][1] == '-' || (long_only && - (argv[GNoptind][2] || !my_index (optstring, argv[GNoptind][1]))))) - { - char *nameend; - const struct GNoption *p; - const struct GNoption *pfound = NULL; - int exact = 0; - int ambig = 0; - int indfound = -1; - int option_index; - - for (nameend = nextchar; *nameend && *nameend != '='; nameend++) - /* Do nothing. */ ; - - /* Test all long options for either exact match - * or abbreviated matches. */ - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, nextchar, nameend - nextchar)) - { - if ((unsigned int) (nameend - nextchar) == - (unsigned int) strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else - /* Second or later nonexact match found. */ - ambig = 1; - } - - if (ambig && !exact) + (argv[GNoptind][2] || !my_index (optstring, argv[GNoptind][1]))))) { - if (GNopterr) - fprintf (stderr, _("%s: option `%s' is ambiguous\n"), argv[0], - argv[GNoptind]); - nextchar += strlen (nextchar); - GNoptind++; - return '?'; - } + char *nameend; + const struct GNoption *p; + const struct GNoption *pfound = NULL; + int exact = 0; + int ambig = 0; + int indfound = -1; + int option_index; - if (pfound != NULL) - { - option_index = indfound; - GNoptind++; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - * allow it to be used on enums. */ - if (pfound->has_arg) - GNoptarg = nameend + 1; - else - { - if (GNopterr) - { - if (argv[GNoptind - 1][1] == '-') - /* --option */ - fprintf (stderr, - _("%s: option `--%s' does not allow an argument\n"), - argv[0], pfound->name); - else - /* +option or -option */ - fprintf (stderr, - _("%s: option `%c%s' does not allow an argument\n"), - argv[0], argv[GNoptind - 1][0], pfound->name); - } - nextchar += strlen (nextchar); - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (GNoptind < argc) - { - GNoptarg = argv[GNoptind++]; - } - else - { - if (GNopterr) - { - fprintf (stderr, _("%s: option `%s' requires an argument\n"), - argv[0], argv[GNoptind - 1]); - } - nextchar += strlen (nextchar); - return (optstring[0] == ':') ? ':' : '?'; - } - } - nextchar += strlen (nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; - } + for (nameend = nextchar; *nameend && *nameend != '='; nameend++) + /* Do nothing. */ ; - /* Can't find it as a long option. If this is not getopt_long_only, - * or the option starts with '--' or is not a valid short - * option, then it's an error. - * Otherwise interpret it as a short option. */ - if (!long_only || argv[GNoptind][1] == '-' || - my_index (optstring, *nextchar) == NULL) - { - if (GNopterr) - { - if (argv[GNoptind][1] == '-') - /* --option */ - fprintf (stderr, _("%s: unrecognized option `--%s'\n"), argv[0], - nextchar); - else - /* +option or -option */ - fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), argv[0], - argv[GNoptind][0], nextchar); - } - nextchar = (char *) ""; - GNoptind++; - return '?'; + /* Test all long options for either exact match + * or abbreviated matches. */ + for (p = longopts, option_index = 0; p->name; p++, option_index++) + if (!strncmp (p->name, nextchar, nameend - nextchar)) + { + if ((unsigned int) (nameend - nextchar) == + (unsigned int) strlen (p->name)) + { + /* Exact match found. */ + pfound = p; + indfound = option_index; + exact = 1; + break; + } + else if (pfound == NULL) + { + /* First nonexact match found. */ + pfound = p; + indfound = option_index; + } + else + /* Second or later nonexact match found. */ + ambig = 1; + } + + if (ambig && !exact) + { + if (GNopterr) + fprintf (stderr, _("%s: option `%s' is ambiguous\n"), argv[0], + argv[GNoptind]); + nextchar += strlen (nextchar); + GNoptind++; + return '?'; + } + + if (pfound != NULL) + { + option_index = indfound; + GNoptind++; + if (*nameend) + { + /* Don't test has_arg with >, because some C compilers don't + * allow it to be used on enums. */ + if (pfound->has_arg) + GNoptarg = nameend + 1; + else + { + if (GNopterr) + { + if (argv[GNoptind - 1][1] == '-') + /* --option */ + fprintf (stderr, + _ + ("%s: option `--%s' does not allow an argument\n"), + argv[0], pfound->name); + else + /* +option or -option */ + fprintf (stderr, + _ + ("%s: option `%c%s' does not allow an argument\n"), + argv[0], argv[GNoptind - 1][0], + pfound->name); + } + nextchar += strlen (nextchar); + return '?'; + } + } + else if (pfound->has_arg == 1) + { + if (GNoptind < argc) + { + GNoptarg = argv[GNoptind++]; + } + else + { + if (GNopterr) + { + fprintf (stderr, + _("%s: option `%s' requires an argument\n"), + argv[0], argv[GNoptind - 1]); + } + nextchar += strlen (nextchar); + return (optstring[0] == ':') ? ':' : '?'; + } + } + nextchar += strlen (nextchar); + if (longind != NULL) + *longind = option_index; + if (pfound->flag) + { + *(pfound->flag) = pfound->val; + return 0; + } + return pfound->val; + } + + /* Can't find it as a long option. If this is not getopt_long_only, + * or the option starts with '--' or is not a valid short + * option, then it's an error. + * Otherwise interpret it as a short option. */ + if (!long_only || argv[GNoptind][1] == '-' || + my_index (optstring, *nextchar) == NULL) + { + if (GNopterr) + { + if (argv[GNoptind][1] == '-') + /* --option */ + fprintf (stderr, _("%s: unrecognized option `--%s'\n"), + argv[0], nextchar); + else + /* +option or -option */ + fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), + argv[0], argv[GNoptind][0], nextchar); + } + nextchar = (char *) ""; + GNoptind++; + return '?'; + } } - } /* Look at and handle the next short option-character. */ @@ -764,191 +770,193 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, ++GNoptind; if (temp == NULL || c == ':') - { - if (GNopterr) { - if (posixly_correct) - /* 1003.2 specifies the format of this message. */ - fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); - else - fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); + if (GNopterr) + { + if (posixly_correct) + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); + else + fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); + } + return '?'; } - return '?'; - } /* Convenience. Treat POSIX -W foo same as long option --foo */ if (temp[0] == 'W' && temp[1] == ';') - { - char *nameend; - const struct GNoption *p; - const struct GNoption *pfound = NULL; - int exact = 0; - int ambig = 0; - int indfound = 0; - int option_index; - - /* This is an option that requires an argument. */ - if (*nextchar != '\0') - { - GNoptarg = nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - * we must advance to the next element now. */ - GNoptind++; - } - else if (GNoptind == argc) { - if (GNopterr) - { - /* 1003.2 specifies the format of this message. */ - fprintf (stderr, _("%s: option requires an argument -- %c\n"), - argv[0], c); - } - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - return c; - } - else - /* We already incremented `GNoptind' once; - * increment it again when taking next ARGV-elt as argument. */ - GNoptarg = argv[GNoptind++]; - - /* GNoptarg is now the argument, see if it's in the - * table of longopts. */ - - for (nextchar = nameend = GNoptarg; *nameend && *nameend != '='; - nameend++) - /* Do nothing. */ ; - - /* Test all long options for either exact match - * or abbreviated matches. */ - if (longopts != NULL) - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, nextchar, nameend - nextchar)) - { - if ((unsigned int) (nameend - nextchar) == strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else - /* Second or later nonexact match found. */ - ambig = 1; - } - if (ambig && !exact) - { - if (GNopterr) - fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), argv[0], - argv[GNoptind]); - nextchar += strlen (nextchar); - GNoptind++; - return '?'; - } - if (pfound != NULL) - { - option_index = indfound; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - * allow it to be used on enums. */ - if (pfound->has_arg) - GNoptarg = nameend + 1; - else - { - if (GNopterr) - fprintf (stderr, _("\ + char *nameend; + const struct GNoption *p; + const struct GNoption *pfound = NULL; + int exact = 0; + int ambig = 0; + int indfound = 0; + int option_index; + + /* This is an option that requires an argument. */ + if (*nextchar != '\0') + { + GNoptarg = nextchar; + /* If we end this ARGV-element by taking the rest as an arg, + * we must advance to the next element now. */ + GNoptind++; + } + else if (GNoptind == argc) + { + if (GNopterr) + { + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, _("%s: option requires an argument -- %c\n"), + argv[0], c); + } + if (optstring[0] == ':') + c = ':'; + else + c = '?'; + return c; + } + else + /* We already incremented `GNoptind' once; + * increment it again when taking next ARGV-elt as argument. */ + GNoptarg = argv[GNoptind++]; + + /* GNoptarg is now the argument, see if it's in the + * table of longopts. */ + + for (nextchar = nameend = GNoptarg; *nameend && *nameend != '='; + nameend++) + /* Do nothing. */ ; + + /* Test all long options for either exact match + * or abbreviated matches. */ + if (longopts != NULL) + for (p = longopts, option_index = 0; p->name; p++, option_index++) + if (!strncmp (p->name, nextchar, nameend - nextchar)) + { + if ((unsigned int) (nameend - nextchar) == strlen (p->name)) + { + /* Exact match found. */ + pfound = p; + indfound = option_index; + exact = 1; + break; + } + else if (pfound == NULL) + { + /* First nonexact match found. */ + pfound = p; + indfound = option_index; + } + else + /* Second or later nonexact match found. */ + ambig = 1; + } + if (ambig && !exact) + { + if (GNopterr) + fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), + argv[0], argv[GNoptind]); + nextchar += strlen (nextchar); + GNoptind++; + return '?'; + } + if (pfound != NULL) + { + option_index = indfound; + if (*nameend) + { + /* Don't test has_arg with >, because some C compilers don't + * allow it to be used on enums. */ + if (pfound->has_arg) + GNoptarg = nameend + 1; + else + { + if (GNopterr) + fprintf (stderr, _("\ %s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name); - nextchar += strlen (nextchar); - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (GNoptind < argc) - GNoptarg = argv[GNoptind++]; - else - { - if (GNopterr) - fprintf (stderr, _("%s: option `%s' requires an argument\n"), - argv[0], argv[GNoptind - 1]); - nextchar += strlen (nextchar); - return optstring[0] == ':' ? ':' : '?'; - } - } - nextchar += strlen (nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; + nextchar += strlen (nextchar); + return '?'; + } + } + else if (pfound->has_arg == 1) + { + if (GNoptind < argc) + GNoptarg = argv[GNoptind++]; + else + { + if (GNopterr) + fprintf (stderr, + _("%s: option `%s' requires an argument\n"), + argv[0], argv[GNoptind - 1]); + nextchar += strlen (nextchar); + return optstring[0] == ':' ? ':' : '?'; + } + } + nextchar += strlen (nextchar); + if (longind != NULL) + *longind = option_index; + if (pfound->flag) + { + *(pfound->flag) = pfound->val; + return 0; + } + return pfound->val; + } + nextchar = NULL; + return 'W'; /* Let the application handle it. */ } - nextchar = NULL; - return 'W'; /* Let the application handle it. */ - } if (temp[1] == ':') - { - if (temp[2] == ':') { - /* This is an option that accepts an argument optionally. */ - if (*nextchar != '\0') - { - GNoptarg = nextchar; - GNoptind++; - } - else - GNoptarg = NULL; - nextchar = NULL; + if (temp[2] == ':') + { + /* This is an option that accepts an argument optionally. */ + if (*nextchar != '\0') + { + GNoptarg = nextchar; + GNoptind++; + } + else + GNoptarg = NULL; + nextchar = NULL; + } + else + { + /* This is an option that requires an argument. */ + if (*nextchar != '\0') + { + GNoptarg = nextchar; + /* If we end this ARGV-element by taking the rest as an arg, + * we must advance to the next element now. */ + GNoptind++; + } + else if (GNoptind == argc) + { + if (GNopterr) + { + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, + _("%s: option requires an argument -- %c\n"), + argv[0], c); + } + if (optstring[0] == ':') + c = ':'; + else + c = '?'; + } + else + /* We already incremented `GNoptind' once; + * increment it again when taking next ARGV-elt as argument. */ + GNoptarg = argv[GNoptind++]; + nextchar = NULL; + } } - else - { - /* This is an option that requires an argument. */ - if (*nextchar != '\0') - { - GNoptarg = nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - * we must advance to the next element now. */ - GNoptind++; - } - else if (GNoptind == argc) - { - if (GNopterr) - { - /* 1003.2 specifies the format of this message. */ - fprintf (stderr, _("%s: option requires an argument -- %c\n"), - argv[0], c); - } - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - } - else - /* We already incremented `GNoptind' once; - * increment it again when taking next ARGV-elt as argument. */ - GNoptarg = argv[GNoptind++]; - nextchar = NULL; - } - } return c; } } static int GNgetopt_long (int argc, char *const *argv, const char *options, - const struct GNoption *long_options, int *opt_index) + const struct GNoption *long_options, int *opt_index) { return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0); } @@ -967,8 +975,8 @@ GNgetopt_long (int argc, char *const *argv, const char *options, */ int GNUNET_GETOPT_run (const char *binaryOptions, - const struct GNUNET_GETOPT_CommandLineOption *allOptions, - unsigned int argc, char *const *argv) + const struct GNUNET_GETOPT_CommandLineOption *allOptions, + unsigned int argc, char *const *argv) { struct GNoption *long_options; struct GNUNET_GETOPT_CommandLineProcessorContext clpc; @@ -993,15 +1001,15 @@ GNUNET_GETOPT_run (const char *binaryOptions, shorts = GNUNET_malloc (count * 2 + 1); spos = 0; for (i = 0; i < count; i++) - { - long_options[i].name = allOptions[i].name; - long_options[i].has_arg = allOptions[i].require_argument; - long_options[i].flag = NULL; - long_options[i].val = allOptions[i].shortName; - shorts[spos++] = allOptions[i].shortName; - if (allOptions[i].require_argument != 0) - shorts[spos++] = ':'; - } + { + long_options[i].name = allOptions[i].name; + long_options[i].has_arg = allOptions[i].require_argument; + long_options[i].flag = NULL; + long_options[i].val = allOptions[i].shortName; + shorts[spos++] = allOptions[i].shortName; + if (allOptions[i].require_argument != 0) + shorts[spos++] = ':'; + } long_options[count].name = NULL; long_options[count].has_arg = 0; long_options[count].flag = NULL; @@ -1010,31 +1018,31 @@ GNUNET_GETOPT_run (const char *binaryOptions, cont = GNUNET_OK; /* main getopt loop */ while (cont == GNUNET_OK) - { - int option_index = 0; - - c = GNgetopt_long (argc, argv, shorts, long_options, &option_index); - - if (c == GNUNET_SYSERR) - break; /* No more flags to process */ - - for (i = 0; i < count; i++) - { - clpc.currentArgument = GNoptind - 1; - if ((char) c == allOptions[i].shortName) - { - cont = - allOptions[i].processor (&clpc, allOptions[i].scls, - allOptions[i].name, GNoptarg); - break; - } - } - if (i == count) { - fprintf (stderr, _("Use --help to get a list of options.\n")); - cont = GNUNET_SYSERR; + int option_index = 0; + + c = GNgetopt_long (argc, argv, shorts, long_options, &option_index); + + if (c == GNUNET_SYSERR) + break; /* No more flags to process */ + + for (i = 0; i < count; i++) + { + clpc.currentArgument = GNoptind - 1; + if ((char) c == allOptions[i].shortName) + { + cont = + allOptions[i].processor (&clpc, allOptions[i].scls, + allOptions[i].name, GNoptarg); + break; + } + } + if (i == count) + { + fprintf (stderr, _("Use --help to get a list of options.\n")); + cont = GNUNET_SYSERR; + } } - } GNUNET_free (shorts); GNUNET_free (long_options); diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index f00c27f29..8564d591f 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c @@ -28,6 +28,8 @@ #include "gnunet_common.h" #include "gnunet_getopt_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Print out program version (implements --version). @@ -40,8 +42,8 @@ */ int GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext - *ctx, void *scls, const char *option, - const char *value) + *ctx, void *scls, const char *option, + const char *value) { const char *version = scls; @@ -64,8 +66,8 @@ GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext */ int GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext - *ctx, void *scls, const char *option, - const char *value) + *ctx, void *scls, const char *option, + const char *value) { const char *about = scls; size_t slen; @@ -79,74 +81,74 @@ GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext printf ("%s\n%s\n", ctx->binaryOptions, gettext (about)); printf (_ - ("Arguments mandatory for long options are also mandatory for short options.\n")); + ("Arguments mandatory for long options are also mandatory for short options.\n")); i = 0; opt = ctx->allOptions; while (opt[i].description != NULL) - { - if (opt[i].shortName == '\0') - printf (" "); - else - printf (" -%c, ", opt[i].shortName); - printf ("--%s", opt[i].name); - slen = 8 + strlen (opt[i].name); - if (opt[i].argumentHelp != NULL) - { - printf ("=%s", opt[i].argumentHelp); - slen += 1 + strlen (opt[i].argumentHelp); - } - if (slen > BORDER) { - printf ("\n%*s", BORDER, ""); - slen = BORDER; + if (opt[i].shortName == '\0') + printf (" "); + else + printf (" -%c, ", opt[i].shortName); + printf ("--%s", opt[i].name); + slen = 8 + strlen (opt[i].name); + if (opt[i].argumentHelp != NULL) + { + printf ("=%s", opt[i].argumentHelp); + slen += 1 + strlen (opt[i].argumentHelp); + } + if (slen > BORDER) + { + printf ("\n%*s", BORDER, ""); + slen = BORDER; + } + if (slen < BORDER) + { + printf ("%*s", (int) (BORDER - slen), ""); + slen = BORDER; + } + if (0 < strlen (opt[i].description)) + trans = gettext (opt[i].description); + else + trans = ""; + ml = strlen (trans); + p = 0; + OUTER: + while (ml - p > 78 - slen) + { + for (j = p + 78 - slen; j > p; j--) + { + if (isspace ((unsigned char) trans[j])) + { + scp = GNUNET_malloc (j - p + 1); + memcpy (scp, &trans[p], j - p); + scp[j - p] = '\0'; + printf ("%s\n%*s", scp, BORDER + 2, ""); + GNUNET_free (scp); + p = j + 1; + slen = BORDER + 2; + goto OUTER; + } + } + /* could not find space to break line */ + scp = GNUNET_malloc (78 - slen + 1); + memcpy (scp, &trans[p], 78 - slen); + scp[78 - slen] = '\0'; + printf ("%s\n%*s", scp, BORDER + 2, ""); + GNUNET_free (scp); + slen = BORDER + 2; + p = p + 78 - slen; + } + /* print rest */ + if (p < ml) + printf ("%s\n", &trans[p]); + if (strlen (trans) == 0) + printf ("\n"); + i++; } - if (slen < BORDER) - { - printf ("%*s", (int) (BORDER - slen), ""); - slen = BORDER; - } - if (0 < strlen (opt[i].description)) - trans = gettext (opt[i].description); - else - trans = ""; - ml = strlen (trans); - p = 0; -OUTER: - while (ml - p > 78 - slen) - { - for (j = p + 78 - slen; j > p; j--) - { - if (isspace ((unsigned char) trans[j])) - { - scp = GNUNET_malloc (j - p + 1); - memcpy (scp, &trans[p], j - p); - scp[j - p] = '\0'; - printf ("%s\n%*s", scp, BORDER + 2, ""); - GNUNET_free (scp); - p = j + 1; - slen = BORDER + 2; - goto OUTER; - } - } - /* could not find space to break line */ - scp = GNUNET_malloc (78 - slen + 1); - memcpy (scp, &trans[p], 78 - slen); - scp[78 - slen] = '\0'; - printf ("%s\n%*s", scp, BORDER + 2, ""); - GNUNET_free (scp); - slen = BORDER + 2; - p = p + 78 - slen; - } - /* print rest */ - if (p < ml) - printf ("%s\n", &trans[p]); - if (strlen (trans) == 0) - printf ("\n"); - i++; - } printf ("Report bugs to gnunet-developers@gnu.org.\n" - "GNUnet home page: http://www.gnu.org/software/gnunet/\n" - "General help using GNU software: http://www.gnu.org/gethelp/\n"); + "GNUnet home page: http://www.gnu.org/software/gnunet/\n" + "General help using GNU software: http://www.gnu.org/gethelp/\n"); return GNUNET_SYSERR; } @@ -166,9 +168,10 @@ OUTER: * @return GNUNET_OK */ int -GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext - *ctx, void *scls, const char *option, - const char *value) +GNUNET_GETOPT_increment_value (struct + GNUNET_GETOPT_CommandLineProcessorContext *ctx, + void *scls, const char *option, + const char *value) { int *val = scls; @@ -193,7 +196,7 @@ GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext */ int GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, - void *scls, const char *option, const char *value) + void *scls, const char *option, const char *value) { int *val = scls; @@ -217,8 +220,9 @@ GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, * @return GNUNET_OK */ int -GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, - void *scls, const char *option, const char *value) +GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext + *ctx, void *scls, const char *option, + const char *value) { char **val = scls; @@ -243,16 +247,18 @@ GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, * @return GNUNET_OK if parsing the value worked */ int -GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, - void *scls, const char *option, const char *value) +GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext + *ctx, void *scls, const char *option, + const char *value) { unsigned long long *val = scls; if (1 != SSCANF (value, "%llu", val)) - { - fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option); - return GNUNET_SYSERR; - } + { + fprintf (stderr, _("You must pass a number to the `%s' option.\n"), + option); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -272,15 +278,16 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, */ int GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, - void *scls, const char *option, const char *value) + void *scls, const char *option, const char *value) { unsigned int *val = scls; if (1 != SSCANF (value, "%u", val)) - { - fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option); - return GNUNET_SYSERR; - } + { + fprintf (stderr, _("You must pass a number to the `%s' option.\n"), + option); + return GNUNET_SYSERR; + } return GNUNET_OK; } diff --git a/src/util/gnunet-config-diff.c b/src/util/gnunet-config-diff.c index 207b9518a..992480a30 100644 --- a/src/util/gnunet-config-diff.c +++ b/src/util/gnunet-config-diff.c @@ -8,10 +8,10 @@ main (int argc, char **argv) struct GNUNET_CONFIGURATION_Handle *i2; if (argc != 3) - { - fprintf (stderr, "Invoke using `%s DEFAULTS-IN DIFFS'\n", argv[0]); - return 1; - } + { + fprintf (stderr, "Invoke using `%s DEFAULTS-IN DIFFS'\n", argv[0]); + return 1; + } i1 = GNUNET_CONFIGURATION_create (); i2 = GNUNET_CONFIGURATION_create (); if ((GNUNET_OK != GNUNET_CONFIGURATION_load (i1, argv[1])) || diff --git a/src/util/gnunet-resolver.c b/src/util/gnunet-resolver.c index 131693dd1..e97793114 100644 --- a/src/util/gnunet-resolver.c +++ b/src/util/gnunet-resolver.c @@ -72,9 +72,9 @@ main (int argc, char *const *argv) GNUNET_GETOPT_OPTION_END }; return (GNUNET_OK == - GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]", - gettext_noop ("Test GNUnet DNS resolver code."), - options, &run, NULL)) ? 0 : 1; + GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]", + gettext_noop ("Test GNUnet DNS resolver code."), + options, &run, NULL)) ? 0 : 1; } /* end of gnunet-resolver.c */ diff --git a/src/util/gnunet-service-resolver.c b/src/util/gnunet-service-resolver.c index 5d4c5c224..c15f0d3f5 100644 --- a/src/util/gnunet-service-resolver.c +++ b/src/util/gnunet-service-resolver.c @@ -89,7 +89,7 @@ getnameinfo_resolve (struct IPCache *cache) if (0 == getnameinfo (cache->sa, cache->salen, hostname, sizeof (hostname), NULL, - 0, 0)) + 0, 0)) cache->addr = GNUNET_strdup (hostname); } #endif @@ -107,20 +107,20 @@ gethostbyaddr_resolve (struct IPCache *cache) struct hostent *ent; switch (cache->sa->sa_family) - { - case AF_INET: - ent = - gethostbyaddr (&((struct sockaddr_in *) cache->sa)->sin_addr, - sizeof (struct in_addr), AF_INET); - break; - case AF_INET6: - ent = - gethostbyaddr (&((struct sockaddr_in6 *) cache->sa)->sin6_addr, - sizeof (struct in6_addr), AF_INET6); - break; - default: - ent = NULL; - } + { + case AF_INET: + ent = + gethostbyaddr (&((struct sockaddr_in *) cache->sa)->sin_addr, + sizeof (struct in_addr), AF_INET); + break; + case AF_INET6: + ent = + gethostbyaddr (&((struct sockaddr_in6 *) cache->sa)->sin6_addr, + sizeof (struct in6_addr), AF_INET6); + break; + default: + ent = NULL; + } if (ent != NULL) cache->addr = GNUNET_strdup (ent->h_name); } @@ -158,7 +158,7 @@ cache_resolve (struct IPCache *cache) */ static void get_ip_as_string (struct GNUNET_SERVER_Client *client, - const struct sockaddr *sa, socklen_t salen) + const struct sockaddr *sa, socklen_t salen) { struct IPCache *cache; struct IPCache *prev; @@ -166,73 +166,73 @@ get_ip_as_string (struct GNUNET_SERVER_Client *client, struct GNUNET_SERVER_TransmitContext *tc; if (salen < sizeof (struct sockaddr)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } now = GNUNET_TIME_absolute_get (); cache = head; prev = NULL; while ((cache != NULL) && - ((cache->salen != salen) || (0 != memcmp (cache->sa, sa, salen)))) - { - if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value < - 60 * 60 * 1000) + ((cache->salen != salen) || (0 != memcmp (cache->sa, sa, salen)))) { - if (prev != NULL) - { - prev->next = cache->next; - GNUNET_free_non_null (cache->addr); - GNUNET_free (cache->sa); - GNUNET_free (cache); - cache = prev->next; - } - else - { - head = cache->next; - GNUNET_free_non_null (cache->addr); - GNUNET_free (cache->sa); - GNUNET_free (cache); - cache = head; - } - continue; + if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value < + 60 * 60 * 1000) + { + if (prev != NULL) + { + prev->next = cache->next; + GNUNET_free_non_null (cache->addr); + GNUNET_free (cache->sa); + GNUNET_free (cache); + cache = prev->next; + } + else + { + head = cache->next; + GNUNET_free_non_null (cache->addr); + GNUNET_free (cache->sa); + GNUNET_free (cache); + cache = head; + } + continue; + } + prev = cache; + cache = cache->next; } - prev = cache; - cache = cache->next; - } if (cache != NULL) - { - cache->last_request = now; - if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value < - 60 * 60 * 1000) { - GNUNET_free_non_null (cache->addr); + cache->last_request = now; + if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value < + 60 * 60 * 1000) + { + GNUNET_free_non_null (cache->addr); + cache->addr = NULL; + cache->salen = 0; + cache_resolve (cache); + } + } + else + { + cache = GNUNET_malloc (sizeof (struct IPCache)); + cache->next = head; + cache->salen = salen; + cache->sa = GNUNET_malloc (salen); + memcpy (cache->sa, sa, salen); + cache->last_request = GNUNET_TIME_absolute_get (); + cache->last_refresh = GNUNET_TIME_absolute_get (); cache->addr = NULL; - cache->salen = 0; cache_resolve (cache); + head = cache; } - } - else - { - cache = GNUNET_malloc (sizeof (struct IPCache)); - cache->next = head; - cache->salen = salen; - cache->sa = GNUNET_malloc (salen); - memcpy (cache->sa, sa, salen); - cache->last_request = GNUNET_TIME_absolute_get (); - cache->last_refresh = GNUNET_TIME_absolute_get (); - cache->addr = NULL; - cache_resolve (cache); - head = cache; - } tc = GNUNET_SERVER_transmit_context_create (client); if (cache->addr != NULL) GNUNET_SERVER_transmit_context_append_data (tc, cache->addr, - strlen (cache->addr) + 1, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + strlen (cache->addr) + 1, + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } @@ -240,7 +240,7 @@ get_ip_as_string (struct GNUNET_SERVER_Client *client, #if HAVE_GETADDRINFO static int getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc, - const char *hostname, int domain) + const char *hostname, int domain) { int s; struct addrinfo hints; @@ -254,36 +254,37 @@ getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc, #else hints.ai_family = AF_INET; #endif - hints.ai_socktype = SOCK_STREAM; /* go for TCP */ + hints.ai_socktype = SOCK_STREAM; /* go for TCP */ if (0 != (s = getaddrinfo (hostname, NULL, &hints, &result))) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Could not resolve `%s' (%s): %s\n"), - hostname, - (domain == - AF_INET) ? "IPv4" : ((domain == AF_INET6) ? "IPv6" : "any"), - gai_strerror (s)); - if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Could not resolve `%s' (%s): %s\n"), hostname, + (domain == + AF_INET) ? "IPv4" : ((domain == + AF_INET6) ? "IPv6" : "any"), + gai_strerror (s)); + if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY) #ifndef MINGW - || (s == EAI_SYSTEM) + || (s == EAI_SYSTEM) #else - // FIXME NILS - || 1 + // FIXME NILS + || 1 #endif - ) - return GNUNET_NO; /* other function may still succeed */ - return GNUNET_SYSERR; - } + ) + return GNUNET_NO; /* other function may still succeed */ + return GNUNET_SYSERR; + } if (result == NULL) return GNUNET_SYSERR; pos = result; while (pos != NULL) - { - GNUNET_SERVER_transmit_context_append_data (tc, pos->ai_addr, - pos->ai_addrlen, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); - pos = pos->ai_next; - } + { + GNUNET_SERVER_transmit_context_append_data (tc, pos->ai_addr, + pos->ai_addrlen, + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + pos = pos->ai_next; + } freeaddrinfo (result); return GNUNET_OK; } @@ -292,7 +293,7 @@ getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc, #if HAVE_GETHOSTBYNAME2 static int gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, - const char *hostname, int domain) + const char *hostname, int domain) { struct hostent *hp; struct sockaddr_in a4; @@ -301,48 +302,48 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, int ret2; if (domain == AF_UNSPEC) - { - ret1 = gethostbyname2_resolve (tc, hostname, AF_INET); - ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6); - if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK)) - return GNUNET_OK; - if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR)) - return GNUNET_SYSERR; - return GNUNET_NO; - } + { + ret1 = gethostbyname2_resolve (tc, hostname, AF_INET); + ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6); + if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK)) + return GNUNET_OK; + if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR)) + return GNUNET_SYSERR; + return GNUNET_NO; + } hp = gethostbyname2 (hostname, domain); if (hp == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Could not find IP of host `%s': %s\n"), hostname, - hstrerror (h_errno)); - return GNUNET_SYSERR; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Could not find IP of host `%s': %s\n"), hostname, + hstrerror (h_errno)); + return GNUNET_SYSERR; + } GNUNET_assert (hp->h_addrtype == domain); if (domain == AF_INET) - { - GNUNET_assert (hp->h_length == sizeof (struct in_addr)); - memset (&a4, 0, sizeof (a4)); - a4.sin_family = AF_INET; + { + GNUNET_assert (hp->h_length == sizeof (struct in_addr)); + memset (&a4, 0, sizeof (a4)); + a4.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - a4.sin_len = (u_char) sizeof (struct sockaddr_in); + a4.sin_len = (u_char) sizeof (struct sockaddr_in); #endif - memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length); - GNUNET_SERVER_transmit_context_append_data (tc, &a4, sizeof (a4), - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); - } + memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length); + GNUNET_SERVER_transmit_context_append_data (tc, &a4, sizeof (a4), + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + } else - { - GNUNET_assert (hp->h_length == sizeof (struct in6_addr)); - memset (&a6, 0, sizeof (a6)); - a6.sin6_family = AF_INET6; + { + GNUNET_assert (hp->h_length == sizeof (struct in6_addr)); + memset (&a6, 0, sizeof (a6)); + a6.sin6_family = AF_INET6; #if HAVE_SOCKADDR_IN_SIN_LEN - a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); + a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); #endif - memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length); - GNUNET_SERVER_transmit_context_append_data (tc, &a6, sizeof (a6), - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); - } + memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length); + GNUNET_SERVER_transmit_context_append_data (tc, &a6, sizeof (a6), + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + } return GNUNET_OK; } #endif @@ -350,24 +351,24 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, #if HAVE_GETHOSTBYNAME static int gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc, - const char *hostname) + const char *hostname) { struct hostent *hp; struct sockaddr_in addr; hp = GETHOSTBYNAME (hostname); if (hp == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Could not find IP of host `%s': %s\n"), hostname, - hstrerror (h_errno)); - return GNUNET_SYSERR; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Could not find IP of host `%s': %s\n"), hostname, + hstrerror (h_errno)); + return GNUNET_SYSERR; + } if (hp->h_addrtype != AF_INET) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } + { + GNUNET_break (0); + return GNUNET_SYSERR; + } GNUNET_assert (hp->h_length == sizeof (struct in_addr)); memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; @@ -376,7 +377,7 @@ gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc, #endif memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length); GNUNET_SERVER_transmit_context_append_data (tc, &addr, sizeof (addr), - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); return GNUNET_OK; } #endif @@ -390,8 +391,8 @@ gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc, * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any" */ static void -get_ip_from_hostname (struct GNUNET_SERVER_Client *client, const char *hostname, - int domain) +get_ip_from_hostname (struct GNUNET_SERVER_Client *client, + const char *hostname, int domain) { int ret; struct GNUNET_SERVER_TransmitContext *tc; @@ -411,7 +412,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client, const char *hostname, gethostbyname_resolve (tc, hostname); #endif GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } @@ -425,7 +426,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client, const char *hostname, */ static void handle_get (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { uint16_t msize; const struct GNUNET_RESOLVER_GetMessage *msg; @@ -437,78 +438,78 @@ handle_get (void *cls, struct GNUNET_SERVER_Client *client, msize = ntohs (message->size); if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } msg = (const struct GNUNET_RESOLVER_GetMessage *) message; size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage); direction = ntohl (msg->direction); domain = ntohl (msg->domain); if (direction == GNUNET_NO) - { - /* IP from hostname */ - hostname = (const char *) &msg[1]; - if (hostname[size - 1] != '\0') { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + /* IP from hostname */ + hostname = (const char *) &msg[1]; + if (hostname[size - 1] != '\0') + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver asked to look up `%s'.\n"), - hostname); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + _("Resolver asked to look up `%s'.\n"), hostname); #endif - get_ip_from_hostname (client, hostname, domain); - } + get_ip_from_hostname (client, hostname, domain); + } else - { + { #if DEBUG_RESOLVER - char buf[INET6_ADDRSTRLEN]; + char buf[INET6_ADDRSTRLEN]; #endif - if (size < sizeof (struct sockaddr)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } - sa = (const struct sockaddr *) &msg[1]; - switch (sa->sa_family) - { - case AF_INET: - if (size != sizeof (struct sockaddr_in)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + if (size < sizeof (struct sockaddr)) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + sa = (const struct sockaddr *) &msg[1]; + switch (sa->sa_family) + { + case AF_INET: + if (size != sizeof (struct sockaddr_in)) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } #if DEBUG_RESOLVER - inet_ntop (AF_INET, sa, buf, size); + inet_ntop (AF_INET, sa, buf, size); #endif - break; - case AF_INET6: - if (size != sizeof (struct sockaddr_in6)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + break; + case AF_INET6: + if (size != sizeof (struct sockaddr_in6)) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } #if DEBUG_RESOLVER - inet_ntop (AF_INET6, sa, buf, size); + inet_ntop (AF_INET6, sa, buf, size); #endif - break; - default: - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + break; + default: + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _("Resolver asked to look up IP address `%s'.\n"), buf); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + _("Resolver asked to look up IP address `%s'.\n"), buf); #endif - get_ip_as_string (client, sa, size); - } + get_ip_as_string (client, sa, size); + } } @@ -545,18 +546,18 @@ main (int argc, char *const *argv) struct IPCache *pos; ret = - (GNUNET_OK == - GNUNET_SERVICE_run (argc, argv, "resolver", GNUNET_SERVICE_OPTION_NONE, - &run, NULL)) ? 0 : 1; + (GNUNET_OK == + GNUNET_SERVICE_run (argc, argv, "resolver", GNUNET_SERVICE_OPTION_NONE, + &run, NULL)) ? 0 : 1; while (head != NULL) - { - pos = head->next; - GNUNET_free_non_null (head->addr); - GNUNET_free (head->sa); - GNUNET_free (head); - head = pos; - } + { + pos = head->next; + GNUNET_free_non_null (head->addr); + GNUNET_free (head->sa); + GNUNET_free (head); + head = pos; + } return ret; } diff --git a/src/util/load.c b/src/util/load.c index 39e861597..1df1abc7c 100644 --- a/src/util/load.c +++ b/src/util/load.c @@ -28,6 +28,8 @@ #define DEBUG_LOAD GNUNET_EXTRA_LOGGING +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Values we track for load calculations. */ @@ -92,23 +94,23 @@ internal_update (struct GNUNET_LOAD_Value *load) if (delta.rel_value < load->autodecline.rel_value) return; if (load->autodecline.rel_value == 0) - { - load->runavg_delay = 0.0; - load->load = 0; - return; - } + { + load->runavg_delay = 0.0; + load->load = 0; + return; + } n = delta.rel_value / load->autodecline.rel_value; if (n > 16) - { - load->runavg_delay = 0.0; - load->load = 0; - return; - } + { + load->runavg_delay = 0.0; + load->load = 0; + return; + } while (n > 0) - { - n--; - load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; - } + { + n--; + load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; + } } @@ -140,7 +142,7 @@ GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline) */ void GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load, - struct GNUNET_TIME_Relative autodecline) + struct GNUNET_TIME_Relative autodecline) { internal_update (load); load->autodecline = autodecline; @@ -175,10 +177,10 @@ calculate_load (struct GNUNET_LOAD_Value *load) nm1 = n - 1.0; avgdel = sum_val_i / n; stddev = - (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + - n * avgdel * avgdel) / nm1; + (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + + n * avgdel * avgdel) / nm1; if (stddev <= 0) - stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */ + stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */ /* now calculate load based on how far out we are from * std dev; or if we are below average, simply assume load zero */ if (load->runavg_delay < avgdel) @@ -241,11 +243,11 @@ GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data) internal_update (load); load->last_update = GNUNET_TIME_absolute_get (); if (data > 64 * 1024) - { - /* very large */ - load->load = 100.0; - return; - } + { + /* very large */ + load->load = 100.0; + return; + } dv = (uint32_t) data; load->cummulative_delay += dv; load->cummulative_squared_delay += dv * dv; diff --git a/src/util/network.c b/src/util/network.c index 4fa1ce27a..a62a57758 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -29,6 +29,10 @@ #include "disk.h" #include "gnunet_container_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING #define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING @@ -86,11 +90,11 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock) mode = !doBlock; if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR) - { - SetErrnoFromWinsockError (WSAGetLastError ()); - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); - return GNUNET_SYSERR; - } + { + SetErrnoFromWinsockError (WSAGetLastError ()); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); + return GNUNET_SYSERR; + } return GNUNET_OK; #else @@ -99,10 +103,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock) if (flags == -1) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); + return GNUNET_SYSERR; + } if (doBlock) flags &= ~O_NONBLOCK; @@ -110,10 +114,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock) flags |= O_NONBLOCK; if (0 != fcntl (fd->fd, F_SETFL, flags)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); + return GNUNET_SYSERR; + } return GNUNET_OK; #endif } @@ -158,8 +162,8 @@ socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h) if (0 != setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, - sizeof (abs_value))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); + sizeof (abs_value))) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); } #endif @@ -177,15 +181,16 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h) #ifndef WINDOWS int value = 1; - if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); + if (0 != + setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value))) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); #else const char *abs_value = "1"; if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, - sizeof (abs_value))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); + sizeof (abs_value))) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); #endif } @@ -200,7 +205,8 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h) */ struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, - struct sockaddr *address, socklen_t * address_len) + struct sockaddr *address, + socklen_t * address_len) { struct GNUNET_NETWORK_Handle *ret; @@ -212,8 +218,8 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, int gsn = getsockname (desc->fd, &name, &namelen); if (gsn == 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n", - GNUNET_a2s (&name, namelen)); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n", + GNUNET_a2s (&name, namelen)); } #endif ret->fd = accept (desc->fd, address, address_len); @@ -222,36 +228,36 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, else ret->af = desc->af; if (ret->fd == INVALID_SOCKET) - { + { #ifdef MINGW - SetErrnoFromWinsockError (WSAGetLastError ()); + SetErrnoFromWinsockError (WSAGetLastError ()); #endif - GNUNET_free (ret); - return NULL; - } + GNUNET_free (ret); + return NULL; + } #ifndef MINGW if (ret->fd >= FD_SETSIZE) - { - GNUNET_break (0 == close (ret->fd)); - GNUNET_free (ret); - errno = EMFILE; - return NULL; - } + { + GNUNET_break (0 == close (ret->fd)); + GNUNET_free (ret); + errno = EMFILE; + return NULL; + } #endif if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) - { + { - /* we might want to treat this one as fatal... */ - GNUNET_break (0); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); - return NULL; - } + /* we might want to treat this one as fatal... */ + GNUNET_break (0); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); + return NULL; + } #ifndef MINGW if (GNUNET_OK != socket_set_inheritable (ret)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "socket_set_inheritable"); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "socket_set_inheritable"); #endif #ifdef DARWIN socket_set_nosigpipe (ret); @@ -273,8 +279,8 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, */ int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, - const struct sockaddr *address, - socklen_t address_len) + const struct sockaddr *address, + socklen_t address_len) { int ret; @@ -283,23 +289,24 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, const int on = 1; if (desc->af == AF_INET6) - if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); + if (0 != + setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on))) + LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); #endif #endif #ifndef WINDOWS /* This is required, and required here, but only on UNIX */ if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); + LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); #endif #ifndef LINUX #ifndef MINGW if (address->sa_family == AF_UNIX) - { - const struct sockaddr_un *un = (const struct sockaddr_un *) address; + { + const struct sockaddr_un *un = (const struct sockaddr_un *) address; - (void) unlink (un->sun_path); - } + (void) unlink (un->sun_path); + } #endif #endif ret = bind (desc->fd, address, address_len); @@ -334,17 +341,17 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc) DWORD error = 0; #if DEBUG_NETWORK - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", - "Closing 0x%x\n", desc->fd); + LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", + "Closing 0x%x\n", desc->fd); #endif SetLastError (0); ret = closesocket (desc->fd); error = WSAGetLastError (); SetErrnoFromWinsockError (error); #if DEBUG_NETWORK - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", - "Closed 0x%x, closesocket() returned %d, GLE is %u\n", - desc->fd, ret, error); + LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", + "Closed 0x%x, closesocket() returned %d, GLE is %u\n", + desc->fd, ret, error); #endif #else ret = close (desc->fd); @@ -352,13 +359,12 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc) #ifndef LINUX #ifndef MINGW if ((desc->af == AF_UNIX) && (NULL != desc->addr)) - { - const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr; + { + const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr; - if (0 != unlink (un->sun_path)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", - un->sun_path); - } + if (0 != unlink (un->sun_path)) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path); + } #endif #endif GNUNET_free_non_null (desc->addr); @@ -382,7 +388,7 @@ GNUNET_NETWORK_socket_box_native (int fd) struct GNUNET_NETWORK_Handle *ret; if (fcntl (fd, F_GETFD) < 0) - return NULL; /* invalid FD */ + return NULL; /* invalid FD */ ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); ret->fd = fd; ret->af = AF_UNSPEC; @@ -400,8 +406,8 @@ GNUNET_NETWORK_socket_box_native (int fd) */ int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, - const struct sockaddr *address, - socklen_t address_len) + const struct sockaddr *address, + socklen_t address_len) { int ret; @@ -409,11 +415,11 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, #ifdef MINGW if (SOCKET_ERROR == ret) - { - SetErrnoFromWinsockError (WSAGetLastError ()); - if (errno == EWOULDBLOCK) - errno = EINPROGRESS; - } + { + SetErrnoFromWinsockError (WSAGetLastError ()); + if (errno == EWOULDBLOCK) + errno = EINPROGRESS; + } #endif return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } @@ -431,8 +437,8 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, */ int GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, - int level, int optname, void *optval, - socklen_t * optlen) + int level, int optname, void *optval, + socklen_t * optlen) { int ret; @@ -457,7 +463,7 @@ GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, */ int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, - int backlog) + int backlog) { int ret; @@ -480,7 +486,7 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, */ ssize_t GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * - desc) + desc) { int error; @@ -513,8 +519,9 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * */ ssize_t GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc, - void *buffer, size_t length, - struct sockaddr * src_addr, socklen_t * addrlen) + void *buffer, size_t length, + struct sockaddr * src_addr, + socklen_t * addrlen) { int ret; int flags; @@ -542,7 +549,7 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc, */ ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc, - void *buffer, size_t length) + void *buffer, size_t length) { int ret; int flags; @@ -571,7 +578,7 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc, */ ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc, - const void *buffer, size_t length) + const void *buffer, size_t length) { int ret; int flags; @@ -610,9 +617,9 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc, */ ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc, - const void *message, size_t length, - const struct sockaddr * dest_addr, - socklen_t dest_len) + const void *message, size_t length, + const struct sockaddr * dest_addr, + socklen_t dest_len) { int ret; int flags; @@ -645,8 +652,8 @@ GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc, */ int GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level, - int option_name, const void *option_value, - socklen_t option_len) + int option_name, const void *option_value, + socklen_t option_len) { int ret; @@ -678,36 +685,36 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol) ret->af = domain; ret->fd = socket (domain, type, protocol); if (INVALID_SOCKET == ret->fd) - { + { #ifdef MINGW - SetErrnoFromWinsockError (WSAGetLastError ()); + SetErrnoFromWinsockError (WSAGetLastError ()); #endif - GNUNET_free (ret); - return NULL; - } + GNUNET_free (ret); + return NULL; + } #ifndef MINGW if (ret->fd >= FD_SETSIZE) - { - GNUNET_break (0 == close (ret->fd)); - GNUNET_free (ret); - errno = EMFILE; - return NULL; - } + { + GNUNET_break (0 == close (ret->fd)); + GNUNET_free (ret); + errno = EMFILE; + return NULL; + } #endif if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) - { - /* we might want to treat this one as fatal... */ - GNUNET_break (0); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); - return NULL; - } + { + /* we might want to treat this one as fatal... */ + GNUNET_break (0); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); + return NULL; + } #ifndef MINGW if (GNUNET_OK != socket_set_inheritable (ret)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "socket_set_inheritable"); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "socket_set_inheritable"); #endif #ifdef DARWIN socket_set_nosigpipe (ret); @@ -716,7 +723,7 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol) #ifdef AF_UNIX && (domain != AF_UNIX) #endif - ) + ) socket_set_nodelay (ret); return ret; } @@ -761,24 +768,24 @@ GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc) if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value, - sizeof (value)))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); + sizeof (value)))) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value, - sizeof (value)))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); + sizeof (value)))) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); #elif LINUX int value = 0; if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value)))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value)))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); #endif return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } @@ -805,7 +812,7 @@ GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds) */ void GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, - const struct GNUNET_NETWORK_Handle *desc) + const struct GNUNET_NETWORK_Handle *desc) { FD_SET (desc->fd, &fds->sds); if (desc->fd + 1 > fds->nsds) @@ -821,7 +828,7 @@ GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, */ int GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, - const struct GNUNET_NETWORK_Handle *desc) + const struct GNUNET_NETWORK_Handle *desc) { return FD_ISSET (desc->fd, &fds->sds); } @@ -834,18 +841,18 @@ GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, */ void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, - const struct GNUNET_NETWORK_FDSet *src) + const struct GNUNET_NETWORK_FDSet *src) { int nfds; for (nfds = src->nsds; nfds > 0; nfds--) if (FD_ISSET (nfds, &src->sds)) - { - FD_SET (nfds, &dst->sds); - if (nfds + 1 > dst->nsds) - dst->nsds = nfds + 1; - } + { + FD_SET (nfds, &dst->sds); + if (nfds + 1 > dst->nsds) + dst->nsds = nfds + 1; + } #ifdef MINGW GNUNET_CONTAINER_slist_append (dst->handles, src->handles); #endif @@ -860,7 +867,7 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, */ void GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, - const struct GNUNET_NETWORK_FDSet *from) + const struct GNUNET_NETWORK_FDSet *from) { FD_COPY (&from->sds, &to->sds); to->nsds = from->nsds; @@ -894,7 +901,7 @@ GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc) */ void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, - const fd_set * from, int nfds) + const fd_set * from, int nfds) { FD_COPY (from, &to->sds); to->nsds = nfds; @@ -925,7 +932,7 @@ GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd) */ int GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, - int nfd) + int nfd) { if ((nfd == -1) || (to == NULL)) return GNUNET_NO; @@ -940,12 +947,12 @@ GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, */ void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, - const struct GNUNET_DISK_FileHandle *h) + const struct GNUNET_DISK_FileHandle *h) { #ifdef MINGW GNUNET_CONTAINER_slist_add (fds->handles, - GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h, - sizeof (struct GNUNET_DISK_FileHandle)); + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h, + sizeof (struct GNUNET_DISK_FileHandle)); #else int fd; @@ -967,13 +974,13 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, */ int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, - const struct GNUNET_DISK_FileHandle *h) + const struct GNUNET_DISK_FileHandle *h) { #ifdef MINGW return GNUNET_CONTAINER_slist_contains (fds->handles, h, - sizeof (struct - GNUNET_DISK_FileHandle)); + sizeof (struct + GNUNET_DISK_FileHandle)); #else return FD_ISSET (h->fd, &fds->sds); #endif @@ -988,7 +995,7 @@ GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, */ int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, - const struct GNUNET_NETWORK_FDSet *fds2) + const struct GNUNET_NETWORK_FDSet *fds2) { #ifndef MINGW int nfds; @@ -997,11 +1004,11 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, if (nfds > fds2->nsds) nfds = fds2->nsds; while (nfds > 0) - { - nfds--; - if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds)) - return GNUNET_YES; - } + { + nfds--; + if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds)) + return GNUNET_YES; + } #else struct GNUNET_CONTAINER_SList_Iterator *it; struct GNUNET_DISK_FileHandle *h; @@ -1012,45 +1019,48 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, * inside of fd_set; also the O(n^2) is really bad... */ for (i = 0; i < fds1->sds.fd_count; i++) - { - for (j = 0; j < fds2->sds.fd_count; j++) { - if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j]) - return GNUNET_YES; + for (j = 0; j < fds2->sds.fd_count; j++) + { + if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j]) + return GNUNET_YES; + } } - } it = GNUNET_CONTAINER_slist_begin (fds1->handles); while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES) - { + { #if DEBUG_NETWORK - struct GNUNET_CONTAINER_SList_Iterator *t; + struct GNUNET_CONTAINER_SList_Iterator *t; #endif - h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL); + h = + (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, + NULL); #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Checking that FD 0x%x is in another set:\n", h->h); - for (t = GNUNET_CONTAINER_slist_begin (fds2->handles); - GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; - GNUNET_CONTAINER_slist_next (t)) - { - struct GNUNET_DISK_FileHandle *fh; - - fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, - NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h); - } -#endif - if (GNUNET_CONTAINER_slist_contains - (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle))) - { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Checking that FD 0x%x is in another set:\n", h->h); + for (t = GNUNET_CONTAINER_slist_begin (fds2->handles); + GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; + GNUNET_CONTAINER_slist_next (t)) + { + struct GNUNET_DISK_FileHandle *fh; + + fh = + (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, + NULL); + LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h); + } +#endif + if (GNUNET_CONTAINER_slist_contains + (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle))) + { #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); #endif - GNUNET_CONTAINER_slist_iter_destroy (it); - return GNUNET_YES; + GNUNET_CONTAINER_slist_iter_destroy (it); + return GNUNET_YES; + } + GNUNET_CONTAINER_slist_next (it); } - GNUNET_CONTAINER_slist_next (it); - } GNUNET_CONTAINER_slist_iter_destroy (it); #endif return GNUNET_NO; @@ -1098,9 +1108,9 @@ GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds) */ int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, - struct GNUNET_NETWORK_FDSet *wfds, - struct GNUNET_NETWORK_FDSet *efds, - const struct GNUNET_TIME_Relative timeout) + struct GNUNET_NETWORK_FDSet *wfds, + struct GNUNET_NETWORK_FDSet *efds, + const struct GNUNET_TIME_Relative timeout) { int nfds = 0; @@ -1130,7 +1140,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, DWORD newretcode = 0; int returnedpos = 0; - struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except; + struct GNUNET_CONTAINER_SList *handles_read, *handles_write, + *handles_except; fd_set aread, awrite, aexcept; @@ -1144,67 +1155,68 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, struct timeval tv; #endif if (NULL != rfds) - { - nfds = rfds->nsds; + { + nfds = rfds->nsds; #ifdef MINGW - handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles); + handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles); #if DEBUG_NETWORK - { - struct GNUNET_CONTAINER_SList_Iterator *t; - - for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); - GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; - GNUNET_CONTAINER_slist_next (t)) { - struct GNUNET_DISK_FileHandle *fh; - - fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, - NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", - fh->h, fh); + struct GNUNET_CONTAINER_SList_Iterator *t; + + for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); + GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; + GNUNET_CONTAINER_slist_next (t)) + { + struct GNUNET_DISK_FileHandle *fh; + + fh = + (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, + NULL); + LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", + fh->h, fh); + } } - } #endif #endif - } + } if (NULL != wfds) - { - nfds = GNUNET_MAX (nfds, wfds->nsds); + { + nfds = GNUNET_MAX (nfds, wfds->nsds); #ifdef MINGW - handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles); + handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles); #endif - } + } if (NULL != efds) - { - nfds = GNUNET_MAX (nfds, efds->nsds); + { + nfds = GNUNET_MAX (nfds, efds->nsds); #ifdef MINGW - handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles); + handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles); #endif - } + } if ((nfds == 0) && (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) #ifdef MINGW && handles == 0 #endif - ) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), - "select"); - GNUNET_break (0); - } + ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), + "select"); + GNUNET_break (0); + } #ifndef MINGW tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value; tv.tv_usec = - 1000 * (timeout.rel_value - - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value)); + 1000 * (timeout.rel_value - + (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value)); return select (nfds, (rfds != NULL) ? &rfds->sds : NULL, - (wfds != NULL) ? &wfds->sds : NULL, - (efds != NULL) ? &efds->sds : NULL, - (timeout.rel_value == - GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv); + (wfds != NULL) ? &wfds->sds : NULL, + (efds != NULL) ? &efds->sds : NULL, + (timeout.rel_value == + GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv); #else #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) @@ -1215,10 +1227,10 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value; /* select() may be used as a portable way to sleep */ if (!(rfds || wfds || efds)) - { - Sleep (ms_total); - return 0; - } + { + Sleep (ms_total); + return 0; + } /* Events for sockets */ if (!hEventRead) @@ -1254,212 +1266,217 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, FD_ZERO (&bexcept); #endif if (rfds) - { - FD_COPY (&rfds->sds, &aread); + { + FD_COPY (&rfds->sds, &aread); #if DEBUG_NETWORK - FD_COPY (&rfds->sds, &bread); + FD_COPY (&rfds->sds, &bread); #endif - } + } if (wfds) - { - FD_COPY (&wfds->sds, &awrite); + { + FD_COPY (&wfds->sds, &awrite); #if DEBUG_NETWORK - FD_COPY (&wfds->sds, &bwrite); + FD_COPY (&wfds->sds, &bwrite); #endif - } + } if (efds) - { - FD_COPY (&efds->sds, &aexcept); + { + FD_COPY (&efds->sds, &aexcept); #if DEBUG_NETWORK - FD_COPY (&efds->sds, &bexcept); + FD_COPY (&efds->sds, &bexcept); #endif - } + } /* We will first Add the PIPES to the events */ /* Read Pipes */ if (rfds && read_handles) - { - struct GNUNET_CONTAINER_SList_Iterator *i; - - for (i = GNUNET_CONTAINER_slist_begin (rfds->handles); - GNUNET_CONTAINER_slist_end (i) != GNUNET_YES; - GNUNET_CONTAINER_slist_next (i)) { - struct GNUNET_DISK_FileHandle *fh; - - fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, - NULL); - if (fh->type == GNUNET_PIPE) - { - /* Read zero bytes to check the status of the pipe */ + struct GNUNET_CONTAINER_SList_Iterator *i; + + for (i = GNUNET_CONTAINER_slist_begin (rfds->handles); + GNUNET_CONTAINER_slist_end (i) != GNUNET_YES; + GNUNET_CONTAINER_slist_next (i)) + { + struct GNUNET_DISK_FileHandle *fh; + + fh = + (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, + NULL); + if (fh->type == GNUNET_PIPE) + { + /* Read zero bytes to check the status of the pipe */ #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Reading 0 bytes from the pipe 0x%x\n", fh->h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Reading 0 bytes from the pipe 0x%x\n", fh->h); #endif - if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead)) - { - DWORD error_code = GetLastError (); + if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead)) + { + DWORD error_code = GetLastError (); - if (error_code == ERROR_IO_PENDING) - { + if (error_code == ERROR_IO_PENDING) + { #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding the pipe's 0x%x overlapped event to the array as %d\n", - fh->h, nhandles); -#endif - handle_array[nhandles++] = fh->oOverlapRead->hEvent; - readArray[readPipes++] = fh; - } - /* - * else - * { - * SetErrnoFromWinError (error_code); - * } - */ - } - else - { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding the pipe's 0x%x overlapped event to the array as %d\n", + fh->h, nhandles); +#endif + handle_array[nhandles++] = fh->oOverlapRead->hEvent; + readArray[readPipes++] = fh; + } + /* + * else + * { + * SetErrnoFromWinError (error_code); + * } + */ + } + else + { #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding the read ready event to the array as %d\n", - nhandles); -#endif - handle_array[nhandles++] = hEventReadReady; - readArray[readPipes++] = fh; - } - } - else - { - GNUNET_CONTAINER_slist_add (handles_read, - GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - fh, sizeof (struct GNUNET_DISK_FileHandle)); - } + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding the read ready event to the array as %d\n", + nhandles); +#endif + handle_array[nhandles++] = hEventReadReady; + readArray[readPipes++] = fh; + } + } + else + { + GNUNET_CONTAINER_slist_add (handles_read, + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + fh, + sizeof (struct + GNUNET_DISK_FileHandle)); + } + } + GNUNET_CONTAINER_slist_iter_destroy (i); } - GNUNET_CONTAINER_slist_iter_destroy (i); - } if (wfds && write_handles) - { + { #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding the write ready event to the array as %d\n", nhandles); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding the write ready event to the array as %d\n", nhandles); #endif - handle_array[nhandles++] = hEventPipeWrite; - writePipePos = nhandles; - } + handle_array[nhandles++] = hEventPipeWrite; + writePipePos = nhandles; + } if (efds && ex_handles) - { - struct GNUNET_CONTAINER_SList_Iterator *i; - - for (i = GNUNET_CONTAINER_slist_begin (efds->handles); - GNUNET_CONTAINER_slist_end (i) != GNUNET_YES; - GNUNET_CONTAINER_slist_next (i)) { - struct GNUNET_DISK_FileHandle *fh; - DWORD dwBytes; - - fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, - NULL); - if (fh->type == GNUNET_PIPE) - { - if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL)) - { - GNUNET_CONTAINER_slist_add (handles_except, - GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - fh, - sizeof (struct GNUNET_DISK_FileHandle)); - newretcode++; - } - } + struct GNUNET_CONTAINER_SList_Iterator *i; + + for (i = GNUNET_CONTAINER_slist_begin (efds->handles); + GNUNET_CONTAINER_slist_end (i) != GNUNET_YES; + GNUNET_CONTAINER_slist_next (i)) + { + struct GNUNET_DISK_FileHandle *fh; + DWORD dwBytes; + + fh = + (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, + NULL); + if (fh->type == GNUNET_PIPE) + { + if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL)) + { + GNUNET_CONTAINER_slist_add (handles_except, + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + fh, + sizeof (struct + GNUNET_DISK_FileHandle)); + newretcode++; + } + } + } + GNUNET_CONTAINER_slist_iter_destroy (i); } - GNUNET_CONTAINER_slist_iter_destroy (i); - } if (nfds > 0) - { - if (rfds) { + if (rfds) + { #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding the socket read event to the array as %d\n", - nhandles); -#endif - handle_array[nhandles++] = hEventRead; - nSockEvents++; - for (i = 0; i < rfds->sds.fd_count; i++) - { - WSAEventSelect (rfds->sds.fd_array[i], hEventRead, - FD_ACCEPT | FD_READ | FD_CLOSE); - nsock++; - } - } - if (wfds) - { - int wakeup = 0; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding the socket read event to the array as %d\n", nhandles); +#endif + handle_array[nhandles++] = hEventRead; + nSockEvents++; + for (i = 0; i < rfds->sds.fd_count; i++) + { + WSAEventSelect (rfds->sds.fd_array[i], hEventRead, + FD_ACCEPT | FD_READ | FD_CLOSE); + nsock++; + } + } + if (wfds) + { + int wakeup = 0; #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding the socket write event to the array as %d\n", - nhandles); -#endif - handle_array[nhandles++] = hEventWrite; - nSockEvents++; - for (i = 0; i < wfds->sds.fd_count; i++) - { - DWORD error; - int status; - - status = send (wfds->sds.fd_array[i], NULL, 0, 0); - error = GetLastError (); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding the socket write event to the array as %d\n", + nhandles); +#endif + handle_array[nhandles++] = hEventWrite; + nSockEvents++; + for (i = 0; i < wfds->sds.fd_count; i++) + { + DWORD error; + int status; + + status = send (wfds->sds.fd_array[i], NULL, 0, 0); + error = GetLastError (); #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "pre-send to the socket %d returned %d (%u)\n", i, status, - error); -#endif - if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)) - wakeup = 1; - WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, - FD_WRITE | FD_CONNECT | FD_CLOSE); - nsock++; - } - if (wakeup) - SetEvent (hEventWrite); - } - if (efds) - { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "pre-send to the socket %d returned %d (%u)\n", i, status, + error); +#endif + if (status == 0 + || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)) + wakeup = 1; + WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, + FD_WRITE | FD_CONNECT | FD_CLOSE); + nsock++; + } + if (wakeup) + SetEvent (hEventWrite); + } + if (efds) + { #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding the socket error event to the array as %d\n", - nhandles); -#endif - handle_array[nhandles++] = hEventException; - nSockEvents++; - for (i = 0; i < efds->sds.fd_count; i++) - { - WSAEventSelect (efds->sds.fd_array[i], hEventException, - FD_OOB | FD_CLOSE); - nsock++; - } + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding the socket error event to the array as %d\n", + nhandles); +#endif + handle_array[nhandles++] = hEventException; + nSockEvents++; + for (i = 0; i < efds->sds.fd_count; i++) + { + WSAEventSelect (efds->sds.fd_array[i], hEventException, + FD_OOB | FD_CLOSE); + nsock++; + } + } } - } handle_array[nhandles] = NULL; #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles); + LOG (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total); #endif if (nhandles) returncode = - WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total); + WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total); #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", - returncode); + LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", + returncode); #endif returnedpos = returncode - WAIT_OBJECT_0; #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos); + LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos); #endif /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */ @@ -1468,237 +1485,241 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles); */ if (nhandles && (returnedpos < nhandles)) - { - DWORD waitstatus; - - /* Do the select */ - if (nfds) { - struct timeval tvslice; - - tvslice.tv_sec = 0; - tvslice.tv_usec = 10; - retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice); - if (retcode == -1) - retcode = 0; + DWORD waitstatus; + + /* Do the select */ + if (nfds) + { + struct timeval tvslice; + + tvslice.tv_sec = 0; + tvslice.tv_usec = 10; + retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice); + if (retcode == -1) + retcode = 0; #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode); -#endif - } - /* FIXME: <= writePipePos? Really? */ - if ((writePipePos != -1) && (returnedpos <= writePipePos)) - { - GNUNET_CONTAINER_slist_append (handles_write, wfds->handles); - retcode += write_handles; + LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode); +#endif + } + /* FIXME: <= writePipePos? Really? */ + if ((writePipePos != -1) && (returnedpos <= writePipePos)) + { + GNUNET_CONTAINER_slist_append (handles_write, wfds->handles); + retcode += write_handles; #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); #endif - } + } #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes); -#endif - /* We have some pipes ready for read. */ - /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */ - - if (returnedpos < readPipes) - { - /* - * for (i = 0; i < readPipes; i++) - * { - * waitstatus = WaitForSingleObject (handle_array[i], 0); - * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus); - * if (waitstatus != WAIT_OBJECT_0) - * continue; - * GNUNET_CONTAINER_slist_add (handles_read, - * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - * readArray[i], sizeof (struct GNUNET_DISK_FileHandle)); - * retcode++; - * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n"); - * } - */ - for (i = 0; i < readPipes; i++) - { - DWORD error; - BOOL bret; - - SetLastError (0); - waitstatus = 0; - bret = - PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL); - error = GetLastError (); + LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes); +#endif + /* We have some pipes ready for read. */ + /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */ + + if (returnedpos < readPipes) + { + /* + * for (i = 0; i < readPipes; i++) + * { + * waitstatus = WaitForSingleObject (handle_array[i], 0); + * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus); + * if (waitstatus != WAIT_OBJECT_0) + * continue; + * GNUNET_CONTAINER_slist_add (handles_read, + * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + * readArray[i], sizeof (struct GNUNET_DISK_FileHandle)); + * retcode++; + * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n"); + * } + */ + for (i = 0; i < readPipes; i++) + { + DWORD error; + BOOL bret; + + SetLastError (0); + waitstatus = 0; + bret = + PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, + NULL); + error = GetLastError (); #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n", - i, readArray[i]->h, bret, waitstatus, error); -#endif - if (bret == 0) - { - if (error != ERROR_BROKEN_PIPE) - continue; - } - else if (waitstatus <= 0) - continue; - GNUNET_CONTAINER_slist_add (handles_read, - GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - readArray[i], - sizeof (struct GNUNET_DISK_FileHandle)); - retcode++; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n", + i, readArray[i]->h, bret, waitstatus, error); +#endif + if (bret == 0) + { + if (error != ERROR_BROKEN_PIPE) + continue; + } + else if (waitstatus <= 0) + continue; + GNUNET_CONTAINER_slist_add (handles_read, + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + readArray[i], + sizeof (struct + GNUNET_DISK_FileHandle)); + retcode++; #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n", - readArray[i], readArray[i]->h); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n", + readArray[i], readArray[i]->h); #endif - } - } - waitstatus = WaitForSingleObject (hEventWrite, 0); + } + } + waitstatus = WaitForSingleObject (hEventWrite, 0); #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Wait for the write event returned %d\n", waitstatus); -#endif - if (waitstatus == WAIT_OBJECT_0) - { - for (i = 0; i < wfds->sds.fd_count; i++) - { - DWORD error; - int status; - int so_error = 0; - int sizeof_so_error = sizeof (so_error); - int gso_result = - getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR, - (char *) &so_error, &sizeof_so_error); - - status = send (wfds->sds.fd_array[i], NULL, 0, 0); - error = GetLastError (); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Wait for the write event returned %d\n", waitstatus); +#endif + if (waitstatus == WAIT_OBJECT_0) + { + for (i = 0; i < wfds->sds.fd_count; i++) + { + DWORD error; + int status; + int so_error = 0; + int sizeof_so_error = sizeof (so_error); + int gso_result = + getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR, + (char *) &so_error, &sizeof_so_error); + + status = send (wfds->sds.fd_array[i], NULL, 0, 0); + error = GetLastError (); #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "send to the socket %d returned %d (%u)\n", i, status, - error); -#endif - if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) || - (status == -1 && gso_result == 0 && error == WSAENOTCONN && - so_error == WSAECONNREFUSED)) - { - FD_SET (wfds->sds.fd_array[i], &awrite); - retcode += 1; - } - } + LOG (GNUNET_ERROR_TYPE_DEBUG, + "send to the socket %d returned %d (%u)\n", i, status, + error); +#endif + if (status == 0 + || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) + || (status == -1 && gso_result == 0 && error == WSAENOTCONN + && so_error == WSAECONNREFUSED)) + { + FD_SET (wfds->sds.fd_array[i], &awrite); + retcode += 1; + } + } + } } - } #if DEBUG_NETWORK if (!nhandles || (returnedpos >= nhandles)) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Returning from _select() with nothing!\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n"); #endif if (rfds) - { - struct GNUNET_CONTAINER_SList_Iterator *t; - - for (i = 0; i < rfds->sds.fd_count; i++) { - WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0); - nsock++; - } - for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); - GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; - GNUNET_CONTAINER_slist_next (t)) - { - struct GNUNET_DISK_FileHandle *fh; + struct GNUNET_CONTAINER_SList_Iterator *t; - fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, - NULL); - if (fh->type == GNUNET_PIPE) - { - CancelIo (fh->h); - } - } - GNUNET_CONTAINER_slist_iter_destroy (t); + for (i = 0; i < rfds->sds.fd_count; i++) + { + WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0); + nsock++; + } + for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); + GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; + GNUNET_CONTAINER_slist_next (t)) + { + struct GNUNET_DISK_FileHandle *fh; + + fh = + (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, + NULL); + if (fh->type == GNUNET_PIPE) + { + CancelIo (fh->h); + } + } + GNUNET_CONTAINER_slist_iter_destroy (t); #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n"); #endif - GNUNET_NETWORK_fdset_zero (rfds); - if (retcode != -1 && nhandles && (returnedpos < nhandles)) - GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode); - GNUNET_CONTAINER_slist_append (rfds->handles, handles_read); - } + GNUNET_NETWORK_fdset_zero (rfds); + if (retcode != -1 && nhandles && (returnedpos < nhandles)) + GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode); + GNUNET_CONTAINER_slist_append (rfds->handles, handles_read); + } if (wfds) - { - for (i = 0; i < wfds->sds.fd_count; i++) { - WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0); - nsock++; - } + for (i = 0; i < wfds->sds.fd_count; i++) + { + WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0); + nsock++; + } #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n"); #endif - GNUNET_NETWORK_fdset_zero (wfds); - if (retcode != -1 && nhandles && (returnedpos < nhandles)) - GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode); - GNUNET_CONTAINER_slist_append (wfds->handles, handles_write); - } + GNUNET_NETWORK_fdset_zero (wfds); + if (retcode != -1 && nhandles && (returnedpos < nhandles)) + GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode); + GNUNET_CONTAINER_slist_append (wfds->handles, handles_write); + } if (efds) - { - for (i = 0; i < efds->sds.fd_count; i++) { - WSAEventSelect (efds->sds.fd_array[i], hEventException, 0); - nsock++; - } + for (i = 0; i < efds->sds.fd_count; i++) + { + WSAEventSelect (efds->sds.fd_array[i], hEventException, 0); + nsock++; + } #if DEBUG_NETWORK - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n"); #endif - GNUNET_NETWORK_fdset_zero (efds); - if (retcode != -1 && nhandles && (returnedpos < nhandles)) - GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode); - GNUNET_CONTAINER_slist_append (efds->handles, handles_except); - } + GNUNET_NETWORK_fdset_zero (efds); + if (retcode != -1 && nhandles && (returnedpos < nhandles)) + GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode); + GNUNET_CONTAINER_slist_append (efds->handles, handles_except); + } GNUNET_CONTAINER_slist_destroy (handles_read); GNUNET_CONTAINER_slist_destroy (handles_write); GNUNET_CONTAINER_slist_destroy (handles_except); #if DEBUG_NETWORK if (rfds) - { - struct GNUNET_CONTAINER_SList_Iterator *t; - - for (i = 0; i < bread.fd_count; i++) { - if (bread.fd_array[i] != 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n", - bread.fd_array[i], - (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" : - "NOT SET"); - } - for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); - GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; - GNUNET_CONTAINER_slist_next (t)) - { - struct GNUNET_DISK_FileHandle *fh; + struct GNUNET_CONTAINER_SList_Iterator *t; - fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, - NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h); + for (i = 0; i < bread.fd_count; i++) + { + if (bread.fd_array[i] != 0) + LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n", + bread.fd_array[i], + (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" : + "NOT SET"); + } + for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); + GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; + GNUNET_CONTAINER_slist_next (t)) + { + struct GNUNET_DISK_FileHandle *fh; + + fh = + (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, + NULL); + LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h); + } } - } if (wfds) - { - for (i = 0; i < bwrite.fd_count; i++) { - if (bwrite.fd_array[i] != 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n", - bwrite.fd_array[i], - (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : - "NOT SET"); + for (i = 0; i < bwrite.fd_count; i++) + { + if (bwrite.fd_array[i] != 0) + LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n", + bwrite.fd_array[i], + (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : + "NOT SET"); + } } - } if (efds) - { - for (i = 0; i < bexcept.fd_count; i++) { - if (bexcept.fd_array[i] != 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n", - bexcept.fd_array[i], - (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : - "NOT SET"); + for (i = 0; i < bexcept.fd_count; i++) + { + if (bexcept.fd_array[i] != 0) + LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n", + bexcept.fd_array[i], + (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : + "NOT SET"); + } } - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode); #endif if (nhandles && (returnedpos < nhandles)) return retcode; diff --git a/src/util/os_installation.c b/src/util/os_installation.c index 1f4c5f000..688cc448e 100644 --- a/src/util/os_installation.c +++ b/src/util/os_installation.c @@ -38,6 +38,10 @@ #include #endif +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + #if LINUX /** * Try to determine path by reading /proc/PID/exe @@ -56,16 +60,16 @@ get_path_from_proc_maps () if (f == NULL) return NULL; while (NULL != fgets (line, sizeof (line), f)) - { - if ((1 == - sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) && - (NULL != (lgu = strstr (dir, "libgnunetutil")))) { - lgu[0] = '\0'; - fclose (f); - return GNUNET_strdup (dir); + if ((1 == + sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", + dir)) && (NULL != (lgu = strstr (dir, "libgnunetutil")))) + { + lgu[0] = '\0'; + fclose (f); + return GNUNET_strdup (dir); + } } - } fclose (f); return NULL; } @@ -83,19 +87,19 @@ get_path_from_proc_exe () GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ()); size = readlink (fn, lnk, sizeof (lnk) - 1); if (size <= 0) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); - return NULL; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); + return NULL; + } GNUNET_assert (size < sizeof (lnk)); lnk[size] = '\0'; while ((lnk[size] != '/') && (size > 0)) size--; if ((size < 4) || (lnk[size - 4] != '/')) - { - /* not installed in "/bin/" -- binary path probably useless */ - return NULL; - } + { + /* not installed in "/bin/" -- binary path probably useless */ + return NULL; + } lnk[size] = '\0'; return GNUNET_strdup (lnk); } @@ -134,7 +138,7 @@ get_path_from_NSGetExecutablePath () path = NULL; func = - (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); + (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); if (!func) return NULL; path = &zero; @@ -146,10 +150,10 @@ get_path_from_NSGetExecutablePath () path = GNUNET_malloc (len); ret = func (path, &len); if (ret != 0) - { - GNUNET_free (path); - return NULL; - } + { + GNUNET_free (path); + return NULL; + } len = strlen (path); while ((path[len] != '/') && (len > 0)) len--; @@ -168,22 +172,22 @@ get_path_from_dyld_image () p = NULL; c = _dyld_image_count (); for (i = 0; i < c; i++) - { - if (_dyld_get_image_header (i) == &_mh_dylib_header) { - path = _dyld_get_image_name (i); - if (path != NULL && strlen (path) > 0) - { - p = strdup (path); - s = p + strlen (p); - while ((s > p) && (*s != '/')) - s--; - s++; - *s = '\0'; - } - break; + if (_dyld_get_image_header (i) == &_mh_dylib_header) + { + path = _dyld_get_image_name (i); + if (path != NULL && strlen (path) > 0) + { + p = strdup (path); + s = p + strlen (p); + while ((s > p) && (*s != '/')) + s--; + s++; + *s = '\0'; + } + break; + } } - } return p; } #endif @@ -207,30 +211,30 @@ get_path_from_PATH (const char *binary) p = getenv ("PATH"); if (p == NULL) return NULL; - path = GNUNET_strdup (p); /* because we write on it */ + path = GNUNET_strdup (p); /* because we write on it */ buf = GNUNET_malloc (strlen (path) + 20); pos = path; while (NULL != (end = strchr (pos, PATH_SEPARATOR))) - { - *end = '\0'; - sprintf (buf, "%s/%s", pos, binary); - if (GNUNET_DISK_file_test (buf) == GNUNET_YES) + { + *end = '\0'; + sprintf (buf, "%s/%s", pos, binary); + if (GNUNET_DISK_file_test (buf) == GNUNET_YES) + { + pos = GNUNET_strdup (pos); + GNUNET_free (buf); + GNUNET_free (path); + return pos; + } + pos = end + 1; + } + sprintf (buf, "%s/%s", pos, binary); + if (GNUNET_DISK_file_test (buf) == GNUNET_YES) { pos = GNUNET_strdup (pos); GNUNET_free (buf); GNUNET_free (path); return pos; } - pos = end + 1; - } - sprintf (buf, "%s/%s", pos, binary); - if (GNUNET_DISK_file_test (buf) == GNUNET_YES) - { - pos = GNUNET_strdup (pos); - GNUNET_free (buf); - GNUNET_free (path); - return pos; - } GNUNET_free (buf); GNUNET_free (path); return NULL; @@ -286,10 +290,10 @@ os_get_gnunet_path () if (ret != NULL) return ret; /* other attempts here */ - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("Could not determine installation path for %s. Set `%s' environment variable.\n"), - "GNUnet", "GNUNET_PREFIX"); + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Could not determine installation path for %s. Set `%s' environment variable.\n"), + "GNUnet", "GNUNET_PREFIX"); return NULL; } @@ -355,11 +359,11 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) n = strlen (execpath); if (n == 0) - { - /* should never happen, but better safe than sorry */ - GNUNET_free (execpath); - return NULL; - } + { + /* should never happen, but better safe than sorry */ + GNUNET_free (execpath); + return NULL; + } /* remove filename itself */ while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) execpath[--n] = '\0'; @@ -368,59 +372,62 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) if ((n > 5) && ((0 == strcasecmp (&execpath[n - 5], "lib32")) || (0 == strcasecmp (&execpath[n - 5], "lib64")))) - { - if (dirkind != GNUNET_OS_IPK_LIBDIR) { - /* strip '/lib32' or '/lib64' */ - execpath[n - 5] = '\0'; - n -= 5; + if (dirkind != GNUNET_OS_IPK_LIBDIR) + { + /* strip '/lib32' or '/lib64' */ + execpath[n - 5] = '\0'; + n -= 5; + } + else + isbasedir = 0; } - else - isbasedir = 0; - } else if ((n > 3) && - ((0 == strcasecmp (&execpath[n - 3], "bin")) || - (0 == strcasecmp (&execpath[n - 3], "lib")))) - { - /* strip '/bin' or '/lib' */ - execpath[n - 3] = '\0'; - n -= 3; - } + ((0 == strcasecmp (&execpath[n - 3], "bin")) || + (0 == strcasecmp (&execpath[n - 3], "lib")))) + { + /* strip '/bin' or '/lib' */ + execpath[n - 3] = '\0'; + n -= 3; + } /* in case this was a directory named foo-bin, remove "foo-" */ while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) execpath[--n] = '\0'; switch (dirkind) - { - case GNUNET_OS_IPK_PREFIX: - case GNUNET_OS_IPK_SELF_PREFIX: - dirname = DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_BINDIR: - dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_LIBDIR: - if (isbasedir) + { + case GNUNET_OS_IPK_PREFIX: + case GNUNET_OS_IPK_SELF_PREFIX: + dirname = DIR_SEPARATOR_STR; + break; + case GNUNET_OS_IPK_BINDIR: + dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; + break; + case GNUNET_OS_IPK_LIBDIR: + if (isbasedir) + dirname = + DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" + DIR_SEPARATOR_STR; + else + dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR; + break; + case GNUNET_OS_IPK_DATADIR: dirname = - DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR; - else - dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_DATADIR: - dirname = - DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_LOCALEDIR: - dirname = - DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_ICONDIR: - dirname = - DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; - break; - default: - GNUNET_free (execpath); - return NULL; - } + DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" + DIR_SEPARATOR_STR; + break; + case GNUNET_OS_IPK_LOCALEDIR: + dirname = + DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" + DIR_SEPARATOR_STR; + break; + case GNUNET_OS_IPK_ICONDIR: + dirname = + DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; + break; + default: + GNUNET_free (execpath); + return NULL; + } tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); sprintf (tmp, "%s%s", execpath, dirname); GNUNET_free (execpath); @@ -453,59 +460,59 @@ GNUNET_OS_check_helper_binary (const char *binary) GNUNET_asprintf (&binaryexe, "%s.exe", binary); p = get_path_from_PATH (binaryexe); if (p != NULL) - { - GNUNET_asprintf (&pf, "%s/%s", p, binaryexe); - GNUNET_free (p); - p = pf; - } + { + GNUNET_asprintf (&pf, "%s/%s", p, binaryexe); + GNUNET_free (p); + p = pf; + } free (binaryexe); #else p = get_path_from_PATH (binary); if (p != NULL) - { - GNUNET_asprintf (&pf, "%s/%s", p, binary); - GNUNET_free (p); - p = pf; - } + { + GNUNET_asprintf (&pf, "%s/%s", p, binary); + GNUNET_free (p); + p = pf; + } #endif if (p == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Could not find binary `%s' in PATH!\n"), binary); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_INFO, + _("Could not find binary `%s' in PATH!\n"), binary); + return GNUNET_SYSERR; + } if (0 != STAT (p, &statbuf)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p, - STRERROR (errno)); - GNUNET_free (p); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p, + STRERROR (errno)); + GNUNET_free (p); + return GNUNET_SYSERR; + } #ifndef MINGW if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0)) - { - GNUNET_free (p); - return GNUNET_YES; - } + { + GNUNET_free (p); + return GNUNET_YES; + } if (0 == ACCESS (p, X_OK)) - { - GNUNET_free (p); - return GNUNET_NO; - } + { + GNUNET_free (p); + return GNUNET_NO; + } GNUNET_free (p); return GNUNET_SYSERR; #else GNUNET_free (p); rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); if (INVALID_SOCKET == rawsock) - { - DWORD err = GetLastError (); - - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", - err); - return GNUNET_NO; /* not running as administrator */ - } + { + DWORD err = GetLastError (); + + LOG (GNUNET_ERROR_TYPE_INFO, + "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", + err); + return GNUNET_NO; /* not running as administrator */ + } closesocket (rawsock); return GNUNET_YES; #endif diff --git a/src/util/os_network.c b/src/util/os_network.c index b55789c99..88fefd066 100644 --- a/src/util/os_network.c +++ b/src/util/os_network.c @@ -31,6 +31,10 @@ #include "gnunet_common.h" #include "gnunet_os_lib.h" + +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + + /** * @brief Enumerate all network interfaces * @@ -39,7 +43,7 @@ */ void GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, - void *proc_cls) + void *proc_cls) { #ifdef MINGW PMIB_IFTABLE pTable; @@ -48,118 +52,121 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, IPAddr theIP; /* Determine our external NIC */ - theIP = inet_addr ("192.0.34.166"); /* www.example.com */ + theIP = inet_addr ("192.0.34.166"); /* www.example.com */ if ((!GNGetBestInterface) || (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR)) - { - dwExternalNIC = 0; - } + { + dwExternalNIC = 0; + } /* Enumerate NICs */ EnumNICs (&pTable, &pAddrTable); if (pTable) - { - for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++) { - char szEntry[1001]; - DWORD dwIP = 0; - PIP_ADAPTER_INFO pAdapterInfo; - PIP_ADAPTER_INFO pAdapter = NULL; - DWORD dwRetVal = 0; - - /* Get IP-Address */ - int i; - - for (i = 0; i < pAddrTable->dwNumEntries; i++) - { - if (pAddrTable->table[i].dwIndex == pTable->table[dwIfIdx].dwIndex) - { - dwIP = pAddrTable->table[i].dwAddr; - break; - } - } - - if (dwIP) - { - BYTE bPhysAddr[MAXLEN_PHYSADDR]; - char *pszIfName = NULL; - char dst[INET_ADDRSTRLEN]; - struct sockaddr_in sa; - - /* Get friendly interface name */ - pAdapterInfo = (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO)); - ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); - - /* Make an initial call to GetAdaptersInfo to get - * the necessary size into the ulOutBufLen variable */ - if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) == - ERROR_BUFFER_OVERFLOW) - { - free (pAdapterInfo); - pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); - } - - if ((dwRetVal = - GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR) - { - pAdapter = pAdapterInfo; - while (pAdapter) - { - if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index) - { - char szKey[251]; - long lLen = 250; - - sprintf (szKey, - "SYSTEM\\CurrentControlSet\\Control\\Network\\" - "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection", - pAdapter->AdapterName); - pszIfName = (char *) malloc (251); - if (QueryRegistry - (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName, - &lLen) != ERROR_SUCCESS) - { - free (pszIfName); - pszIfName = NULL; - } - } - pAdapter = pAdapter->Next; - } - } - free (pAdapterInfo); - - /* Set entry */ - memset (bPhysAddr, 0, MAXLEN_PHYSADDR); - memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr, - pTable->table[dwIfIdx].dwPhysAddrLen); - - snprintf (szEntry, 1000, "%s (%s - %I64u)", - pszIfName ? pszIfName : (char *) pTable-> - table[dwIfIdx].bDescr, inet_ntop (AF_INET, &dwIP, dst, - INET_ADDRSTRLEN), - *((unsigned long long *) bPhysAddr)); - szEntry[1000] = 0; - - if (pszIfName) - free (pszIfName); - - sa.sin_family = AF_INET; + for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++) + { + char szEntry[1001]; + DWORD dwIP = 0; + PIP_ADAPTER_INFO pAdapterInfo; + PIP_ADAPTER_INFO pAdapter = NULL; + DWORD dwRetVal = 0; + + /* Get IP-Address */ + int i; + + for (i = 0; i < pAddrTable->dwNumEntries; i++) + { + if (pAddrTable->table[i].dwIndex == + pTable->table[dwIfIdx].dwIndex) + { + dwIP = pAddrTable->table[i].dwAddr; + break; + } + } + + if (dwIP) + { + BYTE bPhysAddr[MAXLEN_PHYSADDR]; + char *pszIfName = NULL; + char dst[INET_ADDRSTRLEN]; + struct sockaddr_in sa; + + /* Get friendly interface name */ + pAdapterInfo = + (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO)); + ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); + + /* Make an initial call to GetAdaptersInfo to get + * the necessary size into the ulOutBufLen variable */ + if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) == + ERROR_BUFFER_OVERFLOW) + { + free (pAdapterInfo); + pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); + } + + if ((dwRetVal = + GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR) + { + pAdapter = pAdapterInfo; + while (pAdapter) + { + if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index) + { + char szKey[251]; + long lLen = 250; + + sprintf (szKey, + "SYSTEM\\CurrentControlSet\\Control\\Network\\" + "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection", + pAdapter->AdapterName); + pszIfName = (char *) malloc (251); + if (QueryRegistry + (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName, + &lLen) != ERROR_SUCCESS) + { + free (pszIfName); + pszIfName = NULL; + } + } + pAdapter = pAdapter->Next; + } + } + free (pAdapterInfo); + + /* Set entry */ + memset (bPhysAddr, 0, MAXLEN_PHYSADDR); + memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr, + pTable->table[dwIfIdx].dwPhysAddrLen); + + snprintf (szEntry, 1000, "%s (%s - %I64u)", + pszIfName ? pszIfName : (char *) + pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET, + &dwIP, dst, + INET_ADDRSTRLEN), + *((unsigned long long *) bPhysAddr)); + szEntry[1000] = 0; + + if (pszIfName) + free (pszIfName); + + sa.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - sa.sin_len = (u_char) sizeof (struct sockaddr_in); + sa.sin_len = (u_char) sizeof (struct sockaddr_in); #endif - sa.sin_addr.S_un.S_addr = dwIP; - - if (GNUNET_OK != - proc (proc_cls, szEntry, - pTable->table[dwIfIdx].dwIndex == dwExternalNIC, - (const struct sockaddr *) &sa, sizeof (sa))) - break; - } + sa.sin_addr.S_un.S_addr = dwIP; + + if (GNUNET_OK != + proc (proc_cls, szEntry, + pTable->table[dwIfIdx].dwIndex == dwExternalNIC, + (const struct sockaddr *) &sa, sizeof (sa))) + break; + } + } + GlobalFree (pAddrTable); + GlobalFree (pTable); } - GlobalFree (pAddrTable); - GlobalFree (pTable); - } return; @@ -170,28 +177,29 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, socklen_t alen; if (getifaddrs (&ifa_first) == 0) - { - for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) { - if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL && - (ifa_ptr->ifa_flags & IFF_UP) != 0) - { - if ((ifa_ptr->ifa_addr->sa_family != AF_INET) && - (ifa_ptr->ifa_addr->sa_family != AF_INET6)) - continue; - if (ifa_ptr->ifa_addr->sa_family == AF_INET) - alen = sizeof (struct sockaddr_in); - else - alen = sizeof (struct sockaddr_in6); - if (GNUNET_OK != - proc (proc_cls, ifa_ptr->ifa_name, - 0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE), - ifa_ptr->ifa_addr, alen)) - break; - } + for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) + { + if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL && + (ifa_ptr->ifa_flags & IFF_UP) != 0) + { + if ((ifa_ptr->ifa_addr->sa_family != AF_INET) && + (ifa_ptr->ifa_addr->sa_family != AF_INET6)) + continue; + if (ifa_ptr->ifa_addr->sa_family == AF_INET) + alen = sizeof (struct sockaddr_in); + else + alen = sizeof (struct sockaddr_in6); + if (GNUNET_OK != + proc (proc_cls, ifa_ptr->ifa_name, + 0 == strcmp (ifa_ptr->ifa_name, + GNUNET_DEFAULT_INTERFACE), + ifa_ptr->ifa_addr, alen)) + break; + } + } + freeifaddrs (ifa_first); } - freeifaddrs (ifa_first); - } #else char line[1024]; const char *start; @@ -212,73 +220,76 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, else f = popen ("ifconfig -a 2> /dev/null", "r"); if (!f) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | - GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig"); - return; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | + GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig"); + return; + } have_ifc = GNUNET_NO; ifc[11] = '\0'; while (NULL != fgets (line, sizeof (line), f)) - { - if (strlen (line) == 0) - { - have_ifc = GNUNET_NO; - continue; - } - if (!isspace (line[0])) - { - have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO; - /* would end with ':' on OSX, fix it! */ - if (ifc[strlen (ifc) - 1] == ':') - ifc[strlen (ifc) - 1] = '\0'; - continue; - } - if (!have_ifc) - continue; /* strange input, hope for the best */ - start = line; - while (('\0' != *start) && (isspace (*start))) - start++; - if ( /* Linux */ - (1 == SSCANF (start, "inet addr:%127s", addrstr)) || - (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) || - /* Solaris, OS X */ - (1 == SSCANF (start, "inet %127s", addrstr)) || - (1 == SSCANF (start, "inet6 %127s", addrstr))) { - /* IPv4 */ - if (1 == inet_pton (AF_INET, addrstr, &v4)) - { - memset (&a4, 0, sizeof (a4)); - a4.sin_family = AF_INET; + if (strlen (line) == 0) + { + have_ifc = GNUNET_NO; + continue; + } + if (!isspace (line[0])) + { + have_ifc = + (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO; + /* would end with ':' on OSX, fix it! */ + if (ifc[strlen (ifc) - 1] == ':') + ifc[strlen (ifc) - 1] = '\0'; + continue; + } + if (!have_ifc) + continue; /* strange input, hope for the best */ + start = line; + while (('\0' != *start) && (isspace (*start))) + start++; + if ( /* Linux */ + (1 == SSCANF (start, "inet addr:%127s", addrstr)) || + (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) || + /* Solaris, OS X */ + (1 == SSCANF (start, "inet %127s", addrstr)) || + (1 == SSCANF (start, "inet6 %127s", addrstr))) + { + /* IPv4 */ + if (1 == inet_pton (AF_INET, addrstr, &v4)) + { + memset (&a4, 0, sizeof (a4)); + a4.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - a4.sin_len = (u_char) sizeof (struct sockaddr_in); + a4.sin_len = (u_char) sizeof (struct sockaddr_in); #endif - a4.sin_addr = v4; - if (GNUNET_OK != - proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), - (const struct sockaddr *) &a4, sizeof (a4))) - break; - continue; - } - /* IPv6 */ - if (1 == inet_pton (AF_INET6, addrstr, &v6)) - { - memset (&a6, 0, sizeof (a6)); - a6.sin6_family = AF_INET6; + a4.sin_addr = v4; + if (GNUNET_OK != + proc (proc_cls, ifc, + 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), + (const struct sockaddr *) &a4, sizeof (a4))) + break; + continue; + } + /* IPv6 */ + if (1 == inet_pton (AF_INET6, addrstr, &v6)) + { + memset (&a6, 0, sizeof (a6)); + a6.sin6_family = AF_INET6; #if HAVE_SOCKADDR_IN_SIN_LEN - a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); + a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); #endif - a6.sin6_addr = v6; - if (GNUNET_OK != - proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), - (const struct sockaddr *) &a6, sizeof (a6))) - break; - continue; - } + a6.sin6_addr = v6; + if (GNUNET_OK != + proc (proc_cls, ifc, + 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), + (const struct sockaddr *) &a6, sizeof (a6))) + break; + continue; + } + } } - } pclose (f); #endif } diff --git a/src/util/os_priority.c b/src/util/os_priority.c index 55a728ce6..4f21e7edb 100644 --- a/src/util/os_priority.c +++ b/src/util/os_priority.c @@ -30,6 +30,12 @@ #include "gnunet_scheduler_lib.h" #include "disk.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE" struct GNUNET_OS_Process @@ -53,42 +59,43 @@ static struct GNUNET_OS_Process current_process; */ static void parent_control_handler (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_DISK_FileHandle *control_pipe = - (struct GNUNET_DISK_FileHandle *) cls; + (struct GNUNET_DISK_FileHandle *) cls; int sig; #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", - __FUNCTION__, tc->reason); + LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", + __FUNCTION__, tc->reason); #endif if (tc->reason & (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT | GNUNET_SCHEDULER_REASON_PREREQ_DONE)) - { - GNUNET_DISK_npipe_close (control_pipe); - } - else - { - if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) != - sizeof (sig)) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read"); GNUNET_DISK_npipe_close (control_pipe); } - else + else { + if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) != + sizeof (sig)) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read"); + GNUNET_DISK_npipe_close (control_pipe); + } + else + { #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got control code %d from parent\n", - sig); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Got control code %d from parent\n", + sig); #endif - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - control_pipe, &parent_control_handler, - control_pipe); - raise (sig); + GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, + control_pipe, + &parent_control_handler, + control_pipe); + raise (sig); + } } - } } @@ -97,36 +104,35 @@ parent_control_handler (void *cls, */ void GNUNET_OS_install_parent_control_handler (void *cls, - const struct - GNUNET_SCHEDULER_TaskContext *tc) + const struct + GNUNET_SCHEDULER_TaskContext *tc) { const char *env_buf; struct GNUNET_DISK_FileHandle *control_pipe; env_buf = getenv (GNUNET_OS_CONTROL_PIPE); if ((env_buf == NULL) || (strlen (env_buf) <= 0)) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Not installing a handler because $%s=%s\n"), - GNUNET_OS_CONTROL_PIPE, env_buf); - return; - } + { + LOG (GNUNET_ERROR_TYPE_INFO, + _("Not installing a handler because $%s=%s\n"), + GNUNET_OS_CONTROL_PIPE, env_buf); + return; + } control_pipe = - GNUNET_DISK_npipe_open (env_buf, GNUNET_DISK_OPEN_READ, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_npipe_open (env_buf, GNUNET_DISK_OPEN_READ, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); if (control_pipe == NULL) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", env_buf); - return; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf); + return; + } #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding parent control handler pipe `%s' to the scheduler\n", - env_buf); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding parent control handler pipe `%s' to the scheduler\n", env_buf); #endif GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe, - &parent_control_handler, control_pipe); + &parent_control_handler, control_pipe); } @@ -160,90 +166,90 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig) ret = GNUNET_DISK_file_write (proc->control_pipe, &sig, sizeof (sig)); if (ret != sizeof (sig)) - { - if (errno == ECOMM) { - /* Child process is not controllable via pipe */ + if (errno == ECOMM) + { + /* Child process is not controllable via pipe */ #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Child process is not controllable, will kill it directly\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Child process is not controllable, will kill it directly\n"); #endif - } - else if (errno == EPIPE) - { + } + else if (errno == EPIPE) + { #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Failed to write into control pipe, because pipe is invalid (the child is most likely dead)\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Failed to write into control pipe, because pipe is invalid (the child is most likely dead)\n"); #endif - } - else - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Failed to write into control pipe , errno is %d\n", errno); + } + else + LOG (GNUNET_ERROR_TYPE_WARNING, + "Failed to write into control pipe , errno is %d\n", errno); #if WINDOWS && !defined(__CYGWIN__) - TerminateProcess (proc->handle, 0); + TerminateProcess (proc->handle, 0); #else - PLIBC_KILL (proc->pid, sig); + PLIBC_KILL (proc->pid, sig); #endif - } + } else - { + { #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Wrote control code into control pipe, now waiting\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Wrote control code into control pipe, now waiting\n"); #endif #if WINDOWS - /* Give it 3 seconds to die, then kill it in a nice Windows-specific way */ - if (WaitForSingleObject (proc->handle, 3000) != WAIT_OBJECT_0) - TerminateProcess (proc->handle, 0); - res = 0; + /* Give it 3 seconds to die, then kill it in a nice Windows-specific way */ + if (WaitForSingleObject (proc->handle, 3000) != WAIT_OBJECT_0) + TerminateProcess (proc->handle, 0); + res = 0; #else - struct GNUNET_NETWORK_FDSet *rfds; - struct GNUNET_NETWORK_FDSet *efds; - - rfds = GNUNET_NETWORK_fdset_create (); - efds = GNUNET_NETWORK_fdset_create (); - - GNUNET_NETWORK_fdset_handle_set (rfds, proc->control_pipe); - GNUNET_NETWORK_fdset_handle_set (efds, proc->control_pipe); - - /* Ndurner thought this up, and i have no idea what it does. - * There's have never been any code to answer the shutdown call - * (write a single int into the pipe, so that this function can read it). - * On *nix select() will probably tell that pipe is ready - * for reading, once the other process shuts down, - * but the read () call will fail, triggering a kill () - * on the pid that is already dead. This will probably result in non-0 - * return from kill(), and therefore from this function. - */ - while (1) - { - ret = - GNUNET_NETWORK_socket_select (rfds, NULL, efds, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_relative_get_unit (), - 5000)); - - if (ret < 1 || - GNUNET_NETWORK_fdset_handle_isset (efds, proc->control_pipe)) - { - /* Just to be sure */ - PLIBC_KILL (proc->pid, sig); - res = 0; - break; - } - else - { - if (GNUNET_DISK_file_read (proc->control_pipe, &ret, sizeof (ret)) != - GNUNET_OK) - res = PLIBC_KILL (proc->pid, sig); - - /* Child signaled shutdown is in progress */ - continue; - } - } + struct GNUNET_NETWORK_FDSet *rfds; + struct GNUNET_NETWORK_FDSet *efds; + + rfds = GNUNET_NETWORK_fdset_create (); + efds = GNUNET_NETWORK_fdset_create (); + + GNUNET_NETWORK_fdset_handle_set (rfds, proc->control_pipe); + GNUNET_NETWORK_fdset_handle_set (efds, proc->control_pipe); + + /* Ndurner thought this up, and i have no idea what it does. + * There's have never been any code to answer the shutdown call + * (write a single int into the pipe, so that this function can read it). + * On *nix select() will probably tell that pipe is ready + * for reading, once the other process shuts down, + * but the read () call will fail, triggering a kill () + * on the pid that is already dead. This will probably result in non-0 + * return from kill(), and therefore from this function. + */ + while (1) + { + ret = + GNUNET_NETWORK_socket_select (rfds, NULL, efds, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_relative_get_unit (), + 5000)); + + if (ret < 1 || + GNUNET_NETWORK_fdset_handle_isset (efds, proc->control_pipe)) + { + /* Just to be sure */ + PLIBC_KILL (proc->pid, sig); + res = 0; + break; + } + else + { + if (GNUNET_DISK_file_read + (proc->control_pipe, &ret, sizeof (ret)) != GNUNET_OK) + res = PLIBC_KILL (proc->pid, sig); + + /* Child signaled shutdown is in progress */ + continue; + } + } #endif - } + } return res; #else @@ -318,7 +324,7 @@ ChildWaitThread (void *arg) */ int GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc, - enum GNUNET_SCHEDULER_Priority prio) + enum GNUNET_SCHEDULER_Priority prio) { int rprio; @@ -328,51 +334,51 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc, /* convert to MINGW/Unix values */ switch (prio) - { - case GNUNET_SCHEDULER_PRIORITY_UI: - case GNUNET_SCHEDULER_PRIORITY_URGENT: + { + case GNUNET_SCHEDULER_PRIORITY_UI: + case GNUNET_SCHEDULER_PRIORITY_URGENT: #ifdef MINGW - rprio = HIGH_PRIORITY_CLASS; + rprio = HIGH_PRIORITY_CLASS; #else - rprio = 0; + rprio = 0; #endif - break; + break; - case GNUNET_SCHEDULER_PRIORITY_HIGH: + case GNUNET_SCHEDULER_PRIORITY_HIGH: #ifdef MINGW - rprio = ABOVE_NORMAL_PRIORITY_CLASS; + rprio = ABOVE_NORMAL_PRIORITY_CLASS; #else - rprio = 5; + rprio = 5; #endif - break; + break; - case GNUNET_SCHEDULER_PRIORITY_DEFAULT: + case GNUNET_SCHEDULER_PRIORITY_DEFAULT: #ifdef MINGW - rprio = NORMAL_PRIORITY_CLASS; + rprio = NORMAL_PRIORITY_CLASS; #else - rprio = 7; + rprio = 7; #endif - break; + break; - case GNUNET_SCHEDULER_PRIORITY_BACKGROUND: + case GNUNET_SCHEDULER_PRIORITY_BACKGROUND: #ifdef MINGW - rprio = BELOW_NORMAL_PRIORITY_CLASS; + rprio = BELOW_NORMAL_PRIORITY_CLASS; #else - rprio = 10; + rprio = 10; #endif - break; + break; - case GNUNET_SCHEDULER_PRIORITY_IDLE: + case GNUNET_SCHEDULER_PRIORITY_IDLE: #ifdef MINGW - rprio = IDLE_PRIORITY_CLASS; + rprio = IDLE_PRIORITY_CLASS; #else - rprio = 19; + rprio = 19; #endif - break; - default: - GNUNET_assert (0); - return GNUNET_SYSERR; - } + break; + default: + GNUNET_assert (0); + return GNUNET_SYSERR; + } /* Set process priority */ #ifdef MINGW @@ -387,31 +393,31 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc, pid = proc->pid; if ((0 == pid) || (pid == getpid ())) - { - int have = nice (0); - int delta = rprio - have; - - errno = 0; - if ((delta != 0) && (rprio == nice (delta)) && (errno != 0)) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, - "nice"); - return GNUNET_SYSERR; + int have = nice (0); + int delta = rprio - have; + + errno = 0; + if ((delta != 0) && (rprio == nice (delta)) && (errno != 0)) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, + "nice"); + return GNUNET_SYSERR; + } } - } else - { - if (0 != setpriority (PRIO_PROCESS, pid, rprio)) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, - "setpriority"); - return GNUNET_SYSERR; + if (0 != setpriority (PRIO_PROCESS, pid, rprio)) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, + "setpriority"); + return GNUNET_SYSERR; + } } - } #else #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, - "Priority management not availabe for this platform\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, + "Priority management not availabe for this platform\n"); #endif #endif return GNUNET_OK; @@ -434,84 +440,84 @@ CreateCustomEnvTable (char **vars) win32_env_table = GetEnvironmentStringsA (); if (win32_env_table == NULL) return NULL; - for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ; + for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++); n_var = c; index = GNUNET_malloc (sizeof (char *) * n_var); for (c = 0; c < n_var; c++) index[c] = 0; for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++) - { - size_t len = strlen (ptr); - int found = 0; - - for (var_ptr = vars; *var_ptr; var_ptr++) { - var = *var_ptr++; - val = *var_ptr; - var_len = strlen (var); - if (strncmp (var, ptr, var_len) == 0) - { - found = 1; - index[c] = 1; - tablesize += var_len + strlen (val) + 1; - break; - } + size_t len = strlen (ptr); + int found = 0; + + for (var_ptr = vars; *var_ptr; var_ptr++) + { + var = *var_ptr++; + val = *var_ptr; + var_len = strlen (var); + if (strncmp (var, ptr, var_len) == 0) + { + found = 1; + index[c] = 1; + tablesize += var_len + strlen (val) + 1; + break; + } + } + if (!found) + tablesize += len + 1; + ptr += len + 1; } - if (!found) - tablesize += len + 1; - ptr += len + 1; - } for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) - { - var = *var_ptr++; - val = *var_ptr; - if (index[c] != 1) - n_found += strlen (var) + strlen (val) + 1; - } - result = GNUNET_malloc (tablesize + n_found + 1); - for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;) - { - size_t len = strlen (ptr); - int found = 0; - - for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) { var = *var_ptr++; val = *var_ptr; - var_len = strlen (var); - if (strncmp (var, ptr, var_len) == 0) - { - found = 1; - break; - } - } - if (!found) - { - strcpy (result_ptr, ptr); - result_ptr += len + 1; + if (index[c] != 1) + n_found += strlen (var) + strlen (val) + 1; } - else + result = GNUNET_malloc (tablesize + n_found + 1); + for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;) { - strcpy (result_ptr, var); - result_ptr += var_len; - strcpy (result_ptr, val); - result_ptr += strlen (val) + 1; + size_t len = strlen (ptr); + int found = 0; + + for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) + { + var = *var_ptr++; + val = *var_ptr; + var_len = strlen (var); + if (strncmp (var, ptr, var_len) == 0) + { + found = 1; + break; + } + } + if (!found) + { + strcpy (result_ptr, ptr); + result_ptr += len + 1; + } + else + { + strcpy (result_ptr, var); + result_ptr += var_len; + strcpy (result_ptr, val); + result_ptr += strlen (val) + 1; + } + ptr += len + 1; } - ptr += len + 1; - } for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) - { - var = *var_ptr++; - val = *var_ptr; - var_len = strlen (var); - if (index[c] != 1) { - strcpy (result_ptr, var); - result_ptr += var_len; - strcpy (result_ptr, val); - result_ptr += strlen (val) + 1; + var = *var_ptr++; + val = *var_ptr; + var_len = strlen (var); + if (index[c] != 1) + { + strcpy (result_ptr, var); + result_ptr += var_len; + strcpy (result_ptr, val); + result_ptr += strlen (val) + 1; + } } - } FreeEnvironmentStrings (win32_env_table); GNUNET_free (index); *result_ptr = 0; @@ -531,8 +537,8 @@ CreateCustomEnvTable (char **vars) */ struct GNUNET_OS_Process * GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, - struct GNUNET_DISK_PipeHandle *pipe_stdout, - const char *filename, va_list va) + struct GNUNET_DISK_PipeHandle *pipe_stdout, + const char *filename, va_list va) { va_list ap; @@ -553,9 +559,9 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, #if ENABLE_WINDOWS_WORKAROUNDS control_pipe = - GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); if (control_pipe == NULL) return NULL; #endif @@ -563,35 +569,38 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, argc = 0; va_copy (ap, va); while (NULL != va_arg (ap, char *)) - argc++; + argc++; va_end (ap); argv = GNUNET_malloc (sizeof (char *) * (argc + 1)); argc = 0; va_copy (ap, va); while (NULL != (argv[argc] = va_arg (ap, char *))) - argc++; + argc++; va_end (ap); if (pipe_stdout != NULL) - { - GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle - (pipe_stdout, - GNUNET_DISK_PIPE_END_WRITE), - &fd_stdout_write, sizeof (int)); - GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle - (pipe_stdout, GNUNET_DISK_PIPE_END_READ), - &fd_stdout_read, sizeof (int)); - } + { + GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle + (pipe_stdout, + GNUNET_DISK_PIPE_END_WRITE), + &fd_stdout_write, sizeof (int)); + GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle + (pipe_stdout, + GNUNET_DISK_PIPE_END_READ), + &fd_stdout_read, sizeof (int)); + } if (pipe_stdin != NULL) - { - GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle - (pipe_stdin, GNUNET_DISK_PIPE_END_READ), - &fd_stdin_read, sizeof (int)); - GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle - (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), - &fd_stdin_write, sizeof (int)); - } + { + GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle + (pipe_stdin, + GNUNET_DISK_PIPE_END_READ), + &fd_stdin_read, sizeof (int)); + GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle + (pipe_stdin, + GNUNET_DISK_PIPE_END_WRITE), + &fd_stdin_write, sizeof (int)); + } #if HAVE_WORKING_VFORK ret = vfork (); @@ -599,44 +608,46 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, ret = fork (); #endif if (ret != 0) - { - if (ret == -1) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); + if (ret == -1) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); #if ENABLE_WINDOWS_WORKAROUNDS - GNUNET_DISK_npipe_close (control_pipe); + GNUNET_DISK_npipe_close (control_pipe); #endif - } - else - { + } + else + { #if HAVE_WORKING_VFORK - /* let's hope vfork actually works; for some extreme cases (including - * a testcase) we need 'execvp' to have run before we return, since - * we may send a signal to the process next and we don't want it - * to be caught by OUR signal handler (but either by the default - * handler or the actual handler as installed by the process itself). */ + /* let's hope vfork actually works; for some extreme cases (including + * a testcase) we need 'execvp' to have run before we return, since + * we may send a signal to the process next and we don't want it + * to be caught by OUR signal handler (but either by the default + * handler or the actual handler as installed by the process itself). */ #else - /* let's give the child process a chance to run execvp, 1s should - * be plenty in practice */ - if (pipe_stdout != NULL) - GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); - if (pipe_stdin != NULL) - GNUNET_DISK_pipe_close_end (pipe_stdin, GNUNET_DISK_PIPE_END_READ); - sleep (1); + /* let's give the child process a chance to run execvp, 1s should + * be plenty in practice */ + if (pipe_stdout != NULL) + GNUNET_DISK_pipe_close_end (pipe_stdout, + GNUNET_DISK_PIPE_END_WRITE); + if (pipe_stdin != NULL) + GNUNET_DISK_pipe_close_end (pipe_stdin, + GNUNET_DISK_PIPE_END_READ); + sleep (1); #endif - gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); - gnunet_proc->pid = ret; + gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); + gnunet_proc->pid = ret; #if ENABLE_WINDOWS_WORKAROUNDS - gnunet_proc->control_pipe = control_pipe; + gnunet_proc->control_pipe = control_pipe; #endif - } - GNUNET_free (argv); + } + GNUNET_free (argv); #if ENABLE_WINDOWS_WORKAROUNDS - GNUNET_free (childpipename); + GNUNET_free (childpipename); #endif - return gnunet_proc; - } + return gnunet_proc; + } #if ENABLE_WINDOWS_WORKAROUNDS setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1); @@ -644,23 +655,23 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, #endif if (pipe_stdout != NULL) - { - GNUNET_break (0 == close (fd_stdout_read)); - if (-1 == dup2 (fd_stdout_write, 1)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2"); - GNUNET_break (0 == close (fd_stdout_write)); - } + { + GNUNET_break (0 == close (fd_stdout_read)); + if (-1 == dup2 (fd_stdout_write, 1)) + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2"); + GNUNET_break (0 == close (fd_stdout_write)); + } if (pipe_stdin != NULL) - { + { - GNUNET_break (0 == close (fd_stdin_write)); - if (-1 == dup2 (fd_stdin_read, 0)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2"); - GNUNET_break (0 == close (fd_stdin_read)); - } + GNUNET_break (0 == close (fd_stdin_write)); + if (-1 == dup2 (fd_stdin_read, 0)) + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2"); + GNUNET_break (0 == close (fd_stdin_read)); + } execvp (filename, argv); - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); _exit (1); #else char *arg; @@ -694,8 +705,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0); alloc_len = - pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + - strlen (libdir); + pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + + strlen (libdir); pathbuf = GNUNET_malloc (alloc_len * sizeof (char)); @@ -718,39 +729,39 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, if (non_const_filename[1] == ':') snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename); else if (!SearchPathA - (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), - path, NULL)) - { - SetErrnoFromWinError (GetLastError ()); - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "SearchPath", - non_const_filename); - GNUNET_free (non_const_filename); - GNUNET_free (pathbuf); - return NULL; - } + (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), + path, NULL)) + { + SetErrnoFromWinError (GetLastError ()); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath", + non_const_filename); + GNUNET_free (non_const_filename); + GNUNET_free (pathbuf); + return NULL; + } GNUNET_free (pathbuf); GNUNET_free (non_const_filename); cmdlen = 0; va_copy (ap, va); while (NULL != (arg = va_arg (ap, char *))) - { - if (cmdlen == 0) - cmdlen = cmdlen + strlen (path) + 3; - else - cmdlen = cmdlen + strlen (arg) + 3; - } + { + if (cmdlen == 0) + cmdlen = cmdlen + strlen (path) + 3; + else + cmdlen = cmdlen + strlen (arg) + 3; + } va_end (ap); cmd = idx = GNUNET_malloc (sizeof (char) * (cmdlen + 1)); va_copy (ap, va); while (NULL != (arg = va_arg (ap, char *))) - { - if (idx == cmd) - idx += sprintf (idx, "\"%s\" ", path); - else - idx += sprintf (idx, "\"%s\" ", arg); - } + { + if (idx == cmd) + idx += sprintf (idx, "\"%s\" ", path); + else + idx += sprintf (idx, "\"%s\" ", arg); + } va_end (ap); memset (&start, 0, sizeof (start)); @@ -760,36 +771,37 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, start.dwFlags |= STARTF_USESTDHANDLES; if (pipe_stdin != NULL) - { - GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle - (pipe_stdin, GNUNET_DISK_PIPE_END_READ), - &stdin_handle, sizeof (HANDLE)); - start.hStdInput = stdin_handle; - } + { + GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle + (pipe_stdin, + GNUNET_DISK_PIPE_END_READ), + &stdin_handle, sizeof (HANDLE)); + start.hStdInput = stdin_handle; + } if (pipe_stdout != NULL) - { - GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle - (pipe_stdout, - GNUNET_DISK_PIPE_END_WRITE), - &stdout_handle, sizeof (HANDLE)); - start.hStdOutput = stdout_handle; - } + { + GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle + (pipe_stdout, + GNUNET_DISK_PIPE_END_WRITE), + &stdout_handle, sizeof (HANDLE)); + start.hStdOutput = stdout_handle; + } control_pipe = - GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); if (control_pipe == NULL) - { - GNUNET_free (cmd); - GNUNET_free (path); - return NULL; - } + { + GNUNET_free (cmd); + GNUNET_free (path); + return NULL; + } #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Opened the parent end of the pipe `%s'\n", childpipename); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Opened the parent end of the pipe `%s'\n", childpipename); #endif GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE); @@ -802,13 +814,13 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, if (!CreateProcessA (path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | CREATE_SUSPENDED, env_block, NULL, &start, &proc)) - { - SetErrnoFromWinError (GetLastError ()); - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path); - GNUNET_free (env_block); - GNUNET_free (cmd); - return NULL; - } + { + SetErrnoFromWinError (GetLastError ()); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path); + GNUNET_free (env_block); + GNUNET_free (cmd); + return NULL; + } GNUNET_free (env_block); @@ -842,8 +854,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, */ struct GNUNET_OS_Process * GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, - struct GNUNET_DISK_PipeHandle *pipe_stdout, - const char *filename, ...) + struct GNUNET_DISK_PipeHandle *pipe_stdout, + const char *filename, ...) { struct GNUNET_OS_Process *ret; va_list ap; @@ -866,7 +878,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, */ struct GNUNET_OS_Process * GNUNET_OS_start_process_v (const int *lsocks, const char *filename, - char *const argv[]) + char *const argv[]) { #if ENABLE_WINDOWS_WORKAROUNDS struct GNUNET_DISK_FileHandle *control_pipe = NULL; @@ -888,9 +900,9 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, #if ENABLE_WINDOWS_WORKAROUNDS control_pipe = - GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); if (control_pipe == NULL) return NULL; #endif @@ -898,52 +910,52 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, lscp = NULL; ls = 0; if (lsocks != NULL) - { - i = 0; - while (-1 != (k = lsocks[i++])) - GNUNET_array_append (lscp, ls, k); - GNUNET_array_append (lscp, ls, -1); - } + { + i = 0; + while (-1 != (k = lsocks[i++])) + GNUNET_array_append (lscp, ls, k); + GNUNET_array_append (lscp, ls, -1); + } #if HAVE_WORKING_VFORK ret = vfork (); #else ret = fork (); #endif if (ret != 0) - { - if (ret == -1) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); + if (ret == -1) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); #if ENABLE_WINDOWS_WORKAROUNDS - GNUNET_DISK_npipe_close (control_pipe); + GNUNET_DISK_npipe_close (control_pipe); #endif - } - else - { + } + else + { #if HAVE_WORKING_VFORK - /* let's hope vfork actually works; for some extreme cases (including - * a testcase) we need 'execvp' to have run before we return, since - * we may send a signal to the process next and we don't want it - * to be caught by OUR signal handler (but either by the default - * handler or the actual handler as installed by the process itself). */ + /* let's hope vfork actually works; for some extreme cases (including + * a testcase) we need 'execvp' to have run before we return, since + * we may send a signal to the process next and we don't want it + * to be caught by OUR signal handler (but either by the default + * handler or the actual handler as installed by the process itself). */ #else - /* let's give the child process a chance to run execvp, 1s should - * be plenty in practice */ - sleep (1); + /* let's give the child process a chance to run execvp, 1s should + * be plenty in practice */ + sleep (1); #endif - gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); - gnunet_proc->pid = ret; + gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); + gnunet_proc->pid = ret; #if ENABLE_WINDOWS_WORKAROUNDS - gnunet_proc->control_pipe = control_pipe; + gnunet_proc->control_pipe = control_pipe; #endif - } - GNUNET_array_grow (lscp, ls, 0); + } + GNUNET_array_grow (lscp, ls, 0); #if ENABLE_WINDOWS_WORKAROUNDS - GNUNET_free (childpipename); + GNUNET_free (childpipename); #endif - return gnunet_proc; - } + return gnunet_proc; + } #if ENABLE_WINDOWS_WORKAROUNDS setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1); @@ -951,50 +963,50 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, #endif if (lscp != NULL) - { - /* read systemd documentation... */ - GNUNET_snprintf (lpid, sizeof (lpid), "%u", getpid ()); - setenv ("LISTEN_PID", lpid, 1); - i = 0; - tgt = 3; - while (-1 != lscp[i]) { - j = i + 1; - while (-1 != lscp[j]) - { - if (lscp[j] == tgt) - { - /* dup away */ - k = dup (lscp[j]); - GNUNET_assert (-1 != k); - GNUNET_assert (0 == close (lscp[j])); - lscp[j] = k; - break; - } - j++; - } - if (lscp[i] != tgt) - { - /* Bury any existing FD, no matter what; they should all be closed - * on exec anyway and the important onces have been dup'ed away */ - (void) close (tgt); - GNUNET_assert (-1 != dup2 (lscp[i], tgt)); - } - /* unset close-on-exec flag */ - flags = fcntl (tgt, F_GETFD); - GNUNET_assert (flags >= 0); - flags &= ~FD_CLOEXEC; - fflush (stderr); - (void) fcntl (tgt, F_SETFD, flags); - tgt++; - i++; + /* read systemd documentation... */ + GNUNET_snprintf (lpid, sizeof (lpid), "%u", getpid ()); + setenv ("LISTEN_PID", lpid, 1); + i = 0; + tgt = 3; + while (-1 != lscp[i]) + { + j = i + 1; + while (-1 != lscp[j]) + { + if (lscp[j] == tgt) + { + /* dup away */ + k = dup (lscp[j]); + GNUNET_assert (-1 != k); + GNUNET_assert (0 == close (lscp[j])); + lscp[j] = k; + break; + } + j++; + } + if (lscp[i] != tgt) + { + /* Bury any existing FD, no matter what; they should all be closed + * on exec anyway and the important onces have been dup'ed away */ + (void) close (tgt); + GNUNET_assert (-1 != dup2 (lscp[i], tgt)); + } + /* unset close-on-exec flag */ + flags = fcntl (tgt, F_GETFD); + GNUNET_assert (flags >= 0); + flags &= ~FD_CLOEXEC; + fflush (stderr); + (void) fcntl (tgt, F_SETFD, flags); + tgt++; + i++; + } + GNUNET_snprintf (fds, sizeof (fds), "%u", i); + setenv ("LISTEN_FDS", fds, 1); } - GNUNET_snprintf (fds, sizeof (fds), "%u", i); - setenv ("LISTEN_FDS", fds, 1); - } GNUNET_array_grow (lscp, ls, 0); execvp (filename, argv); - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); _exit (1); #else char **arg, **non_const_argv; @@ -1029,8 +1041,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0); alloc_len = - pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + - strlen (libdir); + pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + + strlen (libdir); pathbuf = GNUNET_malloc (alloc_len * sizeof (char)); @@ -1042,11 +1054,11 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len); if (alloc_len != pathbuf_len - 1) - { - GNUNET_free (pathbuf); - errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */ - return NULL; - } + { + GNUNET_free (pathbuf); + errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */ + return NULL; + } cmdlen = strlen (filename); if (cmdlen < 5 || strcmp (&filename[cmdlen - 4], ".exe") != 0) @@ -1058,26 +1070,26 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, if (non_const_filename[1] == ':') snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename); else if (!SearchPathA - (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), - path, NULL)) - { - SetErrnoFromWinError (GetLastError ()); - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "SearchPath", - non_const_filename); - GNUNET_free (non_const_filename); - GNUNET_free (pathbuf); - return NULL; - } + (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), + path, NULL)) + { + SetErrnoFromWinError (GetLastError ()); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath", + non_const_filename); + GNUNET_free (non_const_filename); + GNUNET_free (pathbuf); + return NULL; + } GNUNET_free (pathbuf); GNUNET_free (non_const_filename); /* Count the number of arguments */ arg = (char **) argv; while (*arg) - { - arg++; - argcount++; - } + { + arg++; + argcount++; + } /* Allocate a copy argv */ non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1)); @@ -1086,33 +1098,33 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, argcount = 0; arg = (char **) argv; while (*arg) - { - if (arg == argv) - non_const_argv[argcount] = GNUNET_strdup (path); - else - non_const_argv[argcount] = GNUNET_strdup (*arg); - arg++; - argcount++; - } + { + if (arg == argv) + non_const_argv[argcount] = GNUNET_strdup (path); + else + non_const_argv[argcount] = GNUNET_strdup (*arg); + arg++; + argcount++; + } non_const_argv[argcount] = NULL; /* Count cmd len */ cmdlen = 1; arg = non_const_argv; while (*arg) - { - cmdlen = cmdlen + strlen (*arg) + 3; - arg++; - } + { + cmdlen = cmdlen + strlen (*arg) + 3; + arg++; + } /* Allocate and create cmd */ cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen); arg = non_const_argv; while (*arg) - { - idx += sprintf (idx, "\"%s\" ", *arg); - arg++; - } + { + idx += sprintf (idx, "\"%s\" ", *arg); + arg++; + } while (argcount > 0) GNUNET_free (non_const_argv[--argcount]); @@ -1122,19 +1134,19 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, start.cb = sizeof (start); control_pipe = - GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); if (control_pipe == NULL) - { - GNUNET_free (cmd); - GNUNET_free (path); - return NULL; - } + { + GNUNET_free (cmd); + GNUNET_free (path); + return NULL; + } #if DEBUG_OS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Opened the parent end of the pipe `%s'\n", childpipename); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Opened the parent end of the pipe `%s'\n", childpipename); #endif GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE); @@ -1147,13 +1159,13 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, if (!CreateProcess (path, cmd, NULL, NULL, FALSE, DETACHED_PROCESS | CREATE_SUSPENDED, env_block, NULL, &start, &proc)) - { - SetErrnoFromWinError (GetLastError ()); - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "CreateProcess"); - GNUNET_free (env_block); - GNUNET_free (cmd); - return NULL; - } + { + SetErrnoFromWinError (GetLastError ()); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "CreateProcess"); + GNUNET_free (env_block); + GNUNET_free (cmd); + return NULL; + } GNUNET_free (env_block); @@ -1182,8 +1194,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename, */ int GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, - enum GNUNET_OS_ProcessStatusType *type, - unsigned long *code) + enum GNUNET_OS_ProcessStatusType *type, + unsigned long *code) { #ifndef MINGW int status; @@ -1192,48 +1204,48 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, GNUNET_assert (0 != proc); ret = waitpid (proc->pid, &status, WNOHANG); if (ret < 0) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid"); + return GNUNET_SYSERR; + } if (0 == ret) - { - *type = GNUNET_OS_PROCESS_RUNNING; - *code = 0; - return GNUNET_NO; - } + { + *type = GNUNET_OS_PROCESS_RUNNING; + *code = 0; + return GNUNET_NO; + } if (proc->pid != ret) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid"); + return GNUNET_SYSERR; + } if (WIFEXITED (status)) - { - *type = GNUNET_OS_PROCESS_EXITED; - *code = WEXITSTATUS (status); - } + { + *type = GNUNET_OS_PROCESS_EXITED; + *code = WEXITSTATUS (status); + } else if (WIFSIGNALED (status)) - { - *type = GNUNET_OS_PROCESS_SIGNALED; - *code = WTERMSIG (status); - } + { + *type = GNUNET_OS_PROCESS_SIGNALED; + *code = WTERMSIG (status); + } else if (WIFSTOPPED (status)) - { - *type = GNUNET_OS_PROCESS_SIGNALED; - *code = WSTOPSIG (status); - } + { + *type = GNUNET_OS_PROCESS_SIGNALED; + *code = WSTOPSIG (status); + } #ifdef WIFCONTINUED else if (WIFCONTINUED (status)) - { - *type = GNUNET_OS_PROCESS_RUNNING; - *code = 0; - } + { + *type = GNUNET_OS_PROCESS_RUNNING; + *code = 0; + } #endif else - { - *type = GNUNET_OS_PROCESS_UNKNOWN; - *code = 0; - } + { + *type = GNUNET_OS_PROCESS_UNKNOWN; + *code = 0; + } #else HANDLE h; DWORD c, error_code, ret; @@ -1241,11 +1253,11 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, h = proc->handle; ret = proc->pid; if (h == NULL || ret == 0) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Invalid process information {%d, %08X}\n", ret, h); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + "Invalid process information {%d, %08X}\n", ret, h); + return GNUNET_SYSERR; + } if (h == NULL) h = GetCurrentProcess (); @@ -1253,17 +1265,17 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, ret = GetExitCodeProcess (h, &c); error_code = GetLastError (); if (ret == 0 || error_code != NO_ERROR) - { - SetErrnoFromWinError (error_code); - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess"); - return GNUNET_SYSERR; - } + { + SetErrnoFromWinError (error_code); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess"); + return GNUNET_SYSERR; + } if (STILL_ACTIVE == c) - { - *type = GNUNET_OS_PROCESS_RUNNING; - *code = 0; - return GNUNET_NO; - } + { + *type = GNUNET_OS_PROCESS_RUNNING; + *code = 0; + return GNUNET_NO; + } *type = GNUNET_OS_PROCESS_EXITED; *code = c; #endif @@ -1293,19 +1305,19 @@ GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc) h = proc->handle; if (NULL == h) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Invalid process information {%d, %08X}\n", proc->pid, h); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + "Invalid process information {%d, %08X}\n", proc->pid, h); + return GNUNET_SYSERR; + } if (h == NULL) h = GetCurrentProcess (); if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE)) - { - SetErrnoFromWinError (GetLastError ()); - ret = GNUNET_SYSERR; - } + { + SetErrnoFromWinError (GetLastError ()); + ret = GNUNET_SYSERR; + } else ret = GNUNET_OK; @@ -1379,10 +1391,10 @@ GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd) { if (cmd->proc != NULL) - { - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask); - GNUNET_SCHEDULER_cancel (cmd->rtask); - } + { + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask); + GNUNET_SCHEDULER_cancel (cmd->rtask); + } (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL); GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip)); GNUNET_OS_process_close (cmd->eip); @@ -1406,42 +1418,43 @@ cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) ssize_t ret; cmd->rtask = GNUNET_SCHEDULER_NO_TASK; - if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r)) - { - /* timeout, shutdown, etc. */ - proc = cmd->proc; - cmd->proc = NULL; - proc (cmd->proc_cls, NULL); - return; - } + if (GNUNET_YES != + GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r)) + { + /* timeout, shutdown, etc. */ + proc = cmd->proc; + cmd->proc = NULL; + proc (cmd->proc_cls, NULL); + return; + } ret = - GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off], - sizeof (cmd->buf) - cmd->off); + GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off], + sizeof (cmd->buf) - cmd->off); if (ret <= 0) - { - if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf))) { - cmd->buf[cmd->off] = '\0'; - cmd->proc (cmd->proc_cls, cmd->buf); + if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf))) + { + cmd->buf[cmd->off] = '\0'; + cmd->proc (cmd->proc_cls, cmd->buf); + } + proc = cmd->proc; + cmd->proc = NULL; + proc (cmd->proc_cls, NULL); + return; } - proc = cmd->proc; - cmd->proc = NULL; - proc (cmd->proc_cls, NULL); - return; - } end = memchr (&cmd->buf[cmd->off], '\n', ret); cmd->off += ret; while (end != NULL) - { - *end = '\0'; - cmd->proc (cmd->proc_cls, cmd->buf); - memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf)); - cmd->off -= (end + 1 - cmd->buf); - end = memchr (cmd->buf, '\n', cmd->off); - } + { + *end = '\0'; + cmd->proc (cmd->proc_cls, cmd->buf); + memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf)); + cmd->off -= (end + 1 - cmd->buf); + end = memchr (cmd->buf, '\n', cmd->off); + } cmd->rtask = - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining - (cmd->timeout), cmd->r, &cmd_read, cmd); + GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining + (cmd->timeout), cmd->r, &cmd_read, cmd); } @@ -1458,8 +1471,8 @@ cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ struct GNUNET_OS_CommandHandle * GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls, - struct GNUNET_TIME_Relative timeout, const char *binary, - ...) + struct GNUNET_TIME_Relative timeout, + const char *binary, ...) { struct GNUNET_OS_CommandHandle *cmd; struct GNUNET_OS_Process *eip; @@ -1473,10 +1486,10 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls, eip = GNUNET_OS_start_process_va (NULL, opipe, binary, ap); va_end (ap); if (NULL == eip) - { - GNUNET_DISK_pipe_close (opipe); - return NULL; - } + { + GNUNET_DISK_pipe_close (opipe); + return NULL; + } GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE); cmd = GNUNET_malloc (sizeof (struct GNUNET_OS_CommandHandle)); cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout); @@ -1485,7 +1498,8 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls, cmd->proc = proc; cmd->proc_cls = proc_cls; cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ); - cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd); + cmd->rtask = + GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd); return cmd; } diff --git a/src/util/peer.c b/src/util/peer.c index 39d11883d..c88e03c15 100644 --- a/src/util/peer.c +++ b/src/util/peer.c @@ -27,6 +27,8 @@ #include "platform.h" #include "gnunet_peer_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + struct PeerEntry { @@ -105,7 +107,7 @@ GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid) * @return the interned identity. */ GNUNET_PEER_Id -GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid) +GNUNET_PEER_intern (const struct GNUNET_PeerIdentity * pid) { GNUNET_PEER_Id ret; struct PeerEntry *e; @@ -119,24 +121,24 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid) off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey); e = (off == 0) ? NULL : &table[off]; if (e != NULL) - { - GNUNET_assert (e->rc > 0); - e->rc++; - return e->pid; - } + { + GNUNET_assert (e->rc > 0); + e->rc++; + return e->pid; + } ret = free_list_start; if (ret == size) - { - GNUNET_array_grow (table, size, size + 16); - for (i = ret; i < size; i++) - table[i].pid = i + 1; - } + { + GNUNET_array_grow (table, size, size + 16); + for (i = ret; i < size; i++) + table[i].pid = i + 1; + } if (ret == 0) - { - table[0].pid = 0; - table[0].rc = 1; - ret = 1; - } + { + table[0].pid = 0; + table[0].rc = 1; + ret = 1; + } GNUNET_assert (ret < size); GNUNET_assert (table[ret].rc == 0); free_list_start = table[ret].pid; @@ -144,9 +146,9 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid) table[ret].rc = 1; table[ret].pid = ret; GNUNET_break (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (map, &pid->hashPubKey, - (void *) (long) ret, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + GNUNET_CONTAINER_multihashmap_put (map, &pid->hashPubKey, + (void *) (long) ret, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); return ret; } @@ -158,7 +160,7 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid) * @param count size of the ids array */ void -GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count) +GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, unsigned int count) { int i; GNUNET_PEER_Id id; @@ -166,24 +168,25 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count) if (count == 0) return; for (i = count - 1; i >= 0; i--) - { - id = ids[i]; - if (id == 0) - continue; - GNUNET_assert (id < size); - GNUNET_assert (table[id].rc > 0); - table[id].rc--; - if (table[id].rc == 0) { - GNUNET_break (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_remove (map, - &table[id]. - id.hashPubKey, - (void *) (long) id)); - table[id].pid = free_list_start; - free_list_start = id; + id = ids[i]; + if (id == 0) + continue; + GNUNET_assert (id < size); + GNUNET_assert (table[id].rc > 0); + table[id].rc--; + if (table[id].rc == 0) + { + GNUNET_break (GNUNET_OK == + GNUNET_CONTAINER_multihashmap_remove (map, + &table[id]. + id.hashPubKey, + (void *) (long) + id)); + table[id].pid = free_list_start; + free_list_start = id; + } } - } } @@ -203,15 +206,16 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta) GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta)); table[id].rc += delta; if (table[id].rc == 0) - { - GNUNET_break (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_remove (map, - &table[id]. - id.hashPubKey, - (void *) (long) id)); - table[id].pid = free_list_start; - free_list_start = id; - } + { + GNUNET_break (GNUNET_OK == + GNUNET_CONTAINER_multihashmap_remove (map, + &table[id]. + id.hashPubKey, + (void *) (long) + id)); + table[id].pid = free_list_start; + free_list_start = id; + } } @@ -225,11 +229,11 @@ void GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) { if (id == 0) - { - memset (pid, 0, sizeof (struct GNUNET_PeerIdentity)); - GNUNET_break (0); - return; - } + { + memset (pid, 0, sizeof (struct GNUNET_PeerIdentity)); + GNUNET_break (0); + return; + } GNUNET_assert (id < size); GNUNET_assert (table[id].rc > 0); *pid = table[id].id; diff --git a/src/util/perf_crypto_hash.c b/src/util/perf_crypto_hash.c index d883776b4..746b33eca 100644 --- a/src/util/perf_crypto_hash.c +++ b/src/util/perf_crypto_hash.c @@ -42,11 +42,11 @@ perfHash () memset (buf, 1, 1024 * 64); GNUNET_CRYPTO_hash ("foo", 3, &hc1); for (i = 0; i < 1024; i++) - { - GNUNET_CRYPTO_hash (&hc1, sizeof (GNUNET_HashCode), &hc2); - GNUNET_CRYPTO_hash (&hc2, sizeof (GNUNET_HashCode), &hc1); - GNUNET_CRYPTO_hash (buf, 1024 * 64, &hc3); - } + { + GNUNET_CRYPTO_hash (&hc1, sizeof (GNUNET_HashCode), &hc2); + GNUNET_CRYPTO_hash (&hc2, sizeof (GNUNET_HashCode), &hc1); + GNUNET_CRYPTO_hash (buf, 1024 * 64, &hc3); + } GNUNET_free (buf); } @@ -58,12 +58,12 @@ main (int argc, char *argv[]) start = GNUNET_TIME_absolute_get (); perfHash (); printf ("Hash perf took %llu ms\n", - (unsigned long long) - GNUNET_TIME_absolute_get_duration (start).rel_value); + (unsigned long long) + GNUNET_TIME_absolute_get_duration (start).rel_value); GAUGER ("UTIL", "Cryptographic hashing", - 1024 * 64 * 1024 / (1 + - GNUNET_TIME_absolute_get_duration - (start).rel_value), "kb/s"); + 1024 * 64 * 1024 / (1 + + GNUNET_TIME_absolute_get_duration + (start).rel_value), "kb/s"); return 0; } diff --git a/src/util/plugin.c b/src/util/plugin.c index dbd3519aa..77631410c 100644 --- a/src/util/plugin.c +++ b/src/util/plugin.c @@ -30,6 +30,8 @@ #include "gnunet_os_lib.h" #include "gnunet_plugin_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Linked list of active plugins. */ @@ -83,30 +85,30 @@ plugin_init () err = lt_dlinit (); if (err > 0) - { - fprintf (stderr, _("Initialization of plugin mechanism failed: %s!\n"), - lt_dlerror ()); - return; - } + { + fprintf (stderr, _("Initialization of plugin mechanism failed: %s!\n"), + lt_dlerror ()); + return; + } opath = lt_dlgetsearchpath (); if (opath != NULL) old_dlsearchpath = GNUNET_strdup (opath); path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); if (path != NULL) - { - if (opath != NULL) { - GNUNET_asprintf (&cpath, "%s:%s", opath, path); - lt_dlsetsearchpath (cpath); - GNUNET_free (path); - GNUNET_free (cpath); + if (opath != NULL) + { + GNUNET_asprintf (&cpath, "%s:%s", opath, path); + lt_dlsetsearchpath (cpath); + GNUNET_free (path); + GNUNET_free (cpath); + } + else + { + lt_dlsetsearchpath (path); + GNUNET_free (path); + } } - else - { - lt_dlsetsearchpath (path); - GNUNET_free (path); - } - } } @@ -118,10 +120,10 @@ plugin_fini () { lt_dlsetsearchpath (old_dlsearchpath); if (old_dlsearchpath != NULL) - { - GNUNET_free (old_dlsearchpath); - old_dlsearchpath = NULL; - } + { + GNUNET_free (old_dlsearchpath); + old_dlsearchpath = NULL; + } lt_dlexit (); } @@ -140,9 +142,9 @@ resolve_function (struct PluginList *plug, const char *name) if (mptr == NULL) mptr = lt_dlsym (plug->handle, initName); if (mptr == NULL) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("`%s' failed to resolve method '%s' with error: %s\n"), - "lt_dlsym", &initName[1], lt_dlerror ()); + LOG (GNUNET_ERROR_TYPE_ERROR, + _("`%s' failed to resolve method '%s' with error: %s\n"), + "lt_dlsym", &initName[1], lt_dlerror ()); GNUNET_free (initName); return mptr; } @@ -164,10 +166,10 @@ GNUNET_PLUGIN_test (const char *library_name) struct PluginList plug; if (!initialized) - { - initialized = GNUNET_YES; - plugin_init (); - } + { + initialized = GNUNET_YES; + plugin_init (); + } libhandle = lt_dlopenext (library_name); if (libhandle == NULL) return GNUNET_NO; @@ -175,11 +177,11 @@ GNUNET_PLUGIN_test (const char *library_name) plug.name = (char *) library_name; init = resolve_function (&plug, "init"); if (init == NULL) - { - GNUNET_break (0); - lt_dlclose (libhandle); - return GNUNET_NO; - } + { + GNUNET_break (0); + lt_dlclose (libhandle); + return GNUNET_NO; + } lt_dlclose (libhandle); return GNUNET_YES; } @@ -206,18 +208,18 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg) void *ret; if (!initialized) - { - initialized = GNUNET_YES; - plugin_init (); - } + { + initialized = GNUNET_YES; + plugin_init (); + } libhandle = lt_dlopenext (library_name); if (libhandle == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("`%s' failed for library `%s' with error: %s\n"), - "lt_dlopenext", library_name, lt_dlerror ()); - return NULL; - } + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("`%s' failed for library `%s' with error: %s\n"), + "lt_dlopenext", library_name, lt_dlerror ()); + return NULL; + } plug = GNUNET_malloc (sizeof (struct PluginList)); plug->handle = libhandle; plug->name = GNUNET_strdup (library_name); @@ -225,13 +227,13 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg) plugins = plug; init = resolve_function (plug, "init"); if ((init == NULL) || (NULL == (ret = init (arg)))) - { - lt_dlclose (libhandle); - GNUNET_free (plug->name); - plugins = plug->next; - GNUNET_free (plug); - return NULL; - } + { + lt_dlclose (libhandle); + GNUNET_free (plug->name); + plugins = plug->next; + GNUNET_free (plug); + return NULL; + } return ret; } @@ -255,10 +257,10 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg) prev = NULL; pos = plugins; while ((pos != NULL) && (0 != strcmp (pos->name, library_name))) - { - prev = pos; - pos = pos->next; - } + { + prev = pos; + pos = pos->next; + } if (pos == NULL) return NULL; @@ -274,10 +276,10 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg) GNUNET_free (pos->name); GNUNET_free (pos); if (plugins == NULL) - { - plugin_fini (); - initialized = GNUNET_NO; - } + { + plugin_fini (); + initialized = GNUNET_NO; + } return ret; } diff --git a/src/util/program.c b/src/util/program.c index 6538f303f..5582f30c6 100644 --- a/src/util/program.c +++ b/src/util/program.c @@ -36,6 +36,10 @@ #include "gnunet_scheduler_lib.h" #include +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + /** * Context for the command. */ @@ -125,9 +129,9 @@ cmd_sorter (__const void *a1, __const void *a2) */ int GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName, - const char *binaryHelp, - const struct GNUNET_GETOPT_CommandLineOption *options, - GNUNET_PROGRAM_Main task, void *task_cls) + const char *binaryHelp, + const struct GNUNET_GETOPT_CommandLineOption *options, + GNUNET_PROGRAM_Main task, void *task_cls) { struct CommandContext cc; char *path; @@ -155,29 +159,29 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName, logfile = NULL; gargs = getenv ("GNUNET_ARGS"); if (gargs != NULL) - { - char **gargv; - unsigned int gargc; - int i; - char *tok; - char *cargs; - - gargv = NULL; - gargc = 0; - for (i = 0; i < argc; i++) - GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i])); - cargs = GNUNET_strdup (gargs); - tok = strtok (cargs, " "); - while (NULL != tok) { - GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok)); - tok = strtok (NULL, " "); + char **gargv; + unsigned int gargc; + int i; + char *tok; + char *cargs; + + gargv = NULL; + gargc = 0; + for (i = 0; i < argc; i++) + GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i])); + cargs = GNUNET_strdup (gargs); + tok = strtok (cargs, " "); + while (NULL != tok) + { + GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok)); + tok = strtok (NULL, " "); + } + GNUNET_free (cargs); + GNUNET_array_append (gargv, gargc, NULL); + argv = (char *const *) gargv; + argc = gargc - 1; } - GNUNET_free (cargs); - GNUNET_array_append (gargv, gargc, NULL); - argv = (char *const *) gargv; - argc = gargc - 1; - } memset (&cc, 0, sizeof (cc)); loglev = NULL; cc.task = task; @@ -189,27 +193,28 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName, setlocale (LC_ALL, ""); path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); if (path != NULL) - { - BINDTEXTDOMAIN ("GNUnet", path); - GNUNET_free (path); - } + { + BINDTEXTDOMAIN ("GNUnet", path); + GNUNET_free (path); + } textdomain ("GNUnet"); #endif cnt = 0; while (options[cnt].name != NULL) cnt++; allopts = - GNUNET_malloc ((cnt + - 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) + - sizeof (defoptions)); + GNUNET_malloc ((cnt + + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) + + sizeof (defoptions)); memcpy (allopts, defoptions, sizeof (defoptions)); memcpy (&allopts - [sizeof (defoptions) / - sizeof (struct GNUNET_GETOPT_CommandLineOption)], options, - (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption)); - cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption); + [sizeof (defoptions) / + sizeof (struct GNUNET_GETOPT_CommandLineOption)], options, + (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption)); + cnt += + sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption); qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption), - &cmd_sorter); + &cmd_sorter); loglev = NULL; cc.cfgfile = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE); lpfx = GNUNET_strdup (binaryName); @@ -217,30 +222,31 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName, *spc = '\0'; if ((-1 == (ret = - GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv))) || - (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)) ) - { - GNUNET_CONFIGURATION_destroy (cfg); - GNUNET_free_non_null (cc.cfgfile); - GNUNET_free_non_null (loglev); - GNUNET_free_non_null (logfile); - GNUNET_free (allopts); - GNUNET_free (lpfx); - return GNUNET_SYSERR; - } + GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv))) + || (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile))) + { + GNUNET_CONFIGURATION_destroy (cfg); + GNUNET_free_non_null (cc.cfgfile); + GNUNET_free_non_null (loglev); + GNUNET_free_non_null (logfile); + GNUNET_free (allopts); + GNUNET_free (lpfx); + return GNUNET_SYSERR; + } (void) GNUNET_CONFIGURATION_load (cfg, cc.cfgfile); GNUNET_free (allopts); GNUNET_free (lpfx); if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset", - &skew_offset) && + &skew_offset) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", - "skew_variance", &skew_variance))) - { - clock_offset = skew_offset - skew_variance; - GNUNET_TIME_set_offset (clock_offset); - } + "skew_variance", + &skew_variance))) + { + clock_offset = skew_offset - skew_variance; + GNUNET_TIME_set_offset (clock_offset); + } /* run */ cc.args = &argv[ret]; GNUNET_SCHEDULER_run (&program_main, &cc); diff --git a/src/util/pseudonym.c b/src/util/pseudonym.c index 85353c6da..b6e51af18 100644 --- a/src/util/pseudonym.c +++ b/src/util/pseudonym.c @@ -31,6 +31,10 @@ #include "gnunet_pseudonym_lib.h" #include "gnunet_bio_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + /** * Name of the directory which stores meta data for pseudonym */ @@ -78,16 +82,16 @@ static struct DiscoveryCallback *head; */ static void internal_notify (const GNUNET_HashCode * id, - const struct GNUNET_CONTAINER_MetaData *md, int rating) + const struct GNUNET_CONTAINER_MetaData *md, int rating) { struct DiscoveryCallback *pos; pos = head; while (pos != NULL) - { - pos->callback (pos->closure, id, md, rating); - pos = pos->next; - } + { + pos->callback (pos->closure, id, md, rating); + pos = pos->next; + } } /** @@ -99,9 +103,10 @@ internal_notify (const GNUNET_HashCode * id, */ int GNUNET_PSEUDONYM_discovery_callback_register (const struct - GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_PSEUDONYM_Iterator - iterator, void *closure) + GNUNET_CONFIGURATION_Handle + *cfg, + GNUNET_PSEUDONYM_Iterator + iterator, void *closure) { struct DiscoveryCallback *list; @@ -121,7 +126,7 @@ GNUNET_PSEUDONYM_discovery_callback_register (const struct */ int GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator - iterator, void *closure) + iterator, void *closure) { struct DiscoveryCallback *prev; struct DiscoveryCallback *pos; @@ -129,11 +134,11 @@ GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator prev = NULL; pos = head; while ((pos != NULL) && - ((pos->callback != iterator) || (pos->closure != closure))) - { - prev = pos; - pos = pos->next; - } + ((pos->callback != iterator) || (pos->closure != closure))) + { + prev = pos; + pos = pos->next; + } if (pos == NULL) return GNUNET_SYSERR; if (prev == NULL) @@ -155,16 +160,17 @@ GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator */ static char * get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *prefix, const GNUNET_HashCode * psid) + const char *prefix, const GNUNET_HashCode * psid) { struct GNUNET_CRYPTO_HashAsciiEncoded enc; if (psid != NULL) GNUNET_CRYPTO_hash_to_enc (psid, &enc); - return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix, - (psid == - NULL) ? NULL : (const char *) &enc, - NULL); + return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, + prefix, + (psid == + NULL) ? NULL : (const char *) &enc, + NULL); } @@ -178,9 +184,9 @@ get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, */ static void write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg, - const GNUNET_HashCode * nsid, - const struct GNUNET_CONTAINER_MetaData *meta, - int32_t ranking, const char *ns_name) + const GNUNET_HashCode * nsid, + const struct GNUNET_CONTAINER_MetaData *meta, + int32_t ranking, const char *ns_name) { char *fn; struct GNUNET_BIO_WriteHandle *fileW; @@ -189,23 +195,23 @@ write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_assert (fn != NULL); fileW = GNUNET_BIO_write_open (fn); if (NULL != fileW) - { - if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) || - (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) || - (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta))) { - (void) GNUNET_BIO_write_close (fileW); - GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); - GNUNET_free (fn); - return; - } - if (GNUNET_OK != GNUNET_BIO_write_close (fileW)) - { - GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); - GNUNET_free (fn); - return; + if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) || + (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) || + (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta))) + { + (void) GNUNET_BIO_write_close (fileW); + GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); + GNUNET_free (fn); + return; + } + if (GNUNET_OK != GNUNET_BIO_write_close (fileW)) + { + GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); + GNUNET_free (fn); + return; + } } - } GNUNET_free (fn); /* create entry for pseudonym name in names */ /* FIXME: 90% of what this call does is not needed @@ -224,9 +230,9 @@ write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg, */ static int read_info (const struct GNUNET_CONFIGURATION_Handle *cfg, - const GNUNET_HashCode * nsid, - struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking, - char **ns_name) + const GNUNET_HashCode * nsid, + struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking, + char **ns_name) { char *fn; char *emsg; @@ -236,10 +242,10 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_assert (fn != NULL); fileR = GNUNET_BIO_read_open (fn); if (fileR == NULL) - { - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + GNUNET_free (fn); + return GNUNET_SYSERR; + } emsg = NULL; *ns_name = NULL; if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) || @@ -247,30 +253,30 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) || (GNUNET_OK != GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta))) - { - (void) GNUNET_BIO_read_close (fileR, &emsg); - GNUNET_free_non_null (emsg); - GNUNET_free_non_null (*ns_name); - *ns_name = NULL; - GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + (void) GNUNET_BIO_read_close (fileR, &emsg); + GNUNET_free_non_null (emsg); + GNUNET_free_non_null (*ns_name); + *ns_name = NULL; + GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); + GNUNET_free (fn); + return GNUNET_SYSERR; + } if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ - ("Failed to parse metadata about pseudonym from file `%s': %s\n"), - fn, emsg); - GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); - GNUNET_CONTAINER_meta_data_destroy (*meta); - *meta = NULL; - GNUNET_free_non_null (*ns_name); - *ns_name = NULL; - GNUNET_free_non_null (emsg); - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _ + ("Failed to parse metadata about pseudonym from file `%s': %s\n"), + fn, emsg); + GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); + GNUNET_CONTAINER_meta_data_destroy (*meta); + *meta = NULL; + GNUNET_free_non_null (*ns_name); + *ns_name = NULL; + GNUNET_free_non_null (emsg); + GNUNET_free (fn); + return GNUNET_SYSERR; + } GNUNET_free (fn); return GNUNET_OK; } @@ -286,7 +292,7 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg, */ char * GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg, - const GNUNET_HashCode * nsid) + const GNUNET_HashCode * nsid) { struct GNUNET_CONTAINER_MetaData *meta; char *name; @@ -304,25 +310,25 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg, meta = NULL; name = NULL; if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name)) - { - if ((meta != NULL) && (name == NULL)) - name = - GNUNET_CONTAINER_meta_data_get_first_by_types (meta, - EXTRACTOR_METATYPE_TITLE, - EXTRACTOR_METATYPE_FILENAME, - EXTRACTOR_METATYPE_DESCRIPTION, - EXTRACTOR_METATYPE_SUBJECT, - EXTRACTOR_METATYPE_PUBLISHER, - EXTRACTOR_METATYPE_AUTHOR_NAME, - EXTRACTOR_METATYPE_COMMENT, - EXTRACTOR_METATYPE_SUMMARY, - -1); - if (meta != NULL) { - GNUNET_CONTAINER_meta_data_destroy (meta); - meta = NULL; + if ((meta != NULL) && (name == NULL)) + name = + GNUNET_CONTAINER_meta_data_get_first_by_types (meta, + EXTRACTOR_METATYPE_TITLE, + EXTRACTOR_METATYPE_FILENAME, + EXTRACTOR_METATYPE_DESCRIPTION, + EXTRACTOR_METATYPE_SUBJECT, + EXTRACTOR_METATYPE_PUBLISHER, + EXTRACTOR_METATYPE_AUTHOR_NAME, + EXTRACTOR_METATYPE_COMMENT, + EXTRACTOR_METATYPE_SUMMARY, + -1); + if (meta != NULL) + { + GNUNET_CONTAINER_meta_data_destroy (meta); + meta = NULL; + } } - } if (name == NULL) name = GNUNET_strdup (_("no-name")); GNUNET_CRYPTO_hash (name, strlen (name), &nh); @@ -333,31 +339,31 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg, if (0 == STAT (fn, &sbuf)) GNUNET_DISK_file_size (fn, &len, GNUNET_YES); fh = GNUNET_DISK_file_open (fn, - GNUNET_DISK_OPEN_CREATE | - GNUNET_DISK_OPEN_READWRITE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_OPEN_CREATE | + GNUNET_DISK_OPEN_READWRITE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); i = 0; idx = -1; while ((len >= sizeof (GNUNET_HashCode)) && - (sizeof (GNUNET_HashCode) == - GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode)))) - { - if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode))) + (sizeof (GNUNET_HashCode) == + GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode)))) { - idx = i; - break; + if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode))) + { + idx = i; + break; + } + i++; + len -= sizeof (GNUNET_HashCode); } - i++; - len -= sizeof (GNUNET_HashCode); - } if (idx == -1) - { - idx = i; - if (sizeof (GNUNET_HashCode) != - GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode))) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); - } + { + idx = i; + if (sizeof (GNUNET_HashCode) != + GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode))) + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn); + } GNUNET_DISK_file_close (fh); ret = GNUNET_malloc (strlen (name) + 32); GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx); @@ -376,7 +382,7 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg, */ int GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *ns_uname, GNUNET_HashCode * nsid) + const char *ns_uname, GNUNET_HashCode * nsid) { size_t slen; uint64_t len; @@ -402,24 +408,24 @@ GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg, if ((GNUNET_OK != GNUNET_DISK_file_test (fn) || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) || ((idx + 1) * sizeof (GNUNET_HashCode) > len)) - { - GNUNET_free (fn); - return GNUNET_SYSERR; - } + { + GNUNET_free (fn); + return GNUNET_SYSERR; + } fh = GNUNET_DISK_file_open (fn, - GNUNET_DISK_OPEN_CREATE | - GNUNET_DISK_OPEN_READWRITE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_OPEN_CREATE | + GNUNET_DISK_OPEN_READWRITE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); GNUNET_free (fn); GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), - GNUNET_DISK_SEEK_SET); + GNUNET_DISK_SEEK_SET); if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode))) - { - GNUNET_DISK_file_close (fh); - return GNUNET_SYSERR; - } + { + GNUNET_DISK_file_close (fh); + return GNUNET_SYSERR; + } GNUNET_DISK_file_close (fh); return GNUNET_OK; } @@ -469,15 +475,15 @@ list_pseudonym_helper (void *cls, const char *fullname) if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) return GNUNET_OK; fn = &fullname[strlen (fullname) + 1 - - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; if (fn[-1] != DIR_SEPARATOR) return GNUNET_OK; ret = GNUNET_OK; if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id)) - return GNUNET_OK; /* invalid name */ + return GNUNET_OK; /* invalid name */ str = NULL; if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str)) - return GNUNET_OK; /* ignore entry */ + return GNUNET_OK; /* ignore entry */ GNUNET_free_non_null (str); if (c->iterator != NULL) ret = c->iterator (c->closure, &id, meta, rating); @@ -496,7 +502,7 @@ list_pseudonym_helper (void *cls, const char *fullname) */ int GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_PSEUDONYM_Iterator iterator, void *closure) + GNUNET_PSEUDONYM_Iterator iterator, void *closure) { struct ListPseudonymClosure cls; char *fn; @@ -525,7 +531,7 @@ GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg, */ int GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg, - const GNUNET_HashCode * nsid, int delta) + const GNUNET_HashCode * nsid, int delta) { struct GNUNET_CONTAINER_MetaData *meta; int ret; @@ -535,10 +541,10 @@ GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg, name = NULL; ret = read_info (cfg, nsid, &meta, &ranking, &name); if (ret == GNUNET_SYSERR) - { - ranking = 0; - meta = GNUNET_CONTAINER_meta_data_create (); - } + { + ranking = 0; + meta = GNUNET_CONTAINER_meta_data_create (); + } ranking += delta; write_pseudonym_info (cfg, nsid, meta, ranking, name); GNUNET_CONTAINER_meta_data_destroy (meta); @@ -558,8 +564,8 @@ GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg, */ void GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg, - const GNUNET_HashCode * id, - const struct GNUNET_CONTAINER_MetaData *meta) + const GNUNET_HashCode * id, + const struct GNUNET_CONTAINER_MetaData *meta) { char *name; int32_t ranking; @@ -573,16 +579,16 @@ GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg, if ((0 == STAT (fn, &sbuf)) && (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name))) - { - GNUNET_CONTAINER_meta_data_merge (old, meta); - write_pseudonym_info (cfg, id, old, ranking, name); - GNUNET_CONTAINER_meta_data_destroy (old); - GNUNET_free_non_null (name); - } + { + GNUNET_CONTAINER_meta_data_merge (old, meta); + write_pseudonym_info (cfg, id, old, ranking, name); + GNUNET_CONTAINER_meta_data_destroy (old); + GNUNET_free_non_null (name); + } else - { - write_pseudonym_info (cfg, id, meta, ranking, NULL); - } + { + write_pseudonym_info (cfg, id, meta, ranking, NULL); + } GNUNET_free (fn); internal_notify (id, meta, ranking); } diff --git a/src/util/resolver_api.c b/src/util/resolver_api.c index 913769ae1..3bdc40143 100644 --- a/src/util/resolver_api.c +++ b/src/util/resolver_api.c @@ -35,6 +35,8 @@ #define LOG(kind,...) GNUNET_log_from (kind, "resolver-api", __VA_ARGS__) +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "resolver-api", syscall) + /** * Maximum supported length for a hostname */ @@ -194,26 +196,26 @@ check_config () #endif if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (resolver_cfg, "resolver", - "HOSTNAME", &hostname)) - { - LOG (GNUNET_ERROR_TYPE_ERROR, - _("Must specify `%s' for `%s' in configuration!\n"), "HOSTNAME", - "resolver"); - GNUNET_assert (0); - } + "HOSTNAME", &hostname)) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Must specify `%s' for `%s' in configuration!\n"), "HOSTNAME", + "resolver"); + GNUNET_assert (0); + } if ((1 != inet_pton (AF_INET, hostname, &v4)) || (1 != inet_pton (AF_INET6, hostname, &v6))) - { - GNUNET_free (hostname); - return; - } - i = 0; - while (loopback[i] != NULL) - if (0 == strcasecmp (loopback[i++], hostname)) { GNUNET_free (hostname); return; } + i = 0; + while (loopback[i] != NULL) + if (0 == strcasecmp (loopback[i++], hostname)) + { + GNUNET_free (hostname); + return; + } LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"), @@ -247,23 +249,23 @@ GNUNET_RESOLVER_disconnect () GNUNET_assert (NULL == req_head); GNUNET_assert (NULL == req_tail); if (NULL != client) - { + { #if DEBUG_RESOLVER - LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n"); #endif - GNUNET_CLIENT_disconnect (client, GNUNET_NO); - client = NULL; - } + GNUNET_CLIENT_disconnect (client, GNUNET_NO); + client = NULL; + } if (r_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (r_task); - r_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (r_task); + r_task = GNUNET_SCHEDULER_NO_TASK; + } if (s_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (s_task); - s_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (s_task); + s_task = GNUNET_SCHEDULER_NO_TASK; + } } @@ -284,37 +286,35 @@ no_resolve (const struct sockaddr *sa, socklen_t salen) if (salen < sizeof (struct sockaddr)) return NULL; switch (sa->sa_family) - { - case AF_INET: - if (salen != sizeof (struct sockaddr_in)) - return NULL; - if (NULL == - inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr, inet4, - INET_ADDRSTRLEN)) { - GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "resolver-api", - "inet_ntop"); - return NULL; - } - ret = GNUNET_strdup (inet4); - break; - case AF_INET6: - if (salen != sizeof (struct sockaddr_in6)) - return NULL; - if (NULL == - inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr, inet6, - INET6_ADDRSTRLEN)) - { - GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "resolver-api", - "inet_ntop"); - return NULL; + case AF_INET: + if (salen != sizeof (struct sockaddr_in)) + return NULL; + if (NULL == + inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr, inet4, + INET_ADDRSTRLEN)) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); + return NULL; + } + ret = GNUNET_strdup (inet4); + break; + case AF_INET6: + if (salen != sizeof (struct sockaddr_in6)) + return NULL; + if (NULL == + inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr, + inet6, INET6_ADDRSTRLEN)) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); + return NULL; + } + ret = GNUNET_strdup (inet6); + break; + default: + ret = NULL; + break; } - ret = GNUNET_strdup (inet6); - break; - default: - ret = NULL; - break; - } return ret; } @@ -322,15 +322,13 @@ no_resolve (const struct sockaddr *sa, socklen_t salen) /** * Adjust exponential back-off and reconnect to the service. */ -static void -reconnect (); +static void reconnect (); /** * Process pending requests to the resolver. */ -static void -process_requests (); +static void process_requests (); /** @@ -352,73 +350,32 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg) LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving response from DNS service\n"); #endif if (msg == NULL) - { - if (NULL != rh->name_callback) - LOG (GNUNET_ERROR_TYPE_INFO, - _("Timeout trying to resolve IP address `%s'.\n"), - GNUNET_a2s ((const void *) &rh[1], rh->data_len)); - else - LOG (GNUNET_ERROR_TYPE_INFO, - _("Timeout trying to resolve hostname `%s'.\n"), - (const char *) &rh[1]); - /* check if request was canceled */ - if (rh->was_transmitted != GNUNET_SYSERR) - { - if (NULL != rh->name_callback) - { - /* no reverse lookup was successful, return ip as string */ - if (rh->received_response == GNUNET_NO) - rh->name_callback (rh->cls, - no_resolve ((const struct sockaddr *) &rh[1], - rh->data_len)); - /* at least one reverse lookup was successful */ - else - rh->name_callback (rh->cls, NULL); - } - if (NULL != rh->addr_callback) - rh->addr_callback (rh->cls, NULL, 0); - } - GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); - GNUNET_free (rh); - GNUNET_CLIENT_disconnect (client, GNUNET_NO); - client = NULL; - reconnect (); - return; - } - if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type)) - { - GNUNET_break (0); - GNUNET_CLIENT_disconnect (client, GNUNET_NO); - client = NULL; - reconnect (); - return; - } - size = ntohs (msg->size); - /* message contains not data, just header */ - if (size == sizeof (struct GNUNET_MessageHeader)) - { - /* check if request was canceled */ - if (rh->was_transmitted != GNUNET_SYSERR) { if (NULL != rh->name_callback) - rh->name_callback (rh->cls, NULL); - if (NULL != rh->addr_callback) - rh->addr_callback (rh->cls, NULL, 0); - } - GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); - GNUNET_free (rh); - process_requests (); - return; - } - /* return reverse lookup results to caller */ - if (NULL != rh->name_callback) - { - hostname = (const char *) &msg[1]; - if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0') - { - GNUNET_break (0); + LOG (GNUNET_ERROR_TYPE_INFO, + _("Timeout trying to resolve IP address `%s'.\n"), + GNUNET_a2s ((const void *) &rh[1], rh->data_len)); + else + LOG (GNUNET_ERROR_TYPE_INFO, + _("Timeout trying to resolve hostname `%s'.\n"), + (const char *) &rh[1]); + /* check if request was canceled */ if (rh->was_transmitted != GNUNET_SYSERR) - rh->name_callback (rh->cls, NULL); + { + if (NULL != rh->name_callback) + { + /* no reverse lookup was successful, return ip as string */ + if (rh->received_response == GNUNET_NO) + rh->name_callback (rh->cls, + no_resolve ((const struct sockaddr *) + &rh[1], rh->data_len)); + /* at least one reverse lookup was successful */ + else + rh->name_callback (rh->cls, NULL); + } + if (NULL != rh->addr_callback) + rh->addr_callback (rh->cls, NULL, 0); + } GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); GNUNET_free (rh); GNUNET_CLIENT_disconnect (client, GNUNET_NO); @@ -426,46 +383,89 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg) reconnect (); return; } -#if DEBUG_RESOLVER - LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s' for IP `%s'.\n"), - hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len)); -#endif - if (rh->was_transmitted != GNUNET_SYSERR) - rh->name_callback (rh->cls, hostname); - rh->received_response = GNUNET_YES; - GNUNET_CLIENT_receive (client, &handle_response, rh, - GNUNET_TIME_absolute_get_remaining (rh->timeout)); - } - /* return lookup results to caller */ - if (NULL != rh->addr_callback) - { - sa = (const struct sockaddr *) &msg[1]; - salen = size - sizeof (struct GNUNET_MessageHeader); - if (salen < sizeof (struct sockaddr)) + if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type)) { GNUNET_break (0); - if (rh->was_transmitted != GNUNET_SYSERR) - rh->addr_callback (rh->cls, NULL, 0); - GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); - GNUNET_free (rh); GNUNET_CLIENT_disconnect (client, GNUNET_NO); client = NULL; reconnect (); return; } + size = ntohs (msg->size); + /* message contains not data, just header */ + if (size == sizeof (struct GNUNET_MessageHeader)) + { + /* check if request was canceled */ + if (rh->was_transmitted != GNUNET_SYSERR) + { + if (NULL != rh->name_callback) + rh->name_callback (rh->cls, NULL); + if (NULL != rh->addr_callback) + rh->addr_callback (rh->cls, NULL, 0); + } + GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); + GNUNET_free (rh); + process_requests (); + return; + } + /* return reverse lookup results to caller */ + if (NULL != rh->name_callback) + { + hostname = (const char *) &msg[1]; + if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0') + { + GNUNET_break (0); + if (rh->was_transmitted != GNUNET_SYSERR) + rh->name_callback (rh->cls, NULL); + GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); + GNUNET_free (rh); + GNUNET_CLIENT_disconnect (client, GNUNET_NO); + client = NULL; + reconnect (); + return; + } #if DEBUG_RESOLVER + LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s' for IP `%s'.\n"), + hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len)); +#endif + if (rh->was_transmitted != GNUNET_SYSERR) + rh->name_callback (rh->cls, hostname); + rh->received_response = GNUNET_YES; + GNUNET_CLIENT_receive (client, &handle_response, rh, + GNUNET_TIME_absolute_get_remaining + (rh->timeout)); + } + /* return lookup results to caller */ + if (NULL != rh->addr_callback) { - char *ips = no_resolve (sa, salen); + sa = (const struct sockaddr *) &msg[1]; + salen = size - sizeof (struct GNUNET_MessageHeader); + if (salen < sizeof (struct sockaddr)) + { + GNUNET_break (0); + if (rh->was_transmitted != GNUNET_SYSERR) + rh->addr_callback (rh->cls, NULL, 0); + GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); + GNUNET_free (rh); + GNUNET_CLIENT_disconnect (client, GNUNET_NO); + client = NULL; + reconnect (); + return; + } +#if DEBUG_RESOLVER + { + char *ips = no_resolve (sa, salen); - LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s' for `%s'.\n", ips, - (const char *) &rh[1]); - GNUNET_free (ips); - } + LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s' for `%s'.\n", + ips, (const char *) &rh[1]); + GNUNET_free (ips); + } #endif - rh->addr_callback (rh->cls, sa, salen); - GNUNET_CLIENT_receive (client, &handle_response, rh, - GNUNET_TIME_absolute_get_remaining (rh->timeout)); - } + rh->addr_callback (rh->cls, sa, salen); + GNUNET_CLIENT_receive (client, &handle_response, rh, + GNUNET_TIME_absolute_get_remaining + (rh->timeout)); + } } @@ -498,26 +498,27 @@ numeric_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) hostname = (const char *) &rh[1]; if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET)) && (1 == inet_pton (AF_INET, hostname, &v4.sin_addr))) - { - rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); - if ((rh->domain == AF_UNSPEC) && - (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr))) { - /* this can happen on some systems IF "hostname" is "localhost" */ - rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); + rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); + if ((rh->domain == AF_UNSPEC) && + (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr))) + { + /* this can happen on some systems IF "hostname" is "localhost" */ + rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, + sizeof (v6)); + } + rh->addr_callback (rh->cls, NULL, 0); + GNUNET_free (rh); + return; } - rh->addr_callback (rh->cls, NULL, 0); - GNUNET_free (rh); - return; - } if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) && (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr))) - { - rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); - rh->addr_callback (rh->cls, NULL, 0); - GNUNET_free (rh); - return; - } + { + rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); + rh->addr_callback (rh->cls, NULL, 0); + GNUNET_free (rh); + return; + } /* why are we here? this task should not have been scheduled! */ GNUNET_assert (0); GNUNET_free (rh); @@ -552,21 +553,21 @@ loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) #endif v6.sin6_addr = in6addr_loopback; switch (rh->domain) - { - case AF_INET: - rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); - break; - case AF_INET6: - rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); - break; - case AF_UNSPEC: - rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); - rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); - break; - default: - GNUNET_break (0); - break; - } + { + case AF_INET: + rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); + break; + case AF_INET6: + rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); + break; + case AF_UNSPEC: + rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); + rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); + break; + default: + GNUNET_break (0); + break; + } rh->addr_callback (rh->cls, NULL, 0); GNUNET_free (rh); } @@ -594,25 +595,25 @@ process_requests () struct GNUNET_RESOLVER_RequestHandle *rh; if (NULL == client) - { - reconnect (); - return; - } + { + reconnect (); + return; + } rh = req_head; if (NULL == rh) - { - /* nothing to do, release socket really soon if there is nothing - * else happening... */ - s_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, - &shutdown_task, NULL); - return; - } + { + /* nothing to do, release socket really soon if there is nothing + * else happening... */ + s_task = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, + &shutdown_task, NULL); + return; + } if (GNUNET_YES == rh->was_transmitted) - return; /* waiting for reply */ + return; /* waiting for reply */ msg = (struct GNUNET_RESOLVER_GetMessage *) buf; msg->header.size = - htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + rh->data_len); + htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + rh->data_len); msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST); msg->direction = htonl (rh->direction); msg->domain = htonl (rh->domain); @@ -623,15 +624,15 @@ process_requests () #endif if (GNUNET_OK != GNUNET_CLIENT_transmit_and_get_response (client, &msg->header, - GNUNET_TIME_absolute_get_remaining - (rh->timeout), GNUNET_YES, - &handle_response, rh)) - { - GNUNET_CLIENT_disconnect (client, GNUNET_NO); - client = NULL; - reconnect (); - return; - } + GNUNET_TIME_absolute_get_remaining + (rh->timeout), GNUNET_YES, + &handle_response, rh)) + { + GNUNET_CLIENT_disconnect (client, GNUNET_NO); + client = NULL; + reconnect (); + return; + } rh->was_transmitted = GNUNET_YES; } @@ -647,7 +648,7 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { r_task = GNUNET_SCHEDULER_NO_TASK; if (NULL == req_head) - return; /* no work pending */ + return; /* no work pending */ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) return; #if DEBUG_RESOLVER @@ -655,11 +656,12 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) #endif client = GNUNET_CLIENT_connect ("resolver", resolver_cfg); if (NULL == client) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect, will try again later\n"); - reconnect (); - return; - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Failed to connect, will try again later\n"); + reconnect (); + return; + } process_requests (); } @@ -676,26 +678,26 @@ reconnect () return; GNUNET_assert (NULL == client); if (NULL != (rh = req_head)) - { - switch (rh->was_transmitted) { - case GNUNET_NO: - /* nothing more to do */ - break; - case GNUNET_YES: - /* disconnected, transmit again! */ - rh->was_transmitted = GNUNET_NO; - break; - case GNUNET_SYSERR: - /* request was cancelled, remove entirely */ - GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); - GNUNET_free (rh); - break; - default: - GNUNET_assert (0); - break; + switch (rh->was_transmitted) + { + case GNUNET_NO: + /* nothing more to do */ + break; + case GNUNET_YES: + /* disconnected, transmit again! */ + rh->was_transmitted = GNUNET_NO; + break; + case GNUNET_SYSERR: + /* request was cancelled, remove entirely */ + GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); + GNUNET_free (rh); + break; + default: + GNUNET_assert (0); + break; + } } - } #if DEBUG_RESOLVER LOG (GNUNET_ERROR_TYPE_DEBUG, "Will try to connect to DNS service in %llu ms\n", @@ -719,9 +721,9 @@ reconnect () */ struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_ip_get (const char *hostname, int domain, - struct GNUNET_TIME_Relative timeout, - GNUNET_RESOLVER_AddressCallback callback, - void *callback_cls) + struct GNUNET_TIME_Relative timeout, + GNUNET_RESOLVER_AddressCallback callback, + void *callback_cls) { struct GNUNET_RESOLVER_RequestHandle *rh; size_t slen; @@ -732,10 +734,10 @@ GNUNET_RESOLVER_ip_get (const char *hostname, int domain, slen = strlen (hostname) + 1; if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) - { - GNUNET_break (0); - return NULL; - } + { + GNUNET_break (0); + return NULL; + } rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen); rh->domain = domain; rh->addr_callback = callback; @@ -749,25 +751,25 @@ GNUNET_RESOLVER_ip_get (const char *hostname, int domain, ((domain == AF_INET) || (domain == AF_UNSPEC))) || ((1 == inet_pton (AF_INET6, hostname, &v6)) && ((domain == AF_INET6) || (domain == AF_UNSPEC)))) - { - rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, rh); - return rh; - } + { + rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, rh); + return rh; + } /* then, check if this is a loopback address */ i = 0; while (loopback[i] != NULL) if (0 == strcasecmp (loopback[i++], hostname)) - { - rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh); - return rh; - } + { + rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh); + return rh; + } GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh); rh->was_queued = GNUNET_YES; if (s_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (s_task); - s_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (s_task); + s_task = GNUNET_SCHEDULER_NO_TASK; + } process_requests (); return rh; } @@ -791,10 +793,10 @@ numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result); #endif if (result != NULL) - { - rh->name_callback (rh->cls, result); - GNUNET_free (result); - } + { + rh->name_callback (rh->cls, result); + GNUNET_free (result); + } rh->name_callback (rh->cls, NULL); GNUNET_free (rh); } @@ -814,10 +816,10 @@ numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen, - int do_resolve, - struct GNUNET_TIME_Relative timeout, - GNUNET_RESOLVER_HostnameCallback callback, - void *cls) + int do_resolve, + struct GNUNET_TIME_Relative timeout, + GNUNET_RESOLVER_HostnameCallback callback, + void *cls) { struct GNUNET_RESOLVER_RequestHandle *rh; @@ -831,24 +833,24 @@ GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen, rh->direction = GNUNET_YES; rh->received_response = GNUNET_NO; if (GNUNET_NO == do_resolve) - { - rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh); - return rh; - } + { + rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh); + return rh; + } if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) - { - GNUNET_break (0); - GNUNET_free (rh); - return NULL; - } + { + GNUNET_break (0); + GNUNET_free (rh); + return NULL; + } GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh); rh->was_queued = GNUNET_YES; if (s_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (s_task); - s_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (s_task); + s_task = GNUNET_SCHEDULER_NO_TASK; + } process_requests (); return rh; } @@ -866,21 +868,21 @@ GNUNET_RESOLVER_local_fqdn_get () char hostname[GNUNET_OS_get_hostname_max_length () + 1]; if (0 != gethostname (hostname, sizeof (hostname) - 1)) - { - GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "resolver-api", "gethostname"); - return NULL; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "gethostname"); + return NULL; + } #if DEBUG_RESOLVER LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), hostname); #endif host = gethostbyname (hostname); if (NULL == host) - { - LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not resolve our FQDN : %s\n"), - hstrerror (h_errno)); - return NULL; - } + { + LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not resolve our FQDN : %s\n"), + hstrerror (h_errno)); + return NULL; + } return GNUNET_strdup (host->h_name); } @@ -896,18 +898,18 @@ GNUNET_RESOLVER_local_fqdn_get () */ struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_hostname_resolve (int domain, - struct GNUNET_TIME_Relative timeout, - GNUNET_RESOLVER_AddressCallback callback, - void *cls) + struct GNUNET_TIME_Relative timeout, + GNUNET_RESOLVER_AddressCallback callback, + void *cls) { char hostname[GNUNET_OS_get_hostname_max_length () + 1]; if (0 != gethostname (hostname, sizeof (hostname) - 1)) - { - GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "resolver-api", "gethostname"); - return NULL; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "gethostname"); + return NULL; + } #if DEBUG_RESOLVER LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our hostname `%s'\n"), hostname); #endif @@ -927,19 +929,19 @@ void GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh) { if (rh->task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (rh->task); - rh->task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (rh->task); + rh->task = GNUNET_SCHEDULER_NO_TASK; + } if (rh->was_transmitted == GNUNET_NO) - { - if (rh->was_queued == GNUNET_YES) - GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); - GNUNET_free (rh); - return; - } + { + if (rh->was_queued == GNUNET_YES) + GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); + GNUNET_free (rh); + return; + } GNUNET_assert (rh->was_transmitted == GNUNET_YES); - rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */ + rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */ } diff --git a/src/util/scheduler.c b/src/util/scheduler.c index b7860650e..d40853963 100644 --- a/src/util/scheduler.c +++ b/src/util/scheduler.c @@ -34,6 +34,10 @@ #include "execinfo.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + /** * Use lsof to generate file descriptor reports on select error? * (turn off for stable releases). @@ -523,10 +527,9 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs, while (pos != NULL) { #if DEBUG_TASKS - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Checking readiness of task: %llu / %p\n", pos->id, - pos->callback_cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Checking readiness of task: %llu / %p\n", pos->id, + pos->callback_cls); #endif next = pos->next; if (GNUNET_YES == is_ready (pos, now, rs, ws)) @@ -656,12 +659,10 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws) if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value > DELAY_THRESHOLD.rel_value) { - GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, - "util", - "Task %llu took %llums to be scheduled\n", pos->id, - (unsigned long long) - GNUNET_TIME_absolute_get_duration - (pos->start_time).rel_value); + LOG (GNUNET_ERROR_TYPE_ERROR, + "Task %llu took %llums to be scheduled\n", pos->id, + (unsigned long long) + GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value); } #endif tc.reason = pos->reason; @@ -678,20 +679,17 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws) (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd))) abort (); // added to ready in previous select loop! #if DEBUG_TASKS - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Running task: %llu / %p\n", pos->id, - pos->callback_cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Running task: %llu / %p\n", pos->id, pos->callback_cls); #endif pos->callback (pos->callback_cls, &tc); #if EXECINFO int i; for (i = 0; i < pos->num_backtrace_strings; i++) - GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, - "util", - "Task %llu trace %d: %s\n", pos->id, - i, pos->backtrace_strings[i]); + LOG (GNUNET_ERROR_TYPE_ERROR, + "Task %llu trace %d: %s\n", pos->id, + i, pos->backtrace_strings[i]); #endif active_task = NULL; destroy_task (pos); @@ -844,8 +842,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls) if (errno == EINTR) continue; - GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "util", - "select"); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select"); #ifndef MINGW #if USE_LSOF char lsof[512]; @@ -854,8 +851,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls) (void) close (1); (void) dup2 (2, 1); if (0 != system (lsof)) - GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "util", - "system"); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system"); #endif #endif abort (); @@ -863,8 +859,8 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls) } if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16)) { - GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, - "util", _("Looks like we're busy waiting...\n")); + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Looks like we're busy waiting...\n")); sleep (1); /* mitigate */ } check_ready (rs, ws); @@ -994,10 +990,9 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task) p++; if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT) { - GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, - "util", - _("Attempt to cancel dead task %llu!\n"), - (unsigned long long) task); + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Attempt to cancel dead task %llu!\n"), + (unsigned long long) task); GNUNET_assert (0); } prev = NULL; @@ -1037,9 +1032,8 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task) } ret = t->callback_cls; #if DEBUG_TASKS - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Canceling task: %llu / %p\n", task, t->callback_cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Canceling task: %llu / %p\n", task, t->callback_cls); #endif destroy_task (t); return ret; @@ -1086,10 +1080,8 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls, t->priority = current_priority; t->lifeness = current_lifeness; #if DEBUG_TASKS - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Adding continuation task: %llu / %p\n", - t->id, t->callback_cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding continuation task: %llu / %p\n", t->id, t->callback_cls); #endif queue_ready_task (t); } @@ -1220,18 +1212,15 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay, pending_timeout_last = t; #if DEBUG_TASKS - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Adding task: %llu / %p\n", t->id, t->callback_cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding task: %llu / %p\n", t->id, t->callback_cls); #endif #if EXECINFO int i; for (i = 0; i < t->num_backtrace_strings; i++) - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Task %llu trace %d: %s\n", t->id, i, - t->backtrace_strings[i]); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]); #endif return t->id; @@ -1352,14 +1341,14 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd, if ((flags == -1) && (errno == EBADF)) { - GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, - "util", "Got invalid file descriptor %d!\n", rfd); + LOG (GNUNET_ERROR_TYPE_ERROR, + "Got invalid file descriptor %d!\n", rfd); #if EXECINFO int i; for (i = 0; i < t->num_backtrace_strings; i++) - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", "Trace: %s\n", t->backtrace_strings[i]); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Trace: %s\n", t->backtrace_strings[i]); #endif GNUNET_assert (0); } @@ -1370,14 +1359,14 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd, if (flags == -1 && errno == EBADF) { - GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, - "util", "Got invalid file descriptor %d!\n", wfd); + LOG (GNUNET_ERROR_TYPE_ERROR, + "Got invalid file descriptor %d!\n", wfd); #if EXECINFO int i; for (i = 0; i < t->num_backtrace_strings; i++) - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", "Trace: %s\n", t->backtrace_strings[i]); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Trace: %s\n", t->backtrace_strings[i]); #endif GNUNET_assert (0); } @@ -1398,17 +1387,15 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd, pending = t; max_priority_added = GNUNET_MAX (max_priority_added, t->priority); #if DEBUG_TASKS - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Adding task: %llu / %p\n", t->id, t->callback_cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding task: %llu / %p\n", t->id, t->callback_cls); #endif #if EXECINFO int i; for (i = 0; i < t->num_backtrace_strings; i++) - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "util", - "Task %llu trace %d: %s\n", t->id, i, - t->backtrace_strings[i]); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]); #endif return t->id; } @@ -1673,18 +1660,15 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio, pending = t; max_priority_added = GNUNET_MAX (max_priority_added, t->priority); #if DEBUG_TASKS - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Adding task: %llu / %p\n", t->id, t->callback_cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding task: %llu / %p\n", t->id, t->callback_cls); #endif #if EXECINFO int i; for (i = 0; i < t->num_backtrace_strings; i++) - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, - "util", - "Task %llu trace %d: %s\n", t->id, i, - t->backtrace_strings[i]); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]); #endif return t->id; } diff --git a/src/util/server.c b/src/util/server.c index 63993ee37..f792e5a67 100644 --- a/src/util/server.c +++ b/src/util/server.c @@ -33,6 +33,12 @@ #include "gnunet_disk_lib.h" #include "gnunet_protocols.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + #define DEBUG_SERVER GNUNET_EXTRA_LOGGING /** @@ -258,7 +264,8 @@ struct GNUNET_SERVER_Client * @param tc reason why we are running right now */ static void -process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +process_listen_socket (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_SERVER_Handle *server = cls; struct GNUNET_CONNECTION_Handle *sock; @@ -272,46 +279,47 @@ process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) while (NULL != server->listen_sockets[i]) GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]); if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - { - /* ignore shutdown, someone else will take care of it! */ - server->listen_task = - GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, - GNUNET_SCHEDULER_NO_TASK, - GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, - &process_listen_socket, server); - GNUNET_NETWORK_fdset_destroy (r); - return; - } + { + /* ignore shutdown, someone else will take care of it! */ + server->listen_task = + GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, + GNUNET_SCHEDULER_NO_TASK, + GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, + &process_listen_socket, server); + GNUNET_NETWORK_fdset_destroy (r); + return; + } i = 0; while (NULL != server->listen_sockets[i]) - { - if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i])) { - sock = - GNUNET_CONNECTION_create_from_accept (server->access, - server->access_cls, - server->listen_sockets[i]); - if (sock != NULL) - { + if (GNUNET_NETWORK_fdset_isset + (tc->read_ready, server->listen_sockets[i])) + { + sock = + GNUNET_CONNECTION_create_from_accept (server->access, + server->access_cls, + server->listen_sockets[i]); + if (sock != NULL) + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server accepted incoming connection.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server accepted incoming connection.\n"); #endif - client = GNUNET_SERVER_connect_socket (server, sock); - GNUNET_CONNECTION_ignore_shutdown (sock, - server->clients_ignore_shutdown); - /* decrement reference count, we don't keep "client" alive */ - GNUNET_SERVER_client_drop (client); - } + client = GNUNET_SERVER_connect_socket (server, sock); + GNUNET_CONNECTION_ignore_shutdown (sock, + server->clients_ignore_shutdown); + /* decrement reference count, we don't keep "client" alive */ + GNUNET_SERVER_client_drop (client); + } + } + i++; } - i++; - } /* listen for more! */ server->listen_task = - GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, - GNUNET_SCHEDULER_NO_TASK, - GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, - &process_listen_socket, server); + GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, + GNUNET_SCHEDULER_NO_TASK, + GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, + &process_listen_socket, server); GNUNET_NETWORK_fdset_destroy (r); } @@ -332,87 +340,87 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) int eno; switch (serverAddr->sa_family) - { - case AF_INET: - port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port); - break; - case AF_INET6: - port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port); - break; - case AF_UNIX: - port = 0; - break; - default: - GNUNET_break (0); - port = 0; - break; - } + { + case AF_INET: + port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port); + break; + case AF_INET6: + port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port); + break; + case AF_UNIX: + port = 0; + break; + default: + GNUNET_break (0); + port = 0; + break; + } sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0); if (NULL == sock) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); - errno = 0; - return NULL; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); + errno = 0; + return NULL; + } if (port != 0) - { - if (GNUNET_NETWORK_socket_setsockopt - (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "setsockopt"); + { + if (GNUNET_NETWORK_socket_setsockopt + (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "setsockopt"); #ifdef IPV6_V6ONLY - if ((serverAddr->sa_family == AF_INET6) && - (GNUNET_NETWORK_socket_setsockopt - (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "setsockopt"); + if ((serverAddr->sa_family == AF_INET6) && + (GNUNET_NETWORK_socket_setsockopt + (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "setsockopt"); #endif - } + } /* bind the socket */ if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK) - { - eno = errno; - if (errno != EADDRINUSE) { - /* we don't log 'EADDRINUSE' here since an IPv4 bind may - * fail if we already took the port on IPv6; if both IPv4 and - * IPv6 binds fail, then our caller will log using the - * errno preserved in 'eno' */ - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); - if (port != 0) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("`%s' failed for port %d (%s).\n"), "bind", port, - (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); - eno = 0; + eno = errno; + if (errno != EADDRINUSE) + { + /* we don't log 'EADDRINUSE' here since an IPv4 bind may + * fail if we already took the port on IPv6; if both IPv4 and + * IPv6 binds fail, then our caller will log using the + * errno preserved in 'eno' */ + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind"); + if (port != 0) + LOG (GNUNET_ERROR_TYPE_ERROR, + _("`%s' failed for port %d (%s).\n"), "bind", port, + (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); + eno = 0; + } + else + { + if (port != 0) + LOG (GNUNET_ERROR_TYPE_WARNING, + _("`%s' failed for port %d (%s): address already in use\n"), + "bind", port, + (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); + else if (serverAddr->sa_family == AF_UNIX) + LOG (GNUNET_ERROR_TYPE_WARNING, + _("`%s' failed for `%s': address already in use\n"), "bind", + ((const struct sockaddr_un *) serverAddr)->sun_path); + + } + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); + errno = eno; + return NULL; } - else + if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5)) { - if (port != 0) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("`%s' failed for port %d (%s): address already in use\n"), - "bind", port, - (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); - else if (serverAddr->sa_family == AF_UNIX) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("`%s' failed for `%s': address already in use\n"), "bind", - ((const struct sockaddr_un *) serverAddr)->sun_path); - + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen"); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); + errno = 0; + return NULL; } - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - errno = eno; - return NULL; - } - if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - errno = 0; - return NULL; - } #if DEBUG_SERVER if (port != 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server starts to listen on port %u.\n", port); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server starts to listen on port %u.\n", port); #endif return sock; } @@ -432,10 +440,10 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) */ struct GNUNET_SERVER_Handle * GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, - void *access_cls, - struct GNUNET_NETWORK_Handle **lsocks, - struct GNUNET_TIME_Relative idle_timeout, - int require_found) + void *access_cls, + struct GNUNET_NETWORK_Handle **lsocks, + struct GNUNET_TIME_Relative idle_timeout, + int require_found) { struct GNUNET_SERVER_Handle *ret; struct GNUNET_NETWORK_FDSet *r; @@ -448,18 +456,18 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, ret->access_cls = access_cls; ret->require_found = require_found; if (lsocks != NULL) - { - r = GNUNET_NETWORK_fdset_create (); - i = 0; - while (NULL != ret->listen_sockets[i]) - GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]); - ret->listen_task = - GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, - GNUNET_SCHEDULER_NO_TASK, - GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, - &process_listen_socket, ret); - GNUNET_NETWORK_fdset_destroy (r); - } + { + r = GNUNET_NETWORK_fdset_create (); + i = 0; + while (NULL != ret->listen_sockets[i]) + GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]); + ret->listen_task = + GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, + GNUNET_SCHEDULER_NO_TASK, + GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, + &process_listen_socket, ret); + GNUNET_NETWORK_fdset_destroy (r); + } return ret; } @@ -479,10 +487,10 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, */ struct GNUNET_SERVER_Handle * GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls, - struct sockaddr *const *serverAddr, - const socklen_t * socklen, - struct GNUNET_TIME_Relative idle_timeout, - int require_found) + struct sockaddr *const *serverAddr, + const socklen_t * socklen, + struct GNUNET_TIME_Relative idle_timeout, + int require_found) { struct GNUNET_NETWORK_Handle **lsocks; unsigned int i; @@ -492,31 +500,32 @@ GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls, while (serverAddr[i] != NULL) i++; if (i > 0) - { - lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1)); - i = 0; - j = 0; - while (serverAddr[i] != NULL) { - lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]); - if (lsocks[j] != NULL) - j++; - i++; + lsocks = + GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1)); + i = 0; + j = 0; + while (serverAddr[i] != NULL) + { + lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]); + if (lsocks[j] != NULL) + j++; + i++; + } + if (j == 0) + { + if (errno != 0) + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind"); + GNUNET_free (lsocks); + lsocks = NULL; + } } - if (j == 0) + else { - if (errno != 0) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); - GNUNET_free (lsocks); lsocks = NULL; } - } - else - { - lsocks = NULL; - } return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks, - idle_timeout, require_found); + idle_timeout, require_found); } @@ -533,35 +542,35 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s) unsigned int i; #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n"); #endif if (GNUNET_SCHEDULER_NO_TASK != s->listen_task) - { - GNUNET_SCHEDULER_cancel (s->listen_task); - s->listen_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (s->listen_task); + s->listen_task = GNUNET_SCHEDULER_NO_TASK; + } if (s->listen_sockets != NULL) - { - i = 0; - while (s->listen_sockets[i] != NULL) - GNUNET_break (GNUNET_OK == - GNUNET_NETWORK_socket_close (s->listen_sockets[i++])); - GNUNET_free (s->listen_sockets); - s->listen_sockets = NULL; - } + { + i = 0; + while (s->listen_sockets[i] != NULL) + GNUNET_break (GNUNET_OK == + GNUNET_NETWORK_socket_close (s->listen_sockets[i++])); + GNUNET_free (s->listen_sockets); + s->listen_sockets = NULL; + } while (s->clients != NULL) GNUNET_SERVER_client_disconnect (s->clients); while (NULL != (hpos = s->handlers)) - { - s->handlers = hpos->next; - GNUNET_free (hpos); - } + { + s->handlers = hpos->next; + GNUNET_free (hpos); + } while (NULL != (npos = s->disconnect_notify_list)) - { - npos->callback (npos->callback_cls, NULL); - s->disconnect_notify_list = npos->next; - GNUNET_free (npos); - } + { + npos->callback (npos->callback_cls, NULL); + s->disconnect_notify_list = npos->next; + GNUNET_free (npos); + } GNUNET_free (s); } @@ -581,7 +590,8 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s) */ void GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server, - const struct GNUNET_SERVER_MessageHandler *handlers) + const struct GNUNET_SERVER_MessageHandler + *handlers) { struct HandlerList *p; @@ -599,21 +609,21 @@ GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server, * @param tc scheduler context (unused) */ static void -warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +warn_no_receive_done (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_SERVER_Client *client = cls; client->warn_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, - &warn_no_receive_done, client); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, + &warn_no_receive_done, client); if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ - ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"), - (unsigned int) client->warn_type, - (unsigned long long) - GNUNET_TIME_absolute_get_duration - (client->warn_start).rel_value); + LOG (GNUNET_ERROR_TYPE_WARNING, + _ + ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"), + (unsigned int) client->warn_type, + (unsigned long long) + GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value); } @@ -625,13 +635,14 @@ warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * @param client client for which to disable the warning */ void -GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client) +GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client + *client) { if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) - { - GNUNET_SCHEDULER_cancel (client->warn_task); - client->warn_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (client->warn_task); + client->warn_task = GNUNET_SCHEDULER_NO_TASK; + } } @@ -652,8 +663,8 @@ GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client) */ int GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, - struct GNUNET_SERVER_Client *sender, - const struct GNUNET_MessageHeader *message) + struct GNUNET_SERVER_Client *sender, + const struct GNUNET_MessageHeader *message) { struct HandlerList *pos; const struct GNUNET_SERVER_MessageHandler *mh; @@ -666,56 +677,57 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, size = ntohs (message->size); #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server schedules transmission of %u-byte message of type %u to client.\n", - size, type); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server schedules transmission of %u-byte message of type %u to client.\n", + size, type); #endif pos = server->handlers; found = GNUNET_NO; while (pos != NULL) - { - i = 0; - while (pos->handlers[i].callback != NULL) { - mh = &pos->handlers[i]; - if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL)) - { - if ((mh->expected_size != 0) && (mh->expected_size != size)) - { + i = 0; + while (pos->handlers[i].callback != NULL) + { + mh = &pos->handlers[i]; + if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL)) + { + if ((mh->expected_size != 0) && (mh->expected_size != size)) + { #if GNUNET8_NETWORK_IS_DEAD - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Expected %u bytes for message of type %u, got %u\n", - mh->expected_size, mh->type, size); - GNUNET_break_op (0); + LOG (GNUNET_ERROR_TYPE_WARNING, + "Expected %u bytes for message of type %u, got %u\n", + mh->expected_size, mh->type, size); + GNUNET_break_op (0); #endif - return GNUNET_SYSERR; - } - if (sender != NULL) - { - if (0 == sender->suspended) - { - sender->warn_start = GNUNET_TIME_absolute_get (); - sender->warn_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, - &warn_no_receive_done, sender); - sender->warn_type = type; - } - sender->suspended++; - } - mh->callback (mh->callback_cls, sender, message); - found = GNUNET_YES; - } - i++; + return GNUNET_SYSERR; + } + if (sender != NULL) + { + if (0 == sender->suspended) + { + sender->warn_start = GNUNET_TIME_absolute_get (); + sender->warn_task = + GNUNET_SCHEDULER_add_delayed + (GNUNET_TIME_UNIT_MINUTES, &warn_no_receive_done, + sender); + sender->warn_type = type; + } + sender->suspended++; + } + mh->callback (mh->callback_cls, sender, message); + found = GNUNET_YES; + } + i++; + } + pos = pos->next; } - pos = pos->next; - } if (found == GNUNET_NO) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, - "Received message of unknown type %d\n", type); - if (server->require_found == GNUNET_YES) - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, + "Received message of unknown type %d\n", type); + if (server->require_found == GNUNET_YES) + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -732,7 +744,8 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, */ static void process_incoming (void *cls, const void *buf, size_t available, - const struct sockaddr *addr, socklen_t addrlen, int errCode); + const struct sockaddr *addr, socklen_t addrlen, + int errCode); /** @@ -752,44 +765,44 @@ static void process_mst (struct GNUNET_SERVER_Client *client, int ret) { while ((ret != GNUNET_SYSERR) && (client->server != NULL) && - (GNUNET_YES != client->shutdown_now) && (0 == client->suspended)) - { - if (ret == GNUNET_OK) + (GNUNET_YES != client->shutdown_now) && (0 == client->suspended)) { - client->receive_pending = GNUNET_YES; + if (ret == GNUNET_OK) + { + client->receive_pending = GNUNET_YES; #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server re-enters receive loop, timeout: %llu.\n", - client->idle_timeout.rel_value); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server re-enters receive loop, timeout: %llu.\n", + client->idle_timeout.rel_value); #endif - GNUNET_CONNECTION_receive (client->connection, - GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, - client->idle_timeout, &process_incoming, - client); - break; - } + GNUNET_CONNECTION_receive (client->connection, + GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, + client->idle_timeout, &process_incoming, + client); + break; + } #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server processes additional messages instantly.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server processes additional messages instantly.\n"); #endif - ret = - GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, - GNUNET_YES); - } + ret = + GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, + GNUNET_YES); + } #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n", - ret, client->server, client->shutdown_now, client->suspended); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n", + ret, client->server, client->shutdown_now, client->suspended); #endif if (ret == GNUNET_NO) - { + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server has more data pending but is suspended.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server has more data pending but is suspended.\n"); #endif - client->receive_pending = GNUNET_SYSERR; /* data pending */ - } + client->receive_pending = GNUNET_SYSERR; /* data pending */ + } if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now)) GNUNET_SERVER_client_disconnect (client); GNUNET_SERVER_client_drop (client); @@ -808,7 +821,7 @@ process_mst (struct GNUNET_SERVER_Client *client, int ret) */ static void process_incoming (void *cls, const void *buf, size_t available, - const struct sockaddr *addr, socklen_t addrlen, int errCode) + const struct sockaddr *addr, socklen_t addrlen, int errCode) { struct GNUNET_SERVER_Client *client = cls; struct GNUNET_SERVER_Handle *server = client->server; @@ -819,43 +832,44 @@ process_incoming (void *cls, const void *buf, size_t available, GNUNET_assert (client->receive_pending == GNUNET_YES); client->receive_pending = GNUNET_NO; now = GNUNET_TIME_absolute_get (); - end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout); + end = + GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout); if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) && (client->shutdown_now != GNUNET_YES) && (server != NULL) && (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) && (end.abs_value > now.abs_value)) - { - /* wait longer, timeout changed (i.e. due to us sending) */ + { + /* wait longer, timeout changed (i.e. due to us sending) */ #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Receive time out, but no disconnect due to sending (%p)\n", - GNUNET_a2s (addr, addrlen)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Receive time out, but no disconnect due to sending (%p)\n", + GNUNET_a2s (addr, addrlen)); #endif - client->receive_pending = GNUNET_YES; - GNUNET_CONNECTION_receive (client->connection, - GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, - GNUNET_TIME_absolute_get_remaining (end), - &process_incoming, client); - return; - } - if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) || - (client->shutdown_now == GNUNET_YES) || - (GNUNET_YES != GNUNET_CONNECTION_check (client->connection))) - { - /* other side closed connection, error connecting, etc. */ - GNUNET_SERVER_client_disconnect (client); - return; - } + client->receive_pending = GNUNET_YES; + GNUNET_CONNECTION_receive (client->connection, + GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, + GNUNET_TIME_absolute_get_remaining (end), + &process_incoming, client); + return; + } + if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) + || (client->shutdown_now == GNUNET_YES) + || (GNUNET_YES != GNUNET_CONNECTION_check (client->connection))) + { + /* other side closed connection, error connecting, etc. */ + GNUNET_SERVER_client_disconnect (client); + return; + } #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n", - (unsigned int) available, GNUNET_a2s (addr, addrlen)); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n", + (unsigned int) available, GNUNET_a2s (addr, addrlen)); #endif GNUNET_SERVER_client_keep (client); client->last_activity = now; ret = - GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, - GNUNET_YES); + GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, + GNUNET_YES); process_mst (client, ret); } @@ -876,25 +890,26 @@ restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) client->restart_task = GNUNET_SCHEDULER_NO_TASK; if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && (GNUNET_NO == server->clients_ignore_shutdown)) - { - GNUNET_SERVER_client_disconnect (client); - return; - } + { + GNUNET_SERVER_client_disconnect (client); + return; + } if (client->receive_pending == GNUNET_NO) - { + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server begins to read again from client.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server begins to read again from client.\n"); #endif - client->receive_pending = GNUNET_YES; - GNUNET_CONNECTION_receive (client->connection, - GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, - client->idle_timeout, &process_incoming, client); - return; - } + client->receive_pending = GNUNET_YES; + GNUNET_CONNECTION_receive (client->connection, + GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, + client->idle_timeout, &process_incoming, + client); + return; + } #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server continues processing messages still in the buffer.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server continues processing messages still in the buffer.\n"); #endif GNUNET_SERVER_client_keep (client); client->receive_pending = GNUNET_NO; @@ -912,7 +927,7 @@ restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ static void client_message_tokenizer_callback (void *cls, void *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { struct GNUNET_SERVER_Handle *server = cls; struct GNUNET_SERVER_Client *sender = client; @@ -920,9 +935,9 @@ client_message_tokenizer_callback (void *cls, void *client, #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Tokenizer gives server message of type %u from client\n", - ntohs (message->type)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tokenizer gives server message of type %u from client\n", + ntohs (message->type)); #endif sender->in_process_client_buffer = GNUNET_YES; ret = GNUNET_SERVER_inject (server, sender, message); @@ -946,14 +961,14 @@ client_message_tokenizer_callback (void *cls, void *client, */ struct GNUNET_SERVER_Client * GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server, - struct GNUNET_CONNECTION_Handle *connection) + struct GNUNET_CONNECTION_Handle *connection) { struct GNUNET_SERVER_Client *client; client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client)); client->connection = connection; client->mst = - GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server); + GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server); client->reference_count = 1; client->server = server; client->last_activity = GNUNET_TIME_absolute_get (); @@ -964,8 +979,8 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server, client->callback = NULL; client->callback_cls = NULL; GNUNET_CONNECTION_receive (client->connection, - GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, - client->idle_timeout, &process_incoming, client); + GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, + client->idle_timeout, &process_incoming, client); return client; } @@ -980,7 +995,7 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server, */ void GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client, - struct GNUNET_TIME_Relative timeout) + struct GNUNET_TIME_Relative timeout) { client->idle_timeout = timeout; } @@ -1028,7 +1043,7 @@ GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client) */ int GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client, - void **addr, size_t * addrlen) + void **addr, size_t * addrlen) { return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen); } @@ -1046,8 +1061,8 @@ GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client, */ void GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server, - GNUNET_SERVER_DisconnectCallback callback, - void *callback_cls) + GNUNET_SERVER_DisconnectCallback callback, + void *callback_cls) { struct NotifyList *n; @@ -1068,8 +1083,8 @@ GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server, */ void GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server, - GNUNET_SERVER_DisconnectCallback - callback, void *callback_cls) + GNUNET_SERVER_DisconnectCallback + callback, void *callback_cls) { struct NotifyList *pos; struct NotifyList *prev; @@ -1077,17 +1092,17 @@ GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server, prev = NULL; pos = server->disconnect_notify_list; while (pos != NULL) - { - if ((pos->callback == callback) && (pos->callback_cls == callback_cls)) - break; - prev = pos; - pos = pos->next; - } + { + if ((pos->callback == callback) && (pos->callback_cls == callback_cls)) + break; + prev = pos; + pos = pos->next; + } if (pos == NULL) - { - GNUNET_break (0); - return; - } + { + GNUNET_break (0); + return; + } if (prev == NULL) server->disconnect_notify_list = pos->next; else @@ -1114,76 +1129,76 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client) unsigned int rc; #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Client is being disconnected from the server.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Client is being disconnected from the server.\n"); #endif if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (client->restart_task); - client->restart_task = GNUNET_SCHEDULER_NO_TASK; - } - if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (client->warn_task); - client->warn_task = GNUNET_SCHEDULER_NO_TASK; - } - if (GNUNET_YES == client->receive_pending) - { - GNUNET_CONNECTION_receive_cancel (client->connection); - client->receive_pending = GNUNET_NO; - } - - rc = client->reference_count; - if (client->server != NULL) - { - server = client->server; - client->server = NULL; - client->shutdown_now = GNUNET_YES; - prev = NULL; - pos = server->clients; - while ((pos != NULL) && (pos != client)) - { - prev = pos; - pos = pos->next; - } - GNUNET_assert (pos != NULL); - if (prev == NULL) - server->clients = pos->next; - else - prev->next = pos->next; - if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) { GNUNET_SCHEDULER_cancel (client->restart_task); client->restart_task = GNUNET_SCHEDULER_NO_TASK; } - if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) + if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) { GNUNET_SCHEDULER_cancel (client->warn_task); client->warn_task = GNUNET_SCHEDULER_NO_TASK; } - n = server->disconnect_notify_list; - while (n != NULL) + if (GNUNET_YES == client->receive_pending) { - n->callback (n->callback_cls, client); - n = n->next; + GNUNET_CONNECTION_receive_cancel (client->connection); + client->receive_pending = GNUNET_NO; + } + + rc = client->reference_count; + if (client->server != NULL) + { + server = client->server; + client->server = NULL; + client->shutdown_now = GNUNET_YES; + prev = NULL; + pos = server->clients; + while ((pos != NULL) && (pos != client)) + { + prev = pos; + pos = pos->next; + } + GNUNET_assert (pos != NULL); + if (prev == NULL) + server->clients = pos->next; + else + prev->next = pos->next; + if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) + { + GNUNET_SCHEDULER_cancel (client->restart_task); + client->restart_task = GNUNET_SCHEDULER_NO_TASK; + } + if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) + { + GNUNET_SCHEDULER_cancel (client->warn_task); + client->warn_task = GNUNET_SCHEDULER_NO_TASK; + } + n = server->disconnect_notify_list; + while (n != NULL) + { + n->callback (n->callback_cls, client); + n = n->next; + } } - } if (rc > 0) - { + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "RC still positive, not destroying everything.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "RC still positive, not destroying everything.\n"); #endif - return; - } + return; + } if (client->in_process_client_buffer == GNUNET_YES) - { + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Still processing inputs, not destroying everything.\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Still processing inputs, not destroying everything.\n"); #endif - return; - } + return; + } if (client->persist == GNUNET_YES) GNUNET_CONNECTION_persist_ (client->connection); @@ -1247,17 +1262,17 @@ transmit_ready_callback_wrapper (void *cls, size_t size, void *buf) */ struct GNUNET_CONNECTION_TransmitHandle * GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client, - size_t size, - struct GNUNET_TIME_Relative timeout, - GNUNET_CONNECTION_TransmitReadyNotify - callback, void *callback_cls) + size_t size, + struct GNUNET_TIME_Relative timeout, + GNUNET_CONNECTION_TransmitReadyNotify + callback, void *callback_cls) { client->callback_cls = callback_cls; client->callback = callback; return GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, - timeout, - &transmit_ready_callback_wrapper, - client); + timeout, + &transmit_ready_callback_wrapper, + client); } @@ -1294,46 +1309,47 @@ GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success) GNUNET_assert (client->suspended > 0); client->suspended--; if (success != GNUNET_OK) - { + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "GNUNET_SERVER_receive_done called with failure indication\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "GNUNET_SERVER_receive_done called with failure indication\n"); #endif - GNUNET_SERVER_client_disconnect (client); - return; - } + GNUNET_SERVER_client_disconnect (client); + return; + } if (client->suspended > 0) - { + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "GNUNET_SERVER_receive_done called, but more clients pending\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "GNUNET_SERVER_receive_done called, but more clients pending\n"); #endif - return; - } + return; + } if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) - { - GNUNET_SCHEDULER_cancel (client->warn_task); - client->warn_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (client->warn_task); + client->warn_task = GNUNET_SCHEDULER_NO_TASK; + } if (client->in_process_client_buffer == GNUNET_YES) - { + { #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "GNUNET_SERVER_receive_done called while still in processing loop\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "GNUNET_SERVER_receive_done called while still in processing loop\n"); #endif - return; - } + return; + } if (client->server == NULL) - { - GNUNET_SERVER_client_disconnect (client); - return; - } + { + GNUNET_SERVER_client_disconnect (client); + return; + } #if DEBUG_SERVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "GNUNET_SERVER_receive_done causes restart in reading from the socket\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "GNUNET_SERVER_receive_done causes restart in reading from the socket\n"); #endif GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task); - client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client); + client->restart_task = + GNUNET_SCHEDULER_add_now (&restart_processing, client); } diff --git a/src/util/server_mst.c b/src/util/server_mst.c index adb0a0818..6fd2647d7 100644 --- a/src/util/server_mst.c +++ b/src/util/server_mst.c @@ -39,6 +39,8 @@ #define ALIGN_FACTOR 8 #endif +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Handle to a message stream tokenizer. @@ -89,7 +91,7 @@ struct GNUNET_SERVER_MessageStreamTokenizer */ struct GNUNET_SERVER_MessageStreamTokenizer * GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb, - void *cb_cls) + void *cb_cls) { struct GNUNET_SERVER_MessageStreamTokenizer *ret; @@ -119,8 +121,8 @@ GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb, */ int GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, - void *client_identity, const char *buf, size_t size, - int purge, int one_shot) + void *client_identity, const char *buf, + size_t size, int purge, int one_shot) { const struct GNUNET_MessageHeader *hdr; size_t delta; @@ -131,161 +133,161 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, int ret; #if DEBUG_SERVER_MST - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server-mst receives %u bytes with %u bytes already in private buffer\n", - (unsigned int) size, (unsigned int) (mst->pos - mst->off)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server-mst receives %u bytes with %u bytes already in private buffer\n", + (unsigned int) size, (unsigned int) (mst->pos - mst->off)); #endif ret = GNUNET_OK; ibuf = (char *) mst->hdr; while (mst->pos > 0) - { -do_align: - if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) || - (0 != (mst->off % ALIGN_FACTOR))) - { - /* need to align or need more space */ - mst->pos -= mst->off; - memmove (ibuf, &ibuf[mst->off], mst->pos); - mst->off = 0; - } - if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) - { - delta = - GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - - (mst->pos - mst->off), size); - memcpy (&ibuf[mst->pos], buf, delta); - mst->pos += delta; - buf += delta; - size -= delta; - } - if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) - { - if (purge) - { - mst->off = 0; - mst->pos = 0; - } - return GNUNET_OK; - } - hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; - want = ntohs (hdr->size); - if (want < sizeof (struct GNUNET_MessageHeader)) { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - if (mst->curr_buf - mst->off < want) - { - /* need more space */ - mst->pos -= mst->off; - memmove (ibuf, &ibuf[mst->off], mst->pos); - mst->off = 0; - } - if (want > mst->curr_buf) - { - mst->hdr = GNUNET_realloc (mst->hdr, want); - ibuf = (char *) mst->hdr; - mst->curr_buf = want; - } - hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; - if (mst->pos - mst->off < want) - { - delta = GNUNET_MIN (want - (mst->pos - mst->off), size); - memcpy (&ibuf[mst->pos], buf, delta); - mst->pos += delta; - buf += delta; - size -= delta; - } - if (mst->pos - mst->off < want) - { - if (purge) - { - mst->off = 0; - mst->pos = 0; - } - return GNUNET_OK; - } - if (one_shot == GNUNET_SYSERR) - { - /* cannot call callback again, but return value saying that - * we have another full message in the buffer */ - ret = GNUNET_NO; - goto copy; - } - if (one_shot == GNUNET_YES) - one_shot = GNUNET_SYSERR; - mst->cb (mst->cb_cls, client_identity, hdr); - mst->off += want; - if (mst->off == mst->pos) - { - /* reset to beginning of buffer, it's free right now! */ - mst->off = 0; - mst->pos = 0; - } - } - while (size > 0) - { -#if DEBUG_SERVER_MST - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server-mst has %u bytes left in inbound buffer\n", - (unsigned int) size); -#endif - if (size < sizeof (struct GNUNET_MessageHeader)) - break; - offset = (unsigned long) buf; - need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO; - if (GNUNET_NO == need_align) - { - /* can try to do zero-copy and process directly from original buffer */ - hdr = (const struct GNUNET_MessageHeader *) buf; + do_align: + if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) || + (0 != (mst->off % ALIGN_FACTOR))) + { + /* need to align or need more space */ + mst->pos -= mst->off; + memmove (ibuf, &ibuf[mst->off], mst->pos); + mst->off = 0; + } + if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) + { + delta = + GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - + (mst->pos - mst->off), size); + memcpy (&ibuf[mst->pos], buf, delta); + mst->pos += delta; + buf += delta; + size -= delta; + } + if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) + { + if (purge) + { + mst->off = 0; + mst->pos = 0; + } + return GNUNET_OK; + } + hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; want = ntohs (hdr->size); if (want < sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_break_op (0); - mst->off = 0; - return GNUNET_SYSERR; - } - if (size < want) - break; /* or not, buffer incomplete, so copy to private buffer... */ + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + if (mst->curr_buf - mst->off < want) + { + /* need more space */ + mst->pos -= mst->off; + memmove (ibuf, &ibuf[mst->off], mst->pos); + mst->off = 0; + } + if (want > mst->curr_buf) + { + mst->hdr = GNUNET_realloc (mst->hdr, want); + ibuf = (char *) mst->hdr; + mst->curr_buf = want; + } + hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; + if (mst->pos - mst->off < want) + { + delta = GNUNET_MIN (want - (mst->pos - mst->off), size); + memcpy (&ibuf[mst->pos], buf, delta); + mst->pos += delta; + buf += delta; + size -= delta; + } + if (mst->pos - mst->off < want) + { + if (purge) + { + mst->off = 0; + mst->pos = 0; + } + return GNUNET_OK; + } if (one_shot == GNUNET_SYSERR) - { - /* cannot call callback again, but return value saying that - * we have another full message in the buffer */ - ret = GNUNET_NO; - goto copy; - } + { + /* cannot call callback again, but return value saying that + * we have another full message in the buffer */ + ret = GNUNET_NO; + goto copy; + } if (one_shot == GNUNET_YES) - one_shot = GNUNET_SYSERR; + one_shot = GNUNET_SYSERR; mst->cb (mst->cb_cls, client_identity, hdr); - buf += want; - size -= want; + mst->off += want; + if (mst->off == mst->pos) + { + /* reset to beginning of buffer, it's free right now! */ + mst->off = 0; + mst->pos = 0; + } } - else + while (size > 0) { - /* need to copy to private buffer to align; - * yes, we go a bit more spagetti than usual here */ - goto do_align; +#if DEBUG_SERVER_MST + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server-mst has %u bytes left in inbound buffer\n", + (unsigned int) size); +#endif + if (size < sizeof (struct GNUNET_MessageHeader)) + break; + offset = (unsigned long) buf; + need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO; + if (GNUNET_NO == need_align) + { + /* can try to do zero-copy and process directly from original buffer */ + hdr = (const struct GNUNET_MessageHeader *) buf; + want = ntohs (hdr->size); + if (want < sizeof (struct GNUNET_MessageHeader)) + { + GNUNET_break_op (0); + mst->off = 0; + return GNUNET_SYSERR; + } + if (size < want) + break; /* or not, buffer incomplete, so copy to private buffer... */ + if (one_shot == GNUNET_SYSERR) + { + /* cannot call callback again, but return value saying that + * we have another full message in the buffer */ + ret = GNUNET_NO; + goto copy; + } + if (one_shot == GNUNET_YES) + one_shot = GNUNET_SYSERR; + mst->cb (mst->cb_cls, client_identity, hdr); + buf += want; + size -= want; + } + else + { + /* need to copy to private buffer to align; + * yes, we go a bit more spagetti than usual here */ + goto do_align; + } } - } copy: if ((size > 0) && (!purge)) - { - if (size + mst->pos > mst->curr_buf) { - mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos); - ibuf = (char *) mst->hdr; - mst->curr_buf = size + mst->pos; + if (size + mst->pos > mst->curr_buf) + { + mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos); + ibuf = (char *) mst->hdr; + mst->curr_buf = size + mst->pos; + } + GNUNET_assert (mst->pos + size <= mst->curr_buf); + memcpy (&ibuf[mst->pos], buf, size); + mst->pos += size; } - GNUNET_assert (mst->pos + size <= mst->curr_buf); - memcpy (&ibuf[mst->pos], buf, size); - mst->pos += size; - } if (purge) mst->off = 0; #if DEBUG_SERVER_MST - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server-mst leaves %u bytes in private buffer\n", - (unsigned int) (mst->pos - mst->off)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Server-mst leaves %u bytes in private buffer\n", + (unsigned int) (mst->pos - mst->off)); #endif return ret; } diff --git a/src/util/server_nc.c b/src/util/server_nc.c index dc7a27c8b..cbe98a014 100644 --- a/src/util/server_nc.c +++ b/src/util/server_nc.c @@ -33,6 +33,8 @@ #include "gnunet_server_lib.h" #include "gnunet_time_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + #define DEBUG_SERVER_NC GNUNET_EXTRA_LOGGING @@ -154,40 +156,40 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) struct PendingMessageList *pml; if (client == NULL) - { - nc->server = NULL; - return; - } + { + nc->server = NULL; + return; + } prev = NULL; pos = nc->clients; while (NULL != pos) - { - if (pos->client == client) - break; - prev = pos; - pos = pos->next; - } + { + if (pos->client == client) + break; + prev = pos; + pos = pos->next; + } if (pos == NULL) return; #if DEBUG_SERVER_NC - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Client disconnected, cleaning up %u messages in NC queue\n", - pos->num_pending); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Client disconnected, cleaning up %u messages in NC queue\n", + pos->num_pending); #endif if (prev == NULL) nc->clients = pos->next; else prev->next = pos->next; while (NULL != (pml = pos->pending_head)) - { - GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml); - GNUNET_free (pml); - } + { + GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml); + GNUNET_free (pml); + } if (pos->th != NULL) - { - GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th); - pos->th = NULL; - } + { + GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th); + pos->th = NULL; + } GNUNET_SERVER_client_drop (client); GNUNET_free (pos); } @@ -203,8 +205,8 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) * @return handle to the notification context */ struct GNUNET_SERVER_NotificationContext * -GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server, - unsigned int queue_length) +GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle + *server, unsigned int queue_length) { struct GNUNET_SERVER_NotificationContext *ret; @@ -223,26 +225,27 @@ GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server, */ void GNUNET_SERVER_notification_context_destroy (struct - GNUNET_SERVER_NotificationContext - *nc) + GNUNET_SERVER_NotificationContext + *nc) { struct ClientList *pos; struct PendingMessageList *pml; while (NULL != (pos = nc->clients)) - { - nc->clients = pos->next; - GNUNET_SERVER_client_drop (pos->client); - while (NULL != (pml = pos->pending_head)) { - GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml); - GNUNET_free (pml); + nc->clients = pos->next; + GNUNET_SERVER_client_drop (pos->client); + while (NULL != (pml = pos->pending_head)) + { + GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, + pml); + GNUNET_free (pml); + } + GNUNET_free (pos); } - GNUNET_free (pos); - } if (nc->server != NULL) GNUNET_SERVER_disconnect_notify_cancel (nc->server, - &handle_client_disconnect, nc); + &handle_client_disconnect, nc); GNUNET_free (nc); } @@ -254,9 +257,9 @@ GNUNET_SERVER_notification_context_destroy (struct * @param client client to add */ void -GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext - *nc, - struct GNUNET_SERVER_Client *client) +GNUNET_SERVER_notification_context_add (struct + GNUNET_SERVER_NotificationContext *nc, + struct GNUNET_SERVER_Client *client) { struct ClientList *cl; @@ -290,44 +293,45 @@ transmit_message (void *cls, size_t size, void *buf) cl->th = NULL; if (buf == NULL) - { - /* 'cl' should be freed via disconnect notification shortly */ + { + /* 'cl' should be freed via disconnect notification shortly */ #if DEBUG_SERVER_NC - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Failed to transmit message from NC queue to client\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Failed to transmit message from NC queue to client\n"); #endif - return 0; - } + return 0; + } ret = 0; while (NULL != (pml = cl->pending_head)) - { - msize = ntohs (pml->msg->size); - if (size < msize) - break; - GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml); + { + msize = ntohs (pml->msg->size); + if (size < msize) + break; + GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml); #if DEBUG_SERVER_NC - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Copying message of type %u and size %u from pending queue to transmission buffer\n", - ntohs (pml->msg->type), msize); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Copying message of type %u and size %u from pending queue to transmission buffer\n", + ntohs (pml->msg->type), msize); #endif - memcpy (&cbuf[ret], pml->msg, msize); - ret += msize; - size -= msize; - GNUNET_free (pml); - cl->num_pending--; - } + memcpy (&cbuf[ret], pml->msg, msize); + ret += msize; + size -= msize; + GNUNET_free (pml); + cl->num_pending--; + } if (pml != NULL) - { + { #if DEBUG_SERVER_NC - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Have %u messages left in NC queue, will try transmission again\n", - cl->num_pending); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Have %u messages left in NC queue, will try transmission again\n", + cl->num_pending); #endif - cl->th = - GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size), - GNUNET_TIME_UNIT_FOREVER_REL, - &transmit_message, cl); - } + cl->th = + GNUNET_SERVER_notify_transmit_ready (cl->client, + ntohs (pml->msg->size), + GNUNET_TIME_UNIT_FOREVER_REL, + &transmit_message, cl); + } else GNUNET_assert (cl->num_pending == 0); return ret; @@ -344,47 +348,46 @@ transmit_message (void *cls, size_t size, void *buf) */ static void do_unicast (struct GNUNET_SERVER_NotificationContext *nc, - struct ClientList *client, const struct GNUNET_MessageHeader *msg, - int can_drop) + struct ClientList *client, const struct GNUNET_MessageHeader *msg, + int can_drop) { struct PendingMessageList *pml; uint16_t size; if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop)) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Dropping message of type %u and size %u due to full queue (%u entries)\n", - ntohs (msg->type), ntohs (msg->size), - (unsigned int) nc->queue_length); - return; /* drop! */ - } + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Dropping message of type %u and size %u due to full queue (%u entries)\n", + ntohs (msg->type), ntohs (msg->size), + (unsigned int) nc->queue_length); + return; /* drop! */ + } if (client->num_pending > nc->queue_length) - { - /* FIXME: consider checking for other messages in the - * queue that are 'droppable' */ - } + { + /* FIXME: consider checking for other messages in the + * queue that are 'droppable' */ + } client->num_pending++; size = ntohs (msg->size); pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size); pml->msg = (const struct GNUNET_MessageHeader *) &pml[1]; pml->can_drop = can_drop; #if DEBUG_SERVER_NC - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding message of type %u and size %u to pending queue (which has %u entries)\n", - ntohs (msg->type), ntohs (msg->size), - (unsigned int) nc->queue_length); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Adding message of type %u and size %u to pending queue (which has %u entries)\n", + ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length); #endif memcpy (&pml[1], msg, size); /* append */ - GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail, - pml); + GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, + client->pending_tail, pml); if (client->th == NULL) client->th = - GNUNET_SERVER_notify_transmit_ready (client->client, - ntohs (client->pending_head-> - msg->size), - GNUNET_TIME_UNIT_FOREVER_REL, - &transmit_message, client); + GNUNET_SERVER_notify_transmit_ready (client->client, + ntohs (client->pending_head-> + msg->size), + GNUNET_TIME_UNIT_FOREVER_REL, + &transmit_message, client); } @@ -399,21 +402,22 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc, */ void GNUNET_SERVER_notification_context_unicast (struct - GNUNET_SERVER_NotificationContext - *nc, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader - *msg, int can_drop) + GNUNET_SERVER_NotificationContext + *nc, + struct GNUNET_SERVER_Client + *client, + const struct GNUNET_MessageHeader + *msg, int can_drop) { struct ClientList *pos; pos = nc->clients; while (NULL != pos) - { - if (pos->client == client) - break; - pos = pos->next; - } + { + if (pos->client == client) + break; + pos = pos->next; + } GNUNET_assert (pos != NULL); do_unicast (nc, pos, msg, can_drop); } @@ -428,19 +432,20 @@ GNUNET_SERVER_notification_context_unicast (struct */ void GNUNET_SERVER_notification_context_broadcast (struct - GNUNET_SERVER_NotificationContext - *nc, - const struct GNUNET_MessageHeader - *msg, int can_drop) + GNUNET_SERVER_NotificationContext + *nc, + const struct + GNUNET_MessageHeader *msg, + int can_drop) { struct ClientList *pos; pos = nc->clients; while (NULL != pos) - { - do_unicast (nc, pos, msg, can_drop); - pos = pos->next; - } + { + do_unicast (nc, pos, msg, can_drop); + pos = pos->next; + } } diff --git a/src/util/server_tc.c b/src/util/server_tc.c index 5e9a9e946..020fd86db 100644 --- a/src/util/server_tc.c +++ b/src/util/server_tc.c @@ -33,6 +33,8 @@ #include "gnunet_time_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * How much buffer space do we want to have at least @@ -81,10 +83,10 @@ transmit_response (void *cls, size_t size, void *buf) size_t msize; if (buf == NULL) - { - GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); - return 0; - } + { + GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); + return 0; + } if (tc->total - tc->off > size) msize = size; else @@ -92,27 +94,28 @@ transmit_response (void *cls, size_t size, void *buf) memcpy (buf, &tc->buf[tc->off], msize); tc->off += msize; if (tc->total == tc->off) - { + { - GNUNET_SERVER_receive_done (tc->client, GNUNET_OK); - GNUNET_SERVER_client_drop (tc->client); - GNUNET_free_non_null (tc->buf); - GNUNET_free (tc); - } + GNUNET_SERVER_receive_done (tc->client, GNUNET_OK); + GNUNET_SERVER_client_drop (tc->client); + GNUNET_free_non_null (tc->buf); + GNUNET_free (tc); + } else - { - if (NULL == - GNUNET_SERVER_notify_transmit_ready (tc->client, - GNUNET_MIN (MIN_BLOCK_SIZE, - tc->total - tc->off), - GNUNET_TIME_absolute_get_remaining - (tc->timeout), &transmit_response, - tc)) { - GNUNET_break (0); - GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); + if (NULL == + GNUNET_SERVER_notify_transmit_ready (tc->client, + GNUNET_MIN (MIN_BLOCK_SIZE, + tc->total - + tc->off), + GNUNET_TIME_absolute_get_remaining + (tc->timeout), + &transmit_response, tc)) + { + GNUNET_break (0); + GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); + } } - } return msize; } @@ -148,9 +151,10 @@ GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client) * @param type type of the message */ void -GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext - *tc, const void *data, - size_t length, uint16_t type) +GNUNET_SERVER_transmit_context_append_data (struct + GNUNET_SERVER_TransmitContext *tc, + const void *data, size_t length, + uint16_t type) { struct GNUNET_MessageHeader *msg; size_t size; @@ -177,10 +181,10 @@ GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext */ void GNUNET_SERVER_transmit_context_append_message (struct - GNUNET_SERVER_TransmitContext - *tc, - const struct GNUNET_MessageHeader - *msg) + GNUNET_SERVER_TransmitContext + *tc, + const struct + GNUNET_MessageHeader *msg) { struct GNUNET_MessageHeader *m; uint16_t size; @@ -204,18 +208,18 @@ GNUNET_SERVER_transmit_context_append_message (struct */ void GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc, - struct GNUNET_TIME_Relative timeout) + struct GNUNET_TIME_Relative timeout) { tc->timeout = GNUNET_TIME_relative_to_absolute (timeout); if (NULL == GNUNET_SERVER_notify_transmit_ready (tc->client, - GNUNET_MIN (MIN_BLOCK_SIZE, - tc->total), timeout, - &transmit_response, tc)) - { - GNUNET_break (0); - GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); - } + GNUNET_MIN (MIN_BLOCK_SIZE, + tc->total), timeout, + &transmit_response, tc)) + { + GNUNET_break (0); + GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); + } } @@ -233,8 +237,7 @@ GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc, */ void GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext - *tc, - int success) + *tc, int success) { GNUNET_SERVER_receive_done (tc->client, success); GNUNET_SERVER_client_drop (tc->client); diff --git a/src/util/service.c b/src/util/service.c index 15937e117..7cafe35c8 100644 --- a/src/util/service.c +++ b/src/util/service.c @@ -36,6 +36,12 @@ #include "gnunet_server_lib.h" #include "gnunet_service_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + +#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) + #define DEBUG_SERVICE GNUNET_EXTRA_LOGGING /* ******************* access control ******************** */ @@ -98,120 +104,121 @@ parse_ipv4_specification (const char *routeList) i = 0; pos = 0; while (i < count) - { - cnt = - sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0], - &temps[1], &temps[2], &temps[3], &temps[4], &temps[5], - &temps[6], &temps[7]); - if (cnt == 8) - { - for (j = 0; j < 8; j++) - if (temps[j] > 0xFF) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Invalid format for IP: `%s'\n"), &routeList[pos]); - GNUNET_free (result); - return NULL; - } - result[i].network.s_addr = - htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + - temps[3]); - result[i].netmask.s_addr = - htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) + - temps[7]); - while (routeList[pos] != ';') - pos++; - pos++; - i++; - continue; - } - /* try second notation */ - cnt = - sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1], - &temps[2], &temps[3], &slash); - if (cnt == 5) { - for (j = 0; j < 4; j++) - if (temps[j] > 0xFF) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Invalid format for IP: `%s'\n"), &routeList[pos]); - GNUNET_free (result); - return NULL; - } - result[i].network.s_addr = - htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + - temps[3]); - if ((slash <= 32) && (slash >= 0)) - { - result[i].netmask.s_addr = 0; - while (slash > 0) - { - result[i].netmask.s_addr = - (result[i].netmask.s_addr >> 1) + 0x80000000; - slash--; - } - result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); - while (routeList[pos] != ';') - pos++; - pos++; - i++; - continue; - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), - slash); - GNUNET_free (result); - return NULL; /* error */ - } + cnt = + sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0], + &temps[1], &temps[2], &temps[3], &temps[4], &temps[5], + &temps[6], &temps[7]); + if (cnt == 8) + { + for (j = 0; j < 8; j++) + if (temps[j] > 0xFF) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Invalid format for IP: `%s'\n"), &routeList[pos]); + GNUNET_free (result); + return NULL; + } + result[i].network.s_addr = + htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + + temps[3]); + result[i].netmask.s_addr = + htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) + + temps[7]); + while (routeList[pos] != ';') + pos++; + pos++; + i++; + continue; + } + /* try second notation */ + cnt = + sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1], + &temps[2], &temps[3], &slash); + if (cnt == 5) + { + for (j = 0; j < 4; j++) + if (temps[j] > 0xFF) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Invalid format for IP: `%s'\n"), &routeList[pos]); + GNUNET_free (result); + return NULL; + } + result[i].network.s_addr = + htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + + temps[3]); + if ((slash <= 32) && (slash >= 0)) + { + result[i].netmask.s_addr = 0; + while (slash > 0) + { + result[i].netmask.s_addr = + (result[i].netmask.s_addr >> 1) + 0x80000000; + slash--; + } + result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); + while (routeList[pos] != ';') + pos++; + pos++; + i++; + continue; + } + else + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), + slash); + GNUNET_free (result); + return NULL; /* error */ + } + } + /* try third notation */ + slash = 32; + cnt = + sscanf (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1], + &temps[2], &temps[3]); + if (cnt == 4) + { + for (j = 0; j < 4; j++) + if (temps[j] > 0xFF) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Invalid format for IP: `%s'\n"), &routeList[pos]); + GNUNET_free (result); + return NULL; + } + result[i].network.s_addr = + htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + + temps[3]); + result[i].netmask.s_addr = 0; + while (slash > 0) + { + result[i].netmask.s_addr = + (result[i].netmask.s_addr >> 1) + 0x80000000; + slash--; + } + result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); + while (routeList[pos] != ';') + pos++; + pos++; + i++; + continue; + } + LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), + &routeList[pos]); + GNUNET_free (result); + return NULL; /* error */ } - /* try third notation */ - slash = 32; - cnt = - sscanf (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1], - &temps[2], &temps[3]); - if (cnt == 4) + if (pos < strlen (routeList)) { - for (j = 0; j < 4; j++) - if (temps[j] > 0xFF) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Invalid format for IP: `%s'\n"), &routeList[pos]); - GNUNET_free (result); - return NULL; - } - result[i].network.s_addr = - htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + - temps[3]); - result[i].netmask.s_addr = 0; - while (slash > 0) - { - result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000; - slash--; - } - result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); - while (routeList[pos] != ';') - pos++; - pos++; - i++; - continue; + LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), + &routeList[pos]); + GNUNET_free (result); + return NULL; /* oops */ } - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), - &routeList[pos]); - GNUNET_free (result); - return NULL; /* error */ - } - if (pos < strlen (routeList)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), - &routeList[pos]); - GNUNET_free (result); - return NULL; /* oops */ - } - return result; /* ok */ + return result; /* ok */ } @@ -253,84 +260,85 @@ parse_ipv6_specification (const char *routeListX) if (routeList[i] == ';') count++; if (routeList[len - 1] != ';') - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Invalid network notation (does not end with ';': `%s')\n"), - routeList); - GNUNET_free (routeList); - return NULL; - } + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Invalid network notation (does not end with ';': `%s')\n"), + routeList); + GNUNET_free (routeList); + return NULL; + } result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1)); memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1)); i = 0; pos = 0; while (i < count) - { - start = pos; - while (routeList[pos] != ';') - pos++; - slash = pos; - while ((slash >= start) && (routeList[slash] != '/')) - slash--; - if (slash < start) { - memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); + start = pos; + while (routeList[pos] != ';') + pos++; slash = pos; - } - else - { - routeList[pos] = '\0'; - ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask); - if (ret <= 0) - { - save = errno; - if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128)) - { - if (ret == 0) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Wrong format `%s' for netmask\n"), - &routeList[slash + 1]); - else - { - errno = save; - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); - } - GNUNET_free (result); - GNUNET_free (routeList); - return NULL; - } - off = 0; - while (bits > 8) - { - result[i].netmask.s6_addr[off++] = 0xFF; - bits -= 8; - } - while (bits > 0) - { - result[i].netmask.s6_addr[off] = - (result[i].netmask.s6_addr[off] >> 1) + 0x80; - bits--; - } - } - } - routeList[slash] = '\0'; - ret = inet_pton (AF_INET6, &routeList[start], &result[i].network); - if (ret <= 0) - { - if (ret == 0) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Wrong format `%s' for network\n"), - &routeList[slash + 1]); + while ((slash >= start) && (routeList[slash] != '/')) + slash--; + if (slash < start) + { + memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); + slash = pos; + } else - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); - GNUNET_free (result); - GNUNET_free (routeList); - return NULL; + { + routeList[pos] = '\0'; + ret = + inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask); + if (ret <= 0) + { + save = errno; + if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) + || (bits >= 128)) + { + if (ret == 0) + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Wrong format `%s' for netmask\n"), + &routeList[slash + 1]); + else + { + errno = save; + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); + } + GNUNET_free (result); + GNUNET_free (routeList); + return NULL; + } + off = 0; + while (bits > 8) + { + result[i].netmask.s6_addr[off++] = 0xFF; + bits -= 8; + } + while (bits > 0) + { + result[i].netmask.s6_addr[off] = + (result[i].netmask.s6_addr[off] >> 1) + 0x80; + bits--; + } + } + } + routeList[slash] = '\0'; + ret = inet_pton (AF_INET6, &routeList[start], &result[i].network); + if (ret <= 0) + { + if (ret == 0) + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Wrong format `%s' for network\n"), &routeList[slash + 1]); + else + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); + GNUNET_free (result); + GNUNET_free (routeList); + return NULL; + } + pos++; + i++; } - pos++; - i++; - } GNUNET_free (routeList); return result; } @@ -344,7 +352,8 @@ parse_ipv6_specification (const char *routeListX) * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is */ static int -check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add) +check_ipv4_listed (const struct IPv4NetworkSet *list, + const struct in_addr *add) { int i; @@ -353,12 +362,12 @@ check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add) return GNUNET_NO; while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0)) - { - if ((add->s_addr & list[i].netmask.s_addr) == - (list[i].network.s_addr & list[i].netmask.s_addr)) - return GNUNET_YES; - i++; - } + { + if ((add->s_addr & list[i].netmask.s_addr) == + (list[i].network.s_addr & list[i].netmask.s_addr)) + return GNUNET_YES; + i++; + } return GNUNET_NO; } @@ -370,7 +379,8 @@ check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add) * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is */ static int -check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip) +check_ipv6_listed (const struct IPv6NetworkSet *list, + const struct in6_addr *ip) { unsigned int i; unsigned int j; @@ -383,16 +393,16 @@ check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip) i = 0; NEXT: while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0) - { - for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++) - if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) != - (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j])) - { - i++; - goto NEXT; - } - return GNUNET_YES; - } + { + for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++) + if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) != + (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j])) + { + i++; + goto NEXT; + } + return GNUNET_YES; + } return GNUNET_NO; } @@ -526,10 +536,10 @@ write_test (void *cls, size_t size, void *buf) struct GNUNET_MessageHeader *msg; if (size < sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return 0; /* client disconnected */ - } + { + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return 0; /* client disconnected */ + } msg = (struct GNUNET_MessageHeader *) buf; msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); @@ -546,14 +556,15 @@ write_test (void *cls, size_t size, void *buf) */ static void handle_test (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { /* simply bounce message back to acknowledge */ if (NULL == GNUNET_SERVER_notify_transmit_ready (client, - sizeof (struct GNUNET_MessageHeader), - GNUNET_TIME_UNIT_FOREVER_REL, - &write_test, client)) + sizeof (struct + GNUNET_MessageHeader), + GNUNET_TIME_UNIT_FOREVER_REL, + &write_test, client)) GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); } @@ -586,7 +597,7 @@ static const struct GNUNET_SERVER_MessageHandler defhandlers[] = { */ static int check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, - const struct sockaddr *addr, socklen_t addrlen) + const struct sockaddr *addr, socklen_t addrlen) { struct GNUNET_SERVICE_Context *sctx = cls; const struct sockaddr_in *i4; @@ -594,51 +605,51 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, int ret; switch (addr->sa_family) - { - case AF_INET: - GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); - i4 = (const struct sockaddr_in *) addr; - ret = ((sctx->v4_allowed == NULL) || - (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) && - ((sctx->v4_denied == NULL) || - (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr))); - break; - case AF_INET6: - GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); - i6 = (const struct sockaddr_in6 *) addr; - ret = ((sctx->v6_allowed == NULL) || - (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) && - ((sctx->v6_denied == NULL) || - (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr))); - break; + { + case AF_INET: + GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); + i4 = (const struct sockaddr_in *) addr; + ret = ((sctx->v4_allowed == NULL) || + (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) && + ((sctx->v4_denied == NULL) || + (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr))); + break; + case AF_INET6: + GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); + i6 = (const struct sockaddr_in6 *) addr; + ret = ((sctx->v6_allowed == NULL) || + (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) && + ((sctx->v6_denied == NULL) || + (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr))); + break; #ifndef WINDOWS - case AF_UNIX: - ret = GNUNET_OK; /* always OK for now */ - if ((sctx->match_uid == GNUNET_YES) || (sctx->match_gid == GNUNET_YES)) - ret = GNUNET_NO; - if ((uc != NULL) && - ((sctx->match_uid != GNUNET_YES) || (uc->uid == geteuid ()) || - (uc->uid == getuid ())) && ((sctx->match_gid != GNUNET_YES) || - (uc->gid == getegid ()) || - (uc->gid == getgid ()))) - ret = GNUNET_YES; - else - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Access denied to UID %d / GID %d\n"), - (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid); - break; + case AF_UNIX: + ret = GNUNET_OK; /* always OK for now */ + if ((sctx->match_uid == GNUNET_YES) || (sctx->match_gid == GNUNET_YES)) + ret = GNUNET_NO; + if ((uc != NULL) && + ((sctx->match_uid != GNUNET_YES) || (uc->uid == geteuid ()) || + (uc->uid == getuid ())) && ((sctx->match_gid != GNUNET_YES) || + (uc->gid == getegid ()) || + (uc->gid == getgid ()))) + ret = GNUNET_YES; + else + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Access denied to UID %d / GID %d\n"), + (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid); + break; #endif - default: - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"), - addr->sa_family); - return GNUNET_SYSERR; - } + default: + LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"), + addr->sa_family); + return GNUNET_SYSERR; + } if (ret != GNUNET_OK) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Access from `%s' denied to service `%s'\n"), - GNUNET_a2s (addr, addrlen), sctx->serviceName); - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Access from `%s' denied to service `%s'\n"), + GNUNET_a2s (addr, addrlen), sctx->serviceName); + } return ret; } @@ -655,7 +666,7 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx) if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName, - "PIDFILE", &pif)) + "PIDFILE", &pif)) return NULL; return pif; } @@ -665,26 +676,26 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx) * Parse an IPv4 access control list. */ static int -process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, - const char *option) +process_acl4 (struct IPv4NetworkSet **ret, + struct GNUNET_SERVICE_Context *sctx, const char *option) { char *opt; if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option)) return GNUNET_OK; GNUNET_break (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (sctx->cfg, - sctx->serviceName, - option, &opt)); + GNUNET_CONFIGURATION_get_value_string (sctx->cfg, + sctx->serviceName, + option, &opt)); if (NULL == (*ret = parse_ipv4_specification (opt))) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ - ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"), - opt, sctx->serviceName, option); - GNUNET_free (opt); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _ + ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"), + opt, sctx->serviceName, option); + GNUNET_free (opt); + return GNUNET_SYSERR; + } GNUNET_free (opt); return GNUNET_OK; } @@ -694,26 +705,26 @@ process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, * Parse an IPv4 access control list. */ static int -process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, - const char *option) +process_acl6 (struct IPv6NetworkSet **ret, + struct GNUNET_SERVICE_Context *sctx, const char *option) { char *opt; if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option)) return GNUNET_OK; GNUNET_break (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (sctx->cfg, - sctx->serviceName, - option, &opt)); + GNUNET_CONFIGURATION_get_value_string (sctx->cfg, + sctx->serviceName, + option, &opt)); if (NULL == (*ret = parse_ipv6_specification (opt))) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ - ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"), - opt, sctx->serviceName, option); - GNUNET_free (opt); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _ + ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"), + opt, sctx->serviceName, option); + GNUNET_free (opt); + return GNUNET_SYSERR; + } GNUNET_free (opt); return GNUNET_OK; } @@ -728,7 +739,7 @@ process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, */ static void add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens, - const char *unixpath) + const char *unixpath) { #ifdef AF_UNIX struct sockaddr_un *un; @@ -780,9 +791,9 @@ add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens, */ int GNUNET_SERVICE_get_server_addresses (const char *serviceName, - const struct GNUNET_CONFIGURATION_Handle - *cfg, struct sockaddr ***addrs, - socklen_t ** addr_lens) + const struct GNUNET_CONFIGURATION_Handle + *cfg, struct sockaddr ***addrs, + socklen_t ** addr_lens) { int disablev6; struct GNUNET_NETWORK_Handle *desc; @@ -803,62 +814,64 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, *addr_lens = NULL; desc = NULL; if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6")) - { - if (GNUNET_SYSERR == - (disablev6 = - GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, "DISABLEV6"))) - return GNUNET_SYSERR; - } + { + if (GNUNET_SYSERR == + (disablev6 = + GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, + "DISABLEV6"))) + return GNUNET_SYSERR; + } else disablev6 = GNUNET_NO; if (!disablev6) - { - /* probe IPv6 support */ - desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); - if (NULL == desc) { - if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || - (errno == EACCES)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); - return GNUNET_SYSERR; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _ - ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"), - serviceName, STRERROR (errno)); - disablev6 = GNUNET_YES; - } - else - { - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc)); - desc = NULL; + /* probe IPv6 support */ + desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); + if (NULL == desc) + { + if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || + (errno == EACCES)) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); + return GNUNET_SYSERR; + } + LOG (GNUNET_ERROR_TYPE_INFO, + _ + ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"), + serviceName, STRERROR (errno)); + disablev6 = GNUNET_YES; + } + else + { + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc)); + desc = NULL; + } } - } port = 0; if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT")) - { - GNUNET_break (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_number (cfg, serviceName, - "PORT", &port)); - if (port > 65535) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("Require valid port number for service `%s' in configuration!\n"), - serviceName); - return GNUNET_SYSERR; + GNUNET_break (GNUNET_OK == + GNUNET_CONFIGURATION_get_value_number (cfg, serviceName, + "PORT", &port)); + if (port > 65535) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Require valid port number for service `%s' in configuration!\n"), + serviceName); + return GNUNET_SYSERR; + } } - } if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO")) - { - GNUNET_break (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, - "BINDTO", &hostname)); - } + { + GNUNET_break (GNUNET_OK == + GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, + "BINDTO", + &hostname)); + } else hostname = NULL; @@ -868,211 +881,211 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH", - &unixpath)) && + &unixpath)) && (0 < strlen (unixpath))) - { - /* probe UNIX support */ - struct sockaddr_un s_un; - - if (strlen (unixpath) >= sizeof (s_un.sun_path)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("UNIXPATH `%s' too long, maximum length is %llu\n"), - unixpath, sizeof (s_un.sun_path)); - GNUNET_free_non_null (hostname); - GNUNET_free (unixpath); - return GNUNET_SYSERR; - } - - desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); - if (NULL == desc) { - if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || - (errno == EACCES)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); - GNUNET_free_non_null (hostname); - GNUNET_free (unixpath); - return GNUNET_SYSERR; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _ - ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"), - serviceName, STRERROR (errno)); - GNUNET_free (unixpath); - unixpath = NULL; - } - else - { - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc)); - desc = NULL; + /* probe UNIX support */ + struct sockaddr_un s_un; + + if (strlen (unixpath) >= sizeof (s_un.sun_path)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("UNIXPATH `%s' too long, maximum length is %llu\n"), + unixpath, sizeof (s_un.sun_path)); + GNUNET_free_non_null (hostname); + GNUNET_free (unixpath); + return GNUNET_SYSERR; + } + + desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); + if (NULL == desc) + { + if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || + (errno == EACCES)) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); + GNUNET_free_non_null (hostname); + GNUNET_free (unixpath); + return GNUNET_SYSERR; + } + LOG (GNUNET_ERROR_TYPE_INFO, + _ + ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"), + serviceName, STRERROR (errno)); + GNUNET_free (unixpath); + unixpath = NULL; + } + else + { + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc)); + desc = NULL; + } } - } #endif if ((port == 0) && (unixpath == NULL)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"), - serviceName); - GNUNET_free_non_null (hostname); - return GNUNET_SYSERR; - } - if (port == 0) - { - saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *)); - saddrlens = GNUNET_malloc (2 * sizeof (socklen_t)); - add_unixpath (saddrs, saddrlens, unixpath); - GNUNET_free_non_null (unixpath); - GNUNET_free_non_null (hostname); - *addrs = saddrs; - *addr_lens = saddrlens; - return 1; - } - - if (hostname != NULL) - { -#if DEBUG_SERVICE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Resolving `%s' since that is where `%s' will bind to.\n", - hostname, serviceName); -#endif - memset (&hints, 0, sizeof (struct addrinfo)); - if (disablev6) - hints.ai_family = AF_INET; - if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) || - (res == NULL)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), - hostname, gai_strerror (ret)); - GNUNET_free (hostname); - GNUNET_free_non_null (unixpath); - return GNUNET_SYSERR; - } - next = res; - i = 0; - while (NULL != (pos = next)) - { - next = pos->ai_next; - if ((disablev6) && (pos->ai_family == AF_INET6)) - continue; - i++; - } - if (0 == i) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to find %saddress for `%s'.\n"), - disablev6 ? "IPv4 " : "", hostname); - freeaddrinfo (res); - GNUNET_free (hostname); - GNUNET_free_non_null (unixpath); + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"), + serviceName); + GNUNET_free_non_null (hostname); return GNUNET_SYSERR; } - resi = i; - if (NULL != unixpath) - resi++; - saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); - saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); - i = 0; - if (NULL != unixpath) + if (port == 0) { + saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *)); + saddrlens = GNUNET_malloc (2 * sizeof (socklen_t)); add_unixpath (saddrs, saddrlens, unixpath); - i++; + GNUNET_free_non_null (unixpath); + GNUNET_free_non_null (hostname); + *addrs = saddrs; + *addr_lens = saddrlens; + return 1; } - next = res; - while (NULL != (pos = next)) + + if (hostname != NULL) { - next = pos->ai_next; - if ((disablev6) && (pos->ai_family == AF_INET6)) - continue; - if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0)) - continue; /* not TCP */ - if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0)) - continue; /* huh? */ #if DEBUG_SERVICE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n", - serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Resolving `%s' since that is where `%s' will bind to.\n", + hostname, serviceName); #endif - if (pos->ai_family == AF_INET) - { - GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in)); - saddrlens[i] = pos->ai_addrlen; - saddrs[i] = GNUNET_malloc (saddrlens[i]); - memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); - ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); - } - else - { - GNUNET_assert (pos->ai_family == AF_INET6); - GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6)); - saddrlens[i] = pos->ai_addrlen; - saddrs[i] = GNUNET_malloc (saddrlens[i]); - memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); - ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port); - } - i++; - } - GNUNET_free (hostname); - freeaddrinfo (res); - resi = i; - } - else - { - /* will bind against everything, just set port */ - if (disablev6) - { - /* V4-only */ - resi = 1; - if (NULL != unixpath) - resi++; + memset (&hints, 0, sizeof (struct addrinfo)); + if (disablev6) + hints.ai_family = AF_INET; + if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) || + (res == NULL)) + { + LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), + hostname, gai_strerror (ret)); + GNUNET_free (hostname); + GNUNET_free_non_null (unixpath); + return GNUNET_SYSERR; + } + next = res; i = 0; + while (NULL != (pos = next)) + { + next = pos->ai_next; + if ((disablev6) && (pos->ai_family == AF_INET6)) + continue; + i++; + } + if (0 == i) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Failed to find %saddress for `%s'.\n"), + disablev6 ? "IPv4 " : "", hostname); + freeaddrinfo (res); + GNUNET_free (hostname); + GNUNET_free_non_null (unixpath); + return GNUNET_SYSERR; + } + resi = i; + if (NULL != unixpath) + resi++; saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); + i = 0; if (NULL != unixpath) - { - add_unixpath (saddrs, saddrlens, unixpath); - i++; - } - saddrlens[i] = sizeof (struct sockaddr_in); - saddrs[i] = GNUNET_malloc (saddrlens[i]); -#if HAVE_SOCKADDR_IN_SIN_LEN - ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i]; + { + add_unixpath (saddrs, saddrlens, unixpath); + i++; + } + next = res; + while (NULL != (pos = next)) + { + next = pos->ai_next; + if ((disablev6) && (pos->ai_family == AF_INET6)) + continue; + if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0)) + continue; /* not TCP */ + if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0)) + continue; /* huh? */ +#if DEBUG_SERVICE + LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n", + serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen)); #endif - ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; - ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); + if (pos->ai_family == AF_INET) + { + GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in)); + saddrlens[i] = pos->ai_addrlen; + saddrs[i] = GNUNET_malloc (saddrlens[i]); + memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); + ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); + } + else + { + GNUNET_assert (pos->ai_family == AF_INET6); + GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6)); + saddrlens[i] = pos->ai_addrlen; + saddrs[i] = GNUNET_malloc (saddrlens[i]); + memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); + ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port); + } + i++; + } + GNUNET_free (hostname); + freeaddrinfo (res); + resi = i; } - else + else { - /* dual stack */ - resi = 2; - if (NULL != unixpath) - resi++; - saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); - saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); - i = 0; - if (NULL != unixpath) - { - add_unixpath (saddrs, saddrlens, unixpath); - i++; - } - saddrlens[i] = sizeof (struct sockaddr_in6); - saddrs[i] = GNUNET_malloc (saddrlens[i]); + /* will bind against everything, just set port */ + if (disablev6) + { + /* V4-only */ + resi = 1; + if (NULL != unixpath) + resi++; + i = 0; + saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); + saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); + if (NULL != unixpath) + { + add_unixpath (saddrs, saddrlens, unixpath); + i++; + } + saddrlens[i] = sizeof (struct sockaddr_in); + saddrs[i] = GNUNET_malloc (saddrlens[i]); #if HAVE_SOCKADDR_IN_SIN_LEN - ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0]; + ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i]; #endif - ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6; - ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port); - i++; - saddrlens[i] = sizeof (struct sockaddr_in); - saddrs[i] = GNUNET_malloc (saddrlens[i]); + ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; + ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); + } + else + { + /* dual stack */ + resi = 2; + if (NULL != unixpath) + resi++; + saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); + saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); + i = 0; + if (NULL != unixpath) + { + add_unixpath (saddrs, saddrlens, unixpath); + i++; + } + saddrlens[i] = sizeof (struct sockaddr_in6); + saddrs[i] = GNUNET_malloc (saddrlens[i]); #if HAVE_SOCKADDR_IN_SIN_LEN - ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1]; + ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0]; #endif - ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; - ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); + ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6; + ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port); + i++; + saddrlens[i] = sizeof (struct sockaddr_in); + saddrs[i] = GNUNET_malloc (saddrlens[i]); +#if HAVE_SOCKADDR_IN_SIN_LEN + ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1]; +#endif + ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; + ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); + } } - } GNUNET_free_non_null (unixpath); *addrs = saddrs; *addr_lens = saddrlens; @@ -1111,36 +1124,37 @@ setup_service (struct GNUNET_SERVICE_Context *sctx) int flags; #endif - if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TIMEOUT")) - { - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName, - "TIMEOUT", &idleout)) + if (GNUNET_CONFIGURATION_have_value + (sctx->cfg, sctx->serviceName, "TIMEOUT")) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Specified value for `%s' of service `%s' is invalid\n"), - "TIMEOUT", sctx->serviceName); - return GNUNET_SYSERR; + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName, + "TIMEOUT", &idleout)) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Specified value for `%s' of service `%s' is invalid\n"), + "TIMEOUT", sctx->serviceName); + return GNUNET_SYSERR; + } + sctx->timeout = idleout; } - sctx->timeout = idleout; - } else sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL; if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TOLERANT")) - { - if (GNUNET_SYSERR == - (tolerant = - GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, - "TOLERANT"))) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Specified value for `%s' of service `%s' is invalid\n"), - "TOLERANT", sctx->serviceName); - return GNUNET_SYSERR; + if (GNUNET_SYSERR == + (tolerant = + GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, + "TOLERANT"))) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Specified value for `%s' of service `%s' is invalid\n"), + "TOLERANT", sctx->serviceName); + return GNUNET_SYSERR; + } } - } else tolerant = GNUNET_NO; @@ -1151,45 +1165,48 @@ setup_service (struct GNUNET_SERVICE_Context *sctx) (NULL != (nfds = getenv ("LISTEN_FDS"))) && (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) && (cnt + 4 < FD_SETSIZE)) - { - sctx->lsocks = - GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1)); - while (0 < cnt--) { - flags = fcntl (3 + cnt, F_GETFD); - if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) || - (NULL == - (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt)))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("Could not access pre-bound socket %u, will try to bind myself\n"), - (unsigned int) 3 + cnt); - cnt++; - while (sctx->lsocks[cnt] != NULL) - GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++])); - GNUNET_free (sctx->lsocks); - sctx->lsocks = NULL; - break; - } + sctx->lsocks = + GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1)); + while (0 < cnt--) + { + flags = fcntl (3 + cnt, F_GETFD); + if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) || + (NULL == + (sctx->lsocks[cnt] = + GNUNET_NETWORK_socket_box_native (3 + cnt)))) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Could not access pre-bound socket %u, will try to bind myself\n"), + (unsigned int) 3 + cnt); + cnt++; + while (sctx->lsocks[cnt] != NULL) + GNUNET_break (0 == + GNUNET_NETWORK_socket_close (sctx->lsocks + [cnt++])); + GNUNET_free (sctx->lsocks); + sctx->lsocks = NULL; + break; + } + } + unsetenv ("LISTEN_PID"); + unsetenv ("LISTEN_FDS"); } - unsetenv ("LISTEN_PID"); - unsetenv ("LISTEN_FDS"); - } #endif if ((sctx->lsocks == NULL) && (GNUNET_SYSERR == GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg, - &sctx->addrs, &sctx->addrlens))) + &sctx->addrs, &sctx->addrlens))) return GNUNET_SYSERR; sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES; sctx->match_uid = - GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, - "UNIX_MATCH_UID"); + GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, + "UNIX_MATCH_UID"); sctx->match_gid = - GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, - "UNIX_MATCH_GID"); + GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, + "UNIX_MATCH_GID"); process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM"); process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM"); process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6"); @@ -1211,7 +1228,7 @@ get_user_name (struct GNUNET_SERVICE_Context *sctx) if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName, - "USERNAME", &un)) + "USERNAME", &un)) return NULL; return un; } @@ -1229,7 +1246,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid) int len; if (NULL == (pif = get_pid_file_name (sctx))) - return GNUNET_OK; /* no file desired */ + return GNUNET_OK; /* no file desired */ user = get_user_name (sctx); rdir = GNUNET_strdup (pif); len = strlen (rdir); @@ -1237,32 +1254,32 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid) len--; rdir[len] = '\0'; if (0 != ACCESS (rdir, F_OK)) - { - /* we get to create a directory -- and claim it - * as ours! */ - GNUNET_DISK_directory_create (rdir); - if ((user != NULL) && (0 < strlen (user))) - GNUNET_DISK_file_change_owner (rdir, user); - } + { + /* we get to create a directory -- and claim it + * as ours! */ + GNUNET_DISK_directory_create (rdir); + if ((user != NULL) && (0 < strlen (user))) + GNUNET_DISK_file_change_owner (rdir, user); + } if (0 != ACCESS (rdir, W_OK | X_OK)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir); - GNUNET_free (rdir); - GNUNET_free_non_null (user); - GNUNET_free (pif); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir); + GNUNET_free (rdir); + GNUNET_free_non_null (user); + GNUNET_free (pif); + return GNUNET_SYSERR; + } GNUNET_free (rdir); pidfd = FOPEN (pif, "w"); if (pidfd == NULL) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif); - GNUNET_free (pif); - GNUNET_free_non_null (user); - return GNUNET_SYSERR; - } + { + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif); + GNUNET_free (pif); + GNUNET_free_non_null (user); + return GNUNET_SYSERR; + } if (0 > FPRINTF (pidfd, "%u", pid)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif); GNUNET_break (0 == fclose (pidfd)); if ((user != NULL) && (0 < strlen (user))) GNUNET_DISK_file_change_owner (pif, user); @@ -1299,35 +1316,35 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_RESOLVER_connect (sctx->cfg); if (sctx->lsocks != NULL) sctx->server = - GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, - sctx->timeout, sctx->require_found); + GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, + sctx->timeout, sctx->require_found); else sctx->server = - GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, - sctx->timeout, sctx->require_found); + GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, + sctx->timeout, sctx->require_found); if (sctx->server == NULL) - { - if (sctx->addrs != NULL) { - i = 0; - while (sctx->addrs[i] != NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"), - sctx->serviceName, GNUNET_a2s (sctx->addrs[i], - sctx->addrlens[i])); - i++; - } + if (sctx->addrs != NULL) + { + i = 0; + while (sctx->addrs[i] != NULL) + { + LOG (GNUNET_ERROR_TYPE_INFO, + _("Failed to start `%s' at `%s'\n"), sctx->serviceName, + GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i])); + i++; + } + } + sctx->ret = GNUNET_SYSERR; + return; } - sctx->ret = GNUNET_SYSERR; - return; - } if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN)) - { - /* install a task that will kill the server - * process if the scheduler ever gets a shutdown signal */ - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, - sctx->server); - } + { + /* install a task that will kill the server + * process if the scheduler ever gets a shutdown signal */ + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, + &shutdown_task, sctx->server); + } sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); i = 0; @@ -1335,23 +1352,23 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sctx->my_handlers[i++].callback_cls = sctx; GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers); if (sctx->ready_confirm_fd != -1) - { - GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1)); - GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd)); - sctx->ready_confirm_fd = -1; - write_pid_file (sctx, getpid ()); - } + { + GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1)); + GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd)); + sctx->ready_confirm_fd = -1; + write_pid_file (sctx, getpid ()); + } if (sctx->addrs != NULL) - { - i = 0; - while (sctx->addrs[i] != NULL) { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"), - sctx->serviceName, GNUNET_a2s (sctx->addrs[i], - sctx->addrlens[i])); - i++; + i = 0; + while (sctx->addrs[i] != NULL) + { + LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"), + sctx->serviceName, GNUNET_a2s (sctx->addrs[i], + sctx->addrlens[i])); + i++; + } } - } sctx->task (sctx->task_cls, sctx->server, sctx->cfg); } @@ -1368,45 +1385,45 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx) int filedes[2]; if (0 != PIPE (filedes)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe"); + return GNUNET_SYSERR; + } pid = fork (); if (pid < 0) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); + return GNUNET_SYSERR; + } if (pid != 0) - { - /* Parent */ - char c; - - GNUNET_break (0 == CLOSE (filedes[1])); - c = 'X'; - if (1 != READ (filedes[0], &c, sizeof (char))) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read"); - fflush (stdout); - switch (c) { - case '.': - exit (0); - case 'I': - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Service process failed to initialize\n")); - break; - case 'S': - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Service process could not initialize server function\n")); - break; - case 'X': - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Service process failed to report status\n")); - break; + /* Parent */ + char c; + + GNUNET_break (0 == CLOSE (filedes[1])); + c = 'X'; + if (1 != READ (filedes[0], &c, sizeof (char))) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read"); + fflush (stdout); + switch (c) + { + case '.': + exit (0); + case 'I': + LOG (GNUNET_ERROR_TYPE_INFO, + _("Service process failed to initialize\n")); + break; + case 'S': + LOG (GNUNET_ERROR_TYPE_INFO, + _("Service process could not initialize server function\n")); + break; + case 'X': + LOG (GNUNET_ERROR_TYPE_INFO, + _("Service process failed to report status\n")); + break; + } + exit (1); /* child reported error */ } - exit (1); /* child reported error */ - } GNUNET_break (0 == CLOSE (0)); GNUNET_break (0 == CLOSE (1)); GNUNET_break (0 == CLOSE (filedes[0])); @@ -1415,16 +1432,16 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx) return GNUNET_SYSERR; /* set stdin/stdout to /dev/null */ if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2"); - (void) CLOSE (nullfd); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2"); + (void) CLOSE (nullfd); + return GNUNET_SYSERR; + } (void) CLOSE (nullfd); /* Detach from controlling terminal */ pid = setsid (); if (pid == -1) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid"); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid"); sctx->ready_confirm_fd = filedes[1]; #else /* FIXME: we probably need to do something else @@ -1444,36 +1461,36 @@ set_user_id (struct GNUNET_SERVICE_Context *sctx) char *user; if (NULL == (user = get_user_name (sctx))) - return GNUNET_OK; /* keep */ + return GNUNET_OK; /* keep */ #ifndef MINGW struct passwd *pws; errno = 0; pws = getpwnam (user); if (pws == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Cannot obtain information about user `%s': %s\n"), user, - errno == 0 ? _("No such user") : STRERROR (errno)); - GNUNET_free (user); - return GNUNET_SYSERR; - } + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Cannot obtain information about user `%s': %s\n"), user, + errno == 0 ? _("No such user") : STRERROR (errno)); + GNUNET_free (user); + return GNUNET_SYSERR; + } if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) || #if HAVE_INITGROUPS (0 != initgroups (user, pws->pw_gid)) || #endif (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid))) - { - if ((0 != setregid (pws->pw_gid, pws->pw_gid)) || - (0 != setreuid (pws->pw_uid, pws->pw_uid))) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Cannot change user/group to `%s': %s\n"), user, - STRERROR (errno)); - GNUNET_free (user); - return GNUNET_SYSERR; + if ((0 != setregid (pws->pw_gid, pws->pw_gid)) || + (0 != setreuid (pws->pw_uid, pws->pw_uid))) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Cannot change user/group to `%s': %s\n"), user, + STRERROR (errno)); + GNUNET_free (user); + return GNUNET_SYSERR; + } } - } #endif GNUNET_free (user); return GNUNET_OK; @@ -1489,9 +1506,9 @@ pid_file_delete (struct GNUNET_SERVICE_Context *sctx) char *pif = get_pid_file_name (sctx); if (pif == NULL) - return; /* no PID file */ + return; /* no PID file */ if (0 != UNLINK (pif)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif); GNUNET_free (pif); } @@ -1511,8 +1528,8 @@ pid_file_delete (struct GNUNET_SERVICE_Context *sctx) */ int GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, - enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task, - void *task_cls) + enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task, + void *task_cls) { #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0) @@ -1568,24 +1585,24 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, if (GNUNET_OK != set_user_id (&sctx)) goto shutdown; #if DEBUG_SERVICE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Service `%s' runs with configuration from `%s'\n", serviceName, - cfg_fn); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Service `%s' runs with configuration from `%s'\n", serviceName, + cfg_fn); #endif if (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", "skew_offset", - &skew_offset) && - (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", - "skew_variance", &skew_variance))) - { - clock_offset = skew_offset - skew_variance; - GNUNET_TIME_set_offset (clock_offset); + GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", + "skew_offset", &skew_offset) + && (GNUNET_OK == + GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", + "skew_variance", + &skew_variance))) + { + clock_offset = skew_offset - skew_variance; + GNUNET_TIME_set_offset (clock_offset); #if DEBUG_SERVICE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll\n", - clock_offset); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll\n", clock_offset); #endif - } + } /* actually run service */ err = 0; GNUNET_SCHEDULER_run (&service_task, &sctx); @@ -1597,11 +1614,11 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, shutdown: if (sctx.ready_confirm_fd != -1) - { - if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write"); - GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd)); - } + { + if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1)) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write"); + GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd)); + } GNUNET_CONFIGURATION_destroy (cfg); i = 0; @@ -1632,13 +1649,13 @@ shutdown: */ struct GNUNET_SERVICE_Context * GNUNET_SERVICE_start (const char *serviceName, - const struct GNUNET_CONFIGURATION_Handle *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { int i; struct GNUNET_SERVICE_Context *sctx; sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context)); - sctx->ready_confirm_fd = -1; /* no daemonizing */ + sctx->ready_confirm_fd = -1; /* no daemonizing */ sctx->ret = GNUNET_OK; sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL; sctx->serviceName = serviceName; @@ -1646,24 +1663,24 @@ GNUNET_SERVICE_start (const char *serviceName, /* setup subsystems */ if (GNUNET_OK != setup_service (sctx)) - { - GNUNET_SERVICE_stop (sctx); - return NULL; - } + { + GNUNET_SERVICE_stop (sctx); + return NULL; + } if (sctx->lsocks != NULL) sctx->server = - GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, - sctx->timeout, sctx->require_found); + GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, + sctx->timeout, sctx->require_found); else sctx->server = - GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, - sctx->timeout, sctx->require_found); + GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, + sctx->timeout, sctx->require_found); if (NULL == sctx->server) - { - GNUNET_SERVICE_stop (sctx); - return NULL; - } + { + GNUNET_SERVICE_stop (sctx); + return NULL; + } sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); i = 0; @@ -1701,12 +1718,12 @@ GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx) GNUNET_SERVER_destroy (sctx->server); GNUNET_free_non_null (sctx->my_handlers); if (sctx->addrs != NULL) - { - i = 0; - while (sctx->addrs[i] != NULL) - GNUNET_free (sctx->addrs[i++]); - GNUNET_free (sctx->addrs); - } + { + i = 0; + while (sctx->addrs[i] != NULL) + GNUNET_free (sctx->addrs[i++]); + GNUNET_free (sctx->addrs); + } GNUNET_free_non_null (sctx->addrlens); GNUNET_free_non_null (sctx->v4_denied); GNUNET_free_non_null (sctx->v6_denied); diff --git a/src/util/signal.c b/src/util/signal.c index 395e8e517..a986b6260 100644 --- a/src/util/signal.c +++ b/src/util/signal.c @@ -28,6 +28,9 @@ #include "gnunet_common.h" #include "gnunet_signal_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + + struct GNUNET_SIGNAL_Context { int sig; @@ -60,7 +63,7 @@ GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler) sig.sa_handler = (void *) handler; sigemptyset (&sig.sa_mask); #ifdef SA_INTERRUPT - sig.sa_flags = SA_INTERRUPT; /* SunOS */ + sig.sa_flags = SA_INTERRUPT; /* SunOS */ #else sig.sa_flags = SA_RESTART; #endif @@ -69,15 +72,15 @@ GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler) if (signum == GNUNET_SIGCHLD) w32_sigchld_handler = handler; else - { - __p_sig_fn_t sigret = signal (signum, (__p_sig_fn_t) handler); - - if (sigret == SIG_ERR) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("signal (%d, %p) returned %d.\n"), signum, handler, sigret); + __p_sig_fn_t sigret = signal (signum, (__p_sig_fn_t) handler); + + if (sigret == SIG_ERR) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("signal (%d, %p) returned %d.\n"), signum, handler, sigret); + } } - } #endif return ret; } diff --git a/src/util/strings.c b/src/util/strings.c index e2ca69e10..9e9aac3b1 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -32,6 +32,10 @@ #include "gnunet_common.h" #include "gnunet_strings_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + +#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) + /** * Fill a buffer of the given size with @@ -55,7 +59,8 @@ * (or number of bytes that would have been written) */ size_t -GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) +GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, + ...) { size_t needed; size_t slen; @@ -65,18 +70,18 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) needed = 0; va_start (ap, count); while (count > 0) - { - s = va_arg (ap, const char *); - - slen = strlen (s) + 1; - if (buffer != NULL) { - GNUNET_assert (needed + slen <= size); - memcpy (&buffer[needed], s, slen); + s = va_arg (ap, const char *); + + slen = strlen (s) + 1; + if (buffer != NULL) + { + GNUNET_assert (needed + slen <= size); + memcpy (&buffer[needed], s, slen); + } + needed += slen; + count--; } - needed += slen; - count--; - } va_end (ap); return needed; } @@ -97,7 +102,7 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) */ unsigned int GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, - unsigned int count, ...) + unsigned int count, ...) { unsigned int start; unsigned int needed; @@ -107,21 +112,21 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, needed = 0; va_start (ap, count); while (count > 0) - { - r = va_arg (ap, const char **); - - start = needed; - while ((needed < size) && (buffer[needed] != '\0')) - needed++; - if (needed == size) { - va_end (ap); - return 0; /* error */ + r = va_arg (ap, const char **); + + start = needed; + while ((needed < size) && (buffer[needed] != '\0')) + needed++; + if (needed == size) + { + va_end (ap); + return 0; /* error */ + } + *r = &buffer[start]; + needed++; /* skip 0-termination */ + count--; } - *r = &buffer[start]; - needed++; /* skip 0-termination */ - count--; - } va_end (ap); return needed; } @@ -140,25 +145,25 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size) char *ret; if (size > 5 * 1024) - { - size = size / 1024; - unit = _( /* size unit */ "KiB"); - if (size > 5 * 1024) { size = size / 1024; - unit = _( /* size unit */ "MiB"); + unit = _( /* size unit */ "KiB"); if (size > 5 * 1024) - { - size = size / 1024; - unit = _( /* size unit */ "GiB"); - if (size > 5 * 1024) - { - size = size / 1024; - unit = _( /* size unit */ "TiB"); - } - } + { + size = size / 1024; + unit = _( /* size unit */ "MiB"); + if (size > 5 * 1024) + { + size = size / 1024; + unit = _( /* size unit */ "GiB"); + if (size > 5 * 1024) + { + size = size / 1024; + unit = _( /* size unit */ "TiB"); + } + } + } } - } ret = GNUNET_malloc (32); GNUNET_snprintf (ret, 32, "%llu %s", size, unit); return ret; @@ -187,41 +192,41 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) cd = iconv_open ("UTF-8", charset); if (cd == (iconv_t) - 1) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_open"); - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Character set requested was `%s'\n"), charset); - ret = GNUNET_malloc (len + 1); - memcpy (ret, input, len); - ret[len] = '\0'; - return ret; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open"); + LOG (GNUNET_ERROR_TYPE_WARNING, + _("Character set requested was `%s'\n"), charset); + ret = GNUNET_malloc (len + 1); + memcpy (ret, input, len); + ret[len] = '\0'; + return ret; + } tmpSize = 3 * len + 4; tmp = GNUNET_malloc (tmpSize); itmp = tmp; finSize = tmpSize; if (iconv (cd, #if FREEBSD || DARWIN || WINDOWS - (const char **) &input, + (const char **) &input, #else - (char **) &input, + (char **) &input, #endif - &len, &itmp, &finSize) == SIZE_MAX) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv"); - iconv_close (cd); - GNUNET_free (tmp); - ret = GNUNET_malloc (len + 1); - memcpy (ret, input, len); - ret[len] = '\0'; - return ret; - } + &len, &itmp, &finSize) == SIZE_MAX) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv"); + iconv_close (cd); + GNUNET_free (tmp); + ret = GNUNET_malloc (len + 1); + memcpy (ret, input, len); + ret[len] = '\0'; + return ret; + } ret = GNUNET_malloc (tmpSize - finSize + 1); memcpy (ret, tmp, tmpSize - finSize); ret[tmpSize - finSize] = '\0'; GNUNET_free (tmp); if (0 != iconv_close (cd)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_close"); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close"); return ret; #else ret = GNUNET_malloc (len + 1); @@ -262,89 +267,89 @@ GNUNET_STRINGS_filename_expand (const char *fil) /* absolute path, just copy */ return GNUNET_strdup (fil); if (fil[0] == '~') - { - fm = getenv ("HOME"); - if (fm == NULL) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ - ("Failed to expand `$HOME': environment variable `HOME' not set")); - return NULL; + fm = getenv ("HOME"); + if (fm == NULL) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _ + ("Failed to expand `$HOME': environment variable `HOME' not set")); + return NULL; + } + fm = GNUNET_strdup (fm); + /* do not copy '~' */ + fil_ptr = fil + 1; + + /* skip over dir seperator to be consistent */ + if (fil_ptr[0] == DIR_SEPARATOR) + fil_ptr++; } - fm = GNUNET_strdup (fm); - /* do not copy '~' */ - fil_ptr = fil + 1; - - /* skip over dir seperator to be consistent */ - if (fil_ptr[0] == DIR_SEPARATOR) - fil_ptr++; - } else - { - /* relative path */ - fil_ptr = fil; - len = 512; - fm = NULL; - while (1) { - buffer = GNUNET_malloc (len); - if (getcwd (buffer, len) != NULL) - { - fm = buffer; - break; - } - if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) - { - len *= 2; - GNUNET_free (buffer); - continue; - } - GNUNET_free (buffer); - break; + /* relative path */ + fil_ptr = fil; + len = 512; + fm = NULL; + while (1) + { + buffer = GNUNET_malloc (len); + if (getcwd (buffer, len) != NULL) + { + fm = buffer; + break; + } + if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) + { + len *= 2; + GNUNET_free (buffer); + continue; + } + GNUNET_free (buffer); + break; + } + if (fm == NULL) + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd"); + buffer = getenv ("PWD"); /* alternative */ + if (buffer != NULL) + fm = GNUNET_strdup (buffer); + } + if (fm == NULL) + fm = GNUNET_strdup ("./"); /* give up */ } - if (fm == NULL) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getcwd"); - buffer = getenv ("PWD"); /* alternative */ - if (buffer != NULL) - fm = GNUNET_strdup (buffer); - } - if (fm == NULL) - fm = GNUNET_strdup ("./"); /* give up */ - } n = strlen (fm) + 1 + strlen (fil_ptr) + 1; buffer = GNUNET_malloc (n); GNUNET_snprintf (buffer, n, "%s%s%s", fm, - (fm[strlen (fm) - 1] == - DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr); + (fm[strlen (fm) - 1] == + DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr); GNUNET_free (fm); return buffer; #else fn = GNUNET_malloc (MAX_PATH + 1); if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS) - { - SetErrnoFromWinError (lRet); - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); - return NULL; - } + { + SetErrnoFromWinError (lRet); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); + return NULL; + } /* is the path relative? */ if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0)) - { - char szCurDir[MAX_PATH + 1]; - - lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir); - if (lRet + strlen (fn) + 1 > (MAX_PATH + 1)) { - SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW); - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory"); - return NULL; + char szCurDir[MAX_PATH + 1]; + + lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir); + if (lRet + strlen (fn) + 1 > (MAX_PATH + 1)) + { + SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW); + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory"); + return NULL; + } + buffer = GNUNET_malloc (MAX_PATH + 1); + GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn); + GNUNET_free (fn); + fn = buffer; } - buffer = GNUNET_malloc (MAX_PATH + 1); - GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn); - GNUNET_free (fn); - fn = buffer; - } return fn; #endif @@ -367,25 +372,25 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta) if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) return GNUNET_strdup (_("eternity")); if (dval > 5 * 1000) - { - dval = dval / 1000; - unit = _( /* time unit */ "s"); - if (dval > 5 * 60) { - dval = dval / 60; - unit = _( /* time unit */ "m"); + dval = dval / 1000; + unit = _( /* time unit */ "s"); if (dval > 5 * 60) - { - dval = dval / 60; - unit = _( /* time unit */ "h"); - if (dval > 5 * 24) - { - dval = dval / 24; - unit = _( /* time unit */ " days"); - } - } + { + dval = dval / 60; + unit = _( /* time unit */ "m"); + if (dval > 5 * 60) + { + dval = dval / 60; + unit = _( /* time unit */ "h"); + if (dval > 5 * 24) + { + dval = dval / 24; + unit = _( /* time unit */ " days"); + } + } + } } - } GNUNET_asprintf (&ret, "%llu %s", dval, unit); return ret; } diff --git a/src/util/test_bio.c b/src/util/test_bio.c index df4becdd1..e72c95295 100644 --- a/src/util/test_bio.c +++ b/src/util/test_bio.c @@ -57,16 +57,17 @@ test_normal_rw () GNUNET_assert (NULL != fileR); readResultString = NULL; GNUNET_assert (GNUNET_OK == - GNUNET_BIO_read_string (fileR, "Read string error", - &readResultString, 200)); + GNUNET_BIO_read_string (fileR, "Read string error", + &readResultString, 200)); GNUNET_assert (NULL != readResultString); GNUNET_assert (0 == strcmp (TESTSTRING, readResultString)); GNUNET_free (readResultString); GNUNET_assert (GNUNET_OK == - GNUNET_BIO_read_meta_data (fileR, "Read meta error", - &metaDataR)); + GNUNET_BIO_read_meta_data (fileR, "Read meta error", + &metaDataR)); GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_meta_data_test_equal (metaDataR, metaDataW)); + GNUNET_CONTAINER_meta_data_test_equal (metaDataR, + metaDataW)); GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_int64 (fileR, &testNum)); GNUNET_BIO_read_close (fileR, &msg); GNUNET_CONTAINER_meta_data_destroy (metaDataW); @@ -93,8 +94,8 @@ test_nullstring_rw () fileR = GNUNET_BIO_read_open (fileName); GNUNET_assert (NULL != fileR); GNUNET_assert (GNUNET_OK == - GNUNET_BIO_read_string (fileR, "Read string error", - &readResultString, 200)); + GNUNET_BIO_read_string (fileR, "Read string error", + &readResultString, 200)); GNUNET_assert (NULL == readResultString); GNUNET_BIO_read_close (fileR, &msg); GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName)); @@ -121,8 +122,8 @@ test_emptystring_rw () GNUNET_assert (NULL != fileR); readResultString = NULL; GNUNET_assert (GNUNET_OK == - GNUNET_BIO_read_string (fileR, "Read string error", - &readResultString, 200)); + GNUNET_BIO_read_string (fileR, "Read string error", + &readResultString, 200)); GNUNET_free (readResultString); GNUNET_BIO_read_close (fileR, &msg); GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName)); @@ -148,8 +149,8 @@ test_bigstring_rw () GNUNET_assert (NULL != fileR); readResultString = NULL; GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read_string (fileR, "Read string error", - &readResultString, 1)); + GNUNET_BIO_read_string (fileR, "Read string error", + &readResultString, 1)); GNUNET_assert (NULL == readResultString); msg = NULL; GNUNET_BIO_read_close (fileR, &msg); @@ -181,8 +182,8 @@ test_bigmeta_rw () GNUNET_assert (NULL != fileR); metaDataR = NULL; GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read_meta_data (fileR, "Read meta error", - &metaDataR)); + GNUNET_BIO_read_meta_data (fileR, "Read meta error", + &metaDataR)); msg = NULL; GNUNET_BIO_read_close (fileR, &msg); GNUNET_free (msg); @@ -203,8 +204,8 @@ test_directory_r () fileR = GNUNET_BIO_read_open ("/dev"); GNUNET_assert (NULL != fileR); GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read (fileR, "Read error", readResult, - sizeof (readResult))); + GNUNET_BIO_read (fileR, "Read error", readResult, + sizeof (readResult))); msg = NULL; GNUNET_BIO_read_close (fileR, &msg); GNUNET_free (msg); @@ -267,18 +268,18 @@ test_fullfile_rw () fileR = GNUNET_BIO_read_open ("/dev/null"); GNUNET_assert (NULL != fileR); GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read (fileR, "Read error", readResult, - sizeof (readResult))); + GNUNET_BIO_read (fileR, "Read error", readResult, + sizeof (readResult))); readResultString = NULL; GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read_string (fileR, "Read string error", - &readResultString, 200)); + GNUNET_BIO_read_string (fileR, "Read string error", + &readResultString, 200)); GNUNET_assert (NULL == readResultString); GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_int64 (fileR, &testNum)); metaDataR = NULL; GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read_meta_data (fileR, "Read meta error", - &metaDataR)); + GNUNET_BIO_read_meta_data (fileR, "Read meta error", + &metaDataR)); msg = NULL; GNUNET_BIO_read_close (fileR, &msg); GNUNET_free (msg); @@ -305,8 +306,8 @@ test_fakestring_rw () fileR = GNUNET_BIO_read_open (fileName); GNUNET_assert (NULL != fileR); GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read_string (fileR, "Read string error", - &readResult, 200)); + GNUNET_BIO_read_string (fileR, "Read string error", + &readResult, 200)); msg = NULL; GNUNET_BIO_read_close (fileR, &msg); GNUNET_free (msg); @@ -334,8 +335,8 @@ test_fakemeta_rw () GNUNET_assert (NULL != fileR); metaDataR = NULL; GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read_meta_data (fileR, "Read meta error", - &metaDataR)); + GNUNET_BIO_read_meta_data (fileR, "Read meta error", + &metaDataR)); GNUNET_assert (NULL == metaDataR); msg = NULL; GNUNET_BIO_read_close (fileR, &msg); @@ -364,8 +365,8 @@ test_fakebigmeta_rw () GNUNET_assert (NULL != fileR); metaDataR = NULL; GNUNET_assert (GNUNET_SYSERR == - GNUNET_BIO_read_meta_data (fileR, "Read meta error", - &metaDataR)); + GNUNET_BIO_read_meta_data (fileR, "Read meta error", + &metaDataR)); msg = NULL; GNUNET_BIO_read_close (fileR, &msg); GNUNET_free (msg); diff --git a/src/util/test_client.c b/src/util/test_client.c index bb7d5008a..ad88874f2 100644 --- a/src/util/test_client.c +++ b/src/util/test_client.c @@ -71,24 +71,25 @@ copy_msg (void *cls, size_t size, void *buf) */ static void echo_cb (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { struct CopyContext *cc; struct GNUNET_MessageHeader *cpy; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Receiving message from client, bouncing back\n"); - GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)); + "Receiving message from client, bouncing back\n"); + GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == + ntohs (message->size)); cc = GNUNET_malloc (sizeof (struct CopyContext)); cc->client = client; cpy = GNUNET_malloc (ntohs (message->size)); memcpy (cpy, message, ntohs (message->size)); cc->cpy = cpy; GNUNET_assert (NULL != - GNUNET_SERVER_notify_transmit_ready (client, - ntohs (message->size), - GNUNET_TIME_UNIT_SECONDS, - ©_msg, cc)); + GNUNET_SERVER_notify_transmit_ready (client, + ntohs (message->size), + GNUNET_TIME_UNIT_SECONDS, + ©_msg, cc)); } @@ -104,11 +105,13 @@ recv_bounce (void *cls, const struct GNUNET_MessageHeader *got) int *ok = cls; struct GNUNET_MessageHeader msg; - GNUNET_assert (got != NULL); /* timeout */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n"); + GNUNET_assert (got != NULL); /* timeout */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Receiving bounce, checking content\n"); msg.type = htons (MY_TYPE); msg.size = htons (sizeof (struct GNUNET_MessageHeader)); - GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader))); + GNUNET_assert (0 == + memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader))); GNUNET_CLIENT_disconnect (client, GNUNET_YES); client = NULL; GNUNET_SERVER_destroy (server); @@ -148,9 +151,9 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sa.sin_family = AF_INET; sa.sin_port = htons (PORT); server = - GNUNET_SERVER_create (NULL, NULL, sap, slens, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO); + GNUNET_SERVER_create (NULL, NULL, sap, slens, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO); GNUNET_assert (server != NULL); handlers[0].callback_cls = cls; handlers[1].callback_cls = cls; @@ -158,15 +161,15 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) client = GNUNET_CLIENT_connect (MYNAME, cfg); GNUNET_assert (client != NULL); GNUNET_assert (NULL != - GNUNET_CLIENT_notify_transmit_ready (client, - sizeof (struct - GNUNET_MessageHeader), - GNUNET_TIME_UNIT_SECONDS, - GNUNET_NO, &make_msg, - NULL)); + GNUNET_CLIENT_notify_transmit_ready (client, + sizeof (struct + GNUNET_MessageHeader), + GNUNET_TIME_UNIT_SECONDS, + GNUNET_NO, &make_msg, + NULL)); GNUNET_CLIENT_receive (client, &recv_bounce, cls, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 10000)); + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 10000)); } @@ -181,9 +184,10 @@ check () cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT); - GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost"); + GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", + "localhost"); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); ok = 1; GNUNET_SCHEDULER_run (&task, &ok); GNUNET_CONFIGURATION_destroy (cfg); @@ -197,11 +201,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_client", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check (); return ret; diff --git a/src/util/test_common_allocation.c b/src/util/test_common_allocation.c index 950f0cedb..2fb1329b1 100644 --- a/src/util/test_common_allocation.c +++ b/src/util/test_common_allocation.c @@ -36,21 +36,21 @@ check () unsigned int ui; /* GNUNET_malloc/GNUNET_free test */ - k = 352; /* random start value */ + k = 352; /* random start value */ for (i = 1; i < MAX_TESTVAL; i++) - { - ptrs[i] = GNUNET_malloc (i); - for (j = 0; j < i; j++) - ptrs[i][j] = k++; - } + { + ptrs[i] = GNUNET_malloc (i); + for (j = 0; j < i; j++) + ptrs[i][j] = k++; + } for (i = MAX_TESTVAL - 1; i >= 1; i--) - { - for (j = i - 1; j >= 0; j--) - if (ptrs[i][j] != (char) --k) - return 1; - GNUNET_free (ptrs[i]); - } + { + for (j = i - 1; j >= 0; j--) + if (ptrs[i][j] != (char) --k) + return 1; + GNUNET_free (ptrs[i]); + } /* GNUNET_free_non_null test */ GNUNET_free_non_null (NULL); diff --git a/src/util/test_common_logging.c b/src/util/test_common_logging.c index 39ef58267..6b4922c65 100644 --- a/src/util/test_common_logging.c +++ b/src/util/test_common_logging.c @@ -28,7 +28,7 @@ static void my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component, - const char *date, const char *msg) + const char *date, const char *msg) { unsigned int *c = ctx; @@ -57,23 +57,26 @@ main (int argc, char *argv[]) /* the last 6 calls should be merged (repated bulk messages!) */ GNUNET_logger_remove (&my_log, &logs); if (logs != 4) - { - fprintf (stdout, "Expected 4 log calls, got %u\n", logs); - failureCount++; - } + { + fprintf (stdout, "Expected 4 log calls, got %u\n", logs); + failureCount++; + } GNUNET_break (0 == - strcmp (_("ERROR"), - GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_ERROR))); + strcmp (_("ERROR"), + GNUNET_error_type_to_string + (GNUNET_ERROR_TYPE_ERROR))); GNUNET_break (0 == - strcmp (_("WARNING"), - GNUNET_error_type_to_string - (GNUNET_ERROR_TYPE_WARNING))); + strcmp (_("WARNING"), + GNUNET_error_type_to_string + (GNUNET_ERROR_TYPE_WARNING))); GNUNET_break (0 == - strcmp (_("INFO"), - GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_INFO))); + strcmp (_("INFO"), + GNUNET_error_type_to_string + (GNUNET_ERROR_TYPE_INFO))); GNUNET_break (0 == - strcmp (_("DEBUG"), - GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG))); + strcmp (_("DEBUG"), + GNUNET_error_type_to_string + (GNUNET_ERROR_TYPE_DEBUG))); GNUNET_log_setup ("test_common_logging", "WARNING", "/dev/null"); logs = 0; GNUNET_logger_add (&my_log, &logs); @@ -81,17 +84,17 @@ main (int argc, char *argv[]) GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Drop me...\n"); GNUNET_logger_remove (&my_log, &logs); if (logs != 1) - { - fprintf (stdout, "Expected 1 log call, got %u\n", logs); - failureCount++; - } + { + fprintf (stdout, "Expected 1 log call, got %u\n", logs); + failureCount++; + } if (failureCount != 0) - { - fprintf (stdout, "%u TESTS FAILED!\n", failureCount); - return -1; - } + { + fprintf (stdout, "%u TESTS FAILED!\n", failureCount); + return -1; + } return 0; -} /* end of main */ +} /* end of main */ /* end of test_common_logging.c */ diff --git a/src/util/test_common_logging_dummy.c b/src/util/test_common_logging_dummy.c index 64d164a13..d87f56d42 100644 --- a/src/util/test_common_logging_dummy.c +++ b/src/util/test_common_logging_dummy.c @@ -33,7 +33,7 @@ static void my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component, - const char *date, const char *msg) + const char *date, const char *msg) { if (strncmp ("test-common-logging-dummy", component, 25) != 0) return; @@ -87,6 +87,6 @@ main (int argc, char *argv[]) pr2 (GNUNET_ERROR_TYPE_INFO, "INFO"); pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG"); return 0; -} /* end of main */ +} /* end of main */ /* end of test_common_logging_dummy.c */ diff --git a/src/util/test_common_logging_runtime_loglevels.c b/src/util/test_common_logging_runtime_loglevels.c index 815a3e516..388f74ec1 100644 --- a/src/util/test_common_logging_runtime_loglevels.c +++ b/src/util/test_common_logging_runtime_loglevels.c @@ -47,34 +47,35 @@ static void runone (); static void end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase, ok); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase, + ok); if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); + } GNUNET_OS_process_wait (proc); GNUNET_OS_process_close (proc); proc = NULL; GNUNET_DISK_pipe_close (pipe_stdout); if (ok == 1) - { - if (phase < 9) { - phase += 1; - runone (); + if (phase < 9) + { + phase += 1; + runone (); + } + else + ok = 0; } - else - ok = 0; - } else GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n"); } static char * read_output_line (int phase_from1, int phase_to1, int phase_from2, - int phase_to2, char c, char *expect_level, - long delay_morethan, long delay_lessthan, int phase, char *p, - int *len, long *delay, char level[8]) + int phase_to2, char c, char *expect_level, + long delay_morethan, long delay_lessthan, int phase, + char *p, int *len, long *delay, char level[8]) { char *r = p; char t[7]; @@ -82,67 +83,71 @@ read_output_line (int phase_from1, int phase_to1, int phase_from2, j = 0; int stage = 0; if (!(phase >= phase_from1 && phase <= phase_to1) && - !(phase >= phase_from2 && phase <= phase_to2)) + !(phase >= phase_from2 && phase <= phase_to2)) return p; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p); for (i = 0; i < *len && !stop; i++) - { - switch (stage) { - case 0: /* read first char */ - if (r[i] != c) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c, r[i]); - GNUNET_break (0); - return NULL; - } - stage += 1; - break; - case 1: /* read at most 7 char-long error level string, finished by ' ' */ - if (r[i] == ' ') - { - level[j] = '\0'; - stage += 1; - j = 0; - } - else if (i == 8) - { - GNUNET_break (0); - ok = 2; - return NULL; - } - else - level[j++] = r[i]; - break; - case 2: /* read the delay, finished by '\n' */ - t[j++] = r[i]; + switch (stage) + { + case 0: /* read first char */ + if (r[i] != c) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Expected '%c', but got '%c'\n", c, r[i]); + GNUNET_break (0); + return NULL; + } + stage += 1; + break; + case 1: /* read at most 7 char-long error level string, finished by ' ' */ + if (r[i] == ' ') + { + level[j] = '\0'; + stage += 1; + j = 0; + } + else if (i == 8) + { + GNUNET_break (0); + ok = 2; + return NULL; + } + else + level[j++] = r[i]; + break; + case 2: /* read the delay, finished by '\n' */ + t[j++] = r[i]; #if WINDOWS - if (r[i] == '\r' && r[i + 1] == '\n') - { - i += 1; - t[j - 1] = '\0'; - *delay = strtol (t, NULL, 10); - stop = 1; - } + if (r[i] == '\r' && r[i + 1] == '\n') + { + i += 1; + t[j - 1] = '\0'; + *delay = strtol (t, NULL, 10); + stop = 1; + } #else - if (r[i] == '\n') - { - t[j - 1] = '\0'; - *delay = strtol (t, NULL, 10); - stop = 1; - } + if (r[i] == '\n') + { + t[j - 1] = '\0'; + *delay = strtol (t, NULL, 10); + stop = 1; + } #endif - break; + break; + } } - } - if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 || *delay > 1000 || - (!((*delay < delay_lessthan) || !(*delay > delay_morethan)) && c != '1' && c != '2')) + if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 + || *delay > 1000 + || (!((*delay < delay_lessthan) || !(*delay > delay_morethan)) + && c != '1' && c != '2')) return NULL; *len = *len - i; return &r[i]; } -char buf[20*16]; +char buf[20 * 16]; char *buf_ptr; int bytes; @@ -155,19 +160,20 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) long delays[8]; int rd; - rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes); + rd = + GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes); if (rd > 0) - { - buf_ptr += rd; - bytes += rd; + { + buf_ptr += rd; + bytes += rd; #if VERBOSE - fprintf (stderr, "got %d bytes, reading more\n", rd); + fprintf (stderr, "got %d bytes, reading more\n", rd); #endif - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - stdout_read_handle, &read_call, - (void *) stdout_read_handle); - return; - } + GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, + stdout_read_handle, &read_call, + (void *) stdout_read_handle); + return; + } #if VERBOSE fprintf (stderr, "bytes is %d:%s\n", bytes, buf); @@ -189,32 +195,64 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * | 9 * * * * */ char *p = buf; - if (bytes == 20*16 || - !(p = read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, &bytes, &delays[0], level)) || - !(p = read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p, &bytes, &delays[1], level)) || - !(p = read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, &bytes, &delays[2], level)) || - !(p = read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, &bytes, &delays[3], level)) || - !(p = read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, &bytes, &delays[4], level)) || - !(p = read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p, &bytes, &delays[5], level)) || - !(p = read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, &bytes, &delays[6], level)) || - !(p = read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes, &delay, level)) || - !(p = read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, &bytes, &delays[7], level))) - { - if (bytes == 20*16) - fprintf (stderr, "Ran out of buffer space!\n"); - GNUNET_break (0); - ok = 2; - GNUNET_SCHEDULER_cancel (die_task); - GNUNET_SCHEDULER_add_now (&end_task, NULL); - return; - } + if (bytes == 20 * 16 || + !(p = + read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, + &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, + &bytes, &delays[0], level)) + || !(p = + read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, + &bytes, &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p, + &bytes, &delays[1], level)) + || !(p = + read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, + &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, + &bytes, &delays[2], level)) + || !(p = + read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, + &bytes, &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, + &bytes, &delays[3], level)) + || !(p = + read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, + &bytes, &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, + &bytes, &delays[4], level)) + || !(p = + read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, + &bytes, &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p, + &bytes, &delays[5], level)) + || !(p = + read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, + &bytes, &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, + &bytes, &delays[6], level)) + || !(p = + read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, + &bytes, &delay, level)) + || !(p = + read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, + &bytes, &delays[7], level))) + { + if (bytes == 20 * 16) + fprintf (stderr, "Ran out of buffer space!\n"); + GNUNET_break (0); + ok = 2; + GNUNET_SCHEDULER_cancel (die_task); + GNUNET_SCHEDULER_add_now (&end_task, NULL); + return; + } GNUNET_SCHEDULER_cancel (die_task); GNUNET_SCHEDULER_add_now (&end_task, NULL); @@ -228,52 +266,52 @@ runone () pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); if (pipe_stdout == NULL) - { - GNUNET_break (0); - ok = 2; - return; - } + { + GNUNET_break (0); + ok = 2; + return; + } putenv ("GNUNET_LOG="); putenv ("GNUNET_FORCE_LOG="); switch (phase) - { - case 0: - putenv ("GNUNET_LOG=;;;;ERROR"); - break; - case 1: - putenv ("GNUNET_LOG=;;;;WARNING"); - break; - case 2: - putenv ("GNUNET_LOG=;;;;INFO"); - break; - case 3: - putenv ("GNUNET_LOG=;;;;DEBUG"); - break; - case 4: - putenv ("GNUNET_FORCE_LOG=;;;;ERROR"); - break; - case 5: - putenv ("GNUNET_FORCE_LOG=;;;;WARNING"); - break; - case 6: - putenv ("GNUNET_FORCE_LOG=;;;;INFO"); - break; - case 7: - putenv ("GNUNET_FORCE_LOG=;;;;DEBUG"); - break; - case 8: - putenv ("GNUNET_LOG=blah;;;;ERROR"); - break; - case 9: - putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR"); - break; - } + { + case 0: + putenv ("GNUNET_LOG=;;;;ERROR"); + break; + case 1: + putenv ("GNUNET_LOG=;;;;WARNING"); + break; + case 2: + putenv ("GNUNET_LOG=;;;;INFO"); + break; + case 3: + putenv ("GNUNET_LOG=;;;;DEBUG"); + break; + case 4: + putenv ("GNUNET_FORCE_LOG=;;;;ERROR"); + break; + case 5: + putenv ("GNUNET_FORCE_LOG=;;;;WARNING"); + break; + case 6: + putenv ("GNUNET_FORCE_LOG=;;;;INFO"); + break; + case 7: + putenv ("GNUNET_FORCE_LOG=;;;;DEBUG"); + break; + case 8: + putenv ("GNUNET_LOG=blah;;;;ERROR"); + break; + case 9: + putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR"); + break; + } proc = - GNUNET_OS_start_process (NULL, pipe_stdout, - "./test_common_logging_dummy", - "test_common_logging_dummy", NULL); + GNUNET_OS_start_process (NULL, pipe_stdout, + "./test_common_logging_dummy", + "test_common_logging_dummy", NULL); putenv ("GNUNET_FORCE_LOG="); putenv ("GNUNET_LOG="); @@ -281,20 +319,20 @@ runone () GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); stdout_read_handle = - GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ); + GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ); die_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 10), &end_task, - NULL); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 10), &end_task, + NULL); bytes = 0; buf_ptr = buf; memset (&buf, 0, sizeof (buf)); GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - stdout_read_handle, &read_call, - (void *) stdout_read_handle); + stdout_read_handle, &read_call, + (void *) stdout_read_handle); } static void @@ -324,11 +362,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test-common-logging-runtime-loglevels", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret = check (); return ret; diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c index f02705f86..1c40f06f6 100644 --- a/src/util/test_configuration.c +++ b/src/util/test_configuration.c @@ -42,7 +42,7 @@ enum REMOVE_ENTRY, COMPARE #if DEBUG - , PRINT + , PRINT #endif }; @@ -76,80 +76,80 @@ initDiffsCBData (struct DiffsCBData *cbData) */ static void diffsCallBack (void *cls, const char *section, const char *option, - const char *value) + const char *value) { struct DiffsCBData *cbData = cls; int cbOption = cbData->callBackOption; switch (cbOption) - { - case EDIT_SECTION: - if (NULL == cbData->section) - cbData->section = section; - if (strcmp (cbData->section, section) == 0) { + case EDIT_SECTION: + if (NULL == cbData->section) + cbData->section = section; + if (strcmp (cbData->section, section) == 0) + { + GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option, + "new-value"); + GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, + option, "new-value"); + } + break; + case EDIT_ALL: GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option, - "new-value"); - GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option, - "new-value"); - } - break; - case EDIT_ALL: - GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option, - "new-value"); - GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option, - "new-value"); - break; - case ADD_NEW_ENTRY: - { - static int hit = 0; - - if (hit == 0) - { - hit = 1; - GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key", - "new-value"); + "new-value"); GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, - "new-key", "new-value"); - } - break; - } - case COMPARE: - { - int ret; - char *diffValue; - - diffValue = NULL; - ret = - GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section, - option, &diffValue); - if (NULL != diffValue) - { - if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0) - cbData->status = 1; - } - else - cbData->status = 1; - GNUNET_free_non_null (diffValue); - break; - } + option, "new-value"); + break; + case ADD_NEW_ENTRY: + { + static int hit = 0; + + if (hit == 0) + { + hit = 1; + GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, + "new-key", "new-value"); + GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, + "new-key", "new-value"); + } + break; + } + case COMPARE: + { + int ret; + char *diffValue; + + diffValue = NULL; + ret = + GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section, + option, &diffValue); + if (NULL != diffValue) + { + if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0) + cbData->status = 1; + } + else + cbData->status = 1; + GNUNET_free_non_null (diffValue); + break; + } #if DEBUG - case PRINT: - if (NULL == cbData->section) - { - cbData->section = section; - printf ("\nSection: %s\n", section); - } - else if (strcmp (cbData->section, section) != 0) - { - cbData->section = section; - printf ("\nSection: %s\n", section); - } - printf ("%s = %s\n", option, value); + case PRINT: + if (NULL == cbData->section) + { + cbData->section = section; + printf ("\nSection: %s\n", section); + } + else if (strcmp (cbData->section, section) != 0) + { + cbData->section = section; + printf ("\nSection: %s\n", section); + } + printf ("%s = %s\n", option, value); #endif - default: - break; - } + default: + break; + } } @@ -162,40 +162,41 @@ editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option) diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create (); switch (option) - { - case EDIT_SECTION: - case EDIT_ALL: - case ADD_NEW_ENTRY: - diffsCB.callBackOption = option; - diffsCB.cfg = cfg; - GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB); - break; - case EDIT_NOTHING: - /* Do nothing */ - break; - case ADD_NEW_SECTION: - { - int i; - char *key; - - for (i = 0; i < 5; i++) { - GNUNET_asprintf (&key, "key%d", i); - GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key, - "new-value"); - GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section", - key, "new-value"); - GNUNET_free (key); + case EDIT_SECTION: + case EDIT_ALL: + case ADD_NEW_ENTRY: + diffsCB.callBackOption = option; + diffsCB.cfg = cfg; + GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB); + break; + case EDIT_NOTHING: + /* Do nothing */ + break; + case ADD_NEW_SECTION: + { + int i; + char *key; + + for (i = 0; i < 5; i++) + { + GNUNET_asprintf (&key, "key%d", i); + GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key, + "new-value"); + GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, + "new-section", key, + "new-value"); + GNUNET_free (key); + } + break; + } + case REMOVE_SECTION: + break; + case REMOVE_ENTRY: + break; + default: + break; } - break; - } - case REMOVE_SECTION: - break; - case REMOVE_ENTRY: - break; - default: - break; - } return diffsCB.cfgDiffs; } @@ -220,35 +221,37 @@ checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option) /* Modify configuration and save it */ cfgDiffs = editConfiguration (cfg, option); - diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf"); + diffsFileName = + GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf"); if (diffsFileName == NULL) - { - GNUNET_break (0); - GNUNET_CONFIGURATION_destroy (cfg); - GNUNET_CONFIGURATION_destroy (cfgDiffs); - return 1; - } + { + GNUNET_break (0); + GNUNET_CONFIGURATION_destroy (cfg); + GNUNET_CONFIGURATION_destroy (cfgDiffs); + return 1; + } GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName); GNUNET_CONFIGURATION_destroy (cfg); /* Compare the dumped configuration with modifications done */ cfg = GNUNET_CONFIGURATION_create (); - GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName)); + GNUNET_assert (GNUNET_OK == + GNUNET_CONFIGURATION_parse (cfg, diffsFileName)); remove (diffsFileName); cbData.callBackOption = COMPARE; cbData.cfgDiffs = cfgDiffs; GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData); if (1 == (ret = cbData.status)) - { - fprintf (stderr, - "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n"); - goto housekeeping; - } + { + fprintf (stderr, + "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n"); + goto housekeeping; + } cbData.cfgDiffs = cfg; GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData); if ((ret = cbData.status) == 1) fprintf (stderr, - "Incorrect Configuration Diffs: Data may be missing in diffs\n"); + "Incorrect Configuration Diffs: Data may be missing in diffs\n"); housekeeping: #if DEBUG @@ -273,64 +276,65 @@ testConfig () char *c; unsigned long long l; - if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c)) + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c)) return 1; if (0 != strcmp ("b", c)) - { - fprintf (stderr, "Got `%s'\n", c); - GNUNET_free (c); - return 2; - } + { + fprintf (stderr, "Got `%s'\n", c); + GNUNET_free (c); + return 2; + } GNUNET_free (c); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l)) - { - GNUNET_break (0); - return 3; - } + { + GNUNET_break (0); + return 3; + } if (5 != l) - { - GNUNET_break (0); - return 4; - } + { + GNUNET_break (0); + return 4; + } GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES"); if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c")) - { - GNUNET_break (0); - return 5; - } + { + GNUNET_break (0); + return 5; + } GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c)) - { - GNUNET_break (0); - return 6; - } + { + GNUNET_break (0); + return 6; + } if (0 != strcmp (c, "10")) - { - GNUNET_free (c); - GNUNET_break (0); - return 7; - } + { + GNUNET_free (c); + GNUNET_break (0); + return 7; + } GNUNET_free (c); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c)) - { - GNUNET_break (0); - return 8; - } + { + GNUNET_break (0); + return 8; + } #ifndef MINGW if (0 != strcmp (c, "/hello/world")) #else #define HI "\\hello\\world" if (strstr (c, HI) != c + strlen (c) - strlen (HI)) #endif - { - GNUNET_break (0); - GNUNET_free (c); - return 9; - } + { + GNUNET_break (0); + GNUNET_free (c); + return 9; + } GNUNET_free (c); return 0; @@ -350,10 +354,10 @@ check (void *data, const char *fn) int *idx = data; if (0 == strcmp (want[*idx], fn)) - { - (*idx)++; - return GNUNET_OK; - } + { + (*idx)++; + return GNUNET_OK; + } GNUNET_break (0); return GNUNET_SYSERR; } @@ -366,66 +370,66 @@ testConfigFilenames () idx = 0; if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test", - &check, &idx)) - { - GNUNET_break (0); - return 8; - } + &check, &idx)) + { + GNUNET_break (0); + return 8; + } if (idx != 3) return 16; if (GNUNET_OK != GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", - "/File Name")) - { - GNUNET_break (0); - return 24; - } + "/File Name")) + { + GNUNET_break (0); + return 24; + } if (GNUNET_NO != GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", - "/File Name")) - { - GNUNET_break (0); - return 32; - } + "/File Name")) + { + GNUNET_break (0); + return 32; + } if (GNUNET_NO != GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", - "Stuff")) - { - GNUNET_break (0); - return 40; - } + "Stuff")) + { + GNUNET_break (0); + return 40; + } if (GNUNET_NO != GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", - "/Hello")) - { - GNUNET_break (0); - return 48; - } + "/Hello")) + { + GNUNET_break (0); + return 48; + } if (GNUNET_NO != GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", - "/World")) - { - GNUNET_break (0); - return 56; - } + "/World")) + { + GNUNET_break (0); + return 56; + } if (GNUNET_YES != GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", - "/File 1")) - { - GNUNET_break (0); - return 64; - } + "/File 1")) + { + GNUNET_break (0); + return 64; + } if (GNUNET_YES != GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", - "/File 2")) - { - GNUNET_break (0); - return 72; - } + "/File 2")) + { + GNUNET_break (0); + return 72; + } idx = 0; want[1] = "/World"; @@ -433,16 +437,16 @@ testConfigFilenames () want[3] = "/File 2"; if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test", - &check, &idx)) - { - GNUNET_break (0); - return 80; - } + &check, &idx)) + { + GNUNET_break (0); + return 80; + } if (idx != 4) - { - GNUNET_break (0); - return 88; - } + { + GNUNET_break (0); + return 88; + } return 0; } @@ -458,11 +462,11 @@ main (int argc, char *argv[]) GNUNET_assert (cfg != NULL); if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf")) - { - fprintf (stderr, "Failed to parse configuration file\n"); - GNUNET_CONFIGURATION_destroy (cfg); - return 1; - } + { + fprintf (stderr, "Failed to parse configuration file\n"); + GNUNET_CONFIGURATION_destroy (cfg); + return 1; + } failureCount += testConfig (); if (failureCount > 0) goto error; @@ -472,36 +476,37 @@ main (int argc, char *argv[]) goto error; if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf")) - { - fprintf (stderr, "Failed to write configuration file\n"); - GNUNET_CONFIGURATION_destroy (cfg); - return 1; - } + { + fprintf (stderr, "Failed to write configuration file\n"); + GNUNET_CONFIGURATION_destroy (cfg); + return 1; + } GNUNET_CONFIGURATION_destroy (cfg); GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf")); cfg = GNUNET_CONFIGURATION_create (); if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf")) - { - GNUNET_break (0); - GNUNET_CONFIGURATION_destroy (cfg); - return 1; - } + { + GNUNET_break (0); + GNUNET_CONFIGURATION_destroy (cfg); + return 1; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c)) - { - GNUNET_break (0); - GNUNET_CONFIGURATION_destroy (cfg); - return 1; - } + GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", + &c)) + { + GNUNET_break (0); + GNUNET_CONFIGURATION_destroy (cfg); + return 1; + } if (0 != strcmp (c, "YES")) - { - GNUNET_break (0); - GNUNET_free (c); - GNUNET_CONFIGURATION_destroy (cfg); - return 1; - } + { + GNUNET_break (0); + GNUNET_free (c); + GNUNET_CONFIGURATION_destroy (cfg); + return 1; + } GNUNET_free (c); GNUNET_CONFIGURATION_destroy (cfg); @@ -509,11 +514,11 @@ main (int argc, char *argv[]) /* Testing configuration diffs */ cfgDefault = GNUNET_CONFIGURATION_create (); if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL)) - { - GNUNET_break (0); - GNUNET_CONFIGURATION_destroy (cfgDefault); - return 1; - } + { + GNUNET_break (0); + GNUNET_CONFIGURATION_destroy (cfgDefault); + return 1; + } /* Nothing changed in the new configuration */ failureCount += checkDiffs (cfgDefault, EDIT_NOTHING); @@ -534,9 +539,9 @@ main (int argc, char *argv[]) error: if (failureCount != 0) - { - fprintf (stderr, "Test failed: %u\n", failureCount); - return 1; - } + { + fprintf (stderr, "Test failed: %u\n", failureCount); + return 1; + } return 0; } diff --git a/src/util/test_connection.c b/src/util/test_connection.c index 92c0cea2e..1ac5b99cd 100644 --- a/src/util/test_connection.c +++ b/src/util/test_connection.c @@ -66,44 +66,46 @@ open_listen_socket () GNUNET_assert (desc != NULL); if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "setsockopt"); GNUNET_assert (GNUNET_NETWORK_socket_bind - (desc, (const struct sockaddr *) &sa, - sizeof (sa)) == GNUNET_OK); + (desc, (const struct sockaddr *) &sa, + sizeof (sa)) == GNUNET_OK); GNUNET_NETWORK_socket_listen (desc, 5); return desc; } static void receive_check (void *cls, const void *buf, size_t available, - const struct sockaddr *addr, socklen_t addrlen, int errCode) + const struct sockaddr *addr, socklen_t addrlen, int errCode) { int *ok = cls; #if VERBOSE GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n"); #endif - GNUNET_assert (buf != NULL); /* no timeout */ + GNUNET_assert (buf != NULL); /* no timeout */ if (0 == memcmp (&"Hello World"[sofar], buf, available)) sofar += available; if (sofar < 12) - { + { #if VERBOSE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n"); #endif - GNUNET_CONNECTION_receive (asock, 1024, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, - cls); - } + GNUNET_CONNECTION_receive (asock, 1024, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 5), + &receive_check, cls); + } else - { + { #if VERBOSE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive closes accepted socket\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Receive closes accepted socket\n"); #endif - *ok = 0; - GNUNET_CONNECTION_destroy (asock, GNUNET_YES); - } + *ok = 0; + GNUNET_CONNECTION_destroy (asock, GNUNET_YES); + } } @@ -122,12 +124,12 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); #if VERBOSE GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Test asks to receive on accepted socket\n"); + "Test asks to receive on accepted socket\n"); #endif GNUNET_CONNECTION_receive (asock, 1024, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, - cls); + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, + cls); } static size_t @@ -135,7 +137,7 @@ make_hello (void *cls, size_t size, void *buf) { #if VERBOSE GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Test prepares to transmit on connect socket\n"); + "Test prepares to transmit on connect socket\n"); #endif GNUNET_assert (size >= 12); strcpy ((char *) buf, "Hello World"); @@ -158,14 +160,14 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n"); #endif GNUNET_assert (NULL != - GNUNET_CONNECTION_notify_transmit_ready (csock, 12, - GNUNET_TIME_UNIT_SECONDS, - &make_hello, NULL)); + GNUNET_CONNECTION_notify_transmit_ready (csock, 12, + GNUNET_TIME_UNIT_SECONDS, + &make_hello, NULL)); #if VERBOSE GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n"); #endif - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept, - cls); + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, + &run_accept, cls); } @@ -181,7 +183,7 @@ check () ok = 1; cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); GNUNET_SCHEDULER_run (&task, &ok); GNUNET_CONFIGURATION_destroy (cfg); return ok; @@ -196,11 +198,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_connection", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check (); return ret; } diff --git a/src/util/test_connection_addressing.c b/src/util/test_connection_addressing.c index 018cf61fb..c413c18c8 100644 --- a/src/util/test_connection_addressing.c +++ b/src/util/test_connection_addressing.c @@ -66,15 +66,16 @@ open_listen_socket () GNUNET_assert (desc != 0); if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "setsockopt"); if (GNUNET_OK != GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, - sizeof (sa))) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "bind"); - GNUNET_assert (0); - } + sizeof (sa))) + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "bind"); + GNUNET_assert (0); + } GNUNET_NETWORK_socket_listen (desc, 5); return desc; } @@ -82,25 +83,25 @@ open_listen_socket () static void receive_check (void *cls, const void *buf, size_t available, - const struct sockaddr *addr, socklen_t addrlen, int errCode) + const struct sockaddr *addr, socklen_t addrlen, int errCode) { int *ok = cls; - GNUNET_assert (buf != NULL); /* no timeout */ + GNUNET_assert (buf != NULL); /* no timeout */ if (0 == memcmp (&"Hello World"[sofar], buf, available)) sofar += available; if (sofar < 12) - { - GNUNET_CONNECTION_receive (asock, 1024, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, - cls); - } + { + GNUNET_CONNECTION_receive (asock, 1024, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 5), + &receive_check, cls); + } else - { - *ok = 0; - GNUNET_CONNECTION_destroy (asock, GNUNET_YES); - } + { + *ok = 0; + GNUNET_CONNECTION_destroy (asock, GNUNET_YES); + } } @@ -116,7 +117,7 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_assert (asock != NULL); GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); GNUNET_assert (GNUNET_OK == - GNUNET_CONNECTION_get_address (asock, &addr, &alen)); + GNUNET_CONNECTION_get_address (asock, &addr, &alen)); GNUNET_assert (alen == sizeof (struct sockaddr_in)); v4 = addr; memset (&expect, 0, sizeof (expect)); @@ -130,9 +131,9 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_free (addr); GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); GNUNET_CONNECTION_receive (asock, 1024, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, - cls); + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, + cls); } static size_t @@ -159,17 +160,17 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) v4.sin_port = htons (PORT); v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK); csock = - GNUNET_CONNECTION_create_from_sockaddr (AF_INET, - (const struct sockaddr *) &v4, - sizeof (v4)); + GNUNET_CONNECTION_create_from_sockaddr (AF_INET, + (const struct sockaddr *) &v4, + sizeof (v4)); GNUNET_assert (csock != NULL); GNUNET_assert (NULL != - GNUNET_CONNECTION_notify_transmit_ready (csock, 12, - GNUNET_TIME_UNIT_SECONDS, - &make_hello, NULL)); + GNUNET_CONNECTION_notify_transmit_ready (csock, 12, + GNUNET_TIME_UNIT_SECONDS, + &make_hello, NULL)); GNUNET_CONNECTION_destroy (csock, GNUNET_YES); - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept, - cls); + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, + &run_accept, cls); } @@ -196,11 +197,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_connection_addressing", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check (); return ret; } diff --git a/src/util/test_connection_receive_cancel.c b/src/util/test_connection_receive_cancel.c index 63829344d..bd095fdda 100644 --- a/src/util/test_connection_receive_cancel.c +++ b/src/util/test_connection_receive_cancel.c @@ -65,10 +65,11 @@ open_listen_socket () GNUNET_assert (desc != NULL); if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "setsockopt"); GNUNET_assert (GNUNET_NETWORK_socket_bind - (desc, (const struct sockaddr *) &sa, - sizeof (sa)) == GNUNET_OK); + (desc, (const struct sockaddr *) &sa, + sizeof (sa)) == GNUNET_OK); GNUNET_NETWORK_socket_listen (desc, 5); return desc; } @@ -77,7 +78,7 @@ open_listen_socket () static void dead_receive (void *cls, const void *buf, size_t available, - const struct sockaddr *addr, socklen_t addrlen, int errCode) + const struct sockaddr *addr, socklen_t addrlen, int errCode) { GNUNET_assert (0); } @@ -92,8 +93,9 @@ run_accept_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); GNUNET_CONNECTION_receive (asock, 1024, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_SECONDS, 5), &dead_receive, cls); + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 5), &dead_receive, + cls); } @@ -119,9 +121,9 @@ task_receive_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); GNUNET_assert (csock != NULL); GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, - &run_accept_cancel, cls); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &receive_cancel_task, - cls); + &run_accept_cancel, cls); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, + &receive_cancel_task, cls); } @@ -137,7 +139,7 @@ check_receive_cancel () ok = 1; cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); GNUNET_SCHEDULER_run (&task_receive_cancel, &ok); GNUNET_CONFIGURATION_destroy (cfg); return ok; diff --git a/src/util/test_connection_timeout.c b/src/util/test_connection_timeout.c index 1700718b9..5a0bb7c3a 100644 --- a/src/util/test_connection_timeout.c +++ b/src/util/test_connection_timeout.c @@ -62,10 +62,11 @@ open_listen_socket () GNUNET_assert (desc != NULL); if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "setsockopt"); GNUNET_assert (GNUNET_NETWORK_socket_bind - (desc, (const struct sockaddr *) &sa, - sizeof (sa)) == GNUNET_OK); + (desc, (const struct sockaddr *) &sa, + sizeof (sa)) == GNUNET_OK); GNUNET_NETWORK_socket_listen (desc, 5); return desc; } @@ -77,16 +78,16 @@ send_kilo (void *cls, size_t size, void *buf) int *ok = cls; if (size == 0) - { + { #if VERBOSE - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got the desired timeout!\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got the desired timeout!\n"); #endif - GNUNET_assert (buf == NULL); - *ok = 0; - GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); - GNUNET_CONNECTION_destroy (csock, GNUNET_YES); - return 0; - } + GNUNET_assert (buf == NULL); + *ok = 0; + GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); + GNUNET_CONNECTION_destroy (csock, GNUNET_YES); + return 0; + } #if VERBOSE GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending kilo to fill buffer.\n"); #endif @@ -94,9 +95,9 @@ send_kilo (void *cls, size_t size, void *buf) memset (buf, 42, 1024); GNUNET_assert (NULL != - GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, - GNUNET_TIME_UNIT_SECONDS, - &send_kilo, cls)); + GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, + GNUNET_TIME_UNIT_SECONDS, + &send_kilo, cls)); return 1024; } @@ -111,9 +112,9 @@ task_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); GNUNET_assert (csock != NULL); GNUNET_assert (NULL != - GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, - GNUNET_TIME_UNIT_SECONDS, - &send_kilo, cls)); + GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, + GNUNET_TIME_UNIT_SECONDS, + &send_kilo, cls)); } @@ -129,7 +130,7 @@ check_timeout () ok = 1; cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); GNUNET_SCHEDULER_run (&task_timeout, &ok); GNUNET_CONFIGURATION_destroy (cfg); return ok; @@ -142,11 +143,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_connection_timeout", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check_timeout (); return ret; } diff --git a/src/util/test_connection_timeout_no_connect.c b/src/util/test_connection_timeout_no_connect.c index b598907ec..00b247c52 100644 --- a/src/util/test_connection_timeout_no_connect.c +++ b/src/util/test_connection_timeout_no_connect.c @@ -57,10 +57,10 @@ task_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); GNUNET_assert (csock != NULL); GNUNET_assert (NULL != - GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, - GNUNET_TIME_UNIT_SECONDS, - &handle_timeout, - cls)); + GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, + GNUNET_TIME_UNIT_SECONDS, + &handle_timeout, + cls)); } @@ -76,7 +76,7 @@ check_timeout () ok = 1; cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); GNUNET_SCHEDULER_run (&task_timeout, &ok); GNUNET_CONFIGURATION_destroy (cfg); return ok; @@ -89,11 +89,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_connection_timeout_no_connect", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check_timeout (); return ret; } diff --git a/src/util/test_connection_transmit_cancel.c b/src/util/test_connection_transmit_cancel.c index 50308a2d0..540d8c51d 100644 --- a/src/util/test_connection_transmit_cancel.c +++ b/src/util/test_connection_transmit_cancel.c @@ -43,7 +43,8 @@ not_run (void *cls, size_t size, void *buf) static void -task_transmit_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +task_transmit_cancel (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { int *ok = cls; struct GNUNET_CONNECTION_TransmitHandle *th; @@ -52,8 +53,8 @@ task_transmit_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); GNUNET_assert (csock != NULL); th = GNUNET_CONNECTION_notify_transmit_ready (csock, 12, - GNUNET_TIME_UNIT_MINUTES, - ¬_run, cls); + GNUNET_TIME_UNIT_MINUTES, + ¬_run, cls); GNUNET_assert (NULL != th); GNUNET_CONNECTION_notify_transmit_ready_cancel (th); GNUNET_CONNECTION_destroy (csock, GNUNET_YES); @@ -74,7 +75,7 @@ check_transmit_cancel () ok = 1; cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); GNUNET_SCHEDULER_run (&task_transmit_cancel, &ok); GNUNET_CONFIGURATION_destroy (cfg); return ok; @@ -88,11 +89,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_connection_transmit_cancel", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check_transmit_cancel (); return ret; diff --git a/src/util/test_container_bloomfilter.c b/src/util/test_container_bloomfilter.c index c14adb075..04bb34889 100644 --- a/src/util/test_container_bloomfilter.c +++ b/src/util/test_container_bloomfilter.c @@ -75,29 +75,30 @@ main (int argc, char *argv[]) bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K); for (i = 0; i < 200; i++) - { - nextHC (&tmp); - GNUNET_CONTAINER_bloomfilter_add (bf, &tmp); - } + { + nextHC (&tmp); + GNUNET_CONTAINER_bloomfilter_add (bf, &tmp); + } SRANDOM (1); ok1 = 0; for (i = 0; i < 200; i++) - { - nextHC (&tmp); - if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) - ok1++; - } + { + nextHC (&tmp); + if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) + ok1++; + } if (ok1 != 200) - { - printf ("Got %d elements out of" "200 expected after insertion.\n", ok1); - GNUNET_CONTAINER_bloomfilter_free (bf); - return -1; - } + { + printf ("Got %d elements out of" "200 expected after insertion.\n", + ok1); + GNUNET_CONTAINER_bloomfilter_free (bf); + return -1; + } if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, buf, SIZE)) - { - GNUNET_CONTAINER_bloomfilter_free (bf); - return -1; - } + { + GNUNET_CONTAINER_bloomfilter_free (bf); + return -1; + } GNUNET_CONTAINER_bloomfilter_free (bf); @@ -110,93 +111,94 @@ main (int argc, char *argv[]) ok1 = 0; ok2 = 0; for (i = 0; i < 200; i++) - { - nextHC (&tmp); - if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) - ok1++; - if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) - ok2++; - } + { + nextHC (&tmp); + if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) + ok1++; + if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) + ok2++; + } if (ok1 != 200) - { - printf ("Got %d elements out of 200 " "expected after reloading.\n", ok1); - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + printf ("Got %d elements out of 200 " "expected after reloading.\n", + ok1); + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } if (ok2 != 200) - { - printf ("Got %d elements out of 200 " "expected after initialization.\n", - ok2); - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + printf ("Got %d elements out of 200 " + "expected after initialization.\n", ok2); + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } SRANDOM (1); for (i = 0; i < 100; i++) - { - nextHC (&tmp); - GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp); - GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp); - } + { + nextHC (&tmp); + GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp); + GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp); + } SRANDOM (1); ok1 = 0; ok2 = 0; for (i = 0; i < 200; i++) - { - nextHC (&tmp); - if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) - ok1++; - if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) - ok2++; - } + { + nextHC (&tmp); + if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) + ok1++; + if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) + ok2++; + } if (ok1 != 100) - { - printf ("Expected 100 elements in loaded filter" - " after adding 200 and deleting 100, got %d\n", ok1); - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + printf ("Expected 100 elements in loaded filter" + " after adding 200 and deleting 100, got %d\n", ok1); + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } if (ok2 != 200) - { - printf ("Expected 200 elements in initialized filter" - " after adding 200 and deleting 100 " - "(which should do nothing for a filter not backed by a file), got %d\n", - ok2); - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + printf ("Expected 200 elements in initialized filter" + " after adding 200 and deleting 100 " + "(which should do nothing for a filter not backed by a file), got %d\n", + ok2); + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } SRANDOM (3); GNUNET_CONTAINER_bloomfilter_clear (bf); falseok = 0; for (i = 0; i < 1000; i++) - { - nextHC (&tmp); - if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) - falseok++; - } + { + nextHC (&tmp); + if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) + falseok++; + } if (falseok > 0) - { - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or (bf, buf, SIZE)) - { - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } SRANDOM (2); i = 20; @@ -210,30 +212,30 @@ main (int argc, char *argv[]) ok1 = 0; ok2 = 0; for (i = 0; i < 20; i++) - { - nextHC (&tmp); - if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) - ok1++; - if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) - ok2++; - } + { + nextHC (&tmp); + if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) + ok1++; + if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) + ok2++; + } if (ok1 != 20) - { - printf ("Expected 20 elements in resized file-backed filter" - " after adding 20, got %d\n", ok1); - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + printf ("Expected 20 elements in resized file-backed filter" + " after adding 20, got %d\n", ok1); + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } if (ok2 != 20) - { - printf ("Expected 20 elements in resized filter" - " after adding 20, got %d\n", ok2); - GNUNET_CONTAINER_bloomfilter_free (bf); - GNUNET_CONTAINER_bloomfilter_free (bfi); - return -1; - } + { + printf ("Expected 20 elements in resized filter" + " after adding 20, got %d\n", ok2); + GNUNET_CONTAINER_bloomfilter_free (bf); + GNUNET_CONTAINER_bloomfilter_free (bfi); + return -1; + } GNUNET_CONTAINER_bloomfilter_free (bf); diff --git a/src/util/test_container_heap.c b/src/util/test_container_heap.c index a2a004aa7..c1c4eb360 100644 --- a/src/util/test_container_heap.c +++ b/src/util/test_container_heap.c @@ -30,7 +30,7 @@ static int iterator_callback (void *cls, struct GNUNET_CONTAINER_HeapNode *node, - void *element, GNUNET_CONTAINER_HeapCostType cost) + void *element, GNUNET_CONTAINER_HeapCostType cost) { return GNUNET_OK; } @@ -107,15 +107,15 @@ check () n6 = GNUNET_CONTAINER_heap_insert (myHeap, "30/200", 30); GNUNET_assert (5 == GNUNET_CONTAINER_heap_get_size (myHeap)); GNUNET_CONTAINER_heap_remove_node (n5); - r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n1 */ + r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n1 */ GNUNET_assert (NULL != r); GNUNET_assert (0 == strcmp ("11", r)); GNUNET_CONTAINER_heap_update_cost (myHeap, n6, 200); GNUNET_CONTAINER_heap_remove_node (n3); - r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n4 */ + r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n4 */ GNUNET_assert (NULL != r); GNUNET_assert (0 == strcmp ("50", r)); - r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n6 */ + r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n6 */ GNUNET_assert (NULL != r); GNUNET_assert (0 == strcmp ("30/200", r)); GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (myHeap)); @@ -172,9 +172,9 @@ check () GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2))); GNUNET_assert (0 == - nstrcmp ("10", GNUNET_CONTAINER_heap_remove_root (myHeap))); + nstrcmp ("10", GNUNET_CONTAINER_heap_remove_root (myHeap))); GNUNET_assert (0 == - nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap))); + nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap))); n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10); n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20); @@ -232,7 +232,7 @@ check () GNUNET_CONTAINER_heap_remove_node (n2); GNUNET_CONTAINER_heap_remove_node (n1); GNUNET_assert (0 == - nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap))); + nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap))); n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10); n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10); diff --git a/src/util/test_container_meta_data.c b/src/util/test_container_meta_data.c index fe1dd79c1..66f90ff10 100644 --- a/src/util/test_container_meta_data.c +++ b/src/util/test_container_meta_data.c @@ -41,51 +41,55 @@ testMeta (int i) m = GNUNET_CONTAINER_meta_data_create (); if (GNUNET_OK != - GNUNET_CONTAINER_meta_data_insert (m, "", EXTRACTOR_METATYPE_TITLE, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", "TestTitle", - strlen ("TestTitle") + 1)) + GNUNET_CONTAINER_meta_data_insert (m, "", + EXTRACTOR_METATYPE_TITLE, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", "TestTitle", + strlen ("TestTitle") + 1)) ABORT (m); if (GNUNET_OK != GNUNET_CONTAINER_meta_data_insert (m, "", - EXTRACTOR_METATYPE_AUTHOR_NAME, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", "TestTitle", - strlen ("TestTitle") + 1)) + EXTRACTOR_METATYPE_AUTHOR_NAME, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", "TestTitle", + strlen ("TestTitle") + 1)) ABORT (m); - if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ + if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ ABORT (m); - if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "", EXTRACTOR_METATYPE_AUTHOR_NAME, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ + if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "", EXTRACTOR_METATYPE_AUTHOR_NAME, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ ABORT (m); if (2 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) ABORT (m); if (GNUNET_OK != GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, - "TestTitle", strlen ("TestTitle") + 1)) + "TestTitle", + strlen ("TestTitle") + 1)) ABORT (m); - if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ + if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ ABORT (m); if (1 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) ABORT (m); if (GNUNET_OK != GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, - "TestTitle", strlen ("TestTitle") + 1)) + "TestTitle", + strlen ("TestTitle") + 1)) ABORT (m); - if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ + if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ ABORT (m); if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) ABORT (m); for (j = 0; j < i; j++) - { - GNUNET_snprintf (val, sizeof (val), "%s.%d", - "A teststring that should compress well.", j); - if (GNUNET_OK != - GNUNET_CONTAINER_meta_data_insert (m, "", - EXTRACTOR_METATYPE_UNKNOWN, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", val, strlen (val) + 1)) - ABORT (m); - } + { + GNUNET_snprintf (val, sizeof (val), "%s.%d", + "A teststring that should compress well.", j); + if (GNUNET_OK != + GNUNET_CONTAINER_meta_data_insert (m, "", + EXTRACTOR_METATYPE_UNKNOWN, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", val, + strlen (val) + 1)) + ABORT (m); + } if (i != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) ABORT (m); @@ -93,27 +97,27 @@ testMeta (int i) sval = NULL; if (size != GNUNET_CONTAINER_meta_data_serialize (m, &sval, size, - GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) - { - GNUNET_free_non_null (sval); - ABORT (m); - } + GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) + { + GNUNET_free_non_null (sval); + ABORT (m); + } GNUNET_CONTAINER_meta_data_destroy (m); m = GNUNET_CONTAINER_meta_data_deserialize (sval, size); GNUNET_free (sval); if (m == NULL) ABORT (m); for (j = 0; j < i; j++) - { - GNUNET_snprintf (val, sizeof (val), "%s.%d", - "A teststring that should compress well.", j); - if (GNUNET_OK != - GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_UNKNOWN, val, - strlen (val) + 1)) { - ABORT (m); + GNUNET_snprintf (val, sizeof (val), "%s.%d", + "A teststring that should compress well.", j); + if (GNUNET_OK != + GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_UNKNOWN, + val, strlen (val) + 1)) + { + ABORT (m); + } } - } if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) ABORT (m); GNUNET_CONTAINER_meta_data_destroy (m); @@ -131,22 +135,22 @@ testMetaMore (int i) meta = GNUNET_CONTAINER_meta_data_create (); for (q = 0; q <= i; q++) - { - GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); - GNUNET_CONTAINER_meta_data_insert (meta, "", - q % EXTRACTOR_metatype_get_max (), - EXTRACTOR_METAFORMAT_UTF8, "text/plain", - txt, strlen (txt) + 1); - } + { + GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); + GNUNET_CONTAINER_meta_data_insert (meta, "", + q % EXTRACTOR_metatype_get_max (), + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", txt, strlen (txt) + 1); + } size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta); data = GNUNET_malloc (size * 4); if (size != GNUNET_CONTAINER_meta_data_serialize (meta, &data, size * 4, - GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) - { - GNUNET_free (data); - ABORT (meta); - } + GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) + { + GNUNET_free (data); + ABORT (meta); + } GNUNET_CONTAINER_meta_data_destroy (meta); GNUNET_free (data); return 0; @@ -162,22 +166,22 @@ testMetaLink () m = GNUNET_CONTAINER_meta_data_create (); if (GNUNET_OK != GNUNET_CONTAINER_meta_data_insert (m, "", - EXTRACTOR_METATYPE_UNKNOWN, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", "link", - strlen ("link") + 1)) + EXTRACTOR_METATYPE_UNKNOWN, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", "link", + strlen ("link") + 1)) ABORT (m); if (GNUNET_OK != GNUNET_CONTAINER_meta_data_insert (m, "", - EXTRACTOR_METATYPE_FILENAME, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", "lib-link.m4", - strlen ("lib-link.m4") + 1)) + EXTRACTOR_METATYPE_FILENAME, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", "lib-link.m4", + strlen ("lib-link.m4") + 1)) ABORT (m); val = NULL; size = - GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) - 1, - GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); + GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) - 1, + GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); GNUNET_CONTAINER_meta_data_destroy (m); m = GNUNET_CONTAINER_meta_data_deserialize (val, size); GNUNET_free (val); @@ -201,44 +205,46 @@ check () meta = GNUNET_CONTAINER_meta_data_create (); meta2 = GNUNET_CONTAINER_meta_data_create (); for (q = 0; q <= i; q++) - { - GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); - GNUNET_CONTAINER_meta_data_insert (meta, "", - EXTRACTOR_METATYPE_UNKNOWN, - EXTRACTOR_METAFORMAT_UTF8, "text/plain", - "TestTitle", strlen ("TestTitle") + 1); - GNUNET_CONTAINER_meta_data_insert (meta2, "", - EXTRACTOR_METATYPE_UNKNOWN, - EXTRACTOR_METAFORMAT_UTF8, "text/plain", - "TestTitle", strlen ("TestTitle") + 1); - } + { + GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); + GNUNET_CONTAINER_meta_data_insert (meta, "", + EXTRACTOR_METATYPE_UNKNOWN, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", "TestTitle", + strlen ("TestTitle") + 1); + GNUNET_CONTAINER_meta_data_insert (meta2, "", + EXTRACTOR_METATYPE_UNKNOWN, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", "TestTitle", + strlen ("TestTitle") + 1); + } //check meta_data_test_equal if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2)) - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } //check meta_data_clear GNUNET_CONTAINER_meta_data_clear (meta2); if (0 != GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } // check equal branch in meta_data_test_equal if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta)) - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } // check "count" branch in meta_data_test_equal if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2)) - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } // check meta_data_add_publication_date GNUNET_CONTAINER_meta_data_add_publication_date (meta2); @@ -247,74 +253,74 @@ check () GNUNET_CONTAINER_meta_data_clear (meta2); GNUNET_CONTAINER_meta_data_merge (meta2, meta); if (100 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } // check meta_data_get_by_type GNUNET_CONTAINER_meta_data_clear (meta2); if (NULL != (str = GNUNET_CONTAINER_meta_data_get_by_type (meta2, - EXTRACTOR_METATYPE_UNKNOWN))) - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - GNUNET_free (str); - ABORT (meta); - } + EXTRACTOR_METATYPE_UNKNOWN))) + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + GNUNET_free (str); + ABORT (meta); + } str = - GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN); + GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN); GNUNET_assert (NULL != str); if (str[0] != 'T') - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - GNUNET_free (str); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + GNUNET_free (str); + ABORT (meta); + } GNUNET_free (str); // check branch if (NULL != (str = GNUNET_CONTAINER_meta_data_get_by_type (meta, - EXTRACTOR_METATYPE_PUBLICATION_DATE))) - { - GNUNET_free (str); - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + EXTRACTOR_METATYPE_PUBLICATION_DATE))) + { + GNUNET_free (str); + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } //check meta_data_get_first_by_types str = - GNUNET_CONTAINER_meta_data_get_first_by_types (meta, - EXTRACTOR_METATYPE_UNKNOWN, - -1); + GNUNET_CONTAINER_meta_data_get_first_by_types (meta, + EXTRACTOR_METATYPE_UNKNOWN, + -1); GNUNET_assert (NULL != str); if (str[0] != 'T') - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - GNUNET_free (str); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + GNUNET_free (str); + ABORT (meta); + } GNUNET_free (str); //check meta_data_get_thumbnail if (GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb) != 0) - { - GNUNET_free (thumb); - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + { + GNUNET_free (thumb); + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } GNUNET_CONTAINER_meta_data_destroy (meta2); //check meta_data_duplicate meta2 = GNUNET_CONTAINER_meta_data_duplicate (meta); if (200 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) - { - GNUNET_CONTAINER_meta_data_destroy (meta2); - ABORT (meta); - } + { + GNUNET_CONTAINER_meta_data_destroy (meta2); + ABORT (meta); + } GNUNET_CONTAINER_meta_data_destroy (meta2); GNUNET_CONTAINER_meta_data_destroy (meta); return 0; diff --git a/src/util/test_container_multihashmap.c b/src/util/test_container_multihashmap.c index ba621c17e..5732ace93 100644 --- a/src/util/test_container_multihashmap.c +++ b/src/util/test_container_multihashmap.c @@ -52,38 +52,41 @@ testMap (int i) CHECK (0 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1)); CHECK (0 == GNUNET_CONTAINER_multihashmap_size (m)); CHECK (0 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL)); - CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL)); + CHECK (0 == + GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL)); CHECK (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", - GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); + GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", + GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m)); ret = GNUNET_CONTAINER_multihashmap_get (m, &k1); GNUNET_assert (ret != NULL); CHECK (0 == strcmp ("v1", ret)); CHECK (GNUNET_NO == - GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", - GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); + GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", + GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m)); CHECK (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); CHECK (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3", - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3", + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); CHECK (3 == GNUNET_CONTAINER_multihashmap_size (m)); CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (m, &k1, "v3")); CHECK (2 == GNUNET_CONTAINER_multihashmap_size (m)); CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (m, &k1)); CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2)); - CHECK (2 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL)); - CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL)); + CHECK (2 == + GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL)); + CHECK (0 == + GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL)); CHECK (2 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL)); CHECK (2 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1)); for (j = 0; j < 1024; j++) CHECK (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); GNUNET_CONTAINER_multihashmap_destroy (m); return 0; } diff --git a/src/util/test_container_slist.c b/src/util/test_container_slist.c index 4a277dab5..307a6b248 100644 --- a/src/util/test_container_slist.c +++ b/src/util/test_container_slist.c @@ -46,26 +46,27 @@ main (int argc, char *argv[]) GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0); for (i = 0; i < 100; i++) - GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - &i, sizeof (i)); + GNUNET_CONTAINER_slist_add (l, + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + &i, sizeof (i)); GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); for (it = GNUNET_CONTAINER_slist_begin (l), i = 99; GNUNET_CONTAINER_slist_end (it) != GNUNET_YES; GNUNET_CONTAINER_slist_next (it), i--) - { - p = GNUNET_CONTAINER_slist_get (it, &s); - - if ((p == NULL) || (i != (j = *(int *) p)) || (s != sizeof (i))) { - GNUNET_CONTAINER_slist_iter_destroy (it); - GNUNET_assert (0); + p = GNUNET_CONTAINER_slist_get (it, &s); + + if ((p == NULL) || (i != (j = *(int *) p)) || (s != sizeof (i))) + { + GNUNET_CONTAINER_slist_iter_destroy (it); + GNUNET_assert (0); + } + j *= 2; + GNUNET_CONTAINER_slist_insert (it, + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + &j, sizeof (j)); } - j *= 2; - GNUNET_CONTAINER_slist_insert (it, - GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - &j, sizeof (j)); - } GNUNET_CONTAINER_slist_iter_destroy (it); GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200); i = 198; @@ -73,39 +74,40 @@ main (int argc, char *argv[]) for (it = GNUNET_CONTAINER_slist_begin (l); GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;) - { - p = GNUNET_CONTAINER_slist_get (it, &s); - GNUNET_assert (p != NULL); - GNUNET_assert (s == sizeof (i)); - i = *(int *) p; + { + p = GNUNET_CONTAINER_slist_get (it, &s); + GNUNET_assert (p != NULL); + GNUNET_assert (s == sizeof (i)); + i = *(int *) p; - GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES); - GNUNET_assert (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES); + GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES); + GNUNET_assert (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES); - p = GNUNET_CONTAINER_slist_get (it, &s); - GNUNET_assert (p != NULL); - GNUNET_assert (s == sizeof (j)); - j = *(int *) p; + p = GNUNET_CONTAINER_slist_get (it, &s); + GNUNET_assert (p != NULL); + GNUNET_assert (s == sizeof (j)); + j = *(int *) p; - GNUNET_assert (j * 2 == i); + GNUNET_assert (j * 2 == i); - GNUNET_CONTAINER_slist_erase (it); - } + GNUNET_CONTAINER_slist_erase (it); + } GNUNET_CONTAINER_slist_iter_destroy (it); GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); i = 99; GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == - GNUNET_NO); + GNUNET_NO); i = 198; GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == - GNUNET_YES); + GNUNET_YES); GNUNET_CONTAINER_slist_clear (l); GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0); for (i = 0; i < 100; i++) - GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - &i, sizeof (i)); + GNUNET_CONTAINER_slist_add (l, + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + &i, sizeof (i)); /*check slist_append */ GNUNET_CONTAINER_slist_append (l, l); GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200); @@ -116,23 +118,23 @@ main (int argc, char *argv[]) l = GNUNET_CONTAINER_slist_create (); for (i = 0; i < 100; i++) GNUNET_CONTAINER_slist_add_end (l, - GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - &i, sizeof (i)); + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + &i, sizeof (i)); GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); for (it = GNUNET_CONTAINER_slist_begin (l), i = 0; GNUNET_CONTAINER_slist_end (it) != GNUNET_YES; GNUNET_CONTAINER_slist_next (it), i++) - { - p = GNUNET_CONTAINER_slist_get (it, &s); - - if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i))) { - GNUNET_CONTAINER_slist_iter_destroy (it); - GNUNET_assert (0); + p = GNUNET_CONTAINER_slist_get (it, &s); + + if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i))) + { + GNUNET_CONTAINER_slist_iter_destroy (it); + GNUNET_assert (0); + } } - } GNUNET_CONTAINER_slist_iter_destroy (it); GNUNET_CONTAINER_slist_destroy (l); @@ -140,12 +142,13 @@ main (int argc, char *argv[]) l = GNUNET_CONTAINER_slist_create (); for (i = 0; i < 100; i++) - { - ip = GNUNET_malloc (sizeof (int)); - *ip = i; - GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC, - ip, sizeof (int)); - } + { + ip = GNUNET_malloc (sizeof (int)); + *ip = i; + GNUNET_CONTAINER_slist_add (l, + GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC, + ip, sizeof (int)); + } //creat_add it = GNUNET_CONTAINER_slist_begin (l); p = GNUNET_CONTAINER_slist_get (it, &s); diff --git a/src/util/test_crypto_aes.c b/src/util/test_crypto_aes.c index 971e9afbe..e928b96a7 100644 --- a/src/util/test_crypto_aes.c +++ b/src/util/test_crypto_aes.c @@ -40,30 +40,30 @@ testSymcipher () GNUNET_CRYPTO_aes_create_session_key (&key); size = - GNUNET_CRYPTO_aes_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key, - (const struct - GNUNET_CRYPTO_AesInitializationVector *) - INITVALUE, result); + GNUNET_CRYPTO_aes_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key, + (const struct + GNUNET_CRYPTO_AesInitializationVector *) + INITVALUE, result); if (size == -1) - { - printf ("symciphertest failed: encryptBlock returned %d\n", size); - return 1; - } + { + printf ("symciphertest failed: encryptBlock returned %d\n", size); + return 1; + } size = - GNUNET_CRYPTO_aes_decrypt (result, size, &key, - (const struct - GNUNET_CRYPTO_AesInitializationVector *) - INITVALUE, res); + GNUNET_CRYPTO_aes_decrypt (result, size, &key, + (const struct + GNUNET_CRYPTO_AesInitializationVector *) + INITVALUE, res); if (strlen (TESTSTRING) + 1 != size) - { - printf ("symciphertest failed: decryptBlock returned %d\n", size); - return 1; - } + { + printf ("symciphertest failed: decryptBlock returned %d\n", size); + return 1; + } if (0 != strcmp (res, TESTSTRING)) - { - printf ("symciphertest failed: %s != %s\n", res, TESTSTRING); - return 1; - } + { + printf ("symciphertest failed: %s != %s\n", res, TESTSTRING); + return 1; + } else return 0; } @@ -77,16 +77,16 @@ verifyCrypto () int ret; unsigned char plain[] = - { 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249, + { 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249, 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164 }; unsigned char raw_key[] = - { 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, + { 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, 34, 191 }; unsigned char encrresult[] = - { 167, 102, 230, 233, 127, 195, 176, 107, 17, 91, 199, 127, 96, 113, 75, + { 167, 102, 230, 233, 127, 195, 176, 107, 17, 91, 199, 127, 96, 113, 75, 195, 245, 217, 61, 236, 159, 165, 103, 121, 203, 99, 202, 41, 23, 222, 25, 102 }; @@ -96,54 +96,54 @@ verifyCrypto () memcpy (key.key, raw_key, GNUNET_CRYPTO_AES_KEY_LENGTH); key.crc32 = - htonl (GNUNET_CRYPTO_crc32_n (&key, GNUNET_CRYPTO_AES_KEY_LENGTH)); + htonl (GNUNET_CRYPTO_crc32_n (&key, GNUNET_CRYPTO_AES_KEY_LENGTH)); if (ntohl (key.crc32) != (unsigned int) 38125195LL) - { - printf ("Static key has different CRC: %u - %u\n", ntohl (key.crc32), - key.crc32); + { + printf ("Static key has different CRC: %u - %u\n", ntohl (key.crc32), + key.crc32); - ret = 1; - goto error; - } + ret = 1; + goto error; + } if (GNUNET_CRYPTO_AES_KEY_LENGTH != GNUNET_CRYPTO_aes_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, - (const struct - GNUNET_CRYPTO_AesInitializationVector *) - "testtesttesttest", result)) - { - printf ("Wrong return value from encrypt block.\n"); - ret = 1; - goto error; - } + (const struct + GNUNET_CRYPTO_AesInitializationVector *) + "testtesttesttest", result)) + { + printf ("Wrong return value from encrypt block.\n"); + ret = 1; + goto error; + } if (memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0) - { - printf ("Encrypted result wrong.\n"); - ret = 1; - goto error; - } + { + printf ("Encrypted result wrong.\n"); + ret = 1; + goto error; + } res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH); if (GNUNET_CRYPTO_AES_KEY_LENGTH != GNUNET_CRYPTO_aes_decrypt (result, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, - (const struct - GNUNET_CRYPTO_AesInitializationVector *) - "testtesttesttest", res)) - { - printf ("Wrong return value from decrypt block.\n"); - ret = 1; - goto error; - } + (const struct + GNUNET_CRYPTO_AesInitializationVector *) + "testtesttesttest", res)) + { + printf ("Wrong return value from decrypt block.\n"); + ret = 1; + goto error; + } if (memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0) - { - printf ("Decrypted result does not match input.\n"); + { + printf ("Decrypted result does not match input.\n"); - ret = 1; - } + ret = 1; + } error: @@ -160,15 +160,15 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL); GNUNET_CRYPTO_random_disable_entropy_gathering (); GNUNET_assert (strlen (INITVALUE) > - sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); + sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); failureCount += testSymcipher (); failureCount += verifyCrypto (); if (failureCount != 0) - { - printf ("%d TESTS FAILED!\n", failureCount); - return -1; - } + { + printf ("%d TESTS FAILED!\n", failureCount); + return -1; + } return 0; } diff --git a/src/util/test_crypto_aes_weak.c b/src/util/test_crypto_aes_weak.c index a0161b575..25931b625 100644 --- a/src/util/test_crypto_aes_weak.c +++ b/src/util/test_crypto_aes_weak.c @@ -40,9 +40,9 @@ printWeakKey (struct GNUNET_CRYPTO_AesSessionKey *key) int i; for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) - { - printf ("%x ", (int) (key->key[i])); - } + { + printf ("%x ", (int) (key->key[i])); + } } static int @@ -55,7 +55,7 @@ testWeakKey () struct GNUNET_CRYPTO_AesInitializationVector INITVALUE; memset (&INITVALUE, 42, - sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); + sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); /* sorry, this is not a weak key -- I don't have * any at the moment! */ weak_key.key[0] = (char) (0x4c); @@ -92,31 +92,31 @@ testWeakKey () weak_key.key[31] = (char) (0xaa); /* memset(&weak_key, 0, 32); */ weak_key.crc32 = - htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH)); + htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH)); size = - GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING, - strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key, - &INITVALUE, result); + GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING, + strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key, + &INITVALUE, result); if (size == -1) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res); if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } if (0 != strcmp (res, WEAK_KEY_TESTSTRING)) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } else return 0; } @@ -133,42 +133,45 @@ getWeakKeys () for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS; number_of_runs++) - { - - if (number_of_runs % 1000 == 0) - fprintf (stderr, "."); - /*printf("Got to run number %d.\n", number_of_runs); */ - GNUNET_CRYPTO_aes_create_session_key (&sessionkey); - - rc = gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, - 0); - - if (rc) { - printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n", - number_of_runs, gcry_strerror (rc)); - continue; - } - rc = gcry_cipher_setkey (handle, &sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); + if (number_of_runs % 1000 == 0) + fprintf (stderr, "."); + /*printf("Got to run number %d.\n", number_of_runs); */ + GNUNET_CRYPTO_aes_create_session_key (&sessionkey); + + rc = + gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, + 0); + + if (rc) + { + printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n", + number_of_runs, gcry_strerror (rc)); + continue; + } + + rc = + gcry_cipher_setkey (handle, &sessionkey, + GNUNET_CRYPTO_AES_KEY_LENGTH); + + if ((char) rc == GPG_ERR_WEAK_KEY) + { + printf ("\nWeak key (in hex): "); + printWeakKey (&sessionkey); + printf ("\n"); + number_of_weak_keys++; + } + else if (rc) + { + printf ("\nUnexpected error generating keys. Error is %s\n", + gcry_strerror (rc)); + } + + gcry_cipher_close (handle); - if ((char) rc == GPG_ERR_WEAK_KEY) - { - printf ("\nWeak key (in hex): "); - printWeakKey (&sessionkey); - printf ("\n"); - number_of_weak_keys++; - } - else if (rc) - { - printf ("\nUnexpected error generating keys. Error is %s\n", - gcry_strerror (rc)); } - gcry_cipher_close (handle); - - } - return number_of_weak_keys; } @@ -180,19 +183,19 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL); GNUNET_CRYPTO_random_disable_entropy_gathering (); if (GENERATE_WEAK_KEYS) - { - weak_keys = getWeakKeys (); - - if (weak_keys == 0) - { - printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS); - } - else { - printf ("\n%d weak keys found in %d runs.\n", weak_keys, - MAX_WEAK_KEY_TRIALS); + weak_keys = getWeakKeys (); + + if (weak_keys == 0) + { + printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS); + } + else + { + printf ("\n%d weak keys found in %d runs.\n", weak_keys, + MAX_WEAK_KEY_TRIALS); + } } - } if (testWeakKey () != 0) return -1; diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c index b0fa4f332..a32a28ef0 100644 --- a/src/util/test_crypto_hash.c +++ b/src/util/test_crypto_hash.c @@ -42,10 +42,10 @@ test (int number) memset (&h1, number, sizeof (GNUNET_HashCode)); GNUNET_CRYPTO_hash_to_enc (&h1, &enc); if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2)) - { - printf ("enc2hash failed!\n"); - return 1; - } + { + printf ("enc2hash failed!\n"); + return 1; + } if (0 != memcmp (&h1, &h2, sizeof (GNUNET_HashCode))) return 1; return 0; @@ -124,8 +124,9 @@ static void file_hasher (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { GNUNET_assert (NULL != - GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT, - FILENAME, 1024, &finished_task, cls)); + GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT, + FILENAME, 1024, &finished_task, + cls)); } diff --git a/src/util/test_crypto_hkdf.c b/src/util/test_crypto_hkdf.c index 752116194..d5ee4d433 100644 --- a/src/util/test_crypto_hkdf.c +++ b/src/util/test_crypto_hkdf.c @@ -36,17 +36,17 @@ void tc1 () { unsigned char ikm[22] = - { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; unsigned char salt[13] = - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c }; unsigned char info[10] = - { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 }; + { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 }; unsigned char okm[42] = - { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, + { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65 @@ -56,9 +56,9 @@ tc1 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, - sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), - NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, + sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), + NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } @@ -67,7 +67,7 @@ void tc2 () { unsigned char ikm[80] = - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, @@ -76,7 +76,7 @@ tc2 () 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }; unsigned char salt[80] = - { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, @@ -85,7 +85,7 @@ tc2 () 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf }; unsigned char info[80] = - { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, + { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, @@ -94,7 +94,7 @@ tc2 () 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff }; unsigned char okm[82] = - { 0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7, + { 0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7, 0xf7, 0x8c, 0x59, 0x6a, 0x49, 0x34, 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e, 0xfa, 0xd8, 0xa0, 0x50, 0xcc, 0x4c, 0x19, 0xaf, 0xa9, 0x7c, 0x59, 0x04, 0x5a, 0x99, 0xca, 0xc7, 0x82, 0x72, 0x71, 0xcb, 0x41, 0xc6, 0x5e, 0x59, @@ -107,9 +107,9 @@ tc2 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, - sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), - NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, + sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), + NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } @@ -118,11 +118,11 @@ void tc3 () { unsigned char ikm[22] = - { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; unsigned char okm[42] = - { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f, 0x71, 0x5f, + { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f, 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31, 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e, 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d, 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a, 0x96, 0xc8 @@ -132,8 +132,8 @@ tc3 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, NULL, 0, ikm, - sizeof (ikm), NULL, 0, NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, NULL, 0, ikm, + sizeof (ikm), NULL, 0, NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } @@ -142,17 +142,17 @@ void tc4 () { unsigned char ikm[11] = - { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; unsigned char salt[13] = - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c }; unsigned char info[10] = - { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 }; + { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 }; unsigned char okm[42] = - { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69, 0x33, 0x06, + { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69, 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81, 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15, 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2, 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3, 0xf8, 0x96 @@ -162,8 +162,9 @@ tc4 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), - ikm, sizeof (ikm), info, sizeof (info), NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), + ikm, sizeof (ikm), info, sizeof (info), + NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } @@ -172,7 +173,7 @@ void tc5 () { unsigned char ikm[80] = - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, @@ -181,7 +182,7 @@ tc5 () 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }; unsigned char salt[80] = - { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, @@ -190,7 +191,7 @@ tc5 () 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf }; unsigned char info[80] = - { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, + { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, @@ -199,7 +200,7 @@ tc5 () 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff }; unsigned char okm[82] = - { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, + { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3, 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, @@ -212,8 +213,9 @@ tc5 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), - ikm, sizeof (ikm), info, sizeof (info), NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), + ikm, sizeof (ikm), info, sizeof (info), + NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } @@ -222,11 +224,11 @@ void tc6 () { unsigned char ikm[22] = - { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; unsigned char okm[42] = - { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61, 0xd1, 0xe5, + { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61, 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06, 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06, 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0, 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3, 0x49, 0x18 @@ -236,8 +238,8 @@ tc6 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm, - sizeof (ikm), NULL, 0, NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm, + sizeof (ikm), NULL, 0, NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } @@ -246,7 +248,7 @@ void tc7 () { unsigned char ikm[80] = - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, @@ -255,7 +257,7 @@ tc7 () 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }; unsigned char salt[80] = - { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, @@ -275,7 +277,7 @@ tc7 () 0xfe, 0xff }; unsigned char okm[82] = - { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, + { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3, 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, @@ -288,9 +290,9 @@ tc7 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), - ikm, sizeof (ikm), info1, sizeof (info1), info2, - sizeof (info2), NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), + ikm, sizeof (ikm), info1, sizeof (info1), info2, + sizeof (info2), NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } @@ -299,13 +301,13 @@ void tc8 () { unsigned char ikm[32] = - { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72, + { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72, 0x11, 0xa1, 0xdc, 0x0b, 0x07, 0xdb, 0x1a, 0x5e, 0xd9, 0xb9, 0x81, 0xbe, 0xea, 0xe4, 0x31, 0x5f, 0x24, 0xff, 0xfe, 0x50, 0x8a, 0xde }; unsigned char salt[4] = { 0xfc, 0x62, 0x76, 0x35 }; unsigned char info[86] = - { 0x8c, 0x0d, 0xcf, 0xb3, 0x25, 0x6e, 0x88, 0x0d, 0xc1, 0x0b, + { 0x8c, 0x0d, 0xcf, 0xb3, 0x25, 0x6e, 0x88, 0x0d, 0xc1, 0x0b, 0x1d, 0x33, 0x15, 0x3e, 0x52, 0x0b, 0xb0, 0x77, 0xff, 0x7d, 0xc3, 0xc7, 0xef, 0xe5, 0x8e, 0x3c, 0xc4, 0x4e, 0x8b, 0x41, 0x46, 0x1f, 0x02, 0x94, 0x82, 0x35, 0xc5, 0xa6, 0x5e, 0x91, 0xd8, 0xa2, 0x90, 0xfd, 0x6f, 0xb4, @@ -315,7 +317,7 @@ tc8 () 0x74, 0x6f, 0x72, 0x00 }; unsigned char okm[16] = - { 0xd6, 0x90, 0xec, 0x9e, 0x62, 0xdf, 0xb9, 0x41, 0xff, 0x92, + { 0xd6, 0x90, 0xec, 0x9e, 0x62, 0xdf, 0xb9, 0x41, 0xff, 0x92, 0x4f, 0xd2, 0xf6, 0x1d, 0x67, 0xe0 }; char result[18]; @@ -323,9 +325,9 @@ tc8 () memset (result, 0, sizeof (result)); GNUNET_assert (GNUNET_CRYPTO_hkdf - (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt, - sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), - NULL) == GNUNET_YES); + (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt, + sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), + NULL) == GNUNET_YES); GNUNET_assert (memcmp (result, okm, l) == 0); GNUNET_assert (memcmp (result + l, "\0", 2) == 0); } diff --git a/src/util/test_crypto_ksk.c b/src/util/test_crypto_ksk.c index 2789f5edb..55d2dffcb 100644 --- a/src/util/test_crypto_ksk.c +++ b/src/util/test_crypto_ksk.c @@ -39,7 +39,7 @@ static int testCorrectKey () { const char *want = - "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000"; + "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000"; GNUNET_HashCode in; struct GNUNET_CRYPTO_RsaPrivateKey *hostkey; struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; @@ -50,27 +50,29 @@ testCorrectKey () GNUNET_CRYPTO_hash ("X", strlen ("X"), &in); hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); if (hostkey == NULL) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } + { + GNUNET_break (0); + return GNUNET_SYSERR; + } GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); GNUNET_CRYPTO_rsa_key_free (hostkey); #if 0 - for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++) + for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); + i++) printf ("%02x", ((unsigned char *) &pkey)[i]); printf ("\n"); #endif - for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++) - { - snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]); - if (0 != strncmp (out, &want[i * 2], 2)) + for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); + i++) { - fprintf (stderr, " Failed! Wanted %.2s but got %2s at %d\n", &want[i * 2], - out, i); - return GNUNET_SYSERR; + snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]); + if (0 != strncmp (out, &want[i * 2], 2)) + { + fprintf (stderr, " Failed! Wanted %.2s but got %2s at %d\n", + &want[i * 2], out, i); + return GNUNET_SYSERR; + } } - } fprintf (stderr, " OK\n"); return GNUNET_OK; } @@ -89,10 +91,10 @@ testMultiKey (const char *word) GNUNET_CRYPTO_hash (word, strlen (word), &in); hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); if (hostkey == NULL) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } + { + GNUNET_break (0); + return GNUNET_SYSERR; + } GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); /* * for (i=0;i (buf[i] = GNUNET_CRYPTO_random_u32 (mode, 1024))); for (i = 0; i < 10; i++) - { - b2 = GNUNET_CRYPTO_random_permute (mode, 1024); - if (0 == memcmp (b2, buf, sizeof (buf))) { - fprintf (stderr, "!"); + b2 = GNUNET_CRYPTO_random_permute (mode, 1024); + if (0 == memcmp (b2, buf, sizeof (buf))) + { + fprintf (stderr, "!"); + GNUNET_free (b2); + continue; + } GNUNET_free (b2); - continue; + break; } - GNUNET_free (b2); - break; - } if (i == 10) - return 1; /* virtually impossible... */ + return 1; /* virtually impossible... */ for (n = 10; n < 1024LL * 1024LL * 1024LL; n *= 10) GNUNET_break (n > GNUNET_CRYPTO_random_u64 (mode, n)); diff --git a/src/util/test_crypto_rsa.c b/src/util/test_crypto_rsa.c index f26a76a38..6a2c1b355 100644 --- a/src/util/test_crypto_rsa.c +++ b/src/util/test_crypto_rsa.c @@ -54,36 +54,36 @@ testEncryptDecrypt () ok = 0; start = GNUNET_TIME_absolute_get (); for (i = 0; i < ITER; i++) - { - fprintf (stderr, "."); - if (GNUNET_SYSERR == - GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey, - &target)) { - fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); - ok++; - continue; - } - if (-1 == - GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result, - strlen (TESTSTRING) + 1)) - { - fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); - ok++; - continue; + fprintf (stderr, "."); + if (GNUNET_SYSERR == + GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, + &pkey, &target)) + { + fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); + ok++; + continue; + } + if (-1 == + GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result, + strlen (TESTSTRING) + 1)) + { + fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); + ok++; + continue; + } + if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0) + { + printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING, + (int) MAX_TESTVAL, result); + ok++; + continue; + } } - if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0) - { - printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING, - (int) MAX_TESTVAL, result); - ok++; - continue; - } - } printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER, - (unsigned long long) - GNUNET_TIME_absolute_get_duration (start).rel_value, ok); + (unsigned long long) + GNUNET_TIME_absolute_get_duration (start).rel_value, ok); GNUNET_CRYPTO_rsa_key_free (hostkey); if (ok == 0) return GNUNET_OK; @@ -109,20 +109,20 @@ testEncryptPerformance () ok = 0; start = GNUNET_TIME_absolute_get (); for (i = 0; i < ITER; i++) - { - fprintf (stderr, "."); - if (GNUNET_SYSERR == - GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey, - &target)) { - fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); - ok++; - continue; + fprintf (stderr, "."); + if (GNUNET_SYSERR == + GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, + &pkey, &target)) + { + fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); + ok++; + continue; + } } - } printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER, - (unsigned long long) - GNUNET_TIME_absolute_get_duration (start).rel_value, ok); + (unsigned long long) + GNUNET_TIME_absolute_get_duration (start).rel_value, ok); GNUNET_CRYPTO_rsa_key_free (hostkey); if (ok != 0) return GNUNET_SYSERR; @@ -149,37 +149,39 @@ testEncryptDecryptSK () ok = 0; start = GNUNET_TIME_absolute_get (); for (i = 0; i < ITER; i++) - { - fprintf (stderr, "."); - GNUNET_CRYPTO_aes_create_session_key (&insk); - if (GNUNET_SYSERR == - GNUNET_CRYPTO_rsa_encrypt (&insk, - sizeof (struct GNUNET_CRYPTO_AesSessionKey), - &pkey, &target)) - { - fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); - ok++; - continue; - } - if (-1 == - GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk, - sizeof (struct GNUNET_CRYPTO_AesSessionKey))) - { - fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); - ok++; - continue; - } - if (0 != - memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey))) { - printf ("testEncryptDecryptSK failed!\n"); - ok++; - continue; + fprintf (stderr, "."); + GNUNET_CRYPTO_aes_create_session_key (&insk); + if (GNUNET_SYSERR == + GNUNET_CRYPTO_rsa_encrypt (&insk, + sizeof (struct + GNUNET_CRYPTO_AesSessionKey), + &pkey, &target)) + { + fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); + ok++; + continue; + } + if (-1 == + GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk, + sizeof (struct + GNUNET_CRYPTO_AesSessionKey))) + { + fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); + ok++; + continue; + } + if (0 != + memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey))) + { + printf ("testEncryptDecryptSK failed!\n"); + ok++; + continue; + } } - } printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER, - (unsigned long long) - GNUNET_TIME_absolute_get_duration (start).rel_value, ok); + (unsigned long long) + GNUNET_TIME_absolute_get_duration (start).rel_value, ok); GNUNET_CRYPTO_rsa_key_free (hostkey); if (ok != 0) return GNUNET_SYSERR; @@ -206,34 +208,34 @@ testSignVerify () purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); for (i = 0; i < ITER; i++) - { - fprintf (stderr, "."); - if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) - { - fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); - ok = GNUNET_SYSERR; - continue; - } - if (GNUNET_SYSERR == - GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig, - &pkey)) { - printf ("GNUNET_CRYPTO_rsa_verify failed!\n"); - ok = GNUNET_SYSERR; - continue; + fprintf (stderr, "."); + if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) + { + fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); + ok = GNUNET_SYSERR; + continue; + } + if (GNUNET_SYSERR == + GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, + &sig, &pkey)) + { + printf ("GNUNET_CRYPTO_rsa_verify failed!\n"); + ok = GNUNET_SYSERR; + continue; + } + if (GNUNET_SYSERR != + GNUNET_CRYPTO_rsa_verify + (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey)) + { + printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n"); + ok = GNUNET_SYSERR; + continue; + } } - if (GNUNET_SYSERR != - GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, - &purp, &sig, &pkey)) - { - printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n"); - ok = GNUNET_SYSERR; - continue; - } - } printf ("%d RSA sign/verify operations %llums\n", ITER, - (unsigned long long) - GNUNET_TIME_absolute_get_duration (start).rel_value); + (unsigned long long) + GNUNET_TIME_absolute_get_duration (start).rel_value); GNUNET_CRYPTO_rsa_key_free (hostkey); return ok; } @@ -258,18 +260,18 @@ testSignPerformance () GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); start = GNUNET_TIME_absolute_get (); for (i = 0; i < ITER; i++) - { - fprintf (stderr, "."); - if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) { - fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); - ok = GNUNET_SYSERR; - continue; + fprintf (stderr, "."); + if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) + { + fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); + ok = GNUNET_SYSERR; + continue; + } } - } printf ("%d RSA sign operations %llu ms\n", ITER, - (unsigned long long) - GNUNET_TIME_absolute_get_duration (start).rel_value); + (unsigned long long) + GNUNET_TIME_absolute_get_duration (start).rel_value); GNUNET_CRYPTO_rsa_key_free (hostkey); return ok; } @@ -326,9 +328,9 @@ main (int argc, char *argv[]) failureCount++; if (failureCount != 0) - { - printf ("\n\n%d TESTS FAILED!\n\n", failureCount); - return -1; - } + { + printf ("\n\n%d TESTS FAILED!\n\n", failureCount); + return -1; + } return 0; -} /* end of main */ +} /* end of main */ diff --git a/src/util/test_disk.c b/src/util/test_disk.c index 91154a6f5..e8ec88c9f 100644 --- a/src/util/test_disk.c +++ b/src/util/test_disk.c @@ -38,40 +38,41 @@ testReadWrite () if (strlen (TESTSTRING) != GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING), - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE)) + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE)) return 1; if (GNUNET_OK != GNUNET_DISK_file_test (".testfile")) return 1; ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1); if (ret < 0) - { - fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile"); - return 1; - } + { + fprintf (stderr, "Error reading file `%s' in testReadWrite\n", + ".testfile"); + return 1; + } tmp[ret] = '\0'; if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) - { - fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, - TESTSTRING, ".testfile"); - return 1; - } + { + fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", + tmp, TESTSTRING, ".testfile"); + return 1; + } GNUNET_DISK_file_copy (".testfile", ".testfile2"); memset (tmp, 0, sizeof (tmp)); ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1); if (ret < 0) - { - fprintf (stderr, "Error reading file `%s' in testReadWrite\n", - ".testfile2"); - return 1; - } + { + fprintf (stderr, "Error reading file `%s' in testReadWrite\n", + ".testfile2"); + return 1; + } tmp[ret] = '\0'; if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) - { - fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, - TESTSTRING, ".testfile2"); - return 1; - } + { + fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", + tmp, TESTSTRING, ".testfile2"); + return 1; + } GNUNET_break (0 == UNLINK (".testfile")); GNUNET_break (0 == UNLINK (".testfile2")); @@ -89,15 +90,15 @@ testOpenClose () long avail; fh = GNUNET_DISK_file_open (".testfile", - GNUNET_DISK_OPEN_READWRITE | - GNUNET_DISK_OPEN_CREATE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE); + GNUNET_DISK_OPEN_READWRITE | + GNUNET_DISK_OPEN_CREATE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh)); GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5)); GNUNET_DISK_file_close (fh); GNUNET_break (GNUNET_OK == - GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO)); + GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO)); if (size != 5) return 1; GNUNET_break (0 == UNLINK (".testfile")); @@ -107,19 +108,19 @@ testOpenClose () avail = GNUNET_DISK_get_blocks_available (".testfile"); GNUNET_log_skip (0, GNUNET_NO); fh = GNUNET_DISK_file_open (".testfile", - GNUNET_DISK_OPEN_READWRITE | - GNUNET_DISK_OPEN_CREATE, - GNUNET_DISK_PERM_USER_WRITE | - GNUNET_DISK_PERM_USER_READ); + GNUNET_DISK_OPEN_READWRITE | + GNUNET_DISK_OPEN_CREATE, + GNUNET_DISK_PERM_USER_WRITE | + GNUNET_DISK_PERM_USER_READ); GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh)); while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) && - (avail != -1)) + (avail != -1)) if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16)) - { - GNUNET_DISK_file_close (fh); - GNUNET_break (0 == UNLINK (".testfile")); - return 1; - } + { + GNUNET_DISK_file_close (fh); + GNUNET_break (0 == UNLINK (".testfile")); + return 1; + } GNUNET_DISK_file_close (fh); GNUNET_break (0 == UNLINK (".testfile")); @@ -146,7 +147,7 @@ testDirScan () GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more")) return 1; GNUNET_DISK_directory_scan ("test", &scan_callback, - "test" DIR_SEPARATOR_STR "entry"); + "test" DIR_SEPARATOR_STR "entry"); if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) return 1; if (ok < 2) @@ -156,7 +157,7 @@ testDirScan () static void iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di, - const char *filename, const char *dirname) + const char *filename, const char *dirname) { int *i = cls; @@ -168,7 +169,7 @@ static void iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT, - "test", &iter_callback, cls); + "test", &iter_callback, cls); } static int @@ -202,14 +203,14 @@ testGetHome () cfg = GNUNET_CONFIGURATION_create (); GNUNET_assert (cfg != NULL); GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME", - "/tmp/test-gnunet-disk-a/b/c"); + "/tmp/test-gnunet-disk-a/b/c"); fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL); GNUNET_assert (fn != NULL); GNUNET_CONFIGURATION_destroy (cfg); ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn); GNUNET_free (fn); GNUNET_break (GNUNET_OK == - GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a")); + GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a")); return ret; } @@ -220,10 +221,10 @@ testCanonicalize () GNUNET_DISK_filename_canonicalize (fn); if (0 != strcmp (fn, "ab____cd_ef__g_")) - { - GNUNET_free (fn); - return 1; - } + { + GNUNET_free (fn); + return 1; + } GNUNET_free (fn); return 0; } @@ -275,9 +276,9 @@ main (int argc, char *argv[]) failureCount += testChangeOwner (); failureCount += testDirMani (); if (failureCount != 0) - { - fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount); - return -1; - } + { + fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount); + return -1; + } return 0; -} /* end of main */ +} /* end of main */ diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c index a517887bf..f830454cd 100644 --- a/src/util/test_getopt.c +++ b/src/util/test_getopt.c @@ -63,15 +63,15 @@ testVerbose () }; if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv)) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } if (vflags != 2) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } return 0; } @@ -89,10 +89,10 @@ testVersion () }; if (-1 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv)) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } return 0; } @@ -110,10 +110,10 @@ testAbout () }; if (-1 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv)) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } return 0; } @@ -136,18 +136,18 @@ testLogOpts () }; if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv)) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } GNUNET_assert (fn != NULL); if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename"))) - { - GNUNET_break (0); - GNUNET_free (level); - GNUNET_free (fn); - return 1; - } + { + GNUNET_break (0); + GNUNET_free (level); + GNUNET_free (fn); + return 1; + } GNUNET_free (level); GNUNET_free (fn); return 0; @@ -178,15 +178,15 @@ testFlagNum () }; if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv)) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } if ((1 != flag) || (42 != num) || (42 != lnum)) - { - GNUNET_break (0); - return 1; - } + { + GNUNET_break (0); + return 1; + } return 0; } diff --git a/src/util/test_os_network.c b/src/util/test_os_network.c index 315f97d97..28cc9cd3a 100644 --- a/src/util/test_os_network.c +++ b/src/util/test_os_network.c @@ -44,10 +44,10 @@ proc (void *cls, const char *name, int isDefault, const struct sockaddr *addr, return GNUNET_OK; inet_ntop (addr->sa_family, - (addr->sa_family == - AF_INET) ? (void *) &((struct sockaddr_in *) addr)->sin_addr - : (void *) &((struct sockaddr_in6 *) addr)->sin6_addr, buf, - sizeof (buf)); + (addr->sa_family == + AF_INET) ? (void *) &((struct sockaddr_in *) addr)->sin_addr + : (void *) &((struct sockaddr_in6 *) addr)->sin6_addr, buf, + sizeof (buf)); if ((0 == strcmp ("::1", buf)) || (0 == strcmp ("127.0.0.1", buf))) *ok = 0; return GNUNET_OK; diff --git a/src/util/test_os_priority.c b/src/util/test_os_priority.c index 94e2719a2..59f3ee816 100644 --- a/src/util/test_os_priority.c +++ b/src/util/test_os_priority.c @@ -32,27 +32,27 @@ testprio () { if (GNUNET_OK != GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), - GNUNET_SCHEDULER_PRIORITY_DEFAULT)) + GNUNET_SCHEDULER_PRIORITY_DEFAULT)) return 1; if (GNUNET_OK != GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), - GNUNET_SCHEDULER_PRIORITY_UI)) + GNUNET_SCHEDULER_PRIORITY_UI)) return 1; if (GNUNET_OK != GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), - GNUNET_SCHEDULER_PRIORITY_IDLE)) + GNUNET_SCHEDULER_PRIORITY_IDLE)) return 1; if (GNUNET_OK != GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), - GNUNET_SCHEDULER_PRIORITY_BACKGROUND)) + GNUNET_SCHEDULER_PRIORITY_BACKGROUND)) return 1; if (GNUNET_OK != GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), - GNUNET_SCHEDULER_PRIORITY_HIGH)) + GNUNET_SCHEDULER_PRIORITY_HIGH)) return 1; if (GNUNET_OK != GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), - GNUNET_SCHEDULER_PRIORITY_HIGH)) + GNUNET_SCHEDULER_PRIORITY_HIGH)) return 1; return 0; } diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c index df8520546..918ef1bc5 100644 --- a/src/util/test_os_start_process.c +++ b/src/util/test_os_start_process.c @@ -53,9 +53,9 @@ end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); + } GNUNET_OS_process_wait (proc); GNUNET_OS_process_close (proc); proc = NULL; @@ -79,28 +79,28 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) #endif if (bytes < 1) - { - GNUNET_break (0); - ok = 1; - GNUNET_SCHEDULER_cancel (die_task); - GNUNET_SCHEDULER_add_now (&end_task, NULL); - return; - } + { + GNUNET_break (0); + ok = 1; + GNUNET_SCHEDULER_cancel (die_task); + GNUNET_SCHEDULER_add_now (&end_task, NULL); + return; + } ok = strncmp (&buf[0], test_phrase, strlen (test_phrase)); #if VERBOSE fprintf (stderr, "read %s\n", &buf[0]); #endif if (ok == 0) - { - GNUNET_SCHEDULER_cancel (die_task); - GNUNET_SCHEDULER_add_now (&end_task, NULL); - return; - } + { + GNUNET_SCHEDULER_cancel (die_task); + GNUNET_SCHEDULER_add_now (&end_task, NULL); + return; + } GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - stdout_read_handle, &read_call, - stdout_read_handle); + stdout_read_handle, &read_call, + stdout_read_handle); } @@ -118,16 +118,16 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) - { - GNUNET_break (0); - ok = 1; - GNUNET_free (fn); - return; - } + { + GNUNET_break (0); + ok = 1; + GNUNET_free (fn); + return; + } proc = - GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn, - "test_gnunet_echo_hello", "-", NULL); + GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn, + "test_gnunet_echo_hello", "-", NULL); GNUNET_free (fn); /* Close the write end of the read pipe */ @@ -140,26 +140,26 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) /* Write the test_phrase to the cat process */ if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) != strlen (test_phrase) + 1) - { - GNUNET_break (0); - ok = 1; - return; - } + { + GNUNET_break (0); + ok = 1; + return; + } /* Close the write end to end the cycle! */ GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); stdout_read_handle = - GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ); + GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ); die_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MINUTES, 1), &end_task, - NULL); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MINUTES, 1), &end_task, + NULL); GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - stdout_read_handle, &read_call, - (void *) stdout_read_handle); + stdout_read_handle, &read_call, + (void *) stdout_read_handle); } @@ -183,11 +183,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test-os-start-process", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret = check (); return ret; diff --git a/src/util/test_peer.c b/src/util/test_peer.c index 3817d8243..5bf5108bc 100644 --- a/src/util/test_peer.c +++ b/src/util/test_peer.c @@ -43,13 +43,13 @@ generatePeerIdList () int i; for (i = 0; i < NUMBER_OF_PEERS; i++) - { - GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, - &pidArr[i].hashPubKey); + { + GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, + &pidArr[i].hashPubKey); #if DEBUG - printf ("Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i])); + printf ("Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i])); #endif - } + } } @@ -65,25 +65,27 @@ check () GNUNET_assert (0 == GNUNET_PEER_intern (NULL)); /* Insert Peers into PeerEntry table and hashmap */ for (i = 0; i < NUMBER_OF_PEERS; i++) - { - pid = GNUNET_PEER_intern (&pidArr[i]); - if (pid != (i + 1)) { - fprintf (stderr, "Unexpected Peer ID returned by intern function\n"); - return 1; + pid = GNUNET_PEER_intern (&pidArr[i]); + if (pid != (i + 1)) + { + fprintf (stderr, + "Unexpected Peer ID returned by intern function\n"); + return 1; + } } - } /* Referencing the first 3 peers once again */ for (i = 0; i < 3; i++) - { - pid = GNUNET_PEER_intern (&pidArr[i]); - if (pid != (i + 1)) { - fprintf (stderr, "Unexpected Peer ID returned by intern function\n"); - return 1; + pid = GNUNET_PEER_intern (&pidArr[i]); + if (pid != (i + 1)) + { + fprintf (stderr, + "Unexpected Peer ID returned by intern function\n"); + return 1; + } } - } /* Dereferencing the first 3 peers once [decrementing their reference count] */ GNUNET_PEER_decrement_rcs (ids, 3); @@ -128,11 +130,11 @@ main () GNUNET_log_setup ("test-peer", "ERROR", NULL); for (i = 0; i < 1; i++) - { - generatePeerIdList (); - if (0 != check ()) - return 1; - } + { + generatePeerIdList (); + if (0 != check ()) + return 1; + } return 0; } diff --git a/src/util/test_program.c b/src/util/test_program.c index faeb4e716..0ff49b81e 100644 --- a/src/util/test_program.c +++ b/src/util/test_program.c @@ -58,7 +58,7 @@ static struct GNUNET_GETOPT_CommandLineOption options4[] = { static void runner (void *cls, char *const *args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { int *ok = cls; @@ -91,18 +91,18 @@ check () }; GNUNET_assert (GNUNET_OK == - GNUNET_PROGRAM_run (7, argv, "test_program", "A test", - options1, &runner, &ok)); + GNUNET_PROGRAM_run (7, argv, "test_program", "A test", + options1, &runner, &ok)); GNUNET_assert (GNUNET_OK == - GNUNET_PROGRAM_run (7, argv, "test_program", "A test", - options2, &runner, &ok)); + GNUNET_PROGRAM_run (7, argv, "test_program", "A test", + options2, &runner, &ok)); GNUNET_assert (GNUNET_OK == - GNUNET_PROGRAM_run (7, argv, "test_program", "A test", - options3, &runner, &ok)); + GNUNET_PROGRAM_run (7, argv, "test_program", "A test", + options3, &runner, &ok)); GNUNET_assert (GNUNET_OK == - GNUNET_PROGRAM_run (7, argv, "test_program", "A test", - options4, &runner, &ok)); + GNUNET_PROGRAM_run (7, argv, "test_program", "A test", + options4, &runner, &ok)); return ok; } diff --git a/src/util/test_pseudonym.c b/src/util/test_pseudonym.c index 20a3d3d96..30a04598c 100644 --- a/src/util/test_pseudonym.c +++ b/src/util/test_pseudonym.c @@ -45,16 +45,16 @@ iter (void *cls, const GNUNET_HashCode * pseudonym, if ((0 == memcmp (pseudonym, &id1, sizeof (GNUNET_HashCode))) && (!GNUNET_CONTAINER_meta_data_test_equal (md, meta))) - { - *ok = GNUNET_NO; - GNUNET_break (0); - } + { + *ok = GNUNET_NO; + GNUNET_break (0); + } return GNUNET_OK; } static int noti_callback (void *cls, const GNUNET_HashCode * pseudonym, - const struct GNUNET_CONTAINER_MetaData *md, int rating) + const struct GNUNET_CONTAINER_MetaData *md, int rating) { int *ret = cls; @@ -64,7 +64,7 @@ noti_callback (void *cls, const GNUNET_HashCode * pseudonym, static int fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym, - const struct GNUNET_CONTAINER_MetaData *md, int rating) + const struct GNUNET_CONTAINER_MetaData *md, int rating) { int *ret = cls; @@ -74,7 +74,7 @@ fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym, static int false_callback (void *cls, const GNUNET_HashCode * pseudonym, - const struct GNUNET_CONTAINER_MetaData *md, int rating) + const struct GNUNET_CONTAINER_MetaData *md, int rating) { return GNUNET_OK; } @@ -109,28 +109,28 @@ main (int argc, char *argv[]) (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"); cfg = GNUNET_CONFIGURATION_create (); if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf")) - { - GNUNET_CONFIGURATION_destroy (cfg); - GNUNET_break (0); - return -1; - } + { + GNUNET_CONFIGURATION_destroy (cfg); + GNUNET_break (0); + return -1; + } notiCount = 0; fakenotiCount = 0; count = 0; GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback, - &fakenotiCount); + &fakenotiCount); GNUNET_PSEUDONYM_discovery_callback_register (cfg, ¬i_callback, - ¬iCount); + ¬iCount); GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count); GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback, - &fakenotiCount); + &fakenotiCount); /* ACTUAL TEST CODE */ old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL); meta = GNUNET_CONTAINER_meta_data_create (); GNUNET_CONTAINER_meta_data_insert (meta, "", EXTRACTOR_METATYPE_TITLE, - EXTRACTOR_METAFORMAT_UTF8, "text/plain", - "test", strlen ("test") + 1); + EXTRACTOR_METAFORMAT_UTF8, "text/plain", + "test", strlen ("test") + 1); GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1); GNUNET_PSEUDONYM_add (cfg, &id1, meta); CHECK (notiCount == 1); @@ -145,11 +145,11 @@ main (int argc, char *argv[]) newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok); CHECK (old < newVal); GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_meta_data_insert (meta, "", - EXTRACTOR_METATYPE_COMMENT, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", m, - strlen (m) + 1)); + GNUNET_CONTAINER_meta_data_insert (meta, "", + EXTRACTOR_METATYPE_COMMENT, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", m, + strlen (m) + 1)); GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3); GNUNET_PSEUDONYM_add (cfg, &id3, meta); name3 = GNUNET_PSEUDONYM_id_to_name (cfg, &id3); @@ -184,7 +184,7 @@ FAILURE: GNUNET_CONTAINER_meta_data_destroy (meta); GNUNET_CONFIGURATION_destroy (cfg); GNUNET_break (GNUNET_OK == - GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test")); + GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test")); return (ok == GNUNET_YES) ? 0 : 1; } diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c index 4e248aa72..24c71b41f 100644 --- a/src/util/test_resolver_api.c +++ b/src/util/test_resolver_api.c @@ -46,12 +46,13 @@ check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen) int *ok = cls; if (salen == 0) - { - (*ok) &= ~8; - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"), - GNUNET_a2s (sa, salen)); + { + (*ok) &= ~8; + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Got IP address `%s' for our host.\n"), GNUNET_a2s (sa, + salen)); } @@ -63,21 +64,21 @@ check_localhost_num (void *cls, const char *hostname) if (hostname == NULL) return; if (0 == strcmp (hostname, "127.0.0.1")) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n", - hostname); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received correct hostname `%s'.\n", hostname); #endif - (*ok) &= ~4; - } + (*ok) &= ~4; + } else - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received invalid hostname `%s'.\n", - hostname); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received invalid hostname `%s'.\n", hostname); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } } @@ -89,19 +90,19 @@ check_localhost (void *cls, const char *hostname) if (hostname == NULL) return; if (0 == strcmp (hostname, "localhost")) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n", - hostname); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received correct hostname `%s'.\n", hostname); #endif - (*ok) &= ~2; - } + (*ok) &= ~2; + } else - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n", - hostname); - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n", + hostname); + } } static void @@ -114,19 +115,19 @@ check_127 (void *cls, const struct sockaddr *sa, socklen_t salen) return; GNUNET_assert (sizeof (struct sockaddr_in) == salen); if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK)) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n"); #endif - (*ok) &= ~1; - } + (*ok) &= ~1; + } else - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n"); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } } static void @@ -138,32 +139,32 @@ check_local_fqdn (void *cls, const char *gnunet_fqdn) char hostname[GNUNET_OS_get_hostname_max_length () + 1]; if (0 != gethostname (hostname, sizeof (hostname) - 1)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "gethostname"); - return; - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "gethostname"); + return; + } #if DEBUG_RESOLVER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), - hostname); + hostname); #endif host = gethostbyname (hostname); if (NULL == host) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not resolve our FQDN : %s %u\n"), hstrerror (h_errno), - h_errno); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not resolve our FQDN : %s %u\n"), + hstrerror (h_errno), h_errno); + return; + } GNUNET_assert (0 != host); result = strcmp (host->h_name, gnunet_fqdn); if (0 != result) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Local resolved and resolver resolved fqdns are not equal\n"); - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Local resolved and resolver resolved fqdns are not equal\n"); + } GNUNET_assert (0 == result); } @@ -180,21 +181,21 @@ check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen) GNUNET_assert (sizeof (struct sockaddr_in) == salen); if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP)) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received correct rootserver ip address.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received correct rootserver ip address.\n"); #endif - (*ok) &= ~1; - } + (*ok) &= ~1; + } else - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received incorrect rootserver ip address.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received incorrect rootserver ip address.\n"); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } } static void @@ -206,21 +207,21 @@ check_rootserver_name (void *cls, const char *hostname) return; if (0 == strcmp (hostname, ROOTSERVER_NAME)) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received correct rootserver hostname `%s'.\n", hostname); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received correct rootserver hostname `%s'.\n", hostname); #endif - (*ok) &= ~2; - } + (*ok) &= ~2; + } else - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received invalid rootserver hostname `%s'.\n", hostname); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received invalid rootserver hostname `%s'.\n", hostname); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } } static void @@ -230,7 +231,7 @@ run (void *cls, char *const *args, const char *cfgfile, int *ok = cls; struct sockaddr_in sa; struct GNUNET_TIME_Relative timeout = - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30); + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30); int count_ips = 0; char *own_fqdn; @@ -257,50 +258,50 @@ run (void *cls, char *const *args, const char *cfgfile, rootserver = gethostbyname (rootserver_name); if (rootserver == NULL) - { - /* Error: resolving ip addresses does not work */ + { + /* Error: resolving ip addresses does not work */ #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("gethostbyname() could not lookup IP address: %s\n"), - hstrerror (h_errno)); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("gethostbyname() could not lookup IP address: %s\n"), + hstrerror (h_errno)); #endif - fprintf (stderr, - "System seems to be off-line, will not run all DNS tests\n"); - *ok = 0; /* mark test as passing anyway */ - return; - } + fprintf (stderr, + "System seems to be off-line, will not run all DNS tests\n"); + *ok = 0; /* mark test as passing anyway */ + return; + } /* Counting returned IP addresses */ while (rootserver->h_addr_list[count_ips] != NULL) count_ips++; if (count_ips > 1) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "IP received range for root name server, but a root name server has only 1 IP\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "IP received range for root name server, but a root name server has only 1 IP\n"); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } /* Comparing to resolved address to the address the root name server should have */ if (strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]), ROOTSERVER_IP) != 0) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "IP received and IP for root name server differ\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "IP received and IP for root name server differ\n"); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } #if DEBUG_RESOLVER GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "System's own forward name resolution is working\n"); + "System's own forward name resolution is working\n"); #endif /* Resolve the same using GNUNET */ GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout, - &check_rootserver_ip, cls); + &check_rootserver_ip, cls); /* * Success: forward lookups work as expected @@ -311,41 +312,41 @@ run (void *cls, char *const *args, const char *cfgfile, rootserver->h_name = ""; if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr)) - { + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Could not transform root name server IP address\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Could not transform root name server IP address\n"); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } rootserver = - gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET); + gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET); if (rootserver == NULL) - { - /* Error: resolving IP addresses does not work */ + { + /* Error: resolving IP addresses does not work */ #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("gethostbyaddr() could not lookup hostname: %s\n"), - hstrerror (h_errno)); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("gethostbyaddr() could not lookup hostname: %s\n"), + hstrerror (h_errno)); #endif - GNUNET_break (0); - } + GNUNET_break (0); + } else - { - if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME)) { + if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME)) + { #if DEBUG_RESOLVER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received hostname and hostname for root name server differ\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received hostname and hostname for root name server differ\n"); #endif - GNUNET_break (0); + GNUNET_break (0); + } } - } #if DEBUG_RESOLVER GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "System's own reverse name resolution is working\n"); + "System's own reverse name resolution is working\n"); #endif /* Resolve the same using GNUNET */ @@ -360,8 +361,8 @@ run (void *cls, char *const *args, const char *cfgfile, sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP); #endif GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, - sizeof (struct sockaddr), GNUNET_YES, timeout, - &check_rootserver_name, cls); + sizeof (struct sockaddr), GNUNET_YES, timeout, + &check_rootserver_name, cls); memset (&sa, 0, sizeof (sa)); sa.sin_family = AF_INET; @@ -372,12 +373,12 @@ run (void *cls, char *const *args, const char *cfgfile, GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls); GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, - sizeof (struct sockaddr), GNUNET_YES, timeout, - &check_localhost, cls); + sizeof (struct sockaddr), GNUNET_YES, timeout, + &check_localhost, cls); GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, - sizeof (struct sockaddr), GNUNET_NO, timeout, - &check_localhost_num, cls); + sizeof (struct sockaddr), GNUNET_NO, timeout, + &check_localhost_num, cls); GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls); } @@ -391,33 +392,33 @@ check () struct GNUNET_OS_Process *proc; char *const argv[] = - { "test-resolver-api", "-c", "test_resolver_api_data.conf", + { "test-resolver-api", "-c", "test_resolver_api_data.conf", #if VERBOSE "-L", "DEBUG", #endif NULL }; struct GNUNET_GETOPT_CommandLineOption options[] = - { GNUNET_GETOPT_OPTION_END }; + { GNUNET_GETOPT_OPTION_END }; pfx = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR); GNUNET_asprintf (&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR); GNUNET_free (pfx); proc = GNUNET_OS_start_process (NULL, NULL, fn, "gnunet-service-resolver", #if VERBOSE - "-L", "DEBUG", + "-L", "DEBUG", #endif - "-c", "test_resolver_api_data.conf", NULL); + "-c", "test_resolver_api_data.conf", NULL); GNUNET_assert (NULL != proc); GNUNET_free (fn); GNUNET_assert (GNUNET_OK == - GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, - argv, "test-resolver-api", "nohelp", - options, &run, &ok)); + GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, + argv, "test-resolver-api", "nohelp", + options, &run, &ok)); if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - ok = 1; - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); + ok = 1; + } GNUNET_OS_process_wait (proc); GNUNET_OS_process_close (proc); proc = NULL; @@ -433,11 +434,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test-resolver-api", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret = check (); return ret; diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c index 788ba13fa..e2a28c1b7 100644 --- a/src/util/test_scheduler.c +++ b/src/util/test_scheduler.c @@ -36,7 +36,7 @@ task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) /* t4 should be ready (albeit with lower priority) */ GNUNET_assert (1 == - GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT)); + GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT)); GNUNET_assert (3 == *ok); (*ok) = 4; } @@ -51,7 +51,7 @@ task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) (*ok) = 3; /* t3 will go before t4: higher priority */ GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task3, - cls); + cls); } static void @@ -106,8 +106,8 @@ taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0])); GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1)); (*ok) = 8; - GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast, - cls); + GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, + &taskLast, cls); GNUNET_SCHEDULER_shutdown (); } @@ -123,10 +123,10 @@ task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_assert (NULL != p); fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ); fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE); - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], &taskRd, - cls); + GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], + &taskRd, cls); GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1], - &taskWrt, cls); + &taskWrt, cls); } @@ -226,7 +226,7 @@ taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_assert (1 == *ok); *ok = 0; GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_after - (GNUNET_SCHEDULER_NO_TASK, &taskNeverRun, NULL)); + (GNUNET_SCHEDULER_NO_TASK, &taskNeverRun, NULL)); } diff --git a/src/util/test_scheduler_delay.c b/src/util/test_scheduler_delay.c index ff6966a87..20783da8d 100644 --- a/src/util/test_scheduler_delay.c +++ b/src/util/test_scheduler_delay.c @@ -57,17 +57,17 @@ test_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) else cumDelta += (target.abs_value - now.abs_value); target = - GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, i)); + GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, i)); fprintf (stderr, "."); if (i > MAXV) - { - fprintf (stderr, "\n"); - return; - } + { + fprintf (stderr, "\n"); + return; + } GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, i), &test_task, - NULL); + (GNUNET_TIME_UNIT_MILLISECONDS, i), + &test_task, NULL); i += INCR; } @@ -77,10 +77,10 @@ check () target = GNUNET_TIME_absolute_get (); GNUNET_SCHEDULER_run (&test_task, NULL); FPRINTF (stdout, "Sleep precision: %llu ms. ", - cumDelta / 1000 / (MAXV / INCR)); + cumDelta / 1000 / (MAXV / INCR)); if (cumDelta <= 10 * MAXV / INCR) fprintf (stdout, "Timer precision is excellent.\n"); - else if (cumDelta <= 50 * MAXV / INCR) /* 50 ms average deviation */ + else if (cumDelta <= 50 * MAXV / INCR) /* 50 ms average deviation */ fprintf (stdout, "Timer precision is good.\n"); else if (cumDelta > 250 * MAXV / INCR) fprintf (stdout, "Timer precision is awful.\n"); diff --git a/src/util/test_server.c b/src/util/test_server.c index 6c79e6064..360c9c242 100644 --- a/src/util/test_server.c +++ b/src/util/test_server.c @@ -61,7 +61,7 @@ finish_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void recv_fin_cb (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { GNUNET_assert (ok == 5); ok = 6; @@ -98,19 +98,20 @@ reply_msg (void *cls, size_t size, void *buf) static void recv_cb (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { GNUNET_assert (ok == 2); ok = 3; argclient = client; GNUNET_SERVER_client_keep (argclient); - GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)); + GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == + ntohs (message->size)); GNUNET_assert (MY_TYPE == ntohs (message->type)); GNUNET_assert (NULL != - GNUNET_SERVER_notify_transmit_ready (client, - ntohs (message->size), - TIMEOUT, &reply_msg, - NULL)); + GNUNET_SERVER_notify_transmit_ready (client, + ntohs (message->size), + TIMEOUT, &reply_msg, + NULL)); } @@ -146,12 +147,12 @@ transmit_initial_message (void *cls, size_t size, void *buf) msg.size = htons (sizeof (struct GNUNET_MessageHeader)); memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader)); GNUNET_assert (NULL != - GNUNET_CLIENT_notify_transmit_ready (cc, - sizeof (struct - GNUNET_MessageHeader), - TIMEOUT, GNUNET_YES, - &transmit_second_message, - NULL)); + GNUNET_CLIENT_notify_transmit_ready (cc, + sizeof (struct + GNUNET_MessageHeader), + TIMEOUT, GNUNET_YES, + &transmit_second_message, + NULL)); GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT); return sizeof (struct GNUNET_MessageHeader); } @@ -180,18 +181,18 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT); GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", - "localhost"); + "localhost"); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); cc = GNUNET_CLIENT_connect ("test-server", cfg); GNUNET_assert (cc != NULL); GNUNET_assert (NULL != - GNUNET_CLIENT_notify_transmit_ready (cc, - sizeof (struct - GNUNET_MessageHeader), - TIMEOUT, GNUNET_YES, - &transmit_initial_message, - NULL)); + GNUNET_CLIENT_notify_transmit_ready (cc, + sizeof (struct + GNUNET_MessageHeader), + TIMEOUT, GNUNET_YES, + &transmit_initial_message, + NULL)); } diff --git a/src/util/test_server_disconnect.c b/src/util/test_server_disconnect.c index 69207f81a..e38840d92 100644 --- a/src/util/test_server_disconnect.c +++ b/src/util/test_server_disconnect.c @@ -81,13 +81,14 @@ server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void recv_cb (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { GNUNET_assert (ok == 2); ok = 3; GNUNET_SERVER_client_keep (client); GNUNET_SCHEDULER_add_now (&server_disconnect, client); - GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)); + GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == + ntohs (message->size)); GNUNET_assert (MY_TYPE == ntohs (message->type)); GNUNET_SERVER_receive_done (client, GNUNET_OK); } @@ -138,18 +139,18 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT); GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", - "localhost"); + "localhost"); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); cc = GNUNET_CLIENT_connect ("test-server", cfg); GNUNET_assert (cc != NULL); GNUNET_assert (NULL != - GNUNET_CLIENT_notify_transmit_ready (cc, - sizeof (struct - GNUNET_MessageHeader), - TIMEOUT, GNUNET_YES, - &transmit_initial_message, - NULL)); + GNUNET_CLIENT_notify_transmit_ready (cc, + sizeof (struct + GNUNET_MessageHeader), + TIMEOUT, GNUNET_YES, + &transmit_initial_message, + NULL)); } diff --git a/src/util/test_server_with_client.c b/src/util/test_server_with_client.c index 5b6e51718..21c706b07 100644 --- a/src/util/test_server_with_client.c +++ b/src/util/test_server_with_client.c @@ -58,7 +58,7 @@ send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { void *addr; size_t addrlen; @@ -66,7 +66,8 @@ recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient, struct sockaddr_in *have; GNUNET_assert (GNUNET_OK == - GNUNET_SERVER_client_get_address (argclient, &addr, &addrlen)); + GNUNET_SERVER_client_get_address (argclient, &addr, + &addrlen)); GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); have = addr; @@ -80,21 +81,21 @@ recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient, GNUNET_assert (0 == memcmp (&sa, addr, addrlen)); GNUNET_free (addr); switch (ok) - { - case 2: - ok++; - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 50), - &send_done, argclient); - break; - case 4: - ok++; - GNUNET_CLIENT_disconnect (client, GNUNET_YES); - GNUNET_SERVER_receive_done (argclient, GNUNET_OK); - break; - default: - GNUNET_assert (0); - } + { + case 2: + ok++; + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 50), + &send_done, argclient); + break; + case 4: + ok++; + GNUNET_CLIENT_disconnect (client, GNUNET_YES); + GNUNET_SERVER_receive_done (argclient, GNUNET_OK); + break; + default: + GNUNET_assert (0); + } } @@ -169,24 +170,25 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sa.sin_family = AF_INET; sa.sin_port = htons (PORT); server = - GNUNET_SERVER_create (NULL, NULL, sap, slens, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO); + GNUNET_SERVER_create (NULL, NULL, sap, slens, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO); GNUNET_assert (server != NULL); handlers[0].callback_cls = cls; GNUNET_SERVER_add_handlers (server, handlers); GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls); cfg = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT); - GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost"); + GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", + "localhost"); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); client = GNUNET_CLIENT_connect ("test", cfg); GNUNET_assert (client != NULL); GNUNET_CLIENT_notify_transmit_ready (client, 256, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 250), - GNUNET_NO, ¬ify_ready, NULL); + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 250), + GNUNET_NO, ¬ify_ready, NULL); } @@ -211,11 +213,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_server_with_client", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check (); return ret; diff --git a/src/util/test_server_with_client_unix.c b/src/util/test_server_with_client_unix.c index 4f80f188c..11f731243 100644 --- a/src/util/test_server_with_client_unix.c +++ b/src/util/test_server_with_client_unix.c @@ -56,24 +56,24 @@ send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { switch (ok) - { - case 2: - ok++; - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 50), - &send_done, argclient); - break; - case 4: - ok++; - GNUNET_CLIENT_disconnect (client, GNUNET_YES); - GNUNET_SERVER_receive_done (argclient, GNUNET_OK); - break; - default: - GNUNET_assert (0); - } + { + case 2: + ok++; + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 50), + &send_done, argclient); + break; + case 4: + ok++; + GNUNET_CLIENT_disconnect (client, GNUNET_YES); + GNUNET_SERVER_receive_done (argclient, GNUNET_OK); + break; + default: + GNUNET_assert (0); + } } @@ -156,9 +156,9 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sap[1] = NULL; slens[1] = 0; server = - GNUNET_SERVER_create (NULL, NULL, sap, slens, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO); + GNUNET_SERVER_create (NULL, NULL, sap, slens, + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO); GNUNET_assert (server != NULL); handlers[0].callback_cls = cls; GNUNET_SERVER_add_handlers (server, handlers); @@ -167,14 +167,14 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath); GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", - "localhost"); + "localhost"); client = GNUNET_CLIENT_connect ("test", cfg); GNUNET_assert (client != NULL); GNUNET_CLIENT_notify_transmit_ready (client, 256, - GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, 250), - GNUNET_NO, ¬ify_ready, NULL); + GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, 250), + GNUNET_NO, ¬ify_ready, NULL); } @@ -199,11 +199,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test_server_with_client_unix", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check (); return ret; diff --git a/src/util/test_service.c b/src/util/test_service.c index 1b78932f8..33d69db10 100644 --- a/src/util/test_service.c +++ b/src/util/test_service.c @@ -68,11 +68,11 @@ ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) client = GNUNET_CLIENT_connect ("test_service", cfg); GNUNET_assert (client != NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Client connecting, waiting to transmit\n"); + "Client connecting, waiting to transmit\n"); GNUNET_CLIENT_notify_transmit_ready (client, - sizeof (struct GNUNET_MessageHeader), - GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, - &build_msg, client); + sizeof (struct GNUNET_MessageHeader), + GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, + &build_msg, client); } @@ -85,7 +85,7 @@ do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void recv_cb (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + const struct GNUNET_MessageHeader *message) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n"); GNUNET_SERVER_receive_done (client, GNUNET_OK); @@ -105,12 +105,12 @@ static struct GNUNET_SERVER_MessageHandler myhandlers[] = { static void runner (void *cls, struct GNUNET_SERVER_Handle *server, - const struct GNUNET_CONFIGURATION_Handle *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n"); GNUNET_SERVER_add_handlers (server, myhandlers); GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS, - &ready, (void *) cfg); + &ready, (void *) cfg); } @@ -136,8 +136,9 @@ check () }; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n"); GNUNET_assert (GNUNET_OK == - GNUNET_SERVICE_run (5, argv, "test_service", - GNUNET_SERVICE_OPTION_NONE, &runner, &ok)); + GNUNET_SERVICE_run (5, argv, "test_service", + GNUNET_SERVICE_OPTION_NONE, &runner, + &ok)); GNUNET_assert (0 == ok); return ok; } @@ -154,19 +155,19 @@ ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_assert (client != NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n"); GNUNET_CLIENT_notify_transmit_ready (client, - sizeof (struct GNUNET_MessageHeader), - GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, - &build_msg, client); + sizeof (struct GNUNET_MessageHeader), + GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, + &build_msg, client); } static void runner6 (void *cls, struct GNUNET_SERVER_Handle *server, - const struct GNUNET_CONFIGURATION_Handle *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n"); GNUNET_SERVER_add_handlers (server, myhandlers); GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS, - &ready6, (void *) cfg); + &ready6, (void *) cfg); } /** @@ -190,9 +191,9 @@ check6 () }; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n"); GNUNET_assert (GNUNET_OK == - GNUNET_SERVICE_run (5, argv, "test_service6", - GNUNET_SERVICE_OPTION_NONE, &runner6, - &ok)); + GNUNET_SERVICE_run (5, argv, "test_service6", + GNUNET_SERVICE_OPTION_NONE, &runner6, + &ok)); GNUNET_assert (0 == ok); return ok; } @@ -201,11 +202,12 @@ check6 () static void start_stop_main (void *cls, char *const *args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { int *ret = cls; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service using start method\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Starting service using start method\n"); sctx = GNUNET_SERVICE_start ("test_service", cfg); GNUNET_assert (NULL != sctx); runner (cls, GNUNET_SERVICE_get_server (sctx), cfg); @@ -234,8 +236,9 @@ check_start_stop () int ret = 1; GNUNET_assert (GNUNET_OK == - GNUNET_PROGRAM_run (5, argv, "test-service-program", "no help", - options, &start_stop_main, &ret)); + GNUNET_PROGRAM_run (5, argv, "test-service-program", + "no help", options, &start_stop_main, + &ret)); GNUNET_break (0 == ret); return ret; @@ -250,11 +253,11 @@ main (int argc, char *argv[]) GNUNET_log_setup ("test-service", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); + NULL); ret += check (); ret += check (); @@ -263,22 +266,22 @@ main (int argc, char *argv[]) s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); #endif if (NULL == s) - { - if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || - (errno == EACCES)) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); - return 1; + if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || + (errno == EACCES)) + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); + return 1; + } + fprintf (stderr, + "IPv6 support seems to not be available (%s), not testing it!\n", + strerror (errno)); } - fprintf (stderr, - "IPv6 support seems to not be available (%s), not testing it!\n", - strerror (errno)); - } else - { - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); - ret += check6 (); - } + { + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); + ret += check6 (); + } ret += check_start_stop (); return ret; diff --git a/src/util/test_strings.c b/src/util/test_strings.c index c6d604144..bb0e6088f 100644 --- a/src/util/test_strings.c +++ b/src/util/test_strings.c @@ -50,18 +50,18 @@ check () WANT (buf, b); sprintf (buf, "4 %s", _( /* time unit */ "ms")); b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, - 4)); + (GNUNET_TIME_UNIT_MILLISECONDS, + 4)); WANT (buf, b); sprintf (buf, "7 %s", _( /* time unit */ "s")); b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, - 7 * 1000)); + (GNUNET_TIME_UNIT_MILLISECONDS, + 7 * 1000)); WANT (buf, b); sprintf (buf, "7 %s", _( /* time unit */ "h")); b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, - 7 * 60 * 60 * 1000)); + (GNUNET_TIME_UNIT_MILLISECONDS, + 7 * 60 * 60 * 1000)); WANT (buf, b); #ifndef MINGW hdir = getenv ("HOME"); @@ -88,12 +88,12 @@ check () * where the details of the day and hour depend on the timezone; * however, the "0:05 19" should always be there; hence: */ if (NULL == strstr (r, "0:05 19")) - { - fprintf (stderr, "Got %s\n", r); - GNUNET_break (0); - GNUNET_free (r); - return 1; - } + { + fprintf (stderr, "Got %s\n", r); + GNUNET_break (0); + GNUNET_free (r); + return 1; + } GNUNET_free (r); b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII"); WANT ("TEST", b); diff --git a/src/util/test_time.c b/src/util/test_time.c index c5232ff2a..80109615a 100644 --- a/src/util/test_time.c +++ b/src/util/test_time.c @@ -63,7 +63,7 @@ check () /*check zero */ rel.rel_value = (UINT64_MAX) - 1024; GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value == - GNUNET_TIME_relative_multiply (rel, 0).rel_value); + GNUNET_TIME_relative_multiply (rel, 0).rel_value); /* test infinity-check for relative to absolute */ GNUNET_log_skip (1, GNUNET_NO); @@ -74,11 +74,11 @@ check () /*check relative to absolute */ rel.rel_value = 0; GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value == - GNUNET_TIME_relative_to_absolute (rel).abs_value); + GNUNET_TIME_relative_to_absolute (rel).abs_value); /*check forever */ rel.rel_value = UINT64_MAX; GNUNET_assert (GNUNET_TIME_absolute_get_forever ().abs_value == - GNUNET_TIME_relative_to_absolute (rel).abs_value); + GNUNET_TIME_relative_to_absolute (rel).abs_value); /* check overflow for r2a */ rel.rel_value = (UINT64_MAX) - 1024; GNUNET_log_skip (1, GNUNET_NO); @@ -103,13 +103,13 @@ check () /* check relation check in get_duration */ future.abs_value = now.abs_value + 1000000; - GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value == - 1000000); - GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value == - 0); + GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value + == 1000000); + GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value + == 0); GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value - == forever.abs_value); + == forever.abs_value); past.abs_value = now.abs_value - 1000000; rel = GNUNET_TIME_absolute_get_duration (future); @@ -127,7 +127,7 @@ check () GNUNET_assert (rel.rel_value <= 1000000); forever = GNUNET_TIME_absolute_get_forever (); GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value == - GNUNET_TIME_absolute_get_remaining (forever).rel_value); + GNUNET_TIME_absolute_get_remaining (forever).rel_value); /* check endianess */ reln = GNUNET_TIME_relative_hton (rel); @@ -152,23 +152,24 @@ check () GNUNET_assert (future.abs_value == now.abs_value); GNUNET_assert (forever.abs_value == - GNUNET_TIME_absolute_subtract (forever, - GNUNET_TIME_UNIT_MINUTES).abs_value); + GNUNET_TIME_absolute_subtract (forever, + GNUNET_TIME_UNIT_MINUTES).abs_value); /*check absolute subtract */ now.abs_value = 50000; rel.rel_value = 100000; GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value == - (GNUNET_TIME_absolute_subtract (now, rel)).abs_value); + (GNUNET_TIME_absolute_subtract (now, rel)).abs_value); rel.rel_value = 10000; - GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value); + GNUNET_assert (40000 == + (GNUNET_TIME_absolute_subtract (now, rel)).abs_value); /*check relative divide */ GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == - (GNUNET_TIME_relative_divide (rel, 0)).rel_value); + (GNUNET_TIME_relative_divide (rel, 0)).rel_value); rel = GNUNET_TIME_UNIT_FOREVER_REL; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == - (GNUNET_TIME_relative_divide (rel, 2)).rel_value); + (GNUNET_TIME_relative_divide (rel, 2)).rel_value); rel = GNUNET_TIME_relative_divide (relUnit, 2); GNUNET_assert (rel.rel_value == relUnit.rel_value / 2); @@ -182,50 +183,51 @@ check () forever = GNUNET_TIME_absolute_get_forever (); forever.abs_value = forever.abs_value - 1024; GNUNET_assert (GNUNET_TIME_absolute_get_zero ().abs_value == - GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value); + GNUNET_TIME_calculate_eta (forever, 50000, + 100000).rel_value); /* check zero */ GNUNET_log_skip (1, GNUNET_NO); GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value == - (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value); + (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value); GNUNET_log_skip (0, GNUNET_YES); /*check forever */ GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == - (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value); + (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value); /*check relative subtract */ now = GNUNET_TIME_absolute_get (); rel.rel_value = now.abs_value; relForever.rel_value = rel.rel_value + 1024; GNUNET_assert (1024 == - GNUNET_TIME_relative_subtract (relForever, rel).rel_value); + GNUNET_TIME_relative_subtract (relForever, rel).rel_value); /*check zero */ GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value == - GNUNET_TIME_relative_subtract (rel, relForever).rel_value); + GNUNET_TIME_relative_subtract (rel, relForever).rel_value); /*check forever */ rel.rel_value = UINT64_MAX; GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value == - GNUNET_TIME_relative_subtract (rel, relForever).rel_value); + GNUNET_TIME_relative_subtract (rel, relForever).rel_value); /*check GNUNET_TIME_relative_min */ now = GNUNET_TIME_absolute_get (); rel.rel_value = now.abs_value; relForever.rel_value = rel.rel_value - 1024; GNUNET_assert (relForever.rel_value == - GNUNET_TIME_relative_min (rel, relForever).rel_value); + GNUNET_TIME_relative_min (rel, relForever).rel_value); /*check GNUNET_TIME_relative_max */ GNUNET_assert (rel.rel_value == - GNUNET_TIME_relative_max (rel, relForever).rel_value); + GNUNET_TIME_relative_max (rel, relForever).rel_value); /*check GNUNET_TIME_absolute_min */ now = GNUNET_TIME_absolute_get (); last.abs_value = now.abs_value - 1024; GNUNET_assert (last.abs_value == - GNUNET_TIME_absolute_min (now, last).abs_value); + GNUNET_TIME_absolute_min (now, last).abs_value); /*check GNUNET_TIME_absolute_max */ GNUNET_assert (now.abs_value == - GNUNET_TIME_absolute_max (now, last).abs_value); + GNUNET_TIME_absolute_max (now, last).abs_value); return 0; } diff --git a/src/util/time.c b/src/util/time.c index d3dd3b5ec..e48377fa3 100644 --- a/src/util/time.c +++ b/src/util/time.c @@ -26,6 +26,8 @@ #include "platform.h" #include "gnunet_time_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + static long long timestamp_offset; /** @@ -53,8 +55,8 @@ GNUNET_TIME_absolute_get () GETTIMEOFDAY (&tv, NULL); ret.abs_value = - (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) + - ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset; + (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) + + ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset; return ret; } @@ -128,10 +130,10 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel) struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); if (rel.rel_value + now.abs_value < rel.rel_value) - { - GNUNET_break (0); /* overflow... */ - return GNUNET_TIME_absolute_get_forever (); - } + { + GNUNET_break (0); /* overflow... */ + return GNUNET_TIME_absolute_get_forever (); + } ret.abs_value = rel.rel_value + now.abs_value; return ret; } @@ -146,7 +148,7 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel) */ struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1, - struct GNUNET_TIME_Relative t2) + struct GNUNET_TIME_Relative t2) { return (t1.rel_value < t2.rel_value) ? t1 : t2; } @@ -161,7 +163,7 @@ GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1, */ struct GNUNET_TIME_Relative GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1, - struct GNUNET_TIME_Relative t2) + struct GNUNET_TIME_Relative t2) { return (t1.rel_value > t2.rel_value) ? t1 : t2; } @@ -177,7 +179,7 @@ GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1, */ struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1, - struct GNUNET_TIME_Absolute t2) + struct GNUNET_TIME_Absolute t2) { return (t1.abs_value < t2.abs_value) ? t1 : t2; } @@ -192,7 +194,7 @@ GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1, */ struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1, - struct GNUNET_TIME_Absolute t2) + struct GNUNET_TIME_Absolute t2) { return (t1.abs_value > t2.abs_value) ? t1 : t2; } @@ -228,7 +230,7 @@ GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future) */ struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start, - struct GNUNET_TIME_Absolute end) + struct GNUNET_TIME_Absolute end) { struct GNUNET_TIME_Relative ret; @@ -269,17 +271,17 @@ GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence) */ struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start, - struct GNUNET_TIME_Relative duration) + struct GNUNET_TIME_Relative duration) { struct GNUNET_TIME_Absolute ret; if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX)) return GNUNET_TIME_absolute_get_forever (); if (start.abs_value + duration.rel_value < start.abs_value) - { - GNUNET_break (0); - return GNUNET_TIME_absolute_get_forever (); - } + { + GNUNET_break (0); + return GNUNET_TIME_absolute_get_forever (); + } ret.abs_value = start.abs_value + duration.rel_value; return ret; } @@ -295,7 +297,7 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start, */ struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start, - struct GNUNET_TIME_Relative duration) + struct GNUNET_TIME_Relative duration) { struct GNUNET_TIME_Absolute ret; @@ -315,7 +317,7 @@ GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start, */ struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, - unsigned int factor) + unsigned int factor) { struct GNUNET_TIME_Relative ret; @@ -323,10 +325,10 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, return GNUNET_TIME_relative_get_zero (); ret.rel_value = rel.rel_value * (unsigned long long) factor; if (ret.rel_value / factor != rel.rel_value) - { - GNUNET_break (0); - return GNUNET_TIME_relative_get_forever (); - } + { + GNUNET_break (0); + return GNUNET_TIME_relative_get_forever (); + } return ret; } @@ -340,7 +342,7 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, */ struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel, - unsigned int factor) + unsigned int factor) { struct GNUNET_TIME_Relative ret; @@ -363,8 +365,8 @@ GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel, * assuming it continues at the same speed */ struct GNUNET_TIME_Relative -GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished, - uint64_t total) +GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, + uint64_t finished, uint64_t total) { struct GNUNET_TIME_Relative dur; double exp; @@ -391,17 +393,17 @@ GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished, */ struct GNUNET_TIME_Relative GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1, - struct GNUNET_TIME_Relative a2) + struct GNUNET_TIME_Relative a2) { struct GNUNET_TIME_Relative ret; if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX)) return GNUNET_TIME_relative_get_forever (); if (a1.rel_value + a2.rel_value < a1.rel_value) - { - GNUNET_break (0); - return GNUNET_TIME_relative_get_forever (); - } + { + GNUNET_break (0); + return GNUNET_TIME_relative_get_forever (); + } ret.rel_value = a1.rel_value + a2.rel_value; return ret; } @@ -416,7 +418,7 @@ GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1, */ struct GNUNET_TIME_Relative GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1, - struct GNUNET_TIME_Relative a2) + struct GNUNET_TIME_Relative a2) { struct GNUNET_TIME_Relative ret; diff --git a/src/util/winproc.c b/src/util/winproc.c index 9594c5d6f..cf4240cd7 100644 --- a/src/util/winproc.c +++ b/src/util/winproc.c @@ -72,9 +72,9 @@ TSetNamedSecurityInfo GNSetNamedSecurityInfo; void plibc_panic (int err, char *msg) { - GNUNET_log (((err == - INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR), - "%s", msg); + LOG (((err == + INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR), + "%s", msg); } /** @@ -99,137 +99,139 @@ GNInitWinEnv () /* Function to get CPU usage under Win NT */ if (hNTDLL) - { - GNNtQuerySystemInformation = - (TNtQuerySystemInformation) GetProcAddress (hNTDLL, - "NtQuerySystemInformation"); - } + { + GNNtQuerySystemInformation = + (TNtQuerySystemInformation) GetProcAddress (hNTDLL, + "NtQuerySystemInformation"); + } else - { - GNNtQuerySystemInformation = NULL; - } + { + GNNtQuerySystemInformation = NULL; + } /* Functions to get information about a network adapter */ hIphlpapi = LoadLibrary ("iphlpapi.dll"); if (hIphlpapi) - { - GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry"); - GNGetIpAddrTable = - (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable"); - GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable"); - GNGetBestInterface = - (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface"); - GGetAdaptersInfo = - (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo"); - } + { + GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry"); + GNGetIpAddrTable = + (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable"); + GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable"); + GNGetBestInterface = + (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface"); + GGetAdaptersInfo = + (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo"); + } else - { - GNGetIfEntry = NULL; - GNGetIpAddrTable = NULL; - GNGetIfTable = NULL; - GNGetBestInterface = NULL; - GGetAdaptersInfo = NULL; - } + { + GNGetIfEntry = NULL; + GNGetIpAddrTable = NULL; + GNGetIfTable = NULL; + GNGetBestInterface = NULL; + GGetAdaptersInfo = NULL; + } /* Service & Account functions */ hAdvapi = LoadLibrary ("advapi32.dll"); if (hAdvapi) - { - GNOpenSCManager = - (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA"); - GNCreateService = - (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA"); - GNCloseServiceHandle = - (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle"); - GNDeleteService = - (TDeleteService) GetProcAddress (hAdvapi, "DeleteService"); - GNRegisterServiceCtrlHandler = - (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi, - "RegisterServiceCtrlHandlerA"); - GNSetServiceStatus = - (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus"); - GNStartServiceCtrlDispatcher = - (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi, - "StartServiceCtrlDispatcherA"); - GNControlService = - (TControlService) GetProcAddress (hAdvapi, "ControlService"); - GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA"); + { + GNOpenSCManager = + (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA"); + GNCreateService = + (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA"); + GNCloseServiceHandle = + (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle"); + GNDeleteService = + (TDeleteService) GetProcAddress (hAdvapi, "DeleteService"); + GNRegisterServiceCtrlHandler = + (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi, + "RegisterServiceCtrlHandlerA"); + GNSetServiceStatus = + (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus"); + GNStartServiceCtrlDispatcher = + (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi, + "StartServiceCtrlDispatcherA"); + GNControlService = + (TControlService) GetProcAddress (hAdvapi, "ControlService"); + GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA"); - GNLsaOpenPolicy = - (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy"); - GNLsaAddAccountRights = - (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights"); - GNLsaRemoveAccountRights = - (TLsaRemoveAccountRights) GetProcAddress (hAdvapi, - "LsaRemoveAccountRights"); - GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose"); - GNLookupAccountName = - (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA"); + GNLsaOpenPolicy = + (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy"); + GNLsaAddAccountRights = + (TLsaAddAccountRights) GetProcAddress (hAdvapi, + "LsaAddAccountRights"); + GNLsaRemoveAccountRights = + (TLsaRemoveAccountRights) GetProcAddress (hAdvapi, + "LsaRemoveAccountRights"); + GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose"); + GNLookupAccountName = + (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA"); - GNGetFileSecurity = - (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA"); - GNInitializeSecurityDescriptor = - (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi, - "InitializeSecurityDescriptor"); - GNGetSecurityDescriptorDacl = - (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi, - "GetSecurityDescriptorDacl"); - GNGetAclInformation = - (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation"); - GNInitializeAcl = - (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl"); - GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce"); - GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid"); - GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce"); - GNAddAccessAllowedAce = - (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce"); - GNSetNamedSecurityInfo = - (TSetNamedSecurityInfo) GetProcAddress (hAdvapi, - "SetNamedSecurityInfoA"); - } + GNGetFileSecurity = + (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA"); + GNInitializeSecurityDescriptor = + (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi, + "InitializeSecurityDescriptor"); + GNGetSecurityDescriptorDacl = + (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi, + "GetSecurityDescriptorDacl"); + GNGetAclInformation = + (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation"); + GNInitializeAcl = + (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl"); + GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce"); + GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid"); + GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce"); + GNAddAccessAllowedAce = + (TAddAccessAllowedAce) GetProcAddress (hAdvapi, + "AddAccessAllowedAce"); + GNSetNamedSecurityInfo = + (TSetNamedSecurityInfo) GetProcAddress (hAdvapi, + "SetNamedSecurityInfoA"); + } else - { - GNOpenSCManager = NULL; - GNCreateService = NULL; - GNCloseServiceHandle = NULL; - GNDeleteService = NULL; - GNRegisterServiceCtrlHandler = NULL; - GNSetServiceStatus = NULL; - GNStartServiceCtrlDispatcher = NULL; - GNControlService = NULL; - GNOpenService = NULL; + { + GNOpenSCManager = NULL; + GNCreateService = NULL; + GNCloseServiceHandle = NULL; + GNDeleteService = NULL; + GNRegisterServiceCtrlHandler = NULL; + GNSetServiceStatus = NULL; + GNStartServiceCtrlDispatcher = NULL; + GNControlService = NULL; + GNOpenService = NULL; - GNLsaOpenPolicy = NULL; - GNLsaAddAccountRights = NULL; - GNLsaRemoveAccountRights = NULL; - GNLsaClose = NULL; - GNLookupAccountName = NULL; + GNLsaOpenPolicy = NULL; + GNLsaAddAccountRights = NULL; + GNLsaRemoveAccountRights = NULL; + GNLsaClose = NULL; + GNLookupAccountName = NULL; - GNGetFileSecurity = NULL; - GNInitializeSecurityDescriptor = NULL; - GNGetSecurityDescriptorDacl = NULL; - GNGetAclInformation = NULL; - GNInitializeAcl = NULL; - GNGetAce = NULL; - GNEqualSid = NULL; - GNAddAce = NULL; - GNAddAccessAllowedAce = NULL; - GNSetNamedSecurityInfo = NULL; - } + GNGetFileSecurity = NULL; + GNInitializeSecurityDescriptor = NULL; + GNGetSecurityDescriptorDacl = NULL; + GNGetAclInformation = NULL; + GNInitializeAcl = NULL; + GNGetAce = NULL; + GNEqualSid = NULL; + GNAddAce = NULL; + GNAddAccessAllowedAce = NULL; + GNSetNamedSecurityInfo = NULL; + } /* Account function */ hNetapi = LoadLibrary ("netapi32.dll"); if (hNetapi) - { - GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd"); - GNNetUserSetInfo = - (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo"); - } + { + GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd"); + GNNetUserSetInfo = + (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo"); + } else - { - GNNetUserAdd = NULL; - GNNetUserSetInfo = NULL; - } + { + GNNetUserAdd = NULL; + GNNetUserSetInfo = NULL; + } return ret; } -- cgit v1.2.3