aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/bandwidth.c141
-rw-r--r--src/util/bio.c253
-rw-r--r--src/util/client.c804
-rw-r--r--src/util/common_allocation.c56
-rw-r--r--src/util/common_logging.c400
-rw-r--r--src/util/configuration.c768
-rw-r--r--src/util/connection.c1015
-rw-r--r--src/util/container_bloomfilter.c260
-rw-r--r--src/util/container_heap.c201
-rw-r--r--src/util/container_meta_data.c792
-rw-r--r--src/util/container_multihashmap.c235
-rw-r--r--src/util/container_slist.c72
-rw-r--r--src/util/crypto_aes.c77
-rw-r--r--src/util/crypto_crc.c12
-rw-r--r--src/util/crypto_hash.c214
-rw-r--r--src/util/crypto_hkdf.c118
-rw-r--r--src/util/crypto_kdf.c14
-rw-r--r--src/util/crypto_ksk.c513
-rw-r--r--src/util/crypto_random.c199
-rw-r--r--src/util/crypto_rsa.c753
-rw-r--r--src/util/disk.c1437
-rw-r--r--src/util/getopt.c1018
-rw-r--r--src/util/getopt_helpers.c171
-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.c46
-rw-r--r--src/util/network.c1072
-rw-r--r--src/util/os_installation.c274
-rw-r--r--src/util/os_network.c365
-rw-r--r--src/util/os_priority.c1110
-rw-r--r--src/util/peer.c102
-rw-r--r--src/util/perf_crypto_hash.c20
-rw-r--r--src/util/plugin.c143
-rw-r--r--src/util/program.c108
-rw-r--r--src/util/pseudonym.c277
-rw-r--r--src/util/resolver_api.c598
-rw-r--r--src/util/scheduler.c825
-rw-r--r--src/util/server.c769
-rw-r--r--src/util/server_mst.c264
-rw-r--r--src/util/server_nc.c205
-rw-r--r--src/util/server_tc.c76
-rw-r--r--src/util/service.c1406
-rw-r--r--src/util/signal.c16
-rw-r--r--src/util/strings.c404
-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.c53
-rw-r--r--src/util/test_common_logging_dummy.c4
-rw-r--r--src/util/test_common_logging_runtime_loglevels.c364
-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.c93
-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_plugin.c6
-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.c45
-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.c62
-rw-r--r--src/util/winproc.c227
91 files changed, 10847 insertions, 11119 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c
index a736b7607..0920362f5 100644
--- a/src/util/bandwidth.c
+++ b/src/util/bandwidth.c
@@ -60,11 +60,11 @@ GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
60 */ 60 */
61struct GNUNET_BANDWIDTH_Value32NBO 61struct GNUNET_BANDWIDTH_Value32NBO
62GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, 62GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
63 struct GNUNET_BANDWIDTH_Value32NBO b2) 63 struct GNUNET_BANDWIDTH_Value32NBO b2)
64{ 64{
65 return 65 return
66 GNUNET_BANDWIDTH_value_init (GNUNET_MIN 66 GNUNET_BANDWIDTH_value_init (GNUNET_MIN
67 (ntohl (b1.value__), ntohl (b2.value__))); 67 (ntohl (b1.value__), ntohl (b2.value__)));
68} 68}
69 69
70 70
@@ -78,9 +78,9 @@ GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
78 */ 78 */
79uint64_t 79uint64_t
80GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO 80GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
81 bps, 81 bps,
82 struct GNUNET_TIME_Relative 82 struct GNUNET_TIME_Relative
83 deadline) 83 deadline)
84{ 84{
85 uint64_t b; 85 uint64_t b;
86 86
@@ -105,20 +105,20 @@ GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
105 */ 105 */
106struct GNUNET_TIME_Relative 106struct GNUNET_TIME_Relative
107GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps, 107GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
108 uint64_t size) 108 uint64_t size)
109{ 109{
110 uint64_t b; 110 uint64_t b;
111 struct GNUNET_TIME_Relative ret; 111 struct GNUNET_TIME_Relative ret;
112 112
113 b = ntohl (bps.value__); 113 b = ntohl (bps.value__);
114 if (b == 0) 114 if (b == 0)
115 { 115 {
116#if DEBUG_BANDWIDTH 116#if DEBUG_BANDWIDTH
117 LOG (GNUNET_ERROR_TYPE_DEBUG, 117 LOG (GNUNET_ERROR_TYPE_DEBUG,
118 "Bandwidth suggests delay of infinity (zero bandwidth)\n"); 118 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
119#endif 119#endif
120 return GNUNET_TIME_UNIT_FOREVER_REL; 120 return GNUNET_TIME_UNIT_FOREVER_REL;
121 } 121 }
122 ret.rel_value = size * 1000LL / b; 122 ret.rel_value = size * 1000LL / b;
123#if DEBUG_BANDWIDTH 123#if DEBUG_BANDWIDTH
124 LOG (GNUNET_ERROR_TYPE_DEBUG, 124 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -145,8 +145,8 @@ GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
145 */ 145 */
146void 146void
147GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av, 147GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
148 struct GNUNET_BANDWIDTH_Value32NBO 148 struct GNUNET_BANDWIDTH_Value32NBO
149 bytes_per_second_limit, uint32_t max_carry_s) 149 bytes_per_second_limit, uint32_t max_carry_s)
150{ 150{
151 av->consumption_since_last_update__ = 0; 151 av->consumption_since_last_update__ = 0;
152 av->last_update__ = GNUNET_TIME_absolute_get (); 152 av->last_update__ = GNUNET_TIME_absolute_get ();
@@ -155,8 +155,7 @@ GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
155#if DEBUG_BANDWIDTH 155#if DEBUG_BANDWIDTH
156 LOG (GNUNET_ERROR_TYPE_DEBUG, 156 LOG (GNUNET_ERROR_TYPE_DEBUG,
157 "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,
158 (unsigned int) av->available_bytes_per_s__, 158 (unsigned int) av->available_bytes_per_s__, (unsigned int) max_carry_s);
159 (unsigned int) max_carry_s);
160#endif 159#endif
161} 160}
162 161
@@ -179,25 +178,25 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
179 now = GNUNET_TIME_absolute_get (); 178 now = GNUNET_TIME_absolute_get ();
180 delta_time = now.abs_value - av->last_update__.abs_value; 179 delta_time = now.abs_value - av->last_update__.abs_value;
181 delta_avail = 180 delta_avail =
182 (delta_time * ((unsigned long long) av->available_bytes_per_s__) + 181 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
183 500LL) / 1000LL; 182 500LL) / 1000LL;
184 av->consumption_since_last_update__ -= delta_avail; 183 av->consumption_since_last_update__ -= delta_avail;
185 av->last_update__ = now; 184 av->last_update__ = now;
186 if (av->consumption_since_last_update__ < 0) 185 if (av->consumption_since_last_update__ < 0)
187 { 186 {
188 left_bytes = -av->consumption_since_last_update__; 187 left_bytes = -av->consumption_since_last_update__;
189 max_carry = av->available_bytes_per_s__ * av->max_carry_s__; 188 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
190 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE) 189 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
191 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE; 190 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
192 if (max_carry > left_bytes) 191 if (max_carry > left_bytes)
193 av->consumption_since_last_update__ = -left_bytes; 192 av->consumption_since_last_update__ = -left_bytes;
194 else 193 else
195 av->consumption_since_last_update__ = -max_carry; 194 av->consumption_since_last_update__ = -max_carry;
196 } 195 }
197#if DEBUG_BANDWIDTH 196#if DEBUG_BANDWIDTH
198 LOG (GNUNET_ERROR_TYPE_DEBUG, 197 LOG (GNUNET_ERROR_TYPE_DEBUG,
199 "Tracker %p updated, have %u Bps, last update was %llu ms ago\n", 198 "Tracker %p updated, have %u Bps, last update was %llu ms ago\n", av,
200 av, (unsigned int) av->available_bytes_per_s__, 199 (unsigned int) av->available_bytes_per_s__,
201 (unsigned long long) delta_time); 200 (unsigned long long) delta_time);
202#endif 201#endif
203 202
@@ -217,7 +216,7 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
217 */ 216 */
218int 217int
219GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, 218GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
220 ssize_t size) 219 ssize_t size)
221{ 220{
222 int64_t nc; 221 int64_t nc;
223 222
@@ -226,29 +225,29 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
226 (int) size); 225 (int) size);
227#endif 226#endif
228 if (size > 0) 227 if (size > 0)
228 {
229 nc = av->consumption_since_last_update__ + size;
230 if (nc < av->consumption_since_last_update__)
231 {
232 GNUNET_break (0);
233 return GNUNET_SYSERR;
234 }
235 av->consumption_since_last_update__ = nc;
236 update_tracker (av);
237 if (av->consumption_since_last_update__ > 0)
229 { 238 {
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 {
240#if DEBUG_BANDWIDTH 239#if DEBUG_BANDWIDTH
241 LOG (GNUNET_ERROR_TYPE_DEBUG, 240 LOG (GNUNET_ERROR_TYPE_DEBUG,
242 "Tracker %p consumption %llu bytes above limit\n", av, 241 "Tracker %p consumption %llu bytes above limit\n", av,
243 (unsigned long long) av->consumption_since_last_update__); 242 (unsigned long long) av->consumption_since_last_update__);
244#endif 243#endif
245 return GNUNET_YES; 244 return GNUNET_YES;
246 }
247 } 245 }
246 }
248 else 247 else
249 { 248 {
250 av->consumption_since_last_update__ += size; 249 av->consumption_since_last_update__ += size;
251 } 250 }
252 return GNUNET_NO; 251 return GNUNET_NO;
253} 252}
254 253
@@ -264,35 +263,34 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
264 */ 263 */
265struct GNUNET_TIME_Relative 264struct GNUNET_TIME_Relative
266GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av, 265GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
267 size_t size) 266 size_t size)
268{ 267{
269 struct GNUNET_TIME_Relative ret; 268 struct GNUNET_TIME_Relative ret;
270 int64_t bytes_needed; 269 int64_t bytes_needed;
271 270
272 if (av->available_bytes_per_s__ == 0) 271 if (av->available_bytes_per_s__ == 0)
273 { 272 {
274#if DEBUG_BANDWIDTH 273#if DEBUG_BANDWIDTH
275 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av); 274 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
276#endif 275#endif
277 return GNUNET_TIME_UNIT_FOREVER_REL; 276 return GNUNET_TIME_UNIT_FOREVER_REL;
278 } 277 }
279 update_tracker (av); 278 update_tracker (av);
280 bytes_needed = size + av->consumption_since_last_update__; 279 bytes_needed = size + av->consumption_since_last_update__;
281 if (bytes_needed <= 0) 280 if (bytes_needed <= 0)
282 { 281 {
283#if DEBUG_BANDWIDTH 282#if DEBUG_BANDWIDTH
284 LOG (GNUNET_ERROR_TYPE_DEBUG, 283 LOG (GNUNET_ERROR_TYPE_DEBUG, "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);
287#endif 285#endif
288 return GNUNET_TIME_UNIT_ZERO; 286 return GNUNET_TIME_UNIT_ZERO;
289 } 287 }
290 ret.rel_value = 288 ret.rel_value =
291 (1000LL * bytes_needed) / (unsigned long long) av->available_bytes_per_s__; 289 (1000LL * bytes_needed) /
290 (unsigned long long) av->available_bytes_per_s__;
292#if DEBUG_BANDWIDTH 291#if DEBUG_BANDWIDTH
293 LOG (GNUNET_ERROR_TYPE_DEBUG, 292 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay for %u bytes is %llu ms\n",
294 "Tracker %p delay for %u bytes is %llu ms\n", av, 293 av, (unsigned int) size, (unsigned long long) ret.rel_value);
295 (unsigned int) size, (unsigned long long) ret.rel_value);
296#endif 294#endif
297 return ret; 295 return ret;
298} 296}
@@ -315,9 +313,9 @@ GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
315 update_tracker (av); 313 update_tracker (av);
316 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__); 314 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
317 avail = 315 avail =
318 GNUNET_BANDWIDTH_value_get_available_until (bps, 316 GNUNET_BANDWIDTH_value_get_available_until (bps,
319 GNUNET_TIME_absolute_get_duration 317 GNUNET_TIME_absolute_get_duration
320 (av->last_update__)); 318 (av->last_update__));
321 used = av->consumption_since_last_update__; 319 used = av->consumption_since_last_update__;
322#if DEBUG_BANDWIDTH 320#if DEBUG_BANDWIDTH
323 LOG (GNUNET_ERROR_TYPE_DEBUG, 321 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -336,23 +334,22 @@ GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
336 */ 334 */
337void 335void
338GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av, 336GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
339 struct GNUNET_BANDWIDTH_Value32NBO 337 struct GNUNET_BANDWIDTH_Value32NBO
340 bytes_per_second_limit) 338 bytes_per_second_limit)
341{ 339{
342 uint32_t old_limit; 340 uint32_t old_limit;
343 uint32_t new_limit; 341 uint32_t new_limit;
344 342
345 new_limit = ntohl (bytes_per_second_limit.value__); 343 new_limit = ntohl (bytes_per_second_limit.value__);
346#if DEBUG_BANDWIDTH 344#if DEBUG_BANDWIDTH
347 LOG (GNUNET_ERROR_TYPE_DEBUG, 345 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p bandwidth changed to %u Bps\n", av,
348 "Tracker %p bandwidth changed to %u Bps\n", av,
349 (unsigned int) new_limit); 346 (unsigned int) new_limit);
350#endif 347#endif
351 update_tracker (av); 348 update_tracker (av);
352 old_limit = av->available_bytes_per_s__; 349 old_limit = av->available_bytes_per_s__;
353 av->available_bytes_per_s__ = new_limit; 350 av->available_bytes_per_s__ = new_limit;
354 if (old_limit > new_limit) 351 if (old_limit > new_limit)
355 update_tracker (av); /* maximum excess might be less now */ 352 update_tracker (av); /* maximum excess might be less now */
356} 353}
357 354
358 355
diff --git a/src/util/bio.c b/src/util/bio.c
index 9c2b9d0dd..41ad5fd5d 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -58,8 +58,7 @@ GNUNET_BIO_read_open (const char *fn)
58 struct GNUNET_DISK_FileHandle *fd; 58 struct GNUNET_DISK_FileHandle *fd;
59 struct GNUNET_BIO_ReadHandle *h; 59 struct GNUNET_BIO_ReadHandle *h;
60 60
61 fd = 61 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
62 GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
63 if (NULL == fd) 62 if (NULL == fd)
64 return NULL; 63 return NULL;
65 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); 64 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
@@ -105,7 +104,7 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
105 */ 104 */
106int 105int
107GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, 106GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
108 void *result, size_t len) 107 void *result, size_t len)
109{ 108{
110 char *dst = result; 109 char *dst = result;
111 size_t min; 110 size_t min;
@@ -116,38 +115,38 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
116 return GNUNET_SYSERR; 115 return GNUNET_SYSERR;
117 pos = 0; 116 pos = 0;
118 do 117 do
118 {
119 /* first, use buffer */
120 min = h->have - h->pos;
121 if (min > 0)
119 { 122 {
120 /* first, use buffer */ 123 if (min > len - pos)
121 min = h->have - h->pos; 124 min = len - pos;
122 if (min > 0) 125 memcpy (&dst[pos], &h->buffer[h->pos], min);
123 { 126 h->pos += min;
124 if (min > len - pos) 127 pos += min;
125 min = len - pos; 128 }
126 memcpy (&dst[pos], &h->buffer[h->pos], min); 129 if (pos == len)
127 h->pos += min; 130 return GNUNET_OK; /* done! */
128 pos += min; 131 GNUNET_assert (h->have == h->pos);
129 } 132 /* fill buffer */
130 if (pos == len) 133 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
131 return GNUNET_OK; /* done! */ 134 if (ret == -1)
132 GNUNET_assert (h->have == h->pos); 135 {
133 /* fill buffer */ 136 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
134 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); 137 STRERROR (errno));
135 if (ret == -1) 138 return GNUNET_SYSERR;
136 { 139 }
137 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, 140 if (ret == 0)
138 STRERROR (errno)); 141 {
139 return GNUNET_SYSERR; 142 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
140 } 143 _("End of file"));
141 if (ret == 0) 144 return GNUNET_SYSERR;
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;
149 } 145 }
150 while (pos < len); /* should always be true */ 146 h->pos = 0;
147 h->have = ret;
148 }
149 while (pos < len); /* should always be true */
151 return GNUNET_OK; 150 return GNUNET_OK;
152} 151}
153 152
@@ -163,8 +162,8 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
163 * @return GNUNET_OK on success, GNUNET_SYSERR on failure 162 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
164 */ 163 */
165int 164int
166GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, 165GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line,
167 int line, void *result, size_t len) 166 void *result, size_t len)
168{ 167{
169 char what[1024]; 168 char what[1024];
170 169
@@ -185,41 +184,39 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file,
185 */ 184 */
186int 185int
187GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, 186GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
188 char **result, size_t maxLen) 187 char **result, size_t maxLen)
189{ 188{
190 char *buf; 189 char *buf;
191 uint32_t big; 190 uint32_t big;
192 191
193 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) 192 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
194 { 193 {
195 GNUNET_free_non_null (h->emsg); 194 GNUNET_free_non_null (h->emsg);
196 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), 195 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
197 what); 196 return GNUNET_SYSERR;
198 return GNUNET_SYSERR; 197 }
199 }
200 if (big == 0) 198 if (big == 0)
201 { 199 {
202 *result = NULL; 200 *result = NULL;
203 return GNUNET_OK; 201 return GNUNET_OK;
204 } 202 }
205 if (big > maxLen) 203 if (big > maxLen)
206 { 204 {
207 GNUNET_asprintf (&h->emsg, 205 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
208 _("String `%s' longer than allowed (%u > %u)"), what, 206 what, big, maxLen);
209 big, maxLen); 207 return GNUNET_SYSERR;
210 return GNUNET_SYSERR; 208 }
211 }
212 buf = GNUNET_malloc (big); 209 buf = GNUNET_malloc (big);
213 *result = buf; 210 *result = buf;
214 buf[--big] = '\0'; 211 buf[--big] = '\0';
215 if (big == 0) 212 if (big == 0)
216 return GNUNET_OK; 213 return GNUNET_OK;
217 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) 214 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
218 { 215 {
219 GNUNET_free (buf); 216 GNUNET_free (buf);
220 *result = NULL; 217 *result = NULL;
221 return GNUNET_SYSERR; 218 return GNUNET_SYSERR;
222 } 219 }
223 return GNUNET_OK; 220 return GNUNET_OK;
224} 221}
225 222
@@ -234,7 +231,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
234 */ 231 */
235int 232int
236GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, 233GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
237 struct GNUNET_CONTAINER_MetaData **result) 234 struct GNUNET_CONTAINER_MetaData **result)
238{ 235{
239 uint32_t size; 236 uint32_t size;
240 char *buf; 237 char *buf;
@@ -243,32 +240,30 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
243 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK) 240 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
244 return GNUNET_SYSERR; 241 return GNUNET_SYSERR;
245 if (size == 0) 242 if (size == 0)
246 { 243 {
247 *result = NULL; 244 *result = NULL;
248 return GNUNET_OK; 245 return GNUNET_OK;
249 } 246 }
250 if (size > MAX_META_DATA) 247 if (size > MAX_META_DATA)
251 { 248 {
252 GNUNET_asprintf (&h->emsg, 249 GNUNET_asprintf (&h->emsg,
253 _ 250 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
254 ("Serialized metadata `%s' larger than allowed (%u>%u)"), 251 what, size, MAX_META_DATA);
255 what, size, MAX_META_DATA); 252 return GNUNET_SYSERR;
256 return GNUNET_SYSERR; 253 }
257 }
258 buf = GNUNET_malloc (size); 254 buf = GNUNET_malloc (size);
259 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) 255 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
260 { 256 {
261 GNUNET_free (buf); 257 GNUNET_free (buf);
262 return GNUNET_SYSERR; 258 return GNUNET_SYSERR;
263 } 259 }
264 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); 260 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
265 if (meta == NULL) 261 if (meta == NULL)
266 { 262 {
267 GNUNET_free (buf); 263 GNUNET_free (buf);
268 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), 264 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
269 what); 265 return GNUNET_SYSERR;
270 return GNUNET_SYSERR; 266 }
271 }
272 GNUNET_free (buf); 267 GNUNET_free (buf);
273 *result = meta; 268 *result = meta;
274 return GNUNET_OK; 269 return GNUNET_OK;
@@ -286,7 +281,7 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
286 */ 281 */
287int 282int
288GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, 283GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
289 int line, int32_t * i) 284 int line, int32_t * i)
290{ 285{
291 int32_t big; 286 int32_t big;
292 287
@@ -308,7 +303,7 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
308 */ 303 */
309int 304int
310GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file, 305GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
311 int line, int64_t * i) 306 int line, int64_t * i)
312{ 307{
313 int64_t big; 308 int64_t big;
314 309
@@ -344,15 +339,13 @@ GNUNET_BIO_write_open (const char *fn)
344 struct GNUNET_BIO_WriteHandle *h; 339 struct GNUNET_BIO_WriteHandle *h;
345 340
346 fd = GNUNET_DISK_file_open (fn, 341 fd = GNUNET_DISK_file_open (fn,
347 GNUNET_DISK_OPEN_WRITE | 342 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
348 GNUNET_DISK_OPEN_TRUNCATE | 343 | GNUNET_DISK_OPEN_CREATE,
349 GNUNET_DISK_OPEN_CREATE, 344 GNUNET_DISK_PERM_USER_READ |
350 GNUNET_DISK_PERM_USER_READ | 345 GNUNET_DISK_PERM_USER_WRITE);
351 GNUNET_DISK_PERM_USER_WRITE);
352 if (NULL == fd) 346 if (NULL == fd)
353 return NULL; 347 return NULL;
354 h = 348 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
355 GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
356 h->buffer = (char *) &h[1]; 349 h->buffer = (char *) &h[1];
357 h->size = BIO_BUFFER_SIZE; 350 h->size = BIO_BUFFER_SIZE;
358 h->fd = fd; 351 h->fd = fd;
@@ -374,18 +367,18 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
374 int ret; 367 int ret;
375 368
376 if (NULL == h->fd) 369 if (NULL == h->fd)
377 { 370 {
378 ret = GNUNET_SYSERR; 371 ret = GNUNET_SYSERR;
379 } 372 }
380 else 373 else
381 { 374 {
382 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); 375 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
383 if (wrt == h->have) 376 if (wrt == h->have)
384 ret = GNUNET_OK; 377 ret = GNUNET_OK;
385 else 378 else
386 ret = GNUNET_SYSERR; 379 ret = GNUNET_SYSERR;
387 GNUNET_DISK_file_close (h->fd); 380 GNUNET_DISK_file_close (h->fd);
388 } 381 }
389 GNUNET_free (h); 382 GNUNET_free (h);
390 return ret; 383 return ret;
391} 384}
@@ -401,7 +394,7 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
401 */ 394 */
402int 395int
403GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, 396GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
404 size_t n) 397 size_t n)
405{ 398{
406 const char *src = buffer; 399 const char *src = buffer;
407 size_t min; 400 size_t min;
@@ -412,27 +405,27 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
412 return GNUNET_SYSERR; 405 return GNUNET_SYSERR;
413 pos = 0; 406 pos = 0;
414 do 407 do
408 {
409 /* first, just use buffer */
410 min = h->size - h->have;
411 if (min > n - pos)
412 min = n - pos;
413 memcpy (&h->buffer[h->have], &src[pos], min);
414 pos += min;
415 h->have += min;
416 if (pos == n)
417 return GNUNET_OK; /* done */
418 GNUNET_assert (h->have == h->size);
419 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
420 if (ret != h->size)
415 { 421 {
416 /* first, just use buffer */ 422 GNUNET_DISK_file_close (h->fd);
417 min = h->size - h->have; 423 h->fd = NULL;
418 if (min > n - pos) 424 return GNUNET_SYSERR; /* error */
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;
434 } 425 }
435 while (pos < n); /* should always be true */ 426 h->have = 0;
427 }
428 while (pos < n); /* should always be true */
436 GNUNET_break (0); 429 GNUNET_break (0);
437 return GNUNET_OK; 430 return GNUNET_OK;
438} 431}
@@ -468,7 +461,7 @@ GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
468 */ 461 */
469int 462int
470GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, 463GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
471 const struct GNUNET_CONTAINER_MetaData *m) 464 const struct GNUNET_CONTAINER_MetaData *m)
472{ 465{
473 ssize_t size; 466 ssize_t size;
474 char *buf; 467 char *buf;
@@ -477,19 +470,19 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
477 return GNUNET_BIO_write_int32 (h, 0); 470 return GNUNET_BIO_write_int32 (h, 0);
478 buf = NULL; 471 buf = NULL;
479 size = 472 size =
480 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, 473 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
481 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); 474 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
482 if (size == -1) 475 if (size == -1)
483 { 476 {
484 GNUNET_free (buf); 477 GNUNET_free (buf);
485 return GNUNET_SYSERR; 478 return GNUNET_SYSERR;
486 } 479 }
487 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || 480 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
488 (GNUNET_OK != GNUNET_BIO_write (h, buf, size))) 481 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
489 { 482 {
490 GNUNET_free (buf); 483 GNUNET_free (buf);
491 return GNUNET_SYSERR; 484 return GNUNET_SYSERR;
492 } 485 }
493 GNUNET_free (buf); 486 GNUNET_free (buf);
494 return GNUNET_OK; 487 return GNUNET_OK;
495} 488}
diff --git a/src/util/client.c b/src/util/client.c
index d2f347947..e71ccdf39 100644
--- a/src/util/client.c
+++ b/src/util/client.c
@@ -259,8 +259,7 @@ struct GNUNET_CLIENT_Connection
259 */ 259 */
260static struct GNUNET_CONNECTION_Handle * 260static struct GNUNET_CONNECTION_Handle *
261do_connect (const char *service_name, 261do_connect (const char *service_name,
262 const struct GNUNET_CONFIGURATION_Handle *cfg, 262 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
263 unsigned int attempt)
264{ 263{
265 struct GNUNET_CONNECTION_Handle *sock; 264 struct GNUNET_CONNECTION_Handle *sock;
266 char *hostname; 265 char *hostname;
@@ -270,80 +269,78 @@ do_connect (const char *service_name,
270 sock = NULL; 269 sock = NULL;
271#if AF_UNIX 270#if AF_UNIX
272 if (0 == (attempt % 2)) 271 if (0 == (attempt % 2))
272 {
273 /* on even rounds, try UNIX */
274 unixpath = NULL;
275 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? */
273 { 276 {
274 /* on even rounds, try UNIX */ 277 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
275 unixpath = NULL; 278 if (sock != NULL)
276 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 {
277 {
278 sock =
279 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
280 if (sock != NULL)
281 {
282#if DEBUG_CLIENT 280#if DEBUG_CLIENT
283 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", 281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
284 unixpath); 282 unixpath);
285#endif 283#endif
286 GNUNET_free (unixpath); 284 GNUNET_free (unixpath);
287 return sock; 285 return sock;
288 } 286 }
289 }
290 GNUNET_free_non_null (unixpath);
291 } 287 }
288 GNUNET_free_non_null (unixpath);
289 }
292#endif 290#endif
293 291
294 if ((GNUNET_OK != 292 if ((GNUNET_OK !=
295 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", 293 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
296 &port)) || (port > 65535) 294 || (port > 65535) ||
297 || (GNUNET_OK != 295 (GNUNET_OK !=
298 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, 296 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
299 "HOSTNAME", &hostname))) 297 &hostname)))
300 { 298 {
301 LOG (GNUNET_ERROR_TYPE_WARNING, 299 LOG (GNUNET_ERROR_TYPE_WARNING,
302 _ 300 _
303 ("Could not determine valid hostname and port for service `%s' from configuration.\n"), 301 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
304 service_name); 302 service_name);
305 return NULL; 303 return NULL;
306 } 304 }
307 if (0 == strlen (hostname)) 305 if (0 == strlen (hostname))
308 { 306 {
309 GNUNET_free (hostname); 307 GNUNET_free (hostname);
310 LOG (GNUNET_ERROR_TYPE_WARNING, 308 LOG (GNUNET_ERROR_TYPE_WARNING,
311 _("Need a non-empty hostname for service `%s'.\n"), service_name); 309 _("Need a non-empty hostname for service `%s'.\n"), service_name);
312 return NULL; 310 return NULL;
313 } 311 }
314 if (port == 0) 312 if (port == 0)
315 { 313 {
316#if AF_UNIX 314#if AF_UNIX
317 if (0 != (attempt % 2)) 315 if (0 != (attempt % 2))
318 { 316 {
319 /* try UNIX */ 317 /* try UNIX */
320 unixpath = NULL; 318 unixpath = NULL;
321 if ((GNUNET_OK == 319 if ((GNUNET_OK ==
322 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, 320 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
323 "UNIXPATH", &unixpath)) 321 &unixpath)) &&
324 && (0 < strlen (unixpath))) 322 (0 < strlen (unixpath)))
325 { 323 {
326 sock = 324 sock =
327 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, 325 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
328 unixpath); 326 if (sock != NULL)
329 if (sock != NULL) 327 {
330 { 328 GNUNET_free (unixpath);
331 GNUNET_free (unixpath); 329 GNUNET_free (hostname);
332 GNUNET_free (hostname); 330 return sock;
333 return sock; 331 }
334 } 332 }
335 } 333 GNUNET_free_non_null (unixpath);
336 GNUNET_free_non_null (unixpath); 334 }
337 }
338#endif 335#endif
339#if DEBUG_CLIENT 336#if DEBUG_CLIENT
340 LOG (GNUNET_ERROR_TYPE_DEBUG, 337 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n", 338 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
342 service_name); 339 service_name);
343#endif 340#endif
344 GNUNET_free (hostname); 341 GNUNET_free (hostname);
345 return NULL; 342 return NULL;
346 } 343 }
347 344
348 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port); 345 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
349 GNUNET_free (hostname); 346 GNUNET_free (hostname);
@@ -360,7 +357,7 @@ do_connect (const char *service_name,
360 */ 357 */
361struct GNUNET_CLIENT_Connection * 358struct GNUNET_CLIENT_Connection *
362GNUNET_CLIENT_connect (const char *service_name, 359GNUNET_CLIENT_connect (const char *service_name,
363 const struct GNUNET_CONFIGURATION_Handle *cfg) 360 const struct GNUNET_CONFIGURATION_Handle *cfg)
364{ 361{
365 struct GNUNET_CLIENT_Connection *ret; 362 struct GNUNET_CLIENT_Connection *ret;
366 struct GNUNET_CONNECTION_Handle *sock; 363 struct GNUNET_CONNECTION_Handle *sock;
@@ -393,33 +390,33 @@ GNUNET_CLIENT_connect (const char *service_name,
393 */ 390 */
394void 391void
395GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock, 392GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
396 int finish_pending_write) 393 int finish_pending_write)
397{ 394{
398 if (sock->in_receive == GNUNET_YES) 395 if (sock->in_receive == GNUNET_YES)
399 { 396 {
400 GNUNET_CONNECTION_receive_cancel (sock->sock); 397 GNUNET_CONNECTION_receive_cancel (sock->sock);
401 sock->in_receive = GNUNET_NO; 398 sock->in_receive = GNUNET_NO;
402 } 399 }
403 if (sock->th != NULL) 400 if (sock->th != NULL)
404 { 401 {
405 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th); 402 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
406 sock->th = NULL; 403 sock->th = NULL;
407 } 404 }
408 if (NULL != sock->sock) 405 if (NULL != sock->sock)
409 { 406 {
410 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write); 407 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
411 sock->sock = NULL; 408 sock->sock = NULL;
412 } 409 }
413 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK) 410 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
414 { 411 {
415 GNUNET_SCHEDULER_cancel (sock->receive_task); 412 GNUNET_SCHEDULER_cancel (sock->receive_task);
416 sock->receive_task = GNUNET_SCHEDULER_NO_TASK; 413 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
417 } 414 }
418 if (sock->tag != NULL) 415 if (sock->tag != NULL)
419 { 416 {
420 GNUNET_free (sock->tag); 417 GNUNET_free (sock->tag);
421 sock->tag = NULL; 418 sock->tag = NULL;
422 } 419 }
423 sock->receiver_handler = NULL; 420 sock->receiver_handler = NULL;
424 GNUNET_array_grow (sock->received_buf, sock->received_size, 0); 421 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
425 GNUNET_free (sock->service_name); 422 GNUNET_free (sock->service_name);
@@ -436,7 +433,7 @@ check_complete (struct GNUNET_CLIENT_Connection *conn)
436 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) && 433 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
437 (conn->received_pos >= 434 (conn->received_pos >=
438 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)-> 435 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
439 size))) 436 size)))
440 conn->msg_complete = GNUNET_YES; 437 conn->msg_complete = GNUNET_YES;
441} 438}
442 439
@@ -455,7 +452,7 @@ check_complete (struct GNUNET_CLIENT_Connection *conn)
455 */ 452 */
456static void 453static void
457receive_helper (void *cls, const void *buf, size_t available, 454receive_helper (void *cls, const void *buf, size_t available,
458 const struct sockaddr *addr, socklen_t addrlen, int errCode) 455 const struct sockaddr *addr, socklen_t addrlen, int errCode)
459{ 456{
460 struct GNUNET_CLIENT_Connection *conn = cls; 457 struct GNUNET_CLIENT_Connection *conn = cls;
461 struct GNUNET_TIME_Relative remaining; 458 struct GNUNET_TIME_Relative remaining;
@@ -465,22 +462,22 @@ receive_helper (void *cls, const void *buf, size_t available,
465 GNUNET_assert (conn->msg_complete == GNUNET_NO); 462 GNUNET_assert (conn->msg_complete == GNUNET_NO);
466 conn->in_receive = GNUNET_NO; 463 conn->in_receive = GNUNET_NO;
467 if ((available == 0) || (conn->sock == NULL) || (errCode != 0)) 464 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
468 { 465 {
469 /* signal timeout! */ 466 /* signal timeout! */
470#if DEBUG_CLIENT 467#if DEBUG_CLIENT
471 LOG (GNUNET_ERROR_TYPE_DEBUG, 468 LOG (GNUNET_ERROR_TYPE_DEBUG,
472 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n", 469 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
473 (unsigned int) available, 470 (unsigned int) available, conn->sock == NULL ? "NULL" : "non-NULL",
474 conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode)); 471 STRERROR (errCode));
475#endif 472#endif
476 if (NULL != (receive_handler = conn->receiver_handler)) 473 if (NULL != (receive_handler = conn->receiver_handler))
477 { 474 {
478 receive_handler_cls = conn->receiver_handler_cls; 475 receive_handler_cls = conn->receiver_handler_cls;
479 conn->receiver_handler = NULL; 476 conn->receiver_handler = NULL;
480 receive_handler (receive_handler_cls, NULL); 477 receive_handler (receive_handler_cls, NULL);
481 }
482 return;
483 } 478 }
479 return;
480 }
484 481
485 /* FIXME: optimize for common fast case where buf contains the 482 /* FIXME: optimize for common fast case where buf contains the
486 * entire message and we need no copying... */ 483 * entire message and we need no copying... */
@@ -489,22 +486,22 @@ receive_helper (void *cls, const void *buf, size_t available,
489 /* slow path: append to array */ 486 /* slow path: append to array */
490 if (conn->received_size < conn->received_pos + available) 487 if (conn->received_size < conn->received_pos + available)
491 GNUNET_array_grow (conn->received_buf, conn->received_size, 488 GNUNET_array_grow (conn->received_buf, conn->received_size,
492 conn->received_pos + available); 489 conn->received_pos + available);
493 memcpy (&conn->received_buf[conn->received_pos], buf, available); 490 memcpy (&conn->received_buf[conn->received_pos], buf, available);
494 conn->received_pos += available; 491 conn->received_pos += available;
495 check_complete (conn); 492 check_complete (conn);
496 /* check for timeout */ 493 /* check for timeout */
497 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout); 494 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
498 if (remaining.rel_value == 0) 495 if (remaining.rel_value == 0)
499 { 496 {
500 /* signal timeout! */ 497 /* signal timeout! */
501 if (NULL != conn->receiver_handler) 498 if (NULL != conn->receiver_handler)
502 conn->receiver_handler (conn->receiver_handler_cls, NULL); 499 conn->receiver_handler (conn->receiver_handler_cls, NULL);
503 return; 500 return;
504 } 501 }
505 /* back to receive -- either for more data or to call callback! */ 502 /* back to receive -- either for more data or to call callback! */
506 GNUNET_CLIENT_receive (conn, conn->receiver_handler, 503 GNUNET_CLIENT_receive (conn, conn->receiver_handler,
507 conn->receiver_handler_cls, remaining); 504 conn->receiver_handler_cls, remaining);
508} 505}
509 506
510 507
@@ -520,23 +517,22 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
520 struct GNUNET_CLIENT_Connection *sock = cls; 517 struct GNUNET_CLIENT_Connection *sock = cls;
521 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler; 518 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
522 const struct GNUNET_MessageHeader *cmsg = 519 const struct GNUNET_MessageHeader *cmsg =
523 (const struct GNUNET_MessageHeader *) sock->received_buf; 520 (const struct GNUNET_MessageHeader *) sock->received_buf;
524 void *handler_cls = sock->receiver_handler_cls; 521 void *handler_cls = sock->receiver_handler_cls;
525 uint16_t msize = ntohs (cmsg->size); 522 uint16_t msize = ntohs (cmsg->size);
526 char mbuf[msize]; 523 char mbuf[msize];
527 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf; 524 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
528 525
529#if DEBUG_CLIENT 526#if DEBUG_CLIENT
530 LOG (GNUNET_ERROR_TYPE_DEBUG, 527 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
531 "Received message of type %u and size %u\n", ntohs (cmsg->type), 528 ntohs (cmsg->type), msize);
532 msize);
533#endif 529#endif
534 sock->receive_task = GNUNET_SCHEDULER_NO_TASK; 530 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
535 GNUNET_assert (GNUNET_YES == sock->msg_complete); 531 GNUNET_assert (GNUNET_YES == sock->msg_complete);
536 GNUNET_assert (sock->received_pos >= msize); 532 GNUNET_assert (sock->received_pos >= msize);
537 memcpy (msg, cmsg, msize); 533 memcpy (msg, cmsg, msize);
538 memmove (sock->received_buf, &sock->received_buf[msize], 534 memmove (sock->received_buf, &sock->received_buf[msize],
539 sock->received_pos - msize); 535 sock->received_pos - msize);
540 sock->received_pos -= msize; 536 sock->received_pos -= msize;
541 sock->msg_complete = GNUNET_NO; 537 sock->msg_complete = GNUNET_NO;
542 sock->receiver_handler = NULL; 538 sock->receiver_handler = NULL;
@@ -556,36 +552,35 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
556 */ 552 */
557void 553void
558GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock, 554GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
559 GNUNET_CLIENT_MessageHandler handler, 555 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
560 void *handler_cls, struct GNUNET_TIME_Relative timeout) 556 struct GNUNET_TIME_Relative timeout)
561{ 557{
562 if (sock->sock == NULL) 558 if (sock->sock == NULL)
563 { 559 {
564 /* already disconnected, fail instantly! */ 560 /* already disconnected, fail instantly! */
565 GNUNET_break (0); /* this should not happen in well-written code! */ 561 GNUNET_break (0); /* this should not happen in well-written code! */
566 if (NULL != handler) 562 if (NULL != handler)
567 handler (handler_cls, NULL); 563 handler (handler_cls, NULL);
568 return; 564 return;
569 } 565 }
570 sock->receiver_handler = handler; 566 sock->receiver_handler = handler;
571 sock->receiver_handler_cls = handler_cls; 567 sock->receiver_handler_cls = handler_cls;
572 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 568 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
573 if (GNUNET_YES == sock->msg_complete) 569 if (GNUNET_YES == sock->msg_complete)
574 { 570 {
575 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task); 571 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
576 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock); 572 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
577 } 573 }
578 else 574 else
579 { 575 {
580 GNUNET_assert (sock->in_receive == GNUNET_NO); 576 GNUNET_assert (sock->in_receive == GNUNET_NO);
581 sock->in_receive = GNUNET_YES; 577 sock->in_receive = GNUNET_YES;
582#if DEBUG_CLIENT 578#if DEBUG_CLIENT
583 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n"); 579 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
584#endif 580#endif
585 GNUNET_CONNECTION_receive (sock->sock, 581 GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
586 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, timeout, 582 timeout, &receive_helper, sock);
587 &receive_helper, sock); 583 }
588 }
589} 584}
590 585
591 586
@@ -596,7 +591,7 @@ static void
596service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls) 591service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
597{ 592{
598 GNUNET_SCHEDULER_add_continuation (task, task_cls, 593 GNUNET_SCHEDULER_add_continuation (task, task_cls,
599 GNUNET_SCHEDULER_REASON_TIMEOUT); 594 GNUNET_SCHEDULER_REASON_TIMEOUT);
600} 595}
601 596
602 597
@@ -615,18 +610,18 @@ confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
615 * detail in the future, for example, is this the 610 * detail in the future, for example, is this the
616 * correct service? FIXME! */ 611 * correct service? FIXME! */
617 if (msg != NULL) 612 if (msg != NULL)
618 { 613 {
619#if DEBUG_CLIENT 614#if DEBUG_CLIENT
620 LOG (GNUNET_ERROR_TYPE_DEBUG, 615 LOG (GNUNET_ERROR_TYPE_DEBUG,
621 "Received confirmation that service is running.\n"); 616 "Received confirmation that service is running.\n");
622#endif 617#endif
623 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls, 618 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls,
624 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 619 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
625 } 620 }
626 else 621 else
627 { 622 {
628 service_test_error (conn->test_cb, conn->test_cb_cls); 623 service_test_error (conn->test_cb, conn->test_cb_cls);
629 } 624 }
630 GNUNET_CLIENT_disconnect (conn, GNUNET_NO); 625 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
631} 626}
632 627
@@ -647,14 +642,14 @@ write_test (void *cls, size_t size, void *buf)
647 struct GNUNET_MessageHeader *msg; 642 struct GNUNET_MessageHeader *msg;
648 643
649 if (size < sizeof (struct GNUNET_MessageHeader)) 644 if (size < sizeof (struct GNUNET_MessageHeader))
650 { 645 {
651#if DEBUG_CLIENT 646#if DEBUG_CLIENT
652 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n")); 647 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
653#endif 648#endif
654 service_test_error (conn->test_cb, conn->test_cb_cls); 649 service_test_error (conn->test_cb, conn->test_cb_cls);
655 GNUNET_CLIENT_disconnect (conn, GNUNET_NO); 650 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
656 return 0; /* client disconnected */ 651 return 0; /* client disconnected */
657 } 652 }
658#if DEBUG_CLIENT 653#if DEBUG_CLIENT
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST"); 654 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
660#endif 655#endif
@@ -662,8 +657,8 @@ write_test (void *cls, size_t size, void *buf)
662 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); 657 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
663 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 658 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
664 GNUNET_CLIENT_receive (conn, &confirm_handler, conn, 659 GNUNET_CLIENT_receive (conn, &confirm_handler, conn,
665 GNUNET_TIME_absolute_get_remaining 660 GNUNET_TIME_absolute_get_remaining
666 (conn->test_deadline)); 661 (conn->test_deadline));
667 return sizeof (struct GNUNET_MessageHeader); 662 return sizeof (struct GNUNET_MessageHeader);
668} 663}
669 664
@@ -683,9 +678,9 @@ write_test (void *cls, size_t size, void *buf)
683 */ 678 */
684void 679void
685GNUNET_CLIENT_service_test (const char *service, 680GNUNET_CLIENT_service_test (const char *service,
686 const struct GNUNET_CONFIGURATION_Handle *cfg, 681 const struct GNUNET_CONFIGURATION_Handle *cfg,
687 struct GNUNET_TIME_Relative timeout, 682 struct GNUNET_TIME_Relative timeout,
688 GNUNET_SCHEDULER_Task task, void *task_cls) 683 GNUNET_SCHEDULER_Task task, void *task_cls)
689{ 684{
690 char *hostname; 685 char *hostname;
691 unsigned long long port; 686 unsigned long long port;
@@ -704,50 +699,49 @@ GNUNET_CLIENT_service_test (const char *service,
704 char *unixpath; 699 char *unixpath;
705 700
706 unixpath = NULL; 701 unixpath = NULL;
707 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? */ 702 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? */
703 {
704 if (strlen (unixpath) >= sizeof (s_un.sun_path))
705 {
706 LOG (GNUNET_ERROR_TYPE_WARNING,
707 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
708 sizeof (s_un.sun_path));
709 }
710 else
708 { 711 {
709 if (strlen (unixpath) >= sizeof (s_un.sun_path)) 712 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
710 { 713 if (sock != NULL)
711 LOG (GNUNET_ERROR_TYPE_WARNING, 714 {
712 _("UNIXPATH `%s' too long, maximum length is %llu\n"), 715 memset (&s_un, 0, sizeof (s_un));
713 unixpath, sizeof (s_un.sun_path)); 716 s_un.sun_family = AF_UNIX;
714 } 717 slen = strlen (unixpath) + 1;
715 else 718 if (slen >= sizeof (s_un.sun_path))
716 { 719 slen = sizeof (s_un.sun_path) - 1;
717 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); 720 memcpy (s_un.sun_path, unixpath, slen);
718 if (sock != NULL) 721 s_un.sun_path[slen] = '\0';
719 { 722 slen = sizeof (struct sockaddr_un);
720 memset (&s_un, 0, sizeof (s_un));
721 s_un.sun_family = AF_UNIX;
722 slen = strlen (unixpath) + 1;
723 if (slen >= sizeof (s_un.sun_path))
724 slen = sizeof (s_un.sun_path) - 1;
725 memcpy (s_un.sun_path, unixpath, slen);
726 s_un.sun_path[slen] = '\0';
727 slen = sizeof (struct sockaddr_un);
728#if LINUX 723#if LINUX
729 s_un.sun_path[0] = '\0'; 724 s_un.sun_path[0] = '\0';
730#endif 725#endif
731#if HAVE_SOCKADDR_IN_SIN_LEN 726#if HAVE_SOCKADDR_IN_SIN_LEN
732 s_un.sun_len = (u_char) slen; 727 s_un.sun_len = (u_char) slen;
733#endif 728#endif
734 if (GNUNET_OK != 729 if (GNUNET_OK !=
735 GNUNET_NETWORK_socket_bind (sock, 730 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
736 (const struct sockaddr *) 731 slen))
737 &s_un, slen)) 732 {
738 { 733 /* failed to bind => service must be running */
739 /* failed to bind => service must be running */ 734 GNUNET_free (unixpath);
740 GNUNET_free (unixpath); 735 (void) GNUNET_NETWORK_socket_close (sock);
741 (void) GNUNET_NETWORK_socket_close (sock); 736 GNUNET_SCHEDULER_add_continuation (task, task_cls,
742 GNUNET_SCHEDULER_add_continuation (task, task_cls, 737 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
743 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 738 return;
744 return; 739 }
745 } 740 (void) GNUNET_NETWORK_socket_close (sock);
746 (void) GNUNET_NETWORK_socket_close (sock); 741 }
747 } 742 /* let's try IP */
748 /* let's try IP */
749 }
750 } 743 }
744 }
751 GNUNET_free_non_null (unixpath); 745 GNUNET_free_non_null (unixpath);
752 } 746 }
753#endif 747#endif
@@ -758,124 +752,121 @@ GNUNET_CLIENT_service_test (const char *service,
758 (port > 65535) || 752 (port > 65535) ||
759 (GNUNET_OK != 753 (GNUNET_OK !=
760 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME", 754 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
761 &hostname))) 755 &hostname)))
762 { 756 {
763 /* UNIXPATH failed (if possible) AND IP failed => error */ 757 /* UNIXPATH failed (if possible) AND IP failed => error */
764 service_test_error (task, task_cls); 758 service_test_error (task, task_cls);
765 return; 759 return;
766 } 760 }
767 761
768 if (0 == strcmp ("localhost", hostname) 762 if (0 == strcmp ("localhost", hostname)
769#if !LINUX 763#if !LINUX
770 && 0 764 && 0
771#endif 765#endif
772 ) 766 )
773 { 767 {
774 /* can test using 'bind' */ 768 /* can test using 'bind' */
775 struct sockaddr_in s_in; 769 struct sockaddr_in s_in;
776 770
777 memset (&s_in, 0, sizeof (s_in)); 771 memset (&s_in, 0, sizeof (s_in));
778#if HAVE_SOCKADDR_IN_SIN_LEN 772#if HAVE_SOCKADDR_IN_SIN_LEN
779 s_in.sin_len = sizeof (struct sockaddr_in); 773 s_in.sin_len = sizeof (struct sockaddr_in);
780#endif 774#endif
781 s_in.sin_family = AF_INET; 775 s_in.sin_family = AF_INET;
782 s_in.sin_port = htons (port); 776 s_in.sin_port = htons (port);
783 777
784 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); 778 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
785 if (sock != NULL) 779 if (sock != NULL)
786 { 780 {
787 if (GNUNET_OK != 781 if (GNUNET_OK !=
788 GNUNET_NETWORK_socket_bind (sock, 782 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
789 (const struct sockaddr *) &s_in, 783 sizeof (s_in)))
790 sizeof (s_in))) 784 {
791 { 785 /* failed to bind => service must be running */
792 /* failed to bind => service must be running */ 786 GNUNET_free (hostname);
793 GNUNET_free (hostname); 787 (void) GNUNET_NETWORK_socket_close (sock);
794 (void) GNUNET_NETWORK_socket_close (sock); 788 GNUNET_SCHEDULER_add_continuation (task, task_cls,
795 GNUNET_SCHEDULER_add_continuation (task, task_cls, 789 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
796 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 790 return;
797 return; 791 }
798 } 792 (void) GNUNET_NETWORK_socket_close (sock);
799 (void) GNUNET_NETWORK_socket_close (sock);
800 }
801 } 793 }
794 }
802 795
803 if (0 == strcmp ("ip6-localhost", hostname) 796 if (0 == strcmp ("ip6-localhost", hostname)
804#if !LINUX 797#if !LINUX
805 && 0 798 && 0
806#endif 799#endif
807 ) 800 )
808 { 801 {
809 /* can test using 'bind' */ 802 /* can test using 'bind' */
810 struct sockaddr_in6 s_in6; 803 struct sockaddr_in6 s_in6;
811 804
812 memset (&s_in6, 0, sizeof (s_in6)); 805 memset (&s_in6, 0, sizeof (s_in6));
813#if HAVE_SOCKADDR_IN_SIN_LEN 806#if HAVE_SOCKADDR_IN_SIN_LEN
814 s_in6.sin6_len = sizeof (struct sockaddr_in6); 807 s_in6.sin6_len = sizeof (struct sockaddr_in6);
815#endif 808#endif
816 s_in6.sin6_family = AF_INET6; 809 s_in6.sin6_family = AF_INET6;
817 s_in6.sin6_port = htons (port); 810 s_in6.sin6_port = htons (port);
818 811
819 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0); 812 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
820 if (sock != NULL) 813 if (sock != NULL)
821 { 814 {
822 if (GNUNET_OK != 815 if (GNUNET_OK !=
823 GNUNET_NETWORK_socket_bind (sock, 816 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
824 (const struct sockaddr *) &s_in6, 817 sizeof (s_in6)))
825 sizeof (s_in6))) 818 {
826 { 819 /* failed to bind => service must be running */
827 /* failed to bind => service must be running */ 820 GNUNET_free (hostname);
828 GNUNET_free (hostname); 821 (void) GNUNET_NETWORK_socket_close (sock);
829 (void) GNUNET_NETWORK_socket_close (sock); 822 GNUNET_SCHEDULER_add_continuation (task, task_cls,
830 GNUNET_SCHEDULER_add_continuation (task, task_cls, 823 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
831 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 824 return;
832 return; 825 }
833 } 826 (void) GNUNET_NETWORK_socket_close (sock);
834 (void) GNUNET_NETWORK_socket_close (sock);
835 }
836 } 827 }
828 }
837 829
838 if (((0 == strcmp ("localhost", hostname)) || 830 if (((0 == strcmp ("localhost", hostname)) ||
839 (0 == strcmp ("ip6-localhost", hostname))) 831 (0 == strcmp ("ip6-localhost", hostname)))
840#if !LINUX 832#if !LINUX
841 && 0 833 && 0
842#endif 834#endif
843 ) 835 )
844 { 836 {
845 /* all binds succeeded => claim service not running right now */ 837 /* all binds succeeded => claim service not running right now */
846 GNUNET_free_non_null (hostname); 838 GNUNET_free_non_null (hostname);
847 service_test_error (task, task_cls); 839 service_test_error (task, task_cls);
848 return; 840 return;
849 } 841 }
850 GNUNET_free_non_null (hostname); 842 GNUNET_free_non_null (hostname);
851 843
852 /* non-localhost, try 'connect' method */ 844 /* non-localhost, try 'connect' method */
853 conn = GNUNET_CLIENT_connect (service, cfg); 845 conn = GNUNET_CLIENT_connect (service, cfg);
854 if (conn == NULL) 846 if (conn == NULL)
855 { 847 {
856 LOG (GNUNET_ERROR_TYPE_INFO, 848 LOG (GNUNET_ERROR_TYPE_INFO,
857 _("Could not connect to service `%s', must not be running.\n"), 849 _("Could not connect to service `%s', must not be running.\n"),
858 service); 850 service);
859 service_test_error (task, task_cls); 851 service_test_error (task, task_cls);
860 return; 852 return;
861 } 853 }
862 conn->test_cb = task; 854 conn->test_cb = task;
863 conn->test_cb_cls = task_cls; 855 conn->test_cb_cls = task_cls;
864 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout); 856 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
865 857
866 if (NULL == 858 if (NULL ==
867 GNUNET_CLIENT_notify_transmit_ready (conn, 859 GNUNET_CLIENT_notify_transmit_ready (conn,
868 sizeof (struct 860 sizeof (struct GNUNET_MessageHeader),
869 GNUNET_MessageHeader), 861 timeout, GNUNET_YES, &write_test,
870 timeout, GNUNET_YES, &write_test, 862 conn))
871 conn)) 863 {
872 { 864 LOG (GNUNET_ERROR_TYPE_WARNING,
873 LOG (GNUNET_ERROR_TYPE_WARNING, 865 _("Failure to transmit request to service `%s'\n"), service);
874 _("Failure to transmit request to service `%s'\n"), service); 866 service_test_error (task, task_cls);
875 service_test_error (task, task_cls); 867 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
876 GNUNET_CLIENT_disconnect (conn, GNUNET_NO); 868 return;
877 return; 869 }
878 }
879} 870}
880 871
881 872
@@ -889,7 +880,8 @@ GNUNET_CLIENT_service_test (const char *service,
889 * @param buf where to write them 880 * @param buf where to write them
890 * @return number of bytes written to buf 881 * @return number of bytes written to buf
891 */ 882 */
892static size_t client_notify (void *cls, size_t size, void *buf); 883static size_t
884client_notify (void *cls, size_t size, void *buf);
893 885
894 886
895/** 887/**
@@ -900,58 +892,57 @@ static size_t client_notify (void *cls, size_t size, void *buf);
900 * @param tc unused 892 * @param tc unused
901 */ 893 */
902static void 894static void
903client_delayed_retry (void *cls, 895client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
904 const struct GNUNET_SCHEDULER_TaskContext *tc)
905{ 896{
906 struct GNUNET_CLIENT_TransmitHandle *th = cls; 897 struct GNUNET_CLIENT_TransmitHandle *th = cls;
907 struct GNUNET_TIME_Relative delay; 898 struct GNUNET_TIME_Relative delay;
908 899
909 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 900 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
910 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 901 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
911 { 902 {
912#if DEBUG_CLIENT 903#if DEBUG_CLIENT
913 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n"); 904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n");
914#endif 905#endif
915 th->sock->th = NULL; 906 th->sock->th = NULL;
916 th->notify (th->notify_cls, 0, NULL); 907 th->notify (th->notify_cls, 0, NULL);
917 GNUNET_free (th); 908 GNUNET_free (th);
918 return; 909 return;
919 } 910 }
920 th->sock->sock = 911 th->sock->sock =
921 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++); 912 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++);
922 if (NULL == th->sock->sock) 913 if (NULL == th->sock->sock)
923 { 914 {
924 /* could happen if we're out of sockets */ 915 /* could happen if we're out of sockets */
925 delay = 916 delay =
926 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining 917 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
927 (th->timeout), th->sock->back_off); 918 (th->timeout), th->sock->back_off);
928 th->sock->back_off = 919 th->sock->back_off =
929 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply 920 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
930 (th->sock->back_off, 2), 921 (th->sock->back_off, 2),
931 GNUNET_TIME_UNIT_SECONDS); 922 GNUNET_TIME_UNIT_SECONDS);
932#if DEBUG_CLIENT 923#if DEBUG_CLIENT
933 LOG (GNUNET_ERROR_TYPE_DEBUG, 924 LOG (GNUNET_ERROR_TYPE_DEBUG,
934 "Transmission failed %u times, trying again in %llums.\n", 925 "Transmission failed %u times, trying again in %llums.\n",
935 MAX_ATTEMPTS - th->attempts_left, 926 MAX_ATTEMPTS - th->attempts_left,
936 (unsigned long long) delay.rel_value); 927 (unsigned long long) delay.rel_value);
937#endif 928#endif
938 th->reconnect_task = 929 th->reconnect_task =
939 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); 930 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
940 return; 931 return;
941 } 932 }
942 th->th = 933 th->th =
943 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size, 934 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size,
944 GNUNET_TIME_absolute_get_remaining 935 GNUNET_TIME_absolute_get_remaining
945 (th->timeout), &client_notify, 936 (th->timeout), &client_notify,
946 th); 937 th);
947 if (th->th == NULL) 938 if (th->th == NULL)
948 { 939 {
949 GNUNET_break (0); 940 GNUNET_break (0);
950 th->sock->th = NULL; 941 th->sock->th = NULL;
951 th->notify (th->notify_cls, 0, NULL); 942 th->notify (th->notify_cls, 0, NULL);
952 GNUNET_free (th); 943 GNUNET_free (th);
953 return; 944 return;
954 } 945 }
955} 946}
956 947
957 948
@@ -974,47 +965,47 @@ client_notify (void *cls, size_t size, void *buf)
974 th->th = NULL; 965 th->th = NULL;
975 th->sock->th = NULL; 966 th->sock->th = NULL;
976 if (buf == NULL) 967 if (buf == NULL)
968 {
969 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
970 delay.rel_value /= 2;
971 if ((0 !=
972 (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
973 (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
974 (delay.rel_value < 1))
977 { 975 {
978 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
979 delay.rel_value /= 2;
980 if ((0 !=
981 (GNUNET_SCHEDULER_REASON_SHUTDOWN &
982 GNUNET_SCHEDULER_get_reason ())) || (GNUNET_YES != th->auto_retry)
983 || (0 == --th->attempts_left) || (delay.rel_value < 1))
984 {
985#if DEBUG_CLIENT 976#if DEBUG_CLIENT
986 LOG (GNUNET_ERROR_TYPE_DEBUG, 977 LOG (GNUNET_ERROR_TYPE_DEBUG,
987 "Transmission failed %u times, giving up.\n", 978 "Transmission failed %u times, giving up.\n",
988 MAX_ATTEMPTS - th->attempts_left); 979 MAX_ATTEMPTS - th->attempts_left);
989#endif 980#endif
990 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL)); 981 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
991 GNUNET_free (th); 982 GNUNET_free (th);
992 return 0; 983 return 0;
993 } 984 }
994 /* auto-retry */ 985 /* auto-retry */
995#if DEBUG_CLIENT 986#if DEBUG_CLIENT
996 LOG (GNUNET_ERROR_TYPE_DEBUG, 987 LOG (GNUNET_ERROR_TYPE_DEBUG,
997 "Failed to connect to `%s', automatically trying again.\n", 988 "Failed to connect to `%s', automatically trying again.\n",
998 th->sock->service_name); 989 th->sock->service_name);
999#endif 990#endif
1000 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO); 991 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
1001 th->sock->sock = NULL; 992 th->sock->sock = NULL;
1002 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off); 993 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
1003 th->sock->back_off = 994 th->sock->back_off =
1004 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply 995 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1005 (th->sock->back_off, 2), 996 (th->sock->back_off, 2),
1006 GNUNET_TIME_UNIT_SECONDS); 997 GNUNET_TIME_UNIT_SECONDS);
1007#if DEBUG_CLIENT 998#if DEBUG_CLIENT
1008 LOG (GNUNET_ERROR_TYPE_DEBUG, 999 LOG (GNUNET_ERROR_TYPE_DEBUG,
1009 "Transmission failed %u times, trying again in %llums.\n", 1000 "Transmission failed %u times, trying again in %llums.\n",
1010 MAX_ATTEMPTS - th->attempts_left, 1001 MAX_ATTEMPTS - th->attempts_left,
1011 (unsigned long long) delay.rel_value); 1002 (unsigned long long) delay.rel_value);
1012#endif 1003#endif
1013 th->sock->th = th; 1004 th->sock->th = th;
1014 th->reconnect_task = 1005 th->reconnect_task =
1015 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); 1006 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1016 return 0; 1007 return 0;
1017 } 1008 }
1018 GNUNET_assert (size >= th->size); 1009 GNUNET_assert (size >= th->size);
1019 ret = th->notify (th->notify_cls, size, buf); 1010 ret = th->notify (th->notify_cls, size, buf);
1020 GNUNET_free (th); 1011 GNUNET_free (th);
@@ -1043,21 +1034,21 @@ client_notify (void *cls, size_t size, void *buf)
1043 */ 1034 */
1044struct GNUNET_CLIENT_TransmitHandle * 1035struct GNUNET_CLIENT_TransmitHandle *
1045GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, 1036GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1046 size_t size, 1037 size_t size,
1047 struct GNUNET_TIME_Relative timeout, 1038 struct GNUNET_TIME_Relative timeout,
1048 int auto_retry, 1039 int auto_retry,
1049 GNUNET_CONNECTION_TransmitReadyNotify 1040 GNUNET_CONNECTION_TransmitReadyNotify
1050 notify, void *notify_cls) 1041 notify, void *notify_cls)
1051{ 1042{
1052 struct GNUNET_CLIENT_TransmitHandle *th; 1043 struct GNUNET_CLIENT_TransmitHandle *th;
1053 1044
1054 if (NULL != sock->th) 1045 if (NULL != sock->th)
1055 { 1046 {
1056 /* If this breaks, you most likley called this function twice without waiting 1047 /* If this breaks, you most likley called this function twice without waiting
1057 * for completion or canceling the request */ 1048 * for completion or canceling the request */
1058 GNUNET_break (0); 1049 GNUNET_break (0);
1059 return NULL; 1050 return NULL;
1060 } 1051 }
1061 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle)); 1052 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1062 th->sock = sock; 1053 th->sock = sock;
1063 th->size = size; 1054 th->size = size;
@@ -1068,25 +1059,25 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1068 th->attempts_left = MAX_ATTEMPTS; 1059 th->attempts_left = MAX_ATTEMPTS;
1069 sock->th = th; 1060 sock->th = th;
1070 if (sock->sock == NULL) 1061 if (sock->sock == NULL)
1071 { 1062 {
1072 th->reconnect_task = 1063 th->reconnect_task =
1073 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry, 1064 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry,
1074 th); 1065 th);
1075 1066
1076 } 1067 }
1077 else 1068 else
1069 {
1070 th->th =
1071 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout,
1072 &client_notify, th);
1073 if (NULL == th->th)
1078 { 1074 {
1079 th->th = 1075 GNUNET_break (0);
1080 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout, 1076 GNUNET_free (th);
1081 &client_notify, th); 1077 sock->th = NULL;
1082 if (NULL == th->th) 1078 return NULL;
1083 {
1084 GNUNET_break (0);
1085 GNUNET_free (th);
1086 sock->th = NULL;
1087 return NULL;
1088 }
1089 } 1079 }
1080 }
1090 return th; 1081 return th;
1091} 1082}
1092 1083
@@ -1097,20 +1088,20 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1097 * @param th handle from the original request. 1088 * @param th handle from the original request.
1098 */ 1089 */
1099void 1090void
1100GNUNET_CLIENT_notify_transmit_ready_cancel (struct 1091GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1101 GNUNET_CLIENT_TransmitHandle *th) 1092 *th)
1102{ 1093{
1103 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK) 1094 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1104 { 1095 {
1105 GNUNET_assert (NULL == th->th); 1096 GNUNET_assert (NULL == th->th);
1106 GNUNET_SCHEDULER_cancel (th->reconnect_task); 1097 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1107 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 1098 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1108 } 1099 }
1109 else 1100 else
1110 { 1101 {
1111 GNUNET_assert (NULL != th->th); 1102 GNUNET_assert (NULL != th->th);
1112 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th); 1103 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1113 } 1104 }
1114 th->sock->th = NULL; 1105 th->sock->th = NULL;
1115 GNUNET_free (th); 1106 GNUNET_free (th);
1116} 1107}
@@ -1136,21 +1127,20 @@ transmit_for_response (void *cls, size_t size, void *buf)
1136 tc->sock->tag = NULL; 1127 tc->sock->tag = NULL;
1137 msize = ntohs (tc->hdr->size); 1128 msize = ntohs (tc->hdr->size);
1138 if (NULL == buf) 1129 if (NULL == buf)
1139 { 1130 {
1140#if DEBUG_CLIENT 1131#if DEBUG_CLIENT
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, 1132 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 _ 1133 _("Could not submit request, not expecting to receive a response.\n"));
1143 ("Could not submit request, not expecting to receive a response.\n"));
1144#endif 1134#endif
1145 if (NULL != tc->rn) 1135 if (NULL != tc->rn)
1146 tc->rn (tc->rn_cls, NULL); 1136 tc->rn (tc->rn_cls, NULL);
1147 GNUNET_free (tc); 1137 GNUNET_free (tc);
1148 return 0; 1138 return 0;
1149 } 1139 }
1150 GNUNET_assert (size >= msize); 1140 GNUNET_assert (size >= msize);
1151 memcpy (buf, tc->hdr, msize); 1141 memcpy (buf, tc->hdr, msize);
1152 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls, 1142 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls,
1153 GNUNET_TIME_absolute_get_remaining (tc->timeout)); 1143 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1154 GNUNET_free (tc); 1144 GNUNET_free (tc);
1155 return msize; 1145 return msize;
1156} 1146}
@@ -1178,14 +1168,12 @@ transmit_for_response (void *cls, size_t size, void *buf)
1178 * is already pending 1168 * is already pending
1179 */ 1169 */
1180int 1170int
1181GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection 1171GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
1182 *sock, 1172 const struct GNUNET_MessageHeader *hdr,
1183 const struct GNUNET_MessageHeader 1173 struct GNUNET_TIME_Relative timeout,
1184 *hdr, 1174 int auto_retry,
1185 struct GNUNET_TIME_Relative timeout, 1175 GNUNET_CLIENT_MessageHandler rn,
1186 int auto_retry, 1176 void *rn_cls)
1187 GNUNET_CLIENT_MessageHandler rn,
1188 void *rn_cls)
1189{ 1177{
1190 struct TransmitGetResponseContext *tc; 1178 struct TransmitGetResponseContext *tc;
1191 uint16_t msize; 1179 uint16_t msize;
@@ -1203,12 +1191,12 @@ GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1203 tc->rn_cls = rn_cls; 1191 tc->rn_cls = rn_cls;
1204 if (NULL == 1192 if (NULL ==
1205 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry, 1193 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry,
1206 &transmit_for_response, tc)) 1194 &transmit_for_response, tc))
1207 { 1195 {
1208 GNUNET_break (0); 1196 GNUNET_break (0);
1209 GNUNET_free (tc); 1197 GNUNET_free (tc);
1210 return GNUNET_SYSERR; 1198 return GNUNET_SYSERR;
1211 } 1199 }
1212 sock->tag = tc; 1200 sock->tag = tc;
1213 return GNUNET_OK; 1201 return GNUNET_OK;
1214} 1202}
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c
index 31da5829b..340ad5334 100644
--- a/src/util/common_allocation.c
+++ b/src/util/common_allocation.c
@@ -65,10 +65,10 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
65 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); 65 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
66 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber); 66 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
67 if (ret == NULL) 67 if (ret == NULL)
68 { 68 {
69 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc"); 69 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
70 abort (); 70 abort ();
71 } 71 }
72 return ret; 72 return ret;
73} 73}
74 74
@@ -86,7 +86,7 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
86 */ 86 */
87void * 87void *
88GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, 88GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
89 int linenumber) 89 int linenumber)
90{ 90{
91 void *ret; 91 void *ret;
92 92
@@ -101,10 +101,10 @@ GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
101 GNUNET_assert_at (size < INT_MAX, filename, linenumber); 101 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
102 ret = malloc (size); 102 ret = malloc (size);
103 if (ret == NULL) 103 if (ret == NULL)
104 { 104 {
105 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc"); 105 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
106 abort (); 106 abort ();
107 } 107 }
108#ifdef W32_MEM_LIMIT 108#ifdef W32_MEM_LIMIT
109 *((size_t *) ret) = size; 109 *((size_t *) ret) = size;
110 ret = &((size_t *) ret)[1]; 110 ret = &((size_t *) ret)[1];
@@ -172,10 +172,10 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
172#endif 172#endif
173 ptr = realloc (ptr, n); 173 ptr = realloc (ptr, n);
174 if ((NULL == ptr) && (n > 0)) 174 if ((NULL == ptr) && (n > 0))
175 { 175 {
176 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc"); 176 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
177 abort (); 177 abort ();
178 } 178 }
179#ifdef W32_MEM_LIMIT 179#ifdef W32_MEM_LIMIT
180 ptr = &((size_t *) ptr)[1]; 180 ptr = &((size_t *) ptr)[1];
181#endif 181#endif
@@ -233,7 +233,7 @@ GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
233 */ 233 */
234char * 234char *
235GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, 235GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
236 int linenumber) 236 int linenumber)
237{ 237{
238 char *res; 238 char *res;
239 239
@@ -260,7 +260,7 @@ GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
260 */ 260 */
261void 261void
262GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, 262GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
263 unsigned int newCount, const char *filename, int linenumber) 263 unsigned int newCount, const char *filename, int linenumber)
264{ 264{
265 void *tmp; 265 void *tmp;
266 size_t size; 266 size_t size;
@@ -268,22 +268,22 @@ GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
268 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber); 268 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
269 size = newCount * elementSize; 269 size = newCount * elementSize;
270 if (size == 0) 270 if (size == 0)
271 { 271 {
272 tmp = NULL; 272 tmp = NULL;
273 } 273 }
274 else 274 else
275 { 275 {
276 tmp = GNUNET_xmalloc_ (size, filename, linenumber); 276 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
277 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... */
278 if (*oldCount > newCount) 278 if (*oldCount > newCount)
279 *oldCount = newCount; /* shrink is also allowed! */ 279 *oldCount = newCount; /* shrink is also allowed! */
280 memcpy (tmp, *old, elementSize * (*oldCount)); 280 memcpy (tmp, *old, elementSize * (*oldCount));
281 } 281 }
282 282
283 if (*old != NULL) 283 if (*old != NULL)
284 { 284 {
285 GNUNET_xfree_ (*old, filename, linenumber); 285 GNUNET_xfree_ (*old, filename, linenumber);
286 } 286 }
287 *old = tmp; 287 *old = tmp;
288 *oldCount = newCount; 288 *oldCount = newCount;
289} 289}
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index 1d3843cfd..0b7d06bab 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -276,22 +276,21 @@ resize_logdefs ()
276 */ 276 */
277static void 277static void
278add_definition (char *component, char *file, char *function, int from_line, 278add_definition (char *component, char *file, char *function, int from_line,
279 int to_line, int level, int force) 279 int to_line, int level, int force)
280{ 280{
281 if (logdefs_size == logdefs_len) 281 if (logdefs_size == logdefs_len)
282 resize_logdefs (); 282 resize_logdefs ();
283 struct LogDef n; 283 struct LogDef n;
284
284 memset (&n, 0, sizeof (n)); 285 memset (&n, 0, sizeof (n));
285 if (strlen (component) > 0 && component[0] != '*') 286 if (strlen (component) > 0 && component[0] != '*')
286 n.component = GNUNET_strdup (component); 287 n.component = GNUNET_strdup (component);
287 if (strlen (file) > 0 && file[0] != '*') 288 if (strlen (file) > 0 && file[0] != '*')
288 { 289 {
289 n.file = GNUNET_strdup (file); 290 n.file = GNUNET_strdup (file);
290 n.strlen_file = strlen (file); 291 n.strlen_file = strlen (file);
291 } 292 }
292 if ( (NULL != function) && 293 if ((NULL != function) && (strlen (function) > 0) && (function[0] != '*'))
293 (strlen (function) > 0) &&
294 (function[0] != '*') )
295 n.function = GNUNET_strdup (function); 294 n.function = GNUNET_strdup (function);
296 n.from_line = from_line; 295 n.from_line = from_line;
297 n.to_line = to_line; 296 n.to_line = to_line;
@@ -316,7 +315,7 @@ add_definition (char *component, char *file, char *function, int from_line,
316 */ 315 */
317int 316int
318GNUNET_get_log_call_status (int caller_level, const char *comp, 317GNUNET_get_log_call_status (int caller_level, const char *comp,
319 const char *file, const char *function, int line) 318 const char *file, const char *function, int line)
320{ 319{
321 struct LogDef *ld; 320 struct LogDef *ld;
322 int i; 321 int i;
@@ -337,20 +336,22 @@ GNUNET_get_log_call_status (int caller_level, const char *comp,
337 force_only = min_level >= 0; 336 force_only = min_level >= 0;
338 strlen_file = strlen (file); 337 strlen_file = strlen (file);
339 for (i = 0; i < logdefs_len; i++) 338 for (i = 0; i < logdefs_len; i++)
339 {
340 ld = &logdefs[i];
341 if ((!force_only || ld->force) &&
342 (line >= ld->from_line && line <= ld->to_line) && (ld->component == NULL
343 || strcmp (comp,
344 ld->component)
345 == 0) &&
346 (ld->file == NULL ||
347 (ld->strlen_file <= strlen_file &&
348 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
349 (ld->function == NULL || strcmp (function, ld->function) == 0))
340 { 350 {
341 ld = &logdefs[i]; 351 /* We're finished */
342 if ((!force_only || ld->force) && 352 return caller_level <= ld->level;
343 (line >= ld->from_line && line <= ld->to_line) &&
344 (ld->component == NULL || strcmp (comp, ld->component) == 0) &&
345 (ld->file == NULL ||
346 (ld->strlen_file <= strlen_file &&
347 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
348 (ld->function == NULL || strcmp (function, ld->function) == 0))
349 {
350 /* We're finished */
351 return caller_level <= ld->level;
352 }
353 } 353 }
354 }
354 /* No matches - use global level, if defined */ 355 /* No matches - use global level, if defined */
355 if (min_level >= 0) 356 if (min_level >= 0)
356 return caller_level <= min_level; 357 return caller_level <= min_level;
@@ -400,6 +401,7 @@ parse_definitions (const char *constname, int force)
400 int from_line, to_line; 401 int from_line, to_line;
401 int counter = 0; 402 int counter = 0;
402 int keep_looking = 1; 403 int keep_looking = 1;
404
403 tmp = getenv (constname); 405 tmp = getenv (constname);
404 if (tmp == NULL) 406 if (tmp == NULL)
405 return 0; 407 return 0;
@@ -408,85 +410,84 @@ parse_definitions (const char *constname, int force)
408 from_line = 0; 410 from_line = 0;
409 to_line = INT_MAX; 411 to_line = INT_MAX;
410 for (p = def, state = 0, start = def; keep_looking; p++) 412 for (p = def, state = 0, start = def; keep_looking; p++)
413 {
414 switch (p[0])
411 { 415 {
412 switch (p[0]) 416 case ';': /* found a field separator */
413 { 417 p[0] = '\0';
414 case ';': /* found a field separator */ 418 switch (state)
415 p[0] = '\0'; 419 {
416 switch (state) 420 case 0: /* within a component name */
417 { 421 comp = start;
418 case 0: /* within a component name */ 422 break;
419 comp = start; 423 case 1: /* within a file name */
420 break; 424 file = start;
421 case 1: /* within a file name */ 425 break;
422 file = start; 426 case 2: /* within a function name */
423 break; 427 /* after a file name there must be a function name */
424 case 2: /* within a function name */ 428 function = start;
425 /* after a file name there must be a function name */ 429 break;
426 function = start; 430 case 3: /* within a from-to line range */
427 break; 431 if (strlen (start) > 0)
428 case 3: /* within a from-to line range */ 432 {
429 if (strlen (start) > 0) 433 errno = 0;
430 { 434 from_line = strtol (start, &t, 10);
431 errno = 0; 435 if (errno != 0 || from_line < 0)
432 from_line = strtol (start, &t, 10); 436 {
433 if (errno != 0 || from_line < 0) 437 free (def);
434 { 438 return counter;
435 free (def); 439 }
436 return counter; 440 if (t < p && t[0] == '-')
437 } 441 {
438 if (t < p && t[0] == '-') 442 errno = 0;
439 { 443 start = t + 1;
440 errno = 0; 444 to_line = strtol (start, &t, 10);
441 start = t + 1; 445 if (errno != 0 || to_line < 0 || t != p)
442 to_line = strtol (start, &t, 10); 446 {
443 if (errno != 0 || to_line < 0 || t != p) 447 free (def);
444 { 448 return counter;
445 free (def); 449 }
446 return counter; 450 }
447 } 451 else /* one number means "match this line only" */
448 } 452 to_line = from_line;
449 else /* one number means "match this line only" */ 453 }
450 to_line = from_line; 454 else /* default to 0-max */
451 } 455 {
452 else /* default to 0-max */ 456 from_line = 0;
453 { 457 to_line = INT_MAX;
454 from_line = 0; 458 }
455 to_line = INT_MAX; 459 break;
456 } 460 }
457 break; 461 start = p + 1;
458 } 462 state += 1;
459 start = p + 1; 463 break;
460 state += 1; 464 case '\0': /* found EOL */
461 break; 465 keep_looking = 0;
462 case '\0': /* found EOL */ 466 /* fall through to '/' */
463 keep_looking = 0; 467 case '/': /* found a definition separator */
464 /* fall through to '/' */ 468 switch (state)
465 case '/': /* found a definition separator */ 469 {
466 switch (state) 470 case 4: /* within a log level */
467 { 471 p[0] = '\0';
468 case 4: /* within a log level */ 472 state = 0;
469 p[0] = '\0'; 473 level = get_type ((const char *) start);
470 state = 0; 474 if (level == GNUNET_ERROR_TYPE_INVALID ||
471 level = get_type ((const char *) start); 475 level == GNUNET_ERROR_TYPE_UNSPECIFIED)
472 if (level == GNUNET_ERROR_TYPE_INVALID 476 {
473 || level == GNUNET_ERROR_TYPE_UNSPECIFIED) 477 free (def);
474 { 478 return counter;
475 free (def); 479 }
476 return counter; 480 add_definition (comp, file, function, from_line, to_line, level, force);
477 } 481 counter += 1;
478 add_definition (comp, file, function, from_line, to_line, level, 482 start = p + 1;
479 force); 483 break;
480 counter += 1; 484 default:
481 start = p + 1; 485 break;
482 break; 486 }
483 default: 487 default:
484 break; 488 break;
485 }
486 default:
487 break;
488 }
489 } 489 }
490 }
490 free (def); 491 free (def);
491 return counter; 492 return counter;
492} 493}
@@ -502,7 +503,7 @@ parse_all_definitions ()
502 gnunet_log_parsed = GNUNET_YES; 503 gnunet_log_parsed = GNUNET_YES;
503 if (gnunet_force_log_parsed == GNUNET_NO) 504 if (gnunet_force_log_parsed == GNUNET_NO)
504 gnunet_force_log_present = 505 gnunet_force_log_present =
505 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; 506 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
506 gnunet_force_log_parsed = GNUNET_YES; 507 gnunet_force_log_parsed = GNUNET_YES;
507} 508}
508#endif 509#endif
@@ -546,16 +547,15 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
546 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); 547 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
547 altlog = FOPEN (fn, "a"); 548 altlog = FOPEN (fn, "a");
548 if (altlog == NULL) 549 if (altlog == NULL)
549 { 550 {
550 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); 551 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
551 if (dirwarn) 552 if (dirwarn)
552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553 _ 554 _("Failed to create or access directory for log file `%s'\n"),
554 ("Failed to create or access directory for log file `%s'\n"), 555 fn);
555 fn); 556 GNUNET_free (fn);
556 GNUNET_free (fn); 557 return GNUNET_SYSERR;
557 return GNUNET_SYSERR; 558 }
558 }
559 GNUNET_free (fn); 559 GNUNET_free (fn);
560 if (GNUNET_stderr != NULL) 560 if (GNUNET_stderr != NULL)
561 fclose (GNUNET_stderr); 561 fclose (GNUNET_stderr);
@@ -596,11 +596,11 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
596 prev = NULL; 596 prev = NULL;
597 pos = loggers; 597 pos = loggers;
598 while ((pos != NULL) && 598 while ((pos != NULL) &&
599 ((pos->logger != logger) || (pos->logger_cls != logger_cls))) 599 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
600 { 600 {
601 prev = pos; 601 prev = pos;
602 pos = pos->next; 602 pos = pos->next;
603 } 603 }
604 GNUNET_assert (pos != NULL); 604 GNUNET_assert (pos != NULL);
605 if (prev == NULL) 605 if (prev == NULL)
606 loggers = pos->next; 606 loggers = pos->next;
@@ -620,22 +620,22 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
620 */ 620 */
621static void 621static void
622output_message (enum GNUNET_ErrorType kind, const char *comp, 622output_message (enum GNUNET_ErrorType kind, const char *comp,
623 const char *datestr, const char *msg) 623 const char *datestr, const char *msg)
624{ 624{
625 struct CustomLogger *pos; 625 struct CustomLogger *pos;
626 626
627 if (GNUNET_stderr != NULL) 627 if (GNUNET_stderr != NULL)
628 { 628 {
629 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, 629 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
630 GNUNET_error_type_to_string (kind), msg); 630 GNUNET_error_type_to_string (kind), msg);
631 fflush (GNUNET_stderr); 631 fflush (GNUNET_stderr);
632 } 632 }
633 pos = loggers; 633 pos = loggers;
634 while (pos != NULL) 634 while (pos != NULL)
635 { 635 {
636 pos->logger (pos->logger_cls, kind, comp, datestr, msg); 636 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
637 pos = pos->next; 637 pos = pos->next;
638 } 638 }
639} 639}
640 640
641 641
@@ -661,16 +661,15 @@ flush_bulk (const char *datestr)
661 else if (last != last_bulk) 661 else if (last != last_bulk)
662 last--; 662 last--;
663 if (last[0] == '\n') 663 if (last[0] == '\n')
664 { 664 {
665 rev = 1; 665 rev = 1;
666 last[0] = '\0'; 666 last[0] = '\0';
667 } 667 }
668 ft = 668 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
669 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration 669 (last_bulk_time));
670 (last_bulk_time));
671 snprintf (msg, sizeof (msg), 670 snprintf (msg, sizeof (msg),
672 _("Message `%.*s' repeated %u times in the last %s\n"), 671 _("Message `%.*s' repeated %u times in the last %s\n"),
673 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); 672 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
674 GNUNET_free (ft); 673 GNUNET_free (ft);
675 if (rev == 1) 674 if (rev == 1)
676 last[0] = '\n'; 675 last[0] = '\n';
@@ -690,14 +689,14 @@ void
690GNUNET_log_skip (unsigned int n, int check_reset) 689GNUNET_log_skip (unsigned int n, int check_reset)
691{ 690{
692 if (n == 0) 691 if (n == 0)
693 { 692 {
694 int ok; 693 int ok;
695 694
696 ok = (0 == skip_log); 695 ok = (0 == skip_log);
697 skip_log = 0; 696 skip_log = 0;
698 if (check_reset) 697 if (check_reset)
699 GNUNET_assert (ok); 698 GNUNET_assert (ok);
700 } 699 }
701 else 700 else
702 skip_log += n; 701 skip_log += n;
703} 702}
@@ -736,34 +735,33 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message,
736 tmptr = localtime (&timetmp); 735 tmptr = localtime (&timetmp);
737 gettimeofday (&timeofday, NULL); 736 gettimeofday (&timeofday, NULL);
738 if (NULL != tmptr) 737 if (NULL != tmptr)
739 { 738 {
740#ifdef WINDOWS 739#ifdef WINDOWS
741 LARGE_INTEGER pc; 740 LARGE_INTEGER pc;
742 741
743 pc.QuadPart = 0; 742 pc.QuadPart = 0;
744 QueryPerformanceCounter (&pc); 743 QueryPerformanceCounter (&pc);
745 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); 744 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
746 snprintf (date, sizeof (date), date2, 745 snprintf (date, sizeof (date), date2,
747 (long long) (pc.QuadPart / 746 (long long) (pc.QuadPart /
748 (performance_frequency.QuadPart / 1000))); 747 (performance_frequency.QuadPart / 1000)));
749#else 748#else
750 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); 749 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
751 snprintf (date, sizeof (date), date2, timeofday.tv_usec); 750 snprintf (date, sizeof (date), date2, timeofday.tv_usec);
752#endif 751#endif
753 } 752 }
754 else 753 else
755 strcpy (date, "localtime error"); 754 strcpy (date, "localtime error");
756 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) 755 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
757 && (last_bulk_time.abs_value != 0) 756 (last_bulk_time.abs_value != 0) &&
758 && (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) 757 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
759 { 758 {
760 last_bulk_repeat++; 759 last_bulk_repeat++;
761 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > 760 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value >
762 BULK_DELAY_THRESHOLD) 761 BULK_DELAY_THRESHOLD) || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
763 || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) 762 flush_bulk (date);
764 flush_bulk (date); 763 return;
765 return; 764 }
766 }
767 flush_bulk (date); 765 flush_bulk (date);
768 strncpy (last_bulk, buf, sizeof (last_bulk)); 766 strncpy (last_bulk, buf, sizeof (last_bulk));
769 last_bulk_repeat = 0; 767 last_bulk_repeat = 0;
@@ -804,7 +802,7 @@ GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
804 */ 802 */
805void 803void
806GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp, 804GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
807 const char *message, ...) 805 const char *message, ...)
808{ 806{
809 va_list va; 807 va_list va;
810 char comp_w_pid[128]; 808 char comp_w_pid[128];
@@ -922,44 +920,44 @@ GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
922 if (addr == NULL) 920 if (addr == NULL)
923 return _("unknown address"); 921 return _("unknown address");
924 switch (addr->sa_family) 922 switch (addr->sa_family)
925 { 923 {
926 case AF_INET: 924 case AF_INET:
927 if (addrlen != sizeof (struct sockaddr_in)) 925 if (addrlen != sizeof (struct sockaddr_in))
928 return "<invalid v4 address>"; 926 return "<invalid v4 address>";
929 v4 = (const struct sockaddr_in *) addr; 927 v4 = (const struct sockaddr_in *) addr;
930 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); 928 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
931 if (0 == ntohs (v4->sin_port)) 929 if (0 == ntohs (v4->sin_port))
932 return buf;
933 strcat (buf, ":");
934 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
935 strcat (buf, b2);
936 return buf;
937 case AF_INET6:
938 if (addrlen != sizeof (struct sockaddr_in6))
939 return "<invalid v4 address>";
940 v6 = (const struct sockaddr_in6 *) addr;
941 buf[0] = '[';
942 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
943 if (0 == ntohs (v6->sin6_port))
944 return &buf[1];
945 strcat (buf, "]:");
946 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
947 strcat (buf, b2);
948 return buf;
949 case AF_UNIX:
950 if (addrlen <= sizeof (sa_family_t))
951 return "<unbound UNIX client>";
952 un = (const struct sockaddr_un *) addr;
953 off = 0;
954 if (un->sun_path[0] == '\0')
955 off++;
956 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
957 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
958 &un->sun_path[off]);
959 return buf; 930 return buf;
960 default: 931 strcat (buf, ":");
961 return _("invalid address"); 932 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
962 } 933 strcat (buf, b2);
934 return buf;
935 case AF_INET6:
936 if (addrlen != sizeof (struct sockaddr_in6))
937 return "<invalid v4 address>";
938 v6 = (const struct sockaddr_in6 *) addr;
939 buf[0] = '[';
940 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
941 if (0 == ntohs (v6->sin6_port))
942 return &buf[1];
943 strcat (buf, "]:");
944 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
945 strcat (buf, b2);
946 return buf;
947 case AF_UNIX:
948 if (addrlen <= sizeof (sa_family_t))
949 return "<unbound UNIX client>";
950 un = (const struct sockaddr_un *) addr;
951 off = 0;
952 if (un->sun_path[0] == '\0')
953 off++;
954 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
955 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
956 &un->sun_path[off]);
957 return buf;
958 default:
959 return _("invalid address");
960 }
963} 961}
964 962
965 963
diff --git a/src/util/configuration.c b/src/util/configuration.c
index 9a131fb50..41a4e1975 100644
--- a/src/util/configuration.c
+++ b/src/util/configuration.c
@@ -150,7 +150,7 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
150 */ 150 */
151int 151int
152GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, 152GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
153 const char *filename) 153 const char *filename)
154{ 154{
155 int dirty; 155 int dirty;
156 char line[256]; 156 char line[256];
@@ -167,89 +167,87 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
167 fn = GNUNET_STRINGS_filename_expand (filename); 167 fn = GNUNET_STRINGS_filename_expand (filename);
168 if (fn == NULL) 168 if (fn == NULL)
169 return GNUNET_SYSERR; 169 return GNUNET_SYSERR;
170 dirty = cfg->dirty; /* back up value! */ 170 dirty = cfg->dirty; /* back up value! */
171 if (NULL == (fp = FOPEN (fn, "r"))) 171 if (NULL == (fp = FOPEN (fn, "r")))
172 { 172 {
173 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); 173 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
174 GNUNET_free (fn); 174 GNUNET_free (fn);
175 return GNUNET_SYSERR; 175 return GNUNET_SYSERR;
176 } 176 }
177 GNUNET_free (fn); 177 GNUNET_free (fn);
178 ret = GNUNET_OK; 178 ret = GNUNET_OK;
179 section = GNUNET_strdup (""); 179 section = GNUNET_strdup ("");
180 memset (line, 0, 256); 180 memset (line, 0, 256);
181 nr = 0; 181 nr = 0;
182 while (NULL != fgets (line, 255, fp)) 182 while (NULL != fgets (line, 255, fp))
183 {
184 nr++;
185 for (i = 0; i < 255; i++)
186 if (line[i] == '\t')
187 line[i] = ' ';
188 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r')
189 continue;
190 emptyline = 1;
191 for (i = 0; (i < 255 && line[i] != 0); i++)
192 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
193 emptyline = 0;
194 if (emptyline == 1)
195 continue;
196 /* remove tailing whitespace */
197 for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
198 i--)
199 line[i] = '\0';
200 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
183 { 201 {
184 nr++; 202 /* @INLINE@ value */
185 for (i = 0; i < 255; i++) 203 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
186 if (line[i] == '\t') 204 ret = GNUNET_SYSERR; /* failed to parse included config */
187 line[i] = ' '; 205 }
188 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' 206 else if (1 == sscanf (line, "[%99[^]]]", value))
189 || line[0] == '\r') 207 {
190 continue; 208 /* [value] */
191 emptyline = 1; 209 GNUNET_free (section);
192 for (i = 0; (i < 255 && line[i] != 0); i++) 210 section = GNUNET_strdup (value);
193 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r') 211 }
194 emptyline = 0; 212 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
195 if (emptyline == 1) 213 {
196 continue; 214 /* tag = value */
197 /* remove tailing whitespace */ 215 /* Strip LF */
198 for (i = strlen (line) - 1; 216 i = strlen (value) - 1;
199 (i >= 0) && (isspace ((unsigned char) line[i])); i--) 217 while ((i >= 0) && (isspace ((unsigned char) value[i])))
200 line[i] = '\0'; 218 value[i--] = '\0';
201 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value)) 219 /* remove quotes */
202 { 220 i = 0;
203 /* @INLINE@ value */ 221 if (value[0] == '"')
204 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value)) 222 {
205 ret = GNUNET_SYSERR; /* failed to parse included config */ 223 i = 1;
206 } 224 while ((value[i] != '\0') && (value[i] != '"'))
207 else if (1 == sscanf (line, "[%99[^]]]", value)) 225 i++;
208 { 226 if (value[i] == '"')
209 /* [value] */ 227 {
210 GNUNET_free (section); 228 value[i] = '\0';
211 section = GNUNET_strdup (value); 229 i = 1;
212 } 230 }
213 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value)) 231 else
214 { 232 i = 0;
215 /* tag = value */ 233 }
216 /* Strip LF */ 234 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
217 i = strlen (value) - 1; 235 }
218 while ((i >= 0) && (isspace ((unsigned char) value[i]))) 236 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
219 value[i--] = '\0'; 237 {
220 /* remove quotes */ 238 /* tag = */
221 i = 0; 239 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
222 if (value[0] == '"') 240 }
223 { 241 else
224 i = 1; 242 {
225 while ((value[i] != '\0') && (value[i] != '"')) 243 /* parse error */
226 i++; 244 LOG (GNUNET_ERROR_TYPE_WARNING,
227 if (value[i] == '"') 245 _("Syntax error in configuration file `%s' at line %u.\n"), filename,
228 { 246 nr);
229 value[i] = '\0'; 247 ret = GNUNET_SYSERR;
230 i = 1; 248 break;
231 }
232 else
233 i = 0;
234 }
235 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag,
236 &value[i]);
237 }
238 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
239 {
240 /* tag = */
241 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
242 }
243 else
244 {
245 /* parse error */
246 LOG (GNUNET_ERROR_TYPE_WARNING,
247 _("Syntax error in configuration file `%s' at line %u.\n"),
248 filename, nr);
249 ret = GNUNET_SYSERR;
250 break;
251 }
252 } 249 }
250 }
253 GNUNET_assert (0 == fclose (fp)); 251 GNUNET_assert (0 == fclose (fp));
254 /* restore dirty flag - anything we set in the meantime 252 /* restore dirty flag - anything we set in the meantime
255 * came from disk */ 253 * came from disk */
@@ -282,7 +280,7 @@ GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
282 */ 280 */
283int 281int
284GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, 282GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
285 const char *filename) 283 const char *filename)
286{ 284{
287 struct ConfigSection *sec; 285 struct ConfigSection *sec;
288 struct ConfigEntry *ent; 286 struct ConfigEntry *ent;
@@ -296,67 +294,67 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
296 if (fn == NULL) 294 if (fn == NULL)
297 return GNUNET_SYSERR; 295 return GNUNET_SYSERR;
298 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)) 296 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
299 { 297 {
300 GNUNET_free (fn); 298 GNUNET_free (fn);
301 return GNUNET_SYSERR; 299 return GNUNET_SYSERR;
302 } 300 }
303 if (NULL == (fp = FOPEN (fn, "w"))) 301 if (NULL == (fp = FOPEN (fn, "w")))
304 { 302 {
305 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn); 303 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
306 GNUNET_free (fn); 304 GNUNET_free (fn);
307 return GNUNET_SYSERR; 305 return GNUNET_SYSERR;
308 } 306 }
309 GNUNET_free (fn); 307 GNUNET_free (fn);
310 error = 0; 308 error = 0;
311 sec = cfg->sections; 309 sec = cfg->sections;
312 while (sec != NULL) 310 while (sec != NULL)
311 {
312 if (0 > fprintf (fp, "[%s]\n", sec->name))
313 { 313 {
314 if (0 > fprintf (fp, "[%s]\n", sec->name)) 314 error = 1;
315 { 315 break;
316 error = 1;
317 break;
318 }
319 ent = sec->entries;
320 while (ent != NULL)
321 {
322 if (ent->val != NULL)
323 {
324 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
325 strcpy (val, ent->val);
326 while (NULL != (pos = strstr (val, "\n")))
327 {
328 memmove (&pos[2], &pos[1], strlen (&pos[1]));
329 pos[0] = '\\';
330 pos[1] = 'n';
331 }
332 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
333 {
334 error = 1;
335 GNUNET_free (val);
336 break;
337 }
338 GNUNET_free (val);
339 }
340 ent = ent->next;
341 }
342 if (error != 0)
343 break;
344 if (0 > fprintf (fp, "\n"))
345 {
346 error = 1;
347 break;
348 }
349 sec = sec->next;
350 } 316 }
317 ent = sec->entries;
318 while (ent != NULL)
319 {
320 if (ent->val != NULL)
321 {
322 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
323 strcpy (val, ent->val);
324 while (NULL != (pos = strstr (val, "\n")))
325 {
326 memmove (&pos[2], &pos[1], strlen (&pos[1]));
327 pos[0] = '\\';
328 pos[1] = 'n';
329 }
330 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
331 {
332 error = 1;
333 GNUNET_free (val);
334 break;
335 }
336 GNUNET_free (val);
337 }
338 ent = ent->next;
339 }
340 if (error != 0)
341 break;
342 if (0 > fprintf (fp, "\n"))
343 {
344 error = 1;
345 break;
346 }
347 sec = sec->next;
348 }
351 if (error != 0) 349 if (error != 0)
352 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename); 350 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
353 GNUNET_assert (0 == fclose (fp)); 351 GNUNET_assert (0 == fclose (fp));
354 if (error != 0) 352 if (error != 0)
355 { 353 {
356 cfg->dirty = GNUNET_SYSERR; /* last write failed */ 354 cfg->dirty = GNUNET_SYSERR; /* last write failed */
357 return GNUNET_SYSERR; 355 return GNUNET_SYSERR;
358 } 356 }
359 cfg->dirty = GNUNET_NO; /* last write succeeded */ 357 cfg->dirty = GNUNET_NO; /* last write succeeded */
360 return GNUNET_OK; 358 return GNUNET_OK;
361} 359}
362 360
@@ -370,23 +368,23 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
370 */ 368 */
371void 369void
372GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, 370GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
373 GNUNET_CONFIGURATION_Iterator iter, 371 GNUNET_CONFIGURATION_Iterator iter,
374 void *iter_cls) 372 void *iter_cls)
375{ 373{
376 struct ConfigSection *spos; 374 struct ConfigSection *spos;
377 struct ConfigEntry *epos; 375 struct ConfigEntry *epos;
378 376
379 spos = cfg->sections; 377 spos = cfg->sections;
380 while (spos != NULL) 378 while (spos != NULL)
379 {
380 epos = spos->entries;
381 while (epos != NULL)
381 { 382 {
382 epos = spos->entries; 383 iter (iter_cls, spos->name, epos->key, epos->val);
383 while (epos != NULL) 384 epos = epos->next;
384 {
385 iter (iter_cls, spos->name, epos->key, epos->val);
386 epos = epos->next;
387 }
388 spos = spos->next;
389 } 385 }
386 spos = spos->next;
387 }
390} 388}
391 389
392 390
@@ -400,10 +398,10 @@ GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
400 */ 398 */
401void 399void
402GNUNET_CONFIGURATION_iterate_section_values (const struct 400GNUNET_CONFIGURATION_iterate_section_values (const struct
403 GNUNET_CONFIGURATION_Handle *cfg, 401 GNUNET_CONFIGURATION_Handle *cfg,
404 const char *section, 402 const char *section,
405 GNUNET_CONFIGURATION_Iterator 403 GNUNET_CONFIGURATION_Iterator iter,
406 iter, void *iter_cls) 404 void *iter_cls)
407{ 405{
408 struct ConfigSection *spos; 406 struct ConfigSection *spos;
409 struct ConfigEntry *epos; 407 struct ConfigEntry *epos;
@@ -417,10 +415,10 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct
417 415
418 epos = spos->entries; 416 epos = spos->entries;
419 while (epos != NULL) 417 while (epos != NULL)
420 { 418 {
421 iter (iter_cls, spos->name, epos->key, epos->val); 419 iter (iter_cls, spos->name, epos->key, epos->val);
422 epos = epos->next; 420 epos = epos->next;
423 } 421 }
424} 422}
425 423
426 424
@@ -432,21 +430,21 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct
432 * @param iter_cls closure for iter 430 * @param iter_cls closure for iter
433 */ 431 */
434void 432void
435GNUNET_CONFIGURATION_iterate_sections (const struct 433GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
436 GNUNET_CONFIGURATION_Handle *cfg, 434 *cfg,
437 GNUNET_CONFIGURATION_Section_Iterator 435 GNUNET_CONFIGURATION_Section_Iterator
438 iter, void *iter_cls) 436 iter, void *iter_cls)
439{ 437{
440 struct ConfigSection *spos; 438 struct ConfigSection *spos;
441 struct ConfigSection *next; 439 struct ConfigSection *next;
442 440
443 next = cfg->sections; 441 next = cfg->sections;
444 while (next != NULL) 442 while (next != NULL)
445 { 443 {
446 spos = next; 444 spos = next;
447 next = spos->next; 445 next = spos->next;
448 iter (iter_cls, spos->name); 446 iter (iter_cls, spos->name);
449 } 447 }
450} 448}
451 449
452/** 450/**
@@ -457,7 +455,7 @@ GNUNET_CONFIGURATION_iterate_sections (const struct
457 */ 455 */
458void 456void
459GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, 457GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
460 const char *section) 458 const char *section)
461{ 459{
462 struct ConfigSection *spos; 460 struct ConfigSection *spos;
463 struct ConfigSection *prev; 461 struct ConfigSection *prev;
@@ -466,28 +464,28 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
466 prev = NULL; 464 prev = NULL;
467 spos = cfg->sections; 465 spos = cfg->sections;
468 while (spos != NULL) 466 while (spos != NULL)
467 {
468 if (0 == strcmp (section, spos->name))
469 { 469 {
470 if (0 == strcmp (section, spos->name)) 470 if (prev == NULL)
471 { 471 cfg->sections = spos->next;
472 if (prev == NULL) 472 else
473 cfg->sections = spos->next; 473 prev->next = spos->next;
474 else 474 while (NULL != (ent = spos->entries))
475 prev->next = spos->next; 475 {
476 while (NULL != (ent = spos->entries)) 476 spos->entries = ent->next;
477 { 477 GNUNET_free (ent->key);
478 spos->entries = ent->next; 478 GNUNET_free_non_null (ent->val);
479 GNUNET_free (ent->key); 479 GNUNET_free (ent);
480 GNUNET_free_non_null (ent->val); 480 cfg->dirty = GNUNET_YES;
481 GNUNET_free (ent); 481 }
482 cfg->dirty = GNUNET_YES; 482 GNUNET_free (spos->name);
483 } 483 GNUNET_free (spos);
484 GNUNET_free (spos->name); 484 return;
485 GNUNET_free (spos);
486 return;
487 }
488 prev = spos;
489 spos = spos->next;
490 } 485 }
486 prev = spos;
487 spos = spos->next;
488 }
491} 489}
492 490
493 491
@@ -502,7 +500,7 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
502 */ 500 */
503static void 501static void
504copy_entry (void *cls, const char *section, const char *option, 502copy_entry (void *cls, const char *section, const char *option,
505 const char *value) 503 const char *value)
506{ 504{
507 struct GNUNET_CONFIGURATION_Handle *dst = cls; 505 struct GNUNET_CONFIGURATION_Handle *dst = cls;
508 506
@@ -535,8 +533,7 @@ GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
535 * @return matching entry, NULL if not found 533 * @return matching entry, NULL if not found
536 */ 534 */
537static struct ConfigSection * 535static struct ConfigSection *
538findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, 536findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
539 const char *section)
540{ 537{
541 struct ConfigSection *pos; 538 struct ConfigSection *pos;
542 539
@@ -557,7 +554,7 @@ findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
557 */ 554 */
558static struct ConfigEntry * 555static struct ConfigEntry *
559findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, 556findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
560 const char *key) 557 const char *key)
561{ 558{
562 struct ConfigSection *sec; 559 struct ConfigSection *sec;
563 struct ConfigEntry *pos; 560 struct ConfigEntry *pos;
@@ -584,7 +581,7 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
584 */ 581 */
585static void 582static void
586compareEntries (void *cls, const char *section, const char *option, 583compareEntries (void *cls, const char *section, const char *option,
587 const char *value) 584 const char *value)
588{ 585{
589 struct DiffHandle *dh = cls; 586 struct DiffHandle *dh = cls;
590 struct ConfigEntry *entNew; 587 struct ConfigEntry *entNew;
@@ -605,9 +602,9 @@ compareEntries (void *cls, const char *section, const char *option,
605 */ 602 */
606int 603int
607GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle 604GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
608 *cfgDefault, 605 *cfgDefault,
609 const struct GNUNET_CONFIGURATION_Handle 606 const struct GNUNET_CONFIGURATION_Handle
610 *cfgNew, const char *filename) 607 *cfgNew, const char *filename)
611{ 608{
612 int ret; 609 int ret;
613 struct DiffHandle diffHandle; 610 struct DiffHandle diffHandle;
@@ -630,28 +627,28 @@ GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
630 * @param value value to set 627 * @param value value to set
631 */ 628 */
632void 629void
633GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle 630GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
634 *cfg, const char *section, 631 const char *section, const char *option,
635 const char *option, const char *value) 632 const char *value)
636{ 633{
637 struct ConfigSection *sec; 634 struct ConfigSection *sec;
638 struct ConfigEntry *e; 635 struct ConfigEntry *e;
639 636
640 e = findEntry (cfg, section, option); 637 e = findEntry (cfg, section, option);
641 if (e != NULL) 638 if (e != NULL)
642 { 639 {
643 GNUNET_free_non_null (e->val); 640 GNUNET_free_non_null (e->val);
644 e->val = GNUNET_strdup (value); 641 e->val = GNUNET_strdup (value);
645 return; 642 return;
646 } 643 }
647 sec = findSection (cfg, section); 644 sec = findSection (cfg, section);
648 if (sec == NULL) 645 if (sec == NULL)
649 { 646 {
650 sec = GNUNET_malloc (sizeof (struct ConfigSection)); 647 sec = GNUNET_malloc (sizeof (struct ConfigSection));
651 sec->name = GNUNET_strdup (section); 648 sec->name = GNUNET_strdup (section);
652 sec->next = cfg->sections; 649 sec->next = cfg->sections;
653 cfg->sections = sec; 650 cfg->sections = sec;
654 } 651 }
655 e = GNUNET_malloc (sizeof (struct ConfigEntry)); 652 e = GNUNET_malloc (sizeof (struct ConfigEntry));
656 e->key = GNUNET_strdup (option); 653 e->key = GNUNET_strdup (option);
657 e->val = GNUNET_strdup (value); 654 e->val = GNUNET_strdup (value);
@@ -669,10 +666,9 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
669 * @param number value to set 666 * @param number value to set
670 */ 667 */
671void 668void
672GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle 669GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
673 *cfg, const char *section, 670 const char *section, const char *option,
674 const char *option, 671 unsigned long long number)
675 unsigned long long number)
676{ 672{
677 char s[64]; 673 char s[64];
678 674
@@ -691,11 +687,10 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
691 * @return GNUNET_OK on success, GNUNET_SYSERR on error 687 * @return GNUNET_OK on success, GNUNET_SYSERR on error
692 */ 688 */
693int 689int
694GNUNET_CONFIGURATION_get_value_number (const struct 690GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
695 GNUNET_CONFIGURATION_Handle *cfg, 691 *cfg, const char *section,
696 const char *section, 692 const char *option,
697 const char *option, 693 unsigned long long *number)
698 unsigned long long *number)
699{ 694{
700 struct ConfigEntry *e; 695 struct ConfigEntry *e;
701 696
@@ -719,9 +714,9 @@ GNUNET_CONFIGURATION_get_value_number (const struct
719 */ 714 */
720int 715int
721GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle 716GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
722 *cfg, const char *section, 717 *cfg, const char *section,
723 const char *option, 718 const char *option,
724 struct GNUNET_TIME_Relative *time) 719 struct GNUNET_TIME_Relative *time)
725{ 720{
726 struct ConfigEntry *e; 721 struct ConfigEntry *e;
727 722
@@ -729,8 +724,7 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
729 if (e == NULL) 724 if (e == NULL)
730 return GNUNET_SYSERR; 725 return GNUNET_SYSERR;
731 726
732 return GNUNET_STRINGS_fancy_time_to_relative (e->val, 727 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
733 time);
734} 728}
735 729
736 730
@@ -754,8 +748,7 @@ GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
754 e = findEntry (cfg, section, option); 748 e = findEntry (cfg, section, option);
755 if (e == NULL) 749 if (e == NULL)
756 return GNUNET_SYSERR; 750 return GNUNET_SYSERR;
757 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, 751 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
758 size);
759} 752}
760 753
761 754
@@ -770,19 +763,18 @@ GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
770 * @return GNUNET_OK on success, GNUNET_SYSERR on error 763 * @return GNUNET_OK on success, GNUNET_SYSERR on error
771 */ 764 */
772int 765int
773GNUNET_CONFIGURATION_get_value_string (const struct 766GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
774 GNUNET_CONFIGURATION_Handle *cfg, 767 *cfg, const char *section,
775 const char *section, 768 const char *option, char **value)
776 const char *option, char **value)
777{ 769{
778 struct ConfigEntry *e; 770 struct ConfigEntry *e;
779 771
780 e = findEntry (cfg, section, option); 772 e = findEntry (cfg, section, option);
781 if ((e == NULL) || (e->val == NULL)) 773 if ((e == NULL) || (e->val == NULL))
782 { 774 {
783 *value = NULL; 775 *value = NULL;
784 return GNUNET_SYSERR; 776 return GNUNET_SYSERR;
785 } 777 }
786 *value = GNUNET_strdup (e->val); 778 *value = GNUNET_strdup (e->val);
787 return GNUNET_OK; 779 return GNUNET_OK;
788} 780}
@@ -801,12 +793,10 @@ GNUNET_CONFIGURATION_get_value_string (const struct
801 * @return GNUNET_OK on success, GNUNET_SYSERR on error 793 * @return GNUNET_OK on success, GNUNET_SYSERR on error
802 */ 794 */
803int 795int
804GNUNET_CONFIGURATION_get_value_choice (const struct 796GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
805 GNUNET_CONFIGURATION_Handle *cfg, 797 *cfg, const char *section,
806 const char *section, 798 const char *option, const char **choices,
807 const char *option, 799 const char **value)
808 const char **choices,
809 const char **value)
810{ 800{
811 struct ConfigEntry *e; 801 struct ConfigEntry *e;
812 int i; 802 int i;
@@ -816,19 +806,19 @@ GNUNET_CONFIGURATION_get_value_choice (const struct
816 return GNUNET_SYSERR; 806 return GNUNET_SYSERR;
817 i = 0; 807 i = 0;
818 while (choices[i] != NULL) 808 while (choices[i] != NULL)
819 { 809 {
820 if (0 == strcasecmp (choices[i], e->val)) 810 if (0 == strcasecmp (choices[i], e->val))
821 break; 811 break;
822 i++; 812 i++;
823 } 813 }
824 if (choices[i] == NULL) 814 if (choices[i] == NULL)
825 { 815 {
826 LOG (GNUNET_ERROR_TYPE_ERROR, 816 LOG (GNUNET_ERROR_TYPE_ERROR,
827 _("Configuration value '%s' for '%s'" 817 _("Configuration value '%s' for '%s'"
828 " in section '%s' is not in set of legal choices\n"), e->val, 818 " in section '%s' is not in set of legal choices\n"), e->val, option,
829 option, section); 819 section);
830 return GNUNET_SYSERR; 820 return GNUNET_SYSERR;
831 } 821 }
832 *value = choices[i]; 822 *value = choices[i];
833 return GNUNET_OK; 823 return GNUNET_OK;
834} 824}
@@ -842,9 +832,8 @@ GNUNET_CONFIGURATION_get_value_choice (const struct
842 * @return GNUNET_YES if so, GNUNET_NO if not. 832 * @return GNUNET_YES if so, GNUNET_NO if not.
843 */ 833 */
844int 834int
845GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle 835GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
846 *cfg, const char *section, 836 const char *section, const char *option)
847 const char *option)
848{ 837{
849 struct ConfigEntry *e; 838 struct ConfigEntry *e;
850 839
@@ -865,7 +854,7 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
865 */ 854 */
866char * 855char *
867GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle 856GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
868 *cfg, char *orig) 857 *cfg, char *orig)
869{ 858{
870 int i; 859 int i;
871 char *prefix; 860 char *prefix;
@@ -879,25 +868,24 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
879 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0')) 868 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
880 i++; 869 i++;
881 if (orig[i] == '\0') 870 if (orig[i] == '\0')
882 { 871 {
883 post = ""; 872 post = "";
884 } 873 }
885 else 874 else
886 { 875 {
887 orig[i] = '\0'; 876 orig[i] = '\0';
888 post = &orig[i + 1]; 877 post = &orig[i + 1];
889 } 878 }
890 if (GNUNET_OK != 879 if (GNUNET_OK !=
891 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], 880 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
892 &prefix)) 881 {
882 if (NULL == (env = getenv (&orig[1])))
893 { 883 {
894 if (NULL == (env = getenv (&orig[1]))) 884 orig[i] = DIR_SEPARATOR;
895 { 885 return orig;
896 orig[i] = DIR_SEPARATOR;
897 return orig;
898 }
899 prefix = GNUNET_strdup (env);
900 } 886 }
887 prefix = GNUNET_strdup (env);
888 }
901 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2); 889 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
902 strcpy (result, prefix); 890 strcpy (result, prefix);
903 if ((strlen (prefix) == 0) || 891 if ((strlen (prefix) == 0) ||
@@ -922,18 +910,18 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
922 */ 910 */
923int 911int
924GNUNET_CONFIGURATION_get_value_filename (const struct 912GNUNET_CONFIGURATION_get_value_filename (const struct
925 GNUNET_CONFIGURATION_Handle *cfg, 913 GNUNET_CONFIGURATION_Handle *cfg,
926 const char *section, 914 const char *section,
927 const char *option, char **value) 915 const char *option, char **value)
928{ 916{
929 char *tmp; 917 char *tmp;
930 918
931 if (GNUNET_OK != 919 if (GNUNET_OK !=
932 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp)) 920 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
933 { 921 {
934 *value = NULL; 922 *value = NULL;
935 return GNUNET_SYSERR; 923 return GNUNET_SYSERR;
936 } 924 }
937 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp); 925 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
938 *value = GNUNET_STRINGS_filename_expand (tmp); 926 *value = GNUNET_STRINGS_filename_expand (tmp);
939 GNUNET_free (tmp); 927 GNUNET_free (tmp);
@@ -954,15 +942,15 @@ GNUNET_CONFIGURATION_get_value_filename (const struct
954 */ 942 */
955int 943int
956GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle 944GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
957 *cfg, const char *section, 945 *cfg, const char *section,
958 const char *option) 946 const char *option)
959{ 947{
960 static const char *yesno[] = { "YES", "NO", NULL }; 948 static const char *yesno[] = { "YES", "NO", NULL };
961 const char *val; 949 const char *val;
962 int ret; 950 int ret;
963 951
964 ret = 952 ret =
965 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val); 953 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
966 if (ret == GNUNET_SYSERR) 954 if (ret == GNUNET_SYSERR)
967 return ret; 955 return ret;
968 if (val == yesno[0]) 956 if (val == yesno[0])
@@ -983,11 +971,11 @@ GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
983 */ 971 */
984int 972int
985GNUNET_CONFIGURATION_iterate_value_filenames (const struct 973GNUNET_CONFIGURATION_iterate_value_filenames (const struct
986 GNUNET_CONFIGURATION_Handle 974 GNUNET_CONFIGURATION_Handle *cfg,
987 *cfg, const char *section, 975 const char *section,
988 const char *option, 976 const char *option,
989 GNUNET_FileNameCallback cb, 977 GNUNET_FileNameCallback cb,
990 void *cb_cls) 978 void *cb_cls)
991{ 979{
992 char *list; 980 char *list;
993 char *pos; 981 char *pos;
@@ -1002,46 +990,46 @@ GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1002 ret = 0; 990 ret = 0;
1003 pos = list; 991 pos = list;
1004 while (1) 992 while (1)
993 {
994 while (pos[0] == ' ')
995 pos++;
996 if (strlen (pos) == 0)
997 break;
998 end = pos + 1;
999 while ((end[0] != ' ') && (end[0] != '\0'))
1005 { 1000 {
1006 while (pos[0] == ' ') 1001 if (end[0] == '\\')
1007 pos++; 1002 {
1008 if (strlen (pos) == 0) 1003 switch (end[1])
1009 break; 1004 {
1010 end = pos + 1; 1005 case '\\':
1011 while ((end[0] != ' ') && (end[0] != '\0')) 1006 case ' ':
1012 { 1007 memmove (end, &end[1], strlen (&end[1]) + 1);
1013 if (end[0] == '\\') 1008 case '\0':
1014 { 1009 /* illegal, but just keep it */
1015 switch (end[1]) 1010 break;
1016 { 1011 default:
1017 case '\\': 1012 /* illegal, but just ignore that there was a '/' */
1018 case ' ': 1013 break;
1019 memmove (end, &end[1], strlen (&end[1]) + 1); 1014 }
1020 case '\0': 1015 }
1021 /* illegal, but just keep it */ 1016 end++;
1022 break;
1023 default:
1024 /* illegal, but just ignore that there was a '/' */
1025 break;
1026 }
1027 }
1028 end++;
1029 }
1030 old = end[0];
1031 end[0] = '\0';
1032 if (strlen (pos) > 0)
1033 {
1034 ret++;
1035 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1036 {
1037 ret = GNUNET_SYSERR;
1038 break;
1039 }
1040 }
1041 if (old == '\0')
1042 break;
1043 pos = end + 1;
1044 } 1017 }
1018 old = end[0];
1019 end[0] = '\0';
1020 if (strlen (pos) > 0)
1021 {
1022 ret++;
1023 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1024 {
1025 ret = GNUNET_SYSERR;
1026 break;
1027 }
1028 }
1029 if (old == '\0')
1030 break;
1031 pos = end + 1;
1032 }
1045 GNUNET_free (list); 1033 GNUNET_free (list);
1046 return ret; 1034 return ret;
1047} 1035}
@@ -1065,21 +1053,21 @@ escape_name (const char *value)
1065 rpos = value; 1053 rpos = value;
1066 wpos = escaped; 1054 wpos = escaped;
1067 while (rpos[0] != '\0') 1055 while (rpos[0] != '\0')
1056 {
1057 switch (rpos[0])
1068 { 1058 {
1069 switch (rpos[0]) 1059 case '\\':
1070 { 1060 case ' ':
1071 case '\\': 1061 wpos[0] = '\\';
1072 case ' ': 1062 wpos[1] = rpos[0];
1073 wpos[0] = '\\'; 1063 wpos += 2;
1074 wpos[1] = rpos[0]; 1064 break;
1075 wpos += 2; 1065 default:
1076 break; 1066 wpos[0] = rpos[0];
1077 default: 1067 wpos++;
1078 wpos[0] = rpos[0];
1079 wpos++;
1080 }
1081 rpos++;
1082 } 1068 }
1069 rpos++;
1070 }
1083 return escaped; 1071 return escaped;
1084} 1072}
1085 1073
@@ -1114,9 +1102,9 @@ test_match (void *cls, const char *fn)
1114 */ 1102 */
1115int 1103int
1116GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle 1104GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1117 *cfg, const char *section, 1105 *cfg, const char *section,
1118 const char *option, 1106 const char *option,
1119 const char *value) 1107 const char *value)
1120{ 1108{
1121 char *escaped; 1109 char *escaped;
1122 char *old; 1110 char *old;
@@ -1124,9 +1112,9 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1124 1112
1125 if (GNUNET_SYSERR == 1113 if (GNUNET_SYSERR ==
1126 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option, 1114 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1127 &test_match, 1115 &test_match,
1128 (void *) value)) 1116 (void *) value))
1129 return GNUNET_NO; /* already exists */ 1117 return GNUNET_NO; /* already exists */
1130 if (GNUNET_OK != 1118 if (GNUNET_OK !=
1131 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old)) 1119 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1132 old = GNUNET_strdup (""); 1120 old = GNUNET_strdup ("");
@@ -1158,9 +1146,9 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1158 */ 1146 */
1159int 1147int
1160GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle 1148GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1161 *cfg, const char *section, 1149 *cfg, const char *section,
1162 const char *option, 1150 const char *option,
1163 const char *value) 1151 const char *value)
1164{ 1152{
1165 char *list; 1153 char *list;
1166 char *pos; 1154 char *pos;
@@ -1174,55 +1162,55 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1174 match = escape_name (value); 1162 match = escape_name (value);
1175 pos = list; 1163 pos = list;
1176 while (1) 1164 while (1)
1165 {
1166 while (pos[0] == ' ')
1167 pos++;
1168 if (strlen (pos) == 0)
1169 break;
1170 end = pos + 1;
1171 while ((end[0] != ' ') && (end[0] != '\0'))
1172 {
1173 if (end[0] == '\\')
1174 {
1175 switch (end[1])
1176 {
1177 case '\\':
1178 case ' ':
1179 end++;
1180 break;
1181 case '\0':
1182 /* illegal, but just keep it */
1183 break;
1184 default:
1185 /* illegal, but just ignore that there was a '/' */
1186 break;
1187 }
1188 }
1189 end++;
1190 }
1191 old = end[0];
1192 end[0] = '\0';
1193 if (0 == strcmp (pos, match))
1177 { 1194 {
1178 while (pos[0] == ' ') 1195 if (old != '\0')
1179 pos++; 1196 memmove (pos, &end[1], strlen (&end[1]) + 1);
1180 if (strlen (pos) == 0) 1197 else
1181 break; 1198 {
1182 end = pos + 1; 1199 if (pos != list)
1183 while ((end[0] != ' ') && (end[0] != '\0')) 1200 pos[-1] = '\0';
1184 { 1201 else
1185 if (end[0] == '\\') 1202 pos[0] = '\0';
1186 { 1203 }
1187 switch (end[1]) 1204 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1188 { 1205 GNUNET_free (list);
1189 case '\\': 1206 GNUNET_free (match);
1190 case ' ': 1207 return GNUNET_OK;
1191 end++;
1192 break;
1193 case '\0':
1194 /* illegal, but just keep it */
1195 break;
1196 default:
1197 /* illegal, but just ignore that there was a '/' */
1198 break;
1199 }
1200 }
1201 end++;
1202 }
1203 old = end[0];
1204 end[0] = '\0';
1205 if (0 == strcmp (pos, match))
1206 {
1207 if (old != '\0')
1208 memmove (pos, &end[1], strlen (&end[1]) + 1);
1209 else
1210 {
1211 if (pos != list)
1212 pos[-1] = '\0';
1213 else
1214 pos[0] = '\0';
1215 }
1216 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1217 GNUNET_free (list);
1218 GNUNET_free (match);
1219 return GNUNET_OK;
1220 }
1221 if (old == '\0')
1222 break;
1223 end[0] = old;
1224 pos = end + 1;
1225 } 1208 }
1209 if (old == '\0')
1210 break;
1211 end[0] = old;
1212 pos = end + 1;
1213 }
1226 GNUNET_free (list); 1214 GNUNET_free (list);
1227 GNUNET_free (match); 1215 GNUNET_free (match);
1228 return GNUNET_NO; 1216 return GNUNET_NO;
@@ -1237,8 +1225,7 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1237 * @return GNUNET_OK on success 1225 * @return GNUNET_OK on success
1238 */ 1226 */
1239static int 1227static int
1240parse_configuration_file (void *cls, 1228parse_configuration_file (void *cls, const char *filename)
1241 const char *filename)
1242{ 1229{
1243 struct GNUNET_CONFIGURATION_Handle *cfg = cls; 1230 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1244 int ret; 1231 int ret;
@@ -1258,7 +1245,7 @@ parse_configuration_file (void *cls,
1258 */ 1245 */
1259int 1246int
1260GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, 1247GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1261 const char *filename) 1248 const char *filename)
1262{ 1249{
1263 char *baseconfig; 1250 char *baseconfig;
1264 char *ipath; 1251 char *ipath;
@@ -1267,29 +1254,26 @@ GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1267 if (ipath == NULL) 1254 if (ipath == NULL)
1268 return GNUNET_SYSERR; 1255 return GNUNET_SYSERR;
1269 baseconfig = NULL; 1256 baseconfig = NULL;
1270 GNUNET_asprintf (&baseconfig, "%s%s", ipath, 1257 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1271 "config.d");
1272 GNUNET_free (ipath); 1258 GNUNET_free (ipath);
1273 if (GNUNET_SYSERR == 1259 if (GNUNET_SYSERR ==
1274 GNUNET_DISK_directory_scan (baseconfig, 1260 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1275 &parse_configuration_file,
1276 cfg))
1277 { 1261 {
1278 GNUNET_free (baseconfig); 1262 GNUNET_free (baseconfig);
1279 return GNUNET_SYSERR; /* no configuration at all found */ 1263 return GNUNET_SYSERR; /* no configuration at all found */
1280 } 1264 }
1281 GNUNET_free (baseconfig); 1265 GNUNET_free (baseconfig);
1282 if ( (filename != NULL) && 1266 if ((filename != NULL) &&
1283 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)) ) 1267 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1284 { 1268 {
1285 /* specified configuration not found */ 1269 /* specified configuration not found */
1286 return GNUNET_SYSERR; 1270 return GNUNET_SYSERR;
1287 } 1271 }
1288 if (((GNUNET_YES != 1272 if (((GNUNET_YES !=
1289 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) && 1273 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1290 (filename != NULL)) 1274 (filename != NULL))
1291 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", 1275 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1292 filename); 1276 filename);
1293 if ((GNUNET_YES == 1277 if ((GNUNET_YES ==
1294 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) && 1278 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1295 (GNUNET_YES == 1279 (GNUNET_YES ==
diff --git a/src/util/connection.c b/src/util/connection.c
index cd0035a8f..61c26673b 100644
--- a/src/util/connection.c
+++ b/src/util/connection.c
@@ -333,8 +333,7 @@ GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
333 * @return the boxed socket handle 333 * @return the boxed socket handle
334 */ 334 */
335struct GNUNET_CONNECTION_Handle * 335struct GNUNET_CONNECTION_Handle *
336GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle 336GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
337 *osSocket)
338{ 337{
339 struct GNUNET_CONNECTION_Handle *ret; 338 struct GNUNET_CONNECTION_Handle *ret;
340 339
@@ -357,8 +356,8 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
357 */ 356 */
358struct GNUNET_CONNECTION_Handle * 357struct GNUNET_CONNECTION_Handle *
359GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, 358GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
360 void *access_cls, 359 void *access_cls,
361 struct GNUNET_NETWORK_Handle *lsock) 360 struct GNUNET_NETWORK_Handle *lsock)
362{ 361{
363 struct GNUNET_CONNECTION_Handle *ret; 362 struct GNUNET_CONNECTION_Handle *ret;
364 char addr[128]; 363 char addr[128];
@@ -379,94 +378,94 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
379 378
380 addrlen = sizeof (addr); 379 addrlen = sizeof (addr);
381 sock = 380 sock =
382 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen); 381 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
383 if (NULL == sock) 382 if (NULL == sock)
384 { 383 {
385 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept"); 384 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept");
386 return NULL; 385 return NULL;
387 } 386 }
388 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t))) 387 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t)))
389 { 388 {
390 GNUNET_break (0); 389 GNUNET_break (0);
391 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); 390 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
392 return NULL; 391 return NULL;
393 } 392 }
394 393
395 sa = (struct sockaddr *) addr; 394 sa = (struct sockaddr *) addr;
396 v6 = (struct sockaddr_in6 *) addr; 395 v6 = (struct sockaddr_in6 *) addr;
397 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr))) 396 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
398 { 397 {
399 /* convert to V4 address */ 398 /* convert to V4 address */
400 v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); 399 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
401 memset (v4, 0, sizeof (struct sockaddr_in)); 400 memset (v4, 0, sizeof (struct sockaddr_in));
402 v4->sin_family = AF_INET; 401 v4->sin_family = AF_INET;
403#if HAVE_SOCKADDR_IN_SIN_LEN 402#if HAVE_SOCKADDR_IN_SIN_LEN
404 v4->sin_len = (u_char) sizeof (struct sockaddr_in); 403 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
405#endif 404#endif
406 memcpy (&v4->sin_addr, 405 memcpy (&v4->sin_addr,
407 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) - 406 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
408 sizeof (struct in_addr)], 407 sizeof (struct in_addr)],
409 sizeof (struct in_addr)); 408 sizeof (struct in_addr));
410 v4->sin_port = v6->sin6_port; 409 v4->sin_port = v6->sin6_port;
411 uaddr = v4; 410 uaddr = v4;
412 addrlen = sizeof (struct sockaddr_in); 411 addrlen = sizeof (struct sockaddr_in);
413 } 412 }
414 else 413 else
415 { 414 {
416 uaddr = GNUNET_malloc (addrlen); 415 uaddr = GNUNET_malloc (addrlen);
417 memcpy (uaddr, addr, addrlen); 416 memcpy (uaddr, addr, addrlen);
418 } 417 }
419 gcp = NULL; 418 gcp = NULL;
420 gc.uid = 0; 419 gc.uid = 0;
421 gc.gid = 0; 420 gc.gid = 0;
422 if (sa->sa_family == AF_UNIX) 421 if (sa->sa_family == AF_UNIX)
423 { 422 {
424#if HAVE_GETPEEREID 423#if HAVE_GETPEEREID
425 /* most BSDs */ 424 /* most BSDs */
426 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid)) 425 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid))
427 gcp = &gc; 426 gcp = &gc;
428#else 427#else
429#ifdef SO_PEERCRED 428#ifdef SO_PEERCRED
430 /* largely traditional GNU/Linux */ 429 /* largely traditional GNU/Linux */
431 olen = sizeof (uc); 430 olen = sizeof (uc);
432 if ((0 == 431 if ((0 ==
433 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, 432 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc,
434 &uc, &olen)) && (olen == sizeof (uc))) 433 &olen)) && (olen == sizeof (uc)))
435 { 434 {
436 gc.uid = uc.uid; 435 gc.uid = uc.uid;
437 gc.gid = uc.gid; 436 gc.gid = uc.gid;
438 gcp = &gc; 437 gcp = &gc;
439 } 438 }
440#else 439#else
441#if HAVE_GETPEERUCRED 440#if HAVE_GETPEERUCRED
442 /* this is for Solaris 10 */ 441 /* this is for Solaris 10 */
443 ucred_t *uc; 442 ucred_t *uc;
444 443
445 uc = NULL; 444 uc = NULL;
446 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc)) 445 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
447 { 446 {
448 gc.uid = ucred_geteuid (uc); 447 gc.uid = ucred_geteuid (uc);
449 gc.gid = ucred_getegid (uc); 448 gc.gid = ucred_getegid (uc);
450 gcp = &gc; 449 gcp = &gc;
451 } 450 }
452 ucred_free (uc); 451 ucred_free (uc);
453#endif 452#endif
454#endif 453#endif
455#endif 454#endif
456 } 455 }
457 456
458 if ((access != NULL) && 457 if ((access != NULL) &&
459 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen)))) 458 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
460 { 459 {
461 if (aret == GNUNET_NO) 460 if (aret == GNUNET_NO)
462 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"), 461 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
463 GNUNET_a2s (uaddr, addrlen)); 462 GNUNET_a2s (uaddr, addrlen));
464 GNUNET_break (GNUNET_OK == 463 GNUNET_break (GNUNET_OK ==
465 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR)); 464 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
466 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); 465 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
467 GNUNET_free (uaddr); 466 GNUNET_free (uaddr);
468 return NULL; 467 return NULL;
469 } 468 }
470 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 469 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
471 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 470 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
472 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); 471 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
@@ -490,7 +489,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
490 */ 489 */
491int 490int
492GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock, 491GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
493 void **addr, size_t * addrlen) 492 void **addr, size_t * addrlen)
494{ 493{
495 if ((sock->addr == NULL) || (sock->addrlen == 0)) 494 if ((sock->addr == NULL) || (sock->addrlen == 0))
496 return GNUNET_NO; 495 return GNUNET_NO;
@@ -523,8 +522,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
523 * @param tc unused 522 * @param tc unused
524 */ 523 */
525static void 524static void
526destroy_continuation (void *cls, 525destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 const struct GNUNET_SCHEDULER_TaskContext *tc)
528{ 526{
529 struct GNUNET_CONNECTION_Handle *sock = cls; 527 struct GNUNET_CONNECTION_Handle *sock = cls;
530 GNUNET_CONNECTION_TransmitReadyNotify notify; 528 GNUNET_CONNECTION_TransmitReadyNotify notify;
@@ -533,78 +531,77 @@ destroy_continuation (void *cls,
533 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK; 531 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
534 GNUNET_assert (sock->dns_active == NULL); 532 GNUNET_assert (sock->dns_active == NULL);
535 if (0 != (sock->ccs & COCO_TRANSMIT_READY)) 533 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
536 { 534 {
537#if DEBUG_CONNECTION 535#if DEBUG_CONNECTION
538 LOG (GNUNET_ERROR_TYPE_DEBUG, 536 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy waits for CCS-TR to be done (%p)\n",
539 "Destroy waits for CCS-TR to be done (%p)\n", sock); 537 sock);
540#endif 538#endif
541 sock->ccs |= COCO_DESTROY_CONTINUATION; 539 sock->ccs |= COCO_DESTROY_CONTINUATION;
542 return; 540 return;
543 } 541 }
544 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 542 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
545 { 543 {
546#if DEBUG_CONNECTION 544#if DEBUG_CONNECTION
547 LOG (GNUNET_ERROR_TYPE_DEBUG, 545 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 "Destroy waits for write_task to be done (%p)\n", sock); 546 "Destroy waits for write_task to be done (%p)\n", sock);
549#endif 547#endif
550 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 548 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
551 sock->destroy_task = 549 sock->destroy_task =
552 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation, 550 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation,
553 sock); 551 sock);
554 return; 552 return;
555 } 553 }
556 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN)) 554 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
557 { 555 {
558 sock->ccs |= COCO_DESTROY_CONTINUATION; 556 sock->ccs |= COCO_DESTROY_CONTINUATION;
559 return; 557 return;
560 } 558 }
561 if (sock->sock != NULL) 559 if (sock->sock != NULL)
562 { 560 {
563#if DEBUG_CONNECTION 561#if DEBUG_CONNECTION
564 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock); 562 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
565#endif 563#endif
566 if (sock->persist != GNUNET_YES) 564 if (sock->persist != GNUNET_YES)
567 {
568 if ((GNUNET_YES !=
569 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
570 && (errno != ENOTCONN) && (errno != ECONNRESET))
571 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
572 }
573 }
574 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
575 { 565 {
576 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 566 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
577 sock->destroy_task = 567 && (errno != ENOTCONN) && (errno != ECONNRESET))
578 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation, 568 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
579 sock);
580 return;
581 } 569 }
570 }
571 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
572 {
573 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
574 sock->destroy_task =
575 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation,
576 sock);
577 return;
578 }
582#if DEBUG_CONNECTION 579#if DEBUG_CONNECTION
583 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock); 580 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
584#endif 581#endif
585 while (NULL != (pos = sock->ap_head)) 582 while (NULL != (pos = sock->ap_head))
586 { 583 {
587 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 584 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
588 GNUNET_SCHEDULER_cancel (pos->task); 585 GNUNET_SCHEDULER_cancel (pos->task);
589 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos); 586 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
590 GNUNET_free (pos); 587 GNUNET_free (pos);
591 } 588 }
592 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 589 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
593 GNUNET_assert (sock->ccs == COCO_NONE); 590 GNUNET_assert (sock->ccs == COCO_NONE);
594 if (NULL != (notify = sock->nth.notify_ready)) 591 if (NULL != (notify = sock->nth.notify_ready))
595 { 592 {
596 sock->nth.notify_ready = NULL; 593 sock->nth.notify_ready = NULL;
597 notify (sock->nth.notify_ready_cls, 0, NULL); 594 notify (sock->nth.notify_ready_cls, 0, NULL);
598 } 595 }
599 596
600 if (sock->sock != NULL) 597 if (sock->sock != NULL)
601 { 598 {
602 if (sock->persist != GNUNET_YES) 599 if (sock->persist != GNUNET_YES)
603 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 600 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
604 else 601 else
605 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately 602 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
606 * leak the socket in this special case) ... */ 603 * leak the socket in this special case) ... */
607 } 604 }
608 GNUNET_free_non_null (sock->addr); 605 GNUNET_free_non_null (sock->addr);
609 GNUNET_free_non_null (sock->hostname); 606 GNUNET_free_non_null (sock->hostname);
610 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 607 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
@@ -639,9 +636,8 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
639{ 636{
640#if DEBUG_CONNECTION 637#if DEBUG_CONNECTION
641 LOG ((0 != 638 LOG ((0 !=
642 strncmp (h->hostname, "localhost:", 639 strncmp (h->hostname, "localhost:",
643 10)) ? GNUNET_ERROR_TYPE_INFO : 640 10)) ? GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
644 GNUNET_ERROR_TYPE_WARNING,
645 _ 641 _
646 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), 642 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
647 h->hostname, h->port); 643 h->hostname, h->port);
@@ -654,39 +650,39 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
654 650
655 /* trigger jobs that used to wait on "connect_task" */ 651 /* trigger jobs that used to wait on "connect_task" */
656 if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) 652 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
657 { 653 {
658#if DEBUG_CONNECTION 654#if DEBUG_CONNECTION
659 LOG (GNUNET_ERROR_TYPE_DEBUG, 655 LOG (GNUNET_ERROR_TYPE_DEBUG,
660 "connect_fail_continuation triggers receive_again (%p)\n", h); 656 "connect_fail_continuation triggers receive_again (%p)\n", h);
661#endif 657#endif
662 h->ccs -= COCO_RECEIVE_AGAIN; 658 h->ccs -= COCO_RECEIVE_AGAIN;
663 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 659 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
664 } 660 }
665 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 661 if (0 != (h->ccs & COCO_TRANSMIT_READY))
666 { 662 {
667#if DEBUG_CONNECTION 663#if DEBUG_CONNECTION
668 LOG (GNUNET_ERROR_TYPE_DEBUG, 664 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n", 665 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
670 h); 666 h);
671#endif 667#endif
672 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 668 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
673 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 669 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
674 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 670 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
675 h->ccs -= COCO_TRANSMIT_READY; 671 h->ccs -= COCO_TRANSMIT_READY;
676 GNUNET_assert (h->nth.notify_ready != NULL); 672 GNUNET_assert (h->nth.notify_ready != NULL);
677 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 673 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
678 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h); 674 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
679 } 675 }
680 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) 676 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
681 { 677 {
682#if DEBUG_CONNECTION 678#if DEBUG_CONNECTION
683 LOG (GNUNET_ERROR_TYPE_DEBUG, 679 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "connect_fail_continuation runs destroy_continuation (%p)\n", h); 680 "connect_fail_continuation runs destroy_continuation (%p)\n", h);
685#endif 681#endif
686 h->ccs -= COCO_DESTROY_CONTINUATION; 682 h->ccs -= COCO_DESTROY_CONTINUATION;
687 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); 683 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
688 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); 684 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
689 } 685 }
690} 686}
691 687
692 688
@@ -704,43 +700,42 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
704#endif 700#endif
705 /* trigger jobs that waited for the connection */ 701 /* trigger jobs that waited for the connection */
706 if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) 702 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
707 { 703 {
708#if DEBUG_CONNECTION 704#if DEBUG_CONNECTION
709 LOG (GNUNET_ERROR_TYPE_DEBUG, 705 LOG (GNUNET_ERROR_TYPE_DEBUG,
710 "connect_success_continuation runs receive_again (%p)\n", h); 706 "connect_success_continuation runs receive_again (%p)\n", h);
711#endif 707#endif
712 h->ccs -= COCO_RECEIVE_AGAIN; 708 h->ccs -= COCO_RECEIVE_AGAIN;
713 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 709 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
714 } 710 }
715 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 711 if (0 != (h->ccs & COCO_TRANSMIT_READY))
716 { 712 {
717#if DEBUG_CONNECTION 713#if DEBUG_CONNECTION
718 LOG (GNUNET_ERROR_TYPE_DEBUG, 714 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", 715 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
720 h); 716 h);
721#endif 717#endif
722 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 718 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
723 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 719 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
724 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 720 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
725 h->ccs -= COCO_TRANSMIT_READY; 721 h->ccs -= COCO_TRANSMIT_READY;
726 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 722 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
727 GNUNET_assert (h->nth.notify_ready != NULL); 723 GNUNET_assert (h->nth.notify_ready != NULL);
728 h->write_task = 724 h->write_task =
729 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 725 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
730 (h->nth.transmit_timeout), h->sock, 726 (h->nth.transmit_timeout), h->sock,
731 &transmit_ready, h); 727 &transmit_ready, h);
732 } 728 }
733 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) 729 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
734 { 730 {
735#if DEBUG_CONNECTION 731#if DEBUG_CONNECTION
736 LOG (GNUNET_ERROR_TYPE_DEBUG, 732 LOG (GNUNET_ERROR_TYPE_DEBUG,
737 "connect_success_continuation runs destroy_continuation (%p)\n", 733 "connect_success_continuation runs destroy_continuation (%p)\n", h);
738 h);
739#endif 734#endif
740 h->ccs -= COCO_DESTROY_CONTINUATION; 735 h->ccs -= COCO_DESTROY_CONTINUATION;
741 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); 736 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
742 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); 737 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
743 } 738 }
744} 739}
745 740
746 741
@@ -753,7 +748,7 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
753 */ 748 */
754static void 749static void
755connect_probe_continuation (void *cls, 750connect_probe_continuation (void *cls,
756 const struct GNUNET_SCHEDULER_TaskContext *tc) 751 const struct GNUNET_SCHEDULER_TaskContext *tc)
757{ 752{
758 struct AddressProbe *ap = cls; 753 struct AddressProbe *ap = cls;
759 struct GNUNET_CONNECTION_Handle *h = ap->h; 754 struct GNUNET_CONNECTION_Handle *h = ap->h;
@@ -768,15 +763,15 @@ connect_probe_continuation (void *cls,
768 error = 0; 763 error = 0;
769 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || 764 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
770 (GNUNET_OK != 765 (GNUNET_OK !=
771 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, 766 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error,
772 &error, &len)) || (error != 0)) 767 &len)) || (error != 0))
773 { 768 {
774 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 769 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
775 GNUNET_free (ap); 770 GNUNET_free (ap);
776 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO)) 771 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
777 connect_fail_continuation (h); 772 connect_fail_continuation (h);
778 return; 773 return;
779 } 774 }
780 GNUNET_assert (h->sock == NULL); 775 GNUNET_assert (h->sock == NULL);
781 h->sock = ap->sock; 776 h->sock = ap->sock;
782 GNUNET_assert (h->addr == NULL); 777 GNUNET_assert (h->addr == NULL);
@@ -786,12 +781,12 @@ connect_probe_continuation (void *cls,
786 GNUNET_free (ap); 781 GNUNET_free (ap);
787 /* cancel all other attempts */ 782 /* cancel all other attempts */
788 while (NULL != (pos = h->ap_head)) 783 while (NULL != (pos = h->ap_head))
789 { 784 {
790 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 785 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
791 GNUNET_SCHEDULER_cancel (pos->task); 786 GNUNET_SCHEDULER_cancel (pos->task);
792 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos); 787 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
793 GNUNET_free (pos); 788 GNUNET_free (pos);
794 } 789 }
795 connect_success_continuation (h); 790 connect_success_continuation (h);
796} 791}
797 792
@@ -806,27 +801,27 @@ connect_probe_continuation (void *cls,
806 */ 801 */
807static void 802static void
808try_connect_using_address (void *cls, const struct sockaddr *addr, 803try_connect_using_address (void *cls, const struct sockaddr *addr,
809 socklen_t addrlen) 804 socklen_t addrlen)
810{ 805{
811 struct GNUNET_CONNECTION_Handle *h = cls; 806 struct GNUNET_CONNECTION_Handle *h = cls;
812 struct AddressProbe *ap; 807 struct AddressProbe *ap;
813 struct GNUNET_TIME_Relative delay; 808 struct GNUNET_TIME_Relative delay;
814 809
815 if (addr == NULL) 810 if (addr == NULL)
816 { 811 {
817 h->dns_active = NULL; 812 h->dns_active = NULL;
818 if ((NULL == h->ap_head) && (NULL == h->sock)) 813 if ((NULL == h->ap_head) && (NULL == h->sock))
819 connect_fail_continuation (h); 814 connect_fail_continuation (h);
820 return; 815 return;
821 } 816 }
822 if (h->sock != NULL) 817 if (h->sock != NULL)
823 return; /* already connected */ 818 return; /* already connected */
824 GNUNET_assert (h->addr == NULL); 819 GNUNET_assert (h->addr == NULL);
825 /* try to connect */ 820 /* try to connect */
826#if DEBUG_CONNECTION 821#if DEBUG_CONNECTION
827 LOG (GNUNET_ERROR_TYPE_DEBUG, 822 LOG (GNUNET_ERROR_TYPE_DEBUG,
828 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, 823 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, h->port,
829 h->port, GNUNET_a2s (addr, addrlen), h->port); 824 GNUNET_a2s (addr, addrlen), h->port);
830#endif 825#endif
831 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen); 826 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
832 ap->addr = (const struct sockaddr *) &ap[1]; 827 ap->addr = (const struct sockaddr *) &ap[1];
@@ -835,25 +830,24 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
835 ap->h = h; 830 ap->h = h;
836 831
837 switch (ap->addr->sa_family) 832 switch (ap->addr->sa_family)
838 { 833 {
839 case AF_INET: 834 case AF_INET:
840 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port); 835 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
841 break; 836 break;
842 case AF_INET6: 837 case AF_INET6:
843 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port); 838 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
844 break; 839 break;
845 default: 840 default:
846 GNUNET_break (0); 841 GNUNET_break (0);
847 GNUNET_free (ap); 842 GNUNET_free (ap);
848 return; /* not supported by us */ 843 return; /* not supported by us */
849 } 844 }
850 ap->sock = 845 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
851 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
852 if (ap->sock == NULL) 846 if (ap->sock == NULL)
853 { 847 {
854 GNUNET_free (ap); 848 GNUNET_free (ap);
855 return; /* not supported by OS */ 849 return; /* not supported by OS */
856 } 850 }
857#if DEBUG_CONNECTION 851#if DEBUG_CONNECTION
858 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), 852 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
859 GNUNET_a2s (ap->addr, ap->addrlen), h); 853 GNUNET_a2s (ap->addr, ap->addrlen), h);
@@ -861,33 +855,32 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
861 if ((GNUNET_OK != 855 if ((GNUNET_OK !=
862 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && 856 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
863 (errno != EINPROGRESS)) 857 (errno != EINPROGRESS))
864 { 858 {
865 /* maybe refused / unsupported address, try next */ 859 /* maybe refused / unsupported address, try next */
866 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); 860 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
867#if 0 861#if 0
868 LOG (GNUNET_ERROR_TYPE_INFO, 862 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to `%s' (%p)\n"),
869 _("Failed to connect to `%s' (%p)\n"), 863 GNUNET_a2s (ap->addr, ap->addrlen), h);
870 GNUNET_a2s (ap->addr, ap->addrlen), h);
871#endif 864#endif
872 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 865 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
873 GNUNET_free (ap); 866 GNUNET_free (ap);
874 return; 867 return;
875 } 868 }
876 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap); 869 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
877 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT; 870 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
878 if (h->nth.notify_ready != NULL) 871 if (h->nth.notify_ready != NULL)
879 delay = 872 delay =
880 GNUNET_TIME_relative_min (delay, 873 GNUNET_TIME_relative_min (delay,
881 GNUNET_TIME_absolute_get_remaining (h-> 874 GNUNET_TIME_absolute_get_remaining (h->
882 nth.transmit_timeout)); 875 nth.transmit_timeout));
883 if (h->receiver != NULL) 876 if (h->receiver != NULL)
884 delay = 877 delay =
885 GNUNET_TIME_relative_min (delay, 878 GNUNET_TIME_relative_min (delay,
886 GNUNET_TIME_absolute_get_remaining 879 GNUNET_TIME_absolute_get_remaining
887 (h->receive_timeout)); 880 (h->receive_timeout));
888 ap->task = 881 ap->task =
889 GNUNET_SCHEDULER_add_write_net (delay, ap->sock, 882 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
890 &connect_probe_continuation, ap); 883 &connect_probe_continuation, ap);
891} 884}
892 885
893 886
@@ -902,13 +895,13 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
902 * @return the socket handle 895 * @return the socket handle
903 */ 896 */
904struct GNUNET_CONNECTION_Handle * 897struct GNUNET_CONNECTION_Handle *
905GNUNET_CONNECTION_create_from_connect (const struct 898GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
906 GNUNET_CONFIGURATION_Handle *cfg, 899 *cfg, const char *hostname,
907 const char *hostname, uint16_t port) 900 uint16_t port)
908{ 901{
909 struct GNUNET_CONNECTION_Handle *ret; 902 struct GNUNET_CONNECTION_Handle *ret;
910 903
911 GNUNET_assert (0 < strlen (hostname)); /* sanity check */ 904 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
912 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 905 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
913 ret->cfg = cfg; 906 ret->cfg = cfg;
914 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 907 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
@@ -916,9 +909,9 @@ GNUNET_CONNECTION_create_from_connect (const struct
916 ret->port = port; 909 ret->port = port;
917 ret->hostname = GNUNET_strdup (hostname); 910 ret->hostname = GNUNET_strdup (hostname);
918 ret->dns_active = 911 ret->dns_active =
919 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC, 912 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
920 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT, 913 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
921 &try_connect_using_address, ret); 914 &try_connect_using_address, ret);
922 return ret; 915 return ret;
923} 916}
924 917
@@ -934,15 +927,15 @@ GNUNET_CONNECTION_create_from_connect (const struct
934 */ 927 */
935struct GNUNET_CONNECTION_Handle * 928struct GNUNET_CONNECTION_Handle *
936GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct 929GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
937 GNUNET_CONFIGURATION_Handle 930 GNUNET_CONFIGURATION_Handle
938 *cfg, const char *unixpath) 931 *cfg, const char *unixpath)
939{ 932{
940#ifdef AF_UNIX 933#ifdef AF_UNIX
941 struct GNUNET_CONNECTION_Handle *ret; 934 struct GNUNET_CONNECTION_Handle *ret;
942 struct sockaddr_un *un; 935 struct sockaddr_un *un;
943 size_t slen; 936 size_t slen;
944 937
945 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ 938 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
946 un = GNUNET_malloc (sizeof (struct sockaddr_un)); 939 un = GNUNET_malloc (sizeof (struct sockaddr_un));
947 un->sun_family = AF_UNIX; 940 un->sun_family = AF_UNIX;
948 slen = strlen (unixpath); 941 slen = strlen (unixpath);
@@ -967,20 +960,20 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
967 ret->addrlen = slen; 960 ret->addrlen = slen;
968 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); 961 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
969 if (NULL == ret->sock) 962 if (NULL == ret->sock)
970 { 963 {
971 GNUNET_free (ret->addr); 964 GNUNET_free (ret->addr);
972 GNUNET_free (ret->write_buffer); 965 GNUNET_free (ret->write_buffer);
973 GNUNET_free (ret); 966 GNUNET_free (ret);
974 return NULL; 967 return NULL;
975 } 968 }
976 if (GNUNET_OK != 969 if (GNUNET_OK !=
977 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen)) 970 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
978 { 971 {
979 /* Just return; we expect everything to work eventually so don't fail HARD */ 972 /* Just return; we expect everything to work eventually so don't fail HARD */
980 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock)); 973 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
981 ret->sock = NULL; 974 ret->sock = NULL;
982 return ret; 975 return ret;
983 } 976 }
984 connect_success_continuation (ret); 977 connect_success_continuation (ret);
985 return ret; 978 return ret;
986#else 979#else
@@ -1001,8 +994,8 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
1001 */ 994 */
1002struct GNUNET_CONNECTION_Handle * 995struct GNUNET_CONNECTION_Handle *
1003GNUNET_CONNECTION_create_from_sockaddr (int af_family, 996GNUNET_CONNECTION_create_from_sockaddr (int af_family,
1004 const struct sockaddr *serv_addr, 997 const struct sockaddr *serv_addr,
1005 socklen_t addrlen) 998 socklen_t addrlen)
1006{ 999{
1007 struct GNUNET_NETWORK_Handle *s; 1000 struct GNUNET_NETWORK_Handle *s;
1008 struct GNUNET_CONNECTION_Handle *ret; 1001 struct GNUNET_CONNECTION_Handle *ret;
@@ -1010,22 +1003,20 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
1010 1003
1011 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); 1004 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1012 if (s == NULL) 1005 if (s == NULL)
1013 { 1006 {
1014 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 1007 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket");
1015 "socket"); 1008 return NULL;
1016 return NULL; 1009 }
1017 }
1018 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && 1010 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
1019 (errno != EINPROGRESS)) 1011 (errno != EINPROGRESS))
1020 { 1012 {
1021 /* maybe refused / unsupported address, try next */ 1013 /* maybe refused / unsupported address, try next */
1022 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); 1014 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
1023 LOG (GNUNET_ERROR_TYPE_INFO, 1015 LOG (GNUNET_ERROR_TYPE_INFO, _("Attempt to connect to `%s' failed\n"),
1024 _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr, 1016 GNUNET_a2s (serv_addr, addrlen));
1025 addrlen)); 1017 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1026 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); 1018 return NULL;
1027 return NULL; 1019 }
1028 }
1029 ret = GNUNET_CONNECTION_create_from_existing (s); 1020 ret = GNUNET_CONNECTION_create_from_existing (s);
1030 ret->addr = GNUNET_malloc (addrlen); 1021 ret->addr = GNUNET_malloc (addrlen);
1031 memcpy (ret->addr, serv_addr, addrlen); 1022 memcpy (ret->addr, serv_addr, addrlen);
@@ -1050,7 +1041,7 @@ int
1050GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock) 1041GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1051{ 1042{
1052 if ((sock->ap_head != NULL) || (sock->dns_active != NULL)) 1043 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1053 return GNUNET_YES; /* still trying to connect */ 1044 return GNUNET_YES; /* still trying to connect */
1054 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES; 1045 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1055} 1046}
1056 1047
@@ -1070,23 +1061,23 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1070 */ 1061 */
1071void 1062void
1072GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock, 1063GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1073 int finish_pending_write) 1064 int finish_pending_write)
1074{ 1065{
1075 if (GNUNET_NO == finish_pending_write) 1066 if (GNUNET_NO == finish_pending_write)
1067 {
1068 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1076 { 1069 {
1077 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 1070 GNUNET_SCHEDULER_cancel (sock->write_task);
1078 { 1071 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1079 GNUNET_SCHEDULER_cancel (sock->write_task); 1072 sock->write_buffer_off = 0;
1080 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1081 sock->write_buffer_off = 0;
1082 }
1083 sock->nth.notify_ready = NULL;
1084 } 1073 }
1074 sock->nth.notify_ready = NULL;
1075 }
1085 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL)) 1076 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1086 { 1077 {
1087 GNUNET_RESOLVER_request_cancel (sock->dns_active); 1078 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1088 sock->dns_active = NULL; 1079 sock->dns_active = NULL;
1089 } 1080 }
1090 1081
1091 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 1082 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1092 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock); 1083 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
@@ -1102,8 +1093,8 @@ signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1102 GNUNET_CONNECTION_Receiver receiver; 1093 GNUNET_CONNECTION_Receiver receiver;
1103 1094
1104#if DEBUG_CONNECTION 1095#if DEBUG_CONNECTION
1105 LOG (GNUNET_ERROR_TYPE_DEBUG, 1096 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network signals time out to receiver (%p)!\n",
1106 "Network signals time out to receiver (%p)!\n", sh); 1097 sh);
1107#endif 1098#endif
1108 GNUNET_assert (NULL != (receiver = sh->receiver)); 1099 GNUNET_assert (NULL != (receiver = sh->receiver));
1109 sh->receiver = NULL; 1100 sh->receiver = NULL;
@@ -1141,62 +1132,61 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1141 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1132 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1142 if ((GNUNET_YES == sh->ignore_shutdown) && 1133 if ((GNUNET_YES == sh->ignore_shutdown) &&
1143 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1134 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1144 { 1135 {
1145 /* ignore shutdown request, go again immediately */ 1136 /* ignore shutdown request, go again immediately */
1146#if DEBUG_CONNECTION 1137#if DEBUG_CONNECTION
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, 1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1148 "Ignoring shutdown signal per configuration\n"); 1139 "Ignoring shutdown signal per configuration\n");
1149#endif 1140#endif
1150 sh->read_task = 1141 sh->read_task =
1151 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1142 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1152 (sh->receive_timeout), sh->sock, 1143 (sh->receive_timeout), sh->sock,
1153 &receive_ready, sh); 1144 &receive_ready, sh);
1154 return; 1145 return;
1155 } 1146 }
1156 now = GNUNET_TIME_absolute_get (); 1147 now = GNUNET_TIME_absolute_get ();
1157 if ((now.abs_value > sh->receive_timeout.abs_value) || 1148 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1158 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) || 1149 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1159 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1150 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1160 { 1151 {
1161#if DEBUG_CONNECTION 1152#if DEBUG_CONNECTION
1162 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1153 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, 1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1164 "Receive from `%s' encounters error: time out by %llums... (%p)\n", 1155 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1165 GNUNET_a2s (sh->addr, sh->addrlen), 1156 GNUNET_a2s (sh->addr, sh->addrlen),
1166 GNUNET_TIME_absolute_get_duration (sh->receive_timeout). 1157 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).rel_value,
1167 rel_value, sh); 1158 sh);
1168#endif 1159#endif
1169 signal_timeout (sh); 1160 signal_timeout (sh);
1170 return; 1161 return;
1171 } 1162 }
1172 if (sh->sock == NULL) 1163 if (sh->sock == NULL)
1173 { 1164 {
1174 /* connect failed for good */ 1165 /* connect failed for good */
1175#if DEBUG_CONNECTION 1166#if DEBUG_CONNECTION
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, 1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1177 "Receive encounters error, socket closed... (%p)\n", sh); 1168 "Receive encounters error, socket closed... (%p)\n", sh);
1178#endif 1169#endif
1179 signal_error (sh, ECONNREFUSED); 1170 signal_error (sh, ECONNREFUSED);
1180 return; 1171 return;
1181 } 1172 }
1182 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock)); 1173 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1183RETRY: 1174RETRY:
1184 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max); 1175 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1185 if (ret == -1) 1176 if (ret == -1)
1186 { 1177 {
1187 if (errno == EINTR) 1178 if (errno == EINTR)
1188 goto RETRY; 1179 goto RETRY;
1189#if DEBUG_CONNECTION 1180#if DEBUG_CONNECTION
1190 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", 1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno));
1191 STRERROR (errno));
1192#endif 1182#endif
1193 signal_error (sh, errno); 1183 signal_error (sh, errno);
1194 return; 1184 return;
1195 } 1185 }
1196#if DEBUG_CONNECTION 1186#if DEBUG_CONNECTION
1197 LOG (GNUNET_ERROR_TYPE_DEBUG, 1187 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "receive_ready read %u/%u bytes from `%s' (%p)!\n", 1188 "receive_ready read %u/%u bytes from `%s' (%p)!\n", (unsigned int) ret,
1199 (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh); 1189 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1200#endif 1190#endif
1201 GNUNET_assert (NULL != (receiver = sh->receiver)); 1191 GNUNET_assert (NULL != (receiver = sh->receiver));
1202 sh->receiver = NULL; 1192 sh->receiver = NULL;
@@ -1222,32 +1212,32 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1222 1212
1223 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1213 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1224 if (sh->sock == NULL) 1214 if (sh->sock == NULL)
1225 { 1215 {
1226 /* not connected and no longer trying */ 1216 /* not connected and no longer trying */
1227#if DEBUG_CONNECTION 1217#if DEBUG_CONNECTION
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, 1218 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "Receive encounters error, socket closed (%p)...\n", sh); 1219 "Receive encounters error, socket closed (%p)...\n", sh);
1230#endif 1220#endif
1231 signal_error (sh, ECONNREFUSED); 1221 signal_error (sh, ECONNREFUSED);
1232 return; 1222 return;
1233 } 1223 }
1234 now = GNUNET_TIME_absolute_get (); 1224 now = GNUNET_TIME_absolute_get ();
1235 if ((now.abs_value > sh->receive_timeout.abs_value) || 1225 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1236 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1226 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1237 { 1227 {
1238#if DEBUG_CONNECTION 1228#if DEBUG_CONNECTION
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, 1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "Receive encounters error: time out (%p)...\n", sh); 1230 "Receive encounters error: time out (%p)...\n", sh);
1241#endif 1231#endif
1242 signal_timeout (sh); 1232 signal_timeout (sh);
1243 return; 1233 return;
1244 } 1234 }
1245 GNUNET_assert (sh->sock != NULL); 1235 GNUNET_assert (sh->sock != NULL);
1246 /* connect succeeded, wait for data! */ 1236 /* connect succeeded, wait for data! */
1247 sh->read_task = 1237 sh->read_task =
1248 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1238 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1249 (sh->receive_timeout), sh->sock, 1239 (sh->receive_timeout), sh->sock,
1250 &receive_ready, sh); 1240 &receive_ready, sh);
1251} 1241}
1252 1242
1253 1243
@@ -1266,31 +1256,31 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1266 */ 1256 */
1267void 1257void
1268GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max, 1258GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1269 struct GNUNET_TIME_Relative timeout, 1259 struct GNUNET_TIME_Relative timeout,
1270 GNUNET_CONNECTION_Receiver receiver, 1260 GNUNET_CONNECTION_Receiver receiver,
1271 void *receiver_cls) 1261 void *receiver_cls)
1272{ 1262{
1273 struct GNUNET_SCHEDULER_TaskContext tc; 1263 struct GNUNET_SCHEDULER_TaskContext tc;
1274 1264
1275 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) && 1265 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1276 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) && 1266 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1277 (sock->receiver == NULL)); 1267 (sock->receiver == NULL));
1278 sock->receiver = receiver; 1268 sock->receiver = receiver;
1279 sock->receiver_cls = receiver_cls; 1269 sock->receiver_cls = receiver_cls;
1280 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 1270 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1281 sock->max = max; 1271 sock->max = max;
1282 if (sock->sock != NULL) 1272 if (sock->sock != NULL)
1283 { 1273 {
1284 memset (&tc, 0, sizeof (tc)); 1274 memset (&tc, 0, sizeof (tc));
1285 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; 1275 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1286 receive_again (sock, &tc); 1276 receive_again (sock, &tc);
1287 return; 1277 return;
1288 } 1278 }
1289 if ((sock->dns_active == NULL) && (sock->ap_head == NULL)) 1279 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1290 { 1280 {
1291 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); 1281 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1292 return; 1282 return;
1293 } 1283 }
1294 sock->ccs += COCO_RECEIVE_AGAIN; 1284 sock->ccs += COCO_RECEIVE_AGAIN;
1295} 1285}
1296 1286
@@ -1303,7 +1293,7 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1303 */ 1293 */
1304void 1294void
1305GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock, 1295GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1306 int do_ignore) 1296 int do_ignore)
1307{ 1297{
1308 sock->ignore_shutdown = do_ignore; 1298 sock->ignore_shutdown = do_ignore;
1309} 1299}
@@ -1321,15 +1311,15 @@ void *
1321GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock) 1311GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1322{ 1312{
1323 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1313 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1324 { 1314 {
1325 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task)); 1315 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1326 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1316 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1327 } 1317 }
1328 else 1318 else
1329 { 1319 {
1330 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN)); 1320 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1331 sock->ccs -= COCO_RECEIVE_AGAIN; 1321 sock->ccs -= COCO_RECEIVE_AGAIN;
1332 } 1322 }
1333 sock->receiver = NULL; 1323 sock->receiver = NULL;
1334 return sock->receiver_cls; 1324 return sock->receiver_cls;
1335} 1325}
@@ -1360,18 +1350,18 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
1360 return GNUNET_NO; 1350 return GNUNET_NO;
1361 sock->nth.notify_ready = NULL; 1351 sock->nth.notify_ready = NULL;
1362 if (sock->write_buffer_size - sock->write_buffer_off < size) 1352 if (sock->write_buffer_size - sock->write_buffer_off < size)
1363 { 1353 {
1364 /* need to compact */ 1354 /* need to compact */
1365 memmove (sock->write_buffer, 1355 memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos],
1366 &sock->write_buffer[sock->write_buffer_pos], used); 1356 used);
1367 sock->write_buffer_off -= sock->write_buffer_pos; 1357 sock->write_buffer_off -= sock->write_buffer_pos;
1368 sock->write_buffer_pos = 0; 1358 sock->write_buffer_pos = 0;
1369 } 1359 }
1370 avail = sock->write_buffer_size - sock->write_buffer_off; 1360 avail = sock->write_buffer_size - sock->write_buffer_off;
1371 GNUNET_assert (avail >= size); 1361 GNUNET_assert (avail >= size);
1372 size = 1362 size =
1373 notify (sock->nth.notify_ready_cls, avail, 1363 notify (sock->nth.notify_ready_cls, avail,
1374 &sock->write_buffer[sock->write_buffer_off]); 1364 &sock->write_buffer[sock->write_buffer_off]);
1375 GNUNET_assert (size <= avail); 1365 GNUNET_assert (size <= avail);
1376 sock->write_buffer_off += size; 1366 sock->write_buffer_off += size;
1377 return GNUNET_YES; 1367 return GNUNET_YES;
@@ -1401,12 +1391,11 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1401 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 1391 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1402#if DEBUG_CONNECTION 1392#if DEBUG_CONNECTION
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, 1393 LOG (GNUNET_ERROR_TYPE_DEBUG,
1404 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", 1394 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", sock->hostname,
1405 sock->hostname, sock->port, GNUNET_a2s (sock->addr, 1395 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1406 sock->addrlen), sock);
1407#endif 1396#endif
1408 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY)); 1397 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1409 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */ 1398 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1410 notify = sock->nth.notify_ready; 1399 notify = sock->nth.notify_ready;
1411 sock->nth.notify_ready = NULL; 1400 sock->nth.notify_ready = NULL;
1412 notify (sock->nth.notify_ready_cls, 0, NULL); 1401 notify (sock->nth.notify_ready_cls, 0, NULL);
@@ -1451,20 +1440,20 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1451 GNUNET_CONNECTION_TransmitReadyNotify notify; 1440 GNUNET_CONNECTION_TransmitReadyNotify notify;
1452 1441
1453 if (NULL != sock->sock) 1442 if (NULL != sock->sock)
1454 { 1443 {
1455 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); 1444 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1456 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 1445 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
1457 sock->sock = NULL; 1446 sock->sock = NULL;
1458 } 1447 }
1459 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1448 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1460 { 1449 {
1461 GNUNET_SCHEDULER_cancel (sock->read_task); 1450 GNUNET_SCHEDULER_cancel (sock->read_task);
1462 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1451 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1463 signal_timeout (sock); 1452 signal_timeout (sock);
1464 return; 1453 return;
1465 } 1454 }
1466 if (sock->nth.notify_ready == NULL) 1455 if (sock->nth.notify_ready == NULL)
1467 return; /* nobody to tell about it */ 1456 return; /* nobody to tell about it */
1468 notify = sock->nth.notify_ready; 1457 notify = sock->nth.notify_ready;
1469 sock->nth.notify_ready = NULL; 1458 sock->nth.notify_ready = NULL;
1470 notify (sock->nth.notify_ready_cls, 0, NULL); 1459 notify (sock->nth.notify_ready_cls, 0, NULL);
@@ -1494,109 +1483,107 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1494 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1483 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1495 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 1484 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1496 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1485 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1497 { 1486 {
1498 if ( (sock->ignore_shutdown == GNUNET_YES) && 1487 if ((sock->ignore_shutdown == GNUNET_YES) && (NULL != sock->sock))
1499 (NULL != sock->sock) ) 1488 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1500 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1501#if DEBUG_CONNECTION 1489#if DEBUG_CONNECTION
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, 1490 LOG (GNUNET_ERROR_TYPE_DEBUG,
1503 "Transmit to `%s' fails, shutdown happened (%p).\n", 1491 "Transmit to `%s' fails, shutdown happened (%p).\n",
1504 GNUNET_a2s (sock->addr, sock->addrlen), sock); 1492 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1505#endif 1493#endif
1506 notify = sock->nth.notify_ready; 1494 notify = sock->nth.notify_ready;
1507 if (NULL != notify) 1495 if (NULL != notify)
1508 {
1509 sock->nth.notify_ready = NULL;
1510 notify (sock->nth.notify_ready_cls, 0, NULL);
1511 }
1512 return;
1513 }
1514 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1515 { 1496 {
1516#if DEBUG_CONNECTION
1517 LOG (GNUNET_ERROR_TYPE_DEBUG,
1518 "Transmit to `%s' fails, time out reached (%p).\n",
1519 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1520#endif
1521 notify = sock->nth.notify_ready;
1522 GNUNET_assert (NULL != notify);
1523 sock->nth.notify_ready = NULL; 1497 sock->nth.notify_ready = NULL;
1524 notify (sock->nth.notify_ready_cls, 0, NULL); 1498 notify (sock->nth.notify_ready_cls, 0, NULL);
1525 return;
1526 } 1499 }
1500 return;
1501 }
1502 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1503 {
1504#if DEBUG_CONNECTION
1505 LOG (GNUNET_ERROR_TYPE_DEBUG,
1506 "Transmit to `%s' fails, time out reached (%p).\n",
1507 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1508#endif
1509 notify = sock->nth.notify_ready;
1510 GNUNET_assert (NULL != notify);
1511 sock->nth.notify_ready = NULL;
1512 notify (sock->nth.notify_ready_cls, 0, NULL);
1513 return;
1514 }
1527 GNUNET_assert (NULL != sock->sock); 1515 GNUNET_assert (NULL != sock->sock);
1528 if (tc->write_ready == NULL) 1516 if (tc->write_ready == NULL)
1529 { 1517 {
1530 /* special circumstances (in particular, 1518 /* special circumstances (in particular,
1531 * PREREQ_DONE after connect): not yet ready to write, 1519 * PREREQ_DONE after connect): not yet ready to write,
1532 * but no "fatal" error either. Hence retry. */ 1520 * but no "fatal" error either. Hence retry. */
1533 goto SCHEDULE_WRITE; 1521 goto SCHEDULE_WRITE;
1534 } 1522 }
1535 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)) 1523 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1536 { 1524 {
1537#if DEBUG_CONNECTION 1525#if DEBUG_CONNECTION
1538 LOG (GNUNET_ERROR_TYPE_INFO, 1526 LOG (GNUNET_ERROR_TYPE_INFO,
1539 _ 1527 _
1540 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"), 1528 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1541 sock); 1529 sock);
1542#endif 1530#endif
1543 transmit_error (sock); 1531 transmit_error (sock);
1544 return; /* connect failed for good, we're finished */ 1532 return; /* connect failed for good, we're finished */
1545 } 1533 }
1546 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos); 1534 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1547 if ((sock->nth.notify_ready != NULL) && 1535 if ((sock->nth.notify_ready != NULL) &&
1548 (sock->write_buffer_size < sock->nth.notify_size)) 1536 (sock->write_buffer_size < sock->nth.notify_size))
1549 { 1537 {
1550 sock->write_buffer = 1538 sock->write_buffer =
1551 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size); 1539 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
1552 sock->write_buffer_size = sock->nth.notify_size; 1540 sock->write_buffer_size = sock->nth.notify_size;
1553 } 1541 }
1554 process_notify (sock); 1542 process_notify (sock);
1555 have = sock->write_buffer_off - sock->write_buffer_pos; 1543 have = sock->write_buffer_off - sock->write_buffer_pos;
1556 if (have == 0) 1544 if (have == 0)
1557 { 1545 {
1558 /* no data ready for writing, terminate write loop */ 1546 /* no data ready for writing, terminate write loop */
1559 return; 1547 return;
1560 } 1548 }
1561 GNUNET_assert (have <= sock->write_buffer_size); 1549 GNUNET_assert (have <= sock->write_buffer_size);
1562 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size); 1550 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1563 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size); 1551 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1564RETRY: 1552RETRY:
1565 ret = 1553 ret =
1566 GNUNET_NETWORK_socket_send (sock->sock, 1554 GNUNET_NETWORK_socket_send (sock->sock,
1567 &sock->write_buffer[sock->write_buffer_pos], 1555 &sock->write_buffer[sock->write_buffer_pos],
1568 have); 1556 have);
1569 if (ret == -1) 1557 if (ret == -1)
1570 { 1558 {
1571 if (errno == EINTR) 1559 if (errno == EINTR)
1572 goto RETRY; 1560 goto RETRY;
1573#if 0 1561#if 0
1574 int en = errno; 1562 int en = errno;
1575 1563
1576 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"), 1564 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"),
1577 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en)); 1565 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en));
1578#endif 1566#endif
1579#if DEBUG_CONNECTION 1567#if DEBUG_CONNECTION
1580 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send"); 1568 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1581#endif 1569#endif
1582 transmit_error (sock); 1570 transmit_error (sock);
1583 return; 1571 return;
1584 } 1572 }
1585#if DEBUG_CONNECTION 1573#if DEBUG_CONNECTION
1586 LOG (GNUNET_ERROR_TYPE_DEBUG, 1574 LOG (GNUNET_ERROR_TYPE_DEBUG,
1587 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", 1575 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1588 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), 1576 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1589 sock);
1590#endif 1577#endif
1591 sock->write_buffer_pos += ret; 1578 sock->write_buffer_pos += ret;
1592 if (sock->write_buffer_pos == sock->write_buffer_off) 1579 if (sock->write_buffer_pos == sock->write_buffer_off)
1593 { 1580 {
1594 /* transmitted all pending data */ 1581 /* transmitted all pending data */
1595 sock->write_buffer_pos = 0; 1582 sock->write_buffer_pos = 0;
1596 sock->write_buffer_off = 0; 1583 sock->write_buffer_off = 0;
1597 } 1584 }
1598 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready)) 1585 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1599 return; /* all data sent! */ 1586 return; /* all data sent! */
1600 /* not done writing, schedule more */ 1587 /* not done writing, schedule more */
1601SCHEDULE_WRITE: 1588SCHEDULE_WRITE:
1602#if DEBUG_CONNECTION 1589#if DEBUG_CONNECTION
@@ -1607,11 +1594,11 @@ SCHEDULE_WRITE:
1607 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0)); 1594 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
1608 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) 1595 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1609 sock->write_task = 1596 sock->write_task =
1610 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == 1597 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
1611 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : 1598 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1612 GNUNET_TIME_absolute_get_remaining 1599 GNUNET_TIME_absolute_get_remaining
1613 (sock->nth.transmit_timeout), 1600 (sock->nth.transmit_timeout),
1614 sock->sock, &transmit_ready, sock); 1601 sock->sock, &transmit_ready, sock);
1615} 1602}
1616 1603
1617 1604
@@ -1630,17 +1617,17 @@ SCHEDULE_WRITE:
1630 * NULL if we are already going to notify someone else (busy) 1617 * NULL if we are already going to notify someone else (busy)
1631 */ 1618 */
1632struct GNUNET_CONNECTION_TransmitHandle * 1619struct GNUNET_CONNECTION_TransmitHandle *
1633GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle 1620GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1634 *sock, size_t size, 1621 size_t size,
1635 struct GNUNET_TIME_Relative timeout, 1622 struct GNUNET_TIME_Relative timeout,
1636 GNUNET_CONNECTION_TransmitReadyNotify 1623 GNUNET_CONNECTION_TransmitReadyNotify
1637 notify, void *notify_cls) 1624 notify, void *notify_cls)
1638{ 1625{
1639 if (sock->nth.notify_ready != NULL) 1626 if (sock->nth.notify_ready != NULL)
1640 { 1627 {
1641 GNUNET_assert (0); 1628 GNUNET_assert (0);
1642 return NULL; 1629 return NULL;
1643 } 1630 }
1644 GNUNET_assert (notify != NULL); 1631 GNUNET_assert (notify != NULL);
1645 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); 1632 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1646 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size); 1633 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
@@ -1654,36 +1641,34 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1654 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task); 1641 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1655 if ((sock->sock == NULL) && (sock->ap_head == NULL) && 1642 if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
1656 (sock->dns_active == NULL)) 1643 (sock->dns_active == NULL))
1657 { 1644 {
1658 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 1645 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1659 GNUNET_SCHEDULER_cancel (sock->write_task); 1646 GNUNET_SCHEDULER_cancel (sock->write_task);
1660 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock); 1647 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1661 return &sock->nth; 1648 return &sock->nth;
1662 } 1649 }
1663 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task) 1650 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1664 return &sock->nth; 1651 return &sock->nth;
1665 if (sock->sock != NULL) 1652 if (sock->sock != NULL)
1666 { 1653 {
1667#if DEBUG_CONNECTION 1654#if DEBUG_CONNECTION
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", 1655 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", sock);
1669 sock);
1670#endif 1656#endif
1671 sock->write_task = 1657 sock->write_task =
1672 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 1658 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1673 (sock->nth.transmit_timeout), 1659 (sock->nth.transmit_timeout),
1674 sock->sock, &transmit_ready, sock); 1660 sock->sock, &transmit_ready, sock);
1675 } 1661 }
1676 else 1662 else
1677 { 1663 {
1678#if DEBUG_CONNECTION 1664#if DEBUG_CONNECTION
1679 LOG (GNUNET_ERROR_TYPE_DEBUG, 1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1680 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", 1666 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", sock);
1681 sock);
1682#endif 1667#endif
1683 sock->ccs |= COCO_TRANSMIT_READY; 1668 sock->ccs |= COCO_TRANSMIT_READY;
1684 sock->nth.timeout_task = 1669 sock->nth.timeout_task =
1685 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock); 1670 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
1686 } 1671 }
1687 return &sock->nth; 1672 return &sock->nth;
1688} 1673}
1689 1674
@@ -1695,28 +1680,28 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1695 */ 1680 */
1696void 1681void
1697GNUNET_CONNECTION_notify_transmit_ready_cancel (struct 1682GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1698 GNUNET_CONNECTION_TransmitHandle 1683 GNUNET_CONNECTION_TransmitHandle
1699 *th) 1684 *th)
1700{ 1685{
1701 GNUNET_assert (th->notify_ready != NULL); 1686 GNUNET_assert (th->notify_ready != NULL);
1702 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY)) 1687 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
1703 { 1688 {
1704#if DEBUG_CONNECTION 1689#if DEBUG_CONNECTION
1705 LOG (GNUNET_ERROR_TYPE_DEBUG, 1690 LOG (GNUNET_ERROR_TYPE_DEBUG,
1706 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th); 1691 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1707#endif 1692#endif
1708 GNUNET_SCHEDULER_cancel (th->timeout_task); 1693 GNUNET_SCHEDULER_cancel (th->timeout_task);
1709 th->timeout_task = GNUNET_SCHEDULER_NO_TASK; 1694 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1710 th->sh->ccs -= COCO_TRANSMIT_READY; 1695 th->sh->ccs -= COCO_TRANSMIT_READY;
1711 } 1696 }
1712 else 1697 else
1698 {
1699 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1713 { 1700 {
1714 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK) 1701 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1715 { 1702 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1716 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1717 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1718 }
1719 } 1703 }
1704 }
1720 th->notify_ready = NULL; 1705 th->notify_ready = NULL;
1721} 1706}
1722 1707
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index a33b619ff..180aab4c3 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -89,8 +89,8 @@ struct GNUNET_CONTAINER_BloomFilter
89 * @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
90 */ 90 */
91size_t 91size_t
92GNUNET_CONTAINER_bloomfilter_get_size (const struct 92GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
93 GNUNET_CONTAINER_BloomFilter *bf) 93 *bf)
94{ 94{
95 if (bf == NULL) 95 if (bf == NULL)
96 return 0; 96 return 0;
@@ -106,10 +106,10 @@ GNUNET_CONTAINER_bloomfilter_get_size (const struct
106 */ 106 */
107struct GNUNET_CONTAINER_BloomFilter * 107struct GNUNET_CONTAINER_BloomFilter *
108GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter 108GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter
109 *bf) 109 *bf)
110{ 110{
111 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize, 111 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize,
112 bf->addressesPerElement); 112 bf->addressesPerElement);
113} 113}
114 114
115 115
@@ -181,7 +181,7 @@ testBit (char *bitArray, unsigned int bitIdx)
181 */ 181 */
182static void 182static void
183incrementBit (char *bitArray, unsigned int bitIdx, 183incrementBit (char *bitArray, unsigned int bitIdx,
184 const struct GNUNET_DISK_FileHandle *fh) 184 const struct GNUNET_DISK_FileHandle *fh)
185{ 185{
186 off_t fileSlot; 186 off_t fileSlot;
187 unsigned char value; 187 unsigned char value;
@@ -197,25 +197,25 @@ incrementBit (char *bitArray, unsigned int bitIdx,
197 targetLoc = bitIdx % 2; 197 targetLoc = bitIdx % 2;
198 198
199 GNUNET_assert (fileSlot == 199 GNUNET_assert (fileSlot ==
200 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); 200 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
201 if (1 != GNUNET_DISK_file_read (fh, &value, 1)) 201 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
202 value = 0; 202 value = 0;
203 low = value & 0xF; 203 low = value & 0xF;
204 high = (value & (~0xF)) >> 4; 204 high = (value & (~0xF)) >> 4;
205 205
206 if (targetLoc == 0) 206 if (targetLoc == 0)
207 { 207 {
208 if (low < 0xF) 208 if (low < 0xF)
209 low++; 209 low++;
210 } 210 }
211 else 211 else
212 { 212 {
213 if (high < 0xF) 213 if (high < 0xF)
214 high++; 214 high++;
215 } 215 }
216 value = ((high << 4) | low); 216 value = ((high << 4) | low);
217 GNUNET_assert (fileSlot == 217 GNUNET_assert (fileSlot ==
218 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET)); 218 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
219 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1)); 219 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
220} 220}
221 221
@@ -229,7 +229,7 @@ incrementBit (char *bitArray, unsigned int bitIdx,
229 */ 229 */
230static void 230static void
231decrementBit (char *bitArray, unsigned int bitIdx, 231decrementBit (char *bitArray, unsigned int bitIdx,
232 const struct GNUNET_DISK_FileHandle *fh) 232 const struct GNUNET_DISK_FileHandle *fh)
233{ 233{
234 off_t fileSlot; 234 off_t fileSlot;
235 unsigned char value; 235 unsigned char value;
@@ -238,7 +238,7 @@ decrementBit (char *bitArray, unsigned int bitIdx,
238 unsigned int targetLoc; 238 unsigned int targetLoc;
239 239
240 if (GNUNET_DISK_handle_invalid (fh)) 240 if (GNUNET_DISK_handle_invalid (fh))
241 return; /* cannot decrement! */ 241 return; /* cannot decrement! */
242 /* 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 */
243 fileSlot = bitIdx / 2; 243 fileSlot = bitIdx / 2;
244 targetLoc = bitIdx % 2; 244 targetLoc = bitIdx % 2;
@@ -250,23 +250,23 @@ decrementBit (char *bitArray, unsigned int bitIdx,
250 250
251 /* decrement, but once we have reached the max, never go back! */ 251 /* decrement, but once we have reached the max, never go back! */
252 if (targetLoc == 0) 252 if (targetLoc == 0)
253 {
254 if ((low > 0) && (low < 0xF))
255 low--;
256 if (low == 0)
253 { 257 {
254 if ((low > 0) && (low < 0xF)) 258 clearBit (bitArray, bitIdx);
255 low--;
256 if (low == 0)
257 {
258 clearBit (bitArray, bitIdx);
259 }
260 } 259 }
260 }
261 else 261 else
262 {
263 if ((high > 0) && (high < 0xF))
264 high--;
265 if (high == 0)
262 { 266 {
263 if ((high > 0) && (high < 0xF)) 267 clearBit (bitArray, bitIdx);
264 high--;
265 if (high == 0)
266 {
267 clearBit (bitArray, bitIdx);
268 }
269 } 268 }
269 }
270 value = ((high << 4) | low); 270 value = ((high << 4) | low);
271 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET); 271 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
272 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1)); 272 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
@@ -295,19 +295,19 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
295 GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET); 295 GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
296 296
297 while (bytesleft > 0) 297 while (bytesleft > 0)
298 {
299 if (bytesleft > BUFFSIZE)
298 { 300 {
299 if (bytesleft > BUFFSIZE) 301 res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE);
300 { 302 bytesleft -= BUFFSIZE;
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);
310 } 303 }
304 else
305 {
306 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
307 bytesleft = 0;
308 }
309 GNUNET_assert (res != GNUNET_SYSERR);
310 }
311 GNUNET_free (buffer); 311 GNUNET_free (buffer);
312 return GNUNET_OK; 312 return GNUNET_OK;
313} 313}
@@ -325,8 +325,8 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
325 * @return GNUNET_YES to continue, GNUNET_NO to stop early 325 * @return GNUNET_YES to continue, GNUNET_NO to stop early
326 */ 326 */
327typedef int (*BitIterator) (void *cls, 327typedef int (*BitIterator) (void *cls,
328 const struct GNUNET_CONTAINER_BloomFilter * bf, 328 const struct GNUNET_CONTAINER_BloomFilter * bf,
329 unsigned int bit); 329 unsigned int bit);
330 330
331 331
332/** 332/**
@@ -340,7 +340,7 @@ typedef int (*BitIterator) (void *cls,
340 */ 340 */
341static void 341static void
342iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf, 342iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
343 BitIterator callback, void *arg, const GNUNET_HashCode * key) 343 BitIterator callback, void *arg, const GNUNET_HashCode * key)
344{ 344{
345 GNUNET_HashCode tmp[2]; 345 GNUNET_HashCode tmp[2];
346 int bitCount; 346 int bitCount;
@@ -351,27 +351,27 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
351 tmp[0] = *key; 351 tmp[0] = *key;
352 round = 0; 352 round = 0;
353 while (bitCount > 0) 353 while (bitCount > 0)
354 {
355 while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
354 { 356 {
355 while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t))) 357 if (GNUNET_YES !=
356 { 358 callback (arg, bf,
357 if (GNUNET_YES != 359 (((uint32_t *) & tmp[round & 1])[slot]) &
358 callback (arg, bf, 360 ((bf->bitArraySize * 8) - 1)))
359 (((uint32_t *) & tmp[round & 1])[slot]) & 361 return;
360 ((bf->bitArraySize * 8) - 1))) 362 slot++;
361 return; 363 bitCount--;
362 slot++; 364 if (bitCount == 0)
363 bitCount--; 365 break;
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 }
374 } 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 }
374 }
375} 375}
376 376
377 377
@@ -384,9 +384,8 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
384 * @return GNUNET_YES 384 * @return GNUNET_YES
385 */ 385 */
386static int 386static int
387incrementBitCallback (void *cls, 387incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
388 const struct GNUNET_CONTAINER_BloomFilter *bf, 388 unsigned int bit)
389 unsigned int bit)
390{ 389{
391 struct GNUNET_CONTAINER_BloomFilter *b = cls; 390 struct GNUNET_CONTAINER_BloomFilter *b = cls;
392 391
@@ -404,9 +403,8 @@ incrementBitCallback (void *cls,
404 * @return GNUNET_YES 403 * @return GNUNET_YES
405 */ 404 */
406static int 405static int
407decrementBitCallback (void *cls, 406decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
408 const struct GNUNET_CONTAINER_BloomFilter *bf, 407 unsigned int bit)
409 unsigned int bit)
410{ 408{
411 struct GNUNET_CONTAINER_BloomFilter *b = cls; 409 struct GNUNET_CONTAINER_BloomFilter *b = cls;
412 410
@@ -425,15 +423,15 @@ decrementBitCallback (void *cls,
425 */ 423 */
426static int 424static int
427testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, 425testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
428 unsigned int bit) 426 unsigned int bit)
429{ 427{
430 int *arg = cls; 428 int *arg = cls;
431 429
432 if (GNUNET_NO == testBit (bf->bitArray, bit)) 430 if (GNUNET_NO == testBit (bf->bitArray, bit))
433 { 431 {
434 *arg = GNUNET_NO; 432 *arg = GNUNET_NO;
435 return GNUNET_NO; 433 return GNUNET_NO;
436 } 434 }
437 return GNUNET_YES; 435 return GNUNET_YES;
438} 436}
439 437
@@ -451,7 +449,7 @@ testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
451 */ 449 */
452struct GNUNET_CONTAINER_BloomFilter * 450struct GNUNET_CONTAINER_BloomFilter *
453GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, 451GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
454 unsigned int k) 452 unsigned int k)
455{ 453{
456 struct GNUNET_CONTAINER_BloomFilter *bf; 454 struct GNUNET_CONTAINER_BloomFilter *bf;
457 char *rbuff; 455 char *rbuff;
@@ -467,32 +465,32 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
467 ui = 1; 465 ui = 1;
468 while (ui < size) 466 while (ui < size)
469 ui *= 2; 467 ui *= 2;
470 size = ui; /* make sure it's a power of 2 */ 468 size = ui; /* make sure it's a power of 2 */
471 469
472 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); 470 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
473 /* Try to open a bloomfilter file */ 471 /* Try to open a bloomfilter file */
474 bf->fh = 472 bf->fh =
475 GNUNET_DISK_file_open (filename, 473 GNUNET_DISK_file_open (filename,
476 GNUNET_DISK_OPEN_READWRITE | 474 GNUNET_DISK_OPEN_READWRITE |
477 GNUNET_DISK_OPEN_CREATE, 475 GNUNET_DISK_OPEN_CREATE,
478 GNUNET_DISK_PERM_USER_READ | 476 GNUNET_DISK_PERM_USER_READ |
479 GNUNET_DISK_PERM_USER_WRITE); 477 GNUNET_DISK_PERM_USER_WRITE);
480 if (NULL == bf->fh) 478 if (NULL == bf->fh)
481 { 479 {
482 GNUNET_free (bf); 480 GNUNET_free (bf);
483 return NULL; 481 return NULL;
484 } 482 }
485 bf->filename = GNUNET_strdup (filename); 483 bf->filename = GNUNET_strdup (filename);
486 /* Alloc block */ 484 /* Alloc block */
487 bf->bitArray = GNUNET_malloc_large (size); 485 bf->bitArray = GNUNET_malloc_large (size);
488 if (bf->bitArray == NULL) 486 if (bf->bitArray == NULL)
489 { 487 {
490 if (bf->fh != NULL) 488 if (bf->fh != NULL)
491 GNUNET_DISK_file_close (bf->fh); 489 GNUNET_DISK_file_close (bf->fh);
492 GNUNET_free (bf->filename); 490 GNUNET_free (bf->filename);
493 GNUNET_free (bf); 491 GNUNET_free (bf);
494 return NULL; 492 return NULL;
495 } 493 }
496 bf->bitArraySize = size; 494 bf->bitArraySize = size;
497 bf->addressesPerElement = k; 495 bf->addressesPerElement = k;
498 memset (bf->bitArray, 0, bf->bitArraySize); 496 memset (bf->bitArray, 0, bf->bitArraySize);
@@ -501,27 +499,27 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
501 rbuff = GNUNET_malloc (BUFFSIZE); 499 rbuff = GNUNET_malloc (BUFFSIZE);
502 pos = 0; 500 pos = 0;
503 while (pos < size * 8) 501 while (pos < size * 8)
502 {
503 int res;
504
505 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
506 if (res == -1)
507 {
508 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
509 }
510 if (res == 0)
511 break; /* is ok! we just did not use that many bits yet */
512 for (i = 0; i < res; i++)
504 { 513 {
505 int res; 514 if ((rbuff[i] & 0x0F) != 0)
506 515 setBit (bf->bitArray, pos + i * 2);
507 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE); 516 if ((rbuff[i] & 0xF0) != 0)
508 if (res == -1) 517 setBit (bf->bitArray, pos + i * 2 + 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 */
524 } 518 }
519 if (res < BUFFSIZE)
520 break;
521 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
522 }
525 GNUNET_free (rbuff); 523 GNUNET_free (rbuff);
526 return bf; 524 return bf;
527} 525}
@@ -542,7 +540,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
542 */ 540 */
543struct GNUNET_CONTAINER_BloomFilter * 541struct GNUNET_CONTAINER_BloomFilter *
544GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size, 542GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
545 unsigned int k) 543 unsigned int k)
546{ 544{
547 struct GNUNET_CONTAINER_BloomFilter *bf; 545 struct GNUNET_CONTAINER_BloomFilter *bf;
548 size_t ui; 546 size_t ui;
@@ -553,19 +551,19 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
553 while (ui < size) 551 while (ui < size)
554 ui *= 2; 552 ui *= 2;
555 if (size != ui) 553 if (size != ui)
556 { 554 {
557 GNUNET_break (0); 555 GNUNET_break (0);
558 return NULL; 556 return NULL;
559 } 557 }
560 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); 558 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
561 bf->filename = NULL; 559 bf->filename = NULL;
562 bf->fh = NULL; 560 bf->fh = NULL;
563 bf->bitArray = GNUNET_malloc_large (size); 561 bf->bitArray = GNUNET_malloc_large (size);
564 if (bf->bitArray == NULL) 562 if (bf->bitArray == NULL)
565 { 563 {
566 GNUNET_free (bf); 564 GNUNET_free (bf);
567 return NULL; 565 return NULL;
568 } 566 }
569 bf->bitArraySize = size; 567 bf->bitArraySize = size;
570 bf->addressesPerElement = k; 568 bf->addressesPerElement = k;
571 if (data != NULL) 569 if (data != NULL)
@@ -587,8 +585,8 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
587 */ 585 */
588int 586int
589GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct 587GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
590 GNUNET_CONTAINER_BloomFilter *bf, 588 GNUNET_CONTAINER_BloomFilter *bf,
591 char *data, size_t size) 589 char *data, size_t size)
592{ 590{
593 if (NULL == bf) 591 if (NULL == bf)
594 return GNUNET_SYSERR; 592 return GNUNET_SYSERR;
@@ -645,7 +643,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
645 */ 643 */
646int 644int
647GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter 645GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
648 *bf, const GNUNET_HashCode * e) 646 *bf, const GNUNET_HashCode * e)
649{ 647{
650 int res; 648 int res;
651 649
@@ -665,7 +663,7 @@ GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
665 */ 663 */
666void 664void
667GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf, 665GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
668 const GNUNET_HashCode * e) 666 const GNUNET_HashCode * e)
669{ 667{
670 if (NULL == bf) 668 if (NULL == bf)
671 return; 669 return;
@@ -685,7 +683,7 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
685 */ 683 */
686int 684int
687GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf, 685GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
688 const char *data, size_t size) 686 const char *data, size_t size)
689{ 687{
690 unsigned int i; 688 unsigned int i;
691 unsigned int n; 689 unsigned int n;
@@ -719,8 +717,8 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
719 */ 717 */
720int 718int
721GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf, 719GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
722 const struct GNUNET_CONTAINER_BloomFilter 720 const struct GNUNET_CONTAINER_BloomFilter
723 *to_or, size_t size) 721 *to_or, size_t size)
724{ 722{
725 unsigned int i; 723 unsigned int i;
726 unsigned int n; 724 unsigned int n;
@@ -750,7 +748,7 @@ GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
750 */ 748 */
751void 749void
752GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf, 750GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
753 const GNUNET_HashCode * e) 751 const GNUNET_HashCode * e)
754{ 752{
755 if (NULL == bf) 753 if (NULL == bf)
756 return; 754 return;
@@ -772,9 +770,9 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
772 */ 770 */
773void 771void
774GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, 772GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
775 GNUNET_HashCodeIterator iterator, 773 GNUNET_HashCodeIterator iterator,
776 void *iterator_cls, size_t size, 774 void *iterator_cls, size_t size,
777 unsigned int k) 775 unsigned int k)
778{ 776{
779 GNUNET_HashCode hc; 777 GNUNET_HashCode hc;
780 unsigned int i; 778 unsigned int i;
@@ -783,7 +781,7 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
783 i = 1; 781 i = 1;
784 while (i < size) 782 while (i < size)
785 i *= 2; 783 i *= 2;
786 size = i; /* make sure it's a power of 2 */ 784 size = i; /* make sure it's a power of 2 */
787 785
788 bf->bitArraySize = size; 786 bf->bitArraySize = size;
789 bf->bitArray = GNUNET_malloc (size); 787 bf->bitArray = GNUNET_malloc (size);
diff --git a/src/util/container_heap.c b/src/util/container_heap.c
index 051b85a25..c34e220ce 100644
--- a/src/util/container_heap.c
+++ b/src/util/container_heap.c
@@ -116,10 +116,10 @@ check (const struct GNUNET_CONTAINER_HeapNode *node)
116 if (NULL == node) 116 if (NULL == node)
117 return; 117 return;
118 GNUNET_assert (node->tree_size == 118 GNUNET_assert (node->tree_size ==
119 ((node->left_child == 119 ((node->left_child ==
120 NULL) ? 0 : 1 + node->left_child->tree_size) + 120 NULL) ? 0 : 1 + node->left_child->tree_size) +
121 ((node->right_child == 121 ((node->right_child ==
122 NULL) ? 0 : 1 + node->right_child->tree_size)); 122 NULL) ? 0 : 1 + node->right_child->tree_size));
123 check (node->left_child); 123 check (node->left_child);
124 check (node->right_child); 124 check (node->right_child);
125} 125}
@@ -198,7 +198,7 @@ GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap)
198 */ 198 */
199GNUNET_CONTAINER_HeapCostType 199GNUNET_CONTAINER_HeapCostType
200GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode 200GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
201 * node) 201 *node)
202{ 202{
203 return node->cost; 203 return node->cost;
204} 204}
@@ -214,8 +214,8 @@ GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
214 */ 214 */
215static int 215static int
216node_iterator (const struct GNUNET_CONTAINER_Heap *heap, 216node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
217 struct GNUNET_CONTAINER_HeapNode *node, 217 struct GNUNET_CONTAINER_HeapNode *node,
218 GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls) 218 GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls)
219{ 219{
220 if (node == NULL) 220 if (node == NULL)
221 return GNUNET_YES; 221 return GNUNET_YES;
@@ -238,8 +238,8 @@ node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
238 */ 238 */
239void 239void
240GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap, 240GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
241 GNUNET_CONTAINER_HeapIterator iterator, 241 GNUNET_CONTAINER_HeapIterator iterator,
242 void *iterator_cls) 242 void *iterator_cls)
243{ 243{
244 (void) node_iterator (heap, heap->root, iterator, iterator_cls); 244 (void) node_iterator (heap, heap->root, iterator, iterator_cls);
245} 245}
@@ -269,9 +269,9 @@ GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
269 pos = heap->root; 269 pos = heap->root;
270 element = pos->element; 270 element = pos->element;
271 heap->walk_pos = 271 heap->walk_pos =
272 (0 == 272 (0 ==
273 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 273 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
274 2)) ? pos->right_child : pos->left_child; 274 2)) ? pos->right_child : pos->left_child;
275 return element; 275 return element;
276} 276}
277 277
@@ -286,51 +286,51 @@ GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
286 */ 286 */
287static void 287static void
288insert_node (struct GNUNET_CONTAINER_Heap *heap, 288insert_node (struct GNUNET_CONTAINER_Heap *heap,
289 struct GNUNET_CONTAINER_HeapNode *pos, 289 struct GNUNET_CONTAINER_HeapNode *pos,
290 struct GNUNET_CONTAINER_HeapNode *node) 290 struct GNUNET_CONTAINER_HeapNode *node)
291{ 291{
292 struct GNUNET_CONTAINER_HeapNode *parent; 292 struct GNUNET_CONTAINER_HeapNode *parent;
293 293
294 GNUNET_assert (node->parent == NULL); 294 GNUNET_assert (node->parent == NULL);
295 while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >= 295 while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >=
296 node->cost) 296 node->cost)
297 : (pos->cost <= node->cost)) 297 : (pos->cost <= node->cost))
298 {
299 /* node is descendent of pos */
300 pos->tree_size += (1 + node->tree_size);
301 if (pos->left_child == NULL)
298 { 302 {
299 /* node is descendent of pos */ 303 pos->left_child = node;
300 pos->tree_size += (1 + node->tree_size); 304 node->parent = pos;
301 if (pos->left_child == NULL) 305 return;
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;
318 } 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;
318 }
319 /* make 'node' parent of 'pos' */ 319 /* make 'node' parent of 'pos' */
320 parent = pos->parent; 320 parent = pos->parent;
321 pos->parent = NULL; 321 pos->parent = NULL;
322 node->parent = parent; 322 node->parent = parent;
323 if (NULL == parent) 323 if (NULL == parent)
324 { 324 {
325 heap->root = node; 325 heap->root = node;
326 } 326 }
327 else 327 else
328 { 328 {
329 if (parent->left_child == pos) 329 if (parent->left_child == pos)
330 parent->left_child = node; 330 parent->left_child = node;
331 else 331 else
332 parent->right_child = node; 332 parent->right_child = node;
333 } 333 }
334 /* insert 'pos' below 'node' */ 334 /* insert 'pos' below 'node' */
335 insert_node (heap, node, pos); 335 insert_node (heap, node, pos);
336 CHECK (pos); 336 CHECK (pos);
@@ -346,9 +346,8 @@ insert_node (struct GNUNET_CONTAINER_Heap *heap,
346 * @return node for the new element 346 * @return node for the new element
347 */ 347 */
348struct GNUNET_CONTAINER_HeapNode * 348struct GNUNET_CONTAINER_HeapNode *
349GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, 349GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
350 void *element, 350 GNUNET_CONTAINER_HeapCostType cost)
351 GNUNET_CONTAINER_HeapCostType cost)
352{ 351{
353 struct GNUNET_CONTAINER_HeapNode *node; 352 struct GNUNET_CONTAINER_HeapNode *node;
354 353
@@ -384,27 +383,27 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
384 heap->size--; 383 heap->size--;
385 ret = root->element; 384 ret = root->element;
386 if (root->left_child == NULL) 385 if (root->left_child == NULL)
387 { 386 {
388 heap->root = root->right_child; 387 heap->root = root->right_child;
389 if (root->right_child != NULL) 388 if (root->right_child != NULL)
390 root->right_child->parent = NULL; 389 root->right_child->parent = NULL;
391 } 390 }
392 else if (root->right_child == NULL) 391 else if (root->right_child == NULL)
393 { 392 {
394 heap->root = root->left_child; 393 heap->root = root->left_child;
395 root->left_child->parent = NULL; 394 root->left_child->parent = NULL;
396 } 395 }
397 else 396 else
398 { 397 {
399 root->left_child->parent = NULL; 398 root->left_child->parent = NULL;
400 root->right_child->parent = NULL; 399 root->right_child->parent = NULL;
401 heap->root = root->left_child; 400 heap->root = root->left_child;
402 insert_node (heap, heap->root, root->right_child); 401 insert_node (heap, heap->root, root->right_child);
403 } 402 }
404 GNUNET_free (root); 403 GNUNET_free (root);
405#if DEBUG 404#if DEBUG
406 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 405 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
407 (heap->size == heap->root->tree_size + 1)); 406 (heap->size == heap->root->tree_size + 1));
408 CHECK (heap->root); 407 CHECK (heap->root);
409#endif 408#endif
410 return ret; 409 return ret;
@@ -436,43 +435,43 @@ remove_node (struct GNUNET_CONTAINER_HeapNode *node)
436 435
437 /* unlink 'node' itself and insert children in its place */ 436 /* unlink 'node' itself and insert children in its place */
438 if (node->parent == NULL) 437 if (node->parent == NULL)
438 {
439 if (node->left_child != NULL)
439 { 440 {
440 if (node->left_child != NULL) 441 heap->root = node->left_child;
441 { 442 node->left_child->parent = NULL;
442 heap->root = node->left_child; 443 if (node->right_child != NULL)
443 node->left_child->parent = NULL; 444 {
444 if (node->right_child != NULL) 445 node->right_child->parent = NULL;
445 { 446 insert_node (heap, heap->root, node->right_child);
446 node->right_child->parent = NULL; 447 }
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 }
456 } 448 }
457 else 449 else
458 { 450 {
459 if (node->parent->left_child == node) 451 heap->root = node->right_child;
460 node->parent->left_child = NULL;
461 else
462 node->parent->right_child = NULL;
463 if (node->left_child != NULL)
464 {
465 node->left_child->parent = NULL;
466 node->parent->tree_size -= (1 + node->left_child->tree_size);
467 insert_node (heap, node->parent, node->left_child);
468 }
469 if (node->right_child != NULL) 452 if (node->right_child != NULL)
470 { 453 node->right_child->parent = NULL;
471 node->right_child->parent = NULL; 454 }
472 node->parent->tree_size -= (1 + node->right_child->tree_size); 455 }
473 insert_node (heap, node->parent, node->right_child); 456 else
474 } 457 {
458 if (node->parent->left_child == node)
459 node->parent->left_child = NULL;
460 else
461 node->parent->right_child = NULL;
462 if (node->left_child != NULL)
463 {
464 node->left_child->parent = NULL;
465 node->parent->tree_size -= (1 + node->left_child->tree_size);
466 insert_node (heap, node->parent, node->left_child);
467 }
468 if (node->right_child != NULL)
469 {
470 node->right_child->parent = NULL;
471 node->parent->tree_size -= (1 + node->right_child->tree_size);
472 insert_node (heap, node->parent, node->right_child);
475 } 473 }
474 }
476 node->parent = NULL; 475 node->parent = NULL;
477 node->left_child = NULL; 476 node->left_child = NULL;
478 node->right_child = NULL; 477 node->right_child = NULL;
@@ -506,7 +505,7 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
506#if DEBUG 505#if DEBUG
507 CHECK (heap->root); 506 CHECK (heap->root);
508 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 507 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
509 (heap->size == heap->root->tree_size + 1)); 508 (heap->size == heap->root->tree_size + 1));
510#endif 509#endif
511 return ret; 510 return ret;
512} 511}
@@ -521,19 +520,19 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
521 */ 520 */
522void 521void
523GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap, 522GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
524 struct GNUNET_CONTAINER_HeapNode *node, 523 struct GNUNET_CONTAINER_HeapNode *node,
525 GNUNET_CONTAINER_HeapCostType new_cost) 524 GNUNET_CONTAINER_HeapCostType new_cost)
526{ 525{
527#if DEBUG 526#if DEBUG
528 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 527 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
529 (heap->size == heap->root->tree_size + 1)); 528 (heap->size == heap->root->tree_size + 1));
530 CHECK (heap->root); 529 CHECK (heap->root);
531#endif 530#endif
532 remove_node (node); 531 remove_node (node);
533#if DEBUG 532#if DEBUG
534 CHECK (heap->root); 533 CHECK (heap->root);
535 GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) || 534 GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) ||
536 (heap->size == heap->root->tree_size + 2)); 535 (heap->size == heap->root->tree_size + 2));
537#endif 536#endif
538 node->cost = new_cost; 537 node->cost = new_cost;
539 if (heap->root == NULL) 538 if (heap->root == NULL)
@@ -543,7 +542,7 @@ GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
543#if DEBUG 542#if DEBUG
544 CHECK (heap->root); 543 CHECK (heap->root);
545 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || 544 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
546 (heap->size == heap->root->tree_size + 1)); 545 (heap->size == heap->root->tree_size + 1));
547#endif 546#endif
548} 547}
549 548
diff --git a/src/util/container_meta_data.c b/src/util/container_meta_data.c
index 1ea77f63a..c2ec01c04 100644
--- a/src/util/container_meta_data.c
+++ b/src/util/container_meta_data.c
@@ -162,10 +162,10 @@ GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md)
162 if (md == NULL) 162 if (md == NULL)
163 return; 163 return;
164 while (NULL != (item = md->items)) 164 while (NULL != (item = md->items))
165 { 165 {
166 md->items = item->next; 166 md->items = item->next;
167 meta_item_free (item); 167 meta_item_free (item);
168 } 168 }
169 GNUNET_free_non_null (md->sbuf); 169 GNUNET_free_non_null (md->sbuf);
170 GNUNET_free (md); 170 GNUNET_free (md);
171} 171}
@@ -184,10 +184,10 @@ GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md)
184 if (md == NULL) 184 if (md == NULL)
185 return; 185 return;
186 while (NULL != (item = md->items)) 186 while (NULL != (item = md->items))
187 { 187 {
188 md->items = item->next; 188 md->items = item->next;
189 meta_item_free (item); 189 meta_item_free (item);
190 } 190 }
191 GNUNET_free_non_null (md->sbuf); 191 GNUNET_free_non_null (md->sbuf);
192 memset (md, 0, sizeof (struct GNUNET_CONTAINER_MetaData)); 192 memset (md, 0, sizeof (struct GNUNET_CONTAINER_MetaData));
193} 193}
@@ -206,9 +206,9 @@ GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md)
206 */ 206 */
207int 207int
208GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData 208GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
209 *md1, 209 *md1,
210 const struct GNUNET_CONTAINER_MetaData 210 const struct GNUNET_CONTAINER_MetaData
211 *md2) 211 *md2)
212{ 212{
213 struct MetaItem *i; 213 struct MetaItem *i;
214 struct MetaItem *j; 214 struct MetaItem *j;
@@ -221,24 +221,24 @@ GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
221 221
222 i = md1->items; 222 i = md1->items;
223 while (NULL != i) 223 while (NULL != i)
224 {
225 found = GNUNET_NO;
226 j = md2->items;
227 while (NULL != j)
224 { 228 {
225 found = GNUNET_NO; 229 if ((i->type == j->type) && (i->format == j->format) &&
226 j = md2->items; 230 (i->data_size == j->data_size) &&
227 while (NULL != j) 231 (0 == memcmp (i->data, j->data, i->data_size)))
228 { 232 {
229 if ((i->type == j->type) && (i->format == j->format) && 233 found = GNUNET_YES;
230 (i->data_size == j->data_size) && 234 break;
231 (0 == memcmp (i->data, j->data, i->data_size))) 235 }
232 { 236 j = j->next;
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;
241 } 237 }
238 if (found == GNUNET_NO)
239 return GNUNET_NO;
240 i = i->next;
241 }
242 return GNUNET_YES; 242 return GNUNET_YES;
243} 243}
244 244
@@ -263,11 +263,11 @@ GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
263 */ 263 */
264int 264int
265GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md, 265GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
266 const char *plugin_name, 266 const char *plugin_name,
267 enum EXTRACTOR_MetaType type, 267 enum EXTRACTOR_MetaType type,
268 enum EXTRACTOR_MetaFormat format, 268 enum EXTRACTOR_MetaFormat format,
269 const char *data_mime_type, 269 const char *data_mime_type, const char *data,
270 const char *data, size_t data_len) 270 size_t data_len)
271{ 271{
272 struct MetaItem *prev; 272 struct MetaItem *prev;
273 struct MetaItem *pos; 273 struct MetaItem *pos;
@@ -277,28 +277,28 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
277 prev = NULL; 277 prev = NULL;
278 pos = md->items; 278 pos = md->items;
279 while (NULL != pos) 279 while (NULL != pos)
280 {
281 if (pos->data_size < data_len)
282 break;
283 if ((pos->type == type) && (pos->data_size == data_len) &&
284 (0 == memcmp (pos->data, data, data_len)))
280 { 285 {
281 if (pos->data_size < data_len) 286 if ((pos->mime_type == NULL) && (data_mime_type != NULL))
282 break; 287 {
283 if ((pos->type == type) && (pos->data_size == data_len) && 288 pos->mime_type = GNUNET_strdup (data_mime_type);
284 (0 == memcmp (pos->data, data, data_len))) 289 invalidate_sbuf (md);
285 { 290 }
286 if ((pos->mime_type == NULL) && (data_mime_type != NULL)) 291 if ((pos->format == EXTRACTOR_METAFORMAT_C_STRING) &&
287 { 292 (format == EXTRACTOR_METAFORMAT_UTF8))
288 pos->mime_type = GNUNET_strdup (data_mime_type); 293 {
289 invalidate_sbuf (md); 294 pos->format = EXTRACTOR_METAFORMAT_UTF8;
290 } 295 invalidate_sbuf (md);
291 if ((pos->format == EXTRACTOR_METAFORMAT_C_STRING) && 296 }
292 (format == EXTRACTOR_METAFORMAT_UTF8)) 297 return GNUNET_SYSERR;
293 {
294 pos->format = EXTRACTOR_METAFORMAT_UTF8;
295 invalidate_sbuf (md);
296 }
297 return GNUNET_SYSERR;
298 }
299 prev = pos;
300 pos = pos->next;
301 } 298 }
299 prev = pos;
300 pos = pos->next;
301 }
302 md->item_count++; 302 md->item_count++;
303 i = GNUNET_malloc (sizeof (struct MetaItem)); 303 i = GNUNET_malloc (sizeof (struct MetaItem));
304 i->type = type; 304 i->type = type;
@@ -310,23 +310,23 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
310 else 310 else
311 prev->next = i; 311 prev->next = i;
312 i->mime_type = 312 i->mime_type =
313 (data_mime_type == NULL) ? NULL : GNUNET_strdup (data_mime_type); 313 (data_mime_type == NULL) ? NULL : GNUNET_strdup (data_mime_type);
314 i->plugin_name = (plugin_name == NULL) ? NULL : GNUNET_strdup (plugin_name); 314 i->plugin_name = (plugin_name == NULL) ? NULL : GNUNET_strdup (plugin_name);
315 i->data = GNUNET_malloc (data_len); 315 i->data = GNUNET_malloc (data_len);
316 memcpy (i->data, data, data_len); 316 memcpy (i->data, data, data_len);
317 /* change OS native dir separators to unix '/' and others to '_' */ 317 /* change OS native dir separators to unix '/' and others to '_' */
318 if (type == EXTRACTOR_METATYPE_FILENAME) 318 if (type == EXTRACTOR_METATYPE_FILENAME)
319 {
320 p = i->data;
321 while ((*p != '\0') && (p < i->data + data_len))
319 { 322 {
320 p = i->data; 323 if (*p == DIR_SEPARATOR)
321 while ((*p != '\0') && (p < i->data + data_len)) 324 *p = '/';
322 { 325 else if (*p == '\\')
323 if (*p == DIR_SEPARATOR) 326 *p = '_';
324 *p = '/'; 327 p++;
325 else if (*p == '\\')
326 *p = '_';
327 p++;
328 }
329 } 328 }
329 }
330 invalidate_sbuf (md); 330 invalidate_sbuf (md);
331 return GNUNET_OK; 331 return GNUNET_OK;
332} 332}
@@ -349,14 +349,14 @@ GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
349 * @return 0 (to continue) 349 * @return 0 (to continue)
350 */ 350 */
351static int 351static int
352merge_helper (void *cls, const char *plugin_name, 352merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
353 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, 353 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
354 const char *data_mime_type, const char *data, size_t data_len) 354 const char *data, size_t data_len)
355{ 355{
356 struct GNUNET_CONTAINER_MetaData *md = cls; 356 struct GNUNET_CONTAINER_MetaData *md = cls;
357 357
358 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format, 358 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
359 data_mime_type, data, data_len); 359 data_mime_type, data, data_len);
360 return 0; 360 return 0;
361} 361}
362 362
@@ -370,7 +370,7 @@ merge_helper (void *cls, const char *plugin_name,
370 */ 370 */
371void 371void
372GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md, 372GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
373 const struct GNUNET_CONTAINER_MetaData *in) 373 const struct GNUNET_CONTAINER_MetaData *in)
374{ 374{
375 GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md); 375 GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md);
376} 376}
@@ -388,8 +388,8 @@ GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
388 */ 388 */
389int 389int
390GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md, 390GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
391 enum EXTRACTOR_MetaType type, 391 enum EXTRACTOR_MetaType type,
392 const char *data, size_t data_len) 392 const char *data, size_t data_len)
393{ 393{
394 struct MetaItem *pos; 394 struct MetaItem *pos;
395 struct MetaItem *prev; 395 struct MetaItem *prev;
@@ -397,24 +397,24 @@ GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
397 prev = NULL; 397 prev = NULL;
398 pos = md->items; 398 pos = md->items;
399 while (NULL != pos) 399 while (NULL != pos)
400 {
401 if ((pos->type == type) &&
402 ((data == NULL) ||
403 ((pos->data_size == data_len) &&
404 (0 == memcmp (pos->data, data, data_len)))))
400 { 405 {
401 if ((pos->type == type) && 406 if (prev == NULL)
402 ((data == NULL) || 407 md->items = pos->next;
403 ((pos->data_size == data_len) && 408 else
404 (0 == memcmp (pos->data, data, data_len))))) 409 prev->next = pos->next;
405 { 410 meta_item_free (pos);
406 if (prev == NULL) 411 md->item_count--;
407 md->items = pos->next; 412 invalidate_sbuf (md);
408 else 413 return GNUNET_OK;
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;
417 } 414 }
415 prev = pos;
416 pos = pos->next;
417 }
418 return GNUNET_SYSERR; 418 return GNUNET_SYSERR;
419} 419}
420 420
@@ -427,20 +427,19 @@ GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
427 */ 427 */
428void 428void
429GNUNET_CONTAINER_meta_data_add_publication_date (struct 429GNUNET_CONTAINER_meta_data_add_publication_date (struct
430 GNUNET_CONTAINER_MetaData 430 GNUNET_CONTAINER_MetaData *md)
431 *md)
432{ 431{
433 char *dat; 432 char *dat;
434 struct GNUNET_TIME_Absolute t; 433 struct GNUNET_TIME_Absolute t;
435 434
436 t = GNUNET_TIME_absolute_get (); 435 t = GNUNET_TIME_absolute_get ();
437 GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_METATYPE_PUBLICATION_DATE, 436 GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_METATYPE_PUBLICATION_DATE,
438 NULL, 0); 437 NULL, 0);
439 dat = GNUNET_STRINGS_absolute_time_to_string (t); 438 dat = GNUNET_STRINGS_absolute_time_to_string (t);
440 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>", 439 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
441 EXTRACTOR_METATYPE_PUBLICATION_DATE, 440 EXTRACTOR_METATYPE_PUBLICATION_DATE,
442 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 441 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
443 dat, strlen (dat) + 1); 442 dat, strlen (dat) + 1);
444 GNUNET_free (dat); 443 GNUNET_free (dat);
445} 444}
446 445
@@ -454,9 +453,9 @@ GNUNET_CONTAINER_meta_data_add_publication_date (struct
454 * @return number of entries 453 * @return number of entries
455 */ 454 */
456int 455int
457GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData 456GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
458 *md, EXTRACTOR_MetaDataProcessor iter, 457 EXTRACTOR_MetaDataProcessor iter,
459 void *iter_cls) 458 void *iter_cls)
460{ 459{
461 struct MetaItem *pos; 460 struct MetaItem *pos;
462 461
@@ -466,13 +465,13 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData
466 return md->item_count; 465 return md->item_count;
467 pos = md->items; 466 pos = md->items;
468 while (NULL != pos) 467 while (NULL != pos)
469 { 468 {
470 if (0 != 469 if (0 !=
471 iter (iter_cls, pos->plugin_name, pos->type, pos->format, 470 iter (iter_cls, pos->plugin_name, pos->type, pos->format,
472 pos->mime_type, pos->data, pos->data_size)) 471 pos->mime_type, pos->data, pos->data_size))
473 return md->item_count; 472 return md->item_count;
474 pos = pos->next; 473 pos = pos->next;
475 } 474 }
476 return md->item_count; 475 return md->item_count;
477} 476}
478 477
@@ -489,7 +488,7 @@ GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData
489 */ 488 */
490char * 489char *
491GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData 490GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
492 *md, enum EXTRACTOR_MetaType type) 491 *md, enum EXTRACTOR_MetaType type)
493{ 492{
494 struct MetaItem *pos; 493 struct MetaItem *pos;
495 494
@@ -497,13 +496,13 @@ GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
497 return NULL; 496 return NULL;
498 pos = md->items; 497 pos = md->items;
499 while (NULL != pos) 498 while (NULL != pos)
500 { 499 {
501 if ((type == pos->type) && 500 if ((type == pos->type) &&
502 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) || 501 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) ||
503 (pos->format == EXTRACTOR_METAFORMAT_C_STRING))) 502 (pos->format == EXTRACTOR_METAFORMAT_C_STRING)))
504 return GNUNET_strdup (pos->data); 503 return GNUNET_strdup (pos->data);
505 pos = pos->next; 504 pos = pos->next;
506 } 505 }
507 return NULL; 506 return NULL;
508} 507}
509 508
@@ -521,8 +520,8 @@ GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
521 */ 520 */
522char * 521char *
523GNUNET_CONTAINER_meta_data_get_first_by_types (const struct 522GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
524 GNUNET_CONTAINER_MetaData *md, 523 GNUNET_CONTAINER_MetaData *md,
525 ...) 524 ...)
526{ 525{
527 char *ret; 526 char *ret;
528 va_list args; 527 va_list args;
@@ -533,15 +532,15 @@ GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
533 ret = NULL; 532 ret = NULL;
534 va_start (args, md); 533 va_start (args, md);
535 while (1) 534 while (1)
536 { 535 {
537 type = va_arg (args, enum EXTRACTOR_MetaType); 536 type = va_arg (args, enum EXTRACTOR_MetaType);
538 537
539 if (type == -1) 538 if (type == -1)
540 break; 539 break;
541 ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type); 540 ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type);
542 if (ret != NULL) 541 if (ret != NULL)
543 break; 542 break;
544 } 543 }
545 va_end (args); 544 va_end (args);
546 return ret; 545 return ret;
547} 546}
@@ -556,9 +555,8 @@ GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
556 * @return number of bytes in thumbnail, 0 if not available 555 * @return number of bytes in thumbnail, 0 if not available
557 */ 556 */
558size_t 557size_t
559GNUNET_CONTAINER_meta_data_get_thumbnail (const struct 558GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
560 GNUNET_CONTAINER_MetaData * md, 559 * md, unsigned char **thumb)
561 unsigned char **thumb)
562{ 560{
563 struct MetaItem *pos; 561 struct MetaItem *pos;
564 struct MetaItem *match; 562 struct MetaItem *match;
@@ -568,19 +566,19 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct
568 match = NULL; 566 match = NULL;
569 pos = md->items; 567 pos = md->items;
570 while (NULL != pos) 568 while (NULL != pos)
569 {
570 if ((NULL != pos->mime_type) &&
571 (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) &&
572 (pos->format == EXTRACTOR_METAFORMAT_BINARY))
571 { 573 {
572 if ((NULL != pos->mime_type) && 574 if (match == NULL)
573 (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) && 575 match = pos;
574 (pos->format == EXTRACTOR_METAFORMAT_BINARY)) 576 else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) &&
575 { 577 (pos->type == EXTRACTOR_METATYPE_THUMBNAIL))
576 if (match == NULL) 578 match = pos;
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;
583 } 579 }
580 pos = pos->next;
581 }
584 if ((match == NULL) || (match->data_size == 0)) 582 if ((match == NULL) || (match->data_size == 0))
585 return 0; 583 return 0;
586 *thumb = GNUNET_malloc (match->data_size); 584 *thumb = GNUNET_malloc (match->data_size);
@@ -597,7 +595,7 @@ GNUNET_CONTAINER_meta_data_get_thumbnail (const struct
597 */ 595 */
598struct GNUNET_CONTAINER_MetaData * 596struct GNUNET_CONTAINER_MetaData *
599GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData 597GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
600 *md) 598 *md)
601{ 599{
602 struct GNUNET_CONTAINER_MetaData *ret; 600 struct GNUNET_CONTAINER_MetaData *ret;
603 struct MetaItem *pos; 601 struct MetaItem *pos;
@@ -607,12 +605,12 @@ GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
607 ret = GNUNET_CONTAINER_meta_data_create (); 605 ret = GNUNET_CONTAINER_meta_data_create ();
608 pos = md->items; 606 pos = md->items;
609 while (NULL != pos) 607 while (NULL != pos)
610 { 608 {
611 GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type, 609 GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type,
612 pos->format, pos->mime_type, 610 pos->format, pos->mime_type, pos->data,
613 pos->data, pos->data_size); 611 pos->data_size);
614 pos = pos->next; 612 pos = pos->next;
615 } 613 }
616 return ret; 614 return ret;
617} 615}
618 616
@@ -633,7 +631,7 @@ GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
633 */ 631 */
634static int 632static int
635try_compression (const char *data, size_t oldSize, char **result, 633try_compression (const char *data, size_t oldSize, char **result,
636 size_t * newSize) 634 size_t * newSize)
637{ 635{
638 char *tmp; 636 char *tmp;
639 uLongf dlen; 637 uLongf dlen;
@@ -648,14 +646,14 @@ try_compression (const char *data, size_t oldSize, char **result,
648 tmp = GNUNET_malloc (dlen); 646 tmp = GNUNET_malloc (dlen);
649 if (Z_OK == 647 if (Z_OK ==
650 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9)) 648 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9))
649 {
650 if (dlen < oldSize)
651 { 651 {
652 if (dlen < oldSize) 652 *result = tmp;
653 { 653 *newSize = dlen;
654 *result = tmp; 654 return GNUNET_YES;
655 *newSize = dlen;
656 return GNUNET_YES;
657 }
658 } 655 }
656 }
659 GNUNET_free (tmp); 657 GNUNET_free (tmp);
660 return GNUNET_NO; 658 return GNUNET_NO;
661} 659}
@@ -756,10 +754,10 @@ struct MetaDataEntry
756 */ 754 */
757ssize_t 755ssize_t
758GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData 756GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
759 *md, char **target, size_t max, 757 *md, char **target, size_t max,
760 enum 758 enum
761 GNUNET_CONTAINER_MetaDataSerializationOptions 759 GNUNET_CONTAINER_MetaDataSerializationOptions
762 opt) 760 opt)
763{ 761{
764 struct GNUNET_CONTAINER_MetaData *vmd; 762 struct GNUNET_CONTAINER_MetaData *vmd;
765 struct MetaItem *pos; 763 struct MetaItem *pos;
@@ -781,79 +779,79 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
781 int comp; 779 int comp;
782 780
783 if (max < sizeof (struct MetaDataHeader)) 781 if (max < sizeof (struct MetaDataHeader))
784 return GNUNET_SYSERR; /* far too small */ 782 return GNUNET_SYSERR; /* far too small */
785 if (md == NULL) 783 if (md == NULL)
786 return 0; 784 return 0;
787 785
788 if (md->sbuf != NULL) 786 if (md->sbuf != NULL)
787 {
788 /* try to use serialization cache */
789 if (md->sbuf_size <= max)
789 { 790 {
790 /* try to use serialization cache */ 791 if (NULL == *target)
791 if (md->sbuf_size <= max) 792 *target = GNUNET_malloc (md->sbuf_size);
792 { 793 memcpy (*target, md->sbuf, md->sbuf_size);
793 if (NULL == *target) 794 return md->sbuf_size;
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 ... */
801 } 795 }
796 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
797 return GNUNET_SYSERR; /* can say that this will fail */
798 /* need to compute a partial serialization, sbuf useless ... */
799 }
802 dst = NULL; 800 dst = NULL;
803 msize = 0; 801 msize = 0;
804 pos = md->items; 802 pos = md->items;
805 while (NULL != pos) 803 while (NULL != pos)
806 { 804 {
807 msize += sizeof (struct MetaDataEntry); 805 msize += sizeof (struct MetaDataEntry);
808 msize += pos->data_size; 806 msize += pos->data_size;
809 if (pos->plugin_name != NULL) 807 if (pos->plugin_name != NULL)
810 msize += strlen (pos->plugin_name) + 1; 808 msize += strlen (pos->plugin_name) + 1;
811 if (pos->mime_type != NULL) 809 if (pos->mime_type != NULL)
812 msize += strlen (pos->mime_type) + 1; 810 msize += strlen (pos->mime_type) + 1;
813 pos = pos->next; 811 pos = pos->next;
814 } 812 }
815 size = (size_t) msize; 813 size = (size_t) msize;
816 if (size != msize) 814 if (size != msize)
817 { 815 {
818 GNUNET_break (0); /* integer overflow */ 816 GNUNET_break (0); /* integer overflow */
819 return GNUNET_SYSERR; 817 return GNUNET_SYSERR;
820 } 818 }
821 if (size >= GNUNET_MAX_MALLOC_CHECKED) 819 if (size >= GNUNET_MAX_MALLOC_CHECKED)
822 { 820 {
823 /* too large to be processed */ 821 /* too large to be processed */
824 return GNUNET_SYSERR; 822 return GNUNET_SYSERR;
825 } 823 }
826 ent = GNUNET_malloc (size); 824 ent = GNUNET_malloc (size);
827 mdata = (char *) &ent[md->item_count]; 825 mdata = (char *) &ent[md->item_count];
828 off = size - (md->item_count * sizeof (struct MetaDataEntry)); 826 off = size - (md->item_count * sizeof (struct MetaDataEntry));
829 i = 0; 827 i = 0;
830 pos = md->items; 828 pos = md->items;
831 while (NULL != pos) 829 while (NULL != pos)
832 { 830 {
833 ent[i].type = htonl ((uint32_t) pos->type); 831 ent[i].type = htonl ((uint32_t) pos->type);
834 ent[i].format = htonl ((uint32_t) pos->format); 832 ent[i].format = htonl ((uint32_t) pos->format);
835 ent[i].data_size = htonl ((uint32_t) pos->data_size); 833 ent[i].data_size = htonl ((uint32_t) pos->data_size);
836 if (pos->plugin_name == NULL) 834 if (pos->plugin_name == NULL)
837 plen = 0; 835 plen = 0;
838 else 836 else
839 plen = strlen (pos->plugin_name) + 1; 837 plen = strlen (pos->plugin_name) + 1;
840 ent[i].plugin_name_len = htonl ((uint32_t) plen); 838 ent[i].plugin_name_len = htonl ((uint32_t) plen);
841 if (pos->mime_type == NULL) 839 if (pos->mime_type == NULL)
842 mlen = 0; 840 mlen = 0;
843 else 841 else
844 mlen = strlen (pos->mime_type) + 1; 842 mlen = strlen (pos->mime_type) + 1;
845 ent[i].mime_type_len = htonl ((uint32_t) mlen); 843 ent[i].mime_type_len = htonl ((uint32_t) mlen);
846 off -= pos->data_size; 844 off -= pos->data_size;
847 memcpy (&mdata[off], pos->data, pos->data_size); 845 memcpy (&mdata[off], pos->data, pos->data_size);
848 off -= plen; 846 off -= plen;
849 if (pos->plugin_name != NULL) 847 if (pos->plugin_name != NULL)
850 memcpy (&mdata[off], pos->plugin_name, plen); 848 memcpy (&mdata[off], pos->plugin_name, plen);
851 off -= mlen; 849 off -= mlen;
852 if (pos->mime_type != NULL) 850 if (pos->mime_type != NULL)
853 memcpy (&mdata[off], pos->mime_type, mlen); 851 memcpy (&mdata[off], pos->mime_type, mlen);
854 i++; 852 i++;
855 pos = pos->next; 853 pos = pos->next;
856 } 854 }
857 GNUNET_assert (off == 0); 855 GNUNET_assert (off == 0);
858 856
859 clen = 0; 857 clen = 0;
@@ -862,95 +860,95 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
862 i = 0; 860 i = 0;
863 pos = md->items; 861 pos = md->items;
864 while (pos != NULL) 862 while (pos != NULL)
863 {
864 comp = GNUNET_NO;
865 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
866 comp = try_compression ((const char *) &ent[i], left, &cdata, &clen);
867
868 if ((md->sbuf == NULL) && (i == 0))
869 {
870 /* fill 'sbuf'; this "modifies" md, but since this is only
871 * an internal cache we will cast away the 'const' instead
872 * of making the API look strange. */
873 vmd = (struct GNUNET_CONTAINER_MetaData *) md;
874 hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
875 hdr->size = htonl (left);
876 hdr->entries = htonl (md->item_count);
877 if (GNUNET_YES == comp)
878 {
879 GNUNET_assert (clen < left);
880 hdr->version = htonl (2 | HEADER_COMPRESSED);
881 memcpy (&hdr[1], cdata, clen);
882 vmd->sbuf_size = clen + sizeof (struct MetaDataHeader);
883 }
884 else
885 {
886 hdr->version = htonl (2);
887 memcpy (&hdr[1], &ent[0], left);
888 vmd->sbuf_size = left + sizeof (struct MetaDataHeader);
889 }
890 vmd->sbuf = (char *) hdr;
891 }
892
893 if (((left + sizeof (struct MetaDataHeader)) <= max) ||
894 ((comp == GNUNET_YES) && (clen <= max)))
895 {
896 /* success, this now fits! */
897 if (GNUNET_YES == comp)
898 {
899 if (dst == NULL)
900 dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader));
901 hdr = (struct MetaDataHeader *) dst;
902 hdr->version = htonl (2 | HEADER_COMPRESSED);
903 hdr->size = htonl (left);
904 hdr->entries = htonl (md->item_count - i);
905 memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen);
906 GNUNET_free (cdata);
907 GNUNET_free (ent);
908 rlen = clen + sizeof (struct MetaDataHeader);
909 }
910 else
911 {
912 if (dst == NULL)
913 dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
914 hdr = (struct MetaDataHeader *) dst;
915 hdr->version = htonl (2);
916 hdr->entries = htonl (md->item_count - i);
917 hdr->size = htonl (left);
918 memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left);
919 GNUNET_free (ent);
920 rlen = left + sizeof (struct MetaDataHeader);
921 }
922 if (NULL != *target)
923 {
924 memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
925 GNUNET_free (dst);
926 }
927 else
928 {
929 *target = dst;
930 }
931 return rlen;
932 }
933
934 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
865 { 935 {
866 comp = GNUNET_NO; 936 /* does not fit! */
867 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS)) 937 GNUNET_free (ent);
868 comp = try_compression ((const char *) &ent[i], left, &cdata, &clen); 938 return GNUNET_SYSERR;
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++;
953 } 939 }
940
941 /* next iteration: ignore the corresponding meta data at the
942 * end and try again without it */
943 left -= sizeof (struct MetaDataEntry);
944 left -= pos->data_size;
945 if (pos->plugin_name != NULL)
946 left -= strlen (pos->plugin_name) + 1;
947 if (pos->mime_type != NULL)
948 left -= strlen (pos->mime_type) + 1;
949 pos = pos->next;
950 i++;
951 }
954 GNUNET_free (ent); 952 GNUNET_free (ent);
955 953
956 /* nothing fit, only write header! */ 954 /* nothing fit, only write header! */
@@ -972,7 +970,7 @@ GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
972 */ 970 */
973ssize_t 971ssize_t
974GNUNET_CONTAINER_meta_data_get_serialized_size (const struct 972GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
975 GNUNET_CONTAINER_MetaData *md) 973 GNUNET_CONTAINER_MetaData *md)
976{ 974{
977 ssize_t ret; 975 ssize_t ret;
978 char *ptr; 976 char *ptr;
@@ -981,8 +979,8 @@ GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
981 return md->sbuf_size; 979 return md->sbuf_size;
982 ptr = NULL; 980 ptr = NULL;
983 ret = 981 ret =
984 GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED, 982 GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED,
985 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); 983 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
986 if (ret != -1) 984 if (ret != -1)
987 GNUNET_free (ptr); 985 GNUNET_free (ptr);
988 return ret; 986 return ret;
@@ -1009,14 +1007,14 @@ decompress (const char *input, size_t inputSize, size_t outputSize)
1009 output = GNUNET_malloc (olen); 1007 output = GNUNET_malloc (olen);
1010 if (Z_OK == 1008 if (Z_OK ==
1011 uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize)) 1009 uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize))
1012 { 1010 {
1013 return output; 1011 return output;
1014 } 1012 }
1015 else 1013 else
1016 { 1014 {
1017 GNUNET_free (output); 1015 GNUNET_free (output);
1018 return NULL; 1016 return NULL;
1019 } 1017 }
1020} 1018}
1021 1019
1022 1020
@@ -1058,121 +1056,121 @@ GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size)
1058 compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0; 1056 compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0;
1059 1057
1060 if (version == 1) 1058 if (version == 1)
1061 return NULL; /* null pointer */ 1059 return NULL; /* null pointer */
1062 if (version != 2) 1060 if (version != 2)
1063 { 1061 {
1064 GNUNET_break_op (0); /* unsupported version */ 1062 GNUNET_break_op (0); /* unsupported version */
1065 return NULL; 1063 return NULL;
1066 } 1064 }
1067 1065
1068 ic = ntohl (hdr.entries); 1066 ic = ntohl (hdr.entries);
1069 dataSize = ntohl (hdr.size); 1067 dataSize = ntohl (hdr.size);
1070 if ((sizeof (struct MetaDataEntry) * ic) > dataSize) 1068 if ((sizeof (struct MetaDataEntry) * ic) > dataSize)
1069 {
1070 GNUNET_break_op (0);
1071 return NULL;
1072 }
1073
1074 if (compressed)
1075 {
1076 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED)
1071 { 1077 {
1078 /* make sure we don't blow our memory limit because of a mal-formed
1079 * message... */
1072 GNUNET_break_op (0); 1080 GNUNET_break_op (0);
1073 return NULL; 1081 return NULL;
1074 } 1082 }
1075 1083 data =
1076 if (compressed) 1084 decompress ((const char *) &input[sizeof (struct MetaDataHeader)],
1085 size - sizeof (struct MetaDataHeader), dataSize);
1086 if (data == NULL)
1077 { 1087 {
1078 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED) 1088 GNUNET_break_op (0);
1079 { 1089 return NULL;
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;
1094 } 1090 }
1091 cdata = data;
1092 }
1095 else 1093 else
1094 {
1095 data = NULL;
1096 cdata = (const char *) &input[sizeof (struct MetaDataHeader)];
1097 if (dataSize != size - sizeof (struct MetaDataHeader))
1096 { 1098 {
1097 data = NULL; 1099 GNUNET_break_op (0);
1098 cdata = (const char *) &input[sizeof (struct MetaDataHeader)]; 1100 return NULL;
1099 if (dataSize != size - sizeof (struct MetaDataHeader))
1100 {
1101 GNUNET_break_op (0);
1102 return NULL;
1103 }
1104 } 1101 }
1102 }
1105 1103
1106 md = GNUNET_CONTAINER_meta_data_create (); 1104 md = GNUNET_CONTAINER_meta_data_create ();
1107 left = dataSize - ic * sizeof (struct MetaDataEntry); 1105 left = dataSize - ic * sizeof (struct MetaDataEntry);
1108 mdata = &cdata[ic * sizeof (struct MetaDataEntry)]; 1106 mdata = &cdata[ic * sizeof (struct MetaDataEntry)];
1109 for (i = 0; i < ic; i++) 1107 for (i = 0; i < ic; i++)
1108 {
1109 memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)],
1110 sizeof (struct MetaDataEntry));
1111 format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
1112 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1113 (format != EXTRACTOR_METAFORMAT_C_STRING) &&
1114 (format != EXTRACTOR_METAFORMAT_BINARY))
1110 { 1115 {
1111 memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)], 1116 GNUNET_break_op (0);
1112 sizeof (struct MetaDataEntry)); 1117 break;
1113 format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format); 1118 }
1114 if ((format != EXTRACTOR_METAFORMAT_UTF8) && 1119 dlen = ntohl (ent.data_size);
1115 (format != EXTRACTOR_METAFORMAT_C_STRING) && 1120 plen = ntohl (ent.plugin_name_len);
1116 (format != EXTRACTOR_METAFORMAT_BINARY)) 1121 mlen = ntohl (ent.mime_type_len);
1117 { 1122 if (dlen > left)
1118 GNUNET_break_op (0); 1123 {
1119 break; 1124 GNUNET_break_op (0);
1120 } 1125 break;
1121 dlen = ntohl (ent.data_size); 1126 }
1122 plen = ntohl (ent.plugin_name_len); 1127 left -= dlen;
1123 mlen = ntohl (ent.mime_type_len); 1128 meta_data = &mdata[left];
1124 if (dlen > left) 1129 if ((format == EXTRACTOR_METAFORMAT_UTF8) ||
1125 { 1130 (format == EXTRACTOR_METAFORMAT_C_STRING))
1126 GNUNET_break_op (0); 1131 {
1127 break; 1132 if ((dlen == 0) || (mdata[left + dlen - 1] != '\0'))
1128 } 1133 {
1129 left -= dlen; 1134 GNUNET_break_op (0);
1130 meta_data = &mdata[left]; 1135 break;
1131 if ((format == EXTRACTOR_METAFORMAT_UTF8) || 1136 }
1132 (format == EXTRACTOR_METAFORMAT_C_STRING)) 1137 }
1133 { 1138 if (plen > left)
1134 if ((dlen == 0) || (mdata[left + dlen - 1] != '\0')) 1139 {
1135 { 1140 GNUNET_break_op (0);
1136 GNUNET_break_op (0); 1141 break;
1137 break; 1142 }
1138 } 1143 left -= plen;
1139 } 1144 if ((plen > 0) && (mdata[left + plen - 1] != '\0'))
1140 if (plen > left) 1145 {
1141 { 1146 GNUNET_break_op (0);
1142 GNUNET_break_op (0); 1147 break;
1143 break; 1148 }
1144 } 1149 if (plen == 0)
1145 left -= plen; 1150 plugin_name = NULL;
1146 if ((plen > 0) && (mdata[left + plen - 1] != '\0')) 1151 else
1147 { 1152 plugin_name = &mdata[left];
1148 GNUNET_break_op (0); 1153
1149 break; 1154 if (mlen > left)
1150 } 1155 {
1151 if (plen == 0) 1156 GNUNET_break_op (0);
1152 plugin_name = NULL; 1157 break;
1153 else 1158 }
1154 plugin_name = &mdata[left]; 1159 left -= mlen;
1155 1160 if ((mlen > 0) && (mdata[left + mlen - 1] != '\0'))
1156 if (mlen > left) 1161 {
1157 { 1162 GNUNET_break_op (0);
1158 GNUNET_break_op (0); 1163 break;
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);
1175 } 1164 }
1165 if (mlen == 0)
1166 mime_type = NULL;
1167 else
1168 mime_type = &mdata[left];
1169 GNUNET_CONTAINER_meta_data_insert (md, plugin_name,
1170 (enum EXTRACTOR_MetaType)
1171 ntohl (ent.type), format, mime_type,
1172 meta_data, dlen);
1173 }
1176 GNUNET_free_non_null (data); 1174 GNUNET_free_non_null (data);
1177 return md; 1175 return md;
1178} 1176}
diff --git a/src/util/container_multihashmap.c b/src/util/container_multihashmap.c
index 60d4a43d5..7e53a6440 100644
--- a/src/util/container_multihashmap.c
+++ b/src/util/container_multihashmap.c
@@ -103,19 +103,19 @@ GNUNET_CONTAINER_multihashmap_create (unsigned int len)
103 */ 103 */
104void 104void
105GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap 105GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
106 *map) 106 *map)
107{ 107{
108 unsigned int i; 108 unsigned int i;
109 struct MapEntry *e; 109 struct MapEntry *e;
110 110
111 for (i = 0; i < map->map_length; i++) 111 for (i = 0; i < map->map_length; i++)
112 {
113 while (NULL != (e = map->map[i]))
112 { 114 {
113 while (NULL != (e = map->map[i])) 115 map->map[i] = e->next;
114 { 116 GNUNET_free (e);
115 map->map[i] = e->next;
116 GNUNET_free (e);
117 }
118 } 117 }
118 }
119 GNUNET_free (map->map); 119 GNUNET_free (map->map);
120 GNUNET_free (map); 120 GNUNET_free (map);
121} 121}
@@ -130,7 +130,7 @@ GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
130 */ 130 */
131static unsigned int 131static unsigned int
132idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m, 132idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
133 const GNUNET_HashCode * key) 133 const GNUNET_HashCode * key)
134{ 134{
135 GNUNET_assert (m != NULL); 135 GNUNET_assert (m != NULL);
136 return (*(unsigned int *) key) % m->map_length; 136 return (*(unsigned int *) key) % m->map_length;
@@ -145,7 +145,7 @@ idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
145 */ 145 */
146unsigned int 146unsigned int
147GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap 147GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
148 *map) 148 *map)
149{ 149{
150 return map->size; 150 return map->size;
151} 151}
@@ -163,17 +163,17 @@ GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
163 */ 163 */
164void * 164void *
165GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap 165GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
166 *map, const GNUNET_HashCode * key) 166 *map, const GNUNET_HashCode * key)
167{ 167{
168 struct MapEntry *e; 168 struct MapEntry *e;
169 169
170 e = map->map[idx_of (map, key)]; 170 e = map->map[idx_of (map, key)];
171 while (e != NULL) 171 while (e != NULL)
172 { 172 {
173 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 173 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
174 return e->value; 174 return e->value;
175 e = e->next; 175 e = e->next;
176 } 176 }
177 return NULL; 177 return NULL;
178} 178}
179 179
@@ -189,9 +189,9 @@ GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
189 */ 189 */
190int 190int
191GNUNET_CONTAINER_multihashmap_iterate (const struct 191GNUNET_CONTAINER_multihashmap_iterate (const struct
192 GNUNET_CONTAINER_MultiHashMap *map, 192 GNUNET_CONTAINER_MultiHashMap *map,
193 GNUNET_CONTAINER_HashMapIterator it, 193 GNUNET_CONTAINER_HashMapIterator it,
194 void *it_cls) 194 void *it_cls)
195{ 195{
196 int count; 196 int count;
197 unsigned int i; 197 unsigned int i;
@@ -202,20 +202,20 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct
202 count = 0; 202 count = 0;
203 GNUNET_assert (map != NULL); 203 GNUNET_assert (map != NULL);
204 for (i = 0; i < map->map_length; i++) 204 for (i = 0; i < map->map_length; i++)
205 {
206 n = map->map[i];
207 while (NULL != (e = n))
205 { 208 {
206 n = map->map[i]; 209 n = e->next;
207 while (NULL != (e = n)) 210 if (NULL != it)
208 { 211 {
209 n = e->next; 212 kc = e->key;
210 if (NULL != it) 213 if (GNUNET_OK != it (it_cls, &kc, e->value))
211 { 214 return GNUNET_SYSERR;
212 kc = e->key; 215 }
213 if (GNUNET_OK != it (it_cls, &kc, e->value)) 216 count++;
214 return GNUNET_SYSERR;
215 }
216 count++;
217 }
218 } 217 }
218 }
219 return count; 219 return count;
220} 220}
221 221
@@ -232,9 +232,8 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct
232 * is not in the map 232 * is not in the map
233 */ 233 */
234int 234int
235GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap 235GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
236 *map, const GNUNET_HashCode * key, 236 const GNUNET_HashCode * key, void *value)
237 void *value)
238{ 237{
239 struct MapEntry *e; 238 struct MapEntry *e;
240 struct MapEntry *p; 239 struct MapEntry *p;
@@ -244,21 +243,21 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
244 p = NULL; 243 p = NULL;
245 e = map->map[i]; 244 e = map->map[i];
246 while (e != NULL) 245 while (e != NULL)
246 {
247 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
248 (value == e->value))
247 { 249 {
248 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && 250 if (p == NULL)
249 (value == e->value)) 251 map->map[i] = e->next;
250 { 252 else
251 if (p == NULL) 253 p->next = e->next;
252 map->map[i] = e->next; 254 GNUNET_free (e);
253 else 255 map->size--;
254 p->next = e->next; 256 return GNUNET_YES;
255 GNUNET_free (e);
256 map->size--;
257 return GNUNET_YES;
258 }
259 p = e;
260 e = e->next;
261 } 257 }
258 p = e;
259 e = e->next;
260 }
262 return GNUNET_NO; 261 return GNUNET_NO;
263} 262}
264 263
@@ -273,7 +272,7 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
273 */ 272 */
274int 273int
275GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap 274GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
276 *map, const GNUNET_HashCode * key) 275 *map, const GNUNET_HashCode * key)
277{ 276{
278 struct MapEntry *e; 277 struct MapEntry *e;
279 struct MapEntry *p; 278 struct MapEntry *p;
@@ -285,27 +284,27 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
285 p = NULL; 284 p = NULL;
286 e = map->map[i]; 285 e = map->map[i];
287 while (e != NULL) 286 while (e != NULL)
287 {
288 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
288 { 289 {
289 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 290 if (p == NULL)
290 { 291 map->map[i] = e->next;
291 if (p == NULL)
292 map->map[i] = e->next;
293 else
294 p->next = e->next;
295 GNUNET_free (e);
296 map->size--;
297 if (p == NULL)
298 e = map->map[i];
299 else
300 e = p->next;
301 ret++;
302 }
303 else 292 else
304 { 293 p->next = e->next;
305 p = e; 294 GNUNET_free (e);
306 e = e->next; 295 map->size--;
307 } 296 if (p == NULL)
297 e = map->map[i];
298 else
299 e = p->next;
300 ret++;
301 }
302 else
303 {
304 p = e;
305 e = e->next;
308 } 306 }
307 }
309 return ret; 308 return ret;
310} 309}
311 310
@@ -321,18 +320,18 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
321 */ 320 */
322int 321int
323GNUNET_CONTAINER_multihashmap_contains (const struct 322GNUNET_CONTAINER_multihashmap_contains (const struct
324 GNUNET_CONTAINER_MultiHashMap *map, 323 GNUNET_CONTAINER_MultiHashMap *map,
325 const GNUNET_HashCode * key) 324 const GNUNET_HashCode * key)
326{ 325{
327 struct MapEntry *e; 326 struct MapEntry *e;
328 327
329 e = map->map[idx_of (map, key)]; 328 e = map->map[idx_of (map, key)];
330 while (e != NULL) 329 while (e != NULL)
331 { 330 {
332 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 331 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
333 return GNUNET_YES; 332 return GNUNET_YES;
334 e = e->next; 333 e = e->next;
335 } 334 }
336 return GNUNET_NO; 335 return GNUNET_NO;
337} 336}
338 337
@@ -349,21 +348,20 @@ GNUNET_CONTAINER_multihashmap_contains (const struct
349 */ 348 */
350int 349int
351GNUNET_CONTAINER_multihashmap_contains_value (const struct 350GNUNET_CONTAINER_multihashmap_contains_value (const struct
352 GNUNET_CONTAINER_MultiHashMap 351 GNUNET_CONTAINER_MultiHashMap
353 *map, 352 *map, const GNUNET_HashCode * key,
354 const GNUNET_HashCode * key, 353 const void *value)
355 const void *value)
356{ 354{
357 struct MapEntry *e; 355 struct MapEntry *e;
358 356
359 e = map->map[idx_of (map, key)]; 357 e = map->map[idx_of (map, key)];
360 while (e != NULL) 358 while (e != NULL)
361 { 359 {
362 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) && 360 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
363 (e->value == value)) 361 (e->value == value))
364 return GNUNET_YES; 362 return GNUNET_YES;
365 e = e->next; 363 e = e->next;
366 } 364 }
367 return GNUNET_NO; 365 return GNUNET_NO;
368} 366}
369 367
@@ -391,15 +389,15 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map)
391 map->map_length = new_len; 389 map->map_length = new_len;
392 map->map = new_map; 390 map->map = new_map;
393 for (i = 0; i < old_len; i++) 391 for (i = 0; i < old_len; i++)
392 {
393 while (NULL != (e = old_map[i]))
394 { 394 {
395 while (NULL != (e = old_map[i])) 395 old_map[i] = e->next;
396 { 396 idx = idx_of (map, &e->key);
397 old_map[i] = e->next; 397 e->next = new_map[idx];
398 idx = idx_of (map, &e->key); 398 new_map[idx] = e;
399 e->next = new_map[idx];
400 new_map[idx] = e;
401 }
402 } 399 }
400 }
403 GNUNET_free (old_map); 401 GNUNET_free (old_map);
404} 402}
405 403
@@ -418,9 +416,8 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map)
418 */ 416 */
419int 417int
420GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, 418GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
421 const GNUNET_HashCode * key, void *value, 419 const GNUNET_HashCode * key, void *value,
422 enum GNUNET_CONTAINER_MultiHashMapOption 420 enum GNUNET_CONTAINER_MultiHashMapOption opt)
423 opt)
424{ 421{
425 struct MapEntry *e; 422 struct MapEntry *e;
426 unsigned int i; 423 unsigned int i;
@@ -428,25 +425,25 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
428 i = idx_of (map, key); 425 i = idx_of (map, key);
429 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 426 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
430 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 427 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
428 {
429 e = map->map[i];
430 while (e != NULL)
431 { 431 {
432 e = map->map[i]; 432 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
433 while (e != NULL) 433 {
434 { 434 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
435 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 435 return GNUNET_SYSERR;
436 { 436 e->value = value;
437 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 437 return GNUNET_NO;
438 return GNUNET_SYSERR; 438 }
439 e->value = value; 439 e = e->next;
440 return GNUNET_NO;
441 }
442 e = e->next;
443 }
444 } 440 }
441 }
445 if (map->size / 3 >= map->map_length / 4) 442 if (map->size / 3 >= map->map_length / 4)
446 { 443 {
447 grow (map); 444 grow (map);
448 i = idx_of (map, key); 445 i = idx_of (map, key);
449 } 446 }
450 e = GNUNET_malloc (sizeof (struct MapEntry)); 447 e = GNUNET_malloc (sizeof (struct MapEntry));
451 e->key = *key; 448 e->key = *key;
452 e->value = value; 449 e->value = value;
@@ -469,10 +466,10 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
469 */ 466 */
470int 467int
471GNUNET_CONTAINER_multihashmap_get_multiple (const struct 468GNUNET_CONTAINER_multihashmap_get_multiple (const struct
472 GNUNET_CONTAINER_MultiHashMap 469 GNUNET_CONTAINER_MultiHashMap *map,
473 *map, const GNUNET_HashCode * key, 470 const GNUNET_HashCode * key,
474 GNUNET_CONTAINER_HashMapIterator 471 GNUNET_CONTAINER_HashMapIterator it,
475 it, void *it_cls) 472 void *it_cls)
476{ 473{
477 int count; 474 int count;
478 struct MapEntry *e; 475 struct MapEntry *e;
@@ -481,14 +478,14 @@ GNUNET_CONTAINER_multihashmap_get_multiple (const struct
481 count = 0; 478 count = 0;
482 n = map->map[idx_of (map, key)]; 479 n = map->map[idx_of (map, key)];
483 while (NULL != (e = n)) 480 while (NULL != (e = n))
484 { 481 {
485 n = e->next; 482 n = e->next;
486 if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode))) 483 if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
487 continue; 484 continue;
488 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value))) 485 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
489 return GNUNET_SYSERR; 486 return GNUNET_SYSERR;
490 count++; 487 count++;
491 } 488 }
492 return count; 489 return count;
493} 490}
494 491
diff --git a/src/util/container_slist.c b/src/util/container_slist.c
index 2704713d4..7b85dc877 100644
--- a/src/util/container_slist.c
+++ b/src/util/container_slist.c
@@ -89,21 +89,21 @@ struct GNUNET_CONTAINER_SList
89 */ 89 */
90static struct GNUNET_CONTAINER_SList_Elem * 90static struct GNUNET_CONTAINER_SList_Elem *
91create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf, 91create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
92 size_t len) 92 size_t len)
93{ 93{
94 struct GNUNET_CONTAINER_SList_Elem *e; 94 struct GNUNET_CONTAINER_SList_Elem *e;
95 95
96 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT) 96 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
97 { 97 {
98 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len); 98 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
99 memcpy (&e[1], buf, len); 99 memcpy (&e[1], buf, len);
100 e->elem = (void *) &e[1]; 100 e->elem = (void *) &e[1];
101 } 101 }
102 else 102 else
103 { 103 {
104 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem)); 104 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
105 e->elem = (void *) buf; 105 e->elem = (void *) buf;
106 } 106 }
107 e->disp = disp; 107 e->disp = disp;
108 e->len = len; 108 e->len = len;
109 return e; 109 return e;
@@ -119,8 +119,8 @@ create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
119 */ 119 */
120void 120void
121GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, 121GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
122 enum GNUNET_CONTAINER_SListDisposition disp, 122 enum GNUNET_CONTAINER_SListDisposition disp,
123 const void *buf, size_t len) 123 const void *buf, size_t len)
124{ 124{
125 struct GNUNET_CONTAINER_SList_Elem *e; 125 struct GNUNET_CONTAINER_SList_Elem *e;
126 126
@@ -141,8 +141,8 @@ GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
141 */ 141 */
142void 142void
143GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l, 143GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
144 enum GNUNET_CONTAINER_SListDisposition disp, 144 enum GNUNET_CONTAINER_SListDisposition disp,
145 const void *buf, size_t len) 145 const void *buf, size_t len)
146{ 146{
147 struct GNUNET_CONTAINER_SList_Elem *e; 147 struct GNUNET_CONTAINER_SList_Elem *e;
148 148
@@ -163,7 +163,7 @@ GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
163 */ 163 */
164void 164void
165GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, 165GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
166 struct GNUNET_CONTAINER_SList *src) 166 struct GNUNET_CONTAINER_SList *src)
167{ 167{
168 struct GNUNET_CONTAINER_SList_Iterator i; 168 struct GNUNET_CONTAINER_SList_Iterator i;
169 169
@@ -171,15 +171,14 @@ GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
171 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES; 171 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
172 GNUNET_CONTAINER_slist_next (&i)) 172 GNUNET_CONTAINER_slist_next (&i))
173 173
174 { 174 {
175 GNUNET_CONTAINER_slist_add (dst, 175 GNUNET_CONTAINER_slist_add (dst,
176 (i.elem->disp == 176 (i.elem->disp ==
177 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) 177 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ?
178 ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC 178 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC :
179 : 179 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
180 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 180 i.elem->elem, i.elem->len);
181 i.elem->elem, i.elem->len); 181 }
182 }
183 GNUNET_CONTAINER_slist_iter_destroy (&i); 182 GNUNET_CONTAINER_slist_iter_destroy (&i);
184} 183}
185 184
@@ -236,13 +235,13 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
236 235
237 e = l->head; 236 e = l->head;
238 while (e != NULL) 237 while (e != NULL)
239 { 238 {
240 n = e->next; 239 n = e->next;
241 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC) 240 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
242 GNUNET_free (e->elem); 241 GNUNET_free (e->elem);
243 GNUNET_free (e); 242 GNUNET_free (e);
244 e = n; 243 e = n;
245 } 244 }
246 l->head = NULL; 245 l->head = NULL;
247 l->tail = NULL; 246 l->tail = NULL;
248 l->length = 0; 247 l->length = 0;
@@ -258,7 +257,7 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
258 */ 257 */
259int 258int
260GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, 259GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
261 const void *buf, size_t len) 260 const void *buf, size_t len)
262{ 261{
263 struct GNUNET_CONTAINER_SList_Elem *e; 262 struct GNUNET_CONTAINER_SList_Elem *e;
264 263
@@ -315,8 +314,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
315 */ 314 */
316void 315void
317GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before, 316GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
318 enum GNUNET_CONTAINER_SListDisposition disp, 317 enum GNUNET_CONTAINER_SListDisposition disp,
319 const void *buf, size_t len) 318 const void *buf, size_t len)
320{ 319{
321 struct GNUNET_CONTAINER_SList_Elem *e; 320 struct GNUNET_CONTAINER_SList_Elem *e;
322 321
@@ -369,7 +368,7 @@ GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
369 */ 368 */
370void * 369void *
371GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, 370GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
372 size_t * len) 371 size_t * len)
373{ 372{
374 if (len) 373 if (len)
375 *len = i->elem->len; 374 *len = i->elem->len;
@@ -381,8 +380,7 @@ GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
381 * @param i iterator 380 * @param i iterator
382 */ 381 */
383void 382void
384GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator 383GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i)
385 *i)
386{ 384{
387} 385}
388 386
diff --git a/src/util/crypto_aes.c b/src/util/crypto_aes.c
index 9e194094c..8b031f367 100644
--- a/src/util/crypto_aes.c
+++ b/src/util/crypto_aes.c
@@ -39,9 +39,9 @@ void
39GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key) 39GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
40{ 40{
41 gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH, 41 gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH,
42 GCRY_STRONG_RANDOM); 42 GCRY_STRONG_RANDOM);
43 key->crc32 = 43 key->crc32 =
44 htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH)); 44 htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH));
45} 45}
46 46
47/** 47/**
@@ -51,7 +51,7 @@ GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
51 */ 51 */
52int 52int
53GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey 53GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
54 *key) 54 *key)
55{ 55{
56 uint32_t crc; 56 uint32_t crc;
57 57
@@ -76,29 +76,28 @@ GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
76 */ 76 */
77ssize_t 77ssize_t
78GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len, 78GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
79 const struct GNUNET_CRYPTO_AesSessionKey * 79 const struct GNUNET_CRYPTO_AesSessionKey *
80 sessionkey, 80 sessionkey,
81 const struct GNUNET_CRYPTO_AesInitializationVector 81 const struct GNUNET_CRYPTO_AesInitializationVector *
82 * iv, void *result) 82 iv, void *result)
83{ 83{
84 gcry_cipher_hd_t handle; 84 gcry_cipher_hd_t handle;
85 int rc; 85 int rc;
86 86
87 if (sessionkey->crc32 != 87 if (sessionkey->crc32 !=
88 htonl (GNUNET_CRYPTO_crc32_n 88 htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
89 (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) 89 {
90 { 90 GNUNET_break (0);
91 GNUNET_break (0); 91 return -1;
92 return -1; 92 }
93 }
94 GNUNET_assert (0 == 93 GNUNET_assert (0 ==
95 gcry_cipher_open (&handle, GCRY_CIPHER_AES256, 94 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
96 GCRY_CIPHER_MODE_CFB, 0)); 95 GCRY_CIPHER_MODE_CFB, 0));
97 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); 96 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
98 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 97 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
99 rc = gcry_cipher_setiv (handle, iv, 98 rc = gcry_cipher_setiv (handle, iv,
100 sizeof (struct 99 sizeof (struct
101 GNUNET_CRYPTO_AesInitializationVector)); 100 GNUNET_CRYPTO_AesInitializationVector));
102 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 101 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
103 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len)); 102 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len));
104 gcry_cipher_close (handle); 103 gcry_cipher_close (handle);
@@ -118,32 +117,30 @@ GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
118 */ 117 */
119ssize_t 118ssize_t
120GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size, 119GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
121 const struct GNUNET_CRYPTO_AesSessionKey * 120 const struct GNUNET_CRYPTO_AesSessionKey *
122 sessionkey, 121 sessionkey,
123 const struct GNUNET_CRYPTO_AesInitializationVector 122 const struct GNUNET_CRYPTO_AesInitializationVector *
124 * iv, void *result) 123 iv, void *result)
125{ 124{
126 gcry_cipher_hd_t handle; 125 gcry_cipher_hd_t handle;
127 int rc; 126 int rc;
128 127
129 if (sessionkey->crc32 != 128 if (sessionkey->crc32 !=
130 htonl (GNUNET_CRYPTO_crc32_n 129 htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
131 (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH))) 130 {
132 { 131 GNUNET_break (0);
133 GNUNET_break (0); 132 return -1;
134 return -1; 133 }
135 }
136 GNUNET_assert (0 == 134 GNUNET_assert (0 ==
137 gcry_cipher_open (&handle, GCRY_CIPHER_AES256, 135 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
138 GCRY_CIPHER_MODE_CFB, 0)); 136 GCRY_CIPHER_MODE_CFB, 0));
139 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); 137 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
140 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 138 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
141 rc = gcry_cipher_setiv (handle, iv, 139 rc = gcry_cipher_setiv (handle, iv,
142 sizeof (struct 140 sizeof (struct
143 GNUNET_CRYPTO_AesInitializationVector)); 141 GNUNET_CRYPTO_AesInitializationVector));
144 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); 142 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
145 GNUNET_assert (0 == 143 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size));
146 gcry_cipher_decrypt (handle, result, size, block, size));
147 gcry_cipher_close (handle); 144 gcry_cipher_close (handle);
148 return size; 145 return size;
149} 146}
@@ -158,8 +155,8 @@ GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
158 */ 155 */
159void 156void
160GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv, 157GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
161 const struct GNUNET_CRYPTO_AesSessionKey *skey, 158 const struct GNUNET_CRYPTO_AesSessionKey *skey,
162 const void *salt, size_t salt_len, ...) 159 const void *salt, size_t salt_len, ...)
163{ 160{
164 va_list argp; 161 va_list argp;
165 162
@@ -177,14 +174,12 @@ GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
177 * @param argp pairs of void * & size_t for context chunks, terminated by NULL 174 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
178 */ 175 */
179void 176void
180GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector 177GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
181 *iv, 178 const struct GNUNET_CRYPTO_AesSessionKey *skey,
182 const struct GNUNET_CRYPTO_AesSessionKey *skey, 179 const void *salt, size_t salt_len, va_list argp)
183 const void *salt, size_t salt_len,
184 va_list argp)
185{ 180{
186 GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key, 181 GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key,
187 sizeof (skey->key), argp); 182 sizeof (skey->key), argp);
188} 183}
189 184
190/* end of crypto_aes.c */ 185/* end of crypto_aes.c */
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c
index 5920ddc93..698d8d450 100644
--- a/src/util/crypto_crc.c
+++ b/src/util/crypto_crc.c
@@ -66,12 +66,12 @@ crc_init ()
66 once = 1; 66 once = 1;
67 crc_table[0] = 0; 67 crc_table[0] = 0;
68 for (i = 128; i; i >>= 1) 68 for (i = 128; i; i >>= 1)
69 { 69 {
70 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0); 70 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
71 /* h is now crc_table[i] */ 71 /* h is now crc_table[i] */
72 for (j = 0; j < 256; j += 2 * i) 72 for (j = 0; j < 256; j += 2 * i)
73 crc_table[i + j] = crc_table[j] ^ h; 73 crc_table[i + j] = crc_table[j] ^ h;
74 } 74 }
75} 75}
76 76
77/* 77/*
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index b8c9a8082..2ab682494 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -119,14 +119,14 @@ struct GNUNET_CRYPTO_FileHashContext
119 */ 119 */
120static void 120static void
121file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc, 121file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
122 const GNUNET_HashCode * res) 122 const GNUNET_HashCode * res)
123{ 123{
124 fhc->callback (fhc->callback_cls, res); 124 fhc->callback (fhc->callback_cls, res);
125 GNUNET_free (fhc->filename); 125 GNUNET_free (fhc->filename);
126 if (!GNUNET_DISK_handle_invalid (fhc->fh)) 126 if (!GNUNET_DISK_handle_invalid (fhc->fh))
127 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh)); 127 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
128 gcry_md_close (fhc->md); 128 gcry_md_close (fhc->md);
129 GNUNET_free (fhc); /* also frees fhc->buffer */ 129 GNUNET_free (fhc); /* also frees fhc->buffer */
130} 130}
131 131
132 132
@@ -149,19 +149,19 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 if (fhc->fsize - fhc->offset < delta) 149 if (fhc->fsize - fhc->offset < delta)
150 delta = fhc->fsize - fhc->offset; 150 delta = fhc->fsize - fhc->offset;
151 if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta)) 151 if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
152 { 152 {
153 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename); 153 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
154 file_hash_finish (fhc, NULL); 154 file_hash_finish (fhc, NULL);
155 return; 155 return;
156 } 156 }
157 gcry_md_write (fhc->md, fhc->buffer, delta); 157 gcry_md_write (fhc->md, fhc->buffer, delta);
158 fhc->offset += delta; 158 fhc->offset += delta;
159 if (fhc->offset == fhc->fsize) 159 if (fhc->offset == fhc->fsize)
160 { 160 {
161 res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512); 161 res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512);
162 file_hash_finish (fhc, res); 162 file_hash_finish (fhc, res);
163 return; 163 return;
164 } 164 }
165 fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc); 165 fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc);
166} 166}
167 167
@@ -178,43 +178,43 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 */ 178 */
179struct GNUNET_CRYPTO_FileHashContext * 179struct GNUNET_CRYPTO_FileHashContext *
180GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority, 180GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
181 const char *filename, size_t blocksize, 181 const char *filename, size_t blocksize,
182 GNUNET_CRYPTO_HashCompletedCallback callback, 182 GNUNET_CRYPTO_HashCompletedCallback callback,
183 void *callback_cls) 183 void *callback_cls)
184{ 184{
185 struct GNUNET_CRYPTO_FileHashContext *fhc; 185 struct GNUNET_CRYPTO_FileHashContext *fhc;
186 186
187 GNUNET_assert (blocksize > 0); 187 GNUNET_assert (blocksize > 0);
188 fhc = 188 fhc =
189 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize); 189 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
190 fhc->callback = callback; 190 fhc->callback = callback;
191 fhc->callback_cls = callback_cls; 191 fhc->callback_cls = callback_cls;
192 fhc->buffer = (unsigned char *) &fhc[1]; 192 fhc->buffer = (unsigned char *) &fhc[1];
193 fhc->filename = GNUNET_strdup (filename); 193 fhc->filename = GNUNET_strdup (filename);
194 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))
195 { 195 {
196 GNUNET_break (0); 196 GNUNET_break (0);
197 GNUNET_free (fhc); 197 GNUNET_free (fhc);
198 return NULL; 198 return NULL;
199 } 199 }
200 fhc->bsize = blocksize; 200 fhc->bsize = blocksize;
201 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))
202 { 202 {
203 GNUNET_free (fhc->filename); 203 GNUNET_free (fhc->filename);
204 GNUNET_free (fhc); 204 GNUNET_free (fhc);
205 return NULL; 205 return NULL;
206 } 206 }
207 fhc->fh = 207 fhc->fh =
208 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 208 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
209 GNUNET_DISK_PERM_NONE); 209 GNUNET_DISK_PERM_NONE);
210 if (!fhc->fh) 210 if (!fhc->fh)
211 { 211 {
212 GNUNET_free (fhc->filename); 212 GNUNET_free (fhc->filename);
213 GNUNET_free (fhc); 213 GNUNET_free (fhc);
214 return NULL; 214 return NULL;
215 } 215 }
216 fhc->task = 216 fhc->task =
217 GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc); 217 GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
218 return fhc; 218 return fhc;
219} 219}
220 220
@@ -260,7 +260,7 @@ getValue__ (unsigned char a)
260 */ 260 */
261void 261void
262GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block, 262GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
263 struct GNUNET_CRYPTO_HashAsciiEncoded *result) 263 struct GNUNET_CRYPTO_HashAsciiEncoded *result)
264{ 264{
265 /** 265 /**
266 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters) 266 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters)
@@ -278,23 +278,22 @@ GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
278 rpos = 0; 278 rpos = 0;
279 bits = 0; 279 bits = 0;
280 while ((rpos < sizeof (GNUNET_HashCode)) || (vbit > 0)) 280 while ((rpos < sizeof (GNUNET_HashCode)) || (vbit > 0))
281 {
282 if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5))
283 {
284 bits = (bits << 8) | ((unsigned char *) block)[rpos++]; /* eat 8 more bits */
285 vbit += 8;
286 }
287 if (vbit < 5)
281 { 288 {
282 if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5)) 289 bits <<= (5 - vbit); /* zero-padding */
283 { 290 GNUNET_assert (vbit == 2); /* padding by 3: 512+3 mod 5 == 0 */
284 bits = (bits << 8) | ((unsigned char *) block)[rpos++]; /* eat 8 more bits */ 291 vbit = 5;
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;
297 } 292 }
293 GNUNET_assert (wpos < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
294 result->encoding[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
295 vbit -= 5;
296 }
298 GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1); 297 GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
299 GNUNET_assert (vbit == 0); 298 GNUNET_assert (vbit == 0);
300 result->encoding[wpos] = '\0'; 299 result->encoding[wpos] = '\0';
@@ -318,22 +317,22 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
318 if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1) 317 if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
319 return GNUNET_SYSERR; 318 return GNUNET_SYSERR;
320 319
321 vbit = 2; /* padding! */ 320 vbit = 2; /* padding! */
322 wpos = sizeof (GNUNET_HashCode); 321 wpos = sizeof (GNUNET_HashCode);
323 rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1; 322 rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
324 bits = getValue__ (enc[--rpos]) >> 3; 323 bits = getValue__ (enc[--rpos]) >> 3;
325 while (wpos > 0) 324 while (wpos > 0)
325 {
326 GNUNET_assert (rpos > 0);
327 bits = (getValue__ (enc[--rpos]) << vbit) | bits;
328 vbit += 5;
329 if (vbit >= 8)
326 { 330 {
327 GNUNET_assert (rpos > 0); 331 ((unsigned char *) result)[--wpos] = (unsigned char) bits;
328 bits = (getValue__ (enc[--rpos]) << vbit) | bits; 332 bits >>= 8;
329 vbit += 5; 333 vbit -= 8;
330 if (vbit >= 8)
331 {
332 ((unsigned char *) result)[--wpos] = (unsigned char) bits;
333 bits >>= 8;
334 vbit -= 8;
335 }
336 } 334 }
335 }
337 GNUNET_assert (rpos == 0); 336 GNUNET_assert (rpos == 0);
338 GNUNET_assert (vbit == 0); 337 GNUNET_assert (vbit == 0);
339 return GNUNET_OK; 338 return GNUNET_OK;
@@ -352,7 +351,7 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
352 */ 351 */
353unsigned int 352unsigned int
354GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, 353GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
355 const GNUNET_HashCode * b) 354 const GNUNET_HashCode * b)
356{ 355{
357 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16; 356 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
358 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16; 357 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
@@ -362,7 +361,7 @@ GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
362 361
363void 362void
364GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, 363GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
365 GNUNET_HashCode * result) 364 GNUNET_HashCode * result)
366{ 365{
367 int i; 366 int i;
368 367
@@ -372,37 +371,33 @@ GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
372 371
373void 372void
374GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, 373GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
375 const GNUNET_HashCode * b, 374 const GNUNET_HashCode * b,
376 GNUNET_HashCode * result) 375 GNUNET_HashCode * result)
377{ 376{
378 int i; 377 int i;
379 378
380 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; 379 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
381 i--)
382 result->bits[i] = b->bits[i] - a->bits[i]; 380 result->bits[i] = b->bits[i] - a->bits[i];
383} 381}
384 382
385void 383void
386GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, 384GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
387 const GNUNET_HashCode * delta, 385 const GNUNET_HashCode * delta, GNUNET_HashCode * result)
388 GNUNET_HashCode * result)
389{ 386{
390 int i; 387 int i;
391 388
392 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; 389 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
393 i--)
394 result->bits[i] = delta->bits[i] + a->bits[i]; 390 result->bits[i] = delta->bits[i] + a->bits[i];
395} 391}
396 392
397 393
398void 394void
399GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, 395GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
400 GNUNET_HashCode * result) 396 GNUNET_HashCode * result)
401{ 397{
402 int i; 398 int i;
403 399
404 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; 400 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
405 i--)
406 result->bits[i] = a->bits[i] ^ b->bits[i]; 401 result->bits[i] = a->bits[i] ^ b->bits[i];
407} 402}
408 403
@@ -412,18 +407,17 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
412 */ 407 */
413void 408void
414GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, 409GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
415 struct GNUNET_CRYPTO_AesSessionKey *skey, 410 struct GNUNET_CRYPTO_AesSessionKey *skey,
416 struct GNUNET_CRYPTO_AesInitializationVector 411 struct GNUNET_CRYPTO_AesInitializationVector *iv)
417 *iv)
418{ 412{
419 GNUNET_assert (sizeof (GNUNET_HashCode) >= 413 GNUNET_assert (sizeof (GNUNET_HashCode) >=
420 GNUNET_CRYPTO_AES_KEY_LENGTH + 414 GNUNET_CRYPTO_AES_KEY_LENGTH +
421 sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); 415 sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
422 memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH); 416 memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH);
423 skey->crc32 = 417 skey->crc32 =
424 htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH)); 418 htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH));
425 memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH], 419 memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH],
426 sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); 420 sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
427} 421}
428 422
429 423
@@ -454,13 +448,13 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
454 */ 448 */
455unsigned int 449unsigned int
456GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first, 450GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
457 const GNUNET_HashCode * second) 451 const GNUNET_HashCode * second)
458{ 452{
459 unsigned int i; 453 unsigned int i;
460 454
461 for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++) 455 for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
462 if (GNUNET_CRYPTO_hash_get_bit (first, i) != 456 if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
463 GNUNET_CRYPTO_hash_get_bit (second, i)) 457 GNUNET_CRYPTO_hash_get_bit (second, i))
464 return i; 458 return i;
465 return sizeof (GNUNET_HashCode) * 8; 459 return sizeof (GNUNET_HashCode) * 8;
466} 460}
@@ -472,8 +466,7 @@ GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
472 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. 466 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
473 */ 467 */
474int 468int
475GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, 469GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
476 const GNUNET_HashCode * h2)
477{ 470{
478 unsigned int *i1; 471 unsigned int *i1;
479 unsigned int *i2; 472 unsigned int *i2;
@@ -481,14 +474,13 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
481 474
482 i1 = (unsigned int *) h1; 475 i1 = (unsigned int *) h1;
483 i2 = (unsigned int *) h2; 476 i2 = (unsigned int *) h2;
484 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; 477 for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
485 i--) 478 {
486 { 479 if (i1[i] > i2[i])
487 if (i1[i] > i2[i]) 480 return 1;
488 return 1; 481 if (i1[i] < i2[i])
489 if (i1[i] < i2[i]) 482 return -1;
490 return -1; 483 }
491 }
492 return 0; 484 return 0;
493} 485}
494 486
@@ -500,22 +492,22 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
500 */ 492 */
501int 493int
502GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, 494GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
503 const GNUNET_HashCode * h2, 495 const GNUNET_HashCode * h2,
504 const GNUNET_HashCode * target) 496 const GNUNET_HashCode * target)
505{ 497{
506 int i; 498 int i;
507 unsigned int d1; 499 unsigned int d1;
508 unsigned int d2; 500 unsigned int d2;
509 501
510 for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--) 502 for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--)
511 { 503 {
512 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; 504 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
513 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; 505 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
514 if (d1 > d2) 506 if (d1 > d2)
515 return 1; 507 return 1;
516 else if (d1 < d2) 508 else if (d1 < d2)
517 return -1; 509 return -1;
518 } 510 }
519 return 0; 511 return 0;
520} 512}
521 513
@@ -530,8 +522,8 @@ GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
530 */ 522 */
531void 523void
532GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key, 524GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
533 const struct GNUNET_CRYPTO_AesSessionKey *rkey, 525 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
534 const void *salt, size_t salt_len, ...) 526 const void *salt, size_t salt_len, ...)
535{ 527{
536 va_list argp; 528 va_list argp;
537 529
@@ -551,12 +543,12 @@ GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
551 */ 543 */
552void 544void
553GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, 545GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
554 const struct GNUNET_CRYPTO_AesSessionKey 546 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
555 *rkey, const void *salt, size_t salt_len, 547 const void *salt, size_t salt_len,
556 va_list argp) 548 va_list argp)
557{ 549{
558 GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key, 550 GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key,
559 sizeof (rkey->key), argp); 551 sizeof (rkey->key), argp);
560} 552}
561 553
562 554
@@ -570,14 +562,14 @@ GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
570 */ 562 */
571void 563void
572GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key, 564GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
573 const void *plaintext, size_t plaintext_len, 565 const void *plaintext, size_t plaintext_len,
574 GNUNET_HashCode * hmac) 566 GNUNET_HashCode * hmac)
575{ 567{
576 gcry_md_hd_t md; 568 gcry_md_hd_t md;
577 const unsigned char *mc; 569 const unsigned char *mc;
578 570
579 GNUNET_assert (GPG_ERR_NO_ERROR == 571 GNUNET_assert (GPG_ERR_NO_ERROR ==
580 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC)); 572 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
581 gcry_md_setkey (md, key->key, sizeof (key->key)); 573 gcry_md_setkey (md, key->key, sizeof (key->key));
582 gcry_md_write (md, plaintext, plaintext_len); 574 gcry_md_write (md, plaintext, plaintext_len);
583 mc = gcry_md_read (md, GCRY_MD_SHA512); 575 mc = gcry_md_read (md, GCRY_MD_SHA512);
diff --git a/src/util/crypto_hkdf.c b/src/util/crypto_hkdf.c
index 6af76ba68..40bfa6774 100644
--- a/src/util/crypto_hkdf.c
+++ b/src/util/crypto_hkdf.c
@@ -74,7 +74,7 @@
74 */ 74 */
75static const void * 75static const void *
76doHMAC (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,
77 size_t buf_len) 77 size_t buf_len)
78{ 78{
79 gcry_md_setkey (mac, key, key_len); 79 gcry_md_setkey (mac, key, key_len);
80 gcry_md_write (mac, buf, buf_len); 80 gcry_md_write (mac, buf, buf_len);
@@ -94,7 +94,7 @@ doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
94 */ 94 */
95static int 95static int
96getPRK (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,
97 size_t skm_len, void *prk) 97 size_t skm_len, void *prk)
98{ 98{
99 const void *ret; 99 const void *ret;
100 100
@@ -115,9 +115,9 @@ dump (const char *src, const void *p, unsigned int l)
115 115
116 printf ("\n%s: ", src); 116 printf ("\n%s: ", src);
117 for (i = 0; i < l; i++) 117 for (i = 0; i < l; i++)
118 { 118 {
119 printf ("%2x", (int) ((const unsigned char *) p)[i]); 119 printf ("%2x", (int) ((const unsigned char *) p)[i]);
120 } 120 }
121 printf ("\n"); 121 printf ("\n");
122} 122}
123#endif 123#endif
@@ -137,9 +137,9 @@ dump (const char *src, const void *p, unsigned int l)
137 * @return GNUNET_YES on success 137 * @return GNUNET_YES on success
138 */ 138 */
139int 139int
140GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, 140GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
141 int prf_algo, const void *xts, size_t xts_len, 141 const void *xts, size_t xts_len, const void *skm,
142 const void *skm, size_t skm_len, va_list argp) 142 size_t skm_len, va_list argp)
143{ 143{
144 const void *hc; 144 const void *hc;
145 unsigned long i, t, d; 145 unsigned long i, t, d;
@@ -158,16 +158,16 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo,
158 return GNUNET_SYSERR; 158 return GNUNET_SYSERR;
159 159
160 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)
161 { 161 {
162 gcry_md_close (xtr); 162 gcry_md_close (xtr);
163 return GNUNET_SYSERR; 163 return GNUNET_SYSERR;
164 } 164 }
165 165
166 va_copy (args, argp); 166 va_copy (args, argp);
167 167
168 ctx_len = 0; 168 ctx_len = 0;
169 while (NULL != va_arg (args, void *)) 169 while (NULL != va_arg (args, void *))
170 ctx_len += va_arg (args, size_t); 170 ctx_len += va_arg (args, size_t);
171 171
172 va_end (args); 172 va_end (args);
173 173
@@ -191,65 +191,65 @@ GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo,
191 dst = plain + k; 191 dst = plain + k;
192 va_copy (args, argp); 192 va_copy (args, argp);
193 while ((ctx = va_arg (args, void *))) 193 while ((ctx = va_arg (args, void *)))
194 { 194 {
195 size_t len; 195 size_t len;
196 196
197 len = va_arg (args, size_t); 197 len = va_arg (args, size_t);
198 memcpy (dst, ctx, len); 198 memcpy (dst, ctx, len);
199 dst += len; 199 dst += len;
200 } 200 }
201 va_end (args); 201 va_end (args);
202 202
203 if (t > 0) 203 if (t > 0)
204 { 204 {
205 memset (plain + k + ctx_len, 1, 1); 205 memset (plain + k + ctx_len, 1, 1);
206#if DEBUG_HKDF 206#if DEBUG_HKDF
207 dump ("K(1)", plain, plain_len); 207 dump ("K(1)", plain, plain_len);
208#endif 208#endif
209 hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1); 209 hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
210 if (hc == NULL) 210 if (hc == NULL)
211 goto hkdf_error; 211 goto hkdf_error;
212 memcpy (result, hc, k); 212 memcpy (result, hc, k);
213 result += k; 213 result += k;
214 } 214 }
215 215
216 /* K(i+1) */ 216 /* K(i+1) */
217 for (i = 1; i < t; i++) 217 for (i = 1; i < t; i++)
218 { 218 {
219 memcpy (plain, result - k, k); 219 memcpy (plain, result - k, k);
220 memset (plain + k + ctx_len, i + 1, 1); 220 memset (plain + k + ctx_len, i + 1, 1);
221 gcry_md_reset (prf); 221 gcry_md_reset (prf);
222#if DEBUG_HKDF 222#if DEBUG_HKDF
223 dump ("K(i+1)", plain, plain_len); 223 dump ("K(i+1)", plain, plain_len);
224#endif 224#endif
225 hc = doHMAC (prf, prk, xtr_len, plain, plain_len); 225 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
226 if (hc == NULL) 226 if (hc == NULL)
227 goto hkdf_error; 227 goto hkdf_error;
228 memcpy (result, hc, k); 228 memcpy (result, hc, k);
229 result += k; 229 result += k;
230 } 230 }
231 231
232 /* K(t):d */ 232 /* K(t):d */
233 if (d > 0) 233 if (d > 0)
234 {
235 if (t > 0)
234 { 236 {
235 if (t > 0) 237 memcpy (plain, result - k, k);
236 { 238 i++;
237 memcpy (plain, result - k, k); 239 }
238 i++; 240 memset (plain + k + ctx_len, i, 1);
239 } 241 gcry_md_reset (prf);
240 memset (plain + k + ctx_len, i, 1);
241 gcry_md_reset (prf);
242#if DEBUG_HKDF 242#if DEBUG_HKDF
243 dump ("K(t):d", plain, plain_len); 243 dump ("K(t):d", plain, plain_len);
244#endif 244#endif
245 if (t > 0) 245 if (t > 0)
246 hc = doHMAC (prf, prk, xtr_len, plain, plain_len); 246 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
247 else 247 else
248 hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k); 248 hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
249 if (hc == NULL) 249 if (hc == NULL)
250 goto hkdf_error; 250 goto hkdf_error;
251 memcpy (result, hc, d); 251 memcpy (result, hc, d);
252 } 252 }
253#if DEBUG_HKDF 253#if DEBUG_HKDF
254 dump ("result", result - k, out_len); 254 dump ("result", result - k, out_len);
255#endif 255#endif
@@ -281,16 +281,16 @@ hkdf_ok:
281 */ 281 */
282int 282int
283GNUNET_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,
284 const void *xts, size_t xts_len, const void *skm, 284 const void *xts, size_t xts_len, const void *skm,
285 size_t skm_len, ...) 285 size_t skm_len, ...)
286{ 286{
287 va_list argp; 287 va_list argp;
288 int ret; 288 int ret;
289 289
290 va_start (argp, skm_len); 290 va_start (argp, skm_len);
291 ret = 291 ret =
292 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,
293 skm, skm_len, argp); 293 skm, skm_len, argp);
294 va_end (argp); 294 va_end (argp);
295 295
296 return ret; 296 return ret;
diff --git a/src/util/crypto_kdf.c b/src/util/crypto_kdf.c
index d8fcbd866..0e7fbbbec 100644
--- a/src/util/crypto_kdf.c
+++ b/src/util/crypto_kdf.c
@@ -44,8 +44,8 @@
44 */ 44 */
45int 45int
46GNUNET_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,
47 size_t xts_len, const void *skm, size_t skm_len, 47 size_t xts_len, const void *skm, size_t skm_len,
48 va_list argp) 48 va_list argp)
49{ 49{
50 /* 50 /*
51 * "Finally, we point out to a particularly advantageous instantiation using 51 * "Finally, we point out to a particularly advantageous instantiation using
@@ -59,9 +59,8 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
59 * http://eprint.iacr.org/2010/264 59 * http://eprint.iacr.org/2010/264
60 */ 60 */
61 61
62 return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512, 62 return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512, GCRY_MD_SHA256,
63 GCRY_MD_SHA256, xts, xts_len, skm, skm_len, 63 xts, xts_len, skm, skm_len, argp);
64 argp);
65} 64}
66 65
67/** 66/**
@@ -77,14 +76,13 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
77 */ 76 */
78int 77int
79GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts, 78GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
80 size_t xts_len, const void *skm, size_t skm_len, ...) 79 size_t xts_len, const void *skm, size_t skm_len, ...)
81{ 80{
82 va_list argp; 81 va_list argp;
83 int ret; 82 int ret;
84 83
85 va_start (argp, skm_len); 84 va_start (argp, skm_len);
86 ret = 85 ret = GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp);
87 GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp);
88 va_end (argp); 86 va_end (argp);
89 87
90 return ret; 88 return ret;
diff --git a/src/util/crypto_ksk.c b/src/util/crypto_ksk.c
index 3d40ce27b..60639a709 100644
--- a/src/util/crypto_ksk.c
+++ b/src/util/crypto_ksk.c
@@ -51,12 +51,12 @@
51 51
52typedef struct 52typedef struct
53{ 53{
54 gcry_mpi_t n; /* public modulus */ 54 gcry_mpi_t n; /* public modulus */
55 gcry_mpi_t e; /* public exponent */ 55 gcry_mpi_t e; /* public exponent */
56 gcry_mpi_t d; /* exponent */ 56 gcry_mpi_t d; /* exponent */
57 gcry_mpi_t p; /* prime p. */ 57 gcry_mpi_t p; /* prime p. */
58 gcry_mpi_t q; /* prime q. */ 58 gcry_mpi_t q; /* prime q. */
59 gcry_mpi_t u; /* inverse of p mod q. */ 59 gcry_mpi_t u; /* inverse of p mod q. */
60} KBlock_secret_key; 60} KBlock_secret_key;
61 61
62/** 62/**
@@ -84,23 +84,23 @@ mpz_randomize (gcry_mpi_t n, unsigned int nbits, GNUNET_HashCode * rnd)
84 84
85 tmp = *rnd; 85 tmp = *rnd;
86 for (i = 0; i < cnt; i++) 86 for (i = 0; i < cnt; i++)
87 { 87 {
88 int j; 88 int j;
89 89
90 if (i > 0) 90 if (i > 0)
91 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), &tmp); 91 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), &tmp);
92 for (j = 0; j < sizeof (GNUNET_HashCode) / sizeof (uint32_t); j++) 92 for (j = 0; j < sizeof (GNUNET_HashCode) / sizeof (uint32_t); j++)
93 { 93 {
94#if HAVE_GCRY_MPI_LSHIFT 94#if HAVE_GCRY_MPI_LSHIFT
95 gcry_mpi_lshift (n, n, sizeof (uint32_t) * 8); 95 gcry_mpi_lshift (n, n, sizeof (uint32_t) * 8);
96#else 96#else
97 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); 97 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4));
98 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4)); 98 gcry_mpi_mul_ui (n, n, 1 << (sizeof (uint32_t) * 4));
99#endif 99#endif
100 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;
103 } 101 }
102 hc = tmp;
103 }
104 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), rnd); 104 GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), rnd);
105 i = gcry_mpi_get_nbits (n); 105 i = gcry_mpi_get_nbits (n);
106 while (i > nbits) 106 while (i > nbits)
@@ -114,10 +114,10 @@ mpz_trailing_zeroes (gcry_mpi_t n)
114 114
115 cnt = gcry_mpi_get_nbits (n); 115 cnt = gcry_mpi_get_nbits (n);
116 for (idx = 0; idx < cnt; idx++) 116 for (idx = 0; idx < cnt; idx++)
117 { 117 {
118 if (gcry_mpi_test_bit (n, idx) == 0) 118 if (gcry_mpi_test_bit (n, idx) == 0)
119 return idx; 119 return idx;
120 } 120 }
121 121
122 return ULONG_MAX; 122 return ULONG_MAX;
123} 123}
@@ -164,31 +164,31 @@ is_prime (gcry_mpi_t n, int steps, GNUNET_HashCode * hc)
164 mpz_tdiv_q_2exp (q, q, k); 164 mpz_tdiv_q_2exp (q, q, k);
165 165
166 for (i = 0; i < steps; i++) 166 for (i = 0; i < steps; i++)
167 {
168 if (!i)
167 { 169 {
168 if (!i) 170 gcry_mpi_set_ui (x, 2);
169 {
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 }
190 } 171 }
191 rc = 1; /* May be a prime. */ 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 }
190 }
191 rc = 1; /* May be a prime. */
192 192
193leave: 193leave:
194 gcry_mpi_release (x); 194 gcry_mpi_release (x);
@@ -210,10 +210,10 @@ static void
210adjust (unsigned char *buf, size_t size, size_t target) 210adjust (unsigned char *buf, size_t size, size_t target)
211{ 211{
212 if (size < target) 212 if (size < target)
213 { 213 {
214 memmove (&buf[target - size], buf, size); 214 memmove (&buf[target - size], buf, size);
215 memset (buf, 0, target - size); 215 memset (buf, 0, target - size);
216 } 216 }
217} 217}
218 218
219 219
@@ -326,70 +326,70 @@ gen_prime (gcry_mpi_t * ptest, unsigned int nbits, GNUNET_HashCode * hc)
326 tmp = gcry_mpi_new (0); 326 tmp = gcry_mpi_new (0);
327 sp = gcry_mpi_new (0); 327 sp = gcry_mpi_new (0);
328 while (1) 328 while (1)
329 {
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. */
341 for (i = 0; i < no_of_small_prime_numbers; i++)
329 { 342 {
330 /* generate a random number */ 343 size_t written;
331 mpz_randomize (prime, nbits, hc); 344
332 /* Set high order bit to 1, set low order bit to 1. If we are 345 gcry_mpi_set_ui (sp, small_prime_numbers[i]);
333 * generating a secret prime we are most probably doing that 346 gcry_mpi_div (NULL, tmp, prime, sp, -1);
334 * for RSA, to make sure that the modulus does have the 347 mods[i] = 0;
335 * requested key size we set the 2 high order bits. */ 348 written = sizeof (unsigned int);
336 gcry_mpi_set_bit (prime, nbits - 1); 349 GNUNET_assert (0 ==
337 gcry_mpi_set_bit (prime, nbits - 2); 350 gcry_mpi_print (GCRYMPI_FMT_USG,
338 gcry_mpi_set_bit (prime, 0); 351 (unsigned char *) &mods[i], written,
339 352 &written, tmp));
340 /* Calculate all remainders. */ 353 adjust ((unsigned char *) &mods[i], written, sizeof (unsigned int));
354 mods[i] = ntohl (mods[i]);
355 }
356 /* Now try some primes starting with prime. */
357 for (step = 0; step < 20000; step += 2)
358 {
359 /* Check against all the small primes we have in mods. */
341 for (i = 0; i < no_of_small_prime_numbers; i++) 360 for (i = 0; i < no_of_small_prime_numbers; i++)
342 { 361 {
343 size_t written; 362 uint16_t x = small_prime_numbers[i];
344 363
345 gcry_mpi_set_ui (sp, small_prime_numbers[i]); 364 while (mods[i] + step >= x)
346 gcry_mpi_div (NULL, tmp, prime, sp, -1); 365 mods[i] -= x;
347 mods[i] = 0; 366 if (!(mods[i] + step))
348 written = sizeof (unsigned int); 367 break;
349 GNUNET_assert (0 == 368 }
350 gcry_mpi_print (GCRYMPI_FMT_USG, 369 if (i < no_of_small_prime_numbers)
351 (unsigned char *) &mods[i], written, 370 continue; /* Found a multiple of an already known prime. */
352 &written, tmp)); 371
353 adjust ((unsigned char *) &mods[i], written, sizeof (unsigned int)); 372 gcry_mpi_add_ui (*ptest, prime, step);
354 mods[i] = ntohl (mods[i]); 373 if (!gcry_mpi_test_bit (*ptest, nbits - 2))
355 } 374 break;
356 /* Now try some primes starting with prime. */ 375
357 for (step = 0; step < 20000; step += 2) 376 /* Do a fast Fermat test now. */
358 { 377 gcry_mpi_sub_ui (pminus1, *ptest, 1);
359 /* Check against all the small primes we have in mods. */ 378 gcry_mpi_powm (result, val_2, pminus1, *ptest);
360 for (i = 0; i < no_of_small_prime_numbers; i++) 379 if ((!gcry_mpi_cmp_ui (result, 1)) && (is_prime (*ptest, 5, hc)))
361 { 380 {
362 uint16_t x = small_prime_numbers[i]; 381 /* Got it. */
363 382 gcry_mpi_release (sp);
364 while (mods[i] + step >= x) 383 gcry_mpi_release (tmp);
365 mods[i] -= x; 384 gcry_mpi_release (val_2);
366 if (!(mods[i] + step)) 385 gcry_mpi_release (val_3);
367 break; 386 gcry_mpi_release (result);
368 } 387 gcry_mpi_release (pminus1);
369 if (i < no_of_small_prime_numbers) 388 gcry_mpi_release (prime);
370 continue; /* Found a multiple of an already known prime. */ 389 return;
371 390 }
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 }
392 } 391 }
392 }
393} 393}
394 394
395/** 395/**
@@ -399,11 +399,11 @@ gen_prime (gcry_mpi_t * ptest, unsigned int nbits, GNUNET_HashCode * hc)
399 * @param hc the HC to use for PRNG (modified!) 399 * @param hc the HC to use for PRNG (modified!)
400 */ 400 */
401static void 401static void
402generate_kblock_key (KBlock_secret_key * sk, unsigned int nbits, 402generate_kblock_key (KBlock_secret_key *sk, unsigned int nbits,
403 GNUNET_HashCode * hc) 403 GNUNET_HashCode * hc)
404{ 404{
405 gcry_mpi_t t1, t2; 405 gcry_mpi_t t1, t2;
406 gcry_mpi_t phi; /* helper: (p-1)(q-1) */ 406 gcry_mpi_t phi; /* helper: (p-1)(q-1) */
407 gcry_mpi_t g; 407 gcry_mpi_t g;
408 gcry_mpi_t f; 408 gcry_mpi_t f;
409 409
@@ -425,36 +425,36 @@ generate_kblock_key (KBlock_secret_key * sk, unsigned int nbits,
425 f = gcry_mpi_new (0); 425 f = gcry_mpi_new (0);
426 426
427 do 427 do
428 {
429 do
428 { 430 {
429 do 431 gcry_mpi_release (sk->p);
430 { 432 gcry_mpi_release (sk->q);
431 gcry_mpi_release (sk->p); 433 gen_prime (&sk->p, nbits / 2, hc);
432 gcry_mpi_release (sk->q); 434 gen_prime (&sk->q, nbits / 2, hc);
433 gen_prime (&sk->p, nbits / 2, hc); 435
434 gen_prime (&sk->q, nbits / 2, hc); 436 if (gcry_mpi_cmp (sk->p, sk->q) > 0) /* p shall be smaller than q (for calc of u) */
435 437 gcry_mpi_swap (sk->p, sk->q);
436 if (gcry_mpi_cmp (sk->p, sk->q) > 0) /* p shall be smaller than q (for calc of u) */ 438 /* calculate the modulus */
437 gcry_mpi_swap (sk->p, sk->q); 439 gcry_mpi_mul (sk->n, sk->p, sk->q);
438 /* calculate the modulus */ 440 }
439 gcry_mpi_mul (sk->n, sk->p, sk->q); 441 while (gcry_mpi_get_nbits (sk->n) != nbits);
440 } 442
441 while (gcry_mpi_get_nbits (sk->n) != nbits); 443 /* calculate Euler totient: phi = (p-1)(q-1) */
442 444 gcry_mpi_sub_ui (t1, sk->p, 1);
443 /* calculate Euler totient: phi = (p-1)(q-1) */ 445 gcry_mpi_sub_ui (t2, sk->q, 1);
444 gcry_mpi_sub_ui (t1, sk->p, 1); 446 gcry_mpi_mul (phi, t1, t2);
445 gcry_mpi_sub_ui (t2, sk->q, 1); 447 gcry_mpi_gcd (g, t1, t2);
446 gcry_mpi_mul (phi, t1, t2); 448 gcry_mpi_div (f, NULL, phi, g, 0);
447 gcry_mpi_gcd (g, t1, t2); 449 while (0 == gcry_mpi_gcd (t1, sk->e, phi))
448 gcry_mpi_div (f, NULL, phi, g, 0); 450 { /* (while gcd is not 1) */
449 while (0 == gcry_mpi_gcd (t1, sk->e, phi)) 451 gcry_mpi_add_ui (sk->e, sk->e, 2);
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 */
455 } 452 }
453
454 /* calculate the secret key d = e^1 mod phi */
455 }
456 while ((0 == gcry_mpi_invm (sk->d, sk->e, f)) || 456 while ((0 == gcry_mpi_invm (sk->d, sk->e, f)) ||
457 (0 == gcry_mpi_invm (sk->u, sk->p, sk->q))); 457 (0 == gcry_mpi_invm (sk->u, sk->p, sk->q)));
458 458
459 gcry_mpi_release (t1); 459 gcry_mpi_release (t1);
460 gcry_mpi_release (t2); 460 gcry_mpi_release (t2);
@@ -473,13 +473,13 @@ struct KskRsaPrivateKeyBinaryEncoded
473 * Total size of the structure, in bytes, in big-endian! 473 * Total size of the structure, in bytes, in big-endian!
474 */ 474 */
475 uint16_t len GNUNET_PACKED; 475 uint16_t len GNUNET_PACKED;
476 uint16_t sizen GNUNET_PACKED; /* in big-endian! */ 476 uint16_t sizen GNUNET_PACKED; /* in big-endian! */
477 uint16_t sizee GNUNET_PACKED; /* in big-endian! */ 477 uint16_t sizee GNUNET_PACKED; /* in big-endian! */
478 uint16_t sized GNUNET_PACKED; /* in big-endian! */ 478 uint16_t sized GNUNET_PACKED; /* in big-endian! */
479 uint16_t sizep GNUNET_PACKED; /* in big-endian! */ 479 uint16_t sizep GNUNET_PACKED; /* in big-endian! */
480 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ 480 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */
481 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ 481 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */
482 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ 482 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */
483 /* followed by the actual values */ 483 /* followed by the actual values */
484}; 484};
485 485
@@ -501,16 +501,16 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc)
501 size_t size; 501 size_t size;
502 502
503 hx = *hc; 503 hx = *hc;
504 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
505 * -- we simply cannot afford 2048 bits 505 * -- we simply cannot afford 2048 bits
506 * even on modern hardware, and especially 506 * even on modern hardware, and especially
507 * not since clearly a dictionary attack 507 * not since clearly a dictionary attack
508 * will still be much cheaper 508 * will still be much cheaper
509 * than breaking a 1024 bit RSA key. 509 * than breaking a 1024 bit RSA key.
510 * If an adversary can spend the time to 510 * If an adversary can spend the time to
511 * break a 1024 bit RSA key just to forge 511 * break a 1024 bit RSA key just to forge
512 * a signature -- SO BE IT. [ CG, 6/2005 ] */ 512 * a signature -- SO BE IT. [ CG, 6/2005 ] */
513 &hx); 513 &hx);
514 pkv[0] = &sk.n; 514 pkv[0] = &sk.n;
515 pkv[1] = &sk.e; 515 pkv[1] = &sk.e;
516 pkv[2] = &sk.d; 516 pkv[2] = &sk.d;
@@ -519,10 +519,10 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc)
519 pkv[5] = &sk.u; 519 pkv[5] = &sk.u;
520 size = sizeof (struct KskRsaPrivateKeyBinaryEncoded); 520 size = sizeof (struct KskRsaPrivateKeyBinaryEncoded);
521 for (i = 0; i < 6; i++) 521 for (i = 0; i < 6; i++)
522 { 522 {
523 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]);
524 size += sizes[i]; 524 size += sizes[i];
525 } 525 }
526 GNUNET_assert (size < 65536); 526 GNUNET_assert (size < 65536);
527 retval = GNUNET_malloc (size); 527 retval = GNUNET_malloc (size);
528 retval->len = htons (size); 528 retval->len = htons (size);
@@ -547,10 +547,10 @@ makeKblockKeyInternal (const GNUNET_HashCode * hc)
547 retval->sizedmq1 = htons (0); 547 retval->sizedmq1 = htons (0);
548 memcpy (&((char *) &retval[1])[i], pbu[5], sizes[5]); 548 memcpy (&((char *) &retval[1])[i], pbu[5], sizes[5]);
549 for (i = 0; i < 6; i++) 549 for (i = 0; i < 6; i++)
550 { 550 {
551 gcry_mpi_release (*pkv[i]); 551 gcry_mpi_release (*pkv[i]);
552 free (pbu[i]); 552 free (pbu[i]);
553 } 553 }
554 return retval; 554 return retval;
555} 555}
556 556
@@ -572,123 +572,122 @@ ksk_decode_key (const struct KskRsaPrivateKeyBinaryEncoded *encoding)
572 pos = 0; 572 pos = 0;
573 size = ntohs (encoding->sizen); 573 size = ntohs (encoding->sizen);
574 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, 574 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG,
575 &((const unsigned char *) (&encoding[1]))[pos], size, 575 &((const unsigned char *) (&encoding[1]))[pos], size,
576 &size); 576 &size);
577 pos += ntohs (encoding->sizen); 577 pos += ntohs (encoding->sizen);
578 if (rc) 578 if (rc)
579 { 579 {
580 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 580 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
581 return NULL; 581 return NULL;
582 } 582 }
583 size = ntohs (encoding->sizee); 583 size = ntohs (encoding->sizee);
584 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, 584 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG,
585 &((const unsigned char *) (&encoding[1]))[pos], size, 585 &((const unsigned char *) (&encoding[1]))[pos], size,
586 &size); 586 &size);
587 pos += ntohs (encoding->sizee); 587 pos += ntohs (encoding->sizee);
588 if (rc) 588 if (rc)
589 { 589 {
590 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 590 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
591 gcry_mpi_release (n); 591 gcry_mpi_release (n);
592 return NULL; 592 return NULL;
593 } 593 }
594 size = ntohs (encoding->sized); 594 size = ntohs (encoding->sized);
595 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG, 595 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG,
596 &((const unsigned char *) (&encoding[1]))[pos], size, 596 &((const unsigned char *) (&encoding[1]))[pos], size,
597 &size); 597 &size);
598 pos += ntohs (encoding->sized); 598 pos += ntohs (encoding->sized);
599 if (rc) 599 if (rc)
600 {
601 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
602 gcry_mpi_release (n);
603 gcry_mpi_release (e);
604 return NULL;
605 }
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], size,
612 &size);
613 pos += ntohs (encoding->sizep);
614 if (rc)
600 { 615 {
601 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 616 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
602 gcry_mpi_release (n); 617 gcry_mpi_release (n);
603 gcry_mpi_release (e); 618 gcry_mpi_release (e);
619 gcry_mpi_release (d);
604 return NULL; 620 return NULL;
605 } 621 }
606 /* swap p and q! */ 622 }
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 }
623 else 623 else
624 q = NULL; 624 q = NULL;
625 size = ntohs (encoding->sizeq); 625 size = ntohs (encoding->sizeq);
626 if (size > 0) 626 if (size > 0)
627 {
628 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG,
629 &((const unsigned char *) (&encoding[1]))[pos], size,
630 &size);
631 pos += ntohs (encoding->sizeq);
632 if (rc)
627 { 633 {
628 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG, 634 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
629 &((const unsigned char *) (&encoding[1]))[pos], 635 gcry_mpi_release (n);
630 size, &size); 636 gcry_mpi_release (e);
631 pos += ntohs (encoding->sizeq); 637 gcry_mpi_release (d);
632 if (rc) 638 if (q != NULL)
633 { 639 gcry_mpi_release (q);
634 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 640 return NULL;
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 }
642 } 641 }
642 }
643 else 643 else
644 p = NULL; 644 p = NULL;
645 pos += ntohs (encoding->sizedmp1); 645 pos += ntohs (encoding->sizedmp1);
646 pos += ntohs (encoding->sizedmq1); 646 pos += ntohs (encoding->sizedmq1);
647 size = 647 size =
648 ntohs (encoding->len) - sizeof (struct KskRsaPrivateKeyBinaryEncoded) - 648 ntohs (encoding->len) - sizeof (struct KskRsaPrivateKeyBinaryEncoded) -
649 pos; 649 pos;
650 if (size > 0) 650 if (size > 0)
651 {
652 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG,
653 &((const unsigned char *) (&encoding[1]))[pos], size,
654 &size);
655 if (rc)
651 { 656 {
652 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG, 657 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
653 &((const unsigned char *) (&encoding[1]))[pos], 658 gcry_mpi_release (n);
654 size, &size); 659 gcry_mpi_release (e);
655 if (rc) 660 gcry_mpi_release (d);
656 { 661 if (p != NULL)
657 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 662 gcry_mpi_release (p);
658 gcry_mpi_release (n); 663 if (q != NULL)
659 gcry_mpi_release (e); 664 gcry_mpi_release (q);
660 gcry_mpi_release (d); 665 return NULL;
661 if (p != NULL)
662 gcry_mpi_release (p);
663 if (q != NULL)
664 gcry_mpi_release (q);
665 return NULL;
666 }
667 } 666 }
667 }
668 else 668 else
669 u = NULL; 669 u = NULL;
670 670
671 if ((p != NULL) && (q != NULL) && (u != NULL)) 671 if ((p != NULL) && (q != NULL) && (u != NULL))
672 {
673 rc = gcry_sexp_build (&res, &size, /* erroff */
674 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))",
675 n, e, d, p, q, u);
676 }
677 else
678 {
679 if ((p != NULL) && (q != NULL))
672 { 680 {
673 rc = gcry_sexp_build (&res, &size, /* erroff */ 681 rc = gcry_sexp_build (&res, &size, /* erroff */
674 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))", 682 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))",
675 n, e, d, p, q, u); 683 n, e, d, p, q);
676 } 684 }
677 else 685 else
678 { 686 {
679 if ((p != NULL) && (q != NULL)) 687 rc = gcry_sexp_build (&res, &size, /* erroff */
680 { 688 "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, d);
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 }
691 } 689 }
690 }
692 gcry_mpi_release (n); 691 gcry_mpi_release (n);
693 gcry_mpi_release (e); 692 gcry_mpi_release (e);
694 gcry_mpi_release (d); 693 gcry_mpi_release (d);
@@ -703,10 +702,10 @@ ksk_decode_key (const struct KskRsaPrivateKeyBinaryEncoded *encoding)
703 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 702 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
704#if EXTRA_CHECKS 703#if EXTRA_CHECKS
705 if (gcry_pk_testkey (res)) 704 if (gcry_pk_testkey (res))
706 { 705 {
707 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 706 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
708 return NULL; 707 return NULL;
709 } 708 }
710#endif 709#endif
711 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); 710 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
712 ret->sexp = res; 711 ret->sexp = res;
@@ -736,13 +735,13 @@ GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc)
736 unsigned int i; 735 unsigned int i;
737 736
738 for (i = 0; i < cacheSize; i++) 737 for (i = 0; i < cacheSize; i++)
738 {
739 if (0 == memcmp (hc, &cache[i]->hc, sizeof (GNUNET_HashCode)))
739 { 740 {
740 if (0 == memcmp (hc, &cache[i]->hc, sizeof (GNUNET_HashCode))) 741 ret = ksk_decode_key (cache[i]->pke);
741 { 742 return ret;
742 ret = ksk_decode_key (cache[i]->pke);
743 return ret;
744 }
745 } 743 }
744 }
746 745
747 line = GNUNET_malloc (sizeof (struct KBlockKeyCacheLine)); 746 line = GNUNET_malloc (sizeof (struct KBlockKeyCacheLine));
748 line->hc = *hc; 747 line->hc = *hc;
@@ -758,10 +757,10 @@ void __attribute__ ((destructor)) GNUNET_CRYPTO_ksk_fini ()
758 unsigned int i; 757 unsigned int i;
759 758
760 for (i = 0; i < cacheSize; i++) 759 for (i = 0; i < cacheSize; i++)
761 { 760 {
762 GNUNET_free (cache[i]->pke); 761 GNUNET_free (cache[i]->pke);
763 GNUNET_free (cache[i]); 762 GNUNET_free (cache[i]);
764 } 763 }
765 GNUNET_array_grow (cache, cacheSize, 0); 764 GNUNET_array_grow (cache, cacheSize, 0);
766} 765}
767 766
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index 7fb2d1428..c74d60f4f 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -65,37 +65,37 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
65 GNUNET_assert (i > 0); 65 GNUNET_assert (i > 0);
66 66
67 switch (mode) 67 switch (mode)
68 { 68 {
69 case GNUNET_CRYPTO_QUALITY_STRONG: 69 case GNUNET_CRYPTO_QUALITY_STRONG:
70 /* 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 */
71#ifdef gcry_fast_random_poll 71#ifdef gcry_fast_random_poll
72 if ((invokeCount++ % 256) == 0) 72 if ((invokeCount++ % 256) == 0)
73 gcry_fast_random_poll (); 73 gcry_fast_random_poll ();
74#endif 74#endif
75 ul = UINT32_MAX - (UINT32_MAX % i); 75 ul = UINT32_MAX - (UINT32_MAX % i);
76 do 76 do
77 { 77 {
78 gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t), 78 gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t),
79 GCRY_STRONG_RANDOM); 79 GCRY_STRONG_RANDOM);
80 } 80 }
81 while (ret >= ul); 81 while (ret >= ul);
82 return ret % i; 82 return ret % i;
83 case GNUNET_CRYPTO_QUALITY_NONCE: 83 case GNUNET_CRYPTO_QUALITY_NONCE:
84 ul = UINT32_MAX - (UINT32_MAX % i); 84 ul = UINT32_MAX - (UINT32_MAX % i);
85 do 85 do
86 { 86 {
87 gcry_create_nonce (&ret, sizeof (ret)); 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);
98 } 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);
98 }
99 return 0; 99 return 0;
100} 100}
101 101
@@ -121,12 +121,12 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
121 for (i = 0; i < n; i++) 121 for (i = 0; i < n; i++)
122 ret[i] = i; 122 ret[i] = i;
123 for (i = n - 1; i > 0; i--) 123 for (i = n - 1; i > 0; i--)
124 { 124 {
125 x = GNUNET_CRYPTO_random_u32 (mode, i + 1); 125 x = GNUNET_CRYPTO_random_u32 (mode, i + 1);
126 tmp = ret[x]; 126 tmp = ret[x];
127 ret[x] = ret[i]; 127 ret[x] = ret[i];
128 ret[i] = tmp; 128 ret[i] = tmp;
129 } 129 }
130 return ret; 130 return ret;
131} 131}
132 132
@@ -146,33 +146,33 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
146 146
147 GNUNET_assert (max > 0); 147 GNUNET_assert (max > 0);
148 switch (mode) 148 switch (mode)
149 {
150 case GNUNET_CRYPTO_QUALITY_STRONG:
151 ul = UINT64_MAX - (UINT64_MAX % max);
152 do
149 { 153 {
150 case GNUNET_CRYPTO_QUALITY_STRONG: 154 gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t),
151 ul = UINT64_MAX - (UINT64_MAX % max); 155 GCRY_STRONG_RANDOM);
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);
175 } 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);
175 }
176 return 0; 176 return 0;
177} 177}
178 178
@@ -200,7 +200,7 @@ static struct GNUNET_OS_Process *genproc;
200 */ 200 */
201static void 201static void
202entropy_generator (void *cls, const char *what, int printchar, int current, 202entropy_generator (void *cls, const char *what, int printchar, int current,
203 int total) 203 int total)
204{ 204{
205 unsigned long code; 205 unsigned long code;
206 enum GNUNET_OS_ProcessStatusType type; 206 enum GNUNET_OS_ProcessStatusType type;
@@ -209,39 +209,39 @@ entropy_generator (void *cls, const char *what, int printchar, int current,
209 if (0 != strcmp (what, "need_entropy")) 209 if (0 != strcmp (what, "need_entropy"))
210 return; 210 return;
211 if (current == total) 211 if (current == total)
212 {
213 if (genproc != NULL)
212 { 214 {
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 }
233 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) 215 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
234 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill"); 216 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill");
235 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); 217 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
236 GNUNET_OS_process_close (genproc); 218 GNUNET_OS_process_close (genproc);
237 genproc = NULL; 219 genproc = NULL;
238 } 220 }
239 LOG (GNUNET_ERROR_TYPE_INFO, 221 return;
240 _("Starting `%s' process to generate entropy\n"), "find"); 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 }
233 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
234 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill");
235 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
236 GNUNET_OS_process_close (genproc);
237 genproc = NULL;
238 }
239 LOG (GNUNET_ERROR_TYPE_INFO, _("Starting `%s' process to generate entropy\n"),
240 "find");
241 genproc = 241 genproc =
242 GNUNET_OS_start_process (NULL, NULL, "sh", "sh", "-c", 242 GNUNET_OS_start_process (NULL, NULL, "sh", "sh", "-c",
243 "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",
244 NULL); 244 NULL);
245} 245}
246 246
247 247
@@ -249,11 +249,11 @@ static void
249killfind () 249killfind ()
250{ 250{
251 if (genproc != NULL) 251 if (genproc != NULL)
252 { 252 {
253 GNUNET_OS_process_kill (genproc, SIGKILL); 253 GNUNET_OS_process_kill (genproc, SIGKILL);
254 GNUNET_OS_process_close (genproc); 254 GNUNET_OS_process_close (genproc);
255 genproc = NULL; 255 genproc = NULL;
256 } 256 }
257} 257}
258 258
259 259
@@ -261,21 +261,20 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
261{ 261{
262 gcry_control (GCRYCTL_DISABLE_SECMEM, 0); 262 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
263 if (!gcry_check_version (GCRYPT_VERSION)) 263 if (!gcry_check_version (GCRYPT_VERSION))
264 { 264 {
265 fprintf (stderr, 265 fprintf (stderr,
266 _ 266 _
267 ("libgcrypt has not the expected version (version %s is required).\n"), 267 ("libgcrypt has not the expected version (version %s is required).\n"),
268 GCRYPT_VERSION); 268 GCRYPT_VERSION);
269 abort (); 269 abort ();
270 } 270 }
271#ifdef gcry_fast_random_poll 271#ifdef gcry_fast_random_poll
272 gcry_fast_random_poll (); 272 gcry_fast_random_poll ();
273#endif 273#endif
274 gcry_set_progress_handler (&entropy_generator, NULL); 274 gcry_set_progress_handler (&entropy_generator, NULL);
275 atexit (&killfind); 275 atexit (&killfind);
276 SRANDOM (time (NULL) ^ 276 SRANDOM (time (NULL) ^
277 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 277 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
278 UINT32_MAX));
279} 278}
280 279
281 280
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index 7f0c81692..d29566ac8 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -66,13 +66,13 @@ struct RsaPrivateKeyBinaryEncoded
66 * Total size of the structure, in bytes, in big-endian! 66 * Total size of the structure, in bytes, in big-endian!
67 */ 67 */
68 uint16_t len GNUNET_PACKED; 68 uint16_t len GNUNET_PACKED;
69 uint16_t sizen GNUNET_PACKED; /* in big-endian! */ 69 uint16_t sizen GNUNET_PACKED; /* in big-endian! */
70 uint16_t sizee GNUNET_PACKED; /* in big-endian! */ 70 uint16_t sizee GNUNET_PACKED; /* in big-endian! */
71 uint16_t sized GNUNET_PACKED; /* in big-endian! */ 71 uint16_t sized GNUNET_PACKED; /* in big-endian! */
72 uint16_t sizep GNUNET_PACKED; /* in big-endian! */ 72 uint16_t sizep GNUNET_PACKED; /* in big-endian! */
73 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ 73 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */
74 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ 74 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */
75 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ 75 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */
76 /* followed by the actual values */ 76 /* followed by the actual values */
77}; 77};
78 78
@@ -98,10 +98,10 @@ static void
98adjust (unsigned char *buf, size_t size, size_t target) 98adjust (unsigned char *buf, size_t size, size_t target)
99{ 99{
100 if (size < target) 100 if (size < target)
101 { 101 {
102 memmove (&buf[target - size], buf, size); 102 memmove (&buf[target - size], buf, size);
103 memset (buf, 0, target - size); 103 memset (buf, 0, target - size);
104 } 104 }
105} 105}
106 106
107/** 107/**
@@ -115,9 +115,9 @@ GNUNET_CRYPTO_rsa_key_create ()
115 gcry_sexp_t s_keyparam; 115 gcry_sexp_t s_keyparam;
116 116
117 GNUNET_assert (0 == 117 GNUNET_assert (0 ==
118 gcry_sexp_build (&s_keyparam, NULL, 118 gcry_sexp_build (&s_keyparam, NULL,
119 "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))", 119 "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))",
120 HOSTKEY_LEN)); 120 HOSTKEY_LEN));
121 GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam)); 121 GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam));
122 gcry_sexp_release (s_keyparam); 122 gcry_sexp_release (s_keyparam);
123#if EXTRA_CHECKS 123#if EXTRA_CHECKS
@@ -140,7 +140,7 @@ GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
140 140
141static int 141static int
142key_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,
143 const char *elems) 143 const char *elems)
144{ 144{
145 gcry_sexp_t list, l2; 145 gcry_sexp_t list, l2;
146 const char *s; 146 const char *s;
@@ -148,44 +148,44 @@ key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
148 148
149 list = gcry_sexp_find_token (sexp, topname, 0); 149 list = gcry_sexp_find_token (sexp, topname, 0);
150 if (!list) 150 if (!list)
151 { 151 {
152 return 1; 152 return 1;
153 } 153 }
154 l2 = gcry_sexp_cadr (list); 154 l2 = gcry_sexp_cadr (list);
155 gcry_sexp_release (list); 155 gcry_sexp_release (list);
156 list = l2; 156 list = l2;
157 if (!list) 157 if (!list)
158 { 158 {
159 return 2; 159 return 2;
160 } 160 }
161 161
162 idx = 0; 162 idx = 0;
163 for (s = elems; *s; s++, idx++) 163 for (s = elems; *s; s++, idx++)
164 {
165 l2 = gcry_sexp_find_token (list, s, 1);
166 if (!l2)
164 { 167 {
165 l2 = gcry_sexp_find_token (list, s, 1); 168 for (i = 0; i < idx; i++)
166 if (!l2) 169 {
167 { 170 gcry_free (array[i]);
168 for (i = 0; i < idx; i++) 171 array[i] = NULL;
169 { 172 }
170 gcry_free (array[i]); 173 gcry_sexp_release (list);
171 array[i] = NULL; 174 return 3; /* required parameter not found */
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 }
188 } 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 }
188 }
189 gcry_sexp_release (list); 189 gcry_sexp_release (list);
190 return 0; 190 return 0;
191} 191}
@@ -197,10 +197,9 @@ key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
197 */ 197 */
198void 198void
199GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey 199GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
200 *priv, 200 *priv,
201 struct 201 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
202 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 202 *pub)
203 *pub)
204{ 203{
205 gcry_mpi_t skey[2]; 204 gcry_mpi_t skey[2];
206 size_t size; 205 size_t size;
@@ -213,25 +212,24 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
213 rc = key_from_sexp (skey, priv->sexp, "rsa", "ne"); 212 rc = key_from_sexp (skey, priv->sexp, "rsa", "ne");
214 GNUNET_assert (0 == rc); 213 GNUNET_assert (0 == rc);
215 pub->len = 214 pub->len =
216 htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) - 215 htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) -
217 sizeof (pub->padding)); 216 sizeof (pub->padding));
218 pub->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); 217 pub->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
219 pub->padding = 0; 218 pub->padding = 0;
220 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; 219 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
221 GNUNET_assert (0 == 220 GNUNET_assert (0 ==
222 gcry_mpi_print (GCRYMPI_FMT_USG, &pub->key[0], size, &size, 221 gcry_mpi_print (GCRYMPI_FMT_USG, &pub->key[0], size, &size,
223 skey[0])); 222 skey[0]));
224 adjust (&pub->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); 223 adjust (&pub->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
225 size = 224 size = GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
226 GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
227 GNUNET_assert (0 == 225 GNUNET_assert (0 ==
228 gcry_mpi_print (GCRYMPI_FMT_USG, 226 gcry_mpi_print (GCRYMPI_FMT_USG,
229 &pub->key 227 &pub->key
230 [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], 228 [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size,
231 size, &size, skey[1])); 229 &size, skey[1]));
232 adjust (&pub->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size, 230 adjust (&pub->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size,
233 GNUNET_CRYPTO_RSA_KEY_LENGTH - 231 GNUNET_CRYPTO_RSA_KEY_LENGTH -
234 GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH); 232 GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
235 gcry_mpi_release (skey[0]); 233 gcry_mpi_release (skey[0]);
236 gcry_mpi_release (skey[1]); 234 gcry_mpi_release (skey[1]);
237} 235}
@@ -245,7 +243,7 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
245 */ 243 */
246static struct GNUNET_CRYPTO_RsaPrivateKey * 244static struct GNUNET_CRYPTO_RsaPrivateKey *
247public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 245public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
248 *publicKey) 246 *publicKey)
249{ 247{
250 struct GNUNET_CRYPTO_RsaPrivateKey *ret; 248 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
251 gcry_sexp_t result; 249 gcry_sexp_t result;
@@ -259,38 +257,36 @@ public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
259 (ntohs (publicKey->len) != 257 (ntohs (publicKey->len) !=
260 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) - 258 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) -
261 sizeof (publicKey->padding))) 259 sizeof (publicKey->padding)))
262 { 260 {
263 GNUNET_break (0); 261 GNUNET_break (0);
264 return NULL; 262 return NULL;
265 } 263 }
266 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; 264 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
267 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, &publicKey->key[0], size, &size); 265 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, &publicKey->key[0], size, &size);
268 if (rc) 266 if (rc)
269 { 267 {
270 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 268 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
271 return NULL; 269 return NULL;
272 } 270 }
273 size = 271 size = GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
274 GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH; 272 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG,
275 rc = 273 &publicKey->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH],
276 gcry_mpi_scan (&e, GCRYMPI_FMT_USG, 274 size, &size);
277 &publicKey->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH],
278 size, &size);
279 if (rc) 275 if (rc)
280 { 276 {
281 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 277 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
282 gcry_mpi_release (n); 278 gcry_mpi_release (n);
283 return NULL; 279 return NULL;
284 } 280 }
285 rc = gcry_sexp_build (&result, &erroff, "(public-key(rsa(n %m)(e %m)))", n, 281 rc = gcry_sexp_build (&result, &erroff, "(public-key(rsa(n %m)(e %m)))", n,
286 e); 282 e);
287 gcry_mpi_release (n); 283 gcry_mpi_release (n);
288 gcry_mpi_release (e); 284 gcry_mpi_release (e);
289 if (rc) 285 if (rc)
290 { 286 {
291 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */ 287 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */
292 return NULL; 288 return NULL;
293 } 289 }
294 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); 290 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
295 ret->sexp = result; 291 ret->sexp = result;
296 return ret; 292 return ret;
@@ -316,10 +312,10 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
316 312
317#if EXTRA_CHECKS 313#if EXTRA_CHECKS
318 if (gcry_pk_testkey (hostkey->sexp)) 314 if (gcry_pk_testkey (hostkey->sexp))
319 { 315 {
320 GNUNET_break (0); 316 GNUNET_break (0);
321 return NULL; 317 return NULL;
322 } 318 }
323#endif 319#endif
324 320
325 memset (pkv, 0, sizeof (gcry_mpi_t) * 6); 321 memset (pkv, 0, sizeof (gcry_mpi_t) * 6);
@@ -337,21 +333,21 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
337 GNUNET_assert (0 == rc); 333 GNUNET_assert (0 == rc);
338 size = sizeof (struct RsaPrivateKeyBinaryEncoded); 334 size = sizeof (struct RsaPrivateKeyBinaryEncoded);
339 for (i = 0; i < 6; i++) 335 for (i = 0; i < 6; i++)
336 {
337 if (pkv[i] != NULL)
340 { 338 {
341 if (pkv[i] != NULL) 339 GNUNET_assert (0 ==
342 { 340 gcry_mpi_aprint (GCRYMPI_FMT_USG,
343 GNUNET_assert (0 == 341 (unsigned char **) &pbu[i], &sizes[i],
344 gcry_mpi_aprint (GCRYMPI_FMT_USG, 342 pkv[i]));
345 (unsigned char **) &pbu[i], 343 size += sizes[i];
346 &sizes[i], pkv[i]));
347 size += sizes[i];
348 }
349 else
350 {
351 pbu[i] = NULL;
352 sizes[i] = 0;
353 }
354 } 344 }
345 else
346 {
347 pbu[i] = NULL;
348 sizes[i] = 0;
349 }
350 }
355 GNUNET_assert (size < 65536); 351 GNUNET_assert (size < 65536);
356 retval = GNUNET_malloc (size); 352 retval = GNUNET_malloc (size);
357 retval->len = htons (size); 353 retval->len = htons (size);
@@ -376,12 +372,12 @@ rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
376 retval->sizedmq1 = htons (0); 372 retval->sizedmq1 = htons (0);
377 memcpy (&((char *) (&retval[1]))[i], pbu[5], sizes[5]); 373 memcpy (&((char *) (&retval[1]))[i], pbu[5], sizes[5]);
378 for (i = 0; i < 6; i++) 374 for (i = 0; i < 6; i++)
379 { 375 {
380 if (pkv[i] != NULL) 376 if (pkv[i] != NULL)
381 gcry_mpi_release (pkv[i]); 377 gcry_mpi_release (pkv[i]);
382 if (pbu[i] != NULL) 378 if (pbu[i] != NULL)
383 free (pbu[i]); 379 free (pbu[i]);
384 } 380 }
385 return retval; 381 return retval;
386} 382}
387 383
@@ -397,7 +393,7 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len)
397{ 393{
398 struct GNUNET_CRYPTO_RsaPrivateKey *ret; 394 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
399 const struct RsaPrivateKeyBinaryEncoded *encoding = 395 const struct RsaPrivateKeyBinaryEncoded *encoding =
400 (const struct RsaPrivateKeyBinaryEncoded *) buf; 396 (const struct RsaPrivateKeyBinaryEncoded *) buf;
401 gcry_sexp_t res; 397 gcry_sexp_t res;
402 gcry_mpi_t n, e, d, p, q, u; 398 gcry_mpi_t n, e, d, p, q, u;
403 int rc; 399 int rc;
@@ -412,122 +408,121 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len)
412 pos = 0; 408 pos = 0;
413 size = ntohs (encoding->sizen); 409 size = ntohs (encoding->sizen);
414 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, 410 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG,
415 &((const unsigned char *) (&encoding[1]))[pos], size, 411 &((const unsigned char *) (&encoding[1]))[pos], size,
416 &size); 412 &size);
417 pos += ntohs (encoding->sizen); 413 pos += ntohs (encoding->sizen);
418 if (rc) 414 if (rc)
419 { 415 {
420 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 416 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
421 return NULL; 417 return NULL;
422 } 418 }
423 size = ntohs (encoding->sizee); 419 size = ntohs (encoding->sizee);
424 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG, 420 rc = gcry_mpi_scan (&e, GCRYMPI_FMT_USG,
425 &((const unsigned char *) (&encoding[1]))[pos], size, 421 &((const unsigned char *) (&encoding[1]))[pos], size,
426 &size); 422 &size);
427 pos += ntohs (encoding->sizee); 423 pos += ntohs (encoding->sizee);
428 if (rc) 424 if (rc)
429 { 425 {
430 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 426 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
431 gcry_mpi_release (n); 427 gcry_mpi_release (n);
432 return NULL; 428 return NULL;
433 } 429 }
434 size = ntohs (encoding->sized); 430 size = ntohs (encoding->sized);
435 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG, 431 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG,
436 &((const unsigned char *) (&encoding[1]))[pos], size, 432 &((const unsigned char *) (&encoding[1]))[pos], size,
437 &size); 433 &size);
438 pos += ntohs (encoding->sized); 434 pos += ntohs (encoding->sized);
439 if (rc) 435 if (rc)
436 {
437 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
438 gcry_mpi_release (n);
439 gcry_mpi_release (e);
440 return NULL;
441 }
442 /* swap p and q! */
443 size = ntohs (encoding->sizep);
444 if (size > 0)
445 {
446 rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG,
447 &((const unsigned char *) (&encoding[1]))[pos], size,
448 &size);
449 pos += ntohs (encoding->sizep);
450 if (rc)
440 { 451 {
441 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 452 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
442 gcry_mpi_release (n); 453 gcry_mpi_release (n);
443 gcry_mpi_release (e); 454 gcry_mpi_release (e);
455 gcry_mpi_release (d);
444 return NULL; 456 return NULL;
445 } 457 }
446 /* swap p and q! */ 458 }
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 }
463 else 459 else
464 q = NULL; 460 q = NULL;
465 size = ntohs (encoding->sizeq); 461 size = ntohs (encoding->sizeq);
466 if (size > 0) 462 if (size > 0)
463 {
464 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG,
465 &((const unsigned char *) (&encoding[1]))[pos], size,
466 &size);
467 pos += ntohs (encoding->sizeq);
468 if (rc)
467 { 469 {
468 rc = gcry_mpi_scan (&p, GCRYMPI_FMT_USG, 470 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
469 &((const unsigned char *) (&encoding[1]))[pos], 471 gcry_mpi_release (n);
470 size, &size); 472 gcry_mpi_release (e);
471 pos += ntohs (encoding->sizeq); 473 gcry_mpi_release (d);
472 if (rc) 474 if (q != NULL)
473 { 475 gcry_mpi_release (q);
474 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 476 return NULL;
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 }
482 } 477 }
478 }
483 else 479 else
484 p = NULL; 480 p = NULL;
485 pos += ntohs (encoding->sizedmp1); 481 pos += ntohs (encoding->sizedmp1);
486 pos += ntohs (encoding->sizedmq1); 482 pos += ntohs (encoding->sizedmq1);
487 size = 483 size =
488 ntohs (encoding->len) - sizeof (struct RsaPrivateKeyBinaryEncoded) - pos; 484 ntohs (encoding->len) - sizeof (struct RsaPrivateKeyBinaryEncoded) - pos;
489 if (size > 0) 485 if (size > 0)
486 {
487 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG,
488 &((const unsigned char *) (&encoding[1]))[pos], size,
489 &size);
490 if (rc)
490 { 491 {
491 rc = gcry_mpi_scan (&u, GCRYMPI_FMT_USG, 492 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
492 &((const unsigned char *) (&encoding[1]))[pos], 493 gcry_mpi_release (n);
493 size, &size); 494 gcry_mpi_release (e);
494 if (rc) 495 gcry_mpi_release (d);
495 { 496 if (p != NULL)
496 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 497 gcry_mpi_release (p);
497 gcry_mpi_release (n); 498 if (q != NULL)
498 gcry_mpi_release (e); 499 gcry_mpi_release (q);
499 gcry_mpi_release (d); 500 return NULL;
500 if (p != NULL)
501 gcry_mpi_release (p);
502 if (q != NULL)
503 gcry_mpi_release (q);
504 return NULL;
505 }
506 } 501 }
502 }
507 else 503 else
508 u = NULL; 504 u = NULL;
509 505
510 if ((p != NULL) && (q != NULL) && (u != NULL)) 506 if ((p != NULL) && (q != NULL) && (u != NULL))
507 {
508 rc = gcry_sexp_build (&res, &size, /* erroff */
509 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))",
510 n, e, d, p, q, u);
511 }
512 else
513 {
514 if ((p != NULL) && (q != NULL))
511 { 515 {
512 rc = gcry_sexp_build (&res, &size, /* erroff */ 516 rc = gcry_sexp_build (&res, &size, /* erroff */
513 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))", 517 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))",
514 n, e, d, p, q, u); 518 n, e, d, p, q);
515 } 519 }
516 else 520 else
517 { 521 {
518 if ((p != NULL) && (q != NULL)) 522 rc = gcry_sexp_build (&res, &size, /* erroff */
519 { 523 "(private-key(rsa(n %m)(e %m)(d %m)))", n, e, d);
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 }
530 } 524 }
525 }
531 gcry_mpi_release (n); 526 gcry_mpi_release (n);
532 gcry_mpi_release (e); 527 gcry_mpi_release (e);
533 gcry_mpi_release (d); 528 gcry_mpi_release (d);
@@ -542,10 +537,10 @@ GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len)
542 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 537 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
543#if EXTRA_CHECKS 538#if EXTRA_CHECKS
544 if (gcry_pk_testkey (res)) 539 if (gcry_pk_testkey (res))
545 { 540 {
546 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 541 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
547 return NULL; 542 return NULL;
548 } 543 }
549#endif 544#endif
550 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey)); 545 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
551 ret->sexp = res; 546 ret->sexp = res;
@@ -582,172 +577,165 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
582 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename)) 577 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
583 return NULL; 578 return NULL;
584 while (GNUNET_YES != GNUNET_DISK_file_test (filename)) 579 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
580 {
581 fd = GNUNET_DISK_file_open (filename,
582 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
583 | GNUNET_DISK_OPEN_FAILIFEXISTS,
584 GNUNET_DISK_PERM_USER_READ |
585 GNUNET_DISK_PERM_USER_WRITE);
586 if (NULL == fd)
585 { 587 {
586 fd = GNUNET_DISK_file_open (filename, 588 if (errno == EEXIST)
587 GNUNET_DISK_OPEN_WRITE | 589 {
588 GNUNET_DISK_OPEN_CREATE | 590 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
589 GNUNET_DISK_OPEN_FAILIFEXISTS, 591 {
590 GNUNET_DISK_PERM_USER_READ | 592 /* must exist but not be accessible, fail for good! */
591 GNUNET_DISK_PERM_USER_WRITE); 593 if (0 != ACCESS (filename, R_OK))
592 if (NULL == fd) 594 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
593 { 595 else
594 if (errno == EEXIST) 596 GNUNET_break (0); /* what is going on!? */
595 { 597 return NULL;
596 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 598 }
597 { 599 continue;
598 /* must exist but not be accessible, fail for good! */ 600 }
599 if (0 != ACCESS (filename, R_OK)) 601 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
600 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", 602 return NULL;
601 filename); 603 }
602 else 604 cnt = 0;
603 GNUNET_break (0); /* what is going on!? */ 605
604 return NULL; 606 while (GNUNET_YES !=
605 } 607 GNUNET_DISK_file_lock (fd, 0,
606 continue; 608 sizeof (struct RsaPrivateKeyBinaryEncoded),
607 } 609 GNUNET_YES))
608 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); 610 {
609 return NULL; 611 sleep (1);
610 } 612 if (0 == ++cnt % 10)
611 cnt = 0; 613 {
612 614 ec = errno;
613 while (GNUNET_YES != 615 LOG (GNUNET_ERROR_TYPE_ERROR,
614 GNUNET_DISK_file_lock (fd, 0, 616 _("Could not aquire lock on file `%s': %s...\n"), filename,
615 sizeof (struct 617 STRERROR (ec));
616 RsaPrivateKeyBinaryEncoded), 618 }
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;
651 } 619 }
620 LOG (GNUNET_ERROR_TYPE_INFO,
621 _("Creating a new private key. This may take a while.\n"));
622 ret = GNUNET_CRYPTO_rsa_key_create ();
623 GNUNET_assert (ret != NULL);
624 enc = rsa_encode_key (ret);
625 GNUNET_assert (enc != NULL);
626 GNUNET_assert (ntohs (enc->len) ==
627 GNUNET_DISK_file_write (fd, enc, ntohs (enc->len)));
628 GNUNET_free (enc);
629
630 GNUNET_DISK_file_sync (fd);
631 if (GNUNET_YES !=
632 GNUNET_DISK_file_unlock (fd, 0,
633 sizeof (struct RsaPrivateKeyBinaryEncoded)))
634 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
635 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
636 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub);
637 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
638 LOG (GNUNET_ERROR_TYPE_INFO,
639 _("I am host `%s'. Stored new private key in `%s'.\n"),
640 GNUNET_i2s (&pid), filename);
641 return ret;
642 }
652 /* hostkey file exists already, read it! */ 643 /* hostkey file exists already, read it! */
653 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 644 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
654 GNUNET_DISK_PERM_NONE); 645 GNUNET_DISK_PERM_NONE);
655 if (NULL == fd) 646 if (NULL == fd)
647 {
648 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
649 return NULL;
650 }
651 cnt = 0;
652 while (1)
653 {
654 if (GNUNET_YES !=
655 GNUNET_DISK_file_lock (fd, 0,
656 sizeof (struct RsaPrivateKeyBinaryEncoded),
657 GNUNET_NO))
656 { 658 {
657 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); 659 if (0 == ++cnt % 60)
660 {
661 ec = errno;
662 LOG (GNUNET_ERROR_TYPE_ERROR,
663 _("Could not aquire lock on file `%s': %s...\n"), filename,
664 STRERROR (ec));
665 LOG (GNUNET_ERROR_TYPE_ERROR,
666 _
667 ("This may be ok if someone is currently generating a hostkey.\n"));
668 }
669 sleep (1);
670 continue;
671 }
672 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
673 {
674 /* eh, what!? File we opened is now gone!? */
675 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
676 if (GNUNET_YES !=
677 GNUNET_DISK_file_unlock (fd, 0,
678 sizeof (struct RsaPrivateKeyBinaryEncoded)))
679 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
680 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
681
658 return NULL; 682 return NULL;
659 } 683 }
660 cnt = 0; 684 if (GNUNET_YES != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES))
661 while (1) 685 fs = 0;
686 if (fs < sizeof (struct RsaPrivateKeyBinaryEncoded))
662 { 687 {
688 /* maybe we got the read lock before the hostkey generating
689 * process had a chance to get the write lock; give it up! */
663 if (GNUNET_YES != 690 if (GNUNET_YES !=
664 GNUNET_DISK_file_lock (fd, 0, 691 GNUNET_DISK_file_unlock (fd, 0,
665 sizeof (struct RsaPrivateKeyBinaryEncoded), 692 sizeof (struct RsaPrivateKeyBinaryEncoded)))
666 GNUNET_NO)) 693 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
667 { 694 if (0 == ++cnt % 10)
668 if (0 == ++cnt % 60) 695 {
669 { 696 LOG (GNUNET_ERROR_TYPE_ERROR,
670 ec = errno; 697 _
671 LOG (GNUNET_ERROR_TYPE_ERROR, 698 ("When trying to read hostkey file `%s' I found %u bytes but I need at least %u.\n"),
672 _("Could not aquire lock on file `%s': %s...\n"), filename, 699 filename, (unsigned int) fs,
673 STRERROR (ec)); 700 (unsigned int) sizeof (struct RsaPrivateKeyBinaryEncoded));
674 LOG (GNUNET_ERROR_TYPE_ERROR, 701 LOG (GNUNET_ERROR_TYPE_ERROR,
675 _ 702 _
676 ("This may be ok if someone is currently generating a hostkey.\n")); 703 ("This may be ok if someone is currently generating a hostkey.\n"));
677 } 704 }
678 sleep (1); 705 sleep (2); /* wait a bit longer! */
679 continue; 706 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;
720 } 707 }
708 break;
709 }
721 enc = GNUNET_malloc (fs); 710 enc = GNUNET_malloc (fs);
722 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs)); 711 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs));
723 len = ntohs (enc->len); 712 len = ntohs (enc->len);
724 ret = NULL; 713 ret = NULL;
725 if ((len != fs) || 714 if ((len != fs) ||
726 (NULL == (ret = GNUNET_CRYPTO_rsa_decode_key ((char *) enc, len)))) 715 (NULL == (ret = GNUNET_CRYPTO_rsa_decode_key ((char *) enc, len))))
716 {
717 LOG (GNUNET_ERROR_TYPE_ERROR,
718 _("File `%s' does not contain a valid private key. Deleting it.\n"),
719 filename);
720 if (0 != UNLINK (filename))
727 { 721 {
728 LOG (GNUNET_ERROR_TYPE_ERROR, 722 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
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 }
736 } 723 }
724 }
737 GNUNET_free (enc); 725 GNUNET_free (enc);
738 if (GNUNET_YES != 726 if (GNUNET_YES !=
739 GNUNET_DISK_file_unlock (fd, 0, 727 GNUNET_DISK_file_unlock (fd, 0,
740 sizeof (struct RsaPrivateKeyBinaryEncoded))) 728 sizeof (struct RsaPrivateKeyBinaryEncoded)))
741 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 729 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
742 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); 730 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
743 if (ret != NULL) 731 if (ret != NULL)
744 { 732 {
745 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub); 733 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub);
746 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey); 734 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
747 LOG (GNUNET_ERROR_TYPE_INFO, 735 LOG (GNUNET_ERROR_TYPE_INFO,
748 _("I am host `%s'. Read private key from `%s'.\n"), 736 _("I am host `%s'. Read private key from `%s'.\n"), GNUNET_i2s (&pid),
749 GNUNET_i2s (&pid), filename); 737 filename);
750 } 738 }
751 return ret; 739 return ret;
752} 740}
753 741
@@ -764,9 +752,9 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
764 */ 752 */
765int 753int
766GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size, 754GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
767 const struct 755 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
768 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey, 756 *publicKey,
769 struct GNUNET_CRYPTO_RsaEncryptedData *target) 757 struct GNUNET_CRYPTO_RsaEncryptedData *target)
770{ 758{
771 gcry_sexp_t result; 759 gcry_sexp_t result;
772 gcry_sexp_t data; 760 gcry_sexp_t data;
@@ -782,10 +770,10 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
782 return GNUNET_SYSERR; 770 return GNUNET_SYSERR;
783 isize = size; 771 isize = size;
784 GNUNET_assert (0 == 772 GNUNET_assert (0 ==
785 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize)); 773 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize));
786 GNUNET_assert (0 == 774 GNUNET_assert (0 ==
787 gcry_sexp_build (&data, &erroff, 775 gcry_sexp_build (&data, &erroff,
788 "(data (flags pkcs1)(value %m))", val)); 776 "(data (flags pkcs1)(value %m))", val));
789 gcry_mpi_release (val); 777 gcry_mpi_release (val);
790 GNUNET_assert (0 == gcry_pk_encrypt (&result, data, pubkey->sexp)); 778 GNUNET_assert (0 == gcry_pk_encrypt (&result, data, pubkey->sexp));
791 gcry_sexp_release (data); 779 gcry_sexp_release (data);
@@ -795,11 +783,11 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
795 gcry_sexp_release (result); 783 gcry_sexp_release (result);
796 isize = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData); 784 isize = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData);
797 GNUNET_assert (0 == 785 GNUNET_assert (0 ==
798 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) target, 786 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) target,
799 isize, &isize, rval)); 787 isize, &isize, rval));
800 gcry_mpi_release (rval); 788 gcry_mpi_release (rval);
801 adjust (&target->encoding[0], isize, 789 adjust (&target->encoding[0], isize,
802 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData)); 790 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData));
803 return GNUNET_OK; 791 return GNUNET_OK;
804} 792}
805 793
@@ -815,8 +803,8 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
815 */ 803 */
816ssize_t 804ssize_t
817GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key, 805GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key,
818 const struct GNUNET_CRYPTO_RsaEncryptedData * 806 const struct GNUNET_CRYPTO_RsaEncryptedData * block,
819 block, void *result, size_t max) 807 void *result, size_t max)
820{ 808{
821 gcry_sexp_t resultsexp; 809 gcry_sexp_t resultsexp;
822 gcry_sexp_t data; 810 gcry_sexp_t data;
@@ -831,22 +819,21 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key,
831#endif 819#endif
832 size = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData); 820 size = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData);
833 GNUNET_assert (0 == 821 GNUNET_assert (0 ==
834 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, &block->encoding[0], 822 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, &block->encoding[0],
835 size, &size)); 823 size, &size));
836 GNUNET_assert (0 == 824 GNUNET_assert (0 ==
837 gcry_sexp_build (&data, &erroff, 825 gcry_sexp_build (&data, &erroff, "(enc-val(flags)(rsa(a %m)))",
838 "(enc-val(flags)(rsa(a %m)))", val)); 826 val));
839 gcry_mpi_release (val); 827 gcry_mpi_release (val);
840 GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp)); 828 GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp));
841 gcry_sexp_release (data); 829 gcry_sexp_release (data);
842 /* resultsexp has format "(value %m)" */ 830 /* resultsexp has format "(value %m)" */
843 GNUNET_assert (NULL != 831 GNUNET_assert (NULL !=
844 (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG))); 832 (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG)));
845 gcry_sexp_release (resultsexp); 833 gcry_sexp_release (resultsexp);
846 tmp = GNUNET_malloc (max + HOSTKEY_LEN / 8); 834 tmp = GNUNET_malloc (max + HOSTKEY_LEN / 8);
847 size = max + HOSTKEY_LEN / 8; 835 size = max + HOSTKEY_LEN / 8;
848 GNUNET_assert (0 == 836 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val));
849 gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val));
850 gcry_mpi_release (val); 837 gcry_mpi_release (val);
851 endp = tmp; 838 endp = tmp;
852 endp += (size - max); 839 endp += (size - max);
@@ -867,8 +854,8 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key,
867 */ 854 */
868int 855int
869GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key, 856GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
870 const struct GNUNET_CRYPTO_RsaSignaturePurpose 857 const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose,
871 *purpose, struct GNUNET_CRYPTO_RsaSignature *sig) 858 struct GNUNET_CRYPTO_RsaSignature *sig)
872{ 859{
873 gcry_sexp_t result; 860 gcry_sexp_t result;
874 gcry_sexp_t data; 861 gcry_sexp_t data;
@@ -884,10 +871,10 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
884 buff = GNUNET_malloc (bufSize); 871 buff = GNUNET_malloc (bufSize);
885 memcpy (buff, FORMATSTRING, bufSize); 872 memcpy (buff, FORMATSTRING, bufSize);
886 memcpy (&buff 873 memcpy (&buff
887 [bufSize - 874 [bufSize -
888 strlen 875 strlen
889 ("0123456789012345678901234567890123456789012345678901234567890123))") 876 ("0123456789012345678901234567890123456789012345678901234567890123))")
890 - 1], &hc, sizeof (GNUNET_HashCode)); 877 - 1], &hc, sizeof (GNUNET_HashCode));
891 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0)); 878 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
892 GNUNET_free (buff); 879 GNUNET_free (buff);
893 GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp)); 880 GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp));
@@ -896,8 +883,8 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
896 gcry_sexp_release (result); 883 gcry_sexp_release (result);
897 ssize = sizeof (struct GNUNET_CRYPTO_RsaSignature); 884 ssize = sizeof (struct GNUNET_CRYPTO_RsaSignature);
898 GNUNET_assert (0 == 885 GNUNET_assert (0 ==
899 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) sig, 886 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) sig, ssize,
900 ssize, &ssize, rval)); 887 &ssize, rval));
901 gcry_mpi_release (rval); 888 gcry_mpi_release (rval);
902 adjust (sig->sig, ssize, sizeof (struct GNUNET_CRYPTO_RsaSignature)); 889 adjust (sig->sig, ssize, sizeof (struct GNUNET_CRYPTO_RsaSignature));
903 return GNUNET_OK; 890 return GNUNET_OK;
@@ -915,11 +902,11 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
915 */ 902 */
916int 903int
917GNUNET_CRYPTO_rsa_verify (uint32_t purpose, 904GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
918 const struct GNUNET_CRYPTO_RsaSignaturePurpose 905 const struct GNUNET_CRYPTO_RsaSignaturePurpose
919 *validate, 906 *validate,
920 const struct GNUNET_CRYPTO_RsaSignature *sig, 907 const struct GNUNET_CRYPTO_RsaSignature *sig,
921 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 908 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
922 *publicKey) 909 *publicKey)
923{ 910{
924 gcry_sexp_t data; 911 gcry_sexp_t data;
925 gcry_sexp_t sigdata; 912 gcry_sexp_t sigdata;
@@ -933,44 +920,44 @@ GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
933 int rc; 920 int rc;
934 921
935 if (purpose != ntohl (validate->purpose)) 922 if (purpose != ntohl (validate->purpose))
936 return GNUNET_SYSERR; /* purpose mismatch */ 923 return GNUNET_SYSERR; /* purpose mismatch */
937 GNUNET_CRYPTO_hash (validate, ntohl (validate->size), &hc); 924 GNUNET_CRYPTO_hash (validate, ntohl (validate->size), &hc);
938 size = sizeof (struct GNUNET_CRYPTO_RsaSignature); 925 size = sizeof (struct GNUNET_CRYPTO_RsaSignature);
939 GNUNET_assert (0 == 926 GNUNET_assert (0 ==
940 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, 927 gcry_mpi_scan (&val, GCRYMPI_FMT_USG,
941 (const unsigned char *) sig, size, &size)); 928 (const unsigned char *) sig, size, &size));
942 GNUNET_assert (0 == 929 GNUNET_assert (0 ==
943 gcry_sexp_build (&sigdata, &erroff, "(sig-val(rsa(s %m)))", 930 gcry_sexp_build (&sigdata, &erroff, "(sig-val(rsa(s %m)))",
944 val)); 931 val));
945 gcry_mpi_release (val); 932 gcry_mpi_release (val);
946 bufSize = strlen (FORMATSTRING) + 1; 933 bufSize = strlen (FORMATSTRING) + 1;
947 buff = GNUNET_malloc (bufSize); 934 buff = GNUNET_malloc (bufSize);
948 memcpy (buff, FORMATSTRING, bufSize); 935 memcpy (buff, FORMATSTRING, bufSize);
949 memcpy (&buff 936 memcpy (&buff
950 [strlen (FORMATSTRING) - 937 [strlen (FORMATSTRING) -
951 strlen 938 strlen
952 ("0123456789012345678901234567890123456789012345678901234567890123))")], 939 ("0123456789012345678901234567890123456789012345678901234567890123))")],
953 &hc, sizeof (GNUNET_HashCode)); 940 &hc, sizeof (GNUNET_HashCode));
954 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0)); 941 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
955 GNUNET_free (buff); 942 GNUNET_free (buff);
956 hostkey = public2PrivateKey (publicKey); 943 hostkey = public2PrivateKey (publicKey);
957 if (hostkey == NULL) 944 if (hostkey == NULL)
958 { 945 {
959 gcry_sexp_release (data); 946 gcry_sexp_release (data);
960 gcry_sexp_release (sigdata); 947 gcry_sexp_release (sigdata);
961 return GNUNET_SYSERR; 948 return GNUNET_SYSERR;
962 } 949 }
963 rc = gcry_pk_verify (sigdata, data, hostkey->sexp); 950 rc = gcry_pk_verify (sigdata, data, hostkey->sexp);
964 GNUNET_CRYPTO_rsa_key_free (hostkey); 951 GNUNET_CRYPTO_rsa_key_free (hostkey);
965 gcry_sexp_release (data); 952 gcry_sexp_release (data);
966 gcry_sexp_release (sigdata); 953 gcry_sexp_release (sigdata);
967 if (rc) 954 if (rc)
968 { 955 {
969 LOG (GNUNET_ERROR_TYPE_WARNING, 956 LOG (GNUNET_ERROR_TYPE_WARNING,
970 _("RSA signature verification failed at %s:%d: %s\n"), __FILE__, 957 _("RSA signature verification failed at %s:%d: %s\n"), __FILE__,
971 __LINE__, gcry_strerror (rc)); 958 __LINE__, gcry_strerror (rc));
972 return GNUNET_SYSERR; 959 return GNUNET_SYSERR;
973 } 960 }
974 return GNUNET_OK; 961 return GNUNET_OK;
975} 962}
976 963
diff --git a/src/util/disk.c b/src/util/disk.c
index 12f985791..2bdcf3262 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -67,8 +67,8 @@
67#define PIPE_BUF 512 67#define PIPE_BUF 512
68ULONG PipeSerialNumber; 68ULONG PipeSerialNumber;
69#endif 69#endif
70#define _IFMT 0170000 /* type of file */ 70#define _IFMT 0170000 /* type of file */
71#define _IFLNK 0120000 /* symbolic link */ 71#define _IFLNK 0120000 /* symbolic link */
72#define S_ISLNK(m) (((m)&_IFMT) == _IFLNK) 72#define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
73#else 73#else
74#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?)
@@ -165,25 +165,25 @@ getSizeRec (void *cls, const char *fn)
165 165
166#ifdef HAVE_STAT64 166#ifdef HAVE_STAT64
167 if (0 != STAT64 (fn, &buf)) 167 if (0 != STAT64 (fn, &buf))
168 { 168 {
169 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat64", fn); 169 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat64", fn);
170 return GNUNET_SYSERR; 170 return GNUNET_SYSERR;
171 } 171 }
172#else 172#else
173 if (0 != STAT (fn, &buf)) 173 if (0 != STAT (fn, &buf))
174 { 174 {
175 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fn); 175 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fn);
176 return GNUNET_SYSERR; 176 return GNUNET_SYSERR;
177 } 177 }
178#endif 178#endif
179 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))
180 gfsd->total += buf.st_size; 180 gfsd->total += buf.st_size;
181 if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) && 181 if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) &&
182 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) 182 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
183 { 183 {
184 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) 184 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
185 return GNUNET_SYSERR; 185 return GNUNET_SYSERR;
186 } 186 }
187 return GNUNET_OK; 187 return GNUNET_OK;
188} 188}
189 189
@@ -215,13 +215,13 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
215 */ 215 */
216off_t 216off_t
217GNUNET_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,
218 enum GNUNET_DISK_Seek whence) 218 enum GNUNET_DISK_Seek whence)
219{ 219{
220 if (h == NULL) 220 if (h == NULL)
221 { 221 {
222 errno = EINVAL; 222 errno = EINVAL;
223 return GNUNET_SYSERR; 223 return GNUNET_SYSERR;
224 } 224 }
225 225
226#ifdef MINGW 226#ifdef MINGW
227 DWORD ret; 227 DWORD ret;
@@ -232,10 +232,10 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
232 232
233 ret = SetFilePointer (h->h, offset, NULL, t[whence]); 233 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
234 if (ret == INVALID_SET_FILE_POINTER) 234 if (ret == INVALID_SET_FILE_POINTER)
235 { 235 {
236 SetErrnoFromWinError (GetLastError ()); 236 SetErrnoFromWinError (GetLastError ());
237 return GNUNET_SYSERR; 237 return GNUNET_SYSERR;
238 } 238 }
239 return ret; 239 return ret;
240#else 240#else
241 static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET, 241 static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET,
@@ -261,7 +261,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
261 */ 261 */
262int 262int
263GNUNET_DISK_file_size (const char *filename, uint64_t * size, 263GNUNET_DISK_file_size (const char *filename, uint64_t * size,
264 int includeSymLinks) 264 int includeSymLinks)
265{ 265{
266 struct GetFileSizeData gfsd; 266 struct GetFileSizeData gfsd;
267 int ret; 267 int ret;
@@ -292,29 +292,29 @@ GNUNET_DISK_file_size (const char *filename, uint64_t * size,
292 */ 292 */
293int 293int
294GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, 294GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
295 uint64_t * ino) 295 uint64_t * ino)
296{ 296{
297#if LINUX 297#if LINUX
298 struct stat sbuf; 298 struct stat sbuf;
299 struct statvfs fbuf; 299 struct statvfs fbuf;
300 300
301 if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf))) 301 if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf)))
302 { 302 {
303 *dev = (uint64_t) fbuf.f_fsid; 303 *dev = (uint64_t) fbuf.f_fsid;
304 *ino = (uint64_t) sbuf.st_ino; 304 *ino = (uint64_t) sbuf.st_ino;
305 return GNUNET_OK; 305 return GNUNET_OK;
306 } 306 }
307#elif SOMEBSD 307#elif SOMEBSD
308 struct stat sbuf; 308 struct stat sbuf;
309 struct statfs fbuf; 309 struct statfs fbuf;
310 310
311 if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf))) 311 if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf)))
312 { 312 {
313 *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || 313 *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 ||
314 ((uint64_t) fbuf.f_fsid.val[1]); 314 ((uint64_t) fbuf.f_fsid.val[1]);
315 *ino = (uint64_t) sbuf.st_ino; 315 *ino = (uint64_t) sbuf.st_ino;
316 return GNUNET_OK; 316 return GNUNET_OK;
317 } 317 }
318#elif WINDOWS 318#elif WINDOWS
319 // FIXME NILS: test this 319 // FIXME NILS: test this
320 struct GNUNET_DISK_FileHandle *fh; 320 struct GNUNET_DISK_FileHandle *fh;
@@ -327,11 +327,11 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
327 succ = GetFileInformationByHandle (fh->h, &info); 327 succ = GetFileInformationByHandle (fh->h, &info);
328 GNUNET_DISK_file_close (fh); 328 GNUNET_DISK_file_close (fh);
329 if (succ) 329 if (succ)
330 { 330 {
331 *dev = info.dwVolumeSerialNumber; 331 *dev = info.dwVolumeSerialNumber;
332 *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow); 332 *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow);
333 return GNUNET_OK; 333 return GNUNET_OK;
334 } 334 }
335 else 335 else
336 return GNUNET_SYSERR; 336 return GNUNET_SYSERR;
337 337
@@ -363,35 +363,35 @@ GNUNET_DISK_mktemp (const char *t)
363#if WINDOWS 363#if WINDOWS
364 && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) 364 && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':'))
365#endif 365#endif
366 ) 366 )
367 { 367 {
368 tmpdir = getenv ("TMPDIR"); 368 tmpdir = getenv ("TMPDIR");
369 tmpdir = tmpdir ? tmpdir : "/tmp"; 369 tmpdir = tmpdir ? tmpdir : "/tmp";
370 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); 370 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
371 } 371 }
372 else 372 else
373 { 373 {
374 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX"); 374 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
375 } 375 }
376#ifdef MINGW 376#ifdef MINGW
377 fn = (char *) GNUNET_malloc (MAX_PATH + 1); 377 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
378 if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn)) 378 if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn))
379 { 379 {
380 GNUNET_free (fn); 380 GNUNET_free (fn);
381 GNUNET_free (tmpl); 381 GNUNET_free (tmpl);
382 return NULL; 382 return NULL;
383 } 383 }
384 GNUNET_free (tmpl); 384 GNUNET_free (tmpl);
385#else 385#else
386 fn = tmpl; 386 fn = tmpl;
387#endif 387#endif
388 fd = mkstemp (fn); 388 fd = mkstemp (fn);
389 if (fd == -1) 389 if (fd == -1)
390 { 390 {
391 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); 391 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
392 GNUNET_free (fn); 392 GNUNET_free (fn);
393 return NULL; 393 return NULL;
394 } 394 }
395 if (0 != CLOSE (fd)) 395 if (0 != CLOSE (fd))
396 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn); 396 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
397 return fn; 397 return fn;
@@ -412,10 +412,10 @@ GNUNET_DISK_get_blocks_available (const char *part)
412 struct statvfs buf; 412 struct statvfs buf;
413 413
414 if (0 != statvfs (part, &buf)) 414 if (0 != statvfs (part, &buf))
415 { 415 {
416 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part); 416 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
417 return -1; 417 return -1;
418 } 418 }
419 return buf.f_bavail; 419 return buf.f_bavail;
420#elif MINGW 420#elif MINGW
421 DWORD dwDummy; 421 DWORD dwDummy;
@@ -430,22 +430,21 @@ GNUNET_DISK_get_blocks_available (const char *part)
430 GNUNET_free (path); 430 GNUNET_free (path);
431 szDrive[3] = 0; 431 szDrive[3] = 0;
432 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy)) 432 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
433 { 433 {
434 LOG (GNUNET_ERROR_TYPE_WARNING, 434 LOG (GNUNET_ERROR_TYPE_WARNING, _("`%s' failed for drive `%s': %u\n"),
435 _("`%s' failed for drive `%s': %u\n"), "GetDiskFreeSpace", 435 "GetDiskFreeSpace", szDrive, GetLastError ());
436 szDrive, GetLastError ());
437 436
438 return -1; 437 return -1;
439 } 438 }
440 return dwBlocks; 439 return dwBlocks;
441#else 440#else
442 struct statfs s; 441 struct statfs s;
443 442
444 if (0 != statfs (part, &s)) 443 if (0 != statfs (part, &s))
445 { 444 {
446 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part); 445 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
447 return -1; 446 return -1;
448 } 447 }
449 return s.f_bavail; 448 return s.f_bavail;
450#endif 449#endif
451} 450}
@@ -469,21 +468,21 @@ GNUNET_DISK_directory_test (const char *fil)
469 468
470 ret = STAT (fil, &filestat); 469 ret = STAT (fil, &filestat);
471 if (ret != 0) 470 if (ret != 0)
471 {
472 if (errno != ENOENT)
472 { 473 {
473 if (errno != ENOENT) 474 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
474 { 475 return GNUNET_SYSERR;
475 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
476 return GNUNET_SYSERR;
477 }
478 return GNUNET_NO;
479 } 476 }
477 return GNUNET_NO;
478 }
480 if (!S_ISDIR (filestat.st_mode)) 479 if (!S_ISDIR (filestat.st_mode))
481 return GNUNET_NO; 480 return GNUNET_NO;
482 if (ACCESS (fil, R_OK | X_OK) < 0) 481 if (ACCESS (fil, R_OK | X_OK) < 0)
483 { 482 {
484 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); 483 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
485 return GNUNET_SYSERR; 484 return GNUNET_SYSERR;
486 } 485 }
487 return GNUNET_YES; 486 return GNUNET_YES;
488} 487}
489 488
@@ -508,27 +507,27 @@ GNUNET_DISK_file_test (const char *fil)
508 507
509 ret = STAT (rdir, &filestat); 508 ret = STAT (rdir, &filestat);
510 if (ret != 0) 509 if (ret != 0)
510 {
511 if (errno != ENOENT)
511 { 512 {
512 if (errno != ENOENT) 513 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
513 {
514 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
515 GNUNET_free (rdir);
516 return GNUNET_SYSERR;
517 }
518 GNUNET_free (rdir); 514 GNUNET_free (rdir);
519 return GNUNET_NO; 515 return GNUNET_SYSERR;
520 } 516 }
517 GNUNET_free (rdir);
518 return GNUNET_NO;
519 }
521 if (!S_ISREG (filestat.st_mode)) 520 if (!S_ISREG (filestat.st_mode))
522 { 521 {
523 GNUNET_free (rdir); 522 GNUNET_free (rdir);
524 return GNUNET_NO; 523 return GNUNET_NO;
525 } 524 }
526 if (ACCESS (rdir, R_OK) < 0) 525 if (ACCESS (rdir, R_OK) < 0)
527 { 526 {
528 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir); 527 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
529 GNUNET_free (rdir); 528 GNUNET_free (rdir);
530 return GNUNET_SYSERR; 529 return GNUNET_SYSERR;
531 } 530 }
532 GNUNET_free (rdir); 531 GNUNET_free (rdir);
533 return GNUNET_YES; 532 return GNUNET_YES;
534} 533}
@@ -553,56 +552,56 @@ GNUNET_DISK_directory_create (const char *dir)
553 552
554 len = strlen (rdir); 553 len = strlen (rdir);
555#ifndef MINGW 554#ifndef MINGW
556 pos = 1; /* skip heading '/' */ 555 pos = 1; /* skip heading '/' */
557#else 556#else
558 /* Local or Network path? */ 557 /* Local or Network path? */
559 if (strncmp (rdir, "\\\\", 2) == 0) 558 if (strncmp (rdir, "\\\\", 2) == 0)
559 {
560 pos = 2;
561 while (rdir[pos])
560 { 562 {
561 pos = 2; 563 if (rdir[pos] == '\\')
562 while (rdir[pos]) 564 {
563 { 565 pos++;
564 if (rdir[pos] == '\\') 566 break;
565 { 567 }
566 pos++; 568 pos++;
567 break;
568 }
569 pos++;
570 }
571 } 569 }
570 }
572 else 571 else
573 { 572 {
574 pos = 3; /* strlen("C:\\") */ 573 pos = 3; /* strlen("C:\\") */
575 } 574 }
576#endif 575#endif
577 while (pos <= len) 576 while (pos <= len)
577 {
578 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
578 { 579 {
579 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) 580 rdir[pos] = '\0';
580 { 581 ret = GNUNET_DISK_directory_test (rdir);
581 rdir[pos] = '\0'; 582 if (ret == GNUNET_SYSERR)
582 ret = GNUNET_DISK_directory_test (rdir); 583 {
583 if (ret == GNUNET_SYSERR) 584 GNUNET_free (rdir);
584 { 585 return GNUNET_SYSERR;
585 GNUNET_free (rdir); 586 }
586 return GNUNET_SYSERR; 587 if (ret == GNUNET_NO)
587 } 588 {
588 if (ret == GNUNET_NO)
589 {
590#ifndef MINGW 589#ifndef MINGW
591 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ 590 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
592#else 591#else
593 ret = mkdir (rdir); 592 ret = mkdir (rdir);
594#endif 593#endif
595 if ((ret != 0) && (errno != EEXIST)) 594 if ((ret != 0) && (errno != EEXIST))
596 { 595 {
597 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); 596 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
598 GNUNET_free (rdir); 597 GNUNET_free (rdir);
599 return GNUNET_SYSERR; 598 return GNUNET_SYSERR;
600 } 599 }
601 } 600 }
602 rdir[pos] = DIR_SEPARATOR; 601 rdir[pos] = DIR_SEPARATOR;
603 }
604 pos++;
605 } 602 }
603 pos++;
604 }
606 GNUNET_free (rdir); 605 GNUNET_free (rdir);
607 return GNUNET_OK; 606 return GNUNET_OK;
608} 607}
@@ -649,37 +648,37 @@ GNUNET_DISK_directory_create_for_file (const char *filename)
649 */ 648 */
650ssize_t 649ssize_t
651GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, 650GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result,
652 size_t len) 651 size_t len)
653{ 652{
654 if (h == NULL) 653 if (h == NULL)
655 { 654 {
656 errno = EINVAL; 655 errno = EINVAL;
657 return GNUNET_SYSERR; 656 return GNUNET_SYSERR;
658 } 657 }
659 658
660#ifdef MINGW 659#ifdef MINGW
661 DWORD bytesRead; 660 DWORD bytesRead;
662 661
663 if (h->type != GNUNET_PIPE) 662 if (h->type != GNUNET_PIPE)
663 {
664 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
664 { 665 {
665 if (!ReadFile (h->h, result, len, &bytesRead, NULL)) 666 SetErrnoFromWinError (GetLastError ());
666 { 667 return GNUNET_SYSERR;
667 SetErrnoFromWinError (GetLastError ());
668 return GNUNET_SYSERR;
669 }
670 } 668 }
669 }
671 else 670 else
671 {
672 if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead))
672 { 673 {
673 if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead)) 674 if (GetLastError () != ERROR_IO_PENDING)
674 { 675 {
675 if (GetLastError () != ERROR_IO_PENDING) 676 SetErrnoFromWinError (GetLastError ());
676 { 677 return GNUNET_SYSERR;
677 SetErrnoFromWinError (GetLastError ()); 678 }
678 return GNUNET_SYSERR;
679 }
680 }
681 GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE);
682 } 679 }
680 GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE);
681 }
683 return bytesRead; 682 return bytesRead;
684#else 683#else
685 return read (h->fd, result, len); 684 return read (h->fd, result, len);
@@ -701,8 +700,7 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
701 struct GNUNET_DISK_FileHandle *fh; 700 struct GNUNET_DISK_FileHandle *fh;
702 ssize_t ret; 701 ssize_t ret;
703 702
704 fh = 703 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
705 GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
706 if (!fh) 704 if (!fh)
707 return GNUNET_SYSERR; 705 return GNUNET_SYSERR;
708 ret = GNUNET_DISK_file_read (fh, result, len); 706 ret = GNUNET_DISK_file_read (fh, result, len);
@@ -721,46 +719,46 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
721 */ 719 */
722ssize_t 720ssize_t
723GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, 721GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
724 const void *buffer, size_t n) 722 const void *buffer, size_t n)
725{ 723{
726 if (h == NULL) 724 if (h == NULL)
727 { 725 {
728 errno = EINVAL; 726 errno = EINVAL;
729 return GNUNET_SYSERR; 727 return GNUNET_SYSERR;
730 } 728 }
731 729
732#ifdef MINGW 730#ifdef MINGW
733 DWORD bytesWritten; 731 DWORD bytesWritten;
734 732
735 if (h->type != GNUNET_PIPE) 733 if (h->type != GNUNET_PIPE)
734 {
735 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
736 { 736 {
737 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) 737 SetErrnoFromWinError (GetLastError ());
738 { 738 return GNUNET_SYSERR;
739 SetErrnoFromWinError (GetLastError ());
740 return GNUNET_SYSERR;
741 }
742 } 739 }
740 }
743 else 741 else
744 { 742 {
745#if DEBUG_PIPE 743#if DEBUG_PIPE
746 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write\n"); 744 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write\n");
747#endif 745#endif
748 if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite)) 746 if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite))
749 { 747 {
750 if (GetLastError () != ERROR_IO_PENDING) 748 if (GetLastError () != ERROR_IO_PENDING)
751 { 749 {
752 SetErrnoFromWinError (GetLastError ()); 750 SetErrnoFromWinError (GetLastError ());
753#if DEBUG_PIPE 751#if DEBUG_PIPE
754 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n"); 752 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n");
755#endif 753#endif
756 return GNUNET_SYSERR; 754 return GNUNET_SYSERR;
757 } 755 }
758 } 756 }
759#if DEBUG_PIPE 757#if DEBUG_PIPE
760 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); 758 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
761#endif 759#endif
762 GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE); 760 GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE);
763 } 761 }
764 return bytesWritten; 762 return bytesWritten;
765#else 763#else
766 return write (h->fd, buffer, n); 764 return write (h->fd, buffer, n);
@@ -779,15 +777,14 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
779 */ 777 */
780ssize_t 778ssize_t
781GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n, 779GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
782 enum GNUNET_DISK_AccessPermissions mode) 780 enum GNUNET_DISK_AccessPermissions mode)
783{ 781{
784 struct GNUNET_DISK_FileHandle *fh; 782 struct GNUNET_DISK_FileHandle *fh;
785 ssize_t ret; 783 ssize_t ret;
786 784
787 fh = GNUNET_DISK_file_open (fn, 785 fh = GNUNET_DISK_file_open (fn,
788 GNUNET_DISK_OPEN_WRITE | 786 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
789 GNUNET_DISK_OPEN_TRUNCATE | 787 | GNUNET_DISK_OPEN_CREATE, mode);
790 GNUNET_DISK_OPEN_CREATE, mode);
791 if (!fh) 788 if (!fh)
792 return GNUNET_SYSERR; 789 return GNUNET_SYSERR;
793 ret = GNUNET_DISK_file_write (fh, buffer, n); 790 ret = GNUNET_DISK_file_write (fh, buffer, n);
@@ -808,8 +805,8 @@ GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
808 */ 805 */
809int 806int
810GNUNET_DISK_directory_scan (const char *dirName, 807GNUNET_DISK_directory_scan (const char *dirName,
811 GNUNET_FileNameCallback callback, 808 GNUNET_FileNameCallback callback,
812 void *callback_cls) 809 void *callback_cls)
813{ 810{
814 DIR *dinfo; 811 DIR *dinfo;
815 struct dirent *finfo; 812 struct dirent *finfo;
@@ -827,61 +824,61 @@ GNUNET_DISK_directory_scan (const char *dirName,
827 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) 824 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
828 dname[strlen (dname) - 1] = '\0'; 825 dname[strlen (dname) - 1] = '\0';
829 if (0 != STAT (dname, &istat)) 826 if (0 != STAT (dname, &istat))
830 { 827 {
831 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname); 828 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
832 GNUNET_free (dname); 829 GNUNET_free (dname);
833 return GNUNET_SYSERR; 830 return GNUNET_SYSERR;
834 } 831 }
835 if (!S_ISDIR (istat.st_mode)) 832 if (!S_ISDIR (istat.st_mode))
836 { 833 {
837 LOG (GNUNET_ERROR_TYPE_WARNING, 834 LOG (GNUNET_ERROR_TYPE_WARNING, _("Expected `%s' to be a directory!\n"),
838 _("Expected `%s' to be a directory!\n"), dirName); 835 dirName);
839 GNUNET_free (dname); 836 GNUNET_free (dname);
840 return GNUNET_SYSERR; 837 return GNUNET_SYSERR;
841 } 838 }
842 errno = 0; 839 errno = 0;
843 dinfo = OPENDIR (dname); 840 dinfo = OPENDIR (dname);
844 if ((errno == EACCES) || (dinfo == NULL)) 841 if ((errno == EACCES) || (dinfo == NULL))
845 { 842 {
846 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); 843 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
847 if (dinfo != NULL) 844 if (dinfo != NULL)
848 closedir (dinfo); 845 closedir (dinfo);
849 GNUNET_free (dname); 846 GNUNET_free (dname);
850 return GNUNET_SYSERR; 847 return GNUNET_SYSERR;
851 } 848 }
852 name_len = 256; 849 name_len = 256;
853 n_size = strlen (dname) + name_len + 2; 850 n_size = strlen (dname) + name_len + 2;
854 name = GNUNET_malloc (n_size); 851 name = GNUNET_malloc (n_size);
855 while ((finfo = readdir (dinfo)) != NULL) 852 while ((finfo = readdir (dinfo)) != NULL)
853 {
854 if ((0 == strcmp (finfo->d_name, ".")) ||
855 (0 == strcmp (finfo->d_name, "..")))
856 continue;
857 if (callback != NULL)
856 { 858 {
857 if ((0 == strcmp (finfo->d_name, ".")) || 859 if (name_len < strlen (finfo->d_name))
858 (0 == strcmp (finfo->d_name, ".."))) 860 {
859 continue; 861 GNUNET_free (name);
860 if (callback != NULL) 862 name_len = strlen (finfo->d_name);
861 { 863 n_size = strlen (dname) + name_len + 2;
862 if (name_len < strlen (finfo->d_name)) 864 name = GNUNET_malloc (n_size);
863 { 865 }
864 GNUNET_free (name); 866 /* dname can end in "/" only if dname == "/";
865 name_len = strlen (finfo->d_name); 867 * if dname does not end in "/", we need to add
866 n_size = strlen (dname) + name_len + 2; 868 * a "/" (otherwise, we must not!) */
867 name = GNUNET_malloc (n_size); 869 GNUNET_snprintf (name, n_size, "%s%s%s", dname,
868 } 870 (strcmp (dname, DIR_SEPARATOR_STR) ==
869 /* dname can end in "/" only if dname == "/"; 871 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
870 * if dname does not end in "/", we need to add 872 if (GNUNET_OK != callback (callback_cls, name))
871 * a "/" (otherwise, we must not!) */ 873 {
872 GNUNET_snprintf (name, n_size, "%s%s%s", dname, 874 closedir (dinfo);
873 (strcmp (dname, DIR_SEPARATOR_STR) == 875 GNUNET_free (name);
874 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); 876 GNUNET_free (dname);
875 if (GNUNET_OK != callback (callback_cls, name)) 877 return GNUNET_SYSERR;
876 { 878 }
877 closedir (dinfo);
878 GNUNET_free (name);
879 GNUNET_free (dname);
880 return GNUNET_SYSERR;
881 }
882 }
883 count++;
884 } 879 }
880 count++;
881 }
885 closedir (dinfo); 882 closedir (dinfo);
886 GNUNET_free (name); 883 GNUNET_free (name);
887 GNUNET_free (dname); 884 GNUNET_free (dname);
@@ -933,7 +930,7 @@ struct GNUNET_DISK_DirectoryIterator
933 */ 930 */
934static void 931static void
935directory_iterator_task (void *cls, 932directory_iterator_task (void *cls,
936 const struct GNUNET_SCHEDULER_TaskContext *tc) 933 const struct GNUNET_SCHEDULER_TaskContext *tc)
937{ 934{
938 struct GNUNET_DISK_DirectoryIterator *iter = cls; 935 struct GNUNET_DISK_DirectoryIterator *iter = cls;
939 char *name; 936 char *name;
@@ -958,35 +955,35 @@ directory_iterator_task (void *cls,
958 * GNUNET_SYSERR if abort was YES 955 * GNUNET_SYSERR if abort was YES
959 */ 956 */
960int 957int
961GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator 958GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
962 *iter, int can) 959 int can)
963{ 960{
964 struct dirent *finfo; 961 struct dirent *finfo;
965 962
966 GNUNET_assert (iter->next_name == NULL); 963 GNUNET_assert (iter->next_name == NULL);
967 if (can == GNUNET_YES) 964 if (can == GNUNET_YES)
968 { 965 {
969 closedir (iter->directory); 966 closedir (iter->directory);
970 GNUNET_free (iter->dirname); 967 GNUNET_free (iter->dirname);
971 GNUNET_free (iter); 968 GNUNET_free (iter);
972 return GNUNET_SYSERR; 969 return GNUNET_SYSERR;
973 } 970 }
974 while (NULL != (finfo = readdir (iter->directory))) 971 while (NULL != (finfo = readdir (iter->directory)))
975 { 972 {
976 if ((0 == strcmp (finfo->d_name, ".")) || 973 if ((0 == strcmp (finfo->d_name, ".")) ||
977 (0 == strcmp (finfo->d_name, ".."))) 974 (0 == strcmp (finfo->d_name, "..")))
978 continue; 975 continue;
979 GNUNET_asprintf (&iter->next_name, "%s%s%s", iter->dirname, 976 GNUNET_asprintf (&iter->next_name, "%s%s%s", iter->dirname,
980 DIR_SEPARATOR_STR, finfo->d_name); 977 DIR_SEPARATOR_STR, finfo->d_name);
981 break; 978 break;
982 } 979 }
983 if (finfo == NULL) 980 if (finfo == NULL)
984 { 981 {
985 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES); 982 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
986 return GNUNET_NO; 983 return GNUNET_NO;
987 } 984 }
988 GNUNET_SCHEDULER_add_with_priority (iter->priority, 985 GNUNET_SCHEDULER_add_with_priority (iter->priority, &directory_iterator_task,
989 &directory_iterator_task, iter); 986 iter);
990 return GNUNET_YES; 987 return GNUNET_YES;
991} 988}
992 989
@@ -1004,9 +1001,9 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
1004 */ 1001 */
1005void 1002void
1006GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio, 1003GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
1007 const char *dirName, 1004 const char *dirName,
1008 GNUNET_DISK_DirectoryIteratorCallback 1005 GNUNET_DISK_DirectoryIteratorCallback
1009 callback, void *callback_cls) 1006 callback, void *callback_cls)
1010{ 1007{
1011 struct GNUNET_DISK_DirectoryIterator *di; 1008 struct GNUNET_DISK_DirectoryIterator *di;
1012 1009
@@ -1015,11 +1012,11 @@ GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
1015 di->callback_cls = callback_cls; 1012 di->callback_cls = callback_cls;
1016 di->directory = OPENDIR (dirName); 1013 di->directory = OPENDIR (dirName);
1017 if (di->directory == NULL) 1014 if (di->directory == NULL)
1018 { 1015 {
1019 GNUNET_free (di); 1016 GNUNET_free (di);
1020 callback (callback_cls, NULL, NULL, NULL); 1017 callback (callback_cls, NULL, NULL, NULL);
1021 return; 1018 return;
1022 } 1019 }
1023 di->dirname = GNUNET_strdup (dirName); 1020 di->dirname = GNUNET_strdup (dirName);
1024 di->priority = prio; 1021 di->priority = prio;
1025 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO); 1022 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
@@ -1056,7 +1053,7 @@ GNUNET_DISK_directory_remove (const char *fileName)
1056 struct stat istat; 1053 struct stat istat;
1057 1054
1058 if (0 != LSTAT (fileName, &istat)) 1055 if (0 != LSTAT (fileName, &istat))
1059 return GNUNET_NO; /* file may not exist... */ 1056 return GNUNET_NO; /* file may not exist... */
1060 CHMOD (fileName, S_IWUSR | S_IRUSR | S_IXUSR); 1057 CHMOD (fileName, S_IWUSR | S_IRUSR | S_IXUSR);
1061 if (UNLINK (fileName) == 0) 1058 if (UNLINK (fileName) == 0)
1062 return GNUNET_OK; 1059 return GNUNET_OK;
@@ -1065,18 +1062,18 @@ GNUNET_DISK_directory_remove (const char *fileName)
1065 * sticky /tmp directory may result in EPERM on BSD. 1062 * sticky /tmp directory may result in EPERM on BSD.
1066 * So we also explicitly check "isDirectory" */ 1063 * So we also explicitly check "isDirectory" */
1067 (GNUNET_YES != GNUNET_DISK_directory_test (fileName))) 1064 (GNUNET_YES != GNUNET_DISK_directory_test (fileName)))
1068 { 1065 {
1069 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); 1066 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
1070 return GNUNET_SYSERR; 1067 return GNUNET_SYSERR;
1071 } 1068 }
1072 if (GNUNET_SYSERR == 1069 if (GNUNET_SYSERR ==
1073 GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL)) 1070 GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL))
1074 return GNUNET_SYSERR; 1071 return GNUNET_SYSERR;
1075 if (0 != RMDIR (fileName)) 1072 if (0 != RMDIR (fileName))
1076 { 1073 {
1077 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); 1074 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
1078 return GNUNET_SYSERR; 1075 return GNUNET_SYSERR;
1079 } 1076 }
1080 return GNUNET_OK; 1077 return GNUNET_OK;
1081} 1078}
1082 1079
@@ -1102,34 +1099,34 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
1102 return GNUNET_SYSERR; 1099 return GNUNET_SYSERR;
1103 pos = 0; 1100 pos = 0;
1104 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, 1101 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
1105 GNUNET_DISK_PERM_NONE); 1102 GNUNET_DISK_PERM_NONE);
1106 if (!in) 1103 if (!in)
1107 return GNUNET_SYSERR; 1104 return GNUNET_SYSERR;
1108 out = 1105 out =
1109 GNUNET_DISK_file_open (dst, 1106 GNUNET_DISK_file_open (dst,
1110 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 1107 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
1111 GNUNET_DISK_OPEN_FAILIFEXISTS, 1108 GNUNET_DISK_OPEN_FAILIFEXISTS,
1112 GNUNET_DISK_PERM_USER_READ | 1109 GNUNET_DISK_PERM_USER_READ |
1113 GNUNET_DISK_PERM_USER_WRITE | 1110 GNUNET_DISK_PERM_USER_WRITE |
1114 GNUNET_DISK_PERM_GROUP_READ | 1111 GNUNET_DISK_PERM_GROUP_READ |
1115 GNUNET_DISK_PERM_GROUP_WRITE); 1112 GNUNET_DISK_PERM_GROUP_WRITE);
1116 if (!out) 1113 if (!out)
1117 { 1114 {
1118 GNUNET_DISK_file_close (in); 1115 GNUNET_DISK_file_close (in);
1119 return GNUNET_SYSERR; 1116 return GNUNET_SYSERR;
1120 } 1117 }
1121 buf = GNUNET_malloc (COPY_BLK_SIZE); 1118 buf = GNUNET_malloc (COPY_BLK_SIZE);
1122 while (pos < size) 1119 while (pos < size)
1123 { 1120 {
1124 len = COPY_BLK_SIZE; 1121 len = COPY_BLK_SIZE;
1125 if (len > size - pos) 1122 if (len > size - pos)
1126 len = size - pos; 1123 len = size - pos;
1127 if (len != GNUNET_DISK_file_read (in, buf, len)) 1124 if (len != GNUNET_DISK_file_read (in, buf, len))
1128 goto FAIL; 1125 goto FAIL;
1129 if (len != GNUNET_DISK_file_write (out, buf, len)) 1126 if (len != GNUNET_DISK_file_write (out, buf, len))
1130 goto FAIL; 1127 goto FAIL;
1131 pos += len; 1128 pos += len;
1132 } 1129 }
1133 GNUNET_free (buf); 1130 GNUNET_free (buf);
1134 GNUNET_DISK_file_close (in); 1131 GNUNET_DISK_file_close (in);
1135 GNUNET_DISK_file_close (out); 1132 GNUNET_DISK_file_close (out);
@@ -1154,17 +1151,17 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1154 1151
1155 idx = fn; 1152 idx = fn;
1156 while (*idx) 1153 while (*idx)
1157 { 1154 {
1158 c = *idx; 1155 c = *idx;
1159
1160 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?'
1161 || c == '"' || c == '<' || c == '>' || c == '|')
1162 {
1163 *idx = '_';
1164 }
1165 1156
1166 idx++; 1157 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' ||
1158 c == '<' || c == '>' || c == '|')
1159 {
1160 *idx = '_';
1167 } 1161 }
1162
1163 idx++;
1164 }
1168} 1165}
1169 1166
1170 1167
@@ -1184,12 +1181,12 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1184 1181
1185 pws = getpwnam (user); 1182 pws = getpwnam (user);
1186 if (pws == NULL) 1183 if (pws == NULL)
1187 { 1184 {
1188 LOG (GNUNET_ERROR_TYPE_ERROR, 1185 LOG (GNUNET_ERROR_TYPE_ERROR,
1189 _("Cannot obtain information about user `%s': %s\n"), user, 1186 _("Cannot obtain information about user `%s': %s\n"), user,
1190 STRERROR (errno)); 1187 STRERROR (errno));
1191 return GNUNET_SYSERR; 1188 return GNUNET_SYSERR;
1192 } 1189 }
1193 if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) 1190 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1194 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename); 1191 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1195#endif 1192#endif
@@ -1207,13 +1204,13 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1207 */ 1204 */
1208int 1205int
1209GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart, 1206GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1210 off_t lockEnd, int excl) 1207 off_t lockEnd, int excl)
1211{ 1208{
1212 if (fh == NULL) 1209 if (fh == NULL)
1213 { 1210 {
1214 errno = EINVAL; 1211 errno = EINVAL;
1215 return GNUNET_SYSERR; 1212 return GNUNET_SYSERR;
1216 } 1213 }
1217 1214
1218#ifndef MINGW 1215#ifndef MINGW
1219 struct flock fl; 1216 struct flock fl;
@@ -1232,13 +1229,12 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1232 o.Offset = lockStart; 1229 o.Offset = lockStart;
1233 1230
1234 if (!LockFileEx 1231 if (!LockFileEx
1235 (fh->h, 1232 (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY,
1236 (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY, 0, 1233 0, lockEnd - lockStart, 0, &o))
1237 lockEnd - lockStart, 0, &o)) 1234 {
1238 { 1235 SetErrnoFromWinError (GetLastError ());
1239 SetErrnoFromWinError (GetLastError ()); 1236 return GNUNET_SYSERR;
1240 return GNUNET_SYSERR; 1237 }
1241 }
1242 1238
1243 return GNUNET_OK; 1239 return GNUNET_OK;
1244#endif 1240#endif
@@ -1254,13 +1250,13 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1254 */ 1250 */
1255int 1251int
1256GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, 1252GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1257 off_t unlockEnd) 1253 off_t unlockEnd)
1258{ 1254{
1259 if (fh == NULL) 1255 if (fh == NULL)
1260 { 1256 {
1261 errno = EINVAL; 1257 errno = EINVAL;
1262 return GNUNET_SYSERR; 1258 return GNUNET_SYSERR;
1263 } 1259 }
1264 1260
1265#ifndef MINGW 1261#ifndef MINGW
1266 struct flock fl; 1262 struct flock fl;
@@ -1279,10 +1275,10 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1279 o.Offset = unlockStart; 1275 o.Offset = unlockStart;
1280 1276
1281 if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o)) 1277 if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o))
1282 { 1278 {
1283 SetErrnoFromWinError (GetLastError ()); 1279 SetErrnoFromWinError (GetLastError ());
1284 return GNUNET_SYSERR; 1280 return GNUNET_SYSERR;
1285 } 1281 }
1286 1282
1287 return GNUNET_OK; 1283 return GNUNET_OK;
1288#endif 1284#endif
@@ -1303,7 +1299,7 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1303 */ 1299 */
1304struct GNUNET_DISK_FileHandle * 1300struct GNUNET_DISK_FileHandle *
1305GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, 1301GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1306 enum GNUNET_DISK_AccessPermissions perm) 1302 enum GNUNET_DISK_AccessPermissions perm)
1307{ 1303{
1308 char *expfn; 1304 char *expfn;
1309 struct GNUNET_DISK_FileHandle *ret; 1305 struct GNUNET_DISK_FileHandle *ret;
@@ -1324,17 +1320,17 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1324#ifndef MINGW 1320#ifndef MINGW
1325 mode = 0; 1321 mode = 0;
1326 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) 1322 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1327 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ 1323 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1328 else if (flags & GNUNET_DISK_OPEN_READ) 1324 else if (flags & GNUNET_DISK_OPEN_READ)
1329 oflags = O_RDONLY; 1325 oflags = O_RDONLY;
1330 else if (flags & GNUNET_DISK_OPEN_WRITE) 1326 else if (flags & GNUNET_DISK_OPEN_WRITE)
1331 oflags = O_WRONLY; 1327 oflags = O_WRONLY;
1332 else 1328 else
1333 { 1329 {
1334 GNUNET_break (0); 1330 GNUNET_break (0);
1335 GNUNET_free (expfn); 1331 GNUNET_free (expfn);
1336 return NULL; 1332 return NULL;
1337 } 1333 }
1338 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) 1334 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1339 oflags |= (O_CREAT | O_EXCL); 1335 oflags |= (O_CREAT | O_EXCL);
1340 if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1336 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
@@ -1342,22 +1338,22 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1342 if (flags & GNUNET_DISK_OPEN_APPEND) 1338 if (flags & GNUNET_DISK_OPEN_APPEND)
1343 oflags |= O_APPEND; 1339 oflags |= O_APPEND;
1344 if (flags & GNUNET_DISK_OPEN_CREATE) 1340 if (flags & GNUNET_DISK_OPEN_CREATE)
1345 { 1341 {
1346 (void) GNUNET_DISK_directory_create_for_file (expfn); 1342 (void) GNUNET_DISK_directory_create_for_file (expfn);
1347 oflags |= O_CREAT; 1343 oflags |= O_CREAT;
1348 mode = translate_unix_perms (perm); 1344 mode = translate_unix_perms (perm);
1349 } 1345 }
1350 1346
1351 fd = open (expfn, oflags | O_LARGEFILE, mode); 1347 fd = open (expfn, oflags | O_LARGEFILE, mode);
1352 if (fd == -1) 1348 if (fd == -1)
1353 { 1349 {
1354 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) 1350 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1355 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn); 1351 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1356 else 1352 else
1357 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn); 1353 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1358 GNUNET_free (expfn); 1354 GNUNET_free (expfn);
1359 return NULL; 1355 return NULL;
1360 } 1356 }
1361#else 1357#else
1362 access = 0; 1358 access = 0;
1363 disp = OPEN_ALWAYS; 1359 disp = OPEN_ALWAYS;
@@ -1370,48 +1366,47 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1370 access = FILE_WRITE_DATA; 1366 access = FILE_WRITE_DATA;
1371 1367
1372 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) 1368 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1373 { 1369 {
1374 disp = CREATE_NEW; 1370 disp = CREATE_NEW;
1375 } 1371 }
1376 else if (flags & GNUNET_DISK_OPEN_CREATE) 1372 else if (flags & GNUNET_DISK_OPEN_CREATE)
1377 { 1373 {
1378 (void) GNUNET_DISK_directory_create_for_file (expfn); 1374 (void) GNUNET_DISK_directory_create_for_file (expfn);
1379 if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1375 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1380 disp = CREATE_ALWAYS; 1376 disp = CREATE_ALWAYS;
1381 else 1377 else
1382 disp = OPEN_ALWAYS; 1378 disp = OPEN_ALWAYS;
1383 } 1379 }
1384 else if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1380 else if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1385 { 1381 {
1386 disp = TRUNCATE_EXISTING; 1382 disp = TRUNCATE_EXISTING;
1387 } 1383 }
1388 else 1384 else
1389 { 1385 {
1390 disp = OPEN_EXISTING; 1386 disp = OPEN_EXISTING;
1391 } 1387 }
1392 1388
1393 /* TODO: access priviledges? */ 1389 /* TODO: access priviledges? */
1394 h = CreateFile (expfn, access, 1390 h = CreateFile (expfn, access,
1395 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 1391 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1396 NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL); 1392 disp, FILE_ATTRIBUTE_NORMAL, NULL);
1397 if (h == INVALID_HANDLE_VALUE) 1393 if (h == INVALID_HANDLE_VALUE)
1394 {
1395 SetErrnoFromWinError (GetLastError ());
1396 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1397 GNUNET_free (expfn);
1398 return NULL;
1399 }
1400
1401 if (flags & GNUNET_DISK_OPEN_APPEND)
1402 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1398 { 1403 {
1399 SetErrnoFromWinError (GetLastError ()); 1404 SetErrnoFromWinError (GetLastError ());
1400 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn); 1405 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn);
1406 CloseHandle (h);
1401 GNUNET_free (expfn); 1407 GNUNET_free (expfn);
1402 return NULL; 1408 return NULL;
1403 } 1409 }
1404
1405 if (flags & GNUNET_DISK_OPEN_APPEND)
1406 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1407 {
1408 SetErrnoFromWinError (GetLastError ());
1409 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer",
1410 expfn);
1411 CloseHandle (h);
1412 GNUNET_free (expfn);
1413 return NULL;
1414 }
1415#endif 1410#endif
1416 1411
1417 ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle)); 1412 ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
@@ -1435,28 +1430,28 @@ int
1435GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) 1430GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1436{ 1431{
1437 if (h == NULL) 1432 if (h == NULL)
1438 { 1433 {
1439 errno = EINVAL; 1434 errno = EINVAL;
1440 return GNUNET_SYSERR; 1435 return GNUNET_SYSERR;
1441 } 1436 }
1442 1437
1443#if MINGW 1438#if MINGW
1444 if (!CloseHandle (h->h)) 1439 if (!CloseHandle (h->h))
1445 { 1440 {
1446 SetErrnoFromWinError (GetLastError ()); 1441 SetErrnoFromWinError (GetLastError ());
1447 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); 1442 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1448 GNUNET_free (h->oOverlapRead); 1443 GNUNET_free (h->oOverlapRead);
1449 GNUNET_free (h->oOverlapWrite); 1444 GNUNET_free (h->oOverlapWrite);
1450 GNUNET_free (h); 1445 GNUNET_free (h);
1451 return GNUNET_SYSERR; 1446 return GNUNET_SYSERR;
1452 } 1447 }
1453#else 1448#else
1454 if (close (h->fd) != 0) 1449 if (close (h->fd) != 0)
1455 { 1450 {
1456 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); 1451 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1457 GNUNET_free (h); 1452 GNUNET_free (h);
1458 return GNUNET_SYSERR; 1453 return GNUNET_SYSERR;
1459 } 1454 }
1460#endif 1455#endif
1461 GNUNET_free (h); 1456 GNUNET_free (h);
1462 return GNUNET_OK; 1457 return GNUNET_OK;
@@ -1479,7 +1474,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1479 */ 1474 */
1480char * 1475char *
1481GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, 1476GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1482 const char *serviceName, ...) 1477 const char *serviceName, ...)
1483{ 1478{
1484 const char *c; 1479 const char *c;
1485 char *pfx; 1480 char *pfx;
@@ -1488,45 +1483,44 @@ GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1488 unsigned int needed; 1483 unsigned int needed;
1489 1484
1490 if (GNUNET_OK != 1485 if (GNUNET_OK !=
1491 GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME", 1486 GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME", &pfx))
1492 &pfx))
1493 return NULL; 1487 return NULL;
1494 if (pfx == NULL) 1488 if (pfx == NULL)
1495 { 1489 {
1496 LOG (GNUNET_ERROR_TYPE_WARNING, 1490 LOG (GNUNET_ERROR_TYPE_WARNING,
1497 _("No `%s' specified for service `%s' in configuration.\n"), 1491 _("No `%s' specified for service `%s' in configuration.\n"), "HOME",
1498 "HOME", serviceName); 1492 serviceName);
1499 return NULL; 1493 return NULL;
1500 } 1494 }
1501 needed = strlen (pfx) + 2; 1495 needed = strlen (pfx) + 2;
1502 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\')) 1496 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1503 needed++; 1497 needed++;
1504 va_start (ap, serviceName); 1498 va_start (ap, serviceName);
1505 while (1) 1499 while (1)
1506 { 1500 {
1507 c = va_arg (ap, const char *); 1501 c = va_arg (ap, const char *);
1508 1502
1509 if (c == NULL) 1503 if (c == NULL)
1510 break; 1504 break;
1511 needed += strlen (c); 1505 needed += strlen (c);
1512 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) 1506 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1513 needed++; 1507 needed++;
1514 } 1508 }
1515 va_end (ap); 1509 va_end (ap);
1516 ret = GNUNET_malloc (needed); 1510 ret = GNUNET_malloc (needed);
1517 strcpy (ret, pfx); 1511 strcpy (ret, pfx);
1518 GNUNET_free (pfx); 1512 GNUNET_free (pfx);
1519 va_start (ap, serviceName); 1513 va_start (ap, serviceName);
1520 while (1) 1514 while (1)
1521 { 1515 {
1522 c = va_arg (ap, const char *); 1516 c = va_arg (ap, const char *);
1523 1517
1524 if (c == NULL) 1518 if (c == NULL)
1525 break; 1519 break;
1526 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) 1520 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1527 strcat (ret, DIR_SEPARATOR_STR); 1521 strcat (ret, DIR_SEPARATOR_STR);
1528 strcat (ret, c); 1522 strcat (ret, c);
1529 } 1523 }
1530 va_end (ap); 1524 va_end (ap);
1531 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\')) 1525 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1532 (void) GNUNET_DISK_directory_create_for_file (ret); 1526 (void) GNUNET_DISK_directory_create_for_file (ret);
@@ -1575,56 +1569,56 @@ struct GNUNET_DISK_MapHandle
1575 */ 1569 */
1576void * 1570void *
1577GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, 1571GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1578 struct GNUNET_DISK_MapHandle **m, 1572 struct GNUNET_DISK_MapHandle **m,
1579 enum GNUNET_DISK_MapType access, size_t len) 1573 enum GNUNET_DISK_MapType access, size_t len)
1580{ 1574{
1581 if (h == NULL) 1575 if (h == NULL)
1582 { 1576 {
1583 errno = EINVAL; 1577 errno = EINVAL;
1584 return NULL; 1578 return NULL;
1585 } 1579 }
1586 1580
1587#ifdef MINGW 1581#ifdef MINGW
1588 DWORD mapAccess, protect; 1582 DWORD mapAccess, protect;
1589 1583
1590 if ((access & GNUNET_DISK_MAP_TYPE_READ) && 1584 if ((access & GNUNET_DISK_MAP_TYPE_READ) &&
1591 (access & GNUNET_DISK_MAP_TYPE_WRITE)) 1585 (access & GNUNET_DISK_MAP_TYPE_WRITE))
1592 { 1586 {
1593 protect = PAGE_READWRITE; 1587 protect = PAGE_READWRITE;
1594 mapAccess = FILE_MAP_ALL_ACCESS; 1588 mapAccess = FILE_MAP_ALL_ACCESS;
1595 } 1589 }
1596 else if (access & GNUNET_DISK_MAP_TYPE_READ) 1590 else if (access & GNUNET_DISK_MAP_TYPE_READ)
1597 { 1591 {
1598 protect = PAGE_READONLY; 1592 protect = PAGE_READONLY;
1599 mapAccess = FILE_MAP_READ; 1593 mapAccess = FILE_MAP_READ;
1600 } 1594 }
1601 else if (access & GNUNET_DISK_MAP_TYPE_WRITE) 1595 else if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1602 { 1596 {
1603 protect = PAGE_READWRITE; 1597 protect = PAGE_READWRITE;
1604 mapAccess = FILE_MAP_WRITE; 1598 mapAccess = FILE_MAP_WRITE;
1605 } 1599 }
1606 else 1600 else
1607 { 1601 {
1608 GNUNET_break (0); 1602 GNUNET_break (0);
1609 return NULL; 1603 return NULL;
1610 } 1604 }
1611 1605
1612 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle)); 1606 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1613 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL); 1607 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1614 if ((*m)->h == INVALID_HANDLE_VALUE) 1608 if ((*m)->h == INVALID_HANDLE_VALUE)
1615 { 1609 {
1616 SetErrnoFromWinError (GetLastError ()); 1610 SetErrnoFromWinError (GetLastError ());
1617 GNUNET_free (*m); 1611 GNUNET_free (*m);
1618 return NULL; 1612 return NULL;
1619 } 1613 }
1620 1614
1621 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); 1615 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1622 if (!(*m)->addr) 1616 if (!(*m)->addr)
1623 { 1617 {
1624 SetErrnoFromWinError (GetLastError ()); 1618 SetErrnoFromWinError (GetLastError ());
1625 CloseHandle ((*m)->h); 1619 CloseHandle ((*m)->h);
1626 GNUNET_free (*m); 1620 GNUNET_free (*m);
1627 } 1621 }
1628 1622
1629 return (*m)->addr; 1623 return (*m)->addr;
1630#else 1624#else
@@ -1639,10 +1633,10 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1639 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0); 1633 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1640 GNUNET_assert (NULL != (*m)->addr); 1634 GNUNET_assert (NULL != (*m)->addr);
1641 if (MAP_FAILED == (*m)->addr) 1635 if (MAP_FAILED == (*m)->addr)
1642 { 1636 {
1643 GNUNET_free (*m); 1637 GNUNET_free (*m);
1644 return NULL; 1638 return NULL;
1645 } 1639 }
1646 (*m)->len = len; 1640 (*m)->len = len;
1647 return (*m)->addr; 1641 return (*m)->addr;
1648#endif 1642#endif
@@ -1659,20 +1653,20 @@ GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1659 int ret; 1653 int ret;
1660 1654
1661 if (h == NULL) 1655 if (h == NULL)
1662 { 1656 {
1663 errno = EINVAL; 1657 errno = EINVAL;
1664 return GNUNET_SYSERR; 1658 return GNUNET_SYSERR;
1665 } 1659 }
1666 1660
1667#ifdef MINGW 1661#ifdef MINGW
1668 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; 1662 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR;
1669 if (ret != GNUNET_OK) 1663 if (ret != GNUNET_OK)
1670 SetErrnoFromWinError (GetLastError ()); 1664 SetErrnoFromWinError (GetLastError ());
1671 if (!CloseHandle (h->h) && (ret == GNUNET_OK)) 1665 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1672 { 1666 {
1673 ret = GNUNET_SYSERR; 1667 ret = GNUNET_SYSERR;
1674 SetErrnoFromWinError (GetLastError ()); 1668 SetErrnoFromWinError (GetLastError ());
1675 } 1669 }
1676#else 1670#else
1677 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; 1671 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1678#endif 1672#endif
@@ -1690,10 +1684,10 @@ int
1690GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) 1684GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1691{ 1685{
1692 if (h == NULL) 1686 if (h == NULL)
1693 { 1687 {
1694 errno = EINVAL; 1688 errno = EINVAL;
1695 return GNUNET_SYSERR; 1689 return GNUNET_SYSERR;
1696 } 1690 }
1697 1691
1698#ifdef MINGW 1692#ifdef MINGW
1699 int ret; 1693 int ret;
@@ -1722,8 +1716,8 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1722 unlike CreatePipe, which returns a bool for success or failure. */ 1716 unlike CreatePipe, which returns a bool for success or failure. */
1723static int 1717static int
1724create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, 1718create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
1725 LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize, 1719 LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize,
1726 DWORD dwReadMode, DWORD dwWriteMode) 1720 DWORD dwReadMode, DWORD dwWriteMode)
1727{ 1721{
1728 /* Default to error. */ 1722 /* Default to error. */
1729 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; 1723 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE;
@@ -1740,103 +1734,103 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
1740 * Retrying will probably never be necessary, but we want 1734 * Retrying will probably never be necessary, but we want
1741 * to be as robust as possible. */ 1735 * to be as robust as possible. */
1742 while (1) 1736 while (1)
1743 { 1737 {
1744 static volatile LONG pipe_unique_id; 1738 static volatile LONG pipe_unique_id;
1745 1739
1746 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", 1740 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld",
1747 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); 1741 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id));
1748#if DEBUG_PIPE 1742#if DEBUG_PIPE
1749 LOG (GNUNET_ERROR_TYPE_DEBUG, 1743 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n",
1750 "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize); 1744 pipename, psize);
1751#endif 1745#endif
1752 /* Use CreateNamedPipe instead of CreatePipe, because the latter 1746 /* Use CreateNamedPipe instead of CreatePipe, because the latter
1753 * returns a write handle that does not permit FILE_READ_ATTRIBUTES 1747 * returns a write handle that does not permit FILE_READ_ATTRIBUTES
1754 * access, on versions of win32 earlier than WinXP SP2. 1748 * access, on versions of win32 earlier than WinXP SP2.
1755 * CreatePipe also stupidly creates a full duplex pipe, which is 1749 * CreatePipe also stupidly creates a full duplex pipe, which is
1756 * a waste, since only a single direction is actually used. 1750 * a waste, since only a single direction is actually used.
1757 * It's important to only allow a single instance, to ensure that 1751 * It's important to only allow a single instance, to ensure that
1758 * the pipe was not created earlier by some other process, even if 1752 * the pipe was not created earlier by some other process, even if
1759 * the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE 1753 * the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE
1760 * because that is only available for Win2k SP2 and WinXP. */ 1754 * because that is only available for Win2k SP2 and WinXP. */
1761 read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */ 1755 read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */
1762 psize, /* output buffer size */ 1756 psize, /* output buffer size */
1763 psize, /* input buffer size */ 1757 psize, /* input buffer size */
1764 NMPWAIT_USE_DEFAULT_WAIT, sa_ptr); 1758 NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
1765 1759
1766 if (read_pipe != INVALID_HANDLE_VALUE) 1760 if (read_pipe != INVALID_HANDLE_VALUE)
1767 { 1761 {
1768#if DEBUG_PIPE 1762#if DEBUG_PIPE
1769 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe); 1763 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe);
1770#endif 1764#endif
1771 break; 1765 break;
1772 } 1766 }
1773 1767
1774 DWORD err = GetLastError (); 1768 DWORD err = GetLastError ();
1775 1769
1776 switch (err) 1770 switch (err)
1777 { 1771 {
1778 case ERROR_PIPE_BUSY: 1772 case ERROR_PIPE_BUSY:
1779 /* The pipe is already open with compatible parameters. 1773 /* The pipe is already open with compatible parameters.
1780 * Pick a new name and retry. */ 1774 * Pick a new name and retry. */
1781#if DEBUG_PIPE 1775#if DEBUG_PIPE
1782 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); 1776 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n");
1783#endif 1777#endif
1784 continue; 1778 continue;
1785 case ERROR_ACCESS_DENIED: 1779 case ERROR_ACCESS_DENIED:
1786 /* The pipe is already open with incompatible parameters. 1780 /* The pipe is already open with incompatible parameters.
1787 * Pick a new name and retry. */ 1781 * Pick a new name and retry. */
1788#if DEBUG_PIPE 1782#if DEBUG_PIPE
1789 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); 1783 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n");
1790#endif 1784#endif
1791 continue; 1785 continue;
1792 case ERROR_CALL_NOT_IMPLEMENTED: 1786 case ERROR_CALL_NOT_IMPLEMENTED:
1793 /* We are on an older Win9x platform without named pipes. 1787 /* We are on an older Win9x platform without named pipes.
1794 * Return an anonymous pipe as the best approximation. */ 1788 * Return an anonymous pipe as the best approximation. */
1795#if DEBUG_PIPE 1789#if DEBUG_PIPE
1796 LOG (GNUNET_ERROR_TYPE_DEBUG, 1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1797 "CreateNamedPipe not implemented, resorting to " 1791 "CreateNamedPipe not implemented, resorting to "
1798 "CreatePipe: size = %lu\n", psize); 1792 "CreatePipe: size = %lu\n", psize);
1799#endif 1793#endif
1800 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) 1794 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize))
1801 { 1795 {
1802#if DEBUG_PIPE 1796#if DEBUG_PIPE
1803 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", 1797 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n",
1804 *read_pipe_ptr); 1798 *read_pipe_ptr);
1805 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", 1799 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n",
1806 *write_pipe_ptr); 1800 *write_pipe_ptr);
1807#endif 1801#endif
1808 return GNUNET_OK; 1802 return GNUNET_OK;
1809 } 1803 }
1810 err = GetLastError (); 1804 err = GetLastError ();
1811 LOG (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); 1805 LOG (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err);
1812 return err; 1806 return err;
1813 default: 1807 default:
1814 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); 1808 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err);
1815 return err; 1809 return err;
1816 }
1817 /* NOTREACHED */
1818 } 1810 }
1811 /* NOTREACHED */
1812 }
1819#if DEBUG_PIPE 1813#if DEBUG_PIPE
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); 1814 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename);
1821#endif 1815#endif
1822 1816
1823 /* Open the named pipe for writing. 1817 /* Open the named pipe for writing.
1824 * Be sure to permit FILE_READ_ATTRIBUTES access. */ 1818 * Be sure to permit FILE_READ_ATTRIBUTES access. */
1825 write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */ 1819 write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */
1826 sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */ 1820 sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */
1827 0); /* handle to template file */ 1821 0); /* handle to template file */
1828 1822
1829 if (write_pipe == INVALID_HANDLE_VALUE) 1823 if (write_pipe == INVALID_HANDLE_VALUE)
1830 { 1824 {
1831 /* Failure. */ 1825 /* Failure. */
1832 DWORD err = GetLastError (); 1826 DWORD err = GetLastError ();
1833 1827
1834#if DEBUG_PIPE 1828#if DEBUG_PIPE
1835 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); 1829 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err);
1836#endif 1830#endif
1837 CloseHandle (read_pipe); 1831 CloseHandle (read_pipe);
1838 return err; 1832 return err;
1839 } 1833 }
1840#if DEBUG_PIPE 1834#if DEBUG_PIPE
1841 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); 1835 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe);
1842#endif 1836#endif
@@ -1863,7 +1857,7 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1863 struct GNUNET_DISK_FileHandle *fds; 1857 struct GNUNET_DISK_FileHandle *fds;
1864 1858
1865 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + 1859 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) +
1866 2 * sizeof (struct GNUNET_DISK_FileHandle)); 1860 2 * sizeof (struct GNUNET_DISK_FileHandle));
1867 fds = (struct GNUNET_DISK_FileHandle *) &p[1]; 1861 fds = (struct GNUNET_DISK_FileHandle *) &p[1];
1868 p->fd[0] = &fds[0]; 1862 p->fd[0] = &fds[0];
1869 p->fd[1] = &fds[1]; 1863 p->fd[1] = &fds[1];
@@ -1875,13 +1869,13 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1875 1869
1876 ret = pipe (fd); 1870 ret = pipe (fd);
1877 if (ret == -1) 1871 if (ret == -1)
1878 { 1872 {
1879 eno = errno; 1873 eno = errno;
1880 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe"); 1874 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1881 GNUNET_free (p); 1875 GNUNET_free (p);
1882 errno = eno; 1876 errno = eno;
1883 return NULL; 1877 return NULL;
1884 } 1878 }
1885 p->fd[0]->fd = fd[0]; 1879 p->fd[0]->fd = fd[0];
1886 p->fd[1]->fd = fd[1]; 1880 p->fd[1]->fd = fd[1];
1887 ret = 0; 1881 ret = 0;
@@ -1905,62 +1899,62 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1905 if (0 > fcntl (fd[1], F_SETFD, flags)) 1899 if (0 > fcntl (fd[1], F_SETFD, flags))
1906 ret = -1; 1900 ret = -1;
1907 if (ret == -1) 1901 if (ret == -1)
1908 { 1902 {
1909 eno = errno; 1903 eno = errno;
1910 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl"); 1904 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl");
1911 GNUNET_break (0 == close (p->fd[0]->fd)); 1905 GNUNET_break (0 == close (p->fd[0]->fd));
1912 GNUNET_break (0 == close (p->fd[1]->fd)); 1906 GNUNET_break (0 == close (p->fd[1]->fd));
1913 GNUNET_free (p); 1907 GNUNET_free (p);
1914 errno = eno; 1908 errno = eno;
1915 return NULL; 1909 return NULL;
1916 } 1910 }
1917#else 1911#else
1918 BOOL ret; 1912 BOOL ret;
1919 HANDLE tmp_handle; 1913 HANDLE tmp_handle;
1920 1914
1921 ret = 1915 ret =
1922 create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, 1916 create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0,
1923 FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED); 1917 FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED);
1924 if (!ret) 1918 if (!ret)
1925 { 1919 {
1926 GNUNET_free (p); 1920 GNUNET_free (p);
1927 SetErrnoFromWinError (GetLastError ()); 1921 SetErrnoFromWinError (GetLastError ());
1928 return NULL; 1922 return NULL;
1929 } 1923 }
1930 if (!DuplicateHandle 1924 if (!DuplicateHandle
1931 (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 1925 (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 0,
1932 0, inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) 1926 inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
1933 { 1927 {
1934 SetErrnoFromWinError (GetLastError ()); 1928 SetErrnoFromWinError (GetLastError ());
1935 CloseHandle (p->fd[0]->h); 1929 CloseHandle (p->fd[0]->h);
1936 CloseHandle (p->fd[1]->h); 1930 CloseHandle (p->fd[1]->h);
1937 GNUNET_free (p); 1931 GNUNET_free (p);
1938 return NULL; 1932 return NULL;
1939 } 1933 }
1940 CloseHandle (p->fd[0]->h); 1934 CloseHandle (p->fd[0]->h);
1941 p->fd[0]->h = tmp_handle; 1935 p->fd[0]->h = tmp_handle;
1942 1936
1943 if (!DuplicateHandle 1937 if (!DuplicateHandle
1944 (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 1938 (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 0,
1945 0, inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) 1939 inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
1946 { 1940 {
1947 SetErrnoFromWinError (GetLastError ()); 1941 SetErrnoFromWinError (GetLastError ());
1948 CloseHandle (p->fd[0]->h); 1942 CloseHandle (p->fd[0]->h);
1949 CloseHandle (p->fd[1]->h); 1943 CloseHandle (p->fd[1]->h);
1950 GNUNET_free (p); 1944 GNUNET_free (p);
1951 return NULL; 1945 return NULL;
1952 } 1946 }
1953 CloseHandle (p->fd[1]->h); 1947 CloseHandle (p->fd[1]->h);
1954 p->fd[1]->h = tmp_handle; 1948 p->fd[1]->h = tmp_handle;
1955 if (!blocking) 1949 if (!blocking)
1956 { 1950 {
1957 DWORD mode; 1951 DWORD mode;
1958 1952
1959 mode = PIPE_NOWAIT; 1953 mode = PIPE_NOWAIT;
1960 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); 1954 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
1961 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); 1955 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
1962 /* this always fails on Windows 95, so we don't care about error handling */ 1956 /* this always fails on Windows 95, so we don't care about error handling */
1963 } 1957 }
1964 p->fd[0]->type = GNUNET_PIPE; 1958 p->fd[0]->type = GNUNET_PIPE;
1965 p->fd[1]->type = GNUNET_PIPE; 1959 p->fd[1]->type = GNUNET_PIPE;
1966 1960
@@ -1989,51 +1983,51 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
1989 */ 1983 */
1990int 1984int
1991GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, 1985GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
1992 enum GNUNET_DISK_PipeEnd end) 1986 enum GNUNET_DISK_PipeEnd end)
1993{ 1987{
1994 int ret = GNUNET_OK; 1988 int ret = GNUNET_OK;
1995 int save; 1989 int save;
1996 1990
1997#ifdef MINGW 1991#ifdef MINGW
1998 if (end == GNUNET_DISK_PIPE_END_READ) 1992 if (end == GNUNET_DISK_PIPE_END_READ)
1993 {
1994 if (!CloseHandle (p->fd[0]->h))
1999 { 1995 {
2000 if (!CloseHandle (p->fd[0]->h)) 1996 SetErrnoFromWinError (GetLastError ());
2001 { 1997 ret = GNUNET_SYSERR;
2002 SetErrnoFromWinError (GetLastError ());
2003 ret = GNUNET_SYSERR;
2004 }
2005 p->fd[0]->h = INVALID_HANDLE_VALUE;
2006 } 1998 }
1999 p->fd[0]->h = INVALID_HANDLE_VALUE;
2000 }
2007 else if (end == GNUNET_DISK_PIPE_END_WRITE) 2001 else if (end == GNUNET_DISK_PIPE_END_WRITE)
2002 {
2003 if (!CloseHandle (p->fd[1]->h))
2008 { 2004 {
2009 if (!CloseHandle (p->fd[1]->h)) 2005 SetErrnoFromWinError (GetLastError ());
2010 { 2006 ret = GNUNET_SYSERR;
2011 SetErrnoFromWinError (GetLastError ());
2012 ret = GNUNET_SYSERR;
2013 }
2014 p->fd[1]->h = INVALID_HANDLE_VALUE;
2015 } 2007 }
2008 p->fd[1]->h = INVALID_HANDLE_VALUE;
2009 }
2016 save = errno; 2010 save = errno;
2017#else 2011#else
2018 save = 0; 2012 save = 0;
2019 if (end == GNUNET_DISK_PIPE_END_READ) 2013 if (end == GNUNET_DISK_PIPE_END_READ)
2014 {
2015 if (0 != close (p->fd[0]->fd))
2020 { 2016 {
2021 if (0 != close (p->fd[0]->fd)) 2017 ret = GNUNET_SYSERR;
2022 { 2018 save = errno;
2023 ret = GNUNET_SYSERR;
2024 save = errno;
2025 }
2026 p->fd[0]->fd = -1;
2027 } 2019 }
2020 p->fd[0]->fd = -1;
2021 }
2028 else if (end == GNUNET_DISK_PIPE_END_WRITE) 2022 else if (end == GNUNET_DISK_PIPE_END_WRITE)
2023 {
2024 if (0 != close (p->fd[1]->fd))
2029 { 2025 {
2030 if (0 != close (p->fd[1]->fd)) 2026 ret = GNUNET_SYSERR;
2031 { 2027 save = errno;
2032 ret = GNUNET_SYSERR;
2033 save = errno;
2034 }
2035 p->fd[1]->fd = -1;
2036 } 2028 }
2029 p->fd[1]->fd = -1;
2030 }
2037#endif 2031#endif
2038 errno = save; 2032 errno = save;
2039 return ret; 2033 return ret;
@@ -2053,35 +2047,35 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
2053 2047
2054#ifdef MINGW 2048#ifdef MINGW
2055 if (!CloseHandle (p->fd[0]->h)) 2049 if (!CloseHandle (p->fd[0]->h))
2056 { 2050 {
2057 SetErrnoFromWinError (GetLastError ()); 2051 SetErrnoFromWinError (GetLastError ());
2058 ret = GNUNET_SYSERR; 2052 ret = GNUNET_SYSERR;
2059 } 2053 }
2060 if (!CloseHandle (p->fd[1]->h)) 2054 if (!CloseHandle (p->fd[1]->h))
2061 { 2055 {
2062 SetErrnoFromWinError (GetLastError ()); 2056 SetErrnoFromWinError (GetLastError ());
2063 ret = GNUNET_SYSERR; 2057 ret = GNUNET_SYSERR;
2064 } 2058 }
2065 save = errno; 2059 save = errno;
2066#else 2060#else
2067 save = 0; 2061 save = 0;
2068 if (p->fd[0]->fd != -1) 2062 if (p->fd[0]->fd != -1)
2063 {
2064 if (0 != close (p->fd[0]->fd))
2069 { 2065 {
2070 if (0 != close (p->fd[0]->fd)) 2066 ret = GNUNET_SYSERR;
2071 { 2067 save = errno;
2072 ret = GNUNET_SYSERR;
2073 save = errno;
2074 }
2075 } 2068 }
2069 }
2076 2070
2077 if (p->fd[1]->fd != -1) 2071 if (p->fd[1]->fd != -1)
2072 {
2073 if (0 != close (p->fd[1]->fd))
2078 { 2074 {
2079 if (0 != close (p->fd[1]->fd)) 2075 ret = GNUNET_SYSERR;
2080 { 2076 save = errno;
2081 ret = GNUNET_SYSERR;
2082 save = errno;
2083 }
2084 } 2077 }
2078 }
2085#endif 2079#endif
2086 GNUNET_free (p); 2080 GNUNET_free (p);
2087 errno = save; 2081 errno = save;
@@ -2098,7 +2092,7 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
2098 */ 2092 */
2099struct GNUNET_DISK_FileHandle * 2093struct GNUNET_DISK_FileHandle *
2100GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags, 2094GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2101 enum GNUNET_DISK_AccessPermissions perm) 2095 enum GNUNET_DISK_AccessPermissions perm)
2102{ 2096{
2103#ifdef MINGW 2097#ifdef MINGW
2104 struct GNUNET_DISK_FileHandle *ret; 2098 struct GNUNET_DISK_FileHandle *ret;
@@ -2118,71 +2112,70 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2118 openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE; 2112 openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE;
2119 2113
2120 while (h == NULL) 2114 while (h == NULL)
2121 { 2115 {
2122 DWORD error_code; 2116 DWORD error_code;
2123 2117
2124 name = NULL; 2118 name = NULL;
2125 if (*fn != NULL) 2119 if (*fn != NULL)
2126 { 2120 {
2127 GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn); 2121 GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn);
2128#if DEBUG_NPIPE 2122#if DEBUG_NPIPE
2129 LOG (GNUNET_ERROR_TYPE_DEBUG, 2123 LOG (GNUNET_ERROR_TYPE_DEBUG,
2130 "Trying to create an instance of named pipe `%s'\n", name); 2124 "Trying to create an instance of named pipe `%s'\n", name);
2131#endif 2125#endif
2132 h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED, 2126 h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED,
2133 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 2127 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0,
2134 0, NULL); 2128 NULL);
2135 } 2129 }
2136 else 2130 else
2137 { 2131 {
2138 GNUNET_asprintf (fn, "\\\\.\\pipe\\gnunet-%llu", 2132 GNUNET_asprintf (fn, "\\\\.\\pipe\\gnunet-%llu",
2139 GNUNET_CRYPTO_random_u64 2133 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2140 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX)); 2134 UINT64_MAX));
2141#if DEBUG_NPIPE 2135#if DEBUG_NPIPE
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, 2136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying to create unique named pipe `%s'\n",
2143 "Trying to create unique named pipe `%s'\n", *fn); 2137 *fn);
2144#endif 2138#endif
2145 h = CreateNamedPipe (*fn, 2139 h = CreateNamedPipe (*fn,
2146 openMode | FILE_FLAG_OVERLAPPED | 2140 openMode | FILE_FLAG_OVERLAPPED |
2147 FILE_FLAG_FIRST_PIPE_INSTANCE, 2141 FILE_FLAG_FIRST_PIPE_INSTANCE,
2148 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 2142 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0,
2149 0, NULL); 2143 NULL);
2150 } 2144 }
2151 error_code = GetLastError (); 2145 error_code = GetLastError ();
2152 if (name) 2146 if (name)
2153 GNUNET_free (name); 2147 GNUNET_free (name);
2154 /* don't re-set name to NULL yet */ 2148 /* don't re-set name to NULL yet */
2155 if (h == INVALID_HANDLE_VALUE) 2149 if (h == INVALID_HANDLE_VALUE)
2156 { 2150 {
2157 SetErrnoFromWinError (error_code); 2151 SetErrnoFromWinError (error_code);
2158#if DEBUG_NPIPE 2152#if DEBUG_NPIPE
2159 LOG (GNUNET_ERROR_TYPE_DEBUG, 2153 LOG (GNUNET_ERROR_TYPE_DEBUG,
2160 "Pipe creation have failed because of %d, errno is %d\n", 2154 "Pipe creation have failed because of %d, errno is %d\n", error_code,
2161 error_code, errno); 2155 errno);
2162#endif 2156#endif
2163 if (name == NULL) 2157 if (name == NULL)
2164 { 2158 {
2165#if DEBUG_NPIPE 2159#if DEBUG_NPIPE
2166 LOG (GNUNET_ERROR_TYPE_DEBUG, 2160 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "Pipe was to be unique, considering re-creation\n"); 2161 "Pipe was to be unique, considering re-creation\n");
2168#endif 2162#endif
2169 GNUNET_free (*fn); 2163 GNUNET_free (*fn);
2170 *fn = NULL; 2164 *fn = NULL;
2171 if (error_code != ERROR_ACCESS_DENIED 2165 if (error_code != ERROR_ACCESS_DENIED && error_code != ERROR_PIPE_BUSY)
2172 && error_code != ERROR_PIPE_BUSY) 2166 {
2173 { 2167 return NULL;
2174 return NULL; 2168 }
2175 }
2176#if DEBUG_NPIPE 2169#if DEBUG_NPIPE
2177 LOG (GNUNET_ERROR_TYPE_DEBUG, 2170 LOG (GNUNET_ERROR_TYPE_DEBUG,
2178 "Pipe name was not unique, trying again\n"); 2171 "Pipe name was not unique, trying again\n");
2179#endif 2172#endif
2180 h = NULL; 2173 h = NULL;
2181 } 2174 }
2182 else 2175 else
2183 return NULL; 2176 return NULL;
2184 }
2185 } 2177 }
2178 }
2186 errno = 0; 2179 errno = 0;
2187 2180
2188 ret = GNUNET_malloc (sizeof (*ret)); 2181 ret = GNUNET_malloc (sizeof (*ret));
@@ -2198,22 +2191,22 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2198 return ret; 2191 return ret;
2199#else 2192#else
2200 if (*fn == NULL) 2193 if (*fn == NULL)
2194 {
2195 char dir[] = "/tmp/gnunet-pipe-XXXXXX";
2196
2197 if (mkdtemp (dir) == NULL)
2201 { 2198 {
2202 char dir[] = "/tmp/gnunet-pipe-XXXXXX"; 2199 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "mkdtemp");
2203 2200 return NULL;
2204 if (mkdtemp (dir) == NULL)
2205 {
2206 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "mkdtemp");
2207 return NULL;
2208 }
2209 GNUNET_asprintf (fn, "%s/child-control", dir);
2210 } 2201 }
2202 GNUNET_asprintf (fn, "%s/child-control", dir);
2203 }
2211 2204
2212 if (mkfifo (*fn, translate_unix_perms (perm)) == -1) 2205 if (mkfifo (*fn, translate_unix_perms (perm)) == -1)
2213 { 2206 {
2214 if ((errno != EEXIST) || (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))) 2207 if ((errno != EEXIST) || (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)))
2215 return NULL; 2208 return NULL;
2216 } 2209 }
2217 2210
2218 flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS); 2211 flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS);
2219 return GNUNET_DISK_file_open (*fn, flags, perm); 2212 return GNUNET_DISK_file_open (*fn, flags, perm);
@@ -2231,7 +2224,7 @@ GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
2231 */ 2224 */
2232struct GNUNET_DISK_FileHandle * 2225struct GNUNET_DISK_FileHandle *
2233GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, 2226GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
2234 enum GNUNET_DISK_AccessPermissions perm) 2227 enum GNUNET_DISK_AccessPermissions perm)
2235{ 2228{
2236#ifdef MINGW 2229#ifdef MINGW
2237 struct GNUNET_DISK_FileHandle *ret; 2230 struct GNUNET_DISK_FileHandle *ret;
@@ -2247,12 +2240,12 @@ GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
2247 openMode = GENERIC_WRITE; 2240 openMode = GENERIC_WRITE;
2248 2241
2249 h = CreateFile (fn, openMode, 0, NULL, OPEN_EXISTING, 2242 h = CreateFile (fn, openMode, 0, NULL, OPEN_EXISTING,
2250 FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL); 2243 FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL);
2251 if (h == INVALID_HANDLE_VALUE) 2244 if (h == INVALID_HANDLE_VALUE)
2252 { 2245 {
2253 SetErrnoFromWinError (GetLastError ()); 2246 SetErrnoFromWinError (GetLastError ());
2254 return NULL; 2247 return NULL;
2255 } 2248 }
2256 2249
2257 ret = GNUNET_malloc (sizeof (*ret)); 2250 ret = GNUNET_malloc (sizeof (*ret));
2258 ret->h = h; 2251 ret->h = h;
@@ -2284,10 +2277,10 @@ GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe)
2284 2277
2285 ret = CloseHandle (pipe->h); 2278 ret = CloseHandle (pipe->h);
2286 if (!ret) 2279 if (!ret)
2287 { 2280 {
2288 SetErrnoFromWinError (GetLastError ()); 2281 SetErrnoFromWinError (GetLastError ());
2289 return GNUNET_SYSERR; 2282 return GNUNET_SYSERR;
2290 } 2283 }
2291 else 2284 else
2292 return GNUNET_OK; 2285 return GNUNET_OK;
2293#endif 2286#endif
@@ -2303,17 +2296,17 @@ GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe)
2303 */ 2296 */
2304const struct GNUNET_DISK_FileHandle * 2297const struct GNUNET_DISK_FileHandle *
2305GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, 2298GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
2306 enum GNUNET_DISK_PipeEnd n) 2299 enum GNUNET_DISK_PipeEnd n)
2307{ 2300{
2308 switch (n) 2301 switch (n)
2309 { 2302 {
2310 case GNUNET_DISK_PIPE_END_READ: 2303 case GNUNET_DISK_PIPE_END_READ:
2311 case GNUNET_DISK_PIPE_END_WRITE: 2304 case GNUNET_DISK_PIPE_END_WRITE:
2312 return p->fd[n]; 2305 return p->fd[n];
2313 default: 2306 default:
2314 GNUNET_break (0); 2307 GNUNET_break (0);
2315 return NULL; 2308 return NULL;
2316 } 2309 }
2317} 2310}
2318 2311
2319 2312
@@ -2327,7 +2320,7 @@ GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
2327 */ 2320 */
2328int 2321int
2329GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, 2322GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
2330 void *dst, size_t dst_len) 2323 void *dst, size_t dst_len)
2331{ 2324{
2332#ifdef MINGW 2325#ifdef MINGW
2333 if (dst_len < sizeof (HANDLE)) 2326 if (dst_len < sizeof (HANDLE))
diff --git a/src/util/getopt.c b/src/util/getopt.c
index 4a36678b2..317bc0748 100644
--- a/src/util/getopt.c
+++ b/src/util/getopt.c
@@ -198,19 +198,20 @@ static char *posixly_correct;
198/* Avoid depending on library functions or files 198/* Avoid depending on library functions or files
199 whose names are inconsistent. */ 199 whose names are inconsistent. */
200 200
201char *getenv (); 201char *
202getenv ();
202 203
203static char * 204static char *
204my_index (str, chr) 205my_index (str, chr)
205 const char *str; 206 const char *str;
206 int chr; 207 int chr;
207{ 208{
208 while (*str) 209 while (*str)
209 { 210 {
210 if (*str == chr) 211 if (*str == chr)
211 return (char *) str; 212 return (char *) str;
212 str++; 213 str++;
213 } 214 }
214 return 0; 215 return 0;
215} 216}
216 217
@@ -222,7 +223,8 @@ my_index (str, chr)
222#if !defined (__STDC__) || !__STDC__ 223#if !defined (__STDC__) || !__STDC__
223/* gcc with -traditional declares the built-in strlen to return int, 224/* gcc with -traditional declares the built-in strlen to return int,
224 and has done so at least since version 2.4.5. -- rms. */ 225 and has done so at least since version 2.4.5. -- rms. */
225extern int strlen (const char *); 226extern int
227strlen (const char *);
226#endif /* not __STDC__ */ 228#endif /* not __STDC__ */
227#endif /* __GNUC__ */ 229#endif /* __GNUC__ */
228 230
@@ -256,7 +258,7 @@ extern pid_t __libc_pid;
256 is valid for the getopt call we must make sure that the ARGV passed 258 is valid for the getopt call we must make sure that the ARGV passed
257 to getopt is that one passed to the process. */ 259 to getopt is that one passed to the process. */
258static void 260static void
259 __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv) 261 __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv)
260{ 262{
261 /* XXX This is no good solution. We should rather copy the args so 263 /* XXX This is no good solution. We should rather copy the args so
262 * that we can compare them later. But we must not use malloc(3). */ 264 * that we can compare them later. But we must not use malloc(3). */
@@ -287,12 +289,13 @@ text_set_element (__libc_subinit, store_args_and_env);
287 the new indices of the non-options in ARGV after they are moved. */ 289 the new indices of the non-options in ARGV after they are moved. */
288 290
289#if defined (__STDC__) && __STDC__ 291#if defined (__STDC__) && __STDC__
290static void exchange (char **); 292static void
293exchange (char **);
291#endif 294#endif
292 295
293static void 296static void
294exchange (argv) 297exchange (argv)
295 char **argv; 298 char **argv;
296{ 299{
297 int bottom = first_nonopt; 300 int bottom = first_nonopt;
298 int middle = last_nonopt; 301 int middle = last_nonopt;
@@ -309,61 +312,61 @@ exchange (argv)
309 * string can work normally. Our top argument must be in the range 312 * string can work normally. Our top argument must be in the range
310 * of the string. */ 313 * of the string. */
311 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 314 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
312 { 315 {
313 /* We must extend the array. The user plays games with us and 316 /* We must extend the array. The user plays games with us and
314 * presents new arguments. */ 317 * presents new arguments. */
315 char *new_str = malloc (top + 1); 318 char *new_str = malloc (top + 1);
316 319
317 if (new_str == NULL) 320 if (new_str == NULL)
318 nonoption_flags_len = nonoption_flags_max_len = 0; 321 nonoption_flags_len = nonoption_flags_max_len = 0;
319 else 322 else
320 { 323 {
321 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); 324 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
322 memset (&new_str[nonoption_flags_max_len], '\0', 325 memset (&new_str[nonoption_flags_max_len], '\0',
323 top + 1 - nonoption_flags_max_len); 326 top + 1 - nonoption_flags_max_len);
324 nonoption_flags_max_len = top + 1; 327 nonoption_flags_max_len = top + 1;
325 __getopt_nonoption_flags = new_str; 328 __getopt_nonoption_flags = new_str;
326 }
327 } 329 }
330 }
328#endif 331#endif
329 332
330 while (top > middle && middle > bottom) 333 while (top > middle && middle > bottom)
334 {
335 if (top - middle > middle - bottom)
331 { 336 {
332 if (top - middle > middle - bottom) 337 /* Bottom segment is the short one. */
333 { 338 int len = middle - bottom;
334 /* Bottom segment is the short one. */ 339 register int i;
335 int len = middle - bottom; 340
336 register int i; 341 /* Swap it with the top part of the top segment. */
337 342 for (i = 0; i < len; i++)
338 /* Swap it with the top part of the top segment. */ 343 {
339 for (i = 0; i < len; i++) 344 tem = argv[bottom + i];
340 { 345 argv[bottom + i] = argv[top - (middle - bottom) + i];
341 tem = argv[bottom + i]; 346 argv[top - (middle - bottom) + i] = tem;
342 argv[bottom + i] = argv[top - (middle - bottom) + i]; 347 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
343 argv[top - (middle - bottom) + i] = tem; 348 }
344 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); 349 /* Exclude the moved bottom segment from further swapping. */
345 } 350 top -= len;
346 /* Exclude the moved bottom segment from further swapping. */ 351 }
347 top -= len; 352 else
348 } 353 {
349 else 354 /* Top segment is the short one. */
350 { 355 int len = top - middle;
351 /* Top segment is the short one. */ 356 register int i;
352 int len = top - middle; 357
353 register int i; 358 /* Swap it with the bottom part of the bottom segment. */
354 359 for (i = 0; i < len; i++)
355 /* Swap it with the bottom part of the bottom segment. */ 360 {
356 for (i = 0; i < len; i++) 361 tem = argv[bottom + i];
357 { 362 argv[bottom + i] = argv[middle + i];
358 tem = argv[bottom + i]; 363 argv[middle + i] = tem;
359 argv[bottom + i] = argv[middle + i]; 364 SWAP_FLAGS (bottom + i, middle + i);
360 argv[middle + i] = tem; 365 }
361 SWAP_FLAGS (bottom + i, middle + i); 366 /* Exclude the moved top segment from further swapping. */
362 } 367 bottom += len;
363 /* Exclude the moved top segment from further swapping. */
364 bottom += len;
365 }
366 } 368 }
369 }
367 370
368 /* Update records for the slots the non-options now occupy. */ 371 /* Update records for the slots the non-options now occupy. */
369 372
@@ -374,13 +377,14 @@ exchange (argv)
374/* Initialize the internal data when the first call is made. */ 377/* Initialize the internal data when the first call is made. */
375 378
376#if defined (__STDC__) && __STDC__ 379#if defined (__STDC__) && __STDC__
377static const char *_getopt_initialize (int, char *const *, const char *); 380static const char *
381_getopt_initialize (int, char *const *, const char *);
378#endif 382#endif
379static const char * 383static const char *
380_getopt_initialize (argc, argv, optstring) 384_getopt_initialize (argc, argv, optstring)
381 int argc; 385 int argc;
382 char *const *argv; 386 char *const *argv;
383 const char *optstring; 387 const char *optstring;
384{ 388{
385 /* Start processing options with ARGV-element 1 (since ARGV-element 0 389 /* Start processing options with ARGV-element 1 (since ARGV-element 0
386 * is the program name); the sequence of previously skipped 390 * is the program name); the sequence of previously skipped
@@ -395,50 +399,48 @@ _getopt_initialize (argc, argv, optstring)
395 /* Determine how to handle the ordering of options and nonoptions. */ 399 /* Determine how to handle the ordering of options and nonoptions. */
396 400
397 if (optstring[0] == '-') 401 if (optstring[0] == '-')
398 { 402 {
399 ordering = RETURN_IN_ORDER; 403 ordering = RETURN_IN_ORDER;
400 ++optstring; 404 ++optstring;
401 } 405 }
402 else if (optstring[0] == '+') 406 else if (optstring[0] == '+')
403 { 407 {
404 ordering = REQUIRE_ORDER; 408 ordering = REQUIRE_ORDER;
405 ++optstring; 409 ++optstring;
406 } 410 }
407 else if (posixly_correct != NULL) 411 else if (posixly_correct != NULL)
408 ordering = REQUIRE_ORDER; 412 ordering = REQUIRE_ORDER;
409 else 413 else
410 ordering = PERMUTE; 414 ordering = PERMUTE;
411 415
412#ifdef _LIBC 416#ifdef _LIBC
413 if (posixly_correct == NULL && argc == original_argc 417 if (posixly_correct == NULL && argc == original_argc && argv == original_argv)
414 && argv == original_argv) 418 {
419 if (nonoption_flags_max_len == 0)
415 { 420 {
416 if (nonoption_flags_max_len == 0) 421 if (__getopt_nonoption_flags == NULL ||
417 { 422 __getopt_nonoption_flags[0] == '\0')
418 if (__getopt_nonoption_flags == NULL || 423 nonoption_flags_max_len = -1;
419 __getopt_nonoption_flags[0] == '\0') 424 else
420 nonoption_flags_max_len = -1; 425 {
421 else 426 const char *orig_str = __getopt_nonoption_flags;
422 { 427 int len = nonoption_flags_max_len = strlen (orig_str);
423 const char *orig_str = __getopt_nonoption_flags; 428
424 int len = nonoption_flags_max_len = strlen (orig_str); 429 if (nonoption_flags_max_len < argc)
425 430 nonoption_flags_max_len = argc;
426 if (nonoption_flags_max_len < argc) 431 __getopt_nonoption_flags = (char *) malloc (nonoption_flags_max_len);
427 nonoption_flags_max_len = argc; 432 if (__getopt_nonoption_flags == NULL)
428 __getopt_nonoption_flags = 433 nonoption_flags_max_len = -1;
429 (char *) malloc (nonoption_flags_max_len); 434 else
430 if (__getopt_nonoption_flags == NULL) 435 {
431 nonoption_flags_max_len = -1; 436 memcpy (__getopt_nonoption_flags, orig_str, len);
432 else 437 memset (&__getopt_nonoption_flags[len], '\0',
433 { 438 nonoption_flags_max_len - len);
434 memcpy (__getopt_nonoption_flags, orig_str, len); 439 }
435 memset (&__getopt_nonoption_flags[len], '\0', 440 }
436 nonoption_flags_max_len - len);
437 }
438 }
439 }
440 nonoption_flags_len = nonoption_flags_max_len;
441 } 441 }
442 nonoption_flags_len = nonoption_flags_max_len;
443 }
442 else 444 else
443 nonoption_flags_len = 0; 445 nonoption_flags_len = 0;
444#endif 446#endif
@@ -504,8 +506,8 @@ _getopt_initialize (argc, argv, optstring)
504 506
505static int 507static int
506GN_getopt_internal (int argc, char *const *argv, const char *optstring, 508GN_getopt_internal (int argc, char *const *argv, const char *optstring,
507 const struct GNoption *longopts, int *longind, 509 const struct GNoption *longopts, int *longind,
508 int long_only) 510 int long_only)
509{ 511{
510 static int __getopt_initialized = 0; 512 static int __getopt_initialized = 0;
511 static int GNopterr = 1; 513 static int GNopterr = 1;
@@ -513,12 +515,12 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
513 GNoptarg = NULL; 515 GNoptarg = NULL;
514 516
515 if (GNoptind == 0 || !__getopt_initialized) 517 if (GNoptind == 0 || !__getopt_initialized)
516 { 518 {
517 if (GNoptind == 0) 519 if (GNoptind == 0)
518 GNoptind = 1; /* Don't scan ARGV[0], the program name. */ 520 GNoptind = 1; /* Don't scan ARGV[0], the program name. */
519 optstring = _getopt_initialize (argc, argv, optstring); 521 optstring = _getopt_initialize (argc, argv, optstring);
520 __getopt_initialized = 1; 522 __getopt_initialized = 1;
521 } 523 }
522 524
523 /* Test whether ARGV[GNoptind] points to a non-option argument. 525 /* Test whether ARGV[GNoptind] points to a non-option argument.
524 * Either it does not have option syntax, or there is an environment flag 526 * Either it does not have option syntax, or there is an environment flag
@@ -533,81 +535,81 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
533#endif 535#endif
534 536
535 if (nextchar == NULL || *nextchar == '\0') 537 if (nextchar == NULL || *nextchar == '\0')
538 {
539 /* Advance to the next ARGV-element. */
540
541 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been
542 * moved back by the user (who may also have changed the arguments). */
543 if (last_nonopt > GNoptind)
544 last_nonopt = GNoptind;
545 if (first_nonopt > GNoptind)
546 first_nonopt = GNoptind;
547
548 if (ordering == PERMUTE)
536 { 549 {
537 /* Advance to the next ARGV-element. */ 550 /* If we have just processed some options following some non-options,
538 551 * exchange them so that the options come first. */
539 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been 552
540 * moved back by the user (who may also have changed the arguments). */ 553 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
541 if (last_nonopt > GNoptind) 554 exchange ((char **) argv);
542 last_nonopt = GNoptind; 555 else if (last_nonopt != GNoptind)
543 if (first_nonopt > GNoptind) 556 first_nonopt = GNoptind;
544 first_nonopt = GNoptind; 557
545 558 /* Skip any additional non-options
546 if (ordering == PERMUTE) 559 * and extend the range of non-options previously skipped. */
547 { 560
548 /* If we have just processed some options following some non-options, 561 while (GNoptind < argc && NONOPTION_P)
549 * exchange them so that the options come first. */ 562 GNoptind++;
550 563 last_nonopt = GNoptind;
551 if (first_nonopt != last_nonopt && last_nonopt != GNoptind) 564 }
552 exchange ((char **) argv); 565
553 else if (last_nonopt != GNoptind) 566 /* The special ARGV-element `--' means premature end of options.
554 first_nonopt = GNoptind; 567 * Skip it like a null option,
555 568 * then exchange with previous non-options as if it were an option,
556 /* Skip any additional non-options 569 * then skip everything else like a non-option. */
557 * and extend the range of non-options previously skipped. */ 570 if (GNoptind != argc && !strcmp (argv[GNoptind], "--"))
558 571 {
559 while (GNoptind < argc && NONOPTION_P) 572 GNoptind++;
560 GNoptind++; 573
561 last_nonopt = GNoptind; 574 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
562 } 575 exchange ((char **) argv);
563 576 else if (first_nonopt == last_nonopt)
564 /* The special ARGV-element `--' means premature end of options. 577 first_nonopt = GNoptind;
565 * Skip it like a null option, 578 last_nonopt = argc;
566 * then exchange with previous non-options as if it were an option, 579
567 * then skip everything else like a non-option. */ 580 GNoptind = argc;
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] == '-'));
609 } 581 }
610 582
583 /* If we have done all the ARGV-elements, stop the scan
584 * and back over any non-options that we skipped and permuted. */
585
586 if (GNoptind == argc)
587 {
588 /* Set the next-arg-index to point at the non-options
589 * that we previously skipped, so the caller will digest them. */
590 if (first_nonopt != last_nonopt)
591 GNoptind = first_nonopt;
592 return -1;
593 }
594
595 /* If we have come to a non-option and did not permute it,
596 * either stop the scan or describe it to the caller and pass it by. */
597
598 if (NONOPTION_P)
599 {
600 if (ordering == REQUIRE_ORDER)
601 return -1;
602 GNoptarg = argv[GNoptind++];
603 return 1;
604 }
605
606 /* We have found another option-ARGV-element.
607 * Skip the initial punctuation. */
608
609 nextchar =
610 (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-'));
611 }
612
611 /* Decode the current option-ARGV-element. */ 613 /* Decode the current option-ARGV-element. */
612 614
613 /* Check whether the ARGV-element is a long option. 615 /* Check whether the ARGV-element is a long option.
@@ -626,138 +628,134 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
626 if (longopts != NULL && 628 if (longopts != NULL &&
627 (argv[GNoptind][1] == '-' || 629 (argv[GNoptind][1] == '-' ||
628 (long_only && 630 (long_only &&
629 (argv[GNoptind][2] || !my_index (optstring, argv[GNoptind][1]))))) 631 (argv[GNoptind][2] || !my_index (optstring, argv[GNoptind][1])))))
630 { 632 {
631 char *nameend; 633 char *nameend;
632 const struct GNoption *p; 634 const struct GNoption *p;
633 const struct GNoption *pfound = NULL; 635 const struct GNoption *pfound = NULL;
634 int exact = 0; 636 int exact = 0;
635 int ambig = 0; 637 int ambig = 0;
636 int indfound = -1; 638 int indfound = -1;
637 int option_index; 639 int option_index;
638 640
639 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 641 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
640 /* Do nothing. */ ; 642 /* Do nothing. */ ;
643
644 /* Test all long options for either exact match
645 * or abbreviated matches. */
646 for (p = longopts, option_index = 0; p->name; p++, option_index++)
647 if (!strncmp (p->name, nextchar, nameend - nextchar))
648 {
649 if ((unsigned int) (nameend - nextchar) ==
650 (unsigned int) strlen (p->name))
651 {
652 /* Exact match found. */
653 pfound = p;
654 indfound = option_index;
655 exact = 1;
656 break;
657 }
658 else if (pfound == NULL)
659 {
660 /* First nonexact match found. */
661 pfound = p;
662 indfound = option_index;
663 }
664 else
665 /* Second or later nonexact match found. */
666 ambig = 1;
667 }
641 668
642 /* Test all long options for either exact match 669 if (ambig && !exact)
643 * or abbreviated matches. */ 670 {
644 for (p = longopts, option_index = 0; p->name; p++, option_index++) 671 if (GNopterr)
645 if (!strncmp (p->name, nextchar, nameend - nextchar)) 672 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), argv[0],
646 { 673 argv[GNoptind]);
647 if ((unsigned int) (nameend - nextchar) == 674 nextchar += strlen (nextchar);
648 (unsigned int) strlen (p->name)) 675 GNoptind++;
649 { 676 return '?';
650 /* Exact match found. */ 677 }
651 pfound = p;
652 indfound = option_index;
653 exact = 1;
654 break;
655 }
656 else if (pfound == NULL)
657 {
658 /* First nonexact match found. */
659 pfound = p;
660 indfound = option_index;
661 }
662 else
663 /* Second or later nonexact match found. */
664 ambig = 1;
665 }
666 678
667 if (ambig && !exact) 679 if (pfound != NULL)
668 { 680 {
669 if (GNopterr) 681 option_index = indfound;
670 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), argv[0], 682 GNoptind++;
671 argv[GNoptind]); 683 if (*nameend)
672 nextchar += strlen (nextchar); 684 {
673 GNoptind++; 685 /* Don't test has_arg with >, because some C compilers don't
674 return '?'; 686 * allow it to be used on enums. */
675 } 687 if (pfound->has_arg)
688 GNoptarg = nameend + 1;
689 else
690 {
691 if (GNopterr)
692 {
693 if (argv[GNoptind - 1][1] == '-')
694 /* --option */
695 fprintf (stderr,
696 _("%s: option `--%s' does not allow an argument\n"),
697 argv[0], pfound->name);
698 else
699 /* +option or -option */
700 fprintf (stderr,
701 _("%s: option `%c%s' does not allow an argument\n"),
702 argv[0], argv[GNoptind - 1][0], pfound->name);
703 }
704 nextchar += strlen (nextchar);
705 return '?';
706 }
707 }
708 else if (pfound->has_arg == 1)
709 {
710 if (GNoptind < argc)
711 {
712 GNoptarg = argv[GNoptind++];
713 }
714 else
715 {
716 if (GNopterr)
717 {
718 fprintf (stderr, _("%s: option `%s' requires an argument\n"),
719 argv[0], argv[GNoptind - 1]);
720 }
721 nextchar += strlen (nextchar);
722 return (optstring[0] == ':') ? ':' : '?';
723 }
724 }
725 nextchar += strlen (nextchar);
726 if (longind != NULL)
727 *longind = option_index;
728 if (pfound->flag)
729 {
730 *(pfound->flag) = pfound->val;
731 return 0;
732 }
733 return pfound->val;
734 }
676 735
677 if (pfound != NULL) 736 /* Can't find it as a long option. If this is not getopt_long_only,
678 { 737 * or the option starts with '--' or is not a valid short
679 option_index = indfound; 738 * option, then it's an error.
680 GNoptind++; 739 * Otherwise interpret it as a short option. */
681 if (*nameend) 740 if (!long_only || argv[GNoptind][1] == '-' ||
682 { 741 my_index (optstring, *nextchar) == NULL)
683 /* Don't test has_arg with >, because some C compilers don't 742 {
684 * allow it to be used on enums. */ 743 if (GNopterr)
685 if (pfound->has_arg) 744 {
686 GNoptarg = nameend + 1; 745 if (argv[GNoptind][1] == '-')
687 else 746 /* --option */
688 { 747 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), argv[0],
689 if (GNopterr) 748 nextchar);
690 { 749 else
691 if (argv[GNoptind - 1][1] == '-') 750 /* +option or -option */
692 /* --option */ 751 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), argv[0],
693 fprintf (stderr, 752 argv[GNoptind][0], nextchar);
694 _ 753 }
695 ("%s: option `--%s' does not allow an argument\n"), 754 nextchar = (char *) "";
696 argv[0], pfound->name); 755 GNoptind++;
697 else 756 return '?';
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 }
760 } 757 }
758 }
761 759
762 /* Look at and handle the next short option-character. */ 760 /* Look at and handle the next short option-character. */
763 761
@@ -770,193 +768,191 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
770 ++GNoptind; 768 ++GNoptind;
771 769
772 if (temp == NULL || c == ':') 770 if (temp == NULL || c == ':')
771 {
772 if (GNopterr)
773 { 773 {
774 if (GNopterr) 774 if (posixly_correct)
775 { 775 /* 1003.2 specifies the format of this message. */
776 if (posixly_correct) 776 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
777 /* 1003.2 specifies the format of this message. */ 777 else
778 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); 778 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
779 else
780 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
781 }
782 return '?';
783 } 779 }
780 return '?';
781 }
784 /* Convenience. Treat POSIX -W foo same as long option --foo */ 782 /* Convenience. Treat POSIX -W foo same as long option --foo */
785 if (temp[0] == 'W' && temp[1] == ';') 783 if (temp[0] == 'W' && temp[1] == ';')
784 {
785 char *nameend;
786 const struct GNoption *p;
787 const struct GNoption *pfound = NULL;
788 int exact = 0;
789 int ambig = 0;
790 int indfound = 0;
791 int option_index;
792
793 /* This is an option that requires an argument. */
794 if (*nextchar != '\0')
786 { 795 {
787 char *nameend; 796 GNoptarg = nextchar;
788 const struct GNoption *p; 797 /* If we end this ARGV-element by taking the rest as an arg,
789 const struct GNoption *pfound = NULL; 798 * we must advance to the next element now. */
790 int exact = 0; 799 GNoptind++;
791 int ambig = 0; 800 }
792 int indfound = 0; 801 else if (GNoptind == argc)
793 int option_index; 802 {
794 803 if (GNopterr)
795 /* This is an option that requires an argument. */ 804 {
796 if (*nextchar != '\0') 805 /* 1003.2 specifies the format of this message. */
797 { 806 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
798 GNoptarg = nextchar; 807 argv[0], c);
799 /* If we end this ARGV-element by taking the rest as an arg, 808 }
800 * we must advance to the next element now. */ 809 if (optstring[0] == ':')
801 GNoptind++; 810 c = ':';
802 } 811 else
803 else if (GNoptind == argc) 812 c = '?';
804 { 813 return c;
805 if (GNopterr) 814 }
806 { 815 else
807 /* 1003.2 specifies the format of this message. */ 816 /* We already incremented `GNoptind' once;
808 fprintf (stderr, _("%s: option requires an argument -- %c\n"), 817 * increment it again when taking next ARGV-elt as argument. */
809 argv[0], c); 818 GNoptarg = argv[GNoptind++];
810 } 819
811 if (optstring[0] == ':') 820 /* GNoptarg is now the argument, see if it's in the
812 c = ':'; 821 * table of longopts. */
813 else 822
814 c = '?'; 823 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '=';
815 return c; 824 nameend++)
816 } 825 /* Do nothing. */ ;
817 else 826
818 /* We already incremented `GNoptind' once; 827 /* Test all long options for either exact match
819 * increment it again when taking next ARGV-elt as argument. */ 828 * or abbreviated matches. */
820 GNoptarg = argv[GNoptind++]; 829 if (longopts != NULL)
821 830 for (p = longopts, option_index = 0; p->name; p++, option_index++)
822 /* GNoptarg is now the argument, see if it's in the 831 if (!strncmp (p->name, nextchar, nameend - nextchar))
823 * table of longopts. */ 832 {
824 833 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
825 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '='; 834 {
826 nameend++) 835 /* Exact match found. */
827 /* Do nothing. */ ; 836 pfound = p;
828 837 indfound = option_index;
829 /* Test all long options for either exact match 838 exact = 1;
830 * or abbreviated matches. */ 839 break;
831 if (longopts != NULL) 840 }
832 for (p = longopts, option_index = 0; p->name; p++, option_index++) 841 else if (pfound == NULL)
833 if (!strncmp (p->name, nextchar, nameend - nextchar)) 842 {
834 { 843 /* First nonexact match found. */
835 if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 844 pfound = p;
836 { 845 indfound = option_index;
837 /* Exact match found. */ 846 }
838 pfound = p; 847 else
839 indfound = option_index; 848 /* Second or later nonexact match found. */
840 exact = 1; 849 ambig = 1;
841 break; 850 }
842 } 851 if (ambig && !exact)
843 else if (pfound == NULL) 852 {
844 { 853 if (GNopterr)
845 /* First nonexact match found. */ 854 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), argv[0],
846 pfound = p; 855 argv[GNoptind]);
847 indfound = option_index; 856 nextchar += strlen (nextchar);
848 } 857 GNoptind++;
849 else 858 return '?';
850 /* Second or later nonexact match found. */ 859 }
851 ambig = 1; 860 if (pfound != NULL)
852 } 861 {
853 if (ambig && !exact) 862 option_index = indfound;
854 { 863 if (*nameend)
855 if (GNopterr) 864 {
856 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 865 /* Don't test has_arg with >, because some C compilers don't
857 argv[0], argv[GNoptind]); 866 * allow it to be used on enums. */
858 nextchar += strlen (nextchar); 867 if (pfound->has_arg)
859 GNoptind++; 868 GNoptarg = nameend + 1;
860 return '?'; 869 else
861 } 870 {
862 if (pfound != NULL) 871 if (GNopterr)
863 { 872 fprintf (stderr, _("\
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, _("\
875%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name); 873%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name);
876 874
877 nextchar += strlen (nextchar); 875 nextchar += strlen (nextchar);
878 return '?'; 876 return '?';
879 } 877 }
880 } 878 }
881 else if (pfound->has_arg == 1) 879 else if (pfound->has_arg == 1)
882 { 880 {
883 if (GNoptind < argc) 881 if (GNoptind < argc)
884 GNoptarg = argv[GNoptind++]; 882 GNoptarg = argv[GNoptind++];
885 else 883 else
886 { 884 {
887 if (GNopterr) 885 if (GNopterr)
888 fprintf (stderr, 886 fprintf (stderr, _("%s: option `%s' requires an argument\n"),
889 _("%s: option `%s' requires an argument\n"), 887 argv[0], argv[GNoptind - 1]);
890 argv[0], argv[GNoptind - 1]); 888 nextchar += strlen (nextchar);
891 nextchar += strlen (nextchar); 889 return optstring[0] == ':' ? ':' : '?';
892 return optstring[0] == ':' ? ':' : '?'; 890 }
893 } 891 }
894 } 892 nextchar += strlen (nextchar);
895 nextchar += strlen (nextchar); 893 if (longind != NULL)
896 if (longind != NULL) 894 *longind = option_index;
897 *longind = option_index; 895 if (pfound->flag)
898 if (pfound->flag) 896 {
899 { 897 *(pfound->flag) = pfound->val;
900 *(pfound->flag) = pfound->val; 898 return 0;
901 return 0; 899 }
902 } 900 return pfound->val;
903 return pfound->val;
904 }
905 nextchar = NULL;
906 return 'W'; /* Let the application handle it. */
907 } 901 }
902 nextchar = NULL;
903 return 'W'; /* Let the application handle it. */
904 }
908 if (temp[1] == ':') 905 if (temp[1] == ':')
906 {
907 if (temp[2] == ':')
909 { 908 {
910 if (temp[2] == ':') 909 /* This is an option that accepts an argument optionally. */
911 { 910 if (*nextchar != '\0')
912 /* This is an option that accepts an argument optionally. */ 911 {
913 if (*nextchar != '\0') 912 GNoptarg = nextchar;
914 { 913 GNoptind++;
915 GNoptarg = nextchar; 914 }
916 GNoptind++; 915 else
917 } 916 GNoptarg = NULL;
918 else 917 nextchar = NULL;
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 }
952 } 918 }
919 else
920 {
921 /* This is an option that requires an argument. */
922 if (*nextchar != '\0')
923 {
924 GNoptarg = nextchar;
925 /* If we end this ARGV-element by taking the rest as an arg,
926 * we must advance to the next element now. */
927 GNoptind++;
928 }
929 else if (GNoptind == argc)
930 {
931 if (GNopterr)
932 {
933 /* 1003.2 specifies the format of this message. */
934 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
935 argv[0], c);
936 }
937 if (optstring[0] == ':')
938 c = ':';
939 else
940 c = '?';
941 }
942 else
943 /* We already incremented `GNoptind' once;
944 * increment it again when taking next ARGV-elt as argument. */
945 GNoptarg = argv[GNoptind++];
946 nextchar = NULL;
947 }
948 }
953 return c; 949 return c;
954 } 950 }
955} 951}
956 952
957static int 953static int
958GNgetopt_long (int argc, char *const *argv, const char *options, 954GNgetopt_long (int argc, char *const *argv, const char *options,
959 const struct GNoption *long_options, int *opt_index) 955 const struct GNoption *long_options, int *opt_index)
960{ 956{
961 return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0); 957 return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0);
962} 958}
@@ -975,8 +971,8 @@ GNgetopt_long (int argc, char *const *argv, const char *options,
975 */ 971 */
976int 972int
977GNUNET_GETOPT_run (const char *binaryOptions, 973GNUNET_GETOPT_run (const char *binaryOptions,
978 const struct GNUNET_GETOPT_CommandLineOption *allOptions, 974 const struct GNUNET_GETOPT_CommandLineOption *allOptions,
979 unsigned int argc, char *const *argv) 975 unsigned int argc, char *const *argv)
980{ 976{
981 struct GNoption *long_options; 977 struct GNoption *long_options;
982 struct GNUNET_GETOPT_CommandLineProcessorContext clpc; 978 struct GNUNET_GETOPT_CommandLineProcessorContext clpc;
@@ -1001,15 +997,15 @@ GNUNET_GETOPT_run (const char *binaryOptions,
1001 shorts = GNUNET_malloc (count * 2 + 1); 997 shorts = GNUNET_malloc (count * 2 + 1);
1002 spos = 0; 998 spos = 0;
1003 for (i = 0; i < count; i++) 999 for (i = 0; i < count; i++)
1004 { 1000 {
1005 long_options[i].name = allOptions[i].name; 1001 long_options[i].name = allOptions[i].name;
1006 long_options[i].has_arg = allOptions[i].require_argument; 1002 long_options[i].has_arg = allOptions[i].require_argument;
1007 long_options[i].flag = NULL; 1003 long_options[i].flag = NULL;
1008 long_options[i].val = allOptions[i].shortName; 1004 long_options[i].val = allOptions[i].shortName;
1009 shorts[spos++] = allOptions[i].shortName; 1005 shorts[spos++] = allOptions[i].shortName;
1010 if (allOptions[i].require_argument != 0) 1006 if (allOptions[i].require_argument != 0)
1011 shorts[spos++] = ':'; 1007 shorts[spos++] = ':';
1012 } 1008 }
1013 long_options[count].name = NULL; 1009 long_options[count].name = NULL;
1014 long_options[count].has_arg = 0; 1010 long_options[count].has_arg = 0;
1015 long_options[count].flag = NULL; 1011 long_options[count].flag = NULL;
@@ -1018,31 +1014,31 @@ GNUNET_GETOPT_run (const char *binaryOptions,
1018 cont = GNUNET_OK; 1014 cont = GNUNET_OK;
1019 /* main getopt loop */ 1015 /* main getopt loop */
1020 while (cont == GNUNET_OK) 1016 while (cont == GNUNET_OK)
1017 {
1018 int option_index = 0;
1019
1020 c = GNgetopt_long (argc, argv, shorts, long_options, &option_index);
1021
1022 if (c == GNUNET_SYSERR)
1023 break; /* No more flags to process */
1024
1025 for (i = 0; i < count; i++)
1026 {
1027 clpc.currentArgument = GNoptind - 1;
1028 if ((char) c == allOptions[i].shortName)
1029 {
1030 cont =
1031 allOptions[i].processor (&clpc, allOptions[i].scls,
1032 allOptions[i].name, GNoptarg);
1033 break;
1034 }
1035 }
1036 if (i == count)
1021 { 1037 {
1022 int option_index = 0; 1038 fprintf (stderr, _("Use --help to get a list of options.\n"));
1023 1039 cont = GNUNET_SYSERR;
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 }
1045 } 1040 }
1041 }
1046 1042
1047 GNUNET_free (shorts); 1043 GNUNET_free (shorts);
1048 GNUNET_free (long_options); 1044 GNUNET_free (long_options);
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index 8564d591f..8b49c5907 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -42,8 +42,8 @@
42 */ 42 */
43int 43int
44GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext 44GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
45 *ctx, void *scls, const char *option, 45 *ctx, void *scls, const char *option,
46 const char *value) 46 const char *value)
47{ 47{
48 const char *version = scls; 48 const char *version = scls;
49 49
@@ -66,8 +66,8 @@ GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
66 */ 66 */
67int 67int
68GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext 68GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
69 *ctx, void *scls, const char *option, 69 *ctx, void *scls, const char *option,
70 const char *value) 70 const char *value)
71{ 71{
72 const char *about = scls; 72 const char *about = scls;
73 size_t slen; 73 size_t slen;
@@ -81,74 +81,74 @@ GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
81 81
82 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about)); 82 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
83 printf (_ 83 printf (_
84 ("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"));
85 i = 0; 85 i = 0;
86 opt = ctx->allOptions; 86 opt = ctx->allOptions;
87 while (opt[i].description != NULL) 87 while (opt[i].description != NULL)
88 {
89 if (opt[i].shortName == '\0')
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)
88 { 96 {
89 if (opt[i].shortName == '\0') 97 printf ("=%s", opt[i].argumentHelp);
90 printf (" "); 98 slen += 1 + strlen (opt[i].argumentHelp);
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++;
148 } 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;
116OUTER:
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++;
148 }
149 printf ("Report bugs to gnunet-developers@gnu.org.\n" 149 printf ("Report bugs to gnunet-developers@gnu.org.\n"
150 "GNUnet home page: http://www.gnu.org/software/gnunet/\n" 150 "GNUnet home page: http://www.gnu.org/software/gnunet/\n"
151 "General help using GNU software: http://www.gnu.org/gethelp/\n"); 151 "General help using GNU software: http://www.gnu.org/gethelp/\n");
152 return GNUNET_SYSERR; 152 return GNUNET_SYSERR;
153} 153}
154 154
@@ -168,10 +168,9 @@ GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
168 * @return GNUNET_OK 168 * @return GNUNET_OK
169 */ 169 */
170int 170int
171GNUNET_GETOPT_increment_value (struct 171GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext
172 GNUNET_GETOPT_CommandLineProcessorContext *ctx, 172 *ctx, void *scls, const char *option,
173 void *scls, const char *option, 173 const char *value)
174 const char *value)
175{ 174{
176 int *val = scls; 175 int *val = scls;
177 176
@@ -196,7 +195,7 @@ GNUNET_GETOPT_increment_value (struct
196 */ 195 */
197int 196int
198GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 197GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
199 void *scls, const char *option, const char *value) 198 void *scls, const char *option, const char *value)
200{ 199{
201 int *val = scls; 200 int *val = scls;
202 201
@@ -220,9 +219,8 @@ GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
220 * @return GNUNET_OK 219 * @return GNUNET_OK
221 */ 220 */
222int 221int
223GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext 222GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
224 *ctx, void *scls, const char *option, 223 void *scls, const char *option, const char *value)
225 const char *value)
226{ 224{
227 char **val = scls; 225 char **val = scls;
228 226
@@ -247,18 +245,16 @@ GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext
247 * @return GNUNET_OK if parsing the value worked 245 * @return GNUNET_OK if parsing the value worked
248 */ 246 */
249int 247int
250GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext 248GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
251 *ctx, void *scls, const char *option, 249 void *scls, const char *option, const char *value)
252 const char *value)
253{ 250{
254 unsigned long long *val = scls; 251 unsigned long long *val = scls;
255 252
256 if (1 != SSCANF (value, "%llu", val)) 253 if (1 != SSCANF (value, "%llu", val))
257 { 254 {
258 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), 255 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option);
259 option); 256 return GNUNET_SYSERR;
260 return GNUNET_SYSERR; 257 }
261 }
262 return GNUNET_OK; 258 return GNUNET_OK;
263} 259}
264 260
@@ -278,16 +274,15 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext
278 */ 274 */
279int 275int
280GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 276GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
281 void *scls, const char *option, const char *value) 277 void *scls, const char *option, const char *value)
282{ 278{
283 unsigned int *val = scls; 279 unsigned int *val = scls;
284 280
285 if (1 != SSCANF (value, "%u", val)) 281 if (1 != SSCANF (value, "%u", val))
286 { 282 {
287 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), 283 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option);
288 option); 284 return GNUNET_SYSERR;
289 return GNUNET_SYSERR; 285 }
290 }
291 return GNUNET_OK; 286 return GNUNET_OK;
292} 287}
293 288
diff --git a/src/util/gnunet-config-diff.c b/src/util/gnunet-config-diff.c
index 992480a30..207b9518a 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 e97793114..131693dd1 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 c15f0d3f5..5d4c5c224 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)
179 { 182 {
180 if (GNUNET_TIME_absolute_get_duration (cache->last_request).rel_value < 183 if (prev != NULL)
181 60 * 60 * 1000) 184 {
182 { 185 prev->next = cache->next;
183 if (prev != NULL) 186 GNUNET_free_non_null (cache->addr);
184 { 187 GNUNET_free (cache->sa);
185 prev->next = cache->next; 188 GNUNET_free (cache);
186 GNUNET_free_non_null (cache->addr); 189 cache = prev->next;
187 GNUNET_free (cache->sa); 190 }
188 GNUNET_free (cache); 191 else
189 cache = prev->next; 192 {
190 } 193 head = cache->next;
191 else 194 GNUNET_free_non_null (cache->addr);
192 { 195 GNUNET_free (cache->sa);
193 head = cache->next; 196 GNUNET_free (cache);
194 GNUNET_free_non_null (cache->addr); 197 cache = head;
195 GNUNET_free (cache->sa); 198 }
196 GNUNET_free (cache); 199 continue;
197 cache = head;
198 }
199 continue;
200 }
201 prev = cache;
202 cache = cache->next;
203 } 200 }
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)
205 { 209 {
206 cache->last_request = now; 210 GNUNET_free_non_null (cache->addr);
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 ();
225 cache->addr = NULL; 211 cache->addr = NULL;
212 cache->salen = 0;
226 cache_resolve (cache); 213 cache_resolve (cache);
227 head = cache;
228 } 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 ();
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,37 +254,36 @@ 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, 261 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Could not resolve `%s' (%s): %s\n"),
262 _("Could not resolve `%s' (%s): %s\n"), hostname, 262 hostname,
263 (domain == 263 (domain ==
264 AF_INET) ? "IPv4" : ((domain == 264 AF_INET) ? "IPv4" : ((domain == AF_INET6) ? "IPv6" : "any"),
265 AF_INET6) ? "IPv6" : "any"), 265 gai_strerror (s));
266 gai_strerror (s)); 266 if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY)
267 if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY)
268#ifndef MINGW 267#ifndef MINGW
269 || (s == EAI_SYSTEM) 268 || (s == EAI_SYSTEM)
270#else 269#else
271 // FIXME NILS 270 // FIXME NILS
272 || 1 271 || 1
273#endif 272#endif
274 ) 273 )
275 return GNUNET_NO; /* other function may still succeed */ 274 return GNUNET_NO; /* other function may still succeed */
276 return GNUNET_SYSERR; 275 return GNUNET_SYSERR;
277 } 276 }
278 if (result == NULL) 277 if (result == NULL)
279 return GNUNET_SYSERR; 278 return GNUNET_SYSERR;
280 pos = result; 279 pos = result;
281 while (pos != NULL) 280 while (pos != NULL)
282 { 281 {
283 GNUNET_SERVER_transmit_context_append_data (tc, pos->ai_addr, 282 GNUNET_SERVER_transmit_context_append_data (tc, pos->ai_addr,
284 pos->ai_addrlen, 283 pos->ai_addrlen,
285 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 284 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
286 pos = pos->ai_next; 285 pos = pos->ai_next;
287 } 286 }
288 freeaddrinfo (result); 287 freeaddrinfo (result);
289 return GNUNET_OK; 288 return GNUNET_OK;
290} 289}
@@ -293,7 +292,7 @@ getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc,
293#if HAVE_GETHOSTBYNAME2 292#if HAVE_GETHOSTBYNAME2
294static int 293static int
295gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, 294gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
296 const char *hostname, int domain) 295 const char *hostname, int domain)
297{ 296{
298 struct hostent *hp; 297 struct hostent *hp;
299 struct sockaddr_in a4; 298 struct sockaddr_in a4;
@@ -302,48 +301,48 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
302 int ret2; 301 int ret2;
303 302
304 if (domain == AF_UNSPEC) 303 if (domain == AF_UNSPEC)
305 { 304 {
306 ret1 = gethostbyname2_resolve (tc, hostname, AF_INET); 305 ret1 = gethostbyname2_resolve (tc, hostname, AF_INET);
307 ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6); 306 ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6);
308 if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK)) 307 if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK))
309 return GNUNET_OK; 308 return GNUNET_OK;
310 if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR)) 309 if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR))
311 return GNUNET_SYSERR; 310 return GNUNET_SYSERR;
312 return GNUNET_NO; 311 return GNUNET_NO;
313 } 312 }
314 hp = gethostbyname2 (hostname, domain); 313 hp = gethostbyname2 (hostname, domain);
315 if (hp == NULL) 314 if (hp == NULL)
316 { 315 {
317 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 316 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
318 _("Could not find IP of host `%s': %s\n"), hostname, 317 _("Could not find IP of host `%s': %s\n"), hostname,
319 hstrerror (h_errno)); 318 hstrerror (h_errno));
320 return GNUNET_SYSERR; 319 return GNUNET_SYSERR;
321 } 320 }
322 GNUNET_assert (hp->h_addrtype == domain); 321 GNUNET_assert (hp->h_addrtype == domain);
323 if (domain == AF_INET) 322 if (domain == AF_INET)
324 { 323 {
325 GNUNET_assert (hp->h_length == sizeof (struct in_addr)); 324 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
326 memset (&a4, 0, sizeof (a4)); 325 memset (&a4, 0, sizeof (a4));
327 a4.sin_family = AF_INET; 326 a4.sin_family = AF_INET;
328#if HAVE_SOCKADDR_IN_SIN_LEN 327#if HAVE_SOCKADDR_IN_SIN_LEN
329 a4.sin_len = (u_char) sizeof (struct sockaddr_in); 328 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
330#endif 329#endif
331 memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length); 330 memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length);
332 GNUNET_SERVER_transmit_context_append_data (tc, &a4, sizeof (a4), 331 GNUNET_SERVER_transmit_context_append_data (tc, &a4, sizeof (a4),
333 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 332 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
334 } 333 }
335 else 334 else
336 { 335 {
337 GNUNET_assert (hp->h_length == sizeof (struct in6_addr)); 336 GNUNET_assert (hp->h_length == sizeof (struct in6_addr));
338 memset (&a6, 0, sizeof (a6)); 337 memset (&a6, 0, sizeof (a6));
339 a6.sin6_family = AF_INET6; 338 a6.sin6_family = AF_INET6;
340#if HAVE_SOCKADDR_IN_SIN_LEN 339#if HAVE_SOCKADDR_IN_SIN_LEN
341 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); 340 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
342#endif 341#endif
343 memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length); 342 memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length);
344 GNUNET_SERVER_transmit_context_append_data (tc, &a6, sizeof (a6), 343 GNUNET_SERVER_transmit_context_append_data (tc, &a6, sizeof (a6),
345 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 344 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
346 } 345 }
347 return GNUNET_OK; 346 return GNUNET_OK;
348} 347}
349#endif 348#endif
@@ -351,24 +350,24 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
351#if HAVE_GETHOSTBYNAME 350#if HAVE_GETHOSTBYNAME
352static int 351static int
353gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc, 352gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
354 const char *hostname) 353 const char *hostname)
355{ 354{
356 struct hostent *hp; 355 struct hostent *hp;
357 struct sockaddr_in addr; 356 struct sockaddr_in addr;
358 357
359 hp = GETHOSTBYNAME (hostname); 358 hp = GETHOSTBYNAME (hostname);
360 if (hp == NULL) 359 if (hp == NULL)
361 { 360 {
362 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 361 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
363 _("Could not find IP of host `%s': %s\n"), hostname, 362 _("Could not find IP of host `%s': %s\n"), hostname,
364 hstrerror (h_errno)); 363 hstrerror (h_errno));
365 return GNUNET_SYSERR; 364 return GNUNET_SYSERR;
366 } 365 }
367 if (hp->h_addrtype != AF_INET) 366 if (hp->h_addrtype != AF_INET)
368 { 367 {
369 GNUNET_break (0); 368 GNUNET_break (0);
370 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
371 } 370 }
372 GNUNET_assert (hp->h_length == sizeof (struct in_addr)); 371 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
373 memset (&addr, 0, sizeof (addr)); 372 memset (&addr, 0, sizeof (addr));
374 addr.sin_family = AF_INET; 373 addr.sin_family = AF_INET;
@@ -377,7 +376,7 @@ gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
377#endif 376#endif
378 memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length); 377 memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length);
379 GNUNET_SERVER_transmit_context_append_data (tc, &addr, sizeof (addr), 378 GNUNET_SERVER_transmit_context_append_data (tc, &addr, sizeof (addr),
380 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 379 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
381 return GNUNET_OK; 380 return GNUNET_OK;
382} 381}
383#endif 382#endif
@@ -391,8 +390,8 @@ gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
391 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any" 390 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
392 */ 391 */
393static void 392static void
394get_ip_from_hostname (struct GNUNET_SERVER_Client *client, 393get_ip_from_hostname (struct GNUNET_SERVER_Client *client, const char *hostname,
395 const char *hostname, int domain) 394 int domain)
396{ 395{
397 int ret; 396 int ret;
398 struct GNUNET_SERVER_TransmitContext *tc; 397 struct GNUNET_SERVER_TransmitContext *tc;
@@ -412,7 +411,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
412 gethostbyname_resolve (tc, hostname); 411 gethostbyname_resolve (tc, hostname);
413#endif 412#endif
414 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, 413 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
415 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 414 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
416 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); 415 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
417} 416}
418 417
@@ -426,7 +425,7 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
426 */ 425 */
427static void 426static void
428handle_get (void *cls, struct GNUNET_SERVER_Client *client, 427handle_get (void *cls, struct GNUNET_SERVER_Client *client,
429 const struct GNUNET_MessageHeader *message) 428 const struct GNUNET_MessageHeader *message)
430{ 429{
431 uint16_t msize; 430 uint16_t msize;
432 const struct GNUNET_RESOLVER_GetMessage *msg; 431 const struct GNUNET_RESOLVER_GetMessage *msg;
@@ -438,78 +437,78 @@ handle_get (void *cls, struct GNUNET_SERVER_Client *client,
438 437
439 msize = ntohs (message->size); 438 msize = ntohs (message->size);
440 if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage)) 439 if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage))
441 { 440 {
442 GNUNET_break (0); 441 GNUNET_break (0);
443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
444 return; 443 return;
445 } 444 }
446 msg = (const struct GNUNET_RESOLVER_GetMessage *) message; 445 msg = (const struct GNUNET_RESOLVER_GetMessage *) message;
447 size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage); 446 size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage);
448 direction = ntohl (msg->direction); 447 direction = ntohl (msg->direction);
449 domain = ntohl (msg->domain); 448 domain = ntohl (msg->domain);
450 if (direction == GNUNET_NO) 449 if (direction == GNUNET_NO)
450 {
451 /* IP from hostname */
452 hostname = (const char *) &msg[1];
453 if (hostname[size - 1] != '\0')
451 { 454 {
452 /* IP from hostname */ 455 GNUNET_break (0);
453 hostname = (const char *) &msg[1]; 456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
454 if (hostname[size - 1] != '\0') 457 return;
455 { 458 }
456 GNUNET_break (0);
457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
458 return;
459 }
460#if DEBUG_RESOLVER 459#if DEBUG_RESOLVER
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver asked to look up `%s'.\n"),
462 _("Resolver asked to look up `%s'.\n"), hostname); 461 hostname);
463#endif 462#endif
464 get_ip_from_hostname (client, hostname, domain); 463 get_ip_from_hostname (client, hostname, domain);
465 } 464 }
466 else 465 else
467 { 466 {
468#if DEBUG_RESOLVER 467#if DEBUG_RESOLVER
469 char buf[INET6_ADDRSTRLEN]; 468 char buf[INET6_ADDRSTRLEN];
470#endif 469#endif
471 if (size < sizeof (struct sockaddr)) 470 if (size < sizeof (struct sockaddr))
472 { 471 {
473 GNUNET_break (0); 472 GNUNET_break (0);
474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
475 return; 474 return;
476 } 475 }
477 sa = (const struct sockaddr *) &msg[1]; 476 sa = (const struct sockaddr *) &msg[1];
478 switch (sa->sa_family) 477 switch (sa->sa_family)
479 { 478 {
480 case AF_INET: 479 case AF_INET:
481 if (size != sizeof (struct sockaddr_in)) 480 if (size != sizeof (struct sockaddr_in))
482 { 481 {
483 GNUNET_break (0); 482 GNUNET_break (0);
484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
485 return; 484 return;
486 } 485 }
487#if DEBUG_RESOLVER 486#if DEBUG_RESOLVER
488 inet_ntop (AF_INET, sa, buf, size); 487 inet_ntop (AF_INET, sa, buf, size);
489#endif 488#endif
490 break; 489 break;
491 case AF_INET6: 490 case AF_INET6:
492 if (size != sizeof (struct sockaddr_in6)) 491 if (size != sizeof (struct sockaddr_in6))
493 { 492 {
494 GNUNET_break (0); 493 GNUNET_break (0);
495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
496 return; 495 return;
497 } 496 }
498#if DEBUG_RESOLVER 497#if DEBUG_RESOLVER
499 inet_ntop (AF_INET6, sa, buf, size); 498 inet_ntop (AF_INET6, sa, buf, size);
500#endif 499#endif
501 break; 500 break;
502 default: 501 default:
503 GNUNET_break (0); 502 GNUNET_break (0);
504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
505 return; 504 return;
506 } 505 }
507#if DEBUG_RESOLVER 506#if DEBUG_RESOLVER
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 _("Resolver asked to look up IP address `%s'.\n"), buf); 508 _("Resolver asked to look up IP address `%s'.\n"), buf);
510#endif 509#endif
511 get_ip_as_string (client, sa, size); 510 get_ip_as_string (client, sa, size);
512 } 511 }
513} 512}
514 513
515 514
@@ -546,18 +545,18 @@ main (int argc, char *const *argv)
546 struct IPCache *pos; 545 struct IPCache *pos;
547 546
548 ret = 547 ret =
549 (GNUNET_OK == 548 (GNUNET_OK ==
550 GNUNET_SERVICE_run (argc, argv, "resolver", GNUNET_SERVICE_OPTION_NONE, 549 GNUNET_SERVICE_run (argc, argv, "resolver", GNUNET_SERVICE_OPTION_NONE,
551 &run, NULL)) ? 0 : 1; 550 &run, NULL)) ? 0 : 1;
552 551
553 while (head != NULL) 552 while (head != NULL)
554 { 553 {
555 pos = head->next; 554 pos = head->next;
556 GNUNET_free_non_null (head->addr); 555 GNUNET_free_non_null (head->addr);
557 GNUNET_free (head->sa); 556 GNUNET_free (head->sa);
558 GNUNET_free (head); 557 GNUNET_free (head);
559 head = pos; 558 head = pos;
560 } 559 }
561 return ret; 560 return ret;
562} 561}
563 562
diff --git a/src/util/load.c b/src/util/load.c
index 1df1abc7c..e978a950d 100644
--- a/src/util/load.c
+++ b/src/util/load.c
@@ -94,23 +94,23 @@ internal_update (struct GNUNET_LOAD_Value *load)
94 if (delta.rel_value < load->autodecline.rel_value) 94 if (delta.rel_value < load->autodecline.rel_value)
95 return; 95 return;
96 if (load->autodecline.rel_value == 0) 96 if (load->autodecline.rel_value == 0)
97 { 97 {
98 load->runavg_delay = 0.0; 98 load->runavg_delay = 0.0;
99 load->load = 0; 99 load->load = 0;
100 return; 100 return;
101 } 101 }
102 n = delta.rel_value / load->autodecline.rel_value; 102 n = delta.rel_value / load->autodecline.rel_value;
103 if (n > 16) 103 if (n > 16)
104 { 104 {
105 load->runavg_delay = 0.0; 105 load->runavg_delay = 0.0;
106 load->load = 0; 106 load->load = 0;
107 return; 107 return;
108 } 108 }
109 while (n > 0) 109 while (n > 0)
110 { 110 {
111 n--; 111 n--;
112 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; 112 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
113 } 113 }
114} 114}
115 115
116 116
@@ -142,7 +142,7 @@ GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
142 */ 142 */
143void 143void
144GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load, 144GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
145 struct GNUNET_TIME_Relative autodecline) 145 struct GNUNET_TIME_Relative autodecline)
146{ 146{
147 internal_update (load); 147 internal_update (load);
148 load->autodecline = autodecline; 148 load->autodecline = autodecline;
@@ -177,10 +177,10 @@ calculate_load (struct GNUNET_LOAD_Value *load)
177 nm1 = n - 1.0; 177 nm1 = n - 1.0;
178 avgdel = sum_val_i / n; 178 avgdel = sum_val_i / n;
179 stddev = 179 stddev =
180 (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + 180 (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i +
181 n * avgdel * avgdel) / nm1; 181 n * avgdel * avgdel) / nm1;
182 if (stddev <= 0) 182 if (stddev <= 0)
183 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 */
184 /* now calculate load based on how far out we are from 184 /* now calculate load based on how far out we are from
185 * 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 */
186 if (load->runavg_delay < avgdel) 186 if (load->runavg_delay < avgdel)
@@ -243,11 +243,11 @@ GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data)
243 internal_update (load); 243 internal_update (load);
244 load->last_update = GNUNET_TIME_absolute_get (); 244 load->last_update = GNUNET_TIME_absolute_get ();
245 if (data > 64 * 1024) 245 if (data > 64 * 1024)
246 { 246 {
247 /* very large */ 247 /* very large */
248 load->load = 100.0; 248 load->load = 100.0;
249 return; 249 return;
250 } 250 }
251 dv = (uint32_t) data; 251 dv = (uint32_t) data;
252 load->cummulative_delay += dv; 252 load->cummulative_delay += dv;
253 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 3fc71e666..180adc164 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -90,11 +90,11 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
90 mode = !doBlock; 90 mode = !doBlock;
91 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR) 91 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
92 92
93 { 93 {
94 SetErrnoFromWinsockError (WSAGetLastError ()); 94 SetErrnoFromWinsockError (WSAGetLastError ());
95 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); 95 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
96 return GNUNET_SYSERR; 96 return GNUNET_SYSERR;
97 } 97 }
98 return GNUNET_OK; 98 return GNUNET_OK;
99 99
100#else 100#else
@@ -103,10 +103,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
103 103
104 if (flags == -1) 104 if (flags == -1)
105 105
106 { 106 {
107 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 107 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
108 return GNUNET_SYSERR; 108 return GNUNET_SYSERR;
109 } 109 }
110 if (doBlock) 110 if (doBlock)
111 flags &= ~O_NONBLOCK; 111 flags &= ~O_NONBLOCK;
112 112
@@ -114,10 +114,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
114 flags |= O_NONBLOCK; 114 flags |= O_NONBLOCK;
115 if (0 != fcntl (fd->fd, F_SETFL, flags)) 115 if (0 != fcntl (fd->fd, F_SETFL, flags))
116 116
117 { 117 {
118 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 118 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
119 return GNUNET_SYSERR; 119 return GNUNET_SYSERR;
120 } 120 }
121 return GNUNET_OK; 121 return GNUNET_OK;
122#endif 122#endif
123} 123}
@@ -162,7 +162,7 @@ socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
162 162
163 if (0 != 163 if (0 !=
164 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, 164 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value,
165 sizeof (abs_value))) 165 sizeof (abs_value)))
166 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 166 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
167} 167}
168#endif 168#endif
@@ -181,15 +181,14 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
181#ifndef WINDOWS 181#ifndef WINDOWS
182 int value = 1; 182 int value = 1;
183 183
184 if (0 != 184 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
185 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
186 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 185 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
187#else 186#else
188 const char *abs_value = "1"; 187 const char *abs_value = "1";
189 188
190 if (0 != 189 if (0 !=
191 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, 190 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
192 sizeof (abs_value))) 191 sizeof (abs_value)))
193 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 192 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
194#endif 193#endif
195} 194}
@@ -205,8 +204,7 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
205 */ 204 */
206struct GNUNET_NETWORK_Handle * 205struct GNUNET_NETWORK_Handle *
207GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, 206GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
208 struct sockaddr *address, 207 struct sockaddr *address, socklen_t * address_len)
209 socklen_t * address_len)
210{ 208{
211 struct GNUNET_NETWORK_Handle *ret; 209 struct GNUNET_NETWORK_Handle *ret;
212 210
@@ -219,7 +217,7 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
219 217
220 if (gsn == 0) 218 if (gsn == 0)
221 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n", 219 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
222 GNUNET_a2s (&name, namelen)); 220 GNUNET_a2s (&name, namelen));
223 } 221 }
224#endif 222#endif
225 ret->fd = accept (desc->fd, address, address_len); 223 ret->fd = accept (desc->fd, address, address_len);
@@ -228,36 +226,36 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
228 else 226 else
229 ret->af = desc->af; 227 ret->af = desc->af;
230 if (ret->fd == INVALID_SOCKET) 228 if (ret->fd == INVALID_SOCKET)
231 { 229 {
232#ifdef MINGW 230#ifdef MINGW
233 SetErrnoFromWinsockError (WSAGetLastError ()); 231 SetErrnoFromWinsockError (WSAGetLastError ());
234#endif 232#endif
235 GNUNET_free (ret); 233 GNUNET_free (ret);
236 return NULL; 234 return NULL;
237 } 235 }
238#ifndef MINGW 236#ifndef MINGW
239 if (ret->fd >= FD_SETSIZE) 237 if (ret->fd >= FD_SETSIZE)
240 { 238 {
241 GNUNET_break (0 == close (ret->fd)); 239 GNUNET_break (0 == close (ret->fd));
242 GNUNET_free (ret); 240 GNUNET_free (ret);
243 errno = EMFILE; 241 errno = EMFILE;
244 return NULL; 242 return NULL;
245 } 243 }
246#endif 244#endif
247 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) 245 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
248 246
249 { 247 {
250 248
251 /* we might want to treat this one as fatal... */ 249 /* we might want to treat this one as fatal... */
252 GNUNET_break (0); 250 GNUNET_break (0);
253 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); 251 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
254 return NULL; 252 return NULL;
255 } 253 }
256 254
257#ifndef MINGW 255#ifndef MINGW
258 if (GNUNET_OK != socket_set_inheritable (ret)) 256 if (GNUNET_OK != socket_set_inheritable (ret))
259 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 257 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
260 "socket_set_inheritable"); 258 "socket_set_inheritable");
261#endif 259#endif
262#ifdef DARWIN 260#ifdef DARWIN
263 socket_set_nosigpipe (ret); 261 socket_set_nosigpipe (ret);
@@ -279,8 +277,8 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
279 */ 277 */
280int 278int
281GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, 279GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
282 const struct sockaddr *address, 280 const struct sockaddr *address,
283 socklen_t address_len) 281 socklen_t address_len)
284{ 282{
285 int ret; 283 int ret;
286 284
@@ -289,8 +287,7 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
289 const int on = 1; 287 const int on = 1;
290 288
291 if (desc->af == AF_INET6) 289 if (desc->af == AF_INET6)
292 if (0 != 290 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
293 setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
294 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); 291 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
295#endif 292#endif
296#endif 293#endif
@@ -302,11 +299,11 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
302#ifndef LINUX 299#ifndef LINUX
303#ifndef MINGW 300#ifndef MINGW
304 if (address->sa_family == AF_UNIX) 301 if (address->sa_family == AF_UNIX)
305 { 302 {
306 const struct sockaddr_un *un = (const struct sockaddr_un *) address; 303 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
307 304
308 (void) unlink (un->sun_path); 305 (void) unlink (un->sun_path);
309 } 306 }
310#endif 307#endif
311#endif 308#endif
312 ret = bind (desc->fd, address, address_len); 309 ret = bind (desc->fd, address, address_len);
@@ -341,8 +338,8 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
341 DWORD error = 0; 338 DWORD error = 0;
342 339
343#if DEBUG_NETWORK 340#if DEBUG_NETWORK
344 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", 341 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", "Closing 0x%x\n",
345 "Closing 0x%x\n", desc->fd); 342 desc->fd);
346#endif 343#endif
347 SetLastError (0); 344 SetLastError (0);
348 ret = closesocket (desc->fd); 345 ret = closesocket (desc->fd);
@@ -350,8 +347,8 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
350 SetErrnoFromWinsockError (error); 347 SetErrnoFromWinsockError (error);
351#if DEBUG_NETWORK 348#if DEBUG_NETWORK
352 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", 349 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
353 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", 350 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret,
354 desc->fd, ret, error); 351 error);
355#endif 352#endif
356#else 353#else
357 ret = close (desc->fd); 354 ret = close (desc->fd);
@@ -359,12 +356,12 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
359#ifndef LINUX 356#ifndef LINUX
360#ifndef MINGW 357#ifndef MINGW
361 if ((desc->af == AF_UNIX) && (NULL != desc->addr)) 358 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
362 { 359 {
363 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr; 360 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
364 361
365 if (0 != unlink (un->sun_path)) 362 if (0 != unlink (un->sun_path))
366 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path); 363 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path);
367 } 364 }
368#endif 365#endif
369#endif 366#endif
370 GNUNET_free_non_null (desc->addr); 367 GNUNET_free_non_null (desc->addr);
@@ -388,7 +385,7 @@ GNUNET_NETWORK_socket_box_native (int fd)
388 struct GNUNET_NETWORK_Handle *ret; 385 struct GNUNET_NETWORK_Handle *ret;
389 386
390 if (fcntl (fd, F_GETFD) < 0) 387 if (fcntl (fd, F_GETFD) < 0)
391 return NULL; /* invalid FD */ 388 return NULL; /* invalid FD */
392 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); 389 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
393 ret->fd = fd; 390 ret->fd = fd;
394 ret->af = AF_UNSPEC; 391 ret->af = AF_UNSPEC;
@@ -406,8 +403,8 @@ GNUNET_NETWORK_socket_box_native (int fd)
406 */ 403 */
407int 404int
408GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, 405GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
409 const struct sockaddr *address, 406 const struct sockaddr *address,
410 socklen_t address_len) 407 socklen_t address_len)
411{ 408{
412 int ret; 409 int ret;
413 410
@@ -415,11 +412,11 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
415 412
416#ifdef MINGW 413#ifdef MINGW
417 if (SOCKET_ERROR == ret) 414 if (SOCKET_ERROR == ret)
418 { 415 {
419 SetErrnoFromWinsockError (WSAGetLastError ()); 416 SetErrnoFromWinsockError (WSAGetLastError ());
420 if (errno == EWOULDBLOCK) 417 if (errno == EWOULDBLOCK)
421 errno = EINPROGRESS; 418 errno = EINPROGRESS;
422 } 419 }
423#endif 420#endif
424 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 421 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
425} 422}
@@ -437,8 +434,8 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
437 */ 434 */
438int 435int
439GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, 436GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
440 int level, int optname, void *optval, 437 int level, int optname, void *optval,
441 socklen_t * optlen) 438 socklen_t * optlen)
442{ 439{
443 int ret; 440 int ret;
444 441
@@ -463,7 +460,7 @@ GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
463 */ 460 */
464int 461int
465GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, 462GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
466 int backlog) 463 int backlog)
467{ 464{
468 int ret; 465 int ret;
469 466
@@ -486,7 +483,7 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
486 */ 483 */
487ssize_t 484ssize_t
488GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * 485GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
489 desc) 486 desc)
490{ 487{
491 int error; 488 int error;
492 489
@@ -519,9 +516,8 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
519 */ 516 */
520ssize_t 517ssize_t
521GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc, 518GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
522 void *buffer, size_t length, 519 void *buffer, size_t length,
523 struct sockaddr * src_addr, 520 struct sockaddr * src_addr, socklen_t * addrlen)
524 socklen_t * addrlen)
525{ 521{
526 int ret; 522 int ret;
527 int flags; 523 int flags;
@@ -549,7 +545,7 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
549 */ 545 */
550ssize_t 546ssize_t
551GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc, 547GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
552 void *buffer, size_t length) 548 void *buffer, size_t length)
553{ 549{
554 int ret; 550 int ret;
555 int flags; 551 int flags;
@@ -578,7 +574,7 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
578 */ 574 */
579ssize_t 575ssize_t
580GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc, 576GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
581 const void *buffer, size_t length) 577 const void *buffer, size_t length)
582{ 578{
583 int ret; 579 int ret;
584 int flags; 580 int flags;
@@ -617,9 +613,9 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
617 */ 613 */
618ssize_t 614ssize_t
619GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc, 615GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
620 const void *message, size_t length, 616 const void *message, size_t length,
621 const struct sockaddr * dest_addr, 617 const struct sockaddr * dest_addr,
622 socklen_t dest_len) 618 socklen_t dest_len)
623{ 619{
624 int ret; 620 int ret;
625 int flags; 621 int flags;
@@ -652,8 +648,8 @@ GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
652 */ 648 */
653int 649int
654GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level, 650GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
655 int option_name, const void *option_value, 651 int option_name, const void *option_value,
656 socklen_t option_len) 652 socklen_t option_len)
657{ 653{
658 int ret; 654 int ret;
659 655
@@ -685,36 +681,36 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
685 ret->af = domain; 681 ret->af = domain;
686 ret->fd = socket (domain, type, protocol); 682 ret->fd = socket (domain, type, protocol);
687 if (INVALID_SOCKET == ret->fd) 683 if (INVALID_SOCKET == ret->fd)
688 { 684 {
689#ifdef MINGW 685#ifdef MINGW
690 SetErrnoFromWinsockError (WSAGetLastError ()); 686 SetErrnoFromWinsockError (WSAGetLastError ());
691#endif 687#endif
692 GNUNET_free (ret); 688 GNUNET_free (ret);
693 return NULL; 689 return NULL;
694 } 690 }
695 691
696#ifndef MINGW 692#ifndef MINGW
697 if (ret->fd >= FD_SETSIZE) 693 if (ret->fd >= FD_SETSIZE)
698 { 694 {
699 GNUNET_break (0 == close (ret->fd)); 695 GNUNET_break (0 == close (ret->fd));
700 GNUNET_free (ret); 696 GNUNET_free (ret);
701 errno = EMFILE; 697 errno = EMFILE;
702 return NULL; 698 return NULL;
703 } 699 }
704 700
705#endif 701#endif
706 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) 702 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
707 { 703 {
708 /* we might want to treat this one as fatal... */ 704 /* we might want to treat this one as fatal... */
709 GNUNET_break (0); 705 GNUNET_break (0);
710 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); 706 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
711 return NULL; 707 return NULL;
712 } 708 }
713 709
714#ifndef MINGW 710#ifndef MINGW
715 if (GNUNET_OK != socket_set_inheritable (ret)) 711 if (GNUNET_OK != socket_set_inheritable (ret))
716 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 712 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
717 "socket_set_inheritable"); 713 "socket_set_inheritable");
718#endif 714#endif
719#ifdef DARWIN 715#ifdef DARWIN
720 socket_set_nosigpipe (ret); 716 socket_set_nosigpipe (ret);
@@ -723,7 +719,7 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
723#ifdef AF_UNIX 719#ifdef AF_UNIX
724 && (domain != AF_UNIX) 720 && (domain != AF_UNIX)
725#endif 721#endif
726 ) 722 )
727 socket_set_nodelay (ret); 723 socket_set_nodelay (ret);
728 return ret; 724 return ret;
729} 725}
@@ -762,18 +758,19 @@ int
762GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc) 758GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
763{ 759{
764 int ret = 0; 760 int ret = 0;
761
765#if WINDOWS 762#if WINDOWS
766 int value = 0; 763 int value = 0;
767 764
768 if (0 != 765 if (0 !=
769 (ret = 766 (ret =
770 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value, 767 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
771 sizeof (value)))) 768 sizeof (value))))
772 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 769 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
773 if (0 != 770 if (0 !=
774 (ret = 771 (ret =
775 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value, 772 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
776 sizeof (value)))) 773 sizeof (value))))
777 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 774 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
778#elif LINUX 775#elif LINUX
779 int value = 0; 776 int value = 0;
@@ -812,7 +809,7 @@ GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
812 */ 809 */
813void 810void
814GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, 811GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
815 const struct GNUNET_NETWORK_Handle *desc) 812 const struct GNUNET_NETWORK_Handle *desc)
816{ 813{
817 FD_SET (desc->fd, &fds->sds); 814 FD_SET (desc->fd, &fds->sds);
818 if (desc->fd + 1 > fds->nsds) 815 if (desc->fd + 1 > fds->nsds)
@@ -828,7 +825,7 @@ GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
828 */ 825 */
829int 826int
830GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, 827GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
831 const struct GNUNET_NETWORK_Handle *desc) 828 const struct GNUNET_NETWORK_Handle *desc)
832{ 829{
833 return FD_ISSET (desc->fd, &fds->sds); 830 return FD_ISSET (desc->fd, &fds->sds);
834} 831}
@@ -841,18 +838,18 @@ GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
841 */ 838 */
842void 839void
843GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, 840GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
844 const struct GNUNET_NETWORK_FDSet *src) 841 const struct GNUNET_NETWORK_FDSet *src)
845{ 842{
846 int nfds; 843 int nfds;
847 844
848 for (nfds = src->nsds; nfds > 0; nfds--) 845 for (nfds = src->nsds; nfds > 0; nfds--)
849 if (FD_ISSET (nfds, &src->sds)) 846 if (FD_ISSET (nfds, &src->sds))
850 847
851 { 848 {
852 FD_SET (nfds, &dst->sds); 849 FD_SET (nfds, &dst->sds);
853 if (nfds + 1 > dst->nsds) 850 if (nfds + 1 > dst->nsds)
854 dst->nsds = nfds + 1; 851 dst->nsds = nfds + 1;
855 } 852 }
856#ifdef MINGW 853#ifdef MINGW
857 GNUNET_CONTAINER_slist_append (dst->handles, src->handles); 854 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
858#endif 855#endif
@@ -867,7 +864,7 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
867 */ 864 */
868void 865void
869GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, 866GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
870 const struct GNUNET_NETWORK_FDSet *from) 867 const struct GNUNET_NETWORK_FDSet *from)
871{ 868{
872 FD_COPY (&from->sds, &to->sds); 869 FD_COPY (&from->sds, &to->sds);
873 to->nsds = from->nsds; 870 to->nsds = from->nsds;
@@ -901,7 +898,7 @@ GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
901 */ 898 */
902void 899void
903GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, 900GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
904 const fd_set * from, int nfds) 901 const fd_set * from, int nfds)
905{ 902{
906 FD_COPY (from, &to->sds); 903 FD_COPY (from, &to->sds);
907 to->nsds = nfds; 904 to->nsds = nfds;
@@ -932,7 +929,7 @@ GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
932 */ 929 */
933int 930int
934GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, 931GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
935 int nfd) 932 int nfd)
936{ 933{
937 if ((nfd == -1) || (to == NULL)) 934 if ((nfd == -1) || (to == NULL))
938 return GNUNET_NO; 935 return GNUNET_NO;
@@ -947,12 +944,12 @@ GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
947 */ 944 */
948void 945void
949GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, 946GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
950 const struct GNUNET_DISK_FileHandle *h) 947 const struct GNUNET_DISK_FileHandle *h)
951{ 948{
952#ifdef MINGW 949#ifdef MINGW
953 GNUNET_CONTAINER_slist_add (fds->handles, 950 GNUNET_CONTAINER_slist_add (fds->handles,
954 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h, 951 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
955 sizeof (struct GNUNET_DISK_FileHandle)); 952 sizeof (struct GNUNET_DISK_FileHandle));
956 953
957#else 954#else
958 int fd; 955 int fd;
@@ -974,13 +971,13 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
974 */ 971 */
975int 972int
976GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, 973GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
977 const struct GNUNET_DISK_FileHandle *h) 974 const struct GNUNET_DISK_FileHandle *h)
978{ 975{
979 976
980#ifdef MINGW 977#ifdef MINGW
981 return GNUNET_CONTAINER_slist_contains (fds->handles, h, 978 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
982 sizeof (struct 979 sizeof (struct
983 GNUNET_DISK_FileHandle)); 980 GNUNET_DISK_FileHandle));
984#else 981#else
985 return FD_ISSET (h->fd, &fds->sds); 982 return FD_ISSET (h->fd, &fds->sds);
986#endif 983#endif
@@ -995,7 +992,7 @@ GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
995 */ 992 */
996int 993int
997GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, 994GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
998 const struct GNUNET_NETWORK_FDSet *fds2) 995 const struct GNUNET_NETWORK_FDSet *fds2)
999{ 996{
1000#ifndef MINGW 997#ifndef MINGW
1001 int nfds; 998 int nfds;
@@ -1004,11 +1001,11 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1004 if (nfds > fds2->nsds) 1001 if (nfds > fds2->nsds)
1005 nfds = fds2->nsds; 1002 nfds = fds2->nsds;
1006 while (nfds > 0) 1003 while (nfds > 0)
1007 { 1004 {
1008 nfds--; 1005 nfds--;
1009 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds)) 1006 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1010 return GNUNET_YES; 1007 return GNUNET_YES;
1011 } 1008 }
1012#else 1009#else
1013 struct GNUNET_CONTAINER_SList_Iterator it; 1010 struct GNUNET_CONTAINER_SList_Iterator it;
1014 struct GNUNET_DISK_FileHandle *h; 1011 struct GNUNET_DISK_FileHandle *h;
@@ -1019,47 +1016,45 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1019 * inside of fd_set; also the O(n^2) is really bad... */ 1016 * inside of fd_set; also the O(n^2) is really bad... */
1020 1017
1021 for (i = 0; i < fds1->sds.fd_count; i++) 1018 for (i = 0; i < fds1->sds.fd_count; i++)
1019 {
1020 for (j = 0; j < fds2->sds.fd_count; j++)
1022 { 1021 {
1023 for (j = 0; j < fds2->sds.fd_count; j++) 1022 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1024 { 1023 return GNUNET_YES;
1025 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1026 return GNUNET_YES;
1027 }
1028 } 1024 }
1025 }
1029 it = GNUNET_CONTAINER_slist_begin (fds1->handles); 1026 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1030 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES) 1027 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES)
1031 { 1028 {
1032#if DEBUG_NETWORK 1029#if DEBUG_NETWORK
1033 struct GNUNET_CONTAINER_SList_Iterator t; 1030 struct GNUNET_CONTAINER_SList_Iterator t;
1034#endif 1031#endif
1035 h = 1032 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it,
1036 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it, 1033 NULL);
1037 NULL);
1038#if DEBUG_NETWORK 1034#if DEBUG_NETWORK
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, 1035 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking that FD 0x%x is in another set:\n",
1040 "Checking that FD 0x%x is in another set:\n", h->h); 1036 h->h);
1041 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles); 1037 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1042 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES; 1038 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1043 GNUNET_CONTAINER_slist_next (&t)) 1039 GNUNET_CONTAINER_slist_next (&t))
1044 { 1040 {
1045 struct GNUNET_DISK_FileHandle *fh; 1041 struct GNUNET_DISK_FileHandle *fh;
1046 1042
1047 fh = 1043 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1048 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t, 1044 NULL);
1049 NULL); 1045 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h); 1046 }
1051 } 1047#endif
1052#endif 1048 if (GNUNET_CONTAINER_slist_contains
1053 if (GNUNET_CONTAINER_slist_contains 1049 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1054 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle))) 1050 {
1055 {
1056#if DEBUG_NETWORK 1051#if DEBUG_NETWORK
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); 1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1058#endif 1053#endif
1059 return GNUNET_YES; 1054 return GNUNET_YES;
1060 }
1061 GNUNET_CONTAINER_slist_next (&it);
1062 } 1055 }
1056 GNUNET_CONTAINER_slist_next (&it);
1057 }
1063#endif 1058#endif
1064 return GNUNET_NO; 1059 return GNUNET_NO;
1065} 1060}
@@ -1106,9 +1101,9 @@ GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1106 */ 1101 */
1107int 1102int
1108GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, 1103GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1109 struct GNUNET_NETWORK_FDSet *wfds, 1104 struct GNUNET_NETWORK_FDSet *wfds,
1110 struct GNUNET_NETWORK_FDSet *efds, 1105 struct GNUNET_NETWORK_FDSet *efds,
1111 const struct GNUNET_TIME_Relative timeout) 1106 const struct GNUNET_TIME_Relative timeout)
1112{ 1107{
1113 int nfds = 0; 1108 int nfds = 0;
1114 1109
@@ -1138,8 +1133,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1138 DWORD newretcode = 0; 1133 DWORD newretcode = 0;
1139 int returnedpos = 0; 1134 int returnedpos = 0;
1140 1135
1141 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, 1136 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1142 *handles_except;
1143 1137
1144 fd_set aread, awrite, aexcept; 1138 fd_set aread, awrite, aexcept;
1145 1139
@@ -1153,68 +1147,67 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1153 struct timeval tv; 1147 struct timeval tv;
1154#endif 1148#endif
1155 if (NULL != rfds) 1149 if (NULL != rfds)
1156 { 1150 {
1157 nfds = rfds->nsds; 1151 nfds = rfds->nsds;
1158#ifdef MINGW 1152#ifdef MINGW
1159 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles); 1153 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1160#if DEBUG_NETWORK 1154#if DEBUG_NETWORK
1155 {
1156 struct GNUNET_CONTAINER_SList_Iterator t;
1157
1158 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1159 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1160 GNUNET_CONTAINER_slist_next (&t))
1161 { 1161 {
1162 struct GNUNET_CONTAINER_SList_Iterator t; 1162 struct GNUNET_DISK_FileHandle *fh;
1163 1163
1164 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); 1164 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1165 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES; 1165 NULL);
1166 GNUNET_CONTAINER_slist_next (&t)) 1166 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", fh->h,
1167 { 1167 fh);
1168 struct GNUNET_DISK_FileHandle *fh;
1169
1170 fh =
1171 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1172 NULL);
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n",
1174 fh->h, fh);
1175 }
1176 } 1168 }
1169 }
1177#endif 1170#endif
1178#endif 1171#endif
1179 } 1172 }
1180 if (NULL != wfds) 1173 if (NULL != wfds)
1181 { 1174 {
1182 nfds = GNUNET_MAX (nfds, wfds->nsds); 1175 nfds = GNUNET_MAX (nfds, wfds->nsds);
1183#ifdef MINGW 1176#ifdef MINGW
1184 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles); 1177 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1185#endif 1178#endif
1186 } 1179 }
1187 if (NULL != efds) 1180 if (NULL != efds)
1188 { 1181 {
1189 nfds = GNUNET_MAX (nfds, efds->nsds); 1182 nfds = GNUNET_MAX (nfds, efds->nsds);
1190#ifdef MINGW 1183#ifdef MINGW
1191 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles); 1184 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1192#endif 1185#endif
1193 } 1186 }
1194 1187
1195 if ((nfds == 0) && 1188 if ((nfds == 0) &&
1196 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) 1189 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1197#ifdef MINGW 1190#ifdef MINGW
1198 && handles == 0 1191 && handles == 0
1199#endif 1192#endif
1200 ) 1193 )
1201 { 1194 {
1202 LOG (GNUNET_ERROR_TYPE_ERROR, 1195 LOG (GNUNET_ERROR_TYPE_ERROR,
1203 _ 1196 _
1204 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 1197 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1205 "select"); 1198 "select");
1206 GNUNET_break (0); 1199 GNUNET_break (0);
1207 } 1200 }
1208#ifndef MINGW 1201#ifndef MINGW
1209 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value; 1202 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1210 tv.tv_usec = 1203 tv.tv_usec =
1211 1000 * (timeout.rel_value - 1204 1000 * (timeout.rel_value -
1212 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value)); 1205 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1213 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL, 1206 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1214 (wfds != NULL) ? &wfds->sds : NULL, 1207 (wfds != NULL) ? &wfds->sds : NULL,
1215 (efds != NULL) ? &efds->sds : NULL, 1208 (efds != NULL) ? &efds->sds : NULL,
1216 (timeout.rel_value == 1209 (timeout.rel_value ==
1217 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv); 1210 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1218 1211
1219#else 1212#else
1220#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) 1213#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
@@ -1225,10 +1218,10 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1225 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value; 1218 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1226 /* select() may be used as a portable way to sleep */ 1219 /* select() may be used as a portable way to sleep */
1227 if (!(rfds || wfds || efds)) 1220 if (!(rfds || wfds || efds))
1228 { 1221 {
1229 Sleep (ms_total); 1222 Sleep (ms_total);
1230 return 0; 1223 return 0;
1231 } 1224 }
1232 1225
1233 /* Events for sockets */ 1226 /* Events for sockets */
1234 if (!hEventRead) 1227 if (!hEventRead)
@@ -1264,194 +1257,184 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1264 FD_ZERO (&bexcept); 1257 FD_ZERO (&bexcept);
1265#endif 1258#endif
1266 if (rfds) 1259 if (rfds)
1267 { 1260 {
1268 FD_COPY (&rfds->sds, &aread); 1261 FD_COPY (&rfds->sds, &aread);
1269#if DEBUG_NETWORK 1262#if DEBUG_NETWORK
1270 FD_COPY (&rfds->sds, &bread); 1263 FD_COPY (&rfds->sds, &bread);
1271#endif 1264#endif
1272 } 1265 }
1273 if (wfds) 1266 if (wfds)
1274 { 1267 {
1275 FD_COPY (&wfds->sds, &awrite); 1268 FD_COPY (&wfds->sds, &awrite);
1276#if DEBUG_NETWORK 1269#if DEBUG_NETWORK
1277 FD_COPY (&wfds->sds, &bwrite); 1270 FD_COPY (&wfds->sds, &bwrite);
1278#endif 1271#endif
1279 } 1272 }
1280 if (efds) 1273 if (efds)
1281 { 1274 {
1282 FD_COPY (&efds->sds, &aexcept); 1275 FD_COPY (&efds->sds, &aexcept);
1283#if DEBUG_NETWORK 1276#if DEBUG_NETWORK
1284 FD_COPY (&efds->sds, &bexcept); 1277 FD_COPY (&efds->sds, &bexcept);
1285#endif 1278#endif
1286 } 1279 }
1287 /* We will first Add the PIPES to the events */ 1280 /* We will first Add the PIPES to the events */
1288 /* Read Pipes */ 1281 /* Read Pipes */
1289 if (rfds && read_handles) 1282 if (rfds && read_handles)
1283 {
1284 struct GNUNET_CONTAINER_SList_Iterator i;
1285
1286 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1287 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1288 GNUNET_CONTAINER_slist_next (&i))
1290 { 1289 {
1291 struct GNUNET_CONTAINER_SList_Iterator i; 1290 struct GNUNET_DISK_FileHandle *fh;
1292 1291
1293 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles); 1292 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1294 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES; 1293 NULL);
1295 GNUNET_CONTAINER_slist_next (&i)) 1294 if (fh->type == GNUNET_PIPE)
1296 { 1295 {
1297 struct GNUNET_DISK_FileHandle *fh; 1296 /* Read zero bytes to check the status of the pipe */
1298
1299 fh =
1300 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1301 NULL);
1302 if (fh->type == GNUNET_PIPE)
1303 {
1304 /* Read zero bytes to check the status of the pipe */
1305#if DEBUG_NETWORK 1297#if DEBUG_NETWORK
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, 1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
1307 "Reading 0 bytes from the pipe 0x%x\n", fh->h); 1299 fh->h);
1308#endif 1300#endif
1309 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead)) 1301 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1310 { 1302 {
1311 DWORD error_code = GetLastError (); 1303 DWORD error_code = GetLastError ();
1312 1304
1313 if (error_code == ERROR_IO_PENDING) 1305 if (error_code == ERROR_IO_PENDING)
1314 { 1306 {
1315#if DEBUG_NETWORK 1307#if DEBUG_NETWORK
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, 1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1317 "Adding the pipe's 0x%x overlapped event to the array as %d\n", 1309 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1318 fh->h, nhandles); 1310 fh->h, nhandles);
1319#endif 1311#endif
1320 handle_array[nhandles++] = fh->oOverlapRead->hEvent; 1312 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1321 readArray[readPipes++] = fh; 1313 readArray[readPipes++] = fh;
1322 } 1314 }
1323 /* 1315 /*
1324 * else 1316 * else
1325 * { 1317 * {
1326 * SetErrnoFromWinError (error_code); 1318 * SetErrnoFromWinError (error_code);
1327 * } 1319 * }
1328 */ 1320 */
1329 } 1321 }
1330 else 1322 else
1331 { 1323 {
1332#if DEBUG_NETWORK 1324#if DEBUG_NETWORK
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, 1325 LOG (GNUNET_ERROR_TYPE_DEBUG,
1334 "Adding the read ready event to the array as %d\n", 1326 "Adding the read ready event to the array as %d\n", nhandles);
1335 nhandles); 1327#endif
1336#endif 1328 handle_array[nhandles++] = hEventReadReady;
1337 handle_array[nhandles++] = hEventReadReady; 1329 readArray[readPipes++] = fh;
1338 readArray[readPipes++] = fh; 1330 }
1339 } 1331 }
1340 } 1332 else
1341 else 1333 {
1342 { 1334 GNUNET_CONTAINER_slist_add (handles_read,
1343 GNUNET_CONTAINER_slist_add (handles_read, 1335 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1344 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1336 fh, sizeof (struct GNUNET_DISK_FileHandle));
1345 fh, 1337 }
1346 sizeof (struct
1347 GNUNET_DISK_FileHandle));
1348 }
1349 }
1350 } 1338 }
1339 }
1351 if (wfds && write_handles) 1340 if (wfds && write_handles)
1352 { 1341 {
1353#if DEBUG_NETWORK 1342#if DEBUG_NETWORK
1354 LOG (GNUNET_ERROR_TYPE_DEBUG, 1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1355 "Adding the write ready event to the array as %d\n", nhandles); 1344 "Adding the write ready event to the array as %d\n", nhandles);
1356#endif 1345#endif
1357 handle_array[nhandles++] = hEventPipeWrite; 1346 handle_array[nhandles++] = hEventPipeWrite;
1358 writePipePos = nhandles; 1347 writePipePos = nhandles;
1359 } 1348 }
1360 if (efds && ex_handles) 1349 if (efds && ex_handles)
1350 {
1351 struct GNUNET_CONTAINER_SList_Iterator i;
1352
1353 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1354 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1355 GNUNET_CONTAINER_slist_next (&i))
1361 { 1356 {
1362 struct GNUNET_CONTAINER_SList_Iterator i; 1357 struct GNUNET_DISK_FileHandle *fh;
1363 1358 DWORD dwBytes;
1364 for (i = GNUNET_CONTAINER_slist_begin (efds->handles); 1359
1365 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES; 1360 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1366 GNUNET_CONTAINER_slist_next (&i)) 1361 NULL);
1367 { 1362 if (fh->type == GNUNET_PIPE)
1368 struct GNUNET_DISK_FileHandle *fh; 1363 {
1369 DWORD dwBytes; 1364 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1370 1365 {
1371 fh = 1366 GNUNET_CONTAINER_slist_add (handles_except,
1372 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i, 1367 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1373 NULL); 1368 fh,
1374 if (fh->type == GNUNET_PIPE) 1369 sizeof (struct GNUNET_DISK_FileHandle));
1375 { 1370 newretcode++;
1376 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL)) 1371 }
1377 { 1372 }
1378 GNUNET_CONTAINER_slist_add (handles_except,
1379 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1380 fh,
1381 sizeof (struct
1382 GNUNET_DISK_FileHandle));
1383 newretcode++;
1384 }
1385 }
1386 }
1387 } 1373 }
1374 }
1388 if (nfds > 0) 1375 if (nfds > 0)
1376 {
1377 if (rfds)
1389 { 1378 {
1390 if (rfds)
1391 {
1392#if DEBUG_NETWORK 1379#if DEBUG_NETWORK
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, 1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1394 "Adding the socket read event to the array as %d\n", nhandles); 1381 "Adding the socket read event to the array as %d\n", nhandles);
1395#endif 1382#endif
1396 handle_array[nhandles++] = hEventRead; 1383 handle_array[nhandles++] = hEventRead;
1397 nSockEvents++; 1384 nSockEvents++;
1398 for (i = 0; i < rfds->sds.fd_count; i++) 1385 for (i = 0; i < rfds->sds.fd_count; i++)
1399 { 1386 {
1400 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 1387 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1401 FD_ACCEPT | FD_READ | FD_CLOSE); 1388 FD_ACCEPT | FD_READ | FD_CLOSE);
1402 nsock++; 1389 nsock++;
1403 } 1390 }
1404 } 1391 }
1405 if (wfds) 1392 if (wfds)
1406 { 1393 {
1407 int wakeup = 0; 1394 int wakeup = 0;
1408 1395
1409#if DEBUG_NETWORK 1396#if DEBUG_NETWORK
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, 1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "Adding the socket write event to the array as %d\n", 1398 "Adding the socket write event to the array as %d\n", nhandles);
1412 nhandles); 1399#endif
1413#endif 1400 handle_array[nhandles++] = hEventWrite;
1414 handle_array[nhandles++] = hEventWrite; 1401 nSockEvents++;
1415 nSockEvents++; 1402 for (i = 0; i < wfds->sds.fd_count; i++)
1416 for (i = 0; i < wfds->sds.fd_count; i++) 1403 {
1417 { 1404 DWORD error;
1418 DWORD error; 1405 int status;
1419 int status; 1406
1420 1407 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1421 status = send (wfds->sds.fd_array[i], NULL, 0, 0); 1408 error = GetLastError ();
1422 error = GetLastError ();
1423#if DEBUG_NETWORK 1409#if DEBUG_NETWORK
1424 LOG (GNUNET_ERROR_TYPE_DEBUG, 1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1425 "pre-send to the socket %d returned %d (%u)\n", i, status, 1411 "pre-send to the socket %d returned %d (%u)\n", i, status, error);
1426 error); 1412#endif
1427#endif 1413 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1428 if (status == 0 1414 wakeup = 1;
1429 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)) 1415 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1430 wakeup = 1; 1416 FD_WRITE | FD_CONNECT | FD_CLOSE);
1431 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 1417 nsock++;
1432 FD_WRITE | FD_CONNECT | FD_CLOSE); 1418 }
1433 nsock++; 1419 if (wakeup)
1434 } 1420 SetEvent (hEventWrite);
1435 if (wakeup) 1421 }
1436 SetEvent (hEventWrite); 1422 if (efds)
1437 } 1423 {
1438 if (efds)
1439 {
1440#if DEBUG_NETWORK 1424#if DEBUG_NETWORK
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, 1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1442 "Adding the socket error event to the array as %d\n", 1426 "Adding the socket error event to the array as %d\n", nhandles);
1443 nhandles); 1427#endif
1444#endif 1428 handle_array[nhandles++] = hEventException;
1445 handle_array[nhandles++] = hEventException; 1429 nSockEvents++;
1446 nSockEvents++; 1430 for (i = 0; i < efds->sds.fd_count; i++)
1447 for (i = 0; i < efds->sds.fd_count; i++) 1431 {
1448 { 1432 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1449 WSAEventSelect (efds->sds.fd_array[i], hEventException, 1433 FD_OOB | FD_CLOSE);
1450 FD_OOB | FD_CLOSE); 1434 nsock++;
1451 nsock++; 1435 }
1452 }
1453 }
1454 } 1436 }
1437 }
1455 1438
1456 handle_array[nhandles] = NULL; 1439 handle_array[nhandles] = NULL;
1457 1440
@@ -1464,7 +1447,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1464 1447
1465 if (nhandles) 1448 if (nhandles)
1466 returncode = 1449 returncode =
1467 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total); 1450 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1468#if DEBUG_NETWORK 1451#if DEBUG_NETWORK
1469 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", 1452 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1470 returncode); 1453 returncode);
@@ -1481,239 +1464,230 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1481 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles); 1464 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1482 */ 1465 */
1483 if (nhandles && (returnedpos < nhandles)) 1466 if (nhandles && (returnedpos < nhandles))
1467 {
1468 DWORD waitstatus;
1469
1470 /* Do the select */
1471 if (nfds)
1484 { 1472 {
1485 DWORD waitstatus; 1473 struct timeval tvslice;
1486 1474
1487 /* Do the select */ 1475 tvslice.tv_sec = 0;
1488 if (nfds) 1476 tvslice.tv_usec = 10;
1489 { 1477 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1490 struct timeval tvslice; 1478 if (retcode == -1)
1491 1479 retcode = 0;
1492 tvslice.tv_sec = 0;
1493 tvslice.tv_usec = 10;
1494 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1495 if (retcode == -1)
1496 retcode = 0;
1497#if DEBUG_NETWORK 1480#if DEBUG_NETWORK
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode); 1481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1499#endif 1482#endif
1500 } 1483 }
1501 /* FIXME: <= writePipePos? Really? */ 1484 /* FIXME: <= writePipePos? Really? */
1502 if ((writePipePos != -1) && (returnedpos <= writePipePos)) 1485 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1503 { 1486 {
1504 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles); 1487 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1505 retcode += write_handles; 1488 retcode += write_handles;
1506#if DEBUG_NETWORK 1489#if DEBUG_NETWORK
1507 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); 1490 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1508#endif 1491#endif
1509 } 1492 }
1510#if DEBUG_NETWORK 1493#if DEBUG_NETWORK
1511 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes); 1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1512#endif 1495#endif
1513 /* We have some pipes ready for read. */ 1496 /* We have some pipes ready for read. */
1514 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */ 1497 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1515 1498
1516 if (returnedpos < readPipes) 1499 if (returnedpos < readPipes)
1517 { 1500 {
1518 /* 1501 /*
1519 * for (i = 0; i < readPipes; i++) 1502 * for (i = 0; i < readPipes; i++)
1520 * { 1503 * {
1521 * waitstatus = WaitForSingleObject (handle_array[i], 0); 1504 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1522 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus); 1505 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1523 * if (waitstatus != WAIT_OBJECT_0) 1506 * if (waitstatus != WAIT_OBJECT_0)
1524 * continue; 1507 * continue;
1525 * GNUNET_CONTAINER_slist_add (handles_read, 1508 * GNUNET_CONTAINER_slist_add (handles_read,
1526 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1509 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1527 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle)); 1510 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1528 * retcode++; 1511 * retcode++;
1529 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n"); 1512 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1530 * } 1513 * }
1531 */ 1514 */
1532 for (i = 0; i < readPipes; i++) 1515 for (i = 0; i < readPipes; i++)
1533 { 1516 {
1534 DWORD error; 1517 DWORD error;
1535 BOOL bret; 1518 BOOL bret;
1536 1519
1537 SetLastError (0); 1520 SetLastError (0);
1538 waitstatus = 0; 1521 waitstatus = 0;
1539 bret = 1522 bret =
1540 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, 1523 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1541 NULL); 1524 error = GetLastError ();
1542 error = GetLastError ();
1543#if DEBUG_NETWORK 1525#if DEBUG_NETWORK
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, 1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1545 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n", 1527 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1546 i, readArray[i]->h, bret, waitstatus, error); 1528 i, readArray[i]->h, bret, waitstatus, error);
1547#endif 1529#endif
1548 if (bret == 0) 1530 if (bret == 0)
1549 { 1531 {
1550 if (error != ERROR_BROKEN_PIPE) 1532 if (error != ERROR_BROKEN_PIPE)
1551 continue; 1533 continue;
1552 } 1534 }
1553 else if (waitstatus <= 0) 1535 else if (waitstatus <= 0)
1554 continue; 1536 continue;
1555 GNUNET_CONTAINER_slist_add (handles_read, 1537 GNUNET_CONTAINER_slist_add (handles_read,
1556 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1538 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1557 readArray[i], 1539 readArray[i],
1558 sizeof (struct 1540 sizeof (struct GNUNET_DISK_FileHandle));
1559 GNUNET_DISK_FileHandle)); 1541 retcode++;
1560 retcode++;
1561#if DEBUG_NETWORK 1542#if DEBUG_NETWORK
1562 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n", 1543 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1563 readArray[i], readArray[i]->h); 1544 readArray[i], readArray[i]->h);
1564#endif 1545#endif
1565 } 1546 }
1566 } 1547 }
1567 waitstatus = WaitForSingleObject (hEventWrite, 0); 1548 waitstatus = WaitForSingleObject (hEventWrite, 0);
1568#if DEBUG_NETWORK 1549#if DEBUG_NETWORK
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, 1550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n",
1570 "Wait for the write event returned %d\n", waitstatus); 1551 waitstatus);
1571#endif 1552#endif
1572 if (waitstatus == WAIT_OBJECT_0) 1553 if (waitstatus == WAIT_OBJECT_0)
1573 { 1554 {
1574 for (i = 0; i < wfds->sds.fd_count; i++) 1555 for (i = 0; i < wfds->sds.fd_count; i++)
1575 { 1556 {
1576 DWORD error; 1557 DWORD error;
1577 int status; 1558 int status;
1578 int so_error = 0; 1559 int so_error = 0;
1579 int sizeof_so_error = sizeof (so_error); 1560 int sizeof_so_error = sizeof (so_error);
1580 int gso_result = 1561 int gso_result =
1581 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR, 1562 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1582 (char *) &so_error, &sizeof_so_error); 1563 (char *) &so_error, &sizeof_so_error);
1583 1564
1584 status = send (wfds->sds.fd_array[i], NULL, 0, 0); 1565 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1585 error = GetLastError (); 1566 error = GetLastError ();
1586#if DEBUG_NETWORK 1567#if DEBUG_NETWORK
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, 1568 LOG (GNUNET_ERROR_TYPE_DEBUG,
1588 "send to the socket %d returned %d (%u)\n", i, status, 1569 "send to the socket %d returned %d (%u)\n", i, status, error);
1589 error); 1570#endif
1590#endif 1571 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
1591 if (status == 0 1572 (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
1592 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) 1573 so_error == WSAECONNREFUSED))
1593 || (status == -1 && gso_result == 0 && error == WSAENOTCONN 1574 {
1594 && so_error == WSAECONNREFUSED)) 1575 FD_SET (wfds->sds.fd_array[i], &awrite);
1595 { 1576 retcode += 1;
1596 FD_SET (wfds->sds.fd_array[i], &awrite); 1577 }
1597 retcode += 1; 1578 }
1598 }
1599 }
1600 }
1601 } 1579 }
1580 }
1602#if DEBUG_NETWORK 1581#if DEBUG_NETWORK
1603 if (!nhandles || (returnedpos >= nhandles)) 1582 if (!nhandles || (returnedpos >= nhandles))
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n"); 1583 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1605#endif 1584#endif
1606 if (rfds) 1585 if (rfds)
1586 {
1587 struct GNUNET_CONTAINER_SList_Iterator t;
1588
1589 for (i = 0; i < rfds->sds.fd_count; i++)
1607 { 1590 {
1608 struct GNUNET_CONTAINER_SList_Iterator t; 1591 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1592 nsock++;
1593 }
1594 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1595 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1596 GNUNET_CONTAINER_slist_next (&t))
1597 {
1598 struct GNUNET_DISK_FileHandle *fh;
1609 1599
1610 for (i = 0; i < rfds->sds.fd_count; i++) 1600 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1611 { 1601 NULL);
1612 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0); 1602 if (fh->type == GNUNET_PIPE)
1613 nsock++; 1603 {
1614 } 1604 CancelIo (fh->h);
1615 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles); 1605 }
1616 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES; 1606 }
1617 GNUNET_CONTAINER_slist_next (&t))
1618 {
1619 struct GNUNET_DISK_FileHandle *fh;
1620
1621 fh =
1622 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1623 NULL);
1624 if (fh->type == GNUNET_PIPE)
1625 {
1626 CancelIo (fh->h);
1627 }
1628 }
1629#if DEBUG_NETWORK 1607#if DEBUG_NETWORK
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n"); 1608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1631#endif 1609#endif
1632 GNUNET_NETWORK_fdset_zero (rfds); 1610 GNUNET_NETWORK_fdset_zero (rfds);
1633 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1611 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1634 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode); 1612 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1635 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read); 1613 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1636 } 1614 }
1637 if (wfds) 1615 if (wfds)
1616 {
1617 for (i = 0; i < wfds->sds.fd_count; i++)
1638 { 1618 {
1639 for (i = 0; i < wfds->sds.fd_count; i++) 1619 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1640 { 1620 nsock++;
1641 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0); 1621 }
1642 nsock++;
1643 }
1644#if DEBUG_NETWORK 1622#if DEBUG_NETWORK
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n"); 1623 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1646#endif 1624#endif
1647 GNUNET_NETWORK_fdset_zero (wfds); 1625 GNUNET_NETWORK_fdset_zero (wfds);
1648 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1626 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1649 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode); 1627 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1650 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write); 1628 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1651 } 1629 }
1652 if (efds) 1630 if (efds)
1631 {
1632 for (i = 0; i < efds->sds.fd_count; i++)
1653 { 1633 {
1654 for (i = 0; i < efds->sds.fd_count; i++) 1634 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1655 { 1635 nsock++;
1656 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0); 1636 }
1657 nsock++;
1658 }
1659#if DEBUG_NETWORK 1637#if DEBUG_NETWORK
1660 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n"); 1638 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1661#endif 1639#endif
1662 GNUNET_NETWORK_fdset_zero (efds); 1640 GNUNET_NETWORK_fdset_zero (efds);
1663 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1641 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1664 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode); 1642 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1665 GNUNET_CONTAINER_slist_append (efds->handles, handles_except); 1643 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1666 } 1644 }
1667 GNUNET_CONTAINER_slist_destroy (handles_read); 1645 GNUNET_CONTAINER_slist_destroy (handles_read);
1668 GNUNET_CONTAINER_slist_destroy (handles_write); 1646 GNUNET_CONTAINER_slist_destroy (handles_write);
1669 GNUNET_CONTAINER_slist_destroy (handles_except); 1647 GNUNET_CONTAINER_slist_destroy (handles_except);
1670#if DEBUG_NETWORK 1648#if DEBUG_NETWORK
1671 if (rfds) 1649 if (rfds)
1650 {
1651 struct GNUNET_CONTAINER_SList_Iterator t;
1652
1653 for (i = 0; i < bread.fd_count; i++)
1672 { 1654 {
1673 struct GNUNET_CONTAINER_SList_Iterator t; 1655 if (bread.fd_array[i] != 0)
1656 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1657 bread.fd_array[i],
1658 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" : "NOT SET");
1659 }
1660 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1661 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1662 GNUNET_CONTAINER_slist_next (&t))
1663 {
1664 struct GNUNET_DISK_FileHandle *fh;
1674 1665
1675 for (i = 0; i < bread.fd_count; i++) 1666 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1676 { 1667 NULL);
1677 if (bread.fd_array[i] != 0) 1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1678 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1679 bread.fd_array[i],
1680 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1681 "NOT SET");
1682 }
1683 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1684 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1685 GNUNET_CONTAINER_slist_next (&t))
1686 {
1687 struct GNUNET_DISK_FileHandle *fh;
1688
1689 fh =
1690 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1691 NULL);
1692 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1693 }
1694 } 1669 }
1670 }
1695 if (wfds) 1671 if (wfds)
1672 {
1673 for (i = 0; i < bwrite.fd_count; i++)
1696 { 1674 {
1697 for (i = 0; i < bwrite.fd_count; i++) 1675 if (bwrite.fd_array[i] != 0)
1698 { 1676 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1699 if (bwrite.fd_array[i] != 0) 1677 bwrite.fd_array[i],
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n", 1678 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : "NOT SET");
1701 bwrite.fd_array[i],
1702 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" :
1703 "NOT SET");
1704 }
1705 } 1679 }
1680 }
1706 if (efds) 1681 if (efds)
1682 {
1683 for (i = 0; i < bexcept.fd_count; i++)
1707 { 1684 {
1708 for (i = 0; i < bexcept.fd_count; i++) 1685 if (bexcept.fd_array[i] != 0)
1709 { 1686 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1710 if (bexcept.fd_array[i] != 0) 1687 bexcept.fd_array[i],
1711 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n", 1688 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : "NOT SET");
1712 bexcept.fd_array[i],
1713 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" :
1714 "NOT SET");
1715 }
1716 } 1689 }
1690 }
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode); 1691 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1718#endif 1692#endif
1719 if (nhandles && (returnedpos < nhandles)) 1693 if (nhandles && (returnedpos < nhandles))
diff --git a/src/util/os_installation.c b/src/util/os_installation.c
index 98080a3ed..a7a6ae915 100644
--- a/src/util/os_installation.c
+++ b/src/util/os_installation.c
@@ -60,16 +60,16 @@ get_path_from_proc_maps ()
60 if (f == NULL) 60 if (f == NULL)
61 return NULL; 61 return NULL;
62 while (NULL != fgets (line, sizeof (line), f)) 62 while (NULL != fgets (line, sizeof (line), f))
63 {
64 if ((1 ==
65 sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) &&
66 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
63 { 67 {
64 if ((1 == 68 lgu[0] = '\0';
65 sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", 69 fclose (f);
66 dir)) && (NULL != (lgu = strstr (dir, "libgnunetutil")))) 70 return GNUNET_strdup (dir);
67 {
68 lgu[0] = '\0';
69 fclose (f);
70 return GNUNET_strdup (dir);
71 }
72 } 71 }
72 }
73 fclose (f); 73 fclose (f);
74 return NULL; 74 return NULL;
75} 75}
@@ -87,19 +87,19 @@ get_path_from_proc_exe ()
87 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ()); 87 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
88 size = readlink (fn, lnk, sizeof (lnk) - 1); 88 size = readlink (fn, lnk, sizeof (lnk) - 1);
89 if (size <= 0) 89 if (size <= 0)
90 { 90 {
91 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); 91 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
92 return NULL; 92 return NULL;
93 } 93 }
94 GNUNET_assert (size < sizeof (lnk)); 94 GNUNET_assert (size < sizeof (lnk));
95 lnk[size] = '\0'; 95 lnk[size] = '\0';
96 while ((lnk[size] != '/') && (size > 0)) 96 while ((lnk[size] != '/') && (size > 0))
97 size--; 97 size--;
98 if ((size < 4) || (lnk[size - 4] != '/')) 98 if ((size < 4) || (lnk[size - 4] != '/'))
99 { 99 {
100 /* not installed in "/bin/" -- binary path probably useless */ 100 /* not installed in "/bin/" -- binary path probably useless */
101 return NULL; 101 return NULL;
102 } 102 }
103 lnk[size] = '\0'; 103 lnk[size] = '\0';
104 return GNUNET_strdup (lnk); 104 return GNUNET_strdup (lnk);
105} 105}
@@ -138,7 +138,7 @@ get_path_from_NSGetExecutablePath ()
138 138
139 path = NULL; 139 path = NULL;
140 func = 140 func =
141 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); 141 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
142 if (!func) 142 if (!func)
143 return NULL; 143 return NULL;
144 path = &zero; 144 path = &zero;
@@ -150,10 +150,10 @@ get_path_from_NSGetExecutablePath ()
150 path = GNUNET_malloc (len); 150 path = GNUNET_malloc (len);
151 ret = func (path, &len); 151 ret = func (path, &len);
152 if (ret != 0) 152 if (ret != 0)
153 { 153 {
154 GNUNET_free (path); 154 GNUNET_free (path);
155 return NULL; 155 return NULL;
156 } 156 }
157 len = strlen (path); 157 len = strlen (path);
158 while ((path[len] != '/') && (len > 0)) 158 while ((path[len] != '/') && (len > 0))
159 len--; 159 len--;
@@ -172,22 +172,22 @@ get_path_from_dyld_image ()
172 p = NULL; 172 p = NULL;
173 c = _dyld_image_count (); 173 c = _dyld_image_count ();
174 for (i = 0; i < c; i++) 174 for (i = 0; i < c; i++)
175 {
176 if (_dyld_get_image_header (i) == &_mh_dylib_header)
175 { 177 {
176 if (_dyld_get_image_header (i) == &_mh_dylib_header) 178 path = _dyld_get_image_name (i);
177 { 179 if (path != NULL && strlen (path) > 0)
178 path = _dyld_get_image_name (i); 180 {
179 if (path != NULL && strlen (path) > 0) 181 p = GNUNET_strdup (path);
180 { 182 s = p + strlen (p);
181 p = GNUNET_strdup (path); 183 while ((s > p) && (*s != '/'))
182 s = p + strlen (p); 184 s--;
183 while ((s > p) && (*s != '/')) 185 s++;
184 s--; 186 *s = '\0';
185 s++; 187 }
186 *s = '\0'; 188 break;
187 }
188 break;
189 }
190 } 189 }
190 }
191 return p; 191 return p;
192} 192}
193#endif 193#endif
@@ -211,30 +211,30 @@ get_path_from_PATH (const char *binary)
211 p = getenv ("PATH"); 211 p = getenv ("PATH");
212 if (p == NULL) 212 if (p == NULL)
213 return NULL; 213 return NULL;
214 path = GNUNET_strdup (p); /* because we write on it */ 214 path = GNUNET_strdup (p); /* because we write on it */
215 buf = GNUNET_malloc (strlen (path) + 20); 215 buf = GNUNET_malloc (strlen (path) + 20);
216 pos = path; 216 pos = path;
217 while (NULL != (end = strchr (pos, PATH_SEPARATOR))) 217 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
218 { 218 {
219 *end = '\0'; 219 *end = '\0';
220 sprintf (buf, "%s/%s", pos, binary); 220 sprintf (buf, "%s/%s", pos, binary);
221 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)
232 { 222 {
233 pos = GNUNET_strdup (pos); 223 pos = GNUNET_strdup (pos);
234 GNUNET_free (buf); 224 GNUNET_free (buf);
235 GNUNET_free (path); 225 GNUNET_free (path);
236 return pos; 226 return pos;
237 } 227 }
228 pos = end + 1;
229 }
230 sprintf (buf, "%s/%s", pos, binary);
231 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
232 {
233 pos = GNUNET_strdup (pos);
234 GNUNET_free (buf);
235 GNUNET_free (path);
236 return pos;
237 }
238 GNUNET_free (buf); 238 GNUNET_free (buf);
239 GNUNET_free (path); 239 GNUNET_free (path);
240 return NULL; 240 return NULL;
@@ -359,11 +359,11 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
359 359
360 n = strlen (execpath); 360 n = strlen (execpath);
361 if (n == 0) 361 if (n == 0)
362 { 362 {
363 /* should never happen, but better safe than sorry */ 363 /* should never happen, but better safe than sorry */
364 GNUNET_free (execpath); 364 GNUNET_free (execpath);
365 return NULL; 365 return NULL;
366 } 366 }
367 /* remove filename itself */ 367 /* remove filename itself */
368 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) 368 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
369 execpath[--n] = '\0'; 369 execpath[--n] = '\0';
@@ -372,62 +372,59 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
372 if ((n > 5) && 372 if ((n > 5) &&
373 ((0 == strcasecmp (&execpath[n - 5], "lib32")) || 373 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
374 (0 == strcasecmp (&execpath[n - 5], "lib64")))) 374 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
375 {
376 if (dirkind != GNUNET_OS_IPK_LIBDIR)
375 { 377 {
376 if (dirkind != GNUNET_OS_IPK_LIBDIR) 378 /* strip '/lib32' or '/lib64' */
377 { 379 execpath[n - 5] = '\0';
378 /* strip '/lib32' or '/lib64' */ 380 n -= 5;
379 execpath[n - 5] = '\0';
380 n -= 5;
381 }
382 else
383 isbasedir = 0;
384 } 381 }
382 else
383 isbasedir = 0;
384 }
385 else if ((n > 3) && 385 else if ((n > 3) &&
386 ((0 == strcasecmp (&execpath[n - 3], "bin")) || 386 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
387 (0 == strcasecmp (&execpath[n - 3], "lib")))) 387 (0 == strcasecmp (&execpath[n - 3], "lib"))))
388 { 388 {
389 /* strip '/bin' or '/lib' */ 389 /* strip '/bin' or '/lib' */
390 execpath[n - 3] = '\0'; 390 execpath[n - 3] = '\0';
391 n -= 3; 391 n -= 3;
392 } 392 }
393 /* in case this was a directory named foo-bin, remove "foo-" */ 393 /* in case this was a directory named foo-bin, remove "foo-" */
394 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) 394 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
395 execpath[--n] = '\0'; 395 execpath[--n] = '\0';
396 switch (dirkind) 396 switch (dirkind)
397 { 397 {
398 case GNUNET_OS_IPK_PREFIX: 398 case GNUNET_OS_IPK_PREFIX:
399 case GNUNET_OS_IPK_SELF_PREFIX: 399 case GNUNET_OS_IPK_SELF_PREFIX:
400 dirname = DIR_SEPARATOR_STR; 400 dirname = DIR_SEPARATOR_STR;
401 break; 401 break;
402 case GNUNET_OS_IPK_BINDIR: 402 case GNUNET_OS_IPK_BINDIR:
403 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; 403 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
404 break; 404 break;
405 case GNUNET_OS_IPK_LIBDIR: 405 case GNUNET_OS_IPK_LIBDIR:
406 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:
414 dirname =
415 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
416 DIR_SEPARATOR_STR;
417 break;
418 case GNUNET_OS_IPK_LOCALEDIR:
419 dirname =
420 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
421 DIR_SEPARATOR_STR;
422 break;
423 case GNUNET_OS_IPK_ICONDIR:
424 dirname = 407 dirname =
425 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; 408 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
426 break; 409 else
427 default: 410 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
428 GNUNET_free (execpath); 411 break;
429 return NULL; 412 case GNUNET_OS_IPK_DATADIR:
430 } 413 dirname =
414 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
415 break;
416 case GNUNET_OS_IPK_LOCALEDIR:
417 dirname =
418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
419 break;
420 case GNUNET_OS_IPK_ICONDIR:
421 dirname =
422 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
423 break;
424 default:
425 GNUNET_free (execpath);
426 return NULL;
427 }
431 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); 428 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
432 sprintf (tmp, "%s%s", execpath, dirname); 429 sprintf (tmp, "%s%s", execpath, dirname);
433 GNUNET_free (execpath); 430 GNUNET_free (execpath);
@@ -460,59 +457,58 @@ GNUNET_OS_check_helper_binary (const char *binary)
460 GNUNET_asprintf (&binaryexe, "%s.exe", binary); 457 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
461 p = get_path_from_PATH (binaryexe); 458 p = get_path_from_PATH (binaryexe);
462 if (p != NULL) 459 if (p != NULL)
463 { 460 {
464 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe); 461 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
465 GNUNET_free (p); 462 GNUNET_free (p);
466 p = pf; 463 p = pf;
467 } 464 }
468 free (binaryexe); 465 free (binaryexe);
469#else 466#else
470 p = get_path_from_PATH (binary); 467 p = get_path_from_PATH (binary);
471 if (p != NULL) 468 if (p != NULL)
472 { 469 {
473 GNUNET_asprintf (&pf, "%s/%s", p, binary); 470 GNUNET_asprintf (&pf, "%s/%s", p, binary);
474 GNUNET_free (p); 471 GNUNET_free (p);
475 p = pf; 472 p = pf;
476 } 473 }
477#endif 474#endif
478 if (p == NULL) 475 if (p == NULL)
479 { 476 {
480 LOG (GNUNET_ERROR_TYPE_INFO, 477 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
481 _("Could not find binary `%s' in PATH!\n"), binary); 478 binary);
482 return GNUNET_SYSERR; 479 return GNUNET_SYSERR;
483 } 480 }
484 if (0 != STAT (p, &statbuf)) 481 if (0 != STAT (p, &statbuf))
485 { 482 {
486 LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p, 483 LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
487 STRERROR (errno)); 484 STRERROR (errno));
488 GNUNET_free (p); 485 GNUNET_free (p);
489 return GNUNET_SYSERR; 486 return GNUNET_SYSERR;
490 } 487 }
491#ifndef MINGW 488#ifndef MINGW
492 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0)) 489 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
493 { 490 {
494 GNUNET_free (p); 491 GNUNET_free (p);
495 return GNUNET_YES; 492 return GNUNET_YES;
496 } 493 }
497 if (0 == ACCESS (p, X_OK)) 494 if (0 == ACCESS (p, X_OK))
498 { 495 {
499 GNUNET_free (p); 496 GNUNET_free (p);
500 return GNUNET_NO; 497 return GNUNET_NO;
501 } 498 }
502 GNUNET_free (p); 499 GNUNET_free (p);
503 return GNUNET_SYSERR; 500 return GNUNET_SYSERR;
504#else 501#else
505 GNUNET_free (p); 502 GNUNET_free (p);
506 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); 503 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
507 if (INVALID_SOCKET == rawsock) 504 if (INVALID_SOCKET == rawsock)
508 { 505 {
509 DWORD err = GetLastError (); 506 DWORD err = GetLastError ();
510 507
511 LOG (GNUNET_ERROR_TYPE_INFO, 508 LOG (GNUNET_ERROR_TYPE_INFO,
512 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", 509 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
513 err); 510 return GNUNET_NO; /* not running as administrator */
514 return GNUNET_NO; /* not running as administrator */ 511 }
515 }
516 closesocket (rawsock); 512 closesocket (rawsock);
517 return GNUNET_YES; 513 return GNUNET_YES;
518#endif 514#endif
diff --git a/src/util/os_network.c b/src/util/os_network.c
index 2835e6193..70d143ba1 100644
--- a/src/util/os_network.c
+++ b/src/util/os_network.c
@@ -43,7 +43,7 @@
43 */ 43 */
44void 44void
45GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, 45GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
46 void *proc_cls) 46 void *proc_cls)
47{ 47{
48#ifdef MINGW 48#ifdef MINGW
49 PMIB_IFTABLE pTable; 49 PMIB_IFTABLE pTable;
@@ -52,121 +52,118 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
52 IPAddr theIP; 52 IPAddr theIP;
53 53
54 /* Determine our external NIC */ 54 /* Determine our external NIC */
55 theIP = inet_addr ("192.0.34.166"); /* www.example.com */ 55 theIP = inet_addr ("192.0.34.166"); /* www.example.com */
56 if ((!GNGetBestInterface) || 56 if ((!GNGetBestInterface) ||
57 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR)) 57 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR))
58 { 58 {
59 dwExternalNIC = 0; 59 dwExternalNIC = 0;
60 } 60 }
61 61
62 /* Enumerate NICs */ 62 /* Enumerate NICs */
63 EnumNICs (&pTable, &pAddrTable); 63 EnumNICs (&pTable, &pAddrTable);
64 64
65 if (pTable) 65 if (pTable)
66 {
67 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
66 { 68 {
67 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++) 69 char szEntry[1001];
68 { 70 DWORD dwIP = 0;
69 char szEntry[1001]; 71 PIP_ADAPTER_INFO pAdapterInfo;
70 DWORD dwIP = 0; 72 PIP_ADAPTER_INFO pAdapter = NULL;
71 PIP_ADAPTER_INFO pAdapterInfo; 73 DWORD dwRetVal = 0;
72 PIP_ADAPTER_INFO pAdapter = NULL; 74
73 DWORD dwRetVal = 0; 75 /* Get IP-Address */
74 76 int i;
75 /* Get IP-Address */ 77
76 int i; 78 for (i = 0; i < pAddrTable->dwNumEntries; i++)
77 79 {
78 for (i = 0; i < pAddrTable->dwNumEntries; i++) 80 if (pAddrTable->table[i].dwIndex == pTable->table[dwIfIdx].dwIndex)
79 { 81 {
80 if (pAddrTable->table[i].dwIndex == 82 dwIP = pAddrTable->table[i].dwAddr;
81 pTable->table[dwIfIdx].dwIndex) 83 break;
82 { 84 }
83 dwIP = pAddrTable->table[i].dwAddr; 85 }
84 break; 86
85 } 87 if (dwIP)
86 } 88 {
87 89 BYTE bPhysAddr[MAXLEN_PHYSADDR];
88 if (dwIP) 90 char *pszIfName = NULL;
89 { 91 char dst[INET_ADDRSTRLEN];
90 BYTE bPhysAddr[MAXLEN_PHYSADDR]; 92 struct sockaddr_in sa;
91 char *pszIfName = NULL; 93
92 char dst[INET_ADDRSTRLEN]; 94 /* Get friendly interface name */
93 struct sockaddr_in sa; 95 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
94 96 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
95 /* Get friendly interface name */ 97
96 pAdapterInfo = 98 /* Make an initial call to GetAdaptersInfo to get
97 (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO)); 99 * the necessary size into the ulOutBufLen variable */
98 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); 100 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
99 101 ERROR_BUFFER_OVERFLOW)
100 /* Make an initial call to GetAdaptersInfo to get 102 {
101 * the necessary size into the ulOutBufLen variable */ 103 free (pAdapterInfo);
102 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) == 104 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
103 ERROR_BUFFER_OVERFLOW) 105 }
104 { 106
105 free (pAdapterInfo); 107 if ((dwRetVal =
106 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); 108 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
107 } 109 {
108 110 pAdapter = pAdapterInfo;
109 if ((dwRetVal = 111 while (pAdapter)
110 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR) 112 {
111 { 113 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
112 pAdapter = pAdapterInfo; 114 {
113 while (pAdapter) 115 char szKey[251];
114 { 116 long lLen = 250;
115 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index) 117
116 { 118 sprintf (szKey,
117 char szKey[251]; 119 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
118 long lLen = 250; 120 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
119 121 pAdapter->AdapterName);
120 sprintf (szKey, 122 pszIfName = (char *) malloc (251);
121 "SYSTEM\\CurrentControlSet\\Control\\Network\\" 123 if (QueryRegistry
122 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection", 124 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
123 pAdapter->AdapterName); 125 &lLen) != ERROR_SUCCESS)
124 pszIfName = (char *) malloc (251); 126 {
125 if (QueryRegistry 127 free (pszIfName);
126 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName, 128 pszIfName = NULL;
127 &lLen) != ERROR_SUCCESS) 129 }
128 { 130 }
129 free (pszIfName); 131 pAdapter = pAdapter->Next;
130 pszIfName = NULL; 132 }
131 } 133 }
132 } 134 free (pAdapterInfo);
133 pAdapter = pAdapter->Next; 135
134 } 136 /* Set entry */
135 } 137 memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
136 free (pAdapterInfo); 138 memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr,
137 139 pTable->table[dwIfIdx].dwPhysAddrLen);
138 /* Set entry */ 140
139 memset (bPhysAddr, 0, MAXLEN_PHYSADDR); 141 snprintf (szEntry, 1000, "%s (%s - %I64u)",
140 memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr, 142 pszIfName ? pszIfName : (char *) pTable->
141 pTable->table[dwIfIdx].dwPhysAddrLen); 143 table[dwIfIdx].bDescr, inet_ntop (AF_INET, &dwIP, dst,
142 144 INET_ADDRSTRLEN),
143 snprintf (szEntry, 1000, "%s (%s - %I64u)", 145 *((unsigned long long *) bPhysAddr));
144 pszIfName ? pszIfName : (char *) 146 szEntry[1000] = 0;
145 pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET, 147
146 &dwIP, dst, 148 if (pszIfName)
147 INET_ADDRSTRLEN), 149 free (pszIfName);
148 *((unsigned long long *) bPhysAddr)); 150
149 szEntry[1000] = 0; 151 sa.sin_family = AF_INET;
150
151 if (pszIfName)
152 free (pszIfName);
153
154 sa.sin_family = AF_INET;
155#if HAVE_SOCKADDR_IN_SIN_LEN 152#if HAVE_SOCKADDR_IN_SIN_LEN
156 sa.sin_len = (u_char) sizeof (struct sockaddr_in); 153 sa.sin_len = (u_char) sizeof (struct sockaddr_in);
157#endif 154#endif
158 sa.sin_addr.S_un.S_addr = dwIP; 155 sa.sin_addr.S_un.S_addr = dwIP;
159 156
160 if (GNUNET_OK != 157 if (GNUNET_OK !=
161 proc (proc_cls, szEntry, 158 proc (proc_cls, szEntry,
162 pTable->table[dwIfIdx].dwIndex == dwExternalNIC, 159 pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
163 (const struct sockaddr *) &sa, sizeof (sa))) 160 (const struct sockaddr *) &sa, sizeof (sa)))
164 break; 161 break;
165 } 162 }
166 }
167 GlobalFree (pAddrTable);
168 GlobalFree (pTable);
169 } 163 }
164 GlobalFree (pAddrTable);
165 GlobalFree (pTable);
166 }
170 167
171 return; 168 return;
172 169
@@ -177,29 +174,28 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
177 socklen_t alen; 174 socklen_t alen;
178 175
179 if (getifaddrs (&ifa_first) == 0) 176 if (getifaddrs (&ifa_first) == 0)
177 {
178 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
180 { 179 {
181 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) 180 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
182 { 181 (ifa_ptr->ifa_flags & IFF_UP) != 0)
183 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL && 182 {
184 (ifa_ptr->ifa_flags & IFF_UP) != 0) 183 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
185 { 184 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
186 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) && 185 continue;
187 (ifa_ptr->ifa_addr->sa_family != AF_INET6)) 186 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
188 continue; 187 alen = sizeof (struct sockaddr_in);
189 if (ifa_ptr->ifa_addr->sa_family == AF_INET) 188 else
190 alen = sizeof (struct sockaddr_in); 189 alen = sizeof (struct sockaddr_in6);
191 else 190 if (GNUNET_OK !=
192 alen = sizeof (struct sockaddr_in6); 191 proc (proc_cls, ifa_ptr->ifa_name,
193 if (GNUNET_OK != 192 0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE),
194 proc (proc_cls, ifa_ptr->ifa_name, 193 ifa_ptr->ifa_addr, alen))
195 0 == strcmp (ifa_ptr->ifa_name, 194 break;
196 GNUNET_DEFAULT_INTERFACE), 195 }
197 ifa_ptr->ifa_addr, alen))
198 break;
199 }
200 }
201 freeifaddrs (ifa_first);
202 } 196 }
197 freeifaddrs (ifa_first);
198 }
203#else 199#else
204 char line[1024]; 200 char line[1024];
205 const char *start; 201 const char *start;
@@ -220,76 +216,73 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
220 else 216 else
221 f = popen ("ifconfig -a 2> /dev/null", "r"); 217 f = popen ("ifconfig -a 2> /dev/null", "r");
222 if (!f) 218 if (!f)
223 { 219 {
224 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | 220 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
225 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig"); 221 "popen", "ifconfig");
226 return; 222 return;
227 } 223 }
228 224
229 have_ifc = GNUNET_NO; 225 have_ifc = GNUNET_NO;
230 ifc[11] = '\0'; 226 ifc[11] = '\0';
231 while (NULL != fgets (line, sizeof (line), f)) 227 while (NULL != fgets (line, sizeof (line), f))
228 {
229 if (strlen (line) == 0)
230 {
231 have_ifc = GNUNET_NO;
232 continue;
233 }
234 if (!isspace (line[0]))
235 {
236 have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
237 /* would end with ':' on OSX, fix it! */
238 if (ifc[strlen (ifc) - 1] == ':')
239 ifc[strlen (ifc) - 1] = '\0';
240 continue;
241 }
242 if (!have_ifc)
243 continue; /* strange input, hope for the best */
244 start = line;
245 while (('\0' != *start) && (isspace (*start)))
246 start++;
247 if ( /* Linux */
248 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
249 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
250 /* Solaris, OS X */
251 (1 == SSCANF (start, "inet %127s", addrstr)) ||
252 (1 == SSCANF (start, "inet6 %127s", addrstr)))
232 { 253 {
233 if (strlen (line) == 0) 254 /* IPv4 */
234 { 255 if (1 == inet_pton (AF_INET, addrstr, &v4))
235 have_ifc = GNUNET_NO; 256 {
236 continue; 257 memset (&a4, 0, sizeof (a4));
237 } 258 a4.sin_family = AF_INET;
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;
264#if HAVE_SOCKADDR_IN_SIN_LEN 259#if HAVE_SOCKADDR_IN_SIN_LEN
265 a4.sin_len = (u_char) sizeof (struct sockaddr_in); 260 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
266#endif 261#endif
267 a4.sin_addr = v4; 262 a4.sin_addr = v4;
268 if (GNUNET_OK != 263 if (GNUNET_OK !=
269 proc (proc_cls, ifc, 264 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
270 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), 265 (const struct sockaddr *) &a4, sizeof (a4)))
271 (const struct sockaddr *) &a4, sizeof (a4))) 266 break;
272 break; 267 continue;
273 continue; 268 }
274 } 269 /* IPv6 */
275 /* IPv6 */ 270 if (1 == inet_pton (AF_INET6, addrstr, &v6))
276 if (1 == inet_pton (AF_INET6, addrstr, &v6)) 271 {
277 { 272 memset (&a6, 0, sizeof (a6));
278 memset (&a6, 0, sizeof (a6)); 273 a6.sin6_family = AF_INET6;
279 a6.sin6_family = AF_INET6;
280#if HAVE_SOCKADDR_IN_SIN_LEN 274#if HAVE_SOCKADDR_IN_SIN_LEN
281 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); 275 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
282#endif 276#endif
283 a6.sin6_addr = v6; 277 a6.sin6_addr = v6;
284 if (GNUNET_OK != 278 if (GNUNET_OK !=
285 proc (proc_cls, ifc, 279 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
286 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), 280 (const struct sockaddr *) &a6, sizeof (a6)))
287 (const struct sockaddr *) &a6, sizeof (a6))) 281 break;
288 break; 282 continue;
289 continue; 283 }
290 }
291 }
292 } 284 }
285 }
293 pclose (f); 286 pclose (f);
294#endif 287#endif
295} 288}
diff --git a/src/util/os_priority.c b/src/util/os_priority.c
index 4f21e7edb..a7ce0c5e5 100644
--- a/src/util/os_priority.c
+++ b/src/util/os_priority.c
@@ -59,43 +59,41 @@ static struct GNUNET_OS_Process current_process;
59 */ 59 */
60static void 60static void
61parent_control_handler (void *cls, 61parent_control_handler (void *cls,
62 const struct GNUNET_SCHEDULER_TaskContext *tc) 62 const struct GNUNET_SCHEDULER_TaskContext *tc)
63{ 63{
64 struct GNUNET_DISK_FileHandle *control_pipe = 64 struct GNUNET_DISK_FileHandle *control_pipe =
65 (struct GNUNET_DISK_FileHandle *) cls; 65 (struct GNUNET_DISK_FileHandle *) cls;
66 int sig; 66 int sig;
67 67
68#if DEBUG_OS 68#if DEBUG_OS
69 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", 69 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", __FUNCTION__,
70 __FUNCTION__, tc->reason); 70 tc->reason);
71#endif 71#endif
72 if (tc->reason & 72 if (tc->reason &
73 (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT | 73 (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT |
74 GNUNET_SCHEDULER_REASON_PREREQ_DONE)) 74 GNUNET_SCHEDULER_REASON_PREREQ_DONE))
75 {
76 GNUNET_DISK_npipe_close (control_pipe);
77 }
78 else
79 {
80 if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) !=
81 sizeof (sig))
75 { 82 {
83 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
76 GNUNET_DISK_npipe_close (control_pipe); 84 GNUNET_DISK_npipe_close (control_pipe);
77 } 85 }
78 else 86 else
79 { 87 {
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 {
88#if DEBUG_OS 88#if DEBUG_OS
89 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", sig);
90 sig);
91#endif 90#endif
92 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 91 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
93 control_pipe, 92 control_pipe, &parent_control_handler,
94 &parent_control_handler, 93 control_pipe);
95 control_pipe); 94 raise (sig);
96 raise (sig);
97 }
98 } 95 }
96 }
99} 97}
100 98
101 99
@@ -104,35 +102,34 @@ parent_control_handler (void *cls,
104 */ 102 */
105void 103void
106GNUNET_OS_install_parent_control_handler (void *cls, 104GNUNET_OS_install_parent_control_handler (void *cls,
107 const struct 105 const struct
108 GNUNET_SCHEDULER_TaskContext *tc) 106 GNUNET_SCHEDULER_TaskContext *tc)
109{ 107{
110 const char *env_buf; 108 const char *env_buf;
111 struct GNUNET_DISK_FileHandle *control_pipe; 109 struct GNUNET_DISK_FileHandle *control_pipe;
112 110
113 env_buf = getenv (GNUNET_OS_CONTROL_PIPE); 111 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
114 if ((env_buf == NULL) || (strlen (env_buf) <= 0)) 112 if ((env_buf == NULL) || (strlen (env_buf) <= 0))
115 { 113 {
116 LOG (GNUNET_ERROR_TYPE_INFO, 114 LOG (GNUNET_ERROR_TYPE_INFO, _("Not installing a handler because $%s=%s\n"),
117 _("Not installing a handler because $%s=%s\n"), 115 GNUNET_OS_CONTROL_PIPE, env_buf);
118 GNUNET_OS_CONTROL_PIPE, env_buf); 116 return;
119 return; 117 }
120 }
121 control_pipe = 118 control_pipe =
122 GNUNET_DISK_npipe_open (env_buf, GNUNET_DISK_OPEN_READ, 119 GNUNET_DISK_npipe_open (env_buf, GNUNET_DISK_OPEN_READ,
123 GNUNET_DISK_PERM_USER_READ | 120 GNUNET_DISK_PERM_USER_READ |
124 GNUNET_DISK_PERM_USER_WRITE); 121 GNUNET_DISK_PERM_USER_WRITE);
125 if (control_pipe == NULL) 122 if (control_pipe == NULL)
126 { 123 {
127 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf); 124 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
128 return; 125 return;
129 } 126 }
130#if DEBUG_OS 127#if DEBUG_OS
131 LOG (GNUNET_ERROR_TYPE_DEBUG, 128 LOG (GNUNET_ERROR_TYPE_DEBUG,
132 "Adding parent control handler pipe `%s' to the scheduler\n", env_buf); 129 "Adding parent control handler pipe `%s' to the scheduler\n", env_buf);
133#endif 130#endif
134 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe, 131 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe,
135 &parent_control_handler, control_pipe); 132 &parent_control_handler, control_pipe);
136} 133}
137 134
138 135
@@ -166,90 +163,90 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
166 163
167 ret = GNUNET_DISK_file_write (proc->control_pipe, &sig, sizeof (sig)); 164 ret = GNUNET_DISK_file_write (proc->control_pipe, &sig, sizeof (sig));
168 if (ret != sizeof (sig)) 165 if (ret != sizeof (sig))
166 {
167 if (errno == ECOMM)
169 { 168 {
170 if (errno == ECOMM) 169 /* Child process is not controllable via pipe */
171 {
172 /* Child process is not controllable via pipe */
173#if DEBUG_OS 170#if DEBUG_OS
174 LOG (GNUNET_ERROR_TYPE_DEBUG, 171 LOG (GNUNET_ERROR_TYPE_DEBUG,
175 "Child process is not controllable, will kill it directly\n"); 172 "Child process is not controllable, will kill it directly\n");
176#endif 173#endif
177 } 174 }
178 else if (errno == EPIPE) 175 else if (errno == EPIPE)
179 { 176 {
180#if DEBUG_OS 177#if DEBUG_OS
181 LOG (GNUNET_ERROR_TYPE_DEBUG, 178 LOG (GNUNET_ERROR_TYPE_DEBUG,
182 "Failed to write into control pipe, because pipe is invalid (the child is most likely dead)\n"); 179 "Failed to write into control pipe, because pipe is invalid (the child is most likely dead)\n");
183#endif 180#endif
184 } 181 }
185 else 182 else
186 LOG (GNUNET_ERROR_TYPE_WARNING, 183 LOG (GNUNET_ERROR_TYPE_WARNING,
187 "Failed to write into control pipe , errno is %d\n", errno); 184 "Failed to write into control pipe , errno is %d\n", errno);
188#if WINDOWS && !defined(__CYGWIN__) 185#if WINDOWS && !defined(__CYGWIN__)
189 TerminateProcess (proc->handle, 0); 186 TerminateProcess (proc->handle, 0);
190#else 187#else
191 PLIBC_KILL (proc->pid, sig); 188 PLIBC_KILL (proc->pid, sig);
192#endif 189#endif
193 } 190 }
194 else 191 else
195 { 192 {
196#if DEBUG_OS 193#if DEBUG_OS
197 LOG (GNUNET_ERROR_TYPE_DEBUG, 194 LOG (GNUNET_ERROR_TYPE_DEBUG,
198 "Wrote control code into control pipe, now waiting\n"); 195 "Wrote control code into control pipe, now waiting\n");
199#endif 196#endif
200 197
201#if WINDOWS 198#if WINDOWS
202 /* Give it 3 seconds to die, then kill it in a nice Windows-specific way */ 199 /* Give it 3 seconds to die, then kill it in a nice Windows-specific way */
203 if (WaitForSingleObject (proc->handle, 3000) != WAIT_OBJECT_0) 200 if (WaitForSingleObject (proc->handle, 3000) != WAIT_OBJECT_0)
204 TerminateProcess (proc->handle, 0); 201 TerminateProcess (proc->handle, 0);
205 res = 0; 202 res = 0;
206#else 203#else
207 struct GNUNET_NETWORK_FDSet *rfds; 204 struct GNUNET_NETWORK_FDSet *rfds;
208 struct GNUNET_NETWORK_FDSet *efds; 205 struct GNUNET_NETWORK_FDSet *efds;
209 206
210 rfds = GNUNET_NETWORK_fdset_create (); 207 rfds = GNUNET_NETWORK_fdset_create ();
211 efds = GNUNET_NETWORK_fdset_create (); 208 efds = GNUNET_NETWORK_fdset_create ();
212 209
213 GNUNET_NETWORK_fdset_handle_set (rfds, proc->control_pipe); 210 GNUNET_NETWORK_fdset_handle_set (rfds, proc->control_pipe);
214 GNUNET_NETWORK_fdset_handle_set (efds, proc->control_pipe); 211 GNUNET_NETWORK_fdset_handle_set (efds, proc->control_pipe);
215 212
216 /* Ndurner thought this up, and i have no idea what it does. 213 /* Ndurner thought this up, and i have no idea what it does.
217 * There's have never been any code to answer the shutdown call 214 * There's have never been any code to answer the shutdown call
218 * (write a single int into the pipe, so that this function can read it). 215 * (write a single int into the pipe, so that this function can read it).
219 * On *nix select() will probably tell that pipe is ready 216 * On *nix select() will probably tell that pipe is ready
220 * for reading, once the other process shuts down, 217 * for reading, once the other process shuts down,
221 * but the read () call will fail, triggering a kill () 218 * but the read () call will fail, triggering a kill ()
222 * on the pid that is already dead. This will probably result in non-0 219 * on the pid that is already dead. This will probably result in non-0
223 * return from kill(), and therefore from this function. 220 * return from kill(), and therefore from this function.
224 */ 221 */
225 while (1) 222 while (1)
226 { 223 {
227 ret = 224 ret =
228 GNUNET_NETWORK_socket_select (rfds, NULL, efds, 225 GNUNET_NETWORK_socket_select (rfds, NULL, efds,
229 GNUNET_TIME_relative_multiply 226 GNUNET_TIME_relative_multiply
230 (GNUNET_TIME_relative_get_unit (), 227 (GNUNET_TIME_relative_get_unit (),
231 5000)); 228 5000));
232 229
233 if (ret < 1 || 230 if (ret < 1 ||
234 GNUNET_NETWORK_fdset_handle_isset (efds, proc->control_pipe)) 231 GNUNET_NETWORK_fdset_handle_isset (efds, proc->control_pipe))
235 { 232 {
236 /* Just to be sure */ 233 /* Just to be sure */
237 PLIBC_KILL (proc->pid, sig); 234 PLIBC_KILL (proc->pid, sig);
238 res = 0; 235 res = 0;
239 break; 236 break;
240 } 237 }
241 else 238 else
242 { 239 {
243 if (GNUNET_DISK_file_read 240 if (GNUNET_DISK_file_read (proc->control_pipe, &ret, sizeof (ret)) !=
244 (proc->control_pipe, &ret, sizeof (ret)) != GNUNET_OK) 241 GNUNET_OK)
245 res = PLIBC_KILL (proc->pid, sig); 242 res = PLIBC_KILL (proc->pid, sig);
246 243
247 /* Child signaled shutdown is in progress */ 244 /* Child signaled shutdown is in progress */
248 continue; 245 continue;
249 } 246 }
250 }
251#endif
252 } 247 }
248#endif
249 }
253 250
254 return res; 251 return res;
255#else 252#else
@@ -324,7 +321,7 @@ ChildWaitThread (void *arg)
324 */ 321 */
325int 322int
326GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc, 323GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
327 enum GNUNET_SCHEDULER_Priority prio) 324 enum GNUNET_SCHEDULER_Priority prio)
328{ 325{
329 int rprio; 326 int rprio;
330 327
@@ -334,51 +331,51 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
334 331
335 /* convert to MINGW/Unix values */ 332 /* convert to MINGW/Unix values */
336 switch (prio) 333 switch (prio)
337 { 334 {
338 case GNUNET_SCHEDULER_PRIORITY_UI: 335 case GNUNET_SCHEDULER_PRIORITY_UI:
339 case GNUNET_SCHEDULER_PRIORITY_URGENT: 336 case GNUNET_SCHEDULER_PRIORITY_URGENT:
340#ifdef MINGW 337#ifdef MINGW
341 rprio = HIGH_PRIORITY_CLASS; 338 rprio = HIGH_PRIORITY_CLASS;
342#else 339#else
343 rprio = 0; 340 rprio = 0;
344#endif 341#endif
345 break; 342 break;
346 343
347 case GNUNET_SCHEDULER_PRIORITY_HIGH: 344 case GNUNET_SCHEDULER_PRIORITY_HIGH:
348#ifdef MINGW 345#ifdef MINGW
349 rprio = ABOVE_NORMAL_PRIORITY_CLASS; 346 rprio = ABOVE_NORMAL_PRIORITY_CLASS;
350#else 347#else
351 rprio = 5; 348 rprio = 5;
352#endif 349#endif
353 break; 350 break;
354 351
355 case GNUNET_SCHEDULER_PRIORITY_DEFAULT: 352 case GNUNET_SCHEDULER_PRIORITY_DEFAULT:
356#ifdef MINGW 353#ifdef MINGW
357 rprio = NORMAL_PRIORITY_CLASS; 354 rprio = NORMAL_PRIORITY_CLASS;
358#else 355#else
359 rprio = 7; 356 rprio = 7;
360#endif 357#endif
361 break; 358 break;
362 359
363 case GNUNET_SCHEDULER_PRIORITY_BACKGROUND: 360 case GNUNET_SCHEDULER_PRIORITY_BACKGROUND:
364#ifdef MINGW 361#ifdef MINGW
365 rprio = BELOW_NORMAL_PRIORITY_CLASS; 362 rprio = BELOW_NORMAL_PRIORITY_CLASS;
366#else 363#else
367 rprio = 10; 364 rprio = 10;
368#endif 365#endif
369 break; 366 break;
370 367
371 case GNUNET_SCHEDULER_PRIORITY_IDLE: 368 case GNUNET_SCHEDULER_PRIORITY_IDLE:
372#ifdef MINGW 369#ifdef MINGW
373 rprio = IDLE_PRIORITY_CLASS; 370 rprio = IDLE_PRIORITY_CLASS;
374#else 371#else
375 rprio = 19; 372 rprio = 19;
376#endif 373#endif
377 break; 374 break;
378 default: 375 default:
379 GNUNET_assert (0); 376 GNUNET_assert (0);
380 return GNUNET_SYSERR; 377 return GNUNET_SYSERR;
381 } 378 }
382 379
383 /* Set process priority */ 380 /* Set process priority */
384#ifdef MINGW 381#ifdef MINGW
@@ -393,27 +390,26 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
393 390
394 pid = proc->pid; 391 pid = proc->pid;
395 if ((0 == pid) || (pid == getpid ())) 392 if ((0 == pid) || (pid == getpid ()))
393 {
394 int have = nice (0);
395 int delta = rprio - have;
396
397 errno = 0;
398 if ((delta != 0) && (rprio == nice (delta)) && (errno != 0))
396 { 399 {
397 int have = nice (0); 400 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "nice");
398 int delta = rprio - have; 401 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 }
407 } 402 }
403 }
408 else 404 else
405 {
406 if (0 != setpriority (PRIO_PROCESS, pid, rprio))
409 { 407 {
410 if (0 != setpriority (PRIO_PROCESS, pid, rprio)) 408 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
411 { 409 "setpriority");
412 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 410 return GNUNET_SYSERR;
413 "setpriority");
414 return GNUNET_SYSERR;
415 }
416 } 411 }
412 }
417#else 413#else
418#if DEBUG_OS 414#if DEBUG_OS
419 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 415 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
@@ -440,84 +436,84 @@ CreateCustomEnvTable (char **vars)
440 win32_env_table = GetEnvironmentStringsA (); 436 win32_env_table = GetEnvironmentStringsA ();
441 if (win32_env_table == NULL) 437 if (win32_env_table == NULL)
442 return NULL; 438 return NULL;
443 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++); 439 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
444 n_var = c; 440 n_var = c;
445 index = GNUNET_malloc (sizeof (char *) * n_var); 441 index = GNUNET_malloc (sizeof (char *) * n_var);
446 for (c = 0; c < n_var; c++) 442 for (c = 0; c < n_var; c++)
447 index[c] = 0; 443 index[c] = 0;
448 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++) 444 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
445 {
446 size_t len = strlen (ptr);
447 int found = 0;
448
449 for (var_ptr = vars; *var_ptr; var_ptr++)
449 { 450 {
450 size_t len = strlen (ptr); 451 var = *var_ptr++;
451 int found = 0; 452 val = *var_ptr;
452 453 var_len = strlen (var);
453 for (var_ptr = vars; *var_ptr; var_ptr++) 454 if (strncmp (var, ptr, var_len) == 0)
454 { 455 {
455 var = *var_ptr++; 456 found = 1;
456 val = *var_ptr; 457 index[c] = 1;
457 var_len = strlen (var); 458 tablesize += var_len + strlen (val) + 1;
458 if (strncmp (var, ptr, var_len) == 0) 459 break;
459 { 460 }
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;
469 } 461 }
462 if (!found)
463 tablesize += len + 1;
464 ptr += len + 1;
465 }
470 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) 466 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
467 {
468 var = *var_ptr++;
469 val = *var_ptr;
470 if (index[c] != 1)
471 n_found += strlen (var) + strlen (val) + 1;
472 }
473 result = GNUNET_malloc (tablesize + n_found + 1);
474 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
475 {
476 size_t len = strlen (ptr);
477 int found = 0;
478
479 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
471 { 480 {
472 var = *var_ptr++; 481 var = *var_ptr++;
473 val = *var_ptr; 482 val = *var_ptr;
474 if (index[c] != 1) 483 var_len = strlen (var);
475 n_found += strlen (var) + strlen (val) + 1; 484 if (strncmp (var, ptr, var_len) == 0)
485 {
486 found = 1;
487 break;
488 }
476 } 489 }
477 result = GNUNET_malloc (tablesize + n_found + 1); 490 if (!found)
478 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
479 { 491 {
480 size_t len = strlen (ptr); 492 strcpy (result_ptr, ptr);
481 int found = 0; 493 result_ptr += len + 1;
482 494 }
483 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) 495 else
484 { 496 {
485 var = *var_ptr++; 497 strcpy (result_ptr, var);
486 val = *var_ptr; 498 result_ptr += var_len;
487 var_len = strlen (var); 499 strcpy (result_ptr, val);
488 if (strncmp (var, ptr, var_len) == 0) 500 result_ptr += strlen (val) + 1;
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;
507 } 501 }
502 ptr += len + 1;
503 }
508 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++) 504 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
505 {
506 var = *var_ptr++;
507 val = *var_ptr;
508 var_len = strlen (var);
509 if (index[c] != 1)
509 { 510 {
510 var = *var_ptr++; 511 strcpy (result_ptr, var);
511 val = *var_ptr; 512 result_ptr += var_len;
512 var_len = strlen (var); 513 strcpy (result_ptr, val);
513 if (index[c] != 1) 514 result_ptr += strlen (val) + 1;
514 {
515 strcpy (result_ptr, var);
516 result_ptr += var_len;
517 strcpy (result_ptr, val);
518 result_ptr += strlen (val) + 1;
519 }
520 } 515 }
516 }
521 FreeEnvironmentStrings (win32_env_table); 517 FreeEnvironmentStrings (win32_env_table);
522 GNUNET_free (index); 518 GNUNET_free (index);
523 *result_ptr = 0; 519 *result_ptr = 0;
@@ -537,8 +533,8 @@ CreateCustomEnvTable (char **vars)
537 */ 533 */
538struct GNUNET_OS_Process * 534struct GNUNET_OS_Process *
539GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin, 535GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
540 struct GNUNET_DISK_PipeHandle *pipe_stdout, 536 struct GNUNET_DISK_PipeHandle *pipe_stdout,
541 const char *filename, va_list va) 537 const char *filename, va_list va)
542{ 538{
543 va_list ap; 539 va_list ap;
544 540
@@ -559,9 +555,9 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
559 555
560#if ENABLE_WINDOWS_WORKAROUNDS 556#if ENABLE_WINDOWS_WORKAROUNDS
561 control_pipe = 557 control_pipe =
562 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 558 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
563 GNUNET_DISK_PERM_USER_READ | 559 GNUNET_DISK_PERM_USER_READ |
564 GNUNET_DISK_PERM_USER_WRITE); 560 GNUNET_DISK_PERM_USER_WRITE);
565 if (control_pipe == NULL) 561 if (control_pipe == NULL)
566 return NULL; 562 return NULL;
567#endif 563#endif
@@ -569,38 +565,35 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
569 argc = 0; 565 argc = 0;
570 va_copy (ap, va); 566 va_copy (ap, va);
571 while (NULL != va_arg (ap, char *)) 567 while (NULL != va_arg (ap, char *))
572 argc++; 568 argc++;
573 569
574 va_end (ap); 570 va_end (ap);
575 argv = GNUNET_malloc (sizeof (char *) * (argc + 1)); 571 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
576 argc = 0; 572 argc = 0;
577 va_copy (ap, va); 573 va_copy (ap, va);
578 while (NULL != (argv[argc] = va_arg (ap, char *))) 574 while (NULL != (argv[argc] = va_arg (ap, char *)))
579 argc++; 575 argc++;
580 576
581 va_end (ap); 577 va_end (ap);
582 if (pipe_stdout != NULL) 578 if (pipe_stdout != NULL)
583 { 579 {
584 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 580 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
585 (pipe_stdout, 581 (pipe_stdout,
586 GNUNET_DISK_PIPE_END_WRITE), 582 GNUNET_DISK_PIPE_END_WRITE),
587 &fd_stdout_write, sizeof (int)); 583 &fd_stdout_write, sizeof (int));
588 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 584 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
589 (pipe_stdout, 585 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
590 GNUNET_DISK_PIPE_END_READ), 586 &fd_stdout_read, sizeof (int));
591 &fd_stdout_read, sizeof (int)); 587 }
592 }
593 if (pipe_stdin != NULL) 588 if (pipe_stdin != NULL)
594 { 589 {
595 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 590 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
596 (pipe_stdin, 591 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
597 GNUNET_DISK_PIPE_END_READ), 592 &fd_stdin_read, sizeof (int));
598 &fd_stdin_read, sizeof (int)); 593 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
599 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 594 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
600 (pipe_stdin, 595 &fd_stdin_write, sizeof (int));
601 GNUNET_DISK_PIPE_END_WRITE), 596 }
602 &fd_stdin_write, sizeof (int));
603 }
604 597
605#if HAVE_WORKING_VFORK 598#if HAVE_WORKING_VFORK
606 ret = vfork (); 599 ret = vfork ();
@@ -608,46 +601,44 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
608 ret = fork (); 601 ret = fork ();
609#endif 602#endif
610 if (ret != 0) 603 if (ret != 0)
604 {
605 if (ret == -1)
611 { 606 {
612 if (ret == -1) 607 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
613 {
614 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
615#if ENABLE_WINDOWS_WORKAROUNDS 608#if ENABLE_WINDOWS_WORKAROUNDS
616 GNUNET_DISK_npipe_close (control_pipe); 609 GNUNET_DISK_npipe_close (control_pipe);
617#endif 610#endif
618 } 611 }
619 else 612 else
620 { 613 {
621 614
622#if HAVE_WORKING_VFORK 615#if HAVE_WORKING_VFORK
623 /* let's hope vfork actually works; for some extreme cases (including 616 /* let's hope vfork actually works; for some extreme cases (including
624 * a testcase) we need 'execvp' to have run before we return, since 617 * a testcase) we need 'execvp' to have run before we return, since
625 * we may send a signal to the process next and we don't want it 618 * we may send a signal to the process next and we don't want it
626 * to be caught by OUR signal handler (but either by the default 619 * to be caught by OUR signal handler (but either by the default
627 * handler or the actual handler as installed by the process itself). */ 620 * handler or the actual handler as installed by the process itself). */
628#else 621#else
629 /* let's give the child process a chance to run execvp, 1s should 622 /* let's give the child process a chance to run execvp, 1s should
630 * be plenty in practice */ 623 * be plenty in practice */
631 if (pipe_stdout != NULL) 624 if (pipe_stdout != NULL)
632 GNUNET_DISK_pipe_close_end (pipe_stdout, 625 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
633 GNUNET_DISK_PIPE_END_WRITE); 626 if (pipe_stdin != NULL)
634 if (pipe_stdin != NULL) 627 GNUNET_DISK_pipe_close_end (pipe_stdin, GNUNET_DISK_PIPE_END_READ);
635 GNUNET_DISK_pipe_close_end (pipe_stdin, 628 sleep (1);
636 GNUNET_DISK_PIPE_END_READ);
637 sleep (1);
638#endif 629#endif
639 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); 630 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
640 gnunet_proc->pid = ret; 631 gnunet_proc->pid = ret;
641#if ENABLE_WINDOWS_WORKAROUNDS 632#if ENABLE_WINDOWS_WORKAROUNDS
642 gnunet_proc->control_pipe = control_pipe; 633 gnunet_proc->control_pipe = control_pipe;
643#endif 634#endif
644 } 635 }
645 GNUNET_free (argv); 636 GNUNET_free (argv);
646#if ENABLE_WINDOWS_WORKAROUNDS 637#if ENABLE_WINDOWS_WORKAROUNDS
647 GNUNET_free (childpipename); 638 GNUNET_free (childpipename);
648#endif 639#endif
649 return gnunet_proc; 640 return gnunet_proc;
650 } 641 }
651 642
652#if ENABLE_WINDOWS_WORKAROUNDS 643#if ENABLE_WINDOWS_WORKAROUNDS
653 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1); 644 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1);
@@ -655,21 +646,21 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
655#endif 646#endif
656 647
657 if (pipe_stdout != NULL) 648 if (pipe_stdout != NULL)
658 { 649 {
659 GNUNET_break (0 == close (fd_stdout_read)); 650 GNUNET_break (0 == close (fd_stdout_read));
660 if (-1 == dup2 (fd_stdout_write, 1)) 651 if (-1 == dup2 (fd_stdout_write, 1))
661 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2"); 652 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
662 GNUNET_break (0 == close (fd_stdout_write)); 653 GNUNET_break (0 == close (fd_stdout_write));
663 } 654 }
664 655
665 if (pipe_stdin != NULL) 656 if (pipe_stdin != NULL)
666 { 657 {
667 658
668 GNUNET_break (0 == close (fd_stdin_write)); 659 GNUNET_break (0 == close (fd_stdin_write));
669 if (-1 == dup2 (fd_stdin_read, 0)) 660 if (-1 == dup2 (fd_stdin_read, 0))
670 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2"); 661 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
671 GNUNET_break (0 == close (fd_stdin_read)); 662 GNUNET_break (0 == close (fd_stdin_read));
672 } 663 }
673 execvp (filename, argv); 664 execvp (filename, argv);
674 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); 665 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
675 _exit (1); 666 _exit (1);
@@ -705,8 +696,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
705 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0); 696 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
706 697
707 alloc_len = 698 alloc_len =
708 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + 699 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
709 strlen (libdir); 700 strlen (libdir);
710 701
711 pathbuf = GNUNET_malloc (alloc_len * sizeof (char)); 702 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
712 703
@@ -729,39 +720,39 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
729 if (non_const_filename[1] == ':') 720 if (non_const_filename[1] == ':')
730 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename); 721 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
731 else if (!SearchPathA 722 else if (!SearchPathA
732 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), 723 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
733 path, NULL)) 724 path, NULL))
734 { 725 {
735 SetErrnoFromWinError (GetLastError ()); 726 SetErrnoFromWinError (GetLastError ());
736 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath", 727 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
737 non_const_filename); 728 non_const_filename);
738 GNUNET_free (non_const_filename); 729 GNUNET_free (non_const_filename);
739 GNUNET_free (pathbuf); 730 GNUNET_free (pathbuf);
740 return NULL; 731 return NULL;
741 } 732 }
742 GNUNET_free (pathbuf); 733 GNUNET_free (pathbuf);
743 GNUNET_free (non_const_filename); 734 GNUNET_free (non_const_filename);
744 735
745 cmdlen = 0; 736 cmdlen = 0;
746 va_copy (ap, va); 737 va_copy (ap, va);
747 while (NULL != (arg = va_arg (ap, char *))) 738 while (NULL != (arg = va_arg (ap, char *)))
748 { 739 {
749 if (cmdlen == 0) 740 if (cmdlen == 0)
750 cmdlen = cmdlen + strlen (path) + 3; 741 cmdlen = cmdlen + strlen (path) + 3;
751 else 742 else
752 cmdlen = cmdlen + strlen (arg) + 3; 743 cmdlen = cmdlen + strlen (arg) + 3;
753 } 744 }
754 va_end (ap); 745 va_end (ap);
755 746
756 cmd = idx = GNUNET_malloc (sizeof (char) * (cmdlen + 1)); 747 cmd = idx = GNUNET_malloc (sizeof (char) * (cmdlen + 1));
757 va_copy (ap, va); 748 va_copy (ap, va);
758 while (NULL != (arg = va_arg (ap, char *))) 749 while (NULL != (arg = va_arg (ap, char *)))
759 { 750 {
760 if (idx == cmd) 751 if (idx == cmd)
761 idx += sprintf (idx, "\"%s\" ", path); 752 idx += sprintf (idx, "\"%s\" ", path);
762 else 753 else
763 idx += sprintf (idx, "\"%s\" ", arg); 754 idx += sprintf (idx, "\"%s\" ", arg);
764 } 755 }
765 va_end (ap); 756 va_end (ap);
766 757
767 memset (&start, 0, sizeof (start)); 758 memset (&start, 0, sizeof (start));
@@ -771,37 +762,36 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
771 start.dwFlags |= STARTF_USESTDHANDLES; 762 start.dwFlags |= STARTF_USESTDHANDLES;
772 763
773 if (pipe_stdin != NULL) 764 if (pipe_stdin != NULL)
774 { 765 {
775 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 766 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
776 (pipe_stdin, 767 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
777 GNUNET_DISK_PIPE_END_READ), 768 &stdin_handle, sizeof (HANDLE));
778 &stdin_handle, sizeof (HANDLE)); 769 start.hStdInput = stdin_handle;
779 start.hStdInput = stdin_handle; 770 }
780 }
781 771
782 if (pipe_stdout != NULL) 772 if (pipe_stdout != NULL)
783 { 773 {
784 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle 774 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
785 (pipe_stdout, 775 (pipe_stdout,
786 GNUNET_DISK_PIPE_END_WRITE), 776 GNUNET_DISK_PIPE_END_WRITE),
787 &stdout_handle, sizeof (HANDLE)); 777 &stdout_handle, sizeof (HANDLE));
788 start.hStdOutput = stdout_handle; 778 start.hStdOutput = stdout_handle;
789 } 779 }
790 780
791 control_pipe = 781 control_pipe =
792 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 782 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
793 GNUNET_DISK_PERM_USER_READ | 783 GNUNET_DISK_PERM_USER_READ |
794 GNUNET_DISK_PERM_USER_WRITE); 784 GNUNET_DISK_PERM_USER_WRITE);
795 if (control_pipe == NULL) 785 if (control_pipe == NULL)
796 { 786 {
797 GNUNET_free (cmd); 787 GNUNET_free (cmd);
798 GNUNET_free (path); 788 GNUNET_free (path);
799 return NULL; 789 return NULL;
800 } 790 }
801 791
802#if DEBUG_OS 792#if DEBUG_OS
803 LOG (GNUNET_ERROR_TYPE_DEBUG, 793 LOG (GNUNET_ERROR_TYPE_DEBUG, "Opened the parent end of the pipe `%s'\n",
804 "Opened the parent end of the pipe `%s'\n", childpipename); 794 childpipename);
805#endif 795#endif
806 796
807 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE); 797 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE);
@@ -814,13 +804,13 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
814 if (!CreateProcessA 804 if (!CreateProcessA
815 (path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | CREATE_SUSPENDED, 805 (path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | CREATE_SUSPENDED,
816 env_block, NULL, &start, &proc)) 806 env_block, NULL, &start, &proc))
817 { 807 {
818 SetErrnoFromWinError (GetLastError ()); 808 SetErrnoFromWinError (GetLastError ());
819 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path); 809 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path);
820 GNUNET_free (env_block); 810 GNUNET_free (env_block);
821 GNUNET_free (cmd); 811 GNUNET_free (cmd);
822 return NULL; 812 return NULL;
823 } 813 }
824 814
825 GNUNET_free (env_block); 815 GNUNET_free (env_block);
826 816
@@ -854,8 +844,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
854 */ 844 */
855struct GNUNET_OS_Process * 845struct GNUNET_OS_Process *
856GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, 846GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
857 struct GNUNET_DISK_PipeHandle *pipe_stdout, 847 struct GNUNET_DISK_PipeHandle *pipe_stdout,
858 const char *filename, ...) 848 const char *filename, ...)
859{ 849{
860 struct GNUNET_OS_Process *ret; 850 struct GNUNET_OS_Process *ret;
861 va_list ap; 851 va_list ap;
@@ -878,7 +868,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
878 */ 868 */
879struct GNUNET_OS_Process * 869struct GNUNET_OS_Process *
880GNUNET_OS_start_process_v (const int *lsocks, const char *filename, 870GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
881 char *const argv[]) 871 char *const argv[])
882{ 872{
883#if ENABLE_WINDOWS_WORKAROUNDS 873#if ENABLE_WINDOWS_WORKAROUNDS
884 struct GNUNET_DISK_FileHandle *control_pipe = NULL; 874 struct GNUNET_DISK_FileHandle *control_pipe = NULL;
@@ -900,9 +890,9 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
900 890
901#if ENABLE_WINDOWS_WORKAROUNDS 891#if ENABLE_WINDOWS_WORKAROUNDS
902 control_pipe = 892 control_pipe =
903 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 893 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
904 GNUNET_DISK_PERM_USER_READ | 894 GNUNET_DISK_PERM_USER_READ |
905 GNUNET_DISK_PERM_USER_WRITE); 895 GNUNET_DISK_PERM_USER_WRITE);
906 if (control_pipe == NULL) 896 if (control_pipe == NULL)
907 return NULL; 897 return NULL;
908#endif 898#endif
@@ -910,52 +900,52 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
910 lscp = NULL; 900 lscp = NULL;
911 ls = 0; 901 ls = 0;
912 if (lsocks != NULL) 902 if (lsocks != NULL)
913 { 903 {
914 i = 0; 904 i = 0;
915 while (-1 != (k = lsocks[i++])) 905 while (-1 != (k = lsocks[i++]))
916 GNUNET_array_append (lscp, ls, k); 906 GNUNET_array_append (lscp, ls, k);
917 GNUNET_array_append (lscp, ls, -1); 907 GNUNET_array_append (lscp, ls, -1);
918 } 908 }
919#if HAVE_WORKING_VFORK 909#if HAVE_WORKING_VFORK
920 ret = vfork (); 910 ret = vfork ();
921#else 911#else
922 ret = fork (); 912 ret = fork ();
923#endif 913#endif
924 if (ret != 0) 914 if (ret != 0)
915 {
916 if (ret == -1)
925 { 917 {
926 if (ret == -1) 918 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
927 {
928 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
929#if ENABLE_WINDOWS_WORKAROUNDS 919#if ENABLE_WINDOWS_WORKAROUNDS
930 GNUNET_DISK_npipe_close (control_pipe); 920 GNUNET_DISK_npipe_close (control_pipe);
931#endif 921#endif
932 } 922 }
933 else 923 else
934 { 924 {
935#if HAVE_WORKING_VFORK 925#if HAVE_WORKING_VFORK
936 /* let's hope vfork actually works; for some extreme cases (including 926 /* let's hope vfork actually works; for some extreme cases (including
937 * a testcase) we need 'execvp' to have run before we return, since 927 * a testcase) we need 'execvp' to have run before we return, since
938 * we may send a signal to the process next and we don't want it 928 * we may send a signal to the process next and we don't want it
939 * to be caught by OUR signal handler (but either by the default 929 * to be caught by OUR signal handler (but either by the default
940 * handler or the actual handler as installed by the process itself). */ 930 * handler or the actual handler as installed by the process itself). */
941#else 931#else
942 /* let's give the child process a chance to run execvp, 1s should 932 /* let's give the child process a chance to run execvp, 1s should
943 * be plenty in practice */ 933 * be plenty in practice */
944 sleep (1); 934 sleep (1);
945#endif 935#endif
946 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process)); 936 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
947 gnunet_proc->pid = ret; 937 gnunet_proc->pid = ret;
948#if ENABLE_WINDOWS_WORKAROUNDS 938#if ENABLE_WINDOWS_WORKAROUNDS
949 gnunet_proc->control_pipe = control_pipe; 939 gnunet_proc->control_pipe = control_pipe;
950 940
951#endif 941#endif
952 } 942 }
953 GNUNET_array_grow (lscp, ls, 0); 943 GNUNET_array_grow (lscp, ls, 0);
954#if ENABLE_WINDOWS_WORKAROUNDS 944#if ENABLE_WINDOWS_WORKAROUNDS
955 GNUNET_free (childpipename); 945 GNUNET_free (childpipename);
956#endif 946#endif
957 return gnunet_proc; 947 return gnunet_proc;
958 } 948 }
959 949
960#if ENABLE_WINDOWS_WORKAROUNDS 950#if ENABLE_WINDOWS_WORKAROUNDS
961 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1); 951 setenv (GNUNET_OS_CONTROL_PIPE, childpipename, 1);
@@ -963,47 +953,47 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
963#endif 953#endif
964 954
965 if (lscp != NULL) 955 if (lscp != NULL)
956 {
957 /* read systemd documentation... */
958 GNUNET_snprintf (lpid, sizeof (lpid), "%u", getpid ());
959 setenv ("LISTEN_PID", lpid, 1);
960 i = 0;
961 tgt = 3;
962 while (-1 != lscp[i])
966 { 963 {
967 /* read systemd documentation... */ 964 j = i + 1;
968 GNUNET_snprintf (lpid, sizeof (lpid), "%u", getpid ()); 965 while (-1 != lscp[j])
969 setenv ("LISTEN_PID", lpid, 1); 966 {
970 i = 0; 967 if (lscp[j] == tgt)
971 tgt = 3; 968 {
972 while (-1 != lscp[i]) 969 /* dup away */
973 { 970 k = dup (lscp[j]);
974 j = i + 1; 971 GNUNET_assert (-1 != k);
975 while (-1 != lscp[j]) 972 GNUNET_assert (0 == close (lscp[j]));
976 { 973 lscp[j] = k;
977 if (lscp[j] == tgt) 974 break;
978 { 975 }
979 /* dup away */ 976 j++;
980 k = dup (lscp[j]); 977 }
981 GNUNET_assert (-1 != k); 978 if (lscp[i] != tgt)
982 GNUNET_assert (0 == close (lscp[j])); 979 {
983 lscp[j] = k; 980 /* Bury any existing FD, no matter what; they should all be closed
984 break; 981 * on exec anyway and the important onces have been dup'ed away */
985 } 982 (void) close (tgt);
986 j++; 983 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
987 } 984 }
988 if (lscp[i] != tgt) 985 /* unset close-on-exec flag */
989 { 986 flags = fcntl (tgt, F_GETFD);
990 /* Bury any existing FD, no matter what; they should all be closed 987 GNUNET_assert (flags >= 0);
991 * on exec anyway and the important onces have been dup'ed away */ 988 flags &= ~FD_CLOEXEC;
992 (void) close (tgt); 989 fflush (stderr);
993 GNUNET_assert (-1 != dup2 (lscp[i], tgt)); 990 (void) fcntl (tgt, F_SETFD, flags);
994 } 991 tgt++;
995 /* unset close-on-exec flag */ 992 i++;
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);
1006 } 993 }
994 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
995 setenv ("LISTEN_FDS", fds, 1);
996 }
1007 GNUNET_array_grow (lscp, ls, 0); 997 GNUNET_array_grow (lscp, ls, 0);
1008 execvp (filename, argv); 998 execvp (filename, argv);
1009 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); 999 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
@@ -1041,8 +1031,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1041 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0); 1031 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
1042 1032
1043 alloc_len = 1033 alloc_len =
1044 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 + 1034 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
1045 strlen (libdir); 1035 strlen (libdir);
1046 1036
1047 pathbuf = GNUNET_malloc (alloc_len * sizeof (char)); 1037 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
1048 1038
@@ -1054,11 +1044,11 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1054 1044
1055 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len); 1045 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
1056 if (alloc_len != pathbuf_len - 1) 1046 if (alloc_len != pathbuf_len - 1)
1057 { 1047 {
1058 GNUNET_free (pathbuf); 1048 GNUNET_free (pathbuf);
1059 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */ 1049 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
1060 return NULL; 1050 return NULL;
1061 } 1051 }
1062 1052
1063 cmdlen = strlen (filename); 1053 cmdlen = strlen (filename);
1064 if (cmdlen < 5 || strcmp (&filename[cmdlen - 4], ".exe") != 0) 1054 if (cmdlen < 5 || strcmp (&filename[cmdlen - 4], ".exe") != 0)
@@ -1070,26 +1060,26 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1070 if (non_const_filename[1] == ':') 1060 if (non_const_filename[1] == ':')
1071 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename); 1061 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
1072 else if (!SearchPathA 1062 else if (!SearchPathA
1073 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char), 1063 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
1074 path, NULL)) 1064 path, NULL))
1075 { 1065 {
1076 SetErrnoFromWinError (GetLastError ()); 1066 SetErrnoFromWinError (GetLastError ());
1077 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath", 1067 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
1078 non_const_filename); 1068 non_const_filename);
1079 GNUNET_free (non_const_filename); 1069 GNUNET_free (non_const_filename);
1080 GNUNET_free (pathbuf); 1070 GNUNET_free (pathbuf);
1081 return NULL; 1071 return NULL;
1082 } 1072 }
1083 GNUNET_free (pathbuf); 1073 GNUNET_free (pathbuf);
1084 GNUNET_free (non_const_filename); 1074 GNUNET_free (non_const_filename);
1085 1075
1086 /* Count the number of arguments */ 1076 /* Count the number of arguments */
1087 arg = (char **) argv; 1077 arg = (char **) argv;
1088 while (*arg) 1078 while (*arg)
1089 { 1079 {
1090 arg++; 1080 arg++;
1091 argcount++; 1081 argcount++;
1092 } 1082 }
1093 1083
1094 /* Allocate a copy argv */ 1084 /* Allocate a copy argv */
1095 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1)); 1085 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
@@ -1098,33 +1088,33 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1098 argcount = 0; 1088 argcount = 0;
1099 arg = (char **) argv; 1089 arg = (char **) argv;
1100 while (*arg) 1090 while (*arg)
1101 { 1091 {
1102 if (arg == argv) 1092 if (arg == argv)
1103 non_const_argv[argcount] = GNUNET_strdup (path); 1093 non_const_argv[argcount] = GNUNET_strdup (path);
1104 else 1094 else
1105 non_const_argv[argcount] = GNUNET_strdup (*arg); 1095 non_const_argv[argcount] = GNUNET_strdup (*arg);
1106 arg++; 1096 arg++;
1107 argcount++; 1097 argcount++;
1108 } 1098 }
1109 non_const_argv[argcount] = NULL; 1099 non_const_argv[argcount] = NULL;
1110 1100
1111 /* Count cmd len */ 1101 /* Count cmd len */
1112 cmdlen = 1; 1102 cmdlen = 1;
1113 arg = non_const_argv; 1103 arg = non_const_argv;
1114 while (*arg) 1104 while (*arg)
1115 { 1105 {
1116 cmdlen = cmdlen + strlen (*arg) + 3; 1106 cmdlen = cmdlen + strlen (*arg) + 3;
1117 arg++; 1107 arg++;
1118 } 1108 }
1119 1109
1120 /* Allocate and create cmd */ 1110 /* Allocate and create cmd */
1121 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen); 1111 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
1122 arg = non_const_argv; 1112 arg = non_const_argv;
1123 while (*arg) 1113 while (*arg)
1124 { 1114 {
1125 idx += sprintf (idx, "\"%s\" ", *arg); 1115 idx += sprintf (idx, "\"%s\" ", *arg);
1126 arg++; 1116 arg++;
1127 } 1117 }
1128 1118
1129 while (argcount > 0) 1119 while (argcount > 0)
1130 GNUNET_free (non_const_argv[--argcount]); 1120 GNUNET_free (non_const_argv[--argcount]);
@@ -1134,19 +1124,19 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1134 start.cb = sizeof (start); 1124 start.cb = sizeof (start);
1135 1125
1136 control_pipe = 1126 control_pipe =
1137 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE, 1127 GNUNET_DISK_npipe_create (&childpipename, GNUNET_DISK_OPEN_WRITE,
1138 GNUNET_DISK_PERM_USER_READ | 1128 GNUNET_DISK_PERM_USER_READ |
1139 GNUNET_DISK_PERM_USER_WRITE); 1129 GNUNET_DISK_PERM_USER_WRITE);
1140 if (control_pipe == NULL) 1130 if (control_pipe == NULL)
1141 { 1131 {
1142 GNUNET_free (cmd); 1132 GNUNET_free (cmd);
1143 GNUNET_free (path); 1133 GNUNET_free (path);
1144 return NULL; 1134 return NULL;
1145 } 1135 }
1146 1136
1147#if DEBUG_OS 1137#if DEBUG_OS
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, 1138 LOG (GNUNET_ERROR_TYPE_DEBUG, "Opened the parent end of the pipe `%s'\n",
1149 "Opened the parent end of the pipe `%s'\n", childpipename); 1139 childpipename);
1150#endif 1140#endif
1151 1141
1152 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE); 1142 GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE);
@@ -1159,13 +1149,13 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1159 if (!CreateProcess 1149 if (!CreateProcess
1160 (path, cmd, NULL, NULL, FALSE, DETACHED_PROCESS | CREATE_SUSPENDED, 1150 (path, cmd, NULL, NULL, FALSE, DETACHED_PROCESS | CREATE_SUSPENDED,
1161 env_block, NULL, &start, &proc)) 1151 env_block, NULL, &start, &proc))
1162 { 1152 {
1163 SetErrnoFromWinError (GetLastError ()); 1153 SetErrnoFromWinError (GetLastError ());
1164 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "CreateProcess"); 1154 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "CreateProcess");
1165 GNUNET_free (env_block); 1155 GNUNET_free (env_block);
1166 GNUNET_free (cmd); 1156 GNUNET_free (cmd);
1167 return NULL; 1157 return NULL;
1168 } 1158 }
1169 1159
1170 GNUNET_free (env_block); 1160 GNUNET_free (env_block);
1171 1161
@@ -1194,8 +1184,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
1194 */ 1184 */
1195int 1185int
1196GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, 1186GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1197 enum GNUNET_OS_ProcessStatusType *type, 1187 enum GNUNET_OS_ProcessStatusType *type,
1198 unsigned long *code) 1188 unsigned long *code)
1199{ 1189{
1200#ifndef MINGW 1190#ifndef MINGW
1201 int status; 1191 int status;
@@ -1204,48 +1194,48 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1204 GNUNET_assert (0 != proc); 1194 GNUNET_assert (0 != proc);
1205 ret = waitpid (proc->pid, &status, WNOHANG); 1195 ret = waitpid (proc->pid, &status, WNOHANG);
1206 if (ret < 0) 1196 if (ret < 0)
1207 { 1197 {
1208 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 1198 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1209 return GNUNET_SYSERR; 1199 return GNUNET_SYSERR;
1210 } 1200 }
1211 if (0 == ret) 1201 if (0 == ret)
1212 { 1202 {
1213 *type = GNUNET_OS_PROCESS_RUNNING; 1203 *type = GNUNET_OS_PROCESS_RUNNING;
1214 *code = 0; 1204 *code = 0;
1215 return GNUNET_NO; 1205 return GNUNET_NO;
1216 } 1206 }
1217 if (proc->pid != ret) 1207 if (proc->pid != ret)
1218 { 1208 {
1219 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 1209 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1220 return GNUNET_SYSERR; 1210 return GNUNET_SYSERR;
1221 } 1211 }
1222 if (WIFEXITED (status)) 1212 if (WIFEXITED (status))
1223 { 1213 {
1224 *type = GNUNET_OS_PROCESS_EXITED; 1214 *type = GNUNET_OS_PROCESS_EXITED;
1225 *code = WEXITSTATUS (status); 1215 *code = WEXITSTATUS (status);
1226 } 1216 }
1227 else if (WIFSIGNALED (status)) 1217 else if (WIFSIGNALED (status))
1228 { 1218 {
1229 *type = GNUNET_OS_PROCESS_SIGNALED; 1219 *type = GNUNET_OS_PROCESS_SIGNALED;
1230 *code = WTERMSIG (status); 1220 *code = WTERMSIG (status);
1231 } 1221 }
1232 else if (WIFSTOPPED (status)) 1222 else if (WIFSTOPPED (status))
1233 { 1223 {
1234 *type = GNUNET_OS_PROCESS_SIGNALED; 1224 *type = GNUNET_OS_PROCESS_SIGNALED;
1235 *code = WSTOPSIG (status); 1225 *code = WSTOPSIG (status);
1236 } 1226 }
1237#ifdef WIFCONTINUED 1227#ifdef WIFCONTINUED
1238 else if (WIFCONTINUED (status)) 1228 else if (WIFCONTINUED (status))
1239 { 1229 {
1240 *type = GNUNET_OS_PROCESS_RUNNING; 1230 *type = GNUNET_OS_PROCESS_RUNNING;
1241 *code = 0; 1231 *code = 0;
1242 } 1232 }
1243#endif 1233#endif
1244 else 1234 else
1245 { 1235 {
1246 *type = GNUNET_OS_PROCESS_UNKNOWN; 1236 *type = GNUNET_OS_PROCESS_UNKNOWN;
1247 *code = 0; 1237 *code = 0;
1248 } 1238 }
1249#else 1239#else
1250 HANDLE h; 1240 HANDLE h;
1251 DWORD c, error_code, ret; 1241 DWORD c, error_code, ret;
@@ -1253,11 +1243,11 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1253 h = proc->handle; 1243 h = proc->handle;
1254 ret = proc->pid; 1244 ret = proc->pid;
1255 if (h == NULL || ret == 0) 1245 if (h == NULL || ret == 0)
1256 { 1246 {
1257 LOG (GNUNET_ERROR_TYPE_WARNING, 1247 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1258 "Invalid process information {%d, %08X}\n", ret, h); 1248 ret, h);
1259 return GNUNET_SYSERR; 1249 return GNUNET_SYSERR;
1260 } 1250 }
1261 if (h == NULL) 1251 if (h == NULL)
1262 h = GetCurrentProcess (); 1252 h = GetCurrentProcess ();
1263 1253
@@ -1265,17 +1255,17 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1265 ret = GetExitCodeProcess (h, &c); 1255 ret = GetExitCodeProcess (h, &c);
1266 error_code = GetLastError (); 1256 error_code = GetLastError ();
1267 if (ret == 0 || error_code != NO_ERROR) 1257 if (ret == 0 || error_code != NO_ERROR)
1268 { 1258 {
1269 SetErrnoFromWinError (error_code); 1259 SetErrnoFromWinError (error_code);
1270 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess"); 1260 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1271 return GNUNET_SYSERR; 1261 return GNUNET_SYSERR;
1272 } 1262 }
1273 if (STILL_ACTIVE == c) 1263 if (STILL_ACTIVE == c)
1274 { 1264 {
1275 *type = GNUNET_OS_PROCESS_RUNNING; 1265 *type = GNUNET_OS_PROCESS_RUNNING;
1276 *code = 0; 1266 *code = 0;
1277 return GNUNET_NO; 1267 return GNUNET_NO;
1278 } 1268 }
1279 *type = GNUNET_OS_PROCESS_EXITED; 1269 *type = GNUNET_OS_PROCESS_EXITED;
1280 *code = c; 1270 *code = c;
1281#endif 1271#endif
@@ -1305,19 +1295,19 @@ GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1305 1295
1306 h = proc->handle; 1296 h = proc->handle;
1307 if (NULL == h) 1297 if (NULL == h)
1308 { 1298 {
1309 LOG (GNUNET_ERROR_TYPE_WARNING, 1299 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1310 "Invalid process information {%d, %08X}\n", proc->pid, h); 1300 proc->pid, h);
1311 return GNUNET_SYSERR; 1301 return GNUNET_SYSERR;
1312 } 1302 }
1313 if (h == NULL) 1303 if (h == NULL)
1314 h = GetCurrentProcess (); 1304 h = GetCurrentProcess ();
1315 1305
1316 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE)) 1306 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1317 { 1307 {
1318 SetErrnoFromWinError (GetLastError ()); 1308 SetErrnoFromWinError (GetLastError ());
1319 ret = GNUNET_SYSERR; 1309 ret = GNUNET_SYSERR;
1320 } 1310 }
1321 else 1311 else
1322 ret = GNUNET_OK; 1312 ret = GNUNET_OK;
1323 1313
@@ -1391,10 +1381,10 @@ GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1391{ 1381{
1392 1382
1393 if (cmd->proc != NULL) 1383 if (cmd->proc != NULL)
1394 { 1384 {
1395 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask); 1385 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask);
1396 GNUNET_SCHEDULER_cancel (cmd->rtask); 1386 GNUNET_SCHEDULER_cancel (cmd->rtask);
1397 } 1387 }
1398 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL); 1388 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1399 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip)); 1389 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1400 GNUNET_OS_process_close (cmd->eip); 1390 GNUNET_OS_process_close (cmd->eip);
@@ -1418,43 +1408,42 @@ cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1418 ssize_t ret; 1408 ssize_t ret;
1419 1409
1420 cmd->rtask = GNUNET_SCHEDULER_NO_TASK; 1410 cmd->rtask = GNUNET_SCHEDULER_NO_TASK;
1421 if (GNUNET_YES != 1411 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1422 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r)) 1412 {
1423 { 1413 /* timeout, shutdown, etc. */
1424 /* timeout, shutdown, etc. */ 1414 proc = cmd->proc;
1425 proc = cmd->proc; 1415 cmd->proc = NULL;
1426 cmd->proc = NULL; 1416 proc (cmd->proc_cls, NULL);
1427 proc (cmd->proc_cls, NULL); 1417 return;
1428 return; 1418 }
1429 }
1430 ret = 1419 ret =
1431 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off], 1420 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off],
1432 sizeof (cmd->buf) - cmd->off); 1421 sizeof (cmd->buf) - cmd->off);
1433 if (ret <= 0) 1422 if (ret <= 0)
1423 {
1424 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1434 { 1425 {
1435 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf))) 1426 cmd->buf[cmd->off] = '\0';
1436 { 1427 cmd->proc (cmd->proc_cls, cmd->buf);
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;
1444 } 1428 }
1429 proc = cmd->proc;
1430 cmd->proc = NULL;
1431 proc (cmd->proc_cls, NULL);
1432 return;
1433 }
1445 end = memchr (&cmd->buf[cmd->off], '\n', ret); 1434 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1446 cmd->off += ret; 1435 cmd->off += ret;
1447 while (end != NULL) 1436 while (end != NULL)
1448 { 1437 {
1449 *end = '\0'; 1438 *end = '\0';
1450 cmd->proc (cmd->proc_cls, cmd->buf); 1439 cmd->proc (cmd->proc_cls, cmd->buf);
1451 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf)); 1440 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1452 cmd->off -= (end + 1 - cmd->buf); 1441 cmd->off -= (end + 1 - cmd->buf);
1453 end = memchr (cmd->buf, '\n', cmd->off); 1442 end = memchr (cmd->buf, '\n', cmd->off);
1454 } 1443 }
1455 cmd->rtask = 1444 cmd->rtask =
1456 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining 1445 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1457 (cmd->timeout), cmd->r, &cmd_read, cmd); 1446 (cmd->timeout), cmd->r, &cmd_read, cmd);
1458} 1447}
1459 1448
1460 1449
@@ -1471,8 +1460,8 @@ cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1471 */ 1460 */
1472struct GNUNET_OS_CommandHandle * 1461struct GNUNET_OS_CommandHandle *
1473GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls, 1462GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1474 struct GNUNET_TIME_Relative timeout, 1463 struct GNUNET_TIME_Relative timeout, const char *binary,
1475 const char *binary, ...) 1464 ...)
1476{ 1465{
1477 struct GNUNET_OS_CommandHandle *cmd; 1466 struct GNUNET_OS_CommandHandle *cmd;
1478 struct GNUNET_OS_Process *eip; 1467 struct GNUNET_OS_Process *eip;
@@ -1486,10 +1475,10 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1486 eip = GNUNET_OS_start_process_va (NULL, opipe, binary, ap); 1475 eip = GNUNET_OS_start_process_va (NULL, opipe, binary, ap);
1487 va_end (ap); 1476 va_end (ap);
1488 if (NULL == eip) 1477 if (NULL == eip)
1489 { 1478 {
1490 GNUNET_DISK_pipe_close (opipe); 1479 GNUNET_DISK_pipe_close (opipe);
1491 return NULL; 1480 return NULL;
1492 } 1481 }
1493 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE); 1482 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1494 cmd = GNUNET_malloc (sizeof (struct GNUNET_OS_CommandHandle)); 1483 cmd = GNUNET_malloc (sizeof (struct GNUNET_OS_CommandHandle));
1495 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout); 1484 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
@@ -1498,8 +1487,7 @@ GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1498 cmd->proc = proc; 1487 cmd->proc = proc;
1499 cmd->proc_cls = proc_cls; 1488 cmd->proc_cls = proc_cls;
1500 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ); 1489 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1501 cmd->rtask = 1490 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1502 GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1503 return cmd; 1491 return cmd;
1504} 1492}
1505 1493
diff --git a/src/util/peer.c b/src/util/peer.c
index c88e03c15..2444cb9f8 100644
--- a/src/util/peer.c
+++ b/src/util/peer.c
@@ -107,7 +107,7 @@ GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
107 * @return the interned identity. 107 * @return the interned identity.
108 */ 108 */
109GNUNET_PEER_Id 109GNUNET_PEER_Id
110GNUNET_PEER_intern (const struct GNUNET_PeerIdentity * pid) 110GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
111{ 111{
112 GNUNET_PEER_Id ret; 112 GNUNET_PEER_Id ret;
113 struct PeerEntry *e; 113 struct PeerEntry *e;
@@ -121,24 +121,24 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity * pid)
121 off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey); 121 off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
122 e = (off == 0) ? NULL : &table[off]; 122 e = (off == 0) ? NULL : &table[off];
123 if (e != NULL) 123 if (e != NULL)
124 { 124 {
125 GNUNET_assert (e->rc > 0); 125 GNUNET_assert (e->rc > 0);
126 e->rc++; 126 e->rc++;
127 return e->pid; 127 return e->pid;
128 } 128 }
129 ret = free_list_start; 129 ret = free_list_start;
130 if (ret == size) 130 if (ret == size)
131 { 131 {
132 GNUNET_array_grow (table, size, size + 16); 132 GNUNET_array_grow (table, size, size + 16);
133 for (i = ret; i < size; i++) 133 for (i = ret; i < size; i++)
134 table[i].pid = i + 1; 134 table[i].pid = i + 1;
135 } 135 }
136 if (ret == 0) 136 if (ret == 0)
137 { 137 {
138 table[0].pid = 0; 138 table[0].pid = 0;
139 table[0].rc = 1; 139 table[0].rc = 1;
140 ret = 1; 140 ret = 1;
141 } 141 }
142 GNUNET_assert (ret < size); 142 GNUNET_assert (ret < size);
143 GNUNET_assert (table[ret].rc == 0); 143 GNUNET_assert (table[ret].rc == 0);
144 free_list_start = table[ret].pid; 144 free_list_start = table[ret].pid;
@@ -146,9 +146,9 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity * pid)
146 table[ret].rc = 1; 146 table[ret].rc = 1;
147 table[ret].pid = ret; 147 table[ret].pid = ret;
148 GNUNET_break (GNUNET_OK == 148 GNUNET_break (GNUNET_OK ==
149 GNUNET_CONTAINER_multihashmap_put (map, &pid->hashPubKey, 149 GNUNET_CONTAINER_multihashmap_put (map, &pid->hashPubKey,
150 (void *) (long) ret, 150 (void *) (long) ret,
151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152 return ret; 152 return ret;
153} 153}
154 154
@@ -160,7 +160,7 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity * pid)
160 * @param count size of the ids array 160 * @param count size of the ids array
161 */ 161 */
162void 162void
163GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, unsigned int count) 163GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
164{ 164{
165 int i; 165 int i;
166 GNUNET_PEER_Id id; 166 GNUNET_PEER_Id id;
@@ -168,25 +168,24 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, unsigned int count)
168 if (count == 0) 168 if (count == 0)
169 return; 169 return;
170 for (i = count - 1; i >= 0; i--) 170 for (i = count - 1; i >= 0; i--)
171 {
172 id = ids[i];
173 if (id == 0)
174 continue;
175 GNUNET_assert (id < size);
176 GNUNET_assert (table[id].rc > 0);
177 table[id].rc--;
178 if (table[id].rc == 0)
171 { 179 {
172 id = ids[i]; 180 GNUNET_break (GNUNET_OK ==
173 if (id == 0) 181 GNUNET_CONTAINER_multihashmap_remove (map,
174 continue; 182 &table[id].
175 GNUNET_assert (id < size); 183 id.hashPubKey,
176 GNUNET_assert (table[id].rc > 0); 184 (void *) (long) id));
177 table[id].rc--; 185 table[id].pid = free_list_start;
178 if (table[id].rc == 0) 186 free_list_start = id;
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 }
189 } 187 }
188 }
190} 189}
191 190
192 191
@@ -206,16 +205,15 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
206 GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta)); 205 GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta));
207 table[id].rc += delta; 206 table[id].rc += delta;
208 if (table[id].rc == 0) 207 if (table[id].rc == 0)
209 { 208 {
210 GNUNET_break (GNUNET_OK == 209 GNUNET_break (GNUNET_OK ==
211 GNUNET_CONTAINER_multihashmap_remove (map, 210 GNUNET_CONTAINER_multihashmap_remove (map,
212 &table[id]. 211 &table[id].
213 id.hashPubKey, 212 id.hashPubKey,
214 (void *) (long) 213 (void *) (long) id));
215 id)); 214 table[id].pid = free_list_start;
216 table[id].pid = free_list_start; 215 free_list_start = id;
217 free_list_start = id; 216 }
218 }
219} 217}
220 218
221 219
@@ -229,11 +227,11 @@ void
229GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) 227GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
230{ 228{
231 if (id == 0) 229 if (id == 0)
232 { 230 {
233 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity)); 231 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
234 GNUNET_break (0); 232 GNUNET_break (0);
235 return; 233 return;
236 } 234 }
237 GNUNET_assert (id < size); 235 GNUNET_assert (id < size);
238 GNUNET_assert (table[id].rc > 0); 236 GNUNET_assert (table[id].rc > 0);
239 *pid = table[id].id; 237 *pid = table[id].id;
diff --git a/src/util/perf_crypto_hash.c b/src/util/perf_crypto_hash.c
index 746b33eca..d883776b4 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 dffc8ecd6..fe5fb87f9 100644
--- a/src/util/plugin.c
+++ b/src/util/plugin.c
@@ -85,30 +85,30 @@ plugin_init ()
85 85
86 err = lt_dlinit (); 86 err = lt_dlinit ();
87 if (err > 0) 87 if (err > 0)
88 { 88 {
89 fprintf (stderr, _("Initialization of plugin mechanism failed: %s!\n"), 89 fprintf (stderr, _("Initialization of plugin mechanism failed: %s!\n"),
90 lt_dlerror ()); 90 lt_dlerror ());
91 return; 91 return;
92 } 92 }
93 opath = lt_dlgetsearchpath (); 93 opath = lt_dlgetsearchpath ();
94 if (opath != NULL) 94 if (opath != NULL)
95 old_dlsearchpath = GNUNET_strdup (opath); 95 old_dlsearchpath = GNUNET_strdup (opath);
96 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); 96 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
97 if (path != NULL) 97 if (path != NULL)
98 {
99 if (opath != NULL)
98 { 100 {
99 if (opath != NULL) 101 GNUNET_asprintf (&cpath, "%s:%s", opath, path);
100 { 102 lt_dlsetsearchpath (cpath);
101 GNUNET_asprintf (&cpath, "%s:%s", opath, path); 103 GNUNET_free (path);
102 lt_dlsetsearchpath (cpath); 104 GNUNET_free (cpath);
103 GNUNET_free (path);
104 GNUNET_free (cpath);
105 }
106 else
107 {
108 lt_dlsetsearchpath (path);
109 GNUNET_free (path);
110 }
111 } 105 }
106 else
107 {
108 lt_dlsetsearchpath (path);
109 GNUNET_free (path);
110 }
111 }
112} 112}
113 113
114 114
@@ -120,10 +120,10 @@ plugin_fini ()
120{ 120{
121 lt_dlsetsearchpath (old_dlsearchpath); 121 lt_dlsetsearchpath (old_dlsearchpath);
122 if (old_dlsearchpath != NULL) 122 if (old_dlsearchpath != NULL)
123 { 123 {
124 GNUNET_free (old_dlsearchpath); 124 GNUNET_free (old_dlsearchpath);
125 old_dlsearchpath = NULL; 125 old_dlsearchpath = NULL;
126 } 126 }
127 lt_dlexit (); 127 lt_dlexit ();
128} 128}
129 129
@@ -143,8 +143,8 @@ resolve_function (struct PluginList *plug, const char *name)
143 mptr = lt_dlsym (plug->handle, initName); 143 mptr = lt_dlsym (plug->handle, initName);
144 if (mptr == NULL) 144 if (mptr == NULL)
145 LOG (GNUNET_ERROR_TYPE_ERROR, 145 LOG (GNUNET_ERROR_TYPE_ERROR,
146 _("`%s' failed to resolve method '%s' with error: %s\n"), 146 _("`%s' failed to resolve method '%s' with error: %s\n"), "lt_dlsym",
147 "lt_dlsym", &initName[1], lt_dlerror ()); 147 &initName[1], lt_dlerror ());
148 GNUNET_free (initName); 148 GNUNET_free (initName);
149 return mptr; 149 return mptr;
150} 150}
@@ -166,10 +166,10 @@ GNUNET_PLUGIN_test (const char *library_name)
166 struct PluginList plug; 166 struct PluginList plug;
167 167
168 if (!initialized) 168 if (!initialized)
169 { 169 {
170 initialized = GNUNET_YES; 170 initialized = GNUNET_YES;
171 plugin_init (); 171 plugin_init ();
172 } 172 }
173 libhandle = lt_dlopenext (library_name); 173 libhandle = lt_dlopenext (library_name);
174 if (libhandle == NULL) 174 if (libhandle == NULL)
175 return GNUNET_NO; 175 return GNUNET_NO;
@@ -177,11 +177,11 @@ GNUNET_PLUGIN_test (const char *library_name)
177 plug.name = (char *) library_name; 177 plug.name = (char *) library_name;
178 init = resolve_function (&plug, "init"); 178 init = resolve_function (&plug, "init");
179 if (init == NULL) 179 if (init == NULL)
180 { 180 {
181 GNUNET_break (0); 181 GNUNET_break (0);
182 lt_dlclose (libhandle); 182 lt_dlclose (libhandle);
183 return GNUNET_NO; 183 return GNUNET_NO;
184 } 184 }
185 lt_dlclose (libhandle); 185 lt_dlclose (libhandle);
186 return GNUNET_YES; 186 return GNUNET_YES;
187} 187}
@@ -208,18 +208,18 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
208 void *ret; 208 void *ret;
209 209
210 if (!initialized) 210 if (!initialized)
211 { 211 {
212 initialized = GNUNET_YES; 212 initialized = GNUNET_YES;
213 plugin_init (); 213 plugin_init ();
214 } 214 }
215 libhandle = lt_dlopenext (library_name); 215 libhandle = lt_dlopenext (library_name);
216 if (libhandle == NULL) 216 if (libhandle == NULL)
217 { 217 {
218 LOG (GNUNET_ERROR_TYPE_ERROR, 218 LOG (GNUNET_ERROR_TYPE_ERROR,
219 _("`%s' failed for library `%s' with error: %s\n"), 219 _("`%s' failed for library `%s' with error: %s\n"), "lt_dlopenext",
220 "lt_dlopenext", library_name, lt_dlerror ()); 220 library_name, lt_dlerror ());
221 return NULL; 221 return NULL;
222 } 222 }
223 plug = GNUNET_malloc (sizeof (struct PluginList)); 223 plug = GNUNET_malloc (sizeof (struct PluginList));
224 plug->handle = libhandle; 224 plug->handle = libhandle;
225 plug->name = GNUNET_strdup (library_name); 225 plug->name = GNUNET_strdup (library_name);
@@ -227,13 +227,13 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
227 plugins = plug; 227 plugins = plug;
228 init = resolve_function (plug, "init"); 228 init = resolve_function (plug, "init");
229 if ((init == NULL) || (NULL == (ret = init (arg)))) 229 if ((init == NULL) || (NULL == (ret = init (arg))))
230 { 230 {
231 lt_dlclose (libhandle); 231 lt_dlclose (libhandle);
232 GNUNET_free (plug->name); 232 GNUNET_free (plug->name);
233 plugins = plug->next; 233 plugins = plug->next;
234 GNUNET_free (plug); 234 GNUNET_free (plug);
235 return NULL; 235 return NULL;
236 } 236 }
237 return ret; 237 return ret;
238} 238}
239 239
@@ -257,10 +257,10 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg)
257 prev = NULL; 257 prev = NULL;
258 pos = plugins; 258 pos = plugins;
259 while ((pos != NULL) && (0 != strcmp (pos->name, library_name))) 259 while ((pos != NULL) && (0 != strcmp (pos->name, library_name)))
260 { 260 {
261 prev = pos; 261 prev = pos;
262 pos = pos->next; 262 pos = pos->next;
263 } 263 }
264 if (pos == NULL) 264 if (pos == NULL)
265 return NULL; 265 return NULL;
266 266
@@ -276,10 +276,10 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg)
276 GNUNET_free (pos->name); 276 GNUNET_free (pos->name);
277 GNUNET_free (pos); 277 GNUNET_free (pos);
278 if (plugins == NULL) 278 if (plugins == NULL)
279 { 279 {
280 plugin_fini (); 280 plugin_fini ();
281 initialized = GNUNET_NO; 281 initialized = GNUNET_NO;
282 } 282 }
283 return ret; 283 return ret;
284} 284}
285 285
@@ -294,8 +294,7 @@ struct LoadAllContext
294 294
295 295
296static int 296static int
297find_libraries (void *cls, 297find_libraries (void *cls, const char *filename)
298 const char *filename)
299{ 298{
300 struct LoadAllContext *lac = cls; 299 struct LoadAllContext *lac = cls;
301 const char *slashpos; 300 const char *slashpos;
@@ -309,14 +308,10 @@ find_libraries (void *cls,
309 while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR))) 308 while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR)))
310 libname = slashpos + 1; 309 libname = slashpos + 1;
311 n = strlen (libname); 310 n = strlen (libname);
312 if (0 != strncmp (lac->basename, 311 if (0 != strncmp (lac->basename, libname, strlen (lac->basename)))
313 libname, 312 return GNUNET_OK; /* wrong name */
314 strlen (lac->basename))) 313 if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la")))
315 return GNUNET_OK; /* wrong name */ 314 return GNUNET_OK; /* .la file */
316 if ( (n > 3) &&
317 (0 == strcmp (&libname[n-3],
318 ".la")) )
319 return GNUNET_OK; /* .la file */
320 basename = GNUNET_strdup (libname); 315 basename = GNUNET_strdup (libname);
321 if (NULL != (dot = strstr (basename, "."))) 316 if (NULL != (dot = strstr (basename, ".")))
322 *dot = '\0'; 317 *dot = '\0';
@@ -340,11 +335,9 @@ find_libraries (void *cls,
340 * @param cb function to call for each plugin found 335 * @param cb function to call for each plugin found
341 * @param cb_cls closure for 'cb' 336 * @param cb_cls closure for 'cb'
342 */ 337 */
343void 338void
344GNUNET_PLUGIN_load_all (const char *basename, 339GNUNET_PLUGIN_load_all (const char *basename, void *arg,
345 void *arg, 340 GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
346 GNUNET_PLUGIN_LoaderCallback cb,
347 void *cb_cls)
348{ 341{
349 struct LoadAllContext lac; 342 struct LoadAllContext lac;
350 char *path; 343 char *path;
@@ -353,16 +346,14 @@ GNUNET_PLUGIN_load_all (const char *basename,
353 if (path == NULL) 346 if (path == NULL)
354 { 347 {
355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
356 _("Could not determine plugin installation path.\n")); 349 _("Could not determine plugin installation path.\n"));
357 return; 350 return;
358 } 351 }
359 lac.basename = basename; 352 lac.basename = basename;
360 lac.arg = arg; 353 lac.arg = arg;
361 lac.cb = cb; 354 lac.cb = cb;
362 lac.cb_cls = cb_cls; 355 lac.cb_cls = cb_cls;
363 GNUNET_DISK_directory_scan (path, 356 GNUNET_DISK_directory_scan (path, &find_libraries, &lac);
364 &find_libraries,
365 &lac);
366 GNUNET_free (path); 357 GNUNET_free (path);
367} 358}
368 359
diff --git a/src/util/program.c b/src/util/program.c
index 5582f30c6..6a0e5a555 100644
--- a/src/util/program.c
+++ b/src/util/program.c
@@ -129,9 +129,9 @@ cmd_sorter (__const void *a1, __const void *a2)
129 */ 129 */
130int 130int
131GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName, 131GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
132 const char *binaryHelp, 132 const char *binaryHelp,
133 const struct GNUNET_GETOPT_CommandLineOption *options, 133 const struct GNUNET_GETOPT_CommandLineOption *options,
134 GNUNET_PROGRAM_Main task, void *task_cls) 134 GNUNET_PROGRAM_Main task, void *task_cls)
135{ 135{
136 struct CommandContext cc; 136 struct CommandContext cc;
137 char *path; 137 char *path;
@@ -159,29 +159,29 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
159 logfile = NULL; 159 logfile = NULL;
160 gargs = getenv ("GNUNET_ARGS"); 160 gargs = getenv ("GNUNET_ARGS");
161 if (gargs != NULL) 161 if (gargs != NULL)
162 { 162 {
163 char **gargv; 163 char **gargv;
164 unsigned int gargc; 164 unsigned int gargc;
165 int i; 165 int i;
166 char *tok; 166 char *tok;
167 char *cargs; 167 char *cargs;
168 168
169 gargv = NULL; 169 gargv = NULL;
170 gargc = 0; 170 gargc = 0;
171 for (i = 0; i < argc; i++) 171 for (i = 0; i < argc; i++)
172 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i])); 172 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
173 cargs = GNUNET_strdup (gargs); 173 cargs = GNUNET_strdup (gargs);
174 tok = strtok (cargs, " "); 174 tok = strtok (cargs, " ");
175 while (NULL != tok) 175 while (NULL != tok)
176 { 176 {
177 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok)); 177 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
178 tok = strtok (NULL, " "); 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;
184 } 179 }
180 GNUNET_free (cargs);
181 GNUNET_array_append (gargv, gargc, NULL);
182 argv = (char *const *) gargv;
183 argc = gargc - 1;
184 }
185 memset (&cc, 0, sizeof (cc)); 185 memset (&cc, 0, sizeof (cc));
186 loglev = NULL; 186 loglev = NULL;
187 cc.task = task; 187 cc.task = task;
@@ -193,28 +193,27 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
193 setlocale (LC_ALL, ""); 193 setlocale (LC_ALL, "");
194 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); 194 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
195 if (path != NULL) 195 if (path != NULL)
196 { 196 {
197 BINDTEXTDOMAIN ("GNUnet", path); 197 BINDTEXTDOMAIN ("GNUnet", path);
198 GNUNET_free (path); 198 GNUNET_free (path);
199 } 199 }
200 textdomain ("GNUnet"); 200 textdomain ("GNUnet");
201#endif 201#endif
202 cnt = 0; 202 cnt = 0;
203 while (options[cnt].name != NULL) 203 while (options[cnt].name != NULL)
204 cnt++; 204 cnt++;
205 allopts = 205 allopts =
206 GNUNET_malloc ((cnt + 206 GNUNET_malloc ((cnt +
207 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) + 207 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
208 sizeof (defoptions)); 208 sizeof (defoptions));
209 memcpy (allopts, defoptions, sizeof (defoptions)); 209 memcpy (allopts, defoptions, sizeof (defoptions));
210 memcpy (&allopts 210 memcpy (&allopts
211 [sizeof (defoptions) / 211 [sizeof (defoptions) /
212 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options, 212 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
213 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption)); 213 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
214 cnt += 214 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
215 sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
216 qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption), 215 qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption),
217 &cmd_sorter); 216 &cmd_sorter);
218 loglev = NULL; 217 loglev = NULL;
219 cc.cfgfile = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE); 218 cc.cfgfile = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
220 lpfx = GNUNET_strdup (binaryName); 219 lpfx = GNUNET_strdup (binaryName);
@@ -222,31 +221,30 @@ GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName,
222 *spc = '\0'; 221 *spc = '\0';
223 if ((-1 == 222 if ((-1 ==
224 (ret = 223 (ret =
225 GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv))) 224 GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv))) ||
226 || (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile))) 225 (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
227 { 226 {
228 GNUNET_CONFIGURATION_destroy (cfg); 227 GNUNET_CONFIGURATION_destroy (cfg);
229 GNUNET_free_non_null (cc.cfgfile); 228 GNUNET_free_non_null (cc.cfgfile);
230 GNUNET_free_non_null (loglev); 229 GNUNET_free_non_null (loglev);
231 GNUNET_free_non_null (logfile); 230 GNUNET_free_non_null (logfile);
232 GNUNET_free (allopts); 231 GNUNET_free (allopts);
233 GNUNET_free (lpfx); 232 GNUNET_free (lpfx);
234 return GNUNET_SYSERR; 233 return GNUNET_SYSERR;
235 } 234 }
236 (void) GNUNET_CONFIGURATION_load (cfg, cc.cfgfile); 235 (void) GNUNET_CONFIGURATION_load (cfg, cc.cfgfile);
237 GNUNET_free (allopts); 236 GNUNET_free (allopts);
238 GNUNET_free (lpfx); 237 GNUNET_free (lpfx);
239 if (GNUNET_OK == 238 if (GNUNET_OK ==
240 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset", 239 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset",
241 &skew_offset) && 240 &skew_offset) &&
242 (GNUNET_OK == 241 (GNUNET_OK ==
243 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", 242 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing",
244 "skew_variance", 243 "skew_variance", &skew_variance)))
245 &skew_variance))) 244 {
246 { 245 clock_offset = skew_offset - skew_variance;
247 clock_offset = skew_offset - skew_variance; 246 GNUNET_TIME_set_offset (clock_offset);
248 GNUNET_TIME_set_offset (clock_offset); 247 }
249 }
250 /* run */ 248 /* run */
251 cc.args = &argv[ret]; 249 cc.args = &argv[ret];
252 GNUNET_SCHEDULER_run (&program_main, &cc); 250 GNUNET_SCHEDULER_run (&program_main, &cc);
diff --git a/src/util/pseudonym.c b/src/util/pseudonym.c
index b6e51af18..2a736bc46 100644
--- a/src/util/pseudonym.c
+++ b/src/util/pseudonym.c
@@ -82,16 +82,16 @@ static struct DiscoveryCallback *head;
82 */ 82 */
83static void 83static void
84internal_notify (const GNUNET_HashCode * id, 84internal_notify (const GNUNET_HashCode * id,
85 const struct GNUNET_CONTAINER_MetaData *md, int rating) 85 const struct GNUNET_CONTAINER_MetaData *md, int rating)
86{ 86{
87 struct DiscoveryCallback *pos; 87 struct DiscoveryCallback *pos;
88 88
89 pos = head; 89 pos = head;
90 while (pos != NULL) 90 while (pos != NULL)
91 { 91 {
92 pos->callback (pos->closure, id, md, rating); 92 pos->callback (pos->closure, id, md, rating);
93 pos = pos->next; 93 pos = pos->next;
94 } 94 }
95} 95}
96 96
97/** 97/**
@@ -103,10 +103,9 @@ internal_notify (const GNUNET_HashCode * id,
103 */ 103 */
104int 104int
105GNUNET_PSEUDONYM_discovery_callback_register (const struct 105GNUNET_PSEUDONYM_discovery_callback_register (const struct
106 GNUNET_CONFIGURATION_Handle 106 GNUNET_CONFIGURATION_Handle *cfg,
107 *cfg, 107 GNUNET_PSEUDONYM_Iterator
108 GNUNET_PSEUDONYM_Iterator 108 iterator, void *closure)
109 iterator, void *closure)
110{ 109{
111 struct DiscoveryCallback *list; 110 struct DiscoveryCallback *list;
112 111
@@ -126,7 +125,7 @@ GNUNET_PSEUDONYM_discovery_callback_register (const struct
126 */ 125 */
127int 126int
128GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator 127GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
129 iterator, void *closure) 128 iterator, void *closure)
130{ 129{
131 struct DiscoveryCallback *prev; 130 struct DiscoveryCallback *prev;
132 struct DiscoveryCallback *pos; 131 struct DiscoveryCallback *pos;
@@ -134,11 +133,11 @@ GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
134 prev = NULL; 133 prev = NULL;
135 pos = head; 134 pos = head;
136 while ((pos != NULL) && 135 while ((pos != NULL) &&
137 ((pos->callback != iterator) || (pos->closure != closure))) 136 ((pos->callback != iterator) || (pos->closure != closure)))
138 { 137 {
139 prev = pos; 138 prev = pos;
140 pos = pos->next; 139 pos = pos->next;
141 } 140 }
142 if (pos == NULL) 141 if (pos == NULL)
143 return GNUNET_SYSERR; 142 return GNUNET_SYSERR;
144 if (prev == NULL) 143 if (prev == NULL)
@@ -160,17 +159,16 @@ GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
160 */ 159 */
161static char * 160static char *
162get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, 161get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
163 const char *prefix, const GNUNET_HashCode * psid) 162 const char *prefix, const GNUNET_HashCode * psid)
164{ 163{
165 struct GNUNET_CRYPTO_HashAsciiEncoded enc; 164 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
166 165
167 if (psid != NULL) 166 if (psid != NULL)
168 GNUNET_CRYPTO_hash_to_enc (psid, &enc); 167 GNUNET_CRYPTO_hash_to_enc (psid, &enc);
169 return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, 168 return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix,
170 prefix, 169 (psid ==
171 (psid == 170 NULL) ? NULL : (const char *) &enc,
172 NULL) ? NULL : (const char *) &enc, 171 NULL);
173 NULL);
174} 172}
175 173
176 174
@@ -184,9 +182,9 @@ get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
184 */ 182 */
185static void 183static void
186write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg, 184write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
187 const GNUNET_HashCode * nsid, 185 const GNUNET_HashCode * nsid,
188 const struct GNUNET_CONTAINER_MetaData *meta, 186 const struct GNUNET_CONTAINER_MetaData *meta,
189 int32_t ranking, const char *ns_name) 187 int32_t ranking, const char *ns_name)
190{ 188{
191 char *fn; 189 char *fn;
192 struct GNUNET_BIO_WriteHandle *fileW; 190 struct GNUNET_BIO_WriteHandle *fileW;
@@ -195,23 +193,23 @@ write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
195 GNUNET_assert (fn != NULL); 193 GNUNET_assert (fn != NULL);
196 fileW = GNUNET_BIO_write_open (fn); 194 fileW = GNUNET_BIO_write_open (fn);
197 if (NULL != fileW) 195 if (NULL != fileW)
196 {
197 if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
198 (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
199 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
198 { 200 {
199 if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) || 201 (void) GNUNET_BIO_write_close (fileW);
200 (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) || 202 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
201 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta))) 203 GNUNET_free (fn);
202 { 204 return;
203 (void) GNUNET_BIO_write_close (fileW);
204 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
205 GNUNET_free (fn);
206 return;
207 }
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 }
214 } 205 }
206 if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
207 {
208 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
209 GNUNET_free (fn);
210 return;
211 }
212 }
215 GNUNET_free (fn); 213 GNUNET_free (fn);
216 /* create entry for pseudonym name in names */ 214 /* create entry for pseudonym name in names */
217 /* FIXME: 90% of what this call does is not needed 215 /* FIXME: 90% of what this call does is not needed
@@ -230,9 +228,9 @@ write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
230 */ 228 */
231static int 229static int
232read_info (const struct GNUNET_CONFIGURATION_Handle *cfg, 230read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
233 const GNUNET_HashCode * nsid, 231 const GNUNET_HashCode * nsid,
234 struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking, 232 struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
235 char **ns_name) 233 char **ns_name)
236{ 234{
237 char *fn; 235 char *fn;
238 char *emsg; 236 char *emsg;
@@ -242,10 +240,10 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
242 GNUNET_assert (fn != NULL); 240 GNUNET_assert (fn != NULL);
243 fileR = GNUNET_BIO_read_open (fn); 241 fileR = GNUNET_BIO_read_open (fn);
244 if (fileR == NULL) 242 if (fileR == NULL)
245 { 243 {
246 GNUNET_free (fn); 244 GNUNET_free (fn);
247 return GNUNET_SYSERR; 245 return GNUNET_SYSERR;
248 } 246 }
249 emsg = NULL; 247 emsg = NULL;
250 *ns_name = NULL; 248 *ns_name = NULL;
251 if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) || 249 if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
@@ -253,30 +251,29 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
253 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) || 251 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
254 (GNUNET_OK != 252 (GNUNET_OK !=
255 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta))) 253 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
256 { 254 {
257 (void) GNUNET_BIO_read_close (fileR, &emsg); 255 (void) GNUNET_BIO_read_close (fileR, &emsg);
258 GNUNET_free_non_null (emsg); 256 GNUNET_free_non_null (emsg);
259 GNUNET_free_non_null (*ns_name); 257 GNUNET_free_non_null (*ns_name);
260 *ns_name = NULL; 258 *ns_name = NULL;
261 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); 259 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
262 GNUNET_free (fn); 260 GNUNET_free (fn);
263 return GNUNET_SYSERR; 261 return GNUNET_SYSERR;
264 } 262 }
265 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg)) 263 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
266 { 264 {
267 LOG (GNUNET_ERROR_TYPE_WARNING, 265 LOG (GNUNET_ERROR_TYPE_WARNING,
268 _ 266 _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
269 ("Failed to parse metadata about pseudonym from file `%s': %s\n"), 267 emsg);
270 fn, emsg); 268 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
271 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn)); 269 GNUNET_CONTAINER_meta_data_destroy (*meta);
272 GNUNET_CONTAINER_meta_data_destroy (*meta); 270 *meta = NULL;
273 *meta = NULL; 271 GNUNET_free_non_null (*ns_name);
274 GNUNET_free_non_null (*ns_name); 272 *ns_name = NULL;
275 *ns_name = NULL; 273 GNUNET_free_non_null (emsg);
276 GNUNET_free_non_null (emsg); 274 GNUNET_free (fn);
277 GNUNET_free (fn); 275 return GNUNET_SYSERR;
278 return GNUNET_SYSERR; 276 }
279 }
280 GNUNET_free (fn); 277 GNUNET_free (fn);
281 return GNUNET_OK; 278 return GNUNET_OK;
282} 279}
@@ -292,7 +289,7 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
292 */ 289 */
293char * 290char *
294GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg, 291GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
295 const GNUNET_HashCode * nsid) 292 const GNUNET_HashCode * nsid)
296{ 293{
297 struct GNUNET_CONTAINER_MetaData *meta; 294 struct GNUNET_CONTAINER_MetaData *meta;
298 char *name; 295 char *name;
@@ -310,25 +307,25 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
310 meta = NULL; 307 meta = NULL;
311 name = NULL; 308 name = NULL;
312 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name)) 309 if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
310 {
311 if ((meta != NULL) && (name == NULL))
312 name =
313 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
314 EXTRACTOR_METATYPE_TITLE,
315 EXTRACTOR_METATYPE_FILENAME,
316 EXTRACTOR_METATYPE_DESCRIPTION,
317 EXTRACTOR_METATYPE_SUBJECT,
318 EXTRACTOR_METATYPE_PUBLISHER,
319 EXTRACTOR_METATYPE_AUTHOR_NAME,
320 EXTRACTOR_METATYPE_COMMENT,
321 EXTRACTOR_METATYPE_SUMMARY,
322 -1);
323 if (meta != NULL)
313 { 324 {
314 if ((meta != NULL) && (name == NULL)) 325 GNUNET_CONTAINER_meta_data_destroy (meta);
315 name = 326 meta = NULL;
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 }
331 } 327 }
328 }
332 if (name == NULL) 329 if (name == NULL)
333 name = GNUNET_strdup (_("no-name")); 330 name = GNUNET_strdup (_("no-name"));
334 GNUNET_CRYPTO_hash (name, strlen (name), &nh); 331 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
@@ -339,31 +336,31 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
339 if (0 == STAT (fn, &sbuf)) 336 if (0 == STAT (fn, &sbuf))
340 GNUNET_DISK_file_size (fn, &len, GNUNET_YES); 337 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
341 fh = GNUNET_DISK_file_open (fn, 338 fh = GNUNET_DISK_file_open (fn,
342 GNUNET_DISK_OPEN_CREATE | 339 GNUNET_DISK_OPEN_CREATE |
343 GNUNET_DISK_OPEN_READWRITE, 340 GNUNET_DISK_OPEN_READWRITE,
344 GNUNET_DISK_PERM_USER_READ | 341 GNUNET_DISK_PERM_USER_READ |
345 GNUNET_DISK_PERM_USER_WRITE); 342 GNUNET_DISK_PERM_USER_WRITE);
346 i = 0; 343 i = 0;
347 idx = -1; 344 idx = -1;
348 while ((len >= sizeof (GNUNET_HashCode)) && 345 while ((len >= sizeof (GNUNET_HashCode)) &&
349 (sizeof (GNUNET_HashCode) == 346 (sizeof (GNUNET_HashCode) ==
350 GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode)))) 347 GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
351 { 348 {
352 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode))) 349 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
353 {
354 idx = i;
355 break;
356 }
357 i++;
358 len -= sizeof (GNUNET_HashCode);
359 }
360 if (idx == -1)
361 { 350 {
362 idx = i; 351 idx = i;
363 if (sizeof (GNUNET_HashCode) != 352 break;
364 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
365 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
366 } 353 }
354 i++;
355 len -= sizeof (GNUNET_HashCode);
356 }
357 if (idx == -1)
358 {
359 idx = i;
360 if (sizeof (GNUNET_HashCode) !=
361 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
362 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
363 }
367 GNUNET_DISK_file_close (fh); 364 GNUNET_DISK_file_close (fh);
368 ret = GNUNET_malloc (strlen (name) + 32); 365 ret = GNUNET_malloc (strlen (name) + 32);
369 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx); 366 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
@@ -382,7 +379,7 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
382 */ 379 */
383int 380int
384GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg, 381GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
385 const char *ns_uname, GNUNET_HashCode * nsid) 382 const char *ns_uname, GNUNET_HashCode * nsid)
386{ 383{
387 size_t slen; 384 size_t slen;
388 uint64_t len; 385 uint64_t len;
@@ -408,24 +405,24 @@ GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
408 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) || 405 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
409 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) || 406 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
410 ((idx + 1) * sizeof (GNUNET_HashCode) > len)) 407 ((idx + 1) * sizeof (GNUNET_HashCode) > len))
411 { 408 {
412 GNUNET_free (fn); 409 GNUNET_free (fn);
413 return GNUNET_SYSERR; 410 return GNUNET_SYSERR;
414 } 411 }
415 fh = GNUNET_DISK_file_open (fn, 412 fh = GNUNET_DISK_file_open (fn,
416 GNUNET_DISK_OPEN_CREATE | 413 GNUNET_DISK_OPEN_CREATE |
417 GNUNET_DISK_OPEN_READWRITE, 414 GNUNET_DISK_OPEN_READWRITE,
418 GNUNET_DISK_PERM_USER_READ | 415 GNUNET_DISK_PERM_USER_READ |
419 GNUNET_DISK_PERM_USER_WRITE); 416 GNUNET_DISK_PERM_USER_WRITE);
420 GNUNET_free (fn); 417 GNUNET_free (fn);
421 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), 418 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
422 GNUNET_DISK_SEEK_SET); 419 GNUNET_DISK_SEEK_SET);
423 if (sizeof (GNUNET_HashCode) != 420 if (sizeof (GNUNET_HashCode) !=
424 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode))) 421 GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
425 { 422 {
426 GNUNET_DISK_file_close (fh); 423 GNUNET_DISK_file_close (fh);
427 return GNUNET_SYSERR; 424 return GNUNET_SYSERR;
428 } 425 }
429 GNUNET_DISK_file_close (fh); 426 GNUNET_DISK_file_close (fh);
430 return GNUNET_OK; 427 return GNUNET_OK;
431} 428}
@@ -475,15 +472,15 @@ list_pseudonym_helper (void *cls, const char *fullname)
475 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) 472 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
476 return GNUNET_OK; 473 return GNUNET_OK;
477 fn = &fullname[strlen (fullname) + 1 - 474 fn = &fullname[strlen (fullname) + 1 -
478 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; 475 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
479 if (fn[-1] != DIR_SEPARATOR) 476 if (fn[-1] != DIR_SEPARATOR)
480 return GNUNET_OK; 477 return GNUNET_OK;
481 ret = GNUNET_OK; 478 ret = GNUNET_OK;
482 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id)) 479 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
483 return GNUNET_OK; /* invalid name */ 480 return GNUNET_OK; /* invalid name */
484 str = NULL; 481 str = NULL;
485 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str)) 482 if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str))
486 return GNUNET_OK; /* ignore entry */ 483 return GNUNET_OK; /* ignore entry */
487 GNUNET_free_non_null (str); 484 GNUNET_free_non_null (str);
488 if (c->iterator != NULL) 485 if (c->iterator != NULL)
489 ret = c->iterator (c->closure, &id, meta, rating); 486 ret = c->iterator (c->closure, &id, meta, rating);
@@ -502,7 +499,7 @@ list_pseudonym_helper (void *cls, const char *fullname)
502 */ 499 */
503int 500int
504GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg, 501GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
505 GNUNET_PSEUDONYM_Iterator iterator, void *closure) 502 GNUNET_PSEUDONYM_Iterator iterator, void *closure)
506{ 503{
507 struct ListPseudonymClosure cls; 504 struct ListPseudonymClosure cls;
508 char *fn; 505 char *fn;
@@ -531,7 +528,7 @@ GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
531 */ 528 */
532int 529int
533GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg, 530GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
534 const GNUNET_HashCode * nsid, int delta) 531 const GNUNET_HashCode * nsid, int delta)
535{ 532{
536 struct GNUNET_CONTAINER_MetaData *meta; 533 struct GNUNET_CONTAINER_MetaData *meta;
537 int ret; 534 int ret;
@@ -541,10 +538,10 @@ GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
541 name = NULL; 538 name = NULL;
542 ret = read_info (cfg, nsid, &meta, &ranking, &name); 539 ret = read_info (cfg, nsid, &meta, &ranking, &name);
543 if (ret == GNUNET_SYSERR) 540 if (ret == GNUNET_SYSERR)
544 { 541 {
545 ranking = 0; 542 ranking = 0;
546 meta = GNUNET_CONTAINER_meta_data_create (); 543 meta = GNUNET_CONTAINER_meta_data_create ();
547 } 544 }
548 ranking += delta; 545 ranking += delta;
549 write_pseudonym_info (cfg, nsid, meta, ranking, name); 546 write_pseudonym_info (cfg, nsid, meta, ranking, name);
550 GNUNET_CONTAINER_meta_data_destroy (meta); 547 GNUNET_CONTAINER_meta_data_destroy (meta);
@@ -564,8 +561,8 @@ GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
564 */ 561 */
565void 562void
566GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg, 563GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
567 const GNUNET_HashCode * id, 564 const GNUNET_HashCode * id,
568 const struct GNUNET_CONTAINER_MetaData *meta) 565 const struct GNUNET_CONTAINER_MetaData *meta)
569{ 566{
570 char *name; 567 char *name;
571 int32_t ranking; 568 int32_t ranking;
@@ -579,16 +576,16 @@ GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
579 576
580 if ((0 == STAT (fn, &sbuf)) && 577 if ((0 == STAT (fn, &sbuf)) &&
581 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name))) 578 (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
582 { 579 {
583 GNUNET_CONTAINER_meta_data_merge (old, meta); 580 GNUNET_CONTAINER_meta_data_merge (old, meta);
584 write_pseudonym_info (cfg, id, old, ranking, name); 581 write_pseudonym_info (cfg, id, old, ranking, name);
585 GNUNET_CONTAINER_meta_data_destroy (old); 582 GNUNET_CONTAINER_meta_data_destroy (old);
586 GNUNET_free_non_null (name); 583 GNUNET_free_non_null (name);
587 } 584 }
588 else 585 else
589 { 586 {
590 write_pseudonym_info (cfg, id, meta, ranking, NULL); 587 write_pseudonym_info (cfg, id, meta, ranking, NULL);
591 } 588 }
592 GNUNET_free (fn); 589 GNUNET_free (fn);
593 internal_notify (id, meta, ranking); 590 internal_notify (id, meta, ranking);
594} 591}
diff --git a/src/util/resolver_api.c b/src/util/resolver_api.c
index 3bdc40143..03dc2ce89 100644
--- a/src/util/resolver_api.c
+++ b/src/util/resolver_api.c
@@ -196,26 +196,26 @@ check_config ()
196#endif 196#endif
197 if (GNUNET_OK != 197 if (GNUNET_OK !=
198 GNUNET_CONFIGURATION_get_value_string (resolver_cfg, "resolver", 198 GNUNET_CONFIGURATION_get_value_string (resolver_cfg, "resolver",
199 "HOSTNAME", &hostname)) 199 "HOSTNAME", &hostname))
200 { 200 {
201 LOG (GNUNET_ERROR_TYPE_ERROR, 201 LOG (GNUNET_ERROR_TYPE_ERROR,
202 _("Must specify `%s' for `%s' in configuration!\n"), "HOSTNAME", 202 _("Must specify `%s' for `%s' in configuration!\n"), "HOSTNAME",
203 "resolver"); 203 "resolver");
204 GNUNET_assert (0); 204 GNUNET_assert (0);
205 } 205 }
206 if ((1 != inet_pton (AF_INET, hostname, &v4)) || 206 if ((1 != inet_pton (AF_INET, hostname, &v4)) ||
207 (1 != inet_pton (AF_INET6, hostname, &v6))) 207 (1 != inet_pton (AF_INET6, hostname, &v6)))
208 {
209 GNUNET_free (hostname);
210 return;
211 }
212 i = 0;
213 while (loopback[i] != NULL)
214 if (0 == strcasecmp (loopback[i++], hostname))
208 { 215 {
209 GNUNET_free (hostname); 216 GNUNET_free (hostname);
210 return; 217 return;
211 } 218 }
212 i = 0;
213 while (loopback[i] != NULL)
214 if (0 == strcasecmp (loopback[i++], hostname))
215 {
216 GNUNET_free (hostname);
217 return;
218 }
219 LOG (GNUNET_ERROR_TYPE_ERROR, 219 LOG (GNUNET_ERROR_TYPE_ERROR,
220 _ 220 _
221 ("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"),
@@ -249,23 +249,23 @@ GNUNET_RESOLVER_disconnect ()
249 GNUNET_assert (NULL == req_head); 249 GNUNET_assert (NULL == req_head);
250 GNUNET_assert (NULL == req_tail); 250 GNUNET_assert (NULL == req_tail);
251 if (NULL != client) 251 if (NULL != client)
252 { 252 {
253#if DEBUG_RESOLVER 253#if DEBUG_RESOLVER
254 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n"); 254 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n");
255#endif 255#endif
256 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 256 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
257 client = NULL; 257 client = NULL;
258 } 258 }
259 if (r_task != GNUNET_SCHEDULER_NO_TASK) 259 if (r_task != GNUNET_SCHEDULER_NO_TASK)
260 { 260 {
261 GNUNET_SCHEDULER_cancel (r_task); 261 GNUNET_SCHEDULER_cancel (r_task);
262 r_task = GNUNET_SCHEDULER_NO_TASK; 262 r_task = GNUNET_SCHEDULER_NO_TASK;
263 } 263 }
264 if (s_task != GNUNET_SCHEDULER_NO_TASK) 264 if (s_task != GNUNET_SCHEDULER_NO_TASK)
265 { 265 {
266 GNUNET_SCHEDULER_cancel (s_task); 266 GNUNET_SCHEDULER_cancel (s_task);
267 s_task = GNUNET_SCHEDULER_NO_TASK; 267 s_task = GNUNET_SCHEDULER_NO_TASK;
268 } 268 }
269} 269}
270 270
271 271
@@ -286,35 +286,35 @@ no_resolve (const struct sockaddr *sa, socklen_t salen)
286 if (salen < sizeof (struct sockaddr)) 286 if (salen < sizeof (struct sockaddr))
287 return NULL; 287 return NULL;
288 switch (sa->sa_family) 288 switch (sa->sa_family)
289 {
290 case AF_INET:
291 if (salen != sizeof (struct sockaddr_in))
292 return NULL;
293 if (NULL ==
294 inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr, inet4,
295 INET_ADDRSTRLEN))
289 { 296 {
290 case AF_INET: 297 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
291 if (salen != sizeof (struct sockaddr_in)) 298 return NULL;
292 return NULL;
293 if (NULL ==
294 inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr, inet4,
295 INET_ADDRSTRLEN))
296 {
297 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
298 return NULL;
299 }
300 ret = GNUNET_strdup (inet4);
301 break;
302 case AF_INET6:
303 if (salen != sizeof (struct sockaddr_in6))
304 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;
317 } 299 }
300 ret = GNUNET_strdup (inet4);
301 break;
302 case AF_INET6:
303 if (salen != sizeof (struct sockaddr_in6))
304 return NULL;
305 if (NULL ==
306 inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr, inet6,
307 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;
317 }
318 return ret; 318 return ret;
319} 319}
320 320
@@ -322,13 +322,15 @@ 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 reconnect (); 325static void
326reconnect ();
326 327
327 328
328/** 329/**
329 * Process pending requests to the resolver. 330 * Process pending requests to the resolver.
330 */ 331 */
331static void process_requests (); 332static void
333process_requests ();
332 334
333 335
334/** 336/**
@@ -350,122 +352,120 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving response from DNS service\n"); 352 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving response from DNS service\n");
351#endif 353#endif
352 if (msg == NULL) 354 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)
353 { 366 {
354 if (NULL != rh->name_callback) 367 if (NULL != rh->name_callback)
355 LOG (GNUNET_ERROR_TYPE_INFO, 368 {
356 _("Timeout trying to resolve IP address `%s'.\n"), 369 /* no reverse lookup was successful, return ip as string */
357 GNUNET_a2s ((const void *) &rh[1], rh->data_len)); 370 if (rh->received_response == GNUNET_NO)
358 else 371 rh->name_callback (rh->cls,
359 LOG (GNUNET_ERROR_TYPE_INFO, 372 no_resolve ((const struct sockaddr *) &rh[1],
360 _("Timeout trying to resolve hostname `%s'.\n"), 373 rh->data_len));
361 (const char *) &rh[1]); 374 /* at least one reverse lookup was successful */
362 /* check if request was canceled */ 375 else
363 if (rh->was_transmitted != GNUNET_SYSERR) 376 rh->name_callback (rh->cls, NULL);
364 { 377 }
365 if (NULL != rh->name_callback) 378 if (NULL != rh->addr_callback)
366 { 379 rh->addr_callback (rh->cls, NULL, 0);
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 }
379 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
380 GNUNET_free (rh);
381 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
382 client = NULL;
383 reconnect ();
384 return;
385 } 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 }
386 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type)) 388 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
387 { 389 {
388 GNUNET_break (0); 390 GNUNET_break (0);
389 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 391 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
390 client = NULL; 392 client = NULL;
391 reconnect (); 393 reconnect ();
392 return; 394 return;
393 } 395 }
394 size = ntohs (msg->size); 396 size = ntohs (msg->size);
395 /* message contains not data, just header */ 397 /* message contains not data, just header */
396 if (size == sizeof (struct GNUNET_MessageHeader)) 398 if (size == sizeof (struct GNUNET_MessageHeader))
399 {
400 /* check if request was canceled */
401 if (rh->was_transmitted != GNUNET_SYSERR)
402 {
403 if (NULL != rh->name_callback)
404 rh->name_callback (rh->cls, NULL);
405 if (NULL != rh->addr_callback)
406 rh->addr_callback (rh->cls, NULL, 0);
407 }
408 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
409 GNUNET_free (rh);
410 process_requests ();
411 return;
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')
397 { 418 {
398 /* check if request was canceled */ 419 GNUNET_break (0);
399 if (rh->was_transmitted != GNUNET_SYSERR) 420 if (rh->was_transmitted != GNUNET_SYSERR)
400 { 421 rh->name_callback (rh->cls, NULL);
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); 422 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
407 GNUNET_free (rh); 423 GNUNET_free (rh);
408 process_requests (); 424 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
425 client = NULL;
426 reconnect ();
409 return; 427 return;
410 } 428 }
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 }
427#if DEBUG_RESOLVER 429#if DEBUG_RESOLVER
428 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s' for IP `%s'.\n"), 430 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s' for IP `%s'.\n"),
429 hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len)); 431 hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len));
430#endif 432#endif
431 if (rh->was_transmitted != GNUNET_SYSERR) 433 if (rh->was_transmitted != GNUNET_SYSERR)
432 rh->name_callback (rh->cls, hostname); 434 rh->name_callback (rh->cls, hostname);
433 rh->received_response = GNUNET_YES; 435 rh->received_response = GNUNET_YES;
434 GNUNET_CLIENT_receive (client, &handle_response, rh, 436 GNUNET_CLIENT_receive (client, &handle_response, rh,
435 GNUNET_TIME_absolute_get_remaining 437 GNUNET_TIME_absolute_get_remaining (rh->timeout));
436 (rh->timeout)); 438 }
437 }
438 /* return lookup results to caller */ 439 /* return lookup results to caller */
439 if (NULL != rh->addr_callback) 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))
440 { 445 {
441 sa = (const struct sockaddr *) &msg[1]; 446 GNUNET_break (0);
442 salen = size - sizeof (struct GNUNET_MessageHeader); 447 if (rh->was_transmitted != GNUNET_SYSERR)
443 if (salen < sizeof (struct sockaddr)) 448 rh->addr_callback (rh->cls, NULL, 0);
444 { 449 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
445 GNUNET_break (0); 450 GNUNET_free (rh);
446 if (rh->was_transmitted != GNUNET_SYSERR) 451 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
447 rh->addr_callback (rh->cls, NULL, 0); 452 client = NULL;
448 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); 453 reconnect ();
449 GNUNET_free (rh); 454 return;
450 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 455 }
451 client = NULL;
452 reconnect ();
453 return;
454 }
455#if DEBUG_RESOLVER 456#if DEBUG_RESOLVER
456 { 457 {
457 char *ips = no_resolve (sa, salen); 458 char *ips = no_resolve (sa, salen);
458 459
459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s' for `%s'.\n", 460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s' for `%s'.\n", ips,
460 ips, (const char *) &rh[1]); 461 (const char *) &rh[1]);
461 GNUNET_free (ips); 462 GNUNET_free (ips);
462 }
463#endif
464 rh->addr_callback (rh->cls, sa, salen);
465 GNUNET_CLIENT_receive (client, &handle_response, rh,
466 GNUNET_TIME_absolute_get_remaining
467 (rh->timeout));
468 } 463 }
464#endif
465 rh->addr_callback (rh->cls, sa, salen);
466 GNUNET_CLIENT_receive (client, &handle_response, rh,
467 GNUNET_TIME_absolute_get_remaining (rh->timeout));
468 }
469} 469}
470 470
471 471
@@ -498,27 +498,26 @@ 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)))
501 { 505 {
502 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); 506 /* this can happen on some systems IF "hostname" is "localhost" */
503 if ((rh->domain == AF_UNSPEC) && 507 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
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;
513 } 508 }
509 rh->addr_callback (rh->cls, NULL, 0);
510 GNUNET_free (rh);
511 return;
512 }
514 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) && 513 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) &&
515 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr))) 514 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr)))
516 { 515 {
517 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); 516 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
518 rh->addr_callback (rh->cls, NULL, 0); 517 rh->addr_callback (rh->cls, NULL, 0);
519 GNUNET_free (rh); 518 GNUNET_free (rh);
520 return; 519 return;
521 } 520 }
522 /* why are we here? this task should not have been scheduled! */ 521 /* why are we here? this task should not have been scheduled! */
523 GNUNET_assert (0); 522 GNUNET_assert (0);
524 GNUNET_free (rh); 523 GNUNET_free (rh);
@@ -553,21 +552,21 @@ loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
553#endif 552#endif
554 v6.sin6_addr = in6addr_loopback; 553 v6.sin6_addr = in6addr_loopback;
555 switch (rh->domain) 554 switch (rh->domain)
556 { 555 {
557 case AF_INET: 556 case AF_INET:
558 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); 557 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
559 break; 558 break;
560 case AF_INET6: 559 case AF_INET6:
561 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); 560 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
562 break; 561 break;
563 case AF_UNSPEC: 562 case AF_UNSPEC:
564 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6)); 563 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
565 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4)); 564 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
566 break; 565 break;
567 default: 566 default:
568 GNUNET_break (0); 567 GNUNET_break (0);
569 break; 568 break;
570 } 569 }
571 rh->addr_callback (rh->cls, NULL, 0); 570 rh->addr_callback (rh->cls, NULL, 0);
572 GNUNET_free (rh); 571 GNUNET_free (rh);
573} 572}
@@ -595,25 +594,25 @@ process_requests ()
595 struct GNUNET_RESOLVER_RequestHandle *rh; 594 struct GNUNET_RESOLVER_RequestHandle *rh;
596 595
597 if (NULL == client) 596 if (NULL == client)
598 { 597 {
599 reconnect (); 598 reconnect ();
600 return; 599 return;
601 } 600 }
602 rh = req_head; 601 rh = req_head;
603 if (NULL == rh) 602 if (NULL == rh)
604 { 603 {
605 /* nothing to do, release socket really soon if there is nothing 604 /* nothing to do, release socket really soon if there is nothing
606 * else happening... */ 605 * else happening... */
607 s_task = 606 s_task =
608 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, 607 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
609 &shutdown_task, NULL); 608 &shutdown_task, NULL);
610 return; 609 return;
611 } 610 }
612 if (GNUNET_YES == rh->was_transmitted) 611 if (GNUNET_YES == rh->was_transmitted)
613 return; /* waiting for reply */ 612 return; /* waiting for reply */
614 msg = (struct GNUNET_RESOLVER_GetMessage *) buf; 613 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
615 msg->header.size = 614 msg->header.size =
616 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + rh->data_len); 615 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + rh->data_len);
617 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST); 616 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
618 msg->direction = htonl (rh->direction); 617 msg->direction = htonl (rh->direction);
619 msg->domain = htonl (rh->domain); 618 msg->domain = htonl (rh->domain);
@@ -624,15 +623,15 @@ process_requests ()
624#endif 623#endif
625 if (GNUNET_OK != 624 if (GNUNET_OK !=
626 GNUNET_CLIENT_transmit_and_get_response (client, &msg->header, 625 GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
627 GNUNET_TIME_absolute_get_remaining 626 GNUNET_TIME_absolute_get_remaining
628 (rh->timeout), GNUNET_YES, 627 (rh->timeout), GNUNET_YES,
629 &handle_response, rh)) 628 &handle_response, rh))
630 { 629 {
631 GNUNET_CLIENT_disconnect (client, GNUNET_NO); 630 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
632 client = NULL; 631 client = NULL;
633 reconnect (); 632 reconnect ();
634 return; 633 return;
635 } 634 }
636 rh->was_transmitted = GNUNET_YES; 635 rh->was_transmitted = GNUNET_YES;
637} 636}
638 637
@@ -648,7 +647,7 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
648{ 647{
649 r_task = GNUNET_SCHEDULER_NO_TASK; 648 r_task = GNUNET_SCHEDULER_NO_TASK;
650 if (NULL == req_head) 649 if (NULL == req_head)
651 return; /* no work pending */ 650 return; /* no work pending */
652 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 651 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
653 return; 652 return;
654#if DEBUG_RESOLVER 653#if DEBUG_RESOLVER
@@ -656,12 +655,11 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
656#endif 655#endif
657 client = GNUNET_CLIENT_connect ("resolver", resolver_cfg); 656 client = GNUNET_CLIENT_connect ("resolver", resolver_cfg);
658 if (NULL == client) 657 if (NULL == client)
659 { 658 {
660 LOG (GNUNET_ERROR_TYPE_DEBUG, 659 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect, will try again later\n");
661 "Failed to connect, will try again later\n"); 660 reconnect ();
662 reconnect (); 661 return;
663 return; 662 }
664 }
665 process_requests (); 663 process_requests ();
666} 664}
667 665
@@ -678,26 +676,26 @@ reconnect ()
678 return; 676 return;
679 GNUNET_assert (NULL == client); 677 GNUNET_assert (NULL == client);
680 if (NULL != (rh = req_head)) 678 if (NULL != (rh = req_head))
679 {
680 switch (rh->was_transmitted)
681 { 681 {
682 switch (rh->was_transmitted) 682 case GNUNET_NO:
683 { 683 /* nothing more to do */
684 case GNUNET_NO: 684 break;
685 /* nothing more to do */ 685 case GNUNET_YES:
686 break; 686 /* disconnected, transmit again! */
687 case GNUNET_YES: 687 rh->was_transmitted = GNUNET_NO;
688 /* disconnected, transmit again! */ 688 break;
689 rh->was_transmitted = GNUNET_NO; 689 case GNUNET_SYSERR:
690 break; 690 /* request was cancelled, remove entirely */
691 case GNUNET_SYSERR: 691 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
692 /* request was cancelled, remove entirely */ 692 GNUNET_free (rh);
693 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); 693 break;
694 GNUNET_free (rh); 694 default:
695 break; 695 GNUNET_assert (0);
696 default: 696 break;
697 GNUNET_assert (0);
698 break;
699 }
700 } 697 }
698 }
701#if DEBUG_RESOLVER 699#if DEBUG_RESOLVER
702 LOG (GNUNET_ERROR_TYPE_DEBUG, 700 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "Will try to connect to DNS service in %llu ms\n", 701 "Will try to connect to DNS service in %llu ms\n",
@@ -721,9 +719,9 @@ reconnect ()
721 */ 719 */
722struct GNUNET_RESOLVER_RequestHandle * 720struct GNUNET_RESOLVER_RequestHandle *
723GNUNET_RESOLVER_ip_get (const char *hostname, int domain, 721GNUNET_RESOLVER_ip_get (const char *hostname, int domain,
724 struct GNUNET_TIME_Relative timeout, 722 struct GNUNET_TIME_Relative timeout,
725 GNUNET_RESOLVER_AddressCallback callback, 723 GNUNET_RESOLVER_AddressCallback callback,
726 void *callback_cls) 724 void *callback_cls)
727{ 725{
728 struct GNUNET_RESOLVER_RequestHandle *rh; 726 struct GNUNET_RESOLVER_RequestHandle *rh;
729 size_t slen; 727 size_t slen;
@@ -734,10 +732,10 @@ GNUNET_RESOLVER_ip_get (const char *hostname, int domain,
734 slen = strlen (hostname) + 1; 732 slen = strlen (hostname) + 1;
735 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= 733 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
736 GNUNET_SERVER_MAX_MESSAGE_SIZE) 734 GNUNET_SERVER_MAX_MESSAGE_SIZE)
737 { 735 {
738 GNUNET_break (0); 736 GNUNET_break (0);
739 return NULL; 737 return NULL;
740 } 738 }
741 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen); 739 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
742 rh->domain = domain; 740 rh->domain = domain;
743 rh->addr_callback = callback; 741 rh->addr_callback = callback;
@@ -751,25 +749,25 @@ GNUNET_RESOLVER_ip_get (const char *hostname, int domain,
751 ((domain == AF_INET) || (domain == AF_UNSPEC))) || 749 ((domain == AF_INET) || (domain == AF_UNSPEC))) ||
752 ((1 == inet_pton (AF_INET6, hostname, &v6)) && 750 ((1 == inet_pton (AF_INET6, hostname, &v6)) &&
753 ((domain == AF_INET6) || (domain == AF_UNSPEC)))) 751 ((domain == AF_INET6) || (domain == AF_UNSPEC))))
754 { 752 {
755 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, rh); 753 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, rh);
756 return rh; 754 return rh;
757 } 755 }
758 /* then, check if this is a loopback address */ 756 /* then, check if this is a loopback address */
759 i = 0; 757 i = 0;
760 while (loopback[i] != NULL) 758 while (loopback[i] != NULL)
761 if (0 == strcasecmp (loopback[i++], hostname)) 759 if (0 == strcasecmp (loopback[i++], hostname))
762 { 760 {
763 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh); 761 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh);
764 return rh; 762 return rh;
765 } 763 }
766 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh); 764 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh);
767 rh->was_queued = GNUNET_YES; 765 rh->was_queued = GNUNET_YES;
768 if (s_task != GNUNET_SCHEDULER_NO_TASK) 766 if (s_task != GNUNET_SCHEDULER_NO_TASK)
769 { 767 {
770 GNUNET_SCHEDULER_cancel (s_task); 768 GNUNET_SCHEDULER_cancel (s_task);
771 s_task = GNUNET_SCHEDULER_NO_TASK; 769 s_task = GNUNET_SCHEDULER_NO_TASK;
772 } 770 }
773 process_requests (); 771 process_requests ();
774 return rh; 772 return rh;
775} 773}
@@ -793,10 +791,10 @@ numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
793 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result); 791 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result);
794#endif 792#endif
795 if (result != NULL) 793 if (result != NULL)
796 { 794 {
797 rh->name_callback (rh->cls, result); 795 rh->name_callback (rh->cls, result);
798 GNUNET_free (result); 796 GNUNET_free (result);
799 } 797 }
800 rh->name_callback (rh->cls, NULL); 798 rh->name_callback (rh->cls, NULL);
801 GNUNET_free (rh); 799 GNUNET_free (rh);
802} 800}
@@ -816,10 +814,10 @@ numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
816 */ 814 */
817struct GNUNET_RESOLVER_RequestHandle * 815struct GNUNET_RESOLVER_RequestHandle *
818GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen, 816GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen,
819 int do_resolve, 817 int do_resolve,
820 struct GNUNET_TIME_Relative timeout, 818 struct GNUNET_TIME_Relative timeout,
821 GNUNET_RESOLVER_HostnameCallback callback, 819 GNUNET_RESOLVER_HostnameCallback callback,
822 void *cls) 820 void *cls)
823{ 821{
824 struct GNUNET_RESOLVER_RequestHandle *rh; 822 struct GNUNET_RESOLVER_RequestHandle *rh;
825 823
@@ -833,24 +831,24 @@ GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, socklen_t salen,
833 rh->direction = GNUNET_YES; 831 rh->direction = GNUNET_YES;
834 rh->received_response = GNUNET_NO; 832 rh->received_response = GNUNET_NO;
835 if (GNUNET_NO == do_resolve) 833 if (GNUNET_NO == do_resolve)
836 { 834 {
837 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh); 835 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh);
838 return rh; 836 return rh;
839 } 837 }
840 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= 838 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
841 GNUNET_SERVER_MAX_MESSAGE_SIZE) 839 GNUNET_SERVER_MAX_MESSAGE_SIZE)
842 { 840 {
843 GNUNET_break (0); 841 GNUNET_break (0);
844 GNUNET_free (rh); 842 GNUNET_free (rh);
845 return NULL; 843 return NULL;
846 } 844 }
847 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh); 845 GNUNET_CONTAINER_DLL_insert_tail (req_head, req_tail, rh);
848 rh->was_queued = GNUNET_YES; 846 rh->was_queued = GNUNET_YES;
849 if (s_task != GNUNET_SCHEDULER_NO_TASK) 847 if (s_task != GNUNET_SCHEDULER_NO_TASK)
850 { 848 {
851 GNUNET_SCHEDULER_cancel (s_task); 849 GNUNET_SCHEDULER_cancel (s_task);
852 s_task = GNUNET_SCHEDULER_NO_TASK; 850 s_task = GNUNET_SCHEDULER_NO_TASK;
853 } 851 }
854 process_requests (); 852 process_requests ();
855 return rh; 853 return rh;
856} 854}
@@ -868,21 +866,21 @@ GNUNET_RESOLVER_local_fqdn_get ()
868 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 866 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
869 867
870 if (0 != gethostname (hostname, sizeof (hostname) - 1)) 868 if (0 != gethostname (hostname, sizeof (hostname) - 1))
871 { 869 {
872 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 870 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
873 "gethostname"); 871 "gethostname");
874 return NULL; 872 return NULL;
875 } 873 }
876#if DEBUG_RESOLVER 874#if DEBUG_RESOLVER
877 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), hostname); 875 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), hostname);
878#endif 876#endif
879 host = gethostbyname (hostname); 877 host = gethostbyname (hostname);
880 if (NULL == host) 878 if (NULL == host)
881 { 879 {
882 LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not resolve our FQDN : %s\n"), 880 LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not resolve our FQDN : %s\n"),
883 hstrerror (h_errno)); 881 hstrerror (h_errno));
884 return NULL; 882 return NULL;
885 } 883 }
886 return GNUNET_strdup (host->h_name); 884 return GNUNET_strdup (host->h_name);
887} 885}
888 886
@@ -898,18 +896,18 @@ GNUNET_RESOLVER_local_fqdn_get ()
898 */ 896 */
899struct GNUNET_RESOLVER_RequestHandle * 897struct GNUNET_RESOLVER_RequestHandle *
900GNUNET_RESOLVER_hostname_resolve (int domain, 898GNUNET_RESOLVER_hostname_resolve (int domain,
901 struct GNUNET_TIME_Relative timeout, 899 struct GNUNET_TIME_Relative timeout,
902 GNUNET_RESOLVER_AddressCallback callback, 900 GNUNET_RESOLVER_AddressCallback callback,
903 void *cls) 901 void *cls)
904{ 902{
905 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 903 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
906 904
907 if (0 != gethostname (hostname, sizeof (hostname) - 1)) 905 if (0 != gethostname (hostname, sizeof (hostname) - 1))
908 { 906 {
909 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 907 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
910 "gethostname"); 908 "gethostname");
911 return NULL; 909 return NULL;
912 } 910 }
913#if DEBUG_RESOLVER 911#if DEBUG_RESOLVER
914 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our hostname `%s'\n"), hostname); 912 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our hostname `%s'\n"), hostname);
915#endif 913#endif
@@ -929,19 +927,19 @@ void
929GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh) 927GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
930{ 928{
931 if (rh->task != GNUNET_SCHEDULER_NO_TASK) 929 if (rh->task != GNUNET_SCHEDULER_NO_TASK)
932 { 930 {
933 GNUNET_SCHEDULER_cancel (rh->task); 931 GNUNET_SCHEDULER_cancel (rh->task);
934 rh->task = GNUNET_SCHEDULER_NO_TASK; 932 rh->task = GNUNET_SCHEDULER_NO_TASK;
935 } 933 }
936 if (rh->was_transmitted == GNUNET_NO) 934 if (rh->was_transmitted == GNUNET_NO)
937 { 935 {
938 if (rh->was_queued == GNUNET_YES) 936 if (rh->was_queued == GNUNET_YES)
939 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh); 937 GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
940 GNUNET_free (rh); 938 GNUNET_free (rh);
941 return; 939 return;
942 } 940 }
943 GNUNET_assert (rh->was_transmitted == GNUNET_YES); 941 GNUNET_assert (rh->was_transmitted == GNUNET_YES);
944 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */ 942 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
945} 943}
946 944
947 945
diff --git a/src/util/scheduler.c b/src/util/scheduler.c
index 7c5077fab..0c4374471 100644
--- a/src/util/scheduler.c
+++ b/src/util/scheduler.c
@@ -269,7 +269,7 @@ static void *scheduler_select_cls;
269 */ 269 */
270void 270void
271GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select, 271GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
272 void *new_select_cls) 272 void *new_select_cls)
273{ 273{
274 scheduler_select = new_select; 274 scheduler_select = new_select;
275 scheduler_select_cls = new_select_cls; 275 scheduler_select_cls = new_select_cls;
@@ -288,7 +288,7 @@ check_priority (enum GNUNET_SCHEDULER_Priority p)
288 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT)) 288 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
289 return p; 289 return p;
290 GNUNET_assert (0); 290 GNUNET_assert (0);
291 return 0; /* make compiler happy */ 291 return 0; /* make compiler happy */
292} 292}
293 293
294 294
@@ -310,37 +310,37 @@ is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
310 310
311 if (id < lowest_pending_id) 311 if (id < lowest_pending_id)
312 return GNUNET_NO; 312 return GNUNET_NO;
313 min = -1; /* maximum value */ 313 min = -1; /* maximum value */
314 pos = pending; 314 pos = pending;
315 while (pos != NULL) 315 while (pos != NULL)
316 { 316 {
317 if (pos->id == id) 317 if (pos->id == id)
318 return GNUNET_YES; 318 return GNUNET_YES;
319 if (pos->id < min) 319 if (pos->id < min)
320 min = pos->id; 320 min = pos->id;
321 pos = pos->next; 321 pos = pos->next;
322 } 322 }
323 pos = pending_timeout; 323 pos = pending_timeout;
324 while (pos != NULL) 324 while (pos != NULL)
325 {
326 if (pos->id == id)
327 return GNUNET_YES;
328 if (pos->id < min)
329 min = pos->id;
330 pos = pos->next;
331 }
332 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
333 {
334 pos = ready[p];
335 while (pos != NULL)
325 { 336 {
326 if (pos->id == id) 337 if (pos->id == id)
327 return GNUNET_YES; 338 return GNUNET_YES;
328 if (pos->id < min) 339 if (pos->id < min)
329 min = pos->id; 340 min = pos->id;
330 pos = pos->next; 341 pos = pos->next;
331 } 342 }
332 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++) 343 }
333 {
334 pos = ready[p];
335 while (pos != NULL)
336 {
337 if (pos->id == id)
338 return GNUNET_YES;
339 if (pos->id < min)
340 min = pos->id;
341 pos = pos->next;
342 }
343 }
344 lowest_pending_id = min; 344 lowest_pending_id = min;
345 return GNUNET_NO; 345 return GNUNET_NO;
346} 346}
@@ -355,7 +355,7 @@ is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
355 */ 355 */
356static void 356static void
357update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws, 357update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
358 struct GNUNET_TIME_Relative *timeout) 358 struct GNUNET_TIME_Relative *timeout)
359{ 359{
360 struct Task *pos; 360 struct Task *pos;
361 struct GNUNET_TIME_Absolute now; 361 struct GNUNET_TIME_Absolute now;
@@ -364,40 +364,40 @@ update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
364 now = GNUNET_TIME_absolute_get (); 364 now = GNUNET_TIME_absolute_get ();
365 pos = pending_timeout; 365 pos = pending_timeout;
366 if (pos != NULL) 366 if (pos != NULL)
367 { 367 {
368 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout); 368 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
369 if (timeout->rel_value > to.rel_value) 369 if (timeout->rel_value > to.rel_value)
370 *timeout = to; 370 *timeout = to;
371 if (pos->reason != 0) 371 if (pos->reason != 0)
372 *timeout = GNUNET_TIME_UNIT_ZERO; 372 *timeout = GNUNET_TIME_UNIT_ZERO;
373 } 373 }
374 pos = pending; 374 pos = pending;
375 while (pos != NULL) 375 while (pos != NULL)
376 {
377 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
378 (GNUNET_YES == is_pending (pos->prereq_id)))
376 { 379 {
377 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
378 (GNUNET_YES == is_pending (pos->prereq_id)))
379 {
380 pos = pos->next;
381 continue;
382 }
383 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
384 {
385 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
386 if (timeout->rel_value > to.rel_value)
387 *timeout = to;
388 }
389 if (pos->read_fd != -1)
390 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
391 if (pos->write_fd != -1)
392 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
393 if (pos->read_set != NULL)
394 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
395 if (pos->write_set != NULL)
396 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
397 if (pos->reason != 0)
398 *timeout = GNUNET_TIME_UNIT_ZERO;
399 pos = pos->next; 380 pos = pos->next;
381 continue;
382 }
383 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
384 {
385 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
386 if (timeout->rel_value > to.rel_value)
387 *timeout = to;
400 } 388 }
389 if (pos->read_fd != -1)
390 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
391 if (pos->write_fd != -1)
392 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
393 if (pos->read_set != NULL)
394 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
395 if (pos->write_set != NULL)
396 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
397 if (pos->reason != 0)
398 *timeout = GNUNET_TIME_UNIT_ZERO;
399 pos = pos->next;
400 }
401} 401}
402 402
403 403
@@ -412,17 +412,17 @@ update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
412 */ 412 */
413static int 413static int
414set_overlaps (const struct GNUNET_NETWORK_FDSet *ready, 414set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
415 struct GNUNET_NETWORK_FDSet *want) 415 struct GNUNET_NETWORK_FDSet *want)
416{ 416{
417 if ((NULL == want) || (NULL == ready)) 417 if ((NULL == want) || (NULL == ready))
418 return GNUNET_NO; 418 return GNUNET_NO;
419 if (GNUNET_NETWORK_fdset_overlap (ready, want)) 419 if (GNUNET_NETWORK_fdset_overlap (ready, want))
420 { 420 {
421 /* copy all over (yes, there maybe unrelated bits, 421 /* copy all over (yes, there maybe unrelated bits,
422 * but this should not hurt well-written clients) */ 422 * but this should not hurt well-written clients) */
423 GNUNET_NETWORK_fdset_copy (want, ready); 423 GNUNET_NETWORK_fdset_copy (want, ready);
424 return GNUNET_YES; 424 return GNUNET_YES;
425 } 425 }
426 return GNUNET_NO; 426 return GNUNET_NO;
427} 427}
428 428
@@ -439,8 +439,8 @@ set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
439 */ 439 */
440static int 440static int
441is_ready (struct Task *task, struct GNUNET_TIME_Absolute now, 441is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
442 const struct GNUNET_NETWORK_FDSet *rs, 442 const struct GNUNET_NETWORK_FDSet *rs,
443 const struct GNUNET_NETWORK_FDSet *ws) 443 const struct GNUNET_NETWORK_FDSet *ws)
444{ 444{
445 enum GNUNET_SCHEDULER_Reason reason; 445 enum GNUNET_SCHEDULER_Reason reason;
446 446
@@ -449,25 +449,25 @@ is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
449 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; 449 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
450 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) && 450 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
451 (((task->read_fd != -1) && 451 (((task->read_fd != -1) &&
452 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) 452 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
453 || (set_overlaps (rs, task->read_set)))) 453 (set_overlaps (rs, task->read_set))))
454 reason |= GNUNET_SCHEDULER_REASON_READ_READY; 454 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
455 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) && 455 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
456 (((task->write_fd != -1) && 456 (((task->write_fd != -1) &&
457 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd))) 457 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
458 || (set_overlaps (ws, task->write_set)))) 458 || (set_overlaps (ws, task->write_set))))
459 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY; 459 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
460 if (reason == 0) 460 if (reason == 0)
461 return GNUNET_NO; /* not ready */ 461 return GNUNET_NO; /* not ready */
462 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK) 462 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
463 {
464 if (GNUNET_YES == is_pending (task->prereq_id))
463 { 465 {
464 if (GNUNET_YES == is_pending (task->prereq_id)) 466 task->reason = reason;
465 { 467 return GNUNET_NO; /* prereq waiting */
466 task->reason = reason;
467 return GNUNET_NO; /* prereq waiting */
468 }
469 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
470 } 468 }
469 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
470 }
471 task->reason = reason; 471 task->reason = reason;
472 return GNUNET_YES; 472 return GNUNET_YES;
473} 473}
@@ -500,7 +500,7 @@ queue_ready_task (struct Task *task)
500 */ 500 */
501static void 501static void
502check_ready (const struct GNUNET_NETWORK_FDSet *rs, 502check_ready (const struct GNUNET_NETWORK_FDSet *rs,
503 const struct GNUNET_NETWORK_FDSet *ws) 503 const struct GNUNET_NETWORK_FDSet *ws)
504{ 504{
505 struct Task *pos; 505 struct Task *pos;
506 struct Task *prev; 506 struct Task *prev;
@@ -511,40 +511,39 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs,
511 prev = NULL; 511 prev = NULL;
512 pos = pending_timeout; 512 pos = pending_timeout;
513 while (pos != NULL) 513 while (pos != NULL)
514 { 514 {
515 next = pos->next; 515 next = pos->next;
516 if (now.abs_value >= pos->timeout.abs_value) 516 if (now.abs_value >= pos->timeout.abs_value)
517 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; 517 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
518 if (0 == pos->reason) 518 if (0 == pos->reason)
519 break; 519 break;
520 pending_timeout = next; 520 pending_timeout = next;
521 if (pending_timeout_last == pos) 521 if (pending_timeout_last == pos)
522 pending_timeout_last = NULL; 522 pending_timeout_last = NULL;
523 queue_ready_task (pos); 523 queue_ready_task (pos);
524 pos = next; 524 pos = next;
525 } 525 }
526 pos = pending; 526 pos = pending;
527 while (pos != NULL) 527 while (pos != NULL)
528 { 528 {
529#if DEBUG_TASKS 529#if DEBUG_TASKS
530 LOG (GNUNET_ERROR_TYPE_DEBUG, 530 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking readiness of task: %llu / %p\n",
531 "Checking readiness of task: %llu / %p\n", pos->id, 531 pos->id, pos->callback_cls);
532 pos->callback_cls);
533#endif 532#endif
534 next = pos->next; 533 next = pos->next;
535 if (GNUNET_YES == is_ready (pos, now, rs, ws)) 534 if (GNUNET_YES == is_ready (pos, now, rs, ws))
536 { 535 {
537 if (prev == NULL) 536 if (prev == NULL)
538 pending = next; 537 pending = next;
539 else 538 else
540 prev->next = next; 539 prev->next = next;
541 queue_ready_task (pos); 540 queue_ready_task (pos);
542 pos = next;
543 continue;
544 }
545 prev = pos;
546 pos = next; 541 pos = next;
542 continue;
547 } 543 }
544 prev = pos;
545 pos = next;
546 }
548} 547}
549 548
550 549
@@ -563,15 +562,26 @@ GNUNET_SCHEDULER_shutdown ()
563 562
564 pos = pending_timeout; 563 pos = pending_timeout;
565 while (pos != NULL) 564 while (pos != NULL)
566 { 565 {
567 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; 566 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
568 /* we don't move the task into the ready queue yet; check_ready 567 /* we don't move the task into the ready queue yet; check_ready
569 * will do that later, possibly adding additional 568 * will do that later, possibly adding additional
570 * readiness-factors */ 569 * readiness-factors */
571 pos = pos->next; 570 pos = pos->next;
572 } 571 }
573 pos = pending; 572 pos = pending;
574 while (pos != NULL) 573 while (pos != NULL)
574 {
575 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
576 /* we don't move the task into the ready queue yet; check_ready
577 * will do that later, possibly adding additional
578 * readiness-factors */
579 pos = pos->next;
580 }
581 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
582 {
583 pos = ready[i];
584 while (pos != NULL)
575 { 585 {
576 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; 586 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
577 /* we don't move the task into the ready queue yet; check_ready 587 /* we don't move the task into the ready queue yet; check_ready
@@ -579,18 +589,7 @@ GNUNET_SCHEDULER_shutdown ()
579 * readiness-factors */ 589 * readiness-factors */
580 pos = pos->next; 590 pos = pos->next;
581 } 591 }
582 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++) 592 }
583 {
584 pos = ready[i];
585 while (pos != NULL)
586 {
587 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
588 /* we don't move the task into the ready queue yet; check_ready
589 * will do that later, possibly adding additional
590 * readiness-factors */
591 pos = pos->next;
592 }
593 }
594} 593}
595 594
596 595
@@ -632,69 +631,68 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
632 631
633 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP; 632 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
634 do 633 do
634 {
635 if (ready_count == 0)
636 return;
637 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
638 /* yes, p>0 is correct, 0 is "KEEP" which should
639 * always be an empty queue (see assertion)! */
640 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
635 { 641 {
636 if (ready_count == 0) 642 pos = ready[p];
637 return; 643 if (pos != NULL)
638 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL); 644 break;
639 /* yes, p>0 is correct, 0 is "KEEP" which should 645 }
640 * always be an empty queue (see assertion)! */ 646 GNUNET_assert (pos != NULL); /* ready_count wrong? */
641 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--) 647 ready[p] = pos->next;
642 { 648 ready_count--;
643 pos = ready[p]; 649 if (current_priority != pos->priority)
644 if (pos != NULL) 650 {
645 break; 651 current_priority = pos->priority;
646 } 652 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
647 GNUNET_assert (pos != NULL); /* ready_count wrong? */ 653 pos->priority);
648 ready[p] = pos->next; 654 }
649 ready_count--; 655 current_lifeness = pos->lifeness;
650 if (current_priority != pos->priority) 656 active_task = pos;
651 {
652 current_priority = pos->priority;
653 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
654 pos->priority);
655 }
656 current_lifeness = pos->lifeness;
657 active_task = pos;
658#if PROFILE_DELAYS 657#if PROFILE_DELAYS
659 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value > 658 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
660 DELAY_THRESHOLD.rel_value) 659 DELAY_THRESHOLD.rel_value)
661 { 660 {
662 LOG (GNUNET_ERROR_TYPE_ERROR, 661 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu took %llums to be scheduled\n",
663 "Task %llu took %llums to be scheduled\n", pos->id, 662 pos->id,
664 (unsigned long long) 663 (unsigned long long)
665 GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value); 664 GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
666 } 665 }
667#endif 666#endif
668 tc.reason = pos->reason; 667 tc.reason = pos->reason;
669 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set; 668 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
670 if ((pos->read_fd != -1) && 669 if ((pos->read_fd != -1) &&
671 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY))) 670 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
672 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd); 671 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
673 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set; 672 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
674 if ((pos->write_fd != -1) && 673 if ((pos->write_fd != -1) &&
675 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))) 674 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
676 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd); 675 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
677 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) && 676 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
678 (pos->write_fd != -1) && 677 (pos->write_fd != -1) &&
679 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd))) 678 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
680 abort (); // added to ready in previous select loop! 679 abort (); // added to ready in previous select loop!
681#if DEBUG_TASKS 680#if DEBUG_TASKS
682 LOG (GNUNET_ERROR_TYPE_DEBUG, 681 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running task: %llu / %p\n", pos->id,
683 "Running task: %llu / %p\n", pos->id, pos->callback_cls); 682 pos->callback_cls);
684#endif 683#endif
685 pos->callback (pos->callback_cls, &tc); 684 pos->callback (pos->callback_cls, &tc);
686#if EXECINFO 685#if EXECINFO
687 int i; 686 int i;
688 687
689 for (i = 0; i < pos->num_backtrace_strings; i++) 688 for (i = 0; i < pos->num_backtrace_strings; i++)
690 LOG (GNUNET_ERROR_TYPE_ERROR, 689 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu trace %d: %s\n", pos->id, i,
691 "Task %llu trace %d: %s\n", pos->id, 690 pos->backtrace_strings[i]);
692 i, pos->backtrace_strings[i]);
693#endif 691#endif
694 active_task = NULL; 692 active_task = NULL;
695 destroy_task (pos); 693 destroy_task (pos);
696 tasks_run++; 694 tasks_run++;
697 } 695 }
698 while ((pending == NULL) || (p >= max_priority_added)); 696 while ((pending == NULL) || (p >= max_priority_added));
699} 697}
700 698
@@ -720,11 +718,11 @@ static void
720sighandler_shutdown () 718sighandler_shutdown ()
721{ 719{
722 static char c; 720 static char c;
723 int old_errno = errno; /* backup errno */ 721 int old_errno = errno; /* backup errno */
724 722
725 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle 723 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
726 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), 724 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
727 &c, sizeof (c)); 725 &c, sizeof (c));
728 errno = old_errno; 726 errno = old_errno;
729} 727}
730 728
@@ -750,10 +748,10 @@ check_lifeness ()
750 if (t->lifeness == GNUNET_YES) 748 if (t->lifeness == GNUNET_YES)
751 return GNUNET_OK; 749 return GNUNET_OK;
752 if ((NULL != pending) || (NULL != pending_timeout)) 750 if ((NULL != pending) || (NULL != pending_timeout))
753 { 751 {
754 GNUNET_SCHEDULER_shutdown (); 752 GNUNET_SCHEDULER_shutdown ();
755 return GNUNET_OK; 753 return GNUNET_OK;
756 } 754 }
757 return GNUNET_NO; 755 return GNUNET_NO;
758} 756}
759 757
@@ -799,7 +797,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
799 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO); 797 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
800 GNUNET_assert (shutdown_pipe_handle != NULL); 798 GNUNET_assert (shutdown_pipe_handle != NULL);
801 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, 799 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
802 GNUNET_DISK_PIPE_END_READ); 800 GNUNET_DISK_PIPE_END_READ);
803 GNUNET_assert (pr != NULL); 801 GNUNET_assert (pr != NULL);
804 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown); 802 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
805 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown); 803 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
@@ -811,77 +809,76 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
811 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT; 809 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
812 current_lifeness = GNUNET_YES; 810 current_lifeness = GNUNET_YES;
813 GNUNET_SCHEDULER_add_continuation (task, task_cls, 811 GNUNET_SCHEDULER_add_continuation (task, task_cls,
814 GNUNET_SCHEDULER_REASON_STARTUP); 812 GNUNET_SCHEDULER_REASON_STARTUP);
815#if ENABLE_WINDOWS_WORKAROUNDS 813#if ENABLE_WINDOWS_WORKAROUNDS
816 active_task = (void *) (long) -1; /* force passing of sanity check */ 814 active_task = (void *) (long) -1; /* force passing of sanity check */
817 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO, 815 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
818 &GNUNET_OS_install_parent_control_handler, 816 &GNUNET_OS_install_parent_control_handler,
819 NULL); 817 NULL);
820 active_task = NULL; 818 active_task = NULL;
821#endif 819#endif
822 last_tr = 0; 820 last_tr = 0;
823 busy_wait_warning = 0; 821 busy_wait_warning = 0;
824 while (GNUNET_OK == check_lifeness ()) 822 while (GNUNET_OK == check_lifeness ())
823 {
824 GNUNET_NETWORK_fdset_zero (rs);
825 GNUNET_NETWORK_fdset_zero (ws);
826 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
827 update_sets (rs, ws, &timeout);
828 GNUNET_NETWORK_fdset_handle_set (rs, pr);
829 if (ready_count > 0)
825 { 830 {
826 GNUNET_NETWORK_fdset_zero (rs); 831 /* no blocking, more work already ready! */
827 GNUNET_NETWORK_fdset_zero (ws); 832 timeout = GNUNET_TIME_UNIT_ZERO;
828 timeout = GNUNET_TIME_UNIT_FOREVER_REL; 833 }
829 update_sets (rs, ws, &timeout); 834 if (NULL == scheduler_select)
830 GNUNET_NETWORK_fdset_handle_set (rs, pr); 835 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
831 if (ready_count > 0) 836 else
832 { 837 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
833 /* no blocking, more work already ready! */ 838 if (ret == GNUNET_SYSERR)
834 timeout = GNUNET_TIME_UNIT_ZERO; 839 {
835 } 840 if (errno == EINTR)
836 if (NULL == scheduler_select) 841 continue;
837 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
838 else
839 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
840 if (ret == GNUNET_SYSERR)
841 {
842 if (errno == EINTR)
843 continue;
844 842
845 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select"); 843 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
846#ifndef MINGW 844#ifndef MINGW
847#if USE_LSOF 845#if USE_LSOF
848 char lsof[512]; 846 char lsof[512];
849 847
850 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ()); 848 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
851 (void) close (1); 849 (void) close (1);
852 (void) dup2 (2, 1); 850 (void) dup2 (2, 1);
853 if (0 != system (lsof)) 851 if (0 != system (lsof))
854 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system"); 852 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
855#endif 853#endif
856#endif 854#endif
857 abort (); 855 abort ();
858 break; 856 break;
859 } 857 }
860 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16)) 858 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
861 { 859 {
862 LOG (GNUNET_ERROR_TYPE_WARNING, 860 LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n"));
863 _("Looks like we're busy waiting...\n")); 861 sleep (1); /* mitigate */
864 sleep (1); /* mitigate */ 862 }
865 } 863 check_ready (rs, ws);
866 check_ready (rs, ws); 864 run_ready (rs, ws);
867 run_ready (rs, ws); 865 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
868 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr)) 866 {
869 { 867 /* consume the signal */
870 /* consume the signal */ 868 GNUNET_DISK_file_read (pr, &c, sizeof (c));
871 GNUNET_DISK_file_read (pr, &c, sizeof (c)); 869 /* mark all active tasks as ready due to shutdown */
872 /* mark all active tasks as ready due to shutdown */ 870 GNUNET_SCHEDULER_shutdown ();
873 GNUNET_SCHEDULER_shutdown (); 871 }
874 } 872 if (last_tr == tasks_run)
875 if (last_tr == tasks_run) 873 {
876 { 874 busy_wait_warning++;
877 busy_wait_warning++; 875 }
878 } 876 else
879 else 877 {
880 { 878 last_tr = tasks_run;
881 last_tr = tasks_run; 879 busy_wait_warning = 0;
882 busy_wait_warning = 0;
883 }
884 } 880 }
881 }
885 GNUNET_SIGNAL_handler_uninstall (shc_int); 882 GNUNET_SIGNAL_handler_uninstall (shc_int);
886 GNUNET_SIGNAL_handler_uninstall (shc_term); 883 GNUNET_SIGNAL_handler_uninstall (shc_term);
887#ifndef MINGW 884#ifndef MINGW
@@ -934,10 +931,10 @@ GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
934 ret = 0; 931 ret = 0;
935 pos = ready[check_priority (p)]; 932 pos = ready[check_priority (p)];
936 while (pos != NULL) 933 while (pos != NULL)
937 { 934 {
938 pos = pos->next; 935 pos = pos->next;
939 ret++; 936 ret++;
940 } 937 }
941 return ret; 938 return ret;
942} 939}
943 940
@@ -963,77 +960,76 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
963 prev = NULL; 960 prev = NULL;
964 t = pending; 961 t = pending;
965 while (t != NULL) 962 while (t != NULL)
963 {
964 if (t->id == task)
965 break;
966 prev = t;
967 t = t->next;
968 }
969 if (t == NULL)
970 {
971 prev = NULL;
972 to = 1;
973 t = pending_timeout;
974 while (t != NULL)
966 { 975 {
967 if (t->id == task) 976 if (t->id == task)
968 break; 977 break;
969 prev = t; 978 prev = t;
970 t = t->next; 979 t = t->next;
971 } 980 }
972 if (t == NULL) 981 if (pending_timeout_last == t)
973 { 982 pending_timeout_last = NULL;
974 prev = NULL; 983 }
975 to = 1;
976 t = pending_timeout;
977 while (t != NULL)
978 {
979 if (t->id == task)
980 break;
981 prev = t;
982 t = t->next;
983 }
984 if (pending_timeout_last == t)
985 pending_timeout_last = NULL;
986 }
987 p = 0; 984 p = 0;
988 while (t == NULL) 985 while (t == NULL)
986 {
987 p++;
988 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
989 { 989 {
990 p++; 990 LOG (GNUNET_ERROR_TYPE_ERROR, _("Attempt to cancel dead task %llu!\n"),
991 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT) 991 (unsigned long long) task);
992 { 992 GNUNET_assert (0);
993 LOG (GNUNET_ERROR_TYPE_ERROR,
994 _("Attempt to cancel dead task %llu!\n"),
995 (unsigned long long) task);
996 GNUNET_assert (0);
997 }
998 prev = NULL;
999 t = ready[p];
1000 while (t != NULL)
1001 {
1002 if (t->id == task)
1003 {
1004 ready_count--;
1005 break;
1006 }
1007 prev = t;
1008 t = t->next;
1009 }
1010 } 993 }
994 prev = NULL;
995 t = ready[p];
996 while (t != NULL)
997 {
998 if (t->id == task)
999 {
1000 ready_count--;
1001 break;
1002 }
1003 prev = t;
1004 t = t->next;
1005 }
1006 }
1011 if (prev == NULL) 1007 if (prev == NULL)
1008 {
1009 if (p == 0)
1012 { 1010 {
1013 if (p == 0) 1011 if (to == 0)
1014 { 1012 {
1015 if (to == 0) 1013 pending = t->next;
1016 { 1014 }
1017 pending = t->next;
1018 }
1019 else
1020 {
1021 pending_timeout = t->next;
1022 }
1023 }
1024 else 1015 else
1025 { 1016 {
1026 ready[p] = t->next; 1017 pending_timeout = t->next;
1027 } 1018 }
1028 } 1019 }
1029 else 1020 else
1030 { 1021 {
1031 prev->next = t->next; 1022 ready[p] = t->next;
1032 } 1023 }
1024 }
1025 else
1026 {
1027 prev->next = t->next;
1028 }
1033 ret = t->callback_cls; 1029 ret = t->callback_cls;
1034#if DEBUG_TASKS 1030#if DEBUG_TASKS
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, 1031 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
1036 "Canceling task: %llu / %p\n", task, t->callback_cls); 1032 t->callback_cls);
1037#endif 1033#endif
1038 destroy_task (t); 1034 destroy_task (t);
1039 return ret; 1035 return ret;
@@ -1051,7 +1047,7 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
1051 */ 1047 */
1052void 1048void
1053GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls, 1049GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1054 enum GNUNET_SCHEDULER_Reason reason) 1050 enum GNUNET_SCHEDULER_Reason reason)
1055{ 1051{
1056 struct Task *t; 1052 struct Task *t;
1057 1053
@@ -1061,12 +1057,12 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1061 1057
1062 GNUNET_assert (NULL != task); 1058 GNUNET_assert (NULL != task);
1063 GNUNET_assert ((active_task != NULL) || 1059 GNUNET_assert ((active_task != NULL) ||
1064 (reason == GNUNET_SCHEDULER_REASON_STARTUP)); 1060 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1065 t = GNUNET_malloc (sizeof (struct Task)); 1061 t = GNUNET_malloc (sizeof (struct Task));
1066#if EXECINFO 1062#if EXECINFO
1067 t->num_backtrace_strings = backtrace (backtrace_array, 50); 1063 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1068 t->backtrace_strings = 1064 t->backtrace_strings =
1069 backtrace_symbols (backtrace_array, t->num_backtrace_strings); 1065 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1070#endif 1066#endif
1071 t->read_fd = -1; 1067 t->read_fd = -1;
1072 t->write_fd = -1; 1068 t->write_fd = -1;
@@ -1080,8 +1076,8 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1080 t->priority = current_priority; 1076 t->priority = current_priority;
1081 t->lifeness = current_lifeness; 1077 t->lifeness = current_lifeness;
1082#if DEBUG_TASKS 1078#if DEBUG_TASKS
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, 1079 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n", t->id,
1084 "Adding continuation task: %llu / %p\n", t->id, t->callback_cls); 1080 t->callback_cls);
1085#endif 1081#endif
1086 queue_ready_task (t); 1082 queue_ready_task (t);
1087} 1083}
@@ -1106,12 +1102,11 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1106 */ 1102 */
1107GNUNET_SCHEDULER_TaskIdentifier 1103GNUNET_SCHEDULER_TaskIdentifier
1108GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, 1104GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1109 GNUNET_SCHEDULER_Task task, void *task_cls) 1105 GNUNET_SCHEDULER_Task task, void *task_cls)
1110{ 1106{
1111 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, 1107 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1112 prerequisite_task, 1108 prerequisite_task, GNUNET_TIME_UNIT_ZERO,
1113 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, 1109 NULL, NULL, task, task_cls);
1114 task_cls);
1115} 1110}
1116 1111
1117 1112
@@ -1126,12 +1121,11 @@ GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1126 */ 1121 */
1127GNUNET_SCHEDULER_TaskIdentifier 1122GNUNET_SCHEDULER_TaskIdentifier
1128GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio, 1123GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1129 GNUNET_SCHEDULER_Task task, 1124 GNUNET_SCHEDULER_Task task, void *task_cls)
1130 void *task_cls)
1131{ 1125{
1132 return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK, 1126 return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK,
1133 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, 1127 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1134 task_cls); 1128 task_cls);
1135} 1129}
1136 1130
1137 1131
@@ -1150,7 +1144,7 @@ GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1150 */ 1144 */
1151GNUNET_SCHEDULER_TaskIdentifier 1145GNUNET_SCHEDULER_TaskIdentifier
1152GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay, 1146GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1153 GNUNET_SCHEDULER_Task task, void *task_cls) 1147 GNUNET_SCHEDULER_Task task, void *task_cls)
1154{ 1148{
1155#if 1 1149#if 1
1156 /* new, optimized version */ 1150 /* new, optimized version */
@@ -1170,7 +1164,7 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1170#if EXECINFO 1164#if EXECINFO
1171 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH); 1165 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1172 t->backtrace_strings = 1166 t->backtrace_strings =
1173 backtrace_symbols (backtrace_array, t->num_backtrace_strings); 1167 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1174#endif 1168#endif
1175 t->read_fd = -1; 1169 t->read_fd = -1;
1176 t->write_fd = -1; 1170 t->write_fd = -1;
@@ -1185,24 +1179,24 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1185 * appending to a long list of tasks with timeouts) */ 1179 * appending to a long list of tasks with timeouts) */
1186 prev = pending_timeout_last; 1180 prev = pending_timeout_last;
1187 if (prev != NULL) 1181 if (prev != NULL)
1188 { 1182 {
1189 if (prev->timeout.abs_value > t->timeout.abs_value) 1183 if (prev->timeout.abs_value > t->timeout.abs_value)
1190 prev = NULL; 1184 prev = NULL;
1191 else 1185 else
1192 pos = prev->next; /* heuristic success! */ 1186 pos = prev->next; /* heuristic success! */
1193 } 1187 }
1194 if (prev == NULL) 1188 if (prev == NULL)
1195 { 1189 {
1196 /* heuristic failed, do traversal of timeout list */ 1190 /* heuristic failed, do traversal of timeout list */
1197 pos = pending_timeout; 1191 pos = pending_timeout;
1198 } 1192 }
1199 while ((pos != NULL) && 1193 while ((pos != NULL) &&
1200 ((pos->timeout.abs_value <= t->timeout.abs_value) || 1194 ((pos->timeout.abs_value <= t->timeout.abs_value) ||
1201 (pos->reason != 0))) 1195 (pos->reason != 0)))
1202 { 1196 {
1203 prev = pos; 1197 prev = pos;
1204 pos = pos->next; 1198 pos = pos->next;
1205 } 1199 }
1206 if (prev == NULL) 1200 if (prev == NULL)
1207 pending_timeout = t; 1201 pending_timeout = t;
1208 else 1202 else
@@ -1212,23 +1206,23 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1212 pending_timeout_last = t; 1206 pending_timeout_last = t;
1213 1207
1214#if DEBUG_TASKS 1208#if DEBUG_TASKS
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, 1209 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1216 "Adding task: %llu / %p\n", t->id, t->callback_cls); 1210 t->callback_cls);
1217#endif 1211#endif
1218#if EXECINFO 1212#if EXECINFO
1219 int i; 1213 int i;
1220 1214
1221 for (i = 0; i < t->num_backtrace_strings; i++) 1215 for (i = 0; i < t->num_backtrace_strings; i++)
1222 LOG (GNUNET_ERROR_TYPE_DEBUG, 1216 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1223 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]); 1217 t->backtrace_strings[i]);
1224#endif 1218#endif
1225 return t->id; 1219 return t->id;
1226 1220
1227#else 1221#else
1228 /* unoptimized version */ 1222 /* unoptimized version */
1229 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, 1223 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1230 GNUNET_SCHEDULER_NO_TASK, delay, NULL, 1224 GNUNET_SCHEDULER_NO_TASK, delay, NULL,
1231 NULL, task, task_cls); 1225 NULL, task, task_cls);
1232#endif 1226#endif
1233} 1227}
1234 1228
@@ -1247,9 +1241,9 @@ GNUNET_SCHEDULER_TaskIdentifier
1247GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls) 1241GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1248{ 1242{
1249 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, 1243 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1250 GNUNET_SCHEDULER_NO_TASK, 1244 GNUNET_SCHEDULER_NO_TASK,
1251 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, 1245 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1252 task_cls); 1246 task_cls);
1253} 1247}
1254 1248
1255 1249
@@ -1269,16 +1263,16 @@ GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1269 */ 1263 */
1270GNUNET_SCHEDULER_TaskIdentifier 1264GNUNET_SCHEDULER_TaskIdentifier
1271GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness, 1265GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1272 GNUNET_SCHEDULER_Task task, 1266 GNUNET_SCHEDULER_Task task,
1273 void *task_cls) 1267 void *task_cls)
1274{ 1268{
1275 GNUNET_SCHEDULER_TaskIdentifier ret; 1269 GNUNET_SCHEDULER_TaskIdentifier ret;
1276 1270
1277 ret = 1271 ret =
1278 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP, 1272 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1279 GNUNET_SCHEDULER_NO_TASK, 1273 GNUNET_SCHEDULER_NO_TASK,
1280 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task, 1274 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1281 task_cls); 1275 task_cls);
1282 GNUNET_assert (pending->id == ret); 1276 GNUNET_assert (pending->id == ret);
1283 pending->lifeness = lifeness; 1277 pending->lifeness = lifeness;
1284 return ret; 1278 return ret;
@@ -1316,7 +1310,7 @@ GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1316#ifndef MINGW 1310#ifndef MINGW
1317GNUNET_SCHEDULER_TaskIdentifier 1311GNUNET_SCHEDULER_TaskIdentifier
1318add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd, 1312add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1319 GNUNET_SCHEDULER_Task task, void *task_cls) 1313 GNUNET_SCHEDULER_Task task, void *task_cls)
1320{ 1314{
1321 struct Task *t; 1315 struct Task *t;
1322 1316
@@ -1332,45 +1326,41 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1332#if EXECINFO 1326#if EXECINFO
1333 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH); 1327 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1334 t->backtrace_strings = 1328 t->backtrace_strings =
1335 backtrace_symbols (backtrace_array, t->num_backtrace_strings); 1329 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1336#endif 1330#endif
1337#if DEBUG_FDS 1331#if DEBUG_FDS
1338 if (-1 != rfd) 1332 if (-1 != rfd)
1339 { 1333 {
1340 int flags = fcntl (rfd, F_GETFD); 1334 int flags = fcntl (rfd, F_GETFD);
1341 1335
1342 if ((flags == -1) && (errno == EBADF)) 1336 if ((flags == -1) && (errno == EBADF))
1343 { 1337 {
1344 LOG (GNUNET_ERROR_TYPE_ERROR, 1338 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1345 "Got invalid file descriptor %d!\n", rfd);
1346#if EXECINFO 1339#if EXECINFO
1347 int i; 1340 int i;
1348 1341
1349 for (i = 0; i < t->num_backtrace_strings; i++) 1342 for (i = 0; i < t->num_backtrace_strings; i++)
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, 1343 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1351 "Trace: %s\n", t->backtrace_strings[i]);
1352#endif 1344#endif
1353 GNUNET_assert (0); 1345 GNUNET_assert (0);
1354 }
1355 } 1346 }
1347 }
1356 if (-1 != wfd) 1348 if (-1 != wfd)
1357 { 1349 {
1358 int flags = fcntl (wfd, F_GETFD); 1350 int flags = fcntl (wfd, F_GETFD);
1359 1351
1360 if (flags == -1 && errno == EBADF) 1352 if (flags == -1 && errno == EBADF)
1361 { 1353 {
1362 LOG (GNUNET_ERROR_TYPE_ERROR, 1354 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1363 "Got invalid file descriptor %d!\n", wfd);
1364#if EXECINFO 1355#if EXECINFO
1365 int i; 1356 int i;
1366 1357
1367 for (i = 0; i < t->num_backtrace_strings; i++) 1358 for (i = 0; i < t->num_backtrace_strings; i++)
1368 LOG (GNUNET_ERROR_TYPE_DEBUG, 1359 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1369 "Trace: %s\n", t->backtrace_strings[i]);
1370#endif 1360#endif
1371 GNUNET_assert (0); 1361 GNUNET_assert (0);
1372 }
1373 } 1362 }
1363 }
1374#endif 1364#endif
1375 t->read_fd = rfd; 1365 t->read_fd = rfd;
1376 GNUNET_assert (wfd >= -1); 1366 GNUNET_assert (wfd >= -1);
@@ -1387,15 +1377,15 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1387 pending = t; 1377 pending = t;
1388 max_priority_added = GNUNET_MAX (max_priority_added, t->priority); 1378 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1389#if DEBUG_TASKS 1379#if DEBUG_TASKS
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, 1380 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1391 "Adding task: %llu / %p\n", t->id, t->callback_cls); 1381 t->callback_cls);
1392#endif 1382#endif
1393#if EXECINFO 1383#if EXECINFO
1394 int i; 1384 int i;
1395 1385
1396 for (i = 0; i < t->num_backtrace_strings; i++) 1386 for (i = 0; i < t->num_backtrace_strings; i++)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, 1387 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1398 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]); 1388 t->backtrace_strings[i]);
1399#endif 1389#endif
1400 return t->id; 1390 return t->id;
1401} 1391}
@@ -1421,8 +1411,8 @@ add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1421 */ 1411 */
1422GNUNET_SCHEDULER_TaskIdentifier 1412GNUNET_SCHEDULER_TaskIdentifier
1423GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay, 1413GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1424 struct GNUNET_NETWORK_Handle * rfd, 1414 struct GNUNET_NETWORK_Handle *rfd,
1425 GNUNET_SCHEDULER_Task task, void *task_cls) 1415 GNUNET_SCHEDULER_Task task, void *task_cls)
1426{ 1416{
1427#if MINGW 1417#if MINGW
1428 struct GNUNET_NETWORK_FDSet *rs; 1418 struct GNUNET_NETWORK_FDSet *rs;
@@ -1432,14 +1422,14 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1432 rs = GNUNET_NETWORK_fdset_create (); 1422 rs = GNUNET_NETWORK_fdset_create ();
1433 GNUNET_NETWORK_fdset_set (rs, rfd); 1423 GNUNET_NETWORK_fdset_set (rs, rfd);
1434 ret = 1424 ret =
1435 GNUNET_SCHEDULER_add_select (check_priority (current_priority), 1425 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1436 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL, 1426 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1437 task, task_cls); 1427 task, task_cls);
1438 GNUNET_NETWORK_fdset_destroy (rs); 1428 GNUNET_NETWORK_fdset_destroy (rs);
1439 return ret; 1429 return ret;
1440#else 1430#else
1441 return add_without_sets (delay, GNUNET_NETWORK_get_fd (rfd), -1, task, 1431 return add_without_sets (delay, GNUNET_NETWORK_get_fd (rfd), -1, task,
1442 task_cls); 1432 task_cls);
1443#endif 1433#endif
1444} 1434}
1445 1435
@@ -1462,8 +1452,8 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1462 */ 1452 */
1463GNUNET_SCHEDULER_TaskIdentifier 1453GNUNET_SCHEDULER_TaskIdentifier
1464GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay, 1454GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1465 struct GNUNET_NETWORK_Handle * wfd, 1455 struct GNUNET_NETWORK_Handle *wfd,
1466 GNUNET_SCHEDULER_Task task, void *task_cls) 1456 GNUNET_SCHEDULER_Task task, void *task_cls)
1467{ 1457{
1468#if MINGW 1458#if MINGW
1469 struct GNUNET_NETWORK_FDSet *ws; 1459 struct GNUNET_NETWORK_FDSet *ws;
@@ -1473,15 +1463,15 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1473 ws = GNUNET_NETWORK_fdset_create (); 1463 ws = GNUNET_NETWORK_fdset_create ();
1474 GNUNET_NETWORK_fdset_set (ws, wfd); 1464 GNUNET_NETWORK_fdset_set (ws, wfd);
1475 ret = 1465 ret =
1476 GNUNET_SCHEDULER_add_select (check_priority (current_priority), 1466 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1477 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws, 1467 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1478 task, task_cls); 1468 task, task_cls);
1479 GNUNET_NETWORK_fdset_destroy (ws); 1469 GNUNET_NETWORK_fdset_destroy (ws);
1480 return ret; 1470 return ret;
1481#else 1471#else
1482 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0); 1472 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1483 return add_without_sets (delay, -1, GNUNET_NETWORK_get_fd (wfd), task, 1473 return add_without_sets (delay, -1, GNUNET_NETWORK_get_fd (wfd), task,
1484 task_cls); 1474 task_cls);
1485#endif 1475#endif
1486} 1476}
1487 1477
@@ -1504,8 +1494,8 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1504 */ 1494 */
1505GNUNET_SCHEDULER_TaskIdentifier 1495GNUNET_SCHEDULER_TaskIdentifier
1506GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay, 1496GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1507 const struct GNUNET_DISK_FileHandle * rfd, 1497 const struct GNUNET_DISK_FileHandle *rfd,
1508 GNUNET_SCHEDULER_Task task, void *task_cls) 1498 GNUNET_SCHEDULER_Task task, void *task_cls)
1509{ 1499{
1510#if MINGW 1500#if MINGW
1511 struct GNUNET_NETWORK_FDSet *rs; 1501 struct GNUNET_NETWORK_FDSet *rs;
@@ -1515,9 +1505,9 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1515 rs = GNUNET_NETWORK_fdset_create (); 1505 rs = GNUNET_NETWORK_fdset_create ();
1516 GNUNET_NETWORK_fdset_handle_set (rs, rfd); 1506 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1517 ret = 1507 ret =
1518 GNUNET_SCHEDULER_add_select (check_priority (current_priority), 1508 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1519 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL, 1509 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1520 task, task_cls); 1510 task, task_cls);
1521 GNUNET_NETWORK_fdset_destroy (rs); 1511 GNUNET_NETWORK_fdset_destroy (rs);
1522 return ret; 1512 return ret;
1523#else 1513#else
@@ -1548,8 +1538,8 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1548 */ 1538 */
1549GNUNET_SCHEDULER_TaskIdentifier 1539GNUNET_SCHEDULER_TaskIdentifier
1550GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay, 1540GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1551 const struct GNUNET_DISK_FileHandle * wfd, 1541 const struct GNUNET_DISK_FileHandle *wfd,
1552 GNUNET_SCHEDULER_Task task, void *task_cls) 1542 GNUNET_SCHEDULER_Task task, void *task_cls)
1553{ 1543{
1554#if MINGW 1544#if MINGW
1555 struct GNUNET_NETWORK_FDSet *ws; 1545 struct GNUNET_NETWORK_FDSet *ws;
@@ -1559,9 +1549,9 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1559 ws = GNUNET_NETWORK_fdset_create (); 1549 ws = GNUNET_NETWORK_fdset_create ();
1560 GNUNET_NETWORK_fdset_handle_set (ws, wfd); 1550 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1561 ret = 1551 ret =
1562 GNUNET_SCHEDULER_add_select (check_priority (current_priority), 1552 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1563 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws, 1553 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1564 task, task_cls); 1554 task, task_cls);
1565 GNUNET_NETWORK_fdset_destroy (ws); 1555 GNUNET_NETWORK_fdset_destroy (ws);
1566 return ret; 1556 return ret;
1567#else 1557#else
@@ -1610,12 +1600,11 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1610 */ 1600 */
1611GNUNET_SCHEDULER_TaskIdentifier 1601GNUNET_SCHEDULER_TaskIdentifier
1612GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio, 1602GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1613 GNUNET_SCHEDULER_TaskIdentifier 1603 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1614 prerequisite_task, 1604 struct GNUNET_TIME_Relative delay,
1615 struct GNUNET_TIME_Relative delay, 1605 const struct GNUNET_NETWORK_FDSet *rs,
1616 const struct GNUNET_NETWORK_FDSet * rs, 1606 const struct GNUNET_NETWORK_FDSet *ws,
1617 const struct GNUNET_NETWORK_FDSet * ws, 1607 GNUNET_SCHEDULER_Task task, void *task_cls)
1618 GNUNET_SCHEDULER_Task task, void *task_cls)
1619{ 1608{
1620 struct Task *t; 1609 struct Task *t;
1621 1610
@@ -1631,20 +1620,20 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1631#if EXECINFO 1620#if EXECINFO
1632 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH); 1621 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1633 t->backtrace_strings = 1622 t->backtrace_strings =
1634 backtrace_symbols (backtrace_array, t->num_backtrace_strings); 1623 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1635#endif 1624#endif
1636 t->read_fd = -1; 1625 t->read_fd = -1;
1637 t->write_fd = -1; 1626 t->write_fd = -1;
1638 if (rs != NULL) 1627 if (rs != NULL)
1639 { 1628 {
1640 t->read_set = GNUNET_NETWORK_fdset_create (); 1629 t->read_set = GNUNET_NETWORK_fdset_create ();
1641 GNUNET_NETWORK_fdset_copy (t->read_set, rs); 1630 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1642 } 1631 }
1643 if (ws != NULL) 1632 if (ws != NULL)
1644 { 1633 {
1645 t->write_set = GNUNET_NETWORK_fdset_create (); 1634 t->write_set = GNUNET_NETWORK_fdset_create ();
1646 GNUNET_NETWORK_fdset_copy (t->write_set, ws); 1635 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1647 } 1636 }
1648 t->id = ++last_id; 1637 t->id = ++last_id;
1649#if PROFILE_DELAYS 1638#if PROFILE_DELAYS
1650 t->start_time = GNUNET_TIME_absolute_get (); 1639 t->start_time = GNUNET_TIME_absolute_get ();
@@ -1652,23 +1641,23 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1652 t->prereq_id = prerequisite_task; 1641 t->prereq_id = prerequisite_task;
1653 t->timeout = GNUNET_TIME_relative_to_absolute (delay); 1642 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1654 t->priority = 1643 t->priority =
1655 check_priority ((prio == 1644 check_priority ((prio ==
1656 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : 1645 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1657 prio); 1646 prio);
1658 t->lifeness = current_lifeness; 1647 t->lifeness = current_lifeness;
1659 t->next = pending; 1648 t->next = pending;
1660 pending = t; 1649 pending = t;
1661 max_priority_added = GNUNET_MAX (max_priority_added, t->priority); 1650 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1662#if DEBUG_TASKS 1651#if DEBUG_TASKS
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, 1652 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1664 "Adding task: %llu / %p\n", t->id, t->callback_cls); 1653 t->callback_cls);
1665#endif 1654#endif
1666#if EXECINFO 1655#if EXECINFO
1667 int i; 1656 int i;
1668 1657
1669 for (i = 0; i < t->num_backtrace_strings; i++) 1658 for (i = 0; i < t->num_backtrace_strings; i++)
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, 1659 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1671 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]); 1660 t->backtrace_strings[i]);
1672#endif 1661#endif
1673 return t->id; 1662 return t->id;
1674} 1663}
diff --git a/src/util/server.c b/src/util/server.c
index f792e5a67..6f1b8cdb4 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -264,8 +264,7 @@ struct GNUNET_SERVER_Client
264 * @param tc reason why we are running right now 264 * @param tc reason why we are running right now
265 */ 265 */
266static void 266static void
267process_listen_socket (void *cls, 267process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
268 const struct GNUNET_SCHEDULER_TaskContext *tc)
269{ 268{
270 struct GNUNET_SERVER_Handle *server = cls; 269 struct GNUNET_SERVER_Handle *server = cls;
271 struct GNUNET_CONNECTION_Handle *sock; 270 struct GNUNET_CONNECTION_Handle *sock;
@@ -279,47 +278,45 @@ process_listen_socket (void *cls,
279 while (NULL != server->listen_sockets[i]) 278 while (NULL != server->listen_sockets[i])
280 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]); 279 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
281 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 280 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
282 { 281 {
283 /* ignore shutdown, someone else will take care of it! */ 282 /* ignore shutdown, someone else will take care of it! */
284 server->listen_task = 283 server->listen_task =
285 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 284 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
286 GNUNET_SCHEDULER_NO_TASK, 285 GNUNET_SCHEDULER_NO_TASK,
287 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, 286 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
288 &process_listen_socket, server); 287 &process_listen_socket, server);
289 GNUNET_NETWORK_fdset_destroy (r); 288 GNUNET_NETWORK_fdset_destroy (r);
290 return; 289 return;
291 } 290 }
292 i = 0; 291 i = 0;
293 while (NULL != server->listen_sockets[i]) 292 while (NULL != server->listen_sockets[i])
293 {
294 if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i]))
294 { 295 {
295 if (GNUNET_NETWORK_fdset_isset 296 sock =
296 (tc->read_ready, server->listen_sockets[i])) 297 GNUNET_CONNECTION_create_from_accept (server->access,
297 { 298 server->access_cls,
298 sock = 299 server->listen_sockets[i]);
299 GNUNET_CONNECTION_create_from_accept (server->access, 300 if (sock != NULL)
300 server->access_cls, 301 {
301 server->listen_sockets[i]);
302 if (sock != NULL)
303 {
304#if DEBUG_SERVER 302#if DEBUG_SERVER
305 LOG (GNUNET_ERROR_TYPE_DEBUG, 303 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n");
306 "Server accepted incoming connection.\n");
307#endif 304#endif
308 client = GNUNET_SERVER_connect_socket (server, sock); 305 client = GNUNET_SERVER_connect_socket (server, sock);
309 GNUNET_CONNECTION_ignore_shutdown (sock, 306 GNUNET_CONNECTION_ignore_shutdown (sock,
310 server->clients_ignore_shutdown); 307 server->clients_ignore_shutdown);
311 /* decrement reference count, we don't keep "client" alive */ 308 /* decrement reference count, we don't keep "client" alive */
312 GNUNET_SERVER_client_drop (client); 309 GNUNET_SERVER_client_drop (client);
313 } 310 }
314 }
315 i++;
316 } 311 }
312 i++;
313 }
317 /* listen for more! */ 314 /* listen for more! */
318 server->listen_task = 315 server->listen_task =
319 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 316 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
320 GNUNET_SCHEDULER_NO_TASK, 317 GNUNET_SCHEDULER_NO_TASK,
321 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, 318 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
322 &process_listen_socket, server); 319 &process_listen_socket, server);
323 GNUNET_NETWORK_fdset_destroy (r); 320 GNUNET_NETWORK_fdset_destroy (r);
324} 321}
325 322
@@ -340,87 +337,87 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
340 int eno; 337 int eno;
341 338
342 switch (serverAddr->sa_family) 339 switch (serverAddr->sa_family)
343 { 340 {
344 case AF_INET: 341 case AF_INET:
345 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port); 342 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
346 break; 343 break;
347 case AF_INET6: 344 case AF_INET6:
348 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port); 345 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
349 break; 346 break;
350 case AF_UNIX: 347 case AF_UNIX:
351 port = 0; 348 port = 0;
352 break; 349 break;
353 default: 350 default:
354 GNUNET_break (0); 351 GNUNET_break (0);
355 port = 0; 352 port = 0;
356 break; 353 break;
357 } 354 }
358 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0); 355 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
359 if (NULL == sock) 356 if (NULL == sock)
360 { 357 {
361 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); 358 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
362 errno = 0; 359 errno = 0;
363 return NULL; 360 return NULL;
364 } 361 }
365 if (port != 0) 362 if (port != 0)
366 { 363 {
367 if (GNUNET_NETWORK_socket_setsockopt 364 if (GNUNET_NETWORK_socket_setsockopt
368 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 365 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
369 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 366 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
370 "setsockopt"); 367 "setsockopt");
371#ifdef IPV6_V6ONLY 368#ifdef IPV6_V6ONLY
372 if ((serverAddr->sa_family == AF_INET6) && 369 if ((serverAddr->sa_family == AF_INET6) &&
373 (GNUNET_NETWORK_socket_setsockopt 370 (GNUNET_NETWORK_socket_setsockopt
374 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) 371 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
375 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 372 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
376 "setsockopt"); 373 "setsockopt");
377#endif 374#endif
378 } 375 }
379 /* bind the socket */ 376 /* bind the socket */
380 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK) 377 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
378 {
379 eno = errno;
380 if (errno != EADDRINUSE)
381 { 381 {
382 eno = errno; 382 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
383 if (errno != EADDRINUSE) 383 * fail if we already took the port on IPv6; if both IPv4 and
384 { 384 * IPv6 binds fail, then our caller will log using the
385 /* we don't log 'EADDRINUSE' here since an IPv4 bind may 385 * errno preserved in 'eno' */
386 * fail if we already took the port on IPv6; if both IPv4 and 386 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
387 * IPv6 binds fail, then our caller will log using the 387 if (port != 0)
388 * errno preserved in 'eno' */ 388 LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"),
389 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind"); 389 "bind", port,
390 if (port != 0) 390 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
391 LOG (GNUNET_ERROR_TYPE_ERROR, 391 eno = 0;
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;
412 } 392 }
413 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5)) 393 else
414 { 394 {
415 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen"); 395 if (port != 0)
416 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); 396 LOG (GNUNET_ERROR_TYPE_WARNING,
417 errno = 0; 397 _("`%s' failed for port %d (%s): address already in use\n"),
418 return NULL; 398 "bind", port,
399 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
400 else if (serverAddr->sa_family == AF_UNIX)
401 LOG (GNUNET_ERROR_TYPE_WARNING,
402 _("`%s' failed for `%s': address already in use\n"), "bind",
403 ((const struct sockaddr_un *) serverAddr)->sun_path);
404
419 } 405 }
406 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
407 errno = eno;
408 return NULL;
409 }
410 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
411 {
412 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
413 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
414 errno = 0;
415 return NULL;
416 }
420#if DEBUG_SERVER 417#if DEBUG_SERVER
421 if (port != 0) 418 if (port != 0)
422 LOG (GNUNET_ERROR_TYPE_DEBUG, 419 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n",
423 "Server starts to listen on port %u.\n", port); 420 port);
424#endif 421#endif
425 return sock; 422 return sock;
426} 423}
@@ -440,10 +437,10 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
440 */ 437 */
441struct GNUNET_SERVER_Handle * 438struct GNUNET_SERVER_Handle *
442GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, 439GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
443 void *access_cls, 440 void *access_cls,
444 struct GNUNET_NETWORK_Handle **lsocks, 441 struct GNUNET_NETWORK_Handle **lsocks,
445 struct GNUNET_TIME_Relative idle_timeout, 442 struct GNUNET_TIME_Relative idle_timeout,
446 int require_found) 443 int require_found)
447{ 444{
448 struct GNUNET_SERVER_Handle *ret; 445 struct GNUNET_SERVER_Handle *ret;
449 struct GNUNET_NETWORK_FDSet *r; 446 struct GNUNET_NETWORK_FDSet *r;
@@ -456,18 +453,18 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
456 ret->access_cls = access_cls; 453 ret->access_cls = access_cls;
457 ret->require_found = require_found; 454 ret->require_found = require_found;
458 if (lsocks != NULL) 455 if (lsocks != NULL)
459 { 456 {
460 r = GNUNET_NETWORK_fdset_create (); 457 r = GNUNET_NETWORK_fdset_create ();
461 i = 0; 458 i = 0;
462 while (NULL != ret->listen_sockets[i]) 459 while (NULL != ret->listen_sockets[i])
463 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]); 460 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
464 ret->listen_task = 461 ret->listen_task =
465 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 462 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
466 GNUNET_SCHEDULER_NO_TASK, 463 GNUNET_SCHEDULER_NO_TASK,
467 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, 464 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
468 &process_listen_socket, ret); 465 &process_listen_socket, ret);
469 GNUNET_NETWORK_fdset_destroy (r); 466 GNUNET_NETWORK_fdset_destroy (r);
470 } 467 }
471 return ret; 468 return ret;
472} 469}
473 470
@@ -487,10 +484,10 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
487 */ 484 */
488struct GNUNET_SERVER_Handle * 485struct GNUNET_SERVER_Handle *
489GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls, 486GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
490 struct sockaddr *const *serverAddr, 487 struct sockaddr *const *serverAddr,
491 const socklen_t * socklen, 488 const socklen_t * socklen,
492 struct GNUNET_TIME_Relative idle_timeout, 489 struct GNUNET_TIME_Relative idle_timeout,
493 int require_found) 490 int require_found)
494{ 491{
495 struct GNUNET_NETWORK_Handle **lsocks; 492 struct GNUNET_NETWORK_Handle **lsocks;
496 unsigned int i; 493 unsigned int i;
@@ -500,32 +497,31 @@ GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
500 while (serverAddr[i] != NULL) 497 while (serverAddr[i] != NULL)
501 i++; 498 i++;
502 if (i > 0) 499 if (i > 0)
500 {
501 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
502 i = 0;
503 j = 0;
504 while (serverAddr[i] != NULL)
503 { 505 {
504 lsocks = 506 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
505 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1)); 507 if (lsocks[j] != NULL)
506 i = 0; 508 j++;
507 j = 0; 509 i++;
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 }
522 } 510 }
523 else 511 if (j == 0)
524 { 512 {
513 if (errno != 0)
514 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
515 GNUNET_free (lsocks);
525 lsocks = NULL; 516 lsocks = NULL;
526 } 517 }
518 }
519 else
520 {
521 lsocks = NULL;
522 }
527 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks, 523 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks,
528 idle_timeout, require_found); 524 idle_timeout, require_found);
529} 525}
530 526
531 527
@@ -545,32 +541,32 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n"); 541 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
546#endif 542#endif
547 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task) 543 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
548 { 544 {
549 GNUNET_SCHEDULER_cancel (s->listen_task); 545 GNUNET_SCHEDULER_cancel (s->listen_task);
550 s->listen_task = GNUNET_SCHEDULER_NO_TASK; 546 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
551 } 547 }
552 if (s->listen_sockets != NULL) 548 if (s->listen_sockets != NULL)
553 { 549 {
554 i = 0; 550 i = 0;
555 while (s->listen_sockets[i] != NULL) 551 while (s->listen_sockets[i] != NULL)
556 GNUNET_break (GNUNET_OK == 552 GNUNET_break (GNUNET_OK ==
557 GNUNET_NETWORK_socket_close (s->listen_sockets[i++])); 553 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
558 GNUNET_free (s->listen_sockets); 554 GNUNET_free (s->listen_sockets);
559 s->listen_sockets = NULL; 555 s->listen_sockets = NULL;
560 } 556 }
561 while (s->clients != NULL) 557 while (s->clients != NULL)
562 GNUNET_SERVER_client_disconnect (s->clients); 558 GNUNET_SERVER_client_disconnect (s->clients);
563 while (NULL != (hpos = s->handlers)) 559 while (NULL != (hpos = s->handlers))
564 { 560 {
565 s->handlers = hpos->next; 561 s->handlers = hpos->next;
566 GNUNET_free (hpos); 562 GNUNET_free (hpos);
567 } 563 }
568 while (NULL != (npos = s->disconnect_notify_list)) 564 while (NULL != (npos = s->disconnect_notify_list))
569 { 565 {
570 npos->callback (npos->callback_cls, NULL); 566 npos->callback (npos->callback_cls, NULL);
571 s->disconnect_notify_list = npos->next; 567 s->disconnect_notify_list = npos->next;
572 GNUNET_free (npos); 568 GNUNET_free (npos);
573 } 569 }
574 GNUNET_free (s); 570 GNUNET_free (s);
575} 571}
576 572
@@ -590,8 +586,7 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
590 */ 586 */
591void 587void
592GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server, 588GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
593 const struct GNUNET_SERVER_MessageHandler 589 const struct GNUNET_SERVER_MessageHandler *handlers)
594 *handlers)
595{ 590{
596 struct HandlerList *p; 591 struct HandlerList *p;
597 592
@@ -609,21 +604,20 @@ GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
609 * @param tc scheduler context (unused) 604 * @param tc scheduler context (unused)
610 */ 605 */
611static void 606static void
612warn_no_receive_done (void *cls, 607warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
613 const struct GNUNET_SCHEDULER_TaskContext *tc)
614{ 608{
615 struct GNUNET_SERVER_Client *client = cls; 609 struct GNUNET_SERVER_Client *client = cls;
616 610
617 client->warn_task = 611 client->warn_task =
618 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 612 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
619 &warn_no_receive_done, client); 613 &warn_no_receive_done, client);
620 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) 614 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
621 LOG (GNUNET_ERROR_TYPE_WARNING, 615 LOG (GNUNET_ERROR_TYPE_WARNING,
622 _ 616 _
623 ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"), 617 ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
624 (unsigned int) client->warn_type, 618 (unsigned int) client->warn_type,
625 (unsigned long long) 619 (unsigned long long)
626 GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value); 620 GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
627} 621}
628 622
629 623
@@ -635,14 +629,13 @@ warn_no_receive_done (void *cls,
635 * @param client client for which to disable the warning 629 * @param client client for which to disable the warning
636 */ 630 */
637void 631void
638GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client 632GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
639 *client)
640{ 633{
641 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) 634 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
642 { 635 {
643 GNUNET_SCHEDULER_cancel (client->warn_task); 636 GNUNET_SCHEDULER_cancel (client->warn_task);
644 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 637 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
645 } 638 }
646} 639}
647 640
648 641
@@ -663,8 +656,8 @@ GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
663 */ 656 */
664int 657int
665GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, 658GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
666 struct GNUNET_SERVER_Client *sender, 659 struct GNUNET_SERVER_Client *sender,
667 const struct GNUNET_MessageHeader *message) 660 const struct GNUNET_MessageHeader *message)
668{ 661{
669 struct HandlerList *pos; 662 struct HandlerList *pos;
670 const struct GNUNET_SERVER_MessageHandler *mh; 663 const struct GNUNET_SERVER_MessageHandler *mh;
@@ -684,50 +677,49 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
684 pos = server->handlers; 677 pos = server->handlers;
685 found = GNUNET_NO; 678 found = GNUNET_NO;
686 while (pos != NULL) 679 while (pos != NULL)
680 {
681 i = 0;
682 while (pos->handlers[i].callback != NULL)
687 { 683 {
688 i = 0; 684 mh = &pos->handlers[i];
689 while (pos->handlers[i].callback != NULL) 685 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
690 { 686 {
691 mh = &pos->handlers[i]; 687 if ((mh->expected_size != 0) && (mh->expected_size != size))
692 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL)) 688 {
693 {
694 if ((mh->expected_size != 0) && (mh->expected_size != size))
695 {
696#if GNUNET8_NETWORK_IS_DEAD 689#if GNUNET8_NETWORK_IS_DEAD
697 LOG (GNUNET_ERROR_TYPE_WARNING, 690 LOG (GNUNET_ERROR_TYPE_WARNING,
698 "Expected %u bytes for message of type %u, got %u\n", 691 "Expected %u bytes for message of type %u, got %u\n",
699 mh->expected_size, mh->type, size); 692 mh->expected_size, mh->type, size);
700 GNUNET_break_op (0); 693 GNUNET_break_op (0);
701#endif 694#endif
702 return GNUNET_SYSERR; 695 return GNUNET_SYSERR;
703 } 696 }
704 if (sender != NULL) 697 if (sender != NULL)
705 { 698 {
706 if (0 == sender->suspended) 699 if (0 == sender->suspended)
707 { 700 {
708 sender->warn_start = GNUNET_TIME_absolute_get (); 701 sender->warn_start = GNUNET_TIME_absolute_get ();
709 sender->warn_task = 702 sender->warn_task =
710 GNUNET_SCHEDULER_add_delayed 703 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
711 (GNUNET_TIME_UNIT_MINUTES, &warn_no_receive_done, 704 &warn_no_receive_done, sender);
712 sender); 705 sender->warn_type = type;
713 sender->warn_type = type; 706 }
714 } 707 sender->suspended++;
715 sender->suspended++; 708 }
716 } 709 mh->callback (mh->callback_cls, sender, message);
717 mh->callback (mh->callback_cls, sender, message); 710 found = GNUNET_YES;
718 found = GNUNET_YES; 711 }
719 } 712 i++;
720 i++;
721 }
722 pos = pos->next;
723 } 713 }
714 pos = pos->next;
715 }
724 if (found == GNUNET_NO) 716 if (found == GNUNET_NO)
725 { 717 {
726 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 718 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
727 "Received message of unknown type %d\n", type); 719 "Received message of unknown type %d\n", type);
728 if (server->require_found == GNUNET_YES) 720 if (server->require_found == GNUNET_YES)
729 return GNUNET_SYSERR; 721 return GNUNET_SYSERR;
730 } 722 }
731 return GNUNET_OK; 723 return GNUNET_OK;
732} 724}
733 725
@@ -744,8 +736,7 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
744 */ 736 */
745static void 737static void
746process_incoming (void *cls, const void *buf, size_t available, 738process_incoming (void *cls, const void *buf, size_t available,
747 const struct sockaddr *addr, socklen_t addrlen, 739 const struct sockaddr *addr, socklen_t addrlen, int errCode);
748 int errCode);
749 740
750 741
751/** 742/**
@@ -765,44 +756,44 @@ static void
765process_mst (struct GNUNET_SERVER_Client *client, int ret) 756process_mst (struct GNUNET_SERVER_Client *client, int ret)
766{ 757{
767 while ((ret != GNUNET_SYSERR) && (client->server != NULL) && 758 while ((ret != GNUNET_SYSERR) && (client->server != NULL) &&
768 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended)) 759 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
760 {
761 if (ret == GNUNET_OK)
769 { 762 {
770 if (ret == GNUNET_OK) 763 client->receive_pending = GNUNET_YES;
771 {
772 client->receive_pending = GNUNET_YES;
773#if DEBUG_SERVER
774 LOG (GNUNET_ERROR_TYPE_DEBUG,
775 "Server re-enters receive loop, timeout: %llu.\n",
776 client->idle_timeout.rel_value);
777#endif
778 GNUNET_CONNECTION_receive (client->connection,
779 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
780 client->idle_timeout, &process_incoming,
781 client);
782 break;
783 }
784#if DEBUG_SERVER 764#if DEBUG_SERVER
785 LOG (GNUNET_ERROR_TYPE_DEBUG, 765 LOG (GNUNET_ERROR_TYPE_DEBUG,
786 "Server processes additional messages instantly.\n"); 766 "Server re-enters receive loop, timeout: %llu.\n",
767 client->idle_timeout.rel_value);
787#endif 768#endif
788 ret = 769 GNUNET_CONNECTION_receive (client->connection,
789 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, 770 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
790 GNUNET_YES); 771 client->idle_timeout, &process_incoming,
772 client);
773 break;
791 } 774 }
792#if DEBUG_SERVER 775#if DEBUG_SERVER
776 LOG (GNUNET_ERROR_TYPE_DEBUG,
777 "Server processes additional messages instantly.\n");
778#endif
779 ret =
780 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
781 GNUNET_YES);
782 }
783#if DEBUG_SERVER
793 LOG (GNUNET_ERROR_TYPE_DEBUG, 784 LOG (GNUNET_ERROR_TYPE_DEBUG,
794 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n", 785 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
795 ret, client->server, client->shutdown_now, client->suspended); 786 ret, client->server, client->shutdown_now, client->suspended);
796#endif 787#endif
797 788
798 if (ret == GNUNET_NO) 789 if (ret == GNUNET_NO)
799 { 790 {
800#if DEBUG_SERVER 791#if DEBUG_SERVER
801 LOG (GNUNET_ERROR_TYPE_DEBUG, 792 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 "Server has more data pending but is suspended.\n"); 793 "Server has more data pending but is suspended.\n");
803#endif 794#endif
804 client->receive_pending = GNUNET_SYSERR; /* data pending */ 795 client->receive_pending = GNUNET_SYSERR; /* data pending */
805 } 796 }
806 if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now)) 797 if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now))
807 GNUNET_SERVER_client_disconnect (client); 798 GNUNET_SERVER_client_disconnect (client);
808 GNUNET_SERVER_client_drop (client); 799 GNUNET_SERVER_client_drop (client);
@@ -821,7 +812,7 @@ process_mst (struct GNUNET_SERVER_Client *client, int ret)
821 */ 812 */
822static void 813static void
823process_incoming (void *cls, const void *buf, size_t available, 814process_incoming (void *cls, const void *buf, size_t available,
824 const struct sockaddr *addr, socklen_t addrlen, int errCode) 815 const struct sockaddr *addr, socklen_t addrlen, int errCode)
825{ 816{
826 struct GNUNET_SERVER_Client *client = cls; 817 struct GNUNET_SERVER_Client *client = cls;
827 struct GNUNET_SERVER_Handle *server = client->server; 818 struct GNUNET_SERVER_Handle *server = client->server;
@@ -832,35 +823,34 @@ process_incoming (void *cls, const void *buf, size_t available,
832 GNUNET_assert (client->receive_pending == GNUNET_YES); 823 GNUNET_assert (client->receive_pending == GNUNET_YES);
833 client->receive_pending = GNUNET_NO; 824 client->receive_pending = GNUNET_NO;
834 now = GNUNET_TIME_absolute_get (); 825 now = GNUNET_TIME_absolute_get ();
835 end = 826 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
836 GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
837 827
838 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) && 828 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) &&
839 (client->shutdown_now != GNUNET_YES) && (server != NULL) && 829 (client->shutdown_now != GNUNET_YES) && (server != NULL) &&
840 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) && 830 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
841 (end.abs_value > now.abs_value)) 831 (end.abs_value > now.abs_value))
842 { 832 {
843 /* wait longer, timeout changed (i.e. due to us sending) */ 833 /* wait longer, timeout changed (i.e. due to us sending) */
844#if DEBUG_SERVER 834#if DEBUG_SERVER
845 LOG (GNUNET_ERROR_TYPE_DEBUG, 835 LOG (GNUNET_ERROR_TYPE_DEBUG,
846 "Receive time out, but no disconnect due to sending (%p)\n", 836 "Receive time out, but no disconnect due to sending (%p)\n",
847 GNUNET_a2s (addr, addrlen)); 837 GNUNET_a2s (addr, addrlen));
848#endif 838#endif
849 client->receive_pending = GNUNET_YES; 839 client->receive_pending = GNUNET_YES;
850 GNUNET_CONNECTION_receive (client->connection, 840 GNUNET_CONNECTION_receive (client->connection,
851 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 841 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
852 GNUNET_TIME_absolute_get_remaining (end), 842 GNUNET_TIME_absolute_get_remaining (end),
853 &process_incoming, client); 843 &process_incoming, client);
854 return; 844 return;
855 } 845 }
856 if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) 846 if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) ||
857 || (client->shutdown_now == GNUNET_YES) 847 (client->shutdown_now == GNUNET_YES) ||
858 || (GNUNET_YES != GNUNET_CONNECTION_check (client->connection))) 848 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
859 { 849 {
860 /* other side closed connection, error connecting, etc. */ 850 /* other side closed connection, error connecting, etc. */
861 GNUNET_SERVER_client_disconnect (client); 851 GNUNET_SERVER_client_disconnect (client);
862 return; 852 return;
863 } 853 }
864#if DEBUG_SERVER 854#if DEBUG_SERVER
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n", 855 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n",
866 (unsigned int) available, GNUNET_a2s (addr, addrlen)); 856 (unsigned int) available, GNUNET_a2s (addr, addrlen));
@@ -868,8 +858,8 @@ process_incoming (void *cls, const void *buf, size_t available,
868 GNUNET_SERVER_client_keep (client); 858 GNUNET_SERVER_client_keep (client);
869 client->last_activity = now; 859 client->last_activity = now;
870 ret = 860 ret =
871 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, 861 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
872 GNUNET_YES); 862 GNUNET_YES);
873 process_mst (client, ret); 863 process_mst (client, ret);
874} 864}
875 865
@@ -890,23 +880,21 @@ restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
890 client->restart_task = GNUNET_SCHEDULER_NO_TASK; 880 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
891 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && 881 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
892 (GNUNET_NO == server->clients_ignore_shutdown)) 882 (GNUNET_NO == server->clients_ignore_shutdown))
893 { 883 {
894 GNUNET_SERVER_client_disconnect (client); 884 GNUNET_SERVER_client_disconnect (client);
895 return; 885 return;
896 } 886 }
897 if (client->receive_pending == GNUNET_NO) 887 if (client->receive_pending == GNUNET_NO)
898 { 888 {
899#if DEBUG_SERVER 889#if DEBUG_SERVER
900 LOG (GNUNET_ERROR_TYPE_DEBUG, 890 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
901 "Server begins to read again from client.\n");
902#endif 891#endif
903 client->receive_pending = GNUNET_YES; 892 client->receive_pending = GNUNET_YES;
904 GNUNET_CONNECTION_receive (client->connection, 893 GNUNET_CONNECTION_receive (client->connection,
905 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 894 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
906 client->idle_timeout, &process_incoming, 895 client->idle_timeout, &process_incoming, client);
907 client); 896 return;
908 return; 897 }
909 }
910#if DEBUG_SERVER 898#if DEBUG_SERVER
911 LOG (GNUNET_ERROR_TYPE_DEBUG, 899 LOG (GNUNET_ERROR_TYPE_DEBUG,
912 "Server continues processing messages still in the buffer.\n"); 900 "Server continues processing messages still in the buffer.\n");
@@ -927,7 +915,7 @@ restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
927 */ 915 */
928static void 916static void
929client_message_tokenizer_callback (void *cls, void *client, 917client_message_tokenizer_callback (void *cls, void *client,
930 const struct GNUNET_MessageHeader *message) 918 const struct GNUNET_MessageHeader *message)
931{ 919{
932 struct GNUNET_SERVER_Handle *server = cls; 920 struct GNUNET_SERVER_Handle *server = cls;
933 struct GNUNET_SERVER_Client *sender = client; 921 struct GNUNET_SERVER_Client *sender = client;
@@ -961,14 +949,14 @@ client_message_tokenizer_callback (void *cls, void *client,
961 */ 949 */
962struct GNUNET_SERVER_Client * 950struct GNUNET_SERVER_Client *
963GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server, 951GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
964 struct GNUNET_CONNECTION_Handle *connection) 952 struct GNUNET_CONNECTION_Handle *connection)
965{ 953{
966 struct GNUNET_SERVER_Client *client; 954 struct GNUNET_SERVER_Client *client;
967 955
968 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client)); 956 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
969 client->connection = connection; 957 client->connection = connection;
970 client->mst = 958 client->mst =
971 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server); 959 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
972 client->reference_count = 1; 960 client->reference_count = 1;
973 client->server = server; 961 client->server = server;
974 client->last_activity = GNUNET_TIME_absolute_get (); 962 client->last_activity = GNUNET_TIME_absolute_get ();
@@ -979,8 +967,8 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
979 client->callback = NULL; 967 client->callback = NULL;
980 client->callback_cls = NULL; 968 client->callback_cls = NULL;
981 GNUNET_CONNECTION_receive (client->connection, 969 GNUNET_CONNECTION_receive (client->connection,
982 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 970 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
983 client->idle_timeout, &process_incoming, client); 971 client->idle_timeout, &process_incoming, client);
984 return client; 972 return client;
985} 973}
986 974
@@ -995,7 +983,7 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
995 */ 983 */
996void 984void
997GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client, 985GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
998 struct GNUNET_TIME_Relative timeout) 986 struct GNUNET_TIME_Relative timeout)
999{ 987{
1000 client->idle_timeout = timeout; 988 client->idle_timeout = timeout;
1001} 989}
@@ -1043,7 +1031,7 @@ GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1043 */ 1031 */
1044int 1032int
1045GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client, 1033GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1046 void **addr, size_t * addrlen) 1034 void **addr, size_t * addrlen)
1047{ 1035{
1048 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen); 1036 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1049} 1037}
@@ -1061,8 +1049,8 @@ GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1061 */ 1049 */
1062void 1050void
1063GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server, 1051GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1064 GNUNET_SERVER_DisconnectCallback callback, 1052 GNUNET_SERVER_DisconnectCallback callback,
1065 void *callback_cls) 1053 void *callback_cls)
1066{ 1054{
1067 struct NotifyList *n; 1055 struct NotifyList *n;
1068 1056
@@ -1083,8 +1071,8 @@ GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1083 */ 1071 */
1084void 1072void
1085GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server, 1073GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1086 GNUNET_SERVER_DisconnectCallback 1074 GNUNET_SERVER_DisconnectCallback
1087 callback, void *callback_cls) 1075 callback, void *callback_cls)
1088{ 1076{
1089 struct NotifyList *pos; 1077 struct NotifyList *pos;
1090 struct NotifyList *prev; 1078 struct NotifyList *prev;
@@ -1092,17 +1080,17 @@ GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1092 prev = NULL; 1080 prev = NULL;
1093 pos = server->disconnect_notify_list; 1081 pos = server->disconnect_notify_list;
1094 while (pos != NULL) 1082 while (pos != NULL)
1095 { 1083 {
1096 if ((pos->callback == callback) && (pos->callback_cls == callback_cls)) 1084 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1097 break; 1085 break;
1098 prev = pos; 1086 prev = pos;
1099 pos = pos->next; 1087 pos = pos->next;
1100 } 1088 }
1101 if (pos == NULL) 1089 if (pos == NULL)
1102 { 1090 {
1103 GNUNET_break (0); 1091 GNUNET_break (0);
1104 return; 1092 return;
1105 } 1093 }
1106 if (prev == NULL) 1094 if (prev == NULL)
1107 server->disconnect_notify_list = pos->next; 1095 server->disconnect_notify_list = pos->next;
1108 else 1096 else
@@ -1133,72 +1121,72 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1133 "Client is being disconnected from the server.\n"); 1121 "Client is being disconnected from the server.\n");
1134#endif 1122#endif
1135 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) 1123 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1124 {
1125 GNUNET_SCHEDULER_cancel (client->restart_task);
1126 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1127 }
1128 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1129 {
1130 GNUNET_SCHEDULER_cancel (client->warn_task);
1131 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1132 }
1133 if (GNUNET_YES == client->receive_pending)
1134 {
1135 GNUNET_CONNECTION_receive_cancel (client->connection);
1136 client->receive_pending = GNUNET_NO;
1137 }
1138
1139 rc = client->reference_count;
1140 if (client->server != NULL)
1141 {
1142 server = client->server;
1143 client->server = NULL;
1144 client->shutdown_now = GNUNET_YES;
1145 prev = NULL;
1146 pos = server->clients;
1147 while ((pos != NULL) && (pos != client))
1148 {
1149 prev = pos;
1150 pos = pos->next;
1151 }
1152 GNUNET_assert (pos != NULL);
1153 if (prev == NULL)
1154 server->clients = pos->next;
1155 else
1156 prev->next = pos->next;
1157 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1136 { 1158 {
1137 GNUNET_SCHEDULER_cancel (client->restart_task); 1159 GNUNET_SCHEDULER_cancel (client->restart_task);
1138 client->restart_task = GNUNET_SCHEDULER_NO_TASK; 1160 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1139 } 1161 }
1140 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) 1162 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1141 { 1163 {
1142 GNUNET_SCHEDULER_cancel (client->warn_task); 1164 GNUNET_SCHEDULER_cancel (client->warn_task);
1143 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 1165 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1144 } 1166 }
1145 if (GNUNET_YES == client->receive_pending) 1167 n = server->disconnect_notify_list;
1146 { 1168 while (n != NULL)
1147 GNUNET_CONNECTION_receive_cancel (client->connection);
1148 client->receive_pending = GNUNET_NO;
1149 }
1150
1151 rc = client->reference_count;
1152 if (client->server != NULL)
1153 { 1169 {
1154 server = client->server; 1170 n->callback (n->callback_cls, client);
1155 client->server = NULL; 1171 n = n->next;
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 }
1185 } 1172 }
1173 }
1186 if (rc > 0) 1174 if (rc > 0)
1187 { 1175 {
1188#if DEBUG_SERVER 1176#if DEBUG_SERVER
1189 LOG (GNUNET_ERROR_TYPE_DEBUG, 1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1190 "RC still positive, not destroying everything.\n"); 1178 "RC still positive, not destroying everything.\n");
1191#endif 1179#endif
1192 return; 1180 return;
1193 } 1181 }
1194 if (client->in_process_client_buffer == GNUNET_YES) 1182 if (client->in_process_client_buffer == GNUNET_YES)
1195 { 1183 {
1196#if DEBUG_SERVER 1184#if DEBUG_SERVER
1197 LOG (GNUNET_ERROR_TYPE_DEBUG, 1185 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "Still processing inputs, not destroying everything.\n"); 1186 "Still processing inputs, not destroying everything.\n");
1199#endif 1187#endif
1200 return; 1188 return;
1201 } 1189 }
1202 1190
1203 if (client->persist == GNUNET_YES) 1191 if (client->persist == GNUNET_YES)
1204 GNUNET_CONNECTION_persist_ (client->connection); 1192 GNUNET_CONNECTION_persist_ (client->connection);
@@ -1262,17 +1250,17 @@ transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1262 */ 1250 */
1263struct GNUNET_CONNECTION_TransmitHandle * 1251struct GNUNET_CONNECTION_TransmitHandle *
1264GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client, 1252GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1265 size_t size, 1253 size_t size,
1266 struct GNUNET_TIME_Relative timeout, 1254 struct GNUNET_TIME_Relative timeout,
1267 GNUNET_CONNECTION_TransmitReadyNotify 1255 GNUNET_CONNECTION_TransmitReadyNotify
1268 callback, void *callback_cls) 1256 callback, void *callback_cls)
1269{ 1257{
1270 client->callback_cls = callback_cls; 1258 client->callback_cls = callback_cls;
1271 client->callback = callback; 1259 client->callback = callback;
1272 return GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, 1260 return GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1273 timeout, 1261 timeout,
1274 &transmit_ready_callback_wrapper, 1262 &transmit_ready_callback_wrapper,
1275 client); 1263 client);
1276} 1264}
1277 1265
1278 1266
@@ -1309,47 +1297,46 @@ GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1309 GNUNET_assert (client->suspended > 0); 1297 GNUNET_assert (client->suspended > 0);
1310 client->suspended--; 1298 client->suspended--;
1311 if (success != GNUNET_OK) 1299 if (success != GNUNET_OK)
1312 { 1300 {
1313#if DEBUG_SERVER 1301#if DEBUG_SERVER
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, 1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1315 "GNUNET_SERVER_receive_done called with failure indication\n"); 1303 "GNUNET_SERVER_receive_done called with failure indication\n");
1316#endif 1304#endif
1317 GNUNET_SERVER_client_disconnect (client); 1305 GNUNET_SERVER_client_disconnect (client);
1318 return; 1306 return;
1319 } 1307 }
1320 if (client->suspended > 0) 1308 if (client->suspended > 0)
1321 { 1309 {
1322#if DEBUG_SERVER 1310#if DEBUG_SERVER
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, 1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "GNUNET_SERVER_receive_done called, but more clients pending\n"); 1312 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1325#endif 1313#endif
1326 return; 1314 return;
1327 } 1315 }
1328 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) 1316 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1329 { 1317 {
1330 GNUNET_SCHEDULER_cancel (client->warn_task); 1318 GNUNET_SCHEDULER_cancel (client->warn_task);
1331 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 1319 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1332 } 1320 }
1333 if (client->in_process_client_buffer == GNUNET_YES) 1321 if (client->in_process_client_buffer == GNUNET_YES)
1334 { 1322 {
1335#if DEBUG_SERVER 1323#if DEBUG_SERVER
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, 1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "GNUNET_SERVER_receive_done called while still in processing loop\n"); 1325 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1338#endif 1326#endif
1339 return; 1327 return;
1340 } 1328 }
1341 if (client->server == NULL) 1329 if (client->server == NULL)
1342 { 1330 {
1343 GNUNET_SERVER_client_disconnect (client); 1331 GNUNET_SERVER_client_disconnect (client);
1344 return; 1332 return;
1345 } 1333 }
1346#if DEBUG_SERVER 1334#if DEBUG_SERVER
1347 LOG (GNUNET_ERROR_TYPE_DEBUG, 1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1348 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n"); 1336 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1349#endif 1337#endif
1350 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task); 1338 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1351 client->restart_task = 1339 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client);
1352 GNUNET_SCHEDULER_add_now (&restart_processing, client);
1353} 1340}
1354 1341
1355 1342
diff --git a/src/util/server_mst.c b/src/util/server_mst.c
index 6fd2647d7..dcb8c4810 100644
--- a/src/util/server_mst.c
+++ b/src/util/server_mst.c
@@ -91,7 +91,7 @@ struct GNUNET_SERVER_MessageStreamTokenizer
91 */ 91 */
92struct GNUNET_SERVER_MessageStreamTokenizer * 92struct GNUNET_SERVER_MessageStreamTokenizer *
93GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb, 93GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
94 void *cb_cls) 94 void *cb_cls)
95{ 95{
96 struct GNUNET_SERVER_MessageStreamTokenizer *ret; 96 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
97 97
@@ -121,8 +121,8 @@ GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
121 */ 121 */
122int 122int
123GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, 123GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
124 void *client_identity, const char *buf, 124 void *client_identity, const char *buf, size_t size,
125 size_t size, int purge, int one_shot) 125 int purge, int one_shot)
126{ 126{
127 const struct GNUNET_MessageHeader *hdr; 127 const struct GNUNET_MessageHeader *hdr;
128 size_t delta; 128 size_t delta;
@@ -140,148 +140,148 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
140 ret = GNUNET_OK; 140 ret = GNUNET_OK;
141 ibuf = (char *) mst->hdr; 141 ibuf = (char *) mst->hdr;
142 while (mst->pos > 0) 142 while (mst->pos > 0)
143 {
144do_align:
145 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
146 (0 != (mst->off % ALIGN_FACTOR)))
143 { 147 {
144 do_align: 148 /* need to align or need more space */
145 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) || 149 mst->pos -= mst->off;
146 (0 != (mst->off % ALIGN_FACTOR))) 150 memmove (ibuf, &ibuf[mst->off], mst->pos);
147 { 151 mst->off = 0;
148 /* need to align or need more space */ 152 }
149 mst->pos -= mst->off; 153 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
150 memmove (ibuf, &ibuf[mst->off], mst->pos); 154 {
151 mst->off = 0; 155 delta =
152 } 156 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
153 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) 157 (mst->pos - mst->off), size);
154 { 158 memcpy (&ibuf[mst->pos], buf, delta);
155 delta = 159 mst->pos += delta;
156 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - 160 buf += delta;
157 (mst->pos - mst->off), size); 161 size -= delta;
158 memcpy (&ibuf[mst->pos], buf, delta); 162 }
159 mst->pos += delta; 163 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
160 buf += delta; 164 {
161 size -= delta; 165 if (purge)
162 } 166 {
163 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) 167 mst->off = 0;
164 { 168 mst->pos = 0;
165 if (purge) 169 }
166 { 170 return GNUNET_OK;
167 mst->off = 0; 171 }
168 mst->pos = 0; 172 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
169 } 173 want = ntohs (hdr->size);
170 return GNUNET_OK; 174 if (want < sizeof (struct GNUNET_MessageHeader))
171 } 175 {
172 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; 176 GNUNET_break_op (0);
177 return GNUNET_SYSERR;
178 }
179 if (mst->curr_buf - mst->off < want)
180 {
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 }
210 if (one_shot == GNUNET_SYSERR)
211 {
212 /* cannot call callback again, but return value saying that
213 * we have another full message in the buffer */
214 ret = GNUNET_NO;
215 goto copy;
216 }
217 if (one_shot == GNUNET_YES)
218 one_shot = GNUNET_SYSERR;
219 mst->cb (mst->cb_cls, client_identity, hdr);
220 mst->off += 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 }
227 }
228 while (size > 0)
229 {
230#if DEBUG_SERVER_MST
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
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;
173 want = ntohs (hdr->size); 243 want = ntohs (hdr->size);
174 if (want < sizeof (struct GNUNET_MessageHeader)) 244 if (want < sizeof (struct GNUNET_MessageHeader))
175 { 245 {
176 GNUNET_break_op (0); 246 GNUNET_break_op (0);
177 return GNUNET_SYSERR; 247 mst->off = 0;
178 } 248 return GNUNET_SYSERR;
179 if (mst->curr_buf - mst->off < want) 249 }
180 { 250 if (size < want)
181 /* need more space */ 251 break; /* or not, buffer incomplete, so copy to private buffer... */
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 }
210 if (one_shot == GNUNET_SYSERR) 252 if (one_shot == GNUNET_SYSERR)
211 { 253 {
212 /* cannot call callback again, but return value saying that 254 /* cannot call callback again, but return value saying that
213 * we have another full message in the buffer */ 255 * we have another full message in the buffer */
214 ret = GNUNET_NO; 256 ret = GNUNET_NO;
215 goto copy; 257 goto copy;
216 } 258 }
217 if (one_shot == GNUNET_YES) 259 if (one_shot == GNUNET_YES)
218 one_shot = GNUNET_SYSERR; 260 one_shot = GNUNET_SYSERR;
219 mst->cb (mst->cb_cls, client_identity, hdr); 261 mst->cb (mst->cb_cls, client_identity, hdr);
220 mst->off += want; 262 buf += want;
221 if (mst->off == mst->pos) 263 size -= want;
222 {
223 /* reset to beginning of buffer, it's free right now! */
224 mst->off = 0;
225 mst->pos = 0;
226 }
227 } 264 }
228 while (size > 0) 265 else
229 { 266 {
230#if DEBUG_SERVER_MST 267 /* need to copy to private buffer to align;
231 LOG (GNUNET_ERROR_TYPE_DEBUG, 268 * yes, we go a bit more spagetti than usual here */
232 "Server-mst has %u bytes left in inbound buffer\n", 269 goto do_align;
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 }
271 } 270 }
271 }
272copy: 272copy:
273 if ((size > 0) && (!purge)) 273 if ((size > 0) && (!purge))
274 {
275 if (size + mst->pos > mst->curr_buf)
274 { 276 {
275 if (size + mst->pos > mst->curr_buf) 277 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
276 { 278 ibuf = (char *) mst->hdr;
277 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos); 279 mst->curr_buf = 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;
284 } 280 }
281 GNUNET_assert (mst->pos + size <= mst->curr_buf);
282 memcpy (&ibuf[mst->pos], buf, size);
283 mst->pos += size;
284 }
285 if (purge) 285 if (purge)
286 mst->off = 0; 286 mst->off = 0;
287#if DEBUG_SERVER_MST 287#if DEBUG_SERVER_MST
diff --git a/src/util/server_nc.c b/src/util/server_nc.c
index cbe98a014..a36fa0c50 100644
--- a/src/util/server_nc.c
+++ b/src/util/server_nc.c
@@ -156,19 +156,19 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
156 struct PendingMessageList *pml; 156 struct PendingMessageList *pml;
157 157
158 if (client == NULL) 158 if (client == NULL)
159 { 159 {
160 nc->server = NULL; 160 nc->server = NULL;
161 return; 161 return;
162 } 162 }
163 prev = NULL; 163 prev = NULL;
164 pos = nc->clients; 164 pos = nc->clients;
165 while (NULL != pos) 165 while (NULL != pos)
166 { 166 {
167 if (pos->client == client) 167 if (pos->client == client)
168 break; 168 break;
169 prev = pos; 169 prev = pos;
170 pos = pos->next; 170 pos = pos->next;
171 } 171 }
172 if (pos == NULL) 172 if (pos == NULL)
173 return; 173 return;
174#if DEBUG_SERVER_NC 174#if DEBUG_SERVER_NC
@@ -181,15 +181,15 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
181 else 181 else
182 prev->next = pos->next; 182 prev->next = pos->next;
183 while (NULL != (pml = pos->pending_head)) 183 while (NULL != (pml = pos->pending_head))
184 { 184 {
185 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml); 185 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
186 GNUNET_free (pml); 186 GNUNET_free (pml);
187 } 187 }
188 if (pos->th != NULL) 188 if (pos->th != NULL)
189 { 189 {
190 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th); 190 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
191 pos->th = NULL; 191 pos->th = NULL;
192 } 192 }
193 GNUNET_SERVER_client_drop (client); 193 GNUNET_SERVER_client_drop (client);
194 GNUNET_free (pos); 194 GNUNET_free (pos);
195} 195}
@@ -205,8 +205,8 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
205 * @return handle to the notification context 205 * @return handle to the notification context
206 */ 206 */
207struct GNUNET_SERVER_NotificationContext * 207struct GNUNET_SERVER_NotificationContext *
208GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle 208GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
209 *server, unsigned int queue_length) 209 unsigned int queue_length)
210{ 210{
211 struct GNUNET_SERVER_NotificationContext *ret; 211 struct GNUNET_SERVER_NotificationContext *ret;
212 212
@@ -225,27 +225,26 @@ GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle
225 */ 225 */
226void 226void
227GNUNET_SERVER_notification_context_destroy (struct 227GNUNET_SERVER_notification_context_destroy (struct
228 GNUNET_SERVER_NotificationContext 228 GNUNET_SERVER_NotificationContext
229 *nc) 229 *nc)
230{ 230{
231 struct ClientList *pos; 231 struct ClientList *pos;
232 struct PendingMessageList *pml; 232 struct PendingMessageList *pml;
233 233
234 while (NULL != (pos = nc->clients)) 234 while (NULL != (pos = nc->clients))
235 {
236 nc->clients = pos->next;
237 GNUNET_SERVER_client_drop (pos->client);
238 while (NULL != (pml = pos->pending_head))
235 { 239 {
236 nc->clients = pos->next; 240 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
237 GNUNET_SERVER_client_drop (pos->client); 241 GNUNET_free (pml);
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);
245 } 242 }
243 GNUNET_free (pos);
244 }
246 if (nc->server != NULL) 245 if (nc->server != NULL)
247 GNUNET_SERVER_disconnect_notify_cancel (nc->server, 246 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
248 &handle_client_disconnect, nc); 247 &handle_client_disconnect, nc);
249 GNUNET_free (nc); 248 GNUNET_free (nc);
250} 249}
251 250
@@ -257,9 +256,9 @@ GNUNET_SERVER_notification_context_destroy (struct
257 * @param client client to add 256 * @param client client to add
258 */ 257 */
259void 258void
260GNUNET_SERVER_notification_context_add (struct 259GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
261 GNUNET_SERVER_NotificationContext *nc, 260 *nc,
262 struct GNUNET_SERVER_Client *client) 261 struct GNUNET_SERVER_Client *client)
263{ 262{
264 struct ClientList *cl; 263 struct ClientList *cl;
265 264
@@ -293,45 +292,44 @@ transmit_message (void *cls, size_t size, void *buf)
293 292
294 cl->th = NULL; 293 cl->th = NULL;
295 if (buf == NULL) 294 if (buf == NULL)
296 { 295 {
297 /* 'cl' should be freed via disconnect notification shortly */ 296 /* 'cl' should be freed via disconnect notification shortly */
298#if DEBUG_SERVER_NC 297#if DEBUG_SERVER_NC
299 LOG (GNUNET_ERROR_TYPE_DEBUG, 298 LOG (GNUNET_ERROR_TYPE_DEBUG,
300 "Failed to transmit message from NC queue to client\n"); 299 "Failed to transmit message from NC queue to client\n");
301#endif 300#endif
302 return 0; 301 return 0;
303 } 302 }
304 ret = 0; 303 ret = 0;
305 while (NULL != (pml = cl->pending_head)) 304 while (NULL != (pml = cl->pending_head))
306 { 305 {
307 msize = ntohs (pml->msg->size); 306 msize = ntohs (pml->msg->size);
308 if (size < msize) 307 if (size < msize)
309 break; 308 break;
310 GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml); 309 GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
311#if DEBUG_SERVER_NC 310#if DEBUG_SERVER_NC
312 LOG (GNUNET_ERROR_TYPE_DEBUG, 311 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Copying message of type %u and size %u from pending queue to transmission buffer\n", 312 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
314 ntohs (pml->msg->type), msize); 313 ntohs (pml->msg->type), msize);
315#endif 314#endif
316 memcpy (&cbuf[ret], pml->msg, msize); 315 memcpy (&cbuf[ret], pml->msg, msize);
317 ret += msize; 316 ret += msize;
318 size -= msize; 317 size -= msize;
319 GNUNET_free (pml); 318 GNUNET_free (pml);
320 cl->num_pending--; 319 cl->num_pending--;
321 } 320 }
322 if (pml != NULL) 321 if (pml != NULL)
323 { 322 {
324#if DEBUG_SERVER_NC 323#if DEBUG_SERVER_NC
325 LOG (GNUNET_ERROR_TYPE_DEBUG, 324 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 "Have %u messages left in NC queue, will try transmission again\n", 325 "Have %u messages left in NC queue, will try transmission again\n",
327 cl->num_pending); 326 cl->num_pending);
328#endif 327#endif
329 cl->th = 328 cl->th =
330 GNUNET_SERVER_notify_transmit_ready (cl->client, 329 GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size),
331 ntohs (pml->msg->size), 330 GNUNET_TIME_UNIT_FOREVER_REL,
332 GNUNET_TIME_UNIT_FOREVER_REL, 331 &transmit_message, cl);
333 &transmit_message, cl); 332 }
334 }
335 else 333 else
336 GNUNET_assert (cl->num_pending == 0); 334 GNUNET_assert (cl->num_pending == 0);
337 return ret; 335 return ret;
@@ -348,25 +346,24 @@ transmit_message (void *cls, size_t size, void *buf)
348 */ 346 */
349static void 347static void
350do_unicast (struct GNUNET_SERVER_NotificationContext *nc, 348do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
351 struct ClientList *client, const struct GNUNET_MessageHeader *msg, 349 struct ClientList *client, const struct GNUNET_MessageHeader *msg,
352 int can_drop) 350 int can_drop)
353{ 351{
354 struct PendingMessageList *pml; 352 struct PendingMessageList *pml;
355 uint16_t size; 353 uint16_t size;
356 354
357 if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop)) 355 if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop))
358 { 356 {
359 LOG (GNUNET_ERROR_TYPE_INFO, 357 LOG (GNUNET_ERROR_TYPE_INFO,
360 "Dropping message of type %u and size %u due to full queue (%u entries)\n", 358 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
361 ntohs (msg->type), ntohs (msg->size), 359 ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
362 (unsigned int) nc->queue_length); 360 return; /* drop! */
363 return; /* drop! */ 361 }
364 }
365 if (client->num_pending > nc->queue_length) 362 if (client->num_pending > nc->queue_length)
366 { 363 {
367 /* FIXME: consider checking for other messages in the 364 /* FIXME: consider checking for other messages in the
368 * queue that are 'droppable' */ 365 * queue that are 'droppable' */
369 } 366 }
370 client->num_pending++; 367 client->num_pending++;
371 size = ntohs (msg->size); 368 size = ntohs (msg->size);
372 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size); 369 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
@@ -379,15 +376,15 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
379#endif 376#endif
380 memcpy (&pml[1], msg, size); 377 memcpy (&pml[1], msg, size);
381 /* append */ 378 /* append */
382 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, 379 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
383 client->pending_tail, pml); 380 pml);
384 if (client->th == NULL) 381 if (client->th == NULL)
385 client->th = 382 client->th =
386 GNUNET_SERVER_notify_transmit_ready (client->client, 383 GNUNET_SERVER_notify_transmit_ready (client->client,
387 ntohs (client->pending_head-> 384 ntohs (client->pending_head->
388 msg->size), 385 msg->size),
389 GNUNET_TIME_UNIT_FOREVER_REL, 386 GNUNET_TIME_UNIT_FOREVER_REL,
390 &transmit_message, client); 387 &transmit_message, client);
391} 388}
392 389
393 390
@@ -402,22 +399,21 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
402 */ 399 */
403void 400void
404GNUNET_SERVER_notification_context_unicast (struct 401GNUNET_SERVER_notification_context_unicast (struct
405 GNUNET_SERVER_NotificationContext 402 GNUNET_SERVER_NotificationContext
406 *nc, 403 *nc,
407 struct GNUNET_SERVER_Client 404 struct GNUNET_SERVER_Client *client,
408 *client, 405 const struct GNUNET_MessageHeader
409 const struct GNUNET_MessageHeader 406 *msg, int can_drop)
410 *msg, int can_drop)
411{ 407{
412 struct ClientList *pos; 408 struct ClientList *pos;
413 409
414 pos = nc->clients; 410 pos = nc->clients;
415 while (NULL != pos) 411 while (NULL != pos)
416 { 412 {
417 if (pos->client == client) 413 if (pos->client == client)
418 break; 414 break;
419 pos = pos->next; 415 pos = pos->next;
420 } 416 }
421 GNUNET_assert (pos != NULL); 417 GNUNET_assert (pos != NULL);
422 do_unicast (nc, pos, msg, can_drop); 418 do_unicast (nc, pos, msg, can_drop);
423} 419}
@@ -432,20 +428,19 @@ GNUNET_SERVER_notification_context_unicast (struct
432 */ 428 */
433void 429void
434GNUNET_SERVER_notification_context_broadcast (struct 430GNUNET_SERVER_notification_context_broadcast (struct
435 GNUNET_SERVER_NotificationContext 431 GNUNET_SERVER_NotificationContext
436 *nc, 432 *nc,
437 const struct 433 const struct GNUNET_MessageHeader
438 GNUNET_MessageHeader *msg, 434 *msg, int can_drop)
439 int can_drop)
440{ 435{
441 struct ClientList *pos; 436 struct ClientList *pos;
442 437
443 pos = nc->clients; 438 pos = nc->clients;
444 while (NULL != pos) 439 while (NULL != pos)
445 { 440 {
446 do_unicast (nc, pos, msg, can_drop); 441 do_unicast (nc, pos, msg, can_drop);
447 pos = pos->next; 442 pos = pos->next;
448 } 443 }
449} 444}
450 445
451 446
diff --git a/src/util/server_tc.c b/src/util/server_tc.c
index 020fd86db..ce40db19c 100644
--- a/src/util/server_tc.c
+++ b/src/util/server_tc.c
@@ -83,10 +83,10 @@ transmit_response (void *cls, size_t size, void *buf)
83 size_t msize; 83 size_t msize;
84 84
85 if (buf == NULL) 85 if (buf == NULL)
86 { 86 {
87 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); 87 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
88 return 0; 88 return 0;
89 } 89 }
90 if (tc->total - tc->off > size) 90 if (tc->total - tc->off > size)
91 msize = size; 91 msize = size;
92 else 92 else
@@ -94,28 +94,27 @@ transmit_response (void *cls, size_t size, void *buf)
94 memcpy (buf, &tc->buf[tc->off], msize); 94 memcpy (buf, &tc->buf[tc->off], msize);
95 tc->off += msize; 95 tc->off += msize;
96 if (tc->total == tc->off) 96 if (tc->total == tc->off)
97 { 97 {
98 98
99 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK); 99 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
100 GNUNET_SERVER_client_drop (tc->client); 100 GNUNET_SERVER_client_drop (tc->client);
101 GNUNET_free_non_null (tc->buf); 101 GNUNET_free_non_null (tc->buf);
102 GNUNET_free (tc); 102 GNUNET_free (tc);
103 } 103 }
104 else 104 else
105 {
106 if (NULL ==
107 GNUNET_SERVER_notify_transmit_ready (tc->client,
108 GNUNET_MIN (MIN_BLOCK_SIZE,
109 tc->total - tc->off),
110 GNUNET_TIME_absolute_get_remaining
111 (tc->timeout), &transmit_response,
112 tc))
105 { 113 {
106 if (NULL == 114 GNUNET_break (0);
107 GNUNET_SERVER_notify_transmit_ready (tc->client, 115 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
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 }
118 } 116 }
117 }
119 return msize; 118 return msize;
120} 119}
121 120
@@ -151,10 +150,9 @@ GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
151 * @param type type of the message 150 * @param type type of the message
152 */ 151 */
153void 152void
154GNUNET_SERVER_transmit_context_append_data (struct 153GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
155 GNUNET_SERVER_TransmitContext *tc, 154 *tc, const void *data,
156 const void *data, size_t length, 155 size_t length, uint16_t type)
157 uint16_t type)
158{ 156{
159 struct GNUNET_MessageHeader *msg; 157 struct GNUNET_MessageHeader *msg;
160 size_t size; 158 size_t size;
@@ -181,10 +179,10 @@ GNUNET_SERVER_transmit_context_append_data (struct
181 */ 179 */
182void 180void
183GNUNET_SERVER_transmit_context_append_message (struct 181GNUNET_SERVER_transmit_context_append_message (struct
184 GNUNET_SERVER_TransmitContext 182 GNUNET_SERVER_TransmitContext
185 *tc, 183 *tc,
186 const struct 184 const struct GNUNET_MessageHeader
187 GNUNET_MessageHeader *msg) 185 *msg)
188{ 186{
189 struct GNUNET_MessageHeader *m; 187 struct GNUNET_MessageHeader *m;
190 uint16_t size; 188 uint16_t size;
@@ -208,18 +206,18 @@ GNUNET_SERVER_transmit_context_append_message (struct
208 */ 206 */
209void 207void
210GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc, 208GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
211 struct GNUNET_TIME_Relative timeout) 209 struct GNUNET_TIME_Relative timeout)
212{ 210{
213 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout); 211 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
214 if (NULL == 212 if (NULL ==
215 GNUNET_SERVER_notify_transmit_ready (tc->client, 213 GNUNET_SERVER_notify_transmit_ready (tc->client,
216 GNUNET_MIN (MIN_BLOCK_SIZE, 214 GNUNET_MIN (MIN_BLOCK_SIZE,
217 tc->total), timeout, 215 tc->total), timeout,
218 &transmit_response, tc)) 216 &transmit_response, tc))
219 { 217 {
220 GNUNET_break (0); 218 GNUNET_break (0);
221 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR); 219 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
222 } 220 }
223} 221}
224 222
225 223
@@ -237,7 +235,7 @@ GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
237 */ 235 */
238void 236void
239GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext 237GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
240 *tc, int success) 238 *tc, int success)
241{ 239{
242 GNUNET_SERVER_receive_done (tc->client, success); 240 GNUNET_SERVER_receive_done (tc->client, success);
243 GNUNET_SERVER_client_drop (tc->client); 241 GNUNET_SERVER_client_drop (tc->client);
diff --git a/src/util/service.c b/src/util/service.c
index 7cafe35c8..cf1002ca9 100644
--- a/src/util/service.c
+++ b/src/util/service.c
@@ -104,121 +104,119 @@ parse_ipv4_specification (const char *routeList)
104 i = 0; 104 i = 0;
105 pos = 0; 105 pos = 0;
106 while (i < count) 106 while (i < count)
107 {
108 cnt =
109 sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
110 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
111 &temps[6], &temps[7]);
112 if (cnt == 8)
107 { 113 {
108 cnt = 114 for (j = 0; j < 8; j++)
109 sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0], 115 if (temps[j] > 0xFF)
110 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5], 116 {
111 &temps[6], &temps[7]); 117 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
112 if (cnt == 8) 118 &routeList[pos]);
113 { 119 GNUNET_free (result);
114 for (j = 0; j < 8; j++) 120 return NULL;
115 if (temps[j] > 0xFF) 121 }
116 { 122 result[i].network.s_addr =
117 LOG (GNUNET_ERROR_TYPE_ERROR, 123 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
118 _("Invalid format for IP: `%s'\n"), &routeList[pos]); 124 temps[3]);
119 GNUNET_free (result); 125 result[i].netmask.s_addr =
120 return NULL; 126 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
121 } 127 temps[7]);
122 result[i].network.s_addr = 128 while (routeList[pos] != ';')
123 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + 129 pos++;
124 temps[3]); 130 pos++;
125 result[i].netmask.s_addr = 131 i++;
126 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) + 132 continue;
127 temps[7]);
128 while (routeList[pos] != ';')
129 pos++;
130 pos++;
131 i++;
132 continue;
133 }
134 /* try second notation */
135 cnt =
136 sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
137 &temps[2], &temps[3], &slash);
138 if (cnt == 5)
139 {
140 for (j = 0; j < 4; j++)
141 if (temps[j] > 0xFF)
142 {
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 */
213 } 133 }
214 if (pos < strlen (routeList)) 134 /* try second notation */
135 cnt =
136 sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
137 &temps[2], &temps[3], &slash);
138 if (cnt == 5)
215 { 139 {
216 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"), 140 for (j = 0; j < 4; j++)
217 &routeList[pos]); 141 if (temps[j] > 0xFF)
218 GNUNET_free (result); 142 {
219 return NULL; /* oops */ 143 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
144 &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 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
171 slash);
172 GNUNET_free (result);
173 return NULL; /* error */
174 }
220 } 175 }
221 return result; /* ok */ 176 /* try third notation */
177 slash = 32;
178 cnt =
179 sscanf (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
180 &temps[2], &temps[3]);
181 if (cnt == 4)
182 {
183 for (j = 0; j < 4; j++)
184 if (temps[j] > 0xFF)
185 {
186 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
187 &routeList[pos]);
188 GNUNET_free (result);
189 return NULL;
190 }
191 result[i].network.s_addr =
192 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
193 temps[3]);
194 result[i].netmask.s_addr = 0;
195 while (slash > 0)
196 {
197 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
198 slash--;
199 }
200 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
201 while (routeList[pos] != ';')
202 pos++;
203 pos++;
204 i++;
205 continue;
206 }
207 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
208 &routeList[pos]);
209 GNUNET_free (result);
210 return NULL; /* error */
211 }
212 if (pos < strlen (routeList))
213 {
214 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
215 &routeList[pos]);
216 GNUNET_free (result);
217 return NULL; /* oops */
218 }
219 return result; /* ok */
222} 220}
223 221
224 222
@@ -260,85 +258,82 @@ parse_ipv6_specification (const char *routeListX)
260 if (routeList[i] == ';') 258 if (routeList[i] == ';')
261 count++; 259 count++;
262 if (routeList[len - 1] != ';') 260 if (routeList[len - 1] != ';')
263 { 261 {
264 LOG (GNUNET_ERROR_TYPE_ERROR, 262 LOG (GNUNET_ERROR_TYPE_ERROR,
265 _("Invalid network notation (does not end with ';': `%s')\n"), 263 _("Invalid network notation (does not end with ';': `%s')\n"),
266 routeList); 264 routeList);
267 GNUNET_free (routeList); 265 GNUNET_free (routeList);
268 return NULL; 266 return NULL;
269 } 267 }
270 268
271 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1)); 269 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
272 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1)); 270 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
273 i = 0; 271 i = 0;
274 pos = 0; 272 pos = 0;
275 while (i < count) 273 while (i < count)
274 {
275 start = pos;
276 while (routeList[pos] != ';')
277 pos++;
278 slash = pos;
279 while ((slash >= start) && (routeList[slash] != '/'))
280 slash--;
281 if (slash < start)
276 { 282 {
277 start = pos; 283 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
278 while (routeList[pos] != ';')
279 pos++;
280 slash = pos; 284 slash = pos;
281 while ((slash >= start) && (routeList[slash] != '/')) 285 }
282 slash--; 286 else
283 if (slash < start) 287 {
284 { 288 routeList[pos] = '\0';
285 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); 289 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
286 slash = pos;
287 }
288 else
289 {
290 routeList[pos] = '\0';
291 ret =
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) 290 if (ret <= 0)
329 { 291 {
330 if (ret == 0) 292 save = errno;
331 LOG (GNUNET_ERROR_TYPE_ERROR, 293 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
332 _("Wrong format `%s' for network\n"), &routeList[slash + 1]); 294 {
333 else 295 if (ret == 0)
334 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); 296 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
335 GNUNET_free (result); 297 &routeList[slash + 1]);
336 GNUNET_free (routeList); 298 else
337 return NULL; 299 {
338 } 300 errno = save;
339 pos++; 301 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
340 i++; 302 }
303 GNUNET_free (result);
304 GNUNET_free (routeList);
305 return NULL;
306 }
307 off = 0;
308 while (bits > 8)
309 {
310 result[i].netmask.s6_addr[off++] = 0xFF;
311 bits -= 8;
312 }
313 while (bits > 0)
314 {
315 result[i].netmask.s6_addr[off] =
316 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
317 bits--;
318 }
319 }
320 }
321 routeList[slash] = '\0';
322 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
323 if (ret <= 0)
324 {
325 if (ret == 0)
326 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
327 &routeList[slash + 1]);
328 else
329 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
330 GNUNET_free (result);
331 GNUNET_free (routeList);
332 return NULL;
341 } 333 }
334 pos++;
335 i++;
336 }
342 GNUNET_free (routeList); 337 GNUNET_free (routeList);
343 return result; 338 return result;
344} 339}
@@ -352,8 +347,7 @@ parse_ipv6_specification (const char *routeListX)
352 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is 347 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
353 */ 348 */
354static int 349static int
355check_ipv4_listed (const struct IPv4NetworkSet *list, 350check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
356 const struct in_addr *add)
357{ 351{
358 int i; 352 int i;
359 353
@@ -362,12 +356,12 @@ check_ipv4_listed (const struct IPv4NetworkSet *list,
362 return GNUNET_NO; 356 return GNUNET_NO;
363 357
364 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0)) 358 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
365 { 359 {
366 if ((add->s_addr & list[i].netmask.s_addr) == 360 if ((add->s_addr & list[i].netmask.s_addr) ==
367 (list[i].network.s_addr & list[i].netmask.s_addr)) 361 (list[i].network.s_addr & list[i].netmask.s_addr))
368 return GNUNET_YES; 362 return GNUNET_YES;
369 i++; 363 i++;
370 } 364 }
371 return GNUNET_NO; 365 return GNUNET_NO;
372} 366}
373 367
@@ -379,8 +373,7 @@ check_ipv4_listed (const struct IPv4NetworkSet *list,
379 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is 373 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
380 */ 374 */
381static int 375static int
382check_ipv6_listed (const struct IPv6NetworkSet *list, 376check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
383 const struct in6_addr *ip)
384{ 377{
385 unsigned int i; 378 unsigned int i;
386 unsigned int j; 379 unsigned int j;
@@ -393,16 +386,16 @@ check_ipv6_listed (const struct IPv6NetworkSet *list,
393 i = 0; 386 i = 0;
394NEXT: 387NEXT:
395 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0) 388 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
396 { 389 {
397 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++) 390 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
398 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) != 391 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
399 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j])) 392 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
400 { 393 {
401 i++; 394 i++;
402 goto NEXT; 395 goto NEXT;
403 } 396 }
404 return GNUNET_YES; 397 return GNUNET_YES;
405 } 398 }
406 return GNUNET_NO; 399 return GNUNET_NO;
407} 400}
408 401
@@ -536,10 +529,10 @@ write_test (void *cls, size_t size, void *buf)
536 struct GNUNET_MessageHeader *msg; 529 struct GNUNET_MessageHeader *msg;
537 530
538 if (size < sizeof (struct GNUNET_MessageHeader)) 531 if (size < sizeof (struct GNUNET_MessageHeader))
539 { 532 {
540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
541 return 0; /* client disconnected */ 534 return 0; /* client disconnected */
542 } 535 }
543 msg = (struct GNUNET_MessageHeader *) buf; 536 msg = (struct GNUNET_MessageHeader *) buf;
544 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); 537 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
545 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 538 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
@@ -556,15 +549,14 @@ write_test (void *cls, size_t size, void *buf)
556 */ 549 */
557static void 550static void
558handle_test (void *cls, struct GNUNET_SERVER_Client *client, 551handle_test (void *cls, struct GNUNET_SERVER_Client *client,
559 const struct GNUNET_MessageHeader *message) 552 const struct GNUNET_MessageHeader *message)
560{ 553{
561 /* simply bounce message back to acknowledge */ 554 /* simply bounce message back to acknowledge */
562 if (NULL == 555 if (NULL ==
563 GNUNET_SERVER_notify_transmit_ready (client, 556 GNUNET_SERVER_notify_transmit_ready (client,
564 sizeof (struct 557 sizeof (struct GNUNET_MessageHeader),
565 GNUNET_MessageHeader), 558 GNUNET_TIME_UNIT_FOREVER_REL,
566 GNUNET_TIME_UNIT_FOREVER_REL, 559 &write_test, client))
567 &write_test, client))
568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
569} 561}
570 562
@@ -597,7 +589,7 @@ static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
597 */ 589 */
598static int 590static int
599check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, 591check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
600 const struct sockaddr *addr, socklen_t addrlen) 592 const struct sockaddr *addr, socklen_t addrlen)
601{ 593{
602 struct GNUNET_SERVICE_Context *sctx = cls; 594 struct GNUNET_SERVICE_Context *sctx = cls;
603 const struct sockaddr_in *i4; 595 const struct sockaddr_in *i4;
@@ -605,51 +597,51 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
605 int ret; 597 int ret;
606 598
607 switch (addr->sa_family) 599 switch (addr->sa_family)
608 { 600 {
609 case AF_INET: 601 case AF_INET:
610 GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); 602 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
611 i4 = (const struct sockaddr_in *) addr; 603 i4 = (const struct sockaddr_in *) addr;
612 ret = ((sctx->v4_allowed == NULL) || 604 ret = ((sctx->v4_allowed == NULL) ||
613 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) && 605 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
614 ((sctx->v4_denied == NULL) || 606 ((sctx->v4_denied == NULL) ||
615 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr))); 607 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
616 break; 608 break;
617 case AF_INET6: 609 case AF_INET6:
618 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); 610 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
619 i6 = (const struct sockaddr_in6 *) addr; 611 i6 = (const struct sockaddr_in6 *) addr;
620 ret = ((sctx->v6_allowed == NULL) || 612 ret = ((sctx->v6_allowed == NULL) ||
621 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) && 613 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
622 ((sctx->v6_denied == NULL) || 614 ((sctx->v6_denied == NULL) ||
623 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr))); 615 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
624 break; 616 break;
625#ifndef WINDOWS 617#ifndef WINDOWS
626 case AF_UNIX: 618 case AF_UNIX:
627 ret = GNUNET_OK; /* always OK for now */ 619 ret = GNUNET_OK; /* always OK for now */
628 if ((sctx->match_uid == GNUNET_YES) || (sctx->match_gid == GNUNET_YES)) 620 if ((sctx->match_uid == GNUNET_YES) || (sctx->match_gid == GNUNET_YES))
629 ret = GNUNET_NO; 621 ret = GNUNET_NO;
630 if ((uc != NULL) && 622 if ((uc != NULL) &&
631 ((sctx->match_uid != GNUNET_YES) || (uc->uid == geteuid ()) || 623 ((sctx->match_uid != GNUNET_YES) || (uc->uid == geteuid ()) ||
632 (uc->uid == getuid ())) && ((sctx->match_gid != GNUNET_YES) || 624 (uc->uid == getuid ())) && ((sctx->match_gid != GNUNET_YES) ||
633 (uc->gid == getegid ()) || 625 (uc->gid == getegid ()) ||
634 (uc->gid == getgid ()))) 626 (uc->gid == getgid ())))
635 ret = GNUNET_YES; 627 ret = GNUNET_YES;
636 else 628 else
637 LOG (GNUNET_ERROR_TYPE_WARNING, 629 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
638 _("Access denied to UID %d / GID %d\n"), 630 (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid);
639 (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid); 631 break;
640 break;
641#endif 632#endif
642 default: 633 default:
643 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"), 634 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
644 addr->sa_family); 635 addr->sa_family);
645 return GNUNET_SYSERR; 636 return GNUNET_SYSERR;
646 } 637 }
647 if (ret != GNUNET_OK) 638 if (ret != GNUNET_OK)
648 { 639 {
649 LOG (GNUNET_ERROR_TYPE_WARNING, 640 LOG (GNUNET_ERROR_TYPE_WARNING,
650 _("Access from `%s' denied to service `%s'\n"), 641 _("Access from `%s' denied to service `%s'\n"), GNUNET_a2s (addr,
651 GNUNET_a2s (addr, addrlen), sctx->serviceName); 642 addrlen),
652 } 643 sctx->serviceName);
644 }
653 return ret; 645 return ret;
654} 646}
655 647
@@ -666,7 +658,7 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
666 658
667 if (GNUNET_OK != 659 if (GNUNET_OK !=
668 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName, 660 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
669 "PIDFILE", &pif)) 661 "PIDFILE", &pif))
670 return NULL; 662 return NULL;
671 return pif; 663 return pif;
672} 664}
@@ -676,26 +668,25 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
676 * Parse an IPv4 access control list. 668 * Parse an IPv4 access control list.
677 */ 669 */
678static int 670static int
679process_acl4 (struct IPv4NetworkSet **ret, 671process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
680 struct GNUNET_SERVICE_Context *sctx, const char *option) 672 const char *option)
681{ 673{
682 char *opt; 674 char *opt;
683 675
684 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option)) 676 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
685 return GNUNET_OK; 677 return GNUNET_OK;
686 GNUNET_break (GNUNET_OK == 678 GNUNET_break (GNUNET_OK ==
687 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, 679 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
688 sctx->serviceName, 680 sctx->serviceName,
689 option, &opt)); 681 option, &opt));
690 if (NULL == (*ret = parse_ipv4_specification (opt))) 682 if (NULL == (*ret = parse_ipv4_specification (opt)))
691 { 683 {
692 LOG (GNUNET_ERROR_TYPE_WARNING, 684 LOG (GNUNET_ERROR_TYPE_WARNING,
693 _ 685 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
694 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"), 686 opt, sctx->serviceName, option);
695 opt, sctx->serviceName, option); 687 GNUNET_free (opt);
696 GNUNET_free (opt); 688 return GNUNET_SYSERR;
697 return GNUNET_SYSERR; 689 }
698 }
699 GNUNET_free (opt); 690 GNUNET_free (opt);
700 return GNUNET_OK; 691 return GNUNET_OK;
701} 692}
@@ -705,26 +696,25 @@ process_acl4 (struct IPv4NetworkSet **ret,
705 * Parse an IPv4 access control list. 696 * Parse an IPv4 access control list.
706 */ 697 */
707static int 698static int
708process_acl6 (struct IPv6NetworkSet **ret, 699process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
709 struct GNUNET_SERVICE_Context *sctx, const char *option) 700 const char *option)
710{ 701{
711 char *opt; 702 char *opt;
712 703
713 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option)) 704 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
714 return GNUNET_OK; 705 return GNUNET_OK;
715 GNUNET_break (GNUNET_OK == 706 GNUNET_break (GNUNET_OK ==
716 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, 707 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
717 sctx->serviceName, 708 sctx->serviceName,
718 option, &opt)); 709 option, &opt));
719 if (NULL == (*ret = parse_ipv6_specification (opt))) 710 if (NULL == (*ret = parse_ipv6_specification (opt)))
720 { 711 {
721 LOG (GNUNET_ERROR_TYPE_WARNING, 712 LOG (GNUNET_ERROR_TYPE_WARNING,
722 _ 713 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
723 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"), 714 opt, sctx->serviceName, option);
724 opt, sctx->serviceName, option); 715 GNUNET_free (opt);
725 GNUNET_free (opt); 716 return GNUNET_SYSERR;
726 return GNUNET_SYSERR; 717 }
727 }
728 GNUNET_free (opt); 718 GNUNET_free (opt);
729 return GNUNET_OK; 719 return GNUNET_OK;
730} 720}
@@ -739,7 +729,7 @@ process_acl6 (struct IPv6NetworkSet **ret,
739 */ 729 */
740static void 730static void
741add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens, 731add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
742 const char *unixpath) 732 const char *unixpath)
743{ 733{
744#ifdef AF_UNIX 734#ifdef AF_UNIX
745 struct sockaddr_un *un; 735 struct sockaddr_un *un;
@@ -791,9 +781,9 @@ add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
791 */ 781 */
792int 782int
793GNUNET_SERVICE_get_server_addresses (const char *serviceName, 783GNUNET_SERVICE_get_server_addresses (const char *serviceName,
794 const struct GNUNET_CONFIGURATION_Handle 784 const struct GNUNET_CONFIGURATION_Handle
795 *cfg, struct sockaddr ***addrs, 785 *cfg, struct sockaddr ***addrs,
796 socklen_t ** addr_lens) 786 socklen_t ** addr_lens)
797{ 787{
798 int disablev6; 788 int disablev6;
799 struct GNUNET_NETWORK_Handle *desc; 789 struct GNUNET_NETWORK_Handle *desc;
@@ -814,64 +804,61 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
814 *addr_lens = NULL; 804 *addr_lens = NULL;
815 desc = NULL; 805 desc = NULL;
816 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6")) 806 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6"))
817 { 807 {
818 if (GNUNET_SYSERR == 808 if (GNUNET_SYSERR ==
819 (disablev6 = 809 (disablev6 =
820 GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, 810 GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, "DISABLEV6")))
821 "DISABLEV6"))) 811 return GNUNET_SYSERR;
822 return GNUNET_SYSERR; 812 }
823 }
824 else 813 else
825 disablev6 = GNUNET_NO; 814 disablev6 = GNUNET_NO;
826 815
827 if (!disablev6) 816 if (!disablev6)
817 {
818 /* probe IPv6 support */
819 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
820 if (NULL == desc)
828 { 821 {
829 /* probe IPv6 support */ 822 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
830 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); 823 (errno == EACCES))
831 if (NULL == desc) 824 {
832 { 825 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
833 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || 826 return GNUNET_SYSERR;
834 (errno == EACCES)) 827 }
835 { 828 LOG (GNUNET_ERROR_TYPE_INFO,
836 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); 829 _
837 return GNUNET_SYSERR; 830 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
838 } 831 serviceName, STRERROR (errno));
839 LOG (GNUNET_ERROR_TYPE_INFO, 832 disablev6 = GNUNET_YES;
840 _
841 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
842 serviceName, STRERROR (errno));
843 disablev6 = GNUNET_YES;
844 }
845 else
846 {
847 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
848 desc = NULL;
849 }
850 } 833 }
834 else
835 {
836 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
837 desc = NULL;
838 }
839 }
851 840
852 port = 0; 841 port = 0;
853 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT")) 842 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
843 {
844 GNUNET_break (GNUNET_OK ==
845 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
846 "PORT", &port));
847 if (port > 65535)
854 { 848 {
855 GNUNET_break (GNUNET_OK == 849 LOG (GNUNET_ERROR_TYPE_ERROR,
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 }
866 } 853 }
854 }
867 855
868 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO")) 856 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
869 { 857 {
870 GNUNET_break (GNUNET_OK == 858 GNUNET_break (GNUNET_OK ==
871 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, 859 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
872 "BINDTO", 860 "BINDTO", &hostname));
873 &hostname)); 861 }
874 }
875 else 862 else
876 hostname = NULL; 863 hostname = NULL;
877 864
@@ -881,211 +868,210 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
881 GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) && 868 GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) &&
882 (GNUNET_OK == 869 (GNUNET_OK ==
883 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH", 870 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH",
884 &unixpath)) && 871 &unixpath)) &&
885 (0 < strlen (unixpath))) 872 (0 < strlen (unixpath)))
873 {
874 /* probe UNIX support */
875 struct sockaddr_un s_un;
876
877 if (strlen (unixpath) >= sizeof (s_un.sun_path))
886 { 878 {
887 /* probe UNIX support */ 879 LOG (GNUNET_ERROR_TYPE_WARNING,
888 struct sockaddr_un s_un; 880 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
889 881 sizeof (s_un.sun_path));
890 if (strlen (unixpath) >= sizeof (s_un.sun_path)) 882 GNUNET_free_non_null (hostname);
891 { 883 GNUNET_free (unixpath);
892 LOG (GNUNET_ERROR_TYPE_WARNING, 884 return GNUNET_SYSERR;
893 _("UNIXPATH `%s' too long, maximum length is %llu\n"), 885 }
894 unixpath, sizeof (s_un.sun_path)); 886
895 GNUNET_free_non_null (hostname); 887 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
896 GNUNET_free (unixpath); 888 if (NULL == desc)
897 return GNUNET_SYSERR; 889 {
898 } 890 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
899 891 (errno == EACCES))
900 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); 892 {
901 if (NULL == desc) 893 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
902 { 894 GNUNET_free_non_null (hostname);
903 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || 895 GNUNET_free (unixpath);
904 (errno == EACCES)) 896 return GNUNET_SYSERR;
905 { 897 }
906 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); 898 LOG (GNUNET_ERROR_TYPE_INFO,
907 GNUNET_free_non_null (hostname); 899 _
908 GNUNET_free (unixpath); 900 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
909 return GNUNET_SYSERR; 901 serviceName, STRERROR (errno));
910 } 902 GNUNET_free (unixpath);
911 LOG (GNUNET_ERROR_TYPE_INFO, 903 unixpath = NULL;
912 _ 904 }
913 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"), 905 else
914 serviceName, STRERROR (errno)); 906 {
915 GNUNET_free (unixpath); 907 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
916 unixpath = NULL; 908 desc = NULL;
917 }
918 else
919 {
920 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
921 desc = NULL;
922 }
923 } 909 }
910 }
924#endif 911#endif
925 912
926 if ((port == 0) && (unixpath == NULL)) 913 if ((port == 0) && (unixpath == NULL))
914 {
915 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 LOG (GNUNET_ERROR_TYPE_DEBUG,
938 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
939 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))
927 { 946 {
928 LOG (GNUNET_ERROR_TYPE_ERROR, 947 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
929 _ 948 gai_strerror (ret));
930 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"), 949 GNUNET_free (hostname);
931 serviceName); 950 GNUNET_free_non_null (unixpath);
932 GNUNET_free_non_null (hostname);
933 return GNUNET_SYSERR; 951 return GNUNET_SYSERR;
934 } 952 }
935 if (port == 0) 953 next = res;
954 i = 0;
955 while (NULL != (pos = next))
936 { 956 {
937 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *)); 957 next = pos->ai_next;
938 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t)); 958 if ((disablev6) && (pos->ai_family == AF_INET6))
939 add_unixpath (saddrs, saddrlens, unixpath); 959 continue;
960 i++;
961 }
962 if (0 == i)
963 {
964 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
965 disablev6 ? "IPv4 " : "", hostname);
966 freeaddrinfo (res);
967 GNUNET_free (hostname);
940 GNUNET_free_non_null (unixpath); 968 GNUNET_free_non_null (unixpath);
941 GNUNET_free_non_null (hostname); 969 return GNUNET_SYSERR;
942 *addrs = saddrs;
943 *addr_lens = saddrlens;
944 return 1;
945 } 970 }
946 971 resi = i;
947 if (hostname != NULL) 972 if (NULL != unixpath)
973 resi++;
974 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
975 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
976 i = 0;
977 if (NULL != unixpath)
948 { 978 {
979 add_unixpath (saddrs, saddrlens, unixpath);
980 i++;
981 }
982 next = res;
983 while (NULL != (pos = next))
984 {
985 next = pos->ai_next;
986 if ((disablev6) && (pos->ai_family == AF_INET6))
987 continue;
988 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
989 continue; /* not TCP */
990 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
991 continue; /* huh? */
949#if DEBUG_SERVICE 992#if DEBUG_SERVICE
950 LOG (GNUNET_ERROR_TYPE_DEBUG, 993 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
951 "Resolving `%s' since that is where `%s' will bind to.\n", 994 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
952 hostname, serviceName);
953#endif 995#endif
954 memset (&hints, 0, sizeof (struct addrinfo)); 996 if (pos->ai_family == AF_INET)
955 if (disablev6) 997 {
956 hints.ai_family = AF_INET; 998 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
957 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) || 999 saddrlens[i] = pos->ai_addrlen;
958 (res == NULL)) 1000 saddrs[i] = GNUNET_malloc (saddrlens[i]);
959 { 1001 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
960 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), 1002 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
961 hostname, gai_strerror (ret)); 1003 }
962 GNUNET_free (hostname); 1004 else
963 GNUNET_free_non_null (unixpath); 1005 {
964 return GNUNET_SYSERR; 1006 GNUNET_assert (pos->ai_family == AF_INET6);
965 } 1007 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
966 next = res; 1008 saddrlens[i] = pos->ai_addrlen;
967 i = 0; 1009 saddrs[i] = GNUNET_malloc (saddrlens[i]);
968 while (NULL != (pos = next)) 1010 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
969 { 1011 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
970 next = pos->ai_next; 1012 }
971 if ((disablev6) && (pos->ai_family == AF_INET6)) 1013 i++;
972 continue; 1014 }
973 i++; 1015 GNUNET_free (hostname);
974 } 1016 freeaddrinfo (res);
975 if (0 == i) 1017 resi = i;
976 { 1018 }
977 LOG (GNUNET_ERROR_TYPE_ERROR, 1019 else
978 _("Failed to find %saddress for `%s'.\n"), 1020 {
979 disablev6 ? "IPv4 " : "", hostname); 1021 /* will bind against everything, just set port */
980 freeaddrinfo (res); 1022 if (disablev6)
981 GNUNET_free (hostname); 1023 {
982 GNUNET_free_non_null (unixpath); 1024 /* V4-only */
983 return GNUNET_SYSERR; 1025 resi = 1;
984 }
985 resi = i;
986 if (NULL != unixpath) 1026 if (NULL != unixpath)
987 resi++; 1027 resi++;
1028 i = 0;
988 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); 1029 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
989 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); 1030 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
990 i = 0;
991 if (NULL != unixpath) 1031 if (NULL != unixpath)
992 { 1032 {
993 add_unixpath (saddrs, saddrlens, unixpath); 1033 add_unixpath (saddrs, saddrlens, unixpath);
994 i++; 1034 i++;
995 } 1035 }
996 next = res; 1036 saddrlens[i] = sizeof (struct sockaddr_in);
997 while (NULL != (pos = next)) 1037 saddrs[i] = GNUNET_malloc (saddrlens[i]);
998 { 1038#if HAVE_SOCKADDR_IN_SIN_LEN
999 next = pos->ai_next; 1039 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
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));
1009#endif 1040#endif
1010 if (pos->ai_family == AF_INET) 1041 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1011 { 1042 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
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;
1032 } 1043 }
1033 else 1044 else
1034 { 1045 {
1035 /* will bind against everything, just set port */ 1046 /* dual stack */
1036 if (disablev6) 1047 resi = 2;
1037 { 1048 if (NULL != unixpath)
1038 /* V4-only */ 1049 resi++;
1039 resi = 1; 1050 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1040 if (NULL != unixpath) 1051 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1041 resi++; 1052 i = 0;
1042 i = 0; 1053 if (NULL != unixpath)
1043 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *)); 1054 {
1044 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t)); 1055 add_unixpath (saddrs, saddrlens, unixpath);
1045 if (NULL != unixpath) 1056 i++;
1046 { 1057 }
1047 add_unixpath (saddrs, saddrlens, unixpath); 1058 saddrlens[i] = sizeof (struct sockaddr_in6);
1048 i++; 1059 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1049 }
1050 saddrlens[i] = sizeof (struct sockaddr_in);
1051 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1052#if HAVE_SOCKADDR_IN_SIN_LEN
1053 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1054#endif
1055 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1056 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1057 }
1058 else
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]);
1074#if HAVE_SOCKADDR_IN_SIN_LEN 1060#if HAVE_SOCKADDR_IN_SIN_LEN
1075 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0]; 1061 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1076#endif 1062#endif
1077 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6; 1063 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1078 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port); 1064 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1079 i++; 1065 i++;
1080 saddrlens[i] = sizeof (struct sockaddr_in); 1066 saddrlens[i] = sizeof (struct sockaddr_in);
1081 saddrs[i] = GNUNET_malloc (saddrlens[i]); 1067 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1082#if HAVE_SOCKADDR_IN_SIN_LEN 1068#if HAVE_SOCKADDR_IN_SIN_LEN
1083 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1]; 1069 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1084#endif 1070#endif
1085 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET; 1071 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1086 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port); 1072 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1087 }
1088 } 1073 }
1074 }
1089 GNUNET_free_non_null (unixpath); 1075 GNUNET_free_non_null (unixpath);
1090 *addrs = saddrs; 1076 *addrs = saddrs;
1091 *addr_lens = saddrlens; 1077 *addr_lens = saddrlens;
@@ -1124,37 +1110,36 @@ setup_service (struct GNUNET_SERVICE_Context *sctx)
1124 int flags; 1110 int flags;
1125#endif 1111#endif
1126 1112
1127 if (GNUNET_CONFIGURATION_have_value 1113 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TIMEOUT"))
1128 (sctx->cfg, sctx->serviceName, "TIMEOUT")) 1114 {
1115 if (GNUNET_OK !=
1116 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName,
1117 "TIMEOUT", &idleout))
1129 { 1118 {
1130 if (GNUNET_OK != 1119 LOG (GNUNET_ERROR_TYPE_ERROR,
1131 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName, 1120 _("Specified value for `%s' of service `%s' is invalid\n"),
1132 "TIMEOUT", &idleout)) 1121 "TIMEOUT", sctx->serviceName);
1133 { 1122 return GNUNET_SYSERR;
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;
1140 } 1123 }
1124 sctx->timeout = idleout;
1125 }
1141 else 1126 else
1142 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL; 1127 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1143 1128
1144 if (GNUNET_CONFIGURATION_have_value 1129 if (GNUNET_CONFIGURATION_have_value
1145 (sctx->cfg, sctx->serviceName, "TOLERANT")) 1130 (sctx->cfg, sctx->serviceName, "TOLERANT"))
1131 {
1132 if (GNUNET_SYSERR ==
1133 (tolerant =
1134 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1135 "TOLERANT")))
1146 { 1136 {
1147 if (GNUNET_SYSERR == 1137 LOG (GNUNET_ERROR_TYPE_ERROR,
1148 (tolerant = 1138 _("Specified value for `%s' of service `%s' is invalid\n"),
1149 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, 1139 "TOLERANT", sctx->serviceName);
1150 "TOLERANT"))) 1140 return GNUNET_SYSERR;
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 }
1157 } 1141 }
1142 }
1158 else 1143 else
1159 tolerant = GNUNET_NO; 1144 tolerant = GNUNET_NO;
1160 1145
@@ -1165,48 +1150,45 @@ setup_service (struct GNUNET_SERVICE_Context *sctx)
1165 (NULL != (nfds = getenv ("LISTEN_FDS"))) && 1150 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1166 (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) && 1151 (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1167 (cnt + 4 < FD_SETSIZE)) 1152 (cnt + 4 < FD_SETSIZE))
1153 {
1154 sctx->lsocks =
1155 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1156 while (0 < cnt--)
1168 { 1157 {
1169 sctx->lsocks = 1158 flags = fcntl (3 + cnt, F_GETFD);
1170 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1)); 1159 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1171 while (0 < cnt--) 1160 (NULL ==
1172 { 1161 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1173 flags = fcntl (3 + cnt, F_GETFD); 1162 {
1174 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) || 1163 LOG (GNUNET_ERROR_TYPE_ERROR,
1175 (NULL == 1164 _
1176 (sctx->lsocks[cnt] = 1165 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1177 GNUNET_NETWORK_socket_box_native (3 + cnt)))) 1166 (unsigned int) 3 + cnt);
1178 { 1167 cnt++;
1179 LOG (GNUNET_ERROR_TYPE_ERROR, 1168 while (sctx->lsocks[cnt] != NULL)
1180 _ 1169 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1181 ("Could not access pre-bound socket %u, will try to bind myself\n"), 1170 GNUNET_free (sctx->lsocks);
1182 (unsigned int) 3 + cnt); 1171 sctx->lsocks = NULL;
1183 cnt++; 1172 break;
1184 while (sctx->lsocks[cnt] != NULL) 1173 }
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");
1195 } 1174 }
1175 unsetenv ("LISTEN_PID");
1176 unsetenv ("LISTEN_FDS");
1177 }
1196#endif 1178#endif
1197 1179
1198 if ((sctx->lsocks == NULL) && 1180 if ((sctx->lsocks == NULL) &&
1199 (GNUNET_SYSERR == 1181 (GNUNET_SYSERR ==
1200 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg, 1182 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
1201 &sctx->addrs, &sctx->addrlens))) 1183 &sctx->addrs, &sctx->addrlens)))
1202 return GNUNET_SYSERR; 1184 return GNUNET_SYSERR;
1203 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES; 1185 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1204 sctx->match_uid = 1186 sctx->match_uid =
1205 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, 1187 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1206 "UNIX_MATCH_UID"); 1188 "UNIX_MATCH_UID");
1207 sctx->match_gid = 1189 sctx->match_gid =
1208 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName, 1190 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1209 "UNIX_MATCH_GID"); 1191 "UNIX_MATCH_GID");
1210 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM"); 1192 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1211 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM"); 1193 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1212 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6"); 1194 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
@@ -1228,7 +1210,7 @@ get_user_name (struct GNUNET_SERVICE_Context *sctx)
1228 1210
1229 if (GNUNET_OK != 1211 if (GNUNET_OK !=
1230 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName, 1212 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
1231 "USERNAME", &un)) 1213 "USERNAME", &un))
1232 return NULL; 1214 return NULL;
1233 return un; 1215 return un;
1234} 1216}
@@ -1246,7 +1228,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1246 int len; 1228 int len;
1247 1229
1248 if (NULL == (pif = get_pid_file_name (sctx))) 1230 if (NULL == (pif = get_pid_file_name (sctx)))
1249 return GNUNET_OK; /* no file desired */ 1231 return GNUNET_OK; /* no file desired */
1250 user = get_user_name (sctx); 1232 user = get_user_name (sctx);
1251 rdir = GNUNET_strdup (pif); 1233 rdir = GNUNET_strdup (pif);
1252 len = strlen (rdir); 1234 len = strlen (rdir);
@@ -1254,30 +1236,30 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1254 len--; 1236 len--;
1255 rdir[len] = '\0'; 1237 rdir[len] = '\0';
1256 if (0 != ACCESS (rdir, F_OK)) 1238 if (0 != ACCESS (rdir, F_OK))
1257 { 1239 {
1258 /* we get to create a directory -- and claim it 1240 /* we get to create a directory -- and claim it
1259 * as ours! */ 1241 * as ours! */
1260 GNUNET_DISK_directory_create (rdir); 1242 GNUNET_DISK_directory_create (rdir);
1261 if ((user != NULL) && (0 < strlen (user))) 1243 if ((user != NULL) && (0 < strlen (user)))
1262 GNUNET_DISK_file_change_owner (rdir, user); 1244 GNUNET_DISK_file_change_owner (rdir, user);
1263 } 1245 }
1264 if (0 != ACCESS (rdir, W_OK | X_OK)) 1246 if (0 != ACCESS (rdir, W_OK | X_OK))
1265 { 1247 {
1266 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir); 1248 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1267 GNUNET_free (rdir); 1249 GNUNET_free (rdir);
1268 GNUNET_free_non_null (user); 1250 GNUNET_free_non_null (user);
1269 GNUNET_free (pif); 1251 GNUNET_free (pif);
1270 return GNUNET_SYSERR; 1252 return GNUNET_SYSERR;
1271 } 1253 }
1272 GNUNET_free (rdir); 1254 GNUNET_free (rdir);
1273 pidfd = FOPEN (pif, "w"); 1255 pidfd = FOPEN (pif, "w");
1274 if (pidfd == NULL) 1256 if (pidfd == NULL)
1275 { 1257 {
1276 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif); 1258 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1277 GNUNET_free (pif); 1259 GNUNET_free (pif);
1278 GNUNET_free_non_null (user); 1260 GNUNET_free_non_null (user);
1279 return GNUNET_SYSERR; 1261 return GNUNET_SYSERR;
1280 } 1262 }
1281 if (0 > FPRINTF (pidfd, "%u", pid)) 1263 if (0 > FPRINTF (pidfd, "%u", pid))
1282 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif); 1264 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1283 GNUNET_break (0 == fclose (pidfd)); 1265 GNUNET_break (0 == fclose (pidfd));
@@ -1316,35 +1298,34 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1316 GNUNET_RESOLVER_connect (sctx->cfg); 1298 GNUNET_RESOLVER_connect (sctx->cfg);
1317 if (sctx->lsocks != NULL) 1299 if (sctx->lsocks != NULL)
1318 sctx->server = 1300 sctx->server =
1319 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, 1301 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1320 sctx->timeout, sctx->require_found); 1302 sctx->timeout, sctx->require_found);
1321 else 1303 else
1322 sctx->server = 1304 sctx->server =
1323 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, 1305 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1324 sctx->timeout, sctx->require_found); 1306 sctx->timeout, sctx->require_found);
1325 if (sctx->server == NULL) 1307 if (sctx->server == NULL)
1308 {
1309 if (sctx->addrs != NULL)
1326 { 1310 {
1327 if (sctx->addrs != NULL) 1311 i = 0;
1328 { 1312 while (sctx->addrs[i] != NULL)
1329 i = 0; 1313 {
1330 while (sctx->addrs[i] != NULL) 1314 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1331 { 1315 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1332 LOG (GNUNET_ERROR_TYPE_INFO, 1316 i++;
1333 _("Failed to start `%s' at `%s'\n"), sctx->serviceName, 1317 }
1334 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1335 i++;
1336 }
1337 }
1338 sctx->ret = GNUNET_SYSERR;
1339 return;
1340 } 1318 }
1319 sctx->ret = GNUNET_SYSERR;
1320 return;
1321 }
1341 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN)) 1322 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1342 { 1323 {
1343 /* install a task that will kill the server 1324 /* install a task that will kill the server
1344 * process if the scheduler ever gets a shutdown signal */ 1325 * process if the scheduler ever gets a shutdown signal */
1345 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 1326 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1346 &shutdown_task, sctx->server); 1327 sctx->server);
1347 } 1328 }
1348 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); 1329 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1349 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); 1330 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1350 i = 0; 1331 i = 0;
@@ -1352,23 +1333,22 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1352 sctx->my_handlers[i++].callback_cls = sctx; 1333 sctx->my_handlers[i++].callback_cls = sctx;
1353 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers); 1334 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1354 if (sctx->ready_confirm_fd != -1) 1335 if (sctx->ready_confirm_fd != -1)
1355 { 1336 {
1356 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1)); 1337 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1357 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd)); 1338 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1358 sctx->ready_confirm_fd = -1; 1339 sctx->ready_confirm_fd = -1;
1359 write_pid_file (sctx, getpid ()); 1340 write_pid_file (sctx, getpid ());
1360 } 1341 }
1361 if (sctx->addrs != NULL) 1342 if (sctx->addrs != NULL)
1343 {
1344 i = 0;
1345 while (sctx->addrs[i] != NULL)
1362 { 1346 {
1363 i = 0; 1347 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1364 while (sctx->addrs[i] != NULL) 1348 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1365 { 1349 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 }
1371 } 1350 }
1351 }
1372 sctx->task (sctx->task_cls, sctx->server, sctx->cfg); 1352 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1373} 1353}
1374 1354
@@ -1385,45 +1365,44 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1385 int filedes[2]; 1365 int filedes[2];
1386 1366
1387 if (0 != PIPE (filedes)) 1367 if (0 != PIPE (filedes))
1388 { 1368 {
1389 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe"); 1369 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1390 return GNUNET_SYSERR; 1370 return GNUNET_SYSERR;
1391 } 1371 }
1392 pid = fork (); 1372 pid = fork ();
1393 if (pid < 0) 1373 if (pid < 0)
1394 { 1374 {
1395 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); 1375 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1396 return GNUNET_SYSERR; 1376 return GNUNET_SYSERR;
1397 } 1377 }
1398 if (pid != 0) 1378 if (pid != 0)
1379 {
1380 /* Parent */
1381 char c;
1382
1383 GNUNET_break (0 == CLOSE (filedes[1]));
1384 c = 'X';
1385 if (1 != READ (filedes[0], &c, sizeof (char)))
1386 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1387 fflush (stdout);
1388 switch (c)
1399 { 1389 {
1400 /* Parent */ 1390 case '.':
1401 char c; 1391 exit (0);
1402 1392 case 'I':
1403 GNUNET_break (0 == CLOSE (filedes[1])); 1393 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1404 c = 'X'; 1394 break;
1405 if (1 != READ (filedes[0], &c, sizeof (char))) 1395 case 'S':
1406 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read"); 1396 LOG (GNUNET_ERROR_TYPE_INFO,
1407 fflush (stdout); 1397 _("Service process could not initialize server function\n"));
1408 switch (c) 1398 break;
1409 { 1399 case 'X':
1410 case '.': 1400 LOG (GNUNET_ERROR_TYPE_INFO,
1411 exit (0); 1401 _("Service process failed to report status\n"));
1412 case 'I': 1402 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 */
1426 } 1403 }
1404 exit (1); /* child reported error */
1405 }
1427 GNUNET_break (0 == CLOSE (0)); 1406 GNUNET_break (0 == CLOSE (0));
1428 GNUNET_break (0 == CLOSE (1)); 1407 GNUNET_break (0 == CLOSE (1));
1429 GNUNET_break (0 == CLOSE (filedes[0])); 1408 GNUNET_break (0 == CLOSE (filedes[0]));
@@ -1432,11 +1411,11 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1432 return GNUNET_SYSERR; 1411 return GNUNET_SYSERR;
1433 /* set stdin/stdout to /dev/null */ 1412 /* set stdin/stdout to /dev/null */
1434 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0)) 1413 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1435 { 1414 {
1436 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2"); 1415 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1437 (void) CLOSE (nullfd); 1416 (void) CLOSE (nullfd);
1438 return GNUNET_SYSERR; 1417 return GNUNET_SYSERR;
1439 } 1418 }
1440 (void) CLOSE (nullfd); 1419 (void) CLOSE (nullfd);
1441 /* Detach from controlling terminal */ 1420 /* Detach from controlling terminal */
1442 pid = setsid (); 1421 pid = setsid ();
@@ -1461,36 +1440,35 @@ set_user_id (struct GNUNET_SERVICE_Context *sctx)
1461 char *user; 1440 char *user;
1462 1441
1463 if (NULL == (user = get_user_name (sctx))) 1442 if (NULL == (user = get_user_name (sctx)))
1464 return GNUNET_OK; /* keep */ 1443 return GNUNET_OK; /* keep */
1465#ifndef MINGW 1444#ifndef MINGW
1466 struct passwd *pws; 1445 struct passwd *pws;
1467 1446
1468 errno = 0; 1447 errno = 0;
1469 pws = getpwnam (user); 1448 pws = getpwnam (user);
1470 if (pws == NULL) 1449 if (pws == NULL)
1471 { 1450 {
1472 LOG (GNUNET_ERROR_TYPE_ERROR, 1451 LOG (GNUNET_ERROR_TYPE_ERROR,
1473 _("Cannot obtain information about user `%s': %s\n"), user, 1452 _("Cannot obtain information about user `%s': %s\n"), user,
1474 errno == 0 ? _("No such user") : STRERROR (errno)); 1453 errno == 0 ? _("No such user") : STRERROR (errno));
1475 GNUNET_free (user); 1454 GNUNET_free (user);
1476 return GNUNET_SYSERR; 1455 return GNUNET_SYSERR;
1477 } 1456 }
1478 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) || 1457 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1479#if HAVE_INITGROUPS 1458#if HAVE_INITGROUPS
1480 (0 != initgroups (user, pws->pw_gid)) || 1459 (0 != initgroups (user, pws->pw_gid)) ||
1481#endif 1460#endif
1482 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid))) 1461 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1462 {
1463 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1464 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1483 { 1465 {
1484 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) || 1466 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1485 (0 != setreuid (pws->pw_uid, pws->pw_uid))) 1467 user, STRERROR (errno));
1486 { 1468 GNUNET_free (user);
1487 LOG (GNUNET_ERROR_TYPE_ERROR, 1469 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 }
1493 } 1470 }
1471 }
1494#endif 1472#endif
1495 GNUNET_free (user); 1473 GNUNET_free (user);
1496 return GNUNET_OK; 1474 return GNUNET_OK;
@@ -1506,7 +1484,7 @@ pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1506 char *pif = get_pid_file_name (sctx); 1484 char *pif = get_pid_file_name (sctx);
1507 1485
1508 if (pif == NULL) 1486 if (pif == NULL)
1509 return; /* no PID file */ 1487 return; /* no PID file */
1510 if (0 != UNLINK (pif)) 1488 if (0 != UNLINK (pif))
1511 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif); 1489 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1512 GNUNET_free (pif); 1490 GNUNET_free (pif);
@@ -1528,8 +1506,8 @@ pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1528 */ 1506 */
1529int 1507int
1530GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, 1508GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1531 enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task, 1509 enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task,
1532 void *task_cls) 1510 void *task_cls)
1533{ 1511{
1534#define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0) 1512#define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1535 1513
@@ -1586,23 +1564,21 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1586 goto shutdown; 1564 goto shutdown;
1587#if DEBUG_SERVICE 1565#if DEBUG_SERVICE
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, 1566 LOG (GNUNET_ERROR_TYPE_DEBUG,
1589 "Service `%s' runs with configuration from `%s'\n", serviceName, 1567 "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn);
1590 cfg_fn);
1591#endif 1568#endif
1592 if (GNUNET_OK == 1569 if (GNUNET_OK ==
1593 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", 1570 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", "skew_offset",
1594 "skew_offset", &skew_offset) 1571 &skew_offset) &&
1595 && (GNUNET_OK == 1572 (GNUNET_OK ==
1596 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing", 1573 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "testing",
1597 "skew_variance", 1574 "skew_variance", &skew_variance)))
1598 &skew_variance))) 1575 {
1599 { 1576 clock_offset = skew_offset - skew_variance;
1600 clock_offset = skew_offset - skew_variance; 1577 GNUNET_TIME_set_offset (clock_offset);
1601 GNUNET_TIME_set_offset (clock_offset);
1602#if DEBUG_SERVICE 1578#if DEBUG_SERVICE
1603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll\n", clock_offset); 1579 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll\n", clock_offset);
1604#endif 1580#endif
1605 } 1581 }
1606 /* actually run service */ 1582 /* actually run service */
1607 err = 0; 1583 err = 0;
1608 GNUNET_SCHEDULER_run (&service_task, &sctx); 1584 GNUNET_SCHEDULER_run (&service_task, &sctx);
@@ -1614,11 +1590,11 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1614 1590
1615shutdown: 1591shutdown:
1616 if (sctx.ready_confirm_fd != -1) 1592 if (sctx.ready_confirm_fd != -1)
1617 { 1593 {
1618 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1)) 1594 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1619 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write"); 1595 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1620 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd)); 1596 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1621 } 1597 }
1622 1598
1623 GNUNET_CONFIGURATION_destroy (cfg); 1599 GNUNET_CONFIGURATION_destroy (cfg);
1624 i = 0; 1600 i = 0;
@@ -1649,13 +1625,13 @@ shutdown:
1649 */ 1625 */
1650struct GNUNET_SERVICE_Context * 1626struct GNUNET_SERVICE_Context *
1651GNUNET_SERVICE_start (const char *serviceName, 1627GNUNET_SERVICE_start (const char *serviceName,
1652 const struct GNUNET_CONFIGURATION_Handle *cfg) 1628 const struct GNUNET_CONFIGURATION_Handle *cfg)
1653{ 1629{
1654 int i; 1630 int i;
1655 struct GNUNET_SERVICE_Context *sctx; 1631 struct GNUNET_SERVICE_Context *sctx;
1656 1632
1657 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context)); 1633 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1658 sctx->ready_confirm_fd = -1; /* no daemonizing */ 1634 sctx->ready_confirm_fd = -1; /* no daemonizing */
1659 sctx->ret = GNUNET_OK; 1635 sctx->ret = GNUNET_OK;
1660 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL; 1636 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1661 sctx->serviceName = serviceName; 1637 sctx->serviceName = serviceName;
@@ -1663,24 +1639,24 @@ GNUNET_SERVICE_start (const char *serviceName,
1663 1639
1664 /* setup subsystems */ 1640 /* setup subsystems */
1665 if (GNUNET_OK != setup_service (sctx)) 1641 if (GNUNET_OK != setup_service (sctx))
1666 { 1642 {
1667 GNUNET_SERVICE_stop (sctx); 1643 GNUNET_SERVICE_stop (sctx);
1668 return NULL; 1644 return NULL;
1669 } 1645 }
1670 if (sctx->lsocks != NULL) 1646 if (sctx->lsocks != NULL)
1671 sctx->server = 1647 sctx->server =
1672 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, 1648 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1673 sctx->timeout, sctx->require_found); 1649 sctx->timeout, sctx->require_found);
1674 else 1650 else
1675 sctx->server = 1651 sctx->server =
1676 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, 1652 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1677 sctx->timeout, sctx->require_found); 1653 sctx->timeout, sctx->require_found);
1678 1654
1679 if (NULL == sctx->server) 1655 if (NULL == sctx->server)
1680 { 1656 {
1681 GNUNET_SERVICE_stop (sctx); 1657 GNUNET_SERVICE_stop (sctx);
1682 return NULL; 1658 return NULL;
1683 } 1659 }
1684 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); 1660 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1685 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); 1661 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1686 i = 0; 1662 i = 0;
@@ -1718,12 +1694,12 @@ GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1718 GNUNET_SERVER_destroy (sctx->server); 1694 GNUNET_SERVER_destroy (sctx->server);
1719 GNUNET_free_non_null (sctx->my_handlers); 1695 GNUNET_free_non_null (sctx->my_handlers);
1720 if (sctx->addrs != NULL) 1696 if (sctx->addrs != NULL)
1721 { 1697 {
1722 i = 0; 1698 i = 0;
1723 while (sctx->addrs[i] != NULL) 1699 while (sctx->addrs[i] != NULL)
1724 GNUNET_free (sctx->addrs[i++]); 1700 GNUNET_free (sctx->addrs[i++]);
1725 GNUNET_free (sctx->addrs); 1701 GNUNET_free (sctx->addrs);
1726 } 1702 }
1727 GNUNET_free_non_null (sctx->addrlens); 1703 GNUNET_free_non_null (sctx->addrlens);
1728 GNUNET_free_non_null (sctx->v4_denied); 1704 GNUNET_free_non_null (sctx->v4_denied);
1729 GNUNET_free_non_null (sctx->v6_denied); 1705 GNUNET_free_non_null (sctx->v6_denied);
diff --git a/src/util/signal.c b/src/util/signal.c
index a986b6260..c3bb718bf 100644
--- a/src/util/signal.c
+++ b/src/util/signal.c
@@ -63,7 +63,7 @@ GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler)
63 sig.sa_handler = (void *) handler; 63 sig.sa_handler = (void *) handler;
64 sigemptyset (&sig.sa_mask); 64 sigemptyset (&sig.sa_mask);
65#ifdef SA_INTERRUPT 65#ifdef SA_INTERRUPT
66 sig.sa_flags = SA_INTERRUPT; /* SunOS */ 66 sig.sa_flags = SA_INTERRUPT; /* SunOS */
67#else 67#else
68 sig.sa_flags = SA_RESTART; 68 sig.sa_flags = SA_RESTART;
69#endif 69#endif
@@ -72,15 +72,15 @@ GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler)
72 if (signum == GNUNET_SIGCHLD) 72 if (signum == GNUNET_SIGCHLD)
73 w32_sigchld_handler = handler; 73 w32_sigchld_handler = handler;
74 else 74 else
75 { 75 {
76 __p_sig_fn_t sigret = signal (signum, (__p_sig_fn_t) handler); 76 __p_sig_fn_t sigret = signal (signum, (__p_sig_fn_t) handler);
77 77
78 if (sigret == SIG_ERR) 78 if (sigret == SIG_ERR)
79 { 79 {
80 LOG (GNUNET_ERROR_TYPE_WARNING, 80 LOG (GNUNET_ERROR_TYPE_WARNING, _("signal (%d, %p) returned %d.\n"),
81 _("signal (%d, %p) returned %d.\n"), signum, handler, sigret); 81 signum, handler, sigret);
82 }
83 } 82 }
83 }
84#endif 84#endif
85 return ret; 85 return ret;
86} 86}
diff --git a/src/util/strings.c b/src/util/strings.c
index d48d552a1..dc4037a1f 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -59,8 +59,7 @@
59 * (or number of bytes that would have been written) 59 * (or number of bytes that would have been written)
60 */ 60 */
61size_t 61size_t
62GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, 62GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
63 ...)
64{ 63{
65 size_t needed; 64 size_t needed;
66 size_t slen; 65 size_t slen;
@@ -70,18 +69,18 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count,
70 needed = 0; 69 needed = 0;
71 va_start (ap, count); 70 va_start (ap, count);
72 while (count > 0) 71 while (count > 0)
73 { 72 {
74 s = va_arg (ap, const char *); 73 s = va_arg (ap, const char *);
75 74
76 slen = strlen (s) + 1; 75 slen = strlen (s) + 1;
77 if (buffer != NULL) 76 if (buffer != NULL)
78 { 77 {
79 GNUNET_assert (needed + slen <= size); 78 GNUNET_assert (needed + slen <= size);
80 memcpy (&buffer[needed], s, slen); 79 memcpy (&buffer[needed], s, slen);
81 }
82 needed += slen;
83 count--;
84 } 80 }
81 needed += slen;
82 count--;
83 }
85 va_end (ap); 84 va_end (ap);
86 return needed; 85 return needed;
87} 86}
@@ -102,7 +101,7 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count,
102 */ 101 */
103unsigned int 102unsigned int
104GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, 103GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
105 unsigned int count, ...) 104 unsigned int count, ...)
106{ 105{
107 unsigned int start; 106 unsigned int start;
108 unsigned int needed; 107 unsigned int needed;
@@ -112,21 +111,21 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
112 needed = 0; 111 needed = 0;
113 va_start (ap, count); 112 va_start (ap, count);
114 while (count > 0) 113 while (count > 0)
114 {
115 r = va_arg (ap, const char **);
116
117 start = needed;
118 while ((needed < size) && (buffer[needed] != '\0'))
119 needed++;
120 if (needed == size)
115 { 121 {
116 r = va_arg (ap, const char **); 122 va_end (ap);
117 123 return 0; /* error */
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--;
129 } 124 }
125 *r = &buffer[start];
126 needed++; /* skip 0-termination */
127 count--;
128 }
130 va_end (ap); 129 va_end (ap);
131 return needed; 130 return needed;
132} 131}
@@ -145,25 +144,25 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
145 char *ret; 144 char *ret;
146 145
147 if (size > 5 * 1024) 146 if (size > 5 * 1024)
147 {
148 size = size / 1024;
149 unit = "KiB";
150 if (size > 5 * 1024)
148 { 151 {
149 size = size / 1024; 152 size = size / 1024;
150 unit = "KiB"; 153 unit = "MiB";
151 if (size > 5 * 1024) 154 if (size > 5 * 1024)
152 { 155 {
153 size = size / 1024; 156 size = size / 1024;
154 unit = "MiB"; 157 unit = "GiB";
155 if (size > 5 * 1024) 158 if (size > 5 * 1024)
156 { 159 {
157 size = size / 1024; 160 size = size / 1024;
158 unit = "GiB"; 161 unit = "TiB";
159 if (size > 5 * 1024) 162 }
160 { 163 }
161 size = size / 1024;
162 unit = "TiB";
163 }
164 }
165 }
166 } 164 }
165 }
167 ret = GNUNET_malloc (32); 166 ret = GNUNET_malloc (32);
168 GNUNET_snprintf (ret, 32, "%llu %s", size, unit); 167 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
169 return ret; 168 return ret;
@@ -179,26 +178,42 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
179 */ 178 */
180int 179int
181GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, 180GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
182 unsigned long long *size) 181 unsigned long long *size)
183{ 182{
184 struct { 183 struct
185 const char *name; 184 {
185 const char *name;
186 unsigned long long value; 186 unsigned long long value;
187 } table[] = { 187 } table[] =
188 { "B", 1 }, 188 {
189 { "KiB", 1024 }, 189 {
190 { "kB", 1000 }, 190 "B", 1},
191 { "MiB", 1024 * 1024 }, 191 {
192 { "MB", 1000 * 1000 }, 192 "KiB", 1024},
193 { "GiB", 1024 * 1024 * 1024 }, 193 {
194 { "GB", 1000 * 1000 * 1000 }, 194 "kB", 1000},
195 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL }, 195 {
196 { "TB", 1000LL * 1000LL * 1000LL * 1024LL }, 196 "MiB", 1024 * 1024},
197 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL }, 197 {
198 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL}, 198 "MB", 1000 * 1000},
199 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, 199 {
200 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL }, 200 "GiB", 1024 * 1024 * 1024},
201 { NULL, 0 } 201 {
202 "GB", 1000 * 1000 * 1000},
203 {
204 "TiB", 1024LL * 1024LL * 1024LL * 1024LL},
205 {
206 "TB", 1000LL * 1000LL * 1000LL * 1024LL},
207 {
208 "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
209 {
210 "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
211 {
212 "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
213 {
214 "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
215 {
216 NULL, 0}
202 }; 217 };
203 unsigned long long ret; 218 unsigned long long ret;
204 char *in; 219 char *in;
@@ -211,9 +226,8 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
211 in = GNUNET_strdup (fancy_size); 226 in = GNUNET_strdup (fancy_size);
212 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) 227 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
213 { 228 {
214 i=0; 229 i = 0;
215 while ( (table[i].name != NULL) && 230 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
216 (0 != strcasecmp (table[i].name, tok) ) )
217 i++; 231 i++;
218 if (table[i].name != NULL) 232 if (table[i].name != NULL)
219 last *= table[i].value; 233 last *= table[i].value;
@@ -222,8 +236,8 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
222 ret += last; 236 ret += last;
223 last = 0; 237 last = 0;
224 if (1 != sscanf (tok, "%llu", &last)) 238 if (1 != sscanf (tok, "%llu", &last))
225 return GNUNET_SYSERR; /* expected number */ 239 return GNUNET_SYSERR; /* expected number */
226 } 240 }
227 } 241 }
228 ret += last; 242 ret += last;
229 *size = ret; 243 *size = ret;
@@ -241,43 +255,54 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
241 */ 255 */
242int 256int
243GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size, 257GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
244 struct GNUNET_TIME_Relative *rtime) 258 struct GNUNET_TIME_Relative *rtime)
245{ 259{
246 struct { 260 struct
247 const char *name; 261 {
262 const char *name;
248 unsigned long long value; 263 unsigned long long value;
249 } table[] = { 264 } table[] =
250 { "ms", 1 }, 265 {
251 { "s", 1000 }, 266 {
252 { "\"", 1000 }, 267 "ms", 1},
253 { "min", 60 * 1000 }, 268 {
254 { "minutes", 60 * 1000 }, 269 "s", 1000},
255 { "'", 60 * 1000 }, 270 {
256 { "h", 60 * 60 * 1000 }, 271 "\"", 1000},
257 { "d", 24 * 60 * 60 * 1000 }, 272 {
258 { "a", 31557600 /* year */ }, 273 "min", 60 * 1000},
259 { NULL, 0 } 274 {
275 "minutes", 60 * 1000},
276 {
277 "'", 60 * 1000},
278 {
279 "h", 60 * 60 * 1000},
280 {
281 "d", 24 * 60 * 60 * 1000},
282 {
283 "a", 31557600 /* year */ },
284 {
285 NULL, 0}
260 }; 286 };
261 unsigned long long ret; 287 unsigned long long ret;
262 char *in; 288 char *in;
263 const char *tok; 289 const char *tok;
264 unsigned long long last; 290 unsigned long long last;
265 unsigned int i; 291 unsigned int i;
266 292
267 if ((0 == strcasecmp (fancy_size, "infinity")) || 293 if ((0 == strcasecmp (fancy_size, "infinity")) ||
268 (0 == strcasecmp (fancy_size, "forever"))) 294 (0 == strcasecmp (fancy_size, "forever")))
269 { 295 {
270 *rtime = GNUNET_TIME_UNIT_FOREVER_REL; 296 *rtime = GNUNET_TIME_UNIT_FOREVER_REL;
271 return GNUNET_OK; 297 return GNUNET_OK;
272 } 298 }
273 ret = 0; 299 ret = 0;
274 last = 0; 300 last = 0;
275 in = GNUNET_strdup (fancy_size); 301 in = GNUNET_strdup (fancy_size);
276 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) 302 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
277 { 303 {
278 i=0; 304 i = 0;
279 while ( (table[i].name != NULL) && 305 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
280 (0 != strcasecmp (table[i].name, tok) ) )
281 i++; 306 i++;
282 if (table[i].name != NULL) 307 if (table[i].name != NULL)
283 last *= table[i].value; 308 last *= table[i].value;
@@ -286,8 +311,8 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
286 ret += last; 311 ret += last;
287 last = 0; 312 last = 0;
288 if (1 != sscanf (tok, "%llu", &last)) 313 if (1 != sscanf (tok, "%llu", &last))
289 return GNUNET_SYSERR; /* expected number */ 314 return GNUNET_SYSERR; /* expected number */
290 } 315 }
291 } 316 }
292 ret += last; 317 ret += last;
293 rtime->rel_value = (uint64_t) ret; 318 rtime->rel_value = (uint64_t) ret;
@@ -317,35 +342,35 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
317 342
318 cd = iconv_open ("UTF-8", charset); 343 cd = iconv_open ("UTF-8", charset);
319 if (cd == (iconv_t) - 1) 344 if (cd == (iconv_t) - 1)
320 { 345 {
321 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open"); 346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
322 LOG (GNUNET_ERROR_TYPE_WARNING, 347 LOG (GNUNET_ERROR_TYPE_WARNING, _("Character set requested was `%s'\n"),
323 _("Character set requested was `%s'\n"), charset); 348 charset);
324 ret = GNUNET_malloc (len + 1); 349 ret = GNUNET_malloc (len + 1);
325 memcpy (ret, input, len); 350 memcpy (ret, input, len);
326 ret[len] = '\0'; 351 ret[len] = '\0';
327 return ret; 352 return ret;
328 } 353 }
329 tmpSize = 3 * len + 4; 354 tmpSize = 3 * len + 4;
330 tmp = GNUNET_malloc (tmpSize); 355 tmp = GNUNET_malloc (tmpSize);
331 itmp = tmp; 356 itmp = tmp;
332 finSize = tmpSize; 357 finSize = tmpSize;
333 if (iconv (cd, 358 if (iconv (cd,
334#if FREEBSD || DARWIN || WINDOWS 359#if FREEBSD || DARWIN || WINDOWS
335 (const char **) &input, 360 (const char **) &input,
336#else 361#else
337 (char **) &input, 362 (char **) &input,
338#endif 363#endif
339 &len, &itmp, &finSize) == SIZE_MAX) 364 &len, &itmp, &finSize) == SIZE_MAX)
340 { 365 {
341 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv"); 366 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
342 iconv_close (cd); 367 iconv_close (cd);
343 GNUNET_free (tmp); 368 GNUNET_free (tmp);
344 ret = GNUNET_malloc (len + 1); 369 ret = GNUNET_malloc (len + 1);
345 memcpy (ret, input, len); 370 memcpy (ret, input, len);
346 ret[len] = '\0'; 371 ret[len] = '\0';
347 return ret; 372 return ret;
348 } 373 }
349 ret = GNUNET_malloc (tmpSize - finSize + 1); 374 ret = GNUNET_malloc (tmpSize - finSize + 1);
350 memcpy (ret, tmp, tmpSize - finSize); 375 memcpy (ret, tmp, tmpSize - finSize);
351 ret[tmpSize - finSize] = '\0'; 376 ret[tmpSize - finSize] = '\0';
@@ -392,89 +417,88 @@ GNUNET_STRINGS_filename_expand (const char *fil)
392 /* absolute path, just copy */ 417 /* absolute path, just copy */
393 return GNUNET_strdup (fil); 418 return GNUNET_strdup (fil);
394 if (fil[0] == '~') 419 if (fil[0] == '~')
420 {
421 fm = getenv ("HOME");
422 if (fm == NULL)
395 { 423 {
396 fm = getenv ("HOME"); 424 LOG (GNUNET_ERROR_TYPE_WARNING,
397 if (fm == NULL) 425 _("Failed to expand `$HOME': environment variable `HOME' not set"));
398 { 426 return NULL;
399 LOG (GNUNET_ERROR_TYPE_WARNING,
400 _
401 ("Failed to expand `$HOME': environment variable `HOME' not set"));
402 return NULL;
403 }
404 fm = GNUNET_strdup (fm);
405 /* do not copy '~' */
406 fil_ptr = fil + 1;
407
408 /* skip over dir seperator to be consistent */
409 if (fil_ptr[0] == DIR_SEPARATOR)
410 fil_ptr++;
411 } 427 }
428 fm = GNUNET_strdup (fm);
429 /* do not copy '~' */
430 fil_ptr = fil + 1;
431
432 /* skip over dir seperator to be consistent */
433 if (fil_ptr[0] == DIR_SEPARATOR)
434 fil_ptr++;
435 }
412 else 436 else
437 {
438 /* relative path */
439 fil_ptr = fil;
440 len = 512;
441 fm = NULL;
442 while (1)
413 { 443 {
414 /* relative path */ 444 buffer = GNUNET_malloc (len);
415 fil_ptr = fil; 445 if (getcwd (buffer, len) != NULL)
416 len = 512; 446 {
417 fm = NULL; 447 fm = buffer;
418 while (1) 448 break;
419 { 449 }
420 buffer = GNUNET_malloc (len); 450 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
421 if (getcwd (buffer, len) != NULL) 451 {
422 { 452 len *= 2;
423 fm = buffer; 453 GNUNET_free (buffer);
424 break; 454 continue;
425 } 455 }
426 if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) 456 GNUNET_free (buffer);
427 { 457 break;
428 len *= 2; 458 }
429 GNUNET_free (buffer); 459 if (fm == NULL)
430 continue; 460 {
431 } 461 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
432 GNUNET_free (buffer); 462 buffer = getenv ("PWD"); /* alternative */
433 break; 463 if (buffer != NULL)
434 } 464 fm = GNUNET_strdup (buffer);
435 if (fm == NULL)
436 {
437 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
438 buffer = getenv ("PWD"); /* alternative */
439 if (buffer != NULL)
440 fm = GNUNET_strdup (buffer);
441 }
442 if (fm == NULL)
443 fm = GNUNET_strdup ("./"); /* give up */
444 } 465 }
466 if (fm == NULL)
467 fm = GNUNET_strdup ("./"); /* give up */
468 }
445 n = strlen (fm) + 1 + strlen (fil_ptr) + 1; 469 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
446 buffer = GNUNET_malloc (n); 470 buffer = GNUNET_malloc (n);
447 GNUNET_snprintf (buffer, n, "%s%s%s", fm, 471 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
448 (fm[strlen (fm) - 1] == 472 (fm[strlen (fm) - 1] ==
449 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr); 473 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
450 GNUNET_free (fm); 474 GNUNET_free (fm);
451 return buffer; 475 return buffer;
452#else 476#else
453 fn = GNUNET_malloc (MAX_PATH + 1); 477 fn = GNUNET_malloc (MAX_PATH + 1);
454 478
455 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS) 479 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
456 { 480 {
457 SetErrnoFromWinError (lRet); 481 SetErrnoFromWinError (lRet);
458 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); 482 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
459 return NULL; 483 return NULL;
460 } 484 }
461 /* is the path relative? */ 485 /* is the path relative? */
462 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0)) 486 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
487 {
488 char szCurDir[MAX_PATH + 1];
489
490 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
491 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
463 { 492 {
464 char szCurDir[MAX_PATH + 1]; 493 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
465 494 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
466 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir); 495 return NULL;
467 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
468 {
469 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
470 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
471 return NULL;
472 }
473 buffer = GNUNET_malloc (MAX_PATH + 1);
474 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
475 GNUNET_free (fn);
476 fn = buffer;
477 } 496 }
497 buffer = GNUNET_malloc (MAX_PATH + 1);
498 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
499 GNUNET_free (fn);
500 fn = buffer;
501 }
478 502
479 return fn; 503 return fn;
480#endif 504#endif
@@ -497,25 +521,25 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
497 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) 521 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
498 return GNUNET_strdup (_("eternity")); 522 return GNUNET_strdup (_("eternity"));
499 if (dval > 5 * 1000) 523 if (dval > 5 * 1000)
524 {
525 dval = dval / 1000;
526 unit = _( /* time unit */ "s");
527 if (dval > 5 * 60)
500 { 528 {
501 dval = dval / 1000; 529 dval = dval / 60;
502 unit = _( /* time unit */ "s"); 530 unit = _( /* time unit */ "m");
503 if (dval > 5 * 60) 531 if (dval > 5 * 60)
504 { 532 {
505 dval = dval / 60; 533 dval = dval / 60;
506 unit = _( /* time unit */ "m"); 534 unit = _( /* time unit */ "h");
507 if (dval > 5 * 60) 535 if (dval > 5 * 24)
508 { 536 {
509 dval = dval / 60; 537 dval = dval / 24;
510 unit = _( /* time unit */ "h"); 538 unit = _( /* time unit */ " days");
511 if (dval > 5 * 24) 539 }
512 { 540 }
513 dval = dval / 24;
514 unit = _( /* time unit */ " days");
515 }
516 }
517 }
518 } 541 }
542 }
519 GNUNET_asprintf (&ret, "%llu %s", dval, unit); 543 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
520 return ret; 544 return ret;
521} 545}
diff --git a/src/util/test_bio.c b/src/util/test_bio.c
index e72c95295..df4becdd1 100644
--- a/src/util/test_bio.c
+++ b/src/util/test_bio.c
@@ -57,17 +57,16 @@ 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, 69 GNUNET_CONTAINER_meta_data_test_equal (metaDataR, metaDataW));
70 metaDataW));
71 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_int64 (fileR, &testNum)); 70 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_int64 (fileR, &testNum));
72 GNUNET_BIO_read_close (fileR, &msg); 71 GNUNET_BIO_read_close (fileR, &msg);
73 GNUNET_CONTAINER_meta_data_destroy (metaDataW); 72 GNUNET_CONTAINER_meta_data_destroy (metaDataW);
@@ -94,8 +93,8 @@ test_nullstring_rw ()
94 fileR = GNUNET_BIO_read_open (fileName); 93 fileR = GNUNET_BIO_read_open (fileName);
95 GNUNET_assert (NULL != fileR); 94 GNUNET_assert (NULL != fileR);
96 GNUNET_assert (GNUNET_OK == 95 GNUNET_assert (GNUNET_OK ==
97 GNUNET_BIO_read_string (fileR, "Read string error", 96 GNUNET_BIO_read_string (fileR, "Read string error",
98 &readResultString, 200)); 97 &readResultString, 200));
99 GNUNET_assert (NULL == readResultString); 98 GNUNET_assert (NULL == readResultString);
100 GNUNET_BIO_read_close (fileR, &msg); 99 GNUNET_BIO_read_close (fileR, &msg);
101 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName)); 100 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
@@ -122,8 +121,8 @@ test_emptystring_rw ()
122 GNUNET_assert (NULL != fileR); 121 GNUNET_assert (NULL != fileR);
123 readResultString = NULL; 122 readResultString = NULL;
124 GNUNET_assert (GNUNET_OK == 123 GNUNET_assert (GNUNET_OK ==
125 GNUNET_BIO_read_string (fileR, "Read string error", 124 GNUNET_BIO_read_string (fileR, "Read string error",
126 &readResultString, 200)); 125 &readResultString, 200));
127 GNUNET_free (readResultString); 126 GNUNET_free (readResultString);
128 GNUNET_BIO_read_close (fileR, &msg); 127 GNUNET_BIO_read_close (fileR, &msg);
129 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName)); 128 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
@@ -149,8 +148,8 @@ test_bigstring_rw ()
149 GNUNET_assert (NULL != fileR); 148 GNUNET_assert (NULL != fileR);
150 readResultString = NULL; 149 readResultString = NULL;
151 GNUNET_assert (GNUNET_SYSERR == 150 GNUNET_assert (GNUNET_SYSERR ==
152 GNUNET_BIO_read_string (fileR, "Read string error", 151 GNUNET_BIO_read_string (fileR, "Read string error",
153 &readResultString, 1)); 152 &readResultString, 1));
154 GNUNET_assert (NULL == readResultString); 153 GNUNET_assert (NULL == readResultString);
155 msg = NULL; 154 msg = NULL;
156 GNUNET_BIO_read_close (fileR, &msg); 155 GNUNET_BIO_read_close (fileR, &msg);
@@ -182,8 +181,8 @@ test_bigmeta_rw ()
182 GNUNET_assert (NULL != fileR); 181 GNUNET_assert (NULL != fileR);
183 metaDataR = NULL; 182 metaDataR = NULL;
184 GNUNET_assert (GNUNET_SYSERR == 183 GNUNET_assert (GNUNET_SYSERR ==
185 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 184 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
186 &metaDataR)); 185 &metaDataR));
187 msg = NULL; 186 msg = NULL;
188 GNUNET_BIO_read_close (fileR, &msg); 187 GNUNET_BIO_read_close (fileR, &msg);
189 GNUNET_free (msg); 188 GNUNET_free (msg);
@@ -204,8 +203,8 @@ test_directory_r ()
204 fileR = GNUNET_BIO_read_open ("/dev"); 203 fileR = GNUNET_BIO_read_open ("/dev");
205 GNUNET_assert (NULL != fileR); 204 GNUNET_assert (NULL != fileR);
206 GNUNET_assert (GNUNET_SYSERR == 205 GNUNET_assert (GNUNET_SYSERR ==
207 GNUNET_BIO_read (fileR, "Read error", readResult, 206 GNUNET_BIO_read (fileR, "Read error", readResult,
208 sizeof (readResult))); 207 sizeof (readResult)));
209 msg = NULL; 208 msg = NULL;
210 GNUNET_BIO_read_close (fileR, &msg); 209 GNUNET_BIO_read_close (fileR, &msg);
211 GNUNET_free (msg); 210 GNUNET_free (msg);
@@ -268,18 +267,18 @@ test_fullfile_rw ()
268 fileR = GNUNET_BIO_read_open ("/dev/null"); 267 fileR = GNUNET_BIO_read_open ("/dev/null");
269 GNUNET_assert (NULL != fileR); 268 GNUNET_assert (NULL != fileR);
270 GNUNET_assert (GNUNET_SYSERR == 269 GNUNET_assert (GNUNET_SYSERR ==
271 GNUNET_BIO_read (fileR, "Read error", readResult, 270 GNUNET_BIO_read (fileR, "Read error", readResult,
272 sizeof (readResult))); 271 sizeof (readResult)));
273 readResultString = NULL; 272 readResultString = NULL;
274 GNUNET_assert (GNUNET_SYSERR == 273 GNUNET_assert (GNUNET_SYSERR ==
275 GNUNET_BIO_read_string (fileR, "Read string error", 274 GNUNET_BIO_read_string (fileR, "Read string error",
276 &readResultString, 200)); 275 &readResultString, 200));
277 GNUNET_assert (NULL == readResultString); 276 GNUNET_assert (NULL == readResultString);
278 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_int64 (fileR, &testNum)); 277 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_int64 (fileR, &testNum));
279 metaDataR = NULL; 278 metaDataR = NULL;
280 GNUNET_assert (GNUNET_SYSERR == 279 GNUNET_assert (GNUNET_SYSERR ==
281 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 280 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
282 &metaDataR)); 281 &metaDataR));
283 msg = NULL; 282 msg = NULL;
284 GNUNET_BIO_read_close (fileR, &msg); 283 GNUNET_BIO_read_close (fileR, &msg);
285 GNUNET_free (msg); 284 GNUNET_free (msg);
@@ -306,8 +305,8 @@ test_fakestring_rw ()
306 fileR = GNUNET_BIO_read_open (fileName); 305 fileR = GNUNET_BIO_read_open (fileName);
307 GNUNET_assert (NULL != fileR); 306 GNUNET_assert (NULL != fileR);
308 GNUNET_assert (GNUNET_SYSERR == 307 GNUNET_assert (GNUNET_SYSERR ==
309 GNUNET_BIO_read_string (fileR, "Read string error", 308 GNUNET_BIO_read_string (fileR, "Read string error",
310 &readResult, 200)); 309 &readResult, 200));
311 msg = NULL; 310 msg = NULL;
312 GNUNET_BIO_read_close (fileR, &msg); 311 GNUNET_BIO_read_close (fileR, &msg);
313 GNUNET_free (msg); 312 GNUNET_free (msg);
@@ -335,8 +334,8 @@ test_fakemeta_rw ()
335 GNUNET_assert (NULL != fileR); 334 GNUNET_assert (NULL != fileR);
336 metaDataR = NULL; 335 metaDataR = NULL;
337 GNUNET_assert (GNUNET_SYSERR == 336 GNUNET_assert (GNUNET_SYSERR ==
338 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 337 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
339 &metaDataR)); 338 &metaDataR));
340 GNUNET_assert (NULL == metaDataR); 339 GNUNET_assert (NULL == metaDataR);
341 msg = NULL; 340 msg = NULL;
342 GNUNET_BIO_read_close (fileR, &msg); 341 GNUNET_BIO_read_close (fileR, &msg);
@@ -365,8 +364,8 @@ test_fakebigmeta_rw ()
365 GNUNET_assert (NULL != fileR); 364 GNUNET_assert (NULL != fileR);
366 metaDataR = NULL; 365 metaDataR = NULL;
367 GNUNET_assert (GNUNET_SYSERR == 366 GNUNET_assert (GNUNET_SYSERR ==
368 GNUNET_BIO_read_meta_data (fileR, "Read meta error", 367 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
369 &metaDataR)); 368 &metaDataR));
370 msg = NULL; 369 msg = NULL;
371 GNUNET_BIO_read_close (fileR, &msg); 370 GNUNET_BIO_read_close (fileR, &msg);
372 GNUNET_free (msg); 371 GNUNET_free (msg);
diff --git a/src/util/test_client.c b/src/util/test_client.c
index ad88874f2..bb7d5008a 100644
--- a/src/util/test_client.c
+++ b/src/util/test_client.c
@@ -71,25 +71,24 @@ copy_msg (void *cls, size_t size, void *buf)
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) == 81 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
82 ntohs (message->size));
83 cc = GNUNET_malloc (sizeof (struct CopyContext)); 82 cc = GNUNET_malloc (sizeof (struct CopyContext));
84 cc->client = client; 83 cc->client = client;
85 cpy = GNUNET_malloc (ntohs (message->size)); 84 cpy = GNUNET_malloc (ntohs (message->size));
86 memcpy (cpy, message, ntohs (message->size)); 85 memcpy (cpy, message, ntohs (message->size));
87 cc->cpy = cpy; 86 cc->cpy = cpy;
88 GNUNET_assert (NULL != 87 GNUNET_assert (NULL !=
89 GNUNET_SERVER_notify_transmit_ready (client, 88 GNUNET_SERVER_notify_transmit_ready (client,
90 ntohs (message->size), 89 ntohs (message->size),
91 GNUNET_TIME_UNIT_SECONDS, 90 GNUNET_TIME_UNIT_SECONDS,
92 &copy_msg, cc)); 91 &copy_msg, cc));
93} 92}
94 93
95 94
@@ -105,13 +104,11 @@ recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
105 int *ok = cls; 104 int *ok = cls;
106 struct GNUNET_MessageHeader msg; 105 struct GNUNET_MessageHeader msg;
107 106
108 GNUNET_assert (got != NULL); /* timeout */ 107 GNUNET_assert (got != NULL); /* timeout */
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n");
110 "Receiving bounce, checking content\n");
111 msg.type = htons (MY_TYPE); 109 msg.type = htons (MY_TYPE);
112 msg.size = htons (sizeof (struct GNUNET_MessageHeader)); 110 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
113 GNUNET_assert (0 == 111 GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
114 memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
115 GNUNET_CLIENT_disconnect (client, GNUNET_YES); 112 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
116 client = NULL; 113 client = NULL;
117 GNUNET_SERVER_destroy (server); 114 GNUNET_SERVER_destroy (server);
@@ -151,9 +148,9 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 sa.sin_family = AF_INET; 148 sa.sin_family = AF_INET;
152 sa.sin_port = htons (PORT); 149 sa.sin_port = htons (PORT);
153 server = 150 server =
154 GNUNET_SERVER_create (NULL, NULL, sap, slens, 151 GNUNET_SERVER_create (NULL, NULL, sap, slens,
155 GNUNET_TIME_relative_multiply 152 GNUNET_TIME_relative_multiply
156 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO); 153 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
157 GNUNET_assert (server != NULL); 154 GNUNET_assert (server != NULL);
158 handlers[0].callback_cls = cls; 155 handlers[0].callback_cls = cls;
159 handlers[1].callback_cls = cls; 156 handlers[1].callback_cls = cls;
@@ -161,15 +158,15 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 client = GNUNET_CLIENT_connect (MYNAME, cfg); 158 client = GNUNET_CLIENT_connect (MYNAME, cfg);
162 GNUNET_assert (client != NULL); 159 GNUNET_assert (client != NULL);
163 GNUNET_assert (NULL != 160 GNUNET_assert (NULL !=
164 GNUNET_CLIENT_notify_transmit_ready (client, 161 GNUNET_CLIENT_notify_transmit_ready (client,
165 sizeof (struct 162 sizeof (struct
166 GNUNET_MessageHeader), 163 GNUNET_MessageHeader),
167 GNUNET_TIME_UNIT_SECONDS, 164 GNUNET_TIME_UNIT_SECONDS,
168 GNUNET_NO, &make_msg, 165 GNUNET_NO, &make_msg,
169 NULL)); 166 NULL));
170 GNUNET_CLIENT_receive (client, &recv_bounce, cls, 167 GNUNET_CLIENT_receive (client, &recv_bounce, cls,
171 GNUNET_TIME_relative_multiply 168 GNUNET_TIME_relative_multiply
172 (GNUNET_TIME_UNIT_MILLISECONDS, 10000)); 169 (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
173} 170}
174 171
175 172
@@ -184,10 +181,9 @@ check ()
184 181
185 cfg = GNUNET_CONFIGURATION_create (); 182 cfg = GNUNET_CONFIGURATION_create ();
186 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT); 183 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
187 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", 184 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost");
188 "localhost");
189 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 185 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
190 "localhost"); 186 "localhost");
191 ok = 1; 187 ok = 1;
192 GNUNET_SCHEDULER_run (&task, &ok); 188 GNUNET_SCHEDULER_run (&task, &ok);
193 GNUNET_CONFIGURATION_destroy (cfg); 189 GNUNET_CONFIGURATION_destroy (cfg);
@@ -201,11 +197,11 @@ main (int argc, char *argv[])
201 197
202 GNUNET_log_setup ("test_client", 198 GNUNET_log_setup ("test_client",
203#if VERBOSE 199#if VERBOSE
204 "DEBUG", 200 "DEBUG",
205#else 201#else
206 "WARNING", 202 "WARNING",
207#endif 203#endif
208 NULL); 204 NULL);
209 ret += check (); 205 ret += check ();
210 206
211 return ret; 207 return ret;
diff --git a/src/util/test_common_allocation.c b/src/util/test_common_allocation.c
index 2fb1329b1..950f0cedb 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 a940d3c07..cf291161e 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
@@ -44,9 +44,7 @@ main (int argc, char *argv[])
44 unsigned int logs = 0; 44 unsigned int logs = 0;
45 45
46 if (0 != setenv ("GNUNET_FORCE_LOG", "", 1)) 46 if (0 != setenv ("GNUNET_FORCE_LOG", "", 1))
47 fprintf (stderr, 47 fprintf (stderr, "Failed to setenv: %s\n", strerror (errno));
48 "Failed to setenv: %s\n",
49 strerror (errno));
50 GNUNET_log_setup ("test-common-logging", "DEBUG", "/dev/null"); 48 GNUNET_log_setup ("test-common-logging", "DEBUG", "/dev/null");
51 GNUNET_logger_add (&my_log, &logs); 49 GNUNET_logger_add (&my_log, &logs);
52 GNUNET_logger_add (&my_log, &logs); 50 GNUNET_logger_add (&my_log, &logs);
@@ -61,26 +59,23 @@ main (int argc, char *argv[])
61 /* the last 6 calls should be merged (repated bulk messages!) */ 59 /* the last 6 calls should be merged (repated bulk messages!) */
62 GNUNET_logger_remove (&my_log, &logs); 60 GNUNET_logger_remove (&my_log, &logs);
63 if (logs != 4) 61 if (logs != 4)
64 { 62 {
65 fprintf (stdout, "Expected 4 log calls, got %u\n", logs); 63 fprintf (stdout, "Expected 4 log calls, got %u\n", logs);
66 failureCount++; 64 failureCount++;
67 } 65 }
68 GNUNET_break (0 == 66 GNUNET_break (0 ==
69 strcmp (_("ERROR"), 67 strcmp (_("ERROR"),
70 GNUNET_error_type_to_string 68 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_ERROR)));
71 (GNUNET_ERROR_TYPE_ERROR)));
72 GNUNET_break (0 == 69 GNUNET_break (0 ==
73 strcmp (_("WARNING"), 70 strcmp (_("WARNING"),
74 GNUNET_error_type_to_string 71 GNUNET_error_type_to_string
75 (GNUNET_ERROR_TYPE_WARNING))); 72 (GNUNET_ERROR_TYPE_WARNING)));
76 GNUNET_break (0 == 73 GNUNET_break (0 ==
77 strcmp (_("INFO"), 74 strcmp (_("INFO"),
78 GNUNET_error_type_to_string 75 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_INFO)));
79 (GNUNET_ERROR_TYPE_INFO)));
80 GNUNET_break (0 == 76 GNUNET_break (0 ==
81 strcmp (_("DEBUG"), 77 strcmp (_("DEBUG"),
82 GNUNET_error_type_to_string 78 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG)));
83 (GNUNET_ERROR_TYPE_DEBUG)));
84 GNUNET_log_setup ("test_common_logging", "WARNING", "/dev/null"); 79 GNUNET_log_setup ("test_common_logging", "WARNING", "/dev/null");
85 logs = 0; 80 logs = 0;
86 GNUNET_logger_add (&my_log, &logs); 81 GNUNET_logger_add (&my_log, &logs);
@@ -88,17 +83,17 @@ main (int argc, char *argv[])
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Drop me...\n"); 83 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Drop me...\n");
89 GNUNET_logger_remove (&my_log, &logs); 84 GNUNET_logger_remove (&my_log, &logs);
90 if (logs != 1) 85 if (logs != 1)
91 { 86 {
92 fprintf (stdout, "Expected 1 log call, got %u\n", logs); 87 fprintf (stdout, "Expected 1 log call, got %u\n", logs);
93 failureCount++; 88 failureCount++;
94 } 89 }
95 90
96 if (failureCount != 0) 91 if (failureCount != 0)
97 { 92 {
98 fprintf (stdout, "%u TESTS FAILED!\n", failureCount); 93 fprintf (stdout, "%u TESTS FAILED!\n", failureCount);
99 return -1; 94 return -1;
100 } 95 }
101 return 0; 96 return 0;
102} /* end of main */ 97} /* end of main */
103 98
104/* end of test_common_logging.c */ 99/* 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 d87f56d42..005b5caef 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 6485e4e4a..5b89b7221 100644
--- a/src/util/test_common_logging_runtime_loglevels.c
+++ b/src/util/test_common_logging_runtime_loglevels.c
@@ -42,108 +42,110 @@ static struct GNUNET_DISK_PipeHandle *pipe_stdout;
42 42
43static GNUNET_SCHEDULER_TaskIdentifier die_task; 43static GNUNET_SCHEDULER_TaskIdentifier die_task;
44 44
45static void runone (); 45static void
46runone ();
46 47
47static void 48static void
48end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 49end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
49{ 50{
50 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase, 51 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase,
51 ok); 52 ok);
52 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 53 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
53 { 54 {
54 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 55 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
55 } 56 }
56 GNUNET_OS_process_wait (proc); 57 GNUNET_OS_process_wait (proc);
57 GNUNET_OS_process_close (proc); 58 GNUNET_OS_process_close (proc);
58 proc = NULL; 59 proc = NULL;
59 GNUNET_DISK_pipe_close (pipe_stdout); 60 GNUNET_DISK_pipe_close (pipe_stdout);
60 if (ok == 1) 61 if (ok == 1)
62 {
63 if (phase < 9)
61 { 64 {
62 if (phase < 9) 65 phase += 1;
63 { 66 runone ();
64 phase += 1;
65 runone ();
66 }
67 else
68 ok = 0;
69 } 67 }
68 else
69 ok = 0;
70 }
70 else 71 else
71 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n"); 72 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
72} 73}
73 74
74static char * 75static char *
75read_output_line (int phase_from1, int phase_to1, int phase_from2, 76read_output_line (int phase_from1, int phase_to1, int phase_from2,
76 int phase_to2, char c, char *expect_level, 77 int phase_to2, char c, char *expect_level,
77 long delay_morethan, long delay_lessthan, int phase, 78 long delay_morethan, long delay_lessthan, int phase, char *p,
78 char *p, int *len, long *delay, char level[8]) 79 int *len, long *delay, char level[8])
79{ 80{
80 char *r = p; 81 char *r = p;
81 char t[7]; 82 char t[7];
82 int i, j, stop = 0; 83 int i, j, stop = 0;
84
83 j = 0; 85 j = 0;
84 int stage = 0; 86 int stage = 0;
87
85 if (!(phase >= phase_from1 && phase <= phase_to1) && 88 if (!(phase >= phase_from1 && phase <= phase_to1) &&
86 !(phase >= phase_from2 && phase <= phase_to2)) 89 !(phase >= phase_from2 && phase <= phase_to2))
87 return p; 90 return p;
88#if 0 91#if 0
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p); 93 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p);
91#endif 94#endif
92 for (i = 0; i < *len && !stop; i++) 95 for (i = 0; i < *len && !stop; i++)
96 {
97 switch (stage)
93 { 98 {
94 switch (stage) 99 case 0: /* read first char */
95 { 100 if (r[i] != c)
96 case 0: /* read first char */ 101 {
97 if (r[i] != c) 102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c,
98 { 103 r[i]);
99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 104 GNUNET_break (0);
100 "Expected '%c', but got '%c'\n", c, r[i]); 105 return NULL;
101 GNUNET_break (0); 106 }
102 return NULL; 107 stage += 1;
103 } 108 break;
104 stage += 1; 109 case 1: /* read at most 7 char-long error level string, finished by ' ' */
105 break; 110 if (r[i] == ' ')
106 case 1: /* read at most 7 char-long error level string, finished by ' ' */ 111 {
107 if (r[i] == ' ') 112 level[j] = '\0';
108 { 113 stage += 1;
109 level[j] = '\0'; 114 j = 0;
110 stage += 1; 115 }
111 j = 0; 116 else if (i == 8)
112 } 117 {
113 else if (i == 8) 118 GNUNET_break (0);
114 { 119 ok = 2;
115 GNUNET_break (0); 120 return NULL;
116 ok = 2; 121 }
117 return NULL; 122 else
118 } 123 level[j++] = r[i];
119 else 124 break;
120 level[j++] = r[i]; 125 case 2: /* read the delay, finished by '\n' */
121 break; 126 t[j++] = r[i];
122 case 2: /* read the delay, finished by '\n' */
123 t[j++] = r[i];
124#if WINDOWS 127#if WINDOWS
125 if (r[i] == '\r' && r[i + 1] == '\n') 128 if (r[i] == '\r' && r[i + 1] == '\n')
126 { 129 {
127 i += 1; 130 i += 1;
128 t[j - 1] = '\0'; 131 t[j - 1] = '\0';
129 *delay = strtol (t, NULL, 10); 132 *delay = strtol (t, NULL, 10);
130 stop = 1; 133 stop = 1;
131 } 134 }
132#else 135#else
133 if (r[i] == '\n') 136 if (r[i] == '\n')
134 { 137 {
135 t[j - 1] = '\0'; 138 t[j - 1] = '\0';
136 *delay = strtol (t, NULL, 10); 139 *delay = strtol (t, NULL, 10);
137 stop = 1; 140 stop = 1;
138 } 141 }
139#endif 142#endif
140 break; 143 break;
141 }
142 } 144 }
143 if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 145 }
144 || *delay > 1000 146 if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 || *delay > 1000
145 || (!((*delay < delay_lessthan) || !(*delay > delay_morethan)) 147 || (!((*delay < delay_lessthan) || !(*delay > delay_morethan)) && c != '1'
146 && c != '1' && c != '2')) 148 && c != '2'))
147 return NULL; 149 return NULL;
148 *len = *len - i; 150 *len = *len - i;
149 return &r[i]; 151 return &r[i];
@@ -162,20 +164,20 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 long delays[8]; 164 long delays[8];
163 int rd; 165 int rd;
164 166
165 rd = 167 rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr,
166 GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes); 168 sizeof (buf) - bytes);
167 if (rd > 0) 169 if (rd > 0)
168 { 170 {
169 buf_ptr += rd; 171 buf_ptr += rd;
170 bytes += rd; 172 bytes += rd;
171#if VERBOSE 173#if VERBOSE
172 fprintf (stderr, "got %d bytes, reading more\n", rd); 174 fprintf (stderr, "got %d bytes, reading more\n", rd);
173#endif 175#endif
174 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 176 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
175 stdout_read_handle, &read_call, 177 stdout_read_handle, &read_call,
176 (void *) stdout_read_handle); 178 (void *) stdout_read_handle);
177 return; 179 return;
178 } 180 }
179 181
180#if VERBOSE 182#if VERBOSE
181 fprintf (stderr, "bytes is %d:%s\n", bytes, buf); 183 fprintf (stderr, "bytes is %d:%s\n", bytes, buf);
@@ -197,64 +199,65 @@ read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
197 * | 9 * * * * 199 * | 9 * * * *
198 */ 200 */
199 char *p = buf; 201 char *p = buf;
202
200 if (bytes == 20 * 16 || 203 if (bytes == 20 * 16 ||
201 !(p = 204 !(p =
202 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, 205 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
203 &delay, level)) 206 &delay, level)) ||
204 || !(p = 207 !(p =
205 read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, 208 read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, &bytes,
206 &bytes, &delays[0], level)) 209 &delays[0], level)) ||
207 || !(p = 210 !(p =
208 read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, 211 read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes,
209 &bytes, &delay, level)) 212 &delay, level)) ||
210 || !(p = 213 !(p =
211 read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p, 214 read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p,
212 &bytes, &delays[1], level)) 215 &bytes, &delays[1], level)) ||
213 || !(p = 216 !(p =
214 read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, 217 read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
215 &delay, level)) 218 &delay, level)) ||
216 || !(p = 219 !(p =
217 read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, 220 read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, &bytes,
218 &bytes, &delays[2], level)) 221 &delays[2], level)) ||
219 || !(p = 222 !(p =
220 read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, 223 read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes,
221 &bytes, &delay, level)) 224 &delay, level)) ||
222 || !(p = 225 !(p =
223 read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, 226 read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, &bytes,
224 &bytes, &delays[3], level)) 227 &delays[3], level)) ||
225 || !(p = 228 !(p =
226 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, 229 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
227 &bytes, &delay, level)) 230 &delay, level)) ||
228 || !(p = 231 !(p =
229 read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, 232 read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, &bytes,
230 &bytes, &delays[4], level)) 233 &delays[4], level)) ||
231 || !(p = 234 !(p =
232 read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, 235 read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes,
233 &bytes, &delay, level)) 236 &delay, level)) ||
234 || !(p = 237 !(p =
235 read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p, 238 read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p,
236 &bytes, &delays[5], level)) 239 &bytes, &delays[5], level)) ||
237 || !(p = 240 !(p =
238 read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, 241 read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
239 &bytes, &delay, level)) 242 &delay, level)) ||
240 || !(p = 243 !(p =
241 read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, 244 read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, &bytes,
242 &bytes, &delays[6], level)) 245 &delays[6], level)) ||
243 || !(p = 246 !(p =
244 read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, 247 read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes,
245 &bytes, &delay, level)) 248 &delay, level)) ||
246 || !(p = 249 !(p =
247 read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, 250 read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, &bytes,
248 &bytes, &delays[7], level))) 251 &delays[7], level)))
249 { 252 {
250 if (bytes == 20 * 16) 253 if (bytes == 20 * 16)
251 fprintf (stderr, "Ran out of buffer space!\n"); 254 fprintf (stderr, "Ran out of buffer space!\n");
252 GNUNET_break (0); 255 GNUNET_break (0);
253 ok = 2; 256 ok = 2;
254 GNUNET_SCHEDULER_cancel (die_task); 257 GNUNET_SCHEDULER_cancel (die_task);
255 GNUNET_SCHEDULER_add_now (&end_task, NULL); 258 GNUNET_SCHEDULER_add_now (&end_task, NULL);
256 return; 259 return;
257 } 260 }
258 261
259 GNUNET_SCHEDULER_cancel (die_task); 262 GNUNET_SCHEDULER_cancel (die_task);
260 GNUNET_SCHEDULER_add_now (&end_task, NULL); 263 GNUNET_SCHEDULER_add_now (&end_task, NULL);
@@ -268,52 +271,51 @@ runone ()
268 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); 271 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
269 272
270 if (pipe_stdout == NULL) 273 if (pipe_stdout == NULL)
271 { 274 {
272 GNUNET_break (0); 275 GNUNET_break (0);
273 ok = 2; 276 ok = 2;
274 return; 277 return;
275 } 278 }
276 279
277 putenv ("GNUNET_LOG="); 280 putenv ("GNUNET_LOG=");
278 putenv ("GNUNET_FORCE_LOG="); 281 putenv ("GNUNET_FORCE_LOG=");
279 switch (phase) 282 switch (phase)
280 { 283 {
281 case 0: 284 case 0:
282 putenv ("GNUNET_LOG=;;;;ERROR"); 285 putenv ("GNUNET_LOG=;;;;ERROR");
283 break; 286 break;
284 case 1: 287 case 1:
285 putenv ("GNUNET_LOG=;;;;WARNING"); 288 putenv ("GNUNET_LOG=;;;;WARNING");
286 break; 289 break;
287 case 2: 290 case 2:
288 putenv ("GNUNET_LOG=;;;;INFO"); 291 putenv ("GNUNET_LOG=;;;;INFO");
289 break; 292 break;
290 case 3: 293 case 3:
291 putenv ("GNUNET_LOG=;;;;DEBUG"); 294 putenv ("GNUNET_LOG=;;;;DEBUG");
292 break; 295 break;
293 case 4: 296 case 4:
294 putenv ("GNUNET_FORCE_LOG=;;;;ERROR"); 297 putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
295 break; 298 break;
296 case 5: 299 case 5:
297 putenv ("GNUNET_FORCE_LOG=;;;;WARNING"); 300 putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
298 break; 301 break;
299 case 6: 302 case 6:
300 putenv ("GNUNET_FORCE_LOG=;;;;INFO"); 303 putenv ("GNUNET_FORCE_LOG=;;;;INFO");
301 break; 304 break;
302 case 7: 305 case 7:
303 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG"); 306 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
304 break; 307 break;
305 case 8: 308 case 8:
306 putenv ("GNUNET_LOG=blah;;;;ERROR"); 309 putenv ("GNUNET_LOG=blah;;;;ERROR");
307 break; 310 break;
308 case 9: 311 case 9:
309 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR"); 312 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
310 break; 313 break;
311 } 314 }
312 315
313 proc = 316 proc =
314 GNUNET_OS_start_process (NULL, pipe_stdout, 317 GNUNET_OS_start_process (NULL, pipe_stdout, "./test_common_logging_dummy",
315 "./test_common_logging_dummy", 318 "test_common_logging_dummy", NULL);
316 "test_common_logging_dummy", NULL);
317 putenv ("GNUNET_FORCE_LOG="); 319 putenv ("GNUNET_FORCE_LOG=");
318 putenv ("GNUNET_LOG="); 320 putenv ("GNUNET_LOG=");
319 321
@@ -321,20 +323,20 @@ runone ()
321 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); 323 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
322 324
323 stdout_read_handle = 325 stdout_read_handle =
324 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ); 326 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
325 327
326 die_task = 328 die_task =
327 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 329 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
328 (GNUNET_TIME_UNIT_SECONDS, 10), &end_task, 330 (GNUNET_TIME_UNIT_SECONDS, 10), &end_task,
329 NULL); 331 NULL);
330 332
331 bytes = 0; 333 bytes = 0;
332 buf_ptr = buf; 334 buf_ptr = buf;
333 memset (&buf, 0, sizeof (buf)); 335 memset (&buf, 0, sizeof (buf));
334 336
335 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 337 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
336 stdout_read_handle, &read_call, 338 stdout_read_handle, &read_call,
337 (void *) stdout_read_handle); 339 (void *) stdout_read_handle);
338} 340}
339 341
340static void 342static void
@@ -364,11 +366,11 @@ main (int argc, char *argv[])
364 366
365 GNUNET_log_setup ("test-common-logging-runtime-loglevels", 367 GNUNET_log_setup ("test-common-logging-runtime-loglevels",
366#if VERBOSE 368#if VERBOSE
367 "DEBUG", 369 "DEBUG",
368#else 370#else
369 "WARNING", 371 "WARNING",
370#endif 372#endif
371 NULL); 373 NULL);
372 ret = check (); 374 ret = check ();
373 375
374 return ret; 376 return ret;
diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c
index 9deb4fc35..3780b3649 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)
85 { 90 {
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:
98 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option, 91 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
99 "new-value"); 92 "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");
100 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, 112 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
101 option, "new-value"); 113 "new-key", "new-value");
102 break; 114 }
103 case ADD_NEW_ENTRY: 115 break;
104 { 116 }
105 static int hit = 0; 117 case COMPARE:
106 118 {
107 if (hit == 0) 119 int ret;
108 { 120 char *diffValue;
109 hit = 1; 121
110 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, 122 diffValue = NULL;
111 "new-key", "new-value"); 123 ret =
112 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, 124 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
113 "new-key", "new-value"); 125 option, &diffValue);
114 } 126 if (NULL != diffValue)
115 break; 127 {
116 } 128 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
117 case COMPARE: 129 cbData->status = 1;
118 { 130 }
119 int ret; 131 else
120 char *diffValue; 132 cbData->status = 1;
121 133 GNUNET_free_non_null (diffValue);
122 diffValue = NULL; 134 break;
123 ret = 135 }
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 0 136#if 0
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 }
143 else if (strcmp (cbData->section, section) != 0)
144 {
145 cbData->section = section;
146 printf ("\nSection: %s\n", section);
147 }
148 printf ("%s = %s\n", option, value);
149#endif
150 default:
151 break;
152 } 142 }
143 else if (strcmp (cbData->section, section) != 0)
144 {
145 cbData->section = section;
146 printf ("\nSection: %s\n", section);
147 }
148 printf ("%s = %s\n", option, value);
149#endif
150 default:
151 break;
152 }
153} 153}
154 154
155 155
@@ -162,41 +162,40 @@ 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++)
165 { 182 {
166 case EDIT_SECTION: 183 GNUNET_asprintf (&key, "key%d", i);
167 case EDIT_ALL: 184 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
168 case ADD_NEW_ENTRY: 185 "new-value");
169 diffsCB.callBackOption = option; 186 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
170 diffsCB.cfg = cfg; 187 key, "new-value");
171 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB); 188 GNUNET_free (key);
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;
199 } 189 }
190 break;
191 }
192 case REMOVE_SECTION:
193 break;
194 case REMOVE_ENTRY:
195 break;
196 default:
197 break;
198 }
200 199
201 return diffsCB.cfgDiffs; 200 return diffsCB.cfgDiffs;
202} 201}
@@ -221,37 +220,35 @@ checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
221 220
222 /* Modify configuration and save it */ 221 /* Modify configuration and save it */
223 cfgDiffs = editConfiguration (cfg, option); 222 cfgDiffs = editConfiguration (cfg, option);
224 diffsFileName = 223 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
225 GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
226 if (diffsFileName == NULL) 224 if (diffsFileName == NULL)
227 { 225 {
228 GNUNET_break (0); 226 GNUNET_break (0);
229 GNUNET_CONFIGURATION_destroy (cfg); 227 GNUNET_CONFIGURATION_destroy (cfg);
230 GNUNET_CONFIGURATION_destroy (cfgDiffs); 228 GNUNET_CONFIGURATION_destroy (cfgDiffs);
231 return 1; 229 return 1;
232 } 230 }
233 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName); 231 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
234 GNUNET_CONFIGURATION_destroy (cfg); 232 GNUNET_CONFIGURATION_destroy (cfg);
235 233
236 /* Compare the dumped configuration with modifications done */ 234 /* Compare the dumped configuration with modifications done */
237 cfg = GNUNET_CONFIGURATION_create (); 235 cfg = GNUNET_CONFIGURATION_create ();
238 GNUNET_assert (GNUNET_OK == 236 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
239 GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
240 remove (diffsFileName); 237 remove (diffsFileName);
241 cbData.callBackOption = COMPARE; 238 cbData.callBackOption = COMPARE;
242 cbData.cfgDiffs = cfgDiffs; 239 cbData.cfgDiffs = cfgDiffs;
243 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData); 240 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
244 if (1 == (ret = cbData.status)) 241 if (1 == (ret = cbData.status))
245 { 242 {
246 fprintf (stderr, 243 fprintf (stderr,
247 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n"); 244 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
248 goto housekeeping; 245 goto housekeeping;
249 } 246 }
250 cbData.cfgDiffs = cfg; 247 cbData.cfgDiffs = cfg;
251 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData); 248 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
252 if ((ret = cbData.status) == 1) 249 if ((ret = cbData.status) == 1)
253 fprintf (stderr, 250 fprintf (stderr,
254 "Incorrect Configuration Diffs: Data may be missing in diffs\n"); 251 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
255 252
256housekeeping: 253housekeeping:
257#if 0 254#if 0
@@ -276,65 +273,64 @@ testConfig ()
276 char *c; 273 char *c;
277 unsigned long long l; 274 unsigned long long l;
278 275
279 if (GNUNET_OK != 276 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
280 GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
281 return 1; 277 return 1;
282 if (0 != strcmp ("b", c)) 278 if (0 != strcmp ("b", c))
283 { 279 {
284 fprintf (stderr, "Got `%s'\n", c); 280 fprintf (stderr, "Got `%s'\n", c);
285 GNUNET_free (c); 281 GNUNET_free (c);
286 return 2; 282 return 2;
287 } 283 }
288 GNUNET_free (c); 284 GNUNET_free (c);
289 if (GNUNET_OK != 285 if (GNUNET_OK !=
290 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l)) 286 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
291 { 287 {
292 GNUNET_break (0); 288 GNUNET_break (0);
293 return 3; 289 return 3;
294 } 290 }
295 if (5 != l) 291 if (5 != l)
296 { 292 {
297 GNUNET_break (0); 293 GNUNET_break (0);
298 return 4; 294 return 4;
299 } 295 }
300 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES"); 296 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
301 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c")) 297 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
302 { 298 {
303 GNUNET_break (0); 299 GNUNET_break (0);
304 return 5; 300 return 5;
305 } 301 }
306 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10); 302 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
307 if (GNUNET_OK != 303 if (GNUNET_OK !=
308 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c)) 304 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
309 { 305 {
310 GNUNET_break (0); 306 GNUNET_break (0);
311 return 6; 307 return 6;
312 } 308 }
313 if (0 != strcmp (c, "10")) 309 if (0 != strcmp (c, "10"))
314 { 310 {
315 GNUNET_free (c); 311 GNUNET_free (c);
316 GNUNET_break (0); 312 GNUNET_break (0);
317 return 7; 313 return 7;
318 } 314 }
319 GNUNET_free (c); 315 GNUNET_free (c);
320 316
321 if (GNUNET_OK != 317 if (GNUNET_OK !=
322 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c)) 318 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
323 { 319 {
324 GNUNET_break (0); 320 GNUNET_break (0);
325 return 8; 321 return 8;
326 } 322 }
327#ifndef MINGW 323#ifndef MINGW
328 if (0 != strcmp (c, "/hello/world")) 324 if (0 != strcmp (c, "/hello/world"))
329#else 325#else
330#define HI "\\hello\\world" 326#define HI "\\hello\\world"
331 if (strstr (c, HI) != c + strlen (c) - strlen (HI)) 327 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
332#endif 328#endif
333 { 329 {
334 GNUNET_break (0); 330 GNUNET_break (0);
335 GNUNET_free (c); 331 GNUNET_free (c);
336 return 9; 332 return 9;
337 } 333 }
338 GNUNET_free (c); 334 GNUNET_free (c);
339 335
340 if (GNUNET_OK != 336 if (GNUNET_OK !=
@@ -365,10 +361,10 @@ check (void *data, const char *fn)
365 int *idx = data; 361 int *idx = data;
366 362
367 if (0 == strcmp (want[*idx], fn)) 363 if (0 == strcmp (want[*idx], fn))
368 { 364 {
369 (*idx)++; 365 (*idx)++;
370 return GNUNET_OK; 366 return GNUNET_OK;
371 } 367 }
372 GNUNET_break (0); 368 GNUNET_break (0);
373 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
374} 370}
@@ -381,66 +377,66 @@ testConfigFilenames ()
381 idx = 0; 377 idx = 0;
382 if (3 != 378 if (3 !=
383 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test", 379 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
384 &check, &idx)) 380 &check, &idx))
385 { 381 {
386 GNUNET_break (0); 382 GNUNET_break (0);
387 return 8; 383 return 8;
388 } 384 }
389 if (idx != 3) 385 if (idx != 3)
390 return 16; 386 return 16;
391 if (GNUNET_OK != 387 if (GNUNET_OK !=
392 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", 388 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
393 "/File Name")) 389 "/File Name"))
394 { 390 {
395 GNUNET_break (0); 391 GNUNET_break (0);
396 return 24; 392 return 24;
397 } 393 }
398 394
399 if (GNUNET_NO != 395 if (GNUNET_NO !=
400 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", 396 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
401 "/File Name")) 397 "/File Name"))
402 { 398 {
403 GNUNET_break (0); 399 GNUNET_break (0);
404 return 32; 400 return 32;
405 } 401 }
406 if (GNUNET_NO != 402 if (GNUNET_NO !=
407 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test", 403 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
408 "Stuff")) 404 "Stuff"))
409 { 405 {
410 GNUNET_break (0); 406 GNUNET_break (0);
411 return 40; 407 return 40;
412 } 408 }
413 409
414 if (GNUNET_NO != 410 if (GNUNET_NO !=
415 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 411 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
416 "/Hello")) 412 "/Hello"))
417 { 413 {
418 GNUNET_break (0); 414 GNUNET_break (0);
419 return 48; 415 return 48;
420 } 416 }
421 if (GNUNET_NO != 417 if (GNUNET_NO !=
422 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 418 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
423 "/World")) 419 "/World"))
424 { 420 {
425 GNUNET_break (0); 421 GNUNET_break (0);
426 return 56; 422 return 56;
427 } 423 }
428 424
429 if (GNUNET_YES != 425 if (GNUNET_YES !=
430 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 426 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
431 "/File 1")) 427 "/File 1"))
432 { 428 {
433 GNUNET_break (0); 429 GNUNET_break (0);
434 return 64; 430 return 64;
435 } 431 }
436 432
437 if (GNUNET_YES != 433 if (GNUNET_YES !=
438 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test", 434 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
439 "/File 2")) 435 "/File 2"))
440 { 436 {
441 GNUNET_break (0); 437 GNUNET_break (0);
442 return 72; 438 return 72;
443 } 439 }
444 440
445 idx = 0; 441 idx = 0;
446 want[1] = "/World"; 442 want[1] = "/World";
@@ -448,16 +444,16 @@ testConfigFilenames ()
448 want[3] = "/File 2"; 444 want[3] = "/File 2";
449 if (4 != 445 if (4 !=
450 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test", 446 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
451 &check, &idx)) 447 &check, &idx))
452 { 448 {
453 GNUNET_break (0); 449 GNUNET_break (0);
454 return 80; 450 return 80;
455 } 451 }
456 if (idx != 4) 452 if (idx != 4)
457 { 453 {
458 GNUNET_break (0); 454 GNUNET_break (0);
459 return 88; 455 return 88;
460 } 456 }
461 return 0; 457 return 0;
462} 458}
463 459
@@ -473,11 +469,11 @@ main (int argc, char *argv[])
473 GNUNET_assert (cfg != NULL); 469 GNUNET_assert (cfg != NULL);
474 if (GNUNET_OK != 470 if (GNUNET_OK !=
475 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf")) 471 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
476 { 472 {
477 fprintf (stderr, "Failed to parse configuration file\n"); 473 fprintf (stderr, "Failed to parse configuration file\n");
478 GNUNET_CONFIGURATION_destroy (cfg); 474 GNUNET_CONFIGURATION_destroy (cfg);
479 return 1; 475 return 1;
480 } 476 }
481 failureCount += testConfig (); 477 failureCount += testConfig ();
482 if (failureCount > 0) 478 if (failureCount > 0)
483 goto error; 479 goto error;
@@ -487,37 +483,36 @@ main (int argc, char *argv[])
487 goto error; 483 goto error;
488 484
489 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf")) 485 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
490 { 486 {
491 fprintf (stderr, "Failed to write configuration file\n"); 487 fprintf (stderr, "Failed to write configuration file\n");
492 GNUNET_CONFIGURATION_destroy (cfg); 488 GNUNET_CONFIGURATION_destroy (cfg);
493 return 1; 489 return 1;
494 } 490 }
495 GNUNET_CONFIGURATION_destroy (cfg); 491 GNUNET_CONFIGURATION_destroy (cfg);
496 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf")); 492 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
497 493
498 cfg = GNUNET_CONFIGURATION_create (); 494 cfg = GNUNET_CONFIGURATION_create ();
499 if (GNUNET_OK != 495 if (GNUNET_OK !=
500 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf")) 496 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
501 { 497 {
502 GNUNET_break (0); 498 GNUNET_break (0);
503 GNUNET_CONFIGURATION_destroy (cfg); 499 GNUNET_CONFIGURATION_destroy (cfg);
504 return 1; 500 return 1;
505 } 501 }
506 if (GNUNET_OK != 502 if (GNUNET_OK !=
507 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", 503 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
508 &c)) 504 {
509 { 505 GNUNET_break (0);
510 GNUNET_break (0); 506 GNUNET_CONFIGURATION_destroy (cfg);
511 GNUNET_CONFIGURATION_destroy (cfg); 507 return 1;
512 return 1; 508 }
513 }
514 if (0 != strcmp (c, "YES")) 509 if (0 != strcmp (c, "YES"))
515 { 510 {
516 GNUNET_break (0); 511 GNUNET_break (0);
517 GNUNET_free (c); 512 GNUNET_free (c);
518 GNUNET_CONFIGURATION_destroy (cfg); 513 GNUNET_CONFIGURATION_destroy (cfg);
519 return 1; 514 return 1;
520 } 515 }
521 516
522 GNUNET_free (c); 517 GNUNET_free (c);
523 GNUNET_CONFIGURATION_destroy (cfg); 518 GNUNET_CONFIGURATION_destroy (cfg);
@@ -525,11 +520,11 @@ main (int argc, char *argv[])
525 /* Testing configuration diffs */ 520 /* Testing configuration diffs */
526 cfgDefault = GNUNET_CONFIGURATION_create (); 521 cfgDefault = GNUNET_CONFIGURATION_create ();
527 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL)) 522 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
528 { 523 {
529 GNUNET_break (0); 524 GNUNET_break (0);
530 GNUNET_CONFIGURATION_destroy (cfgDefault); 525 GNUNET_CONFIGURATION_destroy (cfgDefault);
531 return 1; 526 return 1;
532 } 527 }
533 528
534 /* Nothing changed in the new configuration */ 529 /* Nothing changed in the new configuration */
535 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING); 530 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
@@ -550,9 +545,9 @@ main (int argc, char *argv[])
550 545
551error: 546error:
552 if (failureCount != 0) 547 if (failureCount != 0)
553 { 548 {
554 fprintf (stderr, "Test failed: %u\n", failureCount); 549 fprintf (stderr, "Test failed: %u\n", failureCount);
555 return 1; 550 return 1;
556 } 551 }
557 return 0; 552 return 0;
558} 553}
diff --git a/src/util/test_connection.c b/src/util/test_connection.c
index 1ac5b99cd..92c0cea2e 100644
--- a/src/util/test_connection.c
+++ b/src/util/test_connection.c
@@ -66,46 +66,44 @@ 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, 69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt");
70 "setsockopt");
71 GNUNET_assert (GNUNET_NETWORK_socket_bind 70 GNUNET_assert (GNUNET_NETWORK_socket_bind
72 (desc, (const struct sockaddr *) &sa, 71 (desc, (const struct sockaddr *) &sa,
73 sizeof (sa)) == GNUNET_OK); 72 sizeof (sa)) == GNUNET_OK);
74 GNUNET_NETWORK_socket_listen (desc, 5); 73 GNUNET_NETWORK_socket_listen (desc, 5);
75 return desc; 74 return desc;
76} 75}
77 76
78static void 77static void
79receive_check (void *cls, const void *buf, size_t available, 78receive_check (void *cls, const void *buf, size_t available,
80 const struct sockaddr *addr, socklen_t addrlen, int errCode) 79 const struct sockaddr *addr, socklen_t addrlen, int errCode)
81{ 80{
82 int *ok = cls; 81 int *ok = cls;
83 82
84#if VERBOSE 83#if VERBOSE
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n"); 84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n");
86#endif 85#endif
87 GNUNET_assert (buf != NULL); /* no timeout */ 86 GNUNET_assert (buf != NULL); /* no timeout */
88 if (0 == memcmp (&"Hello World"[sofar], buf, available)) 87 if (0 == memcmp (&"Hello World"[sofar], buf, available))
89 sofar += available; 88 sofar += available;
90 if (sofar < 12) 89 if (sofar < 12)
91 { 90 {
92#if VERBOSE 91#if VERBOSE
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n"); 92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n");
94#endif 93#endif
95 GNUNET_CONNECTION_receive (asock, 1024, 94 GNUNET_CONNECTION_receive (asock, 1024,
96 GNUNET_TIME_relative_multiply 95 GNUNET_TIME_relative_multiply
97 (GNUNET_TIME_UNIT_SECONDS, 5), 96 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
98 &receive_check, cls); 97 cls);
99 } 98 }
100 else 99 else
101 { 100 {
102#if VERBOSE 101#if VERBOSE
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive closes accepted socket\n");
104 "Receive closes accepted socket\n");
105#endif 103#endif
106 *ok = 0; 104 *ok = 0;
107 GNUNET_CONNECTION_destroy (asock, GNUNET_YES); 105 GNUNET_CONNECTION_destroy (asock, GNUNET_YES);
108 } 106 }
109} 107}
110 108
111 109
@@ -124,12 +122,12 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 122 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
125#if VERBOSE 123#if VERBOSE
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Test asks to receive on accepted socket\n"); 125 "Test asks to receive on accepted socket\n");
128#endif 126#endif
129 GNUNET_CONNECTION_receive (asock, 1024, 127 GNUNET_CONNECTION_receive (asock, 1024,
130 GNUNET_TIME_relative_multiply 128 GNUNET_TIME_relative_multiply
131 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, 129 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
132 cls); 130 cls);
133} 131}
134 132
135static size_t 133static size_t
@@ -137,7 +135,7 @@ make_hello (void *cls, size_t size, void *buf)
137{ 135{
138#if VERBOSE 136#if VERBOSE
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "Test prepares to transmit on connect socket\n"); 138 "Test prepares to transmit on connect socket\n");
141#endif 139#endif
142 GNUNET_assert (size >= 12); 140 GNUNET_assert (size >= 12);
143 strcpy ((char *) buf, "Hello World"); 141 strcpy ((char *) buf, "Hello World");
@@ -160,14 +158,14 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n"); 158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n");
161#endif 159#endif
162 GNUNET_assert (NULL != 160 GNUNET_assert (NULL !=
163 GNUNET_CONNECTION_notify_transmit_ready (csock, 12, 161 GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
164 GNUNET_TIME_UNIT_SECONDS, 162 GNUNET_TIME_UNIT_SECONDS,
165 &make_hello, NULL)); 163 &make_hello, NULL));
166#if VERBOSE 164#if VERBOSE
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n"); 165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n");
168#endif 166#endif
169 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, 167 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept,
170 &run_accept, cls); 168 cls);
171} 169}
172 170
173 171
@@ -183,7 +181,7 @@ check ()
183 ok = 1; 181 ok = 1;
184 cfg = GNUNET_CONFIGURATION_create (); 182 cfg = GNUNET_CONFIGURATION_create ();
185 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 183 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
186 "localhost"); 184 "localhost");
187 GNUNET_SCHEDULER_run (&task, &ok); 185 GNUNET_SCHEDULER_run (&task, &ok);
188 GNUNET_CONFIGURATION_destroy (cfg); 186 GNUNET_CONFIGURATION_destroy (cfg);
189 return ok; 187 return ok;
@@ -198,11 +196,11 @@ main (int argc, char *argv[])
198 196
199 GNUNET_log_setup ("test_connection", 197 GNUNET_log_setup ("test_connection",
200#if VERBOSE 198#if VERBOSE
201 "DEBUG", 199 "DEBUG",
202#else 200#else
203 "WARNING", 201 "WARNING",
204#endif 202#endif
205 NULL); 203 NULL);
206 ret += check (); 204 ret += check ();
207 return ret; 205 return ret;
208} 206}
diff --git a/src/util/test_connection_addressing.c b/src/util/test_connection_addressing.c
index c413c18c8..018cf61fb 100644
--- a/src/util/test_connection_addressing.c
+++ b/src/util/test_connection_addressing.c
@@ -66,16 +66,15 @@ 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, 69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt");
70 "setsockopt");
71 if (GNUNET_OK != 70 if (GNUNET_OK !=
72 GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, 71 GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa,
73 sizeof (sa))) 72 sizeof (sa)))
74 { 73 {
75 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 74 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
76 "bind"); 75 "bind");
77 GNUNET_assert (0); 76 GNUNET_assert (0);
78 } 77 }
79 GNUNET_NETWORK_socket_listen (desc, 5); 78 GNUNET_NETWORK_socket_listen (desc, 5);
80 return desc; 79 return desc;
81} 80}
@@ -83,25 +82,25 @@ open_listen_socket ()
83 82
84static void 83static void
85receive_check (void *cls, const void *buf, size_t available, 84receive_check (void *cls, const void *buf, size_t available,
86 const struct sockaddr *addr, socklen_t addrlen, int errCode) 85 const struct sockaddr *addr, socklen_t addrlen, int errCode)
87{ 86{
88 int *ok = cls; 87 int *ok = cls;
89 88
90 GNUNET_assert (buf != NULL); /* no timeout */ 89 GNUNET_assert (buf != NULL); /* no timeout */
91 if (0 == memcmp (&"Hello World"[sofar], buf, available)) 90 if (0 == memcmp (&"Hello World"[sofar], buf, available))
92 sofar += available; 91 sofar += available;
93 if (sofar < 12) 92 if (sofar < 12)
94 { 93 {
95 GNUNET_CONNECTION_receive (asock, 1024, 94 GNUNET_CONNECTION_receive (asock, 1024,
96 GNUNET_TIME_relative_multiply 95 GNUNET_TIME_relative_multiply
97 (GNUNET_TIME_UNIT_SECONDS, 5), 96 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
98 &receive_check, cls); 97 cls);
99 } 98 }
100 else 99 else
101 { 100 {
102 *ok = 0; 101 *ok = 0;
103 GNUNET_CONNECTION_destroy (asock, GNUNET_YES); 102 GNUNET_CONNECTION_destroy (asock, GNUNET_YES);
104 } 103 }
105} 104}
106 105
107 106
@@ -117,7 +116,7 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 GNUNET_assert (asock != NULL); 116 GNUNET_assert (asock != NULL);
118 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); 117 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock));
119 GNUNET_assert (GNUNET_OK == 118 GNUNET_assert (GNUNET_OK ==
120 GNUNET_CONNECTION_get_address (asock, &addr, &alen)); 119 GNUNET_CONNECTION_get_address (asock, &addr, &alen));
121 GNUNET_assert (alen == sizeof (struct sockaddr_in)); 120 GNUNET_assert (alen == sizeof (struct sockaddr_in));
122 v4 = addr; 121 v4 = addr;
123 memset (&expect, 0, sizeof (expect)); 122 memset (&expect, 0, sizeof (expect));
@@ -131,9 +130,9 @@ run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 GNUNET_free (addr); 130 GNUNET_free (addr);
132 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 131 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
133 GNUNET_CONNECTION_receive (asock, 1024, 132 GNUNET_CONNECTION_receive (asock, 1024,
134 GNUNET_TIME_relative_multiply 133 GNUNET_TIME_relative_multiply
135 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, 134 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
136 cls); 135 cls);
137} 136}
138 137
139static size_t 138static size_t
@@ -160,17 +159,17 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 v4.sin_port = htons (PORT); 159 v4.sin_port = htons (PORT);
161 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 160 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
162 csock = 161 csock =
163 GNUNET_CONNECTION_create_from_sockaddr (AF_INET, 162 GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
164 (const struct sockaddr *) &v4, 163 (const struct sockaddr *) &v4,
165 sizeof (v4)); 164 sizeof (v4));
166 GNUNET_assert (csock != NULL); 165 GNUNET_assert (csock != NULL);
167 GNUNET_assert (NULL != 166 GNUNET_assert (NULL !=
168 GNUNET_CONNECTION_notify_transmit_ready (csock, 12, 167 GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
169 GNUNET_TIME_UNIT_SECONDS, 168 GNUNET_TIME_UNIT_SECONDS,
170 &make_hello, NULL)); 169 &make_hello, NULL));
171 GNUNET_CONNECTION_destroy (csock, GNUNET_YES); 170 GNUNET_CONNECTION_destroy (csock, GNUNET_YES);
172 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, 171 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept,
173 &run_accept, cls); 172 cls);
174} 173}
175 174
176 175
@@ -197,11 +196,11 @@ main (int argc, char *argv[])
197 196
198 GNUNET_log_setup ("test_connection_addressing", 197 GNUNET_log_setup ("test_connection_addressing",
199#if VERBOSE 198#if VERBOSE
200 "DEBUG", 199 "DEBUG",
201#else 200#else
202 "WARNING", 201 "WARNING",
203#endif 202#endif
204 NULL); 203 NULL);
205 ret += check (); 204 ret += check ();
206 return ret; 205 return ret;
207} 206}
diff --git a/src/util/test_connection_receive_cancel.c b/src/util/test_connection_receive_cancel.c
index bd095fdda..63829344d 100644
--- a/src/util/test_connection_receive_cancel.c
+++ b/src/util/test_connection_receive_cancel.c
@@ -65,11 +65,10 @@ 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, 68 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt");
69 "setsockopt");
70 GNUNET_assert (GNUNET_NETWORK_socket_bind 69 GNUNET_assert (GNUNET_NETWORK_socket_bind
71 (desc, (const struct sockaddr *) &sa, 70 (desc, (const struct sockaddr *) &sa,
72 sizeof (sa)) == GNUNET_OK); 71 sizeof (sa)) == GNUNET_OK);
73 GNUNET_NETWORK_socket_listen (desc, 5); 72 GNUNET_NETWORK_socket_listen (desc, 5);
74 return desc; 73 return desc;
75} 74}
@@ -78,7 +77,7 @@ open_listen_socket ()
78 77
79static void 78static void
80dead_receive (void *cls, const void *buf, size_t available, 79dead_receive (void *cls, const void *buf, size_t available,
81 const struct sockaddr *addr, socklen_t addrlen, int errCode) 80 const struct sockaddr *addr, socklen_t addrlen, int errCode)
82{ 81{
83 GNUNET_assert (0); 82 GNUNET_assert (0);
84} 83}
@@ -93,9 +92,8 @@ run_accept_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); 92 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock));
94 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 93 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
95 GNUNET_CONNECTION_receive (asock, 1024, 94 GNUNET_CONNECTION_receive (asock, 1024,
96 GNUNET_TIME_relative_multiply 95 GNUNET_TIME_relative_multiply
97 (GNUNET_TIME_UNIT_SECONDS, 5), &dead_receive, 96 (GNUNET_TIME_UNIT_SECONDS, 5), &dead_receive, cls);
98 cls);
99} 97}
100 98
101 99
@@ -121,9 +119,9 @@ task_receive_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); 119 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
122 GNUNET_assert (csock != NULL); 120 GNUNET_assert (csock != NULL);
123 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, 121 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls,
124 &run_accept_cancel, cls); 122 &run_accept_cancel, cls);
125 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 123 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &receive_cancel_task,
126 &receive_cancel_task, cls); 124 cls);
127} 125}
128 126
129 127
@@ -139,7 +137,7 @@ check_receive_cancel ()
139 ok = 1; 137 ok = 1;
140 cfg = GNUNET_CONFIGURATION_create (); 138 cfg = GNUNET_CONFIGURATION_create ();
141 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 139 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
142 "localhost"); 140 "localhost");
143 GNUNET_SCHEDULER_run (&task_receive_cancel, &ok); 141 GNUNET_SCHEDULER_run (&task_receive_cancel, &ok);
144 GNUNET_CONFIGURATION_destroy (cfg); 142 GNUNET_CONFIGURATION_destroy (cfg);
145 return ok; 143 return ok;
diff --git a/src/util/test_connection_timeout.c b/src/util/test_connection_timeout.c
index 5a0bb7c3a..1700718b9 100644
--- a/src/util/test_connection_timeout.c
+++ b/src/util/test_connection_timeout.c
@@ -62,11 +62,10 @@ 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, 65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt");
66 "setsockopt");
67 GNUNET_assert (GNUNET_NETWORK_socket_bind 66 GNUNET_assert (GNUNET_NETWORK_socket_bind
68 (desc, (const struct sockaddr *) &sa, 67 (desc, (const struct sockaddr *) &sa,
69 sizeof (sa)) == GNUNET_OK); 68 sizeof (sa)) == GNUNET_OK);
70 GNUNET_NETWORK_socket_listen (desc, 5); 69 GNUNET_NETWORK_socket_listen (desc, 5);
71 return desc; 70 return desc;
72} 71}
@@ -78,16 +77,16 @@ send_kilo (void *cls, size_t size, void *buf)
78 int *ok = cls; 77 int *ok = cls;
79 78
80 if (size == 0) 79 if (size == 0)
81 { 80 {
82#if VERBOSE 81#if VERBOSE
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got the desired timeout!\n"); 82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got the desired timeout!\n");
84#endif 83#endif
85 GNUNET_assert (buf == NULL); 84 GNUNET_assert (buf == NULL);
86 *ok = 0; 85 *ok = 0;
87 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES); 86 GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
88 GNUNET_CONNECTION_destroy (csock, GNUNET_YES); 87 GNUNET_CONNECTION_destroy (csock, GNUNET_YES);
89 return 0; 88 return 0;
90 } 89 }
91#if VERBOSE 90#if VERBOSE
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending kilo to fill buffer.\n"); 91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending kilo to fill buffer.\n");
93#endif 92#endif
@@ -95,9 +94,9 @@ send_kilo (void *cls, size_t size, void *buf)
95 memset (buf, 42, 1024); 94 memset (buf, 42, 1024);
96 95
97 GNUNET_assert (NULL != 96 GNUNET_assert (NULL !=
98 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, 97 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024,
99 GNUNET_TIME_UNIT_SECONDS, 98 GNUNET_TIME_UNIT_SECONDS,
100 &send_kilo, cls)); 99 &send_kilo, cls));
101 return 1024; 100 return 1024;
102} 101}
103 102
@@ -112,9 +111,9 @@ task_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); 111 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
113 GNUNET_assert (csock != NULL); 112 GNUNET_assert (csock != NULL);
114 GNUNET_assert (NULL != 113 GNUNET_assert (NULL !=
115 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024, 114 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024,
116 GNUNET_TIME_UNIT_SECONDS, 115 GNUNET_TIME_UNIT_SECONDS,
117 &send_kilo, cls)); 116 &send_kilo, cls));
118} 117}
119 118
120 119
@@ -130,7 +129,7 @@ check_timeout ()
130 ok = 1; 129 ok = 1;
131 cfg = GNUNET_CONFIGURATION_create (); 130 cfg = GNUNET_CONFIGURATION_create ();
132 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 131 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
133 "localhost"); 132 "localhost");
134 GNUNET_SCHEDULER_run (&task_timeout, &ok); 133 GNUNET_SCHEDULER_run (&task_timeout, &ok);
135 GNUNET_CONFIGURATION_destroy (cfg); 134 GNUNET_CONFIGURATION_destroy (cfg);
136 return ok; 135 return ok;
@@ -143,11 +142,11 @@ main (int argc, char *argv[])
143 142
144 GNUNET_log_setup ("test_connection_timeout", 143 GNUNET_log_setup ("test_connection_timeout",
145#if VERBOSE 144#if VERBOSE
146 "DEBUG", 145 "DEBUG",
147#else 146#else
148 "WARNING", 147 "WARNING",
149#endif 148#endif
150 NULL); 149 NULL);
151 ret += check_timeout (); 150 ret += check_timeout ();
152 return ret; 151 return ret;
153} 152}
diff --git a/src/util/test_connection_timeout_no_connect.c b/src/util/test_connection_timeout_no_connect.c
index 00b247c52..b598907ec 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 540d8c51d..50308a2d0 100644
--- a/src/util/test_connection_transmit_cancel.c
+++ b/src/util/test_connection_transmit_cancel.c
@@ -43,8 +43,7 @@ not_run (void *cls, size_t size, void *buf)
43 43
44 44
45static void 45static void
46task_transmit_cancel (void *cls, 46task_transmit_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47 const struct GNUNET_SCHEDULER_TaskContext *tc)
48{ 47{
49 int *ok = cls; 48 int *ok = cls;
50 struct GNUNET_CONNECTION_TransmitHandle *th; 49 struct GNUNET_CONNECTION_TransmitHandle *th;
@@ -53,8 +52,8 @@ task_transmit_cancel (void *cls,
53 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT); 52 csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
54 GNUNET_assert (csock != NULL); 53 GNUNET_assert (csock != NULL);
55 th = GNUNET_CONNECTION_notify_transmit_ready (csock, 12, 54 th = GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
56 GNUNET_TIME_UNIT_MINUTES, 55 GNUNET_TIME_UNIT_MINUTES,
57 &not_run, cls); 56 &not_run, cls);
58 GNUNET_assert (NULL != th); 57 GNUNET_assert (NULL != th);
59 GNUNET_CONNECTION_notify_transmit_ready_cancel (th); 58 GNUNET_CONNECTION_notify_transmit_ready_cancel (th);
60 GNUNET_CONNECTION_destroy (csock, GNUNET_YES); 59 GNUNET_CONNECTION_destroy (csock, GNUNET_YES);
@@ -75,7 +74,7 @@ check_transmit_cancel ()
75 ok = 1; 74 ok = 1;
76 cfg = GNUNET_CONFIGURATION_create (); 75 cfg = GNUNET_CONFIGURATION_create ();
77 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 76 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
78 "localhost"); 77 "localhost");
79 GNUNET_SCHEDULER_run (&task_transmit_cancel, &ok); 78 GNUNET_SCHEDULER_run (&task_transmit_cancel, &ok);
80 GNUNET_CONFIGURATION_destroy (cfg); 79 GNUNET_CONFIGURATION_destroy (cfg);
81 return ok; 80 return ok;
@@ -89,11 +88,11 @@ main (int argc, char *argv[])
89 88
90 GNUNET_log_setup ("test_connection_transmit_cancel", 89 GNUNET_log_setup ("test_connection_transmit_cancel",
91#if VERBOSE 90#if VERBOSE
92 "DEBUG", 91 "DEBUG",
93#else 92#else
94 "WARNING", 93 "WARNING",
95#endif 94#endif
96 NULL); 95 NULL);
97 ret += check_transmit_cancel (); 96 ret += check_transmit_cancel ();
98 97
99 return ret; 98 return ret;
diff --git a/src/util/test_container_bloomfilter.c b/src/util/test_container_bloomfilter.c
index 04bb34889..c14adb075 100644
--- a/src/util/test_container_bloomfilter.c
+++ b/src/util/test_container_bloomfilter.c
@@ -75,30 +75,29 @@ 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", 92 printf ("Got %d elements out of" "200 expected after insertion.\n", ok1);
93 ok1); 93 GNUNET_CONTAINER_bloomfilter_free (bf);
94 GNUNET_CONTAINER_bloomfilter_free (bf); 94 return -1;
95 return -1; 95 }
96 }
97 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, buf, SIZE)) 96 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, buf, SIZE))
98 { 97 {
99 GNUNET_CONTAINER_bloomfilter_free (bf); 98 GNUNET_CONTAINER_bloomfilter_free (bf);
100 return -1; 99 return -1;
101 } 100 }
102 101
103 GNUNET_CONTAINER_bloomfilter_free (bf); 102 GNUNET_CONTAINER_bloomfilter_free (bf);
104 103
@@ -111,94 +110,93 @@ main (int argc, char *argv[])
111 ok1 = 0; 110 ok1 = 0;
112 ok2 = 0; 111 ok2 = 0;
113 for (i = 0; i < 200; i++) 112 for (i = 0; i < 200; i++)
114 { 113 {
115 nextHC (&tmp); 114 nextHC (&tmp);
116 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 115 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
117 ok1++; 116 ok1++;
118 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) 117 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
119 ok2++; 118 ok2++;
120 } 119 }
121 if (ok1 != 200) 120 if (ok1 != 200)
122 { 121 {
123 printf ("Got %d elements out of 200 " "expected after reloading.\n", 122 printf ("Got %d elements out of 200 " "expected after reloading.\n", ok1);
124 ok1); 123 GNUNET_CONTAINER_bloomfilter_free (bf);
125 GNUNET_CONTAINER_bloomfilter_free (bf); 124 GNUNET_CONTAINER_bloomfilter_free (bfi);
126 GNUNET_CONTAINER_bloomfilter_free (bfi); 125 return -1;
127 return -1; 126 }
128 }
129 127
130 if (ok2 != 200) 128 if (ok2 != 200)
131 { 129 {
132 printf ("Got %d elements out of 200 " 130 printf ("Got %d elements out of 200 " "expected after initialization.\n",
133 "expected after initialization.\n", ok2); 131 ok2);
134 GNUNET_CONTAINER_bloomfilter_free (bf); 132 GNUNET_CONTAINER_bloomfilter_free (bf);
135 GNUNET_CONTAINER_bloomfilter_free (bfi); 133 GNUNET_CONTAINER_bloomfilter_free (bfi);
136 return -1; 134 return -1;
137 } 135 }
138 136
139 SRANDOM (1); 137 SRANDOM (1);
140 for (i = 0; i < 100; i++) 138 for (i = 0; i < 100; i++)
141 { 139 {
142 nextHC (&tmp); 140 nextHC (&tmp);
143 GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp); 141 GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp);
144 GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp); 142 GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp);
145 } 143 }
146 144
147 SRANDOM (1); 145 SRANDOM (1);
148 146
149 ok1 = 0; 147 ok1 = 0;
150 ok2 = 0; 148 ok2 = 0;
151 for (i = 0; i < 200; i++) 149 for (i = 0; i < 200; i++)
152 { 150 {
153 nextHC (&tmp); 151 nextHC (&tmp);
154 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 152 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
155 ok1++; 153 ok1++;
156 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) 154 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
157 ok2++; 155 ok2++;
158 } 156 }
159 157
160 if (ok1 != 100) 158 if (ok1 != 100)
161 { 159 {
162 printf ("Expected 100 elements in loaded filter" 160 printf ("Expected 100 elements in loaded filter"
163 " after adding 200 and deleting 100, got %d\n", ok1); 161 " after adding 200 and deleting 100, got %d\n", ok1);
164 GNUNET_CONTAINER_bloomfilter_free (bf); 162 GNUNET_CONTAINER_bloomfilter_free (bf);
165 GNUNET_CONTAINER_bloomfilter_free (bfi); 163 GNUNET_CONTAINER_bloomfilter_free (bfi);
166 return -1; 164 return -1;
167 } 165 }
168 if (ok2 != 200) 166 if (ok2 != 200)
169 { 167 {
170 printf ("Expected 200 elements in initialized filter" 168 printf ("Expected 200 elements in initialized filter"
171 " after adding 200 and deleting 100 " 169 " after adding 200 and deleting 100 "
172 "(which should do nothing for a filter not backed by a file), got %d\n", 170 "(which should do nothing for a filter not backed by a file), got %d\n",
173 ok2); 171 ok2);
174 GNUNET_CONTAINER_bloomfilter_free (bf); 172 GNUNET_CONTAINER_bloomfilter_free (bf);
175 GNUNET_CONTAINER_bloomfilter_free (bfi); 173 GNUNET_CONTAINER_bloomfilter_free (bfi);
176 return -1; 174 return -1;
177 } 175 }
178 176
179 SRANDOM (3); 177 SRANDOM (3);
180 178
181 GNUNET_CONTAINER_bloomfilter_clear (bf); 179 GNUNET_CONTAINER_bloomfilter_clear (bf);
182 falseok = 0; 180 falseok = 0;
183 for (i = 0; i < 1000; i++) 181 for (i = 0; i < 1000; i++)
184 { 182 {
185 nextHC (&tmp); 183 nextHC (&tmp);
186 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 184 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
187 falseok++; 185 falseok++;
188 } 186 }
189 if (falseok > 0) 187 if (falseok > 0)
190 { 188 {
191 GNUNET_CONTAINER_bloomfilter_free (bf); 189 GNUNET_CONTAINER_bloomfilter_free (bf);
192 GNUNET_CONTAINER_bloomfilter_free (bfi); 190 GNUNET_CONTAINER_bloomfilter_free (bfi);
193 return -1; 191 return -1;
194 } 192 }
195 193
196 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or (bf, buf, SIZE)) 194 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or (bf, buf, SIZE))
197 { 195 {
198 GNUNET_CONTAINER_bloomfilter_free (bf); 196 GNUNET_CONTAINER_bloomfilter_free (bf);
199 GNUNET_CONTAINER_bloomfilter_free (bfi); 197 GNUNET_CONTAINER_bloomfilter_free (bfi);
200 return -1; 198 return -1;
201 } 199 }
202 200
203 SRANDOM (2); 201 SRANDOM (2);
204 i = 20; 202 i = 20;
@@ -212,30 +210,30 @@ main (int argc, char *argv[])
212 ok1 = 0; 210 ok1 = 0;
213 ok2 = 0; 211 ok2 = 0;
214 for (i = 0; i < 20; i++) 212 for (i = 0; i < 20; i++)
215 { 213 {
216 nextHC (&tmp); 214 nextHC (&tmp);
217 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES) 215 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
218 ok1++; 216 ok1++;
219 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES) 217 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
220 ok2++; 218 ok2++;
221 } 219 }
222 220
223 if (ok1 != 20) 221 if (ok1 != 20)
224 { 222 {
225 printf ("Expected 20 elements in resized file-backed filter" 223 printf ("Expected 20 elements in resized file-backed filter"
226 " after adding 20, got %d\n", ok1); 224 " after adding 20, got %d\n", ok1);
227 GNUNET_CONTAINER_bloomfilter_free (bf); 225 GNUNET_CONTAINER_bloomfilter_free (bf);
228 GNUNET_CONTAINER_bloomfilter_free (bfi); 226 GNUNET_CONTAINER_bloomfilter_free (bfi);
229 return -1; 227 return -1;
230 } 228 }
231 if (ok2 != 20) 229 if (ok2 != 20)
232 { 230 {
233 printf ("Expected 20 elements in resized filter" 231 printf ("Expected 20 elements in resized filter"
234 " after adding 20, got %d\n", ok2); 232 " after adding 20, got %d\n", ok2);
235 GNUNET_CONTAINER_bloomfilter_free (bf); 233 GNUNET_CONTAINER_bloomfilter_free (bf);
236 GNUNET_CONTAINER_bloomfilter_free (bfi); 234 GNUNET_CONTAINER_bloomfilter_free (bfi);
237 return -1; 235 return -1;
238 } 236 }
239 237
240 238
241 GNUNET_CONTAINER_bloomfilter_free (bf); 239 GNUNET_CONTAINER_bloomfilter_free (bf);
diff --git a/src/util/test_container_heap.c b/src/util/test_container_heap.c
index c1c4eb360..a2a004aa7 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 66f90ff10..fe1dd79c1 100644
--- a/src/util/test_container_meta_data.c
+++ b/src/util/test_container_meta_data.c
@@ -41,55 +41,51 @@ 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>", 44 GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE,
45 EXTRACTOR_METATYPE_TITLE, 45 EXTRACTOR_METAFORMAT_UTF8,
46 EXTRACTOR_METAFORMAT_UTF8, 46 "text/plain", "TestTitle",
47 "text/plain", "TestTitle", 47 strlen ("TestTitle") + 1))
48 strlen ("TestTitle") + 1))
49 ABORT (m); 48 ABORT (m);
50 if (GNUNET_OK != 49 if (GNUNET_OK !=
51 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 50 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
52 EXTRACTOR_METATYPE_AUTHOR_NAME, 51 EXTRACTOR_METATYPE_AUTHOR_NAME,
53 EXTRACTOR_METAFORMAT_UTF8, 52 EXTRACTOR_METAFORMAT_UTF8,
54 "text/plain", "TestTitle", 53 "text/plain", "TestTitle",
55 strlen ("TestTitle") + 1)) 54 strlen ("TestTitle") + 1))
56 ABORT (m); 55 ABORT (m);
57 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */ 56 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */
58 ABORT (m); 57 ABORT (m);
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! */ 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! */
60 ABORT (m); 59 ABORT (m);
61 if (2 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 60 if (2 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
62 ABORT (m); 61 ABORT (m);
63 if (GNUNET_OK != 62 if (GNUNET_OK !=
64 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, 63 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME,
65 "TestTitle", 64 "TestTitle", strlen ("TestTitle") + 1))
66 strlen ("TestTitle") + 1))
67 ABORT (m); 65 ABORT (m);
68 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ 66 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */
69 ABORT (m); 67 ABORT (m);
70 if (1 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 68 if (1 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
71 ABORT (m); 69 ABORT (m);
72 if (GNUNET_OK != 70 if (GNUNET_OK !=
73 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, 71 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE,
74 "TestTitle", 72 "TestTitle", strlen ("TestTitle") + 1))
75 strlen ("TestTitle") + 1))
76 ABORT (m); 73 ABORT (m);
77 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */ 74 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */
78 ABORT (m); 75 ABORT (m);
79 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 76 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
80 ABORT (m); 77 ABORT (m);
81 for (j = 0; j < i; j++) 78 for (j = 0; j < i; j++)
82 { 79 {
83 GNUNET_snprintf (val, sizeof (val), "%s.%d", 80 GNUNET_snprintf (val, sizeof (val), "%s.%d",
84 "A teststring that should compress well.", j); 81 "A teststring that should compress well.", j);
85 if (GNUNET_OK != 82 if (GNUNET_OK !=
86 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 83 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
87 EXTRACTOR_METATYPE_UNKNOWN, 84 EXTRACTOR_METATYPE_UNKNOWN,
88 EXTRACTOR_METAFORMAT_UTF8, 85 EXTRACTOR_METAFORMAT_UTF8,
89 "text/plain", val, 86 "text/plain", val, strlen (val) + 1))
90 strlen (val) + 1)) 87 ABORT (m);
91 ABORT (m); 88 }
92 }
93 if (i != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 89 if (i != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
94 ABORT (m); 90 ABORT (m);
95 91
@@ -97,27 +93,27 @@ testMeta (int i)
97 sval = NULL; 93 sval = NULL;
98 if (size != 94 if (size !=
99 GNUNET_CONTAINER_meta_data_serialize (m, &sval, size, 95 GNUNET_CONTAINER_meta_data_serialize (m, &sval, size,
100 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) 96 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
101 { 97 {
102 GNUNET_free_non_null (sval); 98 GNUNET_free_non_null (sval);
103 ABORT (m); 99 ABORT (m);
104 } 100 }
105 GNUNET_CONTAINER_meta_data_destroy (m); 101 GNUNET_CONTAINER_meta_data_destroy (m);
106 m = GNUNET_CONTAINER_meta_data_deserialize (sval, size); 102 m = GNUNET_CONTAINER_meta_data_deserialize (sval, size);
107 GNUNET_free (sval); 103 GNUNET_free (sval);
108 if (m == NULL) 104 if (m == NULL)
109 ABORT (m); 105 ABORT (m);
110 for (j = 0; j < i; j++) 106 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))
111 { 113 {
112 GNUNET_snprintf (val, sizeof (val), "%s.%d", 114 ABORT (m);
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 }
120 } 115 }
116 }
121 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL)) 117 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
122 ABORT (m); 118 ABORT (m);
123 GNUNET_CONTAINER_meta_data_destroy (m); 119 GNUNET_CONTAINER_meta_data_destroy (m);
@@ -135,22 +131,22 @@ testMetaMore (int i)
135 131
136 meta = GNUNET_CONTAINER_meta_data_create (); 132 meta = GNUNET_CONTAINER_meta_data_create ();
137 for (q = 0; q <= i; q++) 133 for (q = 0; q <= i; q++)
138 { 134 {
139 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); 135 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
140 GNUNET_CONTAINER_meta_data_insert (meta, "<test>", 136 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
141 q % EXTRACTOR_metatype_get_max (), 137 q % EXTRACTOR_metatype_get_max (),
142 EXTRACTOR_METAFORMAT_UTF8, 138 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
143 "text/plain", txt, strlen (txt) + 1); 139 txt, strlen (txt) + 1);
144 } 140 }
145 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta); 141 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
146 data = GNUNET_malloc (size * 4); 142 data = GNUNET_malloc (size * 4);
147 if (size != 143 if (size !=
148 GNUNET_CONTAINER_meta_data_serialize (meta, &data, size * 4, 144 GNUNET_CONTAINER_meta_data_serialize (meta, &data, size * 4,
149 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) 145 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
150 { 146 {
151 GNUNET_free (data); 147 GNUNET_free (data);
152 ABORT (meta); 148 ABORT (meta);
153 } 149 }
154 GNUNET_CONTAINER_meta_data_destroy (meta); 150 GNUNET_CONTAINER_meta_data_destroy (meta);
155 GNUNET_free (data); 151 GNUNET_free (data);
156 return 0; 152 return 0;
@@ -166,22 +162,22 @@ testMetaLink ()
166 m = GNUNET_CONTAINER_meta_data_create (); 162 m = GNUNET_CONTAINER_meta_data_create ();
167 if (GNUNET_OK != 163 if (GNUNET_OK !=
168 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 164 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
169 EXTRACTOR_METATYPE_UNKNOWN, 165 EXTRACTOR_METATYPE_UNKNOWN,
170 EXTRACTOR_METAFORMAT_UTF8, 166 EXTRACTOR_METAFORMAT_UTF8,
171 "text/plain", "link", 167 "text/plain", "link",
172 strlen ("link") + 1)) 168 strlen ("link") + 1))
173 ABORT (m); 169 ABORT (m);
174 if (GNUNET_OK != 170 if (GNUNET_OK !=
175 GNUNET_CONTAINER_meta_data_insert (m, "<test>", 171 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
176 EXTRACTOR_METATYPE_FILENAME, 172 EXTRACTOR_METATYPE_FILENAME,
177 EXTRACTOR_METAFORMAT_UTF8, 173 EXTRACTOR_METAFORMAT_UTF8,
178 "text/plain", "lib-link.m4", 174 "text/plain", "lib-link.m4",
179 strlen ("lib-link.m4") + 1)) 175 strlen ("lib-link.m4") + 1))
180 ABORT (m); 176 ABORT (m);
181 val = NULL; 177 val = NULL;
182 size = 178 size =
183 GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) - 1, 179 GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) - 1,
184 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); 180 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
185 GNUNET_CONTAINER_meta_data_destroy (m); 181 GNUNET_CONTAINER_meta_data_destroy (m);
186 m = GNUNET_CONTAINER_meta_data_deserialize (val, size); 182 m = GNUNET_CONTAINER_meta_data_deserialize (val, size);
187 GNUNET_free (val); 183 GNUNET_free (val);
@@ -205,46 +201,44 @@ check ()
205 meta = GNUNET_CONTAINER_meta_data_create (); 201 meta = GNUNET_CONTAINER_meta_data_create ();
206 meta2 = GNUNET_CONTAINER_meta_data_create (); 202 meta2 = GNUNET_CONTAINER_meta_data_create ();
207 for (q = 0; q <= i; q++) 203 for (q = 0; q <= i; q++)
208 { 204 {
209 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q); 205 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
210 GNUNET_CONTAINER_meta_data_insert (meta, "<test>", 206 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
211 EXTRACTOR_METATYPE_UNKNOWN, 207 EXTRACTOR_METATYPE_UNKNOWN,
212 EXTRACTOR_METAFORMAT_UTF8, 208 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
213 "text/plain", "TestTitle", 209 "TestTitle", strlen ("TestTitle") + 1);
214 strlen ("TestTitle") + 1); 210 GNUNET_CONTAINER_meta_data_insert (meta2, "<test>",
215 GNUNET_CONTAINER_meta_data_insert (meta2, "<test>", 211 EXTRACTOR_METATYPE_UNKNOWN,
216 EXTRACTOR_METATYPE_UNKNOWN, 212 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
217 EXTRACTOR_METAFORMAT_UTF8, 213 "TestTitle", strlen ("TestTitle") + 1);
218 "text/plain", "TestTitle", 214 }
219 strlen ("TestTitle") + 1);
220 }
221 215
222 //check meta_data_test_equal 216 //check meta_data_test_equal
223 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2)) 217 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
224 { 218 {
225 GNUNET_CONTAINER_meta_data_destroy (meta2); 219 GNUNET_CONTAINER_meta_data_destroy (meta2);
226 ABORT (meta); 220 ABORT (meta);
227 } 221 }
228 222
229 //check meta_data_clear 223 //check meta_data_clear
230 GNUNET_CONTAINER_meta_data_clear (meta2); 224 GNUNET_CONTAINER_meta_data_clear (meta2);
231 if (0 != GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) 225 if (0 != GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
232 { 226 {
233 GNUNET_CONTAINER_meta_data_destroy (meta2); 227 GNUNET_CONTAINER_meta_data_destroy (meta2);
234 ABORT (meta); 228 ABORT (meta);
235 } 229 }
236 // check equal branch in meta_data_test_equal 230 // check equal branch in meta_data_test_equal
237 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta)) 231 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta))
238 { 232 {
239 GNUNET_CONTAINER_meta_data_destroy (meta2); 233 GNUNET_CONTAINER_meta_data_destroy (meta2);
240 ABORT (meta); 234 ABORT (meta);
241 } 235 }
242 // check "count" branch in meta_data_test_equal 236 // check "count" branch in meta_data_test_equal
243 if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2)) 237 if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
244 { 238 {
245 GNUNET_CONTAINER_meta_data_destroy (meta2); 239 GNUNET_CONTAINER_meta_data_destroy (meta2);
246 ABORT (meta); 240 ABORT (meta);
247 } 241 }
248 242
249 // check meta_data_add_publication_date 243 // check meta_data_add_publication_date
250 GNUNET_CONTAINER_meta_data_add_publication_date (meta2); 244 GNUNET_CONTAINER_meta_data_add_publication_date (meta2);
@@ -253,74 +247,74 @@ check ()
253 GNUNET_CONTAINER_meta_data_clear (meta2); 247 GNUNET_CONTAINER_meta_data_clear (meta2);
254 GNUNET_CONTAINER_meta_data_merge (meta2, meta); 248 GNUNET_CONTAINER_meta_data_merge (meta2, meta);
255 if (100 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) 249 if (100 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
256 { 250 {
257 GNUNET_CONTAINER_meta_data_destroy (meta2); 251 GNUNET_CONTAINER_meta_data_destroy (meta2);
258 ABORT (meta); 252 ABORT (meta);
259 } 253 }
260 254
261 // check meta_data_get_by_type 255 // check meta_data_get_by_type
262 GNUNET_CONTAINER_meta_data_clear (meta2); 256 GNUNET_CONTAINER_meta_data_clear (meta2);
263 if (NULL != 257 if (NULL !=
264 (str = 258 (str =
265 GNUNET_CONTAINER_meta_data_get_by_type (meta2, 259 GNUNET_CONTAINER_meta_data_get_by_type (meta2,
266 EXTRACTOR_METATYPE_UNKNOWN))) 260 EXTRACTOR_METATYPE_UNKNOWN)))
267 { 261 {
268 GNUNET_CONTAINER_meta_data_destroy (meta2); 262 GNUNET_CONTAINER_meta_data_destroy (meta2);
269 GNUNET_free (str); 263 GNUNET_free (str);
270 ABORT (meta); 264 ABORT (meta);
271 } 265 }
272 266
273 str = 267 str =
274 GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN); 268 GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN);
275 GNUNET_assert (NULL != str); 269 GNUNET_assert (NULL != str);
276 if (str[0] != 'T') 270 if (str[0] != 'T')
277 { 271 {
278 GNUNET_CONTAINER_meta_data_destroy (meta2); 272 GNUNET_CONTAINER_meta_data_destroy (meta2);
279 GNUNET_free (str); 273 GNUNET_free (str);
280 ABORT (meta); 274 ABORT (meta);
281 } 275 }
282 GNUNET_free (str); 276 GNUNET_free (str);
283 277
284 // check branch 278 // check branch
285 if (NULL != 279 if (NULL !=
286 (str = 280 (str =
287 GNUNET_CONTAINER_meta_data_get_by_type (meta, 281 GNUNET_CONTAINER_meta_data_get_by_type (meta,
288 EXTRACTOR_METATYPE_PUBLICATION_DATE))) 282 EXTRACTOR_METATYPE_PUBLICATION_DATE)))
289 { 283 {
290 GNUNET_free (str); 284 GNUNET_free (str);
291 GNUNET_CONTAINER_meta_data_destroy (meta2); 285 GNUNET_CONTAINER_meta_data_destroy (meta2);
292 ABORT (meta); 286 ABORT (meta);
293 } 287 }
294 288
295 //check meta_data_get_first_by_types 289 //check meta_data_get_first_by_types
296 str = 290 str =
297 GNUNET_CONTAINER_meta_data_get_first_by_types (meta, 291 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
298 EXTRACTOR_METATYPE_UNKNOWN, 292 EXTRACTOR_METATYPE_UNKNOWN,
299 -1); 293 -1);
300 GNUNET_assert (NULL != str); 294 GNUNET_assert (NULL != str);
301 if (str[0] != 'T') 295 if (str[0] != 'T')
302 { 296 {
303 GNUNET_CONTAINER_meta_data_destroy (meta2); 297 GNUNET_CONTAINER_meta_data_destroy (meta2);
304 GNUNET_free (str); 298 GNUNET_free (str);
305 ABORT (meta); 299 ABORT (meta);
306 } 300 }
307 GNUNET_free (str); 301 GNUNET_free (str);
308 302
309 //check meta_data_get_thumbnail 303 //check meta_data_get_thumbnail
310 if (GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb) != 0) 304 if (GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb) != 0)
311 { 305 {
312 GNUNET_free (thumb); 306 GNUNET_free (thumb);
313 GNUNET_CONTAINER_meta_data_destroy (meta2); 307 GNUNET_CONTAINER_meta_data_destroy (meta2);
314 ABORT (meta); 308 ABORT (meta);
315 } 309 }
316 GNUNET_CONTAINER_meta_data_destroy (meta2); 310 GNUNET_CONTAINER_meta_data_destroy (meta2);
317 //check meta_data_duplicate 311 //check meta_data_duplicate
318 meta2 = GNUNET_CONTAINER_meta_data_duplicate (meta); 312 meta2 = GNUNET_CONTAINER_meta_data_duplicate (meta);
319 if (200 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL)) 313 if (200 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
320 { 314 {
321 GNUNET_CONTAINER_meta_data_destroy (meta2); 315 GNUNET_CONTAINER_meta_data_destroy (meta2);
322 ABORT (meta); 316 ABORT (meta);
323 } 317 }
324 GNUNET_CONTAINER_meta_data_destroy (meta2); 318 GNUNET_CONTAINER_meta_data_destroy (meta2);
325 GNUNET_CONTAINER_meta_data_destroy (meta); 319 GNUNET_CONTAINER_meta_data_destroy (meta);
326 return 0; 320 return 0;
diff --git a/src/util/test_container_multihashmap.c b/src/util/test_container_multihashmap.c
index 5732ace93..ba621c17e 100644
--- a/src/util/test_container_multihashmap.c
+++ b/src/util/test_container_multihashmap.c
@@ -52,41 +52,38 @@ 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 == 55 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
56 GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
57 56
58 CHECK (GNUNET_OK == 57 CHECK (GNUNET_OK ==
59 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", 58 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
60 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 59 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
61 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m)); 60 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
62 ret = GNUNET_CONTAINER_multihashmap_get (m, &k1); 61 ret = GNUNET_CONTAINER_multihashmap_get (m, &k1);
63 GNUNET_assert (ret != NULL); 62 GNUNET_assert (ret != NULL);
64 CHECK (0 == strcmp ("v1", ret)); 63 CHECK (0 == strcmp ("v1", ret));
65 CHECK (GNUNET_NO == 64 CHECK (GNUNET_NO ==
66 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1", 65 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
67 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 66 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
68 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m)); 67 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
69 CHECK (GNUNET_OK == 68 CHECK (GNUNET_OK ==
70 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", 69 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
71 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 70 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
72 CHECK (GNUNET_OK == 71 CHECK (GNUNET_OK ==
73 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3", 72 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3",
74 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 73 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
75 CHECK (3 == GNUNET_CONTAINER_multihashmap_size (m)); 74 CHECK (3 == GNUNET_CONTAINER_multihashmap_size (m));
76 CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (m, &k1, "v3")); 75 CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (m, &k1, "v3"));
77 CHECK (2 == GNUNET_CONTAINER_multihashmap_size (m)); 76 CHECK (2 == GNUNET_CONTAINER_multihashmap_size (m));
78 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (m, &k1)); 77 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (m, &k1));
79 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2)); 78 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2));
80 CHECK (2 == 79 CHECK (2 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
81 GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL)); 80 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL));
82 CHECK (0 ==
83 GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL));
84 CHECK (2 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL)); 81 CHECK (2 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL));
85 CHECK (2 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1)); 82 CHECK (2 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1));
86 for (j = 0; j < 1024; j++) 83 for (j = 0; j < 1024; j++)
87 CHECK (GNUNET_OK == 84 CHECK (GNUNET_OK ==
88 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2", 85 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
89 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 86 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
90 GNUNET_CONTAINER_multihashmap_destroy (m); 87 GNUNET_CONTAINER_multihashmap_destroy (m);
91 return 0; 88 return 0;
92} 89}
diff --git a/src/util/test_container_slist.c b/src/util/test_container_slist.c
index 394dc4cdb..1b63d3dbc 100644
--- a/src/util/test_container_slist.c
+++ b/src/util/test_container_slist.c
@@ -46,66 +46,64 @@ 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, 49 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
50 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 50 &i, sizeof (i));
51 &i, sizeof (i));
52 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); 51 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
53 52
54 for (it = GNUNET_CONTAINER_slist_begin (l), i = 99; 53 for (it = GNUNET_CONTAINER_slist_begin (l), i = 99;
55 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES; 54 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES;
56 GNUNET_CONTAINER_slist_next (&it), i--) 55 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)))
57 { 60 {
58 p = GNUNET_CONTAINER_slist_get (&it, &s); 61 GNUNET_CONTAINER_slist_iter_destroy (&it);
59 62 GNUNET_assert (0);
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));
69 } 63 }
64 j *= 2;
65 GNUNET_CONTAINER_slist_insert (&it,
66 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
67 &j, sizeof (j));
68 }
70 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200); 69 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
71 i = 198; 70 i = 198;
72 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i))); 71 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
73 72
74 for (it = GNUNET_CONTAINER_slist_begin (l); 73 for (it = GNUNET_CONTAINER_slist_begin (l);
75 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES;) 74 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES;)
76 { 75 {
77 p = GNUNET_CONTAINER_slist_get (&it, &s); 76 p = GNUNET_CONTAINER_slist_get (&it, &s);
78 GNUNET_assert (p != NULL); 77 GNUNET_assert (p != NULL);
79 GNUNET_assert (s == sizeof (i)); 78 GNUNET_assert (s == sizeof (i));
80 i = *(int *) p; 79 i = *(int *) p;
81 80
82 GNUNET_assert (GNUNET_CONTAINER_slist_next (&it) == GNUNET_YES); 81 GNUNET_assert (GNUNET_CONTAINER_slist_next (&it) == GNUNET_YES);
83 GNUNET_assert (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES); 82 GNUNET_assert (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES);
84 83
85 p = GNUNET_CONTAINER_slist_get (&it, &s); 84 p = GNUNET_CONTAINER_slist_get (&it, &s);
86 GNUNET_assert (p != NULL); 85 GNUNET_assert (p != NULL);
87 GNUNET_assert (s == sizeof (j)); 86 GNUNET_assert (s == sizeof (j));
88 j = *(int *) p; 87 j = *(int *) p;
89 88
90 GNUNET_assert (j * 2 == i); 89 GNUNET_assert (j * 2 == i);
91 90
92 GNUNET_CONTAINER_slist_erase (&it); 91 GNUNET_CONTAINER_slist_erase (&it);
93 } 92 }
94 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); 93 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
95 i = 99; 94 i = 99;
96 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == 95 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
97 GNUNET_NO); 96 GNUNET_NO);
98 i = 198; 97 i = 198;
99 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == 98 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
100 GNUNET_YES); 99 GNUNET_YES);
101 100
102 GNUNET_CONTAINER_slist_clear (l); 101 GNUNET_CONTAINER_slist_clear (l);
103 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0); 102 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
104 103
105 for (i = 0; i < 100; i++) 104 for (i = 0; i < 100; i++)
106 GNUNET_CONTAINER_slist_add (l, 105 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
107 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 106 &i, sizeof (i));
108 &i, sizeof (i));
109 /*check slist_append */ 107 /*check slist_append */
110 GNUNET_CONTAINER_slist_append (l, l); 108 GNUNET_CONTAINER_slist_append (l, l);
111 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200); 109 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
@@ -116,35 +114,34 @@ main (int argc, char *argv[])
116 l = GNUNET_CONTAINER_slist_create (); 114 l = GNUNET_CONTAINER_slist_create ();
117 for (i = 0; i < 100; i++) 115 for (i = 0; i < 100; i++)
118 GNUNET_CONTAINER_slist_add_end (l, 116 GNUNET_CONTAINER_slist_add_end (l,
119 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 117 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
120 &i, sizeof (i)); 118 &i, sizeof (i));
121 119
122 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100); 120 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
123 121
124 for (it = GNUNET_CONTAINER_slist_begin (l), i = 0; 122 for (it = GNUNET_CONTAINER_slist_begin (l), i = 0;
125 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES; 123 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES;
126 GNUNET_CONTAINER_slist_next (&it), i++) 124 GNUNET_CONTAINER_slist_next (&it), i++)
127 { 125 {
128 p = GNUNET_CONTAINER_slist_get (&it, &s); 126 p = GNUNET_CONTAINER_slist_get (&it, &s);
129 127
130 if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i))) 128 if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i)))
131 { 129 {
132 GNUNET_assert (0); 130 GNUNET_assert (0);
133 }
134 } 131 }
132 }
135 GNUNET_CONTAINER_slist_destroy (l); 133 GNUNET_CONTAINER_slist_destroy (l);
136 134
137 /*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC */ 135 /*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC */
138 l = GNUNET_CONTAINER_slist_create (); 136 l = GNUNET_CONTAINER_slist_create ();
139 137
140 for (i = 0; i < 100; i++) 138 for (i = 0; i < 100; i++)
141 { 139 {
142 ip = GNUNET_malloc (sizeof (int)); 140 ip = GNUNET_malloc (sizeof (int));
143 *ip = i; 141 *ip = i;
144 GNUNET_CONTAINER_slist_add (l, 142 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC,
145 GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC, 143 ip, sizeof (int));
146 ip, sizeof (int)); 144 }
147 }
148 //creat_add 145 //creat_add
149 it = GNUNET_CONTAINER_slist_begin (l); 146 it = GNUNET_CONTAINER_slist_begin (l);
150 p = GNUNET_CONTAINER_slist_get (&it, &s); 147 p = GNUNET_CONTAINER_slist_get (&it, &s);
diff --git a/src/util/test_crypto_aes.c b/src/util/test_crypto_aes.c
index e928b96a7..971e9afbe 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 25931b625..a0161b575 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,45 +133,42 @@ 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)
136 { 147 {
148 printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n",
149 number_of_runs, gcry_strerror (rc));
150 continue;
151 }
137 152
138 if (number_of_runs % 1000 == 0) 153 rc = gcry_cipher_setkey (handle, &sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
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);
172 154
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));
173 } 166 }
174 167
168 gcry_cipher_close (handle);
169
170 }
171
175 return number_of_weak_keys; 172 return number_of_weak_keys;
176} 173}
177 174
@@ -183,19 +180,19 @@ main (int argc, char *argv[])
183 GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL); 180 GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL);
184 GNUNET_CRYPTO_random_disable_entropy_gathering (); 181 GNUNET_CRYPTO_random_disable_entropy_gathering ();
185 if (GENERATE_WEAK_KEYS) 182 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
186 { 191 {
187 weak_keys = getWeakKeys (); 192 printf ("\n%d weak keys found in %d runs.\n", weak_keys,
188 193 MAX_WEAK_KEY_TRIALS);
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 }
198 } 194 }
195 }
199 196
200 if (testWeakKey () != 0) 197 if (testWeakKey () != 0)
201 return -1; 198 return -1;
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c
index a32a28ef0..b0fa4f332 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,9 +124,8 @@ 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, 128 FILENAME, 1024, &finished_task, cls));
129 cls));
130} 129}
131 130
132 131
diff --git a/src/util/test_crypto_hkdf.c b/src/util/test_crypto_hkdf.c
index d5ee4d433..752116194 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,9 +162,8 @@ 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), 166 ikm, sizeof (ikm), info, sizeof (info), NULL) == GNUNET_YES);
167 NULL) == GNUNET_YES);
168 GNUNET_assert (memcmp (result, okm, l) == 0); 167 GNUNET_assert (memcmp (result, okm, l) == 0);
169 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 168 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
170} 169}
@@ -173,7 +172,7 @@ void
173tc5 () 172tc5 ()
174{ 173{
175 unsigned char ikm[80] = 174 unsigned char ikm[80] =
176 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 175 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
177 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 176 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
178 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 177 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
179 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 178 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
@@ -182,7 +181,7 @@ tc5 ()
182 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f 181 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
183 }; 182 };
184 unsigned char salt[80] = 183 unsigned char salt[80] =
185 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 184 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
186 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 185 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
187 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 186 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81,
188 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 187 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
@@ -191,7 +190,7 @@ tc5 ()
191 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf 190 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf
192 }; 191 };
193 unsigned char info[80] = 192 unsigned char info[80] =
194 { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 193 { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
195 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 194 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
196 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 195 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
197 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 196 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd,
@@ -200,7 +199,7 @@ tc5 ()
200 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff 199 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
201 }; 200 };
202 unsigned char okm[82] = 201 unsigned char okm[82] =
203 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 202 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1,
204 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 203 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d,
205 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3, 204 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3,
206 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, 205 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2,
@@ -213,9 +212,8 @@ tc5 ()
213 212
214 memset (result, 0, sizeof (result)); 213 memset (result, 0, sizeof (result));
215 GNUNET_assert (GNUNET_CRYPTO_hkdf 214 GNUNET_assert (GNUNET_CRYPTO_hkdf
216 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), 215 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt),
217 ikm, sizeof (ikm), info, sizeof (info), 216 ikm, sizeof (ikm), info, sizeof (info), NULL) == GNUNET_YES);
218 NULL) == GNUNET_YES);
219 GNUNET_assert (memcmp (result, okm, l) == 0); 217 GNUNET_assert (memcmp (result, okm, l) == 0);
220 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 218 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
221} 219}
@@ -224,11 +222,11 @@ void
224tc6 () 222tc6 ()
225{ 223{
226 unsigned char ikm[22] = 224 unsigned char ikm[22] =
227 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 225 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
228 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 226 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
229 }; 227 };
230 unsigned char okm[42] = 228 unsigned char okm[42] =
231 { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61, 0xd1, 0xe5, 229 { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61, 0xd1, 0xe5,
232 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06, 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 230 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06, 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20,
233 0xa3, 0x06, 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0, 0xea, 0x00, 231 0xa3, 0x06, 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0, 0xea, 0x00,
234 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3, 0x49, 0x18 232 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3, 0x49, 0x18
@@ -238,8 +236,8 @@ tc6 ()
238 236
239 memset (result, 0, sizeof (result)); 237 memset (result, 0, sizeof (result));
240 GNUNET_assert (GNUNET_CRYPTO_hkdf 238 GNUNET_assert (GNUNET_CRYPTO_hkdf
241 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm, 239 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm,
242 sizeof (ikm), NULL, 0, NULL) == GNUNET_YES); 240 sizeof (ikm), NULL, 0, NULL) == GNUNET_YES);
243 GNUNET_assert (memcmp (result, okm, l) == 0); 241 GNUNET_assert (memcmp (result, okm, l) == 0);
244 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 242 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
245} 243}
@@ -248,7 +246,7 @@ void
248tc7 () 246tc7 ()
249{ 247{
250 unsigned char ikm[80] = 248 unsigned char ikm[80] =
251 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 249 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
252 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 250 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
253 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 251 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
254 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 252 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
@@ -257,7 +255,7 @@ tc7 ()
257 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f 255 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
258 }; 256 };
259 unsigned char salt[80] = 257 unsigned char salt[80] =
260 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 258 { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
261 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 259 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
262 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 260 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81,
263 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 261 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
@@ -277,7 +275,7 @@ tc7 ()
277 0xfe, 0xff 275 0xfe, 0xff
278 }; 276 };
279 unsigned char okm[82] = 277 unsigned char okm[82] =
280 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 278 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1,
281 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 279 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d,
282 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3, 280 0x92, 0x19, 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3,
283 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, 281 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2,
@@ -290,9 +288,9 @@ tc7 ()
290 288
291 memset (result, 0, sizeof (result)); 289 memset (result, 0, sizeof (result));
292 GNUNET_assert (GNUNET_CRYPTO_hkdf 290 GNUNET_assert (GNUNET_CRYPTO_hkdf
293 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt), 291 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof (salt),
294 ikm, sizeof (ikm), info1, sizeof (info1), info2, 292 ikm, sizeof (ikm), info1, sizeof (info1), info2,
295 sizeof (info2), NULL) == GNUNET_YES); 293 sizeof (info2), NULL) == GNUNET_YES);
296 GNUNET_assert (memcmp (result, okm, l) == 0); 294 GNUNET_assert (memcmp (result, okm, l) == 0);
297 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 295 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
298} 296}
@@ -301,13 +299,13 @@ void
301tc8 () 299tc8 ()
302{ 300{
303 unsigned char ikm[32] = 301 unsigned char ikm[32] =
304 { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72, 302 { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72,
305 0x11, 0xa1, 0xdc, 0x0b, 0x07, 0xdb, 0x1a, 0x5e, 0xd9, 0xb9, 0x81, 0xbe, 303 0x11, 0xa1, 0xdc, 0x0b, 0x07, 0xdb, 0x1a, 0x5e, 0xd9, 0xb9, 0x81, 0xbe,
306 0xea, 0xe4, 0x31, 0x5f, 0x24, 0xff, 0xfe, 0x50, 0x8a, 0xde 304 0xea, 0xe4, 0x31, 0x5f, 0x24, 0xff, 0xfe, 0x50, 0x8a, 0xde
307 }; 305 };
308 unsigned char salt[4] = { 0xfc, 0x62, 0x76, 0x35 }; 306 unsigned char salt[4] = { 0xfc, 0x62, 0x76, 0x35 };
309 unsigned char info[86] = 307 unsigned char info[86] =
310 { 0x8c, 0x0d, 0xcf, 0xb3, 0x25, 0x6e, 0x88, 0x0d, 0xc1, 0x0b, 308 { 0x8c, 0x0d, 0xcf, 0xb3, 0x25, 0x6e, 0x88, 0x0d, 0xc1, 0x0b,
311 0x1d, 0x33, 0x15, 0x3e, 0x52, 0x0b, 0xb0, 0x77, 0xff, 0x7d, 0xc3, 0xc7, 309 0x1d, 0x33, 0x15, 0x3e, 0x52, 0x0b, 0xb0, 0x77, 0xff, 0x7d, 0xc3, 0xc7,
312 0xef, 0xe5, 0x8e, 0x3c, 0xc4, 0x4e, 0x8b, 0x41, 0x46, 0x1f, 0x02, 0x94, 310 0xef, 0xe5, 0x8e, 0x3c, 0xc4, 0x4e, 0x8b, 0x41, 0x46, 0x1f, 0x02, 0x94,
313 0x82, 0x35, 0xc5, 0xa6, 0x5e, 0x91, 0xd8, 0xa2, 0x90, 0xfd, 0x6f, 0xb4, 311 0x82, 0x35, 0xc5, 0xa6, 0x5e, 0x91, 0xd8, 0xa2, 0x90, 0xfd, 0x6f, 0xb4,
@@ -317,7 +315,7 @@ tc8 ()
317 0x74, 0x6f, 0x72, 0x00 315 0x74, 0x6f, 0x72, 0x00
318 }; 316 };
319 unsigned char okm[16] = 317 unsigned char okm[16] =
320 { 0xd6, 0x90, 0xec, 0x9e, 0x62, 0xdf, 0xb9, 0x41, 0xff, 0x92, 318 { 0xd6, 0x90, 0xec, 0x9e, 0x62, 0xdf, 0xb9, 0x41, 0xff, 0x92,
321 0x4f, 0xd2, 0xf6, 0x1d, 0x67, 0xe0 319 0x4f, 0xd2, 0xf6, 0x1d, 0x67, 0xe0
322 }; 320 };
323 char result[18]; 321 char result[18];
@@ -325,9 +323,9 @@ tc8 ()
325 323
326 memset (result, 0, sizeof (result)); 324 memset (result, 0, sizeof (result));
327 GNUNET_assert (GNUNET_CRYPTO_hkdf 325 GNUNET_assert (GNUNET_CRYPTO_hkdf
328 (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt, 326 (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt,
329 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info), 327 sizeof (salt), ikm, sizeof (ikm), info, sizeof (info),
330 NULL) == GNUNET_YES); 328 NULL) == GNUNET_YES);
331 GNUNET_assert (memcmp (result, okm, l) == 0); 329 GNUNET_assert (memcmp (result, okm, l) == 0);
332 GNUNET_assert (memcmp (result + l, "\0", 2) == 0); 330 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
333} 331}
diff --git a/src/util/test_crypto_ksk.c b/src/util/test_crypto_ksk.c
index 55d2dffcb..2789f5edb 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,29 +50,27 @@ 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); 60 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++)
61 i++)
62 printf ("%02x", ((unsigned char *) &pkey)[i]); 61 printf ("%02x", ((unsigned char *) &pkey)[i]);
63 printf ("\n"); 62 printf ("\n");
64#endif 63#endif
65 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); 64 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++)
66 i++) 65 {
66 snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]);
67 if (0 != strncmp (out, &want[i * 2], 2))
67 { 68 {
68 snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]); 69 fprintf (stderr, " Failed! Wanted %.2s but got %2s at %d\n", &want[i * 2],
69 if (0 != strncmp (out, &want[i * 2], 2)) 70 out, i);
70 { 71 return GNUNET_SYSERR;
71 fprintf (stderr, " Failed! Wanted %.2s but got %2s at %d\n",
72 &want[i * 2], out, i);
73 return GNUNET_SYSERR;
74 }
75 } 72 }
73 }
76 fprintf (stderr, " OK\n"); 74 fprintf (stderr, " OK\n");
77 return GNUNET_OK; 75 return GNUNET_OK;
78} 76}
@@ -91,10 +89,10 @@ testMultiKey (const char *word)
91 GNUNET_CRYPTO_hash (word, strlen (word), &in); 89 GNUNET_CRYPTO_hash (word, strlen (word), &in);
92 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); 90 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
93 if (hostkey == NULL) 91 if (hostkey == NULL)
94 { 92 {
95 GNUNET_break (0); 93 GNUNET_break (0);
96 return GNUNET_SYSERR; 94 return GNUNET_SYSERR;
97 } 95 }
98 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); 96 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
99 /* 97 /*
100 * for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++) 98 * for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
@@ -102,26 +100,26 @@ testMultiKey (const char *word)
102 * printf("\n"); */ 100 * printf("\n"); */
103 GNUNET_CRYPTO_rsa_key_free (hostkey); 101 GNUNET_CRYPTO_rsa_key_free (hostkey);
104 for (i = 0; i < UNIQUE_ITER; i++) 102 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)
105 { 107 {
106 fprintf (stderr, "."); 108 GNUNET_break (0);
107 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); 109 fprintf (stderr, " ERROR\n");
108 if (hostkey == NULL) 110 return GNUNET_SYSERR;
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 }
124 } 111 }
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 }
125 fprintf (stderr, " OK\n"); 123 fprintf (stderr, " OK\n");
126 return GNUNET_OK; 124 return GNUNET_OK;
127} 125}
@@ -143,35 +141,35 @@ testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
143 ok = 0; 141 ok = 0;
144 start = GNUNET_TIME_absolute_get (); 142 start = GNUNET_TIME_absolute_get ();
145 for (i = 0; i < ITER; i++) 143 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))
146 { 157 {
147 fprintf (stderr, "."); 158 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
148 if (GNUNET_SYSERR == 159 ok++;
149 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, 160 continue;
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 }
171 } 161 }
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 }
172 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER, 170 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER,
173 (unsigned long long) 171 (unsigned long long)
174 GNUNET_TIME_absolute_get_duration (start).rel_value, ok); 172 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
175 if (ok == 0) 173 if (ok == 0)
176 return GNUNET_OK; 174 return GNUNET_OK;
177 else 175 else
@@ -194,34 +192,34 @@ testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
194 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)); 192 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
195 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); 193 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
196 for (i = 0; i < ITER; i++) 194 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))
197 { 214 {
198 fprintf (stderr, "."); 215 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
199 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) 216 ok = GNUNET_SYSERR;
200 { 217 continue;
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 }
221 } 218 }
219 }
222 printf ("%d RSA sign/verify operations %llums\n", ITER, 220 printf ("%d RSA sign/verify operations %llums\n", ITER,
223 (unsigned long long) 221 (unsigned long long)
224 GNUNET_TIME_absolute_get_duration (start).rel_value); 222 GNUNET_TIME_absolute_get_duration (start).rel_value);
225 return ok; 223 return ok;
226} 224}
227 225
@@ -239,10 +237,10 @@ main (int argc, char *argv[])
239 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in); 237 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in);
240 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in); 238 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
241 if (hostkey == NULL) 239 if (hostkey == NULL)
242 { 240 {
243 printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n"); 241 printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
244 return 1; 242 return 1;
245 } 243 }
246 if (GNUNET_OK != testMultiKey ("foo")) 244 if (GNUNET_OK != testMultiKey ("foo"))
247 failureCount++; 245 failureCount++;
248 if (GNUNET_OK != testMultiKey ("bar")) 246 if (GNUNET_OK != testMultiKey ("bar"))
@@ -254,9 +252,9 @@ main (int argc, char *argv[])
254 GNUNET_CRYPTO_rsa_key_free (hostkey); 252 GNUNET_CRYPTO_rsa_key_free (hostkey);
255 253
256 if (failureCount != 0) 254 if (failureCount != 0)
257 { 255 {
258 printf ("\n\n%d TESTS FAILED!\n\n", failureCount); 256 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
259 return -1; 257 return -1;
260 } 258 }
261 return 0; 259 return 0;
262} 260}
diff --git a/src/util/test_crypto_random.c b/src/util/test_crypto_random.c
index 3b86ececd..46d3ed0aa 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)))
42 { 45 {
43 b2 = GNUNET_CRYPTO_random_permute (mode, 1024); 46 fprintf (stderr, "!");
44 if (0 == memcmp (b2, buf, sizeof (buf)))
45 {
46 fprintf (stderr, "!");
47 GNUNET_free (b2);
48 continue;
49 }
50 GNUNET_free (b2); 47 GNUNET_free (b2);
51 break; 48 continue;
52 } 49 }
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 6a2c1b355..f26a76a38 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))
57 { 62 {
58 fprintf (stderr, "."); 63 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
59 if (GNUNET_SYSERR == 64 ok++;
60 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, 65 continue;
61 &pkey, &target)) 66 }
62 { 67 if (-1 ==
63 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n"); 68 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
64 ok++; 69 strlen (TESTSTRING) + 1))
65 continue; 70 {
66 } 71 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
67 if (-1 == 72 ok++;
68 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result, 73 continue;
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 }
83 } 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 }
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))
112 { 117 {
113 fprintf (stderr, "."); 118 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
114 if (GNUNET_SYSERR == 119 ok++;
115 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, 120 continue;
116 &pkey, &target))
117 {
118 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
119 ok++;
120 continue;
121 }
122 } 121 }
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,39 +149,37 @@ 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)))
152 { 174 {
153 fprintf (stderr, "."); 175 printf ("testEncryptDecryptSK failed!\n");
154 GNUNET_CRYPTO_aes_create_session_key (&insk); 176 ok++;
155 if (GNUNET_SYSERR == 177 continue;
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 }
181 } 178 }
179 }
182 printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER, 180 printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER,
183 (unsigned long long) 181 (unsigned long long)
184 GNUNET_TIME_absolute_get_duration (start).rel_value, ok); 182 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
185 GNUNET_CRYPTO_rsa_key_free (hostkey); 183 GNUNET_CRYPTO_rsa_key_free (hostkey);
186 if (ok != 0) 184 if (ok != 0)
187 return GNUNET_SYSERR; 185 return GNUNET_SYSERR;
@@ -208,34 +206,34 @@ testSignVerify ()
208 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); 206 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
209 207
210 for (i = 0; i < ITER; i++) 208 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))
211 { 220 {
212 fprintf (stderr, "."); 221 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
213 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) 222 ok = GNUNET_SYSERR;
214 { 223 continue;
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 }
235 } 224 }
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 }
236 printf ("%d RSA sign/verify operations %llums\n", ITER, 234 printf ("%d RSA sign/verify operations %llums\n", ITER,
237 (unsigned long long) 235 (unsigned long long)
238 GNUNET_TIME_absolute_get_duration (start).rel_value); 236 GNUNET_TIME_absolute_get_duration (start).rel_value);
239 GNUNET_CRYPTO_rsa_key_free (hostkey); 237 GNUNET_CRYPTO_rsa_key_free (hostkey);
240 return ok; 238 return ok;
241} 239}
@@ -260,18 +258,18 @@ testSignPerformance ()
260 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); 258 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
261 start = GNUNET_TIME_absolute_get (); 259 start = GNUNET_TIME_absolute_get ();
262 for (i = 0; i < ITER; i++) 260 for (i = 0; i < ITER; i++)
261 {
262 fprintf (stderr, ".");
263 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
263 { 264 {
264 fprintf (stderr, "."); 265 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
265 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) 266 ok = GNUNET_SYSERR;
266 { 267 continue;
267 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
268 ok = GNUNET_SYSERR;
269 continue;
270 }
271 } 268 }
269 }
272 printf ("%d RSA sign operations %llu ms\n", ITER, 270 printf ("%d RSA sign operations %llu ms\n", ITER,
273 (unsigned long long) 271 (unsigned long long)
274 GNUNET_TIME_absolute_get_duration (start).rel_value); 272 GNUNET_TIME_absolute_get_duration (start).rel_value);
275 GNUNET_CRYPTO_rsa_key_free (hostkey); 273 GNUNET_CRYPTO_rsa_key_free (hostkey);
276 return ok; 274 return ok;
277} 275}
@@ -328,9 +326,9 @@ main (int argc, char *argv[])
328 failureCount++; 326 failureCount++;
329 327
330 if (failureCount != 0) 328 if (failureCount != 0)
331 { 329 {
332 printf ("\n\n%d TESTS FAILED!\n\n", failureCount); 330 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
333 return -1; 331 return -1;
334 } 332 }
335 return 0; 333 return 0;
336} /* end of main */ 334} /* end of main */
diff --git a/src/util/test_disk.c b/src/util/test_disk.c
index e8ec88c9f..91154a6f5 100644
--- a/src/util/test_disk.c
+++ b/src/util/test_disk.c
@@ -38,41 +38,40 @@ 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", 49 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
50 ".testfile"); 50 return 1;
51 return 1; 51 }
52 }
53 tmp[ret] = '\0'; 52 tmp[ret] = '\0';
54 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) 53 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
55 { 54 {
56 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", 55 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
57 tmp, TESTSTRING, ".testfile"); 56 TESTSTRING, ".testfile");
58 return 1; 57 return 1;
59 } 58 }
60 GNUNET_DISK_file_copy (".testfile", ".testfile2"); 59 GNUNET_DISK_file_copy (".testfile", ".testfile2");
61 memset (tmp, 0, sizeof (tmp)); 60 memset (tmp, 0, sizeof (tmp));
62 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1); 61 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
63 if (ret < 0) 62 if (ret < 0)
64 { 63 {
65 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", 64 fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
66 ".testfile2"); 65 ".testfile2");
67 return 1; 66 return 1;
68 } 67 }
69 tmp[ret] = '\0'; 68 tmp[ret] = '\0';
70 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) 69 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
71 { 70 {
72 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", 71 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
73 tmp, TESTSTRING, ".testfile2"); 72 TESTSTRING, ".testfile2");
74 return 1; 73 return 1;
75 } 74 }
76 75
77 GNUNET_break (0 == UNLINK (".testfile")); 76 GNUNET_break (0 == UNLINK (".testfile"));
78 GNUNET_break (0 == UNLINK (".testfile2")); 77 GNUNET_break (0 == UNLINK (".testfile2"));
@@ -90,15 +89,15 @@ testOpenClose ()
90 long avail; 89 long avail;
91 90
92 fh = GNUNET_DISK_file_open (".testfile", 91 fh = GNUNET_DISK_file_open (".testfile",
93 GNUNET_DISK_OPEN_READWRITE | 92 GNUNET_DISK_OPEN_READWRITE |
94 GNUNET_DISK_OPEN_CREATE, 93 GNUNET_DISK_OPEN_CREATE,
95 GNUNET_DISK_PERM_USER_READ | 94 GNUNET_DISK_PERM_USER_READ |
96 GNUNET_DISK_PERM_USER_WRITE); 95 GNUNET_DISK_PERM_USER_WRITE);
97 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh)); 96 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
98 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5)); 97 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
99 GNUNET_DISK_file_close (fh); 98 GNUNET_DISK_file_close (fh);
100 GNUNET_break (GNUNET_OK == 99 GNUNET_break (GNUNET_OK ==
101 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO)); 100 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
102 if (size != 5) 101 if (size != 5)
103 return 1; 102 return 1;
104 GNUNET_break (0 == UNLINK (".testfile")); 103 GNUNET_break (0 == UNLINK (".testfile"));
@@ -108,19 +107,19 @@ testOpenClose ()
108 avail = GNUNET_DISK_get_blocks_available (".testfile"); 107 avail = GNUNET_DISK_get_blocks_available (".testfile");
109 GNUNET_log_skip (0, GNUNET_NO); 108 GNUNET_log_skip (0, GNUNET_NO);
110 fh = GNUNET_DISK_file_open (".testfile", 109 fh = GNUNET_DISK_file_open (".testfile",
111 GNUNET_DISK_OPEN_READWRITE | 110 GNUNET_DISK_OPEN_READWRITE |
112 GNUNET_DISK_OPEN_CREATE, 111 GNUNET_DISK_OPEN_CREATE,
113 GNUNET_DISK_PERM_USER_WRITE | 112 GNUNET_DISK_PERM_USER_WRITE |
114 GNUNET_DISK_PERM_USER_READ); 113 GNUNET_DISK_PERM_USER_READ);
115 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh)); 114 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
116 while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) && 115 while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) &&
117 (avail != -1)) 116 (avail != -1))
118 if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16)) 117 if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16))
119 { 118 {
120 GNUNET_DISK_file_close (fh); 119 GNUNET_DISK_file_close (fh);
121 GNUNET_break (0 == UNLINK (".testfile")); 120 GNUNET_break (0 == UNLINK (".testfile"));
122 return 1; 121 return 1;
123 } 122 }
124 GNUNET_DISK_file_close (fh); 123 GNUNET_DISK_file_close (fh);
125 GNUNET_break (0 == UNLINK (".testfile")); 124 GNUNET_break (0 == UNLINK (".testfile"));
126 125
@@ -147,7 +146,7 @@ testDirScan ()
147 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more")) 146 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
148 return 1; 147 return 1;
149 GNUNET_DISK_directory_scan ("test", &scan_callback, 148 GNUNET_DISK_directory_scan ("test", &scan_callback,
150 "test" DIR_SEPARATOR_STR "entry"); 149 "test" DIR_SEPARATOR_STR "entry");
151 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) 150 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
152 return 1; 151 return 1;
153 if (ok < 2) 152 if (ok < 2)
@@ -157,7 +156,7 @@ testDirScan ()
157 156
158static void 157static void
159iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di, 158iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
160 const char *filename, const char *dirname) 159 const char *filename, const char *dirname)
161{ 160{
162 int *i = cls; 161 int *i = cls;
163 162
@@ -169,7 +168,7 @@ static void
169iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 168iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170{ 169{
171 GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 170 GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
172 "test", &iter_callback, cls); 171 "test", &iter_callback, cls);
173} 172}
174 173
175static int 174static int
@@ -203,14 +202,14 @@ testGetHome ()
203 cfg = GNUNET_CONFIGURATION_create (); 202 cfg = GNUNET_CONFIGURATION_create ();
204 GNUNET_assert (cfg != NULL); 203 GNUNET_assert (cfg != NULL);
205 GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME", 204 GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
206 "/tmp/test-gnunet-disk-a/b/c"); 205 "/tmp/test-gnunet-disk-a/b/c");
207 fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL); 206 fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
208 GNUNET_assert (fn != NULL); 207 GNUNET_assert (fn != NULL);
209 GNUNET_CONFIGURATION_destroy (cfg); 208 GNUNET_CONFIGURATION_destroy (cfg);
210 ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn); 209 ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
211 GNUNET_free (fn); 210 GNUNET_free (fn);
212 GNUNET_break (GNUNET_OK == 211 GNUNET_break (GNUNET_OK ==
213 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a")); 212 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a"));
214 return ret; 213 return ret;
215} 214}
216 215
@@ -221,10 +220,10 @@ testCanonicalize ()
221 220
222 GNUNET_DISK_filename_canonicalize (fn); 221 GNUNET_DISK_filename_canonicalize (fn);
223 if (0 != strcmp (fn, "ab____cd_ef__g_")) 222 if (0 != strcmp (fn, "ab____cd_ef__g_"))
224 { 223 {
225 GNUNET_free (fn); 224 GNUNET_free (fn);
226 return 1; 225 return 1;
227 } 226 }
228 GNUNET_free (fn); 227 GNUNET_free (fn);
229 return 0; 228 return 0;
230} 229}
@@ -276,9 +275,9 @@ main (int argc, char *argv[])
276 failureCount += testChangeOwner (); 275 failureCount += testChangeOwner ();
277 failureCount += testDirMani (); 276 failureCount += testDirMani ();
278 if (failureCount != 0) 277 if (failureCount != 0)
279 { 278 {
280 fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount); 279 fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount);
281 return -1; 280 return -1;
282 } 281 }
283 return 0; 282 return 0;
284} /* end of main */ 283} /* end of main */
diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c
index f830454cd..a517887bf 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 28cc9cd3a..315f97d97 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 59f3ee816..94e2719a2 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 918ef1bc5..df8520546 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 5bf5108bc..3817d8243 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,27 +65,25 @@ 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))
68 { 71 {
69 pid = GNUNET_PEER_intern (&pidArr[i]); 72 fprintf (stderr, "Unexpected Peer ID returned by intern function\n");
70 if (pid != (i + 1)) 73 return 1;
71 {
72 fprintf (stderr,
73 "Unexpected Peer ID returned by intern function\n");
74 return 1;
75 }
76 } 74 }
75 }
77 76
78 /* Referencing the first 3 peers once again */ 77 /* Referencing the first 3 peers once again */
79 for (i = 0; i < 3; i++) 78 for (i = 0; i < 3; i++)
79 {
80 pid = GNUNET_PEER_intern (&pidArr[i]);
81 if (pid != (i + 1))
80 { 82 {
81 pid = GNUNET_PEER_intern (&pidArr[i]); 83 fprintf (stderr, "Unexpected Peer ID returned by intern function\n");
82 if (pid != (i + 1)) 84 return 1;
83 {
84 fprintf (stderr,
85 "Unexpected Peer ID returned by intern function\n");
86 return 1;
87 }
88 } 85 }
86 }
89 87
90 /* Dereferencing the first 3 peers once [decrementing their reference count] */ 88 /* Dereferencing the first 3 peers once [decrementing their reference count] */
91 GNUNET_PEER_decrement_rcs (ids, 3); 89 GNUNET_PEER_decrement_rcs (ids, 3);
@@ -130,11 +128,11 @@ main ()
130 128
131 GNUNET_log_setup ("test-peer", "ERROR", NULL); 129 GNUNET_log_setup ("test-peer", "ERROR", NULL);
132 for (i = 0; i < 1; i++) 130 for (i = 0; i < 1; i++)
133 { 131 {
134 generatePeerIdList (); 132 generatePeerIdList ();
135 if (0 != check ()) 133 if (0 != check ())
136 return 1; 134 return 1;
137 } 135 }
138 return 0; 136 return 0;
139} 137}
140 138
diff --git a/src/util/test_plugin.c b/src/util/test_plugin.c
index bfa3cf110..4635e1357 100644
--- a/src/util/test_plugin.c
+++ b/src/util/test_plugin.c
@@ -27,9 +27,7 @@
27#define VERBOSE GNUNET_EXTRA_LOGGING 27#define VERBOSE GNUNET_EXTRA_LOGGING
28 28
29static void 29static void
30test_cb (void *cls, 30test_cb (void *cls, const char *libname, void *lib_ret)
31 const char *libname,
32 void *lib_ret)
33{ 31{
34 void *ret; 32 void *ret;
35 33
@@ -39,7 +37,7 @@ test_cb (void *cls,
39 GNUNET_assert (NULL != ret); 37 GNUNET_assert (NULL != ret);
40 GNUNET_assert (0 == strcmp (ret, "World")); 38 GNUNET_assert (0 == strcmp (ret, "World"));
41} 39}
42 40
43 41
44static int 42static int
45check () 43check ()
diff --git a/src/util/test_program.c b/src/util/test_program.c
index 0ff49b81e..faeb4e716 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 30a04598c..20a3d3d96 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 24c71b41f..4e248aa72 100644
--- a/src/util/test_resolver_api.c
+++ b/src/util/test_resolver_api.c
@@ -46,13 +46,12 @@ check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
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, 53 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"),
54 _("Got IP address `%s' for our host.\n"), GNUNET_a2s (sa, 54 GNUNET_a2s (sa, salen));
55 salen));
56} 55}
57 56
58 57
@@ -64,21 +63,21 @@ check_localhost_num (void *cls, const char *hostname)
64 if (hostname == NULL) 63 if (hostname == NULL)
65 return; 64 return;
66 if (0 == strcmp (hostname, "127.0.0.1")) 65 if (0 == strcmp (hostname, "127.0.0.1"))
67 { 66 {
68#if DEBUG_RESOLVER 67#if DEBUG_RESOLVER
69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n",
70 "Received correct hostname `%s'.\n", hostname); 69 hostname);
71#endif 70#endif
72 (*ok) &= ~4; 71 (*ok) &= ~4;
73 } 72 }
74 else 73 else
75 { 74 {
76#if DEBUG_RESOLVER 75#if DEBUG_RESOLVER
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received invalid hostname `%s'.\n",
78 "Received invalid hostname `%s'.\n", hostname); 77 hostname);
79#endif 78#endif
80 GNUNET_break (0); 79 GNUNET_break (0);
81 } 80 }
82} 81}
83 82
84 83
@@ -90,19 +89,19 @@ check_localhost (void *cls, const char *hostname)
90 if (hostname == NULL) 89 if (hostname == NULL)
91 return; 90 return;
92 if (0 == strcmp (hostname, "localhost")) 91 if (0 == strcmp (hostname, "localhost"))
93 { 92 {
94#if DEBUG_RESOLVER 93#if DEBUG_RESOLVER
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n",
96 "Received correct hostname `%s'.\n", hostname); 95 hostname);
97#endif 96#endif
98 (*ok) &= ~2; 97 (*ok) &= ~2;
99 } 98 }
100 else 99 else
101 { 100 {
102 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
103 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n", 102 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
104 hostname); 103 hostname);
105 } 104 }
106} 105}
107 106
108static void 107static void
@@ -115,19 +114,19 @@ check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
115 return; 114 return;
116 GNUNET_assert (sizeof (struct sockaddr_in) == salen); 115 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
117 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK)) 116 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
118 { 117 {
119#if DEBUG_RESOLVER 118#if DEBUG_RESOLVER
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n"); 119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
121#endif 120#endif
122 (*ok) &= ~1; 121 (*ok) &= ~1;
123 } 122 }
124 else 123 else
125 { 124 {
126#if DEBUG_RESOLVER 125#if DEBUG_RESOLVER
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n"); 126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
128#endif 127#endif
129 GNUNET_break (0); 128 GNUNET_break (0);
130 } 129 }
131} 130}
132 131
133static void 132static void
@@ -139,32 +138,32 @@ check_local_fqdn (void *cls, const char *gnunet_fqdn)
139 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 138 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
140 139
141 if (0 != gethostname (hostname, sizeof (hostname) - 1)) 140 if (0 != gethostname (hostname, sizeof (hostname) - 1))
142 { 141 {
143 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 142 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
144 "gethostname"); 143 "gethostname");
145 return; 144 return;
146 } 145 }
147#if DEBUG_RESOLVER 146#if DEBUG_RESOLVER
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), 147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"),
149 hostname); 148 hostname);
150#endif 149#endif
151 host = gethostbyname (hostname); 150 host = gethostbyname (hostname);
152 if (NULL == host) 151 if (NULL == host)
153 { 152 {
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 _("Could not resolve our FQDN : %s %u\n"), 154 _("Could not resolve our FQDN : %s %u\n"), hstrerror (h_errno),
156 hstrerror (h_errno), h_errno); 155 h_errno);
157 return; 156 return;
158 } 157 }
159 158
160 GNUNET_assert (0 != host); 159 GNUNET_assert (0 != host);
161 160
162 result = strcmp (host->h_name, gnunet_fqdn); 161 result = strcmp (host->h_name, gnunet_fqdn);
163 if (0 != result) 162 if (0 != result)
164 { 163 {
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 "Local resolved and resolver resolved fqdns are not equal\n"); 165 "Local resolved and resolver resolved fqdns are not equal\n");
167 } 166 }
168 GNUNET_assert (0 == result); 167 GNUNET_assert (0 == result);
169} 168}
170 169
@@ -181,21 +180,21 @@ check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
181 GNUNET_assert (sizeof (struct sockaddr_in) == salen); 180 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
182 181
183 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP)) 182 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
184 { 183 {
185#if DEBUG_RESOLVER 184#if DEBUG_RESOLVER
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Received correct rootserver ip address.\n"); 186 "Received correct rootserver ip address.\n");
188#endif 187#endif
189 (*ok) &= ~1; 188 (*ok) &= ~1;
190 } 189 }
191 else 190 else
192 { 191 {
193#if DEBUG_RESOLVER 192#if DEBUG_RESOLVER
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Received incorrect rootserver ip address.\n"); 194 "Received incorrect rootserver ip address.\n");
196#endif 195#endif
197 GNUNET_break (0); 196 GNUNET_break (0);
198 } 197 }
199} 198}
200 199
201static void 200static void
@@ -207,21 +206,21 @@ check_rootserver_name (void *cls, const char *hostname)
207 return; 206 return;
208 207
209 if (0 == strcmp (hostname, ROOTSERVER_NAME)) 208 if (0 == strcmp (hostname, ROOTSERVER_NAME))
210 { 209 {
211#if DEBUG_RESOLVER 210#if DEBUG_RESOLVER
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Received correct rootserver hostname `%s'.\n", hostname); 212 "Received correct rootserver hostname `%s'.\n", hostname);
214#endif 213#endif
215 (*ok) &= ~2; 214 (*ok) &= ~2;
216 } 215 }
217 else 216 else
218 { 217 {
219#if DEBUG_RESOLVER 218#if DEBUG_RESOLVER
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Received invalid rootserver hostname `%s'.\n", hostname); 220 "Received invalid rootserver hostname `%s'.\n", hostname);
222#endif 221#endif
223 GNUNET_break (0); 222 GNUNET_break (0);
224 } 223 }
225} 224}
226 225
227static void 226static void
@@ -231,7 +230,7 @@ run (void *cls, char *const *args, const char *cfgfile,
231 int *ok = cls; 230 int *ok = cls;
232 struct sockaddr_in sa; 231 struct sockaddr_in sa;
233 struct GNUNET_TIME_Relative timeout = 232 struct GNUNET_TIME_Relative timeout =
234 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30); 233 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
235 int count_ips = 0; 234 int count_ips = 0;
236 char *own_fqdn; 235 char *own_fqdn;
237 236
@@ -258,50 +257,50 @@ run (void *cls, char *const *args, const char *cfgfile,
258 257
259 rootserver = gethostbyname (rootserver_name); 258 rootserver = gethostbyname (rootserver_name);
260 if (rootserver == NULL) 259 if (rootserver == NULL)
261 { 260 {
262 /* Error: resolving ip addresses does not work */ 261 /* Error: resolving ip addresses does not work */
263#if DEBUG_RESOLVER 262#if DEBUG_RESOLVER
264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
265 _("gethostbyname() could not lookup IP address: %s\n"), 264 _("gethostbyname() could not lookup IP address: %s\n"),
266 hstrerror (h_errno)); 265 hstrerror (h_errno));
267#endif 266#endif
268 fprintf (stderr, 267 fprintf (stderr,
269 "System seems to be off-line, will not run all DNS tests\n"); 268 "System seems to be off-line, will not run all DNS tests\n");
270 *ok = 0; /* mark test as passing anyway */ 269 *ok = 0; /* mark test as passing anyway */
271 return; 270 return;
272 } 271 }
273 272
274 /* Counting returned IP addresses */ 273 /* Counting returned IP addresses */
275 while (rootserver->h_addr_list[count_ips] != NULL) 274 while (rootserver->h_addr_list[count_ips] != NULL)
276 count_ips++; 275 count_ips++;
277 if (count_ips > 1) 276 if (count_ips > 1)
278 { 277 {
279#if DEBUG_RESOLVER 278#if DEBUG_RESOLVER
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "IP received range for root name server, but a root name server has only 1 IP\n"); 280 "IP received range for root name server, but a root name server has only 1 IP\n");
282#endif 281#endif
283 GNUNET_break (0); 282 GNUNET_break (0);
284 } 283 }
285 284
286 /* Comparing to resolved address to the address the root name server should have */ 285 /* Comparing to resolved address to the address the root name server should have */
287 if (strcmp 286 if (strcmp
288 (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]), 287 (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
289 ROOTSERVER_IP) != 0) 288 ROOTSERVER_IP) != 0)
290 { 289 {
291#if DEBUG_RESOLVER 290#if DEBUG_RESOLVER
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "IP received and IP for root name server differ\n"); 292 "IP received and IP for root name server differ\n");
294#endif 293#endif
295 GNUNET_break (0); 294 GNUNET_break (0);
296 } 295 }
297#if DEBUG_RESOLVER 296#if DEBUG_RESOLVER
298 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 297 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
299 "System's own forward name resolution is working\n"); 298 "System's own forward name resolution is working\n");
300#endif 299#endif
301 300
302 /* Resolve the same using GNUNET */ 301 /* Resolve the same using GNUNET */
303 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout, 302 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
304 &check_rootserver_ip, cls); 303 &check_rootserver_ip, cls);
305 304
306 /* 305 /*
307 * Success: forward lookups work as expected 306 * Success: forward lookups work as expected
@@ -312,41 +311,41 @@ run (void *cls, char *const *args, const char *cfgfile,
312 311
313 rootserver->h_name = ""; 312 rootserver->h_name = "";
314 if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr)) 313 if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr))
315 { 314 {
316#if DEBUG_RESOLVER 315#if DEBUG_RESOLVER
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Could not transform root name server IP address\n"); 317 "Could not transform root name server IP address\n");
319#endif 318#endif
320 GNUNET_break (0); 319 GNUNET_break (0);
321 } 320 }
322 321
323 rootserver = 322 rootserver =
324 gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET); 323 gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET);
325 if (rootserver == NULL) 324 if (rootserver == NULL)
326 { 325 {
327 /* Error: resolving IP addresses does not work */ 326 /* Error: resolving IP addresses does not work */
328#if DEBUG_RESOLVER 327#if DEBUG_RESOLVER
329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
330 _("gethostbyaddr() could not lookup hostname: %s\n"), 329 _("gethostbyaddr() could not lookup hostname: %s\n"),
331 hstrerror (h_errno)); 330 hstrerror (h_errno));
332#endif 331#endif
333 GNUNET_break (0); 332 GNUNET_break (0);
334 } 333 }
335 else 334 else
335 {
336 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
336 { 337 {
337 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
338 {
339#if DEBUG_RESOLVER 338#if DEBUG_RESOLVER
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Received hostname and hostname for root name server differ\n"); 340 "Received hostname and hostname for root name server differ\n");
342#endif 341#endif
343 GNUNET_break (0); 342 GNUNET_break (0);
344 }
345 } 343 }
344 }
346 345
347#if DEBUG_RESOLVER 346#if DEBUG_RESOLVER
348 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
349 "System's own reverse name resolution is working\n"); 348 "System's own reverse name resolution is working\n");
350#endif 349#endif
351 350
352 /* Resolve the same using GNUNET */ 351 /* Resolve the same using GNUNET */
@@ -361,8 +360,8 @@ run (void *cls, char *const *args, const char *cfgfile,
361 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP); 360 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
362#endif 361#endif
363 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, 362 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
364 sizeof (struct sockaddr), GNUNET_YES, timeout, 363 sizeof (struct sockaddr), GNUNET_YES, timeout,
365 &check_rootserver_name, cls); 364 &check_rootserver_name, cls);
366 365
367 memset (&sa, 0, sizeof (sa)); 366 memset (&sa, 0, sizeof (sa));
368 sa.sin_family = AF_INET; 367 sa.sin_family = AF_INET;
@@ -373,12 +372,12 @@ run (void *cls, char *const *args, const char *cfgfile,
373 372
374 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls); 373 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
375 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, 374 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
376 sizeof (struct sockaddr), GNUNET_YES, timeout, 375 sizeof (struct sockaddr), GNUNET_YES, timeout,
377 &check_localhost, cls); 376 &check_localhost, cls);
378 377
379 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa, 378 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
380 sizeof (struct sockaddr), GNUNET_NO, timeout, 379 sizeof (struct sockaddr), GNUNET_NO, timeout,
381 &check_localhost_num, cls); 380 &check_localhost_num, cls);
382 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls); 381 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
383 382
384} 383}
@@ -392,33 +391,33 @@ check ()
392 struct GNUNET_OS_Process *proc; 391 struct GNUNET_OS_Process *proc;
393 392
394 char *const argv[] = 393 char *const argv[] =
395 { "test-resolver-api", "-c", "test_resolver_api_data.conf", 394 { "test-resolver-api", "-c", "test_resolver_api_data.conf",
396#if VERBOSE 395#if VERBOSE
397 "-L", "DEBUG", 396 "-L", "DEBUG",
398#endif 397#endif
399 NULL 398 NULL
400 }; 399 };
401 struct GNUNET_GETOPT_CommandLineOption options[] = 400 struct GNUNET_GETOPT_CommandLineOption options[] =
402 { GNUNET_GETOPT_OPTION_END }; 401 { GNUNET_GETOPT_OPTION_END };
403 pfx = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR); 402 pfx = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
404 GNUNET_asprintf (&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR); 403 GNUNET_asprintf (&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
405 GNUNET_free (pfx); 404 GNUNET_free (pfx);
406 proc = GNUNET_OS_start_process (NULL, NULL, fn, "gnunet-service-resolver", 405 proc = GNUNET_OS_start_process (NULL, NULL, fn, "gnunet-service-resolver",
407#if VERBOSE 406#if VERBOSE
408 "-L", "DEBUG", 407 "-L", "DEBUG",
409#endif 408#endif
410 "-c", "test_resolver_api_data.conf", NULL); 409 "-c", "test_resolver_api_data.conf", NULL);
411 GNUNET_assert (NULL != proc); 410 GNUNET_assert (NULL != proc);
412 GNUNET_free (fn); 411 GNUNET_free (fn);
413 GNUNET_assert (GNUNET_OK == 412 GNUNET_assert (GNUNET_OK ==
414 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 413 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
415 argv, "test-resolver-api", "nohelp", 414 argv, "test-resolver-api", "nohelp",
416 options, &run, &ok)); 415 options, &run, &ok));
417 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 416 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
418 { 417 {
419 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 418 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
420 ok = 1; 419 ok = 1;
421 } 420 }
422 GNUNET_OS_process_wait (proc); 421 GNUNET_OS_process_wait (proc);
423 GNUNET_OS_process_close (proc); 422 GNUNET_OS_process_close (proc);
424 proc = NULL; 423 proc = NULL;
@@ -434,11 +433,11 @@ main (int argc, char *argv[])
434 433
435 GNUNET_log_setup ("test-resolver-api", 434 GNUNET_log_setup ("test-resolver-api",
436#if VERBOSE 435#if VERBOSE
437 "DEBUG", 436 "DEBUG",
438#else 437#else
439 "WARNING", 438 "WARNING",
440#endif 439#endif
441 NULL); 440 NULL);
442 ret = check (); 441 ret = check ();
443 442
444 return ret; 443 return ret;
diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c
index e2a28c1b7..788ba13fa 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, 109 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast,
110 &taskLast, cls); 110 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], 126 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], &taskRd,
127 &taskRd, cls); 127 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 20783da8d..ff6966a87 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), 69 (GNUNET_TIME_UNIT_MILLISECONDS, i), &test_task,
70 &test_task, NULL); 70 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 8adde5346..625829042 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,20 +98,19 @@ 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) == 107 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
108 ntohs (message->size));
109 GNUNET_assert (MY_TYPE == ntohs (message->type)); 108 GNUNET_assert (MY_TYPE == ntohs (message->type));
110 GNUNET_assert (NULL != 109 GNUNET_assert (NULL !=
111 GNUNET_SERVER_notify_transmit_ready (client, 110 GNUNET_SERVER_notify_transmit_ready (client,
112 ntohs (message->size), 111 ntohs (message->size),
113 TIMEOUT, &reply_msg, 112 TIMEOUT, &reply_msg,
114 NULL)); 113 NULL));
115} 114}
116 115
117 116
@@ -139,7 +138,7 @@ static size_t
139transmit_initial_message (void *cls, size_t size, void *buf) 138transmit_initial_message (void *cls, size_t size, void *buf)
140{ 139{
141 struct GNUNET_MessageHeader msg; 140 struct GNUNET_MessageHeader msg;
142 141
143 GNUNET_assert (ok == 1); 142 GNUNET_assert (ok == 1);
144 ok = 2; 143 ok = 2;
145 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); 144 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
@@ -147,12 +146,12 @@ transmit_initial_message (void *cls, size_t size, void *buf)
147 msg.size = htons (sizeof (struct GNUNET_MessageHeader)); 146 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
148 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader)); 147 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
149 GNUNET_assert (NULL != 148 GNUNET_assert (NULL !=
150 GNUNET_CLIENT_notify_transmit_ready (cc, 149 GNUNET_CLIENT_notify_transmit_ready (cc,
151 sizeof (struct 150 sizeof (struct
152 GNUNET_MessageHeader), 151 GNUNET_MessageHeader),
153 TIMEOUT, GNUNET_YES, 152 TIMEOUT, GNUNET_YES,
154 &transmit_second_message, 153 &transmit_second_message,
155 NULL)); 154 NULL));
156 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT); 155 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
157 return sizeof (struct GNUNET_MessageHeader); 156 return sizeof (struct GNUNET_MessageHeader);
158} 157}
@@ -181,18 +180,18 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 cfg = GNUNET_CONFIGURATION_create (); 180 cfg = GNUNET_CONFIGURATION_create ();
182 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT); 181 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
183 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", 182 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
184 "localhost"); 183 "localhost");
185 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 184 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
186 "localhost"); 185 "localhost");
187 cc = GNUNET_CLIENT_connect ("test-server", cfg); 186 cc = GNUNET_CLIENT_connect ("test-server", cfg);
188 GNUNET_assert (cc != NULL); 187 GNUNET_assert (cc != NULL);
189 GNUNET_assert (NULL != 188 GNUNET_assert (NULL !=
190 GNUNET_CLIENT_notify_transmit_ready (cc, 189 GNUNET_CLIENT_notify_transmit_ready (cc,
191 sizeof (struct 190 sizeof (struct
192 GNUNET_MessageHeader), 191 GNUNET_MessageHeader),
193 TIMEOUT, GNUNET_YES, 192 TIMEOUT, GNUNET_YES,
194 &transmit_initial_message, 193 &transmit_initial_message,
195 NULL)); 194 NULL));
196} 195}
197 196
198 197
diff --git a/src/util/test_server_disconnect.c b/src/util/test_server_disconnect.c
index e38840d92..69207f81a 100644
--- a/src/util/test_server_disconnect.c
+++ b/src/util/test_server_disconnect.c
@@ -81,14 +81,13 @@ server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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) == 90 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
91 ntohs (message->size));
92 GNUNET_assert (MY_TYPE == ntohs (message->type)); 91 GNUNET_assert (MY_TYPE == ntohs (message->type));
93 GNUNET_SERVER_receive_done (client, GNUNET_OK); 92 GNUNET_SERVER_receive_done (client, GNUNET_OK);
94} 93}
@@ -139,18 +138,18 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 cfg = GNUNET_CONFIGURATION_create (); 138 cfg = GNUNET_CONFIGURATION_create ();
140 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT); 139 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
141 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", 140 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
142 "localhost"); 141 "localhost");
143 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 142 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
144 "localhost"); 143 "localhost");
145 cc = GNUNET_CLIENT_connect ("test-server", cfg); 144 cc = GNUNET_CLIENT_connect ("test-server", cfg);
146 GNUNET_assert (cc != NULL); 145 GNUNET_assert (cc != NULL);
147 GNUNET_assert (NULL != 146 GNUNET_assert (NULL !=
148 GNUNET_CLIENT_notify_transmit_ready (cc, 147 GNUNET_CLIENT_notify_transmit_ready (cc,
149 sizeof (struct 148 sizeof (struct
150 GNUNET_MessageHeader), 149 GNUNET_MessageHeader),
151 TIMEOUT, GNUNET_YES, 150 TIMEOUT, GNUNET_YES,
152 &transmit_initial_message, 151 &transmit_initial_message,
153 NULL)); 152 NULL));
154} 153}
155 154
156 155
diff --git a/src/util/test_server_with_client.c b/src/util/test_server_with_client.c
index 21c706b07..5b6e51718 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,8 +66,7 @@ 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, 69 GNUNET_SERVER_client_get_address (argclient, &addr, &addrlen));
70 &addrlen));
71 70
72 GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); 71 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
73 have = addr; 72 have = addr;
@@ -81,21 +80,21 @@ recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
81 GNUNET_assert (0 == memcmp (&sa, addr, addrlen)); 80 GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
82 GNUNET_free (addr); 81 GNUNET_free (addr);
83 switch (ok) 82 switch (ok)
84 { 83 {
85 case 2: 84 case 2:
86 ok++; 85 ok++;
87 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 86 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
88 (GNUNET_TIME_UNIT_MILLISECONDS, 50), 87 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
89 &send_done, argclient); 88 &send_done, argclient);
90 break; 89 break;
91 case 4: 90 case 4:
92 ok++; 91 ok++;
93 GNUNET_CLIENT_disconnect (client, GNUNET_YES); 92 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
94 GNUNET_SERVER_receive_done (argclient, GNUNET_OK); 93 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
95 break; 94 break;
96 default: 95 default:
97 GNUNET_assert (0); 96 GNUNET_assert (0);
98 } 97 }
99 98
100} 99}
101 100
@@ -170,25 +169,24 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 sa.sin_family = AF_INET; 169 sa.sin_family = AF_INET;
171 sa.sin_port = htons (PORT); 170 sa.sin_port = htons (PORT);
172 server = 171 server =
173 GNUNET_SERVER_create (NULL, NULL, sap, slens, 172 GNUNET_SERVER_create (NULL, NULL, sap, slens,
174 GNUNET_TIME_relative_multiply 173 GNUNET_TIME_relative_multiply
175 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO); 174 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
176 GNUNET_assert (server != NULL); 175 GNUNET_assert (server != NULL);
177 handlers[0].callback_cls = cls; 176 handlers[0].callback_cls = cls;
178 GNUNET_SERVER_add_handlers (server, handlers); 177 GNUNET_SERVER_add_handlers (server, handlers);
179 GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls); 178 GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
180 cfg = GNUNET_CONFIGURATION_create (); 179 cfg = GNUNET_CONFIGURATION_create ();
181 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT); 180 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
182 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", 181 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
183 "localhost");
184 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 182 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
185 "localhost"); 183 "localhost");
186 client = GNUNET_CLIENT_connect ("test", cfg); 184 client = GNUNET_CLIENT_connect ("test", cfg);
187 GNUNET_assert (client != NULL); 185 GNUNET_assert (client != NULL);
188 GNUNET_CLIENT_notify_transmit_ready (client, 256, 186 GNUNET_CLIENT_notify_transmit_ready (client, 256,
189 GNUNET_TIME_relative_multiply 187 GNUNET_TIME_relative_multiply
190 (GNUNET_TIME_UNIT_MILLISECONDS, 250), 188 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
191 GNUNET_NO, &notify_ready, NULL); 189 GNUNET_NO, &notify_ready, NULL);
192} 190}
193 191
194 192
@@ -213,11 +211,11 @@ main (int argc, char *argv[])
213 211
214 GNUNET_log_setup ("test_server_with_client", 212 GNUNET_log_setup ("test_server_with_client",
215#if VERBOSE 213#if VERBOSE
216 "DEBUG", 214 "DEBUG",
217#else 215#else
218 "WARNING", 216 "WARNING",
219#endif 217#endif
220 NULL); 218 NULL);
221 ret += check (); 219 ret += check ();
222 220
223 return ret; 221 return ret;
diff --git a/src/util/test_server_with_client_unix.c b/src/util/test_server_with_client_unix.c
index 11f731243..4f80f188c 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 33d69db10..1b78932f8 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,9 +136,8 @@ 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, 140 GNUNET_SERVICE_OPTION_NONE, &runner, &ok));
141 &ok));
142 GNUNET_assert (0 == ok); 141 GNUNET_assert (0 == ok);
143 return ok; 142 return ok;
144} 143}
@@ -155,19 +154,19 @@ ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 GNUNET_assert (client != NULL); 154 GNUNET_assert (client != NULL);
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n"); 155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
157 GNUNET_CLIENT_notify_transmit_ready (client, 156 GNUNET_CLIENT_notify_transmit_ready (client,
158 sizeof (struct GNUNET_MessageHeader), 157 sizeof (struct GNUNET_MessageHeader),
159 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, 158 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
160 &build_msg, client); 159 &build_msg, client);
161} 160}
162 161
163static void 162static void
164runner6 (void *cls, struct GNUNET_SERVER_Handle *server, 163runner6 (void *cls, struct GNUNET_SERVER_Handle *server,
165 const struct GNUNET_CONFIGURATION_Handle *cfg) 164 const struct GNUNET_CONFIGURATION_Handle *cfg)
166{ 165{
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n"); 166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
168 GNUNET_SERVER_add_handlers (server, myhandlers); 167 GNUNET_SERVER_add_handlers (server, myhandlers);
169 GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS, 168 GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS,
170 &ready6, (void *) cfg); 169 &ready6, (void *) cfg);
171} 170}
172 171
173/** 172/**
@@ -191,9 +190,9 @@ check6 ()
191 }; 190 };
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n"); 191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
193 GNUNET_assert (GNUNET_OK == 192 GNUNET_assert (GNUNET_OK ==
194 GNUNET_SERVICE_run (5, argv, "test_service6", 193 GNUNET_SERVICE_run (5, argv, "test_service6",
195 GNUNET_SERVICE_OPTION_NONE, &runner6, 194 GNUNET_SERVICE_OPTION_NONE, &runner6,
196 &ok)); 195 &ok));
197 GNUNET_assert (0 == ok); 196 GNUNET_assert (0 == ok);
198 return ok; 197 return ok;
199} 198}
@@ -202,12 +201,11 @@ check6 ()
202 201
203static void 202static void
204start_stop_main (void *cls, char *const *args, const char *cfgfile, 203start_stop_main (void *cls, char *const *args, const char *cfgfile,
205 const struct GNUNET_CONFIGURATION_Handle *cfg) 204 const struct GNUNET_CONFIGURATION_Handle *cfg)
206{ 205{
207 int *ret = cls; 206 int *ret = cls;
208 207
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service using start method\n");
210 "Starting service using start method\n");
211 sctx = GNUNET_SERVICE_start ("test_service", cfg); 209 sctx = GNUNET_SERVICE_start ("test_service", cfg);
212 GNUNET_assert (NULL != sctx); 210 GNUNET_assert (NULL != sctx);
213 runner (cls, GNUNET_SERVICE_get_server (sctx), cfg); 211 runner (cls, GNUNET_SERVICE_get_server (sctx), cfg);
@@ -236,9 +234,8 @@ check_start_stop ()
236 int ret = 1; 234 int ret = 1;
237 235
238 GNUNET_assert (GNUNET_OK == 236 GNUNET_assert (GNUNET_OK ==
239 GNUNET_PROGRAM_run (5, argv, "test-service-program", 237 GNUNET_PROGRAM_run (5, argv, "test-service-program", "no help",
240 "no help", options, &start_stop_main, 238 options, &start_stop_main, &ret));
241 &ret));
242 239
243 GNUNET_break (0 == ret); 240 GNUNET_break (0 == ret);
244 return ret; 241 return ret;
@@ -253,11 +250,11 @@ main (int argc, char *argv[])
253 250
254 GNUNET_log_setup ("test-service", 251 GNUNET_log_setup ("test-service",
255#if VERBOSE 252#if VERBOSE
256 "DEBUG", 253 "DEBUG",
257#else 254#else
258 "WARNING", 255 "WARNING",
259#endif 256#endif
260 NULL); 257 NULL);
261 ret += check (); 258 ret += check ();
262 ret += check (); 259 ret += check ();
263 260
@@ -266,22 +263,22 @@ main (int argc, char *argv[])
266 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); 263 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
267#endif 264#endif
268 if (NULL == s) 265 if (NULL == s)
266 {
267 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
268 (errno == EACCES))
269 { 269 {
270 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || 270 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
271 (errno == EACCES)) 271 return 1;
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));
279 } 272 }
273 fprintf (stderr,
274 "IPv6 support seems to not be available (%s), not testing it!\n",
275 strerror (errno));
276 }
280 else 277 else
281 { 278 {
282 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); 279 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
283 ret += check6 (); 280 ret += check6 ();
284 } 281 }
285 ret += check_start_stop (); 282 ret += check_start_stop ();
286 283
287 return ret; 284 return ret;
diff --git a/src/util/test_strings.c b/src/util/test_strings.c
index bb0e6088f..c6d604144 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 80109615a..c5232ff2a 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,24 +152,23 @@ 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 == 163 GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value);
164 (GNUNET_TIME_absolute_subtract (now, rel)).abs_value);
165 164
166 /*check relative divide */ 165 /*check relative divide */
167 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == 166 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
168 (GNUNET_TIME_relative_divide (rel, 0)).rel_value); 167 (GNUNET_TIME_relative_divide (rel, 0)).rel_value);
169 168
170 rel = GNUNET_TIME_UNIT_FOREVER_REL; 169 rel = GNUNET_TIME_UNIT_FOREVER_REL;
171 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == 170 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
172 (GNUNET_TIME_relative_divide (rel, 2)).rel_value); 171 (GNUNET_TIME_relative_divide (rel, 2)).rel_value);
173 172
174 rel = GNUNET_TIME_relative_divide (relUnit, 2); 173 rel = GNUNET_TIME_relative_divide (relUnit, 2);
175 GNUNET_assert (rel.rel_value == relUnit.rel_value / 2); 174 GNUNET_assert (rel.rel_value == relUnit.rel_value / 2);
@@ -183,51 +182,50 @@ check ()
183 forever = GNUNET_TIME_absolute_get_forever (); 182 forever = GNUNET_TIME_absolute_get_forever ();
184 forever.abs_value = forever.abs_value - 1024; 183 forever.abs_value = forever.abs_value - 1024;
185 GNUNET_assert (GNUNET_TIME_absolute_get_zero ().abs_value == 184 GNUNET_assert (GNUNET_TIME_absolute_get_zero ().abs_value ==
186 GNUNET_TIME_calculate_eta (forever, 50000, 185 GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value);
187 100000).rel_value);
188 /* check zero */ 186 /* check zero */
189 GNUNET_log_skip (1, GNUNET_NO); 187 GNUNET_log_skip (1, GNUNET_NO);
190 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value == 188 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value ==
191 (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value); 189 (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value);
192 GNUNET_log_skip (0, GNUNET_YES); 190 GNUNET_log_skip (0, GNUNET_YES);
193 /*check forever */ 191 /*check forever */
194 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == 192 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
195 (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value); 193 (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value);
196 194
197 /*check relative subtract */ 195 /*check relative subtract */
198 now = GNUNET_TIME_absolute_get (); 196 now = GNUNET_TIME_absolute_get ();
199 rel.rel_value = now.abs_value; 197 rel.rel_value = now.abs_value;
200 relForever.rel_value = rel.rel_value + 1024; 198 relForever.rel_value = rel.rel_value + 1024;
201 GNUNET_assert (1024 == 199 GNUNET_assert (1024 ==
202 GNUNET_TIME_relative_subtract (relForever, rel).rel_value); 200 GNUNET_TIME_relative_subtract (relForever, rel).rel_value);
203 /*check zero */ 201 /*check zero */
204 GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value == 202 GNUNET_assert (GNUNET_TIME_relative_get_zero ().rel_value ==
205 GNUNET_TIME_relative_subtract (rel, relForever).rel_value); 203 GNUNET_TIME_relative_subtract (rel, relForever).rel_value);
206 /*check forever */ 204 /*check forever */
207 rel.rel_value = UINT64_MAX; 205 rel.rel_value = UINT64_MAX;
208 GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value == 206 GNUNET_assert (GNUNET_TIME_relative_get_forever ().rel_value ==
209 GNUNET_TIME_relative_subtract (rel, relForever).rel_value); 207 GNUNET_TIME_relative_subtract (rel, relForever).rel_value);
210 208
211 /*check GNUNET_TIME_relative_min */ 209 /*check GNUNET_TIME_relative_min */
212 now = GNUNET_TIME_absolute_get (); 210 now = GNUNET_TIME_absolute_get ();
213 rel.rel_value = now.abs_value; 211 rel.rel_value = now.abs_value;
214 relForever.rel_value = rel.rel_value - 1024; 212 relForever.rel_value = rel.rel_value - 1024;
215 GNUNET_assert (relForever.rel_value == 213 GNUNET_assert (relForever.rel_value ==
216 GNUNET_TIME_relative_min (rel, relForever).rel_value); 214 GNUNET_TIME_relative_min (rel, relForever).rel_value);
217 215
218 /*check GNUNET_TIME_relative_max */ 216 /*check GNUNET_TIME_relative_max */
219 GNUNET_assert (rel.rel_value == 217 GNUNET_assert (rel.rel_value ==
220 GNUNET_TIME_relative_max (rel, relForever).rel_value); 218 GNUNET_TIME_relative_max (rel, relForever).rel_value);
221 219
222 /*check GNUNET_TIME_absolute_min */ 220 /*check GNUNET_TIME_absolute_min */
223 now = GNUNET_TIME_absolute_get (); 221 now = GNUNET_TIME_absolute_get ();
224 last.abs_value = now.abs_value - 1024; 222 last.abs_value = now.abs_value - 1024;
225 GNUNET_assert (last.abs_value == 223 GNUNET_assert (last.abs_value ==
226 GNUNET_TIME_absolute_min (now, last).abs_value); 224 GNUNET_TIME_absolute_min (now, last).abs_value);
227 225
228 /*check GNUNET_TIME_absolute_max */ 226 /*check GNUNET_TIME_absolute_max */
229 GNUNET_assert (now.abs_value == 227 GNUNET_assert (now.abs_value ==
230 GNUNET_TIME_absolute_max (now, last).abs_value); 228 GNUNET_TIME_absolute_max (now, last).abs_value);
231 229
232 return 0; 230 return 0;
233} 231}
diff --git a/src/util/time.c b/src/util/time.c
index e48377fa3..3ee15516a 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -55,8 +55,8 @@ GNUNET_TIME_absolute_get ()
55 55
56 GETTIMEOFDAY (&tv, NULL); 56 GETTIMEOFDAY (&tv, NULL);
57 ret.abs_value = 57 ret.abs_value =
58 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) + 58 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) +
59 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset; 59 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset;
60 return ret; 60 return ret;
61} 61}
62 62
@@ -130,10 +130,10 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
130 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 130 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
131 131
132 if (rel.rel_value + now.abs_value < rel.rel_value) 132 if (rel.rel_value + now.abs_value < rel.rel_value)
133 { 133 {
134 GNUNET_break (0); /* overflow... */ 134 GNUNET_break (0); /* overflow... */
135 return GNUNET_TIME_absolute_get_forever (); 135 return GNUNET_TIME_absolute_get_forever ();
136 } 136 }
137 ret.abs_value = rel.rel_value + now.abs_value; 137 ret.abs_value = rel.rel_value + now.abs_value;
138 return ret; 138 return ret;
139} 139}
@@ -148,7 +148,7 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
148 */ 148 */
149struct GNUNET_TIME_Relative 149struct GNUNET_TIME_Relative
150GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1, 150GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
151 struct GNUNET_TIME_Relative t2) 151 struct GNUNET_TIME_Relative t2)
152{ 152{
153 return (t1.rel_value < t2.rel_value) ? t1 : t2; 153 return (t1.rel_value < t2.rel_value) ? t1 : t2;
154} 154}
@@ -163,7 +163,7 @@ GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
163 */ 163 */
164struct GNUNET_TIME_Relative 164struct GNUNET_TIME_Relative
165GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1, 165GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
166 struct GNUNET_TIME_Relative t2) 166 struct GNUNET_TIME_Relative t2)
167{ 167{
168 return (t1.rel_value > t2.rel_value) ? t1 : t2; 168 return (t1.rel_value > t2.rel_value) ? t1 : t2;
169} 169}
@@ -179,7 +179,7 @@ GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
179 */ 179 */
180struct GNUNET_TIME_Absolute 180struct GNUNET_TIME_Absolute
181GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1, 181GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
182 struct GNUNET_TIME_Absolute t2) 182 struct GNUNET_TIME_Absolute t2)
183{ 183{
184 return (t1.abs_value < t2.abs_value) ? t1 : t2; 184 return (t1.abs_value < t2.abs_value) ? t1 : t2;
185} 185}
@@ -194,7 +194,7 @@ GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
194 */ 194 */
195struct GNUNET_TIME_Absolute 195struct GNUNET_TIME_Absolute
196GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1, 196GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
197 struct GNUNET_TIME_Absolute t2) 197 struct GNUNET_TIME_Absolute t2)
198{ 198{
199 return (t1.abs_value > t2.abs_value) ? t1 : t2; 199 return (t1.abs_value > t2.abs_value) ? t1 : t2;
200} 200}
@@ -230,7 +230,7 @@ GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
230 */ 230 */
231struct GNUNET_TIME_Relative 231struct GNUNET_TIME_Relative
232GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start, 232GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
233 struct GNUNET_TIME_Absolute end) 233 struct GNUNET_TIME_Absolute end)
234{ 234{
235 struct GNUNET_TIME_Relative ret; 235 struct GNUNET_TIME_Relative ret;
236 236
@@ -271,17 +271,17 @@ GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
271 */ 271 */
272struct GNUNET_TIME_Absolute 272struct GNUNET_TIME_Absolute
273GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start, 273GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
274 struct GNUNET_TIME_Relative duration) 274 struct GNUNET_TIME_Relative duration)
275{ 275{
276 struct GNUNET_TIME_Absolute ret; 276 struct GNUNET_TIME_Absolute ret;
277 277
278 if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX)) 278 if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX))
279 return GNUNET_TIME_absolute_get_forever (); 279 return GNUNET_TIME_absolute_get_forever ();
280 if (start.abs_value + duration.rel_value < start.abs_value) 280 if (start.abs_value + duration.rel_value < start.abs_value)
281 { 281 {
282 GNUNET_break (0); 282 GNUNET_break (0);
283 return GNUNET_TIME_absolute_get_forever (); 283 return GNUNET_TIME_absolute_get_forever ();
284 } 284 }
285 ret.abs_value = start.abs_value + duration.rel_value; 285 ret.abs_value = start.abs_value + duration.rel_value;
286 return ret; 286 return ret;
287} 287}
@@ -297,7 +297,7 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
297 */ 297 */
298struct GNUNET_TIME_Absolute 298struct GNUNET_TIME_Absolute
299GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start, 299GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
300 struct GNUNET_TIME_Relative duration) 300 struct GNUNET_TIME_Relative duration)
301{ 301{
302 struct GNUNET_TIME_Absolute ret; 302 struct GNUNET_TIME_Absolute ret;
303 303
@@ -317,7 +317,7 @@ GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
317 */ 317 */
318struct GNUNET_TIME_Relative 318struct GNUNET_TIME_Relative
319GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, 319GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
320 unsigned int factor) 320 unsigned int factor)
321{ 321{
322 struct GNUNET_TIME_Relative ret; 322 struct GNUNET_TIME_Relative ret;
323 323
@@ -325,10 +325,10 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
325 return GNUNET_TIME_relative_get_zero (); 325 return GNUNET_TIME_relative_get_zero ();
326 ret.rel_value = rel.rel_value * (unsigned long long) factor; 326 ret.rel_value = rel.rel_value * (unsigned long long) factor;
327 if (ret.rel_value / factor != rel.rel_value) 327 if (ret.rel_value / factor != rel.rel_value)
328 { 328 {
329 GNUNET_break (0); 329 GNUNET_break (0);
330 return GNUNET_TIME_relative_get_forever (); 330 return GNUNET_TIME_relative_get_forever ();
331 } 331 }
332 return ret; 332 return ret;
333} 333}
334 334
@@ -342,7 +342,7 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
342 */ 342 */
343struct GNUNET_TIME_Relative 343struct GNUNET_TIME_Relative
344GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel, 344GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
345 unsigned int factor) 345 unsigned int factor)
346{ 346{
347 struct GNUNET_TIME_Relative ret; 347 struct GNUNET_TIME_Relative ret;
348 348
@@ -365,8 +365,8 @@ GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
365 * assuming it continues at the same speed 365 * assuming it continues at the same speed
366 */ 366 */
367struct GNUNET_TIME_Relative 367struct GNUNET_TIME_Relative
368GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, 368GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
369 uint64_t finished, uint64_t total) 369 uint64_t total)
370{ 370{
371 struct GNUNET_TIME_Relative dur; 371 struct GNUNET_TIME_Relative dur;
372 double exp; 372 double exp;
@@ -393,17 +393,17 @@ GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
393 */ 393 */
394struct GNUNET_TIME_Relative 394struct GNUNET_TIME_Relative
395GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1, 395GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
396 struct GNUNET_TIME_Relative a2) 396 struct GNUNET_TIME_Relative a2)
397{ 397{
398 struct GNUNET_TIME_Relative ret; 398 struct GNUNET_TIME_Relative ret;
399 399
400 if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX)) 400 if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX))
401 return GNUNET_TIME_relative_get_forever (); 401 return GNUNET_TIME_relative_get_forever ();
402 if (a1.rel_value + a2.rel_value < a1.rel_value) 402 if (a1.rel_value + a2.rel_value < a1.rel_value)
403 { 403 {
404 GNUNET_break (0); 404 GNUNET_break (0);
405 return GNUNET_TIME_relative_get_forever (); 405 return GNUNET_TIME_relative_get_forever ();
406 } 406 }
407 ret.rel_value = a1.rel_value + a2.rel_value; 407 ret.rel_value = a1.rel_value + a2.rel_value;
408 return ret; 408 return ret;
409} 409}
@@ -418,7 +418,7 @@ GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
418 */ 418 */
419struct GNUNET_TIME_Relative 419struct GNUNET_TIME_Relative
420GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1, 420GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
421 struct GNUNET_TIME_Relative a2) 421 struct GNUNET_TIME_Relative a2)
422{ 422{
423 struct GNUNET_TIME_Relative ret; 423 struct GNUNET_TIME_Relative ret;
424 424
diff --git a/src/util/winproc.c b/src/util/winproc.c
index aa4757f4a..252cacbfb 100644
--- a/src/util/winproc.c
+++ b/src/util/winproc.c
@@ -73,8 +73,7 @@ TSetNamedSecurityInfo GNSetNamedSecurityInfo;
73void 73void
74plibc_panic (int err, char *msg) 74plibc_panic (int err, char *msg)
75{ 75{
76 LOG (((err == 76 LOG (((err == INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
77 INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
78 "%s", msg); 77 "%s", msg);
79} 78}
80 79
@@ -100,139 +99,137 @@ GNInitWinEnv ()
100 99
101 /* Function to get CPU usage under Win NT */ 100 /* Function to get CPU usage under Win NT */
102 if (hNTDLL) 101 if (hNTDLL)
103 { 102 {
104 GNNtQuerySystemInformation = 103 GNNtQuerySystemInformation =
105 (TNtQuerySystemInformation) GetProcAddress (hNTDLL, 104 (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
106 "NtQuerySystemInformation"); 105 "NtQuerySystemInformation");
107 } 106 }
108 else 107 else
109 { 108 {
110 GNNtQuerySystemInformation = NULL; 109 GNNtQuerySystemInformation = NULL;
111 } 110 }
112 111
113 /* Functions to get information about a network adapter */ 112 /* Functions to get information about a network adapter */
114 hIphlpapi = LoadLibrary ("iphlpapi.dll"); 113 hIphlpapi = LoadLibrary ("iphlpapi.dll");
115 if (hIphlpapi) 114 if (hIphlpapi)
116 { 115 {
117 GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry"); 116 GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
118 GNGetIpAddrTable = 117 GNGetIpAddrTable =
119 (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable"); 118 (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
120 GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable"); 119 GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
121 GNGetBestInterface = 120 GNGetBestInterface =
122 (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface"); 121 (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface");
123 GGetAdaptersInfo = 122 GGetAdaptersInfo =
124 (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo"); 123 (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
125 } 124 }
126 else 125 else
127 { 126 {
128 GNGetIfEntry = NULL; 127 GNGetIfEntry = NULL;
129 GNGetIpAddrTable = NULL; 128 GNGetIpAddrTable = NULL;
130 GNGetIfTable = NULL; 129 GNGetIfTable = NULL;
131 GNGetBestInterface = NULL; 130 GNGetBestInterface = NULL;
132 GGetAdaptersInfo = NULL; 131 GGetAdaptersInfo = NULL;
133 } 132 }
134 133
135 /* Service & Account functions */ 134 /* Service & Account functions */
136 hAdvapi = LoadLibrary ("advapi32.dll"); 135 hAdvapi = LoadLibrary ("advapi32.dll");
137 if (hAdvapi) 136 if (hAdvapi)
138 { 137 {
139 GNOpenSCManager = 138 GNOpenSCManager =
140 (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA"); 139 (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
141 GNCreateService = 140 GNCreateService =
142 (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA"); 141 (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
143 GNCloseServiceHandle = 142 GNCloseServiceHandle =
144 (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle"); 143 (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
145 GNDeleteService = 144 GNDeleteService =
146 (TDeleteService) GetProcAddress (hAdvapi, "DeleteService"); 145 (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
147 GNRegisterServiceCtrlHandler = 146 GNRegisterServiceCtrlHandler =
148 (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi, 147 (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
149 "RegisterServiceCtrlHandlerA"); 148 "RegisterServiceCtrlHandlerA");
150 GNSetServiceStatus = 149 GNSetServiceStatus =
151 (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus"); 150 (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
152 GNStartServiceCtrlDispatcher = 151 GNStartServiceCtrlDispatcher =
153 (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi, 152 (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
154 "StartServiceCtrlDispatcherA"); 153 "StartServiceCtrlDispatcherA");
155 GNControlService = 154 GNControlService =
156 (TControlService) GetProcAddress (hAdvapi, "ControlService"); 155 (TControlService) GetProcAddress (hAdvapi, "ControlService");
157 GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA"); 156 GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
158 157
159 GNLsaOpenPolicy = 158 GNLsaOpenPolicy =
160 (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy"); 159 (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
161 GNLsaAddAccountRights = 160 GNLsaAddAccountRights =
162 (TLsaAddAccountRights) GetProcAddress (hAdvapi, 161 (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights");
163 "LsaAddAccountRights"); 162 GNLsaRemoveAccountRights =
164 GNLsaRemoveAccountRights = 163 (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
165 (TLsaRemoveAccountRights) GetProcAddress (hAdvapi, 164 "LsaRemoveAccountRights");
166 "LsaRemoveAccountRights"); 165 GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
167 GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose"); 166 GNLookupAccountName =
168 GNLookupAccountName = 167 (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
169 (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
170 168
171 GNGetFileSecurity = 169 GNGetFileSecurity =
172 (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA"); 170 (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
173 GNInitializeSecurityDescriptor = 171 GNInitializeSecurityDescriptor =
174 (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi, 172 (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
175 "InitializeSecurityDescriptor"); 173 "InitializeSecurityDescriptor");
176 GNGetSecurityDescriptorDacl = 174 GNGetSecurityDescriptorDacl =
177 (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi, 175 (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
178 "GetSecurityDescriptorDacl"); 176 "GetSecurityDescriptorDacl");
179 GNGetAclInformation = 177 GNGetAclInformation =
180 (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation"); 178 (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
181 GNInitializeAcl = 179 GNInitializeAcl =
182 (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl"); 180 (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
183 GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce"); 181 GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
184 GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid"); 182 GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
185 GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce"); 183 GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
186 GNAddAccessAllowedAce = 184 GNAddAccessAllowedAce =
187 (TAddAccessAllowedAce) GetProcAddress (hAdvapi, 185 (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce");
188 "AddAccessAllowedAce"); 186 GNSetNamedSecurityInfo =
189 GNSetNamedSecurityInfo = 187 (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
190 (TSetNamedSecurityInfo) GetProcAddress (hAdvapi, 188 "SetNamedSecurityInfoA");
191 "SetNamedSecurityInfoA"); 189 }
192 }
193 else 190 else
194 { 191 {
195 GNOpenSCManager = NULL; 192 GNOpenSCManager = NULL;
196 GNCreateService = NULL; 193 GNCreateService = NULL;
197 GNCloseServiceHandle = NULL; 194 GNCloseServiceHandle = NULL;
198 GNDeleteService = NULL; 195 GNDeleteService = NULL;
199 GNRegisterServiceCtrlHandler = NULL; 196 GNRegisterServiceCtrlHandler = NULL;
200 GNSetServiceStatus = NULL; 197 GNSetServiceStatus = NULL;
201 GNStartServiceCtrlDispatcher = NULL; 198 GNStartServiceCtrlDispatcher = NULL;
202 GNControlService = NULL; 199 GNControlService = NULL;
203 GNOpenService = NULL; 200 GNOpenService = NULL;
204 201
205 GNLsaOpenPolicy = NULL; 202 GNLsaOpenPolicy = NULL;
206 GNLsaAddAccountRights = NULL; 203 GNLsaAddAccountRights = NULL;
207 GNLsaRemoveAccountRights = NULL; 204 GNLsaRemoveAccountRights = NULL;
208 GNLsaClose = NULL; 205 GNLsaClose = NULL;
209 GNLookupAccountName = NULL; 206 GNLookupAccountName = NULL;
210 207
211 GNGetFileSecurity = NULL; 208 GNGetFileSecurity = NULL;
212 GNInitializeSecurityDescriptor = NULL; 209 GNInitializeSecurityDescriptor = NULL;
213 GNGetSecurityDescriptorDacl = NULL; 210 GNGetSecurityDescriptorDacl = NULL;
214 GNGetAclInformation = NULL; 211 GNGetAclInformation = NULL;
215 GNInitializeAcl = NULL; 212 GNInitializeAcl = NULL;
216 GNGetAce = NULL; 213 GNGetAce = NULL;
217 GNEqualSid = NULL; 214 GNEqualSid = NULL;
218 GNAddAce = NULL; 215 GNAddAce = NULL;
219 GNAddAccessAllowedAce = NULL; 216 GNAddAccessAllowedAce = NULL;
220 GNSetNamedSecurityInfo = NULL; 217 GNSetNamedSecurityInfo = NULL;
221 } 218 }
222 219
223 /* Account function */ 220 /* Account function */
224 hNetapi = LoadLibrary ("netapi32.dll"); 221 hNetapi = LoadLibrary ("netapi32.dll");
225 if (hNetapi) 222 if (hNetapi)
226 { 223 {
227 GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd"); 224 GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
228 GNNetUserSetInfo = 225 GNNetUserSetInfo =
229 (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo"); 226 (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
230 } 227 }
231 else 228 else
232 { 229 {
233 GNNetUserAdd = NULL; 230 GNNetUserAdd = NULL;
234 GNNetUserSetInfo = NULL; 231 GNNetUserSetInfo = NULL;
235 } 232 }
236 233
237 return ret; 234 return ret;
238} 235}