aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-10-11 09:43:04 +0000
committerChristian Grothoff <christian@grothoff.org>2011-10-11 09:43:04 +0000
commitd9d94d0e53d26af75ec8241383d166544ebd79f3 (patch)
tree9080b73624389403a198257fe0547bb4634e64d2 /src/util
parent2d792ee2e9cc0c993b8907e2c8edb0c2b8465343 (diff)
downloadgnunet-d9d94d0e53d26af75ec8241383d166544ebd79f3.tar.gz
gnunet-d9d94d0e53d26af75ec8241383d166544ebd79f3.zip
converting to GNUNET_LOG_from*
Diffstat (limited to 'src/util')
-rw-r--r--src/util/bandwidth.c184
-rw-r--r--src/util/bio.c255
-rw-r--r--src/util/client.c822
-rw-r--r--src/util/common_allocation.c60
-rw-r--r--src/util/common_endian.c2
-rw-r--r--src/util/common_logging.c405
-rw-r--r--src/util/configuration.c769
-rw-r--r--src/util/connection.c1086
-rw-r--r--src/util/container_bloomfilter.c263
-rw-r--r--src/util/container_heap.c202
-rw-r--r--src/util/container_meta_data.c794
-rw-r--r--src/util/container_multihashmap.c237
-rw-r--r--src/util/container_slist.c74
-rw-r--r--src/util/crypto_aes.c79
-rw-r--r--src/util/crypto_crc.c14
-rw-r--r--src/util/crypto_hash.c217
-rw-r--r--src/util/crypto_hkdf.c120
-rw-r--r--src/util/crypto_kdf.c16
-rw-r--r--src/util/crypto_ksk.c517
-rw-r--r--src/util/crypto_random.c203
-rw-r--r--src/util/crypto_rsa.c765
-rw-r--r--src/util/disk.c1454
-rw-r--r--src/util/getopt.c1022
-rw-r--r--src/util/getopt_helpers.c173
-rw-r--r--src/util/gnunet-config-diff.c8
-rw-r--r--src/util/gnunet-resolver.c6
-rw-r--r--src/util/gnunet-service-resolver.c411
-rw-r--r--src/util/load.c48
-rw-r--r--src/util/network.c1137
-rw-r--r--src/util/os_installation.c289
-rw-r--r--src/util/os_network.c369
-rw-r--r--src/util/os_priority.c1132
-rw-r--r--src/util/peer.c104
-rw-r--r--src/util/perf_crypto_hash.c20
-rw-r--r--src/util/plugin.c118
-rw-r--r--src/util/program.c112
-rw-r--r--src/util/pseudonym.c282
-rw-r--r--src/util/resolver_api.c602
-rw-r--r--src/util/scheduler.c110
-rw-r--r--src/util/server.c816
-rw-r--r--src/util/server_mst.c278
-rw-r--r--src/util/server_nc.c221
-rw-r--r--src/util/server_tc.c79
-rw-r--r--src/util/service.c1433
-rw-r--r--src/util/signal.c19
-rw-r--r--src/util/strings.c291
-rw-r--r--src/util/test_bio.c55
-rw-r--r--src/util/test_client.c56
-rw-r--r--src/util/test_common_allocation.c24
-rw-r--r--src/util/test_common_logging.c49
-rw-r--r--src/util/test_common_logging_dummy.c4
-rw-r--r--src/util/test_common_logging_runtime_loglevels.c334
-rw-r--r--src/util/test_configuration.c477
-rw-r--r--src/util/test_connection.c64
-rw-r--r--src/util/test_connection_addressing.c69
-rw-r--r--src/util/test_connection_receive_cancel.c22
-rw-r--r--src/util/test_connection_timeout.c43
-rw-r--r--src/util/test_connection_timeout_no_connect.c16
-rw-r--r--src/util/test_connection_transmit_cancel.c15
-rw-r--r--src/util/test_container_bloomfilter.c206
-rw-r--r--src/util/test_container_heap.c14
-rw-r--r--src/util/test_container_meta_data.c256
-rw-r--r--src/util/test_container_multihashmap.c29
-rw-r--r--src/util/test_container_slist.c95
-rw-r--r--src/util/test_crypto_aes.c120
-rw-r--r--src/util/test_crypto_aes_weak.c127
-rw-r--r--src/util/test_crypto_hash.c13
-rw-r--r--src/util/test_crypto_hkdf.c94
-rw-r--r--src/util/test_crypto_ksk.c192
-rw-r--r--src/util/test_crypto_random.c18
-rw-r--r--src/util/test_crypto_rsa.c210
-rw-r--r--src/util/test_disk.c101
-rw-r--r--src/util/test_getopt.c68
-rw-r--r--src/util/test_os_network.c8
-rw-r--r--src/util/test_os_priority.c12
-rw-r--r--src/util/test_os_start_process.c78
-rw-r--r--src/util/test_peer.c46
-rw-r--r--src/util/test_program.c18
-rw-r--r--src/util/test_pseudonym.c46
-rw-r--r--src/util/test_resolver_api.c267
-rw-r--r--src/util/test_scheduler.c16
-rw-r--r--src/util/test_scheduler_delay.c20
-rw-r--r--src/util/test_server.c43
-rw-r--r--src/util/test_server_disconnect.c21
-rw-r--r--src/util/test_server_with_client.c58
-rw-r--r--src/util/test_server_with_client_unix.c52
-rw-r--r--src/util/test_service.c77
-rw-r--r--src/util/test_strings.c24
-rw-r--r--src/util/test_time.c52
-rw-r--r--src/util/time.c64
-rw-r--r--src/util/winproc.c230
91 files changed, 10941 insertions, 10580 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c
index c0ac54054..ceac4d87b 100644
--- a/src/util/bandwidth.c
+++ b/src/util/bandwidth.c
@@ -29,6 +29,8 @@
29 29
30#define DEBUG_BANDWIDTH GNUNET_EXTRA_LOGGING 30#define DEBUG_BANDWIDTH GNUNET_EXTRA_LOGGING
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
33
32/** 34/**
33 * Create a new bandwidth value. 35 * Create a new bandwidth value.
34 * 36 *
@@ -41,8 +43,8 @@ GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
41 struct GNUNET_BANDWIDTH_Value32NBO ret; 43 struct GNUNET_BANDWIDTH_Value32NBO ret;
42 44
43#if DEBUG_BANDWIDTH 45#if DEBUG_BANDWIDTH
44 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing bandwidth of %u Bps\n", 46 LOG (GNUNET_ERROR_TYPE_DEBUG, "Initializing bandwidth of %u Bps\n",
45 (unsigned int) bytes_per_second); 47 (unsigned int) bytes_per_second);
46#endif 48#endif
47 ret.value__ = htonl (bytes_per_second); 49 ret.value__ = htonl (bytes_per_second);
48 return ret; 50 return ret;
@@ -58,11 +60,11 @@ GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
58 */ 60 */
59struct GNUNET_BANDWIDTH_Value32NBO 61struct GNUNET_BANDWIDTH_Value32NBO
60GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, 62GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
61 struct GNUNET_BANDWIDTH_Value32NBO b2) 63 struct GNUNET_BANDWIDTH_Value32NBO b2)
62{ 64{
63 return 65 return
64 GNUNET_BANDWIDTH_value_init (GNUNET_MIN 66 GNUNET_BANDWIDTH_value_init (GNUNET_MIN
65 (ntohl (b1.value__), ntohl (b2.value__))); 67 (ntohl (b1.value__), ntohl (b2.value__)));
66} 68}
67 69
68 70
@@ -76,18 +78,18 @@ GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
76 */ 78 */
77uint64_t 79uint64_t
78GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO 80GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
79 bps, 81 bps,
80 struct GNUNET_TIME_Relative 82 struct GNUNET_TIME_Relative
81 deadline) 83 deadline)
82{ 84{
83 uint64_t b; 85 uint64_t b;
84 86
85 b = ntohl (bps.value__); 87 b = ntohl (bps.value__);
86#if DEBUG_BANDWIDTH 88#if DEBUG_BANDWIDTH
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 89 LOG (GNUNET_ERROR_TYPE_DEBUG,
88 "Bandwidth has %llu bytes available until deadline in %llums\n", 90 "Bandwidth has %llu bytes available until deadline in %llums\n",
89 (unsigned long long) ((b * deadline.rel_value + 500LL) / 1000LL), 91 (unsigned long long) ((b * deadline.rel_value + 500LL) / 1000LL),
90 deadline.rel_value); 92 deadline.rel_value);
91#endif 93#endif
92 return (b * deadline.rel_value + 500LL) / 1000LL; 94 return (b * deadline.rel_value + 500LL) / 1000LL;
93} 95}
@@ -103,25 +105,25 @@ GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
103 */ 105 */
104struct GNUNET_TIME_Relative 106struct GNUNET_TIME_Relative
105GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps, 107GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
106 uint64_t size) 108 uint64_t size)
107{ 109{
108 uint64_t b; 110 uint64_t b;
109 struct GNUNET_TIME_Relative ret; 111 struct GNUNET_TIME_Relative ret;
110 112
111 b = ntohl (bps.value__); 113 b = ntohl (bps.value__);
112 if (b == 0) 114 if (b == 0)
113 { 115 {
114#if DEBUG_BANDWIDTH 116#if DEBUG_BANDWIDTH
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 117 LOG (GNUNET_ERROR_TYPE_DEBUG,
116 "Bandwidth suggests delay of infinity (zero bandwidth)\n"); 118 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
117#endif 119#endif
118 return GNUNET_TIME_UNIT_FOREVER_REL; 120 return GNUNET_TIME_UNIT_FOREVER_REL;
119 } 121 }
120 ret.rel_value = size * 1000LL / b; 122 ret.rel_value = size * 1000LL / b;
121#if DEBUG_BANDWIDTH 123#if DEBUG_BANDWIDTH
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 124 LOG (GNUNET_ERROR_TYPE_DEBUG,
123 "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n", 125 "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n",
124 (unsigned long long) ret.rel_value, (unsigned long long) size); 126 (unsigned long long) ret.rel_value, (unsigned long long) size);
125#endif 127#endif
126 return ret; 128 return ret;
127} 129}
@@ -143,18 +145,18 @@ GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
143 */ 145 */
144void 146void
145GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av, 147GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
146 struct GNUNET_BANDWIDTH_Value32NBO 148 struct GNUNET_BANDWIDTH_Value32NBO
147 bytes_per_second_limit, uint32_t max_carry_s) 149 bytes_per_second_limit, uint32_t max_carry_s)
148{ 150{
149 av->consumption_since_last_update__ = 0; 151 av->consumption_since_last_update__ = 0;
150 av->last_update__ = GNUNET_TIME_absolute_get (); 152 av->last_update__ = GNUNET_TIME_absolute_get ();
151 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__); 153 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
152 av->max_carry_s__ = max_carry_s; 154 av->max_carry_s__ = max_carry_s;
153#if DEBUG_BANDWIDTH 155#if DEBUG_BANDWIDTH
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 156 LOG (GNUNET_ERROR_TYPE_DEBUG,
155 "Tracker %p initialized with %u Bps and max carry %u\n", av, 157 "Tracker %p initialized with %u Bps and max carry %u\n", av,
156 (unsigned int) av->available_bytes_per_s__, 158 (unsigned int) av->available_bytes_per_s__,
157 (unsigned int) max_carry_s); 159 (unsigned int) max_carry_s);
158#endif 160#endif
159} 161}
160 162
@@ -177,26 +179,26 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
177 now = GNUNET_TIME_absolute_get (); 179 now = GNUNET_TIME_absolute_get ();
178 delta_time = now.abs_value - av->last_update__.abs_value; 180 delta_time = now.abs_value - av->last_update__.abs_value;
179 delta_avail = 181 delta_avail =
180 (delta_time * ((unsigned long long) av->available_bytes_per_s__) + 182 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
181 500LL) / 1000LL; 183 500LL) / 1000LL;
182 av->consumption_since_last_update__ -= delta_avail; 184 av->consumption_since_last_update__ -= delta_avail;
183 av->last_update__ = now; 185 av->last_update__ = now;
184 if (av->consumption_since_last_update__ < 0) 186 if (av->consumption_since_last_update__ < 0)
185 { 187 {
186 left_bytes = -av->consumption_since_last_update__; 188 left_bytes = -av->consumption_since_last_update__;
187 max_carry = av->available_bytes_per_s__ * av->max_carry_s__; 189 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
188 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE) 190 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
189 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE; 191 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
190 if (max_carry > left_bytes) 192 if (max_carry > left_bytes)
191 av->consumption_since_last_update__ = -left_bytes; 193 av->consumption_since_last_update__ = -left_bytes;
192 else 194 else
193 av->consumption_since_last_update__ = -max_carry; 195 av->consumption_since_last_update__ = -max_carry;
194 } 196 }
195#if DEBUG_BANDWIDTH 197#if DEBUG_BANDWIDTH
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 198 LOG (GNUNET_ERROR_TYPE_DEBUG,
197 "Tracker %p updated, have %u Bps, last update was %llu ms ago\n", 199 "Tracker %p updated, have %u Bps, last update was %llu ms ago\n",
198 av, (unsigned int) av->available_bytes_per_s__, 200 av, (unsigned int) av->available_bytes_per_s__,
199 (unsigned long long) delta_time); 201 (unsigned long long) delta_time);
200#endif 202#endif
201 203
202} 204}
@@ -215,38 +217,38 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
215 */ 217 */
216int 218int
217GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, 219GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
218 ssize_t size) 220 ssize_t size)
219{ 221{
220 int64_t nc; 222 int64_t nc;
221 223
222#if DEBUG_BANDWIDTH 224#if DEBUG_BANDWIDTH
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av, 225 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av,
224 (int) size); 226 (int) size);
225#endif 227#endif
226 if (size > 0) 228 if (size > 0)
227 {
228 nc = av->consumption_since_last_update__ + size;
229 if (nc < av->consumption_since_last_update__)
230 {
231 GNUNET_break (0);
232 return GNUNET_SYSERR;
233 }
234 av->consumption_since_last_update__ = nc;
235 update_tracker (av);
236 if (av->consumption_since_last_update__ > 0)
237 { 229 {
230 nc = av->consumption_since_last_update__ + size;
231 if (nc < av->consumption_since_last_update__)
232 {
233 GNUNET_break (0);
234 return GNUNET_SYSERR;
235 }
236 av->consumption_since_last_update__ = nc;
237 update_tracker (av);
238 if (av->consumption_since_last_update__ > 0)
239 {
238#if DEBUG_BANDWIDTH 240#if DEBUG_BANDWIDTH
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 241 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Tracker %p consumption %llu bytes above limit\n", av, 242 "Tracker %p consumption %llu bytes above limit\n", av,
241 (unsigned long long) av->consumption_since_last_update__); 243 (unsigned long long) av->consumption_since_last_update__);
242#endif 244#endif
243 return GNUNET_YES; 245 return GNUNET_YES;
246 }
244 } 247 }
245 }
246 else 248 else
247 { 249 {
248 av->consumption_since_last_update__ += size; 250 av->consumption_since_last_update__ += size;
249 } 251 }
250 return GNUNET_NO; 252 return GNUNET_NO;
251} 253}
252 254
@@ -262,35 +264,35 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
262 */ 264 */
263struct GNUNET_TIME_Relative 265struct GNUNET_TIME_Relative
264GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av, 266GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
265 size_t size) 267 size_t size)
266{ 268{
267 struct GNUNET_TIME_Relative ret; 269 struct GNUNET_TIME_Relative ret;
268 int64_t bytes_needed; 270 int64_t bytes_needed;
269 271
270 if (av->available_bytes_per_s__ == 0) 272 if (av->available_bytes_per_s__ == 0)
271 { 273 {
272#if DEBUG_BANDWIDTH 274#if DEBUG_BANDWIDTH
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av); 275 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
274#endif 276#endif
275 return GNUNET_TIME_UNIT_FOREVER_REL; 277 return GNUNET_TIME_UNIT_FOREVER_REL;
276 } 278 }
277 update_tracker (av); 279 update_tracker (av);
278 bytes_needed = size + av->consumption_since_last_update__; 280 bytes_needed = size + av->consumption_since_last_update__;
279 if (bytes_needed <= 0) 281 if (bytes_needed <= 0)
280 { 282 {
281#if DEBUG_BANDWIDTH 283#if DEBUG_BANDWIDTH
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 284 LOG (GNUNET_ERROR_TYPE_DEBUG,
283 "Tracker %p delay for %u bytes is zero\n", av, 285 "Tracker %p delay for %u bytes is zero\n", av,
284 (unsigned int) size); 286 (unsigned int) size);
285#endif 287#endif
286 return GNUNET_TIME_UNIT_ZERO; 288 return GNUNET_TIME_UNIT_ZERO;
287 } 289 }
288 ret.rel_value = 290 ret.rel_value =
289 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__; 291 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
290#if DEBUG_BANDWIDTH 292#if DEBUG_BANDWIDTH
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 293 LOG (GNUNET_ERROR_TYPE_DEBUG,
292 "Tracker %p delay for %u bytes is %llu ms\n", av, 294 "Tracker %p delay for %u bytes is %llu ms\n", av,
293 (unsigned int) size, (unsigned long long) ret.rel_value); 295 (unsigned int) size, (unsigned long long) ret.rel_value);
294#endif 296#endif
295 return ret; 297 return ret;
296} 298}
@@ -313,14 +315,14 @@ GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
313 update_tracker (av); 315 update_tracker (av);
314 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__); 316 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
315 avail = 317 avail =
316 GNUNET_BANDWIDTH_value_get_available_until (bps, 318 GNUNET_BANDWIDTH_value_get_available_until (bps,
317 GNUNET_TIME_absolute_get_duration 319 GNUNET_TIME_absolute_get_duration
318 (av->last_update__)); 320 (av->last_update__));
319 used = av->consumption_since_last_update__; 321 used = av->consumption_since_last_update__;
320#if DEBUG_BANDWIDTH 322#if DEBUG_BANDWIDTH
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 323 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "Tracker %p available bandwidth is %lld bytes\n", av, 324 "Tracker %p available bandwidth is %lld bytes\n", av,
323 (long long) (int64_t) (avail - used)); 325 (long long) (int64_t) (avail - used));
324#endif 326#endif
325 return (int64_t) (avail - used); 327 return (int64_t) (avail - used);
326} 328}
@@ -334,23 +336,23 @@ GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
334 */ 336 */
335void 337void
336GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av, 338GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
337 struct GNUNET_BANDWIDTH_Value32NBO 339 struct GNUNET_BANDWIDTH_Value32NBO
338 bytes_per_second_limit) 340 bytes_per_second_limit)
339{ 341{
340 uint32_t old_limit; 342 uint32_t old_limit;
341 uint32_t new_limit; 343 uint32_t new_limit;
342 344
343 new_limit = ntohl (bytes_per_second_limit.value__); 345 new_limit = ntohl (bytes_per_second_limit.value__);
344#if DEBUG_BANDWIDTH 346#if DEBUG_BANDWIDTH
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 347 LOG (GNUNET_ERROR_TYPE_DEBUG,
346 "Tracker %p bandwidth changed to %u Bps\n", av, 348 "Tracker %p bandwidth changed to %u Bps\n", av,
347 (unsigned int) new_limit); 349 (unsigned int) new_limit);
348#endif 350#endif
349 update_tracker (av); 351 update_tracker (av);
350 old_limit = av->available_bytes_per_s__; 352 old_limit = av->available_bytes_per_s__;
351 av->available_bytes_per_s__ = new_limit; 353 av->available_bytes_per_s__ = new_limit;
352 if (old_limit > new_limit) 354 if (old_limit > new_limit)
353 update_tracker (av); /* maximum excess might be less now */ 355 update_tracker (av); /* maximum excess might be less now */
354} 356}
355 357
356 358
diff --git a/src/util/bio.c b/src/util/bio.c
index 404b7aa3b..9c2b9d0dd 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -26,6 +26,8 @@
26#include "gnunet_bio_lib.h" 26#include "gnunet_bio_lib.h"
27#include "gnunet_disk_lib.h" 27#include "gnunet_disk_lib.h"
28 28
29#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
30
29#define BIO_BUFFER_SIZE 65536 31#define BIO_BUFFER_SIZE 65536
30 32
31#define MAX_META_DATA (1024 * 1024) 33#define MAX_META_DATA (1024 * 1024)
@@ -56,7 +58,8 @@ GNUNET_BIO_read_open (const char *fn)
56 struct GNUNET_DISK_FileHandle *fd; 58 struct GNUNET_DISK_FileHandle *fd;
57 struct GNUNET_BIO_ReadHandle *h; 59 struct GNUNET_BIO_ReadHandle *h;
58 60
59 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); 61 fd =
62 GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
60 if (NULL == fd) 63 if (NULL == fd)
61 return NULL; 64 return NULL;
62 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); 65 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
@@ -102,7 +105,7 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
102 */ 105 */
103int 106int
104GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, 107GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
105 void *result, size_t len) 108 void *result, size_t len)
106{ 109{
107 char *dst = result; 110 char *dst = result;
108 size_t min; 111 size_t min;
@@ -113,38 +116,38 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
113 return GNUNET_SYSERR; 116 return GNUNET_SYSERR;
114 pos = 0; 117 pos = 0;
115 do 118 do
116 {
117 /* first, use buffer */
118 min = h->have - h->pos;
119 if (min > 0)
120 {
121 if (min > len - pos)
122 min = len - pos;
123 memcpy (&dst[pos], &h->buffer[h->pos], min);
124 h->pos += min;
125 pos += min;
126 }
127 if (pos == len)
128 return GNUNET_OK; /* done! */
129 GNUNET_assert (h->have == h->pos);
130 /* fill buffer */
131 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
132 if (ret == -1)
133 {
134 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
135 STRERROR (errno));
136 return GNUNET_SYSERR;
137 }
138 if (ret == 0)
139 { 119 {
140 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, 120 /* first, use buffer */
141 _("End of file")); 121 min = h->have - h->pos;
142 return GNUNET_SYSERR; 122 if (min > 0)
123 {
124 if (min > len - pos)
125 min = len - pos;
126 memcpy (&dst[pos], &h->buffer[h->pos], min);
127 h->pos += min;
128 pos += min;
129 }
130 if (pos == len)
131 return GNUNET_OK; /* done! */
132 GNUNET_assert (h->have == h->pos);
133 /* fill buffer */
134 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
135 if (ret == -1)
136 {
137 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
138 STRERROR (errno));
139 return GNUNET_SYSERR;
140 }
141 if (ret == 0)
142 {
143 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
144 _("End of file"));
145 return GNUNET_SYSERR;
146 }
147 h->pos = 0;
148 h->have = ret;
143 } 149 }
144 h->pos = 0; 150 while (pos < len); /* should always be true */
145 h->have = ret;
146 }
147 while (pos < len); /* should always be true */
148 return GNUNET_OK; 151 return GNUNET_OK;
149} 152}
150 153
@@ -160,8 +163,8 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
160 * @return GNUNET_OK on success, GNUNET_SYSERR on failure 163 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
161 */ 164 */
162int 165int
163GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line, 166GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file,
164 void *result, size_t len) 167 int line, void *result, size_t len)
165{ 168{
166 char what[1024]; 169 char what[1024];
167 170
@@ -182,39 +185,41 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line,
182 */ 185 */
183int 186int
184GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, 187GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
185 char **result, size_t maxLen) 188 char **result, size_t maxLen)
186{ 189{
187 char *buf; 190 char *buf;
188 uint32_t big; 191 uint32_t big;
189 192
190 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) 193 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
191 { 194 {
192 GNUNET_free_non_null (h->emsg); 195 GNUNET_free_non_null (h->emsg);
193 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what); 196 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"),
194 return GNUNET_SYSERR; 197 what);
195 } 198 return GNUNET_SYSERR;
199 }
196 if (big == 0) 200 if (big == 0)
197 { 201 {
198 *result = NULL; 202 *result = NULL;
199 return GNUNET_OK; 203 return GNUNET_OK;
200 } 204 }
201 if (big > maxLen) 205 if (big > maxLen)
202 { 206 {
203 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"), 207 GNUNET_asprintf (&h->emsg,
204 what, big, maxLen); 208 _("String `%s' longer than allowed (%u > %u)"), what,
205 return GNUNET_SYSERR; 209 big, maxLen);
206 } 210 return GNUNET_SYSERR;
211 }
207 buf = GNUNET_malloc (big); 212 buf = GNUNET_malloc (big);
208 *result = buf; 213 *result = buf;
209 buf[--big] = '\0'; 214 buf[--big] = '\0';
210 if (big == 0) 215 if (big == 0)
211 return GNUNET_OK; 216 return GNUNET_OK;
212 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) 217 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
213 { 218 {
214 GNUNET_free (buf); 219 GNUNET_free (buf);
215 *result = NULL; 220 *result = NULL;
216 return GNUNET_SYSERR; 221 return GNUNET_SYSERR;
217 } 222 }
218 return GNUNET_OK; 223 return GNUNET_OK;
219} 224}
220 225
@@ -229,7 +234,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
229 */ 234 */
230int 235int
231GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, 236GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
232 struct GNUNET_CONTAINER_MetaData **result) 237 struct GNUNET_CONTAINER_MetaData **result)
233{ 238{
234 uint32_t size; 239 uint32_t size;
235 char *buf; 240 char *buf;
@@ -238,30 +243,32 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
238 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK) 243 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
239 return GNUNET_SYSERR; 244 return GNUNET_SYSERR;
240 if (size == 0) 245 if (size == 0)
241 { 246 {
242 *result = NULL; 247 *result = NULL;
243 return GNUNET_OK; 248 return GNUNET_OK;
244 } 249 }
245 if (size > MAX_META_DATA) 250 if (size > MAX_META_DATA)
246 { 251 {
247 GNUNET_asprintf (&h->emsg, 252 GNUNET_asprintf (&h->emsg,
248 _("Serialized metadata `%s' larger than allowed (%u>%u)"), 253 _
249 what, size, MAX_META_DATA); 254 ("Serialized metadata `%s' larger than allowed (%u>%u)"),
250 return GNUNET_SYSERR; 255 what, size, MAX_META_DATA);
251 } 256 return GNUNET_SYSERR;
257 }
252 buf = GNUNET_malloc (size); 258 buf = GNUNET_malloc (size);
253 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) 259 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
254 { 260 {
255 GNUNET_free (buf); 261 GNUNET_free (buf);
256 return GNUNET_SYSERR; 262 return GNUNET_SYSERR;
257 } 263 }
258 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); 264 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
259 if (meta == NULL) 265 if (meta == NULL)
260 { 266 {
261 GNUNET_free (buf); 267 GNUNET_free (buf);
262 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what); 268 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"),
263 return GNUNET_SYSERR; 269 what);
264 } 270 return GNUNET_SYSERR;
271 }
265 GNUNET_free (buf); 272 GNUNET_free (buf);
266 *result = meta; 273 *result = meta;
267 return GNUNET_OK; 274 return GNUNET_OK;
@@ -279,7 +286,7 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
279 */ 286 */
280int 287int
281GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, 288GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
282 int line, int32_t * i) 289 int line, int32_t * i)
283{ 290{
284 int32_t big; 291 int32_t big;
285 292
@@ -301,7 +308,7 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
301 */ 308 */
302int 309int
303GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file, 310GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
304 int line, int64_t * i) 311 int line, int64_t * i)
305{ 312{
306 int64_t big; 313 int64_t big;
307 314
@@ -337,13 +344,15 @@ GNUNET_BIO_write_open (const char *fn)
337 struct GNUNET_BIO_WriteHandle *h; 344 struct GNUNET_BIO_WriteHandle *h;
338 345
339 fd = GNUNET_DISK_file_open (fn, 346 fd = GNUNET_DISK_file_open (fn,
340 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE 347 GNUNET_DISK_OPEN_WRITE |
341 | GNUNET_DISK_OPEN_CREATE, 348 GNUNET_DISK_OPEN_TRUNCATE |
342 GNUNET_DISK_PERM_USER_READ | 349 GNUNET_DISK_OPEN_CREATE,
343 GNUNET_DISK_PERM_USER_WRITE); 350 GNUNET_DISK_PERM_USER_READ |
351 GNUNET_DISK_PERM_USER_WRITE);
344 if (NULL == fd) 352 if (NULL == fd)
345 return NULL; 353 return NULL;
346 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); 354 h =
355 GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
347 h->buffer = (char *) &h[1]; 356 h->buffer = (char *) &h[1];
348 h->size = BIO_BUFFER_SIZE; 357 h->size = BIO_BUFFER_SIZE;
349 h->fd = fd; 358 h->fd = fd;
@@ -365,18 +374,18 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
365 int ret; 374 int ret;
366 375
367 if (NULL == h->fd) 376 if (NULL == h->fd)
368 { 377 {
369 ret = GNUNET_SYSERR;
370 }
371 else
372 {
373 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
374 if (wrt == h->have)
375 ret = GNUNET_OK;
376 else
377 ret = GNUNET_SYSERR; 378 ret = GNUNET_SYSERR;
378 GNUNET_DISK_file_close (h->fd); 379 }
379 } 380 else
381 {
382 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
383 if (wrt == h->have)
384 ret = GNUNET_OK;
385 else
386 ret = GNUNET_SYSERR;
387 GNUNET_DISK_file_close (h->fd);
388 }
380 GNUNET_free (h); 389 GNUNET_free (h);
381 return ret; 390 return ret;
382} 391}
@@ -392,7 +401,7 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
392 */ 401 */
393int 402int
394GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, 403GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
395 size_t n) 404 size_t n)
396{ 405{
397 const char *src = buffer; 406 const char *src = buffer;
398 size_t min; 407 size_t min;
@@ -403,27 +412,27 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
403 return GNUNET_SYSERR; 412 return GNUNET_SYSERR;
404 pos = 0; 413 pos = 0;
405 do 414 do
406 {
407 /* first, just use buffer */
408 min = h->size - h->have;
409 if (min > n - pos)
410 min = n - pos;
411 memcpy (&h->buffer[h->have], &src[pos], min);
412 pos += min;
413 h->have += min;
414 if (pos == n)
415 return GNUNET_OK; /* done */
416 GNUNET_assert (h->have == h->size);
417 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
418 if (ret != h->size)
419 { 415 {
420 GNUNET_DISK_file_close (h->fd); 416 /* first, just use buffer */
421 h->fd = NULL; 417 min = h->size - h->have;
422 return GNUNET_SYSERR; /* error */ 418 if (min > n - pos)
419 min = n - pos;
420 memcpy (&h->buffer[h->have], &src[pos], min);
421 pos += min;
422 h->have += min;
423 if (pos == n)
424 return GNUNET_OK; /* done */
425 GNUNET_assert (h->have == h->size);
426 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
427 if (ret != h->size)
428 {
429 GNUNET_DISK_file_close (h->fd);
430 h->fd = NULL;
431 return GNUNET_SYSERR; /* error */
432 }
433 h->have = 0;
423 } 434 }
424 h->have = 0; 435 while (pos < n); /* should always be true */
425 }
426 while (pos < n); /* should always be true */
427 GNUNET_break (0); 436 GNUNET_break (0);
428 return GNUNET_OK; 437 return GNUNET_OK;
429} 438}
@@ -459,7 +468,7 @@ GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
459 */ 468 */
460int 469int
461GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, 470GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
462 const struct GNUNET_CONTAINER_MetaData *m) 471 const struct GNUNET_CONTAINER_MetaData *m)
463{ 472{
464 ssize_t size; 473 ssize_t size;
465 char *buf; 474 char *buf;
@@ -468,19 +477,19 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
468 return GNUNET_BIO_write_int32 (h, 0); 477 return GNUNET_BIO_write_int32 (h, 0);
469 buf = NULL; 478 buf = NULL;
470 size = 479 size =
471 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, 480 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
472 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); 481 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
473 if (size == -1) 482 if (size == -1)
474 { 483 {
475 GNUNET_free (buf); 484 GNUNET_free (buf);
476 return GNUNET_SYSERR; 485 return GNUNET_SYSERR;
477 } 486 }
478 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || 487 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
479 (GNUNET_OK != GNUNET_BIO_write (h, buf, size))) 488 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
480 { 489 {
481 GNUNET_free (buf); 490 GNUNET_free (buf);
482 return GNUNET_SYSERR; 491 return GNUNET_SYSERR;
483 } 492 }
484 GNUNET_free (buf); 493 GNUNET_free (buf);
485 return GNUNET_OK; 494 return GNUNET_OK;
486} 495}
diff --git a/src/util/client.c b/src/util/client.c
index 5193f751f..5d5387ef9 100644
--- a/src/util/client.c
+++ b/src/util/client.c
@@ -43,6 +43,8 @@
43 */ 43 */
44#define MAX_ATTEMPTS 50 44#define MAX_ATTEMPTS 50
45 45
46#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
47
46/** 48/**
47 * Handle for a transmission request. 49 * Handle for a transmission request.
48 */ 50 */
@@ -262,7 +264,8 @@ struct GNUNET_CLIENT_Connection
262 */ 264 */
263static struct GNUNET_CONNECTION_Handle * 265static struct GNUNET_CONNECTION_Handle *
264do_connect (const char *service_name, 266do_connect (const char *service_name,
265 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt) 267 const struct GNUNET_CONFIGURATION_Handle *cfg,
268 unsigned int attempt)
266{ 269{
267 struct GNUNET_CONNECTION_Handle *sock; 270 struct GNUNET_CONNECTION_Handle *sock;
268 char *hostname; 271 char *hostname;
@@ -272,79 +275,80 @@ do_connect (const char *service_name,
272 sock = NULL; 275 sock = NULL;
273#if AF_UNIX 276#if AF_UNIX
274 if (0 == (attempt % 2)) 277 if (0 == (attempt % 2))
275 {
276 /* on even rounds, try UNIX */
277 unixpath = NULL;
278 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? */
279 { 278 {
280 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); 279 /* on even rounds, try UNIX */
281 if (sock != NULL) 280 unixpath = NULL;
282 { 281 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? */
282 {
283 sock =
284 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
285 if (sock != NULL)
286 {
283#if DEBUG_CLIENT 287#if DEBUG_CLIENT
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", 288 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
285 unixpath); 289 unixpath);
286#endif 290#endif
287 GNUNET_free (unixpath); 291 GNUNET_free (unixpath);
288 return sock; 292 return sock;
289 } 293 }
294 }
295 GNUNET_free_non_null (unixpath);
290 } 296 }
291 GNUNET_free_non_null (unixpath);
292 }
293#endif 297#endif
294 298
295 if ((GNUNET_OK != 299 if ((GNUNET_OK !=
296 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) 300 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT",
297 || (port > 65535) || 301 &port)) || (port > 65535)
298 (GNUNET_OK != 302 || (GNUNET_OK !=
299 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME", 303 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
300 &hostname))) 304 "HOSTNAME", &hostname)))
301 { 305 {
302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 306 LOG (GNUNET_ERROR_TYPE_WARNING,
303 _ 307 _
304 ("Could not determine valid hostname and port for service `%s' from configuration.\n"), 308 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
305 service_name); 309 service_name);
306 return NULL; 310 return NULL;
307 } 311 }
308 if (0 == strlen (hostname)) 312 if (0 == strlen (hostname))
309 {
310 GNUNET_free (hostname);
311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
312 _("Need a non-empty hostname for service `%s'.\n"),
313 service_name);
314 return NULL;
315 }
316 if (port == 0)
317 {
318#if AF_UNIX
319 if (0 != (attempt % 2))
320 { 313 {
321 /* try UNIX */ 314 GNUNET_free (hostname);
322 unixpath = NULL; 315 LOG (GNUNET_ERROR_TYPE_WARNING,
323 if ((GNUNET_OK == 316 _("Need a non-empty hostname for service `%s'.\n"), service_name);
324 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", 317 return NULL;
325 &unixpath)) &&
326 (0 < strlen (unixpath)))
327 {
328 sock =
329 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
330 if (sock != NULL)
331 {
332 GNUNET_free (unixpath);
333 GNUNET_free (hostname);
334 return sock;
335 }
336 }
337 GNUNET_free_non_null (unixpath);
338 } 318 }
319 if (port == 0)
320 {
321#if AF_UNIX
322 if (0 != (attempt % 2))
323 {
324 /* try UNIX */
325 unixpath = NULL;
326 if ((GNUNET_OK ==
327 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
328 "UNIXPATH", &unixpath))
329 && (0 < strlen (unixpath)))
330 {
331 sock =
332 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
333 unixpath);
334 if (sock != NULL)
335 {
336 GNUNET_free (unixpath);
337 GNUNET_free (hostname);
338 return sock;
339 }
340 }
341 GNUNET_free_non_null (unixpath);
342 }
339#endif 343#endif
340#if DEBUG_CLIENT 344#if DEBUG_CLIENT
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 345 LOG (GNUNET_ERROR_TYPE_DEBUG,
342 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n", 346 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
343 service_name); 347 service_name);
344#endif 348#endif
345 GNUNET_free (hostname); 349 GNUNET_free (hostname);
346 return NULL; 350 return NULL;
347 } 351 }
348 352
349 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port); 353 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
350 GNUNET_free (hostname); 354 GNUNET_free (hostname);
@@ -361,7 +365,7 @@ do_connect (const char *service_name,
361 */ 365 */
362struct GNUNET_CLIENT_Connection * 366struct GNUNET_CLIENT_Connection *
363GNUNET_CLIENT_connect (const char *service_name, 367GNUNET_CLIENT_connect (const char *service_name,
364 const struct GNUNET_CONFIGURATION_Handle *cfg) 368 const struct GNUNET_CONFIGURATION_Handle *cfg)
365{ 369{
366 struct GNUNET_CLIENT_Connection *ret; 370 struct GNUNET_CLIENT_Connection *ret;
367 struct GNUNET_CONNECTION_Handle *sock; 371 struct GNUNET_CONNECTION_Handle *sock;
@@ -385,7 +389,7 @@ GNUNET_CLIENT_connect (const char *service_name,
385 */ 389 */
386void 390void
387GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h, 391GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
388 int do_ignore) 392 int do_ignore)
389{ 393{
390 h->ignore_shutdown = do_ignore; 394 h->ignore_shutdown = do_ignore;
391 if (h->sock != NULL) 395 if (h->sock != NULL)
@@ -410,33 +414,33 @@ GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
410 */ 414 */
411void 415void
412GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock, 416GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
413 int finish_pending_write) 417 int finish_pending_write)
414{ 418{
415 if (sock->in_receive == GNUNET_YES) 419 if (sock->in_receive == GNUNET_YES)
416 { 420 {
417 GNUNET_CONNECTION_receive_cancel (sock->sock); 421 GNUNET_CONNECTION_receive_cancel (sock->sock);
418 sock->in_receive = GNUNET_NO; 422 sock->in_receive = GNUNET_NO;
419 } 423 }
420 if (sock->th != NULL) 424 if (sock->th != NULL)
421 { 425 {
422 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th); 426 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
423 sock->th = NULL; 427 sock->th = NULL;
424 } 428 }
425 if (NULL != sock->sock) 429 if (NULL != sock->sock)
426 { 430 {
427 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write); 431 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
428 sock->sock = NULL; 432 sock->sock = NULL;
429 } 433 }
430 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK) 434 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
431 { 435 {
432 GNUNET_SCHEDULER_cancel (sock->receive_task); 436 GNUNET_SCHEDULER_cancel (sock->receive_task);
433 sock->receive_task = GNUNET_SCHEDULER_NO_TASK; 437 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
434 } 438 }
435 if (sock->tag != NULL) 439 if (sock->tag != NULL)
436 { 440 {
437 GNUNET_free (sock->tag); 441 GNUNET_free (sock->tag);
438 sock->tag = NULL; 442 sock->tag = NULL;
439 } 443 }
440 sock->receiver_handler = NULL; 444 sock->receiver_handler = NULL;
441 GNUNET_array_grow (sock->received_buf, sock->received_size, 0); 445 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
442 GNUNET_free (sock->service_name); 446 GNUNET_free (sock->service_name);
@@ -453,7 +457,7 @@ check_complete (struct GNUNET_CLIENT_Connection *conn)
453 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) && 457 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
454 (conn->received_pos >= 458 (conn->received_pos >=
455 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)-> 459 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
456 size))) 460 size)))
457 conn->msg_complete = GNUNET_YES; 461 conn->msg_complete = GNUNET_YES;
458} 462}
459 463
@@ -472,7 +476,7 @@ check_complete (struct GNUNET_CLIENT_Connection *conn)
472 */ 476 */
473static void 477static void
474receive_helper (void *cls, const void *buf, size_t available, 478receive_helper (void *cls, const void *buf, size_t available,
475 const struct sockaddr *addr, socklen_t addrlen, int errCode) 479 const struct sockaddr *addr, socklen_t addrlen, int errCode)
476{ 480{
477 struct GNUNET_CLIENT_Connection *conn = cls; 481 struct GNUNET_CLIENT_Connection *conn = cls;
478 struct GNUNET_TIME_Relative remaining; 482 struct GNUNET_TIME_Relative remaining;
@@ -482,22 +486,22 @@ receive_helper (void *cls, const void *buf, size_t available,
482 GNUNET_assert (conn->msg_complete == GNUNET_NO); 486 GNUNET_assert (conn->msg_complete == GNUNET_NO);
483 conn->in_receive = GNUNET_NO; 487 conn->in_receive = GNUNET_NO;
484 if ((available == 0) || (conn->sock == NULL) || (errCode != 0)) 488 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
485 { 489 {
486 /* signal timeout! */ 490 /* signal timeout! */
487#if DEBUG_CLIENT 491#if DEBUG_CLIENT
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 492 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n", 493 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
490 (unsigned int) available, 494 (unsigned int) available,
491 conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode)); 495 conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode));
492#endif 496#endif
493 if (NULL != (receive_handler = conn->receiver_handler)) 497 if (NULL != (receive_handler = conn->receiver_handler))
494 { 498 {
495 receive_handler_cls = conn->receiver_handler_cls; 499 receive_handler_cls = conn->receiver_handler_cls;
496 conn->receiver_handler = NULL; 500 conn->receiver_handler = NULL;
497 receive_handler (receive_handler_cls, NULL); 501 receive_handler (receive_handler_cls, NULL);
502 }
503 return;
498 } 504 }
499 return;
500 }
501 505
502 /* FIXME: optimize for common fast case where buf contains the 506 /* FIXME: optimize for common fast case where buf contains the
503 * entire message and we need no copying... */ 507 * entire message and we need no copying... */
@@ -506,22 +510,22 @@ receive_helper (void *cls, const void *buf, size_t available,
506 /* slow path: append to array */ 510 /* slow path: append to array */
507 if (conn->received_size < conn->received_pos + available) 511 if (conn->received_size < conn->received_pos + available)
508 GNUNET_array_grow (conn->received_buf, conn->received_size, 512 GNUNET_array_grow (conn->received_buf, conn->received_size,
509 conn->received_pos + available); 513 conn->received_pos + available);
510 memcpy (&conn->received_buf[conn->received_pos], buf, available); 514 memcpy (&conn->received_buf[conn->received_pos], buf, available);
511 conn->received_pos += available; 515 conn->received_pos += available;
512 check_complete (conn); 516 check_complete (conn);
513 /* check for timeout */ 517 /* check for timeout */
514 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout); 518 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
515 if (remaining.rel_value == 0) 519 if (remaining.rel_value == 0)
516 { 520 {
517 /* signal timeout! */ 521 /* signal timeout! */
518 if (NULL != conn->receiver_handler) 522 if (NULL != conn->receiver_handler)
519 conn->receiver_handler (conn->receiver_handler_cls, NULL); 523 conn->receiver_handler (conn->receiver_handler_cls, NULL);
520 return; 524 return;
521 } 525 }
522 /* back to receive -- either for more data or to call callback! */ 526 /* back to receive -- either for more data or to call callback! */
523 GNUNET_CLIENT_receive (conn, conn->receiver_handler, 527 GNUNET_CLIENT_receive (conn, conn->receiver_handler,
524 conn->receiver_handler_cls, remaining); 528 conn->receiver_handler_cls, remaining);
525} 529}
526 530
527 531
@@ -537,23 +541,23 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
537 struct GNUNET_CLIENT_Connection *sock = cls; 541 struct GNUNET_CLIENT_Connection *sock = cls;
538 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler; 542 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
539 const struct GNUNET_MessageHeader *cmsg = 543 const struct GNUNET_MessageHeader *cmsg =
540 (const struct GNUNET_MessageHeader *) sock->received_buf; 544 (const struct GNUNET_MessageHeader *) sock->received_buf;
541 void *handler_cls = sock->receiver_handler_cls; 545 void *handler_cls = sock->receiver_handler_cls;
542 uint16_t msize = ntohs (cmsg->size); 546 uint16_t msize = ntohs (cmsg->size);
543 char mbuf[msize]; 547 char mbuf[msize];
544 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf; 548 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
545 549
546#if DEBUG_CLIENT 550#if DEBUG_CLIENT
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 551 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 "Received message of type %u and size %u\n", ntohs (cmsg->type), 552 "Received message of type %u and size %u\n", ntohs (cmsg->type),
549 msize); 553 msize);
550#endif 554#endif
551 sock->receive_task = GNUNET_SCHEDULER_NO_TASK; 555 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
552 GNUNET_assert (GNUNET_YES == sock->msg_complete); 556 GNUNET_assert (GNUNET_YES == sock->msg_complete);
553 GNUNET_assert (sock->received_pos >= msize); 557 GNUNET_assert (sock->received_pos >= msize);
554 memcpy (msg, cmsg, msize); 558 memcpy (msg, cmsg, msize);
555 memmove (sock->received_buf, &sock->received_buf[msize], 559 memmove (sock->received_buf, &sock->received_buf[msize],
556 sock->received_pos - msize); 560 sock->received_pos - msize);
557 sock->received_pos -= msize; 561 sock->received_pos -= msize;
558 sock->msg_complete = GNUNET_NO; 562 sock->msg_complete = GNUNET_NO;
559 sock->receiver_handler = NULL; 563 sock->receiver_handler = NULL;
@@ -573,35 +577,36 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
573 */ 577 */
574void 578void
575GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock, 579GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
576 GNUNET_CLIENT_MessageHandler handler, void *handler_cls, 580 GNUNET_CLIENT_MessageHandler handler,
577 struct GNUNET_TIME_Relative timeout) 581 void *handler_cls, struct GNUNET_TIME_Relative timeout)
578{ 582{
579 if (sock->sock == NULL) 583 if (sock->sock == NULL)
580 { 584 {
581 /* already disconnected, fail instantly! */ 585 /* already disconnected, fail instantly! */
582 GNUNET_break (0); /* this should not happen in well-written code! */ 586 GNUNET_break (0); /* this should not happen in well-written code! */
583 if (NULL != handler) 587 if (NULL != handler)
584 handler (handler_cls, NULL); 588 handler (handler_cls, NULL);
585 return; 589 return;
586 } 590 }
587 sock->receiver_handler = handler; 591 sock->receiver_handler = handler;
588 sock->receiver_handler_cls = handler_cls; 592 sock->receiver_handler_cls = handler_cls;
589 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 593 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
590 if (GNUNET_YES == sock->msg_complete) 594 if (GNUNET_YES == sock->msg_complete)
591 { 595 {
592 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task); 596 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
593 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock); 597 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
594 } 598 }
595 else 599 else
596 { 600 {
597 GNUNET_assert (sock->in_receive == GNUNET_NO); 601 GNUNET_assert (sock->in_receive == GNUNET_NO);
598 sock->in_receive = GNUNET_YES; 602 sock->in_receive = GNUNET_YES;
599#if DEBUG_CLIENT 603#if DEBUG_CLIENT
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n"); 604 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
601#endif 605#endif
602 GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 606 GNUNET_CONNECTION_receive (sock->sock,
603 timeout, &receive_helper, sock); 607 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, timeout,
604 } 608 &receive_helper, sock);
609 }
605} 610}
606 611
607 612
@@ -612,7 +617,7 @@ static void
612service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls) 617service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
613{ 618{
614 GNUNET_SCHEDULER_add_continuation (task, task_cls, 619 GNUNET_SCHEDULER_add_continuation (task, task_cls,
615 GNUNET_SCHEDULER_REASON_TIMEOUT); 620 GNUNET_SCHEDULER_REASON_TIMEOUT);
616} 621}
617 622
618 623
@@ -631,18 +636,18 @@ confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
631 * detail in the future, for example, is this the 636 * detail in the future, for example, is this the
632 * correct service? FIXME! */ 637 * correct service? FIXME! */
633 if (msg != NULL) 638 if (msg != NULL)
634 { 639 {
635#if DEBUG_CLIENT 640#if DEBUG_CLIENT
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 641 LOG (GNUNET_ERROR_TYPE_DEBUG,
637 "Received confirmation that service is running.\n"); 642 "Received confirmation that service is running.\n");
638#endif 643#endif
639 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls, 644 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls,
640 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 645 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
641 } 646 }
642 else 647 else
643 { 648 {
644 service_test_error (conn->test_cb, conn->test_cb_cls); 649 service_test_error (conn->test_cb, conn->test_cb_cls);
645 } 650 }
646 GNUNET_CLIENT_disconnect (conn, GNUNET_NO); 651 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
647} 652}
648 653
@@ -663,24 +668,23 @@ write_test (void *cls, size_t size, void *buf)
663 struct GNUNET_MessageHeader *msg; 668 struct GNUNET_MessageHeader *msg;
664 669
665 if (size < sizeof (struct GNUNET_MessageHeader)) 670 if (size < sizeof (struct GNUNET_MessageHeader))
666 { 671 {
667#if DEBUG_CLIENT 672#if DEBUG_CLIENT
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 673 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
669 _("Failure to transmit TEST request.\n"));
670#endif 674#endif
671 service_test_error (conn->test_cb, conn->test_cb_cls); 675 service_test_error (conn->test_cb, conn->test_cb_cls);
672 GNUNET_CLIENT_disconnect (conn, GNUNET_NO); 676 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
673 return 0; /* client disconnected */ 677 return 0; /* client disconnected */
674 } 678 }
675#if DEBUG_CLIENT 679#if DEBUG_CLIENT
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST"); 680 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
677#endif 681#endif
678 msg = (struct GNUNET_MessageHeader *) buf; 682 msg = (struct GNUNET_MessageHeader *) buf;
679 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); 683 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
680 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 684 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
681 GNUNET_CLIENT_receive (conn, &confirm_handler, conn, 685 GNUNET_CLIENT_receive (conn, &confirm_handler, conn,
682 GNUNET_TIME_absolute_get_remaining 686 GNUNET_TIME_absolute_get_remaining
683 (conn->test_deadline)); 687 (conn->test_deadline));
684 return sizeof (struct GNUNET_MessageHeader); 688 return sizeof (struct GNUNET_MessageHeader);
685} 689}
686 690
@@ -700,9 +704,9 @@ write_test (void *cls, size_t size, void *buf)
700 */ 704 */
701void 705void
702GNUNET_CLIENT_service_test (const char *service, 706GNUNET_CLIENT_service_test (const char *service,
703 const struct GNUNET_CONFIGURATION_Handle *cfg, 707 const struct GNUNET_CONFIGURATION_Handle *cfg,
704 struct GNUNET_TIME_Relative timeout, 708 struct GNUNET_TIME_Relative timeout,
705 GNUNET_SCHEDULER_Task task, void *task_cls) 709 GNUNET_SCHEDULER_Task task, void *task_cls)
706{ 710{
707 char *hostname; 711 char *hostname;
708 unsigned long long port; 712 unsigned long long port;
@@ -710,8 +714,8 @@ GNUNET_CLIENT_service_test (const char *service,
710 struct GNUNET_CLIENT_Connection *conn; 714 struct GNUNET_CLIENT_Connection *conn;
711 715
712#if DEBUG_CLIENT 716#if DEBUG_CLIENT
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n", 717 LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
714 service); 718 service);
715#endif 719#endif
716#ifdef AF_UNIX 720#ifdef AF_UNIX
717 { 721 {
@@ -721,49 +725,50 @@ GNUNET_CLIENT_service_test (const char *service,
721 char *unixpath; 725 char *unixpath;
722 726
723 unixpath = NULL; 727 unixpath = NULL;
724 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? */ 728 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? */
725 {
726 if (strlen (unixpath) >= sizeof (s_un.sun_path))
727 { 729 {
728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 730 if (strlen (unixpath) >= sizeof (s_un.sun_path))
729 _("UNIXPATH `%s' too long, maximum length is %llu\n"), 731 {
730 unixpath, sizeof (s_un.sun_path)); 732 LOG (GNUNET_ERROR_TYPE_WARNING,
731 } 733 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
732 else 734 unixpath, sizeof (s_un.sun_path));
733 { 735 }
734 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); 736 else
735 if (sock != NULL) 737 {
736 { 738 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
737 memset (&s_un, 0, sizeof (s_un)); 739 if (sock != NULL)
738 s_un.sun_family = AF_UNIX; 740 {
739 slen = strlen (unixpath) + 1; 741 memset (&s_un, 0, sizeof (s_un));
740 if (slen >= sizeof (s_un.sun_path)) 742 s_un.sun_family = AF_UNIX;
741 slen = sizeof (s_un.sun_path) - 1; 743 slen = strlen (unixpath) + 1;
742 memcpy (s_un.sun_path, unixpath, slen); 744 if (slen >= sizeof (s_un.sun_path))
743 s_un.sun_path[slen] = '\0'; 745 slen = sizeof (s_un.sun_path) - 1;
744 slen = sizeof (struct sockaddr_un); 746 memcpy (s_un.sun_path, unixpath, slen);
747 s_un.sun_path[slen] = '\0';
748 slen = sizeof (struct sockaddr_un);
745#if LINUX 749#if LINUX
746 s_un.sun_path[0] = '\0'; 750 s_un.sun_path[0] = '\0';
747#endif 751#endif
748#if HAVE_SOCKADDR_IN_SIN_LEN 752#if HAVE_SOCKADDR_IN_SIN_LEN
749 s_un.sun_len = (u_char) slen; 753 s_un.sun_len = (u_char) slen;
750#endif 754#endif
751 if (GNUNET_OK != 755 if (GNUNET_OK !=
752 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un, 756 GNUNET_NETWORK_socket_bind (sock,
753 slen)) 757 (const struct sockaddr *)
754 { 758 &s_un, slen))
755 /* failed to bind => service must be running */ 759 {
756 GNUNET_free (unixpath); 760 /* failed to bind => service must be running */
757 (void) GNUNET_NETWORK_socket_close (sock); 761 GNUNET_free (unixpath);
758 GNUNET_SCHEDULER_add_continuation (task, task_cls, 762 (void) GNUNET_NETWORK_socket_close (sock);
759 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 763 GNUNET_SCHEDULER_add_continuation (task, task_cls,
760 return; 764 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
761 } 765 return;
762 (void) GNUNET_NETWORK_socket_close (sock); 766 }
763 } 767 (void) GNUNET_NETWORK_socket_close (sock);
764 /* let's try IP */ 768 }
769 /* let's try IP */
770 }
765 } 771 }
766 }
767 GNUNET_free_non_null (unixpath); 772 GNUNET_free_non_null (unixpath);
768 } 773 }
769#endif 774#endif
@@ -774,121 +779,124 @@ GNUNET_CLIENT_service_test (const char *service,
774 (port > 65535) || 779 (port > 65535) ||
775 (GNUNET_OK != 780 (GNUNET_OK !=
776 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME", 781 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
777 &hostname))) 782 &hostname)))
778 { 783 {
779 /* UNIXPATH failed (if possible) AND IP failed => error */ 784 /* UNIXPATH failed (if possible) AND IP failed => error */
780 service_test_error (task, task_cls); 785 service_test_error (task, task_cls);
781 return; 786 return;
782 } 787 }
783 788
784 if (0 == strcmp ("localhost", hostname) 789 if (0 == strcmp ("localhost", hostname)
785#if !LINUX 790#if !LINUX
786 && 0 791 && 0
787#endif 792#endif
788 ) 793 )
789 { 794 {
790 /* can test using 'bind' */ 795 /* can test using 'bind' */
791 struct sockaddr_in s_in; 796 struct sockaddr_in s_in;
792 797
793 memset (&s_in, 0, sizeof (s_in)); 798 memset (&s_in, 0, sizeof (s_in));
794#if HAVE_SOCKADDR_IN_SIN_LEN 799#if HAVE_SOCKADDR_IN_SIN_LEN
795 s_in.sin_len = sizeof (struct sockaddr_in); 800 s_in.sin_len = sizeof (struct sockaddr_in);
796#endif 801#endif
797 s_in.sin_family = AF_INET; 802 s_in.sin_family = AF_INET;
798 s_in.sin_port = htons (port); 803 s_in.sin_port = htons (port);
799 804
800 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); 805 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
801 if (sock != NULL) 806 if (sock != NULL)
802 { 807 {
803 if (GNUNET_OK != 808 if (GNUNET_OK !=
804 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in, 809 GNUNET_NETWORK_socket_bind (sock,
805 sizeof (s_in))) 810 (const struct sockaddr *) &s_in,
806 { 811 sizeof (s_in)))
807 /* failed to bind => service must be running */ 812 {
808 GNUNET_free (hostname); 813 /* failed to bind => service must be running */
809 (void) GNUNET_NETWORK_socket_close (sock); 814 GNUNET_free (hostname);
810 GNUNET_SCHEDULER_add_continuation (task, task_cls, 815 (void) GNUNET_NETWORK_socket_close (sock);
811 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 816 GNUNET_SCHEDULER_add_continuation (task, task_cls,
812 return; 817 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
813 } 818 return;
814 (void) GNUNET_NETWORK_socket_close (sock); 819 }
820 (void) GNUNET_NETWORK_socket_close (sock);
821 }
815 } 822 }
816 }
817 823
818 if (0 == strcmp ("ip6-localhost", hostname) 824 if (0 == strcmp ("ip6-localhost", hostname)
819#if !LINUX 825#if !LINUX
820 && 0 826 && 0
821#endif 827#endif
822 ) 828 )
823 { 829 {
824 /* can test using 'bind' */ 830 /* can test using 'bind' */
825 struct sockaddr_in6 s_in6; 831 struct sockaddr_in6 s_in6;
826 832
827 memset (&s_in6, 0, sizeof (s_in6)); 833 memset (&s_in6, 0, sizeof (s_in6));
828#if HAVE_SOCKADDR_IN_SIN_LEN 834#if HAVE_SOCKADDR_IN_SIN_LEN
829 s_in6.sin6_len = sizeof (struct sockaddr_in6); 835 s_in6.sin6_len = sizeof (struct sockaddr_in6);
830#endif 836#endif
831 s_in6.sin6_family = AF_INET6; 837 s_in6.sin6_family = AF_INET6;
832 s_in6.sin6_port = htons (port); 838 s_in6.sin6_port = htons (port);
833 839
834 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0); 840 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
835 if (sock != NULL) 841 if (sock != NULL)
836 { 842 {
837 if (GNUNET_OK != 843 if (GNUNET_OK !=
838 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6, 844 GNUNET_NETWORK_socket_bind (sock,
839 sizeof (s_in6))) 845 (const struct sockaddr *) &s_in6,
840 { 846 sizeof (s_in6)))
841 /* failed to bind => service must be running */ 847 {
842 GNUNET_free (hostname); 848 /* failed to bind => service must be running */
843 (void) GNUNET_NETWORK_socket_close (sock); 849 GNUNET_free (hostname);
844 GNUNET_SCHEDULER_add_continuation (task, task_cls, 850 (void) GNUNET_NETWORK_socket_close (sock);
845 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 851 GNUNET_SCHEDULER_add_continuation (task, task_cls,
846 return; 852 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
847 } 853 return;
848 (void) GNUNET_NETWORK_socket_close (sock); 854 }
855 (void) GNUNET_NETWORK_socket_close (sock);
856 }
849 } 857 }
850 }
851 858
852 if (((0 == strcmp ("localhost", hostname)) || 859 if (((0 == strcmp ("localhost", hostname)) ||
853 (0 == strcmp ("ip6-localhost", hostname))) 860 (0 == strcmp ("ip6-localhost", hostname)))
854#if !LINUX 861#if !LINUX
855 && 0 862 && 0
856#endif 863#endif
857 ) 864 )
858 { 865 {
859 /* all binds succeeded => claim service not running right now */ 866 /* all binds succeeded => claim service not running right now */
860 GNUNET_free_non_null (hostname); 867 GNUNET_free_non_null (hostname);
861 service_test_error (task, task_cls); 868 service_test_error (task, task_cls);
862 return; 869 return;
863 } 870 }
864 GNUNET_free_non_null (hostname); 871 GNUNET_free_non_null (hostname);
865 872
866 /* non-localhost, try 'connect' method */ 873 /* non-localhost, try 'connect' method */
867 conn = GNUNET_CLIENT_connect (service, cfg); 874 conn = GNUNET_CLIENT_connect (service, cfg);
868 if (conn == NULL) 875 if (conn == NULL)
869 { 876 {
870 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 877 LOG (GNUNET_ERROR_TYPE_INFO,
871 _("Could not connect to service `%s', must not be running.\n"), 878 _("Could not connect to service `%s', must not be running.\n"),
872 service); 879 service);
873 service_test_error (task, task_cls); 880 service_test_error (task, task_cls);
874 return; 881 return;
875 } 882 }
876 conn->test_cb = task; 883 conn->test_cb = task;
877 conn->test_cb_cls = task_cls; 884 conn->test_cb_cls = task_cls;
878 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout); 885 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
879 886
880 if (NULL == 887 if (NULL ==
881 GNUNET_CLIENT_notify_transmit_ready (conn, 888 GNUNET_CLIENT_notify_transmit_ready (conn,
882 sizeof (struct GNUNET_MessageHeader), 889 sizeof (struct
883 timeout, GNUNET_YES, &write_test, 890 GNUNET_MessageHeader),
884 conn)) 891 timeout, GNUNET_YES, &write_test,
885 { 892 conn))
886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 893 {
887 _("Failure to transmit request to service `%s'\n"), service); 894 LOG (GNUNET_ERROR_TYPE_WARNING,
888 service_test_error (task, task_cls); 895 _("Failure to transmit request to service `%s'\n"), service);
889 GNUNET_CLIENT_disconnect (conn, GNUNET_NO); 896 service_test_error (task, task_cls);
890 return; 897 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
891 } 898 return;
899 }
892} 900}
893 901
894 902
@@ -902,8 +910,7 @@ GNUNET_CLIENT_service_test (const char *service,
902 * @param buf where to write them 910 * @param buf where to write them
903 * @return number of bytes written to buf 911 * @return number of bytes written to buf
904 */ 912 */
905static size_t 913static size_t client_notify (void *cls, size_t size, void *buf);
906client_notify (void *cls, size_t size, void *buf);
907 914
908 915
909/** 916/**
@@ -914,59 +921,60 @@ client_notify (void *cls, size_t size, void *buf);
914 * @param tc unused 921 * @param tc unused
915 */ 922 */
916static void 923static void
917client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 924client_delayed_retry (void *cls,
925 const struct GNUNET_SCHEDULER_TaskContext *tc)
918{ 926{
919 struct GNUNET_CLIENT_TransmitHandle *th = cls; 927 struct GNUNET_CLIENT_TransmitHandle *th = cls;
920 struct GNUNET_TIME_Relative delay; 928 struct GNUNET_TIME_Relative delay;
921 929
922 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 930 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
923 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 931 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
924 { 932 {
925#if DEBUG_CLIENT 933#if DEBUG_CLIENT
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 934 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n");
927 "Transmission failed due to shutdown.\n");
928#endif 935#endif
929 th->sock->th = NULL; 936 th->sock->th = NULL;
930 th->notify (th->notify_cls, 0, NULL); 937 th->notify (th->notify_cls, 0, NULL);
931 GNUNET_free (th); 938 GNUNET_free (th);
932 return; 939 return;
933 } 940 }
934 th->sock->sock = 941 th->sock->sock =
935 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++); 942 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++);
936 if (NULL == th->sock->sock) 943 if (NULL == th->sock->sock)
937 { 944 {
938 /* could happen if we're out of sockets */ 945 /* could happen if we're out of sockets */
939 delay = 946 delay =
940 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining 947 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
941 (th->timeout), th->sock->back_off); 948 (th->timeout), th->sock->back_off);
942 th->sock->back_off = 949 th->sock->back_off =
943 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply 950 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
944 (th->sock->back_off, 2), 951 (th->sock->back_off, 2),
945 GNUNET_TIME_UNIT_SECONDS); 952 GNUNET_TIME_UNIT_SECONDS);
946#if DEBUG_CLIENT 953#if DEBUG_CLIENT
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 954 LOG (GNUNET_ERROR_TYPE_DEBUG,
948 "Transmission failed %u times, trying again in %llums.\n", 955 "Transmission failed %u times, trying again in %llums.\n",
949 MAX_ATTEMPTS - th->attempts_left, 956 MAX_ATTEMPTS - th->attempts_left,
950 (unsigned long long) delay.rel_value); 957 (unsigned long long) delay.rel_value);
951#endif 958#endif
952 th->reconnect_task = 959 th->reconnect_task =
953 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); 960 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
954 return; 961 return;
955 } 962 }
956 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock, th->sock->ignore_shutdown); 963 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
964 th->sock->ignore_shutdown);
957 th->th = 965 th->th =
958 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size, 966 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size,
959 GNUNET_TIME_absolute_get_remaining 967 GNUNET_TIME_absolute_get_remaining
960 (th->timeout), &client_notify, 968 (th->timeout), &client_notify,
961 th); 969 th);
962 if (th->th == NULL) 970 if (th->th == NULL)
963 { 971 {
964 GNUNET_break (0); 972 GNUNET_break (0);
965 th->sock->th = NULL; 973 th->sock->th = NULL;
966 th->notify (th->notify_cls, 0, NULL); 974 th->notify (th->notify_cls, 0, NULL);
967 GNUNET_free (th); 975 GNUNET_free (th);
968 return; 976 return;
969 } 977 }
970} 978}
971 979
972 980
@@ -989,47 +997,47 @@ client_notify (void *cls, size_t size, void *buf)
989 th->th = NULL; 997 th->th = NULL;
990 th->sock->th = NULL; 998 th->sock->th = NULL;
991 if (buf == NULL) 999 if (buf == NULL)
992 {
993 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
994 delay.rel_value /= 2;
995 if ((0 !=
996 (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
997 (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
998 (delay.rel_value < 1))
999 { 1000 {
1001 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1002 delay.rel_value /= 2;
1003 if ((0 !=
1004 (GNUNET_SCHEDULER_REASON_SHUTDOWN &
1005 GNUNET_SCHEDULER_get_reason ())) || (GNUNET_YES != th->auto_retry)
1006 || (0 == --th->attempts_left) || (delay.rel_value < 1))
1007 {
1000#if DEBUG_CLIENT 1008#if DEBUG_CLIENT
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1002 "Transmission failed %u times, giving up.\n", 1010 "Transmission failed %u times, giving up.\n",
1003 MAX_ATTEMPTS - th->attempts_left); 1011 MAX_ATTEMPTS - th->attempts_left);
1004#endif 1012#endif
1005 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL)); 1013 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1006 GNUNET_free (th); 1014 GNUNET_free (th);
1007 return 0; 1015 return 0;
1008 } 1016 }
1009 /* auto-retry */ 1017 /* auto-retry */
1010#if DEBUG_CLIENT 1018#if DEBUG_CLIENT
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1019 LOG (GNUNET_ERROR_TYPE_DEBUG,
1012 "Failed to connect to `%s', automatically trying again.\n", 1020 "Failed to connect to `%s', automatically trying again.\n",
1013 th->sock->service_name); 1021 th->sock->service_name);
1014#endif 1022#endif
1015 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO); 1023 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
1016 th->sock->sock = NULL; 1024 th->sock->sock = NULL;
1017 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off); 1025 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
1018 th->sock->back_off = 1026 th->sock->back_off =
1019 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply 1027 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1020 (th->sock->back_off, 2), 1028 (th->sock->back_off, 2),
1021 GNUNET_TIME_UNIT_SECONDS); 1029 GNUNET_TIME_UNIT_SECONDS);
1022#if DEBUG_CLIENT 1030#if DEBUG_CLIENT
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1024 "Transmission failed %u times, trying again in %llums.\n", 1032 "Transmission failed %u times, trying again in %llums.\n",
1025 MAX_ATTEMPTS - th->attempts_left, 1033 MAX_ATTEMPTS - th->attempts_left,
1026 (unsigned long long) delay.rel_value); 1034 (unsigned long long) delay.rel_value);
1027#endif 1035#endif
1028 th->sock->th = th; 1036 th->sock->th = th;
1029 th->reconnect_task = 1037 th->reconnect_task =
1030 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); 1038 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1031 return 0; 1039 return 0;
1032 } 1040 }
1033 GNUNET_assert (size >= th->size); 1041 GNUNET_assert (size >= th->size);
1034 ret = th->notify (th->notify_cls, size, buf); 1042 ret = th->notify (th->notify_cls, size, buf);
1035 GNUNET_free (th); 1043 GNUNET_free (th);
@@ -1058,21 +1066,21 @@ client_notify (void *cls, size_t size, void *buf)
1058 */ 1066 */
1059struct GNUNET_CLIENT_TransmitHandle * 1067struct GNUNET_CLIENT_TransmitHandle *
1060GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, 1068GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1061 size_t size, 1069 size_t size,
1062 struct GNUNET_TIME_Relative timeout, 1070 struct GNUNET_TIME_Relative timeout,
1063 int auto_retry, 1071 int auto_retry,
1064 GNUNET_CONNECTION_TransmitReadyNotify 1072 GNUNET_CONNECTION_TransmitReadyNotify
1065 notify, void *notify_cls) 1073 notify, void *notify_cls)
1066{ 1074{
1067 struct GNUNET_CLIENT_TransmitHandle *th; 1075 struct GNUNET_CLIENT_TransmitHandle *th;
1068 1076
1069 if (NULL != sock->th) 1077 if (NULL != sock->th)
1070 { 1078 {
1071 /* If this breaks, you most likley called this function twice without waiting 1079 /* If this breaks, you most likley called this function twice without waiting
1072 * for completion or canceling the request */ 1080 * for completion or canceling the request */
1073 GNUNET_break (0); 1081 GNUNET_break (0);
1074 return NULL; 1082 return NULL;
1075 } 1083 }
1076 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle)); 1084 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1077 th->sock = sock; 1085 th->sock = sock;
1078 th->size = size; 1086 th->size = size;
@@ -1083,25 +1091,25 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1083 th->attempts_left = MAX_ATTEMPTS; 1091 th->attempts_left = MAX_ATTEMPTS;
1084 sock->th = th; 1092 sock->th = th;
1085 if (sock->sock == NULL) 1093 if (sock->sock == NULL)
1086 { 1094 {
1087 th->reconnect_task = 1095 th->reconnect_task =
1088 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry, 1096 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry,
1089 th); 1097 th);
1090 1098
1091 } 1099 }
1092 else 1100 else
1093 {
1094 th->th =
1095 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout,
1096 &client_notify, th);
1097 if (NULL == th->th)
1098 { 1101 {
1099 GNUNET_break (0); 1102 th->th =
1100 GNUNET_free (th); 1103 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout,
1101 sock->th = NULL; 1104 &client_notify, th);
1102 return NULL; 1105 if (NULL == th->th)
1106 {
1107 GNUNET_break (0);
1108 GNUNET_free (th);
1109 sock->th = NULL;
1110 return NULL;
1111 }
1103 } 1112 }
1104 }
1105 return th; 1113 return th;
1106} 1114}
1107 1115
@@ -1112,20 +1120,20 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1112 * @param th handle from the original request. 1120 * @param th handle from the original request.
1113 */ 1121 */
1114void 1122void
1115GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle 1123GNUNET_CLIENT_notify_transmit_ready_cancel (struct
1116 *th) 1124 GNUNET_CLIENT_TransmitHandle *th)
1117{ 1125{
1118 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK) 1126 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1119 { 1127 {
1120 GNUNET_assert (NULL == th->th); 1128 GNUNET_assert (NULL == th->th);
1121 GNUNET_SCHEDULER_cancel (th->reconnect_task); 1129 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1122 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 1130 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1123 } 1131 }
1124 else 1132 else
1125 { 1133 {
1126 GNUNET_assert (NULL != th->th); 1134 GNUNET_assert (NULL != th->th);
1127 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th); 1135 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1128 } 1136 }
1129 th->sock->th = NULL; 1137 th->sock->th = NULL;
1130 GNUNET_free (th); 1138 GNUNET_free (th);
1131} 1139}
@@ -1151,21 +1159,21 @@ transmit_for_response (void *cls, size_t size, void *buf)
1151 tc->sock->tag = NULL; 1159 tc->sock->tag = NULL;
1152 msize = ntohs (tc->hdr->size); 1160 msize = ntohs (tc->hdr->size);
1153 if (NULL == buf) 1161 if (NULL == buf)
1154 { 1162 {
1155#if DEBUG_CLIENT 1163#if DEBUG_CLIENT
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1164 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 _ 1165 _
1158 ("Could not submit request, not expecting to receive a response.\n")); 1166 ("Could not submit request, not expecting to receive a response.\n"));
1159#endif 1167#endif
1160 if (NULL != tc->rn) 1168 if (NULL != tc->rn)
1161 tc->rn (tc->rn_cls, NULL); 1169 tc->rn (tc->rn_cls, NULL);
1162 GNUNET_free (tc); 1170 GNUNET_free (tc);
1163 return 0; 1171 return 0;
1164 } 1172 }
1165 GNUNET_assert (size >= msize); 1173 GNUNET_assert (size >= msize);
1166 memcpy (buf, tc->hdr, msize); 1174 memcpy (buf, tc->hdr, msize);
1167 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls, 1175 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls,
1168 GNUNET_TIME_absolute_get_remaining (tc->timeout)); 1176 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1169 GNUNET_free (tc); 1177 GNUNET_free (tc);
1170 return msize; 1178 return msize;
1171} 1179}
@@ -1193,12 +1201,14 @@ transmit_for_response (void *cls, size_t size, void *buf)
1193 * is already pending 1201 * is already pending
1194 */ 1202 */
1195int 1203int
1196GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, 1204GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1197 const struct GNUNET_MessageHeader *hdr, 1205 *sock,
1198 struct GNUNET_TIME_Relative timeout, 1206 const struct GNUNET_MessageHeader
1199 int auto_retry, 1207 *hdr,
1200 GNUNET_CLIENT_MessageHandler rn, 1208 struct GNUNET_TIME_Relative timeout,
1201 void *rn_cls) 1209 int auto_retry,
1210 GNUNET_CLIENT_MessageHandler rn,
1211 void *rn_cls)
1202{ 1212{
1203 struct TransmitGetResponseContext *tc; 1213 struct TransmitGetResponseContext *tc;
1204 uint16_t msize; 1214 uint16_t msize;
@@ -1216,12 +1226,12 @@ GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
1216 tc->rn_cls = rn_cls; 1226 tc->rn_cls = rn_cls;
1217 if (NULL == 1227 if (NULL ==
1218 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry, 1228 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry,
1219 &transmit_for_response, tc)) 1229 &transmit_for_response, tc))
1220 { 1230 {
1221 GNUNET_break (0); 1231 GNUNET_break (0);
1222 GNUNET_free (tc); 1232 GNUNET_free (tc);
1223 return GNUNET_SYSERR; 1233 return GNUNET_SYSERR;
1224 } 1234 }
1225 sock->tag = tc; 1235 sock->tag = tc;
1226 return GNUNET_OK; 1236 return GNUNET_OK;
1227} 1237}
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c
index 98c89d86a..31da5829b 100644
--- a/src/util/common_allocation.c
+++ b/src/util/common_allocation.c
@@ -27,6 +27,10 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29 29
30#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
31
32#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
33
30#ifndef INT_MAX 34#ifndef INT_MAX
31#define INT_MAX 0x7FFFFFFF 35#define INT_MAX 0x7FFFFFFF
32#endif 36#endif
@@ -61,10 +65,10 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
61 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); 65 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
62 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber); 66 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
63 if (ret == NULL) 67 if (ret == NULL)
64 { 68 {
65 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); 69 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
66 abort (); 70 abort ();
67 } 71 }
68 return ret; 72 return ret;
69} 73}
70 74
@@ -82,7 +86,7 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
82 */ 86 */
83void * 87void *
84GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, 88GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
85 int linenumber) 89 int linenumber)
86{ 90{
87 void *ret; 91 void *ret;
88 92
@@ -97,10 +101,10 @@ GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
97 GNUNET_assert_at (size < INT_MAX, filename, linenumber); 101 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
98 ret = malloc (size); 102 ret = malloc (size);
99 if (ret == NULL) 103 if (ret == NULL)
100 { 104 {
101 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); 105 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
102 abort (); 106 abort ();
103 } 107 }
104#ifdef W32_MEM_LIMIT 108#ifdef W32_MEM_LIMIT
105 *((size_t *) ret) = size; 109 *((size_t *) ret) = size;
106 ret = &((size_t *) ret)[1]; 110 ret = &((size_t *) ret)[1];
@@ -168,10 +172,10 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
168#endif 172#endif
169 ptr = realloc (ptr, n); 173 ptr = realloc (ptr, n);
170 if ((NULL == ptr) && (n > 0)) 174 if ((NULL == ptr) && (n > 0))
171 { 175 {
172 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc"); 176 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
173 abort (); 177 abort ();
174 } 178 }
175#ifdef W32_MEM_LIMIT 179#ifdef W32_MEM_LIMIT
176 ptr = &((size_t *) ptr)[1]; 180 ptr = &((size_t *) ptr)[1];
177#endif 181#endif
@@ -229,7 +233,7 @@ GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
229 */ 233 */
230char * 234char *
231GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, 235GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
232 int linenumber) 236 int linenumber)
233{ 237{
234 char *res; 238 char *res;
235 239
@@ -256,7 +260,7 @@ GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
256 */ 260 */
257void 261void
258GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, 262GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
259 unsigned int newCount, const char *filename, int linenumber) 263 unsigned int newCount, const char *filename, int linenumber)
260{ 264{
261 void *tmp; 265 void *tmp;
262 size_t size; 266 size_t size;
@@ -264,22 +268,22 @@ GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
264 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber); 268 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
265 size = newCount * elementSize; 269 size = newCount * elementSize;
266 if (size == 0) 270 if (size == 0)
267 { 271 {
268 tmp = NULL; 272 tmp = NULL;
269 } 273 }
270 else 274 else
271 { 275 {
272 tmp = GNUNET_xmalloc_ (size, filename, linenumber); 276 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
273 memset (tmp, 0, size); /* client code should not rely on this, though... */ 277 memset (tmp, 0, size); /* client code should not rely on this, though... */
274 if (*oldCount > newCount) 278 if (*oldCount > newCount)
275 *oldCount = newCount; /* shrink is also allowed! */ 279 *oldCount = newCount; /* shrink is also allowed! */
276 memcpy (tmp, *old, elementSize * (*oldCount)); 280 memcpy (tmp, *old, elementSize * (*oldCount));
277 } 281 }
278 282
279 if (*old != NULL) 283 if (*old != NULL)
280 { 284 {
281 GNUNET_xfree_ (*old, filename, linenumber); 285 GNUNET_xfree_ (*old, filename, linenumber);
282 } 286 }
283 *old = tmp; 287 *old = tmp;
284 *oldCount = newCount; 288 *oldCount = newCount;
285} 289}
diff --git a/src/util/common_endian.c b/src/util/common_endian.c
index 223144c40..5d7aa50d5 100644
--- a/src/util/common_endian.c
+++ b/src/util/common_endian.c
@@ -27,6 +27,8 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29 29
30#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
31
30unsigned long long 32unsigned long long
31GNUNET_ntohll (unsigned long long n) 33GNUNET_ntohll (unsigned long long n)
32{ 34{
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index ccee8e028..fd32036d0 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -251,6 +251,7 @@ get_type (const char *log)
251 return GNUNET_ERROR_TYPE_NONE; 251 return GNUNET_ERROR_TYPE_NONE;
252 return GNUNET_ERROR_TYPE_INVALID; 252 return GNUNET_ERROR_TYPE_INVALID;
253} 253}
254
254#if !defined(GNUNET_CULL_LOGGING) 255#if !defined(GNUNET_CULL_LOGGING)
255/** 256/**
256 * Utility function - reallocates logdefs array to be twice as large. 257 * Utility function - reallocates logdefs array to be twice as large.
@@ -258,8 +259,8 @@ get_type (const char *log)
258static void 259static void
259resize_logdefs () 260resize_logdefs ()
260{ 261{
261 logdefs_size = (logdefs_size + 1) * 2; 262 logdefs_size = (logdefs_size + 1) * 2;
262 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef)); 263 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
263} 264}
264 265
265/** 266/**
@@ -274,7 +275,8 @@ resize_logdefs ()
274 * @param force see struct LogDef 275 * @param force see struct LogDef
275 */ 276 */
276static void 277static void
277add_definition (char *component, char *file, char *function, int from_line, int to_line, int level, int force) 278add_definition (char *component, char *file, char *function, int from_line,
279 int to_line, int level, int force)
278{ 280{
279 if (logdefs_size == logdefs_len) 281 if (logdefs_size == logdefs_len)
280 resize_logdefs (); 282 resize_logdefs ();
@@ -283,10 +285,10 @@ add_definition (char *component, char *file, char *function, int from_line, int
283 if (strlen (component) > 0 && component[0] != '*') 285 if (strlen (component) > 0 && component[0] != '*')
284 n.component = strdup (component); 286 n.component = strdup (component);
285 if (strlen (file) > 0 && file[0] != '*') 287 if (strlen (file) > 0 && file[0] != '*')
286 { 288 {
287 n.file = strdup (file); 289 n.file = strdup (file);
288 n.strlen_file = strlen (file); 290 n.strlen_file = strlen (file);
289 } 291 }
290 if (strlen (function) > 0 && function[0] != '*') 292 if (strlen (function) > 0 && function[0] != '*')
291 n.function = strdup (function); 293 n.function = strdup (function);
292 n.from_line = from_line; 294 n.from_line = from_line;
@@ -310,8 +312,9 @@ add_definition (char *component, char *file, char *function, int from_line, int
310 * @param line line at which the call is made, usually __LINE__ 312 * @param line line at which the call is made, usually __LINE__
311 * @return 0 to disallow the call, 1 to allow it 313 * @return 0 to disallow the call, 1 to allow it
312 */ 314 */
313int 315int
314GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file, const char *function, int line) 316GNUNET_get_log_call_status (int caller_level, const char *comp,
317 const char *file, const char *function, int line)
315{ 318{
316 struct LogDef *ld; 319 struct LogDef *ld;
317 int i; 320 int i;
@@ -332,21 +335,20 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file
332 force_only = min_level >= 0; 335 force_only = min_level >= 0;
333 strlen_file = strlen (file); 336 strlen_file = strlen (file);
334 for (i = 0; i < logdefs_len; i++) 337 for (i = 0; i < logdefs_len; i++)
335 {
336 ld = &logdefs[i];
337 if ((!force_only || ld->force) &&
338 (line >= ld->from_line && line <= ld->to_line) &&
339 (ld->component == NULL || strcmp (comp, ld->component) == 0) &&
340 (ld->file == NULL ||
341 (ld->strlen_file <= strlen_file &&
342 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
343 (ld->function == NULL || strcmp (function, ld->function) == 0)
344 )
345 { 338 {
346 /* We're finished */ 339 ld = &logdefs[i];
347 return caller_level <= ld->level; 340 if ((!force_only || ld->force) &&
341 (line >= ld->from_line && line <= ld->to_line) &&
342 (ld->component == NULL || strcmp (comp, ld->component) == 0) &&
343 (ld->file == NULL ||
344 (ld->strlen_file <= strlen_file &&
345 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
346 (ld->function == NULL || strcmp (function, ld->function) == 0))
347 {
348 /* We're finished */
349 return caller_level <= ld->level;
350 }
348 } 351 }
349 }
350 /* No matches - use global level, if defined */ 352 /* No matches - use global level, if defined */
351 if (min_level >= 0) 353 if (min_level >= 0)
352 return caller_level <= min_level; 354 return caller_level <= min_level;
@@ -404,83 +406,85 @@ parse_definitions (const char *constname, int force)
404 from_line = 0; 406 from_line = 0;
405 to_line = INT_MAX; 407 to_line = INT_MAX;
406 for (p = def, state = 0, start = def; keep_looking; p++) 408 for (p = def, state = 0, start = def; keep_looking; p++)
407 {
408 switch (p[0])
409 { 409 {
410 case ';': /* found a field separator */ 410 switch (p[0])
411 p[0] = '\0'; 411 {
412 switch (state) 412 case ';': /* found a field separator */
413 { 413 p[0] = '\0';
414 case 0: /* within a component name */ 414 switch (state)
415 comp = start; 415 {
416 break; 416 case 0: /* within a component name */
417 case 1: /* within a file name */ 417 comp = start;
418 file = start; 418 break;
419 break; 419 case 1: /* within a file name */
420 case 2: /* within a function name */ 420 file = start;
421 /* after a file name there must be a function name */ 421 break;
422 function = start; 422 case 2: /* within a function name */
423 break; 423 /* after a file name there must be a function name */
424 case 3: /* within a from-to line range */ 424 function = start;
425 if (strlen (start) > 0) 425 break;
426 { 426 case 3: /* within a from-to line range */
427 errno = 0; 427 if (strlen (start) > 0)
428 from_line = strtol (start, &t, 10); 428 {
429 if (errno != 0 || from_line < 0) 429 errno = 0;
430 { 430 from_line = strtol (start, &t, 10);
431 free (def); 431 if (errno != 0 || from_line < 0)
432 return counter; 432 {
433 } 433 free (def);
434 if (t < p && t[0] == '-') 434 return counter;
435 { 435 }
436 errno = 0; 436 if (t < p && t[0] == '-')
437 start = t + 1; 437 {
438 to_line = strtol (start, &t, 10); 438 errno = 0;
439 if (errno != 0 || to_line < 0 || t != p) 439 start = t + 1;
440 { 440 to_line = strtol (start, &t, 10);
441 free (def); 441 if (errno != 0 || to_line < 0 || t != p)
442 return counter; 442 {
443 } 443 free (def);
444 } 444 return counter;
445 else /* one number means "match this line only" */ 445 }
446 to_line = from_line; 446 }
447 } 447 else /* one number means "match this line only" */
448 else /* default to 0-max */ 448 to_line = from_line;
449 { 449 }
450 from_line = 0; 450 else /* default to 0-max */
451 to_line = INT_MAX; 451 {
452 } 452 from_line = 0;
453 break; 453 to_line = INT_MAX;
454 } 454 }
455 start = p + 1; 455 break;
456 state += 1; 456 }
457 break; 457 start = p + 1;
458 case '\0': /* found EOL */ 458 state += 1;
459 keep_looking = 0; 459 break;
460 /* fall through to '/' */ 460 case '\0': /* found EOL */
461 case '/': /* found a definition separator */ 461 keep_looking = 0;
462 switch (state) 462 /* fall through to '/' */
463 { 463 case '/': /* found a definition separator */
464 case 4: /* within a log level */ 464 switch (state)
465 p[0] = '\0'; 465 {
466 state = 0; 466 case 4: /* within a log level */
467 level = get_type ((const char *) start); 467 p[0] = '\0';
468 if (level == GNUNET_ERROR_TYPE_INVALID || level == GNUNET_ERROR_TYPE_UNSPECIFIED) 468 state = 0;
469 { 469 level = get_type ((const char *) start);
470 free (def); 470 if (level == GNUNET_ERROR_TYPE_INVALID
471 return counter; 471 || level == GNUNET_ERROR_TYPE_UNSPECIFIED)
472 } 472 {
473 add_definition (comp, file, function, from_line, to_line, level, force); 473 free (def);
474 counter += 1; 474 return counter;
475 start = p + 1; 475 }
476 break; 476 add_definition (comp, file, function, from_line, to_line, level,
477 default: 477 force);
478 break; 478 counter += 1;
479 } 479 start = p + 1;
480 default: 480 break;
481 break; 481 default:
482 break;
483 }
484 default:
485 break;
486 }
482 } 487 }
483 }
484 free (def); 488 free (def);
485 return counter; 489 return counter;
486} 490}
@@ -495,7 +499,8 @@ parse_all_definitions ()
495 parse_definitions ("GNUNET_LOG", 0); 499 parse_definitions ("GNUNET_LOG", 0);
496 gnunet_log_parsed = GNUNET_YES; 500 gnunet_log_parsed = GNUNET_YES;
497 if (gnunet_force_log_parsed == GNUNET_NO) 501 if (gnunet_force_log_parsed == GNUNET_NO)
498 gnunet_force_log_present = parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; 502 gnunet_force_log_present =
503 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
499 gnunet_force_log_parsed = GNUNET_YES; 504 gnunet_force_log_parsed = GNUNET_YES;
500} 505}
501#endif 506#endif
@@ -539,15 +544,16 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
539 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); 544 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
540 altlog = FOPEN (fn, "a"); 545 altlog = FOPEN (fn, "a");
541 if (altlog == NULL) 546 if (altlog == NULL)
542 { 547 {
543 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); 548 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
544 if (dirwarn) 549 if (dirwarn)
545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 550 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
546 _("Failed to create or access directory for log file `%s'\n"), 551 _
547 fn); 552 ("Failed to create or access directory for log file `%s'\n"),
548 GNUNET_free (fn); 553 fn);
549 return GNUNET_SYSERR; 554 GNUNET_free (fn);
550 } 555 return GNUNET_SYSERR;
556 }
551 GNUNET_free (fn); 557 GNUNET_free (fn);
552 if (GNUNET_stderr != NULL) 558 if (GNUNET_stderr != NULL)
553 fclose (GNUNET_stderr); 559 fclose (GNUNET_stderr);
@@ -588,11 +594,11 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
588 prev = NULL; 594 prev = NULL;
589 pos = loggers; 595 pos = loggers;
590 while ((pos != NULL) && 596 while ((pos != NULL) &&
591 ((pos->logger != logger) || (pos->logger_cls != logger_cls))) 597 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
592 { 598 {
593 prev = pos; 599 prev = pos;
594 pos = pos->next; 600 pos = pos->next;
595 } 601 }
596 GNUNET_assert (pos != NULL); 602 GNUNET_assert (pos != NULL);
597 if (prev == NULL) 603 if (prev == NULL)
598 loggers = pos->next; 604 loggers = pos->next;
@@ -612,22 +618,22 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
612 */ 618 */
613static void 619static void
614output_message (enum GNUNET_ErrorType kind, const char *comp, 620output_message (enum GNUNET_ErrorType kind, const char *comp,
615 const char *datestr, const char *msg) 621 const char *datestr, const char *msg)
616{ 622{
617 struct CustomLogger *pos; 623 struct CustomLogger *pos;
618 624
619 if (GNUNET_stderr != NULL) 625 if (GNUNET_stderr != NULL)
620 { 626 {
621 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, 627 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
622 GNUNET_error_type_to_string (kind), msg); 628 GNUNET_error_type_to_string (kind), msg);
623 fflush (GNUNET_stderr); 629 fflush (GNUNET_stderr);
624 } 630 }
625 pos = loggers; 631 pos = loggers;
626 while (pos != NULL) 632 while (pos != NULL)
627 { 633 {
628 pos->logger (pos->logger_cls, kind, comp, datestr, msg); 634 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
629 pos = pos->next; 635 pos = pos->next;
630 } 636 }
631} 637}
632 638
633 639
@@ -653,15 +659,16 @@ flush_bulk (const char *datestr)
653 else if (last != last_bulk) 659 else if (last != last_bulk)
654 last--; 660 last--;
655 if (last[0] == '\n') 661 if (last[0] == '\n')
656 { 662 {
657 rev = 1; 663 rev = 1;
658 last[0] = '\0'; 664 last[0] = '\0';
659 } 665 }
660 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration 666 ft =
661 (last_bulk_time)); 667 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
668 (last_bulk_time));
662 snprintf (msg, sizeof (msg), 669 snprintf (msg, sizeof (msg),
663 _("Message `%.*s' repeated %u times in the last %s\n"), 670 _("Message `%.*s' repeated %u times in the last %s\n"),
664 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); 671 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
665 GNUNET_free (ft); 672 GNUNET_free (ft);
666 if (rev == 1) 673 if (rev == 1)
667 last[0] = '\n'; 674 last[0] = '\n';
@@ -681,14 +688,14 @@ void
681GNUNET_log_skip (unsigned int n, int check_reset) 688GNUNET_log_skip (unsigned int n, int check_reset)
682{ 689{
683 if (n == 0) 690 if (n == 0)
684 { 691 {
685 int ok; 692 int ok;
686 693
687 ok = (0 == skip_log); 694 ok = (0 == skip_log);
688 skip_log = 0; 695 skip_log = 0;
689 if (check_reset) 696 if (check_reset)
690 GNUNET_assert (ok); 697 GNUNET_assert (ok);
691 } 698 }
692 else 699 else
693 skip_log += n; 700 skip_log += n;
694} 701}
@@ -720,40 +727,42 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message,
720 va_end (vacp); 727 va_end (vacp);
721 buf = malloc (size); 728 buf = malloc (size);
722 if (buf == NULL) 729 if (buf == NULL)
723 return; /* oops */ 730 return; /* oops */
724 VSNPRINTF (buf, size, message, va); 731 VSNPRINTF (buf, size, message, va);
725 time (&timetmp); 732 time (&timetmp);
726 memset (date, 0, DATE_STR_SIZE); 733 memset (date, 0, DATE_STR_SIZE);
727 tmptr = localtime (&timetmp); 734 tmptr = localtime (&timetmp);
728 gettimeofday (&timeofday, NULL); 735 gettimeofday (&timeofday, NULL);
729 if (NULL != tmptr) 736 if (NULL != tmptr)
730 { 737 {
731#ifdef WINDOWS 738#ifdef WINDOWS
732 LARGE_INTEGER pc; 739 LARGE_INTEGER pc;
733 740
734 pc.QuadPart = 0; 741 pc.QuadPart = 0;
735 QueryPerformanceCounter (&pc); 742 QueryPerformanceCounter (&pc);
736 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); 743 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
737 snprintf (date, sizeof (date), date2, 744 snprintf (date, sizeof (date), date2,
738 (long long) (pc.QuadPart / 745 (long long) (pc.QuadPart /
739 (performance_frequency.QuadPart / 1000))); 746 (performance_frequency.QuadPart / 1000)));
740#else 747#else
741 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); 748 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
742 snprintf (date, sizeof (date), date2, timeofday.tv_usec); 749 snprintf (date, sizeof (date), date2, timeofday.tv_usec);
743#endif 750#endif
744 } 751 }
745 else 752 else
746 strcpy (date, "localtime error"); 753 strcpy (date, "localtime error");
747 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && (last_bulk_time.abs_value != 0) 754 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK))
755 && (last_bulk_time.abs_value != 0)
748 && (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) 756 && (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
749 { 757 {
750 last_bulk_repeat++; 758 last_bulk_repeat++;
751 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > 759 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value >
752 BULK_DELAY_THRESHOLD) || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) 760 BULK_DELAY_THRESHOLD)
753 flush_bulk (date); 761 || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
754 free (buf); 762 flush_bulk (date);
755 return; 763 free (buf);
756 } 764 return;
765 }
757 flush_bulk (date); 766 flush_bulk (date);
758 strncpy (last_bulk, buf, sizeof (last_bulk)); 767 strncpy (last_bulk, buf, sizeof (last_bulk));
759 last_bulk_repeat = 0; 768 last_bulk_repeat = 0;
@@ -794,7 +803,7 @@ GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
794 */ 803 */
795void 804void
796GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp, 805GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
797 const char *message, ...) 806 const char *message, ...)
798{ 807{
799 va_list va; 808 va_list va;
800 char comp_w_pid[128]; 809 char comp_w_pid[128];
@@ -912,44 +921,44 @@ GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
912 if (addr == NULL) 921 if (addr == NULL)
913 return _("unknown address"); 922 return _("unknown address");
914 switch (addr->sa_family) 923 switch (addr->sa_family)
915 { 924 {
916 case AF_INET: 925 case AF_INET:
917 if (addrlen != sizeof (struct sockaddr_in)) 926 if (addrlen != sizeof (struct sockaddr_in))
918 return "<invalid v4 address>"; 927 return "<invalid v4 address>";
919 v4 = (const struct sockaddr_in *) addr; 928 v4 = (const struct sockaddr_in *) addr;
920 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); 929 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
921 if (0 == ntohs (v4->sin_port)) 930 if (0 == ntohs (v4->sin_port))
931 return buf;
932 strcat (buf, ":");
933 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
934 strcat (buf, b2);
935 return buf;
936 case AF_INET6:
937 if (addrlen != sizeof (struct sockaddr_in6))
938 return "<invalid v4 address>";
939 v6 = (const struct sockaddr_in6 *) addr;
940 buf[0] = '[';
941 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
942 if (0 == ntohs (v6->sin6_port))
943 return &buf[1];
944 strcat (buf, "]:");
945 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
946 strcat (buf, b2);
922 return buf; 947 return buf;
923 strcat (buf, ":"); 948 case AF_UNIX:
924 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port)); 949 if (addrlen <= sizeof (sa_family_t))
925 strcat (buf, b2); 950 return "<unbound UNIX client>";
926 return buf; 951 un = (const struct sockaddr_un *) addr;
927 case AF_INET6: 952 off = 0;
928 if (addrlen != sizeof (struct sockaddr_in6)) 953 if (un->sun_path[0] == '\0')
929 return "<invalid v4 address>"; 954 off++;
930 v6 = (const struct sockaddr_in6 *) addr; 955 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
931 buf[0] = '['; 956 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
932 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN); 957 &un->sun_path[off]);
933 if (0 == ntohs (v6->sin6_port)) 958 return buf;
934 return &buf[1]; 959 default:
935 strcat (buf, "]:"); 960 return _("invalid address");
936 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port)); 961 }
937 strcat (buf, b2);
938 return buf;
939 case AF_UNIX:
940 if (addrlen <= sizeof (sa_family_t))
941 return "<unbound UNIX client>";
942 un = (const struct sockaddr_un *) addr;
943 off = 0;
944 if (un->sun_path[0] == '\0')
945 off++;
946 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
947 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
948 &un->sun_path[off]);
949 return buf;
950 default:
951 return _("invalid address");
952 }
953} 962}
954 963
955 964
diff --git a/src/util/configuration.c b/src/util/configuration.c
index c69042175..adfd4ea5a 100644
--- a/src/util/configuration.c
+++ b/src/util/configuration.c
@@ -33,6 +33,9 @@
33#include "gnunet_os_lib.h" 33#include "gnunet_os_lib.h"
34#include "gnunet_strings_lib.h" 34#include "gnunet_strings_lib.h"
35 35
36#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37
38#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36 39
37/** 40/**
38 * @brief configuration entry 41 * @brief configuration entry
@@ -149,7 +152,7 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
149 */ 152 */
150int 153int
151GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, 154GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
152 const char *filename) 155 const char *filename)
153{ 156{
154 int dirty; 157 int dirty;
155 char line[256]; 158 char line[256];
@@ -166,87 +169,89 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
166 fn = GNUNET_STRINGS_filename_expand (filename); 169 fn = GNUNET_STRINGS_filename_expand (filename);
167 if (fn == NULL) 170 if (fn == NULL)
168 return GNUNET_SYSERR; 171 return GNUNET_SYSERR;
169 dirty = cfg->dirty; /* back up value! */ 172 dirty = cfg->dirty; /* back up value! */
170 if (NULL == (fp = FOPEN (fn, "r"))) 173 if (NULL == (fp = FOPEN (fn, "r")))
171 { 174 {
172 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); 175 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
173 GNUNET_free (fn); 176 GNUNET_free (fn);
174 return GNUNET_SYSERR; 177 return GNUNET_SYSERR;
175 } 178 }
176 GNUNET_free (fn); 179 GNUNET_free (fn);
177 ret = GNUNET_OK; 180 ret = GNUNET_OK;
178 section = GNUNET_strdup (""); 181 section = GNUNET_strdup ("");
179 memset (line, 0, 256); 182 memset (line, 0, 256);
180 nr = 0; 183 nr = 0;
181 while (NULL != fgets (line, 255, fp)) 184 while (NULL != fgets (line, 255, fp))
182 {
183 nr++;
184 for (i = 0; i < 255; i++)
185 if (line[i] == '\t')
186 line[i] = ' ';
187 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r')
188 continue;
189 emptyline = 1;
190 for (i = 0; (i < 255 && line[i] != 0); i++)
191 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
192 emptyline = 0;
193 if (emptyline == 1)
194 continue;
195 /* remove tailing whitespace */
196 for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
197 i--)
198 line[i] = '\0';
199 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
200 {
201 /* @INLINE@ value */
202 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
203 ret = GNUNET_SYSERR; /* failed to parse included config */
204 }
205 else if (1 == sscanf (line, "[%99[^]]]", value))
206 {
207 /* [value] */
208 GNUNET_free (section);
209 section = GNUNET_strdup (value);
210 }
211 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
212 {
213 /* tag = value */
214 /* Strip LF */
215 i = strlen (value) - 1;
216 while ((i >= 0) && (isspace ((unsigned char) value[i])))
217 value[i--] = '\0';
218 /* remove quotes */
219 i = 0;
220 if (value[0] == '"')
221 {
222 i = 1;
223 while ((value[i] != '\0') && (value[i] != '"'))
224 i++;
225 if (value[i] == '"')
226 {
227 value[i] = '\0';
228 i = 1;
229 }
230 else
231 i = 0;
232 }
233 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
234 }
235 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
236 {
237 /* tag = */
238 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
239 }
240 else
241 { 185 {
242 /* parse error */ 186 nr++;
243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 187 for (i = 0; i < 255; i++)
244 _("Syntax error in configuration file `%s' at line %u.\n"), 188 if (line[i] == '\t')
245 filename, nr); 189 line[i] = ' ';
246 ret = GNUNET_SYSERR; 190 if (line[0] == '\n' || line[0] == '#' || line[0] == '%'
247 break; 191 || line[0] == '\r')
192 continue;
193 emptyline = 1;
194 for (i = 0; (i < 255 && line[i] != 0); i++)
195 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
196 emptyline = 0;
197 if (emptyline == 1)
198 continue;
199 /* remove tailing whitespace */
200 for (i = strlen (line) - 1;
201 (i >= 0) && (isspace ((unsigned char) line[i])); i--)
202 line[i] = '\0';
203 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
204 {
205 /* @INLINE@ value */
206 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
207 ret = GNUNET_SYSERR; /* failed to parse included config */
208 }
209 else if (1 == sscanf (line, "[%99[^]]]", value))
210 {
211 /* [value] */
212 GNUNET_free (section);
213 section = GNUNET_strdup (value);
214 }
215 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
216 {
217 /* tag = value */
218 /* Strip LF */
219 i = strlen (value) - 1;
220 while ((i >= 0) && (isspace ((unsigned char) value[i])))
221 value[i--] = '\0';
222 /* remove quotes */
223 i = 0;
224 if (value[0] == '"')
225 {
226 i = 1;
227 while ((value[i] != '\0') && (value[i] != '"'))
228 i++;
229 if (value[i] == '"')
230 {
231 value[i] = '\0';
232 i = 1;
233 }
234 else
235 i = 0;
236 }
237 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag,
238 &value[i]);
239 }
240 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
241 {
242 /* tag = */
243 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
244 }
245 else
246 {
247 /* parse error */
248 LOG (GNUNET_ERROR_TYPE_WARNING,
249 _("Syntax error in configuration file `%s' at line %u.\n"),
250 filename, nr);
251 ret = GNUNET_SYSERR;
252 break;
253 }
248 } 254 }
249 }
250 GNUNET_assert (0 == fclose (fp)); 255 GNUNET_assert (0 == fclose (fp));
251 /* restore dirty flag - anything we set in the meantime 256 /* restore dirty flag - anything we set in the meantime
252 * came from disk */ 257 * came from disk */
@@ -279,7 +284,7 @@ GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
279 */ 284 */
280int 285int
281GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, 286GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
282 const char *filename) 287 const char *filename)
283{ 288{
284 struct ConfigSection *sec; 289 struct ConfigSection *sec;
285 struct ConfigEntry *ent; 290 struct ConfigEntry *ent;
@@ -293,67 +298,67 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
293 if (fn == NULL) 298 if (fn == NULL)
294 return GNUNET_SYSERR; 299 return GNUNET_SYSERR;
295 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)) 300 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
296 { 301 {
297 GNUNET_free (fn); 302 GNUNET_free (fn);
298 return GNUNET_SYSERR; 303 return GNUNET_SYSERR;
299 } 304 }
300 if (NULL == (fp = FOPEN (fn, "w"))) 305 if (NULL == (fp = FOPEN (fn, "w")))
301 { 306 {
302 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); 307 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
303 GNUNET_free (fn); 308 GNUNET_free (fn);
304 return GNUNET_SYSERR; 309 return GNUNET_SYSERR;
305 } 310 }
306 GNUNET_free (fn); 311 GNUNET_free (fn);
307 error = 0; 312 error = 0;
308 sec = cfg->sections; 313 sec = cfg->sections;
309 while (sec != NULL) 314 while (sec != NULL)
310 {
311 if (0 > fprintf (fp, "[%s]\n", sec->name))
312 {
313 error = 1;
314 break;
315 }
316 ent = sec->entries;
317 while (ent != NULL)
318 {
319 if (ent->val != NULL)
320 {
321 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
322 strcpy (val, ent->val);
323 while (NULL != (pos = strstr (val, "\n")))
324 {
325 memmove (&pos[2], &pos[1], strlen (&pos[1]));
326 pos[0] = '\\';
327 pos[1] = 'n';
328 }
329 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
330 {
331 error = 1;
332 GNUNET_free (val);
333 break;
334 }
335 GNUNET_free (val);
336 }
337 ent = ent->next;
338 }
339 if (error != 0)
340 break;
341 if (0 > fprintf (fp, "\n"))
342 { 315 {
343 error = 1; 316 if (0 > fprintf (fp, "[%s]\n", sec->name))
344 break; 317 {
318 error = 1;
319 break;
320 }
321 ent = sec->entries;
322 while (ent != NULL)
323 {
324 if (ent->val != NULL)
325 {
326 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
327 strcpy (val, ent->val);
328 while (NULL != (pos = strstr (val, "\n")))
329 {
330 memmove (&pos[2], &pos[1], strlen (&pos[1]));
331 pos[0] = '\\';
332 pos[1] = 'n';
333 }
334 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
335 {
336 error = 1;
337 GNUNET_free (val);
338 break;
339 }
340 GNUNET_free (val);
341 }
342 ent = ent->next;
343 }
344 if (error != 0)
345 break;
346 if (0 > fprintf (fp, "\n"))
347 {
348 error = 1;
349 break;
350 }
351 sec = sec->next;
345 } 352 }
346 sec = sec->next;
347 }
348 if (error != 0) 353 if (error != 0)
349 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename); 354 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
350 GNUNET_assert (0 == fclose (fp)); 355 GNUNET_assert (0 == fclose (fp));
351 if (error != 0) 356 if (error != 0)
352 { 357 {
353 cfg->dirty = GNUNET_SYSERR; /* last write failed */ 358 cfg->dirty = GNUNET_SYSERR; /* last write failed */
354 return GNUNET_SYSERR; 359 return GNUNET_SYSERR;
355 } 360 }
356 cfg->dirty = GNUNET_NO; /* last write succeeded */ 361 cfg->dirty = GNUNET_NO; /* last write succeeded */
357 return GNUNET_OK; 362 return GNUNET_OK;
358} 363}
359 364
@@ -367,23 +372,23 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
367 */ 372 */
368void 373void
369GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, 374GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
370 GNUNET_CONFIGURATION_Iterator iter, 375 GNUNET_CONFIGURATION_Iterator iter,
371 void *iter_cls) 376 void *iter_cls)
372{ 377{
373 struct ConfigSection *spos; 378 struct ConfigSection *spos;
374 struct ConfigEntry *epos; 379 struct ConfigEntry *epos;
375 380
376 spos = cfg->sections; 381 spos = cfg->sections;
377 while (spos != NULL) 382 while (spos != NULL)
378 {
379 epos = spos->entries;
380 while (epos != NULL)
381 { 383 {
382 iter (iter_cls, spos->name, epos->key, epos->val); 384 epos = spos->entries;
383 epos = epos->next; 385 while (epos != NULL)
386 {
387 iter (iter_cls, spos->name, epos->key, epos->val);
388 epos = epos->next;
389 }
390 spos = spos->next;
384 } 391 }
385 spos = spos->next;
386 }
387} 392}
388 393
389 394
@@ -397,10 +402,10 @@ GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
397 */ 402 */
398void 403void
399GNUNET_CONFIGURATION_iterate_section_values (const struct 404GNUNET_CONFIGURATION_iterate_section_values (const struct
400 GNUNET_CONFIGURATION_Handle *cfg, 405 GNUNET_CONFIGURATION_Handle *cfg,
401 const char *section, 406 const char *section,
402 GNUNET_CONFIGURATION_Iterator iter, 407 GNUNET_CONFIGURATION_Iterator
403 void *iter_cls) 408 iter, void *iter_cls)
404{ 409{
405 struct ConfigSection *spos; 410 struct ConfigSection *spos;
406 struct ConfigEntry *epos; 411 struct ConfigEntry *epos;
@@ -414,10 +419,10 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct
414 419
415 epos = spos->entries; 420 epos = spos->entries;
416 while (epos != NULL) 421 while (epos != NULL)
417 { 422 {
418 iter (iter_cls, spos->name, epos->key, epos->val); 423 iter (iter_cls, spos->name, epos->key, epos->val);
419 epos = epos->next; 424 epos = epos->next;
420 } 425 }
421} 426}
422 427
423 428
@@ -429,21 +434,21 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct
429 * @param iter_cls closure for iter 434 * @param iter_cls closure for iter
430 */ 435 */
431void 436void
432GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle 437GNUNET_CONFIGURATION_iterate_sections (const struct
433 *cfg, 438 GNUNET_CONFIGURATION_Handle *cfg,
434 GNUNET_CONFIGURATION_Section_Iterator 439 GNUNET_CONFIGURATION_Section_Iterator
435 iter, void *iter_cls) 440 iter, void *iter_cls)
436{ 441{
437 struct ConfigSection *spos; 442 struct ConfigSection *spos;
438 struct ConfigSection *next; 443 struct ConfigSection *next;
439 444
440 next = cfg->sections; 445 next = cfg->sections;
441 while (next != NULL) 446 while (next != NULL)
442 { 447 {
443 spos = next; 448 spos = next;
444 next = spos->next; 449 next = spos->next;
445 iter (iter_cls, spos->name); 450 iter (iter_cls, spos->name);
446 } 451 }
447} 452}
448 453
449/** 454/**
@@ -454,7 +459,7 @@ GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
454 */ 459 */
455void 460void
456GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, 461GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
457 const char *section) 462 const char *section)
458{ 463{
459 struct ConfigSection *spos; 464 struct ConfigSection *spos;
460 struct ConfigSection *prev; 465 struct ConfigSection *prev;
@@ -463,28 +468,28 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
463 prev = NULL; 468 prev = NULL;
464 spos = cfg->sections; 469 spos = cfg->sections;
465 while (spos != NULL) 470 while (spos != NULL)
466 {
467 if (0 == strcmp (section, spos->name))
468 { 471 {
469 if (prev == NULL) 472 if (0 == strcmp (section, spos->name))
470 cfg->sections = spos->next; 473 {
471 else 474 if (prev == NULL)
472 prev->next = spos->next; 475 cfg->sections = spos->next;
473 while (NULL != (ent = spos->entries)) 476 else
474 { 477 prev->next = spos->next;
475 spos->entries = ent->next; 478 while (NULL != (ent = spos->entries))
476 GNUNET_free (ent->key); 479 {
477 GNUNET_free_non_null (ent->val); 480 spos->entries = ent->next;
478 GNUNET_free (ent); 481 GNUNET_free (ent->key);
479 cfg->dirty = GNUNET_YES; 482 GNUNET_free_non_null (ent->val);
480 } 483 GNUNET_free (ent);
481 GNUNET_free (spos->name); 484 cfg->dirty = GNUNET_YES;
482 GNUNET_free (spos); 485 }
483 return; 486 GNUNET_free (spos->name);
487 GNUNET_free (spos);
488 return;
489 }
490 prev = spos;
491 spos = spos->next;
484 } 492 }
485 prev = spos;
486 spos = spos->next;
487 }
488} 493}
489 494
490 495
@@ -499,7 +504,7 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
499 */ 504 */
500static void 505static void
501copy_entry (void *cls, const char *section, const char *option, 506copy_entry (void *cls, const char *section, const char *option,
502 const char *value) 507 const char *value)
503{ 508{
504 struct GNUNET_CONFIGURATION_Handle *dst = cls; 509 struct GNUNET_CONFIGURATION_Handle *dst = cls;
505 510
@@ -532,7 +537,8 @@ GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
532 * @return matching entry, NULL if not found 537 * @return matching entry, NULL if not found
533 */ 538 */
534static struct ConfigSection * 539static struct ConfigSection *
535findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section) 540findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
541 const char *section)
536{ 542{
537 struct ConfigSection *pos; 543 struct ConfigSection *pos;
538 544
@@ -553,7 +559,7 @@ findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
553 */ 559 */
554static struct ConfigEntry * 560static struct ConfigEntry *
555findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, 561findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
556 const char *key) 562 const char *key)
557{ 563{
558 struct ConfigSection *sec; 564 struct ConfigSection *sec;
559 struct ConfigEntry *pos; 565 struct ConfigEntry *pos;
@@ -580,7 +586,7 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
580 */ 586 */
581static void 587static void
582compareEntries (void *cls, const char *section, const char *option, 588compareEntries (void *cls, const char *section, const char *option,
583 const char *value) 589 const char *value)
584{ 590{
585 struct DiffHandle *dh = cls; 591 struct DiffHandle *dh = cls;
586 struct ConfigEntry *entNew; 592 struct ConfigEntry *entNew;
@@ -601,9 +607,9 @@ compareEntries (void *cls, const char *section, const char *option,
601 */ 607 */
602int 608int
603GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle 609GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
604 *cfgDefault, 610 *cfgDefault,
605 const struct GNUNET_CONFIGURATION_Handle 611 const struct GNUNET_CONFIGURATION_Handle
606 *cfgNew, const char *filename) 612 *cfgNew, const char *filename)
607{ 613{
608 int ret; 614 int ret;
609 struct DiffHandle diffHandle; 615 struct DiffHandle diffHandle;
@@ -626,28 +632,28 @@ GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
626 * @param value value to set 632 * @param value value to set
627 */ 633 */
628void 634void
629GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg, 635GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
630 const char *section, const char *option, 636 *cfg, const char *section,
631 const char *value) 637 const char *option, const char *value)
632{ 638{
633 struct ConfigSection *sec; 639 struct ConfigSection *sec;
634 struct ConfigEntry *e; 640 struct ConfigEntry *e;
635 641
636 e = findEntry (cfg, section, option); 642 e = findEntry (cfg, section, option);
637 if (e != NULL) 643 if (e != NULL)
638 { 644 {
639 GNUNET_free_non_null (e->val); 645 GNUNET_free_non_null (e->val);
640 e->val = GNUNET_strdup (value); 646 e->val = GNUNET_strdup (value);
641 return; 647 return;
642 } 648 }
643 sec = findSection (cfg, section); 649 sec = findSection (cfg, section);
644 if (sec == NULL) 650 if (sec == NULL)
645 { 651 {
646 sec = GNUNET_malloc (sizeof (struct ConfigSection)); 652 sec = GNUNET_malloc (sizeof (struct ConfigSection));
647 sec->name = GNUNET_strdup (section); 653 sec->name = GNUNET_strdup (section);
648 sec->next = cfg->sections; 654 sec->next = cfg->sections;
649 cfg->sections = sec; 655 cfg->sections = sec;
650 } 656 }
651 e = GNUNET_malloc (sizeof (struct ConfigEntry)); 657 e = GNUNET_malloc (sizeof (struct ConfigEntry));
652 e->key = GNUNET_strdup (option); 658 e->key = GNUNET_strdup (option);
653 e->val = GNUNET_strdup (value); 659 e->val = GNUNET_strdup (value);
@@ -665,9 +671,10 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
665 * @param number value to set 671 * @param number value to set
666 */ 672 */
667void 673void
668GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg, 674GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
669 const char *section, const char *option, 675 *cfg, const char *section,
670 unsigned long long number) 676 const char *option,
677 unsigned long long number)
671{ 678{
672 char s[64]; 679 char s[64];
673 680
@@ -686,10 +693,11 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
686 * @return GNUNET_OK on success, GNUNET_SYSERR on error 693 * @return GNUNET_OK on success, GNUNET_SYSERR on error
687 */ 694 */
688int 695int
689GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle 696GNUNET_CONFIGURATION_get_value_number (const struct
690 *cfg, const char *section, 697 GNUNET_CONFIGURATION_Handle *cfg,
691 const char *option, 698 const char *section,
692 unsigned long long *number) 699 const char *option,
700 unsigned long long *number)
693{ 701{
694 struct ConfigEntry *e; 702 struct ConfigEntry *e;
695 703
@@ -713,9 +721,9 @@ GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
713 */ 721 */
714int 722int
715GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle 723GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
716 *cfg, const char *section, 724 *cfg, const char *section,
717 const char *option, 725 const char *option,
718 struct GNUNET_TIME_Relative *time) 726 struct GNUNET_TIME_Relative *time)
719{ 727{
720 struct ConfigEntry *e; 728 struct ConfigEntry *e;
721 unsigned long long num; 729 unsigned long long num;
@@ -725,10 +733,10 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
725 return GNUNET_SYSERR; 733 return GNUNET_SYSERR;
726 if ((0 == strcasecmp (e->val, "infinity")) || 734 if ((0 == strcasecmp (e->val, "infinity")) ||
727 (0 == strcasecmp (e->val, "forever"))) 735 (0 == strcasecmp (e->val, "forever")))
728 { 736 {
729 *time = GNUNET_TIME_UNIT_FOREVER_REL; 737 *time = GNUNET_TIME_UNIT_FOREVER_REL;
730 return GNUNET_OK; 738 return GNUNET_OK;
731 } 739 }
732 if (1 != SSCANF (e->val, "%llu", &num)) 740 if (1 != SSCANF (e->val, "%llu", &num))
733 return GNUNET_SYSERR; 741 return GNUNET_SYSERR;
734 time->rel_value = (uint64_t) num; 742 time->rel_value = (uint64_t) num;
@@ -747,18 +755,19 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
747 * @return GNUNET_OK on success, GNUNET_SYSERR on error 755 * @return GNUNET_OK on success, GNUNET_SYSERR on error
748 */ 756 */
749int 757int
750GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle 758GNUNET_CONFIGURATION_get_value_string (const struct
751 *cfg, const char *section, 759 GNUNET_CONFIGURATION_Handle *cfg,
752 const char *option, char **value) 760 const char *section,
761 const char *option, char **value)
753{ 762{
754 struct ConfigEntry *e; 763 struct ConfigEntry *e;
755 764
756 e = findEntry (cfg, section, option); 765 e = findEntry (cfg, section, option);
757 if ((e == NULL) || (e->val == NULL)) 766 if ((e == NULL) || (e->val == NULL))
758 { 767 {
759 *value = NULL; 768 *value = NULL;
760 return GNUNET_SYSERR; 769 return GNUNET_SYSERR;
761 } 770 }
762 *value = GNUNET_strdup (e->val); 771 *value = GNUNET_strdup (e->val);
763 return GNUNET_OK; 772 return GNUNET_OK;
764} 773}
@@ -777,10 +786,12 @@ GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
777 * @return GNUNET_OK on success, GNUNET_SYSERR on error 786 * @return GNUNET_OK on success, GNUNET_SYSERR on error
778 */ 787 */
779int 788int
780GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle 789GNUNET_CONFIGURATION_get_value_choice (const struct
781 *cfg, const char *section, 790 GNUNET_CONFIGURATION_Handle *cfg,
782 const char *option, const char **choices, 791 const char *section,
783 const char **value) 792 const char *option,
793 const char **choices,
794 const char **value)
784{ 795{
785 struct ConfigEntry *e; 796 struct ConfigEntry *e;
786 int i; 797 int i;
@@ -790,19 +801,19 @@ GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
790 return GNUNET_SYSERR; 801 return GNUNET_SYSERR;
791 i = 0; 802 i = 0;
792 while (choices[i] != NULL) 803 while (choices[i] != NULL)
793 { 804 {
794 if (0 == strcasecmp (choices[i], e->val)) 805 if (0 == strcasecmp (choices[i], e->val))
795 break; 806 break;
796 i++; 807 i++;
797 } 808 }
798 if (choices[i] == NULL) 809 if (choices[i] == NULL)
799 { 810 {
800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 811 LOG (GNUNET_ERROR_TYPE_ERROR,
801 _("Configuration value '%s' for '%s'" 812 _("Configuration value '%s' for '%s'"
802 " in section '%s' is not in set of legal choices\n"), e->val, 813 " in section '%s' is not in set of legal choices\n"), e->val,
803 option, section); 814 option, section);
804 return GNUNET_SYSERR; 815 return GNUNET_SYSERR;
805 } 816 }
806 *value = choices[i]; 817 *value = choices[i];
807 return GNUNET_OK; 818 return GNUNET_OK;
808} 819}
@@ -816,8 +827,9 @@ GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
816 * @return GNUNET_YES if so, GNUNET_NO if not. 827 * @return GNUNET_YES if so, GNUNET_NO if not.
817 */ 828 */
818int 829int
819GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, 830GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
820 const char *section, const char *option) 831 *cfg, const char *section,
832 const char *option)
821{ 833{
822 struct ConfigEntry *e; 834 struct ConfigEntry *e;
823 835
@@ -838,7 +850,7 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
838 */ 850 */
839char * 851char *
840GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle 852GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
841 *cfg, char *orig) 853 *cfg, char *orig)
842{ 854{
843 int i; 855 int i;
844 char *prefix; 856 char *prefix;
@@ -852,24 +864,25 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
852 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0')) 864 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
853 i++; 865 i++;
854 if (orig[i] == '\0') 866 if (orig[i] == '\0')
855 { 867 {
856 post = ""; 868 post = "";
857 } 869 }
858 else 870 else
859 { 871 {
860 orig[i] = '\0'; 872 orig[i] = '\0';
861 post = &orig[i + 1]; 873 post = &orig[i + 1];
862 } 874 }
863 if (GNUNET_OK != 875 if (GNUNET_OK !=
864 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix)) 876 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1],
865 { 877 &prefix))
866 if (NULL == (env = getenv (&orig[1])))
867 { 878 {
868 orig[i] = DIR_SEPARATOR; 879 if (NULL == (env = getenv (&orig[1])))
869 return orig; 880 {
881 orig[i] = DIR_SEPARATOR;
882 return orig;
883 }
884 prefix = GNUNET_strdup (env);
870 } 885 }
871 prefix = GNUNET_strdup (env);
872 }
873 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2); 886 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
874 strcpy (result, prefix); 887 strcpy (result, prefix);
875 if ((strlen (prefix) == 0) || 888 if ((strlen (prefix) == 0) ||
@@ -894,18 +907,18 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
894 */ 907 */
895int 908int
896GNUNET_CONFIGURATION_get_value_filename (const struct 909GNUNET_CONFIGURATION_get_value_filename (const struct
897 GNUNET_CONFIGURATION_Handle *cfg, 910 GNUNET_CONFIGURATION_Handle *cfg,
898 const char *section, 911 const char *section,
899 const char *option, char **value) 912 const char *option, char **value)
900{ 913{
901 char *tmp; 914 char *tmp;
902 915
903 if (GNUNET_OK != 916 if (GNUNET_OK !=
904 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp)) 917 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
905 { 918 {
906 *value = NULL; 919 *value = NULL;
907 return GNUNET_SYSERR; 920 return GNUNET_SYSERR;
908 } 921 }
909 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp); 922 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
910 *value = GNUNET_STRINGS_filename_expand (tmp); 923 *value = GNUNET_STRINGS_filename_expand (tmp);
911 GNUNET_free (tmp); 924 GNUNET_free (tmp);
@@ -926,15 +939,15 @@ GNUNET_CONFIGURATION_get_value_filename (const struct
926 */ 939 */
927int 940int
928GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle 941GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
929 *cfg, const char *section, 942 *cfg, const char *section,
930 const char *option) 943 const char *option)
931{ 944{
932 static const char *yesno[] = { "YES", "NO", NULL }; 945 static const char *yesno[] = { "YES", "NO", NULL };
933 const char *val; 946 const char *val;
934 int ret; 947 int ret;
935 948
936 ret = 949 ret =
937 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val); 950 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
938 if (ret == GNUNET_SYSERR) 951 if (ret == GNUNET_SYSERR)
939 return ret; 952 return ret;
940 if (val == yesno[0]) 953 if (val == yesno[0])
@@ -955,11 +968,11 @@ GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
955 */ 968 */
956int 969int
957GNUNET_CONFIGURATION_iterate_value_filenames (const struct 970GNUNET_CONFIGURATION_iterate_value_filenames (const struct
958 GNUNET_CONFIGURATION_Handle *cfg, 971 GNUNET_CONFIGURATION_Handle
959 const char *section, 972 *cfg, const char *section,
960 const char *option, 973 const char *option,
961 GNUNET_FileNameCallback cb, 974 GNUNET_FileNameCallback cb,
962 void *cb_cls) 975 void *cb_cls)
963{ 976{
964 char *list; 977 char *list;
965 char *pos; 978 char *pos;
@@ -974,46 +987,46 @@ GNUNET_CONFIGURATION_iterate_value_filenames (const struct
974 ret = 0; 987 ret = 0;
975 pos = list; 988 pos = list;
976 while (1) 989 while (1)
977 {
978 while (pos[0] == ' ')
979 pos++;
980 if (strlen (pos) == 0)
981 break;
982 end = pos + 1;
983 while ((end[0] != ' ') && (end[0] != '\0'))
984 { 990 {
985 if (end[0] == '\\') 991 while (pos[0] == ' ')
986 { 992 pos++;
987 switch (end[1]) 993 if (strlen (pos) == 0)
988 { 994 break;
989 case '\\': 995 end = pos + 1;
990 case ' ': 996 while ((end[0] != ' ') && (end[0] != '\0'))
991 memmove (end, &end[1], strlen (&end[1]) + 1); 997 {
992 case '\0': 998 if (end[0] == '\\')
993 /* illegal, but just keep it */ 999 {
994 break; 1000 switch (end[1])
995 default: 1001 {
996 /* illegal, but just ignore that there was a '/' */ 1002 case '\\':
997 break; 1003 case ' ':
998 } 1004 memmove (end, &end[1], strlen (&end[1]) + 1);
999 } 1005 case '\0':
1000 end++; 1006 /* illegal, but just keep it */
1007 break;
1008 default:
1009 /* illegal, but just ignore that there was a '/' */
1010 break;
1011 }
1012 }
1013 end++;
1014 }
1015 old = end[0];
1016 end[0] = '\0';
1017 if (strlen (pos) > 0)
1018 {
1019 ret++;
1020 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1021 {
1022 ret = GNUNET_SYSERR;
1023 break;
1024 }
1025 }
1026 if (old == '\0')
1027 break;
1028 pos = end + 1;
1001 } 1029 }
1002 old = end[0];
1003 end[0] = '\0';
1004 if (strlen (pos) > 0)
1005 {
1006 ret++;
1007 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1008 {
1009 ret = GNUNET_SYSERR;
1010 break;
1011 }
1012 }
1013 if (old == '\0')
1014 break;
1015 pos = end + 1;
1016 }
1017 GNUNET_free (list); 1030 GNUNET_free (list);
1018 return ret; 1031 return ret;
1019} 1032}
@@ -1037,21 +1050,21 @@ escape_name (const char *value)
1037 rpos = value; 1050 rpos = value;
1038 wpos = escaped; 1051 wpos = escaped;
1039 while (rpos[0] != '\0') 1052 while (rpos[0] != '\0')
1040 {
1041 switch (rpos[0])
1042 { 1053 {
1043 case '\\': 1054 switch (rpos[0])
1044 case ' ': 1055 {
1045 wpos[0] = '\\'; 1056 case '\\':
1046 wpos[1] = rpos[0]; 1057 case ' ':
1047 wpos += 2; 1058 wpos[0] = '\\';
1048 break; 1059 wpos[1] = rpos[0];
1049 default: 1060 wpos += 2;
1050 wpos[0] = rpos[0]; 1061 break;
1051 wpos++; 1062 default:
1063 wpos[0] = rpos[0];
1064 wpos++;
1065 }
1066 rpos++;
1052 } 1067 }
1053 rpos++;
1054 }
1055 return escaped; 1068 return escaped;
1056} 1069}
1057 1070
@@ -1086,9 +1099,9 @@ test_match (void *cls, const char *fn)
1086 */ 1099 */
1087int 1100int
1088GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle 1101GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1089 *cfg, const char *section, 1102 *cfg, const char *section,
1090 const char *option, 1103 const char *option,
1091 const char *value) 1104 const char *value)
1092{ 1105{
1093 char *escaped; 1106 char *escaped;
1094 char *old; 1107 char *old;
@@ -1096,9 +1109,9 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1096 1109
1097 if (GNUNET_SYSERR == 1110 if (GNUNET_SYSERR ==
1098 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option, 1111 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1099 &test_match, 1112 &test_match,
1100 (void *) value)) 1113 (void *) value))
1101 return GNUNET_NO; /* already exists */ 1114 return GNUNET_NO; /* already exists */
1102 if (GNUNET_OK != 1115 if (GNUNET_OK !=
1103 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old)) 1116 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1104 old = GNUNET_strdup (""); 1117 old = GNUNET_strdup ("");
@@ -1130,9 +1143,9 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1130 */ 1143 */
1131int 1144int
1132GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle 1145GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1133 *cfg, const char *section, 1146 *cfg, const char *section,
1134 const char *option, 1147 const char *option,
1135 const char *value) 1148 const char *value)
1136{ 1149{
1137 char *list; 1150 char *list;
1138 char *pos; 1151 char *pos;
@@ -1146,55 +1159,55 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1146 match = escape_name (value); 1159 match = escape_name (value);
1147 pos = list; 1160 pos = list;
1148 while (1) 1161 while (1)
1149 {
1150 while (pos[0] == ' ')
1151 pos++;
1152 if (strlen (pos) == 0)
1153 break;
1154 end = pos + 1;
1155 while ((end[0] != ' ') && (end[0] != '\0'))
1156 {
1157 if (end[0] == '\\')
1158 {
1159 switch (end[1])
1160 {
1161 case '\\':
1162 case ' ':
1163 end++;
1164 break;
1165 case '\0':
1166 /* illegal, but just keep it */
1167 break;
1168 default:
1169 /* illegal, but just ignore that there was a '/' */
1170 break;
1171 }
1172 }
1173 end++;
1174 }
1175 old = end[0];
1176 end[0] = '\0';
1177 if (0 == strcmp (pos, match))
1178 { 1162 {
1179 if (old != '\0') 1163 while (pos[0] == ' ')
1180 memmove (pos, &end[1], strlen (&end[1]) + 1); 1164 pos++;
1181 else 1165 if (strlen (pos) == 0)
1182 { 1166 break;
1183 if (pos != list) 1167 end = pos + 1;
1184 pos[-1] = '\0'; 1168 while ((end[0] != ' ') && (end[0] != '\0'))
1185 else 1169 {
1186 pos[0] = '\0'; 1170 if (end[0] == '\\')
1187 } 1171 {
1188 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list); 1172 switch (end[1])
1189 GNUNET_free (list); 1173 {
1190 GNUNET_free (match); 1174 case '\\':
1191 return GNUNET_OK; 1175 case ' ':
1176 end++;
1177 break;
1178 case '\0':
1179 /* illegal, but just keep it */
1180 break;
1181 default:
1182 /* illegal, but just ignore that there was a '/' */
1183 break;
1184 }
1185 }
1186 end++;
1187 }
1188 old = end[0];
1189 end[0] = '\0';
1190 if (0 == strcmp (pos, match))
1191 {
1192 if (old != '\0')
1193 memmove (pos, &end[1], strlen (&end[1]) + 1);
1194 else
1195 {
1196 if (pos != list)
1197 pos[-1] = '\0';
1198 else
1199 pos[0] = '\0';
1200 }
1201 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1202 GNUNET_free (list);
1203 GNUNET_free (match);
1204 return GNUNET_OK;
1205 }
1206 if (old == '\0')
1207 break;
1208 end[0] = old;
1209 pos = end + 1;
1192 } 1210 }
1193 if (old == '\0')
1194 break;
1195 end[0] = old;
1196 pos = end + 1;
1197 }
1198 GNUNET_free (list); 1211 GNUNET_free (list);
1199 GNUNET_free (match); 1212 GNUNET_free (match);
1200 return GNUNET_NO; 1213 return GNUNET_NO;
@@ -1211,7 +1224,7 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1211 */ 1224 */
1212int 1225int
1213GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, 1226GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1214 const char *filename) 1227 const char *filename)
1215{ 1228{
1216 char *baseconfig; 1229 char *baseconfig;
1217 char *ipath; 1230 char *ipath;
@@ -1221,21 +1234,21 @@ GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1221 return GNUNET_SYSERR; 1234 return GNUNET_SYSERR;
1222 baseconfig = NULL; 1235 baseconfig = NULL;
1223 GNUNET_asprintf (&baseconfig, "%s%s%s", ipath, DIR_SEPARATOR_STR, 1236 GNUNET_asprintf (&baseconfig, "%s%s%s", ipath, DIR_SEPARATOR_STR,
1224 "defaults.conf"); 1237 "defaults.conf");
1225 GNUNET_free (ipath); 1238 GNUNET_free (ipath);
1226 if ((GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, baseconfig)) || 1239 if ((GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1227 (!((filename == NULL) || 1240 (!((filename == NULL) ||
1228 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename))))) 1241 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1229 { 1242 {
1230 GNUNET_free (baseconfig); 1243 GNUNET_free (baseconfig);
1231 return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR; 1244 return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR;
1232 } 1245 }
1233 GNUNET_free (baseconfig); 1246 GNUNET_free (baseconfig);
1234 if (((GNUNET_YES != 1247 if (((GNUNET_YES !=
1235 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) && 1248 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1236 (filename != NULL)) 1249 (filename != NULL))
1237 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", 1250 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1238 filename); 1251 filename);
1239 if ((GNUNET_YES == 1252 if ((GNUNET_YES ==
1240 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) && 1253 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1241 (GNUNET_YES == 1254 (GNUNET_YES ==
diff --git a/src/util/connection.c b/src/util/connection.c
index 6b2cf75e0..4beafa3f3 100644
--- a/src/util/connection.c
+++ b/src/util/connection.c
@@ -41,6 +41,10 @@
41 41
42#define DEBUG_CONNECTION GNUNET_EXTRA_LOGGING 42#define DEBUG_CONNECTION GNUNET_EXTRA_LOGGING
43 43
44#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
45
46#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
47
44/** 48/**
45 * Possible functions to call after connect failed or succeeded. 49 * Possible functions to call after connect failed or succeeded.
46 */ 50 */
@@ -329,7 +333,8 @@ GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
329 * @return the boxed socket handle 333 * @return the boxed socket handle
330 */ 334 */
331struct GNUNET_CONNECTION_Handle * 335struct GNUNET_CONNECTION_Handle *
332GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket) 336GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
337 *osSocket)
333{ 338{
334 struct GNUNET_CONNECTION_Handle *ret; 339 struct GNUNET_CONNECTION_Handle *ret;
335 340
@@ -352,8 +357,8 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
352 */ 357 */
353struct GNUNET_CONNECTION_Handle * 358struct GNUNET_CONNECTION_Handle *
354GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, 359GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
355 void *access_cls, 360 void *access_cls,
356 struct GNUNET_NETWORK_Handle *lsock) 361 struct GNUNET_NETWORK_Handle *lsock)
357{ 362{
358 struct GNUNET_CONNECTION_Handle *ret; 363 struct GNUNET_CONNECTION_Handle *ret;
359 char addr[128]; 364 char addr[128];
@@ -374,94 +379,94 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
374 379
375 addrlen = sizeof (addr); 380 addrlen = sizeof (addr);
376 sock = 381 sock =
377 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen); 382 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
378 if (NULL == sock) 383 if (NULL == sock)
379 { 384 {
380 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept"); 385 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept");
381 return NULL; 386 return NULL;
382 } 387 }
383 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t))) 388 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t)))
384 { 389 {
385 GNUNET_break (0); 390 GNUNET_break (0);
386 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); 391 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
387 return NULL; 392 return NULL;
388 } 393 }
389 394
390 sa = (struct sockaddr *) addr; 395 sa = (struct sockaddr *) addr;
391 v6 = (struct sockaddr_in6 *) addr; 396 v6 = (struct sockaddr_in6 *) addr;
392 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr))) 397 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
393 { 398 {
394 /* convert to V4 address */ 399 /* convert to V4 address */
395 v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); 400 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
396 memset (v4, 0, sizeof (struct sockaddr_in)); 401 memset (v4, 0, sizeof (struct sockaddr_in));
397 v4->sin_family = AF_INET; 402 v4->sin_family = AF_INET;
398#if HAVE_SOCKADDR_IN_SIN_LEN 403#if HAVE_SOCKADDR_IN_SIN_LEN
399 v4->sin_len = (u_char) sizeof (struct sockaddr_in); 404 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
400#endif 405#endif
401 memcpy (&v4->sin_addr, 406 memcpy (&v4->sin_addr,
402 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) - 407 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
403 sizeof (struct in_addr)], 408 sizeof (struct in_addr)],
404 sizeof (struct in_addr)); 409 sizeof (struct in_addr));
405 v4->sin_port = v6->sin6_port; 410 v4->sin_port = v6->sin6_port;
406 uaddr = v4; 411 uaddr = v4;
407 addrlen = sizeof (struct sockaddr_in); 412 addrlen = sizeof (struct sockaddr_in);
408 } 413 }
409 else 414 else
410 { 415 {
411 uaddr = GNUNET_malloc (addrlen); 416 uaddr = GNUNET_malloc (addrlen);
412 memcpy (uaddr, addr, addrlen); 417 memcpy (uaddr, addr, addrlen);
413 } 418 }
414 gcp = NULL; 419 gcp = NULL;
415 gc.uid = 0; 420 gc.uid = 0;
416 gc.gid = 0; 421 gc.gid = 0;
417 if (sa->sa_family == AF_UNIX) 422 if (sa->sa_family == AF_UNIX)
418 { 423 {
419#if HAVE_GETPEEREID 424#if HAVE_GETPEEREID
420 /* most BSDs */ 425 /* most BSDs */
421 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid)) 426 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid))
422 gcp = &gc; 427 gcp = &gc;
423#else 428#else
424#ifdef SO_PEERCRED 429#ifdef SO_PEERCRED
425 /* largely traditional GNU/Linux */ 430 /* largely traditional GNU/Linux */
426 olen = sizeof (uc); 431 olen = sizeof (uc);
427 if ((0 == 432 if ((0 ==
428 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc, 433 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED,
429 &olen)) && (olen == sizeof (uc))) 434 &uc, &olen)) && (olen == sizeof (uc)))
430 { 435 {
431 gc.uid = uc.uid; 436 gc.uid = uc.uid;
432 gc.gid = uc.gid; 437 gc.gid = uc.gid;
433 gcp = &gc; 438 gcp = &gc;
434 } 439 }
435#else 440#else
436#if HAVE_GETPEERUCRED 441#if HAVE_GETPEERUCRED
437 /* this is for Solaris 10 */ 442 /* this is for Solaris 10 */
438 ucred_t *uc; 443 ucred_t *uc;
439 444
440 uc = NULL; 445 uc = NULL;
441 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc)) 446 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
442 { 447 {
443 gc.uid = ucred_geteuid (uc); 448 gc.uid = ucred_geteuid (uc);
444 gc.gid = ucred_getegid (uc); 449 gc.gid = ucred_getegid (uc);
445 gcp = &gc; 450 gcp = &gc;
446 } 451 }
447 ucred_free (uc); 452 ucred_free (uc);
448#endif 453#endif
449#endif 454#endif
450#endif 455#endif
451 } 456 }
452 457
453 if ((access != NULL) && 458 if ((access != NULL) &&
454 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen)))) 459 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
455 { 460 {
456 if (aret == GNUNET_NO) 461 if (aret == GNUNET_NO)
457 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"), 462 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
458 GNUNET_a2s (uaddr, addrlen)); 463 GNUNET_a2s (uaddr, addrlen));
459 GNUNET_break (GNUNET_OK == 464 GNUNET_break (GNUNET_OK ==
460 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR)); 465 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
461 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); 466 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
462 GNUNET_free (uaddr); 467 GNUNET_free (uaddr);
463 return NULL; 468 return NULL;
464 } 469 }
465 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 470 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
466 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 471 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
467 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); 472 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
@@ -469,8 +474,8 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
469 ret->addrlen = addrlen; 474 ret->addrlen = addrlen;
470 ret->sock = sock; 475 ret->sock = sock;
471#if DEBUG_CONNECTION 476#if DEBUG_CONNECTION
472 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"), 477 LOG (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"),
473 GNUNET_a2s (uaddr, addrlen), ret); 478 GNUNET_a2s (uaddr, addrlen), ret);
474#endif 479#endif
475 return ret; 480 return ret;
476} 481}
@@ -485,7 +490,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
485 */ 490 */
486int 491int
487GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock, 492GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
488 void **addr, size_t * addrlen) 493 void **addr, size_t * addrlen)
489{ 494{
490 if ((sock->addr == NULL) || (sock->addrlen == 0)) 495 if ((sock->addr == NULL) || (sock->addrlen == 0))
491 return GNUNET_NO; 496 return GNUNET_NO;
@@ -518,7 +523,8 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
518 * @param tc unused 523 * @param tc unused
519 */ 524 */
520static void 525static void
521destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 526destroy_continuation (void *cls,
527 const struct GNUNET_SCHEDULER_TaskContext *tc)
522{ 528{
523 struct GNUNET_CONNECTION_Handle *sock = cls; 529 struct GNUNET_CONNECTION_Handle *sock = cls;
524 GNUNET_CONNECTION_TransmitReadyNotify notify; 530 GNUNET_CONNECTION_TransmitReadyNotify notify;
@@ -527,83 +533,83 @@ destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK; 533 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
528 GNUNET_assert (sock->dns_active == NULL); 534 GNUNET_assert (sock->dns_active == NULL);
529 if (0 != (sock->ccs & COCO_TRANSMIT_READY)) 535 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
530 { 536 {
531#if DEBUG_CONNECTION 537#if DEBUG_CONNECTION
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 538 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 "Destroy waits for CCS-TR to be done (%p)\n", sock); 539 "Destroy waits for CCS-TR to be done (%p)\n", sock);
534#endif 540#endif
535 sock->ccs |= COCO_DESTROY_CONTINUATION; 541 sock->ccs |= COCO_DESTROY_CONTINUATION;
536 return; 542 return;
537 } 543 }
538 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 544 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
539 { 545 {
540#if DEBUG_CONNECTION 546#if DEBUG_CONNECTION
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 547 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Destroy waits for write_task to be done (%p)\n", sock); 548 "Destroy waits for write_task to be done (%p)\n", sock);
543#endif 549#endif
544 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 550 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
545 sock->destroy_task = 551 sock->destroy_task =
546 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation, 552 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation,
547 sock); 553 sock);
548 return; 554 return;
549 } 555 }
550 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN)) 556 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
551 { 557 {
552 sock->ccs |= COCO_DESTROY_CONTINUATION; 558 sock->ccs |= COCO_DESTROY_CONTINUATION;
553 return; 559 return;
554 } 560 }
555 if (sock->sock != NULL) 561 if (sock->sock != NULL)
556 { 562 {
557#if DEBUG_CONNECTION 563#if DEBUG_CONNECTION
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock); 564 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
559#endif 565#endif
560 if (sock->persist != GNUNET_YES) 566 if (sock->persist != GNUNET_YES)
561 { 567 {
562 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) 568 if ((GNUNET_YES !=
563 && (errno != ENOTCONN) && (errno != ECONNRESET)) 569 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
564 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown"); 570 && (errno != ENOTCONN) && (errno != ECONNRESET))
571 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
572 }
565 } 573 }
566 }
567 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 574 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
568 { 575 {
569 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 576 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
570 sock->destroy_task = 577 sock->destroy_task =
571 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation, 578 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation,
572 sock); 579 sock);
573 return; 580 return;
574 } 581 }
575#if DEBUG_CONNECTION 582#if DEBUG_CONNECTION
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock); 583 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
577#endif 584#endif
578 while (NULL != (pos = sock->ap_head)) 585 while (NULL != (pos = sock->ap_head))
579 { 586 {
580 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 587 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
581 GNUNET_SCHEDULER_cancel (pos->task); 588 GNUNET_SCHEDULER_cancel (pos->task);
582 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos); 589 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
583 GNUNET_free (pos); 590 GNUNET_free (pos);
584 } 591 }
585 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 592 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
586 GNUNET_assert (sock->ccs == COCO_NONE); 593 GNUNET_assert (sock->ccs == COCO_NONE);
587 if (NULL != (notify = sock->nth.notify_ready)) 594 if (NULL != (notify = sock->nth.notify_ready))
588 { 595 {
589 sock->nth.notify_ready = NULL; 596 sock->nth.notify_ready = NULL;
590 notify (sock->nth.notify_ready_cls, 0, NULL); 597 notify (sock->nth.notify_ready_cls, 0, NULL);
591 } 598 }
592 599
593 if (sock->sock != NULL) 600 if (sock->sock != NULL)
594 { 601 {
595 if (sock->persist != GNUNET_YES) 602 if (sock->persist != GNUNET_YES)
596 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 603 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
597 else 604 else
598 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately 605 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
599 * leak the socket in this special case) ... */ 606 * leak the socket in this special case) ... */
600 } 607 }
601 GNUNET_free_non_null (sock->addr); 608 GNUNET_free_non_null (sock->addr);
602 GNUNET_free_non_null (sock->hostname); 609 GNUNET_free_non_null (sock->hostname);
603 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 610 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
604#if DEBUG_CONNECTION 611#if DEBUG_CONNECTION
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", 612 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", sock);
606 sock);
607#endif 613#endif
608 GNUNET_free (sock->write_buffer); 614 GNUNET_free (sock->write_buffer);
609 GNUNET_free (sock); 615 GNUNET_free (sock);
@@ -632,13 +638,13 @@ static void
632connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) 638connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
633{ 639{
634#if DEBUG_CONNECTION 640#if DEBUG_CONNECTION
635 GNUNET_log ((0 != 641 LOG ((0 !=
636 strncmp (h->hostname, "localhost:", 642 strncmp (h->hostname, "localhost:",
637 10)) ? GNUNET_ERROR_TYPE_INFO : 643 10)) ? GNUNET_ERROR_TYPE_INFO :
638 GNUNET_ERROR_TYPE_WARNING, 644 GNUNET_ERROR_TYPE_WARNING,
639 _ 645 _
640 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), 646 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
641 h->hostname, h->port); 647 h->hostname, h->port);
642#endif 648#endif
643 /* connect failed / timed out */ 649 /* connect failed / timed out */
644 GNUNET_break (h->ap_head == NULL); 650 GNUNET_break (h->ap_head == NULL);
@@ -648,40 +654,39 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
648 654
649 /* trigger jobs that used to wait on "connect_task" */ 655 /* trigger jobs that used to wait on "connect_task" */
650 if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) 656 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
651 { 657 {
652#if DEBUG_CONNECTION 658#if DEBUG_CONNECTION
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 659 LOG (GNUNET_ERROR_TYPE_DEBUG,
654 "connect_fail_continuation triggers receive_again (%p)\n", h); 660 "connect_fail_continuation triggers receive_again (%p)\n", h);
655#endif 661#endif
656 h->ccs -= COCO_RECEIVE_AGAIN; 662 h->ccs -= COCO_RECEIVE_AGAIN;
657 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 663 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
658 } 664 }
659 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 665 if (0 != (h->ccs & COCO_TRANSMIT_READY))
660 { 666 {
661#if DEBUG_CONNECTION 667#if DEBUG_CONNECTION
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 668 LOG (GNUNET_ERROR_TYPE_DEBUG,
663 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n", 669 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
664 h); 670 h);
665#endif 671#endif
666 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 672 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
667 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 673 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
668 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 674 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
669 h->ccs -= COCO_TRANSMIT_READY; 675 h->ccs -= COCO_TRANSMIT_READY;
670 GNUNET_assert (h->nth.notify_ready != NULL); 676 GNUNET_assert (h->nth.notify_ready != NULL);
671 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 677 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
672 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h); 678 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
673 } 679 }
674 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) 680 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
675 { 681 {
676#if DEBUG_CONNECTION 682#if DEBUG_CONNECTION
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 683 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "connect_fail_continuation runs destroy_continuation (%p)\n", 684 "connect_fail_continuation runs destroy_continuation (%p)\n", h);
679 h);
680#endif 685#endif
681 h->ccs -= COCO_DESTROY_CONTINUATION; 686 h->ccs -= COCO_DESTROY_CONTINUATION;
682 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); 687 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
683 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); 688 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
684 } 689 }
685} 690}
686 691
687 692
@@ -694,48 +699,48 @@ static void
694connect_success_continuation (struct GNUNET_CONNECTION_Handle *h) 699connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
695{ 700{
696#if DEBUG_CONNECTION 701#if DEBUG_CONNECTION
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n", 702 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
698 GNUNET_a2s (h->addr, h->addrlen), h); 703 GNUNET_a2s (h->addr, h->addrlen), h);
699#endif 704#endif
700 /* trigger jobs that waited for the connection */ 705 /* trigger jobs that waited for the connection */
701 if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) 706 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
702 { 707 {
703#if DEBUG_CONNECTION 708#if DEBUG_CONNECTION
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 709 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "connect_success_continuation runs receive_again (%p)\n", h); 710 "connect_success_continuation runs receive_again (%p)\n", h);
706#endif 711#endif
707 h->ccs -= COCO_RECEIVE_AGAIN; 712 h->ccs -= COCO_RECEIVE_AGAIN;
708 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 713 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
709 } 714 }
710 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 715 if (0 != (h->ccs & COCO_TRANSMIT_READY))
711 { 716 {
712#if DEBUG_CONNECTION 717#if DEBUG_CONNECTION
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 718 LOG (GNUNET_ERROR_TYPE_DEBUG,
714 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", 719 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
715 h); 720 h);
716#endif 721#endif
717 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 722 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
718 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 723 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
719 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 724 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
720 h->ccs -= COCO_TRANSMIT_READY; 725 h->ccs -= COCO_TRANSMIT_READY;
721 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 726 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
722 GNUNET_assert (h->nth.notify_ready != NULL); 727 GNUNET_assert (h->nth.notify_ready != NULL);
723 h->write_task = 728 h->write_task =
724 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 729 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
725 (h->nth.transmit_timeout), h->sock, 730 (h->nth.transmit_timeout), h->sock,
726 &transmit_ready, h); 731 &transmit_ready, h);
727 } 732 }
728 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) 733 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
729 { 734 {
730#if DEBUG_CONNECTION 735#if DEBUG_CONNECTION
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 736 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "connect_success_continuation runs destroy_continuation (%p)\n", 737 "connect_success_continuation runs destroy_continuation (%p)\n",
733 h); 738 h);
734#endif 739#endif
735 h->ccs -= COCO_DESTROY_CONTINUATION; 740 h->ccs -= COCO_DESTROY_CONTINUATION;
736 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); 741 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
737 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); 742 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
738 } 743 }
739} 744}
740 745
741 746
@@ -748,7 +753,7 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
748 */ 753 */
749static void 754static void
750connect_probe_continuation (void *cls, 755connect_probe_continuation (void *cls,
751 const struct GNUNET_SCHEDULER_TaskContext *tc) 756 const struct GNUNET_SCHEDULER_TaskContext *tc)
752{ 757{
753 struct AddressProbe *ap = cls; 758 struct AddressProbe *ap = cls;
754 struct GNUNET_CONNECTION_Handle *h = ap->h; 759 struct GNUNET_CONNECTION_Handle *h = ap->h;
@@ -763,15 +768,15 @@ connect_probe_continuation (void *cls,
763 error = 0; 768 error = 0;
764 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || 769 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
765 (GNUNET_OK != 770 (GNUNET_OK !=
766 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error, 771 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
767 &len)) || (error != 0)) 772 &error, &len)) || (error != 0))
768 { 773 {
769 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 774 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
770 GNUNET_free (ap); 775 GNUNET_free (ap);
771 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO)) 776 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
772 connect_fail_continuation (h); 777 connect_fail_continuation (h);
773 return; 778 return;
774 } 779 }
775 GNUNET_assert (h->sock == NULL); 780 GNUNET_assert (h->sock == NULL);
776 h->sock = ap->sock; 781 h->sock = ap->sock;
777 GNUNET_assert (h->addr == NULL); 782 GNUNET_assert (h->addr == NULL);
@@ -781,12 +786,12 @@ connect_probe_continuation (void *cls,
781 GNUNET_free (ap); 786 GNUNET_free (ap);
782 /* cancel all other attempts */ 787 /* cancel all other attempts */
783 while (NULL != (pos = h->ap_head)) 788 while (NULL != (pos = h->ap_head))
784 { 789 {
785 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 790 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
786 GNUNET_SCHEDULER_cancel (pos->task); 791 GNUNET_SCHEDULER_cancel (pos->task);
787 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos); 792 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
788 GNUNET_free (pos); 793 GNUNET_free (pos);
789 } 794 }
790 connect_success_continuation (h); 795 connect_success_continuation (h);
791} 796}
792 797
@@ -801,28 +806,27 @@ connect_probe_continuation (void *cls,
801 */ 806 */
802static void 807static void
803try_connect_using_address (void *cls, const struct sockaddr *addr, 808try_connect_using_address (void *cls, const struct sockaddr *addr,
804 socklen_t addrlen) 809 socklen_t addrlen)
805{ 810{
806 struct GNUNET_CONNECTION_Handle *h = cls; 811 struct GNUNET_CONNECTION_Handle *h = cls;
807 struct AddressProbe *ap; 812 struct AddressProbe *ap;
808 struct GNUNET_TIME_Relative delay; 813 struct GNUNET_TIME_Relative delay;
809 814
810 if (addr == NULL) 815 if (addr == NULL)
811 { 816 {
812 h->dns_active = NULL; 817 h->dns_active = NULL;
813 if ( (NULL == h->ap_head) && 818 if ((NULL == h->ap_head) && (NULL == h->sock))
814 (NULL == h->sock) ) 819 connect_fail_continuation (h);
815 connect_fail_continuation (h); 820 return;
816 return; 821 }
817 }
818 if (h->sock != NULL) 822 if (h->sock != NULL)
819 return; /* already connected */ 823 return; /* already connected */
820 GNUNET_assert (h->addr == NULL); 824 GNUNET_assert (h->addr == NULL);
821 /* try to connect */ 825 /* try to connect */
822#if DEBUG_CONNECTION 826#if DEBUG_CONNECTION
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 827 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, 828 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname,
825 h->port, GNUNET_a2s (addr, addrlen), h->port); 829 h->port, GNUNET_a2s (addr, addrlen), h->port);
826#endif 830#endif
827 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen); 831 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
828 ap->addr = (const struct sockaddr *) &ap[1]; 832 ap->addr = (const struct sockaddr *) &ap[1];
@@ -831,58 +835,59 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
831 ap->h = h; 835 ap->h = h;
832 836
833 switch (ap->addr->sa_family) 837 switch (ap->addr->sa_family)
834 { 838 {
835 case AF_INET: 839 case AF_INET:
836 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port); 840 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
837 break; 841 break;
838 case AF_INET6: 842 case AF_INET6:
839 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port); 843 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
840 break; 844 break;
841 default: 845 default:
842 GNUNET_break (0); 846 GNUNET_break (0);
843 GNUNET_free (ap); 847 GNUNET_free (ap);
844 return; /* not supported by us */ 848 return; /* not supported by us */
845 } 849 }
846 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0); 850 ap->sock =
851 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
847 if (ap->sock == NULL) 852 if (ap->sock == NULL)
848 { 853 {
849 GNUNET_free (ap); 854 GNUNET_free (ap);
850 return; /* not supported by OS */ 855 return; /* not supported by OS */
851 } 856 }
852#if DEBUG_CONNECTION 857#if DEBUG_CONNECTION
853 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), 858 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
854 GNUNET_a2s (ap->addr, ap->addrlen), h); 859 GNUNET_a2s (ap->addr, ap->addrlen), h);
855#endif 860#endif
856 if ((GNUNET_OK != 861 if ((GNUNET_OK !=
857 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && 862 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
858 (errno != EINPROGRESS)) 863 (errno != EINPROGRESS))
859 { 864 {
860 /* maybe refused / unsupported address, try next */ 865 /* maybe refused / unsupported address, try next */
861 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); 866 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
862#if 0 867#if 0
863 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 868 LOG (GNUNET_ERROR_TYPE_INFO,
864 _("Failed to connect to `%s' (%p)\n"), 869 _("Failed to connect to `%s' (%p)\n"),
865 GNUNET_a2s (ap->addr, ap->addrlen), h); 870 GNUNET_a2s (ap->addr, ap->addrlen), h);
866#endif 871#endif
867 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 872 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
868 GNUNET_free (ap); 873 GNUNET_free (ap);
869 return; 874 return;
870 } 875 }
871 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap); 876 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
872 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT; 877 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
873 if (h->nth.notify_ready != NULL) 878 if (h->nth.notify_ready != NULL)
874 delay = 879 delay =
875 GNUNET_TIME_relative_min (delay, 880 GNUNET_TIME_relative_min (delay,
876 GNUNET_TIME_absolute_get_remaining (h-> 881 GNUNET_TIME_absolute_get_remaining (h->
877 nth.transmit_timeout)); 882 nth.transmit_timeout));
878 if (h->receiver != NULL) 883 if (h->receiver != NULL)
879 delay = 884 delay =
880 GNUNET_TIME_relative_min (delay, 885 GNUNET_TIME_relative_min (delay,
881 GNUNET_TIME_absolute_get_remaining 886 GNUNET_TIME_absolute_get_remaining
882 (h->receive_timeout)); 887 (h->receive_timeout));
883 ap->task = 888 ap->task =
884 GNUNET_SCHEDULER_add_write_net (delay, ap->sock, 889 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
885 &connect_probe_continuation, ap); 890 &connect_probe_continuation, ap);
886} 891}
887 892
888 893
@@ -897,13 +902,13 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
897 * @return the socket handle 902 * @return the socket handle
898 */ 903 */
899struct GNUNET_CONNECTION_Handle * 904struct GNUNET_CONNECTION_Handle *
900GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle 905GNUNET_CONNECTION_create_from_connect (const struct
901 *cfg, const char *hostname, 906 GNUNET_CONFIGURATION_Handle *cfg,
902 uint16_t port) 907 const char *hostname, uint16_t port)
903{ 908{
904 struct GNUNET_CONNECTION_Handle *ret; 909 struct GNUNET_CONNECTION_Handle *ret;
905 910
906 GNUNET_assert (0 < strlen (hostname)); /* sanity check */ 911 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
907 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 912 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
908 ret->cfg = cfg; 913 ret->cfg = cfg;
909 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 914 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
@@ -911,9 +916,9 @@ GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
911 ret->port = port; 916 ret->port = port;
912 ret->hostname = GNUNET_strdup (hostname); 917 ret->hostname = GNUNET_strdup (hostname);
913 ret->dns_active = 918 ret->dns_active =
914 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC, 919 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
915 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT, 920 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
916 &try_connect_using_address, ret); 921 &try_connect_using_address, ret);
917 return ret; 922 return ret;
918} 923}
919 924
@@ -929,15 +934,15 @@ GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
929 */ 934 */
930struct GNUNET_CONNECTION_Handle * 935struct GNUNET_CONNECTION_Handle *
931GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct 936GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
932 GNUNET_CONFIGURATION_Handle 937 GNUNET_CONFIGURATION_Handle
933 *cfg, const char *unixpath) 938 *cfg, const char *unixpath)
934{ 939{
935#ifdef AF_UNIX 940#ifdef AF_UNIX
936 struct GNUNET_CONNECTION_Handle *ret; 941 struct GNUNET_CONNECTION_Handle *ret;
937 struct sockaddr_un *un; 942 struct sockaddr_un *un;
938 size_t slen; 943 size_t slen;
939 944
940 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ 945 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
941 un = GNUNET_malloc (sizeof (struct sockaddr_un)); 946 un = GNUNET_malloc (sizeof (struct sockaddr_un));
942 un->sun_family = AF_UNIX; 947 un->sun_family = AF_UNIX;
943 slen = strlen (unixpath); 948 slen = strlen (unixpath);
@@ -962,20 +967,20 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
962 ret->addrlen = slen; 967 ret->addrlen = slen;
963 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); 968 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
964 if (NULL == ret->sock) 969 if (NULL == ret->sock)
965 { 970 {
966 GNUNET_free (ret->addr); 971 GNUNET_free (ret->addr);
967 GNUNET_free (ret->write_buffer); 972 GNUNET_free (ret->write_buffer);
968 GNUNET_free (ret); 973 GNUNET_free (ret);
969 return NULL; 974 return NULL;
970 } 975 }
971 if (GNUNET_OK != 976 if (GNUNET_OK !=
972 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen)) 977 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
973 { 978 {
974 /* Just return; we expect everything to work eventually so don't fail HARD */ 979 /* Just return; we expect everything to work eventually so don't fail HARD */
975 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock)); 980 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
976 ret->sock = NULL; 981 ret->sock = NULL;
977 return ret; 982 return ret;
978 } 983 }
979 connect_success_continuation (ret); 984 connect_success_continuation (ret);
980 return ret; 985 return ret;
981#else 986#else
@@ -996,8 +1001,8 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
996 */ 1001 */
997struct GNUNET_CONNECTION_Handle * 1002struct GNUNET_CONNECTION_Handle *
998GNUNET_CONNECTION_create_from_sockaddr (int af_family, 1003GNUNET_CONNECTION_create_from_sockaddr (int af_family,
999 const struct sockaddr *serv_addr, 1004 const struct sockaddr *serv_addr,
1000 socklen_t addrlen) 1005 socklen_t addrlen)
1001{ 1006{
1002 struct GNUNET_NETWORK_Handle *s; 1007 struct GNUNET_NETWORK_Handle *s;
1003 struct GNUNET_CONNECTION_Handle *ret; 1008 struct GNUNET_CONNECTION_Handle *ret;
@@ -1005,29 +1010,29 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
1005 1010
1006 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); 1011 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1007 if (s == NULL) 1012 if (s == NULL)
1008 { 1013 {
1009 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 1014 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1010 "socket"); 1015 "socket");
1011 return NULL; 1016 return NULL;
1012 } 1017 }
1013 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && 1018 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
1014 (errno != EINPROGRESS)) 1019 (errno != EINPROGRESS))
1015 { 1020 {
1016 /* maybe refused / unsupported address, try next */ 1021 /* maybe refused / unsupported address, try next */
1017 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); 1022 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
1018 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1023 LOG (GNUNET_ERROR_TYPE_INFO,
1019 _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr, 1024 _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr,
1020 addrlen)); 1025 addrlen));
1021 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); 1026 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1022 return NULL; 1027 return NULL;
1023 } 1028 }
1024 ret = GNUNET_CONNECTION_create_from_existing (s); 1029 ret = GNUNET_CONNECTION_create_from_existing (s);
1025 ret->addr = GNUNET_malloc (addrlen); 1030 ret->addr = GNUNET_malloc (addrlen);
1026 memcpy (ret->addr, serv_addr, addrlen); 1031 memcpy (ret->addr, serv_addr, addrlen);
1027 ret->addrlen = addrlen; 1032 ret->addrlen = addrlen;
1028#if DEBUG_CONNECTION 1033#if DEBUG_CONNECTION
1029 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), 1034 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
1030 GNUNET_a2s (serv_addr, addrlen), ret); 1035 GNUNET_a2s (serv_addr, addrlen), ret);
1031#endif 1036#endif
1032 return ret; 1037 return ret;
1033} 1038}
@@ -1045,7 +1050,7 @@ int
1045GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock) 1050GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1046{ 1051{
1047 if ((sock->ap_head != NULL) || (sock->dns_active != NULL)) 1052 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1048 return GNUNET_YES; /* still trying to connect */ 1053 return GNUNET_YES; /* still trying to connect */
1049 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES; 1054 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1050} 1055}
1051 1056
@@ -1065,23 +1070,23 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1065 */ 1070 */
1066void 1071void
1067GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock, 1072GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1068 int finish_pending_write) 1073 int finish_pending_write)
1069{ 1074{
1070 if (GNUNET_NO == finish_pending_write) 1075 if (GNUNET_NO == finish_pending_write)
1071 {
1072 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1073 { 1076 {
1074 GNUNET_SCHEDULER_cancel (sock->write_task); 1077 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1075 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1078 {
1076 sock->write_buffer_off = 0; 1079 GNUNET_SCHEDULER_cancel (sock->write_task);
1080 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1081 sock->write_buffer_off = 0;
1082 }
1083 sock->nth.notify_ready = NULL;
1077 } 1084 }
1078 sock->nth.notify_ready = NULL;
1079 }
1080 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL)) 1085 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1081 { 1086 {
1082 GNUNET_RESOLVER_request_cancel (sock->dns_active); 1087 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1083 sock->dns_active = NULL; 1088 sock->dns_active = NULL;
1084 } 1089 }
1085 1090
1086 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 1091 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1087 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock); 1092 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
@@ -1097,8 +1102,8 @@ signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1097 GNUNET_CONNECTION_Receiver receiver; 1102 GNUNET_CONNECTION_Receiver receiver;
1098 1103
1099#if DEBUG_CONNECTION 1104#if DEBUG_CONNECTION
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1105 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 "Network signals time out to receiver (%p)!\n", sh); 1106 "Network signals time out to receiver (%p)!\n", sh);
1102#endif 1107#endif
1103 GNUNET_assert (NULL != (receiver = sh->receiver)); 1108 GNUNET_assert (NULL != (receiver = sh->receiver));
1104 sh->receiver = NULL; 1109 sh->receiver = NULL;
@@ -1136,63 +1141,62 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1136 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1141 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1137 if ((GNUNET_YES == sh->ignore_shutdown) && 1142 if ((GNUNET_YES == sh->ignore_shutdown) &&
1138 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1143 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1139 { 1144 {
1140 /* ignore shutdown request, go again immediately */ 1145 /* ignore shutdown request, go again immediately */
1141#if DEBUG_CONNECTION 1146#if DEBUG_CONNECTION
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "Ignoring shutdown signal per configuration\n"); 1148 "Ignoring shutdown signal per configuration\n");
1144#endif 1149#endif
1145 sh->read_task = 1150 sh->read_task =
1146 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1151 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1147 (sh->receive_timeout), sh->sock, 1152 (sh->receive_timeout), sh->sock,
1148 &receive_ready, sh); 1153 &receive_ready, sh);
1149 return; 1154 return;
1150 } 1155 }
1151 now = GNUNET_TIME_absolute_get (); 1156 now = GNUNET_TIME_absolute_get ();
1152 if ((now.abs_value > sh->receive_timeout.abs_value) || 1157 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1153 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) || 1158 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1154 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1159 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1155 { 1160 {
1156#if DEBUG_CONNECTION 1161#if DEBUG_CONNECTION
1157 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1162 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1163 LOG (GNUNET_ERROR_TYPE_DEBUG,
1159 "Receive from `%s' encounters error: time out by %llums... (%p)\n", 1164 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1160 GNUNET_a2s (sh->addr, sh->addrlen), 1165 GNUNET_a2s (sh->addr, sh->addrlen),
1161 GNUNET_TIME_absolute_get_duration (sh->receive_timeout). 1166 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1162 rel_value, sh); 1167 rel_value, sh);
1163#endif 1168#endif
1164 signal_timeout (sh); 1169 signal_timeout (sh);
1165 return; 1170 return;
1166 } 1171 }
1167 if (sh->sock == NULL) 1172 if (sh->sock == NULL)
1168 { 1173 {
1169 /* connect failed for good */ 1174 /* connect failed for good */
1170#if DEBUG_CONNECTION 1175#if DEBUG_CONNECTION
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1176 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Receive encounters error, socket closed... (%p)\n", sh); 1177 "Receive encounters error, socket closed... (%p)\n", sh);
1173#endif 1178#endif
1174 signal_error (sh, ECONNREFUSED); 1179 signal_error (sh, ECONNREFUSED);
1175 return; 1180 return;
1176 } 1181 }
1177 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock)); 1182 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1178RETRY: 1183RETRY:
1179 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max); 1184 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1180 if (ret == -1) 1185 if (ret == -1)
1181 { 1186 {
1182 if (errno == EINTR) 1187 if (errno == EINTR)
1183 goto RETRY; 1188 goto RETRY;
1184#if DEBUG_CONNECTION 1189#if DEBUG_CONNECTION
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", 1190 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n",
1186 STRERROR (errno)); 1191 STRERROR (errno));
1187#endif 1192#endif
1188 signal_error (sh, errno); 1193 signal_error (sh, errno);
1189 return; 1194 return;
1190 } 1195 }
1191#if DEBUG_CONNECTION 1196#if DEBUG_CONNECTION
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1193 "receive_ready read %u/%u bytes from `%s' (%p)!\n", 1198 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1194 (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen), 1199 (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1195 sh);
1196#endif 1200#endif
1197 GNUNET_assert (NULL != (receiver = sh->receiver)); 1201 GNUNET_assert (NULL != (receiver = sh->receiver));
1198 sh->receiver = NULL; 1202 sh->receiver = NULL;
@@ -1218,32 +1222,32 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 1222
1219 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1223 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1220 if (sh->sock == NULL) 1224 if (sh->sock == NULL)
1221 { 1225 {
1222 /* not connected and no longer trying */ 1226 /* not connected and no longer trying */
1223#if DEBUG_CONNECTION 1227#if DEBUG_CONNECTION
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Receive encounters error, socket closed (%p)...\n", sh); 1229 "Receive encounters error, socket closed (%p)...\n", sh);
1226#endif 1230#endif
1227 signal_error (sh, ECONNREFUSED); 1231 signal_error (sh, ECONNREFUSED);
1228 return; 1232 return;
1229 } 1233 }
1230 now = GNUNET_TIME_absolute_get (); 1234 now = GNUNET_TIME_absolute_get ();
1231 if ((now.abs_value > sh->receive_timeout.abs_value) || 1235 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1232 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1236 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1233 { 1237 {
1234#if DEBUG_CONNECTION 1238#if DEBUG_CONNECTION
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1236 "Receive encounters error: time out (%p)...\n", sh); 1240 "Receive encounters error: time out (%p)...\n", sh);
1237#endif 1241#endif
1238 signal_timeout (sh); 1242 signal_timeout (sh);
1239 return; 1243 return;
1240 } 1244 }
1241 GNUNET_assert (sh->sock != NULL); 1245 GNUNET_assert (sh->sock != NULL);
1242 /* connect succeeded, wait for data! */ 1246 /* connect succeeded, wait for data! */
1243 sh->read_task = 1247 sh->read_task =
1244 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1248 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1245 (sh->receive_timeout), sh->sock, 1249 (sh->receive_timeout), sh->sock,
1246 &receive_ready, sh); 1250 &receive_ready, sh);
1247} 1251}
1248 1252
1249 1253
@@ -1262,31 +1266,31 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1262 */ 1266 */
1263void 1267void
1264GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max, 1268GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1265 struct GNUNET_TIME_Relative timeout, 1269 struct GNUNET_TIME_Relative timeout,
1266 GNUNET_CONNECTION_Receiver receiver, 1270 GNUNET_CONNECTION_Receiver receiver,
1267 void *receiver_cls) 1271 void *receiver_cls)
1268{ 1272{
1269 struct GNUNET_SCHEDULER_TaskContext tc; 1273 struct GNUNET_SCHEDULER_TaskContext tc;
1270 1274
1271 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) && 1275 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1272 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) && 1276 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1273 (sock->receiver == NULL)); 1277 (sock->receiver == NULL));
1274 sock->receiver = receiver; 1278 sock->receiver = receiver;
1275 sock->receiver_cls = receiver_cls; 1279 sock->receiver_cls = receiver_cls;
1276 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 1280 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1277 sock->max = max; 1281 sock->max = max;
1278 if (sock->sock != NULL) 1282 if (sock->sock != NULL)
1279 { 1283 {
1280 memset (&tc, 0, sizeof (tc)); 1284 memset (&tc, 0, sizeof (tc));
1281 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; 1285 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1282 receive_again (sock, &tc); 1286 receive_again (sock, &tc);
1283 return; 1287 return;
1284 } 1288 }
1285 if ((sock->dns_active == NULL) && (sock->ap_head == NULL)) 1289 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1286 { 1290 {
1287 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); 1291 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1288 return; 1292 return;
1289 } 1293 }
1290 sock->ccs += COCO_RECEIVE_AGAIN; 1294 sock->ccs += COCO_RECEIVE_AGAIN;
1291} 1295}
1292 1296
@@ -1299,7 +1303,7 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1299 */ 1303 */
1300void 1304void
1301GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock, 1305GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1302 int do_ignore) 1306 int do_ignore)
1303{ 1307{
1304 sock->ignore_shutdown = do_ignore; 1308 sock->ignore_shutdown = do_ignore;
1305} 1309}
@@ -1317,15 +1321,15 @@ void *
1317GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock) 1321GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1318{ 1322{
1319 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1323 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1320 { 1324 {
1321 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task)); 1325 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1322 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1326 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1323 } 1327 }
1324 else 1328 else
1325 { 1329 {
1326 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN)); 1330 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1327 sock->ccs -= COCO_RECEIVE_AGAIN; 1331 sock->ccs -= COCO_RECEIVE_AGAIN;
1328 } 1332 }
1329 sock->receiver = NULL; 1333 sock->receiver = NULL;
1330 return sock->receiver_cls; 1334 return sock->receiver_cls;
1331} 1335}
@@ -1356,18 +1360,18 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
1356 return GNUNET_NO; 1360 return GNUNET_NO;
1357 sock->nth.notify_ready = NULL; 1361 sock->nth.notify_ready = NULL;
1358 if (sock->write_buffer_size - sock->write_buffer_off < size) 1362 if (sock->write_buffer_size - sock->write_buffer_off < size)
1359 { 1363 {
1360 /* need to compact */ 1364 /* need to compact */
1361 memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos], 1365 memmove (sock->write_buffer,
1362 used); 1366 &sock->write_buffer[sock->write_buffer_pos], used);
1363 sock->write_buffer_off -= sock->write_buffer_pos; 1367 sock->write_buffer_off -= sock->write_buffer_pos;
1364 sock->write_buffer_pos = 0; 1368 sock->write_buffer_pos = 0;
1365 } 1369 }
1366 avail = sock->write_buffer_size - sock->write_buffer_off; 1370 avail = sock->write_buffer_size - sock->write_buffer_off;
1367 GNUNET_assert (avail >= size); 1371 GNUNET_assert (avail >= size);
1368 size = 1372 size =
1369 notify (sock->nth.notify_ready_cls, avail, 1373 notify (sock->nth.notify_ready_cls, avail,
1370 &sock->write_buffer[sock->write_buffer_off]); 1374 &sock->write_buffer[sock->write_buffer_off]);
1371 GNUNET_assert (size <= avail); 1375 GNUNET_assert (size <= avail);
1372 sock->write_buffer_off += size; 1376 sock->write_buffer_off += size;
1373 return GNUNET_YES; 1377 return GNUNET_YES;
@@ -1392,17 +1396,17 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1392 GNUNET_CONNECTION_TransmitReadyNotify notify; 1396 GNUNET_CONNECTION_TransmitReadyNotify notify;
1393 1397
1394#if DEBUG_CONNECTION 1398#if DEBUG_CONNECTION
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock); 1399 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock);
1396#endif 1400#endif
1397 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 1401 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1398#if DEBUG_CONNECTION 1402#if DEBUG_CONNECTION
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1403 LOG (GNUNET_ERROR_TYPE_DEBUG,
1400 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", 1404 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1401 sock->hostname, sock->port, GNUNET_a2s (sock->addr, 1405 sock->hostname, sock->port, GNUNET_a2s (sock->addr,
1402 sock->addrlen), sock); 1406 sock->addrlen), sock);
1403#endif 1407#endif
1404 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY)); 1408 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1405 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */ 1409 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1406 notify = sock->nth.notify_ready; 1410 notify = sock->nth.notify_ready;
1407 sock->nth.notify_ready = NULL; 1411 sock->nth.notify_ready = NULL;
1408 notify (sock->nth.notify_ready_cls, 0, NULL); 1412 notify (sock->nth.notify_ready_cls, 0, NULL);
@@ -1425,9 +1429,9 @@ connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1425 GNUNET_CONNECTION_TransmitReadyNotify notify; 1429 GNUNET_CONNECTION_TransmitReadyNotify notify;
1426 1430
1427#if DEBUG_CONNECTION 1431#if DEBUG_CONNECTION
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1432 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n", 1433 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1430 sock->nth.notify_size, sock->hostname, sock->port, sock); 1434 sock->nth.notify_size, sock->hostname, sock->port, sock);
1431#endif 1435#endif
1432 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1436 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1433 notify = sock->nth.notify_ready; 1437 notify = sock->nth.notify_ready;
@@ -1447,20 +1451,20 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1447 GNUNET_CONNECTION_TransmitReadyNotify notify; 1451 GNUNET_CONNECTION_TransmitReadyNotify notify;
1448 1452
1449 if (NULL != sock->sock) 1453 if (NULL != sock->sock)
1450 { 1454 {
1451 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); 1455 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1452 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 1456 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
1453 sock->sock = NULL; 1457 sock->sock = NULL;
1454 } 1458 }
1455 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1459 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1456 { 1460 {
1457 GNUNET_SCHEDULER_cancel (sock->read_task); 1461 GNUNET_SCHEDULER_cancel (sock->read_task);
1458 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1462 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1459 signal_timeout (sock); 1463 signal_timeout (sock);
1460 return; 1464 return;
1461 } 1465 }
1462 if (sock->nth.notify_ready == NULL) 1466 if (sock->nth.notify_ready == NULL)
1463 return; /* nobody to tell about it */ 1467 return; /* nobody to tell about it */
1464 notify = sock->nth.notify_ready; 1468 notify = sock->nth.notify_ready;
1465 sock->nth.notify_ready = NULL; 1469 sock->nth.notify_ready = NULL;
1466 notify (sock->nth.notify_ready_cls, 0, NULL); 1470 notify (sock->nth.notify_ready_cls, 0, NULL);
@@ -1484,129 +1488,129 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1484 size_t have; 1488 size_t have;
1485 1489
1486#if DEBUG_CONNECTION 1490#if DEBUG_CONNECTION
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock); 1491 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
1488#endif 1492#endif
1489 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK); 1493 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1490 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1494 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1491 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 1495 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1492 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1496 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1493 { 1497 {
1494 if (sock->ignore_shutdown == GNUNET_YES) 1498 if (sock->ignore_shutdown == GNUNET_YES)
1495 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ 1499 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1496#if DEBUG_CONNECTION 1500#if DEBUG_CONNECTION
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1501 LOG (GNUNET_ERROR_TYPE_DEBUG,
1498 "Transmit to `%s' fails, shutdown happened (%p).\n", 1502 "Transmit to `%s' fails, shutdown happened (%p).\n",
1499 GNUNET_a2s (sock->addr, sock->addrlen), sock); 1503 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1500#endif 1504#endif
1501 notify = sock->nth.notify_ready; 1505 notify = sock->nth.notify_ready;
1502 if (NULL != notify) 1506 if (NULL != notify)
1503 { 1507 {
1504 sock->nth.notify_ready = NULL; 1508 sock->nth.notify_ready = NULL;
1505 notify (sock->nth.notify_ready_cls, 0, NULL); 1509 notify (sock->nth.notify_ready_cls, 0, NULL);
1510 }
1511 return;
1506 } 1512 }
1507 return;
1508 }
1509 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) 1513 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1510 { 1514 {
1511#if DEBUG_CONNECTION 1515#if DEBUG_CONNECTION
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1516 LOG (GNUNET_ERROR_TYPE_DEBUG,
1513 "Transmit to `%s' fails, time out reached (%p).\n", 1517 "Transmit to `%s' fails, time out reached (%p).\n",
1514 GNUNET_a2s (sock->addr, sock->addrlen), sock); 1518 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1515#endif 1519#endif
1516 notify = sock->nth.notify_ready; 1520 notify = sock->nth.notify_ready;
1517 GNUNET_assert (NULL != notify); 1521 GNUNET_assert (NULL != notify);
1518 sock->nth.notify_ready = NULL; 1522 sock->nth.notify_ready = NULL;
1519 notify (sock->nth.notify_ready_cls, 0, NULL); 1523 notify (sock->nth.notify_ready_cls, 0, NULL);
1520 return; 1524 return;
1521 } 1525 }
1522 GNUNET_assert (NULL != sock->sock); 1526 GNUNET_assert (NULL != sock->sock);
1523 if (tc->write_ready == NULL) 1527 if (tc->write_ready == NULL)
1524 { 1528 {
1525 /* special circumstances (in particular, 1529 /* special circumstances (in particular,
1526 * PREREQ_DONE after connect): not yet ready to write, 1530 * PREREQ_DONE after connect): not yet ready to write,
1527 * but no "fatal" error either. Hence retry. */ 1531 * but no "fatal" error either. Hence retry. */
1528 goto SCHEDULE_WRITE; 1532 goto SCHEDULE_WRITE;
1529 } 1533 }
1530 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)) 1534 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1531 { 1535 {
1532#if DEBUG_CONNECTION 1536#if DEBUG_CONNECTION
1533 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1537 LOG (GNUNET_ERROR_TYPE_INFO,
1534 _ 1538 _
1535 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"), 1539 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1536 sock); 1540 sock);
1537#endif 1541#endif
1538 transmit_error (sock); 1542 transmit_error (sock);
1539 return; /* connect failed for good, we're finished */ 1543 return; /* connect failed for good, we're finished */
1540 } 1544 }
1541 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos); 1545 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1542 if ((sock->nth.notify_ready != NULL) && 1546 if ((sock->nth.notify_ready != NULL) &&
1543 (sock->write_buffer_size < sock->nth.notify_size)) 1547 (sock->write_buffer_size < sock->nth.notify_size))
1544 { 1548 {
1545 sock->write_buffer = 1549 sock->write_buffer =
1546 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size); 1550 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
1547 sock->write_buffer_size = sock->nth.notify_size; 1551 sock->write_buffer_size = sock->nth.notify_size;
1548 } 1552 }
1549 process_notify (sock); 1553 process_notify (sock);
1550 have = sock->write_buffer_off - sock->write_buffer_pos; 1554 have = sock->write_buffer_off - sock->write_buffer_pos;
1551 if (have == 0) 1555 if (have == 0)
1552 { 1556 {
1553 /* no data ready for writing, terminate write loop */ 1557 /* no data ready for writing, terminate write loop */
1554 return; 1558 return;
1555 } 1559 }
1556 GNUNET_assert (have <= sock->write_buffer_size); 1560 GNUNET_assert (have <= sock->write_buffer_size);
1557 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size); 1561 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1558 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size); 1562 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1559RETRY: 1563RETRY:
1560 ret = 1564 ret =
1561 GNUNET_NETWORK_socket_send (sock->sock, 1565 GNUNET_NETWORK_socket_send (sock->sock,
1562 &sock->write_buffer[sock->write_buffer_pos], 1566 &sock->write_buffer[sock->write_buffer_pos],
1563 have); 1567 have);
1564 if (ret == -1) 1568 if (ret == -1)
1565 { 1569 {
1566 if (errno == EINTR) 1570 if (errno == EINTR)
1567 goto RETRY; 1571 goto RETRY;
1568#if 0 1572#if 0
1569 int en = errno; 1573 int en = errno;
1570 1574
1571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"), 1575 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"),
1572 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en)); 1576 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en));
1573#endif 1577#endif
1574#if DEBUG_CONNECTION 1578#if DEBUG_CONNECTION
1575 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send"); 1579 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1576#endif 1580#endif
1577 transmit_error (sock); 1581 transmit_error (sock);
1578 return; 1582 return;
1579 } 1583 }
1580#if DEBUG_CONNECTION 1584#if DEBUG_CONNECTION
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1585 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", 1586 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1583 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), 1587 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen),
1584 sock); 1588 sock);
1585#endif 1589#endif
1586 sock->write_buffer_pos += ret; 1590 sock->write_buffer_pos += ret;
1587 if (sock->write_buffer_pos == sock->write_buffer_off) 1591 if (sock->write_buffer_pos == sock->write_buffer_off)
1588 { 1592 {
1589 /* transmitted all pending data */ 1593 /* transmitted all pending data */
1590 sock->write_buffer_pos = 0; 1594 sock->write_buffer_pos = 0;
1591 sock->write_buffer_off = 0; 1595 sock->write_buffer_off = 0;
1592 } 1596 }
1593 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready)) 1597 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1594 return; /* all data sent! */ 1598 return; /* all data sent! */
1595 /* not done writing, schedule more */ 1599 /* not done writing, schedule more */
1596SCHEDULE_WRITE: 1600SCHEDULE_WRITE:
1597#if DEBUG_CONNECTION 1601#if DEBUG_CONNECTION
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1602 LOG (GNUNET_ERROR_TYPE_DEBUG,
1599 "Re-scheduling transmit_ready (more to do) (%p).\n", sock); 1603 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1600#endif 1604#endif
1601 have = sock->write_buffer_off - sock->write_buffer_pos; 1605 have = sock->write_buffer_off - sock->write_buffer_pos;
1602 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0)); 1606 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
1603 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) 1607 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1604 sock->write_task = 1608 sock->write_task =
1605 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == 1609 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
1606 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : 1610 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1607 GNUNET_TIME_absolute_get_remaining 1611 GNUNET_TIME_absolute_get_remaining
1608 (sock->nth.transmit_timeout), 1612 (sock->nth.transmit_timeout),
1609 sock->sock, &transmit_ready, sock); 1613 sock->sock, &transmit_ready, sock);
1610} 1614}
1611 1615
1612 1616
@@ -1625,17 +1629,17 @@ SCHEDULE_WRITE:
1625 * NULL if we are already going to notify someone else (busy) 1629 * NULL if we are already going to notify someone else (busy)
1626 */ 1630 */
1627struct GNUNET_CONNECTION_TransmitHandle * 1631struct GNUNET_CONNECTION_TransmitHandle *
1628GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock, 1632GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1629 size_t size, 1633 *sock, size_t size,
1630 struct GNUNET_TIME_Relative timeout, 1634 struct GNUNET_TIME_Relative timeout,
1631 GNUNET_CONNECTION_TransmitReadyNotify 1635 GNUNET_CONNECTION_TransmitReadyNotify
1632 notify, void *notify_cls) 1636 notify, void *notify_cls)
1633{ 1637{
1634 if (sock->nth.notify_ready != NULL) 1638 if (sock->nth.notify_ready != NULL)
1635 { 1639 {
1636 GNUNET_assert (0); 1640 GNUNET_assert (0);
1637 return NULL; 1641 return NULL;
1638 } 1642 }
1639 GNUNET_assert (notify != NULL); 1643 GNUNET_assert (notify != NULL);
1640 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); 1644 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1641 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size); 1645 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
@@ -1649,36 +1653,36 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1649 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task); 1653 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1650 if ((sock->sock == NULL) && (sock->ap_head == NULL) && 1654 if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
1651 (sock->dns_active == NULL)) 1655 (sock->dns_active == NULL))
1652 { 1656 {
1653 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 1657 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1654 GNUNET_SCHEDULER_cancel (sock->write_task); 1658 GNUNET_SCHEDULER_cancel (sock->write_task);
1655 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock); 1659 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1656 return &sock->nth; 1660 return &sock->nth;
1657 } 1661 }
1658 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task) 1662 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1659 return &sock->nth; 1663 return &sock->nth;
1660 if (sock->sock != NULL) 1664 if (sock->sock != NULL)
1661 { 1665 {
1662#if DEBUG_CONNECTION 1666#if DEBUG_CONNECTION
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", 1667 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n",
1664 sock); 1668 sock);
1665#endif 1669#endif
1666 sock->write_task = 1670 sock->write_task =
1667 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 1671 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1668 (sock->nth.transmit_timeout), 1672 (sock->nth.transmit_timeout),
1669 sock->sock, &transmit_ready, sock); 1673 sock->sock, &transmit_ready, sock);
1670 } 1674 }
1671 else 1675 else
1672 { 1676 {
1673#if DEBUG_CONNECTION 1677#if DEBUG_CONNECTION
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1678 LOG (GNUNET_ERROR_TYPE_DEBUG,
1675 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", 1679 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1676 sock); 1680 sock);
1677#endif 1681#endif
1678 sock->ccs |= COCO_TRANSMIT_READY; 1682 sock->ccs |= COCO_TRANSMIT_READY;
1679 sock->nth.timeout_task = 1683 sock->nth.timeout_task =
1680 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock); 1684 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
1681 } 1685 }
1682 return &sock->nth; 1686 return &sock->nth;
1683} 1687}
1684 1688
@@ -1690,28 +1694,28 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1690 */ 1694 */
1691void 1695void
1692GNUNET_CONNECTION_notify_transmit_ready_cancel (struct 1696GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1693 GNUNET_CONNECTION_TransmitHandle 1697 GNUNET_CONNECTION_TransmitHandle
1694 *th) 1698 *th)
1695{ 1699{
1696 GNUNET_assert (th->notify_ready != NULL); 1700 GNUNET_assert (th->notify_ready != NULL);
1697 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY)) 1701 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
1698 { 1702 {
1699#if DEBUG_CONNECTION 1703#if DEBUG_CONNECTION
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1704 LOG (GNUNET_ERROR_TYPE_DEBUG,
1701 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th); 1705 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1702#endif 1706#endif
1703 GNUNET_SCHEDULER_cancel (th->timeout_task); 1707 GNUNET_SCHEDULER_cancel (th->timeout_task);
1704 th->timeout_task = GNUNET_SCHEDULER_NO_TASK; 1708 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1705 th->sh->ccs -= COCO_TRANSMIT_READY; 1709 th->sh->ccs -= COCO_TRANSMIT_READY;
1706 } 1710 }
1707 else 1711 else
1708 {
1709 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1710 { 1712 {
1711 GNUNET_SCHEDULER_cancel (th->sh->write_task); 1713 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1712 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK; 1714 {
1715 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1716 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1717 }
1713 } 1718 }
1714 }
1715 th->notify_ready = NULL; 1719 th->notify_ready = NULL;
1716} 1720}
1717 1721
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index b7be764b5..a33b619ff 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -44,6 +44,12 @@
44#include "gnunet_container_lib.h" 44#include "gnunet_container_lib.h"
45#include "gnunet_disk_lib.h" 45#include "gnunet_disk_lib.h"
46 46
47#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
48
49#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
50
51#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
52
47struct GNUNET_CONTAINER_BloomFilter 53struct GNUNET_CONTAINER_BloomFilter
48{ 54{
49 55
@@ -83,8 +89,8 @@ struct GNUNET_CONTAINER_BloomFilter
83 * @return number of bytes used for the data of the bloom filter 89 * @return number of bytes used for the data of the bloom filter
84 */ 90 */
85size_t 91size_t
86GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter 92GNUNET_CONTAINER_bloomfilter_get_size (const struct
87 *bf) 93 GNUNET_CONTAINER_BloomFilter *bf)
88{ 94{
89 if (bf == NULL) 95 if (bf == NULL)
90 return 0; 96 return 0;
@@ -100,10 +106,10 @@ GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
100 */ 106 */
101struct GNUNET_CONTAINER_BloomFilter * 107struct GNUNET_CONTAINER_BloomFilter *
102GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter 108GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter
103 *bf) 109 *bf)
104{ 110{
105 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize, 111 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize,
106 bf->addressesPerElement); 112 bf->addressesPerElement);
107} 113}
108 114
109 115
@@ -175,7 +181,7 @@ testBit (char *bitArray, unsigned int bitIdx)
175 */ 181 */
176static void 182static void
177incrementBit (char *bitArray, unsigned int bitIdx, 183incrementBit (char *bitArray, unsigned int bitIdx,
178 const struct GNUNET_DISK_FileHandle *fh) 184 const struct GNUNET_DISK_FileHandle *fh)
179{ 185{
180 off_t fileSlot; 186 off_t fileSlot;
181 unsigned char value; 187 unsigned char value;
@@ -191,25 +197,25 @@ incrementBit (char *bitArray, unsigned int bitIdx,
191 targetLoc = bitIdx % 2; 197 targetLoc = bitIdx % 2;
192 198
193 GNUNET_assert (fileSlot == 199 GNUNET_assert (fileSlot ==
194 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); 200 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
195 if (1 != GNUNET_DISK_file_read (fh, &value, 1)) 201 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
196 value = 0; 202 value = 0;
197 low = value & 0xF; 203 low = value & 0xF;
198 high = (value & (~0xF)) >> 4; 204 high = (value & (~0xF)) >> 4;
199 205
200 if (targetLoc == 0) 206 if (targetLoc == 0)
201 { 207 {
202 if (low < 0xF) 208 if (low < 0xF)
203 low++; 209 low++;
204 } 210 }
205 else 211 else
206 { 212 {
207 if (high < 0xF) 213 if (high < 0xF)
208 high++; 214 high++;
209 } 215 }
210 value = ((high << 4) | low); 216 value = ((high << 4) | low);
211 GNUNET_assert (fileSlot == 217 GNUNET_assert (fileSlot ==
212 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); 218 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
213 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1)); 219 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
214} 220}
215 221
@@ -223,7 +229,7 @@ incrementBit (char *bitArray, unsigned int bitIdx,
223 */ 229 */
224static void 230static void
225decrementBit (char *bitArray, unsigned int bitIdx, 231decrementBit (char *bitArray, unsigned int bitIdx,
226 const struct GNUNET_DISK_FileHandle *fh) 232 const struct GNUNET_DISK_FileHandle *fh)
227{ 233{
228 off_t fileSlot; 234 off_t fileSlot;
229 unsigned char value; 235 unsigned char value;
@@ -232,7 +238,7 @@ decrementBit (char *bitArray, unsigned int bitIdx,
232 unsigned int targetLoc; 238 unsigned int targetLoc;
233 239
234 if (GNUNET_DISK_handle_invalid (fh)) 240 if (GNUNET_DISK_handle_invalid (fh))
235 return; /* cannot decrement! */ 241 return; /* cannot decrement! */
236 /* Each char slot in the counter file holds two 4 bit counters */ 242 /* Each char slot in the counter file holds two 4 bit counters */
237 fileSlot = bitIdx / 2; 243 fileSlot = bitIdx / 2;
238 targetLoc = bitIdx % 2; 244 targetLoc = bitIdx % 2;
@@ -244,23 +250,23 @@ decrementBit (char *bitArray, unsigned int bitIdx,
244 250
245 /* decrement, but once we have reached the max, never go back! */ 251 /* decrement, but once we have reached the max, never go back! */
246 if (targetLoc == 0) 252 if (targetLoc == 0)
247 {
248 if ((low > 0) && (low < 0xF))
249 low--;
250 if (low == 0)
251 { 253 {
252 clearBit (bitArray, bitIdx); 254 if ((low > 0) && (low < 0xF))
255 low--;
256 if (low == 0)
257 {
258 clearBit (bitArray, bitIdx);
259 }
253 } 260 }
254 }
255 else 261 else
256 {
257 if ((high > 0) && (high < 0xF))
258 high--;
259 if (high == 0)
260 { 262 {
261 clearBit (bitArray, bitIdx); 263 if ((high > 0) && (high < 0xF))
264 high--;
265 if (high == 0)
266 {
267 clearBit (bitArray, bitIdx);
268 }
262 } 269 }
263 }
264 value = ((high << 4) | low); 270 value = ((high << 4) | low);
265 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET); 271 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
266 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1)); 272 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
@@ -289,19 +295,19 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
289 GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET); 295 GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
290 296
291 while (bytesleft > 0) 297 while (bytesleft > 0)
292 {
293 if (bytesleft > BUFFSIZE)
294 { 298 {
295 res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE); 299 if (bytesleft > BUFFSIZE)
296 bytesleft -= BUFFSIZE; 300 {
301 res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE);
302 bytesleft -= BUFFSIZE;
303 }
304 else
305 {
306 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
307 bytesleft = 0;
308 }
309 GNUNET_assert (res != GNUNET_SYSERR);
297 } 310 }
298 else
299 {
300 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
301 bytesleft = 0;
302 }
303 GNUNET_assert (res != GNUNET_SYSERR);
304 }
305 GNUNET_free (buffer); 311 GNUNET_free (buffer);
306 return GNUNET_OK; 312 return GNUNET_OK;
307} 313}
@@ -334,7 +340,7 @@ typedef int (*BitIterator) (void *cls,
334 */ 340 */
335static void 341static void
336iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf, 342iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
337 BitIterator callback, void *arg, const GNUNET_HashCode * key) 343 BitIterator callback, void *arg, const GNUNET_HashCode * key)
338{ 344{
339 GNUNET_HashCode tmp[2]; 345 GNUNET_HashCode tmp[2];
340 int bitCount; 346 int bitCount;
@@ -345,27 +351,27 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
345 tmp[0] = *key; 351 tmp[0] = *key;
346 round = 0; 352 round = 0;
347 while (bitCount > 0) 353 while (bitCount > 0)
348 {
349 while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
350 {
351 if (GNUNET_YES !=
352 callback (arg, bf,
353 (((uint32_t *) & tmp[round & 1])[slot]) &
354 ((bf->bitArraySize * 8) - 1)))
355 return;
356 slot++;
357 bitCount--;
358 if (bitCount == 0)
359 break;
360 }
361 if (bitCount > 0)
362 { 354 {
363 GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode), 355 while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
364 &tmp[(round + 1) & 1]); 356 {
365 round++; 357 if (GNUNET_YES !=
366 slot = 0; 358 callback (arg, bf,
359 (((uint32_t *) & tmp[round & 1])[slot]) &
360 ((bf->bitArraySize * 8) - 1)))
361 return;
362 slot++;
363 bitCount--;
364 if (bitCount == 0)
365 break;
366 }
367 if (bitCount > 0)
368 {
369 GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode),
370 &tmp[(round + 1) & 1]);
371 round++;
372 slot = 0;
373 }
367 } 374 }
368 }
369} 375}
370 376
371 377
@@ -378,8 +384,9 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
378 * @return GNUNET_YES 384 * @return GNUNET_YES
379 */ 385 */
380static int 386static int
381incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, 387incrementBitCallback (void *cls,
382 unsigned int bit) 388 const struct GNUNET_CONTAINER_BloomFilter *bf,
389 unsigned int bit)
383{ 390{
384 struct GNUNET_CONTAINER_BloomFilter *b = cls; 391 struct GNUNET_CONTAINER_BloomFilter *b = cls;
385 392
@@ -397,8 +404,9 @@ incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
397 * @return GNUNET_YES 404 * @return GNUNET_YES
398 */ 405 */
399static int 406static int
400decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, 407decrementBitCallback (void *cls,
401 unsigned int bit) 408 const struct GNUNET_CONTAINER_BloomFilter *bf,
409 unsigned int bit)
402{ 410{
403 struct GNUNET_CONTAINER_BloomFilter *b = cls; 411 struct GNUNET_CONTAINER_BloomFilter *b = cls;
404 412
@@ -417,15 +425,15 @@ decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
417 */ 425 */
418static int 426static int
419testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, 427testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
420 unsigned int bit) 428 unsigned int bit)
421{ 429{
422 int *arg = cls; 430 int *arg = cls;
423 431
424 if (GNUNET_NO == testBit (bf->bitArray, bit)) 432 if (GNUNET_NO == testBit (bf->bitArray, bit))
425 { 433 {
426 *arg = GNUNET_NO; 434 *arg = GNUNET_NO;
427 return GNUNET_NO; 435 return GNUNET_NO;
428 } 436 }
429 return GNUNET_YES; 437 return GNUNET_YES;
430} 438}
431 439
@@ -443,7 +451,7 @@ testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
443 */ 451 */
444struct GNUNET_CONTAINER_BloomFilter * 452struct GNUNET_CONTAINER_BloomFilter *
445GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, 453GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
446 unsigned int k) 454 unsigned int k)
447{ 455{
448 struct GNUNET_CONTAINER_BloomFilter *bf; 456 struct GNUNET_CONTAINER_BloomFilter *bf;
449 char *rbuff; 457 char *rbuff;
@@ -459,32 +467,32 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
459 ui = 1; 467 ui = 1;
460 while (ui < size) 468 while (ui < size)
461 ui *= 2; 469 ui *= 2;
462 size = ui; /* make sure it's a power of 2 */ 470 size = ui; /* make sure it's a power of 2 */
463 471
464 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); 472 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
465 /* Try to open a bloomfilter file */ 473 /* Try to open a bloomfilter file */
466 bf->fh = 474 bf->fh =
467 GNUNET_DISK_file_open (filename, 475 GNUNET_DISK_file_open (filename,
468 GNUNET_DISK_OPEN_READWRITE | 476 GNUNET_DISK_OPEN_READWRITE |
469 GNUNET_DISK_OPEN_CREATE, 477 GNUNET_DISK_OPEN_CREATE,
470 GNUNET_DISK_PERM_USER_READ | 478 GNUNET_DISK_PERM_USER_READ |
471 GNUNET_DISK_PERM_USER_WRITE); 479 GNUNET_DISK_PERM_USER_WRITE);
472 if (NULL == bf->fh) 480 if (NULL == bf->fh)
473 { 481 {
474 GNUNET_free (bf); 482 GNUNET_free (bf);
475 return NULL; 483 return NULL;
476 } 484 }
477 bf->filename = GNUNET_strdup (filename); 485 bf->filename = GNUNET_strdup (filename);
478 /* Alloc block */ 486 /* Alloc block */
479 bf->bitArray = GNUNET_malloc_large (size); 487 bf->bitArray = GNUNET_malloc_large (size);
480 if (bf->bitArray == NULL) 488 if (bf->bitArray == NULL)
481 { 489 {
482 if (bf->fh != NULL) 490 if (bf->fh != NULL)
483 GNUNET_DISK_file_close (bf->fh); 491 GNUNET_DISK_file_close (bf->fh);
484 GNUNET_free (bf->filename); 492 GNUNET_free (bf->filename);
485 GNUNET_free (bf); 493 GNUNET_free (bf);
486 return NULL; 494 return NULL;
487 } 495 }
488 bf->bitArraySize = size; 496 bf->bitArraySize = size;
489 bf->addressesPerElement = k; 497 bf->addressesPerElement = k;
490 memset (bf->bitArray, 0, bf->bitArraySize); 498 memset (bf->bitArray, 0, bf->bitArraySize);
@@ -493,28 +501,27 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
493 rbuff = GNUNET_malloc (BUFFSIZE); 501 rbuff = GNUNET_malloc (BUFFSIZE);
494 pos = 0; 502 pos = 0;
495 while (pos < size * 8) 503 while (pos < size * 8)
496 {
497 int res;
498
499 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
500 if (res == -1)
501 {
502 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read",
503 bf->filename);
504 }
505 if (res == 0)
506 break; /* is ok! we just did not use that many bits yet */
507 for (i = 0; i < res; i++)
508 { 504 {
509 if ((rbuff[i] & 0x0F) != 0) 505 int res;
510 setBit (bf->bitArray, pos + i * 2); 506
511 if ((rbuff[i] & 0xF0) != 0) 507 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
512 setBit (bf->bitArray, pos + i * 2 + 1); 508 if (res == -1)
509 {
510 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
511 }
512 if (res == 0)
513 break; /* is ok! we just did not use that many bits yet */
514 for (i = 0; i < res; i++)
515 {
516 if ((rbuff[i] & 0x0F) != 0)
517 setBit (bf->bitArray, pos + i * 2);
518 if ((rbuff[i] & 0xF0) != 0)
519 setBit (bf->bitArray, pos + i * 2 + 1);
520 }
521 if (res < BUFFSIZE)
522 break;
523 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
513 } 524 }
514 if (res < BUFFSIZE)
515 break;
516 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
517 }
518 GNUNET_free (rbuff); 525 GNUNET_free (rbuff);
519 return bf; 526 return bf;
520} 527}
@@ -535,7 +542,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
535 */ 542 */
536struct GNUNET_CONTAINER_BloomFilter * 543struct GNUNET_CONTAINER_BloomFilter *
537GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size, 544GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
538 unsigned int k) 545 unsigned int k)
539{ 546{
540 struct GNUNET_CONTAINER_BloomFilter *bf; 547 struct GNUNET_CONTAINER_BloomFilter *bf;
541 size_t ui; 548 size_t ui;
@@ -546,19 +553,19 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
546 while (ui < size) 553 while (ui < size)
547 ui *= 2; 554 ui *= 2;
548 if (size != ui) 555 if (size != ui)
549 { 556 {
550 GNUNET_break (0); 557 GNUNET_break (0);
551 return NULL; 558 return NULL;
552 } 559 }
553 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); 560 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
554 bf->filename = NULL; 561 bf->filename = NULL;
555 bf->fh = NULL; 562 bf->fh = NULL;
556 bf->bitArray = GNUNET_malloc_large (size); 563 bf->bitArray = GNUNET_malloc_large (size);
557 if (bf->bitArray == NULL) 564 if (bf->bitArray == NULL)
558 { 565 {
559 GNUNET_free (bf); 566 GNUNET_free (bf);
560 return NULL; 567 return NULL;
561 } 568 }
562 bf->bitArraySize = size; 569 bf->bitArraySize = size;
563 bf->addressesPerElement = k; 570 bf->addressesPerElement = k;
564 if (data != NULL) 571 if (data != NULL)
@@ -580,8 +587,8 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
580 */ 587 */
581int 588int
582GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct 589GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
583 GNUNET_CONTAINER_BloomFilter *bf, 590 GNUNET_CONTAINER_BloomFilter *bf,
584 char *data, size_t size) 591 char *data, size_t size)
585{ 592{
586 if (NULL == bf) 593 if (NULL == bf)
587 return GNUNET_SYSERR; 594 return GNUNET_SYSERR;
@@ -638,7 +645,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
638 */ 645 */
639int 646int
640GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter 647GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
641 *bf, const GNUNET_HashCode * e) 648 *bf, const GNUNET_HashCode * e)
642{ 649{
643 int res; 650 int res;
644 651
@@ -658,7 +665,7 @@ GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
658 */ 665 */
659void 666void
660GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf, 667GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
661 const GNUNET_HashCode * e) 668 const GNUNET_HashCode * e)
662{ 669{
663 if (NULL == bf) 670 if (NULL == bf)
664 return; 671 return;
@@ -678,7 +685,7 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
678 */ 685 */
679int 686int
680GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf, 687GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
681 const char *data, size_t size) 688 const char *data, size_t size)
682{ 689{
683 unsigned int i; 690 unsigned int i;
684 unsigned int n; 691 unsigned int n;
@@ -712,8 +719,8 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
712 */ 719 */
713int 720int
714GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf, 721GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
715 const struct GNUNET_CONTAINER_BloomFilter 722 const struct GNUNET_CONTAINER_BloomFilter
716 *to_or, size_t size) 723 *to_or, size_t size)
717{ 724{
718 unsigned int i; 725 unsigned int i;
719 unsigned int n; 726 unsigned int n;
@@ -743,7 +750,7 @@ GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
743 */ 750 */
744void 751void
745GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf, 752GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
746 const GNUNET_HashCode * e) 753 const GNUNET_HashCode * e)
747{ 754{
748 if (NULL == bf) 755 if (NULL == bf)
749 return; 756 return;
@@ -765,9 +772,9 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
765 */ 772 */
766void 773void
767GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, 774GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
768 GNUNET_HashCodeIterator iterator, 775 GNUNET_HashCodeIterator iterator,
769 void *iterator_cls, size_t size, 776 void *iterator_cls, size_t size,
770 unsigned int k) 777 unsigned int k)
771{ 778{
772 GNUNET_HashCode hc; 779 GNUNET_HashCode hc;
773 unsigned int i; 780 unsigned int i;
@@ -776,7 +783,7 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
776 i = 1; 783 i = 1;
777 while (i < size) 784 while (i < size)
778 i *= 2; 785 i *= 2;
779 size = i; /* make sure it's a power of 2 */ 786 size = i; /* make sure it's a power of 2 */
780 787
781 bf->bitArraySize = size; 788 bf->bitArraySize = size;
782 bf->bitArray = GNUNET_malloc (size); 789 bf->bitArray = GNUNET_malloc (size);
diff --git a/src/util/container_heap.c b/src/util/container_heap.c
index cd4d7909a..051b85a25 100644
--- a/src/util/container_heap.c
+++ b/src/util/container_heap.c
@@ -28,6 +28,7 @@
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30 30
31#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31 32
32#define DEBUG 0 33#define DEBUG 0
33 34
@@ -115,10 +116,10 @@ check (const struct GNUNET_CONTAINER_HeapNode *node)
115 if (NULL == node) 116 if (NULL == node)
116 return; 117 return;
117 GNUNET_assert (node->tree_size == 118 GNUNET_assert (node->tree_size ==
118 ((node->left_child == 119 ((node->left_child ==
119 NULL) ? 0 : 1 + node->left_child->tree_size) + 120 NULL) ? 0 : 1 + node->left_child->tree_size) +
120 ((node->right_child == 121 ((node->right_child ==
121 NULL) ? 0 : 1 + node->right_child->tree_size)); 122 NULL) ? 0 : 1 + node->right_child->tree_size));
122 check (node->left_child); 123 check (node->left_child);
123 check (node->right_child); 124 check (node->right_child);
124} 125}
@@ -197,7 +198,7 @@ GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap)
197 */ 198 */
198GNUNET_CONTAINER_HeapCostType 199GNUNET_CONTAINER_HeapCostType
199GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode 200GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
200 *node) 201 * node)
201{ 202{
202 return node->cost; 203 return node->cost;
203} 204}
@@ -213,8 +214,8 @@ GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
213 */ 214 */
214static int 215static int
215node_iterator (const struct GNUNET_CONTAINER_Heap *heap, 216node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
216 struct GNUNET_CONTAINER_HeapNode *node, 217 struct GNUNET_CONTAINER_HeapNode *node,
217 GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls) 218 GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls)
218{ 219{
219 if (node == NULL) 220 if (node == NULL)
220 return GNUNET_YES; 221 return GNUNET_YES;
@@ -237,8 +238,8 @@ node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
237 */ 238 */
238void 239void
239GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap, 240GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
240 GNUNET_CONTAINER_HeapIterator iterator, 241 GNUNET_CONTAINER_HeapIterator iterator,
241 void *iterator_cls) 242 void *iterator_cls)
242{ 243{
243 (void) node_iterator (heap, heap->root, iterator, iterator_cls); 244 (void) node_iterator (heap, heap->root, iterator, iterator_cls);
244} 245}
@@ -268,9 +269,9 @@ GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
268 pos = heap->root; 269 pos = heap->root;
269 element = pos->element; 270 element = pos->element;
270 heap->walk_pos = 271 heap->walk_pos =
271 (0 == 272 (0 ==
272 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 273 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
273 2)) ? pos->right_child : pos->left_child; 274 2)) ? pos->right_child : pos->left_child;
274 return element; 275 return element;
275} 276}
276 277
@@ -285,51 +286,51 @@ GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
285 */ 286 */
286static void 287static void
287insert_node (struct GNUNET_CONTAINER_Heap *heap, 288insert_node (struct GNUNET_CONTAINER_Heap *heap,
288 struct GNUNET_CONTAINER_HeapNode *pos, 289 struct GNUNET_CONTAINER_HeapNode *pos,
289 struct GNUNET_CONTAINER_HeapNode *node) 290 struct GNUNET_CONTAINER_HeapNode *node)
290{ 291{
291 struct GNUNET_CONTAINER_HeapNode *parent; 292 struct GNUNET_CONTAINER_HeapNode *parent;
292 293
293 GNUNET_assert (node->parent == NULL); 294 GNUNET_assert (node->parent == NULL);
294 while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >= 295 while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >=
295 node->cost) 296 node->cost)
296 : (pos->cost <= node->cost)) 297 : (pos->cost <= node->cost))
297 {
298 /* node is descendent of pos */
299 pos->tree_size += (1 + node->tree_size);
300 if (pos->left_child == NULL)
301 { 298 {
302 pos->left_child = node; 299 /* node is descendent of pos */
303 node->parent = pos; 300 pos->tree_size += (1 + node->tree_size);
304 return; 301 if (pos->left_child == NULL)
302 {
303 pos->left_child = node;
304 node->parent = pos;
305 return;
306 }
307 if (pos->right_child == NULL)
308 {
309 pos->right_child = node;
310 node->parent = pos;
311 return;
312 }
313 /* keep it balanced by descending into smaller subtree */
314 if (pos->left_child->tree_size < pos->right_child->tree_size)
315 pos = pos->left_child;
316 else
317 pos = pos->right_child;
305 } 318 }
306 if (pos->right_child == NULL)
307 {
308 pos->right_child = node;
309 node->parent = pos;
310 return;
311 }
312 /* keep it balanced by descending into smaller subtree */
313 if (pos->left_child->tree_size < pos->right_child->tree_size)
314 pos = pos->left_child;
315 else
316 pos = pos->right_child;
317 }
318 /* make 'node' parent of 'pos' */ 319 /* make 'node' parent of 'pos' */
319 parent = pos->parent; 320 parent = pos->parent;
320 pos->parent = NULL; 321 pos->parent = NULL;
321 node->parent = parent; 322 node->parent = parent;
322 if (NULL == parent) 323 if (NULL == parent)
323 { 324 {
324 heap->root = node; 325 heap->root = node;
325 } 326 }
326 else 327 else
327 { 328 {
328 if (parent->left_child == pos) 329 if (parent->left_child == pos)
329 parent->left_child = node; 330 parent->left_child = node;
330 else 331 else
331 parent->right_child = node; 332 parent->right_child = node;
332 } 333 }
333 /* insert 'pos' below 'node' */ 334 /* insert 'pos' below 'node' */
334 insert_node (heap, node, pos); 335 insert_node (heap, node, pos);
335 CHECK (pos); 336 CHECK (pos);
@@ -345,8 +346,9 @@ insert_node (struct GNUNET_CONTAINER_Heap *heap,
345 * @return node for the new element 346 * @return node for the new element
346 */ 347 */
347struct GNUNET_CONTAINER_HeapNode * 348struct GNUNET_CONTAINER_HeapNode *
348GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element, 349GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
349 GNUNET_CONTAINER_HeapCostType cost) 350 void *element,
351 GNUNET_CONTAINER_HeapCostType cost)
350{ 352{
351 struct GNUNET_CONTAINER_HeapNode *node; 353 struct GNUNET_CONTAINER_HeapNode *node;
352 354
@@ -382,27 +384,27 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
382 heap->size--; 384 heap->size--;
383 ret = root->element; 385 ret = root->element;
384 if (root->left_child == NULL) 386 if (root->left_child == NULL)
385 { 387 {
386 heap->root = root->right_child; 388 heap->root = root->right_child;
387 if (root->right_child != NULL) 389 if (root->right_child != NULL)
388 root->right_child->parent = NULL; 390 root->right_child->parent = NULL;
389 } 391 }
390 else if (root->right_child == NULL) 392 else if (root->right_child == NULL)
391 { 393 {
392 heap->root = root->left_child; 394 heap->root = root->left_child;
393 root->left_child->parent = NULL; 395 root->left_child->parent = NULL;
394 } 396 }
395 else 397 else
396 { 398 {
397 root->left_child->parent = NULL; 399 root->left_child->parent = NULL;
398 root->right_child->parent = NULL; 400 root->right_child->parent = NULL;
399 heap->root = root->left_child; 401 heap->root = root->left_child;
400 insert_node (heap, heap->root, root->right_child); 402 insert_node (heap, heap->root, root->right_child);
401 } 403 }
402 GNUNET_free (root); 404 GNUNET_free (root);
403#if DEBUG 405#if DEBUG
404 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 406 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
405 (heap->size == heap->root->tree_size + 1)); 407 (heap->size == heap->root->tree_size + 1));
406 CHECK (heap->root); 408 CHECK (heap->root);
407#endif 409#endif
408 return ret; 410 return ret;
@@ -434,43 +436,43 @@ remove_node (struct GNUNET_CONTAINER_HeapNode *node)
434 436
435 /* unlink 'node' itself and insert children in its place */ 437 /* unlink 'node' itself and insert children in its place */
436 if (node->parent == NULL) 438 if (node->parent == NULL)
437 {
438 if (node->left_child != NULL)
439 { 439 {
440 heap->root = node->left_child; 440 if (node->left_child != NULL)
441 node->left_child->parent = NULL; 441 {
442 if (node->right_child != NULL) 442 heap->root = node->left_child;
443 { 443 node->left_child->parent = NULL;
444 node->right_child->parent = NULL; 444 if (node->right_child != NULL)
445 insert_node (heap, heap->root, node->right_child); 445 {
446 } 446 node->right_child->parent = NULL;
447 insert_node (heap, heap->root, node->right_child);
448 }
449 }
450 else
451 {
452 heap->root = node->right_child;
453 if (node->right_child != NULL)
454 node->right_child->parent = NULL;
455 }
447 } 456 }
448 else
449 {
450 heap->root = node->right_child;
451 if (node->right_child != NULL)
452 node->right_child->parent = NULL;
453 }
454 }
455 else 457 else
456 {
457 if (node->parent->left_child == node)
458 node->parent->left_child = NULL;
459 else
460 node->parent->right_child = NULL;
461 if (node->left_child != NULL)
462 { 458 {
463 node->left_child->parent = NULL; 459 if (node->parent->left_child == node)
464 node->parent->tree_size -= (1 + node->left_child->tree_size); 460 node->parent->left_child = NULL;
465 insert_node (heap, node->parent, node->left_child); 461 else
466 } 462 node->parent->right_child = NULL;
467 if (node->right_child != NULL) 463 if (node->left_child != NULL)
468 { 464 {
469 node->right_child->parent = NULL; 465 node->left_child->parent = NULL;
470 node->parent->tree_size -= (1 + node->right_child->tree_size); 466 node->parent->tree_size -= (1 + node->left_child->tree_size);
471 insert_node (heap, node->parent, node->right_child); 467 insert_node (heap, node->parent, node->left_child);
468 }
469 if (node->right_child != NULL)
470 {
471 node->right_child->parent = NULL;
472 node->parent->tree_size -= (1 + node->right_child->tree_size);
473 insert_node (heap, node->parent, node->right_child);
474 }
472 } 475 }
473 }
474 node->parent = NULL; 476 node->parent = NULL;
475 node->left_child = NULL; 477 node->left_child = NULL;
476 node->right_child = NULL; 478 node->right_child = NULL;
@@ -504,7 +506,7 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
504#if DEBUG 506#if DEBUG
505 CHECK (heap->root); 507 CHECK (heap->root);
506 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 508 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
507 (heap->size == heap->root->tree_size + 1)); 509 (heap->size == heap->root->tree_size + 1));
508#endif 510#endif
509 return ret; 511 return ret;
510} 512}
@@ -519,19 +521,19 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
519 */ 521 */
520void 522void
521GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap, 523GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
522 struct GNUNET_CONTAINER_HeapNode *node, 524 struct GNUNET_CONTAINER_HeapNode *node,
523 GNUNET_CONTAINER_HeapCostType new_cost) 525 GNUNET_CONTAINER_HeapCostType new_cost)
524{ 526{
525#if DEBUG 527#if DEBUG
526 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 528 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
527 (heap->size == heap->root->tree_size + 1)); 529 (heap->size == heap->root->tree_size + 1));
528 CHECK (heap->root); 530 CHECK (heap->root);
529#endif 531#endif
530 remove_node (node); 532 remove_node (node);
531#if DEBUG 533#if DEBUG
532 CHECK (heap->root); 534 CHECK (heap->root);
533 GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) || 535 GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) ||
534 (heap->size == heap->root->tree_size + 2)); 536 (heap->size == heap->root->tree_size + 2));
535#endif 537#endif
536 node->cost = new_cost; 538 node->cost = new_cost;
537 if (heap->root == NULL) 539 if (heap->root == NULL)
@@ -541,7 +543,7 @@ GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
541#if DEBUG 543#if DEBUG
542 CHECK (heap->root); 544 CHECK (heap->root);
543 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 545 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
544 (heap->size == heap->root->tree_size + 1)); 546 (heap->size == heap->root->tree_size + 1));
545#endif 547#endif
546} 548}
547 549
diff --git a/src/util/container_meta_data.c b/src/util/container_meta_data.c
index 16173cca9..1ea77f63a 100644
--- a/src/util/container_meta_data.c
+++ b/src/util/container_meta_data.c
@@ -32,6 +32,8 @@
32#include <extractor.h> 32#include <extractor.h>
33#include <zlib.h> 33#include <zlib.h>
34 34
35#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36
35/** 37/**
36 * Meta data item. 38 * Meta data item.
37 */ 39 */
@@ -160,10 +162,10 @@ GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md)
160 if (md == NULL) 162 if (md == NULL)
161 return; 163 return;
162 while (NULL != (item = md->items)) 164 while (NULL != (item = md->items))
163 { 165 {
164 md->items = item->next; 166 md->items = item->next;
165 meta_item_free (item); 167 meta_item_free (item);
166 } 168 }
167 GNUNET_free_non_null (md->sbuf); 169 GNUNET_free_non_null (md->sbuf);
168 GNUNET_free (md); 170 GNUNET_free (md);
169} 171}
@@ -182,10 +184,10 @@ GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md)
182 if (md == NULL) 184 if (md == NULL)
183 return; 185 return;
184 while (NULL != (item = md->items)) 186 while (NULL != (item = md->items))
185 { 187 {
186 md->items = item->next; 188 md->items = item->next;
187 meta_item_free (item); 189 meta_item_free (item);
188 } 190 }
189 GNUNET_free_non_null (md->sbuf); 191 GNUNET_free_non_null (md->sbuf);
190 memset (md, 0, sizeof (struct GNUNET_CONTAINER_MetaData)); 192 memset (md, 0, sizeof (struct GNUNET_CONTAINER_MetaData));
191} 193}
@@ -204,9 +206,9 @@ GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md)
204 */ 206 */
205int 207int
206GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData 208GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
207 *md1, 209 *md1,
208 const struct GNUNET_CONTAINER_MetaData 210 const struct GNUNET_CONTAINER_MetaData
209 *md2) 211 *md2)
210{ 212{
211 struct MetaItem *i; 213 struct MetaItem *i;
212 struct MetaItem *j; 214 struct MetaItem *j;
@@ -219,24 +221,24 @@ GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
219 221
220 i = md1->items; 222 i = md1->items;
221 while (NULL != i) 223 while (NULL != i)
222 {
223 found = GNUNET_NO;
224 j = md2->items;
225 while (NULL != j)
226 { 224 {
227 if ((i->type == j->type) && (i->format == j->format) && 225 found = GNUNET_NO;
228 (i->data_size == j->data_size) && 226 j = md2->items;
229 (0 == memcmp (i->data, j->data, i->data_size))) 227 while (NULL != j)
230 { 228 {
231 found = GNUNET_YES; 229 if ((i->type == j->type) && (i->format == j->format) &&
232 break; 230 (i->data_size == j->data_size) &&
233 } 231 (0 == memcmp (i->data, j->data, i->data_size)))
234 j = j->next; 232 {
233 found = GNUNET_YES;
234 break;
235 }
236 j = j->next;
237 }
238 if (found == GNUNET_NO)
239 return GNUNET_NO;
240 i = i->next;
235 } 241 }
236 if (found == GNUNET_NO)
237 return GNUNET_NO;
238 i = i->next;
239 }
240 return GNUNET_YES; 242 return GNUNET_YES;
241} 243}
242 244
@@ -261,11 +263,11 @@ GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
261 */ 263 */
262int 264int
263GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md, 265GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
264 const char *plugin_name, 266 const char *plugin_name,
265 enum EXTRACTOR_MetaType type, 267 enum EXTRACTOR_MetaType type,
266 enum EXTRACTOR_MetaFormat format, 268 enum EXTRACTOR_MetaFormat format,
267 const char *data_mime_type, const char *data, 269 const char *data_mime_type,
268 size_t data_len) 270 const char *data, size_t data_len)
269{ 271{
270 struct MetaItem *prev; 272 struct MetaItem *prev;
271 struct MetaItem *pos; 273 struct MetaItem *pos;
@@ -275,28 +277,28 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
275 prev = NULL; 277 prev = NULL;
276 pos = md->items; 278 pos = md->items;
277 while (NULL != pos) 279 while (NULL != pos)
278 {
279 if (pos->data_size < data_len)
280 break;
281 if ((pos->type == type) && (pos->data_size == data_len) &&
282 (0 == memcmp (pos->data, data, data_len)))
283 { 280 {
284 if ((pos->mime_type == NULL) && (data_mime_type != NULL)) 281 if (pos->data_size < data_len)
285 { 282 break;
286 pos->mime_type = GNUNET_strdup (data_mime_type); 283 if ((pos->type == type) && (pos->data_size == data_len) &&
287 invalidate_sbuf (md); 284 (0 == memcmp (pos->data, data, data_len)))
288 } 285 {
289 if ((pos->format == EXTRACTOR_METAFORMAT_C_STRING) && 286 if ((pos->mime_type == NULL) && (data_mime_type != NULL))
290 (format == EXTRACTOR_METAFORMAT_UTF8)) 287 {
291 { 288 pos->mime_type = GNUNET_strdup (data_mime_type);
292 pos->format = EXTRACTOR_METAFORMAT_UTF8; 289 invalidate_sbuf (md);
293 invalidate_sbuf (md); 290 }
294 } 291 if ((pos->format == EXTRACTOR_METAFORMAT_C_STRING) &&
295 return GNUNET_SYSERR; 292 (format == EXTRACTOR_METAFORMAT_UTF8))
293 {
294 pos->format = EXTRACTOR_METAFORMAT_UTF8;
295 invalidate_sbuf (md);
296 }
297 return GNUNET_SYSERR;
298 }
299 prev = pos;
300 pos = pos->next;
296 } 301 }
297 prev = pos;
298 pos = pos->next;
299 }
300 md->item_count++; 302 md->item_count++;
301 i = GNUNET_malloc (sizeof (struct MetaItem)); 303 i = GNUNET_malloc (sizeof (struct MetaItem));
302 i->type = type; 304 i->type = type;
@@ -308,23 +310,23 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
308 else 310 else
309 prev->next = i; 311 prev->next = i;
310 i->mime_type = 312 i->mime_type =
311 (data_mime_type == NULL) ? NULL : GNUNET_strdup (data_mime_type); 313 (data_mime_type == NULL) ? NULL : GNUNET_strdup (data_mime_type);
312 i->plugin_name = (plugin_name == NULL) ? NULL : GNUNET_strdup (plugin_name); 314 i->plugin_name = (plugin_name == NULL) ? NULL : GNUNET_strdup (plugin_name);
313 i->data = GNUNET_malloc (data_len); 315 i->data = GNUNET_malloc (data_len);
314 memcpy (i->data, data, data_len); 316 memcpy (i->data, data, data_len);
315 /* change OS native dir separators to unix '/' and others to '_' */ 317 /* change OS native dir separators to unix '/' and others to '_' */
316 if (type == EXTRACTOR_METATYPE_FILENAME) 318 if (type == EXTRACTOR_METATYPE_FILENAME)
317 {
318 p = i->data;
319 while ((*p != '\0') && (p < i->data + data_len))
320 { 319 {
321 if (*p == DIR_SEPARATOR) 320 p = i->data;
322 *p = '/'; 321 while ((*p != '\0') && (p < i->data + data_len))
323 else if (*p == '\\') 322 {
324 *p = '_'; 323 if (*p == DIR_SEPARATOR)
325 p++; 324 *p = '/';
325 else if (*p == '\\')
326 *p = '_';
327 p++;
328 }
326 } 329 }
327 }
328 invalidate_sbuf (md); 330 invalidate_sbuf (md);
329 return GNUNET_OK; 331 return GNUNET_OK;
330} 332}
@@ -347,14 +349,14 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
347 * @return 0 (to continue) 349 * @return 0 (to continue)
348 */ 350 */
349static int 351static int
350merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, 352merge_helper (void *cls, const char *plugin_name,
351 enum EXTRACTOR_MetaFormat format, const char *data_mime_type, 353 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
352 const char *data, size_t data_len) 354 const char *data_mime_type, const char *data, size_t data_len)
353{ 355{
354 struct GNUNET_CONTAINER_MetaData *md = cls; 356 struct GNUNET_CONTAINER_MetaData *md = cls;
355 357
356 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format, 358 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
357 data_mime_type, data, data_len); 359 data_mime_type, data, data_len);
358 return 0; 360 return 0;
359} 361}
360 362
@@ -368,7 +370,7 @@ merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
368 */ 370 */
369void 371void
370GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md, 372GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
371 const struct GNUNET_CONTAINER_MetaData *in) 373 const struct GNUNET_CONTAINER_MetaData *in)
372{ 374{
373 GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md); 375 GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md);
374} 376}
@@ -386,8 +388,8 @@ GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
386 */ 388 */
387int 389int
388GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md, 390GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
389 enum EXTRACTOR_MetaType type, 391 enum EXTRACTOR_MetaType type,
390 const char *data, size_t data_len) 392 const char *data, size_t data_len)
391{ 393{
392 struct MetaItem *pos; 394 struct MetaItem *pos;
393 struct MetaItem *prev; 395 struct MetaItem *prev;
@@ -395,24 +397,24 @@ GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
395 prev = NULL; 397 prev = NULL;
396 pos = md->items; 398 pos = md->items;
397 while (NULL != pos) 399 while (NULL != pos)
398 {
399 if ((pos->type == type) &&
400 ((data == NULL) ||
401 ((pos->data_size == data_len) &&
402 (0 == memcmp (pos->data, data, data_len)))))
403 { 400 {
404 if (prev == NULL) 401 if ((pos->type == type) &&
405 md->items = pos->next; 402 ((data == NULL) ||
406 else 403 ((pos->data_size == data_len) &&
407 prev->next = pos->next; 404 (0 == memcmp (pos->data, data, data_len)))))
408 meta_item_free (pos); 405 {
409 md->item_count--; 406 if (prev == NULL)
410 invalidate_sbuf (md); 407 md->items = pos->next;
411 return GNUNET_OK; 408 else
409 prev->next = pos->next;
410 meta_item_free (pos);
411 md->item_count--;
412 invalidate_sbuf (md);
413 return GNUNET_OK;
414 }
415 prev = pos;
416 pos = pos->next;
412 } 417 }
413 prev = pos;
414 pos = pos->next;
415 }
416 return GNUNET_SYSERR; 418 return GNUNET_SYSERR;
417} 419}
418 420
@@ -425,19 +427,20 @@ GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
425 */ 427 */
426void 428void
427GNUNET_CONTAINER_meta_data_add_publication_date (struct 429GNUNET_CONTAINER_meta_data_add_publication_date (struct
428 GNUNET_CONTAINER_MetaData *md) 430 GNUNET_CONTAINER_MetaData
431 *md)
429{ 432{
430 char *dat; 433 char *dat;
431 struct GNUNET_TIME_Absolute t; 434 struct GNUNET_TIME_Absolute t;
432 435
433 t = GNUNET_TIME_absolute_get (); 436 t = GNUNET_TIME_absolute_get ();
434 GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_METATYPE_PUBLICATION_DATE, 437 GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_METATYPE_PUBLICATION_DATE,
435 NULL, 0); 438 NULL, 0);
436 dat = GNUNET_STRINGS_absolute_time_to_string (t); 439 dat = GNUNET_STRINGS_absolute_time_to_string (t);
437 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>", 440 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
438 EXTRACTOR_METATYPE_PUBLICATION_DATE, 441 EXTRACTOR_METATYPE_PUBLICATION_DATE,
439 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 442 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
440 dat, strlen (dat) + 1); 443 dat, strlen (dat) + 1);
441 GNUNET_free (dat); 444 GNUNET_free (dat);
442} 445}
443 446
@@ -451,9 +454,9 @@ GNUNET_CONTAINER_meta_data_add_publication_date (struct
451 * @return number of entries 454 * @return number of entries
452 */ 455 */
453int 456int
454GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md, 457GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData
455 EXTRACTOR_MetaDataProcessor iter, 458 *md, EXTRACTOR_MetaDataProcessor iter,
456 void *iter_cls) 459 void *iter_cls)
457{ 460{
458 struct MetaItem *pos; 461 struct MetaItem *pos;
459 462
@@ -463,13 +466,13 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
463 return md->item_count; 466 return md->item_count;
464 pos = md->items; 467 pos = md->items;
465 while (NULL != pos) 468 while (NULL != pos)
466 { 469 {
467 if (0 != 470 if (0 !=
468 iter (iter_cls, pos->plugin_name, pos->type, pos->format, 471 iter (iter_cls, pos->plugin_name, pos->type, pos->format,
469 pos->mime_type, pos->data, pos->data_size)) 472 pos->mime_type, pos->data, pos->data_size))
470 return md->item_count; 473 return md->item_count;
471 pos = pos->next; 474 pos = pos->next;
472 } 475 }
473 return md->item_count; 476 return md->item_count;
474} 477}
475 478
@@ -486,7 +489,7 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
486 */ 489 */
487char * 490char *
488GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData 491GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
489 *md, enum EXTRACTOR_MetaType type) 492 *md, enum EXTRACTOR_MetaType type)
490{ 493{
491 struct MetaItem *pos; 494 struct MetaItem *pos;
492 495
@@ -494,13 +497,13 @@ GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
494 return NULL; 497 return NULL;
495 pos = md->items; 498 pos = md->items;
496 while (NULL != pos) 499 while (NULL != pos)
497 { 500 {
498 if ((type == pos->type) && 501 if ((type == pos->type) &&
499 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) || 502 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) ||
500 (pos->format == EXTRACTOR_METAFORMAT_C_STRING))) 503 (pos->format == EXTRACTOR_METAFORMAT_C_STRING)))
501 return GNUNET_strdup (pos->data); 504 return GNUNET_strdup (pos->data);
502 pos = pos->next; 505 pos = pos->next;
503 } 506 }
504 return NULL; 507 return NULL;
505} 508}
506 509
@@ -518,8 +521,8 @@ GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
518 */ 521 */
519char * 522char *
520GNUNET_CONTAINER_meta_data_get_first_by_types (const struct 523GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
521 GNUNET_CONTAINER_MetaData *md, 524 GNUNET_CONTAINER_MetaData *md,
522 ...) 525 ...)
523{ 526{
524 char *ret; 527 char *ret;
525 va_list args; 528 va_list args;
@@ -530,15 +533,15 @@ GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
530 ret = NULL; 533 ret = NULL;
531 va_start (args, md); 534 va_start (args, md);
532 while (1) 535 while (1)
533 { 536 {
534 type = va_arg (args, enum EXTRACTOR_MetaType); 537 type = va_arg (args, enum EXTRACTOR_MetaType);
535 538
536 if (type == -1) 539 if (type == -1)
537 break; 540 break;
538 ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type); 541 ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type);
539 if (ret != NULL) 542 if (ret != NULL)
540 break; 543 break;
541 } 544 }
542 va_end (args); 545 va_end (args);
543 return ret; 546 return ret;
544} 547}
@@ -553,8 +556,9 @@ GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
553 * @return number of bytes in thumbnail, 0 if not available 556 * @return number of bytes in thumbnail, 0 if not available
554 */ 557 */
555size_t 558size_t
556GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData 559GNUNET_CONTAINER_meta_data_get_thumbnail (const struct
557 * md, unsigned char **thumb) 560 GNUNET_CONTAINER_MetaData * md,
561 unsigned char **thumb)
558{ 562{
559 struct MetaItem *pos; 563 struct MetaItem *pos;
560 struct MetaItem *match; 564 struct MetaItem *match;
@@ -564,19 +568,19 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
564 match = NULL; 568 match = NULL;
565 pos = md->items; 569 pos = md->items;
566 while (NULL != pos) 570 while (NULL != pos)
567 {
568 if ((NULL != pos->mime_type) &&
569 (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) &&
570 (pos->format == EXTRACTOR_METAFORMAT_BINARY))
571 { 571 {
572 if (match == NULL) 572 if ((NULL != pos->mime_type) &&
573 match = pos; 573 (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) &&
574 else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) && 574 (pos->format == EXTRACTOR_METAFORMAT_BINARY))
575 (pos->type == EXTRACTOR_METATYPE_THUMBNAIL)) 575 {
576 match = pos; 576 if (match == NULL)
577 match = pos;
578 else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) &&
579 (pos->type == EXTRACTOR_METATYPE_THUMBNAIL))
580 match = pos;
581 }
582 pos = pos->next;
577 } 583 }
578 pos = pos->next;
579 }
580 if ((match == NULL) || (match->data_size == 0)) 584 if ((match == NULL) || (match->data_size == 0))
581 return 0; 585 return 0;
582 *thumb = GNUNET_malloc (match->data_size); 586 *thumb = GNUNET_malloc (match->data_size);
@@ -593,7 +597,7 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
593 */ 597 */
594struct GNUNET_CONTAINER_MetaData * 598struct GNUNET_CONTAINER_MetaData *
595GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData 599GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
596 *md) 600 *md)
597{ 601{
598 struct GNUNET_CONTAINER_MetaData *ret; 602 struct GNUNET_CONTAINER_MetaData *ret;
599 struct MetaItem *pos; 603 struct MetaItem *pos;
@@ -603,12 +607,12 @@ GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
603 ret = GNUNET_CONTAINER_meta_data_create (); 607 ret = GNUNET_CONTAINER_meta_data_create ();
604 pos = md->items; 608 pos = md->items;
605 while (NULL != pos) 609 while (NULL != pos)
606 { 610 {
607 GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type, 611 GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type,
608 pos->format, pos->mime_type, pos->data, 612 pos->format, pos->mime_type,
609 pos->data_size); 613 pos->data, pos->data_size);
610 pos = pos->next; 614 pos = pos->next;
611 } 615 }
612 return ret; 616 return ret;
613} 617}
614 618
@@ -629,7 +633,7 @@ GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
629 */ 633 */
630static int 634static int
631try_compression (const char *data, size_t oldSize, char **result, 635try_compression (const char *data, size_t oldSize, char **result,
632 size_t * newSize) 636 size_t * newSize)
633{ 637{
634 char *tmp; 638 char *tmp;
635 uLongf dlen; 639 uLongf dlen;
@@ -644,14 +648,14 @@ try_compression (const char *data, size_t oldSize, char **result,
644 tmp = GNUNET_malloc (dlen); 648 tmp = GNUNET_malloc (dlen);
645 if (Z_OK == 649 if (Z_OK ==
646 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9)) 650 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9))
647 {
648 if (dlen < oldSize)
649 { 651 {
650 *result = tmp; 652 if (dlen < oldSize)
651 *newSize = dlen; 653 {
652 return GNUNET_YES; 654 *result = tmp;
655 *newSize = dlen;
656 return GNUNET_YES;
657 }
653 } 658 }
654 }
655 GNUNET_free (tmp); 659 GNUNET_free (tmp);
656 return GNUNET_NO; 660 return GNUNET_NO;
657} 661}
@@ -752,10 +756,10 @@ struct MetaDataEntry
752 */ 756 */
753ssize_t 757ssize_t
754GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData 758GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
755 *md, char **target, size_t max, 759 *md, char **target, size_t max,
756 enum 760 enum
757 GNUNET_CONTAINER_MetaDataSerializationOptions 761 GNUNET_CONTAINER_MetaDataSerializationOptions
758 opt) 762 opt)
759{ 763{
760 struct GNUNET_CONTAINER_MetaData *vmd; 764 struct GNUNET_CONTAINER_MetaData *vmd;
761 struct MetaItem *pos; 765 struct MetaItem *pos;
@@ -777,79 +781,79 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
777 int comp; 781 int comp;
778 782
779 if (max < sizeof (struct MetaDataHeader)) 783 if (max < sizeof (struct MetaDataHeader))
780 return GNUNET_SYSERR; /* far too small */ 784 return GNUNET_SYSERR; /* far too small */
781 if (md == NULL) 785 if (md == NULL)
782 return 0; 786 return 0;
783 787
784 if (md->sbuf != NULL) 788 if (md->sbuf != NULL)
785 {
786 /* try to use serialization cache */
787 if (md->sbuf_size <= max)
788 { 789 {
789 if (NULL == *target) 790 /* try to use serialization cache */
790 *target = GNUNET_malloc (md->sbuf_size); 791 if (md->sbuf_size <= max)
791 memcpy (*target, md->sbuf, md->sbuf_size); 792 {
792 return md->sbuf_size; 793 if (NULL == *target)
794 *target = GNUNET_malloc (md->sbuf_size);
795 memcpy (*target, md->sbuf, md->sbuf_size);
796 return md->sbuf_size;
797 }
798 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
799 return GNUNET_SYSERR; /* can say that this will fail */
800 /* need to compute a partial serialization, sbuf useless ... */
793 } 801 }
794 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
795 return GNUNET_SYSERR; /* can say that this will fail */
796 /* need to compute a partial serialization, sbuf useless ... */
797 }
798 dst = NULL; 802 dst = NULL;
799 msize = 0; 803 msize = 0;
800 pos = md->items; 804 pos = md->items;
801 while (NULL != pos) 805 while (NULL != pos)
802 { 806 {
803 msize += sizeof (struct MetaDataEntry); 807 msize += sizeof (struct MetaDataEntry);
804 msize += pos->data_size; 808 msize += pos->data_size;
805 if (pos->plugin_name != NULL) 809 if (pos->plugin_name != NULL)
806 msize += strlen (pos->plugin_name) + 1; 810 msize += strlen (pos->plugin_name) + 1;
807 if (pos->mime_type != NULL) 811 if (pos->mime_type != NULL)
808 msize += strlen (pos->mime_type) + 1; 812 msize += strlen (pos->mime_type) + 1;
809 pos = pos->next; 813 pos = pos->next;
810 } 814 }
811 size = (size_t) msize; 815 size = (size_t) msize;
812 if (size != msize) 816 if (size != msize)
813 { 817 {
814 GNUNET_break (0); /* integer overflow */ 818 GNUNET_break (0); /* integer overflow */
815 return GNUNET_SYSERR; 819 return GNUNET_SYSERR;
816 } 820 }
817 if (size >= GNUNET_MAX_MALLOC_CHECKED) 821 if (size >= GNUNET_MAX_MALLOC_CHECKED)
818 { 822 {
819 /* too large to be processed */ 823 /* too large to be processed */
820 return GNUNET_SYSERR; 824 return GNUNET_SYSERR;
821 } 825 }
822 ent = GNUNET_malloc (size); 826 ent = GNUNET_malloc (size);
823 mdata = (char *) &ent[md->item_count]; 827 mdata = (char *) &ent[md->item_count];
824 off = size - (md->item_count * sizeof (struct MetaDataEntry)); 828 off = size - (md->item_count * sizeof (struct MetaDataEntry));
825 i = 0; 829 i = 0;
826 pos = md->items; 830 pos = md->items;
827 while (NULL != pos) 831 while (NULL != pos)
828 { 832 {
829 ent[i].type = htonl ((uint32_t) pos->type); 833 ent[i].type = htonl ((uint32_t) pos->type);
830 ent[i].format = htonl ((uint32_t) pos->format); 834 ent[i].format = htonl ((uint32_t) pos->format);
831 ent[i].data_size = htonl ((uint32_t) pos->data_size); 835 ent[i].data_size = htonl ((uint32_t) pos->data_size);
832 if (pos->plugin_name == NULL) 836 if (pos->plugin_name == NULL)
833 plen = 0; 837 plen = 0;
834 else 838 else
835 plen = strlen (pos->plugin_name) + 1; 839 plen = strlen (pos->plugin_name) + 1;
836 ent[i].plugin_name_len = htonl ((uint32_t) plen); 840 ent[i].plugin_name_len = htonl ((uint32_t) plen);
837 if (pos->mime_type == NULL) 841 if (pos->mime_type == NULL)
838 mlen = 0; 842 mlen = 0;
839 else 843 else
840 mlen = strlen (pos->mime_type) + 1; 844 mlen = strlen (pos->mime_type) + 1;
841 ent[i].mime_type_len = htonl ((uint32_t) mlen); 845 ent[i].mime_type_len = htonl ((uint32_t) mlen);
842 off -= pos->data_size; 846 off -= pos->data_size;
843 memcpy (&mdata[off], pos->data, pos->data_size); 847 memcpy (&mdata[off], pos->data, pos->data_size);
844 off -= plen; 848 off -= plen;
845 if (pos->plugin_name != NULL) 849 if (pos->plugin_name != NULL)
846 memcpy (&mdata[off], pos->plugin_name, plen); 850 memcpy (&mdata[off], pos->plugin_name, plen);
847 off -= mlen; 851 off -= mlen;
848 if (pos->mime_type != NULL) 852 if (pos->mime_type != NULL)
849 memcpy (&mdata[off], pos->mime_type, mlen); 853 memcpy (&mdata[off], pos->mime_type, mlen);
850 i++; 854 i++;
851 pos = pos->next; 855 pos = pos->next;
852 } 856 }
853 GNUNET_assert (off == 0); 857 GNUNET_assert (off == 0);
854 858
855 clen = 0; 859 clen = 0;
@@ -858,95 +862,95 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
858 i = 0; 862 i = 0;
859 pos = md->items; 863 pos = md->items;
860 while (pos != NULL) 864 while (pos != NULL)
861 {
862 comp = GNUNET_NO;
863 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
864 comp = try_compression ((const char *) &ent[i], left, &cdata, &clen);
865
866 if ((md->sbuf == NULL) && (i == 0))
867 {
868 /* fill 'sbuf'; this "modifies" md, but since this is only
869 * an internal cache we will cast away the 'const' instead
870 * of making the API look strange. */
871 vmd = (struct GNUNET_CONTAINER_MetaData *) md;
872 hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
873 hdr->size = htonl (left);
874 hdr->entries = htonl (md->item_count);
875 if (GNUNET_YES == comp)
876 {
877 GNUNET_assert (clen < left);
878 hdr->version = htonl (2 | HEADER_COMPRESSED);
879 memcpy (&hdr[1], cdata, clen);
880 vmd->sbuf_size = clen + sizeof (struct MetaDataHeader);
881 }
882 else
883 {
884 hdr->version = htonl (2);
885 memcpy (&hdr[1], &ent[0], left);
886 vmd->sbuf_size = left + sizeof (struct MetaDataHeader);
887 }
888 vmd->sbuf = (char *) hdr;
889 }
890
891 if (((left + sizeof (struct MetaDataHeader)) <= max) ||
892 ((comp == GNUNET_YES) && (clen <= max)))
893 {
894 /* success, this now fits! */
895 if (GNUNET_YES == comp)
896 {
897 if (dst == NULL)
898 dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader));
899 hdr = (struct MetaDataHeader *) dst;
900 hdr->version = htonl (2 | HEADER_COMPRESSED);
901 hdr->size = htonl (left);
902 hdr->entries = htonl (md->item_count - i);
903 memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen);
904 GNUNET_free (cdata);
905 GNUNET_free (ent);
906 rlen = clen + sizeof (struct MetaDataHeader);
907 }
908 else
909 {
910 if (dst == NULL)
911 dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
912 hdr = (struct MetaDataHeader *) dst;
913 hdr->version = htonl (2);
914 hdr->entries = htonl (md->item_count - i);
915 hdr->size = htonl (left);
916 memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left);
917 GNUNET_free (ent);
918 rlen = left + sizeof (struct MetaDataHeader);
919 }
920 if (NULL != *target)
921 {
922 memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
923 GNUNET_free (dst);
924 }
925 else
926 {
927 *target = dst;
928 }
929 return rlen;
930 }
931
932 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
933 { 865 {
934 /* does not fit! */ 866 comp = GNUNET_NO;
935 GNUNET_free (ent); 867 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
936 return GNUNET_SYSERR; 868 comp = try_compression ((const char *) &ent[i], left, &cdata, &clen);
869
870 if ((md->sbuf == NULL) && (i == 0))
871 {
872 /* fill 'sbuf'; this "modifies" md, but since this is only
873 * an internal cache we will cast away the 'const' instead
874 * of making the API look strange. */
875 vmd = (struct GNUNET_CONTAINER_MetaData *) md;
876 hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
877 hdr->size = htonl (left);
878 hdr->entries = htonl (md->item_count);
879 if (GNUNET_YES == comp)
880 {
881 GNUNET_assert (clen < left);
882 hdr->version = htonl (2 | HEADER_COMPRESSED);
883 memcpy (&hdr[1], cdata, clen);
884 vmd->sbuf_size = clen + sizeof (struct MetaDataHeader);
885 }
886 else
887 {
888 hdr->version = htonl (2);
889 memcpy (&hdr[1], &ent[0], left);
890 vmd->sbuf_size = left + sizeof (struct MetaDataHeader);
891 }
892 vmd->sbuf = (char *) hdr;
893 }
894
895 if (((left + sizeof (struct MetaDataHeader)) <= max) ||
896 ((comp == GNUNET_YES) && (clen <= max)))
897 {
898 /* success, this now fits! */
899 if (GNUNET_YES == comp)
900 {
901 if (dst == NULL)
902 dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader));
903 hdr = (struct MetaDataHeader *) dst;
904 hdr->version = htonl (2 | HEADER_COMPRESSED);
905 hdr->size = htonl (left);
906 hdr->entries = htonl (md->item_count - i);
907 memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen);
908 GNUNET_free (cdata);
909 GNUNET_free (ent);
910 rlen = clen + sizeof (struct MetaDataHeader);
911 }
912 else
913 {
914 if (dst == NULL)
915 dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
916 hdr = (struct MetaDataHeader *) dst;
917 hdr->version = htonl (2);
918 hdr->entries = htonl (md->item_count - i);
919 hdr->size = htonl (left);
920 memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left);
921 GNUNET_free (ent);
922 rlen = left + sizeof (struct MetaDataHeader);
923 }
924 if (NULL != *target)
925 {
926 memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
927 GNUNET_free (dst);
928 }
929 else
930 {
931 *target = dst;
932 }
933 return rlen;
934 }
935
936 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
937 {
938 /* does not fit! */
939 GNUNET_free (ent);
940 return GNUNET_SYSERR;
941 }
942
943 /* next iteration: ignore the corresponding meta data at the
944 * end and try again without it */
945 left -= sizeof (struct MetaDataEntry);
946 left -= pos->data_size;
947 if (pos->plugin_name != NULL)
948 left -= strlen (pos->plugin_name) + 1;
949 if (pos->mime_type != NULL)
950 left -= strlen (pos->mime_type) + 1;
951 pos = pos->next;
952 i++;
937 } 953 }
938
939 /* next iteration: ignore the corresponding meta data at the
940 * end and try again without it */
941 left -= sizeof (struct MetaDataEntry);
942 left -= pos->data_size;
943 if (pos->plugin_name != NULL)
944 left -= strlen (pos->plugin_name) + 1;
945 if (pos->mime_type != NULL)
946 left -= strlen (pos->mime_type) + 1;
947 pos = pos->next;
948 i++;
949 }
950 GNUNET_free (ent); 954 GNUNET_free (ent);
951 955
952 /* nothing fit, only write header! */ 956 /* nothing fit, only write header! */
@@ -968,7 +972,7 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
968 */ 972 */
969ssize_t 973ssize_t
970GNUNET_CONTAINER_meta_data_get_serialized_size (const struct 974GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
971 GNUNET_CONTAINER_MetaData *md) 975 GNUNET_CONTAINER_MetaData *md)
972{ 976{
973 ssize_t ret; 977 ssize_t ret;
974 char *ptr; 978 char *ptr;
@@ -977,8 +981,8 @@ GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
977 return md->sbuf_size; 981 return md->sbuf_size;
978 ptr = NULL; 982 ptr = NULL;
979 ret = 983 ret =
980 GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED, 984 GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED,
981 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); 985 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
982 if (ret != -1) 986 if (ret != -1)
983 GNUNET_free (ptr); 987 GNUNET_free (ptr);
984 return ret; 988 return ret;
@@ -1005,14 +1009,14 @@ decompress (const char *input, size_t inputSize, size_t outputSize)
1005 output = GNUNET_malloc (olen); 1009 output = GNUNET_malloc (olen);
1006 if (Z_OK == 1010 if (Z_OK ==
1007 uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize)) 1011 uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize))
1008 { 1012 {
1009 return output; 1013 return output;
1010 } 1014 }
1011 else 1015 else
1012 { 1016 {
1013 GNUNET_free (output); 1017 GNUNET_free (output);
1014 return NULL; 1018 return NULL;
1015 } 1019 }
1016} 1020}
1017 1021
1018 1022
@@ -1054,121 +1058,121 @@ GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size)
1054 compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0; 1058 compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0;
1055 1059
1056 if (version == 1) 1060 if (version == 1)
1057 return NULL; /* null pointer */ 1061 return NULL; /* null pointer */
1058 if (version != 2) 1062 if (version != 2)
1059 { 1063 {
1060 GNUNET_break_op (0); /* unsupported version */ 1064 GNUNET_break_op (0); /* unsupported version */
1061 return NULL; 1065 return NULL;
1062 } 1066 }
1063 1067
1064 ic = ntohl (hdr.entries); 1068 ic = ntohl (hdr.entries);
1065 dataSize = ntohl (hdr.size); 1069 dataSize = ntohl (hdr.size);
1066 if ((sizeof (struct MetaDataEntry) * ic) > dataSize) 1070 if ((sizeof (struct MetaDataEntry) * ic) > dataSize)
1067 {
1068 GNUNET_break_op (0);
1069 return NULL;
1070 }
1071
1072 if (compressed)
1073 {
1074 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED)
1075 { 1071 {
1076 /* make sure we don't blow our memory limit because of a mal-formed
1077 * message... */
1078 GNUNET_break_op (0); 1072 GNUNET_break_op (0);
1079 return NULL; 1073 return NULL;
1080 } 1074 }
1081 data = 1075
1082 decompress ((const char *) &input[sizeof (struct MetaDataHeader)], 1076 if (compressed)
1083 size - sizeof (struct MetaDataHeader), dataSize);
1084 if (data == NULL)
1085 { 1077 {
1086 GNUNET_break_op (0); 1078 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED)
1087 return NULL; 1079 {
1080 /* make sure we don't blow our memory limit because of a mal-formed
1081 * message... */
1082 GNUNET_break_op (0);
1083 return NULL;
1084 }
1085 data =
1086 decompress ((const char *) &input[sizeof (struct MetaDataHeader)],
1087 size - sizeof (struct MetaDataHeader), dataSize);
1088 if (data == NULL)
1089 {
1090 GNUNET_break_op (0);
1091 return NULL;
1092 }
1093 cdata = data;
1088 } 1094 }
1089 cdata = data;
1090 }
1091 else 1095 else
1092 {
1093 data = NULL;
1094 cdata = (const char *) &input[sizeof (struct MetaDataHeader)];
1095 if (dataSize != size - sizeof (struct MetaDataHeader))
1096 { 1096 {
1097 GNUNET_break_op (0); 1097 data = NULL;
1098 return NULL; 1098 cdata = (const char *) &input[sizeof (struct MetaDataHeader)];
1099 if (dataSize != size - sizeof (struct MetaDataHeader))
1100 {
1101 GNUNET_break_op (0);
1102 return NULL;
1103 }
1099 } 1104 }
1100 }
1101 1105
1102 md = GNUNET_CONTAINER_meta_data_create (); 1106 md = GNUNET_CONTAINER_meta_data_create ();
1103 left = dataSize - ic * sizeof (struct MetaDataEntry); 1107 left = dataSize - ic * sizeof (struct MetaDataEntry);
1104 mdata = &cdata[ic * sizeof (struct MetaDataEntry)]; 1108 mdata = &cdata[ic * sizeof (struct MetaDataEntry)];
1105 for (i = 0; i < ic; i++) 1109 for (i = 0; i < ic; i++)
1106 {
1107 memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)],
1108 sizeof (struct MetaDataEntry));
1109 format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
1110 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1111 (format != EXTRACTOR_METAFORMAT_C_STRING) &&
1112 (format != EXTRACTOR_METAFORMAT_BINARY))
1113 {
1114 GNUNET_break_op (0);
1115 break;
1116 }
1117 dlen = ntohl (ent.data_size);
1118 plen = ntohl (ent.plugin_name_len);
1119 mlen = ntohl (ent.mime_type_len);
1120 if (dlen > left)
1121 {
1122 GNUNET_break_op (0);
1123 break;
1124 }
1125 left -= dlen;
1126 meta_data = &mdata[left];
1127 if ((format == EXTRACTOR_METAFORMAT_UTF8) ||
1128 (format == EXTRACTOR_METAFORMAT_C_STRING))
1129 {
1130 if ((dlen == 0) || (mdata[left + dlen - 1] != '\0'))
1131 {
1132 GNUNET_break_op (0);
1133 break;
1134 }
1135 }
1136 if (plen > left)
1137 {
1138 GNUNET_break_op (0);
1139 break;
1140 }
1141 left -= plen;
1142 if ((plen > 0) && (mdata[left + plen - 1] != '\0'))
1143 { 1110 {
1144 GNUNET_break_op (0); 1111 memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)],
1145 break; 1112 sizeof (struct MetaDataEntry));
1146 } 1113 format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
1147 if (plen == 0) 1114 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1148 plugin_name = NULL; 1115 (format != EXTRACTOR_METAFORMAT_C_STRING) &&
1149 else 1116 (format != EXTRACTOR_METAFORMAT_BINARY))
1150 plugin_name = &mdata[left]; 1117 {
1151 1118 GNUNET_break_op (0);
1152 if (mlen > left) 1119 break;
1153 { 1120 }
1154 GNUNET_break_op (0); 1121 dlen = ntohl (ent.data_size);
1155 break; 1122 plen = ntohl (ent.plugin_name_len);
1156 } 1123 mlen = ntohl (ent.mime_type_len);
1157 left -= mlen; 1124 if (dlen > left)
1158 if ((mlen > 0) && (mdata[left + mlen - 1] != '\0')) 1125 {
1159 { 1126 GNUNET_break_op (0);
1160 GNUNET_break_op (0); 1127 break;
1161 break; 1128 }
1129 left -= dlen;
1130 meta_data = &mdata[left];
1131 if ((format == EXTRACTOR_METAFORMAT_UTF8) ||
1132 (format == EXTRACTOR_METAFORMAT_C_STRING))
1133 {
1134 if ((dlen == 0) || (mdata[left + dlen - 1] != '\0'))
1135 {
1136 GNUNET_break_op (0);
1137 break;
1138 }
1139 }
1140 if (plen > left)
1141 {
1142 GNUNET_break_op (0);
1143 break;
1144 }
1145 left -= plen;
1146 if ((plen > 0) && (mdata[left + plen - 1] != '\0'))
1147 {
1148 GNUNET_break_op (0);
1149 break;
1150 }
1151 if (plen == 0)
1152 plugin_name = NULL;
1153 else
1154 plugin_name = &mdata[left];
1155
1156 if (mlen > left)
1157 {
1158 GNUNET_break_op (0);
1159 break;
1160 }
1161 left -= mlen;
1162 if ((mlen > 0) && (mdata[left + mlen - 1] != '\0'))
1163 {
1164 GNUNET_break_op (0);
1165 break;
1166 }
1167 if (mlen == 0)
1168 mime_type = NULL;
1169 else
1170 mime_type = &mdata[left];
1171 GNUNET_CONTAINER_meta_data_insert (md, plugin_name,
1172 (enum EXTRACTOR_MetaType)
1173 ntohl (ent.type), format, mime_type,
1174 meta_data, dlen);
1162 } 1175 }
1163 if (mlen == 0)
1164 mime_type = NULL;
1165 else
1166 mime_type = &mdata[left];
1167 GNUNET_CONTAINER_meta_data_insert (md, plugin_name,
1168 (enum EXTRACTOR_MetaType)
1169 ntohl (ent.type), format, mime_type,
1170 meta_data, dlen);
1171 }
1172 GNUNET_free_non_null (data); 1176 GNUNET_free_non_null (data);
1173 return md; 1177 return md;
1174} 1178}
diff --git a/src/util/container_multihashmap.c b/src/util/container_multihashmap.c
index 974d6c7f7..60d4a43d5 100644
--- a/src/util/container_multihashmap.c
+++ b/src/util/container_multihashmap.c
@@ -28,6 +28,8 @@
28#include "gnunet_container_lib.h" 28#include "gnunet_container_lib.h"
29#include "gnunet_crypto_lib.h" 29#include "gnunet_crypto_lib.h"
30 30
31#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32
31/** 33/**
32 * An entry in the hash map. 34 * An entry in the hash map.
33 */ 35 */
@@ -101,19 +103,19 @@ GNUNET_CONTAINER_multihashmap_create (unsigned int len)
101 */ 103 */
102void 104void
103GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap 105GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
104 *map) 106 *map)
105{ 107{
106 unsigned int i; 108 unsigned int i;
107 struct MapEntry *e; 109 struct MapEntry *e;
108 110
109 for (i = 0; i < map->map_length; i++) 111 for (i = 0; i < map->map_length; i++)
110 {
111 while (NULL != (e = map->map[i]))
112 { 112 {
113 map->map[i] = e->next; 113 while (NULL != (e = map->map[i]))
114 GNUNET_free (e); 114 {
115 map->map[i] = e->next;
116 GNUNET_free (e);
117 }
115 } 118 }
116 }
117 GNUNET_free (map->map); 119 GNUNET_free (map->map);
118 GNUNET_free (map); 120 GNUNET_free (map);
119} 121}
@@ -128,7 +130,7 @@ GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
128 */ 130 */
129static unsigned int 131static unsigned int
130idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m, 132idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
131 const GNUNET_HashCode * key) 133 const GNUNET_HashCode * key)
132{ 134{
133 GNUNET_assert (m != NULL); 135 GNUNET_assert (m != NULL);
134 return (*(unsigned int *) key) % m->map_length; 136 return (*(unsigned int *) key) % m->map_length;
@@ -143,7 +145,7 @@ idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
143 */ 145 */
144unsigned int 146unsigned int
145GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap 147GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
146 *map) 148 *map)
147{ 149{
148 return map->size; 150 return map->size;
149} 151}
@@ -161,17 +163,17 @@ GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
161 */ 163 */
162void * 164void *
163GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap 165GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
164 *map, const GNUNET_HashCode * key) 166 *map, const GNUNET_HashCode * key)
165{ 167{
166 struct MapEntry *e; 168 struct MapEntry *e;
167 169
168 e = map->map[idx_of (map, key)]; 170 e = map->map[idx_of (map, key)];
169 while (e != NULL) 171 while (e != NULL)
170 { 172 {
171 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 173 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
172 return e->value; 174 return e->value;
173 e = e->next; 175 e = e->next;
174 } 176 }
175 return NULL; 177 return NULL;
176} 178}
177 179
@@ -187,9 +189,9 @@ GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
187 */ 189 */
188int 190int
189GNUNET_CONTAINER_multihashmap_iterate (const struct 191GNUNET_CONTAINER_multihashmap_iterate (const struct
190 GNUNET_CONTAINER_MultiHashMap *map, 192 GNUNET_CONTAINER_MultiHashMap *map,
191 GNUNET_CONTAINER_HashMapIterator it, 193 GNUNET_CONTAINER_HashMapIterator it,
192 void *it_cls) 194 void *it_cls)
193{ 195{
194 int count; 196 int count;
195 unsigned int i; 197 unsigned int i;
@@ -200,20 +202,20 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct
200 count = 0; 202 count = 0;
201 GNUNET_assert (map != NULL); 203 GNUNET_assert (map != NULL);
202 for (i = 0; i < map->map_length; i++) 204 for (i = 0; i < map->map_length; i++)
203 {
204 n = map->map[i];
205 while (NULL != (e = n))
206 { 205 {
207 n = e->next; 206 n = map->map[i];
208 if (NULL != it) 207 while (NULL != (e = n))
209 { 208 {
210 kc = e->key; 209 n = e->next;
211 if (GNUNET_OK != it (it_cls, &kc, e->value)) 210 if (NULL != it)
212 return GNUNET_SYSERR; 211 {
213 } 212 kc = e->key;
214 count++; 213 if (GNUNET_OK != it (it_cls, &kc, e->value))
214 return GNUNET_SYSERR;
215 }
216 count++;
217 }
215 } 218 }
216 }
217 return count; 219 return count;
218} 220}
219 221
@@ -230,8 +232,9 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct
230 * is not in the map 232 * is not in the map
231 */ 233 */
232int 234int
233GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, 235GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
234 const GNUNET_HashCode * key, void *value) 236 *map, const GNUNET_HashCode * key,
237 void *value)
235{ 238{
236 struct MapEntry *e; 239 struct MapEntry *e;
237 struct MapEntry *p; 240 struct MapEntry *p;
@@ -241,21 +244,21 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
241 p = NULL; 244 p = NULL;
242 e = map->map[i]; 245 e = map->map[i];
243 while (e != NULL) 246 while (e != NULL)
244 {
245 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
246 (value == e->value))
247 { 247 {
248 if (p == NULL) 248 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
249 map->map[i] = e->next; 249 (value == e->value))
250 else 250 {
251 p->next = e->next; 251 if (p == NULL)
252 GNUNET_free (e); 252 map->map[i] = e->next;
253 map->size--; 253 else
254 return GNUNET_YES; 254 p->next = e->next;
255 GNUNET_free (e);
256 map->size--;
257 return GNUNET_YES;
258 }
259 p = e;
260 e = e->next;
255 } 261 }
256 p = e;
257 e = e->next;
258 }
259 return GNUNET_NO; 262 return GNUNET_NO;
260} 263}
261 264
@@ -270,7 +273,7 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
270 */ 273 */
271int 274int
272GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap 275GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
273 *map, const GNUNET_HashCode * key) 276 *map, const GNUNET_HashCode * key)
274{ 277{
275 struct MapEntry *e; 278 struct MapEntry *e;
276 struct MapEntry *p; 279 struct MapEntry *p;
@@ -282,27 +285,27 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
282 p = NULL; 285 p = NULL;
283 e = map->map[i]; 286 e = map->map[i];
284 while (e != NULL) 287 while (e != NULL)
285 {
286 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
287 { 288 {
288 if (p == NULL) 289 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
289 map->map[i] = e->next; 290 {
290 else 291 if (p == NULL)
291 p->next = e->next; 292 map->map[i] = e->next;
292 GNUNET_free (e); 293 else
293 map->size--; 294 p->next = e->next;
294 if (p == NULL) 295 GNUNET_free (e);
295 e = map->map[i]; 296 map->size--;
297 if (p == NULL)
298 e = map->map[i];
299 else
300 e = p->next;
301 ret++;
302 }
296 else 303 else
297 e = p->next; 304 {
298 ret++; 305 p = e;
299 } 306 e = e->next;
300 else 307 }
301 {
302 p = e;
303 e = e->next;
304 } 308 }
305 }
306 return ret; 309 return ret;
307} 310}
308 311
@@ -318,18 +321,18 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
318 */ 321 */
319int 322int
320GNUNET_CONTAINER_multihashmap_contains (const struct 323GNUNET_CONTAINER_multihashmap_contains (const struct
321 GNUNET_CONTAINER_MultiHashMap *map, 324 GNUNET_CONTAINER_MultiHashMap *map,
322 const GNUNET_HashCode * key) 325 const GNUNET_HashCode * key)
323{ 326{
324 struct MapEntry *e; 327 struct MapEntry *e;
325 328
326 e = map->map[idx_of (map, key)]; 329 e = map->map[idx_of (map, key)];
327 while (e != NULL) 330 while (e != NULL)
328 { 331 {
329 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 332 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
330 return GNUNET_YES; 333 return GNUNET_YES;
331 e = e->next; 334 e = e->next;
332 } 335 }
333 return GNUNET_NO; 336 return GNUNET_NO;
334} 337}
335 338
@@ -346,20 +349,21 @@ GNUNET_CONTAINER_multihashmap_contains (const struct
346 */ 349 */
347int 350int
348GNUNET_CONTAINER_multihashmap_contains_value (const struct 351GNUNET_CONTAINER_multihashmap_contains_value (const struct
349 GNUNET_CONTAINER_MultiHashMap 352 GNUNET_CONTAINER_MultiHashMap
350 *map, const GNUNET_HashCode * key, 353 *map,
351 const void *value) 354 const GNUNET_HashCode * key,
355 const void *value)
352{ 356{
353 struct MapEntry *e; 357 struct MapEntry *e;
354 358
355 e = map->map[idx_of (map, key)]; 359 e = map->map[idx_of (map, key)];
356 while (e != NULL) 360 while (e != NULL)
357 { 361 {
358 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && 362 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
359 (e->value == value)) 363 (e->value == value))
360 return GNUNET_YES; 364 return GNUNET_YES;
361 e = e->next; 365 e = e->next;
362 } 366 }
363 return GNUNET_NO; 367 return GNUNET_NO;
364} 368}
365 369
@@ -387,15 +391,15 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map)
387 map->map_length = new_len; 391 map->map_length = new_len;
388 map->map = new_map; 392 map->map = new_map;
389 for (i = 0; i < old_len; i++) 393 for (i = 0; i < old_len; i++)
390 {
391 while (NULL != (e = old_map[i]))
392 { 394 {
393 old_map[i] = e->next; 395 while (NULL != (e = old_map[i]))
394 idx = idx_of (map, &e->key); 396 {
395 e->next = new_map[idx]; 397 old_map[i] = e->next;
396 new_map[idx] = e; 398 idx = idx_of (map, &e->key);
399 e->next = new_map[idx];
400 new_map[idx] = e;
401 }
397 } 402 }
398 }
399 GNUNET_free (old_map); 403 GNUNET_free (old_map);
400} 404}
401 405
@@ -414,8 +418,9 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map)
414 */ 418 */
415int 419int
416GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, 420GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
417 const GNUNET_HashCode * key, void *value, 421 const GNUNET_HashCode * key, void *value,
418 enum GNUNET_CONTAINER_MultiHashMapOption opt) 422 enum GNUNET_CONTAINER_MultiHashMapOption
423 opt)
419{ 424{
420 struct MapEntry *e; 425 struct MapEntry *e;
421 unsigned int i; 426 unsigned int i;
@@ -423,25 +428,25 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
423 i = idx_of (map, key); 428 i = idx_of (map, key);
424 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 429 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
425 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 430 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
426 {
427 e = map->map[i];
428 while (e != NULL)
429 { 431 {
430 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 432 e = map->map[i];
431 { 433 while (e != NULL)
432 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 434 {
433 return GNUNET_SYSERR; 435 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
434 e->value = value; 436 {
435 return GNUNET_NO; 437 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
436 } 438 return GNUNET_SYSERR;
437 e = e->next; 439 e->value = value;
440 return GNUNET_NO;
441 }
442 e = e->next;
443 }
438 } 444 }
439 }
440 if (map->size / 3 >= map->map_length / 4) 445 if (map->size / 3 >= map->map_length / 4)
441 { 446 {
442 grow (map); 447 grow (map);
443 i = idx_of (map, key); 448 i = idx_of (map, key);
444 } 449 }
445 e = GNUNET_malloc (sizeof (struct MapEntry)); 450 e = GNUNET_malloc (sizeof (struct MapEntry));
446 e->key = *key; 451 e->key = *key;
447 e->value = value; 452 e->value = value;
@@ -464,10 +469,10 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
464 */ 469 */
465int 470int
466GNUNET_CONTAINER_multihashmap_get_multiple (const struct 471GNUNET_CONTAINER_multihashmap_get_multiple (const struct
467 GNUNET_CONTAINER_MultiHashMap *map, 472 GNUNET_CONTAINER_MultiHashMap
468 const GNUNET_HashCode * key, 473 *map, const GNUNET_HashCode * key,
469 GNUNET_CONTAINER_HashMapIterator it, 474 GNUNET_CONTAINER_HashMapIterator
470 void *it_cls) 475 it, void *it_cls)
471{ 476{
472 int count; 477 int count;
473 struct MapEntry *e; 478 struct MapEntry *e;
@@ -476,14 +481,14 @@ GNUNET_CONTAINER_multihashmap_get_multiple (const struct
476 count = 0; 481 count = 0;
477 n = map->map[idx_of (map, key)]; 482 n = map->map[idx_of (map, key)];
478 while (NULL != (e = n)) 483 while (NULL != (e = n))
479 { 484 {
480 n = e->next; 485 n = e->next;
481 if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 486 if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
482 continue; 487 continue;
483 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value))) 488 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
484 return GNUNET_SYSERR; 489 return GNUNET_SYSERR;
485 count++; 490 count++;
486 } 491 }
487 return count; 492 return count;
488} 493}
489 494
diff --git a/src/util/container_slist.c b/src/util/container_slist.c
index 1dd0344e4..144d352ce 100644
--- a/src/util/container_slist.c
+++ b/src/util/container_slist.c
@@ -27,6 +27,8 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_container_lib.h" 28#include "gnunet_container_lib.h"
29 29
30#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31
30/** 32/**
31 * Element in our linked list. 33 * Element in our linked list.
32 */ 34 */
@@ -108,21 +110,21 @@ struct GNUNET_CONTAINER_SList_Iterator
108 */ 110 */
109static struct GNUNET_CONTAINER_SList_Elem * 111static struct GNUNET_CONTAINER_SList_Elem *
110create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf, 112create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
111 size_t len) 113 size_t len)
112{ 114{
113 struct GNUNET_CONTAINER_SList_Elem *e; 115 struct GNUNET_CONTAINER_SList_Elem *e;
114 116
115 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT) 117 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
116 { 118 {
117 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len); 119 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
118 memcpy (&e[1], buf, len); 120 memcpy (&e[1], buf, len);
119 e->elem = (void *) &e[1]; 121 e->elem = (void *) &e[1];
120 } 122 }
121 else 123 else
122 { 124 {
123 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem)); 125 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
124 e->elem = (void *) buf; 126 e->elem = (void *) buf;
125 } 127 }
126 e->disp = disp; 128 e->disp = disp;
127 e->len = len; 129 e->len = len;
128 return e; 130 return e;
@@ -138,8 +140,8 @@ create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
138 */ 140 */
139void 141void
140GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, 142GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
141 enum GNUNET_CONTAINER_SListDisposition disp, 143 enum GNUNET_CONTAINER_SListDisposition disp,
142 const void *buf, size_t len) 144 const void *buf, size_t len)
143{ 145{
144 struct GNUNET_CONTAINER_SList_Elem *e; 146 struct GNUNET_CONTAINER_SList_Elem *e;
145 147
@@ -160,8 +162,8 @@ GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
160 */ 162 */
161void 163void
162GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l, 164GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
163 enum GNUNET_CONTAINER_SListDisposition disp, 165 enum GNUNET_CONTAINER_SListDisposition disp,
164 const void *buf, size_t len) 166 const void *buf, size_t len)
165{ 167{
166 struct GNUNET_CONTAINER_SList_Elem *e; 168 struct GNUNET_CONTAINER_SList_Elem *e;
167 169
@@ -182,7 +184,7 @@ GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
182 */ 184 */
183void 185void
184GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, 186GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
185 struct GNUNET_CONTAINER_SList *src) 187 struct GNUNET_CONTAINER_SList *src)
186{ 188{
187 struct GNUNET_CONTAINER_SList_Iterator *i; 189 struct GNUNET_CONTAINER_SList_Iterator *i;
188 190
@@ -190,14 +192,15 @@ GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
190 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES; 192 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
191 GNUNET_CONTAINER_slist_next (i)) 193 GNUNET_CONTAINER_slist_next (i))
192 194
193 { 195 {
194 GNUNET_CONTAINER_slist_add (dst, 196 GNUNET_CONTAINER_slist_add (dst,
195 (i->elem->disp == 197 (i->elem->disp ==
196 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ? 198 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC)
197 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC : 199 ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC
198 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 200 :
199 i->elem->elem, i->elem->len); 201 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
200 } 202 i->elem->elem, i->elem->len);
203 }
201 GNUNET_CONTAINER_slist_iter_destroy (i); 204 GNUNET_CONTAINER_slist_iter_destroy (i);
202} 205}
203 206
@@ -254,13 +257,13 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
254 257
255 e = l->head; 258 e = l->head;
256 while (e != NULL) 259 while (e != NULL)
257 { 260 {
258 n = e->next; 261 n = e->next;
259 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC) 262 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
260 GNUNET_free (e->elem); 263 GNUNET_free (e->elem);
261 GNUNET_free (e); 264 GNUNET_free (e);
262 e = n; 265 e = n;
263 } 266 }
264 l->head = NULL; 267 l->head = NULL;
265 l->tail = NULL; 268 l->tail = NULL;
266 l->length = 0; 269 l->length = 0;
@@ -276,7 +279,7 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
276 */ 279 */
277int 280int
278GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, 281GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
279 const void *buf, size_t len) 282 const void *buf, size_t len)
280{ 283{
281 struct GNUNET_CONTAINER_SList_Elem *e; 284 struct GNUNET_CONTAINER_SList_Elem *e;
282 285
@@ -333,8 +336,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
333 */ 336 */
334void 337void
335GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before, 338GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
336 enum GNUNET_CONTAINER_SListDisposition disp, 339 enum GNUNET_CONTAINER_SListDisposition disp,
337 const void *buf, size_t len) 340 const void *buf, size_t len)
338{ 341{
339 struct GNUNET_CONTAINER_SList_Elem *e; 342 struct GNUNET_CONTAINER_SList_Elem *e;
340 343
@@ -387,7 +390,7 @@ GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
387 */ 390 */
388void * 391void *
389GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, 392GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
390 size_t * len) 393 size_t * len)
391{ 394{
392 if (len) 395 if (len)
393 *len = i->elem->len; 396 *len = i->elem->len;
@@ -399,7 +402,8 @@ GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
399 * @param i iterator 402 * @param i iterator
400 */ 403 */
401void 404void
402GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i) 405GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
406 *i)
403{ 407{
404 GNUNET_free (i); 408 GNUNET_free (i);
405} 409}
diff --git a/src/util/crypto_aes.c b/src/util/crypto_aes.c
index a984e17fc..9e194094c 100644
--- a/src/util/crypto_aes.c
+++ b/src/util/crypto_aes.c
@@ -30,6 +30,8 @@
30#include "gnunet_crypto_lib.h" 30#include "gnunet_crypto_lib.h"
31#include <gcrypt.h> 31#include <gcrypt.h>
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
33/** 35/**
34 * Create a new SessionKey (for AES-256). 36 * Create a new SessionKey (for AES-256).
35 */ 37 */
@@ -37,9 +39,9 @@ void
37GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key) 39GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
38{ 40{
39 gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH, 41 gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH,
40 GCRY_STRONG_RANDOM); 42 GCRY_STRONG_RANDOM);
41 key->crc32 = 43 key->crc32 =
42 htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH)); 44 htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH));
43} 45}
44 46
45/** 47/**
@@ -49,7 +51,7 @@ GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
49 */ 51 */
50int 52int
51GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey 53GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
52 *key) 54 *key)
53{ 55{
54 uint32_t crc; 56 uint32_t crc;
55 57
@@ -74,28 +76,29 @@ GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
74 */ 76 */
75ssize_t 77ssize_t
76GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len, 78GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
77 const struct GNUNET_CRYPTO_AesSessionKey * 79 const struct GNUNET_CRYPTO_AesSessionKey *
78 sessionkey, 80 sessionkey,
79 const struct GNUNET_CRYPTO_AesInitializationVector * 81 const struct GNUNET_CRYPTO_AesInitializationVector
80 iv, void *result) 82 * iv, void *result)
81{ 83{
82 gcry_cipher_hd_t handle; 84 gcry_cipher_hd_t handle;
83 int rc; 85 int rc;
84 86
85 if (sessionkey->crc32 != 87 if (sessionkey->crc32 !=
86 htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) 88 htonl (GNUNET_CRYPTO_crc32_n
87 { 89 (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
88 GNUNET_break (0); 90 {
89 return -1; 91 GNUNET_break (0);
90 } 92 return -1;
93 }
91 GNUNET_assert (0 == 94 GNUNET_assert (0 ==
92 gcry_cipher_open (&handle, GCRY_CIPHER_AES256, 95 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
93 GCRY_CIPHER_MODE_CFB, 0)); 96 GCRY_CIPHER_MODE_CFB, 0));
94 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); 97 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
95 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 98 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
96 rc = gcry_cipher_setiv (handle, iv, 99 rc = gcry_cipher_setiv (handle, iv,
97 sizeof (struct 100 sizeof (struct
98 GNUNET_CRYPTO_AesInitializationVector)); 101 GNUNET_CRYPTO_AesInitializationVector));
99 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 102 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
100 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len)); 103 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len));
101 gcry_cipher_close (handle); 104 gcry_cipher_close (handle);
@@ -115,30 +118,32 @@ GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
115 */ 118 */
116ssize_t 119ssize_t
117GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size, 120GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
118 const struct GNUNET_CRYPTO_AesSessionKey * 121 const struct GNUNET_CRYPTO_AesSessionKey *
119 sessionkey, 122 sessionkey,
120 const struct GNUNET_CRYPTO_AesInitializationVector * 123 const struct GNUNET_CRYPTO_AesInitializationVector
121 iv, void *result) 124 * iv, void *result)
122{ 125{
123 gcry_cipher_hd_t handle; 126 gcry_cipher_hd_t handle;
124 int rc; 127 int rc;
125 128
126 if (sessionkey->crc32 != 129 if (sessionkey->crc32 !=
127 htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) 130 htonl (GNUNET_CRYPTO_crc32_n
128 { 131 (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
129 GNUNET_break (0); 132 {
130 return -1; 133 GNUNET_break (0);
131 } 134 return -1;
135 }
132 GNUNET_assert (0 == 136 GNUNET_assert (0 ==
133 gcry_cipher_open (&handle, GCRY_CIPHER_AES256, 137 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
134 GCRY_CIPHER_MODE_CFB, 0)); 138 GCRY_CIPHER_MODE_CFB, 0));
135 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); 139 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
136 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 140 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
137 rc = gcry_cipher_setiv (handle, iv, 141 rc = gcry_cipher_setiv (handle, iv,
138 sizeof (struct 142 sizeof (struct
139 GNUNET_CRYPTO_AesInitializationVector)); 143 GNUNET_CRYPTO_AesInitializationVector));
140 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 144 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
141 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size)); 145 GNUNET_assert (0 ==
146 gcry_cipher_decrypt (handle, result, size, block, size));
142 gcry_cipher_close (handle); 147 gcry_cipher_close (handle);
143 return size; 148 return size;
144} 149}
@@ -153,8 +158,8 @@ GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
153 */ 158 */
154void 159void
155GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv, 160GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
156 const struct GNUNET_CRYPTO_AesSessionKey *skey, 161 const struct GNUNET_CRYPTO_AesSessionKey *skey,
157 const void *salt, size_t salt_len, ...) 162 const void *salt, size_t salt_len, ...)
158{ 163{
159 va_list argp; 164 va_list argp;
160 165
@@ -172,12 +177,14 @@ GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
172 * @param argp pairs of void * & size_t for context chunks, terminated by NULL 177 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
173 */ 178 */
174void 179void
175GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv, 180GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector
176 const struct GNUNET_CRYPTO_AesSessionKey *skey, 181 *iv,
177 const void *salt, size_t salt_len, va_list argp) 182 const struct GNUNET_CRYPTO_AesSessionKey *skey,
183 const void *salt, size_t salt_len,
184 va_list argp)
178{ 185{
179 GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key, 186 GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key,
180 sizeof (skey->key), argp); 187 sizeof (skey->key), argp);
181} 188}
182 189
183/* end of crypto_aes.c */ 190/* end of crypto_aes.c */
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c
index 543bc4c65..5920ddc93 100644
--- a/src/util/crypto_crc.c
+++ b/src/util/crypto_crc.c
@@ -32,6 +32,8 @@
32#include "gnunet_common.h" 32#include "gnunet_common.h"
33#include "gnunet_crypto_lib.h" 33#include "gnunet_crypto_lib.h"
34 34
35#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36
35/* Avoid wasting space on 8-byte longs. */ 37/* Avoid wasting space on 8-byte longs. */
36#if UINT_MAX >= 0xffffffff 38#if UINT_MAX >= 0xffffffff
37typedef unsigned int uLong; 39typedef unsigned int uLong;
@@ -64,12 +66,12 @@ crc_init ()
64 once = 1; 66 once = 1;
65 crc_table[0] = 0; 67 crc_table[0] = 0;
66 for (i = 128; i; i >>= 1) 68 for (i = 128; i; i >>= 1)
67 { 69 {
68 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0); 70 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
69 /* h is now crc_table[i] */ 71 /* h is now crc_table[i] */
70 for (j = 0; j < 256; j += 2 * i) 72 for (j = 0; j < 256; j += 2 * i)
71 crc_table[i + j] = crc_table[j] ^ h; 73 crc_table[i + j] = crc_table[j] ^ h;
72 } 74 }
73} 75}
74 76
75/* 77/*
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index c253bbf2f..b8c9a8082 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -36,6 +36,9 @@
36#include "gnunet_disk_lib.h" 36#include "gnunet_disk_lib.h"
37#include <gcrypt.h> 37#include <gcrypt.h>
38 38
39#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40
41#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
39 42
40/** 43/**
41 * Hash block of given size. 44 * Hash block of given size.
@@ -116,14 +119,14 @@ struct GNUNET_CRYPTO_FileHashContext
116 */ 119 */
117static void 120static void
118file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc, 121file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
119 const GNUNET_HashCode * res) 122 const GNUNET_HashCode * res)
120{ 123{
121 fhc->callback (fhc->callback_cls, res); 124 fhc->callback (fhc->callback_cls, res);
122 GNUNET_free (fhc->filename); 125 GNUNET_free (fhc->filename);
123 if (!GNUNET_DISK_handle_invalid (fhc->fh)) 126 if (!GNUNET_DISK_handle_invalid (fhc->fh))
124 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh)); 127 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
125 gcry_md_close (fhc->md); 128 gcry_md_close (fhc->md);
126 GNUNET_free (fhc); /* also frees fhc->buffer */ 129 GNUNET_free (fhc); /* also frees fhc->buffer */
127} 130}
128 131
129 132
@@ -146,19 +149,19 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 if (fhc->fsize - fhc->offset < delta) 149 if (fhc->fsize - fhc->offset < delta)
147 delta = fhc->fsize - fhc->offset; 150 delta = fhc->fsize - fhc->offset;
148 if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta)) 151 if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
149 { 152 {
150 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename); 153 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
151 file_hash_finish (fhc, NULL); 154 file_hash_finish (fhc, NULL);
152 return; 155 return;
153 } 156 }
154 gcry_md_write (fhc->md, fhc->buffer, delta); 157 gcry_md_write (fhc->md, fhc->buffer, delta);
155 fhc->offset += delta; 158 fhc->offset += delta;
156 if (fhc->offset == fhc->fsize) 159 if (fhc->offset == fhc->fsize)
157 { 160 {
158 res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512); 161 res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512);
159 file_hash_finish (fhc, res); 162 file_hash_finish (fhc, res);
160 return; 163 return;
161 } 164 }
162 fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc); 165 fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc);
163} 166}
164 167
@@ -175,43 +178,43 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 */ 178 */
176struct GNUNET_CRYPTO_FileHashContext * 179struct GNUNET_CRYPTO_FileHashContext *
177GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority, 180GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
178 const char *filename, size_t blocksize, 181 const char *filename, size_t blocksize,
179 GNUNET_CRYPTO_HashCompletedCallback callback, 182 GNUNET_CRYPTO_HashCompletedCallback callback,
180 void *callback_cls) 183 void *callback_cls)
181{ 184{
182 struct GNUNET_CRYPTO_FileHashContext *fhc; 185 struct GNUNET_CRYPTO_FileHashContext *fhc;
183 186
184 GNUNET_assert (blocksize > 0); 187 GNUNET_assert (blocksize > 0);
185 fhc = 188 fhc =
186 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize); 189 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
187 fhc->callback = callback; 190 fhc->callback = callback;
188 fhc->callback_cls = callback_cls; 191 fhc->callback_cls = callback_cls;
189 fhc->buffer = (unsigned char *) &fhc[1]; 192 fhc->buffer = (unsigned char *) &fhc[1];
190 fhc->filename = GNUNET_strdup (filename); 193 fhc->filename = GNUNET_strdup (filename);
191 if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0)) 194 if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
192 { 195 {
193 GNUNET_break (0); 196 GNUNET_break (0);
194 GNUNET_free (fhc); 197 GNUNET_free (fhc);
195 return NULL; 198 return NULL;
196 } 199 }
197 fhc->bsize = blocksize; 200 fhc->bsize = blocksize;
198 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO)) 201 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO))
199 { 202 {
200 GNUNET_free (fhc->filename); 203 GNUNET_free (fhc->filename);
201 GNUNET_free (fhc); 204 GNUNET_free (fhc);
202 return NULL; 205 return NULL;
203 } 206 }
204 fhc->fh = 207 fhc->fh =
205 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 208 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
206 GNUNET_DISK_PERM_NONE); 209 GNUNET_DISK_PERM_NONE);
207 if (!fhc->fh) 210 if (!fhc->fh)
208 { 211 {
209 GNUNET_free (fhc->filename); 212 GNUNET_free (fhc->filename);
210 GNUNET_free (fhc); 213 GNUNET_free (fhc);
211 return NULL; 214 return NULL;
212 } 215 }
213 fhc->task = 216 fhc->task =
214 GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc); 217 GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
215 return fhc; 218 return fhc;
216} 219}
217 220
@@ -257,7 +260,7 @@ getValue__ (unsigned char a)
257 */ 260 */
258void 261void
259GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block, 262GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
260 struct GNUNET_CRYPTO_HashAsciiEncoded *result) 263 struct GNUNET_CRYPTO_HashAsciiEncoded *result)
261{ 264{
262 /** 265 /**
263 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters) 266 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters)
@@ -275,22 +278,23 @@ GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
275 rpos = 0; 278 rpos = 0;
276 bits = 0; 279 bits = 0;
277 while ((rpos < sizeof (GNUNET_HashCode)) || (vbit > 0)) 280 while ((rpos < sizeof (GNUNET_HashCode)) || (vbit > 0))
278 {
279 if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5))
280 {
281 bits = (bits << 8) | ((unsigned char *) block)[rpos++]; /* eat 8 more bits */
282 vbit += 8;
283 }
284 if (vbit < 5)
285 { 281 {
286 bits <<= (5 - vbit); /* zero-padding */ 282 if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5))
287 GNUNET_assert (vbit == 2); /* padding by 3: 512+3 mod 5 == 0 */ 283 {
288 vbit = 5; 284 bits = (bits << 8) | ((unsigned char *) block)[rpos++]; /* eat 8 more bits */
285 vbit += 8;
286 }
287 if (vbit < 5)
288 {
289 bits <<= (5 - vbit); /* zero-padding */
290 GNUNET_assert (vbit == 2); /* padding by 3: 512+3 mod 5 == 0 */
291 vbit = 5;
292 }
293 GNUNET_assert (wpos <
294 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
295 result->encoding[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
296 vbit -= 5;
289 } 297 }
290 GNUNET_assert (wpos < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
291 result->encoding[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
292 vbit -= 5;
293 }
294 GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1); 298 GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
295 GNUNET_assert (vbit == 0); 299 GNUNET_assert (vbit == 0);
296 result->encoding[wpos] = '\0'; 300 result->encoding[wpos] = '\0';
@@ -314,22 +318,22 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
314 if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1) 318 if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
315 return GNUNET_SYSERR; 319 return GNUNET_SYSERR;
316 320
317 vbit = 2; /* padding! */ 321 vbit = 2; /* padding! */
318 wpos = sizeof (GNUNET_HashCode); 322 wpos = sizeof (GNUNET_HashCode);
319 rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1; 323 rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
320 bits = getValue__ (enc[--rpos]) >> 3; 324 bits = getValue__ (enc[--rpos]) >> 3;
321 while (wpos > 0) 325 while (wpos > 0)
322 {
323 GNUNET_assert (rpos > 0);
324 bits = (getValue__ (enc[--rpos]) << vbit) | bits;
325 vbit += 5;
326 if (vbit >= 8)
327 { 326 {
328 ((unsigned char *) result)[--wpos] = (unsigned char) bits; 327 GNUNET_assert (rpos > 0);
329 bits >>= 8; 328 bits = (getValue__ (enc[--rpos]) << vbit) | bits;
330 vbit -= 8; 329 vbit += 5;
330 if (vbit >= 8)
331 {
332 ((unsigned char *) result)[--wpos] = (unsigned char) bits;
333 bits >>= 8;
334 vbit -= 8;
335 }
331 } 336 }
332 }
333 GNUNET_assert (rpos == 0); 337 GNUNET_assert (rpos == 0);
334 GNUNET_assert (vbit == 0); 338 GNUNET_assert (vbit == 0);
335 return GNUNET_OK; 339 return GNUNET_OK;
@@ -348,7 +352,7 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
348 */ 352 */
349unsigned int 353unsigned int
350GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, 354GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
351 const GNUNET_HashCode * b) 355 const GNUNET_HashCode * b)
352{ 356{
353 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16; 357 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
354 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16; 358 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
@@ -358,7 +362,7 @@ GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
358 362
359void 363void
360GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, 364GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
361 GNUNET_HashCode * result) 365 GNUNET_HashCode * result)
362{ 366{
363 int i; 367 int i;
364 368
@@ -368,33 +372,37 @@ GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
368 372
369void 373void
370GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, 374GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
371 const GNUNET_HashCode * b, 375 const GNUNET_HashCode * b,
372 GNUNET_HashCode * result) 376 GNUNET_HashCode * result)
373{ 377{
374 int i; 378 int i;
375 379
376 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) 380 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0;
381 i--)
377 result->bits[i] = b->bits[i] - a->bits[i]; 382 result->bits[i] = b->bits[i] - a->bits[i];
378} 383}
379 384
380void 385void
381GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, 386GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
382 const GNUNET_HashCode * delta, GNUNET_HashCode * result) 387 const GNUNET_HashCode * delta,
388 GNUNET_HashCode * result)
383{ 389{
384 int i; 390 int i;
385 391
386 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) 392 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0;
393 i--)
387 result->bits[i] = delta->bits[i] + a->bits[i]; 394 result->bits[i] = delta->bits[i] + a->bits[i];
388} 395}
389 396
390 397
391void 398void
392GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, 399GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
393 GNUNET_HashCode * result) 400 GNUNET_HashCode * result)
394{ 401{
395 int i; 402 int i;
396 403
397 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) 404 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0;
405 i--)
398 result->bits[i] = a->bits[i] ^ b->bits[i]; 406 result->bits[i] = a->bits[i] ^ b->bits[i];
399} 407}
400 408
@@ -404,17 +412,18 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
404 */ 412 */
405void 413void
406GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, 414GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
407 struct GNUNET_CRYPTO_AesSessionKey *skey, 415 struct GNUNET_CRYPTO_AesSessionKey *skey,
408 struct GNUNET_CRYPTO_AesInitializationVector *iv) 416 struct GNUNET_CRYPTO_AesInitializationVector
417 *iv)
409{ 418{
410 GNUNET_assert (sizeof (GNUNET_HashCode) >= 419 GNUNET_assert (sizeof (GNUNET_HashCode) >=
411 GNUNET_CRYPTO_AES_KEY_LENGTH + 420 GNUNET_CRYPTO_AES_KEY_LENGTH +
412 sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); 421 sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
413 memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH); 422 memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH);
414 skey->crc32 = 423 skey->crc32 =
415 htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH)); 424 htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH));
416 memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH], 425 memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH],
417 sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); 426 sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
418} 427}
419 428
420 429
@@ -445,13 +454,13 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
445 */ 454 */
446unsigned int 455unsigned int
447GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first, 456GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
448 const GNUNET_HashCode * second) 457 const GNUNET_HashCode * second)
449{ 458{
450 unsigned int i; 459 unsigned int i;
451 460
452 for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++) 461 for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
453 if (GNUNET_CRYPTO_hash_get_bit (first, i) != 462 if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
454 GNUNET_CRYPTO_hash_get_bit (second, i)) 463 GNUNET_CRYPTO_hash_get_bit (second, i))
455 return i; 464 return i;
456 return sizeof (GNUNET_HashCode) * 8; 465 return sizeof (GNUNET_HashCode) * 8;
457} 466}
@@ -463,7 +472,8 @@ GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
463 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. 472 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
464 */ 473 */
465int 474int
466GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2) 475GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
476 const GNUNET_HashCode * h2)
467{ 477{
468 unsigned int *i1; 478 unsigned int *i1;
469 unsigned int *i2; 479 unsigned int *i2;
@@ -471,13 +481,14 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
471 481
472 i1 = (unsigned int *) h1; 482 i1 = (unsigned int *) h1;
473 i2 = (unsigned int *) h2; 483 i2 = (unsigned int *) h2;
474 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) 484 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0;
475 { 485 i--)
476 if (i1[i] > i2[i]) 486 {
477 return 1; 487 if (i1[i] > i2[i])
478 if (i1[i] < i2[i]) 488 return 1;
479 return -1; 489 if (i1[i] < i2[i])
480 } 490 return -1;
491 }
481 return 0; 492 return 0;
482} 493}
483 494
@@ -489,22 +500,22 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
489 */ 500 */
490int 501int
491GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, 502GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
492 const GNUNET_HashCode * h2, 503 const GNUNET_HashCode * h2,
493 const GNUNET_HashCode * target) 504 const GNUNET_HashCode * target)
494{ 505{
495 int i; 506 int i;
496 unsigned int d1; 507 unsigned int d1;
497 unsigned int d2; 508 unsigned int d2;
498 509
499 for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--) 510 for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--)
500 { 511 {
501 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; 512 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
502 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; 513 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
503 if (d1 > d2) 514 if (d1 > d2)
504 return 1; 515 return 1;
505 else if (d1 < d2) 516 else if (d1 < d2)
506 return -1; 517 return -1;
507 } 518 }
508 return 0; 519 return 0;
509} 520}
510 521
@@ -519,8 +530,8 @@ GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
519 */ 530 */
520void 531void
521GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key, 532GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
522 const struct GNUNET_CRYPTO_AesSessionKey *rkey, 533 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
523 const void *salt, size_t salt_len, ...) 534 const void *salt, size_t salt_len, ...)
524{ 535{
525 va_list argp; 536 va_list argp;
526 537
@@ -540,12 +551,12 @@ GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
540 */ 551 */
541void 552void
542GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, 553GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
543 const struct GNUNET_CRYPTO_AesSessionKey *rkey, 554 const struct GNUNET_CRYPTO_AesSessionKey
544 const void *salt, size_t salt_len, 555 *rkey, const void *salt, size_t salt_len,
545 va_list argp) 556 va_list argp)
546{ 557{
547 GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key, 558 GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key,
548 sizeof (rkey->key), argp); 559 sizeof (rkey->key), argp);
549} 560}
550 561
551 562
@@ -559,14 +570,14 @@ GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
559 */ 570 */
560void 571void
561GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key, 572GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
562 const void *plaintext, size_t plaintext_len, 573 const void *plaintext, size_t plaintext_len,
563 GNUNET_HashCode * hmac) 574 GNUNET_HashCode * hmac)
564{ 575{
565 gcry_md_hd_t md; 576 gcry_md_hd_t md;
566 const unsigned char *mc; 577 const unsigned char *mc;
567 578
568 GNUNET_assert (GPG_ERR_NO_ERROR == 579 GNUNET_assert (GPG_ERR_NO_ERROR ==
569 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC)); 580 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
570 gcry_md_setkey (md, key->key, sizeof (key->key)); 581 gcry_md_setkey (md, key->key, sizeof (key->key));
571 gcry_md_write (md, plaintext, plaintext_len); 582 gcry_md_write (md, plaintext, plaintext_len);
572 mc = gcry_md_read (md, GCRY_MD_SHA512); 583 mc = gcry_md_read (md, GCRY_MD_SHA512);
diff --git a/src/util/crypto_hkdf.c b/src/util/crypto_hkdf.c
index 0654b51f6..6af76ba68 100644
--- a/src/util/crypto_hkdf.c
+++ b/src/util/crypto_hkdf.c
@@ -36,6 +36,8 @@
36 * - Matthias Wachs (08.10.2010) 36 * - Matthias Wachs (08.10.2010)
37 */ 37 */
38 38
39#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40
39/** 41/**
40 * Set this to 0 if you compile this code outside of GNUnet. 42 * Set this to 0 if you compile this code outside of GNUnet.
41 */ 43 */
@@ -72,7 +74,7 @@
72 */ 74 */
73static const void * 75static const void *
74doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf, 76doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
75 size_t buf_len) 77 size_t buf_len)
76{ 78{
77 gcry_md_setkey (mac, key, key_len); 79 gcry_md_setkey (mac, key, key_len);
78 gcry_md_write (mac, buf, buf_len); 80 gcry_md_write (mac, buf, buf_len);
@@ -92,7 +94,7 @@ doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
92 */ 94 */
93static int 95static int
94getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm, 96getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
95 size_t skm_len, void *prk) 97 size_t skm_len, void *prk)
96{ 98{
97 const void *ret; 99 const void *ret;
98 100
@@ -113,9 +115,9 @@ dump (const char *src, const void *p, unsigned int l)
113 115
114 printf ("\n%s: ", src); 116 printf ("\n%s: ", src);
115 for (i = 0; i < l; i++) 117 for (i = 0; i < l; i++)
116 { 118 {
117 printf ("%2x", (int) ((const unsigned char *) p)[i]); 119 printf ("%2x", (int) ((const unsigned char *) p)[i]);
118 } 120 }
119 printf ("\n"); 121 printf ("\n");
120} 122}
121#endif 123#endif
@@ -135,9 +137,9 @@ dump (const char *src, const void *p, unsigned int l)
135 * @return GNUNET_YES on success 137 * @return GNUNET_YES on success
136 */ 138 */
137int 139int
138GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo, 140GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo,
139 const void *xts, size_t xts_len, const void *skm, 141 int prf_algo, const void *xts, size_t xts_len,
140 size_t skm_len, va_list argp) 142 const void *skm, size_t skm_len, va_list argp)
141{ 143{
142 const void *hc; 144 const void *hc;
143 unsigned long i, t, d; 145 unsigned long i, t, d;
@@ -156,16 +158,16 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
156 return GNUNET_SYSERR; 158 return GNUNET_SYSERR;
157 159
158 if (gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR) 160 if (gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
159 { 161 {
160 gcry_md_close (xtr); 162 gcry_md_close (xtr);
161 return GNUNET_SYSERR; 163 return GNUNET_SYSERR;
162 } 164 }
163 165
164 va_copy (args, argp); 166 va_copy (args, argp);
165 167
166 ctx_len = 0; 168 ctx_len = 0;
167 while (NULL != va_arg (args, void *)) 169 while (NULL != va_arg (args, void *))
168 ctx_len += va_arg (args, size_t); 170 ctx_len += va_arg (args, size_t);
169 171
170 va_end (args); 172 va_end (args);
171 173
@@ -189,65 +191,65 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
189 dst = plain + k; 191 dst = plain + k;
190 va_copy (args, argp); 192 va_copy (args, argp);
191 while ((ctx = va_arg (args, void *))) 193 while ((ctx = va_arg (args, void *)))
192 { 194 {
193 size_t len; 195 size_t len;
194 196
195 len = va_arg (args, size_t); 197 len = va_arg (args, size_t);
196 memcpy (dst, ctx, len); 198 memcpy (dst, ctx, len);
197 dst += len; 199 dst += len;
198 } 200 }
199 va_end (args); 201 va_end (args);
200 202
201 if (t > 0) 203 if (t > 0)
202 { 204 {
203 memset (plain + k + ctx_len, 1, 1); 205 memset (plain + k + ctx_len, 1, 1);
204#if DEBUG_HKDF 206#if DEBUG_HKDF
205 dump ("K(1)", plain, plain_len); 207 dump ("K(1)", plain, plain_len);
206#endif 208#endif
207 hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1); 209 hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
208 if (hc == NULL) 210 if (hc == NULL)
209 goto hkdf_error; 211 goto hkdf_error;
210 memcpy (result, hc, k); 212 memcpy (result, hc, k);
211 result += k; 213 result += k;
212 } 214 }
213 215
214 /* K(i+1) */ 216 /* K(i+1) */
215 for (i = 1; i < t; i++) 217 for (i = 1; i < t; i++)
216 { 218 {
217 memcpy (plain, result - k, k); 219 memcpy (plain, result - k, k);
218 memset (plain + k + ctx_len, i + 1, 1); 220 memset (plain + k + ctx_len, i + 1, 1);
219 gcry_md_reset (prf); 221 gcry_md_reset (prf);
220#if DEBUG_HKDF 222#if DEBUG_HKDF
221 dump ("K(i+1)", plain, plain_len); 223 dump ("K(i+1)", plain, plain_len);
222#endif 224#endif
223 hc = doHMAC (prf, prk, xtr_len, plain, plain_len); 225 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
224 if (hc == NULL) 226 if (hc == NULL)
225 goto hkdf_error; 227 goto hkdf_error;
226 memcpy (result, hc, k); 228 memcpy (result, hc, k);
227 result += k; 229 result += k;
228 } 230 }
229 231
230 /* K(t):d */ 232 /* K(t):d */
231 if (d > 0) 233 if (d > 0)
232 {
233 if (t > 0)
234 { 234 {
235 memcpy (plain, result - k, k); 235 if (t > 0)
236 i++; 236 {
237 } 237 memcpy (plain, result - k, k);
238 memset (plain + k + ctx_len, i, 1); 238 i++;
239 gcry_md_reset (prf); 239 }
240 memset (plain + k + ctx_len, i, 1);
241 gcry_md_reset (prf);
240#if DEBUG_HKDF 242#if DEBUG_HKDF
241 dump ("K(t):d", plain, plain_len); 243 dump ("K(t):d", plain, plain_len);
242#endif 244#endif
243 if (t > 0) 245 if (t > 0)
244 hc = doHMAC (prf, prk, xtr_len, plain, plain_len); 246 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
245 else 247 else
246 hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k); 248 hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
247 if (hc == NULL) 249 if (hc == NULL)
248 goto hkdf_error; 250 goto hkdf_error;
249 memcpy (result, hc, d); 251 memcpy (result, hc, d);
250 } 252 }
251#if DEBUG_HKDF 253#if DEBUG_HKDF
252 dump ("result", result - k, out_len); 254 dump ("result", result - k, out_len);
253#endif 255#endif
@@ -279,16 +281,16 @@ hkdf_ok:
279 */ 281 */
280int 282int
281GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo, 283GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
282 const void *xts, size_t xts_len, const void *skm, 284 const void *xts, size_t xts_len, const void *skm,
283 size_t skm_len, ...) 285 size_t skm_len, ...)
284{ 286{
285 va_list argp; 287 va_list argp;
286 int ret; 288 int ret;
287 289
288 va_start (argp, skm_len); 290 va_start (argp, skm_len);
289 ret = 291 ret =
290 GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len, 292 GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
291 skm, skm_len, argp); 293 skm, skm_len, argp);
292 va_end (argp); 294 va_end (argp);
293 295
294 return ret; 296 return ret;
diff --git a/src/util/crypto_kdf.c b/src/util/crypto_kdf.c
index eff4e6fd6..d8fcbd866 100644
--- a/src/util/crypto_kdf.c
+++ b/src/util/crypto_kdf.c
@@ -29,6 +29,8 @@
29#include "platform.h" 29#include "platform.h"
30#include "gnunet_crypto_lib.h" 30#include "gnunet_crypto_lib.h"
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33
32/** 34/**
33 * @brief Derive key 35 * @brief Derive key
34 * @param result buffer for the derived key, allocated by caller 36 * @param result buffer for the derived key, allocated by caller
@@ -42,8 +44,8 @@
42 */ 44 */
43int 45int
44GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts, 46GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
45 size_t xts_len, const void *skm, size_t skm_len, 47 size_t xts_len, const void *skm, size_t skm_len,
46 va_list argp) 48 va_list argp)
47{ 49{
48 /* 50 /*
49 * "Finally, we point out to a particularly advantageous instantiation using 51 * "Finally, we point out to a particularly advantageous instantiation using
@@ -57,8 +59,9 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
57 * http://eprint.iacr.org/2010/264 59 * http://eprint.iacr.org/2010/264
58 */ 60 */
59 61
60 return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512, GCRY_MD_SHA256, 62 return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512,
61 xts, xts_len, skm, skm_len, argp); 63 GCRY_MD_SHA256, xts, xts_len, skm, skm_len,
64 argp);
62} 65}
63 66
64/** 67/**
@@ -74,13 +77,14 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
74 */ 77 */
75int 78int
76GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts, 79GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
77 size_t xts_len, const void *skm, size_t skm_len, ...) 80 size_t xts_len, const void *skm, size_t skm_len, ...)
78{ 81{
79 va_list argp; 82 va_list argp;
80 int ret; 83 int ret;
81 84
82 va_start (argp, skm_len); 85 va_start (argp, skm_len);
83 ret = GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp); 86 ret =
87 GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp);
84 va_end (argp); 88 va_end (argp);
85 89
86 return ret; 90 return ret;
diff --git a/src/util/crypto_ksk.c b/src/util/crypto_ksk.c
index bec87c803..3d40ce27b 100644
--- a/src/util/crypto_ksk.c
+++ b/src/util/crypto_ksk.c
@@ -39,22 +39,24 @@
39#include <gcrypt.h> 39#include <gcrypt.h>
40#include <limits.h> 40#include <limits.h>
41 41
42#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
43
42/** 44/**
43 * Log an error message at log-level 'level' that indicates 45 * Log an error message at log-level 'level' that indicates
44 * a failure of the command 'cmd' with the message given 46 * a failure of the command 'cmd' with the message given
45 * by gcry_strerror(rc). 47 * by gcry_strerror(rc).
46 */ 48 */
47#define LOG_GCRY(level, cmd, rc) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0); 49#define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0);
48 50
49 51
50typedef struct 52typedef struct
51{ 53{
52 gcry_mpi_t n; /* public modulus */ 54 gcry_mpi_t n; /* public modulus */
53 gcry_mpi_t e; /* public exponent */ 55 gcry_mpi_t e; /* public exponent */
54 gcry_mpi_t d; /* exponent */ 56 gcry_mpi_t d; /* exponent */
55 gcry_mpi_t p; /* prime p. */ 57 gcry_mpi_t p; /* prime p. */
56 gcry_mpi_t q; /* prime q. */ 58 gcry_mpi_t q; /* prime q. */
57 gcry_mpi_t u; /* inverse of p mod q. */ 59 gcry_mpi_t u; /* inverse of p mod q. */
58} KBlock_secret_key; 60} KBlock_secret_key;
59 61
60/** 62/**
@@ -82,23 +84,23 @@ mpz_randomize (gcry_mpi_t n, unsigned int nbits, GNUNET_HashCode * rnd)
82 84
83 tmp = *rnd; 85 tmp = *rnd;
84 for (i = 0; i < cnt; i++) 86 for (i = 0; i < cnt; i++)
85 {
86 int j;
87
88 if (i > 0)
89 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), &tmp);
90 for (j = 0; j < sizeof (GNUNET_HashCode) / sizeof (uint32_t); j++)
91 { 87 {
88 int j;
89
90 if (i > 0)
91 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), &tmp);
92 for (j = 0; j < sizeof (GNUNET_HashCode) / sizeof (uint32_t); j++)
93 {
92#if HAVE_GCRY_MPI_LSHIFT 94#if HAVE_GCRY_MPI_LSHIFT
93 gcry_mpi_lshift (n, n, sizeof (uint32_t) * 8); 95 gcry_mpi_lshift (n, n, sizeof (uint32_t) * 8);
94#else 96#else
95 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); 97 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4));
96 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); 98 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4));
97#endif 99#endif
98 gcry_mpi_add_ui (n, n, ntohl (((uint32_t *) & tmp)[j])); 100 gcry_mpi_add_ui (n, n, ntohl (((uint32_t *) & tmp)[j]));
101 }
102 hc = tmp;
99 } 103 }
100 hc = tmp;
101 }
102 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), rnd); 104 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), rnd);
103 i = gcry_mpi_get_nbits (n); 105 i = gcry_mpi_get_nbits (n);
104 while (i > nbits) 106 while (i > nbits)
@@ -112,10 +114,10 @@ mpz_trailing_zeroes (gcry_mpi_t n)
112 114
113 cnt = gcry_mpi_get_nbits (n); 115 cnt = gcry_mpi_get_nbits (n);
114 for (idx = 0; idx < cnt; idx++) 116 for (idx = 0; idx < cnt; idx++)
115 { 117 {
116 if (gcry_mpi_test_bit (n, idx) == 0) 118 if (gcry_mpi_test_bit (n, idx) == 0)
117 return idx; 119 return idx;
118 } 120 }
119 121
120 return ULONG_MAX; 122 return ULONG_MAX;
121} 123}
@@ -162,31 +164,31 @@ is_prime (gcry_mpi_t n, int steps, GNUNET_HashCode * hc)
162 mpz_tdiv_q_2exp (q, q, k); 164 mpz_tdiv_q_2exp (q, q, k);
163 165
164 for (i = 0; i < steps; i++) 166 for (i = 0; i < steps; i++)
165 {
166 if (!i)
167 {
168 gcry_mpi_set_ui (x, 2);
169 }
170 else
171 { 167 {
172 mpz_randomize (x, nbits - 1, hc); 168 if (!i)
173 GNUNET_assert (gcry_mpi_cmp (x, nminus1) < 0); 169 {
174 GNUNET_assert (gcry_mpi_cmp_ui (x, 1) > 0); 170 gcry_mpi_set_ui (x, 2);
171 }
172 else
173 {
174 mpz_randomize (x, nbits - 1, hc);
175 GNUNET_assert (gcry_mpi_cmp (x, nminus1) < 0);
176 GNUNET_assert (gcry_mpi_cmp_ui (x, 1) > 0);
177 }
178 gcry_mpi_powm (y, x, q, n);
179 if (gcry_mpi_cmp_ui (y, 1) && gcry_mpi_cmp (y, nminus1))
180 {
181 for (j = 1; j < k && gcry_mpi_cmp (y, nminus1); j++)
182 {
183 gcry_mpi_powm (y, y, a2, n);
184 if (!gcry_mpi_cmp_ui (y, 1))
185 goto leave; /* Not a prime. */
186 }
187 if (gcry_mpi_cmp (y, nminus1))
188 goto leave; /* Not a prime. */
189 }
175 } 190 }
176 gcry_mpi_powm (y, x, q, n); 191 rc = 1; /* May be a prime. */
177 if (gcry_mpi_cmp_ui (y, 1) && gcry_mpi_cmp (y, nminus1))
178 {
179 for (j = 1; j < k && gcry_mpi_cmp (y, nminus1); j++)
180 {
181 gcry_mpi_powm (y, y, a2, n);
182 if (!gcry_mpi_cmp_ui (y, 1))
183 goto leave; /* Not a prime. */
184 }
185 if (gcry_mpi_cmp (y, nminus1))
186 goto leave; /* Not a prime. */
187 }
188 }
189 rc = 1; /* May be a prime. */
190 192
191leave: 193leave:
192 gcry_mpi_release (x); 194 gcry_mpi_release (x);
@@ -208,10 +210,10 @@ static void
208adjust (unsigned char *buf, size_t size, size_t target) 210adjust (unsigned char *buf, size_t size, size_t target)
209{ 211{
210 if (size < target) 212 if (size < target)
211 { 213 {
212 memmove (&buf[target - size], buf, size); 214 memmove (&buf[target - size], buf, size);
213 memset (buf, 0, target - size); 215 memset (buf, 0, target - size);
214 } 216 }
215} 217}
216 218
217 219
@@ -324,70 +326,70 @@ gen_prime (gcry_mpi_t * ptest, unsigned int nbits, GNUNET_HashCode * hc)
324 tmp = gcry_mpi_new (0); 326 tmp = gcry_mpi_new (0);
325 sp = gcry_mpi_new (0); 327 sp = gcry_mpi_new (0);
326 while (1) 328 while (1)
327 {
328 /* generate a random number */
329 mpz_randomize (prime, nbits, hc);
330 /* Set high order bit to 1, set low order bit to 1. If we are
331 * generating a secret prime we are most probably doing that
332 * for RSA, to make sure that the modulus does have the
333 * requested key size we set the 2 high order bits. */
334 gcry_mpi_set_bit (prime, nbits - 1);
335 gcry_mpi_set_bit (prime, nbits - 2);
336 gcry_mpi_set_bit (prime, 0);
337
338 /* Calculate all remainders. */
339 for (i = 0; i < no_of_small_prime_numbers; i++)
340 {
341 size_t written;
342
343 gcry_mpi_set_ui (sp, small_prime_numbers[i]);
344 gcry_mpi_div (NULL, tmp, prime, sp, -1);
345 mods[i] = 0;
346 written = sizeof (unsigned int);
347 GNUNET_assert (0 ==
348 gcry_mpi_print (GCRYMPI_FMT_USG,
349 (unsigned char *) &mods[i], written,
350 &written, tmp));
351 adjust ((unsigned char *) &mods[i], written, sizeof (unsigned int));
352 mods[i] = ntohl (mods[i]);
353 }
354 /* Now try some primes starting with prime. */
355 for (step = 0; step < 20000; step += 2)
356 { 329 {
357 /* Check against all the small primes we have in mods. */ 330 /* generate a random number */
331 mpz_randomize (prime, nbits, hc);
332 /* Set high order bit to 1, set low order bit to 1. If we are
333 * generating a secret prime we are most probably doing that
334 * for RSA, to make sure that the modulus does have the
335 * requested key size we set the 2 high order bits. */
336 gcry_mpi_set_bit (prime, nbits - 1);
337 gcry_mpi_set_bit (prime, nbits - 2);
338 gcry_mpi_set_bit (prime, 0);
339
340 /* Calculate all remainders. */
358 for (i = 0; i < no_of_small_prime_numbers; i++) 341 for (i = 0; i < no_of_small_prime_numbers; i++)
359 { 342 {
360 uint16_t x = small_prime_numbers[i]; 343 size_t written;
361 344
362 while (mods[i] + step >= x) 345 gcry_mpi_set_ui (sp, small_prime_numbers[i]);
363 mods[i] -= x; 346 gcry_mpi_div (NULL, tmp, prime, sp, -1);
364 if (!(mods[i] + step)) 347 mods[i] = 0;
365 break; 348 written = sizeof (unsigned int);
366 } 349 GNUNET_assert (0 ==
367 if (i < no_of_small_prime_numbers) 350 gcry_mpi_print (GCRYMPI_FMT_USG,
368 continue; /* Found a multiple of an already known prime. */ 351 (unsigned char *) &mods[i], written,
369 352 &written, tmp));
370 gcry_mpi_add_ui (*ptest, prime, step); 353 adjust ((unsigned char *) &mods[i], written, sizeof (unsigned int));
371 if (!gcry_mpi_test_bit (*ptest, nbits - 2)) 354 mods[i] = ntohl (mods[i]);
372 break; 355 }
373 356 /* Now try some primes starting with prime. */
374 /* Do a fast Fermat test now. */ 357 for (step = 0; step < 20000; step += 2)
375 gcry_mpi_sub_ui (pminus1, *ptest, 1); 358 {
376 gcry_mpi_powm (result, val_2, pminus1, *ptest); 359 /* Check against all the small primes we have in mods. */
377 if ((!gcry_mpi_cmp_ui (result, 1)) && (is_prime (*ptest, 5, hc))) 360 for (i = 0; i < no_of_small_prime_numbers; i++)
378 { 361 {
379 /* Got it. */ 362 uint16_t x = small_prime_numbers[i];
380 gcry_mpi_release (sp); 363
381 gcry_mpi_release (tmp); 364 while (mods[i] + step >= x)
382 gcry_mpi_release (val_2); 365 mods[i] -= x;
383 gcry_mpi_release (val_3); 366 if (!(mods[i] + step))
384 gcry_mpi_release (result); 367 break;
385 gcry_mpi_release (pminus1); 368 }
386 gcry_mpi_release (prime); 369 if (i < no_of_small_prime_numbers)
387 return; 370 continue; /* Found a multiple of an already known prime. */
388 } 371
372 gcry_mpi_add_ui (*ptest, prime, step);
373 if (!gcry_mpi_test_bit (*ptest, nbits - 2))
374 break;
375
376 /* Do a fast Fermat test now. */
377 gcry_mpi_sub_ui (pminus1, *ptest, 1);
378 gcry_mpi_powm (result, val_2, pminus1, *ptest);
379 if ((!gcry_mpi_cmp_ui (result, 1)) && (is_prime (*ptest, 5, hc)))
380 {
381 /* Got it. */
382 gcry_mpi_release (sp);
383 gcry_mpi_release (tmp);
384 gcry_mpi_release (val_2);
385 gcry_mpi_release (val_3);
386 gcry_mpi_release (result);
387 gcry_mpi_release (pminus1);
388 gcry_mpi_release (prime);
389 return;
390 }
391 }
389 } 392 }
390 }
391} 393}
392 394
393/** 395/**
@@ -397,11 +399,11 @@ gen_prime (gcry_mpi_t * ptest, unsigned int nbits, GNUNET_HashCode * hc)
397 * @param hc the HC to use for PRNG (modified!) 399 * @param hc the HC to use for PRNG (modified!)
398 */ 400 */
399static void 401static void
400generate_kblock_key (KBlock_secret_key *sk, unsigned int nbits, 402generate_kblock_key (KBlock_secret_key * sk, unsigned int nbits,
401 GNUNET_HashCode * hc) 403 GNUNET_HashCode * hc)
402{ 404{
403 gcry_mpi_t t1, t2; 405 gcry_mpi_t t1, t2;
404 gcry_mpi_t phi; /* helper: (p-1)(q-1) */ 406 gcry_mpi_t phi; /* helper: (p-1)(q-1) */
405 gcry_mpi_t g; 407 gcry_mpi_t g;
406 gcry_mpi_t f; 408 gcry_mpi_t f;
407 409
@@ -423,36 +425,36 @@ generate_kblock_key (KBlock_secret_key *sk, unsigned int nbits,
423 f = gcry_mpi_new (0); 425 f = gcry_mpi_new (0);
424 426
425 do 427 do
426 {
427 do
428 { 428 {
429 gcry_mpi_release (sk->p); 429 do
430 gcry_mpi_release (sk->q); 430 {
431 gen_prime (&sk->p, nbits / 2, hc); 431 gcry_mpi_release (sk->p);
432 gen_prime (&sk->q, nbits / 2, hc); 432 gcry_mpi_release (sk->q);
433 433 gen_prime (&sk->p, nbits / 2, hc);
434 if (gcry_mpi_cmp (sk->p, sk->q) > 0) /* p shall be smaller than q (for calc of u) */ 434 gen_prime (&sk->q, nbits / 2, hc);
435 gcry_mpi_swap (sk->p, sk->q); 435
436 /* calculate the modulus */ 436 if (gcry_mpi_cmp (sk->p, sk->q) > 0) /* p shall be smaller than q (for calc of u) */
437 gcry_mpi_mul (sk->n, sk->p, sk->q); 437 gcry_mpi_swap (sk->p, sk->q);
438 /* calculate the modulus */
439 gcry_mpi_mul (sk->n, sk->p, sk->q);
440 }
441 while (gcry_mpi_get_nbits (sk->n) != nbits);
442
443 /* calculate Euler totient: phi = (p-1)(q-1) */
444 gcry_mpi_sub_ui (t1, sk->p, 1);
445 gcry_mpi_sub_ui (t2, sk->q, 1);
446 gcry_mpi_mul (phi, t1, t2);
447 gcry_mpi_gcd (g, t1, t2);
448 gcry_mpi_div (f, NULL, phi, g, 0);
449 while (0 == gcry_mpi_gcd (t1, sk->e, phi))
450 { /* (while gcd is not 1) */
451 gcry_mpi_add_ui (sk->e, sk->e, 2);
452 }
453
454 /* calculate the secret key d = e^1 mod phi */
438 } 455 }
439 while (gcry_mpi_get_nbits (sk->n) != nbits);
440
441 /* calculate Euler totient: phi = (p-1)(q-1) */
442 gcry_mpi_sub_ui (t1, sk->p, 1);
443 gcry_mpi_sub_ui (t2, sk->q, 1);
444 gcry_mpi_mul (phi, t1, t2);
445 gcry_mpi_gcd (g, t1, t2);
446 gcry_mpi_div (f, NULL, phi, g, 0);
447 while (0 == gcry_mpi_gcd (t1, sk->e, phi))
448 { /* (while gcd is not 1) */
449 gcry_mpi_add_ui (sk->e, sk->e, 2);
450 }
451
452 /* calculate the secret key d = e^1 mod phi */
453 }
454 while ((0 == gcry_mpi_invm (sk->d, sk->e, f)) || 456 while ((0 == gcry_mpi_invm (sk->d, sk->e, f)) ||
455 (0 == gcry_mpi_invm (sk->u, sk->p, sk->q))); 457 (0 == gcry_mpi_invm (sk->u, sk->p, sk->q)));
456 458
457 gcry_mpi_release (t1); 459 gcry_mpi_release (t1);
458 gcry_mpi_release (t2); 460 gcry_mpi_release (t2);
@@ -471,13 +473,13 @@ struct KskRsaPrivateKeyBinaryEncoded
471 * Total size of the structure, in bytes, in big-endian! 473 * Total size of the structure, in bytes, in big-endian!
472 */ 474 */
473 uint16_t len GNUNET_PACKED; 475 uint16_t len GNUNET_PACKED;
474 uint16_t sizen GNUNET_PACKED; /* in big-endian! */ 476 uint16_t sizen GNUNET_PACKED; /* in big-endian! */
475 uint16_t sizee GNUNET_PACKED; /* in big-endian! */ 477 uint16_t sizee GNUNET_PACKED; /* in big-endian! */
476 uint16_t sized GNUNET_PACKED; /* in big-endian! */ 478 uint16_t sized GNUNET_PACKED; /* in big-endian! */
477 uint16_t sizep GNUNET_PACKED; /* in big-endian! */ 479 uint16_t sizep GNUNET_PACKED; /* in big-endian! */
478 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ 480 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */
479 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ 481 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */
480 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ 482 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */
481 /* followed by the actual values */ 483 /* followed by the actual values */
482}; 484};
483 485
@@ -499,16 +501,16 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc)
499 size_t size; 501 size_t size;
500 502
501 hx = *hc; 503 hx = *hc;
502 generate_kblock_key (&sk, 1024, /* at least 10x as fast than 2048 bits 504 generate_kblock_key (&sk, 1024, /* at least 10x as fast than 2048 bits
503 * -- we simply cannot afford 2048 bits 505 * -- we simply cannot afford 2048 bits
504 * even on modern hardware, and especially 506 * even on modern hardware, and especially
505 * not since clearly a dictionary attack 507 * not since clearly a dictionary attack
506 * will still be much cheaper 508 * will still be much cheaper
507 * than breaking a 1024 bit RSA key. 509 * than breaking a 1024 bit RSA key.
508 * If an adversary can spend the time to 510 * If an adversary can spend the time to
509 * break a 1024 bit RSA key just to forge 511 * break a 1024 bit RSA key just to forge
510 * a signature -- SO BE IT. [ CG, 6/2005 ] */ 512 * a signature -- SO BE IT. [ CG, 6/2005 ] */
511 &hx); 513 &hx);
512 pkv[0] = &sk.n; 514 pkv[0] = &sk.n;
513 pkv[1] = &sk.e; 515 pkv[1] = &sk.e;
514 pkv[2] = &sk.d; 516 pkv[2] = &sk.d;
@@ -517,10 +519,10 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc)
517 pkv[5] = &sk.u; 519 pkv[5] = &sk.u;
518 size = sizeof (struct KskRsaPrivateKeyBinaryEncoded); 520 size = sizeof (struct KskRsaPrivateKeyBinaryEncoded);
519 for (i = 0; i < 6; i++) 521 for (i = 0; i < 6; i++)
520 { 522 {
521 gcry_mpi_aprint (GCRYMPI_FMT_STD, &pbu[i], &sizes[i], *pkv[i]); 523 gcry_mpi_aprint (GCRYMPI_FMT_STD, &pbu[i], &sizes[i], *pkv[i]);
522 size += sizes[i]; 524 size += sizes[i];
523 } 525 }
524 GNUNET_assert (size < 65536); 526 GNUNET_assert (size < 65536);
525 retval = GNUNET_malloc (size); 527 retval = GNUNET_malloc (size);
526 retval->len = htons (size); 528 retval->len = htons (size);
@@ -545,10 +547,10 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc)
545 retval->sizedmq1 = htons (0); 547 retval->sizedmq1 = htons (0);
546 memcpy (&((char *) &retval[1])[i], pbu[5], sizes[5]); 548 memcpy (&((char *) &retval[1])[i], pbu[5], sizes[5]);
547 for (i = 0; i < 6; i++) 549 for (i = 0; i < 6; i++)
548 { 550 {
549 gcry_mpi_release (*pkv[i]); 551 gcry_mpi_release (*pkv[i]);
550 free (pbu[i]); 552 free (pbu[i]);
551 } 553 }
552 return retval; 554 return retval;
553} 555}
554 556
@@ -570,122 +572,123 @@ ksk_decode_key (const struct KskRsaPrivateKeyBinaryEncoded *encoding)
570 pos = 0; 572 pos = 0;
571 size = ntohs (encoding->sizen); 573 size = ntohs (encoding->sizen);
572 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, 574 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG,
573 &((const unsigned char *) (&encoding[1]))[pos], size, 575 &((const unsigned char *) (&encoding[1]))[pos], size,
574 &size); 576 &size);
575 pos += ntohs (encoding->sizen); 577 pos += ntohs (encoding->sizen);
576 if (rc) 578 if (rc)
577 { 579 {
578 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 580 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
579 return NULL; 581 return NULL;
580 } 582 }
581 size = ntohs (encoding->sizee); 583 size = ntohs (encoding->sizee);
582 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, 584 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG,
583 &((const unsigned char *) (&encoding[1]))[pos], size, 585 &((const unsigned char *) (&encoding[1]))[pos], size,
584 &size); 586 &size);
585 pos += ntohs (encoding->sizee); 587 pos += ntohs (encoding->sizee);
586 if (rc) 588 if (rc)
587 { 589 {
588 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 590 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
589 gcry_mpi_release (n); 591 gcry_mpi_release (n);
590 return NULL; 592 return NULL;
591 } 593 }
592 size = ntohs (encoding->sized); 594 size = ntohs (encoding->sized);
593 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG, 595 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG,
594 &((const unsigned char *) (&encoding[1]))[pos], size, 596 &((const unsigned char *) (&encoding[1]))[pos], size,
595 &size); 597 &size);
596 pos += ntohs (encoding->sized); 598 pos += ntohs (encoding->sized);
597 if (rc) 599 if (rc)
598 {
599 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
600 gcry_mpi_release (n);
601 gcry_mpi_release (e);
602 return NULL;
603 }
604 /* swap p and q! */
605 size = ntohs (encoding->sizep);
606 if (size > 0)
607 {
608 rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG,
609 &((const unsigned char *) (&encoding[1]))[pos], size,
610 &size);
611 pos += ntohs (encoding->sizep);
612 if (rc)
613 { 600 {
614 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 601 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
615 gcry_mpi_release (n); 602 gcry_mpi_release (n);
616 gcry_mpi_release (e); 603 gcry_mpi_release (e);
617 gcry_mpi_release (d);
618 return NULL; 604 return NULL;
619 } 605 }
620 } 606 /* swap p and q! */
607 size = ntohs (encoding->sizep);
608 if (size > 0)
609 {
610 rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG,
611 &((const unsigned char *) (&encoding[1]))[pos],
612 size, &size);
613 pos += ntohs (encoding->sizep);
614 if (rc)
615 {
616 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
617 gcry_mpi_release (n);
618 gcry_mpi_release (e);
619 gcry_mpi_release (d);
620 return NULL;
621 }
622 }
621 else 623 else
622 q = NULL; 624 q = NULL;
623 size = ntohs (encoding->sizeq); 625 size = ntohs (encoding->sizeq);
624 if (size > 0) 626 if (size > 0)
625 {
626 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG,
627 &((const unsigned char *) (&encoding[1]))[pos], size,
628 &size);
629 pos += ntohs (encoding->sizeq);
630 if (rc)
631 { 627 {
632 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 628 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG,
633 gcry_mpi_release (n); 629 &((const unsigned char *) (&encoding[1]))[pos],
634 gcry_mpi_release (e); 630 size, &size);
635 gcry_mpi_release (d); 631 pos += ntohs (encoding->sizeq);
636 if (q != NULL) 632 if (rc)
637 gcry_mpi_release (q); 633 {
638 return NULL; 634 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
635 gcry_mpi_release (n);
636 gcry_mpi_release (e);
637 gcry_mpi_release (d);
638 if (q != NULL)
639 gcry_mpi_release (q);
640 return NULL;
641 }
639 } 642 }
640 }
641 else 643 else
642 p = NULL; 644 p = NULL;
643 pos += ntohs (encoding->sizedmp1); 645 pos += ntohs (encoding->sizedmp1);
644 pos += ntohs (encoding->sizedmq1); 646 pos += ntohs (encoding->sizedmq1);
645 size = 647 size =
646 ntohs (encoding->len) - sizeof (struct KskRsaPrivateKeyBinaryEncoded) - 648 ntohs (encoding->len) - sizeof (struct KskRsaPrivateKeyBinaryEncoded) -
647 pos; 649 pos;
648 if (size > 0) 650 if (size > 0)
649 {
650 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG,
651 &((const unsigned char *) (&encoding[1]))[pos], size,
652 &size);
653 if (rc)
654 { 651 {
655 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 652 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG,
656 gcry_mpi_release (n); 653 &((const unsigned char *) (&encoding[1]))[pos],
657 gcry_mpi_release (e); 654 size, &size);
658 gcry_mpi_release (d); 655 if (rc)
659 if (p != NULL) 656 {
660 gcry_mpi_release (p); 657 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
661 if (q != NULL) 658 gcry_mpi_release (n);
662 gcry_mpi_release (q); 659 gcry_mpi_release (e);
663 return NULL; 660 gcry_mpi_release (d);
661 if (p != NULL)
662 gcry_mpi_release (p);
663 if (q != NULL)
664 gcry_mpi_release (q);
665 return NULL;
666 }
664 } 667 }
665 }
666 else 668 else
667 u = NULL; 669 u = NULL;
668 670
669 if ((p != NULL) && (q != NULL) && (u != NULL)) 671 if ((p != NULL) && (q != NULL) && (u != NULL))
670 {
671 rc = gcry_sexp_build (&res, &size, /* erroff */
672 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))",
673 n, e, d, p, q, u);
674 }
675 else
676 {
677 if ((p != NULL) && (q != NULL))
678 { 672 {
679 rc = gcry_sexp_build (&res, &size, /* erroff */ 673 rc = gcry_sexp_build (&res, &size, /* erroff */
680 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))", 674 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))",
681 n, e, d, p, q); 675 n, e, d, p, q, u);
682 } 676 }
683 else 677 else
684 { 678 {
685 rc = gcry_sexp_build (&res, &size, /* erroff */ 679 if ((p != NULL) && (q != NULL))
686 "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, d); 680 {
681 rc = gcry_sexp_build (&res, &size, /* erroff */
682 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))",
683 n, e, d, p, q);
684 }
685 else
686 {
687 rc = gcry_sexp_build (&res, &size, /* erroff */
688 "(private-key(rsa(n %m)(e %m)(d %m)))", n, e,
689 d);
690 }
687 } 691 }
688 }
689 gcry_mpi_release (n); 692 gcry_mpi_release (n);
690 gcry_mpi_release (e); 693 gcry_mpi_release (e);
691 gcry_mpi_release (d); 694 gcry_mpi_release (d);
@@ -700,10 +703,10 @@ ksk_decode_key (const struct KskRsaPrivateKeyBinaryEncoded *encoding)
700 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 703 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
701#if EXTRA_CHECKS 704#if EXTRA_CHECKS
702 if (gcry_pk_testkey (res)) 705 if (gcry_pk_testkey (res))
703 { 706 {
704 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 707 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
705 return NULL; 708 return NULL;
706 } 709 }
707#endif 710#endif
708 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); 711 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
709 ret->sexp = res; 712 ret->sexp = res;
@@ -733,13 +736,13 @@ GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc)
733 unsigned int i; 736 unsigned int i;
734 737
735 for (i = 0; i < cacheSize; i++) 738 for (i = 0; i < cacheSize; i++)
736 {
737 if (0 == memcmp (hc, &cache[i]->hc, sizeof (GNUNET_HashCode)))
738 { 739 {
739 ret = ksk_decode_key (cache[i]->pke); 740 if (0 == memcmp (hc, &cache[i]->hc, sizeof (GNUNET_HashCode)))
740 return ret; 741 {
742 ret = ksk_decode_key (cache[i]->pke);
743 return ret;
744 }
741 } 745 }
742 }
743 746
744 line = GNUNET_malloc (sizeof (struct KBlockKeyCacheLine)); 747 line = GNUNET_malloc (sizeof (struct KBlockKeyCacheLine));
745 line->hc = *hc; 748 line->hc = *hc;
@@ -755,10 +758,10 @@ void __attribute__ ((destructor)) GNUNET_CRYPTO_ksk_fini ()
755 unsigned int i; 758 unsigned int i;
756 759
757 for (i = 0; i < cacheSize; i++) 760 for (i = 0; i < cacheSize; i++)
758 { 761 {
759 GNUNET_free (cache[i]->pke); 762 GNUNET_free (cache[i]->pke);
760 GNUNET_free (cache[i]); 763 GNUNET_free (cache[i]);
761 } 764 }
762 GNUNET_array_grow (cache, cacheSize, 0); 765 GNUNET_array_grow (cache, cacheSize, 0);
763} 766}
764 767
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index 445f99a62..7fb2d1428 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -30,6 +30,10 @@
30#include "gnunet_os_lib.h" 30#include "gnunet_os_lib.h"
31#include <gcrypt.h> 31#include <gcrypt.h>
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
35#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
36
33/** 37/**
34 * Create a cryptographically weak pseudo-random number in the interval of 0 to 1. 38 * Create a cryptographically weak pseudo-random number in the interval of 0 to 1.
35 * 39 *
@@ -61,37 +65,37 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
61 GNUNET_assert (i > 0); 65 GNUNET_assert (i > 0);
62 66
63 switch (mode) 67 switch (mode)
64 { 68 {
65 case GNUNET_CRYPTO_QUALITY_STRONG: 69 case GNUNET_CRYPTO_QUALITY_STRONG:
66 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ 70 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
67#ifdef gcry_fast_random_poll 71#ifdef gcry_fast_random_poll
68 if ((invokeCount++ % 256) == 0) 72 if ((invokeCount++ % 256) == 0)
69 gcry_fast_random_poll (); 73 gcry_fast_random_poll ();
70#endif 74#endif
71 ul = UINT32_MAX - (UINT32_MAX % i); 75 ul = UINT32_MAX - (UINT32_MAX % i);
72 do 76 do
73 { 77 {
74 gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t), 78 gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t),
75 GCRY_STRONG_RANDOM); 79 GCRY_STRONG_RANDOM);
80 }
81 while (ret >= ul);
82 return ret % i;
83 case GNUNET_CRYPTO_QUALITY_NONCE:
84 ul = UINT32_MAX - (UINT32_MAX % i);
85 do
86 {
87 gcry_create_nonce (&ret, sizeof (ret));
88 }
89 while (ret >= ul);
90 return ret % i;
91 case GNUNET_CRYPTO_QUALITY_WEAK:
92 ret = i * weak_random ();
93 if (ret >= i)
94 ret = i - 1;
95 return ret;
96 default:
97 GNUNET_assert (0);
76 } 98 }
77 while (ret >= ul);
78 return ret % i;
79 case GNUNET_CRYPTO_QUALITY_NONCE:
80 ul = UINT32_MAX - (UINT32_MAX % i);
81 do
82 {
83 gcry_create_nonce (&ret, sizeof (ret));
84 }
85 while (ret >= ul);
86 return ret % i;
87 case GNUNET_CRYPTO_QUALITY_WEAK:
88 ret = i * weak_random ();
89 if (ret >= i)
90 ret = i - 1;
91 return ret;
92 default:
93 GNUNET_assert (0);
94 }
95 return 0; 99 return 0;
96} 100}
97 101
@@ -117,12 +121,12 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
117 for (i = 0; i < n; i++) 121 for (i = 0; i < n; i++)
118 ret[i] = i; 122 ret[i] = i;
119 for (i = n - 1; i > 0; i--) 123 for (i = n - 1; i > 0; i--)
120 { 124 {
121 x = GNUNET_CRYPTO_random_u32 (mode, i + 1); 125 x = GNUNET_CRYPTO_random_u32 (mode, i + 1);
122 tmp = ret[x]; 126 tmp = ret[x];
123 ret[x] = ret[i]; 127 ret[x] = ret[i];
124 ret[i] = tmp; 128 ret[i] = tmp;
125 } 129 }
126 return ret; 130 return ret;
127} 131}
128 132
@@ -142,33 +146,33 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
142 146
143 GNUNET_assert (max > 0); 147 GNUNET_assert (max > 0);
144 switch (mode) 148 switch (mode)
145 {
146 case GNUNET_CRYPTO_QUALITY_STRONG:
147 ul = UINT64_MAX - (UINT64_MAX % max);
148 do
149 {
150 gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t),
151 GCRY_STRONG_RANDOM);
152 }
153 while (ret >= ul);
154 return ret % max;
155 case GNUNET_CRYPTO_QUALITY_NONCE:
156 ul = UINT64_MAX - (UINT64_MAX % max);
157 do
158 { 149 {
159 gcry_create_nonce (&ret, sizeof (ret)); 150 case GNUNET_CRYPTO_QUALITY_STRONG:
151 ul = UINT64_MAX - (UINT64_MAX % max);
152 do
153 {
154 gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t),
155 GCRY_STRONG_RANDOM);
156 }
157 while (ret >= ul);
158 return ret % max;
159 case GNUNET_CRYPTO_QUALITY_NONCE:
160 ul = UINT64_MAX - (UINT64_MAX % max);
161 do
162 {
163 gcry_create_nonce (&ret, sizeof (ret));
164 }
165 while (ret >= ul);
166
167 return ret % max;
168 case GNUNET_CRYPTO_QUALITY_WEAK:
169 ret = max * weak_random ();
170 if (ret >= max)
171 ret = max - 1;
172 return ret;
173 default:
174 GNUNET_assert (0);
160 } 175 }
161 while (ret >= ul);
162
163 return ret % max;
164 case GNUNET_CRYPTO_QUALITY_WEAK:
165 ret = max * weak_random ();
166 if (ret >= max)
167 ret = max - 1;
168 return ret;
169 default:
170 GNUNET_assert (0);
171 }
172 return 0; 176 return 0;
173} 177}
174 178
@@ -196,7 +200,7 @@ static struct GNUNET_OS_Process *genproc;
196 */ 200 */
197static void 201static void
198entropy_generator (void *cls, const char *what, int printchar, int current, 202entropy_generator (void *cls, const char *what, int printchar, int current,
199 int total) 203 int total)
200{ 204{
201 unsigned long code; 205 unsigned long code;
202 enum GNUNET_OS_ProcessStatusType type; 206 enum GNUNET_OS_ProcessStatusType type;
@@ -205,39 +209,39 @@ entropy_generator (void *cls, const char *what, int printchar, int current,
205 if (0 != strcmp (what, "need_entropy")) 209 if (0 != strcmp (what, "need_entropy"))
206 return; 210 return;
207 if (current == total) 211 if (current == total)
208 {
209 if (genproc != NULL)
210 { 212 {
213 if (genproc != NULL)
214 {
215 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
216 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill");
217 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
218 GNUNET_OS_process_close (genproc);
219 genproc = NULL;
220 }
221 return;
222 }
223 if (genproc != NULL)
224 {
225 ret = GNUNET_OS_process_status (genproc, &type, &code);
226 if (ret == GNUNET_NO)
227 return; /* still running */
228 if (ret == GNUNET_SYSERR)
229 {
230 GNUNET_break (0);
231 return;
232 }
211 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) 233 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); 234 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill");
213 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); 235 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
214 GNUNET_OS_process_close (genproc); 236 GNUNET_OS_process_close (genproc);
215 genproc = NULL; 237 genproc = NULL;
216 } 238 }
217 return; 239 LOG (GNUNET_ERROR_TYPE_INFO,
218 } 240 _("Starting `%s' process to generate entropy\n"), "find");
219 if (genproc != NULL)
220 {
221 ret = GNUNET_OS_process_status (genproc, &type, &code);
222 if (ret == GNUNET_NO)
223 return; /* still running */
224 if (ret == GNUNET_SYSERR)
225 {
226 GNUNET_break (0);
227 return;
228 }
229 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
230 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
231 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
232 GNUNET_OS_process_close (genproc);
233 genproc = NULL;
234 }
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 _("Starting `%s' process to generate entropy\n"), "find");
237 genproc = 241 genproc =
238 GNUNET_OS_start_process (NULL, NULL, "sh", "sh", "-c", 242 GNUNET_OS_start_process (NULL, NULL, "sh", "sh", "-c",
239 "exec find / -mount -type f -exec cp {} /dev/null \\; 2>/dev/null", 243 "exec find / -mount -type f -exec cp {} /dev/null \\; 2>/dev/null",
240 NULL); 244 NULL);
241} 245}
242 246
243 247
@@ -245,11 +249,11 @@ static void
245killfind () 249killfind ()
246{ 250{
247 if (genproc != NULL) 251 if (genproc != NULL)
248 { 252 {
249 GNUNET_OS_process_kill (genproc, SIGKILL); 253 GNUNET_OS_process_kill (genproc, SIGKILL);
250 GNUNET_OS_process_close (genproc); 254 GNUNET_OS_process_close (genproc);
251 genproc = NULL; 255 genproc = NULL;
252 } 256 }
253} 257}
254 258
255 259
@@ -257,20 +261,21 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
257{ 261{
258 gcry_control (GCRYCTL_DISABLE_SECMEM, 0); 262 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
259 if (!gcry_check_version (GCRYPT_VERSION)) 263 if (!gcry_check_version (GCRYPT_VERSION))
260 { 264 {
261 fprintf (stderr, 265 fprintf (stderr,
262 _ 266 _
263 ("libgcrypt has not the expected version (version %s is required).\n"), 267 ("libgcrypt has not the expected version (version %s is required).\n"),
264 GCRYPT_VERSION); 268 GCRYPT_VERSION);
265 abort (); 269 abort ();
266 } 270 }
267#ifdef gcry_fast_random_poll 271#ifdef gcry_fast_random_poll
268 gcry_fast_random_poll (); 272 gcry_fast_random_poll ();
269#endif 273#endif
270 gcry_set_progress_handler (&entropy_generator, NULL); 274 gcry_set_progress_handler (&entropy_generator, NULL);
271 atexit (&killfind); 275 atexit (&killfind);
272 SRANDOM (time (NULL) ^ 276 SRANDOM (time (NULL) ^
273 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX)); 277 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
278 UINT32_MAX));
274} 279}
275 280
276 281
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index 46778a842..7f0c81692 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -37,6 +37,12 @@
37#include "gnunet_crypto_lib.h" 37#include "gnunet_crypto_lib.h"
38#include "gnunet_disk_lib.h" 38#include "gnunet_disk_lib.h"
39 39
40#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
41
42#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
43
44#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
45
40/** 46/**
41 * The private information of an RSA key pair. 47 * The private information of an RSA key pair.
42 * NOTE: this must match the definition in crypto_ksk.c 48 * NOTE: this must match the definition in crypto_ksk.c
@@ -60,13 +66,13 @@ struct RsaPrivateKeyBinaryEncoded
60 * Total size of the structure, in bytes, in big-endian! 66 * Total size of the structure, in bytes, in big-endian!
61 */ 67 */
62 uint16_t len GNUNET_PACKED; 68 uint16_t len GNUNET_PACKED;
63 uint16_t sizen GNUNET_PACKED; /* in big-endian! */ 69 uint16_t sizen GNUNET_PACKED; /* in big-endian! */
64 uint16_t sizee GNUNET_PACKED; /* in big-endian! */ 70 uint16_t sizee GNUNET_PACKED; /* in big-endian! */
65 uint16_t sized GNUNET_PACKED; /* in big-endian! */ 71 uint16_t sized GNUNET_PACKED; /* in big-endian! */
66 uint16_t sizep GNUNET_PACKED; /* in big-endian! */ 72 uint16_t sizep GNUNET_PACKED; /* in big-endian! */
67 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ 73 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */
68 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ 74 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */
69 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ 75 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */
70 /* followed by the actual values */ 76 /* followed by the actual values */
71}; 77};
72 78
@@ -81,7 +87,7 @@ struct RsaPrivateKeyBinaryEncoded
81 * a failure of the command 'cmd' with the message given 87 * a failure of the command 'cmd' with the message given
82 * by gcry_strerror(rc). 88 * by gcry_strerror(rc).
83 */ 89 */
84#define LOG_GCRY(level, cmd, rc) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0); 90#define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0);
85 91
86/** 92/**
87 * If target != size, move target bytes to the 93 * If target != size, move target bytes to the
@@ -92,10 +98,10 @@ static void
92adjust (unsigned char *buf, size_t size, size_t target) 98adjust (unsigned char *buf, size_t size, size_t target)
93{ 99{
94 if (size < target) 100 if (size < target)
95 { 101 {
96 memmove (&buf[target - size], buf, size); 102 memmove (&buf[target - size], buf, size);
97 memset (buf, 0, target - size); 103 memset (buf, 0, target - size);
98 } 104 }
99} 105}
100 106
101/** 107/**
@@ -109,9 +115,9 @@ GNUNET_CRYPTO_rsa_key_create ()
109 gcry_sexp_t s_keyparam; 115 gcry_sexp_t s_keyparam;
110 116
111 GNUNET_assert (0 == 117 GNUNET_assert (0 ==
112 gcry_sexp_build (&s_keyparam, NULL, 118 gcry_sexp_build (&s_keyparam, NULL,
113 "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))", 119 "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))",
114 HOSTKEY_LEN)); 120 HOSTKEY_LEN));
115 GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam)); 121 GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam));
116 gcry_sexp_release (s_keyparam); 122 gcry_sexp_release (s_keyparam);
117#if EXTRA_CHECKS 123#if EXTRA_CHECKS
@@ -134,7 +140,7 @@ GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
134 140
135static int 141static int
136key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname, 142key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
137 const char *elems) 143 const char *elems)
138{ 144{
139 gcry_sexp_t list, l2; 145 gcry_sexp_t list, l2;
140 const char *s; 146 const char *s;
@@ -142,44 +148,44 @@ key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
142 148
143 list = gcry_sexp_find_token (sexp, topname, 0); 149 list = gcry_sexp_find_token (sexp, topname, 0);
144 if (!list) 150 if (!list)
145 { 151 {
146 return 1; 152 return 1;
147 } 153 }
148 l2 = gcry_sexp_cadr (list); 154 l2 = gcry_sexp_cadr (list);
149 gcry_sexp_release (list); 155 gcry_sexp_release (list);
150 list = l2; 156 list = l2;
151 if (!list) 157 if (!list)
152 { 158 {
153 return 2; 159 return 2;
154 } 160 }
155 161
156 idx = 0; 162 idx = 0;
157 for (s = elems; *s; s++, idx++) 163 for (s = elems; *s; s++, idx++)
158 {
159 l2 = gcry_sexp_find_token (list, s, 1);
160 if (!l2)
161 {
162 for (i = 0; i < idx; i++)
163 {
164 gcry_free (array[i]);
165 array[i] = NULL;
166 }
167 gcry_sexp_release (list);
168 return 3; /* required parameter not found */
169 }
170 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
171 gcry_sexp_release (l2);
172 if (!array[idx])
173 { 164 {
174 for (i = 0; i < idx; i++) 165 l2 = gcry_sexp_find_token (list, s, 1);
175 { 166 if (!l2)
176 gcry_free (array[i]); 167 {
177 array[i] = NULL; 168 for (i = 0; i < idx; i++)
178 } 169 {
179 gcry_sexp_release (list); 170 gcry_free (array[i]);
180 return 4; /* required parameter is invalid */ 171 array[i] = NULL;
172 }
173 gcry_sexp_release (list);
174 return 3; /* required parameter not found */
175 }
176 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
177 gcry_sexp_release (l2);
178 if (!array[idx])
179 {
180 for (i = 0; i < idx; i++)
181 {
182 gcry_free (array[i]);
183 array[i] = NULL;
184 }
185 gcry_sexp_release (list);
186 return 4; /* required parameter is invalid */
187 }
181 } 188 }
182 }
183 gcry_sexp_release (list); 189 gcry_sexp_release (list);
184 return 0; 190 return 0;
185} 191}
@@ -191,9 +197,10 @@ key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
191 */ 197 */
192void 198void
193GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey 199GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
194 *priv, 200 *priv,
195 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 201 struct
196 *pub) 202 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
203 *pub)
197{ 204{
198 gcry_mpi_t skey[2]; 205 gcry_mpi_t skey[2];
199 size_t size; 206 size_t size;
@@ -206,24 +213,25 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
206 rc = key_from_sexp (skey, priv->sexp, "rsa", "ne"); 213 rc = key_from_sexp (skey, priv->sexp, "rsa", "ne");
207 GNUNET_assert (0 == rc); 214 GNUNET_assert (0 == rc);
208 pub->len = 215 pub->len =
209 htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) - 216 htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) -
210 sizeof (pub->padding)); 217 sizeof (pub->padding));
211 pub->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); 218 pub->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
212 pub->padding = 0; 219 pub->padding = 0;
213 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; 220 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
214 GNUNET_assert (0 == 221 GNUNET_assert (0 ==
215 gcry_mpi_print (GCRYMPI_FMT_USG, &pub->key[0], size, &size, 222 gcry_mpi_print (GCRYMPI_FMT_USG, &pub->key[0], size, &size,
216 skey[0])); 223 skey[0]));
217 adjust (&pub->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); 224 adjust (&pub->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
218 size = GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; 225 size =
226 GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
219 GNUNET_assert (0 == 227 GNUNET_assert (0 ==
220 gcry_mpi_print (GCRYMPI_FMT_USG, 228 gcry_mpi_print (GCRYMPI_FMT_USG,
221 &pub->key 229 &pub->key
222 [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size, 230 [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH],
223 &size, skey[1])); 231 size, &size, skey[1]));
224 adjust (&pub->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size, 232 adjust (&pub->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size,
225 GNUNET_CRYPTO_RSA_KEY_LENGTH - 233 GNUNET_CRYPTO_RSA_KEY_LENGTH -
226 GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); 234 GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
227 gcry_mpi_release (skey[0]); 235 gcry_mpi_release (skey[0]);
228 gcry_mpi_release (skey[1]); 236 gcry_mpi_release (skey[1]);
229} 237}
@@ -237,7 +245,7 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
237 */ 245 */
238static struct GNUNET_CRYPTO_RsaPrivateKey * 246static struct GNUNET_CRYPTO_RsaPrivateKey *
239public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 247public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
240 *publicKey) 248 *publicKey)
241{ 249{
242 struct GNUNET_CRYPTO_RsaPrivateKey *ret; 250 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
243 gcry_sexp_t result; 251 gcry_sexp_t result;
@@ -251,36 +259,38 @@ public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
251 (ntohs (publicKey->len) != 259 (ntohs (publicKey->len) !=
252 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) - 260 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) -
253 sizeof (publicKey->padding))) 261 sizeof (publicKey->padding)))
254 { 262 {
255 GNUNET_break (0); 263 GNUNET_break (0);
256 return NULL; 264 return NULL;
257 } 265 }
258 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; 266 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
259 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, &publicKey->key[0], size, &size); 267 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, &publicKey->key[0], size, &size);
260 if (rc) 268 if (rc)
261 { 269 {
262 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 270 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
263 return NULL; 271 return NULL;
264 } 272 }
265 size = GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; 273 size =
266 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, 274 GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
267 &publicKey->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], 275 rc =
268 size, &size); 276 gcry_mpi_scan (&e, GCRYMPI_FMT_USG,
277 &publicKey->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH],
278 size, &size);
269 if (rc) 279 if (rc)
270 { 280 {
271 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 281 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
272 gcry_mpi_release (n); 282 gcry_mpi_release (n);
273 return NULL; 283 return NULL;
274 } 284 }
275 rc = gcry_sexp_build (&result, &erroff, "(public-key(rsa(n %m)(e %m)))", n, 285 rc = gcry_sexp_build (&result, &erroff, "(public-key(rsa(n %m)(e %m)))", n,
276 e); 286 e);
277 gcry_mpi_release (n); 287 gcry_mpi_release (n);
278 gcry_mpi_release (e); 288 gcry_mpi_release (e);
279 if (rc) 289 if (rc)
280 { 290 {
281 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */ 291 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */
282 return NULL; 292 return NULL;
283 } 293 }
284 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); 294 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
285 ret->sexp = result; 295 ret->sexp = result;
286 return ret; 296 return ret;
@@ -306,10 +316,10 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
306 316
307#if EXTRA_CHECKS 317#if EXTRA_CHECKS
308 if (gcry_pk_testkey (hostkey->sexp)) 318 if (gcry_pk_testkey (hostkey->sexp))
309 { 319 {
310 GNUNET_break (0); 320 GNUNET_break (0);
311 return NULL; 321 return NULL;
312 } 322 }
313#endif 323#endif
314 324
315 memset (pkv, 0, sizeof (gcry_mpi_t) * 6); 325 memset (pkv, 0, sizeof (gcry_mpi_t) * 6);
@@ -327,21 +337,21 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
327 GNUNET_assert (0 == rc); 337 GNUNET_assert (0 == rc);
328 size = sizeof (struct RsaPrivateKeyBinaryEncoded); 338 size = sizeof (struct RsaPrivateKeyBinaryEncoded);
329 for (i = 0; i < 6; i++) 339 for (i = 0; i < 6; i++)
330 {
331 if (pkv[i] != NULL)
332 { 340 {
333 GNUNET_assert (0 == 341 if (pkv[i] != NULL)
334 gcry_mpi_aprint (GCRYMPI_FMT_USG, 342 {
335 (unsigned char **) &pbu[i], &sizes[i], 343 GNUNET_assert (0 ==
336 pkv[i])); 344 gcry_mpi_aprint (GCRYMPI_FMT_USG,
337 size += sizes[i]; 345 (unsigned char **) &pbu[i],
346 &sizes[i], pkv[i]));
347 size += sizes[i];
348 }
349 else
350 {
351 pbu[i] = NULL;
352 sizes[i] = 0;
353 }
338 } 354 }
339 else
340 {
341 pbu[i] = NULL;
342 sizes[i] = 0;
343 }
344 }
345 GNUNET_assert (size < 65536); 355 GNUNET_assert (size < 65536);
346 retval = GNUNET_malloc (size); 356 retval = GNUNET_malloc (size);
347 retval->len = htons (size); 357 retval->len = htons (size);
@@ -366,12 +376,12 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
366 retval->sizedmq1 = htons (0); 376 retval->sizedmq1 = htons (0);
367 memcpy (&((char *) (&retval[1]))[i], pbu[5], sizes[5]); 377 memcpy (&((char *) (&retval[1]))[i], pbu[5], sizes[5]);
368 for (i = 0; i < 6; i++) 378 for (i = 0; i < 6; i++)
369 { 379 {
370 if (pkv[i] != NULL) 380 if (pkv[i] != NULL)
371 gcry_mpi_release (pkv[i]); 381 gcry_mpi_release (pkv[i]);
372 if (pbu[i] != NULL) 382 if (pbu[i] != NULL)
373 free (pbu[i]); 383 free (pbu[i]);
374 } 384 }
375 return retval; 385 return retval;
376} 386}
377 387
@@ -387,7 +397,7 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len)
387{ 397{
388 struct GNUNET_CRYPTO_RsaPrivateKey *ret; 398 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
389 const struct RsaPrivateKeyBinaryEncoded *encoding = 399 const struct RsaPrivateKeyBinaryEncoded *encoding =
390 (const struct RsaPrivateKeyBinaryEncoded *) buf; 400 (const struct RsaPrivateKeyBinaryEncoded *) buf;
391 gcry_sexp_t res; 401 gcry_sexp_t res;
392 gcry_mpi_t n, e, d, p, q, u; 402 gcry_mpi_t n, e, d, p, q, u;
393 int rc; 403 int rc;
@@ -402,121 +412,122 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len)
402 pos = 0; 412 pos = 0;
403 size = ntohs (encoding->sizen); 413 size = ntohs (encoding->sizen);
404 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, 414 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG,
405 &((const unsigned char *) (&encoding[1]))[pos], size, 415 &((const unsigned char *) (&encoding[1]))[pos], size,
406 &size); 416 &size);
407 pos += ntohs (encoding->sizen); 417 pos += ntohs (encoding->sizen);
408 if (rc) 418 if (rc)
409 { 419 {
410 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 420 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
411 return NULL; 421 return NULL;
412 } 422 }
413 size = ntohs (encoding->sizee); 423 size = ntohs (encoding->sizee);
414 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, 424 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG,
415 &((const unsigned char *) (&encoding[1]))[pos], size, 425 &((const unsigned char *) (&encoding[1]))[pos], size,
416 &size); 426 &size);
417 pos += ntohs (encoding->sizee); 427 pos += ntohs (encoding->sizee);
418 if (rc) 428 if (rc)
419 { 429 {
420 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 430 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
421 gcry_mpi_release (n); 431 gcry_mpi_release (n);
422 return NULL; 432 return NULL;
423 } 433 }
424 size = ntohs (encoding->sized); 434 size = ntohs (encoding->sized);
425 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG, 435 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG,
426 &((const unsigned char *) (&encoding[1]))[pos], size, 436 &((const unsigned char *) (&encoding[1]))[pos], size,
427 &size); 437 &size);
428 pos += ntohs (encoding->sized); 438 pos += ntohs (encoding->sized);
429 if (rc) 439 if (rc)
430 {
431 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
432 gcry_mpi_release (n);
433 gcry_mpi_release (e);
434 return NULL;
435 }
436 /* swap p and q! */
437 size = ntohs (encoding->sizep);
438 if (size > 0)
439 {
440 rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG,
441 &((const unsigned char *) (&encoding[1]))[pos], size,
442 &size);
443 pos += ntohs (encoding->sizep);
444 if (rc)
445 { 440 {
446 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 441 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
447 gcry_mpi_release (n); 442 gcry_mpi_release (n);
448 gcry_mpi_release (e); 443 gcry_mpi_release (e);
449 gcry_mpi_release (d);
450 return NULL; 444 return NULL;
451 } 445 }
452 } 446 /* swap p and q! */
447 size = ntohs (encoding->sizep);
448 if (size > 0)
449 {
450 rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG,
451 &((const unsigned char *) (&encoding[1]))[pos],
452 size, &size);
453 pos += ntohs (encoding->sizep);
454 if (rc)
455 {
456 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
457 gcry_mpi_release (n);
458 gcry_mpi_release (e);
459 gcry_mpi_release (d);
460 return NULL;
461 }
462 }
453 else 463 else
454 q = NULL; 464 q = NULL;
455 size = ntohs (encoding->sizeq); 465 size = ntohs (encoding->sizeq);
456 if (size > 0) 466 if (size > 0)
457 {
458 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG,
459 &((const unsigned char *) (&encoding[1]))[pos], size,
460 &size);
461 pos += ntohs (encoding->sizeq);
462 if (rc)
463 { 467 {
464 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 468 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG,
465 gcry_mpi_release (n); 469 &((const unsigned char *) (&encoding[1]))[pos],
466 gcry_mpi_release (e); 470 size, &size);
467 gcry_mpi_release (d); 471 pos += ntohs (encoding->sizeq);
468 if (q != NULL) 472 if (rc)
469 gcry_mpi_release (q); 473 {
470 return NULL; 474 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
475 gcry_mpi_release (n);
476 gcry_mpi_release (e);
477 gcry_mpi_release (d);
478 if (q != NULL)
479 gcry_mpi_release (q);
480 return NULL;
481 }
471 } 482 }
472 }
473 else 483 else
474 p = NULL; 484 p = NULL;
475 pos += ntohs (encoding->sizedmp1); 485 pos += ntohs (encoding->sizedmp1);
476 pos += ntohs (encoding->sizedmq1); 486 pos += ntohs (encoding->sizedmq1);
477 size = 487 size =
478 ntohs (encoding->len) - sizeof (struct RsaPrivateKeyBinaryEncoded) - pos; 488 ntohs (encoding->len) - sizeof (struct RsaPrivateKeyBinaryEncoded) - pos;
479 if (size > 0) 489 if (size > 0)
480 {
481 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG,
482 &((const unsigned char *) (&encoding[1]))[pos], size,
483 &size);
484 if (rc)
485 { 490 {
486 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 491 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG,
487 gcry_mpi_release (n); 492 &((const unsigned char *) (&encoding[1]))[pos],
488 gcry_mpi_release (e); 493 size, &size);
489 gcry_mpi_release (d); 494 if (rc)
490 if (p != NULL) 495 {
491 gcry_mpi_release (p); 496 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
492 if (q != NULL) 497 gcry_mpi_release (n);
493 gcry_mpi_release (q); 498 gcry_mpi_release (e);
494 return NULL; 499 gcry_mpi_release (d);
500 if (p != NULL)
501 gcry_mpi_release (p);
502 if (q != NULL)
503 gcry_mpi_release (q);
504 return NULL;
505 }
495 } 506 }
496 }
497 else 507 else
498 u = NULL; 508 u = NULL;
499 509
500 if ((p != NULL) && (q != NULL) && (u != NULL)) 510 if ((p != NULL) && (q != NULL) && (u != NULL))
501 {
502 rc = gcry_sexp_build (&res, &size, /* erroff */
503 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))",
504 n, e, d, p, q, u);
505 }
506 else
507 {
508 if ((p != NULL) && (q != NULL))
509 { 511 {
510 rc = gcry_sexp_build (&res, &size, /* erroff */ 512 rc = gcry_sexp_build (&res, &size, /* erroff */
511 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))", 513 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))",
512 n, e, d, p, q); 514 n, e, d, p, q, u);
513 } 515 }
514 else 516 else
515 { 517 {
516 rc = gcry_sexp_build (&res, &size, /* erroff */ 518 if ((p != NULL) && (q != NULL))
517 "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, d); 519 {
520 rc = gcry_sexp_build (&res, &size, /* erroff */
521 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))",
522 n, e, d, p, q);
523 }
524 else
525 {
526 rc = gcry_sexp_build (&res, &size, /* erroff */
527 "(private-key(rsa(n %m)(e %m)(d %m)))", n, e,
528 d);
529 }
518 } 530 }
519 }
520 gcry_mpi_release (n); 531 gcry_mpi_release (n);
521 gcry_mpi_release (e); 532 gcry_mpi_release (e);
522 gcry_mpi_release (d); 533 gcry_mpi_release (d);
@@ -531,10 +542,10 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len)
531 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 542 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
532#if EXTRA_CHECKS 543#if EXTRA_CHECKS
533 if (gcry_pk_testkey (res)) 544 if (gcry_pk_testkey (res))
534 { 545 {
535 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 546 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
536 return NULL; 547 return NULL;
537 } 548 }
538#endif 549#endif
539 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); 550 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
540 ret->sexp = res; 551 ret->sexp = res;
@@ -571,167 +582,172 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
571 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename)) 582 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
572 return NULL; 583 return NULL;
573 while (GNUNET_YES != GNUNET_DISK_file_test (filename)) 584 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
574 {
575 fd = GNUNET_DISK_file_open (filename,
576 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
577 | GNUNET_DISK_OPEN_FAILIFEXISTS,
578 GNUNET_DISK_PERM_USER_READ |
579 GNUNET_DISK_PERM_USER_WRITE);
580 if (NULL == fd)
581 {
582 if (errno == EEXIST)
583 {
584 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
585 {
586 /* must exist but not be accessible, fail for good! */
587 if (0 != ACCESS (filename, R_OK))
588 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access",
589 filename);
590 else
591 GNUNET_break (0); /* what is going on!? */
592 return NULL;
593 }
594 continue;
595 }
596 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
597 return NULL;
598 }
599 cnt = 0;
600
601 while (GNUNET_YES !=
602 GNUNET_DISK_file_lock (fd, 0,
603 sizeof (struct RsaPrivateKeyBinaryEncoded),
604 GNUNET_YES))
605 { 585 {
606 sleep (1); 586 fd = GNUNET_DISK_file_open (filename,
607 if (0 == ++cnt % 10) 587 GNUNET_DISK_OPEN_WRITE |
608 { 588 GNUNET_DISK_OPEN_CREATE |
609 ec = errno; 589 GNUNET_DISK_OPEN_FAILIFEXISTS,
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 590 GNUNET_DISK_PERM_USER_READ |
611 _("Could not aquire lock on file `%s': %s...\n"), filename, 591 GNUNET_DISK_PERM_USER_WRITE);
612 STRERROR (ec)); 592 if (NULL == fd)
613 } 593 {
594 if (errno == EEXIST)
595 {
596 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
597 {
598 /* must exist but not be accessible, fail for good! */
599 if (0 != ACCESS (filename, R_OK))
600 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access",
601 filename);
602 else
603 GNUNET_break (0); /* what is going on!? */
604 return NULL;
605 }
606 continue;
607 }
608 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
609 return NULL;
610 }
611 cnt = 0;
612
613 while (GNUNET_YES !=
614 GNUNET_DISK_file_lock (fd, 0,
615 sizeof (struct
616 RsaPrivateKeyBinaryEncoded),
617 GNUNET_YES))
618 {
619 sleep (1);
620 if (0 == ++cnt % 10)
621 {
622 ec = errno;
623 LOG (GNUNET_ERROR_TYPE_ERROR,
624 _("Could not aquire lock on file `%s': %s...\n"), filename,
625 STRERROR (ec));
626 }
627 }
628 LOG (GNUNET_ERROR_TYPE_INFO,
629 _("Creating a new private key. This may take a while.\n"));
630 ret = GNUNET_CRYPTO_rsa_key_create ();
631 GNUNET_assert (ret != NULL);
632 enc = rsa_encode_key (ret);
633 GNUNET_assert (enc != NULL);
634 GNUNET_assert (ntohs (enc->len) ==
635 GNUNET_DISK_file_write (fd, enc, ntohs (enc->len)));
636 GNUNET_free (enc);
637
638 GNUNET_DISK_file_sync (fd);
639 if (GNUNET_YES !=
640 GNUNET_DISK_file_unlock (fd, 0,
641 sizeof (struct
642 RsaPrivateKeyBinaryEncoded)))
643 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
644 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
645 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub);
646 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
647 LOG (GNUNET_ERROR_TYPE_INFO,
648 _("I am host `%s'. Stored new private key in `%s'.\n"),
649 GNUNET_i2s (&pid), filename);
650 return ret;
614 } 651 }
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
616 _("Creating a new private key. This may take a while.\n"));
617 ret = GNUNET_CRYPTO_rsa_key_create ();
618 GNUNET_assert (ret != NULL);
619 enc = rsa_encode_key (ret);
620 GNUNET_assert (enc != NULL);
621 GNUNET_assert (ntohs (enc->len) ==
622 GNUNET_DISK_file_write (fd, enc, ntohs (enc->len)));
623 GNUNET_free (enc);
624
625 GNUNET_DISK_file_sync (fd);
626 if (GNUNET_YES !=
627 GNUNET_DISK_file_unlock (fd, 0,
628 sizeof (struct RsaPrivateKeyBinaryEncoded)))
629 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
630 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
631 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub);
632 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
633 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
634 _("I am host `%s'. Stored new private key in `%s'.\n"),
635 GNUNET_i2s (&pid), filename);
636 return ret;
637 }
638 /* hostkey file exists already, read it! */ 652 /* hostkey file exists already, read it! */
639 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 653 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
640 GNUNET_DISK_PERM_NONE); 654 GNUNET_DISK_PERM_NONE);
641 if (NULL == fd) 655 if (NULL == fd)
642 {
643 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
644 return NULL;
645 }
646 cnt = 0;
647 while (1)
648 {
649 if (GNUNET_YES !=
650 GNUNET_DISK_file_lock (fd, 0,
651 sizeof (struct RsaPrivateKeyBinaryEncoded),
652 GNUNET_NO))
653 { 656 {
654 if (0 == ++cnt % 60) 657 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
655 {
656 ec = errno;
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
658 _("Could not aquire lock on file `%s': %s...\n"), filename,
659 STRERROR (ec));
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
661 _
662 ("This may be ok if someone is currently generating a hostkey.\n"));
663 }
664 sleep (1);
665 continue;
666 }
667 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
668 {
669 /* eh, what!? File we opened is now gone!? */
670 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
671 if (GNUNET_YES !=
672 GNUNET_DISK_file_unlock (fd, 0,
673 sizeof (struct RsaPrivateKeyBinaryEncoded)))
674 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
675 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
676
677 return NULL; 658 return NULL;
678 } 659 }
679 if (GNUNET_YES != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES)) 660 cnt = 0;
680 fs = 0; 661 while (1)
681 if (fs < sizeof (struct RsaPrivateKeyBinaryEncoded))
682 { 662 {
683 /* maybe we got the read lock before the hostkey generating
684 * process had a chance to get the write lock; give it up! */
685 if (GNUNET_YES != 663 if (GNUNET_YES !=
686 GNUNET_DISK_file_unlock (fd, 0, 664 GNUNET_DISK_file_lock (fd, 0,
687 sizeof (struct RsaPrivateKeyBinaryEncoded))) 665 sizeof (struct RsaPrivateKeyBinaryEncoded),
688 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 666 GNUNET_NO))
689 if (0 == ++cnt % 10) 667 {
690 { 668 if (0 == ++cnt % 60)
691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 669 {
692 _ 670 ec = errno;
693 ("When trying to read hostkey file `%s' I found %u bytes but I need at least %u.\n"), 671 LOG (GNUNET_ERROR_TYPE_ERROR,
694 filename, (unsigned int) fs, 672 _("Could not aquire lock on file `%s': %s...\n"), filename,
695 (unsigned int) sizeof (struct RsaPrivateKeyBinaryEncoded)); 673 STRERROR (ec));
696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 674 LOG (GNUNET_ERROR_TYPE_ERROR,
697 _ 675 _
698 ("This may be ok if someone is currently generating a hostkey.\n")); 676 ("This may be ok if someone is currently generating a hostkey.\n"));
699 } 677 }
700 sleep (2); /* wait a bit longer! */ 678 sleep (1);
701 continue; 679 continue;
680 }
681 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
682 {
683 /* eh, what!? File we opened is now gone!? */
684 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
685 if (GNUNET_YES !=
686 GNUNET_DISK_file_unlock (fd, 0,
687 sizeof (struct
688 RsaPrivateKeyBinaryEncoded)))
689 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
690 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
691
692 return NULL;
693 }
694 if (GNUNET_YES != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES))
695 fs = 0;
696 if (fs < sizeof (struct RsaPrivateKeyBinaryEncoded))
697 {
698 /* maybe we got the read lock before the hostkey generating
699 * process had a chance to get the write lock; give it up! */
700 if (GNUNET_YES !=
701 GNUNET_DISK_file_unlock (fd, 0,
702 sizeof (struct
703 RsaPrivateKeyBinaryEncoded)))
704 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
705 if (0 == ++cnt % 10)
706 {
707 LOG (GNUNET_ERROR_TYPE_ERROR,
708 _
709 ("When trying to read hostkey file `%s' I found %u bytes but I need at least %u.\n"),
710 filename, (unsigned int) fs,
711 (unsigned int) sizeof (struct RsaPrivateKeyBinaryEncoded));
712 LOG (GNUNET_ERROR_TYPE_ERROR,
713 _
714 ("This may be ok if someone is currently generating a hostkey.\n"));
715 }
716 sleep (2); /* wait a bit longer! */
717 continue;
718 }
719 break;
702 } 720 }
703 break;
704 }
705 enc = GNUNET_malloc (fs); 721 enc = GNUNET_malloc (fs);
706 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs)); 722 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs));
707 len = ntohs (enc->len); 723 len = ntohs (enc->len);
708 ret = NULL; 724 ret = NULL;
709 if ((len != fs) || 725 if ((len != fs) ||
710 (NULL == (ret = GNUNET_CRYPTO_rsa_decode_key ((char *) enc, len)))) 726 (NULL == (ret = GNUNET_CRYPTO_rsa_decode_key ((char *) enc, len))))
711 {
712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
713 _
714 ("File `%s' does not contain a valid private key. Deleting it.\n"),
715 filename);
716 if (0 != UNLINK (filename))
717 { 727 {
718 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename); 728 LOG (GNUNET_ERROR_TYPE_ERROR,
729 _
730 ("File `%s' does not contain a valid private key. Deleting it.\n"),
731 filename);
732 if (0 != UNLINK (filename))
733 {
734 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
735 }
719 } 736 }
720 }
721 GNUNET_free (enc); 737 GNUNET_free (enc);
722 if (GNUNET_YES != 738 if (GNUNET_YES !=
723 GNUNET_DISK_file_unlock (fd, 0, 739 GNUNET_DISK_file_unlock (fd, 0,
724 sizeof (struct RsaPrivateKeyBinaryEncoded))) 740 sizeof (struct RsaPrivateKeyBinaryEncoded)))
725 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 741 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
726 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); 742 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
727 if (ret != NULL) 743 if (ret != NULL)
728 { 744 {
729 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub); 745 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub);
730 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey); 746 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
731 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 747 LOG (GNUNET_ERROR_TYPE_INFO,
732 _("I am host `%s'. Read private key from `%s'.\n"), 748 _("I am host `%s'. Read private key from `%s'.\n"),
733 GNUNET_i2s (&pid), filename); 749 GNUNET_i2s (&pid), filename);
734 } 750 }
735 return ret; 751 return ret;
736} 752}
737 753
@@ -748,9 +764,9 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
748 */ 764 */
749int 765int
750GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size, 766GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
751 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 767 const struct
752 *publicKey, 768 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey,
753 struct GNUNET_CRYPTO_RsaEncryptedData *target) 769 struct GNUNET_CRYPTO_RsaEncryptedData *target)
754{ 770{
755 gcry_sexp_t result; 771 gcry_sexp_t result;
756 gcry_sexp_t data; 772 gcry_sexp_t data;
@@ -766,10 +782,10 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
766 return GNUNET_SYSERR; 782 return GNUNET_SYSERR;
767 isize = size; 783 isize = size;
768 GNUNET_assert (0 == 784 GNUNET_assert (0 ==
769 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize)); 785 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize));
770 GNUNET_assert (0 == 786 GNUNET_assert (0 ==
771 gcry_sexp_build (&data, &erroff, 787 gcry_sexp_build (&data, &erroff,
772 "(data (flags pkcs1)(value %m))", val)); 788 "(data (flags pkcs1)(value %m))", val));
773 gcry_mpi_release (val); 789 gcry_mpi_release (val);
774 GNUNET_assert (0 == gcry_pk_encrypt (&result, data, pubkey->sexp)); 790 GNUNET_assert (0 == gcry_pk_encrypt (&result, data, pubkey->sexp));
775 gcry_sexp_release (data); 791 gcry_sexp_release (data);
@@ -779,11 +795,11 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
779 gcry_sexp_release (result); 795 gcry_sexp_release (result);
780 isize = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData); 796 isize = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData);
781 GNUNET_assert (0 == 797 GNUNET_assert (0 ==
782 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) target, 798 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) target,
783 isize, &isize, rval)); 799 isize, &isize, rval));
784 gcry_mpi_release (rval); 800 gcry_mpi_release (rval);
785 adjust (&target->encoding[0], isize, 801 adjust (&target->encoding[0], isize,
786 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData)); 802 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData));
787 return GNUNET_OK; 803 return GNUNET_OK;
788} 804}
789 805
@@ -799,8 +815,8 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
799 */ 815 */
800ssize_t 816ssize_t
801GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key, 817GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key,
802 const struct GNUNET_CRYPTO_RsaEncryptedData * block, 818 const struct GNUNET_CRYPTO_RsaEncryptedData *
803 void *result, size_t max) 819 block, void *result, size_t max)
804{ 820{
805 gcry_sexp_t resultsexp; 821 gcry_sexp_t resultsexp;
806 gcry_sexp_t data; 822 gcry_sexp_t data;
@@ -815,21 +831,22 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key,
815#endif 831#endif
816 size = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData); 832 size = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData);
817 GNUNET_assert (0 == 833 GNUNET_assert (0 ==
818 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, &block->encoding[0], 834 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, &block->encoding[0],
819 size, &size)); 835 size, &size));
820 GNUNET_assert (0 == 836 GNUNET_assert (0 ==
821 gcry_sexp_build (&data, &erroff, "(enc-val(flags)(rsa(a %m)))", 837 gcry_sexp_build (&data, &erroff,
822 val)); 838 "(enc-val(flags)(rsa(a %m)))", val));
823 gcry_mpi_release (val); 839 gcry_mpi_release (val);
824 GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp)); 840 GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp));
825 gcry_sexp_release (data); 841 gcry_sexp_release (data);
826 /* resultsexp has format "(value %m)" */ 842 /* resultsexp has format "(value %m)" */
827 GNUNET_assert (NULL != 843 GNUNET_assert (NULL !=
828 (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG))); 844 (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG)));
829 gcry_sexp_release (resultsexp); 845 gcry_sexp_release (resultsexp);
830 tmp = GNUNET_malloc (max + HOSTKEY_LEN / 8); 846 tmp = GNUNET_malloc (max + HOSTKEY_LEN / 8);
831 size = max + HOSTKEY_LEN / 8; 847 size = max + HOSTKEY_LEN / 8;
832 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val)); 848 GNUNET_assert (0 ==
849 gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val));
833 gcry_mpi_release (val); 850 gcry_mpi_release (val);
834 endp = tmp; 851 endp = tmp;
835 endp += (size - max); 852 endp += (size - max);
@@ -850,8 +867,8 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key,
850 */ 867 */
851int 868int
852GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key, 869GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
853 const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose, 870 const struct GNUNET_CRYPTO_RsaSignaturePurpose
854 struct GNUNET_CRYPTO_RsaSignature *sig) 871 *purpose, struct GNUNET_CRYPTO_RsaSignature *sig)
855{ 872{
856 gcry_sexp_t result; 873 gcry_sexp_t result;
857 gcry_sexp_t data; 874 gcry_sexp_t data;
@@ -867,10 +884,10 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
867 buff = GNUNET_malloc (bufSize); 884 buff = GNUNET_malloc (bufSize);
868 memcpy (buff, FORMATSTRING, bufSize); 885 memcpy (buff, FORMATSTRING, bufSize);
869 memcpy (&buff 886 memcpy (&buff
870 [bufSize - 887 [bufSize -
871 strlen 888 strlen
872 ("0123456789012345678901234567890123456789012345678901234567890123))") 889 ("0123456789012345678901234567890123456789012345678901234567890123))")
873 - 1], &hc, sizeof (GNUNET_HashCode)); 890 - 1], &hc, sizeof (GNUNET_HashCode));
874 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0)); 891 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
875 GNUNET_free (buff); 892 GNUNET_free (buff);
876 GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp)); 893 GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp));
@@ -879,8 +896,8 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
879 gcry_sexp_release (result); 896 gcry_sexp_release (result);
880 ssize = sizeof (struct GNUNET_CRYPTO_RsaSignature); 897 ssize = sizeof (struct GNUNET_CRYPTO_RsaSignature);
881 GNUNET_assert (0 == 898 GNUNET_assert (0 ==
882 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) sig, ssize, 899 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) sig,
883 &ssize, rval)); 900 ssize, &ssize, rval));
884 gcry_mpi_release (rval); 901 gcry_mpi_release (rval);
885 adjust (sig->sig, ssize, sizeof (struct GNUNET_CRYPTO_RsaSignature)); 902 adjust (sig->sig, ssize, sizeof (struct GNUNET_CRYPTO_RsaSignature));
886 return GNUNET_OK; 903 return GNUNET_OK;
@@ -898,11 +915,11 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
898 */ 915 */
899int 916int
900GNUNET_CRYPTO_rsa_verify (uint32_t purpose, 917GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
901 const struct GNUNET_CRYPTO_RsaSignaturePurpose 918 const struct GNUNET_CRYPTO_RsaSignaturePurpose
902 *validate, 919 *validate,
903 const struct GNUNET_CRYPTO_RsaSignature *sig, 920 const struct GNUNET_CRYPTO_RsaSignature *sig,
904 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 921 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
905 *publicKey) 922 *publicKey)
906{ 923{
907 gcry_sexp_t data; 924 gcry_sexp_t data;
908 gcry_sexp_t sigdata; 925 gcry_sexp_t sigdata;
@@ -916,44 +933,44 @@ GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
916 int rc; 933 int rc;
917 934
918 if (purpose != ntohl (validate->purpose)) 935 if (purpose != ntohl (validate->purpose))
919 return GNUNET_SYSERR; /* purpose mismatch */ 936 return GNUNET_SYSERR; /* purpose mismatch */
920 GNUNET_CRYPTO_hash (validate, ntohl (validate->size), &hc); 937 GNUNET_CRYPTO_hash (validate, ntohl (validate->size), &hc);
921 size = sizeof (struct GNUNET_CRYPTO_RsaSignature); 938 size = sizeof (struct GNUNET_CRYPTO_RsaSignature);
922 GNUNET_assert (0 == 939 GNUNET_assert (0 ==
923 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, 940 gcry_mpi_scan (&val, GCRYMPI_FMT_USG,
924 (const unsigned char *) sig, size, &size)); 941 (const unsigned char *) sig, size, &size));
925 GNUNET_assert (0 == 942 GNUNET_assert (0 ==
926 gcry_sexp_build (&sigdata, &erroff, "(sig-val(rsa(s %m)))", 943 gcry_sexp_build (&sigdata, &erroff, "(sig-val(rsa(s %m)))",
927 val)); 944 val));
928 gcry_mpi_release (val); 945 gcry_mpi_release (val);
929 bufSize = strlen (FORMATSTRING) + 1; 946 bufSize = strlen (FORMATSTRING) + 1;
930 buff = GNUNET_malloc (bufSize); 947 buff = GNUNET_malloc (bufSize);
931 memcpy (buff, FORMATSTRING, bufSize); 948 memcpy (buff, FORMATSTRING, bufSize);
932 memcpy (&buff 949 memcpy (&buff
933 [strlen (FORMATSTRING) - 950 [strlen (FORMATSTRING) -
934 strlen 951 strlen
935 ("0123456789012345678901234567890123456789012345678901234567890123))")], 952 ("0123456789012345678901234567890123456789012345678901234567890123))")],
936 &hc, sizeof (GNUNET_HashCode)); 953 &hc, sizeof (GNUNET_HashCode));
937 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0)); 954 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
938 GNUNET_free (buff); 955 GNUNET_free (buff);
939 hostkey = public2PrivateKey (publicKey); 956 hostkey = public2PrivateKey (publicKey);
940 if (hostkey == NULL) 957 if (hostkey == NULL)
941 { 958 {
942 gcry_sexp_release (data); 959 gcry_sexp_release (data);
943 gcry_sexp_release (sigdata); 960 gcry_sexp_release (sigdata);
944 return GNUNET_SYSERR; 961 return GNUNET_SYSERR;
945 } 962 }
946 rc = gcry_pk_verify (sigdata, data, hostkey->sexp); 963 rc = gcry_pk_verify (sigdata, data, hostkey->sexp);
947 GNUNET_CRYPTO_rsa_key_free (hostkey); 964 GNUNET_CRYPTO_rsa_key_free (hostkey);
948 gcry_sexp_release (data); 965 gcry_sexp_release (data);
949 gcry_sexp_release (sigdata); 966 gcry_sexp_release (sigdata);
950 if (rc) 967 if (rc)
951 { 968 {
952 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 969 LOG (GNUNET_ERROR_TYPE_WARNING,
953 _("RSA signature verification failed at %s:%d: %s\n"), __FILE__, 970 _("RSA signature verification failed at %s:%d: %s\n"), __FILE__,
954 __LINE__, gcry_strerror (rc)); 971 __LINE__, gcry_strerror (rc));
955 return GNUNET_SYSERR; 972 return GNUNET_SYSERR;
956 } 973 }
957 return GNUNET_OK; 974 return GNUNET_OK;
958} 975}
959 976
diff --git a/src/util/disk.c b/src/util/disk.c
index bb507bbff..2021d6493 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -34,6 +34,12 @@
34#include "gnunet_crypto_lib.h" 34#include "gnunet_crypto_lib.h"
35#include "disk.h" 35#include "disk.h"
36 36
37#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38
39#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
40
41#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42
37#define DEBUG_NPIPE GNUNET_EXTRA_LOGGING 43#define DEBUG_NPIPE GNUNET_EXTRA_LOGGING
38 44
39#define DEBUG_PIPE GNUNET_EXTRA_LOGGING 45#define DEBUG_PIPE GNUNET_EXTRA_LOGGING
@@ -61,8 +67,8 @@
61#define PIPE_BUF 512 67#define PIPE_BUF 512
62ULONG PipeSerialNumber; 68ULONG PipeSerialNumber;
63#endif 69#endif
64#define _IFMT 0170000 /* type of file */ 70#define _IFMT 0170000 /* type of file */
65#define _IFLNK 0120000 /* symbolic link */ 71#define _IFLNK 0120000 /* symbolic link */
66#define S_ISLNK(m) (((m)&_IFMT) == _IFLNK) 72#define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
67#else 73#else
68#error PORT-ME: need to port statfs (how much space is left on the drive?) 74#error PORT-ME: need to port statfs (how much space is left on the drive?)
@@ -159,25 +165,25 @@ getSizeRec (void *cls, const char *fn)
159 165
160#ifdef HAVE_STAT64 166#ifdef HAVE_STAT64
161 if (0 != STAT64 (fn, &buf)) 167 if (0 != STAT64 (fn, &buf))
162 { 168 {
163 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat64", fn); 169 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat64", fn);
164 return GNUNET_SYSERR; 170 return GNUNET_SYSERR;
165 } 171 }
166#else 172#else
167 if (0 != STAT (fn, &buf)) 173 if (0 != STAT (fn, &buf))
168 { 174 {
169 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fn); 175 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fn);
170 return GNUNET_SYSERR; 176 return GNUNET_SYSERR;
171 } 177 }
172#endif 178#endif
173 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) 179 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
174 gfsd->total += buf.st_size; 180 gfsd->total += buf.st_size;
175 if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) && 181 if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) &&
176 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) 182 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
177 { 183 {
178 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) 184 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
179 return GNUNET_SYSERR; 185 return GNUNET_SYSERR;
180 } 186 }
181 return GNUNET_OK; 187 return GNUNET_OK;
182} 188}
183 189
@@ -209,13 +215,13 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
209 */ 215 */
210off_t 216off_t
211GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset, 217GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
212 enum GNUNET_DISK_Seek whence) 218 enum GNUNET_DISK_Seek whence)
213{ 219{
214 if (h == NULL) 220 if (h == NULL)
215 { 221 {
216 errno = EINVAL; 222 errno = EINVAL;
217 return GNUNET_SYSERR; 223 return GNUNET_SYSERR;
218 } 224 }
219 225
220#ifdef MINGW 226#ifdef MINGW
221 DWORD ret; 227 DWORD ret;
@@ -226,10 +232,10 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
226 232
227 ret = SetFilePointer (h->h, offset, NULL, t[whence]); 233 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
228 if (ret == INVALID_SET_FILE_POINTER) 234 if (ret == INVALID_SET_FILE_POINTER)
229 { 235 {
230 SetErrnoFromWinError (GetLastError ()); 236 SetErrnoFromWinError (GetLastError ());
231 return GNUNET_SYSERR; 237 return GNUNET_SYSERR;
232 } 238 }
233 return ret; 239 return ret;
234#else 240#else
235 static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET, 241 static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET,
@@ -255,7 +261,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
255 */ 261 */
256int 262int
257GNUNET_DISK_file_size (const char *filename, uint64_t * size, 263GNUNET_DISK_file_size (const char *filename, uint64_t * size,
258 int includeSymLinks) 264 int includeSymLinks)
259{ 265{
260 struct GetFileSizeData gfsd; 266 struct GetFileSizeData gfsd;
261 int ret; 267 int ret;
@@ -286,29 +292,29 @@ GNUNET_DISK_file_size (const char *filename, uint64_t * size,
286 */ 292 */
287int 293int
288GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, 294GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
289 uint64_t * ino) 295 uint64_t * ino)
290{ 296{
291#if LINUX 297#if LINUX
292 struct stat sbuf; 298 struct stat sbuf;
293 struct statvfs fbuf; 299 struct statvfs fbuf;
294 300
295 if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf))) 301 if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf)))
296 { 302 {
297 *dev = (uint64_t) fbuf.f_fsid; 303 *dev = (uint64_t) fbuf.f_fsid;
298 *ino = (uint64_t) sbuf.st_ino; 304 *ino = (uint64_t) sbuf.st_ino;
299 return GNUNET_OK; 305 return GNUNET_OK;
300 } 306 }
301#elif SOMEBSD 307#elif SOMEBSD
302 struct stat sbuf; 308 struct stat sbuf;
303 struct statfs fbuf; 309 struct statfs fbuf;
304 310
305 if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf))) 311 if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf)))
306 { 312 {
307 *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || 313 *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 ||
308 ((uint64_t) fbuf.f_fsid.val[1]); 314 ((uint64_t) fbuf.f_fsid.val[1]);
309 *ino = (uint64_t) sbuf.st_ino; 315 *ino = (uint64_t) sbuf.st_ino;
310 return GNUNET_OK; 316 return GNUNET_OK;
311 } 317 }
312#elif WINDOWS 318#elif WINDOWS
313 // FIXME NILS: test this 319 // FIXME NILS: test this
314 struct GNUNET_DISK_FileHandle *fh; 320 struct GNUNET_DISK_FileHandle *fh;
@@ -321,11 +327,11 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
321 succ = GetFileInformationByHandle (fh->h, &info); 327 succ = GetFileInformationByHandle (fh->h, &info);
322 GNUNET_DISK_file_close (fh); 328 GNUNET_DISK_file_close (fh);
323 if (succ) 329 if (succ)
324 { 330 {
325 *dev = info.dwVolumeSerialNumber; 331 *dev = info.dwVolumeSerialNumber;
326 *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow); 332 *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow);
327 return GNUNET_OK; 333 return GNUNET_OK;
328 } 334 }
329 else 335 else
330 return GNUNET_SYSERR; 336 return GNUNET_SYSERR;
331 337
@@ -357,37 +363,37 @@ GNUNET_DISK_mktemp (const char *t)
357#if WINDOWS 363#if WINDOWS
358 && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) 364 && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':'))
359#endif 365#endif
360 ) 366 )
361 { 367 {
362 tmpdir = getenv ("TMPDIR"); 368 tmpdir = getenv ("TMPDIR");
363 tmpdir = tmpdir ? tmpdir : "/tmp"; 369 tmpdir = tmpdir ? tmpdir : "/tmp";
364 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); 370 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
365 } 371 }
366 else 372 else
367 { 373 {
368 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX"); 374 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
369 } 375 }
370#ifdef MINGW 376#ifdef MINGW
371 fn = (char *) GNUNET_malloc (MAX_PATH + 1); 377 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
372 if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn)) 378 if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn))
373 { 379 {
374 GNUNET_free (fn); 380 GNUNET_free (fn);
375 GNUNET_free (tmpl); 381 GNUNET_free (tmpl);
376 return NULL; 382 return NULL;
377 } 383 }
378 GNUNET_free (tmpl); 384 GNUNET_free (tmpl);
379#else 385#else
380 fn = tmpl; 386 fn = tmpl;
381#endif 387#endif
382 fd = mkstemp (fn); 388 fd = mkstemp (fn);
383 if (fd == -1) 389 if (fd == -1)
384 { 390 {
385 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); 391 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
386 GNUNET_free (fn); 392 GNUNET_free (fn);
387 return NULL; 393 return NULL;
388 } 394 }
389 if (0 != CLOSE (fd)) 395 if (0 != CLOSE (fd))
390 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn); 396 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
391 return fn; 397 return fn;
392} 398}
393 399
@@ -406,10 +412,10 @@ GNUNET_DISK_get_blocks_available (const char *part)
406 struct statvfs buf; 412 struct statvfs buf;
407 413
408 if (0 != statvfs (part, &buf)) 414 if (0 != statvfs (part, &buf))
409 { 415 {
410 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); 416 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
411 return -1; 417 return -1;
412 } 418 }
413 return buf.f_bavail; 419 return buf.f_bavail;
414#elif MINGW 420#elif MINGW
415 DWORD dwDummy; 421 DWORD dwDummy;
@@ -424,22 +430,22 @@ GNUNET_DISK_get_blocks_available (const char *part)
424 GNUNET_free (path); 430 GNUNET_free (path);
425 szDrive[3] = 0; 431 szDrive[3] = 0;
426 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy)) 432 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
427 { 433 {
428 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 434 LOG (GNUNET_ERROR_TYPE_WARNING,
429 _("`%s' failed for drive `%s': %u\n"), "GetDiskFreeSpace", 435 _("`%s' failed for drive `%s': %u\n"), "GetDiskFreeSpace",
430 szDrive, GetLastError ()); 436 szDrive, GetLastError ());
431 437
432 return -1; 438 return -1;
433 } 439 }
434 return dwBlocks; 440 return dwBlocks;
435#else 441#else
436 struct statfs s; 442 struct statfs s;
437 443
438 if (0 != statfs (part, &s)) 444 if (0 != statfs (part, &s))
439 { 445 {
440 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); 446 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
441 return -1; 447 return -1;
442 } 448 }
443 return s.f_bavail; 449 return s.f_bavail;
444#endif 450#endif
445} 451}
@@ -463,21 +469,21 @@ GNUNET_DISK_directory_test (const char *fil)
463 469
464 ret = STAT (fil, &filestat); 470 ret = STAT (fil, &filestat);
465 if (ret != 0) 471 if (ret != 0)
466 {
467 if (errno != ENOENT)
468 { 472 {
469 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil); 473 if (errno != ENOENT)
470 return GNUNET_SYSERR; 474 {
475 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
476 return GNUNET_SYSERR;
477 }
478 return GNUNET_NO;
471 } 479 }
472 return GNUNET_NO;
473 }
474 if (!S_ISDIR (filestat.st_mode)) 480 if (!S_ISDIR (filestat.st_mode))
475 return GNUNET_NO; 481 return GNUNET_NO;
476 if (ACCESS (fil, R_OK | X_OK) < 0) 482 if (ACCESS (fil, R_OK | X_OK) < 0)
477 { 483 {
478 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil); 484 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
479 return GNUNET_SYSERR; 485 return GNUNET_SYSERR;
480 } 486 }
481 return GNUNET_YES; 487 return GNUNET_YES;
482} 488}
483 489
@@ -502,27 +508,27 @@ GNUNET_DISK_file_test (const char *fil)
502 508
503 ret = STAT (rdir, &filestat); 509 ret = STAT (rdir, &filestat);
504 if (ret != 0) 510 if (ret != 0)
505 {
506 if (errno != ENOENT)
507 { 511 {
508 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); 512 if (errno != ENOENT)
513 {
514 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
515 GNUNET_free (rdir);
516 return GNUNET_SYSERR;
517 }
509 GNUNET_free (rdir); 518 GNUNET_free (rdir);
510 return GNUNET_SYSERR; 519 return GNUNET_NO;
511 } 520 }
512 GNUNET_free (rdir);
513 return GNUNET_NO;
514 }
515 if (!S_ISREG (filestat.st_mode)) 521 if (!S_ISREG (filestat.st_mode))
516 { 522 {
517 GNUNET_free (rdir); 523 GNUNET_free (rdir);
518 return GNUNET_NO; 524 return GNUNET_NO;
519 } 525 }
520 if (ACCESS (rdir, R_OK) < 0) 526 if (ACCESS (rdir, R_OK) < 0)
521 { 527 {
522 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir); 528 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
523 GNUNET_free (rdir); 529 GNUNET_free (rdir);
524 return GNUNET_SYSERR; 530 return GNUNET_SYSERR;
525 } 531 }
526 GNUNET_free (rdir); 532 GNUNET_free (rdir);
527 return GNUNET_YES; 533 return GNUNET_YES;
528} 534}
@@ -547,56 +553,56 @@ GNUNET_DISK_directory_create (const char *dir)
547 553
548 len = strlen (rdir); 554 len = strlen (rdir);
549#ifndef MINGW 555#ifndef MINGW
550 pos = 1; /* skip heading '/' */ 556 pos = 1; /* skip heading '/' */
551#else 557#else
552 /* Local or Network path? */ 558 /* Local or Network path? */
553 if (strncmp (rdir, "\\\\", 2) == 0) 559 if (strncmp (rdir, "\\\\", 2) == 0)
554 {
555 pos = 2;
556 while (rdir[pos])
557 { 560 {
558 if (rdir[pos] == '\\') 561 pos = 2;
559 { 562 while (rdir[pos])
560 pos++; 563 {
561 break; 564 if (rdir[pos] == '\\')
562 } 565 {
563 pos++; 566 pos++;
567 break;
568 }
569 pos++;
570 }
564 } 571 }
565 }
566 else 572 else
567 { 573 {
568 pos = 3; /* strlen("C:\\") */ 574 pos = 3; /* strlen("C:\\") */
569 } 575 }
570#endif 576#endif
571 while (pos <= len) 577 while (pos <= len)
572 {
573 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
574 { 578 {
575 rdir[pos] = '\0'; 579 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
576 ret = GNUNET_DISK_directory_test (rdir); 580 {
577 if (ret == GNUNET_SYSERR) 581 rdir[pos] = '\0';
578 { 582 ret = GNUNET_DISK_directory_test (rdir);
579 GNUNET_free (rdir); 583 if (ret == GNUNET_SYSERR)
580 return GNUNET_SYSERR; 584 {
581 } 585 GNUNET_free (rdir);
582 if (ret == GNUNET_NO) 586 return GNUNET_SYSERR;
583 { 587 }
588 if (ret == GNUNET_NO)
589 {
584#ifndef MINGW 590#ifndef MINGW
585 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ 591 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
586#else 592#else
587 ret = mkdir (rdir); 593 ret = mkdir (rdir);
588#endif 594#endif
589 if ((ret != 0) && (errno != EEXIST)) 595 if ((ret != 0) && (errno != EEXIST))
590 { 596 {
591 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); 597 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
592 GNUNET_free (rdir); 598 GNUNET_free (rdir);
593 return GNUNET_SYSERR; 599 return GNUNET_SYSERR;
594 } 600 }
595 } 601 }
596 rdir[pos] = DIR_SEPARATOR; 602 rdir[pos] = DIR_SEPARATOR;
603 }
604 pos++;
597 } 605 }
598 pos++;
599 }
600 GNUNET_free (rdir); 606 GNUNET_free (rdir);
601 return GNUNET_OK; 607 return GNUNET_OK;
602} 608}
@@ -643,37 +649,37 @@ GNUNET_DISK_directory_create_for_file (const char *filename)
643 */ 649 */
644ssize_t 650ssize_t
645GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, 651GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result,
646 size_t len) 652 size_t len)
647{ 653{
648 if (h == NULL) 654 if (h == NULL)
649 { 655 {
650 errno = EINVAL; 656 errno = EINVAL;
651 return GNUNET_SYSERR; 657 return GNUNET_SYSERR;
652 } 658 }
653 659
654#ifdef MINGW 660#ifdef MINGW
655 DWORD bytesRead; 661 DWORD bytesRead;
656 662
657 if (h->type != GNUNET_PIPE) 663 if (h->type != GNUNET_PIPE)
658 {
659 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
660 { 664 {
661 SetErrnoFromWinError (GetLastError ()); 665 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
662 return GNUNET_SYSERR; 666 {
667 SetErrnoFromWinError (GetLastError ());
668 return GNUNET_SYSERR;
669 }
663 } 670 }
664 }
665 else 671 else
666 {
667 if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead))
668 { 672 {
669 if (GetLastError () != ERROR_IO_PENDING) 673 if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead))
670 { 674 {
671 SetErrnoFromWinError (GetLastError ()); 675 if (GetLastError () != ERROR_IO_PENDING)
672 return GNUNET_SYSERR; 676 {
673 } 677 SetErrnoFromWinError (GetLastError ());
678 return GNUNET_SYSERR;
679 }
680 }
681 GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE);
674 } 682 }
675 GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE);
676 }
677 return bytesRead; 683 return bytesRead;
678#else 684#else
679 return read (h->fd, result, len); 685 return read (h->fd, result, len);
@@ -695,7 +701,8 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
695 struct GNUNET_DISK_FileHandle *fh; 701 struct GNUNET_DISK_FileHandle *fh;
696 ssize_t ret; 702 ssize_t ret;
697 703
698 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); 704 fh =
705 GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
699 if (!fh) 706 if (!fh)
700 return GNUNET_SYSERR; 707 return GNUNET_SYSERR;
701 ret = GNUNET_DISK_file_read (fh, result, len); 708 ret = GNUNET_DISK_file_read (fh, result, len);
@@ -714,46 +721,46 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
714 */ 721 */
715ssize_t 722ssize_t
716GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, 723GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
717 const void *buffer, size_t n) 724 const void *buffer, size_t n)
718{ 725{
719 if (h == NULL) 726 if (h == NULL)
720 { 727 {
721 errno = EINVAL; 728 errno = EINVAL;
722 return GNUNET_SYSERR; 729 return GNUNET_SYSERR;
723 } 730 }
724 731
725#ifdef MINGW 732#ifdef MINGW
726 DWORD bytesWritten; 733 DWORD bytesWritten;
727 734
728 if (h->type != GNUNET_PIPE) 735 if (h->type != GNUNET_PIPE)
729 {
730 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
731 { 736 {
732 SetErrnoFromWinError (GetLastError ()); 737 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
733 return GNUNET_SYSERR; 738 {
739 SetErrnoFromWinError (GetLastError ());
740 return GNUNET_SYSERR;
741 }
734 } 742 }
735 }
736 else 743 else
737 { 744 {
738#if DEBUG_PIPE 745#if DEBUG_PIPE
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write\n"); 746 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write\n");
740#endif 747#endif
741 if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite)) 748 if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite))
742 { 749 {
743 if (GetLastError () != ERROR_IO_PENDING) 750 if (GetLastError () != ERROR_IO_PENDING)
744 { 751 {
745 SetErrnoFromWinError (GetLastError ()); 752 SetErrnoFromWinError (GetLastError ());
746#if DEBUG_PIPE 753#if DEBUG_PIPE
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n"); 754 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n");
748#endif 755#endif
749 return GNUNET_SYSERR; 756 return GNUNET_SYSERR;
750 } 757 }
751 } 758 }
752#if DEBUG_PIPE 759#if DEBUG_PIPE
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); 760 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
754#endif 761#endif
755 GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE); 762 GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE);
756 } 763 }
757 return bytesWritten; 764 return bytesWritten;
758#else 765#else
759 return write (h->fd, buffer, n); 766 return write (h->fd, buffer, n);
@@ -772,14 +779,15 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
772 */ 779 */
773ssize_t 780ssize_t
774GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n, 781GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
775 enum GNUNET_DISK_AccessPermissions mode) 782 enum GNUNET_DISK_AccessPermissions mode)
776{ 783{
777 struct GNUNET_DISK_FileHandle *fh; 784 struct GNUNET_DISK_FileHandle *fh;
778 ssize_t ret; 785 ssize_t ret;
779 786
780 fh = GNUNET_DISK_file_open (fn, 787 fh = GNUNET_DISK_file_open (fn,
781 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE 788 GNUNET_DISK_OPEN_WRITE |
782 | GNUNET_DISK_OPEN_CREATE, mode); 789 GNUNET_DISK_OPEN_TRUNCATE |
790 GNUNET_DISK_OPEN_CREATE, mode);
783 if (!fh) 791 if (!fh)
784 return GNUNET_SYSERR; 792 return GNUNET_SYSERR;
785 ret = GNUNET_DISK_file_write (fh, buffer, n); 793 ret = GNUNET_DISK_file_write (fh, buffer, n);
@@ -799,8 +807,8 @@ GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
799 */ 807 */
800int 808int
801GNUNET_DISK_directory_scan (const char *dirName, 809GNUNET_DISK_directory_scan (const char *dirName,
802 GNUNET_FileNameCallback callback, 810 GNUNET_FileNameCallback callback,
803 void *callback_cls) 811 void *callback_cls)
804{ 812{
805 DIR *dinfo; 813 DIR *dinfo;
806 struct dirent *finfo; 814 struct dirent *finfo;
@@ -818,61 +826,61 @@ GNUNET_DISK_directory_scan (const char *dirName,
818 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) 826 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
819 dname[strlen (dname) - 1] = '\0'; 827 dname[strlen (dname) - 1] = '\0';
820 if (0 != STAT (dname, &istat)) 828 if (0 != STAT (dname, &istat))
821 { 829 {
822 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname); 830 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
823 GNUNET_free (dname); 831 GNUNET_free (dname);
824 return GNUNET_SYSERR; 832 return GNUNET_SYSERR;
825 } 833 }
826 if (!S_ISDIR (istat.st_mode)) 834 if (!S_ISDIR (istat.st_mode))
827 { 835 {
828 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 836 LOG (GNUNET_ERROR_TYPE_WARNING,
829 _("Expected `%s' to be a directory!\n"), dirName); 837 _("Expected `%s' to be a directory!\n"), dirName);
830 GNUNET_free (dname); 838 GNUNET_free (dname);
831 return GNUNET_SYSERR; 839 return GNUNET_SYSERR;
832 } 840 }
833 errno = 0; 841 errno = 0;
834 dinfo = OPENDIR (dname); 842 dinfo = OPENDIR (dname);
835 if ((errno == EACCES) || (dinfo == NULL)) 843 if ((errno == EACCES) || (dinfo == NULL))
836 { 844 {
837 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); 845 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
838 if (dinfo != NULL) 846 if (dinfo != NULL)
839 closedir (dinfo); 847 closedir (dinfo);
840 GNUNET_free (dname); 848 GNUNET_free (dname);
841 return GNUNET_SYSERR; 849 return GNUNET_SYSERR;
842 } 850 }
843 name_len = 256; 851 name_len = 256;
844 n_size = strlen (dname) + name_len + 2; 852 n_size = strlen (dname) + name_len + 2;
845 name = GNUNET_malloc (n_size); 853 name = GNUNET_malloc (n_size);
846 while ((finfo = readdir (dinfo)) != NULL) 854 while ((finfo = readdir (dinfo)) != NULL)
847 {
848 if ((0 == strcmp (finfo->d_name, ".")) ||
849 (0 == strcmp (finfo->d_name, "..")))
850 continue;
851 if (callback != NULL)
852 { 855 {
853 if (name_len < strlen (finfo->d_name)) 856 if ((0 == strcmp (finfo->d_name, ".")) ||
854 { 857 (0 == strcmp (finfo->d_name, "..")))
855 GNUNET_free (name); 858 continue;
856 name_len = strlen (finfo->d_name); 859 if (callback != NULL)
857 n_size = strlen (dname) + name_len + 2; 860 {
858 name = GNUNET_malloc (n_size); 861 if (name_len < strlen (finfo->d_name))
859 } 862 {
860 /* dname can end in "/" only if dname == "/"; 863 GNUNET_free (name);
861 * if dname does not end in "/", we need to add 864 name_len = strlen (finfo->d_name);
862 * a "/" (otherwise, we must not!) */ 865 n_size = strlen (dname) + name_len + 2;
863 GNUNET_snprintf (name, n_size, "%s%s%s", dname, 866 name = GNUNET_malloc (n_size);
864 (strcmp (dname, DIR_SEPARATOR_STR) == 867 }
865 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); 868 /* dname can end in "/" only if dname == "/";
866 if (GNUNET_OK != callback (callback_cls, name)) 869 * if dname does not end in "/", we need to add
867 { 870 * a "/" (otherwise, we must not!) */
868 closedir (dinfo); 871 GNUNET_snprintf (name, n_size, "%s%s%s", dname,
869 GNUNET_free (name); 872 (strcmp (dname, DIR_SEPARATOR_STR) ==
870 GNUNET_free (dname); 873 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
871 return GNUNET_SYSERR; 874 if (GNUNET_OK != callback (callback_cls, name))
872 } 875 {
876 closedir (dinfo);
877 GNUNET_free (name);
878 GNUNET_free (dname);
879 return GNUNET_SYSERR;
880 }
881 }
882 count++;
873 } 883 }
874 count++;
875 }
876 closedir (dinfo); 884 closedir (dinfo);
877 GNUNET_free (name); 885 GNUNET_free (name);
878 GNUNET_free (dname); 886 GNUNET_free (dname);
@@ -924,7 +932,7 @@ struct GNUNET_DISK_DirectoryIterator
924 */ 932 */
925static void 933static void
926directory_iterator_task (void *cls, 934directory_iterator_task (void *cls,
927 const struct GNUNET_SCHEDULER_TaskContext *tc) 935 const struct GNUNET_SCHEDULER_TaskContext *tc)
928{ 936{
929 struct GNUNET_DISK_DirectoryIterator *iter = cls; 937 struct GNUNET_DISK_DirectoryIterator *iter = cls;
930 char *name; 938 char *name;
@@ -949,35 +957,35 @@ directory_iterator_task (void *cls,
949 * GNUNET_SYSERR if abort was YES 957 * GNUNET_SYSERR if abort was YES
950 */ 958 */
951int 959int
952GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter, 960GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
953 int can) 961 *iter, int can)
954{ 962{
955 struct dirent *finfo; 963 struct dirent *finfo;
956 964
957 GNUNET_assert (iter->next_name == NULL); 965 GNUNET_assert (iter->next_name == NULL);
958 if (can == GNUNET_YES) 966 if (can == GNUNET_YES)
959 { 967 {
960 closedir (iter->directory); 968 closedir (iter->directory);
961 GNUNET_free (iter->dirname); 969 GNUNET_free (iter->dirname);
962 GNUNET_free (iter); 970 GNUNET_free (iter);
963 return GNUNET_SYSERR; 971 return GNUNET_SYSERR;
964 } 972 }
965 while (NULL != (finfo = readdir (iter->directory))) 973 while (NULL != (finfo = readdir (iter->directory)))
966 { 974 {
967 if ((0 == strcmp (finfo->d_name, ".")) || 975 if ((0 == strcmp (finfo->d_name, ".")) ||
968 (0 == strcmp (finfo->d_name, ".."))) 976 (0 == strcmp (finfo->d_name, "..")))
969 continue; 977 continue;
970 GNUNET_asprintf (&iter->next_name, "%s%s%s", iter->dirname, 978 GNUNET_asprintf (&iter->next_name, "%s%s%s", iter->dirname,
971 DIR_SEPARATOR_STR, finfo->d_name); 979 DIR_SEPARATOR_STR, finfo->d_name);
972 break; 980 break;
973 } 981 }
974 if (finfo == NULL) 982 if (finfo == NULL)
975 { 983 {
976 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES); 984 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
977 return GNUNET_NO; 985 return GNUNET_NO;
978 } 986 }
979 GNUNET_SCHEDULER_add_with_priority (iter->priority, &directory_iterator_task, 987 GNUNET_SCHEDULER_add_with_priority (iter->priority,
980 iter); 988 &directory_iterator_task, iter);
981 return GNUNET_YES; 989 return GNUNET_YES;
982} 990}
983 991
@@ -995,9 +1003,9 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
995 */ 1003 */
996void 1004void
997GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio, 1005GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
998 const char *dirName, 1006 const char *dirName,
999 GNUNET_DISK_DirectoryIteratorCallback 1007 GNUNET_DISK_DirectoryIteratorCallback
1000 callback, void *callback_cls) 1008 callback, void *callback_cls)
1001{ 1009{
1002 struct GNUNET_DISK_DirectoryIterator *di; 1010 struct GNUNET_DISK_DirectoryIterator *di;
1003 1011
@@ -1006,11 +1014,11 @@ GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
1006 di->callback_cls = callback_cls; 1014 di->callback_cls = callback_cls;
1007 di->directory = OPENDIR (dirName); 1015 di->directory = OPENDIR (dirName);
1008 if (di->directory == NULL) 1016 if (di->directory == NULL)
1009 { 1017 {
1010 GNUNET_free (di); 1018 GNUNET_free (di);
1011 callback (callback_cls, NULL, NULL, NULL); 1019 callback (callback_cls, NULL, NULL, NULL);
1012 return; 1020 return;
1013 } 1021 }
1014 di->dirname = GNUNET_strdup (dirName); 1022 di->dirname = GNUNET_strdup (dirName);
1015 di->priority = prio; 1023 di->priority = prio;
1016 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO); 1024 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
@@ -1047,7 +1055,7 @@ GNUNET_DISK_directory_remove (const char *fileName)
1047 struct stat istat; 1055 struct stat istat;
1048 1056
1049 if (0 != LSTAT (fileName, &istat)) 1057 if (0 != LSTAT (fileName, &istat))
1050 return GNUNET_NO; /* file may not exist... */ 1058 return GNUNET_NO; /* file may not exist... */
1051 CHMOD (fileName, S_IWUSR | S_IRUSR | S_IXUSR); 1059 CHMOD (fileName, S_IWUSR | S_IRUSR | S_IXUSR);
1052 if (UNLINK (fileName) == 0) 1060 if (UNLINK (fileName) == 0)
1053 return GNUNET_OK; 1061 return GNUNET_OK;
@@ -1056,18 +1064,18 @@ GNUNET_DISK_directory_remove (const char *fileName)
1056 * sticky /tmp directory may result in EPERM on BSD. 1064 * sticky /tmp directory may result in EPERM on BSD.
1057 * So we also explicitly check "isDirectory" */ 1065 * So we also explicitly check "isDirectory" */
1058 (GNUNET_YES != GNUNET_DISK_directory_test (fileName))) 1066 (GNUNET_YES != GNUNET_DISK_directory_test (fileName)))
1059 { 1067 {
1060 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); 1068 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
1061 return GNUNET_SYSERR; 1069 return GNUNET_SYSERR;
1062 } 1070 }
1063 if (GNUNET_SYSERR == 1071 if (GNUNET_SYSERR ==
1064 GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL)) 1072 GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL))
1065 return GNUNET_SYSERR; 1073 return GNUNET_SYSERR;
1066 if (0 != RMDIR (fileName)) 1074 if (0 != RMDIR (fileName))
1067 { 1075 {
1068 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); 1076 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
1069 return GNUNET_SYSERR; 1077 return GNUNET_SYSERR;
1070 } 1078 }
1071 return GNUNET_OK; 1079 return GNUNET_OK;
1072} 1080}
1073 1081
@@ -1093,34 +1101,34 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
1093 return GNUNET_SYSERR; 1101 return GNUNET_SYSERR;
1094 pos = 0; 1102 pos = 0;
1095 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, 1103 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
1096 GNUNET_DISK_PERM_NONE); 1104 GNUNET_DISK_PERM_NONE);
1097 if (!in) 1105 if (!in)
1098 return GNUNET_SYSERR; 1106 return GNUNET_SYSERR;
1099 out = 1107 out =
1100 GNUNET_DISK_file_open (dst, 1108 GNUNET_DISK_file_open (dst,
1101 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 1109 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
1102 GNUNET_DISK_OPEN_FAILIFEXISTS, 1110 GNUNET_DISK_OPEN_FAILIFEXISTS,
1103 GNUNET_DISK_PERM_USER_READ | 1111 GNUNET_DISK_PERM_USER_READ |
1104 GNUNET_DISK_PERM_USER_WRITE | 1112 GNUNET_DISK_PERM_USER_WRITE |
1105 GNUNET_DISK_PERM_GROUP_READ | 1113 GNUNET_DISK_PERM_GROUP_READ |
1106 GNUNET_DISK_PERM_GROUP_WRITE); 1114 GNUNET_DISK_PERM_GROUP_WRITE);
1107 if (!out) 1115 if (!out)
1108 { 1116 {
1109 GNUNET_DISK_file_close (in); 1117 GNUNET_DISK_file_close (in);
1110 return GNUNET_SYSERR; 1118 return GNUNET_SYSERR;
1111 } 1119 }
1112 buf = GNUNET_malloc (COPY_BLK_SIZE); 1120 buf = GNUNET_malloc (COPY_BLK_SIZE);
1113 while (pos < size) 1121 while (pos < size)
1114 { 1122 {
1115 len = COPY_BLK_SIZE; 1123 len = COPY_BLK_SIZE;
1116 if (len > size - pos) 1124 if (len > size - pos)
1117 len = size - pos; 1125 len = size - pos;
1118 if (len != GNUNET_DISK_file_read (in, buf, len)) 1126 if (len != GNUNET_DISK_file_read (in, buf, len))
1119 goto FAIL; 1127 goto FAIL;
1120 if (len != GNUNET_DISK_file_write (out, buf, len)) 1128 if (len != GNUNET_DISK_file_write (out, buf, len))
1121 goto FAIL; 1129 goto FAIL;
1122 pos += len; 1130 pos += len;
1123 } 1131 }
1124 GNUNET_free (buf); 1132 GNUNET_free (buf);
1125 GNUNET_DISK_file_close (in); 1133 GNUNET_DISK_file_close (in);
1126 GNUNET_DISK_file_close (out); 1134 GNUNET_DISK_file_close (out);
@@ -1145,17 +1153,17 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1145 1153
1146 idx = fn; 1154 idx = fn;
1147 while (*idx) 1155 while (*idx)
1148 {
1149 c = *idx;
1150
1151 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' ||
1152 c == '<' || c == '>' || c == '|')
1153 { 1156 {
1154 *idx = '_'; 1157 c = *idx;
1155 } 1158
1159 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?'
1160 || c == '"' || c == '<' || c == '>' || c == '|')
1161 {
1162 *idx = '_';
1163 }
1156 1164
1157 idx++; 1165 idx++;
1158 } 1166 }
1159} 1167}
1160 1168
1161 1169
@@ -1175,14 +1183,14 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1175 1183
1176 pws = getpwnam (user); 1184 pws = getpwnam (user);
1177 if (pws == NULL) 1185 if (pws == NULL)
1178 { 1186 {
1179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1187 LOG (GNUNET_ERROR_TYPE_ERROR,
1180 _("Cannot obtain information about user `%s': %s\n"), user, 1188 _("Cannot obtain information about user `%s': %s\n"), user,
1181 STRERROR (errno)); 1189 STRERROR (errno));
1182 return GNUNET_SYSERR; 1190 return GNUNET_SYSERR;
1183 } 1191 }
1184 if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) 1192 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1185 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename); 1193 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1186#endif 1194#endif
1187 return GNUNET_OK; 1195 return GNUNET_OK;
1188} 1196}
@@ -1198,13 +1206,13 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1198 */ 1206 */
1199int 1207int
1200GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart, 1208GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1201 off_t lockEnd, int excl) 1209 off_t lockEnd, int excl)
1202{ 1210{
1203 if (fh == NULL) 1211 if (fh == NULL)
1204 { 1212 {
1205 errno = EINVAL; 1213 errno = EINVAL;
1206 return GNUNET_SYSERR; 1214 return GNUNET_SYSERR;
1207 } 1215 }
1208 1216
1209#ifndef MINGW 1217#ifndef MINGW
1210 struct flock fl; 1218 struct flock fl;
@@ -1223,12 +1231,13 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1223 o.Offset = lockStart; 1231 o.Offset = lockStart;
1224 1232
1225 if (!LockFileEx 1233 if (!LockFileEx
1226 (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY, 1234 (fh->h,
1227 0, lockEnd - lockStart, 0, &o)) 1235 (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY, 0,
1228 { 1236 lockEnd - lockStart, 0, &o))
1229 SetErrnoFromWinError (GetLastError ()); 1237 {
1230 return GNUNET_SYSERR; 1238 SetErrnoFromWinError (GetLastError ());
1231 } 1239 return GNUNET_SYSERR;
1240 }
1232 1241
1233 return GNUNET_OK; 1242 return GNUNET_OK;
1234#endif 1243#endif
@@ -1244,13 +1253,13 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1244 */ 1253 */
1245int 1254int
1246GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, 1255GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1247 off_t unlockEnd) 1256 off_t unlockEnd)
1248{ 1257{
1249 if (fh == NULL) 1258 if (fh == NULL)
1250 { 1259 {
1251 errno = EINVAL; 1260 errno = EINVAL;
1252 return GNUNET_SYSERR; 1261 return GNUNET_SYSERR;
1253 } 1262 }
1254 1263
1255#ifndef MINGW 1264#ifndef MINGW
1256 struct flock fl; 1265 struct flock fl;
@@ -1269,10 +1278,10 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1269 o.Offset = unlockStart; 1278 o.Offset = unlockStart;
1270 1279
1271 if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o)) 1280 if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o))
1272 { 1281 {
1273 SetErrnoFromWinError (GetLastError ()); 1282 SetErrnoFromWinError (GetLastError ());
1274 return GNUNET_SYSERR; 1283 return GNUNET_SYSERR;
1275 } 1284 }
1276 1285
1277 return GNUNET_OK; 1286 return GNUNET_OK;
1278#endif 1287#endif
@@ -1293,7 +1302,7 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1293 */ 1302 */
1294struct GNUNET_DISK_FileHandle * 1303struct GNUNET_DISK_FileHandle *
1295GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, 1304GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1296 enum GNUNET_DISK_AccessPermissions perm) 1305 enum GNUNET_DISK_AccessPermissions perm)
1297{ 1306{
1298 char *expfn; 1307 char *expfn;
1299 struct GNUNET_DISK_FileHandle *ret; 1308 struct GNUNET_DISK_FileHandle *ret;
@@ -1314,17 +1323,17 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1314#ifndef MINGW 1323#ifndef MINGW
1315 mode = 0; 1324 mode = 0;
1316 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) 1325 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1317 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ 1326 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1318 else if (flags & GNUNET_DISK_OPEN_READ) 1327 else if (flags & GNUNET_DISK_OPEN_READ)
1319 oflags = O_RDONLY; 1328 oflags = O_RDONLY;
1320 else if (flags & GNUNET_DISK_OPEN_WRITE) 1329 else if (flags & GNUNET_DISK_OPEN_WRITE)
1321 oflags = O_WRONLY; 1330 oflags = O_WRONLY;
1322 else 1331 else
1323 { 1332 {
1324 GNUNET_break (0); 1333 GNUNET_break (0);
1325 GNUNET_free (expfn); 1334 GNUNET_free (expfn);
1326 return NULL; 1335 return NULL;
1327 } 1336 }
1328 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) 1337 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1329 oflags |= (O_CREAT | O_EXCL); 1338 oflags |= (O_CREAT | O_EXCL);
1330 if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1339 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
@@ -1332,22 +1341,22 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1332 if (flags & GNUNET_DISK_OPEN_APPEND) 1341 if (flags & GNUNET_DISK_OPEN_APPEND)
1333 oflags |= O_APPEND; 1342 oflags |= O_APPEND;
1334 if (flags & GNUNET_DISK_OPEN_CREATE) 1343 if (flags & GNUNET_DISK_OPEN_CREATE)
1335 { 1344 {
1336 (void) GNUNET_DISK_directory_create_for_file (expfn); 1345 (void) GNUNET_DISK_directory_create_for_file (expfn);
1337 oflags |= O_CREAT; 1346 oflags |= O_CREAT;
1338 mode = translate_unix_perms (perm); 1347 mode = translate_unix_perms (perm);
1339 } 1348 }
1340 1349
1341 fd = open (expfn, oflags | O_LARGEFILE, mode); 1350 fd = open (expfn, oflags | O_LARGEFILE, mode);
1342 if (fd == -1) 1351 if (fd == -1)
1343 { 1352 {
1344 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) 1353 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1345 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn); 1354 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1346 else 1355 else
1347 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_DEBUG, "open", expfn); 1356 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1348 GNUNET_free (expfn); 1357 GNUNET_free (expfn);
1349 return NULL; 1358 return NULL;
1350 } 1359 }
1351#else 1360#else
1352 access = 0; 1361 access = 0;
1353 disp = OPEN_ALWAYS; 1362 disp = OPEN_ALWAYS;
@@ -1360,48 +1369,48 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1360 access = FILE_WRITE_DATA; 1369 access = FILE_WRITE_DATA;
1361 1370
1362 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) 1371 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1363 { 1372 {
1364 disp = CREATE_NEW; 1373 disp = CREATE_NEW;
1365 } 1374 }
1366 else if (flags & GNUNET_DISK_OPEN_CREATE) 1375 else if (flags & GNUNET_DISK_OPEN_CREATE)
1367 { 1376 {
1368 (void) GNUNET_DISK_directory_create_for_file (expfn); 1377 (void) GNUNET_DISK_directory_create_for_file (expfn);
1369 if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1378 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1370 disp = CREATE_ALWAYS; 1379 disp = CREATE_ALWAYS;
1371 else 1380 else
1372 disp = OPEN_ALWAYS; 1381 disp = OPEN_ALWAYS;
1373 } 1382 }
1374 else if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1383 else if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1375 { 1384 {
1376 disp = TRUNCATE_EXISTING; 1385 disp = TRUNCATE_EXISTING;
1377 } 1386 }
1378 else 1387 else
1379 { 1388 {
1380 disp = OPEN_EXISTING; 1389 disp = OPEN_EXISTING;
1381 } 1390 }
1382 1391
1383 /* TODO: access priviledges? */ 1392 /* TODO: access priviledges? */
1384 h = CreateFile (expfn, access, 1393 h = CreateFile (expfn, access,
1385 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 1394 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
1386 disp, FILE_ATTRIBUTE_NORMAL, NULL); 1395 NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
1387 if (h == INVALID_HANDLE_VALUE) 1396 if (h == INVALID_HANDLE_VALUE)
1388 {
1389 SetErrnoFromWinError (GetLastError ());
1390 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1391 GNUNET_free (expfn);
1392 return NULL;
1393 }
1394
1395 if (flags & GNUNET_DISK_OPEN_APPEND)
1396 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1397 { 1397 {
1398 SetErrnoFromWinError (GetLastError ()); 1398 SetErrnoFromWinError (GetLastError ());
1399 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", 1399 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1400 expfn);
1401 CloseHandle (h);
1402 GNUNET_free (expfn); 1400 GNUNET_free (expfn);
1403 return NULL; 1401 return NULL;
1404 } 1402 }
1403
1404 if (flags & GNUNET_DISK_OPEN_APPEND)
1405 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1406 {
1407 SetErrnoFromWinError (GetLastError ());
1408 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer",
1409 expfn);
1410 CloseHandle (h);
1411 GNUNET_free (expfn);
1412 return NULL;
1413 }
1405#endif 1414#endif
1406 1415
1407 ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle)); 1416 ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
@@ -1425,28 +1434,28 @@ int
1425GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) 1434GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1426{ 1435{
1427 if (h == NULL) 1436 if (h == NULL)
1428 { 1437 {
1429 errno = EINVAL; 1438 errno = EINVAL;
1430 return GNUNET_SYSERR; 1439 return GNUNET_SYSERR;
1431 } 1440 }
1432 1441
1433#if MINGW 1442#if MINGW
1434 if (!CloseHandle (h->h)) 1443 if (!CloseHandle (h->h))
1435 { 1444 {
1436 SetErrnoFromWinError (GetLastError ()); 1445 SetErrnoFromWinError (GetLastError ());
1437 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); 1446 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1438 GNUNET_free (h->oOverlapRead); 1447 GNUNET_free (h->oOverlapRead);
1439 GNUNET_free (h->oOverlapWrite); 1448 GNUNET_free (h->oOverlapWrite);
1440 GNUNET_free (h); 1449 GNUNET_free (h);
1441 return GNUNET_SYSERR; 1450 return GNUNET_SYSERR;
1442 } 1451 }
1443#else 1452#else
1444 if (close (h->fd) != 0) 1453 if (close (h->fd) != 0)
1445 { 1454 {
1446 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); 1455 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1447 GNUNET_free (h); 1456 GNUNET_free (h);
1448 return GNUNET_SYSERR; 1457 return GNUNET_SYSERR;
1449 } 1458 }
1450#endif 1459#endif
1451 GNUNET_free (h); 1460 GNUNET_free (h);
1452 return GNUNET_OK; 1461 return GNUNET_OK;
@@ -1469,7 +1478,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1469 */ 1478 */
1470char * 1479char *
1471GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, 1480GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1472 const char *serviceName, ...) 1481 const char *serviceName, ...)
1473{ 1482{
1474 const char *c; 1483 const char *c;
1475 char *pfx; 1484 char *pfx;
@@ -1478,44 +1487,45 @@ GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1478 unsigned int needed; 1487 unsigned int needed;
1479 1488
1480 if (GNUNET_OK != 1489 if (GNUNET_OK !=
1481 GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME", &pfx)) 1490 GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME",
1491 &pfx))
1482 return NULL; 1492 return NULL;
1483 if (pfx == NULL) 1493 if (pfx == NULL)
1484 { 1494 {
1485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1495 LOG (GNUNET_ERROR_TYPE_WARNING,
1486 _("No `%s' specified for service `%s' in configuration.\n"), 1496 _("No `%s' specified for service `%s' in configuration.\n"),
1487 "HOME", serviceName); 1497 "HOME", serviceName);
1488 return NULL; 1498 return NULL;
1489 } 1499 }
1490 needed = strlen (pfx) + 2; 1500 needed = strlen (pfx) + 2;
1491 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\')) 1501 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1492 needed++; 1502 needed++;
1493 va_start (ap, serviceName); 1503 va_start (ap, serviceName);
1494 while (1) 1504 while (1)
1495 { 1505 {
1496 c = va_arg (ap, const char *); 1506 c = va_arg (ap, const char *);
1497 1507
1498 if (c == NULL) 1508 if (c == NULL)
1499 break; 1509 break;
1500 needed += strlen (c); 1510 needed += strlen (c);
1501 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) 1511 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1502 needed++; 1512 needed++;
1503 } 1513 }
1504 va_end (ap); 1514 va_end (ap);
1505 ret = GNUNET_malloc (needed); 1515 ret = GNUNET_malloc (needed);
1506 strcpy (ret, pfx); 1516 strcpy (ret, pfx);
1507 GNUNET_free (pfx); 1517 GNUNET_free (pfx);
1508 va_start (ap, serviceName); 1518 va_start (ap, serviceName);
1509 while (1) 1519 while (1)
1510 { 1520 {
1511 c = va_arg (ap, const char *); 1521 c = va_arg (ap, const char *);
1512 1522
1513 if (c == NULL) 1523 if (c == NULL)
1514 break; 1524 break;
1515 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) 1525 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1516 strcat (ret, DIR_SEPARATOR_STR); 1526 strcat (ret, DIR_SEPARATOR_STR);
1517 strcat (ret, c); 1527 strcat (ret, c);
1518 } 1528 }
1519 va_end (ap); 1529 va_end (ap);
1520 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\')) 1530 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1521 (void) GNUNET_DISK_directory_create_for_file (ret); 1531 (void) GNUNET_DISK_directory_create_for_file (ret);
@@ -1564,56 +1574,56 @@ struct GNUNET_DISK_MapHandle
1564 */ 1574 */
1565void * 1575void *
1566GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, 1576GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1567 struct GNUNET_DISK_MapHandle **m, 1577 struct GNUNET_DISK_MapHandle **m,
1568 enum GNUNET_DISK_MapType access, size_t len) 1578 enum GNUNET_DISK_MapType access, size_t len)
1569{ 1579{
1570 if (h == NULL) 1580 if (h == NULL)
1571 { 1581 {
1572 errno = EINVAL; 1582 errno = EINVAL;
1573 return NULL; 1583 return NULL;
1574 } 1584 }
1575 1585
1576#ifdef MINGW 1586#ifdef MINGW
1577 DWORD mapAccess, protect; 1587 DWORD mapAccess, protect;
1578 1588
1579 if ((access & GNUNET_DISK_MAP_TYPE_READ) && 1589 if ((access & GNUNET_DISK_MAP_TYPE_READ) &&
1580 (access & GNUNET_DISK_MAP_TYPE_WRITE)) 1590 (access & GNUNET_DISK_MAP_TYPE_WRITE))
1581 { 1591 {
1582 protect = PAGE_READWRITE; 1592 protect = PAGE_READWRITE;
1583 mapAccess = FILE_MAP_ALL_ACCESS; 1593 mapAccess = FILE_MAP_ALL_ACCESS;
1584 } 1594 }
1585 else if (access & GNUNET_DISK_MAP_TYPE_READ) 1595 else if (access & GNUNET_DISK_MAP_TYPE_READ)
1586 { 1596 {
1587 protect = PAGE_READONLY; 1597 protect = PAGE_READONLY;
1588 mapAccess = FILE_MAP_READ; 1598 mapAccess = FILE_MAP_READ;
1589 } 1599 }
1590 else if (access & GNUNET_DISK_MAP_TYPE_WRITE) 1600 else if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1591 { 1601 {
1592 protect = PAGE_READWRITE; 1602 protect = PAGE_READWRITE;
1593 mapAccess = FILE_MAP_WRITE; 1603 mapAccess = FILE_MAP_WRITE;
1594 } 1604 }
1595 else 1605 else
1596 { 1606 {
1597 GNUNET_break (0); 1607 GNUNET_break (0);
1598 return NULL; 1608 return NULL;
1599 } 1609 }
1600 1610
1601 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle)); 1611 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1602 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL); 1612 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1603 if ((*m)->h == INVALID_HANDLE_VALUE) 1613 if ((*m)->h == INVALID_HANDLE_VALUE)
1604 { 1614 {
1605 SetErrnoFromWinError (GetLastError ()); 1615 SetErrnoFromWinError (GetLastError ());
1606 GNUNET_free (*m); 1616 GNUNET_free (*m);
1607 return NULL; 1617 return NULL;
1608 } 1618 }
1609 1619
1610 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); 1620 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1611 if (!(*m)->addr) 1621 if (!(*m)->addr)
1612 { 1622 {
1613 SetErrnoFromWinError (GetLastError ()); 1623 SetErrnoFromWinError (GetLastError ());
1614 CloseHandle ((*m)->h); 1624 CloseHandle ((*m)->h);
1615 GNUNET_free (*m); 1625 GNUNET_free (*m);
1616 } 1626 }
1617 1627
1618 return (*m)->addr; 1628 return (*m)->addr;
1619#else 1629#else
@@ -1628,10 +1638,10 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1628 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0); 1638 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1629 GNUNET_assert (NULL != (*m)->addr); 1639 GNUNET_assert (NULL != (*m)->addr);
1630 if (MAP_FAILED == (*m)->addr) 1640 if (MAP_FAILED == (*m)->addr)
1631 { 1641 {
1632 GNUNET_free (*m); 1642 GNUNET_free (*m);
1633 return NULL; 1643 return NULL;
1634 } 1644 }
1635 (*m)->len = len; 1645 (*m)->len = len;
1636 return (*m)->addr; 1646 return (*m)->addr;
1637#endif 1647#endif
@@ -1648,20 +1658,20 @@ GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1648 int ret; 1658 int ret;
1649 1659
1650 if (h == NULL) 1660 if (h == NULL)
1651 { 1661 {
1652 errno = EINVAL; 1662 errno = EINVAL;
1653 return GNUNET_SYSERR; 1663 return GNUNET_SYSERR;
1654 } 1664 }
1655 1665
1656#ifdef MINGW 1666#ifdef MINGW
1657 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; 1667 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR;
1658 if (ret != GNUNET_OK) 1668 if (ret != GNUNET_OK)
1659 SetErrnoFromWinError (GetLastError ()); 1669 SetErrnoFromWinError (GetLastError ());
1660 if (!CloseHandle (h->h) && (ret == GNUNET_OK)) 1670 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1661 { 1671 {
1662 ret = GNUNET_SYSERR; 1672 ret = GNUNET_SYSERR;
1663 SetErrnoFromWinError (GetLastError ()); 1673 SetErrnoFromWinError (GetLastError ());
1664 } 1674 }
1665#else 1675#else
1666 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; 1676 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1667#endif 1677#endif
@@ -1679,10 +1689,10 @@ int
1679GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) 1689GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1680{ 1690{
1681 if (h == NULL) 1691 if (h == NULL)
1682 { 1692 {
1683 errno = EINVAL; 1693 errno = EINVAL;
1684 return GNUNET_SYSERR; 1694 return GNUNET_SYSERR;
1685 } 1695 }
1686 1696
1687#ifdef MINGW 1697#ifdef MINGW
1688 int ret; 1698 int ret;
@@ -1711,8 +1721,8 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1711 unlike CreatePipe, which returns a bool for success or failure. */ 1721 unlike CreatePipe, which returns a bool for success or failure. */
1712static int 1722static int
1713create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, 1723create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
1714 LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize, 1724 LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize,
1715 DWORD dwReadMode, DWORD dwWriteMode) 1725 DWORD dwReadMode, DWORD dwWriteMode)
1716{ 1726{
1717 /* Default to error. */ 1727 /* Default to error. */
1718 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; 1728 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE;
@@ -1729,106 +1739,105 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
1729 * Retrying will probably never be necessary, but we want 1739 * Retrying will probably never be necessary, but we want
1730 * to be as robust as possible. */ 1740 * to be as robust as possible. */
1731 while (1) 1741 while (1)
1732 { 1742 {
1733 static volatile LONG pipe_unique_id; 1743 static volatile LONG pipe_unique_id;
1734 1744
1735 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", 1745 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld",
1736 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); 1746 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id));
1737#if DEBUG_PIPE 1747#if DEBUG_PIPE
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1748 LOG (GNUNET_ERROR_TYPE_DEBUG,
1739 "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize); 1749 "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize);
1740#endif 1750#endif
1741 /* Use CreateNamedPipe instead of CreatePipe, because the latter 1751 /* Use CreateNamedPipe instead of CreatePipe, because the latter
1742 * returns a write handle that does not permit FILE_READ_ATTRIBUTES 1752 * returns a write handle that does not permit FILE_READ_ATTRIBUTES
1743 * access, on versions of win32 earlier than WinXP SP2. 1753 * access, on versions of win32 earlier than WinXP SP2.
1744 * CreatePipe also stupidly creates a full duplex pipe, which is 1754 * CreatePipe also stupidly creates a full duplex pipe, which is
1745 * a waste, since only a single direction is actually used. 1755 * a waste, since only a single direction is actually used.
1746 * It's important to only allow a single instance, to ensure that 1756 * It's important to only allow a single instance, to ensure that
1747 * the pipe was not created earlier by some other process, even if 1757 * the pipe was not created earlier by some other process, even if
1748 * the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE 1758 * the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE
1749 * because that is only available for Win2k SP2 and WinXP. */ 1759 * because that is only available for Win2k SP2 and WinXP. */
1750 read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */ 1760 read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */
1751 psize, /* output buffer size */ 1761 psize, /* output buffer size */
1752 psize, /* input buffer size */ 1762 psize, /* input buffer size */
1753 NMPWAIT_USE_DEFAULT_WAIT, sa_ptr); 1763 NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
1754 1764
1755 if (read_pipe != INVALID_HANDLE_VALUE) 1765 if (read_pipe != INVALID_HANDLE_VALUE)
1756 { 1766 {
1757#if DEBUG_PIPE 1767#if DEBUG_PIPE
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", 1768 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe);
1759 read_pipe);
1760#endif 1769#endif
1761 break; 1770 break;
1762 } 1771 }
1763 1772
1764 DWORD err = GetLastError (); 1773 DWORD err = GetLastError ();
1765 1774
1766 switch (err) 1775 switch (err)
1767 { 1776 {
1768 case ERROR_PIPE_BUSY: 1777 case ERROR_PIPE_BUSY:
1769 /* The pipe is already open with compatible parameters. 1778 /* The pipe is already open with compatible parameters.
1770 * Pick a new name and retry. */ 1779 * Pick a new name and retry. */
1771#if DEBUG_PIPE 1780#if DEBUG_PIPE
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); 1781 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n");
1773#endif 1782#endif
1774 continue; 1783 continue;
1775 case ERROR_ACCESS_DENIED: 1784 case ERROR_ACCESS_DENIED:
1776 /* The pipe is already open with incompatible parameters. 1785 /* The pipe is already open with incompatible parameters.
1777 * Pick a new name and retry. */ 1786 * Pick a new name and retry. */
1778#if DEBUG_PIPE 1787#if DEBUG_PIPE
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); 1788 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n");
1780#endif 1789#endif
1781 continue; 1790 continue;
1782 case ERROR_CALL_NOT_IMPLEMENTED: 1791 case ERROR_CALL_NOT_IMPLEMENTED:
1783 /* We are on an older Win9x platform without named pipes. 1792 /* We are on an older Win9x platform without named pipes.
1784 * Return an anonymous pipe as the best approximation. */ 1793 * Return an anonymous pipe as the best approximation. */
1785#if DEBUG_PIPE 1794#if DEBUG_PIPE
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1795 LOG (GNUNET_ERROR_TYPE_DEBUG,
1787 "CreateNamedPipe not implemented, resorting to " 1796 "CreateNamedPipe not implemented, resorting to "
1788 "CreatePipe: size = %lu\n", psize); 1797 "CreatePipe: size = %lu\n", psize);
1789#endif 1798#endif
1790 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) 1799 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize))
1791 { 1800 {
1792#if DEBUG_PIPE 1801#if DEBUG_PIPE
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", 1802 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n",
1794 *read_pipe_ptr); 1803 *read_pipe_ptr);
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", 1804 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n",
1796 *write_pipe_ptr); 1805 *write_pipe_ptr);
1797#endif 1806#endif
1798 return GNUNET_OK; 1807 return GNUNET_OK;
1799 } 1808 }
1800 err = GetLastError (); 1809 err = GetLastError ();
1801 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); 1810 LOG (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err);
1802 return err; 1811 return err;
1803 default: 1812 default:
1804 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); 1813 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err);
1805 return err; 1814 return err;
1815 }
1816 /* NOTREACHED */
1806 } 1817 }
1807 /* NOTREACHED */
1808 }
1809#if DEBUG_PIPE 1818#if DEBUG_PIPE
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); 1819 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename);
1811#endif 1820#endif
1812 1821
1813 /* Open the named pipe for writing. 1822 /* Open the named pipe for writing.
1814 * Be sure to permit FILE_READ_ATTRIBUTES access. */ 1823 * Be sure to permit FILE_READ_ATTRIBUTES access. */
1815 write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */ 1824 write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */
1816 sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */ 1825 sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */
1817 0); /* handle to template file */ 1826 0); /* handle to template file */
1818 1827
1819 if (write_pipe == INVALID_HANDLE_VALUE) 1828 if (write_pipe == INVALID_HANDLE_VALUE)
1820 { 1829 {
1821 /* Failure. */ 1830 /* Failure. */
1822 DWORD err = GetLastError (); 1831 DWORD err = GetLastError ();
1823 1832
1824#if DEBUG_PIPE 1833#if DEBUG_PIPE
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); 1834 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err);
1826#endif 1835#endif
1827 CloseHandle (read_pipe); 1836 CloseHandle (read_pipe);
1828 return err; 1837 return err;
1829 } 1838 }
1830#if DEBUG_PIPE 1839#if DEBUG_PIPE
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); 1840 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe);
1832#endif 1841#endif
1833 /* Success. */ 1842 /* Success. */
1834 *read_pipe_ptr = read_pipe; 1843 *read_pipe_ptr = read_pipe;
@@ -1853,7 +1862,7 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1853 struct GNUNET_DISK_FileHandle *fds; 1862 struct GNUNET_DISK_FileHandle *fds;
1854 1863
1855 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + 1864 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) +
1856 2 * sizeof (struct GNUNET_DISK_FileHandle)); 1865 2 * sizeof (struct GNUNET_DISK_FileHandle));
1857 fds = (struct GNUNET_DISK_FileHandle *) &p[1]; 1866 fds = (struct GNUNET_DISK_FileHandle *) &p[1];
1858 p->fd[0] = &fds[0]; 1867 p->fd[0] = &fds[0];
1859 p->fd[1] = &fds[1]; 1868 p->fd[1] = &fds[1];
@@ -1865,13 +1874,13 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1865 1874
1866 ret = pipe (fd); 1875 ret = pipe (fd);
1867 if (ret == -1) 1876 if (ret == -1)
1868 { 1877 {
1869 eno = errno; 1878 eno = errno;
1870 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); 1879 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1871 GNUNET_free (p); 1880 GNUNET_free (p);
1872 errno = eno; 1881 errno = eno;
1873 return NULL; 1882 return NULL;
1874 } 1883 }
1875 p->fd[0]->fd = fd[0]; 1884 p->fd[0]->fd = fd[0];
1876 p->fd[1]->fd = fd[1]; 1885 p->fd[1]->fd = fd[1];
1877 ret = 0; 1886 ret = 0;
@@ -1895,62 +1904,62 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1895 if (0 > fcntl (fd[1], F_SETFD, flags)) 1904 if (0 > fcntl (fd[1], F_SETFD, flags))
1896 ret = -1; 1905 ret = -1;
1897 if (ret == -1) 1906 if (ret == -1)
1898 { 1907 {
1899 eno = errno; 1908 eno = errno;
1900 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fcntl"); 1909 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl");
1901 GNUNET_break (0 == close (p->fd[0]->fd)); 1910 GNUNET_break (0 == close (p->fd[0]->fd));
1902 GNUNET_break (0 == close (p->fd[1]->fd)); 1911 GNUNET_break (0 == close (p->fd[1]->fd));
1903 GNUNET_free (p); 1912 GNUNET_free (p);
1904 errno = eno; 1913 errno = eno;
1905 return NULL; 1914 return NULL;
1906 } 1915 }
1907#else 1916#else
1908 BOOL ret; 1917 BOOL ret;
1909 HANDLE tmp_handle; 1918 HANDLE tmp_handle;
1910 1919
1911 ret = 1920 ret =
1912 create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, 1921 create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0,
1913 FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED); 1922 FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED);
1914 if (!ret) 1923 if (!ret)
1915 { 1924 {
1916 GNUNET_free (p); 1925 GNUNET_free (p);
1917 SetErrnoFromWinError (GetLastError ()); 1926 SetErrnoFromWinError (GetLastError ());
1918 return NULL; 1927 return NULL;
1919 } 1928 }
1920 if (!DuplicateHandle 1929 if (!DuplicateHandle
1921 (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 0, 1930 (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle,
1922 inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) 1931 0, inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
1923 { 1932 {
1924 SetErrnoFromWinError (GetLastError ()); 1933 SetErrnoFromWinError (GetLastError ());
1925 CloseHandle (p->fd[0]->h); 1934 CloseHandle (p->fd[0]->h);
1926 CloseHandle (p->fd[1]->h); 1935 CloseHandle (p->fd[1]->h);
1927 GNUNET_free (p); 1936 GNUNET_free (p);
1928 return NULL; 1937 return NULL;
1929 } 1938 }
1930 CloseHandle (p->fd[0]->h); 1939 CloseHandle (p->fd[0]->h);
1931 p->fd[0]->h = tmp_handle; 1940 p->fd[0]->h = tmp_handle;
1932 1941
1933 if (!DuplicateHandle 1942 if (!DuplicateHandle
1934 (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 0, 1943 (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle,
1935 inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) 1944 0, inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
1936 { 1945 {
1937 SetErrnoFromWinError (GetLastError ()); 1946 SetErrnoFromWinError (GetLastError ());
1938 CloseHandle (p->fd[0]->h); 1947 CloseHandle (p->fd[0]->h);
1939 CloseHandle (p->fd[1]->h); 1948 CloseHandle (p->fd[1]->h);
1940 GNUNET_free (p); 1949 GNUNET_free (p);
1941 return NULL; 1950 return NULL;
1942 } 1951 }
1943 CloseHandle (p->fd[1]->h); 1952 CloseHandle (p->fd[1]->h);
1944 p->fd[1]->h = tmp_handle; 1953 p->fd[1]->h = tmp_handle;
1945 if (!blocking) 1954 if (!blocking)
1946 { 1955 {
1947 DWORD mode; 1956 DWORD mode;
1948 1957
1949 mode = PIPE_NOWAIT; 1958 mode = PIPE_NOWAIT;
1950 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); 1959 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
1951 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); 1960 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
1952 /* this always fails on Windows 95, so we don't care about error handling */ 1961 /* this always fails on Windows 95, so we don't care about error handling */
1953 } 1962 }
1954 p->fd[0]->type = GNUNET_PIPE; 1963 p->fd[0]->type = GNUNET_PIPE;
1955 p->fd[1]->type = GNUNET_PIPE; 1964 p->fd[1]->type = GNUNET_PIPE;
1956 1965
@@ -1979,51 +1988,51 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1979 */ 1988 */
1980int 1989int
1981GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, 1990GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
1982 enum GNUNET_DISK_PipeEnd end) 1991 enum GNUNET_DISK_PipeEnd end)
1983{ 1992{
1984 int ret = GNUNET_OK; 1993 int ret = GNUNET_OK;
1985 int save; 1994 int save;
1986 1995
1987#ifdef MINGW 1996#ifdef MINGW
1988 if (end == GNUNET_DISK_PIPE_END_READ) 1997 if (end == GNUNET_DISK_PIPE_END_READ)
1989 {
1990 if (!CloseHandle (p->fd[0]->h))
1991 { 1998 {
1992 SetErrnoFromWinError (GetLastError ()); 1999 if (!CloseHandle (p->fd[0]->h))
1993 ret = GNUNET_SYSERR; 2000 {
2001 SetErrnoFromWinError (GetLastError ());
2002 ret = GNUNET_SYSERR;
2003 }
2004 p->fd[0]->h = INVALID_HANDLE_VALUE;
1994 } 2005 }
1995 p->fd[0]->h = INVALID_HANDLE_VALUE;
1996 }
1997 else if (end == GNUNET_DISK_PIPE_END_WRITE) 2006 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1998 {
1999 if (!CloseHandle (p->fd[1]->h))
2000 { 2007 {
2001 SetErrnoFromWinError (GetLastError ()); 2008 if (!CloseHandle (p->fd[1]->h))
2002 ret = GNUNET_SYSERR; 2009 {
2010 SetErrnoFromWinError (GetLastError ());
2011 ret = GNUNET_SYSERR;
2012 }
2013 p->fd[1]->h = INVALID_HANDLE_VALUE;
2003 } 2014 }
2004 p->fd[1]->h = INVALID_HANDLE_VALUE;
2005 }
2006 save = errno; 2015 save = errno;
2007#else 2016#else
2008 save = 0; 2017 save = 0;
2009 if (end == GNUNET_DISK_PIPE_END_READ) 2018 if (end == GNUNET_DISK_PIPE_END_READ)
2010 {
2011 if (0 != close (p->fd[0]->fd))
2012 { 2019 {
2013 ret = GNUNET_SYSERR; 2020 if (0 != close (p->fd[0]->fd))
2014 save = errno; 2021 {
2022 ret = GNUNET_SYSERR;
2023 save = errno;
2024 }
2025 p->fd[0]->fd = -1;
2015 } 2026 }
2016 p->fd[0]->fd = -1;
2017 }
2018 else if (end == GNUNET_DISK_PIPE_END_WRITE) 2027 else if (end == GNUNET_DISK_PIPE_END_WRITE)
2019 {
2020 if (0 != close (p->fd[1]->fd))
2021 { 2028 {
2022 ret = GNUNET_SYSERR; 2029 if (0 != close (p->fd[1]->fd))
2023 save = errno; 2030 {
2031 ret = GNUNET_SYSERR;
2032 save = errno;
2033 }
2034 p->fd[1]->fd = -1;
2024 } 2035 }
2025 p->fd[1]->fd = -1;
2026 }
2027#endif 2036#endif
2028 errno = save; 2037 errno = save;
2029 return ret; 2038 return ret;
@@ -2043,35 +2052,35 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
2043 2052
2044#ifdef MINGW 2053#ifdef MINGW
2045 if (!CloseHandle (p->fd[0]->h)) 2054 if (!CloseHandle (p->fd[0]->h))
2046 { 2055 {
2047 SetErrnoFromWinError (GetLastError ()); 2056 SetErrnoFromWinError (GetLastError ());
2048 ret = GNUNET_SYSERR; 2057 ret = GNUNET_SYSERR;
2049 } 2058 }
2050 if (!CloseHandle (p->fd[1]->h)) 2059 if (!CloseHandle (p->fd[1]->h))
2051 { 2060 {
2052 SetErrnoFromWinError (GetLastError ()); 2061 SetErrnoFromWinError (GetLastError ());
2053 ret = GNUNET_SYSERR; 2062 ret = GNUNET_SYSERR;
2054 } 2063 }
2055 save = errno; 2064 save = errno;
2056#else 2065#else
2057 save = 0; 2066 save = 0;
2058 if (p->fd[0]->fd != -1) 2067 if (p->fd[0]->fd != -1)
2059 {
2060 if (0 != close (p->fd[0]->fd))
2061 { 2068 {
2062 ret = GNUNET_SYSERR; 2069 if (0 != close (p->fd[0]->fd))
2063 save = errno; 2070 {
2071 ret = GNUNET_SYSERR;
2072 save = errno;
2073 }
2064 } 2074 }
2065 }
2066 2075
2067 if (p->fd[1]->fd != -1) 2076 if (p->fd[1]->fd != -1)
2068 {
2069 if (0 != close (p->fd[1]->fd))
2070 { 2077 {
2071 ret = GNUNET_SYSERR; 2078 if (0 != close (p->fd[1]->fd))
2072 save = errno; 2079 {
2080 ret = GNUNET_SYSERR;
2081 save = errno;
2082 }
2073 } 2083 }
2074 }
2075#endif 2084#endif
2076 GNUNET_free (p); 2085 GNUNET_free (p);
2077 errno = save; 2086 errno = save;
@@ -2088,7 +2097,7 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
2088 */ 2097 */
2089struct GNUNET_DISK_FileHandle * 2098struct GNUNET_DISK_FileHandle *
2090GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags, 2099GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2091 enum GNUNET_DISK_AccessPermissions perm) 2100 enum GNUNET_DISK_AccessPermissions perm)
2092{ 2101{
2093#ifdef MINGW 2102#ifdef MINGW
2094 struct GNUNET_DISK_FileHandle *ret; 2103 struct GNUNET_DISK_FileHandle *ret;
@@ -2108,70 +2117,71 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2108 openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE; 2117 openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE;
2109 2118
2110 while (h == NULL) 2119 while (h == NULL)
2111 {
2112 DWORD error_code;
2113
2114 name = NULL;
2115 if (*fn != NULL)
2116 { 2120 {
2117 GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn); 2121 DWORD error_code;
2122
2123 name = NULL;
2124 if (*fn != NULL)
2125 {
2126 GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn);
2118#if DEBUG_NPIPE 2127#if DEBUG_NPIPE
2119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2128 LOG (GNUNET_ERROR_TYPE_DEBUG,
2120 "Trying to create an instance of named pipe `%s'\n", name); 2129 "Trying to create an instance of named pipe `%s'\n", name);
2121#endif 2130#endif
2122 h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED, 2131 h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED,
2123 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, 2132 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1,
2124 NULL); 2133 0, NULL);
2125 } 2134 }
2126 else 2135 else
2127 { 2136 {
2128 GNUNET_asprintf (fn, "\\\\.\\pipe\\gnunet-%llu", 2137 GNUNET_asprintf (fn, "\\\\.\\pipe\\gnunet-%llu",
2129 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 2138 GNUNET_CRYPTO_random_u64
2130 UINT64_MAX)); 2139 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
2131#if DEBUG_NPIPE 2140#if DEBUG_NPIPE
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2141 LOG (GNUNET_ERROR_TYPE_DEBUG,
2133 "Trying to create unique named pipe `%s'\n", *fn); 2142 "Trying to create unique named pipe `%s'\n", *fn);
2134#endif 2143#endif
2135 h = CreateNamedPipe (*fn, 2144 h = CreateNamedPipe (*fn,
2136 openMode | FILE_FLAG_OVERLAPPED | 2145 openMode | FILE_FLAG_OVERLAPPED |
2137 FILE_FLAG_FIRST_PIPE_INSTANCE, 2146 FILE_FLAG_FIRST_PIPE_INSTANCE,
2138 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, 2147 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1,
2139 NULL); 2148 0, NULL);
2140 } 2149 }
2141 error_code = GetLastError (); 2150 error_code = GetLastError ();
2142 if (name) 2151 if (name)
2143 GNUNET_free (name); 2152 GNUNET_free (name);
2144 /* don't re-set name to NULL yet */ 2153 /* don't re-set name to NULL yet */
2145 if (h == INVALID_HANDLE_VALUE) 2154 if (h == INVALID_HANDLE_VALUE)
2146 { 2155 {
2147 SetErrnoFromWinError (error_code); 2156 SetErrnoFromWinError (error_code);
2148#if DEBUG_NPIPE 2157#if DEBUG_NPIPE
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2158 LOG (GNUNET_ERROR_TYPE_DEBUG,
2150 "Pipe creation have failed because of %d, errno is %d\n", 2159 "Pipe creation have failed because of %d, errno is %d\n",
2151 error_code, errno); 2160 error_code, errno);
2152#endif 2161#endif
2153 if (name == NULL) 2162 if (name == NULL)
2154 { 2163 {
2155#if DEBUG_NPIPE 2164#if DEBUG_NPIPE
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2165 LOG (GNUNET_ERROR_TYPE_DEBUG,
2157 "Pipe was to be unique, considering re-creation\n"); 2166 "Pipe was to be unique, considering re-creation\n");
2158#endif 2167#endif
2159 GNUNET_free (*fn); 2168 GNUNET_free (*fn);
2160 *fn = NULL; 2169 *fn = NULL;
2161 if (error_code != ERROR_ACCESS_DENIED && error_code != ERROR_PIPE_BUSY) 2170 if (error_code != ERROR_ACCESS_DENIED
2162 { 2171 && error_code != ERROR_PIPE_BUSY)
2163 return NULL; 2172 {
2164 } 2173 return NULL;
2174 }
2165#if DEBUG_NPIPE 2175#if DEBUG_NPIPE
2166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2176 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "Pipe name was not unique, trying again\n"); 2177 "Pipe name was not unique, trying again\n");
2168#endif 2178#endif
2169 h = NULL; 2179 h = NULL;
2170 } 2180 }
2171 else 2181 else
2172 return NULL; 2182 return NULL;
2183 }
2173 } 2184 }
2174 }
2175 errno = 0; 2185 errno = 0;
2176 2186
2177 ret = GNUNET_malloc (sizeof (*ret)); 2187 ret = GNUNET_malloc (sizeof (*ret));
@@ -2187,22 +2197,22 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2187 return ret; 2197 return ret;
2188#else 2198#else
2189 if (*fn == NULL) 2199 if (*fn == NULL)
2190 {
2191 char dir[] = "/tmp/gnunet-pipe-XXXXXX";
2192
2193 if (mkdtemp (dir) == NULL)
2194 { 2200 {
2195 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "mkdtemp"); 2201 char dir[] = "/tmp/gnunet-pipe-XXXXXX";
2196 return NULL; 2202
2203 if (mkdtemp (dir) == NULL)
2204 {
2205 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "mkdtemp");
2206 return NULL;
2207 }
2208 GNUNET_asprintf (fn, "%s/child-control", dir);
2197 } 2209 }
2198 GNUNET_asprintf (fn, "%s/child-control", dir);
2199 }
2200 2210
2201 if (mkfifo (*fn, translate_unix_perms (perm)) == -1) 2211 if (mkfifo (*fn, translate_unix_perms (perm)) == -1)
2202 { 2212 {
2203 if ((errno != EEXIST) || (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))) 2213 if ((errno != EEXIST) || (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)))
2204 return NULL; 2214 return NULL;
2205 } 2215 }
2206 2216
2207 flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS); 2217 flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS);
2208 return GNUNET_DISK_file_open (*fn, flags, perm); 2218 return GNUNET_DISK_file_open (*fn, flags, perm);
@@ -2220,7 +2230,7 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2220 */ 2230 */
2221struct GNUNET_DISK_FileHandle * 2231struct GNUNET_DISK_FileHandle *
2222GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, 2232GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
2223 enum GNUNET_DISK_AccessPermissions perm) 2233 enum GNUNET_DISK_AccessPermissions perm)
2224{ 2234{
2225#ifdef MINGW 2235#ifdef MINGW
2226 struct GNUNET_DISK_FileHandle *ret; 2236 struct GNUNET_DISK_FileHandle *ret;
@@ -2236,12 +2246,12 @@ GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
2236 openMode = GENERIC_WRITE; 2246 openMode = GENERIC_WRITE;
2237 2247
2238 h = CreateFile (fn, openMode, 0, NULL, OPEN_EXISTING, 2248 h = CreateFile (fn, openMode, 0, NULL, OPEN_EXISTING,
2239 FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL); 2249 FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL);
2240 if (h == INVALID_HANDLE_VALUE) 2250 if (h == INVALID_HANDLE_VALUE)
2241 { 2251 {
2242 SetErrnoFromWinError (GetLastError ()); 2252 SetErrnoFromWinError (GetLastError ());
2243 return NULL; 2253 return NULL;
2244 } 2254 }
2245 2255
2246 ret = GNUNET_malloc (sizeof (*ret)); 2256 ret = GNUNET_malloc (sizeof (*ret));
2247 ret->h = h; 2257 ret->h = h;
@@ -2273,10 +2283,10 @@ GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe)
2273 2283
2274 ret = CloseHandle (pipe->h); 2284 ret = CloseHandle (pipe->h);
2275 if (!ret) 2285 if (!ret)
2276 { 2286 {
2277 SetErrnoFromWinError (GetLastError ()); 2287 SetErrnoFromWinError (GetLastError ());
2278 return GNUNET_SYSERR; 2288 return GNUNET_SYSERR;
2279 } 2289 }
2280 else 2290 else
2281 return GNUNET_OK; 2291 return GNUNET_OK;
2282#endif 2292#endif
@@ -2292,17 +2302,17 @@ GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe)
2292 */ 2302 */
2293const struct GNUNET_DISK_FileHandle * 2303const struct GNUNET_DISK_FileHandle *
2294GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, 2304GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
2295 enum GNUNET_DISK_PipeEnd n) 2305 enum GNUNET_DISK_PipeEnd n)
2296{ 2306{
2297 switch (n) 2307 switch (n)
2298 { 2308 {
2299 case GNUNET_DISK_PIPE_END_READ: 2309 case GNUNET_DISK_PIPE_END_READ:
2300 case GNUNET_DISK_PIPE_END_WRITE: 2310 case GNUNET_DISK_PIPE_END_WRITE:
2301 return p->fd[n]; 2311 return p->fd[n];
2302 default: 2312 default:
2303 GNUNET_break (0); 2313 GNUNET_break (0);
2304 return NULL; 2314 return NULL;
2305 } 2315 }
2306} 2316}
2307 2317
2308 2318
@@ -2316,7 +2326,7 @@ GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
2316 */ 2326 */
2317int 2327int
2318GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, 2328GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
2319 void *dst, size_t dst_len) 2329 void *dst, size_t dst_len)
2320{ 2330{
2321#ifdef MINGW 2331#ifdef MINGW
2322 if (dst_len < sizeof (HANDLE)) 2332 if (dst_len < sizeof (HANDLE))
diff --git a/src/util/getopt.c b/src/util/getopt.c
index d48183b2a..4a36678b2 100644
--- a/src/util/getopt.c
+++ b/src/util/getopt.c
@@ -49,6 +49,10 @@ Copyright (C) 2006 Christian Grothoff
49#endif 49#endif
50#endif 50#endif
51 51
52#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
53
54#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
55
52#if defined (WIN32) && !defined (__CYGWIN32__) 56#if defined (WIN32) && !defined (__CYGWIN32__)
53/* It's not Unix, really. See? Capital letters. */ 57/* It's not Unix, really. See? Capital letters. */
54#include <windows.h> 58#include <windows.h>
@@ -194,20 +198,19 @@ static char *posixly_correct;
194/* Avoid depending on library functions or files 198/* Avoid depending on library functions or files
195 whose names are inconsistent. */ 199 whose names are inconsistent. */
196 200
197char * 201char *getenv ();
198getenv ();
199 202
200static char * 203static char *
201my_index (str, chr) 204my_index (str, chr)
202 const char *str; 205 const char *str;
203 int chr; 206 int chr;
204{ 207{
205 while (*str) 208 while (*str)
206 { 209 {
207 if (*str == chr) 210 if (*str == chr)
208 return (char *) str; 211 return (char *) str;
209 str++; 212 str++;
210 } 213 }
211 return 0; 214 return 0;
212} 215}
213 216
@@ -219,8 +222,7 @@ my_index (str, chr)
219#if !defined (__STDC__) || !__STDC__ 222#if !defined (__STDC__) || !__STDC__
220/* gcc with -traditional declares the built-in strlen to return int, 223/* gcc with -traditional declares the built-in strlen to return int,
221 and has done so at least since version 2.4.5. -- rms. */ 224 and has done so at least since version 2.4.5. -- rms. */
222extern int 225extern int strlen (const char *);
223strlen (const char *);
224#endif /* not __STDC__ */ 226#endif /* not __STDC__ */
225#endif /* __GNUC__ */ 227#endif /* __GNUC__ */
226 228
@@ -254,7 +256,7 @@ extern pid_t __libc_pid;
254 is valid for the getopt call we must make sure that the ARGV passed 256 is valid for the getopt call we must make sure that the ARGV passed
255 to getopt is that one passed to the process. */ 257 to getopt is that one passed to the process. */
256static void 258static void
257 __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv) 259 __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv)
258{ 260{
259 /* XXX This is no good solution. We should rather copy the args so 261 /* XXX This is no good solution. We should rather copy the args so
260 * that we can compare them later. But we must not use malloc(3). */ 262 * that we can compare them later. But we must not use malloc(3). */
@@ -285,13 +287,12 @@ text_set_element (__libc_subinit, store_args_and_env);
285 the new indices of the non-options in ARGV after they are moved. */ 287 the new indices of the non-options in ARGV after they are moved. */
286 288
287#if defined (__STDC__) && __STDC__ 289#if defined (__STDC__) && __STDC__
288static void 290static void exchange (char **);
289exchange (char **);
290#endif 291#endif
291 292
292static void 293static void
293exchange (argv) 294exchange (argv)
294 char **argv; 295 char **argv;
295{ 296{
296 int bottom = first_nonopt; 297 int bottom = first_nonopt;
297 int middle = last_nonopt; 298 int middle = last_nonopt;
@@ -308,61 +309,61 @@ exchange (argv)
308 * string can work normally. Our top argument must be in the range 309 * string can work normally. Our top argument must be in the range
309 * of the string. */ 310 * of the string. */
310 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 311 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
311 {
312 /* We must extend the array. The user plays games with us and
313 * presents new arguments. */
314 char *new_str = malloc (top + 1);
315
316 if (new_str == NULL)
317 nonoption_flags_len = nonoption_flags_max_len = 0;
318 else
319 { 312 {
320 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); 313 /* We must extend the array. The user plays games with us and
321 memset (&new_str[nonoption_flags_max_len], '\0', 314 * presents new arguments. */
322 top + 1 - nonoption_flags_max_len); 315 char *new_str = malloc (top + 1);
323 nonoption_flags_max_len = top + 1; 316
324 __getopt_nonoption_flags = new_str; 317 if (new_str == NULL)
318 nonoption_flags_len = nonoption_flags_max_len = 0;
319 else
320 {
321 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
322 memset (&new_str[nonoption_flags_max_len], '\0',
323 top + 1 - nonoption_flags_max_len);
324 nonoption_flags_max_len = top + 1;
325 __getopt_nonoption_flags = new_str;
326 }
325 } 327 }
326 }
327#endif 328#endif
328 329
329 while (top > middle && middle > bottom) 330 while (top > middle && middle > bottom)
330 {
331 if (top - middle > middle - bottom)
332 {
333 /* Bottom segment is the short one. */
334 int len = middle - bottom;
335 register int i;
336
337 /* Swap it with the top part of the top segment. */
338 for (i = 0; i < len; i++)
339 {
340 tem = argv[bottom + i];
341 argv[bottom + i] = argv[top - (middle - bottom) + i];
342 argv[top - (middle - bottom) + i] = tem;
343 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
344 }
345 /* Exclude the moved bottom segment from further swapping. */
346 top -= len;
347 }
348 else
349 { 331 {
350 /* Top segment is the short one. */ 332 if (top - middle > middle - bottom)
351 int len = top - middle; 333 {
352 register int i; 334 /* Bottom segment is the short one. */
353 335 int len = middle - bottom;
354 /* Swap it with the bottom part of the bottom segment. */ 336 register int i;
355 for (i = 0; i < len; i++) 337
356 { 338 /* Swap it with the top part of the top segment. */
357 tem = argv[bottom + i]; 339 for (i = 0; i < len; i++)
358 argv[bottom + i] = argv[middle + i]; 340 {
359 argv[middle + i] = tem; 341 tem = argv[bottom + i];
360 SWAP_FLAGS (bottom + i, middle + i); 342 argv[bottom + i] = argv[top - (middle - bottom) + i];
361 } 343 argv[top - (middle - bottom) + i] = tem;
362 /* Exclude the moved top segment from further swapping. */ 344 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
363 bottom += len; 345 }
346 /* Exclude the moved bottom segment from further swapping. */
347 top -= len;
348 }
349 else
350 {
351 /* Top segment is the short one. */
352 int len = top - middle;
353 register int i;
354
355 /* Swap it with the bottom part of the bottom segment. */
356 for (i = 0; i < len; i++)
357 {
358 tem = argv[bottom + i];
359 argv[bottom + i] = argv[middle + i];
360 argv[middle + i] = tem;
361 SWAP_FLAGS (bottom + i, middle + i);
362 }
363 /* Exclude the moved top segment from further swapping. */
364 bottom += len;
365 }
364 } 366 }
365 }
366 367
367 /* Update records for the slots the non-options now occupy. */ 368 /* Update records for the slots the non-options now occupy. */
368 369
@@ -373,14 +374,13 @@ exchange (argv)
373/* Initialize the internal data when the first call is made. */ 374/* Initialize the internal data when the first call is made. */
374 375
375#if defined (__STDC__) && __STDC__ 376#if defined (__STDC__) && __STDC__
376static const char * 377static const char *_getopt_initialize (int, char *const *, const char *);
377_getopt_initialize (int, char *const *, const char *);
378#endif 378#endif
379static const char * 379static const char *
380_getopt_initialize (argc, argv, optstring) 380_getopt_initialize (argc, argv, optstring)
381 int argc; 381 int argc;
382 char *const *argv; 382 char *const *argv;
383 const char *optstring; 383 const char *optstring;
384{ 384{
385 /* Start processing options with ARGV-element 1 (since ARGV-element 0 385 /* Start processing options with ARGV-element 1 (since ARGV-element 0
386 * is the program name); the sequence of previously skipped 386 * is the program name); the sequence of previously skipped
@@ -395,48 +395,50 @@ _getopt_initialize (argc, argv, optstring)
395 /* Determine how to handle the ordering of options and nonoptions. */ 395 /* Determine how to handle the ordering of options and nonoptions. */
396 396
397 if (optstring[0] == '-') 397 if (optstring[0] == '-')
398 { 398 {
399 ordering = RETURN_IN_ORDER; 399 ordering = RETURN_IN_ORDER;
400 ++optstring; 400 ++optstring;
401 } 401 }
402 else if (optstring[0] == '+') 402 else if (optstring[0] == '+')
403 { 403 {
404 ordering = REQUIRE_ORDER; 404 ordering = REQUIRE_ORDER;
405 ++optstring; 405 ++optstring;
406 } 406 }
407 else if (posixly_correct != NULL) 407 else if (posixly_correct != NULL)
408 ordering = REQUIRE_ORDER; 408 ordering = REQUIRE_ORDER;
409 else 409 else
410 ordering = PERMUTE; 410 ordering = PERMUTE;
411 411
412#ifdef _LIBC 412#ifdef _LIBC
413 if (posixly_correct == NULL && argc == original_argc && argv == original_argv) 413 if (posixly_correct == NULL && argc == original_argc
414 { 414 && argv == original_argv)
415 if (nonoption_flags_max_len == 0)
416 { 415 {
417 if (__getopt_nonoption_flags == NULL || 416 if (nonoption_flags_max_len == 0)
418 __getopt_nonoption_flags[0] == '\0') 417 {
419 nonoption_flags_max_len = -1; 418 if (__getopt_nonoption_flags == NULL ||
420 else 419 __getopt_nonoption_flags[0] == '\0')
421 { 420 nonoption_flags_max_len = -1;
422 const char *orig_str = __getopt_nonoption_flags; 421 else
423 int len = nonoption_flags_max_len = strlen (orig_str); 422 {
424 423 const char *orig_str = __getopt_nonoption_flags;
425 if (nonoption_flags_max_len < argc) 424 int len = nonoption_flags_max_len = strlen (orig_str);
426 nonoption_flags_max_len = argc; 425
427 __getopt_nonoption_flags = (char *) malloc (nonoption_flags_max_len); 426 if (nonoption_flags_max_len < argc)
428 if (__getopt_nonoption_flags == NULL) 427 nonoption_flags_max_len = argc;
429 nonoption_flags_max_len = -1; 428 __getopt_nonoption_flags =
430 else 429 (char *) malloc (nonoption_flags_max_len);
431 { 430 if (__getopt_nonoption_flags == NULL)
432 memcpy (__getopt_nonoption_flags, orig_str, len); 431 nonoption_flags_max_len = -1;
433 memset (&__getopt_nonoption_flags[len], '\0', 432 else
434 nonoption_flags_max_len - len); 433 {
435 } 434 memcpy (__getopt_nonoption_flags, orig_str, len);
436 } 435 memset (&__getopt_nonoption_flags[len], '\0',
436 nonoption_flags_max_len - len);
437 }
438 }
439 }
440 nonoption_flags_len = nonoption_flags_max_len;
437 } 441 }
438 nonoption_flags_len = nonoption_flags_max_len;
439 }
440 else 442 else
441 nonoption_flags_len = 0; 443 nonoption_flags_len = 0;
442#endif 444#endif
@@ -502,8 +504,8 @@ _getopt_initialize (argc, argv, optstring)
502 504
503static int 505static int
504GN_getopt_internal (int argc, char *const *argv, const char *optstring, 506GN_getopt_internal (int argc, char *const *argv, const char *optstring,
505 const struct GNoption *longopts, int *longind, 507 const struct GNoption *longopts, int *longind,
506 int long_only) 508 int long_only)
507{ 509{
508 static int __getopt_initialized = 0; 510 static int __getopt_initialized = 0;
509 static int GNopterr = 1; 511 static int GNopterr = 1;
@@ -511,12 +513,12 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
511 GNoptarg = NULL; 513 GNoptarg = NULL;
512 514
513 if (GNoptind == 0 || !__getopt_initialized) 515 if (GNoptind == 0 || !__getopt_initialized)
514 { 516 {
515 if (GNoptind == 0) 517 if (GNoptind == 0)
516 GNoptind = 1; /* Don't scan ARGV[0], the program name. */ 518 GNoptind = 1; /* Don't scan ARGV[0], the program name. */
517 optstring = _getopt_initialize (argc, argv, optstring); 519 optstring = _getopt_initialize (argc, argv, optstring);
518 __getopt_initialized = 1; 520 __getopt_initialized = 1;
519 } 521 }
520 522
521 /* Test whether ARGV[GNoptind] points to a non-option argument. 523 /* Test whether ARGV[GNoptind] points to a non-option argument.
522 * Either it does not have option syntax, or there is an environment flag 524 * Either it does not have option syntax, or there is an environment flag
@@ -531,81 +533,81 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
531#endif 533#endif
532 534
533 if (nextchar == NULL || *nextchar == '\0') 535 if (nextchar == NULL || *nextchar == '\0')
534 {
535 /* Advance to the next ARGV-element. */
536
537 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been
538 * moved back by the user (who may also have changed the arguments). */
539 if (last_nonopt > GNoptind)
540 last_nonopt = GNoptind;
541 if (first_nonopt > GNoptind)
542 first_nonopt = GNoptind;
543
544 if (ordering == PERMUTE)
545 {
546 /* If we have just processed some options following some non-options,
547 * exchange them so that the options come first. */
548
549 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
550 exchange ((char **) argv);
551 else if (last_nonopt != GNoptind)
552 first_nonopt = GNoptind;
553
554 /* Skip any additional non-options
555 * and extend the range of non-options previously skipped. */
556
557 while (GNoptind < argc && NONOPTION_P)
558 GNoptind++;
559 last_nonopt = GNoptind;
560 }
561
562 /* The special ARGV-element `--' means premature end of options.
563 * Skip it like a null option,
564 * then exchange with previous non-options as if it were an option,
565 * then skip everything else like a non-option. */
566 if (GNoptind != argc && !strcmp (argv[GNoptind], "--"))
567 { 536 {
568 GNoptind++; 537 /* Advance to the next ARGV-element. */
569 538
570 if (first_nonopt != last_nonopt && last_nonopt != GNoptind) 539 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been
571 exchange ((char **) argv); 540 * moved back by the user (who may also have changed the arguments). */
572 else if (first_nonopt == last_nonopt) 541 if (last_nonopt > GNoptind)
573 first_nonopt = GNoptind; 542 last_nonopt = GNoptind;
574 last_nonopt = argc; 543 if (first_nonopt > GNoptind)
575 544 first_nonopt = GNoptind;
576 GNoptind = argc; 545
546 if (ordering == PERMUTE)
547 {
548 /* If we have just processed some options following some non-options,
549 * exchange them so that the options come first. */
550
551 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
552 exchange ((char **) argv);
553 else if (last_nonopt != GNoptind)
554 first_nonopt = GNoptind;
555
556 /* Skip any additional non-options
557 * and extend the range of non-options previously skipped. */
558
559 while (GNoptind < argc && NONOPTION_P)
560 GNoptind++;
561 last_nonopt = GNoptind;
562 }
563
564 /* The special ARGV-element `--' means premature end of options.
565 * Skip it like a null option,
566 * then exchange with previous non-options as if it were an option,
567 * then skip everything else like a non-option. */
568 if (GNoptind != argc && !strcmp (argv[GNoptind], "--"))
569 {
570 GNoptind++;
571
572 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
573 exchange ((char **) argv);
574 else if (first_nonopt == last_nonopt)
575 first_nonopt = GNoptind;
576 last_nonopt = argc;
577
578 GNoptind = argc;
579 }
580
581 /* If we have done all the ARGV-elements, stop the scan
582 * and back over any non-options that we skipped and permuted. */
583
584 if (GNoptind == argc)
585 {
586 /* Set the next-arg-index to point at the non-options
587 * that we previously skipped, so the caller will digest them. */
588 if (first_nonopt != last_nonopt)
589 GNoptind = first_nonopt;
590 return -1;
591 }
592
593 /* If we have come to a non-option and did not permute it,
594 * either stop the scan or describe it to the caller and pass it by. */
595
596 if (NONOPTION_P)
597 {
598 if (ordering == REQUIRE_ORDER)
599 return -1;
600 GNoptarg = argv[GNoptind++];
601 return 1;
602 }
603
604 /* We have found another option-ARGV-element.
605 * Skip the initial punctuation. */
606
607 nextchar =
608 (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-'));
577 } 609 }
578 610
579 /* If we have done all the ARGV-elements, stop the scan
580 * and back over any non-options that we skipped and permuted. */
581
582 if (GNoptind == argc)
583 {
584 /* Set the next-arg-index to point at the non-options
585 * that we previously skipped, so the caller will digest them. */
586 if (first_nonopt != last_nonopt)
587 GNoptind = first_nonopt;
588 return -1;
589 }
590
591 /* If we have come to a non-option and did not permute it,
592 * either stop the scan or describe it to the caller and pass it by. */
593
594 if (NONOPTION_P)
595 {
596 if (ordering == REQUIRE_ORDER)
597 return -1;
598 GNoptarg = argv[GNoptind++];
599 return 1;
600 }
601
602 /* We have found another option-ARGV-element.
603 * Skip the initial punctuation. */
604
605 nextchar =
606 (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-'));
607 }
608
609 /* Decode the current option-ARGV-element. */ 611 /* Decode the current option-ARGV-element. */
610 612
611 /* Check whether the ARGV-element is a long option. 613 /* Check whether the ARGV-element is a long option.
@@ -624,134 +626,138 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
624 if (longopts != NULL && 626 if (longopts != NULL &&
625 (argv[GNoptind][1] == '-' || 627 (argv[GNoptind][1] == '-' ||
626 (long_only && 628 (long_only &&
627 (argv[GNoptind][2] || !my_index (optstring, argv[GNoptind][1]))))) 629 (argv[GNoptind][2] || !my_index (optstring, argv[GNoptind][1])))))
628 {
629 char *nameend;
630 const struct GNoption *p;
631 const struct GNoption *pfound = NULL;
632 int exact = 0;
633 int ambig = 0;
634 int indfound = -1;
635 int option_index;
636
637 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
638 /* Do nothing. */ ;
639
640 /* Test all long options for either exact match
641 * or abbreviated matches. */
642 for (p = longopts, option_index = 0; p->name; p++, option_index++)
643 if (!strncmp (p->name, nextchar, nameend - nextchar))
644 {
645 if ((unsigned int) (nameend - nextchar) ==
646 (unsigned int) strlen (p->name))
647 {
648 /* Exact match found. */
649 pfound = p;
650 indfound = option_index;
651 exact = 1;
652 break;
653 }
654 else if (pfound == NULL)
655 {
656 /* First nonexact match found. */
657 pfound = p;
658 indfound = option_index;
659 }
660 else
661 /* Second or later nonexact match found. */
662 ambig = 1;
663 }
664
665 if (ambig && !exact)
666 { 630 {
667 if (GNopterr) 631 char *nameend;
668 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), argv[0], 632 const struct GNoption *p;
669 argv[GNoptind]); 633 const struct GNoption *pfound = NULL;
670 nextchar += strlen (nextchar); 634 int exact = 0;
671 GNoptind++; 635 int ambig = 0;
672 return '?'; 636 int indfound = -1;
673 } 637 int option_index;
674 638
675 if (pfound != NULL) 639 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
676 { 640 /* Do nothing. */ ;
677 option_index = indfound;
678 GNoptind++;
679 if (*nameend)
680 {
681 /* Don't test has_arg with >, because some C compilers don't
682 * allow it to be used on enums. */
683 if (pfound->has_arg)
684 GNoptarg = nameend + 1;
685 else
686 {
687 if (GNopterr)
688 {
689 if (argv[GNoptind - 1][1] == '-')
690 /* --option */
691 fprintf (stderr,
692 _("%s: option `--%s' does not allow an argument\n"),
693 argv[0], pfound->name);
694 else
695 /* +option or -option */
696 fprintf (stderr,
697 _("%s: option `%c%s' does not allow an argument\n"),
698 argv[0], argv[GNoptind - 1][0], pfound->name);
699 }
700 nextchar += strlen (nextchar);
701 return '?';
702 }
703 }
704 else if (pfound->has_arg == 1)
705 {
706 if (GNoptind < argc)
707 {
708 GNoptarg = argv[GNoptind++];
709 }
710 else
711 {
712 if (GNopterr)
713 {
714 fprintf (stderr, _("%s: option `%s' requires an argument\n"),
715 argv[0], argv[GNoptind - 1]);
716 }
717 nextchar += strlen (nextchar);
718 return (optstring[0] == ':') ? ':' : '?';
719 }
720 }
721 nextchar += strlen (nextchar);
722 if (longind != NULL)
723 *longind = option_index;
724 if (pfound->flag)
725 {
726 *(pfound->flag) = pfound->val;
727 return 0;
728 }
729 return pfound->val;
730 }
731 641
732 /* Can't find it as a long option. If this is not getopt_long_only, 642 /* Test all long options for either exact match
733 * or the option starts with '--' or is not a valid short 643 * or abbreviated matches. */
734 * option, then it's an error. 644 for (p = longopts, option_index = 0; p->name; p++, option_index++)
735 * Otherwise interpret it as a short option. */ 645 if (!strncmp (p->name, nextchar, nameend - nextchar))
736 if (!long_only || argv[GNoptind][1] == '-' || 646 {
737 my_index (optstring, *nextchar) == NULL) 647 if ((unsigned int) (nameend - nextchar) ==
738 { 648 (unsigned int) strlen (p->name))
739 if (GNopterr) 649 {
740 { 650 /* Exact match found. */
741 if (argv[GNoptind][1] == '-') 651 pfound = p;
742 /* --option */ 652 indfound = option_index;
743 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), argv[0], 653 exact = 1;
744 nextchar); 654 break;
745 else 655 }
746 /* +option or -option */ 656 else if (pfound == NULL)
747 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), argv[0], 657 {
748 argv[GNoptind][0], nextchar); 658 /* First nonexact match found. */
749 } 659 pfound = p;
750 nextchar = (char *) ""; 660 indfound = option_index;
751 GNoptind++; 661 }
752 return '?'; 662 else
663 /* Second or later nonexact match found. */
664 ambig = 1;
665 }
666
667 if (ambig && !exact)
668 {
669 if (GNopterr)
670 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), argv[0],
671 argv[GNoptind]);
672 nextchar += strlen (nextchar);
673 GNoptind++;
674 return '?';
675 }
676
677 if (pfound != NULL)
678 {
679 option_index = indfound;
680 GNoptind++;
681 if (*nameend)
682 {
683 /* Don't test has_arg with >, because some C compilers don't
684 * allow it to be used on enums. */
685 if (pfound->has_arg)
686 GNoptarg = nameend + 1;
687 else
688 {
689 if (GNopterr)
690 {
691 if (argv[GNoptind - 1][1] == '-')
692 /* --option */
693 fprintf (stderr,
694 _
695 ("%s: option `--%s' does not allow an argument\n"),
696 argv[0], pfound->name);
697 else
698 /* +option or -option */
699 fprintf (stderr,
700 _
701 ("%s: option `%c%s' does not allow an argument\n"),
702 argv[0], argv[GNoptind - 1][0],
703 pfound->name);
704 }
705 nextchar += strlen (nextchar);
706 return '?';
707 }
708 }
709 else if (pfound->has_arg == 1)
710 {
711 if (GNoptind < argc)
712 {
713 GNoptarg = argv[GNoptind++];
714 }
715 else
716 {
717 if (GNopterr)
718 {
719 fprintf (stderr,
720 _("%s: option `%s' requires an argument\n"),
721 argv[0], argv[GNoptind - 1]);
722 }
723 nextchar += strlen (nextchar);
724 return (optstring[0] == ':') ? ':' : '?';
725 }
726 }
727 nextchar += strlen (nextchar);
728 if (longind != NULL)
729 *longind = option_index;
730 if (pfound->flag)
731 {
732 *(pfound->flag) = pfound->val;
733 return 0;
734 }
735 return pfound->val;
736 }
737
738 /* Can't find it as a long option. If this is not getopt_long_only,
739 * or the option starts with '--' or is not a valid short
740 * option, then it's an error.
741 * Otherwise interpret it as a short option. */
742 if (!long_only || argv[GNoptind][1] == '-' ||
743 my_index (optstring, *nextchar) == NULL)
744 {
745 if (GNopterr)
746 {
747 if (argv[GNoptind][1] == '-')
748 /* --option */
749 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
750 argv[0], nextchar);
751 else
752 /* +option or -option */
753 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
754 argv[0], argv[GNoptind][0], nextchar);
755 }
756 nextchar = (char *) "";
757 GNoptind++;
758 return '?';
759 }
753 } 760 }
754 }
755 761
756 /* Look at and handle the next short option-character. */ 762 /* Look at and handle the next short option-character. */
757 763
@@ -764,191 +770,193 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
764 ++GNoptind; 770 ++GNoptind;
765 771
766 if (temp == NULL || c == ':') 772 if (temp == NULL || c == ':')
767 {
768 if (GNopterr)
769 { 773 {
770 if (posixly_correct) 774 if (GNopterr)
771 /* 1003.2 specifies the format of this message. */ 775 {
772 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); 776 if (posixly_correct)
773 else 777 /* 1003.2 specifies the format of this message. */
774 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); 778 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
779 else
780 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
781 }
782 return '?';
775 } 783 }
776 return '?';
777 }
778 /* Convenience. Treat POSIX -W foo same as long option --foo */ 784 /* Convenience. Treat POSIX -W foo same as long option --foo */
779 if (temp[0] == 'W' && temp[1] == ';') 785 if (temp[0] == 'W' && temp[1] == ';')
780 {
781 char *nameend;
782 const struct GNoption *p;
783 const struct GNoption *pfound = NULL;
784 int exact = 0;
785 int ambig = 0;
786 int indfound = 0;
787 int option_index;
788
789 /* This is an option that requires an argument. */
790 if (*nextchar != '\0')
791 {
792 GNoptarg = nextchar;
793 /* If we end this ARGV-element by taking the rest as an arg,
794 * we must advance to the next element now. */
795 GNoptind++;
796 }
797 else if (GNoptind == argc)
798 { 786 {
799 if (GNopterr) 787 char *nameend;
800 { 788 const struct GNoption *p;
801 /* 1003.2 specifies the format of this message. */ 789 const struct GNoption *pfound = NULL;
802 fprintf (stderr, _("%s: option requires an argument -- %c\n"), 790 int exact = 0;
803 argv[0], c); 791 int ambig = 0;
804 } 792 int indfound = 0;
805 if (optstring[0] == ':') 793 int option_index;
806 c = ':'; 794
807 else 795 /* This is an option that requires an argument. */
808 c = '?'; 796 if (*nextchar != '\0')
809 return c; 797 {
810 } 798 GNoptarg = nextchar;
811 else 799 /* If we end this ARGV-element by taking the rest as an arg,
812 /* We already incremented `GNoptind' once; 800 * we must advance to the next element now. */
813 * increment it again when taking next ARGV-elt as argument. */ 801 GNoptind++;
814 GNoptarg = argv[GNoptind++]; 802 }
815 803 else if (GNoptind == argc)
816 /* GNoptarg is now the argument, see if it's in the 804 {
817 * table of longopts. */ 805 if (GNopterr)
818 806 {
819 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '='; 807 /* 1003.2 specifies the format of this message. */
820 nameend++) 808 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
821 /* Do nothing. */ ; 809 argv[0], c);
822 810 }
823 /* Test all long options for either exact match 811 if (optstring[0] == ':')
824 * or abbreviated matches. */ 812 c = ':';
825 if (longopts != NULL) 813 else
826 for (p = longopts, option_index = 0; p->name; p++, option_index++) 814 c = '?';
827 if (!strncmp (p->name, nextchar, nameend - nextchar)) 815 return c;
828 { 816 }
829 if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 817 else
830 { 818 /* We already incremented `GNoptind' once;
831 /* Exact match found. */ 819 * increment it again when taking next ARGV-elt as argument. */
832 pfound = p; 820 GNoptarg = argv[GNoptind++];
833 indfound = option_index; 821
834 exact = 1; 822 /* GNoptarg is now the argument, see if it's in the
835 break; 823 * table of longopts. */
836 } 824
837 else if (pfound == NULL) 825 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '=';
838 { 826 nameend++)
839 /* First nonexact match found. */ 827 /* Do nothing. */ ;
840 pfound = p; 828
841 indfound = option_index; 829 /* Test all long options for either exact match
842 } 830 * or abbreviated matches. */
843 else 831 if (longopts != NULL)
844 /* Second or later nonexact match found. */ 832 for (p = longopts, option_index = 0; p->name; p++, option_index++)
845 ambig = 1; 833 if (!strncmp (p->name, nextchar, nameend - nextchar))
846 } 834 {
847 if (ambig && !exact) 835 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
848 { 836 {
849 if (GNopterr) 837 /* Exact match found. */
850 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), argv[0], 838 pfound = p;
851 argv[GNoptind]); 839 indfound = option_index;
852 nextchar += strlen (nextchar); 840 exact = 1;
853 GNoptind++; 841 break;
854 return '?'; 842 }
855 } 843 else if (pfound == NULL)
856 if (pfound != NULL) 844 {
857 { 845 /* First nonexact match found. */
858 option_index = indfound; 846 pfound = p;
859 if (*nameend) 847 indfound = option_index;
860 { 848 }
861 /* Don't test has_arg with >, because some C compilers don't 849 else
862 * allow it to be used on enums. */ 850 /* Second or later nonexact match found. */
863 if (pfound->has_arg) 851 ambig = 1;
864 GNoptarg = nameend + 1; 852 }
865 else 853 if (ambig && !exact)
866 { 854 {
867 if (GNopterr) 855 if (GNopterr)
868 fprintf (stderr, _("\ 856 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
857 argv[0], argv[GNoptind]);
858 nextchar += strlen (nextchar);
859 GNoptind++;
860 return '?';
861 }
862 if (pfound != NULL)
863 {
864 option_index = indfound;
865 if (*nameend)
866 {
867 /* Don't test has_arg with >, because some C compilers don't
868 * allow it to be used on enums. */
869 if (pfound->has_arg)
870 GNoptarg = nameend + 1;
871 else
872 {
873 if (GNopterr)
874 fprintf (stderr, _("\
869%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name); 875%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name);
870 876
871 nextchar += strlen (nextchar); 877 nextchar += strlen (nextchar);
872 return '?'; 878 return '?';
873 } 879 }
874 } 880 }
875 else if (pfound->has_arg == 1) 881 else if (pfound->has_arg == 1)
876 { 882 {
877 if (GNoptind < argc) 883 if (GNoptind < argc)
878 GNoptarg = argv[GNoptind++]; 884 GNoptarg = argv[GNoptind++];
879 else 885 else
880 { 886 {
881 if (GNopterr) 887 if (GNopterr)
882 fprintf (stderr, _("%s: option `%s' requires an argument\n"), 888 fprintf (stderr,
883 argv[0], argv[GNoptind - 1]); 889 _("%s: option `%s' requires an argument\n"),
884 nextchar += strlen (nextchar); 890 argv[0], argv[GNoptind - 1]);
885 return optstring[0] == ':' ? ':' : '?'; 891 nextchar += strlen (nextchar);
886 } 892 return optstring[0] == ':' ? ':' : '?';
887 } 893 }
888 nextchar += strlen (nextchar); 894 }
889 if (longind != NULL) 895 nextchar += strlen (nextchar);
890 *longind = option_index; 896 if (longind != NULL)
891 if (pfound->flag) 897 *longind = option_index;
892 { 898 if (pfound->flag)
893 *(pfound->flag) = pfound->val; 899 {
894 return 0; 900 *(pfound->flag) = pfound->val;
895 } 901 return 0;
896 return pfound->val; 902 }
903 return pfound->val;
904 }
905 nextchar = NULL;
906 return 'W'; /* Let the application handle it. */
897 } 907 }
898 nextchar = NULL;
899 return 'W'; /* Let the application handle it. */
900 }
901 if (temp[1] == ':') 908 if (temp[1] == ':')
902 {
903 if (temp[2] == ':')
904 { 909 {
905 /* This is an option that accepts an argument optionally. */ 910 if (temp[2] == ':')
906 if (*nextchar != '\0') 911 {
907 { 912 /* This is an option that accepts an argument optionally. */
908 GNoptarg = nextchar; 913 if (*nextchar != '\0')
909 GNoptind++; 914 {
910 } 915 GNoptarg = nextchar;
911 else 916 GNoptind++;
912 GNoptarg = NULL; 917 }
913 nextchar = NULL; 918 else
919 GNoptarg = NULL;
920 nextchar = NULL;
921 }
922 else
923 {
924 /* This is an option that requires an argument. */
925 if (*nextchar != '\0')
926 {
927 GNoptarg = nextchar;
928 /* If we end this ARGV-element by taking the rest as an arg,
929 * we must advance to the next element now. */
930 GNoptind++;
931 }
932 else if (GNoptind == argc)
933 {
934 if (GNopterr)
935 {
936 /* 1003.2 specifies the format of this message. */
937 fprintf (stderr,
938 _("%s: option requires an argument -- %c\n"),
939 argv[0], c);
940 }
941 if (optstring[0] == ':')
942 c = ':';
943 else
944 c = '?';
945 }
946 else
947 /* We already incremented `GNoptind' once;
948 * increment it again when taking next ARGV-elt as argument. */
949 GNoptarg = argv[GNoptind++];
950 nextchar = NULL;
951 }
914 } 952 }
915 else
916 {
917 /* This is an option that requires an argument. */
918 if (*nextchar != '\0')
919 {
920 GNoptarg = nextchar;
921 /* If we end this ARGV-element by taking the rest as an arg,
922 * we must advance to the next element now. */
923 GNoptind++;
924 }
925 else if (GNoptind == argc)
926 {
927 if (GNopterr)
928 {
929 /* 1003.2 specifies the format of this message. */
930 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
931 argv[0], c);
932 }
933 if (optstring[0] == ':')
934 c = ':';
935 else
936 c = '?';
937 }
938 else
939 /* We already incremented `GNoptind' once;
940 * increment it again when taking next ARGV-elt as argument. */
941 GNoptarg = argv[GNoptind++];
942 nextchar = NULL;
943 }
944 }
945 return c; 953 return c;
946 } 954 }
947} 955}
948 956
949static int 957static int
950GNgetopt_long (int argc, char *const *argv, const char *options, 958GNgetopt_long (int argc, char *const *argv, const char *options,
951 const struct GNoption *long_options, int *opt_index) 959 const struct GNoption *long_options, int *opt_index)
952{ 960{
953 return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0); 961 return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0);
954} 962}
@@ -967,8 +975,8 @@ GNgetopt_long (int argc, char *const *argv, const char *options,
967 */ 975 */
968int 976int
969GNUNET_GETOPT_run (const char *binaryOptions, 977GNUNET_GETOPT_run (const char *binaryOptions,
970 const struct GNUNET_GETOPT_CommandLineOption *allOptions, 978 const struct GNUNET_GETOPT_CommandLineOption *allOptions,
971 unsigned int argc, char *const *argv) 979 unsigned int argc, char *const *argv)
972{ 980{
973 struct GNoption *long_options; 981 struct GNoption *long_options;
974 struct GNUNET_GETOPT_CommandLineProcessorContext clpc; 982 struct GNUNET_GETOPT_CommandLineProcessorContext clpc;
@@ -993,15 +1001,15 @@ GNUNET_GETOPT_run (const char *binaryOptions,
993 shorts = GNUNET_malloc (count * 2 + 1); 1001 shorts = GNUNET_malloc (count * 2 + 1);
994 spos = 0; 1002 spos = 0;
995 for (i = 0; i < count; i++) 1003 for (i = 0; i < count; i++)
996 { 1004 {
997 long_options[i].name = allOptions[i].name; 1005 long_options[i].name = allOptions[i].name;
998 long_options[i].has_arg = allOptions[i].require_argument; 1006 long_options[i].has_arg = allOptions[i].require_argument;
999 long_options[i].flag = NULL; 1007 long_options[i].flag = NULL;
1000 long_options[i].val = allOptions[i].shortName; 1008 long_options[i].val = allOptions[i].shortName;
1001 shorts[spos++] = allOptions[i].shortName; 1009 shorts[spos++] = allOptions[i].shortName;
1002 if (allOptions[i].require_argument != 0) 1010 if (allOptions[i].require_argument != 0)
1003 shorts[spos++] = ':'; 1011 shorts[spos++] = ':';
1004 } 1012 }
1005 long_options[count].name = NULL; 1013 long_options[count].name = NULL;
1006 long_options[count].has_arg = 0; 1014 long_options[count].has_arg = 0;
1007 long_options[count].flag = NULL; 1015 long_options[count].flag = NULL;
@@ -1010,31 +1018,31 @@ GNUNET_GETOPT_run (const char *binaryOptions,
1010 cont = GNUNET_OK; 1018 cont = GNUNET_OK;
1011 /* main getopt loop */ 1019 /* main getopt loop */
1012 while (cont == GNUNET_OK) 1020 while (cont == GNUNET_OK)
1013 {
1014 int option_index = 0;
1015
1016 c = GNgetopt_long (argc, argv, shorts, long_options, &option_index);
1017
1018 if (c == GNUNET_SYSERR)
1019 break; /* No more flags to process */
1020
1021 for (i = 0; i < count; i++)
1022 {
1023 clpc.currentArgument = GNoptind - 1;
1024 if ((char) c == allOptions[i].shortName)
1025 {
1026 cont =
1027 allOptions[i].processor (&clpc, allOptions[i].scls,
1028 allOptions[i].name, GNoptarg);
1029 break;
1030 }
1031 }
1032 if (i == count)
1033 { 1021 {
1034 fprintf (stderr, _("Use --help to get a list of options.\n")); 1022 int option_index = 0;
1035 cont = GNUNET_SYSERR; 1023
1024 c = GNgetopt_long (argc, argv, shorts, long_options, &option_index);
1025
1026 if (c == GNUNET_SYSERR)
1027 break; /* No more flags to process */
1028
1029 for (i = 0; i < count; i++)
1030 {
1031 clpc.currentArgument = GNoptind - 1;
1032 if ((char) c == allOptions[i].shortName)
1033 {
1034 cont =
1035 allOptions[i].processor (&clpc, allOptions[i].scls,
1036 allOptions[i].name, GNoptarg);
1037 break;
1038 }
1039 }
1040 if (i == count)
1041 {
1042 fprintf (stderr, _("Use --help to get a list of options.\n"));
1043 cont = GNUNET_SYSERR;
1044 }
1036 } 1045 }
1037 }
1038 1046
1039 GNUNET_free (shorts); 1047 GNUNET_free (shorts);
1040 GNUNET_free (long_options); 1048 GNUNET_free (long_options);
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index f00c27f29..8564d591f 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -28,6 +28,8 @@
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29#include "gnunet_getopt_lib.h" 29#include "gnunet_getopt_lib.h"
30 30
31#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32
31 33
32/** 34/**
33 * Print out program version (implements --version). 35 * Print out program version (implements --version).
@@ -40,8 +42,8 @@
40 */ 42 */
41int 43int
42GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext 44GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
43 *ctx, void *scls, const char *option, 45 *ctx, void *scls, const char *option,
44 const char *value) 46 const char *value)
45{ 47{
46 const char *version = scls; 48 const char *version = scls;
47 49
@@ -64,8 +66,8 @@ GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
64 */ 66 */
65int 67int
66GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext 68GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
67 *ctx, void *scls, const char *option, 69 *ctx, void *scls, const char *option,
68 const char *value) 70 const char *value)
69{ 71{
70 const char *about = scls; 72 const char *about = scls;
71 size_t slen; 73 size_t slen;
@@ -79,74 +81,74 @@ GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
79 81
80 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about)); 82 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
81 printf (_ 83 printf (_
82 ("Arguments mandatory for long options are also mandatory for short options.\n")); 84 ("Arguments mandatory for long options are also mandatory for short options.\n"));
83 i = 0; 85 i = 0;
84 opt = ctx->allOptions; 86 opt = ctx->allOptions;
85 while (opt[i].description != NULL) 87 while (opt[i].description != NULL)
86 {
87 if (opt[i].shortName == '\0')
88 printf (" ");
89 else
90 printf (" -%c, ", opt[i].shortName);
91 printf ("--%s", opt[i].name);
92 slen = 8 + strlen (opt[i].name);
93 if (opt[i].argumentHelp != NULL)
94 {
95 printf ("=%s", opt[i].argumentHelp);
96 slen += 1 + strlen (opt[i].argumentHelp);
97 }
98 if (slen > BORDER)
99 { 88 {
100 printf ("\n%*s", BORDER, ""); 89 if (opt[i].shortName == '\0')
101 slen = BORDER; 90 printf (" ");
91 else
92 printf (" -%c, ", opt[i].shortName);
93 printf ("--%s", opt[i].name);
94 slen = 8 + strlen (opt[i].name);
95 if (opt[i].argumentHelp != NULL)
96 {
97 printf ("=%s", opt[i].argumentHelp);
98 slen += 1 + strlen (opt[i].argumentHelp);
99 }
100 if (slen > BORDER)
101 {
102 printf ("\n%*s", BORDER, "");
103 slen = BORDER;
104 }
105 if (slen < BORDER)
106 {
107 printf ("%*s", (int) (BORDER - slen), "");
108 slen = BORDER;
109 }
110 if (0 < strlen (opt[i].description))
111 trans = gettext (opt[i].description);
112 else
113 trans = "";
114 ml = strlen (trans);
115 p = 0;
116 OUTER:
117 while (ml - p > 78 - slen)
118 {
119 for (j = p + 78 - slen; j > p; j--)
120 {
121 if (isspace ((unsigned char) trans[j]))
122 {
123 scp = GNUNET_malloc (j - p + 1);
124 memcpy (scp, &trans[p], j - p);
125 scp[j - p] = '\0';
126 printf ("%s\n%*s", scp, BORDER + 2, "");
127 GNUNET_free (scp);
128 p = j + 1;
129 slen = BORDER + 2;
130 goto OUTER;
131 }
132 }
133 /* could not find space to break line */
134 scp = GNUNET_malloc (78 - slen + 1);
135 memcpy (scp, &trans[p], 78 - slen);
136 scp[78 - slen] = '\0';
137 printf ("%s\n%*s", scp, BORDER + 2, "");
138 GNUNET_free (scp);
139 slen = BORDER + 2;
140 p = p + 78 - slen;
141 }
142 /* print rest */
143 if (p < ml)
144 printf ("%s\n", &trans[p]);
145 if (strlen (trans) == 0)
146 printf ("\n");
147 i++;
102 } 148 }
103 if (slen < BORDER)
104 {
105 printf ("%*s", (int) (BORDER - slen), "");
106 slen = BORDER;
107 }
108 if (0 < strlen (opt[i].description))
109 trans = gettext (opt[i].description);
110 else
111 trans = "";
112 ml = strlen (trans);
113 p = 0;
114OUTER:
115 while (ml - p > 78 - slen)
116 {
117 for (j = p + 78 - slen; j > p; j--)
118 {
119 if (isspace ((unsigned char) trans[j]))
120 {
121 scp = GNUNET_malloc (j - p + 1);
122 memcpy (scp, &trans[p], j - p);
123 scp[j - p] = '\0';
124 printf ("%s\n%*s", scp, BORDER + 2, "");
125 GNUNET_free (scp);
126 p = j + 1;
127 slen = BORDER + 2;
128 goto OUTER;
129 }
130 }
131 /* could not find space to break line */
132 scp = GNUNET_malloc (78 - slen + 1);
133 memcpy (scp, &trans[p], 78 - slen);
134 scp[78 - slen] = '\0';
135 printf ("%s\n%*s", scp, BORDER + 2, "");
136 GNUNET_free (scp);
137 slen = BORDER + 2;
138 p = p + 78 - slen;
139 }
140 /* print rest */
141 if (p < ml)
142 printf ("%s\n", &trans[p]);
143 if (strlen (trans) == 0)
144 printf ("\n");
145 i++;
146 }
147 printf ("Report bugs to gnunet-developers@gnu.org.\n" 149 printf ("Report bugs to gnunet-developers@gnu.org.\n"
148 "GNUnet home page: http://www.gnu.org/software/gnunet/\n" 150 "GNUnet home page: http://www.gnu.org/software/gnunet/\n"
149 "General help using GNU software: http://www.gnu.org/gethelp/\n"); 151 "General help using GNU software: http://www.gnu.org/gethelp/\n");
150 return GNUNET_SYSERR; 152 return GNUNET_SYSERR;
151} 153}
152 154
@@ -166,9 +168,10 @@ OUTER:
166 * @return GNUNET_OK 168 * @return GNUNET_OK
167 */ 169 */
168int 170int
169GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext 171GNUNET_GETOPT_increment_value (struct
170 *ctx, void *scls, const char *option, 172 GNUNET_GETOPT_CommandLineProcessorContext *ctx,
171 const char *value) 173 void *scls, const char *option,
174 const char *value)
172{ 175{
173 int *val = scls; 176 int *val = scls;
174 177
@@ -193,7 +196,7 @@ GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext
193 */ 196 */
194int 197int
195GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 198GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
196 void *scls, const char *option, const char *value) 199 void *scls, const char *option, const char *value)
197{ 200{
198 int *val = scls; 201 int *val = scls;
199 202
@@ -217,8 +220,9 @@ GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
217 * @return GNUNET_OK 220 * @return GNUNET_OK
218 */ 221 */
219int 222int
220GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 223GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext
221 void *scls, const char *option, const char *value) 224 *ctx, void *scls, const char *option,
225 const char *value)
222{ 226{
223 char **val = scls; 227 char **val = scls;
224 228
@@ -243,16 +247,18 @@ GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
243 * @return GNUNET_OK if parsing the value worked 247 * @return GNUNET_OK if parsing the value worked
244 */ 248 */
245int 249int
246GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 250GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext
247 void *scls, const char *option, const char *value) 251 *ctx, void *scls, const char *option,
252 const char *value)
248{ 253{
249 unsigned long long *val = scls; 254 unsigned long long *val = scls;
250 255
251 if (1 != SSCANF (value, "%llu", val)) 256 if (1 != SSCANF (value, "%llu", val))
252 { 257 {
253 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option); 258 fprintf (stderr, _("You must pass a number to the `%s' option.\n"),
254 return GNUNET_SYSERR; 259 option);
255 } 260 return GNUNET_SYSERR;
261 }
256 return GNUNET_OK; 262 return GNUNET_OK;
257} 263}
258 264
@@ -272,15 +278,16 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
272 */ 278 */
273int 279int
274GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 280GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
275 void *scls, const char *option, const char *value) 281 void *scls, const char *option, const char *value)
276{ 282{
277 unsigned int *val = scls; 283 unsigned int *val = scls;
278 284
279 if (1 != SSCANF (value, "%u", val)) 285 if (1 != SSCANF (value, "%u", val))
280 { 286 {
281 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option); 287 fprintf (stderr, _("You must pass a number to the `%s' option.\n"),
282 return GNUNET_SYSERR; 288 option);
283 } 289 return GNUNET_SYSERR;
290 }
284 return GNUNET_OK; 291 return GNUNET_OK;
285} 292}
286 293
diff --git a/src/util/gnunet-config-diff.c b/src/util/gnunet-config-diff.c
index 207b9518a..992480a30 100644
--- a/src/util/gnunet-config-diff.c
+++ b/src/util/gnunet-config-diff.c
@@ -8,10 +8,10 @@ main (int argc, char **argv)
8 struct GNUNET_CONFIGURATION_Handle *i2; 8 struct GNUNET_CONFIGURATION_Handle *i2;
9 9
10 if (argc != 3) 10 if (argc != 3)
11 { 11 {
12 fprintf (stderr, "Invoke using `%s DEFAULTS-IN DIFFS'\n", argv[0]); 12 fprintf (stderr, "Invoke using `%s DEFAULTS-IN DIFFS'\n", argv[0]);
13 return 1; 13 return 1;
14 } 14 }
15 i1 = GNUNET_CONFIGURATION_create (); 15 i1 = GNUNET_CONFIGURATION_create ();
16 i2 = GNUNET_CONFIGURATION_create (); 16 i2 = GNUNET_CONFIGURATION_create ();
17 if ((GNUNET_OK != GNUNET_CONFIGURATION_load (i1, argv[1])) || 17 if ((GNUNET_OK != GNUNET_CONFIGURATION_load (i1, argv[1])) ||
diff --git a/src/util/gnunet-resolver.c b/src/util/gnunet-resolver.c
index 131693dd1..e97793114 100644
--- a/src/util/gnunet-resolver.c
+++ b/src/util/gnunet-resolver.c
@@ -72,9 +72,9 @@ main (int argc, char *const *argv)
72 GNUNET_GETOPT_OPTION_END 72 GNUNET_GETOPT_OPTION_END
73 }; 73 };
74 return (GNUNET_OK == 74 return (GNUNET_OK ==
75 GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]", 75 GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]",
76 gettext_noop ("Test GNUnet DNS resolver code."), 76 gettext_noop ("Test GNUnet DNS resolver code."),
77 options, &run, NULL)) ? 0 : 1; 77 options, &run, NULL)) ? 0 : 1;
78} 78}
79 79
80/* end of gnunet-resolver.c */ 80/* end of gnunet-resolver.c */
diff --git a/src/util/gnunet-service-resolver.c b/src/util/gnunet-service-resolver.c
index 5d4c5c224..c15f0d3f5 100644
--- a/src/util/gnunet-service-resolver.c
+++ b/src/util/gnunet-service-resolver.c
@@ -89,7 +89,7 @@ getnameinfo_resolve (struct IPCache *cache)
89 89
90 if (0 == 90 if (0 ==
91 getnameinfo (cache->sa, cache->salen, hostname, sizeof (hostname), NULL, 91 getnameinfo (cache->sa, cache->salen, hostname, sizeof (hostname), NULL,
92 0, 0)) 92 0, 0))
93 cache->addr = GNUNET_strdup (hostname); 93 cache->addr = GNUNET_strdup (hostname);
94} 94}
95#endif 95#endif
@@ -107,20 +107,20 @@ gethostbyaddr_resolve (struct IPCache *cache)
107 struct hostent *ent; 107 struct hostent *ent;
108 108
109 switch (cache->sa->sa_family) 109 switch (cache->sa->sa_family)
110 { 110 {
111 case AF_INET: 111 case AF_INET:
112 ent = 112 ent =
113 gethostbyaddr (&((struct sockaddr_in *) cache->sa)->sin_addr, 113 gethostbyaddr (&((struct sockaddr_in *) cache->sa)->sin_addr,
114 sizeof (struct in_addr), AF_INET); 114 sizeof (struct in_addr), AF_INET);
115 break; 115 break;
116 case AF_INET6: 116 case AF_INET6:
117 ent = 117 ent =
118 gethostbyaddr (&((struct sockaddr_in6 *) cache->sa)->sin6_addr, 118 gethostbyaddr (&((struct sockaddr_in6 *) cache->sa)->sin6_addr,
119 sizeof (struct in6_addr), AF_INET6); 119 sizeof (struct in6_addr), AF_INET6);
120 break; 120 break;
121 default: 121 default:
122 ent = NULL; 122 ent = NULL;
123 } 123 }
124 if (ent != NULL) 124 if (ent != NULL)
125 cache->addr = GNUNET_strdup (ent->h_name); 125 cache->addr = GNUNET_strdup (ent->h_name);
126} 126}
@@ -158,7 +158,7 @@ cache_resolve (struct IPCache *cache)
158 */ 158 */
159static void 159static void
160get_ip_as_string (struct GNUNET_SERVER_Client *client, 160get_ip_as_string (struct GNUNET_SERVER_Client *client,
161 const struct sockaddr *sa, socklen_t salen) 161 const struct sockaddr *sa, socklen_t salen)
162{ 162{
163 struct IPCache *cache; 163 struct IPCache *cache;
164 struct IPCache *prev; 164 struct IPCache *prev;
@@ -166,73 +166,73 @@ get_ip_as_string (struct GNUNET_SERVER_Client *client,
166 struct GNUNET_SERVER_TransmitContext *tc; 166 struct GNUNET_SERVER_TransmitContext *tc;
167 167
168 if (salen < sizeof (struct sockaddr)) 168 if (salen < sizeof (struct sockaddr))
169 { 169 {
170 GNUNET_break (0); 170 GNUNET_break (0);
171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
172 return; 172 return;
173 } 173 }
174 now = GNUNET_TIME_absolute_get (); 174 now = GNUNET_TIME_absolute_get ();
175 cache = head; 175 cache = head;
176 prev = NULL; 176 prev = NULL;
177 while ((cache != NULL) && 177 while ((cache != NULL) &&
178 ((cache->salen != salen) || (0 != memcmp (cache->sa, sa, salen)))) 178 ((cache->salen != salen) || (0 != memcmp (cache->sa, sa, salen))))
179 {
180 if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value <
181 60 * 60 * 1000)
182 { 179 {
183 if (prev != NULL) 180 if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value <
184 { 181 60 * 60 * 1000)
185 prev->next = cache->next; 182 {
186 GNUNET_free_non_null (cache->addr); 183 if (prev != NULL)
187 GNUNET_free (cache->sa); 184 {
188 GNUNET_free (cache); 185 prev->next = cache->next;
189 cache = prev->next; 186 GNUNET_free_non_null (cache->addr);
190 } 187 GNUNET_free (cache->sa);
191 else 188 GNUNET_free (cache);
192 { 189 cache = prev->next;
193 head = cache->next; 190 }
194 GNUNET_free_non_null (cache->addr); 191 else
195 GNUNET_free (cache->sa); 192 {
196 GNUNET_free (cache); 193 head = cache->next;
197 cache = head; 194 GNUNET_free_non_null (cache->addr);
198 } 195 GNUNET_free (cache->sa);
199 continue; 196 GNUNET_free (cache);
197 cache = head;
198 }
199 continue;
200 }
201 prev = cache;
202 cache = cache->next;
200 } 203 }
201 prev = cache;
202 cache = cache->next;
203 }
204 if (cache != NULL) 204 if (cache != NULL)
205 {
206 cache->last_request = now;
207 if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value <
208 60 * 60 * 1000)
209 { 205 {
210 GNUNET_free_non_null (cache->addr); 206 cache->last_request = now;
207 if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value <
208 60 * 60 * 1000)
209 {
210 GNUNET_free_non_null (cache->addr);
211 cache->addr = NULL;
212 cache->salen = 0;
213 cache_resolve (cache);
214 }
215 }
216 else
217 {
218 cache = GNUNET_malloc (sizeof (struct IPCache));
219 cache->next = head;
220 cache->salen = salen;
221 cache->sa = GNUNET_malloc (salen);
222 memcpy (cache->sa, sa, salen);
223 cache->last_request = GNUNET_TIME_absolute_get ();
224 cache->last_refresh = GNUNET_TIME_absolute_get ();
211 cache->addr = NULL; 225 cache->addr = NULL;
212 cache->salen = 0;
213 cache_resolve (cache); 226 cache_resolve (cache);
227 head = cache;
214 } 228 }
215 }
216 else
217 {
218 cache = GNUNET_malloc (sizeof (struct IPCache));
219 cache->next = head;
220 cache->salen = salen;
221 cache->sa = GNUNET_malloc (salen);
222 memcpy (cache->sa, sa, salen);
223 cache->last_request = GNUNET_TIME_absolute_get ();
224 cache->last_refresh = GNUNET_TIME_absolute_get ();
225 cache->addr = NULL;
226 cache_resolve (cache);
227 head = cache;
228 }
229 tc = GNUNET_SERVER_transmit_context_create (client); 229 tc = GNUNET_SERVER_transmit_context_create (client);
230 if (cache->addr != NULL) 230 if (cache->addr != NULL)
231 GNUNET_SERVER_transmit_context_append_data (tc, cache->addr, 231 GNUNET_SERVER_transmit_context_append_data (tc, cache->addr,
232 strlen (cache->addr) + 1, 232 strlen (cache->addr) + 1,
233 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 233 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
234 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, 234 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
235 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 235 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
236 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); 236 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
237} 237}
238 238
@@ -240,7 +240,7 @@ get_ip_as_string (struct GNUNET_SERVER_Client *client,
240#if HAVE_GETADDRINFO 240#if HAVE_GETADDRINFO
241static int 241static int
242getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc, 242getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc,
243 const char *hostname, int domain) 243 const char *hostname, int domain)
244{ 244{
245 int s; 245 int s;
246 struct addrinfo hints; 246 struct addrinfo hints;
@@ -254,36 +254,37 @@ getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc,
254#else 254#else
255 hints.ai_family = AF_INET; 255 hints.ai_family = AF_INET;
256#endif 256#endif
257 hints.ai_socktype = SOCK_STREAM; /* go for TCP */ 257 hints.ai_socktype = SOCK_STREAM; /* go for TCP */
258 258
259 if (0 != (s = getaddrinfo (hostname, NULL, &hints, &result))) 259 if (0 != (s = getaddrinfo (hostname, NULL, &hints, &result)))
260 { 260 {
261 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Could not resolve `%s' (%s): %s\n"), 261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
262 hostname, 262 _("Could not resolve `%s' (%s): %s\n"), hostname,
263 (domain == 263 (domain ==
264 AF_INET) ? "IPv4" : ((domain == AF_INET6) ? "IPv6" : "any"), 264 AF_INET) ? "IPv4" : ((domain ==
265 gai_strerror (s)); 265 AF_INET6) ? "IPv6" : "any"),
266 if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY) 266 gai_strerror (s));
267 if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY)
267#ifndef MINGW 268#ifndef MINGW
268 || (s == EAI_SYSTEM) 269 || (s == EAI_SYSTEM)
269#else 270#else
270 // FIXME NILS 271 // FIXME NILS
271 || 1 272 || 1
272#endif 273#endif
273 ) 274 )
274 return GNUNET_NO; /* other function may still succeed */ 275 return GNUNET_NO; /* other function may still succeed */
275 return GNUNET_SYSERR; 276 return GNUNET_SYSERR;
276 } 277 }
277 if (result == NULL) 278 if (result == NULL)
278 return GNUNET_SYSERR; 279 return GNUNET_SYSERR;
279 pos = result; 280 pos = result;
280 while (pos != NULL) 281 while (pos != NULL)
281 { 282 {
282 GNUNET_SERVER_transmit_context_append_data (tc, pos->ai_addr, 283 GNUNET_SERVER_transmit_context_append_data (tc, pos->ai_addr,
283 pos->ai_addrlen, 284 pos->ai_addrlen,
284 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 285 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
285 pos = pos->ai_next; 286 pos = pos->ai_next;
286 } 287 }
287 freeaddrinfo (result); 288 freeaddrinfo (result);
288 return GNUNET_OK; 289 return GNUNET_OK;
289} 290}
@@ -292,7 +293,7 @@ getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc,
292#if HAVE_GETHOSTBYNAME2 293#if HAVE_GETHOSTBYNAME2
293static int 294static int
294gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, 295gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
295 const char *hostname, int domain) 296 const char *hostname, int domain)
296{ 297{
297 struct hostent *hp; 298 struct hostent *hp;
298 struct sockaddr_in a4; 299 struct sockaddr_in a4;
@@ -301,48 +302,48 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
301 int ret2; 302 int ret2;
302 303
303 if (domain == AF_UNSPEC) 304 if (domain == AF_UNSPEC)
304 { 305 {
305 ret1 = gethostbyname2_resolve (tc, hostname, AF_INET); 306 ret1 = gethostbyname2_resolve (tc, hostname, AF_INET);
306 ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6); 307 ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6);
307 if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK)) 308 if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK))
308 return GNUNET_OK; 309 return GNUNET_OK;
309 if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR)) 310 if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR))
310 return GNUNET_SYSERR; 311 return GNUNET_SYSERR;
311 return GNUNET_NO; 312 return GNUNET_NO;
312 } 313 }
313 hp = gethostbyname2 (hostname, domain); 314 hp = gethostbyname2 (hostname, domain);
314 if (hp == NULL) 315 if (hp == NULL)
315 { 316 {
316 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
317 _("Could not find IP of host `%s': %s\n"), hostname, 318 _("Could not find IP of host `%s': %s\n"), hostname,
318 hstrerror (h_errno)); 319 hstrerror (h_errno));
319 return GNUNET_SYSERR; 320 return GNUNET_SYSERR;
320 } 321 }
321 GNUNET_assert (hp->h_addrtype == domain); 322 GNUNET_assert (hp->h_addrtype == domain);
322 if (domain == AF_INET) 323 if (domain == AF_INET)
323 { 324 {
324 GNUNET_assert (hp->h_length == sizeof (struct in_addr)); 325 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
325 memset (&a4, 0, sizeof (a4)); 326 memset (&a4, 0, sizeof (a4));
326 a4.sin_family = AF_INET; 327 a4.sin_family = AF_INET;
327#if HAVE_SOCKADDR_IN_SIN_LEN 328#if HAVE_SOCKADDR_IN_SIN_LEN
328 a4.sin_len = (u_char) sizeof (struct sockaddr_in); 329 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
329#endif 330#endif
330 memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length); 331 memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length);
331 GNUNET_SERVER_transmit_context_append_data (tc, &a4, sizeof (a4), 332 GNUNET_SERVER_transmit_context_append_data (tc, &a4, sizeof (a4),
332 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 333 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
333 } 334 }
334 else 335 else
335 { 336 {
336 GNUNET_assert (hp->h_length == sizeof (struct in6_addr)); 337 GNUNET_assert (hp->h_length == sizeof (struct in6_addr));
337 memset (&a6, 0, sizeof (a6)); 338 memset (&a6, 0, sizeof (a6));
338 a6.sin6_family = AF_INET6; 339 a6.sin6_family = AF_INET6;
339#if HAVE_SOCKADDR_IN_SIN_LEN 340#if HAVE_SOCKADDR_IN_SIN_LEN
340 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); 341 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
341#endif 342#endif
342 memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length); 343 memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length);
343 GNUNET_SERVER_transmit_context_append_data (tc, &a6, sizeof (a6), 344 GNUNET_SERVER_transmit_context_append_data (tc, &a6, sizeof (a6),
344 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 345 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
345 } 346 }
346 return GNUNET_OK; 347 return GNUNET_OK;
347} 348}
348#endif 349#endif
@@ -350,24 +351,24 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
350#if HAVE_GETHOSTBYNAME 351#if HAVE_GETHOSTBYNAME
351static int 352static int
352gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc, 353gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
353 const char *hostname) 354 const char *hostname)
354{ 355{
355 struct hostent *hp; 356 struct hostent *hp;
356 struct sockaddr_in addr; 357 struct sockaddr_in addr;
357 358
358 hp = GETHOSTBYNAME (hostname); 359 hp = GETHOSTBYNAME (hostname);
359 if (hp == NULL) 360 if (hp == NULL)
360 { 361 {
361 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 362 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
362 _("Could not find IP of host `%s': %s\n"), hostname, 363 _("Could not find IP of host `%s': %s\n"), hostname,
363 hstrerror (h_errno)); 364 hstrerror (h_errno));
364 return GNUNET_SYSERR; 365 return GNUNET_SYSERR;
365 } 366 }
366 if (hp->h_addrtype != AF_INET) 367 if (hp->h_addrtype != AF_INET)
367 { 368 {
368 GNUNET_break (0); 369 GNUNET_break (0);
369 return GNUNET_SYSERR; 370 return GNUNET_SYSERR;
370 } 371 }
371 GNUNET_assert (hp->h_length == sizeof (struct in_addr)); 372 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
372 memset (&addr, 0, sizeof (addr)); 373 memset (&addr, 0, sizeof (addr));
373 addr.sin_family = AF_INET; 374 addr.sin_family = AF_INET;
@@ -376,7 +377,7 @@ gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
376#endif 377#endif
377 memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length); 378 memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length);
378 GNUNET_SERVER_transmit_context_append_data (tc, &addr, sizeof (addr), 379 GNUNET_SERVER_transmit_context_append_data (tc, &addr, sizeof (addr),
379 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 380 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
380 return GNUNET_OK; 381 return GNUNET_OK;
381} 382}
382#endif 383#endif
@@ -390,8 +391,8 @@ gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
390 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any" 391 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
391 */ 392 */
392static void 393static void
393get_ip_from_hostname (struct GNUNET_SERVER_Client *client, const char *hostname, 394get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
394 int domain) 395 const char *hostname, int domain)
395{ 396{
396 int ret; 397 int ret;
397 struct GNUNET_SERVER_TransmitContext *tc; 398 struct GNUNET_SERVER_TransmitContext *tc;
@@ -411,7 +412,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client, const char *hostname,
411 gethostbyname_resolve (tc, hostname); 412 gethostbyname_resolve (tc, hostname);
412#endif 413#endif
413 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, 414 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
414 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 415 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
415 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); 416 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
416} 417}
417 418
@@ -425,7 +426,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client, const char *hostname,
425 */ 426 */
426static void 427static void
427handle_get (void *cls, struct GNUNET_SERVER_Client *client, 428handle_get (void *cls, struct GNUNET_SERVER_Client *client,
428 const struct GNUNET_MessageHeader *message) 429 const struct GNUNET_MessageHeader *message)
429{ 430{
430 uint16_t msize; 431 uint16_t msize;
431 const struct GNUNET_RESOLVER_GetMessage *msg; 432 const struct GNUNET_RESOLVER_GetMessage *msg;
@@ -437,78 +438,78 @@ handle_get (void *cls, struct GNUNET_SERVER_Client *client,
437 438
438 msize = ntohs (message->size); 439 msize = ntohs (message->size);
439 if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage)) 440 if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage))
440 { 441 {
441 GNUNET_break (0); 442 GNUNET_break (0);
442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
443 return; 444 return;
444 } 445 }
445 msg = (const struct GNUNET_RESOLVER_GetMessage *) message; 446 msg = (const struct GNUNET_RESOLVER_GetMessage *) message;
446 size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage); 447 size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage);
447 direction = ntohl (msg->direction); 448 direction = ntohl (msg->direction);
448 domain = ntohl (msg->domain); 449 domain = ntohl (msg->domain);
449 if (direction == GNUNET_NO) 450 if (direction == GNUNET_NO)
450 {
451 /* IP from hostname */
452 hostname = (const char *) &msg[1];
453 if (hostname[size - 1] != '\0')
454 { 451 {
455 GNUNET_break (0); 452 /* IP from hostname */
456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 453 hostname = (const char *) &msg[1];
457 return; 454 if (hostname[size - 1] != '\0')
458 } 455 {
456 GNUNET_break (0);
457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
458 return;
459 }
459#if DEBUG_RESOLVER 460#if DEBUG_RESOLVER
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver asked to look up `%s'.\n"), 461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 hostname); 462 _("Resolver asked to look up `%s'.\n"), hostname);
462#endif 463#endif
463 get_ip_from_hostname (client, hostname, domain); 464 get_ip_from_hostname (client, hostname, domain);
464 } 465 }
465 else 466 else
466 { 467 {
467#if DEBUG_RESOLVER 468#if DEBUG_RESOLVER
468 char buf[INET6_ADDRSTRLEN]; 469 char buf[INET6_ADDRSTRLEN];
469#endif 470#endif
470 if (size < sizeof (struct sockaddr)) 471 if (size < sizeof (struct sockaddr))
471 { 472 {
472 GNUNET_break (0); 473 GNUNET_break (0);
473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
474 return; 475 return;
475 } 476 }
476 sa = (const struct sockaddr *) &msg[1]; 477 sa = (const struct sockaddr *) &msg[1];
477 switch (sa->sa_family) 478 switch (sa->sa_family)
478 { 479 {
479 case AF_INET: 480 case AF_INET:
480 if (size != sizeof (struct sockaddr_in)) 481 if (size != sizeof (struct sockaddr_in))
481 { 482 {
482 GNUNET_break (0); 483 GNUNET_break (0);
483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
484 return; 485 return;
485 } 486 }
486#if DEBUG_RESOLVER 487#if DEBUG_RESOLVER
487 inet_ntop (AF_INET, sa, buf, size); 488 inet_ntop (AF_INET, sa, buf, size);
488#endif 489#endif
489 break; 490 break;
490 case AF_INET6: 491 case AF_INET6:
491 if (size != sizeof (struct sockaddr_in6)) 492 if (size != sizeof (struct sockaddr_in6))
492 { 493 {
493 GNUNET_break (0); 494 GNUNET_break (0);
494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
495 return; 496 return;
496 } 497 }
497#if DEBUG_RESOLVER 498#if DEBUG_RESOLVER
498 inet_ntop (AF_INET6, sa, buf, size); 499 inet_ntop (AF_INET6, sa, buf, size);
499#endif 500#endif
500 break; 501 break;
501 default: 502 default:
502 GNUNET_break (0); 503 GNUNET_break (0);
503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
504 return; 505 return;
505 } 506 }
506#if DEBUG_RESOLVER 507#if DEBUG_RESOLVER
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 _("Resolver asked to look up IP address `%s'.\n"), buf); 509 _("Resolver asked to look up IP address `%s'.\n"), buf);
509#endif 510#endif
510 get_ip_as_string (client, sa, size); 511 get_ip_as_string (client, sa, size);
511 } 512 }
512} 513}
513 514
514 515
@@ -545,18 +546,18 @@ main (int argc, char *const *argv)
545 struct IPCache *pos; 546 struct IPCache *pos;
546 547
547 ret = 548 ret =
548 (GNUNET_OK == 549 (GNUNET_OK ==
549 GNUNET_SERVICE_run (argc, argv, "resolver", GNUNET_SERVICE_OPTION_NONE, 550 GNUNET_SERVICE_run (argc, argv, "resolver", GNUNET_SERVICE_OPTION_NONE,
550 &run, NULL)) ? 0 : 1; 551 &run, NULL)) ? 0 : 1;
551 552
552 while (head != NULL) 553 while (head != NULL)
553 { 554 {
554 pos = head->next; 555 pos = head->next;
555 GNUNET_free_non_null (head->addr); 556 GNUNET_free_non_null (head->addr);
556 GNUNET_free (head->sa); 557 GNUNET_free (head->sa);
557 GNUNET_free (head); 558 GNUNET_free (head);
558 head = pos; 559 head = pos;
559 } 560 }
560 return ret; 561 return ret;
561} 562}
562 563
diff --git a/src/util/load.c b/src/util/load.c
index 39e861597..1df1abc7c 100644
--- a/src/util/load.c
+++ b/src/util/load.c
@@ -28,6 +28,8 @@
28 28
29#define DEBUG_LOAD GNUNET_EXTRA_LOGGING 29#define DEBUG_LOAD GNUNET_EXTRA_LOGGING
30 30
31#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32
31/** 33/**
32 * Values we track for load calculations. 34 * Values we track for load calculations.
33 */ 35 */
@@ -92,23 +94,23 @@ internal_update (struct GNUNET_LOAD_Value *load)
92 if (delta.rel_value < load->autodecline.rel_value) 94 if (delta.rel_value < load->autodecline.rel_value)
93 return; 95 return;
94 if (load->autodecline.rel_value == 0) 96 if (load->autodecline.rel_value == 0)
95 { 97 {
96 load->runavg_delay = 0.0; 98 load->runavg_delay = 0.0;
97 load->load = 0; 99 load->load = 0;
98 return; 100 return;
99 } 101 }
100 n = delta.rel_value / load->autodecline.rel_value; 102 n = delta.rel_value / load->autodecline.rel_value;
101 if (n > 16) 103 if (n > 16)
102 { 104 {
103 load->runavg_delay = 0.0; 105 load->runavg_delay = 0.0;
104 load->load = 0; 106 load->load = 0;
105 return; 107 return;
106 } 108 }
107 while (n > 0) 109 while (n > 0)
108 { 110 {
109 n--; 111 n--;
110 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; 112 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
111 } 113 }
112} 114}
113 115
114 116
@@ -140,7 +142,7 @@ GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
140 */ 142 */
141void 143void
142GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load, 144GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
143 struct GNUNET_TIME_Relative autodecline) 145 struct GNUNET_TIME_Relative autodecline)
144{ 146{
145 internal_update (load); 147 internal_update (load);
146 load->autodecline = autodecline; 148 load->autodecline = autodecline;
@@ -175,10 +177,10 @@ calculate_load (struct GNUNET_LOAD_Value *load)
175 nm1 = n - 1.0; 177 nm1 = n - 1.0;
176 avgdel = sum_val_i / n; 178 avgdel = sum_val_i / n;
177 stddev = 179 stddev =
178 (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + 180 (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i +
179 n * avgdel * avgdel) / nm1; 181 n * avgdel * avgdel) / nm1;
180 if (stddev <= 0) 182 if (stddev <= 0)
181 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */ 183 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
182 /* now calculate load based on how far out we are from 184 /* now calculate load based on how far out we are from
183 * std dev; or if we are below average, simply assume load zero */ 185 * std dev; or if we are below average, simply assume load zero */
184 if (load->runavg_delay < avgdel) 186 if (load->runavg_delay < avgdel)
@@ -241,11 +243,11 @@ GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data)
241 internal_update (load); 243 internal_update (load);
242 load->last_update = GNUNET_TIME_absolute_get (); 244 load->last_update = GNUNET_TIME_absolute_get ();
243 if (data > 64 * 1024) 245 if (data > 64 * 1024)
244 { 246 {
245 /* very large */ 247 /* very large */
246 load->load = 100.0; 248 load->load = 100.0;
247 return; 249 return;
248 } 250 }
249 dv = (uint32_t) data; 251 dv = (uint32_t) data;
250 load->cummulative_delay += dv; 252 load->cummulative_delay += dv;
251 load->cummulative_squared_delay += dv * dv; 253 load->cummulative_squared_delay += dv * dv;
diff --git a/src/util/network.c b/src/util/network.c
index 4fa1ce27a..a62a57758 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -29,6 +29,10 @@
29#include "disk.h" 29#include "disk.h"
30#include "gnunet_container_lib.h" 30#include "gnunet_container_lib.h"
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33
34#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
35
32#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING 36#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
33 37
34#define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING 38#define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING
@@ -86,11 +90,11 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
86 mode = !doBlock; 90 mode = !doBlock;
87 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR) 91 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
88 92
89 { 93 {
90 SetErrnoFromWinsockError (WSAGetLastError ()); 94 SetErrnoFromWinsockError (WSAGetLastError ());
91 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); 95 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
92 return GNUNET_SYSERR; 96 return GNUNET_SYSERR;
93 } 97 }
94 return GNUNET_OK; 98 return GNUNET_OK;
95 99
96#else 100#else
@@ -99,10 +103,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
99 103
100 if (flags == -1) 104 if (flags == -1)
101 105
102 { 106 {
103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 107 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
104 return GNUNET_SYSERR; 108 return GNUNET_SYSERR;
105 } 109 }
106 if (doBlock) 110 if (doBlock)
107 flags &= ~O_NONBLOCK; 111 flags &= ~O_NONBLOCK;
108 112
@@ -110,10 +114,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
110 flags |= O_NONBLOCK; 114 flags |= O_NONBLOCK;
111 if (0 != fcntl (fd->fd, F_SETFL, flags)) 115 if (0 != fcntl (fd->fd, F_SETFL, flags))
112 116
113 { 117 {
114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 118 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
115 return GNUNET_SYSERR; 119 return GNUNET_SYSERR;
116 } 120 }
117 return GNUNET_OK; 121 return GNUNET_OK;
118#endif 122#endif
119} 123}
@@ -158,8 +162,8 @@ socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
158 162
159 if (0 != 163 if (0 !=
160 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, 164 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value,
161 sizeof (abs_value))) 165 sizeof (abs_value)))
162 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 166 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
163} 167}
164#endif 168#endif
165 169
@@ -177,15 +181,16 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
177#ifndef WINDOWS 181#ifndef WINDOWS
178 int value = 1; 182 int value = 1;
179 183
180 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value))) 184 if (0 !=
181 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 185 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
186 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
182#else 187#else
183 const char *abs_value = "1"; 188 const char *abs_value = "1";
184 189
185 if (0 != 190 if (0 !=
186 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, 191 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
187 sizeof (abs_value))) 192 sizeof (abs_value)))
188 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 193 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
189#endif 194#endif
190} 195}
191 196
@@ -200,7 +205,8 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
200 */ 205 */
201struct GNUNET_NETWORK_Handle * 206struct GNUNET_NETWORK_Handle *
202GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, 207GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
203 struct sockaddr *address, socklen_t * address_len) 208 struct sockaddr *address,
209 socklen_t * address_len)
204{ 210{
205 struct GNUNET_NETWORK_Handle *ret; 211 struct GNUNET_NETWORK_Handle *ret;
206 212
@@ -212,8 +218,8 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
212 int gsn = getsockname (desc->fd, &name, &namelen); 218 int gsn = getsockname (desc->fd, &name, &namelen);
213 219
214 if (gsn == 0) 220 if (gsn == 0)
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n", 221 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
216 GNUNET_a2s (&name, namelen)); 222 GNUNET_a2s (&name, namelen));
217 } 223 }
218#endif 224#endif
219 ret->fd = accept (desc->fd, address, address_len); 225 ret->fd = accept (desc->fd, address, address_len);
@@ -222,36 +228,36 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
222 else 228 else
223 ret->af = desc->af; 229 ret->af = desc->af;
224 if (ret->fd == INVALID_SOCKET) 230 if (ret->fd == INVALID_SOCKET)
225 { 231 {
226#ifdef MINGW 232#ifdef MINGW
227 SetErrnoFromWinsockError (WSAGetLastError ()); 233 SetErrnoFromWinsockError (WSAGetLastError ());
228#endif 234#endif
229 GNUNET_free (ret); 235 GNUNET_free (ret);
230 return NULL; 236 return NULL;
231 } 237 }
232#ifndef MINGW 238#ifndef MINGW
233 if (ret->fd >= FD_SETSIZE) 239 if (ret->fd >= FD_SETSIZE)
234 { 240 {
235 GNUNET_break (0 == close (ret->fd)); 241 GNUNET_break (0 == close (ret->fd));
236 GNUNET_free (ret); 242 GNUNET_free (ret);
237 errno = EMFILE; 243 errno = EMFILE;
238 return NULL; 244 return NULL;
239 } 245 }
240#endif 246#endif
241 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) 247 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
242 248
243 { 249 {
244 250
245 /* we might want to treat this one as fatal... */ 251 /* we might want to treat this one as fatal... */
246 GNUNET_break (0); 252 GNUNET_break (0);
247 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); 253 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
248 return NULL; 254 return NULL;
249 } 255 }
250 256
251#ifndef MINGW 257#ifndef MINGW
252 if (GNUNET_OK != socket_set_inheritable (ret)) 258 if (GNUNET_OK != socket_set_inheritable (ret))
253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 259 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
254 "socket_set_inheritable"); 260 "socket_set_inheritable");
255#endif 261#endif
256#ifdef DARWIN 262#ifdef DARWIN
257 socket_set_nosigpipe (ret); 263 socket_set_nosigpipe (ret);
@@ -273,8 +279,8 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
273 */ 279 */
274int 280int
275GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, 281GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
276 const struct sockaddr *address, 282 const struct sockaddr *address,
277 socklen_t address_len) 283 socklen_t address_len)
278{ 284{
279 int ret; 285 int ret;
280 286
@@ -283,23 +289,24 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
283 const int on = 1; 289 const int on = 1;
284 290
285 if (desc->af == AF_INET6) 291 if (desc->af == AF_INET6)
286 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on))) 292 if (0 !=
287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); 293 setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
294 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
288#endif 295#endif
289#endif 296#endif
290#ifndef WINDOWS 297#ifndef WINDOWS
291 /* This is required, and required here, but only on UNIX */ 298 /* This is required, and required here, but only on UNIX */
292 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))) 299 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
293 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); 300 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
294#endif 301#endif
295#ifndef LINUX 302#ifndef LINUX
296#ifndef MINGW 303#ifndef MINGW
297 if (address->sa_family == AF_UNIX) 304 if (address->sa_family == AF_UNIX)
298 { 305 {
299 const struct sockaddr_un *un = (const struct sockaddr_un *) address; 306 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
300 307
301 (void) unlink (un->sun_path); 308 (void) unlink (un->sun_path);
302 } 309 }
303#endif 310#endif
304#endif 311#endif
305 ret = bind (desc->fd, address, address_len); 312 ret = bind (desc->fd, address, address_len);
@@ -334,17 +341,17 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
334 DWORD error = 0; 341 DWORD error = 0;
335 342
336#if DEBUG_NETWORK 343#if DEBUG_NETWORK
337 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", 344 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
338 "Closing 0x%x\n", desc->fd); 345 "Closing 0x%x\n", desc->fd);
339#endif 346#endif
340 SetLastError (0); 347 SetLastError (0);
341 ret = closesocket (desc->fd); 348 ret = closesocket (desc->fd);
342 error = WSAGetLastError (); 349 error = WSAGetLastError ();
343 SetErrnoFromWinsockError (error); 350 SetErrnoFromWinsockError (error);
344#if DEBUG_NETWORK 351#if DEBUG_NETWORK
345 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", 352 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
346 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", 353 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
347 desc->fd, ret, error); 354 desc->fd, ret, error);
348#endif 355#endif
349#else 356#else
350 ret = close (desc->fd); 357 ret = close (desc->fd);
@@ -352,13 +359,12 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
352#ifndef LINUX 359#ifndef LINUX
353#ifndef MINGW 360#ifndef MINGW
354 if ((desc->af == AF_UNIX) && (NULL != desc->addr)) 361 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
355 { 362 {
356 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr; 363 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
357 364
358 if (0 != unlink (un->sun_path)) 365 if (0 != unlink (un->sun_path))
359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", 366 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path);
360 un->sun_path); 367 }
361 }
362#endif 368#endif
363#endif 369#endif
364 GNUNET_free_non_null (desc->addr); 370 GNUNET_free_non_null (desc->addr);
@@ -382,7 +388,7 @@ GNUNET_NETWORK_socket_box_native (int fd)
382 struct GNUNET_NETWORK_Handle *ret; 388 struct GNUNET_NETWORK_Handle *ret;
383 389
384 if (fcntl (fd, F_GETFD) < 0) 390 if (fcntl (fd, F_GETFD) < 0)
385 return NULL; /* invalid FD */ 391 return NULL; /* invalid FD */
386 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); 392 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
387 ret->fd = fd; 393 ret->fd = fd;
388 ret->af = AF_UNSPEC; 394 ret->af = AF_UNSPEC;
@@ -400,8 +406,8 @@ GNUNET_NETWORK_socket_box_native (int fd)
400 */ 406 */
401int 407int
402GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, 408GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
403 const struct sockaddr *address, 409 const struct sockaddr *address,
404 socklen_t address_len) 410 socklen_t address_len)
405{ 411{
406 int ret; 412 int ret;
407 413
@@ -409,11 +415,11 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
409 415
410#ifdef MINGW 416#ifdef MINGW
411 if (SOCKET_ERROR == ret) 417 if (SOCKET_ERROR == ret)
412 { 418 {
413 SetErrnoFromWinsockError (WSAGetLastError ()); 419 SetErrnoFromWinsockError (WSAGetLastError ());
414 if (errno == EWOULDBLOCK) 420 if (errno == EWOULDBLOCK)
415 errno = EINPROGRESS; 421 errno = EINPROGRESS;
416 } 422 }
417#endif 423#endif
418 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 424 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
419} 425}
@@ -431,8 +437,8 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
431 */ 437 */
432int 438int
433GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, 439GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
434 int level, int optname, void *optval, 440 int level, int optname, void *optval,
435 socklen_t * optlen) 441 socklen_t * optlen)
436{ 442{
437 int ret; 443 int ret;
438 444
@@ -457,7 +463,7 @@ GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
457 */ 463 */
458int 464int
459GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, 465GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
460 int backlog) 466 int backlog)
461{ 467{
462 int ret; 468 int ret;
463 469
@@ -480,7 +486,7 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
480 */ 486 */
481ssize_t 487ssize_t
482GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * 488GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
483 desc) 489 desc)
484{ 490{
485 int error; 491 int error;
486 492
@@ -513,8 +519,9 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
513 */ 519 */
514ssize_t 520ssize_t
515GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc, 521GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
516 void *buffer, size_t length, 522 void *buffer, size_t length,
517 struct sockaddr * src_addr, socklen_t * addrlen) 523 struct sockaddr * src_addr,
524 socklen_t * addrlen)
518{ 525{
519 int ret; 526 int ret;
520 int flags; 527 int flags;
@@ -542,7 +549,7 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
542 */ 549 */
543ssize_t 550ssize_t
544GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc, 551GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
545 void *buffer, size_t length) 552 void *buffer, size_t length)
546{ 553{
547 int ret; 554 int ret;
548 int flags; 555 int flags;
@@ -571,7 +578,7 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
571 */ 578 */
572ssize_t 579ssize_t
573GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc, 580GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
574 const void *buffer, size_t length) 581 const void *buffer, size_t length)
575{ 582{
576 int ret; 583 int ret;
577 int flags; 584 int flags;
@@ -610,9 +617,9 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
610 */ 617 */
611ssize_t 618ssize_t
612GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc, 619GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
613 const void *message, size_t length, 620 const void *message, size_t length,
614 const struct sockaddr * dest_addr, 621 const struct sockaddr * dest_addr,
615 socklen_t dest_len) 622 socklen_t dest_len)
616{ 623{
617 int ret; 624 int ret;
618 int flags; 625 int flags;
@@ -645,8 +652,8 @@ GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
645 */ 652 */
646int 653int
647GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level, 654GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
648 int option_name, const void *option_value, 655 int option_name, const void *option_value,
649 socklen_t option_len) 656 socklen_t option_len)
650{ 657{
651 int ret; 658 int ret;
652 659
@@ -678,36 +685,36 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
678 ret->af = domain; 685 ret->af = domain;
679 ret->fd = socket (domain, type, protocol); 686 ret->fd = socket (domain, type, protocol);
680 if (INVALID_SOCKET == ret->fd) 687 if (INVALID_SOCKET == ret->fd)
681 { 688 {
682#ifdef MINGW 689#ifdef MINGW
683 SetErrnoFromWinsockError (WSAGetLastError ()); 690 SetErrnoFromWinsockError (WSAGetLastError ());
684#endif 691#endif
685 GNUNET_free (ret); 692 GNUNET_free (ret);
686 return NULL; 693 return NULL;
687 } 694 }
688 695
689#ifndef MINGW 696#ifndef MINGW
690 if (ret->fd >= FD_SETSIZE) 697 if (ret->fd >= FD_SETSIZE)
691 { 698 {
692 GNUNET_break (0 == close (ret->fd)); 699 GNUNET_break (0 == close (ret->fd));
693 GNUNET_free (ret); 700 GNUNET_free (ret);
694 errno = EMFILE; 701 errno = EMFILE;
695 return NULL; 702 return NULL;
696 } 703 }
697 704
698#endif 705#endif
699 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) 706 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
700 { 707 {
701 /* we might want to treat this one as fatal... */ 708 /* we might want to treat this one as fatal... */
702 GNUNET_break (0); 709 GNUNET_break (0);
703 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); 710 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
704 return NULL; 711 return NULL;
705 } 712 }
706 713
707#ifndef MINGW 714#ifndef MINGW
708 if (GNUNET_OK != socket_set_inheritable (ret)) 715 if (GNUNET_OK != socket_set_inheritable (ret))
709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 716 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
710 "socket_set_inheritable"); 717 "socket_set_inheritable");
711#endif 718#endif
712#ifdef DARWIN 719#ifdef DARWIN
713 socket_set_nosigpipe (ret); 720 socket_set_nosigpipe (ret);
@@ -716,7 +723,7 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
716#ifdef AF_UNIX 723#ifdef AF_UNIX
717 && (domain != AF_UNIX) 724 && (domain != AF_UNIX)
718#endif 725#endif
719 ) 726 )
720 socket_set_nodelay (ret); 727 socket_set_nodelay (ret);
721 return ret; 728 return ret;
722} 729}
@@ -761,24 +768,24 @@ GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
761 if (0 != 768 if (0 !=
762 (ret = 769 (ret =
763 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value, 770 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
764 sizeof (value)))) 771 sizeof (value))))
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 772 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
766 if (0 != 773 if (0 !=
767 (ret = 774 (ret =
768 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value, 775 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
769 sizeof (value)))) 776 sizeof (value))))
770 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 777 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
771#elif LINUX 778#elif LINUX
772 int value = 0; 779 int value = 0;
773 780
774 if (0 != 781 if (0 !=
775 (ret = 782 (ret =
776 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value)))) 783 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
777 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 784 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
778 if (0 != 785 if (0 !=
779 (ret = 786 (ret =
780 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value)))) 787 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
781 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 788 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
782#endif 789#endif
783 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 790 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
784} 791}
@@ -805,7 +812,7 @@ GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
805 */ 812 */
806void 813void
807GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, 814GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
808 const struct GNUNET_NETWORK_Handle *desc) 815 const struct GNUNET_NETWORK_Handle *desc)
809{ 816{
810 FD_SET (desc->fd, &fds->sds); 817 FD_SET (desc->fd, &fds->sds);
811 if (desc->fd + 1 > fds->nsds) 818 if (desc->fd + 1 > fds->nsds)
@@ -821,7 +828,7 @@ GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
821 */ 828 */
822int 829int
823GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, 830GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
824 const struct GNUNET_NETWORK_Handle *desc) 831 const struct GNUNET_NETWORK_Handle *desc)
825{ 832{
826 return FD_ISSET (desc->fd, &fds->sds); 833 return FD_ISSET (desc->fd, &fds->sds);
827} 834}
@@ -834,18 +841,18 @@ GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
834 */ 841 */
835void 842void
836GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, 843GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
837 const struct GNUNET_NETWORK_FDSet *src) 844 const struct GNUNET_NETWORK_FDSet *src)
838{ 845{
839 int nfds; 846 int nfds;
840 847
841 for (nfds = src->nsds; nfds > 0; nfds--) 848 for (nfds = src->nsds; nfds > 0; nfds--)
842 if (FD_ISSET (nfds, &src->sds)) 849 if (FD_ISSET (nfds, &src->sds))
843 850
844 { 851 {
845 FD_SET (nfds, &dst->sds); 852 FD_SET (nfds, &dst->sds);
846 if (nfds + 1 > dst->nsds) 853 if (nfds + 1 > dst->nsds)
847 dst->nsds = nfds + 1; 854 dst->nsds = nfds + 1;
848 } 855 }
849#ifdef MINGW 856#ifdef MINGW
850 GNUNET_CONTAINER_slist_append (dst->handles, src->handles); 857 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
851#endif 858#endif
@@ -860,7 +867,7 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
860 */ 867 */
861void 868void
862GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, 869GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
863 const struct GNUNET_NETWORK_FDSet *from) 870 const struct GNUNET_NETWORK_FDSet *from)
864{ 871{
865 FD_COPY (&from->sds, &to->sds); 872 FD_COPY (&from->sds, &to->sds);
866 to->nsds = from->nsds; 873 to->nsds = from->nsds;
@@ -894,7 +901,7 @@ GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
894 */ 901 */
895void 902void
896GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, 903GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
897 const fd_set * from, int nfds) 904 const fd_set * from, int nfds)
898{ 905{
899 FD_COPY (from, &to->sds); 906 FD_COPY (from, &to->sds);
900 to->nsds = nfds; 907 to->nsds = nfds;
@@ -925,7 +932,7 @@ GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
925 */ 932 */
926int 933int
927GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, 934GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
928 int nfd) 935 int nfd)
929{ 936{
930 if ((nfd == -1) || (to == NULL)) 937 if ((nfd == -1) || (to == NULL))
931 return GNUNET_NO; 938 return GNUNET_NO;
@@ -940,12 +947,12 @@ GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
940 */ 947 */
941void 948void
942GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, 949GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
943 const struct GNUNET_DISK_FileHandle *h) 950 const struct GNUNET_DISK_FileHandle *h)
944{ 951{
945#ifdef MINGW 952#ifdef MINGW
946 GNUNET_CONTAINER_slist_add (fds->handles, 953 GNUNET_CONTAINER_slist_add (fds->handles,
947 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h, 954 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
948 sizeof (struct GNUNET_DISK_FileHandle)); 955 sizeof (struct GNUNET_DISK_FileHandle));
949 956
950#else 957#else
951 int fd; 958 int fd;
@@ -967,13 +974,13 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
967 */ 974 */
968int 975int
969GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, 976GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
970 const struct GNUNET_DISK_FileHandle *h) 977 const struct GNUNET_DISK_FileHandle *h)
971{ 978{
972 979
973#ifdef MINGW 980#ifdef MINGW
974 return GNUNET_CONTAINER_slist_contains (fds->handles, h, 981 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
975 sizeof (struct 982 sizeof (struct
976 GNUNET_DISK_FileHandle)); 983 GNUNET_DISK_FileHandle));
977#else 984#else
978 return FD_ISSET (h->fd, &fds->sds); 985 return FD_ISSET (h->fd, &fds->sds);
979#endif 986#endif
@@ -988,7 +995,7 @@ GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
988 */ 995 */
989int 996int
990GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, 997GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
991 const struct GNUNET_NETWORK_FDSet *fds2) 998 const struct GNUNET_NETWORK_FDSet *fds2)
992{ 999{
993#ifndef MINGW 1000#ifndef MINGW
994 int nfds; 1001 int nfds;
@@ -997,11 +1004,11 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
997 if (nfds > fds2->nsds) 1004 if (nfds > fds2->nsds)
998 nfds = fds2->nsds; 1005 nfds = fds2->nsds;
999 while (nfds > 0) 1006 while (nfds > 0)
1000 { 1007 {
1001 nfds--; 1008 nfds--;
1002 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds)) 1009 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1003 return GNUNET_YES; 1010 return GNUNET_YES;
1004 } 1011 }
1005#else 1012#else
1006 struct GNUNET_CONTAINER_SList_Iterator *it; 1013 struct GNUNET_CONTAINER_SList_Iterator *it;
1007 struct GNUNET_DISK_FileHandle *h; 1014 struct GNUNET_DISK_FileHandle *h;
@@ -1012,45 +1019,48 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1012 * inside of fd_set; also the O(n^2) is really bad... */ 1019 * inside of fd_set; also the O(n^2) is really bad... */
1013 1020
1014 for (i = 0; i < fds1->sds.fd_count; i++) 1021 for (i = 0; i < fds1->sds.fd_count; i++)
1015 {
1016 for (j = 0; j < fds2->sds.fd_count; j++)
1017 { 1022 {
1018 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j]) 1023 for (j = 0; j < fds2->sds.fd_count; j++)
1019 return GNUNET_YES; 1024 {
1025 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1026 return GNUNET_YES;
1027 }
1020 } 1028 }
1021 }
1022 it = GNUNET_CONTAINER_slist_begin (fds1->handles); 1029 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1023 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES) 1030 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
1024 { 1031 {
1025#if DEBUG_NETWORK 1032#if DEBUG_NETWORK
1026 struct GNUNET_CONTAINER_SList_Iterator *t; 1033 struct GNUNET_CONTAINER_SList_Iterator *t;
1027#endif 1034#endif
1028 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL); 1035 h =
1036 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it,
1037 NULL);
1029#if DEBUG_NETWORK 1038#if DEBUG_NETWORK
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 "Checking that FD 0x%x is in another set:\n", h->h); 1040 "Checking that FD 0x%x is in another set:\n", h->h);
1032 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles); 1041 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1033 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; 1042 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1034 GNUNET_CONTAINER_slist_next (t)) 1043 GNUNET_CONTAINER_slist_next (t))
1035 { 1044 {
1036 struct GNUNET_DISK_FileHandle *fh; 1045 struct GNUNET_DISK_FileHandle *fh;
1037 1046
1038 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1047 fh =
1039 NULL); 1048 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h); 1049 NULL);
1041 } 1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1042#endif 1051 }
1043 if (GNUNET_CONTAINER_slist_contains 1052#endif
1044 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle))) 1053 if (GNUNET_CONTAINER_slist_contains
1045 { 1054 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1055 {
1046#if DEBUG_NETWORK 1056#if DEBUG_NETWORK
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); 1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1048#endif 1058#endif
1049 GNUNET_CONTAINER_slist_iter_destroy (it); 1059 GNUNET_CONTAINER_slist_iter_destroy (it);
1050 return GNUNET_YES; 1060 return GNUNET_YES;
1061 }
1062 GNUNET_CONTAINER_slist_next (it);
1051 } 1063 }
1052 GNUNET_CONTAINER_slist_next (it);
1053 }
1054 GNUNET_CONTAINER_slist_iter_destroy (it); 1064 GNUNET_CONTAINER_slist_iter_destroy (it);
1055#endif 1065#endif
1056 return GNUNET_NO; 1066 return GNUNET_NO;
@@ -1098,9 +1108,9 @@ GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1098 */ 1108 */
1099int 1109int
1100GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, 1110GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1101 struct GNUNET_NETWORK_FDSet *wfds, 1111 struct GNUNET_NETWORK_FDSet *wfds,
1102 struct GNUNET_NETWORK_FDSet *efds, 1112 struct GNUNET_NETWORK_FDSet *efds,
1103 const struct GNUNET_TIME_Relative timeout) 1113 const struct GNUNET_TIME_Relative timeout)
1104{ 1114{
1105 int nfds = 0; 1115 int nfds = 0;
1106 1116
@@ -1130,7 +1140,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1130 DWORD newretcode = 0; 1140 DWORD newretcode = 0;
1131 int returnedpos = 0; 1141 int returnedpos = 0;
1132 1142
1133 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except; 1143 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1144 *handles_except;
1134 1145
1135 fd_set aread, awrite, aexcept; 1146 fd_set aread, awrite, aexcept;
1136 1147
@@ -1144,67 +1155,68 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1144 struct timeval tv; 1155 struct timeval tv;
1145#endif 1156#endif
1146 if (NULL != rfds) 1157 if (NULL != rfds)
1147 { 1158 {
1148 nfds = rfds->nsds; 1159 nfds = rfds->nsds;
1149#ifdef MINGW 1160#ifdef MINGW
1150 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles); 1161 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1151#if DEBUG_NETWORK 1162#if DEBUG_NETWORK
1152 {
1153 struct GNUNET_CONTAINER_SList_Iterator *t;
1154
1155 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1156 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1157 GNUNET_CONTAINER_slist_next (t))
1158 { 1163 {
1159 struct GNUNET_DISK_FileHandle *fh; 1164 struct GNUNET_CONTAINER_SList_Iterator *t;
1160 1165
1161 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1166 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1162 NULL); 1167 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", 1168 GNUNET_CONTAINER_slist_next (t))
1164 fh->h, fh); 1169 {
1170 struct GNUNET_DISK_FileHandle *fh;
1171
1172 fh =
1173 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1174 NULL);
1175 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n",
1176 fh->h, fh);
1177 }
1165 } 1178 }
1166 }
1167#endif 1179#endif
1168#endif 1180#endif
1169 } 1181 }
1170 if (NULL != wfds) 1182 if (NULL != wfds)
1171 { 1183 {
1172 nfds = GNUNET_MAX (nfds, wfds->nsds); 1184 nfds = GNUNET_MAX (nfds, wfds->nsds);
1173#ifdef MINGW 1185#ifdef MINGW
1174 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles); 1186 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1175#endif 1187#endif
1176 } 1188 }
1177 if (NULL != efds) 1189 if (NULL != efds)
1178 { 1190 {
1179 nfds = GNUNET_MAX (nfds, efds->nsds); 1191 nfds = GNUNET_MAX (nfds, efds->nsds);
1180#ifdef MINGW 1192#ifdef MINGW
1181 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles); 1193 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1182#endif 1194#endif
1183 } 1195 }
1184 1196
1185 if ((nfds == 0) && 1197 if ((nfds == 0) &&
1186 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) 1198 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1187#ifdef MINGW 1199#ifdef MINGW
1188 && handles == 0 1200 && handles == 0
1189#endif 1201#endif
1190 ) 1202 )
1191 { 1203 {
1192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1204 LOG (GNUNET_ERROR_TYPE_ERROR,
1193 _ 1205 _
1194 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 1206 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1195 "select"); 1207 "select");
1196 GNUNET_break (0); 1208 GNUNET_break (0);
1197 } 1209 }
1198#ifndef MINGW 1210#ifndef MINGW
1199 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value; 1211 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1200 tv.tv_usec = 1212 tv.tv_usec =
1201 1000 * (timeout.rel_value - 1213 1000 * (timeout.rel_value -
1202 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value)); 1214 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1203 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL, 1215 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1204 (wfds != NULL) ? &wfds->sds : NULL, 1216 (wfds != NULL) ? &wfds->sds : NULL,
1205 (efds != NULL) ? &efds->sds : NULL, 1217 (efds != NULL) ? &efds->sds : NULL,
1206 (timeout.rel_value == 1218 (timeout.rel_value ==
1207 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv); 1219 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1208 1220
1209#else 1221#else
1210#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) 1222#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
@@ -1215,10 +1227,10 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1215 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value; 1227 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1216 /* select() may be used as a portable way to sleep */ 1228 /* select() may be used as a portable way to sleep */
1217 if (!(rfds || wfds || efds)) 1229 if (!(rfds || wfds || efds))
1218 { 1230 {
1219 Sleep (ms_total); 1231 Sleep (ms_total);
1220 return 0; 1232 return 0;
1221 } 1233 }
1222 1234
1223 /* Events for sockets */ 1235 /* Events for sockets */
1224 if (!hEventRead) 1236 if (!hEventRead)
@@ -1254,212 +1266,217 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1254 FD_ZERO (&bexcept); 1266 FD_ZERO (&bexcept);
1255#endif 1267#endif
1256 if (rfds) 1268 if (rfds)
1257 { 1269 {
1258 FD_COPY (&rfds->sds, &aread); 1270 FD_COPY (&rfds->sds, &aread);
1259#if DEBUG_NETWORK 1271#if DEBUG_NETWORK
1260 FD_COPY (&rfds->sds, &bread); 1272 FD_COPY (&rfds->sds, &bread);
1261#endif 1273#endif
1262 } 1274 }
1263 if (wfds) 1275 if (wfds)
1264 { 1276 {
1265 FD_COPY (&wfds->sds, &awrite); 1277 FD_COPY (&wfds->sds, &awrite);
1266#if DEBUG_NETWORK 1278#if DEBUG_NETWORK
1267 FD_COPY (&wfds->sds, &bwrite); 1279 FD_COPY (&wfds->sds, &bwrite);
1268#endif 1280#endif
1269 } 1281 }
1270 if (efds) 1282 if (efds)
1271 { 1283 {
1272 FD_COPY (&efds->sds, &aexcept); 1284 FD_COPY (&efds->sds, &aexcept);
1273#if DEBUG_NETWORK 1285#if DEBUG_NETWORK
1274 FD_COPY (&efds->sds, &bexcept); 1286 FD_COPY (&efds->sds, &bexcept);
1275#endif 1287#endif
1276 } 1288 }
1277 /* We will first Add the PIPES to the events */ 1289 /* We will first Add the PIPES to the events */
1278 /* Read Pipes */ 1290 /* Read Pipes */
1279 if (rfds && read_handles) 1291 if (rfds && read_handles)
1280 {
1281 struct GNUNET_CONTAINER_SList_Iterator *i;
1282
1283 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1284 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1285 GNUNET_CONTAINER_slist_next (i))
1286 { 1292 {
1287 struct GNUNET_DISK_FileHandle *fh; 1293 struct GNUNET_CONTAINER_SList_Iterator *i;
1288 1294
1289 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, 1295 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1290 NULL); 1296 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1291 if (fh->type == GNUNET_PIPE) 1297 GNUNET_CONTAINER_slist_next (i))
1292 { 1298 {
1293 /* Read zero bytes to check the status of the pipe */ 1299 struct GNUNET_DISK_FileHandle *fh;
1300
1301 fh =
1302 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1303 NULL);
1304 if (fh->type == GNUNET_PIPE)
1305 {
1306 /* Read zero bytes to check the status of the pipe */
1294#if DEBUG_NETWORK 1307#if DEBUG_NETWORK
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Reading 0 bytes from the pipe 0x%x\n", fh->h); 1309 "Reading 0 bytes from the pipe 0x%x\n", fh->h);
1297#endif 1310#endif
1298 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead)) 1311 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1299 { 1312 {
1300 DWORD error_code = GetLastError (); 1313 DWORD error_code = GetLastError ();
1301 1314
1302 if (error_code == ERROR_IO_PENDING) 1315 if (error_code == ERROR_IO_PENDING)
1303 { 1316 {
1304#if DEBUG_NETWORK 1317#if DEBUG_NETWORK
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1318 LOG (GNUNET_ERROR_TYPE_DEBUG,
1306 "Adding the pipe's 0x%x overlapped event to the array as %d\n", 1319 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1307 fh->h, nhandles); 1320 fh->h, nhandles);
1308#endif 1321#endif
1309 handle_array[nhandles++] = fh->oOverlapRead->hEvent; 1322 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1310 readArray[readPipes++] = fh; 1323 readArray[readPipes++] = fh;
1311 } 1324 }
1312 /* 1325 /*
1313 * else 1326 * else
1314 * { 1327 * {
1315 * SetErrnoFromWinError (error_code); 1328 * SetErrnoFromWinError (error_code);
1316 * } 1329 * }
1317 */ 1330 */
1318 } 1331 }
1319 else 1332 else
1320 { 1333 {
1321#if DEBUG_NETWORK 1334#if DEBUG_NETWORK
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "Adding the read ready event to the array as %d\n", 1336 "Adding the read ready event to the array as %d\n",
1324 nhandles); 1337 nhandles);
1325#endif 1338#endif
1326 handle_array[nhandles++] = hEventReadReady; 1339 handle_array[nhandles++] = hEventReadReady;
1327 readArray[readPipes++] = fh; 1340 readArray[readPipes++] = fh;
1328 } 1341 }
1329 } 1342 }
1330 else 1343 else
1331 { 1344 {
1332 GNUNET_CONTAINER_slist_add (handles_read, 1345 GNUNET_CONTAINER_slist_add (handles_read,
1333 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1346 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1334 fh, sizeof (struct GNUNET_DISK_FileHandle)); 1347 fh,
1335 } 1348 sizeof (struct
1349 GNUNET_DISK_FileHandle));
1350 }
1351 }
1352 GNUNET_CONTAINER_slist_iter_destroy (i);
1336 } 1353 }
1337 GNUNET_CONTAINER_slist_iter_destroy (i);
1338 }
1339 if (wfds && write_handles) 1354 if (wfds && write_handles)
1340 { 1355 {
1341#if DEBUG_NETWORK 1356#if DEBUG_NETWORK
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 "Adding the write ready event to the array as %d\n", nhandles); 1358 "Adding the write ready event to the array as %d\n", nhandles);
1344#endif 1359#endif
1345 handle_array[nhandles++] = hEventPipeWrite; 1360 handle_array[nhandles++] = hEventPipeWrite;
1346 writePipePos = nhandles; 1361 writePipePos = nhandles;
1347 } 1362 }
1348 if (efds && ex_handles) 1363 if (efds && ex_handles)
1349 {
1350 struct GNUNET_CONTAINER_SList_Iterator *i;
1351
1352 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1353 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1354 GNUNET_CONTAINER_slist_next (i))
1355 { 1364 {
1356 struct GNUNET_DISK_FileHandle *fh; 1365 struct GNUNET_CONTAINER_SList_Iterator *i;
1357 DWORD dwBytes; 1366
1358 1367 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1359 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, 1368 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1360 NULL); 1369 GNUNET_CONTAINER_slist_next (i))
1361 if (fh->type == GNUNET_PIPE) 1370 {
1362 { 1371 struct GNUNET_DISK_FileHandle *fh;
1363 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL)) 1372 DWORD dwBytes;
1364 { 1373
1365 GNUNET_CONTAINER_slist_add (handles_except, 1374 fh =
1366 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1375 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1367 fh, 1376 NULL);
1368 sizeof (struct GNUNET_DISK_FileHandle)); 1377 if (fh->type == GNUNET_PIPE)
1369 newretcode++; 1378 {
1370 } 1379 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1371 } 1380 {
1381 GNUNET_CONTAINER_slist_add (handles_except,
1382 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1383 fh,
1384 sizeof (struct
1385 GNUNET_DISK_FileHandle));
1386 newretcode++;
1387 }
1388 }
1389 }
1390 GNUNET_CONTAINER_slist_iter_destroy (i);
1372 } 1391 }
1373 GNUNET_CONTAINER_slist_iter_destroy (i);
1374 }
1375 if (nfds > 0) 1392 if (nfds > 0)
1376 {
1377 if (rfds)
1378 { 1393 {
1394 if (rfds)
1395 {
1379#if DEBUG_NETWORK 1396#if DEBUG_NETWORK
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "Adding the socket read event to the array as %d\n", 1398 "Adding the socket read event to the array as %d\n", nhandles);
1382 nhandles); 1399#endif
1383#endif 1400 handle_array[nhandles++] = hEventRead;
1384 handle_array[nhandles++] = hEventRead; 1401 nSockEvents++;
1385 nSockEvents++; 1402 for (i = 0; i < rfds->sds.fd_count; i++)
1386 for (i = 0; i < rfds->sds.fd_count; i++) 1403 {
1387 { 1404 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1388 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 1405 FD_ACCEPT | FD_READ | FD_CLOSE);
1389 FD_ACCEPT | FD_READ | FD_CLOSE); 1406 nsock++;
1390 nsock++; 1407 }
1391 } 1408 }
1392 } 1409 if (wfds)
1393 if (wfds) 1410 {
1394 { 1411 int wakeup = 0;
1395 int wakeup = 0;
1396 1412
1397#if DEBUG_NETWORK 1413#if DEBUG_NETWORK
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "Adding the socket write event to the array as %d\n", 1415 "Adding the socket write event to the array as %d\n",
1400 nhandles); 1416 nhandles);
1401#endif 1417#endif
1402 handle_array[nhandles++] = hEventWrite; 1418 handle_array[nhandles++] = hEventWrite;
1403 nSockEvents++; 1419 nSockEvents++;
1404 for (i = 0; i < wfds->sds.fd_count; i++) 1420 for (i = 0; i < wfds->sds.fd_count; i++)
1405 { 1421 {
1406 DWORD error; 1422 DWORD error;
1407 int status; 1423 int status;
1408 1424
1409 status = send (wfds->sds.fd_array[i], NULL, 0, 0); 1425 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1410 error = GetLastError (); 1426 error = GetLastError ();
1411#if DEBUG_NETWORK 1427#if DEBUG_NETWORK
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1413 "pre-send to the socket %d returned %d (%u)\n", i, status, 1429 "pre-send to the socket %d returned %d (%u)\n", i, status,
1414 error); 1430 error);
1415#endif 1431#endif
1416 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)) 1432 if (status == 0
1417 wakeup = 1; 1433 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1418 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 1434 wakeup = 1;
1419 FD_WRITE | FD_CONNECT | FD_CLOSE); 1435 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1420 nsock++; 1436 FD_WRITE | FD_CONNECT | FD_CLOSE);
1421 } 1437 nsock++;
1422 if (wakeup) 1438 }
1423 SetEvent (hEventWrite); 1439 if (wakeup)
1424 } 1440 SetEvent (hEventWrite);
1425 if (efds) 1441 }
1426 { 1442 if (efds)
1443 {
1427#if DEBUG_NETWORK 1444#if DEBUG_NETWORK
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "Adding the socket error event to the array as %d\n", 1446 "Adding the socket error event to the array as %d\n",
1430 nhandles); 1447 nhandles);
1431#endif 1448#endif
1432 handle_array[nhandles++] = hEventException; 1449 handle_array[nhandles++] = hEventException;
1433 nSockEvents++; 1450 nSockEvents++;
1434 for (i = 0; i < efds->sds.fd_count; i++) 1451 for (i = 0; i < efds->sds.fd_count; i++)
1435 { 1452 {
1436 WSAEventSelect (efds->sds.fd_array[i], hEventException, 1453 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1437 FD_OOB | FD_CLOSE); 1454 FD_OOB | FD_CLOSE);
1438 nsock++; 1455 nsock++;
1439 } 1456 }
1457 }
1440 } 1458 }
1441 }
1442 1459
1443 handle_array[nhandles] = NULL; 1460 handle_array[nhandles] = NULL;
1444 1461
1445#if DEBUG_NETWORK 1462#if DEBUG_NETWORK
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds); 1463 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles); 1464 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode); 1465 LOG (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total); 1466 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1450#endif 1467#endif
1451 1468
1452 if (nhandles) 1469 if (nhandles)
1453 returncode = 1470 returncode =
1454 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total); 1471 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1455#if DEBUG_NETWORK 1472#if DEBUG_NETWORK
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", 1473 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1457 returncode); 1474 returncode);
1458#endif 1475#endif
1459 1476
1460 returnedpos = returncode - WAIT_OBJECT_0; 1477 returnedpos = returncode - WAIT_OBJECT_0;
1461#if DEBUG_NETWORK 1478#if DEBUG_NETWORK
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos); 1479 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1463#endif 1480#endif
1464 1481
1465 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */ 1482 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
@@ -1468,237 +1485,241 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1468 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles); 1485 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1469 */ 1486 */
1470 if (nhandles && (returnedpos < nhandles)) 1487 if (nhandles && (returnedpos < nhandles))
1471 {
1472 DWORD waitstatus;
1473
1474 /* Do the select */
1475 if (nfds)
1476 { 1488 {
1477 struct timeval tvslice; 1489 DWORD waitstatus;
1478 1490
1479 tvslice.tv_sec = 0; 1491 /* Do the select */
1480 tvslice.tv_usec = 10; 1492 if (nfds)
1481 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice); 1493 {
1482 if (retcode == -1) 1494 struct timeval tvslice;
1483 retcode = 0; 1495
1496 tvslice.tv_sec = 0;
1497 tvslice.tv_usec = 10;
1498 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1499 if (retcode == -1)
1500 retcode = 0;
1484#if DEBUG_NETWORK 1501#if DEBUG_NETWORK
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode); 1502 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1486#endif 1503#endif
1487 } 1504 }
1488 /* FIXME: <= writePipePos? Really? */ 1505 /* FIXME: <= writePipePos? Really? */
1489 if ((writePipePos != -1) && (returnedpos <= writePipePos)) 1506 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1490 { 1507 {
1491 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles); 1508 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1492 retcode += write_handles; 1509 retcode += write_handles;
1493#if DEBUG_NETWORK 1510#if DEBUG_NETWORK
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); 1511 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1495#endif 1512#endif
1496 } 1513 }
1497#if DEBUG_NETWORK 1514#if DEBUG_NETWORK
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes); 1515 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1499#endif 1516#endif
1500 /* We have some pipes ready for read. */ 1517 /* We have some pipes ready for read. */
1501 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */ 1518 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1502 1519
1503 if (returnedpos < readPipes) 1520 if (returnedpos < readPipes)
1504 { 1521 {
1505 /* 1522 /*
1506 * for (i = 0; i < readPipes; i++) 1523 * for (i = 0; i < readPipes; i++)
1507 * { 1524 * {
1508 * waitstatus = WaitForSingleObject (handle_array[i], 0); 1525 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1509 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus); 1526 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1510 * if (waitstatus != WAIT_OBJECT_0) 1527 * if (waitstatus != WAIT_OBJECT_0)
1511 * continue; 1528 * continue;
1512 * GNUNET_CONTAINER_slist_add (handles_read, 1529 * GNUNET_CONTAINER_slist_add (handles_read,
1513 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1530 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1514 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle)); 1531 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1515 * retcode++; 1532 * retcode++;
1516 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n"); 1533 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1517 * } 1534 * }
1518 */ 1535 */
1519 for (i = 0; i < readPipes; i++) 1536 for (i = 0; i < readPipes; i++)
1520 { 1537 {
1521 DWORD error; 1538 DWORD error;
1522 BOOL bret; 1539 BOOL bret;
1523 1540
1524 SetLastError (0); 1541 SetLastError (0);
1525 waitstatus = 0; 1542 waitstatus = 0;
1526 bret = 1543 bret =
1527 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL); 1544 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus,
1528 error = GetLastError (); 1545 NULL);
1546 error = GetLastError ();
1529#if DEBUG_NETWORK 1547#if DEBUG_NETWORK
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1548 LOG (GNUNET_ERROR_TYPE_DEBUG,
1531 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n", 1549 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1532 i, readArray[i]->h, bret, waitstatus, error); 1550 i, readArray[i]->h, bret, waitstatus, error);
1533#endif 1551#endif
1534 if (bret == 0) 1552 if (bret == 0)
1535 { 1553 {
1536 if (error != ERROR_BROKEN_PIPE) 1554 if (error != ERROR_BROKEN_PIPE)
1537 continue; 1555 continue;
1538 } 1556 }
1539 else if (waitstatus <= 0) 1557 else if (waitstatus <= 0)
1540 continue; 1558 continue;
1541 GNUNET_CONTAINER_slist_add (handles_read, 1559 GNUNET_CONTAINER_slist_add (handles_read,
1542 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1560 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1543 readArray[i], 1561 readArray[i],
1544 sizeof (struct GNUNET_DISK_FileHandle)); 1562 sizeof (struct
1545 retcode++; 1563 GNUNET_DISK_FileHandle));
1564 retcode++;
1546#if DEBUG_NETWORK 1565#if DEBUG_NETWORK
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n", 1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1548 readArray[i], readArray[i]->h); 1567 readArray[i], readArray[i]->h);
1549#endif 1568#endif
1550 } 1569 }
1551 } 1570 }
1552 waitstatus = WaitForSingleObject (hEventWrite, 0); 1571 waitstatus = WaitForSingleObject (hEventWrite, 0);
1553#if DEBUG_NETWORK 1572#if DEBUG_NETWORK
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1573 LOG (GNUNET_ERROR_TYPE_DEBUG,
1555 "Wait for the write event returned %d\n", waitstatus); 1574 "Wait for the write event returned %d\n", waitstatus);
1556#endif 1575#endif
1557 if (waitstatus == WAIT_OBJECT_0) 1576 if (waitstatus == WAIT_OBJECT_0)
1558 { 1577 {
1559 for (i = 0; i < wfds->sds.fd_count; i++) 1578 for (i = 0; i < wfds->sds.fd_count; i++)
1560 { 1579 {
1561 DWORD error; 1580 DWORD error;
1562 int status; 1581 int status;
1563 int so_error = 0; 1582 int so_error = 0;
1564 int sizeof_so_error = sizeof (so_error); 1583 int sizeof_so_error = sizeof (so_error);
1565 int gso_result = 1584 int gso_result =
1566 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR, 1585 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1567 (char *) &so_error, &sizeof_so_error); 1586 (char *) &so_error, &sizeof_so_error);
1568 1587
1569 status = send (wfds->sds.fd_array[i], NULL, 0, 0); 1588 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1570 error = GetLastError (); 1589 error = GetLastError ();
1571#if DEBUG_NETWORK 1590#if DEBUG_NETWORK
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1591 LOG (GNUNET_ERROR_TYPE_DEBUG,
1573 "send to the socket %d returned %d (%u)\n", i, status, 1592 "send to the socket %d returned %d (%u)\n", i, status,
1574 error); 1593 error);
1575#endif 1594#endif
1576 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) || 1595 if (status == 0
1577 (status == -1 && gso_result == 0 && error == WSAENOTCONN && 1596 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)
1578 so_error == WSAECONNREFUSED)) 1597 || (status == -1 && gso_result == 0 && error == WSAENOTCONN
1579 { 1598 && so_error == WSAECONNREFUSED))
1580 FD_SET (wfds->sds.fd_array[i], &awrite); 1599 {
1581 retcode += 1; 1600 FD_SET (wfds->sds.fd_array[i], &awrite);
1582 } 1601 retcode += 1;
1583 } 1602 }
1603 }
1604 }
1584 } 1605 }
1585 }
1586#if DEBUG_NETWORK 1606#if DEBUG_NETWORK
1587 if (!nhandles || (returnedpos >= nhandles)) 1607 if (!nhandles || (returnedpos >= nhandles))
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1589 "Returning from _select() with nothing!\n");
1590#endif 1609#endif
1591 if (rfds) 1610 if (rfds)
1592 {
1593 struct GNUNET_CONTAINER_SList_Iterator *t;
1594
1595 for (i = 0; i < rfds->sds.fd_count; i++)
1596 { 1611 {
1597 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0); 1612 struct GNUNET_CONTAINER_SList_Iterator *t;
1598 nsock++;
1599 }
1600 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1601 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1602 GNUNET_CONTAINER_slist_next (t))
1603 {
1604 struct GNUNET_DISK_FileHandle *fh;
1605 1613
1606 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1614 for (i = 0; i < rfds->sds.fd_count; i++)
1607 NULL); 1615 {
1608 if (fh->type == GNUNET_PIPE) 1616 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1609 { 1617 nsock++;
1610 CancelIo (fh->h); 1618 }
1611 } 1619 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1612 } 1620 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1613 GNUNET_CONTAINER_slist_iter_destroy (t); 1621 GNUNET_CONTAINER_slist_next (t))
1622 {
1623 struct GNUNET_DISK_FileHandle *fh;
1624
1625 fh =
1626 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1627 NULL);
1628 if (fh->type == GNUNET_PIPE)
1629 {
1630 CancelIo (fh->h);
1631 }
1632 }
1633 GNUNET_CONTAINER_slist_iter_destroy (t);
1614#if DEBUG_NETWORK 1634#if DEBUG_NETWORK
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n"); 1635 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1616#endif 1636#endif
1617 GNUNET_NETWORK_fdset_zero (rfds); 1637 GNUNET_NETWORK_fdset_zero (rfds);
1618 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1638 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1619 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode); 1639 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1620 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read); 1640 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1621 } 1641 }
1622 if (wfds) 1642 if (wfds)
1623 {
1624 for (i = 0; i < wfds->sds.fd_count; i++)
1625 { 1643 {
1626 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0); 1644 for (i = 0; i < wfds->sds.fd_count; i++)
1627 nsock++; 1645 {
1628 } 1646 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1647 nsock++;
1648 }
1629#if DEBUG_NETWORK 1649#if DEBUG_NETWORK
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n"); 1650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1631#endif 1651#endif
1632 GNUNET_NETWORK_fdset_zero (wfds); 1652 GNUNET_NETWORK_fdset_zero (wfds);
1633 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1653 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1634 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode); 1654 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1635 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write); 1655 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1636 } 1656 }
1637 if (efds) 1657 if (efds)
1638 {
1639 for (i = 0; i < efds->sds.fd_count; i++)
1640 { 1658 {
1641 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0); 1659 for (i = 0; i < efds->sds.fd_count; i++)
1642 nsock++; 1660 {
1643 } 1661 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1662 nsock++;
1663 }
1644#if DEBUG_NETWORK 1664#if DEBUG_NETWORK
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n"); 1665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1646#endif 1666#endif
1647 GNUNET_NETWORK_fdset_zero (efds); 1667 GNUNET_NETWORK_fdset_zero (efds);
1648 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1668 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1649 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode); 1669 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1650 GNUNET_CONTAINER_slist_append (efds->handles, handles_except); 1670 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1651 } 1671 }
1652 GNUNET_CONTAINER_slist_destroy (handles_read); 1672 GNUNET_CONTAINER_slist_destroy (handles_read);
1653 GNUNET_CONTAINER_slist_destroy (handles_write); 1673 GNUNET_CONTAINER_slist_destroy (handles_write);
1654 GNUNET_CONTAINER_slist_destroy (handles_except); 1674 GNUNET_CONTAINER_slist_destroy (handles_except);
1655#if DEBUG_NETWORK 1675#if DEBUG_NETWORK
1656 if (rfds) 1676 if (rfds)
1657 {
1658 struct GNUNET_CONTAINER_SList_Iterator *t;
1659
1660 for (i = 0; i < bread.fd_count; i++)
1661 { 1677 {
1662 if (bread.fd_array[i] != 0) 1678 struct GNUNET_CONTAINER_SList_Iterator *t;
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1664 bread.fd_array[i],
1665 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1666 "NOT SET");
1667 }
1668 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1669 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1670 GNUNET_CONTAINER_slist_next (t))
1671 {
1672 struct GNUNET_DISK_FileHandle *fh;
1673 1679
1674 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1680 for (i = 0; i < bread.fd_count; i++)
1675 NULL); 1681 {
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h); 1682 if (bread.fd_array[i] != 0)
1683 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1684 bread.fd_array[i],
1685 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1686 "NOT SET");
1687 }
1688 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1689 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1690 GNUNET_CONTAINER_slist_next (t))
1691 {
1692 struct GNUNET_DISK_FileHandle *fh;
1693
1694 fh =
1695 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1696 NULL);
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1698 }
1677 } 1699 }
1678 }
1679 if (wfds) 1700 if (wfds)
1680 {
1681 for (i = 0; i < bwrite.fd_count; i++)
1682 { 1701 {
1683 if (bwrite.fd_array[i] != 0) 1702 for (i = 0; i < bwrite.fd_count; i++)
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n", 1703 {
1685 bwrite.fd_array[i], 1704 if (bwrite.fd_array[i] != 0)
1686 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : 1705 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1687 "NOT SET"); 1706 bwrite.fd_array[i],
1707 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" :
1708 "NOT SET");
1709 }
1688 } 1710 }
1689 }
1690 if (efds) 1711 if (efds)
1691 {
1692 for (i = 0; i < bexcept.fd_count; i++)
1693 { 1712 {
1694 if (bexcept.fd_array[i] != 0) 1713 for (i = 0; i < bexcept.fd_count; i++)
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n", 1714 {
1696 bexcept.fd_array[i], 1715 if (bexcept.fd_array[i] != 0)
1697 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : 1716 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1698 "NOT SET"); 1717 bexcept.fd_array[i],
1718 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" :
1719 "NOT SET");
1720 }
1699 } 1721 }
1700 } 1722 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1702#endif 1723#endif
1703 if (nhandles && (returnedpos < nhandles)) 1724 if (nhandles && (returnedpos < nhandles))
1704 return retcode; 1725 return retcode;
diff --git a/src/util/os_installation.c b/src/util/os_installation.c
index 1f4c5f000..688cc448e 100644
--- a/src/util/os_installation.c
+++ b/src/util/os_installation.c
@@ -38,6 +38,10 @@
38#include <mach-o/dyld.h> 38#include <mach-o/dyld.h>
39#endif 39#endif
40 40
41#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
42
43#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
44
41#if LINUX 45#if LINUX
42/** 46/**
43 * Try to determine path by reading /proc/PID/exe 47 * Try to determine path by reading /proc/PID/exe
@@ -56,16 +60,16 @@ get_path_from_proc_maps ()
56 if (f == NULL) 60 if (f == NULL)
57 return NULL; 61 return NULL;
58 while (NULL != fgets (line, sizeof (line), f)) 62 while (NULL != fgets (line, sizeof (line), f))
59 {
60 if ((1 ==
61 sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) &&
62 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
63 { 63 {
64 lgu[0] = '\0'; 64 if ((1 ==
65 fclose (f); 65 sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
66 return GNUNET_strdup (dir); 66 dir)) && (NULL != (lgu = strstr (dir, "libgnunetutil"))))
67 {
68 lgu[0] = '\0';
69 fclose (f);
70 return GNUNET_strdup (dir);
71 }
67 } 72 }
68 }
69 fclose (f); 73 fclose (f);
70 return NULL; 74 return NULL;
71} 75}
@@ -83,19 +87,19 @@ get_path_from_proc_exe ()
83 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ()); 87 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
84 size = readlink (fn, lnk, sizeof (lnk) - 1); 88 size = readlink (fn, lnk, sizeof (lnk) - 1);
85 if (size <= 0) 89 if (size <= 0)
86 { 90 {
87 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); 91 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
88 return NULL; 92 return NULL;
89 } 93 }
90 GNUNET_assert (size < sizeof (lnk)); 94 GNUNET_assert (size < sizeof (lnk));
91 lnk[size] = '\0'; 95 lnk[size] = '\0';
92 while ((lnk[size] != '/') && (size > 0)) 96 while ((lnk[size] != '/') && (size > 0))
93 size--; 97 size--;
94 if ((size < 4) || (lnk[size - 4] != '/')) 98 if ((size < 4) || (lnk[size - 4] != '/'))
95 { 99 {
96 /* not installed in "/bin/" -- binary path probably useless */ 100 /* not installed in "/bin/" -- binary path probably useless */
97 return NULL; 101 return NULL;
98 } 102 }
99 lnk[size] = '\0'; 103 lnk[size] = '\0';
100 return GNUNET_strdup (lnk); 104 return GNUNET_strdup (lnk);
101} 105}
@@ -134,7 +138,7 @@ get_path_from_NSGetExecutablePath ()
134 138
135 path = NULL; 139 path = NULL;
136 func = 140 func =
137 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); 141 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
138 if (!func) 142 if (!func)
139 return NULL; 143 return NULL;
140 path = &zero; 144 path = &zero;
@@ -146,10 +150,10 @@ get_path_from_NSGetExecutablePath ()
146 path = GNUNET_malloc (len); 150 path = GNUNET_malloc (len);
147 ret = func (path, &len); 151 ret = func (path, &len);
148 if (ret != 0) 152 if (ret != 0)
149 { 153 {
150 GNUNET_free (path); 154 GNUNET_free (path);
151 return NULL; 155 return NULL;
152 } 156 }
153 len = strlen (path); 157 len = strlen (path);
154 while ((path[len] != '/') && (len > 0)) 158 while ((path[len] != '/') && (len > 0))
155 len--; 159 len--;
@@ -168,22 +172,22 @@ get_path_from_dyld_image ()
168 p = NULL; 172 p = NULL;
169 c = _dyld_image_count (); 173 c = _dyld_image_count ();
170 for (i = 0; i < c; i++) 174 for (i = 0; i < c; i++)
171 {
172 if (_dyld_get_image_header (i) == &_mh_dylib_header)
173 { 175 {
174 path = _dyld_get_image_name (i); 176 if (_dyld_get_image_header (i) == &_mh_dylib_header)
175 if (path != NULL && strlen (path) > 0) 177 {
176 { 178 path = _dyld_get_image_name (i);
177 p = strdup (path); 179 if (path != NULL && strlen (path) > 0)
178 s = p + strlen (p); 180 {
179 while ((s > p) && (*s != '/')) 181 p = strdup (path);
180 s--; 182 s = p + strlen (p);
181 s++; 183 while ((s > p) && (*s != '/'))
182 *s = '\0'; 184 s--;
183 } 185 s++;
184 break; 186 *s = '\0';
187 }
188 break;
189 }
185 } 190 }
186 }
187 return p; 191 return p;
188} 192}
189#endif 193#endif
@@ -207,30 +211,30 @@ get_path_from_PATH (const char *binary)
207 p = getenv ("PATH"); 211 p = getenv ("PATH");
208 if (p == NULL) 212 if (p == NULL)
209 return NULL; 213 return NULL;
210 path = GNUNET_strdup (p); /* because we write on it */ 214 path = GNUNET_strdup (p); /* because we write on it */
211 buf = GNUNET_malloc (strlen (path) + 20); 215 buf = GNUNET_malloc (strlen (path) + 20);
212 pos = path; 216 pos = path;
213 while (NULL != (end = strchr (pos, PATH_SEPARATOR))) 217 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
214 { 218 {
215 *end = '\0'; 219 *end = '\0';
216 sprintf (buf, "%s/%s", pos, binary); 220 sprintf (buf, "%s/%s", pos, binary);
217 if (GNUNET_DISK_file_test (buf) == GNUNET_YES) 221 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
222 {
223 pos = GNUNET_strdup (pos);
224 GNUNET_free (buf);
225 GNUNET_free (path);
226 return pos;
227 }
228 pos = end + 1;
229 }
230 sprintf (buf, "%s/%s", pos, binary);
231 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
218 { 232 {
219 pos = GNUNET_strdup (pos); 233 pos = GNUNET_strdup (pos);
220 GNUNET_free (buf); 234 GNUNET_free (buf);
221 GNUNET_free (path); 235 GNUNET_free (path);
222 return pos; 236 return pos;
223 } 237 }
224 pos = end + 1;
225 }
226 sprintf (buf, "%s/%s", pos, binary);
227 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
228 {
229 pos = GNUNET_strdup (pos);
230 GNUNET_free (buf);
231 GNUNET_free (path);
232 return pos;
233 }
234 GNUNET_free (buf); 238 GNUNET_free (buf);
235 GNUNET_free (path); 239 GNUNET_free (path);
236 return NULL; 240 return NULL;
@@ -286,10 +290,10 @@ os_get_gnunet_path ()
286 if (ret != NULL) 290 if (ret != NULL)
287 return ret; 291 return ret;
288 /* other attempts here */ 292 /* other attempts here */
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 293 LOG (GNUNET_ERROR_TYPE_ERROR,
290 _ 294 _
291 ("Could not determine installation path for %s. Set `%s' environment variable.\n"), 295 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
292 "GNUnet", "GNUNET_PREFIX"); 296 "GNUnet", "GNUNET_PREFIX");
293 return NULL; 297 return NULL;
294} 298}
295 299
@@ -355,11 +359,11 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
355 359
356 n = strlen (execpath); 360 n = strlen (execpath);
357 if (n == 0) 361 if (n == 0)
358 { 362 {
359 /* should never happen, but better safe than sorry */ 363 /* should never happen, but better safe than sorry */
360 GNUNET_free (execpath); 364 GNUNET_free (execpath);
361 return NULL; 365 return NULL;
362 } 366 }
363 /* remove filename itself */ 367 /* remove filename itself */
364 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) 368 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
365 execpath[--n] = '\0'; 369 execpath[--n] = '\0';
@@ -368,59 +372,62 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
368 if ((n > 5) && 372 if ((n > 5) &&
369 ((0 == strcasecmp (&execpath[n - 5], "lib32")) || 373 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
370 (0 == strcasecmp (&execpath[n - 5], "lib64")))) 374 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
371 {
372 if (dirkind != GNUNET_OS_IPK_LIBDIR)
373 { 375 {
374 /* strip '/lib32' or '/lib64' */ 376 if (dirkind != GNUNET_OS_IPK_LIBDIR)
375 execpath[n - 5] = '\0'; 377 {
376 n -= 5; 378 /* strip '/lib32' or '/lib64' */
379 execpath[n - 5] = '\0';
380 n -= 5;
381 }
382 else
383 isbasedir = 0;
377 } 384 }
378 else
379 isbasedir = 0;
380 }
381 else if ((n > 3) && 385 else if ((n > 3) &&
382 ((0 == strcasecmp (&execpath[n - 3], "bin")) || 386 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
383 (0 == strcasecmp (&execpath[n - 3], "lib")))) 387 (0 == strcasecmp (&execpath[n - 3], "lib"))))
384 { 388 {
385 /* strip '/bin' or '/lib' */ 389 /* strip '/bin' or '/lib' */
386 execpath[n - 3] = '\0'; 390 execpath[n - 3] = '\0';
387 n -= 3; 391 n -= 3;
388 } 392 }
389 /* in case this was a directory named foo-bin, remove "foo-" */ 393 /* in case this was a directory named foo-bin, remove "foo-" */
390 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) 394 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
391 execpath[--n] = '\0'; 395 execpath[--n] = '\0';
392 switch (dirkind) 396 switch (dirkind)
393 { 397 {
394 case GNUNET_OS_IPK_PREFIX: 398 case GNUNET_OS_IPK_PREFIX:
395 case GNUNET_OS_IPK_SELF_PREFIX: 399 case GNUNET_OS_IPK_SELF_PREFIX:
396 dirname = DIR_SEPARATOR_STR; 400 dirname = DIR_SEPARATOR_STR;
397 break; 401 break;
398 case GNUNET_OS_IPK_BINDIR: 402 case GNUNET_OS_IPK_BINDIR:
399 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; 403 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
400 break; 404 break;
401 case GNUNET_OS_IPK_LIBDIR: 405 case GNUNET_OS_IPK_LIBDIR:
402 if (isbasedir) 406 if (isbasedir)
407 dirname =
408 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
409 DIR_SEPARATOR_STR;
410 else
411 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
412 break;
413 case GNUNET_OS_IPK_DATADIR:
403 dirname = 414 dirname =
404 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR; 415 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
405 else 416 DIR_SEPARATOR_STR;
406 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR; 417 break;
407 break; 418 case GNUNET_OS_IPK_LOCALEDIR:
408 case GNUNET_OS_IPK_DATADIR: 419 dirname =
409 dirname = 420 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
410 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR; 421 DIR_SEPARATOR_STR;
411 break; 422 break;
412 case GNUNET_OS_IPK_LOCALEDIR: 423 case GNUNET_OS_IPK_ICONDIR:
413 dirname = 424 dirname =
414 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR; 425 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
415 break; 426 break;
416 case GNUNET_OS_IPK_ICONDIR: 427 default:
417 dirname = 428 GNUNET_free (execpath);
418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; 429 return NULL;
419 break; 430 }
420 default:
421 GNUNET_free (execpath);
422 return NULL;
423 }
424 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); 431 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
425 sprintf (tmp, "%s%s", execpath, dirname); 432 sprintf (tmp, "%s%s", execpath, dirname);
426 GNUNET_free (execpath); 433 GNUNET_free (execpath);
@@ -453,59 +460,59 @@ GNUNET_OS_check_helper_binary (const char *binary)
453 GNUNET_asprintf (&binaryexe, "%s.exe", binary); 460 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
454 p = get_path_from_PATH (binaryexe); 461 p = get_path_from_PATH (binaryexe);
455 if (p != NULL) 462 if (p != NULL)
456 { 463 {
457 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe); 464 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
458 GNUNET_free (p); 465 GNUNET_free (p);
459 p = pf; 466 p = pf;
460 } 467 }
461 free (binaryexe); 468 free (binaryexe);
462#else 469#else
463 p = get_path_from_PATH (binary); 470 p = get_path_from_PATH (binary);
464 if (p != NULL) 471 if (p != NULL)
465 { 472 {
466 GNUNET_asprintf (&pf, "%s/%s", p, binary); 473 GNUNET_asprintf (&pf, "%s/%s", p, binary);
467 GNUNET_free (p); 474 GNUNET_free (p);
468 p = pf; 475 p = pf;
469 } 476 }
470#endif 477#endif
471 if (p == NULL) 478 if (p == NULL)
472 { 479 {
473 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 480 LOG (GNUNET_ERROR_TYPE_INFO,
474 _("Could not find binary `%s' in PATH!\n"), binary); 481 _("Could not find binary `%s' in PATH!\n"), binary);
475 return GNUNET_SYSERR; 482 return GNUNET_SYSERR;
476 } 483 }
477 if (0 != STAT (p, &statbuf)) 484 if (0 != STAT (p, &statbuf))
478 { 485 {
479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p, 486 LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
480 STRERROR (errno)); 487 STRERROR (errno));
481 GNUNET_free (p); 488 GNUNET_free (p);
482 return GNUNET_SYSERR; 489 return GNUNET_SYSERR;
483 } 490 }
484#ifndef MINGW 491#ifndef MINGW
485 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0)) 492 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
486 { 493 {
487 GNUNET_free (p); 494 GNUNET_free (p);
488 return GNUNET_YES; 495 return GNUNET_YES;
489 } 496 }
490 if (0 == ACCESS (p, X_OK)) 497 if (0 == ACCESS (p, X_OK))
491 { 498 {
492 GNUNET_free (p); 499 GNUNET_free (p);
493 return GNUNET_NO; 500 return GNUNET_NO;
494 } 501 }
495 GNUNET_free (p); 502 GNUNET_free (p);
496 return GNUNET_SYSERR; 503 return GNUNET_SYSERR;
497#else 504#else
498 GNUNET_free (p); 505 GNUNET_free (p);
499 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); 506 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
500 if (INVALID_SOCKET == rawsock) 507 if (INVALID_SOCKET == rawsock)
501 { 508 {
502 DWORD err = GetLastError (); 509 DWORD err = GetLastError ();
503 510
504 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 511 LOG (GNUNET_ERROR_TYPE_INFO,
505 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", 512 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n",
506 err); 513 err);
507 return GNUNET_NO; /* not running as administrator */ 514 return GNUNET_NO; /* not running as administrator */
508 } 515 }
509 closesocket (rawsock); 516 closesocket (rawsock);
510 return GNUNET_YES; 517 return GNUNET_YES;
511#endif 518#endif
diff --git a/src/util/os_network.c b/src/util/os_network.c
index b55789c99..88fefd066 100644
--- a/src/util/os_network.c
+++ b/src/util/os_network.c
@@ -31,6 +31,10 @@
31#include "gnunet_common.h" 31#include "gnunet_common.h"
32#include "gnunet_os_lib.h" 32#include "gnunet_os_lib.h"
33 33
34
35#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36
37
34/** 38/**
35 * @brief Enumerate all network interfaces 39 * @brief Enumerate all network interfaces
36 * 40 *
@@ -39,7 +43,7 @@
39 */ 43 */
40void 44void
41GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, 45GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
42 void *proc_cls) 46 void *proc_cls)
43{ 47{
44#ifdef MINGW 48#ifdef MINGW
45 PMIB_IFTABLE pTable; 49 PMIB_IFTABLE pTable;
@@ -48,118 +52,121 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
48 IPAddr theIP; 52 IPAddr theIP;
49 53
50 /* Determine our external NIC */ 54 /* Determine our external NIC */
51 theIP = inet_addr ("192.0.34.166"); /* www.example.com */ 55 theIP = inet_addr ("192.0.34.166"); /* www.example.com */
52 if ((!GNGetBestInterface) || 56 if ((!GNGetBestInterface) ||
53 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR)) 57 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR))
54 { 58 {
55 dwExternalNIC = 0; 59 dwExternalNIC = 0;
56 } 60 }
57 61
58 /* Enumerate NICs */ 62 /* Enumerate NICs */
59 EnumNICs (&pTable, &pAddrTable); 63 EnumNICs (&pTable, &pAddrTable);
60 64
61 if (pTable) 65 if (pTable)
62 {
63 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
64 { 66 {
65 char szEntry[1001]; 67 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
66 DWORD dwIP = 0; 68 {
67 PIP_ADAPTER_INFO pAdapterInfo; 69 char szEntry[1001];
68 PIP_ADAPTER_INFO pAdapter = NULL; 70 DWORD dwIP = 0;
69 DWORD dwRetVal = 0; 71 PIP_ADAPTER_INFO pAdapterInfo;
70 72 PIP_ADAPTER_INFO pAdapter = NULL;
71 /* Get IP-Address */ 73 DWORD dwRetVal = 0;
72 int i; 74
73 75 /* Get IP-Address */
74 for (i = 0; i < pAddrTable->dwNumEntries; i++) 76 int i;
75 { 77
76 if (pAddrTable->table[i].dwIndex == pTable->table[dwIfIdx].dwIndex) 78 for (i = 0; i < pAddrTable->dwNumEntries; i++)
77 { 79 {
78 dwIP = pAddrTable->table[i].dwAddr; 80 if (pAddrTable->table[i].dwIndex ==
79 break; 81 pTable->table[dwIfIdx].dwIndex)
80 } 82 {
81 } 83 dwIP = pAddrTable->table[i].dwAddr;
82 84 break;
83 if (dwIP) 85 }
84 { 86 }
85 BYTE bPhysAddr[MAXLEN_PHYSADDR]; 87
86 char *pszIfName = NULL; 88 if (dwIP)
87 char dst[INET_ADDRSTRLEN]; 89 {
88 struct sockaddr_in sa; 90 BYTE bPhysAddr[MAXLEN_PHYSADDR];
89 91 char *pszIfName = NULL;
90 /* Get friendly interface name */ 92 char dst[INET_ADDRSTRLEN];
91 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO)); 93 struct sockaddr_in sa;
92 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); 94
93 95 /* Get friendly interface name */
94 /* Make an initial call to GetAdaptersInfo to get 96 pAdapterInfo =
95 * the necessary size into the ulOutBufLen variable */ 97 (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
96 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) == 98 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
97 ERROR_BUFFER_OVERFLOW) 99
98 { 100 /* Make an initial call to GetAdaptersInfo to get
99 free (pAdapterInfo); 101 * the necessary size into the ulOutBufLen variable */
100 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); 102 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
101 } 103 ERROR_BUFFER_OVERFLOW)
102 104 {
103 if ((dwRetVal = 105 free (pAdapterInfo);
104 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR) 106 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
105 { 107 }
106 pAdapter = pAdapterInfo; 108
107 while (pAdapter) 109 if ((dwRetVal =
108 { 110 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
109 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index) 111 {
110 { 112 pAdapter = pAdapterInfo;
111 char szKey[251]; 113 while (pAdapter)
112 long lLen = 250; 114 {
113 115 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
114 sprintf (szKey, 116 {
115 "SYSTEM\\CurrentControlSet\\Control\\Network\\" 117 char szKey[251];
116 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection", 118 long lLen = 250;
117 pAdapter->AdapterName); 119
118 pszIfName = (char *) malloc (251); 120 sprintf (szKey,
119 if (QueryRegistry 121 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
120 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName, 122 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
121 &lLen) != ERROR_SUCCESS) 123 pAdapter->AdapterName);
122 { 124 pszIfName = (char *) malloc (251);
123 free (pszIfName); 125 if (QueryRegistry
124 pszIfName = NULL; 126 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
125 } 127 &lLen) != ERROR_SUCCESS)
126 } 128 {
127 pAdapter = pAdapter->Next; 129 free (pszIfName);
128 } 130 pszIfName = NULL;
129 } 131 }
130 free (pAdapterInfo); 132 }
131 133 pAdapter = pAdapter->Next;
132 /* Set entry */ 134 }
133 memset (bPhysAddr, 0, MAXLEN_PHYSADDR); 135 }
134 memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr, 136 free (pAdapterInfo);
135 pTable->table[dwIfIdx].dwPhysAddrLen); 137
136 138 /* Set entry */
137 snprintf (szEntry, 1000, "%s (%s - %I64u)", 139 memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
138 pszIfName ? pszIfName : (char *) pTable-> 140 memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr,
139 table[dwIfIdx].bDescr, inet_ntop (AF_INET, &dwIP, dst, 141 pTable->table[dwIfIdx].dwPhysAddrLen);
140 INET_ADDRSTRLEN), 142
141 *((unsigned long long *) bPhysAddr)); 143 snprintf (szEntry, 1000, "%s (%s - %I64u)",
142 szEntry[1000] = 0; 144 pszIfName ? pszIfName : (char *)
143 145 pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET,
144 if (pszIfName) 146 &dwIP, dst,
145 free (pszIfName); 147 INET_ADDRSTRLEN),
146 148 *((unsigned long long *) bPhysAddr));
147 sa.sin_family = AF_INET; 149 szEntry[1000] = 0;
150
151 if (pszIfName)
152 free (pszIfName);
153
154 sa.sin_family = AF_INET;
148#if HAVE_SOCKADDR_IN_SIN_LEN 155#if HAVE_SOCKADDR_IN_SIN_LEN
149 sa.sin_len = (u_char) sizeof (struct sockaddr_in); 156 sa.sin_len = (u_char) sizeof (struct sockaddr_in);
150#endif 157#endif
151 sa.sin_addr.S_un.S_addr = dwIP; 158 sa.sin_addr.S_un.S_addr = dwIP;
152 159
153 if (GNUNET_OK != 160 if (GNUNET_OK !=
154 proc (proc_cls, szEntry, 161 proc (proc_cls, szEntry,
155 pTable->table[dwIfIdx].dwIndex == dwExternalNIC, 162 pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
156 (const struct sockaddr *) &sa, sizeof (sa))) 163 (const struct sockaddr *) &sa, sizeof (sa)))
157 break; 164 break;
158 } 165 }
166 }
167 GlobalFree (pAddrTable);
168 GlobalFree (pTable);
159 } 169 }
160 GlobalFree (pAddrTable);
161 GlobalFree (pTable);
162 }
163 170
164 return; 171 return;
165 172
@@ -170,28 +177,29 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
170 socklen_t alen; 177 socklen_t alen;
171 178
172 if (getifaddrs (&ifa_first) == 0) 179 if (getifaddrs (&ifa_first) == 0)
173 {
174 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
175 { 180 {
176 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL && 181 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
177 (ifa_ptr->ifa_flags & IFF_UP) != 0) 182 {
178 { 183 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
179 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) && 184 (ifa_ptr->ifa_flags & IFF_UP) != 0)
180 (ifa_ptr->ifa_addr->sa_family != AF_INET6)) 185 {
181 continue; 186 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
182 if (ifa_ptr->ifa_addr->sa_family == AF_INET) 187 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
183 alen = sizeof (struct sockaddr_in); 188 continue;
184 else 189 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
185 alen = sizeof (struct sockaddr_in6); 190 alen = sizeof (struct sockaddr_in);
186 if (GNUNET_OK != 191 else
187 proc (proc_cls, ifa_ptr->ifa_name, 192 alen = sizeof (struct sockaddr_in6);
188 0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE), 193 if (GNUNET_OK !=
189 ifa_ptr->ifa_addr, alen)) 194 proc (proc_cls, ifa_ptr->ifa_name,
190 break; 195 0 == strcmp (ifa_ptr->ifa_name,
191 } 196 GNUNET_DEFAULT_INTERFACE),
197 ifa_ptr->ifa_addr, alen))
198 break;
199 }
200 }
201 freeifaddrs (ifa_first);
192 } 202 }
193 freeifaddrs (ifa_first);
194 }
195#else 203#else
196 char line[1024]; 204 char line[1024];
197 const char *start; 205 const char *start;
@@ -212,73 +220,76 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
212 else 220 else
213 f = popen ("ifconfig -a 2> /dev/null", "r"); 221 f = popen ("ifconfig -a 2> /dev/null", "r");
214 if (!f) 222 if (!f)
215 { 223 {
216 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | 224 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING |
217 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig"); 225 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig");
218 return; 226 return;
219 } 227 }
220 228
221 have_ifc = GNUNET_NO; 229 have_ifc = GNUNET_NO;
222 ifc[11] = '\0'; 230 ifc[11] = '\0';
223 while (NULL != fgets (line, sizeof (line), f)) 231 while (NULL != fgets (line, sizeof (line), f))
224 {
225 if (strlen (line) == 0)
226 {
227 have_ifc = GNUNET_NO;
228 continue;
229 }
230 if (!isspace (line[0]))
231 {
232 have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
233 /* would end with ':' on OSX, fix it! */
234 if (ifc[strlen (ifc) - 1] == ':')
235 ifc[strlen (ifc) - 1] = '\0';
236 continue;
237 }
238 if (!have_ifc)
239 continue; /* strange input, hope for the best */
240 start = line;
241 while (('\0' != *start) && (isspace (*start)))
242 start++;
243 if ( /* Linux */
244 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
245 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
246 /* Solaris, OS X */
247 (1 == SSCANF (start, "inet %127s", addrstr)) ||
248 (1 == SSCANF (start, "inet6 %127s", addrstr)))
249 { 232 {
250 /* IPv4 */ 233 if (strlen (line) == 0)
251 if (1 == inet_pton (AF_INET, addrstr, &v4)) 234 {
252 { 235 have_ifc = GNUNET_NO;
253 memset (&a4, 0, sizeof (a4)); 236 continue;
254 a4.sin_family = AF_INET; 237 }
238 if (!isspace (line[0]))
239 {
240 have_ifc =
241 (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
242 /* would end with ':' on OSX, fix it! */
243 if (ifc[strlen (ifc) - 1] == ':')
244 ifc[strlen (ifc) - 1] = '\0';
245 continue;
246 }
247 if (!have_ifc)
248 continue; /* strange input, hope for the best */
249 start = line;
250 while (('\0' != *start) && (isspace (*start)))
251 start++;
252 if ( /* Linux */
253 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
254 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
255 /* Solaris, OS X */
256 (1 == SSCANF (start, "inet %127s", addrstr)) ||
257 (1 == SSCANF (start, "inet6 %127s", addrstr)))
258 {
259 /* IPv4 */
260 if (1 == inet_pton (AF_INET, addrstr, &v4))
261 {
262 memset (&a4, 0, sizeof (a4));
263 a4.sin_family = AF_INET;
255#if HAVE_SOCKADDR_IN_SIN_LEN 264#if HAVE_SOCKADDR_IN_SIN_LEN
256 a4.sin_len = (u_char) sizeof (struct sockaddr_in); 265 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
257#endif 266#endif
258 a4.sin_addr = v4; 267 a4.sin_addr = v4;
259 if (GNUNET_OK != 268 if (GNUNET_OK !=
260 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), 269 proc (proc_cls, ifc,
261 (const struct sockaddr *) &a4, sizeof (a4))) 270 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
262 break; 271 (const struct sockaddr *) &a4, sizeof (a4)))
263 continue; 272 break;
264 } 273 continue;
265 /* IPv6 */ 274 }
266 if (1 == inet_pton (AF_INET6, addrstr, &v6)) 275 /* IPv6 */
267 { 276 if (1 == inet_pton (AF_INET6, addrstr, &v6))
268 memset (&a6, 0, sizeof (a6)); 277 {
269 a6.sin6_family = AF_INET6; 278 memset (&a6, 0, sizeof (a6));
279 a6.sin6_family = AF_INET6;
270#if HAVE_SOCKADDR_IN_SIN_LEN 280#if HAVE_SOCKADDR_IN_SIN_LEN
271 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); 281 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
272#endif 282#endif
273 a6.sin6_addr = v6; 283 a6.sin6_addr = v6;
274 if (GNUNET_OK != 284 if (GNUNET_OK !=
275 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), 285 proc (proc_cls, ifc,
276 (const struct sockaddr *) &a6, sizeof (a6))) 286 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
277 break; 287 (const struct sockaddr *) &a6, sizeof (a6)))
278 continue; 288 break;
279 } 289 continue;
290 }
291 }
280 } 292 }
281 }
282 pclose (f); 293 pclose (f);
283#endif 294#endif
284} 295}
diff --git a/src/util/os_priority.c b/src/util/os_priority.c
index 55a728ce6..4f21e7edb 100644
--- a/src/util/os_priority.c
+++ b/src/util/os_priority.c
@@ -30,6 +30,12 @@
30#include "gnunet_scheduler_lib.h" 30#include "gnunet_scheduler_lib.h"
31#include "disk.h" 31#include "disk.h"
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
35#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
36
37#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
38
33#define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE" 39#define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
34 40
35struct GNUNET_OS_Process 41struct GNUNET_OS_Process
@@ -53,42 +59,43 @@ static struct GNUNET_OS_Process current_process;
53 */ 59 */
54static void 60static void
55parent_control_handler (void *cls, 61parent_control_handler (void *cls,
56 const struct GNUNET_SCHEDULER_TaskContext *tc) 62 const struct GNUNET_SCHEDULER_TaskContext *tc)
57{ 63{
58 struct GNUNET_DISK_FileHandle *control_pipe = 64 struct GNUNET_DISK_FileHandle *control_pipe =
59 (struct GNUNET_DISK_FileHandle *) cls; 65 (struct GNUNET_DISK_FileHandle *) cls;
60 int sig; 66 int sig;
61 67
62#if DEBUG_OS 68#if DEBUG_OS
63 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", 69 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n",
64 __FUNCTION__, tc->reason); 70 __FUNCTION__, tc->reason);
65#endif 71#endif
66 if (tc->reason & 72 if (tc->reason &
67 (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT | 73 (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT |
68 GNUNET_SCHEDULER_REASON_PREREQ_DONE)) 74 GNUNET_SCHEDULER_REASON_PREREQ_DONE))
69 {
70 GNUNET_DISK_npipe_close (control_pipe);
71 }
72 else
73 {
74 if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) !=
75 sizeof (sig))
76 { 75 {
77 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
78 GNUNET_DISK_npipe_close (control_pipe); 76 GNUNET_DISK_npipe_close (control_pipe);
79 } 77 }
80 else 78 else
81 { 79 {
80 if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) !=
81 sizeof (sig))
82 {
83 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
84 GNUNET_DISK_npipe_close (control_pipe);
85 }
86 else
87 {
82#if DEBUG_OS 88#if DEBUG_OS
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got control code %d from parent\n", 89 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got control code %d from parent\n",
84 sig); 90 sig);
85#endif 91#endif
86 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 92 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
87 control_pipe, &parent_control_handler, 93 control_pipe,
88 control_pipe); 94 &parent_control_handler,
89 raise (sig); 95 control_pipe);
96 raise (sig);
97 }
90 } 98 }
91 }
92} 99}
93 100
94 101
@@ -97,36 +104,35 @@ parent_control_handler (void *cls,
97 */ 104 */
98void 105void
99GNUNET_OS_install_parent_control_handler (void *cls, 106GNUNET_OS_install_parent_control_handler (void *cls,
100 const struct 107 const struct
101 GNUNET_SCHEDULER_TaskContext *tc) 108 GNUNET_SCHEDULER_TaskContext *tc)
102{ 109{
103 const char *env_buf; 110 const char *env_buf;
104 struct GNUNET_DISK_FileHandle *control_pipe; 111 struct GNUNET_DISK_FileHandle *control_pipe;
105 112
106 env_buf = getenv (GNUNET_OS_CONTROL_PIPE); 113 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
107 if ((env_buf == NULL) || (strlen (env_buf) <= 0)) 114 if ((env_buf == NULL) || (strlen (env_buf) <= 0))
108 { 115 {
109 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 116 LOG (GNUNET_ERROR_TYPE_INFO,
110 _("Not installing a handler because $%s=%s\n"), 117 _("Not installing a handler because $%s=%s\n"),
111 GNUNET_OS_CONTROL_PIPE, env_buf); 118 GNUNET_OS_CONTROL_PIPE, env_buf);
112 return; 119 return;
113 } 120 }
114 control_pipe = 121 control_pipe =
115 GNUNET_DISK_npipe_open (env_buf, GNUNET_DISK_OPEN_READ, 122 GNUNET_DISK_npipe_open (env_buf, GNUNET_DISK_OPEN_READ,
116 GNUNET_DISK_PERM_USER_READ | 123 GNUNET_DISK_PERM_USER_READ |
117 GNUNET_DISK_PERM_USER_WRITE); 124 GNUNET_DISK_PERM_USER_WRITE);
118 if (control_pipe == NULL) 125 if (control_pipe == NULL)
119 { 126 {
120 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", env_buf); 127 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
121 return; 128 return;
122 } 129 }
123#if DEBUG_OS 130#if DEBUG_OS
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 131 LOG (GNUNET_ERROR_TYPE_DEBUG,
125 "Adding parent control handler pipe `%s' to the scheduler\n", 132 "Adding parent control handler pipe `%s' to the scheduler\n", env_buf);
126 env_buf);
127#endif 133#endif
128 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe, 134 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe,
129 &parent_control_handler, control_pipe); 135 &parent_control_handler, control_pipe);
130} 136}
131 137
132 138
@@ -160,90 +166,90 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
160 166
161 ret = GNUNET_DISK_file_write (proc->control_pipe, &sig, sizeof (sig)); 167 ret = GNUNET_DISK_file_write (proc->control_pipe, &sig, sizeof (sig));
162 if (ret != sizeof (sig)) 168 if (ret != sizeof (sig))
163 {
164 if (errno == ECOMM)
165 { 169 {
166 /* Child process is not controllable via pipe */ 170 if (errno == ECOMM)
171 {
172 /* Child process is not controllable via pipe */
167#if DEBUG_OS 173#if DEBUG_OS
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 174 LOG (GNUNET_ERROR_TYPE_DEBUG,
169 "Child process is not controllable, will kill it directly\n"); 175 "Child process is not controllable, will kill it directly\n");
170#endif 176#endif
171 } 177 }
172 else if (errno == EPIPE) 178 else if (errno == EPIPE)
173 { 179 {
174#if DEBUG_OS 180#if DEBUG_OS
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 181 LOG (GNUNET_ERROR_TYPE_DEBUG,
176 "Failed to write into control pipe, because pipe is invalid (the child is most likely dead)\n"); 182 "Failed to write into control pipe, because pipe is invalid (the child is most likely dead)\n");
177#endif 183#endif
178 } 184 }
179 else 185 else
180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 186 LOG (GNUNET_ERROR_TYPE_WARNING,
181 "Failed to write into control pipe , errno is %d\n", errno); 187 "Failed to write into control pipe , errno is %d\n", errno);
182#if WINDOWS && !defined(__CYGWIN__) 188#if WINDOWS && !defined(__CYGWIN__)
183 TerminateProcess (proc->handle, 0); 189 TerminateProcess (proc->handle, 0);
184#else 190#else
185 PLIBC_KILL (proc->pid, sig); 191 PLIBC_KILL (proc->pid, sig);
186#endif 192#endif
187 } 193 }
188 else 194 else
189 { 195 {
190#if DEBUG_OS 196#if DEBUG_OS
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 197 LOG (GNUNET_ERROR_TYPE_DEBUG,
192 "Wrote control code into control pipe, now waiting\n"); 198 "Wrote control code into control pipe, now waiting\n");
193#endif 199#endif
194 200
195#if WINDOWS 201#if WINDOWS
196 /* Give it 3 seconds to die, then kill it in a nice Windows-specific way */ 202 /* Give it 3 seconds to die, then kill it in a nice Windows-specific way */
197 if (WaitForSingleObject (proc->handle, 3000) != WAIT_OBJECT_0) 203 if (WaitForSingleObject (proc->handle, 3000) != WAIT_OBJECT_0)
198 TerminateProcess (proc->handle, 0); 204 TerminateProcess (proc->handle, 0);
199 res = 0; 205 res = 0;
200#else 206#else
201 struct GNUNET_NETWORK_FDSet *rfds; 207 struct GNUNET_NETWORK_FDSet *rfds;
202 struct GNUNET_NETWORK_FDSet *efds; 208 struct GNUNET_NETWORK_FDSet *efds;
203 209
204 rfds = GNUNET_NETWORK_fdset_create (); 210 rfds = GNUNET_NETWORK_fdset_create ();
205 efds = GNUNET_NETWORK_fdset_create (); 211 efds = GNUNET_NETWORK_fdset_create ();
206 212
207 GNUNET_NETWORK_fdset_handle_set (rfds, proc->control_pipe); 213 GNUNET_NETWORK_fdset_handle_set (rfds, proc->control_pipe);
208 GNUNET_NETWORK_fdset_handle_set (efds, proc->control_pipe); 214 GNUNET_NETWORK_fdset_handle_set (efds, proc->control_pipe);
209 215
210 /* Ndurner thought this up, and i have no idea what it does. 216 /* Ndurner thought this up, and i have no idea what it does.
211 * There's have never been any code to answer the shutdown call 217 * There's have never been any code to answer the shutdown call
212 * (write a single int into the pipe, so that this function can read it). 218 * (write a single int into the pipe, so that this function can read it).
213 * On *nix select() will probably tell that pipe is ready 219 * On *nix select() will probably tell that pipe is ready
214 * for reading, once the other process shuts down, 220 * for reading, once the other process shuts down,
215 * but the read () call will fail, triggering a kill () 221 * but the read () call will fail, triggering a kill ()
216 * on the pid that is already dead. This will probably result in non-0 222 * on the pid that is already dead. This will probably result in non-0
217 * return from kill(), and therefore from this function. 223 * return from kill(), and therefore from this function.
218 */ 224 */
219 while (1) 225 while (1)
220 { 226 {
221 ret = 227 ret =
222 GNUNET_NETWORK_socket_select (rfds, NULL, efds, 228 GNUNET_NETWORK_socket_select (rfds, NULL, efds,
223 GNUNET_TIME_relative_multiply 229 GNUNET_TIME_relative_multiply
224 (GNUNET_TIME_relative_get_unit (), 230 (GNUNET_TIME_relative_get_unit (),
225 5000)); 231 5000));
226 232
227 if (ret < 1 || 233 if (ret < 1 ||
228 GNUNET_NETWORK_fdset_handle_isset (efds, proc->control_pipe)) 234 GNUNET_NETWORK_fdset_handle_isset (efds, proc->control_pipe))
229 { 235 {
230 /* Just to be sure */ 236 /* Just to be sure */
231 PLIBC_KILL (proc->pid, sig); 237 PLIBC_KILL (proc->pid, sig);
232 res = 0; 238 res = 0;
233 break; 239 break;
234 } 240 }
235 else 241 else
236 { 242 {
237 if (GNUNET_DISK_file_read (proc->control_pipe, &ret, sizeof (ret)) != 243 if (GNUNET_DISK_file_read
238 GNUNET_OK) 244 (proc->control_pipe, &ret, sizeof (ret)) != GNUNET_OK)
239 res = PLIBC_KILL (proc->pid, sig); 245 res = PLIBC_KILL (proc->pid, sig);
240 246
241 /* Child signaled shutdown is in progress */ 247 /* Child signaled shutdown is in progress */
242 continue; 248 continue;
243 } 249 }
244 } 250 }
245#endif 251#endif
246 } 252 }
247 253
248 return res; 254 return res;
249#else 255#else
@@ -318,7 +324,7 @@ ChildWaitThread (void *arg)
318 */ 324 */
319int 325int
320GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc, 326GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
321 enum GNUNET_SCHEDULER_Priority prio) 327 enum GNUNET_SCHEDULER_Priority prio)
322{ 328{
323 int rprio; 329 int rprio;
324 330
@@ -328,51 +334,51 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
328 334
329 /* convert to MINGW/Unix values */ 335 /* convert to MINGW/Unix values */
330 switch (prio) 336 switch (prio)
331 { 337 {
332 case GNUNET_SCHEDULER_PRIORITY_UI: 338 case GNUNET_SCHEDULER_PRIORITY_UI:
333 case GNUNET_SCHEDULER_PRIORITY_URGENT: 339 case GNUNET_SCHEDULER_PRIORITY_URGENT:
334#ifdef MINGW 340#ifdef MINGW
335 rprio = HIGH_PRIORITY_CLASS; 341 rprio = HIGH_PRIORITY_CLASS;
336#else 342#else
337 rprio = 0; 343 rprio = 0;
338#endif 344#endif
339 break; 345 break;
340 346
341 case GNUNET_SCHEDULER_PRIORITY_HIGH: 347 case GNUNET_SCHEDULER_PRIORITY_HIGH:
342#ifdef MINGW 348#ifdef MINGW
343 rprio = ABOVE_NORMAL_PRIORITY_CLASS; 349 rprio = ABOVE_NORMAL_PRIORITY_CLASS;
344#else 350#else
345 rprio = 5; 351 rprio = 5;
346#endif 352#endif
347 break; 353 break;
348 354
349 case GNUNET_SCHEDULER_PRIORITY_DEFAULT: 355 case GNUNET_SCHEDULER_PRIORITY_DEFAULT:
350#ifdef MINGW 356#ifdef MINGW
351 rprio = NORMAL_PRIORITY_CLASS; 357 rprio = NORMAL_PRIORITY_CLASS;
352#else 358#else
353 rprio = 7; 359 rprio = 7;
354#endif 360#endif
355 break; 361 break;
356 362
357 case GNUNET_SCHEDULER_PRIORITY_BACKGROUND: 363 case GNUNET_SCHEDULER_PRIORITY_BACKGROUND:
358#ifdef MINGW 364#ifdef MINGW
359 rprio = BELOW_NORMAL_PRIORITY_CLASS; 365 rprio = BELOW_NORMAL_PRIORITY_CLASS;
360#else 366#else
361 rprio = 10; 367 rprio = 10;
362#endif 368#endif
363 break; 369 break;
364 370
365 case GNUNET_SCHEDULER_PRIORITY_IDLE: 371 case GNUNET_SCHEDULER_PRIORITY_IDLE:
366#ifdef MINGW 372#ifdef MINGW
367 rprio = IDLE_PRIORITY_CLASS; 373 rprio = IDLE_PRIORITY_CLASS;
368#else 374#else
369 rprio = 19; 375 rprio = 19;
370#endif 376#endif
371 break; 377 break;
372 default: 378 default:
373 GNUNET_assert (0); 379 GNUNET_assert (0);
374 return GNUNET_SYSERR; 380 return GNUNET_SYSERR;
375 } 381 }
376 382
377 /* Set process priority */ 383 /* Set process priority */
378#ifdef MINGW 384#ifdef MINGW
@@ -387,31 +393,31 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
387 393
388 pid = proc->pid; 394 pid = proc->pid;
389 if ((0 == pid) || (pid == getpid ())) 395 if ((0 == pid) || (pid == getpid ()))
390 {
391 int have = nice (0);
392 int delta = rprio - have;
393
394 errno = 0;
395 if ((delta != 0) && (rprio == nice (delta)) && (errno != 0))
396 { 396 {
397 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 397 int have = nice (0);
398 "nice"); 398 int delta = rprio - have;
399 return GNUNET_SYSERR; 399
400 errno = 0;
401 if ((delta != 0) && (rprio == nice (delta)) && (errno != 0))
402 {
403 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
404 "nice");
405 return GNUNET_SYSERR;
406 }
400 } 407 }
401 }
402 else 408 else
403 {
404 if (0 != setpriority (PRIO_PROCESS, pid, rprio))
405 { 409 {
406 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 410 if (0 != setpriority (PRIO_PROCESS, pid, rprio))
407 "setpriority"); 411 {
408 return GNUNET_SYSERR; 412 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
413 "setpriority");
414 return GNUNET_SYSERR;
415 }
409 } 416 }
410 }
411#else 417#else
412#if DEBUG_OS 418#if DEBUG_OS
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 419 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
414 "Priority management not availabe for this platform\n"); 420 "Priority management not availabe for this platform\n");
415#endif 421#endif
416#endif 422#endif
417 return GNUNET_OK; 423 return GNUNET_OK;
@@ -434,84 +440,84 @@ CreateCustomEnvTable (char **vars)
434 win32_env_table = GetEnvironmentStringsA (); 440 win32_env_table = GetEnvironmentStringsA ();
435 if (win32_env_table == NULL) 441 if (win32_env_table == NULL)
436 return NULL; 442 return NULL;
437 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ; 443 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++);
438 n_var = c; 444 n_var = c;
439 index = GNUNET_malloc (sizeof (char *) * n_var); 445 index = GNUNET_malloc (sizeof (char *) * n_var);
440 for (c = 0; c < n_var; c++) 446 for (c = 0; c < n_var; c++)
441 index[c] = 0; 447 index[c] = 0;
442 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++) 448 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
443 {
444 size_t len = strlen (ptr);
445 int found = 0;
446
447 for (var_ptr = vars; *var_ptr; var_ptr++)
448 { 449 {
449 var = *var_ptr++; 450 size_t len = strlen (ptr);
450 val = *var_ptr; 451 int found = 0;
451 var_len = strlen (var); 452
452 if (strncmp (var, ptr, var_len) == 0) 453 for (var_ptr = vars; *var_ptr; var_ptr++)
453 { 454 {
454 found = 1; 455 var = *var_ptr++;
455 index[c] = 1; 456 val = *var_ptr;
456 tablesize += var_len + strlen (val) + 1; 457 var_len = strlen (var);
457 break; 458 if (strncmp (var, ptr, var_len) == 0)
458 } 459 {
460 found = 1;
461 index[c] = 1;
462 tablesize += var_len + strlen (val) + 1;
463 break;
464 }
465 }
466 if (!found)
467 tablesize += len + 1;
468 ptr += len + 1;
459 } 469 }
460 if (!found)
461 tablesize += len + 1;
462 ptr += len + 1;
463 }
464 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) 470 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
465 {
466 var = *var_ptr++;
467 val = *var_ptr;
468 if (index[c] != 1)
469 n_found += strlen (var) + strlen (val) + 1;
470 }
471 result = GNUNET_malloc (tablesize + n_found + 1);
472 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
473 {
474 size_t len = strlen (ptr);
475 int found = 0;
476
477 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
478 { 471 {
479 var = *var_ptr++; 472 var = *var_ptr++;
480 val = *var_ptr; 473 val = *var_ptr;
481 var_len = strlen (var); 474 if (index[c] != 1)
482 if (strncmp (var, ptr, var_len) == 0) 475 n_found += strlen (var) + strlen (val) + 1;
483 {
484 found = 1;
485 break;
486 }
487 }
488 if (!found)
489 {
490 strcpy (result_ptr, ptr);
491 result_ptr += len + 1;
492 } 476 }
493 else 477 result = GNUNET_malloc (tablesize + n_found + 1);
478 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
494 { 479 {
495 strcpy (result_ptr, var); 480 size_t len = strlen (ptr);
496 result_ptr += var_len; 481 int found = 0;
497 strcpy (result_ptr, val); 482
498 result_ptr += strlen (val) + 1; 483 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
484 {
485 var = *var_ptr++;
486 val = *var_ptr;
487 var_len = strlen (var);
488 if (strncmp (var, ptr, var_len) == 0)
489 {
490 found = 1;
491 break;
492 }
493 }
494 if (!found)
495 {
496 strcpy (result_ptr, ptr);
497 result_ptr += len + 1;
498 }
499 else
500 {
501 strcpy (result_ptr, var);
502 result_ptr += var_len;
503 strcpy (result_ptr, val);
504 result_ptr += strlen (val) + 1;
505 }
506 ptr += len + 1;
499 } 507 }
500 ptr += len + 1;
501 }
502 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) 508 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
503 {
504 var = *var_ptr++;
505 val = *var_ptr;
506 var_len = strlen (var);
507 if (index[c] != 1)
508 { 509 {
509 strcpy (result_ptr, var); 510 var = *var_ptr++;
510 result_ptr += var_len; 511 val = *var_ptr;
511 strcpy (result_ptr, val); 512 var_len = strlen (var);
512 result_ptr += strlen (val) + 1; 513 if (index[c] != 1)
514 {
515 strcpy (result_ptr, var);
516 result_ptr += var_len;
517 strcpy (result_ptr, val);
518 result_ptr += strlen (val) + 1;
519 }
513 } 520 }
514 }
515 FreeEnvironmentStrings (win32_env_table); 521 FreeEnvironmentStrings (win32_env_table);
516 GNUNET_free (index); 522 GNUNET_free (index);
517 *result_ptr = 0; 523 *result_ptr = 0;
@@ -531,8 +537,8 @@ CreateCustomEnvTable (char **vars)
531 */ 537 */
532struct GNUNET_OS_Process * 538struct GNUNET_OS_Process *
533GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, 539GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
534 struct GNUNET_DISK_PipeHandle *pipe_stdout, 540 struct GNUNET_DISK_PipeHandle *pipe_stdout,
535 const char *filename, va_list va) 541 const char *filename, va_list va)
536{ 542{
537 va_list ap; 543 va_list ap;
538 544
@@ -553,9 +559,9 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
553 559
554#if ENABLE_WINDOWS_WORKAROUNDS 560#if ENABLE_WINDOWS_WORKAROUNDS
555 control_pipe = 561 control_pipe =
556 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 562 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
557 GNUNET_DISK_PERM_USER_READ | 563 GNUNET_DISK_PERM_USER_READ |
558 GNUNET_DISK_PERM_USER_WRITE); 564 GNUNET_DISK_PERM_USER_WRITE);
559 if (control_pipe == NULL) 565 if (control_pipe == NULL)
560 return NULL; 566 return NULL;
561#endif 567#endif
@@ -563,35 +569,38 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
563 argc = 0; 569 argc = 0;
564 va_copy (ap, va); 570 va_copy (ap, va);
565 while (NULL != va_arg (ap, char *)) 571 while (NULL != va_arg (ap, char *))
566 argc++; 572 argc++;
567 573
568 va_end (ap); 574 va_end (ap);
569 argv = GNUNET_malloc (sizeof (char *) * (argc + 1)); 575 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
570 argc = 0; 576 argc = 0;
571 va_copy (ap, va); 577 va_copy (ap, va);
572 while (NULL != (argv[argc] = va_arg (ap, char *))) 578 while (NULL != (argv[argc] = va_arg (ap, char *)))
573 argc++; 579 argc++;
574 580
575 va_end (ap); 581 va_end (ap);
576 if (pipe_stdout != NULL) 582 if (pipe_stdout != NULL)
577 { 583 {
578 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 584 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
579 (pipe_stdout, 585 (pipe_stdout,
580 GNUNET_DISK_PIPE_END_WRITE), 586 GNUNET_DISK_PIPE_END_WRITE),
581 &fd_stdout_write, sizeof (int)); 587 &fd_stdout_write, sizeof (int));
582 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 588 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
583 (pipe_stdout, GNUNET_DISK_PIPE_END_READ), 589 (pipe_stdout,
584 &fd_stdout_read, sizeof (int)); 590 GNUNET_DISK_PIPE_END_READ),
585 } 591 &fd_stdout_read, sizeof (int));
592 }
586 if (pipe_stdin != NULL) 593 if (pipe_stdin != NULL)
587 { 594 {
588 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 595 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
589 (pipe_stdin, GNUNET_DISK_PIPE_END_READ), 596 (pipe_stdin,
590 &fd_stdin_read, sizeof (int)); 597 GNUNET_DISK_PIPE_END_READ),
591 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 598 &fd_stdin_read, sizeof (int));
592 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), 599 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
593 &fd_stdin_write, sizeof (int)); 600 (pipe_stdin,
594 } 601 GNUNET_DISK_PIPE_END_WRITE),
602 &fd_stdin_write, sizeof (int));
603 }
595 604
596#if HAVE_WORKING_VFORK 605#if HAVE_WORKING_VFORK
597 ret = vfork (); 606 ret = vfork ();
@@ -599,44 +608,46 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
599 ret = fork (); 608 ret = fork ();
600#endif 609#endif
601 if (ret != 0) 610 if (ret != 0)
602 {
603 if (ret == -1)
604 { 611 {
605 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); 612 if (ret == -1)
613 {
614 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
606#if ENABLE_WINDOWS_WORKAROUNDS 615#if ENABLE_WINDOWS_WORKAROUNDS
607 GNUNET_DISK_npipe_close (control_pipe); 616 GNUNET_DISK_npipe_close (control_pipe);
608#endif 617#endif
609 } 618 }
610 else 619 else
611 { 620 {
612 621
613#if HAVE_WORKING_VFORK 622#if HAVE_WORKING_VFORK
614 /* let's hope vfork actually works; for some extreme cases (including 623 /* let's hope vfork actually works; for some extreme cases (including
615 * a testcase) we need 'execvp' to have run before we return, since 624 * a testcase) we need 'execvp' to have run before we return, since
616 * we may send a signal to the process next and we don't want it 625 * we may send a signal to the process next and we don't want it
617 * to be caught by OUR signal handler (but either by the default 626 * to be caught by OUR signal handler (but either by the default
618 * handler or the actual handler as installed by the process itself). */ 627 * handler or the actual handler as installed by the process itself). */
619#else 628#else
620 /* let's give the child process a chance to run execvp, 1s should 629 /* let's give the child process a chance to run execvp, 1s should
621 * be plenty in practice */ 630 * be plenty in practice */
622 if (pipe_stdout != NULL) 631 if (pipe_stdout != NULL)
623 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); 632 GNUNET_DISK_pipe_close_end (pipe_stdout,
624 if (pipe_stdin != NULL) 633 GNUNET_DISK_PIPE_END_WRITE);
625 GNUNET_DISK_pipe_close_end (pipe_stdin, GNUNET_DISK_PIPE_END_READ); 634 if (pipe_stdin != NULL)
626 sleep (1); 635 GNUNET_DISK_pipe_close_end (pipe_stdin,
636 GNUNET_DISK_PIPE_END_READ);
637 sleep (1);
627#endif 638#endif
628 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); 639 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
629 gnunet_proc->pid = ret; 640 gnunet_proc->pid = ret;
630#if ENABLE_WINDOWS_WORKAROUNDS 641#if ENABLE_WINDOWS_WORKAROUNDS
631 gnunet_proc->control_pipe = control_pipe; 642 gnunet_proc->control_pipe = control_pipe;
632#endif 643#endif
633 } 644 }
634 GNUNET_free (argv); 645 GNUNET_free (argv);
635#if ENABLE_WINDOWS_WORKAROUNDS 646#if ENABLE_WINDOWS_WORKAROUNDS
636 GNUNET_free (childpipename); 647 GNUNET_free (childpipename);
637#endif 648#endif
638 return gnunet_proc; 649 return gnunet_proc;
639 } 650 }
640 651
641#if ENABLE_WINDOWS_WORKAROUNDS 652#if ENABLE_WINDOWS_WORKAROUNDS
642 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1); 653 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1);
@@ -644,23 +655,23 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
644#endif 655#endif
645 656
646 if (pipe_stdout != NULL) 657 if (pipe_stdout != NULL)
647 { 658 {
648 GNUNET_break (0 == close (fd_stdout_read)); 659 GNUNET_break (0 == close (fd_stdout_read));
649 if (-1 == dup2 (fd_stdout_write, 1)) 660 if (-1 == dup2 (fd_stdout_write, 1))
650 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2"); 661 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
651 GNUNET_break (0 == close (fd_stdout_write)); 662 GNUNET_break (0 == close (fd_stdout_write));
652 } 663 }
653 664
654 if (pipe_stdin != NULL) 665 if (pipe_stdin != NULL)
655 { 666 {
656 667
657 GNUNET_break (0 == close (fd_stdin_write)); 668 GNUNET_break (0 == close (fd_stdin_write));
658 if (-1 == dup2 (fd_stdin_read, 0)) 669 if (-1 == dup2 (fd_stdin_read, 0))
659 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2"); 670 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
660 GNUNET_break (0 == close (fd_stdin_read)); 671 GNUNET_break (0 == close (fd_stdin_read));
661 } 672 }
662 execvp (filename, argv); 673 execvp (filename, argv);
663 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); 674 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
664 _exit (1); 675 _exit (1);
665#else 676#else
666 char *arg; 677 char *arg;
@@ -694,8 +705,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
694 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0); 705 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
695 706
696 alloc_len = 707 alloc_len =
697 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + 708 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
698 strlen (libdir); 709 strlen (libdir);
699 710
700 pathbuf = GNUNET_malloc (alloc_len * sizeof (char)); 711 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
701 712
@@ -718,39 +729,39 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
718 if (non_const_filename[1] == ':') 729 if (non_const_filename[1] == ':')
719 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename); 730 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
720 else if (!SearchPathA 731 else if (!SearchPathA
721 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), 732 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
722 path, NULL)) 733 path, NULL))
723 { 734 {
724 SetErrnoFromWinError (GetLastError ()); 735 SetErrnoFromWinError (GetLastError ());
725 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "SearchPath", 736 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
726 non_const_filename); 737 non_const_filename);
727 GNUNET_free (non_const_filename); 738 GNUNET_free (non_const_filename);
728 GNUNET_free (pathbuf); 739 GNUNET_free (pathbuf);
729 return NULL; 740 return NULL;
730 } 741 }
731 GNUNET_free (pathbuf); 742 GNUNET_free (pathbuf);
732 GNUNET_free (non_const_filename); 743 GNUNET_free (non_const_filename);
733 744
734 cmdlen = 0; 745 cmdlen = 0;
735 va_copy (ap, va); 746 va_copy (ap, va);
736 while (NULL != (arg = va_arg (ap, char *))) 747 while (NULL != (arg = va_arg (ap, char *)))
737 { 748 {
738 if (cmdlen == 0) 749 if (cmdlen == 0)
739 cmdlen = cmdlen + strlen (path) + 3; 750 cmdlen = cmdlen + strlen (path) + 3;
740 else 751 else
741 cmdlen = cmdlen + strlen (arg) + 3; 752 cmdlen = cmdlen + strlen (arg) + 3;
742 } 753 }
743 va_end (ap); 754 va_end (ap);
744 755
745 cmd = idx = GNUNET_malloc (sizeof (char) * (cmdlen + 1)); 756 cmd = idx = GNUNET_malloc (sizeof (char) * (cmdlen + 1));
746 va_copy (ap, va); 757 va_copy (ap, va);
747 while (NULL != (arg = va_arg (ap, char *))) 758 while (NULL != (arg = va_arg (ap, char *)))
748 { 759 {
749 if (idx == cmd) 760 if (idx == cmd)
750 idx += sprintf (idx, "\"%s\" ", path); 761 idx += sprintf (idx, "\"%s\" ", path);
751 else 762 else
752 idx += sprintf (idx, "\"%s\" ", arg); 763 idx += sprintf (idx, "\"%s\" ", arg);
753 } 764 }
754 va_end (ap); 765 va_end (ap);
755 766
756 memset (&start, 0, sizeof (start)); 767 memset (&start, 0, sizeof (start));
@@ -760,36 +771,37 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
760 start.dwFlags |= STARTF_USESTDHANDLES; 771 start.dwFlags |= STARTF_USESTDHANDLES;
761 772
762 if (pipe_stdin != NULL) 773 if (pipe_stdin != NULL)
763 { 774 {
764 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 775 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
765 (pipe_stdin, GNUNET_DISK_PIPE_END_READ), 776 (pipe_stdin,
766 &stdin_handle, sizeof (HANDLE)); 777 GNUNET_DISK_PIPE_END_READ),
767 start.hStdInput = stdin_handle; 778 &stdin_handle, sizeof (HANDLE));
768 } 779 start.hStdInput = stdin_handle;
780 }
769 781
770 if (pipe_stdout != NULL) 782 if (pipe_stdout != NULL)
771 { 783 {
772 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 784 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
773 (pipe_stdout, 785 (pipe_stdout,
774 GNUNET_DISK_PIPE_END_WRITE), 786 GNUNET_DISK_PIPE_END_WRITE),
775 &stdout_handle, sizeof (HANDLE)); 787 &stdout_handle, sizeof (HANDLE));
776 start.hStdOutput = stdout_handle; 788 start.hStdOutput = stdout_handle;
777 } 789 }
778 790
779 control_pipe = 791 control_pipe =
780 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 792 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
781 GNUNET_DISK_PERM_USER_READ | 793 GNUNET_DISK_PERM_USER_READ |
782 GNUNET_DISK_PERM_USER_WRITE); 794 GNUNET_DISK_PERM_USER_WRITE);
783 if (control_pipe == NULL) 795 if (control_pipe == NULL)
784 { 796 {
785 GNUNET_free (cmd); 797 GNUNET_free (cmd);
786 GNUNET_free (path); 798 GNUNET_free (path);
787 return NULL; 799 return NULL;
788 } 800 }
789 801
790#if DEBUG_OS 802#if DEBUG_OS
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 803 LOG (GNUNET_ERROR_TYPE_DEBUG,
792 "Opened the parent end of the pipe `%s'\n", childpipename); 804 "Opened the parent end of the pipe `%s'\n", childpipename);
793#endif 805#endif
794 806
795 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE); 807 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE);
@@ -802,13 +814,13 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
802 if (!CreateProcessA 814 if (!CreateProcessA
803 (path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | CREATE_SUSPENDED, 815 (path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | CREATE_SUSPENDED,
804 env_block, NULL, &start, &proc)) 816 env_block, NULL, &start, &proc))
805 { 817 {
806 SetErrnoFromWinError (GetLastError ()); 818 SetErrnoFromWinError (GetLastError ());
807 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path); 819 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path);
808 GNUNET_free (env_block); 820 GNUNET_free (env_block);
809 GNUNET_free (cmd); 821 GNUNET_free (cmd);
810 return NULL; 822 return NULL;
811 } 823 }
812 824
813 GNUNET_free (env_block); 825 GNUNET_free (env_block);
814 826
@@ -842,8 +854,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
842 */ 854 */
843struct GNUNET_OS_Process * 855struct GNUNET_OS_Process *
844GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, 856GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
845 struct GNUNET_DISK_PipeHandle *pipe_stdout, 857 struct GNUNET_DISK_PipeHandle *pipe_stdout,
846 const char *filename, ...) 858 const char *filename, ...)
847{ 859{
848 struct GNUNET_OS_Process *ret; 860 struct GNUNET_OS_Process *ret;
849 va_list ap; 861 va_list ap;
@@ -866,7 +878,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
866 */ 878 */
867struct GNUNET_OS_Process * 879struct GNUNET_OS_Process *
868GNUNET_OS_start_process_v (const int *lsocks, const char *filename, 880GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
869 char *const argv[]) 881 char *const argv[])
870{ 882{
871#if ENABLE_WINDOWS_WORKAROUNDS 883#if ENABLE_WINDOWS_WORKAROUNDS
872 struct GNUNET_DISK_FileHandle *control_pipe = NULL; 884 struct GNUNET_DISK_FileHandle *control_pipe = NULL;
@@ -888,9 +900,9 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
888 900
889#if ENABLE_WINDOWS_WORKAROUNDS 901#if ENABLE_WINDOWS_WORKAROUNDS
890 control_pipe = 902 control_pipe =
891 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 903 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
892 GNUNET_DISK_PERM_USER_READ | 904 GNUNET_DISK_PERM_USER_READ |
893 GNUNET_DISK_PERM_USER_WRITE); 905 GNUNET_DISK_PERM_USER_WRITE);
894 if (control_pipe == NULL) 906 if (control_pipe == NULL)
895 return NULL; 907 return NULL;
896#endif 908#endif
@@ -898,52 +910,52 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
898 lscp = NULL; 910 lscp = NULL;
899 ls = 0; 911 ls = 0;
900 if (lsocks != NULL) 912 if (lsocks != NULL)
901 { 913 {
902 i = 0; 914 i = 0;
903 while (-1 != (k = lsocks[i++])) 915 while (-1 != (k = lsocks[i++]))
904 GNUNET_array_append (lscp, ls, k); 916 GNUNET_array_append (lscp, ls, k);
905 GNUNET_array_append (lscp, ls, -1); 917 GNUNET_array_append (lscp, ls, -1);
906 } 918 }
907#if HAVE_WORKING_VFORK 919#if HAVE_WORKING_VFORK
908 ret = vfork (); 920 ret = vfork ();
909#else 921#else
910 ret = fork (); 922 ret = fork ();
911#endif 923#endif
912 if (ret != 0) 924 if (ret != 0)
913 {
914 if (ret == -1)
915 { 925 {
916 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); 926 if (ret == -1)
927 {
928 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
917#if ENABLE_WINDOWS_WORKAROUNDS 929#if ENABLE_WINDOWS_WORKAROUNDS
918 GNUNET_DISK_npipe_close (control_pipe); 930 GNUNET_DISK_npipe_close (control_pipe);
919#endif 931#endif
920 } 932 }
921 else 933 else
922 { 934 {
923#if HAVE_WORKING_VFORK 935#if HAVE_WORKING_VFORK
924 /* let's hope vfork actually works; for some extreme cases (including 936 /* let's hope vfork actually works; for some extreme cases (including
925 * a testcase) we need 'execvp' to have run before we return, since 937 * a testcase) we need 'execvp' to have run before we return, since
926 * we may send a signal to the process next and we don't want it 938 * we may send a signal to the process next and we don't want it
927 * to be caught by OUR signal handler (but either by the default 939 * to be caught by OUR signal handler (but either by the default
928 * handler or the actual handler as installed by the process itself). */ 940 * handler or the actual handler as installed by the process itself). */
929#else 941#else
930 /* let's give the child process a chance to run execvp, 1s should 942 /* let's give the child process a chance to run execvp, 1s should
931 * be plenty in practice */ 943 * be plenty in practice */
932 sleep (1); 944 sleep (1);
933#endif 945#endif
934 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); 946 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
935 gnunet_proc->pid = ret; 947 gnunet_proc->pid = ret;
936#if ENABLE_WINDOWS_WORKAROUNDS 948#if ENABLE_WINDOWS_WORKAROUNDS
937 gnunet_proc->control_pipe = control_pipe; 949 gnunet_proc->control_pipe = control_pipe;
938 950
939#endif 951#endif
940 } 952 }
941 GNUNET_array_grow (lscp, ls, 0); 953 GNUNET_array_grow (lscp, ls, 0);
942#if ENABLE_WINDOWS_WORKAROUNDS 954#if ENABLE_WINDOWS_WORKAROUNDS
943 GNUNET_free (childpipename); 955 GNUNET_free (childpipename);
944#endif 956#endif
945 return gnunet_proc; 957 return gnunet_proc;
946 } 958 }
947 959
948#if ENABLE_WINDOWS_WORKAROUNDS 960#if ENABLE_WINDOWS_WORKAROUNDS
949 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1); 961 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1);
@@ -951,50 +963,50 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
951#endif 963#endif
952 964
953 if (lscp != NULL) 965 if (lscp != NULL)
954 {
955 /* read systemd documentation... */
956 GNUNET_snprintf (lpid, sizeof (lpid), "%u", getpid ());
957 setenv ("LISTEN_PID", lpid, 1);
958 i = 0;
959 tgt = 3;
960 while (-1 != lscp[i])
961 { 966 {
962 j = i + 1; 967 /* read systemd documentation... */
963 while (-1 != lscp[j]) 968 GNUNET_snprintf (lpid, sizeof (lpid), "%u", getpid ());
964 { 969 setenv ("LISTEN_PID", lpid, 1);
965 if (lscp[j] == tgt) 970 i = 0;
966 { 971 tgt = 3;
967 /* dup away */ 972 while (-1 != lscp[i])
968 k = dup (lscp[j]); 973 {
969 GNUNET_assert (-1 != k); 974 j = i + 1;
970 GNUNET_assert (0 == close (lscp[j])); 975 while (-1 != lscp[j])
971 lscp[j] = k; 976 {
972 break; 977 if (lscp[j] == tgt)
973 } 978 {
974 j++; 979 /* dup away */
975 } 980 k = dup (lscp[j]);
976 if (lscp[i] != tgt) 981 GNUNET_assert (-1 != k);
977 { 982 GNUNET_assert (0 == close (lscp[j]));
978 /* Bury any existing FD, no matter what; they should all be closed 983 lscp[j] = k;
979 * on exec anyway and the important onces have been dup'ed away */ 984 break;
980 (void) close (tgt); 985 }
981 GNUNET_assert (-1 != dup2 (lscp[i], tgt)); 986 j++;
982 } 987 }
983 /* unset close-on-exec flag */ 988 if (lscp[i] != tgt)
984 flags = fcntl (tgt, F_GETFD); 989 {
985 GNUNET_assert (flags >= 0); 990 /* Bury any existing FD, no matter what; they should all be closed
986 flags &= ~FD_CLOEXEC; 991 * on exec anyway and the important onces have been dup'ed away */
987 fflush (stderr); 992 (void) close (tgt);
988 (void) fcntl (tgt, F_SETFD, flags); 993 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
989 tgt++; 994 }
990 i++; 995 /* unset close-on-exec flag */
996 flags = fcntl (tgt, F_GETFD);
997 GNUNET_assert (flags >= 0);
998 flags &= ~FD_CLOEXEC;
999 fflush (stderr);
1000 (void) fcntl (tgt, F_SETFD, flags);
1001 tgt++;
1002 i++;
1003 }
1004 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
1005 setenv ("LISTEN_FDS", fds, 1);
991 } 1006 }
992 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
993 setenv ("LISTEN_FDS", fds, 1);
994 }
995 GNUNET_array_grow (lscp, ls, 0); 1007 GNUNET_array_grow (lscp, ls, 0);
996 execvp (filename, argv); 1008 execvp (filename, argv);
997 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); 1009 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
998 _exit (1); 1010 _exit (1);
999#else 1011#else
1000 char **arg, **non_const_argv; 1012 char **arg, **non_const_argv;
@@ -1029,8 +1041,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1029 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0); 1041 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
1030 1042
1031 alloc_len = 1043 alloc_len =
1032 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + 1044 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
1033 strlen (libdir); 1045 strlen (libdir);
1034 1046
1035 pathbuf = GNUNET_malloc (alloc_len * sizeof (char)); 1047 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
1036 1048
@@ -1042,11 +1054,11 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1042 1054
1043 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len); 1055 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
1044 if (alloc_len != pathbuf_len - 1) 1056 if (alloc_len != pathbuf_len - 1)
1045 { 1057 {
1046 GNUNET_free (pathbuf); 1058 GNUNET_free (pathbuf);
1047 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */ 1059 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
1048 return NULL; 1060 return NULL;
1049 } 1061 }
1050 1062
1051 cmdlen = strlen (filename); 1063 cmdlen = strlen (filename);
1052 if (cmdlen < 5 || strcmp (&filename[cmdlen - 4], ".exe") != 0) 1064 if (cmdlen < 5 || strcmp (&filename[cmdlen - 4], ".exe") != 0)
@@ -1058,26 +1070,26 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1058 if (non_const_filename[1] == ':') 1070 if (non_const_filename[1] == ':')
1059 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename); 1071 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
1060 else if (!SearchPathA 1072 else if (!SearchPathA
1061 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), 1073 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
1062 path, NULL)) 1074 path, NULL))
1063 { 1075 {
1064 SetErrnoFromWinError (GetLastError ()); 1076 SetErrnoFromWinError (GetLastError ());
1065 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "SearchPath", 1077 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
1066 non_const_filename); 1078 non_const_filename);
1067 GNUNET_free (non_const_filename); 1079 GNUNET_free (non_const_filename);
1068 GNUNET_free (pathbuf); 1080 GNUNET_free (pathbuf);
1069 return NULL; 1081 return NULL;
1070 } 1082 }
1071 GNUNET_free (pathbuf); 1083 GNUNET_free (pathbuf);
1072 GNUNET_free (non_const_filename); 1084 GNUNET_free (non_const_filename);
1073 1085
1074 /* Count the number of arguments */ 1086 /* Count the number of arguments */
1075 arg = (char **) argv; 1087 arg = (char **) argv;
1076 while (*arg) 1088 while (*arg)
1077 { 1089 {
1078 arg++; 1090 arg++;
1079 argcount++; 1091 argcount++;
1080 } 1092 }
1081 1093
1082 /* Allocate a copy argv */ 1094 /* Allocate a copy argv */
1083 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1)); 1095 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
@@ -1086,33 +1098,33 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1086 argcount = 0; 1098 argcount = 0;
1087 arg = (char **) argv; 1099 arg = (char **) argv;
1088 while (*arg) 1100 while (*arg)
1089 { 1101 {
1090 if (arg == argv) 1102 if (arg == argv)
1091 non_const_argv[argcount] = GNUNET_strdup (path); 1103 non_const_argv[argcount] = GNUNET_strdup (path);
1092 else 1104 else
1093 non_const_argv[argcount] = GNUNET_strdup (*arg); 1105 non_const_argv[argcount] = GNUNET_strdup (*arg);
1094 arg++; 1106 arg++;
1095 argcount++; 1107 argcount++;
1096 } 1108 }
1097 non_const_argv[argcount] = NULL; 1109 non_const_argv[argcount] = NULL;
1098 1110
1099 /* Count cmd len */ 1111 /* Count cmd len */
1100 cmdlen = 1; 1112 cmdlen = 1;
1101 arg = non_const_argv; 1113 arg = non_const_argv;
1102 while (*arg) 1114 while (*arg)
1103 { 1115 {
1104 cmdlen = cmdlen + strlen (*arg) + 3; 1116 cmdlen = cmdlen + strlen (*arg) + 3;
1105 arg++; 1117 arg++;
1106 } 1118 }
1107 1119
1108 /* Allocate and create cmd */ 1120 /* Allocate and create cmd */
1109 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen); 1121 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
1110 arg = non_const_argv; 1122 arg = non_const_argv;
1111 while (*arg) 1123 while (*arg)
1112 { 1124 {
1113 idx += sprintf (idx, "\"%s\" ", *arg); 1125 idx += sprintf (idx, "\"%s\" ", *arg);
1114 arg++; 1126 arg++;
1115 } 1127 }
1116 1128
1117 while (argcount > 0) 1129 while (argcount > 0)
1118 GNUNET_free (non_const_argv[--argcount]); 1130 GNUNET_free (non_const_argv[--argcount]);
@@ -1122,19 +1134,19 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1122 start.cb = sizeof (start); 1134 start.cb = sizeof (start);
1123 1135
1124 control_pipe = 1136 control_pipe =
1125 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 1137 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
1126 GNUNET_DISK_PERM_USER_READ | 1138 GNUNET_DISK_PERM_USER_READ |
1127 GNUNET_DISK_PERM_USER_WRITE); 1139 GNUNET_DISK_PERM_USER_WRITE);
1128 if (control_pipe == NULL) 1140 if (control_pipe == NULL)
1129 { 1141 {
1130 GNUNET_free (cmd); 1142 GNUNET_free (cmd);
1131 GNUNET_free (path); 1143 GNUNET_free (path);
1132 return NULL; 1144 return NULL;
1133 } 1145 }
1134 1146
1135#if DEBUG_OS 1147#if DEBUG_OS
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "Opened the parent end of the pipe `%s'\n", childpipename); 1149 "Opened the parent end of the pipe `%s'\n", childpipename);
1138#endif 1150#endif
1139 1151
1140 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE); 1152 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE);
@@ -1147,13 +1159,13 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1147 if (!CreateProcess 1159 if (!CreateProcess
1148 (path, cmd, NULL, NULL, FALSE, DETACHED_PROCESS | CREATE_SUSPENDED, 1160 (path, cmd, NULL, NULL, FALSE, DETACHED_PROCESS | CREATE_SUSPENDED,
1149 env_block, NULL, &start, &proc)) 1161 env_block, NULL, &start, &proc))
1150 { 1162 {
1151 SetErrnoFromWinError (GetLastError ()); 1163 SetErrnoFromWinError (GetLastError ());
1152 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "CreateProcess"); 1164 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "CreateProcess");
1153 GNUNET_free (env_block); 1165 GNUNET_free (env_block);
1154 GNUNET_free (cmd); 1166 GNUNET_free (cmd);
1155 return NULL; 1167 return NULL;
1156 } 1168 }
1157 1169
1158 GNUNET_free (env_block); 1170 GNUNET_free (env_block);
1159 1171
@@ -1182,8 +1194,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1182 */ 1194 */
1183int 1195int
1184GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, 1196GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1185 enum GNUNET_OS_ProcessStatusType *type, 1197 enum GNUNET_OS_ProcessStatusType *type,
1186 unsigned long *code) 1198 unsigned long *code)
1187{ 1199{
1188#ifndef MINGW 1200#ifndef MINGW
1189 int status; 1201 int status;
@@ -1192,48 +1204,48 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1192 GNUNET_assert (0 != proc); 1204 GNUNET_assert (0 != proc);
1193 ret = waitpid (proc->pid, &status, WNOHANG); 1205 ret = waitpid (proc->pid, &status, WNOHANG);
1194 if (ret < 0) 1206 if (ret < 0)
1195 { 1207 {
1196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 1208 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1197 return GNUNET_SYSERR; 1209 return GNUNET_SYSERR;
1198 } 1210 }
1199 if (0 == ret) 1211 if (0 == ret)
1200 { 1212 {
1201 *type = GNUNET_OS_PROCESS_RUNNING; 1213 *type = GNUNET_OS_PROCESS_RUNNING;
1202 *code = 0; 1214 *code = 0;
1203 return GNUNET_NO; 1215 return GNUNET_NO;
1204 } 1216 }
1205 if (proc->pid != ret) 1217 if (proc->pid != ret)
1206 { 1218 {
1207 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 1219 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1208 return GNUNET_SYSERR; 1220 return GNUNET_SYSERR;
1209 } 1221 }
1210 if (WIFEXITED (status)) 1222 if (WIFEXITED (status))
1211 { 1223 {
1212 *type = GNUNET_OS_PROCESS_EXITED; 1224 *type = GNUNET_OS_PROCESS_EXITED;
1213 *code = WEXITSTATUS (status); 1225 *code = WEXITSTATUS (status);
1214 } 1226 }
1215 else if (WIFSIGNALED (status)) 1227 else if (WIFSIGNALED (status))
1216 { 1228 {
1217 *type = GNUNET_OS_PROCESS_SIGNALED; 1229 *type = GNUNET_OS_PROCESS_SIGNALED;
1218 *code = WTERMSIG (status); 1230 *code = WTERMSIG (status);
1219 } 1231 }
1220 else if (WIFSTOPPED (status)) 1232 else if (WIFSTOPPED (status))
1221 { 1233 {
1222 *type = GNUNET_OS_PROCESS_SIGNALED; 1234 *type = GNUNET_OS_PROCESS_SIGNALED;
1223 *code = WSTOPSIG (status); 1235 *code = WSTOPSIG (status);
1224 } 1236 }
1225#ifdef WIFCONTINUED 1237#ifdef WIFCONTINUED
1226 else if (WIFCONTINUED (status)) 1238 else if (WIFCONTINUED (status))
1227 { 1239 {
1228 *type = GNUNET_OS_PROCESS_RUNNING; 1240 *type = GNUNET_OS_PROCESS_RUNNING;
1229 *code = 0; 1241 *code = 0;
1230 } 1242 }
1231#endif 1243#endif
1232 else 1244 else
1233 { 1245 {
1234 *type = GNUNET_OS_PROCESS_UNKNOWN; 1246 *type = GNUNET_OS_PROCESS_UNKNOWN;
1235 *code = 0; 1247 *code = 0;
1236 } 1248 }
1237#else 1249#else
1238 HANDLE h; 1250 HANDLE h;
1239 DWORD c, error_code, ret; 1251 DWORD c, error_code, ret;
@@ -1241,11 +1253,11 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1241 h = proc->handle; 1253 h = proc->handle;
1242 ret = proc->pid; 1254 ret = proc->pid;
1243 if (h == NULL || ret == 0) 1255 if (h == NULL || ret == 0)
1244 { 1256 {
1245 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1257 LOG (GNUNET_ERROR_TYPE_WARNING,
1246 "Invalid process information {%d, %08X}\n", ret, h); 1258 "Invalid process information {%d, %08X}\n", ret, h);
1247 return GNUNET_SYSERR; 1259 return GNUNET_SYSERR;
1248 } 1260 }
1249 if (h == NULL) 1261 if (h == NULL)
1250 h = GetCurrentProcess (); 1262 h = GetCurrentProcess ();
1251 1263
@@ -1253,17 +1265,17 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1253 ret = GetExitCodeProcess (h, &c); 1265 ret = GetExitCodeProcess (h, &c);
1254 error_code = GetLastError (); 1266 error_code = GetLastError ();
1255 if (ret == 0 || error_code != NO_ERROR) 1267 if (ret == 0 || error_code != NO_ERROR)
1256 { 1268 {
1257 SetErrnoFromWinError (error_code); 1269 SetErrnoFromWinError (error_code);
1258 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess"); 1270 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1259 return GNUNET_SYSERR; 1271 return GNUNET_SYSERR;
1260 } 1272 }
1261 if (STILL_ACTIVE == c) 1273 if (STILL_ACTIVE == c)
1262 { 1274 {
1263 *type = GNUNET_OS_PROCESS_RUNNING; 1275 *type = GNUNET_OS_PROCESS_RUNNING;
1264 *code = 0; 1276 *code = 0;
1265 return GNUNET_NO; 1277 return GNUNET_NO;
1266 } 1278 }
1267 *type = GNUNET_OS_PROCESS_EXITED; 1279 *type = GNUNET_OS_PROCESS_EXITED;
1268 *code = c; 1280 *code = c;
1269#endif 1281#endif
@@ -1293,19 +1305,19 @@ GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1293 1305
1294 h = proc->handle; 1306 h = proc->handle;
1295 if (NULL == h) 1307 if (NULL == h)
1296 { 1308 {
1297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1309 LOG (GNUNET_ERROR_TYPE_WARNING,
1298 "Invalid process information {%d, %08X}\n", proc->pid, h); 1310 "Invalid process information {%d, %08X}\n", proc->pid, h);
1299 return GNUNET_SYSERR; 1311 return GNUNET_SYSERR;
1300 } 1312 }
1301 if (h == NULL) 1313 if (h == NULL)
1302 h = GetCurrentProcess (); 1314 h = GetCurrentProcess ();
1303 1315
1304 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE)) 1316 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1305 { 1317 {
1306 SetErrnoFromWinError (GetLastError ()); 1318 SetErrnoFromWinError (GetLastError ());
1307 ret = GNUNET_SYSERR; 1319 ret = GNUNET_SYSERR;
1308 } 1320 }
1309 else 1321 else
1310 ret = GNUNET_OK; 1322 ret = GNUNET_OK;
1311 1323
@@ -1379,10 +1391,10 @@ GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1379{ 1391{
1380 1392
1381 if (cmd->proc != NULL) 1393 if (cmd->proc != NULL)
1382 { 1394 {
1383 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask); 1395 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask);
1384 GNUNET_SCHEDULER_cancel (cmd->rtask); 1396 GNUNET_SCHEDULER_cancel (cmd->rtask);
1385 } 1397 }
1386 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL); 1398 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1387 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip)); 1399 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1388 GNUNET_OS_process_close (cmd->eip); 1400 GNUNET_OS_process_close (cmd->eip);
@@ -1406,42 +1418,43 @@ cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1406 ssize_t ret; 1418 ssize_t ret;
1407 1419
1408 cmd->rtask = GNUNET_SCHEDULER_NO_TASK; 1420 cmd->rtask = GNUNET_SCHEDULER_NO_TASK;
1409 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r)) 1421 if (GNUNET_YES !=
1410 { 1422 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1411 /* timeout, shutdown, etc. */ 1423 {
1412 proc = cmd->proc; 1424 /* timeout, shutdown, etc. */
1413 cmd->proc = NULL; 1425 proc = cmd->proc;
1414 proc (cmd->proc_cls, NULL); 1426 cmd->proc = NULL;
1415 return; 1427 proc (cmd->proc_cls, NULL);
1416 } 1428 return;
1429 }
1417 ret = 1430 ret =
1418 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off], 1431 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off],
1419 sizeof (cmd->buf) - cmd->off); 1432 sizeof (cmd->buf) - cmd->off);
1420 if (ret <= 0) 1433 if (ret <= 0)
1421 {
1422 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1423 { 1434 {
1424 cmd->buf[cmd->off] = '\0'; 1435 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1425 cmd->proc (cmd->proc_cls, cmd->buf); 1436 {
1437 cmd->buf[cmd->off] = '\0';
1438 cmd->proc (cmd->proc_cls, cmd->buf);
1439 }
1440 proc = cmd->proc;
1441 cmd->proc = NULL;
1442 proc (cmd->proc_cls, NULL);
1443 return;
1426 } 1444 }
1427 proc = cmd->proc;
1428 cmd->proc = NULL;
1429 proc (cmd->proc_cls, NULL);
1430 return;
1431 }
1432 end = memchr (&cmd->buf[cmd->off], '\n', ret); 1445 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1433 cmd->off += ret; 1446 cmd->off += ret;
1434 while (end != NULL) 1447 while (end != NULL)
1435 { 1448 {
1436 *end = '\0'; 1449 *end = '\0';
1437 cmd->proc (cmd->proc_cls, cmd->buf); 1450 cmd->proc (cmd->proc_cls, cmd->buf);
1438 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf)); 1451 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1439 cmd->off -= (end + 1 - cmd->buf); 1452 cmd->off -= (end + 1 - cmd->buf);
1440 end = memchr (cmd->buf, '\n', cmd->off); 1453 end = memchr (cmd->buf, '\n', cmd->off);
1441 } 1454 }
1442 cmd->rtask = 1455 cmd->rtask =
1443 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining 1456 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1444 (cmd->timeout), cmd->r, &cmd_read, cmd); 1457 (cmd->timeout), cmd->r, &cmd_read, cmd);
1445} 1458}
1446 1459
1447 1460
@@ -1458,8 +1471,8 @@ cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1458 */ 1471 */
1459struct GNUNET_OS_CommandHandle * 1472struct GNUNET_OS_CommandHandle *
1460GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls, 1473GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1461 struct GNUNET_TIME_Relative timeout, const char *binary, 1474 struct GNUNET_TIME_Relative timeout,
1462 ...) 1475 const char *binary, ...)
1463{ 1476{
1464 struct GNUNET_OS_CommandHandle *cmd; 1477 struct GNUNET_OS_CommandHandle *cmd;
1465 struct GNUNET_OS_Process *eip; 1478 struct GNUNET_OS_Process *eip;
@@ -1473,10 +1486,10 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1473 eip = GNUNET_OS_start_process_va (NULL, opipe, binary, ap); 1486 eip = GNUNET_OS_start_process_va (NULL, opipe, binary, ap);
1474 va_end (ap); 1487 va_end (ap);
1475 if (NULL == eip) 1488 if (NULL == eip)
1476 { 1489 {
1477 GNUNET_DISK_pipe_close (opipe); 1490 GNUNET_DISK_pipe_close (opipe);
1478 return NULL; 1491 return NULL;
1479 } 1492 }
1480 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE); 1493 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1481 cmd = GNUNET_malloc (sizeof (struct GNUNET_OS_CommandHandle)); 1494 cmd = GNUNET_malloc (sizeof (struct GNUNET_OS_CommandHandle));
1482 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout); 1495 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
@@ -1485,7 +1498,8 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1485 cmd->proc = proc; 1498 cmd->proc = proc;
1486 cmd->proc_cls = proc_cls; 1499 cmd->proc_cls = proc_cls;
1487 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ); 1500 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1488 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd); 1501 cmd->rtask =
1502 GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1489 return cmd; 1503 return cmd;
1490} 1504}
1491 1505
diff --git a/src/util/peer.c b/src/util/peer.c
index 39d11883d..c88e03c15 100644
--- a/src/util/peer.c
+++ b/src/util/peer.c
@@ -27,6 +27,8 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_peer_lib.h" 28#include "gnunet_peer_lib.h"
29 29
30#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31
30 32
31struct PeerEntry 33struct PeerEntry
32{ 34{
@@ -105,7 +107,7 @@ GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
105 * @return the interned identity. 107 * @return the interned identity.
106 */ 108 */
107GNUNET_PEER_Id 109GNUNET_PEER_Id
108GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid) 110GNUNET_PEER_intern (const struct GNUNET_PeerIdentity * pid)
109{ 111{
110 GNUNET_PEER_Id ret; 112 GNUNET_PEER_Id ret;
111 struct PeerEntry *e; 113 struct PeerEntry *e;
@@ -119,24 +121,24 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
119 off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey); 121 off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
120 e = (off == 0) ? NULL : &table[off]; 122 e = (off == 0) ? NULL : &table[off];
121 if (e != NULL) 123 if (e != NULL)
122 { 124 {
123 GNUNET_assert (e->rc > 0); 125 GNUNET_assert (e->rc > 0);
124 e->rc++; 126 e->rc++;
125 return e->pid; 127 return e->pid;
126 } 128 }
127 ret = free_list_start; 129 ret = free_list_start;
128 if (ret == size) 130 if (ret == size)
129 { 131 {
130 GNUNET_array_grow (table, size, size + 16); 132 GNUNET_array_grow (table, size, size + 16);
131 for (i = ret; i < size; i++) 133 for (i = ret; i < size; i++)
132 table[i].pid = i + 1; 134 table[i].pid = i + 1;
133 } 135 }
134 if (ret == 0) 136 if (ret == 0)
135 { 137 {
136 table[0].pid = 0; 138 table[0].pid = 0;
137 table[0].rc = 1; 139 table[0].rc = 1;
138 ret = 1; 140 ret = 1;
139 } 141 }
140 GNUNET_assert (ret < size); 142 GNUNET_assert (ret < size);
141 GNUNET_assert (table[ret].rc == 0); 143 GNUNET_assert (table[ret].rc == 0);
142 free_list_start = table[ret].pid; 144 free_list_start = table[ret].pid;
@@ -144,9 +146,9 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
144 table[ret].rc = 1; 146 table[ret].rc = 1;
145 table[ret].pid = ret; 147 table[ret].pid = ret;
146 GNUNET_break (GNUNET_OK == 148 GNUNET_break (GNUNET_OK ==
147 GNUNET_CONTAINER_multihashmap_put (map, &pid->hashPubKey, 149 GNUNET_CONTAINER_multihashmap_put (map, &pid->hashPubKey,
148 (void *) (long) ret, 150 (void *) (long) ret,
149 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
150 return ret; 152 return ret;
151} 153}
152 154
@@ -158,7 +160,7 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
158 * @param count size of the ids array 160 * @param count size of the ids array
159 */ 161 */
160void 162void
161GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count) 163GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, unsigned int count)
162{ 164{
163 int i; 165 int i;
164 GNUNET_PEER_Id id; 166 GNUNET_PEER_Id id;
@@ -166,24 +168,25 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
166 if (count == 0) 168 if (count == 0)
167 return; 169 return;
168 for (i = count - 1; i >= 0; i--) 170 for (i = count - 1; i >= 0; i--)
169 {
170 id = ids[i];
171 if (id == 0)
172 continue;
173 GNUNET_assert (id < size);
174 GNUNET_assert (table[id].rc > 0);
175 table[id].rc--;
176 if (table[id].rc == 0)
177 { 171 {
178 GNUNET_break (GNUNET_OK == 172 id = ids[i];
179 GNUNET_CONTAINER_multihashmap_remove (map, 173 if (id == 0)
180 &table[id]. 174 continue;
181 id.hashPubKey, 175 GNUNET_assert (id < size);
182 (void *) (long) id)); 176 GNUNET_assert (table[id].rc > 0);
183 table[id].pid = free_list_start; 177 table[id].rc--;
184 free_list_start = id; 178 if (table[id].rc == 0)
179 {
180 GNUNET_break (GNUNET_OK ==
181 GNUNET_CONTAINER_multihashmap_remove (map,
182 &table[id].
183 id.hashPubKey,
184 (void *) (long)
185 id));
186 table[id].pid = free_list_start;
187 free_list_start = id;
188 }
185 } 189 }
186 }
187} 190}
188 191
189 192
@@ -203,15 +206,16 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
203 GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta)); 206 GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta));
204 table[id].rc += delta; 207 table[id].rc += delta;
205 if (table[id].rc == 0) 208 if (table[id].rc == 0)
206 { 209 {
207 GNUNET_break (GNUNET_OK == 210 GNUNET_break (GNUNET_OK ==
208 GNUNET_CONTAINER_multihashmap_remove (map, 211 GNUNET_CONTAINER_multihashmap_remove (map,
209 &table[id]. 212 &table[id].
210 id.hashPubKey, 213 id.hashPubKey,
211 (void *) (long) id)); 214 (void *) (long)
212 table[id].pid = free_list_start; 215 id));
213 free_list_start = id; 216 table[id].pid = free_list_start;
214 } 217 free_list_start = id;
218 }
215} 219}
216 220
217 221
@@ -225,11 +229,11 @@ void
225GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) 229GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
226{ 230{
227 if (id == 0) 231 if (id == 0)
228 { 232 {
229 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity)); 233 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
230 GNUNET_break (0); 234 GNUNET_break (0);
231 return; 235 return;
232 } 236 }
233 GNUNET_assert (id < size); 237 GNUNET_assert (id < size);
234 GNUNET_assert (table[id].rc > 0); 238 GNUNET_assert (table[id].rc > 0);
235 *pid = table[id].id; 239 *pid = table[id].id;
diff --git a/src/util/perf_crypto_hash.c b/src/util/perf_crypto_hash.c
index d883776b4..746b33eca 100644
--- a/src/util/perf_crypto_hash.c
+++ b/src/util/perf_crypto_hash.c
@@ -42,11 +42,11 @@ perfHash ()
42 memset (buf, 1, 1024 * 64); 42 memset (buf, 1, 1024 * 64);
43 GNUNET_CRYPTO_hash ("foo", 3, &hc1); 43 GNUNET_CRYPTO_hash ("foo", 3, &hc1);
44 for (i = 0; i < 1024; i++) 44 for (i = 0; i < 1024; i++)
45 { 45 {
46 GNUNET_CRYPTO_hash (&hc1, sizeof (GNUNET_HashCode), &hc2); 46 GNUNET_CRYPTO_hash (&hc1, sizeof (GNUNET_HashCode), &hc2);
47 GNUNET_CRYPTO_hash (&hc2, sizeof (GNUNET_HashCode), &hc1); 47 GNUNET_CRYPTO_hash (&hc2, sizeof (GNUNET_HashCode), &hc1);
48 GNUNET_CRYPTO_hash (buf, 1024 * 64, &hc3); 48 GNUNET_CRYPTO_hash (buf, 1024 * 64, &hc3);
49 } 49 }
50 GNUNET_free (buf); 50 GNUNET_free (buf);
51} 51}
52 52
@@ -58,12 +58,12 @@ main (int argc, char *argv[])
58 start = GNUNET_TIME_absolute_get (); 58 start = GNUNET_TIME_absolute_get ();
59 perfHash (); 59 perfHash ();
60 printf ("Hash perf took %llu ms\n", 60 printf ("Hash perf took %llu ms\n",
61 (unsigned long long) 61 (unsigned long long)
62 GNUNET_TIME_absolute_get_duration (start).rel_value); 62 GNUNET_TIME_absolute_get_duration (start).rel_value);
63 GAUGER ("UTIL", "Cryptographic hashing", 63 GAUGER ("UTIL", "Cryptographic hashing",
64 1024 * 64 * 1024 / (1 + 64 1024 * 64 * 1024 / (1 +
65 GNUNET_TIME_absolute_get_duration 65 GNUNET_TIME_absolute_get_duration
66 (start).rel_value), "kb/s"); 66 (start).rel_value), "kb/s");
67 return 0; 67 return 0;
68} 68}
69 69
diff --git a/src/util/plugin.c b/src/util/plugin.c
index dbd3519aa..77631410c 100644
--- a/src/util/plugin.c
+++ b/src/util/plugin.c
@@ -30,6 +30,8 @@
30#include "gnunet_os_lib.h" 30#include "gnunet_os_lib.h"
31#include "gnunet_plugin_lib.h" 31#include "gnunet_plugin_lib.h"
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
33/** 35/**
34 * Linked list of active plugins. 36 * Linked list of active plugins.
35 */ 37 */
@@ -83,30 +85,30 @@ plugin_init ()
83 85
84 err = lt_dlinit (); 86 err = lt_dlinit ();
85 if (err > 0) 87 if (err > 0)
86 { 88 {
87 fprintf (stderr, _("Initialization of plugin mechanism failed: %s!\n"), 89 fprintf (stderr, _("Initialization of plugin mechanism failed: %s!\n"),
88 lt_dlerror ()); 90 lt_dlerror ());
89 return; 91 return;
90 } 92 }
91 opath = lt_dlgetsearchpath (); 93 opath = lt_dlgetsearchpath ();
92 if (opath != NULL) 94 if (opath != NULL)
93 old_dlsearchpath = GNUNET_strdup (opath); 95 old_dlsearchpath = GNUNET_strdup (opath);
94 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); 96 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
95 if (path != NULL) 97 if (path != NULL)
96 {
97 if (opath != NULL)
98 { 98 {
99 GNUNET_asprintf (&cpath, "%s:%s", opath, path); 99 if (opath != NULL)
100 lt_dlsetsearchpath (cpath); 100 {
101 GNUNET_free (path); 101 GNUNET_asprintf (&cpath, "%s:%s", opath, path);
102 GNUNET_free (cpath); 102 lt_dlsetsearchpath (cpath);
103 GNUNET_free (path);
104 GNUNET_free (cpath);
105 }
106 else
107 {
108 lt_dlsetsearchpath (path);
109 GNUNET_free (path);
110 }
103 } 111 }
104 else
105 {
106 lt_dlsetsearchpath (path);
107 GNUNET_free (path);
108 }
109 }
110} 112}
111 113
112 114
@@ -118,10 +120,10 @@ plugin_fini ()
118{ 120{
119 lt_dlsetsearchpath (old_dlsearchpath); 121 lt_dlsetsearchpath (old_dlsearchpath);
120 if (old_dlsearchpath != NULL) 122 if (old_dlsearchpath != NULL)
121 { 123 {
122 GNUNET_free (old_dlsearchpath); 124 GNUNET_free (old_dlsearchpath);
123 old_dlsearchpath = NULL; 125 old_dlsearchpath = NULL;
124 } 126 }
125 lt_dlexit (); 127 lt_dlexit ();
126} 128}
127 129
@@ -140,9 +142,9 @@ resolve_function (struct PluginList *plug, const char *name)
140 if (mptr == NULL) 142 if (mptr == NULL)
141 mptr = lt_dlsym (plug->handle, initName); 143 mptr = lt_dlsym (plug->handle, initName);
142 if (mptr == NULL) 144 if (mptr == NULL)
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 145 LOG (GNUNET_ERROR_TYPE_ERROR,
144 _("`%s' failed to resolve method '%s' with error: %s\n"), 146 _("`%s' failed to resolve method '%s' with error: %s\n"),
145 "lt_dlsym", &initName[1], lt_dlerror ()); 147 "lt_dlsym", &initName[1], lt_dlerror ());
146 GNUNET_free (initName); 148 GNUNET_free (initName);
147 return mptr; 149 return mptr;
148} 150}
@@ -164,10 +166,10 @@ GNUNET_PLUGIN_test (const char *library_name)
164 struct PluginList plug; 166 struct PluginList plug;
165 167
166 if (!initialized) 168 if (!initialized)
167 { 169 {
168 initialized = GNUNET_YES; 170 initialized = GNUNET_YES;
169 plugin_init (); 171 plugin_init ();
170 } 172 }
171 libhandle = lt_dlopenext (library_name); 173 libhandle = lt_dlopenext (library_name);
172 if (libhandle == NULL) 174 if (libhandle == NULL)
173 return GNUNET_NO; 175 return GNUNET_NO;
@@ -175,11 +177,11 @@ GNUNET_PLUGIN_test (const char *library_name)
175 plug.name = (char *) library_name; 177 plug.name = (char *) library_name;
176 init = resolve_function (&plug, "init"); 178 init = resolve_function (&plug, "init");
177 if (init == NULL) 179 if (init == NULL)
178 { 180 {
179 GNUNET_break (0); 181 GNUNET_break (0);
180 lt_dlclose (libhandle); 182 lt_dlclose (libhandle);
181 return GNUNET_NO; 183 return GNUNET_NO;
182 } 184 }
183 lt_dlclose (libhandle); 185 lt_dlclose (libhandle);
184 return GNUNET_YES; 186 return GNUNET_YES;
185} 187}
@@ -206,18 +208,18 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
206 void *ret; 208 void *ret;
207 209
208 if (!initialized) 210 if (!initialized)
209 { 211 {
210 initialized = GNUNET_YES; 212 initialized = GNUNET_YES;
211 plugin_init (); 213 plugin_init ();
212 } 214 }
213 libhandle = lt_dlopenext (library_name); 215 libhandle = lt_dlopenext (library_name);
214 if (libhandle == NULL) 216 if (libhandle == NULL)
215 { 217 {
216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 218 LOG (GNUNET_ERROR_TYPE_ERROR,
217 _("`%s' failed for library `%s' with error: %s\n"), 219 _("`%s' failed for library `%s' with error: %s\n"),
218 "lt_dlopenext", library_name, lt_dlerror ()); 220 "lt_dlopenext", library_name, lt_dlerror ());
219 return NULL; 221 return NULL;
220 } 222 }
221 plug = GNUNET_malloc (sizeof (struct PluginList)); 223 plug = GNUNET_malloc (sizeof (struct PluginList));
222 plug->handle = libhandle; 224 plug->handle = libhandle;
223 plug->name = GNUNET_strdup (library_name); 225 plug->name = GNUNET_strdup (library_name);
@@ -225,13 +227,13 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
225 plugins = plug; 227 plugins = plug;
226 init = resolve_function (plug, "init"); 228 init = resolve_function (plug, "init");
227 if ((init == NULL) || (NULL == (ret = init (arg)))) 229 if ((init == NULL) || (NULL == (ret = init (arg))))
228 { 230 {
229 lt_dlclose (libhandle); 231 lt_dlclose (libhandle);
230 GNUNET_free (plug->name); 232 GNUNET_free (plug->name);
231 plugins = plug->next; 233 plugins = plug->next;
232 GNUNET_free (plug); 234 GNUNET_free (plug);
233 return NULL; 235 return NULL;
234 } 236 }
235 return ret; 237 return ret;
236} 238}
237 239
@@ -255,10 +257,10 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg)
255 prev = NULL; 257 prev = NULL;
256 pos = plugins; 258 pos = plugins;
257 while ((pos != NULL) && (0 != strcmp (pos->name, library_name))) 259 while ((pos != NULL) && (0 != strcmp (pos->name, library_name)))
258 { 260 {
259 prev = pos; 261 prev = pos;
260 pos = pos->next; 262 pos = pos->next;
261 } 263 }
262 if (pos == NULL) 264 if (pos == NULL)
263 return NULL; 265 return NULL;
264 266
@@ -274,10 +276,10 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg)
274 GNUNET_free (pos->name); 276 GNUNET_free (pos->name);
275 GNUNET_free (pos); 277 GNUNET_free (pos);
276 if (plugins == NULL) 278 if (plugins == NULL)
277 { 279 {
278 plugin_fini (); 280 plugin_fini ();
279 initialized = GNUNET_NO; 281 initialized = GNUNET_NO;
280 } 282 }
281 return ret; 283 return ret;
282} 284}
283 285
diff --git a/src/util/program.c b/src/util/program.c
index 6538f303f..5582f30c6 100644
--- a/src/util/program.c
+++ b/src/util/program.c
@@ -36,6 +36,10 @@
36#include "gnunet_scheduler_lib.h" 36#include "gnunet_scheduler_lib.h"
37#include <gcrypt.h> 37#include <gcrypt.h>
38 38
39#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40
41#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42
39/** 43/**
40 * Context for the command. 44 * Context for the command.
41 */ 45 */
@@ -125,9 +129,9 @@ cmd_sorter (__const void *a1, __const void *a2)
125 */ 129 */
126int 130int
127GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName, 131GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
128 const char *binaryHelp, 132 const char *binaryHelp,
129 const struct GNUNET_GETOPT_CommandLineOption *options, 133 const struct GNUNET_GETOPT_CommandLineOption *options,
130 GNUNET_PROGRAM_Main task, void *task_cls) 134 GNUNET_PROGRAM_Main task, void *task_cls)
131{ 135{
132 struct CommandContext cc; 136 struct CommandContext cc;
133 char *path; 137 char *path;
@@ -155,29 +159,29 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
155 logfile = NULL; 159 logfile = NULL;
156 gargs = getenv ("GNUNET_ARGS"); 160 gargs = getenv ("GNUNET_ARGS");
157 if (gargs != NULL) 161 if (gargs != NULL)
158 {
159 char **gargv;
160 unsigned int gargc;
161 int i;
162 char *tok;
163 char *cargs;
164
165 gargv = NULL;
166 gargc = 0;
167 for (i = 0; i < argc; i++)
168 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
169 cargs = GNUNET_strdup (gargs);
170 tok = strtok (cargs, " ");
171 while (NULL != tok)
172 { 162 {
173 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok)); 163 char **gargv;
174 tok = strtok (NULL, " "); 164 unsigned int gargc;
165 int i;
166 char *tok;
167 char *cargs;
168
169 gargv = NULL;
170 gargc = 0;
171 for (i = 0; i < argc; i++)
172 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
173 cargs = GNUNET_strdup (gargs);
174 tok = strtok (cargs, " ");
175 while (NULL != tok)
176 {
177 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
178 tok = strtok (NULL, " ");
179 }
180 GNUNET_free (cargs);
181 GNUNET_array_append (gargv, gargc, NULL);
182 argv = (char *const *) gargv;
183 argc = gargc - 1;
175 } 184 }
176 GNUNET_free (cargs);
177 GNUNET_array_append (gargv, gargc, NULL);
178 argv = (char *const *) gargv;
179 argc = gargc - 1;
180 }
181 memset (&cc, 0, sizeof (cc)); 185 memset (&cc, 0, sizeof (cc));
182 loglev = NULL; 186 loglev = NULL;
183 cc.task = task; 187 cc.task = task;
@@ -189,27 +193,28 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
189 setlocale (LC_ALL, ""); 193 setlocale (LC_ALL, "");
190 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); 194 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
191 if (path != NULL) 195 if (path != NULL)
192 { 196 {
193 BINDTEXTDOMAIN ("GNUnet", path); 197 BINDTEXTDOMAIN ("GNUnet", path);
194 GNUNET_free (path); 198 GNUNET_free (path);
195 } 199 }
196 textdomain ("GNUnet"); 200 textdomain ("GNUnet");
197#endif 201#endif
198 cnt = 0; 202 cnt = 0;
199 while (options[cnt].name != NULL) 203 while (options[cnt].name != NULL)
200 cnt++; 204 cnt++;
201 allopts = 205 allopts =
202 GNUNET_malloc ((cnt + 206 GNUNET_malloc ((cnt +
203 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) + 207 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
204 sizeof (defoptions)); 208 sizeof (defoptions));
205 memcpy (allopts, defoptions, sizeof (defoptions)); 209 memcpy (allopts, defoptions, sizeof (defoptions));
206 memcpy (&allopts 210 memcpy (&allopts
207 [sizeof (defoptions) / 211 [sizeof (defoptions) /
208 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options, 212 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
209 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption)); 213 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
210 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption); 214 cnt +=
215 sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
211 qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption), 216 qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption),
212 &cmd_sorter); 217 &cmd_sorter);
213 loglev = NULL; 218 loglev = NULL;
214 cc.cfgfile = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE); 219 cc.cfgfile = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
215 lpfx = GNUNET_strdup (binaryName); 220 lpfx = GNUNET_strdup (binaryName);
@@ -217,30 +222,31 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
217 *spc = '\0'; 222 *spc = '\0';
218 if ((-1 == 223 if ((-1 ==
219 (ret = 224 (ret =
220 GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv))) || 225 GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv)))
221 (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)) ) 226 || (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
222 { 227 {
223 GNUNET_CONFIGURATION_destroy (cfg); 228 GNUNET_CONFIGURATION_destroy (cfg);
224 GNUNET_free_non_null (cc.cfgfile); 229 GNUNET_free_non_null (cc.cfgfile);
225 GNUNET_free_non_null (loglev); 230 GNUNET_free_non_null (loglev);
226 GNUNET_free_non_null (logfile); 231 GNUNET_free_non_null (logfile);
227 GNUNET_free (allopts); 232 GNUNET_free (allopts);
228 GNUNET_free (lpfx); 233 GNUNET_free (lpfx);
229 return GNUNET_SYSERR; 234 return GNUNET_SYSERR;
230 } 235 }
231 (void) GNUNET_CONFIGURATION_load (cfg, cc.cfgfile); 236 (void) GNUNET_CONFIGURATION_load (cfg, cc.cfgfile);
232 GNUNET_free (allopts); 237 GNUNET_free (allopts);
233 GNUNET_free (lpfx); 238 GNUNET_free (lpfx);
234 if (GNUNET_OK == 239 if (GNUNET_OK ==
235 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset", 240 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset",
236 &skew_offset) && 241 &skew_offset) &&
237 (GNUNET_OK == 242 (GNUNET_OK ==
238 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", 243 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing",
239 "skew_variance", &skew_variance))) 244 "skew_variance",
240 { 245 &skew_variance)))
241 clock_offset = skew_offset - skew_variance; 246 {
242 GNUNET_TIME_set_offset (clock_offset); 247 clock_offset = skew_offset - skew_variance;
243 } 248 GNUNET_TIME_set_offset (clock_offset);
249 }
244 /* run */ 250 /* run */
245 cc.args = &argv[ret]; 251 cc.args = &argv[ret];
246 GNUNET_SCHEDULER_run (&program_main, &cc); 252 GNUNET_SCHEDULER_run (&program_main, &cc);
diff --git a/src/util/pseudonym.c b/src/util/pseudonym.c
index 85353c6da..b6e51af18 100644
--- a/src/util/pseudonym.c
+++ b/src/util/pseudonym.c
@@ -31,6 +31,10 @@
31#include "gnunet_pseudonym_lib.h" 31#include "gnunet_pseudonym_lib.h"
32#include "gnunet_bio_lib.h" 32#include "gnunet_bio_lib.h"
33 33
34#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35
36#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
37
34/** 38/**
35 * Name of the directory which stores meta data for pseudonym 39 * Name of the directory which stores meta data for pseudonym
36 */ 40 */
@@ -78,16 +82,16 @@ static struct DiscoveryCallback *head;
78 */ 82 */
79static void 83static void
80internal_notify (const GNUNET_HashCode * id, 84internal_notify (const GNUNET_HashCode * id,
81 const struct GNUNET_CONTAINER_MetaData *md, int rating) 85 const struct GNUNET_CONTAINER_MetaData *md, int rating)
82{ 86{
83 struct DiscoveryCallback *pos; 87 struct DiscoveryCallback *pos;
84 88
85 pos = head; 89 pos = head;
86 while (pos != NULL) 90 while (pos != NULL)
87 { 91 {
88 pos->callback (pos->closure, id, md, rating); 92 pos->callback (pos->closure, id, md, rating);
89 pos = pos->next; 93 pos = pos->next;
90 } 94 }
91} 95}
92 96
93/** 97/**
@@ -99,9 +103,10 @@ internal_notify (const GNUNET_HashCode * id,
99 */ 103 */
100int 104int
101GNUNET_PSEUDONYM_discovery_callback_register (const struct 105GNUNET_PSEUDONYM_discovery_callback_register (const struct
102 GNUNET_CONFIGURATION_Handle *cfg, 106 GNUNET_CONFIGURATION_Handle
103 GNUNET_PSEUDONYM_Iterator 107 *cfg,
104 iterator, void *closure) 108 GNUNET_PSEUDONYM_Iterator
109 iterator, void *closure)
105{ 110{
106 struct DiscoveryCallback *list; 111 struct DiscoveryCallback *list;
107 112
@@ -121,7 +126,7 @@ GNUNET_PSEUDONYM_discovery_callback_register (const struct
121 */ 126 */
122int 127int
123GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator 128GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
124 iterator, void *closure) 129 iterator, void *closure)
125{ 130{
126 struct DiscoveryCallback *prev; 131 struct DiscoveryCallback *prev;
127 struct DiscoveryCallback *pos; 132 struct DiscoveryCallback *pos;
@@ -129,11 +134,11 @@ GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
129 prev = NULL; 134 prev = NULL;
130 pos = head; 135 pos = head;
131 while ((pos != NULL) && 136 while ((pos != NULL) &&
132 ((pos->callback != iterator) || (pos->closure != closure))) 137 ((pos->callback != iterator) || (pos->closure != closure)))
133 { 138 {
134 prev = pos; 139 prev = pos;
135 pos = pos->next; 140 pos = pos->next;
136 } 141 }
137 if (pos == NULL) 142 if (pos == NULL)
138 return GNUNET_SYSERR; 143 return GNUNET_SYSERR;
139 if (prev == NULL) 144 if (prev == NULL)
@@ -155,16 +160,17 @@ GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
155 */ 160 */
156static char * 161static char *
157get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, 162get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
158 const char *prefix, const GNUNET_HashCode * psid) 163 const char *prefix, const GNUNET_HashCode * psid)
159{ 164{
160 struct GNUNET_CRYPTO_HashAsciiEncoded enc; 165 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
161 166
162 if (psid != NULL) 167 if (psid != NULL)
163 GNUNET_CRYPTO_hash_to_enc (psid, &enc); 168 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
164 return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix, 169 return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME,
165 (psid == 170 prefix,
166 NULL) ? NULL : (const char *) &enc, 171 (psid ==
167 NULL); 172 NULL) ? NULL : (const char *) &enc,
173 NULL);
168} 174}
169 175
170 176
@@ -178,9 +184,9 @@ get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
178 */ 184 */
179static void 185static void
180write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg, 186write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
181 const GNUNET_HashCode * nsid, 187 const GNUNET_HashCode * nsid,
182 const struct GNUNET_CONTAINER_MetaData *meta, 188 const struct GNUNET_CONTAINER_MetaData *meta,
183 int32_t ranking, const char *ns_name) 189 int32_t ranking, const char *ns_name)
184{ 190{
185 char *fn; 191 char *fn;
186 struct GNUNET_BIO_WriteHandle *fileW; 192 struct GNUNET_BIO_WriteHandle *fileW;
@@ -189,23 +195,23 @@ write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
189 GNUNET_assert (fn != NULL); 195 GNUNET_assert (fn != NULL);
190 fileW = GNUNET_BIO_write_open (fn); 196 fileW = GNUNET_BIO_write_open (fn);
191 if (NULL != fileW) 197 if (NULL != fileW)
192 {
193 if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
194 (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
195 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
196 { 198 {
197 (void) GNUNET_BIO_write_close (fileW); 199 if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
198 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); 200 (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
199 GNUNET_free (fn); 201 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
200 return; 202 {
201 } 203 (void) GNUNET_BIO_write_close (fileW);
202 if (GNUNET_OK != GNUNET_BIO_write_close (fileW)) 204 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
203 { 205 GNUNET_free (fn);
204 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); 206 return;
205 GNUNET_free (fn); 207 }
206 return; 208 if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
209 {
210 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
211 GNUNET_free (fn);
212 return;
213 }
207 } 214 }
208 }
209 GNUNET_free (fn); 215 GNUNET_free (fn);
210 /* create entry for pseudonym name in names */ 216 /* create entry for pseudonym name in names */
211 /* FIXME: 90% of what this call does is not needed 217 /* FIXME: 90% of what this call does is not needed
@@ -224,9 +230,9 @@ write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
224 */ 230 */
225static int 231static int
226read_info (const struct GNUNET_CONFIGURATION_Handle *cfg, 232read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
227 const GNUNET_HashCode * nsid, 233 const GNUNET_HashCode * nsid,
228 struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking, 234 struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
229 char **ns_name) 235 char **ns_name)
230{ 236{
231 char *fn; 237 char *fn;
232 char *emsg; 238 char *emsg;
@@ -236,10 +242,10 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
236 GNUNET_assert (fn != NULL); 242 GNUNET_assert (fn != NULL);
237 fileR = GNUNET_BIO_read_open (fn); 243 fileR = GNUNET_BIO_read_open (fn);
238 if (fileR == NULL) 244 if (fileR == NULL)
239 { 245 {
240 GNUNET_free (fn); 246 GNUNET_free (fn);
241 return GNUNET_SYSERR; 247 return GNUNET_SYSERR;
242 } 248 }
243 emsg = NULL; 249 emsg = NULL;
244 *ns_name = NULL; 250 *ns_name = NULL;
245 if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) || 251 if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
@@ -247,30 +253,30 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
247 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) || 253 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
248 (GNUNET_OK != 254 (GNUNET_OK !=
249 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta))) 255 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
250 { 256 {
251 (void) GNUNET_BIO_read_close (fileR, &emsg); 257 (void) GNUNET_BIO_read_close (fileR, &emsg);
252 GNUNET_free_non_null (emsg); 258 GNUNET_free_non_null (emsg);
253 GNUNET_free_non_null (*ns_name); 259 GNUNET_free_non_null (*ns_name);
254 *ns_name = NULL; 260 *ns_name = NULL;
255 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); 261 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
256 GNUNET_free (fn); 262 GNUNET_free (fn);
257 return GNUNET_SYSERR; 263 return GNUNET_SYSERR;
258 } 264 }
259 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg)) 265 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
260 { 266 {
261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 267 LOG (GNUNET_ERROR_TYPE_WARNING,
262 _ 268 _
263 ("Failed to parse metadata about pseudonym from file `%s': %s\n"), 269 ("Failed to parse metadata about pseudonym from file `%s': %s\n"),
264 fn, emsg); 270 fn, emsg);
265 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); 271 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
266 GNUNET_CONTAINER_meta_data_destroy (*meta); 272 GNUNET_CONTAINER_meta_data_destroy (*meta);
267 *meta = NULL; 273 *meta = NULL;
268 GNUNET_free_non_null (*ns_name); 274 GNUNET_free_non_null (*ns_name);
269 *ns_name = NULL; 275 *ns_name = NULL;
270 GNUNET_free_non_null (emsg); 276 GNUNET_free_non_null (emsg);
271 GNUNET_free (fn); 277 GNUNET_free (fn);
272 return GNUNET_SYSERR; 278 return GNUNET_SYSERR;
273 } 279 }
274 GNUNET_free (fn); 280 GNUNET_free (fn);
275 return GNUNET_OK; 281 return GNUNET_OK;
276} 282}
@@ -286,7 +292,7 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
286 */ 292 */
287char * 293char *
288GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg, 294GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
289 const GNUNET_HashCode * nsid) 295 const GNUNET_HashCode * nsid)
290{ 296{
291 struct GNUNET_CONTAINER_MetaData *meta; 297 struct GNUNET_CONTAINER_MetaData *meta;
292 char *name; 298 char *name;
@@ -304,25 +310,25 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
304 meta = NULL; 310 meta = NULL;
305 name = NULL; 311 name = NULL;
306 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name)) 312 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
307 {
308 if ((meta != NULL) && (name == NULL))
309 name =
310 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
311 EXTRACTOR_METATYPE_TITLE,
312 EXTRACTOR_METATYPE_FILENAME,
313 EXTRACTOR_METATYPE_DESCRIPTION,
314 EXTRACTOR_METATYPE_SUBJECT,
315 EXTRACTOR_METATYPE_PUBLISHER,
316 EXTRACTOR_METATYPE_AUTHOR_NAME,
317 EXTRACTOR_METATYPE_COMMENT,
318 EXTRACTOR_METATYPE_SUMMARY,
319 -1);
320 if (meta != NULL)
321 { 313 {
322 GNUNET_CONTAINER_meta_data_destroy (meta); 314 if ((meta != NULL) && (name == NULL))
323 meta = NULL; 315 name =
316 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
317 EXTRACTOR_METATYPE_TITLE,
318 EXTRACTOR_METATYPE_FILENAME,
319 EXTRACTOR_METATYPE_DESCRIPTION,
320 EXTRACTOR_METATYPE_SUBJECT,
321 EXTRACTOR_METATYPE_PUBLISHER,
322 EXTRACTOR_METATYPE_AUTHOR_NAME,
323 EXTRACTOR_METATYPE_COMMENT,
324 EXTRACTOR_METATYPE_SUMMARY,
325 -1);
326 if (meta != NULL)
327 {
328 GNUNET_CONTAINER_meta_data_destroy (meta);
329 meta = NULL;
330 }
324 } 331 }
325 }
326 if (name == NULL) 332 if (name == NULL)
327 name = GNUNET_strdup (_("no-name")); 333 name = GNUNET_strdup (_("no-name"));
328 GNUNET_CRYPTO_hash (name, strlen (name), &nh); 334 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
@@ -333,31 +339,31 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
333 if (0 == STAT (fn, &sbuf)) 339 if (0 == STAT (fn, &sbuf))
334 GNUNET_DISK_file_size (fn, &len, GNUNET_YES); 340 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
335 fh = GNUNET_DISK_file_open (fn, 341 fh = GNUNET_DISK_file_open (fn,
336 GNUNET_DISK_OPEN_CREATE | 342 GNUNET_DISK_OPEN_CREATE |
337 GNUNET_DISK_OPEN_READWRITE, 343 GNUNET_DISK_OPEN_READWRITE,
338 GNUNET_DISK_PERM_USER_READ | 344 GNUNET_DISK_PERM_USER_READ |
339 GNUNET_DISK_PERM_USER_WRITE); 345 GNUNET_DISK_PERM_USER_WRITE);
340 i = 0; 346 i = 0;
341 idx = -1; 347 idx = -1;
342 while ((len >= sizeof (GNUNET_HashCode)) && 348 while ((len >= sizeof (GNUNET_HashCode)) &&
343 (sizeof (GNUNET_HashCode) == 349 (sizeof (GNUNET_HashCode) ==
344 GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode)))) 350 GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
345 {
346 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
347 { 351 {
348 idx = i; 352 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
349 break; 353 {
354 idx = i;
355 break;
356 }
357 i++;
358 len -= sizeof (GNUNET_HashCode);
350 } 359 }
351 i++;
352 len -= sizeof (GNUNET_HashCode);
353 }
354 if (idx == -1) 360 if (idx == -1)
355 { 361 {
356 idx = i; 362 idx = i;
357 if (sizeof (GNUNET_HashCode) != 363 if (sizeof (GNUNET_HashCode) !=
358 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode))) 364 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); 365 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
360 } 366 }
361 GNUNET_DISK_file_close (fh); 367 GNUNET_DISK_file_close (fh);
362 ret = GNUNET_malloc (strlen (name) + 32); 368 ret = GNUNET_malloc (strlen (name) + 32);
363 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx); 369 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
@@ -376,7 +382,7 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
376 */ 382 */
377int 383int
378GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg, 384GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
379 const char *ns_uname, GNUNET_HashCode * nsid) 385 const char *ns_uname, GNUNET_HashCode * nsid)
380{ 386{
381 size_t slen; 387 size_t slen;
382 uint64_t len; 388 uint64_t len;
@@ -402,24 +408,24 @@ GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
402 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) || 408 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
403 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) || 409 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
404 ((idx + 1) * sizeof (GNUNET_HashCode) > len)) 410 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
405 { 411 {
406 GNUNET_free (fn); 412 GNUNET_free (fn);
407 return GNUNET_SYSERR; 413 return GNUNET_SYSERR;
408 } 414 }
409 fh = GNUNET_DISK_file_open (fn, 415 fh = GNUNET_DISK_file_open (fn,
410 GNUNET_DISK_OPEN_CREATE | 416 GNUNET_DISK_OPEN_CREATE |
411 GNUNET_DISK_OPEN_READWRITE, 417 GNUNET_DISK_OPEN_READWRITE,
412 GNUNET_DISK_PERM_USER_READ | 418 GNUNET_DISK_PERM_USER_READ |
413 GNUNET_DISK_PERM_USER_WRITE); 419 GNUNET_DISK_PERM_USER_WRITE);
414 GNUNET_free (fn); 420 GNUNET_free (fn);
415 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), 421 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
416 GNUNET_DISK_SEEK_SET); 422 GNUNET_DISK_SEEK_SET);
417 if (sizeof (GNUNET_HashCode) != 423 if (sizeof (GNUNET_HashCode) !=
418 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode))) 424 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
419 { 425 {
420 GNUNET_DISK_file_close (fh); 426 GNUNET_DISK_file_close (fh);
421 return GNUNET_SYSERR; 427 return GNUNET_SYSERR;
422 } 428 }
423 GNUNET_DISK_file_close (fh); 429 GNUNET_DISK_file_close (fh);
424 return GNUNET_OK; 430 return GNUNET_OK;
425} 431}
@@ -469,15 +475,15 @@ list_pseudonym_helper (void *cls, const char *fullname)
469 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) 475 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
470 return GNUNET_OK; 476 return GNUNET_OK;
471 fn = &fullname[strlen (fullname) + 1 - 477 fn = &fullname[strlen (fullname) + 1 -
472 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; 478 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
473 if (fn[-1] != DIR_SEPARATOR) 479 if (fn[-1] != DIR_SEPARATOR)
474 return GNUNET_OK; 480 return GNUNET_OK;
475 ret = GNUNET_OK; 481 ret = GNUNET_OK;
476 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id)) 482 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
477 return GNUNET_OK; /* invalid name */ 483 return GNUNET_OK; /* invalid name */
478 str = NULL; 484 str = NULL;
479 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str)) 485 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str))
480 return GNUNET_OK; /* ignore entry */ 486 return GNUNET_OK; /* ignore entry */
481 GNUNET_free_non_null (str); 487 GNUNET_free_non_null (str);
482 if (c->iterator != NULL) 488 if (c->iterator != NULL)
483 ret = c->iterator (c->closure, &id, meta, rating); 489 ret = c->iterator (c->closure, &id, meta, rating);
@@ -496,7 +502,7 @@ list_pseudonym_helper (void *cls, const char *fullname)
496 */ 502 */
497int 503int
498GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg, 504GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
499 GNUNET_PSEUDONYM_Iterator iterator, void *closure) 505 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
500{ 506{
501 struct ListPseudonymClosure cls; 507 struct ListPseudonymClosure cls;
502 char *fn; 508 char *fn;
@@ -525,7 +531,7 @@ GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
525 */ 531 */
526int 532int
527GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg, 533GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
528 const GNUNET_HashCode * nsid, int delta) 534 const GNUNET_HashCode * nsid, int delta)
529{ 535{
530 struct GNUNET_CONTAINER_MetaData *meta; 536 struct GNUNET_CONTAINER_MetaData *meta;
531 int ret; 537 int ret;
@@ -535,10 +541,10 @@ GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
535 name = NULL; 541 name = NULL;
536 ret = read_info (cfg, nsid, &meta, &ranking, &name); 542 ret = read_info (cfg, nsid, &meta, &ranking, &name);
537 if (ret == GNUNET_SYSERR) 543 if (ret == GNUNET_SYSERR)
538 { 544 {
539 ranking = 0; 545 ranking = 0;
540 meta = GNUNET_CONTAINER_meta_data_create (); 546 meta = GNUNET_CONTAINER_meta_data_create ();
541 } 547 }
542 ranking += delta; 548 ranking += delta;
543 write_pseudonym_info (cfg, nsid, meta, ranking, name); 549 write_pseudonym_info (cfg, nsid, meta, ranking, name);
544 GNUNET_CONTAINER_meta_data_destroy (meta); 550 GNUNET_CONTAINER_meta_data_destroy (meta);
@@ -558,8 +564,8 @@ GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
558 */ 564 */
559void 565void
560GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg, 566GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
561 const GNUNET_HashCode * id, 567 const GNUNET_HashCode * id,
562 const struct GNUNET_CONTAINER_MetaData *meta) 568 const struct GNUNET_CONTAINER_MetaData *meta)
563{ 569{
564 char *name; 570 char *name;
565 int32_t ranking; 571 int32_t ranking;
@@ -573,16 +579,16 @@ GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
573 579
574 if ((0 == STAT (fn, &sbuf)) && 580 if ((0 == STAT (fn, &sbuf)) &&
575 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name))) 581 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
576 { 582 {
577 GNUNET_CONTAINER_meta_data_merge (old, meta); 583 GNUNET_CONTAINER_meta_data_merge (old, meta);
578 write_pseudonym_info (cfg, id, old, ranking, name); 584 write_pseudonym_info (cfg, id, old, ranking, name);
579 GNUNET_CONTAINER_meta_data_destroy (old); 585 GNUNET_CONTAINER_meta_data_destroy (old);
580 GNUNET_free_non_null (name); 586 GNUNET_free_non_null (name);
581 } 587 }
582 else 588 else
583 { 589 {
584 write_pseudonym_info (cfg, id, meta, ranking, NULL); 590 write_pseudonym_info (cfg, id, meta, ranking, NULL);
585 } 591 }
586 GNUNET_free (fn); 592 GNUNET_free (fn);
587 internal_notify (id, meta, ranking); 593 internal_notify (id, meta, ranking);
588} 594}
diff --git a/src/util/resolver_api.c b/src/util/resolver_api.c
index 913769ae1..3bdc40143 100644
--- a/src/util/resolver_api.c
+++ b/src/util/resolver_api.c
@@ -35,6 +35,8 @@
35 35
36#define LOG(kind,...) GNUNET_log_from (kind, "resolver-api", __VA_ARGS__) 36#define LOG(kind,...) GNUNET_log_from (kind, "resolver-api", __VA_ARGS__)
37 37
38#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "resolver-api", syscall)
39
38/** 40/**
39 * Maximum supported length for a hostname 41 * Maximum supported length for a hostname
40 */ 42 */
@@ -194,26 +196,26 @@ check_config ()
194#endif 196#endif
195 if (GNUNET_OK != 197 if (GNUNET_OK !=
196 GNUNET_CONFIGURATION_get_value_string (resolver_cfg, "resolver", 198 GNUNET_CONFIGURATION_get_value_string (resolver_cfg, "resolver",
197 "HOSTNAME", &hostname)) 199 "HOSTNAME", &hostname))
198 { 200 {
199 LOG (GNUNET_ERROR_TYPE_ERROR, 201 LOG (GNUNET_ERROR_TYPE_ERROR,
200 _("Must specify `%s' for `%s' in configuration!\n"), "HOSTNAME", 202 _("Must specify `%s' for `%s' in configuration!\n"), "HOSTNAME",
201 "resolver"); 203 "resolver");
202 GNUNET_assert (0); 204 GNUNET_assert (0);
203 } 205 }
204 if ((1 != inet_pton (AF_INET, hostname, &v4)) || 206 if ((1 != inet_pton (AF_INET, hostname, &v4)) ||
205 (1 != inet_pton (AF_INET6, hostname, &v6))) 207 (1 != inet_pton (AF_INET6, hostname, &v6)))
206 {
207 GNUNET_free (hostname);
208 return;
209 }
210 i = 0;
211 while (loopback[i] != NULL)
212 if (0 == strcasecmp (loopback[i++], hostname))
213 { 208 {
214 GNUNET_free (hostname); 209 GNUNET_free (hostname);
215 return; 210 return;
216 } 211 }
212 i = 0;
213 while (loopback[i] != NULL)
214 if (0 == strcasecmp (loopback[i++], hostname))
215 {
216 GNUNET_free (hostname);
217 return;
218 }
217 LOG (GNUNET_ERROR_TYPE_ERROR, 219 LOG (GNUNET_ERROR_TYPE_ERROR,
218 _ 220 _
219 ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"), 221 ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
@@ -247,23 +249,23 @@ GNUNET_RESOLVER_disconnect ()
247 GNUNET_assert (NULL == req_head); 249 GNUNET_assert (NULL == req_head);
248 GNUNET_assert (NULL == req_tail); 250 GNUNET_assert (NULL == req_tail);
249 if (NULL != client) 251 if (NULL != client)
250 { 252 {
251#if DEBUG_RESOLVER 253#if DEBUG_RESOLVER
252 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n"); 254 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n");
253#endif 255#endif
254 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 256 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
255 client = NULL; 257 client = NULL;
256 } 258 }
257 if (r_task != GNUNET_SCHEDULER_NO_TASK) 259 if (r_task != GNUNET_SCHEDULER_NO_TASK)
258 { 260 {
259 GNUNET_SCHEDULER_cancel (r_task); 261 GNUNET_SCHEDULER_cancel (r_task);
260 r_task = GNUNET_SCHEDULER_NO_TASK; 262 r_task = GNUNET_SCHEDULER_NO_TASK;
261 } 263 }
262 if (s_task != GNUNET_SCHEDULER_NO_TASK) 264 if (s_task != GNUNET_SCHEDULER_NO_TASK)
263 { 265 {
264 GNUNET_SCHEDULER_cancel (s_task); 266 GNUNET_SCHEDULER_cancel (s_task);
265 s_task = GNUNET_SCHEDULER_NO_TASK; 267 s_task = GNUNET_SCHEDULER_NO_TASK;
266 } 268 }
267} 269}
268 270
269 271
@@ -284,37 +286,35 @@ no_resolve (const struct sockaddr *sa, socklen_t salen)
284 if (salen < sizeof (struct sockaddr)) 286 if (salen < sizeof (struct sockaddr))
285 return NULL; 287 return NULL;
286 switch (sa->sa_family) 288 switch (sa->sa_family)
287 {
288 case AF_INET:
289 if (salen != sizeof (struct sockaddr_in))
290 return NULL;
291 if (NULL ==
292 inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr, inet4,
293 INET_ADDRSTRLEN))
294 { 289 {
295 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "resolver-api", 290 case AF_INET:
296 "inet_ntop"); 291 if (salen != sizeof (struct sockaddr_in))
297 return NULL; 292 return NULL;
298 } 293 if (NULL ==
299 ret = GNUNET_strdup (inet4); 294 inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr, inet4,
300 break; 295 INET_ADDRSTRLEN))
301 case AF_INET6: 296 {
302 if (salen != sizeof (struct sockaddr_in6)) 297 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
303 return NULL; 298 return NULL;
304 if (NULL == 299 }
305 inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr, inet6, 300 ret = GNUNET_strdup (inet4);
306 INET6_ADDRSTRLEN)) 301 break;
307 { 302 case AF_INET6:
308 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "resolver-api", 303 if (salen != sizeof (struct sockaddr_in6))
309 "inet_ntop"); 304 return NULL;
310 return NULL; 305 if (NULL ==
306 inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr,
307 inet6, INET6_ADDRSTRLEN))
308 {
309 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
310 return NULL;
311 }
312 ret = GNUNET_strdup (inet6);
313 break;
314 default:
315 ret = NULL;
316 break;
311 } 317 }
312 ret = GNUNET_strdup (inet6);
313 break;
314 default:
315 ret = NULL;
316 break;
317 }
318 return ret; 318 return ret;
319} 319}
320 320
@@ -322,15 +322,13 @@ no_resolve (const struct sockaddr *sa, socklen_t salen)
322/** 322/**
323 * Adjust exponential back-off and reconnect to the service. 323 * Adjust exponential back-off and reconnect to the service.
324 */ 324 */
325static void 325static void reconnect ();
326reconnect ();
327 326
328 327
329/** 328/**
330 * Process pending requests to the resolver. 329 * Process pending requests to the resolver.
331 */ 330 */
332static void 331static void process_requests ();
333process_requests ();
334 332
335 333
336/** 334/**
@@ -352,73 +350,32 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
352 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving response from DNS service\n"); 350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving response from DNS service\n");
353#endif 351#endif
354 if (msg == NULL) 352 if (msg == NULL)
355 {
356 if (NULL != rh->name_callback)
357 LOG (GNUNET_ERROR_TYPE_INFO,
358 _("Timeout trying to resolve IP address `%s'.\n"),
359 GNUNET_a2s ((const void *) &rh[1], rh->data_len));
360 else
361 LOG (GNUNET_ERROR_TYPE_INFO,
362 _("Timeout trying to resolve hostname `%s'.\n"),
363 (const char *) &rh[1]);
364 /* check if request was canceled */
365 if (rh->was_transmitted != GNUNET_SYSERR)
366 {
367 if (NULL != rh->name_callback)
368 {
369 /* no reverse lookup was successful, return ip as string */
370 if (rh->received_response == GNUNET_NO)
371 rh->name_callback (rh->cls,
372 no_resolve ((const struct sockaddr *) &rh[1],
373 rh->data_len));
374 /* at least one reverse lookup was successful */
375 else
376 rh->name_callback (rh->cls, NULL);
377 }
378 if (NULL != rh->addr_callback)
379 rh->addr_callback (rh->cls, NULL, 0);
380 }
381 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
382 GNUNET_free (rh);
383 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
384 client = NULL;
385 reconnect ();
386 return;
387 }
388 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
389 {
390 GNUNET_break (0);
391 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
392 client = NULL;
393 reconnect ();
394 return;
395 }
396 size = ntohs (msg->size);
397 /* message contains not data, just header */
398 if (size == sizeof (struct GNUNET_MessageHeader))
399 {
400 /* check if request was canceled */
401 if (rh->was_transmitted != GNUNET_SYSERR)
402 { 353 {
403 if (NULL != rh->name_callback) 354 if (NULL != rh->name_callback)
404 rh->name_callback (rh->cls, NULL); 355 LOG (GNUNET_ERROR_TYPE_INFO,
405 if (NULL != rh->addr_callback) 356 _("Timeout trying to resolve IP address `%s'.\n"),
406 rh->addr_callback (rh->cls, NULL, 0); 357 GNUNET_a2s ((const void *) &rh[1], rh->data_len));
407 } 358 else
408 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); 359 LOG (GNUNET_ERROR_TYPE_INFO,
409 GNUNET_free (rh); 360 _("Timeout trying to resolve hostname `%s'.\n"),
410 process_requests (); 361 (const char *) &rh[1]);
411 return; 362 /* check if request was canceled */
412 }
413 /* return reverse lookup results to caller */
414 if (NULL != rh->name_callback)
415 {
416 hostname = (const char *) &msg[1];
417 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
418 {
419 GNUNET_break (0);
420 if (rh->was_transmitted != GNUNET_SYSERR) 363 if (rh->was_transmitted != GNUNET_SYSERR)
421 rh->name_callback (rh->cls, NULL); 364 {
365 if (NULL != rh->name_callback)
366 {
367 /* no reverse lookup was successful, return ip as string */
368 if (rh->received_response == GNUNET_NO)
369 rh->name_callback (rh->cls,
370 no_resolve ((const struct sockaddr *)
371 &rh[1], rh->data_len));
372 /* at least one reverse lookup was successful */
373 else
374 rh->name_callback (rh->cls, NULL);
375 }
376 if (NULL != rh->addr_callback)
377 rh->addr_callback (rh->cls, NULL, 0);
378 }
422 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); 379 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
423 GNUNET_free (rh); 380 GNUNET_free (rh);
424 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 381 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
@@ -426,46 +383,89 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
426 reconnect (); 383 reconnect ();
427 return; 384 return;
428 } 385 }
429#if DEBUG_RESOLVER 386 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
430 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s' for IP `%s'.\n"),
431 hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len));
432#endif
433 if (rh->was_transmitted != GNUNET_SYSERR)
434 rh->name_callback (rh->cls, hostname);
435 rh->received_response = GNUNET_YES;
436 GNUNET_CLIENT_receive (client, &handle_response, rh,
437 GNUNET_TIME_absolute_get_remaining (rh->timeout));
438 }
439 /* return lookup results to caller */
440 if (NULL != rh->addr_callback)
441 {
442 sa = (const struct sockaddr *) &msg[1];
443 salen = size - sizeof (struct GNUNET_MessageHeader);
444 if (salen < sizeof (struct sockaddr))
445 { 387 {
446 GNUNET_break (0); 388 GNUNET_break (0);
447 if (rh->was_transmitted != GNUNET_SYSERR)
448 rh->addr_callback (rh->cls, NULL, 0);
449 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
450 GNUNET_free (rh);
451 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 389 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
452 client = NULL; 390 client = NULL;
453 reconnect (); 391 reconnect ();
454 return; 392 return;
455 } 393 }
394 size = ntohs (msg->size);
395 /* message contains not data, just header */
396 if (size == sizeof (struct GNUNET_MessageHeader))
397 {
398 /* check if request was canceled */
399 if (rh->was_transmitted != GNUNET_SYSERR)
400 {
401 if (NULL != rh->name_callback)
402 rh->name_callback (rh->cls, NULL);
403 if (NULL != rh->addr_callback)
404 rh->addr_callback (rh->cls, NULL, 0);
405 }
406 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
407 GNUNET_free (rh);
408 process_requests ();
409 return;
410 }
411 /* return reverse lookup results to caller */
412 if (NULL != rh->name_callback)
413 {
414 hostname = (const char *) &msg[1];
415 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
416 {
417 GNUNET_break (0);
418 if (rh->was_transmitted != GNUNET_SYSERR)
419 rh->name_callback (rh->cls, NULL);
420 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
421 GNUNET_free (rh);
422 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
423 client = NULL;
424 reconnect ();
425 return;
426 }
456#if DEBUG_RESOLVER 427#if DEBUG_RESOLVER
428 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s' for IP `%s'.\n"),
429 hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len));
430#endif
431 if (rh->was_transmitted != GNUNET_SYSERR)
432 rh->name_callback (rh->cls, hostname);
433 rh->received_response = GNUNET_YES;
434 GNUNET_CLIENT_receive (client, &handle_response, rh,
435 GNUNET_TIME_absolute_get_remaining
436 (rh->timeout));
437 }
438 /* return lookup results to caller */
439 if (NULL != rh->addr_callback)
457 { 440 {
458 char *ips = no_resolve (sa, salen); 441 sa = (const struct sockaddr *) &msg[1];
442 salen = size - sizeof (struct GNUNET_MessageHeader);
443 if (salen < sizeof (struct sockaddr))
444 {
445 GNUNET_break (0);
446 if (rh->was_transmitted != GNUNET_SYSERR)
447 rh->addr_callback (rh->cls, NULL, 0);
448 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
449 GNUNET_free (rh);
450 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
451 client = NULL;
452 reconnect ();
453 return;
454 }
455#if DEBUG_RESOLVER
456 {
457 char *ips = no_resolve (sa, salen);
459 458
460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s' for `%s'.\n", ips, 459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s' for `%s'.\n",
461 (const char *) &rh[1]); 460 ips, (const char *) &rh[1]);
462 GNUNET_free (ips); 461 GNUNET_free (ips);
463 } 462 }
464#endif 463#endif
465 rh->addr_callback (rh->cls, sa, salen); 464 rh->addr_callback (rh->cls, sa, salen);
466 GNUNET_CLIENT_receive (client, &handle_response, rh, 465 GNUNET_CLIENT_receive (client, &handle_response, rh,
467 GNUNET_TIME_absolute_get_remaining (rh->timeout)); 466 GNUNET_TIME_absolute_get_remaining
468 } 467 (rh->timeout));
468 }
469} 469}
470 470
471 471
@@ -498,26 +498,27 @@ numeric_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
498 hostname = (const char *) &rh[1]; 498 hostname = (const char *) &rh[1];
499 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET)) && 499 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET)) &&
500 (1 == inet_pton (AF_INET, hostname, &v4.sin_addr))) 500 (1 == inet_pton (AF_INET, hostname, &v4.sin_addr)))
501 {
502 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
503 if ((rh->domain == AF_UNSPEC) &&
504 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr)))
505 { 501 {
506 /* this can happen on some systems IF "hostname" is "localhost" */ 502 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
507 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); 503 if ((rh->domain == AF_UNSPEC) &&
504 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr)))
505 {
506 /* this can happen on some systems IF "hostname" is "localhost" */
507 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6,
508 sizeof (v6));
509 }
510 rh->addr_callback (rh->cls, NULL, 0);
511 GNUNET_free (rh);
512 return;
508 } 513 }
509 rh->addr_callback (rh->cls, NULL, 0);
510 GNUNET_free (rh);
511 return;
512 }
513 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) && 514 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) &&
514 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr))) 515 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr)))
515 { 516 {
516 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); 517 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
517 rh->addr_callback (rh->cls, NULL, 0); 518 rh->addr_callback (rh->cls, NULL, 0);
518 GNUNET_free (rh); 519 GNUNET_free (rh);
519 return; 520 return;
520 } 521 }
521 /* why are we here? this task should not have been scheduled! */ 522 /* why are we here? this task should not have been scheduled! */
522 GNUNET_assert (0); 523 GNUNET_assert (0);
523 GNUNET_free (rh); 524 GNUNET_free (rh);
@@ -552,21 +553,21 @@ loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
552#endif 553#endif
553 v6.sin6_addr = in6addr_loopback; 554 v6.sin6_addr = in6addr_loopback;
554 switch (rh->domain) 555 switch (rh->domain)
555 { 556 {
556 case AF_INET: 557 case AF_INET:
557 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); 558 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
558 break; 559 break;
559 case AF_INET6: 560 case AF_INET6:
560 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); 561 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
561 break; 562 break;
562 case AF_UNSPEC: 563 case AF_UNSPEC:
563 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); 564 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
564 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); 565 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
565 break; 566 break;
566 default: 567 default:
567 GNUNET_break (0); 568 GNUNET_break (0);
568 break; 569 break;
569 } 570 }
570 rh->addr_callback (rh->cls, NULL, 0); 571 rh->addr_callback (rh->cls, NULL, 0);
571 GNUNET_free (rh); 572 GNUNET_free (rh);
572} 573}
@@ -594,25 +595,25 @@ process_requests ()
594 struct GNUNET_RESOLVER_RequestHandle *rh; 595 struct GNUNET_RESOLVER_RequestHandle *rh;
595 596
596 if (NULL == client) 597 if (NULL == client)
597 { 598 {
598 reconnect (); 599 reconnect ();
599 return; 600 return;
600 } 601 }
601 rh = req_head; 602 rh = req_head;
602 if (NULL == rh) 603 if (NULL == rh)
603 { 604 {
604 /* nothing to do, release socket really soon if there is nothing 605 /* nothing to do, release socket really soon if there is nothing
605 * else happening... */ 606 * else happening... */
606 s_task = 607 s_task =
607 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, 608 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
608 &shutdown_task, NULL); 609 &shutdown_task, NULL);
609 return; 610 return;
610 } 611 }
611 if (GNUNET_YES == rh->was_transmitted) 612 if (GNUNET_YES == rh->was_transmitted)
612 return; /* waiting for reply */ 613 return; /* waiting for reply */
613 msg = (struct GNUNET_RESOLVER_GetMessage *) buf; 614 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
614 msg->header.size = 615 msg->header.size =
615 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + rh->data_len); 616 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + rh->data_len);
616 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST); 617 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
617 msg->direction = htonl (rh->direction); 618 msg->direction = htonl (rh->direction);
618 msg->domain = htonl (rh->domain); 619 msg->domain = htonl (rh->domain);
@@ -623,15 +624,15 @@ process_requests ()
623#endif 624#endif
624 if (GNUNET_OK != 625 if (GNUNET_OK !=
625 GNUNET_CLIENT_transmit_and_get_response (client, &msg->header, 626 GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
626 GNUNET_TIME_absolute_get_remaining 627 GNUNET_TIME_absolute_get_remaining
627 (rh->timeout), GNUNET_YES, 628 (rh->timeout), GNUNET_YES,
628 &handle_response, rh)) 629 &handle_response, rh))
629 { 630 {
630 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 631 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
631 client = NULL; 632 client = NULL;
632 reconnect (); 633 reconnect ();
633 return; 634 return;
634 } 635 }
635 rh->was_transmitted = GNUNET_YES; 636 rh->was_transmitted = GNUNET_YES;
636} 637}
637 638
@@ -647,7 +648,7 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
647{ 648{
648 r_task = GNUNET_SCHEDULER_NO_TASK; 649 r_task = GNUNET_SCHEDULER_NO_TASK;
649 if (NULL == req_head) 650 if (NULL == req_head)
650 return; /* no work pending */ 651 return; /* no work pending */
651 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 652 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
652 return; 653 return;
653#if DEBUG_RESOLVER 654#if DEBUG_RESOLVER
@@ -655,11 +656,12 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
655#endif 656#endif
656 client = GNUNET_CLIENT_connect ("resolver", resolver_cfg); 657 client = GNUNET_CLIENT_connect ("resolver", resolver_cfg);
657 if (NULL == client) 658 if (NULL == client)
658 { 659 {
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect, will try again later\n"); 660 LOG (GNUNET_ERROR_TYPE_DEBUG,
660 reconnect (); 661 "Failed to connect, will try again later\n");
661 return; 662 reconnect ();
662 } 663 return;
664 }
663 process_requests (); 665 process_requests ();
664} 666}
665 667
@@ -676,26 +678,26 @@ reconnect ()
676 return; 678 return;
677 GNUNET_assert (NULL == client); 679 GNUNET_assert (NULL == client);
678 if (NULL != (rh = req_head)) 680 if (NULL != (rh = req_head))
679 {
680 switch (rh->was_transmitted)
681 { 681 {
682 case GNUNET_NO: 682 switch (rh->was_transmitted)
683 /* nothing more to do */ 683 {
684 break; 684 case GNUNET_NO:
685 case GNUNET_YES: 685 /* nothing more to do */
686 /* disconnected, transmit again! */ 686 break;
687 rh->was_transmitted = GNUNET_NO; 687 case GNUNET_YES:
688 break; 688 /* disconnected, transmit again! */
689 case GNUNET_SYSERR: 689 rh->was_transmitted = GNUNET_NO;
690 /* request was cancelled, remove entirely */ 690 break;
691 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); 691 case GNUNET_SYSERR:
692 GNUNET_free (rh); 692 /* request was cancelled, remove entirely */
693 break; 693 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
694 default: 694 GNUNET_free (rh);
695 GNUNET_assert (0); 695 break;
696 break; 696 default:
697 GNUNET_assert (0);
698 break;
699 }
697 } 700 }
698 }
699#if DEBUG_RESOLVER 701#if DEBUG_RESOLVER
700 LOG (GNUNET_ERROR_TYPE_DEBUG, 702 LOG (GNUNET_ERROR_TYPE_DEBUG,
701 "Will try to connect to DNS service in %llu ms\n", 703 "Will try to connect to DNS service in %llu ms\n",
@@ -719,9 +721,9 @@ reconnect ()
719 */ 721 */
720struct GNUNET_RESOLVER_RequestHandle * 722struct GNUNET_RESOLVER_RequestHandle *
721GNUNET_RESOLVER_ip_get (const char *hostname, int domain, 723GNUNET_RESOLVER_ip_get (const char *hostname, int domain,
722 struct GNUNET_TIME_Relative timeout, 724 struct GNUNET_TIME_Relative timeout,
723 GNUNET_RESOLVER_AddressCallback callback, 725 GNUNET_RESOLVER_AddressCallback callback,
724 void *callback_cls) 726 void *callback_cls)
725{ 727{
726 struct GNUNET_RESOLVER_RequestHandle *rh; 728 struct GNUNET_RESOLVER_RequestHandle *rh;
727 size_t slen; 729 size_t slen;
@@ -732,10 +734,10 @@ GNUNET_RESOLVER_ip_get (const char *hostname, int domain,
732 slen = strlen (hostname) + 1; 734 slen = strlen (hostname) + 1;
733 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= 735 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
734 GNUNET_SERVER_MAX_MESSAGE_SIZE) 736 GNUNET_SERVER_MAX_MESSAGE_SIZE)
735 { 737 {
736 GNUNET_break (0); 738 GNUNET_break (0);
737 return NULL; 739 return NULL;
738 } 740 }
739 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen); 741 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
740 rh->domain = domain; 742 rh->domain = domain;
741 rh->addr_callback = callback; 743 rh->addr_callback = callback;
@@ -749,25 +751,25 @@ GNUNET_RESOLVER_ip_get (const char *hostname, int domain,
749 ((domain == AF_INET) || (domain == AF_UNSPEC))) || 751 ((domain == AF_INET) || (domain == AF_UNSPEC))) ||
750 ((1 == inet_pton (AF_INET6, hostname, &v6)) && 752 ((1 == inet_pton (AF_INET6, hostname, &v6)) &&
751 ((domain == AF_INET6) || (domain == AF_UNSPEC)))) 753 ((domain == AF_INET6) || (domain == AF_UNSPEC))))
752 { 754 {
753 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, rh); 755 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, rh);
754 return rh; 756 return rh;
755 } 757 }
756 /* then, check if this is a loopback address */ 758 /* then, check if this is a loopback address */
757 i = 0; 759 i = 0;
758 while (loopback[i] != NULL) 760 while (loopback[i] != NULL)
759 if (0 == strcasecmp (loopback[i++], hostname)) 761 if (0 == strcasecmp (loopback[i++], hostname))
760 { 762 {
761 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh); 763 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh);
762 return rh; 764 return rh;
763 } 765 }
764 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh); 766 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh);
765 rh->was_queued = GNUNET_YES; 767 rh->was_queued = GNUNET_YES;
766 if (s_task != GNUNET_SCHEDULER_NO_TASK) 768 if (s_task != GNUNET_SCHEDULER_NO_TASK)
767 { 769 {
768 GNUNET_SCHEDULER_cancel (s_task); 770 GNUNET_SCHEDULER_cancel (s_task);
769 s_task = GNUNET_SCHEDULER_NO_TASK; 771 s_task = GNUNET_SCHEDULER_NO_TASK;
770 } 772 }
771 process_requests (); 773 process_requests ();
772 return rh; 774 return rh;
773} 775}
@@ -791,10 +793,10 @@ numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
791 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result); 793 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result);
792#endif 794#endif
793 if (result != NULL) 795 if (result != NULL)
794 { 796 {
795 rh->name_callback (rh->cls, result); 797 rh->name_callback (rh->cls, result);
796 GNUNET_free (result); 798 GNUNET_free (result);
797 } 799 }
798 rh->name_callback (rh->cls, NULL); 800 rh->name_callback (rh->cls, NULL);
799 GNUNET_free (rh); 801 GNUNET_free (rh);
800} 802}
@@ -814,10 +816,10 @@ numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
814 */ 816 */
815struct GNUNET_RESOLVER_RequestHandle * 817struct GNUNET_RESOLVER_RequestHandle *
816GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen, 818GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen,
817 int do_resolve, 819 int do_resolve,
818 struct GNUNET_TIME_Relative timeout, 820 struct GNUNET_TIME_Relative timeout,
819 GNUNET_RESOLVER_HostnameCallback callback, 821 GNUNET_RESOLVER_HostnameCallback callback,
820 void *cls) 822 void *cls)
821{ 823{
822 struct GNUNET_RESOLVER_RequestHandle *rh; 824 struct GNUNET_RESOLVER_RequestHandle *rh;
823 825
@@ -831,24 +833,24 @@ GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen,
831 rh->direction = GNUNET_YES; 833 rh->direction = GNUNET_YES;
832 rh->received_response = GNUNET_NO; 834 rh->received_response = GNUNET_NO;
833 if (GNUNET_NO == do_resolve) 835 if (GNUNET_NO == do_resolve)
834 { 836 {
835 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh); 837 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh);
836 return rh; 838 return rh;
837 } 839 }
838 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= 840 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
839 GNUNET_SERVER_MAX_MESSAGE_SIZE) 841 GNUNET_SERVER_MAX_MESSAGE_SIZE)
840 { 842 {
841 GNUNET_break (0); 843 GNUNET_break (0);
842 GNUNET_free (rh); 844 GNUNET_free (rh);
843 return NULL; 845 return NULL;
844 } 846 }
845 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh); 847 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh);
846 rh->was_queued = GNUNET_YES; 848 rh->was_queued = GNUNET_YES;
847 if (s_task != GNUNET_SCHEDULER_NO_TASK) 849 if (s_task != GNUNET_SCHEDULER_NO_TASK)
848 { 850 {
849 GNUNET_SCHEDULER_cancel (s_task); 851 GNUNET_SCHEDULER_cancel (s_task);
850 s_task = GNUNET_SCHEDULER_NO_TASK; 852 s_task = GNUNET_SCHEDULER_NO_TASK;
851 } 853 }
852 process_requests (); 854 process_requests ();
853 return rh; 855 return rh;
854} 856}
@@ -866,21 +868,21 @@ GNUNET_RESOLVER_local_fqdn_get ()
866 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 868 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
867 869
868 if (0 != gethostname (hostname, sizeof (hostname) - 1)) 870 if (0 != gethostname (hostname, sizeof (hostname) - 1))
869 { 871 {
870 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 872 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
871 "resolver-api", "gethostname"); 873 "gethostname");
872 return NULL; 874 return NULL;
873 } 875 }
874#if DEBUG_RESOLVER 876#if DEBUG_RESOLVER
875 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), hostname); 877 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), hostname);
876#endif 878#endif
877 host = gethostbyname (hostname); 879 host = gethostbyname (hostname);
878 if (NULL == host) 880 if (NULL == host)
879 { 881 {
880 LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not resolve our FQDN : %s\n"), 882 LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not resolve our FQDN : %s\n"),
881 hstrerror (h_errno)); 883 hstrerror (h_errno));
882 return NULL; 884 return NULL;
883 } 885 }
884 return GNUNET_strdup (host->h_name); 886 return GNUNET_strdup (host->h_name);
885} 887}
886 888
@@ -896,18 +898,18 @@ GNUNET_RESOLVER_local_fqdn_get ()
896 */ 898 */
897struct GNUNET_RESOLVER_RequestHandle * 899struct GNUNET_RESOLVER_RequestHandle *
898GNUNET_RESOLVER_hostname_resolve (int domain, 900GNUNET_RESOLVER_hostname_resolve (int domain,
899 struct GNUNET_TIME_Relative timeout, 901 struct GNUNET_TIME_Relative timeout,
900 GNUNET_RESOLVER_AddressCallback callback, 902 GNUNET_RESOLVER_AddressCallback callback,
901 void *cls) 903 void *cls)
902{ 904{
903 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 905 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
904 906
905 if (0 != gethostname (hostname, sizeof (hostname) - 1)) 907 if (0 != gethostname (hostname, sizeof (hostname) - 1))
906 { 908 {
907 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 909 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
908 "resolver-api", "gethostname"); 910 "gethostname");
909 return NULL; 911 return NULL;
910 } 912 }
911#if DEBUG_RESOLVER 913#if DEBUG_RESOLVER
912 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our hostname `%s'\n"), hostname); 914 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our hostname `%s'\n"), hostname);
913#endif 915#endif
@@ -927,19 +929,19 @@ void
927GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh) 929GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
928{ 930{
929 if (rh->task != GNUNET_SCHEDULER_NO_TASK) 931 if (rh->task != GNUNET_SCHEDULER_NO_TASK)
930 { 932 {
931 GNUNET_SCHEDULER_cancel (rh->task); 933 GNUNET_SCHEDULER_cancel (rh->task);
932 rh->task = GNUNET_SCHEDULER_NO_TASK; 934 rh->task = GNUNET_SCHEDULER_NO_TASK;
933 } 935 }
934 if (rh->was_transmitted == GNUNET_NO) 936 if (rh->was_transmitted == GNUNET_NO)
935 { 937 {
936 if (rh->was_queued == GNUNET_YES) 938 if (rh->was_queued == GNUNET_YES)
937 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); 939 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
938 GNUNET_free (rh); 940 GNUNET_free (rh);
939 return; 941 return;
940 } 942 }
941 GNUNET_assert (rh->was_transmitted == GNUNET_YES); 943 GNUNET_assert (rh->was_transmitted == GNUNET_YES);
942 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */ 944 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
943} 945}
944 946
945 947
diff --git a/src/util/scheduler.c b/src/util/scheduler.c
index b7860650e..d40853963 100644
--- a/src/util/scheduler.c
+++ b/src/util/scheduler.c
@@ -34,6 +34,10 @@
34#include "execinfo.h" 34#include "execinfo.h"
35 35
36 36
37#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38
39#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
40
37/** 41/**
38 * Use lsof to generate file descriptor reports on select error? 42 * Use lsof to generate file descriptor reports on select error?
39 * (turn off for stable releases). 43 * (turn off for stable releases).
@@ -523,10 +527,9 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs,
523 while (pos != NULL) 527 while (pos != NULL)
524 { 528 {
525#if DEBUG_TASKS 529#if DEBUG_TASKS
526 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 530 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 "util", 531 "Checking readiness of task: %llu / %p\n", pos->id,
528 "Checking readiness of task: %llu / %p\n", pos->id, 532 pos->callback_cls);
529 pos->callback_cls);
530#endif 533#endif
531 next = pos->next; 534 next = pos->next;
532 if (GNUNET_YES == is_ready (pos, now, rs, ws)) 535 if (GNUNET_YES == is_ready (pos, now, rs, ws))
@@ -656,12 +659,10 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
656 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value > 659 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
657 DELAY_THRESHOLD.rel_value) 660 DELAY_THRESHOLD.rel_value)
658 { 661 {
659 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 662 LOG (GNUNET_ERROR_TYPE_ERROR,
660 "util", 663 "Task %llu took %llums to be scheduled\n", pos->id,
661 "Task %llu took %llums to be scheduled\n", pos->id, 664 (unsigned long long)
662 (unsigned long long) 665 GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
663 GNUNET_TIME_absolute_get_duration
664 (pos->start_time).rel_value);
665 } 666 }
666#endif 667#endif
667 tc.reason = pos->reason; 668 tc.reason = pos->reason;
@@ -678,20 +679,17 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
678 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd))) 679 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
679 abort (); // added to ready in previous select loop! 680 abort (); // added to ready in previous select loop!
680#if DEBUG_TASKS 681#if DEBUG_TASKS
681 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 682 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 "util", 683 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
683 "Running task: %llu / %p\n", pos->id,
684 pos->callback_cls);
685#endif 684#endif
686 pos->callback (pos->callback_cls, &tc); 685 pos->callback (pos->callback_cls, &tc);
687#if EXECINFO 686#if EXECINFO
688 int i; 687 int i;
689 688
690 for (i = 0; i < pos->num_backtrace_strings; i++) 689 for (i = 0; i < pos->num_backtrace_strings; i++)
691 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 690 LOG (GNUNET_ERROR_TYPE_ERROR,
692 "util", 691 "Task %llu trace %d: %s\n", pos->id,
693 "Task %llu trace %d: %s\n", pos->id, 692 i, pos->backtrace_strings[i]);
694 i, pos->backtrace_strings[i]);
695#endif 693#endif
696 active_task = NULL; 694 active_task = NULL;
697 destroy_task (pos); 695 destroy_task (pos);
@@ -844,8 +842,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
844 if (errno == EINTR) 842 if (errno == EINTR)
845 continue; 843 continue;
846 844
847 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "util", 845 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
848 "select");
849#ifndef MINGW 846#ifndef MINGW
850#if USE_LSOF 847#if USE_LSOF
851 char lsof[512]; 848 char lsof[512];
@@ -854,8 +851,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
854 (void) close (1); 851 (void) close (1);
855 (void) dup2 (2, 1); 852 (void) dup2 (2, 1);
856 if (0 != system (lsof)) 853 if (0 != system (lsof))
857 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "util", 854 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
858 "system");
859#endif 855#endif
860#endif 856#endif
861 abort (); 857 abort ();
@@ -863,8 +859,8 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
863 } 859 }
864 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16)) 860 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
865 { 861 {
866 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 862 LOG (GNUNET_ERROR_TYPE_WARNING,
867 "util", _("Looks like we're busy waiting...\n")); 863 _("Looks like we're busy waiting...\n"));
868 sleep (1); /* mitigate */ 864 sleep (1); /* mitigate */
869 } 865 }
870 check_ready (rs, ws); 866 check_ready (rs, ws);
@@ -994,10 +990,9 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
994 p++; 990 p++;
995 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT) 991 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
996 { 992 {
997 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 993 LOG (GNUNET_ERROR_TYPE_ERROR,
998 "util", 994 _("Attempt to cancel dead task %llu!\n"),
999 _("Attempt to cancel dead task %llu!\n"), 995 (unsigned long long) task);
1000 (unsigned long long) task);
1001 GNUNET_assert (0); 996 GNUNET_assert (0);
1002 } 997 }
1003 prev = NULL; 998 prev = NULL;
@@ -1037,9 +1032,8 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
1037 } 1032 }
1038 ret = t->callback_cls; 1033 ret = t->callback_cls;
1039#if DEBUG_TASKS 1034#if DEBUG_TASKS
1040 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "util", 1036 "Canceling task: %llu / %p\n", task, t->callback_cls);
1042 "Canceling task: %llu / %p\n", task, t->callback_cls);
1043#endif 1037#endif
1044 destroy_task (t); 1038 destroy_task (t);
1045 return ret; 1039 return ret;
@@ -1086,10 +1080,8 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1086 t->priority = current_priority; 1080 t->priority = current_priority;
1087 t->lifeness = current_lifeness; 1081 t->lifeness = current_lifeness;
1088#if DEBUG_TASKS 1082#if DEBUG_TASKS
1089 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1083 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "util", 1084 "Adding continuation task: %llu / %p\n", t->id, t->callback_cls);
1091 "Adding continuation task: %llu / %p\n",
1092 t->id, t->callback_cls);
1093#endif 1085#endif
1094 queue_ready_task (t); 1086 queue_ready_task (t);
1095} 1087}
@@ -1220,18 +1212,15 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1220 pending_timeout_last = t; 1212 pending_timeout_last = t;
1221 1213
1222#if DEBUG_TASKS 1214#if DEBUG_TASKS
1223 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1215 LOG (GNUNET_ERROR_TYPE_DEBUG,
1224 "util", 1216 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1225 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1226#endif 1217#endif
1227#if EXECINFO 1218#if EXECINFO
1228 int i; 1219 int i;
1229 1220
1230 for (i = 0; i < t->num_backtrace_strings; i++) 1221 for (i = 0; i < t->num_backtrace_strings; i++)
1231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "util", 1223 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1233 "Task %llu trace %d: %s\n", t->id, i,
1234 t->backtrace_strings[i]);
1235#endif 1224#endif
1236 return t->id; 1225 return t->id;
1237 1226
@@ -1352,14 +1341,14 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1352 1341
1353 if ((flags == -1) && (errno == EBADF)) 1342 if ((flags == -1) && (errno == EBADF))
1354 { 1343 {
1355 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 1344 LOG (GNUNET_ERROR_TYPE_ERROR,
1356 "util", "Got invalid file descriptor %d!\n", rfd); 1345 "Got invalid file descriptor %d!\n", rfd);
1357#if EXECINFO 1346#if EXECINFO
1358 int i; 1347 int i;
1359 1348
1360 for (i = 0; i < t->num_backtrace_strings; i++) 1349 for (i = 0; i < t->num_backtrace_strings; i++)
1361 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1362 "util", "Trace: %s\n", t->backtrace_strings[i]); 1351 "Trace: %s\n", t->backtrace_strings[i]);
1363#endif 1352#endif
1364 GNUNET_assert (0); 1353 GNUNET_assert (0);
1365 } 1354 }
@@ -1370,14 +1359,14 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1370 1359
1371 if (flags == -1 && errno == EBADF) 1360 if (flags == -1 && errno == EBADF)
1372 { 1361 {
1373 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 1362 LOG (GNUNET_ERROR_TYPE_ERROR,
1374 "util", "Got invalid file descriptor %d!\n", wfd); 1363 "Got invalid file descriptor %d!\n", wfd);
1375#if EXECINFO 1364#if EXECINFO
1376 int i; 1365 int i;
1377 1366
1378 for (i = 0; i < t->num_backtrace_strings; i++) 1367 for (i = 0; i < t->num_backtrace_strings; i++)
1379 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1368 LOG (GNUNET_ERROR_TYPE_DEBUG,
1380 "util", "Trace: %s\n", t->backtrace_strings[i]); 1369 "Trace: %s\n", t->backtrace_strings[i]);
1381#endif 1370#endif
1382 GNUNET_assert (0); 1371 GNUNET_assert (0);
1383 } 1372 }
@@ -1398,17 +1387,15 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1398 pending = t; 1387 pending = t;
1399 max_priority_added = GNUNET_MAX (max_priority_added, t->priority); 1388 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1400#if DEBUG_TASKS 1389#if DEBUG_TASKS
1401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1390 LOG (GNUNET_ERROR_TYPE_DEBUG,
1402 "util", 1391 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1403 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1404#endif 1392#endif
1405#if EXECINFO 1393#if EXECINFO
1406 int i; 1394 int i;
1407 1395
1408 for (i = 0; i < t->num_backtrace_strings; i++) 1396 for (i = 0; i < t->num_backtrace_strings; i++)
1409 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "util", 1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1410 "Task %llu trace %d: %s\n", t->id, i, 1398 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1411 t->backtrace_strings[i]);
1412#endif 1399#endif
1413 return t->id; 1400 return t->id;
1414} 1401}
@@ -1673,18 +1660,15 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1673 pending = t; 1660 pending = t;
1674 max_priority_added = GNUNET_MAX (max_priority_added, t->priority); 1661 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1675#if DEBUG_TASKS 1662#if DEBUG_TASKS
1676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1663 LOG (GNUNET_ERROR_TYPE_DEBUG,
1677 "util", 1664 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1678 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1679#endif 1665#endif
1680#if EXECINFO 1666#if EXECINFO
1681 int i; 1667 int i;
1682 1668
1683 for (i = 0; i < t->num_backtrace_strings; i++) 1669 for (i = 0; i < t->num_backtrace_strings; i++)
1684 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1670 LOG (GNUNET_ERROR_TYPE_DEBUG,
1685 "util", 1671 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1686 "Task %llu trace %d: %s\n", t->id, i,
1687 t->backtrace_strings[i]);
1688#endif 1672#endif
1689 return t->id; 1673 return t->id;
1690} 1674}
diff --git a/src/util/server.c b/src/util/server.c
index 63993ee37..f792e5a67 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -33,6 +33,12 @@
33#include "gnunet_disk_lib.h" 33#include "gnunet_disk_lib.h"
34#include "gnunet_protocols.h" 34#include "gnunet_protocols.h"
35 35
36#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37
38#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
39
40#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
41
36#define DEBUG_SERVER GNUNET_EXTRA_LOGGING 42#define DEBUG_SERVER GNUNET_EXTRA_LOGGING
37 43
38/** 44/**
@@ -258,7 +264,8 @@ struct GNUNET_SERVER_Client
258 * @param tc reason why we are running right now 264 * @param tc reason why we are running right now
259 */ 265 */
260static void 266static void
261process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 267process_listen_socket (void *cls,
268 const struct GNUNET_SCHEDULER_TaskContext *tc)
262{ 269{
263 struct GNUNET_SERVER_Handle *server = cls; 270 struct GNUNET_SERVER_Handle *server = cls;
264 struct GNUNET_CONNECTION_Handle *sock; 271 struct GNUNET_CONNECTION_Handle *sock;
@@ -272,46 +279,47 @@ process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
272 while (NULL != server->listen_sockets[i]) 279 while (NULL != server->listen_sockets[i])
273 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]); 280 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
274 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 281 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
275 { 282 {
276 /* ignore shutdown, someone else will take care of it! */ 283 /* ignore shutdown, someone else will take care of it! */
277 server->listen_task = 284 server->listen_task =
278 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 285 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
279 GNUNET_SCHEDULER_NO_TASK, 286 GNUNET_SCHEDULER_NO_TASK,
280 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, 287 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
281 &process_listen_socket, server); 288 &process_listen_socket, server);
282 GNUNET_NETWORK_fdset_destroy (r); 289 GNUNET_NETWORK_fdset_destroy (r);
283 return; 290 return;
284 } 291 }
285 i = 0; 292 i = 0;
286 while (NULL != server->listen_sockets[i]) 293 while (NULL != server->listen_sockets[i])
287 {
288 if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i]))
289 { 294 {
290 sock = 295 if (GNUNET_NETWORK_fdset_isset
291 GNUNET_CONNECTION_create_from_accept (server->access, 296 (tc->read_ready, server->listen_sockets[i]))
292 server->access_cls, 297 {
293 server->listen_sockets[i]); 298 sock =
294 if (sock != NULL) 299 GNUNET_CONNECTION_create_from_accept (server->access,
295 { 300 server->access_cls,
301 server->listen_sockets[i]);
302 if (sock != NULL)
303 {
296#if DEBUG_SERVER 304#if DEBUG_SERVER
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 305 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Server accepted incoming connection.\n"); 306 "Server accepted incoming connection.\n");
299#endif 307#endif
300 client = GNUNET_SERVER_connect_socket (server, sock); 308 client = GNUNET_SERVER_connect_socket (server, sock);
301 GNUNET_CONNECTION_ignore_shutdown (sock, 309 GNUNET_CONNECTION_ignore_shutdown (sock,
302 server->clients_ignore_shutdown); 310 server->clients_ignore_shutdown);
303 /* decrement reference count, we don't keep "client" alive */ 311 /* decrement reference count, we don't keep "client" alive */
304 GNUNET_SERVER_client_drop (client); 312 GNUNET_SERVER_client_drop (client);
305 } 313 }
314 }
315 i++;
306 } 316 }
307 i++;
308 }
309 /* listen for more! */ 317 /* listen for more! */
310 server->listen_task = 318 server->listen_task =
311 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 319 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
312 GNUNET_SCHEDULER_NO_TASK, 320 GNUNET_SCHEDULER_NO_TASK,
313 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, 321 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
314 &process_listen_socket, server); 322 &process_listen_socket, server);
315 GNUNET_NETWORK_fdset_destroy (r); 323 GNUNET_NETWORK_fdset_destroy (r);
316} 324}
317 325
@@ -332,87 +340,87 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
332 int eno; 340 int eno;
333 341
334 switch (serverAddr->sa_family) 342 switch (serverAddr->sa_family)
335 { 343 {
336 case AF_INET: 344 case AF_INET:
337 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port); 345 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
338 break; 346 break;
339 case AF_INET6: 347 case AF_INET6:
340 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port); 348 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
341 break; 349 break;
342 case AF_UNIX: 350 case AF_UNIX:
343 port = 0; 351 port = 0;
344 break; 352 break;
345 default: 353 default:
346 GNUNET_break (0); 354 GNUNET_break (0);
347 port = 0; 355 port = 0;
348 break; 356 break;
349 } 357 }
350 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0); 358 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
351 if (NULL == sock) 359 if (NULL == sock)
352 { 360 {
353 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); 361 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
354 errno = 0; 362 errno = 0;
355 return NULL; 363 return NULL;
356 } 364 }
357 if (port != 0) 365 if (port != 0)
358 { 366 {
359 if (GNUNET_NETWORK_socket_setsockopt 367 if (GNUNET_NETWORK_socket_setsockopt
360 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 368 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
361 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 369 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
362 "setsockopt"); 370 "setsockopt");
363#ifdef IPV6_V6ONLY 371#ifdef IPV6_V6ONLY
364 if ((serverAddr->sa_family == AF_INET6) && 372 if ((serverAddr->sa_family == AF_INET6) &&
365 (GNUNET_NETWORK_socket_setsockopt 373 (GNUNET_NETWORK_socket_setsockopt
366 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) 374 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
367 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 375 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
368 "setsockopt"); 376 "setsockopt");
369#endif 377#endif
370 } 378 }
371 /* bind the socket */ 379 /* bind the socket */
372 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK) 380 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
373 {
374 eno = errno;
375 if (errno != EADDRINUSE)
376 { 381 {
377 /* we don't log 'EADDRINUSE' here since an IPv4 bind may 382 eno = errno;
378 * fail if we already took the port on IPv6; if both IPv4 and 383 if (errno != EADDRINUSE)
379 * IPv6 binds fail, then our caller will log using the 384 {
380 * errno preserved in 'eno' */ 385 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
381 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); 386 * fail if we already took the port on IPv6; if both IPv4 and
382 if (port != 0) 387 * IPv6 binds fail, then our caller will log using the
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 388 * errno preserved in 'eno' */
384 _("`%s' failed for port %d (%s).\n"), "bind", port, 389 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
385 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); 390 if (port != 0)
386 eno = 0; 391 LOG (GNUNET_ERROR_TYPE_ERROR,
392 _("`%s' failed for port %d (%s).\n"), "bind", port,
393 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
394 eno = 0;
395 }
396 else
397 {
398 if (port != 0)
399 LOG (GNUNET_ERROR_TYPE_WARNING,
400 _("`%s' failed for port %d (%s): address already in use\n"),
401 "bind", port,
402 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
403 else if (serverAddr->sa_family == AF_UNIX)
404 LOG (GNUNET_ERROR_TYPE_WARNING,
405 _("`%s' failed for `%s': address already in use\n"), "bind",
406 ((const struct sockaddr_un *) serverAddr)->sun_path);
407
408 }
409 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
410 errno = eno;
411 return NULL;
387 } 412 }
388 else 413 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
389 { 414 {
390 if (port != 0) 415 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 416 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
392 _("`%s' failed for port %d (%s): address already in use\n"), 417 errno = 0;
393 "bind", port, 418 return NULL;
394 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
395 else if (serverAddr->sa_family == AF_UNIX)
396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
397 _("`%s' failed for `%s': address already in use\n"), "bind",
398 ((const struct sockaddr_un *) serverAddr)->sun_path);
399
400 } 419 }
401 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
402 errno = eno;
403 return NULL;
404 }
405 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
406 {
407 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
408 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
409 errno = 0;
410 return NULL;
411 }
412#if DEBUG_SERVER 420#if DEBUG_SERVER
413 if (port != 0) 421 if (port != 0)
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 422 LOG (GNUNET_ERROR_TYPE_DEBUG,
415 "Server starts to listen on port %u.\n", port); 423 "Server starts to listen on port %u.\n", port);
416#endif 424#endif
417 return sock; 425 return sock;
418} 426}
@@ -432,10 +440,10 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
432 */ 440 */
433struct GNUNET_SERVER_Handle * 441struct GNUNET_SERVER_Handle *
434GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, 442GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
435 void *access_cls, 443 void *access_cls,
436 struct GNUNET_NETWORK_Handle **lsocks, 444 struct GNUNET_NETWORK_Handle **lsocks,
437 struct GNUNET_TIME_Relative idle_timeout, 445 struct GNUNET_TIME_Relative idle_timeout,
438 int require_found) 446 int require_found)
439{ 447{
440 struct GNUNET_SERVER_Handle *ret; 448 struct GNUNET_SERVER_Handle *ret;
441 struct GNUNET_NETWORK_FDSet *r; 449 struct GNUNET_NETWORK_FDSet *r;
@@ -448,18 +456,18 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
448 ret->access_cls = access_cls; 456 ret->access_cls = access_cls;
449 ret->require_found = require_found; 457 ret->require_found = require_found;
450 if (lsocks != NULL) 458 if (lsocks != NULL)
451 { 459 {
452 r = GNUNET_NETWORK_fdset_create (); 460 r = GNUNET_NETWORK_fdset_create ();
453 i = 0; 461 i = 0;
454 while (NULL != ret->listen_sockets[i]) 462 while (NULL != ret->listen_sockets[i])
455 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]); 463 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
456 ret->listen_task = 464 ret->listen_task =
457 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 465 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
458 GNUNET_SCHEDULER_NO_TASK, 466 GNUNET_SCHEDULER_NO_TASK,
459 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, 467 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
460 &process_listen_socket, ret); 468 &process_listen_socket, ret);
461 GNUNET_NETWORK_fdset_destroy (r); 469 GNUNET_NETWORK_fdset_destroy (r);
462 } 470 }
463 return ret; 471 return ret;
464} 472}
465 473
@@ -479,10 +487,10 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
479 */ 487 */
480struct GNUNET_SERVER_Handle * 488struct GNUNET_SERVER_Handle *
481GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls, 489GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
482 struct sockaddr *const *serverAddr, 490 struct sockaddr *const *serverAddr,
483 const socklen_t * socklen, 491 const socklen_t * socklen,
484 struct GNUNET_TIME_Relative idle_timeout, 492 struct GNUNET_TIME_Relative idle_timeout,
485 int require_found) 493 int require_found)
486{ 494{
487 struct GNUNET_NETWORK_Handle **lsocks; 495 struct GNUNET_NETWORK_Handle **lsocks;
488 unsigned int i; 496 unsigned int i;
@@ -492,31 +500,32 @@ GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
492 while (serverAddr[i] != NULL) 500 while (serverAddr[i] != NULL)
493 i++; 501 i++;
494 if (i > 0) 502 if (i > 0)
495 {
496 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
497 i = 0;
498 j = 0;
499 while (serverAddr[i] != NULL)
500 { 503 {
501 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]); 504 lsocks =
502 if (lsocks[j] != NULL) 505 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
503 j++; 506 i = 0;
504 i++; 507 j = 0;
508 while (serverAddr[i] != NULL)
509 {
510 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
511 if (lsocks[j] != NULL)
512 j++;
513 i++;
514 }
515 if (j == 0)
516 {
517 if (errno != 0)
518 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
519 GNUNET_free (lsocks);
520 lsocks = NULL;
521 }
505 } 522 }
506 if (j == 0) 523 else
507 { 524 {
508 if (errno != 0)
509 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
510 GNUNET_free (lsocks);
511 lsocks = NULL; 525 lsocks = NULL;
512 } 526 }
513 }
514 else
515 {
516 lsocks = NULL;
517 }
518 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks, 527 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks,
519 idle_timeout, require_found); 528 idle_timeout, require_found);
520} 529}
521 530
522 531
@@ -533,35 +542,35 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
533 unsigned int i; 542 unsigned int i;
534 543
535#if DEBUG_SERVER 544#if DEBUG_SERVER
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n"); 545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
537#endif 546#endif
538 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task) 547 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
539 { 548 {
540 GNUNET_SCHEDULER_cancel (s->listen_task); 549 GNUNET_SCHEDULER_cancel (s->listen_task);
541 s->listen_task = GNUNET_SCHEDULER_NO_TASK; 550 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
542 } 551 }
543 if (s->listen_sockets != NULL) 552 if (s->listen_sockets != NULL)
544 { 553 {
545 i = 0; 554 i = 0;
546 while (s->listen_sockets[i] != NULL) 555 while (s->listen_sockets[i] != NULL)
547 GNUNET_break (GNUNET_OK == 556 GNUNET_break (GNUNET_OK ==
548 GNUNET_NETWORK_socket_close (s->listen_sockets[i++])); 557 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
549 GNUNET_free (s->listen_sockets); 558 GNUNET_free (s->listen_sockets);
550 s->listen_sockets = NULL; 559 s->listen_sockets = NULL;
551 } 560 }
552 while (s->clients != NULL) 561 while (s->clients != NULL)
553 GNUNET_SERVER_client_disconnect (s->clients); 562 GNUNET_SERVER_client_disconnect (s->clients);
554 while (NULL != (hpos = s->handlers)) 563 while (NULL != (hpos = s->handlers))
555 { 564 {
556 s->handlers = hpos->next; 565 s->handlers = hpos->next;
557 GNUNET_free (hpos); 566 GNUNET_free (hpos);
558 } 567 }
559 while (NULL != (npos = s->disconnect_notify_list)) 568 while (NULL != (npos = s->disconnect_notify_list))
560 { 569 {
561 npos->callback (npos->callback_cls, NULL); 570 npos->callback (npos->callback_cls, NULL);
562 s->disconnect_notify_list = npos->next; 571 s->disconnect_notify_list = npos->next;
563 GNUNET_free (npos); 572 GNUNET_free (npos);
564 } 573 }
565 GNUNET_free (s); 574 GNUNET_free (s);
566} 575}
567 576
@@ -581,7 +590,8 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
581 */ 590 */
582void 591void
583GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server, 592GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
584 const struct GNUNET_SERVER_MessageHandler *handlers) 593 const struct GNUNET_SERVER_MessageHandler
594 *handlers)
585{ 595{
586 struct HandlerList *p; 596 struct HandlerList *p;
587 597
@@ -599,21 +609,21 @@ GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
599 * @param tc scheduler context (unused) 609 * @param tc scheduler context (unused)
600 */ 610 */
601static void 611static void
602warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 612warn_no_receive_done (void *cls,
613 const struct GNUNET_SCHEDULER_TaskContext *tc)
603{ 614{
604 struct GNUNET_SERVER_Client *client = cls; 615 struct GNUNET_SERVER_Client *client = cls;
605 616
606 client->warn_task = 617 client->warn_task =
607 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 618 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
608 &warn_no_receive_done, client); 619 &warn_no_receive_done, client);
609 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) 620 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 621 LOG (GNUNET_ERROR_TYPE_WARNING,
611 _ 622 _
612 ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"), 623 ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
613 (unsigned int) client->warn_type, 624 (unsigned int) client->warn_type,
614 (unsigned long long) 625 (unsigned long long)
615 GNUNET_TIME_absolute_get_duration 626 GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
616 (client->warn_start).rel_value);
617} 627}
618 628
619 629
@@ -625,13 +635,14 @@ warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
625 * @param client client for which to disable the warning 635 * @param client client for which to disable the warning
626 */ 636 */
627void 637void
628GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client) 638GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
639 *client)
629{ 640{
630 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) 641 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
631 { 642 {
632 GNUNET_SCHEDULER_cancel (client->warn_task); 643 GNUNET_SCHEDULER_cancel (client->warn_task);
633 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 644 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
634 } 645 }
635} 646}
636 647
637 648
@@ -652,8 +663,8 @@ GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
652 */ 663 */
653int 664int
654GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, 665GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
655 struct GNUNET_SERVER_Client *sender, 666 struct GNUNET_SERVER_Client *sender,
656 const struct GNUNET_MessageHeader *message) 667 const struct GNUNET_MessageHeader *message)
657{ 668{
658 struct HandlerList *pos; 669 struct HandlerList *pos;
659 const struct GNUNET_SERVER_MessageHandler *mh; 670 const struct GNUNET_SERVER_MessageHandler *mh;
@@ -666,56 +677,57 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
666 size = ntohs (message->size); 677 size = ntohs (message->size);
667#if DEBUG_SERVER 678#if DEBUG_SERVER
668 679
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 680 LOG (GNUNET_ERROR_TYPE_DEBUG,
670 "Server schedules transmission of %u-byte message of type %u to client.\n", 681 "Server schedules transmission of %u-byte message of type %u to client.\n",
671 size, type); 682 size, type);
672#endif 683#endif
673 pos = server->handlers; 684 pos = server->handlers;
674 found = GNUNET_NO; 685 found = GNUNET_NO;
675 while (pos != NULL) 686 while (pos != NULL)
676 {
677 i = 0;
678 while (pos->handlers[i].callback != NULL)
679 { 687 {
680 mh = &pos->handlers[i]; 688 i = 0;
681 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL)) 689 while (pos->handlers[i].callback != NULL)
682 { 690 {
683 if ((mh->expected_size != 0) && (mh->expected_size != size)) 691 mh = &pos->handlers[i];
684 { 692 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
693 {
694 if ((mh->expected_size != 0) && (mh->expected_size != size))
695 {
685#if GNUNET8_NETWORK_IS_DEAD 696#if GNUNET8_NETWORK_IS_DEAD
686 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 697 LOG (GNUNET_ERROR_TYPE_WARNING,
687 "Expected %u bytes for message of type %u, got %u\n", 698 "Expected %u bytes for message of type %u, got %u\n",
688 mh->expected_size, mh->type, size); 699 mh->expected_size, mh->type, size);
689 GNUNET_break_op (0); 700 GNUNET_break_op (0);
690#endif 701#endif
691 return GNUNET_SYSERR; 702 return GNUNET_SYSERR;
692 } 703 }
693 if (sender != NULL) 704 if (sender != NULL)
694 { 705 {
695 if (0 == sender->suspended) 706 if (0 == sender->suspended)
696 { 707 {
697 sender->warn_start = GNUNET_TIME_absolute_get (); 708 sender->warn_start = GNUNET_TIME_absolute_get ();
698 sender->warn_task = 709 sender->warn_task =
699 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 710 GNUNET_SCHEDULER_add_delayed
700 &warn_no_receive_done, sender); 711 (GNUNET_TIME_UNIT_MINUTES, &warn_no_receive_done,
701 sender->warn_type = type; 712 sender);
702 } 713 sender->warn_type = type;
703 sender->suspended++; 714 }
704 } 715 sender->suspended++;
705 mh->callback (mh->callback_cls, sender, message); 716 }
706 found = GNUNET_YES; 717 mh->callback (mh->callback_cls, sender, message);
707 } 718 found = GNUNET_YES;
708 i++; 719 }
720 i++;
721 }
722 pos = pos->next;
709 } 723 }
710 pos = pos->next;
711 }
712 if (found == GNUNET_NO) 724 if (found == GNUNET_NO)
713 { 725 {
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 726 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
715 "Received message of unknown type %d\n", type); 727 "Received message of unknown type %d\n", type);
716 if (server->require_found == GNUNET_YES) 728 if (server->require_found == GNUNET_YES)
717 return GNUNET_SYSERR; 729 return GNUNET_SYSERR;
718 } 730 }
719 return GNUNET_OK; 731 return GNUNET_OK;
720} 732}
721 733
@@ -732,7 +744,8 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
732 */ 744 */
733static void 745static void
734process_incoming (void *cls, const void *buf, size_t available, 746process_incoming (void *cls, const void *buf, size_t available,
735 const struct sockaddr *addr, socklen_t addrlen, int errCode); 747 const struct sockaddr *addr, socklen_t addrlen,
748 int errCode);
736 749
737 750
738/** 751/**
@@ -752,44 +765,44 @@ static void
752process_mst (struct GNUNET_SERVER_Client *client, int ret) 765process_mst (struct GNUNET_SERVER_Client *client, int ret)
753{ 766{
754 while ((ret != GNUNET_SYSERR) && (client->server != NULL) && 767 while ((ret != GNUNET_SYSERR) && (client->server != NULL) &&
755 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended)) 768 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
756 {
757 if (ret == GNUNET_OK)
758 { 769 {
759 client->receive_pending = GNUNET_YES; 770 if (ret == GNUNET_OK)
771 {
772 client->receive_pending = GNUNET_YES;
760#if DEBUG_SERVER 773#if DEBUG_SERVER
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 774 LOG (GNUNET_ERROR_TYPE_DEBUG,
762 "Server re-enters receive loop, timeout: %llu.\n", 775 "Server re-enters receive loop, timeout: %llu.\n",
763 client->idle_timeout.rel_value); 776 client->idle_timeout.rel_value);
764#endif 777#endif
765 GNUNET_CONNECTION_receive (client->connection, 778 GNUNET_CONNECTION_receive (client->connection,
766 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 779 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
767 client->idle_timeout, &process_incoming, 780 client->idle_timeout, &process_incoming,
768 client); 781 client);
769 break; 782 break;
770 } 783 }
771#if DEBUG_SERVER 784#if DEBUG_SERVER
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 785 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "Server processes additional messages instantly.\n"); 786 "Server processes additional messages instantly.\n");
774#endif 787#endif
775 ret = 788 ret =
776 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, 789 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
777 GNUNET_YES); 790 GNUNET_YES);
778 } 791 }
779#if DEBUG_SERVER 792#if DEBUG_SERVER
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 793 LOG (GNUNET_ERROR_TYPE_DEBUG,
781 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n", 794 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
782 ret, client->server, client->shutdown_now, client->suspended); 795 ret, client->server, client->shutdown_now, client->suspended);
783#endif 796#endif
784 797
785 if (ret == GNUNET_NO) 798 if (ret == GNUNET_NO)
786 { 799 {
787#if DEBUG_SERVER 800#if DEBUG_SERVER
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 801 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "Server has more data pending but is suspended.\n"); 802 "Server has more data pending but is suspended.\n");
790#endif 803#endif
791 client->receive_pending = GNUNET_SYSERR; /* data pending */ 804 client->receive_pending = GNUNET_SYSERR; /* data pending */
792 } 805 }
793 if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now)) 806 if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now))
794 GNUNET_SERVER_client_disconnect (client); 807 GNUNET_SERVER_client_disconnect (client);
795 GNUNET_SERVER_client_drop (client); 808 GNUNET_SERVER_client_drop (client);
@@ -808,7 +821,7 @@ process_mst (struct GNUNET_SERVER_Client *client, int ret)
808 */ 821 */
809static void 822static void
810process_incoming (void *cls, const void *buf, size_t available, 823process_incoming (void *cls, const void *buf, size_t available,
811 const struct sockaddr *addr, socklen_t addrlen, int errCode) 824 const struct sockaddr *addr, socklen_t addrlen, int errCode)
812{ 825{
813 struct GNUNET_SERVER_Client *client = cls; 826 struct GNUNET_SERVER_Client *client = cls;
814 struct GNUNET_SERVER_Handle *server = client->server; 827 struct GNUNET_SERVER_Handle *server = client->server;
@@ -819,43 +832,44 @@ process_incoming (void *cls, const void *buf, size_t available,
819 GNUNET_assert (client->receive_pending == GNUNET_YES); 832 GNUNET_assert (client->receive_pending == GNUNET_YES);
820 client->receive_pending = GNUNET_NO; 833 client->receive_pending = GNUNET_NO;
821 now = GNUNET_TIME_absolute_get (); 834 now = GNUNET_TIME_absolute_get ();
822 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout); 835 end =
836 GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
823 837
824 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) && 838 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) &&
825 (client->shutdown_now != GNUNET_YES) && (server != NULL) && 839 (client->shutdown_now != GNUNET_YES) && (server != NULL) &&
826 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) && 840 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
827 (end.abs_value > now.abs_value)) 841 (end.abs_value > now.abs_value))
828 { 842 {
829 /* wait longer, timeout changed (i.e. due to us sending) */ 843 /* wait longer, timeout changed (i.e. due to us sending) */
830#if DEBUG_SERVER 844#if DEBUG_SERVER
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 845 LOG (GNUNET_ERROR_TYPE_DEBUG,
832 "Receive time out, but no disconnect due to sending (%p)\n", 846 "Receive time out, but no disconnect due to sending (%p)\n",
833 GNUNET_a2s (addr, addrlen)); 847 GNUNET_a2s (addr, addrlen));
834#endif 848#endif
835 client->receive_pending = GNUNET_YES; 849 client->receive_pending = GNUNET_YES;
836 GNUNET_CONNECTION_receive (client->connection, 850 GNUNET_CONNECTION_receive (client->connection,
837 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 851 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
838 GNUNET_TIME_absolute_get_remaining (end), 852 GNUNET_TIME_absolute_get_remaining (end),
839 &process_incoming, client); 853 &process_incoming, client);
840 return; 854 return;
841 } 855 }
842 if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) || 856 if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL)
843 (client->shutdown_now == GNUNET_YES) || 857 || (client->shutdown_now == GNUNET_YES)
844 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection))) 858 || (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
845 { 859 {
846 /* other side closed connection, error connecting, etc. */ 860 /* other side closed connection, error connecting, etc. */
847 GNUNET_SERVER_client_disconnect (client); 861 GNUNET_SERVER_client_disconnect (client);
848 return; 862 return;
849 } 863 }
850#if DEBUG_SERVER 864#if DEBUG_SERVER
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n", 865 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n",
852 (unsigned int) available, GNUNET_a2s (addr, addrlen)); 866 (unsigned int) available, GNUNET_a2s (addr, addrlen));
853#endif 867#endif
854 GNUNET_SERVER_client_keep (client); 868 GNUNET_SERVER_client_keep (client);
855 client->last_activity = now; 869 client->last_activity = now;
856 ret = 870 ret =
857 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, 871 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
858 GNUNET_YES); 872 GNUNET_YES);
859 process_mst (client, ret); 873 process_mst (client, ret);
860} 874}
861 875
@@ -876,25 +890,26 @@ restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
876 client->restart_task = GNUNET_SCHEDULER_NO_TASK; 890 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
877 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && 891 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
878 (GNUNET_NO == server->clients_ignore_shutdown)) 892 (GNUNET_NO == server->clients_ignore_shutdown))
879 { 893 {
880 GNUNET_SERVER_client_disconnect (client); 894 GNUNET_SERVER_client_disconnect (client);
881 return; 895 return;
882 } 896 }
883 if (client->receive_pending == GNUNET_NO) 897 if (client->receive_pending == GNUNET_NO)
884 { 898 {
885#if DEBUG_SERVER 899#if DEBUG_SERVER
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 900 LOG (GNUNET_ERROR_TYPE_DEBUG,
887 "Server begins to read again from client.\n"); 901 "Server begins to read again from client.\n");
888#endif 902#endif
889 client->receive_pending = GNUNET_YES; 903 client->receive_pending = GNUNET_YES;
890 GNUNET_CONNECTION_receive (client->connection, 904 GNUNET_CONNECTION_receive (client->connection,
891 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 905 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
892 client->idle_timeout, &process_incoming, client); 906 client->idle_timeout, &process_incoming,
893 return; 907 client);
894 } 908 return;
909 }
895#if DEBUG_SERVER 910#if DEBUG_SERVER
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 911 LOG (GNUNET_ERROR_TYPE_DEBUG,
897 "Server continues processing messages still in the buffer.\n"); 912 "Server continues processing messages still in the buffer.\n");
898#endif 913#endif
899 GNUNET_SERVER_client_keep (client); 914 GNUNET_SERVER_client_keep (client);
900 client->receive_pending = GNUNET_NO; 915 client->receive_pending = GNUNET_NO;
@@ -912,7 +927,7 @@ restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
912 */ 927 */
913static void 928static void
914client_message_tokenizer_callback (void *cls, void *client, 929client_message_tokenizer_callback (void *cls, void *client,
915 const struct GNUNET_MessageHeader *message) 930 const struct GNUNET_MessageHeader *message)
916{ 931{
917 struct GNUNET_SERVER_Handle *server = cls; 932 struct GNUNET_SERVER_Handle *server = cls;
918 struct GNUNET_SERVER_Client *sender = client; 933 struct GNUNET_SERVER_Client *sender = client;
@@ -920,9 +935,9 @@ client_message_tokenizer_callback (void *cls, void *client,
920 935
921#if DEBUG_SERVER 936#if DEBUG_SERVER
922 937
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 938 LOG (GNUNET_ERROR_TYPE_DEBUG,
924 "Tokenizer gives server message of type %u from client\n", 939 "Tokenizer gives server message of type %u from client\n",
925 ntohs (message->type)); 940 ntohs (message->type));
926#endif 941#endif
927 sender->in_process_client_buffer = GNUNET_YES; 942 sender->in_process_client_buffer = GNUNET_YES;
928 ret = GNUNET_SERVER_inject (server, sender, message); 943 ret = GNUNET_SERVER_inject (server, sender, message);
@@ -946,14 +961,14 @@ client_message_tokenizer_callback (void *cls, void *client,
946 */ 961 */
947struct GNUNET_SERVER_Client * 962struct GNUNET_SERVER_Client *
948GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server, 963GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
949 struct GNUNET_CONNECTION_Handle *connection) 964 struct GNUNET_CONNECTION_Handle *connection)
950{ 965{
951 struct GNUNET_SERVER_Client *client; 966 struct GNUNET_SERVER_Client *client;
952 967
953 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client)); 968 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
954 client->connection = connection; 969 client->connection = connection;
955 client->mst = 970 client->mst =
956 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server); 971 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
957 client->reference_count = 1; 972 client->reference_count = 1;
958 client->server = server; 973 client->server = server;
959 client->last_activity = GNUNET_TIME_absolute_get (); 974 client->last_activity = GNUNET_TIME_absolute_get ();
@@ -964,8 +979,8 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
964 client->callback = NULL; 979 client->callback = NULL;
965 client->callback_cls = NULL; 980 client->callback_cls = NULL;
966 GNUNET_CONNECTION_receive (client->connection, 981 GNUNET_CONNECTION_receive (client->connection,
967 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 982 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
968 client->idle_timeout, &process_incoming, client); 983 client->idle_timeout, &process_incoming, client);
969 return client; 984 return client;
970} 985}
971 986
@@ -980,7 +995,7 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
980 */ 995 */
981void 996void
982GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client, 997GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
983 struct GNUNET_TIME_Relative timeout) 998 struct GNUNET_TIME_Relative timeout)
984{ 999{
985 client->idle_timeout = timeout; 1000 client->idle_timeout = timeout;
986} 1001}
@@ -1028,7 +1043,7 @@ GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1028 */ 1043 */
1029int 1044int
1030GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client, 1045GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1031 void **addr, size_t * addrlen) 1046 void **addr, size_t * addrlen)
1032{ 1047{
1033 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen); 1048 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1034} 1049}
@@ -1046,8 +1061,8 @@ GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1046 */ 1061 */
1047void 1062void
1048GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server, 1063GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1049 GNUNET_SERVER_DisconnectCallback callback, 1064 GNUNET_SERVER_DisconnectCallback callback,
1050 void *callback_cls) 1065 void *callback_cls)
1051{ 1066{
1052 struct NotifyList *n; 1067 struct NotifyList *n;
1053 1068
@@ -1068,8 +1083,8 @@ GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1068 */ 1083 */
1069void 1084void
1070GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server, 1085GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1071 GNUNET_SERVER_DisconnectCallback 1086 GNUNET_SERVER_DisconnectCallback
1072 callback, void *callback_cls) 1087 callback, void *callback_cls)
1073{ 1088{
1074 struct NotifyList *pos; 1089 struct NotifyList *pos;
1075 struct NotifyList *prev; 1090 struct NotifyList *prev;
@@ -1077,17 +1092,17 @@ GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1077 prev = NULL; 1092 prev = NULL;
1078 pos = server->disconnect_notify_list; 1093 pos = server->disconnect_notify_list;
1079 while (pos != NULL) 1094 while (pos != NULL)
1080 { 1095 {
1081 if ((pos->callback == callback) && (pos->callback_cls == callback_cls)) 1096 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1082 break; 1097 break;
1083 prev = pos; 1098 prev = pos;
1084 pos = pos->next; 1099 pos = pos->next;
1085 } 1100 }
1086 if (pos == NULL) 1101 if (pos == NULL)
1087 { 1102 {
1088 GNUNET_break (0); 1103 GNUNET_break (0);
1089 return; 1104 return;
1090 } 1105 }
1091 if (prev == NULL) 1106 if (prev == NULL)
1092 server->disconnect_notify_list = pos->next; 1107 server->disconnect_notify_list = pos->next;
1093 else 1108 else
@@ -1114,76 +1129,76 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1114 unsigned int rc; 1129 unsigned int rc;
1115 1130
1116#if DEBUG_SERVER 1131#if DEBUG_SERVER
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1132 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Client is being disconnected from the server.\n"); 1133 "Client is being disconnected from the server.\n");
1119#endif 1134#endif
1120 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) 1135 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1121 {
1122 GNUNET_SCHEDULER_cancel (client->restart_task);
1123 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1124 }
1125 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1126 {
1127 GNUNET_SCHEDULER_cancel (client->warn_task);
1128 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1129 }
1130 if (GNUNET_YES == client->receive_pending)
1131 {
1132 GNUNET_CONNECTION_receive_cancel (client->connection);
1133 client->receive_pending = GNUNET_NO;
1134 }
1135
1136 rc = client->reference_count;
1137 if (client->server != NULL)
1138 {
1139 server = client->server;
1140 client->server = NULL;
1141 client->shutdown_now = GNUNET_YES;
1142 prev = NULL;
1143 pos = server->clients;
1144 while ((pos != NULL) && (pos != client))
1145 {
1146 prev = pos;
1147 pos = pos->next;
1148 }
1149 GNUNET_assert (pos != NULL);
1150 if (prev == NULL)
1151 server->clients = pos->next;
1152 else
1153 prev->next = pos->next;
1154 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1155 { 1136 {
1156 GNUNET_SCHEDULER_cancel (client->restart_task); 1137 GNUNET_SCHEDULER_cancel (client->restart_task);
1157 client->restart_task = GNUNET_SCHEDULER_NO_TASK; 1138 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1158 } 1139 }
1159 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) 1140 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1160 { 1141 {
1161 GNUNET_SCHEDULER_cancel (client->warn_task); 1142 GNUNET_SCHEDULER_cancel (client->warn_task);
1162 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 1143 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1163 } 1144 }
1164 n = server->disconnect_notify_list; 1145 if (GNUNET_YES == client->receive_pending)
1165 while (n != NULL)
1166 { 1146 {
1167 n->callback (n->callback_cls, client); 1147 GNUNET_CONNECTION_receive_cancel (client->connection);
1168 n = n->next; 1148 client->receive_pending = GNUNET_NO;
1149 }
1150
1151 rc = client->reference_count;
1152 if (client->server != NULL)
1153 {
1154 server = client->server;
1155 client->server = NULL;
1156 client->shutdown_now = GNUNET_YES;
1157 prev = NULL;
1158 pos = server->clients;
1159 while ((pos != NULL) && (pos != client))
1160 {
1161 prev = pos;
1162 pos = pos->next;
1163 }
1164 GNUNET_assert (pos != NULL);
1165 if (prev == NULL)
1166 server->clients = pos->next;
1167 else
1168 prev->next = pos->next;
1169 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1170 {
1171 GNUNET_SCHEDULER_cancel (client->restart_task);
1172 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1173 }
1174 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1175 {
1176 GNUNET_SCHEDULER_cancel (client->warn_task);
1177 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1178 }
1179 n = server->disconnect_notify_list;
1180 while (n != NULL)
1181 {
1182 n->callback (n->callback_cls, client);
1183 n = n->next;
1184 }
1169 } 1185 }
1170 }
1171 if (rc > 0) 1186 if (rc > 0)
1172 { 1187 {
1173#if DEBUG_SERVER 1188#if DEBUG_SERVER
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1189 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "RC still positive, not destroying everything.\n"); 1190 "RC still positive, not destroying everything.\n");
1176#endif 1191#endif
1177 return; 1192 return;
1178 } 1193 }
1179 if (client->in_process_client_buffer == GNUNET_YES) 1194 if (client->in_process_client_buffer == GNUNET_YES)
1180 { 1195 {
1181#if DEBUG_SERVER 1196#if DEBUG_SERVER
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1183 "Still processing inputs, not destroying everything.\n"); 1198 "Still processing inputs, not destroying everything.\n");
1184#endif 1199#endif
1185 return; 1200 return;
1186 } 1201 }
1187 1202
1188 if (client->persist == GNUNET_YES) 1203 if (client->persist == GNUNET_YES)
1189 GNUNET_CONNECTION_persist_ (client->connection); 1204 GNUNET_CONNECTION_persist_ (client->connection);
@@ -1247,17 +1262,17 @@ transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1247 */ 1262 */
1248struct GNUNET_CONNECTION_TransmitHandle * 1263struct GNUNET_CONNECTION_TransmitHandle *
1249GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client, 1264GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1250 size_t size, 1265 size_t size,
1251 struct GNUNET_TIME_Relative timeout, 1266 struct GNUNET_TIME_Relative timeout,
1252 GNUNET_CONNECTION_TransmitReadyNotify 1267 GNUNET_CONNECTION_TransmitReadyNotify
1253 callback, void *callback_cls) 1268 callback, void *callback_cls)
1254{ 1269{
1255 client->callback_cls = callback_cls; 1270 client->callback_cls = callback_cls;
1256 client->callback = callback; 1271 client->callback = callback;
1257 return GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, 1272 return GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1258 timeout, 1273 timeout,
1259 &transmit_ready_callback_wrapper, 1274 &transmit_ready_callback_wrapper,
1260 client); 1275 client);
1261} 1276}
1262 1277
1263 1278
@@ -1294,46 +1309,47 @@ GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1294 GNUNET_assert (client->suspended > 0); 1309 GNUNET_assert (client->suspended > 0);
1295 client->suspended--; 1310 client->suspended--;
1296 if (success != GNUNET_OK) 1311 if (success != GNUNET_OK)
1297 { 1312 {
1298#if DEBUG_SERVER 1313#if DEBUG_SERVER
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1314 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "GNUNET_SERVER_receive_done called with failure indication\n"); 1315 "GNUNET_SERVER_receive_done called with failure indication\n");
1301#endif 1316#endif
1302 GNUNET_SERVER_client_disconnect (client); 1317 GNUNET_SERVER_client_disconnect (client);
1303 return; 1318 return;
1304 } 1319 }
1305 if (client->suspended > 0) 1320 if (client->suspended > 0)
1306 { 1321 {
1307#if DEBUG_SERVER 1322#if DEBUG_SERVER
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "GNUNET_SERVER_receive_done called, but more clients pending\n"); 1324 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1310#endif 1325#endif
1311 return; 1326 return;
1312 } 1327 }
1313 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) 1328 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1314 { 1329 {
1315 GNUNET_SCHEDULER_cancel (client->warn_task); 1330 GNUNET_SCHEDULER_cancel (client->warn_task);
1316 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 1331 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1317 } 1332 }
1318 if (client->in_process_client_buffer == GNUNET_YES) 1333 if (client->in_process_client_buffer == GNUNET_YES)
1319 { 1334 {
1320#if DEBUG_SERVER 1335#if DEBUG_SERVER
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1322 "GNUNET_SERVER_receive_done called while still in processing loop\n"); 1337 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1323#endif 1338#endif
1324 return; 1339 return;
1325 } 1340 }
1326 if (client->server == NULL) 1341 if (client->server == NULL)
1327 { 1342 {
1328 GNUNET_SERVER_client_disconnect (client); 1343 GNUNET_SERVER_client_disconnect (client);
1329 return; 1344 return;
1330 } 1345 }
1331#if DEBUG_SERVER 1346#if DEBUG_SERVER
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1347 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n"); 1348 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1334#endif 1349#endif
1335 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task); 1350 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1336 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client); 1351 client->restart_task =
1352 GNUNET_SCHEDULER_add_now (&restart_processing, client);
1337} 1353}
1338 1354
1339 1355
diff --git a/src/util/server_mst.c b/src/util/server_mst.c
index adb0a0818..6fd2647d7 100644
--- a/src/util/server_mst.c
+++ b/src/util/server_mst.c
@@ -39,6 +39,8 @@
39#define ALIGN_FACTOR 8 39#define ALIGN_FACTOR 8
40#endif 40#endif
41 41
42#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
43
42 44
43/** 45/**
44 * Handle to a message stream tokenizer. 46 * Handle to a message stream tokenizer.
@@ -89,7 +91,7 @@ struct GNUNET_SERVER_MessageStreamTokenizer
89 */ 91 */
90struct GNUNET_SERVER_MessageStreamTokenizer * 92struct GNUNET_SERVER_MessageStreamTokenizer *
91GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb, 93GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
92 void *cb_cls) 94 void *cb_cls)
93{ 95{
94 struct GNUNET_SERVER_MessageStreamTokenizer *ret; 96 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
95 97
@@ -119,8 +121,8 @@ GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
119 */ 121 */
120int 122int
121GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, 123GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
122 void *client_identity, const char *buf, size_t size, 124 void *client_identity, const char *buf,
123 int purge, int one_shot) 125 size_t size, int purge, int one_shot)
124{ 126{
125 const struct GNUNET_MessageHeader *hdr; 127 const struct GNUNET_MessageHeader *hdr;
126 size_t delta; 128 size_t delta;
@@ -131,161 +133,161 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
131 int ret; 133 int ret;
132 134
133#if DEBUG_SERVER_MST 135#if DEBUG_SERVER_MST
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 136 LOG (GNUNET_ERROR_TYPE_DEBUG,
135 "Server-mst receives %u bytes with %u bytes already in private buffer\n", 137 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
136 (unsigned int) size, (unsigned int) (mst->pos - mst->off)); 138 (unsigned int) size, (unsigned int) (mst->pos - mst->off));
137#endif 139#endif
138 ret = GNUNET_OK; 140 ret = GNUNET_OK;
139 ibuf = (char *) mst->hdr; 141 ibuf = (char *) mst->hdr;
140 while (mst->pos > 0) 142 while (mst->pos > 0)
141 {
142do_align:
143 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
144 (0 != (mst->off % ALIGN_FACTOR)))
145 {
146 /* need to align or need more space */
147 mst->pos -= mst->off;
148 memmove (ibuf, &ibuf[mst->off], mst->pos);
149 mst->off = 0;
150 }
151 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
152 {
153 delta =
154 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
155 (mst->pos - mst->off), size);
156 memcpy (&ibuf[mst->pos], buf, delta);
157 mst->pos += delta;
158 buf += delta;
159 size -= delta;
160 }
161 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
162 {
163 if (purge)
164 {
165 mst->off = 0;
166 mst->pos = 0;
167 }
168 return GNUNET_OK;
169 }
170 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
171 want = ntohs (hdr->size);
172 if (want < sizeof (struct GNUNET_MessageHeader))
173 { 143 {
174 GNUNET_break_op (0); 144 do_align:
175 return GNUNET_SYSERR; 145 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
176 } 146 (0 != (mst->off % ALIGN_FACTOR)))
177 if (mst->curr_buf - mst->off < want) 147 {
178 { 148 /* need to align or need more space */
179 /* need more space */ 149 mst->pos -= mst->off;
180 mst->pos -= mst->off; 150 memmove (ibuf, &ibuf[mst->off], mst->pos);
181 memmove (ibuf, &ibuf[mst->off], mst->pos); 151 mst->off = 0;
182 mst->off = 0; 152 }
183 } 153 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
184 if (want > mst->curr_buf) 154 {
185 { 155 delta =
186 mst->hdr = GNUNET_realloc (mst->hdr, want); 156 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
187 ibuf = (char *) mst->hdr; 157 (mst->pos - mst->off), size);
188 mst->curr_buf = want; 158 memcpy (&ibuf[mst->pos], buf, delta);
189 } 159 mst->pos += delta;
190 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; 160 buf += delta;
191 if (mst->pos - mst->off < want) 161 size -= delta;
192 { 162 }
193 delta = GNUNET_MIN (want - (mst->pos - mst->off), size); 163 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
194 memcpy (&ibuf[mst->pos], buf, delta); 164 {
195 mst->pos += delta; 165 if (purge)
196 buf += delta; 166 {
197 size -= delta; 167 mst->off = 0;
198 } 168 mst->pos = 0;
199 if (mst->pos - mst->off < want) 169 }
200 { 170 return GNUNET_OK;
201 if (purge) 171 }
202 { 172 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
203 mst->off = 0;
204 mst->pos = 0;
205 }
206 return GNUNET_OK;
207 }
208 if (one_shot == GNUNET_SYSERR)
209 {
210 /* cannot call callback again, but return value saying that
211 * we have another full message in the buffer */
212 ret = GNUNET_NO;
213 goto copy;
214 }
215 if (one_shot == GNUNET_YES)
216 one_shot = GNUNET_SYSERR;
217 mst->cb (mst->cb_cls, client_identity, hdr);
218 mst->off += want;
219 if (mst->off == mst->pos)
220 {
221 /* reset to beginning of buffer, it's free right now! */
222 mst->off = 0;
223 mst->pos = 0;
224 }
225 }
226 while (size > 0)
227 {
228#if DEBUG_SERVER_MST
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Server-mst has %u bytes left in inbound buffer\n",
231 (unsigned int) size);
232#endif
233 if (size < sizeof (struct GNUNET_MessageHeader))
234 break;
235 offset = (unsigned long) buf;
236 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
237 if (GNUNET_NO == need_align)
238 {
239 /* can try to do zero-copy and process directly from original buffer */
240 hdr = (const struct GNUNET_MessageHeader *) buf;
241 want = ntohs (hdr->size); 173 want = ntohs (hdr->size);
242 if (want < sizeof (struct GNUNET_MessageHeader)) 174 if (want < sizeof (struct GNUNET_MessageHeader))
243 { 175 {
244 GNUNET_break_op (0); 176 GNUNET_break_op (0);
245 mst->off = 0; 177 return GNUNET_SYSERR;
246 return GNUNET_SYSERR; 178 }
247 } 179 if (mst->curr_buf - mst->off < want)
248 if (size < want) 180 {
249 break; /* or not, buffer incomplete, so copy to private buffer... */ 181 /* need more space */
182 mst->pos -= mst->off;
183 memmove (ibuf, &ibuf[mst->off], mst->pos);
184 mst->off = 0;
185 }
186 if (want > mst->curr_buf)
187 {
188 mst->hdr = GNUNET_realloc (mst->hdr, want);
189 ibuf = (char *) mst->hdr;
190 mst->curr_buf = want;
191 }
192 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
193 if (mst->pos - mst->off < want)
194 {
195 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
196 memcpy (&ibuf[mst->pos], buf, delta);
197 mst->pos += delta;
198 buf += delta;
199 size -= delta;
200 }
201 if (mst->pos - mst->off < want)
202 {
203 if (purge)
204 {
205 mst->off = 0;
206 mst->pos = 0;
207 }
208 return GNUNET_OK;
209 }
250 if (one_shot == GNUNET_SYSERR) 210 if (one_shot == GNUNET_SYSERR)
251 { 211 {
252 /* cannot call callback again, but return value saying that 212 /* cannot call callback again, but return value saying that
253 * we have another full message in the buffer */ 213 * we have another full message in the buffer */
254 ret = GNUNET_NO; 214 ret = GNUNET_NO;
255 goto copy; 215 goto copy;
256 } 216 }
257 if (one_shot == GNUNET_YES) 217 if (one_shot == GNUNET_YES)
258 one_shot = GNUNET_SYSERR; 218 one_shot = GNUNET_SYSERR;
259 mst->cb (mst->cb_cls, client_identity, hdr); 219 mst->cb (mst->cb_cls, client_identity, hdr);
260 buf += want; 220 mst->off += want;
261 size -= want; 221 if (mst->off == mst->pos)
222 {
223 /* reset to beginning of buffer, it's free right now! */
224 mst->off = 0;
225 mst->pos = 0;
226 }
262 } 227 }
263 else 228 while (size > 0)
264 { 229 {
265 /* need to copy to private buffer to align; 230#if DEBUG_SERVER_MST
266 * yes, we go a bit more spagetti than usual here */ 231 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 goto do_align; 232 "Server-mst has %u bytes left in inbound buffer\n",
233 (unsigned int) size);
234#endif
235 if (size < sizeof (struct GNUNET_MessageHeader))
236 break;
237 offset = (unsigned long) buf;
238 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
239 if (GNUNET_NO == need_align)
240 {
241 /* can try to do zero-copy and process directly from original buffer */
242 hdr = (const struct GNUNET_MessageHeader *) buf;
243 want = ntohs (hdr->size);
244 if (want < sizeof (struct GNUNET_MessageHeader))
245 {
246 GNUNET_break_op (0);
247 mst->off = 0;
248 return GNUNET_SYSERR;
249 }
250 if (size < want)
251 break; /* or not, buffer incomplete, so copy to private buffer... */
252 if (one_shot == GNUNET_SYSERR)
253 {
254 /* cannot call callback again, but return value saying that
255 * we have another full message in the buffer */
256 ret = GNUNET_NO;
257 goto copy;
258 }
259 if (one_shot == GNUNET_YES)
260 one_shot = GNUNET_SYSERR;
261 mst->cb (mst->cb_cls, client_identity, hdr);
262 buf += want;
263 size -= want;
264 }
265 else
266 {
267 /* need to copy to private buffer to align;
268 * yes, we go a bit more spagetti than usual here */
269 goto do_align;
270 }
268 } 271 }
269 }
270copy: 272copy:
271 if ((size > 0) && (!purge)) 273 if ((size > 0) && (!purge))
272 {
273 if (size + mst->pos > mst->curr_buf)
274 { 274 {
275 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos); 275 if (size + mst->pos > mst->curr_buf)
276 ibuf = (char *) mst->hdr; 276 {
277 mst->curr_buf = size + mst->pos; 277 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
278 ibuf = (char *) mst->hdr;
279 mst->curr_buf = size + mst->pos;
280 }
281 GNUNET_assert (mst->pos + size <= mst->curr_buf);
282 memcpy (&ibuf[mst->pos], buf, size);
283 mst->pos += size;
278 } 284 }
279 GNUNET_assert (mst->pos + size <= mst->curr_buf);
280 memcpy (&ibuf[mst->pos], buf, size);
281 mst->pos += size;
282 }
283 if (purge) 285 if (purge)
284 mst->off = 0; 286 mst->off = 0;
285#if DEBUG_SERVER_MST 287#if DEBUG_SERVER_MST
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 288 LOG (GNUNET_ERROR_TYPE_DEBUG,
287 "Server-mst leaves %u bytes in private buffer\n", 289 "Server-mst leaves %u bytes in private buffer\n",
288 (unsigned int) (mst->pos - mst->off)); 290 (unsigned int) (mst->pos - mst->off));
289#endif 291#endif
290 return ret; 292 return ret;
291} 293}
diff --git a/src/util/server_nc.c b/src/util/server_nc.c
index dc7a27c8b..cbe98a014 100644
--- a/src/util/server_nc.c
+++ b/src/util/server_nc.c
@@ -33,6 +33,8 @@
33#include "gnunet_server_lib.h" 33#include "gnunet_server_lib.h"
34#include "gnunet_time_lib.h" 34#include "gnunet_time_lib.h"
35 35
36#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37
36 38
37#define DEBUG_SERVER_NC GNUNET_EXTRA_LOGGING 39#define DEBUG_SERVER_NC GNUNET_EXTRA_LOGGING
38 40
@@ -154,40 +156,40 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
154 struct PendingMessageList *pml; 156 struct PendingMessageList *pml;
155 157
156 if (client == NULL) 158 if (client == NULL)
157 { 159 {
158 nc->server = NULL; 160 nc->server = NULL;
159 return; 161 return;
160 } 162 }
161 prev = NULL; 163 prev = NULL;
162 pos = nc->clients; 164 pos = nc->clients;
163 while (NULL != pos) 165 while (NULL != pos)
164 { 166 {
165 if (pos->client == client) 167 if (pos->client == client)
166 break; 168 break;
167 prev = pos; 169 prev = pos;
168 pos = pos->next; 170 pos = pos->next;
169 } 171 }
170 if (pos == NULL) 172 if (pos == NULL)
171 return; 173 return;
172#if DEBUG_SERVER_NC 174#if DEBUG_SERVER_NC
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 175 LOG (GNUNET_ERROR_TYPE_DEBUG,
174 "Client disconnected, cleaning up %u messages in NC queue\n", 176 "Client disconnected, cleaning up %u messages in NC queue\n",
175 pos->num_pending); 177 pos->num_pending);
176#endif 178#endif
177 if (prev == NULL) 179 if (prev == NULL)
178 nc->clients = pos->next; 180 nc->clients = pos->next;
179 else 181 else
180 prev->next = pos->next; 182 prev->next = pos->next;
181 while (NULL != (pml = pos->pending_head)) 183 while (NULL != (pml = pos->pending_head))
182 { 184 {
183 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml); 185 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
184 GNUNET_free (pml); 186 GNUNET_free (pml);
185 } 187 }
186 if (pos->th != NULL) 188 if (pos->th != NULL)
187 { 189 {
188 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th); 190 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
189 pos->th = NULL; 191 pos->th = NULL;
190 } 192 }
191 GNUNET_SERVER_client_drop (client); 193 GNUNET_SERVER_client_drop (client);
192 GNUNET_free (pos); 194 GNUNET_free (pos);
193} 195}
@@ -203,8 +205,8 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
203 * @return handle to the notification context 205 * @return handle to the notification context
204 */ 206 */
205struct GNUNET_SERVER_NotificationContext * 207struct GNUNET_SERVER_NotificationContext *
206GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server, 208GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle
207 unsigned int queue_length) 209 *server, unsigned int queue_length)
208{ 210{
209 struct GNUNET_SERVER_NotificationContext *ret; 211 struct GNUNET_SERVER_NotificationContext *ret;
210 212
@@ -223,26 +225,27 @@ GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
223 */ 225 */
224void 226void
225GNUNET_SERVER_notification_context_destroy (struct 227GNUNET_SERVER_notification_context_destroy (struct
226 GNUNET_SERVER_NotificationContext 228 GNUNET_SERVER_NotificationContext
227 *nc) 229 *nc)
228{ 230{
229 struct ClientList *pos; 231 struct ClientList *pos;
230 struct PendingMessageList *pml; 232 struct PendingMessageList *pml;
231 233
232 while (NULL != (pos = nc->clients)) 234 while (NULL != (pos = nc->clients))
233 {
234 nc->clients = pos->next;
235 GNUNET_SERVER_client_drop (pos->client);
236 while (NULL != (pml = pos->pending_head))
237 { 235 {
238 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml); 236 nc->clients = pos->next;
239 GNUNET_free (pml); 237 GNUNET_SERVER_client_drop (pos->client);
238 while (NULL != (pml = pos->pending_head))
239 {
240 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail,
241 pml);
242 GNUNET_free (pml);
243 }
244 GNUNET_free (pos);
240 } 245 }
241 GNUNET_free (pos);
242 }
243 if (nc->server != NULL) 246 if (nc->server != NULL)
244 GNUNET_SERVER_disconnect_notify_cancel (nc->server, 247 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
245 &handle_client_disconnect, nc); 248 &handle_client_disconnect, nc);
246 GNUNET_free (nc); 249 GNUNET_free (nc);
247} 250}
248 251
@@ -254,9 +257,9 @@ GNUNET_SERVER_notification_context_destroy (struct
254 * @param client client to add 257 * @param client client to add
255 */ 258 */
256void 259void
257GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext 260GNUNET_SERVER_notification_context_add (struct
258 *nc, 261 GNUNET_SERVER_NotificationContext *nc,
259 struct GNUNET_SERVER_Client *client) 262 struct GNUNET_SERVER_Client *client)
260{ 263{
261 struct ClientList *cl; 264 struct ClientList *cl;
262 265
@@ -290,44 +293,45 @@ transmit_message (void *cls, size_t size, void *buf)
290 293
291 cl->th = NULL; 294 cl->th = NULL;
292 if (buf == NULL) 295 if (buf == NULL)
293 { 296 {
294 /* 'cl' should be freed via disconnect notification shortly */ 297 /* 'cl' should be freed via disconnect notification shortly */
295#if DEBUG_SERVER_NC 298#if DEBUG_SERVER_NC
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 299 LOG (GNUNET_ERROR_TYPE_DEBUG,
297 "Failed to transmit message from NC queue to client\n"); 300 "Failed to transmit message from NC queue to client\n");
298#endif 301#endif
299 return 0; 302 return 0;
300 } 303 }
301 ret = 0; 304 ret = 0;
302 while (NULL != (pml = cl->pending_head)) 305 while (NULL != (pml = cl->pending_head))
303 { 306 {
304 msize = ntohs (pml->msg->size); 307 msize = ntohs (pml->msg->size);
305 if (size < msize) 308 if (size < msize)
306 break; 309 break;
307 GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml); 310 GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
308#if DEBUG_SERVER_NC 311#if DEBUG_SERVER_NC
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 312 LOG (GNUNET_ERROR_TYPE_DEBUG,
310 "Copying message of type %u and size %u from pending queue to transmission buffer\n", 313 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
311 ntohs (pml->msg->type), msize); 314 ntohs (pml->msg->type), msize);
312#endif 315#endif
313 memcpy (&cbuf[ret], pml->msg, msize); 316 memcpy (&cbuf[ret], pml->msg, msize);
314 ret += msize; 317 ret += msize;
315 size -= msize; 318 size -= msize;
316 GNUNET_free (pml); 319 GNUNET_free (pml);
317 cl->num_pending--; 320 cl->num_pending--;
318 } 321 }
319 if (pml != NULL) 322 if (pml != NULL)
320 { 323 {
321#if DEBUG_SERVER_NC 324#if DEBUG_SERVER_NC
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 325 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Have %u messages left in NC queue, will try transmission again\n", 326 "Have %u messages left in NC queue, will try transmission again\n",
324 cl->num_pending); 327 cl->num_pending);
325#endif 328#endif
326 cl->th = 329 cl->th =
327 GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size), 330 GNUNET_SERVER_notify_transmit_ready (cl->client,
328 GNUNET_TIME_UNIT_FOREVER_REL, 331 ntohs (pml->msg->size),
329 &transmit_message, cl); 332 GNUNET_TIME_UNIT_FOREVER_REL,
330 } 333 &transmit_message, cl);
334 }
331 else 335 else
332 GNUNET_assert (cl->num_pending == 0); 336 GNUNET_assert (cl->num_pending == 0);
333 return ret; 337 return ret;
@@ -344,47 +348,46 @@ transmit_message (void *cls, size_t size, void *buf)
344 */ 348 */
345static void 349static void
346do_unicast (struct GNUNET_SERVER_NotificationContext *nc, 350do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
347 struct ClientList *client, const struct GNUNET_MessageHeader *msg, 351 struct ClientList *client, const struct GNUNET_MessageHeader *msg,
348 int can_drop) 352 int can_drop)
349{ 353{
350 struct PendingMessageList *pml; 354 struct PendingMessageList *pml;
351 uint16_t size; 355 uint16_t size;
352 356
353 if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop)) 357 if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop))
354 { 358 {
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 359 LOG (GNUNET_ERROR_TYPE_INFO,
356 "Dropping message of type %u and size %u due to full queue (%u entries)\n", 360 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
357 ntohs (msg->type), ntohs (msg->size), 361 ntohs (msg->type), ntohs (msg->size),
358 (unsigned int) nc->queue_length); 362 (unsigned int) nc->queue_length);
359 return; /* drop! */ 363 return; /* drop! */
360 } 364 }
361 if (client->num_pending > nc->queue_length) 365 if (client->num_pending > nc->queue_length)
362 { 366 {
363 /* FIXME: consider checking for other messages in the 367 /* FIXME: consider checking for other messages in the
364 * queue that are 'droppable' */ 368 * queue that are 'droppable' */
365 } 369 }
366 client->num_pending++; 370 client->num_pending++;
367 size = ntohs (msg->size); 371 size = ntohs (msg->size);
368 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size); 372 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
369 pml->msg = (const struct GNUNET_MessageHeader *) &pml[1]; 373 pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
370 pml->can_drop = can_drop; 374 pml->can_drop = can_drop;
371#if DEBUG_SERVER_NC 375#if DEBUG_SERVER_NC
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 376 LOG (GNUNET_ERROR_TYPE_DEBUG,
373 "Adding message of type %u and size %u to pending queue (which has %u entries)\n", 377 "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
374 ntohs (msg->type), ntohs (msg->size), 378 ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
375 (unsigned int) nc->queue_length);
376#endif 379#endif
377 memcpy (&pml[1], msg, size); 380 memcpy (&pml[1], msg, size);
378 /* append */ 381 /* append */
379 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail, 382 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head,
380 pml); 383 client->pending_tail, pml);
381 if (client->th == NULL) 384 if (client->th == NULL)
382 client->th = 385 client->th =
383 GNUNET_SERVER_notify_transmit_ready (client->client, 386 GNUNET_SERVER_notify_transmit_ready (client->client,
384 ntohs (client->pending_head-> 387 ntohs (client->pending_head->
385 msg->size), 388 msg->size),
386 GNUNET_TIME_UNIT_FOREVER_REL, 389 GNUNET_TIME_UNIT_FOREVER_REL,
387 &transmit_message, client); 390 &transmit_message, client);
388} 391}
389 392
390 393
@@ -399,21 +402,22 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
399 */ 402 */
400void 403void
401GNUNET_SERVER_notification_context_unicast (struct 404GNUNET_SERVER_notification_context_unicast (struct
402 GNUNET_SERVER_NotificationContext 405 GNUNET_SERVER_NotificationContext
403 *nc, 406 *nc,
404 struct GNUNET_SERVER_Client *client, 407 struct GNUNET_SERVER_Client
405 const struct GNUNET_MessageHeader 408 *client,
406 *msg, int can_drop) 409 const struct GNUNET_MessageHeader
410 *msg, int can_drop)
407{ 411{
408 struct ClientList *pos; 412 struct ClientList *pos;
409 413
410 pos = nc->clients; 414 pos = nc->clients;
411 while (NULL != pos) 415 while (NULL != pos)
412 { 416 {
413 if (pos->client == client) 417 if (pos->client == client)
414 break; 418 break;
415 pos = pos->next; 419 pos = pos->next;
416 } 420 }
417 GNUNET_assert (pos != NULL); 421 GNUNET_assert (pos != NULL);
418 do_unicast (nc, pos, msg, can_drop); 422 do_unicast (nc, pos, msg, can_drop);
419} 423}
@@ -428,19 +432,20 @@ GNUNET_SERVER_notification_context_unicast (struct
428 */ 432 */
429void 433void
430GNUNET_SERVER_notification_context_broadcast (struct 434GNUNET_SERVER_notification_context_broadcast (struct
431 GNUNET_SERVER_NotificationContext 435 GNUNET_SERVER_NotificationContext
432 *nc, 436 *nc,
433 const struct GNUNET_MessageHeader 437 const struct
434 *msg, int can_drop) 438 GNUNET_MessageHeader *msg,
439 int can_drop)
435{ 440{
436 struct ClientList *pos; 441 struct ClientList *pos;
437 442
438 pos = nc->clients; 443 pos = nc->clients;
439 while (NULL != pos) 444 while (NULL != pos)
440 { 445 {
441 do_unicast (nc, pos, msg, can_drop); 446 do_unicast (nc, pos, msg, can_drop);
442 pos = pos->next; 447 pos = pos->next;
443 } 448 }
444} 449}
445 450
446 451
diff --git a/src/util/server_tc.c b/src/util/server_tc.c
index 5e9a9e946..020fd86db 100644
--- a/src/util/server_tc.c
+++ b/src/util/server_tc.c
@@ -33,6 +33,8 @@
33#include "gnunet_time_lib.h" 33#include "gnunet_time_lib.h"
34 34
35 35
36#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37
36 38
37/** 39/**
38 * How much buffer space do we want to have at least 40 * How much buffer space do we want to have at least
@@ -81,10 +83,10 @@ transmit_response (void *cls, size_t size, void *buf)
81 size_t msize; 83 size_t msize;
82 84
83 if (buf == NULL) 85 if (buf == NULL)
84 { 86 {
85 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); 87 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
86 return 0; 88 return 0;
87 } 89 }
88 if (tc->total - tc->off > size) 90 if (tc->total - tc->off > size)
89 msize = size; 91 msize = size;
90 else 92 else
@@ -92,27 +94,28 @@ transmit_response (void *cls, size_t size, void *buf)
92 memcpy (buf, &tc->buf[tc->off], msize); 94 memcpy (buf, &tc->buf[tc->off], msize);
93 tc->off += msize; 95 tc->off += msize;
94 if (tc->total == tc->off) 96 if (tc->total == tc->off)
95 { 97 {
96 98
97 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK); 99 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
98 GNUNET_SERVER_client_drop (tc->client); 100 GNUNET_SERVER_client_drop (tc->client);
99 GNUNET_free_non_null (tc->buf); 101 GNUNET_free_non_null (tc->buf);
100 GNUNET_free (tc); 102 GNUNET_free (tc);
101 } 103 }
102 else 104 else
103 {
104 if (NULL ==
105 GNUNET_SERVER_notify_transmit_ready (tc->client,
106 GNUNET_MIN (MIN_BLOCK_SIZE,
107 tc->total - tc->off),
108 GNUNET_TIME_absolute_get_remaining
109 (tc->timeout), &transmit_response,
110 tc))
111 { 105 {
112 GNUNET_break (0); 106 if (NULL ==
113 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); 107 GNUNET_SERVER_notify_transmit_ready (tc->client,
108 GNUNET_MIN (MIN_BLOCK_SIZE,
109 tc->total -
110 tc->off),
111 GNUNET_TIME_absolute_get_remaining
112 (tc->timeout),
113 &transmit_response, tc))
114 {
115 GNUNET_break (0);
116 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
117 }
114 } 118 }
115 }
116 return msize; 119 return msize;
117} 120}
118 121
@@ -148,9 +151,10 @@ GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
148 * @param type type of the message 151 * @param type type of the message
149 */ 152 */
150void 153void
151GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext 154GNUNET_SERVER_transmit_context_append_data (struct
152 *tc, const void *data, 155 GNUNET_SERVER_TransmitContext *tc,
153 size_t length, uint16_t type) 156 const void *data, size_t length,
157 uint16_t type)
154{ 158{
155 struct GNUNET_MessageHeader *msg; 159 struct GNUNET_MessageHeader *msg;
156 size_t size; 160 size_t size;
@@ -177,10 +181,10 @@ GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
177 */ 181 */
178void 182void
179GNUNET_SERVER_transmit_context_append_message (struct 183GNUNET_SERVER_transmit_context_append_message (struct
180 GNUNET_SERVER_TransmitContext 184 GNUNET_SERVER_TransmitContext
181 *tc, 185 *tc,
182 const struct GNUNET_MessageHeader 186 const struct
183 *msg) 187 GNUNET_MessageHeader *msg)
184{ 188{
185 struct GNUNET_MessageHeader *m; 189 struct GNUNET_MessageHeader *m;
186 uint16_t size; 190 uint16_t size;
@@ -204,18 +208,18 @@ GNUNET_SERVER_transmit_context_append_message (struct
204 */ 208 */
205void 209void
206GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc, 210GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
207 struct GNUNET_TIME_Relative timeout) 211 struct GNUNET_TIME_Relative timeout)
208{ 212{
209 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout); 213 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
210 if (NULL == 214 if (NULL ==
211 GNUNET_SERVER_notify_transmit_ready (tc->client, 215 GNUNET_SERVER_notify_transmit_ready (tc->client,
212 GNUNET_MIN (MIN_BLOCK_SIZE, 216 GNUNET_MIN (MIN_BLOCK_SIZE,
213 tc->total), timeout, 217 tc->total), timeout,
214 &transmit_response, tc)) 218 &transmit_response, tc))
215 { 219 {
216 GNUNET_break (0); 220 GNUNET_break (0);
217 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); 221 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
218 } 222 }
219} 223}
220 224
221 225
@@ -233,8 +237,7 @@ GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
233 */ 237 */
234void 238void
235GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext 239GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
236 *tc, 240 *tc, int success)
237 int success)
238{ 241{
239 GNUNET_SERVER_receive_done (tc->client, success); 242 GNUNET_SERVER_receive_done (tc->client, success);
240 GNUNET_SERVER_client_drop (tc->client); 243 GNUNET_SERVER_client_drop (tc->client);
diff --git a/src/util/service.c b/src/util/service.c
index 15937e117..7cafe35c8 100644
--- a/src/util/service.c
+++ b/src/util/service.c
@@ -36,6 +36,12 @@
36#include "gnunet_server_lib.h" 36#include "gnunet_server_lib.h"
37#include "gnunet_service_lib.h" 37#include "gnunet_service_lib.h"
38 38
39#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40
41#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
42
43#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
44
39#define DEBUG_SERVICE GNUNET_EXTRA_LOGGING 45#define DEBUG_SERVICE GNUNET_EXTRA_LOGGING
40 46
41/* ******************* access control ******************** */ 47/* ******************* access control ******************** */
@@ -98,120 +104,121 @@ parse_ipv4_specification (const char *routeList)
98 i = 0; 104 i = 0;
99 pos = 0; 105 pos = 0;
100 while (i < count) 106 while (i < count)
101 {
102 cnt =
103 sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
104 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
105 &temps[6], &temps[7]);
106 if (cnt == 8)
107 {
108 for (j = 0; j < 8; j++)
109 if (temps[j] > 0xFF)
110 {
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
113 GNUNET_free (result);
114 return NULL;
115 }
116 result[i].network.s_addr =
117 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
118 temps[3]);
119 result[i].netmask.s_addr =
120 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
121 temps[7]);
122 while (routeList[pos] != ';')
123 pos++;
124 pos++;
125 i++;
126 continue;
127 }
128 /* try second notation */
129 cnt =
130 sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
131 &temps[2], &temps[3], &slash);
132 if (cnt == 5)
133 { 107 {
134 for (j = 0; j < 4; j++) 108 cnt =
135 if (temps[j] > 0xFF) 109 sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
136 { 110 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 111 &temps[6], &temps[7]);
138 _("Invalid format for IP: `%s'\n"), &routeList[pos]); 112 if (cnt == 8)
139 GNUNET_free (result); 113 {
140 return NULL; 114 for (j = 0; j < 8; j++)
141 } 115 if (temps[j] > 0xFF)
142 result[i].network.s_addr = 116 {
143 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + 117 LOG (GNUNET_ERROR_TYPE_ERROR,
144 temps[3]); 118 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
145 if ((slash <= 32) && (slash >= 0)) 119 GNUNET_free (result);
146 { 120 return NULL;
147 result[i].netmask.s_addr = 0; 121 }
148 while (slash > 0) 122 result[i].network.s_addr =
149 { 123 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
150 result[i].netmask.s_addr = 124 temps[3]);
151 (result[i].netmask.s_addr >> 1) + 0x80000000; 125 result[i].netmask.s_addr =
152 slash--; 126 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
153 } 127 temps[7]);
154 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); 128 while (routeList[pos] != ';')
155 while (routeList[pos] != ';') 129 pos++;
156 pos++; 130 pos++;
157 pos++; 131 i++;
158 i++; 132 continue;
159 continue; 133 }
160 } 134 /* try second notation */
161 else 135 cnt =
162 { 136 sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 137 &temps[2], &temps[3], &slash);
164 _ 138 if (cnt == 5)
165 ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), 139 {
166 slash); 140 for (j = 0; j < 4; j++)
167 GNUNET_free (result); 141 if (temps[j] > 0xFF)
168 return NULL; /* error */ 142 {
169 } 143 LOG (GNUNET_ERROR_TYPE_ERROR,
144 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
145 GNUNET_free (result);
146 return NULL;
147 }
148 result[i].network.s_addr =
149 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
150 temps[3]);
151 if ((slash <= 32) && (slash >= 0))
152 {
153 result[i].netmask.s_addr = 0;
154 while (slash > 0)
155 {
156 result[i].netmask.s_addr =
157 (result[i].netmask.s_addr >> 1) + 0x80000000;
158 slash--;
159 }
160 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
161 while (routeList[pos] != ';')
162 pos++;
163 pos++;
164 i++;
165 continue;
166 }
167 else
168 {
169 LOG (GNUNET_ERROR_TYPE_ERROR,
170 _
171 ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
172 slash);
173 GNUNET_free (result);
174 return NULL; /* error */
175 }
176 }
177 /* try third notation */
178 slash = 32;
179 cnt =
180 sscanf (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
181 &temps[2], &temps[3]);
182 if (cnt == 4)
183 {
184 for (j = 0; j < 4; j++)
185 if (temps[j] > 0xFF)
186 {
187 LOG (GNUNET_ERROR_TYPE_ERROR,
188 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
189 GNUNET_free (result);
190 return NULL;
191 }
192 result[i].network.s_addr =
193 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
194 temps[3]);
195 result[i].netmask.s_addr = 0;
196 while (slash > 0)
197 {
198 result[i].netmask.s_addr =
199 (result[i].netmask.s_addr >> 1) + 0x80000000;
200 slash--;
201 }
202 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
203 while (routeList[pos] != ';')
204 pos++;
205 pos++;
206 i++;
207 continue;
208 }
209 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
210 &routeList[pos]);
211 GNUNET_free (result);
212 return NULL; /* error */
170 } 213 }
171 /* try third notation */ 214 if (pos < strlen (routeList))
172 slash = 32;
173 cnt =
174 sscanf (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
175 &temps[2], &temps[3]);
176 if (cnt == 4)
177 { 215 {
178 for (j = 0; j < 4; j++) 216 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
179 if (temps[j] > 0xFF) 217 &routeList[pos]);
180 { 218 GNUNET_free (result);
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 219 return NULL; /* oops */
182 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
183 GNUNET_free (result);
184 return NULL;
185 }
186 result[i].network.s_addr =
187 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
188 temps[3]);
189 result[i].netmask.s_addr = 0;
190 while (slash > 0)
191 {
192 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
193 slash--;
194 }
195 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
196 while (routeList[pos] != ';')
197 pos++;
198 pos++;
199 i++;
200 continue;
201 } 220 }
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), 221 return result; /* ok */
203 &routeList[pos]);
204 GNUNET_free (result);
205 return NULL; /* error */
206 }
207 if (pos < strlen (routeList))
208 {
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
210 &routeList[pos]);
211 GNUNET_free (result);
212 return NULL; /* oops */
213 }
214 return result; /* ok */
215} 222}
216 223
217 224
@@ -253,84 +260,85 @@ parse_ipv6_specification (const char *routeListX)
253 if (routeList[i] == ';') 260 if (routeList[i] == ';')
254 count++; 261 count++;
255 if (routeList[len - 1] != ';') 262 if (routeList[len - 1] != ';')
256 { 263 {
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 264 LOG (GNUNET_ERROR_TYPE_ERROR,
258 _("Invalid network notation (does not end with ';': `%s')\n"), 265 _("Invalid network notation (does not end with ';': `%s')\n"),
259 routeList); 266 routeList);
260 GNUNET_free (routeList); 267 GNUNET_free (routeList);
261 return NULL; 268 return NULL;
262 } 269 }
263 270
264 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1)); 271 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
265 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1)); 272 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
266 i = 0; 273 i = 0;
267 pos = 0; 274 pos = 0;
268 while (i < count) 275 while (i < count)
269 {
270 start = pos;
271 while (routeList[pos] != ';')
272 pos++;
273 slash = pos;
274 while ((slash >= start) && (routeList[slash] != '/'))
275 slash--;
276 if (slash < start)
277 { 276 {
278 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); 277 start = pos;
278 while (routeList[pos] != ';')
279 pos++;
279 slash = pos; 280 slash = pos;
280 } 281 while ((slash >= start) && (routeList[slash] != '/'))
281 else 282 slash--;
282 { 283 if (slash < start)
283 routeList[pos] = '\0'; 284 {
284 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask); 285 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
285 if (ret <= 0) 286 slash = pos;
286 { 287 }
287 save = errno;
288 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
289 {
290 if (ret == 0)
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 _("Wrong format `%s' for netmask\n"),
293 &routeList[slash + 1]);
294 else
295 {
296 errno = save;
297 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
298 }
299 GNUNET_free (result);
300 GNUNET_free (routeList);
301 return NULL;
302 }
303 off = 0;
304 while (bits > 8)
305 {
306 result[i].netmask.s6_addr[off++] = 0xFF;
307 bits -= 8;
308 }
309 while (bits > 0)
310 {
311 result[i].netmask.s6_addr[off] =
312 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
313 bits--;
314 }
315 }
316 }
317 routeList[slash] = '\0';
318 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
319 if (ret <= 0)
320 {
321 if (ret == 0)
322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
323 _("Wrong format `%s' for network\n"),
324 &routeList[slash + 1]);
325 else 288 else
326 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); 289 {
327 GNUNET_free (result); 290 routeList[pos] = '\0';
328 GNUNET_free (routeList); 291 ret =
329 return NULL; 292 inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
293 if (ret <= 0)
294 {
295 save = errno;
296 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits))
297 || (bits >= 128))
298 {
299 if (ret == 0)
300 LOG (GNUNET_ERROR_TYPE_ERROR,
301 _("Wrong format `%s' for netmask\n"),
302 &routeList[slash + 1]);
303 else
304 {
305 errno = save;
306 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
307 }
308 GNUNET_free (result);
309 GNUNET_free (routeList);
310 return NULL;
311 }
312 off = 0;
313 while (bits > 8)
314 {
315 result[i].netmask.s6_addr[off++] = 0xFF;
316 bits -= 8;
317 }
318 while (bits > 0)
319 {
320 result[i].netmask.s6_addr[off] =
321 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
322 bits--;
323 }
324 }
325 }
326 routeList[slash] = '\0';
327 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
328 if (ret <= 0)
329 {
330 if (ret == 0)
331 LOG (GNUNET_ERROR_TYPE_ERROR,
332 _("Wrong format `%s' for network\n"), &routeList[slash + 1]);
333 else
334 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
335 GNUNET_free (result);
336 GNUNET_free (routeList);
337 return NULL;
338 }
339 pos++;
340 i++;
330 } 341 }
331 pos++;
332 i++;
333 }
334 GNUNET_free (routeList); 342 GNUNET_free (routeList);
335 return result; 343 return result;
336} 344}
@@ -344,7 +352,8 @@ parse_ipv6_specification (const char *routeListX)
344 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is 352 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
345 */ 353 */
346static int 354static int
347check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add) 355check_ipv4_listed (const struct IPv4NetworkSet *list,
356 const struct in_addr *add)
348{ 357{
349 int i; 358 int i;
350 359
@@ -353,12 +362,12 @@ check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
353 return GNUNET_NO; 362 return GNUNET_NO;
354 363
355 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0)) 364 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
356 { 365 {
357 if ((add->s_addr & list[i].netmask.s_addr) == 366 if ((add->s_addr & list[i].netmask.s_addr) ==
358 (list[i].network.s_addr & list[i].netmask.s_addr)) 367 (list[i].network.s_addr & list[i].netmask.s_addr))
359 return GNUNET_YES; 368 return GNUNET_YES;
360 i++; 369 i++;
361 } 370 }
362 return GNUNET_NO; 371 return GNUNET_NO;
363} 372}
364 373
@@ -370,7 +379,8 @@ check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
370 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is 379 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
371 */ 380 */
372static int 381static int
373check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip) 382check_ipv6_listed (const struct IPv6NetworkSet *list,
383 const struct in6_addr *ip)
374{ 384{
375 unsigned int i; 385 unsigned int i;
376 unsigned int j; 386 unsigned int j;
@@ -383,16 +393,16 @@ check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
383 i = 0; 393 i = 0;
384NEXT: 394NEXT:
385 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0) 395 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
386 { 396 {
387 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++) 397 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
388 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) != 398 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
389 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j])) 399 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
390 { 400 {
391 i++; 401 i++;
392 goto NEXT; 402 goto NEXT;
393 } 403 }
394 return GNUNET_YES; 404 return GNUNET_YES;
395 } 405 }
396 return GNUNET_NO; 406 return GNUNET_NO;
397} 407}
398 408
@@ -526,10 +536,10 @@ write_test (void *cls, size_t size, void *buf)
526 struct GNUNET_MessageHeader *msg; 536 struct GNUNET_MessageHeader *msg;
527 537
528 if (size < sizeof (struct GNUNET_MessageHeader)) 538 if (size < sizeof (struct GNUNET_MessageHeader))
529 { 539 {
530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
531 return 0; /* client disconnected */ 541 return 0; /* client disconnected */
532 } 542 }
533 msg = (struct GNUNET_MessageHeader *) buf; 543 msg = (struct GNUNET_MessageHeader *) buf;
534 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); 544 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
535 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 545 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
@@ -546,14 +556,15 @@ write_test (void *cls, size_t size, void *buf)
546 */ 556 */
547static void 557static void
548handle_test (void *cls, struct GNUNET_SERVER_Client *client, 558handle_test (void *cls, struct GNUNET_SERVER_Client *client,
549 const struct GNUNET_MessageHeader *message) 559 const struct GNUNET_MessageHeader *message)
550{ 560{
551 /* simply bounce message back to acknowledge */ 561 /* simply bounce message back to acknowledge */
552 if (NULL == 562 if (NULL ==
553 GNUNET_SERVER_notify_transmit_ready (client, 563 GNUNET_SERVER_notify_transmit_ready (client,
554 sizeof (struct GNUNET_MessageHeader), 564 sizeof (struct
555 GNUNET_TIME_UNIT_FOREVER_REL, 565 GNUNET_MessageHeader),
556 &write_test, client)) 566 GNUNET_TIME_UNIT_FOREVER_REL,
567 &write_test, client))
557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
558} 569}
559 570
@@ -586,7 +597,7 @@ static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
586 */ 597 */
587static int 598static int
588check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, 599check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
589 const struct sockaddr *addr, socklen_t addrlen) 600 const struct sockaddr *addr, socklen_t addrlen)
590{ 601{
591 struct GNUNET_SERVICE_Context *sctx = cls; 602 struct GNUNET_SERVICE_Context *sctx = cls;
592 const struct sockaddr_in *i4; 603 const struct sockaddr_in *i4;
@@ -594,51 +605,51 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
594 int ret; 605 int ret;
595 606
596 switch (addr->sa_family) 607 switch (addr->sa_family)
597 { 608 {
598 case AF_INET: 609 case AF_INET:
599 GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); 610 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
600 i4 = (const struct sockaddr_in *) addr; 611 i4 = (const struct sockaddr_in *) addr;
601 ret = ((sctx->v4_allowed == NULL) || 612 ret = ((sctx->v4_allowed == NULL) ||
602 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) && 613 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
603 ((sctx->v4_denied == NULL) || 614 ((sctx->v4_denied == NULL) ||
604 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr))); 615 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
605 break; 616 break;
606 case AF_INET6: 617 case AF_INET6:
607 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); 618 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
608 i6 = (const struct sockaddr_in6 *) addr; 619 i6 = (const struct sockaddr_in6 *) addr;
609 ret = ((sctx->v6_allowed == NULL) || 620 ret = ((sctx->v6_allowed == NULL) ||
610 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) && 621 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
611 ((sctx->v6_denied == NULL) || 622 ((sctx->v6_denied == NULL) ||
612 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr))); 623 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
613 break; 624 break;
614#ifndef WINDOWS 625#ifndef WINDOWS
615 case AF_UNIX: 626 case AF_UNIX:
616 ret = GNUNET_OK; /* always OK for now */ 627 ret = GNUNET_OK; /* always OK for now */
617 if ((sctx->match_uid == GNUNET_YES) || (sctx->match_gid == GNUNET_YES)) 628 if ((sctx->match_uid == GNUNET_YES) || (sctx->match_gid == GNUNET_YES))
618 ret = GNUNET_NO; 629 ret = GNUNET_NO;
619 if ((uc != NULL) && 630 if ((uc != NULL) &&
620 ((sctx->match_uid != GNUNET_YES) || (uc->uid == geteuid ()) || 631 ((sctx->match_uid != GNUNET_YES) || (uc->uid == geteuid ()) ||
621 (uc->uid == getuid ())) && ((sctx->match_gid != GNUNET_YES) || 632 (uc->uid == getuid ())) && ((sctx->match_gid != GNUNET_YES) ||
622 (uc->gid == getegid ()) || 633 (uc->gid == getegid ()) ||
623 (uc->gid == getgid ()))) 634 (uc->gid == getgid ())))
624 ret = GNUNET_YES; 635 ret = GNUNET_YES;
625 else 636 else
626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 637 LOG (GNUNET_ERROR_TYPE_WARNING,
627 _("Access denied to UID %d / GID %d\n"), 638 _("Access denied to UID %d / GID %d\n"),
628 (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid); 639 (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid);
629 break; 640 break;
630#endif 641#endif
631 default: 642 default:
632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"), 643 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
633 addr->sa_family); 644 addr->sa_family);
634 return GNUNET_SYSERR; 645 return GNUNET_SYSERR;
635 } 646 }
636 if (ret != GNUNET_OK) 647 if (ret != GNUNET_OK)
637 { 648 {
638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 649 LOG (GNUNET_ERROR_TYPE_WARNING,
639 _("Access from `%s' denied to service `%s'\n"), 650 _("Access from `%s' denied to service `%s'\n"),
640 GNUNET_a2s (addr, addrlen), sctx->serviceName); 651 GNUNET_a2s (addr, addrlen), sctx->serviceName);
641 } 652 }
642 return ret; 653 return ret;
643} 654}
644 655
@@ -655,7 +666,7 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
655 666
656 if (GNUNET_OK != 667 if (GNUNET_OK !=
657 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName, 668 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
658 "PIDFILE", &pif)) 669 "PIDFILE", &pif))
659 return NULL; 670 return NULL;
660 return pif; 671 return pif;
661} 672}
@@ -665,26 +676,26 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
665 * Parse an IPv4 access control list. 676 * Parse an IPv4 access control list.
666 */ 677 */
667static int 678static int
668process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, 679process_acl4 (struct IPv4NetworkSet **ret,
669 const char *option) 680 struct GNUNET_SERVICE_Context *sctx, const char *option)
670{ 681{
671 char *opt; 682 char *opt;
672 683
673 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option)) 684 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
674 return GNUNET_OK; 685 return GNUNET_OK;
675 GNUNET_break (GNUNET_OK == 686 GNUNET_break (GNUNET_OK ==
676 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, 687 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
677 sctx->serviceName, 688 sctx->serviceName,
678 option, &opt)); 689 option, &opt));
679 if (NULL == (*ret = parse_ipv4_specification (opt))) 690 if (NULL == (*ret = parse_ipv4_specification (opt)))
680 { 691 {
681 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 692 LOG (GNUNET_ERROR_TYPE_WARNING,
682 _ 693 _
683 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"), 694 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
684 opt, sctx->serviceName, option); 695 opt, sctx->serviceName, option);
685 GNUNET_free (opt); 696 GNUNET_free (opt);
686 return GNUNET_SYSERR; 697 return GNUNET_SYSERR;
687 } 698 }
688 GNUNET_free (opt); 699 GNUNET_free (opt);
689 return GNUNET_OK; 700 return GNUNET_OK;
690} 701}
@@ -694,26 +705,26 @@ process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
694 * Parse an IPv4 access control list. 705 * Parse an IPv4 access control list.
695 */ 706 */
696static int 707static int
697process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, 708process_acl6 (struct IPv6NetworkSet **ret,
698 const char *option) 709 struct GNUNET_SERVICE_Context *sctx, const char *option)
699{ 710{
700 char *opt; 711 char *opt;
701 712
702 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option)) 713 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
703 return GNUNET_OK; 714 return GNUNET_OK;
704 GNUNET_break (GNUNET_OK == 715 GNUNET_break (GNUNET_OK ==
705 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, 716 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
706 sctx->serviceName, 717 sctx->serviceName,
707 option, &opt)); 718 option, &opt));
708 if (NULL == (*ret = parse_ipv6_specification (opt))) 719 if (NULL == (*ret = parse_ipv6_specification (opt)))
709 { 720 {
710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 721 LOG (GNUNET_ERROR_TYPE_WARNING,
711 _ 722 _
712 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"), 723 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
713 opt, sctx->serviceName, option); 724 opt, sctx->serviceName, option);
714 GNUNET_free (opt); 725 GNUNET_free (opt);
715 return GNUNET_SYSERR; 726 return GNUNET_SYSERR;
716 } 727 }
717 GNUNET_free (opt); 728 GNUNET_free (opt);
718 return GNUNET_OK; 729 return GNUNET_OK;
719} 730}
@@ -728,7 +739,7 @@ process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
728 */ 739 */
729static void 740static void
730add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens, 741add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
731 const char *unixpath) 742 const char *unixpath)
732{ 743{
733#ifdef AF_UNIX 744#ifdef AF_UNIX
734 struct sockaddr_un *un; 745 struct sockaddr_un *un;
@@ -780,9 +791,9 @@ add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
780 */ 791 */
781int 792int
782GNUNET_SERVICE_get_server_addresses (const char *serviceName, 793GNUNET_SERVICE_get_server_addresses (const char *serviceName,
783 const struct GNUNET_CONFIGURATION_Handle 794 const struct GNUNET_CONFIGURATION_Handle
784 *cfg, struct sockaddr ***addrs, 795 *cfg, struct sockaddr ***addrs,
785 socklen_t ** addr_lens) 796 socklen_t ** addr_lens)
786{ 797{
787 int disablev6; 798 int disablev6;
788 struct GNUNET_NETWORK_Handle *desc; 799 struct GNUNET_NETWORK_Handle *desc;
@@ -803,62 +814,64 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
803 *addr_lens = NULL; 814 *addr_lens = NULL;
804 desc = NULL; 815 desc = NULL;
805 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6")) 816 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6"))
806 { 817 {
807 if (GNUNET_SYSERR == 818 if (GNUNET_SYSERR ==
808 (disablev6 = 819 (disablev6 =
809 GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, "DISABLEV6"))) 820 GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName,
810 return GNUNET_SYSERR; 821 "DISABLEV6")))
811 } 822 return GNUNET_SYSERR;
823 }
812 else 824 else
813 disablev6 = GNUNET_NO; 825 disablev6 = GNUNET_NO;
814 826
815 if (!disablev6) 827 if (!disablev6)
816 {
817 /* probe IPv6 support */
818 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
819 if (NULL == desc)
820 { 828 {
821 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || 829 /* probe IPv6 support */
822 (errno == EACCES)) 830 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
823 { 831 if (NULL == desc)
824 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); 832 {
825 return GNUNET_SYSERR; 833 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
826 } 834 (errno == EACCES))
827 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 835 {
828 _ 836 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
829 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"), 837 return GNUNET_SYSERR;
830 serviceName, STRERROR (errno)); 838 }
831 disablev6 = GNUNET_YES; 839 LOG (GNUNET_ERROR_TYPE_INFO,
832 } 840 _
833 else 841 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
834 { 842 serviceName, STRERROR (errno));
835 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc)); 843 disablev6 = GNUNET_YES;
836 desc = NULL; 844 }
845 else
846 {
847 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
848 desc = NULL;
849 }
837 } 850 }
838 }
839 851
840 port = 0; 852 port = 0;
841 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT")) 853 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
842 {
843 GNUNET_break (GNUNET_OK ==
844 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
845 "PORT", &port));
846 if (port > 65535)
847 { 854 {
848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 855 GNUNET_break (GNUNET_OK ==
849 _ 856 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
850 ("Require valid port number for service `%s' in configuration!\n"), 857 "PORT", &port));
851 serviceName); 858 if (port > 65535)
852 return GNUNET_SYSERR; 859 {
860 LOG (GNUNET_ERROR_TYPE_ERROR,
861 _
862 ("Require valid port number for service `%s' in configuration!\n"),
863 serviceName);
864 return GNUNET_SYSERR;
865 }
853 } 866 }
854 }
855 867
856 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO")) 868 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
857 { 869 {
858 GNUNET_break (GNUNET_OK == 870 GNUNET_break (GNUNET_OK ==
859 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, 871 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
860 "BINDTO", &hostname)); 872 "BINDTO",
861 } 873 &hostname));
874 }
862 else 875 else
863 hostname = NULL; 876 hostname = NULL;
864 877
@@ -868,211 +881,211 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
868 GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) && 881 GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) &&
869 (GNUNET_OK == 882 (GNUNET_OK ==
870 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH", 883 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH",
871 &unixpath)) && 884 &unixpath)) &&
872 (0 < strlen (unixpath))) 885 (0 < strlen (unixpath)))
873 {
874 /* probe UNIX support */
875 struct sockaddr_un s_un;
876
877 if (strlen (unixpath) >= sizeof (s_un.sun_path))
878 {
879 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
880 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
881 unixpath, sizeof (s_un.sun_path));
882 GNUNET_free_non_null (hostname);
883 GNUNET_free (unixpath);
884 return GNUNET_SYSERR;
885 }
886
887 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
888 if (NULL == desc)
889 { 886 {
890 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || 887 /* probe UNIX support */
891 (errno == EACCES)) 888 struct sockaddr_un s_un;
892 { 889
893 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); 890 if (strlen (unixpath) >= sizeof (s_un.sun_path))
894 GNUNET_free_non_null (hostname); 891 {
895 GNUNET_free (unixpath); 892 LOG (GNUNET_ERROR_TYPE_WARNING,
896 return GNUNET_SYSERR; 893 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
897 } 894 unixpath, sizeof (s_un.sun_path));
898 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 895 GNUNET_free_non_null (hostname);
899 _ 896 GNUNET_free (unixpath);
900 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"), 897 return GNUNET_SYSERR;
901 serviceName, STRERROR (errno)); 898 }
902 GNUNET_free (unixpath); 899
903 unixpath = NULL; 900 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
904 } 901 if (NULL == desc)
905 else 902 {
906 { 903 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
907 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc)); 904 (errno == EACCES))
908 desc = NULL; 905 {
906 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
907 GNUNET_free_non_null (hostname);
908 GNUNET_free (unixpath);
909 return GNUNET_SYSERR;
910 }
911 LOG (GNUNET_ERROR_TYPE_INFO,
912 _
913 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
914 serviceName, STRERROR (errno));
915 GNUNET_free (unixpath);
916 unixpath = NULL;
917 }
918 else
919 {
920 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
921 desc = NULL;
922 }
909 } 923 }
910 }
911#endif 924#endif
912 925
913 if ((port == 0) && (unixpath == NULL)) 926 if ((port == 0) && (unixpath == NULL))
914 {
915 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
916 _
917 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
918 serviceName);
919 GNUNET_free_non_null (hostname);
920 return GNUNET_SYSERR;
921 }
922 if (port == 0)
923 {
924 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
925 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
926 add_unixpath (saddrs, saddrlens, unixpath);
927 GNUNET_free_non_null (unixpath);
928 GNUNET_free_non_null (hostname);
929 *addrs = saddrs;
930 *addr_lens = saddrlens;
931 return 1;
932 }
933
934 if (hostname != NULL)
935 {
936#if DEBUG_SERVICE
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Resolving `%s' since that is where `%s' will bind to.\n",
939 hostname, serviceName);
940#endif
941 memset (&hints, 0, sizeof (struct addrinfo));
942 if (disablev6)
943 hints.ai_family = AF_INET;
944 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
945 (res == NULL))
946 {
947 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
948 hostname, gai_strerror (ret));
949 GNUNET_free (hostname);
950 GNUNET_free_non_null (unixpath);
951 return GNUNET_SYSERR;
952 }
953 next = res;
954 i = 0;
955 while (NULL != (pos = next))
956 {
957 next = pos->ai_next;
958 if ((disablev6) && (pos->ai_family == AF_INET6))
959 continue;
960 i++;
961 }
962 if (0 == i)
963 { 927 {
964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 928 LOG (GNUNET_ERROR_TYPE_ERROR,
965 _("Failed to find %saddress for `%s'.\n"), 929 _
966 disablev6 ? "IPv4 " : "", hostname); 930 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
967 freeaddrinfo (res); 931 serviceName);
968 GNUNET_free (hostname); 932 GNUNET_free_non_null (hostname);
969 GNUNET_free_non_null (unixpath);
970 return GNUNET_SYSERR; 933 return GNUNET_SYSERR;
971 } 934 }
972 resi = i; 935 if (port == 0)
973 if (NULL != unixpath)
974 resi++;
975 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
976 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
977 i = 0;
978 if (NULL != unixpath)
979 { 936 {
937 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
938 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
980 add_unixpath (saddrs, saddrlens, unixpath); 939 add_unixpath (saddrs, saddrlens, unixpath);
981 i++; 940 GNUNET_free_non_null (unixpath);
941 GNUNET_free_non_null (hostname);
942 *addrs = saddrs;
943 *addr_lens = saddrlens;
944 return 1;
982 } 945 }
983 next = res; 946
984 while (NULL != (pos = next)) 947 if (hostname != NULL)
985 { 948 {
986 next = pos->ai_next;
987 if ((disablev6) && (pos->ai_family == AF_INET6))
988 continue;
989 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
990 continue; /* not TCP */
991 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
992 continue; /* huh? */
993#if DEBUG_SERVICE 949#if DEBUG_SERVICE
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n", 950 LOG (GNUNET_ERROR_TYPE_DEBUG,
995 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen)); 951 "Resolving `%s' since that is where `%s' will bind to.\n",
952 hostname, serviceName);
996#endif 953#endif
997 if (pos->ai_family == AF_INET) 954 memset (&hints, 0, sizeof (struct addrinfo));
998 { 955 if (disablev6)
999 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in)); 956 hints.ai_family = AF_INET;
1000 saddrlens[i] = pos->ai_addrlen; 957 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1001 saddrs[i] = GNUNET_malloc (saddrlens[i]); 958 (res == NULL))
1002 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); 959 {
1003 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); 960 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
1004 } 961 hostname, gai_strerror (ret));
1005 else 962 GNUNET_free (hostname);
1006 { 963 GNUNET_free_non_null (unixpath);
1007 GNUNET_assert (pos->ai_family == AF_INET6); 964 return GNUNET_SYSERR;
1008 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6)); 965 }
1009 saddrlens[i] = pos->ai_addrlen; 966 next = res;
1010 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1011 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1012 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1013 }
1014 i++;
1015 }
1016 GNUNET_free (hostname);
1017 freeaddrinfo (res);
1018 resi = i;
1019 }
1020 else
1021 {
1022 /* will bind against everything, just set port */
1023 if (disablev6)
1024 {
1025 /* V4-only */
1026 resi = 1;
1027 if (NULL != unixpath)
1028 resi++;
1029 i = 0; 967 i = 0;
968 while (NULL != (pos = next))
969 {
970 next = pos->ai_next;
971 if ((disablev6) && (pos->ai_family == AF_INET6))
972 continue;
973 i++;
974 }
975 if (0 == i)
976 {
977 LOG (GNUNET_ERROR_TYPE_ERROR,
978 _("Failed to find %saddress for `%s'.\n"),
979 disablev6 ? "IPv4 " : "", hostname);
980 freeaddrinfo (res);
981 GNUNET_free (hostname);
982 GNUNET_free_non_null (unixpath);
983 return GNUNET_SYSERR;
984 }
985 resi = i;
986 if (NULL != unixpath)
987 resi++;
1030 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); 988 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1031 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); 989 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
990 i = 0;
1032 if (NULL != unixpath) 991 if (NULL != unixpath)
1033 { 992 {
1034 add_unixpath (saddrs, saddrlens, unixpath); 993 add_unixpath (saddrs, saddrlens, unixpath);
1035 i++; 994 i++;
1036 } 995 }
1037 saddrlens[i] = sizeof (struct sockaddr_in); 996 next = res;
1038 saddrs[i] = GNUNET_malloc (saddrlens[i]); 997 while (NULL != (pos = next))
1039#if HAVE_SOCKADDR_IN_SIN_LEN 998 {
1040 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i]; 999 next = pos->ai_next;
1000 if ((disablev6) && (pos->ai_family == AF_INET6))
1001 continue;
1002 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1003 continue; /* not TCP */
1004 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1005 continue; /* huh? */
1006#if DEBUG_SERVICE
1007 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1008 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1041#endif 1009#endif
1042 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; 1010 if (pos->ai_family == AF_INET)
1043 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); 1011 {
1012 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
1013 saddrlens[i] = pos->ai_addrlen;
1014 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1015 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1016 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1017 }
1018 else
1019 {
1020 GNUNET_assert (pos->ai_family == AF_INET6);
1021 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1022 saddrlens[i] = pos->ai_addrlen;
1023 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1024 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1025 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1026 }
1027 i++;
1028 }
1029 GNUNET_free (hostname);
1030 freeaddrinfo (res);
1031 resi = i;
1044 } 1032 }
1045 else 1033 else
1046 { 1034 {
1047 /* dual stack */ 1035 /* will bind against everything, just set port */
1048 resi = 2; 1036 if (disablev6)
1049 if (NULL != unixpath) 1037 {
1050 resi++; 1038 /* V4-only */
1051 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); 1039 resi = 1;
1052 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); 1040 if (NULL != unixpath)
1053 i = 0; 1041 resi++;
1054 if (NULL != unixpath) 1042 i = 0;
1055 { 1043 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1056 add_unixpath (saddrs, saddrlens, unixpath); 1044 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1057 i++; 1045 if (NULL != unixpath)
1058 } 1046 {
1059 saddrlens[i] = sizeof (struct sockaddr_in6); 1047 add_unixpath (saddrs, saddrlens, unixpath);
1060 saddrs[i] = GNUNET_malloc (saddrlens[i]); 1048 i++;
1049 }
1050 saddrlens[i] = sizeof (struct sockaddr_in);
1051 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1061#if HAVE_SOCKADDR_IN_SIN_LEN 1052#if HAVE_SOCKADDR_IN_SIN_LEN
1062 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0]; 1053 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1063#endif 1054#endif
1064 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6; 1055 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1065 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port); 1056 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1066 i++; 1057 }
1067 saddrlens[i] = sizeof (struct sockaddr_in); 1058 else
1068 saddrs[i] = GNUNET_malloc (saddrlens[i]); 1059 {
1060 /* dual stack */
1061 resi = 2;
1062 if (NULL != unixpath)
1063 resi++;
1064 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1065 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1066 i = 0;
1067 if (NULL != unixpath)
1068 {
1069 add_unixpath (saddrs, saddrlens, unixpath);
1070 i++;
1071 }
1072 saddrlens[i] = sizeof (struct sockaddr_in6);
1073 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1069#if HAVE_SOCKADDR_IN_SIN_LEN 1074#if HAVE_SOCKADDR_IN_SIN_LEN
1070 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1]; 1075 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1071#endif 1076#endif
1072 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; 1077 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1073 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); 1078 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1079 i++;
1080 saddrlens[i] = sizeof (struct sockaddr_in);
1081 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1082#if HAVE_SOCKADDR_IN_SIN_LEN
1083 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1084#endif
1085 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1086 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1087 }
1074 } 1088 }
1075 }
1076 GNUNET_free_non_null (unixpath); 1089 GNUNET_free_non_null (unixpath);
1077 *addrs = saddrs; 1090 *addrs = saddrs;
1078 *addr_lens = saddrlens; 1091 *addr_lens = saddrlens;
@@ -1111,36 +1124,37 @@ setup_service (struct GNUNET_SERVICE_Context *sctx)
1111 int flags; 1124 int flags;
1112#endif 1125#endif
1113 1126
1114 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TIMEOUT")) 1127 if (GNUNET_CONFIGURATION_have_value
1115 { 1128 (sctx->cfg, sctx->serviceName, "TIMEOUT"))
1116 if (GNUNET_OK !=
1117 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName,
1118 "TIMEOUT", &idleout))
1119 { 1129 {
1120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1130 if (GNUNET_OK !=
1121 _("Specified value for `%s' of service `%s' is invalid\n"), 1131 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName,
1122 "TIMEOUT", sctx->serviceName); 1132 "TIMEOUT", &idleout))
1123 return GNUNET_SYSERR; 1133 {
1134 LOG (GNUNET_ERROR_TYPE_ERROR,
1135 _("Specified value for `%s' of service `%s' is invalid\n"),
1136 "TIMEOUT", sctx->serviceName);
1137 return GNUNET_SYSERR;
1138 }
1139 sctx->timeout = idleout;
1124 } 1140 }
1125 sctx->timeout = idleout;
1126 }
1127 else 1141 else
1128 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL; 1142 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1129 1143
1130 if (GNUNET_CONFIGURATION_have_value 1144 if (GNUNET_CONFIGURATION_have_value
1131 (sctx->cfg, sctx->serviceName, "TOLERANT")) 1145 (sctx->cfg, sctx->serviceName, "TOLERANT"))
1132 {
1133 if (GNUNET_SYSERR ==
1134 (tolerant =
1135 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1136 "TOLERANT")))
1137 { 1146 {
1138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1147 if (GNUNET_SYSERR ==
1139 _("Specified value for `%s' of service `%s' is invalid\n"), 1148 (tolerant =
1140 "TOLERANT", sctx->serviceName); 1149 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1141 return GNUNET_SYSERR; 1150 "TOLERANT")))
1151 {
1152 LOG (GNUNET_ERROR_TYPE_ERROR,
1153 _("Specified value for `%s' of service `%s' is invalid\n"),
1154 "TOLERANT", sctx->serviceName);
1155 return GNUNET_SYSERR;
1156 }
1142 } 1157 }
1143 }
1144 else 1158 else
1145 tolerant = GNUNET_NO; 1159 tolerant = GNUNET_NO;
1146 1160
@@ -1151,45 +1165,48 @@ setup_service (struct GNUNET_SERVICE_Context *sctx)
1151 (NULL != (nfds = getenv ("LISTEN_FDS"))) && 1165 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1152 (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) && 1166 (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1153 (cnt + 4 < FD_SETSIZE)) 1167 (cnt + 4 < FD_SETSIZE))
1154 {
1155 sctx->lsocks =
1156 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1157 while (0 < cnt--)
1158 { 1168 {
1159 flags = fcntl (3 + cnt, F_GETFD); 1169 sctx->lsocks =
1160 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) || 1170 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1161 (NULL == 1171 while (0 < cnt--)
1162 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt)))) 1172 {
1163 { 1173 flags = fcntl (3 + cnt, F_GETFD);
1164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1174 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1165 _ 1175 (NULL ==
1166 ("Could not access pre-bound socket %u, will try to bind myself\n"), 1176 (sctx->lsocks[cnt] =
1167 (unsigned int) 3 + cnt); 1177 GNUNET_NETWORK_socket_box_native (3 + cnt))))
1168 cnt++; 1178 {
1169 while (sctx->lsocks[cnt] != NULL) 1179 LOG (GNUNET_ERROR_TYPE_ERROR,
1170 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++])); 1180 _
1171 GNUNET_free (sctx->lsocks); 1181 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1172 sctx->lsocks = NULL; 1182 (unsigned int) 3 + cnt);
1173 break; 1183 cnt++;
1174 } 1184 while (sctx->lsocks[cnt] != NULL)
1185 GNUNET_break (0 ==
1186 GNUNET_NETWORK_socket_close (sctx->lsocks
1187 [cnt++]));
1188 GNUNET_free (sctx->lsocks);
1189 sctx->lsocks = NULL;
1190 break;
1191 }
1192 }
1193 unsetenv ("LISTEN_PID");
1194 unsetenv ("LISTEN_FDS");
1175 } 1195 }
1176 unsetenv ("LISTEN_PID");
1177 unsetenv ("LISTEN_FDS");
1178 }
1179#endif 1196#endif
1180 1197
1181 if ((sctx->lsocks == NULL) && 1198 if ((sctx->lsocks == NULL) &&
1182 (GNUNET_SYSERR == 1199 (GNUNET_SYSERR ==
1183 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg, 1200 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
1184 &sctx->addrs, &sctx->addrlens))) 1201 &sctx->addrs, &sctx->addrlens)))
1185 return GNUNET_SYSERR; 1202 return GNUNET_SYSERR;
1186 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES; 1203 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1187 sctx->match_uid = 1204 sctx->match_uid =
1188 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, 1205 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1189 "UNIX_MATCH_UID"); 1206 "UNIX_MATCH_UID");
1190 sctx->match_gid = 1207 sctx->match_gid =
1191 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, 1208 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1192 "UNIX_MATCH_GID"); 1209 "UNIX_MATCH_GID");
1193 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM"); 1210 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1194 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM"); 1211 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1195 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6"); 1212 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
@@ -1211,7 +1228,7 @@ get_user_name (struct GNUNET_SERVICE_Context *sctx)
1211 1228
1212 if (GNUNET_OK != 1229 if (GNUNET_OK !=
1213 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName, 1230 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
1214 "USERNAME", &un)) 1231 "USERNAME", &un))
1215 return NULL; 1232 return NULL;
1216 return un; 1233 return un;
1217} 1234}
@@ -1229,7 +1246,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1229 int len; 1246 int len;
1230 1247
1231 if (NULL == (pif = get_pid_file_name (sctx))) 1248 if (NULL == (pif = get_pid_file_name (sctx)))
1232 return GNUNET_OK; /* no file desired */ 1249 return GNUNET_OK; /* no file desired */
1233 user = get_user_name (sctx); 1250 user = get_user_name (sctx);
1234 rdir = GNUNET_strdup (pif); 1251 rdir = GNUNET_strdup (pif);
1235 len = strlen (rdir); 1252 len = strlen (rdir);
@@ -1237,32 +1254,32 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1237 len--; 1254 len--;
1238 rdir[len] = '\0'; 1255 rdir[len] = '\0';
1239 if (0 != ACCESS (rdir, F_OK)) 1256 if (0 != ACCESS (rdir, F_OK))
1240 { 1257 {
1241 /* we get to create a directory -- and claim it 1258 /* we get to create a directory -- and claim it
1242 * as ours! */ 1259 * as ours! */
1243 GNUNET_DISK_directory_create (rdir); 1260 GNUNET_DISK_directory_create (rdir);
1244 if ((user != NULL) && (0 < strlen (user))) 1261 if ((user != NULL) && (0 < strlen (user)))
1245 GNUNET_DISK_file_change_owner (rdir, user); 1262 GNUNET_DISK_file_change_owner (rdir, user);
1246 } 1263 }
1247 if (0 != ACCESS (rdir, W_OK | X_OK)) 1264 if (0 != ACCESS (rdir, W_OK | X_OK))
1248 { 1265 {
1249 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir); 1266 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1250 GNUNET_free (rdir); 1267 GNUNET_free (rdir);
1251 GNUNET_free_non_null (user); 1268 GNUNET_free_non_null (user);
1252 GNUNET_free (pif); 1269 GNUNET_free (pif);
1253 return GNUNET_SYSERR; 1270 return GNUNET_SYSERR;
1254 } 1271 }
1255 GNUNET_free (rdir); 1272 GNUNET_free (rdir);
1256 pidfd = FOPEN (pif, "w"); 1273 pidfd = FOPEN (pif, "w");
1257 if (pidfd == NULL) 1274 if (pidfd == NULL)
1258 { 1275 {
1259 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif); 1276 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1260 GNUNET_free (pif); 1277 GNUNET_free (pif);
1261 GNUNET_free_non_null (user); 1278 GNUNET_free_non_null (user);
1262 return GNUNET_SYSERR; 1279 return GNUNET_SYSERR;
1263 } 1280 }
1264 if (0 > FPRINTF (pidfd, "%u", pid)) 1281 if (0 > FPRINTF (pidfd, "%u", pid))
1265 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif); 1282 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1266 GNUNET_break (0 == fclose (pidfd)); 1283 GNUNET_break (0 == fclose (pidfd));
1267 if ((user != NULL) && (0 < strlen (user))) 1284 if ((user != NULL) && (0 < strlen (user)))
1268 GNUNET_DISK_file_change_owner (pif, user); 1285 GNUNET_DISK_file_change_owner (pif, user);
@@ -1299,35 +1316,35 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1299 GNUNET_RESOLVER_connect (sctx->cfg); 1316 GNUNET_RESOLVER_connect (sctx->cfg);
1300 if (sctx->lsocks != NULL) 1317 if (sctx->lsocks != NULL)
1301 sctx->server = 1318 sctx->server =
1302 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, 1319 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1303 sctx->timeout, sctx->require_found); 1320 sctx->timeout, sctx->require_found);
1304 else 1321 else
1305 sctx->server = 1322 sctx->server =
1306 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, 1323 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1307 sctx->timeout, sctx->require_found); 1324 sctx->timeout, sctx->require_found);
1308 if (sctx->server == NULL) 1325 if (sctx->server == NULL)
1309 {
1310 if (sctx->addrs != NULL)
1311 { 1326 {
1312 i = 0; 1327 if (sctx->addrs != NULL)
1313 while (sctx->addrs[i] != NULL) 1328 {
1314 { 1329 i = 0;
1315 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"), 1330 while (sctx->addrs[i] != NULL)
1316 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], 1331 {
1317 sctx->addrlens[i])); 1332 LOG (GNUNET_ERROR_TYPE_INFO,
1318 i++; 1333 _("Failed to start `%s' at `%s'\n"), sctx->serviceName,
1319 } 1334 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1335 i++;
1336 }
1337 }
1338 sctx->ret = GNUNET_SYSERR;
1339 return;
1320 } 1340 }
1321 sctx->ret = GNUNET_SYSERR;
1322 return;
1323 }
1324 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN)) 1341 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1325 { 1342 {
1326 /* install a task that will kill the server 1343 /* install a task that will kill the server
1327 * process if the scheduler ever gets a shutdown signal */ 1344 * process if the scheduler ever gets a shutdown signal */
1328 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, 1345 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1329 sctx->server); 1346 &shutdown_task, sctx->server);
1330 } 1347 }
1331 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); 1348 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1332 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); 1349 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1333 i = 0; 1350 i = 0;
@@ -1335,23 +1352,23 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1335 sctx->my_handlers[i++].callback_cls = sctx; 1352 sctx->my_handlers[i++].callback_cls = sctx;
1336 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers); 1353 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1337 if (sctx->ready_confirm_fd != -1) 1354 if (sctx->ready_confirm_fd != -1)
1338 { 1355 {
1339 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1)); 1356 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1340 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd)); 1357 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1341 sctx->ready_confirm_fd = -1; 1358 sctx->ready_confirm_fd = -1;
1342 write_pid_file (sctx, getpid ()); 1359 write_pid_file (sctx, getpid ());
1343 } 1360 }
1344 if (sctx->addrs != NULL) 1361 if (sctx->addrs != NULL)
1345 {
1346 i = 0;
1347 while (sctx->addrs[i] != NULL)
1348 { 1362 {
1349 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"), 1363 i = 0;
1350 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], 1364 while (sctx->addrs[i] != NULL)
1351 sctx->addrlens[i])); 1365 {
1352 i++; 1366 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1367 sctx->serviceName, GNUNET_a2s (sctx->addrs[i],
1368 sctx->addrlens[i]));
1369 i++;
1370 }
1353 } 1371 }
1354 }
1355 sctx->task (sctx->task_cls, sctx->server, sctx->cfg); 1372 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1356} 1373}
1357 1374
@@ -1368,45 +1385,45 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1368 int filedes[2]; 1385 int filedes[2];
1369 1386
1370 if (0 != PIPE (filedes)) 1387 if (0 != PIPE (filedes))
1371 { 1388 {
1372 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); 1389 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1373 return GNUNET_SYSERR; 1390 return GNUNET_SYSERR;
1374 } 1391 }
1375 pid = fork (); 1392 pid = fork ();
1376 if (pid < 0) 1393 if (pid < 0)
1377 { 1394 {
1378 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); 1395 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1379 return GNUNET_SYSERR; 1396 return GNUNET_SYSERR;
1380 } 1397 }
1381 if (pid != 0) 1398 if (pid != 0)
1382 {
1383 /* Parent */
1384 char c;
1385
1386 GNUNET_break (0 == CLOSE (filedes[1]));
1387 c = 'X';
1388 if (1 != READ (filedes[0], &c, sizeof (char)))
1389 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1390 fflush (stdout);
1391 switch (c)
1392 { 1399 {
1393 case '.': 1400 /* Parent */
1394 exit (0); 1401 char c;
1395 case 'I': 1402
1396 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1403 GNUNET_break (0 == CLOSE (filedes[1]));
1397 _("Service process failed to initialize\n")); 1404 c = 'X';
1398 break; 1405 if (1 != READ (filedes[0], &c, sizeof (char)))
1399 case 'S': 1406 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1400 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1407 fflush (stdout);
1401 _("Service process could not initialize server function\n")); 1408 switch (c)
1402 break; 1409 {
1403 case 'X': 1410 case '.':
1404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1411 exit (0);
1405 _("Service process failed to report status\n")); 1412 case 'I':
1406 break; 1413 LOG (GNUNET_ERROR_TYPE_INFO,
1414 _("Service process failed to initialize\n"));
1415 break;
1416 case 'S':
1417 LOG (GNUNET_ERROR_TYPE_INFO,
1418 _("Service process could not initialize server function\n"));
1419 break;
1420 case 'X':
1421 LOG (GNUNET_ERROR_TYPE_INFO,
1422 _("Service process failed to report status\n"));
1423 break;
1424 }
1425 exit (1); /* child reported error */
1407 } 1426 }
1408 exit (1); /* child reported error */
1409 }
1410 GNUNET_break (0 == CLOSE (0)); 1427 GNUNET_break (0 == CLOSE (0));
1411 GNUNET_break (0 == CLOSE (1)); 1428 GNUNET_break (0 == CLOSE (1));
1412 GNUNET_break (0 == CLOSE (filedes[0])); 1429 GNUNET_break (0 == CLOSE (filedes[0]));
@@ -1415,16 +1432,16 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1415 return GNUNET_SYSERR; 1432 return GNUNET_SYSERR;
1416 /* set stdin/stdout to /dev/null */ 1433 /* set stdin/stdout to /dev/null */
1417 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0)) 1434 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1418 { 1435 {
1419 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2"); 1436 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1420 (void) CLOSE (nullfd); 1437 (void) CLOSE (nullfd);
1421 return GNUNET_SYSERR; 1438 return GNUNET_SYSERR;
1422 } 1439 }
1423 (void) CLOSE (nullfd); 1440 (void) CLOSE (nullfd);
1424 /* Detach from controlling terminal */ 1441 /* Detach from controlling terminal */
1425 pid = setsid (); 1442 pid = setsid ();
1426 if (pid == -1) 1443 if (pid == -1)
1427 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid"); 1444 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1428 sctx->ready_confirm_fd = filedes[1]; 1445 sctx->ready_confirm_fd = filedes[1];
1429#else 1446#else
1430 /* FIXME: we probably need to do something else 1447 /* FIXME: we probably need to do something else
@@ -1444,36 +1461,36 @@ set_user_id (struct GNUNET_SERVICE_Context *sctx)
1444 char *user; 1461 char *user;
1445 1462
1446 if (NULL == (user = get_user_name (sctx))) 1463 if (NULL == (user = get_user_name (sctx)))
1447 return GNUNET_OK; /* keep */ 1464 return GNUNET_OK; /* keep */
1448#ifndef MINGW 1465#ifndef MINGW
1449 struct passwd *pws; 1466 struct passwd *pws;
1450 1467
1451 errno = 0; 1468 errno = 0;
1452 pws = getpwnam (user); 1469 pws = getpwnam (user);
1453 if (pws == NULL) 1470 if (pws == NULL)
1454 { 1471 {
1455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1472 LOG (GNUNET_ERROR_TYPE_ERROR,
1456 _("Cannot obtain information about user `%s': %s\n"), user, 1473 _("Cannot obtain information about user `%s': %s\n"), user,
1457 errno == 0 ? _("No such user") : STRERROR (errno)); 1474 errno == 0 ? _("No such user") : STRERROR (errno));
1458 GNUNET_free (user); 1475 GNUNET_free (user);
1459 return GNUNET_SYSERR; 1476 return GNUNET_SYSERR;
1460 } 1477 }
1461 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) || 1478 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1462#if HAVE_INITGROUPS 1479#if HAVE_INITGROUPS
1463 (0 != initgroups (user, pws->pw_gid)) || 1480 (0 != initgroups (user, pws->pw_gid)) ||
1464#endif 1481#endif
1465 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid))) 1482 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1466 {
1467 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1468 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1469 { 1483 {
1470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1484 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1471 _("Cannot change user/group to `%s': %s\n"), user, 1485 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1472 STRERROR (errno)); 1486 {
1473 GNUNET_free (user); 1487 LOG (GNUNET_ERROR_TYPE_ERROR,
1474 return GNUNET_SYSERR; 1488 _("Cannot change user/group to `%s': %s\n"), user,
1489 STRERROR (errno));
1490 GNUNET_free (user);
1491 return GNUNET_SYSERR;
1492 }
1475 } 1493 }
1476 }
1477#endif 1494#endif
1478 GNUNET_free (user); 1495 GNUNET_free (user);
1479 return GNUNET_OK; 1496 return GNUNET_OK;
@@ -1489,9 +1506,9 @@ pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1489 char *pif = get_pid_file_name (sctx); 1506 char *pif = get_pid_file_name (sctx);
1490 1507
1491 if (pif == NULL) 1508 if (pif == NULL)
1492 return; /* no PID file */ 1509 return; /* no PID file */
1493 if (0 != UNLINK (pif)) 1510 if (0 != UNLINK (pif))
1494 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif); 1511 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1495 GNUNET_free (pif); 1512 GNUNET_free (pif);
1496} 1513}
1497 1514
@@ -1511,8 +1528,8 @@ pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1511 */ 1528 */
1512int 1529int
1513GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, 1530GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1514 enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task, 1531 enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task,
1515 void *task_cls) 1532 void *task_cls)
1516{ 1533{
1517#define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0) 1534#define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1518 1535
@@ -1568,24 +1585,24 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1568 if (GNUNET_OK != set_user_id (&sctx)) 1585 if (GNUNET_OK != set_user_id (&sctx))
1569 goto shutdown; 1586 goto shutdown;
1570#if DEBUG_SERVICE 1587#if DEBUG_SERVICE
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1588 LOG (GNUNET_ERROR_TYPE_DEBUG,
1572 "Service `%s' runs with configuration from `%s'\n", serviceName, 1589 "Service `%s' runs with configuration from `%s'\n", serviceName,
1573 cfg_fn); 1590 cfg_fn);
1574#endif 1591#endif
1575 if (GNUNET_OK == 1592 if (GNUNET_OK ==
1576 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", "skew_offset", 1593 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing",
1577 &skew_offset) && 1594 "skew_offset", &skew_offset)
1578 (GNUNET_OK == 1595 && (GNUNET_OK ==
1579 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", 1596 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing",
1580 "skew_variance", &skew_variance))) 1597 "skew_variance",
1581 { 1598 &skew_variance)))
1582 clock_offset = skew_offset - skew_variance; 1599 {
1583 GNUNET_TIME_set_offset (clock_offset); 1600 clock_offset = skew_offset - skew_variance;
1601 GNUNET_TIME_set_offset (clock_offset);
1584#if DEBUG_SERVICE 1602#if DEBUG_SERVICE
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll\n", 1603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll\n", clock_offset);
1586 clock_offset);
1587#endif 1604#endif
1588 } 1605 }
1589 /* actually run service */ 1606 /* actually run service */
1590 err = 0; 1607 err = 0;
1591 GNUNET_SCHEDULER_run (&service_task, &sctx); 1608 GNUNET_SCHEDULER_run (&service_task, &sctx);
@@ -1597,11 +1614,11 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1597 1614
1598shutdown: 1615shutdown:
1599 if (sctx.ready_confirm_fd != -1) 1616 if (sctx.ready_confirm_fd != -1)
1600 { 1617 {
1601 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1)) 1618 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1602 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write"); 1619 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1603 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd)); 1620 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1604 } 1621 }
1605 1622
1606 GNUNET_CONFIGURATION_destroy (cfg); 1623 GNUNET_CONFIGURATION_destroy (cfg);
1607 i = 0; 1624 i = 0;
@@ -1632,13 +1649,13 @@ shutdown:
1632 */ 1649 */
1633struct GNUNET_SERVICE_Context * 1650struct GNUNET_SERVICE_Context *
1634GNUNET_SERVICE_start (const char *serviceName, 1651GNUNET_SERVICE_start (const char *serviceName,
1635 const struct GNUNET_CONFIGURATION_Handle *cfg) 1652 const struct GNUNET_CONFIGURATION_Handle *cfg)
1636{ 1653{
1637 int i; 1654 int i;
1638 struct GNUNET_SERVICE_Context *sctx; 1655 struct GNUNET_SERVICE_Context *sctx;
1639 1656
1640 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context)); 1657 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1641 sctx->ready_confirm_fd = -1; /* no daemonizing */ 1658 sctx->ready_confirm_fd = -1; /* no daemonizing */
1642 sctx->ret = GNUNET_OK; 1659 sctx->ret = GNUNET_OK;
1643 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL; 1660 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1644 sctx->serviceName = serviceName; 1661 sctx->serviceName = serviceName;
@@ -1646,24 +1663,24 @@ GNUNET_SERVICE_start (const char *serviceName,
1646 1663
1647 /* setup subsystems */ 1664 /* setup subsystems */
1648 if (GNUNET_OK != setup_service (sctx)) 1665 if (GNUNET_OK != setup_service (sctx))
1649 { 1666 {
1650 GNUNET_SERVICE_stop (sctx); 1667 GNUNET_SERVICE_stop (sctx);
1651 return NULL; 1668 return NULL;
1652 } 1669 }
1653 if (sctx->lsocks != NULL) 1670 if (sctx->lsocks != NULL)
1654 sctx->server = 1671 sctx->server =
1655 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, 1672 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1656 sctx->timeout, sctx->require_found); 1673 sctx->timeout, sctx->require_found);
1657 else 1674 else
1658 sctx->server = 1675 sctx->server =
1659 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, 1676 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1660 sctx->timeout, sctx->require_found); 1677 sctx->timeout, sctx->require_found);
1661 1678
1662 if (NULL == sctx->server) 1679 if (NULL == sctx->server)
1663 { 1680 {
1664 GNUNET_SERVICE_stop (sctx); 1681 GNUNET_SERVICE_stop (sctx);
1665 return NULL; 1682 return NULL;
1666 } 1683 }
1667 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); 1684 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1668 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); 1685 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1669 i = 0; 1686 i = 0;
@@ -1701,12 +1718,12 @@ GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1701 GNUNET_SERVER_destroy (sctx->server); 1718 GNUNET_SERVER_destroy (sctx->server);
1702 GNUNET_free_non_null (sctx->my_handlers); 1719 GNUNET_free_non_null (sctx->my_handlers);
1703 if (sctx->addrs != NULL) 1720 if (sctx->addrs != NULL)
1704 { 1721 {
1705 i = 0; 1722 i = 0;
1706 while (sctx->addrs[i] != NULL) 1723 while (sctx->addrs[i] != NULL)
1707 GNUNET_free (sctx->addrs[i++]); 1724 GNUNET_free (sctx->addrs[i++]);
1708 GNUNET_free (sctx->addrs); 1725 GNUNET_free (sctx->addrs);
1709 } 1726 }
1710 GNUNET_free_non_null (sctx->addrlens); 1727 GNUNET_free_non_null (sctx->addrlens);
1711 GNUNET_free_non_null (sctx->v4_denied); 1728 GNUNET_free_non_null (sctx->v4_denied);
1712 GNUNET_free_non_null (sctx->v6_denied); 1729 GNUNET_free_non_null (sctx->v6_denied);
diff --git a/src/util/signal.c b/src/util/signal.c
index 395e8e517..a986b6260 100644
--- a/src/util/signal.c
+++ b/src/util/signal.c
@@ -28,6 +28,9 @@
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29#include "gnunet_signal_lib.h" 29#include "gnunet_signal_lib.h"
30 30
31#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32
33
31struct GNUNET_SIGNAL_Context 34struct GNUNET_SIGNAL_Context
32{ 35{
33 int sig; 36 int sig;
@@ -60,7 +63,7 @@ GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler)
60 sig.sa_handler = (void *) handler; 63 sig.sa_handler = (void *) handler;
61 sigemptyset (&sig.sa_mask); 64 sigemptyset (&sig.sa_mask);
62#ifdef SA_INTERRUPT 65#ifdef SA_INTERRUPT
63 sig.sa_flags = SA_INTERRUPT; /* SunOS */ 66 sig.sa_flags = SA_INTERRUPT; /* SunOS */
64#else 67#else
65 sig.sa_flags = SA_RESTART; 68 sig.sa_flags = SA_RESTART;
66#endif 69#endif
@@ -69,15 +72,15 @@ GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler)
69 if (signum == GNUNET_SIGCHLD) 72 if (signum == GNUNET_SIGCHLD)
70 w32_sigchld_handler = handler; 73 w32_sigchld_handler = handler;
71 else 74 else
72 {
73 __p_sig_fn_t sigret = signal (signum, (__p_sig_fn_t) handler);
74
75 if (sigret == SIG_ERR)
76 { 75 {
77 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 76 __p_sig_fn_t sigret = signal (signum, (__p_sig_fn_t) handler);
78 _("signal (%d, %p) returned %d.\n"), signum, handler, sigret); 77
78 if (sigret == SIG_ERR)
79 {
80 LOG (GNUNET_ERROR_TYPE_WARNING,
81 _("signal (%d, %p) returned %d.\n"), signum, handler, sigret);
82 }
79 } 83 }
80 }
81#endif 84#endif
82 return ret; 85 return ret;
83} 86}
diff --git a/src/util/strings.c b/src/util/strings.c
index e2ca69e10..9e9aac3b1 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -32,6 +32,10 @@
32#include "gnunet_common.h" 32#include "gnunet_common.h"
33#include "gnunet_strings_lib.h" 33#include "gnunet_strings_lib.h"
34 34
35#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36
37#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
38
35 39
36/** 40/**
37 * Fill a buffer of the given size with 41 * Fill a buffer of the given size with
@@ -55,7 +59,8 @@
55 * (or number of bytes that would have been written) 59 * (or number of bytes that would have been written)
56 */ 60 */
57size_t 61size_t
58GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) 62GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count,
63 ...)
59{ 64{
60 size_t needed; 65 size_t needed;
61 size_t slen; 66 size_t slen;
@@ -65,18 +70,18 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
65 needed = 0; 70 needed = 0;
66 va_start (ap, count); 71 va_start (ap, count);
67 while (count > 0) 72 while (count > 0)
68 {
69 s = va_arg (ap, const char *);
70
71 slen = strlen (s) + 1;
72 if (buffer != NULL)
73 { 73 {
74 GNUNET_assert (needed + slen <= size); 74 s = va_arg (ap, const char *);
75 memcpy (&buffer[needed], s, slen); 75
76 slen = strlen (s) + 1;
77 if (buffer != NULL)
78 {
79 GNUNET_assert (needed + slen <= size);
80 memcpy (&buffer[needed], s, slen);
81 }
82 needed += slen;
83 count--;
76 } 84 }
77 needed += slen;
78 count--;
79 }
80 va_end (ap); 85 va_end (ap);
81 return needed; 86 return needed;
82} 87}
@@ -97,7 +102,7 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
97 */ 102 */
98unsigned int 103unsigned int
99GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, 104GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
100 unsigned int count, ...) 105 unsigned int count, ...)
101{ 106{
102 unsigned int start; 107 unsigned int start;
103 unsigned int needed; 108 unsigned int needed;
@@ -107,21 +112,21 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
107 needed = 0; 112 needed = 0;
108 va_start (ap, count); 113 va_start (ap, count);
109 while (count > 0) 114 while (count > 0)
110 {
111 r = va_arg (ap, const char **);
112
113 start = needed;
114 while ((needed < size) && (buffer[needed] != '\0'))
115 needed++;
116 if (needed == size)
117 { 115 {
118 va_end (ap); 116 r = va_arg (ap, const char **);
119 return 0; /* error */ 117
118 start = needed;
119 while ((needed < size) && (buffer[needed] != '\0'))
120 needed++;
121 if (needed == size)
122 {
123 va_end (ap);
124 return 0; /* error */
125 }
126 *r = &buffer[start];
127 needed++; /* skip 0-termination */
128 count--;
120 } 129 }
121 *r = &buffer[start];
122 needed++; /* skip 0-termination */
123 count--;
124 }
125 va_end (ap); 130 va_end (ap);
126 return needed; 131 return needed;
127} 132}
@@ -140,25 +145,25 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
140 char *ret; 145 char *ret;
141 146
142 if (size > 5 * 1024) 147 if (size > 5 * 1024)
143 {
144 size = size / 1024;
145 unit = _( /* size unit */ "KiB");
146 if (size > 5 * 1024)
147 { 148 {
148 size = size / 1024; 149 size = size / 1024;
149 unit = _( /* size unit */ "MiB"); 150 unit = _( /* size unit */ "KiB");
150 if (size > 5 * 1024) 151 if (size > 5 * 1024)
151 { 152 {
152 size = size / 1024; 153 size = size / 1024;
153 unit = _( /* size unit */ "GiB"); 154 unit = _( /* size unit */ "MiB");
154 if (size > 5 * 1024) 155 if (size > 5 * 1024)
155 { 156 {
156 size = size / 1024; 157 size = size / 1024;
157 unit = _( /* size unit */ "TiB"); 158 unit = _( /* size unit */ "GiB");
158 } 159 if (size > 5 * 1024)
159 } 160 {
161 size = size / 1024;
162 unit = _( /* size unit */ "TiB");
163 }
164 }
165 }
160 } 166 }
161 }
162 ret = GNUNET_malloc (32); 167 ret = GNUNET_malloc (32);
163 GNUNET_snprintf (ret, 32, "%llu %s", size, unit); 168 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
164 return ret; 169 return ret;
@@ -187,41 +192,41 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
187 192
188 cd = iconv_open ("UTF-8", charset); 193 cd = iconv_open ("UTF-8", charset);
189 if (cd == (iconv_t) - 1) 194 if (cd == (iconv_t) - 1)
190 { 195 {
191 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_open"); 196 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 197 LOG (GNUNET_ERROR_TYPE_WARNING,
193 _("Character set requested was `%s'\n"), charset); 198 _("Character set requested was `%s'\n"), charset);
194 ret = GNUNET_malloc (len + 1); 199 ret = GNUNET_malloc (len + 1);
195 memcpy (ret, input, len); 200 memcpy (ret, input, len);
196 ret[len] = '\0'; 201 ret[len] = '\0';
197 return ret; 202 return ret;
198 } 203 }
199 tmpSize = 3 * len + 4; 204 tmpSize = 3 * len + 4;
200 tmp = GNUNET_malloc (tmpSize); 205 tmp = GNUNET_malloc (tmpSize);
201 itmp = tmp; 206 itmp = tmp;
202 finSize = tmpSize; 207 finSize = tmpSize;
203 if (iconv (cd, 208 if (iconv (cd,
204#if FREEBSD || DARWIN || WINDOWS 209#if FREEBSD || DARWIN || WINDOWS
205 (const char **) &input, 210 (const char **) &input,
206#else 211#else
207 (char **) &input, 212 (char **) &input,
208#endif 213#endif
209 &len, &itmp, &finSize) == SIZE_MAX) 214 &len, &itmp, &finSize) == SIZE_MAX)
210 { 215 {
211 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv"); 216 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
212 iconv_close (cd); 217 iconv_close (cd);
213 GNUNET_free (tmp); 218 GNUNET_free (tmp);
214 ret = GNUNET_malloc (len + 1); 219 ret = GNUNET_malloc (len + 1);
215 memcpy (ret, input, len); 220 memcpy (ret, input, len);
216 ret[len] = '\0'; 221 ret[len] = '\0';
217 return ret; 222 return ret;
218 } 223 }
219 ret = GNUNET_malloc (tmpSize - finSize + 1); 224 ret = GNUNET_malloc (tmpSize - finSize + 1);
220 memcpy (ret, tmp, tmpSize - finSize); 225 memcpy (ret, tmp, tmpSize - finSize);
221 ret[tmpSize - finSize] = '\0'; 226 ret[tmpSize - finSize] = '\0';
222 GNUNET_free (tmp); 227 GNUNET_free (tmp);
223 if (0 != iconv_close (cd)) 228 if (0 != iconv_close (cd))
224 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_close"); 229 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
225 return ret; 230 return ret;
226#else 231#else
227 ret = GNUNET_malloc (len + 1); 232 ret = GNUNET_malloc (len + 1);
@@ -262,89 +267,89 @@ GNUNET_STRINGS_filename_expand (const char *fil)
262 /* absolute path, just copy */ 267 /* absolute path, just copy */
263 return GNUNET_strdup (fil); 268 return GNUNET_strdup (fil);
264 if (fil[0] == '~') 269 if (fil[0] == '~')
265 {
266 fm = getenv ("HOME");
267 if (fm == NULL)
268 { 270 {
269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 271 fm = getenv ("HOME");
270 _ 272 if (fm == NULL)
271 ("Failed to expand `$HOME': environment variable `HOME' not set")); 273 {
272 return NULL; 274 LOG (GNUNET_ERROR_TYPE_WARNING,
275 _
276 ("Failed to expand `$HOME': environment variable `HOME' not set"));
277 return NULL;
278 }
279 fm = GNUNET_strdup (fm);
280 /* do not copy '~' */
281 fil_ptr = fil + 1;
282
283 /* skip over dir seperator to be consistent */
284 if (fil_ptr[0] == DIR_SEPARATOR)
285 fil_ptr++;
273 } 286 }
274 fm = GNUNET_strdup (fm);
275 /* do not copy '~' */
276 fil_ptr = fil + 1;
277
278 /* skip over dir seperator to be consistent */
279 if (fil_ptr[0] == DIR_SEPARATOR)
280 fil_ptr++;
281 }
282 else 287 else
283 {
284 /* relative path */
285 fil_ptr = fil;
286 len = 512;
287 fm = NULL;
288 while (1)
289 { 288 {
290 buffer = GNUNET_malloc (len); 289 /* relative path */
291 if (getcwd (buffer, len) != NULL) 290 fil_ptr = fil;
292 { 291 len = 512;
293 fm = buffer; 292 fm = NULL;
294 break; 293 while (1)
295 } 294 {
296 if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) 295 buffer = GNUNET_malloc (len);
297 { 296 if (getcwd (buffer, len) != NULL)
298 len *= 2; 297 {
299 GNUNET_free (buffer); 298 fm = buffer;
300 continue; 299 break;
301 } 300 }
302 GNUNET_free (buffer); 301 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
303 break; 302 {
303 len *= 2;
304 GNUNET_free (buffer);
305 continue;
306 }
307 GNUNET_free (buffer);
308 break;
309 }
310 if (fm == NULL)
311 {
312 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
313 buffer = getenv ("PWD"); /* alternative */
314 if (buffer != NULL)
315 fm = GNUNET_strdup (buffer);
316 }
317 if (fm == NULL)
318 fm = GNUNET_strdup ("./"); /* give up */
304 } 319 }
305 if (fm == NULL)
306 {
307 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getcwd");
308 buffer = getenv ("PWD"); /* alternative */
309 if (buffer != NULL)
310 fm = GNUNET_strdup (buffer);
311 }
312 if (fm == NULL)
313 fm = GNUNET_strdup ("./"); /* give up */
314 }
315 n = strlen (fm) + 1 + strlen (fil_ptr) + 1; 320 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
316 buffer = GNUNET_malloc (n); 321 buffer = GNUNET_malloc (n);
317 GNUNET_snprintf (buffer, n, "%s%s%s", fm, 322 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
318 (fm[strlen (fm) - 1] == 323 (fm[strlen (fm) - 1] ==
319 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr); 324 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
320 GNUNET_free (fm); 325 GNUNET_free (fm);
321 return buffer; 326 return buffer;
322#else 327#else
323 fn = GNUNET_malloc (MAX_PATH + 1); 328 fn = GNUNET_malloc (MAX_PATH + 1);
324 329
325 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS) 330 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
326 { 331 {
327 SetErrnoFromWinError (lRet); 332 SetErrnoFromWinError (lRet);
328 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); 333 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
329 return NULL; 334 return NULL;
330 } 335 }
331 /* is the path relative? */ 336 /* is the path relative? */
332 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0)) 337 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
333 {
334 char szCurDir[MAX_PATH + 1];
335
336 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
337 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
338 { 338 {
339 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW); 339 char szCurDir[MAX_PATH + 1];
340 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory"); 340
341 return NULL; 341 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
342 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
343 {
344 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
345 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
346 return NULL;
347 }
348 buffer = GNUNET_malloc (MAX_PATH + 1);
349 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
350 GNUNET_free (fn);
351 fn = buffer;
342 } 352 }
343 buffer = GNUNET_malloc (MAX_PATH + 1);
344 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
345 GNUNET_free (fn);
346 fn = buffer;
347 }
348 353
349 return fn; 354 return fn;
350#endif 355#endif
@@ -367,25 +372,25 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
367 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) 372 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
368 return GNUNET_strdup (_("eternity")); 373 return GNUNET_strdup (_("eternity"));
369 if (dval > 5 * 1000) 374 if (dval > 5 * 1000)
370 {
371 dval = dval / 1000;
372 unit = _( /* time unit */ "s");
373 if (dval > 5 * 60)
374 { 375 {
375 dval = dval / 60; 376 dval = dval / 1000;
376 unit = _( /* time unit */ "m"); 377 unit = _( /* time unit */ "s");
377 if (dval > 5 * 60) 378 if (dval > 5 * 60)
378 { 379 {
379 dval = dval / 60; 380 dval = dval / 60;
380 unit = _( /* time unit */ "h"); 381 unit = _( /* time unit */ "m");
381 if (dval > 5 * 24) 382 if (dval > 5 * 60)
382 { 383 {
383 dval = dval / 24; 384 dval = dval / 60;
384 unit = _( /* time unit */ " days"); 385 unit = _( /* time unit */ "h");
385 } 386 if (dval > 5 * 24)
386 } 387 {
388 dval = dval / 24;
389 unit = _( /* time unit */ " days");
390 }
391 }
392 }
387 } 393 }
388 }
389 GNUNET_asprintf (&ret, "%llu %s", dval, unit); 394 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
390 return ret; 395 return ret;
391} 396}
diff --git a/src/util/test_bio.c b/src/util/test_bio.c
index df4becdd1..e72c95295 100644
--- a/src/util/test_bio.c
+++ b/src/util/test_bio.c
@@ -57,16 +57,17 @@ test_normal_rw ()
57 GNUNET_assert (NULL != fileR); 57 GNUNET_assert (NULL != fileR);
58 readResultString = NULL; 58 readResultString = NULL;
59 GNUNET_assert (GNUNET_OK == 59 GNUNET_assert (GNUNET_OK ==
60 GNUNET_BIO_read_string (fileR, "Read string error", 60 GNUNET_BIO_read_string (fileR, "Read string error",
61 &readResultString, 200)); 61 &readResultString, 200));
62 GNUNET_assert (NULL != readResultString); 62 GNUNET_assert (NULL != readResultString);
63 GNUNET_assert (0 == strcmp (TESTSTRING, readResultString)); 63 GNUNET_assert (0 == strcmp (TESTSTRING, readResultString));
64 GNUNET_free (readResultString); 64 GNUNET_free (readResultString);
65 GNUNET_assert (GNUNET_OK == 65 GNUNET_assert (GNUNET_OK ==
66 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 66 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
67 &metaDataR)); 67 &metaDataR));
68 GNUNET_assert (GNUNET_YES == 68 GNUNET_assert (GNUNET_YES ==
69 GNUNET_CONTAINER_meta_data_test_equal (metaDataR, metaDataW)); 69 GNUNET_CONTAINER_meta_data_test_equal (metaDataR,
70 metaDataW));
70 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_int64 (fileR, &testNum)); 71 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_int64 (fileR, &testNum));
71 GNUNET_BIO_read_close (fileR, &msg); 72 GNUNET_BIO_read_close (fileR, &msg);
72 GNUNET_CONTAINER_meta_data_destroy (metaDataW); 73 GNUNET_CONTAINER_meta_data_destroy (metaDataW);
@@ -93,8 +94,8 @@ test_nullstring_rw ()
93 fileR = GNUNET_BIO_read_open (fileName); 94 fileR = GNUNET_BIO_read_open (fileName);
94 GNUNET_assert (NULL != fileR); 95 GNUNET_assert (NULL != fileR);
95 GNUNET_assert (GNUNET_OK == 96 GNUNET_assert (GNUNET_OK ==
96 GNUNET_BIO_read_string (fileR, "Read string error", 97 GNUNET_BIO_read_string (fileR, "Read string error",
97 &readResultString, 200)); 98 &readResultString, 200));
98 GNUNET_assert (NULL == readResultString); 99 GNUNET_assert (NULL == readResultString);
99 GNUNET_BIO_read_close (fileR, &msg); 100 GNUNET_BIO_read_close (fileR, &msg);
100 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName)); 101 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
@@ -121,8 +122,8 @@ test_emptystring_rw ()
121 GNUNET_assert (NULL != fileR); 122 GNUNET_assert (NULL != fileR);
122 readResultString = NULL; 123 readResultString = NULL;
123 GNUNET_assert (GNUNET_OK == 124 GNUNET_assert (GNUNET_OK ==
124 GNUNET_BIO_read_string (fileR, "Read string error", 125 GNUNET_BIO_read_string (fileR, "Read string error",
125 &readResultString, 200)); 126 &readResultString, 200));
126 GNUNET_free (readResultString); 127 GNUNET_free (readResultString);
127 GNUNET_BIO_read_close (fileR, &msg); 128 GNUNET_BIO_read_close (fileR, &msg);
128 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName)); 129 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
@@ -148,8 +149,8 @@ test_bigstring_rw ()
148 GNUNET_assert (NULL != fileR); 149 GNUNET_assert (NULL != fileR);
149 readResultString = NULL; 150 readResultString = NULL;
150 GNUNET_assert (GNUNET_SYSERR == 151 GNUNET_assert (GNUNET_SYSERR ==
151 GNUNET_BIO_read_string (fileR, "Read string error", 152 GNUNET_BIO_read_string (fileR, "Read string error",
152 &readResultString, 1)); 153 &readResultString, 1));
153 GNUNET_assert (NULL == readResultString); 154 GNUNET_assert (NULL == readResultString);
154 msg = NULL; 155 msg = NULL;
155 GNUNET_BIO_read_close (fileR, &msg); 156 GNUNET_BIO_read_close (fileR, &msg);
@@ -181,8 +182,8 @@ test_bigmeta_rw ()
181 GNUNET_assert (NULL != fileR); 182 GNUNET_assert (NULL != fileR);
182 metaDataR = NULL; 183 metaDataR = NULL;
183 GNUNET_assert (GNUNET_SYSERR == 184 GNUNET_assert (GNUNET_SYSERR ==
184 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 185 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
185 &metaDataR)); 186 &metaDataR));
186 msg = NULL; 187 msg = NULL;
187 GNUNET_BIO_read_close (fileR, &msg); 188 GNUNET_BIO_read_close (fileR, &msg);
188 GNUNET_free (msg); 189 GNUNET_free (msg);
@@ -203,8 +204,8 @@ test_directory_r ()
203 fileR = GNUNET_BIO_read_open ("/dev"); 204 fileR = GNUNET_BIO_read_open ("/dev");
204 GNUNET_assert (NULL != fileR); 205 GNUNET_assert (NULL != fileR);
205 GNUNET_assert (GNUNET_SYSERR == 206 GNUNET_assert (GNUNET_SYSERR ==
206 GNUNET_BIO_read (fileR, "Read error", readResult, 207 GNUNET_BIO_read (fileR, "Read error", readResult,
207 sizeof (readResult))); 208 sizeof (readResult)));
208 msg = NULL; 209 msg = NULL;
209 GNUNET_BIO_read_close (fileR, &msg); 210 GNUNET_BIO_read_close (fileR, &msg);
210 GNUNET_free (msg); 211 GNUNET_free (msg);
@@ -267,18 +268,18 @@ test_fullfile_rw ()
267 fileR = GNUNET_BIO_read_open ("/dev/null"); 268 fileR = GNUNET_BIO_read_open ("/dev/null");
268 GNUNET_assert (NULL != fileR); 269 GNUNET_assert (NULL != fileR);
269 GNUNET_assert (GNUNET_SYSERR == 270 GNUNET_assert (GNUNET_SYSERR ==
270 GNUNET_BIO_read (fileR, "Read error", readResult, 271 GNUNET_BIO_read (fileR, "Read error", readResult,
271 sizeof (readResult))); 272 sizeof (readResult)));
272 readResultString = NULL; 273 readResultString = NULL;
273 GNUNET_assert (GNUNET_SYSERR == 274 GNUNET_assert (GNUNET_SYSERR ==
274 GNUNET_BIO_read_string (fileR, "Read string error", 275 GNUNET_BIO_read_string (fileR, "Read string error",
275 &readResultString, 200)); 276 &readResultString, 200));
276 GNUNET_assert (NULL == readResultString); 277 GNUNET_assert (NULL == readResultString);
277 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_int64 (fileR, &testNum)); 278 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_int64 (fileR, &testNum));
278 metaDataR = NULL; 279 metaDataR = NULL;
279 GNUNET_assert (GNUNET_SYSERR == 280 GNUNET_assert (GNUNET_SYSERR ==
280 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 281 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
281 &metaDataR)); 282 &metaDataR));
282 msg = NULL; 283 msg = NULL;
283 GNUNET_BIO_read_close (fileR, &msg); 284 GNUNET_BIO_read_close (fileR, &msg);
284 GNUNET_free (msg); 285 GNUNET_free (msg);
@@ -305,8 +306,8 @@ test_fakestring_rw ()
305 fileR = GNUNET_BIO_read_open (fileName); 306 fileR = GNUNET_BIO_read_open (fileName);
306 GNUNET_assert (NULL != fileR); 307 GNUNET_assert (NULL != fileR);
307 GNUNET_assert (GNUNET_SYSERR == 308 GNUNET_assert (GNUNET_SYSERR ==
308 GNUNET_BIO_read_string (fileR, "Read string error", 309 GNUNET_BIO_read_string (fileR, "Read string error",
309 &readResult, 200)); 310 &readResult, 200));
310 msg = NULL; 311 msg = NULL;
311 GNUNET_BIO_read_close (fileR, &msg); 312 GNUNET_BIO_read_close (fileR, &msg);
312 GNUNET_free (msg); 313 GNUNET_free (msg);
@@ -334,8 +335,8 @@ test_fakemeta_rw ()
334 GNUNET_assert (NULL != fileR); 335 GNUNET_assert (NULL != fileR);
335 metaDataR = NULL; 336 metaDataR = NULL;
336 GNUNET_assert (GNUNET_SYSERR == 337 GNUNET_assert (GNUNET_SYSERR ==
337 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 338 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
338 &metaDataR)); 339 &metaDataR));
339 GNUNET_assert (NULL == metaDataR); 340 GNUNET_assert (NULL == metaDataR);
340 msg = NULL; 341 msg = NULL;
341 GNUNET_BIO_read_close (fileR, &msg); 342 GNUNET_BIO_read_close (fileR, &msg);
@@ -364,8 +365,8 @@ test_fakebigmeta_rw ()
364 GNUNET_assert (NULL != fileR); 365 GNUNET_assert (NULL != fileR);
365 metaDataR = NULL; 366 metaDataR = NULL;
366 GNUNET_assert (GNUNET_SYSERR == 367 GNUNET_assert (GNUNET_SYSERR ==
367 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 368 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
368 &metaDataR)); 369 &metaDataR));
369 msg = NULL; 370 msg = NULL;
370 GNUNET_BIO_read_close (fileR, &msg); 371 GNUNET_BIO_read_close (fileR, &msg);
371 GNUNET_free (msg); 372 GNUNET_free (msg);
diff --git a/src/util/test_client.c b/src/util/test_client.c
index bb7d5008a..ad88874f2 100644
--- a/src/util/test_client.c
+++ b/src/util/test_client.c
@@ -71,24 +71,25 @@ copy_msg (void *cls, size_t size, void *buf)
71 */ 71 */
72static void 72static void
73echo_cb (void *cls, struct GNUNET_SERVER_Client *client, 73echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
74 const struct GNUNET_MessageHeader *message) 74 const struct GNUNET_MessageHeader *message)
75{ 75{
76 struct CopyContext *cc; 76 struct CopyContext *cc;
77 struct GNUNET_MessageHeader *cpy; 77 struct GNUNET_MessageHeader *cpy;
78 78
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80 "Receiving message from client, bouncing back\n"); 80 "Receiving message from client, bouncing back\n");
81 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)); 81 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
82 ntohs (message->size));
82 cc = GNUNET_malloc (sizeof (struct CopyContext)); 83 cc = GNUNET_malloc (sizeof (struct CopyContext));
83 cc->client = client; 84 cc->client = client;
84 cpy = GNUNET_malloc (ntohs (message->size)); 85 cpy = GNUNET_malloc (ntohs (message->size));
85 memcpy (cpy, message, ntohs (message->size)); 86 memcpy (cpy, message, ntohs (message->size));
86 cc->cpy = cpy; 87 cc->cpy = cpy;
87 GNUNET_assert (NULL != 88 GNUNET_assert (NULL !=
88 GNUNET_SERVER_notify_transmit_ready (client, 89 GNUNET_SERVER_notify_transmit_ready (client,
89 ntohs (message->size), 90 ntohs (message->size),
90 GNUNET_TIME_UNIT_SECONDS, 91 GNUNET_TIME_UNIT_SECONDS,
91 &copy_msg, cc)); 92 &copy_msg, cc));
92} 93}
93 94
94 95
@@ -104,11 +105,13 @@ recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
104 int *ok = cls; 105 int *ok = cls;
105 struct GNUNET_MessageHeader msg; 106 struct GNUNET_MessageHeader msg;
106 107
107 GNUNET_assert (got != NULL); /* timeout */ 108 GNUNET_assert (got != NULL); /* timeout */
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n"); 109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "Receiving bounce, checking content\n");
109 msg.type = htons (MY_TYPE); 111 msg.type = htons (MY_TYPE);
110 msg.size = htons (sizeof (struct GNUNET_MessageHeader)); 112 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
111 GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader))); 113 GNUNET_assert (0 ==
114 memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
112 GNUNET_CLIENT_disconnect (client, GNUNET_YES); 115 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
113 client = NULL; 116 client = NULL;
114 GNUNET_SERVER_destroy (server); 117 GNUNET_SERVER_destroy (server);
@@ -148,9 +151,9 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 sa.sin_family = AF_INET; 151 sa.sin_family = AF_INET;
149 sa.sin_port = htons (PORT); 152 sa.sin_port = htons (PORT);
150 server = 153 server =
151 GNUNET_SERVER_create (NULL, NULL, sap, slens, 154 GNUNET_SERVER_create (NULL, NULL, sap, slens,
152 GNUNET_TIME_relative_multiply 155 GNUNET_TIME_relative_multiply
153 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO); 156 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
154 GNUNET_assert (server != NULL); 157 GNUNET_assert (server != NULL);
155 handlers[0].callback_cls = cls; 158 handlers[0].callback_cls = cls;
156 handlers[1].callback_cls = cls; 159 handlers[1].callback_cls = cls;
@@ -158,15 +161,15 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 client = GNUNET_CLIENT_connect (MYNAME, cfg); 161 client = GNUNET_CLIENT_connect (MYNAME, cfg);
159 GNUNET_assert (client != NULL); 162 GNUNET_assert (client != NULL);
160 GNUNET_assert (NULL != 163 GNUNET_assert (NULL !=
161 GNUNET_CLIENT_notify_transmit_ready (client, 164 GNUNET_CLIENT_notify_transmit_ready (client,
162 sizeof (struct 165 sizeof (struct
163 GNUNET_MessageHeader), 166 GNUNET_MessageHeader),
164 GNUNET_TIME_UNIT_SECONDS, 167 GNUNET_TIME_UNIT_SECONDS,
165 GNUNET_NO, &make_msg, 168 GNUNET_NO, &make_msg,
166 NULL)); 169 NULL));
167 GNUNET_CLIENT_receive (client, &recv_bounce, cls, 170 GNUNET_CLIENT_receive (client, &recv_bounce, cls,
168 GNUNET_TIME_relative_multiply 171 GNUNET_TIME_relative_multiply
169 (GNUNET_TIME_UNIT_MILLISECONDS, 10000)); 172 (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
170} 173}
171 174
172 175
@@ -181,9 +184,10 @@ check ()
181 184
182 cfg = GNUNET_CONFIGURATION_create (); 185 cfg = GNUNET_CONFIGURATION_create ();
183 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT); 186 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
184 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost"); 187 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME",
188 "localhost");
185 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 189 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
186 "localhost"); 190 "localhost");
187 ok = 1; 191 ok = 1;
188 GNUNET_SCHEDULER_run (&task, &ok); 192 GNUNET_SCHEDULER_run (&task, &ok);
189 GNUNET_CONFIGURATION_destroy (cfg); 193 GNUNET_CONFIGURATION_destroy (cfg);
@@ -197,11 +201,11 @@ main (int argc, char *argv[])
197 201
198 GNUNET_log_setup ("test_client", 202 GNUNET_log_setup ("test_client",
199#if VERBOSE 203#if VERBOSE
200 "DEBUG", 204 "DEBUG",
201#else 205#else
202 "WARNING", 206 "WARNING",
203#endif 207#endif
204 NULL); 208 NULL);
205 ret += check (); 209 ret += check ();
206 210
207 return ret; 211 return ret;
diff --git a/src/util/test_common_allocation.c b/src/util/test_common_allocation.c
index 950f0cedb..2fb1329b1 100644
--- a/src/util/test_common_allocation.c
+++ b/src/util/test_common_allocation.c
@@ -36,21 +36,21 @@ check ()
36 unsigned int ui; 36 unsigned int ui;
37 37
38 /* GNUNET_malloc/GNUNET_free test */ 38 /* GNUNET_malloc/GNUNET_free test */
39 k = 352; /* random start value */ 39 k = 352; /* random start value */
40 for (i = 1; i < MAX_TESTVAL; i++) 40 for (i = 1; i < MAX_TESTVAL; i++)
41 { 41 {
42 ptrs[i] = GNUNET_malloc (i); 42 ptrs[i] = GNUNET_malloc (i);
43 for (j = 0; j < i; j++) 43 for (j = 0; j < i; j++)
44 ptrs[i][j] = k++; 44 ptrs[i][j] = k++;
45 } 45 }
46 46
47 for (i = MAX_TESTVAL - 1; i >= 1; i--) 47 for (i = MAX_TESTVAL - 1; i >= 1; i--)
48 { 48 {
49 for (j = i - 1; j >= 0; j--) 49 for (j = i - 1; j >= 0; j--)
50 if (ptrs[i][j] != (char) --k) 50 if (ptrs[i][j] != (char) --k)
51 return 1; 51 return 1;
52 GNUNET_free (ptrs[i]); 52 GNUNET_free (ptrs[i]);
53 } 53 }
54 54
55 /* GNUNET_free_non_null test */ 55 /* GNUNET_free_non_null test */
56 GNUNET_free_non_null (NULL); 56 GNUNET_free_non_null (NULL);
diff --git a/src/util/test_common_logging.c b/src/util/test_common_logging.c
index 39ef58267..6b4922c65 100644
--- a/src/util/test_common_logging.c
+++ b/src/util/test_common_logging.c
@@ -28,7 +28,7 @@
28 28
29static void 29static void
30my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component, 30my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component,
31 const char *date, const char *msg) 31 const char *date, const char *msg)
32{ 32{
33 unsigned int *c = ctx; 33 unsigned int *c = ctx;
34 34
@@ -57,23 +57,26 @@ main (int argc, char *argv[])
57 /* the last 6 calls should be merged (repated bulk messages!) */ 57 /* the last 6 calls should be merged (repated bulk messages!) */
58 GNUNET_logger_remove (&my_log, &logs); 58 GNUNET_logger_remove (&my_log, &logs);
59 if (logs != 4) 59 if (logs != 4)
60 { 60 {
61 fprintf (stdout, "Expected 4 log calls, got %u\n", logs); 61 fprintf (stdout, "Expected 4 log calls, got %u\n", logs);
62 failureCount++; 62 failureCount++;
63 } 63 }
64 GNUNET_break (0 == 64 GNUNET_break (0 ==
65 strcmp (_("ERROR"), 65 strcmp (_("ERROR"),
66 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_ERROR))); 66 GNUNET_error_type_to_string
67 (GNUNET_ERROR_TYPE_ERROR)));
67 GNUNET_break (0 == 68 GNUNET_break (0 ==
68 strcmp (_("WARNING"), 69 strcmp (_("WARNING"),
69 GNUNET_error_type_to_string 70 GNUNET_error_type_to_string
70 (GNUNET_ERROR_TYPE_WARNING))); 71 (GNUNET_ERROR_TYPE_WARNING)));
71 GNUNET_break (0 == 72 GNUNET_break (0 ==
72 strcmp (_("INFO"), 73 strcmp (_("INFO"),
73 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_INFO))); 74 GNUNET_error_type_to_string
75 (GNUNET_ERROR_TYPE_INFO)));
74 GNUNET_break (0 == 76 GNUNET_break (0 ==
75 strcmp (_("DEBUG"), 77 strcmp (_("DEBUG"),
76 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG))); 78 GNUNET_error_type_to_string
79 (GNUNET_ERROR_TYPE_DEBUG)));
77 GNUNET_log_setup ("test_common_logging", "WARNING", "/dev/null"); 80 GNUNET_log_setup ("test_common_logging", "WARNING", "/dev/null");
78 logs = 0; 81 logs = 0;
79 GNUNET_logger_add (&my_log, &logs); 82 GNUNET_logger_add (&my_log, &logs);
@@ -81,17 +84,17 @@ main (int argc, char *argv[])
81 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Drop me...\n"); 84 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Drop me...\n");
82 GNUNET_logger_remove (&my_log, &logs); 85 GNUNET_logger_remove (&my_log, &logs);
83 if (logs != 1) 86 if (logs != 1)
84 { 87 {
85 fprintf (stdout, "Expected 1 log call, got %u\n", logs); 88 fprintf (stdout, "Expected 1 log call, got %u\n", logs);
86 failureCount++; 89 failureCount++;
87 } 90 }
88 91
89 if (failureCount != 0) 92 if (failureCount != 0)
90 { 93 {
91 fprintf (stdout, "%u TESTS FAILED!\n", failureCount); 94 fprintf (stdout, "%u TESTS FAILED!\n", failureCount);
92 return -1; 95 return -1;
93 } 96 }
94 return 0; 97 return 0;
95} /* end of main */ 98} /* end of main */
96 99
97/* end of test_common_logging.c */ 100/* end of test_common_logging.c */
diff --git a/src/util/test_common_logging_dummy.c b/src/util/test_common_logging_dummy.c
index 64d164a13..d87f56d42 100644
--- a/src/util/test_common_logging_dummy.c
+++ b/src/util/test_common_logging_dummy.c
@@ -33,7 +33,7 @@
33 33
34static void 34static void
35my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component, 35my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component,
36 const char *date, const char *msg) 36 const char *date, const char *msg)
37{ 37{
38 if (strncmp ("test-common-logging-dummy", component, 25) != 0) 38 if (strncmp ("test-common-logging-dummy", component, 25) != 0)
39 return; 39 return;
@@ -87,6 +87,6 @@ main (int argc, char *argv[])
87 pr2 (GNUNET_ERROR_TYPE_INFO, "INFO"); 87 pr2 (GNUNET_ERROR_TYPE_INFO, "INFO");
88 pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG"); 88 pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
89 return 0; 89 return 0;
90} /* end of main */ 90} /* end of main */
91 91
92/* end of test_common_logging_dummy.c */ 92/* end of test_common_logging_dummy.c */
diff --git a/src/util/test_common_logging_runtime_loglevels.c b/src/util/test_common_logging_runtime_loglevels.c
index 815a3e516..388f74ec1 100644
--- a/src/util/test_common_logging_runtime_loglevels.c
+++ b/src/util/test_common_logging_runtime_loglevels.c
@@ -47,34 +47,35 @@ static void runone ();
47static void 47static void
48end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 48end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
49{ 49{
50 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase, ok); 50 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase,
51 ok);
51 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 52 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
52 { 53 {
53 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 54 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
54 } 55 }
55 GNUNET_OS_process_wait (proc); 56 GNUNET_OS_process_wait (proc);
56 GNUNET_OS_process_close (proc); 57 GNUNET_OS_process_close (proc);
57 proc = NULL; 58 proc = NULL;
58 GNUNET_DISK_pipe_close (pipe_stdout); 59 GNUNET_DISK_pipe_close (pipe_stdout);
59 if (ok == 1) 60 if (ok == 1)
60 {
61 if (phase < 9)
62 { 61 {
63 phase += 1; 62 if (phase < 9)
64 runone (); 63 {
64 phase += 1;
65 runone ();
66 }
67 else
68 ok = 0;
65 } 69 }
66 else
67 ok = 0;
68 }
69 else 70 else
70 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n"); 71 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
71} 72}
72 73
73static char * 74static char *
74read_output_line (int phase_from1, int phase_to1, int phase_from2, 75read_output_line (int phase_from1, int phase_to1, int phase_from2,
75 int phase_to2, char c, char *expect_level, 76 int phase_to2, char c, char *expect_level,
76 long delay_morethan, long delay_lessthan, int phase, char *p, 77 long delay_morethan, long delay_lessthan, int phase,
77 int *len, long *delay, char level[8]) 78 char *p, int *len, long *delay, char level[8])
78{ 79{
79 char *r = p; 80 char *r = p;
80 char t[7]; 81 char t[7];
@@ -82,67 +83,71 @@ read_output_line (int phase_from1, int phase_to1, int phase_from2,
82 j = 0; 83 j = 0;
83 int stage = 0; 84 int stage = 0;
84 if (!(phase >= phase_from1 && phase <= phase_to1) && 85 if (!(phase >= phase_from1 && phase <= phase_to1) &&
85 !(phase >= phase_from2 && phase <= phase_to2)) 86 !(phase >= phase_from2 && phase <= phase_to2))
86 return p; 87 return p;
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p); 88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p);
88 for (i = 0; i < *len && !stop; i++) 90 for (i = 0; i < *len && !stop; i++)
89 {
90 switch (stage)
91 { 91 {
92 case 0: /* read first char */ 92 switch (stage)
93 if (r[i] != c) 93 {
94 { 94 case 0: /* read first char */
95 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c, r[i]); 95 if (r[i] != c)
96 GNUNET_break (0); 96 {
97 return NULL; 97 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98 } 98 "Expected '%c', but got '%c'\n", c, r[i]);
99 stage += 1; 99 GNUNET_break (0);
100 break; 100 return NULL;
101 case 1: /* read at most 7 char-long error level string, finished by ' ' */ 101 }
102 if (r[i] == ' ') 102 stage += 1;
103 { 103 break;
104 level[j] = '\0'; 104 case 1: /* read at most 7 char-long error level string, finished by ' ' */
105 stage += 1; 105 if (r[i] == ' ')
106 j = 0; 106 {
107 } 107 level[j] = '\0';
108 else if (i == 8) 108 stage += 1;
109 { 109 j = 0;
110 GNUNET_break (0); 110 }
111 ok = 2; 111 else if (i == 8)
112 return NULL; 112 {
113 } 113 GNUNET_break (0);
114 else 114 ok = 2;
115 level[j++] = r[i]; 115 return NULL;
116 break; 116 }
117 case 2: /* read the delay, finished by '\n' */ 117 else
118 t[j++] = r[i]; 118 level[j++] = r[i];
119 break;
120 case 2: /* read the delay, finished by '\n' */
121 t[j++] = r[i];
119#if WINDOWS 122#if WINDOWS
120 if (r[i] == '\r' && r[i + 1] == '\n') 123 if (r[i] == '\r' && r[i + 1] == '\n')
121 { 124 {
122 i += 1; 125 i += 1;
123 t[j - 1] = '\0'; 126 t[j - 1] = '\0';
124 *delay = strtol (t, NULL, 10); 127 *delay = strtol (t, NULL, 10);
125 stop = 1; 128 stop = 1;
126 } 129 }
127#else 130#else
128 if (r[i] == '\n') 131 if (r[i] == '\n')
129 { 132 {
130 t[j - 1] = '\0'; 133 t[j - 1] = '\0';
131 *delay = strtol (t, NULL, 10); 134 *delay = strtol (t, NULL, 10);
132 stop = 1; 135 stop = 1;
133 } 136 }
134#endif 137#endif
135 break; 138 break;
139 }
136 } 140 }
137 } 141 if (!stop || strcmp (expect_level, level) != 0 || *delay < 0
138 if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 || *delay > 1000 || 142 || *delay > 1000
139 (!((*delay < delay_lessthan) || !(*delay > delay_morethan)) && c != '1' && c != '2')) 143 || (!((*delay < delay_lessthan) || !(*delay > delay_morethan))
144 && c != '1' && c != '2'))
140 return NULL; 145 return NULL;
141 *len = *len - i; 146 *len = *len - i;
142 return &r[i]; 147 return &r[i];
143} 148}
144 149
145char buf[20*16]; 150char buf[20 * 16];
146char *buf_ptr; 151char *buf_ptr;
147int bytes; 152int bytes;
148 153
@@ -155,19 +160,20 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 long delays[8]; 160 long delays[8];
156 int rd; 161 int rd;
157 162
158 rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes); 163 rd =
164 GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes);
159 if (rd > 0) 165 if (rd > 0)
160 { 166 {
161 buf_ptr += rd; 167 buf_ptr += rd;
162 bytes += rd; 168 bytes += rd;
163#if VERBOSE 169#if VERBOSE
164 fprintf (stderr, "got %d bytes, reading more\n", rd); 170 fprintf (stderr, "got %d bytes, reading more\n", rd);
165#endif 171#endif
166 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 172 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
167 stdout_read_handle, &read_call, 173 stdout_read_handle, &read_call,
168 (void *) stdout_read_handle); 174 (void *) stdout_read_handle);
169 return; 175 return;
170 } 176 }
171 177
172#if VERBOSE 178#if VERBOSE
173 fprintf (stderr, "bytes is %d:%s\n", bytes, buf); 179 fprintf (stderr, "bytes is %d:%s\n", bytes, buf);
@@ -189,32 +195,64 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 * | 9 * * * * 195 * | 9 * * * *
190 */ 196 */
191 char *p = buf; 197 char *p = buf;
192 if (bytes == 20*16 || 198 if (bytes == 20 * 16 ||
193 !(p = read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, &delay, level)) || 199 !(p =
194 !(p = read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, &bytes, &delays[0], level)) || 200 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
195 !(p = read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes, &delay, level)) || 201 &delay, level))
196 !(p = read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p, &bytes, &delays[1], level)) || 202 || !(p =
197 !(p = read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, &delay, level)) || 203 read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p,
198 !(p = read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, &bytes, &delays[2], level)) || 204 &bytes, &delays[0], level))
199 !(p = read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes, &delay, level)) || 205 || !(p =
200 !(p = read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, &bytes, &delays[3], level)) || 206 read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p,
201 !(p = read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, &delay, level)) || 207 &bytes, &delay, level))
202 !(p = read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, &bytes, &delays[4], level)) || 208 || !(p =
203 !(p = read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes, &delay, level)) || 209 read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p,
204 !(p = read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p, &bytes, &delays[5], level)) || 210 &bytes, &delays[1], level))
205 !(p = read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, &delay, level)) || 211 || !(p =
206 !(p = read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, &bytes, &delays[6], level)) || 212 read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
207 !(p = read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes, &delay, level)) || 213 &delay, level))
208 !(p = read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, &bytes, &delays[7], level))) 214 || !(p =
209 { 215 read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p,
210 if (bytes == 20*16) 216 &bytes, &delays[2], level))
211 fprintf (stderr, "Ran out of buffer space!\n"); 217 || !(p =
212 GNUNET_break (0); 218 read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p,
213 ok = 2; 219 &bytes, &delay, level))
214 GNUNET_SCHEDULER_cancel (die_task); 220 || !(p =
215 GNUNET_SCHEDULER_add_now (&end_task, NULL); 221 read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p,
216 return; 222 &bytes, &delays[3], level))
217 } 223 || !(p =
224 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p,
225 &bytes, &delay, level))
226 || !(p =
227 read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p,
228 &bytes, &delays[4], level))
229 || !(p =
230 read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p,
231 &bytes, &delay, level))
232 || !(p =
233 read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p,
234 &bytes, &delays[5], level))
235 || !(p =
236 read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p,
237 &bytes, &delay, level))
238 || !(p =
239 read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p,
240 &bytes, &delays[6], level))
241 || !(p =
242 read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p,
243 &bytes, &delay, level))
244 || !(p =
245 read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p,
246 &bytes, &delays[7], level)))
247 {
248 if (bytes == 20 * 16)
249 fprintf (stderr, "Ran out of buffer space!\n");
250 GNUNET_break (0);
251 ok = 2;
252 GNUNET_SCHEDULER_cancel (die_task);
253 GNUNET_SCHEDULER_add_now (&end_task, NULL);
254 return;
255 }
218 256
219 GNUNET_SCHEDULER_cancel (die_task); 257 GNUNET_SCHEDULER_cancel (die_task);
220 GNUNET_SCHEDULER_add_now (&end_task, NULL); 258 GNUNET_SCHEDULER_add_now (&end_task, NULL);
@@ -228,52 +266,52 @@ runone ()
228 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); 266 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
229 267
230 if (pipe_stdout == NULL) 268 if (pipe_stdout == NULL)
231 { 269 {
232 GNUNET_break (0); 270 GNUNET_break (0);
233 ok = 2; 271 ok = 2;
234 return; 272 return;
235 } 273 }
236 274
237 putenv ("GNUNET_LOG="); 275 putenv ("GNUNET_LOG=");
238 putenv ("GNUNET_FORCE_LOG="); 276 putenv ("GNUNET_FORCE_LOG=");
239 switch (phase) 277 switch (phase)
240 { 278 {
241 case 0: 279 case 0:
242 putenv ("GNUNET_LOG=;;;;ERROR"); 280 putenv ("GNUNET_LOG=;;;;ERROR");
243 break; 281 break;
244 case 1: 282 case 1:
245 putenv ("GNUNET_LOG=;;;;WARNING"); 283 putenv ("GNUNET_LOG=;;;;WARNING");
246 break; 284 break;
247 case 2: 285 case 2:
248 putenv ("GNUNET_LOG=;;;;INFO"); 286 putenv ("GNUNET_LOG=;;;;INFO");
249 break; 287 break;
250 case 3: 288 case 3:
251 putenv ("GNUNET_LOG=;;;;DEBUG"); 289 putenv ("GNUNET_LOG=;;;;DEBUG");
252 break; 290 break;
253 case 4: 291 case 4:
254 putenv ("GNUNET_FORCE_LOG=;;;;ERROR"); 292 putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
255 break; 293 break;
256 case 5: 294 case 5:
257 putenv ("GNUNET_FORCE_LOG=;;;;WARNING"); 295 putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
258 break; 296 break;
259 case 6: 297 case 6:
260 putenv ("GNUNET_FORCE_LOG=;;;;INFO"); 298 putenv ("GNUNET_FORCE_LOG=;;;;INFO");
261 break; 299 break;
262 case 7: 300 case 7:
263 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG"); 301 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
264 break; 302 break;
265 case 8: 303 case 8:
266 putenv ("GNUNET_LOG=blah;;;;ERROR"); 304 putenv ("GNUNET_LOG=blah;;;;ERROR");
267 break; 305 break;
268 case 9: 306 case 9:
269 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR"); 307 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
270 break; 308 break;
271 } 309 }
272 310
273 proc = 311 proc =
274 GNUNET_OS_start_process (NULL, pipe_stdout, 312 GNUNET_OS_start_process (NULL, pipe_stdout,
275 "./test_common_logging_dummy", 313 "./test_common_logging_dummy",
276 "test_common_logging_dummy", NULL); 314 "test_common_logging_dummy", NULL);
277 putenv ("GNUNET_FORCE_LOG="); 315 putenv ("GNUNET_FORCE_LOG=");
278 putenv ("GNUNET_LOG="); 316 putenv ("GNUNET_LOG=");
279 317
@@ -281,20 +319,20 @@ runone ()
281 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); 319 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
282 320
283 stdout_read_handle = 321 stdout_read_handle =
284 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ); 322 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
285 323
286 die_task = 324 die_task =
287 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 325 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
288 (GNUNET_TIME_UNIT_SECONDS, 10), &end_task, 326 (GNUNET_TIME_UNIT_SECONDS, 10), &end_task,
289 NULL); 327 NULL);
290 328
291 bytes = 0; 329 bytes = 0;
292 buf_ptr = buf; 330 buf_ptr = buf;
293 memset (&buf, 0, sizeof (buf)); 331 memset (&buf, 0, sizeof (buf));
294 332
295 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 333 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
296 stdout_read_handle, &read_call, 334 stdout_read_handle, &read_call,
297 (void *) stdout_read_handle); 335 (void *) stdout_read_handle);
298} 336}
299 337
300static void 338static void
@@ -324,11 +362,11 @@ main (int argc, char *argv[])
324 362
325 GNUNET_log_setup ("test-common-logging-runtime-loglevels", 363 GNUNET_log_setup ("test-common-logging-runtime-loglevels",
326#if VERBOSE 364#if VERBOSE
327 "DEBUG", 365 "DEBUG",
328#else 366#else
329 "WARNING", 367 "WARNING",
330#endif 368#endif
331 NULL); 369 NULL);
332 ret = check (); 370 ret = check ();
333 371
334 return ret; 372 return ret;
diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c
index f02705f86..1c40f06f6 100644
--- a/src/util/test_configuration.c
+++ b/src/util/test_configuration.c
@@ -42,7 +42,7 @@ enum
42 REMOVE_ENTRY, 42 REMOVE_ENTRY,
43 COMPARE 43 COMPARE
44#if DEBUG 44#if DEBUG
45 , PRINT 45 , PRINT
46#endif 46#endif
47}; 47};
48 48
@@ -76,80 +76,80 @@ initDiffsCBData (struct DiffsCBData *cbData)
76*/ 76*/
77static void 77static void
78diffsCallBack (void *cls, const char *section, const char *option, 78diffsCallBack (void *cls, const char *section, const char *option,
79 const char *value) 79 const char *value)
80{ 80{
81 struct DiffsCBData *cbData = cls; 81 struct DiffsCBData *cbData = cls;
82 int cbOption = cbData->callBackOption; 82 int cbOption = cbData->callBackOption;
83 83
84 switch (cbOption) 84 switch (cbOption)
85 {
86 case EDIT_SECTION:
87 if (NULL == cbData->section)
88 cbData->section = section;
89 if (strcmp (cbData->section, section) == 0)
90 { 85 {
86 case EDIT_SECTION:
87 if (NULL == cbData->section)
88 cbData->section = section;
89 if (strcmp (cbData->section, section) == 0)
90 {
91 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
92 "new-value");
93 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
94 option, "new-value");
95 }
96 break;
97 case EDIT_ALL:
91 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option, 98 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
92 "new-value"); 99 "new-value");
93 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
94 "new-value");
95 }
96 break;
97 case EDIT_ALL:
98 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
99 "new-value");
100 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
101 "new-value");
102 break;
103 case ADD_NEW_ENTRY:
104 {
105 static int hit = 0;
106
107 if (hit == 0)
108 {
109 hit = 1;
110 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
111 "new-value");
112 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, 100 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
113 "new-key", "new-value"); 101 option, "new-value");
114 } 102 break;
115 break; 103 case ADD_NEW_ENTRY:
116 } 104 {
117 case COMPARE: 105 static int hit = 0;
118 { 106
119 int ret; 107 if (hit == 0)
120 char *diffValue; 108 {
121 109 hit = 1;
122 diffValue = NULL; 110 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section,
123 ret = 111 "new-key", "new-value");
124 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section, 112 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
125 option, &diffValue); 113 "new-key", "new-value");
126 if (NULL != diffValue) 114 }
127 { 115 break;
128 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0) 116 }
129 cbData->status = 1; 117 case COMPARE:
130 } 118 {
131 else 119 int ret;
132 cbData->status = 1; 120 char *diffValue;
133 GNUNET_free_non_null (diffValue); 121
134 break; 122 diffValue = NULL;
135 } 123 ret =
124 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
125 option, &diffValue);
126 if (NULL != diffValue)
127 {
128 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
129 cbData->status = 1;
130 }
131 else
132 cbData->status = 1;
133 GNUNET_free_non_null (diffValue);
134 break;
135 }
136#if DEBUG 136#if DEBUG
137 case PRINT: 137 case PRINT:
138 if (NULL == cbData->section) 138 if (NULL == cbData->section)
139 { 139 {
140 cbData->section = section; 140 cbData->section = section;
141 printf ("\nSection: %s\n", section); 141 printf ("\nSection: %s\n", section);
142 } 142 }
143 else if (strcmp (cbData->section, section) != 0) 143 else if (strcmp (cbData->section, section) != 0)
144 { 144 {
145 cbData->section = section; 145 cbData->section = section;
146 printf ("\nSection: %s\n", section); 146 printf ("\nSection: %s\n", section);
147 } 147 }
148 printf ("%s = %s\n", option, value); 148 printf ("%s = %s\n", option, value);
149#endif 149#endif
150 default: 150 default:
151 break; 151 break;
152 } 152 }
153} 153}
154 154
155 155
@@ -162,40 +162,41 @@ editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
162 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create (); 162 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
163 163
164 switch (option) 164 switch (option)
165 {
166 case EDIT_SECTION:
167 case EDIT_ALL:
168 case ADD_NEW_ENTRY:
169 diffsCB.callBackOption = option;
170 diffsCB.cfg = cfg;
171 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
172 break;
173 case EDIT_NOTHING:
174 /* Do nothing */
175 break;
176 case ADD_NEW_SECTION:
177 {
178 int i;
179 char *key;
180
181 for (i = 0; i < 5; i++)
182 { 165 {
183 GNUNET_asprintf (&key, "key%d", i); 166 case EDIT_SECTION:
184 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key, 167 case EDIT_ALL:
185 "new-value"); 168 case ADD_NEW_ENTRY:
186 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section", 169 diffsCB.callBackOption = option;
187 key, "new-value"); 170 diffsCB.cfg = cfg;
188 GNUNET_free (key); 171 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
172 break;
173 case EDIT_NOTHING:
174 /* Do nothing */
175 break;
176 case ADD_NEW_SECTION:
177 {
178 int i;
179 char *key;
180
181 for (i = 0; i < 5; i++)
182 {
183 GNUNET_asprintf (&key, "key%d", i);
184 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
185 "new-value");
186 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs,
187 "new-section", key,
188 "new-value");
189 GNUNET_free (key);
190 }
191 break;
192 }
193 case REMOVE_SECTION:
194 break;
195 case REMOVE_ENTRY:
196 break;
197 default:
198 break;
189 } 199 }
190 break;
191 }
192 case REMOVE_SECTION:
193 break;
194 case REMOVE_ENTRY:
195 break;
196 default:
197 break;
198 }
199 200
200 return diffsCB.cfgDiffs; 201 return diffsCB.cfgDiffs;
201} 202}
@@ -220,35 +221,37 @@ checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
220 221
221 /* Modify configuration and save it */ 222 /* Modify configuration and save it */
222 cfgDiffs = editConfiguration (cfg, option); 223 cfgDiffs = editConfiguration (cfg, option);
223 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf"); 224 diffsFileName =
225 GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
224 if (diffsFileName == NULL) 226 if (diffsFileName == NULL)
225 { 227 {
226 GNUNET_break (0); 228 GNUNET_break (0);
227 GNUNET_CONFIGURATION_destroy (cfg); 229 GNUNET_CONFIGURATION_destroy (cfg);
228 GNUNET_CONFIGURATION_destroy (cfgDiffs); 230 GNUNET_CONFIGURATION_destroy (cfgDiffs);
229 return 1; 231 return 1;
230 } 232 }
231 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName); 233 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
232 GNUNET_CONFIGURATION_destroy (cfg); 234 GNUNET_CONFIGURATION_destroy (cfg);
233 235
234 /* Compare the dumped configuration with modifications done */ 236 /* Compare the dumped configuration with modifications done */
235 cfg = GNUNET_CONFIGURATION_create (); 237 cfg = GNUNET_CONFIGURATION_create ();
236 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName)); 238 GNUNET_assert (GNUNET_OK ==
239 GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
237 remove (diffsFileName); 240 remove (diffsFileName);
238 cbData.callBackOption = COMPARE; 241 cbData.callBackOption = COMPARE;
239 cbData.cfgDiffs = cfgDiffs; 242 cbData.cfgDiffs = cfgDiffs;
240 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData); 243 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
241 if (1 == (ret = cbData.status)) 244 if (1 == (ret = cbData.status))
242 { 245 {
243 fprintf (stderr, 246 fprintf (stderr,
244 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n"); 247 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
245 goto housekeeping; 248 goto housekeeping;
246 } 249 }
247 cbData.cfgDiffs = cfg; 250 cbData.cfgDiffs = cfg;
248 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData); 251 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
249 if ((ret = cbData.status) == 1) 252 if ((ret = cbData.status) == 1)
250 fprintf (stderr, 253 fprintf (stderr,
251 "Incorrect Configuration Diffs: Data may be missing in diffs\n"); 254 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
252 255
253housekeeping: 256housekeeping:
254#if DEBUG 257#if DEBUG
@@ -273,64 +276,65 @@ testConfig ()
273 char *c; 276 char *c;
274 unsigned long long l; 277 unsigned long long l;
275 278
276 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c)) 279 if (GNUNET_OK !=
280 GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
277 return 1; 281 return 1;
278 if (0 != strcmp ("b", c)) 282 if (0 != strcmp ("b", c))
279 { 283 {
280 fprintf (stderr, "Got `%s'\n", c); 284 fprintf (stderr, "Got `%s'\n", c);
281 GNUNET_free (c); 285 GNUNET_free (c);
282 return 2; 286 return 2;
283 } 287 }
284 GNUNET_free (c); 288 GNUNET_free (c);
285 if (GNUNET_OK != 289 if (GNUNET_OK !=
286 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l)) 290 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
287 { 291 {
288 GNUNET_break (0); 292 GNUNET_break (0);
289 return 3; 293 return 3;
290 } 294 }
291 if (5 != l) 295 if (5 != l)
292 { 296 {
293 GNUNET_break (0); 297 GNUNET_break (0);
294 return 4; 298 return 4;
295 } 299 }
296 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES"); 300 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
297 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c")) 301 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
298 { 302 {
299 GNUNET_break (0); 303 GNUNET_break (0);
300 return 5; 304 return 5;
301 } 305 }
302 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10); 306 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
303 if (GNUNET_OK != 307 if (GNUNET_OK !=
304 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c)) 308 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
305 { 309 {
306 GNUNET_break (0); 310 GNUNET_break (0);
307 return 6; 311 return 6;
308 } 312 }
309 if (0 != strcmp (c, "10")) 313 if (0 != strcmp (c, "10"))
310 { 314 {
311 GNUNET_free (c); 315 GNUNET_free (c);
312 GNUNET_break (0); 316 GNUNET_break (0);
313 return 7; 317 return 7;
314 } 318 }
315 GNUNET_free (c); 319 GNUNET_free (c);
316 320
317 if (GNUNET_OK != 321 if (GNUNET_OK !=
318 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c)) 322 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
319 { 323 {
320 GNUNET_break (0); 324 GNUNET_break (0);
321 return 8; 325 return 8;
322 } 326 }
323#ifndef MINGW 327#ifndef MINGW
324 if (0 != strcmp (c, "/hello/world")) 328 if (0 != strcmp (c, "/hello/world"))
325#else 329#else
326#define HI "\\hello\\world" 330#define HI "\\hello\\world"
327 if (strstr (c, HI) != c + strlen (c) - strlen (HI)) 331 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
328#endif 332#endif
329 { 333 {
330 GNUNET_break (0); 334 GNUNET_break (0);
331 GNUNET_free (c); 335 GNUNET_free (c);
332 return 9; 336 return 9;
333 } 337 }
334 GNUNET_free (c); 338 GNUNET_free (c);
335 339
336 return 0; 340 return 0;
@@ -350,10 +354,10 @@ check (void *data, const char *fn)
350 int *idx = data; 354 int *idx = data;
351 355
352 if (0 == strcmp (want[*idx], fn)) 356 if (0 == strcmp (want[*idx], fn))
353 { 357 {
354 (*idx)++; 358 (*idx)++;
355 return GNUNET_OK; 359 return GNUNET_OK;
356 } 360 }
357 GNUNET_break (0); 361 GNUNET_break (0);
358 return GNUNET_SYSERR; 362 return GNUNET_SYSERR;
359} 363}
@@ -366,66 +370,66 @@ testConfigFilenames ()
366 idx = 0; 370 idx = 0;
367 if (3 != 371 if (3 !=
368 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test", 372 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
369 &check, &idx)) 373 &check, &idx))
370 { 374 {
371 GNUNET_break (0); 375 GNUNET_break (0);
372 return 8; 376 return 8;
373 } 377 }
374 if (idx != 3) 378 if (idx != 3)
375 return 16; 379 return 16;
376 if (GNUNET_OK != 380 if (GNUNET_OK !=
377 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", 381 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
378 "/File Name")) 382 "/File Name"))
379 { 383 {
380 GNUNET_break (0); 384 GNUNET_break (0);
381 return 24; 385 return 24;
382 } 386 }
383 387
384 if (GNUNET_NO != 388 if (GNUNET_NO !=
385 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", 389 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
386 "/File Name")) 390 "/File Name"))
387 { 391 {
388 GNUNET_break (0); 392 GNUNET_break (0);
389 return 32; 393 return 32;
390 } 394 }
391 if (GNUNET_NO != 395 if (GNUNET_NO !=
392 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", 396 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
393 "Stuff")) 397 "Stuff"))
394 { 398 {
395 GNUNET_break (0); 399 GNUNET_break (0);
396 return 40; 400 return 40;
397 } 401 }
398 402
399 if (GNUNET_NO != 403 if (GNUNET_NO !=
400 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 404 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
401 "/Hello")) 405 "/Hello"))
402 { 406 {
403 GNUNET_break (0); 407 GNUNET_break (0);
404 return 48; 408 return 48;
405 } 409 }
406 if (GNUNET_NO != 410 if (GNUNET_NO !=
407 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 411 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
408 "/World")) 412 "/World"))
409 { 413 {
410 GNUNET_break (0); 414 GNUNET_break (0);
411 return 56; 415 return 56;
412 } 416 }
413 417
414 if (GNUNET_YES != 418 if (GNUNET_YES !=
415 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 419 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
416 "/File 1")) 420 "/File 1"))
417 { 421 {
418 GNUNET_break (0); 422 GNUNET_break (0);
419 return 64; 423 return 64;
420 } 424 }
421 425
422 if (GNUNET_YES != 426 if (GNUNET_YES !=
423 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 427 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
424 "/File 2")) 428 "/File 2"))
425 { 429 {
426 GNUNET_break (0); 430 GNUNET_break (0);
427 return 72; 431 return 72;
428 } 432 }
429 433
430 idx = 0; 434 idx = 0;
431 want[1] = "/World"; 435 want[1] = "/World";
@@ -433,16 +437,16 @@ testConfigFilenames ()
433 want[3] = "/File 2"; 437 want[3] = "/File 2";
434 if (4 != 438 if (4 !=
435 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test", 439 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
436 &check, &idx)) 440 &check, &idx))
437 { 441 {
438 GNUNET_break (0); 442 GNUNET_break (0);
439 return 80; 443 return 80;
440 } 444 }
441 if (idx != 4) 445 if (idx != 4)
442 { 446 {
443 GNUNET_break (0); 447 GNUNET_break (0);
444 return 88; 448 return 88;
445 } 449 }
446 return 0; 450 return 0;
447} 451}
448 452
@@ -458,11 +462,11 @@ main (int argc, char *argv[])
458 GNUNET_assert (cfg != NULL); 462 GNUNET_assert (cfg != NULL);
459 if (GNUNET_OK != 463 if (GNUNET_OK !=
460 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf")) 464 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
461 { 465 {
462 fprintf (stderr, "Failed to parse configuration file\n"); 466 fprintf (stderr, "Failed to parse configuration file\n");
463 GNUNET_CONFIGURATION_destroy (cfg); 467 GNUNET_CONFIGURATION_destroy (cfg);
464 return 1; 468 return 1;
465 } 469 }
466 failureCount += testConfig (); 470 failureCount += testConfig ();
467 if (failureCount > 0) 471 if (failureCount > 0)
468 goto error; 472 goto error;
@@ -472,36 +476,37 @@ main (int argc, char *argv[])
472 goto error; 476 goto error;
473 477
474 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf")) 478 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
475 { 479 {
476 fprintf (stderr, "Failed to write configuration file\n"); 480 fprintf (stderr, "Failed to write configuration file\n");
477 GNUNET_CONFIGURATION_destroy (cfg); 481 GNUNET_CONFIGURATION_destroy (cfg);
478 return 1; 482 return 1;
479 } 483 }
480 GNUNET_CONFIGURATION_destroy (cfg); 484 GNUNET_CONFIGURATION_destroy (cfg);
481 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf")); 485 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
482 486
483 cfg = GNUNET_CONFIGURATION_create (); 487 cfg = GNUNET_CONFIGURATION_create ();
484 if (GNUNET_OK != 488 if (GNUNET_OK !=
485 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf")) 489 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
486 { 490 {
487 GNUNET_break (0); 491 GNUNET_break (0);
488 GNUNET_CONFIGURATION_destroy (cfg); 492 GNUNET_CONFIGURATION_destroy (cfg);
489 return 1; 493 return 1;
490 } 494 }
491 if (GNUNET_OK != 495 if (GNUNET_OK !=
492 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c)) 496 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
493 { 497 &c))
494 GNUNET_break (0); 498 {
495 GNUNET_CONFIGURATION_destroy (cfg); 499 GNUNET_break (0);
496 return 1; 500 GNUNET_CONFIGURATION_destroy (cfg);
497 } 501 return 1;
502 }
498 if (0 != strcmp (c, "YES")) 503 if (0 != strcmp (c, "YES"))
499 { 504 {
500 GNUNET_break (0); 505 GNUNET_break (0);
501 GNUNET_free (c); 506 GNUNET_free (c);
502 GNUNET_CONFIGURATION_destroy (cfg); 507 GNUNET_CONFIGURATION_destroy (cfg);
503 return 1; 508 return 1;
504 } 509 }
505 510
506 GNUNET_free (c); 511 GNUNET_free (c);
507 GNUNET_CONFIGURATION_destroy (cfg); 512 GNUNET_CONFIGURATION_destroy (cfg);
@@ -509,11 +514,11 @@ main (int argc, char *argv[])
509 /* Testing configuration diffs */ 514 /* Testing configuration diffs */
510 cfgDefault = GNUNET_CONFIGURATION_create (); 515 cfgDefault = GNUNET_CONFIGURATION_create ();
511 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL)) 516 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
512 { 517 {
513 GNUNET_break (0); 518 GNUNET_break (0);
514 GNUNET_CONFIGURATION_destroy (cfgDefault); 519 GNUNET_CONFIGURATION_destroy (cfgDefault);
515 return 1; 520 return 1;
516 } 521 }
517 522
518 /* Nothing changed in the new configuration */ 523 /* Nothing changed in the new configuration */
519 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING); 524 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
@@ -534,9 +539,9 @@ main (int argc, char *argv[])
534 539
535error: 540error:
536 if (failureCount != 0) 541 if (failureCount != 0)
537 { 542 {
538 fprintf (stderr, "Test failed: %u\n", failureCount); 543 fprintf (stderr, "Test failed: %u\n", failureCount);
539 return 1; 544 return 1;
540 } 545 }
541 return 0; 546 return 0;
542} 547}
diff --git a/src/util/test_connection.c b/src/util/test_connection.c
index 92c0cea2e..1ac5b99cd 100644
--- a/src/util/test_connection.c
+++ b/src/util/test_connection.c
@@ -66,44 +66,46 @@ open_listen_socket ()
66 GNUNET_assert (desc != NULL); 66 GNUNET_assert (desc != NULL);
67 if (GNUNET_NETWORK_socket_setsockopt 67 if (GNUNET_NETWORK_socket_setsockopt
68 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 68 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); 69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
70 "setsockopt");
70 GNUNET_assert (GNUNET_NETWORK_socket_bind 71 GNUNET_assert (GNUNET_NETWORK_socket_bind
71 (desc, (const struct sockaddr *) &sa, 72 (desc, (const struct sockaddr *) &sa,
72 sizeof (sa)) == GNUNET_OK); 73 sizeof (sa)) == GNUNET_OK);
73 GNUNET_NETWORK_socket_listen (desc, 5); 74 GNUNET_NETWORK_socket_listen (desc, 5);
74 return desc; 75 return desc;
75} 76}
76 77
77static void 78static void
78receive_check (void *cls, const void *buf, size_t available, 79receive_check (void *cls, const void *buf, size_t available,
79 const struct sockaddr *addr, socklen_t addrlen, int errCode) 80 const struct sockaddr *addr, socklen_t addrlen, int errCode)
80{ 81{
81 int *ok = cls; 82 int *ok = cls;
82 83
83#if VERBOSE 84#if VERBOSE
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n"); 85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n");
85#endif 86#endif
86 GNUNET_assert (buf != NULL); /* no timeout */ 87 GNUNET_assert (buf != NULL); /* no timeout */
87 if (0 == memcmp (&"Hello World"[sofar], buf, available)) 88 if (0 == memcmp (&"Hello World"[sofar], buf, available))
88 sofar += available; 89 sofar += available;
89 if (sofar < 12) 90 if (sofar < 12)
90 { 91 {
91#if VERBOSE 92#if VERBOSE
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n"); 93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n");
93#endif 94#endif
94 GNUNET_CONNECTION_receive (asock, 1024, 95 GNUNET_CONNECTION_receive (asock, 1024,
95 GNUNET_TIME_relative_multiply 96 GNUNET_TIME_relative_multiply
96 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, 97 (GNUNET_TIME_UNIT_SECONDS, 5),
97 cls); 98 &receive_check, cls);
98 } 99 }
99 else 100 else
100 { 101 {
101#if VERBOSE 102#if VERBOSE
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive closes accepted socket\n"); 103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104 "Receive closes accepted socket\n");
103#endif 105#endif
104 *ok = 0; 106 *ok = 0;
105 GNUNET_CONNECTION_destroy (asock, GNUNET_YES); 107 GNUNET_CONNECTION_destroy (asock, GNUNET_YES);
106 } 108 }
107} 109}
108 110
109 111
@@ -122,12 +124,12 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 124 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
123#if VERBOSE 125#if VERBOSE
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "Test asks to receive on accepted socket\n"); 127 "Test asks to receive on accepted socket\n");
126#endif 128#endif
127 GNUNET_CONNECTION_receive (asock, 1024, 129 GNUNET_CONNECTION_receive (asock, 1024,
128 GNUNET_TIME_relative_multiply 130 GNUNET_TIME_relative_multiply
129 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, 131 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
130 cls); 132 cls);
131} 133}
132 134
133static size_t 135static size_t
@@ -135,7 +137,7 @@ make_hello (void *cls, size_t size, void *buf)
135{ 137{
136#if VERBOSE 138#if VERBOSE
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Test prepares to transmit on connect socket\n"); 140 "Test prepares to transmit on connect socket\n");
139#endif 141#endif
140 GNUNET_assert (size >= 12); 142 GNUNET_assert (size >= 12);
141 strcpy ((char *) buf, "Hello World"); 143 strcpy ((char *) buf, "Hello World");
@@ -158,14 +160,14 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n"); 160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n");
159#endif 161#endif
160 GNUNET_assert (NULL != 162 GNUNET_assert (NULL !=
161 GNUNET_CONNECTION_notify_transmit_ready (csock, 12, 163 GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
162 GNUNET_TIME_UNIT_SECONDS, 164 GNUNET_TIME_UNIT_SECONDS,
163 &make_hello, NULL)); 165 &make_hello, NULL));
164#if VERBOSE 166#if VERBOSE
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n"); 167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n");
166#endif 168#endif
167 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept, 169 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls,
168 cls); 170 &run_accept, cls);
169} 171}
170 172
171 173
@@ -181,7 +183,7 @@ check ()
181 ok = 1; 183 ok = 1;
182 cfg = GNUNET_CONFIGURATION_create (); 184 cfg = GNUNET_CONFIGURATION_create ();
183 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 185 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
184 "localhost"); 186 "localhost");
185 GNUNET_SCHEDULER_run (&task, &ok); 187 GNUNET_SCHEDULER_run (&task, &ok);
186 GNUNET_CONFIGURATION_destroy (cfg); 188 GNUNET_CONFIGURATION_destroy (cfg);
187 return ok; 189 return ok;
@@ -196,11 +198,11 @@ main (int argc, char *argv[])
196 198
197 GNUNET_log_setup ("test_connection", 199 GNUNET_log_setup ("test_connection",
198#if VERBOSE 200#if VERBOSE
199 "DEBUG", 201 "DEBUG",
200#else 202#else
201 "WARNING", 203 "WARNING",
202#endif 204#endif
203 NULL); 205 NULL);
204 ret += check (); 206 ret += check ();
205 return ret; 207 return ret;
206} 208}
diff --git a/src/util/test_connection_addressing.c b/src/util/test_connection_addressing.c
index 018cf61fb..c413c18c8 100644
--- a/src/util/test_connection_addressing.c
+++ b/src/util/test_connection_addressing.c
@@ -66,15 +66,16 @@ open_listen_socket ()
66 GNUNET_assert (desc != 0); 66 GNUNET_assert (desc != 0);
67 if (GNUNET_NETWORK_socket_setsockopt 67 if (GNUNET_NETWORK_socket_setsockopt
68 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 68 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); 69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
70 "setsockopt");
70 if (GNUNET_OK != 71 if (GNUNET_OK !=
71 GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, 72 GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa,
72 sizeof (sa))) 73 sizeof (sa)))
73 { 74 {
74 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 75 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
75 "bind"); 76 "bind");
76 GNUNET_assert (0); 77 GNUNET_assert (0);
77 } 78 }
78 GNUNET_NETWORK_socket_listen (desc, 5); 79 GNUNET_NETWORK_socket_listen (desc, 5);
79 return desc; 80 return desc;
80} 81}
@@ -82,25 +83,25 @@ open_listen_socket ()
82 83
83static void 84static void
84receive_check (void *cls, const void *buf, size_t available, 85receive_check (void *cls, const void *buf, size_t available,
85 const struct sockaddr *addr, socklen_t addrlen, int errCode) 86 const struct sockaddr *addr, socklen_t addrlen, int errCode)
86{ 87{
87 int *ok = cls; 88 int *ok = cls;
88 89
89 GNUNET_assert (buf != NULL); /* no timeout */ 90 GNUNET_assert (buf != NULL); /* no timeout */
90 if (0 == memcmp (&"Hello World"[sofar], buf, available)) 91 if (0 == memcmp (&"Hello World"[sofar], buf, available))
91 sofar += available; 92 sofar += available;
92 if (sofar < 12) 93 if (sofar < 12)
93 { 94 {
94 GNUNET_CONNECTION_receive (asock, 1024, 95 GNUNET_CONNECTION_receive (asock, 1024,
95 GNUNET_TIME_relative_multiply 96 GNUNET_TIME_relative_multiply
96 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, 97 (GNUNET_TIME_UNIT_SECONDS, 5),
97 cls); 98 &receive_check, cls);
98 } 99 }
99 else 100 else
100 { 101 {
101 *ok = 0; 102 *ok = 0;
102 GNUNET_CONNECTION_destroy (asock, GNUNET_YES); 103 GNUNET_CONNECTION_destroy (asock, GNUNET_YES);
103 } 104 }
104} 105}
105 106
106 107
@@ -116,7 +117,7 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 GNUNET_assert (asock != NULL); 117 GNUNET_assert (asock != NULL);
117 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); 118 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock));
118 GNUNET_assert (GNUNET_OK == 119 GNUNET_assert (GNUNET_OK ==
119 GNUNET_CONNECTION_get_address (asock, &addr, &alen)); 120 GNUNET_CONNECTION_get_address (asock, &addr, &alen));
120 GNUNET_assert (alen == sizeof (struct sockaddr_in)); 121 GNUNET_assert (alen == sizeof (struct sockaddr_in));
121 v4 = addr; 122 v4 = addr;
122 memset (&expect, 0, sizeof (expect)); 123 memset (&expect, 0, sizeof (expect));
@@ -130,9 +131,9 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 GNUNET_free (addr); 131 GNUNET_free (addr);
131 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 132 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
132 GNUNET_CONNECTION_receive (asock, 1024, 133 GNUNET_CONNECTION_receive (asock, 1024,
133 GNUNET_TIME_relative_multiply 134 GNUNET_TIME_relative_multiply
134 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, 135 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
135 cls); 136 cls);
136} 137}
137 138
138static size_t 139static size_t
@@ -159,17 +160,17 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 v4.sin_port = htons (PORT); 160 v4.sin_port = htons (PORT);
160 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 161 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
161 csock = 162 csock =
162 GNUNET_CONNECTION_create_from_sockaddr (AF_INET, 163 GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
163 (const struct sockaddr *) &v4, 164 (const struct sockaddr *) &v4,
164 sizeof (v4)); 165 sizeof (v4));
165 GNUNET_assert (csock != NULL); 166 GNUNET_assert (csock != NULL);
166 GNUNET_assert (NULL != 167 GNUNET_assert (NULL !=
167 GNUNET_CONNECTION_notify_transmit_ready (csock, 12, 168 GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
168 GNUNET_TIME_UNIT_SECONDS, 169 GNUNET_TIME_UNIT_SECONDS,
169 &make_hello, NULL)); 170 &make_hello, NULL));
170 GNUNET_CONNECTION_destroy (csock, GNUNET_YES); 171 GNUNET_CONNECTION_destroy (csock, GNUNET_YES);
171 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept, 172 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls,
172 cls); 173 &run_accept, cls);
173} 174}
174 175
175 176
@@ -196,11 +197,11 @@ main (int argc, char *argv[])
196 197
197 GNUNET_log_setup ("test_connection_addressing", 198 GNUNET_log_setup ("test_connection_addressing",
198#if VERBOSE 199#if VERBOSE
199 "DEBUG", 200 "DEBUG",
200#else 201#else
201 "WARNING", 202 "WARNING",
202#endif 203#endif
203 NULL); 204 NULL);
204 ret += check (); 205 ret += check ();
205 return ret; 206 return ret;
206} 207}
diff --git a/src/util/test_connection_receive_cancel.c b/src/util/test_connection_receive_cancel.c
index 63829344d..bd095fdda 100644
--- a/src/util/test_connection_receive_cancel.c
+++ b/src/util/test_connection_receive_cancel.c
@@ -65,10 +65,11 @@ open_listen_socket ()
65 GNUNET_assert (desc != NULL); 65 GNUNET_assert (desc != NULL);
66 if (GNUNET_NETWORK_socket_setsockopt 66 if (GNUNET_NETWORK_socket_setsockopt
67 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 67 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
68 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); 68 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
69 "setsockopt");
69 GNUNET_assert (GNUNET_NETWORK_socket_bind 70 GNUNET_assert (GNUNET_NETWORK_socket_bind
70 (desc, (const struct sockaddr *) &sa, 71 (desc, (const struct sockaddr *) &sa,
71 sizeof (sa)) == GNUNET_OK); 72 sizeof (sa)) == GNUNET_OK);
72 GNUNET_NETWORK_socket_listen (desc, 5); 73 GNUNET_NETWORK_socket_listen (desc, 5);
73 return desc; 74 return desc;
74} 75}
@@ -77,7 +78,7 @@ open_listen_socket ()
77 78
78static void 79static void
79dead_receive (void *cls, const void *buf, size_t available, 80dead_receive (void *cls, const void *buf, size_t available,
80 const struct sockaddr *addr, socklen_t addrlen, int errCode) 81 const struct sockaddr *addr, socklen_t addrlen, int errCode)
81{ 82{
82 GNUNET_assert (0); 83 GNUNET_assert (0);
83} 84}
@@ -92,8 +93,9 @@ run_accept_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); 93 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock));
93 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 94 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
94 GNUNET_CONNECTION_receive (asock, 1024, 95 GNUNET_CONNECTION_receive (asock, 1024,
95 GNUNET_TIME_relative_multiply 96 GNUNET_TIME_relative_multiply
96 (GNUNET_TIME_UNIT_SECONDS, 5), &dead_receive, cls); 97 (GNUNET_TIME_UNIT_SECONDS, 5), &dead_receive,
98 cls);
97} 99}
98 100
99 101
@@ -119,9 +121,9 @@ task_receive_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); 121 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
120 GNUNET_assert (csock != NULL); 122 GNUNET_assert (csock != NULL);
121 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, 123 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls,
122 &run_accept_cancel, cls); 124 &run_accept_cancel, cls);
123 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &receive_cancel_task, 125 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
124 cls); 126 &receive_cancel_task, cls);
125} 127}
126 128
127 129
@@ -137,7 +139,7 @@ check_receive_cancel ()
137 ok = 1; 139 ok = 1;
138 cfg = GNUNET_CONFIGURATION_create (); 140 cfg = GNUNET_CONFIGURATION_create ();
139 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 141 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
140 "localhost"); 142 "localhost");
141 GNUNET_SCHEDULER_run (&task_receive_cancel, &ok); 143 GNUNET_SCHEDULER_run (&task_receive_cancel, &ok);
142 GNUNET_CONFIGURATION_destroy (cfg); 144 GNUNET_CONFIGURATION_destroy (cfg);
143 return ok; 145 return ok;
diff --git a/src/util/test_connection_timeout.c b/src/util/test_connection_timeout.c
index 1700718b9..5a0bb7c3a 100644
--- a/src/util/test_connection_timeout.c
+++ b/src/util/test_connection_timeout.c
@@ -62,10 +62,11 @@ open_listen_socket ()
62 GNUNET_assert (desc != NULL); 62 GNUNET_assert (desc != NULL);
63 if (GNUNET_NETWORK_socket_setsockopt 63 if (GNUNET_NETWORK_socket_setsockopt
64 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 64 (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); 65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
66 "setsockopt");
66 GNUNET_assert (GNUNET_NETWORK_socket_bind 67 GNUNET_assert (GNUNET_NETWORK_socket_bind
67 (desc, (const struct sockaddr *) &sa, 68 (desc, (const struct sockaddr *) &sa,
68 sizeof (sa)) == GNUNET_OK); 69 sizeof (sa)) == GNUNET_OK);
69 GNUNET_NETWORK_socket_listen (desc, 5); 70 GNUNET_NETWORK_socket_listen (desc, 5);
70 return desc; 71 return desc;
71} 72}
@@ -77,16 +78,16 @@ send_kilo (void *cls, size_t size, void *buf)
77 int *ok = cls; 78 int *ok = cls;
78 79
79 if (size == 0) 80 if (size == 0)
80 { 81 {
81#if VERBOSE 82#if VERBOSE
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got the desired timeout!\n"); 83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got the desired timeout!\n");
83#endif 84#endif
84 GNUNET_assert (buf == NULL); 85 GNUNET_assert (buf == NULL);
85 *ok = 0; 86 *ok = 0;
86 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 87 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
87 GNUNET_CONNECTION_destroy (csock, GNUNET_YES); 88 GNUNET_CONNECTION_destroy (csock, GNUNET_YES);
88 return 0; 89 return 0;
89 } 90 }
90#if VERBOSE 91#if VERBOSE
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending kilo to fill buffer.\n"); 92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending kilo to fill buffer.\n");
92#endif 93#endif
@@ -94,9 +95,9 @@ send_kilo (void *cls, size_t size, void *buf)
94 memset (buf, 42, 1024); 95 memset (buf, 42, 1024);
95 96
96 GNUNET_assert (NULL != 97 GNUNET_assert (NULL !=
97 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, 98 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024,
98 GNUNET_TIME_UNIT_SECONDS, 99 GNUNET_TIME_UNIT_SECONDS,
99 &send_kilo, cls)); 100 &send_kilo, cls));
100 return 1024; 101 return 1024;
101} 102}
102 103
@@ -111,9 +112,9 @@ task_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); 112 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
112 GNUNET_assert (csock != NULL); 113 GNUNET_assert (csock != NULL);
113 GNUNET_assert (NULL != 114 GNUNET_assert (NULL !=
114 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, 115 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024,
115 GNUNET_TIME_UNIT_SECONDS, 116 GNUNET_TIME_UNIT_SECONDS,
116 &send_kilo, cls)); 117 &send_kilo, cls));
117} 118}
118 119
119 120
@@ -129,7 +130,7 @@ check_timeout ()
129 ok = 1; 130 ok = 1;
130 cfg = GNUNET_CONFIGURATION_create (); 131 cfg = GNUNET_CONFIGURATION_create ();
131 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 132 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
132 "localhost"); 133 "localhost");
133 GNUNET_SCHEDULER_run (&task_timeout, &ok); 134 GNUNET_SCHEDULER_run (&task_timeout, &ok);
134 GNUNET_CONFIGURATION_destroy (cfg); 135 GNUNET_CONFIGURATION_destroy (cfg);
135 return ok; 136 return ok;
@@ -142,11 +143,11 @@ main (int argc, char *argv[])
142 143
143 GNUNET_log_setup ("test_connection_timeout", 144 GNUNET_log_setup ("test_connection_timeout",
144#if VERBOSE 145#if VERBOSE
145 "DEBUG", 146 "DEBUG",
146#else 147#else
147 "WARNING", 148 "WARNING",
148#endif 149#endif
149 NULL); 150 NULL);
150 ret += check_timeout (); 151 ret += check_timeout ();
151 return ret; 152 return ret;
152} 153}
diff --git a/src/util/test_connection_timeout_no_connect.c b/src/util/test_connection_timeout_no_connect.c
index b598907ec..00b247c52 100644
--- a/src/util/test_connection_timeout_no_connect.c
+++ b/src/util/test_connection_timeout_no_connect.c
@@ -57,10 +57,10 @@ task_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); 57 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
58 GNUNET_assert (csock != NULL); 58 GNUNET_assert (csock != NULL);
59 GNUNET_assert (NULL != 59 GNUNET_assert (NULL !=
60 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, 60 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024,
61 GNUNET_TIME_UNIT_SECONDS, 61 GNUNET_TIME_UNIT_SECONDS,
62 &handle_timeout, 62 &handle_timeout,
63 cls)); 63 cls));
64} 64}
65 65
66 66
@@ -76,7 +76,7 @@ check_timeout ()
76 ok = 1; 76 ok = 1;
77 cfg = GNUNET_CONFIGURATION_create (); 77 cfg = GNUNET_CONFIGURATION_create ();
78 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 78 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
79 "localhost"); 79 "localhost");
80 GNUNET_SCHEDULER_run (&task_timeout, &ok); 80 GNUNET_SCHEDULER_run (&task_timeout, &ok);
81 GNUNET_CONFIGURATION_destroy (cfg); 81 GNUNET_CONFIGURATION_destroy (cfg);
82 return ok; 82 return ok;
@@ -89,11 +89,11 @@ main (int argc, char *argv[])
89 89
90 GNUNET_log_setup ("test_connection_timeout_no_connect", 90 GNUNET_log_setup ("test_connection_timeout_no_connect",
91#if VERBOSE 91#if VERBOSE
92 "DEBUG", 92 "DEBUG",
93#else 93#else
94 "WARNING", 94 "WARNING",
95#endif 95#endif
96 NULL); 96 NULL);
97 ret += check_timeout (); 97 ret += check_timeout ();
98 return ret; 98 return ret;
99} 99}
diff --git a/src/util/test_connection_transmit_cancel.c b/src/util/test_connection_transmit_cancel.c
index 50308a2d0..540d8c51d 100644
--- a/src/util/test_connection_transmit_cancel.c
+++ b/src/util/test_connection_transmit_cancel.c
@@ -43,7 +43,8 @@ not_run (void *cls, size_t size, void *buf)
43 43
44 44
45static void 45static void
46task_transmit_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 46task_transmit_cancel (void *cls,
47 const struct GNUNET_SCHEDULER_TaskContext *tc)
47{ 48{
48 int *ok = cls; 49 int *ok = cls;
49 struct GNUNET_CONNECTION_TransmitHandle *th; 50 struct GNUNET_CONNECTION_TransmitHandle *th;
@@ -52,8 +53,8 @@ task_transmit_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
52 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); 53 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
53 GNUNET_assert (csock != NULL); 54 GNUNET_assert (csock != NULL);
54 th = GNUNET_CONNECTION_notify_transmit_ready (csock, 12, 55 th = GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
55 GNUNET_TIME_UNIT_MINUTES, 56 GNUNET_TIME_UNIT_MINUTES,
56 &not_run, cls); 57 &not_run, cls);
57 GNUNET_assert (NULL != th); 58 GNUNET_assert (NULL != th);
58 GNUNET_CONNECTION_notify_transmit_ready_cancel (th); 59 GNUNET_CONNECTION_notify_transmit_ready_cancel (th);
59 GNUNET_CONNECTION_destroy (csock, GNUNET_YES); 60 GNUNET_CONNECTION_destroy (csock, GNUNET_YES);
@@ -74,7 +75,7 @@ check_transmit_cancel ()
74 ok = 1; 75 ok = 1;
75 cfg = GNUNET_CONFIGURATION_create (); 76 cfg = GNUNET_CONFIGURATION_create ();
76 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 77 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
77 "localhost"); 78 "localhost");
78 GNUNET_SCHEDULER_run (&task_transmit_cancel, &ok); 79 GNUNET_SCHEDULER_run (&task_transmit_cancel, &ok);
79 GNUNET_CONFIGURATION_destroy (cfg); 80 GNUNET_CONFIGURATION_destroy (cfg);
80 return ok; 81 return ok;
@@ -88,11 +89,11 @@ main (int argc, char *argv[])
88 89
89 GNUNET_log_setup ("test_connection_transmit_cancel", 90 GNUNET_log_setup ("test_connection_transmit_cancel",
90#if VERBOSE 91#if VERBOSE
91 "DEBUG", 92 "DEBUG",
92#else 93#else
93 "WARNING", 94 "WARNING",
94#endif 95#endif
95 NULL); 96 NULL);
96 ret += check_transmit_cancel (); 97 ret += check_transmit_cancel ();
97 98
98 return ret; 99 return ret;
diff --git a/src/util/test_container_bloomfilter.c b/src/util/test_container_bloomfilter.c
index c14adb075..04bb34889 100644
--- a/src/util/test_container_bloomfilter.c
+++ b/src/util/test_container_bloomfilter.c
@@ -75,29 +75,30 @@ main (int argc, char *argv[])
75 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K); 75 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K);
76 76
77 for (i = 0; i < 200; i++) 77 for (i = 0; i < 200; i++)
78 { 78 {
79 nextHC (&tmp); 79 nextHC (&tmp);
80 GNUNET_CONTAINER_bloomfilter_add (bf, &tmp); 80 GNUNET_CONTAINER_bloomfilter_add (bf, &tmp);
81 } 81 }
82 SRANDOM (1); 82 SRANDOM (1);
83 ok1 = 0; 83 ok1 = 0;
84 for (i = 0; i < 200; i++) 84 for (i = 0; i < 200; i++)
85 { 85 {
86 nextHC (&tmp); 86 nextHC (&tmp);
87 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 87 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
88 ok1++; 88 ok1++;
89 } 89 }
90 if (ok1 != 200) 90 if (ok1 != 200)
91 { 91 {
92 printf ("Got %d elements out of" "200 expected after insertion.\n", ok1); 92 printf ("Got %d elements out of" "200 expected after insertion.\n",
93 GNUNET_CONTAINER_bloomfilter_free (bf); 93 ok1);
94 return -1; 94 GNUNET_CONTAINER_bloomfilter_free (bf);
95 } 95 return -1;
96 }
96 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, buf, SIZE)) 97 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, buf, SIZE))
97 { 98 {
98 GNUNET_CONTAINER_bloomfilter_free (bf); 99 GNUNET_CONTAINER_bloomfilter_free (bf);
99 return -1; 100 return -1;
100 } 101 }
101 102
102 GNUNET_CONTAINER_bloomfilter_free (bf); 103 GNUNET_CONTAINER_bloomfilter_free (bf);
103 104
@@ -110,93 +111,94 @@ main (int argc, char *argv[])
110 ok1 = 0; 111 ok1 = 0;
111 ok2 = 0; 112 ok2 = 0;
112 for (i = 0; i < 200; i++) 113 for (i = 0; i < 200; i++)
113 { 114 {
114 nextHC (&tmp); 115 nextHC (&tmp);
115 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 116 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
116 ok1++; 117 ok1++;
117 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) 118 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
118 ok2++; 119 ok2++;
119 } 120 }
120 if (ok1 != 200) 121 if (ok1 != 200)
121 { 122 {
122 printf ("Got %d elements out of 200 " "expected after reloading.\n", ok1); 123 printf ("Got %d elements out of 200 " "expected after reloading.\n",
123 GNUNET_CONTAINER_bloomfilter_free (bf); 124 ok1);
124 GNUNET_CONTAINER_bloomfilter_free (bfi); 125 GNUNET_CONTAINER_bloomfilter_free (bf);
125 return -1; 126 GNUNET_CONTAINER_bloomfilter_free (bfi);
126 } 127 return -1;
128 }
127 129
128 if (ok2 != 200) 130 if (ok2 != 200)
129 { 131 {
130 printf ("Got %d elements out of 200 " "expected after initialization.\n", 132 printf ("Got %d elements out of 200 "
131 ok2); 133 "expected after initialization.\n", ok2);
132 GNUNET_CONTAINER_bloomfilter_free (bf); 134 GNUNET_CONTAINER_bloomfilter_free (bf);
133 GNUNET_CONTAINER_bloomfilter_free (bfi); 135 GNUNET_CONTAINER_bloomfilter_free (bfi);
134 return -1; 136 return -1;
135 } 137 }
136 138
137 SRANDOM (1); 139 SRANDOM (1);
138 for (i = 0; i < 100; i++) 140 for (i = 0; i < 100; i++)
139 { 141 {
140 nextHC (&tmp); 142 nextHC (&tmp);
141 GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp); 143 GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp);
142 GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp); 144 GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp);
143 } 145 }
144 146
145 SRANDOM (1); 147 SRANDOM (1);
146 148
147 ok1 = 0; 149 ok1 = 0;
148 ok2 = 0; 150 ok2 = 0;
149 for (i = 0; i < 200; i++) 151 for (i = 0; i < 200; i++)
150 { 152 {
151 nextHC (&tmp); 153 nextHC (&tmp);
152 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 154 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
153 ok1++; 155 ok1++;
154 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) 156 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
155 ok2++; 157 ok2++;
156 } 158 }
157 159
158 if (ok1 != 100) 160 if (ok1 != 100)
159 { 161 {
160 printf ("Expected 100 elements in loaded filter" 162 printf ("Expected 100 elements in loaded filter"
161 " after adding 200 and deleting 100, got %d\n", ok1); 163 " after adding 200 and deleting 100, got %d\n", ok1);
162 GNUNET_CONTAINER_bloomfilter_free (bf); 164 GNUNET_CONTAINER_bloomfilter_free (bf);
163 GNUNET_CONTAINER_bloomfilter_free (bfi); 165 GNUNET_CONTAINER_bloomfilter_free (bfi);
164 return -1; 166 return -1;
165 } 167 }
166 if (ok2 != 200) 168 if (ok2 != 200)
167 { 169 {
168 printf ("Expected 200 elements in initialized filter" 170 printf ("Expected 200 elements in initialized filter"
169 " after adding 200 and deleting 100 " 171 " after adding 200 and deleting 100 "
170 "(which should do nothing for a filter not backed by a file), got %d\n", 172 "(which should do nothing for a filter not backed by a file), got %d\n",
171 ok2); 173 ok2);
172 GNUNET_CONTAINER_bloomfilter_free (bf); 174 GNUNET_CONTAINER_bloomfilter_free (bf);
173 GNUNET_CONTAINER_bloomfilter_free (bfi); 175 GNUNET_CONTAINER_bloomfilter_free (bfi);
174 return -1; 176 return -1;
175 } 177 }
176 178
177 SRANDOM (3); 179 SRANDOM (3);
178 180
179 GNUNET_CONTAINER_bloomfilter_clear (bf); 181 GNUNET_CONTAINER_bloomfilter_clear (bf);
180 falseok = 0; 182 falseok = 0;
181 for (i = 0; i < 1000; i++) 183 for (i = 0; i < 1000; i++)
182 { 184 {
183 nextHC (&tmp); 185 nextHC (&tmp);
184 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 186 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
185 falseok++; 187 falseok++;
186 } 188 }
187 if (falseok > 0) 189 if (falseok > 0)
188 { 190 {
189 GNUNET_CONTAINER_bloomfilter_free (bf); 191 GNUNET_CONTAINER_bloomfilter_free (bf);
190 GNUNET_CONTAINER_bloomfilter_free (bfi); 192 GNUNET_CONTAINER_bloomfilter_free (bfi);
191 return -1; 193 return -1;
192 } 194 }
193 195
194 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or (bf, buf, SIZE)) 196 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or (bf, buf, SIZE))
195 { 197 {
196 GNUNET_CONTAINER_bloomfilter_free (bf); 198 GNUNET_CONTAINER_bloomfilter_free (bf);
197 GNUNET_CONTAINER_bloomfilter_free (bfi); 199 GNUNET_CONTAINER_bloomfilter_free (bfi);
198 return -1; 200 return -1;
199 } 201 }
200 202
201 SRANDOM (2); 203 SRANDOM (2);
202 i = 20; 204 i = 20;
@@ -210,30 +212,30 @@ main (int argc, char *argv[])
210 ok1 = 0; 212 ok1 = 0;
211 ok2 = 0; 213 ok2 = 0;
212 for (i = 0; i < 20; i++) 214 for (i = 0; i < 20; i++)
213 { 215 {
214 nextHC (&tmp); 216 nextHC (&tmp);
215 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 217 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
216 ok1++; 218 ok1++;
217 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) 219 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
218 ok2++; 220 ok2++;
219 } 221 }
220 222
221 if (ok1 != 20) 223 if (ok1 != 20)
222 { 224 {
223 printf ("Expected 20 elements in resized file-backed filter" 225 printf ("Expected 20 elements in resized file-backed filter"
224 " after adding 20, got %d\n", ok1); 226 " after adding 20, got %d\n", ok1);
225 GNUNET_CONTAINER_bloomfilter_free (bf); 227 GNUNET_CONTAINER_bloomfilter_free (bf);
226 GNUNET_CONTAINER_bloomfilter_free (bfi); 228 GNUNET_CONTAINER_bloomfilter_free (bfi);
227 return -1; 229 return -1;
228 } 230 }
229 if (ok2 != 20) 231 if (ok2 != 20)
230 { 232 {
231 printf ("Expected 20 elements in resized filter" 233 printf ("Expected 20 elements in resized filter"
232 " after adding 20, got %d\n", ok2); 234 " after adding 20, got %d\n", ok2);
233 GNUNET_CONTAINER_bloomfilter_free (bf); 235 GNUNET_CONTAINER_bloomfilter_free (bf);
234 GNUNET_CONTAINER_bloomfilter_free (bfi); 236 GNUNET_CONTAINER_bloomfilter_free (bfi);
235 return -1; 237 return -1;
236 } 238 }
237 239
238 240
239 GNUNET_CONTAINER_bloomfilter_free (bf); 241 GNUNET_CONTAINER_bloomfilter_free (bf);
diff --git a/src/util/test_container_heap.c b/src/util/test_container_heap.c
index a2a004aa7..c1c4eb360 100644
--- a/src/util/test_container_heap.c
+++ b/src/util/test_container_heap.c
@@ -30,7 +30,7 @@
30 30
31static int 31static int
32iterator_callback (void *cls, struct GNUNET_CONTAINER_HeapNode *node, 32iterator_callback (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
33 void *element, GNUNET_CONTAINER_HeapCostType cost) 33 void *element, GNUNET_CONTAINER_HeapCostType cost)
34{ 34{
35 return GNUNET_OK; 35 return GNUNET_OK;
36} 36}
@@ -107,15 +107,15 @@ check ()
107 n6 = GNUNET_CONTAINER_heap_insert (myHeap, "30/200", 30); 107 n6 = GNUNET_CONTAINER_heap_insert (myHeap, "30/200", 30);
108 GNUNET_assert (5 == GNUNET_CONTAINER_heap_get_size (myHeap)); 108 GNUNET_assert (5 == GNUNET_CONTAINER_heap_get_size (myHeap));
109 GNUNET_CONTAINER_heap_remove_node (n5); 109 GNUNET_CONTAINER_heap_remove_node (n5);
110 r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n1 */ 110 r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n1 */
111 GNUNET_assert (NULL != r); 111 GNUNET_assert (NULL != r);
112 GNUNET_assert (0 == strcmp ("11", r)); 112 GNUNET_assert (0 == strcmp ("11", r));
113 GNUNET_CONTAINER_heap_update_cost (myHeap, n6, 200); 113 GNUNET_CONTAINER_heap_update_cost (myHeap, n6, 200);
114 GNUNET_CONTAINER_heap_remove_node (n3); 114 GNUNET_CONTAINER_heap_remove_node (n3);
115 r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n4 */ 115 r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n4 */
116 GNUNET_assert (NULL != r); 116 GNUNET_assert (NULL != r);
117 GNUNET_assert (0 == strcmp ("50", r)); 117 GNUNET_assert (0 == strcmp ("50", r));
118 r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n6 */ 118 r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n6 */
119 GNUNET_assert (NULL != r); 119 GNUNET_assert (NULL != r);
120 GNUNET_assert (0 == strcmp ("30/200", r)); 120 GNUNET_assert (0 == strcmp ("30/200", r));
121 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (myHeap)); 121 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (myHeap));
@@ -172,9 +172,9 @@ check ()
172 172
173 GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2))); 173 GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
174 GNUNET_assert (0 == 174 GNUNET_assert (0 ==
175 nstrcmp ("10", GNUNET_CONTAINER_heap_remove_root (myHeap))); 175 nstrcmp ("10", GNUNET_CONTAINER_heap_remove_root (myHeap)));
176 GNUNET_assert (0 == 176 GNUNET_assert (0 ==
177 nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap))); 177 nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap)));
178 178
179 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10); 179 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
180 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20); 180 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20);
@@ -232,7 +232,7 @@ check ()
232 GNUNET_CONTAINER_heap_remove_node (n2); 232 GNUNET_CONTAINER_heap_remove_node (n2);
233 GNUNET_CONTAINER_heap_remove_node (n1); 233 GNUNET_CONTAINER_heap_remove_node (n1);
234 GNUNET_assert (0 == 234 GNUNET_assert (0 ==
235 nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap))); 235 nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap)));
236 236
237 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10); 237 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
238 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10); 238 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
diff --git a/src/util/test_container_meta_data.c b/src/util/test_container_meta_data.c
index fe1dd79c1..66f90ff10 100644
--- a/src/util/test_container_meta_data.c
+++ b/src/util/test_container_meta_data.c
@@ -41,51 +41,55 @@ testMeta (int i)
41 41
42 m = GNUNET_CONTAINER_meta_data_create (); 42 m = GNUNET_CONTAINER_meta_data_create ();
43 if (GNUNET_OK != 43 if (GNUNET_OK !=
44 GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, 44 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
45 EXTRACTOR_METAFORMAT_UTF8, 45 EXTRACTOR_METATYPE_TITLE,
46 "text/plain", "TestTitle", 46 EXTRACTOR_METAFORMAT_UTF8,
47 strlen ("TestTitle") + 1)) 47 "text/plain", "TestTitle",
48 strlen ("TestTitle") + 1))
48 ABORT (m); 49 ABORT (m);
49 if (GNUNET_OK != 50 if (GNUNET_OK !=
50 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 51 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
51 EXTRACTOR_METATYPE_AUTHOR_NAME, 52 EXTRACTOR_METATYPE_AUTHOR_NAME,
52 EXTRACTOR_METAFORMAT_UTF8, 53 EXTRACTOR_METAFORMAT_UTF8,
53 "text/plain", "TestTitle", 54 "text/plain", "TestTitle",
54 strlen ("TestTitle") + 1)) 55 strlen ("TestTitle") + 1))
55 ABORT (m); 56 ABORT (m);
56 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ 57 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */
57 ABORT (m); 58 ABORT (m);
58 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_AUTHOR_NAME, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ 59 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_AUTHOR_NAME, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */
59 ABORT (m); 60 ABORT (m);
60 if (2 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 61 if (2 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
61 ABORT (m); 62 ABORT (m);
62 if (GNUNET_OK != 63 if (GNUNET_OK !=
63 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, 64 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME,
64 "TestTitle", strlen ("TestTitle") + 1)) 65 "TestTitle",
66 strlen ("TestTitle") + 1))
65 ABORT (m); 67 ABORT (m);
66 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ 68 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */
67 ABORT (m); 69 ABORT (m);
68 if (1 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 70 if (1 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
69 ABORT (m); 71 ABORT (m);
70 if (GNUNET_OK != 72 if (GNUNET_OK !=
71 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, 73 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE,
72 "TestTitle", strlen ("TestTitle") + 1)) 74 "TestTitle",
75 strlen ("TestTitle") + 1))
73 ABORT (m); 76 ABORT (m);
74 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ 77 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */
75 ABORT (m); 78 ABORT (m);
76 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 79 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
77 ABORT (m); 80 ABORT (m);
78 for (j = 0; j < i; j++) 81 for (j = 0; j < i; j++)
79 { 82 {
80 GNUNET_snprintf (val, sizeof (val), "%s.%d", 83 GNUNET_snprintf (val, sizeof (val), "%s.%d",
81 "A teststring that should compress well.", j); 84 "A teststring that should compress well.", j);
82 if (GNUNET_OK != 85 if (GNUNET_OK !=
83 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 86 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
84 EXTRACTOR_METATYPE_UNKNOWN, 87 EXTRACTOR_METATYPE_UNKNOWN,
85 EXTRACTOR_METAFORMAT_UTF8, 88 EXTRACTOR_METAFORMAT_UTF8,
86 "text/plain", val, strlen (val) + 1)) 89 "text/plain", val,
87 ABORT (m); 90 strlen (val) + 1))
88 } 91 ABORT (m);
92 }
89 if (i != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 93 if (i != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
90 ABORT (m); 94 ABORT (m);
91 95
@@ -93,27 +97,27 @@ testMeta (int i)
93 sval = NULL; 97 sval = NULL;
94 if (size != 98 if (size !=
95 GNUNET_CONTAINER_meta_data_serialize (m, &sval, size, 99 GNUNET_CONTAINER_meta_data_serialize (m, &sval, size,
96 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) 100 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
97 { 101 {
98 GNUNET_free_non_null (sval); 102 GNUNET_free_non_null (sval);
99 ABORT (m); 103 ABORT (m);
100 } 104 }
101 GNUNET_CONTAINER_meta_data_destroy (m); 105 GNUNET_CONTAINER_meta_data_destroy (m);
102 m = GNUNET_CONTAINER_meta_data_deserialize (sval, size); 106 m = GNUNET_CONTAINER_meta_data_deserialize (sval, size);
103 GNUNET_free (sval); 107 GNUNET_free (sval);
104 if (m == NULL) 108 if (m == NULL)
105 ABORT (m); 109 ABORT (m);
106 for (j = 0; j < i; j++) 110 for (j = 0; j < i; j++)
107 {
108 GNUNET_snprintf (val, sizeof (val), "%s.%d",
109 "A teststring that should compress well.", j);
110 if (GNUNET_OK !=
111 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_UNKNOWN, val,
112 strlen (val) + 1))
113 { 111 {
114 ABORT (m); 112 GNUNET_snprintf (val, sizeof (val), "%s.%d",
113 "A teststring that should compress well.", j);
114 if (GNUNET_OK !=
115 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_UNKNOWN,
116 val, strlen (val) + 1))
117 {
118 ABORT (m);
119 }
115 } 120 }
116 }
117 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 121 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
118 ABORT (m); 122 ABORT (m);
119 GNUNET_CONTAINER_meta_data_destroy (m); 123 GNUNET_CONTAINER_meta_data_destroy (m);
@@ -131,22 +135,22 @@ testMetaMore (int i)
131 135
132 meta = GNUNET_CONTAINER_meta_data_create (); 136 meta = GNUNET_CONTAINER_meta_data_create ();
133 for (q = 0; q <= i; q++) 137 for (q = 0; q <= i; q++)
134 { 138 {
135 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); 139 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
136 GNUNET_CONTAINER_meta_data_insert (meta, "<test>", 140 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
137 q % EXTRACTOR_metatype_get_max (), 141 q % EXTRACTOR_metatype_get_max (),
138 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 142 EXTRACTOR_METAFORMAT_UTF8,
139 txt, strlen (txt) + 1); 143 "text/plain", txt, strlen (txt) + 1);
140 } 144 }
141 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta); 145 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
142 data = GNUNET_malloc (size * 4); 146 data = GNUNET_malloc (size * 4);
143 if (size != 147 if (size !=
144 GNUNET_CONTAINER_meta_data_serialize (meta, &data, size * 4, 148 GNUNET_CONTAINER_meta_data_serialize (meta, &data, size * 4,
145 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) 149 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
146 { 150 {
147 GNUNET_free (data); 151 GNUNET_free (data);
148 ABORT (meta); 152 ABORT (meta);
149 } 153 }
150 GNUNET_CONTAINER_meta_data_destroy (meta); 154 GNUNET_CONTAINER_meta_data_destroy (meta);
151 GNUNET_free (data); 155 GNUNET_free (data);
152 return 0; 156 return 0;
@@ -162,22 +166,22 @@ testMetaLink ()
162 m = GNUNET_CONTAINER_meta_data_create (); 166 m = GNUNET_CONTAINER_meta_data_create ();
163 if (GNUNET_OK != 167 if (GNUNET_OK !=
164 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 168 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
165 EXTRACTOR_METATYPE_UNKNOWN, 169 EXTRACTOR_METATYPE_UNKNOWN,
166 EXTRACTOR_METAFORMAT_UTF8, 170 EXTRACTOR_METAFORMAT_UTF8,
167 "text/plain", "link", 171 "text/plain", "link",
168 strlen ("link") + 1)) 172 strlen ("link") + 1))
169 ABORT (m); 173 ABORT (m);
170 if (GNUNET_OK != 174 if (GNUNET_OK !=
171 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 175 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
172 EXTRACTOR_METATYPE_FILENAME, 176 EXTRACTOR_METATYPE_FILENAME,
173 EXTRACTOR_METAFORMAT_UTF8, 177 EXTRACTOR_METAFORMAT_UTF8,
174 "text/plain", "lib-link.m4", 178 "text/plain", "lib-link.m4",
175 strlen ("lib-link.m4") + 1)) 179 strlen ("lib-link.m4") + 1))
176 ABORT (m); 180 ABORT (m);
177 val = NULL; 181 val = NULL;
178 size = 182 size =
179 GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) - 1, 183 GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) - 1,
180 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); 184 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
181 GNUNET_CONTAINER_meta_data_destroy (m); 185 GNUNET_CONTAINER_meta_data_destroy (m);
182 m = GNUNET_CONTAINER_meta_data_deserialize (val, size); 186 m = GNUNET_CONTAINER_meta_data_deserialize (val, size);
183 GNUNET_free (val); 187 GNUNET_free (val);
@@ -201,44 +205,46 @@ check ()
201 meta = GNUNET_CONTAINER_meta_data_create (); 205 meta = GNUNET_CONTAINER_meta_data_create ();
202 meta2 = GNUNET_CONTAINER_meta_data_create (); 206 meta2 = GNUNET_CONTAINER_meta_data_create ();
203 for (q = 0; q <= i; q++) 207 for (q = 0; q <= i; q++)
204 { 208 {
205 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); 209 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
206 GNUNET_CONTAINER_meta_data_insert (meta, "<test>", 210 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
207 EXTRACTOR_METATYPE_UNKNOWN, 211 EXTRACTOR_METATYPE_UNKNOWN,
208 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 212 EXTRACTOR_METAFORMAT_UTF8,
209 "TestTitle", strlen ("TestTitle") + 1); 213 "text/plain", "TestTitle",
210 GNUNET_CONTAINER_meta_data_insert (meta2, "<test>", 214 strlen ("TestTitle") + 1);
211 EXTRACTOR_METATYPE_UNKNOWN, 215 GNUNET_CONTAINER_meta_data_insert (meta2, "<test>",
212 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 216 EXTRACTOR_METATYPE_UNKNOWN,
213 "TestTitle", strlen ("TestTitle") + 1); 217 EXTRACTOR_METAFORMAT_UTF8,
214 } 218 "text/plain", "TestTitle",
219 strlen ("TestTitle") + 1);
220 }
215 221
216 //check meta_data_test_equal 222 //check meta_data_test_equal
217 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2)) 223 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
218 { 224 {
219 GNUNET_CONTAINER_meta_data_destroy (meta2); 225 GNUNET_CONTAINER_meta_data_destroy (meta2);
220 ABORT (meta); 226 ABORT (meta);
221 } 227 }
222 228
223 //check meta_data_clear 229 //check meta_data_clear
224 GNUNET_CONTAINER_meta_data_clear (meta2); 230 GNUNET_CONTAINER_meta_data_clear (meta2);
225 if (0 != GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) 231 if (0 != GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
226 { 232 {
227 GNUNET_CONTAINER_meta_data_destroy (meta2); 233 GNUNET_CONTAINER_meta_data_destroy (meta2);
228 ABORT (meta); 234 ABORT (meta);
229 } 235 }
230 // check equal branch in meta_data_test_equal 236 // check equal branch in meta_data_test_equal
231 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta)) 237 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta))
232 { 238 {
233 GNUNET_CONTAINER_meta_data_destroy (meta2); 239 GNUNET_CONTAINER_meta_data_destroy (meta2);
234 ABORT (meta); 240 ABORT (meta);
235 } 241 }
236 // check "count" branch in meta_data_test_equal 242 // check "count" branch in meta_data_test_equal
237 if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2)) 243 if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
238 { 244 {
239 GNUNET_CONTAINER_meta_data_destroy (meta2); 245 GNUNET_CONTAINER_meta_data_destroy (meta2);
240 ABORT (meta); 246 ABORT (meta);
241 } 247 }
242 248
243 // check meta_data_add_publication_date 249 // check meta_data_add_publication_date
244 GNUNET_CONTAINER_meta_data_add_publication_date (meta2); 250 GNUNET_CONTAINER_meta_data_add_publication_date (meta2);
@@ -247,74 +253,74 @@ check ()
247 GNUNET_CONTAINER_meta_data_clear (meta2); 253 GNUNET_CONTAINER_meta_data_clear (meta2);
248 GNUNET_CONTAINER_meta_data_merge (meta2, meta); 254 GNUNET_CONTAINER_meta_data_merge (meta2, meta);
249 if (100 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) 255 if (100 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
250 { 256 {
251 GNUNET_CONTAINER_meta_data_destroy (meta2); 257 GNUNET_CONTAINER_meta_data_destroy (meta2);
252 ABORT (meta); 258 ABORT (meta);
253 } 259 }
254 260
255 // check meta_data_get_by_type 261 // check meta_data_get_by_type
256 GNUNET_CONTAINER_meta_data_clear (meta2); 262 GNUNET_CONTAINER_meta_data_clear (meta2);
257 if (NULL != 263 if (NULL !=
258 (str = 264 (str =
259 GNUNET_CONTAINER_meta_data_get_by_type (meta2, 265 GNUNET_CONTAINER_meta_data_get_by_type (meta2,
260 EXTRACTOR_METATYPE_UNKNOWN))) 266 EXTRACTOR_METATYPE_UNKNOWN)))
261 { 267 {
262 GNUNET_CONTAINER_meta_data_destroy (meta2); 268 GNUNET_CONTAINER_meta_data_destroy (meta2);
263 GNUNET_free (str); 269 GNUNET_free (str);
264 ABORT (meta); 270 ABORT (meta);
265 } 271 }
266 272
267 str = 273 str =
268 GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN); 274 GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN);
269 GNUNET_assert (NULL != str); 275 GNUNET_assert (NULL != str);
270 if (str[0] != 'T') 276 if (str[0] != 'T')
271 { 277 {
272 GNUNET_CONTAINER_meta_data_destroy (meta2); 278 GNUNET_CONTAINER_meta_data_destroy (meta2);
273 GNUNET_free (str); 279 GNUNET_free (str);
274 ABORT (meta); 280 ABORT (meta);
275 } 281 }
276 GNUNET_free (str); 282 GNUNET_free (str);
277 283
278 // check branch 284 // check branch
279 if (NULL != 285 if (NULL !=
280 (str = 286 (str =
281 GNUNET_CONTAINER_meta_data_get_by_type (meta, 287 GNUNET_CONTAINER_meta_data_get_by_type (meta,
282 EXTRACTOR_METATYPE_PUBLICATION_DATE))) 288 EXTRACTOR_METATYPE_PUBLICATION_DATE)))
283 { 289 {
284 GNUNET_free (str); 290 GNUNET_free (str);
285 GNUNET_CONTAINER_meta_data_destroy (meta2); 291 GNUNET_CONTAINER_meta_data_destroy (meta2);
286 ABORT (meta); 292 ABORT (meta);
287 } 293 }
288 294
289 //check meta_data_get_first_by_types 295 //check meta_data_get_first_by_types
290 str = 296 str =
291 GNUNET_CONTAINER_meta_data_get_first_by_types (meta, 297 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
292 EXTRACTOR_METATYPE_UNKNOWN, 298 EXTRACTOR_METATYPE_UNKNOWN,
293 -1); 299 -1);
294 GNUNET_assert (NULL != str); 300 GNUNET_assert (NULL != str);
295 if (str[0] != 'T') 301 if (str[0] != 'T')
296 { 302 {
297 GNUNET_CONTAINER_meta_data_destroy (meta2); 303 GNUNET_CONTAINER_meta_data_destroy (meta2);
298 GNUNET_free (str); 304 GNUNET_free (str);
299 ABORT (meta); 305 ABORT (meta);
300 } 306 }
301 GNUNET_free (str); 307 GNUNET_free (str);
302 308
303 //check meta_data_get_thumbnail 309 //check meta_data_get_thumbnail
304 if (GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb) != 0) 310 if (GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb) != 0)
305 { 311 {
306 GNUNET_free (thumb); 312 GNUNET_free (thumb);
307 GNUNET_CONTAINER_meta_data_destroy (meta2); 313 GNUNET_CONTAINER_meta_data_destroy (meta2);
308 ABORT (meta); 314 ABORT (meta);
309 } 315 }
310 GNUNET_CONTAINER_meta_data_destroy (meta2); 316 GNUNET_CONTAINER_meta_data_destroy (meta2);
311 //check meta_data_duplicate 317 //check meta_data_duplicate
312 meta2 = GNUNET_CONTAINER_meta_data_duplicate (meta); 318 meta2 = GNUNET_CONTAINER_meta_data_duplicate (meta);
313 if (200 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) 319 if (200 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
314 { 320 {
315 GNUNET_CONTAINER_meta_data_destroy (meta2); 321 GNUNET_CONTAINER_meta_data_destroy (meta2);
316 ABORT (meta); 322 ABORT (meta);
317 } 323 }
318 GNUNET_CONTAINER_meta_data_destroy (meta2); 324 GNUNET_CONTAINER_meta_data_destroy (meta2);
319 GNUNET_CONTAINER_meta_data_destroy (meta); 325 GNUNET_CONTAINER_meta_data_destroy (meta);
320 return 0; 326 return 0;
diff --git a/src/util/test_container_multihashmap.c b/src/util/test_container_multihashmap.c
index ba621c17e..5732ace93 100644
--- a/src/util/test_container_multihashmap.c
+++ b/src/util/test_container_multihashmap.c
@@ -52,38 +52,41 @@ testMap (int i)
52 CHECK (0 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1)); 52 CHECK (0 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1));
53 CHECK (0 == GNUNET_CONTAINER_multihashmap_size (m)); 53 CHECK (0 == GNUNET_CONTAINER_multihashmap_size (m));
54 CHECK (0 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL)); 54 CHECK (0 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL));
55 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL)); 55 CHECK (0 ==
56 GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
56 57
57 CHECK (GNUNET_OK == 58 CHECK (GNUNET_OK ==
58 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", 59 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
59 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 60 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
60 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m)); 61 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
61 ret = GNUNET_CONTAINER_multihashmap_get (m, &k1); 62 ret = GNUNET_CONTAINER_multihashmap_get (m, &k1);
62 GNUNET_assert (ret != NULL); 63 GNUNET_assert (ret != NULL);
63 CHECK (0 == strcmp ("v1", ret)); 64 CHECK (0 == strcmp ("v1", ret));
64 CHECK (GNUNET_NO == 65 CHECK (GNUNET_NO ==
65 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", 66 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
66 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 67 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
67 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m)); 68 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
68 CHECK (GNUNET_OK == 69 CHECK (GNUNET_OK ==
69 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", 70 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
70 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 71 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
71 CHECK (GNUNET_OK == 72 CHECK (GNUNET_OK ==
72 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3", 73 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3",
73 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 74 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
74 CHECK (3 == GNUNET_CONTAINER_multihashmap_size (m)); 75 CHECK (3 == GNUNET_CONTAINER_multihashmap_size (m));
75 CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (m, &k1, "v3")); 76 CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (m, &k1, "v3"));
76 CHECK (2 == GNUNET_CONTAINER_multihashmap_size (m)); 77 CHECK (2 == GNUNET_CONTAINER_multihashmap_size (m));
77 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (m, &k1)); 78 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (m, &k1));
78 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2)); 79 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2));
79 CHECK (2 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL)); 80 CHECK (2 ==
80 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL)); 81 GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
82 CHECK (0 ==
83 GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL));
81 CHECK (2 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL)); 84 CHECK (2 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL));
82 CHECK (2 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1)); 85 CHECK (2 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1));
83 for (j = 0; j < 1024; j++) 86 for (j = 0; j < 1024; j++)
84 CHECK (GNUNET_OK == 87 CHECK (GNUNET_OK ==
85 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", 88 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
86 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 89 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
87 GNUNET_CONTAINER_multihashmap_destroy (m); 90 GNUNET_CONTAINER_multihashmap_destroy (m);
88 return 0; 91 return 0;
89} 92}
diff --git a/src/util/test_container_slist.c b/src/util/test_container_slist.c
index 4a277dab5..307a6b248 100644
--- a/src/util/test_container_slist.c
+++ b/src/util/test_container_slist.c
@@ -46,26 +46,27 @@ main (int argc, char *argv[])
46 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0); 46 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
47 47
48 for (i = 0; i < 100; i++) 48 for (i = 0; i < 100; i++)
49 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 49 GNUNET_CONTAINER_slist_add (l,
50 &i, sizeof (i)); 50 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
51 &i, sizeof (i));
51 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); 52 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
52 53
53 for (it = GNUNET_CONTAINER_slist_begin (l), i = 99; 54 for (it = GNUNET_CONTAINER_slist_begin (l), i = 99;
54 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES; 55 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
55 GNUNET_CONTAINER_slist_next (it), i--) 56 GNUNET_CONTAINER_slist_next (it), i--)
56 {
57 p = GNUNET_CONTAINER_slist_get (it, &s);
58
59 if ((p == NULL) || (i != (j = *(int *) p)) || (s != sizeof (i)))
60 { 57 {
61 GNUNET_CONTAINER_slist_iter_destroy (it); 58 p = GNUNET_CONTAINER_slist_get (it, &s);
62 GNUNET_assert (0); 59
60 if ((p == NULL) || (i != (j = *(int *) p)) || (s != sizeof (i)))
61 {
62 GNUNET_CONTAINER_slist_iter_destroy (it);
63 GNUNET_assert (0);
64 }
65 j *= 2;
66 GNUNET_CONTAINER_slist_insert (it,
67 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
68 &j, sizeof (j));
63 } 69 }
64 j *= 2;
65 GNUNET_CONTAINER_slist_insert (it,
66 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
67 &j, sizeof (j));
68 }
69 GNUNET_CONTAINER_slist_iter_destroy (it); 70 GNUNET_CONTAINER_slist_iter_destroy (it);
70 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200); 71 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
71 i = 198; 72 i = 198;
@@ -73,39 +74,40 @@ main (int argc, char *argv[])
73 74
74 for (it = GNUNET_CONTAINER_slist_begin (l); 75 for (it = GNUNET_CONTAINER_slist_begin (l);
75 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;) 76 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;)
76 { 77 {
77 p = GNUNET_CONTAINER_slist_get (it, &s); 78 p = GNUNET_CONTAINER_slist_get (it, &s);
78 GNUNET_assert (p != NULL); 79 GNUNET_assert (p != NULL);
79 GNUNET_assert (s == sizeof (i)); 80 GNUNET_assert (s == sizeof (i));
80 i = *(int *) p; 81 i = *(int *) p;
81 82
82 GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES); 83 GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES);
83 GNUNET_assert (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES); 84 GNUNET_assert (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES);
84 85
85 p = GNUNET_CONTAINER_slist_get (it, &s); 86 p = GNUNET_CONTAINER_slist_get (it, &s);
86 GNUNET_assert (p != NULL); 87 GNUNET_assert (p != NULL);
87 GNUNET_assert (s == sizeof (j)); 88 GNUNET_assert (s == sizeof (j));
88 j = *(int *) p; 89 j = *(int *) p;
89 90
90 GNUNET_assert (j * 2 == i); 91 GNUNET_assert (j * 2 == i);
91 92
92 GNUNET_CONTAINER_slist_erase (it); 93 GNUNET_CONTAINER_slist_erase (it);
93 } 94 }
94 GNUNET_CONTAINER_slist_iter_destroy (it); 95 GNUNET_CONTAINER_slist_iter_destroy (it);
95 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); 96 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
96 i = 99; 97 i = 99;
97 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == 98 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
98 GNUNET_NO); 99 GNUNET_NO);
99 i = 198; 100 i = 198;
100 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == 101 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
101 GNUNET_YES); 102 GNUNET_YES);
102 103
103 GNUNET_CONTAINER_slist_clear (l); 104 GNUNET_CONTAINER_slist_clear (l);
104 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0); 105 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
105 106
106 for (i = 0; i < 100; i++) 107 for (i = 0; i < 100; i++)
107 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 108 GNUNET_CONTAINER_slist_add (l,
108 &i, sizeof (i)); 109 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
110 &i, sizeof (i));
109 /*check slist_append */ 111 /*check slist_append */
110 GNUNET_CONTAINER_slist_append (l, l); 112 GNUNET_CONTAINER_slist_append (l, l);
111 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200); 113 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
@@ -116,23 +118,23 @@ main (int argc, char *argv[])
116 l = GNUNET_CONTAINER_slist_create (); 118 l = GNUNET_CONTAINER_slist_create ();
117 for (i = 0; i < 100; i++) 119 for (i = 0; i < 100; i++)
118 GNUNET_CONTAINER_slist_add_end (l, 120 GNUNET_CONTAINER_slist_add_end (l,
119 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 121 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
120 &i, sizeof (i)); 122 &i, sizeof (i));
121 123
122 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); 124 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
123 125
124 for (it = GNUNET_CONTAINER_slist_begin (l), i = 0; 126 for (it = GNUNET_CONTAINER_slist_begin (l), i = 0;
125 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES; 127 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
126 GNUNET_CONTAINER_slist_next (it), i++) 128 GNUNET_CONTAINER_slist_next (it), i++)
127 {
128 p = GNUNET_CONTAINER_slist_get (it, &s);
129
130 if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i)))
131 { 129 {
132 GNUNET_CONTAINER_slist_iter_destroy (it); 130 p = GNUNET_CONTAINER_slist_get (it, &s);
133 GNUNET_assert (0); 131
132 if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i)))
133 {
134 GNUNET_CONTAINER_slist_iter_destroy (it);
135 GNUNET_assert (0);
136 }
134 } 137 }
135 }
136 GNUNET_CONTAINER_slist_iter_destroy (it); 138 GNUNET_CONTAINER_slist_iter_destroy (it);
137 GNUNET_CONTAINER_slist_destroy (l); 139 GNUNET_CONTAINER_slist_destroy (l);
138 140
@@ -140,12 +142,13 @@ main (int argc, char *argv[])
140 l = GNUNET_CONTAINER_slist_create (); 142 l = GNUNET_CONTAINER_slist_create ();
141 143
142 for (i = 0; i < 100; i++) 144 for (i = 0; i < 100; i++)
143 { 145 {
144 ip = GNUNET_malloc (sizeof (int)); 146 ip = GNUNET_malloc (sizeof (int));
145 *ip = i; 147 *ip = i;
146 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC, 148 GNUNET_CONTAINER_slist_add (l,
147 ip, sizeof (int)); 149 GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC,
148 } 150 ip, sizeof (int));
151 }
149 //creat_add 152 //creat_add
150 it = GNUNET_CONTAINER_slist_begin (l); 153 it = GNUNET_CONTAINER_slist_begin (l);
151 p = GNUNET_CONTAINER_slist_get (it, &s); 154 p = GNUNET_CONTAINER_slist_get (it, &s);
diff --git a/src/util/test_crypto_aes.c b/src/util/test_crypto_aes.c
index 971e9afbe..e928b96a7 100644
--- a/src/util/test_crypto_aes.c
+++ b/src/util/test_crypto_aes.c
@@ -40,30 +40,30 @@ testSymcipher ()
40 40
41 GNUNET_CRYPTO_aes_create_session_key (&key); 41 GNUNET_CRYPTO_aes_create_session_key (&key);
42 size = 42 size =
43 GNUNET_CRYPTO_aes_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key, 43 GNUNET_CRYPTO_aes_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key,
44 (const struct 44 (const struct
45 GNUNET_CRYPTO_AesInitializationVector *) 45 GNUNET_CRYPTO_AesInitializationVector *)
46 INITVALUE, result); 46 INITVALUE, result);
47 if (size == -1) 47 if (size == -1)
48 { 48 {
49 printf ("symciphertest failed: encryptBlock returned %d\n", size); 49 printf ("symciphertest failed: encryptBlock returned %d\n", size);
50 return 1; 50 return 1;
51 } 51 }
52 size = 52 size =
53 GNUNET_CRYPTO_aes_decrypt (result, size, &key, 53 GNUNET_CRYPTO_aes_decrypt (result, size, &key,
54 (const struct 54 (const struct
55 GNUNET_CRYPTO_AesInitializationVector *) 55 GNUNET_CRYPTO_AesInitializationVector *)
56 INITVALUE, res); 56 INITVALUE, res);
57 if (strlen (TESTSTRING) + 1 != size) 57 if (strlen (TESTSTRING) + 1 != size)
58 { 58 {
59 printf ("symciphertest failed: decryptBlock returned %d\n", size); 59 printf ("symciphertest failed: decryptBlock returned %d\n", size);
60 return 1; 60 return 1;
61 } 61 }
62 if (0 != strcmp (res, TESTSTRING)) 62 if (0 != strcmp (res, TESTSTRING))
63 { 63 {
64 printf ("symciphertest failed: %s != %s\n", res, TESTSTRING); 64 printf ("symciphertest failed: %s != %s\n", res, TESTSTRING);
65 return 1; 65 return 1;
66 } 66 }
67 else 67 else
68 return 0; 68 return 0;
69} 69}
@@ -77,16 +77,16 @@ verifyCrypto ()
77 int ret; 77 int ret;
78 78
79 unsigned char plain[] = 79 unsigned char plain[] =
80 { 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249, 80 { 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249,
81 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164 81 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164
82 }; 82 };
83 unsigned char raw_key[] = 83 unsigned char raw_key[] =
84 { 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, 84 { 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25,
85 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, 85 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184,
86 34, 191 86 34, 191
87 }; 87 };
88 unsigned char encrresult[] = 88 unsigned char encrresult[] =
89 { 167, 102, 230, 233, 127, 195, 176, 107, 17, 91, 199, 127, 96, 113, 75, 89 { 167, 102, 230, 233, 127, 195, 176, 107, 17, 91, 199, 127, 96, 113, 75,
90 195, 245, 217, 61, 236, 159, 165, 103, 121, 203, 99, 202, 41, 23, 222, 25, 90 195, 245, 217, 61, 236, 159, 165, 103, 121, 203, 99, 202, 41, 23, 222, 25,
91 102 91 102
92 }; 92 };
@@ -96,54 +96,54 @@ verifyCrypto ()
96 96
97 memcpy (key.key, raw_key, GNUNET_CRYPTO_AES_KEY_LENGTH); 97 memcpy (key.key, raw_key, GNUNET_CRYPTO_AES_KEY_LENGTH);
98 key.crc32 = 98 key.crc32 =
99 htonl (GNUNET_CRYPTO_crc32_n (&key, GNUNET_CRYPTO_AES_KEY_LENGTH)); 99 htonl (GNUNET_CRYPTO_crc32_n (&key, GNUNET_CRYPTO_AES_KEY_LENGTH));
100 100
101 if (ntohl (key.crc32) != (unsigned int) 38125195LL) 101 if (ntohl (key.crc32) != (unsigned int) 38125195LL)
102 { 102 {
103 printf ("Static key has different CRC: %u - %u\n", ntohl (key.crc32), 103 printf ("Static key has different CRC: %u - %u\n", ntohl (key.crc32),
104 key.crc32); 104 key.crc32);
105 105
106 ret = 1; 106 ret = 1;
107 goto error; 107 goto error;
108 } 108 }
109 109
110 if (GNUNET_CRYPTO_AES_KEY_LENGTH != 110 if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
111 GNUNET_CRYPTO_aes_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, 111 GNUNET_CRYPTO_aes_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH, &key,
112 (const struct 112 (const struct
113 GNUNET_CRYPTO_AesInitializationVector *) 113 GNUNET_CRYPTO_AesInitializationVector *)
114 "testtesttesttest", result)) 114 "testtesttesttest", result))
115 { 115 {
116 printf ("Wrong return value from encrypt block.\n"); 116 printf ("Wrong return value from encrypt block.\n");
117 ret = 1; 117 ret = 1;
118 goto error; 118 goto error;
119 } 119 }
120 120
121 if (memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0) 121 if (memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0)
122 { 122 {
123 printf ("Encrypted result wrong.\n"); 123 printf ("Encrypted result wrong.\n");
124 ret = 1; 124 ret = 1;
125 goto error; 125 goto error;
126 } 126 }
127 127
128 res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH); 128 res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH);
129 129
130 if (GNUNET_CRYPTO_AES_KEY_LENGTH != 130 if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
131 GNUNET_CRYPTO_aes_decrypt (result, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, 131 GNUNET_CRYPTO_aes_decrypt (result, GNUNET_CRYPTO_AES_KEY_LENGTH, &key,
132 (const struct 132 (const struct
133 GNUNET_CRYPTO_AesInitializationVector *) 133 GNUNET_CRYPTO_AesInitializationVector *)
134 "testtesttesttest", res)) 134 "testtesttesttest", res))
135 { 135 {
136 printf ("Wrong return value from decrypt block.\n"); 136 printf ("Wrong return value from decrypt block.\n");
137 ret = 1; 137 ret = 1;
138 goto error; 138 goto error;
139 } 139 }
140 140
141 if (memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0) 141 if (memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0)
142 { 142 {
143 printf ("Decrypted result does not match input.\n"); 143 printf ("Decrypted result does not match input.\n");
144 144
145 ret = 1; 145 ret = 1;
146 } 146 }
147 147
148error: 148error:
149 149
@@ -160,15 +160,15 @@ main (int argc, char *argv[])
160 GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL); 160 GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL);
161 GNUNET_CRYPTO_random_disable_entropy_gathering (); 161 GNUNET_CRYPTO_random_disable_entropy_gathering ();
162 GNUNET_assert (strlen (INITVALUE) > 162 GNUNET_assert (strlen (INITVALUE) >
163 sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); 163 sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
164 failureCount += testSymcipher (); 164 failureCount += testSymcipher ();
165 failureCount += verifyCrypto (); 165 failureCount += verifyCrypto ();
166 166
167 if (failureCount != 0) 167 if (failureCount != 0)
168 { 168 {
169 printf ("%d TESTS FAILED!\n", failureCount); 169 printf ("%d TESTS FAILED!\n", failureCount);
170 return -1; 170 return -1;
171 } 171 }
172 return 0; 172 return 0;
173} 173}
174 174
diff --git a/src/util/test_crypto_aes_weak.c b/src/util/test_crypto_aes_weak.c
index a0161b575..25931b625 100644
--- a/src/util/test_crypto_aes_weak.c
+++ b/src/util/test_crypto_aes_weak.c
@@ -40,9 +40,9 @@ printWeakKey (struct GNUNET_CRYPTO_AesSessionKey *key)
40 int i; 40 int i;
41 41
42 for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) 42 for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++)
43 { 43 {
44 printf ("%x ", (int) (key->key[i])); 44 printf ("%x ", (int) (key->key[i]));
45 } 45 }
46} 46}
47 47
48static int 48static int
@@ -55,7 +55,7 @@ testWeakKey ()
55 struct GNUNET_CRYPTO_AesInitializationVector INITVALUE; 55 struct GNUNET_CRYPTO_AesInitializationVector INITVALUE;
56 56
57 memset (&INITVALUE, 42, 57 memset (&INITVALUE, 42,
58 sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); 58 sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
59 /* sorry, this is not a weak key -- I don't have 59 /* sorry, this is not a weak key -- I don't have
60 * any at the moment! */ 60 * any at the moment! */
61 weak_key.key[0] = (char) (0x4c); 61 weak_key.key[0] = (char) (0x4c);
@@ -92,31 +92,31 @@ testWeakKey ()
92 weak_key.key[31] = (char) (0xaa); 92 weak_key.key[31] = (char) (0xaa);
93 /* memset(&weak_key, 0, 32); */ 93 /* memset(&weak_key, 0, 32); */
94 weak_key.crc32 = 94 weak_key.crc32 =
95 htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH)); 95 htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH));
96 96
97 size = 97 size =
98 GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING, 98 GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING,
99 strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key, 99 strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key,
100 &INITVALUE, result); 100 &INITVALUE, result);
101 101
102 if (size == -1) 102 if (size == -1)
103 { 103 {
104 GNUNET_break (0); 104 GNUNET_break (0);
105 return 1; 105 return 1;
106 } 106 }
107 107
108 size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res); 108 size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res);
109 109
110 if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size) 110 if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size)
111 { 111 {
112 GNUNET_break (0); 112 GNUNET_break (0);
113 return 1; 113 return 1;
114 } 114 }
115 if (0 != strcmp (res, WEAK_KEY_TESTSTRING)) 115 if (0 != strcmp (res, WEAK_KEY_TESTSTRING))
116 { 116 {
117 GNUNET_break (0); 117 GNUNET_break (0);
118 return 1; 118 return 1;
119 } 119 }
120 else 120 else
121 return 0; 121 return 0;
122} 122}
@@ -133,42 +133,45 @@ getWeakKeys ()
133 133
134 for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS; 134 for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS;
135 number_of_runs++) 135 number_of_runs++)
136 {
137
138 if (number_of_runs % 1000 == 0)
139 fprintf (stderr, ".");
140 /*printf("Got to run number %d.\n", number_of_runs); */
141 GNUNET_CRYPTO_aes_create_session_key (&sessionkey);
142
143 rc = gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB,
144 0);
145
146 if (rc)
147 { 136 {
148 printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n",
149 number_of_runs, gcry_strerror (rc));
150 continue;
151 }
152 137
153 rc = gcry_cipher_setkey (handle, &sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); 138 if (number_of_runs % 1000 == 0)
139 fprintf (stderr, ".");
140 /*printf("Got to run number %d.\n", number_of_runs); */
141 GNUNET_CRYPTO_aes_create_session_key (&sessionkey);
142
143 rc =
144 gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB,
145 0);
146
147 if (rc)
148 {
149 printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n",
150 number_of_runs, gcry_strerror (rc));
151 continue;
152 }
153
154 rc =
155 gcry_cipher_setkey (handle, &sessionkey,
156 GNUNET_CRYPTO_AES_KEY_LENGTH);
157
158 if ((char) rc == GPG_ERR_WEAK_KEY)
159 {
160 printf ("\nWeak key (in hex): ");
161 printWeakKey (&sessionkey);
162 printf ("\n");
163 number_of_weak_keys++;
164 }
165 else if (rc)
166 {
167 printf ("\nUnexpected error generating keys. Error is %s\n",
168 gcry_strerror (rc));
169 }
170
171 gcry_cipher_close (handle);
154 172
155 if ((char) rc == GPG_ERR_WEAK_KEY)
156 {
157 printf ("\nWeak key (in hex): ");
158 printWeakKey (&sessionkey);
159 printf ("\n");
160 number_of_weak_keys++;
161 }
162 else if (rc)
163 {
164 printf ("\nUnexpected error generating keys. Error is %s\n",
165 gcry_strerror (rc));
166 } 173 }
167 174
168 gcry_cipher_close (handle);
169
170 }
171
172 return number_of_weak_keys; 175 return number_of_weak_keys;
173} 176}
174 177
@@ -180,19 +183,19 @@ main (int argc, char *argv[])
180 GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL); 183 GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL);
181 GNUNET_CRYPTO_random_disable_entropy_gathering (); 184 GNUNET_CRYPTO_random_disable_entropy_gathering ();
182 if (GENERATE_WEAK_KEYS) 185 if (GENERATE_WEAK_KEYS)
183 {
184 weak_keys = getWeakKeys ();
185
186 if (weak_keys == 0)
187 {
188 printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS);
189 }
190 else
191 { 186 {
192 printf ("\n%d weak keys found in %d runs.\n", weak_keys, 187 weak_keys = getWeakKeys ();
193 MAX_WEAK_KEY_TRIALS); 188
189 if (weak_keys == 0)
190 {
191 printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS);
192 }
193 else
194 {
195 printf ("\n%d weak keys found in %d runs.\n", weak_keys,
196 MAX_WEAK_KEY_TRIALS);
197 }
194 } 198 }
195 }
196 199
197 if (testWeakKey () != 0) 200 if (testWeakKey () != 0)
198 return -1; 201 return -1;
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c
index b0fa4f332..a32a28ef0 100644
--- a/src/util/test_crypto_hash.c
+++ b/src/util/test_crypto_hash.c
@@ -42,10 +42,10 @@ test (int number)
42 memset (&h1, number, sizeof (GNUNET_HashCode)); 42 memset (&h1, number, sizeof (GNUNET_HashCode));
43 GNUNET_CRYPTO_hash_to_enc (&h1, &enc); 43 GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
44 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2)) 44 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
45 { 45 {
46 printf ("enc2hash failed!\n"); 46 printf ("enc2hash failed!\n");
47 return 1; 47 return 1;
48 } 48 }
49 if (0 != memcmp (&h1, &h2, sizeof (GNUNET_HashCode))) 49 if (0 != memcmp (&h1, &h2, sizeof (GNUNET_HashCode)))
50 return 1; 50 return 1;
51 return 0; 51 return 0;
@@ -124,8 +124,9 @@ static void
124file_hasher (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 124file_hasher (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125{ 125{
126 GNUNET_assert (NULL != 126 GNUNET_assert (NULL !=
127 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 127 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
128 FILENAME, 1024, &finished_task, cls)); 128 FILENAME, 1024, &finished_task,
129 cls));
129} 130}
130 131
131 132
diff --git a/src/util/test_crypto_hkdf.c b/src/util/test_crypto_hkdf.c
index 752116194..d5ee4d433 100644
--- a/src/util/test_crypto_hkdf.c
+++ b/src/util/test_crypto_hkdf.c
@@ -36,17 +36,17 @@ void
36tc1 () 36tc1 ()
37{ 37{
38 unsigned char ikm[22] = 38 unsigned char ikm[22] =
39 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 39 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
40 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 40 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
41 }; 41 };
42 unsigned char salt[13] = 42 unsigned char salt[13] =
43 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 43 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
44 0x0a, 0x0b, 0x0c 44 0x0a, 0x0b, 0x0c
45 }; 45 };
46 unsigned char info[10] = 46 unsigned char info[10] =
47 { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 }; 47 { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 };
48 unsigned char okm[42] = 48 unsigned char okm[42] =
49 { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 49 { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43,
50 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 50 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a,
51 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 51 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00,
52 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65 52 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65
@@ -56,9 +56,9 @@ tc1 ()
56 56
57 memset (result, 0, sizeof (result)); 57 memset (result, 0, sizeof (result));
58 GNUNET_assert (GNUNET_CRYPTO_hkdf 58 GNUNET_assert (GNUNET_CRYPTO_hkdf
59 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, 59 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt,
60 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), 60 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info),
61 NULL) == GNUNET_YES); 61 NULL) == GNUNET_YES);
62 GNUNET_assert (memcmp (result, okm, l) == 0); 62 GNUNET_assert (memcmp (result, okm, l) == 0);
63 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 63 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
64} 64}
@@ -67,7 +67,7 @@ void
67tc2 () 67tc2 ()
68{ 68{
69 unsigned char ikm[80] = 69 unsigned char ikm[80] =
70 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 70 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
71 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 71 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
72 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 72 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
73 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 73 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
@@ -76,7 +76,7 @@ tc2 ()
76 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f 76 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
77 }; 77 };
78 unsigned char salt[80] = 78 unsigned char salt[80] =
79 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 79 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
80 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 80 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
81 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 81 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81,
82 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 82 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
@@ -85,7 +85,7 @@ tc2 ()
85 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf 85 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf
86 }; 86 };
87 unsigned char info[80] = 87 unsigned char info[80] =
88 { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 88 { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
89 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 89 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
90 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 90 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
91 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 91 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd,
@@ -94,7 +94,7 @@ tc2 ()
94 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff 94 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
95 }; 95 };
96 unsigned char okm[82] = 96 unsigned char okm[82] =
97 { 0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7, 97 { 0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7,
98 0xf7, 0x8c, 0x59, 0x6a, 0x49, 0x34, 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e, 98 0xf7, 0x8c, 0x59, 0x6a, 0x49, 0x34, 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e,
99 0xfa, 0xd8, 0xa0, 0x50, 0xcc, 0x4c, 0x19, 0xaf, 0xa9, 0x7c, 0x59, 0x04, 99 0xfa, 0xd8, 0xa0, 0x50, 0xcc, 0x4c, 0x19, 0xaf, 0xa9, 0x7c, 0x59, 0x04,
100 0x5a, 0x99, 0xca, 0xc7, 0x82, 0x72, 0x71, 0xcb, 0x41, 0xc6, 0x5e, 0x59, 100 0x5a, 0x99, 0xca, 0xc7, 0x82, 0x72, 0x71, 0xcb, 0x41, 0xc6, 0x5e, 0x59,
@@ -107,9 +107,9 @@ tc2 ()
107 107
108 memset (result, 0, sizeof (result)); 108 memset (result, 0, sizeof (result));
109 GNUNET_assert (GNUNET_CRYPTO_hkdf 109 GNUNET_assert (GNUNET_CRYPTO_hkdf
110 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, 110 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt,
111 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), 111 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info),
112 NULL) == GNUNET_YES); 112 NULL) == GNUNET_YES);
113 GNUNET_assert (memcmp (result, okm, l) == 0); 113 GNUNET_assert (memcmp (result, okm, l) == 0);
114 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 114 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
115} 115}
@@ -118,11 +118,11 @@ void
118tc3 () 118tc3 ()
119{ 119{
120 unsigned char ikm[22] = 120 unsigned char ikm[22] =
121 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 121 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
122 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 122 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
123 }; 123 };
124 unsigned char okm[42] = 124 unsigned char okm[42] =
125 { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f, 0x71, 0x5f, 125 { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f, 0x71, 0x5f,
126 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31, 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 126 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31, 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1,
127 0x87, 0x9e, 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d, 0x9d, 0x20, 127 0x87, 0x9e, 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d, 0x9d, 0x20,
128 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a, 0x96, 0xc8 128 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a, 0x96, 0xc8
@@ -132,8 +132,8 @@ tc3 ()
132 132
133 memset (result, 0, sizeof (result)); 133 memset (result, 0, sizeof (result));
134 GNUNET_assert (GNUNET_CRYPTO_hkdf 134 GNUNET_assert (GNUNET_CRYPTO_hkdf
135 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, NULL, 0, ikm, 135 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, NULL, 0, ikm,
136 sizeof (ikm), NULL, 0, NULL) == GNUNET_YES); 136 sizeof (ikm), NULL, 0, NULL) == GNUNET_YES);
137 GNUNET_assert (memcmp (result, okm, l) == 0); 137 GNUNET_assert (memcmp (result, okm, l) == 0);
138 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 138 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
139} 139}
@@ -142,17 +142,17 @@ void
142tc4 () 142tc4 ()
143{ 143{
144 unsigned char ikm[11] = 144 unsigned char ikm[11] =
145 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 145 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
146 0x0b 146 0x0b
147 }; 147 };
148 unsigned char salt[13] = 148 unsigned char salt[13] =
149 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 149 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
150 0x0a, 0x0b, 0x0c 150 0x0a, 0x0b, 0x0c
151 }; 151 };
152 unsigned char info[10] = 152 unsigned char info[10] =
153 { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 }; 153 { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 };
154 unsigned char okm[42] = 154 unsigned char okm[42] =
155 { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69, 0x33, 0x06, 155 { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69, 0x33, 0x06,
156 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81, 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 156 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81, 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b,
157 0x09, 0x15, 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2, 0xc2, 0x2e, 157 0x09, 0x15, 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2, 0xc2, 0x2e,
158 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3, 0xf8, 0x96 158 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3, 0xf8, 0x96
@@ -162,8 +162,9 @@ tc4 ()
162 162
163 memset (result, 0, sizeof (result)); 163 memset (result, 0, sizeof (result));
164 GNUNET_assert (GNUNET_CRYPTO_hkdf 164 GNUNET_assert (GNUNET_CRYPTO_hkdf
165 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), 165 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt),
166 ikm, sizeof (ikm), info, sizeof (info), NULL) == GNUNET_YES); 166 ikm, sizeof (ikm), info, sizeof (info),
167 NULL) == GNUNET_YES);
167 GNUNET_assert (memcmp (result, okm, l) == 0); 168 GNUNET_assert (memcmp (result, okm, l) == 0);
168 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 169 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
169} 170}
@@ -172,7 +173,7 @@ void
172tc5 () 173tc5 ()
173{ 174{
174 unsigned char ikm[80] = 175 unsigned char ikm[80] =
175 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 176 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
176 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 177 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
177 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 178 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
178 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 179 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
@@ -181,7 +182,7 @@ tc5 ()
181 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f 182 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
182 }; 183 };
183 unsigned char salt[80] = 184 unsigned char salt[80] =
184 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 185 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
185 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 186 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
186 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 187 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81,
187 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 188 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
@@ -190,7 +191,7 @@ tc5 ()
190 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf 191 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf
191 }; 192 };
192 unsigned char info[80] = 193 unsigned char info[80] =
193 { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 194 { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
194 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 195 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
195 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 196 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
196 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 197 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd,
@@ -199,7 +200,7 @@ tc5 ()
199 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff 200 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
200 }; 201 };
201 unsigned char okm[82] = 202 unsigned char okm[82] =
202 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 203 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1,
203 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 204 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d,
204 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3, 205 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3,
205 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, 206 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2,
@@ -212,8 +213,9 @@ tc5 ()
212 213
213 memset (result, 0, sizeof (result)); 214 memset (result, 0, sizeof (result));
214 GNUNET_assert (GNUNET_CRYPTO_hkdf 215 GNUNET_assert (GNUNET_CRYPTO_hkdf
215 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), 216 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt),
216 ikm, sizeof (ikm), info, sizeof (info), NULL) == GNUNET_YES); 217 ikm, sizeof (ikm), info, sizeof (info),
218 NULL) == GNUNET_YES);
217 GNUNET_assert (memcmp (result, okm, l) == 0); 219 GNUNET_assert (memcmp (result, okm, l) == 0);
218 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 220 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
219} 221}
@@ -222,11 +224,11 @@ void
222tc6 () 224tc6 ()
223{ 225{
224 unsigned char ikm[22] = 226 unsigned char ikm[22] =
225 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 227 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
226 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 228 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
227 }; 229 };
228 unsigned char okm[42] = 230 unsigned char okm[42] =
229 { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61, 0xd1, 0xe5, 231 { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61, 0xd1, 0xe5,
230 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06, 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 232 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06, 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20,
231 0xa3, 0x06, 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0, 0xea, 0x00, 233 0xa3, 0x06, 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0, 0xea, 0x00,
232 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3, 0x49, 0x18 234 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3, 0x49, 0x18
@@ -236,8 +238,8 @@ tc6 ()
236 238
237 memset (result, 0, sizeof (result)); 239 memset (result, 0, sizeof (result));
238 GNUNET_assert (GNUNET_CRYPTO_hkdf 240 GNUNET_assert (GNUNET_CRYPTO_hkdf
239 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm, 241 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm,
240 sizeof (ikm), NULL, 0, NULL) == GNUNET_YES); 242 sizeof (ikm), NULL, 0, NULL) == GNUNET_YES);
241 GNUNET_assert (memcmp (result, okm, l) == 0); 243 GNUNET_assert (memcmp (result, okm, l) == 0);
242 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 244 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
243} 245}
@@ -246,7 +248,7 @@ void
246tc7 () 248tc7 ()
247{ 249{
248 unsigned char ikm[80] = 250 unsigned char ikm[80] =
249 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 251 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
250 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 252 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
251 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 253 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
252 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 254 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
@@ -255,7 +257,7 @@ tc7 ()
255 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f 257 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
256 }; 258 };
257 unsigned char salt[80] = 259 unsigned char salt[80] =
258 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 260 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
259 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 261 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
260 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 262 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81,
261 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 263 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
@@ -275,7 +277,7 @@ tc7 ()
275 0xfe, 0xff 277 0xfe, 0xff
276 }; 278 };
277 unsigned char okm[82] = 279 unsigned char okm[82] =
278 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 280 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1,
279 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 281 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d,
280 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3, 282 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3,
281 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, 283 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2,
@@ -288,9 +290,9 @@ tc7 ()
288 290
289 memset (result, 0, sizeof (result)); 291 memset (result, 0, sizeof (result));
290 GNUNET_assert (GNUNET_CRYPTO_hkdf 292 GNUNET_assert (GNUNET_CRYPTO_hkdf
291 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), 293 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt),
292 ikm, sizeof (ikm), info1, sizeof (info1), info2, 294 ikm, sizeof (ikm), info1, sizeof (info1), info2,
293 sizeof (info2), NULL) == GNUNET_YES); 295 sizeof (info2), NULL) == GNUNET_YES);
294 GNUNET_assert (memcmp (result, okm, l) == 0); 296 GNUNET_assert (memcmp (result, okm, l) == 0);
295 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 297 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
296} 298}
@@ -299,13 +301,13 @@ void
299tc8 () 301tc8 ()
300{ 302{
301 unsigned char ikm[32] = 303 unsigned char ikm[32] =
302 { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72, 304 { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72,
303 0x11, 0xa1, 0xdc, 0x0b, 0x07, 0xdb, 0x1a, 0x5e, 0xd9, 0xb9, 0x81, 0xbe, 305 0x11, 0xa1, 0xdc, 0x0b, 0x07, 0xdb, 0x1a, 0x5e, 0xd9, 0xb9, 0x81, 0xbe,
304 0xea, 0xe4, 0x31, 0x5f, 0x24, 0xff, 0xfe, 0x50, 0x8a, 0xde 306 0xea, 0xe4, 0x31, 0x5f, 0x24, 0xff, 0xfe, 0x50, 0x8a, 0xde
305 }; 307 };
306 unsigned char salt[4] = { 0xfc, 0x62, 0x76, 0x35 }; 308 unsigned char salt[4] = { 0xfc, 0x62, 0x76, 0x35 };
307 unsigned char info[86] = 309 unsigned char info[86] =
308 { 0x8c, 0x0d, 0xcf, 0xb3, 0x25, 0x6e, 0x88, 0x0d, 0xc1, 0x0b, 310 { 0x8c, 0x0d, 0xcf, 0xb3, 0x25, 0x6e, 0x88, 0x0d, 0xc1, 0x0b,
309 0x1d, 0x33, 0x15, 0x3e, 0x52, 0x0b, 0xb0, 0x77, 0xff, 0x7d, 0xc3, 0xc7, 311 0x1d, 0x33, 0x15, 0x3e, 0x52, 0x0b, 0xb0, 0x77, 0xff, 0x7d, 0xc3, 0xc7,
310 0xef, 0xe5, 0x8e, 0x3c, 0xc4, 0x4e, 0x8b, 0x41, 0x46, 0x1f, 0x02, 0x94, 312 0xef, 0xe5, 0x8e, 0x3c, 0xc4, 0x4e, 0x8b, 0x41, 0x46, 0x1f, 0x02, 0x94,
311 0x82, 0x35, 0xc5, 0xa6, 0x5e, 0x91, 0xd8, 0xa2, 0x90, 0xfd, 0x6f, 0xb4, 313 0x82, 0x35, 0xc5, 0xa6, 0x5e, 0x91, 0xd8, 0xa2, 0x90, 0xfd, 0x6f, 0xb4,
@@ -315,7 +317,7 @@ tc8 ()
315 0x74, 0x6f, 0x72, 0x00 317 0x74, 0x6f, 0x72, 0x00
316 }; 318 };
317 unsigned char okm[16] = 319 unsigned char okm[16] =
318 { 0xd6, 0x90, 0xec, 0x9e, 0x62, 0xdf, 0xb9, 0x41, 0xff, 0x92, 320 { 0xd6, 0x90, 0xec, 0x9e, 0x62, 0xdf, 0xb9, 0x41, 0xff, 0x92,
319 0x4f, 0xd2, 0xf6, 0x1d, 0x67, 0xe0 321 0x4f, 0xd2, 0xf6, 0x1d, 0x67, 0xe0
320 }; 322 };
321 char result[18]; 323 char result[18];
@@ -323,9 +325,9 @@ tc8 ()
323 325
324 memset (result, 0, sizeof (result)); 326 memset (result, 0, sizeof (result));
325 GNUNET_assert (GNUNET_CRYPTO_hkdf 327 GNUNET_assert (GNUNET_CRYPTO_hkdf
326 (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt, 328 (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt,
327 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), 329 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info),
328 NULL) == GNUNET_YES); 330 NULL) == GNUNET_YES);
329 GNUNET_assert (memcmp (result, okm, l) == 0); 331 GNUNET_assert (memcmp (result, okm, l) == 0);
330 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 332 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
331} 333}
diff --git a/src/util/test_crypto_ksk.c b/src/util/test_crypto_ksk.c
index 2789f5edb..55d2dffcb 100644
--- a/src/util/test_crypto_ksk.c
+++ b/src/util/test_crypto_ksk.c
@@ -39,7 +39,7 @@ static int
39testCorrectKey () 39testCorrectKey ()
40{ 40{
41 const char *want = 41 const char *want =
42 "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000"; 42 "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000";
43 GNUNET_HashCode in; 43 GNUNET_HashCode in;
44 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey; 44 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
45 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; 45 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
@@ -50,27 +50,29 @@ testCorrectKey ()
50 GNUNET_CRYPTO_hash ("X", strlen ("X"), &in); 50 GNUNET_CRYPTO_hash ("X", strlen ("X"), &in);
51 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); 51 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
52 if (hostkey == NULL) 52 if (hostkey == NULL)
53 { 53 {
54 GNUNET_break (0); 54 GNUNET_break (0);
55 return GNUNET_SYSERR; 55 return GNUNET_SYSERR;
56 } 56 }
57 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); 57 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
58 GNUNET_CRYPTO_rsa_key_free (hostkey); 58 GNUNET_CRYPTO_rsa_key_free (hostkey);
59#if 0 59#if 0
60 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++) 60 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
61 i++)
61 printf ("%02x", ((unsigned char *) &pkey)[i]); 62 printf ("%02x", ((unsigned char *) &pkey)[i]);
62 printf ("\n"); 63 printf ("\n");
63#endif 64#endif
64 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++) 65 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
65 { 66 i++)
66 snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]);
67 if (0 != strncmp (out, &want[i * 2], 2))
68 { 67 {
69 fprintf (stderr, " Failed! Wanted %.2s but got %2s at %d\n", &want[i * 2], 68 snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]);
70 out, i); 69 if (0 != strncmp (out, &want[i * 2], 2))
71 return GNUNET_SYSERR; 70 {
71 fprintf (stderr, " Failed! Wanted %.2s but got %2s at %d\n",
72 &want[i * 2], out, i);
73 return GNUNET_SYSERR;
74 }
72 } 75 }
73 }
74 fprintf (stderr, " OK\n"); 76 fprintf (stderr, " OK\n");
75 return GNUNET_OK; 77 return GNUNET_OK;
76} 78}
@@ -89,10 +91,10 @@ testMultiKey (const char *word)
89 GNUNET_CRYPTO_hash (word, strlen (word), &in); 91 GNUNET_CRYPTO_hash (word, strlen (word), &in);
90 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); 92 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
91 if (hostkey == NULL) 93 if (hostkey == NULL)
92 { 94 {
93 GNUNET_break (0); 95 GNUNET_break (0);
94 return GNUNET_SYSERR; 96 return GNUNET_SYSERR;
95 } 97 }
96 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); 98 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
97 /* 99 /*
98 * for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++) 100 * for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
@@ -100,26 +102,26 @@ testMultiKey (const char *word)
100 * printf("\n"); */ 102 * printf("\n"); */
101 GNUNET_CRYPTO_rsa_key_free (hostkey); 103 GNUNET_CRYPTO_rsa_key_free (hostkey);
102 for (i = 0; i < UNIQUE_ITER; i++) 104 for (i = 0; i < UNIQUE_ITER; i++)
103 {
104 fprintf (stderr, ".");
105 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
106 if (hostkey == NULL)
107 { 105 {
108 GNUNET_break (0); 106 fprintf (stderr, ".");
109 fprintf (stderr, " ERROR\n"); 107 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
110 return GNUNET_SYSERR; 108 if (hostkey == NULL)
109 {
110 GNUNET_break (0);
111 fprintf (stderr, " ERROR\n");
112 return GNUNET_SYSERR;
113 }
114 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1);
115 GNUNET_CRYPTO_rsa_key_free (hostkey);
116 if (0 !=
117 memcmp (&pkey, &pkey1,
118 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
119 {
120 GNUNET_break (0);
121 fprintf (stderr, " ERROR\n");
122 return GNUNET_SYSERR;
123 }
111 } 124 }
112 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1);
113 GNUNET_CRYPTO_rsa_key_free (hostkey);
114 if (0 !=
115 memcmp (&pkey, &pkey1,
116 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
117 {
118 GNUNET_break (0);
119 fprintf (stderr, " ERROR\n");
120 return GNUNET_SYSERR;
121 }
122 }
123 fprintf (stderr, " OK\n"); 125 fprintf (stderr, " OK\n");
124 return GNUNET_OK; 126 return GNUNET_OK;
125} 127}
@@ -141,35 +143,35 @@ testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
141 ok = 0; 143 ok = 0;
142 start = GNUNET_TIME_absolute_get (); 144 start = GNUNET_TIME_absolute_get ();
143 for (i = 0; i < ITER; i++) 145 for (i = 0; i < ITER; i++)
144 {
145 fprintf (stderr, ".");
146 if (GNUNET_SYSERR ==
147 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
148 &target))
149 {
150 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
151 ok++;
152 continue;
153 }
154 if (-1 ==
155 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
156 strlen (TESTSTRING) + 1))
157 { 146 {
158 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); 147 fprintf (stderr, ".");
159 ok++; 148 if (GNUNET_SYSERR ==
160 continue; 149 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
150 &pkey, &target))
151 {
152 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
153 ok++;
154 continue;
155 }
156 if (-1 ==
157 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
158 strlen (TESTSTRING) + 1))
159 {
160 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
161 ok++;
162 continue;
163 }
164 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
165 {
166 printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
167 MAX_TESTVAL, result);
168 ok++;
169 continue;
170 }
161 } 171 }
162 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
163 {
164 printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
165 MAX_TESTVAL, result);
166 ok++;
167 continue;
168 }
169 }
170 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER, 172 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER,
171 (unsigned long long) 173 (unsigned long long)
172 GNUNET_TIME_absolute_get_duration (start).rel_value, ok); 174 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
173 if (ok == 0) 175 if (ok == 0)
174 return GNUNET_OK; 176 return GNUNET_OK;
175 else 177 else
@@ -192,34 +194,34 @@ testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
192 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)); 194 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
193 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); 195 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
194 for (i = 0; i < ITER; i++) 196 for (i = 0; i < ITER; i++)
195 {
196 fprintf (stderr, ".");
197 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
198 {
199 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
200 ok = GNUNET_SYSERR;
201 continue;
202 }
203 if (GNUNET_SYSERR ==
204 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
205 &pkey))
206 {
207 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
208 ok = GNUNET_SYSERR;
209 continue;
210 }
211 if (GNUNET_SYSERR !=
212 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
213 &purp, &sig, &pkey))
214 { 197 {
215 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n"); 198 fprintf (stderr, ".");
216 ok = GNUNET_SYSERR; 199 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
217 continue; 200 {
201 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
202 ok = GNUNET_SYSERR;
203 continue;
204 }
205 if (GNUNET_SYSERR ==
206 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp,
207 &sig, &pkey))
208 {
209 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
210 ok = GNUNET_SYSERR;
211 continue;
212 }
213 if (GNUNET_SYSERR !=
214 GNUNET_CRYPTO_rsa_verify
215 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
216 {
217 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
218 ok = GNUNET_SYSERR;
219 continue;
220 }
218 } 221 }
219 }
220 printf ("%d RSA sign/verify operations %llums\n", ITER, 222 printf ("%d RSA sign/verify operations %llums\n", ITER,
221 (unsigned long long) 223 (unsigned long long)
222 GNUNET_TIME_absolute_get_duration (start).rel_value); 224 GNUNET_TIME_absolute_get_duration (start).rel_value);
223 return ok; 225 return ok;
224} 226}
225 227
@@ -237,10 +239,10 @@ main (int argc, char *argv[])
237 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in); 239 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in);
238 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); 240 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
239 if (hostkey == NULL) 241 if (hostkey == NULL)
240 { 242 {
241 printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n"); 243 printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
242 return 1; 244 return 1;
243 } 245 }
244 if (GNUNET_OK != testMultiKey ("foo")) 246 if (GNUNET_OK != testMultiKey ("foo"))
245 failureCount++; 247 failureCount++;
246 if (GNUNET_OK != testMultiKey ("bar")) 248 if (GNUNET_OK != testMultiKey ("bar"))
@@ -252,9 +254,9 @@ main (int argc, char *argv[])
252 GNUNET_CRYPTO_rsa_key_free (hostkey); 254 GNUNET_CRYPTO_rsa_key_free (hostkey);
253 255
254 if (failureCount != 0) 256 if (failureCount != 0)
255 { 257 {
256 printf ("\n\n%d TESTS FAILED!\n\n", failureCount); 258 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
257 return -1; 259 return -1;
258 } 260 }
259 return 0; 261 return 0;
260} 262}
diff --git a/src/util/test_crypto_random.c b/src/util/test_crypto_random.c
index 46d3ed0aa..3b86ececd 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)
39 for (i = 0; i < 1024; i++) 39 for (i = 0; i < 1024; i++)
40 GNUNET_break (1024 > (buf[i] = GNUNET_CRYPTO_random_u32 (mode, 1024))); 40 GNUNET_break (1024 > (buf[i] = GNUNET_CRYPTO_random_u32 (mode, 1024)));
41 for (i = 0; i < 10; i++) 41 for (i = 0; i < 10; i++)
42 {
43 b2 = GNUNET_CRYPTO_random_permute (mode, 1024);
44 if (0 == memcmp (b2, buf, sizeof (buf)))
45 { 42 {
46 fprintf (stderr, "!"); 43 b2 = GNUNET_CRYPTO_random_permute (mode, 1024);
44 if (0 == memcmp (b2, buf, sizeof (buf)))
45 {
46 fprintf (stderr, "!");
47 GNUNET_free (b2);
48 continue;
49 }
47 GNUNET_free (b2); 50 GNUNET_free (b2);
48 continue; 51 break;
49 } 52 }
50 GNUNET_free (b2);
51 break;
52 }
53 if (i == 10) 53 if (i == 10)
54 return 1; /* virtually impossible... */ 54 return 1; /* virtually impossible... */
55 55
56 for (n = 10; n < 1024LL * 1024LL * 1024LL; n *= 10) 56 for (n = 10; n < 1024LL * 1024LL * 1024LL; n *= 10)
57 GNUNET_break (n > GNUNET_CRYPTO_random_u64 (mode, n)); 57 GNUNET_break (n > GNUNET_CRYPTO_random_u64 (mode, n));
diff --git a/src/util/test_crypto_rsa.c b/src/util/test_crypto_rsa.c
index f26a76a38..6a2c1b355 100644
--- a/src/util/test_crypto_rsa.c
+++ b/src/util/test_crypto_rsa.c
@@ -54,36 +54,36 @@ testEncryptDecrypt ()
54 ok = 0; 54 ok = 0;
55 start = GNUNET_TIME_absolute_get (); 55 start = GNUNET_TIME_absolute_get ();
56 for (i = 0; i < ITER; i++) 56 for (i = 0; i < ITER; i++)
57 {
58 fprintf (stderr, ".");
59 if (GNUNET_SYSERR ==
60 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
61 &target))
62 { 57 {
63 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); 58 fprintf (stderr, ".");
64 ok++; 59 if (GNUNET_SYSERR ==
65 continue; 60 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
66 } 61 &pkey, &target))
67 if (-1 == 62 {
68 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result, 63 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
69 strlen (TESTSTRING) + 1)) 64 ok++;
70 { 65 continue;
71 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); 66 }
72 ok++; 67 if (-1 ==
73 continue; 68 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
69 strlen (TESTSTRING) + 1))
70 {
71 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
72 ok++;
73 continue;
74 74
75 }
76 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
77 {
78 printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
79 (int) MAX_TESTVAL, result);
80 ok++;
81 continue;
82 }
75 } 83 }
76 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
77 {
78 printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
79 (int) MAX_TESTVAL, result);
80 ok++;
81 continue;
82 }
83 }
84 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER, 84 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER,
85 (unsigned long long) 85 (unsigned long long)
86 GNUNET_TIME_absolute_get_duration (start).rel_value, ok); 86 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
87 GNUNET_CRYPTO_rsa_key_free (hostkey); 87 GNUNET_CRYPTO_rsa_key_free (hostkey);
88 if (ok == 0) 88 if (ok == 0)
89 return GNUNET_OK; 89 return GNUNET_OK;
@@ -109,20 +109,20 @@ testEncryptPerformance ()
109 ok = 0; 109 ok = 0;
110 start = GNUNET_TIME_absolute_get (); 110 start = GNUNET_TIME_absolute_get ();
111 for (i = 0; i < ITER; i++) 111 for (i = 0; i < ITER; i++)
112 {
113 fprintf (stderr, ".");
114 if (GNUNET_SYSERR ==
115 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
116 &target))
117 { 112 {
118 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); 113 fprintf (stderr, ".");
119 ok++; 114 if (GNUNET_SYSERR ==
120 continue; 115 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
116 &pkey, &target))
117 {
118 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
119 ok++;
120 continue;
121 }
121 } 122 }
122 }
123 printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER, 123 printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER,
124 (unsigned long long) 124 (unsigned long long)
125 GNUNET_TIME_absolute_get_duration (start).rel_value, ok); 125 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
126 GNUNET_CRYPTO_rsa_key_free (hostkey); 126 GNUNET_CRYPTO_rsa_key_free (hostkey);
127 if (ok != 0) 127 if (ok != 0)
128 return GNUNET_SYSERR; 128 return GNUNET_SYSERR;
@@ -149,37 +149,39 @@ testEncryptDecryptSK ()
149 ok = 0; 149 ok = 0;
150 start = GNUNET_TIME_absolute_get (); 150 start = GNUNET_TIME_absolute_get ();
151 for (i = 0; i < ITER; i++) 151 for (i = 0; i < ITER; i++)
152 {
153 fprintf (stderr, ".");
154 GNUNET_CRYPTO_aes_create_session_key (&insk);
155 if (GNUNET_SYSERR ==
156 GNUNET_CRYPTO_rsa_encrypt (&insk,
157 sizeof (struct GNUNET_CRYPTO_AesSessionKey),
158 &pkey, &target))
159 {
160 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
161 ok++;
162 continue;
163 }
164 if (-1 ==
165 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk,
166 sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
167 {
168 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
169 ok++;
170 continue;
171 }
172 if (0 !=
173 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
174 { 152 {
175 printf ("testEncryptDecryptSK failed!\n"); 153 fprintf (stderr, ".");
176 ok++; 154 GNUNET_CRYPTO_aes_create_session_key (&insk);
177 continue; 155 if (GNUNET_SYSERR ==
156 GNUNET_CRYPTO_rsa_encrypt (&insk,
157 sizeof (struct
158 GNUNET_CRYPTO_AesSessionKey),
159 &pkey, &target))
160 {
161 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
162 ok++;
163 continue;
164 }
165 if (-1 ==
166 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk,
167 sizeof (struct
168 GNUNET_CRYPTO_AesSessionKey)))
169 {
170 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
171 ok++;
172 continue;
173 }
174 if (0 !=
175 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
176 {
177 printf ("testEncryptDecryptSK failed!\n");
178 ok++;
179 continue;
180 }
178 } 181 }
179 }
180 printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER, 182 printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER,
181 (unsigned long long) 183 (unsigned long long)
182 GNUNET_TIME_absolute_get_duration (start).rel_value, ok); 184 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
183 GNUNET_CRYPTO_rsa_key_free (hostkey); 185 GNUNET_CRYPTO_rsa_key_free (hostkey);
184 if (ok != 0) 186 if (ok != 0)
185 return GNUNET_SYSERR; 187 return GNUNET_SYSERR;
@@ -206,34 +208,34 @@ testSignVerify ()
206 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); 208 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
207 209
208 for (i = 0; i < ITER; i++) 210 for (i = 0; i < ITER; i++)
209 {
210 fprintf (stderr, ".");
211 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
212 {
213 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
214 ok = GNUNET_SYSERR;
215 continue;
216 }
217 if (GNUNET_SYSERR ==
218 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
219 &pkey))
220 { 211 {
221 printf ("GNUNET_CRYPTO_rsa_verify failed!\n"); 212 fprintf (stderr, ".");
222 ok = GNUNET_SYSERR; 213 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
223 continue; 214 {
215 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
216 ok = GNUNET_SYSERR;
217 continue;
218 }
219 if (GNUNET_SYSERR ==
220 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp,
221 &sig, &pkey))
222 {
223 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
224 ok = GNUNET_SYSERR;
225 continue;
226 }
227 if (GNUNET_SYSERR !=
228 GNUNET_CRYPTO_rsa_verify
229 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
230 {
231 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
232 ok = GNUNET_SYSERR;
233 continue;
234 }
224 } 235 }
225 if (GNUNET_SYSERR !=
226 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
227 &purp, &sig, &pkey))
228 {
229 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
230 ok = GNUNET_SYSERR;
231 continue;
232 }
233 }
234 printf ("%d RSA sign/verify operations %llums\n", ITER, 236 printf ("%d RSA sign/verify operations %llums\n", ITER,
235 (unsigned long long) 237 (unsigned long long)
236 GNUNET_TIME_absolute_get_duration (start).rel_value); 238 GNUNET_TIME_absolute_get_duration (start).rel_value);
237 GNUNET_CRYPTO_rsa_key_free (hostkey); 239 GNUNET_CRYPTO_rsa_key_free (hostkey);
238 return ok; 240 return ok;
239} 241}
@@ -258,18 +260,18 @@ testSignPerformance ()
258 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); 260 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
259 start = GNUNET_TIME_absolute_get (); 261 start = GNUNET_TIME_absolute_get ();
260 for (i = 0; i < ITER; i++) 262 for (i = 0; i < ITER; i++)
261 {
262 fprintf (stderr, ".");
263 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
264 { 263 {
265 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); 264 fprintf (stderr, ".");
266 ok = GNUNET_SYSERR; 265 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
267 continue; 266 {
267 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
268 ok = GNUNET_SYSERR;
269 continue;
270 }
268 } 271 }
269 }
270 printf ("%d RSA sign operations %llu ms\n", ITER, 272 printf ("%d RSA sign operations %llu ms\n", ITER,
271 (unsigned long long) 273 (unsigned long long)
272 GNUNET_TIME_absolute_get_duration (start).rel_value); 274 GNUNET_TIME_absolute_get_duration (start).rel_value);
273 GNUNET_CRYPTO_rsa_key_free (hostkey); 275 GNUNET_CRYPTO_rsa_key_free (hostkey);
274 return ok; 276 return ok;
275} 277}
@@ -326,9 +328,9 @@ main (int argc, char *argv[])
326 failureCount++; 328 failureCount++;
327 329
328 if (failureCount != 0) 330 if (failureCount != 0)
329 { 331 {
330 printf ("\n\n%d TESTS FAILED!\n\n", failureCount); 332 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
331 return -1; 333 return -1;
332 } 334 }
333 return 0; 335 return 0;
334} /* end of main */ 336} /* end of main */
diff --git a/src/util/test_disk.c b/src/util/test_disk.c
index 91154a6f5..e8ec88c9f 100644
--- a/src/util/test_disk.c
+++ b/src/util/test_disk.c
@@ -38,40 +38,41 @@ testReadWrite ()
38 38
39 if (strlen (TESTSTRING) != 39 if (strlen (TESTSTRING) !=
40 GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING), 40 GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
41 GNUNET_DISK_PERM_USER_READ | 41 GNUNET_DISK_PERM_USER_READ |
42 GNUNET_DISK_PERM_USER_WRITE)) 42 GNUNET_DISK_PERM_USER_WRITE))
43 return 1; 43 return 1;
44 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile")) 44 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
45 return 1; 45 return 1;
46 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1); 46 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
47 if (ret < 0) 47 if (ret < 0)
48 { 48 {
49 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile"); 49 fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
50 return 1; 50 ".testfile");
51 } 51 return 1;
52 }
52 tmp[ret] = '\0'; 53 tmp[ret] = '\0';
53 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) 54 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
54 { 55 {
55 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, 56 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n",
56 TESTSTRING, ".testfile"); 57 tmp, TESTSTRING, ".testfile");
57 return 1; 58 return 1;
58 } 59 }
59 GNUNET_DISK_file_copy (".testfile", ".testfile2"); 60 GNUNET_DISK_file_copy (".testfile", ".testfile2");
60 memset (tmp, 0, sizeof (tmp)); 61 memset (tmp, 0, sizeof (tmp));
61 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1); 62 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
62 if (ret < 0) 63 if (ret < 0)
63 { 64 {
64 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", 65 fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
65 ".testfile2"); 66 ".testfile2");
66 return 1; 67 return 1;
67 } 68 }
68 tmp[ret] = '\0'; 69 tmp[ret] = '\0';
69 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) 70 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
70 { 71 {
71 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, 72 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n",
72 TESTSTRING, ".testfile2"); 73 tmp, TESTSTRING, ".testfile2");
73 return 1; 74 return 1;
74 } 75 }
75 76
76 GNUNET_break (0 == UNLINK (".testfile")); 77 GNUNET_break (0 == UNLINK (".testfile"));
77 GNUNET_break (0 == UNLINK (".testfile2")); 78 GNUNET_break (0 == UNLINK (".testfile2"));
@@ -89,15 +90,15 @@ testOpenClose ()
89 long avail; 90 long avail;
90 91
91 fh = GNUNET_DISK_file_open (".testfile", 92 fh = GNUNET_DISK_file_open (".testfile",
92 GNUNET_DISK_OPEN_READWRITE | 93 GNUNET_DISK_OPEN_READWRITE |
93 GNUNET_DISK_OPEN_CREATE, 94 GNUNET_DISK_OPEN_CREATE,
94 GNUNET_DISK_PERM_USER_READ | 95 GNUNET_DISK_PERM_USER_READ |
95 GNUNET_DISK_PERM_USER_WRITE); 96 GNUNET_DISK_PERM_USER_WRITE);
96 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh)); 97 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
97 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5)); 98 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
98 GNUNET_DISK_file_close (fh); 99 GNUNET_DISK_file_close (fh);
99 GNUNET_break (GNUNET_OK == 100 GNUNET_break (GNUNET_OK ==
100 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO)); 101 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
101 if (size != 5) 102 if (size != 5)
102 return 1; 103 return 1;
103 GNUNET_break (0 == UNLINK (".testfile")); 104 GNUNET_break (0 == UNLINK (".testfile"));
@@ -107,19 +108,19 @@ testOpenClose ()
107 avail = GNUNET_DISK_get_blocks_available (".testfile"); 108 avail = GNUNET_DISK_get_blocks_available (".testfile");
108 GNUNET_log_skip (0, GNUNET_NO); 109 GNUNET_log_skip (0, GNUNET_NO);
109 fh = GNUNET_DISK_file_open (".testfile", 110 fh = GNUNET_DISK_file_open (".testfile",
110 GNUNET_DISK_OPEN_READWRITE | 111 GNUNET_DISK_OPEN_READWRITE |
111 GNUNET_DISK_OPEN_CREATE, 112 GNUNET_DISK_OPEN_CREATE,
112 GNUNET_DISK_PERM_USER_WRITE | 113 GNUNET_DISK_PERM_USER_WRITE |
113 GNUNET_DISK_PERM_USER_READ); 114 GNUNET_DISK_PERM_USER_READ);
114 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh)); 115 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
115 while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) && 116 while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) &&
116 (avail != -1)) 117 (avail != -1))
117 if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16)) 118 if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16))
118 { 119 {
119 GNUNET_DISK_file_close (fh); 120 GNUNET_DISK_file_close (fh);
120 GNUNET_break (0 == UNLINK (".testfile")); 121 GNUNET_break (0 == UNLINK (".testfile"));
121 return 1; 122 return 1;
122 } 123 }
123 GNUNET_DISK_file_close (fh); 124 GNUNET_DISK_file_close (fh);
124 GNUNET_break (0 == UNLINK (".testfile")); 125 GNUNET_break (0 == UNLINK (".testfile"));
125 126
@@ -146,7 +147,7 @@ testDirScan ()
146 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more")) 147 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
147 return 1; 148 return 1;
148 GNUNET_DISK_directory_scan ("test", &scan_callback, 149 GNUNET_DISK_directory_scan ("test", &scan_callback,
149 "test" DIR_SEPARATOR_STR "entry"); 150 "test" DIR_SEPARATOR_STR "entry");
150 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) 151 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
151 return 1; 152 return 1;
152 if (ok < 2) 153 if (ok < 2)
@@ -156,7 +157,7 @@ testDirScan ()
156 157
157static void 158static void
158iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di, 159iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
159 const char *filename, const char *dirname) 160 const char *filename, const char *dirname)
160{ 161{
161 int *i = cls; 162 int *i = cls;
162 163
@@ -168,7 +169,7 @@ static void
168iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 169iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169{ 170{
170 GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 171 GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
171 "test", &iter_callback, cls); 172 "test", &iter_callback, cls);
172} 173}
173 174
174static int 175static int
@@ -202,14 +203,14 @@ testGetHome ()
202 cfg = GNUNET_CONFIGURATION_create (); 203 cfg = GNUNET_CONFIGURATION_create ();
203 GNUNET_assert (cfg != NULL); 204 GNUNET_assert (cfg != NULL);
204 GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME", 205 GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
205 "/tmp/test-gnunet-disk-a/b/c"); 206 "/tmp/test-gnunet-disk-a/b/c");
206 fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL); 207 fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
207 GNUNET_assert (fn != NULL); 208 GNUNET_assert (fn != NULL);
208 GNUNET_CONFIGURATION_destroy (cfg); 209 GNUNET_CONFIGURATION_destroy (cfg);
209 ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn); 210 ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
210 GNUNET_free (fn); 211 GNUNET_free (fn);
211 GNUNET_break (GNUNET_OK == 212 GNUNET_break (GNUNET_OK ==
212 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a")); 213 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a"));
213 return ret; 214 return ret;
214} 215}
215 216
@@ -220,10 +221,10 @@ testCanonicalize ()
220 221
221 GNUNET_DISK_filename_canonicalize (fn); 222 GNUNET_DISK_filename_canonicalize (fn);
222 if (0 != strcmp (fn, "ab____cd_ef__g_")) 223 if (0 != strcmp (fn, "ab____cd_ef__g_"))
223 { 224 {
224 GNUNET_free (fn); 225 GNUNET_free (fn);
225 return 1; 226 return 1;
226 } 227 }
227 GNUNET_free (fn); 228 GNUNET_free (fn);
228 return 0; 229 return 0;
229} 230}
@@ -275,9 +276,9 @@ main (int argc, char *argv[])
275 failureCount += testChangeOwner (); 276 failureCount += testChangeOwner ();
276 failureCount += testDirMani (); 277 failureCount += testDirMani ();
277 if (failureCount != 0) 278 if (failureCount != 0)
278 { 279 {
279 fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount); 280 fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount);
280 return -1; 281 return -1;
281 } 282 }
282 return 0; 283 return 0;
283} /* end of main */ 284} /* end of main */
diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c
index a517887bf..f830454cd 100644
--- a/src/util/test_getopt.c
+++ b/src/util/test_getopt.c
@@ -63,15 +63,15 @@ testVerbose ()
63 }; 63 };
64 64
65 if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv)) 65 if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
66 { 66 {
67 GNUNET_break (0); 67 GNUNET_break (0);
68 return 1; 68 return 1;
69 } 69 }
70 if (vflags != 2) 70 if (vflags != 2)
71 { 71 {
72 GNUNET_break (0); 72 GNUNET_break (0);
73 return 1; 73 return 1;
74 } 74 }
75 return 0; 75 return 0;
76} 76}
77 77
@@ -89,10 +89,10 @@ testVersion ()
89 }; 89 };
90 90
91 if (-1 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv)) 91 if (-1 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
92 { 92 {
93 GNUNET_break (0); 93 GNUNET_break (0);
94 return 1; 94 return 1;
95 } 95 }
96 return 0; 96 return 0;
97} 97}
98 98
@@ -110,10 +110,10 @@ testAbout ()
110 }; 110 };
111 111
112 if (-1 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv)) 112 if (-1 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
113 { 113 {
114 GNUNET_break (0); 114 GNUNET_break (0);
115 return 1; 115 return 1;
116 } 116 }
117 return 0; 117 return 0;
118} 118}
119 119
@@ -136,18 +136,18 @@ testLogOpts ()
136 }; 136 };
137 137
138 if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv)) 138 if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
139 { 139 {
140 GNUNET_break (0); 140 GNUNET_break (0);
141 return 1; 141 return 1;
142 } 142 }
143 GNUNET_assert (fn != NULL); 143 GNUNET_assert (fn != NULL);
144 if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename"))) 144 if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename")))
145 { 145 {
146 GNUNET_break (0); 146 GNUNET_break (0);
147 GNUNET_free (level); 147 GNUNET_free (level);
148 GNUNET_free (fn); 148 GNUNET_free (fn);
149 return 1; 149 return 1;
150 } 150 }
151 GNUNET_free (level); 151 GNUNET_free (level);
152 GNUNET_free (fn); 152 GNUNET_free (fn);
153 return 0; 153 return 0;
@@ -178,15 +178,15 @@ testFlagNum ()
178 }; 178 };
179 179
180 if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv)) 180 if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
181 { 181 {
182 GNUNET_break (0); 182 GNUNET_break (0);
183 return 1; 183 return 1;
184 } 184 }
185 if ((1 != flag) || (42 != num) || (42 != lnum)) 185 if ((1 != flag) || (42 != num) || (42 != lnum))
186 { 186 {
187 GNUNET_break (0); 187 GNUNET_break (0);
188 return 1; 188 return 1;
189 } 189 }
190 return 0; 190 return 0;
191} 191}
192 192
diff --git a/src/util/test_os_network.c b/src/util/test_os_network.c
index 315f97d97..28cc9cd3a 100644
--- a/src/util/test_os_network.c
+++ b/src/util/test_os_network.c
@@ -44,10 +44,10 @@ proc (void *cls, const char *name, int isDefault, const struct sockaddr *addr,
44 return GNUNET_OK; 44 return GNUNET_OK;
45 45
46 inet_ntop (addr->sa_family, 46 inet_ntop (addr->sa_family,
47 (addr->sa_family == 47 (addr->sa_family ==
48 AF_INET) ? (void *) &((struct sockaddr_in *) addr)->sin_addr 48 AF_INET) ? (void *) &((struct sockaddr_in *) addr)->sin_addr
49 : (void *) &((struct sockaddr_in6 *) addr)->sin6_addr, buf, 49 : (void *) &((struct sockaddr_in6 *) addr)->sin6_addr, buf,
50 sizeof (buf)); 50 sizeof (buf));
51 if ((0 == strcmp ("::1", buf)) || (0 == strcmp ("127.0.0.1", buf))) 51 if ((0 == strcmp ("::1", buf)) || (0 == strcmp ("127.0.0.1", buf)))
52 *ok = 0; 52 *ok = 0;
53 return GNUNET_OK; 53 return GNUNET_OK;
diff --git a/src/util/test_os_priority.c b/src/util/test_os_priority.c
index 94e2719a2..59f3ee816 100644
--- a/src/util/test_os_priority.c
+++ b/src/util/test_os_priority.c
@@ -32,27 +32,27 @@ testprio ()
32{ 32{
33 if (GNUNET_OK != 33 if (GNUNET_OK !=
34 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), 34 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
35 GNUNET_SCHEDULER_PRIORITY_DEFAULT)) 35 GNUNET_SCHEDULER_PRIORITY_DEFAULT))
36 return 1; 36 return 1;
37 if (GNUNET_OK != 37 if (GNUNET_OK !=
38 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), 38 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
39 GNUNET_SCHEDULER_PRIORITY_UI)) 39 GNUNET_SCHEDULER_PRIORITY_UI))
40 return 1; 40 return 1;
41 if (GNUNET_OK != 41 if (GNUNET_OK !=
42 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), 42 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
43 GNUNET_SCHEDULER_PRIORITY_IDLE)) 43 GNUNET_SCHEDULER_PRIORITY_IDLE))
44 return 1; 44 return 1;
45 if (GNUNET_OK != 45 if (GNUNET_OK !=
46 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), 46 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
47 GNUNET_SCHEDULER_PRIORITY_BACKGROUND)) 47 GNUNET_SCHEDULER_PRIORITY_BACKGROUND))
48 return 1; 48 return 1;
49 if (GNUNET_OK != 49 if (GNUNET_OK !=
50 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), 50 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
51 GNUNET_SCHEDULER_PRIORITY_HIGH)) 51 GNUNET_SCHEDULER_PRIORITY_HIGH))
52 return 1; 52 return 1;
53 if (GNUNET_OK != 53 if (GNUNET_OK !=
54 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), 54 GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
55 GNUNET_SCHEDULER_PRIORITY_HIGH)) 55 GNUNET_SCHEDULER_PRIORITY_HIGH))
56 return 1; 56 return 1;
57 return 0; 57 return 0;
58} 58}
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c
index df8520546..918ef1bc5 100644
--- a/src/util/test_os_start_process.c
+++ b/src/util/test_os_start_process.c
@@ -53,9 +53,9 @@ end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
53{ 53{
54 54
55 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 55 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
56 { 56 {
57 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 57 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
58 } 58 }
59 GNUNET_OS_process_wait (proc); 59 GNUNET_OS_process_wait (proc);
60 GNUNET_OS_process_close (proc); 60 GNUNET_OS_process_close (proc);
61 proc = NULL; 61 proc = NULL;
@@ -79,28 +79,28 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79#endif 79#endif
80 80
81 if (bytes < 1) 81 if (bytes < 1)
82 { 82 {
83 GNUNET_break (0); 83 GNUNET_break (0);
84 ok = 1; 84 ok = 1;
85 GNUNET_SCHEDULER_cancel (die_task); 85 GNUNET_SCHEDULER_cancel (die_task);
86 GNUNET_SCHEDULER_add_now (&end_task, NULL); 86 GNUNET_SCHEDULER_add_now (&end_task, NULL);
87 return; 87 return;
88 } 88 }
89 89
90 ok = strncmp (&buf[0], test_phrase, strlen (test_phrase)); 90 ok = strncmp (&buf[0], test_phrase, strlen (test_phrase));
91#if VERBOSE 91#if VERBOSE
92 fprintf (stderr, "read %s\n", &buf[0]); 92 fprintf (stderr, "read %s\n", &buf[0]);
93#endif 93#endif
94 if (ok == 0) 94 if (ok == 0)
95 { 95 {
96 GNUNET_SCHEDULER_cancel (die_task); 96 GNUNET_SCHEDULER_cancel (die_task);
97 GNUNET_SCHEDULER_add_now (&end_task, NULL); 97 GNUNET_SCHEDULER_add_now (&end_task, NULL);
98 return; 98 return;
99 } 99 }
100 100
101 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 101 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
102 stdout_read_handle, &read_call, 102 stdout_read_handle, &read_call,
103 stdout_read_handle); 103 stdout_read_handle);
104 104
105} 105}
106 106
@@ -118,16 +118,16 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); 118 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
119 119
120 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 120 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
121 { 121 {
122 GNUNET_break (0); 122 GNUNET_break (0);
123 ok = 1; 123 ok = 1;
124 GNUNET_free (fn); 124 GNUNET_free (fn);
125 return; 125 return;
126 } 126 }
127 127
128 proc = 128 proc =
129 GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn, 129 GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
130 "test_gnunet_echo_hello", "-", NULL); 130 "test_gnunet_echo_hello", "-", NULL);
131 GNUNET_free (fn); 131 GNUNET_free (fn);
132 132
133 /* Close the write end of the read pipe */ 133 /* Close the write end of the read pipe */
@@ -140,26 +140,26 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 /* Write the test_phrase to the cat process */ 140 /* Write the test_phrase to the cat process */
141 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) != 141 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
142 strlen (test_phrase) + 1) 142 strlen (test_phrase) + 1)
143 { 143 {
144 GNUNET_break (0); 144 GNUNET_break (0);
145 ok = 1; 145 ok = 1;
146 return; 146 return;
147 } 147 }
148 148
149 /* Close the write end to end the cycle! */ 149 /* Close the write end to end the cycle! */
150 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); 150 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
151 151
152 stdout_read_handle = 152 stdout_read_handle =
153 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ); 153 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
154 154
155 die_task = 155 die_task =
156 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 156 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
157 (GNUNET_TIME_UNIT_MINUTES, 1), &end_task, 157 (GNUNET_TIME_UNIT_MINUTES, 1), &end_task,
158 NULL); 158 NULL);
159 159
160 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 160 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
161 stdout_read_handle, &read_call, 161 stdout_read_handle, &read_call,
162 (void *) stdout_read_handle); 162 (void *) stdout_read_handle);
163 163
164} 164}
165 165
@@ -183,11 +183,11 @@ main (int argc, char *argv[])
183 183
184 GNUNET_log_setup ("test-os-start-process", 184 GNUNET_log_setup ("test-os-start-process",
185#if VERBOSE 185#if VERBOSE
186 "DEBUG", 186 "DEBUG",
187#else 187#else
188 "WARNING", 188 "WARNING",
189#endif 189#endif
190 NULL); 190 NULL);
191 ret = check (); 191 ret = check ();
192 192
193 return ret; 193 return ret;
diff --git a/src/util/test_peer.c b/src/util/test_peer.c
index 3817d8243..5bf5108bc 100644
--- a/src/util/test_peer.c
+++ b/src/util/test_peer.c
@@ -43,13 +43,13 @@ generatePeerIdList ()
43 int i; 43 int i;
44 44
45 for (i = 0; i < NUMBER_OF_PEERS; i++) 45 for (i = 0; i < NUMBER_OF_PEERS; i++)
46 { 46 {
47 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, 47 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
48 &pidArr[i].hashPubKey); 48 &pidArr[i].hashPubKey);
49#if DEBUG 49#if DEBUG
50 printf ("Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i])); 50 printf ("Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i]));
51#endif 51#endif
52 } 52 }
53} 53}
54 54
55 55
@@ -65,25 +65,27 @@ check ()
65 GNUNET_assert (0 == GNUNET_PEER_intern (NULL)); 65 GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
66 /* Insert Peers into PeerEntry table and hashmap */ 66 /* Insert Peers into PeerEntry table and hashmap */
67 for (i = 0; i < NUMBER_OF_PEERS; i++) 67 for (i = 0; i < NUMBER_OF_PEERS; i++)
68 {
69 pid = GNUNET_PEER_intern (&pidArr[i]);
70 if (pid != (i + 1))
71 { 68 {
72 fprintf (stderr, "Unexpected Peer ID returned by intern function\n"); 69 pid = GNUNET_PEER_intern (&pidArr[i]);
73 return 1; 70 if (pid != (i + 1))
71 {
72 fprintf (stderr,
73 "Unexpected Peer ID returned by intern function\n");
74 return 1;
75 }
74 } 76 }
75 }
76 77
77 /* Referencing the first 3 peers once again */ 78 /* Referencing the first 3 peers once again */
78 for (i = 0; i < 3; i++) 79 for (i = 0; i < 3; i++)
79 {
80 pid = GNUNET_PEER_intern (&pidArr[i]);
81 if (pid != (i + 1))
82 { 80 {
83 fprintf (stderr, "Unexpected Peer ID returned by intern function\n"); 81 pid = GNUNET_PEER_intern (&pidArr[i]);
84 return 1; 82 if (pid != (i + 1))
83 {
84 fprintf (stderr,
85 "Unexpected Peer ID returned by intern function\n");
86 return 1;
87 }
85 } 88 }
86 }
87 89
88 /* Dereferencing the first 3 peers once [decrementing their reference count] */ 90 /* Dereferencing the first 3 peers once [decrementing their reference count] */
89 GNUNET_PEER_decrement_rcs (ids, 3); 91 GNUNET_PEER_decrement_rcs (ids, 3);
@@ -128,11 +130,11 @@ main ()
128 130
129 GNUNET_log_setup ("test-peer", "ERROR", NULL); 131 GNUNET_log_setup ("test-peer", "ERROR", NULL);
130 for (i = 0; i < 1; i++) 132 for (i = 0; i < 1; i++)
131 { 133 {
132 generatePeerIdList (); 134 generatePeerIdList ();
133 if (0 != check ()) 135 if (0 != check ())
134 return 1; 136 return 1;
135 } 137 }
136 return 0; 138 return 0;
137} 139}
138 140
diff --git a/src/util/test_program.c b/src/util/test_program.c
index faeb4e716..0ff49b81e 100644
--- a/src/util/test_program.c
+++ b/src/util/test_program.c
@@ -58,7 +58,7 @@ static struct GNUNET_GETOPT_CommandLineOption options4[] = {
58 58
59static void 59static void
60runner (void *cls, char *const *args, const char *cfgfile, 60runner (void *cls, char *const *args, const char *cfgfile,
61 const struct GNUNET_CONFIGURATION_Handle *cfg) 61 const struct GNUNET_CONFIGURATION_Handle *cfg)
62{ 62{
63 int *ok = cls; 63 int *ok = cls;
64 64
@@ -91,18 +91,18 @@ check ()
91 }; 91 };
92 92
93 GNUNET_assert (GNUNET_OK == 93 GNUNET_assert (GNUNET_OK ==
94 GNUNET_PROGRAM_run (7, argv, "test_program", "A test", 94 GNUNET_PROGRAM_run (7, argv, "test_program", "A test",
95 options1, &runner, &ok)); 95 options1, &runner, &ok));
96 96
97 GNUNET_assert (GNUNET_OK == 97 GNUNET_assert (GNUNET_OK ==
98 GNUNET_PROGRAM_run (7, argv, "test_program", "A test", 98 GNUNET_PROGRAM_run (7, argv, "test_program", "A test",
99 options2, &runner, &ok)); 99 options2, &runner, &ok));
100 GNUNET_assert (GNUNET_OK == 100 GNUNET_assert (GNUNET_OK ==
101 GNUNET_PROGRAM_run (7, argv, "test_program", "A test", 101 GNUNET_PROGRAM_run (7, argv, "test_program", "A test",
102 options3, &runner, &ok)); 102 options3, &runner, &ok));
103 GNUNET_assert (GNUNET_OK == 103 GNUNET_assert (GNUNET_OK ==
104 GNUNET_PROGRAM_run (7, argv, "test_program", "A test", 104 GNUNET_PROGRAM_run (7, argv, "test_program", "A test",
105 options4, &runner, &ok)); 105 options4, &runner, &ok));
106 106
107 return ok; 107 return ok;
108} 108}
diff --git a/src/util/test_pseudonym.c b/src/util/test_pseudonym.c
index 20a3d3d96..30a04598c 100644
--- a/src/util/test_pseudonym.c
+++ b/src/util/test_pseudonym.c
@@ -45,16 +45,16 @@ iter (void *cls, const GNUNET_HashCode * pseudonym,
45 45
46 if ((0 == memcmp (pseudonym, &id1, sizeof (GNUNET_HashCode))) && 46 if ((0 == memcmp (pseudonym, &id1, sizeof (GNUNET_HashCode))) &&
47 (!GNUNET_CONTAINER_meta_data_test_equal (md, meta))) 47 (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
48 { 48 {
49 *ok = GNUNET_NO; 49 *ok = GNUNET_NO;
50 GNUNET_break (0); 50 GNUNET_break (0);
51 } 51 }
52 return GNUNET_OK; 52 return GNUNET_OK;
53} 53}
54 54
55static int 55static int
56noti_callback (void *cls, const GNUNET_HashCode * pseudonym, 56noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
57 const struct GNUNET_CONTAINER_MetaData *md, int rating) 57 const struct GNUNET_CONTAINER_MetaData *md, int rating)
58{ 58{
59 int *ret = cls; 59 int *ret = cls;
60 60
@@ -64,7 +64,7 @@ noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
64 64
65static int 65static int
66fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym, 66fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
67 const struct GNUNET_CONTAINER_MetaData *md, int rating) 67 const struct GNUNET_CONTAINER_MetaData *md, int rating)
68{ 68{
69 int *ret = cls; 69 int *ret = cls;
70 70
@@ -74,7 +74,7 @@ fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
74 74
75static int 75static int
76false_callback (void *cls, const GNUNET_HashCode * pseudonym, 76false_callback (void *cls, const GNUNET_HashCode * pseudonym,
77 const struct GNUNET_CONTAINER_MetaData *md, int rating) 77 const struct GNUNET_CONTAINER_MetaData *md, int rating)
78{ 78{
79 return GNUNET_OK; 79 return GNUNET_OK;
80} 80}
@@ -109,28 +109,28 @@ main (int argc, char *argv[])
109 (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"); 109 (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
110 cfg = GNUNET_CONFIGURATION_create (); 110 cfg = GNUNET_CONFIGURATION_create ();
111 if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf")) 111 if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
112 { 112 {
113 GNUNET_CONFIGURATION_destroy (cfg); 113 GNUNET_CONFIGURATION_destroy (cfg);
114 GNUNET_break (0); 114 GNUNET_break (0);
115 return -1; 115 return -1;
116 } 116 }
117 notiCount = 0; 117 notiCount = 0;
118 fakenotiCount = 0; 118 fakenotiCount = 0;
119 count = 0; 119 count = 0;
120 GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback, 120 GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
121 &fakenotiCount); 121 &fakenotiCount);
122 GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback, 122 GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
123 &notiCount); 123 &notiCount);
124 GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count); 124 GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count);
125 GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback, 125 GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback,
126 &fakenotiCount); 126 &fakenotiCount);
127 127
128 /* ACTUAL TEST CODE */ 128 /* ACTUAL TEST CODE */
129 old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL); 129 old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
130 meta = GNUNET_CONTAINER_meta_data_create (); 130 meta = GNUNET_CONTAINER_meta_data_create ();
131 GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE, 131 GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
132 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 132 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
133 "test", strlen ("test") + 1); 133 "test", strlen ("test") + 1);
134 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1); 134 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1);
135 GNUNET_PSEUDONYM_add (cfg, &id1, meta); 135 GNUNET_PSEUDONYM_add (cfg, &id1, meta);
136 CHECK (notiCount == 1); 136 CHECK (notiCount == 1);
@@ -145,11 +145,11 @@ main (int argc, char *argv[])
145 newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok); 145 newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
146 CHECK (old < newVal); 146 CHECK (old < newVal);
147 GNUNET_assert (GNUNET_OK == 147 GNUNET_assert (GNUNET_OK ==
148 GNUNET_CONTAINER_meta_data_insert (meta, "<test>", 148 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
149 EXTRACTOR_METATYPE_COMMENT, 149 EXTRACTOR_METATYPE_COMMENT,
150 EXTRACTOR_METAFORMAT_UTF8, 150 EXTRACTOR_METAFORMAT_UTF8,
151 "text/plain", m, 151 "text/plain", m,
152 strlen (m) + 1)); 152 strlen (m) + 1));
153 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3); 153 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3);
154 GNUNET_PSEUDONYM_add (cfg, &id3, meta); 154 GNUNET_PSEUDONYM_add (cfg, &id3, meta);
155 name3 = GNUNET_PSEUDONYM_id_to_name (cfg, &id3); 155 name3 = GNUNET_PSEUDONYM_id_to_name (cfg, &id3);
@@ -184,7 +184,7 @@ FAILURE:
184 GNUNET_CONTAINER_meta_data_destroy (meta); 184 GNUNET_CONTAINER_meta_data_destroy (meta);
185 GNUNET_CONFIGURATION_destroy (cfg); 185 GNUNET_CONFIGURATION_destroy (cfg);
186 GNUNET_break (GNUNET_OK == 186 GNUNET_break (GNUNET_OK ==
187 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test")); 187 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
188 return (ok == GNUNET_YES) ? 0 : 1; 188 return (ok == GNUNET_YES) ? 0 : 1;
189} 189}
190 190
diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c
index 4e248aa72..24c71b41f 100644
--- a/src/util/test_resolver_api.c
+++ b/src/util/test_resolver_api.c
@@ -46,12 +46,13 @@ check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
46 int *ok = cls; 46 int *ok = cls;
47 47
48 if (salen == 0) 48 if (salen == 0)
49 { 49 {
50 (*ok) &= ~8; 50 (*ok) &= ~8;
51 return; 51 return;
52 } 52 }
53 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"), 53 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
54 GNUNET_a2s (sa, salen)); 54 _("Got IP address `%s' for our host.\n"), GNUNET_a2s (sa,
55 salen));
55} 56}
56 57
57 58
@@ -63,21 +64,21 @@ check_localhost_num (void *cls, const char *hostname)
63 if (hostname == NULL) 64 if (hostname == NULL)
64 return; 65 return;
65 if (0 == strcmp (hostname, "127.0.0.1")) 66 if (0 == strcmp (hostname, "127.0.0.1"))
66 { 67 {
67#if DEBUG_RESOLVER 68#if DEBUG_RESOLVER
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n", 69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 hostname); 70 "Received correct hostname `%s'.\n", hostname);
70#endif 71#endif
71 (*ok) &= ~4; 72 (*ok) &= ~4;
72 } 73 }
73 else 74 else
74 { 75 {
75#if DEBUG_RESOLVER 76#if DEBUG_RESOLVER
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received invalid hostname `%s'.\n", 77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77 hostname); 78 "Received invalid hostname `%s'.\n", hostname);
78#endif 79#endif
79 GNUNET_break (0); 80 GNUNET_break (0);
80 } 81 }
81} 82}
82 83
83 84
@@ -89,19 +90,19 @@ check_localhost (void *cls, const char *hostname)
89 if (hostname == NULL) 90 if (hostname == NULL)
90 return; 91 return;
91 if (0 == strcmp (hostname, "localhost")) 92 if (0 == strcmp (hostname, "localhost"))
92 { 93 {
93#if DEBUG_RESOLVER 94#if DEBUG_RESOLVER
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n", 95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 hostname); 96 "Received correct hostname `%s'.\n", hostname);
96#endif 97#endif
97 (*ok) &= ~2; 98 (*ok) &= ~2;
98 } 99 }
99 else 100 else
100 { 101 {
101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 102 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
102 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n", 103 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
103 hostname); 104 hostname);
104 } 105 }
105} 106}
106 107
107static void 108static void
@@ -114,19 +115,19 @@ check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
114 return; 115 return;
115 GNUNET_assert (sizeof (struct sockaddr_in) == salen); 116 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
116 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK)) 117 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
117 { 118 {
118#if DEBUG_RESOLVER 119#if DEBUG_RESOLVER
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n"); 120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
120#endif 121#endif
121 (*ok) &= ~1; 122 (*ok) &= ~1;
122 } 123 }
123 else 124 else
124 { 125 {
125#if DEBUG_RESOLVER 126#if DEBUG_RESOLVER
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n"); 127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
127#endif 128#endif
128 GNUNET_break (0); 129 GNUNET_break (0);
129 } 130 }
130} 131}
131 132
132static void 133static void
@@ -138,32 +139,32 @@ check_local_fqdn (void *cls, const char *gnunet_fqdn)
138 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 139 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
139 140
140 if (0 != gethostname (hostname, sizeof (hostname) - 1)) 141 if (0 != gethostname (hostname, sizeof (hostname) - 1))
141 { 142 {
142 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 143 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
143 "gethostname"); 144 "gethostname");
144 return; 145 return;
145 } 146 }
146#if DEBUG_RESOLVER 147#if DEBUG_RESOLVER
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), 148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"),
148 hostname); 149 hostname);
149#endif 150#endif
150 host = gethostbyname (hostname); 151 host = gethostbyname (hostname);
151 if (NULL == host) 152 if (NULL == host)
152 { 153 {
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 _("Could not resolve our FQDN : %s %u\n"), hstrerror (h_errno), 155 _("Could not resolve our FQDN : %s %u\n"),
155 h_errno); 156 hstrerror (h_errno), h_errno);
156 return; 157 return;
157 } 158 }
158 159
159 GNUNET_assert (0 != host); 160 GNUNET_assert (0 != host);
160 161
161 result = strcmp (host->h_name, gnunet_fqdn); 162 result = strcmp (host->h_name, gnunet_fqdn);
162 if (0 != result) 163 if (0 != result)
163 { 164 {
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 "Local resolved and resolver resolved fqdns are not equal\n"); 166 "Local resolved and resolver resolved fqdns are not equal\n");
166 } 167 }
167 GNUNET_assert (0 == result); 168 GNUNET_assert (0 == result);
168} 169}
169 170
@@ -180,21 +181,21 @@ check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
180 GNUNET_assert (sizeof (struct sockaddr_in) == salen); 181 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
181 182
182 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP)) 183 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
183 { 184 {
184#if DEBUG_RESOLVER 185#if DEBUG_RESOLVER
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Received correct rootserver ip address.\n"); 187 "Received correct rootserver ip address.\n");
187#endif 188#endif
188 (*ok) &= ~1; 189 (*ok) &= ~1;
189 } 190 }
190 else 191 else
191 { 192 {
192#if DEBUG_RESOLVER 193#if DEBUG_RESOLVER
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Received incorrect rootserver ip address.\n"); 195 "Received incorrect rootserver ip address.\n");
195#endif 196#endif
196 GNUNET_break (0); 197 GNUNET_break (0);
197 } 198 }
198} 199}
199 200
200static void 201static void
@@ -206,21 +207,21 @@ check_rootserver_name (void *cls, const char *hostname)
206 return; 207 return;
207 208
208 if (0 == strcmp (hostname, ROOTSERVER_NAME)) 209 if (0 == strcmp (hostname, ROOTSERVER_NAME))
209 { 210 {
210#if DEBUG_RESOLVER 211#if DEBUG_RESOLVER
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Received correct rootserver hostname `%s'.\n", hostname); 213 "Received correct rootserver hostname `%s'.\n", hostname);
213#endif 214#endif
214 (*ok) &= ~2; 215 (*ok) &= ~2;
215 } 216 }
216 else 217 else
217 { 218 {
218#if DEBUG_RESOLVER 219#if DEBUG_RESOLVER
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Received invalid rootserver hostname `%s'.\n", hostname); 221 "Received invalid rootserver hostname `%s'.\n", hostname);
221#endif 222#endif
222 GNUNET_break (0); 223 GNUNET_break (0);
223 } 224 }
224} 225}
225 226
226static void 227static void
@@ -230,7 +231,7 @@ run (void *cls, char *const *args, const char *cfgfile,
230 int *ok = cls; 231 int *ok = cls;
231 struct sockaddr_in sa; 232 struct sockaddr_in sa;
232 struct GNUNET_TIME_Relative timeout = 233 struct GNUNET_TIME_Relative timeout =
233 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30); 234 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
234 int count_ips = 0; 235 int count_ips = 0;
235 char *own_fqdn; 236 char *own_fqdn;
236 237
@@ -257,50 +258,50 @@ run (void *cls, char *const *args, const char *cfgfile,
257 258
258 rootserver = gethostbyname (rootserver_name); 259 rootserver = gethostbyname (rootserver_name);
259 if (rootserver == NULL) 260 if (rootserver == NULL)
260 { 261 {
261 /* Error: resolving ip addresses does not work */ 262 /* Error: resolving ip addresses does not work */
262#if DEBUG_RESOLVER 263#if DEBUG_RESOLVER
263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
264 _("gethostbyname() could not lookup IP address: %s\n"), 265 _("gethostbyname() could not lookup IP address: %s\n"),
265 hstrerror (h_errno)); 266 hstrerror (h_errno));
266#endif 267#endif
267 fprintf (stderr, 268 fprintf (stderr,
268 "System seems to be off-line, will not run all DNS tests\n"); 269 "System seems to be off-line, will not run all DNS tests\n");
269 *ok = 0; /* mark test as passing anyway */ 270 *ok = 0; /* mark test as passing anyway */
270 return; 271 return;
271 } 272 }
272 273
273 /* Counting returned IP addresses */ 274 /* Counting returned IP addresses */
274 while (rootserver->h_addr_list[count_ips] != NULL) 275 while (rootserver->h_addr_list[count_ips] != NULL)
275 count_ips++; 276 count_ips++;
276 if (count_ips > 1) 277 if (count_ips > 1)
277 { 278 {
278#if DEBUG_RESOLVER 279#if DEBUG_RESOLVER
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "IP received range for root name server, but a root name server has only 1 IP\n"); 281 "IP received range for root name server, but a root name server has only 1 IP\n");
281#endif 282#endif
282 GNUNET_break (0); 283 GNUNET_break (0);
283 } 284 }
284 285
285 /* Comparing to resolved address to the address the root name server should have */ 286 /* Comparing to resolved address to the address the root name server should have */
286 if (strcmp 287 if (strcmp
287 (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]), 288 (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
288 ROOTSERVER_IP) != 0) 289 ROOTSERVER_IP) != 0)
289 { 290 {
290#if DEBUG_RESOLVER 291#if DEBUG_RESOLVER
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "IP received and IP for root name server differ\n"); 293 "IP received and IP for root name server differ\n");
293#endif 294#endif
294 GNUNET_break (0); 295 GNUNET_break (0);
295 } 296 }
296#if DEBUG_RESOLVER 297#if DEBUG_RESOLVER
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 298 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298 "System's own forward name resolution is working\n"); 299 "System's own forward name resolution is working\n");
299#endif 300#endif
300 301
301 /* Resolve the same using GNUNET */ 302 /* Resolve the same using GNUNET */
302 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout, 303 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
303 &check_rootserver_ip, cls); 304 &check_rootserver_ip, cls);
304 305
305 /* 306 /*
306 * Success: forward lookups work as expected 307 * Success: forward lookups work as expected
@@ -311,41 +312,41 @@ run (void *cls, char *const *args, const char *cfgfile,
311 312
312 rootserver->h_name = ""; 313 rootserver->h_name = "";
313 if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr)) 314 if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr))
314 { 315 {
315#if DEBUG_RESOLVER 316#if DEBUG_RESOLVER
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Could not transform root name server IP address\n"); 318 "Could not transform root name server IP address\n");
318#endif 319#endif
319 GNUNET_break (0); 320 GNUNET_break (0);
320 } 321 }
321 322
322 rootserver = 323 rootserver =
323 gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET); 324 gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET);
324 if (rootserver == NULL) 325 if (rootserver == NULL)
325 { 326 {
326 /* Error: resolving IP addresses does not work */ 327 /* Error: resolving IP addresses does not work */
327#if DEBUG_RESOLVER 328#if DEBUG_RESOLVER
328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
329 _("gethostbyaddr() could not lookup hostname: %s\n"), 330 _("gethostbyaddr() could not lookup hostname: %s\n"),
330 hstrerror (h_errno)); 331 hstrerror (h_errno));
331#endif 332#endif
332 GNUNET_break (0); 333 GNUNET_break (0);
333 } 334 }
334 else 335 else
335 {
336 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
337 { 336 {
337 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
338 {
338#if DEBUG_RESOLVER 339#if DEBUG_RESOLVER
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Received hostname and hostname for root name server differ\n"); 341 "Received hostname and hostname for root name server differ\n");
341#endif 342#endif
342 GNUNET_break (0); 343 GNUNET_break (0);
344 }
343 } 345 }
344 }
345 346
346#if DEBUG_RESOLVER 347#if DEBUG_RESOLVER
347 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 348 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348 "System's own reverse name resolution is working\n"); 349 "System's own reverse name resolution is working\n");
349#endif 350#endif
350 351
351 /* Resolve the same using GNUNET */ 352 /* Resolve the same using GNUNET */
@@ -360,8 +361,8 @@ run (void *cls, char *const *args, const char *cfgfile,
360 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP); 361 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
361#endif 362#endif
362 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, 363 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
363 sizeof (struct sockaddr), GNUNET_YES, timeout, 364 sizeof (struct sockaddr), GNUNET_YES, timeout,
364 &check_rootserver_name, cls); 365 &check_rootserver_name, cls);
365 366
366 memset (&sa, 0, sizeof (sa)); 367 memset (&sa, 0, sizeof (sa));
367 sa.sin_family = AF_INET; 368 sa.sin_family = AF_INET;
@@ -372,12 +373,12 @@ run (void *cls, char *const *args, const char *cfgfile,
372 373
373 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls); 374 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
374 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, 375 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
375 sizeof (struct sockaddr), GNUNET_YES, timeout, 376 sizeof (struct sockaddr), GNUNET_YES, timeout,
376 &check_localhost, cls); 377 &check_localhost, cls);
377 378
378 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, 379 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
379 sizeof (struct sockaddr), GNUNET_NO, timeout, 380 sizeof (struct sockaddr), GNUNET_NO, timeout,
380 &check_localhost_num, cls); 381 &check_localhost_num, cls);
381 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls); 382 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
382 383
383} 384}
@@ -391,33 +392,33 @@ check ()
391 struct GNUNET_OS_Process *proc; 392 struct GNUNET_OS_Process *proc;
392 393
393 char *const argv[] = 394 char *const argv[] =
394 { "test-resolver-api", "-c", "test_resolver_api_data.conf", 395 { "test-resolver-api", "-c", "test_resolver_api_data.conf",
395#if VERBOSE 396#if VERBOSE
396 "-L", "DEBUG", 397 "-L", "DEBUG",
397#endif 398#endif
398 NULL 399 NULL
399 }; 400 };
400 struct GNUNET_GETOPT_CommandLineOption options[] = 401 struct GNUNET_GETOPT_CommandLineOption options[] =
401 { GNUNET_GETOPT_OPTION_END }; 402 { GNUNET_GETOPT_OPTION_END };
402 pfx = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR); 403 pfx = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
403 GNUNET_asprintf (&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR); 404 GNUNET_asprintf (&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
404 GNUNET_free (pfx); 405 GNUNET_free (pfx);
405 proc = GNUNET_OS_start_process (NULL, NULL, fn, "gnunet-service-resolver", 406 proc = GNUNET_OS_start_process (NULL, NULL, fn, "gnunet-service-resolver",
406#if VERBOSE 407#if VERBOSE
407 "-L", "DEBUG", 408 "-L", "DEBUG",
408#endif 409#endif
409 "-c", "test_resolver_api_data.conf", NULL); 410 "-c", "test_resolver_api_data.conf", NULL);
410 GNUNET_assert (NULL != proc); 411 GNUNET_assert (NULL != proc);
411 GNUNET_free (fn); 412 GNUNET_free (fn);
412 GNUNET_assert (GNUNET_OK == 413 GNUNET_assert (GNUNET_OK ==
413 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 414 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
414 argv, "test-resolver-api", "nohelp", 415 argv, "test-resolver-api", "nohelp",
415 options, &run, &ok)); 416 options, &run, &ok));
416 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 417 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
417 { 418 {
418 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 419 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
419 ok = 1; 420 ok = 1;
420 } 421 }
421 GNUNET_OS_process_wait (proc); 422 GNUNET_OS_process_wait (proc);
422 GNUNET_OS_process_close (proc); 423 GNUNET_OS_process_close (proc);
423 proc = NULL; 424 proc = NULL;
@@ -433,11 +434,11 @@ main (int argc, char *argv[])
433 434
434 GNUNET_log_setup ("test-resolver-api", 435 GNUNET_log_setup ("test-resolver-api",
435#if VERBOSE 436#if VERBOSE
436 "DEBUG", 437 "DEBUG",
437#else 438#else
438 "WARNING", 439 "WARNING",
439#endif 440#endif
440 NULL); 441 NULL);
441 ret = check (); 442 ret = check ();
442 443
443 return ret; 444 return ret;
diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c
index 788ba13fa..e2a28c1b7 100644
--- a/src/util/test_scheduler.c
+++ b/src/util/test_scheduler.c
@@ -36,7 +36,7 @@ task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
36 36
37 /* t4 should be ready (albeit with lower priority) */ 37 /* t4 should be ready (albeit with lower priority) */
38 GNUNET_assert (1 == 38 GNUNET_assert (1 ==
39 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT)); 39 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
40 GNUNET_assert (3 == *ok); 40 GNUNET_assert (3 == *ok);
41 (*ok) = 4; 41 (*ok) = 4;
42} 42}
@@ -51,7 +51,7 @@ task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 (*ok) = 3; 51 (*ok) = 3;
52 /* t3 will go before t4: higher priority */ 52 /* t3 will go before t4: higher priority */
53 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task3, 53 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task3,
54 cls); 54 cls);
55} 55}
56 56
57static void 57static void
@@ -106,8 +106,8 @@ taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0])); 106 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
107 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1)); 107 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
108 (*ok) = 8; 108 (*ok) = 8;
109 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast, 109 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
110 cls); 110 &taskLast, cls);
111 GNUNET_SCHEDULER_shutdown (); 111 GNUNET_SCHEDULER_shutdown ();
112} 112}
113 113
@@ -123,10 +123,10 @@ task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
123 GNUNET_assert (NULL != p); 123 GNUNET_assert (NULL != p);
124 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ); 124 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
125 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE); 125 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
126 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], &taskRd, 126 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0],
127 cls); 127 &taskRd, cls);
128 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1], 128 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1],
129 &taskWrt, cls); 129 &taskWrt, cls);
130} 130}
131 131
132 132
@@ -226,7 +226,7 @@ taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 GNUNET_assert (1 == *ok); 226 GNUNET_assert (1 == *ok);
227 *ok = 0; 227 *ok = 0;
228 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_after 228 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_after
229 (GNUNET_SCHEDULER_NO_TASK, &taskNeverRun, NULL)); 229 (GNUNET_SCHEDULER_NO_TASK, &taskNeverRun, NULL));
230} 230}
231 231
232 232
diff --git a/src/util/test_scheduler_delay.c b/src/util/test_scheduler_delay.c
index ff6966a87..20783da8d 100644
--- a/src/util/test_scheduler_delay.c
+++ b/src/util/test_scheduler_delay.c
@@ -57,17 +57,17 @@ test_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 else 57 else
58 cumDelta += (target.abs_value - now.abs_value); 58 cumDelta += (target.abs_value - now.abs_value);
59 target = 59 target =
60 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply 60 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
61 (GNUNET_TIME_UNIT_MILLISECONDS, i)); 61 (GNUNET_TIME_UNIT_MILLISECONDS, i));
62 fprintf (stderr, "."); 62 fprintf (stderr, ".");
63 if (i > MAXV) 63 if (i > MAXV)
64 { 64 {
65 fprintf (stderr, "\n"); 65 fprintf (stderr, "\n");
66 return; 66 return;
67 } 67 }
68 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 68 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
69 (GNUNET_TIME_UNIT_MILLISECONDS, i), &test_task, 69 (GNUNET_TIME_UNIT_MILLISECONDS, i),
70 NULL); 70 &test_task, NULL);
71 i += INCR; 71 i += INCR;
72} 72}
73 73
@@ -77,10 +77,10 @@ check ()
77 target = GNUNET_TIME_absolute_get (); 77 target = GNUNET_TIME_absolute_get ();
78 GNUNET_SCHEDULER_run (&test_task, NULL); 78 GNUNET_SCHEDULER_run (&test_task, NULL);
79 FPRINTF (stdout, "Sleep precision: %llu ms. ", 79 FPRINTF (stdout, "Sleep precision: %llu ms. ",
80 cumDelta / 1000 / (MAXV / INCR)); 80 cumDelta / 1000 / (MAXV / INCR));
81 if (cumDelta <= 10 * MAXV / INCR) 81 if (cumDelta <= 10 * MAXV / INCR)
82 fprintf (stdout, "Timer precision is excellent.\n"); 82 fprintf (stdout, "Timer precision is excellent.\n");
83 else if (cumDelta <= 50 * MAXV / INCR) /* 50 ms average deviation */ 83 else if (cumDelta <= 50 * MAXV / INCR) /* 50 ms average deviation */
84 fprintf (stdout, "Timer precision is good.\n"); 84 fprintf (stdout, "Timer precision is good.\n");
85 else if (cumDelta > 250 * MAXV / INCR) 85 else if (cumDelta > 250 * MAXV / INCR)
86 fprintf (stdout, "Timer precision is awful.\n"); 86 fprintf (stdout, "Timer precision is awful.\n");
diff --git a/src/util/test_server.c b/src/util/test_server.c
index 6c79e6064..360c9c242 100644
--- a/src/util/test_server.c
+++ b/src/util/test_server.c
@@ -61,7 +61,7 @@ finish_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
61 61
62static void 62static void
63recv_fin_cb (void *cls, struct GNUNET_SERVER_Client *client, 63recv_fin_cb (void *cls, struct GNUNET_SERVER_Client *client,
64 const struct GNUNET_MessageHeader *message) 64 const struct GNUNET_MessageHeader *message)
65{ 65{
66 GNUNET_assert (ok == 5); 66 GNUNET_assert (ok == 5);
67 ok = 6; 67 ok = 6;
@@ -98,19 +98,20 @@ reply_msg (void *cls, size_t size, void *buf)
98 98
99static void 99static void
100recv_cb (void *cls, struct GNUNET_SERVER_Client *client, 100recv_cb (void *cls, struct GNUNET_SERVER_Client *client,
101 const struct GNUNET_MessageHeader *message) 101 const struct GNUNET_MessageHeader *message)
102{ 102{
103 GNUNET_assert (ok == 2); 103 GNUNET_assert (ok == 2);
104 ok = 3; 104 ok = 3;
105 argclient = client; 105 argclient = client;
106 GNUNET_SERVER_client_keep (argclient); 106 GNUNET_SERVER_client_keep (argclient);
107 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)); 107 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
108 ntohs (message->size));
108 GNUNET_assert (MY_TYPE == ntohs (message->type)); 109 GNUNET_assert (MY_TYPE == ntohs (message->type));
109 GNUNET_assert (NULL != 110 GNUNET_assert (NULL !=
110 GNUNET_SERVER_notify_transmit_ready (client, 111 GNUNET_SERVER_notify_transmit_ready (client,
111 ntohs (message->size), 112 ntohs (message->size),
112 TIMEOUT, &reply_msg, 113 TIMEOUT, &reply_msg,
113 NULL)); 114 NULL));
114} 115}
115 116
116 117
@@ -146,12 +147,12 @@ transmit_initial_message (void *cls, size_t size, void *buf)
146 msg.size = htons (sizeof (struct GNUNET_MessageHeader)); 147 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
147 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader)); 148 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
148 GNUNET_assert (NULL != 149 GNUNET_assert (NULL !=
149 GNUNET_CLIENT_notify_transmit_ready (cc, 150 GNUNET_CLIENT_notify_transmit_ready (cc,
150 sizeof (struct 151 sizeof (struct
151 GNUNET_MessageHeader), 152 GNUNET_MessageHeader),
152 TIMEOUT, GNUNET_YES, 153 TIMEOUT, GNUNET_YES,
153 &transmit_second_message, 154 &transmit_second_message,
154 NULL)); 155 NULL));
155 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT); 156 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
156 return sizeof (struct GNUNET_MessageHeader); 157 return sizeof (struct GNUNET_MessageHeader);
157} 158}
@@ -180,18 +181,18 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 cfg = GNUNET_CONFIGURATION_create (); 181 cfg = GNUNET_CONFIGURATION_create ();
181 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT); 182 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
182 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", 183 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
183 "localhost"); 184 "localhost");
184 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 185 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
185 "localhost"); 186 "localhost");
186 cc = GNUNET_CLIENT_connect ("test-server", cfg); 187 cc = GNUNET_CLIENT_connect ("test-server", cfg);
187 GNUNET_assert (cc != NULL); 188 GNUNET_assert (cc != NULL);
188 GNUNET_assert (NULL != 189 GNUNET_assert (NULL !=
189 GNUNET_CLIENT_notify_transmit_ready (cc, 190 GNUNET_CLIENT_notify_transmit_ready (cc,
190 sizeof (struct 191 sizeof (struct
191 GNUNET_MessageHeader), 192 GNUNET_MessageHeader),
192 TIMEOUT, GNUNET_YES, 193 TIMEOUT, GNUNET_YES,
193 &transmit_initial_message, 194 &transmit_initial_message,
194 NULL)); 195 NULL));
195} 196}
196 197
197 198
diff --git a/src/util/test_server_disconnect.c b/src/util/test_server_disconnect.c
index 69207f81a..e38840d92 100644
--- a/src/util/test_server_disconnect.c
+++ b/src/util/test_server_disconnect.c
@@ -81,13 +81,14 @@ server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 81
82static void 82static void
83recv_cb (void *cls, struct GNUNET_SERVER_Client *client, 83recv_cb (void *cls, struct GNUNET_SERVER_Client *client,
84 const struct GNUNET_MessageHeader *message) 84 const struct GNUNET_MessageHeader *message)
85{ 85{
86 GNUNET_assert (ok == 2); 86 GNUNET_assert (ok == 2);
87 ok = 3; 87 ok = 3;
88 GNUNET_SERVER_client_keep (client); 88 GNUNET_SERVER_client_keep (client);
89 GNUNET_SCHEDULER_add_now (&server_disconnect, client); 89 GNUNET_SCHEDULER_add_now (&server_disconnect, client);
90 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)); 90 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
91 ntohs (message->size));
91 GNUNET_assert (MY_TYPE == ntohs (message->type)); 92 GNUNET_assert (MY_TYPE == ntohs (message->type));
92 GNUNET_SERVER_receive_done (client, GNUNET_OK); 93 GNUNET_SERVER_receive_done (client, GNUNET_OK);
93} 94}
@@ -138,18 +139,18 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 cfg = GNUNET_CONFIGURATION_create (); 139 cfg = GNUNET_CONFIGURATION_create ();
139 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT); 140 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
140 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", 141 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
141 "localhost"); 142 "localhost");
142 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 143 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
143 "localhost"); 144 "localhost");
144 cc = GNUNET_CLIENT_connect ("test-server", cfg); 145 cc = GNUNET_CLIENT_connect ("test-server", cfg);
145 GNUNET_assert (cc != NULL); 146 GNUNET_assert (cc != NULL);
146 GNUNET_assert (NULL != 147 GNUNET_assert (NULL !=
147 GNUNET_CLIENT_notify_transmit_ready (cc, 148 GNUNET_CLIENT_notify_transmit_ready (cc,
148 sizeof (struct 149 sizeof (struct
149 GNUNET_MessageHeader), 150 GNUNET_MessageHeader),
150 TIMEOUT, GNUNET_YES, 151 TIMEOUT, GNUNET_YES,
151 &transmit_initial_message, 152 &transmit_initial_message,
152 NULL)); 153 NULL));
153} 154}
154 155
155 156
diff --git a/src/util/test_server_with_client.c b/src/util/test_server_with_client.c
index 5b6e51718..21c706b07 100644
--- a/src/util/test_server_with_client.c
+++ b/src/util/test_server_with_client.c
@@ -58,7 +58,7 @@ send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
58 58
59static void 59static void
60recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient, 60recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
61 const struct GNUNET_MessageHeader *message) 61 const struct GNUNET_MessageHeader *message)
62{ 62{
63 void *addr; 63 void *addr;
64 size_t addrlen; 64 size_t addrlen;
@@ -66,7 +66,8 @@ recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
66 struct sockaddr_in *have; 66 struct sockaddr_in *have;
67 67
68 GNUNET_assert (GNUNET_OK == 68 GNUNET_assert (GNUNET_OK ==
69 GNUNET_SERVER_client_get_address (argclient, &addr, &addrlen)); 69 GNUNET_SERVER_client_get_address (argclient, &addr,
70 &addrlen));
70 71
71 GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); 72 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
72 have = addr; 73 have = addr;
@@ -80,21 +81,21 @@ recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
80 GNUNET_assert (0 == memcmp (&sa, addr, addrlen)); 81 GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
81 GNUNET_free (addr); 82 GNUNET_free (addr);
82 switch (ok) 83 switch (ok)
83 { 84 {
84 case 2: 85 case 2:
85 ok++; 86 ok++;
86 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 87 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
87 (GNUNET_TIME_UNIT_MILLISECONDS, 50), 88 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
88 &send_done, argclient); 89 &send_done, argclient);
89 break; 90 break;
90 case 4: 91 case 4:
91 ok++; 92 ok++;
92 GNUNET_CLIENT_disconnect (client, GNUNET_YES); 93 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
93 GNUNET_SERVER_receive_done (argclient, GNUNET_OK); 94 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
94 break; 95 break;
95 default: 96 default:
96 GNUNET_assert (0); 97 GNUNET_assert (0);
97 } 98 }
98 99
99} 100}
100 101
@@ -169,24 +170,25 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 sa.sin_family = AF_INET; 170 sa.sin_family = AF_INET;
170 sa.sin_port = htons (PORT); 171 sa.sin_port = htons (PORT);
171 server = 172 server =
172 GNUNET_SERVER_create (NULL, NULL, sap, slens, 173 GNUNET_SERVER_create (NULL, NULL, sap, slens,
173 GNUNET_TIME_relative_multiply 174 GNUNET_TIME_relative_multiply
174 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO); 175 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
175 GNUNET_assert (server != NULL); 176 GNUNET_assert (server != NULL);
176 handlers[0].callback_cls = cls; 177 handlers[0].callback_cls = cls;
177 GNUNET_SERVER_add_handlers (server, handlers); 178 GNUNET_SERVER_add_handlers (server, handlers);
178 GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls); 179 GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
179 cfg = GNUNET_CONFIGURATION_create (); 180 cfg = GNUNET_CONFIGURATION_create ();
180 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT); 181 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
181 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost"); 182 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME",
183 "localhost");
182 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 184 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
183 "localhost"); 185 "localhost");
184 client = GNUNET_CLIENT_connect ("test", cfg); 186 client = GNUNET_CLIENT_connect ("test", cfg);
185 GNUNET_assert (client != NULL); 187 GNUNET_assert (client != NULL);
186 GNUNET_CLIENT_notify_transmit_ready (client, 256, 188 GNUNET_CLIENT_notify_transmit_ready (client, 256,
187 GNUNET_TIME_relative_multiply 189 GNUNET_TIME_relative_multiply
188 (GNUNET_TIME_UNIT_MILLISECONDS, 250), 190 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
189 GNUNET_NO, &notify_ready, NULL); 191 GNUNET_NO, &notify_ready, NULL);
190} 192}
191 193
192 194
@@ -211,11 +213,11 @@ main (int argc, char *argv[])
211 213
212 GNUNET_log_setup ("test_server_with_client", 214 GNUNET_log_setup ("test_server_with_client",
213#if VERBOSE 215#if VERBOSE
214 "DEBUG", 216 "DEBUG",
215#else 217#else
216 "WARNING", 218 "WARNING",
217#endif 219#endif
218 NULL); 220 NULL);
219 ret += check (); 221 ret += check ();
220 222
221 return ret; 223 return ret;
diff --git a/src/util/test_server_with_client_unix.c b/src/util/test_server_with_client_unix.c
index 4f80f188c..11f731243 100644
--- a/src/util/test_server_with_client_unix.c
+++ b/src/util/test_server_with_client_unix.c
@@ -56,24 +56,24 @@ send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
56 56
57static void 57static void
58recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient, 58recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
59 const struct GNUNET_MessageHeader *message) 59 const struct GNUNET_MessageHeader *message)
60{ 60{
61 switch (ok) 61 switch (ok)
62 { 62 {
63 case 2: 63 case 2:
64 ok++; 64 ok++;
65 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 65 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
66 (GNUNET_TIME_UNIT_MILLISECONDS, 50), 66 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
67 &send_done, argclient); 67 &send_done, argclient);
68 break; 68 break;
69 case 4: 69 case 4:
70 ok++; 70 ok++;
71 GNUNET_CLIENT_disconnect (client, GNUNET_YES); 71 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
72 GNUNET_SERVER_receive_done (argclient, GNUNET_OK); 72 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
73 break; 73 break;
74 default: 74 default:
75 GNUNET_assert (0); 75 GNUNET_assert (0);
76 } 76 }
77 77
78} 78}
79 79
@@ -156,9 +156,9 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 sap[1] = NULL; 156 sap[1] = NULL;
157 slens[1] = 0; 157 slens[1] = 0;
158 server = 158 server =
159 GNUNET_SERVER_create (NULL, NULL, sap, slens, 159 GNUNET_SERVER_create (NULL, NULL, sap, slens,
160 GNUNET_TIME_relative_multiply 160 GNUNET_TIME_relative_multiply
161 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO); 161 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
162 GNUNET_assert (server != NULL); 162 GNUNET_assert (server != NULL);
163 handlers[0].callback_cls = cls; 163 handlers[0].callback_cls = cls;
164 GNUNET_SERVER_add_handlers (server, handlers); 164 GNUNET_SERVER_add_handlers (server, handlers);
@@ -167,14 +167,14 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 167
168 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath); 168 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath);
169 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 169 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
170 "localhost"); 170 "localhost");
171 171
172 client = GNUNET_CLIENT_connect ("test", cfg); 172 client = GNUNET_CLIENT_connect ("test", cfg);
173 GNUNET_assert (client != NULL); 173 GNUNET_assert (client != NULL);
174 GNUNET_CLIENT_notify_transmit_ready (client, 256, 174 GNUNET_CLIENT_notify_transmit_ready (client, 256,
175 GNUNET_TIME_relative_multiply 175 GNUNET_TIME_relative_multiply
176 (GNUNET_TIME_UNIT_MILLISECONDS, 250), 176 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
177 GNUNET_NO, &notify_ready, NULL); 177 GNUNET_NO, &notify_ready, NULL);
178} 178}
179 179
180 180
@@ -199,11 +199,11 @@ main (int argc, char *argv[])
199 199
200 GNUNET_log_setup ("test_server_with_client_unix", 200 GNUNET_log_setup ("test_server_with_client_unix",
201#if VERBOSE 201#if VERBOSE
202 "DEBUG", 202 "DEBUG",
203#else 203#else
204 "WARNING", 204 "WARNING",
205#endif 205#endif
206 NULL); 206 NULL);
207 ret += check (); 207 ret += check ();
208 208
209 return ret; 209 return ret;
diff --git a/src/util/test_service.c b/src/util/test_service.c
index 1b78932f8..33d69db10 100644
--- a/src/util/test_service.c
+++ b/src/util/test_service.c
@@ -68,11 +68,11 @@ ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 client = GNUNET_CLIENT_connect ("test_service", cfg); 68 client = GNUNET_CLIENT_connect ("test_service", cfg);
69 GNUNET_assert (client != NULL); 69 GNUNET_assert (client != NULL);
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71 "Client connecting, waiting to transmit\n"); 71 "Client connecting, waiting to transmit\n");
72 GNUNET_CLIENT_notify_transmit_ready (client, 72 GNUNET_CLIENT_notify_transmit_ready (client,
73 sizeof (struct GNUNET_MessageHeader), 73 sizeof (struct GNUNET_MessageHeader),
74 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, 74 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
75 &build_msg, client); 75 &build_msg, client);
76} 76}
77 77
78 78
@@ -85,7 +85,7 @@ do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 85
86static void 86static void
87recv_cb (void *cls, struct GNUNET_SERVER_Client *client, 87recv_cb (void *cls, struct GNUNET_SERVER_Client *client,
88 const struct GNUNET_MessageHeader *message) 88 const struct GNUNET_MessageHeader *message)
89{ 89{
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n"); 90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
91 GNUNET_SERVER_receive_done (client, GNUNET_OK); 91 GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -105,12 +105,12 @@ static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
105 105
106static void 106static void
107runner (void *cls, struct GNUNET_SERVER_Handle *server, 107runner (void *cls, struct GNUNET_SERVER_Handle *server,
108 const struct GNUNET_CONFIGURATION_Handle *cfg) 108 const struct GNUNET_CONFIGURATION_Handle *cfg)
109{ 109{
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n"); 110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
111 GNUNET_SERVER_add_handlers (server, myhandlers); 111 GNUNET_SERVER_add_handlers (server, myhandlers);
112 GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS, 112 GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS,
113 &ready, (void *) cfg); 113 &ready, (void *) cfg);
114} 114}
115 115
116 116
@@ -136,8 +136,9 @@ check ()
136 }; 136 };
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n"); 137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
138 GNUNET_assert (GNUNET_OK == 138 GNUNET_assert (GNUNET_OK ==
139 GNUNET_SERVICE_run (5, argv, "test_service", 139 GNUNET_SERVICE_run (5, argv, "test_service",
140 GNUNET_SERVICE_OPTION_NONE, &runner, &ok)); 140 GNUNET_SERVICE_OPTION_NONE, &runner,
141 &ok));
141 GNUNET_assert (0 == ok); 142 GNUNET_assert (0 == ok);
142 return ok; 143 return ok;
143} 144}
@@ -154,19 +155,19 @@ ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154 GNUNET_assert (client != NULL); 155 GNUNET_assert (client != NULL);
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n"); 156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
156 GNUNET_CLIENT_notify_transmit_ready (client, 157 GNUNET_CLIENT_notify_transmit_ready (client,
157 sizeof (struct GNUNET_MessageHeader), 158 sizeof (struct GNUNET_MessageHeader),
158 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, 159 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
159 &build_msg, client); 160 &build_msg, client);
160} 161}
161 162
162static void 163static void
163runner6 (void *cls, struct GNUNET_SERVER_Handle *server, 164runner6 (void *cls, struct GNUNET_SERVER_Handle *server,
164 const struct GNUNET_CONFIGURATION_Handle *cfg) 165 const struct GNUNET_CONFIGURATION_Handle *cfg)
165{ 166{
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n"); 167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
167 GNUNET_SERVER_add_handlers (server, myhandlers); 168 GNUNET_SERVER_add_handlers (server, myhandlers);
168 GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS, 169 GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS,
169 &ready6, (void *) cfg); 170 &ready6, (void *) cfg);
170} 171}
171 172
172/** 173/**
@@ -190,9 +191,9 @@ check6 ()
190 }; 191 };
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n"); 192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
192 GNUNET_assert (GNUNET_OK == 193 GNUNET_assert (GNUNET_OK ==
193 GNUNET_SERVICE_run (5, argv, "test_service6", 194 GNUNET_SERVICE_run (5, argv, "test_service6",
194 GNUNET_SERVICE_OPTION_NONE, &runner6, 195 GNUNET_SERVICE_OPTION_NONE, &runner6,
195 &ok)); 196 &ok));
196 GNUNET_assert (0 == ok); 197 GNUNET_assert (0 == ok);
197 return ok; 198 return ok;
198} 199}
@@ -201,11 +202,12 @@ check6 ()
201 202
202static void 203static void
203start_stop_main (void *cls, char *const *args, const char *cfgfile, 204start_stop_main (void *cls, char *const *args, const char *cfgfile,
204 const struct GNUNET_CONFIGURATION_Handle *cfg) 205 const struct GNUNET_CONFIGURATION_Handle *cfg)
205{ 206{
206 int *ret = cls; 207 int *ret = cls;
207 208
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service using start method\n"); 209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Starting service using start method\n");
209 sctx = GNUNET_SERVICE_start ("test_service", cfg); 211 sctx = GNUNET_SERVICE_start ("test_service", cfg);
210 GNUNET_assert (NULL != sctx); 212 GNUNET_assert (NULL != sctx);
211 runner (cls, GNUNET_SERVICE_get_server (sctx), cfg); 213 runner (cls, GNUNET_SERVICE_get_server (sctx), cfg);
@@ -234,8 +236,9 @@ check_start_stop ()
234 int ret = 1; 236 int ret = 1;
235 237
236 GNUNET_assert (GNUNET_OK == 238 GNUNET_assert (GNUNET_OK ==
237 GNUNET_PROGRAM_run (5, argv, "test-service-program", "no help", 239 GNUNET_PROGRAM_run (5, argv, "test-service-program",
238 options, &start_stop_main, &ret)); 240 "no help", options, &start_stop_main,
241 &ret));
239 242
240 GNUNET_break (0 == ret); 243 GNUNET_break (0 == ret);
241 return ret; 244 return ret;
@@ -250,11 +253,11 @@ main (int argc, char *argv[])
250 253
251 GNUNET_log_setup ("test-service", 254 GNUNET_log_setup ("test-service",
252#if VERBOSE 255#if VERBOSE
253 "DEBUG", 256 "DEBUG",
254#else 257#else
255 "WARNING", 258 "WARNING",
256#endif 259#endif
257 NULL); 260 NULL);
258 ret += check (); 261 ret += check ();
259 ret += check (); 262 ret += check ();
260 263
@@ -263,22 +266,22 @@ main (int argc, char *argv[])
263 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); 266 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
264#endif 267#endif
265 if (NULL == s) 268 if (NULL == s)
266 {
267 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
268 (errno == EACCES))
269 { 269 {
270 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); 270 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
271 return 1; 271 (errno == EACCES))
272 {
273 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
274 return 1;
275 }
276 fprintf (stderr,
277 "IPv6 support seems to not be available (%s), not testing it!\n",
278 strerror (errno));
272 } 279 }
273 fprintf (stderr,
274 "IPv6 support seems to not be available (%s), not testing it!\n",
275 strerror (errno));
276 }
277 else 280 else
278 { 281 {
279 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); 282 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
280 ret += check6 (); 283 ret += check6 ();
281 } 284 }
282 ret += check_start_stop (); 285 ret += check_start_stop ();
283 286
284 return ret; 287 return ret;
diff --git a/src/util/test_strings.c b/src/util/test_strings.c
index c6d604144..bb0e6088f 100644
--- a/src/util/test_strings.c
+++ b/src/util/test_strings.c
@@ -50,18 +50,18 @@ check ()
50 WANT (buf, b); 50 WANT (buf, b);
51 sprintf (buf, "4 %s", _( /* time unit */ "ms")); 51 sprintf (buf, "4 %s", _( /* time unit */ "ms"));
52 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply 52 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
53 (GNUNET_TIME_UNIT_MILLISECONDS, 53 (GNUNET_TIME_UNIT_MILLISECONDS,
54 4)); 54 4));
55 WANT (buf, b); 55 WANT (buf, b);
56 sprintf (buf, "7 %s", _( /* time unit */ "s")); 56 sprintf (buf, "7 %s", _( /* time unit */ "s"));
57 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply 57 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
58 (GNUNET_TIME_UNIT_MILLISECONDS, 58 (GNUNET_TIME_UNIT_MILLISECONDS,
59 7 * 1000)); 59 7 * 1000));
60 WANT (buf, b); 60 WANT (buf, b);
61 sprintf (buf, "7 %s", _( /* time unit */ "h")); 61 sprintf (buf, "7 %s", _( /* time unit */ "h"));
62 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply 62 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
63 (GNUNET_TIME_UNIT_MILLISECONDS, 63 (GNUNET_TIME_UNIT_MILLISECONDS,
64 7 * 60 * 60 * 1000)); 64 7 * 60 * 60 * 1000));
65 WANT (buf, b); 65 WANT (buf, b);
66#ifndef MINGW 66#ifndef MINGW
67 hdir = getenv ("HOME"); 67 hdir = getenv ("HOME");
@@ -88,12 +88,12 @@ check ()
88 * where the details of the day and hour depend on the timezone; 88 * where the details of the day and hour depend on the timezone;
89 * however, the "0:05 19" should always be there; hence: */ 89 * however, the "0:05 19" should always be there; hence: */
90 if (NULL == strstr (r, "0:05 19")) 90 if (NULL == strstr (r, "0:05 19"))
91 { 91 {
92 fprintf (stderr, "Got %s\n", r); 92 fprintf (stderr, "Got %s\n", r);
93 GNUNET_break (0); 93 GNUNET_break (0);
94 GNUNET_free (r); 94 GNUNET_free (r);
95 return 1; 95 return 1;
96 } 96 }
97 GNUNET_free (r); 97 GNUNET_free (r);
98 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII"); 98 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
99 WANT ("TEST", b); 99 WANT ("TEST", b);
diff --git a/src/util/test_time.c b/src/util/test_time.c
index c5232ff2a..80109615a 100644
--- a/src/util/test_time.c
+++ b/src/util/test_time.c
@@ -63,7 +63,7 @@ check ()
63 /*check zero */ 63 /*check zero */
64 rel.rel_value = (UINT64_MAX) - 1024; 64 rel.rel_value = (UINT64_MAX) - 1024;
65 GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value == 65 GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value ==
66 GNUNET_TIME_relative_multiply (rel, 0).rel_value); 66 GNUNET_TIME_relative_multiply (rel, 0).rel_value);
67 67
68 /* test infinity-check for relative to absolute */ 68 /* test infinity-check for relative to absolute */
69 GNUNET_log_skip (1, GNUNET_NO); 69 GNUNET_log_skip (1, GNUNET_NO);
@@ -74,11 +74,11 @@ check ()
74 /*check relative to absolute */ 74 /*check relative to absolute */
75 rel.rel_value = 0; 75 rel.rel_value = 0;
76 GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value == 76 GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value ==
77 GNUNET_TIME_relative_to_absolute (rel).abs_value); 77 GNUNET_TIME_relative_to_absolute (rel).abs_value);
78 /*check forever */ 78 /*check forever */
79 rel.rel_value = UINT64_MAX; 79 rel.rel_value = UINT64_MAX;
80 GNUNET_assert (GNUNET_TIME_absolute_get_forever ().abs_value == 80 GNUNET_assert (GNUNET_TIME_absolute_get_forever ().abs_value ==
81 GNUNET_TIME_relative_to_absolute (rel).abs_value); 81 GNUNET_TIME_relative_to_absolute (rel).abs_value);
82 /* check overflow for r2a */ 82 /* check overflow for r2a */
83 rel.rel_value = (UINT64_MAX) - 1024; 83 rel.rel_value = (UINT64_MAX) - 1024;
84 GNUNET_log_skip (1, GNUNET_NO); 84 GNUNET_log_skip (1, GNUNET_NO);
@@ -103,13 +103,13 @@ check ()
103 103
104 /* check relation check in get_duration */ 104 /* check relation check in get_duration */
105 future.abs_value = now.abs_value + 1000000; 105 future.abs_value = now.abs_value + 1000000;
106 GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value == 106 GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value
107 1000000); 107 == 1000000);
108 GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value == 108 GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value
109 0); 109 == 0);
110 110
111 GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value 111 GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value
112 == forever.abs_value); 112 == forever.abs_value);
113 113
114 past.abs_value = now.abs_value - 1000000; 114 past.abs_value = now.abs_value - 1000000;
115 rel = GNUNET_TIME_absolute_get_duration (future); 115 rel = GNUNET_TIME_absolute_get_duration (future);
@@ -127,7 +127,7 @@ check ()
127 GNUNET_assert (rel.rel_value <= 1000000); 127 GNUNET_assert (rel.rel_value <= 1000000);
128 forever = GNUNET_TIME_absolute_get_forever (); 128 forever = GNUNET_TIME_absolute_get_forever ();
129 GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value == 129 GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value ==
130 GNUNET_TIME_absolute_get_remaining (forever).rel_value); 130 GNUNET_TIME_absolute_get_remaining (forever).rel_value);
131 131
132 /* check endianess */ 132 /* check endianess */
133 reln = GNUNET_TIME_relative_hton (rel); 133 reln = GNUNET_TIME_relative_hton (rel);
@@ -152,23 +152,24 @@ check ()
152 GNUNET_assert (future.abs_value == now.abs_value); 152 GNUNET_assert (future.abs_value == now.abs_value);
153 153
154 GNUNET_assert (forever.abs_value == 154 GNUNET_assert (forever.abs_value ==
155 GNUNET_TIME_absolute_subtract (forever, 155 GNUNET_TIME_absolute_subtract (forever,
156 GNUNET_TIME_UNIT_MINUTES).abs_value); 156 GNUNET_TIME_UNIT_MINUTES).abs_value);
157 /*check absolute subtract */ 157 /*check absolute subtract */
158 now.abs_value = 50000; 158 now.abs_value = 50000;
159 rel.rel_value = 100000; 159 rel.rel_value = 100000;
160 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value == 160 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value ==
161 (GNUNET_TIME_absolute_subtract (now, rel)).abs_value); 161 (GNUNET_TIME_absolute_subtract (now, rel)).abs_value);
162 rel.rel_value = 10000; 162 rel.rel_value = 10000;
163 GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value); 163 GNUNET_assert (40000 ==
164 (GNUNET_TIME_absolute_subtract (now, rel)).abs_value);
164 165
165 /*check relative divide */ 166 /*check relative divide */
166 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == 167 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
167 (GNUNET_TIME_relative_divide (rel, 0)).rel_value); 168 (GNUNET_TIME_relative_divide (rel, 0)).rel_value);
168 169
169 rel = GNUNET_TIME_UNIT_FOREVER_REL; 170 rel = GNUNET_TIME_UNIT_FOREVER_REL;
170 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == 171 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
171 (GNUNET_TIME_relative_divide (rel, 2)).rel_value); 172 (GNUNET_TIME_relative_divide (rel, 2)).rel_value);
172 173
173 rel = GNUNET_TIME_relative_divide (relUnit, 2); 174 rel = GNUNET_TIME_relative_divide (relUnit, 2);
174 GNUNET_assert (rel.rel_value == relUnit.rel_value / 2); 175 GNUNET_assert (rel.rel_value == relUnit.rel_value / 2);
@@ -182,50 +183,51 @@ check ()
182 forever = GNUNET_TIME_absolute_get_forever (); 183 forever = GNUNET_TIME_absolute_get_forever ();
183 forever.abs_value = forever.abs_value - 1024; 184 forever.abs_value = forever.abs_value - 1024;
184 GNUNET_assert (GNUNET_TIME_absolute_get_zero ().abs_value == 185 GNUNET_assert (GNUNET_TIME_absolute_get_zero ().abs_value ==
185 GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value); 186 GNUNET_TIME_calculate_eta (forever, 50000,
187 100000).rel_value);
186 /* check zero */ 188 /* check zero */
187 GNUNET_log_skip (1, GNUNET_NO); 189 GNUNET_log_skip (1, GNUNET_NO);
188 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value == 190 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value ==
189 (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value); 191 (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value);
190 GNUNET_log_skip (0, GNUNET_YES); 192 GNUNET_log_skip (0, GNUNET_YES);
191 /*check forever */ 193 /*check forever */
192 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == 194 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
193 (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value); 195 (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value);
194 196
195 /*check relative subtract */ 197 /*check relative subtract */
196 now = GNUNET_TIME_absolute_get (); 198 now = GNUNET_TIME_absolute_get ();
197 rel.rel_value = now.abs_value; 199 rel.rel_value = now.abs_value;
198 relForever.rel_value = rel.rel_value + 1024; 200 relForever.rel_value = rel.rel_value + 1024;
199 GNUNET_assert (1024 == 201 GNUNET_assert (1024 ==
200 GNUNET_TIME_relative_subtract (relForever, rel).rel_value); 202 GNUNET_TIME_relative_subtract (relForever, rel).rel_value);
201 /*check zero */ 203 /*check zero */
202 GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value == 204 GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value ==
203 GNUNET_TIME_relative_subtract (rel, relForever).rel_value); 205 GNUNET_TIME_relative_subtract (rel, relForever).rel_value);
204 /*check forever */ 206 /*check forever */
205 rel.rel_value = UINT64_MAX; 207 rel.rel_value = UINT64_MAX;
206 GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value == 208 GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value ==
207 GNUNET_TIME_relative_subtract (rel, relForever).rel_value); 209 GNUNET_TIME_relative_subtract (rel, relForever).rel_value);
208 210
209 /*check GNUNET_TIME_relative_min */ 211 /*check GNUNET_TIME_relative_min */
210 now = GNUNET_TIME_absolute_get (); 212 now = GNUNET_TIME_absolute_get ();
211 rel.rel_value = now.abs_value; 213 rel.rel_value = now.abs_value;
212 relForever.rel_value = rel.rel_value - 1024; 214 relForever.rel_value = rel.rel_value - 1024;
213 GNUNET_assert (relForever.rel_value == 215 GNUNET_assert (relForever.rel_value ==
214 GNUNET_TIME_relative_min (rel, relForever).rel_value); 216 GNUNET_TIME_relative_min (rel, relForever).rel_value);
215 217
216 /*check GNUNET_TIME_relative_max */ 218 /*check GNUNET_TIME_relative_max */
217 GNUNET_assert (rel.rel_value == 219 GNUNET_assert (rel.rel_value ==
218 GNUNET_TIME_relative_max (rel, relForever).rel_value); 220 GNUNET_TIME_relative_max (rel, relForever).rel_value);
219 221
220 /*check GNUNET_TIME_absolute_min */ 222 /*check GNUNET_TIME_absolute_min */
221 now = GNUNET_TIME_absolute_get (); 223 now = GNUNET_TIME_absolute_get ();
222 last.abs_value = now.abs_value - 1024; 224 last.abs_value = now.abs_value - 1024;
223 GNUNET_assert (last.abs_value == 225 GNUNET_assert (last.abs_value ==
224 GNUNET_TIME_absolute_min (now, last).abs_value); 226 GNUNET_TIME_absolute_min (now, last).abs_value);
225 227
226 /*check GNUNET_TIME_absolute_max */ 228 /*check GNUNET_TIME_absolute_max */
227 GNUNET_assert (now.abs_value == 229 GNUNET_assert (now.abs_value ==
228 GNUNET_TIME_absolute_max (now, last).abs_value); 230 GNUNET_TIME_absolute_max (now, last).abs_value);
229 231
230 return 0; 232 return 0;
231} 233}
diff --git a/src/util/time.c b/src/util/time.c
index d3dd3b5ec..e48377fa3 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -26,6 +26,8 @@
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_time_lib.h" 27#include "gnunet_time_lib.h"
28 28
29#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
30
29static long long timestamp_offset; 31static long long timestamp_offset;
30 32
31/** 33/**
@@ -53,8 +55,8 @@ GNUNET_TIME_absolute_get ()
53 55
54 GETTIMEOFDAY (&tv, NULL); 56 GETTIMEOFDAY (&tv, NULL);
55 ret.abs_value = 57 ret.abs_value =
56 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) + 58 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) +
57 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset; 59 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset;
58 return ret; 60 return ret;
59} 61}
60 62
@@ -128,10 +130,10 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
128 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 130 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
129 131
130 if (rel.rel_value + now.abs_value < rel.rel_value) 132 if (rel.rel_value + now.abs_value < rel.rel_value)
131 { 133 {
132 GNUNET_break (0); /* overflow... */ 134 GNUNET_break (0); /* overflow... */
133 return GNUNET_TIME_absolute_get_forever (); 135 return GNUNET_TIME_absolute_get_forever ();
134 } 136 }
135 ret.abs_value = rel.rel_value + now.abs_value; 137 ret.abs_value = rel.rel_value + now.abs_value;
136 return ret; 138 return ret;
137} 139}
@@ -146,7 +148,7 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
146 */ 148 */
147struct GNUNET_TIME_Relative 149struct GNUNET_TIME_Relative
148GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1, 150GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
149 struct GNUNET_TIME_Relative t2) 151 struct GNUNET_TIME_Relative t2)
150{ 152{
151 return (t1.rel_value < t2.rel_value) ? t1 : t2; 153 return (t1.rel_value < t2.rel_value) ? t1 : t2;
152} 154}
@@ -161,7 +163,7 @@ GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
161 */ 163 */
162struct GNUNET_TIME_Relative 164struct GNUNET_TIME_Relative
163GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1, 165GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
164 struct GNUNET_TIME_Relative t2) 166 struct GNUNET_TIME_Relative t2)
165{ 167{
166 return (t1.rel_value > t2.rel_value) ? t1 : t2; 168 return (t1.rel_value > t2.rel_value) ? t1 : t2;
167} 169}
@@ -177,7 +179,7 @@ GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
177 */ 179 */
178struct GNUNET_TIME_Absolute 180struct GNUNET_TIME_Absolute
179GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1, 181GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
180 struct GNUNET_TIME_Absolute t2) 182 struct GNUNET_TIME_Absolute t2)
181{ 183{
182 return (t1.abs_value < t2.abs_value) ? t1 : t2; 184 return (t1.abs_value < t2.abs_value) ? t1 : t2;
183} 185}
@@ -192,7 +194,7 @@ GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
192 */ 194 */
193struct GNUNET_TIME_Absolute 195struct GNUNET_TIME_Absolute
194GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1, 196GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
195 struct GNUNET_TIME_Absolute t2) 197 struct GNUNET_TIME_Absolute t2)
196{ 198{
197 return (t1.abs_value > t2.abs_value) ? t1 : t2; 199 return (t1.abs_value > t2.abs_value) ? t1 : t2;
198} 200}
@@ -228,7 +230,7 @@ GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
228 */ 230 */
229struct GNUNET_TIME_Relative 231struct GNUNET_TIME_Relative
230GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start, 232GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
231 struct GNUNET_TIME_Absolute end) 233 struct GNUNET_TIME_Absolute end)
232{ 234{
233 struct GNUNET_TIME_Relative ret; 235 struct GNUNET_TIME_Relative ret;
234 236
@@ -269,17 +271,17 @@ GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
269 */ 271 */
270struct GNUNET_TIME_Absolute 272struct GNUNET_TIME_Absolute
271GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start, 273GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
272 struct GNUNET_TIME_Relative duration) 274 struct GNUNET_TIME_Relative duration)
273{ 275{
274 struct GNUNET_TIME_Absolute ret; 276 struct GNUNET_TIME_Absolute ret;
275 277
276 if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX)) 278 if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX))
277 return GNUNET_TIME_absolute_get_forever (); 279 return GNUNET_TIME_absolute_get_forever ();
278 if (start.abs_value + duration.rel_value < start.abs_value) 280 if (start.abs_value + duration.rel_value < start.abs_value)
279 { 281 {
280 GNUNET_break (0); 282 GNUNET_break (0);
281 return GNUNET_TIME_absolute_get_forever (); 283 return GNUNET_TIME_absolute_get_forever ();
282 } 284 }
283 ret.abs_value = start.abs_value + duration.rel_value; 285 ret.abs_value = start.abs_value + duration.rel_value;
284 return ret; 286 return ret;
285} 287}
@@ -295,7 +297,7 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
295 */ 297 */
296struct GNUNET_TIME_Absolute 298struct GNUNET_TIME_Absolute
297GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start, 299GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
298 struct GNUNET_TIME_Relative duration) 300 struct GNUNET_TIME_Relative duration)
299{ 301{
300 struct GNUNET_TIME_Absolute ret; 302 struct GNUNET_TIME_Absolute ret;
301 303
@@ -315,7 +317,7 @@ GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
315 */ 317 */
316struct GNUNET_TIME_Relative 318struct GNUNET_TIME_Relative
317GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, 319GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
318 unsigned int factor) 320 unsigned int factor)
319{ 321{
320 struct GNUNET_TIME_Relative ret; 322 struct GNUNET_TIME_Relative ret;
321 323
@@ -323,10 +325,10 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
323 return GNUNET_TIME_relative_get_zero (); 325 return GNUNET_TIME_relative_get_zero ();
324 ret.rel_value = rel.rel_value * (unsigned long long) factor; 326 ret.rel_value = rel.rel_value * (unsigned long long) factor;
325 if (ret.rel_value / factor != rel.rel_value) 327 if (ret.rel_value / factor != rel.rel_value)
326 { 328 {
327 GNUNET_break (0); 329 GNUNET_break (0);
328 return GNUNET_TIME_relative_get_forever (); 330 return GNUNET_TIME_relative_get_forever ();
329 } 331 }
330 return ret; 332 return ret;
331} 333}
332 334
@@ -340,7 +342,7 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
340 */ 342 */
341struct GNUNET_TIME_Relative 343struct GNUNET_TIME_Relative
342GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel, 344GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
343 unsigned int factor) 345 unsigned int factor)
344{ 346{
345 struct GNUNET_TIME_Relative ret; 347 struct GNUNET_TIME_Relative ret;
346 348
@@ -363,8 +365,8 @@ GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
363 * assuming it continues at the same speed 365 * assuming it continues at the same speed
364 */ 366 */
365struct GNUNET_TIME_Relative 367struct GNUNET_TIME_Relative
366GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished, 368GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
367 uint64_t total) 369 uint64_t finished, uint64_t total)
368{ 370{
369 struct GNUNET_TIME_Relative dur; 371 struct GNUNET_TIME_Relative dur;
370 double exp; 372 double exp;
@@ -391,17 +393,17 @@ GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
391 */ 393 */
392struct GNUNET_TIME_Relative 394struct GNUNET_TIME_Relative
393GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1, 395GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
394 struct GNUNET_TIME_Relative a2) 396 struct GNUNET_TIME_Relative a2)
395{ 397{
396 struct GNUNET_TIME_Relative ret; 398 struct GNUNET_TIME_Relative ret;
397 399
398 if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX)) 400 if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX))
399 return GNUNET_TIME_relative_get_forever (); 401 return GNUNET_TIME_relative_get_forever ();
400 if (a1.rel_value + a2.rel_value < a1.rel_value) 402 if (a1.rel_value + a2.rel_value < a1.rel_value)
401 { 403 {
402 GNUNET_break (0); 404 GNUNET_break (0);
403 return GNUNET_TIME_relative_get_forever (); 405 return GNUNET_TIME_relative_get_forever ();
404 } 406 }
405 ret.rel_value = a1.rel_value + a2.rel_value; 407 ret.rel_value = a1.rel_value + a2.rel_value;
406 return ret; 408 return ret;
407} 409}
@@ -416,7 +418,7 @@ GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
416 */ 418 */
417struct GNUNET_TIME_Relative 419struct GNUNET_TIME_Relative
418GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1, 420GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
419 struct GNUNET_TIME_Relative a2) 421 struct GNUNET_TIME_Relative a2)
420{ 422{
421 struct GNUNET_TIME_Relative ret; 423 struct GNUNET_TIME_Relative ret;
422 424
diff --git a/src/util/winproc.c b/src/util/winproc.c
index 9594c5d6f..cf4240cd7 100644
--- a/src/util/winproc.c
+++ b/src/util/winproc.c
@@ -72,9 +72,9 @@ TSetNamedSecurityInfo GNSetNamedSecurityInfo;
72void 72void
73plibc_panic (int err, char *msg) 73plibc_panic (int err, char *msg)
74{ 74{
75 GNUNET_log (((err == 75 LOG (((err ==
76 INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR), 76 INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
77 "%s", msg); 77 "%s", msg);
78} 78}
79 79
80/** 80/**
@@ -99,137 +99,139 @@ GNInitWinEnv ()
99 99
100 /* Function to get CPU usage under Win NT */ 100 /* Function to get CPU usage under Win NT */
101 if (hNTDLL) 101 if (hNTDLL)
102 { 102 {
103 GNNtQuerySystemInformation = 103 GNNtQuerySystemInformation =
104 (TNtQuerySystemInformation) GetProcAddress (hNTDLL, 104 (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
105 "NtQuerySystemInformation"); 105 "NtQuerySystemInformation");
106 } 106 }
107 else 107 else
108 { 108 {
109 GNNtQuerySystemInformation = NULL; 109 GNNtQuerySystemInformation = NULL;
110 } 110 }
111 111
112 /* Functions to get information about a network adapter */ 112 /* Functions to get information about a network adapter */
113 hIphlpapi = LoadLibrary ("iphlpapi.dll"); 113 hIphlpapi = LoadLibrary ("iphlpapi.dll");
114 if (hIphlpapi) 114 if (hIphlpapi)
115 { 115 {
116 GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry"); 116 GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
117 GNGetIpAddrTable = 117 GNGetIpAddrTable =
118 (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable"); 118 (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
119 GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable"); 119 GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
120 GNGetBestInterface = 120 GNGetBestInterface =
121 (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface"); 121 (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface");
122 GGetAdaptersInfo = 122 GGetAdaptersInfo =
123 (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo"); 123 (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
124 } 124 }
125 else 125 else
126 { 126 {
127 GNGetIfEntry = NULL; 127 GNGetIfEntry = NULL;
128 GNGetIpAddrTable = NULL; 128 GNGetIpAddrTable = NULL;
129 GNGetIfTable = NULL; 129 GNGetIfTable = NULL;
130 GNGetBestInterface = NULL; 130 GNGetBestInterface = NULL;
131 GGetAdaptersInfo = NULL; 131 GGetAdaptersInfo = NULL;
132 } 132 }
133 133
134 /* Service & Account functions */ 134 /* Service & Account functions */
135 hAdvapi = LoadLibrary ("advapi32.dll"); 135 hAdvapi = LoadLibrary ("advapi32.dll");
136 if (hAdvapi) 136 if (hAdvapi)
137 { 137 {
138 GNOpenSCManager = 138 GNOpenSCManager =
139 (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA"); 139 (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
140 GNCreateService = 140 GNCreateService =
141 (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA"); 141 (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
142 GNCloseServiceHandle = 142 GNCloseServiceHandle =
143 (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle"); 143 (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
144 GNDeleteService = 144 GNDeleteService =
145 (TDeleteService) GetProcAddress (hAdvapi, "DeleteService"); 145 (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
146 GNRegisterServiceCtrlHandler = 146 GNRegisterServiceCtrlHandler =
147 (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi, 147 (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
148 "RegisterServiceCtrlHandlerA"); 148 "RegisterServiceCtrlHandlerA");
149 GNSetServiceStatus = 149 GNSetServiceStatus =
150 (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus"); 150 (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
151 GNStartServiceCtrlDispatcher = 151 GNStartServiceCtrlDispatcher =
152 (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi, 152 (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
153 "StartServiceCtrlDispatcherA"); 153 "StartServiceCtrlDispatcherA");
154 GNControlService = 154 GNControlService =
155 (TControlService) GetProcAddress (hAdvapi, "ControlService"); 155 (TControlService) GetProcAddress (hAdvapi, "ControlService");
156 GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA"); 156 GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
157 157
158 GNLsaOpenPolicy = 158 GNLsaOpenPolicy =
159 (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy"); 159 (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
160 GNLsaAddAccountRights = 160 GNLsaAddAccountRights =
161 (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights"); 161 (TLsaAddAccountRights) GetProcAddress (hAdvapi,
162 GNLsaRemoveAccountRights = 162 "LsaAddAccountRights");
163 (TLsaRemoveAccountRights) GetProcAddress (hAdvapi, 163 GNLsaRemoveAccountRights =
164 "LsaRemoveAccountRights"); 164 (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
165 GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose"); 165 "LsaRemoveAccountRights");
166 GNLookupAccountName = 166 GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
167 (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA"); 167 GNLookupAccountName =
168 (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
168 169
169 GNGetFileSecurity = 170 GNGetFileSecurity =
170 (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA"); 171 (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
171 GNInitializeSecurityDescriptor = 172 GNInitializeSecurityDescriptor =
172 (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi, 173 (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
173 "InitializeSecurityDescriptor"); 174 "InitializeSecurityDescriptor");
174 GNGetSecurityDescriptorDacl = 175 GNGetSecurityDescriptorDacl =
175 (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi, 176 (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
176 "GetSecurityDescriptorDacl"); 177 "GetSecurityDescriptorDacl");
177 GNGetAclInformation = 178 GNGetAclInformation =
178 (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation"); 179 (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
179 GNInitializeAcl = 180 GNInitializeAcl =
180 (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl"); 181 (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
181 GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce"); 182 GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
182 GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid"); 183 GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
183 GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce"); 184 GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
184 GNAddAccessAllowedAce = 185 GNAddAccessAllowedAce =
185 (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce"); 186 (TAddAccessAllowedAce) GetProcAddress (hAdvapi,
186 GNSetNamedSecurityInfo = 187 "AddAccessAllowedAce");
187 (TSetNamedSecurityInfo) GetProcAddress (hAdvapi, 188 GNSetNamedSecurityInfo =
188 "SetNamedSecurityInfoA"); 189 (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
189 } 190 "SetNamedSecurityInfoA");
191 }
190 else 192 else
191 { 193 {
192 GNOpenSCManager = NULL; 194 GNOpenSCManager = NULL;
193 GNCreateService = NULL; 195 GNCreateService = NULL;
194 GNCloseServiceHandle = NULL; 196 GNCloseServiceHandle = NULL;
195 GNDeleteService = NULL; 197 GNDeleteService = NULL;
196 GNRegisterServiceCtrlHandler = NULL; 198 GNRegisterServiceCtrlHandler = NULL;
197 GNSetServiceStatus = NULL; 199 GNSetServiceStatus = NULL;
198 GNStartServiceCtrlDispatcher = NULL; 200 GNStartServiceCtrlDispatcher = NULL;
199 GNControlService = NULL; 201 GNControlService = NULL;
200 GNOpenService = NULL; 202 GNOpenService = NULL;
201 203
202 GNLsaOpenPolicy = NULL; 204 GNLsaOpenPolicy = NULL;
203 GNLsaAddAccountRights = NULL; 205 GNLsaAddAccountRights = NULL;
204 GNLsaRemoveAccountRights = NULL; 206 GNLsaRemoveAccountRights = NULL;
205 GNLsaClose = NULL; 207 GNLsaClose = NULL;
206 GNLookupAccountName = NULL; 208 GNLookupAccountName = NULL;
207 209
208 GNGetFileSecurity = NULL; 210 GNGetFileSecurity = NULL;
209 GNInitializeSecurityDescriptor = NULL; 211 GNInitializeSecurityDescriptor = NULL;
210 GNGetSecurityDescriptorDacl = NULL; 212 GNGetSecurityDescriptorDacl = NULL;
211 GNGetAclInformation = NULL; 213 GNGetAclInformation = NULL;
212 GNInitializeAcl = NULL; 214 GNInitializeAcl = NULL;
213 GNGetAce = NULL; 215 GNGetAce = NULL;
214 GNEqualSid = NULL; 216 GNEqualSid = NULL;
215 GNAddAce = NULL; 217 GNAddAce = NULL;
216 GNAddAccessAllowedAce = NULL; 218 GNAddAccessAllowedAce = NULL;
217 GNSetNamedSecurityInfo = NULL; 219 GNSetNamedSecurityInfo = NULL;
218 } 220 }
219 221
220 /* Account function */ 222 /* Account function */
221 hNetapi = LoadLibrary ("netapi32.dll"); 223 hNetapi = LoadLibrary ("netapi32.dll");
222 if (hNetapi) 224 if (hNetapi)
223 { 225 {
224 GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd"); 226 GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
225 GNNetUserSetInfo = 227 GNNetUserSetInfo =
226 (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo"); 228 (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
227 } 229 }
228 else 230 else
229 { 231 {
230 GNNetUserAdd = NULL; 232 GNNetUserAdd = NULL;
231 GNNetUserSetInfo = NULL; 233 GNNetUserSetInfo = NULL;
232 } 234 }
233 235
234 return ret; 236 return ret;
235} 237}