diff options
Diffstat (limited to 'src/util')
91 files changed, 10847 insertions, 11119 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c index a736b7607e..0920362f5a 100644 --- a/src/util/bandwidth.c +++ b/src/util/bandwidth.c @@ -60,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__))); } @@ -78,9 +78,9 @@ 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; @@ -105,20 +105,20 @@ 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 - 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 LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -145,8 +145,8 @@ 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 (); @@ -155,8 +155,7 @@ GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av, #if DEBUG_BANDWIDTH 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); + (unsigned int) av->available_bytes_per_s__, (unsigned int) max_carry_s); #endif } @@ -179,25 +178,25 @@ 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 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__, + "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 @@ -217,7 +216,7 @@ 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; @@ -226,29 +225,29 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *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 - 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; } @@ -264,35 +263,34 @@ 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 - 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 - 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 - 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; } @@ -315,9 +313,9 @@ 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 LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -336,23 +334,22 @@ 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 - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Tracker %p bandwidth changed to %u Bps\n", av, + 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 9c2b9d0dd9..41ad5fd5d3 100644 --- a/src/util/bio.c +++ b/src/util/bio.c @@ -58,8 +58,7 @@ 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); @@ -105,7 +104,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; @@ -116,38 +115,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) { - /* 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; + 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; } - while (pos < len); /* should always be true */ + h->pos = 0; + h->have = ret; + } + while (pos < len); /* should always be true */ return GNUNET_OK; } @@ -163,8 +162,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]; @@ -185,41 +184,39 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, */ 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; } @@ -234,7 +231,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; @@ -243,32 +240,30 @@ 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; @@ -286,7 +281,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; @@ -308,7 +303,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; @@ -344,15 +339,13 @@ 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; @@ -374,18 +367,18 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) int ret; if (NULL == h->fd) - { - ret = GNUNET_SYSERR; - } + { + 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); - } + { + 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; } @@ -401,7 +394,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; @@ -412,27 +405,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) { - /* 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; + GNUNET_DISK_file_close (h->fd); + h->fd = NULL; + return GNUNET_SYSERR; /* error */ } - while (pos < n); /* should always be true */ + h->have = 0; + } + while (pos < n); /* should always be true */ GNUNET_break (0); return GNUNET_OK; } @@ -468,7 +461,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; @@ -477,19 +470,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 d2f347947c..e71ccdf394 100644 --- a/src/util/client.c +++ b/src/util/client.c @@ -259,8 +259,7 @@ 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; @@ -270,80 +269,78 @@ 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? */ { - /* 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) - { + sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); + if (sock != NULL) + { #if DEBUG_CLIENT - 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_non_null (unixpath); + GNUNET_free (unixpath); + return sock; + } } + 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))) - { - 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); - LOG (GNUNET_ERROR_TYPE_WARNING, - _("Need a non-empty hostname for service `%s'.\n"), service_name); - return NULL; - } + { + 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); - } + 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 - 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); @@ -360,7 +357,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; @@ -393,33 +390,33 @@ GNUNET_CLIENT_connect (const char *service_name, */ 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); @@ -436,7 +433,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; } @@ -455,7 +452,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; @@ -465,22 +462,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 - 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); - } - return; + 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; + } /* FIXME: optimize for common fast case where buf contains the * entire message and we need no copying... */ @@ -489,22 +486,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); } @@ -520,23 +517,22 @@ 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 - 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; @@ -556,36 +552,35 @@ 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 - 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); + } } @@ -596,7 +591,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); } @@ -615,18 +610,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 - 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); } @@ -647,14 +642,14 @@ write_test (void *cls, size_t size, void *buf) struct GNUNET_MessageHeader *msg; if (size < sizeof (struct GNUNET_MessageHeader)) - { + { #if DEBUG_CLIENT - 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST"); #endif @@ -662,8 +657,8 @@ write_test (void *cls, size_t size, void *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); } @@ -683,9 +678,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; @@ -704,50 +699,49 @@ 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 ((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)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath, + sizeof (s_un.sun_path)); + } + else { - 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); + 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 @@ -758,124 +752,121 @@ 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) - { - 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)) - { - 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; + } } @@ -889,7 +880,8 @@ 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); /** @@ -900,58 +892,57 @@ static size_t 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 - 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 - 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; - } + th->reconnect_task = + GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); + return; + } 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; + } } @@ -974,47 +965,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 - 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 - 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 - 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); @@ -1043,21 +1034,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; @@ -1068,25 +1059,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) { - 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; - } + GNUNET_break (0); + GNUNET_free (th); + sock->th = NULL; + return NULL; } + } return th; } @@ -1097,20 +1088,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); } @@ -1136,21 +1127,20 @@ 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 - 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; } @@ -1178,14 +1168,12 @@ 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; @@ -1203,12 +1191,12 @@ GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection 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 31da5829b8..340ad53346 100644 --- a/src/util/common_allocation.c +++ b/src/util/common_allocation.c @@ -65,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) - { - LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc"); - abort (); - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc"); + abort (); + } return ret; } @@ -86,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; @@ -101,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) - { - 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]; @@ -172,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)) - { - 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 @@ -233,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; @@ -260,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; @@ -268,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_logging.c b/src/util/common_logging.c index 1d3843cfd3..0b7d06bab9 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c @@ -276,22 +276,21 @@ resize_logdefs () */ static void add_definition (char *component, char *file, char *function, int from_line, - int to_line, int level, int force) + int to_line, int level, int force) { if (logdefs_size == logdefs_len) resize_logdefs (); struct LogDef n; + memset (&n, 0, sizeof (n)); if (strlen (component) > 0 && component[0] != '*') n.component = GNUNET_strdup (component); if (strlen (file) > 0 && file[0] != '*') - { - n.file = GNUNET_strdup (file); - n.strlen_file = strlen (file); - } - if ( (NULL != function) && - (strlen (function) > 0) && - (function[0] != '*') ) + { + n.file = GNUNET_strdup (file); + n.strlen_file = strlen (file); + } + if ((NULL != function) && (strlen (function) > 0) && (function[0] != '*')) n.function = GNUNET_strdup (function); n.from_line = from_line; n.to_line = to_line; @@ -316,7 +315,7 @@ add_definition (char *component, char *file, char *function, int from_line, */ int GNUNET_get_log_call_status (int caller_level, const char *comp, - const char *file, const char *function, int line) + const char *file, const char *function, int line) { struct LogDef *ld; int i; @@ -337,20 +336,22 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, 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)) { - 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; - } + /* We're finished */ + return caller_level <= ld->level; } + } /* No matches - use global level, if defined */ if (min_level >= 0) return caller_level <= min_level; @@ -400,6 +401,7 @@ parse_definitions (const char *constname, int force) int from_line, to_line; int counter = 0; int keep_looking = 1; + tmp = getenv (constname); if (tmp == NULL) return 0; @@ -408,85 +410,84 @@ 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]) { - 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; - } + 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; } @@ -502,7 +503,7 @@ parse_all_definitions () 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; + parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; gnunet_force_log_parsed = GNUNET_YES; } #endif @@ -546,16 +547,15 @@ 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); @@ -596,11 +596,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; @@ -620,22 +620,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; + } } @@ -661,16 +661,15 @@ 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'; @@ -690,14 +689,14 @@ void GNUNET_log_skip (unsigned int n, int check_reset) { if (n == 0) - { - int ok; + { + int ok; - ok = (0 == skip_log); - skip_log = 0; - if (check_reset) - GNUNET_assert (ok); - } + ok = (0 == skip_log); + skip_log = 0; + if (check_reset) + GNUNET_assert (ok); + } else skip_log += n; } @@ -736,34 +735,33 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message, 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) - && (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); - return; - } + 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); + return; + } flush_bulk (date); strncpy (last_bulk, buf, sizeof (last_bulk)); last_bulk_repeat = 0; @@ -804,7 +802,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]; @@ -922,44 +920,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 "<invalid v4 address>"; - 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 "<invalid v4 address>"; - 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 "<unbound UNIX client>"; - 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]); + { + case AF_INET: + if (addrlen != sizeof (struct sockaddr_in)) + return "<invalid v4 address>"; + v4 = (const struct sockaddr_in *) addr; + inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); + if (0 == ntohs (v4->sin_port)) return buf; - default: - return _("invalid address"); - } + 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 "<invalid v4 address>"; + 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 "<unbound UNIX client>"; + 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 9a131fb503..41a4e1975a 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c @@ -150,7 +150,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]; @@ -167,89 +167,87 @@ 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"))) - { - 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)) { - 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; - } + /* @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 */ @@ -282,7 +280,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; @@ -296,67 +294,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"))) - { - 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)) { - 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; + 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; + } if (error != 0) 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; } @@ -370,23 +368,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) { - epos = spos->entries; - while (epos != NULL) - { - iter (iter_cls, spos->name, epos->key, epos->val); - epos = epos->next; - } - spos = spos->next; + iter (iter_cls, spos->name, epos->key, epos->val); + epos = epos->next; } + spos = spos->next; + } } @@ -400,10 +398,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; @@ -417,10 +415,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; + } } @@ -432,21 +430,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); + } } /** @@ -457,7 +455,7 @@ GNUNET_CONFIGURATION_iterate_sections (const struct */ void GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *section) + const char *section) { struct ConfigSection *spos; struct ConfigSection *prev; @@ -466,28 +464,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 (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; + 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; + } } @@ -502,7 +500,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; @@ -535,8 +533,7 @@ 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; @@ -557,7 +554,7 @@ findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, */ 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; @@ -584,7 +581,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; @@ -605,9 +602,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; @@ -630,28 +627,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); @@ -669,10 +666,9 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle * @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]; @@ -691,11 +687,10 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle * @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; @@ -719,9 +714,9 @@ GNUNET_CONFIGURATION_get_value_number (const struct */ 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; @@ -729,8 +724,7 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle if (e == NULL) return GNUNET_SYSERR; - return GNUNET_STRINGS_fancy_time_to_relative (e->val, - time); + return GNUNET_STRINGS_fancy_time_to_relative (e->val, time); } @@ -754,8 +748,7 @@ GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle e = findEntry (cfg, section, option); if (e == NULL) return GNUNET_SYSERR; - return GNUNET_STRINGS_fancy_size_to_bytes (e->val, - size); + return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size); } @@ -770,19 +763,18 @@ GNUNET_CONFIGURATION_get_value_size (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; } @@ -801,12 +793,10 @@ GNUNET_CONFIGURATION_get_value_string (const struct * @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; @@ -816,19 +806,19 @@ GNUNET_CONFIGURATION_get_value_choice (const struct 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) - { - 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; } @@ -842,9 +832,8 @@ GNUNET_CONFIGURATION_get_value_choice (const struct * @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; @@ -865,7 +854,7 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle */ char * GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle - *cfg, char *orig) + *cfg, char *orig) { int i; char *prefix; @@ -879,25 +868,24 @@ 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)) + GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix)) + { + if (NULL == (env = getenv (&orig[1]))) { - if (NULL == (env = getenv (&orig[1]))) - { - orig[i] = DIR_SEPARATOR; - return orig; - } - prefix = GNUNET_strdup (env); + orig[i] = DIR_SEPARATOR; + return orig; } + prefix = GNUNET_strdup (env); + } result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2); strcpy (result, prefix); if ((strlen (prefix) == 0) || @@ -922,18 +910,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); @@ -954,15 +942,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]) @@ -983,11 +971,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; @@ -1002,46 +990,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')) { - 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; + 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; + } GNUNET_free (list); return ret; } @@ -1065,21 +1053,21 @@ escape_name (const char *value) rpos = value; wpos = escaped; while (rpos[0] != '\0') + { + switch (rpos[0]) { - switch (rpos[0]) - { - case '\\': - case ' ': - wpos[0] = '\\'; - wpos[1] = rpos[0]; - wpos += 2; - break; - default: - wpos[0] = rpos[0]; - wpos++; - } - rpos++; + case '\\': + case ' ': + wpos[0] = '\\'; + wpos[1] = rpos[0]; + wpos += 2; + break; + default: + wpos[0] = rpos[0]; + wpos++; } + rpos++; + } return escaped; } @@ -1114,9 +1102,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; @@ -1124,9 +1112,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 (""); @@ -1158,9 +1146,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; @@ -1174,55 +1162,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)) { - 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') + 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; + } GNUNET_free (list); GNUNET_free (match); return GNUNET_NO; @@ -1237,8 +1225,7 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle * @return GNUNET_OK on success */ static int -parse_configuration_file (void *cls, - const char *filename) +parse_configuration_file (void *cls, const char *filename) { struct GNUNET_CONFIGURATION_Handle *cfg = cls; int ret; @@ -1258,7 +1245,7 @@ parse_configuration_file (void *cls, */ int GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, - const char *filename) + const char *filename) { char *baseconfig; char *ipath; @@ -1267,29 +1254,26 @@ GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, if (ipath == NULL) return GNUNET_SYSERR; baseconfig = NULL; - GNUNET_asprintf (&baseconfig, "%s%s", ipath, - "config.d"); + GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d"); GNUNET_free (ipath); if (GNUNET_SYSERR == - GNUNET_DISK_directory_scan (baseconfig, - &parse_configuration_file, - cfg)) + GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg)) { GNUNET_free (baseconfig); - return GNUNET_SYSERR; /* no configuration at all found */ + return GNUNET_SYSERR; /* no configuration at all found */ } - GNUNET_free (baseconfig); - if ( (filename != NULL) && - (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)) ) + GNUNET_free (baseconfig); + if ((filename != NULL) && + (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename))) { /* specified configuration not found */ return GNUNET_SYSERR; } 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 cd0035a8f3..61c26673be 100644 --- a/src/util/connection.c +++ b/src/util/connection.c @@ -333,8 +333,7 @@ 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; @@ -357,8 +356,8 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle */ 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]; @@ -379,94 +378,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) - { - 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) - 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); @@ -490,7 +489,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; @@ -523,8 +522,7 @@ 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; @@ -533,78 +531,77 @@ destroy_continuation (void *cls, sock->destroy_task = GNUNET_SCHEDULER_NO_TASK; GNUNET_assert (sock->dns_active == NULL); if (0 != (sock->ccs & COCO_TRANSMIT_READY)) - { + { #if DEBUG_CONNECTION - 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 - 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 - 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)) - LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown"); - } - } - if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) + if (sock->persist != GNUNET_YES) { - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); - sock->destroy_task = - GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation, - sock); - return; + 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; + } #if DEBUG_CONNECTION 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); @@ -639,9 +636,8 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) { #if DEBUG_CONNECTION LOG ((0 != - strncmp (h->hostname, "localhost:", - 10)) ? GNUNET_ERROR_TYPE_INFO : - GNUNET_ERROR_TYPE_WARNING, + 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); @@ -654,39 +650,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 - 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 - 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 - 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); + } } @@ -704,43 +700,42 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h) #endif /* trigger jobs that waited for the connection */ if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) - { + { #if DEBUG_CONNECTION - 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 - 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 - 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); + } } @@ -753,7 +748,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; @@ -768,15 +763,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); @@ -786,12 +781,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); } @@ -806,27 +801,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 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); + "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]; @@ -835,25 +830,24 @@ 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 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), GNUNET_a2s (ap->addr, ap->addrlen), h); @@ -861,33 +855,32 @@ try_connect_using_address (void *cls, const struct sockaddr *addr, if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && (errno != EINPROGRESS)) - { - /* maybe refused / unsupported address, try next */ - LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); + { + /* maybe refused / unsupported address, try next */ + LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); #if 0 - 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); } @@ -902,13 +895,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; @@ -916,9 +909,9 @@ GNUNET_CONNECTION_create_from_connect (const struct 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; } @@ -934,15 +927,15 @@ GNUNET_CONNECTION_create_from_connect (const struct */ 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); @@ -967,20 +960,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 @@ -1001,8 +994,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; @@ -1010,22 +1003,20 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family, s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); if (s == NULL) - { - 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 */ - 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; - } + { + /* 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); @@ -1050,7 +1041,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; } @@ -1070,23 +1061,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) { - 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; + GNUNET_SCHEDULER_cancel (sock->write_task); + sock->write_task = GNUNET_SCHEDULER_NO_TASK; + sock->write_buffer_off = 0; } + 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); @@ -1102,8 +1093,8 @@ signal_timeout (struct GNUNET_CONNECTION_Handle *sh) GNUNET_CONNECTION_Receiver receiver; #if DEBUG_CONNECTION - 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; @@ -1141,62 +1132,61 @@ 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 - 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)) - 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 - 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 - 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 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); + "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; @@ -1222,32 +1212,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 - 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 - 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); } @@ -1266,31 +1256,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; } @@ -1303,7 +1293,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; } @@ -1321,15 +1311,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; } @@ -1360,18 +1350,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; @@ -1401,12 +1391,11 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; #if DEBUG_CONNECTION 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); + "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); @@ -1451,20 +1440,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); @@ -1494,109 +1483,107 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 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) && - (NULL != sock->sock) ) - goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ + { + if ((sock->ignore_shutdown == GNUNET_YES) && (NULL != sock->sock)) + goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ #if DEBUG_CONNECTION - 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); - } - return; - } - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) + notify = sock->nth.notify_ready; + if (NULL != notify) { -#if DEBUG_CONNECTION - 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; } + return; + } + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) + { +#if DEBUG_CONNECTION + 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; + } 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 - 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; - 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 - 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 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); + (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 @@ -1607,11 +1594,11 @@ SCHEDULE_WRITE: 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); } @@ -1630,17 +1617,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); @@ -1654,36 +1641,34 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle 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 - 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 - 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; } @@ -1695,28 +1680,28 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle */ 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 - 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) { - if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (th->sh->write_task); - 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 a33b619ffb..180aab4c3c 100644 --- a/src/util/container_bloomfilter.c +++ b/src/util/container_bloomfilter.c @@ -89,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; @@ -106,10 +106,10 @@ GNUNET_CONTAINER_bloomfilter_get_size (const struct */ 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); } @@ -181,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; @@ -197,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)); } @@ -229,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; @@ -238,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; @@ -250,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) { - if ((low > 0) && (low < 0xF)) - low--; - if (low == 0) - { - clearBit (bitArray, bitIdx); - } + clearBit (bitArray, bitIdx); } + } else + { + if ((high > 0) && (high < 0xF)) + high--; + if (high == 0) { - if ((high > 0) && (high < 0xF)) - high--; - if (high == 0) - { - clearBit (bitArray, bitIdx); - } + 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)); @@ -295,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) { - 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); + 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); + } GNUNET_free (buffer); return GNUNET_OK; } @@ -325,8 +325,8 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size) * @return GNUNET_YES to continue, GNUNET_NO to stop early */ typedef int (*BitIterator) (void *cls, - const struct GNUNET_CONTAINER_BloomFilter * bf, - unsigned int bit); + const struct GNUNET_CONTAINER_BloomFilter * bf, + unsigned int bit); /** @@ -340,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; @@ -351,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))) { - 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; - } + 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; + } + } } @@ -384,9 +384,8 @@ 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; @@ -404,9 +403,8 @@ incrementBitCallback (void *cls, * @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; @@ -425,15 +423,15 @@ decrementBitCallback (void *cls, */ 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; } @@ -451,7 +449,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; @@ -467,32 +465,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); @@ -501,27 +499,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) + { + 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++) { - 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 ((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 */ + } GNUNET_free (rbuff); return bf; } @@ -542,7 +540,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; @@ -553,19 +551,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) @@ -587,8 +585,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; @@ -645,7 +643,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; @@ -665,7 +663,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; @@ -685,7 +683,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; @@ -719,8 +717,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; @@ -750,7 +748,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; @@ -772,9 +770,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; @@ -783,7 +781,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 051b85a251..c34e220ce9 100644 --- a/src/util/container_heap.c +++ b/src/util/container_heap.c @@ -116,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); } @@ -198,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; } @@ -214,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; @@ -238,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); } @@ -269,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; } @@ -286,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->cost) + : (pos->cost <= node->cost)) + { + /* node is descendent of pos */ + pos->tree_size += (1 + node->tree_size); + if (pos->left_child == NULL) { - /* 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; + 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; + } /* 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); @@ -346,9 +346,8 @@ 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; @@ -384,27 +383,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; @@ -436,43 +435,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) { - 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; - } + 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 + 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); - } + heap->root = node->right_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->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); } + } node->parent = NULL; node->left_child = NULL; node->right_child = NULL; @@ -506,7 +505,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; } @@ -521,19 +520,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) @@ -543,7 +542,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 1ea77f63a5..c2ec01c042 100644 --- a/src/util/container_meta_data.c +++ b/src/util/container_meta_data.c @@ -162,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); } @@ -184,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)); } @@ -206,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; @@ -221,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) { - 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 ((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; + } return GNUNET_YES; } @@ -263,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; @@ -277,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->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; + 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; + } md->item_count++; i = GNUNET_malloc (sizeof (struct MetaItem)); i->type = type; @@ -310,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)) { - p = i->data; - while ((*p != '\0') && (p < i->data + data_len)) - { - if (*p == DIR_SEPARATOR) - *p = '/'; - else if (*p == '\\') - *p = '_'; - p++; - } + if (*p == DIR_SEPARATOR) + *p = '/'; + else if (*p == '\\') + *p = '_'; + p++; } + } invalidate_sbuf (md); return GNUNET_OK; } @@ -349,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; } @@ -370,7 +370,7 @@ merge_helper (void *cls, const char *plugin_name, */ 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); } @@ -388,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; @@ -397,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 ((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; + 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; + } return GNUNET_SYSERR; } @@ -427,20 +427,19 @@ 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, "<gnunet>", - 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); } @@ -454,9 +453,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; @@ -466,13 +465,13 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData 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; } @@ -489,7 +488,7 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData */ 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; @@ -497,13 +496,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; } @@ -521,8 +520,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; @@ -533,15 +532,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; } @@ -556,9 +555,8 @@ 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; @@ -568,19 +566,19 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct 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 ((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; + if (match == NULL) + match = pos; + else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) && + (pos->type == EXTRACTOR_METATYPE_THUMBNAIL)) + match = pos; } + pos = pos->next; + } if ((match == NULL) || (match->data_size == 0)) return 0; *thumb = GNUNET_malloc (match->data_size); @@ -597,7 +595,7 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct */ struct GNUNET_CONTAINER_MetaData * GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData - *md) + *md) { struct GNUNET_CONTAINER_MetaData *ret; struct MetaItem *pos; @@ -607,12 +605,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; } @@ -633,7 +631,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; @@ -648,14 +646,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) { - if (dlen < oldSize) - { - *result = tmp; - *newSize = dlen; - return GNUNET_YES; - } + *result = tmp; + *newSize = dlen; + return GNUNET_YES; } + } GNUNET_free (tmp); return GNUNET_NO; } @@ -756,10 +754,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; @@ -781,79 +779,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) { - /* 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 (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 ... */ + } 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; @@ -862,95 +860,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)) { - 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++; + /* 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++; + } GNUNET_free (ent); /* nothing fit, only write header! */ @@ -972,7 +970,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; @@ -981,8 +979,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; @@ -1009,14 +1007,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; + } } @@ -1058,121 +1056,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; } - - if (compressed) + data = + decompress ((const char *) &input[sizeof (struct MetaDataHeader)], + size - sizeof (struct MetaDataHeader), dataSize); + if (data == 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; + GNUNET_break_op (0); + return NULL; } + cdata = data; + } else + { + data = NULL; + cdata = (const char *) &input[sizeof (struct MetaDataHeader)]; + if (dataSize != size - sizeof (struct MetaDataHeader)) { - data = NULL; - cdata = (const char *) &input[sizeof (struct MetaDataHeader)]; - if (dataSize != size - sizeof (struct MetaDataHeader)) - { - GNUNET_break_op (0); - return NULL; - } + 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)) { - 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); + 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); + } GNUNET_free_non_null (data); return md; } diff --git a/src/util/container_multihashmap.c b/src/util/container_multihashmap.c index 60d4a43d52..7e53a6440e 100644 --- a/src/util/container_multihashmap.c +++ b/src/util/container_multihashmap.c @@ -103,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])) { - while (NULL != (e = map->map[i])) - { - map->map[i] = e->next; - GNUNET_free (e); - } + map->map[i] = e->next; + GNUNET_free (e); } + } GNUNET_free (map->map); GNUNET_free (map); } @@ -130,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; @@ -145,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; } @@ -163,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; } @@ -189,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; @@ -202,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 = 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 = e->next; + if (NULL != it) + { + kc = e->key; + if (GNUNET_OK != it (it_cls, &kc, e->value)) + return GNUNET_SYSERR; + } + count++; } + } return count; } @@ -232,9 +232,8 @@ 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; @@ -244,21 +243,21 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap p = NULL; e = map->map[i]; while (e != NULL) + { + if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && + (value == e->value)) { - 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; + 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; + } return GNUNET_NO; } @@ -273,7 +272,7 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap */ 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; @@ -285,27 +284,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 (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++; - } + if (p == NULL) + map->map[i] = e->next; else - { - p = e; - e = e->next; - } + p->next = e->next; + GNUNET_free (e); + map->size--; + if (p == NULL) + e = map->map[i]; + else + e = p->next; + ret++; + } + else + { + p = e; + e = e->next; } + } return ret; } @@ -321,18 +320,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; } @@ -349,21 +348,20 @@ 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; } @@ -391,15 +389,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])) { - 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; - } + old_map[i] = e->next; + idx = idx_of (map, &e->key); + e->next = new_map[idx]; + new_map[idx] = e; } + } GNUNET_free (old_map); } @@ -418,9 +416,8 @@ 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; @@ -428,25 +425,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) { - 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 (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; @@ -469,10 +466,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; @@ -481,14 +478,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 2704713d41..7b85dc8776 100644 --- a/src/util/container_slist.c +++ b/src/util/container_slist.c @@ -89,21 +89,21 @@ struct GNUNET_CONTAINER_SList */ 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; @@ -119,8 +119,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; @@ -141,8 +141,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; @@ -163,7 +163,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; @@ -171,15 +171,14 @@ 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); } @@ -236,13 +235,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; @@ -258,7 +257,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; @@ -315,8 +314,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; @@ -369,7 +368,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; @@ -381,8 +380,7 @@ 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) { } diff --git a/src/util/crypto_aes.c b/src/util/crypto_aes.c index 9e194094ce..8b031f3677 100644 --- a/src/util/crypto_aes.c +++ b/src/util/crypto_aes.c @@ -39,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)); } /** @@ -51,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; @@ -76,29 +76,28 @@ 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); @@ -118,32 +117,30 @@ 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; } @@ -158,8 +155,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; @@ -177,14 +174,12 @@ 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 5920ddc938..698d8d450c 100644 --- a/src/util/crypto_crc.c +++ b/src/util/crypto_crc.c @@ -66,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 b8c9a80826..2ab6824941 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c @@ -119,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 */ } @@ -149,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)) - { - 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); } @@ -178,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; } @@ -260,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) @@ -278,23 +278,22 @@ 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) { - 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; + 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); GNUNET_assert (vbit == 0); result->encoding[wpos] = '\0'; @@ -318,22 +317,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) { - 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; - } + ((unsigned char *) result)[--wpos] = (unsigned char) bits; + bits >>= 8; + vbit -= 8; } + } GNUNET_assert (rpos == 0); GNUNET_assert (vbit == 0); return GNUNET_OK; @@ -352,7 +351,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; @@ -362,7 +361,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; @@ -372,37 +371,33 @@ 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]; } @@ -412,18 +407,17 @@ 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)); } @@ -454,13 +448,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; } @@ -472,8 +466,7 @@ 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; @@ -481,14 +474,13 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, 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; } @@ -500,22 +492,22 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, */ 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; } @@ -530,8 +522,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; @@ -551,12 +543,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); } @@ -570,14 +562,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 6af76ba681..40bfa6774d 100644 --- a/src/util/crypto_hkdf.c +++ b/src/util/crypto_hkdf.c @@ -74,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); @@ -94,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; @@ -115,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 @@ -137,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; @@ -158,16 +158,16 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_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); @@ -191,65 +191,65 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_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) { - if (t > 0) - { - memcpy (plain, result - k, k); - i++; - } - memset (plain + k + ctx_len, i, 1); - gcry_md_reset (prf); + 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 @@ -281,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 d8fcbd8665..0e7fbbbec1 100644 --- a/src/util/crypto_kdf.c +++ b/src/util/crypto_kdf.c @@ -44,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 @@ -59,9 +59,8 @@ 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); } /** @@ -77,14 +76,13 @@ 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 3d40ce27b5..60639a709e 100644 --- a/src/util/crypto_ksk.c +++ b/src/util/crypto_ksk.c @@ -51,12 +51,12 @@ 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; /** @@ -84,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; + { + 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 (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])); - } - hc = tmp; + gcry_mpi_add_ui (n, n, ntohl (((uint32_t *) & tmp)[j])); } + hc = tmp; + } GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), rnd); i = gcry_mpi_get_nbits (n); while (i > nbits) @@ -114,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; } @@ -164,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) { - 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_set_ui (x, 2); } - rc = 1; /* May be a prime. */ + 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. */ + } + } + rc = 1; /* May be a prime. */ leave: gcry_mpi_release (x); @@ -210,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); + } } @@ -326,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++) { - /* 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. */ + 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++) - { - 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; - } - } + { + 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; + } } + } } /** @@ -399,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; @@ -425,36 +425,36 @@ generate_kblock_key (KBlock_secret_key * sk, unsigned int nbits, f = gcry_mpi_new (0); do + { + 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); - } - 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 */ + 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 ((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); @@ -473,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 */ }; @@ -501,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; @@ -519,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); @@ -547,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; } @@ -572,123 +572,122 @@ 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) { - 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; - } + 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) { - 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; - } + 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)(u %m)))", - n, e, d, p, q, u); + 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 + 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); - } - else - { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, - d); - } + 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); @@ -703,10 +702,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; @@ -736,13 +735,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))) { - if (0 == memcmp (hc, &cache[i]->hc, sizeof (GNUNET_HashCode))) - { - ret = ksk_decode_key (cache[i]->pke); - return ret; - } + ret = ksk_decode_key (cache[i]->pke); + return ret; } + } line = GNUNET_malloc (sizeof (struct KBlockKeyCacheLine)); line->hc = *hc; @@ -758,10 +757,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 7fb2d14281..c74d60f4f8 100644 --- a/src/util/crypto_random.c +++ b/src/util/crypto_random.c @@ -65,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); - } - 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); + 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); + } return 0; } @@ -121,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; } @@ -146,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 { - 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); + 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); + } return 0; } @@ -200,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; @@ -209,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)) - 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; } - LOG (GNUNET_ERROR_TYPE_INFO, - _("Starting `%s' process to generate entropy\n"), "find"); + 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)) + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill"); + GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); + GNUNET_OS_process_close (genproc); + genproc = NULL; + } + 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); } @@ -249,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; + } } @@ -261,21 +261,20 @@ 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 7f0c816924..d29566ac8e 100644 --- a/src/util/crypto_rsa.c +++ b/src/util/crypto_rsa.c @@ -66,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 */ }; @@ -98,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); + } } /** @@ -115,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 @@ -140,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; @@ -148,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) { - 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 */ - } + 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; } @@ -197,10 +197,9 @@ 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; @@ -213,25 +212,24 @@ 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]); } @@ -245,7 +243,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; @@ -259,38 +257,36 @@ 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; @@ -316,10 +312,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); @@ -337,21 +333,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) { - 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; - } + 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; + } + } GNUNET_assert (size < 65536); retval = GNUNET_malloc (size); retval->len = htons (size); @@ -376,12 +372,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; } @@ -397,7 +393,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; @@ -412,122 +408,121 @@ 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) { - 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; - } + 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) { - 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; - } + 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)(u %m)))", - n, e, d, p, q, u); + 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 + 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); - } - else - { - rc = gcry_sexp_build (&res, &size, /* erroff */ - "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, - d); - } + 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); @@ -542,10 +537,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; @@ -582,172 +577,165 @@ 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) { - 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; + 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; + } /* 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) + { + 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)) { - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); + 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; } - cnt = 0; - while (1) + 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_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; + 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; + } 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)))) + { + LOG (GNUNET_ERROR_TYPE_ERROR, + _("File `%s' does not contain a valid private key. Deleting it.\n"), + filename); + if (0 != 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); - } + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename); } + } GNUNET_free (enc); if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, - sizeof (struct RsaPrivateKeyBinaryEncoded))) + 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); - 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; } @@ -764,9 +752,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; @@ -782,10 +770,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); @@ -795,11 +783,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; } @@ -815,8 +803,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; @@ -831,22 +819,21 @@ 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); @@ -867,8 +854,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; @@ -884,10 +871,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)); @@ -896,8 +883,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; @@ -915,11 +902,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; @@ -933,44 +920,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) - { - 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 12f985791b..2bdcf3262a 100644 --- a/src/util/disk.c +++ b/src/util/disk.c @@ -67,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?) @@ -165,25 +165,25 @@ getSizeRec (void *cls, const char *fn) #ifdef HAVE_STAT64 if (0 != STAT64 (fn, &buf)) - { - 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)) - { - 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; } @@ -215,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; @@ -232,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, @@ -261,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; @@ -292,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; @@ -327,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; @@ -363,35 +363,35 @@ 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) - { - 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)) LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn); return fn; @@ -412,10 +412,10 @@ GNUNET_DISK_get_blocks_available (const char *part) struct statvfs buf; if (0 != statvfs (part, &buf)) - { - 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; @@ -430,22 +430,21 @@ GNUNET_DISK_get_blocks_available (const char *part) GNUNET_free (path); szDrive[3] = 0; if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy)) - { - 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)) - { - 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 } @@ -469,21 +468,21 @@ GNUNET_DISK_directory_test (const char *fil) ret = STAT (fil, &filestat); if (ret != 0) + { + if (errno != ENOENT) { - if (errno != ENOENT) - { - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); - return GNUNET_SYSERR; - } - return GNUNET_NO; + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); + return GNUNET_SYSERR; } + return GNUNET_NO; + } if (!S_ISDIR (filestat.st_mode)) return GNUNET_NO; if (ACCESS (fil, R_OK | X_OK) < 0) - { - 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; } @@ -508,27 +507,27 @@ GNUNET_DISK_file_test (const char *fil) ret = STAT (rdir, &filestat); if (ret != 0) + { + if (errno != ENOENT) { - if (errno != ENOENT) - { - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); - GNUNET_free (rdir); - return GNUNET_SYSERR; - } + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); GNUNET_free (rdir); - return GNUNET_NO; + return GNUNET_SYSERR; } + 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) - { - 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; } @@ -553,56 +552,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]) { - pos = 2; - while (rdir[pos]) - { - if (rdir[pos] == '\\') - { - pos++; - break; - } - 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)) { - 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) - { + 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)) - { - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); - GNUNET_free (rdir); - return GNUNET_SYSERR; - } - } - rdir[pos] = DIR_SEPARATOR; - } - pos++; + 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++; + } GNUNET_free (rdir); return GNUNET_OK; } @@ -649,37 +648,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)) { - if (!ReadFile (h->h, result, len, &bytesRead, NULL)) - { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; - } + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; } + } else + { + if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead)) { - 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); + if (GetLastError () != ERROR_IO_PENDING) + { + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; + } } + GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); + } return bytesRead; #else return read (h->fd, result, len); @@ -701,8 +700,7 @@ 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); @@ -721,46 +719,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)) { - if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) - { - SetErrnoFromWinError (GetLastError ()); - return GNUNET_SYSERR; - } + SetErrnoFromWinError (GetLastError ()); + return GNUNET_SYSERR; } + } else - { + { #if DEBUG_PIPE - 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 - 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 - 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); @@ -779,15 +777,14 @@ 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); @@ -808,8 +805,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; @@ -827,61 +824,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)) - { - 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)) - { - 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)) - { - 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 ((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++; + 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++; + } closedir (dinfo); GNUNET_free (name); GNUNET_free (dname); @@ -933,7 +930,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; @@ -958,35 +955,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; } @@ -1004,9 +1001,9 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator */ 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; @@ -1015,11 +1012,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); @@ -1056,7 +1053,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; @@ -1065,18 +1062,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))) - { - 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)) - { - 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; } @@ -1102,34 +1099,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); @@ -1154,17 +1151,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; - idx++; + if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || + c == '<' || c == '>' || c == '|') + { + *idx = '_'; } + + idx++; + } } @@ -1184,12 +1181,12 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user) pws = getpwnam (user); if (pws == NULL) - { - 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)) LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename); #endif @@ -1207,13 +1204,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; @@ -1232,13 +1229,12 @@ 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 @@ -1254,13 +1250,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; @@ -1279,10 +1275,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 @@ -1303,7 +1299,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; @@ -1324,17 +1320,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) @@ -1342,22 +1338,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)) - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn); - else - 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; @@ -1370,48 +1366,47 @@ 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 ()); + 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, "open", expfn); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn); + CloseHandle (h); 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)); @@ -1435,28 +1430,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 ()); - 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) - { - 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; @@ -1479,7 +1474,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; @@ -1488,45 +1483,44 @@ 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) - { - 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); @@ -1575,56 +1569,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 @@ -1639,10 +1633,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 @@ -1659,20 +1653,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 @@ -1690,10 +1684,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; @@ -1722,8 +1716,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; @@ -1740,103 +1734,103 @@ 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 - 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 - 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 - 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 - 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 - 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 - 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); + 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 (); - 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 */ + 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 */ + } #if DEBUG_PIPE 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 - 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); #endif @@ -1863,7 +1857,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]; @@ -1875,13 +1869,13 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) ret = pipe (fd); if (ret == -1) - { - eno = errno; - 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; @@ -1905,62 +1899,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; - 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; @@ -1989,51 +1983,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)) { - if (!CloseHandle (p->fd[0]->h)) - { - SetErrnoFromWinError (GetLastError ()); - ret = GNUNET_SYSERR; - } - p->fd[0]->h = INVALID_HANDLE_VALUE; + SetErrnoFromWinError (GetLastError ()); + ret = GNUNET_SYSERR; } + p->fd[0]->h = INVALID_HANDLE_VALUE; + } else if (end == GNUNET_DISK_PIPE_END_WRITE) + { + if (!CloseHandle (p->fd[1]->h)) { - if (!CloseHandle (p->fd[1]->h)) - { - SetErrnoFromWinError (GetLastError ()); - ret = GNUNET_SYSERR; - } - p->fd[1]->h = INVALID_HANDLE_VALUE; + SetErrnoFromWinError (GetLastError ()); + ret = GNUNET_SYSERR; } + 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)) { - if (0 != close (p->fd[0]->fd)) - { - ret = GNUNET_SYSERR; - save = errno; - } - p->fd[0]->fd = -1; + ret = GNUNET_SYSERR; + save = errno; } + p->fd[0]->fd = -1; + } else if (end == GNUNET_DISK_PIPE_END_WRITE) + { + if (0 != close (p->fd[1]->fd)) { - if (0 != close (p->fd[1]->fd)) - { - ret = GNUNET_SYSERR; - save = errno; - } - p->fd[1]->fd = -1; + ret = GNUNET_SYSERR; + save = errno; } + p->fd[1]->fd = -1; + } #endif errno = save; return ret; @@ -2053,35 +2047,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)) { - if (0 != close (p->fd[0]->fd)) - { - ret = GNUNET_SYSERR; - save = errno; - } + ret = GNUNET_SYSERR; + save = errno; } + } if (p->fd[1]->fd != -1) + { + if (0 != close (p->fd[1]->fd)) { - if (0 != close (p->fd[1]->fd)) - { - ret = GNUNET_SYSERR; - save = errno; - } + ret = GNUNET_SYSERR; + save = errno; } + } #endif GNUNET_free (p); errno = save; @@ -2098,7 +2092,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; @@ -2118,71 +2112,70 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags, openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE; while (h == NULL) - { - DWORD error_code; + { + DWORD error_code; - name = NULL; - if (*fn != NULL) - { - GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn); + name = NULL; + if (*fn != NULL) + { + GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn); #if DEBUG_NPIPE - 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 - 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 - 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 - 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 - 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)); @@ -2198,22 +2191,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) { - 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); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "mkdtemp"); + return NULL; } + 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); @@ -2231,7 +2224,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; @@ -2247,12 +2240,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; @@ -2284,10 +2277,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 @@ -2303,17 +2296,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; + } } @@ -2327,7 +2320,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 4a36678b29..317bc07482 100644 --- a/src/util/getopt.c +++ b/src/util/getopt.c @@ -198,19 +198,20 @@ 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; } @@ -222,7 +223,8 @@ 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__ */ @@ -256,7 +258,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). */ @@ -287,12 +289,13 @@ 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; @@ -309,61 +312,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); + { + /* 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; - } + 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) { - 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; - } + /* 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. */ @@ -374,13 +377,14 @@ 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,50 +399,48 @@ _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 (posixly_correct == NULL && argc == original_argc && argv == original_argv) + { + if (nonoption_flags_max_len == 0) { - 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; + 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; + } else nonoption_flags_len = 0; #endif @@ -504,8 +506,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; @@ -513,12 +515,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 @@ -533,81 +535,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) { - /* 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 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] == '-')); + } + /* Decode the current option-ARGV-element. */ /* Check whether the ARGV-element is a long option. @@ -626,138 +628,134 @@ 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. */ ; + (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; + } - /* 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 (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; + } - 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 '?'; - } + /* 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. */ @@ -770,193 +768,191 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring, ++GNoptind; if (temp == NULL || c == ':') + { + if (GNopterr) { - 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 '?'; + 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 '?'; + } /* 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') { - 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, _("\ + 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 = NULL; - return 'W'; /* Let the application handle it. */ + 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. */ + } if (temp[1] == ':') + { + if (temp[2] == ':') { - 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; - } + /* 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; + } + } 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); } @@ -975,8 +971,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; @@ -1001,15 +997,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; @@ -1018,31 +1014,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) { - 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; - } + 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 8564d591f1..8b49c5907a 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c @@ -42,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; @@ -66,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; @@ -81,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) { - 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++; + 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++; + } 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; } @@ -168,10 +168,9 @@ GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext * @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; @@ -196,7 +195,7 @@ GNUNET_GETOPT_increment_value (struct */ 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; @@ -220,9 +219,8 @@ 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; @@ -247,18 +245,16 @@ GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext * @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; } @@ -278,16 +274,15 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext */ 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 992480a30e..207b9518a3 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 e97793114b..131693dd1a 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 c15f0d3f54..5d4c5c224a 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)))) + ((cache->salen != salen) || (0 != memcmp (cache->sa, sa, salen)))) + { + if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value < + 60 * 60 * 1000) { - 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; + 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; + } if (cache != NULL) + { + cache->last_request = now; + if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value < + 60 * 60 * 1000) { - 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 (); + GNUNET_free_non_null (cache->addr); 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,37 +254,36 @@ 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; } @@ -293,7 +292,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; @@ -302,48 +301,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 @@ -351,24 +350,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; @@ -377,7 +376,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 @@ -391,8 +390,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; @@ -412,7 +411,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client, 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); } @@ -426,7 +425,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client, */ 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; @@ -438,78 +437,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') { - /* IP from hostname */ - hostname = (const char *) &msg[1]; - if (hostname[size - 1] != '\0') - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + 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); + } } @@ -546,18 +545,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 1df1abc7c4..e978a950d3 100644 --- a/src/util/load.c +++ b/src/util/load.c @@ -94,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; + } } @@ -142,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; @@ -177,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) @@ -243,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 3fc71e6660..180adc1641 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -90,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 ()); - 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 @@ -103,10 +103,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock) if (flags == -1) - { - 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; @@ -114,10 +114,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock) flags |= O_NONBLOCK; if (0 != fcntl (fd->fd, F_SETFL, flags)) - { - LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); - return GNUNET_SYSERR; - } + { + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); + return GNUNET_SYSERR; + } return GNUNET_OK; #endif } @@ -162,7 +162,7 @@ socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h) if (0 != setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, - sizeof (abs_value))) + sizeof (abs_value))) LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); } #endif @@ -181,15 +181,14 @@ 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))) + 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))) + sizeof (abs_value))) LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); #endif } @@ -205,8 +204,7 @@ 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; @@ -219,7 +217,7 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, if (gsn == 0) LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n", - GNUNET_a2s (&name, namelen)); + GNUNET_a2s (&name, namelen)); } #endif ret->fd = accept (desc->fd, address, address_len); @@ -228,36 +226,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)) LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "socket_set_inheritable"); + "socket_set_inheritable"); #endif #ifdef DARWIN socket_set_nosigpipe (ret); @@ -279,8 +277,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; @@ -289,8 +287,7 @@ 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))) + if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on))) LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); #endif #endif @@ -302,11 +299,11 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, #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); @@ -341,8 +338,8 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc) DWORD error = 0; #if DEBUG_NETWORK - LOG (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); @@ -350,8 +347,8 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc) SetErrnoFromWinsockError (error); #if DEBUG_NETWORK LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", - "Closed 0x%x, closesocket() returned %d, GLE is %u\n", - desc->fd, ret, error); + "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret, + error); #endif #else ret = close (desc->fd); @@ -359,12 +356,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)) - 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); @@ -388,7 +385,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; @@ -406,8 +403,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; @@ -415,11 +412,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; } @@ -437,8 +434,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; @@ -463,7 +460,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; @@ -486,7 +483,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; @@ -519,9 +516,8 @@ 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; @@ -549,7 +545,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; @@ -578,7 +574,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; @@ -617,9 +613,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; @@ -652,8 +648,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; @@ -685,36 +681,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)) LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "socket_set_inheritable"); + "socket_set_inheritable"); #endif #ifdef DARWIN socket_set_nosigpipe (ret); @@ -723,7 +719,7 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol) #ifdef AF_UNIX && (domain != AF_UNIX) #endif - ) + ) socket_set_nodelay (ret); return ret; } @@ -762,18 +758,19 @@ int GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc) { int ret = 0; + #if WINDOWS int value = 0; if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value, - sizeof (value)))) + sizeof (value)))) LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value, - sizeof (value)))) + sizeof (value)))) LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); #elif LINUX int value = 0; @@ -812,7 +809,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) @@ -828,7 +825,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); } @@ -841,18 +838,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 @@ -867,7 +864,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; @@ -901,7 +898,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; @@ -932,7 +929,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; @@ -947,12 +944,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; @@ -974,13 +971,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 @@ -995,7 +992,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; @@ -1004,11 +1001,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; @@ -1019,47 +1016,45 @@ 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++) { - for (j = 0; j < fds2->sds.fd_count; j++) - { - if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j]) - return GNUNET_YES; - } + 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 - 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))) - { + 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 - LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); #endif - return GNUNET_YES; - } - GNUNET_CONTAINER_slist_next (&it); + return GNUNET_YES; } + GNUNET_CONTAINER_slist_next (&it); + } #endif return GNUNET_NO; } @@ -1106,9 +1101,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; @@ -1138,8 +1133,7 @@ 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; @@ -1153,68 +1147,67 @@ 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_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); - } + 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 - ) - { - 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)) @@ -1225,10 +1218,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) @@ -1264,194 +1257,184 @@ 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_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_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 - 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 - 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 - 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)); + } } + } if (wfds && write_handles) - { + { #if DEBUG_NETWORK - 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_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_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++; + } + } } + } if (nfds > 0) + { + if (rfds) { - if (rfds) - { #if DEBUG_NETWORK - 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 - 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 - 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 - 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; @@ -1464,7 +1447,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, if (nhandles) returncode = - WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total); + WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total); #if DEBUG_NETWORK LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", returncode); @@ -1481,239 +1464,230 @@ 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) { - 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; + 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 - 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 - LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); #endif - } + } #if DEBUG_NETWORK - 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 (); + 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 - 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 - 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 - 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 - 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)) 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++) { - struct GNUNET_CONTAINER_SList_Iterator t; + 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; - 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); - } - } + fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t, + NULL); + if (fh->type == GNUNET_PIPE) + { + CancelIo (fh->h); + } + } #if DEBUG_NETWORK - 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++) { - for (i = 0; i < wfds->sds.fd_count; i++) - { - WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0); - nsock++; - } + WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0); + nsock++; + } #if DEBUG_NETWORK - 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++) { - for (i = 0; i < efds->sds.fd_count; i++) - { - WSAEventSelect (efds->sds.fd_array[i], hEventException, 0); - nsock++; - } + WSAEventSelect (efds->sds.fd_array[i], hEventException, 0); + nsock++; + } #if DEBUG_NETWORK - 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++) { - struct GNUNET_CONTAINER_SList_Iterator t; + 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; - 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); - } + 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++) { - 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 (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++) { - 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"); - } + 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"); } + } LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode); #endif if (nhandles && (returnedpos < nhandles)) diff --git a/src/util/os_installation.c b/src/util/os_installation.c index 98080a3edc..a7a6ae915c 100644 --- a/src/util/os_installation.c +++ b/src/util/os_installation.c @@ -60,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")))) { - 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); - } + lgu[0] = '\0'; + fclose (f); + return GNUNET_strdup (dir); } + } fclose (f); return NULL; } @@ -87,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) - { - 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); } @@ -138,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; @@ -150,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--; @@ -172,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) { - if (_dyld_get_image_header (i) == &_mh_dylib_header) - { - path = _dyld_get_image_name (i); - if (path != NULL && strlen (path) > 0) - { - p = GNUNET_strdup (path); - s = p + strlen (p); - while ((s > p) && (*s != '/')) - s--; - s++; - *s = '\0'; - } - break; - } + path = _dyld_get_image_name (i); + if (path != NULL && strlen (path) > 0) + { + p = GNUNET_strdup (path); + s = p + strlen (p); + while ((s > p) && (*s != '/')) + s--; + s++; + *s = '\0'; + } + break; } + } return p; } #endif @@ -211,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) - { - 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) + { + *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; + } GNUNET_free (buf); GNUNET_free (path); return NULL; @@ -359,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'; @@ -372,62 +372,59 @@ 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) { - if (dirkind != GNUNET_OS_IPK_LIBDIR) - { - /* strip '/lib32' or '/lib64' */ - execpath[n - 5] = '\0'; - n -= 5; - } - else - isbasedir = 0; + /* strip '/lib32' or '/lib64' */ + execpath[n - 5] = '\0'; + n -= 5; } + 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) - 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: + { + 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 "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; - break; - default: - GNUNET_free (execpath); - return NULL; - } + 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; + } tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); sprintf (tmp, "%s%s", execpath, dirname); GNUNET_free (execpath); @@ -460,59 +457,58 @@ 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) - { - 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)) - { - 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 (); + { + 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 */ - } + 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 2835e61938..70d143ba1b 100644 --- a/src/util/os_network.c +++ b/src/util/os_network.c @@ -43,7 +43,7 @@ */ void GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, - void *proc_cls) + void *proc_cls) { #ifdef MINGW PMIB_IFTABLE pTable; @@ -52,121 +52,118 @@ 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++) { - 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; + 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; - } - } - GlobalFree (pAddrTable); - GlobalFree (pTable); + 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); + } return; @@ -177,29 +174,28 @@ 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) { - 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); + 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); + } #else char line[1024]; const char *start; @@ -220,76 +216,73 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, else f = popen ("ifconfig -a 2> /dev/null", "r"); if (!f) - { - 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))) { - 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; + /* 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 4f21e7edb4..a7ce0c5e5d 100644 --- a/src/util/os_priority.c +++ b/src/util/os_priority.c @@ -59,43 +59,41 @@ 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 - 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)) { + 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 - 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); } + } } @@ -104,35 +102,34 @@ 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)) - { - 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) - { - 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 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); } @@ -166,90 +163,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) { - if (errno == ECOMM) - { - /* Child process is not controllable via pipe */ + /* Child process is not controllable via pipe */ #if DEBUG_OS - 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 - 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 - 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 - 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; - } - } -#endif + 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 @@ -324,7 +321,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; @@ -334,51 +331,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 @@ -393,27 +390,26 @@ 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)) { - 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; - } + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "nice"); + return GNUNET_SYSERR; } + } else + { + if (0 != setpriority (PRIO_PROCESS, pid, rprio)) { - if (0 != setpriority (PRIO_PROCESS, pid, rprio)) - { - LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, - "setpriority"); - return GNUNET_SYSERR; - } + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, + "setpriority"); + return GNUNET_SYSERR; } + } #else #if DEBUG_OS LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, @@ -440,84 +436,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++) { - 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; + 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; + } 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; - if (index[c] != 1) - n_found += strlen (var) + strlen (val) + 1; + var_len = strlen (var); + if (strncmp (var, ptr, var_len) == 0) + { + found = 1; + break; + } } - result = GNUNET_malloc (tablesize + n_found + 1); - for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;) + if (!found) { - 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; + 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; + } 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) { - 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; - } + 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; @@ -537,8 +533,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; @@ -559,9 +555,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 @@ -569,38 +565,35 @@ 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 (); @@ -608,46 +601,44 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, ret = fork (); #endif if (ret != 0) + { + if (ret == -1) { - if (ret == -1) - { - LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); + 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); @@ -655,21 +646,21 @@ 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)) - 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)) - 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); LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); _exit (1); @@ -705,8 +696,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)); @@ -729,39 +720,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 ()); - 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)); @@ -771,37 +762,36 @@ 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 - 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); @@ -814,13 +804,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 ()); - 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); @@ -854,8 +844,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; @@ -878,7 +868,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; @@ -900,9 +890,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 @@ -910,52 +900,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) { - if (ret == -1) - { - LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); + 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); @@ -963,47 +953,47 @@ 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]) { - /* 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); + 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_array_grow (lscp, ls, 0); execvp (filename, argv); LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); @@ -1041,8 +1031,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)); @@ -1054,11 +1044,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) @@ -1070,26 +1060,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 ()); - 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)); @@ -1098,33 +1088,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]); @@ -1134,19 +1124,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 - 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); @@ -1159,13 +1149,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 ()); - 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); @@ -1194,8 +1184,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; @@ -1204,48 +1194,48 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, GNUNET_assert (0 != proc); ret = waitpid (proc->pid, &status, WNOHANG); if (ret < 0) - { - 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) - { - 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; @@ -1253,11 +1243,11 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, h = proc->handle; ret = proc->pid; if (h == NULL || ret == 0) - { - 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 (); @@ -1265,17 +1255,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); - 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 @@ -1305,19 +1295,19 @@ GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc) h = proc->handle; if (NULL == h) - { - 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; @@ -1391,10 +1381,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); @@ -1418,43 +1408,42 @@ 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))) { - 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; + cmd->buf[cmd->off] = '\0'; + cmd->proc (cmd->proc_cls, cmd->buf); } + 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); } @@ -1471,8 +1460,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; @@ -1486,10 +1475,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); @@ -1498,8 +1487,7 @@ 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 c88e03c153..2444cb9f8b 100644 --- a/src/util/peer.c +++ b/src/util/peer.c @@ -107,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; @@ -121,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; @@ -146,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; } @@ -160,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; @@ -168,25 +168,24 @@ 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) { - 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; - } + 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; } + } } @@ -206,16 +205,15 @@ 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; + } } @@ -229,11 +227,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 746b33ecaa..d883776b48 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 dffc8ecd61..fe5fb87f98 100644 --- a/src/util/plugin.c +++ b/src/util/plugin.c @@ -85,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) { - 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); - } + GNUNET_asprintf (&cpath, "%s:%s", opath, path); + lt_dlsetsearchpath (cpath); + GNUNET_free (path); + GNUNET_free (cpath); } + else + { + lt_dlsetsearchpath (path); + GNUNET_free (path); + } + } } @@ -120,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 (); } @@ -143,8 +143,8 @@ resolve_function (struct PluginList *plug, const char *name) mptr = lt_dlsym (plug->handle, initName); if (mptr == NULL) LOG (GNUNET_ERROR_TYPE_ERROR, - _("`%s' failed to resolve method '%s' with error: %s\n"), - "lt_dlsym", &initName[1], lt_dlerror ()); + _("`%s' failed to resolve method '%s' with error: %s\n"), "lt_dlsym", + &initName[1], lt_dlerror ()); GNUNET_free (initName); return mptr; } @@ -166,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; @@ -177,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; } @@ -208,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) - { - 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); @@ -227,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; } @@ -257,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; @@ -276,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; } @@ -294,8 +294,7 @@ struct LoadAllContext static int -find_libraries (void *cls, - const char *filename) +find_libraries (void *cls, const char *filename) { struct LoadAllContext *lac = cls; const char *slashpos; @@ -309,14 +308,10 @@ find_libraries (void *cls, while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR))) libname = slashpos + 1; n = strlen (libname); - if (0 != strncmp (lac->basename, - libname, - strlen (lac->basename))) - return GNUNET_OK; /* wrong name */ - if ( (n > 3) && - (0 == strcmp (&libname[n-3], - ".la")) ) - return GNUNET_OK; /* .la file */ + if (0 != strncmp (lac->basename, libname, strlen (lac->basename))) + return GNUNET_OK; /* wrong name */ + if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la"))) + return GNUNET_OK; /* .la file */ basename = GNUNET_strdup (libname); if (NULL != (dot = strstr (basename, "."))) *dot = '\0'; @@ -340,11 +335,9 @@ find_libraries (void *cls, * @param cb function to call for each plugin found * @param cb_cls closure for 'cb' */ -void -GNUNET_PLUGIN_load_all (const char *basename, - void *arg, - GNUNET_PLUGIN_LoaderCallback cb, - void *cb_cls) +void +GNUNET_PLUGIN_load_all (const char *basename, void *arg, + GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls) { struct LoadAllContext lac; char *path; @@ -353,16 +346,14 @@ GNUNET_PLUGIN_load_all (const char *basename, if (path == NULL) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not determine plugin installation path.\n")); + _("Could not determine plugin installation path.\n")); return; } lac.basename = basename; lac.arg = arg; lac.cb = cb; lac.cb_cls = cb_cls; - GNUNET_DISK_directory_scan (path, - &find_libraries, - &lac); + GNUNET_DISK_directory_scan (path, &find_libraries, &lac); GNUNET_free (path); } diff --git a/src/util/program.c b/src/util/program.c index 5582f30c69..6a0e5a5553 100644 --- a/src/util/program.c +++ b/src/util/program.c @@ -129,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; @@ -159,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; + { + 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; + 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; + } memset (&cc, 0, sizeof (cc)); loglev = NULL; cc.task = task; @@ -193,28 +193,27 @@ 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); @@ -222,31 +221,30 @@ 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 b6e51af188..2a736bc464 100644 --- a/src/util/pseudonym.c +++ b/src/util/pseudonym.c @@ -82,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; + } } /** @@ -103,10 +103,9 @@ 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; @@ -126,7 +125,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; @@ -134,11 +133,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) @@ -160,17 +159,16 @@ 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); } @@ -184,9 +182,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; @@ -195,23 +193,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))) { - 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; - } + (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 @@ -230,9 +228,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; @@ -242,10 +240,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)) || @@ -253,30 +251,29 @@ 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)) - { - 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; } @@ -292,7 +289,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; @@ -310,25 +307,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) { - 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; - } + GNUNET_CONTAINER_meta_data_destroy (meta); + meta = NULL; } + } if (name == NULL) name = GNUNET_strdup (_("no-name")); GNUNET_CRYPTO_hash (name, strlen (name), &nh); @@ -339,31 +336,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))) - { - idx = i; - break; - } - i++; - len -= sizeof (GNUNET_HashCode); - } - if (idx == -1) + (sizeof (GNUNET_HashCode) == + GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode)))) + { + if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode))) { idx = i; - if (sizeof (GNUNET_HashCode) != - GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode))) - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn); + break; } + i++; + len -= sizeof (GNUNET_HashCode); + } + if (idx == -1) + { + 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); @@ -382,7 +379,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; @@ -408,24 +405,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; } @@ -475,15 +472,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); @@ -502,7 +499,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; @@ -531,7 +528,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; @@ -541,10 +538,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); @@ -564,8 +561,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; @@ -579,16 +576,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 3bdc40143d..03dc2ce89d 100644 --- a/src/util/resolver_api.c +++ b/src/util/resolver_api.c @@ -196,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"), @@ -249,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; + } } @@ -286,35 +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)) { - 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; + 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; + } return ret; } @@ -322,13 +322,15 @@ 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 (); /** @@ -350,122 +352,120 @@ 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) - 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; + { + /* 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; - } + { + 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') { - /* check if request was canceled */ + GNUNET_break (0); 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); - } + rh->name_callback (rh->cls, NULL); GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); GNUNET_free (rh); - process_requests (); + GNUNET_CLIENT_disconnect (client, GNUNET_NO); + client = NULL; + reconnect (); 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)); + 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)); - } + 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)) { - 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; - } + 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); + { + 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); - } -#endif - rh->addr_callback (rh->cls, sa, salen); - GNUNET_CLIENT_receive (client, &handle_response, rh, - GNUNET_TIME_absolute_get_remaining - (rh->timeout)); + 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)); + } } @@ -498,27 +498,26 @@ 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))) { - 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; + /* 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; + } 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); @@ -553,21 +552,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); } @@ -595,25 +594,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); @@ -624,15 +623,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; } @@ -648,7 +647,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 @@ -656,12 +655,11 @@ 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 (); } @@ -678,26 +676,26 @@ reconnect () return; GNUNET_assert (NULL == client); if (NULL != (rh = req_head)) + { + switch (rh->was_transmitted) { - 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; - } + 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", @@ -721,9 +719,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; @@ -734,10 +732,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; @@ -751,25 +749,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; } @@ -793,10 +791,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); } @@ -816,10 +814,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; @@ -833,24 +831,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; } @@ -868,21 +866,21 @@ GNUNET_RESOLVER_local_fqdn_get () char hostname[GNUNET_OS_get_hostname_max_length () + 1]; if (0 != gethostname (hostname, sizeof (hostname) - 1)) - { - LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "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); } @@ -898,18 +896,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)) - { - LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "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 @@ -929,19 +927,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 7c5077fab7..0c43744713 100644 --- a/src/util/scheduler.c +++ b/src/util/scheduler.c @@ -269,7 +269,7 @@ static void *scheduler_select_cls; */ void GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select, - void *new_select_cls) + void *new_select_cls) { scheduler_select = new_select; scheduler_select_cls = new_select_cls; @@ -288,7 +288,7 @@ check_priority (enum GNUNET_SCHEDULER_Priority p) if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT)) return p; GNUNET_assert (0); - return 0; /* make compiler happy */ + return 0; /* make compiler happy */ } @@ -310,37 +310,37 @@ is_pending (GNUNET_SCHEDULER_TaskIdentifier id) if (id < lowest_pending_id) return GNUNET_NO; - min = -1; /* maximum value */ + min = -1; /* maximum value */ pos = pending; while (pos != NULL) - { - if (pos->id == id) - return GNUNET_YES; - if (pos->id < min) - min = pos->id; - pos = pos->next; - } + { + if (pos->id == id) + return GNUNET_YES; + if (pos->id < min) + min = pos->id; + pos = pos->next; + } pos = pending_timeout; while (pos != NULL) + { + if (pos->id == id) + return GNUNET_YES; + if (pos->id < min) + min = pos->id; + pos = pos->next; + } + for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++) + { + pos = ready[p]; + while (pos != NULL) { if (pos->id == id) - return GNUNET_YES; + return GNUNET_YES; if (pos->id < min) - min = pos->id; + min = pos->id; pos = pos->next; } - for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++) - { - pos = ready[p]; - while (pos != NULL) - { - if (pos->id == id) - return GNUNET_YES; - if (pos->id < min) - min = pos->id; - pos = pos->next; - } - } + } lowest_pending_id = min; return GNUNET_NO; } @@ -355,7 +355,7 @@ is_pending (GNUNET_SCHEDULER_TaskIdentifier id) */ static void update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws, - struct GNUNET_TIME_Relative *timeout) + struct GNUNET_TIME_Relative *timeout) { struct Task *pos; struct GNUNET_TIME_Absolute now; @@ -364,40 +364,40 @@ update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws, now = GNUNET_TIME_absolute_get (); pos = pending_timeout; if (pos != NULL) - { - to = GNUNET_TIME_absolute_get_difference (now, pos->timeout); - if (timeout->rel_value > to.rel_value) - *timeout = to; - if (pos->reason != 0) - *timeout = GNUNET_TIME_UNIT_ZERO; - } + { + to = GNUNET_TIME_absolute_get_difference (now, pos->timeout); + if (timeout->rel_value > to.rel_value) + *timeout = to; + if (pos->reason != 0) + *timeout = GNUNET_TIME_UNIT_ZERO; + } pos = pending; while (pos != NULL) + { + if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) && + (GNUNET_YES == is_pending (pos->prereq_id))) { - if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) && - (GNUNET_YES == is_pending (pos->prereq_id))) - { - pos = pos->next; - continue; - } - if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value) - { - to = GNUNET_TIME_absolute_get_difference (now, pos->timeout); - if (timeout->rel_value > to.rel_value) - *timeout = to; - } - if (pos->read_fd != -1) - GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd); - if (pos->write_fd != -1) - GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd); - if (pos->read_set != NULL) - GNUNET_NETWORK_fdset_add (rs, pos->read_set); - if (pos->write_set != NULL) - GNUNET_NETWORK_fdset_add (ws, pos->write_set); - if (pos->reason != 0) - *timeout = GNUNET_TIME_UNIT_ZERO; pos = pos->next; + continue; + } + if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value) + { + to = GNUNET_TIME_absolute_get_difference (now, pos->timeout); + if (timeout->rel_value > to.rel_value) + *timeout = to; } + if (pos->read_fd != -1) + GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd); + if (pos->write_fd != -1) + GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd); + if (pos->read_set != NULL) + GNUNET_NETWORK_fdset_add (rs, pos->read_set); + if (pos->write_set != NULL) + GNUNET_NETWORK_fdset_add (ws, pos->write_set); + if (pos->reason != 0) + *timeout = GNUNET_TIME_UNIT_ZERO; + pos = pos->next; + } } @@ -412,17 +412,17 @@ update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws, */ static int set_overlaps (const struct GNUNET_NETWORK_FDSet *ready, - struct GNUNET_NETWORK_FDSet *want) + struct GNUNET_NETWORK_FDSet *want) { if ((NULL == want) || (NULL == ready)) return GNUNET_NO; if (GNUNET_NETWORK_fdset_overlap (ready, want)) - { - /* copy all over (yes, there maybe unrelated bits, - * but this should not hurt well-written clients) */ - GNUNET_NETWORK_fdset_copy (want, ready); - return GNUNET_YES; - } + { + /* copy all over (yes, there maybe unrelated bits, + * but this should not hurt well-written clients) */ + GNUNET_NETWORK_fdset_copy (want, ready); + return GNUNET_YES; + } return GNUNET_NO; } @@ -439,8 +439,8 @@ set_overlaps (const struct GNUNET_NETWORK_FDSet *ready, */ static int is_ready (struct Task *task, struct GNUNET_TIME_Absolute now, - const struct GNUNET_NETWORK_FDSet *rs, - const struct GNUNET_NETWORK_FDSet *ws) + const struct GNUNET_NETWORK_FDSet *rs, + const struct GNUNET_NETWORK_FDSet *ws) { enum GNUNET_SCHEDULER_Reason reason; @@ -449,25 +449,25 @@ is_ready (struct Task *task, struct GNUNET_TIME_Absolute now, reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) && (((task->read_fd != -1) && - (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) - || (set_overlaps (rs, task->read_set)))) + (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) || + (set_overlaps (rs, task->read_set)))) reason |= GNUNET_SCHEDULER_REASON_READ_READY; if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) && (((task->write_fd != -1) && - (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd))) + (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd))) || (set_overlaps (ws, task->write_set)))) reason |= GNUNET_SCHEDULER_REASON_WRITE_READY; if (reason == 0) - return GNUNET_NO; /* not ready */ + return GNUNET_NO; /* not ready */ if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK) + { + if (GNUNET_YES == is_pending (task->prereq_id)) { - if (GNUNET_YES == is_pending (task->prereq_id)) - { - task->reason = reason; - return GNUNET_NO; /* prereq waiting */ - } - reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE; + task->reason = reason; + return GNUNET_NO; /* prereq waiting */ } + reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE; + } task->reason = reason; return GNUNET_YES; } @@ -500,7 +500,7 @@ queue_ready_task (struct Task *task) */ static void check_ready (const struct GNUNET_NETWORK_FDSet *rs, - const struct GNUNET_NETWORK_FDSet *ws) + const struct GNUNET_NETWORK_FDSet *ws) { struct Task *pos; struct Task *prev; @@ -511,40 +511,39 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs, prev = NULL; pos = pending_timeout; while (pos != NULL) - { - next = pos->next; - if (now.abs_value >= pos->timeout.abs_value) - pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; - if (0 == pos->reason) - break; - pending_timeout = next; - if (pending_timeout_last == pos) - pending_timeout_last = NULL; - queue_ready_task (pos); - pos = next; - } + { + next = pos->next; + if (now.abs_value >= pos->timeout.abs_value) + pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; + if (0 == pos->reason) + break; + pending_timeout = next; + if (pending_timeout_last == pos) + pending_timeout_last = NULL; + queue_ready_task (pos); + pos = next; + } pos = pending; while (pos != NULL) - { + { #if DEBUG_TASKS - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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)) - { - if (prev == NULL) - pending = next; - else - prev->next = next; - queue_ready_task (pos); - pos = next; - continue; - } - prev = pos; + next = pos->next; + if (GNUNET_YES == is_ready (pos, now, rs, ws)) + { + if (prev == NULL) + pending = next; + else + prev->next = next; + queue_ready_task (pos); pos = next; + continue; } + prev = pos; + pos = next; + } } @@ -563,15 +562,26 @@ GNUNET_SCHEDULER_shutdown () pos = pending_timeout; while (pos != NULL) - { - pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; - /* we don't move the task into the ready queue yet; check_ready - * will do that later, possibly adding additional - * readiness-factors */ - pos = pos->next; - } + { + pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; + /* we don't move the task into the ready queue yet; check_ready + * will do that later, possibly adding additional + * readiness-factors */ + pos = pos->next; + } pos = pending; while (pos != NULL) + { + pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; + /* we don't move the task into the ready queue yet; check_ready + * will do that later, possibly adding additional + * readiness-factors */ + pos = pos->next; + } + for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++) + { + pos = ready[i]; + while (pos != NULL) { pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; /* we don't move the task into the ready queue yet; check_ready @@ -579,18 +589,7 @@ GNUNET_SCHEDULER_shutdown () * readiness-factors */ pos = pos->next; } - for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++) - { - pos = ready[i]; - while (pos != NULL) - { - pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; - /* we don't move the task into the ready queue yet; check_ready - * will do that later, possibly adding additional - * readiness-factors */ - pos = pos->next; - } - } + } } @@ -632,69 +631,68 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws) max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP; do + { + if (ready_count == 0) + return; + GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL); + /* yes, p>0 is correct, 0 is "KEEP" which should + * always be an empty queue (see assertion)! */ + for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--) { - if (ready_count == 0) - return; - GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL); - /* yes, p>0 is correct, 0 is "KEEP" which should - * always be an empty queue (see assertion)! */ - for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--) - { - pos = ready[p]; - if (pos != NULL) - break; - } - GNUNET_assert (pos != NULL); /* ready_count wrong? */ - ready[p] = pos->next; - ready_count--; - if (current_priority != pos->priority) - { - current_priority = pos->priority; - (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), - pos->priority); - } - current_lifeness = pos->lifeness; - active_task = pos; + pos = ready[p]; + if (pos != NULL) + break; + } + GNUNET_assert (pos != NULL); /* ready_count wrong? */ + ready[p] = pos->next; + ready_count--; + if (current_priority != pos->priority) + { + current_priority = pos->priority; + (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), + pos->priority); + } + current_lifeness = pos->lifeness; + active_task = pos; #if PROFILE_DELAYS - if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value > - DELAY_THRESHOLD.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); - } + if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value > + DELAY_THRESHOLD.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; - tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set; - if ((pos->read_fd != -1) && - (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY))) - GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd); - tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set; - if ((pos->write_fd != -1) && - (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))) - GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd); - if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) && - (pos->write_fd != -1) && - (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd))) - abort (); // added to ready in previous select loop! + tc.reason = pos->reason; + tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set; + if ((pos->read_fd != -1) && + (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY))) + GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd); + tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set; + if ((pos->write_fd != -1) && + (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))) + GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd); + if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) && + (pos->write_fd != -1) && + (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd))) + abort (); // added to ready in previous select loop! #if DEBUG_TASKS - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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); + pos->callback (pos->callback_cls, &tc); #if EXECINFO - int i; + int i; - for (i = 0; i < pos->num_backtrace_strings; i++) - LOG (GNUNET_ERROR_TYPE_ERROR, - "Task %llu trace %d: %s\n", pos->id, - i, pos->backtrace_strings[i]); + for (i = 0; i < pos->num_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); - tasks_run++; - } + active_task = NULL; + destroy_task (pos); + tasks_run++; + } while ((pending == NULL) || (p >= max_priority_added)); } @@ -720,11 +718,11 @@ static void sighandler_shutdown () { static char c; - int old_errno = errno; /* backup errno */ + int old_errno = errno; /* backup errno */ GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle - (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), - &c, sizeof (c)); + (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), + &c, sizeof (c)); errno = old_errno; } @@ -750,10 +748,10 @@ check_lifeness () if (t->lifeness == GNUNET_YES) return GNUNET_OK; if ((NULL != pending) || (NULL != pending_timeout)) - { - GNUNET_SCHEDULER_shutdown (); - return GNUNET_OK; - } + { + GNUNET_SCHEDULER_shutdown (); + return GNUNET_OK; + } return GNUNET_NO; } @@ -799,7 +797,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls) shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO); GNUNET_assert (shutdown_pipe_handle != NULL); pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, - GNUNET_DISK_PIPE_END_READ); + GNUNET_DISK_PIPE_END_READ); GNUNET_assert (pr != NULL); shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown); shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown); @@ -811,77 +809,76 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls) current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT; current_lifeness = GNUNET_YES; GNUNET_SCHEDULER_add_continuation (task, task_cls, - GNUNET_SCHEDULER_REASON_STARTUP); + GNUNET_SCHEDULER_REASON_STARTUP); #if ENABLE_WINDOWS_WORKAROUNDS - active_task = (void *) (long) -1; /* force passing of sanity check */ + active_task = (void *) (long) -1; /* force passing of sanity check */ GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO, - &GNUNET_OS_install_parent_control_handler, - NULL); + &GNUNET_OS_install_parent_control_handler, + NULL); active_task = NULL; #endif last_tr = 0; busy_wait_warning = 0; while (GNUNET_OK == check_lifeness ()) + { + GNUNET_NETWORK_fdset_zero (rs); + GNUNET_NETWORK_fdset_zero (ws); + timeout = GNUNET_TIME_UNIT_FOREVER_REL; + update_sets (rs, ws, &timeout); + GNUNET_NETWORK_fdset_handle_set (rs, pr); + if (ready_count > 0) { - GNUNET_NETWORK_fdset_zero (rs); - GNUNET_NETWORK_fdset_zero (ws); - timeout = GNUNET_TIME_UNIT_FOREVER_REL; - update_sets (rs, ws, &timeout); - GNUNET_NETWORK_fdset_handle_set (rs, pr); - if (ready_count > 0) - { - /* no blocking, more work already ready! */ - timeout = GNUNET_TIME_UNIT_ZERO; - } - if (NULL == scheduler_select) - ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout); - else - ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout); - if (ret == GNUNET_SYSERR) - { - if (errno == EINTR) - continue; + /* no blocking, more work already ready! */ + timeout = GNUNET_TIME_UNIT_ZERO; + } + if (NULL == scheduler_select) + ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout); + else + ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout); + if (ret == GNUNET_SYSERR) + { + if (errno == EINTR) + continue; - LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select"); + LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select"); #ifndef MINGW #if USE_LSOF - char lsof[512]; + char lsof[512]; - snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ()); - (void) close (1); - (void) dup2 (2, 1); - if (0 != system (lsof)) - LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system"); + snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ()); + (void) close (1); + (void) dup2 (2, 1); + if (0 != system (lsof)) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system"); #endif #endif - abort (); - break; - } - if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16)) - { - LOG (GNUNET_ERROR_TYPE_WARNING, - _("Looks like we're busy waiting...\n")); - sleep (1); /* mitigate */ - } - check_ready (rs, ws); - run_ready (rs, ws); - if (GNUNET_NETWORK_fdset_handle_isset (rs, pr)) - { - /* consume the signal */ - GNUNET_DISK_file_read (pr, &c, sizeof (c)); - /* mark all active tasks as ready due to shutdown */ - GNUNET_SCHEDULER_shutdown (); - } - if (last_tr == tasks_run) - { - busy_wait_warning++; - } - else - { - last_tr = tasks_run; - busy_wait_warning = 0; - } + abort (); + break; + } + if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n")); + sleep (1); /* mitigate */ + } + check_ready (rs, ws); + run_ready (rs, ws); + if (GNUNET_NETWORK_fdset_handle_isset (rs, pr)) + { + /* consume the signal */ + GNUNET_DISK_file_read (pr, &c, sizeof (c)); + /* mark all active tasks as ready due to shutdown */ + GNUNET_SCHEDULER_shutdown (); + } + if (last_tr == tasks_run) + { + busy_wait_warning++; + } + else + { + last_tr = tasks_run; + busy_wait_warning = 0; } + } GNUNET_SIGNAL_handler_uninstall (shc_int); GNUNET_SIGNAL_handler_uninstall (shc_term); #ifndef MINGW @@ -934,10 +931,10 @@ GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p) ret = 0; pos = ready[check_priority (p)]; while (pos != NULL) - { - pos = pos->next; - ret++; - } + { + pos = pos->next; + ret++; + } return ret; } @@ -963,77 +960,76 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task) prev = NULL; t = pending; while (t != NULL) + { + if (t->id == task) + break; + prev = t; + t = t->next; + } + if (t == NULL) + { + prev = NULL; + to = 1; + t = pending_timeout; + while (t != NULL) { if (t->id == task) - break; + break; prev = t; t = t->next; } - if (t == NULL) - { - prev = NULL; - to = 1; - t = pending_timeout; - while (t != NULL) - { - if (t->id == task) - break; - prev = t; - t = t->next; - } - if (pending_timeout_last == t) - pending_timeout_last = NULL; - } + if (pending_timeout_last == t) + pending_timeout_last = NULL; + } p = 0; while (t == NULL) + { + p++; + if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT) { - p++; - if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT) - { - LOG (GNUNET_ERROR_TYPE_ERROR, - _("Attempt to cancel dead task %llu!\n"), - (unsigned long long) task); - GNUNET_assert (0); - } - prev = NULL; - t = ready[p]; - while (t != NULL) - { - if (t->id == task) - { - ready_count--; - break; - } - prev = t; - t = t->next; - } + LOG (GNUNET_ERROR_TYPE_ERROR, _("Attempt to cancel dead task %llu!\n"), + (unsigned long long) task); + GNUNET_assert (0); } + prev = NULL; + t = ready[p]; + while (t != NULL) + { + if (t->id == task) + { + ready_count--; + break; + } + prev = t; + t = t->next; + } + } if (prev == NULL) + { + if (p == 0) { - if (p == 0) - { - if (to == 0) - { - pending = t->next; - } - else - { - pending_timeout = t->next; - } - } + if (to == 0) + { + pending = t->next; + } else - { - ready[p] = t->next; - } + { + pending_timeout = t->next; + } } - else + else { - prev->next = t->next; + ready[p] = t->next; } + } + else + { + prev->next = t->next; + } ret = t->callback_cls; #if DEBUG_TASKS - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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; @@ -1051,7 +1047,7 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task) */ void GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls, - enum GNUNET_SCHEDULER_Reason reason) + enum GNUNET_SCHEDULER_Reason reason) { struct Task *t; @@ -1061,12 +1057,12 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls, GNUNET_assert (NULL != task); GNUNET_assert ((active_task != NULL) || - (reason == GNUNET_SCHEDULER_REASON_STARTUP)); + (reason == GNUNET_SCHEDULER_REASON_STARTUP)); t = GNUNET_malloc (sizeof (struct Task)); #if EXECINFO t->num_backtrace_strings = backtrace (backtrace_array, 50); t->backtrace_strings = - backtrace_symbols (backtrace_array, t->num_backtrace_strings); + backtrace_symbols (backtrace_array, t->num_backtrace_strings); #endif t->read_fd = -1; t->write_fd = -1; @@ -1080,8 +1076,8 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls, t->priority = current_priority; t->lifeness = current_lifeness; #if DEBUG_TASKS - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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); } @@ -1106,12 +1102,11 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, - GNUNET_SCHEDULER_Task task, void *task_cls) + GNUNET_SCHEDULER_Task task, void *task_cls) { return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, - prerequisite_task, - GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, - task_cls); + prerequisite_task, GNUNET_TIME_UNIT_ZERO, + NULL, NULL, task, task_cls); } @@ -1126,12 +1121,11 @@ GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio, - GNUNET_SCHEDULER_Task task, - void *task_cls) + GNUNET_SCHEDULER_Task task, void *task_cls) { return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK, - GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, - task_cls); + GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, + task_cls); } @@ -1150,7 +1144,7 @@ GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay, - GNUNET_SCHEDULER_Task task, void *task_cls) + GNUNET_SCHEDULER_Task task, void *task_cls) { #if 1 /* new, optimized version */ @@ -1170,7 +1164,7 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay, #if EXECINFO t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH); t->backtrace_strings = - backtrace_symbols (backtrace_array, t->num_backtrace_strings); + backtrace_symbols (backtrace_array, t->num_backtrace_strings); #endif t->read_fd = -1; t->write_fd = -1; @@ -1185,24 +1179,24 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay, * appending to a long list of tasks with timeouts) */ prev = pending_timeout_last; if (prev != NULL) - { - if (prev->timeout.abs_value > t->timeout.abs_value) - prev = NULL; - else - pos = prev->next; /* heuristic success! */ - } + { + if (prev->timeout.abs_value > t->timeout.abs_value) + prev = NULL; + else + pos = prev->next; /* heuristic success! */ + } if (prev == NULL) - { - /* heuristic failed, do traversal of timeout list */ - pos = pending_timeout; - } + { + /* heuristic failed, do traversal of timeout list */ + pos = pending_timeout; + } while ((pos != NULL) && - ((pos->timeout.abs_value <= t->timeout.abs_value) || - (pos->reason != 0))) - { - prev = pos; - pos = pos->next; - } + ((pos->timeout.abs_value <= t->timeout.abs_value) || + (pos->reason != 0))) + { + prev = pos; + pos = pos->next; + } if (prev == NULL) pending_timeout = t; else @@ -1212,23 +1206,23 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay, pending_timeout_last = t; #if DEBUG_TASKS - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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++) - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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; #else /* unoptimized version */ return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, - GNUNET_SCHEDULER_NO_TASK, delay, NULL, - NULL, task, task_cls); + GNUNET_SCHEDULER_NO_TASK, delay, NULL, + NULL, task, task_cls); #endif } @@ -1247,9 +1241,9 @@ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls) { return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, - GNUNET_SCHEDULER_NO_TASK, - GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, - task_cls); + GNUNET_SCHEDULER_NO_TASK, + GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, + task_cls); } @@ -1269,16 +1263,16 @@ GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls) */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness, - GNUNET_SCHEDULER_Task task, - void *task_cls) + GNUNET_SCHEDULER_Task task, + void *task_cls) { GNUNET_SCHEDULER_TaskIdentifier ret; ret = - GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, - GNUNET_SCHEDULER_NO_TASK, - GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, - task_cls); + GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, + GNUNET_SCHEDULER_NO_TASK, + GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, + task_cls); GNUNET_assert (pending->id == ret); pending->lifeness = lifeness; return ret; @@ -1316,7 +1310,7 @@ GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness, #ifndef MINGW GNUNET_SCHEDULER_TaskIdentifier add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd, - GNUNET_SCHEDULER_Task task, void *task_cls) + GNUNET_SCHEDULER_Task task, void *task_cls) { struct Task *t; @@ -1332,45 +1326,41 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd, #if EXECINFO t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH); t->backtrace_strings = - backtrace_symbols (backtrace_array, t->num_backtrace_strings); + backtrace_symbols (backtrace_array, t->num_backtrace_strings); #endif #if DEBUG_FDS if (-1 != rfd) - { - int flags = fcntl (rfd, F_GETFD); + { + int flags = fcntl (rfd, F_GETFD); - if ((flags == -1) && (errno == EBADF)) - { - LOG (GNUNET_ERROR_TYPE_ERROR, - "Got invalid file descriptor %d!\n", rfd); + if ((flags == -1) && (errno == EBADF)) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd); #if EXECINFO - int i; + int i; - for (i = 0; i < t->num_backtrace_strings; i++) - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Trace: %s\n", t->backtrace_strings[i]); + for (i = 0; i < t->num_backtrace_strings; i++) + LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]); #endif - GNUNET_assert (0); - } + GNUNET_assert (0); } + } if (-1 != wfd) - { - int flags = fcntl (wfd, F_GETFD); + { + int flags = fcntl (wfd, F_GETFD); - if (flags == -1 && errno == EBADF) - { - LOG (GNUNET_ERROR_TYPE_ERROR, - "Got invalid file descriptor %d!\n", wfd); + if (flags == -1 && errno == EBADF) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd); #if EXECINFO - int i; + int i; - for (i = 0; i < t->num_backtrace_strings; i++) - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Trace: %s\n", t->backtrace_strings[i]); + for (i = 0; i < t->num_backtrace_strings; i++) + LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]); #endif - GNUNET_assert (0); - } + GNUNET_assert (0); } + } #endif t->read_fd = rfd; GNUNET_assert (wfd >= -1); @@ -1387,15 +1377,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 - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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++) - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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; } @@ -1421,8 +1411,8 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay, - struct GNUNET_NETWORK_Handle * rfd, - GNUNET_SCHEDULER_Task task, void *task_cls) + struct GNUNET_NETWORK_Handle *rfd, + GNUNET_SCHEDULER_Task task, void *task_cls) { #if MINGW struct GNUNET_NETWORK_FDSet *rs; @@ -1432,14 +1422,14 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay, rs = GNUNET_NETWORK_fdset_create (); GNUNET_NETWORK_fdset_set (rs, rfd); ret = - GNUNET_SCHEDULER_add_select (check_priority (current_priority), - GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL, - task, task_cls); + GNUNET_SCHEDULER_add_select (check_priority (current_priority), + GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL, + task, task_cls); GNUNET_NETWORK_fdset_destroy (rs); return ret; #else return add_without_sets (delay, GNUNET_NETWORK_get_fd (rfd), -1, task, - task_cls); + task_cls); #endif } @@ -1462,8 +1452,8 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay, - struct GNUNET_NETWORK_Handle * wfd, - GNUNET_SCHEDULER_Task task, void *task_cls) + struct GNUNET_NETWORK_Handle *wfd, + GNUNET_SCHEDULER_Task task, void *task_cls) { #if MINGW struct GNUNET_NETWORK_FDSet *ws; @@ -1473,15 +1463,15 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay, ws = GNUNET_NETWORK_fdset_create (); GNUNET_NETWORK_fdset_set (ws, wfd); ret = - GNUNET_SCHEDULER_add_select (check_priority (current_priority), - GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws, - task, task_cls); + GNUNET_SCHEDULER_add_select (check_priority (current_priority), + GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws, + task, task_cls); GNUNET_NETWORK_fdset_destroy (ws); return ret; #else GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0); return add_without_sets (delay, -1, GNUNET_NETWORK_get_fd (wfd), task, - task_cls); + task_cls); #endif } @@ -1504,8 +1494,8 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay, - const struct GNUNET_DISK_FileHandle * rfd, - GNUNET_SCHEDULER_Task task, void *task_cls) + const struct GNUNET_DISK_FileHandle *rfd, + GNUNET_SCHEDULER_Task task, void *task_cls) { #if MINGW struct GNUNET_NETWORK_FDSet *rs; @@ -1515,9 +1505,9 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay, rs = GNUNET_NETWORK_fdset_create (); GNUNET_NETWORK_fdset_handle_set (rs, rfd); ret = - GNUNET_SCHEDULER_add_select (check_priority (current_priority), - GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL, - task, task_cls); + GNUNET_SCHEDULER_add_select (check_priority (current_priority), + GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL, + task, task_cls); GNUNET_NETWORK_fdset_destroy (rs); return ret; #else @@ -1548,8 +1538,8 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay, - const struct GNUNET_DISK_FileHandle * wfd, - GNUNET_SCHEDULER_Task task, void *task_cls) + const struct GNUNET_DISK_FileHandle *wfd, + GNUNET_SCHEDULER_Task task, void *task_cls) { #if MINGW struct GNUNET_NETWORK_FDSet *ws; @@ -1559,9 +1549,9 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay, ws = GNUNET_NETWORK_fdset_create (); GNUNET_NETWORK_fdset_handle_set (ws, wfd); ret = - GNUNET_SCHEDULER_add_select (check_priority (current_priority), - GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws, - task, task_cls); + GNUNET_SCHEDULER_add_select (check_priority (current_priority), + GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws, + task, task_cls); GNUNET_NETWORK_fdset_destroy (ws); return ret; #else @@ -1610,12 +1600,11 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay, */ GNUNET_SCHEDULER_TaskIdentifier GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio, - GNUNET_SCHEDULER_TaskIdentifier - prerequisite_task, - struct GNUNET_TIME_Relative delay, - const struct GNUNET_NETWORK_FDSet * rs, - const struct GNUNET_NETWORK_FDSet * ws, - GNUNET_SCHEDULER_Task task, void *task_cls) + GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, + struct GNUNET_TIME_Relative delay, + const struct GNUNET_NETWORK_FDSet *rs, + const struct GNUNET_NETWORK_FDSet *ws, + GNUNET_SCHEDULER_Task task, void *task_cls) { struct Task *t; @@ -1631,20 +1620,20 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio, #if EXECINFO t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH); t->backtrace_strings = - backtrace_symbols (backtrace_array, t->num_backtrace_strings); + backtrace_symbols (backtrace_array, t->num_backtrace_strings); #endif t->read_fd = -1; t->write_fd = -1; if (rs != NULL) - { - t->read_set = GNUNET_NETWORK_fdset_create (); - GNUNET_NETWORK_fdset_copy (t->read_set, rs); - } + { + t->read_set = GNUNET_NETWORK_fdset_create (); + GNUNET_NETWORK_fdset_copy (t->read_set, rs); + } if (ws != NULL) - { - t->write_set = GNUNET_NETWORK_fdset_create (); - GNUNET_NETWORK_fdset_copy (t->write_set, ws); - } + { + t->write_set = GNUNET_NETWORK_fdset_create (); + GNUNET_NETWORK_fdset_copy (t->write_set, ws); + } t->id = ++last_id; #if PROFILE_DELAYS t->start_time = GNUNET_TIME_absolute_get (); @@ -1652,23 +1641,23 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio, t->prereq_id = prerequisite_task; t->timeout = GNUNET_TIME_relative_to_absolute (delay); t->priority = - check_priority ((prio == - GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : - prio); + check_priority ((prio == + GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : + prio); t->lifeness = current_lifeness; t->next = pending; pending = t; max_priority_added = GNUNET_MAX (max_priority_added, t->priority); #if DEBUG_TASKS - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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++) - LOG (GNUNET_ERROR_TYPE_DEBUG, - "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 f792e5a675..6f1b8cdb4a 100644 --- a/src/util/server.c +++ b/src/util/server.c @@ -264,8 +264,7 @@ 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; @@ -279,47 +278,45 @@ process_listen_socket (void *cls, 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])) { - 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) - { + sock = + GNUNET_CONNECTION_create_from_accept (server->access, + server->access_cls, + server->listen_sockets[i]); + if (sock != NULL) + { #if DEBUG_SERVER - 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); - } - } - i++; + 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++; + } /* 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); } @@ -340,87 +337,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) - { - 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) - 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)) - 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) { - 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; + /* 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; } - if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5)) + else { - LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen"); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - errno = 0; - return NULL; + 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; + } + if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5)) + { + 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) - 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; } @@ -440,10 +437,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; @@ -456,18 +453,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; } @@ -487,10 +484,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; @@ -500,32 +497,31 @@ 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 = - 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; - } + lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]); + if (lsocks[j] != NULL) + j++; + i++; } - else + if (j == 0) { + if (errno != 0) + 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); } @@ -545,32 +541,32 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s) 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); } @@ -590,8 +586,7 @@ 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; @@ -609,21 +604,20 @@ 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)) 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); + _ + ("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); } @@ -635,14 +629,13 @@ warn_no_receive_done (void *cls, * @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; + } } @@ -663,8 +656,8 @@ GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_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; @@ -684,50 +677,49 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, pos = server->handlers; found = GNUNET_NO; while (pos != NULL) + { + i = 0; + while (pos->handlers[i].callback != 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)) - { + 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 - 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++; - } - pos = pos->next; + 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; + } if (found == GNUNET_NO) - { - 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; } @@ -744,8 +736,7 @@ 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); /** @@ -765,44 +756,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)) + (GNUNET_YES != client->shutdown_now) && (0 == client->suspended)) + { + if (ret == GNUNET_OK) { - if (ret == GNUNET_OK) - { - client->receive_pending = GNUNET_YES; -#if DEBUG_SERVER - 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; - } + client->receive_pending = GNUNET_YES; #if DEBUG_SERVER LOG (GNUNET_ERROR_TYPE_DEBUG, - "Server processes additional messages instantly.\n"); + "Server re-enters receive loop, timeout: %llu.\n", + client->idle_timeout.rel_value); #endif - ret = - GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, - GNUNET_YES); + GNUNET_CONNECTION_receive (client->connection, + GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, + client->idle_timeout, &process_incoming, + client); + break; } #if DEBUG_SERVER + 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); + } +#if DEBUG_SERVER 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 - 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); @@ -821,7 +812,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; @@ -832,35 +823,34 @@ 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 - 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n", (unsigned int) available, GNUNET_a2s (addr, addrlen)); @@ -868,8 +858,8 @@ process_incoming (void *cls, const void *buf, size_t available, 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); } @@ -890,23 +880,21 @@ 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 - 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server continues processing messages still in the buffer.\n"); @@ -927,7 +915,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; @@ -961,14 +949,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 (); @@ -979,8 +967,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; } @@ -995,7 +983,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; } @@ -1043,7 +1031,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); } @@ -1061,8 +1049,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; @@ -1083,8 +1071,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; @@ -1092,17 +1080,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 @@ -1133,72 +1121,72 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client) "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; } - 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) + n = server->disconnect_notify_list; + while (n != 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; - } + n->callback (n->callback_cls, client); + n = n->next; } + } if (rc > 0) - { + { #if DEBUG_SERVER - 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 - 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); @@ -1262,17 +1250,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); } @@ -1309,47 +1297,46 @@ 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 - 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 - 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 - 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 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 6fd2647d79..dcb8c48102 100644 --- a/src/util/server_mst.c +++ b/src/util/server_mst.c @@ -91,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; @@ -121,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; @@ -140,148 +140,148 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, 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))) { - 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]; + /* 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 + 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); - 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; - } + { + 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; - } + { + /* 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); - mst->off += want; - if (mst->off == mst->pos) - { - /* reset to beginning of buffer, it's free right now! */ - mst->off = 0; - mst->pos = 0; - } + buf += want; + size -= want; } - while (size > 0) + else { -#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; - } + /* 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) { - 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; + 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; + } if (purge) mst->off = 0; #if DEBUG_SERVER_MST diff --git a/src/util/server_nc.c b/src/util/server_nc.c index cbe98a0142..a36fa0c505 100644 --- a/src/util/server_nc.c +++ b/src/util/server_nc.c @@ -156,19 +156,19 @@ 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 @@ -181,15 +181,15 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) 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); } @@ -205,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; @@ -225,27 +225,26 @@ GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle */ 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)) { - 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_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml); + GNUNET_free (pml); } + 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); } @@ -257,9 +256,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; @@ -293,45 +292,44 @@ 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 - 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 - 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 - 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; @@ -348,25 +346,24 @@ 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)) - { - 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); @@ -379,15 +376,15 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc, #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); } @@ -402,22 +399,21 @@ 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); } @@ -432,20 +428,19 @@ 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 020fd86db0..ce40db19c0 100644 --- a/src/util/server_tc.c +++ b/src/util/server_tc.c @@ -83,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 @@ -94,28 +94,27 @@ 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)) { - 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); - } + GNUNET_break (0); + GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); } + } return msize; } @@ -151,10 +150,9 @@ 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; @@ -181,10 +179,10 @@ GNUNET_SERVER_transmit_context_append_data (struct */ 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; @@ -208,18 +206,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); + } } @@ -237,7 +235,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 7cafe35c87..cf1002ca9e 100644 --- a/src/util/service.c +++ b/src/util/service.c @@ -104,121 +104,119 @@ 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) { - 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 */ + 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; } - if (pos < strlen (routeList)) + /* try second notation */ + cnt = + sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1], + &temps[2], &temps[3], &slash); + if (cnt == 5) { - LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), - &routeList[pos]); - GNUNET_free (result); - return NULL; /* oops */ + 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 */ + } } - return result; /* ok */ + /* 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 */ + } + if (pos < strlen (routeList)) + { + LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), + &routeList[pos]); + GNUNET_free (result); + return NULL; /* oops */ + } + return result; /* ok */ } @@ -260,85 +258,82 @@ parse_ipv6_specification (const char *routeListX) if (routeList[i] == ';') count++; if (routeList[len - 1] != ';') - { - 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) { - start = pos; - while (routeList[pos] != ';') - pos++; + memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); slash = pos; - while ((slash >= start) && (routeList[slash] != '/')) - slash--; - if (slash < start) - { - memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); - 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) - 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); + } + else + { + routeList[pos] = '\0'; + ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask); 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++; + { + 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++; + } GNUNET_free (routeList); return result; } @@ -352,8 +347,7 @@ 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; @@ -362,12 +356,12 @@ check_ipv4_listed (const struct IPv4NetworkSet *list, 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; } @@ -379,8 +373,7 @@ check_ipv4_listed (const struct IPv4NetworkSet *list, * @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; @@ -393,16 +386,16 @@ check_ipv6_listed (const struct IPv6NetworkSet *list, 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; } @@ -536,10 +529,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)); @@ -556,15 +549,14 @@ 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); } @@ -597,7 +589,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; @@ -605,51 +597,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 - 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: - 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) - { - 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; } @@ -666,7 +658,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; } @@ -676,26 +668,25 @@ 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))) - { - 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; } @@ -705,26 +696,25 @@ process_acl4 (struct IPv4NetworkSet **ret, * 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))) - { - 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; } @@ -739,7 +729,7 @@ process_acl6 (struct IPv6NetworkSet **ret, */ static void add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens, - const char *unixpath) + const char *unixpath) { #ifdef AF_UNIX struct sockaddr_un *un; @@ -791,9 +781,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; @@ -814,64 +804,61 @@ 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) { - /* 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; - } + 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_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; - } + 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; @@ -881,211 +868,210 @@ 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)) { - /* 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; - } + 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)) + { + 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 + 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)) { - LOG (GNUNET_ERROR_TYPE_ERROR, - _ - ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"), - serviceName); - GNUNET_free_non_null (hostname); + 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; } - if (port == 0) + next = res; + i = 0; + while (NULL != (pos = next)) { - saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *)); - saddrlens = GNUNET_malloc (2 * sizeof (socklen_t)); - add_unixpath (saddrs, saddrlens, unixpath); + 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); - GNUNET_free_non_null (hostname); - *addrs = saddrs; - *addr_lens = saddrlens; - return 1; + return GNUNET_SYSERR; } - - if (hostname != NULL) + 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++; + } + 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, - "Resolving `%s' since that is where `%s' will bind to.\n", - hostname, serviceName); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n", + serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen)); #endif - 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 (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++; + resi++; + i = 0; 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++; - } - 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)); + { + 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]; #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; + ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; + ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); } - else + else { - /* 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_in *) saddrs[i])->sin_len = saddrlens[i]; -#endif - ((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]); + /* 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_in6 *) saddrs[i])->sin6_len = saddrlens[0]; + ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0]; #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_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]; + ((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); - } + ((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; @@ -1124,37 +1110,36 @@ setup_service (struct GNUNET_SERVICE_Context *sctx) int flags; #endif - if (GNUNET_CONFIGURATION_have_value - (sctx->cfg, sctx->serviceName, "TIMEOUT")) + 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_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; + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Specified value for `%s' of service `%s' is invalid\n"), + "TIMEOUT", sctx->serviceName); + return GNUNET_SYSERR; } + 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"))) { - 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; - } + 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; @@ -1165,48 +1150,45 @@ 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--) { - 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"); + 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"); + } #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"); @@ -1228,7 +1210,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; } @@ -1246,7 +1228,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); @@ -1254,30 +1236,30 @@ 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)) - { - 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) - { - 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)) LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif); GNUNET_break (0 == fclose (pidfd)); @@ -1316,35 +1298,34 @@ 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) { - 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; + 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; + } 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; @@ -1352,23 +1333,22 @@ 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) { - 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++; - } + 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); } @@ -1385,45 +1365,44 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx) int filedes[2]; if (0 != PIPE (filedes)) - { - 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) - { - 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))) + LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read"); + fflush (stdout); + switch (c) { - /* 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 */ + 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 */ + } GNUNET_break (0 == CLOSE (0)); GNUNET_break (0 == CLOSE (1)); GNUNET_break (0 == CLOSE (filedes[0])); @@ -1432,11 +1411,11 @@ 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)) - { - 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 (); @@ -1461,36 +1440,35 @@ 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) - { - 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))) { - 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; - } + 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; @@ -1506,7 +1484,7 @@ 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)) LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif); GNUNET_free (pif); @@ -1528,8 +1506,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) @@ -1586,23 +1564,21 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, goto shutdown; #if DEBUG_SERVICE LOG (GNUNET_ERROR_TYPE_DEBUG, - "Service `%s' runs with configuration from `%s'\n", serviceName, - cfg_fn); + "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 - 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); @@ -1614,11 +1590,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)) - 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; @@ -1649,13 +1625,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; @@ -1663,24 +1639,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; @@ -1718,12 +1694,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 a986b62605..c3bb718bfb 100644 --- a/src/util/signal.c +++ b/src/util/signal.c @@ -63,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 @@ -72,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); + { + __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); - } + 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 d48d552a1f..dc4037a1ff 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -59,8 +59,7 @@ * (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; @@ -70,18 +69,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 *); + { + 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--; + slen = strlen (s) + 1; + if (buffer != NULL) + { + GNUNET_assert (needed + slen <= size); + memcpy (&buffer[needed], s, slen); } + needed += slen; + count--; + } va_end (ap); return needed; } @@ -102,7 +101,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; @@ -112,21 +111,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) { - 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--; + va_end (ap); + return 0; /* error */ } + *r = &buffer[start]; + needed++; /* skip 0-termination */ + count--; + } va_end (ap); return needed; } @@ -145,25 +144,25 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size) char *ret; if (size > 5 * 1024) + { + size = size / 1024; + unit = "KiB"; + if (size > 5 * 1024) { size = size / 1024; - unit = "KiB"; + unit = "MiB"; if (size > 5 * 1024) - { - size = size / 1024; - unit = "MiB"; - if (size > 5 * 1024) - { - size = size / 1024; - unit = "GiB"; - if (size > 5 * 1024) - { - size = size / 1024; - unit = "TiB"; - } - } - } + { + size = size / 1024; + unit = "GiB"; + if (size > 5 * 1024) + { + size = size / 1024; + unit = "TiB"; + } + } } + } ret = GNUNET_malloc (32); GNUNET_snprintf (ret, 32, "%llu %s", size, unit); return ret; @@ -179,26 +178,42 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size) */ int GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, - unsigned long long *size) + unsigned long long *size) { - struct { - const char *name; + struct + { + const char *name; unsigned long long value; - } table[] = { - { "B", 1 }, - { "KiB", 1024 }, - { "kB", 1000 }, - { "MiB", 1024 * 1024 }, - { "MB", 1000 * 1000 }, - { "GiB", 1024 * 1024 * 1024 }, - { "GB", 1000 * 1000 * 1000 }, - { "TiB", 1024LL * 1024LL * 1024LL * 1024LL }, - { "TB", 1000LL * 1000LL * 1000LL * 1024LL }, - { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL }, - { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL}, - { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, - { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL }, - { NULL, 0 } + } table[] = + { + { + "B", 1}, + { + "KiB", 1024}, + { + "kB", 1000}, + { + "MiB", 1024 * 1024}, + { + "MB", 1000 * 1000}, + { + "GiB", 1024 * 1024 * 1024}, + { + "GB", 1000 * 1000 * 1000}, + { + "TiB", 1024LL * 1024LL * 1024LL * 1024LL}, + { + "TB", 1000LL * 1000LL * 1000LL * 1024LL}, + { + "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, + { + "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL}, + { + "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, + { + "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL}, + { + NULL, 0} }; unsigned long long ret; char *in; @@ -211,9 +226,8 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, in = GNUNET_strdup (fancy_size); for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) { - i=0; - while ( (table[i].name != NULL) && - (0 != strcasecmp (table[i].name, tok) ) ) + i = 0; + while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok))) i++; if (table[i].name != NULL) last *= table[i].value; @@ -222,8 +236,8 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, ret += last; last = 0; if (1 != sscanf (tok, "%llu", &last)) - return GNUNET_SYSERR; /* expected number */ - } + return GNUNET_SYSERR; /* expected number */ + } } ret += last; *size = ret; @@ -241,43 +255,54 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, */ int GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size, - struct GNUNET_TIME_Relative *rtime) + struct GNUNET_TIME_Relative *rtime) { - struct { - const char *name; + struct + { + const char *name; unsigned long long value; - } table[] = { - { "ms", 1 }, - { "s", 1000 }, - { "\"", 1000 }, - { "min", 60 * 1000 }, - { "minutes", 60 * 1000 }, - { "'", 60 * 1000 }, - { "h", 60 * 60 * 1000 }, - { "d", 24 * 60 * 60 * 1000 }, - { "a", 31557600 /* year */ }, - { NULL, 0 } + } table[] = + { + { + "ms", 1}, + { + "s", 1000}, + { + "\"", 1000}, + { + "min", 60 * 1000}, + { + "minutes", 60 * 1000}, + { + "'", 60 * 1000}, + { + "h", 60 * 60 * 1000}, + { + "d", 24 * 60 * 60 * 1000}, + { + "a", 31557600 /* year */ }, + { + NULL, 0} }; unsigned long long ret; char *in; const char *tok; unsigned long long last; unsigned int i; - + if ((0 == strcasecmp (fancy_size, "infinity")) || (0 == strcasecmp (fancy_size, "forever"))) - { - *rtime = GNUNET_TIME_UNIT_FOREVER_REL; - return GNUNET_OK; - } + { + *rtime = GNUNET_TIME_UNIT_FOREVER_REL; + return GNUNET_OK; + } ret = 0; last = 0; in = GNUNET_strdup (fancy_size); for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) { - i=0; - while ( (table[i].name != NULL) && - (0 != strcasecmp (table[i].name, tok) ) ) + i = 0; + while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok))) i++; if (table[i].name != NULL) last *= table[i].value; @@ -286,8 +311,8 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size, ret += last; last = 0; if (1 != sscanf (tok, "%llu", &last)) - return GNUNET_SYSERR; /* expected number */ - } + return GNUNET_SYSERR; /* expected number */ + } } ret += last; rtime->rel_value = (uint64_t) ret; @@ -317,35 +342,35 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) cd = iconv_open ("UTF-8", charset); if (cd == (iconv_t) - 1) - { - 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; - } + { + 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) - { - 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'; @@ -392,89 +417,88 @@ GNUNET_STRINGS_filename_expand (const char *fil) /* absolute path, just copy */ return GNUNET_strdup (fil); if (fil[0] == '~') + { + fm = getenv ("HOME"); + if (fm == 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++; + 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++; + } else + { + /* relative path */ + fil_ptr = fil; + len = 512; + fm = NULL; + while (1) { - /* 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 */ + 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 */ + } 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); - 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)) { - 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; + 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; + } return fn; #endif @@ -497,25 +521,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 / 1000; - unit = _( /* time unit */ "s"); + dval = dval / 60; + unit = _( /* time unit */ "m"); if (dval > 5 * 60) - { - 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"); - } - } - } + { + 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 e72c95295e..df4becdd18 100644 --- a/src/util/test_bio.c +++ b/src/util/test_bio.c @@ -57,17 +57,16 @@ 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); @@ -94,8 +93,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)); @@ -122,8 +121,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)); @@ -149,8 +148,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); @@ -182,8 +181,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); @@ -204,8 +203,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); @@ -268,18 +267,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); @@ -306,8 +305,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); @@ -335,8 +334,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); @@ -365,8 +364,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 ad88874f26..bb7d5008ac 100644 --- a/src/util/test_client.c +++ b/src/util/test_client.c @@ -71,25 +71,24 @@ 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)); } @@ -105,13 +104,11 @@ 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); @@ -151,9 +148,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; @@ -161,15 +158,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)); } @@ -184,10 +181,9 @@ 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); @@ -201,11 +197,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 2fb1329b13..950f0cedbb 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 a940d3c07a..cf291161ed 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; @@ -44,9 +44,7 @@ main (int argc, char *argv[]) unsigned int logs = 0; if (0 != setenv ("GNUNET_FORCE_LOG", "", 1)) - fprintf (stderr, - "Failed to setenv: %s\n", - strerror (errno)); + fprintf (stderr, "Failed to setenv: %s\n", strerror (errno)); GNUNET_log_setup ("test-common-logging", "DEBUG", "/dev/null"); GNUNET_logger_add (&my_log, &logs); GNUNET_logger_add (&my_log, &logs); @@ -61,26 +59,23 @@ 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); @@ -88,17 +83,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 d87f56d420..005b5caef8 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 6485e4e4a4..5b89b72212 100644 --- a/src/util/test_common_logging_runtime_loglevels.c +++ b/src/util/test_common_logging_runtime_loglevels.c @@ -42,108 +42,110 @@ static struct GNUNET_DISK_PipeHandle *pipe_stdout; static GNUNET_SCHEDULER_TaskIdentifier die_task; -static void runone (); +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); + 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) { - if (phase < 9) - { - phase += 1; - runone (); - } - else - ok = 0; + phase += 1; + runone (); } + 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]; int i, j, stop = 0; + j = 0; int stage = 0; + if (!(phase >= phase_from1 && phase <= phase_to1) && !(phase >= phase_from2 && phase <= phase_to2)) return p; #if 0 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p); + "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p); #endif for (i = 0; i < *len && !stop; i++) + { + switch (stage) { - 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]; + 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]; @@ -162,20 +164,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); @@ -197,64 +199,65 @@ 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; - } + 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); @@ -268,52 +271,51 @@ 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="); @@ -321,20 +323,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 @@ -364,11 +366,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 9deb4fc35b..3780b36490 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"); + "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"); 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"); - 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; - } + "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 0 - 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; + 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; + } } @@ -162,41 +162,40 @@ 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++) { - 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; + 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; + } return diffsCB.cfgDiffs; } @@ -221,37 +220,35 @@ 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 0 @@ -276,65 +273,64 @@ 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); if (GNUNET_OK != @@ -365,10 +361,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; } @@ -381,66 +377,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"; @@ -448,16 +444,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; } @@ -473,11 +469,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; @@ -487,37 +483,36 @@ 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); @@ -525,11 +520,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); @@ -550,9 +545,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 1ac5b99cd3..92c0cea2ea 100644 --- a/src/util/test_connection.c +++ b/src/util/test_connection.c @@ -66,46 +66,44 @@ 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); + } } @@ -124,12 +122,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 @@ -137,7 +135,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"); @@ -160,14 +158,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); } @@ -183,7 +181,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; @@ -198,11 +196,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 c413c18c81..018cf61fb5 100644 --- a/src/util/test_connection_addressing.c +++ b/src/util/test_connection_addressing.c @@ -66,16 +66,15 @@ 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; } @@ -83,25 +82,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); + } } @@ -117,7 +116,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)); @@ -131,9 +130,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 @@ -160,17 +159,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); } @@ -197,11 +196,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 bd095fdda1..63829344d0 100644 --- a/src/util/test_connection_receive_cancel.c +++ b/src/util/test_connection_receive_cancel.c @@ -65,11 +65,10 @@ 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; } @@ -78,7 +77,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); } @@ -93,9 +92,8 @@ 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); } @@ -121,9 +119,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); } @@ -139,7 +137,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 5a0bb7c3af..1700718b9e 100644 --- a/src/util/test_connection_timeout.c +++ b/src/util/test_connection_timeout.c @@ -62,11 +62,10 @@ 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; } @@ -78,16 +77,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 @@ -95,9 +94,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; } @@ -112,9 +111,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)); } @@ -130,7 +129,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; @@ -143,11 +142,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 00b247c52b..b598907ec8 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 540d8c51d4..50308a2d08 100644 --- a/src/util/test_connection_transmit_cancel.c +++ b/src/util/test_connection_transmit_cancel.c @@ -43,8 +43,7 @@ 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; @@ -53,8 +52,8 @@ task_transmit_cancel (void *cls, 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); @@ -75,7 +74,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; @@ -89,11 +88,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 04bb348890..c14adb0755 100644 --- a/src/util/test_container_bloomfilter.c +++ b/src/util/test_container_bloomfilter.c @@ -75,30 +75,29 @@ 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); @@ -111,94 +110,93 @@ 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; @@ -212,30 +210,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 c1c4eb3608..a2a004aa79 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 66f90ff10c..fe1dd79c10 100644 --- a/src/util/test_container_meta_data.c +++ b/src/util/test_container_meta_data.c @@ -41,55 +41,51 @@ testMeta (int i) m = GNUNET_CONTAINER_meta_data_create (); if (GNUNET_OK != - GNUNET_CONTAINER_meta_data_insert (m, "<test>", - EXTRACTOR_METATYPE_TITLE, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", "TestTitle", - strlen ("TestTitle") + 1)) + GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", "TestTitle", + strlen ("TestTitle") + 1)) ABORT (m); if (GNUNET_OK != GNUNET_CONTAINER_meta_data_insert (m, "<test>", - 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, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ + if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ ABORT (m); - if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_AUTHOR_NAME, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ + if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", 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, "<test>", - 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, "<test>", + 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); @@ -97,27 +93,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)) { - 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); - } + ABORT (m); } + } if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) ABORT (m); GNUNET_CONTAINER_meta_data_destroy (m); @@ -135,22 +131,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, "<test>", - 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, "<test>", + 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; @@ -166,22 +162,22 @@ testMetaLink () m = GNUNET_CONTAINER_meta_data_create (); if (GNUNET_OK != GNUNET_CONTAINER_meta_data_insert (m, "<test>", - 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, "<test>", - 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); @@ -205,46 +201,44 @@ 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, "<test>", - EXTRACTOR_METATYPE_UNKNOWN, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", "TestTitle", - strlen ("TestTitle") + 1); - GNUNET_CONTAINER_meta_data_insert (meta2, "<test>", - 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, "<test>", + EXTRACTOR_METATYPE_UNKNOWN, + EXTRACTOR_METAFORMAT_UTF8, "text/plain", + "TestTitle", strlen ("TestTitle") + 1); + GNUNET_CONTAINER_meta_data_insert (meta2, "<test>", + 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); @@ -253,74 +247,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 5732ace932..ba621c17e9 100644 --- a/src/util/test_container_multihashmap.c +++ b/src/util/test_container_multihashmap.c @@ -52,41 +52,38 @@ 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 394dc4cdb5..1b63d3dbce 100644 --- a/src/util/test_container_slist.c +++ b/src/util/test_container_slist.c @@ -46,66 +46,64 @@ 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))) { - 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)); + GNUNET_CONTAINER_slist_iter_destroy (&it); + GNUNET_assert (0); } + j *= 2; + GNUNET_CONTAINER_slist_insert (&it, + GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, + &j, sizeof (j)); + } GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200); i = 198; GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i))); 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_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,35 +114,34 @@ 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); + { + p = GNUNET_CONTAINER_slist_get (&it, &s); - if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i))) - { - GNUNET_assert (0); - } + if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i))) + { + GNUNET_assert (0); } + } GNUNET_CONTAINER_slist_destroy (l); /*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC */ 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 e928b96a7a..971e9afbec 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 25931b6259..a0161b575c 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,45 +133,42 @@ 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; + } - 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); + 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); + + } + return number_of_weak_keys; } @@ -183,19 +180,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 { - 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); - } + 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 a32a28ef03..b0fa4f3325 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,9 +124,8 @@ 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 d5ee4d4333..7521161943 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,9 +162,8 @@ 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); } @@ -173,7 +172,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, @@ -182,7 +181,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, @@ -191,7 +190,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, @@ -200,7 +199,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, @@ -213,9 +212,8 @@ 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); } @@ -224,11 +222,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 @@ -238,8 +236,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); } @@ -248,7 +246,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, @@ -257,7 +255,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, @@ -277,7 +275,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, @@ -290,9 +288,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); } @@ -301,13 +299,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, @@ -317,7 +315,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]; @@ -325,9 +323,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 55d2dffcb4..2789f5edb2 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,29 +50,27 @@ 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++) + 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)) { - 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, " Failed! Wanted %.2s but got %2s at %d\n", &want[i * 2], + out, i); + return GNUNET_SYSERR; } + } fprintf (stderr, " OK\n"); return GNUNET_OK; } @@ -91,10 +89,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<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++) @@ -102,26 +100,26 @@ testMultiKey (const char *word) * printf("\n"); */ GNUNET_CRYPTO_rsa_key_free (hostkey); for (i = 0; i < UNIQUE_ITER; i++) + { + fprintf (stderr, "."); + hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); + if (hostkey == NULL) { - fprintf (stderr, "."); - hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); - if (hostkey == NULL) - { - GNUNET_break (0); - fprintf (stderr, " ERROR\n"); - return GNUNET_SYSERR; - } - GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1); - GNUNET_CRYPTO_rsa_key_free (hostkey); - if (0 != - memcmp (&pkey, &pkey1, - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) - { - GNUNET_break (0); - fprintf (stderr, " ERROR\n"); - return GNUNET_SYSERR; - } + GNUNET_break (0); + fprintf (stderr, " ERROR\n"); + return GNUNET_SYSERR; } + GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1); + GNUNET_CRYPTO_rsa_key_free (hostkey); + if (0 != + memcmp (&pkey, &pkey1, + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) + { + GNUNET_break (0); + fprintf (stderr, " ERROR\n"); + return GNUNET_SYSERR; + } + } fprintf (stderr, " OK\n"); return GNUNET_OK; } @@ -143,35 +141,35 @@ testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey) 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, "."); - 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, - MAX_TESTVAL, result); - ok++; - continue; - } + 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, + 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); if (ok == 0) return GNUNET_OK; else @@ -194,34 +192,34 @@ testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey) purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)); 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; + } + if (GNUNET_SYSERR != + GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, + &purp, &sig, &pkey)) { - 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; - } + 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); return ok; } @@ -239,10 +237,10 @@ main (int argc, char *argv[]) GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in); hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); if (hostkey == NULL) - { - printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n"); - return 1; - } + { + printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n"); + return 1; + } if (GNUNET_OK != testMultiKey ("foo")) failureCount++; if (GNUNET_OK != testMultiKey ("bar")) @@ -254,9 +252,9 @@ main (int argc, char *argv[]) GNUNET_CRYPTO_rsa_key_free (hostkey); 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; } diff --git a/src/util/test_crypto_random.c b/src/util/test_crypto_random.c index 3b86ececd9..46d3ed0aa0 100644 --- a/src/util/test_crypto_random.c +++ b/src/util/test_crypto_random.c @@ -39,19 +39,19 @@ test (enum GNUNET_CRYPTO_Quality mode) for (i = 0; i < 1024; i++) GNUNET_break (1024 > (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))) { - b2 = GNUNET_CRYPTO_random_permute (mode, 1024); - if (0 == memcmp (b2, buf, sizeof (buf))) - { - fprintf (stderr, "!"); - GNUNET_free (b2); - continue; - } + fprintf (stderr, "!"); GNUNET_free (b2); - break; + continue; } + 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 6a2c1b3553..f26a76a38f 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, "."); - 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, "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, "."); - 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, "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,39 +149,37 @@ 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))) { - 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 ("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; @@ -208,34 +206,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)) { - 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; - } + 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; + } + } 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; } @@ -260,18 +258,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, "."); - 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, "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; } @@ -328,9 +326,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 e8ec88c9fc..91154a6f5e 100644 --- a/src/util/test_disk.c +++ b/src/util/test_disk.c @@ -38,41 +38,40 @@ 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")); @@ -90,15 +89,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")); @@ -108,19 +107,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")); @@ -147,7 +146,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) @@ -157,7 +156,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; @@ -169,7 +168,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 @@ -203,14 +202,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; } @@ -221,10 +220,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; } @@ -276,9 +275,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 f830454cd4..a517887bff 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 28cc9cd3ab..315f97d97a 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 59f3ee8166..94e2719a2a 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 918ef1bc5d..df85205460 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 5bf5108bc6..3817d82437 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,27 +65,25 @@ 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)) { - pid = GNUNET_PEER_intern (&pidArr[i]); - if (pid != (i + 1)) - { - fprintf (stderr, - "Unexpected Peer ID returned by intern function\n"); - return 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)) { - pid = GNUNET_PEER_intern (&pidArr[i]); - if (pid != (i + 1)) - { - fprintf (stderr, - "Unexpected Peer ID returned by intern function\n"); - return 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); @@ -130,11 +128,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_plugin.c b/src/util/test_plugin.c index bfa3cf1108..4635e13576 100644 --- a/src/util/test_plugin.c +++ b/src/util/test_plugin.c @@ -27,9 +27,7 @@ #define VERBOSE GNUNET_EXTRA_LOGGING static void -test_cb (void *cls, - const char *libname, - void *lib_ret) +test_cb (void *cls, const char *libname, void *lib_ret) { void *ret; @@ -39,7 +37,7 @@ test_cb (void *cls, GNUNET_assert (NULL != ret); GNUNET_assert (0 == strcmp (ret, "World")); } - + static int check () diff --git a/src/util/test_program.c b/src/util/test_program.c index 0ff49b81ef..faeb4e7162 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 30a04598c7..20a3d3d96c 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, "<test>", 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, "<test>", - EXTRACTOR_METATYPE_COMMENT, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", m, - strlen (m) + 1)); + GNUNET_CONTAINER_meta_data_insert (meta, "<test>", + 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 24c71b41f9..4e248aa725 100644 --- a/src/util/test_resolver_api.c +++ b/src/util/test_resolver_api.c @@ -46,13 +46,12 @@ 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)); } @@ -64,21 +63,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); + } } @@ -90,19 +89,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 @@ -115,19 +114,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 @@ -139,32 +138,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); } @@ -181,21 +180,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 @@ -207,21 +206,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 @@ -231,7 +230,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; @@ -258,50 +257,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 @@ -312,41 +311,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 */ @@ -361,8 +360,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; @@ -373,12 +372,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); } @@ -392,33 +391,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; @@ -434,11 +433,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 e2a28c1b7a..788ba13fa6 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 20783da8d3..ff6966a87a 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 8adde5346b..6258290424 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,20 +98,19 @@ 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)); } @@ -139,7 +138,7 @@ static size_t transmit_initial_message (void *cls, size_t size, void *buf) { struct GNUNET_MessageHeader msg; - + GNUNET_assert (ok == 1); ok = 2; GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); @@ -147,12 +146,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); } @@ -181,18 +180,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 e38840d928..69207f81a7 100644 --- a/src/util/test_server_disconnect.c +++ b/src/util/test_server_disconnect.c @@ -81,14 +81,13 @@ 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); } @@ -139,18 +138,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 21c706b079..5b6e517183 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,8 +66,7 @@ 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; @@ -81,21 +80,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); + } } @@ -170,25 +169,24 @@ 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); } @@ -213,11 +211,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 11f7312433..4f80f188cc 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 33d69db100..1b78932f85 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,9 +136,8 @@ 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; } @@ -155,19 +154,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); } /** @@ -191,9 +190,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; } @@ -202,12 +201,11 @@ 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); @@ -236,9 +234,8 @@ 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; @@ -253,11 +250,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 (); @@ -266,22 +263,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)) { - 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)); + 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)); + } 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 bb0e6088f5..c6d6041447 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 80109615a0..c5232ff2a0 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,24 +152,23 @@ 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); @@ -183,51 +182,50 @@ 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 e48377fa30..3ee15516a1 100644 --- a/src/util/time.c +++ b/src/util/time.c @@ -55,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; } @@ -130,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; } @@ -148,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; } @@ -163,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; } @@ -179,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; } @@ -194,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; } @@ -230,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; @@ -271,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; } @@ -297,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; @@ -317,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; @@ -325,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; } @@ -342,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; @@ -365,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; @@ -393,17 +393,17 @@ GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, */ 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; } @@ -418,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 aa4757f4a9..252cacbfba 100644 --- a/src/util/winproc.c +++ b/src/util/winproc.c @@ -73,8 +73,7 @@ TSetNamedSecurityInfo GNSetNamedSecurityInfo; void plibc_panic (int err, char *msg) { - LOG (((err == - INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR), + LOG (((err == INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR), "%s", msg); } @@ -100,139 +99,137 @@ 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; } |