aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/bandwidth.c376
-rw-r--r--src/util/benchmark.c260
-rw-r--r--src/util/benchmark.h86
-rw-r--r--src/util/bio.c378
-rw-r--r--src/util/common_allocation.c284
-rw-r--r--src/util/common_endian.c26
-rw-r--r--src/util/common_logging.c1032
-rw-r--r--src/util/configuration.c1360
-rw-r--r--src/util/configuration_loader.c62
-rw-r--r--src/util/container_bloomfilter.c539
-rw-r--r--src/util/container_heap.c295
-rw-r--r--src/util/container_meta_data.c948
-rw-r--r--src/util/container_multihashmap.c879
-rw-r--r--src/util/container_multihashmap32.c329
-rw-r--r--src/util/container_multipeermap.c827
-rw-r--r--src/util/container_multishortmap.c781
-rw-r--r--src/util/container_multiuuidmap.c791
-rw-r--r--src/util/crypto_abe.c421
-rw-r--r--src/util/crypto_crc.c62
-rw-r--r--src/util/crypto_ecc.c1394
-rw-r--r--src/util/crypto_ecc_dlog.c349
-rw-r--r--src/util/crypto_ecc_setup.c600
-rw-r--r--src/util/crypto_hash.c313
-rw-r--r--src/util/crypto_hash_file.c164
-rw-r--r--src/util/crypto_hkdf.c182
-rw-r--r--src/util/crypto_kdf.c132
-rw-r--r--src/util/crypto_mpi.c111
-rw-r--r--src/util/crypto_paillier.c435
-rw-r--r--src/util/crypto_random.c284
-rw-r--r--src/util/crypto_rsa.c994
-rw-r--r--src/util/crypto_symmetric.c197
-rw-r--r--src/util/disk.c1373
-rw-r--r--src/util/disk.h4
-rw-r--r--src/util/dnsparser.c1219
-rw-r--r--src/util/dnsstub.c543
-rw-r--r--src/util/getopt.c1009
-rw-r--r--src/util/getopt_helpers.c583
-rw-r--r--src/util/gnunet-config-diff.c20
-rw-r--r--src/util/gnunet-config.c324
-rw-r--r--src/util/gnunet-ecc.c524
-rw-r--r--src/util/gnunet-qr.c244
-rw-r--r--src/util/gnunet-resolver.c138
-rw-r--r--src/util/gnunet-scrypt.c322
-rw-r--r--src/util/gnunet-service-resolver.c1243
-rw-r--r--src/util/gnunet-timeout.c102
-rw-r--r--src/util/gnunet-uri.c142
-rw-r--r--src/util/helper.c556
-rw-r--r--src/util/load.c94
-rw-r--r--src/util/mq.c669
-rw-r--r--src/util/mst.c457
-rw-r--r--src/util/nc.c82
-rw-r--r--src/util/network.c932
-rw-r--r--src/util/op.c148
-rw-r--r--src/util/os_installation.c784
-rw-r--r--src/util/os_network.c544
-rw-r--r--src/util/os_priority.c1160
-rw-r--r--src/util/perf_crypto_asymmetric.c113
-rw-r--r--src/util/perf_crypto_ecc_dlog.c161
-rw-r--r--src/util/perf_crypto_hash.c83
-rw-r--r--src/util/perf_crypto_paillier.c83
-rw-r--r--src/util/perf_crypto_rsa.c241
-rw-r--r--src/util/perf_crypto_symmetric.c49
-rw-r--r--src/util/perf_malloc.c31
-rw-r--r--src/util/plugin.c244
-rw-r--r--src/util/proc_compat.c12
-rw-r--r--src/util/program.c349
-rw-r--r--src/util/regex.c938
-rw-r--r--src/util/resolver.h6
-rw-r--r--src/util/resolver_api.c1284
-rw-r--r--src/util/scheduler.c1887
-rw-r--r--src/util/signal.c45
-rw-r--r--src/util/socks.c534
-rw-r--r--src/util/speedup.c82
-rw-r--r--src/util/speedup.h4
-rw-r--r--src/util/strings.c1789
-rw-r--r--src/util/test_bio.c432
-rw-r--r--src/util/test_client.c150
-rw-r--r--src/util/test_common_allocation.c114
-rw-r--r--src/util/test_common_endian.c16
-rw-r--r--src/util/test_common_logging.c94
-rw-r--r--src/util/test_common_logging_dummy.c88
-rw-r--r--src/util/test_common_logging_runtime_loglevels.c538
-rw-r--r--src/util/test_configuration.c605
-rw-r--r--src/util/test_container_bloomfilter.c300
-rw-r--r--src/util/test_container_dll.c53
-rw-r--r--src/util/test_container_heap.c358
-rw-r--r--src/util/test_container_meta_data.c459
-rw-r--r--src/util/test_container_multihashmap.c139
-rw-r--r--src/util/test_container_multihashmap32.c96
-rw-r--r--src/util/test_container_multipeermap.c139
-rw-r--r--src/util/test_crypto_crc.c8
-rw-r--r--src/util/test_crypto_ecc_dlog.c163
-rw-r--r--src/util/test_crypto_ecdh_eddsa.c75
-rw-r--r--src/util/test_crypto_ecdhe.c51
-rw-r--r--src/util/test_crypto_ecdsa.c264
-rw-r--r--src/util/test_crypto_eddsa.c202
-rw-r--r--src/util/test_crypto_hash.c100
-rw-r--r--src/util/test_crypto_hash_context.c15
-rw-r--r--src/util/test_crypto_hkdf.c155
-rw-r--r--src/util/test_crypto_kdf.c42
-rw-r--r--src/util/test_crypto_paillier.c300
-rw-r--r--src/util/test_crypto_random.c32
-rw-r--r--src/util/test_crypto_rsa.c126
-rw-r--r--src/util/test_crypto_symmetric.c167
-rw-r--r--src/util/test_disk.c331
-rw-r--r--src/util/test_getopt.c136
-rw-r--r--src/util/test_hexcoder.c28
-rw-r--r--src/util/test_mq.c278
-rw-r--r--src/util/test_os_network.c66
-rw-r--r--src/util/test_os_start_process.c283
-rw-r--r--src/util/test_peer.c98
-rw-r--r--src/util/test_plugin.c36
-rw-r--r--src/util/test_plugin_plug.c10
-rw-r--r--src/util/test_program.c124
-rw-r--r--src/util/test_regex.c188
-rw-r--r--src/util/test_resolver_api.c386
-rw-r--r--src/util/test_scheduler.c177
-rw-r--r--src/util/test_scheduler_delay.c52
-rw-r--r--src/util/test_service.c186
-rw-r--r--src/util/test_socks.c244
-rw-r--r--src/util/test_speedup.c81
-rw-r--r--src/util/test_strings.c159
-rw-r--r--src/util/test_strings_to_data.c40
-rw-r--r--src/util/test_time.c262
-rw-r--r--src/util/test_tun.c62
-rw-r--r--src/util/time.c398
-rw-r--r--src/util/tun.c241
127 files changed, 22779 insertions, 22410 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c
index 562eed01b..1f889d678 100644
--- a/src/util/bandwidth.c
+++ b/src/util/bandwidth.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-bandwidth", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)
31 31
32/** 32/**
33 * Create a new bandwidth value. 33 * Create a new bandwidth value.
@@ -36,11 +36,11 @@
36 * @return the new bandwidth value 36 * @return the new bandwidth value
37 */ 37 */
38struct GNUNET_BANDWIDTH_Value32NBO 38struct GNUNET_BANDWIDTH_Value32NBO
39GNUNET_BANDWIDTH_value_init(uint32_t bytes_per_second) 39GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
40{ 40{
41 struct GNUNET_BANDWIDTH_Value32NBO ret; 41 struct GNUNET_BANDWIDTH_Value32NBO ret;
42 42
43 ret.value__ = htonl(bytes_per_second); 43 ret.value__ = htonl (bytes_per_second);
44 return ret; 44 return ret;
45} 45}
46 46
@@ -53,11 +53,11 @@ GNUNET_BANDWIDTH_value_init(uint32_t bytes_per_second)
53 * @return the min of b1 and b2 53 * @return the min of b1 and b2
54 */ 54 */
55struct GNUNET_BANDWIDTH_Value32NBO 55struct GNUNET_BANDWIDTH_Value32NBO
56GNUNET_BANDWIDTH_value_min(struct GNUNET_BANDWIDTH_Value32NBO b1, 56GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
57 struct GNUNET_BANDWIDTH_Value32NBO b2) 57 struct GNUNET_BANDWIDTH_Value32NBO b2)
58{ 58{
59 return GNUNET_BANDWIDTH_value_init( 59 return GNUNET_BANDWIDTH_value_init (
60 GNUNET_MIN(ntohl(b1.value__), ntohl(b2.value__))); 60 GNUNET_MIN (ntohl (b1.value__), ntohl (b2.value__)));
61} 61}
62 62
63 63
@@ -69,11 +69,11 @@ GNUNET_BANDWIDTH_value_min(struct GNUNET_BANDWIDTH_Value32NBO b1,
69 * @return the min of b1 and b2 69 * @return the min of b1 and b2
70 */ 70 */
71struct GNUNET_BANDWIDTH_Value32NBO 71struct GNUNET_BANDWIDTH_Value32NBO
72GNUNET_BANDWIDTH_value_max(struct GNUNET_BANDWIDTH_Value32NBO b1, 72GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1,
73 struct GNUNET_BANDWIDTH_Value32NBO b2) 73 struct GNUNET_BANDWIDTH_Value32NBO b2)
74{ 74{
75 return GNUNET_BANDWIDTH_value_init( 75 return GNUNET_BANDWIDTH_value_init (
76 GNUNET_MAX(ntohl(b1.value__), ntohl(b2.value__))); 76 GNUNET_MAX (ntohl (b1.value__), ntohl (b2.value__)));
77} 77}
78 78
79 79
@@ -85,10 +85,10 @@ GNUNET_BANDWIDTH_value_max(struct GNUNET_BANDWIDTH_Value32NBO b1,
85 * @return the sum of b1 and b2 85 * @return the sum of b1 and b2
86 */ 86 */
87struct GNUNET_BANDWIDTH_Value32NBO 87struct GNUNET_BANDWIDTH_Value32NBO
88GNUNET_BANDWIDTH_value_sum(struct GNUNET_BANDWIDTH_Value32NBO b1, 88GNUNET_BANDWIDTH_value_sum (struct GNUNET_BANDWIDTH_Value32NBO b1,
89 struct GNUNET_BANDWIDTH_Value32NBO b2) 89 struct GNUNET_BANDWIDTH_Value32NBO b2)
90{ 90{
91 return GNUNET_BANDWIDTH_value_init(ntohl(b1.value__) + ntohl(b2.value__)); 91 return GNUNET_BANDWIDTH_value_init (ntohl (b1.value__) + ntohl (b2.value__));
92} 92}
93 93
94 94
@@ -101,18 +101,18 @@ GNUNET_BANDWIDTH_value_sum(struct GNUNET_BANDWIDTH_Value32NBO b1,
101 * @return number of bytes available at bps until deadline 101 * @return number of bytes available at bps until deadline
102 */ 102 */
103uint64_t 103uint64_t
104GNUNET_BANDWIDTH_value_get_available_until( 104GNUNET_BANDWIDTH_value_get_available_until (
105 struct GNUNET_BANDWIDTH_Value32NBO bps, 105 struct GNUNET_BANDWIDTH_Value32NBO bps,
106 struct GNUNET_TIME_Relative deadline) 106 struct GNUNET_TIME_Relative deadline)
107{ 107{
108 uint64_t b; 108 uint64_t b;
109 109
110 b = ntohl(bps.value__); 110 b = ntohl (bps.value__);
111 LOG(GNUNET_ERROR_TYPE_DEBUG, 111 LOG (GNUNET_ERROR_TYPE_DEBUG,
112 "Bandwidth has %llu bytes available until deadline in %s\n", 112 "Bandwidth has %llu bytes available until deadline in %s\n",
113 (unsigned long long)((b * deadline.rel_value_us + 500000LL) / 113 (unsigned long long) ((b * deadline.rel_value_us + 500000LL)
114 1000000LL), 114 / 1000000LL),
115 GNUNET_STRINGS_relative_time_to_string(deadline, GNUNET_YES)); 115 GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES));
116 return (b * deadline.rel_value_us + 500000LL) / 1000000LL; 116 return (b * deadline.rel_value_us + 500000LL) / 1000000LL;
117} 117}
118 118
@@ -126,24 +126,24 @@ GNUNET_BANDWIDTH_value_get_available_until(
126 * @return how long it would take 126 * @return how long it would take
127 */ 127 */
128struct GNUNET_TIME_Relative 128struct GNUNET_TIME_Relative
129GNUNET_BANDWIDTH_value_get_delay_for(struct GNUNET_BANDWIDTH_Value32NBO bps, 129GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
130 uint64_t size) 130 uint64_t size)
131{ 131{
132 uint64_t b; 132 uint64_t b;
133 struct GNUNET_TIME_Relative ret; 133 struct GNUNET_TIME_Relative ret;
134 134
135 b = ntohl(bps.value__); 135 b = ntohl (bps.value__);
136 if (0 == b) 136 if (0 == b)
137 { 137 {
138 LOG(GNUNET_ERROR_TYPE_DEBUG, 138 LOG (GNUNET_ERROR_TYPE_DEBUG,
139 "Bandwidth suggests delay of infinity (zero bandwidth)\n"); 139 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
140 return GNUNET_TIME_UNIT_FOREVER_REL; 140 return GNUNET_TIME_UNIT_FOREVER_REL;
141 } 141 }
142 ret.rel_value_us = size * 1000LL * 1000LL / b; 142 ret.rel_value_us = size * 1000LL * 1000LL / b;
143 LOG(GNUNET_ERROR_TYPE_DEBUG, 143 LOG (GNUNET_ERROR_TYPE_DEBUG,
144 "Bandwidth suggests delay of %s for %llu bytes of traffic\n", 144 "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
145 GNUNET_STRINGS_relative_time_to_string(ret, GNUNET_YES), 145 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES),
146 (unsigned long long)size); 146 (unsigned long long) size);
147 return ret; 147 return ret;
148} 148}
149 149
@@ -154,17 +154,17 @@ GNUNET_BANDWIDTH_value_get_delay_for(struct GNUNET_BANDWIDTH_Value32NBO bps,
154 * @param cls the `struct GNUNET_BANDWIDTH_Tracker` 154 * @param cls the `struct GNUNET_BANDWIDTH_Tracker`
155 */ 155 */
156static void 156static void
157excess_trigger(void *cls) 157excess_trigger (void *cls)
158{ 158{
159 struct GNUNET_BANDWIDTH_Tracker *av = cls; 159 struct GNUNET_BANDWIDTH_Tracker *av = cls;
160 160
161 av->excess_task = NULL; 161 av->excess_task = NULL;
162 if (NULL != av->excess_cb) 162 if (NULL != av->excess_cb)
163 { 163 {
164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Notifying application about excess bandwidth\n"); 165 "Notifying application about excess bandwidth\n");
166 av->excess_cb(av->excess_cb_cls); 166 av->excess_cb (av->excess_cb_cls);
167 } 167 }
168} 168}
169 169
170 170
@@ -172,7 +172,7 @@ excess_trigger(void *cls)
172 * Recalculate when we might need to call the excess callback. 172 * Recalculate when we might need to call the excess callback.
173 */ 173 */
174static void 174static void
175update_excess(struct GNUNET_BANDWIDTH_Tracker *av) 175update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
176{ 176{
177 struct GNUNET_TIME_Relative delay; 177 struct GNUNET_TIME_Relative delay;
178 struct GNUNET_TIME_Absolute now; 178 struct GNUNET_TIME_Absolute now;
@@ -184,53 +184,53 @@ update_excess(struct GNUNET_BANDWIDTH_Tracker *av)
184 184
185 if (NULL == av->excess_cb) 185 if (NULL == av->excess_cb)
186 return; /* nothing to do */ 186 return; /* nothing to do */
187 now = GNUNET_TIME_absolute_get(); 187 now = GNUNET_TIME_absolute_get ();
188 delta_time = now.abs_value_us - av->last_update__.abs_value_us; 188 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
189 delta_avail = 189 delta_avail =
190 (delta_time * ((unsigned long long)av->available_bytes_per_s__) + 190 (delta_time * ((unsigned long long) av->available_bytes_per_s__)
191 500000LL) / 191 + 500000LL)
192 1000000LL; 192 / 1000000LL;
193 current_consumption = av->consumption_since_last_update__ - delta_avail; 193 current_consumption = av->consumption_since_last_update__ - delta_avail;
194 if (current_consumption > av->consumption_since_last_update__) 194 if (current_consumption > av->consumption_since_last_update__)
195 { 195 {
196 /* integer underflow, cap! */ 196 /* integer underflow, cap! */
197 current_consumption = INT64_MIN; 197 current_consumption = INT64_MIN;
198 } 198 }
199 /* negative current_consumption means that we have savings */ 199 /* negative current_consumption means that we have savings */
200 max_carry = ((uint64_t)av->available_bytes_per_s__) * av->max_carry_s__; 200 max_carry = ((uint64_t) av->available_bytes_per_s__) * av->max_carry_s__;
201 if (max_carry < GNUNET_MAX_MESSAGE_SIZE) 201 if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
202 max_carry = GNUNET_MAX_MESSAGE_SIZE; 202 max_carry = GNUNET_MAX_MESSAGE_SIZE;
203 if (max_carry > INT64_MAX) 203 if (max_carry > INT64_MAX)
204 max_carry = INT64_MAX; 204 max_carry = INT64_MAX;
205 left_bytes = current_consumption + max_carry; 205 left_bytes = current_consumption + max_carry;
206 if (left_bytes < current_consumption) 206 if (left_bytes < current_consumption)
207 { 207 {
208 /* integer overflow, cap! */ 208 /* integer overflow, cap! */
209 left_bytes = INT64_MAX; 209 left_bytes = INT64_MAX;
210 } 210 }
211 /* left_bytes now contains the number of bytes needed until 211 /* left_bytes now contains the number of bytes needed until
212 we have more savings than allowed */ 212 we have more savings than allowed */
213 if (left_bytes < 0) 213 if (left_bytes < 0)
214 { 214 {
215 /* having excess already */ 215 /* having excess already */
216 delay = GNUNET_TIME_UNIT_ZERO; 216 delay = GNUNET_TIME_UNIT_ZERO;
217 } 217 }
218 else 218 else
219 { 219 {
220 double factor = 1.0 * left_bytes / (double)av->available_bytes_per_s__; 220 double factor = 1.0 * left_bytes / (double) av->available_bytes_per_s__;
221 delay = 221 delay =
222 GNUNET_TIME_relative_saturating_multiply(GNUNET_TIME_UNIT_SECONDS, 222 GNUNET_TIME_relative_saturating_multiply (GNUNET_TIME_UNIT_SECONDS,
223 (unsigned long long)factor); 223 (unsigned long long) factor);
224 } 224 }
225 GNUNET_log( 225 GNUNET_log (
226 GNUNET_ERROR_TYPE_DEBUG, 226 GNUNET_ERROR_TYPE_DEBUG,
227 "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n", 227 "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n",
228 (unsigned long long)av->available_bytes_per_s__, 228 (unsigned long long) av->available_bytes_per_s__,
229 GNUNET_STRINGS_relative_time_to_string(delay, GNUNET_NO), 229 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO),
230 (long long)left_bytes); 230 (long long) left_bytes);
231 if (NULL != av->excess_task) 231 if (NULL != av->excess_task)
232 GNUNET_SCHEDULER_cancel(av->excess_task); 232 GNUNET_SCHEDULER_cancel (av->excess_task);
233 av->excess_task = GNUNET_SCHEDULER_add_delayed(delay, &excess_trigger, av); 233 av->excess_task = GNUNET_SCHEDULER_add_delayed (delay, &excess_trigger, av);
234} 234}
235 235
236 236
@@ -256,7 +256,7 @@ update_excess(struct GNUNET_BANDWIDTH_Tracker *av)
256 * @param excess_cb_cls closure for @a excess_cb 256 * @param excess_cb_cls closure for @a excess_cb
257 */ 257 */
258void 258void
259GNUNET_BANDWIDTH_tracker_init2( 259GNUNET_BANDWIDTH_tracker_init2 (
260 struct GNUNET_BANDWIDTH_Tracker *av, 260 struct GNUNET_BANDWIDTH_Tracker *av,
261 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, 261 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
262 void *update_cb_cls, 262 void *update_cb_cls,
@@ -268,17 +268,17 @@ GNUNET_BANDWIDTH_tracker_init2(
268 av->update_cb = update_cb; 268 av->update_cb = update_cb;
269 av->update_cb_cls = update_cb_cls; 269 av->update_cb_cls = update_cb_cls;
270 av->consumption_since_last_update__ = 0; 270 av->consumption_since_last_update__ = 0;
271 av->last_update__ = GNUNET_TIME_absolute_get(); 271 av->last_update__ = GNUNET_TIME_absolute_get ();
272 av->available_bytes_per_s__ = ntohl(bytes_per_second_limit.value__); 272 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
273 av->max_carry_s__ = max_carry_s; 273 av->max_carry_s__ = max_carry_s;
274 av->excess_cb = excess_cb; 274 av->excess_cb = excess_cb;
275 av->excess_cb_cls = excess_cb_cls; 275 av->excess_cb_cls = excess_cb_cls;
276 LOG(GNUNET_ERROR_TYPE_DEBUG, 276 LOG (GNUNET_ERROR_TYPE_DEBUG,
277 "Tracker %p initialized with %u Bps and max carry %u\n", 277 "Tracker %p initialized with %u Bps and max carry %u\n",
278 av, 278 av,
279 (unsigned int)av->available_bytes_per_s__, 279 (unsigned int) av->available_bytes_per_s__,
280 (unsigned int)max_carry_s); 280 (unsigned int) max_carry_s);
281 update_excess(av); 281 update_excess (av);
282} 282}
283 283
284 284
@@ -299,20 +299,20 @@ GNUNET_BANDWIDTH_tracker_init2(
299 * may accumulate before it expires 299 * may accumulate before it expires
300 */ 300 */
301void 301void
302GNUNET_BANDWIDTH_tracker_init( 302GNUNET_BANDWIDTH_tracker_init (
303 struct GNUNET_BANDWIDTH_Tracker *av, 303 struct GNUNET_BANDWIDTH_Tracker *av,
304 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, 304 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
305 void *update_cb_cls, 305 void *update_cb_cls,
306 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, 306 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
307 uint32_t max_carry_s) 307 uint32_t max_carry_s)
308{ 308{
309 GNUNET_BANDWIDTH_tracker_init2(av, 309 GNUNET_BANDWIDTH_tracker_init2 (av,
310 update_cb, 310 update_cb,
311 update_cb_cls, 311 update_cb_cls,
312 bytes_per_second_limit, 312 bytes_per_second_limit,
313 max_carry_s, 313 max_carry_s,
314 NULL, 314 NULL,
315 NULL); 315 NULL);
316} 316}
317 317
318 318
@@ -322,10 +322,10 @@ GNUNET_BANDWIDTH_tracker_init(
322 * @param av the respective trackers 322 * @param av the respective trackers
323 */ 323 */
324void 324void
325GNUNET_BANDWIDTH_tracker_notification_stop(struct GNUNET_BANDWIDTH_Tracker *av) 325GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av)
326{ 326{
327 if (NULL != av->excess_task) 327 if (NULL != av->excess_task)
328 GNUNET_SCHEDULER_cancel(av->excess_task); 328 GNUNET_SCHEDULER_cancel (av->excess_task);
329 av->excess_task = NULL; 329 av->excess_task = NULL;
330 av->excess_cb = NULL; 330 av->excess_cb = NULL;
331 av->excess_cb_cls = NULL; 331 av->excess_cb_cls = NULL;
@@ -341,7 +341,7 @@ GNUNET_BANDWIDTH_tracker_notification_stop(struct GNUNET_BANDWIDTH_Tracker *av)
341 * @param av tracker to update 341 * @param av tracker to update
342 */ 342 */
343static void 343static void
344update_tracker(struct GNUNET_BANDWIDTH_Tracker *av) 344update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
345{ 345{
346 struct GNUNET_TIME_Absolute now; 346 struct GNUNET_TIME_Absolute now;
347 uint64_t delta_time; 347 uint64_t delta_time;
@@ -349,39 +349,39 @@ update_tracker(struct GNUNET_BANDWIDTH_Tracker *av)
349 uint64_t left_bytes; 349 uint64_t left_bytes;
350 uint64_t max_carry; 350 uint64_t max_carry;
351 351
352 now = GNUNET_TIME_absolute_get(); 352 now = GNUNET_TIME_absolute_get ();
353 delta_time = now.abs_value_us - av->last_update__.abs_value_us; 353 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
354 delta_avail = 354 delta_avail =
355 (delta_time * ((unsigned long long)av->available_bytes_per_s__) + 355 (delta_time * ((unsigned long long) av->available_bytes_per_s__)
356 500000LL) / 356 + 500000LL)
357 1000000LL; 357 / 1000000LL;
358 av->consumption_since_last_update__ -= delta_avail; 358 av->consumption_since_last_update__ -= delta_avail;
359 av->last_update__ = now; 359 av->last_update__ = now;
360 if (av->consumption_since_last_update__ < 0) 360 if (av->consumption_since_last_update__ < 0)
361 { 361 {
362 left_bytes = -av->consumption_since_last_update__; 362 left_bytes = -av->consumption_since_last_update__;
363 max_carry = 363 max_carry =
364 ((unsigned long long)av->available_bytes_per_s__) * av->max_carry_s__; 364 ((unsigned long long) av->available_bytes_per_s__) * av->max_carry_s__;
365 if (max_carry < GNUNET_MAX_MESSAGE_SIZE) 365 if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
366 max_carry = GNUNET_MAX_MESSAGE_SIZE; 366 max_carry = GNUNET_MAX_MESSAGE_SIZE;
367 if (max_carry > INT64_MAX) 367 if (max_carry > INT64_MAX)
368 max_carry = INT64_MAX; 368 max_carry = INT64_MAX;
369 if (max_carry > left_bytes) 369 if (max_carry > left_bytes)
370 av->consumption_since_last_update__ = -left_bytes; 370 av->consumption_since_last_update__ = -left_bytes;
371 else 371 else
372 av->consumption_since_last_update__ = -max_carry; 372 av->consumption_since_last_update__ = -max_carry;
373 } 373 }
374#if !defined(GNUNET_CULL_LOGGING) 374#if ! defined(GNUNET_CULL_LOGGING)
375 { 375 {
376 struct GNUNET_TIME_Relative delta; 376 struct GNUNET_TIME_Relative delta;
377 377
378 delta.rel_value_us = delta_time; 378 delta.rel_value_us = delta_time;
379 LOG(GNUNET_ERROR_TYPE_DEBUG, 379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n", 380 "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n",
381 av, 381 av,
382 (long long)av->consumption_since_last_update__, 382 (long long) av->consumption_since_last_update__,
383 (unsigned int)av->available_bytes_per_s__, 383 (unsigned int) av->available_bytes_per_s__,
384 GNUNET_STRINGS_relative_time_to_string(delta, GNUNET_YES)); 384 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
385 } 385 }
386#endif 386#endif
387} 387}
@@ -399,48 +399,48 @@ update_tracker(struct GNUNET_BANDWIDTH_Tracker *av)
399 * @return #GNUNET_YES if this consumption is above the limit 399 * @return #GNUNET_YES if this consumption is above the limit
400 */ 400 */
401int 401int
402GNUNET_BANDWIDTH_tracker_consume(struct GNUNET_BANDWIDTH_Tracker *av, 402GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
403 ssize_t size) 403 ssize_t size)
404{ 404{
405 int64_t nc; 405 int64_t nc;
406 406
407 LOG(GNUNET_ERROR_TYPE_DEBUG, 407 LOG (GNUNET_ERROR_TYPE_DEBUG,
408 "Tracker %p consumes %d bytes\n", 408 "Tracker %p consumes %d bytes\n",
409 av, 409 av,
410 (int)size); 410 (int) size);
411 if (size > 0) 411 if (size > 0)
412 {
413 nc = av->consumption_since_last_update__ + size;
414 if (nc < av->consumption_since_last_update__)
412 { 415 {
413 nc = av->consumption_since_last_update__ + size; 416 /* integer overflow, very bad */
414 if (nc < av->consumption_since_last_update__) 417 GNUNET_break (0);
415 { 418 return GNUNET_SYSERR;
416 /* integer overflow, very bad */
417 GNUNET_break(0);
418 return GNUNET_SYSERR;
419 }
420 av->consumption_since_last_update__ = nc;
421 update_tracker(av);
422 update_excess(av);
423 if (av->consumption_since_last_update__ > 0)
424 {
425 LOG(GNUNET_ERROR_TYPE_DEBUG,
426 "Tracker %p consumption %llu bytes above limit\n",
427 av,
428 (unsigned long long)av->consumption_since_last_update__);
429 return GNUNET_YES;
430 }
431 } 419 }
420 av->consumption_since_last_update__ = nc;
421 update_tracker (av);
422 update_excess (av);
423 if (av->consumption_since_last_update__ > 0)
424 {
425 LOG (GNUNET_ERROR_TYPE_DEBUG,
426 "Tracker %p consumption %llu bytes above limit\n",
427 av,
428 (unsigned long long) av->consumption_since_last_update__);
429 return GNUNET_YES;
430 }
431 }
432 else 432 else
433 {
434 nc = av->consumption_since_last_update__ + size;
435 if (nc > av->consumption_since_last_update__)
433 { 436 {
434 nc = av->consumption_since_last_update__ + size; 437 /* integer underflow, very bad */
435 if (nc > av->consumption_since_last_update__) 438 GNUNET_break (0);
436 { 439 return GNUNET_SYSERR;
437 /* integer underflow, very bad */
438 GNUNET_break(0);
439 return GNUNET_SYSERR;
440 }
441 av->consumption_since_last_update__ = nc;
442 update_excess(av);
443 } 440 }
441 av->consumption_since_last_update__ = nc;
442 update_excess (av);
443 }
444 return GNUNET_NO; 444 return GNUNET_NO;
445} 445}
446 446
@@ -455,34 +455,34 @@ GNUNET_BANDWIDTH_tracker_consume(struct GNUNET_BANDWIDTH_Tracker *av,
455 * @return time in ms to wait for consumption to be OK 455 * @return time in ms to wait for consumption to be OK
456 */ 456 */
457struct GNUNET_TIME_Relative 457struct GNUNET_TIME_Relative
458GNUNET_BANDWIDTH_tracker_get_delay(struct GNUNET_BANDWIDTH_Tracker *av, 458GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
459 size_t size) 459 size_t size)
460{ 460{
461 struct GNUNET_TIME_Relative ret; 461 struct GNUNET_TIME_Relative ret;
462 int64_t bytes_needed; 462 int64_t bytes_needed;
463 463
464 if (0 == av->available_bytes_per_s__) 464 if (0 == av->available_bytes_per_s__)
465 { 465 {
466 LOG(GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av); 466 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
467 return GNUNET_TIME_UNIT_FOREVER_REL; 467 return GNUNET_TIME_UNIT_FOREVER_REL;
468 } 468 }
469 update_tracker(av); 469 update_tracker (av);
470 bytes_needed = size + av->consumption_since_last_update__; 470 bytes_needed = size + av->consumption_since_last_update__;
471 if (bytes_needed <= 0) 471 if (bytes_needed <= 0)
472 { 472 {
473 LOG(GNUNET_ERROR_TYPE_DEBUG, 473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "Tracker %p delay for %u bytes is zero\n", 474 "Tracker %p delay for %u bytes is zero\n",
475 av, 475 av,
476 (unsigned int)size); 476 (unsigned int) size);
477 return GNUNET_TIME_UNIT_ZERO; 477 return GNUNET_TIME_UNIT_ZERO;
478 } 478 }
479 ret.rel_value_us = (1000LL * 1000LL * bytes_needed) / 479 ret.rel_value_us = (1000LL * 1000LL * bytes_needed)
480 (unsigned long long)av->available_bytes_per_s__; 480 / (unsigned long long) av->available_bytes_per_s__;
481 LOG(GNUNET_ERROR_TYPE_DEBUG, 481 LOG (GNUNET_ERROR_TYPE_DEBUG,
482 "Tracker %p delay for %u bytes is %s\n", 482 "Tracker %p delay for %u bytes is %s\n",
483 av, 483 av,
484 (unsigned int)size, 484 (unsigned int) size,
485 GNUNET_STRINGS_relative_time_to_string(ret, GNUNET_YES)); 485 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
486 return ret; 486 return ret;
487} 487}
488 488
@@ -495,24 +495,24 @@ GNUNET_BANDWIDTH_tracker_get_delay(struct GNUNET_BANDWIDTH_Tracker *av,
495 * @return number of bytes available for consumption right now 495 * @return number of bytes available for consumption right now
496 */ 496 */
497int64_t 497int64_t
498GNUNET_BANDWIDTH_tracker_get_available(struct GNUNET_BANDWIDTH_Tracker *av) 498GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
499{ 499{
500 struct GNUNET_BANDWIDTH_Value32NBO bps; 500 struct GNUNET_BANDWIDTH_Value32NBO bps;
501 uint64_t avail; 501 uint64_t avail;
502 int64_t used; 502 int64_t used;
503 503
504 update_tracker(av); 504 update_tracker (av);
505 bps = GNUNET_BANDWIDTH_value_init(av->available_bytes_per_s__); 505 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
506 avail = 506 avail =
507 GNUNET_BANDWIDTH_value_get_available_until(bps, 507 GNUNET_BANDWIDTH_value_get_available_until (bps,
508 GNUNET_TIME_absolute_get_duration( 508 GNUNET_TIME_absolute_get_duration (
509 av->last_update__)); 509 av->last_update__));
510 used = av->consumption_since_last_update__; 510 used = av->consumption_since_last_update__;
511 LOG(GNUNET_ERROR_TYPE_DEBUG, 511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "Tracker %p available bandwidth is %lld bytes\n", 512 "Tracker %p available bandwidth is %lld bytes\n",
513 av, 513 av,
514 (long long)(int64_t)(avail - used)); 514 (long long) (int64_t) (avail - used));
515 return (int64_t)(avail - used); 515 return (int64_t) (avail - used);
516} 516}
517 517
518 518
@@ -523,26 +523,26 @@ GNUNET_BANDWIDTH_tracker_get_available(struct GNUNET_BANDWIDTH_Tracker *av)
523 * @param bytes_per_second_limit new limit to assume 523 * @param bytes_per_second_limit new limit to assume
524 */ 524 */
525void 525void
526GNUNET_BANDWIDTH_tracker_update_quota( 526GNUNET_BANDWIDTH_tracker_update_quota (
527 struct GNUNET_BANDWIDTH_Tracker *av, 527 struct GNUNET_BANDWIDTH_Tracker *av,
528 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit) 528 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
529{ 529{
530 uint32_t old_limit; 530 uint32_t old_limit;
531 uint32_t new_limit; 531 uint32_t new_limit;
532 532
533 new_limit = ntohl(bytes_per_second_limit.value__); 533 new_limit = ntohl (bytes_per_second_limit.value__);
534 LOG(GNUNET_ERROR_TYPE_DEBUG, 534 LOG (GNUNET_ERROR_TYPE_DEBUG,
535 "Tracker %p bandwidth changed to %u Bps\n", 535 "Tracker %p bandwidth changed to %u Bps\n",
536 av, 536 av,
537 (unsigned int)new_limit); 537 (unsigned int) new_limit);
538 update_tracker(av); 538 update_tracker (av);
539 old_limit = av->available_bytes_per_s__; 539 old_limit = av->available_bytes_per_s__;
540 av->available_bytes_per_s__ = new_limit; 540 av->available_bytes_per_s__ = new_limit;
541 if (NULL != av->update_cb) 541 if (NULL != av->update_cb)
542 av->update_cb(av->update_cb_cls); 542 av->update_cb (av->update_cb_cls);
543 if (old_limit > new_limit) 543 if (old_limit > new_limit)
544 update_tracker(av); /* maximum excess might be less now */ 544 update_tracker (av); /* maximum excess might be less now */
545 update_excess(av); 545 update_excess (av);
546} 546}
547 547
548 548
diff --git a/src/util/benchmark.c b/src/util/benchmark.c
index c01b7a515..497b3cde3 100644
--- a/src/util/benchmark.c
+++ b/src/util/benchmark.c
@@ -47,109 +47,114 @@ static pthread_once_t key_once = PTHREAD_ONCE_INIT;
47 * @param bd the benchmark data 47 * @param bd the benchmark data
48 */ 48 */
49static void 49static void
50write_benchmark_data(struct BenchmarkData *bd) 50write_benchmark_data (struct BenchmarkData *bd)
51{ 51{
52 struct GNUNET_DISK_FileHandle *fh; 52 struct GNUNET_DISK_FileHandle *fh;
53 pid_t pid = getpid(); 53 pid_t pid = getpid ();
54 pid_t tid = syscall(SYS_gettid); 54 pid_t tid = syscall (SYS_gettid);
55 char *benchmark_dir; 55 char *benchmark_dir;
56 char *s; 56 char *s;
57 57
58 benchmark_dir = getenv("GNUNET_BENCHMARK_DIR"); 58 benchmark_dir = getenv ("GNUNET_BENCHMARK_DIR");
59 59
60 if (NULL == benchmark_dir) 60 if (NULL == benchmark_dir)
61 return; 61 return;
62 62
63 if (GNUNET_OK != GNUNET_DISK_directory_create(benchmark_dir)) 63 if (GNUNET_OK != GNUNET_DISK_directory_create (benchmark_dir))
64 { 64 {
65 GNUNET_break(0); 65 GNUNET_break (0);
66 return; 66 return;
67 } 67 }
68 68
69 GNUNET_asprintf(&s, "%s/gnunet-benchmark-ops-%s-%llu-%llu.txt", 69 GNUNET_asprintf (&s, "%s/gnunet-benchmark-ops-%s-%llu-%llu.txt",
70 benchmark_dir, 70 benchmark_dir,
71 (pid == tid) ? "main" : "thread", 71 (pid == tid) ? "main" : "thread",
72 (unsigned long long)pid, 72 (unsigned long long) pid,
73 (unsigned long long)tid); 73 (unsigned long long) tid);
74 74
75 fh = GNUNET_DISK_file_open(s, 75 fh = GNUNET_DISK_file_open (s,
76 (GNUNET_DISK_OPEN_WRITE | 76 (GNUNET_DISK_OPEN_WRITE
77 GNUNET_DISK_OPEN_TRUNCATE | 77 | GNUNET_DISK_OPEN_TRUNCATE
78 GNUNET_DISK_OPEN_CREATE), 78 | GNUNET_DISK_OPEN_CREATE),
79 (GNUNET_DISK_PERM_USER_READ | 79 (GNUNET_DISK_PERM_USER_READ
80 GNUNET_DISK_PERM_USER_WRITE)); 80 | GNUNET_DISK_PERM_USER_WRITE));
81 GNUNET_assert(NULL != fh); 81 GNUNET_assert (NULL != fh);
82 GNUNET_free(s); 82 GNUNET_free (s);
83 83
84#define WRITE_BENCHMARK_OP(opname) do { \ 84#define WRITE_BENCHMARK_OP(opname) do { \
85 GNUNET_asprintf(&s, "op " #opname " count %llu time_us %llu\n", \ 85 GNUNET_asprintf (&s, "op " #opname " count %llu time_us %llu\n", \
86 (unsigned long long)bd->opname ## _count, \ 86 (unsigned long long) bd->opname ## _count, \
87 (unsigned long long)bd->opname ## _time.rel_value_us); \ 87 (unsigned long long) bd->opname ## _time.rel_value_us); \
88 GNUNET_assert(GNUNET_SYSERR != GNUNET_DISK_file_write_blocking(fh, s, strlen(s))); \ 88 GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s, \
89 GNUNET_free(s); \ 89 strlen ( \
90 } while (0) 90 s))); \
91 91 GNUNET_free (s); \
92 WRITE_BENCHMARK_OP(ecc_ecdh); 92} while (0)
93 WRITE_BENCHMARK_OP(ecdh_eddsa); 93
94 WRITE_BENCHMARK_OP(ecdhe_key_create); 94 WRITE_BENCHMARK_OP (ecc_ecdh);
95 WRITE_BENCHMARK_OP(ecdhe_key_get_public); 95 WRITE_BENCHMARK_OP (ecdh_eddsa);
96 WRITE_BENCHMARK_OP(ecdsa_ecdh); 96 WRITE_BENCHMARK_OP (ecdhe_key_create);
97 WRITE_BENCHMARK_OP(ecdsa_key_create); 97 WRITE_BENCHMARK_OP (ecdhe_key_get_public);
98 WRITE_BENCHMARK_OP(ecdsa_key_get_public); 98 WRITE_BENCHMARK_OP (ecdsa_ecdh);
99 WRITE_BENCHMARK_OP(ecdsa_sign); 99 WRITE_BENCHMARK_OP (ecdsa_key_create);
100 WRITE_BENCHMARK_OP(ecdsa_verify); 100 WRITE_BENCHMARK_OP (ecdsa_key_get_public);
101 WRITE_BENCHMARK_OP(eddsa_ecdh); 101 WRITE_BENCHMARK_OP (ecdsa_sign);
102 WRITE_BENCHMARK_OP(eddsa_key_create); 102 WRITE_BENCHMARK_OP (ecdsa_verify);
103 WRITE_BENCHMARK_OP(eddsa_key_get_public); 103 WRITE_BENCHMARK_OP (eddsa_ecdh);
104 WRITE_BENCHMARK_OP(eddsa_sign); 104 WRITE_BENCHMARK_OP (eddsa_key_create);
105 WRITE_BENCHMARK_OP(eddsa_verify); 105 WRITE_BENCHMARK_OP (eddsa_key_get_public);
106 WRITE_BENCHMARK_OP(hash); 106 WRITE_BENCHMARK_OP (eddsa_sign);
107 WRITE_BENCHMARK_OP(hash_context_finish); 107 WRITE_BENCHMARK_OP (eddsa_verify);
108 WRITE_BENCHMARK_OP(hash_context_read); 108 WRITE_BENCHMARK_OP (hash);
109 WRITE_BENCHMARK_OP(hash_context_start); 109 WRITE_BENCHMARK_OP (hash_context_finish);
110 WRITE_BENCHMARK_OP(hkdf); 110 WRITE_BENCHMARK_OP (hash_context_read);
111 WRITE_BENCHMARK_OP(rsa_blind); 111 WRITE_BENCHMARK_OP (hash_context_start);
112 WRITE_BENCHMARK_OP(rsa_private_key_create); 112 WRITE_BENCHMARK_OP (hkdf);
113 WRITE_BENCHMARK_OP(rsa_private_key_get_public); 113 WRITE_BENCHMARK_OP (rsa_blind);
114 WRITE_BENCHMARK_OP(rsa_sign_blinded); 114 WRITE_BENCHMARK_OP (rsa_private_key_create);
115 WRITE_BENCHMARK_OP(rsa_unblind); 115 WRITE_BENCHMARK_OP (rsa_private_key_get_public);
116 WRITE_BENCHMARK_OP(rsa_verify); 116 WRITE_BENCHMARK_OP (rsa_sign_blinded);
117 WRITE_BENCHMARK_OP (rsa_unblind);
118 WRITE_BENCHMARK_OP (rsa_verify);
117 119
118#undef WRITE_BENCHMARK_OP 120#undef WRITE_BENCHMARK_OP
119 121
120 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); 122 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
121 123
122 GNUNET_asprintf(&s, "%s/gnunet-benchmark-urls-%s-%llu-%llu.txt", 124 GNUNET_asprintf (&s, "%s/gnunet-benchmark-urls-%s-%llu-%llu.txt",
123 benchmark_dir, 125 benchmark_dir,
124 (pid == tid) ? "main" : "thread", 126 (pid == tid) ? "main" : "thread",
125 (unsigned long long)pid, 127 (unsigned long long) pid,
126 (unsigned long long)tid); 128 (unsigned long long) tid);
127 129
128 fh = GNUNET_DISK_file_open(s, 130 fh = GNUNET_DISK_file_open (s,
129 (GNUNET_DISK_OPEN_WRITE | 131 (GNUNET_DISK_OPEN_WRITE
130 GNUNET_DISK_OPEN_TRUNCATE | 132 | GNUNET_DISK_OPEN_TRUNCATE
131 GNUNET_DISK_OPEN_CREATE), 133 | GNUNET_DISK_OPEN_CREATE),
132 (GNUNET_DISK_PERM_USER_READ | 134 (GNUNET_DISK_PERM_USER_READ
133 GNUNET_DISK_PERM_USER_WRITE)); 135 | GNUNET_DISK_PERM_USER_WRITE));
134 GNUNET_assert(NULL != fh); 136 GNUNET_assert (NULL != fh);
135 GNUNET_free(s); 137 GNUNET_free (s);
136 138
137 for (unsigned int i = 0; i < bd->urd_len; i++) 139 for (unsigned int i = 0; i < bd->urd_len; i++)
138 { 140 {
139 struct UrlRequestData *urd = &bd->urd[i]; 141 struct UrlRequestData *urd = &bd->urd[i];
140 GNUNET_asprintf(&s, "url %s status %u count %llu time_us %llu time_us_max %llu bytes_sent %llu bytes_received %llu\n", 142 GNUNET_asprintf (&s,
141 urd->request_url, 143 "url %s status %u count %llu time_us %llu time_us_max %llu bytes_sent %llu bytes_received %llu\n",
142 urd->status, 144 urd->request_url,
143 (unsigned long long)urd->count, 145 urd->status,
144 (unsigned long long)urd->time.rel_value_us, 146 (unsigned long long) urd->count,
145 (unsigned long long)urd->time_max.rel_value_us, 147 (unsigned long long) urd->time.rel_value_us,
146 (unsigned long long)urd->bytes_sent, 148 (unsigned long long) urd->time_max.rel_value_us,
147 (unsigned long long)urd->bytes_received); 149 (unsigned long long) urd->bytes_sent,
148 GNUNET_assert(GNUNET_SYSERR != GNUNET_DISK_file_write_blocking(fh, s, strlen(s))); 150 (unsigned long long) urd->bytes_received);
149 GNUNET_free(s); 151 GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s,
150 } 152 strlen (
153 s)));
154 GNUNET_free (s);
155 }
151 156
152 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); 157 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
153} 158}
154 159
155 160
@@ -157,13 +162,13 @@ write_benchmark_data(struct BenchmarkData *bd)
157 * Called when the main thread exits and benchmark data for it was created. 162 * Called when the main thread exits and benchmark data for it was created.
158 */ 163 */
159static void 164static void
160main_thread_destructor() 165main_thread_destructor ()
161{ 166{
162 struct BenchmarkData *bd; 167 struct BenchmarkData *bd;
163 168
164 bd = pthread_getspecific(key); 169 bd = pthread_getspecific (key);
165 if (NULL != bd) 170 if (NULL != bd)
166 write_benchmark_data(bd); 171 write_benchmark_data (bd);
167} 172}
168 173
169 174
@@ -173,16 +178,16 @@ main_thread_destructor()
173 * @param cls closure 178 * @param cls closure
174 */ 179 */
175static void 180static void
176thread_destructor(void *cls) 181thread_destructor (void *cls)
177{ 182{
178 struct BenchmarkData *bd = cls; 183 struct BenchmarkData *bd = cls;
179 184
180 // main thread will be handled by atexit 185 // main thread will be handled by atexit
181 if (getpid() == (pid_t)syscall(SYS_gettid)) 186 if (getpid () == (pid_t) syscall (SYS_gettid))
182 return; 187 return;
183 188
184 GNUNET_assert(NULL != bd); 189 GNUNET_assert (NULL != bd);
185 write_benchmark_data(bd); 190 write_benchmark_data (bd);
186} 191}
187 192
188 193
@@ -190,9 +195,9 @@ thread_destructor(void *cls)
190 * Initialize the thread-local variable key for benchmark data. 195 * Initialize the thread-local variable key for benchmark data.
191 */ 196 */
192static void 197static void
193make_key() 198make_key ()
194{ 199{
195 (void)pthread_key_create(&key, &thread_destructor); 200 (void) pthread_key_create (&key, &thread_destructor);
196} 201}
197 202
198 203
@@ -203,22 +208,22 @@ make_key()
203 * @return benchmark data for the current thread 208 * @return benchmark data for the current thread
204 */ 209 */
205struct BenchmarkData * 210struct BenchmarkData *
206get_benchmark_data(void) 211get_benchmark_data (void)
207{ 212{
208 struct BenchmarkData *bd; 213 struct BenchmarkData *bd;
209 214
210 (void)pthread_once(&key_once, &make_key); 215 (void) pthread_once (&key_once, &make_key);
211 216
212 if (NULL == (bd = pthread_getspecific(key))) 217 if (NULL == (bd = pthread_getspecific (key)))
218 {
219 bd = GNUNET_new (struct BenchmarkData);
220 (void) pthread_setspecific (key, bd);
221 if (getpid () == (pid_t) syscall (SYS_gettid))
213 { 222 {
214 bd = GNUNET_new(struct BenchmarkData); 223 // We're the main thread!
215 (void)pthread_setspecific(key, bd); 224 atexit (main_thread_destructor);
216 if (getpid() == (pid_t)syscall(SYS_gettid))
217 {
218 // We're the main thread!
219 atexit(main_thread_destructor);
220 }
221 } 225 }
226 }
222 return bd; 227 return bd;
223} 228}
224 229
@@ -233,53 +238,54 @@ get_benchmark_data(void)
233 * @param status http status code 238 * @param status http status code
234 */ 239 */
235struct UrlRequestData * 240struct UrlRequestData *
236get_url_benchmark_data(char *url, unsigned int status) 241get_url_benchmark_data (char *url, unsigned int status)
237{ 242{
238 char trunc[MAX_BENCHMARK_URL_LEN]; 243 char trunc[MAX_BENCHMARK_URL_LEN];
239 struct BenchmarkData *bd; 244 struct BenchmarkData *bd;
240 245
241 if (NULL == url) 246 if (NULL == url)
242 { 247 {
243 /* Should not happen unless curl barfs */ 248 /* Should not happen unless curl barfs */
244 GNUNET_break(0); 249 GNUNET_break (0);
245 url = "<empty>"; 250 url = "<empty>";
246 } 251 }
247 252
248 memcpy(trunc, url, MAX_BENCHMARK_URL_LEN); 253 memcpy (trunc, url, MAX_BENCHMARK_URL_LEN);
249 trunc[MAX_BENCHMARK_URL_LEN - 1] = 0; 254 trunc[MAX_BENCHMARK_URL_LEN - 1] = 0;
250 255
251 /* We're not interested in what's after the query string */ 256 /* We're not interested in what's after the query string */
252 for (size_t i = 0; i < strlen(trunc); i++) 257 for (size_t i = 0; i < strlen (trunc); i++)
258 {
259 if (trunc[i] == '?')
253 { 260 {
254 if (trunc[i] == '?') 261 trunc[i] = 0;
255 { 262 break;
256 trunc[i] = 0;
257 break;
258 }
259 } 263 }
264 }
260 265
261 bd = get_benchmark_data(); 266 bd = get_benchmark_data ();
262 267
263 GNUNET_assert(bd->urd_len <= bd->urd_capacity); 268 GNUNET_assert (bd->urd_len <= bd->urd_capacity);
264 269
265 for (unsigned int i = 0; i < bd->urd_len; i++) 270 for (unsigned int i = 0; i < bd->urd_len; i++)
266 { 271 {
267 if ((0 == strcmp(trunc, bd->urd[i].request_url)) && 272 if ((0 == strcmp (trunc, bd->urd[i].request_url)) &&
268 (bd->urd[i].status == status)) 273 (bd->urd[i].status == status))
269 return &bd->urd[i]; 274 return &bd->urd[i];
270 } 275 }
271 276
272 { 277 {
273 struct UrlRequestData urd = { 0 }; 278 struct UrlRequestData urd = { 0 };
274 279
275 memcpy(&urd.request_url, trunc, MAX_BENCHMARK_URL_LEN); 280 memcpy (&urd.request_url, trunc, MAX_BENCHMARK_URL_LEN);
276 urd.status = status; 281 urd.status = status;
277 282
278 if (bd->urd_len == bd->urd_capacity) 283 if (bd->urd_len == bd->urd_capacity)
279 { 284 {
280 bd->urd_capacity = 2 * (bd->urd_capacity + 1); 285 bd->urd_capacity = 2 * (bd->urd_capacity + 1);
281 bd->urd = GNUNET_realloc(bd->urd, bd->urd_capacity * sizeof(struct UrlRequestData)); 286 bd->urd = GNUNET_realloc (bd->urd, bd->urd_capacity * sizeof(struct
282 } 287 UrlRequestData));
288 }
283 289
284 bd->urd[bd->urd_len++] = urd; 290 bd->urd[bd->urd_len++] = urd;
285 return &bd->urd[bd->urd_len - 1]; 291 return &bd->urd[bd->urd_len - 1];
diff --git a/src/util/benchmark.h b/src/util/benchmark.h
index 6816b47af..f4eb32191 100644
--- a/src/util/benchmark.h
+++ b/src/util/benchmark.h
@@ -37,18 +37,22 @@
37 37
38#if ENABLE_BENCHMARK 38#if ENABLE_BENCHMARK
39#define BENCHMARK_START(opname) \ 39#define BENCHMARK_START(opname) \
40 struct GNUNET_TIME_Absolute _benchmark_ ## opname ## _start = GNUNET_TIME_absolute_get() 40 struct GNUNET_TIME_Absolute _benchmark_ ## opname ## _start = \
41 GNUNET_TIME_absolute_get ()
41#define BENCHMARK_END(opname) do { \ 42#define BENCHMARK_END(opname) do { \
42 { \ 43 { \
43 struct GNUNET_TIME_Absolute _benchmark_ ## opname ## _end = GNUNET_TIME_absolute_get(); \ 44 struct GNUNET_TIME_Absolute _benchmark_ ## opname ## _end = \
44 struct BenchmarkData *bd = get_benchmark_data(); \ 45 GNUNET_TIME_absolute_get (); \
45 bd->opname ## _count++; \ 46 struct BenchmarkData *bd = get_benchmark_data (); \
46 bd->opname ## _time = \ 47 bd->opname ## _count++; \
47 GNUNET_TIME_relative_add(bd->opname ## _time, \ 48 bd->opname ## _time = \
48 GNUNET_TIME_absolute_get_difference(_benchmark_ ## opname ## _start, \ 49 GNUNET_TIME_relative_add (bd->opname ## _time, \
49 _benchmark_ ## opname ## _end)); \ 50 GNUNET_TIME_absolute_get_difference ( \
50 } \ 51 _benchmark_ ## opname ## _start, \
51 } while (0) 52 _benchmark_ \
53 ## opname ## _end)); \
54 } \
55} while (0)
52#else 56#else
53#define BENCHMARK_START(opname) do { } while (0) 57#define BENCHMARK_START(opname) do { } while (0)
54#define BENCHMARK_END(opname) do { } while (0) 58#define BENCHMARK_END(opname) do { } while (0)
@@ -58,7 +62,8 @@
58/** 62/**
59 * Struct for benchmark data for one URL. 63 * Struct for benchmark data for one URL.
60 */ 64 */
61struct UrlRequestData { 65struct UrlRequestData
66{
62 /** 67 /**
63 * Request URL, truncated (but 0-terminated). 68 * Request URL, truncated (but 0-terminated).
64 */ 69 */
@@ -107,32 +112,33 @@ struct UrlRequestData {
107/** 112/**
108 * Thread-local struct for benchmarking data. 113 * Thread-local struct for benchmarking data.
109 */ 114 */
110struct BenchmarkData { 115struct BenchmarkData
111 GNUNET_DECLARE_BENCHMARK_OP(ecc_ecdh); 116{
112 GNUNET_DECLARE_BENCHMARK_OP(ecdh_eddsa); 117 GNUNET_DECLARE_BENCHMARK_OP (ecc_ecdh);
113 GNUNET_DECLARE_BENCHMARK_OP(ecdhe_key_create); 118 GNUNET_DECLARE_BENCHMARK_OP (ecdh_eddsa);
114 GNUNET_DECLARE_BENCHMARK_OP(ecdhe_key_get_public); 119 GNUNET_DECLARE_BENCHMARK_OP (ecdhe_key_create);
115 GNUNET_DECLARE_BENCHMARK_OP(ecdsa_ecdh); 120 GNUNET_DECLARE_BENCHMARK_OP (ecdhe_key_get_public);
116 GNUNET_DECLARE_BENCHMARK_OP(ecdsa_key_create); 121 GNUNET_DECLARE_BENCHMARK_OP (ecdsa_ecdh);
117 GNUNET_DECLARE_BENCHMARK_OP(ecdsa_key_get_public); 122 GNUNET_DECLARE_BENCHMARK_OP (ecdsa_key_create);
118 GNUNET_DECLARE_BENCHMARK_OP(ecdsa_sign); 123 GNUNET_DECLARE_BENCHMARK_OP (ecdsa_key_get_public);
119 GNUNET_DECLARE_BENCHMARK_OP(ecdsa_verify); 124 GNUNET_DECLARE_BENCHMARK_OP (ecdsa_sign);
120 GNUNET_DECLARE_BENCHMARK_OP(eddsa_ecdh); 125 GNUNET_DECLARE_BENCHMARK_OP (ecdsa_verify);
121 GNUNET_DECLARE_BENCHMARK_OP(eddsa_key_create); 126 GNUNET_DECLARE_BENCHMARK_OP (eddsa_ecdh);
122 GNUNET_DECLARE_BENCHMARK_OP(eddsa_key_get_public); 127 GNUNET_DECLARE_BENCHMARK_OP (eddsa_key_create);
123 GNUNET_DECLARE_BENCHMARK_OP(eddsa_sign); 128 GNUNET_DECLARE_BENCHMARK_OP (eddsa_key_get_public);
124 GNUNET_DECLARE_BENCHMARK_OP(eddsa_verify); 129 GNUNET_DECLARE_BENCHMARK_OP (eddsa_sign);
125 GNUNET_DECLARE_BENCHMARK_OP(hash); 130 GNUNET_DECLARE_BENCHMARK_OP (eddsa_verify);
126 GNUNET_DECLARE_BENCHMARK_OP(hash_context_finish); 131 GNUNET_DECLARE_BENCHMARK_OP (hash);
127 GNUNET_DECLARE_BENCHMARK_OP(hash_context_read); 132 GNUNET_DECLARE_BENCHMARK_OP (hash_context_finish);
128 GNUNET_DECLARE_BENCHMARK_OP(hash_context_start); 133 GNUNET_DECLARE_BENCHMARK_OP (hash_context_read);
129 GNUNET_DECLARE_BENCHMARK_OP(hkdf); 134 GNUNET_DECLARE_BENCHMARK_OP (hash_context_start);
130 GNUNET_DECLARE_BENCHMARK_OP(rsa_blind); 135 GNUNET_DECLARE_BENCHMARK_OP (hkdf);
131 GNUNET_DECLARE_BENCHMARK_OP(rsa_private_key_create); 136 GNUNET_DECLARE_BENCHMARK_OP (rsa_blind);
132 GNUNET_DECLARE_BENCHMARK_OP(rsa_private_key_get_public); 137 GNUNET_DECLARE_BENCHMARK_OP (rsa_private_key_create);
133 GNUNET_DECLARE_BENCHMARK_OP(rsa_sign_blinded); 138 GNUNET_DECLARE_BENCHMARK_OP (rsa_private_key_get_public);
134 GNUNET_DECLARE_BENCHMARK_OP(rsa_unblind); 139 GNUNET_DECLARE_BENCHMARK_OP (rsa_sign_blinded);
135 GNUNET_DECLARE_BENCHMARK_OP(rsa_verify); 140 GNUNET_DECLARE_BENCHMARK_OP (rsa_unblind);
141 GNUNET_DECLARE_BENCHMARK_OP (rsa_verify);
136 142
137 struct UrlRequestData *urd; 143 struct UrlRequestData *urd;
138 144
@@ -151,7 +157,7 @@ struct BenchmarkData {
151 * @return benchmark data for the current thread 157 * @return benchmark data for the current thread
152 */ 158 */
153struct BenchmarkData * 159struct BenchmarkData *
154get_benchmark_data(void); 160get_benchmark_data (void);
155 161
156/** 162/**
157 * Get benchmark data for a URL. If the URL is too long, it's truncated 163 * Get benchmark data for a URL. If the URL is too long, it's truncated
@@ -163,6 +169,6 @@ get_benchmark_data(void);
163 * @param status http status code 169 * @param status http status code
164 */ 170 */
165struct UrlRequestData * 171struct UrlRequestData *
166get_url_benchmark_data(char *url, unsigned int status); 172get_url_benchmark_data (char *url, unsigned int status);
167 173
168#endif /* BENCHMARK_H_ */ 174#endif /* BENCHMARK_H_ */
diff --git a/src/util/bio.c b/src/util/bio.c
index 5cbeee084..e05258f73 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -25,7 +25,7 @@
25#include "platform.h" 25#include "platform.h"
26#include "gnunet_util_lib.h" 26#include "gnunet_util_lib.h"
27 27
28#define LOG(kind, ...) GNUNET_log_from(kind, "util-bio", __VA_ARGS__) 28#define LOG(kind, ...) GNUNET_log_from (kind, "util-bio", __VA_ARGS__)
29 29
30#ifndef PATH_MAX 30#ifndef PATH_MAX
31/** 31/**
@@ -50,7 +50,8 @@
50/** 50/**
51 * Handle for buffered reading. 51 * Handle for buffered reading.
52 */ 52 */
53struct GNUNET_BIO_ReadHandle { 53struct GNUNET_BIO_ReadHandle
54{
54 /** 55 /**
55 * Underlying file abstraction. 56 * Underlying file abstraction.
56 */ 57 */
@@ -90,16 +91,16 @@ struct GNUNET_BIO_ReadHandle {
90 * @return IO handle on success, NULL on error 91 * @return IO handle on success, NULL on error
91 */ 92 */
92struct GNUNET_BIO_ReadHandle * 93struct GNUNET_BIO_ReadHandle *
93GNUNET_BIO_read_open(const char *fn) 94GNUNET_BIO_read_open (const char *fn)
94{ 95{
95 struct GNUNET_DISK_FileHandle *fd; 96 struct GNUNET_DISK_FileHandle *fd;
96 struct GNUNET_BIO_ReadHandle *h; 97 struct GNUNET_BIO_ReadHandle *h;
97 98
98 fd = GNUNET_DISK_file_open(fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); 99 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
99 if (NULL == fd) 100 if (NULL == fd)
100 return NULL; 101 return NULL;
101 h = GNUNET_malloc(sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); 102 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
102 h->buffer = (char *)&h[1]; 103 h->buffer = (char *) &h[1];
103 h->size = BIO_BUFFER_SIZE; 104 h->size = BIO_BUFFER_SIZE;
104 h->fd = fd; 105 h->fd = fd;
105 return h; 106 return h;
@@ -115,7 +116,7 @@ GNUNET_BIO_read_open(const char *fn)
115 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 116 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
116 */ 117 */
117int 118int
118GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg) 119GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
119{ 120{
120 int err; 121 int err;
121 122
@@ -123,9 +124,9 @@ GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
123 if (emsg != NULL) 124 if (emsg != NULL)
124 *emsg = h->emsg; 125 *emsg = h->emsg;
125 else 126 else
126 GNUNET_free_non_null(h->emsg); 127 GNUNET_free_non_null (h->emsg);
127 GNUNET_DISK_file_close(h->fd); 128 GNUNET_DISK_file_close (h->fd);
128 GNUNET_free(h); 129 GNUNET_free (h);
129 return err; 130 return err;
130} 131}
131 132
@@ -140,10 +141,10 @@ GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
140 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 141 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
141 */ 142 */
142int 143int
143GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, 144GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
144 const char *what, 145 const char *what,
145 void *result, 146 void *result,
146 size_t len) 147 size_t len)
147{ 148{
148 char *dst = result; 149 char *dst = result;
149 size_t min; 150 size_t min;
@@ -154,41 +155,41 @@ GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h,
154 return GNUNET_SYSERR; 155 return GNUNET_SYSERR;
155 pos = 0; 156 pos = 0;
156 do 157 do
158 {
159 /* first, use buffer */
160 min = h->have - h->pos;
161 if (min > 0)
157 { 162 {
158 /* first, use buffer */ 163 if (min > len - pos)
159 min = h->have - h->pos; 164 min = len - pos;
160 if (min > 0) 165 GNUNET_memcpy (&dst[pos], &h->buffer[h->pos], min);
161 { 166 h->pos += min;
162 if (min > len - pos) 167 pos += min;
163 min = len - pos;
164 GNUNET_memcpy(&dst[pos], &h->buffer[h->pos], min);
165 h->pos += min;
166 pos += min;
167 }
168 if (pos == len)
169 return GNUNET_OK; /* done! */
170 GNUNET_assert(((off_t)h->have) == h->pos);
171 /* fill buffer */
172 ret = GNUNET_DISK_file_read(h->fd, h->buffer, h->size);
173 if (-1 == ret)
174 {
175 GNUNET_asprintf(&h->emsg,
176 _("Error reading `%s': %s"),
177 what,
178 strerror(errno));
179 return GNUNET_SYSERR;
180 }
181 if (0 == ret)
182 {
183 GNUNET_asprintf(&h->emsg,
184 _("Error reading `%s': %s"),
185 what,
186 _("End of file"));
187 return GNUNET_SYSERR;
188 }
189 h->pos = 0;
190 h->have = ret;
191 } 168 }
169 if (pos == len)
170 return GNUNET_OK; /* done! */
171 GNUNET_assert (((off_t) h->have) == h->pos);
172 /* fill buffer */
173 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
174 if (-1 == ret)
175 {
176 GNUNET_asprintf (&h->emsg,
177 _ ("Error reading `%s': %s"),
178 what,
179 strerror (errno));
180 return GNUNET_SYSERR;
181 }
182 if (0 == ret)
183 {
184 GNUNET_asprintf (&h->emsg,
185 _ ("Error reading `%s': %s"),
186 what,
187 _ ("End of file"));
188 return GNUNET_SYSERR;
189 }
190 h->pos = 0;
191 h->have = ret;
192 }
192 while (pos < len); /* should always be true */ 193 while (pos < len); /* should always be true */
193 return GNUNET_OK; 194 return GNUNET_OK;
194} 195}
@@ -205,16 +206,16 @@ GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h,
205 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 206 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
206 */ 207 */
207int 208int
208GNUNET_BIO_read_fn(struct GNUNET_BIO_ReadHandle *h, 209GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
209 const char *file, 210 const char *file,
210 int line, 211 int line,
211 void *result, 212 void *result,
212 size_t len) 213 size_t len)
213{ 214{
214 char what[PATH_MAX + 1024]; 215 char what[PATH_MAX + 1024];
215 216
216 GNUNET_snprintf(what, sizeof(what), "%s:%d", file, line); 217 GNUNET_snprintf (what, sizeof(what), "%s:%d", file, line);
217 return GNUNET_BIO_read(h, what, result, len); 218 return GNUNET_BIO_read (h, what, result, len);
218} 219}
219 220
220 221
@@ -229,45 +230,45 @@ GNUNET_BIO_read_fn(struct GNUNET_BIO_ReadHandle *h,
229 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 230 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
230 */ 231 */
231int 232int
232GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h, 233GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
233 const char *what, 234 const char *what,
234 char **result, 235 char **result,
235 size_t max_length) 236 size_t max_length)
236{ 237{
237 char *buf; 238 char *buf;
238 uint32_t big; 239 uint32_t big;
239 240
240 if (GNUNET_OK != GNUNET_BIO_read_int32(h, &big)) 241 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
241 { 242 {
242 GNUNET_free_non_null(h->emsg); 243 GNUNET_free_non_null (h->emsg);
243 GNUNET_asprintf(&h->emsg, _("Error reading length of string `%s'"), what); 244 GNUNET_asprintf (&h->emsg, _ ("Error reading length of string `%s'"), what);
244 return GNUNET_SYSERR; 245 return GNUNET_SYSERR;
245 } 246 }
246 if (0 == big) 247 if (0 == big)
247 { 248 {
248 *result = NULL; 249 *result = NULL;
249 return GNUNET_OK; 250 return GNUNET_OK;
250 } 251 }
251 if (big > max_length) 252 if (big > max_length)
252 { 253 {
253 GNUNET_asprintf(&h->emsg, 254 GNUNET_asprintf (&h->emsg,
254 _("String `%s' longer than allowed (%u > %u)"), 255 _ ("String `%s' longer than allowed (%u > %u)"),
255 what, 256 what,
256 big, 257 big,
257 max_length); 258 max_length);
258 return GNUNET_SYSERR; 259 return GNUNET_SYSERR;
259 } 260 }
260 buf = GNUNET_malloc(big); 261 buf = GNUNET_malloc (big);
261 *result = buf; 262 *result = buf;
262 buf[--big] = '\0'; 263 buf[--big] = '\0';
263 if (0 == big) 264 if (0 == big)
264 return GNUNET_OK; 265 return GNUNET_OK;
265 if (GNUNET_OK != GNUNET_BIO_read(h, what, buf, big)) 266 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
266 { 267 {
267 GNUNET_free(buf); 268 GNUNET_free (buf);
268 *result = NULL; 269 *result = NULL;
269 return GNUNET_SYSERR; 270 return GNUNET_SYSERR;
270 } 271 }
271 return GNUNET_OK; 272 return GNUNET_OK;
272} 273}
273 274
@@ -281,44 +282,44 @@ GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h,
281 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 282 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
282 */ 283 */
283int 284int
284GNUNET_BIO_read_meta_data(struct GNUNET_BIO_ReadHandle *h, 285GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
285 const char *what, 286 const char *what,
286 struct GNUNET_CONTAINER_MetaData **result) 287 struct GNUNET_CONTAINER_MetaData **result)
287{ 288{
288 uint32_t size; 289 uint32_t size;
289 char *buf; 290 char *buf;
290 struct GNUNET_CONTAINER_MetaData *meta; 291 struct GNUNET_CONTAINER_MetaData *meta;
291 292
292 if (GNUNET_OK != GNUNET_BIO_read_int32(h, (int32_t *)&size)) 293 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, (int32_t *) &size))
293 return GNUNET_SYSERR; 294 return GNUNET_SYSERR;
294 if (size == 0) 295 if (size == 0)
295 { 296 {
296 *result = NULL; 297 *result = NULL;
297 return GNUNET_OK; 298 return GNUNET_OK;
298 } 299 }
299 if (size > MAX_META_DATA) 300 if (size > MAX_META_DATA)
300 { 301 {
301 GNUNET_asprintf(&h->emsg, 302 GNUNET_asprintf (&h->emsg,
302 _("Serialized metadata `%s' larger than allowed (%u>%u)"), 303 _ ("Serialized metadata `%s' larger than allowed (%u>%u)"),
303 what, 304 what,
304 size, 305 size,
305 MAX_META_DATA); 306 MAX_META_DATA);
306 return GNUNET_SYSERR; 307 return GNUNET_SYSERR;
307 } 308 }
308 buf = GNUNET_malloc(size); 309 buf = GNUNET_malloc (size);
309 if (GNUNET_OK != GNUNET_BIO_read(h, what, buf, size)) 310 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
310 { 311 {
311 GNUNET_free(buf); 312 GNUNET_free (buf);
312 return GNUNET_SYSERR; 313 return GNUNET_SYSERR;
313 } 314 }
314 meta = GNUNET_CONTAINER_meta_data_deserialize(buf, size); 315 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
315 if (NULL == meta) 316 if (NULL == meta)
316 { 317 {
317 GNUNET_free(buf); 318 GNUNET_free (buf);
318 GNUNET_asprintf(&h->emsg, _("Metadata `%s' failed to deserialize"), what); 319 GNUNET_asprintf (&h->emsg, _ ("Metadata `%s' failed to deserialize"), what);
319 return GNUNET_SYSERR; 320 return GNUNET_SYSERR;
320 } 321 }
321 GNUNET_free(buf); 322 GNUNET_free (buf);
322 *result = meta; 323 *result = meta;
323 return GNUNET_OK; 324 return GNUNET_OK;
324} 325}
@@ -334,16 +335,16 @@ GNUNET_BIO_read_meta_data(struct GNUNET_BIO_ReadHandle *h,
334 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 335 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
335 */ 336 */
336int 337int
337GNUNET_BIO_read_int32__(struct GNUNET_BIO_ReadHandle *h, 338GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
338 const char *file, 339 const char *file,
339 int line, 340 int line,
340 int32_t *i) 341 int32_t *i)
341{ 342{
342 int32_t big; 343 int32_t big;
343 344
344 if (GNUNET_OK != GNUNET_BIO_read_fn(h, file, line, &big, sizeof(int32_t))) 345 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof(int32_t)))
345 return GNUNET_SYSERR; 346 return GNUNET_SYSERR;
346 *i = ntohl(big); 347 *i = ntohl (big);
347 return GNUNET_OK; 348 return GNUNET_OK;
348} 349}
349 350
@@ -358,16 +359,16 @@ GNUNET_BIO_read_int32__(struct GNUNET_BIO_ReadHandle *h,
358 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 359 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
359 */ 360 */
360int 361int
361GNUNET_BIO_read_int64__(struct GNUNET_BIO_ReadHandle *h, 362GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
362 const char *file, 363 const char *file,
363 int line, 364 int line,
364 int64_t *i) 365 int64_t *i)
365{ 366{
366 int64_t big; 367 int64_t big;
367 368
368 if (GNUNET_OK != GNUNET_BIO_read_fn(h, file, line, &big, sizeof(int64_t))) 369 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof(int64_t)))
369 return GNUNET_SYSERR; 370 return GNUNET_SYSERR;
370 *i = GNUNET_ntohll(big); 371 *i = GNUNET_ntohll (big);
371 return GNUNET_OK; 372 return GNUNET_OK;
372} 373}
373 374
@@ -375,7 +376,8 @@ GNUNET_BIO_read_int64__(struct GNUNET_BIO_ReadHandle *h,
375/** 376/**
376 * Handle for buffered writing. 377 * Handle for buffered writing.
377 */ 378 */
378struct GNUNET_BIO_WriteHandle { 379struct GNUNET_BIO_WriteHandle
380{
379 /** 381 /**
380 * Underlying file handle. 382 * Underlying file handle.
381 */ 383 */
@@ -405,21 +407,21 @@ struct GNUNET_BIO_WriteHandle {
405 * @return IO handle on success, NULL on error 407 * @return IO handle on success, NULL on error
406 */ 408 */
407struct GNUNET_BIO_WriteHandle * 409struct GNUNET_BIO_WriteHandle *
408GNUNET_BIO_write_open(const char *fn) 410GNUNET_BIO_write_open (const char *fn)
409{ 411{
410 struct GNUNET_DISK_FileHandle *fd; 412 struct GNUNET_DISK_FileHandle *fd;
411 struct GNUNET_BIO_WriteHandle *h; 413 struct GNUNET_BIO_WriteHandle *h;
412 414
413 fd = 415 fd =
414 GNUNET_DISK_file_open(fn, 416 GNUNET_DISK_file_open (fn,
415 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | 417 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
416 GNUNET_DISK_OPEN_CREATE, 418 | GNUNET_DISK_OPEN_CREATE,
417 GNUNET_DISK_PERM_USER_READ | 419 GNUNET_DISK_PERM_USER_READ
418 GNUNET_DISK_PERM_USER_WRITE); 420 | GNUNET_DISK_PERM_USER_WRITE);
419 if (NULL == fd) 421 if (NULL == fd)
420 return NULL; 422 return NULL;
421 h = GNUNET_malloc(sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); 423 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
422 h->buffer = (char *)&h[1]; 424 h->buffer = (char *) &h[1];
423 h->size = BIO_BUFFER_SIZE; 425 h->size = BIO_BUFFER_SIZE;
424 h->fd = fd; 426 h->fd = fd;
425 return h; 427 return h;
@@ -433,14 +435,14 @@ GNUNET_BIO_write_open(const char *fn)
433 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 435 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
434 */ 436 */
435int 437int
436GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h) 438GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
437{ 439{
438 int ret; 440 int ret;
439 441
440 ret = GNUNET_SYSERR; 442 ret = GNUNET_SYSERR;
441 if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush(h)))) 443 if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))))
442 GNUNET_DISK_file_close(h->fd); 444 GNUNET_DISK_file_close (h->fd);
443 GNUNET_free(h); 445 GNUNET_free (h);
444 return ret; 446 return ret;
445} 447}
446 448
@@ -453,17 +455,17 @@ GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h)
453 * the file is closed 455 * the file is closed
454 */ 456 */
455int 457int
456GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h) 458GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
457{ 459{
458 ssize_t ret; 460 ssize_t ret;
459 461
460 ret = GNUNET_DISK_file_write(h->fd, h->buffer, h->have); 462 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
461 if (ret != (ssize_t)h->have) 463 if (ret != (ssize_t) h->have)
462 { 464 {
463 GNUNET_DISK_file_close(h->fd); 465 GNUNET_DISK_file_close (h->fd);
464 h->fd = NULL; 466 h->fd = NULL;
465 return GNUNET_SYSERR; /* error */ 467 return GNUNET_SYSERR; /* error */
466 } 468 }
467 h->have = 0; 469 h->have = 0;
468 return GNUNET_OK; 470 return GNUNET_OK;
469} 471}
@@ -478,9 +480,9 @@ GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h)
478 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 480 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
479 */ 481 */
480int 482int
481GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, 483GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
482 const void *buffer, 484 const void *buffer,
483 size_t n) 485 size_t n)
484{ 486{
485 const char *src = buffer; 487 const char *src = buffer;
486 size_t min; 488 size_t min;
@@ -490,22 +492,22 @@ GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h,
490 return GNUNET_SYSERR; 492 return GNUNET_SYSERR;
491 pos = 0; 493 pos = 0;
492 do 494 do
493 { 495 {
494 /* first, just use buffer */ 496 /* first, just use buffer */
495 min = h->size - h->have; 497 min = h->size - h->have;
496 if (min > n - pos) 498 if (min > n - pos)
497 min = n - pos; 499 min = n - pos;
498 GNUNET_memcpy(&h->buffer[h->have], &src[pos], min); 500 GNUNET_memcpy (&h->buffer[h->have], &src[pos], min);
499 pos += min; 501 pos += min;
500 h->have += min; 502 h->have += min;
501 if (pos == n) 503 if (pos == n)
502 return GNUNET_OK; /* done */ 504 return GNUNET_OK; /* done */
503 GNUNET_assert(h->have == h->size); 505 GNUNET_assert (h->have == h->size);
504 if (GNUNET_OK != GNUNET_BIO_flush(h)) 506 if (GNUNET_OK != GNUNET_BIO_flush (h))
505 return GNUNET_SYSERR; /* error */ 507 return GNUNET_SYSERR; /* error */
506 } 508 }
507 while (pos < n); /* should always be true */ 509 while (pos < n); /* should always be true */
508 GNUNET_break(0); 510 GNUNET_break (0);
509 return GNUNET_OK; 511 return GNUNET_OK;
510} 512}
511 513
@@ -518,15 +520,15 @@ GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h,
518 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 520 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
519 */ 521 */
520int 522int
521GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *s) 523GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
522{ 524{
523 uint32_t slen; 525 uint32_t slen;
524 526
525 slen = (uint32_t)((s == NULL) ? 0 : strlen(s) + 1); 527 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
526 if (GNUNET_OK != GNUNET_BIO_write_int32(h, slen)) 528 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
527 return GNUNET_SYSERR; 529 return GNUNET_SYSERR;
528 if (0 != slen) 530 if (0 != slen)
529 return GNUNET_BIO_write(h, s, slen - 1); 531 return GNUNET_BIO_write (h, s, slen - 1);
530 return GNUNET_OK; 532 return GNUNET_OK;
531} 533}
532 534
@@ -539,32 +541,32 @@ GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *s)
539 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 541 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
540 */ 542 */
541int 543int
542GNUNET_BIO_write_meta_data(struct GNUNET_BIO_WriteHandle *h, 544GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
543 const struct GNUNET_CONTAINER_MetaData *m) 545 const struct GNUNET_CONTAINER_MetaData *m)
544{ 546{
545 ssize_t size; 547 ssize_t size;
546 char *buf; 548 char *buf;
547 549
548 if (m == NULL) 550 if (m == NULL)
549 return GNUNET_BIO_write_int32(h, 0); 551 return GNUNET_BIO_write_int32 (h, 0);
550 buf = NULL; 552 buf = NULL;
551 size = GNUNET_CONTAINER_meta_data_serialize( 553 size = GNUNET_CONTAINER_meta_data_serialize (
552 m, 554 m,
553 &buf, 555 &buf,
554 MAX_META_DATA, 556 MAX_META_DATA,
555 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); 557 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
556 if (size == -1) 558 if (size == -1)
557 { 559 {
558 GNUNET_free(buf); 560 GNUNET_free (buf);
559 return GNUNET_SYSERR; 561 return GNUNET_SYSERR;
560 } 562 }
561 if ((GNUNET_OK != GNUNET_BIO_write_int32(h, (uint32_t)size)) || 563 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
562 (GNUNET_OK != GNUNET_BIO_write(h, buf, size))) 564 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
563 { 565 {
564 GNUNET_free(buf); 566 GNUNET_free (buf);
565 return GNUNET_SYSERR; 567 return GNUNET_SYSERR;
566 } 568 }
567 GNUNET_free(buf); 569 GNUNET_free (buf);
568 return GNUNET_OK; 570 return GNUNET_OK;
569} 571}
570 572
@@ -577,12 +579,12 @@ GNUNET_BIO_write_meta_data(struct GNUNET_BIO_WriteHandle *h,
577 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 579 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
578 */ 580 */
579int 581int
580GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, int32_t i) 582GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
581{ 583{
582 int32_t big; 584 int32_t big;
583 585
584 big = htonl(i); 586 big = htonl (i);
585 return GNUNET_BIO_write(h, &big, sizeof(int32_t)); 587 return GNUNET_BIO_write (h, &big, sizeof(int32_t));
586} 588}
587 589
588 590
@@ -594,12 +596,12 @@ GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, int32_t i)
594 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 596 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
595 */ 597 */
596int 598int
597GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, int64_t i) 599GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
598{ 600{
599 int64_t big; 601 int64_t big;
600 602
601 big = GNUNET_htonll(i); 603 big = GNUNET_htonll (i);
602 return GNUNET_BIO_write(h, &big, sizeof(int64_t)); 604 return GNUNET_BIO_write (h, &big, sizeof(int64_t));
603} 605}
604 606
605 607
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c
index d8d6d639f..358c09808 100644
--- a/src/util/common_allocation.c
+++ b/src/util/common_allocation.c
@@ -33,10 +33,10 @@
33#endif 33#endif
34 34
35#define LOG(kind, ...) \ 35#define LOG(kind, ...) \
36 GNUNET_log_from(kind, "util-common-allocation", __VA_ARGS__) 36 GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)
37 37
38#define LOG_STRERROR(kind, syscall) \ 38#define LOG_STRERROR(kind, syscall) \
39 GNUNET_log_from_strerror(kind, "util-common-allocation", syscall) 39 GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
40 40
41#ifndef INT_MAX 41#ifndef INT_MAX
42#define INT_MAX 0x7FFFFFFF 42#define INT_MAX 0x7FFFFFFF
@@ -63,19 +63,19 @@ static LONG mem_used = 0;
63 * @return pointer to size bytes of memory 63 * @return pointer to size bytes of memory
64 */ 64 */
65void * 65void *
66GNUNET_xmalloc_(size_t size, const char *filename, int linenumber) 66GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
67{ 67{
68 void *ret; 68 void *ret;
69 69
70 /* As a security precaution, we generally do not allow very large 70 /* As a security precaution, we generally do not allow very large
71 * allocations using the default 'GNUNET_malloc()' macro */ 71 * allocations using the default 'GNUNET_malloc()' macro */
72 GNUNET_assert_at(size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); 72 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
73 ret = GNUNET_xmalloc_unchecked_(size, filename, linenumber); 73 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
74 if (NULL == ret) 74 if (NULL == ret)
75 { 75 {
76 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "malloc"); 76 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
77 GNUNET_assert(0); 77 GNUNET_assert (0);
78 } 78 }
79 return ret; 79 return ret;
80} 80}
81 81
@@ -95,23 +95,23 @@ GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
95 * @return allocated memory, never NULL 95 * @return allocated memory, never NULL
96 */ 96 */
97void ** 97void **
98GNUNET_xnew_array_2d_(size_t n, 98GNUNET_xnew_array_2d_ (size_t n,
99 size_t m, 99 size_t m,
100 size_t elementSize, 100 size_t elementSize,
101 const char *filename, 101 const char *filename,
102 int linenumber) 102 int linenumber)
103{ 103{
104 /* use char pointer internally to avoid void pointer arithmetic warnings */ 104 /* use char pointer internally to avoid void pointer arithmetic warnings */
105 char **ret = GNUNET_xmalloc_(n * sizeof(void *) + /* 1. dim header */ 105 char **ret = GNUNET_xmalloc_ (n * sizeof(void *) /* 1. dim header */
106 n * m * elementSize, /* element data */ 106 + n * m * elementSize, /* element data */
107 filename, 107 filename,
108 linenumber); 108 linenumber);
109 109
110 for (size_t i = 0; i < n; i++) 110 for (size_t i = 0; i < n; i++)
111 ret[i] = (char *)ret + /* base address */ 111 ret[i] = (char *) ret /* base address */
112 n * sizeof(void *) + /* skip 1. dim header */ 112 + n * sizeof(void *) /* skip 1. dim header */
113 i * m * elementSize; /* skip to 2. dim row header */ 113 + i * m * elementSize; /* skip to 2. dim row header */
114 return (void **)ret; 114 return (void **) ret;
115} 115}
116 116
117 117
@@ -131,34 +131,34 @@ GNUNET_xnew_array_2d_(size_t n,
131 * @return allocated memory, never NULL 131 * @return allocated memory, never NULL
132 */ 132 */
133void *** 133void ***
134GNUNET_xnew_array_3d_(size_t n, 134GNUNET_xnew_array_3d_ (size_t n,
135 size_t m, 135 size_t m,
136 size_t o, 136 size_t o,
137 size_t elementSize, 137 size_t elementSize,
138 const char *filename, 138 const char *filename,
139 int linenumber) 139 int linenumber)
140{ 140{
141 /* use char pointer internally to avoid void pointer arithmetic warnings */ 141 /* use char pointer internally to avoid void pointer arithmetic warnings */
142 char ***ret = GNUNET_xmalloc_(n * sizeof(void **) + /* 1. dim header */ 142 char ***ret = GNUNET_xmalloc_ (n * sizeof(void **) /* 1. dim header */
143 n * m * sizeof(void *) + /* 2. dim header */ 143 + n * m * sizeof(void *) /* 2. dim header */
144 n * m * o * elementSize, /* element data */ 144 + n * m * o * elementSize, /* element data */
145 filename, 145 filename,
146 linenumber); 146 linenumber);
147 147
148 for (size_t i = 0; i < n; i++) 148 for (size_t i = 0; i < n; i++)
149 { 149 {
150 /* need to cast to (char *) temporarily for byte level acuracy */ 150 /* need to cast to (char *) temporarily for byte level acuracy */
151 ret[i] = (char **)((char *)ret + /* base address */ 151 ret[i] = (char **) ((char *) ret /* base address */
152 n * sizeof(void **) + /* skip 1. dim header */ 152 + n * sizeof(void **) /* skip 1. dim header */
153 i * m * sizeof(void *)); /* skip to 2. dim header */ 153 + i * m * sizeof(void *)); /* skip to 2. dim header */
154 for (size_t j = 0; j < m; j++) 154 for (size_t j = 0; j < m; j++)
155 ret[i][j] = (char *)ret + /* base address */ 155 ret[i][j] = (char *) ret /* base address */
156 n * sizeof(void **) + /* skip 1. dim header */ 156 + n * sizeof(void **) /* skip 1. dim header */
157 n * m * sizeof(void *) + /* skip 2. dim header */ 157 + n * m * sizeof(void *) /* skip 2. dim header */
158 i * m * o * elementSize + /* skip to 2. dim part */ 158 + i * m * o * elementSize /* skip to 2. dim part */
159 j * o * elementSize; /* skip to 3. dim row data */ 159 + j * o * elementSize; /* skip to 3. dim row data */
160 } 160 }
161 return (void ***)ret; 161 return (void ***) ret;
162} 162}
163 163
164 164
@@ -174,34 +174,34 @@ GNUNET_xnew_array_3d_(size_t n,
174 * @return allocated memory, never NULL 174 * @return allocated memory, never NULL
175 */ 175 */
176void * 176void *
177GNUNET_xmemdup_(const void *buf, 177GNUNET_xmemdup_ (const void *buf,
178 size_t size, 178 size_t size,
179 const char *filename, 179 const char *filename,
180 int linenumber) 180 int linenumber)
181{ 181{
182 void *ret; 182 void *ret;
183 183
184 /* As a security precaution, we generally do not allow very large 184 /* As a security precaution, we generally do not allow very large
185 * allocations here */ 185 * allocations here */
186 GNUNET_assert_at(size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); 186 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
187#ifdef W32_MEM_LIMIT 187#ifdef W32_MEM_LIMIT
188 size += sizeof(size_t); 188 size += sizeof(size_t);
189 if (mem_used + size > W32_MEM_LIMIT) 189 if (mem_used + size > W32_MEM_LIMIT)
190 return NULL; 190 return NULL;
191#endif 191#endif
192 GNUNET_assert_at(size < INT_MAX, filename, linenumber); 192 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
193 ret = malloc(size); 193 ret = malloc (size);
194 if (ret == NULL) 194 if (ret == NULL)
195 { 195 {
196 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "malloc"); 196 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
197 GNUNET_assert(0); 197 GNUNET_assert (0);
198 } 198 }
199#ifdef W32_MEM_LIMIT 199#ifdef W32_MEM_LIMIT
200 *((size_t *)ret) = size; 200 *((size_t *) ret) = size;
201 ret = &((size_t *)ret)[1]; 201 ret = &((size_t *) ret)[1];
202 mem_used += size; 202 mem_used += size;
203#endif 203#endif
204 GNUNET_memcpy(ret, buf, size); 204 GNUNET_memcpy (ret, buf, size);
205 return ret; 205 return ret;
206} 206}
207 207
@@ -216,26 +216,26 @@ GNUNET_xmemdup_(const void *buf,
216 * @return pointer to size bytes of memory, NULL if we do not have enough memory 216 * @return pointer to size bytes of memory, NULL if we do not have enough memory
217 */ 217 */
218void * 218void *
219GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber) 219GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
220{ 220{
221 void *result; 221 void *result;
222 222
223 (void)filename; 223 (void) filename;
224 (void)linenumber; 224 (void) linenumber;
225#ifdef W32_MEM_LIMIT 225#ifdef W32_MEM_LIMIT
226 size += sizeof(size_t); 226 size += sizeof(size_t);
227 if (mem_used + size > W32_MEM_LIMIT) 227 if (mem_used + size > W32_MEM_LIMIT)
228 return NULL; 228 return NULL;
229#endif 229#endif
230 230
231 result = malloc(size); 231 result = malloc (size);
232 if (NULL == result) 232 if (NULL == result)
233 return NULL; 233 return NULL;
234 memset(result, 0, size); 234 memset (result, 0, size);
235 235
236#ifdef W32_MEM_LIMIT 236#ifdef W32_MEM_LIMIT
237 *((size_t *)result) = size; 237 *((size_t *) result) = size;
238 result = &((size_t *)result)[1]; 238 result = &((size_t *) result)[1];
239 mem_used += size; 239 mem_used += size;
240#endif 240#endif
241 241
@@ -255,24 +255,24 @@ GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber)
255 * @return pointer to size bytes of memory 255 * @return pointer to size bytes of memory
256 */ 256 */
257void * 257void *
258GNUNET_xrealloc_(void *ptr, size_t n, const char *filename, int linenumber) 258GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
259{ 259{
260 (void)filename; 260 (void) filename;
261 (void)linenumber; 261 (void) linenumber;
262 262
263#ifdef W32_MEM_LIMIT 263#ifdef W32_MEM_LIMIT
264 n += sizeof(size_t); 264 n += sizeof(size_t);
265 ptr = &((size_t *)ptr)[-1]; 265 ptr = &((size_t *) ptr)[-1];
266 mem_used = mem_used - *((size_t *)ptr) + n; 266 mem_used = mem_used - *((size_t *) ptr) + n;
267#endif 267#endif
268 ptr = realloc(ptr, n); 268 ptr = realloc (ptr, n);
269 if ((NULL == ptr) && (n > 0)) 269 if ((NULL == ptr) && (n > 0))
270 { 270 {
271 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "realloc"); 271 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
272 GNUNET_assert(0); 272 GNUNET_assert (0);
273 } 273 }
274#ifdef W32_MEM_LIMIT 274#ifdef W32_MEM_LIMIT
275 ptr = &((size_t *)ptr)[1]; 275 ptr = &((size_t *) ptr)[1];
276#endif 276#endif
277 return ptr; 277 return ptr;
278} 278}
@@ -289,9 +289,9 @@ GNUNET_xrealloc_(void *ptr, size_t n, const char *filename, int linenumber)
289#include <malloc_np.h> 289#include <malloc_np.h>
290#endif 290#endif
291#if HAVE_MALLOC_USABLE_SIZE 291#if HAVE_MALLOC_USABLE_SIZE
292#define M_SIZE(p) malloc_usable_size(p) 292#define M_SIZE(p) malloc_usable_size (p)
293#elif HAVE_MALLOC_SIZE 293#elif HAVE_MALLOC_SIZE
294#define M_SIZE(p) malloc_size(p) 294#define M_SIZE(p) malloc_size (p)
295#endif 295#endif
296 296
297/** 297/**
@@ -303,28 +303,28 @@ GNUNET_xrealloc_(void *ptr, size_t n, const char *filename, int linenumber)
303 * @param linenumber where in the code was the call to GNUNET_free 303 * @param linenumber where in the code was the call to GNUNET_free
304 */ 304 */
305void 305void
306GNUNET_xfree_(void *ptr, const char *filename, int linenumber) 306GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
307{ 307{
308 GNUNET_assert_at(NULL != ptr, filename, linenumber); 308 GNUNET_assert_at (NULL != ptr, filename, linenumber);
309#ifdef W32_MEM_LIMIT 309#ifdef W32_MEM_LIMIT
310 ptr = &((size_t *)ptr)[-1]; 310 ptr = &((size_t *) ptr)[-1];
311 mem_used -= *((size_t *)ptr); 311 mem_used -= *((size_t *) ptr);
312#endif 312#endif
313#if defined(M_SIZE) 313#if defined(M_SIZE)
314#if ENABLE_POISONING 314#if ENABLE_POISONING
315 { 315 {
316 const uint64_t baadfood = GNUNET_ntohll(0xBAADF00DBAADF00DLL); 316 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
317 uint64_t *base = ptr; 317 uint64_t *base = ptr;
318 size_t s = M_SIZE(ptr); 318 size_t s = M_SIZE (ptr);
319 size_t i; 319 size_t i;
320 320
321 for (i = 0; i < s / 8; i++) 321 for (i = 0; i < s / 8; i++)
322 base[i] = baadfood; 322 base[i] = baadfood;
323 GNUNET_memcpy(&base[s / 8], &baadfood, s % 8); 323 GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
324 } 324 }
325#endif 325#endif
326#endif 326#endif
327 free(ptr); 327 free (ptr);
328} 328}
329 329
330 330
@@ -337,26 +337,26 @@ GNUNET_xfree_(void *ptr, const char *filename, int linenumber)
337 * @return `strdup(@a str)` 337 * @return `strdup(@a str)`
338 */ 338 */
339char * 339char *
340GNUNET_xstrdup_(const char *str, const char *filename, int linenumber) 340GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
341{ 341{
342 char *res; 342 char *res;
343 size_t slen; 343 size_t slen;
344 344
345 GNUNET_assert_at(str != NULL, filename, linenumber); 345 GNUNET_assert_at (str != NULL, filename, linenumber);
346 slen = strlen(str) + 1; 346 slen = strlen (str) + 1;
347 res = GNUNET_xmalloc_(slen, filename, linenumber); 347 res = GNUNET_xmalloc_ (slen, filename, linenumber);
348 GNUNET_memcpy(res, str, slen); 348 GNUNET_memcpy (res, str, slen);
349 return res; 349 return res;
350} 350}
351 351
352 352
353#if !HAVE_STRNLEN 353#if ! HAVE_STRNLEN
354static size_t 354static size_t
355strnlen(const char *s, size_t n) 355strnlen (const char *s, size_t n)
356{ 356{
357 const char *e; 357 const char *e;
358 358
359 e = memchr(s, '\0', n); 359 e = memchr (s, '\0', n);
360 if (NULL == e) 360 if (NULL == e)
361 return n; 361 return n;
362 return e - s; 362 return e - s;
@@ -374,19 +374,19 @@ strnlen(const char *s, size_t n)
374 * @return `strndup(@a str,@a len)` 374 * @return `strndup(@a str,@a len)`
375 */ 375 */
376char * 376char *
377GNUNET_xstrndup_(const char *str, 377GNUNET_xstrndup_ (const char *str,
378 size_t len, 378 size_t len,
379 const char *filename, 379 const char *filename,
380 int linenumber) 380 int linenumber)
381{ 381{
382 char *res; 382 char *res;
383 383
384 if (0 == len) 384 if (0 == len)
385 return GNUNET_strdup(""); 385 return GNUNET_strdup ("");
386 GNUNET_assert_at(NULL != str, filename, linenumber); 386 GNUNET_assert_at (NULL != str, filename, linenumber);
387 len = strnlen(str, len); 387 len = strnlen (str, len);
388 res = GNUNET_xmalloc_(len + 1, filename, linenumber); 388 res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
389 GNUNET_memcpy(res, str, len); 389 GNUNET_memcpy (res, str, len);
390 /* res[len] = '\0'; 'malloc' zeros out anyway */ 390 /* res[len] = '\0'; 'malloc' zeros out anyway */
391 return res; 391 return res;
392} 392}
@@ -405,35 +405,35 @@ GNUNET_xstrndup_(const char *str,
405 * @param linenumber where in the code was the call to GNUNET_array_grow() 405 * @param linenumber where in the code was the call to GNUNET_array_grow()
406 */ 406 */
407void 407void
408GNUNET_xgrow_(void **old, 408GNUNET_xgrow_ (void **old,
409 size_t elementSize, 409 size_t elementSize,
410 unsigned int *oldCount, 410 unsigned int *oldCount,
411 unsigned int newCount, 411 unsigned int newCount,
412 const char *filename, 412 const char *filename,
413 int linenumber) 413 int linenumber)
414{ 414{
415 void *tmp; 415 void *tmp;
416 size_t size; 416 size_t size;
417 417
418 GNUNET_assert_at(INT_MAX / elementSize > newCount, filename, linenumber); 418 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
419 size = newCount * elementSize; 419 size = newCount * elementSize;
420 if (0 == size) 420 if (0 == size)
421 { 421 {
422 tmp = NULL; 422 tmp = NULL;
423 } 423 }
424 else 424 else
425 {
426 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
427 if (NULL != *old)
425 { 428 {
426 tmp = GNUNET_xmalloc_(size, filename, linenumber); 429 GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN (*oldCount, newCount));
427 if (NULL != *old)
428 {
429 GNUNET_memcpy(tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount));
430 }
431 } 430 }
431 }
432 432
433 if (NULL != *old) 433 if (NULL != *old)
434 { 434 {
435 GNUNET_xfree_(*old, filename, linenumber); 435 GNUNET_xfree_ (*old, filename, linenumber);
436 } 436 }
437 *old = tmp; 437 *old = tmp;
438 *oldCount = newCount; 438 *oldCount = newCount;
439} 439}
@@ -448,19 +448,19 @@ GNUNET_xgrow_(void **old,
448 * @return number of bytes in `*@a buf`, excluding 0-termination 448 * @return number of bytes in `*@a buf`, excluding 0-termination
449 */ 449 */
450int 450int
451GNUNET_asprintf(char **buf, const char *format, ...) 451GNUNET_asprintf (char **buf, const char *format, ...)
452{ 452{
453 int ret; 453 int ret;
454 va_list args; 454 va_list args;
455 455
456 va_start(args, format); 456 va_start (args, format);
457 ret = vsnprintf(NULL, 0, format, args); 457 ret = vsnprintf (NULL, 0, format, args);
458 va_end(args); 458 va_end (args);
459 GNUNET_assert(ret >= 0); 459 GNUNET_assert (ret >= 0);
460 *buf = GNUNET_malloc(ret + 1); 460 *buf = GNUNET_malloc (ret + 1);
461 va_start(args, format); 461 va_start (args, format);
462 ret = vsprintf(*buf, format, args); 462 ret = vsprintf (*buf, format, args);
463 va_end(args); 463 va_end (args);
464 return ret; 464 return ret;
465} 465}
466 466
@@ -475,15 +475,15 @@ GNUNET_asprintf(char **buf, const char *format, ...)
475 * @return number of bytes written to buf or negative value on error 475 * @return number of bytes written to buf or negative value on error
476 */ 476 */
477int 477int
478GNUNET_snprintf(char *buf, size_t size, const char *format, ...) 478GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
479{ 479{
480 int ret; 480 int ret;
481 va_list args; 481 va_list args;
482 482
483 va_start(args, format); 483 va_start (args, format);
484 ret = vsnprintf(buf, size, format, args); 484 ret = vsnprintf (buf, size, format, args);
485 va_end(args); 485 va_end (args);
486 GNUNET_assert((ret >= 0) && (((size_t)ret) < size)); 486 GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
487 return ret; 487 return ret;
488} 488}
489 489
@@ -495,15 +495,15 @@ GNUNET_snprintf(char *buf, size_t size, const char *format, ...)
495 * @return duplicate of the message 495 * @return duplicate of the message
496 */ 496 */
497struct GNUNET_MessageHeader * 497struct GNUNET_MessageHeader *
498GNUNET_copy_message(const struct GNUNET_MessageHeader *msg) 498GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
499{ 499{
500 struct GNUNET_MessageHeader *ret; 500 struct GNUNET_MessageHeader *ret;
501 uint16_t msize; 501 uint16_t msize;
502 502
503 msize = ntohs(msg->size); 503 msize = ntohs (msg->size);
504 GNUNET_assert(msize >= sizeof(struct GNUNET_MessageHeader)); 504 GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
505 ret = GNUNET_malloc(msize); 505 ret = GNUNET_malloc (msize);
506 GNUNET_memcpy(ret, msg, msize); 506 GNUNET_memcpy (ret, msg, msize);
507 return ret; 507 return ret;
508} 508}
509 509
diff --git a/src/util/common_endian.c b/src/util/common_endian.c
index 6ec203dd9..fa7d99d85 100644
--- a/src/util/common_endian.c
+++ b/src/util/common_endian.c
@@ -28,16 +28,16 @@
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_crypto_lib.h" 29#include "gnunet_crypto_lib.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-common-endian", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-common-endian", __VA_ARGS__)
32 32
33 33
34uint64_t 34uint64_t
35GNUNET_htonll(uint64_t n) 35GNUNET_htonll (uint64_t n)
36{ 36{
37#if __BYTE_ORDER == __BIG_ENDIAN 37#if __BYTE_ORDER == __BIG_ENDIAN
38 return n; 38 return n;
39#elif __BYTE_ORDER == __LITTLE_ENDIAN 39#elif __BYTE_ORDER == __LITTLE_ENDIAN
40 return (((uint64_t)htonl(n)) << 32) + htonl(n >> 32); 40 return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32);
41#else 41#else
42 #error byteorder undefined 42 #error byteorder undefined
43#endif 43#endif
@@ -45,12 +45,12 @@ GNUNET_htonll(uint64_t n)
45 45
46 46
47uint64_t 47uint64_t
48GNUNET_ntohll(uint64_t n) 48GNUNET_ntohll (uint64_t n)
49{ 49{
50#if __BYTE_ORDER == __BIG_ENDIAN 50#if __BYTE_ORDER == __BIG_ENDIAN
51 return n; 51 return n;
52#elif __BYTE_ORDER == __LITTLE_ENDIAN 52#elif __BYTE_ORDER == __LITTLE_ENDIAN
53 return (((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32); 53 return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32);
54#else 54#else
55 #error byteorder undefined 55 #error byteorder undefined
56#endif 56#endif
@@ -63,13 +63,13 @@ GNUNET_ntohll(uint64_t n)
63 * @return the same value in host byte order 63 * @return the same value in host byte order
64 */ 64 */
65double 65double
66GNUNET_hton_double(double d) 66GNUNET_hton_double (double d)
67{ 67{
68 double res; 68 double res;
69 uint64_t *in = (uint64_t *)&d; 69 uint64_t *in = (uint64_t *) &d;
70 uint64_t *out = (uint64_t *)&res; 70 uint64_t *out = (uint64_t *) &res;
71 71
72 out[0] = GNUNET_htonll(in[0]); 72 out[0] = GNUNET_htonll (in[0]);
73 73
74 return res; 74 return res;
75} 75}
@@ -81,13 +81,13 @@ GNUNET_hton_double(double d)
81 * @return the same value in host byte order 81 * @return the same value in host byte order
82 */ 82 */
83double 83double
84GNUNET_ntoh_double(double d) 84GNUNET_ntoh_double (double d)
85{ 85{
86 double res; 86 double res;
87 uint64_t *in = (uint64_t *)&d; 87 uint64_t *in = (uint64_t *) &d;
88 uint64_t *out = (uint64_t *)&res; 88 uint64_t *out = (uint64_t *) &res;
89 89
90 out[0] = GNUNET_ntohll(in[0]); 90 out[0] = GNUNET_ntohll (in[0]);
91 91
92 return res; 92 return res;
93} 93}
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index 7a2d5a0b5..3ff9bf18e 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -77,7 +77,8 @@
77/** 77/**
78 * Linked list of active loggers. 78 * Linked list of active loggers.
79 */ 79 */
80struct CustomLogger { 80struct CustomLogger
81{
81 /** 82 /**
82 * This is a linked list. 83 * This is a linked list.
83 */ 84 */
@@ -166,7 +167,8 @@ static FILE *GNUNET_stderr;
166/** 167/**
167 * Represents a single logging definition 168 * Represents a single logging definition
168 */ 169 */
169struct LogDef { 170struct LogDef
171{
170 /** 172 /**
171 * Component name regex 173 * Component name regex
172 */ 174 */
@@ -209,7 +211,7 @@ struct LogDef {
209}; 211};
210 212
211 213
212#if !defined(GNUNET_CULL_LOGGING) 214#if ! defined(GNUNET_CULL_LOGGING)
213/** 215/**
214 * Dynamic array of logging definitions 216 * Dynamic array of logging definitions
215 */ 217 */
@@ -250,21 +252,21 @@ static int gnunet_force_log_present;
250 * @return GNUNET_GE_INVALID if log does not parse 252 * @return GNUNET_GE_INVALID if log does not parse
251 */ 253 */
252static enum GNUNET_ErrorType 254static enum GNUNET_ErrorType
253get_type(const char *log) 255get_type (const char *log)
254{ 256{
255 if (NULL == log) 257 if (NULL == log)
256 return GNUNET_ERROR_TYPE_UNSPECIFIED; 258 return GNUNET_ERROR_TYPE_UNSPECIFIED;
257 if (0 == strcasecmp(log, _("DEBUG"))) 259 if (0 == strcasecmp (log, _ ("DEBUG")))
258 return GNUNET_ERROR_TYPE_DEBUG; 260 return GNUNET_ERROR_TYPE_DEBUG;
259 if (0 == strcasecmp(log, _("INFO"))) 261 if (0 == strcasecmp (log, _ ("INFO")))
260 return GNUNET_ERROR_TYPE_INFO; 262 return GNUNET_ERROR_TYPE_INFO;
261 if (0 == strcasecmp(log, _("MESSAGE"))) 263 if (0 == strcasecmp (log, _ ("MESSAGE")))
262 return GNUNET_ERROR_TYPE_MESSAGE; 264 return GNUNET_ERROR_TYPE_MESSAGE;
263 if (0 == strcasecmp(log, _("WARNING"))) 265 if (0 == strcasecmp (log, _ ("WARNING")))
264 return GNUNET_ERROR_TYPE_WARNING; 266 return GNUNET_ERROR_TYPE_WARNING;
265 if (0 == strcasecmp(log, _("ERROR"))) 267 if (0 == strcasecmp (log, _ ("ERROR")))
266 return GNUNET_ERROR_TYPE_ERROR; 268 return GNUNET_ERROR_TYPE_ERROR;
267 if (0 == strcasecmp(log, _("NONE"))) 269 if (0 == strcasecmp (log, _ ("NONE")))
268 return GNUNET_ERROR_TYPE_NONE; 270 return GNUNET_ERROR_TYPE_NONE;
269 return GNUNET_ERROR_TYPE_INVALID; 271 return GNUNET_ERROR_TYPE_INVALID;
270} 272}
@@ -274,32 +276,32 @@ get_type(const char *log)
274 * Abort the process, generate a core dump if possible. 276 * Abort the process, generate a core dump if possible.
275 */ 277 */
276void 278void
277GNUNET_abort_() 279GNUNET_abort_ ()
278{ 280{
279 abort(); 281 abort ();
280} 282}
281 283
282 284
283#if !defined(GNUNET_CULL_LOGGING) 285#if ! defined(GNUNET_CULL_LOGGING)
284/** 286/**
285 * Utility function - reallocates logdefs array to be twice as large. 287 * Utility function - reallocates logdefs array to be twice as large.
286 */ 288 */
287static void 289static void
288resize_logdefs() 290resize_logdefs ()
289{ 291{
290 logdefs_size = (logdefs_size + 1) * 2; 292 logdefs_size = (logdefs_size + 1) * 2;
291 logdefs = GNUNET_realloc(logdefs, logdefs_size * sizeof(struct LogDef)); 293 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof(struct LogDef));
292} 294}
293 295
294 296
295#if !TALER_WALLET_ONLY 297#if ! TALER_WALLET_ONLY
296/** 298/**
297 * Rotate logs, deleting the oldest log. 299 * Rotate logs, deleting the oldest log.
298 * 300 *
299 * @param new_name new name to add to the rotation 301 * @param new_name new name to add to the rotation
300 */ 302 */
301static void 303static void
302log_rotate(const char *new_name) 304log_rotate (const char *new_name)
303{ 305{
304 static char *rotation[ROTATION_KEEP]; 306 static char *rotation[ROTATION_KEEP];
305 static unsigned int rotation_off; 307 static unsigned int rotation_off;
@@ -309,13 +311,13 @@ log_rotate(const char *new_name)
309 return; /* not a real log file name */ 311 return; /* not a real log file name */
310 discard = rotation[rotation_off % ROTATION_KEEP]; 312 discard = rotation[rotation_off % ROTATION_KEEP];
311 if (NULL != discard) 313 if (NULL != discard)
312 { 314 {
313 /* Note: can't log errors during logging (recursion!), so this 315 /* Note: can't log errors during logging (recursion!), so this
314 operation MUST silently fail... */ 316 operation MUST silently fail... */
315 (void)unlink(discard); 317 (void) unlink (discard);
316 GNUNET_free(discard); 318 GNUNET_free (discard);
317 } 319 }
318 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup(new_name); 320 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name);
319 rotation_off++; 321 rotation_off++;
320} 322}
321 323
@@ -327,7 +329,7 @@ log_rotate(const char *new_name)
327 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 329 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
328 */ 330 */
329static int 331static int
330setup_log_file(const struct tm *tm) 332setup_log_file (const struct tm *tm)
331{ 333{
332 static char last_fn[PATH_MAX + 1]; 334 static char last_fn[PATH_MAX + 1];
333 char fn[PATH_MAX + 1]; 335 char fn[PATH_MAX + 1];
@@ -338,64 +340,64 @@ setup_log_file(const struct tm *tm)
338 340
339 if (NULL == log_file_name) 341 if (NULL == log_file_name)
340 return GNUNET_SYSERR; 342 return GNUNET_SYSERR;
341 if (0 == strftime(fn, sizeof(fn), log_file_name, tm)) 343 if (0 == strftime (fn, sizeof(fn), log_file_name, tm))
342 return GNUNET_SYSERR; 344 return GNUNET_SYSERR;
343 leftsquare = strrchr(fn, '['); 345 leftsquare = strrchr (fn, '[');
344 if ((NULL != leftsquare) && (']' == leftsquare[1])) 346 if ((NULL != leftsquare) && (']' == leftsquare[1]))
345 { 347 {
346 char *logfile_copy = GNUNET_strdup(fn); 348 char *logfile_copy = GNUNET_strdup (fn);
347 349
348 logfile_copy[leftsquare - fn] = '\0'; 350 logfile_copy[leftsquare - fn] = '\0';
349 logfile_copy[leftsquare - fn + 1] = '\0'; 351 logfile_copy[leftsquare - fn + 1] = '\0';
350 snprintf(fn, 352 snprintf (fn,
351 PATH_MAX, 353 PATH_MAX,
352 "%s%d%s", 354 "%s%d%s",
353 logfile_copy, 355 logfile_copy,
354 getpid(), 356 getpid (),
355 &logfile_copy[leftsquare - fn + 2]); 357 &logfile_copy[leftsquare - fn + 2]);
356 GNUNET_free(logfile_copy); 358 GNUNET_free (logfile_copy);
357 } 359 }
358 if (0 == strcmp(fn, last_fn)) 360 if (0 == strcmp (fn, last_fn))
359 return GNUNET_OK; /* no change */ 361 return GNUNET_OK; /* no change */
360 log_rotate(last_fn); 362 log_rotate (last_fn);
361 strcpy(last_fn, fn); 363 strcpy (last_fn, fn);
362 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file(fn)) 364 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (fn))
363 { 365 {
364 fprintf(stderr, 366 fprintf (stderr,
365 "Failed to create directory for `%s': %s\n", 367 "Failed to create directory for `%s': %s\n",
366 fn, 368 fn,
367 strerror(errno)); 369 strerror (errno));
368 return GNUNET_SYSERR; 370 return GNUNET_SYSERR;
369 } 371 }
370 altlog_fd = open(fn, 372 altlog_fd = open (fn,
371 O_APPEND | O_WRONLY | O_CREAT, 373 O_APPEND | O_WRONLY | O_CREAT,
372 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 374 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
373 375
374 if (-1 != altlog_fd) 376 if (-1 != altlog_fd)
377 {
378 if (NULL != GNUNET_stderr)
379 fclose (GNUNET_stderr);
380 dup_return = dup2 (altlog_fd, 2);
381 (void) close (altlog_fd);
382 if (-1 != dup_return)
375 { 383 {
376 if (NULL != GNUNET_stderr) 384 altlog = fdopen (2, "ab");
377 fclose(GNUNET_stderr); 385 if (NULL == altlog)
378 dup_return = dup2(altlog_fd, 2); 386 {
379 (void)close(altlog_fd); 387 (void) close (2);
380 if (-1 != dup_return) 388 altlog_fd = -1;
381 { 389 }
382 altlog = fdopen(2, "ab");
383 if (NULL == altlog)
384 {
385 (void)close(2);
386 altlog_fd = -1;
387 }
388 }
389 else
390 {
391 altlog_fd = -1;
392 }
393 } 390 }
394 if (-1 == altlog_fd) 391 else
395 { 392 {
396 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", fn); 393 altlog_fd = -1;
397 return GNUNET_SYSERR;
398 } 394 }
395 }
396 if (-1 == altlog_fd)
397 {
398 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
399 return GNUNET_SYSERR;
400 }
399 GNUNET_stderr = altlog; 401 GNUNET_stderr = altlog;
400 return GNUNET_OK; 402 return GNUNET_OK;
401} 403}
@@ -415,44 +417,44 @@ setup_log_file(const struct tm *tm)
415 * @return 0 on success, regex-specific error otherwise 417 * @return 0 on success, regex-specific error otherwise
416 */ 418 */
417static int 419static int
418add_definition(const char *component, 420add_definition (const char *component,
419 const char *file, 421 const char *file,
420 const char *function, 422 const char *function,
421 int from_line, 423 int from_line,
422 int to_line, 424 int to_line,
423 int level, 425 int level,
424 int force) 426 int force)
425{ 427{
426 struct LogDef n; 428 struct LogDef n;
427 int r; 429 int r;
428 430
429 if (logdefs_size == logdefs_len) 431 if (logdefs_size == logdefs_len)
430 resize_logdefs(); 432 resize_logdefs ();
431 memset(&n, 0, sizeof(n)); 433 memset (&n, 0, sizeof(n));
432 if (0 == strlen(component)) 434 if (0 == strlen (component))
433 component = (char *)".*"; 435 component = (char *) ".*";
434 r = regcomp(&n.component_regex, (const char *)component, REG_NOSUB); 436 r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
435 if (0 != r) 437 if (0 != r)
436 { 438 {
437 return r; 439 return r;
438 } 440 }
439 if (0 == strlen(file)) 441 if (0 == strlen (file))
440 file = (char *)".*"; 442 file = (char *) ".*";
441 r = regcomp(&n.file_regex, (const char *)file, REG_NOSUB); 443 r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
442 if (0 != r) 444 if (0 != r)
443 { 445 {
444 regfree(&n.component_regex); 446 regfree (&n.component_regex);
445 return r; 447 return r;
446 } 448 }
447 if ((NULL == function) || (0 == strlen(function))) 449 if ((NULL == function) || (0 == strlen (function)))
448 function = (char *)".*"; 450 function = (char *) ".*";
449 r = regcomp(&n.function_regex, (const char *)function, REG_NOSUB); 451 r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
450 if (0 != r) 452 if (0 != r)
451 { 453 {
452 regfree(&n.component_regex); 454 regfree (&n.component_regex);
453 regfree(&n.file_regex); 455 regfree (&n.file_regex);
454 return r; 456 return r;
455 } 457 }
456 n.from_line = from_line; 458 n.from_line = from_line;
457 n.to_line = to_line; 459 n.to_line = to_line;
458 n.level = level; 460 n.level = level;
@@ -476,11 +478,11 @@ add_definition(const char *component,
476 * @return 0 to disallow the call, 1 to allow it 478 * @return 0 to disallow the call, 1 to allow it
477 */ 479 */
478int 480int
479GNUNET_get_log_call_status(int caller_level, 481GNUNET_get_log_call_status (int caller_level,
480 const char *comp, 482 const char *comp,
481 const char *file, 483 const char *file,
482 const char *function, 484 const char *function,
483 int line) 485 int line)
484{ 486{
485 struct LogDef *ld; 487 struct LogDef *ld;
486 int i; 488 int i;
@@ -499,18 +501,18 @@ GNUNET_get_log_call_status(int caller_level,
499 /* Only look for forced definitions? */ 501 /* Only look for forced definitions? */
500 force_only = min_level >= 0; 502 force_only = min_level >= 0;
501 for (i = 0; i < logdefs_len; i++) 503 for (i = 0; i < logdefs_len; i++)
504 {
505 ld = &logdefs[i];
506 if (((! force_only) || ld->force) &&
507 ((line >= ld->from_line) &&(line <= ld->to_line) ) &&
508 (0 == regexec (&ld->component_regex, comp, 0, NULL, 0)) &&
509 (0 == regexec (&ld->file_regex, file, 0, NULL, 0)) &&
510 (0 == regexec (&ld->function_regex, function, 0, NULL, 0)))
502 { 511 {
503 ld = &logdefs[i]; 512 /* We're finished */
504 if (((!force_only) || ld->force) && 513 return caller_level <= ld->level;
505 (line >= ld->from_line && line <= ld->to_line) &&
506 (0 == regexec(&ld->component_regex, comp, 0, NULL, 0)) &&
507 (0 == regexec(&ld->file_regex, file, 0, NULL, 0)) &&
508 (0 == regexec(&ld->function_regex, function, 0, NULL, 0)))
509 {
510 /* We're finished */
511 return caller_level <= ld->level;
512 }
513 } 514 }
515 }
514 /* No matches - use global level, if defined */ 516 /* No matches - use global level, if defined */
515 if (min_level >= 0) 517 if (min_level >= 0)
516 return caller_level <= min_level; 518 return caller_level <= min_level;
@@ -546,7 +548,7 @@ GNUNET_get_log_call_status(int caller_level,
546 * @return number of added definitions 548 * @return number of added definitions
547 */ 549 */
548static int 550static int
549parse_definitions(const char *constname, int force) 551parse_definitions (const char *constname, int force)
550{ 552{
551 char *def; 553 char *def;
552 const char *tmp; 554 const char *tmp;
@@ -562,116 +564,116 @@ parse_definitions(const char *constname, int force)
562 int counter = 0; 564 int counter = 0;
563 int keep_looking = 1; 565 int keep_looking = 1;
564 566
565 tmp = getenv(constname); 567 tmp = getenv (constname);
566 if (NULL == tmp) 568 if (NULL == tmp)
567 return 0; 569 return 0;
568 def = GNUNET_strdup(tmp); 570 def = GNUNET_strdup (tmp);
569 from_line = 0; 571 from_line = 0;
570 to_line = INT_MAX; 572 to_line = INT_MAX;
571 for (p = def, state = 0, start = def; keep_looking; p++) 573 for (p = def, state = 0, start = def; keep_looking; p++)
574 {
575 switch (p[0])
572 { 576 {
573 switch (p[0]) 577 case ';': /* found a field separator */
574 { 578 p[0] = '\0';
575 case ';': /* found a field separator */ 579 switch (state)
576 p[0] = '\0'; 580 {
577 switch (state) 581 case 0: /* within a component name */
578 { 582 comp = start;
579 case 0: /* within a component name */ 583 break;
580 comp = start; 584
581 break; 585 case 1: /* within a file name */
582 586 file = start;
583 case 1: /* within a file name */ 587 break;
584 file = start;
585 break;
586
587 case 2: /* within a function name */
588 /* after a file name there must be a function name */
589 function = start;
590 break;
591
592 case 3: /* within a from-to line range */
593 if (strlen(start) > 0)
594 {
595 errno = 0;
596 from_line = strtol(start, &t, 10);
597 if ((0 != errno) || (from_line < 0))
598 {
599 GNUNET_free(def);
600 return counter;
601 }
602 if ((t < p) && ('-' == t[0]))
603 {
604 errno = 0;
605 start = t + 1;
606 to_line = strtol(start, &t, 10);
607 if ((0 != errno) || (to_line < 0) || (t != p))
608 {
609 GNUNET_free(def);
610 return counter;
611 }
612 }
613 else /* one number means "match this line only" */
614 to_line = from_line;
615 }
616 else /* default to 0-max */
617 {
618 from_line = 0;
619 to_line = INT_MAX;
620 }
621 break;
622
623 default:
624 fprintf(
625 stderr,
626 _("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
627 p);
628 break;
629 }
630 start = p + 1;
631 state++;
632 break;
633 588
634 case '\0': /* found EOL */ 589 case 2: /* within a function name */
635 keep_looking = 0; 590 /* after a file name there must be a function name */
591 function = start;
592 break;
636 593
637 /* fall through to '/' */ 594 case 3: /* within a from-to line range */
638 case '/': /* found a definition separator */ 595 if (strlen (start) > 0)
639 switch (state) 596 {
597 errno = 0;
598 from_line = strtol (start, &t, 10);
599 if ((0 != errno) || (from_line < 0))
600 {
601 GNUNET_free (def);
602 return counter;
603 }
604 if ((t < p) && ('-' == t[0]))
605 {
606 errno = 0;
607 start = t + 1;
608 to_line = strtol (start, &t, 10);
609 if ((0 != errno) || (to_line < 0) || (t != p))
640 { 610 {
641 case 4: /* within a log level */ 611 GNUNET_free (def);
642 p[0] = '\0'; 612 return counter;
643 state = 0;
644 level = get_type((const char *)start);
645 if ((GNUNET_ERROR_TYPE_INVALID == level) ||
646 (GNUNET_ERROR_TYPE_UNSPECIFIED == level) ||
647 (0 != add_definition(comp,
648 file,
649 function,
650 from_line,
651 to_line,
652 level,
653 force)))
654 {
655 GNUNET_free(def);
656 return counter;
657 }
658 counter++;
659 start = p + 1;
660 break;
661
662 default:
663 fprintf(
664 stderr,
665 _("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
666 p);
667 break;
668 } 613 }
614 }
615 else /* one number means "match this line only" */
616 to_line = from_line;
617 }
618 else /* default to 0-max */
619 {
620 from_line = 0;
621 to_line = INT_MAX;
622 }
623 break;
624
625 default:
626 fprintf (
627 stderr,
628 _ ("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
629 p);
630 break;
631 }
632 start = p + 1;
633 state++;
634 break;
669 635
670 default: 636 case '\0': /* found EOL */
671 break; 637 keep_looking = 0;
638
639 /* fall through to '/' */
640 case '/': /* found a definition separator */
641 switch (state)
642 {
643 case 4: /* within a log level */
644 p[0] = '\0';
645 state = 0;
646 level = get_type ((const char *) start);
647 if ((GNUNET_ERROR_TYPE_INVALID == level) ||
648 (GNUNET_ERROR_TYPE_UNSPECIFIED == level) ||
649 (0 != add_definition (comp,
650 file,
651 function,
652 from_line,
653 to_line,
654 level,
655 force)))
656 {
657 GNUNET_free (def);
658 return counter;
672 } 659 }
660 counter++;
661 start = p + 1;
662 break;
663
664 default:
665 fprintf (
666 stderr,
667 _ ("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
668 p);
669 break;
670 }
671
672 default:
673 break;
673 } 674 }
674 GNUNET_free(def); 675 }
676 GNUNET_free (def);
675 return counter; 677 return counter;
676} 678}
677 679
@@ -680,15 +682,15 @@ parse_definitions(const char *constname, int force)
680 * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG. 682 * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG.
681 */ 683 */
682static void 684static void
683parse_all_definitions() 685parse_all_definitions ()
684{ 686{
685 if (GNUNET_NO == gnunet_force_log_parsed) 687 if (GNUNET_NO == gnunet_force_log_parsed)
686 gnunet_force_log_present = 688 gnunet_force_log_present =
687 parse_definitions("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; 689 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
688 gnunet_force_log_parsed = GNUNET_YES; 690 gnunet_force_log_parsed = GNUNET_YES;
689 691
690 if (GNUNET_NO == gnunet_log_parsed) 692 if (GNUNET_NO == gnunet_log_parsed)
691 parse_definitions("GNUNET_LOG", 0); 693 parse_definitions ("GNUNET_LOG", 0);
692 gnunet_log_parsed = GNUNET_YES; 694 gnunet_log_parsed = GNUNET_YES;
693} 695}
694#endif 696#endif
@@ -703,40 +705,40 @@ parse_all_definitions()
703 * @return #GNUNET_OK on success 705 * @return #GNUNET_OK on success
704 */ 706 */
705int 707int
706GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile) 708GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
707{ 709{
708 const char *env_logfile; 710 const char *env_logfile;
709 711
710 min_level = get_type(loglevel); 712 min_level = get_type (loglevel);
711#if !defined(GNUNET_CULL_LOGGING) 713#if ! defined(GNUNET_CULL_LOGGING)
712 parse_all_definitions(); 714 parse_all_definitions ();
713#endif 715#endif
714 GNUNET_free_non_null(component); 716 GNUNET_free_non_null (component);
715 GNUNET_asprintf(&component, "%s-%d", comp, getpid()); 717 GNUNET_asprintf (&component, "%s-%d", comp, getpid ());
716 GNUNET_free_non_null(component_nopid); 718 GNUNET_free_non_null (component_nopid);
717 component_nopid = GNUNET_strdup(comp); 719 component_nopid = GNUNET_strdup (comp);
718 720
719 env_logfile = getenv("GNUNET_FORCE_LOGFILE"); 721 env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
720 if ((NULL != env_logfile) && (strlen(env_logfile) > 0)) 722 if ((NULL != env_logfile) && (strlen (env_logfile) > 0))
721 logfile = env_logfile; 723 logfile = env_logfile;
722 if (NULL == logfile) 724 if (NULL == logfile)
723 return GNUNET_OK; 725 return GNUNET_OK;
724 GNUNET_free_non_null(log_file_name); 726 GNUNET_free_non_null (log_file_name);
725 log_file_name = GNUNET_STRINGS_filename_expand(logfile); 727 log_file_name = GNUNET_STRINGS_filename_expand (logfile);
726 if (NULL == log_file_name) 728 if (NULL == log_file_name)
727 return GNUNET_SYSERR; 729 return GNUNET_SYSERR;
728#if TALER_WALLET_ONLY || defined(GNUNET_CULL_LOGGING) 730#if TALER_WALLET_ONLY || defined(GNUNET_CULL_LOGGING)
729 /* log file option not allowed for wallet logic */ 731 /* log file option not allowed for wallet logic */
730 GNUNET_assert(NULL == logfile); 732 GNUNET_assert (NULL == logfile);
731 return GNUNET_OK; 733 return GNUNET_OK;
732#else 734#else
733 { 735 {
734 time_t t; 736 time_t t;
735 const struct tm *tm; 737 const struct tm *tm;
736 738
737 t = time(NULL); 739 t = time (NULL);
738 tm = gmtime(&t); 740 tm = gmtime (&t);
739 return setup_log_file(tm); 741 return setup_log_file (tm);
740 } 742 }
741#endif 743#endif
742} 744}
@@ -752,11 +754,11 @@ GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
752 * @param logger_cls closure for @a logger 754 * @param logger_cls closure for @a logger
753 */ 755 */
754void 756void
755GNUNET_logger_add(GNUNET_Logger logger, void *logger_cls) 757GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls)
756{ 758{
757 struct CustomLogger *entry; 759 struct CustomLogger *entry;
758 760
759 entry = GNUNET_new(struct CustomLogger); 761 entry = GNUNET_new (struct CustomLogger);
760 entry->logger = logger; 762 entry->logger = logger;
761 entry->logger_cls = logger_cls; 763 entry->logger_cls = logger_cls;
762 entry->next = loggers; 764 entry->next = loggers;
@@ -771,7 +773,7 @@ GNUNET_logger_add(GNUNET_Logger logger, void *logger_cls)
771 * @param logger_cls closure for @a logger 773 * @param logger_cls closure for @a logger
772 */ 774 */
773void 775void
774GNUNET_logger_remove(GNUNET_Logger logger, void *logger_cls) 776GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
775{ 777{
776 struct CustomLogger *pos; 778 struct CustomLogger *pos;
777 struct CustomLogger *prev; 779 struct CustomLogger *prev;
@@ -780,16 +782,16 @@ GNUNET_logger_remove(GNUNET_Logger logger, void *logger_cls)
780 pos = loggers; 782 pos = loggers;
781 while ((NULL != pos) && 783 while ((NULL != pos) &&
782 ((pos->logger != logger) || (pos->logger_cls != logger_cls))) 784 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
783 { 785 {
784 prev = pos; 786 prev = pos;
785 pos = pos->next; 787 pos = pos->next;
786 } 788 }
787 GNUNET_assert(NULL != pos); 789 GNUNET_assert (NULL != pos);
788 if (NULL == prev) 790 if (NULL == prev)
789 loggers = pos->next; 791 loggers = pos->next;
790 else 792 else
791 prev->next = pos->next; 793 prev->next = pos->next;
792 GNUNET_free(pos); 794 GNUNET_free (pos);
793} 795}
794 796
795 797
@@ -802,67 +804,67 @@ GNUNET_logger_remove(GNUNET_Logger logger, void *logger_cls)
802 * @param msg the actual message 804 * @param msg the actual message
803 */ 805 */
804static void 806static void
805output_message(enum GNUNET_ErrorType kind, 807output_message (enum GNUNET_ErrorType kind,
806 const char *comp, 808 const char *comp,
807 const char *datestr, 809 const char *datestr,
808 const char *msg) 810 const char *msg)
809{ 811{
810 struct CustomLogger *pos; 812 struct CustomLogger *pos;
811 813
812 /* only use the standard logger if no custom loggers are present */ 814 /* only use the standard logger if no custom loggers are present */
813 if ((NULL != GNUNET_stderr) && (NULL == loggers)) 815 if ((NULL != GNUNET_stderr) && (NULL == loggers))
816 {
817 if (kind == GNUNET_ERROR_TYPE_MESSAGE)
814 { 818 {
815 if (kind == GNUNET_ERROR_TYPE_MESSAGE) 819 /* The idea here is to produce "normal" output messages
816 { 820 * for end users while still having the power of the
817 /* The idea here is to produce "normal" output messages 821 * logging engine for developer needs. So ideally this
818 * for end users while still having the power of the 822 * is what it should look like when CLI tools are used
819 * logging engine for developer needs. So ideally this 823 * interactively, yet the same message shouldn't look
820 * is what it should look like when CLI tools are used 824 * this way if the output is going to logfiles or robots
821 * interactively, yet the same message shouldn't look 825 * instead.
822 * this way if the output is going to logfiles or robots 826 */
823 * instead. 827 fprintf (GNUNET_stderr, "* %s", msg);
824 */
825 fprintf(GNUNET_stderr, "* %s", msg);
826 }
827 else if (GNUNET_YES == current_async_scope.have_scope)
828 {
829 static GNUNET_THREAD_LOCAL char id_buf[27];
830 char *end;
831
832 /* We're logging, so skip_log must be currently 0. */
833 skip_log = 100;
834 end = GNUNET_STRINGS_data_to_string(&current_async_scope.scope_id,
835 sizeof(struct GNUNET_AsyncScopeId),
836 id_buf,
837 sizeof(id_buf) - 1);
838 GNUNET_assert(NULL != end);
839 *end = '\0';
840 skip_log = 0;
841 fprintf(GNUNET_stderr,
842 "%s %s(%s) %s %s",
843 datestr,
844 comp,
845 id_buf,
846 GNUNET_error_type_to_string(kind),
847 msg);
848 }
849 else
850 {
851 fprintf(GNUNET_stderr,
852 "%s %s %s %s",
853 datestr,
854 comp,
855 GNUNET_error_type_to_string(kind),
856 msg);
857 }
858 fflush(GNUNET_stderr);
859 } 828 }
860 pos = loggers; 829 else if (GNUNET_YES == current_async_scope.have_scope)
861 while (NULL != pos)
862 { 830 {
863 pos->logger(pos->logger_cls, kind, comp, datestr, msg); 831 static GNUNET_THREAD_LOCAL char id_buf[27];
864 pos = pos->next; 832 char *end;
833
834 /* We're logging, so skip_log must be currently 0. */
835 skip_log = 100;
836 end = GNUNET_STRINGS_data_to_string (&current_async_scope.scope_id,
837 sizeof(struct GNUNET_AsyncScopeId),
838 id_buf,
839 sizeof(id_buf) - 1);
840 GNUNET_assert (NULL != end);
841 *end = '\0';
842 skip_log = 0;
843 fprintf (GNUNET_stderr,
844 "%s %s(%s) %s %s",
845 datestr,
846 comp,
847 id_buf,
848 GNUNET_error_type_to_string (kind),
849 msg);
865 } 850 }
851 else
852 {
853 fprintf (GNUNET_stderr,
854 "%s %s %s %s",
855 datestr,
856 comp,
857 GNUNET_error_type_to_string (kind),
858 msg);
859 }
860 fflush (GNUNET_stderr);
861 }
862 pos = loggers;
863 while (NULL != pos)
864 {
865 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
866 pos = pos->next;
867 }
866} 868}
867 869
868 870
@@ -872,7 +874,7 @@ output_message(enum GNUNET_ErrorType kind,
872 * @param datestr our current timestamp 874 * @param datestr our current timestamp
873 */ 875 */
874static void 876static void
875flush_bulk(const char *datestr) 877flush_bulk (const char *datestr)
876{ 878{
877 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256]; 879 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
878 int rev; 880 int rev;
@@ -882,31 +884,31 @@ flush_bulk(const char *datestr)
882 if ((0 == last_bulk_time.abs_value_us) || (0 == last_bulk_repeat)) 884 if ((0 == last_bulk_time.abs_value_us) || (0 == last_bulk_repeat))
883 return; 885 return;
884 rev = 0; 886 rev = 0;
885 last = memchr(last_bulk, '\0', BULK_TRACK_SIZE); 887 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
886 if (last == NULL) 888 if (last == NULL)
887 last = &last_bulk[BULK_TRACK_SIZE - 1]; 889 last = &last_bulk[BULK_TRACK_SIZE - 1];
888 else if (last != last_bulk) 890 else if (last != last_bulk)
889 last--; 891 last--;
890 if (last[0] == '\n') 892 if (last[0] == '\n')
891 { 893 {
892 rev = 1; 894 rev = 1;
893 last[0] = '\0'; 895 last[0] = '\0';
894 } 896 }
895 ft = 897 ft =
896 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration( 898 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (
897 last_bulk_time), 899 last_bulk_time),
898 GNUNET_YES); 900 GNUNET_YES);
899 snprintf(msg, 901 snprintf (msg,
900 sizeof(msg), 902 sizeof(msg),
901 _("Message `%.*s' repeated %u times in the last %s\n"), 903 _ ("Message `%.*s' repeated %u times in the last %s\n"),
902 BULK_TRACK_SIZE, 904 BULK_TRACK_SIZE,
903 last_bulk, 905 last_bulk,
904 last_bulk_repeat, 906 last_bulk_repeat,
905 ft); 907 ft);
906 if (rev == 1) 908 if (rev == 1)
907 last[0] = '\n'; 909 last[0] = '\n';
908 output_message(last_bulk_kind, last_bulk_comp, datestr, msg); 910 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
909 last_bulk_time = GNUNET_TIME_absolute_get(); 911 last_bulk_time = GNUNET_TIME_absolute_get ();
910 last_bulk_repeat = 0; 912 last_bulk_repeat = 0;
911} 913}
912 914
@@ -918,21 +920,21 @@ flush_bulk(const char *datestr)
918 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero 920 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
919 */ 921 */
920void 922void
921GNUNET_log_skip(int n, int check_reset) 923GNUNET_log_skip (int n, int check_reset)
922{ 924{
923 int ok; 925 int ok;
924 926
925 if (0 == n) 927 if (0 == n)
926 { 928 {
927 ok = (0 == skip_log); 929 ok = (0 == skip_log);
928 skip_log = 0; 930 skip_log = 0;
929 if (check_reset) 931 if (check_reset)
930 GNUNET_break(ok); 932 GNUNET_break (ok);
931 } 933 }
932 else 934 else
933 { 935 {
934 skip_log += n; 936 skip_log += n;
935 } 937 }
936} 938}
937 939
938 940
@@ -942,7 +944,7 @@ GNUNET_log_skip(int n, int check_reset)
942 * @return number of log calls to be ignored 944 * @return number of log calls to be ignored
943 */ 945 */
944int 946int
945GNUNET_get_log_skip() 947GNUNET_get_log_skip ()
946{ 948{
947 return skip_log; 949 return skip_log;
948} 950}
@@ -957,10 +959,10 @@ GNUNET_get_log_skip()
957 * @param va arguments to the format string "message" 959 * @param va arguments to the format string "message"
958 */ 960 */
959static void 961static void
960mylog(enum GNUNET_ErrorType kind, 962mylog (enum GNUNET_ErrorType kind,
961 const char *comp, 963 const char *comp,
962 const char *message, 964 const char *message,
963 va_list va) 965 va_list va)
964{ 966{
965 char date[DATE_STR_SIZE]; 967 char date[DATE_STR_SIZE];
966 char date2[DATE_STR_SIZE]; 968 char date2[DATE_STR_SIZE];
@@ -968,78 +970,78 @@ mylog(enum GNUNET_ErrorType kind,
968 size_t size; 970 size_t size;
969 va_list vacp; 971 va_list vacp;
970 972
971 va_copy(vacp, va); 973 va_copy (vacp, va);
972 size = vsnprintf(NULL, 0, message, vacp) + 1; 974 size = vsnprintf (NULL, 0, message, vacp) + 1;
973 GNUNET_assert(0 != size); 975 GNUNET_assert (0 != size);
974 va_end(vacp); 976 va_end (vacp);
975 memset(date, 0, DATE_STR_SIZE); 977 memset (date, 0, DATE_STR_SIZE);
976 { 978 {
977 char buf[size]; 979 char buf[size];
978 long long offset; 980 long long offset;
979 981
980 struct timeval timeofday; 982 struct timeval timeofday;
981 983
982 gettimeofday(&timeofday, NULL); 984 gettimeofday (&timeofday, NULL);
983 offset = GNUNET_TIME_get_offset(); 985 offset = GNUNET_TIME_get_offset ();
984 if (offset > 0) 986 if (offset > 0)
987 {
988 timeofday.tv_sec += offset / 1000LL;
989 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
990 if (timeofday.tv_usec > 1000000LL)
985 { 991 {
986 timeofday.tv_sec += offset / 1000LL; 992 timeofday.tv_usec -= 1000000LL;
987 timeofday.tv_usec += (offset % 1000LL) * 1000LL; 993 timeofday.tv_sec++;
988 if (timeofday.tv_usec > 1000000LL)
989 {
990 timeofday.tv_usec -= 1000000LL;
991 timeofday.tv_sec++;
992 }
993 } 994 }
995 }
994 else 996 else
997 {
998 timeofday.tv_sec += offset / 1000LL;
999 if (timeofday.tv_usec > -(offset % 1000LL) * 1000LL)
995 { 1000 {
996 timeofday.tv_sec += offset / 1000LL; 1001 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
997 if (timeofday.tv_usec > -(offset % 1000LL) * 1000LL)
998 {
999 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
1000 }
1001 else
1002 {
1003 timeofday.tv_usec += 1000000LL + (offset % 1000LL) * 1000LL;
1004 timeofday.tv_sec--;
1005 }
1006 } 1002 }
1007 tmptr = localtime(&timeofday.tv_sec); 1003 else
1008 if (NULL == tmptr)
1009 { 1004 {
1010 strcpy(date, "localtime error"); 1005 timeofday.tv_usec += 1000000LL + (offset % 1000LL) * 1000LL;
1006 timeofday.tv_sec--;
1011 } 1007 }
1008 }
1009 tmptr = localtime (&timeofday.tv_sec);
1010 if (NULL == tmptr)
1011 {
1012 strcpy (date, "localtime error");
1013 }
1012 else 1014 else
1013 { 1015 {
1014 if (0 == strftime(date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr)) 1016 if (0 == strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr))
1015 abort(); 1017 abort ();
1016 if (0 > snprintf(date, sizeof(date), date2, timeofday.tv_usec)) 1018 if (0 > snprintf (date, sizeof(date), date2, timeofday.tv_usec))
1017 abort(); 1019 abort ();
1018 } 1020 }
1019 1021
1020 vsnprintf(buf, size, message, va); 1022 vsnprintf (buf, size, message, va);
1021#if !(defined(GNUNET_CULL_LOGGING) || TALER_WALLET_ONLY) 1023#if ! (defined(GNUNET_CULL_LOGGING) || TALER_WALLET_ONLY)
1022 if (NULL != tmptr) 1024 if (NULL != tmptr)
1023 (void)setup_log_file(tmptr); 1025 (void) setup_log_file (tmptr);
1024#endif 1026#endif
1025 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && 1027 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
1026 (0 != last_bulk_time.abs_value_us) && 1028 (0 != last_bulk_time.abs_value_us) &&
1027 (0 == strncmp(buf, last_bulk, sizeof(last_bulk)))) 1029 (0 == strncmp (buf, last_bulk, sizeof(last_bulk))))
1028 { 1030 {
1029 last_bulk_repeat++; 1031 last_bulk_repeat++;
1030 if ((GNUNET_TIME_absolute_get_duration(last_bulk_time).rel_value_us > 1032 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value_us >
1031 BULK_DELAY_THRESHOLD) || 1033 BULK_DELAY_THRESHOLD) ||
1032 (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) 1034 (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
1033 flush_bulk(date); 1035 flush_bulk (date);
1034 return; 1036 return;
1035 } 1037 }
1036 flush_bulk(date); 1038 flush_bulk (date);
1037 GNUNET_strlcpy(last_bulk, buf, sizeof(last_bulk)); 1039 GNUNET_strlcpy (last_bulk, buf, sizeof(last_bulk));
1038 last_bulk_repeat = 0; 1040 last_bulk_repeat = 0;
1039 last_bulk_kind = kind; 1041 last_bulk_kind = kind;
1040 last_bulk_time = GNUNET_TIME_absolute_get(); 1042 last_bulk_time = GNUNET_TIME_absolute_get ();
1041 GNUNET_strlcpy(last_bulk_comp, comp, sizeof(last_bulk_comp)); 1043 GNUNET_strlcpy (last_bulk_comp, comp, sizeof(last_bulk_comp));
1042 output_message(kind, comp, date, buf); 1044 output_message (kind, comp, date, buf);
1043 } 1045 }
1044} 1046}
1045 1047
@@ -1052,13 +1054,13 @@ mylog(enum GNUNET_ErrorType kind,
1052 * @param ... arguments for format string 1054 * @param ... arguments for format string
1053 */ 1055 */
1054void 1056void
1055GNUNET_log_nocheck(enum GNUNET_ErrorType kind, const char *message, ...) 1057GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
1056{ 1058{
1057 va_list va; 1059 va_list va;
1058 1060
1059 va_start(va, message); 1061 va_start (va, message);
1060 mylog(kind, component, message, va); 1062 mylog (kind, component, message, va);
1061 va_end(va); 1063 va_end (va);
1062} 1064}
1063 1065
1064 1066
@@ -1072,10 +1074,10 @@ GNUNET_log_nocheck(enum GNUNET_ErrorType kind, const char *message, ...)
1072 * @param ... arguments for format string 1074 * @param ... arguments for format string
1073 */ 1075 */
1074void 1076void
1075GNUNET_log_from_nocheck(enum GNUNET_ErrorType kind, 1077GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind,
1076 const char *comp, 1078 const char *comp,
1077 const char *message, 1079 const char *message,
1078 ...) 1080 ...)
1079{ 1081{
1080 va_list va; 1082 va_list va;
1081 char comp_w_pid[128]; 1083 char comp_w_pid[128];
@@ -1083,10 +1085,10 @@ GNUNET_log_from_nocheck(enum GNUNET_ErrorType kind,
1083 if (comp == NULL) 1085 if (comp == NULL)
1084 comp = component_nopid; 1086 comp = component_nopid;
1085 1087
1086 va_start(va, message); 1088 va_start (va, message);
1087 GNUNET_snprintf(comp_w_pid, sizeof(comp_w_pid), "%s-%d", comp, getpid()); 1089 GNUNET_snprintf (comp_w_pid, sizeof(comp_w_pid), "%s-%d", comp, getpid ());
1088 mylog(kind, comp_w_pid, message, va); 1090 mylog (kind, comp_w_pid, message, va);
1089 va_end(va); 1091 va_end (va);
1090} 1092}
1091 1093
1092 1094
@@ -1097,21 +1099,21 @@ GNUNET_log_from_nocheck(enum GNUNET_ErrorType kind,
1097 * @return string corresponding to the type 1099 * @return string corresponding to the type
1098 */ 1100 */
1099const char * 1101const char *
1100GNUNET_error_type_to_string(enum GNUNET_ErrorType kind) 1102GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
1101{ 1103{
1102 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0) 1104 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
1103 return _("ERROR"); 1105 return _ ("ERROR");
1104 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0) 1106 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
1105 return _("WARNING"); 1107 return _ ("WARNING");
1106 if ((kind & GNUNET_ERROR_TYPE_MESSAGE) > 0) 1108 if ((kind & GNUNET_ERROR_TYPE_MESSAGE) > 0)
1107 return _("MESSAGE"); 1109 return _ ("MESSAGE");
1108 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0) 1110 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
1109 return _("INFO"); 1111 return _ ("INFO");
1110 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0) 1112 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
1111 return _("DEBUG"); 1113 return _ ("DEBUG");
1112 if ((kind & ~GNUNET_ERROR_TYPE_BULK) == 0) 1114 if ((kind & ~GNUNET_ERROR_TYPE_BULK) == 0)
1113 return _("NONE"); 1115 return _ ("NONE");
1114 return _("INVALID"); 1116 return _ ("INVALID");
1115} 1117}
1116 1118
1117 1119
@@ -1122,13 +1124,13 @@ GNUNET_error_type_to_string(enum GNUNET_ErrorType kind)
1122 * @return string form; will be overwritten by next call to GNUNET_h2s. 1124 * @return string form; will be overwritten by next call to GNUNET_h2s.
1123 */ 1125 */
1124const char * 1126const char *
1125GNUNET_h2s(const struct GNUNET_HashCode *hc) 1127GNUNET_h2s (const struct GNUNET_HashCode *hc)
1126{ 1128{
1127 static GNUNET_THREAD_LOCAL struct GNUNET_CRYPTO_HashAsciiEncoded ret; 1129 static GNUNET_THREAD_LOCAL struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1128 1130
1129 GNUNET_CRYPTO_hash_to_enc(hc, &ret); 1131 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1130 ret.encoding[8] = '\0'; 1132 ret.encoding[8] = '\0';
1131 return (const char *)ret.encoding; 1133 return (const char *) ret.encoding;
1132} 1134}
1133 1135
1134 1136
@@ -1143,13 +1145,13 @@ GNUNET_h2s(const struct GNUNET_HashCode *hc)
1143 * @return string form; will be overwritten by next call to GNUNET_h2s. 1145 * @return string form; will be overwritten by next call to GNUNET_h2s.
1144 */ 1146 */
1145const char * 1147const char *
1146GNUNET_h2s2(const struct GNUNET_HashCode *hc) 1148GNUNET_h2s2 (const struct GNUNET_HashCode *hc)
1147{ 1149{
1148 static struct GNUNET_CRYPTO_HashAsciiEncoded ret; 1150 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1149 1151
1150 GNUNET_CRYPTO_hash_to_enc(hc, &ret); 1152 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1151 ret.encoding[8] = '\0'; 1153 ret.encoding[8] = '\0';
1152 return (const char *)ret.encoding; 1154 return (const char *) ret.encoding;
1153} 1155}
1154 1156
1155 1157
@@ -1163,15 +1165,15 @@ GNUNET_h2s2(const struct GNUNET_HashCode *hc)
1163 * @return string 1165 * @return string
1164 */ 1166 */
1165const char * 1167const char *
1166GNUNET_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p) 1168GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1167{ 1169{
1168 static struct GNUNET_CRYPTO_HashAsciiEncoded ret; 1170 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1169 struct GNUNET_HashCode hc; 1171 struct GNUNET_HashCode hc;
1170 1172
1171 GNUNET_CRYPTO_hash(p, sizeof(*p), &hc); 1173 GNUNET_CRYPTO_hash (p, sizeof(*p), &hc);
1172 GNUNET_CRYPTO_hash_to_enc(&hc, &ret); 1174 GNUNET_CRYPTO_hash_to_enc (&hc, &ret);
1173 ret.encoding[6] = '\0'; 1175 ret.encoding[6] = '\0';
1174 return (const char *)ret.encoding; 1176 return (const char *) ret.encoding;
1175} 1177}
1176 1178
1177 1179
@@ -1185,15 +1187,15 @@ GNUNET_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1185 * @return string 1187 * @return string
1186 */ 1188 */
1187const char * 1189const char *
1188GNUNET_p2s2(const struct GNUNET_CRYPTO_EddsaPublicKey *p) 1190GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1189{ 1191{
1190 static struct GNUNET_CRYPTO_HashAsciiEncoded ret; 1192 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1191 struct GNUNET_HashCode hc; 1193 struct GNUNET_HashCode hc;
1192 1194
1193 GNUNET_CRYPTO_hash(p, sizeof(*p), &hc); 1195 GNUNET_CRYPTO_hash (p, sizeof(*p), &hc);
1194 GNUNET_CRYPTO_hash_to_enc(&hc, &ret); 1196 GNUNET_CRYPTO_hash_to_enc (&hc, &ret);
1195 ret.encoding[6] = '\0'; 1197 ret.encoding[6] = '\0';
1196 return (const char *)ret.encoding; 1198 return (const char *) ret.encoding;
1197} 1199}
1198 1200
1199 1201
@@ -1207,15 +1209,15 @@ GNUNET_p2s2(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1207 * @return string 1209 * @return string
1208 */ 1210 */
1209const char * 1211const char *
1210GNUNET_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p) 1212GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1211{ 1213{
1212 static struct GNUNET_CRYPTO_HashAsciiEncoded ret; 1214 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1213 struct GNUNET_HashCode hc; 1215 struct GNUNET_HashCode hc;
1214 1216
1215 GNUNET_CRYPTO_hash(p, sizeof(*p), &hc); 1217 GNUNET_CRYPTO_hash (p, sizeof(*p), &hc);
1216 GNUNET_CRYPTO_hash_to_enc(&hc, &ret); 1218 GNUNET_CRYPTO_hash_to_enc (&hc, &ret);
1217 ret.encoding[6] = '\0'; 1219 ret.encoding[6] = '\0';
1218 return (const char *)ret.encoding; 1220 return (const char *) ret.encoding;
1219} 1221}
1220 1222
1221 1223
@@ -1229,15 +1231,15 @@ GNUNET_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1229 * @return string 1231 * @return string
1230 */ 1232 */
1231const char * 1233const char *
1232GNUNET_e2s2(const struct GNUNET_CRYPTO_EcdhePublicKey *p) 1234GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1233{ 1235{
1234 static struct GNUNET_CRYPTO_HashAsciiEncoded ret; 1236 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1235 struct GNUNET_HashCode hc; 1237 struct GNUNET_HashCode hc;
1236 1238
1237 GNUNET_CRYPTO_hash(p, sizeof(*p), &hc); 1239 GNUNET_CRYPTO_hash (p, sizeof(*p), &hc);
1238 GNUNET_CRYPTO_hash_to_enc(&hc, &ret); 1240 GNUNET_CRYPTO_hash_to_enc (&hc, &ret);
1239 ret.encoding[6] = '\0'; 1241 ret.encoding[6] = '\0';
1240 return (const char *)ret.encoding; 1242 return (const char *) ret.encoding;
1241} 1243}
1242 1244
1243 1245
@@ -1251,13 +1253,13 @@ GNUNET_e2s2(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1251 * @return string 1253 * @return string
1252 */ 1254 */
1253const char * 1255const char *
1254GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc) 1256GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc)
1255{ 1257{
1256 static char buf[64]; 1258 static char buf[64];
1257 1259
1258 GNUNET_STRINGS_data_to_string(shc, sizeof(*shc), buf, sizeof(buf)); 1260 GNUNET_STRINGS_data_to_string (shc, sizeof(*shc), buf, sizeof(buf));
1259 buf[6] = '\0'; 1261 buf[6] = '\0';
1260 return (const char *)buf; 1262 return (const char *) buf;
1261} 1263}
1262 1264
1263 1265
@@ -1271,13 +1273,13 @@ GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
1271 * @return string 1273 * @return string
1272 */ 1274 */
1273const char * 1275const char *
1274GNUNET_uuid2s(const struct GNUNET_Uuid *uuid) 1276GNUNET_uuid2s (const struct GNUNET_Uuid *uuid)
1275{ 1277{
1276 static char buf[32]; 1278 static char buf[32];
1277 1279
1278 GNUNET_STRINGS_data_to_string(uuid, sizeof(*uuid), buf, sizeof(buf)); 1280 GNUNET_STRINGS_data_to_string (uuid, sizeof(*uuid), buf, sizeof(buf));
1279 buf[6] = '\0'; 1281 buf[6] = '\0';
1280 return (const char *)buf; 1282 return (const char *) buf;
1281} 1283}
1282 1284
1283 1285
@@ -1290,13 +1292,13 @@ GNUNET_uuid2s(const struct GNUNET_Uuid *uuid)
1290 * @return string form; will be overwritten by next call to GNUNET_h2s_full. 1292 * @return string form; will be overwritten by next call to GNUNET_h2s_full.
1291 */ 1293 */
1292const char * 1294const char *
1293GNUNET_h2s_full(const struct GNUNET_HashCode *hc) 1295GNUNET_h2s_full (const struct GNUNET_HashCode *hc)
1294{ 1296{
1295 static struct GNUNET_CRYPTO_HashAsciiEncoded ret; 1297 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1296 1298
1297 GNUNET_CRYPTO_hash_to_enc(hc, &ret); 1299 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1298 ret.encoding[sizeof(ret) - 1] = '\0'; 1300 ret.encoding[sizeof(ret) - 1] = '\0';
1299 return (const char *)ret.encoding; 1301 return (const char *) ret.encoding;
1300} 1302}
1301 1303
1302 1304
@@ -1308,16 +1310,16 @@ GNUNET_h2s_full(const struct GNUNET_HashCode *hc)
1308 * call to #GNUNET_i2s. 1310 * call to #GNUNET_i2s.
1309 */ 1311 */
1310const char * 1312const char *
1311GNUNET_i2s(const struct GNUNET_PeerIdentity *pid) 1313GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
1312{ 1314{
1313 static GNUNET_THREAD_LOCAL char buf[5]; 1315 static GNUNET_THREAD_LOCAL char buf[5];
1314 char *ret; 1316 char *ret;
1315 1317
1316 if (NULL == pid) 1318 if (NULL == pid)
1317 return "NULL"; 1319 return "NULL";
1318 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&pid->public_key); 1320 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1319 GNUNET_strlcpy(buf, ret, sizeof(buf)); 1321 GNUNET_strlcpy (buf, ret, sizeof(buf));
1320 GNUNET_free(ret); 1322 GNUNET_free (ret);
1321 return buf; 1323 return buf;
1322} 1324}
1323 1325
@@ -1333,16 +1335,16 @@ GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
1333 * call to #GNUNET_i2s. 1335 * call to #GNUNET_i2s.
1334 */ 1336 */
1335const char * 1337const char *
1336GNUNET_i2s2(const struct GNUNET_PeerIdentity *pid) 1338GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid)
1337{ 1339{
1338 static GNUNET_THREAD_LOCAL char buf[5]; 1340 static GNUNET_THREAD_LOCAL char buf[5];
1339 char *ret; 1341 char *ret;
1340 1342
1341 if (NULL == pid) 1343 if (NULL == pid)
1342 return "NULL"; 1344 return "NULL";
1343 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&pid->public_key); 1345 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1344 GNUNET_strlcpy(buf, ret, sizeof(buf)); 1346 GNUNET_strlcpy (buf, ret, sizeof(buf));
1345 GNUNET_free(ret); 1347 GNUNET_free (ret);
1346 return buf; 1348 return buf;
1347} 1349}
1348 1350
@@ -1355,14 +1357,14 @@ GNUNET_i2s2(const struct GNUNET_PeerIdentity *pid)
1355 * call to #GNUNET_i2s_full. 1357 * call to #GNUNET_i2s_full.
1356 */ 1358 */
1357const char * 1359const char *
1358GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid) 1360GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
1359{ 1361{
1360 static GNUNET_THREAD_LOCAL char buf[256]; 1362 static GNUNET_THREAD_LOCAL char buf[256];
1361 char *ret; 1363 char *ret;
1362 1364
1363 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&pid->public_key); 1365 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1364 strcpy(buf, ret); 1366 strcpy (buf, ret);
1365 GNUNET_free(ret); 1367 GNUNET_free (ret);
1366 return buf; 1368 return buf;
1367} 1369}
1368 1370
@@ -1378,11 +1380,11 @@ GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
1378 * will be overwritten by next call to #GNUNET_a2s. 1380 * will be overwritten by next call to #GNUNET_a2s.
1379 */ 1381 */
1380const char * 1382const char *
1381GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen) 1383GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
1382{ 1384{
1383#define LEN \ 1385#define LEN \
1384 GNUNET_MAX((INET6_ADDRSTRLEN + 8), \ 1386 GNUNET_MAX ((INET6_ADDRSTRLEN + 8), \
1385 (1 + sizeof(struct sockaddr_un) - sizeof(sa_family_t))) 1387 (1 + sizeof(struct sockaddr_un) - sizeof(sa_family_t)))
1386 static char buf[LEN]; 1388 static char buf[LEN];
1387#undef LEN 1389#undef LEN
1388 static char b2[6]; 1390 static char b2[6];
@@ -1392,53 +1394,53 @@ GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
1392 unsigned int off; 1394 unsigned int off;
1393 1395
1394 if (addr == NULL) 1396 if (addr == NULL)
1395 return _("unknown address"); 1397 return _ ("unknown address");
1396 switch (addr->sa_family) 1398 switch (addr->sa_family)
1397 { 1399 {
1398 case AF_INET: 1400 case AF_INET:
1399 if (addrlen != sizeof(struct sockaddr_in)) 1401 if (addrlen != sizeof(struct sockaddr_in))
1400 return "<invalid v4 address>"; 1402 return "<invalid v4 address>";
1401 v4 = (const struct sockaddr_in *)addr; 1403 v4 = (const struct sockaddr_in *) addr;
1402 inet_ntop(AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); 1404 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
1403 if (0 == ntohs(v4->sin_port)) 1405 if (0 == ntohs (v4->sin_port))
1404 return buf;
1405 strcat(buf, ":");
1406 GNUNET_snprintf(b2, sizeof(b2), "%u", ntohs(v4->sin_port));
1407 strcat(buf, b2);
1408 return buf;
1409
1410 case AF_INET6:
1411 if (addrlen != sizeof(struct sockaddr_in6))
1412 return "<invalid v4 address>";
1413 v6 = (const struct sockaddr_in6 *)addr;
1414 buf[0] = '[';
1415 inet_ntop(AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
1416 if (0 == ntohs(v6->sin6_port))
1417 return &buf[1];
1418 strcat(buf, "]:");
1419 GNUNET_snprintf(b2, sizeof(b2), "%u", ntohs(v6->sin6_port));
1420 strcat(buf, b2);
1421 return buf;
1422
1423 case AF_UNIX:
1424 if (addrlen <= sizeof(sa_family_t))
1425 return "<unbound UNIX client>";
1426 un = (const struct sockaddr_un *)addr;
1427 off = 0;
1428 if ('\0' == un->sun_path[0])
1429 off++;
1430 memset(buf, 0, sizeof(buf));
1431 GNUNET_snprintf(buf,
1432 sizeof(buf),
1433 "%s%.*s",
1434 (1 == off) ? "@" : "",
1435 (int)(addrlen - sizeof(sa_family_t) - off),
1436 &un->sun_path[off]);
1437 return buf; 1406 return buf;
1438 1407 strcat (buf, ":");
1439 default: 1408 GNUNET_snprintf (b2, sizeof(b2), "%u", ntohs (v4->sin_port));
1440 return _("invalid address"); 1409 strcat (buf, b2);
1441 } 1410 return buf;
1411
1412 case AF_INET6:
1413 if (addrlen != sizeof(struct sockaddr_in6))
1414 return "<invalid v4 address>";
1415 v6 = (const struct sockaddr_in6 *) addr;
1416 buf[0] = '[';
1417 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
1418 if (0 == ntohs (v6->sin6_port))
1419 return &buf[1];
1420 strcat (buf, "]:");
1421 GNUNET_snprintf (b2, sizeof(b2), "%u", ntohs (v6->sin6_port));
1422 strcat (buf, b2);
1423 return buf;
1424
1425 case AF_UNIX:
1426 if (addrlen <= sizeof(sa_family_t))
1427 return "<unbound UNIX client>";
1428 un = (const struct sockaddr_un *) addr;
1429 off = 0;
1430 if ('\0' == un->sun_path[0])
1431 off++;
1432 memset (buf, 0, sizeof(buf));
1433 GNUNET_snprintf (buf,
1434 sizeof(buf),
1435 "%s%.*s",
1436 (1 == off) ? "@" : "",
1437 (int) (addrlen - sizeof(sa_family_t) - off),
1438 &un->sun_path[off]);
1439 return buf;
1440
1441 default:
1442 return _ ("invalid address");
1443 }
1442} 1444}
1443 1445
1444 1446
@@ -1450,15 +1452,15 @@ GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
1450 * @param option name of missing option 1452 * @param option name of missing option
1451 */ 1453 */
1452void 1454void
1453GNUNET_log_config_missing(enum GNUNET_ErrorType kind, 1455GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
1454 const char *section, 1456 const char *section,
1455 const char *option) 1457 const char *option)
1456{ 1458{
1457 GNUNET_log(kind, 1459 GNUNET_log (kind,
1458 _( 1460 _ (
1459 "Configuration fails to specify option `%s' in section `%s'!\n"), 1461 "Configuration fails to specify option `%s' in section `%s'!\n"),
1460 option, 1462 option,
1461 section); 1463 section);
1462} 1464}
1463 1465
1464 1466
@@ -1471,14 +1473,14 @@ GNUNET_log_config_missing(enum GNUNET_ErrorType kind,
1471 * @param required what is required that is invalid about the option 1473 * @param required what is required that is invalid about the option
1472 */ 1474 */
1473void 1475void
1474GNUNET_log_config_invalid(enum GNUNET_ErrorType kind, 1476GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
1475 const char *section, 1477 const char *section,
1476 const char *option, 1478 const char *option,
1477 const char *required) 1479 const char *required)
1478{ 1480{
1479 GNUNET_log( 1481 GNUNET_log (
1480 kind, 1482 kind,
1481 _( 1483 _ (
1482 "Configuration specifies invalid value for option `%s' in section `%s': %s\n"), 1484 "Configuration specifies invalid value for option `%s' in section `%s': %s\n"),
1483 option, 1485 option,
1484 section, 1486 section,
@@ -1493,8 +1495,8 @@ GNUNET_log_config_invalid(enum GNUNET_ErrorType kind,
1493 * @param old_scope[out] location to save the old scope 1495 * @param old_scope[out] location to save the old scope
1494 */ 1496 */
1495void 1497void
1496GNUNET_async_scope_enter(const struct GNUNET_AsyncScopeId *aid, 1498GNUNET_async_scope_enter (const struct GNUNET_AsyncScopeId *aid,
1497 struct GNUNET_AsyncScopeSave *old_scope) 1499 struct GNUNET_AsyncScopeSave *old_scope)
1498{ 1500{
1499 *old_scope = current_async_scope; 1501 *old_scope = current_async_scope;
1500 current_async_scope.have_scope = GNUNET_YES; 1502 current_async_scope.have_scope = GNUNET_YES;
@@ -1508,7 +1510,7 @@ GNUNET_async_scope_enter(const struct GNUNET_AsyncScopeId *aid,
1508 * @param old_scope scope to restore 1510 * @param old_scope scope to restore
1509 */ 1511 */
1510void 1512void
1511GNUNET_async_scope_restore(struct GNUNET_AsyncScopeSave *old_scope) 1513GNUNET_async_scope_restore (struct GNUNET_AsyncScopeSave *old_scope)
1512{ 1514{
1513 current_async_scope = *old_scope; 1515 current_async_scope = *old_scope;
1514} 1516}
@@ -1520,11 +1522,11 @@ GNUNET_async_scope_restore(struct GNUNET_AsyncScopeSave *old_scope)
1520 * @param[out] aid_ret pointer to where the result is stored 1522 * @param[out] aid_ret pointer to where the result is stored
1521 */ 1523 */
1522void 1524void
1523GNUNET_async_scope_fresh(struct GNUNET_AsyncScopeId *aid_ret) 1525GNUNET_async_scope_fresh (struct GNUNET_AsyncScopeId *aid_ret)
1524{ 1526{
1525 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, 1527 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
1526 aid_ret, 1528 aid_ret,
1527 sizeof(struct GNUNET_AsyncScopeId)); 1529 sizeof(struct GNUNET_AsyncScopeId));
1528} 1530}
1529 1531
1530 1532
@@ -1534,7 +1536,7 @@ GNUNET_async_scope_fresh(struct GNUNET_AsyncScopeId *aid_ret)
1534 * @param[out] scope_ret pointer to where the result is stored 1536 * @param[out] scope_ret pointer to where the result is stored
1535 */ 1537 */
1536void 1538void
1537GNUNET_async_scope_get(struct GNUNET_AsyncScopeSave *scope_ret) 1539GNUNET_async_scope_get (struct GNUNET_AsyncScopeSave *scope_ret)
1538{ 1540{
1539 *scope_ret = current_async_scope; 1541 *scope_ret = current_async_scope;
1540} 1542}
@@ -1543,7 +1545,7 @@ GNUNET_async_scope_get(struct GNUNET_AsyncScopeSave *scope_ret)
1543/** 1545/**
1544 * Initializer 1546 * Initializer
1545 */ 1547 */
1546void __attribute__ ((constructor)) GNUNET_util_cl_init() 1548void __attribute__ ((constructor)) GNUNET_util_cl_init ()
1547{ 1549{
1548 GNUNET_stderr = stderr; 1550 GNUNET_stderr = stderr;
1549} 1551}
@@ -1552,7 +1554,7 @@ void __attribute__ ((constructor)) GNUNET_util_cl_init()
1552/** 1554/**
1553 * Destructor 1555 * Destructor
1554 */ 1556 */
1555void __attribute__ ((destructor)) GNUNET_util_cl_fini() 1557void __attribute__ ((destructor)) GNUNET_util_cl_fini ()
1556{ 1558{
1557 1559
1558} 1560}
diff --git a/src/util/configuration.c b/src/util/configuration.c
index 6d72d4fe0..cb6932b15 100644
--- a/src/util/configuration.c
+++ b/src/util/configuration.c
@@ -30,15 +30,16 @@
30#include "gnunet_configuration_lib.h" 30#include "gnunet_configuration_lib.h"
31#include "gnunet_disk_lib.h" 31#include "gnunet_disk_lib.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from(kind, "util", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 34
35#define LOG_STRERROR_FILE(kind, syscall, filename) \ 35#define LOG_STRERROR_FILE(kind, syscall, filename) \
36 GNUNET_log_from_strerror_file(kind, "util", syscall, filename) 36 GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
37 37
38/** 38/**
39 * @brief configuration entry 39 * @brief configuration entry
40 */ 40 */
41struct ConfigEntry { 41struct ConfigEntry
42{
42 /** 43 /**
43 * This is a linked list. 44 * This is a linked list.
44 */ 45 */
@@ -59,7 +60,8 @@ struct ConfigEntry {
59/** 60/**
60 * @brief configuration section 61 * @brief configuration section
61 */ 62 */
62struct ConfigSection { 63struct ConfigSection
64{
63 /** 65 /**
64 * This is a linked list. 66 * This is a linked list.
65 */ 67 */
@@ -80,7 +82,8 @@ struct ConfigSection {
80/** 82/**
81 * @brief configuration data 83 * @brief configuration data
82 */ 84 */
83struct GNUNET_CONFIGURATION_Handle { 85struct GNUNET_CONFIGURATION_Handle
86{
84 /** 87 /**
85 * Configuration sections. 88 * Configuration sections.
86 */ 89 */
@@ -99,7 +102,8 @@ struct GNUNET_CONFIGURATION_Handle {
99 * Used for diffing a configuration object against 102 * Used for diffing a configuration object against
100 * the default one 103 * the default one
101 */ 104 */
102struct DiffHandle { 105struct DiffHandle
106{
103 const struct GNUNET_CONFIGURATION_Handle *cfg_default; 107 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
104 108
105 struct GNUNET_CONFIGURATION_Handle *cfgDiff; 109 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
@@ -112,9 +116,9 @@ struct DiffHandle {
112 * @return fresh configuration object 116 * @return fresh configuration object
113 */ 117 */
114struct GNUNET_CONFIGURATION_Handle * 118struct GNUNET_CONFIGURATION_Handle *
115GNUNET_CONFIGURATION_create() 119GNUNET_CONFIGURATION_create ()
116{ 120{
117 return GNUNET_new(struct GNUNET_CONFIGURATION_Handle); 121 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
118} 122}
119 123
120 124
@@ -124,13 +128,13 @@ GNUNET_CONFIGURATION_create()
124 * @param cfg configuration to destroy 128 * @param cfg configuration to destroy
125 */ 129 */
126void 130void
127GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg) 131GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
128{ 132{
129 struct ConfigSection *sec; 133 struct ConfigSection *sec;
130 134
131 while (NULL != (sec = cfg->sections)) 135 while (NULL != (sec = cfg->sections))
132 GNUNET_CONFIGURATION_remove_section(cfg, sec->name); 136 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
133 GNUNET_free(cfg); 137 GNUNET_free (cfg);
134} 138}
135 139
136 140
@@ -146,22 +150,22 @@ GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
146 * otherwise return value from @a cb. 150 * otherwise return value from @a cb.
147 */ 151 */
148int 152int
149GNUNET_CONFIGURATION_parse_and_run(const char *filename, 153GNUNET_CONFIGURATION_parse_and_run (const char *filename,
150 GNUNET_CONFIGURATION_Callback cb, 154 GNUNET_CONFIGURATION_Callback cb,
151 void *cb_cls) 155 void *cb_cls)
152{ 156{
153 struct GNUNET_CONFIGURATION_Handle *cfg; 157 struct GNUNET_CONFIGURATION_Handle *cfg;
154 int ret; 158 int ret;
155 159
156 cfg = GNUNET_CONFIGURATION_create(); 160 cfg = GNUNET_CONFIGURATION_create ();
157 if (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg, filename)) 161 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, filename))
158 { 162 {
159 GNUNET_break(0); 163 GNUNET_break (0);
160 GNUNET_CONFIGURATION_destroy(cfg); 164 GNUNET_CONFIGURATION_destroy (cfg);
161 return GNUNET_SYSERR; 165 return GNUNET_SYSERR;
162 } 166 }
163 ret = cb(cb_cls, cfg); 167 ret = cb (cb_cls, cfg);
164 GNUNET_CONFIGURATION_destroy(cfg); 168 GNUNET_CONFIGURATION_destroy (cfg);
165 return ret; 169 return ret;
166} 170}
167 171
@@ -178,10 +182,10 @@ GNUNET_CONFIGURATION_parse_and_run(const char *filename,
178 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 182 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
179 */ 183 */
180int 184int
181GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg, 185GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
182 const char *mem, 186 const char *mem,
183 size_t size, 187 size_t size,
184 const char *basedir) 188 const char *basedir)
185{ 189{
186 char *line; 190 char *line;
187 char *line_orig; 191 char *line_orig;
@@ -199,133 +203,133 @@ GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg,
199 char *value; 203 char *value;
200 204
201 ret = GNUNET_OK; 205 ret = GNUNET_OK;
202 section = GNUNET_strdup(""); 206 section = GNUNET_strdup ("");
203 nr = 0; 207 nr = 0;
204 r_bytes = 0; 208 r_bytes = 0;
205 line_orig = NULL; 209 line_orig = NULL;
206 while (r_bytes < size) 210 while (r_bytes < size)
211 {
212 GNUNET_free_non_null (line_orig);
213 /* fgets-like behaviour on buffer */
214 to_read = size - r_bytes;
215 pos = memchr (&mem[r_bytes], '\n', to_read);
216 if (NULL == pos)
207 { 217 {
208 GNUNET_free_non_null(line_orig); 218 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
209 /* fgets-like behaviour on buffer */ 219 r_bytes += line_size;
210 to_read = size - r_bytes; 220 }
211 pos = memchr(&mem[r_bytes], '\n', to_read); 221 else
212 if (NULL == pos) 222 {
213 { 223 line_orig =
214 line_orig = GNUNET_strndup(&mem[r_bytes], line_size = to_read); 224 GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
215 r_bytes += line_size; 225 r_bytes += line_size + 1;
216 } 226 }
217 else 227 line = line_orig;
218 { 228 /* increment line number */
219 line_orig = 229 nr++;
220 GNUNET_strndup(&mem[r_bytes], line_size = (pos - &mem[r_bytes])); 230 /* tabs and '\r' are whitespace */
221 r_bytes += line_size + 1; 231 emptyline = GNUNET_YES;
222 } 232 for (i = 0; i < line_size; i++)
223 line = line_orig; 233 {
224 /* increment line number */ 234 if (line[i] == '\t')
225 nr++; 235 line[i] = ' ';
226 /* tabs and '\r' are whitespace */ 236 if (line[i] == '\r')
227 emptyline = GNUNET_YES; 237 line[i] = ' ';
228 for (i = 0; i < line_size; i++) 238 if (' ' != line[i])
229 { 239 emptyline = GNUNET_NO;
230 if (line[i] == '\t') 240 }
231 line[i] = ' '; 241 /* ignore empty lines */
232 if (line[i] == '\r') 242 if (GNUNET_YES == emptyline)
233 line[i] = ' '; 243 continue;
234 if (' ' != line[i])
235 emptyline = GNUNET_NO;
236 }
237 /* ignore empty lines */
238 if (GNUNET_YES == emptyline)
239 continue;
240 244
241 /* remove tailing whitespace */ 245 /* remove tailing whitespace */
242 for (i = line_size - 1; (i >= 1) && (isspace((unsigned char)line[i])); 246 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));
243 i--) 247 i--)
244 line[i] = '\0'; 248 line[i] = '\0';
245 249
246 /* remove leading whitespace */ 250 /* remove leading whitespace */
247 for (; line[0] != '\0' && (isspace((unsigned char)line[0])); line++) 251 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++)
248 ; 252 ;
249 253
250 /* ignore comments */ 254 /* ignore comments */
251 if (('#' == line[0]) || ('%' == line[0])) 255 if (('#' == line[0]) || ('%' == line[0]))
252 continue; 256 continue;
253 257
254 /* handle special "@INLINE@" directive */ 258 /* handle special "@INLINE@" directive */
255 if (0 == strncasecmp(line, "@INLINE@ ", strlen("@INLINE@ "))) 259 if (0 == strncasecmp (line, "@INLINE@ ", strlen ("@INLINE@ ")))
256 { 260 {
257 /* @INLINE@ value */ 261 /* @INLINE@ value */
258 value = &line[strlen("@INLINE@ ")]; 262 value = &line[strlen ("@INLINE@ ")];
259 if (NULL != basedir) 263 if (NULL != basedir)
260 { 264 {
261 char *fn; 265 char *fn;
262 266
263 GNUNET_asprintf(&fn, "%s/%s", basedir, value); 267 GNUNET_asprintf (&fn, "%s/%s", basedir, value);
264 if (GNUNET_OK != GNUNET_CONFIGURATION_parse(cfg, fn)) 268 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, fn))
265 {
266 GNUNET_free(fn);
267 ret = GNUNET_SYSERR; /* failed to parse included config */
268 break;
269 }
270 GNUNET_free(fn);
271 }
272 else
273 {
274 LOG(GNUNET_ERROR_TYPE_DEBUG,
275 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
276 ret = GNUNET_SYSERR;
277 break;
278 }
279 continue;
280 }
281 if (('[' == line[0]) && (']' == line[line_size - 1]))
282 {
283 /* [value] */
284 line[line_size - 1] = '\0';
285 value = &line[1];
286 GNUNET_free(section);
287 section = GNUNET_strdup(value);
288 continue;
289 }
290 if (NULL != (eq = strchr(line, '=')))
291 { 269 {
292 /* tag = value */ 270 GNUNET_free (fn);
293 tag = GNUNET_strndup(line, eq - line); 271 ret = GNUNET_SYSERR; /* failed to parse included config */
294 /* remove tailing whitespace */ 272 break;
295 for (i = strlen(tag) - 1; (i >= 1) && (isspace((unsigned char)tag[i]));
296 i--)
297 tag[i] = '\0';
298
299 /* Strip whitespace */
300 value = eq + 1;
301 while (isspace((unsigned char)value[0]))
302 value++;
303 for (i = strlen(value) - 1;
304 (i >= 1) && (isspace((unsigned char)value[i]));
305 i--)
306 value[i] = '\0';
307
308 /* remove quotes */
309 i = 0;
310 if (('"' == value[0]) && ('"' == value[strlen(value) - 1]))
311 {
312 value[strlen(value) - 1] = '\0';
313 value++;
314 }
315 GNUNET_CONFIGURATION_set_value_string(cfg, section, tag, &value[i]);
316 GNUNET_free(tag);
317 continue;
318 } 273 }
319 /* parse error */ 274 GNUNET_free (fn);
320 LOG(GNUNET_ERROR_TYPE_WARNING, 275 }
321 _("Syntax error while deserializing in line %u\n"), 276 else
322 nr); 277 {
323 ret = GNUNET_SYSERR; 278 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 break; 279 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
280 ret = GNUNET_SYSERR;
281 break;
282 }
283 continue;
284 }
285 if (('[' == line[0]) && (']' == line[line_size - 1]))
286 {
287 /* [value] */
288 line[line_size - 1] = '\0';
289 value = &line[1];
290 GNUNET_free (section);
291 section = GNUNET_strdup (value);
292 continue;
325 } 293 }
326 GNUNET_free_non_null(line_orig); 294 if (NULL != (eq = strchr (line, '=')))
327 GNUNET_free(section); 295 {
328 GNUNET_assert((GNUNET_OK != ret) || (r_bytes == size)); 296 /* tag = value */
297 tag = GNUNET_strndup (line, eq - line);
298 /* remove tailing whitespace */
299 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));
300 i--)
301 tag[i] = '\0';
302
303 /* Strip whitespace */
304 value = eq + 1;
305 while (isspace ((unsigned char) value[0]))
306 value++;
307 for (i = strlen (value) - 1;
308 (i >= 1) && (isspace ((unsigned char) value[i]));
309 i--)
310 value[i] = '\0';
311
312 /* remove quotes */
313 i = 0;
314 if (('"' == value[0]) && ('"' == value[strlen (value) - 1]))
315 {
316 value[strlen (value) - 1] = '\0';
317 value++;
318 }
319 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
320 GNUNET_free (tag);
321 continue;
322 }
323 /* parse error */
324 LOG (GNUNET_ERROR_TYPE_WARNING,
325 _ ("Syntax error while deserializing in line %u\n"),
326 nr);
327 ret = GNUNET_SYSERR;
328 break;
329 }
330 GNUNET_free_non_null (line_orig);
331 GNUNET_free (section);
332 GNUNET_assert ((GNUNET_OK != ret) || (r_bytes == size));
329 return ret; 333 return ret;
330} 334}
331 335
@@ -339,8 +343,8 @@ GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg,
339 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 343 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
340 */ 344 */
341int 345int
342GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg, 346GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
343 const char *filename) 347 const char *filename)
344{ 348{
345 uint64_t fs64; 349 uint64_t fs64;
346 size_t fs; 350 size_t fs;
@@ -351,43 +355,43 @@ GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg,
351 int ret; 355 int ret;
352 ssize_t sret; 356 ssize_t sret;
353 357
354 fn = GNUNET_STRINGS_filename_expand(filename); 358 fn = GNUNET_STRINGS_filename_expand (filename);
355 LOG(GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", fn); 359 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", fn);
356 if (NULL == fn) 360 if (NULL == fn)
357 return GNUNET_SYSERR; 361 return GNUNET_SYSERR;
358 dirty = cfg->dirty; /* back up value! */ 362 dirty = cfg->dirty; /* back up value! */
359 if (GNUNET_SYSERR == 363 if (GNUNET_SYSERR ==
360 GNUNET_DISK_file_size(fn, &fs64, GNUNET_YES, GNUNET_YES)) 364 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
361 { 365 {
362 LOG(GNUNET_ERROR_TYPE_WARNING, 366 LOG (GNUNET_ERROR_TYPE_WARNING,
363 "Error while determining the file size of `%s'\n", 367 "Error while determining the file size of `%s'\n",
364 fn); 368 fn);
365 GNUNET_free(fn); 369 GNUNET_free (fn);
366 return GNUNET_SYSERR; 370 return GNUNET_SYSERR;
367 } 371 }
368 if (fs64 > SIZE_MAX) 372 if (fs64 > SIZE_MAX)
369 { 373 {
370 GNUNET_break(0); /* File size is more than the heap size */ 374 GNUNET_break (0); /* File size is more than the heap size */
371 GNUNET_free(fn); 375 GNUNET_free (fn);
372 return GNUNET_SYSERR; 376 return GNUNET_SYSERR;
373 } 377 }
374 fs = fs64; 378 fs = fs64;
375 mem = GNUNET_malloc(fs); 379 mem = GNUNET_malloc (fs);
376 sret = GNUNET_DISK_fn_read(fn, mem, fs); 380 sret = GNUNET_DISK_fn_read (fn, mem, fs);
377 if ((sret < 0) || (fs != (size_t)sret)) 381 if ((sret < 0) || (fs != (size_t) sret))
378 { 382 {
379 LOG(GNUNET_ERROR_TYPE_WARNING, _("Error while reading file `%s'\n"), fn); 383 LOG (GNUNET_ERROR_TYPE_WARNING, _ ("Error while reading file `%s'\n"), fn);
380 GNUNET_free(fn); 384 GNUNET_free (fn);
381 GNUNET_free(mem); 385 GNUNET_free (mem);
382 return GNUNET_SYSERR; 386 return GNUNET_SYSERR;
383 } 387 }
384 LOG(GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn); 388 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
385 endsep = strrchr(fn, (int)'/'); 389 endsep = strrchr (fn, (int) '/');
386 if (NULL != endsep) 390 if (NULL != endsep)
387 *endsep = '\0'; 391 *endsep = '\0';
388 ret = GNUNET_CONFIGURATION_deserialize(cfg, mem, fs, fn); 392 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, fn);
389 GNUNET_free(fn); 393 GNUNET_free (fn);
390 GNUNET_free(mem); 394 GNUNET_free (mem);
391 /* restore dirty flag - anything we set in the meantime 395 /* restore dirty flag - anything we set in the meantime
392 * came from disk */ 396 * came from disk */
393 cfg->dirty = dirty; 397 cfg->dirty = dirty;
@@ -403,7 +407,7 @@ GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg,
403 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed) 407 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
404 */ 408 */
405int 409int
406GNUNET_CONFIGURATION_is_dirty(const struct GNUNET_CONFIGURATION_Handle *cfg) 410GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
407{ 411{
408 return cfg->dirty; 412 return cfg->dirty;
409} 413}
@@ -418,8 +422,8 @@ GNUNET_CONFIGURATION_is_dirty(const struct GNUNET_CONFIGURATION_Handle *cfg)
418 * present. This memory should be freed by the caller 422 * present. This memory should be freed by the caller
419 */ 423 */
420char * 424char *
421GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg, 425GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
422 size_t *size) 426 size_t *size)
423{ 427{
424 struct ConfigSection *sec; 428 struct ConfigSection *sec;
425 struct ConfigEntry *ent; 429 struct ConfigEntry *ent;
@@ -433,67 +437,67 @@ GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg,
433 /* Pass1 : calculate the buffer size required */ 437 /* Pass1 : calculate the buffer size required */
434 m_size = 0; 438 m_size = 0;
435 for (sec = cfg->sections; NULL != sec; sec = sec->next) 439 for (sec = cfg->sections; NULL != sec; sec = sec->next)
440 {
441 /* For each section we need to add 3 charaters: {'[',']','\n'} */
442 m_size += strlen (sec->name) + 3;
443 for (ent = sec->entries; NULL != ent; ent = ent->next)
436 { 444 {
437 /* For each section we need to add 3 charaters: {'[',']','\n'} */ 445 if (NULL != ent->val)
438 m_size += strlen(sec->name) + 3; 446 {
439 for (ent = sec->entries; NULL != ent; ent = ent->next) 447 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
448 pos = ent->val;
449 while (NULL != (pos = strstr (pos, "\n")))
440 { 450 {
441 if (NULL != ent->val) 451 m_size++;
442 { 452 pos++;
443 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
444 pos = ent->val;
445 while (NULL != (pos = strstr(pos, "\n")))
446 {
447 m_size++;
448 pos++;
449 }
450 /* For each key = value pair we need to add 4 characters (2
451 spaces and 1 equal-to character and 1 new line) */
452 m_size += strlen(ent->key) + strlen(ent->val) + 4;
453 }
454 } 453 }
455 /* A new line after section end */ 454 /* For each key = value pair we need to add 4 characters (2
456 m_size++; 455 spaces and 1 equal-to character and 1 new line) */
456 m_size += strlen (ent->key) + strlen (ent->val) + 4;
457 }
457 } 458 }
459 /* A new line after section end */
460 m_size++;
461 }
458 462
459 /* Pass2: Allocate memory and write the configuration to it */ 463 /* Pass2: Allocate memory and write the configuration to it */
460 mem = GNUNET_malloc(m_size); 464 mem = GNUNET_malloc (m_size);
461 sec = cfg->sections; 465 sec = cfg->sections;
462 c_size = 0; 466 c_size = 0;
463 *size = c_size; 467 *size = c_size;
464 while (NULL != sec) 468 while (NULL != sec)
469 {
470 int len;
471
472 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
473 GNUNET_assert (0 < len);
474 GNUNET_memcpy (mem + c_size, cbuf, len);
475 c_size += len;
476 GNUNET_free (cbuf);
477 for (ent = sec->entries; NULL != ent; ent = ent->next)
465 { 478 {
466 int len; 479 if (NULL != ent->val)
467 480 {
468 len = GNUNET_asprintf(&cbuf, "[%s]\n", sec->name); 481 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
469 GNUNET_assert(0 < len); 482 strcpy (val, ent->val);
470 GNUNET_memcpy(mem + c_size, cbuf, len); 483 while (NULL != (pos = strstr (val, "\n")))
471 c_size += len;
472 GNUNET_free(cbuf);
473 for (ent = sec->entries; NULL != ent; ent = ent->next)
474 { 484 {
475 if (NULL != ent->val) 485 memmove (&pos[2], &pos[1], strlen (&pos[1]));
476 { 486 pos[0] = '\\';
477 val = GNUNET_malloc(strlen(ent->val) * 2 + 1); 487 pos[1] = 'n';
478 strcpy(val, ent->val);
479 while (NULL != (pos = strstr(val, "\n")))
480 {
481 memmove(&pos[2], &pos[1], strlen(&pos[1]));
482 pos[0] = '\\';
483 pos[1] = 'n';
484 }
485 len = GNUNET_asprintf(&cbuf, "%s = %s\n", ent->key, val);
486 GNUNET_free(val);
487 GNUNET_memcpy(mem + c_size, cbuf, len);
488 c_size += len;
489 GNUNET_free(cbuf);
490 }
491 } 488 }
492 GNUNET_memcpy(mem + c_size, "\n", 1); 489 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
493 c_size++; 490 GNUNET_free (val);
494 sec = sec->next; 491 GNUNET_memcpy (mem + c_size, cbuf, len);
492 c_size += len;
493 GNUNET_free (cbuf);
494 }
495 } 495 }
496 GNUNET_assert(c_size == m_size); 496 GNUNET_memcpy (mem + c_size, "\n", 1);
497 c_size++;
498 sec = sec->next;
499 }
500 GNUNET_assert (c_size == m_size);
497 *size = c_size; 501 *size = c_size;
498 return mem; 502 return mem;
499} 503}
@@ -507,42 +511,42 @@ GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg,
507 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 511 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
508 */ 512 */
509int 513int
510GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, 514GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
511 const char *filename) 515 const char *filename)
512{ 516{
513 char *fn; 517 char *fn;
514 char *cfg_buf; 518 char *cfg_buf;
515 size_t size; 519 size_t size;
516 ssize_t sret; 520 ssize_t sret;
517 521
518 fn = GNUNET_STRINGS_filename_expand(filename); 522 fn = GNUNET_STRINGS_filename_expand (filename);
519 if (fn == NULL) 523 if (fn == NULL)
520 return GNUNET_SYSERR; 524 return GNUNET_SYSERR;
521 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file(fn)) 525 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
522 { 526 {
523 GNUNET_free(fn); 527 GNUNET_free (fn);
524 return GNUNET_SYSERR; 528 return GNUNET_SYSERR;
525 } 529 }
526 cfg_buf = GNUNET_CONFIGURATION_serialize(cfg, &size); 530 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
527 sret = GNUNET_DISK_fn_write(fn, 531 sret = GNUNET_DISK_fn_write (fn,
528 cfg_buf, 532 cfg_buf,
529 size, 533 size,
530 GNUNET_DISK_PERM_USER_READ | 534 GNUNET_DISK_PERM_USER_READ
531 GNUNET_DISK_PERM_USER_WRITE | 535 | GNUNET_DISK_PERM_USER_WRITE
532 GNUNET_DISK_PERM_GROUP_READ | 536 | GNUNET_DISK_PERM_GROUP_READ
533 GNUNET_DISK_PERM_GROUP_WRITE); 537 | GNUNET_DISK_PERM_GROUP_WRITE);
534 if ((sret < 0) || (size != (size_t)sret)) 538 if ((sret < 0) || (size != (size_t) sret))
535 { 539 {
536 GNUNET_free(fn); 540 GNUNET_free (fn);
537 GNUNET_free(cfg_buf); 541 GNUNET_free (cfg_buf);
538 LOG(GNUNET_ERROR_TYPE_WARNING, 542 LOG (GNUNET_ERROR_TYPE_WARNING,
539 "Writing configuration to file `%s' failed\n", 543 "Writing configuration to file `%s' failed\n",
540 filename); 544 filename);
541 cfg->dirty = GNUNET_SYSERR; /* last write failed */ 545 cfg->dirty = GNUNET_SYSERR; /* last write failed */
542 return GNUNET_SYSERR; 546 return GNUNET_SYSERR;
543 } 547 }
544 GNUNET_free(fn); 548 GNUNET_free (fn);
545 GNUNET_free(cfg_buf); 549 GNUNET_free (cfg_buf);
546 cfg->dirty = GNUNET_NO; /* last write succeeded */ 550 cfg->dirty = GNUNET_NO; /* last write succeeded */
547 return GNUNET_OK; 551 return GNUNET_OK;
548} 552}
@@ -556,9 +560,9 @@ GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg,
556 * @param iter_cls closure for @a iter 560 * @param iter_cls closure for @a iter
557 */ 561 */
558void 562void
559GNUNET_CONFIGURATION_iterate(const struct GNUNET_CONFIGURATION_Handle *cfg, 563GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
560 GNUNET_CONFIGURATION_Iterator iter, 564 GNUNET_CONFIGURATION_Iterator iter,
561 void *iter_cls) 565 void *iter_cls)
562{ 566{
563 struct ConfigSection *spos; 567 struct ConfigSection *spos;
564 struct ConfigEntry *epos; 568 struct ConfigEntry *epos;
@@ -566,7 +570,7 @@ GNUNET_CONFIGURATION_iterate(const struct GNUNET_CONFIGURATION_Handle *cfg,
566 for (spos = cfg->sections; NULL != spos; spos = spos->next) 570 for (spos = cfg->sections; NULL != spos; spos = spos->next)
567 for (epos = spos->entries; NULL != epos; epos = epos->next) 571 for (epos = spos->entries; NULL != epos; epos = epos->next)
568 if (NULL != epos->val) 572 if (NULL != epos->val)
569 iter(iter_cls, spos->name, epos->key, epos->val); 573 iter (iter_cls, spos->name, epos->key, epos->val);
570} 574}
571 575
572 576
@@ -579,7 +583,7 @@ GNUNET_CONFIGURATION_iterate(const struct GNUNET_CONFIGURATION_Handle *cfg,
579 * @param iter_cls closure for @a iter 583 * @param iter_cls closure for @a iter
580 */ 584 */
581void 585void
582GNUNET_CONFIGURATION_iterate_section_values( 586GNUNET_CONFIGURATION_iterate_section_values (
583 const struct GNUNET_CONFIGURATION_Handle *cfg, 587 const struct GNUNET_CONFIGURATION_Handle *cfg,
584 const char *section, 588 const char *section,
585 GNUNET_CONFIGURATION_Iterator iter, 589 GNUNET_CONFIGURATION_Iterator iter,
@@ -589,13 +593,13 @@ GNUNET_CONFIGURATION_iterate_section_values(
589 struct ConfigEntry *epos; 593 struct ConfigEntry *epos;
590 594
591 spos = cfg->sections; 595 spos = cfg->sections;
592 while ((spos != NULL) && (0 != strcasecmp(spos->name, section))) 596 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
593 spos = spos->next; 597 spos = spos->next;
594 if (NULL == spos) 598 if (NULL == spos)
595 return; 599 return;
596 for (epos = spos->entries; NULL != epos; epos = epos->next) 600 for (epos = spos->entries; NULL != epos; epos = epos->next)
597 if (NULL != epos->val) 601 if (NULL != epos->val)
598 iter(iter_cls, spos->name, epos->key, epos->val); 602 iter (iter_cls, spos->name, epos->key, epos->val);
599} 603}
600 604
601 605
@@ -607,7 +611,7 @@ GNUNET_CONFIGURATION_iterate_section_values(
607 * @param iter_cls closure for @a iter 611 * @param iter_cls closure for @a iter
608 */ 612 */
609void 613void
610GNUNET_CONFIGURATION_iterate_sections( 614GNUNET_CONFIGURATION_iterate_sections (
611 const struct GNUNET_CONFIGURATION_Handle *cfg, 615 const struct GNUNET_CONFIGURATION_Handle *cfg,
612 GNUNET_CONFIGURATION_Section_Iterator iter, 616 GNUNET_CONFIGURATION_Section_Iterator iter,
613 void *iter_cls) 617 void *iter_cls)
@@ -617,11 +621,11 @@ GNUNET_CONFIGURATION_iterate_sections(
617 621
618 next = cfg->sections; 622 next = cfg->sections;
619 while (next != NULL) 623 while (next != NULL)
620 { 624 {
621 spos = next; 625 spos = next;
622 next = spos->next; 626 next = spos->next;
623 iter(iter_cls, spos->name); 627 iter (iter_cls, spos->name);
624 } 628 }
625} 629}
626 630
627 631
@@ -632,8 +636,8 @@ GNUNET_CONFIGURATION_iterate_sections(
632 * @param section name of the section to remove 636 * @param section name of the section to remove
633 */ 637 */
634void 638void
635GNUNET_CONFIGURATION_remove_section(struct GNUNET_CONFIGURATION_Handle *cfg, 639GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
636 const char *section) 640 const char *section)
637{ 641{
638 struct ConfigSection *spos; 642 struct ConfigSection *spos;
639 struct ConfigSection *prev; 643 struct ConfigSection *prev;
@@ -642,28 +646,28 @@ GNUNET_CONFIGURATION_remove_section(struct GNUNET_CONFIGURATION_Handle *cfg,
642 prev = NULL; 646 prev = NULL;
643 spos = cfg->sections; 647 spos = cfg->sections;
644 while (NULL != spos) 648 while (NULL != spos)
649 {
650 if (0 == strcasecmp (section, spos->name))
645 { 651 {
646 if (0 == strcasecmp(section, spos->name)) 652 if (NULL == prev)
647 { 653 cfg->sections = spos->next;
648 if (NULL == prev) 654 else
649 cfg->sections = spos->next; 655 prev->next = spos->next;
650 else 656 while (NULL != (ent = spos->entries))
651 prev->next = spos->next; 657 {
652 while (NULL != (ent = spos->entries)) 658 spos->entries = ent->next;
653 { 659 GNUNET_free (ent->key);
654 spos->entries = ent->next; 660 GNUNET_free_non_null (ent->val);
655 GNUNET_free(ent->key); 661 GNUNET_free (ent);
656 GNUNET_free_non_null(ent->val); 662 cfg->dirty = GNUNET_YES;
657 GNUNET_free(ent); 663 }
658 cfg->dirty = GNUNET_YES; 664 GNUNET_free (spos->name);
659 } 665 GNUNET_free (spos);
660 GNUNET_free(spos->name); 666 return;
661 GNUNET_free(spos);
662 return;
663 }
664 prev = spos;
665 spos = spos->next;
666 } 667 }
668 prev = spos;
669 spos = spos->next;
670 }
667} 671}
668 672
669 673
@@ -677,14 +681,14 @@ GNUNET_CONFIGURATION_remove_section(struct GNUNET_CONFIGURATION_Handle *cfg,
677 * @param value value to copy 681 * @param value value to copy
678 */ 682 */
679static void 683static void
680copy_entry(void *cls, 684copy_entry (void *cls,
681 const char *section, 685 const char *section,
682 const char *option, 686 const char *option,
683 const char *value) 687 const char *value)
684{ 688{
685 struct GNUNET_CONFIGURATION_Handle *dst = cls; 689 struct GNUNET_CONFIGURATION_Handle *dst = cls;
686 690
687 GNUNET_CONFIGURATION_set_value_string(dst, section, option, value); 691 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
688} 692}
689 693
690 694
@@ -695,12 +699,12 @@ copy_entry(void *cls,
695 * @return duplicate configuration 699 * @return duplicate configuration
696 */ 700 */
697struct GNUNET_CONFIGURATION_Handle * 701struct GNUNET_CONFIGURATION_Handle *
698GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg) 702GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
699{ 703{
700 struct GNUNET_CONFIGURATION_Handle *ret; 704 struct GNUNET_CONFIGURATION_Handle *ret;
701 705
702 ret = GNUNET_CONFIGURATION_create(); 706 ret = GNUNET_CONFIGURATION_create ();
703 GNUNET_CONFIGURATION_iterate(cfg, &copy_entry, ret); 707 GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
704 return ret; 708 return ret;
705} 709}
706 710
@@ -713,13 +717,13 @@ GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
713 * @return matching entry, NULL if not found 717 * @return matching entry, NULL if not found
714 */ 718 */
715static struct ConfigSection * 719static struct ConfigSection *
716find_section(const struct GNUNET_CONFIGURATION_Handle *cfg, 720find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
717 const char *section) 721 const char *section)
718{ 722{
719 struct ConfigSection *pos; 723 struct ConfigSection *pos;
720 724
721 pos = cfg->sections; 725 pos = cfg->sections;
722 while ((pos != NULL) && (0 != strcasecmp(section, pos->name))) 726 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
723 pos = pos->next; 727 pos = pos->next;
724 return pos; 728 return pos;
725} 729}
@@ -734,17 +738,17 @@ find_section(const struct GNUNET_CONFIGURATION_Handle *cfg,
734 * @return matching entry, NULL if not found 738 * @return matching entry, NULL if not found
735 */ 739 */
736static struct ConfigEntry * 740static struct ConfigEntry *
737find_entry(const struct GNUNET_CONFIGURATION_Handle *cfg, 741find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
738 const char *section, 742 const char *section,
739 const char *key) 743 const char *key)
740{ 744{
741 struct ConfigSection *sec; 745 struct ConfigSection *sec;
742 struct ConfigEntry *pos; 746 struct ConfigEntry *pos;
743 747
744 if (NULL == (sec = find_section(cfg, section))) 748 if (NULL == (sec = find_section (cfg, section)))
745 return NULL; 749 return NULL;
746 pos = sec->entries; 750 pos = sec->entries;
747 while ((pos != NULL) && (0 != strcasecmp(key, pos->key))) 751 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
748 pos = pos->next; 752 pos = pos->next;
749 return pos; 753 return pos;
750} 754}
@@ -761,19 +765,19 @@ find_entry(const struct GNUNET_CONFIGURATION_Handle *cfg,
761 * @param value value to copy (of the default conf.) 765 * @param value value to copy (of the default conf.)
762 */ 766 */
763static void 767static void
764compare_entries(void *cls, 768compare_entries (void *cls,
765 const char *section, 769 const char *section,
766 const char *option, 770 const char *option,
767 const char *value) 771 const char *value)
768{ 772{
769 struct DiffHandle *dh = cls; 773 struct DiffHandle *dh = cls;
770 struct ConfigEntry *entNew; 774 struct ConfigEntry *entNew;
771 775
772 entNew = find_entry(dh->cfg_default, section, option); 776 entNew = find_entry (dh->cfg_default, section, option);
773 if ((NULL != entNew) && (NULL != entNew->val) && 777 if ((NULL != entNew) && (NULL != entNew->val) &&
774 (0 == strcmp(entNew->val, value))) 778 (0 == strcmp (entNew->val, value)))
775 return; 779 return;
776 GNUNET_CONFIGURATION_set_value_string(dh->cfgDiff, section, option, value); 780 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
777} 781}
778 782
779 783
@@ -785,15 +789,15 @@ compare_entries(void *cls,
785 * @return configuration with only the differences, never NULL 789 * @return configuration with only the differences, never NULL
786 */ 790 */
787struct GNUNET_CONFIGURATION_Handle * 791struct GNUNET_CONFIGURATION_Handle *
788GNUNET_CONFIGURATION_get_diff( 792GNUNET_CONFIGURATION_get_diff (
789 const struct GNUNET_CONFIGURATION_Handle *cfg_default, 793 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
790 const struct GNUNET_CONFIGURATION_Handle *cfg_new) 794 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
791{ 795{
792 struct DiffHandle diffHandle; 796 struct DiffHandle diffHandle;
793 797
794 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create(); 798 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
795 diffHandle.cfg_default = cfg_default; 799 diffHandle.cfg_default = cfg_default;
796 GNUNET_CONFIGURATION_iterate(cfg_new, &compare_entries, &diffHandle); 800 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
797 return diffHandle.cfgDiff; 801 return diffHandle.cfgDiff;
798} 802}
799 803
@@ -807,7 +811,7 @@ GNUNET_CONFIGURATION_get_diff(
807 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 811 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
808 */ 812 */
809int 813int
810GNUNET_CONFIGURATION_write_diffs( 814GNUNET_CONFIGURATION_write_diffs (
811 const struct GNUNET_CONFIGURATION_Handle *cfg_default, 815 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
812 const struct GNUNET_CONFIGURATION_Handle *cfg_new, 816 const struct GNUNET_CONFIGURATION_Handle *cfg_new,
813 const char *filename) 817 const char *filename)
@@ -815,9 +819,9 @@ GNUNET_CONFIGURATION_write_diffs(
815 int ret; 819 int ret;
816 struct GNUNET_CONFIGURATION_Handle *diff; 820 struct GNUNET_CONFIGURATION_Handle *diff;
817 821
818 diff = GNUNET_CONFIGURATION_get_diff(cfg_default, cfg_new); 822 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
819 ret = GNUNET_CONFIGURATION_write(diff, filename); 823 ret = GNUNET_CONFIGURATION_write (diff, filename);
820 GNUNET_CONFIGURATION_destroy(diff); 824 GNUNET_CONFIGURATION_destroy (diff);
821 return ret; 825 return ret;
822} 826}
823 827
@@ -831,42 +835,42 @@ GNUNET_CONFIGURATION_write_diffs(
831 * @param value value to set 835 * @param value value to set
832 */ 836 */
833void 837void
834GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, 838GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
835 const char *section, 839 const char *section,
836 const char *option, 840 const char *option,
837 const char *value) 841 const char *value)
838{ 842{
839 struct ConfigSection *sec; 843 struct ConfigSection *sec;
840 struct ConfigEntry *e; 844 struct ConfigEntry *e;
841 char *nv; 845 char *nv;
842 846
843 e = find_entry(cfg, section, option); 847 e = find_entry (cfg, section, option);
844 if (NULL != e) 848 if (NULL != e)
849 {
850 if (NULL == value)
845 { 851 {
846 if (NULL == value) 852 GNUNET_free_non_null (e->val);
847 { 853 e->val = NULL;
848 GNUNET_free_non_null(e->val);
849 e->val = NULL;
850 }
851 else
852 {
853 nv = GNUNET_strdup(value);
854 GNUNET_free_non_null(e->val);
855 e->val = nv;
856 }
857 return;
858 } 854 }
859 sec = find_section(cfg, section); 855 else
860 if (sec == NULL)
861 { 856 {
862 sec = GNUNET_new(struct ConfigSection); 857 nv = GNUNET_strdup (value);
863 sec->name = GNUNET_strdup(section); 858 GNUNET_free_non_null (e->val);
864 sec->next = cfg->sections; 859 e->val = nv;
865 cfg->sections = sec;
866 } 860 }
867 e = GNUNET_new(struct ConfigEntry); 861 return;
868 e->key = GNUNET_strdup(option); 862 }
869 e->val = GNUNET_strdup(value); 863 sec = find_section (cfg, section);
864 if (sec == NULL)
865 {
866 sec = GNUNET_new (struct ConfigSection);
867 sec->name = GNUNET_strdup (section);
868 sec->next = cfg->sections;
869 cfg->sections = sec;
870 }
871 e = GNUNET_new (struct ConfigEntry);
872 e->key = GNUNET_strdup (option);
873 e->val = GNUNET_strdup (value);
870 e->next = sec->entries; 874 e->next = sec->entries;
871 sec->entries = e; 875 sec->entries = e;
872} 876}
@@ -881,15 +885,15 @@ GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg,
881 * @param number value to set 885 * @param number value to set
882 */ 886 */
883void 887void
884GNUNET_CONFIGURATION_set_value_number(struct GNUNET_CONFIGURATION_Handle *cfg, 888GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
885 const char *section, 889 const char *section,
886 const char *option, 890 const char *option,
887 unsigned long long number) 891 unsigned long long number)
888{ 892{
889 char s[64]; 893 char s[64];
890 894
891 GNUNET_snprintf(s, 64, "%llu", number); 895 GNUNET_snprintf (s, 64, "%llu", number);
892 GNUNET_CONFIGURATION_set_value_string(cfg, section, option, s); 896 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
893} 897}
894 898
895 899
@@ -903,7 +907,7 @@ GNUNET_CONFIGURATION_set_value_number(struct GNUNET_CONFIGURATION_Handle *cfg,
903 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 907 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
904 */ 908 */
905int 909int
906GNUNET_CONFIGURATION_get_value_number( 910GNUNET_CONFIGURATION_get_value_number (
907 const struct GNUNET_CONFIGURATION_Handle *cfg, 911 const struct GNUNET_CONFIGURATION_Handle *cfg,
908 const char *section, 912 const char *section,
909 const char *option, 913 const char *option,
@@ -912,11 +916,11 @@ GNUNET_CONFIGURATION_get_value_number(
912 struct ConfigEntry *e; 916 struct ConfigEntry *e;
913 char dummy[2]; 917 char dummy[2];
914 918
915 if (NULL == (e = find_entry(cfg, section, option))) 919 if (NULL == (e = find_entry (cfg, section, option)))
916 return GNUNET_SYSERR; 920 return GNUNET_SYSERR;
917 if (NULL == e->val) 921 if (NULL == e->val)
918 return GNUNET_SYSERR; 922 return GNUNET_SYSERR;
919 if (1 != sscanf(e->val, "%llu%1s", number, dummy)) 923 if (1 != sscanf (e->val, "%llu%1s", number, dummy))
920 return GNUNET_SYSERR; 924 return GNUNET_SYSERR;
921 return GNUNET_OK; 925 return GNUNET_OK;
922} 926}
@@ -932,7 +936,7 @@ GNUNET_CONFIGURATION_get_value_number(
932 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 936 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
933 */ 937 */
934int 938int
935GNUNET_CONFIGURATION_get_value_float( 939GNUNET_CONFIGURATION_get_value_float (
936 const struct GNUNET_CONFIGURATION_Handle *cfg, 940 const struct GNUNET_CONFIGURATION_Handle *cfg,
937 const char *section, 941 const char *section,
938 const char *option, 942 const char *option,
@@ -941,11 +945,11 @@ GNUNET_CONFIGURATION_get_value_float(
941 struct ConfigEntry *e; 945 struct ConfigEntry *e;
942 char dummy[2]; 946 char dummy[2];
943 947
944 if (NULL == (e = find_entry(cfg, section, option))) 948 if (NULL == (e = find_entry (cfg, section, option)))
945 return GNUNET_SYSERR; 949 return GNUNET_SYSERR;
946 if (NULL == e->val) 950 if (NULL == e->val)
947 return GNUNET_SYSERR; 951 return GNUNET_SYSERR;
948 if (1 != sscanf(e->val, "%f%1s", number, dummy)) 952 if (1 != sscanf (e->val, "%f%1s", number, dummy))
949 return GNUNET_SYSERR; 953 return GNUNET_SYSERR;
950 return GNUNET_OK; 954 return GNUNET_OK;
951} 955}
@@ -961,7 +965,7 @@ GNUNET_CONFIGURATION_get_value_float(
961 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 965 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
962 */ 966 */
963int 967int
964GNUNET_CONFIGURATION_get_value_time( 968GNUNET_CONFIGURATION_get_value_time (
965 const struct GNUNET_CONFIGURATION_Handle *cfg, 969 const struct GNUNET_CONFIGURATION_Handle *cfg,
966 const char *section, 970 const char *section,
967 const char *option, 971 const char *option,
@@ -970,16 +974,16 @@ GNUNET_CONFIGURATION_get_value_time(
970 struct ConfigEntry *e; 974 struct ConfigEntry *e;
971 int ret; 975 int ret;
972 976
973 if (NULL == (e = find_entry(cfg, section, option))) 977 if (NULL == (e = find_entry (cfg, section, option)))
974 return GNUNET_SYSERR; 978 return GNUNET_SYSERR;
975 if (NULL == e->val) 979 if (NULL == e->val)
976 return GNUNET_SYSERR; 980 return GNUNET_SYSERR;
977 ret = GNUNET_STRINGS_fancy_time_to_relative(e->val, time); 981 ret = GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
978 if (GNUNET_OK != ret) 982 if (GNUNET_OK != ret)
979 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR, 983 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
980 section, 984 section,
981 option, 985 option,
982 _("Not a valid relative time specification")); 986 _ ("Not a valid relative time specification"));
983 return ret; 987 return ret;
984} 988}
985 989
@@ -994,7 +998,7 @@ GNUNET_CONFIGURATION_get_value_time(
994 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 998 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
995 */ 999 */
996int 1000int
997GNUNET_CONFIGURATION_get_value_size( 1001GNUNET_CONFIGURATION_get_value_size (
998 const struct GNUNET_CONFIGURATION_Handle *cfg, 1002 const struct GNUNET_CONFIGURATION_Handle *cfg,
999 const char *section, 1003 const char *section,
1000 const char *option, 1004 const char *option,
@@ -1002,11 +1006,11 @@ GNUNET_CONFIGURATION_get_value_size(
1002{ 1006{
1003 struct ConfigEntry *e; 1007 struct ConfigEntry *e;
1004 1008
1005 if (NULL == (e = find_entry(cfg, section, option))) 1009 if (NULL == (e = find_entry (cfg, section, option)))
1006 return GNUNET_SYSERR; 1010 return GNUNET_SYSERR;
1007 if (NULL == e->val) 1011 if (NULL == e->val)
1008 return GNUNET_SYSERR; 1012 return GNUNET_SYSERR;
1009 return GNUNET_STRINGS_fancy_size_to_bytes(e->val, size); 1013 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
1010} 1014}
1011 1015
1012 1016
@@ -1021,7 +1025,7 @@ GNUNET_CONFIGURATION_get_value_size(
1021 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1025 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1022 */ 1026 */
1023int 1027int
1024GNUNET_CONFIGURATION_get_value_string( 1028GNUNET_CONFIGURATION_get_value_string (
1025 const struct GNUNET_CONFIGURATION_Handle *cfg, 1029 const struct GNUNET_CONFIGURATION_Handle *cfg,
1026 const char *section, 1030 const char *section,
1027 const char *option, 1031 const char *option,
@@ -1029,12 +1033,12 @@ GNUNET_CONFIGURATION_get_value_string(
1029{ 1033{
1030 struct ConfigEntry *e; 1034 struct ConfigEntry *e;
1031 1035
1032 if ((NULL == (e = find_entry(cfg, section, option))) || (NULL == e->val)) 1036 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1033 { 1037 {
1034 *value = NULL; 1038 *value = NULL;
1035 return GNUNET_SYSERR; 1039 return GNUNET_SYSERR;
1036 } 1040 }
1037 *value = GNUNET_strdup(e->val); 1041 *value = GNUNET_strdup (e->val);
1038 return GNUNET_OK; 1042 return GNUNET_OK;
1039} 1043}
1040 1044
@@ -1052,7 +1056,7 @@ GNUNET_CONFIGURATION_get_value_string(
1052 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1056 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1053 */ 1057 */
1054int 1058int
1055GNUNET_CONFIGURATION_get_value_choice( 1059GNUNET_CONFIGURATION_get_value_choice (
1056 const struct GNUNET_CONFIGURATION_Handle *cfg, 1060 const struct GNUNET_CONFIGURATION_Handle *cfg,
1057 const char *section, 1061 const char *section,
1058 const char *option, 1062 const char *option,
@@ -1062,21 +1066,21 @@ GNUNET_CONFIGURATION_get_value_choice(
1062 struct ConfigEntry *e; 1066 struct ConfigEntry *e;
1063 unsigned int i; 1067 unsigned int i;
1064 1068
1065 if (NULL == (e = find_entry(cfg, section, option))) 1069 if (NULL == (e = find_entry (cfg, section, option)))
1066 return GNUNET_SYSERR; 1070 return GNUNET_SYSERR;
1067 for (i = 0; NULL != choices[i]; i++) 1071 for (i = 0; NULL != choices[i]; i++)
1068 if (0 == strcasecmp(choices[i], e->val)) 1072 if (0 == strcasecmp (choices[i], e->val))
1069 break; 1073 break;
1070 if (NULL == choices[i]) 1074 if (NULL == choices[i])
1071 { 1075 {
1072 LOG(GNUNET_ERROR_TYPE_ERROR, 1076 LOG (GNUNET_ERROR_TYPE_ERROR,
1073 _("Configuration value '%s' for '%s'" 1077 _ ("Configuration value '%s' for '%s'"
1074 " in section '%s' is not in set of legal choices\n"), 1078 " in section '%s' is not in set of legal choices\n"),
1075 e->val, 1079 e->val,
1076 option, 1080 option,
1077 section); 1081 section);
1078 return GNUNET_SYSERR; 1082 return GNUNET_SYSERR;
1079 } 1083 }
1080 *value = choices[i]; 1084 *value = choices[i];
1081 return GNUNET_OK; 1085 return GNUNET_OK;
1082} 1086}
@@ -1095,11 +1099,11 @@ GNUNET_CONFIGURATION_get_value_choice(
1095 * #GNUNET_SYSERR on decoding error 1099 * #GNUNET_SYSERR on decoding error
1096 */ 1100 */
1097int 1101int
1098GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg, 1102GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1099 const char *section, 1103 const char *section,
1100 const char *option, 1104 const char *option,
1101 void *buf, 1105 void *buf,
1102 size_t buf_size) 1106 size_t buf_size)
1103{ 1107{
1104 char *enc; 1108 char *enc;
1105 int res; 1109 int res;
@@ -1107,21 +1111,21 @@ GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg,
1107 1111
1108 if (GNUNET_OK != 1112 if (GNUNET_OK !=
1109 (res = 1113 (res =
1110 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &enc))) 1114 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &enc)))
1111 return res; 1115 return res;
1112 data_size = (strlen(enc) * 5) / 8; 1116 data_size = (strlen (enc) * 5) / 8;
1113 if (data_size != buf_size) 1117 if (data_size != buf_size)
1114 { 1118 {
1115 GNUNET_free(enc); 1119 GNUNET_free (enc);
1116 return GNUNET_SYSERR; 1120 return GNUNET_SYSERR;
1117 } 1121 }
1118 if (GNUNET_OK != 1122 if (GNUNET_OK !=
1119 GNUNET_STRINGS_string_to_data(enc, strlen(enc), buf, buf_size)) 1123 GNUNET_STRINGS_string_to_data (enc, strlen (enc), buf, buf_size))
1120 { 1124 {
1121 GNUNET_free(enc); 1125 GNUNET_free (enc);
1122 return GNUNET_SYSERR; 1126 return GNUNET_SYSERR;
1123 } 1127 }
1124 GNUNET_free(enc); 1128 GNUNET_free (enc);
1125 return GNUNET_OK; 1129 return GNUNET_OK;
1126} 1130}
1127 1131
@@ -1135,13 +1139,13 @@ GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg,
1135 * @return #GNUNET_YES if so, #GNUNET_NO if not. 1139 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1136 */ 1140 */
1137int 1141int
1138GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, 1142GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1139 const char *section, 1143 const char *section,
1140 const char *option) 1144 const char *option)
1141{ 1145{
1142 struct ConfigEntry *e; 1146 struct ConfigEntry *e;
1143 1147
1144 if ((NULL == (e = find_entry(cfg, section, option))) || (NULL == e->val)) 1148 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1145 return GNUNET_NO; 1149 return GNUNET_NO;
1146 return GNUNET_YES; 1150 return GNUNET_YES;
1147} 1151}
@@ -1163,9 +1167,9 @@ GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg,
1163 * @return $-expanded string 1167 * @return $-expanded string
1164 */ 1168 */
1165static char * 1169static char *
1166expand_dollar(const struct GNUNET_CONFIGURATION_Handle *cfg, 1170expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1167 char *orig, 1171 char *orig,
1168 unsigned int depth) 1172 unsigned int depth)
1169{ 1173{
1170 char *prefix; 1174 char *prefix;
1171 char *result; 1175 char *result;
@@ -1182,129 +1186,129 @@ expand_dollar(const struct GNUNET_CONFIGURATION_Handle *cfg,
1182 if (NULL == orig) 1186 if (NULL == orig)
1183 return NULL; 1187 return NULL;
1184 if (depth > 128) 1188 if (depth > 128)
1185 { 1189 {
1186 LOG(GNUNET_ERROR_TYPE_WARNING, 1190 LOG (GNUNET_ERROR_TYPE_WARNING,
1187 _( 1191 _ (
1188 "Recursive expansion suspected, aborting $-expansion for term `%s'\n"), 1192 "Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1189 orig); 1193 orig);
1190 return orig; 1194 return orig;
1191 } 1195 }
1192 LOG(GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig); 1196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1193 if ('$' != orig[0]) 1197 if ('$' != orig[0])
1194 { 1198 {
1195 LOG(GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n"); 1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1196 return orig; 1200 return orig;
1197 } 1201 }
1198 erased_char = 0; 1202 erased_char = 0;
1199 erased_pos = NULL; 1203 erased_pos = NULL;
1200 if ('{' == orig[1]) 1204 if ('{' == orig[1])
1205 {
1206 start = &orig[2];
1207 lopen = 1;
1208 end = &orig[1];
1209 while (lopen > 0)
1201 { 1210 {
1202 start = &orig[2]; 1211 end++;
1203 lopen = 1; 1212 switch (*end)
1204 end = &orig[1]; 1213 {
1205 while (lopen > 0) 1214 case '}':
1206 { 1215 lopen--;
1207 end++; 1216 break;
1208 switch (*end) 1217
1209 { 1218 case '{':
1210 case '}': 1219 lopen++;
1211 lopen--; 1220 break;
1212 break; 1221
1213 1222 case '\0':
1214 case '{': 1223 LOG (GNUNET_ERROR_TYPE_WARNING,
1215 lopen++; 1224 _ ("Missing closing `%s' in option `%s'\n"),
1216 break; 1225 "}",
1217 1226 orig);
1218 case '\0': 1227 return orig;
1219 LOG(GNUNET_ERROR_TYPE_WARNING, 1228
1220 _("Missing closing `%s' in option `%s'\n"), 1229 default:
1221 "}", 1230 break;
1222 orig); 1231 }
1223 return orig;
1224
1225 default:
1226 break;
1227 }
1228 }
1229 erased_char = *end;
1230 erased_pos = end;
1231 *end = '\0';
1232 post = end + 1;
1233 def = strchr(orig, ':');
1234 if (NULL != def)
1235 {
1236 *def = '\0';
1237 def++;
1238 if (('-' == *def) || ('=' == *def))
1239 def++;
1240 def = GNUNET_strdup(def);
1241 }
1242 } 1232 }
1233 erased_char = *end;
1234 erased_pos = end;
1235 *end = '\0';
1236 post = end + 1;
1237 def = strchr (orig, ':');
1238 if (NULL != def)
1239 {
1240 *def = '\0';
1241 def++;
1242 if (('-' == *def) || ('=' == *def))
1243 def++;
1244 def = GNUNET_strdup (def);
1245 }
1246 }
1243 else 1247 else
1248 {
1249 int i;
1250
1251 start = &orig[1];
1252 def = NULL;
1253 i = 0;
1254 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0') &&
1255 (orig[i] != ' '))
1256 i++;
1257 if (orig[i] == '\0')
1244 { 1258 {
1245 int i; 1259 post = "";
1246 1260 }
1247 start = &orig[1]; 1261 else
1248 def = NULL; 1262 {
1249 i = 0; 1263 erased_char = orig[i];
1250 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0') && 1264 erased_pos = &orig[i];
1251 (orig[i] != ' ')) 1265 orig[i] = '\0';
1252 i++; 1266 post = &orig[i + 1];
1253 if (orig[i] == '\0')
1254 {
1255 post = "";
1256 }
1257 else
1258 {
1259 erased_char = orig[i];
1260 erased_pos = &orig[i];
1261 orig[i] = '\0';
1262 post = &orig[i + 1];
1263 }
1264 } 1267 }
1265 LOG(GNUNET_ERROR_TYPE_DEBUG, 1268 }
1266 "Split into `%s' and `%s' with default %s\n", 1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1267 start, 1270 "Split into `%s' and `%s' with default %s\n",
1268 post, 1271 start,
1269 def); 1272 post,
1273 def);
1270 if (GNUNET_OK != 1274 if (GNUNET_OK !=
1271 GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", start, &prefix)) 1275 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", start, &prefix))
1276 {
1277 if (NULL == (env = getenv (start)))
1272 { 1278 {
1273 if (NULL == (env = getenv(start))) 1279 /* try default */
1274 { 1280 def = expand_dollar (cfg, def, depth + 1);
1275 /* try default */ 1281 env = def;
1276 def = expand_dollar(cfg, def, depth + 1);
1277 env = def;
1278 }
1279 if (NULL == env)
1280 {
1281 start = GNUNET_strdup(start);
1282 if (erased_pos)
1283 *erased_pos = erased_char;
1284 LOG(GNUNET_ERROR_TYPE_WARNING,
1285 _(
1286 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1287 start,
1288 orig);
1289 GNUNET_free(start);
1290 return orig;
1291 }
1292 prefix = GNUNET_strdup(env);
1293 } 1282 }
1294 prefix = GNUNET_CONFIGURATION_expand_dollar(cfg, prefix); 1283 if (NULL == env)
1295 if ((erased_pos) && ('}' != erased_char))
1296 { 1284 {
1297 len = strlen(prefix) + 1; 1285 start = GNUNET_strdup (start);
1298 prefix = GNUNET_realloc(prefix, len + 1); 1286 if (erased_pos)
1299 prefix[len - 1] = erased_char; 1287 *erased_pos = erased_char;
1300 prefix[len] = '\0'; 1288 LOG (GNUNET_ERROR_TYPE_WARNING,
1289 _ (
1290 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1291 start,
1292 orig);
1293 GNUNET_free (start);
1294 return orig;
1301 } 1295 }
1302 result = GNUNET_malloc(strlen(prefix) + strlen(post) + 1); 1296 prefix = GNUNET_strdup (env);
1303 strcpy(result, prefix); 1297 }
1304 strcat(result, post); 1298 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1305 GNUNET_free_non_null(def); 1299 if ((erased_pos) && ('}' != erased_char))
1306 GNUNET_free(prefix); 1300 {
1307 GNUNET_free(orig); 1301 len = strlen (prefix) + 1;
1302 prefix = GNUNET_realloc (prefix, len + 1);
1303 prefix[len - 1] = erased_char;
1304 prefix[len] = '\0';
1305 }
1306 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1307 strcpy (result, prefix);
1308 strcat (result, post);
1309 GNUNET_free_non_null (def);
1310 GNUNET_free (prefix);
1311 GNUNET_free (orig);
1308 return result; 1312 return result;
1309} 1313}
1310 1314
@@ -1326,7 +1330,7 @@ expand_dollar(const struct GNUNET_CONFIGURATION_Handle *cfg,
1326 * @return $-expanded string 1330 * @return $-expanded string
1327 */ 1331 */
1328char * 1332char *
1329GNUNET_CONFIGURATION_expand_dollar( 1333GNUNET_CONFIGURATION_expand_dollar (
1330 const struct GNUNET_CONFIGURATION_Handle *cfg, 1334 const struct GNUNET_CONFIGURATION_Handle *cfg,
1331 char *orig) 1335 char *orig)
1332{ 1336{
@@ -1335,16 +1339,16 @@ GNUNET_CONFIGURATION_expand_dollar(
1335 size_t len; 1339 size_t len;
1336 1340
1337 for (i = 0; '\0' != orig[i]; i++) 1341 for (i = 0; '\0' != orig[i]; i++)
1338 { 1342 {
1339 if ('$' != orig[i]) 1343 if ('$' != orig[i])
1340 continue; 1344 continue;
1341 dup = GNUNET_strdup(orig + i); 1345 dup = GNUNET_strdup (orig + i);
1342 dup = expand_dollar(cfg, dup, 0); 1346 dup = expand_dollar (cfg, dup, 0);
1343 len = strlen(dup) + 1; 1347 len = strlen (dup) + 1;
1344 orig = GNUNET_realloc(orig, i + len); 1348 orig = GNUNET_realloc (orig, i + len);
1345 GNUNET_memcpy(orig + i, dup, len); 1349 GNUNET_memcpy (orig + i, dup, len);
1346 GNUNET_free(dup); 1350 GNUNET_free (dup);
1347 } 1351 }
1348 return orig; 1352 return orig;
1349} 1353}
1350 1354
@@ -1360,7 +1364,7 @@ GNUNET_CONFIGURATION_expand_dollar(
1360 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1364 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1361 */ 1365 */
1362int 1366int
1363GNUNET_CONFIGURATION_get_value_filename( 1367GNUNET_CONFIGURATION_get_value_filename (
1364 const struct GNUNET_CONFIGURATION_Handle *cfg, 1368 const struct GNUNET_CONFIGURATION_Handle *cfg,
1365 const char *section, 1369 const char *section,
1366 const char *option, 1370 const char *option,
@@ -1369,15 +1373,15 @@ GNUNET_CONFIGURATION_get_value_filename(
1369 char *tmp; 1373 char *tmp;
1370 1374
1371 if (GNUNET_OK != 1375 if (GNUNET_OK !=
1372 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &tmp)) 1376 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1373 { 1377 {
1374 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n"); 1378 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1375 *value = NULL; 1379 *value = NULL;
1376 return GNUNET_SYSERR; 1380 return GNUNET_SYSERR;
1377 } 1381 }
1378 tmp = GNUNET_CONFIGURATION_expand_dollar(cfg, tmp); 1382 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1379 *value = GNUNET_STRINGS_filename_expand(tmp); 1383 *value = GNUNET_STRINGS_filename_expand (tmp);
1380 GNUNET_free(tmp); 1384 GNUNET_free (tmp);
1381 if (*value == NULL) 1385 if (*value == NULL)
1382 return GNUNET_SYSERR; 1386 return GNUNET_SYSERR;
1383 return GNUNET_OK; 1387 return GNUNET_OK;
@@ -1394,7 +1398,7 @@ GNUNET_CONFIGURATION_get_value_filename(
1394 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR 1398 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1395 */ 1399 */
1396int 1400int
1397GNUNET_CONFIGURATION_get_value_yesno( 1401GNUNET_CONFIGURATION_get_value_yesno (
1398 const struct GNUNET_CONFIGURATION_Handle *cfg, 1402 const struct GNUNET_CONFIGURATION_Handle *cfg,
1399 const char *section, 1403 const char *section,
1400 const char *option) 1404 const char *option)
@@ -1404,7 +1408,7 @@ GNUNET_CONFIGURATION_get_value_yesno(
1404 int ret; 1408 int ret;
1405 1409
1406 ret = 1410 ret =
1407 GNUNET_CONFIGURATION_get_value_choice(cfg, section, option, yesno, &val); 1411 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1408 if (ret == GNUNET_SYSERR) 1412 if (ret == GNUNET_SYSERR)
1409 return ret; 1413 return ret;
1410 if (val == yesno[0]) 1414 if (val == yesno[0])
@@ -1424,7 +1428,7 @@ GNUNET_CONFIGURATION_get_value_yesno(
1424 * @return number of filenames iterated over, -1 on error 1428 * @return number of filenames iterated over, -1 on error
1425 */ 1429 */
1426int 1430int
1427GNUNET_CONFIGURATION_iterate_value_filenames( 1431GNUNET_CONFIGURATION_iterate_value_filenames (
1428 const struct GNUNET_CONFIGURATION_Handle *cfg, 1432 const struct GNUNET_CONFIGURATION_Handle *cfg,
1429 const char *section, 1433 const char *section,
1430 const char *option, 1434 const char *option,
@@ -1438,55 +1442,55 @@ GNUNET_CONFIGURATION_iterate_value_filenames(
1438 int ret; 1442 int ret;
1439 1443
1440 if (GNUNET_OK != 1444 if (GNUNET_OK !=
1441 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &list)) 1445 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1442 return 0; 1446 return 0;
1443 GNUNET_assert(list != NULL); 1447 GNUNET_assert (list != NULL);
1444 ret = 0; 1448 ret = 0;
1445 pos = list; 1449 pos = list;
1446 while (1) 1450 while (1)
1451 {
1452 while (pos[0] == ' ')
1453 pos++;
1454 if (strlen (pos) == 0)
1455 break;
1456 end = pos + 1;
1457 while ((end[0] != ' ') && (end[0] != '\0'))
1447 { 1458 {
1448 while (pos[0] == ' ') 1459 if (end[0] == '\\')
1449 pos++; 1460 {
1450 if (strlen(pos) == 0) 1461 switch (end[1])
1451 break;
1452 end = pos + 1;
1453 while ((end[0] != ' ') && (end[0] != '\0'))
1454 { 1462 {
1455 if (end[0] == '\\') 1463 case '\\':
1456 { 1464 case ' ':
1457 switch (end[1]) 1465 memmove (end, &end[1], strlen (&end[1]) + 1);
1458 { 1466
1459 case '\\': 1467 case '\0':
1460 case ' ': 1468 /* illegal, but just keep it */
1461 memmove(end, &end[1], strlen(&end[1]) + 1); 1469 break;
1462 1470
1463 case '\0': 1471 default:
1464 /* illegal, but just keep it */ 1472 /* illegal, but just ignore that there was a '/' */
1465 break; 1473 break;
1466
1467 default:
1468 /* illegal, but just ignore that there was a '/' */
1469 break;
1470 }
1471 }
1472 end++;
1473 }
1474 old = end[0];
1475 end[0] = '\0';
1476 if (strlen(pos) > 0)
1477 {
1478 ret++;
1479 if ((cb != NULL) && (GNUNET_OK != cb(cb_cls, pos)))
1480 {
1481 ret = GNUNET_SYSERR;
1482 break;
1483 }
1484 } 1474 }
1485 if (old == '\0') 1475 }
1476 end++;
1477 }
1478 old = end[0];
1479 end[0] = '\0';
1480 if (strlen (pos) > 0)
1481 {
1482 ret++;
1483 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1484 {
1485 ret = GNUNET_SYSERR;
1486 break; 1486 break;
1487 pos = end + 1; 1487 }
1488 } 1488 }
1489 GNUNET_free(list); 1489 if (old == '\0')
1490 break;
1491 pos = end + 1;
1492 }
1493 GNUNET_free (list);
1490 return ret; 1494 return ret;
1491} 1495}
1492 1496
@@ -1498,33 +1502,33 @@ GNUNET_CONFIGURATION_iterate_value_filenames(
1498 * @return FIXME 1502 * @return FIXME
1499 */ 1503 */
1500static char * 1504static char *
1501escape_name(const char *value) 1505escape_name (const char *value)
1502{ 1506{
1503 char *escaped; 1507 char *escaped;
1504 const char *rpos; 1508 const char *rpos;
1505 char *wpos; 1509 char *wpos;
1506 1510
1507 escaped = GNUNET_malloc(strlen(value) * 2 + 1); 1511 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1508 memset(escaped, 0, strlen(value) * 2 + 1); 1512 memset (escaped, 0, strlen (value) * 2 + 1);
1509 rpos = value; 1513 rpos = value;
1510 wpos = escaped; 1514 wpos = escaped;
1511 while (rpos[0] != '\0') 1515 while (rpos[0] != '\0')
1516 {
1517 switch (rpos[0])
1512 { 1518 {
1513 switch (rpos[0]) 1519 case '\\':
1514 { 1520 case ' ':
1515 case '\\': 1521 wpos[0] = '\\';
1516 case ' ': 1522 wpos[1] = rpos[0];
1517 wpos[0] = '\\'; 1523 wpos += 2;
1518 wpos[1] = rpos[0]; 1524 break;
1519 wpos += 2;
1520 break;
1521 1525
1522 default: 1526 default:
1523 wpos[0] = rpos[0]; 1527 wpos[0] = rpos[0];
1524 wpos++; 1528 wpos++;
1525 }
1526 rpos++;
1527 } 1529 }
1530 rpos++;
1531 }
1528 return escaped; 1532 return escaped;
1529} 1533}
1530 1534
@@ -1537,11 +1541,11 @@ escape_name(const char *value)
1537 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do 1541 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1538 */ 1542 */
1539static int 1543static int
1540test_match(void *cls, const char *fn) 1544test_match (void *cls, const char *fn)
1541{ 1545{
1542 const char *of = cls; 1546 const char *of = cls;
1543 1547
1544 return (0 == strcmp(of, fn)) ? GNUNET_SYSERR : GNUNET_OK; 1548 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1545} 1549}
1546 1550
1547 1551
@@ -1558,7 +1562,7 @@ test_match(void *cls, const char *fn)
1558 * #GNUNET_SYSERR on error 1562 * #GNUNET_SYSERR on error
1559 */ 1563 */
1560int 1564int
1561GNUNET_CONFIGURATION_append_value_filename( 1565GNUNET_CONFIGURATION_append_value_filename (
1562 struct GNUNET_CONFIGURATION_Handle *cfg, 1566 struct GNUNET_CONFIGURATION_Handle *cfg,
1563 const char *section, 1567 const char *section,
1564 const char *option, 1568 const char *option,
@@ -1569,25 +1573,25 @@ GNUNET_CONFIGURATION_append_value_filename(
1569 char *nw; 1573 char *nw;
1570 1574
1571 if (GNUNET_SYSERR == 1575 if (GNUNET_SYSERR ==
1572 GNUNET_CONFIGURATION_iterate_value_filenames(cfg, 1576 GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1573 section, 1577 section,
1574 option, 1578 option,
1575 &test_match, 1579 &test_match,
1576 (void *)value)) 1580 (void *) value))
1577 return GNUNET_NO; /* already exists */ 1581 return GNUNET_NO; /* already exists */
1578 if (GNUNET_OK != 1582 if (GNUNET_OK !=
1579 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &old)) 1583 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1580 old = GNUNET_strdup(""); 1584 old = GNUNET_strdup ("");
1581 escaped = escape_name(value); 1585 escaped = escape_name (value);
1582 nw = GNUNET_malloc(strlen(old) + strlen(escaped) + 2); 1586 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1583 strcpy(nw, old); 1587 strcpy (nw, old);
1584 if (strlen(old) > 0) 1588 if (strlen (old) > 0)
1585 strcat(nw, " "); 1589 strcat (nw, " ");
1586 strcat(nw, escaped); 1590 strcat (nw, escaped);
1587 GNUNET_CONFIGURATION_set_value_string(cfg, section, option, nw); 1591 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1588 GNUNET_free(old); 1592 GNUNET_free (old);
1589 GNUNET_free(nw); 1593 GNUNET_free (nw);
1590 GNUNET_free(escaped); 1594 GNUNET_free (escaped);
1591 return GNUNET_OK; 1595 return GNUNET_OK;
1592} 1596}
1593 1597
@@ -1605,7 +1609,7 @@ GNUNET_CONFIGURATION_append_value_filename(
1605 * #GNUNET_SYSERR on error 1609 * #GNUNET_SYSERR on error
1606 */ 1610 */
1607int 1611int
1608GNUNET_CONFIGURATION_remove_value_filename( 1612GNUNET_CONFIGURATION_remove_value_filename (
1609 struct GNUNET_CONFIGURATION_Handle *cfg, 1613 struct GNUNET_CONFIGURATION_Handle *cfg,
1610 const char *section, 1614 const char *section,
1611 const char *option, 1615 const char *option,
@@ -1618,64 +1622,64 @@ GNUNET_CONFIGURATION_remove_value_filename(
1618 char old; 1622 char old;
1619 1623
1620 if (GNUNET_OK != 1624 if (GNUNET_OK !=
1621 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &list)) 1625 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1622 return GNUNET_NO; 1626 return GNUNET_NO;
1623 match = escape_name(value); 1627 match = escape_name (value);
1624 pos = list; 1628 pos = list;
1625 while (1) 1629 while (1)
1630 {
1631 while (pos[0] == ' ')
1632 pos++;
1633 if (strlen (pos) == 0)
1634 break;
1635 end = pos + 1;
1636 while ((end[0] != ' ') && (end[0] != '\0'))
1626 { 1637 {
1627 while (pos[0] == ' ') 1638 if (end[0] == '\\')
1628 pos++; 1639 {
1629 if (strlen(pos) == 0) 1640 switch (end[1])
1630 break;
1631 end = pos + 1;
1632 while ((end[0] != ' ') && (end[0] != '\0'))
1633 { 1641 {
1634 if (end[0] == '\\') 1642 case '\\':
1635 { 1643 case ' ':
1636 switch (end[1])
1637 {
1638 case '\\':
1639 case ' ':
1640 end++;
1641 break;
1642
1643 case '\0':
1644 /* illegal, but just keep it */
1645 break;
1646
1647 default:
1648 /* illegal, but just ignore that there was a '/' */
1649 break;
1650 }
1651 }
1652 end++; 1644 end++;
1645 break;
1646
1647 case '\0':
1648 /* illegal, but just keep it */
1649 break;
1650
1651 default:
1652 /* illegal, but just ignore that there was a '/' */
1653 break;
1653 } 1654 }
1654 old = end[0]; 1655 }
1655 end[0] = '\0'; 1656 end++;
1656 if (0 == strcmp(pos, match)) 1657 }
1657 { 1658 old = end[0];
1658 if (old != '\0') 1659 end[0] = '\0';
1659 memmove(pos, &end[1], strlen(&end[1]) + 1); 1660 if (0 == strcmp (pos, match))
1660 else 1661 {
1661 { 1662 if (old != '\0')
1662 if (pos != list) 1663 memmove (pos, &end[1], strlen (&end[1]) + 1);
1663 pos[-1] = '\0'; 1664 else
1664 else 1665 {
1665 pos[0] = '\0'; 1666 if (pos != list)
1666 } 1667 pos[-1] = '\0';
1667 GNUNET_CONFIGURATION_set_value_string(cfg, section, option, list); 1668 else
1668 GNUNET_free(list); 1669 pos[0] = '\0';
1669 GNUNET_free(match); 1670 }
1670 return GNUNET_OK; 1671 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1671 } 1672 GNUNET_free (list);
1672 if (old == '\0') 1673 GNUNET_free (match);
1673 break; 1674 return GNUNET_OK;
1674 end[0] = old;
1675 pos = end + 1;
1676 } 1675 }
1677 GNUNET_free(list); 1676 if (old == '\0')
1678 GNUNET_free(match); 1677 break;
1678 end[0] = old;
1679 pos = end + 1;
1680 }
1681 GNUNET_free (list);
1682 GNUNET_free (match);
1679 return GNUNET_NO; 1683 return GNUNET_NO;
1680} 1684}
1681 1685
@@ -1690,21 +1694,21 @@ GNUNET_CONFIGURATION_remove_value_filename(
1690 * @return #GNUNET_OK on success 1694 * @return #GNUNET_OK on success
1691 */ 1695 */
1692static int 1696static int
1693parse_configuration_file(void *cls, const char *filename) 1697parse_configuration_file (void *cls, const char *filename)
1694{ 1698{
1695 struct GNUNET_CONFIGURATION_Handle *cfg = cls; 1699 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1696 char *ext; 1700 char *ext;
1697 int ret; 1701 int ret;
1698 1702
1699 /* Examine file extension */ 1703 /* Examine file extension */
1700 ext = strrchr(filename, '.'); 1704 ext = strrchr (filename, '.');
1701 if ((NULL == ext) || (0 != strcmp(ext, ".conf"))) 1705 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1702 { 1706 {
1703 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename); 1707 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1704 return GNUNET_OK; 1708 return GNUNET_OK;
1705 } 1709 }
1706 1710
1707 ret = GNUNET_CONFIGURATION_parse(cfg, filename); 1711 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1708 return ret; 1712 return ret;
1709} 1713}
1710 1714
@@ -1718,11 +1722,11 @@ parse_configuration_file(void *cls, const char *filename)
1718 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1722 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1719 */ 1723 */
1720int 1724int
1721GNUNET_CONFIGURATION_load_from(struct GNUNET_CONFIGURATION_Handle *cfg, 1725GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1722 const char *defaults_d) 1726 const char *defaults_d)
1723{ 1727{
1724 if (GNUNET_SYSERR == 1728 if (GNUNET_SYSERR ==
1725 GNUNET_DISK_directory_scan(defaults_d, &parse_configuration_file, cfg)) 1729 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1726 return GNUNET_SYSERR; /* no configuration at all found */ 1730 return GNUNET_SYSERR; /* no configuration at all found */
1727 return GNUNET_OK; 1731 return GNUNET_OK;
1728} 1732}
diff --git a/src/util/configuration_loader.c b/src/util/configuration_loader.c
index ccf9a25b2..b043a2e12 100644
--- a/src/util/configuration_loader.c
+++ b/src/util/configuration_loader.c
@@ -27,7 +27,7 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-configuration", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-configuration", __VA_ARGS__)
31 31
32 32
33/** 33/**
@@ -39,48 +39,48 @@
39 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 39 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
40 */ 40 */
41int 41int
42GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, 42GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
43 const char *filename) 43 const char *filename)
44{ 44{
45 char *baseconfig; 45 char *baseconfig;
46 const char *base_config_varname; 46 const char *base_config_varname;
47 47
48 base_config_varname = GNUNET_OS_project_data_get()->base_config_varname; 48 base_config_varname = GNUNET_OS_project_data_get ()->base_config_varname;
49 49
50 if (NULL != (baseconfig = getenv(base_config_varname))) 50 if (NULL != (baseconfig = getenv (base_config_varname)))
51 { 51 {
52 baseconfig = GNUNET_strdup(baseconfig); 52 baseconfig = GNUNET_strdup (baseconfig);
53 } 53 }
54 else 54 else
55 { 55 {
56 char *ipath; 56 char *ipath;
57 57
58 ipath = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR); 58 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
59 if (NULL == ipath) 59 if (NULL == ipath)
60 return GNUNET_SYSERR; 60 return GNUNET_SYSERR;
61 GNUNET_asprintf(&baseconfig, "%s%s", ipath, "config.d"); 61 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
62 GNUNET_free(ipath); 62 GNUNET_free (ipath);
63 } 63 }
64 64
65 if (GNUNET_SYSERR == 65 if (GNUNET_SYSERR ==
66 GNUNET_CONFIGURATION_load_from(cfg, 66 GNUNET_CONFIGURATION_load_from (cfg,
67 baseconfig)) 67 baseconfig))
68 { 68 {
69 GNUNET_free(baseconfig); 69 GNUNET_free (baseconfig);
70 return GNUNET_SYSERR; /* no configuration at all found */ 70 return GNUNET_SYSERR; /* no configuration at all found */
71 } 71 }
72 GNUNET_free(baseconfig); 72 GNUNET_free (baseconfig);
73 if ((NULL != filename) && 73 if ((NULL != filename) &&
74 (GNUNET_OK != GNUNET_CONFIGURATION_parse(cfg, filename))) 74 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
75 { 75 {
76 /* specified configuration not found */ 76 /* specified configuration not found */
77 return GNUNET_SYSERR; 77 return GNUNET_SYSERR;
78 } 78 }
79 if (((GNUNET_YES != 79 if (((GNUNET_YES !=
80 GNUNET_CONFIGURATION_have_value(cfg, "PATHS", "DEFAULTCONFIG"))) && 80 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
81 (filename != NULL)) 81 (filename != NULL))
82 GNUNET_CONFIGURATION_set_value_string(cfg, "PATHS", "DEFAULTCONFIG", 82 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
83 filename); 83 filename);
84 return GNUNET_OK; 84 return GNUNET_OK;
85} 85}
86 86
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index 1bc559ba3..cb7984c3f 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -43,18 +43,19 @@
43#include "gnunet_util_lib.h" 43#include "gnunet_util_lib.h"
44 44
45#define LOG(kind, ...) \ 45#define LOG(kind, ...) \
46 GNUNET_log_from(kind, "util-container-bloomfilter", __VA_ARGS__) 46 GNUNET_log_from (kind, "util-container-bloomfilter", __VA_ARGS__)
47 47
48#define LOG_STRERROR(kind, syscall) \ 48#define LOG_STRERROR(kind, syscall) \
49 GNUNET_log_from_strerror(kind, "util-container-bloomfilter", syscall) 49 GNUNET_log_from_strerror (kind, "util-container-bloomfilter", syscall)
50 50
51#define LOG_STRERROR_FILE(kind, syscall, filename) \ 51#define LOG_STRERROR_FILE(kind, syscall, filename) \
52 GNUNET_log_from_strerror_file(kind, \ 52 GNUNET_log_from_strerror_file (kind, \
53 "util-container-bloomfilter", \ 53 "util-container-bloomfilter", \
54 syscall, \ 54 syscall, \
55 filename) 55 filename)
56 56
57struct GNUNET_CONTAINER_BloomFilter { 57struct GNUNET_CONTAINER_BloomFilter
58{
58 /** 59 /**
59 * The actual bloomfilter bit array 60 * The actual bloomfilter bit array
60 */ 61 */
@@ -89,7 +90,7 @@ struct GNUNET_CONTAINER_BloomFilter {
89 * @return addresses set per element in the bf 90 * @return addresses set per element in the bf
90 */ 91 */
91size_t 92size_t
92GNUNET_CONTAINER_bloomfilter_get_element_addresses( 93GNUNET_CONTAINER_bloomfilter_get_element_addresses (
93 const struct GNUNET_CONTAINER_BloomFilter *bf) 94 const struct GNUNET_CONTAINER_BloomFilter *bf)
94{ 95{
95 if (bf == NULL) 96 if (bf == NULL)
@@ -105,7 +106,7 @@ GNUNET_CONTAINER_bloomfilter_get_element_addresses(
105 * @return number of bytes used for the data of the bloom filter 106 * @return number of bytes used for the data of the bloom filter
106 */ 107 */
107size_t 108size_t
108GNUNET_CONTAINER_bloomfilter_get_size( 109GNUNET_CONTAINER_bloomfilter_get_size (
109 const struct GNUNET_CONTAINER_BloomFilter *bf) 110 const struct GNUNET_CONTAINER_BloomFilter *bf)
110{ 111{
111 if (bf == NULL) 112 if (bf == NULL)
@@ -121,12 +122,12 @@ GNUNET_CONTAINER_bloomfilter_get_size(
121 * @return copy of the bf 122 * @return copy of the bf
122 */ 123 */
123struct GNUNET_CONTAINER_BloomFilter * 124struct GNUNET_CONTAINER_BloomFilter *
124GNUNET_CONTAINER_bloomfilter_copy( 125GNUNET_CONTAINER_bloomfilter_copy (
125 const struct GNUNET_CONTAINER_BloomFilter *bf) 126 const struct GNUNET_CONTAINER_BloomFilter *bf)
126{ 127{
127 return GNUNET_CONTAINER_bloomfilter_init(bf->bitArray, 128 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray,
128 bf->bitArraySize, 129 bf->bitArraySize,
129 bf->addressesPerElement); 130 bf->addressesPerElement);
130} 131}
131 132
132 133
@@ -138,7 +139,7 @@ GNUNET_CONTAINER_bloomfilter_copy(
138 * @param bitIdx which bit to set 139 * @param bitIdx which bit to set
139 */ 140 */
140static void 141static void
141setBit(char *bitArray, unsigned int bitIdx) 142setBit (char *bitArray, unsigned int bitIdx)
142{ 143{
143 size_t arraySlot; 144 size_t arraySlot;
144 unsigned int targetBit; 145 unsigned int targetBit;
@@ -156,7 +157,7 @@ setBit(char *bitArray, unsigned int bitIdx)
156 * @param bitIdx which bit to unset 157 * @param bitIdx which bit to unset
157 */ 158 */
158static void 159static void
159clearBit(char *bitArray, unsigned int bitIdx) 160clearBit (char *bitArray, unsigned int bitIdx)
160{ 161{
161 size_t slot; 162 size_t slot;
162 unsigned int targetBit; 163 unsigned int targetBit;
@@ -174,7 +175,7 @@ clearBit(char *bitArray, unsigned int bitIdx)
174 * @return GNUNET_YES if the bit is set, GNUNET_NO if not. 175 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
175 */ 176 */
176static int 177static int
177testBit(char *bitArray, unsigned int bitIdx) 178testBit (char *bitArray, unsigned int bitIdx)
178{ 179{
179 size_t slot; 180 size_t slot;
180 unsigned int targetBit; 181 unsigned int targetBit;
@@ -197,9 +198,9 @@ testBit(char *bitArray, unsigned int bitIdx)
197 * @param fh A file to keep the 4 bit address usage counters in 198 * @param fh A file to keep the 4 bit address usage counters in
198 */ 199 */
199static void 200static void
200incrementBit(char *bitArray, 201incrementBit (char *bitArray,
201 unsigned int bitIdx, 202 unsigned int bitIdx,
202 const struct GNUNET_DISK_FileHandle *fh) 203 const struct GNUNET_DISK_FileHandle *fh)
203{ 204{
204 off_t fileSlot; 205 off_t fileSlot;
205 unsigned char value; 206 unsigned char value;
@@ -207,34 +208,34 @@ incrementBit(char *bitArray,
207 unsigned int low; 208 unsigned int low;
208 unsigned int targetLoc; 209 unsigned int targetLoc;
209 210
210 setBit(bitArray, bitIdx); 211 setBit (bitArray, bitIdx);
211 if (GNUNET_DISK_handle_invalid(fh)) 212 if (GNUNET_DISK_handle_invalid (fh))
212 return; 213 return;
213 /* Update the counter file on disk */ 214 /* Update the counter file on disk */
214 fileSlot = bitIdx / 2; 215 fileSlot = bitIdx / 2;
215 targetLoc = bitIdx % 2; 216 targetLoc = bitIdx % 2;
216 217
217 GNUNET_assert(fileSlot == 218 GNUNET_assert (fileSlot ==
218 GNUNET_DISK_file_seek(fh, fileSlot, GNUNET_DISK_SEEK_SET)); 219 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
219 if (1 != GNUNET_DISK_file_read(fh, &value, 1)) 220 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
220 value = 0; 221 value = 0;
221 low = value & 0xF; 222 low = value & 0xF;
222 high = (value & (~0xF)) >> 4; 223 high = (value & (~0xF)) >> 4;
223 224
224 if (targetLoc == 0) 225 if (targetLoc == 0)
225 { 226 {
226 if (low < 0xF) 227 if (low < 0xF)
227 low++; 228 low++;
228 } 229 }
229 else 230 else
230 { 231 {
231 if (high < 0xF) 232 if (high < 0xF)
232 high++; 233 high++;
233 } 234 }
234 value = ((high << 4) | low); 235 value = ((high << 4) | low);
235 GNUNET_assert(fileSlot == 236 GNUNET_assert (fileSlot ==
236 GNUNET_DISK_file_seek(fh, fileSlot, GNUNET_DISK_SEEK_SET)); 237 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
237 GNUNET_assert(1 == GNUNET_DISK_file_write(fh, &value, 1)); 238 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
238} 239}
239 240
240/** 241/**
@@ -246,9 +247,9 @@ incrementBit(char *bitArray,
246 * @param fh A file to keep the 4bit address usage counters in 247 * @param fh A file to keep the 4bit address usage counters in
247 */ 248 */
248static void 249static void
249decrementBit(char *bitArray, 250decrementBit (char *bitArray,
250 unsigned int bitIdx, 251 unsigned int bitIdx,
251 const struct GNUNET_DISK_FileHandle *fh) 252 const struct GNUNET_DISK_FileHandle *fh)
252{ 253{
253 off_t fileslot; 254 off_t fileslot;
254 unsigned char value; 255 unsigned char value;
@@ -256,49 +257,49 @@ decrementBit(char *bitArray,
256 unsigned int low; 257 unsigned int low;
257 unsigned int targetLoc; 258 unsigned int targetLoc;
258 259
259 if (GNUNET_DISK_handle_invalid(fh)) 260 if (GNUNET_DISK_handle_invalid (fh))
260 return; /* cannot decrement! */ 261 return; /* cannot decrement! */
261 /* Each char slot in the counter file holds two 4 bit counters */ 262 /* Each char slot in the counter file holds two 4 bit counters */
262 fileslot = bitIdx / 2; 263 fileslot = bitIdx / 2;
263 targetLoc = bitIdx % 2; 264 targetLoc = bitIdx % 2;
264 if (GNUNET_SYSERR == 265 if (GNUNET_SYSERR ==
265 GNUNET_DISK_file_seek(fh, fileslot, GNUNET_DISK_SEEK_SET)) 266 GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
266 { 267 {
267 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "seek"); 268 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
268 return; 269 return;
269 } 270 }
270 if (1 != GNUNET_DISK_file_read(fh, &value, 1)) 271 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
271 value = 0; 272 value = 0;
272 low = value & 0xF; 273 low = value & 0xF;
273 high = (value & 0xF0) >> 4; 274 high = (value & 0xF0) >> 4;
274 275
275 /* decrement, but once we have reached the max, never go back! */ 276 /* decrement, but once we have reached the max, never go back! */
276 if (targetLoc == 0) 277 if (targetLoc == 0)
278 {
279 if ((low > 0) && (low < 0xF))
280 low--;
281 if (low == 0)
277 { 282 {
278 if ((low > 0) && (low < 0xF)) 283 clearBit (bitArray, bitIdx);
279 low--;
280 if (low == 0)
281 {
282 clearBit(bitArray, bitIdx);
283 }
284 } 284 }
285 }
285 else 286 else
287 {
288 if ((high > 0) && (high < 0xF))
289 high--;
290 if (high == 0)
286 { 291 {
287 if ((high > 0) && (high < 0xF)) 292 clearBit (bitArray, bitIdx);
288 high--;
289 if (high == 0)
290 {
291 clearBit(bitArray, bitIdx);
292 }
293 } 293 }
294 }
294 value = ((high << 4) | low); 295 value = ((high << 4) | low);
295 if (GNUNET_SYSERR == 296 if (GNUNET_SYSERR ==
296 GNUNET_DISK_file_seek(fh, fileslot, GNUNET_DISK_SEEK_SET)) 297 GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
297 { 298 {
298 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "seek"); 299 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
299 return; 300 return;
300 } 301 }
301 GNUNET_assert(1 == GNUNET_DISK_file_write(fh, &value, 1)); 302 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
302} 303}
303 304
304#define BUFFSIZE 65536 305#define BUFFSIZE 65536
@@ -311,33 +312,33 @@ decrementBit(char *bitArray,
311 * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise 312 * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
312 */ 313 */
313static int 314static int
314make_empty_file(const struct GNUNET_DISK_FileHandle *fh, size_t size) 315make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size)
315{ 316{
316 char buffer[BUFFSIZE]; 317 char buffer[BUFFSIZE];
317 size_t bytesleft = size; 318 size_t bytesleft = size;
318 int res = 0; 319 int res = 0;
319 320
320 if (GNUNET_DISK_handle_invalid(fh)) 321 if (GNUNET_DISK_handle_invalid (fh))
321 return GNUNET_SYSERR; 322 return GNUNET_SYSERR;
322 memset(buffer, 0, sizeof(buffer)); 323 memset (buffer, 0, sizeof(buffer));
323 GNUNET_DISK_file_seek(fh, 0, GNUNET_DISK_SEEK_SET); 324 GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
324 while (bytesleft > 0) 325 while (bytesleft > 0)
326 {
327 if (bytesleft > sizeof(buffer))
325 { 328 {
326 if (bytesleft > sizeof(buffer)) 329 res = GNUNET_DISK_file_write (fh, buffer, sizeof(buffer));
327 { 330 if (res >= 0)
328 res = GNUNET_DISK_file_write(fh, buffer, sizeof(buffer)); 331 bytesleft -= res;
329 if (res >= 0)
330 bytesleft -= res;
331 }
332 else
333 {
334 res = GNUNET_DISK_file_write(fh, buffer, bytesleft);
335 if (res >= 0)
336 bytesleft -= res;
337 }
338 if (GNUNET_SYSERR == res)
339 return GNUNET_SYSERR;
340 } 332 }
333 else
334 {
335 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
336 if (res >= 0)
337 bytesleft -= res;
338 }
339 if (GNUNET_SYSERR == res)
340 return GNUNET_SYSERR;
341 }
341 return GNUNET_OK; 342 return GNUNET_OK;
342} 343}
343 344
@@ -368,10 +369,10 @@ typedef int (*BitIterator) (void *cls,
368 * @param key the key for which we iterate over the BF bits 369 * @param key the key for which we iterate over the BF bits
369 */ 370 */
370static void 371static void
371iterateBits(const struct GNUNET_CONTAINER_BloomFilter *bf, 372iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
372 BitIterator callback, 373 BitIterator callback,
373 void *arg, 374 void *arg,
374 const struct GNUNET_HashCode *key) 375 const struct GNUNET_HashCode *key)
375{ 376{
376 struct GNUNET_HashCode tmp[2]; 377 struct GNUNET_HashCode tmp[2];
377 int bitCount; 378 int bitCount;
@@ -381,32 +382,32 @@ iterateBits(const struct GNUNET_CONTAINER_BloomFilter *bf,
381 bitCount = bf->addressesPerElement; 382 bitCount = bf->addressesPerElement;
382 tmp[0] = *key; 383 tmp[0] = *key;
383 round = 0; 384 round = 0;
384 GNUNET_assert(bf->bitArraySize > 0); 385 GNUNET_assert (bf->bitArraySize > 0);
385 GNUNET_assert(bf->bitArraySize * 8LL > bf->bitArraySize); 386 GNUNET_assert (bf->bitArraySize * 8LL > bf->bitArraySize);
386 while (bitCount > 0) 387 while (bitCount > 0)
388 {
389 while (slot < (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t)))
390 {
391 if (GNUNET_YES !=
392 callback (arg,
393 bf,
394 ntohl ((((uint32_t *) &tmp[round & 1])[slot]))
395 % ((bf->bitArraySize * 8LL))))
396 return;
397 slot++;
398 bitCount--;
399 if (bitCount == 0)
400 break;
401 }
402 if (bitCount > 0)
387 { 403 {
388 while (slot < (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t))) 404 GNUNET_CRYPTO_hash (&tmp[round & 1],
389 { 405 sizeof(struct GNUNET_HashCode),
390 if (GNUNET_YES != 406 &tmp[(round + 1) & 1]);
391 callback(arg, 407 round++;
392 bf, 408 slot = 0;
393 ntohl((((uint32_t *)&tmp[round & 1])[slot])) %
394 ((bf->bitArraySize * 8LL))))
395 return;
396 slot++;
397 bitCount--;
398 if (bitCount == 0)
399 break;
400 }
401 if (bitCount > 0)
402 {
403 GNUNET_CRYPTO_hash(&tmp[round & 1],
404 sizeof(struct GNUNET_HashCode),
405 &tmp[(round + 1) & 1]);
406 round++;
407 slot = 0;
408 }
409 } 409 }
410 }
410} 411}
411 412
412 413
@@ -419,13 +420,13 @@ iterateBits(const struct GNUNET_CONTAINER_BloomFilter *bf,
419 * @return GNUNET_YES 420 * @return GNUNET_YES
420 */ 421 */
421static int 422static int
422incrementBitCallback(void *cls, 423incrementBitCallback (void *cls,
423 const struct GNUNET_CONTAINER_BloomFilter *bf, 424 const struct GNUNET_CONTAINER_BloomFilter *bf,
424 unsigned int bit) 425 unsigned int bit)
425{ 426{
426 struct GNUNET_CONTAINER_BloomFilter *b = cls; 427 struct GNUNET_CONTAINER_BloomFilter *b = cls;
427 428
428 incrementBit(b->bitArray, bit, bf->fh); 429 incrementBit (b->bitArray, bit, bf->fh);
429 return GNUNET_YES; 430 return GNUNET_YES;
430} 431}
431 432
@@ -439,13 +440,13 @@ incrementBitCallback(void *cls,
439 * @return GNUNET_YES 440 * @return GNUNET_YES
440 */ 441 */
441static int 442static int
442decrementBitCallback(void *cls, 443decrementBitCallback (void *cls,
443 const struct GNUNET_CONTAINER_BloomFilter *bf, 444 const struct GNUNET_CONTAINER_BloomFilter *bf,
444 unsigned int bit) 445 unsigned int bit)
445{ 446{
446 struct GNUNET_CONTAINER_BloomFilter *b = cls; 447 struct GNUNET_CONTAINER_BloomFilter *b = cls;
447 448
448 decrementBit(b->bitArray, bit, bf->fh); 449 decrementBit (b->bitArray, bit, bf->fh);
449 return GNUNET_YES; 450 return GNUNET_YES;
450} 451}
451 452
@@ -459,17 +460,17 @@ decrementBitCallback(void *cls,
459 * @return YES if the bit is set, NO if not 460 * @return YES if the bit is set, NO if not
460 */ 461 */
461static int 462static int
462testBitCallback(void *cls, 463testBitCallback (void *cls,
463 const struct GNUNET_CONTAINER_BloomFilter *bf, 464 const struct GNUNET_CONTAINER_BloomFilter *bf,
464 unsigned int bit) 465 unsigned int bit)
465{ 466{
466 int *arg = cls; 467 int *arg = cls;
467 468
468 if (GNUNET_NO == testBit(bf->bitArray, bit)) 469 if (GNUNET_NO == testBit (bf->bitArray, bit))
469 { 470 {
470 *arg = GNUNET_NO; 471 *arg = GNUNET_NO;
471 return GNUNET_NO; 472 return GNUNET_NO;
472 } 473 }
473 return GNUNET_YES; 474 return GNUNET_YES;
474} 475}
475 476
@@ -487,9 +488,9 @@ testBitCallback(void *cls,
487 * @return the bloomfilter 488 * @return the bloomfilter
488 */ 489 */
489struct GNUNET_CONTAINER_BloomFilter * 490struct GNUNET_CONTAINER_BloomFilter *
490GNUNET_CONTAINER_bloomfilter_load(const char *filename, 491GNUNET_CONTAINER_bloomfilter_load (const char *filename,
491 size_t size, 492 size_t size,
492 unsigned int k) 493 unsigned int k)
493{ 494{
494 struct GNUNET_CONTAINER_BloomFilter *bf; 495 struct GNUNET_CONTAINER_BloomFilter *bf;
495 char *rbuff; 496 char *rbuff;
@@ -499,7 +500,7 @@ GNUNET_CONTAINER_bloomfilter_load(const char *filename,
499 off_t fsize; 500 off_t fsize;
500 int must_read; 501 int must_read;
501 502
502 GNUNET_assert(NULL != filename); 503 GNUNET_assert (NULL != filename);
503 if ((k == 0) || (size == 0)) 504 if ((k == 0) || (size == 0))
504 return NULL; 505 return NULL;
505 if (size < BUFFSIZE) 506 if (size < BUFFSIZE)
@@ -509,115 +510,115 @@ GNUNET_CONTAINER_bloomfilter_load(const char *filename,
509 ui *= 2; 510 ui *= 2;
510 size = ui; /* make sure it's a power of 2 */ 511 size = ui; /* make sure it's a power of 2 */
511 512
512 bf = GNUNET_new(struct GNUNET_CONTAINER_BloomFilter); 513 bf = GNUNET_new (struct GNUNET_CONTAINER_BloomFilter);
513 /* Try to open a bloomfilter file */ 514 /* Try to open a bloomfilter file */
514 if (GNUNET_YES == GNUNET_DISK_file_test(filename)) 515 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
515 bf->fh = GNUNET_DISK_file_open(filename, 516 bf->fh = GNUNET_DISK_file_open (filename,
516 GNUNET_DISK_OPEN_READWRITE, 517 GNUNET_DISK_OPEN_READWRITE,
517 GNUNET_DISK_PERM_USER_READ | 518 GNUNET_DISK_PERM_USER_READ
518 GNUNET_DISK_PERM_USER_WRITE); 519 | GNUNET_DISK_PERM_USER_WRITE);
519 if (NULL != bf->fh) 520 if (NULL != bf->fh)
521 {
522 /* file existed, try to read it! */
523 must_read = GNUNET_YES;
524 if (GNUNET_OK != GNUNET_DISK_file_handle_size (bf->fh, &fsize))
525 {
526 GNUNET_DISK_file_close (bf->fh);
527 GNUNET_free (bf);
528 return NULL;
529 }
530 if (0 == fsize)
520 { 531 {
521 /* file existed, try to read it! */ 532 /* found existing empty file, just overwrite */
522 must_read = GNUNET_YES; 533 if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
523 if (GNUNET_OK != GNUNET_DISK_file_handle_size(bf->fh, &fsize)) 534 {
524 { 535 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
525 GNUNET_DISK_file_close(bf->fh); 536 GNUNET_DISK_file_close (bf->fh);
526 GNUNET_free(bf); 537 GNUNET_free (bf);
527 return NULL; 538 return NULL;
528 } 539 }
529 if (0 == fsize)
530 {
531 /* found existing empty file, just overwrite */
532 if (GNUNET_OK != make_empty_file(bf->fh, size * 4LL))
533 {
534 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "write");
535 GNUNET_DISK_file_close(bf->fh);
536 GNUNET_free(bf);
537 return NULL;
538 }
539 }
540 else if (fsize != ((off_t)size) * 4LL)
541 {
542 GNUNET_log(
543 GNUNET_ERROR_TYPE_ERROR,
544 _(
545 "Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
546 (unsigned long long)(size * 4LL),
547 (unsigned long long)fsize);
548 GNUNET_DISK_file_close(bf->fh);
549 GNUNET_free(bf);
550 return NULL;
551 }
552 } 540 }
541 else if (fsize != ((off_t) size) * 4LL)
542 {
543 GNUNET_log (
544 GNUNET_ERROR_TYPE_ERROR,
545 _ (
546 "Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
547 (unsigned long long) (size * 4LL),
548 (unsigned long long) fsize);
549 GNUNET_DISK_file_close (bf->fh);
550 GNUNET_free (bf);
551 return NULL;
552 }
553 }
553 else 554 else
555 {
556 /* file did not exist, don't read, just create */
557 must_read = GNUNET_NO;
558 bf->fh = GNUNET_DISK_file_open (filename,
559 GNUNET_DISK_OPEN_CREATE
560 | GNUNET_DISK_OPEN_READWRITE,
561 GNUNET_DISK_PERM_USER_READ
562 | GNUNET_DISK_PERM_USER_WRITE);
563 if (NULL == bf->fh)
554 { 564 {
555 /* file did not exist, don't read, just create */ 565 GNUNET_free (bf);
556 must_read = GNUNET_NO; 566 return NULL;
557 bf->fh = GNUNET_DISK_file_open(filename,
558 GNUNET_DISK_OPEN_CREATE |
559 GNUNET_DISK_OPEN_READWRITE,
560 GNUNET_DISK_PERM_USER_READ |
561 GNUNET_DISK_PERM_USER_WRITE);
562 if (NULL == bf->fh)
563 {
564 GNUNET_free(bf);
565 return NULL;
566 }
567 if (GNUNET_OK != make_empty_file(bf->fh, size * 4LL))
568 {
569 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "write");
570 GNUNET_DISK_file_close(bf->fh);
571 GNUNET_free(bf);
572 return NULL;
573 }
574 } 567 }
575 bf->filename = GNUNET_strdup(filename); 568 if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
576 /* Alloc block */
577 bf->bitArray = GNUNET_malloc_large(size);
578 if (NULL == bf->bitArray)
579 { 569 {
580 if (NULL != bf->fh) 570 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
581 GNUNET_DISK_file_close(bf->fh); 571 GNUNET_DISK_file_close (bf->fh);
582 GNUNET_free(bf->filename); 572 GNUNET_free (bf);
583 GNUNET_free(bf);
584 return NULL; 573 return NULL;
585 } 574 }
575 }
576 bf->filename = GNUNET_strdup (filename);
577 /* Alloc block */
578 bf->bitArray = GNUNET_malloc_large (size);
579 if (NULL == bf->bitArray)
580 {
581 if (NULL != bf->fh)
582 GNUNET_DISK_file_close (bf->fh);
583 GNUNET_free (bf->filename);
584 GNUNET_free (bf);
585 return NULL;
586 }
586 bf->bitArraySize = size; 587 bf->bitArraySize = size;
587 bf->addressesPerElement = k; 588 bf->addressesPerElement = k;
588 if (GNUNET_YES != must_read) 589 if (GNUNET_YES != must_read)
589 return bf; /* already done! */ 590 return bf; /* already done! */
590 /* Read from the file what bits we can */ 591 /* Read from the file what bits we can */
591 rbuff = GNUNET_malloc(BUFFSIZE); 592 rbuff = GNUNET_malloc (BUFFSIZE);
592 pos = 0; 593 pos = 0;
593 while (pos < ((off_t)size) * 8LL) 594 while (pos < ((off_t) size) * 8LL)
595 {
596 int res;
597
598 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
599 if (res == -1)
594 { 600 {
595 int res; 601 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
596 602 GNUNET_free (rbuff);
597 res = GNUNET_DISK_file_read(bf->fh, rbuff, BUFFSIZE); 603 GNUNET_free (bf->filename);
598 if (res == -1) 604 GNUNET_DISK_file_close (bf->fh);
599 { 605 GNUNET_free (bf);
600 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "read", bf->filename); 606 return NULL;
601 GNUNET_free(rbuff); 607 }
602 GNUNET_free(bf->filename); 608 if (res == 0)
603 GNUNET_DISK_file_close(bf->fh); 609 break; /* is ok! we just did not use that many bits yet */
604 GNUNET_free(bf); 610 for (i = 0; i < res; i++)
605 return NULL; 611 {
606 } 612 if ((rbuff[i] & 0x0F) != 0)
607 if (res == 0) 613 setBit (bf->bitArray, pos + i * 2);
608 break; /* is ok! we just did not use that many bits yet */ 614 if ((rbuff[i] & 0xF0) != 0)
609 for (i = 0; i < res; i++) 615 setBit (bf->bitArray, pos + i * 2 + 1);
610 {
611 if ((rbuff[i] & 0x0F) != 0)
612 setBit(bf->bitArray, pos + i * 2);
613 if ((rbuff[i] & 0xF0) != 0)
614 setBit(bf->bitArray, pos + i * 2 + 1);
615 }
616 if (res < BUFFSIZE)
617 break;
618 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
619 } 616 }
620 GNUNET_free(rbuff); 617 if (res < BUFFSIZE)
618 break;
619 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
620 }
621 GNUNET_free (rbuff);
621 return bf; 622 return bf;
622} 623}
623 624
@@ -636,27 +637,27 @@ GNUNET_CONTAINER_bloomfilter_load(const char *filename,
636 * @return the bloomfilter 637 * @return the bloomfilter
637 */ 638 */
638struct GNUNET_CONTAINER_BloomFilter * 639struct GNUNET_CONTAINER_BloomFilter *
639GNUNET_CONTAINER_bloomfilter_init(const char *data, 640GNUNET_CONTAINER_bloomfilter_init (const char *data,
640 size_t size, 641 size_t size,
641 unsigned int k) 642 unsigned int k)
642{ 643{
643 struct GNUNET_CONTAINER_BloomFilter *bf; 644 struct GNUNET_CONTAINER_BloomFilter *bf;
644 645
645 if ((0 == k) || (0 == size)) 646 if ((0 == k) || (0 == size))
646 return NULL; 647 return NULL;
647 bf = GNUNET_new(struct GNUNET_CONTAINER_BloomFilter); 648 bf = GNUNET_new (struct GNUNET_CONTAINER_BloomFilter);
648 bf->filename = NULL; 649 bf->filename = NULL;
649 bf->fh = NULL; 650 bf->fh = NULL;
650 bf->bitArray = GNUNET_malloc_large(size); 651 bf->bitArray = GNUNET_malloc_large (size);
651 if (NULL == bf->bitArray) 652 if (NULL == bf->bitArray)
652 { 653 {
653 GNUNET_free(bf); 654 GNUNET_free (bf);
654 return NULL; 655 return NULL;
655 } 656 }
656 bf->bitArraySize = size; 657 bf->bitArraySize = size;
657 bf->addressesPerElement = k; 658 bf->addressesPerElement = k;
658 if (NULL != data) 659 if (NULL != data)
659 GNUNET_memcpy(bf->bitArray, data, size); 660 GNUNET_memcpy (bf->bitArray, data, size);
660 return bf; 661 return bf;
661} 662}
662 663
@@ -671,7 +672,7 @@ GNUNET_CONTAINER_bloomfilter_init(const char *data,
671 * @return #GNUNET_SYSERR if the data array is not big enough 672 * @return #GNUNET_SYSERR if the data array is not big enough
672 */ 673 */
673int 674int
674GNUNET_CONTAINER_bloomfilter_get_raw_data( 675GNUNET_CONTAINER_bloomfilter_get_raw_data (
675 const struct GNUNET_CONTAINER_BloomFilter *bf, 676 const struct GNUNET_CONTAINER_BloomFilter *bf,
676 char *data, 677 char *data,
677 size_t size) 678 size_t size)
@@ -680,7 +681,7 @@ GNUNET_CONTAINER_bloomfilter_get_raw_data(
680 return GNUNET_SYSERR; 681 return GNUNET_SYSERR;
681 if (bf->bitArraySize != size) 682 if (bf->bitArraySize != size)
682 return GNUNET_SYSERR; 683 return GNUNET_SYSERR;
683 GNUNET_memcpy(data, bf->bitArray, size); 684 GNUNET_memcpy (data, bf->bitArray, size);
684 return GNUNET_OK; 685 return GNUNET_OK;
685} 686}
686 687
@@ -693,15 +694,15 @@ GNUNET_CONTAINER_bloomfilter_get_raw_data(
693 * @param bf the filter 694 * @param bf the filter
694 */ 695 */
695void 696void
696GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf) 697GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
697{ 698{
698 if (NULL == bf) 699 if (NULL == bf)
699 return; 700 return;
700 if (bf->fh != NULL) 701 if (bf->fh != NULL)
701 GNUNET_DISK_file_close(bf->fh); 702 GNUNET_DISK_file_close (bf->fh);
702 GNUNET_free_non_null(bf->filename); 703 GNUNET_free_non_null (bf->filename);
703 GNUNET_free(bf->bitArray); 704 GNUNET_free (bf->bitArray);
704 GNUNET_free(bf); 705 GNUNET_free (bf);
705} 706}
706 707
707 708
@@ -711,14 +712,14 @@ GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
711 * @param bf the filter 712 * @param bf the filter
712 */ 713 */
713void 714void
714GNUNET_CONTAINER_bloomfilter_clear(struct GNUNET_CONTAINER_BloomFilter *bf) 715GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
715{ 716{
716 if (NULL == bf) 717 if (NULL == bf)
717 return; 718 return;
718 719
719 memset(bf->bitArray, 0, bf->bitArraySize); 720 memset (bf->bitArray, 0, bf->bitArraySize);
720 if (bf->filename != NULL) 721 if (bf->filename != NULL)
721 make_empty_file(bf->fh, bf->bitArraySize * 4LL); 722 make_empty_file (bf->fh, bf->bitArraySize * 4LL);
722} 723}
723 724
724 725
@@ -730,7 +731,7 @@ GNUNET_CONTAINER_bloomfilter_clear(struct GNUNET_CONTAINER_BloomFilter *bf)
730 * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not 731 * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not
731 */ 732 */
732int 733int
733GNUNET_CONTAINER_bloomfilter_test( 734GNUNET_CONTAINER_bloomfilter_test (
734 const struct GNUNET_CONTAINER_BloomFilter *bf, 735 const struct GNUNET_CONTAINER_BloomFilter *bf,
735 const struct GNUNET_HashCode *e) 736 const struct GNUNET_HashCode *e)
736{ 737{
@@ -739,7 +740,7 @@ GNUNET_CONTAINER_bloomfilter_test(
739 if (NULL == bf) 740 if (NULL == bf)
740 return GNUNET_YES; 741 return GNUNET_YES;
741 res = GNUNET_YES; 742 res = GNUNET_YES;
742 iterateBits(bf, &testBitCallback, &res, e); 743 iterateBits (bf, &testBitCallback, &res, e);
743 return res; 744 return res;
744} 745}
745 746
@@ -751,12 +752,12 @@ GNUNET_CONTAINER_bloomfilter_test(
751 * @param e the element 752 * @param e the element
752 */ 753 */
753void 754void
754GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, 755GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
755 const struct GNUNET_HashCode *e) 756 const struct GNUNET_HashCode *e)
756{ 757{
757 if (NULL == bf) 758 if (NULL == bf)
758 return; 759 return;
759 iterateBits(bf, &incrementBitCallback, bf, e); 760 iterateBits (bf, &incrementBitCallback, bf, e);
760} 761}
761 762
762 763
@@ -771,9 +772,9 @@ GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf,
771 * @param size number of bytes in data 772 * @param size number of bytes in data
772 */ 773 */
773int 774int
774GNUNET_CONTAINER_bloomfilter_or(struct GNUNET_CONTAINER_BloomFilter *bf, 775GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
775 const char *data, 776 const char *data,
776 size_t size) 777 size_t size)
777{ 778{
778 unsigned int i; 779 unsigned int i;
779 unsigned int n; 780 unsigned int n;
@@ -784,8 +785,8 @@ GNUNET_CONTAINER_bloomfilter_or(struct GNUNET_CONTAINER_BloomFilter *bf,
784 return GNUNET_YES; 785 return GNUNET_YES;
785 if (bf->bitArraySize != size) 786 if (bf->bitArraySize != size)
786 return GNUNET_SYSERR; 787 return GNUNET_SYSERR;
787 fc = (unsigned long long *)bf->bitArray; 788 fc = (unsigned long long *) bf->bitArray;
788 dc = (const unsigned long long *)data; 789 dc = (const unsigned long long *) data;
789 n = size / sizeof(unsigned long long); 790 n = size / sizeof(unsigned long long);
790 791
791 for (i = 0; i < n; i++) 792 for (i = 0; i < n; i++)
@@ -806,7 +807,7 @@ GNUNET_CONTAINER_bloomfilter_or(struct GNUNET_CONTAINER_BloomFilter *bf,
806 * @return #GNUNET_OK on success 807 * @return #GNUNET_OK on success
807 */ 808 */
808int 809int
809GNUNET_CONTAINER_bloomfilter_or2( 810GNUNET_CONTAINER_bloomfilter_or2 (
810 struct GNUNET_CONTAINER_BloomFilter *bf, 811 struct GNUNET_CONTAINER_BloomFilter *bf,
811 const struct GNUNET_CONTAINER_BloomFilter *to_or) 812 const struct GNUNET_CONTAINER_BloomFilter *to_or)
812{ 813{
@@ -819,13 +820,13 @@ GNUNET_CONTAINER_bloomfilter_or2(
819 if (NULL == bf) 820 if (NULL == bf)
820 return GNUNET_OK; 821 return GNUNET_OK;
821 if (bf->bitArraySize != to_or->bitArraySize) 822 if (bf->bitArraySize != to_or->bitArraySize)
822 { 823 {
823 GNUNET_break(0); 824 GNUNET_break (0);
824 return GNUNET_SYSERR; 825 return GNUNET_SYSERR;
825 } 826 }
826 size = bf->bitArraySize; 827 size = bf->bitArraySize;
827 fc = (unsigned long long *)bf->bitArray; 828 fc = (unsigned long long *) bf->bitArray;
828 dc = (const unsigned long long *)to_or->bitArray; 829 dc = (const unsigned long long *) to_or->bitArray;
829 n = size / sizeof(unsigned long long); 830 n = size / sizeof(unsigned long long);
830 831
831 for (i = 0; i < n; i++) 832 for (i = 0; i < n; i++)
@@ -843,14 +844,14 @@ GNUNET_CONTAINER_bloomfilter_or2(
843 * @param e the element to remove 844 * @param e the element to remove
844 */ 845 */
845void 846void
846GNUNET_CONTAINER_bloomfilter_remove(struct GNUNET_CONTAINER_BloomFilter *bf, 847GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
847 const struct GNUNET_HashCode *e) 848 const struct GNUNET_HashCode *e)
848{ 849{
849 if (NULL == bf) 850 if (NULL == bf)
850 return; 851 return;
851 if (NULL == bf->filename) 852 if (NULL == bf->filename)
852 return; 853 return;
853 iterateBits(bf, &decrementBitCallback, bf, e); 854 iterateBits (bf, &decrementBitCallback, bf, e);
854} 855}
855 856
856/** 857/**
@@ -865,27 +866,27 @@ GNUNET_CONTAINER_bloomfilter_remove(struct GNUNET_CONTAINER_BloomFilter *bf,
865 * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element 866 * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
866 */ 867 */
867void 868void
868GNUNET_CONTAINER_bloomfilter_resize(struct GNUNET_CONTAINER_BloomFilter *bf, 869GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
869 GNUNET_CONTAINER_HashCodeIterator iterator, 870 GNUNET_CONTAINER_HashCodeIterator iterator,
870 void *iterator_cls, 871 void *iterator_cls,
871 size_t size, 872 size_t size,
872 unsigned int k) 873 unsigned int k)
873{ 874{
874 struct GNUNET_HashCode hc; 875 struct GNUNET_HashCode hc;
875 unsigned int i; 876 unsigned int i;
876 877
877 GNUNET_free(bf->bitArray); 878 GNUNET_free (bf->bitArray);
878 i = 1; 879 i = 1;
879 while (i < size) 880 while (i < size)
880 i *= 2; 881 i *= 2;
881 size = i; /* make sure it's a power of 2 */ 882 size = i; /* make sure it's a power of 2 */
882 bf->addressesPerElement = k; 883 bf->addressesPerElement = k;
883 bf->bitArraySize = size; 884 bf->bitArraySize = size;
884 bf->bitArray = GNUNET_malloc(size); 885 bf->bitArray = GNUNET_malloc (size);
885 if (NULL != bf->filename) 886 if (NULL != bf->filename)
886 make_empty_file(bf->fh, bf->bitArraySize * 4LL); 887 make_empty_file (bf->fh, bf->bitArraySize * 4LL);
887 while (GNUNET_YES == iterator(iterator_cls, &hc)) 888 while (GNUNET_YES == iterator (iterator_cls, &hc))
888 GNUNET_CONTAINER_bloomfilter_add(bf, &hc); 889 GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
889} 890}
890 891
891/* end of container_bloomfilter.c */ 892/* end of container_bloomfilter.c */
diff --git a/src/util/container_heap.c b/src/util/container_heap.c
index 3c8e6ce58..35d7cd4ab 100644
--- a/src/util/container_heap.c
+++ b/src/util/container_heap.c
@@ -28,14 +28,16 @@
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_container_lib.h" 29#include "gnunet_container_lib.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-container-heap", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-container-heap", \
32 __VA_ARGS__)
32 33
33#define EXTRA_CHECKS 0 34#define EXTRA_CHECKS 0
34 35
35/** 36/**
36 * Node in the heap. 37 * Node in the heap.
37 */ 38 */
38struct GNUNET_CONTAINER_HeapNode { 39struct GNUNET_CONTAINER_HeapNode
40{
39 /** 41 /**
40 * Heap this node belongs to. 42 * Heap this node belongs to.
41 */ 43 */
@@ -76,7 +78,8 @@ struct GNUNET_CONTAINER_HeapNode {
76/** 78/**
77 * Handle to a node in a heap. 79 * Handle to a node in a heap.
78 */ 80 */
79struct GNUNET_CONTAINER_Heap { 81struct GNUNET_CONTAINER_Heap
82{
80 /** 83 /**
81 * Root of the heap. 84 * Root of the heap.
82 */ 85 */
@@ -106,21 +109,21 @@ struct GNUNET_CONTAINER_Heap {
106 * @param node subtree to check 109 * @param node subtree to check
107 */ 110 */
108static void 111static void
109check(const struct GNUNET_CONTAINER_HeapNode *node) 112check (const struct GNUNET_CONTAINER_HeapNode *node)
110{ 113{
111 if (NULL == node) 114 if (NULL == node)
112 return; 115 return;
113 GNUNET_assert(node->tree_size == 116 GNUNET_assert (node->tree_size ==
114 ((node->left_child == 117 ((node->left_child ==
115 NULL) ? 0 : 1 + node->left_child->tree_size) + 118 NULL) ? 0 : 1 + node->left_child->tree_size)
116 ((node->right_child == 119 + ((node->right_child ==
117 NULL) ? 0 : 1 + node->right_child->tree_size)); 120 NULL) ? 0 : 1 + node->right_child->tree_size));
118 check(node->left_child); 121 check (node->left_child);
119 check(node->right_child); 122 check (node->right_child);
120} 123}
121 124
122 125
123#define CHECK(n) check(n) 126#define CHECK(n) check (n)
124#else 127#else
125#define CHECK(n) do {} while (0) 128#define CHECK(n) do {} while (0)
126#endif 129#endif
@@ -133,11 +136,11 @@ check(const struct GNUNET_CONTAINER_HeapNode *node)
133 * @return handle to the heap 136 * @return handle to the heap
134 */ 137 */
135struct GNUNET_CONTAINER_Heap * 138struct GNUNET_CONTAINER_Heap *
136GNUNET_CONTAINER_heap_create(enum GNUNET_CONTAINER_HeapOrder order) 139GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order)
137{ 140{
138 struct GNUNET_CONTAINER_Heap *heap; 141 struct GNUNET_CONTAINER_Heap *heap;
139 142
140 heap = GNUNET_new(struct GNUNET_CONTAINER_Heap); 143 heap = GNUNET_new (struct GNUNET_CONTAINER_Heap);
141 heap->order = order; 144 heap->order = order;
142 return heap; 145 return heap;
143} 146}
@@ -150,10 +153,10 @@ GNUNET_CONTAINER_heap_create(enum GNUNET_CONTAINER_HeapOrder order)
150 * @param heap heap to destroy 153 * @param heap heap to destroy
151 */ 154 */
152void 155void
153GNUNET_CONTAINER_heap_destroy(struct GNUNET_CONTAINER_Heap *heap) 156GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
154{ 157{
155 GNUNET_break(heap->size == 0); 158 GNUNET_break (heap->size == 0);
156 GNUNET_free(heap); 159 GNUNET_free (heap);
157} 160}
158 161
159 162
@@ -164,7 +167,7 @@ GNUNET_CONTAINER_heap_destroy(struct GNUNET_CONTAINER_Heap *heap)
164 * @return Element at the root, or NULL if heap is empty. 167 * @return Element at the root, or NULL if heap is empty.
165 */ 168 */
166void * 169void *
167GNUNET_CONTAINER_heap_peek(const struct GNUNET_CONTAINER_Heap *heap) 170GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap)
168{ 171{
169 if (heap->root == NULL) 172 if (heap->root == NULL)
170 return NULL; 173 return NULL;
@@ -182,9 +185,9 @@ GNUNET_CONTAINER_heap_peek(const struct GNUNET_CONTAINER_Heap *heap)
182 * #GNUNET_NO if the heap is empty. 185 * #GNUNET_NO if the heap is empty.
183 */ 186 */
184int 187int
185GNUNET_CONTAINER_heap_peek2(const struct GNUNET_CONTAINER_Heap *heap, 188GNUNET_CONTAINER_heap_peek2 (const struct GNUNET_CONTAINER_Heap *heap,
186 void **element, 189 void **element,
187 GNUNET_CONTAINER_HeapCostType *cost) 190 GNUNET_CONTAINER_HeapCostType *cost)
188{ 191{
189 if (NULL == heap->root) 192 if (NULL == heap->root)
190 return GNUNET_NO; 193 return GNUNET_NO;
@@ -203,7 +206,7 @@ GNUNET_CONTAINER_heap_peek2(const struct GNUNET_CONTAINER_Heap *heap,
203 * @return number of elements stored 206 * @return number of elements stored
204 */ 207 */
205unsigned int 208unsigned int
206GNUNET_CONTAINER_heap_get_size(const struct GNUNET_CONTAINER_Heap *heap) 209GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap)
207{ 210{
208 return heap->size; 211 return heap->size;
209} 212}
@@ -216,8 +219,8 @@ GNUNET_CONTAINER_heap_get_size(const struct GNUNET_CONTAINER_Heap *heap)
216 * @return cost of the node 219 * @return cost of the node
217 */ 220 */
218GNUNET_CONTAINER_HeapCostType 221GNUNET_CONTAINER_HeapCostType
219GNUNET_CONTAINER_heap_node_get_cost(const struct GNUNET_CONTAINER_HeapNode 222GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
220 *node) 223 *node)
221{ 224{
222 return node->cost; 225 return node->cost;
223} 226}
@@ -233,19 +236,19 @@ GNUNET_CONTAINER_heap_node_get_cost(const struct GNUNET_CONTAINER_HeapNode
233 * @return GNUNET_YES to continue to iterate 236 * @return GNUNET_YES to continue to iterate
234 */ 237 */
235static int 238static int
236node_iterator(const struct GNUNET_CONTAINER_Heap *heap, 239node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
237 struct GNUNET_CONTAINER_HeapNode *node, 240 struct GNUNET_CONTAINER_HeapNode *node,
238 GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls) 241 GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls)
239{ 242{
240 if (node == NULL) 243 if (node == NULL)
241 return GNUNET_YES; 244 return GNUNET_YES;
242 if (GNUNET_YES != 245 if (GNUNET_YES !=
243 node_iterator(heap, node->left_child, iterator, iterator_cls)) 246 node_iterator (heap, node->left_child, iterator, iterator_cls))
244 return GNUNET_NO; 247 return GNUNET_NO;
245 if (GNUNET_YES != 248 if (GNUNET_YES !=
246 node_iterator(heap, node->right_child, iterator, iterator_cls)) 249 node_iterator (heap, node->right_child, iterator, iterator_cls))
247 return GNUNET_NO; 250 return GNUNET_NO;
248 return iterator(iterator_cls, node, node->element, node->cost); 251 return iterator (iterator_cls, node, node->element, node->cost);
249} 252}
250 253
251 254
@@ -257,11 +260,11 @@ node_iterator(const struct GNUNET_CONTAINER_Heap *heap,
257 * @param iterator_cls closure for iterator 260 * @param iterator_cls closure for iterator
258 */ 261 */
259void 262void
260GNUNET_CONTAINER_heap_iterate(const struct GNUNET_CONTAINER_Heap *heap, 263GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
261 GNUNET_CONTAINER_HeapIterator iterator, 264 GNUNET_CONTAINER_HeapIterator iterator,
262 void *iterator_cls) 265 void *iterator_cls)
263{ 266{
264 (void)node_iterator(heap, heap->root, iterator, iterator_cls); 267 (void) node_iterator (heap, heap->root, iterator, iterator_cls);
265} 268}
266 269
267 270
@@ -277,7 +280,7 @@ GNUNET_CONTAINER_heap_iterate(const struct GNUNET_CONTAINER_Heap *heap,
277 * NULL if the tree is empty. 280 * NULL if the tree is empty.
278 */ 281 */
279void * 282void *
280GNUNET_CONTAINER_heap_walk_get_next(struct GNUNET_CONTAINER_Heap *heap) 283GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
281{ 284{
282 struct GNUNET_CONTAINER_HeapNode *pos; 285 struct GNUNET_CONTAINER_HeapNode *pos;
283 void *element; 286 void *element;
@@ -290,8 +293,8 @@ GNUNET_CONTAINER_heap_walk_get_next(struct GNUNET_CONTAINER_Heap *heap)
290 element = pos->element; 293 element = pos->element;
291 heap->walk_pos = 294 heap->walk_pos =
292 (0 == 295 (0 ==
293 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 296 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
294 2)) ? pos->right_child : pos->left_child; 297 2)) ? pos->right_child : pos->left_child;
295 return element; 298 return element;
296} 299}
297 300
@@ -305,55 +308,55 @@ GNUNET_CONTAINER_heap_walk_get_next(struct GNUNET_CONTAINER_Heap *heap)
305 * @param node node to insert (which may be a subtree itself) 308 * @param node node to insert (which may be a subtree itself)
306 */ 309 */
307static void 310static void
308insert_node(struct GNUNET_CONTAINER_Heap *heap, 311insert_node (struct GNUNET_CONTAINER_Heap *heap,
309 struct GNUNET_CONTAINER_HeapNode *pos, 312 struct GNUNET_CONTAINER_HeapNode *pos,
310 struct GNUNET_CONTAINER_HeapNode *node) 313 struct GNUNET_CONTAINER_HeapNode *node)
311{ 314{
312 struct GNUNET_CONTAINER_HeapNode *parent; 315 struct GNUNET_CONTAINER_HeapNode *parent;
313 316
314 GNUNET_assert(node->parent == NULL); 317 GNUNET_assert (node->parent == NULL);
315 while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >= 318 while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >=
316 node->cost) 319 node->cost)
317 : (pos->cost <= node->cost)) 320 : (pos->cost <= node->cost))
321 {
322 /* node is descendent of pos */
323 pos->tree_size += (1 + node->tree_size);
324 if (pos->left_child == NULL)
325 {
326 pos->left_child = node;
327 node->parent = pos;
328 return;
329 }
330 if (pos->right_child == NULL)
318 { 331 {
319 /* node is descendent of pos */ 332 pos->right_child = node;
320 pos->tree_size += (1 + node->tree_size); 333 node->parent = pos;
321 if (pos->left_child == NULL) 334 return;
322 {
323 pos->left_child = node;
324 node->parent = pos;
325 return;
326 }
327 if (pos->right_child == NULL)
328 {
329 pos->right_child = node;
330 node->parent = pos;
331 return;
332 }
333 /* keep it balanced by descending into smaller subtree */
334 if (pos->left_child->tree_size < pos->right_child->tree_size)
335 pos = pos->left_child;
336 else
337 pos = pos->right_child;
338 } 335 }
336 /* keep it balanced by descending into smaller subtree */
337 if (pos->left_child->tree_size < pos->right_child->tree_size)
338 pos = pos->left_child;
339 else
340 pos = pos->right_child;
341 }
339 /* make 'node' parent of 'pos' */ 342 /* make 'node' parent of 'pos' */
340 parent = pos->parent; 343 parent = pos->parent;
341 pos->parent = NULL; 344 pos->parent = NULL;
342 node->parent = parent; 345 node->parent = parent;
343 if (NULL == parent) 346 if (NULL == parent)
344 { 347 {
345 heap->root = node; 348 heap->root = node;
346 } 349 }
347 else 350 else
348 { 351 {
349 if (parent->left_child == pos) 352 if (parent->left_child == pos)
350 parent->left_child = node; 353 parent->left_child = node;
351 else 354 else
352 parent->right_child = node; 355 parent->right_child = node;
353 } 356 }
354 /* insert 'pos' below 'node' */ 357 /* insert 'pos' below 'node' */
355 insert_node(heap, node, pos); 358 insert_node (heap, node, pos);
356 CHECK(pos); 359 CHECK (pos);
357} 360}
358 361
359 362
@@ -366,12 +369,12 @@ insert_node(struct GNUNET_CONTAINER_Heap *heap,
366 * @return node for the new element 369 * @return node for the new element
367 */ 370 */
368struct GNUNET_CONTAINER_HeapNode * 371struct GNUNET_CONTAINER_HeapNode *
369GNUNET_CONTAINER_heap_insert(struct GNUNET_CONTAINER_Heap *heap, void *element, 372GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
370 GNUNET_CONTAINER_HeapCostType cost) 373 GNUNET_CONTAINER_HeapCostType cost)
371{ 374{
372 struct GNUNET_CONTAINER_HeapNode *node; 375 struct GNUNET_CONTAINER_HeapNode *node;
373 376
374 node = GNUNET_new(struct GNUNET_CONTAINER_HeapNode); 377 node = GNUNET_new (struct GNUNET_CONTAINER_HeapNode);
375 node->heap = heap; 378 node->heap = heap;
376 node->element = element; 379 node->element = element;
377 node->cost = cost; 380 node->cost = cost;
@@ -379,9 +382,9 @@ GNUNET_CONTAINER_heap_insert(struct GNUNET_CONTAINER_Heap *heap, void *element,
379 if (NULL == heap->root) 382 if (NULL == heap->root)
380 heap->root = node; 383 heap->root = node;
381 else 384 else
382 insert_node(heap, heap->root, node); 385 insert_node (heap, heap->root, node);
383 GNUNET_assert(heap->size == heap->root->tree_size + 1); 386 GNUNET_assert (heap->size == heap->root->tree_size + 1);
384 CHECK(heap->root); 387 CHECK (heap->root);
385 return node; 388 return node;
386} 389}
387 390
@@ -393,7 +396,7 @@ GNUNET_CONTAINER_heap_insert(struct GNUNET_CONTAINER_Heap *heap, void *element,
393 * @return element data stored at the root node, NULL if heap is empty 396 * @return element data stored at the root node, NULL if heap is empty
394 */ 397 */
395void * 398void *
396GNUNET_CONTAINER_heap_remove_root(struct GNUNET_CONTAINER_Heap *heap) 399GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
397{ 400{
398 void *ret; 401 void *ret;
399 struct GNUNET_CONTAINER_HeapNode *root; 402 struct GNUNET_CONTAINER_HeapNode *root;
@@ -403,30 +406,30 @@ GNUNET_CONTAINER_heap_remove_root(struct GNUNET_CONTAINER_Heap *heap)
403 heap->size--; 406 heap->size--;
404 ret = root->element; 407 ret = root->element;
405 if (root->left_child == NULL) 408 if (root->left_child == NULL)
406 { 409 {
407 heap->root = root->right_child; 410 heap->root = root->right_child;
408 if (root->right_child != NULL) 411 if (root->right_child != NULL)
409 root->right_child->parent = NULL; 412 root->right_child->parent = NULL;
410 } 413 }
411 else if (root->right_child == NULL) 414 else if (root->right_child == NULL)
412 { 415 {
413 heap->root = root->left_child; 416 heap->root = root->left_child;
414 root->left_child->parent = NULL; 417 root->left_child->parent = NULL;
415 } 418 }
416 else 419 else
417 { 420 {
418 root->left_child->parent = NULL; 421 root->left_child->parent = NULL;
419 root->right_child->parent = NULL; 422 root->right_child->parent = NULL;
420 heap->root = root->left_child; 423 heap->root = root->left_child;
421 insert_node(heap, heap->root, root->right_child); 424 insert_node (heap, heap->root, root->right_child);
422 } 425 }
423 if (heap->walk_pos == root) 426 if (heap->walk_pos == root)
424 heap->walk_pos = heap->root; 427 heap->walk_pos = heap->root;
425 GNUNET_free(root); 428 GNUNET_free (root);
426#if EXTRA_CHECKS 429#if EXTRA_CHECKS
427 GNUNET_assert(((heap->size == 0) && (heap->root == NULL)) || 430 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
428 (heap->size == heap->root->tree_size + 1)); 431 (heap->size == heap->root->tree_size + 1));
429 CHECK(heap->root); 432 CHECK (heap->root);
430#endif 433#endif
431 return ret; 434 return ret;
432} 435}
@@ -439,7 +442,7 @@ GNUNET_CONTAINER_heap_remove_root(struct GNUNET_CONTAINER_Heap *heap)
439 * 'size' field of the tree. 442 * 'size' field of the tree.
440 */ 443 */
441static void 444static void
442remove_node(struct GNUNET_CONTAINER_HeapNode *node) 445remove_node (struct GNUNET_CONTAINER_HeapNode *node)
443{ 446{
444 struct GNUNET_CONTAINER_HeapNode *ancestor; 447 struct GNUNET_CONTAINER_HeapNode *ancestor;
445 struct GNUNET_CONTAINER_Heap *heap = node->heap; 448 struct GNUNET_CONTAINER_Heap *heap = node->heap;
@@ -457,48 +460,48 @@ remove_node(struct GNUNET_CONTAINER_HeapNode *node)
457 460
458 /* unlink 'node' itself and insert children in its place */ 461 /* unlink 'node' itself and insert children in its place */
459 if (node->parent == NULL) 462 if (node->parent == NULL)
463 {
464 if (node->left_child != NULL)
465 {
466 heap->root = node->left_child;
467 node->left_child->parent = NULL;
468 if (node->right_child != NULL)
469 {
470 node->right_child->parent = NULL;
471 insert_node (heap, heap->root, node->right_child);
472 }
473 }
474 else
460 { 475 {
461 if (node->left_child != NULL) 476 heap->root = node->right_child;
462 { 477 if (node->right_child != NULL)
463 heap->root = node->left_child; 478 node->right_child->parent = NULL;
464 node->left_child->parent = NULL;
465 if (node->right_child != NULL)
466 {
467 node->right_child->parent = NULL;
468 insert_node(heap, heap->root, node->right_child);
469 }
470 }
471 else
472 {
473 heap->root = node->right_child;
474 if (node->right_child != NULL)
475 node->right_child->parent = NULL;
476 }
477 } 479 }
480 }
478 else 481 else
482 {
483 if (node->parent->left_child == node)
484 node->parent->left_child = NULL;
485 else
486 node->parent->right_child = NULL;
487 if (node->left_child != NULL)
479 { 488 {
480 if (node->parent->left_child == node) 489 node->left_child->parent = NULL;
481 node->parent->left_child = NULL; 490 node->parent->tree_size -= (1 + node->left_child->tree_size);
482 else 491 insert_node (heap, node->parent, node->left_child);
483 node->parent->right_child = NULL; 492 }
484 if (node->left_child != NULL) 493 if (node->right_child != NULL)
485 { 494 {
486 node->left_child->parent = NULL; 495 node->right_child->parent = NULL;
487 node->parent->tree_size -= (1 + node->left_child->tree_size); 496 node->parent->tree_size -= (1 + node->right_child->tree_size);
488 insert_node(heap, node->parent, node->left_child); 497 insert_node (heap, node->parent, node->right_child);
489 }
490 if (node->right_child != NULL)
491 {
492 node->right_child->parent = NULL;
493 node->parent->tree_size -= (1 + node->right_child->tree_size);
494 insert_node(heap, node->parent, node->right_child);
495 }
496 } 498 }
499 }
497 node->parent = NULL; 500 node->parent = NULL;
498 node->left_child = NULL; 501 node->left_child = NULL;
499 node->right_child = NULL; 502 node->right_child = NULL;
500 GNUNET_assert(node->tree_size == 0); 503 GNUNET_assert (node->tree_size == 0);
501 CHECK(heap->root); 504 CHECK (heap->root);
502} 505}
503 506
504 507
@@ -509,25 +512,25 @@ remove_node(struct GNUNET_CONTAINER_HeapNode *node)
509 * @return element data stored at the node 512 * @return element data stored at the node
510 */ 513 */
511void * 514void *
512GNUNET_CONTAINER_heap_remove_node(struct GNUNET_CONTAINER_HeapNode *node) 515GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
513{ 516{
514 void *ret; 517 void *ret;
515 struct GNUNET_CONTAINER_Heap *heap; 518 struct GNUNET_CONTAINER_Heap *heap;
516 519
517 heap = node->heap; 520 heap = node->heap;
518 CHECK(heap->root); 521 CHECK (heap->root);
519 if (heap->walk_pos == node) 522 if (heap->walk_pos == node)
520 (void)GNUNET_CONTAINER_heap_walk_get_next(heap); 523 (void) GNUNET_CONTAINER_heap_walk_get_next (heap);
521 remove_node(node); 524 remove_node (node);
522 heap->size--; 525 heap->size--;
523 ret = node->element; 526 ret = node->element;
524 if (heap->walk_pos == node) 527 if (heap->walk_pos == node)
525 heap->walk_pos = NULL; 528 heap->walk_pos = NULL;
526 GNUNET_free(node); 529 GNUNET_free (node);
527#if EXTRA_CHECKS 530#if EXTRA_CHECKS
528 CHECK(heap->root); 531 CHECK (heap->root);
529 GNUNET_assert(((heap->size == 0) && (heap->root == NULL)) || 532 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
530 (heap->size == heap->root->tree_size + 1)); 533 (heap->size == heap->root->tree_size + 1));
531#endif 534#endif
532 return ret; 535 return ret;
533} 536}
@@ -540,19 +543,19 @@ GNUNET_CONTAINER_heap_remove_node(struct GNUNET_CONTAINER_HeapNode *node)
540 * @param new_cost new cost for the node 543 * @param new_cost new cost for the node
541 */ 544 */
542void 545void
543GNUNET_CONTAINER_heap_update_cost(struct GNUNET_CONTAINER_HeapNode *node, 546GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_HeapNode *node,
544 GNUNET_CONTAINER_HeapCostType new_cost) 547 GNUNET_CONTAINER_HeapCostType new_cost)
545{ 548{
546 struct GNUNET_CONTAINER_Heap *heap = node->heap; 549 struct GNUNET_CONTAINER_Heap *heap = node->heap;
547 550
548 remove_node(node); 551 remove_node (node);
549 node->cost = new_cost; 552 node->cost = new_cost;
550 if (NULL == heap->root) 553 if (NULL == heap->root)
551 heap->root = node; 554 heap->root = node;
552 else 555 else
553 insert_node(heap, 556 insert_node (heap,
554 heap->root, 557 heap->root,
555 node); 558 node);
556} 559}
557 560
558 561
diff --git a/src/util/container_meta_data.c b/src/util/container_meta_data.c
index e134ef869..9cbd54f49 100644
--- a/src/util/container_meta_data.c
+++ b/src/util/container_meta_data.c
@@ -31,7 +31,8 @@
31#endif 31#endif
32#include <zlib.h> 32#include <zlib.h>
33 33
34#define LOG(kind, ...) GNUNET_log_from(kind, "util-container-meta-data", __VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from (kind, "util-container-meta-data", \
35 __VA_ARGS__)
35 36
36 37
37 38
@@ -51,10 +52,10 @@
51 * #GNUNET_NO if compression did not help 52 * #GNUNET_NO if compression did not help
52 */ 53 */
53int 54int
54GNUNET_try_compression(const char *data, 55GNUNET_try_compression (const char *data,
55 size_t old_size, 56 size_t old_size,
56 char **result, 57 char **result,
57 size_t *new_size) 58 size_t *new_size)
58{ 59{
59 char *tmp; 60 char *tmp;
60 uLongf dlen; 61 uLongf dlen;
@@ -62,27 +63,27 @@ GNUNET_try_compression(const char *data,
62 *result = NULL; 63 *result = NULL;
63 *new_size = 0; 64 *new_size = 0;
64#ifdef compressBound 65#ifdef compressBound
65 dlen = compressBound(old_size); 66 dlen = compressBound (old_size);
66#else 67#else
67 dlen = old_size + (old_size / 100) + 20; 68 dlen = old_size + (old_size / 100) + 20;
68 /* documentation says 100.1% oldSize + 12 bytes, but we 69 /* documentation says 100.1% oldSize + 12 bytes, but we
69 * should be able to overshoot by more to be safe */ 70 * should be able to overshoot by more to be safe */
70#endif 71#endif
71 tmp = GNUNET_malloc(dlen); 72 tmp = GNUNET_malloc (dlen);
72 if (Z_OK == 73 if (Z_OK ==
73 compress2((Bytef *)tmp, 74 compress2 ((Bytef *) tmp,
74 &dlen, 75 &dlen,
75 (const Bytef *)data, 76 (const Bytef *) data,
76 old_size, 9)) 77 old_size, 9))
78 {
79 if (dlen < old_size)
77 { 80 {
78 if (dlen < old_size) 81 *result = tmp;
79 { 82 *new_size = dlen;
80 *result = tmp; 83 return GNUNET_YES;
81 *new_size = dlen;
82 return GNUNET_YES;
83 }
84 } 84 }
85 GNUNET_free(tmp); 85 }
86 GNUNET_free (tmp);
86 return GNUNET_NO; 87 return GNUNET_NO;
87} 88}
88 89
@@ -98,22 +99,22 @@ GNUNET_try_compression(const char *data,
98 * @return NULL on error, buffer of @a output_size decompressed bytes otherwise 99 * @return NULL on error, buffer of @a output_size decompressed bytes otherwise
99 */ 100 */
100char * 101char *
101GNUNET_decompress(const char *input, 102GNUNET_decompress (const char *input,
102 size_t input_size, 103 size_t input_size,
103 size_t output_size) 104 size_t output_size)
104{ 105{
105 char *output; 106 char *output;
106 uLongf olen; 107 uLongf olen;
107 108
108 olen = output_size; 109 olen = output_size;
109 output = GNUNET_malloc(olen); 110 output = GNUNET_malloc (olen);
110 if (Z_OK == 111 if (Z_OK ==
111 uncompress((Bytef *)output, 112 uncompress ((Bytef *) output,
112 &olen, 113 &olen,
113 (const Bytef *)input, 114 (const Bytef *) input,
114 input_size)) 115 input_size))
115 return output; 116 return output;
116 GNUNET_free(output); 117 GNUNET_free (output);
117 return NULL; 118 return NULL;
118} 119}
119 120
@@ -121,7 +122,8 @@ GNUNET_decompress(const char *input,
121/** 122/**
122 * Meta data item. 123 * Meta data item.
123 */ 124 */
124struct MetaItem { 125struct MetaItem
126{
125 /** 127 /**
126 * This is a doubly linked list. 128 * This is a doubly linked list.
127 */ 129 */
@@ -166,7 +168,8 @@ struct MetaItem {
166/** 168/**
167 * Meta data to associate with a file, directory or namespace. 169 * Meta data to associate with a file, directory or namespace.
168 */ 170 */
169struct GNUNET_CONTAINER_MetaData { 171struct GNUNET_CONTAINER_MetaData
172{
170 /** 173 /**
171 * Head of linked list of the meta data items. 174 * Head of linked list of the meta data items.
172 */ 175 */
@@ -201,9 +204,9 @@ struct GNUNET_CONTAINER_MetaData {
201 * @return empty meta-data container 204 * @return empty meta-data container
202 */ 205 */
203struct GNUNET_CONTAINER_MetaData * 206struct GNUNET_CONTAINER_MetaData *
204GNUNET_CONTAINER_meta_data_create() 207GNUNET_CONTAINER_meta_data_create ()
205{ 208{
206 return GNUNET_new(struct GNUNET_CONTAINER_MetaData); 209 return GNUNET_new (struct GNUNET_CONTAINER_MetaData);
207} 210}
208 211
209 212
@@ -213,12 +216,12 @@ GNUNET_CONTAINER_meta_data_create()
213 * @param mi item to free 216 * @param mi item to free
214 */ 217 */
215static void 218static void
216meta_item_free(struct MetaItem *mi) 219meta_item_free (struct MetaItem *mi)
217{ 220{
218 GNUNET_free_non_null(mi->plugin_name); 221 GNUNET_free_non_null (mi->plugin_name);
219 GNUNET_free_non_null(mi->mime_type); 222 GNUNET_free_non_null (mi->mime_type);
220 GNUNET_free_non_null(mi->data); 223 GNUNET_free_non_null (mi->data);
221 GNUNET_free(mi); 224 GNUNET_free (mi);
222} 225}
223 226
224 227
@@ -229,11 +232,11 @@ meta_item_free(struct MetaItem *mi)
229 * @param md meta data that changed 232 * @param md meta data that changed
230 */ 233 */
231static void 234static void
232invalidate_sbuf(struct GNUNET_CONTAINER_MetaData *md) 235invalidate_sbuf (struct GNUNET_CONTAINER_MetaData *md)
233{ 236{
234 if (NULL == md->sbuf) 237 if (NULL == md->sbuf)
235 return; 238 return;
236 GNUNET_free(md->sbuf); 239 GNUNET_free (md->sbuf);
237 md->sbuf = NULL; 240 md->sbuf = NULL;
238 md->sbuf_size = 0; 241 md->sbuf_size = 0;
239} 242}
@@ -245,19 +248,19 @@ invalidate_sbuf(struct GNUNET_CONTAINER_MetaData *md)
245 * @param md what to free 248 * @param md what to free
246 */ 249 */
247void 250void
248GNUNET_CONTAINER_meta_data_destroy(struct GNUNET_CONTAINER_MetaData *md) 251GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md)
249{ 252{
250 struct MetaItem *pos; 253 struct MetaItem *pos;
251 254
252 if (NULL == md) 255 if (NULL == md)
253 return; 256 return;
254 while (NULL != (pos = md->items_head)) 257 while (NULL != (pos = md->items_head))
255 { 258 {
256 GNUNET_CONTAINER_DLL_remove(md->items_head, md->items_tail, pos); 259 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, pos);
257 meta_item_free(pos); 260 meta_item_free (pos);
258 } 261 }
259 GNUNET_free_non_null(md->sbuf); 262 GNUNET_free_non_null (md->sbuf);
260 GNUNET_free(md); 263 GNUNET_free (md);
261} 264}
262 265
263 266
@@ -267,19 +270,19 @@ GNUNET_CONTAINER_meta_data_destroy(struct GNUNET_CONTAINER_MetaData *md)
267 * @param md metadata to manipulate 270 * @param md metadata to manipulate
268 */ 271 */
269void 272void
270GNUNET_CONTAINER_meta_data_clear(struct GNUNET_CONTAINER_MetaData *md) 273GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md)
271{ 274{
272 struct MetaItem *mi; 275 struct MetaItem *mi;
273 276
274 if (NULL == md) 277 if (NULL == md)
275 return; 278 return;
276 while (NULL != (mi = md->items_head)) 279 while (NULL != (mi = md->items_head))
277 { 280 {
278 GNUNET_CONTAINER_DLL_remove(md->items_head, md->items_tail, mi); 281 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, mi);
279 meta_item_free(mi); 282 meta_item_free (mi);
280 } 283 }
281 GNUNET_free_non_null(md->sbuf); 284 GNUNET_free_non_null (md->sbuf);
282 memset(md, 0, sizeof(struct GNUNET_CONTAINER_MetaData)); 285 memset (md, 0, sizeof(struct GNUNET_CONTAINER_MetaData));
283} 286}
284 287
285 288
@@ -294,10 +297,10 @@ GNUNET_CONTAINER_meta_data_clear(struct GNUNET_CONTAINER_MetaData *md)
294 * @return #GNUNET_YES if they are equal 297 * @return #GNUNET_YES if they are equal
295 */ 298 */
296int 299int
297GNUNET_CONTAINER_meta_data_test_equal(const struct GNUNET_CONTAINER_MetaData 300GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
298 *md1, 301 *md1,
299 const struct GNUNET_CONTAINER_MetaData 302 const struct GNUNET_CONTAINER_MetaData
300 *md2) 303 *md2)
301{ 304{
302 struct MetaItem *i; 305 struct MetaItem *i;
303 struct MetaItem *j; 306 struct MetaItem *j;
@@ -308,23 +311,23 @@ GNUNET_CONTAINER_meta_data_test_equal(const struct GNUNET_CONTAINER_MetaData
308 if (md1->item_count != md2->item_count) 311 if (md1->item_count != md2->item_count)
309 return GNUNET_NO; 312 return GNUNET_NO;
310 for (i = md1->items_head; NULL != i; i = i->next) 313 for (i = md1->items_head; NULL != i; i = i->next)
314 {
315 found = GNUNET_NO;
316 for (j = md2->items_head; NULL != j; j = j->next)
311 { 317 {
312 found = GNUNET_NO; 318 if ((i->type == j->type) && (i->format == j->format) &&
313 for (j = md2->items_head; NULL != j; j = j->next) 319 (i->data_size == j->data_size) &&
314 { 320 (0 == memcmp (i->data, j->data, i->data_size)))
315 if ((i->type == j->type) && (i->format == j->format) && 321 {
316 (i->data_size == j->data_size) && 322 found = GNUNET_YES;
317 (0 == memcmp(i->data, j->data, i->data_size))) 323 break;
318 { 324 }
319 found = GNUNET_YES; 325 if (j->data_size < i->data_size)
320 break; 326 break; /* elements are sorted by (decreasing) size... */
321 }
322 if (j->data_size < i->data_size)
323 break; /* elements are sorted by (decreasing) size... */
324 }
325 if (GNUNET_NO == found)
326 return GNUNET_NO;
327 } 327 }
328 if (GNUNET_NO == found)
329 return GNUNET_NO;
330 }
328 return GNUNET_YES; 331 return GNUNET_YES;
329} 332}
330 333
@@ -348,12 +351,12 @@ GNUNET_CONTAINER_meta_data_test_equal(const struct GNUNET_CONTAINER_MetaData
348 * data_mime_type and plugin_name are not considered for "exists" checks 351 * data_mime_type and plugin_name are not considered for "exists" checks
349 */ 352 */
350int 353int
351GNUNET_CONTAINER_meta_data_insert(struct GNUNET_CONTAINER_MetaData *md, 354GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
352 const char *plugin_name, 355 const char *plugin_name,
353 enum EXTRACTOR_MetaType type, 356 enum EXTRACTOR_MetaType type,
354 enum EXTRACTOR_MetaFormat format, 357 enum EXTRACTOR_MetaFormat format,
355 const char *data_mime_type, const char *data, 358 const char *data_mime_type, const char *data,
356 size_t data_size) 359 size_t data_size)
357{ 360{
358 struct MetaItem *pos; 361 struct MetaItem *pos;
359 struct MetaItem *mi; 362 struct MetaItem *mi;
@@ -361,61 +364,61 @@ GNUNET_CONTAINER_meta_data_insert(struct GNUNET_CONTAINER_MetaData *md,
361 364
362 if ((EXTRACTOR_METAFORMAT_UTF8 == format) || 365 if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
363 (EXTRACTOR_METAFORMAT_C_STRING == format)) 366 (EXTRACTOR_METAFORMAT_C_STRING == format))
364 GNUNET_break('\0' == data[data_size - 1]); 367 GNUNET_break ('\0' == data[data_size - 1]);
365 368
366 for (pos = md->items_head; NULL != pos; pos = pos->next) 369 for (pos = md->items_head; NULL != pos; pos = pos->next)
370 {
371 if (pos->data_size < data_size)
372 break; /* elements are sorted by size in the list */
373 if ((pos->type == type) && (pos->data_size == data_size) &&
374 (0 == memcmp (pos->data, data, data_size)))
367 { 375 {
368 if (pos->data_size < data_size) 376 if ((NULL == pos->mime_type) && (NULL != data_mime_type))
369 break; /* elements are sorted by size in the list */ 377 {
370 if ((pos->type == type) && (pos->data_size == data_size) && 378 pos->mime_type = GNUNET_strdup (data_mime_type);
371 (0 == memcmp(pos->data, data, data_size))) 379 invalidate_sbuf (md);
372 { 380 }
373 if ((NULL == pos->mime_type) && (NULL != data_mime_type)) 381 if ((EXTRACTOR_METAFORMAT_C_STRING == pos->format) &&
374 { 382 (EXTRACTOR_METAFORMAT_UTF8 == format))
375 pos->mime_type = GNUNET_strdup(data_mime_type); 383 {
376 invalidate_sbuf(md); 384 pos->format = EXTRACTOR_METAFORMAT_UTF8;
377 } 385 invalidate_sbuf (md);
378 if ((EXTRACTOR_METAFORMAT_C_STRING == pos->format) && 386 }
379 (EXTRACTOR_METAFORMAT_UTF8 == format)) 387 return GNUNET_SYSERR;
380 {
381 pos->format = EXTRACTOR_METAFORMAT_UTF8;
382 invalidate_sbuf(md);
383 }
384 return GNUNET_SYSERR;
385 }
386 } 388 }
389 }
387 md->item_count++; 390 md->item_count++;
388 mi = GNUNET_new(struct MetaItem); 391 mi = GNUNET_new (struct MetaItem);
389 mi->type = type; 392 mi->type = type;
390 mi->format = format; 393 mi->format = format;
391 mi->data_size = data_size; 394 mi->data_size = data_size;
392 if (NULL == pos) 395 if (NULL == pos)
393 GNUNET_CONTAINER_DLL_insert_tail(md->items_head, 396 GNUNET_CONTAINER_DLL_insert_tail (md->items_head,
394 md->items_tail,
395 mi);
396 else
397 GNUNET_CONTAINER_DLL_insert_after(md->items_head,
398 md->items_tail, 397 md->items_tail,
399 pos->prev,
400 mi); 398 mi);
399 else
400 GNUNET_CONTAINER_DLL_insert_after (md->items_head,
401 md->items_tail,
402 pos->prev,
403 mi);
401 mi->mime_type = 404 mi->mime_type =
402 (NULL == data_mime_type) ? NULL : GNUNET_strdup(data_mime_type); 405 (NULL == data_mime_type) ? NULL : GNUNET_strdup (data_mime_type);
403 mi->plugin_name = (NULL == plugin_name) ? NULL : GNUNET_strdup(plugin_name); 406 mi->plugin_name = (NULL == plugin_name) ? NULL : GNUNET_strdup (plugin_name);
404 mi->data = GNUNET_malloc(data_size); 407 mi->data = GNUNET_malloc (data_size);
405 GNUNET_memcpy(mi->data, data, data_size); 408 GNUNET_memcpy (mi->data, data, data_size);
406 /* change all dir separators to POSIX style ('/') */ 409 /* change all dir separators to POSIX style ('/') */
407 if ((EXTRACTOR_METATYPE_FILENAME == type) || 410 if ((EXTRACTOR_METATYPE_FILENAME == type) ||
408 (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type)) 411 (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type))
412 {
413 p = mi->data;
414 while (('\0' != *p) && (p < mi->data + data_size))
409 { 415 {
410 p = mi->data; 416 if ('\\' == *p)
411 while (('\0' != *p) && (p < mi->data + data_size)) 417 *p = '/';
412 { 418 p++;
413 if ('\\' == *p)
414 *p = '/';
415 p++;
416 }
417 } 419 }
418 invalidate_sbuf(md); 420 }
421 invalidate_sbuf (md);
419 return GNUNET_OK; 422 return GNUNET_OK;
420} 423}
421 424
@@ -437,14 +440,14 @@ GNUNET_CONTAINER_meta_data_insert(struct GNUNET_CONTAINER_MetaData *md,
437 * @return 0 (to continue) 440 * @return 0 (to continue)
438 */ 441 */
439static int 442static int
440merge_helper(void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, 443merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
441 enum EXTRACTOR_MetaFormat format, const char *data_mime_type, 444 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
442 const char *data, size_t data_size) 445 const char *data, size_t data_size)
443{ 446{
444 struct GNUNET_CONTAINER_MetaData *md = cls; 447 struct GNUNET_CONTAINER_MetaData *md = cls;
445 448
446 (void)GNUNET_CONTAINER_meta_data_insert(md, plugin_name, type, format, 449 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
447 data_mime_type, data, data_size); 450 data_mime_type, data, data_size);
448 return 0; 451 return 0;
449} 452}
450 453
@@ -457,10 +460,10 @@ merge_helper(void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
457 * @param in metadata to merge 460 * @param in metadata to merge
458 */ 461 */
459void 462void
460GNUNET_CONTAINER_meta_data_merge(struct GNUNET_CONTAINER_MetaData *md, 463GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
461 const struct GNUNET_CONTAINER_MetaData *in) 464 const struct GNUNET_CONTAINER_MetaData *in)
462{ 465{
463 GNUNET_CONTAINER_meta_data_iterate(in, &merge_helper, md); 466 GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md);
464} 467}
465 468
466 469
@@ -475,28 +478,28 @@ GNUNET_CONTAINER_meta_data_merge(struct GNUNET_CONTAINER_MetaData *md,
475 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the item does not exist in md 478 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the item does not exist in md
476 */ 479 */
477int 480int
478GNUNET_CONTAINER_meta_data_delete(struct GNUNET_CONTAINER_MetaData *md, 481GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
479 enum EXTRACTOR_MetaType type, 482 enum EXTRACTOR_MetaType type,
480 const char *data, size_t data_size) 483 const char *data, size_t data_size)
481{ 484{
482 struct MetaItem *pos; 485 struct MetaItem *pos;
483 486
484 for (pos = md->items_head; NULL != pos; pos = pos->next) 487 for (pos = md->items_head; NULL != pos; pos = pos->next)
488 {
489 if (pos->data_size < data_size)
490 break; /* items are sorted by (decreasing) size */
491 if ((pos->type == type) &&
492 ((NULL == data) ||
493 ((pos->data_size == data_size) &&
494 (0 == memcmp (pos->data, data, data_size)))))
485 { 495 {
486 if (pos->data_size < data_size) 496 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, pos);
487 break; /* items are sorted by (decreasing) size */ 497 meta_item_free (pos);
488 if ((pos->type == type) && 498 md->item_count--;
489 ((NULL == data) || 499 invalidate_sbuf (md);
490 ((pos->data_size == data_size) && 500 return GNUNET_OK;
491 (0 == memcmp(pos->data, data, data_size)))))
492 {
493 GNUNET_CONTAINER_DLL_remove(md->items_head, md->items_tail, pos);
494 meta_item_free(pos);
495 md->item_count--;
496 invalidate_sbuf(md);
497 return GNUNET_OK;
498 }
499 } 501 }
502 }
500 return GNUNET_SYSERR; 503 return GNUNET_SYSERR;
501} 504}
502 505
@@ -508,21 +511,21 @@ GNUNET_CONTAINER_meta_data_delete(struct GNUNET_CONTAINER_MetaData *md,
508 * @param md metadata to modify 511 * @param md metadata to modify
509 */ 512 */
510void 513void
511GNUNET_CONTAINER_meta_data_add_publication_date(struct 514GNUNET_CONTAINER_meta_data_add_publication_date (struct
512 GNUNET_CONTAINER_MetaData *md) 515 GNUNET_CONTAINER_MetaData *md)
513{ 516{
514 const char *dat; 517 const char *dat;
515 struct GNUNET_TIME_Absolute t; 518 struct GNUNET_TIME_Absolute t;
516 519
517 t = GNUNET_TIME_absolute_get(); 520 t = GNUNET_TIME_absolute_get ();
518 GNUNET_CONTAINER_meta_data_delete(md, 521 GNUNET_CONTAINER_meta_data_delete (md,
519 EXTRACTOR_METATYPE_PUBLICATION_DATE, 522 EXTRACTOR_METATYPE_PUBLICATION_DATE,
520 NULL, 0); 523 NULL, 0);
521 dat = GNUNET_STRINGS_absolute_time_to_string(t); 524 dat = GNUNET_STRINGS_absolute_time_to_string (t);
522 GNUNET_CONTAINER_meta_data_insert(md, "<gnunet>", 525 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
523 EXTRACTOR_METATYPE_PUBLICATION_DATE, 526 EXTRACTOR_METATYPE_PUBLICATION_DATE,
524 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 527 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
525 dat, strlen(dat) + 1); 528 dat, strlen (dat) + 1);
526} 529}
527 530
528 531
@@ -535,9 +538,9 @@ GNUNET_CONTAINER_meta_data_add_publication_date(struct
535 * @return number of entries 538 * @return number of entries
536 */ 539 */
537int 540int
538GNUNET_CONTAINER_meta_data_iterate(const struct GNUNET_CONTAINER_MetaData *md, 541GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
539 EXTRACTOR_MetaDataProcessor iter, 542 EXTRACTOR_MetaDataProcessor iter,
540 void *iter_cls) 543 void *iter_cls)
541{ 544{
542 struct MetaItem *pos; 545 struct MetaItem *pos;
543 546
@@ -547,8 +550,8 @@ GNUNET_CONTAINER_meta_data_iterate(const struct GNUNET_CONTAINER_MetaData *md,
547 return md->item_count; 550 return md->item_count;
548 for (pos = md->items_head; NULL != pos; pos = pos->next) 551 for (pos = md->items_head; NULL != pos; pos = pos->next)
549 if (0 != 552 if (0 !=
550 iter(iter_cls, pos->plugin_name, pos->type, pos->format, 553 iter (iter_cls, pos->plugin_name, pos->type, pos->format,
551 pos->mime_type, pos->data, pos->data_size)) 554 pos->mime_type, pos->data, pos->data_size))
552 return md->item_count; 555 return md->item_count;
553 return md->item_count; 556 return md->item_count;
554} 557}
@@ -565,8 +568,9 @@ GNUNET_CONTAINER_meta_data_iterate(const struct GNUNET_CONTAINER_MetaData *md,
565 * @return NULL if no entry was found 568 * @return NULL if no entry was found
566 */ 569 */
567char * 570char *
568GNUNET_CONTAINER_meta_data_get_by_type(const struct GNUNET_CONTAINER_MetaData *md, 571GNUNET_CONTAINER_meta_data_get_by_type (const struct
569 enum EXTRACTOR_MetaType type) 572 GNUNET_CONTAINER_MetaData *md,
573 enum EXTRACTOR_MetaType type)
570{ 574{
571 struct MetaItem *pos; 575 struct MetaItem *pos;
572 576
@@ -576,7 +580,7 @@ GNUNET_CONTAINER_meta_data_get_by_type(const struct GNUNET_CONTAINER_MetaData *m
576 if ((type == pos->type) && 580 if ((type == pos->type) &&
577 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) || 581 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) ||
578 (pos->format == EXTRACTOR_METAFORMAT_C_STRING))) 582 (pos->format == EXTRACTOR_METAFORMAT_C_STRING)))
579 return GNUNET_strdup(pos->data); 583 return GNUNET_strdup (pos->data);
580 return NULL; 584 return NULL;
581} 585}
582 586
@@ -593,9 +597,9 @@ GNUNET_CONTAINER_meta_data_get_by_type(const struct GNUNET_CONTAINER_MetaData *m
593 * otherwise client is responsible for freeing the value! 597 * otherwise client is responsible for freeing the value!
594 */ 598 */
595char * 599char *
596GNUNET_CONTAINER_meta_data_get_first_by_types(const struct 600GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
597 GNUNET_CONTAINER_MetaData *md, 601 GNUNET_CONTAINER_MetaData *md,
598 ...) 602 ...)
599{ 603{
600 char *ret; 604 char *ret;
601 va_list args; 605 va_list args;
@@ -604,16 +608,16 @@ GNUNET_CONTAINER_meta_data_get_first_by_types(const struct
604 if (NULL == md) 608 if (NULL == md)
605 return NULL; 609 return NULL;
606 ret = NULL; 610 ret = NULL;
607 va_start(args, md); 611 va_start (args, md);
608 while (1) 612 while (1)
609 { 613 {
610 type = va_arg(args, int); 614 type = va_arg (args, int);
611 if (-1 == type) 615 if (-1 == type)
612 break; 616 break;
613 if (NULL != (ret = GNUNET_CONTAINER_meta_data_get_by_type(md, type))) 617 if (NULL != (ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type)))
614 break; 618 break;
615 } 619 }
616 va_end(args); 620 va_end (args);
617 return ret; 621 return ret;
618} 622}
619 623
@@ -627,8 +631,8 @@ GNUNET_CONTAINER_meta_data_get_first_by_types(const struct
627 * @return number of bytes in thumbnail, 0 if not available 631 * @return number of bytes in thumbnail, 0 if not available
628 */ 632 */
629size_t 633size_t
630GNUNET_CONTAINER_meta_data_get_thumbnail(const struct GNUNET_CONTAINER_MetaData 634GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
631 * md, unsigned char **thumb) 635 *md, unsigned char **thumb)
632{ 636{
633 struct MetaItem *pos; 637 struct MetaItem *pos;
634 struct MetaItem *match; 638 struct MetaItem *match;
@@ -637,22 +641,22 @@ GNUNET_CONTAINER_meta_data_get_thumbnail(const struct GNUNET_CONTAINER_MetaData
637 return 0; 641 return 0;
638 match = NULL; 642 match = NULL;
639 for (pos = md->items_head; NULL != pos; pos = pos->next) 643 for (pos = md->items_head; NULL != pos; pos = pos->next)
644 {
645 if ((NULL != pos->mime_type) &&
646 (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) &&
647 (EXTRACTOR_METAFORMAT_BINARY == pos->format))
640 { 648 {
641 if ((NULL != pos->mime_type) && 649 if (NULL == match)
642 (0 == strncasecmp("image/", pos->mime_type, strlen("image/"))) && 650 match = pos;
643 (EXTRACTOR_METAFORMAT_BINARY == pos->format)) 651 else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) &&
644 { 652 (pos->type == EXTRACTOR_METATYPE_THUMBNAIL))
645 if (NULL == match) 653 match = pos;
646 match = pos;
647 else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) &&
648 (pos->type == EXTRACTOR_METATYPE_THUMBNAIL))
649 match = pos;
650 }
651 } 654 }
655 }
652 if ((NULL == match) || (0 == match->data_size)) 656 if ((NULL == match) || (0 == match->data_size))
653 return 0; 657 return 0;
654 *thumb = GNUNET_malloc(match->data_size); 658 *thumb = GNUNET_malloc (match->data_size);
655 GNUNET_memcpy(*thumb, match->data, match->data_size); 659 GNUNET_memcpy (*thumb, match->data, match->data_size);
656 return match->data_size; 660 return match->data_size;
657} 661}
658 662
@@ -664,19 +668,19 @@ GNUNET_CONTAINER_meta_data_get_thumbnail(const struct GNUNET_CONTAINER_MetaData
664 * @return duplicate meta-data container 668 * @return duplicate meta-data container
665 */ 669 */
666struct GNUNET_CONTAINER_MetaData * 670struct GNUNET_CONTAINER_MetaData *
667GNUNET_CONTAINER_meta_data_duplicate(const struct GNUNET_CONTAINER_MetaData 671GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
668 *md) 672 *md)
669{ 673{
670 struct GNUNET_CONTAINER_MetaData *ret; 674 struct GNUNET_CONTAINER_MetaData *ret;
671 struct MetaItem *pos; 675 struct MetaItem *pos;
672 676
673 if (NULL == md) 677 if (NULL == md)
674 return NULL; 678 return NULL;
675 ret = GNUNET_CONTAINER_meta_data_create(); 679 ret = GNUNET_CONTAINER_meta_data_create ();
676 for (pos = md->items_tail; NULL != pos; pos = pos->prev) 680 for (pos = md->items_tail; NULL != pos; pos = pos->prev)
677 GNUNET_CONTAINER_meta_data_insert(ret, pos->plugin_name, pos->type, 681 GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type,
678 pos->format, pos->mime_type, pos->data, 682 pos->format, pos->mime_type, pos->data,
679 pos->data_size); 683 pos->data_size);
680 return ret; 684 return ret;
681} 685}
682 686
@@ -696,7 +700,8 @@ GNUNET_CONTAINER_meta_data_duplicate(const struct GNUNET_CONTAINER_MetaData
696/** 700/**
697 * Header for serialized meta data. 701 * Header for serialized meta data.
698 */ 702 */
699struct MetaDataHeader { 703struct MetaDataHeader
704{
700 /** 705 /**
701 * The version of the MD serialization. The highest bit is used to 706 * The version of the MD serialization. The highest bit is used to
702 * indicate compression. 707 * indicate compression.
@@ -729,7 +734,8 @@ struct MetaDataHeader {
729/** 734/**
730 * Entry of serialized meta data. 735 * Entry of serialized meta data.
731 */ 736 */
732struct MetaDataEntry { 737struct MetaDataEntry
738{
733 /** 739 /**
734 * Meta data type. Corresponds to an 'enum EXTRACTOR_MetaType' 740 * Meta data type. Corresponds to an 'enum EXTRACTOR_MetaType'
735 */ 741 */
@@ -772,11 +778,11 @@ struct MetaDataEntry {
772 * space) 778 * space)
773 */ 779 */
774ssize_t 780ssize_t
775GNUNET_CONTAINER_meta_data_serialize(const struct GNUNET_CONTAINER_MetaData 781GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
776 *md, char **target, size_t max, 782 *md, char **target, size_t max,
777 enum 783 enum
778 GNUNET_CONTAINER_MetaDataSerializationOptions 784 GNUNET_CONTAINER_MetaDataSerializationOptions
779 opt) 785 opt)
780{ 786{
781 struct GNUNET_CONTAINER_MetaData *vmd; 787 struct GNUNET_CONTAINER_MetaData *vmd;
782 struct MetaItem *pos; 788 struct MetaItem *pos;
@@ -803,188 +809,188 @@ GNUNET_CONTAINER_meta_data_serialize(const struct GNUNET_CONTAINER_MetaData
803 return 0; 809 return 0;
804 810
805 if (NULL != md->sbuf) 811 if (NULL != md->sbuf)
812 {
813 /* try to use serialization cache */
814 if (md->sbuf_size <= max)
806 { 815 {
807 /* try to use serialization cache */ 816 if (NULL == *target)
808 if (md->sbuf_size <= max) 817 *target = GNUNET_malloc (md->sbuf_size);
809 { 818 GNUNET_memcpy (*target, md->sbuf, md->sbuf_size);
810 if (NULL == *target) 819 return md->sbuf_size;
811 *target = GNUNET_malloc(md->sbuf_size);
812 GNUNET_memcpy(*target, md->sbuf, md->sbuf_size);
813 return md->sbuf_size;
814 }
815 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
816 return GNUNET_SYSERR; /* can say that this will fail */
817 /* need to compute a partial serialization, sbuf useless ... */
818 } 820 }
821 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
822 return GNUNET_SYSERR; /* can say that this will fail */
823 /* need to compute a partial serialization, sbuf useless ... */
824 }
819 dst = NULL; 825 dst = NULL;
820 msize = 0; 826 msize = 0;
821 for (pos = md->items_tail; NULL != pos; pos = pos->prev) 827 for (pos = md->items_tail; NULL != pos; pos = pos->prev)
822 { 828 {
823 msize += sizeof(struct MetaDataEntry); 829 msize += sizeof(struct MetaDataEntry);
824 msize += pos->data_size; 830 msize += pos->data_size;
825 if (NULL != pos->plugin_name) 831 if (NULL != pos->plugin_name)
826 msize += strlen(pos->plugin_name) + 1; 832 msize += strlen (pos->plugin_name) + 1;
827 if (NULL != pos->mime_type) 833 if (NULL != pos->mime_type)
828 msize += strlen(pos->mime_type) + 1; 834 msize += strlen (pos->mime_type) + 1;
829 } 835 }
830 size = (size_t)msize; 836 size = (size_t) msize;
831 if (size != msize) 837 if (size != msize)
832 { 838 {
833 GNUNET_break(0); /* integer overflow */ 839 GNUNET_break (0); /* integer overflow */
834 return GNUNET_SYSERR; 840 return GNUNET_SYSERR;
835 } 841 }
836 if (size >= GNUNET_MAX_MALLOC_CHECKED) 842 if (size >= GNUNET_MAX_MALLOC_CHECKED)
837 { 843 {
838 /* too large to be processed */ 844 /* too large to be processed */
839 return GNUNET_SYSERR; 845 return GNUNET_SYSERR;
840 } 846 }
841 ent = GNUNET_malloc(size); 847 ent = GNUNET_malloc (size);
842 mdata = (char *)&ent[md->item_count]; 848 mdata = (char *) &ent[md->item_count];
843 off = size - (md->item_count * sizeof(struct MetaDataEntry)); 849 off = size - (md->item_count * sizeof(struct MetaDataEntry));
844 i = 0; 850 i = 0;
845 for (pos = md->items_head; NULL != pos; pos = pos->next) 851 for (pos = md->items_head; NULL != pos; pos = pos->next)
846 { 852 {
847 ent[i].type = htonl((uint32_t)pos->type); 853 ent[i].type = htonl ((uint32_t) pos->type);
848 ent[i].format = htonl((uint32_t)pos->format); 854 ent[i].format = htonl ((uint32_t) pos->format);
849 ent[i].data_size = htonl((uint32_t)pos->data_size); 855 ent[i].data_size = htonl ((uint32_t) pos->data_size);
850 if (NULL == pos->plugin_name) 856 if (NULL == pos->plugin_name)
851 plen = 0; 857 plen = 0;
852 else 858 else
853 plen = strlen(pos->plugin_name) + 1; 859 plen = strlen (pos->plugin_name) + 1;
854 ent[i].plugin_name_len = htonl((uint32_t)plen); 860 ent[i].plugin_name_len = htonl ((uint32_t) plen);
855 if (NULL == pos->mime_type) 861 if (NULL == pos->mime_type)
856 mlen = 0; 862 mlen = 0;
857 else 863 else
858 mlen = strlen(pos->mime_type) + 1; 864 mlen = strlen (pos->mime_type) + 1;
859 ent[i].mime_type_len = htonl((uint32_t)mlen); 865 ent[i].mime_type_len = htonl ((uint32_t) mlen);
860 off -= pos->data_size; 866 off -= pos->data_size;
861 if ((EXTRACTOR_METAFORMAT_UTF8 == pos->format) || 867 if ((EXTRACTOR_METAFORMAT_UTF8 == pos->format) ||
862 (EXTRACTOR_METAFORMAT_C_STRING == pos->format)) 868 (EXTRACTOR_METAFORMAT_C_STRING == pos->format))
863 GNUNET_break('\0' == pos->data[pos->data_size - 1]); 869 GNUNET_break ('\0' == pos->data[pos->data_size - 1]);
864 GNUNET_memcpy(&mdata[off], pos->data, pos->data_size); 870 GNUNET_memcpy (&mdata[off], pos->data, pos->data_size);
865 off -= plen; 871 off -= plen;
866 if (NULL != pos->plugin_name) 872 if (NULL != pos->plugin_name)
867 GNUNET_memcpy(&mdata[off], pos->plugin_name, plen); 873 GNUNET_memcpy (&mdata[off], pos->plugin_name, plen);
868 off -= mlen; 874 off -= mlen;
869 if (NULL != pos->mime_type) 875 if (NULL != pos->mime_type)
870 GNUNET_memcpy(&mdata[off], pos->mime_type, mlen); 876 GNUNET_memcpy (&mdata[off], pos->mime_type, mlen);
871 i++; 877 i++;
872 } 878 }
873 GNUNET_assert(0 == off); 879 GNUNET_assert (0 == off);
874 880
875 clen = 0; 881 clen = 0;
876 cdata = NULL; 882 cdata = NULL;
877 left = size; 883 left = size;
878 i = 0; 884 i = 0;
879 for (pos = md->items_head; NULL != pos; pos = pos->next) 885 for (pos = md->items_head; NULL != pos; pos = pos->next)
886 {
887 comp = GNUNET_NO;
888 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
889 comp = GNUNET_try_compression ((const char *) &ent[i],
890 left,
891 &cdata,
892 &clen);
893
894 if ((NULL == md->sbuf) && (0 == i))
880 { 895 {
881 comp = GNUNET_NO; 896 /* fill 'sbuf'; this "modifies" md, but since this is only
882 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS)) 897 * an internal cache we will cast away the 'const' instead
883 comp = GNUNET_try_compression((const char *)&ent[i], 898 * of making the API look strange. */
884 left, 899 vmd = (struct GNUNET_CONTAINER_MetaData *) md;
885 &cdata, 900 hdr = GNUNET_malloc (left + sizeof(struct MetaDataHeader));
886 &clen); 901 hdr->size = htonl (left);
887 902 hdr->entries = htonl (md->item_count);
888 if ((NULL == md->sbuf) && (0 == i)) 903 if (GNUNET_YES == comp)
889 { 904 {
890 /* fill 'sbuf'; this "modifies" md, but since this is only 905 GNUNET_assert (clen < left);
891 * an internal cache we will cast away the 'const' instead 906 hdr->version = htonl (2 | HEADER_COMPRESSED);
892 * of making the API look strange. */ 907 GNUNET_memcpy (&hdr[1], cdata, clen);
893 vmd = (struct GNUNET_CONTAINER_MetaData *)md; 908 vmd->sbuf_size = clen + sizeof(struct MetaDataHeader);
894 hdr = GNUNET_malloc(left + sizeof(struct MetaDataHeader)); 909 }
895 hdr->size = htonl(left); 910 else
896 hdr->entries = htonl(md->item_count); 911 {
897 if (GNUNET_YES == comp) 912 hdr->version = htonl (2);
898 { 913 GNUNET_memcpy (&hdr[1], &ent[0], left);
899 GNUNET_assert(clen < left); 914 vmd->sbuf_size = left + sizeof(struct MetaDataHeader);
900 hdr->version = htonl(2 | HEADER_COMPRESSED); 915 }
901 GNUNET_memcpy(&hdr[1], cdata, clen); 916 vmd->sbuf = (char *) hdr;
902 vmd->sbuf_size = clen + sizeof(struct MetaDataHeader);
903 }
904 else
905 {
906 hdr->version = htonl(2);
907 GNUNET_memcpy(&hdr[1], &ent[0], left);
908 vmd->sbuf_size = left + sizeof(struct MetaDataHeader);
909 }
910 vmd->sbuf = (char *)hdr;
911 }
912
913 if (((left + sizeof(struct MetaDataHeader)) <= max) ||
914 ((GNUNET_YES == comp) && (clen <= max)))
915 {
916 /* success, this now fits! */
917 if (GNUNET_YES == comp)
918 {
919 if (NULL == dst)
920 dst = GNUNET_malloc(clen + sizeof(struct MetaDataHeader));
921 hdr = (struct MetaDataHeader *)dst;
922 hdr->version = htonl(2 | HEADER_COMPRESSED);
923 hdr->size = htonl(left);
924 hdr->entries = htonl(md->item_count - i);
925 GNUNET_memcpy(&dst[sizeof(struct MetaDataHeader)], cdata, clen);
926 GNUNET_free(cdata);
927 cdata = NULL;
928 GNUNET_free(ent);
929 rlen = clen + sizeof(struct MetaDataHeader);
930 }
931 else
932 {
933 if (NULL == dst)
934 dst = GNUNET_malloc(left + sizeof(struct MetaDataHeader));
935 hdr = (struct MetaDataHeader *)dst;
936 hdr->version = htonl(2);
937 hdr->entries = htonl(md->item_count - i);
938 hdr->size = htonl(left);
939 GNUNET_memcpy(&dst[sizeof(struct MetaDataHeader)], &ent[i], left);
940 GNUNET_free(ent);
941 rlen = left + sizeof(struct MetaDataHeader);
942 }
943 if (NULL != *target)
944 {
945 if (GNUNET_YES == comp)
946 GNUNET_memcpy(*target, dst, clen + sizeof(struct MetaDataHeader));
947 else
948 GNUNET_memcpy(*target, dst, left + sizeof(struct MetaDataHeader));
949 GNUNET_free(dst);
950 }
951 else
952 {
953 *target = dst;
954 }
955 return rlen;
956 }
957
958 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
959 {
960 /* does not fit! */
961 GNUNET_free(ent);
962 return GNUNET_SYSERR;
963 }
964
965 /* next iteration: ignore the corresponding meta data at the
966 * end and try again without it */
967 left -= sizeof(struct MetaDataEntry);
968 left -= pos->data_size;
969 if (NULL != pos->plugin_name)
970 left -= strlen(pos->plugin_name) + 1;
971 if (NULL != pos->mime_type)
972 left -= strlen(pos->mime_type) + 1;
973
974 GNUNET_free_non_null(cdata);
975 cdata = NULL;
976
977 i++;
978 } 917 }
979 GNUNET_free(ent); 918
919 if (((left + sizeof(struct MetaDataHeader)) <= max) ||
920 ((GNUNET_YES == comp) && (clen <= max)))
921 {
922 /* success, this now fits! */
923 if (GNUNET_YES == comp)
924 {
925 if (NULL == dst)
926 dst = GNUNET_malloc (clen + sizeof(struct MetaDataHeader));
927 hdr = (struct MetaDataHeader *) dst;
928 hdr->version = htonl (2 | HEADER_COMPRESSED);
929 hdr->size = htonl (left);
930 hdr->entries = htonl (md->item_count - i);
931 GNUNET_memcpy (&dst[sizeof(struct MetaDataHeader)], cdata, clen);
932 GNUNET_free (cdata);
933 cdata = NULL;
934 GNUNET_free (ent);
935 rlen = clen + sizeof(struct MetaDataHeader);
936 }
937 else
938 {
939 if (NULL == dst)
940 dst = GNUNET_malloc (left + sizeof(struct MetaDataHeader));
941 hdr = (struct MetaDataHeader *) dst;
942 hdr->version = htonl (2);
943 hdr->entries = htonl (md->item_count - i);
944 hdr->size = htonl (left);
945 GNUNET_memcpy (&dst[sizeof(struct MetaDataHeader)], &ent[i], left);
946 GNUNET_free (ent);
947 rlen = left + sizeof(struct MetaDataHeader);
948 }
949 if (NULL != *target)
950 {
951 if (GNUNET_YES == comp)
952 GNUNET_memcpy (*target, dst, clen + sizeof(struct MetaDataHeader));
953 else
954 GNUNET_memcpy (*target, dst, left + sizeof(struct MetaDataHeader));
955 GNUNET_free (dst);
956 }
957 else
958 {
959 *target = dst;
960 }
961 return rlen;
962 }
963
964 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
965 {
966 /* does not fit! */
967 GNUNET_free (ent);
968 return GNUNET_SYSERR;
969 }
970
971 /* next iteration: ignore the corresponding meta data at the
972 * end and try again without it */
973 left -= sizeof(struct MetaDataEntry);
974 left -= pos->data_size;
975 if (NULL != pos->plugin_name)
976 left -= strlen (pos->plugin_name) + 1;
977 if (NULL != pos->mime_type)
978 left -= strlen (pos->mime_type) + 1;
979
980 GNUNET_free_non_null (cdata);
981 cdata = NULL;
982
983 i++;
984 }
985 GNUNET_free (ent);
980 986
981 /* nothing fit, only write header! */ 987 /* nothing fit, only write header! */
982 ihdr.version = htonl(2); 988 ihdr.version = htonl (2);
983 ihdr.entries = htonl(0); 989 ihdr.entries = htonl (0);
984 ihdr.size = htonl(0); 990 ihdr.size = htonl (0);
985 if (NULL == *target) 991 if (NULL == *target)
986 *target = (char *)GNUNET_new(struct MetaDataHeader); 992 *target = (char *) GNUNET_new (struct MetaDataHeader);
987 GNUNET_memcpy(*target, &ihdr, sizeof(struct MetaDataHeader)); 993 GNUNET_memcpy (*target, &ihdr, sizeof(struct MetaDataHeader));
988 return sizeof(struct MetaDataHeader); 994 return sizeof(struct MetaDataHeader);
989} 995}
990 996
@@ -996,8 +1002,8 @@ GNUNET_CONTAINER_meta_data_serialize(const struct GNUNET_CONTAINER_MetaData
996 * @return number of bytes needed for serialization, -1 on error 1002 * @return number of bytes needed for serialization, -1 on error
997 */ 1003 */
998ssize_t 1004ssize_t
999GNUNET_CONTAINER_meta_data_get_serialized_size(const struct 1005GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
1000 GNUNET_CONTAINER_MetaData *md) 1006 GNUNET_CONTAINER_MetaData *md)
1001{ 1007{
1002 ssize_t ret; 1008 ssize_t ret;
1003 char *ptr; 1009 char *ptr;
@@ -1006,10 +1012,10 @@ GNUNET_CONTAINER_meta_data_get_serialized_size(const struct
1006 return md->sbuf_size; 1012 return md->sbuf_size;
1007 ptr = NULL; 1013 ptr = NULL;
1008 ret = 1014 ret =
1009 GNUNET_CONTAINER_meta_data_serialize(md, &ptr, GNUNET_MAX_MALLOC_CHECKED, 1015 GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED,
1010 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); 1016 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
1011 if (-1 != ret) 1017 if (-1 != ret)
1012 GNUNET_free(ptr); 1018 GNUNET_free (ptr);
1013 return ret; 1019 return ret;
1014} 1020}
1015 1021
@@ -1023,7 +1029,7 @@ GNUNET_CONTAINER_meta_data_get_serialized_size(const struct
1023 * bad format) 1029 * bad format)
1024 */ 1030 */
1025struct GNUNET_CONTAINER_MetaData * 1031struct GNUNET_CONTAINER_MetaData *
1026GNUNET_CONTAINER_meta_data_deserialize(const char *input, size_t size) 1032GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size)
1027{ 1033{
1028 struct GNUNET_CONTAINER_MetaData *md; 1034 struct GNUNET_CONTAINER_MetaData *md;
1029 struct MetaDataHeader hdr; 1035 struct MetaDataHeader hdr;
@@ -1047,135 +1053,135 @@ GNUNET_CONTAINER_meta_data_deserialize(const char *input, size_t size)
1047 1053
1048 if (size < sizeof(struct MetaDataHeader)) 1054 if (size < sizeof(struct MetaDataHeader))
1049 return NULL; 1055 return NULL;
1050 GNUNET_memcpy(&hdr, input, sizeof(struct MetaDataHeader)); 1056 GNUNET_memcpy (&hdr, input, sizeof(struct MetaDataHeader));
1051 version = ntohl(hdr.version) & HEADER_VERSION_MASK; 1057 version = ntohl (hdr.version) & HEADER_VERSION_MASK;
1052 compressed = (ntohl(hdr.version) & HEADER_COMPRESSED) != 0; 1058 compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0;
1053 1059
1054 if (1 == version) 1060 if (1 == version)
1055 return NULL; /* null pointer */ 1061 return NULL; /* null pointer */
1056 if (2 != version) 1062 if (2 != version)
1057 { 1063 {
1058 GNUNET_break_op(0); /* unsupported version */ 1064 GNUNET_break_op (0); /* unsupported version */
1059 return NULL; 1065 return NULL;
1060 } 1066 }
1061 1067
1062 ic = ntohl(hdr.entries); 1068 ic = ntohl (hdr.entries);
1063 dataSize = ntohl(hdr.size); 1069 dataSize = ntohl (hdr.size);
1064 if (((sizeof(struct MetaDataEntry) * ic) > dataSize) || 1070 if (((sizeof(struct MetaDataEntry) * ic) > dataSize) ||
1065 ((0 != ic) && 1071 ((0 != ic) &&
1066 (dataSize / ic < sizeof(struct MetaDataEntry)))) 1072 (dataSize / ic < sizeof(struct MetaDataEntry))))
1073 {
1074 GNUNET_break_op (0);
1075 return NULL;
1076 }
1077
1078 if (compressed)
1079 {
1080 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED)
1067 { 1081 {
1068 GNUNET_break_op(0); 1082 /* make sure we don't blow our memory limit because of a mal-formed
1083 * message... */
1084 GNUNET_break_op (0);
1069 return NULL; 1085 return NULL;
1070 } 1086 }
1071 1087 data =
1072 if (compressed) 1088 GNUNET_decompress ((const char *) &input[sizeof(struct MetaDataHeader)],
1089 size - sizeof(struct MetaDataHeader),
1090 dataSize);
1091 if (NULL == data)
1073 { 1092 {
1074 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED) 1093 GNUNET_break_op (0);
1075 { 1094 return NULL;
1076 /* make sure we don't blow our memory limit because of a mal-formed
1077 * message... */
1078 GNUNET_break_op(0);
1079 return NULL;
1080 }
1081 data =
1082 GNUNET_decompress((const char *)&input[sizeof(struct MetaDataHeader)],
1083 size - sizeof(struct MetaDataHeader),
1084 dataSize);
1085 if (NULL == data)
1086 {
1087 GNUNET_break_op(0);
1088 return NULL;
1089 }
1090 cdata = data;
1091 } 1095 }
1096 cdata = data;
1097 }
1092 else 1098 else
1099 {
1100 data = NULL;
1101 cdata = (const char *) &input[sizeof(struct MetaDataHeader)];
1102 if (dataSize != size - sizeof(struct MetaDataHeader))
1093 { 1103 {
1094 data = NULL; 1104 GNUNET_break_op (0);
1095 cdata = (const char *)&input[sizeof(struct MetaDataHeader)]; 1105 return NULL;
1096 if (dataSize != size - sizeof(struct MetaDataHeader))
1097 {
1098 GNUNET_break_op(0);
1099 return NULL;
1100 }
1101 } 1106 }
1107 }
1102 1108
1103 md = GNUNET_CONTAINER_meta_data_create(); 1109 md = GNUNET_CONTAINER_meta_data_create ();
1104 left = dataSize - ic * sizeof(struct MetaDataEntry); 1110 left = dataSize - ic * sizeof(struct MetaDataEntry);
1105 mdata = &cdata[ic * sizeof(struct MetaDataEntry)]; 1111 mdata = &cdata[ic * sizeof(struct MetaDataEntry)];
1106 for (i = 0; i < ic; i++) 1112 for (i = 0; i < ic; i++)
1113 {
1114 GNUNET_memcpy (&ent, &cdata[i * sizeof(struct MetaDataEntry)],
1115 sizeof(struct MetaDataEntry));
1116 format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
1117 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
1118 (EXTRACTOR_METAFORMAT_C_STRING != format) &&
1119 (EXTRACTOR_METAFORMAT_BINARY != format))
1107 { 1120 {
1108 GNUNET_memcpy(&ent, &cdata[i * sizeof(struct MetaDataEntry)], 1121 GNUNET_break_op (0);
1109 sizeof(struct MetaDataEntry)); 1122 break;
1110 format = (enum EXTRACTOR_MetaFormat)ntohl(ent.format); 1123 }
1111 if ((EXTRACTOR_METAFORMAT_UTF8 != format) && 1124 dlen = ntohl (ent.data_size);
1112 (EXTRACTOR_METAFORMAT_C_STRING != format) && 1125 plen = ntohl (ent.plugin_name_len);
1113 (EXTRACTOR_METAFORMAT_BINARY != format)) 1126 mlen = ntohl (ent.mime_type_len);
1114 { 1127 if (dlen > left)
1115 GNUNET_break_op(0); 1128 {
1116 break; 1129 GNUNET_break_op (0);
1117 } 1130 break;
1118 dlen = ntohl(ent.data_size); 1131 }
1119 plen = ntohl(ent.plugin_name_len); 1132 left -= dlen;
1120 mlen = ntohl(ent.mime_type_len); 1133 meta_data = &mdata[left];
1121 if (dlen > left) 1134 if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
1122 { 1135 (EXTRACTOR_METAFORMAT_C_STRING == format))
1123 GNUNET_break_op(0); 1136 {
1124 break; 1137 if (0 == dlen)
1125 } 1138 {
1126 left -= dlen; 1139 GNUNET_break_op (0);
1127 meta_data = &mdata[left]; 1140 break;
1128 if ((EXTRACTOR_METAFORMAT_UTF8 == format) || 1141 }
1129 (EXTRACTOR_METAFORMAT_C_STRING == format)) 1142 if ('\0' != meta_data[dlen - 1])
1130 { 1143 {
1131 if (0 == dlen) 1144 GNUNET_break_op (0);
1132 { 1145 break;
1133 GNUNET_break_op(0); 1146 }
1134 break; 1147 }
1135 } 1148 if (plen > left)
1136 if ('\0' != meta_data[dlen - 1]) 1149 {
1137 { 1150 GNUNET_break_op (0);
1138 GNUNET_break_op(0); 1151 break;
1139 break; 1152 }
1140 } 1153 left -= plen;
1141 } 1154 if ((plen > 0) && ('\0' != mdata[left + plen - 1]))
1142 if (plen > left) 1155 {
1143 { 1156 GNUNET_break_op (0);
1144 GNUNET_break_op(0); 1157 break;
1145 break; 1158 }
1146 } 1159 if (0 == plen)
1147 left -= plen; 1160 plugin_name = NULL;
1148 if ((plen > 0) && ('\0' != mdata[left + plen - 1])) 1161 else
1149 { 1162 plugin_name = &mdata[left];
1150 GNUNET_break_op(0); 1163
1151 break; 1164 if (mlen > left)
1152 } 1165 {
1153 if (0 == plen) 1166 GNUNET_break_op (0);
1154 plugin_name = NULL; 1167 break;
1155 else 1168 }
1156 plugin_name = &mdata[left]; 1169 left -= mlen;
1157 1170 if ((mlen > 0) && ('\0' != mdata[left + mlen - 1]))
1158 if (mlen > left) 1171 {
1159 { 1172 GNUNET_break_op (0);
1160 GNUNET_break_op(0); 1173 break;
1161 break;
1162 }
1163 left -= mlen;
1164 if ((mlen > 0) && ('\0' != mdata[left + mlen - 1]))
1165 {
1166 GNUNET_break_op(0);
1167 break;
1168 }
1169 if (0 == mlen)
1170 mime_type = NULL;
1171 else
1172 mime_type = &mdata[left];
1173 GNUNET_CONTAINER_meta_data_insert(md, plugin_name,
1174 (enum EXTRACTOR_MetaType)
1175 ntohl(ent.type), format, mime_type,
1176 meta_data, dlen);
1177 } 1174 }
1178 GNUNET_free_non_null(data); 1175 if (0 == mlen)
1176 mime_type = NULL;
1177 else
1178 mime_type = &mdata[left];
1179 GNUNET_CONTAINER_meta_data_insert (md, plugin_name,
1180 (enum EXTRACTOR_MetaType)
1181 ntohl (ent.type), format, mime_type,
1182 meta_data, dlen);
1183 }
1184 GNUNET_free_non_null (data);
1179 return md; 1185 return md;
1180} 1186}
1181 1187
diff --git a/src/util/container_multihashmap.c b/src/util/container_multihashmap.c
index 62e5311d8..7da34f1a1 100644
--- a/src/util/container_multihashmap.c
+++ b/src/util/container_multihashmap.c
@@ -27,7 +27,7 @@
27#include "gnunet_container_lib.h" 27#include "gnunet_container_lib.h"
28 28
29#define LOG(kind, ...) \ 29#define LOG(kind, ...) \
30 GNUNET_log_from(kind, "util-container-multihashmap", __VA_ARGS__) 30 GNUNET_log_from (kind, "util-container-multihashmap", __VA_ARGS__)
31 31
32/** 32/**
33 * Maximum recursion depth for callbacks of 33 * Maximum recursion depth for callbacks of
@@ -41,7 +41,8 @@
41/** 41/**
42 * An entry in the hash map with the full key. 42 * An entry in the hash map with the full key.
43 */ 43 */
44struct BigMapEntry { 44struct BigMapEntry
45{
45 /** 46 /**
46 * Value of the entry. 47 * Value of the entry.
47 */ 48 */
@@ -62,7 +63,8 @@ struct BigMapEntry {
62/** 63/**
63 * An entry in the hash map with just a pointer to the key. 64 * An entry in the hash map with just a pointer to the key.
64 */ 65 */
65struct SmallMapEntry { 66struct SmallMapEntry
67{
66 /** 68 /**
67 * Value of the entry. 69 * Value of the entry.
68 */ 70 */
@@ -83,7 +85,8 @@ struct SmallMapEntry {
83/** 85/**
84 * Entry in the map. 86 * Entry in the map.
85 */ 87 */
86union MapEntry { 88union MapEntry
89{
87 /** 90 /**
88 * Variant used if map entries only contain a pointer to the key. 91 * Variant used if map entries only contain a pointer to the key.
89 */ 92 */
@@ -99,7 +102,8 @@ union MapEntry {
99/** 102/**
100 * Internal representation of the hash map. 103 * Internal representation of the hash map.
101 */ 104 */
102struct GNUNET_CONTAINER_MultiHashMap { 105struct GNUNET_CONTAINER_MultiHashMap
106{
103 /** 107 /**
104 * All of our buckets. 108 * All of our buckets.
105 */ 109 */
@@ -146,7 +150,8 @@ struct GNUNET_CONTAINER_MultiHashMap {
146 * Cursor into a multihashmap. 150 * Cursor into a multihashmap.
147 * Allows to enumerate elements asynchronously. 151 * Allows to enumerate elements asynchronously.
148 */ 152 */
149struct GNUNET_CONTAINER_MultiHashMapIterator { 153struct GNUNET_CONTAINER_MultiHashMapIterator
154{
150 /** 155 /**
151 * Position in the bucket @e idx 156 * Position in the bucket @e idx
152 */ 157 */
@@ -186,34 +191,34 @@ struct GNUNET_CONTAINER_MultiHashMapIterator {
186 * @return NULL on error 191 * @return NULL on error
187 */ 192 */
188struct GNUNET_CONTAINER_MultiHashMap * 193struct GNUNET_CONTAINER_MultiHashMap *
189GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys) 194GNUNET_CONTAINER_multihashmap_create (unsigned int len, int do_not_copy_keys)
190{ 195{
191 struct GNUNET_CONTAINER_MultiHashMap *hm; 196 struct GNUNET_CONTAINER_MultiHashMap *hm;
192 197
193 GNUNET_assert(len > 0); 198 GNUNET_assert (len > 0);
194 hm = GNUNET_new(struct GNUNET_CONTAINER_MultiHashMap); 199 hm = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap);
195 if (len * sizeof(union MapEntry) > GNUNET_MAX_MALLOC_CHECKED) 200 if (len * sizeof(union MapEntry) > GNUNET_MAX_MALLOC_CHECKED)
201 {
202 size_t s;
203 /* application *explicitly* requested very large map, hopefully
204 it checks the return value... */
205 s = len * sizeof(union MapEntry);
206 if ((s / sizeof(union MapEntry)) != len)
207 return NULL; /* integer overflow on multiplication */
208 if (NULL == (hm->map = GNUNET_malloc_large (s)))
196 { 209 {
197 size_t s; 210 /* out of memory */
198 /* application *explicitly* requested very large map, hopefully 211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
199 it checks the return value... */ 212 "Out of memory allocating large hash map (%u entries)\n",
200 s = len * sizeof(union MapEntry); 213 len);
201 if ((s / sizeof(union MapEntry)) != len) 214 GNUNET_free (hm);
202 return NULL; /* integer overflow on multiplication */ 215 return NULL;
203 if (NULL == (hm->map = GNUNET_malloc_large(s)))
204 {
205 /* out of memory */
206 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
207 "Out of memory allocating large hash map (%u entries)\n",
208 len);
209 GNUNET_free(hm);
210 return NULL;
211 }
212 } 216 }
217 }
213 else 218 else
214 { 219 {
215 hm->map = GNUNET_new_array(len, union MapEntry); 220 hm->map = GNUNET_new_array (len, union MapEntry);
216 } 221 }
217 hm->map_length = len; 222 hm->map_length = len;
218 hm->use_small_entries = do_not_copy_keys; 223 hm->use_small_entries = do_not_copy_keys;
219 return hm; 224 return hm;
@@ -227,44 +232,44 @@ GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
227 * @param map the map 232 * @param map the map
228 */ 233 */
229void 234void
230GNUNET_CONTAINER_multihashmap_destroy( 235GNUNET_CONTAINER_multihashmap_destroy (
231 struct GNUNET_CONTAINER_MultiHashMap *map) 236 struct GNUNET_CONTAINER_MultiHashMap *map)
232{ 237{
233 GNUNET_assert(0 == map->next_cache_off); 238 GNUNET_assert (0 == map->next_cache_off);
234 for (unsigned int i = 0; i < map->map_length; i++) 239 for (unsigned int i = 0; i < map->map_length; i++)
235 { 240 {
236 union MapEntry me; 241 union MapEntry me;
237 242
238 me = map->map[i]; 243 me = map->map[i];
239 if (map->use_small_entries) 244 if (map->use_small_entries)
240 { 245 {
241 struct SmallMapEntry *sme; 246 struct SmallMapEntry *sme;
242 struct SmallMapEntry *nxt; 247 struct SmallMapEntry *nxt;
243 248
244 nxt = me.sme; 249 nxt = me.sme;
245 while (NULL != (sme = nxt)) 250 while (NULL != (sme = nxt))
246 { 251 {
247 nxt = sme->next; 252 nxt = sme->next;
248 GNUNET_free(sme); 253 GNUNET_free (sme);
249 } 254 }
250 me.sme = NULL; 255 me.sme = NULL;
251 }
252 else
253 {
254 struct BigMapEntry *bme;
255 struct BigMapEntry *nxt;
256
257 nxt = me.bme;
258 while (NULL != (bme = nxt))
259 {
260 nxt = bme->next;
261 GNUNET_free(bme);
262 }
263 me.bme = NULL;
264 }
265 } 256 }
266 GNUNET_free(map->map); 257 else
267 GNUNET_free(map); 258 {
259 struct BigMapEntry *bme;
260 struct BigMapEntry *nxt;
261
262 nxt = me.bme;
263 while (NULL != (bme = nxt))
264 {
265 nxt = bme->next;
266 GNUNET_free (bme);
267 }
268 me.bme = NULL;
269 }
270 }
271 GNUNET_free (map->map);
272 GNUNET_free (map);
268} 273}
269 274
270 275
@@ -276,11 +281,11 @@ GNUNET_CONTAINER_multihashmap_destroy(
276 * @return offset into the "map" array of "map" 281 * @return offset into the "map" array of "map"
277 */ 282 */
278static unsigned int 283static unsigned int
279idx_of(const struct GNUNET_CONTAINER_MultiHashMap *map, 284idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map,
280 const struct GNUNET_HashCode *key) 285 const struct GNUNET_HashCode *key)
281{ 286{
282 GNUNET_assert(map != NULL); 287 GNUNET_assert (map != NULL);
283 return (*(unsigned int *)key) % map->map_length; 288 return (*(unsigned int *) key) % map->map_length;
284} 289}
285 290
286 291
@@ -291,7 +296,7 @@ idx_of(const struct GNUNET_CONTAINER_MultiHashMap *map,
291 * @return the number of key value pairs 296 * @return the number of key value pairs
292 */ 297 */
293unsigned int 298unsigned int
294GNUNET_CONTAINER_multihashmap_size( 299GNUNET_CONTAINER_multihashmap_size (
295 const struct GNUNET_CONTAINER_MultiHashMap *map) 300 const struct GNUNET_CONTAINER_MultiHashMap *map)
296{ 301{
297 return map->size; 302 return map->size;
@@ -309,29 +314,29 @@ GNUNET_CONTAINER_multihashmap_size(
309 * key-value pairs with value NULL 314 * key-value pairs with value NULL
310 */ 315 */
311void * 316void *
312GNUNET_CONTAINER_multihashmap_get( 317GNUNET_CONTAINER_multihashmap_get (
313 const struct GNUNET_CONTAINER_MultiHashMap *map, 318 const struct GNUNET_CONTAINER_MultiHashMap *map,
314 const struct GNUNET_HashCode *key) 319 const struct GNUNET_HashCode *key)
315{ 320{
316 union MapEntry me; 321 union MapEntry me;
317 322
318 me = map->map[idx_of(map, key)]; 323 me = map->map[idx_of (map, key)];
319 if (map->use_small_entries) 324 if (map->use_small_entries)
320 { 325 {
321 struct SmallMapEntry *sme; 326 struct SmallMapEntry *sme;
322 327
323 for (sme = me.sme; NULL != sme; sme = sme->next) 328 for (sme = me.sme; NULL != sme; sme = sme->next)
324 if (0 == GNUNET_memcmp(key, sme->key)) 329 if (0 == GNUNET_memcmp (key, sme->key))
325 return sme->value; 330 return sme->value;
326 } 331 }
327 else 332 else
328 { 333 {
329 struct BigMapEntry *bme; 334 struct BigMapEntry *bme;
330 335
331 for (bme = me.bme; NULL != bme; bme = bme->next) 336 for (bme = me.bme; NULL != bme; bme = bme->next)
332 if (0 == GNUNET_memcmp(key, &bme->key)) 337 if (0 == GNUNET_memcmp (key, &bme->key))
333 return bme->value; 338 return bme->value;
334 } 339 }
335 return NULL; 340 return NULL;
336} 341}
337 342
@@ -346,7 +351,7 @@ GNUNET_CONTAINER_multihashmap_get(
346 * #GNUNET_SYSERR if it aborted iteration 351 * #GNUNET_SYSERR if it aborted iteration
347 */ 352 */
348int 353int
349GNUNET_CONTAINER_multihashmap_iterate( 354GNUNET_CONTAINER_multihashmap_iterate (
350 struct GNUNET_CONTAINER_MultiHashMap *map, 355 struct GNUNET_CONTAINER_MultiHashMap *map,
351 GNUNET_CONTAINER_MulitHashMapIteratorCallback it, 356 GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
352 void *it_cls) 357 void *it_cls)
@@ -356,54 +361,54 @@ GNUNET_CONTAINER_multihashmap_iterate(
356 union MapEntry *ce; 361 union MapEntry *ce;
357 struct GNUNET_HashCode kc; 362 struct GNUNET_HashCode kc;
358 363
359 GNUNET_assert(NULL != map); 364 GNUNET_assert (NULL != map);
360 ce = &map->next_cache[map->next_cache_off]; 365 ce = &map->next_cache[map->next_cache_off];
361 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 366 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
362 count = 0; 367 count = 0;
363 for (unsigned i = 0; i < map->map_length; i++) 368 for (unsigned i = 0; i < map->map_length; i++)
369 {
370 me = map->map[i];
371 if (map->use_small_entries)
364 { 372 {
365 me = map->map[i]; 373 struct SmallMapEntry *sme;
366 if (map->use_small_entries) 374
375 ce->sme = me.sme;
376 while (NULL != (sme = ce->sme))
377 {
378 ce->sme = sme->next;
379 if (NULL != it)
367 { 380 {
368 struct SmallMapEntry *sme; 381 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
369 382 {
370 ce->sme = me.sme; 383 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
371 while (NULL != (sme = ce->sme)) 384 return GNUNET_SYSERR;
372 { 385 }
373 ce->sme = sme->next;
374 if (NULL != it)
375 {
376 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
377 {
378 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
379 return GNUNET_SYSERR;
380 }
381 }
382 count++;
383 }
384 } 386 }
385 else 387 count++;
388 }
389 }
390 else
391 {
392 struct BigMapEntry *bme;
393
394 ce->bme = me.bme;
395 while (NULL != (bme = ce->bme))
396 {
397 ce->bme = bme->next;
398 if (NULL != it)
386 { 399 {
387 struct BigMapEntry *bme; 400 kc = bme->key;
388 401 if (GNUNET_OK != it (it_cls, &kc, bme->value))
389 ce->bme = me.bme; 402 {
390 while (NULL != (bme = ce->bme)) 403 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
391 { 404 return GNUNET_SYSERR;
392 ce->bme = bme->next; 405 }
393 if (NULL != it)
394 {
395 kc = bme->key;
396 if (GNUNET_OK != it(it_cls, &kc, bme->value))
397 {
398 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
399 return GNUNET_SYSERR;
400 }
401 }
402 count++;
403 }
404 } 406 }
407 count++;
408 }
405 } 409 }
406 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 410 }
411 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
407 return count; 412 return count;
408} 413}
409 414
@@ -416,8 +421,8 @@ GNUNET_CONTAINER_multihashmap_iterate(
416 * @param bme the entry that is about to be free'd 421 * @param bme the entry that is about to be free'd
417 */ 422 */
418static void 423static void
419update_next_cache_bme(struct GNUNET_CONTAINER_MultiHashMap *map, 424update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map,
420 const struct BigMapEntry *bme) 425 const struct BigMapEntry *bme)
421{ 426{
422 for (unsigned int i = 0; i < map->next_cache_off; i++) 427 for (unsigned int i = 0; i < map->next_cache_off; i++)
423 if (map->next_cache[i].bme == bme) 428 if (map->next_cache[i].bme == bme)
@@ -433,8 +438,8 @@ update_next_cache_bme(struct GNUNET_CONTAINER_MultiHashMap *map,
433 * @param sme the entry that is about to be free'd 438 * @param sme the entry that is about to be free'd
434 */ 439 */
435static void 440static void
436update_next_cache_sme(struct GNUNET_CONTAINER_MultiHashMap *map, 441update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map,
437 const struct SmallMapEntry *sme) 442 const struct SmallMapEntry *sme)
438{ 443{
439 for (unsigned int i = 0; i < map->next_cache_off; i++) 444 for (unsigned int i = 0; i < map->next_cache_off; i++)
440 if (map->next_cache[i].sme == sme) 445 if (map->next_cache[i].sme == sme)
@@ -454,59 +459,59 @@ update_next_cache_sme(struct GNUNET_CONTAINER_MultiHashMap *map,
454 * is not in the map 459 * is not in the map
455 */ 460 */
456int 461int
457GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, 462GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
458 const struct GNUNET_HashCode *key, 463 const struct GNUNET_HashCode *key,
459 const void *value) 464 const void *value)
460{ 465{
461 union MapEntry me; 466 union MapEntry me;
462 unsigned int i; 467 unsigned int i;
463 468
464 map->modification_counter++; 469 map->modification_counter++;
465 470
466 i = idx_of(map, key); 471 i = idx_of (map, key);
467 me = map->map[i]; 472 me = map->map[i];
468 if (map->use_small_entries) 473 if (map->use_small_entries)
469 { 474 {
470 struct SmallMapEntry *p; 475 struct SmallMapEntry *p;
471 476
472 p = NULL; 477 p = NULL;
473 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 478 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
474 { 479 {
475 if ((0 == GNUNET_memcmp(key, sme->key)) && (value == sme->value)) 480 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
476 { 481 {
477 if (NULL == p) 482 if (NULL == p)
478 map->map[i].sme = sme->next; 483 map->map[i].sme = sme->next;
479 else 484 else
480 p->next = sme->next; 485 p->next = sme->next;
481 update_next_cache_sme(map, sme); 486 update_next_cache_sme (map, sme);
482 GNUNET_free(sme); 487 GNUNET_free (sme);
483 map->size--; 488 map->size--;
484 return GNUNET_YES; 489 return GNUNET_YES;
485 } 490 }
486 p = sme; 491 p = sme;
487 }
488 } 492 }
493 }
489 else 494 else
490 { 495 {
491 struct BigMapEntry *p; 496 struct BigMapEntry *p;
492 497
493 p = NULL; 498 p = NULL;
494 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 499 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
495 { 500 {
496 if ((0 == GNUNET_memcmp(key, &bme->key)) && (value == bme->value)) 501 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
497 { 502 {
498 if (NULL == p) 503 if (NULL == p)
499 map->map[i].bme = bme->next; 504 map->map[i].bme = bme->next;
500 else 505 else
501 p->next = bme->next; 506 p->next = bme->next;
502 update_next_cache_bme(map, bme); 507 update_next_cache_bme (map, bme);
503 GNUNET_free(bme); 508 GNUNET_free (bme);
504 map->size--; 509 map->size--;
505 return GNUNET_YES; 510 return GNUNET_YES;
506 } 511 }
507 p = bme; 512 p = bme;
508 }
509 } 513 }
514 }
510 return GNUNET_NO; 515 return GNUNET_NO;
511} 516}
512 517
@@ -520,7 +525,7 @@ GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map,
520 * @return number of values removed 525 * @return number of values removed
521 */ 526 */
522int 527int
523GNUNET_CONTAINER_multihashmap_remove_all( 528GNUNET_CONTAINER_multihashmap_remove_all (
524 struct GNUNET_CONTAINER_MultiHashMap *map, 529 struct GNUNET_CONTAINER_MultiHashMap *map,
525 const struct GNUNET_HashCode *key) 530 const struct GNUNET_HashCode *key)
526{ 531{
@@ -531,70 +536,70 @@ GNUNET_CONTAINER_multihashmap_remove_all(
531 map->modification_counter++; 536 map->modification_counter++;
532 537
533 ret = 0; 538 ret = 0;
534 i = idx_of(map, key); 539 i = idx_of (map, key);
535 me = map->map[i]; 540 me = map->map[i];
536 if (map->use_small_entries) 541 if (map->use_small_entries)
537 { 542 {
538 struct SmallMapEntry *sme; 543 struct SmallMapEntry *sme;
539 struct SmallMapEntry *p; 544 struct SmallMapEntry *p;
540 545
541 p = NULL; 546 p = NULL;
542 sme = me.sme; 547 sme = me.sme;
543 while (NULL != sme) 548 while (NULL != sme)
544 { 549 {
545 if (0 == GNUNET_memcmp(key, sme->key)) 550 if (0 == GNUNET_memcmp (key, sme->key))
546 { 551 {
547 if (NULL == p) 552 if (NULL == p)
548 map->map[i].sme = sme->next; 553 map->map[i].sme = sme->next;
549 else 554 else
550 p->next = sme->next; 555 p->next = sme->next;
551 update_next_cache_sme(map, sme); 556 update_next_cache_sme (map, sme);
552 GNUNET_free(sme); 557 GNUNET_free (sme);
553 map->size--; 558 map->size--;
554 if (NULL == p) 559 if (NULL == p)
555 sme = map->map[i].sme; 560 sme = map->map[i].sme;
556 else 561 else
557 sme = p->next; 562 sme = p->next;
558 ret++; 563 ret++;
559 } 564 }
560 else 565 else
561 { 566 {
562 p = sme; 567 p = sme;
563 sme = sme->next; 568 sme = sme->next;
564 } 569 }
565 }
566 } 570 }
571 }
567 else 572 else
568 { 573 {
569 struct BigMapEntry *bme; 574 struct BigMapEntry *bme;
570 struct BigMapEntry *p; 575 struct BigMapEntry *p;
571 576
572 p = NULL; 577 p = NULL;
573 bme = me.bme; 578 bme = me.bme;
574 while (NULL != bme) 579 while (NULL != bme)
575 { 580 {
576 if (0 == GNUNET_memcmp(key, &bme->key)) 581 if (0 == GNUNET_memcmp (key, &bme->key))
577 { 582 {
578 if (NULL == p) 583 if (NULL == p)
579 map->map[i].bme = bme->next; 584 map->map[i].bme = bme->next;
580 else 585 else
581 p->next = bme->next; 586 p->next = bme->next;
582 update_next_cache_bme(map, bme); 587 update_next_cache_bme (map, bme);
583 GNUNET_free(bme); 588 GNUNET_free (bme);
584 map->size--; 589 map->size--;
585 if (NULL == p) 590 if (NULL == p)
586 bme = map->map[i].bme; 591 bme = map->map[i].bme;
587 else 592 else
588 bme = p->next; 593 bme = p->next;
589 ret++; 594 ret++;
590 } 595 }
591 else 596 else
592 { 597 {
593 p = bme; 598 p = bme;
594 bme = bme->next; 599 bme = bme->next;
595 } 600 }
596 }
597 } 601 }
602 }
598 return ret; 603 return ret;
599} 604}
600 605
@@ -608,12 +613,12 @@ GNUNET_CONTAINER_multihashmap_remove_all(
608 * @return #GNUNET_OK (continue to iterate) 613 * @return #GNUNET_OK (continue to iterate)
609 */ 614 */
610static int 615static int
611remove_all(void *cls, const struct GNUNET_HashCode *key, void *value) 616remove_all (void *cls, const struct GNUNET_HashCode *key, void *value)
612{ 617{
613 struct GNUNET_CONTAINER_MultiHashMap *map = cls; 618 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
614 619
615 GNUNET_assert(GNUNET_YES == 620 GNUNET_assert (GNUNET_YES ==
616 GNUNET_CONTAINER_multihashmap_remove(map, key, value)); 621 GNUNET_CONTAINER_multihashmap_remove (map, key, value));
617 return GNUNET_OK; 622 return GNUNET_OK;
618} 623}
619 624
@@ -627,12 +632,12 @@ remove_all(void *cls, const struct GNUNET_HashCode *key, void *value)
627 * @return number of values removed 632 * @return number of values removed
628 */ 633 */
629unsigned int 634unsigned int
630GNUNET_CONTAINER_multihashmap_clear(struct GNUNET_CONTAINER_MultiHashMap *map) 635GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
631{ 636{
632 unsigned int ret; 637 unsigned int ret;
633 638
634 ret = map->size; 639 ret = map->size;
635 GNUNET_CONTAINER_multihashmap_iterate(map, &remove_all, map); 640 GNUNET_CONTAINER_multihashmap_iterate (map, &remove_all, map);
636 return ret; 641 return ret;
637} 642}
638 643
@@ -647,29 +652,29 @@ GNUNET_CONTAINER_multihashmap_clear(struct GNUNET_CONTAINER_MultiHashMap *map)
647 * #GNUNET_NO if not 652 * #GNUNET_NO if not
648 */ 653 */
649int 654int
650GNUNET_CONTAINER_multihashmap_contains( 655GNUNET_CONTAINER_multihashmap_contains (
651 const struct GNUNET_CONTAINER_MultiHashMap *map, 656 const struct GNUNET_CONTAINER_MultiHashMap *map,
652 const struct GNUNET_HashCode *key) 657 const struct GNUNET_HashCode *key)
653{ 658{
654 union MapEntry me; 659 union MapEntry me;
655 660
656 me = map->map[idx_of(map, key)]; 661 me = map->map[idx_of (map, key)];
657 if (map->use_small_entries) 662 if (map->use_small_entries)
658 { 663 {
659 struct SmallMapEntry *sme; 664 struct SmallMapEntry *sme;
660 665
661 for (sme = me.sme; NULL != sme; sme = sme->next) 666 for (sme = me.sme; NULL != sme; sme = sme->next)
662 if (0 == GNUNET_memcmp(key, sme->key)) 667 if (0 == GNUNET_memcmp (key, sme->key))
663 return GNUNET_YES; 668 return GNUNET_YES;
664 } 669 }
665 else 670 else
666 { 671 {
667 struct BigMapEntry *bme; 672 struct BigMapEntry *bme;
668 673
669 for (bme = me.bme; NULL != bme; bme = bme->next) 674 for (bme = me.bme; NULL != bme; bme = bme->next)
670 if (0 == GNUNET_memcmp(key, &bme->key)) 675 if (0 == GNUNET_memcmp (key, &bme->key))
671 return GNUNET_YES; 676 return GNUNET_YES;
672 } 677 }
673 return GNUNET_NO; 678 return GNUNET_NO;
674} 679}
675 680
@@ -685,30 +690,30 @@ GNUNET_CONTAINER_multihashmap_contains(
685 * #GNUNET_NO if not 690 * #GNUNET_NO if not
686 */ 691 */
687int 692int
688GNUNET_CONTAINER_multihashmap_contains_value( 693GNUNET_CONTAINER_multihashmap_contains_value (
689 const struct GNUNET_CONTAINER_MultiHashMap *map, 694 const struct GNUNET_CONTAINER_MultiHashMap *map,
690 const struct GNUNET_HashCode *key, 695 const struct GNUNET_HashCode *key,
691 const void *value) 696 const void *value)
692{ 697{
693 union MapEntry me; 698 union MapEntry me;
694 699
695 me = map->map[idx_of(map, key)]; 700 me = map->map[idx_of (map, key)];
696 if (map->use_small_entries) 701 if (map->use_small_entries)
697 { 702 {
698 struct SmallMapEntry *sme; 703 struct SmallMapEntry *sme;
699 704
700 for (sme = me.sme; NULL != sme; sme = sme->next) 705 for (sme = me.sme; NULL != sme; sme = sme->next)
701 if ((0 == GNUNET_memcmp(key, sme->key)) && (sme->value == value)) 706 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
702 return GNUNET_YES; 707 return GNUNET_YES;
703 } 708 }
704 else 709 else
705 { 710 {
706 struct BigMapEntry *bme; 711 struct BigMapEntry *bme;
707 712
708 for (bme = me.bme; NULL != bme; bme = bme->next) 713 for (bme = me.bme; NULL != bme; bme = bme->next)
709 if ((0 == GNUNET_memcmp(key, &bme->key)) && (bme->value == value)) 714 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
710 return GNUNET_YES; 715 return GNUNET_YES;
711 } 716 }
712 return GNUNET_NO; 717 return GNUNET_NO;
713} 718}
714 719
@@ -719,7 +724,7 @@ GNUNET_CONTAINER_multihashmap_contains_value(
719 * @param map the hash map to grow 724 * @param map the hash map to grow
720 */ 725 */
721static void 726static void
722grow(struct GNUNET_CONTAINER_MultiHashMap *map) 727grow (struct GNUNET_CONTAINER_MultiHashMap *map)
723{ 728{
724 union MapEntry *old_map; 729 union MapEntry *old_map;
725 union MapEntry *new_map; 730 union MapEntry *new_map;
@@ -729,46 +734,46 @@ grow(struct GNUNET_CONTAINER_MultiHashMap *map)
729 734
730 old_map = map->map; 735 old_map = map->map;
731 old_len = map->map_length; 736 old_len = map->map_length;
732 GNUNET_assert(0 != old_len); 737 GNUNET_assert (0 != old_len);
733 new_len = old_len * 2; 738 new_len = old_len * 2;
734 if (0 == new_len) /* 2^31 * 2 == 0 */ 739 if (0 == new_len) /* 2^31 * 2 == 0 */
735 new_len = old_len; /* never use 0 */ 740 new_len = old_len; /* never use 0 */
736 if (new_len == old_len) 741 if (new_len == old_len)
737 return; /* nothing changed */ 742 return; /* nothing changed */
738 new_map = GNUNET_malloc_large(new_len * sizeof(union MapEntry)); 743 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
739 if (NULL == new_map) 744 if (NULL == new_map)
740 return; /* grow not possible */ 745 return; /* grow not possible */
741 map->modification_counter++; 746 map->modification_counter++;
742 map->map_length = new_len; 747 map->map_length = new_len;
743 map->map = new_map; 748 map->map = new_map;
744 for (unsigned int i = 0; i < old_len; i++) 749 for (unsigned int i = 0; i < old_len; i++)
750 {
751 if (map->use_small_entries)
745 { 752 {
746 if (map->use_small_entries) 753 struct SmallMapEntry *sme;
747 { 754
748 struct SmallMapEntry *sme; 755 while (NULL != (sme = old_map[i].sme))
749 756 {
750 while (NULL != (sme = old_map[i].sme)) 757 old_map[i].sme = sme->next;
751 { 758 idx = idx_of (map, sme->key);
752 old_map[i].sme = sme->next; 759 sme->next = new_map[idx].sme;
753 idx = idx_of(map, sme->key); 760 new_map[idx].sme = sme;
754 sme->next = new_map[idx].sme; 761 }
755 new_map[idx].sme = sme; 762 }
756 } 763 else
757 } 764 {
758 else 765 struct BigMapEntry *bme;
759 { 766
760 struct BigMapEntry *bme; 767 while (NULL != (bme = old_map[i].bme))
761 768 {
762 while (NULL != (bme = old_map[i].bme)) 769 old_map[i].bme = bme->next;
763 { 770 idx = idx_of (map, &bme->key);
764 old_map[i].bme = bme->next; 771 bme->next = new_map[idx].bme;
765 idx = idx_of(map, &bme->key); 772 new_map[idx].bme = bme;
766 bme->next = new_map[idx].bme; 773 }
767 new_map[idx].bme = bme;
768 }
769 }
770 } 774 }
771 GNUNET_free(old_map); 775 }
776 GNUNET_free (old_map);
772} 777}
773 778
774 779
@@ -785,71 +790,71 @@ grow(struct GNUNET_CONTAINER_MultiHashMap *map)
785 * value already exists 790 * value already exists
786 */ 791 */
787int 792int
788GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, 793GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
789 const struct GNUNET_HashCode *key, 794 const struct GNUNET_HashCode *key,
790 void *value, 795 void *value,
791 enum GNUNET_CONTAINER_MultiHashMapOption opt) 796 enum GNUNET_CONTAINER_MultiHashMapOption opt)
792{ 797{
793 union MapEntry me; 798 union MapEntry me;
794 unsigned int i; 799 unsigned int i;
795 800
796 i = idx_of(map, key); 801 i = idx_of (map, key);
797 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 802 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
798 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 803 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
804 {
805 me = map->map[i];
806 if (map->use_small_entries)
799 { 807 {
800 me = map->map[i]; 808 struct SmallMapEntry *sme;
801 if (map->use_small_entries) 809
810 for (sme = me.sme; NULL != sme; sme = sme->next)
811 if (0 == GNUNET_memcmp (key, sme->key))
802 { 812 {
803 struct SmallMapEntry *sme; 813 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
804 814 return GNUNET_SYSERR;
805 for (sme = me.sme; NULL != sme; sme = sme->next) 815 sme->value = value;
806 if (0 == GNUNET_memcmp(key, sme->key)) 816 return GNUNET_NO;
807 {
808 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
809 return GNUNET_SYSERR;
810 sme->value = value;
811 return GNUNET_NO;
812 }
813 } 817 }
814 else 818 }
819 else
820 {
821 struct BigMapEntry *bme;
822
823 for (bme = me.bme; NULL != bme; bme = bme->next)
824 if (0 == GNUNET_memcmp (key, &bme->key))
815 { 825 {
816 struct BigMapEntry *bme; 826 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
817 827 return GNUNET_SYSERR;
818 for (bme = me.bme; NULL != bme; bme = bme->next) 828 bme->value = value;
819 if (0 == GNUNET_memcmp(key, &bme->key)) 829 return GNUNET_NO;
820 {
821 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
822 return GNUNET_SYSERR;
823 bme->value = value;
824 return GNUNET_NO;
825 }
826 } 830 }
827 } 831 }
832 }
828 if (map->size / 3 >= map->map_length / 4) 833 if (map->size / 3 >= map->map_length / 4)
829 { 834 {
830 grow(map); 835 grow (map);
831 i = idx_of(map, key); 836 i = idx_of (map, key);
832 } 837 }
833 if (map->use_small_entries) 838 if (map->use_small_entries)
834 { 839 {
835 struct SmallMapEntry *sme; 840 struct SmallMapEntry *sme;
836 841
837 sme = GNUNET_new(struct SmallMapEntry); 842 sme = GNUNET_new (struct SmallMapEntry);
838 sme->key = key; 843 sme->key = key;
839 sme->value = value; 844 sme->value = value;
840 sme->next = map->map[i].sme; 845 sme->next = map->map[i].sme;
841 map->map[i].sme = sme; 846 map->map[i].sme = sme;
842 } 847 }
843 else 848 else
844 { 849 {
845 struct BigMapEntry *bme; 850 struct BigMapEntry *bme;
846 851
847 bme = GNUNET_new(struct BigMapEntry); 852 bme = GNUNET_new (struct BigMapEntry);
848 bme->key = *key; 853 bme->key = *key;
849 bme->value = value; 854 bme->value = value;
850 bme->next = map->map[i].bme; 855 bme->next = map->map[i].bme;
851 map->map[i].bme = bme; 856 map->map[i].bme = bme;
852 } 857 }
853 map->size++; 858 map->size++;
854 return GNUNET_OK; 859 return GNUNET_OK;
855} 860}
@@ -866,7 +871,7 @@ GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map,
866 * #GNUNET_SYSERR if it aborted iteration 871 * #GNUNET_SYSERR if it aborted iteration
867 */ 872 */
868int 873int
869GNUNET_CONTAINER_multihashmap_get_multiple( 874GNUNET_CONTAINER_multihashmap_get_multiple (
870 struct GNUNET_CONTAINER_MultiHashMap *map, 875 struct GNUNET_CONTAINER_MultiHashMap *map,
871 const struct GNUNET_HashCode *key, 876 const struct GNUNET_HashCode *key,
872 GNUNET_CONTAINER_MulitHashMapIteratorCallback it, 877 GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
@@ -877,46 +882,46 @@ GNUNET_CONTAINER_multihashmap_get_multiple(
877 union MapEntry *ce; 882 union MapEntry *ce;
878 883
879 ce = &map->next_cache[map->next_cache_off]; 884 ce = &map->next_cache[map->next_cache_off];
880 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 885 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
881 count = 0; 886 count = 0;
882 me = &map->map[idx_of(map, key)]; 887 me = &map->map[idx_of (map, key)];
883 if (map->use_small_entries) 888 if (map->use_small_entries)
884 { 889 {
885 struct SmallMapEntry *sme; 890 struct SmallMapEntry *sme;
886 891
887 ce->sme = me->sme; 892 ce->sme = me->sme;
888 while (NULL != (sme = ce->sme)) 893 while (NULL != (sme = ce->sme))
889 { 894 {
890 ce->sme = sme->next; 895 ce->sme = sme->next;
891 if (0 != GNUNET_memcmp(key, sme->key)) 896 if (0 != GNUNET_memcmp (key, sme->key))
892 continue; 897 continue;
893 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, sme->value))) 898 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
894 { 899 {
895 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 900 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
896 return GNUNET_SYSERR; 901 return GNUNET_SYSERR;
897 } 902 }
898 count++; 903 count++;
899 }
900 } 904 }
905 }
901 else 906 else
902 { 907 {
903 struct BigMapEntry *bme; 908 struct BigMapEntry *bme;
904 909
905 ce->bme = me->bme; 910 ce->bme = me->bme;
906 while (NULL != (bme = ce->bme)) 911 while (NULL != (bme = ce->bme))
907 { 912 {
908 ce->bme = bme->next; 913 ce->bme = bme->next;
909 if (0 != GNUNET_memcmp(key, &bme->key)) 914 if (0 != GNUNET_memcmp (key, &bme->key))
910 continue; 915 continue;
911 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, bme->value))) 916 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
912 { 917 {
913 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 918 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
914 return GNUNET_SYSERR; 919 return GNUNET_SYSERR;
915 } 920 }
916 count++; 921 count++;
917 }
918 } 922 }
919 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 923 }
924 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
920 return count; 925 return count;
921} 926}
922 927
@@ -933,7 +938,7 @@ GNUNET_CONTAINER_multihashmap_get_multiple(
933 * @return the number of key value pairs processed, zero or one. 938 * @return the number of key value pairs processed, zero or one.
934 */ 939 */
935unsigned int 940unsigned int
936GNUNET_CONTAINER_multihashmap_get_random( 941GNUNET_CONTAINER_multihashmap_get_random (
937 const struct GNUNET_CONTAINER_MultiHashMap *map, 942 const struct GNUNET_CONTAINER_MultiHashMap *map,
938 GNUNET_CONTAINER_MulitHashMapIteratorCallback it, 943 GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
939 void *it_cls) 944 void *it_cls)
@@ -946,48 +951,48 @@ GNUNET_CONTAINER_multihashmap_get_random(
946 return 0; 951 return 0;
947 if (NULL == it) 952 if (NULL == it)
948 return 1; 953 return 1;
949 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, map->size); 954 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
950 for (idx = 0; idx < map->map_length; idx++) 955 for (idx = 0; idx < map->map_length; idx++)
956 {
957 me = map->map[idx];
958 if (map->use_small_entries)
951 { 959 {
952 me = map->map[idx]; 960 struct SmallMapEntry *sme;
953 if (map->use_small_entries) 961 struct SmallMapEntry *nxt;
962
963 nxt = me.sme;
964 while (NULL != (sme = nxt))
965 {
966 nxt = sme->next;
967 if (0 == off)
954 { 968 {
955 struct SmallMapEntry *sme; 969 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
956 struct SmallMapEntry *nxt; 970 return GNUNET_SYSERR;
957 971 return 1;
958 nxt = me.sme;
959 while (NULL != (sme = nxt))
960 {
961 nxt = sme->next;
962 if (0 == off)
963 {
964 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
965 return GNUNET_SYSERR;
966 return 1;
967 }
968 off--;
969 }
970 } 972 }
971 else 973 off--;
974 }
975 }
976 else
977 {
978 struct BigMapEntry *bme;
979 struct BigMapEntry *nxt;
980
981 nxt = me.bme;
982 while (NULL != (bme = nxt))
983 {
984 nxt = bme->next;
985 if (0 == off)
972 { 986 {
973 struct BigMapEntry *bme; 987 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
974 struct BigMapEntry *nxt; 988 return GNUNET_SYSERR;
975 989 return 1;
976 nxt = me.bme;
977 while (NULL != (bme = nxt))
978 {
979 nxt = bme->next;
980 if (0 == off)
981 {
982 if (GNUNET_OK != it(it_cls, &bme->key, bme->value))
983 return GNUNET_SYSERR;
984 return 1;
985 }
986 off--;
987 }
988 } 990 }
991 off--;
992 }
989 } 993 }
990 GNUNET_break(0); 994 }
995 GNUNET_break (0);
991 return GNUNET_SYSERR; 996 return GNUNET_SYSERR;
992} 997}
993 998
@@ -1005,12 +1010,12 @@ GNUNET_CONTAINER_multihashmap_get_random(
1005 * @return an iterator over the given multihashmap 'map' 1010 * @return an iterator over the given multihashmap 'map'
1006 */ 1011 */
1007struct GNUNET_CONTAINER_MultiHashMapIterator * 1012struct GNUNET_CONTAINER_MultiHashMapIterator *
1008GNUNET_CONTAINER_multihashmap_iterator_create( 1013GNUNET_CONTAINER_multihashmap_iterator_create (
1009 const struct GNUNET_CONTAINER_MultiHashMap *map) 1014 const struct GNUNET_CONTAINER_MultiHashMap *map)
1010{ 1015{
1011 struct GNUNET_CONTAINER_MultiHashMapIterator *iter; 1016 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
1012 1017
1013 iter = GNUNET_new(struct GNUNET_CONTAINER_MultiHashMapIterator); 1018 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
1014 iter->map = map; 1019 iter->map = map;
1015 iter->modification_counter = map->modification_counter; 1020 iter->modification_counter = map->modification_counter;
1016 iter->me = map->map[0]; 1021 iter->me = map->map[0];
@@ -1033,47 +1038,47 @@ GNUNET_CONTAINER_multihashmap_iterator_create(
1033 * #GNUNET_NO if we are out of elements 1038 * #GNUNET_NO if we are out of elements
1034 */ 1039 */
1035int 1040int
1036GNUNET_CONTAINER_multihashmap_iterator_next( 1041GNUNET_CONTAINER_multihashmap_iterator_next (
1037 struct GNUNET_CONTAINER_MultiHashMapIterator *iter, 1042 struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
1038 struct GNUNET_HashCode *key, 1043 struct GNUNET_HashCode *key,
1039 const void **value) 1044 const void **value)
1040{ 1045{
1041 /* make sure the map has not been modified */ 1046 /* make sure the map has not been modified */
1042 GNUNET_assert(iter->modification_counter == iter->map->modification_counter); 1047 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1043 1048
1044 /* look for the next entry, skipping empty buckets */ 1049 /* look for the next entry, skipping empty buckets */
1045 while (1) 1050 while (1)
1051 {
1052 if (iter->idx >= iter->map->map_length)
1053 return GNUNET_NO;
1054 if (GNUNET_YES == iter->map->use_small_entries)
1046 { 1055 {
1047 if (iter->idx >= iter->map->map_length) 1056 if (NULL != iter->me.sme)
1048 return GNUNET_NO; 1057 {
1049 if (GNUNET_YES == iter->map->use_small_entries) 1058 if (NULL != key)
1050 { 1059 *key = *iter->me.sme->key;
1051 if (NULL != iter->me.sme) 1060 if (NULL != value)
1052 { 1061 *value = iter->me.sme->value;
1053 if (NULL != key) 1062 iter->me.sme = iter->me.sme->next;
1054 *key = *iter->me.sme->key; 1063 return GNUNET_YES;
1055 if (NULL != value) 1064 }
1056 *value = iter->me.sme->value; 1065 }
1057 iter->me.sme = iter->me.sme->next; 1066 else
1058 return GNUNET_YES; 1067 {
1059 } 1068 if (NULL != iter->me.bme)
1060 } 1069 {
1061 else 1070 if (NULL != key)
1062 { 1071 *key = iter->me.bme->key;
1063 if (NULL != iter->me.bme) 1072 if (NULL != value)
1064 { 1073 *value = iter->me.bme->value;
1065 if (NULL != key) 1074 iter->me.bme = iter->me.bme->next;
1066 *key = iter->me.bme->key; 1075 return GNUNET_YES;
1067 if (NULL != value) 1076 }
1068 *value = iter->me.bme->value;
1069 iter->me.bme = iter->me.bme->next;
1070 return GNUNET_YES;
1071 }
1072 }
1073 iter->idx += 1;
1074 if (iter->idx < iter->map->map_length)
1075 iter->me = iter->map->map[iter->idx];
1076 } 1077 }
1078 iter->idx += 1;
1079 if (iter->idx < iter->map->map_length)
1080 iter->me = iter->map->map[iter->idx];
1081 }
1077} 1082}
1078 1083
1079 1084
@@ -1083,10 +1088,10 @@ GNUNET_CONTAINER_multihashmap_iterator_next(
1083 * @param iter the iterator to destroy 1088 * @param iter the iterator to destroy
1084 */ 1089 */
1085void 1090void
1086GNUNET_CONTAINER_multihashmap_iterator_destroy( 1091GNUNET_CONTAINER_multihashmap_iterator_destroy (
1087 struct GNUNET_CONTAINER_MultiHashMapIterator *iter) 1092 struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
1088{ 1093{
1089 GNUNET_free(iter); 1094 GNUNET_free (iter);
1090} 1095}
1091 1096
1092 1097
diff --git a/src/util/container_multihashmap32.c b/src/util/container_multihashmap32.c
index 82d908555..f349a5f80 100644
--- a/src/util/container_multihashmap32.c
+++ b/src/util/container_multihashmap32.c
@@ -29,7 +29,7 @@
29#include "gnunet_container_lib.h" 29#include "gnunet_container_lib.h"
30 30
31#define LOG(kind, ...) \ 31#define LOG(kind, ...) \
32 GNUNET_log_from(kind, "util-container-multihashmap32", __VA_ARGS__) 32 GNUNET_log_from (kind, "util-container-multihashmap32", __VA_ARGS__)
33 33
34 34
35/** 35/**
@@ -43,7 +43,8 @@
43/** 43/**
44 * An entry in the hash map. 44 * An entry in the hash map.
45 */ 45 */
46struct MapEntry { 46struct MapEntry
47{
47 /** 48 /**
48 * Key for the entry. 49 * Key for the entry.
49 */ 50 */
@@ -63,7 +64,8 @@ struct MapEntry {
63/** 64/**
64 * Internal representation of the hash map. 65 * Internal representation of the hash map.
65 */ 66 */
66struct GNUNET_CONTAINER_MultiHashMap32 { 67struct GNUNET_CONTAINER_MultiHashMap32
68{
67 /** 69 /**
68 * All of our buckets. 70 * All of our buckets.
69 */ 71 */
@@ -104,7 +106,8 @@ struct GNUNET_CONTAINER_MultiHashMap32 {
104 * Cursor into a multihashmap. 106 * Cursor into a multihashmap.
105 * Allows to enumerate elements asynchronously. 107 * Allows to enumerate elements asynchronously.
106 */ 108 */
107struct GNUNET_CONTAINER_MultiHashMap32Iterator { 109struct GNUNET_CONTAINER_MultiHashMap32Iterator
110{
108 /** 111 /**
109 * Position in the bucket @e idx 112 * Position in the bucket @e idx
110 */ 113 */
@@ -135,18 +138,18 @@ struct GNUNET_CONTAINER_MultiHashMap32Iterator {
135 * @return NULL on error 138 * @return NULL on error
136 */ 139 */
137struct GNUNET_CONTAINER_MultiHashMap32 * 140struct GNUNET_CONTAINER_MultiHashMap32 *
138GNUNET_CONTAINER_multihashmap32_create(unsigned int len) 141GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
139{ 142{
140 struct GNUNET_CONTAINER_MultiHashMap32 *ret; 143 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
141 144
142 GNUNET_assert(len > 0); 145 GNUNET_assert (len > 0);
143 ret = GNUNET_new(struct GNUNET_CONTAINER_MultiHashMap32); 146 ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
144 ret->map = GNUNET_malloc_large(len * sizeof(struct MapEntry *)); 147 ret->map = GNUNET_malloc_large (len * sizeof(struct MapEntry *));
145 if (NULL == ret->map) 148 if (NULL == ret->map)
146 { 149 {
147 GNUNET_free(ret); 150 GNUNET_free (ret);
148 return NULL; 151 return NULL;
149 } 152 }
150 ret->map_length = len; 153 ret->map_length = len;
151 return ret; 154 return ret;
152} 155}
@@ -159,21 +162,21 @@ GNUNET_CONTAINER_multihashmap32_create(unsigned int len)
159 * @param map the map 162 * @param map the map
160 */ 163 */
161void 164void
162GNUNET_CONTAINER_multihashmap32_destroy( 165GNUNET_CONTAINER_multihashmap32_destroy (
163 struct GNUNET_CONTAINER_MultiHashMap32 *map) 166 struct GNUNET_CONTAINER_MultiHashMap32 *map)
164{ 167{
165 struct MapEntry *e; 168 struct MapEntry *e;
166 169
167 for (unsigned int i = 0; i < map->map_length; i++) 170 for (unsigned int i = 0; i < map->map_length; i++)
171 {
172 while (NULL != (e = map->map[i]))
168 { 173 {
169 while (NULL != (e = map->map[i])) 174 map->map[i] = e->next;
170 { 175 GNUNET_free (e);
171 map->map[i] = e->next;
172 GNUNET_free(e);
173 }
174 } 176 }
175 GNUNET_free(map->map); 177 }
176 GNUNET_free(map); 178 GNUNET_free (map->map);
179 GNUNET_free (map);
177} 180}
178 181
179 182
@@ -185,10 +188,10 @@ GNUNET_CONTAINER_multihashmap32_destroy(
185 * @return offset into the "map" array of "m" 188 * @return offset into the "map" array of "m"
186 */ 189 */
187static unsigned int 190static unsigned int
188idx_of(const struct GNUNET_CONTAINER_MultiHashMap32 *m, const uint32_t key) 191idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m, const uint32_t key)
189{ 192{
190 GNUNET_assert(NULL != m); 193 GNUNET_assert (NULL != m);
191 return ((unsigned int)key) % m->map_length; 194 return ((unsigned int) key) % m->map_length;
192} 195}
193 196
194 197
@@ -199,7 +202,7 @@ idx_of(const struct GNUNET_CONTAINER_MultiHashMap32 *m, const uint32_t key)
199 * @return the number of key value pairs 202 * @return the number of key value pairs
200 */ 203 */
201unsigned int 204unsigned int
202GNUNET_CONTAINER_multihashmap32_size( 205GNUNET_CONTAINER_multihashmap32_size (
203 const struct GNUNET_CONTAINER_MultiHashMap32 *map) 206 const struct GNUNET_CONTAINER_MultiHashMap32 *map)
204{ 207{
205 return map->size; 208 return map->size;
@@ -217,19 +220,19 @@ GNUNET_CONTAINER_multihashmap32_size(
217 * key-value pairs with value NULL 220 * key-value pairs with value NULL
218 */ 221 */
219void * 222void *
220GNUNET_CONTAINER_multihashmap32_get( 223GNUNET_CONTAINER_multihashmap32_get (
221 const struct GNUNET_CONTAINER_MultiHashMap32 *map, 224 const struct GNUNET_CONTAINER_MultiHashMap32 *map,
222 uint32_t key) 225 uint32_t key)
223{ 226{
224 struct MapEntry *e; 227 struct MapEntry *e;
225 228
226 e = map->map[idx_of(map, key)]; 229 e = map->map[idx_of (map, key)];
227 while (NULL != e) 230 while (NULL != e)
228 { 231 {
229 if (key == e->key) 232 if (key == e->key)
230 return e->value; 233 return e->value;
231 e = e->next; 234 e = e->next;
232 } 235 }
233 return NULL; 236 return NULL;
234} 237}
235 238
@@ -244,7 +247,7 @@ GNUNET_CONTAINER_multihashmap32_get(
244 * #GNUNET_SYSERR if it aborted iteration 247 * #GNUNET_SYSERR if it aborted iteration
245 */ 248 */
246int 249int
247GNUNET_CONTAINER_multihashmap32_iterate( 250GNUNET_CONTAINER_multihashmap32_iterate (
248 struct GNUNET_CONTAINER_MultiHashMap32 *map, 251 struct GNUNET_CONTAINER_MultiHashMap32 *map,
249 GNUNET_CONTAINER_MulitHashMapIterator32Callback it, 252 GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
250 void *it_cls) 253 void *it_cls)
@@ -253,29 +256,29 @@ GNUNET_CONTAINER_multihashmap32_iterate(
253 struct MapEntry **ce; 256 struct MapEntry **ce;
254 257
255 count = 0; 258 count = 0;
256 GNUNET_assert(NULL != map); 259 GNUNET_assert (NULL != map);
257 ce = &map->next_cache[map->next_cache_off]; 260 ce = &map->next_cache[map->next_cache_off];
258 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 261 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
259 for (unsigned int i = 0; i < map->map_length; i++) 262 for (unsigned int i = 0; i < map->map_length; i++)
260 { 263 {
261 struct MapEntry *e; 264 struct MapEntry *e;
262 265
263 *ce = map->map[i]; 266 *ce = map->map[i];
264 while (NULL != (e = *ce)) 267 while (NULL != (e = *ce))
268 {
269 *ce = e->next;
270 if (NULL != it)
271 {
272 if (GNUNET_OK != it (it_cls, e->key, e->value))
265 { 273 {
266 *ce = e->next; 274 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
267 if (NULL != it) 275 return GNUNET_SYSERR;
268 {
269 if (GNUNET_OK != it(it_cls, e->key, e->value))
270 {
271 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
272 return GNUNET_SYSERR;
273 }
274 }
275 count++;
276 } 276 }
277 }
278 count++;
277 } 279 }
278 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 280 }
281 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
279 return count; 282 return count;
280} 283}
281 284
@@ -288,8 +291,8 @@ GNUNET_CONTAINER_multihashmap32_iterate(
288 * @param bme the entry that is about to be free'd 291 * @param bme the entry that is about to be free'd
289 */ 292 */
290static void 293static void
291update_next_cache(struct GNUNET_CONTAINER_MultiHashMap32 *map, 294update_next_cache (struct GNUNET_CONTAINER_MultiHashMap32 *map,
292 const struct MapEntry *me) 295 const struct MapEntry *me)
293{ 296{
294 for (unsigned int i = 0; i < map->next_cache_off; i++) 297 for (unsigned int i = 0; i < map->next_cache_off; i++)
295 if (map->next_cache[i] == me) 298 if (map->next_cache[i] == me)
@@ -309,7 +312,7 @@ update_next_cache(struct GNUNET_CONTAINER_MultiHashMap32 *map,
309 * is not in the map 312 * is not in the map
310 */ 313 */
311int 314int
312GNUNET_CONTAINER_multihashmap32_remove( 315GNUNET_CONTAINER_multihashmap32_remove (
313 struct GNUNET_CONTAINER_MultiHashMap32 *map, 316 struct GNUNET_CONTAINER_MultiHashMap32 *map,
314 uint32_t key, 317 uint32_t key,
315 const void *value) 318 const void *value)
@@ -320,25 +323,25 @@ GNUNET_CONTAINER_multihashmap32_remove(
320 323
321 map->modification_counter++; 324 map->modification_counter++;
322 325
323 i = idx_of(map, key); 326 i = idx_of (map, key);
324 p = NULL; 327 p = NULL;
325 e = map->map[i]; 328 e = map->map[i];
326 while (e != NULL) 329 while (e != NULL)
330 {
331 if ((key == e->key) && (value == e->value))
327 { 332 {
328 if ((key == e->key) && (value == e->value)) 333 if (p == NULL)
329 { 334 map->map[i] = e->next;
330 if (p == NULL) 335 else
331 map->map[i] = e->next; 336 p->next = e->next;
332 else 337 update_next_cache (map, e);
333 p->next = e->next; 338 GNUNET_free (e);
334 update_next_cache(map, e); 339 map->size--;
335 GNUNET_free(e); 340 return GNUNET_YES;
336 map->size--;
337 return GNUNET_YES;
338 }
339 p = e;
340 e = e->next;
341 } 341 }
342 p = e;
343 e = e->next;
344 }
342 return GNUNET_NO; 345 return GNUNET_NO;
343} 346}
344 347
@@ -352,7 +355,7 @@ GNUNET_CONTAINER_multihashmap32_remove(
352 * @return number of values removed 355 * @return number of values removed
353 */ 356 */
354int 357int
355GNUNET_CONTAINER_multihashmap32_remove_all( 358GNUNET_CONTAINER_multihashmap32_remove_all (
356 struct GNUNET_CONTAINER_MultiHashMap32 *map, 359 struct GNUNET_CONTAINER_MultiHashMap32 *map,
357 uint32_t key) 360 uint32_t key)
358{ 361{
@@ -364,32 +367,32 @@ GNUNET_CONTAINER_multihashmap32_remove_all(
364 map->modification_counter++; 367 map->modification_counter++;
365 368
366 ret = 0; 369 ret = 0;
367 i = idx_of(map, key); 370 i = idx_of (map, key);
368 p = NULL; 371 p = NULL;
369 e = map->map[i]; 372 e = map->map[i];
370 while (e != NULL) 373 while (e != NULL)
374 {
375 if (key == e->key)
371 { 376 {
372 if (key == e->key) 377 if (p == NULL)
373 { 378 map->map[i] = e->next;
374 if (p == NULL)
375 map->map[i] = e->next;
376 else
377 p->next = e->next;
378 update_next_cache(map, e);
379 GNUNET_free(e);
380 map->size--;
381 if (p == NULL)
382 e = map->map[i];
383 else
384 e = p->next;
385 ret++;
386 }
387 else 379 else
388 { 380 p->next = e->next;
389 p = e; 381 update_next_cache (map, e);
390 e = e->next; 382 GNUNET_free (e);
391 } 383 map->size--;
384 if (p == NULL)
385 e = map->map[i];
386 else
387 e = p->next;
388 ret++;
389 }
390 else
391 {
392 p = e;
393 e = e->next;
392 } 394 }
395 }
393 return ret; 396 return ret;
394} 397}
395 398
@@ -404,19 +407,19 @@ GNUNET_CONTAINER_multihashmap32_remove_all(
404 * #GNUNET_NO if not 407 * #GNUNET_NO if not
405 */ 408 */
406int 409int
407GNUNET_CONTAINER_multihashmap32_contains( 410GNUNET_CONTAINER_multihashmap32_contains (
408 const struct GNUNET_CONTAINER_MultiHashMap32 *map, 411 const struct GNUNET_CONTAINER_MultiHashMap32 *map,
409 uint32_t key) 412 uint32_t key)
410{ 413{
411 struct MapEntry *e; 414 struct MapEntry *e;
412 415
413 e = map->map[idx_of(map, key)]; 416 e = map->map[idx_of (map, key)];
414 while (e != NULL) 417 while (e != NULL)
415 { 418 {
416 if (key == e->key) 419 if (key == e->key)
417 return GNUNET_YES; 420 return GNUNET_YES;
418 e = e->next; 421 e = e->next;
419 } 422 }
420 return GNUNET_NO; 423 return GNUNET_NO;
421} 424}
422 425
@@ -432,20 +435,20 @@ GNUNET_CONTAINER_multihashmap32_contains(
432 * #GNUNET_NO if not 435 * #GNUNET_NO if not
433 */ 436 */
434int 437int
435GNUNET_CONTAINER_multihashmap32_contains_value( 438GNUNET_CONTAINER_multihashmap32_contains_value (
436 const struct GNUNET_CONTAINER_MultiHashMap32 *map, 439 const struct GNUNET_CONTAINER_MultiHashMap32 *map,
437 uint32_t key, 440 uint32_t key,
438 const void *value) 441 const void *value)
439{ 442{
440 struct MapEntry *e; 443 struct MapEntry *e;
441 444
442 e = map->map[idx_of(map, key)]; 445 e = map->map[idx_of (map, key)];
443 while (e != NULL) 446 while (e != NULL)
444 { 447 {
445 if ((key == e->key) && (e->value == value)) 448 if ((key == e->key) && (e->value == value))
446 return GNUNET_YES; 449 return GNUNET_YES;
447 e = e->next; 450 e = e->next;
448 } 451 }
449 return GNUNET_NO; 452 return GNUNET_NO;
450} 453}
451 454
@@ -456,7 +459,7 @@ GNUNET_CONTAINER_multihashmap32_contains_value(
456 * @param map the hash map to grow 459 * @param map the hash map to grow
457 */ 460 */
458static void 461static void
459grow(struct GNUNET_CONTAINER_MultiHashMap32 *map) 462grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
460{ 463{
461 struct MapEntry **old_map; 464 struct MapEntry **old_map;
462 struct MapEntry **new_map; 465 struct MapEntry **new_map;
@@ -472,23 +475,23 @@ grow(struct GNUNET_CONTAINER_MultiHashMap32 *map)
472 new_len = old_len; /* never use 0 */ 475 new_len = old_len; /* never use 0 */
473 if (new_len == old_len) 476 if (new_len == old_len)
474 return; /* nothing changed */ 477 return; /* nothing changed */
475 new_map = GNUNET_malloc_large(new_len * sizeof(struct MapEntry *)); 478 new_map = GNUNET_malloc_large (new_len * sizeof(struct MapEntry *));
476 if (NULL == new_map) 479 if (NULL == new_map)
477 return; /* grow not possible */ 480 return; /* grow not possible */
478 map->modification_counter++; 481 map->modification_counter++;
479 map->map_length = new_len; 482 map->map_length = new_len;
480 map->map = new_map; 483 map->map = new_map;
481 for (unsigned int i = 0; i < old_len; i++) 484 for (unsigned int i = 0; i < old_len; i++)
485 {
486 while (NULL != (e = old_map[i]))
482 { 487 {
483 while (NULL != (e = old_map[i])) 488 old_map[i] = e->next;
484 { 489 idx = idx_of (map, e->key);
485 old_map[i] = e->next; 490 e->next = new_map[idx];
486 idx = idx_of(map, e->key); 491 new_map[idx] = e;
487 e->next = new_map[idx];
488 new_map[idx] = e;
489 }
490 } 492 }
491 GNUNET_free(old_map); 493 }
494 GNUNET_free (old_map);
492} 495}
493 496
494 497
@@ -505,7 +508,7 @@ grow(struct GNUNET_CONTAINER_MultiHashMap32 *map)
505 * value already exists 508 * value already exists
506 */ 509 */
507int 510int
508GNUNET_CONTAINER_multihashmap32_put( 511GNUNET_CONTAINER_multihashmap32_put (
509 struct GNUNET_CONTAINER_MultiHashMap32 *map, 512 struct GNUNET_CONTAINER_MultiHashMap32 *map,
510 uint32_t key, 513 uint32_t key,
511 void *value, 514 void *value,
@@ -514,29 +517,29 @@ GNUNET_CONTAINER_multihashmap32_put(
514 struct MapEntry *e; 517 struct MapEntry *e;
515 unsigned int i; 518 unsigned int i;
516 519
517 i = idx_of(map, key); 520 i = idx_of (map, key);
518 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 521 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
519 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 522 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
523 {
524 e = map->map[i];
525 while (e != NULL)
520 { 526 {
521 e = map->map[i]; 527 if (key == e->key)
522 while (e != NULL) 528 {
523 { 529 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
524 if (key == e->key) 530 return GNUNET_SYSERR;
525 { 531 e->value = value;
526 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 532 return GNUNET_NO;
527 return GNUNET_SYSERR; 533 }
528 e->value = value; 534 e = e->next;
529 return GNUNET_NO;
530 }
531 e = e->next;
532 }
533 } 535 }
536 }
534 if (map->size / 3 >= map->map_length / 4) 537 if (map->size / 3 >= map->map_length / 4)
535 { 538 {
536 grow(map); 539 grow (map);
537 i = idx_of(map, key); 540 i = idx_of (map, key);
538 } 541 }
539 e = GNUNET_new(struct MapEntry); 542 e = GNUNET_new (struct MapEntry);
540 e->key = key; 543 e->key = key;
541 e->value = value; 544 e->value = value;
542 e->next = map->map[i]; 545 e->next = map->map[i];
@@ -557,7 +560,7 @@ GNUNET_CONTAINER_multihashmap32_put(
557 * GNUNET_SYSERR if it aborted iteration 560 * GNUNET_SYSERR if it aborted iteration
558 */ 561 */
559int 562int
560GNUNET_CONTAINER_multihashmap32_get_multiple( 563GNUNET_CONTAINER_multihashmap32_get_multiple (
561 struct GNUNET_CONTAINER_MultiHashMap32 *map, 564 struct GNUNET_CONTAINER_MultiHashMap32 *map,
562 uint32_t key, 565 uint32_t key,
563 GNUNET_CONTAINER_MulitHashMapIterator32Callback it, 566 GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
@@ -569,22 +572,22 @@ GNUNET_CONTAINER_multihashmap32_get_multiple(
569 572
570 count = 0; 573 count = 0;
571 ce = &map->next_cache[map->next_cache_off]; 574 ce = &map->next_cache[map->next_cache_off];
572 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 575 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
573 576
574 *ce = map->map[idx_of(map, key)]; 577 *ce = map->map[idx_of (map, key)];
575 while (NULL != (e = *ce)) 578 while (NULL != (e = *ce))
579 {
580 *ce = e->next;
581 if (key != e->key)
582 continue;
583 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, e->value)))
576 { 584 {
577 *ce = e->next; 585 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
578 if (key != e->key) 586 return GNUNET_SYSERR;
579 continue;
580 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, e->value)))
581 {
582 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
583 return GNUNET_SYSERR;
584 }
585 count++;
586 } 587 }
587 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 588 count++;
589 }
590 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
588 return count; 591 return count;
589} 592}
590 593
@@ -602,12 +605,12 @@ GNUNET_CONTAINER_multihashmap32_get_multiple(
602 * @return an iterator over the given multihashmap @a map 605 * @return an iterator over the given multihashmap @a map
603 */ 606 */
604struct GNUNET_CONTAINER_MultiHashMap32Iterator * 607struct GNUNET_CONTAINER_MultiHashMap32Iterator *
605GNUNET_CONTAINER_multihashmap32_iterator_create( 608GNUNET_CONTAINER_multihashmap32_iterator_create (
606 const struct GNUNET_CONTAINER_MultiHashMap32 *map) 609 const struct GNUNET_CONTAINER_MultiHashMap32 *map)
607{ 610{
608 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter; 611 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
609 612
610 iter = GNUNET_new(struct GNUNET_CONTAINER_MultiHashMap32Iterator); 613 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
611 iter->map = map; 614 iter->map = map;
612 iter->modification_counter = map->modification_counter; 615 iter->modification_counter = map->modification_counter;
613 iter->me = map->map[0]; 616 iter->me = map->map[0];
@@ -630,32 +633,32 @@ GNUNET_CONTAINER_multihashmap32_iterator_create(
630 * #GNUNET_NO if we are out of elements 633 * #GNUNET_NO if we are out of elements
631 */ 634 */
632int 635int
633GNUNET_CONTAINER_multihashmap32_iterator_next( 636GNUNET_CONTAINER_multihashmap32_iterator_next (
634 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter, 637 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
635 uint32_t *key, 638 uint32_t *key,
636 const void **value) 639 const void **value)
637{ 640{
638 /* make sure the map has not been modified */ 641 /* make sure the map has not been modified */
639 GNUNET_assert(iter->modification_counter == iter->map->modification_counter); 642 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
640 643
641 /* look for the next entry, skipping empty buckets */ 644 /* look for the next entry, skipping empty buckets */
642 while (1) 645 while (1)
646 {
647 if (iter->idx >= iter->map->map_length)
648 return GNUNET_NO;
649 if (NULL != iter->me)
643 { 650 {
644 if (iter->idx >= iter->map->map_length) 651 if (NULL != key)
645 return GNUNET_NO; 652 *key = iter->me->key;
646 if (NULL != iter->me) 653 if (NULL != value)
647 { 654 *value = iter->me->value;
648 if (NULL != key) 655 iter->me = iter->me->next;
649 *key = iter->me->key; 656 return GNUNET_YES;
650 if (NULL != value)
651 *value = iter->me->value;
652 iter->me = iter->me->next;
653 return GNUNET_YES;
654 }
655 iter->idx += 1;
656 if (iter->idx < iter->map->map_length)
657 iter->me = iter->map->map[iter->idx];
658 } 657 }
658 iter->idx += 1;
659 if (iter->idx < iter->map->map_length)
660 iter->me = iter->map->map[iter->idx];
661 }
659} 662}
660 663
661 664
@@ -665,10 +668,10 @@ GNUNET_CONTAINER_multihashmap32_iterator_next(
665 * @param iter the iterator to destroy 668 * @param iter the iterator to destroy
666 */ 669 */
667void 670void
668GNUNET_CONTAINER_multihashmap32_iterator_destroy( 671GNUNET_CONTAINER_multihashmap32_iterator_destroy (
669 struct GNUNET_CONTAINER_MultiHashMapIterator *iter) 672 struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
670{ 673{
671 GNUNET_free(iter); 674 GNUNET_free (iter);
672} 675}
673 676
674 677
diff --git a/src/util/container_multipeermap.c b/src/util/container_multipeermap.c
index 0cd56b9fc..2c0b2090d 100644
--- a/src/util/container_multipeermap.c
+++ b/src/util/container_multipeermap.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29#define LOG(kind, ...) \ 29#define LOG(kind, ...) \
30 GNUNET_log_from(kind, "util-container-multipeermap", __VA_ARGS__) 30 GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__)
31 31
32/** 32/**
33 * Maximum recursion depth for callbacks of 33 * Maximum recursion depth for callbacks of
@@ -40,7 +40,8 @@
40/** 40/**
41 * An entry in the hash map with the full key. 41 * An entry in the hash map with the full key.
42 */ 42 */
43struct BigMapEntry { 43struct BigMapEntry
44{
44 /** 45 /**
45 * Value of the entry. 46 * Value of the entry.
46 */ 47 */
@@ -61,7 +62,8 @@ struct BigMapEntry {
61/** 62/**
62 * An entry in the hash map with just a pointer to the key. 63 * An entry in the hash map with just a pointer to the key.
63 */ 64 */
64struct SmallMapEntry { 65struct SmallMapEntry
66{
65 /** 67 /**
66 * Value of the entry. 68 * Value of the entry.
67 */ 69 */
@@ -82,7 +84,8 @@ struct SmallMapEntry {
82/** 84/**
83 * Entry in the map. 85 * Entry in the map.
84 */ 86 */
85union MapEntry { 87union MapEntry
88{
86 /** 89 /**
87 * Variant used if map entries only contain a pointer to the key. 90 * Variant used if map entries only contain a pointer to the key.
88 */ 91 */
@@ -98,7 +101,8 @@ union MapEntry {
98/** 101/**
99 * Internal representation of the hash map. 102 * Internal representation of the hash map.
100 */ 103 */
101struct GNUNET_CONTAINER_MultiPeerMap { 104struct GNUNET_CONTAINER_MultiPeerMap
105{
102 /** 106 /**
103 * All of our buckets. 107 * All of our buckets.
104 */ 108 */
@@ -145,7 +149,8 @@ struct GNUNET_CONTAINER_MultiPeerMap {
145 * Cursor into a multipeermap. 149 * Cursor into a multipeermap.
146 * Allows to enumerate elements asynchronously. 150 * Allows to enumerate elements asynchronously.
147 */ 151 */
148struct GNUNET_CONTAINER_MultiPeerMapIterator { 152struct GNUNET_CONTAINER_MultiPeerMapIterator
153{
149 /** 154 /**
150 * Position in the bucket 'idx' 155 * Position in the bucket 'idx'
151 */ 156 */
@@ -185,18 +190,18 @@ struct GNUNET_CONTAINER_MultiPeerMapIterator {
185 * @return NULL on error 190 * @return NULL on error
186 */ 191 */
187struct GNUNET_CONTAINER_MultiPeerMap * 192struct GNUNET_CONTAINER_MultiPeerMap *
188GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys) 193GNUNET_CONTAINER_multipeermap_create (unsigned int len, int do_not_copy_keys)
189{ 194{
190 struct GNUNET_CONTAINER_MultiPeerMap *map; 195 struct GNUNET_CONTAINER_MultiPeerMap *map;
191 196
192 GNUNET_assert(len > 0); 197 GNUNET_assert (len > 0);
193 map = GNUNET_new(struct GNUNET_CONTAINER_MultiPeerMap); 198 map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
194 map->map = GNUNET_malloc_large(len * sizeof(union MapEntry)); 199 map->map = GNUNET_malloc_large (len * sizeof(union MapEntry));
195 if (NULL == map->map) 200 if (NULL == map->map)
196 { 201 {
197 GNUNET_free(map); 202 GNUNET_free (map);
198 return NULL; 203 return NULL;
199 } 204 }
200 map->map_length = len; 205 map->map_length = len;
201 map->use_small_entries = do_not_copy_keys; 206 map->use_small_entries = do_not_copy_keys;
202 return map; 207 return map;
@@ -210,44 +215,44 @@ GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
210 * @param map the map 215 * @param map the map
211 */ 216 */
212void 217void
213GNUNET_CONTAINER_multipeermap_destroy( 218GNUNET_CONTAINER_multipeermap_destroy (
214 struct GNUNET_CONTAINER_MultiPeerMap *map) 219 struct GNUNET_CONTAINER_MultiPeerMap *map)
215{ 220{
216 GNUNET_assert(0 == map->next_cache_off); 221 GNUNET_assert (0 == map->next_cache_off);
217 for (unsigned int i = 0; i < map->map_length; i++) 222 for (unsigned int i = 0; i < map->map_length; i++)
218 { 223 {
219 union MapEntry me; 224 union MapEntry me;
220 225
221 me = map->map[i]; 226 me = map->map[i];
222 if (map->use_small_entries) 227 if (map->use_small_entries)
223 { 228 {
224 struct SmallMapEntry *sme; 229 struct SmallMapEntry *sme;
225 struct SmallMapEntry *nxt; 230 struct SmallMapEntry *nxt;
226 231
227 nxt = me.sme; 232 nxt = me.sme;
228 while (NULL != (sme = nxt)) 233 while (NULL != (sme = nxt))
229 { 234 {
230 nxt = sme->next; 235 nxt = sme->next;
231 GNUNET_free(sme); 236 GNUNET_free (sme);
232 } 237 }
233 me.sme = NULL; 238 me.sme = NULL;
234 }
235 else
236 {
237 struct BigMapEntry *bme;
238 struct BigMapEntry *nxt;
239
240 nxt = me.bme;
241 while (NULL != (bme = nxt))
242 {
243 nxt = bme->next;
244 GNUNET_free(bme);
245 }
246 me.bme = NULL;
247 }
248 } 239 }
249 GNUNET_free(map->map); 240 else
250 GNUNET_free(map); 241 {
242 struct BigMapEntry *bme;
243 struct BigMapEntry *nxt;
244
245 nxt = me.bme;
246 while (NULL != (bme = nxt))
247 {
248 nxt = bme->next;
249 GNUNET_free (bme);
250 }
251 me.bme = NULL;
252 }
253 }
254 GNUNET_free (map->map);
255 GNUNET_free (map);
251} 256}
252 257
253 258
@@ -259,13 +264,13 @@ GNUNET_CONTAINER_multipeermap_destroy(
259 * @return offset into the "map" array of "map" 264 * @return offset into the "map" array of "map"
260 */ 265 */
261static unsigned int 266static unsigned int
262idx_of(const struct GNUNET_CONTAINER_MultiPeerMap *map, 267idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
263 const struct GNUNET_PeerIdentity *key) 268 const struct GNUNET_PeerIdentity *key)
264{ 269{
265 unsigned int kx; 270 unsigned int kx;
266 271
267 GNUNET_assert(NULL != map); 272 GNUNET_assert (NULL != map);
268 GNUNET_memcpy(&kx, key, sizeof(kx)); 273 GNUNET_memcpy (&kx, key, sizeof(kx));
269 return kx % map->map_length; 274 return kx % map->map_length;
270} 275}
271 276
@@ -277,7 +282,7 @@ idx_of(const struct GNUNET_CONTAINER_MultiPeerMap *map,
277 * @return the number of key value pairs 282 * @return the number of key value pairs
278 */ 283 */
279unsigned int 284unsigned int
280GNUNET_CONTAINER_multipeermap_size( 285GNUNET_CONTAINER_multipeermap_size (
281 const struct GNUNET_CONTAINER_MultiPeerMap *map) 286 const struct GNUNET_CONTAINER_MultiPeerMap *map)
282{ 287{
283 return map->size; 288 return map->size;
@@ -295,25 +300,25 @@ GNUNET_CONTAINER_multipeermap_size(
295 * key-value pairs with value NULL 300 * key-value pairs with value NULL
296 */ 301 */
297void * 302void *
298GNUNET_CONTAINER_multipeermap_get( 303GNUNET_CONTAINER_multipeermap_get (
299 const struct GNUNET_CONTAINER_MultiPeerMap *map, 304 const struct GNUNET_CONTAINER_MultiPeerMap *map,
300 const struct GNUNET_PeerIdentity *key) 305 const struct GNUNET_PeerIdentity *key)
301{ 306{
302 union MapEntry me; 307 union MapEntry me;
303 308
304 me = map->map[idx_of(map, key)]; 309 me = map->map[idx_of (map, key)];
305 if (map->use_small_entries) 310 if (map->use_small_entries)
306 { 311 {
307 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 312 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
308 if (0 == GNUNET_memcmp(key, sme->key)) 313 if (0 == GNUNET_memcmp (key, sme->key))
309 return sme->value; 314 return sme->value;
310 } 315 }
311 else 316 else
312 { 317 {
313 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 318 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
314 if (0 == GNUNET_memcmp(key, &bme->key)) 319 if (0 == GNUNET_memcmp (key, &bme->key))
315 return bme->value; 320 return bme->value;
316 } 321 }
317 return NULL; 322 return NULL;
318} 323}
319 324
@@ -328,7 +333,7 @@ GNUNET_CONTAINER_multipeermap_get(
328 * #GNUNET_SYSERR if it aborted iteration 333 * #GNUNET_SYSERR if it aborted iteration
329 */ 334 */
330int 335int
331GNUNET_CONTAINER_multipeermap_iterate( 336GNUNET_CONTAINER_multipeermap_iterate (
332 struct GNUNET_CONTAINER_MultiPeerMap *map, 337 struct GNUNET_CONTAINER_MultiPeerMap *map,
333 GNUNET_CONTAINER_PeerMapIterator it, 338 GNUNET_CONTAINER_PeerMapIterator it,
334 void *it_cls) 339 void *it_cls)
@@ -339,53 +344,53 @@ GNUNET_CONTAINER_multipeermap_iterate(
339 struct GNUNET_PeerIdentity kc; 344 struct GNUNET_PeerIdentity kc;
340 345
341 count = 0; 346 count = 0;
342 GNUNET_assert(NULL != map); 347 GNUNET_assert (NULL != map);
343 ce = &map->next_cache[map->next_cache_off]; 348 ce = &map->next_cache[map->next_cache_off];
344 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 349 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
345 for (unsigned int i = 0; i < map->map_length; i++) 350 for (unsigned int i = 0; i < map->map_length; i++)
351 {
352 me = map->map[i];
353 if (map->use_small_entries)
346 { 354 {
347 me = map->map[i]; 355 struct SmallMapEntry *sme;
348 if (map->use_small_entries) 356
357 ce->sme = me.sme;
358 while (NULL != (sme = ce->sme))
359 {
360 ce->sme = sme->next;
361 if (NULL != it)
349 { 362 {
350 struct SmallMapEntry *sme; 363 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
351 364 {
352 ce->sme = me.sme; 365 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
353 while (NULL != (sme = ce->sme)) 366 return GNUNET_SYSERR;
354 { 367 }
355 ce->sme = sme->next;
356 if (NULL != it)
357 {
358 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
359 {
360 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
361 return GNUNET_SYSERR;
362 }
363 }
364 count++;
365 }
366 } 368 }
367 else 369 count++;
370 }
371 }
372 else
373 {
374 struct BigMapEntry *bme;
375
376 ce->bme = me.bme;
377 while (NULL != (bme = ce->bme))
378 {
379 ce->bme = bme->next;
380 if (NULL != it)
368 { 381 {
369 struct BigMapEntry *bme; 382 kc = bme->key;
370 383 if (GNUNET_OK != it (it_cls, &kc, bme->value))
371 ce->bme = me.bme; 384 {
372 while (NULL != (bme = ce->bme)) 385 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
373 { 386 return GNUNET_SYSERR;
374 ce->bme = bme->next; 387 }
375 if (NULL != it)
376 {
377 kc = bme->key;
378 if (GNUNET_OK != it(it_cls, &kc, bme->value))
379 {
380 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
381 return GNUNET_SYSERR;
382 }
383 }
384 count++;
385 }
386 } 388 }
389 count++;
390 }
387 } 391 }
388 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 392 }
393 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
389 return count; 394 return count;
390} 395}
391 396
@@ -398,8 +403,8 @@ GNUNET_CONTAINER_multipeermap_iterate(
398 * @param bme the entry that is about to be free'd 403 * @param bme the entry that is about to be free'd
399 */ 404 */
400static void 405static void
401update_next_cache_bme(struct GNUNET_CONTAINER_MultiPeerMap *map, 406update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
402 const struct BigMapEntry *bme) 407 const struct BigMapEntry *bme)
403{ 408{
404 for (unsigned int i = 0; i < map->next_cache_off; i++) 409 for (unsigned int i = 0; i < map->next_cache_off; i++)
405 if (map->next_cache[i].bme == bme) 410 if (map->next_cache[i].bme == bme)
@@ -415,8 +420,8 @@ update_next_cache_bme(struct GNUNET_CONTAINER_MultiPeerMap *map,
415 * @param sme the entry that is about to be free'd 420 * @param sme the entry that is about to be free'd
416 */ 421 */
417static void 422static void
418update_next_cache_sme(struct GNUNET_CONTAINER_MultiPeerMap *map, 423update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
419 const struct SmallMapEntry *sme) 424 const struct SmallMapEntry *sme)
420{ 425{
421 for (unsigned int i = 0; i < map->next_cache_off; i++) 426 for (unsigned int i = 0; i < map->next_cache_off; i++)
422 if (map->next_cache[i].sme == sme) 427 if (map->next_cache[i].sme == sme)
@@ -436,56 +441,56 @@ update_next_cache_sme(struct GNUNET_CONTAINER_MultiPeerMap *map,
436 * is not in the map 441 * is not in the map
437 */ 442 */
438int 443int
439GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, 444GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
440 const struct GNUNET_PeerIdentity *key, 445 const struct GNUNET_PeerIdentity *key,
441 const void *value) 446 const void *value)
442{ 447{
443 union MapEntry me; 448 union MapEntry me;
444 unsigned int i; 449 unsigned int i;
445 450
446 map->modification_counter++; 451 map->modification_counter++;
447 i = idx_of(map, key); 452 i = idx_of (map, key);
448 me = map->map[i]; 453 me = map->map[i];
449 if (map->use_small_entries) 454 if (map->use_small_entries)
450 { 455 {
451 struct SmallMapEntry *p = NULL; 456 struct SmallMapEntry *p = NULL;
452 457
453 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 458 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
454 { 459 {
455 if ((0 == GNUNET_memcmp(key, sme->key)) && (value == sme->value)) 460 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
456 { 461 {
457 if (NULL == p) 462 if (NULL == p)
458 map->map[i].sme = sme->next; 463 map->map[i].sme = sme->next;
459 else 464 else
460 p->next = sme->next; 465 p->next = sme->next;
461 update_next_cache_sme(map, sme); 466 update_next_cache_sme (map, sme);
462 GNUNET_free(sme); 467 GNUNET_free (sme);
463 map->size--; 468 map->size--;
464 return GNUNET_YES; 469 return GNUNET_YES;
465 } 470 }
466 p = sme; 471 p = sme;
467 }
468 } 472 }
473 }
469 else 474 else
470 { 475 {
471 struct BigMapEntry *p = NULL; 476 struct BigMapEntry *p = NULL;
472 477
473 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 478 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
474 { 479 {
475 if ((0 == GNUNET_memcmp(key, &bme->key)) && (value == bme->value)) 480 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
476 { 481 {
477 if (NULL == p) 482 if (NULL == p)
478 map->map[i].bme = bme->next; 483 map->map[i].bme = bme->next;
479 else 484 else
480 p->next = bme->next; 485 p->next = bme->next;
481 update_next_cache_bme(map, bme); 486 update_next_cache_bme (map, bme);
482 GNUNET_free(bme); 487 GNUNET_free (bme);
483 map->size--; 488 map->size--;
484 return GNUNET_YES; 489 return GNUNET_YES;
485 } 490 }
486 p = bme; 491 p = bme;
487 }
488 } 492 }
493 }
489 return GNUNET_NO; 494 return GNUNET_NO;
490} 495}
491 496
@@ -499,7 +504,7 @@ GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map,
499 * @return number of values removed 504 * @return number of values removed
500 */ 505 */
501int 506int
502GNUNET_CONTAINER_multipeermap_remove_all( 507GNUNET_CONTAINER_multipeermap_remove_all (
503 struct GNUNET_CONTAINER_MultiPeerMap *map, 508 struct GNUNET_CONTAINER_MultiPeerMap *map,
504 const struct GNUNET_PeerIdentity *key) 509 const struct GNUNET_PeerIdentity *key)
505{ 510{
@@ -510,70 +515,70 @@ GNUNET_CONTAINER_multipeermap_remove_all(
510 map->modification_counter++; 515 map->modification_counter++;
511 516
512 ret = 0; 517 ret = 0;
513 i = idx_of(map, key); 518 i = idx_of (map, key);
514 me = map->map[i]; 519 me = map->map[i];
515 if (map->use_small_entries) 520 if (map->use_small_entries)
516 { 521 {
517 struct SmallMapEntry *sme; 522 struct SmallMapEntry *sme;
518 struct SmallMapEntry *p; 523 struct SmallMapEntry *p;
519 524
520 p = NULL; 525 p = NULL;
521 sme = me.sme; 526 sme = me.sme;
522 while (NULL != sme) 527 while (NULL != sme)
523 { 528 {
524 if (0 == GNUNET_memcmp(key, sme->key)) 529 if (0 == GNUNET_memcmp (key, sme->key))
525 { 530 {
526 if (NULL == p) 531 if (NULL == p)
527 map->map[i].sme = sme->next; 532 map->map[i].sme = sme->next;
528 else 533 else
529 p->next = sme->next; 534 p->next = sme->next;
530 update_next_cache_sme(map, sme); 535 update_next_cache_sme (map, sme);
531 GNUNET_free(sme); 536 GNUNET_free (sme);
532 map->size--; 537 map->size--;
533 if (NULL == p) 538 if (NULL == p)
534 sme = map->map[i].sme; 539 sme = map->map[i].sme;
535 else 540 else
536 sme = p->next; 541 sme = p->next;
537 ret++; 542 ret++;
538 } 543 }
539 else 544 else
540 { 545 {
541 p = sme; 546 p = sme;
542 sme = sme->next; 547 sme = sme->next;
543 } 548 }
544 }
545 } 549 }
550 }
546 else 551 else
547 { 552 {
548 struct BigMapEntry *bme; 553 struct BigMapEntry *bme;
549 struct BigMapEntry *p; 554 struct BigMapEntry *p;
550 555
551 p = NULL; 556 p = NULL;
552 bme = me.bme; 557 bme = me.bme;
553 while (NULL != bme) 558 while (NULL != bme)
554 { 559 {
555 if (0 == GNUNET_memcmp(key, &bme->key)) 560 if (0 == GNUNET_memcmp (key, &bme->key))
556 { 561 {
557 if (NULL == p) 562 if (NULL == p)
558 map->map[i].bme = bme->next; 563 map->map[i].bme = bme->next;
559 else 564 else
560 p->next = bme->next; 565 p->next = bme->next;
561 update_next_cache_bme(map, bme); 566 update_next_cache_bme (map, bme);
562 GNUNET_free(bme); 567 GNUNET_free (bme);
563 map->size--; 568 map->size--;
564 if (NULL == p) 569 if (NULL == p)
565 bme = map->map[i].bme; 570 bme = map->map[i].bme;
566 else 571 else
567 bme = p->next; 572 bme = p->next;
568 ret++; 573 ret++;
569 } 574 }
570 else 575 else
571 { 576 {
572 p = bme; 577 p = bme;
573 bme = bme->next; 578 bme = bme->next;
574 } 579 }
575 }
576 } 580 }
581 }
577 return ret; 582 return ret;
578} 583}
579 584
@@ -588,25 +593,25 @@ GNUNET_CONTAINER_multipeermap_remove_all(
588 * #GNUNET_NO if not 593 * #GNUNET_NO if not
589 */ 594 */
590int 595int
591GNUNET_CONTAINER_multipeermap_contains( 596GNUNET_CONTAINER_multipeermap_contains (
592 const struct GNUNET_CONTAINER_MultiPeerMap *map, 597 const struct GNUNET_CONTAINER_MultiPeerMap *map,
593 const struct GNUNET_PeerIdentity *key) 598 const struct GNUNET_PeerIdentity *key)
594{ 599{
595 union MapEntry me; 600 union MapEntry me;
596 601
597 me = map->map[idx_of(map, key)]; 602 me = map->map[idx_of (map, key)];
598 if (map->use_small_entries) 603 if (map->use_small_entries)
599 { 604 {
600 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 605 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
601 if (0 == GNUNET_memcmp(key, sme->key)) 606 if (0 == GNUNET_memcmp (key, sme->key))
602 return GNUNET_YES; 607 return GNUNET_YES;
603 } 608 }
604 else 609 else
605 { 610 {
606 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 611 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
607 if (0 == GNUNET_memcmp(key, &bme->key)) 612 if (0 == GNUNET_memcmp (key, &bme->key))
608 return GNUNET_YES; 613 return GNUNET_YES;
609 } 614 }
610 return GNUNET_NO; 615 return GNUNET_NO;
611} 616}
612 617
@@ -622,26 +627,26 @@ GNUNET_CONTAINER_multipeermap_contains(
622 * #GNUNET_NO if not 627 * #GNUNET_NO if not
623 */ 628 */
624int 629int
625GNUNET_CONTAINER_multipeermap_contains_value( 630GNUNET_CONTAINER_multipeermap_contains_value (
626 const struct GNUNET_CONTAINER_MultiPeerMap *map, 631 const struct GNUNET_CONTAINER_MultiPeerMap *map,
627 const struct GNUNET_PeerIdentity *key, 632 const struct GNUNET_PeerIdentity *key,
628 const void *value) 633 const void *value)
629{ 634{
630 union MapEntry me; 635 union MapEntry me;
631 636
632 me = map->map[idx_of(map, key)]; 637 me = map->map[idx_of (map, key)];
633 if (map->use_small_entries) 638 if (map->use_small_entries)
634 { 639 {
635 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 640 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
636 if ((0 == GNUNET_memcmp(key, sme->key)) && (sme->value == value)) 641 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
637 return GNUNET_YES; 642 return GNUNET_YES;
638 } 643 }
639 else 644 else
640 { 645 {
641 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 646 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
642 if ((0 == GNUNET_memcmp(key, &bme->key)) && (bme->value == value)) 647 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
643 return GNUNET_YES; 648 return GNUNET_YES;
644 } 649 }
645 return GNUNET_NO; 650 return GNUNET_NO;
646} 651}
647 652
@@ -652,7 +657,7 @@ GNUNET_CONTAINER_multipeermap_contains_value(
652 * @param map the hash map to grow 657 * @param map the hash map to grow
653 */ 658 */
654static void 659static void
655grow(struct GNUNET_CONTAINER_MultiPeerMap *map) 660grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
656{ 661{
657 union MapEntry *old_map; 662 union MapEntry *old_map;
658 union MapEntry *new_map; 663 union MapEntry *new_map;
@@ -662,46 +667,46 @@ grow(struct GNUNET_CONTAINER_MultiPeerMap *map)
662 667
663 old_map = map->map; 668 old_map = map->map;
664 old_len = map->map_length; 669 old_len = map->map_length;
665 GNUNET_assert(0 != old_len); 670 GNUNET_assert (0 != old_len);
666 new_len = old_len * 2; 671 new_len = old_len * 2;
667 if (0 == new_len) /* 2^31 * 2 == 0 */ 672 if (0 == new_len) /* 2^31 * 2 == 0 */
668 new_len = old_len; /* never use 0 */ 673 new_len = old_len; /* never use 0 */
669 if (new_len == old_len) 674 if (new_len == old_len)
670 return; /* nothing changed */ 675 return; /* nothing changed */
671 new_map = GNUNET_malloc_large(new_len * sizeof(union MapEntry)); 676 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
672 if (NULL == new_map) 677 if (NULL == new_map)
673 return; /* grow not possible */ 678 return; /* grow not possible */
674 map->modification_counter++; 679 map->modification_counter++;
675 map->map_length = new_len; 680 map->map_length = new_len;
676 map->map = new_map; 681 map->map = new_map;
677 for (unsigned int i = 0; i < old_len; i++) 682 for (unsigned int i = 0; i < old_len; i++)
683 {
684 if (map->use_small_entries)
678 { 685 {
679 if (map->use_small_entries) 686 struct SmallMapEntry *sme;
680 { 687
681 struct SmallMapEntry *sme; 688 while (NULL != (sme = old_map[i].sme))
682 689 {
683 while (NULL != (sme = old_map[i].sme)) 690 old_map[i].sme = sme->next;
684 { 691 idx = idx_of (map, sme->key);
685 old_map[i].sme = sme->next; 692 sme->next = new_map[idx].sme;
686 idx = idx_of(map, sme->key); 693 new_map[idx].sme = sme;
687 sme->next = new_map[idx].sme; 694 }
688 new_map[idx].sme = sme;
689 }
690 }
691 else
692 {
693 struct BigMapEntry *bme;
694
695 while (NULL != (bme = old_map[i].bme))
696 {
697 old_map[i].bme = bme->next;
698 idx = idx_of(map, &bme->key);
699 bme->next = new_map[idx].bme;
700 new_map[idx].bme = bme;
701 }
702 }
703 } 695 }
704 GNUNET_free(old_map); 696 else
697 {
698 struct BigMapEntry *bme;
699
700 while (NULL != (bme = old_map[i].bme))
701 {
702 old_map[i].bme = bme->next;
703 idx = idx_of (map, &bme->key);
704 bme->next = new_map[idx].bme;
705 new_map[idx].bme = bme;
706 }
707 }
708 }
709 GNUNET_free (old_map);
705} 710}
706 711
707 712
@@ -718,71 +723,71 @@ grow(struct GNUNET_CONTAINER_MultiPeerMap *map)
718 * value already exists 723 * value already exists
719 */ 724 */
720int 725int
721GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, 726GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
722 const struct GNUNET_PeerIdentity *key, 727 const struct GNUNET_PeerIdentity *key,
723 void *value, 728 void *value,
724 enum GNUNET_CONTAINER_MultiHashMapOption opt) 729 enum GNUNET_CONTAINER_MultiHashMapOption opt)
725{ 730{
726 union MapEntry me; 731 union MapEntry me;
727 unsigned int i; 732 unsigned int i;
728 733
729 i = idx_of(map, key); 734 i = idx_of (map, key);
730 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 735 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
731 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 736 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
737 {
738 me = map->map[i];
739 if (map->use_small_entries)
732 { 740 {
733 me = map->map[i]; 741 struct SmallMapEntry *sme;
734 if (map->use_small_entries) 742
743 for (sme = me.sme; NULL != sme; sme = sme->next)
744 if (0 == GNUNET_memcmp (key, sme->key))
735 { 745 {
736 struct SmallMapEntry *sme; 746 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
737 747 return GNUNET_SYSERR;
738 for (sme = me.sme; NULL != sme; sme = sme->next) 748 sme->value = value;
739 if (0 == GNUNET_memcmp(key, sme->key)) 749 return GNUNET_NO;
740 {
741 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
742 return GNUNET_SYSERR;
743 sme->value = value;
744 return GNUNET_NO;
745 }
746 } 750 }
747 else 751 }
752 else
753 {
754 struct BigMapEntry *bme;
755
756 for (bme = me.bme; NULL != bme; bme = bme->next)
757 if (0 == GNUNET_memcmp (key, &bme->key))
748 { 758 {
749 struct BigMapEntry *bme; 759 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
750 760 return GNUNET_SYSERR;
751 for (bme = me.bme; NULL != bme; bme = bme->next) 761 bme->value = value;
752 if (0 == GNUNET_memcmp(key, &bme->key)) 762 return GNUNET_NO;
753 {
754 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
755 return GNUNET_SYSERR;
756 bme->value = value;
757 return GNUNET_NO;
758 }
759 } 763 }
760 } 764 }
765 }
761 if (map->size / 3 >= map->map_length / 4) 766 if (map->size / 3 >= map->map_length / 4)
762 { 767 {
763 grow(map); 768 grow (map);
764 i = idx_of(map, key); 769 i = idx_of (map, key);
765 } 770 }
766 if (map->use_small_entries) 771 if (map->use_small_entries)
767 { 772 {
768 struct SmallMapEntry *sme; 773 struct SmallMapEntry *sme;
769 774
770 sme = GNUNET_new(struct SmallMapEntry); 775 sme = GNUNET_new (struct SmallMapEntry);
771 sme->key = key; 776 sme->key = key;
772 sme->value = value; 777 sme->value = value;
773 sme->next = map->map[i].sme; 778 sme->next = map->map[i].sme;
774 map->map[i].sme = sme; 779 map->map[i].sme = sme;
775 } 780 }
776 else 781 else
777 { 782 {
778 struct BigMapEntry *bme; 783 struct BigMapEntry *bme;
779 784
780 bme = GNUNET_new(struct BigMapEntry); 785 bme = GNUNET_new (struct BigMapEntry);
781 bme->key = *key; 786 bme->key = *key;
782 bme->value = value; 787 bme->value = value;
783 bme->next = map->map[i].bme; 788 bme->next = map->map[i].bme;
784 map->map[i].bme = bme; 789 map->map[i].bme = bme;
785 } 790 }
786 map->size++; 791 map->size++;
787 return GNUNET_OK; 792 return GNUNET_OK;
788} 793}
@@ -799,7 +804,7 @@ GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map,
799 * #GNUNET_SYSERR if it aborted iteration 804 * #GNUNET_SYSERR if it aborted iteration
800 */ 805 */
801int 806int
802GNUNET_CONTAINER_multipeermap_get_multiple( 807GNUNET_CONTAINER_multipeermap_get_multiple (
803 struct GNUNET_CONTAINER_MultiPeerMap *map, 808 struct GNUNET_CONTAINER_MultiPeerMap *map,
804 const struct GNUNET_PeerIdentity *key, 809 const struct GNUNET_PeerIdentity *key,
805 GNUNET_CONTAINER_PeerMapIterator it, 810 GNUNET_CONTAINER_PeerMapIterator it,
@@ -810,46 +815,46 @@ GNUNET_CONTAINER_multipeermap_get_multiple(
810 union MapEntry *ce; 815 union MapEntry *ce;
811 816
812 ce = &map->next_cache[map->next_cache_off]; 817 ce = &map->next_cache[map->next_cache_off];
813 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 818 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
814 count = 0; 819 count = 0;
815 me = map->map[idx_of(map, key)]; 820 me = map->map[idx_of (map, key)];
816 if (map->use_small_entries) 821 if (map->use_small_entries)
817 { 822 {
818 struct SmallMapEntry *sme; 823 struct SmallMapEntry *sme;
819 824
820 ce->sme = me.sme; 825 ce->sme = me.sme;
821 while (NULL != (sme = ce->sme)) 826 while (NULL != (sme = ce->sme))
822 { 827 {
823 ce->sme = sme->next; 828 ce->sme = sme->next;
824 if (0 != GNUNET_memcmp(key, sme->key)) 829 if (0 != GNUNET_memcmp (key, sme->key))
825 continue; 830 continue;
826 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, sme->value))) 831 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
827 { 832 {
828 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 833 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
829 return GNUNET_SYSERR; 834 return GNUNET_SYSERR;
830 } 835 }
831 count++; 836 count++;
832 }
833 } 837 }
838 }
834 else 839 else
835 { 840 {
836 struct BigMapEntry *bme; 841 struct BigMapEntry *bme;
837 842
838 ce->bme = me.bme; 843 ce->bme = me.bme;
839 while (NULL != (bme = ce->bme)) 844 while (NULL != (bme = ce->bme))
840 { 845 {
841 ce->bme = bme->next; 846 ce->bme = bme->next;
842 if (0 != GNUNET_memcmp(key, &bme->key)) 847 if (0 != GNUNET_memcmp (key, &bme->key))
843 continue; 848 continue;
844 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, bme->value))) 849 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
845 { 850 {
846 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 851 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
847 return GNUNET_SYSERR; 852 return GNUNET_SYSERR;
848 } 853 }
849 count++; 854 count++;
850 }
851 } 855 }
852 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 856 }
857 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
853 return count; 858 return count;
854} 859}
855 860
@@ -866,7 +871,7 @@ GNUNET_CONTAINER_multipeermap_get_multiple(
866 * @return the number of key value pairs processed, zero or one. 871 * @return the number of key value pairs processed, zero or one.
867 */ 872 */
868unsigned int 873unsigned int
869GNUNET_CONTAINER_multipeermap_get_random( 874GNUNET_CONTAINER_multipeermap_get_random (
870 const struct GNUNET_CONTAINER_MultiPeerMap *map, 875 const struct GNUNET_CONTAINER_MultiPeerMap *map,
871 GNUNET_CONTAINER_PeerMapIterator it, 876 GNUNET_CONTAINER_PeerMapIterator it,
872 void *it_cls) 877 void *it_cls)
@@ -878,48 +883,48 @@ GNUNET_CONTAINER_multipeermap_get_random(
878 return 0; 883 return 0;
879 if (NULL == it) 884 if (NULL == it)
880 return 1; 885 return 1;
881 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, map->size); 886 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
882 for (unsigned int idx = 0; idx < map->map_length; idx++) 887 for (unsigned int idx = 0; idx < map->map_length; idx++)
888 {
889 me = map->map[idx];
890 if (map->use_small_entries)
883 { 891 {
884 me = map->map[idx]; 892 struct SmallMapEntry *sme;
885 if (map->use_small_entries) 893 struct SmallMapEntry *nxt;
894
895 nxt = me.sme;
896 while (NULL != (sme = nxt))
897 {
898 nxt = sme->next;
899 if (0 == off)
886 { 900 {
887 struct SmallMapEntry *sme; 901 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
888 struct SmallMapEntry *nxt; 902 return GNUNET_SYSERR;
889 903 return 1;
890 nxt = me.sme;
891 while (NULL != (sme = nxt))
892 {
893 nxt = sme->next;
894 if (0 == off)
895 {
896 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
897 return GNUNET_SYSERR;
898 return 1;
899 }
900 off--;
901 }
902 } 904 }
903 else 905 off--;
906 }
907 }
908 else
909 {
910 struct BigMapEntry *bme;
911 struct BigMapEntry *nxt;
912
913 nxt = me.bme;
914 while (NULL != (bme = nxt))
915 {
916 nxt = bme->next;
917 if (0 == off)
904 { 918 {
905 struct BigMapEntry *bme; 919 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
906 struct BigMapEntry *nxt; 920 return GNUNET_SYSERR;
907 921 return 1;
908 nxt = me.bme;
909 while (NULL != (bme = nxt))
910 {
911 nxt = bme->next;
912 if (0 == off)
913 {
914 if (GNUNET_OK != it(it_cls, &bme->key, bme->value))
915 return GNUNET_SYSERR;
916 return 1;
917 }
918 off--;
919 }
920 } 922 }
923 off--;
924 }
921 } 925 }
922 GNUNET_break(0); 926 }
927 GNUNET_break (0);
923 return GNUNET_SYSERR; 928 return GNUNET_SYSERR;
924} 929}
925 930
@@ -937,12 +942,12 @@ GNUNET_CONTAINER_multipeermap_get_random(
937 * @return an iterator over the given multipeermap 'map' 942 * @return an iterator over the given multipeermap 'map'
938 */ 943 */
939struct GNUNET_CONTAINER_MultiPeerMapIterator * 944struct GNUNET_CONTAINER_MultiPeerMapIterator *
940GNUNET_CONTAINER_multipeermap_iterator_create( 945GNUNET_CONTAINER_multipeermap_iterator_create (
941 const struct GNUNET_CONTAINER_MultiPeerMap *map) 946 const struct GNUNET_CONTAINER_MultiPeerMap *map)
942{ 947{
943 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter; 948 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
944 949
945 iter = GNUNET_new(struct GNUNET_CONTAINER_MultiPeerMapIterator); 950 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
946 iter->map = map; 951 iter->map = map;
947 iter->modification_counter = map->modification_counter; 952 iter->modification_counter = map->modification_counter;
948 iter->me = map->map[0]; 953 iter->me = map->map[0];
@@ -965,47 +970,47 @@ GNUNET_CONTAINER_multipeermap_iterator_create(
965 * #GNUNET_NO if we are out of elements 970 * #GNUNET_NO if we are out of elements
966 */ 971 */
967int 972int
968GNUNET_CONTAINER_multipeermap_iterator_next( 973GNUNET_CONTAINER_multipeermap_iterator_next (
969 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter, 974 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
970 struct GNUNET_PeerIdentity *key, 975 struct GNUNET_PeerIdentity *key,
971 const void **value) 976 const void **value)
972{ 977{
973 /* make sure the map has not been modified */ 978 /* make sure the map has not been modified */
974 GNUNET_assert(iter->modification_counter == iter->map->modification_counter); 979 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
975 980
976 /* look for the next entry, skipping empty buckets */ 981 /* look for the next entry, skipping empty buckets */
977 while (1) 982 while (1)
983 {
984 if (iter->idx >= iter->map->map_length)
985 return GNUNET_NO;
986 if (GNUNET_YES == iter->map->use_small_entries)
978 { 987 {
979 if (iter->idx >= iter->map->map_length) 988 if (NULL != iter->me.sme)
980 return GNUNET_NO; 989 {
981 if (GNUNET_YES == iter->map->use_small_entries) 990 if (NULL != key)
982 { 991 *key = *iter->me.sme->key;
983 if (NULL != iter->me.sme) 992 if (NULL != value)
984 { 993 *value = iter->me.sme->value;
985 if (NULL != key) 994 iter->me.sme = iter->me.sme->next;
986 *key = *iter->me.sme->key; 995 return GNUNET_YES;
987 if (NULL != value) 996 }
988 *value = iter->me.sme->value; 997 }
989 iter->me.sme = iter->me.sme->next; 998 else
990 return GNUNET_YES; 999 {
991 } 1000 if (NULL != iter->me.bme)
992 } 1001 {
993 else 1002 if (NULL != key)
994 { 1003 *key = iter->me.bme->key;
995 if (NULL != iter->me.bme) 1004 if (NULL != value)
996 { 1005 *value = iter->me.bme->value;
997 if (NULL != key) 1006 iter->me.bme = iter->me.bme->next;
998 *key = iter->me.bme->key; 1007 return GNUNET_YES;
999 if (NULL != value) 1008 }
1000 *value = iter->me.bme->value;
1001 iter->me.bme = iter->me.bme->next;
1002 return GNUNET_YES;
1003 }
1004 }
1005 iter->idx += 1;
1006 if (iter->idx < iter->map->map_length)
1007 iter->me = iter->map->map[iter->idx];
1008 } 1009 }
1010 iter->idx += 1;
1011 if (iter->idx < iter->map->map_length)
1012 iter->me = iter->map->map[iter->idx];
1013 }
1009} 1014}
1010 1015
1011 1016
@@ -1015,10 +1020,10 @@ GNUNET_CONTAINER_multipeermap_iterator_next(
1015 * @param iter the iterator to destroy 1020 * @param iter the iterator to destroy
1016 */ 1021 */
1017void 1022void
1018GNUNET_CONTAINER_multipeermap_iterator_destroy( 1023GNUNET_CONTAINER_multipeermap_iterator_destroy (
1019 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter) 1024 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1020{ 1025{
1021 GNUNET_free(iter); 1026 GNUNET_free (iter);
1022} 1027}
1023 1028
1024 1029
diff --git a/src/util/container_multishortmap.c b/src/util/container_multishortmap.c
index 4f2bc149d..86ab31107 100644
--- a/src/util/container_multishortmap.c
+++ b/src/util/container_multishortmap.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29#define LOG(kind, ...) \ 29#define LOG(kind, ...) \
30 GNUNET_log_from(kind, "util-container-multishortmap", __VA_ARGS__) 30 GNUNET_log_from (kind, "util-container-multishortmap", __VA_ARGS__)
31 31
32/** 32/**
33 * Maximum recursion depth for callbacks of 33 * Maximum recursion depth for callbacks of
@@ -41,7 +41,8 @@
41/** 41/**
42 * An entry in the hash map with the full key. 42 * An entry in the hash map with the full key.
43 */ 43 */
44struct BigMapEntry { 44struct BigMapEntry
45{
45 /** 46 /**
46 * Value of the entry. 47 * Value of the entry.
47 */ 48 */
@@ -62,7 +63,8 @@ struct BigMapEntry {
62/** 63/**
63 * An entry in the hash map with just a pointer to the key. 64 * An entry in the hash map with just a pointer to the key.
64 */ 65 */
65struct SmallMapEntry { 66struct SmallMapEntry
67{
66 /** 68 /**
67 * Value of the entry. 69 * Value of the entry.
68 */ 70 */
@@ -83,7 +85,8 @@ struct SmallMapEntry {
83/** 85/**
84 * Entry in the map. 86 * Entry in the map.
85 */ 87 */
86union MapEntry { 88union MapEntry
89{
87 /** 90 /**
88 * Variant used if map entries only contain a pointer to the key. 91 * Variant used if map entries only contain a pointer to the key.
89 */ 92 */
@@ -99,7 +102,8 @@ union MapEntry {
99/** 102/**
100 * Internal representation of the hash map. 103 * Internal representation of the hash map.
101 */ 104 */
102struct GNUNET_CONTAINER_MultiShortmap { 105struct GNUNET_CONTAINER_MultiShortmap
106{
103 /** 107 /**
104 * All of our buckets. 108 * All of our buckets.
105 */ 109 */
@@ -146,7 +150,8 @@ struct GNUNET_CONTAINER_MultiShortmap {
146 * Cursor into a multishortmap. 150 * Cursor into a multishortmap.
147 * Allows to enumerate elements asynchronously. 151 * Allows to enumerate elements asynchronously.
148 */ 152 */
149struct GNUNET_CONTAINER_MultiShortmapIterator { 153struct GNUNET_CONTAINER_MultiShortmapIterator
154{
150 /** 155 /**
151 * Position in the bucket 'idx' 156 * Position in the bucket 'idx'
152 */ 157 */
@@ -186,18 +191,18 @@ struct GNUNET_CONTAINER_MultiShortmapIterator {
186 * @return NULL on error 191 * @return NULL on error
187 */ 192 */
188struct GNUNET_CONTAINER_MultiShortmap * 193struct GNUNET_CONTAINER_MultiShortmap *
189GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys) 194GNUNET_CONTAINER_multishortmap_create (unsigned int len, int do_not_copy_keys)
190{ 195{
191 struct GNUNET_CONTAINER_MultiShortmap *map; 196 struct GNUNET_CONTAINER_MultiShortmap *map;
192 197
193 GNUNET_assert(len > 0); 198 GNUNET_assert (len > 0);
194 map = GNUNET_new(struct GNUNET_CONTAINER_MultiShortmap); 199 map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap);
195 map->map = GNUNET_malloc_large(len * sizeof(union MapEntry)); 200 map->map = GNUNET_malloc_large (len * sizeof(union MapEntry));
196 if (NULL == map->map) 201 if (NULL == map->map)
197 { 202 {
198 GNUNET_free(map); 203 GNUNET_free (map);
199 return NULL; 204 return NULL;
200 } 205 }
201 map->map_length = len; 206 map->map_length = len;
202 map->use_small_entries = do_not_copy_keys; 207 map->use_small_entries = do_not_copy_keys;
203 return map; 208 return map;
@@ -211,44 +216,44 @@ GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
211 * @param map the map 216 * @param map the map
212 */ 217 */
213void 218void
214GNUNET_CONTAINER_multishortmap_destroy( 219GNUNET_CONTAINER_multishortmap_destroy (
215 struct GNUNET_CONTAINER_MultiShortmap *map) 220 struct GNUNET_CONTAINER_MultiShortmap *map)
216{ 221{
217 GNUNET_assert(0 == map->next_cache_off); 222 GNUNET_assert (0 == map->next_cache_off);
218 for (unsigned int i = 0; i < map->map_length; i++) 223 for (unsigned int i = 0; i < map->map_length; i++)
219 { 224 {
220 union MapEntry me; 225 union MapEntry me;
221 226
222 me = map->map[i]; 227 me = map->map[i];
223 if (map->use_small_entries) 228 if (map->use_small_entries)
224 { 229 {
225 struct SmallMapEntry *sme; 230 struct SmallMapEntry *sme;
226 struct SmallMapEntry *nxt; 231 struct SmallMapEntry *nxt;
227 232
228 nxt = me.sme; 233 nxt = me.sme;
229 while (NULL != (sme = nxt)) 234 while (NULL != (sme = nxt))
230 { 235 {
231 nxt = sme->next; 236 nxt = sme->next;
232 GNUNET_free(sme); 237 GNUNET_free (sme);
233 } 238 }
234 me.sme = NULL; 239 me.sme = NULL;
235 }
236 else
237 {
238 struct BigMapEntry *bme;
239 struct BigMapEntry *nxt;
240
241 nxt = me.bme;
242 while (NULL != (bme = nxt))
243 {
244 nxt = bme->next;
245 GNUNET_free(bme);
246 }
247 me.bme = NULL;
248 }
249 } 240 }
250 GNUNET_free(map->map); 241 else
251 GNUNET_free(map); 242 {
243 struct BigMapEntry *bme;
244 struct BigMapEntry *nxt;
245
246 nxt = me.bme;
247 while (NULL != (bme = nxt))
248 {
249 nxt = bme->next;
250 GNUNET_free (bme);
251 }
252 me.bme = NULL;
253 }
254 }
255 GNUNET_free (map->map);
256 GNUNET_free (map);
252} 257}
253 258
254 259
@@ -260,13 +265,13 @@ GNUNET_CONTAINER_multishortmap_destroy(
260 * @return offset into the "map" array of "map" 265 * @return offset into the "map" array of "map"
261 */ 266 */
262static unsigned int 267static unsigned int
263idx_of(const struct GNUNET_CONTAINER_MultiShortmap *map, 268idx_of (const struct GNUNET_CONTAINER_MultiShortmap *map,
264 const struct GNUNET_ShortHashCode *key) 269 const struct GNUNET_ShortHashCode *key)
265{ 270{
266 unsigned int kx; 271 unsigned int kx;
267 272
268 GNUNET_assert(NULL != map); 273 GNUNET_assert (NULL != map);
269 GNUNET_memcpy(&kx, key, sizeof(kx)); 274 GNUNET_memcpy (&kx, key, sizeof(kx));
270 return kx % map->map_length; 275 return kx % map->map_length;
271} 276}
272 277
@@ -278,7 +283,7 @@ idx_of(const struct GNUNET_CONTAINER_MultiShortmap *map,
278 * @return the number of key value pairs 283 * @return the number of key value pairs
279 */ 284 */
280unsigned int 285unsigned int
281GNUNET_CONTAINER_multishortmap_size( 286GNUNET_CONTAINER_multishortmap_size (
282 const struct GNUNET_CONTAINER_MultiShortmap *map) 287 const struct GNUNET_CONTAINER_MultiShortmap *map)
283{ 288{
284 return map->size; 289 return map->size;
@@ -296,25 +301,25 @@ GNUNET_CONTAINER_multishortmap_size(
296 * key-value pairs with value NULL 301 * key-value pairs with value NULL
297 */ 302 */
298void * 303void *
299GNUNET_CONTAINER_multishortmap_get( 304GNUNET_CONTAINER_multishortmap_get (
300 const struct GNUNET_CONTAINER_MultiShortmap *map, 305 const struct GNUNET_CONTAINER_MultiShortmap *map,
301 const struct GNUNET_ShortHashCode *key) 306 const struct GNUNET_ShortHashCode *key)
302{ 307{
303 union MapEntry me; 308 union MapEntry me;
304 309
305 me = map->map[idx_of(map, key)]; 310 me = map->map[idx_of (map, key)];
306 if (map->use_small_entries) 311 if (map->use_small_entries)
307 { 312 {
308 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 313 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
309 if (0 == GNUNET_memcmp(key, sme->key)) 314 if (0 == GNUNET_memcmp (key, sme->key))
310 return sme->value; 315 return sme->value;
311 } 316 }
312 else 317 else
313 { 318 {
314 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 319 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
315 if (0 == GNUNET_memcmp(key, &bme->key)) 320 if (0 == GNUNET_memcmp (key, &bme->key))
316 return bme->value; 321 return bme->value;
317 } 322 }
318 return NULL; 323 return NULL;
319} 324}
320 325
@@ -329,7 +334,7 @@ GNUNET_CONTAINER_multishortmap_get(
329 * #GNUNET_SYSERR if it aborted iteration 334 * #GNUNET_SYSERR if it aborted iteration
330 */ 335 */
331int 336int
332GNUNET_CONTAINER_multishortmap_iterate( 337GNUNET_CONTAINER_multishortmap_iterate (
333 struct GNUNET_CONTAINER_MultiShortmap *map, 338 struct GNUNET_CONTAINER_MultiShortmap *map,
334 GNUNET_CONTAINER_ShortmapIterator it, 339 GNUNET_CONTAINER_ShortmapIterator it,
335 void *it_cls) 340 void *it_cls)
@@ -340,50 +345,50 @@ GNUNET_CONTAINER_multishortmap_iterate(
340 struct GNUNET_ShortHashCode kc; 345 struct GNUNET_ShortHashCode kc;
341 346
342 count = 0; 347 count = 0;
343 GNUNET_assert(NULL != map); 348 GNUNET_assert (NULL != map);
344 ce = &map->next_cache[map->next_cache_off]; 349 ce = &map->next_cache[map->next_cache_off];
345 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 350 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
346 for (unsigned int i = 0; i < map->map_length; i++) 351 for (unsigned int i = 0; i < map->map_length; i++)
352 {
353 me = map->map[i];
354 if (map->use_small_entries)
347 { 355 {
348 me = map->map[i]; 356 struct SmallMapEntry *sme;
349 if (map->use_small_entries) 357
358 ce->sme = me.sme;
359 while (NULL != (sme = ce->sme))
360 {
361 ce->sme = sme->next;
362 if ((NULL != it) && (GNUNET_OK != it (it_cls, sme->key, sme->value)))
350 { 363 {
351 struct SmallMapEntry *sme; 364 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
352 365 return GNUNET_SYSERR;
353 ce->sme = me.sme;
354 while (NULL != (sme = ce->sme))
355 {
356 ce->sme = sme->next;
357 if ((NULL != it) && (GNUNET_OK != it(it_cls, sme->key, sme->value)))
358 {
359 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
360 return GNUNET_SYSERR;
361 }
362 count++;
363 }
364 } 366 }
365 else 367 count++;
368 }
369 }
370 else
371 {
372 struct BigMapEntry *bme;
373
374 ce->bme = me.bme;
375 while (NULL != (bme = ce->bme))
376 {
377 ce->bme = bme->next;
378 if (NULL != it)
366 { 379 {
367 struct BigMapEntry *bme; 380 kc = bme->key;
368 381 if (GNUNET_OK != it (it_cls, &kc, bme->value))
369 ce->bme = me.bme; 382 {
370 while (NULL != (bme = ce->bme)) 383 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
371 { 384 return GNUNET_SYSERR;
372 ce->bme = bme->next; 385 }
373 if (NULL != it)
374 {
375 kc = bme->key;
376 if (GNUNET_OK != it(it_cls, &kc, bme->value))
377 {
378 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
379 return GNUNET_SYSERR;
380 }
381 }
382 count++;
383 }
384 } 386 }
387 count++;
388 }
385 } 389 }
386 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 390 }
391 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
387 return count; 392 return count;
388} 393}
389 394
@@ -396,8 +401,8 @@ GNUNET_CONTAINER_multishortmap_iterate(
396 * @param bme the entry that is about to be free'd 401 * @param bme the entry that is about to be free'd
397 */ 402 */
398static void 403static void
399update_next_cache_bme(struct GNUNET_CONTAINER_MultiShortmap *map, 404update_next_cache_bme (struct GNUNET_CONTAINER_MultiShortmap *map,
400 const struct BigMapEntry *bme) 405 const struct BigMapEntry *bme)
401{ 406{
402 for (unsigned int i = 0; i < map->next_cache_off; i++) 407 for (unsigned int i = 0; i < map->next_cache_off; i++)
403 if (map->next_cache[i].bme == bme) 408 if (map->next_cache[i].bme == bme)
@@ -413,8 +418,8 @@ update_next_cache_bme(struct GNUNET_CONTAINER_MultiShortmap *map,
413 * @param sme the entry that is about to be free'd 418 * @param sme the entry that is about to be free'd
414 */ 419 */
415static void 420static void
416update_next_cache_sme(struct GNUNET_CONTAINER_MultiShortmap *map, 421update_next_cache_sme (struct GNUNET_CONTAINER_MultiShortmap *map,
417 const struct SmallMapEntry *sme) 422 const struct SmallMapEntry *sme)
418{ 423{
419 for (unsigned int i = 0; i < map->next_cache_off; i++) 424 for (unsigned int i = 0; i < map->next_cache_off; i++)
420 if (map->next_cache[i].sme == sme) 425 if (map->next_cache[i].sme == sme)
@@ -434,7 +439,7 @@ update_next_cache_sme(struct GNUNET_CONTAINER_MultiShortmap *map,
434 * is not in the map 439 * is not in the map
435 */ 440 */
436int 441int
437GNUNET_CONTAINER_multishortmap_remove( 442GNUNET_CONTAINER_multishortmap_remove (
438 struct GNUNET_CONTAINER_MultiShortmap *map, 443 struct GNUNET_CONTAINER_MultiShortmap *map,
439 const struct GNUNET_ShortHashCode *key, 444 const struct GNUNET_ShortHashCode *key,
440 const void *value) 445 const void *value)
@@ -443,48 +448,48 @@ GNUNET_CONTAINER_multishortmap_remove(
443 unsigned int i; 448 unsigned int i;
444 449
445 map->modification_counter++; 450 map->modification_counter++;
446 i = idx_of(map, key); 451 i = idx_of (map, key);
447 me = map->map[i]; 452 me = map->map[i];
448 if (map->use_small_entries) 453 if (map->use_small_entries)
449 { 454 {
450 struct SmallMapEntry *p = NULL; 455 struct SmallMapEntry *p = NULL;
451 456
452 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 457 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
453 { 458 {
454 if ((0 == GNUNET_memcmp(key, sme->key)) && (value == sme->value)) 459 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
455 { 460 {
456 if (NULL == p) 461 if (NULL == p)
457 map->map[i].sme = sme->next; 462 map->map[i].sme = sme->next;
458 else 463 else
459 p->next = sme->next; 464 p->next = sme->next;
460 update_next_cache_sme(map, sme); 465 update_next_cache_sme (map, sme);
461 GNUNET_free(sme); 466 GNUNET_free (sme);
462 map->size--; 467 map->size--;
463 return GNUNET_YES; 468 return GNUNET_YES;
464 } 469 }
465 p = sme; 470 p = sme;
466 }
467 } 471 }
472 }
468 else 473 else
469 { 474 {
470 struct BigMapEntry *p = NULL; 475 struct BigMapEntry *p = NULL;
471 476
472 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 477 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
473 { 478 {
474 if ((0 == GNUNET_memcmp(key, &bme->key)) && (value == bme->value)) 479 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
475 { 480 {
476 if (NULL == p) 481 if (NULL == p)
477 map->map[i].bme = bme->next; 482 map->map[i].bme = bme->next;
478 else 483 else
479 p->next = bme->next; 484 p->next = bme->next;
480 update_next_cache_bme(map, bme); 485 update_next_cache_bme (map, bme);
481 GNUNET_free(bme); 486 GNUNET_free (bme);
482 map->size--; 487 map->size--;
483 return GNUNET_YES; 488 return GNUNET_YES;
484 } 489 }
485 p = bme; 490 p = bme;
486 }
487 } 491 }
492 }
488 return GNUNET_NO; 493 return GNUNET_NO;
489} 494}
490 495
@@ -498,7 +503,7 @@ GNUNET_CONTAINER_multishortmap_remove(
498 * @return number of values removed 503 * @return number of values removed
499 */ 504 */
500int 505int
501GNUNET_CONTAINER_multishortmap_remove_all( 506GNUNET_CONTAINER_multishortmap_remove_all (
502 struct GNUNET_CONTAINER_MultiShortmap *map, 507 struct GNUNET_CONTAINER_MultiShortmap *map,
503 const struct GNUNET_ShortHashCode *key) 508 const struct GNUNET_ShortHashCode *key)
504{ 509{
@@ -509,70 +514,70 @@ GNUNET_CONTAINER_multishortmap_remove_all(
509 map->modification_counter++; 514 map->modification_counter++;
510 515
511 ret = 0; 516 ret = 0;
512 i = idx_of(map, key); 517 i = idx_of (map, key);
513 me = map->map[i]; 518 me = map->map[i];
514 if (map->use_small_entries) 519 if (map->use_small_entries)
515 { 520 {
516 struct SmallMapEntry *sme; 521 struct SmallMapEntry *sme;
517 struct SmallMapEntry *p; 522 struct SmallMapEntry *p;
518 523
519 p = NULL; 524 p = NULL;
520 sme = me.sme; 525 sme = me.sme;
521 while (NULL != sme) 526 while (NULL != sme)
522 { 527 {
523 if (0 == GNUNET_memcmp(key, sme->key)) 528 if (0 == GNUNET_memcmp (key, sme->key))
524 { 529 {
525 if (NULL == p) 530 if (NULL == p)
526 map->map[i].sme = sme->next; 531 map->map[i].sme = sme->next;
527 else 532 else
528 p->next = sme->next; 533 p->next = sme->next;
529 update_next_cache_sme(map, sme); 534 update_next_cache_sme (map, sme);
530 GNUNET_free(sme); 535 GNUNET_free (sme);
531 map->size--; 536 map->size--;
532 if (NULL == p) 537 if (NULL == p)
533 sme = map->map[i].sme; 538 sme = map->map[i].sme;
534 else 539 else
535 sme = p->next; 540 sme = p->next;
536 ret++; 541 ret++;
537 } 542 }
538 else 543 else
539 { 544 {
540 p = sme; 545 p = sme;
541 sme = sme->next; 546 sme = sme->next;
542 } 547 }
543 }
544 } 548 }
549 }
545 else 550 else
546 { 551 {
547 struct BigMapEntry *bme; 552 struct BigMapEntry *bme;
548 struct BigMapEntry *p; 553 struct BigMapEntry *p;
549 554
550 p = NULL; 555 p = NULL;
551 bme = me.bme; 556 bme = me.bme;
552 while (NULL != bme) 557 while (NULL != bme)
553 { 558 {
554 if (0 == GNUNET_memcmp(key, &bme->key)) 559 if (0 == GNUNET_memcmp (key, &bme->key))
555 { 560 {
556 if (NULL == p) 561 if (NULL == p)
557 map->map[i].bme = bme->next; 562 map->map[i].bme = bme->next;
558 else 563 else
559 p->next = bme->next; 564 p->next = bme->next;
560 update_next_cache_bme(map, bme); 565 update_next_cache_bme (map, bme);
561 GNUNET_free(bme); 566 GNUNET_free (bme);
562 map->size--; 567 map->size--;
563 if (NULL == p) 568 if (NULL == p)
564 bme = map->map[i].bme; 569 bme = map->map[i].bme;
565 else 570 else
566 bme = p->next; 571 bme = p->next;
567 ret++; 572 ret++;
568 } 573 }
569 else 574 else
570 { 575 {
571 p = bme; 576 p = bme;
572 bme = bme->next; 577 bme = bme->next;
573 } 578 }
574 }
575 } 579 }
580 }
576 return ret; 581 return ret;
577} 582}
578 583
@@ -587,25 +592,25 @@ GNUNET_CONTAINER_multishortmap_remove_all(
587 * #GNUNET_NO if not 592 * #GNUNET_NO if not
588 */ 593 */
589int 594int
590GNUNET_CONTAINER_multishortmap_contains( 595GNUNET_CONTAINER_multishortmap_contains (
591 const struct GNUNET_CONTAINER_MultiShortmap *map, 596 const struct GNUNET_CONTAINER_MultiShortmap *map,
592 const struct GNUNET_ShortHashCode *key) 597 const struct GNUNET_ShortHashCode *key)
593{ 598{
594 union MapEntry me; 599 union MapEntry me;
595 600
596 me = map->map[idx_of(map, key)]; 601 me = map->map[idx_of (map, key)];
597 if (map->use_small_entries) 602 if (map->use_small_entries)
598 { 603 {
599 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 604 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
600 if (0 == GNUNET_memcmp(key, sme->key)) 605 if (0 == GNUNET_memcmp (key, sme->key))
601 return GNUNET_YES; 606 return GNUNET_YES;
602 } 607 }
603 else 608 else
604 { 609 {
605 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 610 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
606 if (0 == GNUNET_memcmp(key, &bme->key)) 611 if (0 == GNUNET_memcmp (key, &bme->key))
607 return GNUNET_YES; 612 return GNUNET_YES;
608 } 613 }
609 return GNUNET_NO; 614 return GNUNET_NO;
610} 615}
611 616
@@ -621,26 +626,26 @@ GNUNET_CONTAINER_multishortmap_contains(
621 * #GNUNET_NO if not 626 * #GNUNET_NO if not
622 */ 627 */
623int 628int
624GNUNET_CONTAINER_multishortmap_contains_value( 629GNUNET_CONTAINER_multishortmap_contains_value (
625 const struct GNUNET_CONTAINER_MultiShortmap *map, 630 const struct GNUNET_CONTAINER_MultiShortmap *map,
626 const struct GNUNET_ShortHashCode *key, 631 const struct GNUNET_ShortHashCode *key,
627 const void *value) 632 const void *value)
628{ 633{
629 union MapEntry me; 634 union MapEntry me;
630 635
631 me = map->map[idx_of(map, key)]; 636 me = map->map[idx_of (map, key)];
632 if (map->use_small_entries) 637 if (map->use_small_entries)
633 { 638 {
634 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 639 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
635 if ((0 == GNUNET_memcmp(key, sme->key)) && (sme->value == value)) 640 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
636 return GNUNET_YES; 641 return GNUNET_YES;
637 } 642 }
638 else 643 else
639 { 644 {
640 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 645 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
641 if ((0 == GNUNET_memcmp(key, &bme->key)) && (bme->value == value)) 646 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
642 return GNUNET_YES; 647 return GNUNET_YES;
643 } 648 }
644 return GNUNET_NO; 649 return GNUNET_NO;
645} 650}
646 651
@@ -651,7 +656,7 @@ GNUNET_CONTAINER_multishortmap_contains_value(
651 * @param map the hash map to grow 656 * @param map the hash map to grow
652 */ 657 */
653static void 658static void
654grow(struct GNUNET_CONTAINER_MultiShortmap *map) 659grow (struct GNUNET_CONTAINER_MultiShortmap *map)
655{ 660{
656 union MapEntry *old_map; 661 union MapEntry *old_map;
657 union MapEntry *new_map; 662 union MapEntry *new_map;
@@ -666,40 +671,40 @@ grow(struct GNUNET_CONTAINER_MultiShortmap *map)
666 new_len = old_len; /* never use 0 */ 671 new_len = old_len; /* never use 0 */
667 if (new_len == old_len) 672 if (new_len == old_len)
668 return; /* nothing changed */ 673 return; /* nothing changed */
669 new_map = GNUNET_malloc_large(new_len * sizeof(union MapEntry)); 674 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
670 if (NULL == new_map) 675 if (NULL == new_map)
671 return; /* grow not possible */ 676 return; /* grow not possible */
672 map->modification_counter++; 677 map->modification_counter++;
673 map->map_length = new_len; 678 map->map_length = new_len;
674 map->map = new_map; 679 map->map = new_map;
675 for (unsigned int i = 0; i < old_len; i++) 680 for (unsigned int i = 0; i < old_len; i++)
681 {
682 if (map->use_small_entries)
676 { 683 {
677 if (map->use_small_entries) 684 struct SmallMapEntry *sme;
678 { 685
679 struct SmallMapEntry *sme; 686 while (NULL != (sme = old_map[i].sme))
680 687 {
681 while (NULL != (sme = old_map[i].sme)) 688 old_map[i].sme = sme->next;
682 { 689 idx = idx_of (map, sme->key);
683 old_map[i].sme = sme->next; 690 sme->next = new_map[idx].sme;
684 idx = idx_of(map, sme->key); 691 new_map[idx].sme = sme;
685 sme->next = new_map[idx].sme; 692 }
686 new_map[idx].sme = sme;
687 }
688 }
689 else
690 {
691 struct BigMapEntry *bme;
692
693 while (NULL != (bme = old_map[i].bme))
694 {
695 old_map[i].bme = bme->next;
696 idx = idx_of(map, &bme->key);
697 bme->next = new_map[idx].bme;
698 new_map[idx].bme = bme;
699 }
700 }
701 } 693 }
702 GNUNET_free(old_map); 694 else
695 {
696 struct BigMapEntry *bme;
697
698 while (NULL != (bme = old_map[i].bme))
699 {
700 old_map[i].bme = bme->next;
701 idx = idx_of (map, &bme->key);
702 bme->next = new_map[idx].bme;
703 new_map[idx].bme = bme;
704 }
705 }
706 }
707 GNUNET_free (old_map);
703} 708}
704 709
705 710
@@ -716,7 +721,7 @@ grow(struct GNUNET_CONTAINER_MultiShortmap *map)
716 * value already exists 721 * value already exists
717 */ 722 */
718int 723int
719GNUNET_CONTAINER_multishortmap_put( 724GNUNET_CONTAINER_multishortmap_put (
720 struct GNUNET_CONTAINER_MultiShortmap *map, 725 struct GNUNET_CONTAINER_MultiShortmap *map,
721 const struct GNUNET_ShortHashCode *key, 726 const struct GNUNET_ShortHashCode *key,
722 void *value, 727 void *value,
@@ -725,59 +730,59 @@ GNUNET_CONTAINER_multishortmap_put(
725 union MapEntry me; 730 union MapEntry me;
726 unsigned int i; 731 unsigned int i;
727 732
728 i = idx_of(map, key); 733 i = idx_of (map, key);
729 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 734 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
730 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 735 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
736 {
737 me = map->map[i];
738 if (map->use_small_entries)
731 { 739 {
732 me = map->map[i]; 740 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
733 if (map->use_small_entries) 741 if (0 == GNUNET_memcmp (key, sme->key))
734 { 742 {
735 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 743 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
736 if (0 == GNUNET_memcmp(key, sme->key)) 744 return GNUNET_SYSERR;
737 { 745 sme->value = value;
738 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 746 return GNUNET_NO;
739 return GNUNET_SYSERR;
740 sme->value = value;
741 return GNUNET_NO;
742 }
743 } 747 }
744 else 748 }
749 else
750 {
751 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
752 if (0 == GNUNET_memcmp (key, &bme->key))
745 { 753 {
746 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 754 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
747 if (0 == GNUNET_memcmp(key, &bme->key)) 755 return GNUNET_SYSERR;
748 { 756 bme->value = value;
749 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 757 return GNUNET_NO;
750 return GNUNET_SYSERR;
751 bme->value = value;
752 return GNUNET_NO;
753 }
754 } 758 }
755 } 759 }
760 }
756 if (map->size / 3 >= map->map_length / 4) 761 if (map->size / 3 >= map->map_length / 4)
757 { 762 {
758 grow(map); 763 grow (map);
759 i = idx_of(map, key); 764 i = idx_of (map, key);
760 } 765 }
761 if (map->use_small_entries) 766 if (map->use_small_entries)
762 { 767 {
763 struct SmallMapEntry *sme; 768 struct SmallMapEntry *sme;
764 769
765 sme = GNUNET_new(struct SmallMapEntry); 770 sme = GNUNET_new (struct SmallMapEntry);
766 sme->key = key; 771 sme->key = key;
767 sme->value = value; 772 sme->value = value;
768 sme->next = map->map[i].sme; 773 sme->next = map->map[i].sme;
769 map->map[i].sme = sme; 774 map->map[i].sme = sme;
770 } 775 }
771 else 776 else
772 { 777 {
773 struct BigMapEntry *bme; 778 struct BigMapEntry *bme;
774 779
775 bme = GNUNET_new(struct BigMapEntry); 780 bme = GNUNET_new (struct BigMapEntry);
776 bme->key = *key; 781 bme->key = *key;
777 bme->value = value; 782 bme->value = value;
778 bme->next = map->map[i].bme; 783 bme->next = map->map[i].bme;
779 map->map[i].bme = bme; 784 map->map[i].bme = bme;
780 } 785 }
781 map->size++; 786 map->size++;
782 return GNUNET_OK; 787 return GNUNET_OK;
783} 788}
@@ -794,7 +799,7 @@ GNUNET_CONTAINER_multishortmap_put(
794 * #GNUNET_SYSERR if it aborted iteration 799 * #GNUNET_SYSERR if it aborted iteration
795 */ 800 */
796int 801int
797GNUNET_CONTAINER_multishortmap_get_multiple( 802GNUNET_CONTAINER_multishortmap_get_multiple (
798 struct GNUNET_CONTAINER_MultiShortmap *map, 803 struct GNUNET_CONTAINER_MultiShortmap *map,
799 const struct GNUNET_ShortHashCode *key, 804 const struct GNUNET_ShortHashCode *key,
800 GNUNET_CONTAINER_ShortmapIterator it, 805 GNUNET_CONTAINER_ShortmapIterator it,
@@ -805,46 +810,46 @@ GNUNET_CONTAINER_multishortmap_get_multiple(
805 union MapEntry *ce; 810 union MapEntry *ce;
806 811
807 ce = &map->next_cache[map->next_cache_off]; 812 ce = &map->next_cache[map->next_cache_off];
808 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 813 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
809 count = 0; 814 count = 0;
810 me = map->map[idx_of(map, key)]; 815 me = map->map[idx_of (map, key)];
811 if (map->use_small_entries) 816 if (map->use_small_entries)
812 { 817 {
813 struct SmallMapEntry *sme; 818 struct SmallMapEntry *sme;
814 819
815 ce->sme = me.sme; 820 ce->sme = me.sme;
816 while (NULL != (sme = ce->sme)) 821 while (NULL != (sme = ce->sme))
817 { 822 {
818 ce->sme = sme->next; 823 ce->sme = sme->next;
819 if (0 != GNUNET_memcmp(key, sme->key)) 824 if (0 != GNUNET_memcmp (key, sme->key))
820 continue; 825 continue;
821 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, sme->value))) 826 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
822 { 827 {
823 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 828 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
824 return GNUNET_SYSERR; 829 return GNUNET_SYSERR;
825 } 830 }
826 count++; 831 count++;
827 }
828 } 832 }
833 }
829 else 834 else
830 { 835 {
831 struct BigMapEntry *bme; 836 struct BigMapEntry *bme;
832 837
833 ce->bme = me.bme; 838 ce->bme = me.bme;
834 while (NULL != (bme = ce->bme)) 839 while (NULL != (bme = ce->bme))
835 { 840 {
836 ce->bme = bme->next; 841 ce->bme = bme->next;
837 if (0 != GNUNET_memcmp(key, &bme->key)) 842 if (0 != GNUNET_memcmp (key, &bme->key))
838 continue; 843 continue;
839 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, bme->value))) 844 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
840 { 845 {
841 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 846 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
842 return GNUNET_SYSERR; 847 return GNUNET_SYSERR;
843 } 848 }
844 count++; 849 count++;
845 }
846 } 850 }
847 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 851 }
852 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
848 return count; 853 return count;
849} 854}
850 855
@@ -861,7 +866,7 @@ GNUNET_CONTAINER_multishortmap_get_multiple(
861 * @return the number of key value pairs processed, zero or one. 866 * @return the number of key value pairs processed, zero or one.
862 */ 867 */
863unsigned int 868unsigned int
864GNUNET_CONTAINER_multishortmap_get_random( 869GNUNET_CONTAINER_multishortmap_get_random (
865 const struct GNUNET_CONTAINER_MultiShortmap *map, 870 const struct GNUNET_CONTAINER_MultiShortmap *map,
866 GNUNET_CONTAINER_ShortmapIterator it, 871 GNUNET_CONTAINER_ShortmapIterator it,
867 void *it_cls) 872 void *it_cls)
@@ -873,38 +878,38 @@ GNUNET_CONTAINER_multishortmap_get_random(
873 return 0; 878 return 0;
874 if (NULL == it) 879 if (NULL == it)
875 return 1; 880 return 1;
876 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, map->size); 881 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
877 for (unsigned int idx = 0; idx < map->map_length; idx++) 882 for (unsigned int idx = 0; idx < map->map_length; idx++)
883 {
884 me = map->map[idx];
885 if (map->use_small_entries)
878 { 886 {
879 me = map->map[idx]; 887 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
880 if (map->use_small_entries) 888 {
889 if (0 == off)
881 { 890 {
882 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 891 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
883 { 892 return GNUNET_SYSERR;
884 if (0 == off) 893 return 1;
885 {
886 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
887 return GNUNET_SYSERR;
888 return 1;
889 }
890 off--;
891 }
892 } 894 }
893 else 895 off--;
896 }
897 }
898 else
899 {
900 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
901 {
902 if (0 == off)
894 { 903 {
895 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 904 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
896 { 905 return GNUNET_SYSERR;
897 if (0 == off) 906 return 1;
898 {
899 if (GNUNET_OK != it(it_cls, &bme->key, bme->value))
900 return GNUNET_SYSERR;
901 return 1;
902 }
903 off--;
904 }
905 } 907 }
908 off--;
909 }
906 } 910 }
907 GNUNET_break(0); 911 }
912 GNUNET_break (0);
908 return GNUNET_SYSERR; 913 return GNUNET_SYSERR;
909} 914}
910 915
@@ -922,12 +927,12 @@ GNUNET_CONTAINER_multishortmap_get_random(
922 * @return an iterator over the given multishortmap 'map' 927 * @return an iterator over the given multishortmap 'map'
923 */ 928 */
924struct GNUNET_CONTAINER_MultiShortmapIterator * 929struct GNUNET_CONTAINER_MultiShortmapIterator *
925GNUNET_CONTAINER_multishortmap_iterator_create( 930GNUNET_CONTAINER_multishortmap_iterator_create (
926 const struct GNUNET_CONTAINER_MultiShortmap *map) 931 const struct GNUNET_CONTAINER_MultiShortmap *map)
927{ 932{
928 struct GNUNET_CONTAINER_MultiShortmapIterator *iter; 933 struct GNUNET_CONTAINER_MultiShortmapIterator *iter;
929 934
930 iter = GNUNET_new(struct GNUNET_CONTAINER_MultiShortmapIterator); 935 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmapIterator);
931 iter->map = map; 936 iter->map = map;
932 iter->modification_counter = map->modification_counter; 937 iter->modification_counter = map->modification_counter;
933 iter->me = map->map[0]; 938 iter->me = map->map[0];
@@ -950,47 +955,47 @@ GNUNET_CONTAINER_multishortmap_iterator_create(
950 * #GNUNET_NO if we are out of elements 955 * #GNUNET_NO if we are out of elements
951 */ 956 */
952int 957int
953GNUNET_CONTAINER_multishortmap_iterator_next( 958GNUNET_CONTAINER_multishortmap_iterator_next (
954 struct GNUNET_CONTAINER_MultiShortmapIterator *iter, 959 struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
955 struct GNUNET_ShortHashCode *key, 960 struct GNUNET_ShortHashCode *key,
956 const void **value) 961 const void **value)
957{ 962{
958 /* make sure the map has not been modified */ 963 /* make sure the map has not been modified */
959 GNUNET_assert(iter->modification_counter == iter->map->modification_counter); 964 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
960 965
961 /* look for the next entry, skipping empty buckets */ 966 /* look for the next entry, skipping empty buckets */
962 while (1) 967 while (1)
968 {
969 if (iter->idx >= iter->map->map_length)
970 return GNUNET_NO;
971 if (GNUNET_YES == iter->map->use_small_entries)
963 { 972 {
964 if (iter->idx >= iter->map->map_length) 973 if (NULL != iter->me.sme)
965 return GNUNET_NO; 974 {
966 if (GNUNET_YES == iter->map->use_small_entries) 975 if (NULL != key)
967 { 976 *key = *iter->me.sme->key;
968 if (NULL != iter->me.sme) 977 if (NULL != value)
969 { 978 *value = iter->me.sme->value;
970 if (NULL != key) 979 iter->me.sme = iter->me.sme->next;
971 *key = *iter->me.sme->key; 980 return GNUNET_YES;
972 if (NULL != value) 981 }
973 *value = iter->me.sme->value; 982 }
974 iter->me.sme = iter->me.sme->next; 983 else
975 return GNUNET_YES; 984 {
976 } 985 if (NULL != iter->me.bme)
977 } 986 {
978 else 987 if (NULL != key)
979 { 988 *key = iter->me.bme->key;
980 if (NULL != iter->me.bme) 989 if (NULL != value)
981 { 990 *value = iter->me.bme->value;
982 if (NULL != key) 991 iter->me.bme = iter->me.bme->next;
983 *key = iter->me.bme->key; 992 return GNUNET_YES;
984 if (NULL != value) 993 }
985 *value = iter->me.bme->value;
986 iter->me.bme = iter->me.bme->next;
987 return GNUNET_YES;
988 }
989 }
990 iter->idx += 1;
991 if (iter->idx < iter->map->map_length)
992 iter->me = iter->map->map[iter->idx];
993 } 994 }
995 iter->idx += 1;
996 if (iter->idx < iter->map->map_length)
997 iter->me = iter->map->map[iter->idx];
998 }
994} 999}
995 1000
996 1001
@@ -1000,10 +1005,10 @@ GNUNET_CONTAINER_multishortmap_iterator_next(
1000 * @param iter the iterator to destroy 1005 * @param iter the iterator to destroy
1001 */ 1006 */
1002void 1007void
1003GNUNET_CONTAINER_multishortmap_iterator_destroy( 1008GNUNET_CONTAINER_multishortmap_iterator_destroy (
1004 struct GNUNET_CONTAINER_MultiShortmapIterator *iter) 1009 struct GNUNET_CONTAINER_MultiShortmapIterator *iter)
1005{ 1010{
1006 GNUNET_free(iter); 1011 GNUNET_free (iter);
1007} 1012}
1008 1013
1009 1014
diff --git a/src/util/container_multiuuidmap.c b/src/util/container_multiuuidmap.c
index 78ed7f2b7..5e896aec2 100644
--- a/src/util/container_multiuuidmap.c
+++ b/src/util/container_multiuuidmap.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29#define LOG(kind, ...) \ 29#define LOG(kind, ...) \
30 GNUNET_log_from(kind, "util-container-multiuuidmap", __VA_ARGS__) 30 GNUNET_log_from (kind, "util-container-multiuuidmap", __VA_ARGS__)
31 31
32/** 32/**
33 * Maximum recursion depth for callbacks of 33 * Maximum recursion depth for callbacks of
@@ -41,7 +41,8 @@
41/** 41/**
42 * An entry in the hash map with the full key. 42 * An entry in the hash map with the full key.
43 */ 43 */
44struct BigMapEntry { 44struct BigMapEntry
45{
45 /** 46 /**
46 * Value of the entry. 47 * Value of the entry.
47 */ 48 */
@@ -62,7 +63,8 @@ struct BigMapEntry {
62/** 63/**
63 * An entry in the hash map with just a pointer to the key. 64 * An entry in the hash map with just a pointer to the key.
64 */ 65 */
65struct SmallMapEntry { 66struct SmallMapEntry
67{
66 /** 68 /**
67 * Value of the entry. 69 * Value of the entry.
68 */ 70 */
@@ -83,7 +85,8 @@ struct SmallMapEntry {
83/** 85/**
84 * Entry in the map. 86 * Entry in the map.
85 */ 87 */
86union MapEntry { 88union MapEntry
89{
87 /** 90 /**
88 * Variant used if map entries only contain a pointer to the key. 91 * Variant used if map entries only contain a pointer to the key.
89 */ 92 */
@@ -99,7 +102,8 @@ union MapEntry {
99/** 102/**
100 * Internal representation of the hash map. 103 * Internal representation of the hash map.
101 */ 104 */
102struct GNUNET_CONTAINER_MultiUuidmap { 105struct GNUNET_CONTAINER_MultiUuidmap
106{
103 /** 107 /**
104 * All of our buckets. 108 * All of our buckets.
105 */ 109 */
@@ -146,7 +150,8 @@ struct GNUNET_CONTAINER_MultiUuidmap {
146 * Cursor into a multiuuidmap. 150 * Cursor into a multiuuidmap.
147 * Allows to enumerate elements asynchronously. 151 * Allows to enumerate elements asynchronously.
148 */ 152 */
149struct GNUNET_CONTAINER_MultiUuidmapIterator { 153struct GNUNET_CONTAINER_MultiUuidmapIterator
154{
150 /** 155 /**
151 * Position in the bucket 'idx' 156 * Position in the bucket 'idx'
152 */ 157 */
@@ -186,18 +191,18 @@ struct GNUNET_CONTAINER_MultiUuidmapIterator {
186 * @return NULL on error 191 * @return NULL on error
187 */ 192 */
188struct GNUNET_CONTAINER_MultiUuidmap * 193struct GNUNET_CONTAINER_MultiUuidmap *
189GNUNET_CONTAINER_multiuuidmap_create(unsigned int len, int do_not_copy_keys) 194GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys)
190{ 195{
191 struct GNUNET_CONTAINER_MultiUuidmap *map; 196 struct GNUNET_CONTAINER_MultiUuidmap *map;
192 197
193 GNUNET_assert(len > 0); 198 GNUNET_assert (len > 0);
194 map = GNUNET_new(struct GNUNET_CONTAINER_MultiUuidmap); 199 map = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmap);
195 map->map = GNUNET_malloc_large(len * sizeof(union MapEntry)); 200 map->map = GNUNET_malloc_large (len * sizeof(union MapEntry));
196 if (NULL == map->map) 201 if (NULL == map->map)
197 { 202 {
198 GNUNET_free(map); 203 GNUNET_free (map);
199 return NULL; 204 return NULL;
200 } 205 }
201 map->map_length = len; 206 map->map_length = len;
202 map->use_small_entries = do_not_copy_keys; 207 map->use_small_entries = do_not_copy_keys;
203 return map; 208 return map;
@@ -211,44 +216,44 @@ GNUNET_CONTAINER_multiuuidmap_create(unsigned int len, int do_not_copy_keys)
211 * @param map the map 216 * @param map the map
212 */ 217 */
213void 218void
214GNUNET_CONTAINER_multiuuidmap_destroy( 219GNUNET_CONTAINER_multiuuidmap_destroy (
215 struct GNUNET_CONTAINER_MultiUuidmap *map) 220 struct GNUNET_CONTAINER_MultiUuidmap *map)
216{ 221{
217 GNUNET_assert(0 == map->next_cache_off); 222 GNUNET_assert (0 == map->next_cache_off);
218 for (unsigned int i = 0; i < map->map_length; i++) 223 for (unsigned int i = 0; i < map->map_length; i++)
219 { 224 {
220 union MapEntry me; 225 union MapEntry me;
221 226
222 me = map->map[i]; 227 me = map->map[i];
223 if (map->use_small_entries) 228 if (map->use_small_entries)
224 { 229 {
225 struct SmallMapEntry *sme; 230 struct SmallMapEntry *sme;
226 struct SmallMapEntry *nxt; 231 struct SmallMapEntry *nxt;
227 232
228 nxt = me.sme; 233 nxt = me.sme;
229 while (NULL != (sme = nxt)) 234 while (NULL != (sme = nxt))
230 { 235 {
231 nxt = sme->next; 236 nxt = sme->next;
232 GNUNET_free(sme); 237 GNUNET_free (sme);
233 } 238 }
234 me.sme = NULL; 239 me.sme = NULL;
235 }
236 else
237 {
238 struct BigMapEntry *bme;
239 struct BigMapEntry *nxt;
240
241 nxt = me.bme;
242 while (NULL != (bme = nxt))
243 {
244 nxt = bme->next;
245 GNUNET_free(bme);
246 }
247 me.bme = NULL;
248 }
249 } 240 }
250 GNUNET_free(map->map); 241 else
251 GNUNET_free(map); 242 {
243 struct BigMapEntry *bme;
244 struct BigMapEntry *nxt;
245
246 nxt = me.bme;
247 while (NULL != (bme = nxt))
248 {
249 nxt = bme->next;
250 GNUNET_free (bme);
251 }
252 me.bme = NULL;
253 }
254 }
255 GNUNET_free (map->map);
256 GNUNET_free (map);
252} 257}
253 258
254 259
@@ -260,13 +265,13 @@ GNUNET_CONTAINER_multiuuidmap_destroy(
260 * @return offset into the "map" array of "map" 265 * @return offset into the "map" array of "map"
261 */ 266 */
262static unsigned int 267static unsigned int
263idx_of(const struct GNUNET_CONTAINER_MultiUuidmap *map, 268idx_of (const struct GNUNET_CONTAINER_MultiUuidmap *map,
264 const struct GNUNET_Uuid *key) 269 const struct GNUNET_Uuid *key)
265{ 270{
266 unsigned int kx; 271 unsigned int kx;
267 272
268 GNUNET_assert(NULL != map); 273 GNUNET_assert (NULL != map);
269 GNUNET_memcpy(&kx, key, sizeof(kx)); 274 GNUNET_memcpy (&kx, key, sizeof(kx));
270 return kx % map->map_length; 275 return kx % map->map_length;
271} 276}
272 277
@@ -278,7 +283,7 @@ idx_of(const struct GNUNET_CONTAINER_MultiUuidmap *map,
278 * @return the number of key value pairs 283 * @return the number of key value pairs
279 */ 284 */
280unsigned int 285unsigned int
281GNUNET_CONTAINER_multiuuidmap_size( 286GNUNET_CONTAINER_multiuuidmap_size (
282 const struct GNUNET_CONTAINER_MultiUuidmap *map) 287 const struct GNUNET_CONTAINER_MultiUuidmap *map)
283{ 288{
284 return map->size; 289 return map->size;
@@ -296,25 +301,25 @@ GNUNET_CONTAINER_multiuuidmap_size(
296 * key-value pairs with value NULL 301 * key-value pairs with value NULL
297 */ 302 */
298void * 303void *
299GNUNET_CONTAINER_multiuuidmap_get( 304GNUNET_CONTAINER_multiuuidmap_get (
300 const struct GNUNET_CONTAINER_MultiUuidmap *map, 305 const struct GNUNET_CONTAINER_MultiUuidmap *map,
301 const struct GNUNET_Uuid *key) 306 const struct GNUNET_Uuid *key)
302{ 307{
303 union MapEntry me; 308 union MapEntry me;
304 309
305 me = map->map[idx_of(map, key)]; 310 me = map->map[idx_of (map, key)];
306 if (map->use_small_entries) 311 if (map->use_small_entries)
307 { 312 {
308 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 313 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
309 if (0 == GNUNET_memcmp(key, sme->key)) 314 if (0 == GNUNET_memcmp (key, sme->key))
310 return sme->value; 315 return sme->value;
311 } 316 }
312 else 317 else
313 { 318 {
314 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 319 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
315 if (0 == GNUNET_memcmp(key, &bme->key)) 320 if (0 == GNUNET_memcmp (key, &bme->key))
316 return bme->value; 321 return bme->value;
317 } 322 }
318 return NULL; 323 return NULL;
319} 324}
320 325
@@ -329,7 +334,7 @@ GNUNET_CONTAINER_multiuuidmap_get(
329 * #GNUNET_SYSERR if it aborted iteration 334 * #GNUNET_SYSERR if it aborted iteration
330 */ 335 */
331int 336int
332GNUNET_CONTAINER_multiuuidmap_iterate( 337GNUNET_CONTAINER_multiuuidmap_iterate (
333 struct GNUNET_CONTAINER_MultiUuidmap *map, 338 struct GNUNET_CONTAINER_MultiUuidmap *map,
334 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, 339 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
335 void *it_cls) 340 void *it_cls)
@@ -340,50 +345,50 @@ GNUNET_CONTAINER_multiuuidmap_iterate(
340 struct GNUNET_Uuid kc; 345 struct GNUNET_Uuid kc;
341 346
342 count = 0; 347 count = 0;
343 GNUNET_assert(NULL != map); 348 GNUNET_assert (NULL != map);
344 ce = &map->next_cache[map->next_cache_off]; 349 ce = &map->next_cache[map->next_cache_off];
345 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 350 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
346 for (unsigned int i = 0; i < map->map_length; i++) 351 for (unsigned int i = 0; i < map->map_length; i++)
352 {
353 me = map->map[i];
354 if (map->use_small_entries)
347 { 355 {
348 me = map->map[i]; 356 struct SmallMapEntry *sme;
349 if (map->use_small_entries) 357
358 ce->sme = me.sme;
359 while (NULL != (sme = ce->sme))
360 {
361 ce->sme = sme->next;
362 if ((NULL != it) && (GNUNET_OK != it (it_cls, sme->key, sme->value)))
350 { 363 {
351 struct SmallMapEntry *sme; 364 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
352 365 return GNUNET_SYSERR;
353 ce->sme = me.sme;
354 while (NULL != (sme = ce->sme))
355 {
356 ce->sme = sme->next;
357 if ((NULL != it) && (GNUNET_OK != it(it_cls, sme->key, sme->value)))
358 {
359 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
360 return GNUNET_SYSERR;
361 }
362 count++;
363 }
364 } 366 }
365 else 367 count++;
368 }
369 }
370 else
371 {
372 struct BigMapEntry *bme;
373
374 ce->bme = me.bme;
375 while (NULL != (bme = ce->bme))
376 {
377 ce->bme = bme->next;
378 if (NULL != it)
366 { 379 {
367 struct BigMapEntry *bme; 380 kc = bme->key;
368 381 if (GNUNET_OK != it (it_cls, &kc, bme->value))
369 ce->bme = me.bme; 382 {
370 while (NULL != (bme = ce->bme)) 383 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
371 { 384 return GNUNET_SYSERR;
372 ce->bme = bme->next; 385 }
373 if (NULL != it)
374 {
375 kc = bme->key;
376 if (GNUNET_OK != it(it_cls, &kc, bme->value))
377 {
378 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
379 return GNUNET_SYSERR;
380 }
381 }
382 count++;
383 }
384 } 386 }
387 count++;
388 }
385 } 389 }
386 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 390 }
391 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
387 return count; 392 return count;
388} 393}
389 394
@@ -396,8 +401,8 @@ GNUNET_CONTAINER_multiuuidmap_iterate(
396 * @param bme the entry that is about to be free'd 401 * @param bme the entry that is about to be free'd
397 */ 402 */
398static void 403static void
399update_next_cache_bme(struct GNUNET_CONTAINER_MultiUuidmap *map, 404update_next_cache_bme (struct GNUNET_CONTAINER_MultiUuidmap *map,
400 const struct BigMapEntry *bme) 405 const struct BigMapEntry *bme)
401{ 406{
402 for (unsigned int i = 0; i < map->next_cache_off; i++) 407 for (unsigned int i = 0; i < map->next_cache_off; i++)
403 if (map->next_cache[i].bme == bme) 408 if (map->next_cache[i].bme == bme)
@@ -413,8 +418,8 @@ update_next_cache_bme(struct GNUNET_CONTAINER_MultiUuidmap *map,
413 * @param sme the entry that is about to be free'd 418 * @param sme the entry that is about to be free'd
414 */ 419 */
415static void 420static void
416update_next_cache_sme(struct GNUNET_CONTAINER_MultiUuidmap *map, 421update_next_cache_sme (struct GNUNET_CONTAINER_MultiUuidmap *map,
417 const struct SmallMapEntry *sme) 422 const struct SmallMapEntry *sme)
418{ 423{
419 for (unsigned int i = 0; i < map->next_cache_off; i++) 424 for (unsigned int i = 0; i < map->next_cache_off; i++)
420 if (map->next_cache[i].sme == sme) 425 if (map->next_cache[i].sme == sme)
@@ -434,56 +439,56 @@ update_next_cache_sme(struct GNUNET_CONTAINER_MultiUuidmap *map,
434 * is not in the map 439 * is not in the map
435 */ 440 */
436int 441int
437GNUNET_CONTAINER_multiuuidmap_remove(struct GNUNET_CONTAINER_MultiUuidmap *map, 442GNUNET_CONTAINER_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map,
438 const struct GNUNET_Uuid *key, 443 const struct GNUNET_Uuid *key,
439 const void *value) 444 const void *value)
440{ 445{
441 union MapEntry me; 446 union MapEntry me;
442 unsigned int i; 447 unsigned int i;
443 448
444 map->modification_counter++; 449 map->modification_counter++;
445 i = idx_of(map, key); 450 i = idx_of (map, key);
446 me = map->map[i]; 451 me = map->map[i];
447 if (map->use_small_entries) 452 if (map->use_small_entries)
448 { 453 {
449 struct SmallMapEntry *p = NULL; 454 struct SmallMapEntry *p = NULL;
450 455
451 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 456 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
452 { 457 {
453 if ((0 == GNUNET_memcmp(key, sme->key)) && (value == sme->value)) 458 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
454 { 459 {
455 if (NULL == p) 460 if (NULL == p)
456 map->map[i].sme = sme->next; 461 map->map[i].sme = sme->next;
457 else 462 else
458 p->next = sme->next; 463 p->next = sme->next;
459 update_next_cache_sme(map, sme); 464 update_next_cache_sme (map, sme);
460 GNUNET_free(sme); 465 GNUNET_free (sme);
461 map->size--; 466 map->size--;
462 return GNUNET_YES; 467 return GNUNET_YES;
463 } 468 }
464 p = sme; 469 p = sme;
465 }
466 } 470 }
471 }
467 else 472 else
468 { 473 {
469 struct BigMapEntry *p = NULL; 474 struct BigMapEntry *p = NULL;
470 475
471 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 476 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
472 { 477 {
473 if ((0 == GNUNET_memcmp(key, &bme->key)) && (value == bme->value)) 478 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
474 { 479 {
475 if (NULL == p) 480 if (NULL == p)
476 map->map[i].bme = bme->next; 481 map->map[i].bme = bme->next;
477 else 482 else
478 p->next = bme->next; 483 p->next = bme->next;
479 update_next_cache_bme(map, bme); 484 update_next_cache_bme (map, bme);
480 GNUNET_free(bme); 485 GNUNET_free (bme);
481 map->size--; 486 map->size--;
482 return GNUNET_YES; 487 return GNUNET_YES;
483 } 488 }
484 p = bme; 489 p = bme;
485 }
486 } 490 }
491 }
487 return GNUNET_NO; 492 return GNUNET_NO;
488} 493}
489 494
@@ -497,7 +502,7 @@ GNUNET_CONTAINER_multiuuidmap_remove(struct GNUNET_CONTAINER_MultiUuidmap *map,
497 * @return number of values removed 502 * @return number of values removed
498 */ 503 */
499int 504int
500GNUNET_CONTAINER_multiuuidmap_remove_all( 505GNUNET_CONTAINER_multiuuidmap_remove_all (
501 struct GNUNET_CONTAINER_MultiUuidmap *map, 506 struct GNUNET_CONTAINER_MultiUuidmap *map,
502 const struct GNUNET_Uuid *key) 507 const struct GNUNET_Uuid *key)
503{ 508{
@@ -508,70 +513,70 @@ GNUNET_CONTAINER_multiuuidmap_remove_all(
508 map->modification_counter++; 513 map->modification_counter++;
509 514
510 ret = 0; 515 ret = 0;
511 i = idx_of(map, key); 516 i = idx_of (map, key);
512 me = map->map[i]; 517 me = map->map[i];
513 if (map->use_small_entries) 518 if (map->use_small_entries)
514 { 519 {
515 struct SmallMapEntry *sme; 520 struct SmallMapEntry *sme;
516 struct SmallMapEntry *p; 521 struct SmallMapEntry *p;
517 522
518 p = NULL; 523 p = NULL;
519 sme = me.sme; 524 sme = me.sme;
520 while (NULL != sme) 525 while (NULL != sme)
521 { 526 {
522 if (0 == GNUNET_memcmp(key, sme->key)) 527 if (0 == GNUNET_memcmp (key, sme->key))
523 { 528 {
524 if (NULL == p) 529 if (NULL == p)
525 map->map[i].sme = sme->next; 530 map->map[i].sme = sme->next;
526 else 531 else
527 p->next = sme->next; 532 p->next = sme->next;
528 update_next_cache_sme(map, sme); 533 update_next_cache_sme (map, sme);
529 GNUNET_free(sme); 534 GNUNET_free (sme);
530 map->size--; 535 map->size--;
531 if (NULL == p) 536 if (NULL == p)
532 sme = map->map[i].sme; 537 sme = map->map[i].sme;
533 else 538 else
534 sme = p->next; 539 sme = p->next;
535 ret++; 540 ret++;
536 } 541 }
537 else 542 else
538 { 543 {
539 p = sme; 544 p = sme;
540 sme = sme->next; 545 sme = sme->next;
541 } 546 }
542 }
543 } 547 }
548 }
544 else 549 else
545 { 550 {
546 struct BigMapEntry *bme; 551 struct BigMapEntry *bme;
547 struct BigMapEntry *p; 552 struct BigMapEntry *p;
548 553
549 p = NULL; 554 p = NULL;
550 bme = me.bme; 555 bme = me.bme;
551 while (NULL != bme) 556 while (NULL != bme)
552 { 557 {
553 if (0 == GNUNET_memcmp(key, &bme->key)) 558 if (0 == GNUNET_memcmp (key, &bme->key))
554 { 559 {
555 if (NULL == p) 560 if (NULL == p)
556 map->map[i].bme = bme->next; 561 map->map[i].bme = bme->next;
557 else 562 else
558 p->next = bme->next; 563 p->next = bme->next;
559 update_next_cache_bme(map, bme); 564 update_next_cache_bme (map, bme);
560 GNUNET_free(bme); 565 GNUNET_free (bme);
561 map->size--; 566 map->size--;
562 if (NULL == p) 567 if (NULL == p)
563 bme = map->map[i].bme; 568 bme = map->map[i].bme;
564 else 569 else
565 bme = p->next; 570 bme = p->next;
566 ret++; 571 ret++;
567 } 572 }
568 else 573 else
569 { 574 {
570 p = bme; 575 p = bme;
571 bme = bme->next; 576 bme = bme->next;
572 } 577 }
573 }
574 } 578 }
579 }
575 return ret; 580 return ret;
576} 581}
577 582
@@ -586,25 +591,25 @@ GNUNET_CONTAINER_multiuuidmap_remove_all(
586 * #GNUNET_NO if not 591 * #GNUNET_NO if not
587 */ 592 */
588int 593int
589GNUNET_CONTAINER_multiuuidmap_contains( 594GNUNET_CONTAINER_multiuuidmap_contains (
590 const struct GNUNET_CONTAINER_MultiUuidmap *map, 595 const struct GNUNET_CONTAINER_MultiUuidmap *map,
591 const struct GNUNET_Uuid *key) 596 const struct GNUNET_Uuid *key)
592{ 597{
593 union MapEntry me; 598 union MapEntry me;
594 599
595 me = map->map[idx_of(map, key)]; 600 me = map->map[idx_of (map, key)];
596 if (map->use_small_entries) 601 if (map->use_small_entries)
597 { 602 {
598 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 603 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
599 if (0 == GNUNET_memcmp(key, sme->key)) 604 if (0 == GNUNET_memcmp (key, sme->key))
600 return GNUNET_YES; 605 return GNUNET_YES;
601 } 606 }
602 else 607 else
603 { 608 {
604 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 609 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
605 if (0 == GNUNET_memcmp(key, &bme->key)) 610 if (0 == GNUNET_memcmp (key, &bme->key))
606 return GNUNET_YES; 611 return GNUNET_YES;
607 } 612 }
608 return GNUNET_NO; 613 return GNUNET_NO;
609} 614}
610 615
@@ -620,26 +625,26 @@ GNUNET_CONTAINER_multiuuidmap_contains(
620 * #GNUNET_NO if not 625 * #GNUNET_NO if not
621 */ 626 */
622int 627int
623GNUNET_CONTAINER_multiuuidmap_contains_value( 628GNUNET_CONTAINER_multiuuidmap_contains_value (
624 const struct GNUNET_CONTAINER_MultiUuidmap *map, 629 const struct GNUNET_CONTAINER_MultiUuidmap *map,
625 const struct GNUNET_Uuid *key, 630 const struct GNUNET_Uuid *key,
626 const void *value) 631 const void *value)
627{ 632{
628 union MapEntry me; 633 union MapEntry me;
629 634
630 me = map->map[idx_of(map, key)]; 635 me = map->map[idx_of (map, key)];
631 if (map->use_small_entries) 636 if (map->use_small_entries)
632 { 637 {
633 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 638 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
634 if ((0 == GNUNET_memcmp(key, sme->key)) && (sme->value == value)) 639 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
635 return GNUNET_YES; 640 return GNUNET_YES;
636 } 641 }
637 else 642 else
638 { 643 {
639 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 644 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
640 if ((0 == GNUNET_memcmp(key, &bme->key)) && (bme->value == value)) 645 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
641 return GNUNET_YES; 646 return GNUNET_YES;
642 } 647 }
643 return GNUNET_NO; 648 return GNUNET_NO;
644} 649}
645 650
@@ -650,7 +655,7 @@ GNUNET_CONTAINER_multiuuidmap_contains_value(
650 * @param map the hash map to grow 655 * @param map the hash map to grow
651 */ 656 */
652static void 657static void
653grow(struct GNUNET_CONTAINER_MultiUuidmap *map) 658grow (struct GNUNET_CONTAINER_MultiUuidmap *map)
654{ 659{
655 union MapEntry *old_map; 660 union MapEntry *old_map;
656 union MapEntry *new_map; 661 union MapEntry *new_map;
@@ -665,40 +670,40 @@ grow(struct GNUNET_CONTAINER_MultiUuidmap *map)
665 new_len = old_len; /* never use 0 */ 670 new_len = old_len; /* never use 0 */
666 if (new_len == old_len) 671 if (new_len == old_len)
667 return; /* nothing changed */ 672 return; /* nothing changed */
668 new_map = GNUNET_malloc_large(new_len * sizeof(union MapEntry)); 673 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
669 if (NULL == new_map) 674 if (NULL == new_map)
670 return; /* grow not possible */ 675 return; /* grow not possible */
671 map->modification_counter++; 676 map->modification_counter++;
672 map->map_length = new_len; 677 map->map_length = new_len;
673 map->map = new_map; 678 map->map = new_map;
674 for (unsigned int i = 0; i < old_len; i++) 679 for (unsigned int i = 0; i < old_len; i++)
680 {
681 if (map->use_small_entries)
675 { 682 {
676 if (map->use_small_entries) 683 struct SmallMapEntry *sme;
677 { 684
678 struct SmallMapEntry *sme; 685 while (NULL != (sme = old_map[i].sme))
679 686 {
680 while (NULL != (sme = old_map[i].sme)) 687 old_map[i].sme = sme->next;
681 { 688 idx = idx_of (map, sme->key);
682 old_map[i].sme = sme->next; 689 sme->next = new_map[idx].sme;
683 idx = idx_of(map, sme->key); 690 new_map[idx].sme = sme;
684 sme->next = new_map[idx].sme; 691 }
685 new_map[idx].sme = sme;
686 }
687 }
688 else
689 {
690 struct BigMapEntry *bme;
691
692 while (NULL != (bme = old_map[i].bme))
693 {
694 old_map[i].bme = bme->next;
695 idx = idx_of(map, &bme->key);
696 bme->next = new_map[idx].bme;
697 new_map[idx].bme = bme;
698 }
699 }
700 } 692 }
701 GNUNET_free(old_map); 693 else
694 {
695 struct BigMapEntry *bme;
696
697 while (NULL != (bme = old_map[i].bme))
698 {
699 old_map[i].bme = bme->next;
700 idx = idx_of (map, &bme->key);
701 bme->next = new_map[idx].bme;
702 new_map[idx].bme = bme;
703 }
704 }
705 }
706 GNUNET_free (old_map);
702} 707}
703 708
704 709
@@ -715,67 +720,67 @@ grow(struct GNUNET_CONTAINER_MultiUuidmap *map)
715 * value already exists 720 * value already exists
716 */ 721 */
717int 722int
718GNUNET_CONTAINER_multiuuidmap_put(struct GNUNET_CONTAINER_MultiUuidmap *map, 723GNUNET_CONTAINER_multiuuidmap_put (struct GNUNET_CONTAINER_MultiUuidmap *map,
719 const struct GNUNET_Uuid *key, 724 const struct GNUNET_Uuid *key,
720 void *value, 725 void *value,
721 enum GNUNET_CONTAINER_MultiHashMapOption opt) 726 enum GNUNET_CONTAINER_MultiHashMapOption opt)
722{ 727{
723 union MapEntry me; 728 union MapEntry me;
724 unsigned int i; 729 unsigned int i;
725 730
726 i = idx_of(map, key); 731 i = idx_of (map, key);
727 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 732 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
728 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 733 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
734 {
735 me = map->map[i];
736 if (map->use_small_entries)
729 { 737 {
730 me = map->map[i]; 738 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
731 if (map->use_small_entries) 739 if (0 == GNUNET_memcmp (key, sme->key))
732 { 740 {
733 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 741 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
734 if (0 == GNUNET_memcmp(key, sme->key)) 742 return GNUNET_SYSERR;
735 { 743 sme->value = value;
736 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 744 return GNUNET_NO;
737 return GNUNET_SYSERR;
738 sme->value = value;
739 return GNUNET_NO;
740 }
741 } 745 }
742 else 746 }
747 else
748 {
749 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
750 if (0 == GNUNET_memcmp (key, &bme->key))
743 { 751 {
744 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 752 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
745 if (0 == GNUNET_memcmp(key, &bme->key)) 753 return GNUNET_SYSERR;
746 { 754 bme->value = value;
747 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 755 return GNUNET_NO;
748 return GNUNET_SYSERR;
749 bme->value = value;
750 return GNUNET_NO;
751 }
752 } 756 }
753 } 757 }
758 }
754 if (map->size / 3 >= map->map_length / 4) 759 if (map->size / 3 >= map->map_length / 4)
755 { 760 {
756 grow(map); 761 grow (map);
757 i = idx_of(map, key); 762 i = idx_of (map, key);
758 } 763 }
759 if (map->use_small_entries) 764 if (map->use_small_entries)
760 { 765 {
761 struct SmallMapEntry *sme; 766 struct SmallMapEntry *sme;
762 767
763 sme = GNUNET_new(struct SmallMapEntry); 768 sme = GNUNET_new (struct SmallMapEntry);
764 sme->key = key; 769 sme->key = key;
765 sme->value = value; 770 sme->value = value;
766 sme->next = map->map[i].sme; 771 sme->next = map->map[i].sme;
767 map->map[i].sme = sme; 772 map->map[i].sme = sme;
768 } 773 }
769 else 774 else
770 { 775 {
771 struct BigMapEntry *bme; 776 struct BigMapEntry *bme;
772 777
773 bme = GNUNET_new(struct BigMapEntry); 778 bme = GNUNET_new (struct BigMapEntry);
774 bme->key = *key; 779 bme->key = *key;
775 bme->value = value; 780 bme->value = value;
776 bme->next = map->map[i].bme; 781 bme->next = map->map[i].bme;
777 map->map[i].bme = bme; 782 map->map[i].bme = bme;
778 } 783 }
779 map->size++; 784 map->size++;
780 return GNUNET_OK; 785 return GNUNET_OK;
781} 786}
@@ -792,7 +797,7 @@ GNUNET_CONTAINER_multiuuidmap_put(struct GNUNET_CONTAINER_MultiUuidmap *map,
792 * #GNUNET_SYSERR if it aborted iteration 797 * #GNUNET_SYSERR if it aborted iteration
793 */ 798 */
794int 799int
795GNUNET_CONTAINER_multiuuidmap_get_multiple( 800GNUNET_CONTAINER_multiuuidmap_get_multiple (
796 struct GNUNET_CONTAINER_MultiUuidmap *map, 801 struct GNUNET_CONTAINER_MultiUuidmap *map,
797 const struct GNUNET_Uuid *key, 802 const struct GNUNET_Uuid *key,
798 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, 803 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
@@ -803,46 +808,46 @@ GNUNET_CONTAINER_multiuuidmap_get_multiple(
803 union MapEntry *ce; 808 union MapEntry *ce;
804 809
805 ce = &map->next_cache[map->next_cache_off]; 810 ce = &map->next_cache[map->next_cache_off];
806 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE); 811 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
807 count = 0; 812 count = 0;
808 me = map->map[idx_of(map, key)]; 813 me = map->map[idx_of (map, key)];
809 if (map->use_small_entries) 814 if (map->use_small_entries)
810 { 815 {
811 struct SmallMapEntry *sme; 816 struct SmallMapEntry *sme;
812 817
813 ce->sme = me.sme; 818 ce->sme = me.sme;
814 while (NULL != (sme = ce->sme)) 819 while (NULL != (sme = ce->sme))
815 { 820 {
816 ce->sme = sme->next; 821 ce->sme = sme->next;
817 if (0 != GNUNET_memcmp(key, sme->key)) 822 if (0 != GNUNET_memcmp (key, sme->key))
818 continue; 823 continue;
819 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, sme->value))) 824 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
820 { 825 {
821 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 826 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
822 return GNUNET_SYSERR; 827 return GNUNET_SYSERR;
823 } 828 }
824 count++; 829 count++;
825 }
826 } 830 }
831 }
827 else 832 else
828 { 833 {
829 struct BigMapEntry *bme; 834 struct BigMapEntry *bme;
830 835
831 ce->bme = me.bme; 836 ce->bme = me.bme;
832 while (NULL != (bme = ce->bme)) 837 while (NULL != (bme = ce->bme))
833 { 838 {
834 ce->bme = bme->next; 839 ce->bme = bme->next;
835 if (0 != GNUNET_memcmp(key, &bme->key)) 840 if (0 != GNUNET_memcmp (key, &bme->key))
836 continue; 841 continue;
837 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, bme->value))) 842 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
838 { 843 {
839 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 844 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
840 return GNUNET_SYSERR; 845 return GNUNET_SYSERR;
841 } 846 }
842 count++; 847 count++;
843 }
844 } 848 }
845 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE); 849 }
850 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
846 return count; 851 return count;
847} 852}
848 853
@@ -859,7 +864,7 @@ GNUNET_CONTAINER_multiuuidmap_get_multiple(
859 * @return the number of key value pairs processed, zero or one. 864 * @return the number of key value pairs processed, zero or one.
860 */ 865 */
861unsigned int 866unsigned int
862GNUNET_CONTAINER_multiuuidmap_get_random( 867GNUNET_CONTAINER_multiuuidmap_get_random (
863 const struct GNUNET_CONTAINER_MultiUuidmap *map, 868 const struct GNUNET_CONTAINER_MultiUuidmap *map,
864 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, 869 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
865 void *it_cls) 870 void *it_cls)
@@ -871,38 +876,38 @@ GNUNET_CONTAINER_multiuuidmap_get_random(
871 return 0; 876 return 0;
872 if (NULL == it) 877 if (NULL == it)
873 return 1; 878 return 1;
874 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, map->size); 879 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
875 for (unsigned int idx = 0; idx < map->map_length; idx++) 880 for (unsigned int idx = 0; idx < map->map_length; idx++)
881 {
882 me = map->map[idx];
883 if (map->use_small_entries)
876 { 884 {
877 me = map->map[idx]; 885 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
878 if (map->use_small_entries) 886 {
887 if (0 == off)
879 { 888 {
880 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 889 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
881 { 890 return GNUNET_SYSERR;
882 if (0 == off) 891 return 1;
883 {
884 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
885 return GNUNET_SYSERR;
886 return 1;
887 }
888 off--;
889 }
890 } 892 }
891 else 893 off--;
894 }
895 }
896 else
897 {
898 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
899 {
900 if (0 == off)
892 { 901 {
893 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 902 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
894 { 903 return GNUNET_SYSERR;
895 if (0 == off) 904 return 1;
896 {
897 if (GNUNET_OK != it(it_cls, &bme->key, bme->value))
898 return GNUNET_SYSERR;
899 return 1;
900 }
901 off--;
902 }
903 } 905 }
906 off--;
907 }
904 } 908 }
905 GNUNET_break(0); 909 }
910 GNUNET_break (0);
906 return GNUNET_SYSERR; 911 return GNUNET_SYSERR;
907} 912}
908 913
@@ -920,12 +925,12 @@ GNUNET_CONTAINER_multiuuidmap_get_random(
920 * @return an iterator over the given multiuuidmap 'map' 925 * @return an iterator over the given multiuuidmap 'map'
921 */ 926 */
922struct GNUNET_CONTAINER_MultiUuidmapIterator * 927struct GNUNET_CONTAINER_MultiUuidmapIterator *
923GNUNET_CONTAINER_multiuuidmap_iterator_create( 928GNUNET_CONTAINER_multiuuidmap_iterator_create (
924 const struct GNUNET_CONTAINER_MultiUuidmap *map) 929 const struct GNUNET_CONTAINER_MultiUuidmap *map)
925{ 930{
926 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter; 931 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter;
927 932
928 iter = GNUNET_new(struct GNUNET_CONTAINER_MultiUuidmapIterator); 933 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmapIterator);
929 iter->map = map; 934 iter->map = map;
930 iter->modification_counter = map->modification_counter; 935 iter->modification_counter = map->modification_counter;
931 iter->me = map->map[0]; 936 iter->me = map->map[0];
@@ -948,47 +953,47 @@ GNUNET_CONTAINER_multiuuidmap_iterator_create(
948 * #GNUNET_NO if we are out of elements 953 * #GNUNET_NO if we are out of elements
949 */ 954 */
950int 955int
951GNUNET_CONTAINER_multiuuidmap_iterator_next( 956GNUNET_CONTAINER_multiuuidmap_iterator_next (
952 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter, 957 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter,
953 struct GNUNET_Uuid *key, 958 struct GNUNET_Uuid *key,
954 const void **value) 959 const void **value)
955{ 960{
956 /* make sure the map has not been modified */ 961 /* make sure the map has not been modified */
957 GNUNET_assert(iter->modification_counter == iter->map->modification_counter); 962 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
958 963
959 /* look for the next entry, skipping empty buckets */ 964 /* look for the next entry, skipping empty buckets */
960 while (1) 965 while (1)
966 {
967 if (iter->idx >= iter->map->map_length)
968 return GNUNET_NO;
969 if (GNUNET_YES == iter->map->use_small_entries)
961 { 970 {
962 if (iter->idx >= iter->map->map_length) 971 if (NULL != iter->me.sme)
963 return GNUNET_NO; 972 {
964 if (GNUNET_YES == iter->map->use_small_entries) 973 if (NULL != key)
965 { 974 *key = *iter->me.sme->key;
966 if (NULL != iter->me.sme) 975 if (NULL != value)
967 { 976 *value = iter->me.sme->value;
968 if (NULL != key) 977 iter->me.sme = iter->me.sme->next;
969 *key = *iter->me.sme->key; 978 return GNUNET_YES;
970 if (NULL != value) 979 }
971 *value = iter->me.sme->value; 980 }
972 iter->me.sme = iter->me.sme->next; 981 else
973 return GNUNET_YES; 982 {
974 } 983 if (NULL != iter->me.bme)
975 } 984 {
976 else 985 if (NULL != key)
977 { 986 *key = iter->me.bme->key;
978 if (NULL != iter->me.bme) 987 if (NULL != value)
979 { 988 *value = iter->me.bme->value;
980 if (NULL != key) 989 iter->me.bme = iter->me.bme->next;
981 *key = iter->me.bme->key; 990 return GNUNET_YES;
982 if (NULL != value) 991 }
983 *value = iter->me.bme->value;
984 iter->me.bme = iter->me.bme->next;
985 return GNUNET_YES;
986 }
987 }
988 iter->idx += 1;
989 if (iter->idx < iter->map->map_length)
990 iter->me = iter->map->map[iter->idx];
991 } 992 }
993 iter->idx += 1;
994 if (iter->idx < iter->map->map_length)
995 iter->me = iter->map->map[iter->idx];
996 }
992} 997}
993 998
994 999
@@ -998,10 +1003,10 @@ GNUNET_CONTAINER_multiuuidmap_iterator_next(
998 * @param iter the iterator to destroy 1003 * @param iter the iterator to destroy
999 */ 1004 */
1000void 1005void
1001GNUNET_CONTAINER_multiuuidmap_iterator_destroy( 1006GNUNET_CONTAINER_multiuuidmap_iterator_destroy (
1002 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter) 1007 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter)
1003{ 1008{
1004 GNUNET_free(iter); 1009 GNUNET_free (iter);
1005} 1010}
1006 1011
1007 1012
diff --git a/src/util/crypto_abe.c b/src/util/crypto_abe.c
index d702738de..1fe648e5a 100644
--- a/src/util/crypto_abe.c
+++ b/src/util/crypto_abe.c
@@ -32,65 +32,67 @@
32 32
33#include "gnunet_crypto_lib.h" 33#include "gnunet_crypto_lib.h"
34 34
35struct GNUNET_CRYPTO_AbeMasterKey { 35struct GNUNET_CRYPTO_AbeMasterKey
36 gabe_pub_t* pub; 36{
37 gabe_msk_t* msk; 37 gabe_pub_t*pub;
38 gabe_msk_t*msk;
38}; 39};
39 40
40struct GNUNET_CRYPTO_AbeKey { 41struct GNUNET_CRYPTO_AbeKey
41 gabe_pub_t* pub; 42{
42 gabe_prv_t* prv; 43 gabe_pub_t*pub;
44 gabe_prv_t*prv;
43}; 45};
44 46
45static int 47static int
46init_aes(element_t k, int enc, 48init_aes (element_t k, int enc,
47 gcry_cipher_hd_t* handle, 49 gcry_cipher_hd_t*handle,
48 struct GNUNET_CRYPTO_SymmetricSessionKey *key, 50 struct GNUNET_CRYPTO_SymmetricSessionKey *key,
49 unsigned char* iv) 51 unsigned char*iv)
50{ 52{
51 int rc; 53 int rc;
52 int key_len; 54 int key_len;
53 unsigned char* key_buf; 55 unsigned char*key_buf;
54 56
55 key_len = element_length_in_bytes(k) < 33 ? 3 : element_length_in_bytes(k); 57 key_len = element_length_in_bytes (k) < 33 ? 3 : element_length_in_bytes (k);
56 key_buf = (unsigned char*)malloc(key_len); 58 key_buf = (unsigned char*) malloc (key_len);
57 element_to_bytes(key_buf, k); 59 element_to_bytes (key_buf, k);
58 60
59 GNUNET_memcpy(key->aes_key, 61 GNUNET_memcpy (key->aes_key,
60 key_buf, 62 key_buf,
61 GNUNET_CRYPTO_AES_KEY_LENGTH); 63 GNUNET_CRYPTO_AES_KEY_LENGTH);
62 GNUNET_assert(0 == 64 GNUNET_assert (0 ==
63 gcry_cipher_open(handle, GCRY_CIPHER_AES256, 65 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
64 GCRY_CIPHER_MODE_CFB, 0)); 66 GCRY_CIPHER_MODE_CFB, 0));
65 rc = gcry_cipher_setkey(*handle, 67 rc = gcry_cipher_setkey (*handle,
66 key->aes_key, 68 key->aes_key,
67 sizeof(key->aes_key)); 69 sizeof(key->aes_key));
68 GNUNET_assert((0 == rc) || ((char)rc == GPG_ERR_WEAK_KEY)); 70 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
69 memset(iv, 0, 16); //TODO make reasonable 71 memset (iv, 0, 16); // TODO make reasonable
70 rc = gcry_cipher_setiv(*handle, 72 rc = gcry_cipher_setiv (*handle,
71 iv, 73 iv,
72 16); 74 16);
73 GNUNET_assert((0 == rc) || ((char)rc == GPG_ERR_WEAK_KEY)); 75 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
74 76
75 free(key_buf); 77 free (key_buf);
76 return rc; 78 return rc;
77} 79}
78 80
79static int 81static int
80aes_128_cbc_encrypt(char* pt, 82aes_128_cbc_encrypt (char*pt,
81 int size, 83 int size,
82 element_t k, 84 element_t k,
83 char **ct) 85 char **ct)
84{ 86{
85 gcry_cipher_hd_t handle; 87 gcry_cipher_hd_t handle;
86 struct GNUNET_CRYPTO_SymmetricSessionKey skey; 88 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
87 unsigned char iv[16]; 89 unsigned char iv[16];
88 char* buf; 90 char*buf;
89 int padding; 91 int padding;
90 int buf_size; 92 int buf_size;
91 uint8_t len[4]; 93 uint8_t len[4];
92 94
93 init_aes(k, 1, &handle, &skey, iv); 95 init_aes (k, 1, &handle, &skey, iv);
94 96
95 /* TODO make less crufty */ 97 /* TODO make less crufty */
96 98
@@ -101,37 +103,38 @@ aes_128_cbc_encrypt(char* pt,
101 len[3] = (size & 0xff) >> 0; 103 len[3] = (size & 0xff) >> 0;
102 padding = 16 - ((4 + size) % 16); 104 padding = 16 - ((4 + size) % 16);
103 buf_size = 4 + size + padding; 105 buf_size = 4 + size + padding;
104 buf = GNUNET_malloc(buf_size); 106 buf = GNUNET_malloc (buf_size);
105 GNUNET_memcpy(buf, len, 4); 107 GNUNET_memcpy (buf, len, 4);
106 GNUNET_memcpy(buf + 4, pt, size); 108 GNUNET_memcpy (buf + 4, pt, size);
107 *ct = GNUNET_malloc(buf_size); 109 *ct = GNUNET_malloc (buf_size);
108 110
109 GNUNET_assert(0 == gcry_cipher_encrypt(handle, *ct, buf_size, buf, buf_size)); 111 GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf,
110 gcry_cipher_close(handle); 112 buf_size));
111 //AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT); 113 gcry_cipher_close (handle);
112 GNUNET_free(buf); 114 // AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
115 GNUNET_free (buf);
113 return buf_size; 116 return buf_size;
114} 117}
115 118
116static int 119static int
117aes_128_cbc_decrypt(char* ct, 120aes_128_cbc_decrypt (char*ct,
118 int size, 121 int size,
119 element_t k, 122 element_t k,
120 char **pt) 123 char **pt)
121{ 124{
122 struct GNUNET_CRYPTO_SymmetricSessionKey skey; 125 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
123 gcry_cipher_hd_t handle; 126 gcry_cipher_hd_t handle;
124 unsigned char iv[16]; 127 unsigned char iv[16];
125 char* tmp; 128 char*tmp;
126 uint32_t len; 129 uint32_t len;
127 130
128 init_aes(k, 1, &handle, &skey, iv); 131 init_aes (k, 1, &handle, &skey, iv);
129 132
130 tmp = GNUNET_malloc(size); 133 tmp = GNUNET_malloc (size);
131 134
132 //AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT); 135 // AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
133 GNUNET_assert(0 == gcry_cipher_decrypt(handle, tmp, size, ct, size)); 136 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
134 gcry_cipher_close(handle); 137 gcry_cipher_close (handle);
135 /* TODO make less crufty */ 138 /* TODO make less crufty */
136 139
137 /* get real length */ 140 /* get real length */
@@ -140,194 +143,195 @@ aes_128_cbc_decrypt(char* ct,
140 | ((tmp[0]) << 24) | ((tmp[1]) << 16) 143 | ((tmp[0]) << 24) | ((tmp[1]) << 16)
141 | ((tmp[2]) << 8) | ((tmp[3]) << 0); 144 | ((tmp[2]) << 8) | ((tmp[3]) << 0);
142 /* truncate any garbage from the padding */ 145 /* truncate any garbage from the padding */
143 *pt = GNUNET_malloc(len); 146 *pt = GNUNET_malloc (len);
144 GNUNET_memcpy(*pt, tmp + 4, len); 147 GNUNET_memcpy (*pt, tmp + 4, len);
145 GNUNET_free(tmp); 148 GNUNET_free (tmp);
146 return len; 149 return len;
147} 150}
148 151
149struct GNUNET_CRYPTO_AbeMasterKey* 152struct GNUNET_CRYPTO_AbeMasterKey*
150GNUNET_CRYPTO_cpabe_create_master_key(void) 153GNUNET_CRYPTO_cpabe_create_master_key (void)
151{ 154{
152 struct GNUNET_CRYPTO_AbeMasterKey* key; 155 struct GNUNET_CRYPTO_AbeMasterKey*key;
153 156
154 key = GNUNET_new(struct GNUNET_CRYPTO_AbeMasterKey); 157 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
155 gabe_setup(&key->pub, &key->msk); 158 gabe_setup (&key->pub, &key->msk);
156 GNUNET_assert(NULL != key->pub); 159 GNUNET_assert (NULL != key->pub);
157 GNUNET_assert(NULL != key->msk); 160 GNUNET_assert (NULL != key->msk);
158 return key; 161 return key;
159} 162}
160 163
161void 164void
162GNUNET_CRYPTO_cpabe_delete_master_key(struct GNUNET_CRYPTO_AbeMasterKey *key) 165GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key)
163{ 166{
164 gabe_msk_free(key->msk); 167 gabe_msk_free (key->msk);
165 gabe_pub_free(key->pub); 168 gabe_pub_free (key->pub);
166 //GNUNET_free (key->msk); 169 // GNUNET_free (key->msk);
167 //gabe_msk_free (key->msk); //For some reason free of pub implicit? 170 // gabe_msk_free (key->msk); //For some reason free of pub implicit?
168 GNUNET_free(key); 171 GNUNET_free (key);
169} 172}
170 173
171struct GNUNET_CRYPTO_AbeKey* 174struct GNUNET_CRYPTO_AbeKey*
172GNUNET_CRYPTO_cpabe_create_key(struct GNUNET_CRYPTO_AbeMasterKey *key, 175GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key,
173 char **attrs) 176 char **attrs)
174{ 177{
175 struct GNUNET_CRYPTO_AbeKey *prv_key; 178 struct GNUNET_CRYPTO_AbeKey *prv_key;
176 int size; 179 int size;
177 char *tmp; 180 char *tmp;
178 181
179 prv_key = GNUNET_new(struct GNUNET_CRYPTO_AbeKey); 182 prv_key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
180 prv_key->prv = gabe_keygen(key->pub, key->msk, attrs); 183 prv_key->prv = gabe_keygen (key->pub, key->msk, attrs);
181 size = gabe_pub_serialize(key->pub, &tmp); 184 size = gabe_pub_serialize (key->pub, &tmp);
182 prv_key->pub = gabe_pub_unserialize(tmp, size); 185 prv_key->pub = gabe_pub_unserialize (tmp, size);
183 GNUNET_free(tmp); 186 GNUNET_free (tmp);
184 GNUNET_assert(NULL != prv_key->prv); 187 GNUNET_assert (NULL != prv_key->prv);
185 return prv_key; 188 return prv_key;
186} 189}
187 190
188void 191void
189GNUNET_CRYPTO_cpabe_delete_key(struct GNUNET_CRYPTO_AbeKey *key, 192GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key,
190 int delete_pub) 193 int delete_pub)
191{ 194{
192 //Memory management in gabe is buggy 195 // Memory management in gabe is buggy
193 gabe_prv_free(key->prv); 196 gabe_prv_free (key->prv);
194 if (GNUNET_YES == delete_pub) 197 if (GNUNET_YES == delete_pub)
195 gabe_pub_free(key->pub); 198 gabe_pub_free (key->pub);
196 GNUNET_free(key); 199 GNUNET_free (key);
197} 200}
198 201
199ssize_t 202ssize_t
200write_cpabe(void **result, 203write_cpabe (void **result,
201 uint32_t file_len, 204 uint32_t file_len,
202 char* cph_buf, 205 char*cph_buf,
203 int cph_buf_len, 206 int cph_buf_len,
204 char* aes_buf, 207 char*aes_buf,
205 int aes_buf_len) 208 int aes_buf_len)
206{ 209{
207 char *ptr; 210 char *ptr;
208 uint32_t *len; 211 uint32_t *len;
209 212
210 *result = GNUNET_malloc(12 + cph_buf_len + aes_buf_len); 213 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
211 ptr = *result; 214 ptr = *result;
212 len = (uint32_t*)ptr; 215 len = (uint32_t*) ptr;
213 *len = htonl(file_len); 216 *len = htonl (file_len);
214 ptr += 4; 217 ptr += 4;
215 len = (uint32_t*)ptr; 218 len = (uint32_t*) ptr;
216 *len = htonl(aes_buf_len); 219 *len = htonl (aes_buf_len);
217 ptr += 4; 220 ptr += 4;
218 GNUNET_memcpy(ptr, aes_buf, aes_buf_len); 221 GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
219 ptr += aes_buf_len; 222 ptr += aes_buf_len;
220 len = (uint32_t*)ptr; 223 len = (uint32_t*) ptr;
221 *len = htonl(cph_buf_len); 224 *len = htonl (cph_buf_len);
222 ptr += 4; 225 ptr += 4;
223 GNUNET_memcpy(ptr, cph_buf, cph_buf_len); 226 GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
224 return 12 + cph_buf_len + aes_buf_len; 227 return 12 + cph_buf_len + aes_buf_len;
225} 228}
226 229
227ssize_t 230ssize_t
228read_cpabe(const void *data, 231read_cpabe (const void *data,
229 char** cph_buf, 232 char**cph_buf,
230 int *cph_buf_len, 233 int *cph_buf_len,
231 char** aes_buf, 234 char**aes_buf,
232 int *aes_buf_len) 235 int *aes_buf_len)
233{ 236{
234 int buf_len; 237 int buf_len;
235 char *ptr; 238 char *ptr;
236 uint32_t *len; 239 uint32_t *len;
237 240
238 ptr = (char*)data; 241 ptr = (char*) data;
239 len = (uint32_t*)ptr; 242 len = (uint32_t*) ptr;
240 buf_len = ntohl(*len); 243 buf_len = ntohl (*len);
241 ptr += 4; 244 ptr += 4;
242 len = (uint32_t*)ptr; 245 len = (uint32_t*) ptr;
243 *aes_buf_len = ntohl(*len); 246 *aes_buf_len = ntohl (*len);
244 ptr += 4; 247 ptr += 4;
245 *aes_buf = GNUNET_malloc(*aes_buf_len); 248 *aes_buf = GNUNET_malloc (*aes_buf_len);
246 GNUNET_memcpy(*aes_buf, ptr, *aes_buf_len); 249 GNUNET_memcpy (*aes_buf, ptr, *aes_buf_len);
247 ptr += *aes_buf_len; 250 ptr += *aes_buf_len;
248 len = (uint32_t*)ptr; 251 len = (uint32_t*) ptr;
249 *cph_buf_len = ntohl(*len); 252 *cph_buf_len = ntohl (*len);
250 ptr += 4; 253 ptr += 4;
251 *cph_buf = GNUNET_malloc(*cph_buf_len); 254 *cph_buf = GNUNET_malloc (*cph_buf_len);
252 GNUNET_memcpy(*cph_buf, ptr, *cph_buf_len); 255 GNUNET_memcpy (*cph_buf, ptr, *cph_buf_len);
253 256
254 return buf_len; 257 return buf_len;
255} 258}
256 259
257ssize_t 260ssize_t
258GNUNET_CRYPTO_cpabe_encrypt(const void *block, 261GNUNET_CRYPTO_cpabe_encrypt (const void *block,
259 size_t size, 262 size_t size,
260 const char *policy, 263 const char *policy,
261 const struct GNUNET_CRYPTO_AbeMasterKey *key, 264 const struct GNUNET_CRYPTO_AbeMasterKey *key,
262 void **result) 265 void **result)
263{ 266{
264 gabe_cph_t* cph; 267 gabe_cph_t*cph;
265 char* plt; 268 char*plt;
266 char* cph_buf; 269 char*cph_buf;
267 char* aes_buf; 270 char*aes_buf;
268 element_t m; 271 element_t m;
269 int cph_buf_len; 272 int cph_buf_len;
270 int aes_buf_len; 273 int aes_buf_len;
271 ssize_t result_len; 274 ssize_t result_len;
272 275
273 if (!(cph = gabe_enc(key->pub, m, (char*)policy))) 276 if (! (cph = gabe_enc (key->pub, m, (char*) policy)))
274 return GNUNET_SYSERR; 277 return GNUNET_SYSERR;
275 cph_buf_len = gabe_cph_serialize(cph, 278 cph_buf_len = gabe_cph_serialize (cph,
276 &cph_buf); 279 &cph_buf);
277 gabe_cph_free(cph); 280 gabe_cph_free (cph);
278 GNUNET_free(cph); 281 GNUNET_free (cph);
279 plt = GNUNET_memdup(block, size); 282 plt = GNUNET_memdup (block, size);
280 aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf); 283 aes_buf_len = aes_128_cbc_encrypt (plt, size, m, &aes_buf);
281 GNUNET_free(plt); 284 GNUNET_free (plt);
282 element_clear(m); 285 element_clear (m);
283 result_len = write_cpabe(result, size, cph_buf, cph_buf_len, aes_buf, aes_buf_len); 286 result_len = write_cpabe (result, size, cph_buf, cph_buf_len, aes_buf,
284 GNUNET_free(cph_buf); 287 aes_buf_len);
285 GNUNET_free(aes_buf); 288 GNUNET_free (cph_buf);
289 GNUNET_free (aes_buf);
286 return result_len; 290 return result_len;
287} 291}
288 292
289ssize_t 293ssize_t
290GNUNET_CRYPTO_cpabe_decrypt(const void *block, 294GNUNET_CRYPTO_cpabe_decrypt (const void *block,
291 size_t size, 295 size_t size,
292 const struct GNUNET_CRYPTO_AbeKey *key, 296 const struct GNUNET_CRYPTO_AbeKey *key,
293 void **result) 297 void **result)
294{ 298{
295 char* aes_buf; 299 char*aes_buf;
296 char* cph_buf; 300 char*cph_buf;
297 gabe_cph_t* cph; 301 gabe_cph_t*cph;
298 element_t m; 302 element_t m;
299 int cph_buf_size; 303 int cph_buf_size;
300 int aes_buf_size; 304 int aes_buf_size;
301 int plt_len; 305 int plt_len;
302 306
303 read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size); 307 read_cpabe (block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
304 cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size); 308 cph = gabe_cph_unserialize (key->pub, cph_buf, cph_buf_size);
305 if (!gabe_dec(key->pub, key->prv, cph, m)) 309 if (! gabe_dec (key->pub, key->prv, cph, m))
306 { 310 {
307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "%s\n", gabe_error()); 312 "%s\n", gabe_error ());
309 GNUNET_free(aes_buf); 313 GNUNET_free (aes_buf);
310 GNUNET_free(cph_buf); 314 GNUNET_free (cph_buf);
311 gabe_cph_free(cph); 315 gabe_cph_free (cph);
312 GNUNET_free(cph); 316 GNUNET_free (cph);
313 element_clear(m); 317 element_clear (m);
314 return GNUNET_SYSERR; 318 return GNUNET_SYSERR;
315 } 319 }
316 gabe_cph_free(cph); 320 gabe_cph_free (cph);
317 GNUNET_free(cph); 321 GNUNET_free (cph);
318 plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result); 322 plt_len = aes_128_cbc_decrypt (aes_buf, aes_buf_size, m, (char**) result);
319 GNUNET_free(cph_buf); 323 GNUNET_free (cph_buf);
320 GNUNET_free(aes_buf); 324 GNUNET_free (aes_buf);
321 element_clear(m); 325 element_clear (m);
322 //freeing is buggy in gabe 326 // freeing is buggy in gabe
323 //gabe_prv_free (prv); 327 // gabe_prv_free (prv);
324 //gabe_pub_free (pub); 328 // gabe_pub_free (pub);
325 return plt_len; 329 return plt_len;
326} 330}
327 331
328ssize_t 332ssize_t
329GNUNET_CRYPTO_cpabe_serialize_key(const struct GNUNET_CRYPTO_AbeKey *key, 333GNUNET_CRYPTO_cpabe_serialize_key (const struct GNUNET_CRYPTO_AbeKey *key,
330 void **result) 334 void **result)
331{ 335{
332 ssize_t len; 336 ssize_t len;
333 char *pub; 337 char *pub;
@@ -335,21 +339,21 @@ GNUNET_CRYPTO_cpabe_serialize_key(const struct GNUNET_CRYPTO_AbeKey *key,
335 int pub_len; 339 int pub_len;
336 int prv_len; 340 int prv_len;
337 341
338 pub_len = gabe_pub_serialize(key->pub, &pub); 342 pub_len = gabe_pub_serialize (key->pub, &pub);
339 prv_len = gabe_prv_serialize(key->prv, &prv); 343 prv_len = gabe_prv_serialize (key->prv, &prv);
340 344
341 len = pub_len + prv_len + 12; 345 len = pub_len + prv_len + 12;
342 write_cpabe(result, len, pub, pub_len, prv, prv_len); 346 write_cpabe (result, len, pub, pub_len, prv, prv_len);
343 347
344 GNUNET_free(pub); 348 GNUNET_free (pub);
345 GNUNET_free(prv); 349 GNUNET_free (prv);
346 350
347 return len; 351 return len;
348} 352}
349 353
350struct GNUNET_CRYPTO_AbeKey* 354struct GNUNET_CRYPTO_AbeKey*
351GNUNET_CRYPTO_cpabe_deserialize_key(const void *data, 355GNUNET_CRYPTO_cpabe_deserialize_key (const void *data,
352 size_t len) 356 size_t len)
353{ 357{
354 struct GNUNET_CRYPTO_AbeKey *key; 358 struct GNUNET_CRYPTO_AbeKey *key;
355 char *pub; 359 char *pub;
@@ -357,23 +361,24 @@ GNUNET_CRYPTO_cpabe_deserialize_key(const void *data,
357 int prv_len; 361 int prv_len;
358 int pub_len; 362 int pub_len;
359 363
360 key = GNUNET_new(struct GNUNET_CRYPTO_AbeKey); 364 key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
361 read_cpabe(data, 365 read_cpabe (data,
362 &pub, 366 &pub,
363 &pub_len, 367 &pub_len,
364 &prv, 368 &prv,
365 &prv_len); 369 &prv_len);
366 key->pub = gabe_pub_unserialize(pub, pub_len); 370 key->pub = gabe_pub_unserialize (pub, pub_len);
367 key->prv = gabe_prv_unserialize(key->pub, prv, prv_len); 371 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
368 372
369 GNUNET_free(pub); 373 GNUNET_free (pub);
370 GNUNET_free(prv); 374 GNUNET_free (prv);
371 return key; 375 return key;
372} 376}
373 377
374ssize_t 378ssize_t
375GNUNET_CRYPTO_cpabe_serialize_master_key(const struct GNUNET_CRYPTO_AbeMasterKey *key, 379GNUNET_CRYPTO_cpabe_serialize_master_key (const struct
376 void **result) 380 GNUNET_CRYPTO_AbeMasterKey *key,
381 void **result)
377{ 382{
378 ssize_t len; 383 ssize_t len;
379 char *pub; 384 char *pub;
@@ -381,21 +386,21 @@ GNUNET_CRYPTO_cpabe_serialize_master_key(const struct GNUNET_CRYPTO_AbeMasterKey
381 int pub_len; 386 int pub_len;
382 int msk_len; 387 int msk_len;
383 388
384 pub_len = gabe_pub_serialize(key->pub, &pub); 389 pub_len = gabe_pub_serialize (key->pub, &pub);
385 msk_len = gabe_msk_serialize(key->msk, &msk); 390 msk_len = gabe_msk_serialize (key->msk, &msk);
386 391
387 len = pub_len + msk_len + 12; 392 len = pub_len + msk_len + 12;
388 write_cpabe(result, len, pub, pub_len, msk, msk_len); 393 write_cpabe (result, len, pub, pub_len, msk, msk_len);
389 394
390 GNUNET_free(pub); 395 GNUNET_free (pub);
391 GNUNET_free(msk); 396 GNUNET_free (msk);
392 397
393 return len; 398 return len;
394} 399}
395 400
396struct GNUNET_CRYPTO_AbeMasterKey* 401struct GNUNET_CRYPTO_AbeMasterKey*
397GNUNET_CRYPTO_cpabe_deserialize_master_key(const void *data, 402GNUNET_CRYPTO_cpabe_deserialize_master_key (const void *data,
398 size_t len) 403 size_t len)
399{ 404{
400 struct GNUNET_CRYPTO_AbeMasterKey *key; 405 struct GNUNET_CRYPTO_AbeMasterKey *key;
401 char *msk; 406 char *msk;
@@ -403,17 +408,17 @@ GNUNET_CRYPTO_cpabe_deserialize_master_key(const void *data,
403 int msk_len; 408 int msk_len;
404 int pub_len; 409 int pub_len;
405 410
406 key = GNUNET_new(struct GNUNET_CRYPTO_AbeMasterKey); 411 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
407 read_cpabe(data, 412 read_cpabe (data,
408 &pub, 413 &pub,
409 &pub_len, 414 &pub_len,
410 &msk, 415 &msk,
411 &msk_len); 416 &msk_len);
412 key->pub = gabe_pub_unserialize(pub, pub_len); 417 key->pub = gabe_pub_unserialize (pub, pub_len);
413 key->msk = gabe_msk_unserialize(key->pub, msk, msk_len); 418 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);
414 419
415 GNUNET_free(pub); 420 GNUNET_free (pub);
416 GNUNET_free(msk); 421 GNUNET_free (msk);
417 422
418 return key; 423 return key;
419} 424}
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c
index e547ef329..acc4a8ab4 100644
--- a/src/util/crypto_crc.c
+++ b/src/util/crypto_crc.c
@@ -30,7 +30,7 @@
30#include "platform.h" 30#include "platform.h"
31#include "gnunet_crypto_lib.h" 31#include "gnunet_crypto_lib.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-crc", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-crc", __VA_ARGS__)
34 34
35/* Avoid wasting space on 8-byte longs. */ 35/* Avoid wasting space on 8-byte longs. */
36#if UINT_MAX >= 0xffffffff 36#if UINT_MAX >= 0xffffffff
@@ -44,7 +44,7 @@ typedef unsigned long GNUNET_uLong;
44#define Z_NULL 0 44#define Z_NULL 0
45 45
46 46
47#define POLYNOMIAL (GNUNET_uLong)0xedb88320 47#define POLYNOMIAL (GNUNET_uLong) 0xedb88320
48static GNUNET_uLong crc_table[256]; 48static GNUNET_uLong crc_table[256];
49 49
50/* 50/*
@@ -53,7 +53,7 @@ static GNUNET_uLong crc_table[256];
53 * even on a table that someone else is using concurrently. 53 * even on a table that someone else is using concurrently.
54 */ 54 */
55static void 55static void
56crc_init() 56crc_init ()
57{ 57{
58 static int once; 58 static int once;
59 unsigned int i, j; 59 unsigned int i, j;
@@ -64,12 +64,12 @@ crc_init()
64 once = 1; 64 once = 1;
65 crc_table[0] = 0; 65 crc_table[0] = 0;
66 for (i = 128; i; i >>= 1) 66 for (i = 128; i; i >>= 1)
67 { 67 {
68 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0); 68 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
69 /* h is now crc_table[i] */ 69 /* h is now crc_table[i] */
70 for (j = 0; j < 256; j += 2 * i) 70 for (j = 0; j < 256; j += 2 * i)
71 crc_table[i + j] = crc_table[j] ^ h; 71 crc_table[i + j] = crc_table[j] ^ h;
72 } 72 }
73} 73}
74 74
75/* 75/*
@@ -83,10 +83,10 @@ crc_init()
83 * property of detecting all burst errors of length 32 bits or less. 83 * property of detecting all burst errors of length 32 bits or less.
84 */ 84 */
85static GNUNET_uLong 85static GNUNET_uLong
86crc32(GNUNET_uLong crc, const char *buf, size_t len) 86crc32 (GNUNET_uLong crc, const char *buf, size_t len)
87{ 87{
88 crc_init(); 88 crc_init ();
89 GNUNET_assert(crc_table[255] != 0); 89 GNUNET_assert (crc_table[255] != 0);
90 crc ^= 0xffffffff; 90 crc ^= 0xffffffff;
91 while (len--) 91 while (len--)
92 crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; 92 crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
@@ -102,12 +102,12 @@ crc32(GNUNET_uLong crc, const char *buf, size_t len)
102 * @return the resulting CRC32 checksum 102 * @return the resulting CRC32 checksum
103 */ 103 */
104int32_t 104int32_t
105GNUNET_CRYPTO_crc32_n(const void *buf, size_t len) 105GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
106{ 106{
107 GNUNET_uLong crc; 107 GNUNET_uLong crc;
108 108
109 crc = crc32(0L, Z_NULL, 0); 109 crc = crc32 (0L, Z_NULL, 0);
110 crc = crc32(crc, (char *)buf, len); 110 crc = crc32 (crc, (char *) buf, len);
111 return crc; 111 return crc;
112} 112}
113 113
@@ -121,14 +121,14 @@ GNUNET_CRYPTO_crc32_n(const void *buf, size_t len)
121 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish() to get actual crc16) 121 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish() to get actual crc16)
122 */ 122 */
123uint32_t 123uint32_t
124GNUNET_CRYPTO_crc16_step(uint32_t sum, const void *buf, size_t len) 124GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len)
125{ 125{
126 const uint16_t *hdr = buf; 126 const uint16_t *hdr = buf;
127 127
128 for (; len >= 2; len -= 2) 128 for (; len >= 2; len -= 2)
129 sum += *(hdr++); 129 sum += *(hdr++);
130 if (len == 1) 130 if (len == 1)
131 sum += (*hdr) & ntohs(0xFF00); 131 sum += (*hdr) & ntohs (0xFF00);
132 return sum; 132 return sum;
133} 133}
134 134
@@ -140,7 +140,7 @@ GNUNET_CRYPTO_crc16_step(uint32_t sum, const void *buf, size_t len)
140 * @return crc16 value 140 * @return crc16 value
141 */ 141 */
142uint16_t 142uint16_t
143GNUNET_CRYPTO_crc16_finish(uint32_t sum) 143GNUNET_CRYPTO_crc16_finish (uint32_t sum)
144{ 144{
145 sum = (sum >> 16) + (sum & 0xFFFF); 145 sum = (sum >> 16) + (sum & 0xFFFF);
146 sum += (sum >> 16); 146 sum += (sum >> 16);
@@ -157,12 +157,12 @@ GNUNET_CRYPTO_crc16_finish(uint32_t sum)
157 * @return crc16 value 157 * @return crc16 value
158 */ 158 */
159uint16_t 159uint16_t
160GNUNET_CRYPTO_crc16_n(const void *buf, size_t len) 160GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
161{ 161{
162 const uint16_t *hdr = buf; 162 const uint16_t *hdr = buf;
163 uint32_t sum = GNUNET_CRYPTO_crc16_step(0, hdr, len); 163 uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
164 164
165 return GNUNET_CRYPTO_crc16_finish(sum); 165 return GNUNET_CRYPTO_crc16_finish (sum);
166} 166}
167 167
168 168
@@ -175,8 +175,8 @@ GNUNET_CRYPTO_crc16_n(const void *buf, size_t len)
175 * @return crc8 value 175 * @return crc8 value
176 */ 176 */
177uint8_t 177uint8_t
178GNUNET_CRYPTO_crc8_n(const void *buf, 178GNUNET_CRYPTO_crc8_n (const void *buf,
179 size_t len) 179 size_t len)
180{ 180{
181 const uint8_t *data = buf; 181 const uint8_t *data = buf;
182 unsigned int crc = 0; 182 unsigned int crc = 0;
@@ -184,16 +184,16 @@ GNUNET_CRYPTO_crc8_n(const void *buf,
184 int j; 184 int j;
185 185
186 for (j = len; 0 != j; j--) 186 for (j = len; 0 != j; j--)
187 {
188 crc ^= (*data++ << 8);
189 for (i = 8; 0 != i; i--)
187 { 190 {
188 crc ^= (*data++ << 8); 191 if (0 != (crc & 0x8000))
189 for (i = 8; 0 != i; i--) 192 crc ^= (0x1070 << 3);
190 { 193 crc <<= 1;
191 if (0 != (crc & 0x8000))
192 crc ^= (0x1070 << 3);
193 crc <<= 1;
194 }
195 } 194 }
196 return (uint8_t)(crc >> 8); 195 }
196 return (uint8_t) (crc >> 8);
197} 197}
198 198
199 199
diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c
index fed7c9ece..6b11cdada 100644
--- a/src/util/crypto_ecc.c
+++ b/src/util/crypto_ecc.c
@@ -39,13 +39,13 @@
39 */ 39 */
40#define CURVE "Ed25519" 40#define CURVE "Ed25519"
41 41
42#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-ecc", __VA_ARGS__) 42#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
43 43
44#define LOG_STRERROR(kind, syscall) \ 44#define LOG_STRERROR(kind, syscall) \
45 GNUNET_log_from_strerror(kind, "util-crypto-ecc", syscall) 45 GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
46 46
47#define LOG_STRERROR_FILE(kind, syscall, filename) \ 47#define LOG_STRERROR_FILE(kind, syscall, filename) \
48 GNUNET_log_from_strerror_file(kind, "util-crypto-ecc", syscall, filename) 48 GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
49 49
50/** 50/**
51 * Log an error message at log-level 'level' that indicates 51 * Log an error message at log-level 'level' that indicates
@@ -54,14 +54,14 @@
54 */ 54 */
55#define LOG_GCRY(level, cmd, rc) \ 55#define LOG_GCRY(level, cmd, rc) \
56 do \ 56 do \
57 { \ 57 { \
58 LOG(level, \ 58 LOG (level, \
59 _("`%s' failed at %s:%d with error: %s\n"), \ 59 _ ("`%s' failed at %s:%d with error: %s\n"), \
60 cmd, \ 60 cmd, \
61 __FILE__, \ 61 __FILE__, \
62 __LINE__, \ 62 __LINE__, \
63 gcry_strerror(rc)); \ 63 gcry_strerror (rc)); \
64 } while (0) 64 } while (0)
65 65
66 66
67/** 67/**
@@ -74,52 +74,52 @@
74 * @return 0 on success 74 * @return 0 on success
75 */ 75 */
76static int 76static int
77key_from_sexp(gcry_mpi_t *array, 77key_from_sexp (gcry_mpi_t *array,
78 gcry_sexp_t sexp, 78 gcry_sexp_t sexp,
79 const char *topname, 79 const char *topname,
80 const char *elems) 80 const char *elems)
81{ 81{
82 gcry_sexp_t list; 82 gcry_sexp_t list;
83 gcry_sexp_t l2; 83 gcry_sexp_t l2;
84 unsigned int idx; 84 unsigned int idx;
85 85
86 list = gcry_sexp_find_token(sexp, topname, 0); 86 list = gcry_sexp_find_token (sexp, topname, 0);
87 if (!list) 87 if (! list)
88 return 1; 88 return 1;
89 l2 = gcry_sexp_cadr(list); 89 l2 = gcry_sexp_cadr (list);
90 gcry_sexp_release(list); 90 gcry_sexp_release (list);
91 list = l2; 91 list = l2;
92 if (!list) 92 if (! list)
93 return 2; 93 return 2;
94 94
95 idx = 0; 95 idx = 0;
96 for (const char *s = elems; *s; s++, idx++) 96 for (const char *s = elems; *s; s++, idx++)
97 {
98 l2 = gcry_sexp_find_token (list, s, 1);
99 if (! l2)
97 { 100 {
98 l2 = gcry_sexp_find_token(list, s, 1); 101 for (unsigned int i = 0; i < idx; i++)
99 if (!l2) 102 {
100 { 103 gcry_free (array[i]);
101 for (unsigned int i = 0; i < idx; i++) 104 array[i] = NULL;
102 { 105 }
103 gcry_free(array[i]); 106 gcry_sexp_release (list);
104 array[i] = NULL; 107 return 3; /* required parameter not found */
105 }
106 gcry_sexp_release(list);
107 return 3; /* required parameter not found */
108 }
109 array[idx] = gcry_sexp_nth_mpi(l2, 1, GCRYMPI_FMT_USG);
110 gcry_sexp_release(l2);
111 if (!array[idx])
112 {
113 for (unsigned int i = 0; i < idx; i++)
114 {
115 gcry_free(array[i]);
116 array[i] = NULL;
117 }
118 gcry_sexp_release(list);
119 return 4; /* required parameter is invalid */
120 }
121 } 108 }
122 gcry_sexp_release(list); 109 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
110 gcry_sexp_release (l2);
111 if (! array[idx])
112 {
113 for (unsigned int i = 0; i < idx; i++)
114 {
115 gcry_free (array[i]);
116 array[i] = NULL;
117 }
118 gcry_sexp_release (list);
119 return 4; /* required parameter is invalid */
120 }
121 }
122 gcry_sexp_release (list);
123 return 0; 123 return 0;
124} 124}
125 125
@@ -132,28 +132,28 @@ key_from_sexp(gcry_mpi_t *array,
132 * @return NULL on error 132 * @return NULL on error
133 */ 133 */
134static gcry_sexp_t 134static gcry_sexp_t
135decode_private_ecdsa_key(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv) 135decode_private_ecdsa_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
136{ 136{
137 gcry_sexp_t result; 137 gcry_sexp_t result;
138 int rc; 138 int rc;
139 139
140 rc = gcry_sexp_build(&result, 140 rc = gcry_sexp_build (&result,
141 NULL, 141 NULL,
142 "(private-key(ecc(curve \"" CURVE "\")" 142 "(private-key(ecc(curve \"" CURVE "\")"
143 "(d %b)))", 143 "(d %b)))",
144 (int)sizeof(priv->d), 144 (int) sizeof(priv->d),
145 priv->d); 145 priv->d);
146 if (0 != rc) 146 if (0 != rc)
147 { 147 {
148 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 148 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
149 GNUNET_assert(0); 149 GNUNET_assert (0);
150 } 150 }
151#if EXTRA_CHECKS 151#if EXTRA_CHECKS
152 if (0 != (rc = gcry_pk_testkey(result))) 152 if (0 != (rc = gcry_pk_testkey (result)))
153 { 153 {
154 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 154 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
155 GNUNET_assert(0); 155 GNUNET_assert (0);
156 } 156 }
157#endif 157#endif
158 return result; 158 return result;
159} 159}
@@ -167,28 +167,28 @@ decode_private_ecdsa_key(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
167 * @return NULL on error 167 * @return NULL on error
168 */ 168 */
169static gcry_sexp_t 169static gcry_sexp_t
170decode_private_eddsa_key(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv) 170decode_private_eddsa_key (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
171{ 171{
172 gcry_sexp_t result; 172 gcry_sexp_t result;
173 int rc; 173 int rc;
174 174
175 rc = gcry_sexp_build(&result, 175 rc = gcry_sexp_build (&result,
176 NULL, 176 NULL,
177 "(private-key(ecc(curve \"" CURVE "\")" 177 "(private-key(ecc(curve \"" CURVE "\")"
178 "(flags eddsa)(d %b)))", 178 "(flags eddsa)(d %b)))",
179 (int)sizeof(priv->d), 179 (int) sizeof(priv->d),
180 priv->d); 180 priv->d);
181 if (0 != rc) 181 if (0 != rc)
182 { 182 {
183 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 183 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
184 GNUNET_assert(0); 184 GNUNET_assert (0);
185 } 185 }
186#if EXTRA_CHECKS 186#if EXTRA_CHECKS
187 if (0 != (rc = gcry_pk_testkey(result))) 187 if (0 != (rc = gcry_pk_testkey (result)))
188 { 188 {
189 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 189 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
190 GNUNET_assert(0); 190 GNUNET_assert (0);
191 } 191 }
192#endif 192#endif
193 return result; 193 return result;
194} 194}
@@ -202,28 +202,28 @@ decode_private_eddsa_key(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
202 * @return NULL on error 202 * @return NULL on error
203 */ 203 */
204static gcry_sexp_t 204static gcry_sexp_t
205decode_private_ecdhe_key(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv) 205decode_private_ecdhe_key (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv)
206{ 206{
207 gcry_sexp_t result; 207 gcry_sexp_t result;
208 int rc; 208 int rc;
209 209
210 rc = gcry_sexp_build(&result, 210 rc = gcry_sexp_build (&result,
211 NULL, 211 NULL,
212 "(private-key(ecc(curve \"" CURVE "\")" 212 "(private-key(ecc(curve \"" CURVE "\")"
213 "(d %b)))", 213 "(d %b)))",
214 (int)sizeof(priv->d), 214 (int) sizeof(priv->d),
215 priv->d); 215 priv->d);
216 if (0 != rc) 216 if (0 != rc)
217 { 217 {
218 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 218 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
219 GNUNET_assert(0); 219 GNUNET_assert (0);
220 } 220 }
221#if EXTRA_CHECKS 221#if EXTRA_CHECKS
222 if (0 != (rc = gcry_pk_testkey(result))) 222 if (0 != (rc = gcry_pk_testkey (result)))
223 { 223 {
224 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 224 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
225 GNUNET_assert(0); 225 GNUNET_assert (0);
226 } 226 }
227#endif 227#endif
228 return result; 228 return result;
229} 229}
@@ -236,7 +236,7 @@ decode_private_ecdhe_key(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv)
236 * @param pub where to write the public key 236 * @param pub where to write the public key
237 */ 237 */
238void 238void
239GNUNET_CRYPTO_ecdsa_key_get_public( 239GNUNET_CRYPTO_ecdsa_key_get_public (
240 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, 240 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
241 struct GNUNET_CRYPTO_EcdsaPublicKey *pub) 241 struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
242{ 242{
@@ -244,19 +244,19 @@ GNUNET_CRYPTO_ecdsa_key_get_public(
244 gcry_ctx_t ctx; 244 gcry_ctx_t ctx;
245 gcry_mpi_t q; 245 gcry_mpi_t q;
246 246
247 BENCHMARK_START(ecdsa_key_get_public); 247 BENCHMARK_START (ecdsa_key_get_public);
248 248
249 sexp = decode_private_ecdsa_key(priv); 249 sexp = decode_private_ecdsa_key (priv);
250 GNUNET_assert(NULL != sexp); 250 GNUNET_assert (NULL != sexp);
251 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, sexp, NULL)); 251 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, sexp, NULL));
252 gcry_sexp_release(sexp); 252 gcry_sexp_release (sexp);
253 q = gcry_mpi_ec_get_mpi("q@eddsa", ctx, 0); 253 q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
254 GNUNET_assert(NULL != q); 254 GNUNET_assert (NULL != q);
255 GNUNET_CRYPTO_mpi_print_unsigned(pub->q_y, sizeof(pub->q_y), q); 255 GNUNET_CRYPTO_mpi_print_unsigned (pub->q_y, sizeof(pub->q_y), q);
256 gcry_mpi_release(q); 256 gcry_mpi_release (q);
257 gcry_ctx_release(ctx); 257 gcry_ctx_release (ctx);
258 258
259 BENCHMARK_END(ecdsa_key_get_public); 259 BENCHMARK_END (ecdsa_key_get_public);
260} 260}
261 261
262 262
@@ -267,7 +267,7 @@ GNUNET_CRYPTO_ecdsa_key_get_public(
267 * @param pub where to write the public key 267 * @param pub where to write the public key
268 */ 268 */
269void 269void
270GNUNET_CRYPTO_eddsa_key_get_public( 270GNUNET_CRYPTO_eddsa_key_get_public (
271 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, 271 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
272 struct GNUNET_CRYPTO_EddsaPublicKey *pub) 272 struct GNUNET_CRYPTO_EddsaPublicKey *pub)
273{ 273{
@@ -275,19 +275,19 @@ GNUNET_CRYPTO_eddsa_key_get_public(
275 gcry_ctx_t ctx; 275 gcry_ctx_t ctx;
276 gcry_mpi_t q; 276 gcry_mpi_t q;
277 277
278 BENCHMARK_START(eddsa_key_get_public); 278 BENCHMARK_START (eddsa_key_get_public);
279 279
280 sexp = decode_private_eddsa_key(priv); 280 sexp = decode_private_eddsa_key (priv);
281 GNUNET_assert(NULL != sexp); 281 GNUNET_assert (NULL != sexp);
282 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, sexp, NULL)); 282 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, sexp, NULL));
283 gcry_sexp_release(sexp); 283 gcry_sexp_release (sexp);
284 q = gcry_mpi_ec_get_mpi("q@eddsa", ctx, 0); 284 q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
285 GNUNET_assert(q); 285 GNUNET_assert (q);
286 GNUNET_CRYPTO_mpi_print_unsigned(pub->q_y, sizeof(pub->q_y), q); 286 GNUNET_CRYPTO_mpi_print_unsigned (pub->q_y, sizeof(pub->q_y), q);
287 gcry_mpi_release(q); 287 gcry_mpi_release (q);
288 gcry_ctx_release(ctx); 288 gcry_ctx_release (ctx);
289 289
290 BENCHMARK_END(eddsa_key_get_public); 290 BENCHMARK_END (eddsa_key_get_public);
291} 291}
292 292
293 293
@@ -298,7 +298,7 @@ GNUNET_CRYPTO_eddsa_key_get_public(
298 * @param pub where to write the public key 298 * @param pub where to write the public key
299 */ 299 */
300void 300void
301GNUNET_CRYPTO_ecdhe_key_get_public( 301GNUNET_CRYPTO_ecdhe_key_get_public (
302 const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, 302 const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
303 struct GNUNET_CRYPTO_EcdhePublicKey *pub) 303 struct GNUNET_CRYPTO_EcdhePublicKey *pub)
304{ 304{
@@ -306,19 +306,19 @@ GNUNET_CRYPTO_ecdhe_key_get_public(
306 gcry_ctx_t ctx; 306 gcry_ctx_t ctx;
307 gcry_mpi_t q; 307 gcry_mpi_t q;
308 308
309 BENCHMARK_START(ecdhe_key_get_public); 309 BENCHMARK_START (ecdhe_key_get_public);
310 310
311 sexp = decode_private_ecdhe_key(priv); 311 sexp = decode_private_ecdhe_key (priv);
312 GNUNET_assert(NULL != sexp); 312 GNUNET_assert (NULL != sexp);
313 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, sexp, NULL)); 313 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, sexp, NULL));
314 gcry_sexp_release(sexp); 314 gcry_sexp_release (sexp);
315 q = gcry_mpi_ec_get_mpi("q@eddsa", ctx, 0); 315 q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
316 GNUNET_assert(q); 316 GNUNET_assert (q);
317 GNUNET_CRYPTO_mpi_print_unsigned(pub->q_y, sizeof(pub->q_y), q); 317 GNUNET_CRYPTO_mpi_print_unsigned (pub->q_y, sizeof(pub->q_y), q);
318 gcry_mpi_release(q); 318 gcry_mpi_release (q);
319 gcry_ctx_release(ctx); 319 gcry_ctx_release (ctx);
320 320
321 BENCHMARK_END(ecdhe_key_get_public); 321 BENCHMARK_END (ecdhe_key_get_public);
322} 322}
323 323
324 324
@@ -329,7 +329,7 @@ GNUNET_CRYPTO_ecdhe_key_get_public(
329 * @return string representing @a pub 329 * @return string representing @a pub
330 */ 330 */
331char * 331char *
332GNUNET_CRYPTO_ecdsa_public_key_to_string( 332GNUNET_CRYPTO_ecdsa_public_key_to_string (
333 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub) 333 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
334{ 334{
335 char *pubkeybuf; 335 char *pubkeybuf;
@@ -339,17 +339,17 @@ GNUNET_CRYPTO_ecdsa_public_key_to_string(
339 if (keylen % 5 > 0) 339 if (keylen % 5 > 0)
340 keylen += 5 - keylen % 5; 340 keylen += 5 - keylen % 5;
341 keylen /= 5; 341 keylen /= 5;
342 pubkeybuf = GNUNET_malloc(keylen + 1); 342 pubkeybuf = GNUNET_malloc (keylen + 1);
343 end = 343 end =
344 GNUNET_STRINGS_data_to_string((unsigned char *)pub, 344 GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
345 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey), 345 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
346 pubkeybuf, 346 pubkeybuf,
347 keylen); 347 keylen);
348 if (NULL == end) 348 if (NULL == end)
349 { 349 {
350 GNUNET_free(pubkeybuf); 350 GNUNET_free (pubkeybuf);
351 return NULL; 351 return NULL;
352 } 352 }
353 *end = '\0'; 353 *end = '\0';
354 return pubkeybuf; 354 return pubkeybuf;
355} 355}
@@ -362,7 +362,7 @@ GNUNET_CRYPTO_ecdsa_public_key_to_string(
362 * @return string representing @a pub 362 * @return string representing @a pub
363 */ 363 */
364char * 364char *
365GNUNET_CRYPTO_eddsa_public_key_to_string( 365GNUNET_CRYPTO_eddsa_public_key_to_string (
366 const struct GNUNET_CRYPTO_EddsaPublicKey *pub) 366 const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
367{ 367{
368 char *pubkeybuf; 368 char *pubkeybuf;
@@ -372,17 +372,17 @@ GNUNET_CRYPTO_eddsa_public_key_to_string(
372 if (keylen % 5 > 0) 372 if (keylen % 5 > 0)
373 keylen += 5 - keylen % 5; 373 keylen += 5 - keylen % 5;
374 keylen /= 5; 374 keylen /= 5;
375 pubkeybuf = GNUNET_malloc(keylen + 1); 375 pubkeybuf = GNUNET_malloc (keylen + 1);
376 end = 376 end =
377 GNUNET_STRINGS_data_to_string((unsigned char *)pub, 377 GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
378 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey), 378 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey),
379 pubkeybuf, 379 pubkeybuf,
380 keylen); 380 keylen);
381 if (NULL == end) 381 if (NULL == end)
382 { 382 {
383 GNUNET_free(pubkeybuf); 383 GNUNET_free (pubkeybuf);
384 return NULL; 384 return NULL;
385 } 385 }
386 *end = '\0'; 386 *end = '\0';
387 return pubkeybuf; 387 return pubkeybuf;
388} 388}
@@ -395,7 +395,7 @@ GNUNET_CRYPTO_eddsa_public_key_to_string(
395 * @return string representing @a pub 395 * @return string representing @a pub
396 */ 396 */
397char * 397char *
398GNUNET_CRYPTO_eddsa_private_key_to_string( 398GNUNET_CRYPTO_eddsa_private_key_to_string (
399 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv) 399 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
400{ 400{
401 char *privkeybuf; 401 char *privkeybuf;
@@ -405,17 +405,17 @@ GNUNET_CRYPTO_eddsa_private_key_to_string(
405 if (keylen % 5 > 0) 405 if (keylen % 5 > 0)
406 keylen += 5 - keylen % 5; 406 keylen += 5 - keylen % 5;
407 keylen /= 5; 407 keylen /= 5;
408 privkeybuf = GNUNET_malloc(keylen + 1); 408 privkeybuf = GNUNET_malloc (keylen + 1);
409 end = GNUNET_STRINGS_data_to_string((unsigned char *)priv, 409 end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv,
410 sizeof( 410 sizeof(
411 struct GNUNET_CRYPTO_EddsaPrivateKey), 411 struct GNUNET_CRYPTO_EddsaPrivateKey),
412 privkeybuf, 412 privkeybuf,
413 keylen); 413 keylen);
414 if (NULL == end) 414 if (NULL == end)
415 { 415 {
416 GNUNET_free(privkeybuf); 416 GNUNET_free (privkeybuf);
417 return NULL; 417 return NULL;
418 } 418 }
419 *end = '\0'; 419 *end = '\0';
420 return privkeybuf; 420 return privkeybuf;
421} 421}
@@ -428,7 +428,7 @@ GNUNET_CRYPTO_eddsa_private_key_to_string(
428 * @return string representing @a priv 428 * @return string representing @a priv
429 */ 429 */
430char * 430char *
431GNUNET_CRYPTO_ecdsa_private_key_to_string( 431GNUNET_CRYPTO_ecdsa_private_key_to_string (
432 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv) 432 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
433{ 433{
434 char *privkeybuf; 434 char *privkeybuf;
@@ -438,17 +438,17 @@ GNUNET_CRYPTO_ecdsa_private_key_to_string(
438 if (keylen % 5 > 0) 438 if (keylen % 5 > 0)
439 keylen += 5 - keylen % 5; 439 keylen += 5 - keylen % 5;
440 keylen /= 5; 440 keylen /= 5;
441 privkeybuf = GNUNET_malloc(keylen + 1); 441 privkeybuf = GNUNET_malloc (keylen + 1);
442 end = GNUNET_STRINGS_data_to_string((unsigned char *)priv, 442 end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv,
443 sizeof( 443 sizeof(
444 struct GNUNET_CRYPTO_EcdsaPrivateKey), 444 struct GNUNET_CRYPTO_EcdsaPrivateKey),
445 privkeybuf, 445 privkeybuf,
446 keylen); 446 keylen);
447 if (NULL == end) 447 if (NULL == end)
448 { 448 {
449 GNUNET_free(privkeybuf); 449 GNUNET_free (privkeybuf);
450 return NULL; 450 return NULL;
451 } 451 }
452 *end = '\0'; 452 *end = '\0';
453 return privkeybuf; 453 return privkeybuf;
454} 454}
@@ -463,7 +463,7 @@ GNUNET_CRYPTO_ecdsa_private_key_to_string(
463 * @return #GNUNET_OK on success 463 * @return #GNUNET_OK on success
464 */ 464 */
465int 465int
466GNUNET_CRYPTO_ecdsa_public_key_from_string( 466GNUNET_CRYPTO_ecdsa_public_key_from_string (
467 const char *enc, 467 const char *enc,
468 size_t enclen, 468 size_t enclen,
469 struct GNUNET_CRYPTO_EcdsaPublicKey *pub) 469 struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
@@ -477,11 +477,11 @@ GNUNET_CRYPTO_ecdsa_public_key_from_string(
477 return GNUNET_SYSERR; 477 return GNUNET_SYSERR;
478 478
479 if (GNUNET_OK != 479 if (GNUNET_OK !=
480 GNUNET_STRINGS_string_to_data(enc, 480 GNUNET_STRINGS_string_to_data (enc,
481 enclen, 481 enclen,
482 pub, 482 pub,
483 sizeof( 483 sizeof(
484 struct GNUNET_CRYPTO_EcdsaPublicKey))) 484 struct GNUNET_CRYPTO_EcdsaPublicKey)))
485 return GNUNET_SYSERR; 485 return GNUNET_SYSERR;
486 return GNUNET_OK; 486 return GNUNET_OK;
487} 487}
@@ -496,7 +496,7 @@ GNUNET_CRYPTO_ecdsa_public_key_from_string(
496 * @return #GNUNET_OK on success 496 * @return #GNUNET_OK on success
497 */ 497 */
498int 498int
499GNUNET_CRYPTO_eddsa_public_key_from_string( 499GNUNET_CRYPTO_eddsa_public_key_from_string (
500 const char *enc, 500 const char *enc,
501 size_t enclen, 501 size_t enclen,
502 struct GNUNET_CRYPTO_EddsaPublicKey *pub) 502 struct GNUNET_CRYPTO_EddsaPublicKey *pub)
@@ -510,11 +510,11 @@ GNUNET_CRYPTO_eddsa_public_key_from_string(
510 return GNUNET_SYSERR; 510 return GNUNET_SYSERR;
511 511
512 if (GNUNET_OK != 512 if (GNUNET_OK !=
513 GNUNET_STRINGS_string_to_data(enc, 513 GNUNET_STRINGS_string_to_data (enc,
514 enclen, 514 enclen,
515 pub, 515 pub,
516 sizeof( 516 sizeof(
517 struct GNUNET_CRYPTO_EddsaPublicKey))) 517 struct GNUNET_CRYPTO_EddsaPublicKey)))
518 return GNUNET_SYSERR; 518 return GNUNET_SYSERR;
519 return GNUNET_OK; 519 return GNUNET_OK;
520} 520}
@@ -529,7 +529,7 @@ GNUNET_CRYPTO_eddsa_public_key_from_string(
529 * @return #GNUNET_OK on success 529 * @return #GNUNET_OK on success
530 */ 530 */
531int 531int
532GNUNET_CRYPTO_eddsa_private_key_from_string( 532GNUNET_CRYPTO_eddsa_private_key_from_string (
533 const char *enc, 533 const char *enc,
534 size_t enclen, 534 size_t enclen,
535 struct GNUNET_CRYPTO_EddsaPrivateKey *priv) 535 struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
@@ -543,18 +543,18 @@ GNUNET_CRYPTO_eddsa_private_key_from_string(
543 return GNUNET_SYSERR; 543 return GNUNET_SYSERR;
544 544
545 if (GNUNET_OK != 545 if (GNUNET_OK !=
546 GNUNET_STRINGS_string_to_data(enc, 546 GNUNET_STRINGS_string_to_data (enc,
547 enclen, 547 enclen,
548 priv, 548 priv,
549 sizeof( 549 sizeof(
550 struct GNUNET_CRYPTO_EddsaPrivateKey))) 550 struct GNUNET_CRYPTO_EddsaPrivateKey)))
551 return GNUNET_SYSERR; 551 return GNUNET_SYSERR;
552#if CRYPTO_BUG 552#if CRYPTO_BUG
553 if (GNUNET_OK != check_eddsa_key(priv)) 553 if (GNUNET_OK != check_eddsa_key (priv))
554 { 554 {
555 GNUNET_break(0); 555 GNUNET_break (0);
556 return GNUNET_OK; 556 return GNUNET_OK;
557 } 557 }
558#endif 558#endif
559 return GNUNET_OK; 559 return GNUNET_OK;
560} 560}
@@ -567,9 +567,9 @@ GNUNET_CRYPTO_eddsa_private_key_from_string(
567 * @param pk location of the key 567 * @param pk location of the key
568 */ 568 */
569void 569void
570GNUNET_CRYPTO_ecdhe_key_clear(struct GNUNET_CRYPTO_EcdhePrivateKey *pk) 570GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
571{ 571{
572 memset(pk, 0, sizeof(struct GNUNET_CRYPTO_EcdhePrivateKey)); 572 memset (pk, 0, sizeof(struct GNUNET_CRYPTO_EcdhePrivateKey));
573} 573}
574 574
575 575
@@ -580,9 +580,9 @@ GNUNET_CRYPTO_ecdhe_key_clear(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
580 * @param pk location of the key 580 * @param pk location of the key
581 */ 581 */
582void 582void
583GNUNET_CRYPTO_ecdsa_key_clear(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk) 583GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
584{ 584{
585 memset(pk, 0, sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)); 585 memset (pk, 0, sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
586} 586}
587 587
588 588
@@ -593,9 +593,9 @@ GNUNET_CRYPTO_ecdsa_key_clear(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
593 * @param pk location of the key 593 * @param pk location of the key
594 */ 594 */
595void 595void
596GNUNET_CRYPTO_eddsa_key_clear(struct GNUNET_CRYPTO_EddsaPrivateKey *pk) 596GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
597{ 597{
598 memset(pk, 0, sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)); 598 memset (pk, 0, sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey));
599} 599}
600 600
601 601
@@ -605,16 +605,16 @@ GNUNET_CRYPTO_eddsa_key_clear(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
605 * @return fresh private key 605 * @return fresh private key
606 */ 606 */
607struct GNUNET_CRYPTO_EcdhePrivateKey * 607struct GNUNET_CRYPTO_EcdhePrivateKey *
608GNUNET_CRYPTO_ecdhe_key_create() 608GNUNET_CRYPTO_ecdhe_key_create ()
609{ 609{
610 struct GNUNET_CRYPTO_EcdhePrivateKey *priv; 610 struct GNUNET_CRYPTO_EcdhePrivateKey *priv;
611 611
612 priv = GNUNET_new(struct GNUNET_CRYPTO_EcdhePrivateKey); 612 priv = GNUNET_new (struct GNUNET_CRYPTO_EcdhePrivateKey);
613 if (GNUNET_OK != GNUNET_CRYPTO_ecdhe_key_create2(priv)) 613 if (GNUNET_OK != GNUNET_CRYPTO_ecdhe_key_create2 (priv))
614 { 614 {
615 GNUNET_free(priv); 615 GNUNET_free (priv);
616 return NULL; 616 return NULL;
617 } 617 }
618 return priv; 618 return priv;
619} 619}
620 620
@@ -627,54 +627,54 @@ GNUNET_CRYPTO_ecdhe_key_create()
627 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 627 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
628 */ 628 */
629int 629int
630GNUNET_CRYPTO_ecdhe_key_create2(struct GNUNET_CRYPTO_EcdhePrivateKey *pk) 630GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
631{ 631{
632 gcry_sexp_t priv_sexp; 632 gcry_sexp_t priv_sexp;
633 gcry_sexp_t s_keyparam; 633 gcry_sexp_t s_keyparam;
634 gcry_mpi_t d; 634 gcry_mpi_t d;
635 int rc; 635 int rc;
636 636
637 BENCHMARK_START(ecdhe_key_create); 637 BENCHMARK_START (ecdhe_key_create);
638 638
639 /* NOTE: For libgcrypt >= 1.7, we do not need the 'eddsa' flag here, 639 /* NOTE: For libgcrypt >= 1.7, we do not need the 'eddsa' flag here,
640 but should also be harmless. For libgcrypt < 1.7, using 'eddsa' 640 but should also be harmless. For libgcrypt < 1.7, using 'eddsa'
641 disables an expensive key testing routine. We do not want to run 641 disables an expensive key testing routine. We do not want to run
642 the expensive check for ECDHE, as we generate TONS of keys to 642 the expensive check for ECDHE, as we generate TONS of keys to
643 use for a very short time. */ 643 use for a very short time. */
644 if (0 != (rc = gcry_sexp_build(&s_keyparam, 644 if (0 != (rc = gcry_sexp_build (&s_keyparam,
645 NULL, 645 NULL,
646 "(genkey(ecc(curve \"" CURVE "\")" 646 "(genkey(ecc(curve \"" CURVE "\")"
647 "(flags eddsa no-keytest)))"))) 647 "(flags eddsa no-keytest)))")))
648 { 648 {
649 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 649 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
650 return GNUNET_SYSERR; 650 return GNUNET_SYSERR;
651 } 651 }
652 if (0 != (rc = gcry_pk_genkey(&priv_sexp, s_keyparam))) 652 if (0 != (rc = gcry_pk_genkey (&priv_sexp, s_keyparam)))
653 { 653 {
654 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc); 654 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
655 gcry_sexp_release(s_keyparam); 655 gcry_sexp_release (s_keyparam);
656 return GNUNET_SYSERR; 656 return GNUNET_SYSERR;
657 } 657 }
658 gcry_sexp_release(s_keyparam); 658 gcry_sexp_release (s_keyparam);
659#if EXTRA_CHECKS 659#if EXTRA_CHECKS
660 if (0 != (rc = gcry_pk_testkey(priv_sexp))) 660 if (0 != (rc = gcry_pk_testkey (priv_sexp)))
661 { 661 {
662 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 662 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
663 gcry_sexp_release(priv_sexp); 663 gcry_sexp_release (priv_sexp);
664 return GNUNET_SYSERR; 664 return GNUNET_SYSERR;
665 } 665 }
666#endif 666#endif
667 if (0 != (rc = key_from_sexp(&d, priv_sexp, "private-key", "d"))) 667 if (0 != (rc = key_from_sexp (&d, priv_sexp, "private-key", "d")))
668 { 668 {
669 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc); 669 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
670 gcry_sexp_release(priv_sexp); 670 gcry_sexp_release (priv_sexp);
671 return GNUNET_SYSERR; 671 return GNUNET_SYSERR;
672 } 672 }
673 gcry_sexp_release(priv_sexp); 673 gcry_sexp_release (priv_sexp);
674 GNUNET_CRYPTO_mpi_print_unsigned(pk->d, sizeof(pk->d), d); 674 GNUNET_CRYPTO_mpi_print_unsigned (pk->d, sizeof(pk->d), d);
675 gcry_mpi_release(d); 675 gcry_mpi_release (d);
676 676
677 BENCHMARK_END(ecdhe_key_create); 677 BENCHMARK_END (ecdhe_key_create);
678 678
679 return GNUNET_OK; 679 return GNUNET_OK;
680} 680}
@@ -686,7 +686,7 @@ GNUNET_CRYPTO_ecdhe_key_create2(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
686 * @return fresh private key 686 * @return fresh private key
687 */ 687 */
688struct GNUNET_CRYPTO_EcdsaPrivateKey * 688struct GNUNET_CRYPTO_EcdsaPrivateKey *
689GNUNET_CRYPTO_ecdsa_key_create() 689GNUNET_CRYPTO_ecdsa_key_create ()
690{ 690{
691 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv; 691 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
692 gcry_sexp_t priv_sexp; 692 gcry_sexp_t priv_sexp;
@@ -694,43 +694,43 @@ GNUNET_CRYPTO_ecdsa_key_create()
694 gcry_mpi_t d; 694 gcry_mpi_t d;
695 int rc; 695 int rc;
696 696
697 BENCHMARK_START(ecdsa_key_create); 697 BENCHMARK_START (ecdsa_key_create);
698 698
699 if (0 != (rc = gcry_sexp_build(&s_keyparam, 699 if (0 != (rc = gcry_sexp_build (&s_keyparam,
700 NULL, 700 NULL,
701 "(genkey(ecc(curve \"" CURVE "\")" 701 "(genkey(ecc(curve \"" CURVE "\")"
702 "(flags)))"))) 702 "(flags)))")))
703 { 703 {
704 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 704 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
705 return NULL; 705 return NULL;
706 } 706 }
707 if (0 != (rc = gcry_pk_genkey(&priv_sexp, s_keyparam))) 707 if (0 != (rc = gcry_pk_genkey (&priv_sexp, s_keyparam)))
708 { 708 {
709 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc); 709 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
710 gcry_sexp_release(s_keyparam); 710 gcry_sexp_release (s_keyparam);
711 return NULL; 711 return NULL;
712 } 712 }
713 gcry_sexp_release(s_keyparam); 713 gcry_sexp_release (s_keyparam);
714#if EXTRA_CHECKS 714#if EXTRA_CHECKS
715 if (0 != (rc = gcry_pk_testkey(priv_sexp))) 715 if (0 != (rc = gcry_pk_testkey (priv_sexp)))
716 { 716 {
717 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 717 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
718 gcry_sexp_release(priv_sexp); 718 gcry_sexp_release (priv_sexp);
719 return NULL; 719 return NULL;
720 } 720 }
721#endif 721#endif
722 if (0 != (rc = key_from_sexp(&d, priv_sexp, "private-key", "d"))) 722 if (0 != (rc = key_from_sexp (&d, priv_sexp, "private-key", "d")))
723 { 723 {
724 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc); 724 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
725 gcry_sexp_release(priv_sexp); 725 gcry_sexp_release (priv_sexp);
726 return NULL; 726 return NULL;
727 } 727 }
728 gcry_sexp_release(priv_sexp); 728 gcry_sexp_release (priv_sexp);
729 priv = GNUNET_new(struct GNUNET_CRYPTO_EcdsaPrivateKey); 729 priv = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
730 GNUNET_CRYPTO_mpi_print_unsigned(priv->d, sizeof(priv->d), d); 730 GNUNET_CRYPTO_mpi_print_unsigned (priv->d, sizeof(priv->d), d);
731 gcry_mpi_release(d); 731 gcry_mpi_release (d);
732 732
733 BENCHMARK_END(ecdsa_key_create); 733 BENCHMARK_END (ecdsa_key_create);
734 734
735 return priv; 735 return priv;
736} 736}
@@ -741,7 +741,7 @@ GNUNET_CRYPTO_ecdsa_key_create()
741 * @return fresh private key 741 * @return fresh private key
742 */ 742 */
743struct GNUNET_CRYPTO_EddsaPrivateKey * 743struct GNUNET_CRYPTO_EddsaPrivateKey *
744GNUNET_CRYPTO_eddsa_key_create() 744GNUNET_CRYPTO_eddsa_key_create ()
745{ 745{
746 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 746 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
747 gcry_sexp_t priv_sexp; 747 gcry_sexp_t priv_sexp;
@@ -749,55 +749,55 @@ GNUNET_CRYPTO_eddsa_key_create()
749 gcry_mpi_t d; 749 gcry_mpi_t d;
750 int rc; 750 int rc;
751 751
752 BENCHMARK_START(eddsa_key_create); 752 BENCHMARK_START (eddsa_key_create);
753 753
754#if CRYPTO_BUG 754#if CRYPTO_BUG
755again: 755again:
756#endif 756#endif
757 if (0 != (rc = gcry_sexp_build(&s_keyparam, 757 if (0 != (rc = gcry_sexp_build (&s_keyparam,
758 NULL, 758 NULL,
759 "(genkey(ecc(curve \"" CURVE "\")" 759 "(genkey(ecc(curve \"" CURVE "\")"
760 "(flags eddsa)))"))) 760 "(flags eddsa)))")))
761 { 761 {
762 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 762 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
763 return NULL; 763 return NULL;
764 } 764 }
765 if (0 != (rc = gcry_pk_genkey(&priv_sexp, s_keyparam))) 765 if (0 != (rc = gcry_pk_genkey (&priv_sexp, s_keyparam)))
766 { 766 {
767 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc); 767 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
768 gcry_sexp_release(s_keyparam); 768 gcry_sexp_release (s_keyparam);
769 return NULL; 769 return NULL;
770 } 770 }
771 gcry_sexp_release(s_keyparam); 771 gcry_sexp_release (s_keyparam);
772#if EXTRA_CHECKS 772#if EXTRA_CHECKS
773 if (0 != (rc = gcry_pk_testkey(priv_sexp))) 773 if (0 != (rc = gcry_pk_testkey (priv_sexp)))
774 { 774 {
775 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); 775 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
776 gcry_sexp_release(priv_sexp); 776 gcry_sexp_release (priv_sexp);
777 return NULL; 777 return NULL;
778 } 778 }
779#endif 779#endif
780 if (0 != (rc = key_from_sexp(&d, priv_sexp, "private-key", "d"))) 780 if (0 != (rc = key_from_sexp (&d, priv_sexp, "private-key", "d")))
781 { 781 {
782 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc); 782 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
783 gcry_sexp_release(priv_sexp); 783 gcry_sexp_release (priv_sexp);
784 return NULL; 784 return NULL;
785 } 785 }
786 gcry_sexp_release(priv_sexp); 786 gcry_sexp_release (priv_sexp);
787 priv = GNUNET_new(struct GNUNET_CRYPTO_EddsaPrivateKey); 787 priv = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey);
788 GNUNET_CRYPTO_mpi_print_unsigned(priv->d, sizeof(priv->d), d); 788 GNUNET_CRYPTO_mpi_print_unsigned (priv->d, sizeof(priv->d), d);
789 gcry_mpi_release(d); 789 gcry_mpi_release (d);
790 790
791#if CRYPTO_BUG 791#if CRYPTO_BUG
792 if (GNUNET_OK != check_eddsa_key(priv)) 792 if (GNUNET_OK != check_eddsa_key (priv))
793 { 793 {
794 GNUNET_break(0); 794 GNUNET_break (0);
795 GNUNET_free(priv); 795 GNUNET_free (priv);
796 goto again; 796 goto again;
797 } 797 }
798#endif 798#endif
799 799
800 BENCHMARK_END(eddsa_key_create); 800 BENCHMARK_END (eddsa_key_create);
801 801
802 return priv; 802 return priv;
803} 803}
@@ -809,7 +809,7 @@ again:
809 * @return "anonymous" private key 809 * @return "anonymous" private key
810 */ 810 */
811const struct GNUNET_CRYPTO_EcdsaPrivateKey * 811const struct GNUNET_CRYPTO_EcdsaPrivateKey *
812GNUNET_CRYPTO_ecdsa_key_get_anonymous() 812GNUNET_CRYPTO_ecdsa_key_get_anonymous ()
813{ 813{
814 /** 814 /**
815 * 'anonymous' pseudonym (global static, d=1, public key = G 815 * 'anonymous' pseudonym (global static, d=1, public key = G
@@ -820,9 +820,9 @@ GNUNET_CRYPTO_ecdsa_key_get_anonymous()
820 820
821 if (once) 821 if (once)
822 return &anonymous; 822 return &anonymous;
823 GNUNET_CRYPTO_mpi_print_unsigned(anonymous.d, 823 GNUNET_CRYPTO_mpi_print_unsigned (anonymous.d,
824 sizeof(anonymous.d), 824 sizeof(anonymous.d),
825 GCRYMPI_CONST_ONE); 825 GCRYMPI_CONST_ONE);
826 once = 1; 826 once = 1;
827 return &anonymous; 827 return &anonymous;
828} 828}
@@ -836,7 +836,7 @@ GNUNET_CRYPTO_ecdsa_key_get_anonymous()
836 * @return converted s-expression 836 * @return converted s-expression
837 */ 837 */
838static gcry_sexp_t 838static gcry_sexp_t
839data_to_eddsa_value(const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose) 839data_to_eddsa_value (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
840{ 840{
841 gcry_sexp_t data; 841 gcry_sexp_t data;
842 int rc; 842 int rc;
@@ -845,29 +845,29 @@ data_to_eddsa_value(const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
845#if 1 845#if 1
846 struct GNUNET_HashCode hc; 846 struct GNUNET_HashCode hc;
847 847
848 GNUNET_CRYPTO_hash(purpose, ntohl(purpose->size), &hc); 848 GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
849 if (0 != (rc = gcry_sexp_build(&data, 849 if (0 != (rc = gcry_sexp_build (&data,
850 NULL, 850 NULL,
851 "(data(flags eddsa)(hash-algo %s)(value %b))", 851 "(data(flags eddsa)(hash-algo %s)(value %b))",
852 "sha512", 852 "sha512",
853 (int)sizeof(hc), 853 (int) sizeof(hc),
854 &hc))) 854 &hc)))
855 { 855 {
856 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 856 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
857 return NULL; 857 return NULL;
858 } 858 }
859#else 859#else
860 GNUNET_CRYPTO_hash(purpose, ntohl(purpose->size), &hc); 860 GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
861 if (0 != (rc = gcry_sexp_build(&data, 861 if (0 != (rc = gcry_sexp_build (&data,
862 NULL, 862 NULL,
863 "(data(flags eddsa)(hash-algo %s)(value %b))", 863 "(data(flags eddsa)(hash-algo %s)(value %b))",
864 "sha512", 864 "sha512",
865 ntohl(purpose->size), 865 ntohl (purpose->size),
866 purpose))) 866 purpose)))
867 { 867 {
868 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 868 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
869 return NULL; 869 return NULL;
870 } 870 }
871#endif 871#endif
872 return data; 872 return data;
873} 873}
@@ -881,7 +881,7 @@ data_to_eddsa_value(const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
881 * @return converted s-expression 881 * @return converted s-expression
882 */ 882 */
883static gcry_sexp_t 883static gcry_sexp_t
884data_to_ecdsa_value(const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose) 884data_to_ecdsa_value (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
885{ 885{
886 gcry_sexp_t data; 886 gcry_sexp_t data;
887 int rc; 887 int rc;
@@ -890,28 +890,28 @@ data_to_ecdsa_value(const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
890#if 1 890#if 1
891 struct GNUNET_HashCode hc; 891 struct GNUNET_HashCode hc;
892 892
893 GNUNET_CRYPTO_hash(purpose, ntohl(purpose->size), &hc); 893 GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
894 if (0 != (rc = gcry_sexp_build(&data, 894 if (0 != (rc = gcry_sexp_build (&data,
895 NULL, 895 NULL,
896 "(data(flags rfc6979)(hash %s %b))", 896 "(data(flags rfc6979)(hash %s %b))",
897 "sha512", 897 "sha512",
898 (int)sizeof(hc), 898 (int) sizeof(hc),
899 &hc))) 899 &hc)))
900 { 900 {
901 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 901 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
902 return NULL; 902 return NULL;
903 } 903 }
904#else 904#else
905 if (0 != (rc = gcry_sexp_build(&data, 905 if (0 != (rc = gcry_sexp_build (&data,
906 NULL, 906 NULL,
907 "(data(flags rfc6979)(hash %s %b))", 907 "(data(flags rfc6979)(hash %s %b))",
908 "sha512", 908 "sha512",
909 ntohl(purpose->size), 909 ntohl (purpose->size),
910 purpose))) 910 purpose)))
911 { 911 {
912 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 912 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
913 return NULL; 913 return NULL;
914 } 914 }
915#endif 915#endif
916 return data; 916 return data;
917} 917}
@@ -926,7 +926,7 @@ data_to_ecdsa_value(const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
926 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 926 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
927 */ 927 */
928int 928int
929GNUNET_CRYPTO_ecdsa_sign( 929GNUNET_CRYPTO_ecdsa_sign (
930 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, 930 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
931 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, 931 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
932 struct GNUNET_CRYPTO_EcdsaSignature *sig) 932 struct GNUNET_CRYPTO_EcdsaSignature *sig)
@@ -937,39 +937,39 @@ GNUNET_CRYPTO_ecdsa_sign(
937 int rc; 937 int rc;
938 gcry_mpi_t rs[2]; 938 gcry_mpi_t rs[2];
939 939
940 BENCHMARK_START(ecdsa_sign); 940 BENCHMARK_START (ecdsa_sign);
941 941
942 priv_sexp = decode_private_ecdsa_key(priv); 942 priv_sexp = decode_private_ecdsa_key (priv);
943 data = data_to_ecdsa_value(purpose); 943 data = data_to_ecdsa_value (purpose);
944 if (0 != (rc = gcry_pk_sign(&sig_sexp, data, priv_sexp))) 944 if (0 != (rc = gcry_pk_sign (&sig_sexp, data, priv_sexp)))
945 { 945 {
946 LOG(GNUNET_ERROR_TYPE_WARNING, 946 LOG (GNUNET_ERROR_TYPE_WARNING,
947 _("ECC signing failed at %s:%d: %s\n"), 947 _ ("ECC signing failed at %s:%d: %s\n"),
948 __FILE__, 948 __FILE__,
949 __LINE__, 949 __LINE__,
950 gcry_strerror(rc)); 950 gcry_strerror (rc));
951 gcry_sexp_release(data); 951 gcry_sexp_release (data);
952 gcry_sexp_release(priv_sexp); 952 gcry_sexp_release (priv_sexp);
953 return GNUNET_SYSERR; 953 return GNUNET_SYSERR;
954 } 954 }
955 gcry_sexp_release(priv_sexp); 955 gcry_sexp_release (priv_sexp);
956 gcry_sexp_release(data); 956 gcry_sexp_release (data);
957 957
958 /* extract 'r' and 's' values from sexpression 'sig_sexp' and store in 958 /* extract 'r' and 's' values from sexpression 'sig_sexp' and store in
959 'signature' */ 959 'signature' */
960 if (0 != (rc = key_from_sexp(rs, sig_sexp, "sig-val", "rs"))) 960 if (0 != (rc = key_from_sexp (rs, sig_sexp, "sig-val", "rs")))
961 { 961 {
962 GNUNET_break(0); 962 GNUNET_break (0);
963 gcry_sexp_release(sig_sexp); 963 gcry_sexp_release (sig_sexp);
964 return GNUNET_SYSERR; 964 return GNUNET_SYSERR;
965 } 965 }
966 gcry_sexp_release(sig_sexp); 966 gcry_sexp_release (sig_sexp);
967 GNUNET_CRYPTO_mpi_print_unsigned(sig->r, sizeof(sig->r), rs[0]); 967 GNUNET_CRYPTO_mpi_print_unsigned (sig->r, sizeof(sig->r), rs[0]);
968 GNUNET_CRYPTO_mpi_print_unsigned(sig->s, sizeof(sig->s), rs[1]); 968 GNUNET_CRYPTO_mpi_print_unsigned (sig->s, sizeof(sig->s), rs[1]);
969 gcry_mpi_release(rs[0]); 969 gcry_mpi_release (rs[0]);
970 gcry_mpi_release(rs[1]); 970 gcry_mpi_release (rs[1]);
971 971
972 BENCHMARK_END(ecdsa_sign); 972 BENCHMARK_END (ecdsa_sign);
973 973
974 return GNUNET_OK; 974 return GNUNET_OK;
975} 975}
@@ -984,7 +984,7 @@ GNUNET_CRYPTO_ecdsa_sign(
984 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 984 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
985 */ 985 */
986int 986int
987GNUNET_CRYPTO_eddsa_sign( 987GNUNET_CRYPTO_eddsa_sign (
988 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, 988 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
989 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, 989 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
990 struct GNUNET_CRYPTO_EddsaSignature *sig) 990 struct GNUNET_CRYPTO_EddsaSignature *sig)
@@ -995,39 +995,39 @@ GNUNET_CRYPTO_eddsa_sign(
995 int rc; 995 int rc;
996 gcry_mpi_t rs[2]; 996 gcry_mpi_t rs[2];
997 997
998 BENCHMARK_START(eddsa_sign); 998 BENCHMARK_START (eddsa_sign);
999 999
1000 priv_sexp = decode_private_eddsa_key(priv); 1000 priv_sexp = decode_private_eddsa_key (priv);
1001 data = data_to_eddsa_value(purpose); 1001 data = data_to_eddsa_value (purpose);
1002 if (0 != (rc = gcry_pk_sign(&sig_sexp, data, priv_sexp))) 1002 if (0 != (rc = gcry_pk_sign (&sig_sexp, data, priv_sexp)))
1003 { 1003 {
1004 LOG(GNUNET_ERROR_TYPE_WARNING, 1004 LOG (GNUNET_ERROR_TYPE_WARNING,
1005 _("EdDSA signing failed at %s:%d: %s\n"), 1005 _ ("EdDSA signing failed at %s:%d: %s\n"),
1006 __FILE__, 1006 __FILE__,
1007 __LINE__, 1007 __LINE__,
1008 gcry_strerror(rc)); 1008 gcry_strerror (rc));
1009 gcry_sexp_release(data); 1009 gcry_sexp_release (data);
1010 gcry_sexp_release(priv_sexp); 1010 gcry_sexp_release (priv_sexp);
1011 return GNUNET_SYSERR; 1011 return GNUNET_SYSERR;
1012 } 1012 }
1013 gcry_sexp_release(priv_sexp); 1013 gcry_sexp_release (priv_sexp);
1014 gcry_sexp_release(data); 1014 gcry_sexp_release (data);
1015 1015
1016 /* extract 'r' and 's' values from sexpression 'sig_sexp' and store in 1016 /* extract 'r' and 's' values from sexpression 'sig_sexp' and store in
1017 'signature' */ 1017 'signature' */
1018 if (0 != (rc = key_from_sexp(rs, sig_sexp, "sig-val", "rs"))) 1018 if (0 != (rc = key_from_sexp (rs, sig_sexp, "sig-val", "rs")))
1019 { 1019 {
1020 GNUNET_break(0); 1020 GNUNET_break (0);
1021 gcry_sexp_release(sig_sexp); 1021 gcry_sexp_release (sig_sexp);
1022 return GNUNET_SYSERR; 1022 return GNUNET_SYSERR;
1023 } 1023 }
1024 gcry_sexp_release(sig_sexp); 1024 gcry_sexp_release (sig_sexp);
1025 GNUNET_CRYPTO_mpi_print_unsigned(sig->r, sizeof(sig->r), rs[0]); 1025 GNUNET_CRYPTO_mpi_print_unsigned (sig->r, sizeof(sig->r), rs[0]);
1026 GNUNET_CRYPTO_mpi_print_unsigned(sig->s, sizeof(sig->s), rs[1]); 1026 GNUNET_CRYPTO_mpi_print_unsigned (sig->s, sizeof(sig->s), rs[1]);
1027 gcry_mpi_release(rs[0]); 1027 gcry_mpi_release (rs[0]);
1028 gcry_mpi_release(rs[1]); 1028 gcry_mpi_release (rs[1]);
1029 1029
1030 BENCHMARK_END(eddsa_sign); 1030 BENCHMARK_END (eddsa_sign);
1031 1031
1032 return GNUNET_OK; 1032 return GNUNET_OK;
1033} 1033}
@@ -1043,7 +1043,7 @@ GNUNET_CRYPTO_eddsa_sign(
1043 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid 1043 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1044 */ 1044 */
1045int 1045int
1046GNUNET_CRYPTO_ecdsa_verify( 1046GNUNET_CRYPTO_ecdsa_verify (
1047 uint32_t purpose, 1047 uint32_t purpose,
1048 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, 1048 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1049 const struct GNUNET_CRYPTO_EcdsaSignature *sig, 1049 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
@@ -1054,49 +1054,49 @@ GNUNET_CRYPTO_ecdsa_verify(
1054 gcry_sexp_t pub_sexpr; 1054 gcry_sexp_t pub_sexpr;
1055 int rc; 1055 int rc;
1056 1056
1057 BENCHMARK_START(ecdsa_verify); 1057 BENCHMARK_START (ecdsa_verify);
1058 1058
1059 if (purpose != ntohl(validate->purpose)) 1059 if (purpose != ntohl (validate->purpose))
1060 return GNUNET_SYSERR; /* purpose mismatch */ 1060 return GNUNET_SYSERR; /* purpose mismatch */
1061 1061
1062 /* build s-expression for signature */ 1062 /* build s-expression for signature */
1063 if (0 != (rc = gcry_sexp_build(&sig_sexpr, 1063 if (0 != (rc = gcry_sexp_build (&sig_sexpr,
1064 NULL, 1064 NULL,
1065 "(sig-val(ecdsa(r %b)(s %b)))", 1065 "(sig-val(ecdsa(r %b)(s %b)))",
1066 (int)sizeof(sig->r), 1066 (int) sizeof(sig->r),
1067 sig->r, 1067 sig->r,
1068 (int)sizeof(sig->s), 1068 (int) sizeof(sig->s),
1069 sig->s))) 1069 sig->s)))
1070 { 1070 {
1071 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 1071 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1072 return GNUNET_SYSERR; 1072 return GNUNET_SYSERR;
1073 } 1073 }
1074 data = data_to_ecdsa_value(validate); 1074 data = data_to_ecdsa_value (validate);
1075 if (0 != (rc = gcry_sexp_build(&pub_sexpr, 1075 if (0 != (rc = gcry_sexp_build (&pub_sexpr,
1076 NULL, 1076 NULL,
1077 "(public-key(ecc(curve " CURVE ")(q %b)))", 1077 "(public-key(ecc(curve " CURVE ")(q %b)))",
1078 (int)sizeof(pub->q_y), 1078 (int) sizeof(pub->q_y),
1079 pub->q_y))) 1079 pub->q_y)))
1080 { 1080 {
1081 gcry_sexp_release(data); 1081 gcry_sexp_release (data);
1082 gcry_sexp_release(sig_sexpr); 1082 gcry_sexp_release (sig_sexpr);
1083 return GNUNET_SYSERR; 1083 return GNUNET_SYSERR;
1084 } 1084 }
1085 rc = gcry_pk_verify(sig_sexpr, data, pub_sexpr); 1085 rc = gcry_pk_verify (sig_sexpr, data, pub_sexpr);
1086 gcry_sexp_release(pub_sexpr); 1086 gcry_sexp_release (pub_sexpr);
1087 gcry_sexp_release(data); 1087 gcry_sexp_release (data);
1088 gcry_sexp_release(sig_sexpr); 1088 gcry_sexp_release (sig_sexpr);
1089 if (0 != rc) 1089 if (0 != rc)
1090 { 1090 {
1091 LOG(GNUNET_ERROR_TYPE_INFO, 1091 LOG (GNUNET_ERROR_TYPE_INFO,
1092 _("ECDSA signature verification failed at %s:%d: %s\n"), 1092 _ ("ECDSA signature verification failed at %s:%d: %s\n"),
1093 __FILE__, 1093 __FILE__,
1094 __LINE__, 1094 __LINE__,
1095 gcry_strerror(rc)); 1095 gcry_strerror (rc));
1096 BENCHMARK_END(ecdsa_verify); 1096 BENCHMARK_END (ecdsa_verify);
1097 return GNUNET_SYSERR; 1097 return GNUNET_SYSERR;
1098 } 1098 }
1099 BENCHMARK_END(ecdsa_verify); 1099 BENCHMARK_END (ecdsa_verify);
1100 return GNUNET_OK; 1100 return GNUNET_OK;
1101} 1101}
1102 1102
@@ -1111,7 +1111,7 @@ GNUNET_CRYPTO_ecdsa_verify(
1111 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid 1111 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1112 */ 1112 */
1113int 1113int
1114GNUNET_CRYPTO_eddsa_verify( 1114GNUNET_CRYPTO_eddsa_verify (
1115 uint32_t purpose, 1115 uint32_t purpose,
1116 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, 1116 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1117 const struct GNUNET_CRYPTO_EddsaSignature *sig, 1117 const struct GNUNET_CRYPTO_EddsaSignature *sig,
@@ -1122,51 +1122,51 @@ GNUNET_CRYPTO_eddsa_verify(
1122 gcry_sexp_t pub_sexpr; 1122 gcry_sexp_t pub_sexpr;
1123 int rc; 1123 int rc;
1124 1124
1125 BENCHMARK_START(eddsa_verify); 1125 BENCHMARK_START (eddsa_verify);
1126 1126
1127 if (purpose != ntohl(validate->purpose)) 1127 if (purpose != ntohl (validate->purpose))
1128 return GNUNET_SYSERR; /* purpose mismatch */ 1128 return GNUNET_SYSERR; /* purpose mismatch */
1129 1129
1130 /* build s-expression for signature */ 1130 /* build s-expression for signature */
1131 if (0 != (rc = gcry_sexp_build(&sig_sexpr, 1131 if (0 != (rc = gcry_sexp_build (&sig_sexpr,
1132 NULL, 1132 NULL,
1133 "(sig-val(eddsa(r %b)(s %b)))", 1133 "(sig-val(eddsa(r %b)(s %b)))",
1134 (int)sizeof(sig->r), 1134 (int) sizeof(sig->r),
1135 sig->r, 1135 sig->r,
1136 (int)sizeof(sig->s), 1136 (int) sizeof(sig->s),
1137 sig->s))) 1137 sig->s)))
1138 { 1138 {
1139 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 1139 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1140 return GNUNET_SYSERR; 1140 return GNUNET_SYSERR;
1141 } 1141 }
1142 data = data_to_eddsa_value(validate); 1142 data = data_to_eddsa_value (validate);
1143 if (0 != (rc = gcry_sexp_build(&pub_sexpr, 1143 if (0 != (rc = gcry_sexp_build (&pub_sexpr,
1144 NULL, 1144 NULL,
1145 "(public-key(ecc(curve " CURVE 1145 "(public-key(ecc(curve " CURVE
1146 ")(flags eddsa)(q %b)))", 1146 ")(flags eddsa)(q %b)))",
1147 (int)sizeof(pub->q_y), 1147 (int) sizeof(pub->q_y),
1148 pub->q_y))) 1148 pub->q_y)))
1149 { 1149 {
1150 gcry_sexp_release(data); 1150 gcry_sexp_release (data);
1151 gcry_sexp_release(sig_sexpr); 1151 gcry_sexp_release (sig_sexpr);
1152 return GNUNET_SYSERR; 1152 return GNUNET_SYSERR;
1153 } 1153 }
1154 rc = gcry_pk_verify(sig_sexpr, data, pub_sexpr); 1154 rc = gcry_pk_verify (sig_sexpr, data, pub_sexpr);
1155 gcry_sexp_release(pub_sexpr); 1155 gcry_sexp_release (pub_sexpr);
1156 gcry_sexp_release(data); 1156 gcry_sexp_release (data);
1157 gcry_sexp_release(sig_sexpr); 1157 gcry_sexp_release (sig_sexpr);
1158 if (0 != rc) 1158 if (0 != rc)
1159 { 1159 {
1160 LOG(GNUNET_ERROR_TYPE_INFO, 1160 LOG (GNUNET_ERROR_TYPE_INFO,
1161 _("EdDSA signature verification of type %u failed at %s:%d: %s\n"), 1161 _ ("EdDSA signature verification of type %u failed at %s:%d: %s\n"),
1162 (unsigned int)purpose, 1162 (unsigned int) purpose,
1163 __FILE__, 1163 __FILE__,
1164 __LINE__, 1164 __LINE__,
1165 gcry_strerror(rc)); 1165 gcry_strerror (rc));
1166 BENCHMARK_END(eddsa_verify); 1166 BENCHMARK_END (eddsa_verify);
1167 return GNUNET_SYSERR; 1167 return GNUNET_SYSERR;
1168 } 1168 }
1169 BENCHMARK_END(eddsa_verify); 1169 BENCHMARK_END (eddsa_verify);
1170 return GNUNET_OK; 1170 return GNUNET_OK;
1171} 1171}
1172 1172
@@ -1180,9 +1180,9 @@ GNUNET_CRYPTO_eddsa_verify(
1180 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 1180 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1181 */ 1181 */
1182int 1182int
1183GNUNET_CRYPTO_ecc_ecdh(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, 1183GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1184 const struct GNUNET_CRYPTO_EcdhePublicKey *pub, 1184 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1185 struct GNUNET_HashCode *key_material) 1185 struct GNUNET_HashCode *key_material)
1186{ 1186{
1187 gcry_mpi_point_t result; 1187 gcry_mpi_point_t result;
1188 gcry_mpi_point_t q; 1188 gcry_mpi_point_t q;
@@ -1193,51 +1193,51 @@ GNUNET_CRYPTO_ecc_ecdh(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1193 unsigned char xbuf[256 / 8]; 1193 unsigned char xbuf[256 / 8];
1194 size_t rsize; 1194 size_t rsize;
1195 1195
1196 BENCHMARK_START(ecc_ecdh); 1196 BENCHMARK_START (ecc_ecdh);
1197 1197
1198 /* first, extract the q = dP value from the public key */ 1198 /* first, extract the q = dP value from the public key */
1199 if (0 != gcry_sexp_build(&pub_sexpr, 1199 if (0 != gcry_sexp_build (&pub_sexpr,
1200 NULL, 1200 NULL,
1201 "(public-key(ecc(curve " CURVE ")(q %b)))", 1201 "(public-key(ecc(curve " CURVE ")(q %b)))",
1202 (int)sizeof(pub->q_y), 1202 (int) sizeof(pub->q_y),
1203 pub->q_y)) 1203 pub->q_y))
1204 return GNUNET_SYSERR; 1204 return GNUNET_SYSERR;
1205 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, pub_sexpr, NULL)); 1205 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1206 gcry_sexp_release(pub_sexpr); 1206 gcry_sexp_release (pub_sexpr);
1207 q = gcry_mpi_ec_get_point("q", ctx, 0); 1207 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1208 1208
1209 /* second, extract the d value from our private key */ 1209 /* second, extract the d value from our private key */
1210 GNUNET_CRYPTO_mpi_scan_unsigned(&d, priv->d, sizeof(priv->d)); 1210 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof(priv->d));
1211 1211
1212 /* then call the 'multiply' function, to compute the product */ 1212 /* then call the 'multiply' function, to compute the product */
1213 result = gcry_mpi_point_new(0); 1213 result = gcry_mpi_point_new (0);
1214 gcry_mpi_ec_mul(result, d, q, ctx); 1214 gcry_mpi_ec_mul (result, d, q, ctx);
1215 gcry_mpi_point_release(q); 1215 gcry_mpi_point_release (q);
1216 gcry_mpi_release(d); 1216 gcry_mpi_release (d);
1217 1217
1218 /* finally, convert point to string for hashing */ 1218 /* finally, convert point to string for hashing */
1219 result_x = gcry_mpi_new(256); 1219 result_x = gcry_mpi_new (256);
1220 if (gcry_mpi_ec_get_affine(result_x, NULL, result, ctx)) 1220 if (gcry_mpi_ec_get_affine (result_x, NULL, result, ctx))
1221 { 1221 {
1222 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0); 1222 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0);
1223 gcry_mpi_point_release(result); 1223 gcry_mpi_point_release (result);
1224 gcry_ctx_release(ctx); 1224 gcry_ctx_release (ctx);
1225 return GNUNET_SYSERR; 1225 return GNUNET_SYSERR;
1226 } 1226 }
1227 gcry_mpi_point_release(result); 1227 gcry_mpi_point_release (result);
1228 gcry_ctx_release(ctx); 1228 gcry_ctx_release (ctx);
1229 1229
1230 rsize = sizeof(xbuf); 1230 rsize = sizeof(xbuf);
1231 GNUNET_assert(!gcry_mpi_get_flag(result_x, GCRYMPI_FLAG_OPAQUE)); 1231 GNUNET_assert (! gcry_mpi_get_flag (result_x, GCRYMPI_FLAG_OPAQUE));
1232 /* result_x can be negative here, so we do not use 'GNUNET_CRYPTO_mpi_print_unsigned' 1232 /* result_x can be negative here, so we do not use 'GNUNET_CRYPTO_mpi_print_unsigned'
1233 as that does not include the sign bit; x should be a 255-bit 1233 as that does not include the sign bit; x should be a 255-bit
1234 value, so with the sign it should fit snugly into the 256-bit 1234 value, so with the sign it should fit snugly into the 256-bit
1235 xbuf */ 1235 xbuf */
1236 GNUNET_assert( 1236 GNUNET_assert (
1237 0 == gcry_mpi_print(GCRYMPI_FMT_STD, xbuf, rsize, &rsize, result_x)); 1237 0 == gcry_mpi_print (GCRYMPI_FMT_STD, xbuf, rsize, &rsize, result_x));
1238 GNUNET_CRYPTO_hash(xbuf, rsize, key_material); 1238 GNUNET_CRYPTO_hash (xbuf, rsize, key_material);
1239 gcry_mpi_release(result_x); 1239 gcry_mpi_release (result_x);
1240 BENCHMARK_END(ecc_ecdh); 1240 BENCHMARK_END (ecc_ecdh);
1241 return GNUNET_OK; 1241 return GNUNET_OK;
1242} 1242}
1243 1243
@@ -1253,27 +1253,27 @@ GNUNET_CRYPTO_ecc_ecdh(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1253 * @return h value 1253 * @return h value
1254 */ 1254 */
1255static gcry_mpi_t 1255static gcry_mpi_t
1256derive_h(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, 1256derive_h (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1257 const char *label, 1257 const char *label,
1258 const char *context) 1258 const char *context)
1259{ 1259{
1260 gcry_mpi_t h; 1260 gcry_mpi_t h;
1261 struct GNUNET_HashCode hc; 1261 struct GNUNET_HashCode hc;
1262 static const char *const salt = "key-derivation"; 1262 static const char *const salt = "key-derivation";
1263 1263
1264 GNUNET_CRYPTO_kdf(&hc, 1264 GNUNET_CRYPTO_kdf (&hc,
1265 sizeof(hc), 1265 sizeof(hc),
1266 salt, 1266 salt,
1267 strlen(salt), 1267 strlen (salt),
1268 pub, 1268 pub,
1269 sizeof(*pub), 1269 sizeof(*pub),
1270 label, 1270 label,
1271 strlen(label), 1271 strlen (label),
1272 context, 1272 context,
1273 strlen(context), 1273 strlen (context),
1274 NULL, 1274 NULL,
1275 0); 1275 0);
1276 GNUNET_CRYPTO_mpi_scan_unsigned(&h, (unsigned char *)&hc, sizeof(hc)); 1276 GNUNET_CRYPTO_mpi_scan_unsigned (&h, (unsigned char *) &hc, sizeof(hc));
1277 return h; 1277 return h;
1278} 1278}
1279 1279
@@ -1291,7 +1291,7 @@ derive_h(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1291 * @return derived private key 1291 * @return derived private key
1292 */ 1292 */
1293struct GNUNET_CRYPTO_EcdsaPrivateKey * 1293struct GNUNET_CRYPTO_EcdsaPrivateKey *
1294GNUNET_CRYPTO_ecdsa_private_key_derive( 1294GNUNET_CRYPTO_ecdsa_private_key_derive (
1295 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, 1295 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1296 const char *label, 1296 const char *label,
1297 const char *context) 1297 const char *context)
@@ -1304,22 +1304,22 @@ GNUNET_CRYPTO_ecdsa_private_key_derive(
1304 gcry_mpi_t n; 1304 gcry_mpi_t n;
1305 gcry_ctx_t ctx; 1305 gcry_ctx_t ctx;
1306 1306
1307 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, NULL, CURVE)); 1307 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
1308 1308
1309 n = gcry_mpi_ec_get_mpi("n", ctx, 1); 1309 n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
1310 GNUNET_CRYPTO_ecdsa_key_get_public(priv, &pub); 1310 GNUNET_CRYPTO_ecdsa_key_get_public (priv, &pub);
1311 1311
1312 h = derive_h(&pub, label, context); 1312 h = derive_h (&pub, label, context);
1313 GNUNET_CRYPTO_mpi_scan_unsigned(&x, priv->d, sizeof(priv->d)); 1313 GNUNET_CRYPTO_mpi_scan_unsigned (&x, priv->d, sizeof(priv->d));
1314 d = gcry_mpi_new(256); 1314 d = gcry_mpi_new (256);
1315 gcry_mpi_mulm(d, h, x, n); 1315 gcry_mpi_mulm (d, h, x, n);
1316 gcry_mpi_release(h); 1316 gcry_mpi_release (h);
1317 gcry_mpi_release(x); 1317 gcry_mpi_release (x);
1318 gcry_mpi_release(n); 1318 gcry_mpi_release (n);
1319 gcry_ctx_release(ctx); 1319 gcry_ctx_release (ctx);
1320 ret = GNUNET_new(struct GNUNET_CRYPTO_EcdsaPrivateKey); 1320 ret = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1321 GNUNET_CRYPTO_mpi_print_unsigned(ret->d, sizeof(ret->d), d); 1321 GNUNET_CRYPTO_mpi_print_unsigned (ret->d, sizeof(ret->d), d);
1322 gcry_mpi_release(d); 1322 gcry_mpi_release (d);
1323 return ret; 1323 return ret;
1324} 1324}
1325 1325
@@ -1335,7 +1335,7 @@ GNUNET_CRYPTO_ecdsa_private_key_derive(
1335 * @param result where to write the derived public key 1335 * @param result where to write the derived public key
1336 */ 1336 */
1337void 1337void
1338GNUNET_CRYPTO_ecdsa_public_key_derive( 1338GNUNET_CRYPTO_ecdsa_public_key_derive (
1339 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, 1339 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1340 const char *label, 1340 const char *label,
1341 const char *context, 1341 const char *context,
@@ -1349,39 +1349,39 @@ GNUNET_CRYPTO_ecdsa_public_key_derive(
1349 gcry_mpi_point_t q; 1349 gcry_mpi_point_t q;
1350 gcry_mpi_point_t v; 1350 gcry_mpi_point_t v;
1351 1351
1352 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, NULL, CURVE)); 1352 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
1353 1353
1354 /* obtain point 'q' from original public key. The provided 'q' is 1354 /* obtain point 'q' from original public key. The provided 'q' is
1355 compressed thus we first store it in the context and then get it 1355 compressed thus we first store it in the context and then get it
1356 back as a (decompresssed) point. */ 1356 back as a (decompresssed) point. */
1357 q_y = gcry_mpi_set_opaque_copy(NULL, pub->q_y, 8 * sizeof(pub->q_y)); 1357 q_y = gcry_mpi_set_opaque_copy (NULL, pub->q_y, 8 * sizeof(pub->q_y));
1358 GNUNET_assert(NULL != q_y); 1358 GNUNET_assert (NULL != q_y);
1359 GNUNET_assert(0 == gcry_mpi_ec_set_mpi("q", q_y, ctx)); 1359 GNUNET_assert (0 == gcry_mpi_ec_set_mpi ("q", q_y, ctx));
1360 gcry_mpi_release(q_y); 1360 gcry_mpi_release (q_y);
1361 q = gcry_mpi_ec_get_point("q", ctx, 0); 1361 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1362 GNUNET_assert(q); 1362 GNUNET_assert (q);
1363 1363
1364 /* calculate h_mod_n = h % n */ 1364 /* calculate h_mod_n = h % n */
1365 h = derive_h(pub, label, context); 1365 h = derive_h (pub, label, context);
1366 n = gcry_mpi_ec_get_mpi("n", ctx, 1); 1366 n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
1367 h_mod_n = gcry_mpi_new(256); 1367 h_mod_n = gcry_mpi_new (256);
1368 gcry_mpi_mod(h_mod_n, h, n); 1368 gcry_mpi_mod (h_mod_n, h, n);
1369 /* calculate v = h_mod_n * q */ 1369 /* calculate v = h_mod_n * q */
1370 v = gcry_mpi_point_new(0); 1370 v = gcry_mpi_point_new (0);
1371 gcry_mpi_ec_mul(v, h_mod_n, q, ctx); 1371 gcry_mpi_ec_mul (v, h_mod_n, q, ctx);
1372 gcry_mpi_release(h_mod_n); 1372 gcry_mpi_release (h_mod_n);
1373 gcry_mpi_release(h); 1373 gcry_mpi_release (h);
1374 gcry_mpi_release(n); 1374 gcry_mpi_release (n);
1375 gcry_mpi_point_release(q); 1375 gcry_mpi_point_release (q);
1376 1376
1377 /* convert point 'v' to public key that we return */ 1377 /* convert point 'v' to public key that we return */
1378 GNUNET_assert(0 == gcry_mpi_ec_set_point("q", v, ctx)); 1378 GNUNET_assert (0 == gcry_mpi_ec_set_point ("q", v, ctx));
1379 gcry_mpi_point_release(v); 1379 gcry_mpi_point_release (v);
1380 q_y = gcry_mpi_ec_get_mpi("q@eddsa", ctx, 0); 1380 q_y = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
1381 GNUNET_assert(q_y); 1381 GNUNET_assert (q_y);
1382 GNUNET_CRYPTO_mpi_print_unsigned(result->q_y, sizeof(result->q_y), q_y); 1382 GNUNET_CRYPTO_mpi_print_unsigned (result->q_y, sizeof(result->q_y), q_y);
1383 gcry_mpi_release(q_y); 1383 gcry_mpi_release (q_y);
1384 gcry_ctx_release(ctx); 1384 gcry_ctx_release (ctx);
1385} 1385}
1386 1386
1387 1387
@@ -1392,17 +1392,17 @@ GNUNET_CRYPTO_ecdsa_public_key_derive(
1392 * @param length number of bytes in @a buffer 1392 * @param length number of bytes in @a buffer
1393 */ 1393 */
1394static void 1394static void
1395reverse_buffer(unsigned char *buffer, size_t length) 1395reverse_buffer (unsigned char *buffer, size_t length)
1396{ 1396{
1397 unsigned char tmp; 1397 unsigned char tmp;
1398 size_t i; 1398 size_t i;
1399 1399
1400 for (i = 0; i < length / 2; i++) 1400 for (i = 0; i < length / 2; i++)
1401 { 1401 {
1402 tmp = buffer[i]; 1402 tmp = buffer[i];
1403 buffer[i] = buffer[length - 1 - i]; 1403 buffer[i] = buffer[length - 1 - i];
1404 buffer[length - 1 - i] = tmp; 1404 buffer[length - 1 - i] = tmp;
1405 } 1405 }
1406} 1406}
1407 1407
1408 1408
@@ -1414,7 +1414,7 @@ reverse_buffer(unsigned char *buffer, size_t length)
1414 * @return value used for the calculation in EdDSA 1414 * @return value used for the calculation in EdDSA
1415 */ 1415 */
1416static gcry_mpi_t 1416static gcry_mpi_t
1417eddsa_d_to_a(gcry_mpi_t d) 1417eddsa_d_to_a (gcry_mpi_t d)
1418{ 1418{
1419 unsigned char rawmpi[32]; /* 256-bit value */ 1419 unsigned char rawmpi[32]; /* 256-bit value */
1420 size_t rawmpilen; 1420 size_t rawmpilen;
@@ -1427,25 +1427,25 @@ eddsa_d_to_a(gcry_mpi_t d)
1427 1427
1428 /* Note that we clear DIGEST so we can use it as input to left pad 1428 /* Note that we clear DIGEST so we can use it as input to left pad
1429 the key with zeroes for hashing. */ 1429 the key with zeroes for hashing. */
1430 memset(digest, 0, sizeof digest); 1430 memset (digest, 0, sizeof digest);
1431 memset(hvec, 0, sizeof hvec); 1431 memset (hvec, 0, sizeof hvec);
1432 rawmpilen = sizeof(rawmpi); 1432 rawmpilen = sizeof(rawmpi);
1433 GNUNET_assert( 1433 GNUNET_assert (
1434 0 == gcry_mpi_print(GCRYMPI_FMT_USG, rawmpi, rawmpilen, &rawmpilen, d)); 1434 0 == gcry_mpi_print (GCRYMPI_FMT_USG, rawmpi, rawmpilen, &rawmpilen, d));
1435 hvec[0].data = digest; 1435 hvec[0].data = digest;
1436 hvec[0].off = 0; 1436 hvec[0].off = 0;
1437 hvec[0].len = b > rawmpilen ? (b - rawmpilen) : 0; 1437 hvec[0].len = b > rawmpilen ? (b - rawmpilen) : 0;
1438 hvec[1].data = rawmpi; 1438 hvec[1].data = rawmpi;
1439 hvec[1].off = 0; 1439 hvec[1].off = 0;
1440 hvec[1].len = rawmpilen; 1440 hvec[1].len = rawmpilen;
1441 GNUNET_assert( 1441 GNUNET_assert (
1442 0 == gcry_md_hash_buffers(GCRY_MD_SHA512, 0 /* flags */, digest, hvec, 2)); 1442 0 == gcry_md_hash_buffers (GCRY_MD_SHA512, 0 /* flags */, digest, hvec, 2));
1443 /* Compute the A value. */ 1443 /* Compute the A value. */
1444 reverse_buffer(digest, 32); /* Only the first half of the hash. */ 1444 reverse_buffer (digest, 32); /* Only the first half of the hash. */
1445 digest[0] = (digest[0] & 0x7f) | 0x40; 1445 digest[0] = (digest[0] & 0x7f) | 0x40;
1446 digest[31] &= 0xf8; 1446 digest[31] &= 0xf8;
1447 1447
1448 GNUNET_CRYPTO_mpi_scan_unsigned(&a, digest, 32); 1448 GNUNET_CRYPTO_mpi_scan_unsigned (&a, digest, 32);
1449 return a; 1449 return a;
1450} 1450}
1451 1451
@@ -1459,32 +1459,32 @@ eddsa_d_to_a(gcry_mpi_t d)
1459 * @return #GNUNET_OK on success 1459 * @return #GNUNET_OK on success
1460 */ 1460 */
1461static int 1461static int
1462point_to_hash(gcry_mpi_point_t result, 1462point_to_hash (gcry_mpi_point_t result,
1463 gcry_ctx_t ctx, 1463 gcry_ctx_t ctx,
1464 struct GNUNET_HashCode *key_material) 1464 struct GNUNET_HashCode *key_material)
1465{ 1465{
1466 gcry_mpi_t result_x; 1466 gcry_mpi_t result_x;
1467 unsigned char xbuf[256 / 8]; 1467 unsigned char xbuf[256 / 8];
1468 size_t rsize; 1468 size_t rsize;
1469 1469
1470 /* finally, convert point to string for hashing */ 1470 /* finally, convert point to string for hashing */
1471 result_x = gcry_mpi_new(256); 1471 result_x = gcry_mpi_new (256);
1472 if (gcry_mpi_ec_get_affine(result_x, NULL, result, ctx)) 1472 if (gcry_mpi_ec_get_affine (result_x, NULL, result, ctx))
1473 { 1473 {
1474 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0); 1474 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0);
1475 return GNUNET_SYSERR; 1475 return GNUNET_SYSERR;
1476 } 1476 }
1477 1477
1478 rsize = sizeof(xbuf); 1478 rsize = sizeof(xbuf);
1479 GNUNET_assert(!gcry_mpi_get_flag(result_x, GCRYMPI_FLAG_OPAQUE)); 1479 GNUNET_assert (! gcry_mpi_get_flag (result_x, GCRYMPI_FLAG_OPAQUE));
1480 /* result_x can be negative here, so we do not use 'GNUNET_CRYPTO_mpi_print_unsigned' 1480 /* result_x can be negative here, so we do not use 'GNUNET_CRYPTO_mpi_print_unsigned'
1481 as that does not include the sign bit; x should be a 255-bit 1481 as that does not include the sign bit; x should be a 255-bit
1482 value, so with the sign it should fit snugly into the 256-bit 1482 value, so with the sign it should fit snugly into the 256-bit
1483 xbuf */ 1483 xbuf */
1484 GNUNET_assert( 1484 GNUNET_assert (
1485 0 == gcry_mpi_print(GCRYMPI_FMT_STD, xbuf, rsize, &rsize, result_x)); 1485 0 == gcry_mpi_print (GCRYMPI_FMT_STD, xbuf, rsize, &rsize, result_x));
1486 GNUNET_CRYPTO_hash(xbuf, rsize, key_material); 1486 GNUNET_CRYPTO_hash (xbuf, rsize, key_material);
1487 gcry_mpi_release(result_x); 1487 gcry_mpi_release (result_x);
1488 return GNUNET_OK; 1488 return GNUNET_OK;
1489} 1489}
1490 1490
@@ -1500,9 +1500,9 @@ point_to_hash(gcry_mpi_point_t result,
1500 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 1500 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1501 */ 1501 */
1502int 1502int
1503GNUNET_CRYPTO_eddsa_ecdh(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, 1503GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1504 const struct GNUNET_CRYPTO_EcdhePublicKey *pub, 1504 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1505 struct GNUNET_HashCode *key_material) 1505 struct GNUNET_HashCode *key_material)
1506{ 1506{
1507 gcry_mpi_point_t result; 1507 gcry_mpi_point_t result;
1508 gcry_mpi_point_t q; 1508 gcry_mpi_point_t q;
@@ -1512,36 +1512,36 @@ GNUNET_CRYPTO_eddsa_ecdh(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1512 gcry_sexp_t pub_sexpr; 1512 gcry_sexp_t pub_sexpr;
1513 int ret; 1513 int ret;
1514 1514
1515 BENCHMARK_START(eddsa_ecdh); 1515 BENCHMARK_START (eddsa_ecdh);
1516 1516
1517 /* first, extract the q = dP value from the public key */ 1517 /* first, extract the q = dP value from the public key */
1518 if (0 != gcry_sexp_build(&pub_sexpr, 1518 if (0 != gcry_sexp_build (&pub_sexpr,
1519 NULL, 1519 NULL,
1520 "(public-key(ecc(curve " CURVE ")(q %b)))", 1520 "(public-key(ecc(curve " CURVE ")(q %b)))",
1521 (int)sizeof(pub->q_y), 1521 (int) sizeof(pub->q_y),
1522 pub->q_y)) 1522 pub->q_y))
1523 return GNUNET_SYSERR; 1523 return GNUNET_SYSERR;
1524 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, pub_sexpr, NULL)); 1524 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1525 gcry_sexp_release(pub_sexpr); 1525 gcry_sexp_release (pub_sexpr);
1526 q = gcry_mpi_ec_get_point("q", ctx, 0); 1526 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1527 1527
1528 /* second, extract the d value from our private key */ 1528 /* second, extract the d value from our private key */
1529 GNUNET_CRYPTO_mpi_scan_unsigned(&d, priv->d, sizeof(priv->d)); 1529 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof(priv->d));
1530 1530
1531 /* NOW, because this is EdDSA, HASH 'd' first! */ 1531 /* NOW, because this is EdDSA, HASH 'd' first! */
1532 a = eddsa_d_to_a(d); 1532 a = eddsa_d_to_a (d);
1533 gcry_mpi_release(d); 1533 gcry_mpi_release (d);
1534 1534
1535 /* then call the 'multiply' function, to compute the product */ 1535 /* then call the 'multiply' function, to compute the product */
1536 result = gcry_mpi_point_new(0); 1536 result = gcry_mpi_point_new (0);
1537 gcry_mpi_ec_mul(result, a, q, ctx); 1537 gcry_mpi_ec_mul (result, a, q, ctx);
1538 gcry_mpi_point_release(q); 1538 gcry_mpi_point_release (q);
1539 gcry_mpi_release(a); 1539 gcry_mpi_release (a);
1540 1540
1541 ret = point_to_hash(result, ctx, key_material); 1541 ret = point_to_hash (result, ctx, key_material);
1542 gcry_mpi_point_release(result); 1542 gcry_mpi_point_release (result);
1543 gcry_ctx_release(ctx); 1543 gcry_ctx_release (ctx);
1544 BENCHMARK_END(eddsa_ecdh); 1544 BENCHMARK_END (eddsa_ecdh);
1545 return ret; 1545 return ret;
1546} 1546}
1547 1547
@@ -1557,9 +1557,9 @@ GNUNET_CRYPTO_eddsa_ecdh(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1557 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 1557 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1558 */ 1558 */
1559int 1559int
1560GNUNET_CRYPTO_ecdsa_ecdh(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, 1560GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1561 const struct GNUNET_CRYPTO_EcdhePublicKey *pub, 1561 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1562 struct GNUNET_HashCode *key_material) 1562 struct GNUNET_HashCode *key_material)
1563{ 1563{
1564 gcry_mpi_point_t result; 1564 gcry_mpi_point_t result;
1565 gcry_mpi_point_t q; 1565 gcry_mpi_point_t q;
@@ -1568,33 +1568,33 @@ GNUNET_CRYPTO_ecdsa_ecdh(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1568 gcry_sexp_t pub_sexpr; 1568 gcry_sexp_t pub_sexpr;
1569 int ret; 1569 int ret;
1570 1570
1571 BENCHMARK_START(ecdsa_ecdh); 1571 BENCHMARK_START (ecdsa_ecdh);
1572 1572
1573 /* first, extract the q = dP value from the public key */ 1573 /* first, extract the q = dP value from the public key */
1574 if (0 != gcry_sexp_build(&pub_sexpr, 1574 if (0 != gcry_sexp_build (&pub_sexpr,
1575 NULL, 1575 NULL,
1576 "(public-key(ecc(curve " CURVE ")(q %b)))", 1576 "(public-key(ecc(curve " CURVE ")(q %b)))",
1577 (int)sizeof(pub->q_y), 1577 (int) sizeof(pub->q_y),
1578 pub->q_y)) 1578 pub->q_y))
1579 return GNUNET_SYSERR; 1579 return GNUNET_SYSERR;
1580 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, pub_sexpr, NULL)); 1580 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1581 gcry_sexp_release(pub_sexpr); 1581 gcry_sexp_release (pub_sexpr);
1582 q = gcry_mpi_ec_get_point("q", ctx, 0); 1582 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1583 1583
1584 /* second, extract the d value from our private key */ 1584 /* second, extract the d value from our private key */
1585 GNUNET_CRYPTO_mpi_scan_unsigned(&d, priv->d, sizeof(priv->d)); 1585 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof(priv->d));
1586 1586
1587 /* then call the 'multiply' function, to compute the product */ 1587 /* then call the 'multiply' function, to compute the product */
1588 result = gcry_mpi_point_new(0); 1588 result = gcry_mpi_point_new (0);
1589 gcry_mpi_ec_mul(result, d, q, ctx); 1589 gcry_mpi_ec_mul (result, d, q, ctx);
1590 gcry_mpi_point_release(q); 1590 gcry_mpi_point_release (q);
1591 gcry_mpi_release(d); 1591 gcry_mpi_release (d);
1592 1592
1593 /* finally, convert point to string for hashing */ 1593 /* finally, convert point to string for hashing */
1594 ret = point_to_hash(result, ctx, key_material); 1594 ret = point_to_hash (result, ctx, key_material);
1595 gcry_mpi_point_release(result); 1595 gcry_mpi_point_release (result);
1596 gcry_ctx_release(ctx); 1596 gcry_ctx_release (ctx);
1597 BENCHMARK_END(ecdsa_ecdh); 1597 BENCHMARK_END (ecdsa_ecdh);
1598 return ret; 1598 return ret;
1599} 1599}
1600 1600
@@ -1610,9 +1610,9 @@ GNUNET_CRYPTO_ecdsa_ecdh(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1610 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 1610 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1611 */ 1611 */
1612int 1612int
1613GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, 1613GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1614 const struct GNUNET_CRYPTO_EddsaPublicKey *pub, 1614 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1615 struct GNUNET_HashCode *key_material) 1615 struct GNUNET_HashCode *key_material)
1616{ 1616{
1617 gcry_mpi_point_t result; 1617 gcry_mpi_point_t result;
1618 gcry_mpi_point_t q; 1618 gcry_mpi_point_t q;
@@ -1621,33 +1621,33 @@ GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1621 gcry_sexp_t pub_sexpr; 1621 gcry_sexp_t pub_sexpr;
1622 int ret; 1622 int ret;
1623 1623
1624 BENCHMARK_START(ecdh_eddsa); 1624 BENCHMARK_START (ecdh_eddsa);
1625 1625
1626 /* first, extract the q = dP value from the public key */ 1626 /* first, extract the q = dP value from the public key */
1627 if (0 != gcry_sexp_build(&pub_sexpr, 1627 if (0 != gcry_sexp_build (&pub_sexpr,
1628 NULL, 1628 NULL,
1629 "(public-key(ecc(curve " CURVE ")(q %b)))", 1629 "(public-key(ecc(curve " CURVE ")(q %b)))",
1630 (int)sizeof(pub->q_y), 1630 (int) sizeof(pub->q_y),
1631 pub->q_y)) 1631 pub->q_y))
1632 return GNUNET_SYSERR; 1632 return GNUNET_SYSERR;
1633 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, pub_sexpr, NULL)); 1633 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1634 gcry_sexp_release(pub_sexpr); 1634 gcry_sexp_release (pub_sexpr);
1635 q = gcry_mpi_ec_get_point("q", ctx, 0); 1635 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1636 1636
1637 /* second, extract the d value from our private key */ 1637 /* second, extract the d value from our private key */
1638 GNUNET_CRYPTO_mpi_scan_unsigned(&d, priv->d, sizeof(priv->d)); 1638 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof(priv->d));
1639 1639
1640 /* then call the 'multiply' function, to compute the product */ 1640 /* then call the 'multiply' function, to compute the product */
1641 result = gcry_mpi_point_new(0); 1641 result = gcry_mpi_point_new (0);
1642 gcry_mpi_ec_mul(result, d, q, ctx); 1642 gcry_mpi_ec_mul (result, d, q, ctx);
1643 gcry_mpi_point_release(q); 1643 gcry_mpi_point_release (q);
1644 gcry_mpi_release(d); 1644 gcry_mpi_release (d);
1645 1645
1646 /* finally, convert point to string for hashing */ 1646 /* finally, convert point to string for hashing */
1647 ret = point_to_hash(result, ctx, key_material); 1647 ret = point_to_hash (result, ctx, key_material);
1648 gcry_mpi_point_release(result); 1648 gcry_mpi_point_release (result);
1649 gcry_ctx_release(ctx); 1649 gcry_ctx_release (ctx);
1650 BENCHMARK_END(ecdh_eddsa); 1650 BENCHMARK_END (ecdh_eddsa);
1651 return ret; 1651 return ret;
1652} 1652}
1653 1653
@@ -1662,14 +1662,14 @@ GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1662 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 1662 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1663 */ 1663 */
1664int 1664int
1665GNUNET_CRYPTO_ecdh_ecdsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, 1665GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1666 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, 1666 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1667 struct GNUNET_HashCode *key_material) 1667 struct GNUNET_HashCode *key_material)
1668{ 1668{
1669 return GNUNET_CRYPTO_ecdh_eddsa(priv, 1669 return GNUNET_CRYPTO_ecdh_eddsa (priv,
1670 (const struct GNUNET_CRYPTO_EddsaPublicKey *) 1670 (const struct GNUNET_CRYPTO_EddsaPublicKey *)
1671 pub, 1671 pub,
1672 key_material); 1672 key_material);
1673} 1673}
1674 1674
1675/* end of crypto_ecc.c */ 1675/* end of crypto_ecc.c */
diff --git a/src/util/crypto_ecc_dlog.c b/src/util/crypto_ecc_dlog.c
index 8b4344b25..6b0dc3503 100644
--- a/src/util/crypto_ecc_dlog.c
+++ b/src/util/crypto_ecc_dlog.c
@@ -44,26 +44,27 @@
44 * 44 *
45 */ 45 */
46static void 46static void
47extract_pk(gcry_mpi_point_t pt, 47extract_pk (gcry_mpi_point_t pt,
48 gcry_ctx_t ctx, 48 gcry_ctx_t ctx,
49 struct GNUNET_PeerIdentity *pid) 49 struct GNUNET_PeerIdentity *pid)
50{ 50{
51 gcry_mpi_t q_y; 51 gcry_mpi_t q_y;
52 52
53 GNUNET_assert(0 == gcry_mpi_ec_set_point("q", pt, ctx)); 53 GNUNET_assert (0 == gcry_mpi_ec_set_point ("q", pt, ctx));
54 q_y = gcry_mpi_ec_get_mpi("q@eddsa", ctx, 0); 54 q_y = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
55 GNUNET_assert(q_y); 55 GNUNET_assert (q_y);
56 GNUNET_CRYPTO_mpi_print_unsigned(pid->public_key.q_y, 56 GNUNET_CRYPTO_mpi_print_unsigned (pid->public_key.q_y,
57 sizeof(pid->public_key.q_y), 57 sizeof(pid->public_key.q_y),
58 q_y); 58 q_y);
59 gcry_mpi_release(q_y); 59 gcry_mpi_release (q_y);
60} 60}
61 61
62 62
63/** 63/**
64 * Internal structure used to cache pre-calculated values for DLOG calculation. 64 * Internal structure used to cache pre-calculated values for DLOG calculation.
65 */ 65 */
66struct GNUNET_CRYPTO_EccDlogContext { 66struct GNUNET_CRYPTO_EccDlogContext
67{
67 /** 68 /**
68 * Maximum absolute value the calculation supports. 69 * Maximum absolute value the calculation supports.
69 */ 70 */
@@ -97,19 +98,19 @@ struct GNUNET_CRYPTO_EccDlogContext {
97 * @param[out] bin binary point representation 98 * @param[out] bin binary point representation
98 */ 99 */
99void 100void
100GNUNET_CRYPTO_ecc_point_to_bin(struct GNUNET_CRYPTO_EccDlogContext *edc, 101GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
101 gcry_mpi_point_t point, 102 gcry_mpi_point_t point,
102 struct GNUNET_CRYPTO_EccPoint *bin) 103 struct GNUNET_CRYPTO_EccPoint *bin)
103{ 104{
104 gcry_mpi_t q_y; 105 gcry_mpi_t q_y;
105 106
106 GNUNET_assert(0 == gcry_mpi_ec_set_point("q", point, edc->ctx)); 107 GNUNET_assert (0 == gcry_mpi_ec_set_point ("q", point, edc->ctx));
107 q_y = gcry_mpi_ec_get_mpi("q@eddsa", edc->ctx, 0); 108 q_y = gcry_mpi_ec_get_mpi ("q@eddsa", edc->ctx, 0);
108 GNUNET_assert(q_y); 109 GNUNET_assert (q_y);
109 GNUNET_CRYPTO_mpi_print_unsigned(bin->q_y, 110 GNUNET_CRYPTO_mpi_print_unsigned (bin->q_y,
110 sizeof(bin->q_y), 111 sizeof(bin->q_y),
111 q_y); 112 q_y);
112 gcry_mpi_release(q_y); 113 gcry_mpi_release (q_y);
113} 114}
114 115
115 116
@@ -121,26 +122,26 @@ GNUNET_CRYPTO_ecc_point_to_bin(struct GNUNET_CRYPTO_EccDlogContext *edc,
121 * @return computational representation 122 * @return computational representation
122 */ 123 */
123gcry_mpi_point_t 124gcry_mpi_point_t
124GNUNET_CRYPTO_ecc_bin_to_point(struct GNUNET_CRYPTO_EccDlogContext *edc, 125GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
125 const struct GNUNET_CRYPTO_EccPoint *bin) 126 const struct GNUNET_CRYPTO_EccPoint *bin)
126{ 127{
127 gcry_sexp_t pub_sexpr; 128 gcry_sexp_t pub_sexpr;
128 gcry_ctx_t ctx; 129 gcry_ctx_t ctx;
129 gcry_mpi_point_t q; 130 gcry_mpi_point_t q;
130 131
131 (void)edc; 132 (void) edc;
132 if (0 != gcry_sexp_build(&pub_sexpr, NULL, 133 if (0 != gcry_sexp_build (&pub_sexpr, NULL,
133 "(public-key(ecc(curve " CURVE ")(q %b)))", 134 "(public-key(ecc(curve " CURVE ")(q %b)))",
134 (int)sizeof(bin->q_y), 135 (int) sizeof(bin->q_y),
135 bin->q_y)) 136 bin->q_y))
136 { 137 {
137 GNUNET_break(0); 138 GNUNET_break (0);
138 return NULL; 139 return NULL;
139 } 140 }
140 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, pub_sexpr, NULL)); 141 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
141 gcry_sexp_release(pub_sexpr); 142 gcry_sexp_release (pub_sexpr);
142 q = gcry_mpi_ec_get_point("q", ctx, 0); 143 q = gcry_mpi_ec_get_point ("q", ctx, 0);
143 gcry_ctx_release(ctx); 144 gcry_ctx_release (ctx);
144 return q; 145 return q;
145} 146}
146 147
@@ -153,8 +154,8 @@ GNUNET_CRYPTO_ecc_bin_to_point(struct GNUNET_CRYPTO_EccDlogContext *edc,
153 * @return NULL on error 154 * @return NULL on error
154 */ 155 */
155struct GNUNET_CRYPTO_EccDlogContext * 156struct GNUNET_CRYPTO_EccDlogContext *
156GNUNET_CRYPTO_ecc_dlog_prepare(unsigned int max, 157GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
157 unsigned int mem) 158 unsigned int mem)
158{ 159{
159 struct GNUNET_CRYPTO_EccDlogContext *edc; 160 struct GNUNET_CRYPTO_EccDlogContext *edc;
160 unsigned int K = ((max + (mem - 1)) / mem); 161 unsigned int K = ((max + (mem - 1)) / mem);
@@ -165,50 +166,50 @@ GNUNET_CRYPTO_ecc_dlog_prepare(unsigned int max,
165 gcry_mpi_t n; 166 gcry_mpi_t n;
166 unsigned int i; 167 unsigned int i;
167 168
168 GNUNET_assert(max < INT32_MAX); 169 GNUNET_assert (max < INT32_MAX);
169 edc = GNUNET_new(struct GNUNET_CRYPTO_EccDlogContext); 170 edc = GNUNET_new (struct GNUNET_CRYPTO_EccDlogContext);
170 edc->max = max; 171 edc->max = max;
171 edc->mem = mem; 172 edc->mem = mem;
172 173
173 edc->map = GNUNET_CONTAINER_multipeermap_create(mem * 2, 174 edc->map = GNUNET_CONTAINER_multipeermap_create (mem * 2,
174 GNUNET_NO); 175 GNUNET_NO);
175 176
176 GNUNET_assert(0 == gcry_mpi_ec_new(&edc->ctx, 177 GNUNET_assert (0 == gcry_mpi_ec_new (&edc->ctx,
177 NULL, 178 NULL,
178 CURVE)); 179 CURVE));
179 g = gcry_mpi_ec_get_point("g", edc->ctx, 0); 180 g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
180 GNUNET_assert(NULL != g); 181 GNUNET_assert (NULL != g);
181 fact = gcry_mpi_new(0); 182 fact = gcry_mpi_new (0);
182 gKi = gcry_mpi_point_new(0); 183 gKi = gcry_mpi_point_new (0);
183 for (i = 0; i <= mem; i++) 184 for (i = 0; i <= mem; i++)
184 { 185 {
185 gcry_mpi_set_ui(fact, i * K); 186 gcry_mpi_set_ui (fact, i * K);
186 gcry_mpi_ec_mul(gKi, fact, g, edc->ctx); 187 gcry_mpi_ec_mul (gKi, fact, g, edc->ctx);
187 extract_pk(gKi, edc->ctx, &key); 188 extract_pk (gKi, edc->ctx, &key);
188 GNUNET_assert(GNUNET_OK == 189 GNUNET_assert (GNUNET_OK ==
189 GNUNET_CONTAINER_multipeermap_put(edc->map, 190 GNUNET_CONTAINER_multipeermap_put (edc->map,
190 &key, 191 &key,
191 (void*)(long)i + max, 192 (void*) (long) i + max,
192 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 193 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
193 } 194 }
194 /* negative values */ 195 /* negative values */
195 n = gcry_mpi_ec_get_mpi("n", edc->ctx, 1); 196 n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
196 for (i = 1; i < mem; i++) 197 for (i = 1; i < mem; i++)
197 { 198 {
198 gcry_mpi_set_ui(fact, i * K); 199 gcry_mpi_set_ui (fact, i * K);
199 gcry_mpi_sub(fact, n, fact); 200 gcry_mpi_sub (fact, n, fact);
200 gcry_mpi_ec_mul(gKi, fact, g, edc->ctx); 201 gcry_mpi_ec_mul (gKi, fact, g, edc->ctx);
201 extract_pk(gKi, edc->ctx, &key); 202 extract_pk (gKi, edc->ctx, &key);
202 GNUNET_assert(GNUNET_OK == 203 GNUNET_assert (GNUNET_OK ==
203 GNUNET_CONTAINER_multipeermap_put(edc->map, 204 GNUNET_CONTAINER_multipeermap_put (edc->map,
204 &key, 205 &key,
205 (void*)(long)max - i, 206 (void*) (long) max - i,
206 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 207 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
207 } 208 }
208 gcry_mpi_release(fact); 209 gcry_mpi_release (fact);
209 gcry_mpi_release(n); 210 gcry_mpi_release (n);
210 gcry_mpi_point_release(gKi); 211 gcry_mpi_point_release (gKi);
211 gcry_mpi_point_release(g); 212 gcry_mpi_point_release (g);
212 return edc; 213 return edc;
213} 214}
214 215
@@ -221,8 +222,8 @@ GNUNET_CRYPTO_ecc_dlog_prepare(unsigned int max,
221 * @return INT_MAX if dlog failed, otherwise the factor 222 * @return INT_MAX if dlog failed, otherwise the factor
222 */ 223 */
223int 224int
224GNUNET_CRYPTO_ecc_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc, 225GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
225 gcry_mpi_point_t input) 226 gcry_mpi_point_t input)
226{ 227{
227 unsigned int K = ((edc->max + (edc->mem - 1)) / edc->mem); 228 unsigned int K = ((edc->max + (edc->mem - 1)) / edc->mem);
228 gcry_mpi_point_t g; 229 gcry_mpi_point_t g;
@@ -232,36 +233,36 @@ GNUNET_CRYPTO_ecc_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc,
232 int res; 233 int res;
233 void *retp; 234 void *retp;
234 235
235 g = gcry_mpi_ec_get_point("g", edc->ctx, 0); 236 g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
236 GNUNET_assert(NULL != g); 237 GNUNET_assert (NULL != g);
237 q = gcry_mpi_point_new(0); 238 q = gcry_mpi_point_new (0);
238 239
239 res = INT_MAX; 240 res = INT_MAX;
240 for (i = 0; i <= edc->max / edc->mem; i++) 241 for (i = 0; i <= edc->max / edc->mem; i++)
242 {
243 if (0 == i)
244 extract_pk (input, edc->ctx, &key);
245 else
246 extract_pk (q, edc->ctx, &key);
247 retp = GNUNET_CONTAINER_multipeermap_get (edc->map,
248 &key);
249 if (NULL != retp)
241 { 250 {
242 if (0 == i) 251 res = (((long) retp) - edc->max) * K - i;
243 extract_pk(input, edc->ctx, &key); 252 /* we continue the loop here to make the implementation
244 else 253 "constant-time". If we do not care about this, we could just
245 extract_pk(q, edc->ctx, &key); 254 'break' here and do fewer operations... */
246 retp = GNUNET_CONTAINER_multipeermap_get(edc->map,
247 &key);
248 if (NULL != retp)
249 {
250 res = (((long)retp) - edc->max) * K - i;
251 /* we continue the loop here to make the implementation
252 "constant-time". If we do not care about this, we could just
253 'break' here and do fewer operations... */
254 }
255 if (i == edc->max / edc->mem)
256 break;
257 /* q = q + g */
258 if (0 == i)
259 gcry_mpi_ec_add(q, input, g, edc->ctx);
260 else
261 gcry_mpi_ec_add(q, q, g, edc->ctx);
262 } 255 }
263 gcry_mpi_point_release(g); 256 if (i == edc->max / edc->mem)
264 gcry_mpi_point_release(q); 257 break;
258 /* q = q + g */
259 if (0 == i)
260 gcry_mpi_ec_add (q, input, g, edc->ctx);
261 else
262 gcry_mpi_ec_add (q, q, g, edc->ctx);
263 }
264 gcry_mpi_point_release (g);
265 gcry_mpi_point_release (q);
265 266
266 return res; 267 return res;
267} 268}
@@ -274,30 +275,30 @@ GNUNET_CRYPTO_ecc_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc,
274 * @return random value mod n. 275 * @return random value mod n.
275 */ 276 */
276gcry_mpi_t 277gcry_mpi_t
277GNUNET_CRYPTO_ecc_random_mod_n(struct GNUNET_CRYPTO_EccDlogContext *edc) 278GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc)
278{ 279{
279 gcry_mpi_t n; 280 gcry_mpi_t n;
280 unsigned int highbit; 281 unsigned int highbit;
281 gcry_mpi_t r; 282 gcry_mpi_t r;
282 283
283 n = gcry_mpi_ec_get_mpi("n", edc->ctx, 1); 284 n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
284 285
285 /* check public key for number of bits, bail out if key is all zeros */ 286 /* check public key for number of bits, bail out if key is all zeros */
286 highbit = 256; /* Curve25519 */ 287 highbit = 256; /* Curve25519 */
287 while ((!gcry_mpi_test_bit(n, highbit)) && 288 while ((! gcry_mpi_test_bit (n, highbit)) &&
288 (0 != highbit)) 289 (0 != highbit))
289 highbit--; 290 highbit--;
290 GNUNET_assert(0 != highbit); 291 GNUNET_assert (0 != highbit);
291 /* generate fact < n (without bias) */ 292 /* generate fact < n (without bias) */
292 GNUNET_assert(NULL != (r = gcry_mpi_new(0))); 293 GNUNET_assert (NULL != (r = gcry_mpi_new (0)));
293 do 294 do
294 { 295 {
295 gcry_mpi_randomize(r, 296 gcry_mpi_randomize (r,
296 highbit + 1, 297 highbit + 1,
297 GCRY_STRONG_RANDOM); 298 GCRY_STRONG_RANDOM);
298 } 299 }
299 while (gcry_mpi_cmp(r, n) >= 0); 300 while (gcry_mpi_cmp (r, n) >= 0);
300 gcry_mpi_release(n); 301 gcry_mpi_release (n);
301 return r; 302 return r;
302} 303}
303 304
@@ -308,11 +309,11 @@ GNUNET_CRYPTO_ecc_random_mod_n(struct GNUNET_CRYPTO_EccDlogContext *edc)
308 * @param edc dlog context 309 * @param edc dlog context
309 */ 310 */
310void 311void
311GNUNET_CRYPTO_ecc_dlog_release(struct GNUNET_CRYPTO_EccDlogContext *edc) 312GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *edc)
312{ 313{
313 gcry_ctx_release(edc->ctx); 314 gcry_ctx_release (edc->ctx);
314 GNUNET_CONTAINER_multipeermap_destroy(edc->map); 315 GNUNET_CONTAINER_multipeermap_destroy (edc->map);
315 GNUNET_free(edc); 316 GNUNET_free (edc);
316} 317}
317 318
318 319
@@ -330,32 +331,32 @@ GNUNET_CRYPTO_ecc_dlog_release(struct GNUNET_CRYPTO_EccDlogContext *edc)
330 * must be freed using #GNUNET_CRYPTO_ecc_free() 331 * must be freed using #GNUNET_CRYPTO_ecc_free()
331 */ 332 */
332gcry_mpi_point_t 333gcry_mpi_point_t
333GNUNET_CRYPTO_ecc_dexp(struct GNUNET_CRYPTO_EccDlogContext *edc, 334GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
334 int val) 335 int val)
335{ 336{
336 gcry_mpi_t fact; 337 gcry_mpi_t fact;
337 gcry_mpi_t n; 338 gcry_mpi_t n;
338 gcry_mpi_point_t g; 339 gcry_mpi_point_t g;
339 gcry_mpi_point_t r; 340 gcry_mpi_point_t r;
340 341
341 g = gcry_mpi_ec_get_point("g", edc->ctx, 0); 342 g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
342 GNUNET_assert(NULL != g); 343 GNUNET_assert (NULL != g);
343 fact = gcry_mpi_new(0); 344 fact = gcry_mpi_new (0);
344 if (val < 0) 345 if (val < 0)
345 { 346 {
346 n = gcry_mpi_ec_get_mpi("n", edc->ctx, 1); 347 n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
347 gcry_mpi_set_ui(fact, -val); 348 gcry_mpi_set_ui (fact, -val);
348 gcry_mpi_sub(fact, n, fact); 349 gcry_mpi_sub (fact, n, fact);
349 gcry_mpi_release(n); 350 gcry_mpi_release (n);
350 } 351 }
351 else 352 else
352 { 353 {
353 gcry_mpi_set_ui(fact, val); 354 gcry_mpi_set_ui (fact, val);
354 } 355 }
355 r = gcry_mpi_point_new(0); 356 r = gcry_mpi_point_new (0);
356 gcry_mpi_ec_mul(r, fact, g, edc->ctx); 357 gcry_mpi_ec_mul (r, fact, g, edc->ctx);
357 gcry_mpi_release(fact); 358 gcry_mpi_release (fact);
358 gcry_mpi_point_release(g); 359 gcry_mpi_point_release (g);
359 return r; 360 return r;
360} 361}
361 362
@@ -370,17 +371,17 @@ GNUNET_CRYPTO_ecc_dexp(struct GNUNET_CRYPTO_EccDlogContext *edc,
370 * must be freed using #GNUNET_CRYPTO_ecc_free() 371 * must be freed using #GNUNET_CRYPTO_ecc_free()
371 */ 372 */
372gcry_mpi_point_t 373gcry_mpi_point_t
373GNUNET_CRYPTO_ecc_dexp_mpi(struct GNUNET_CRYPTO_EccDlogContext *edc, 374GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
374 gcry_mpi_t val) 375 gcry_mpi_t val)
375{ 376{
376 gcry_mpi_point_t g; 377 gcry_mpi_point_t g;
377 gcry_mpi_point_t r; 378 gcry_mpi_point_t r;
378 379
379 g = gcry_mpi_ec_get_point("g", edc->ctx, 0); 380 g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
380 GNUNET_assert(NULL != g); 381 GNUNET_assert (NULL != g);
381 r = gcry_mpi_point_new(0); 382 r = gcry_mpi_point_new (0);
382 gcry_mpi_ec_mul(r, val, g, edc->ctx); 383 gcry_mpi_ec_mul (r, val, g, edc->ctx);
383 gcry_mpi_point_release(g); 384 gcry_mpi_point_release (g);
384 return r; 385 return r;
385} 386}
386 387
@@ -394,14 +395,14 @@ GNUNET_CRYPTO_ecc_dexp_mpi(struct GNUNET_CRYPTO_EccDlogContext *edc,
394 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free() 395 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
395 */ 396 */
396gcry_mpi_point_t 397gcry_mpi_point_t
397GNUNET_CRYPTO_ecc_add(struct GNUNET_CRYPTO_EccDlogContext *edc, 398GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
398 gcry_mpi_point_t a, 399 gcry_mpi_point_t a,
399 gcry_mpi_point_t b) 400 gcry_mpi_point_t b)
400{ 401{
401 gcry_mpi_point_t r; 402 gcry_mpi_point_t r;
402 403
403 r = gcry_mpi_point_new(0); 404 r = gcry_mpi_point_new (0);
404 gcry_mpi_ec_add(r, a, b, edc->ctx); 405 gcry_mpi_ec_add (r, a, b, edc->ctx);
405 return r; 406 return r;
406} 407}
407 408
@@ -416,14 +417,14 @@ GNUNET_CRYPTO_ecc_add(struct GNUNET_CRYPTO_EccDlogContext *edc,
416 * must be freed using #GNUNET_CRYPTO_ecc_free() 417 * must be freed using #GNUNET_CRYPTO_ecc_free()
417 */ 418 */
418gcry_mpi_point_t 419gcry_mpi_point_t
419GNUNET_CRYPTO_ecc_pmul_mpi(struct GNUNET_CRYPTO_EccDlogContext *edc, 420GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
420 gcry_mpi_point_t p, 421 gcry_mpi_point_t p,
421 gcry_mpi_t val) 422 gcry_mpi_t val)
422{ 423{
423 gcry_mpi_point_t r; 424 gcry_mpi_point_t r;
424 425
425 r = gcry_mpi_point_new(0); 426 r = gcry_mpi_point_new (0);
426 gcry_mpi_ec_mul(r, val, p, edc->ctx); 427 gcry_mpi_ec_mul (r, val, p, edc->ctx);
427 return r; 428 return r;
428} 429}
429 430
@@ -438,31 +439,31 @@ GNUNET_CRYPTO_ecc_pmul_mpi(struct GNUNET_CRYPTO_EccDlogContext *edc,
438 * @param[out] r_inv set to the additive inverse of @a r 439 * @param[out] r_inv set to the additive inverse of @a r
439 */ 440 */
440void 441void
441GNUNET_CRYPTO_ecc_rnd(struct GNUNET_CRYPTO_EccDlogContext *edc, 442GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
442 gcry_mpi_point_t *r, 443 gcry_mpi_point_t *r,
443 gcry_mpi_point_t *r_inv) 444 gcry_mpi_point_t *r_inv)
444{ 445{
445 gcry_mpi_t fact; 446 gcry_mpi_t fact;
446 gcry_mpi_t n; 447 gcry_mpi_t n;
447 gcry_mpi_point_t g; 448 gcry_mpi_point_t g;
448 449
449 fact = GNUNET_CRYPTO_ecc_random_mod_n(edc); 450 fact = GNUNET_CRYPTO_ecc_random_mod_n (edc);
450 451
451 /* calculate 'r' */ 452 /* calculate 'r' */
452 g = gcry_mpi_ec_get_point("g", edc->ctx, 0); 453 g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
453 GNUNET_assert(NULL != g); 454 GNUNET_assert (NULL != g);
454 *r = gcry_mpi_point_new(0); 455 *r = gcry_mpi_point_new (0);
455 gcry_mpi_ec_mul(*r, fact, g, edc->ctx); 456 gcry_mpi_ec_mul (*r, fact, g, edc->ctx);
456 457
457 /* calculate 'r_inv' */ 458 /* calculate 'r_inv' */
458 n = gcry_mpi_ec_get_mpi("n", edc->ctx, 1); 459 n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
459 gcry_mpi_sub(fact, n, fact); /* fact = n - fact = - fact */ 460 gcry_mpi_sub (fact, n, fact); /* fact = n - fact = - fact */
460 *r_inv = gcry_mpi_point_new(0); 461 *r_inv = gcry_mpi_point_new (0);
461 gcry_mpi_ec_mul(*r_inv, fact, g, edc->ctx); 462 gcry_mpi_ec_mul (*r_inv, fact, g, edc->ctx);
462 463
463 gcry_mpi_release(n); 464 gcry_mpi_release (n);
464 gcry_mpi_release(fact); 465 gcry_mpi_release (fact);
465 gcry_mpi_point_release(g); 466 gcry_mpi_point_release (g);
466} 467}
467 468
468 469
@@ -475,17 +476,17 @@ GNUNET_CRYPTO_ecc_rnd(struct GNUNET_CRYPTO_EccDlogContext *edc,
475 * @param[out] r_inv set to the multiplicative inverse of @a r 476 * @param[out] r_inv set to the multiplicative inverse of @a r
476 */ 477 */
477void 478void
478GNUNET_CRYPTO_ecc_rnd_mpi(struct GNUNET_CRYPTO_EccDlogContext *edc, 479GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
479 gcry_mpi_t *r, 480 gcry_mpi_t *r,
480 gcry_mpi_t *r_inv) 481 gcry_mpi_t *r_inv)
481{ 482{
482 gcry_mpi_t n; 483 gcry_mpi_t n;
483 484
484 *r = GNUNET_CRYPTO_ecc_random_mod_n(edc); 485 *r = GNUNET_CRYPTO_ecc_random_mod_n (edc);
485 /* r_inv = n - r = - r */ 486 /* r_inv = n - r = - r */
486 *r_inv = gcry_mpi_new(0); 487 *r_inv = gcry_mpi_new (0);
487 n = gcry_mpi_ec_get_mpi("n", edc->ctx, 1); 488 n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
488 gcry_mpi_sub(*r_inv, n, *r); 489 gcry_mpi_sub (*r_inv, n, *r);
489} 490}
490 491
491 492
@@ -495,9 +496,9 @@ GNUNET_CRYPTO_ecc_rnd_mpi(struct GNUNET_CRYPTO_EccDlogContext *edc,
495 * @param p point to free 496 * @param p point to free
496 */ 497 */
497void 498void
498GNUNET_CRYPTO_ecc_free(gcry_mpi_point_t p) 499GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p)
499{ 500{
500 gcry_mpi_point_release(p); 501 gcry_mpi_point_release (p);
501} 502}
502 503
503 504
diff --git a/src/util/crypto_ecc_setup.c b/src/util/crypto_ecc_setup.c
index af42df613..0ce9f9d1e 100644
--- a/src/util/crypto_ecc_setup.c
+++ b/src/util/crypto_ecc_setup.c
@@ -27,13 +27,13 @@
27#include <gcrypt.h> 27#include <gcrypt.h>
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-ecc", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
31 31
32#define LOG_STRERROR(kind, syscall) \ 32#define LOG_STRERROR(kind, syscall) \
33 GNUNET_log_from_strerror(kind, "util-crypto-ecc", syscall) 33 GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
34 34
35#define LOG_STRERROR_FILE(kind, syscall, filename) \ 35#define LOG_STRERROR_FILE(kind, syscall, filename) \
36 GNUNET_log_from_strerror_file(kind, "util-crypto-ecc", syscall, filename) 36 GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
37 37
38/** 38/**
39 * Log an error message at log-level 'level' that indicates 39 * Log an error message at log-level 'level' that indicates
@@ -42,14 +42,14 @@
42 */ 42 */
43#define LOG_GCRY(level, cmd, rc) \ 43#define LOG_GCRY(level, cmd, rc) \
44 do \ 44 do \
45 { \ 45 { \
46 LOG(level, \ 46 LOG (level, \
47 _("`%s' failed at %s:%d with error: %s\n"), \ 47 _ ("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, \ 48 cmd, \
49 __FILE__, \ 49 __FILE__, \
50 __LINE__, \ 50 __LINE__, \
51 gcry_strerror(rc)); \ 51 gcry_strerror (rc)); \
52 } while (0) 52 } while (0)
53 53
54 54
55/** 55/**
@@ -60,12 +60,12 @@
60 * some 'real' work). 60 * some 'real' work).
61 */ 61 */
62static void 62static void
63short_wait() 63short_wait ()
64{ 64{
65 struct GNUNET_TIME_Relative timeout; 65 struct GNUNET_TIME_Relative timeout;
66 66
67 timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100); 67 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
68 (void)GNUNET_NETWORK_socket_select(NULL, NULL, NULL, timeout); 68 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
69} 69}
70 70
71 71
@@ -84,7 +84,7 @@ short_wait()
84 * permission denied) 84 * permission denied)
85 */ 85 */
86struct GNUNET_CRYPTO_EddsaPrivateKey * 86struct GNUNET_CRYPTO_EddsaPrivateKey *
87GNUNET_CRYPTO_eddsa_key_create_from_file(const char *filename) 87GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
88{ 88{
89 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 89 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
90 struct GNUNET_DISK_FileHandle *fd; 90 struct GNUNET_DISK_FileHandle *fd;
@@ -93,159 +93,159 @@ GNUNET_CRYPTO_eddsa_key_create_from_file(const char *filename)
93 uint64_t fs; 93 uint64_t fs;
94 ssize_t sret; 94 ssize_t sret;
95 95
96 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file(filename)) 96 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
97 return NULL; 97 return NULL;
98 while (GNUNET_YES != GNUNET_DISK_file_test(filename)) 98 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
99 {
100 fd =
101 GNUNET_DISK_file_open (filename,
102 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
103 | GNUNET_DISK_OPEN_FAILIFEXISTS,
104 GNUNET_DISK_PERM_USER_READ
105 | GNUNET_DISK_PERM_USER_WRITE);
106 if (NULL == fd)
99 { 107 {
100 fd = 108 if (EEXIST == errno)
101 GNUNET_DISK_file_open(filename, 109 {
102 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 110 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
103 GNUNET_DISK_OPEN_FAILIFEXISTS,
104 GNUNET_DISK_PERM_USER_READ |
105 GNUNET_DISK_PERM_USER_WRITE);
106 if (NULL == fd)
107 { 111 {
108 if (EEXIST == errno) 112 /* must exist but not be accessible, fail for good! */
109 { 113 if (0 != access (filename, R_OK))
110 if (GNUNET_YES != GNUNET_DISK_file_test(filename)) 114 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
111 { 115 else
112 /* must exist but not be accessible, fail for good! */ 116 GNUNET_break (0); /* what is going on!? */
113 if (0 != access(filename, R_OK))
114 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "access", filename);
115 else
116 GNUNET_break(0); /* what is going on!? */
117 return NULL;
118 }
119 continue;
120 }
121 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
122 return NULL; 117 return NULL;
123 } 118 }
124 cnt = 0; 119 continue;
125 while (GNUNET_YES != 120 }
126 GNUNET_DISK_file_lock(fd, 121 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
127 0, 122 return NULL;
128 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey), 123 }
129 GNUNET_YES)) 124 cnt = 0;
130 { 125 while (GNUNET_YES !=
131 short_wait(); 126 GNUNET_DISK_file_lock (fd,
132 if (0 == ++cnt % 10)
133 {
134 ec = errno;
135 LOG(GNUNET_ERROR_TYPE_ERROR,
136 _("Could not acquire lock on file `%s': %s...\n"),
137 filename,
138 strerror(ec));
139 }
140 }
141 LOG(GNUNET_ERROR_TYPE_INFO,
142 _("Creating a new private key. This may take a while.\n"));
143 priv = GNUNET_CRYPTO_eddsa_key_create();
144 GNUNET_assert(NULL != priv);
145 GNUNET_assert(sizeof(*priv) ==
146 GNUNET_DISK_file_write(fd, priv, sizeof(*priv)));
147 GNUNET_DISK_file_sync(fd);
148 if (GNUNET_YES !=
149 GNUNET_DISK_file_unlock(fd,
150 0, 127 0,
151 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))) 128 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
152 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 129 GNUNET_YES))
153 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd)); 130 {
154 return priv; 131 short_wait ();
132 if (0 == ++cnt % 10)
133 {
134 ec = errno;
135 LOG (GNUNET_ERROR_TYPE_ERROR,
136 _ ("Could not acquire lock on file `%s': %s...\n"),
137 filename,
138 strerror (ec));
139 }
155 } 140 }
141 LOG (GNUNET_ERROR_TYPE_INFO,
142 _ ("Creating a new private key. This may take a while.\n"));
143 priv = GNUNET_CRYPTO_eddsa_key_create ();
144 GNUNET_assert (NULL != priv);
145 GNUNET_assert (sizeof(*priv) ==
146 GNUNET_DISK_file_write (fd, priv, sizeof(*priv)));
147 GNUNET_DISK_file_sync (fd);
148 if (GNUNET_YES !=
149 GNUNET_DISK_file_unlock (fd,
150 0,
151 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
152 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
153 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
154 return priv;
155 }
156 /* key file exists already, read it! */ 156 /* key file exists already, read it! */
157 fd = GNUNET_DISK_file_open(filename, 157 fd = GNUNET_DISK_file_open (filename,
158 GNUNET_DISK_OPEN_READ, 158 GNUNET_DISK_OPEN_READ,
159 GNUNET_DISK_PERM_NONE); 159 GNUNET_DISK_PERM_NONE);
160 if (NULL == fd) 160 if (NULL == fd)
161 { 161 {
162 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename); 162 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
163 return NULL; 163 return NULL;
164 } 164 }
165 cnt = 0; 165 cnt = 0;
166 while (1) 166 while (1)
167 {
168 if (GNUNET_YES !=
169 GNUNET_DISK_file_lock (fd,
170 0,
171 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
172 GNUNET_NO))
167 { 173 {
174 if (0 == ++cnt % 60)
175 {
176 ec = errno;
177 LOG (GNUNET_ERROR_TYPE_ERROR,
178 _ ("Could not acquire lock on file `%s': %s...\n"),
179 filename,
180 strerror (ec));
181 LOG (
182 GNUNET_ERROR_TYPE_ERROR,
183 _ (
184 "This may be ok if someone is currently generating a private key.\n"));
185 }
186 short_wait ();
187 continue;
188 }
189 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
190 {
191 /* eh, what!? File we opened is now gone!? */
192 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
168 if (GNUNET_YES != 193 if (GNUNET_YES !=
169 GNUNET_DISK_file_lock(fd, 194 GNUNET_DISK_file_unlock (fd,
170 0, 195 0,
171 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey), 196 sizeof(
172 GNUNET_NO)) 197 struct GNUNET_CRYPTO_EddsaPrivateKey)))
173 { 198 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
174 if (0 == ++cnt % 60) 199 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
175 {
176 ec = errno;
177 LOG(GNUNET_ERROR_TYPE_ERROR,
178 _("Could not acquire lock on file `%s': %s...\n"),
179 filename,
180 strerror(ec));
181 LOG(
182 GNUNET_ERROR_TYPE_ERROR,
183 _(
184 "This may be ok if someone is currently generating a private key.\n"));
185 }
186 short_wait();
187 continue;
188 }
189 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
190 {
191 /* eh, what!? File we opened is now gone!? */
192 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
193 if (GNUNET_YES !=
194 GNUNET_DISK_file_unlock(fd,
195 0,
196 sizeof(
197 struct GNUNET_CRYPTO_EddsaPrivateKey)))
198 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
199 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
200 200
201 return NULL; 201 return NULL;
202 }
203 if (GNUNET_OK !=
204 GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
205 fs = 0;
206 if (fs < sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))
207 {
208 /* maybe we got the read lock before the key generating
209 * process had a chance to get the write lock; give it up! */
210 if (GNUNET_YES !=
211 GNUNET_DISK_file_unlock(fd,
212 0,
213 sizeof(
214 struct GNUNET_CRYPTO_EddsaPrivateKey)))
215 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
216 if (0 == ++cnt % 10)
217 {
218 LOG(GNUNET_ERROR_TYPE_ERROR,
219 _(
220 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
221 filename,
222 (unsigned int)fs,
223 (unsigned int)sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey));
224 LOG(GNUNET_ERROR_TYPE_ERROR,
225 _("This may be ok if someone is currently generating a key.\n"));
226 }
227 short_wait(); /* wait a bit longer! */
228 continue;
229 }
230 break;
231 } 202 }
203 if (GNUNET_OK !=
204 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
205 fs = 0;
206 if (fs < sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))
207 {
208 /* maybe we got the read lock before the key generating
209 * process had a chance to get the write lock; give it up! */
210 if (GNUNET_YES !=
211 GNUNET_DISK_file_unlock (fd,
212 0,
213 sizeof(
214 struct GNUNET_CRYPTO_EddsaPrivateKey)))
215 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
216 if (0 == ++cnt % 10)
217 {
218 LOG (GNUNET_ERROR_TYPE_ERROR,
219 _ (
220 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
221 filename,
222 (unsigned int) fs,
223 (unsigned int) sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey));
224 LOG (GNUNET_ERROR_TYPE_ERROR,
225 _ ("This may be ok if someone is currently generating a key.\n"));
226 }
227 short_wait (); /* wait a bit longer! */
228 continue;
229 }
230 break;
231 }
232 fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey); 232 fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey);
233 priv = GNUNET_malloc(fs); 233 priv = GNUNET_malloc (fs);
234 sret = GNUNET_DISK_file_read(fd, priv, fs); 234 sret = GNUNET_DISK_file_read (fd, priv, fs);
235 GNUNET_assert((sret >= 0) && (fs == (size_t)sret)); 235 GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
236 if (GNUNET_YES != 236 if (GNUNET_YES !=
237 GNUNET_DISK_file_unlock(fd, 237 GNUNET_DISK_file_unlock (fd,
238 0, 238 0,
239 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))) 239 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
240 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 240 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
241 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd)); 241 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
242#if CRYPTO_BUG 242#if CRYPTO_BUG
243 if (GNUNET_OK != check_eddsa_key(priv)) 243 if (GNUNET_OK != check_eddsa_key (priv))
244 { 244 {
245 GNUNET_break(0); 245 GNUNET_break (0);
246 GNUNET_free(priv); 246 GNUNET_free (priv);
247 return NULL; 247 return NULL;
248 } 248 }
249#endif 249#endif
250 return priv; 250 return priv;
251} 251}
@@ -266,7 +266,7 @@ GNUNET_CRYPTO_eddsa_key_create_from_file(const char *filename)
266 * permission denied) 266 * permission denied)
267 */ 267 */
268struct GNUNET_CRYPTO_EcdsaPrivateKey * 268struct GNUNET_CRYPTO_EcdsaPrivateKey *
269GNUNET_CRYPTO_ecdsa_key_create_from_file(const char *filename) 269GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
270{ 270{
271 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv; 271 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
272 struct GNUNET_DISK_FileHandle *fd; 272 struct GNUNET_DISK_FileHandle *fd;
@@ -275,152 +275,152 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file(const char *filename)
275 uint64_t fs; 275 uint64_t fs;
276 ssize_t sret; 276 ssize_t sret;
277 277
278 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file(filename)) 278 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
279 return NULL; 279 return NULL;
280 while (GNUNET_YES != GNUNET_DISK_file_test(filename)) 280 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
281 {
282 fd =
283 GNUNET_DISK_file_open (filename,
284 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
285 | GNUNET_DISK_OPEN_FAILIFEXISTS,
286 GNUNET_DISK_PERM_USER_READ
287 | GNUNET_DISK_PERM_USER_WRITE);
288 if (NULL == fd)
281 { 289 {
282 fd = 290 if (EEXIST == errno)
283 GNUNET_DISK_file_open(filename, 291 {
284 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 292 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
285 GNUNET_DISK_OPEN_FAILIFEXISTS,
286 GNUNET_DISK_PERM_USER_READ |
287 GNUNET_DISK_PERM_USER_WRITE);
288 if (NULL == fd)
289 { 293 {
290 if (EEXIST == errno) 294 /* must exist but not be accessible, fail for good! */
291 { 295 if (0 != access (filename, R_OK))
292 if (GNUNET_YES != GNUNET_DISK_file_test(filename)) 296 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
293 { 297 else
294 /* must exist but not be accessible, fail for good! */ 298 GNUNET_break (0); /* what is going on!? */
295 if (0 != access(filename, R_OK))
296 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "access", filename);
297 else
298 GNUNET_break(0); /* what is going on!? */
299 return NULL;
300 }
301 continue;
302 }
303 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
304 return NULL; 299 return NULL;
305 } 300 }
306 cnt = 0; 301 continue;
307 while (GNUNET_YES != 302 }
308 GNUNET_DISK_file_lock(fd, 303 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
309 0, 304 return NULL;
310 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey), 305 }
311 GNUNET_YES)) 306 cnt = 0;
312 { 307 while (GNUNET_YES !=
313 short_wait(); 308 GNUNET_DISK_file_lock (fd,
314 if (0 == ++cnt % 10)
315 {
316 ec = errno;
317 LOG(GNUNET_ERROR_TYPE_ERROR,
318 _("Could not acquire lock on file `%s': %s...\n"),
319 filename,
320 strerror(ec));
321 }
322 }
323 LOG(GNUNET_ERROR_TYPE_INFO,
324 _("Creating a new private key. This may take a while.\n"));
325 priv = GNUNET_CRYPTO_ecdsa_key_create();
326 GNUNET_assert(NULL != priv);
327 GNUNET_assert(sizeof(*priv) ==
328 GNUNET_DISK_file_write(fd, priv, sizeof(*priv)));
329 GNUNET_DISK_file_sync(fd);
330 if (GNUNET_YES !=
331 GNUNET_DISK_file_unlock(fd,
332 0, 309 0,
333 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))) 310 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
334 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 311 GNUNET_YES))
335 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd)); 312 {
336 return priv; 313 short_wait ();
314 if (0 == ++cnt % 10)
315 {
316 ec = errno;
317 LOG (GNUNET_ERROR_TYPE_ERROR,
318 _ ("Could not acquire lock on file `%s': %s...\n"),
319 filename,
320 strerror (ec));
321 }
337 } 322 }
323 LOG (GNUNET_ERROR_TYPE_INFO,
324 _ ("Creating a new private key. This may take a while.\n"));
325 priv = GNUNET_CRYPTO_ecdsa_key_create ();
326 GNUNET_assert (NULL != priv);
327 GNUNET_assert (sizeof(*priv) ==
328 GNUNET_DISK_file_write (fd, priv, sizeof(*priv)));
329 GNUNET_DISK_file_sync (fd);
330 if (GNUNET_YES !=
331 GNUNET_DISK_file_unlock (fd,
332 0,
333 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
334 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
335 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
336 return priv;
337 }
338 /* key file exists already, read it! */ 338 /* key file exists already, read it! */
339 fd = GNUNET_DISK_file_open(filename, 339 fd = GNUNET_DISK_file_open (filename,
340 GNUNET_DISK_OPEN_READ, 340 GNUNET_DISK_OPEN_READ,
341 GNUNET_DISK_PERM_NONE); 341 GNUNET_DISK_PERM_NONE);
342 if (NULL == fd) 342 if (NULL == fd)
343 { 343 {
344 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename); 344 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
345 return NULL; 345 return NULL;
346 } 346 }
347 cnt = 0; 347 cnt = 0;
348 while (1) 348 while (1)
349 {
350 if (GNUNET_YES !=
351 GNUNET_DISK_file_lock (fd,
352 0,
353 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
354 GNUNET_NO))
349 { 355 {
356 if (0 == ++cnt % 60)
357 {
358 ec = errno;
359 LOG (GNUNET_ERROR_TYPE_ERROR,
360 _ ("Could not acquire lock on file `%s': %s...\n"),
361 filename,
362 strerror (ec));
363 LOG (
364 GNUNET_ERROR_TYPE_ERROR,
365 _ (
366 "This may be ok if someone is currently generating a private key.\n"));
367 }
368 short_wait ();
369 continue;
370 }
371 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
372 {
373 /* eh, what!? File we opened is now gone!? */
374 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
350 if (GNUNET_YES != 375 if (GNUNET_YES !=
351 GNUNET_DISK_file_lock(fd, 376 GNUNET_DISK_file_unlock (fd,
352 0, 377 0,
353 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey), 378 sizeof(
354 GNUNET_NO)) 379 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
355 { 380 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
356 if (0 == ++cnt % 60) 381 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
357 {
358 ec = errno;
359 LOG(GNUNET_ERROR_TYPE_ERROR,
360 _("Could not acquire lock on file `%s': %s...\n"),
361 filename,
362 strerror(ec));
363 LOG(
364 GNUNET_ERROR_TYPE_ERROR,
365 _(
366 "This may be ok if someone is currently generating a private key.\n"));
367 }
368 short_wait();
369 continue;
370 }
371 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
372 {
373 /* eh, what!? File we opened is now gone!? */
374 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
375 if (GNUNET_YES !=
376 GNUNET_DISK_file_unlock(fd,
377 0,
378 sizeof(
379 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
380 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
381 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
382 382
383 return NULL; 383 return NULL;
384 }
385 if (GNUNET_OK !=
386 GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
387 fs = 0;
388 if (fs < sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))
389 {
390 /* maybe we got the read lock before the key generating
391 * process had a chance to get the write lock; give it up! */
392 if (GNUNET_YES !=
393 GNUNET_DISK_file_unlock(fd,
394 0,
395 sizeof(
396 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
397 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
398 if (0 == ++cnt % 10)
399 {
400 LOG(GNUNET_ERROR_TYPE_ERROR,
401 _(
402 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
403 filename,
404 (unsigned int)fs,
405 (unsigned int)sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
406 LOG(GNUNET_ERROR_TYPE_ERROR,
407 _("This may be ok if someone is currently generating a key.\n"));
408 }
409 short_wait(); /* wait a bit longer! */
410 continue;
411 }
412 break;
413 } 384 }
385 if (GNUNET_OK !=
386 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
387 fs = 0;
388 if (fs < sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))
389 {
390 /* maybe we got the read lock before the key generating
391 * process had a chance to get the write lock; give it up! */
392 if (GNUNET_YES !=
393 GNUNET_DISK_file_unlock (fd,
394 0,
395 sizeof(
396 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
397 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
398 if (0 == ++cnt % 10)
399 {
400 LOG (GNUNET_ERROR_TYPE_ERROR,
401 _ (
402 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
403 filename,
404 (unsigned int) fs,
405 (unsigned int) sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
406 LOG (GNUNET_ERROR_TYPE_ERROR,
407 _ ("This may be ok if someone is currently generating a key.\n"));
408 }
409 short_wait (); /* wait a bit longer! */
410 continue;
411 }
412 break;
413 }
414 fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey); 414 fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey);
415 priv = GNUNET_malloc(fs); 415 priv = GNUNET_malloc (fs);
416 sret = GNUNET_DISK_file_read(fd, priv, fs); 416 sret = GNUNET_DISK_file_read (fd, priv, fs);
417 GNUNET_assert((sret >= 0) && (fs == (size_t)sret)); 417 GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
418 if (GNUNET_YES != 418 if (GNUNET_YES !=
419 GNUNET_DISK_file_unlock(fd, 419 GNUNET_DISK_file_unlock (fd,
420 0, 420 0,
421 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))) 421 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
422 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 422 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
423 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd)); 423 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
424 return priv; 424 return priv;
425} 425}
426 426
@@ -434,17 +434,17 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file(const char *filename)
434 * permission denied) 434 * permission denied)
435 */ 435 */
436struct GNUNET_CRYPTO_EddsaPrivateKey * 436struct GNUNET_CRYPTO_EddsaPrivateKey *
437GNUNET_CRYPTO_eddsa_key_create_from_configuration( 437GNUNET_CRYPTO_eddsa_key_create_from_configuration (
438 const struct GNUNET_CONFIGURATION_Handle *cfg) 438 const struct GNUNET_CONFIGURATION_Handle *cfg)
439{ 439{
440 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 440 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
441 char *fn; 441 char *fn;
442 442
443 if (GNUNET_OK != 443 if (GNUNET_OK !=
444 GNUNET_CONFIGURATION_get_value_filename(cfg, "PEER", "PRIVATE_KEY", &fn)) 444 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
445 return NULL; 445 return NULL;
446 priv = GNUNET_CRYPTO_eddsa_key_create_from_file(fn); 446 priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn);
447 GNUNET_free(fn); 447 GNUNET_free (fn);
448 return priv; 448 return priv;
449} 449}
450 450
@@ -458,19 +458,19 @@ GNUNET_CRYPTO_eddsa_key_create_from_configuration(
458 * could not be retrieved 458 * could not be retrieved
459 */ 459 */
460int 460int
461GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, 461GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
462 struct GNUNET_PeerIdentity *dst) 462 struct GNUNET_PeerIdentity *dst)
463{ 463{
464 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 464 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
465 465
466 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg))) 466 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
467 { 467 {
468 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
469 _("Could not load peer's private key\n")); 469 _ ("Could not load peer's private key\n"));
470 return GNUNET_SYSERR; 470 return GNUNET_SYSERR;
471 } 471 }
472 GNUNET_CRYPTO_eddsa_key_get_public(priv, &dst->public_key); 472 GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key);
473 GNUNET_free(priv); 473 GNUNET_free (priv);
474 return GNUNET_OK; 474 return GNUNET_OK;
475} 475}
476 476
@@ -484,17 +484,17 @@ GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg,
484 * @param cfg_name name of the configuration file to use 484 * @param cfg_name name of the configuration file to use
485 */ 485 */
486void 486void
487GNUNET_CRYPTO_eddsa_setup_key(const char *cfg_name) 487GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
488{ 488{
489 struct GNUNET_CONFIGURATION_Handle *cfg; 489 struct GNUNET_CONFIGURATION_Handle *cfg;
490 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 490 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
491 491
492 cfg = GNUNET_CONFIGURATION_create(); 492 cfg = GNUNET_CONFIGURATION_create ();
493 (void)GNUNET_CONFIGURATION_load(cfg, cfg_name); 493 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
494 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg); 494 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
495 if (NULL != priv) 495 if (NULL != priv)
496 GNUNET_free(priv); 496 GNUNET_free (priv);
497 GNUNET_CONFIGURATION_destroy(cfg); 497 GNUNET_CONFIGURATION_destroy (cfg);
498} 498}
499 499
500/* end of crypto_ecc_setup.c */ 500/* end of crypto_ecc_setup.c */
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index ebb1266b2..2bf03db58 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -29,9 +29,13 @@
29#include "benchmark.h" 29#include "benchmark.h"
30#include <gcrypt.h> 30#include <gcrypt.h>
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-hash", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)
33 33
34#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-crypto-hash", syscall, filename) 34#define LOG_STRERROR_FILE(kind, syscall, \
35 filename) GNUNET_log_from_strerror_file (kind, \
36 "util-crypto-hash", \
37 syscall, \
38 filename)
35 39
36/** 40/**
37 * Hash block of given size. 41 * Hash block of given size.
@@ -41,13 +45,13 @@
41 * @param ret pointer to where to write the hashcode 45 * @param ret pointer to where to write the hashcode
42 */ 46 */
43void 47void
44GNUNET_CRYPTO_hash(const void *block, 48GNUNET_CRYPTO_hash (const void *block,
45 size_t size, 49 size_t size,
46 struct GNUNET_HashCode *ret) 50 struct GNUNET_HashCode *ret)
47{ 51{
48 BENCHMARK_START(hash); 52 BENCHMARK_START (hash);
49 gcry_md_hash_buffer(GCRY_MD_SHA512, ret, block, size); 53 gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
50 BENCHMARK_END(hash); 54 BENCHMARK_END (hash);
51} 55}
52 56
53 57
@@ -66,16 +70,18 @@ GNUNET_CRYPTO_hash(const void *block,
66 * safely cast to char*, a '\\0' termination is set). 70 * safely cast to char*, a '\\0' termination is set).
67 */ 71 */
68void 72void
69GNUNET_CRYPTO_hash_to_enc(const struct GNUNET_HashCode *block, 73GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
70 struct GNUNET_CRYPTO_HashAsciiEncoded *result) 74 struct GNUNET_CRYPTO_HashAsciiEncoded *result)
71{ 75{
72 char *np; 76 char *np;
73 77
74 np = GNUNET_STRINGS_data_to_string((const unsigned char *)block, 78 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
75 sizeof(struct GNUNET_HashCode), 79 sizeof(struct GNUNET_HashCode),
76 (char *)result, 80 (char *) result,
77 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) - 1); 81 sizeof(struct
78 GNUNET_assert(NULL != np); 82 GNUNET_CRYPTO_HashAsciiEncoded)
83 - 1);
84 GNUNET_assert (NULL != np);
79 *np = '\0'; 85 *np = '\0';
80} 86}
81 87
@@ -89,18 +95,18 @@ GNUNET_CRYPTO_hash_to_enc(const struct GNUNET_HashCode *block,
89 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding 95 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
90 */ 96 */
91int 97int
92GNUNET_CRYPTO_hash_from_string2(const char *enc, 98GNUNET_CRYPTO_hash_from_string2 (const char *enc,
93 size_t enclen, 99 size_t enclen,
94 struct GNUNET_HashCode *result) 100 struct GNUNET_HashCode *result)
95{ 101{
96 char upper_enc[enclen]; 102 char upper_enc[enclen];
97 char *up_ptr = upper_enc; 103 char *up_ptr = upper_enc;
98 104
99 GNUNET_STRINGS_utf8_toupper(enc, up_ptr); 105 GNUNET_STRINGS_utf8_toupper (enc, up_ptr);
100 106
101 return GNUNET_STRINGS_string_to_data(upper_enc, enclen, 107 return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
102 (unsigned char*)result, 108 (unsigned char*) result,
103 sizeof(struct GNUNET_HashCode)); 109 sizeof(struct GNUNET_HashCode));
104} 110}
105 111
106 112
@@ -118,8 +124,8 @@ GNUNET_CRYPTO_hash_from_string2(const char *enc,
118 * hashcode proximity. 124 * hashcode proximity.
119 */ 125 */
120unsigned int 126unsigned int
121GNUNET_CRYPTO_hash_distance_u32(const struct GNUNET_HashCode *a, 127GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
122 const struct GNUNET_HashCode *b) 128 const struct GNUNET_HashCode *b)
123{ 129{
124 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16; 130 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
125 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16; 131 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
@@ -135,13 +141,13 @@ GNUNET_CRYPTO_hash_distance_u32(const struct GNUNET_HashCode *a,
135 * @param result hash code that is randomized 141 * @param result hash code that is randomized
136 */ 142 */
137void 143void
138GNUNET_CRYPTO_hash_create_random(enum GNUNET_CRYPTO_Quality mode, 144GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
139 struct GNUNET_HashCode *result) 145 struct GNUNET_HashCode *result)
140{ 146{
141 int i; 147 int i;
142 148
143 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t)) - 1; i >= 0; i--) 149 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t)) - 1; i >= 0; i--)
144 result->bits[i] = GNUNET_CRYPTO_random_u32(mode, UINT32_MAX); 150 result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
145} 151}
146 152
147 153
@@ -153,13 +159,14 @@ GNUNET_CRYPTO_hash_create_random(enum GNUNET_CRYPTO_Quality mode,
153 * @param result set to b - a 159 * @param result set to b - a
154 */ 160 */
155void 161void
156GNUNET_CRYPTO_hash_difference(const struct GNUNET_HashCode *a, 162GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
157 const struct GNUNET_HashCode *b, 163 const struct GNUNET_HashCode *b,
158 struct GNUNET_HashCode *result) 164 struct GNUNET_HashCode *result)
159{ 165{
160 int i; 166 int i;
161 167
162 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0; i--) 168 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0;
169 i--)
163 result->bits[i] = b->bits[i] - a->bits[i]; 170 result->bits[i] = b->bits[i] - a->bits[i];
164} 171}
165 172
@@ -172,12 +179,14 @@ GNUNET_CRYPTO_hash_difference(const struct GNUNET_HashCode *a,
172 * @param result set to a + delta 179 * @param result set to a + delta
173 */ 180 */
174void 181void
175GNUNET_CRYPTO_hash_sum(const struct GNUNET_HashCode * a, 182GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
176 const struct GNUNET_HashCode * delta, struct GNUNET_HashCode * result) 183 const struct GNUNET_HashCode *delta, struct
184 GNUNET_HashCode *result)
177{ 185{
178 int i; 186 int i;
179 187
180 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0; i--) 188 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0;
189 i--)
181 result->bits[i] = delta->bits[i] + a->bits[i]; 190 result->bits[i] = delta->bits[i] + a->bits[i];
182} 191}
183 192
@@ -190,13 +199,14 @@ GNUNET_CRYPTO_hash_sum(const struct GNUNET_HashCode * a,
190 * @param result set to a ^ b 199 * @param result set to a ^ b
191 */ 200 */
192void 201void
193GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, 202GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
194 const struct GNUNET_HashCode *b, 203 const struct GNUNET_HashCode *b,
195 struct GNUNET_HashCode *result) 204 struct GNUNET_HashCode *result)
196{ 205{
197 int i; 206 int i;
198 207
199 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0; i--) 208 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0;
209 i--)
200 result->bits[i] = a->bits[i] ^ b->bits[i]; 210 result->bits[i] = a->bits[i] ^ b->bits[i];
201} 211}
202 212
@@ -209,20 +219,25 @@ GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a,
209 * @param iv set to a valid initialization vector 219 * @param iv set to a valid initialization vector
210 */ 220 */
211void 221void
212GNUNET_CRYPTO_hash_to_aes_key(const struct GNUNET_HashCode *hc, 222GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc,
213 struct GNUNET_CRYPTO_SymmetricSessionKey *skey, 223 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
214 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv) 224 struct
225 GNUNET_CRYPTO_SymmetricInitializationVector *iv)
215{ 226{
216 GNUNET_assert(GNUNET_YES == 227 GNUNET_assert (GNUNET_YES ==
217 GNUNET_CRYPTO_kdf(skey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey), 228 GNUNET_CRYPTO_kdf (skey, sizeof(struct
218 "Hash key derivation", strlen("Hash key derivation"), 229 GNUNET_CRYPTO_SymmetricSessionKey),
219 hc, sizeof(struct GNUNET_HashCode), 230 "Hash key derivation", strlen (
220 NULL, 0)); 231 "Hash key derivation"),
221 GNUNET_assert(GNUNET_YES == 232 hc, sizeof(struct GNUNET_HashCode),
222 GNUNET_CRYPTO_kdf(iv, sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector), 233 NULL, 0));
223 "Initialization vector derivation", strlen("Initialization vector derivation"), 234 GNUNET_assert (GNUNET_YES ==
224 hc, sizeof(struct GNUNET_HashCode), 235 GNUNET_CRYPTO_kdf (iv, sizeof(struct
225 NULL, 0)); 236 GNUNET_CRYPTO_SymmetricInitializationVector),
237 "Initialization vector derivation", strlen (
238 "Initialization vector derivation"),
239 hc, sizeof(struct GNUNET_HashCode),
240 NULL, 0));
226} 241}
227 242
228 243
@@ -233,10 +248,11 @@ GNUNET_CRYPTO_hash_to_aes_key(const struct GNUNET_HashCode *hc,
233 * @return Bit \a bit from hashcode \a code, -1 for invalid index 248 * @return Bit \a bit from hashcode \a code, -1 for invalid index
234 */ 249 */
235int 250int
236GNUNET_CRYPTO_hash_get_bit(const struct GNUNET_HashCode * code, unsigned int bit) 251GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code, unsigned int
252 bit)
237{ 253{
238 GNUNET_assert(bit < 8 * sizeof(struct GNUNET_HashCode)); 254 GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
239 return (((unsigned char *)code)[bit >> 3] & (1 << (bit & 7))) > 0; 255 return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
240} 256}
241 257
242 258
@@ -253,14 +269,14 @@ GNUNET_CRYPTO_hash_get_bit(const struct GNUNET_HashCode * code, unsigned int bit
253 * @return the number of bits that match 269 * @return the number of bits that match
254 */ 270 */
255unsigned int 271unsigned int
256GNUNET_CRYPTO_hash_matching_bits(const struct GNUNET_HashCode * first, 272GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
257 const struct GNUNET_HashCode * second) 273 const struct GNUNET_HashCode *second)
258{ 274{
259 unsigned int i; 275 unsigned int i;
260 276
261 for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++) 277 for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++)
262 if (GNUNET_CRYPTO_hash_get_bit(first, i) != 278 if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
263 GNUNET_CRYPTO_hash_get_bit(second, i)) 279 GNUNET_CRYPTO_hash_get_bit (second, i))
264 return i; 280 return i;
265 return sizeof(struct GNUNET_HashCode) * 8; 281 return sizeof(struct GNUNET_HashCode) * 8;
266} 282}
@@ -275,22 +291,23 @@ GNUNET_CRYPTO_hash_matching_bits(const struct GNUNET_HashCode * first,
275 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. 291 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
276 */ 292 */
277int 293int
278GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, 294GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
279 const struct GNUNET_HashCode *h2) 295 const struct GNUNET_HashCode *h2)
280{ 296{
281 unsigned int *i1; 297 unsigned int *i1;
282 unsigned int *i2; 298 unsigned int *i2;
283 int i; 299 int i;
284 300
285 i1 = (unsigned int *)h1; 301 i1 = (unsigned int *) h1;
286 i2 = (unsigned int *)h2; 302 i2 = (unsigned int *) h2;
287 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0; i--) 303 for (i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; i >= 0;
288 { 304 i--)
289 if (i1[i] > i2[i]) 305 {
290 return 1; 306 if (i1[i] > i2[i])
291 if (i1[i] < i2[i]) 307 return 1;
292 return -1; 308 if (i1[i] < i2[i])
293 } 309 return -1;
310 }
294 return 0; 311 return 0;
295} 312}
296 313
@@ -305,23 +322,24 @@ GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1,
305 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2. 322 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
306 */ 323 */
307int 324int
308GNUNET_CRYPTO_hash_xorcmp(const struct GNUNET_HashCode *h1, 325GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
309 const struct GNUNET_HashCode *h2, 326 const struct GNUNET_HashCode *h2,
310 const struct GNUNET_HashCode *target) 327 const struct GNUNET_HashCode *target)
311{ 328{
312 int i; 329 int i;
313 unsigned int d1; 330 unsigned int d1;
314 unsigned int d2; 331 unsigned int d2;
315 332
316 for (i = sizeof(struct GNUNET_HashCode) / sizeof(unsigned int) - 1; i >= 0; i--) 333 for (i = sizeof(struct GNUNET_HashCode) / sizeof(unsigned int) - 1; i >= 0;
317 { 334 i--)
318 d1 = ((unsigned int *)h1)[i] ^ ((unsigned int *)target)[i]; 335 {
319 d2 = ((unsigned int *)h2)[i] ^ ((unsigned int *)target)[i]; 336 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
320 if (d1 > d2) 337 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
321 return 1; 338 if (d1 > d2)
322 else if (d1 < d2) 339 return 1;
323 return -1; 340 else if (d1 < d2)
324 } 341 return -1;
342 }
325 return 0; 343 return 0;
326} 344}
327 345
@@ -335,15 +353,16 @@ GNUNET_CRYPTO_hash_xorcmp(const struct GNUNET_HashCode *h1,
335 * @param ... pair of void * & size_t for context chunks, terminated by NULL 353 * @param ... pair of void * & size_t for context chunks, terminated by NULL
336 */ 354 */
337void 355void
338GNUNET_CRYPTO_hmac_derive_key(struct GNUNET_CRYPTO_AuthKey *key, 356GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
339 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, 357 const struct
340 const void *salt, size_t salt_len, ...) 358 GNUNET_CRYPTO_SymmetricSessionKey *rkey,
359 const void *salt, size_t salt_len, ...)
341{ 360{
342 va_list argp; 361 va_list argp;
343 362
344 va_start(argp, salt_len); 363 va_start (argp, salt_len);
345 GNUNET_CRYPTO_hmac_derive_key_v(key, rkey, salt, salt_len, argp); 364 GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
346 va_end(argp); 365 va_end (argp);
347} 366}
348 367
349 368
@@ -356,15 +375,16 @@ GNUNET_CRYPTO_hmac_derive_key(struct GNUNET_CRYPTO_AuthKey *key,
356 * @param argp pair of void * & size_t for context chunks, terminated by NULL 375 * @param argp pair of void * & size_t for context chunks, terminated by NULL
357 */ 376 */
358void 377void
359GNUNET_CRYPTO_hmac_derive_key_v(struct GNUNET_CRYPTO_AuthKey *key, 378GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
360 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, 379 const struct
361 const void *salt, size_t salt_len, 380 GNUNET_CRYPTO_SymmetricSessionKey *rkey,
362 va_list argp) 381 const void *salt, size_t salt_len,
382 va_list argp)
363{ 383{
364 GNUNET_CRYPTO_kdf_v(key->key, sizeof(key->key), 384 GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key),
365 salt, salt_len, 385 salt, salt_len,
366 rkey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey), 386 rkey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
367 argp); 387 argp);
368} 388}
369 389
370 390
@@ -380,29 +400,29 @@ GNUNET_CRYPTO_hmac_derive_key_v(struct GNUNET_CRYPTO_AuthKey *key,
380 * @param hmac where to store the hmac 400 * @param hmac where to store the hmac
381 */ 401 */
382void 402void
383GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, 403GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
384 const void *plaintext, size_t plaintext_len, 404 const void *plaintext, size_t plaintext_len,
385 struct GNUNET_HashCode *hmac) 405 struct GNUNET_HashCode *hmac)
386{ 406{
387 static int once; 407 static int once;
388 static gcry_md_hd_t md; 408 static gcry_md_hd_t md;
389 const unsigned char *mc; 409 const unsigned char *mc;
390 410
391 if (!once) 411 if (! once)
392 { 412 {
393 once = 1; 413 once = 1;
394 GNUNET_assert(GPG_ERR_NO_ERROR == 414 GNUNET_assert (GPG_ERR_NO_ERROR ==
395 gcry_md_open(&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC)); 415 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
396 } 416 }
397 else 417 else
398 { 418 {
399 gcry_md_reset(md); 419 gcry_md_reset (md);
400 } 420 }
401 gcry_md_setkey(md, key, key_len); 421 gcry_md_setkey (md, key, key_len);
402 gcry_md_write(md, plaintext, plaintext_len); 422 gcry_md_write (md, plaintext, plaintext_len);
403 mc = gcry_md_read(md, GCRY_MD_SHA512); 423 mc = gcry_md_read (md, GCRY_MD_SHA512);
404 GNUNET_assert(NULL != mc); 424 GNUNET_assert (NULL != mc);
405 GNUNET_memcpy(hmac->bits, mc, sizeof(hmac->bits)); 425 GNUNET_memcpy (hmac->bits, mc, sizeof(hmac->bits));
406} 426}
407 427
408 428
@@ -415,20 +435,21 @@ GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len,
415 * @param hmac where to store the hmac 435 * @param hmac where to store the hmac
416 */ 436 */
417void 437void
418GNUNET_CRYPTO_hmac(const struct GNUNET_CRYPTO_AuthKey *key, 438GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
419 const void *plaintext, size_t plaintext_len, 439 const void *plaintext, size_t plaintext_len,
420 struct GNUNET_HashCode *hmac) 440 struct GNUNET_HashCode *hmac)
421{ 441{
422 GNUNET_CRYPTO_hmac_raw((void*)key->key, sizeof(key->key), 442 GNUNET_CRYPTO_hmac_raw ((void*) key->key, sizeof(key->key),
423 plaintext, plaintext_len, 443 plaintext, plaintext_len,
424 hmac); 444 hmac);
425} 445}
426 446
427 447
428/** 448/**
429 * Context for cummulative hashing. 449 * Context for cummulative hashing.
430 */ 450 */
431struct GNUNET_HashContext { 451struct GNUNET_HashContext
452{
432 /** 453 /**
433 * Internal state of the hash function. 454 * Internal state of the hash function.
434 */ 455 */
@@ -442,19 +463,19 @@ struct GNUNET_HashContext {
442 * @return context for incremental hash computation 463 * @return context for incremental hash computation
443 */ 464 */
444struct GNUNET_HashContext * 465struct GNUNET_HashContext *
445GNUNET_CRYPTO_hash_context_start() 466GNUNET_CRYPTO_hash_context_start ()
446{ 467{
447 struct GNUNET_HashContext *hc; 468 struct GNUNET_HashContext *hc;
448 469
449 BENCHMARK_START(hash_context_start); 470 BENCHMARK_START (hash_context_start);
450 471
451 hc = GNUNET_new(struct GNUNET_HashContext); 472 hc = GNUNET_new (struct GNUNET_HashContext);
452 GNUNET_assert(0 == 473 GNUNET_assert (0 ==
453 gcry_md_open(&hc->hd, 474 gcry_md_open (&hc->hd,
454 GCRY_MD_SHA512, 475 GCRY_MD_SHA512,
455 0)); 476 0));
456 477
457 BENCHMARK_END(hash_context_start); 478 BENCHMARK_END (hash_context_start);
458 479
459 return hc; 480 return hc;
460} 481}
@@ -468,13 +489,13 @@ GNUNET_CRYPTO_hash_context_start()
468 * @param size number of bytes in @a buf 489 * @param size number of bytes in @a buf
469 */ 490 */
470void 491void
471GNUNET_CRYPTO_hash_context_read(struct GNUNET_HashContext *hc, 492GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
472 const void *buf, 493 const void *buf,
473 size_t size) 494 size_t size)
474{ 495{
475 BENCHMARK_START(hash_context_read); 496 BENCHMARK_START (hash_context_read);
476 gcry_md_write(hc->hd, buf, size); 497 gcry_md_write (hc->hd, buf, size);
477 BENCHMARK_END(hash_context_read); 498 BENCHMARK_END (hash_context_read);
478} 499}
479 500
480 501
@@ -485,20 +506,20 @@ GNUNET_CRYPTO_hash_context_read(struct GNUNET_HashContext *hc,
485 * @param r_hash where to write the latest / final hash code 506 * @param r_hash where to write the latest / final hash code
486 */ 507 */
487void 508void
488GNUNET_CRYPTO_hash_context_finish(struct GNUNET_HashContext *hc, 509GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
489 struct GNUNET_HashCode *r_hash) 510 struct GNUNET_HashCode *r_hash)
490{ 511{
491 const void *res = gcry_md_read(hc->hd, 0); 512 const void *res = gcry_md_read (hc->hd, 0);
492 513
493 BENCHMARK_START(hash_context_finish); 514 BENCHMARK_START (hash_context_finish);
494 515
495 GNUNET_assert(NULL != res); 516 GNUNET_assert (NULL != res);
496 if (NULL != r_hash) 517 if (NULL != r_hash)
497 GNUNET_memcpy(r_hash, 518 GNUNET_memcpy (r_hash,
498 res, 519 res,
499 sizeof(struct GNUNET_HashCode)); 520 sizeof(struct GNUNET_HashCode));
500 GNUNET_CRYPTO_hash_context_abort(hc); 521 GNUNET_CRYPTO_hash_context_abort (hc);
501 BENCHMARK_END(hash_context_finish); 522 BENCHMARK_END (hash_context_finish);
502} 523}
503 524
504 525
@@ -508,10 +529,10 @@ GNUNET_CRYPTO_hash_context_finish(struct GNUNET_HashContext *hc,
508 * @param hc hash context to destroy 529 * @param hc hash context to destroy
509 */ 530 */
510void 531void
511GNUNET_CRYPTO_hash_context_abort(struct GNUNET_HashContext *hc) 532GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc)
512{ 533{
513 gcry_md_close(hc->hd); 534 gcry_md_close (hc->hd);
514 GNUNET_free(hc); 535 GNUNET_free (hc);
515} 536}
516 537
517 538
diff --git a/src/util/crypto_hash_file.c b/src/util/crypto_hash_file.c
index 78d0d0a0d..0dff6326c 100644
--- a/src/util/crypto_hash_file.c
+++ b/src/util/crypto_hash_file.c
@@ -27,15 +27,21 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include <gcrypt.h> 28#include <gcrypt.h>
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-hash-file", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hash-file", \
31 __VA_ARGS__)
31 32
32#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-crypto-hash-file", syscall, filename) 33#define LOG_STRERROR_FILE(kind, syscall, \
34 filename) GNUNET_log_from_strerror_file (kind, \
35 "util-crypto-hash-file", \
36 syscall, \
37 filename)
33 38
34 39
35/** 40/**
36 * Context used when hashing a file. 41 * Context used when hashing a file.
37 */ 42 */
38struct GNUNET_CRYPTO_FileHashContext { 43struct GNUNET_CRYPTO_FileHashContext
44{
39 /** 45 /**
40 * Function to call upon completion. 46 * Function to call upon completion.
41 */ 47 */
@@ -79,7 +85,7 @@ struct GNUNET_CRYPTO_FileHashContext {
79 /** 85 /**
80 * Current task for hashing. 86 * Current task for hashing.
81 */ 87 */
82 struct GNUNET_SCHEDULER_Task * task; 88 struct GNUNET_SCHEDULER_Task *task;
83 89
84 /** 90 /**
85 * Priority we use. 91 * Priority we use.
@@ -98,15 +104,15 @@ struct GNUNET_CRYPTO_FileHashContext {
98 * and free associated resources. 104 * and free associated resources.
99 */ 105 */
100static void 106static void
101file_hash_finish(struct GNUNET_CRYPTO_FileHashContext *fhc, 107file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
102 const struct GNUNET_HashCode * res) 108 const struct GNUNET_HashCode *res)
103{ 109{
104 fhc->callback(fhc->callback_cls, res); 110 fhc->callback (fhc->callback_cls, res);
105 GNUNET_free(fhc->filename); 111 GNUNET_free (fhc->filename);
106 if (!GNUNET_DISK_handle_invalid(fhc->fh)) 112 if (! GNUNET_DISK_handle_invalid (fhc->fh))
107 GNUNET_break(GNUNET_OK == GNUNET_DISK_file_close(fhc->fh)); 113 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
108 gcry_md_close(fhc->md); 114 gcry_md_close (fhc->md);
109 GNUNET_free(fhc); /* also frees fhc->buffer */ 115 GNUNET_free (fhc); /* also frees fhc->buffer */
110} 116}
111 117
112 118
@@ -116,7 +122,7 @@ file_hash_finish(struct GNUNET_CRYPTO_FileHashContext *fhc,
116 * @param cls closure 122 * @param cls closure
117 */ 123 */
118static void 124static void
119file_hash_task(void *cls) 125file_hash_task (void *cls)
120{ 126{
121 struct GNUNET_CRYPTO_FileHashContext *fhc = cls; 127 struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
122 struct GNUNET_HashCode *res; 128 struct GNUNET_HashCode *res;
@@ -124,37 +130,37 @@ file_hash_task(void *cls)
124 ssize_t sret; 130 ssize_t sret;
125 131
126 fhc->task = NULL; 132 fhc->task = NULL;
127 GNUNET_assert(fhc->offset <= fhc->fsize); 133 GNUNET_assert (fhc->offset <= fhc->fsize);
128 delta = fhc->bsize; 134 delta = fhc->bsize;
129 if (fhc->fsize - fhc->offset < delta) 135 if (fhc->fsize - fhc->offset < delta)
130 delta = fhc->fsize - fhc->offset; 136 delta = fhc->fsize - fhc->offset;
131 sret = GNUNET_DISK_file_read(fhc->fh, 137 sret = GNUNET_DISK_file_read (fhc->fh,
132 fhc->buffer, 138 fhc->buffer,
133 delta); 139 delta);
134 if ((sret < 0) || 140 if ((sret < 0) ||
135 (delta != (size_t)sret)) 141 (delta != (size_t) sret))
136 { 142 {
137 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, 143 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
138 "read", 144 "read",
139 fhc->filename); 145 fhc->filename);
140 file_hash_finish(fhc, 146 file_hash_finish (fhc,
141 NULL); 147 NULL);
142 return; 148 return;
143 } 149 }
144 gcry_md_write(fhc->md, 150 gcry_md_write (fhc->md,
145 fhc->buffer, 151 fhc->buffer,
146 delta); 152 delta);
147 fhc->offset += delta; 153 fhc->offset += delta;
148 if (fhc->offset == fhc->fsize) 154 if (fhc->offset == fhc->fsize)
149 { 155 {
150 res = (struct GNUNET_HashCode *)gcry_md_read(fhc->md, 156 res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md,
151 GCRY_MD_SHA512); 157 GCRY_MD_SHA512);
152 file_hash_finish(fhc, res); 158 file_hash_finish (fhc, res);
153 return; 159 return;
154 } 160 }
155 fhc->task = GNUNET_SCHEDULER_add_with_priority(fhc->priority, 161 fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
156 &file_hash_task, 162 &file_hash_task,
157 fhc); 163 fhc);
158} 164}
159 165
160 166
@@ -169,51 +175,51 @@ file_hash_task(void *cls)
169 * @return NULL on (immediate) errror 175 * @return NULL on (immediate) errror
170 */ 176 */
171struct GNUNET_CRYPTO_FileHashContext * 177struct GNUNET_CRYPTO_FileHashContext *
172GNUNET_CRYPTO_hash_file(enum GNUNET_SCHEDULER_Priority priority, 178GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
173 const char *filename, 179 const char *filename,
174 size_t blocksize, 180 size_t blocksize,
175 GNUNET_CRYPTO_HashCompletedCallback callback, 181 GNUNET_CRYPTO_HashCompletedCallback callback,
176 void *callback_cls) 182 void *callback_cls)
177{ 183{
178 struct GNUNET_CRYPTO_FileHashContext *fhc; 184 struct GNUNET_CRYPTO_FileHashContext *fhc;
179 185
180 GNUNET_assert(blocksize > 0); 186 GNUNET_assert (blocksize > 0);
181 fhc = 187 fhc =
182 GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize); 188 GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize);
183 fhc->callback = callback; 189 fhc->callback = callback;
184 fhc->callback_cls = callback_cls; 190 fhc->callback_cls = callback_cls;
185 fhc->buffer = (unsigned char *)&fhc[1]; 191 fhc->buffer = (unsigned char *) &fhc[1];
186 fhc->filename = GNUNET_strdup(filename); 192 fhc->filename = GNUNET_strdup (filename);
187 if (GPG_ERR_NO_ERROR != gcry_md_open(&fhc->md, GCRY_MD_SHA512, 0)) 193 if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
188 { 194 {
189 GNUNET_break(0); 195 GNUNET_break (0);
190 GNUNET_free(fhc); 196 GNUNET_free (fhc);
191 return NULL; 197 return NULL;
192 } 198 }
193 fhc->bsize = blocksize; 199 fhc->bsize = blocksize;
194 if (GNUNET_OK != 200 if (GNUNET_OK !=
195 GNUNET_DISK_file_size(filename, 201 GNUNET_DISK_file_size (filename,
196 &fhc->fsize, 202 &fhc->fsize,
197 GNUNET_NO, 203 GNUNET_NO,
198 GNUNET_YES)) 204 GNUNET_YES))
199 { 205 {
200 GNUNET_free(fhc->filename); 206 GNUNET_free (fhc->filename);
201 GNUNET_free(fhc); 207 GNUNET_free (fhc);
202 return NULL; 208 return NULL;
203 } 209 }
204 fhc->fh = GNUNET_DISK_file_open(filename, 210 fhc->fh = GNUNET_DISK_file_open (filename,
205 GNUNET_DISK_OPEN_READ, 211 GNUNET_DISK_OPEN_READ,
206 GNUNET_DISK_PERM_NONE); 212 GNUNET_DISK_PERM_NONE);
207 if (!fhc->fh) 213 if (! fhc->fh)
208 { 214 {
209 GNUNET_free(fhc->filename); 215 GNUNET_free (fhc->filename);
210 GNUNET_free(fhc); 216 GNUNET_free (fhc);
211 return NULL; 217 return NULL;
212 } 218 }
213 fhc->priority = priority; 219 fhc->priority = priority;
214 fhc->task = GNUNET_SCHEDULER_add_with_priority(priority, 220 fhc->task = GNUNET_SCHEDULER_add_with_priority (priority,
215 &file_hash_task, 221 &file_hash_task,
216 fhc); 222 fhc);
217 return fhc; 223 return fhc;
218} 224}
219 225
@@ -224,13 +230,13 @@ GNUNET_CRYPTO_hash_file(enum GNUNET_SCHEDULER_Priority priority,
224 * @param fhc operation to cancel (callback must not yet have been invoked) 230 * @param fhc operation to cancel (callback must not yet have been invoked)
225 */ 231 */
226void 232void
227GNUNET_CRYPTO_hash_file_cancel(struct GNUNET_CRYPTO_FileHashContext *fhc) 233GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
228{ 234{
229 GNUNET_SCHEDULER_cancel(fhc->task); 235 GNUNET_SCHEDULER_cancel (fhc->task);
230 GNUNET_free(fhc->filename); 236 GNUNET_free (fhc->filename);
231 GNUNET_break(GNUNET_OK == 237 GNUNET_break (GNUNET_OK ==
232 GNUNET_DISK_file_close(fhc->fh)); 238 GNUNET_DISK_file_close (fhc->fh));
233 GNUNET_free(fhc); 239 GNUNET_free (fhc);
234} 240}
235 241
236/* end of crypto_hash_file.c */ 242/* end of crypto_hash_file.c */
diff --git a/src/util/crypto_hkdf.c b/src/util/crypto_hkdf.c
index 58ae6c8ad..fea4f9861 100644
--- a/src/util/crypto_hkdf.c
+++ b/src/util/crypto_hkdf.c
@@ -36,7 +36,7 @@
36 * - Matthias Wachs (08.10.2010) 36 * - Matthias Wachs (08.10.2010)
37 */ 37 */
38 38
39#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-hkdf", __VA_ARGS__) 39#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hkdf", __VA_ARGS__)
40 40
41/** 41/**
42 * Set this to 0 if you compile this code outside of GNUnet. 42 * Set this to 0 if you compile this code outside of GNUnet.
@@ -75,13 +75,13 @@
75 * @return HMAC, freed by caller via gcry_md_close/_reset 75 * @return HMAC, freed by caller via gcry_md_close/_reset
76 */ 76 */
77static const void * 77static const void *
78doHMAC(gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf, 78doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
79 size_t buf_len) 79 size_t buf_len)
80{ 80{
81 gcry_md_setkey(mac, key, key_len); 81 gcry_md_setkey (mac, key, key_len);
82 gcry_md_write(mac, buf, buf_len); 82 gcry_md_write (mac, buf, buf_len);
83 83
84 return (const void *)gcry_md_read(mac, 0); 84 return (const void *) gcry_md_read (mac, 0);
85} 85}
86 86
87/** 87/**
@@ -95,15 +95,15 @@ doHMAC(gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
95 * @return #GNUNET_YES on success 95 * @return #GNUNET_YES on success
96 */ 96 */
97static int 97static int
98getPRK(gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm, 98getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
99 size_t skm_len, void *prk) 99 size_t skm_len, void *prk)
100{ 100{
101 const void *ret; 101 const void *ret;
102 102
103 ret = doHMAC(mac, xts, xts_len, skm, skm_len); 103 ret = doHMAC (mac, xts, xts_len, skm, skm_len);
104 if (ret == NULL) 104 if (ret == NULL)
105 return GNUNET_SYSERR; 105 return GNUNET_SYSERR;
106 GNUNET_memcpy(prk, ret, gcry_md_get_algo_dlen(gcry_md_get_algo(mac))); 106 GNUNET_memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
107 107
108 return GNUNET_YES; 108 return GNUNET_YES;
109} 109}
@@ -111,16 +111,16 @@ getPRK(gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
111 111
112#if DEBUG_HKDF 112#if DEBUG_HKDF
113static void 113static void
114dump(const char *src, const void *p, unsigned int l) 114dump (const char *src, const void *p, unsigned int l)
115{ 115{
116 unsigned int i; 116 unsigned int i;
117 117
118 printf("\n%s: ", src); 118 printf ("\n%s: ", src);
119 for (i = 0; i < l; i++) 119 for (i = 0; i < l; i++)
120 { 120 {
121 printf("%2x", (int)((const unsigned char *)p)[i]); 121 printf ("%2x", (int) ((const unsigned char *) p)[i]);
122 } 122 }
123 printf("\n"); 123 printf ("\n");
124} 124}
125#endif 125#endif
126 126
@@ -139,9 +139,9 @@ dump(const char *src, const void *p, unsigned int l)
139 * @return #GNUNET_YES on success 139 * @return #GNUNET_YES on success
140 */ 140 */
141int 141int
142GNUNET_CRYPTO_hkdf_v(void *result, size_t out_len, int xtr_algo, int prf_algo, 142GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
143 const void *xts, size_t xts_len, const void *skm, 143 const void *xts, size_t xts_len, const void *skm,
144 size_t skm_len, va_list argp) 144 size_t skm_len, va_list argp)
145{ 145{
146 gcry_md_hd_t xtr; 146 gcry_md_hd_t xtr;
147 gcry_md_hd_t prf; 147 gcry_md_hd_t prf;
@@ -149,39 +149,39 @@ GNUNET_CRYPTO_hkdf_v(void *result, size_t out_len, int xtr_algo, int prf_algo,
149 unsigned long i; 149 unsigned long i;
150 unsigned long t; 150 unsigned long t;
151 unsigned long d; 151 unsigned long d;
152 unsigned int k = gcry_md_get_algo_dlen(prf_algo); 152 unsigned int k = gcry_md_get_algo_dlen (prf_algo);
153 unsigned int xtr_len = gcry_md_get_algo_dlen(xtr_algo); 153 unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
154 char prk[xtr_len]; 154 char prk[xtr_len];
155 int ret; 155 int ret;
156 size_t ctx_len; 156 size_t ctx_len;
157 va_list args; 157 va_list args;
158 158
159 BENCHMARK_START(hkdf); 159 BENCHMARK_START (hkdf);
160 160
161 if (0 == k) 161 if (0 == k)
162 return GNUNET_SYSERR; 162 return GNUNET_SYSERR;
163 if (GPG_ERR_NO_ERROR != 163 if (GPG_ERR_NO_ERROR !=
164 gcry_md_open(&xtr, xtr_algo, GCRY_MD_FLAG_HMAC)) 164 gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC))
165 return GNUNET_SYSERR; 165 return GNUNET_SYSERR;
166 if (GPG_ERR_NO_ERROR != 166 if (GPG_ERR_NO_ERROR !=
167 gcry_md_open(&prf, prf_algo, GCRY_MD_FLAG_HMAC)) 167 gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC))
168 { 168 {
169 gcry_md_close(xtr); 169 gcry_md_close (xtr);
170 return GNUNET_SYSERR; 170 return GNUNET_SYSERR;
171 } 171 }
172 va_copy(args, argp); 172 va_copy (args, argp);
173 173
174 ctx_len = 0; 174 ctx_len = 0;
175 while (NULL != va_arg(args, void *)) 175 while (NULL != va_arg (args, void *))
176 ctx_len += va_arg(args, size_t); 176 ctx_len += va_arg (args, size_t);
177 177
178 va_end(args); 178 va_end (args);
179 179
180 memset(result, 0, out_len); 180 memset (result, 0, out_len);
181 if (getPRK(xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES) 181 if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
182 goto hkdf_error; 182 goto hkdf_error;
183#if DEBUG_HKDF 183#if DEBUG_HKDF
184 dump("PRK", prk, xtr_len); 184 dump ("PRK", prk, xtr_len);
185#endif 185#endif
186 186
187 t = out_len / k; 187 t = out_len / k;
@@ -195,69 +195,69 @@ GNUNET_CRYPTO_hkdf_v(void *result, size_t out_len, int xtr_algo, int prf_algo,
195 char *dst; 195 char *dst;
196 196
197 dst = plain + k; 197 dst = plain + k;
198 va_copy(args, argp); 198 va_copy (args, argp);
199 while ((ctx = va_arg(args, void *))) 199 while ((ctx = va_arg (args, void *)))
200 { 200 {
201 size_t len; 201 size_t len;
202 202
203 len = va_arg(args, size_t); 203 len = va_arg (args, size_t);
204 GNUNET_memcpy(dst, ctx, len); 204 GNUNET_memcpy (dst, ctx, len);
205 dst += len; 205 dst += len;
206 } 206 }
207 va_end(args); 207 va_end (args);
208 208
209 if (t > 0) 209 if (t > 0)
210 { 210 {
211 memset(plain + k + ctx_len, 1, 1); 211 memset (plain + k + ctx_len, 1, 1);
212#if DEBUG_HKDF 212#if DEBUG_HKDF
213 dump("K(1)", plain, plain_len); 213 dump ("K(1)", plain, plain_len);
214#endif 214#endif
215 hc = doHMAC(prf, prk, xtr_len, &plain[k], ctx_len + 1); 215 hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
216 if (hc == NULL) 216 if (hc == NULL)
217 goto hkdf_error; 217 goto hkdf_error;
218 GNUNET_memcpy(result, hc, k); 218 GNUNET_memcpy (result, hc, k);
219 result += k; 219 result += k;
220 } 220 }
221 221
222 /* K(i+1) */ 222 /* K(i+1) */
223 for (i = 1; i < t; i++) 223 for (i = 1; i < t; i++)
224 { 224 {
225 GNUNET_memcpy(plain, result - k, k); 225 GNUNET_memcpy (plain, result - k, k);
226 memset(plain + k + ctx_len, i + 1, 1); 226 memset (plain + k + ctx_len, i + 1, 1);
227 gcry_md_reset(prf); 227 gcry_md_reset (prf);
228#if DEBUG_HKDF 228#if DEBUG_HKDF
229 dump("K(i+1)", plain, plain_len); 229 dump ("K(i+1)", plain, plain_len);
230#endif 230#endif
231 hc = doHMAC(prf, prk, xtr_len, plain, plain_len); 231 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
232 if (hc == NULL) 232 if (hc == NULL)
233 goto hkdf_error; 233 goto hkdf_error;
234 GNUNET_memcpy(result, hc, k); 234 GNUNET_memcpy (result, hc, k);
235 result += k; 235 result += k;
236 } 236 }
237 237
238 /* K(t):d */ 238 /* K(t):d */
239 if (d > 0) 239 if (d > 0)
240 {
241 if (t > 0)
240 { 242 {
241 if (t > 0) 243 GNUNET_memcpy (plain, result - k, k);
242 { 244 i++;
243 GNUNET_memcpy(plain, result - k, k); 245 }
244 i++; 246 memset (plain + k + ctx_len, i, 1);
245 } 247 gcry_md_reset (prf);
246 memset(plain + k + ctx_len, i, 1);
247 gcry_md_reset(prf);
248#if DEBUG_HKDF 248#if DEBUG_HKDF
249 dump("K(t):d", plain, plain_len); 249 dump ("K(t):d", plain, plain_len);
250#endif 250#endif
251 if (t > 0) 251 if (t > 0)
252 hc = doHMAC(prf, prk, xtr_len, plain, plain_len); 252 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
253 else 253 else
254 hc = doHMAC(prf, prk, xtr_len, plain + k, plain_len - k); 254 hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
255 if (hc == NULL) 255 if (hc == NULL)
256 goto hkdf_error; 256 goto hkdf_error;
257 GNUNET_memcpy(result, hc, d); 257 GNUNET_memcpy (result, hc, d);
258 } 258 }
259#if DEBUG_HKDF 259#if DEBUG_HKDF
260 dump("result", result - k, out_len); 260 dump ("result", result - k, out_len);
261#endif 261#endif
262 262
263 ret = GNUNET_YES; 263 ret = GNUNET_YES;
@@ -266,9 +266,9 @@ GNUNET_CRYPTO_hkdf_v(void *result, size_t out_len, int xtr_algo, int prf_algo,
266hkdf_error: 266hkdf_error:
267 ret = GNUNET_SYSERR; 267 ret = GNUNET_SYSERR;
268hkdf_ok: 268hkdf_ok:
269 gcry_md_close(xtr); 269 gcry_md_close (xtr);
270 gcry_md_close(prf); 270 gcry_md_close (prf);
271 BENCHMARK_END(hkdf); 271 BENCHMARK_END (hkdf);
272 return ret; 272 return ret;
273} 273}
274 274
@@ -286,18 +286,18 @@ hkdf_ok:
286 * @return #GNUNET_YES on success 286 * @return #GNUNET_YES on success
287 */ 287 */
288int 288int
289GNUNET_CRYPTO_hkdf(void *result, size_t out_len, int xtr_algo, int prf_algo, 289GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
290 const void *xts, size_t xts_len, const void *skm, 290 const void *xts, size_t xts_len, const void *skm,
291 size_t skm_len, ...) 291 size_t skm_len, ...)
292{ 292{
293 va_list argp; 293 va_list argp;
294 int ret; 294 int ret;
295 295
296 va_start(argp, skm_len); 296 va_start (argp, skm_len);
297 ret = 297 ret =
298 GNUNET_CRYPTO_hkdf_v(result, out_len, xtr_algo, prf_algo, xts, xts_len, 298 GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
299 skm, skm_len, argp); 299 skm, skm_len, argp);
300 va_end(argp); 300 va_end (argp);
301 301
302 return ret; 302 return ret;
303} 303}
diff --git a/src/util/crypto_kdf.c b/src/util/crypto_kdf.c
index ef96889b2..ac881b055 100644
--- a/src/util/crypto_kdf.c
+++ b/src/util/crypto_kdf.c
@@ -30,7 +30,7 @@
30#include "platform.h" 30#include "platform.h"
31#include "gnunet_crypto_lib.h" 31#include "gnunet_crypto_lib.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-kdf", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-kdf", __VA_ARGS__)
34 34
35/** 35/**
36 * @brief Derive key 36 * @brief Derive key
@@ -44,13 +44,13 @@
44 * @return #GNUNET_YES on success 44 * @return #GNUNET_YES on success
45 */ 45 */
46int 46int
47GNUNET_CRYPTO_kdf_v(void *result, 47GNUNET_CRYPTO_kdf_v (void *result,
48 size_t out_len, 48 size_t out_len,
49 const void *xts, 49 const void *xts,
50 size_t xts_len, 50 size_t xts_len,
51 const void *skm, 51 const void *skm,
52 size_t skm_len, 52 size_t skm_len,
53 va_list argp) 53 va_list argp)
54{ 54{
55 /* 55 /*
56 * "Finally, we point out to a particularly advantageous instantiation using 56 * "Finally, we point out to a particularly advantageous instantiation using
@@ -64,15 +64,15 @@ GNUNET_CRYPTO_kdf_v(void *result,
64 * http://eprint.iacr.org/2010/264 64 * http://eprint.iacr.org/2010/264
65 */ 65 */
66 66
67 return GNUNET_CRYPTO_hkdf_v(result, 67 return GNUNET_CRYPTO_hkdf_v (result,
68 out_len, 68 out_len,
69 GCRY_MD_SHA512, 69 GCRY_MD_SHA512,
70 GCRY_MD_SHA256, 70 GCRY_MD_SHA256,
71 xts, 71 xts,
72 xts_len, 72 xts_len,
73 skm, 73 skm,
74 skm_len, 74 skm_len,
75 argp); 75 argp);
76} 76}
77 77
78 78
@@ -88,25 +88,25 @@ GNUNET_CRYPTO_kdf_v(void *result,
88 * @return #GNUNET_YES on success 88 * @return #GNUNET_YES on success
89 */ 89 */
90int 90int
91GNUNET_CRYPTO_kdf(void *result, 91GNUNET_CRYPTO_kdf (void *result,
92 size_t out_len, 92 size_t out_len,
93 const void *xts, 93 const void *xts,
94 size_t xts_len, 94 size_t xts_len,
95 const void *skm, 95 const void *skm,
96 size_t skm_len, ...) 96 size_t skm_len, ...)
97{ 97{
98 va_list argp; 98 va_list argp;
99 int ret; 99 int ret;
100 100
101 va_start(argp, skm_len); 101 va_start (argp, skm_len);
102 ret = GNUNET_CRYPTO_kdf_v(result, 102 ret = GNUNET_CRYPTO_kdf_v (result,
103 out_len, 103 out_len,
104 xts, 104 xts,
105 xts_len, 105 xts_len,
106 skm, 106 skm,
107 skm_len, 107 skm_len,
108 argp); 108 argp);
109 va_end(argp); 109 va_end (argp);
110 110
111 return ret; 111 return ret;
112} 112}
@@ -125,50 +125,50 @@ GNUNET_CRYPTO_kdf(void *result,
125 * @param ctx context string 125 * @param ctx context string
126 */ 126 */
127void 127void
128GNUNET_CRYPTO_kdf_mod_mpi(gcry_mpi_t *r, 128GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
129 gcry_mpi_t n, 129 gcry_mpi_t n,
130 const void *xts, size_t xts_len, 130 const void *xts, size_t xts_len,
131 const void *skm, size_t skm_len, 131 const void *skm, size_t skm_len,
132 const char *ctx) 132 const char *ctx)
133{ 133{
134 gcry_error_t rc; 134 gcry_error_t rc;
135 unsigned int nbits; 135 unsigned int nbits;
136 size_t rsize; 136 size_t rsize;
137 unsigned int ctr; 137 unsigned int ctr;
138 138
139 nbits = gcry_mpi_get_nbits(n); 139 nbits = gcry_mpi_get_nbits (n);
140 /* GNUNET_assert (nbits > 512); */ 140 /* GNUNET_assert (nbits > 512); */
141 141
142 ctr = 0; 142 ctr = 0;
143 while (1) 143 while (1)
144 { 144 {
145 /* Ain't clear if n is always divisible by 8 */ 145 /* Ain't clear if n is always divisible by 8 */
146 uint8_t buf[ (nbits - 1) / 8 + 1 ]; 146 uint8_t buf[ (nbits - 1) / 8 + 1 ];
147 147
148 rc = GNUNET_CRYPTO_kdf(buf, 148 rc = GNUNET_CRYPTO_kdf (buf,
149 sizeof(buf), 149 sizeof(buf),
150 xts, xts_len, 150 xts, xts_len,
151 skm, skm_len, 151 skm, skm_len,
152 ctx, strlen(ctx), 152 ctx, strlen (ctx),
153 &ctr, sizeof(ctr), 153 &ctr, sizeof(ctr),
154 NULL, 0); 154 NULL, 0);
155 GNUNET_assert(GNUNET_YES == rc); 155 GNUNET_assert (GNUNET_YES == rc);
156 156
157 rc = gcry_mpi_scan(r, 157 rc = gcry_mpi_scan (r,
158 GCRYMPI_FMT_USG, 158 GCRYMPI_FMT_USG,
159 (const unsigned char *)buf, 159 (const unsigned char *) buf,
160 sizeof(buf), 160 sizeof(buf),
161 &rsize); 161 &rsize);
162 GNUNET_assert(0 == rc); /* Allocation erro? */ 162 GNUNET_assert (0 == rc); /* Allocation erro? */
163 163
164 gcry_mpi_clear_highbit(*r, nbits); 164 gcry_mpi_clear_highbit (*r, nbits);
165 GNUNET_assert(0 == gcry_mpi_test_bit(*r, nbits)); 165 GNUNET_assert (0 == gcry_mpi_test_bit (*r, nbits));
166 ++ctr; 166 ++ctr;
167 /* We reject this FDH if either *r > n and retry with another ctr */ 167 /* We reject this FDH if either *r > n and retry with another ctr */
168 if (0 > gcry_mpi_cmp(*r, n)) 168 if (0 > gcry_mpi_cmp (*r, n))
169 break; 169 break;
170 gcry_mpi_release(*r); 170 gcry_mpi_release (*r);
171 } 171 }
172} 172}
173 173
174/* end of crypto_kdf.c */ 174/* end of crypto_kdf.c */
diff --git a/src/util/crypto_mpi.c b/src/util/crypto_mpi.c
index 2c402e95a..5f1b456a5 100644
--- a/src/util/crypto_mpi.c
+++ b/src/util/crypto_mpi.c
@@ -29,14 +29,17 @@
29#include "gnunet_crypto_lib.h" 29#include "gnunet_crypto_lib.h"
30 30
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-mpi", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-mpi", __VA_ARGS__)
33 33
34/** 34/**
35 * Log an error message at log-level 'level' that indicates 35 * Log an error message at log-level 'level' that indicates
36 * a failure of the command 'cmd' with the message given 36 * a failure of the command 'cmd' with the message given
37 * by gcry_strerror(rc). 37 * by gcry_strerror(rc).
38 */ 38 */
39#define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while (0) 39#define LOG_GCRY(level, cmd, rc) do { LOG (level, _ ( \
40 "`%s' failed at %s:%d with error: %s\n"), \
41 cmd, __FILE__, __LINE__, \
42 gcry_strerror (rc)); } while (0)
40 43
41 44
42/** 45/**
@@ -48,17 +51,17 @@
48 * @param target target size of the buffer 51 * @param target target size of the buffer
49 */ 52 */
50static void 53static void
51adjust(void *buf, 54adjust (void *buf,
52 size_t size, 55 size_t size,
53 size_t target) 56 size_t target)
54{ 57{
55 char *p = buf; 58 char *p = buf;
56 59
57 if (size < target) 60 if (size < target)
58 { 61 {
59 memmove(&p[target - size], buf, size); 62 memmove (&p[target - size], buf, size);
60 memset(buf, 0, target - size); 63 memset (buf, 0, target - size);
61 } 64 }
62} 65}
63 66
64 67
@@ -72,46 +75,46 @@ adjust(void *buf,
72 * @param val value to write to @a buf 75 * @param val value to write to @a buf
73 */ 76 */
74void 77void
75GNUNET_CRYPTO_mpi_print_unsigned(void *buf, 78GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
76 size_t size, 79 size_t size,
77 gcry_mpi_t val) 80 gcry_mpi_t val)
78{ 81{
79 size_t rsize; 82 size_t rsize;
80 int rc; 83 int rc;
81 84
82 if (gcry_mpi_get_flag(val, GCRYMPI_FLAG_OPAQUE)) 85 if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE))
83 { 86 {
84 /* Store opaque MPIs left aligned into the buffer. */ 87 /* Store opaque MPIs left aligned into the buffer. */
85 unsigned int nbits; 88 unsigned int nbits;
86 const void *p; 89 const void *p;
87 90
88 p = gcry_mpi_get_opaque(val, &nbits); 91 p = gcry_mpi_get_opaque (val, &nbits);
89 GNUNET_assert(p); 92 GNUNET_assert (p);
90 rsize = (nbits + 7) / 8; 93 rsize = (nbits + 7) / 8;
91 if (rsize > size) 94 if (rsize > size)
92 rsize = size; 95 rsize = size;
93 GNUNET_memcpy(buf, p, rsize); 96 GNUNET_memcpy (buf, p, rsize);
94 if (rsize < size) 97 if (rsize < size)
95 memset(buf + rsize, 0, size - rsize); 98 memset (buf + rsize, 0, size - rsize);
96 } 99 }
97 else 100 else
101 {
102 /* Store regular MPIs as unsigned integers right aligned into
103 the buffer. */
104 rsize = size;
105 if (0 !=
106 (rc = gcry_mpi_print (GCRYMPI_FMT_USG,
107 buf,
108 rsize, &rsize,
109 val)))
98 { 110 {
99 /* Store regular MPIs as unsigned integers right aligned into 111 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
100 the buffer. */ 112 "gcry_mpi_print",
101 rsize = size; 113 rc);
102 if (0 != 114 GNUNET_assert (0);
103 (rc = gcry_mpi_print(GCRYMPI_FMT_USG,
104 buf,
105 rsize, &rsize,
106 val)))
107 {
108 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR,
109 "gcry_mpi_print",
110 rc);
111 GNUNET_assert(0);
112 }
113 adjust(buf, rsize, size);
114 } 115 }
116 adjust (buf, rsize, size);
117 }
115} 118}
116 119
117 120
@@ -125,21 +128,21 @@ GNUNET_CRYPTO_mpi_print_unsigned(void *buf,
125 * @param size number of bytes in @a data 128 * @param size number of bytes in @a data
126 */ 129 */
127void 130void
128GNUNET_CRYPTO_mpi_scan_unsigned(gcry_mpi_t *result, 131GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
129 const void *data, 132 const void *data,
130 size_t size) 133 size_t size)
131{ 134{
132 int rc; 135 int rc;
133 136
134 if (0 != (rc = gcry_mpi_scan(result, 137 if (0 != (rc = gcry_mpi_scan (result,
135 GCRYMPI_FMT_USG, 138 GCRYMPI_FMT_USG,
136 data, size, &size))) 139 data, size, &size)))
137 { 140 {
138 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, 141 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
139 "gcry_mpi_scan", 142 "gcry_mpi_scan",
140 rc); 143 rc);
141 GNUNET_assert(0); 144 GNUNET_assert (0);
142 } 145 }
143} 146}
144 147
145/* end of crypto_mpi.c */ 148/* end of crypto_mpi.c */
diff --git a/src/util/crypto_paillier.c b/src/util/crypto_paillier.c
index 27a9af5de..75d607706 100644
--- a/src/util/crypto_paillier.c
+++ b/src/util/crypto_paillier.c
@@ -36,8 +36,10 @@
36 * @param[out] private_key Where to store the private key? 36 * @param[out] private_key Where to store the private key?
37 */ 37 */
38void 38void
39GNUNET_CRYPTO_paillier_create(struct GNUNET_CRYPTO_PaillierPublicKey *public_key, 39GNUNET_CRYPTO_paillier_create (struct
40 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key) 40 GNUNET_CRYPTO_PaillierPublicKey *public_key,
41 struct GNUNET_CRYPTO_PaillierPrivateKey *
42 private_key)
41{ 43{
42 gcry_mpi_t p; 44 gcry_mpi_t p;
43 gcry_mpi_t q; 45 gcry_mpi_t q;
@@ -50,55 +52,56 @@ GNUNET_CRYPTO_paillier_create(struct GNUNET_CRYPTO_PaillierPublicKey *public_key
50 p = NULL; 52 p = NULL;
51 q = NULL; 53 q = NULL;
52 do 54 do
53 { 55 {
54 if (NULL != p) 56 if (NULL != p)
55 gcry_mpi_release(p); 57 gcry_mpi_release (p);
56 if (NULL != q) 58 if (NULL != q)
57 gcry_mpi_release(q); 59 gcry_mpi_release (q);
58 GNUNET_assert(0 == 60 GNUNET_assert (0 ==
59 gcry_prime_generate(&p, 61 gcry_prime_generate (&p,
60 GNUNET_CRYPTO_PAILLIER_BITS / 2, 62 GNUNET_CRYPTO_PAILLIER_BITS / 2,
61 0, NULL, NULL, NULL, 63 0, NULL, NULL, NULL,
62 GCRY_STRONG_RANDOM, 0)); 64 GCRY_STRONG_RANDOM, 0));
63 GNUNET_assert(0 == 65 GNUNET_assert (0 ==
64 gcry_prime_generate(&q, 66 gcry_prime_generate (&q,
65 GNUNET_CRYPTO_PAILLIER_BITS / 2, 67 GNUNET_CRYPTO_PAILLIER_BITS / 2,
66 0, NULL, NULL, NULL, 68 0, NULL, NULL, NULL,
67 GCRY_STRONG_RANDOM, 0)); 69 GCRY_STRONG_RANDOM, 0));
68 } 70 }
69 while (0 == gcry_mpi_cmp(p, q)); 71 while (0 == gcry_mpi_cmp (p, q));
70 /* n = p * q */ 72 /* n = p * q */
71 GNUNET_assert(NULL != (n = gcry_mpi_new(0))); 73 GNUNET_assert (NULL != (n = gcry_mpi_new (0)));
72 gcry_mpi_mul(n, 74 gcry_mpi_mul (n,
73 p, 75 p,
74 q); 76 q);
75 GNUNET_CRYPTO_mpi_print_unsigned(public_key, 77 GNUNET_CRYPTO_mpi_print_unsigned (public_key,
76 sizeof(struct GNUNET_CRYPTO_PaillierPublicKey), 78 sizeof(struct
77 n); 79 GNUNET_CRYPTO_PaillierPublicKey),
80 n);
78 81
79 /* compute phi(n) = (p-1)(q-1) */ 82 /* compute phi(n) = (p-1)(q-1) */
80 GNUNET_assert(NULL != (phi = gcry_mpi_new(0))); 83 GNUNET_assert (NULL != (phi = gcry_mpi_new (0)));
81 gcry_mpi_sub_ui(p, p, 1); 84 gcry_mpi_sub_ui (p, p, 1);
82 gcry_mpi_sub_ui(q, q, 1); 85 gcry_mpi_sub_ui (q, q, 1);
83 gcry_mpi_mul(phi, p, q); 86 gcry_mpi_mul (phi, p, q);
84 gcry_mpi_release(p); 87 gcry_mpi_release (p);
85 gcry_mpi_release(q); 88 gcry_mpi_release (q);
86 89
87 /* lambda equals phi(n) in the simplified key generation */ 90 /* lambda equals phi(n) in the simplified key generation */
88 GNUNET_CRYPTO_mpi_print_unsigned(private_key->lambda, 91 GNUNET_CRYPTO_mpi_print_unsigned (private_key->lambda,
89 GNUNET_CRYPTO_PAILLIER_BITS / 8, 92 GNUNET_CRYPTO_PAILLIER_BITS / 8,
90 phi); 93 phi);
91 /* mu = phi^{-1} mod n, as we use g = n + 1 */ 94 /* mu = phi^{-1} mod n, as we use g = n + 1 */
92 GNUNET_assert(NULL != (mu = gcry_mpi_new(0))); 95 GNUNET_assert (NULL != (mu = gcry_mpi_new (0)));
93 GNUNET_assert(0 != gcry_mpi_invm(mu, 96 GNUNET_assert (0 != gcry_mpi_invm (mu,
94 phi, 97 phi,
95 n)); 98 n));
96 gcry_mpi_release(phi); 99 gcry_mpi_release (phi);
97 gcry_mpi_release(n); 100 gcry_mpi_release (n);
98 GNUNET_CRYPTO_mpi_print_unsigned(private_key->mu, 101 GNUNET_CRYPTO_mpi_print_unsigned (private_key->mu,
99 GNUNET_CRYPTO_PAILLIER_BITS / 8, 102 GNUNET_CRYPTO_PAILLIER_BITS / 8,
100 mu); 103 mu);
101 gcry_mpi_release(mu); 104 gcry_mpi_release (mu);
102} 105}
103 106
104 107
@@ -114,10 +117,12 @@ GNUNET_CRYPTO_paillier_create(struct GNUNET_CRYPTO_PaillierPublicKey *public_key
114 * or -1 if less than one homomorphic operation is possible 117 * or -1 if less than one homomorphic operation is possible
115 */ 118 */
116int 119int
117GNUNET_CRYPTO_paillier_encrypt1(const struct GNUNET_CRYPTO_PaillierPublicKey *public_key, 120GNUNET_CRYPTO_paillier_encrypt1 (const struct
118 const gcry_mpi_t m, 121 GNUNET_CRYPTO_PaillierPublicKey *public_key,
119 int desired_ops, 122 const gcry_mpi_t m,
120 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext) 123 int desired_ops,
124 struct GNUNET_CRYPTO_PaillierCiphertext *
125 ciphertext)
121{ 126{
122 int possible_opts; 127 int possible_opts;
123 gcry_mpi_t n_square; 128 gcry_mpi_t n_square;
@@ -130,70 +135,71 @@ GNUNET_CRYPTO_paillier_encrypt1(const struct GNUNET_CRYPTO_PaillierPublicKey *pu
130 135
131 /* determine how many operations we could allow, if the other number 136 /* determine how many operations we could allow, if the other number
132 has the same length. */ 137 has the same length. */
133 GNUNET_assert(NULL != (tmp1 = gcry_mpi_set_ui(NULL, 1))); 138 GNUNET_assert (NULL != (tmp1 = gcry_mpi_set_ui (NULL, 1)));
134 GNUNET_assert(NULL != (tmp2 = gcry_mpi_set_ui(NULL, 2))); 139 GNUNET_assert (NULL != (tmp2 = gcry_mpi_set_ui (NULL, 2)));
135 gcry_mpi_mul_2exp(tmp1, tmp1, GNUNET_CRYPTO_PAILLIER_BITS); 140 gcry_mpi_mul_2exp (tmp1, tmp1, GNUNET_CRYPTO_PAILLIER_BITS);
136 141
137 /* count number of possible operations 142 /* count number of possible operations
138 this would be nicer with gcry_mpi_get_nbits, however it does not return 143 this would be nicer with gcry_mpi_get_nbits, however it does not return
139 the BITLENGTH of the given MPI's value, but the bits required 144 the BITLENGTH of the given MPI's value, but the bits required
140 to represent the number as MPI. */ 145 to represent the number as MPI. */
141 for (possible_opts = -2; gcry_mpi_cmp(tmp1, m) > 0; possible_opts++) 146 for (possible_opts = -2; gcry_mpi_cmp (tmp1, m) > 0; possible_opts++)
142 gcry_mpi_div(tmp1, NULL, tmp1, tmp2, 0); 147 gcry_mpi_div (tmp1, NULL, tmp1, tmp2, 0);
143 gcry_mpi_release(tmp1); 148 gcry_mpi_release (tmp1);
144 gcry_mpi_release(tmp2); 149 gcry_mpi_release (tmp2);
145 150
146 if (possible_opts < 1) 151 if (possible_opts < 1)
147 possible_opts = 0; 152 possible_opts = 0;
148 /* soft-cap by caller */ 153 /* soft-cap by caller */
149 possible_opts = (desired_ops < possible_opts) ? desired_ops : possible_opts; 154 possible_opts = (desired_ops < possible_opts) ? desired_ops : possible_opts;
150 155
151 ciphertext->remaining_ops = htonl(possible_opts); 156 ciphertext->remaining_ops = htonl (possible_opts);
152 157
153 GNUNET_CRYPTO_mpi_scan_unsigned(&n, 158 GNUNET_CRYPTO_mpi_scan_unsigned (&n,
154 public_key, 159 public_key,
155 sizeof(struct GNUNET_CRYPTO_PaillierPublicKey)); 160 sizeof(struct
161 GNUNET_CRYPTO_PaillierPublicKey));
156 highbit = GNUNET_CRYPTO_PAILLIER_BITS - 1; 162 highbit = GNUNET_CRYPTO_PAILLIER_BITS - 1;
157 while ((!gcry_mpi_test_bit(n, highbit)) && 163 while ((! gcry_mpi_test_bit (n, highbit)) &&
158 (0 != highbit)) 164 (0 != highbit))
159 highbit--; 165 highbit--;
160 if (0 == highbit) 166 if (0 == highbit)
161 { 167 {
162 /* invalid public key */ 168 /* invalid public key */
163 GNUNET_break_op(0); 169 GNUNET_break_op (0);
164 gcry_mpi_release(n); 170 gcry_mpi_release (n);
165 return GNUNET_SYSERR; 171 return GNUNET_SYSERR;
166 } 172 }
167 GNUNET_assert(0 != (n_square = gcry_mpi_new(0))); 173 GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
168 GNUNET_assert(0 != (r = gcry_mpi_new(0))); 174 GNUNET_assert (0 != (r = gcry_mpi_new (0)));
169 GNUNET_assert(0 != (c = gcry_mpi_new(0))); 175 GNUNET_assert (0 != (c = gcry_mpi_new (0)));
170 gcry_mpi_mul(n_square, n, n); 176 gcry_mpi_mul (n_square, n, n);
171 177
172 /* generate r < n (without bias) */ 178 /* generate r < n (without bias) */
173 do 179 do
174 { 180 {
175 gcry_mpi_randomize(r, highbit + 1, GCRY_STRONG_RANDOM); 181 gcry_mpi_randomize (r, highbit + 1, GCRY_STRONG_RANDOM);
176 } 182 }
177 while (gcry_mpi_cmp(r, n) >= 0); 183 while (gcry_mpi_cmp (r, n) >= 0);
178 184
179 /* c = (n+1)^m mod n^2 */ 185 /* c = (n+1)^m mod n^2 */
180 /* c = n + 1 */ 186 /* c = n + 1 */
181 gcry_mpi_add_ui(c, n, 1); 187 gcry_mpi_add_ui (c, n, 1);
182 /* c = (n+1)^m mod n^2 */ 188 /* c = (n+1)^m mod n^2 */
183 gcry_mpi_powm(c, c, m, n_square); 189 gcry_mpi_powm (c, c, m, n_square);
184 /* r <- r^n mod n^2 */ 190 /* r <- r^n mod n^2 */
185 gcry_mpi_powm(r, r, n, n_square); 191 gcry_mpi_powm (r, r, n, n_square);
186 /* c <- r*c mod n^2 */ 192 /* c <- r*c mod n^2 */
187 gcry_mpi_mulm(c, r, c, n_square); 193 gcry_mpi_mulm (c, r, c, n_square);
188 194
189 GNUNET_CRYPTO_mpi_print_unsigned(ciphertext->bits, 195 GNUNET_CRYPTO_mpi_print_unsigned (ciphertext->bits,
190 sizeof ciphertext->bits, 196 sizeof ciphertext->bits,
191 c); 197 c);
192 198
193 gcry_mpi_release(n_square); 199 gcry_mpi_release (n_square);
194 gcry_mpi_release(n); 200 gcry_mpi_release (n);
195 gcry_mpi_release(r); 201 gcry_mpi_release (r);
196 gcry_mpi_release(c); 202 gcry_mpi_release (c);
197 203
198 return possible_opts; 204 return possible_opts;
199} 205}
@@ -211,10 +217,12 @@ GNUNET_CRYPTO_paillier_encrypt1(const struct GNUNET_CRYPTO_PaillierPublicKey *pu
211 * or -1 if less than one homomorphic operation is possible 217 * or -1 if less than one homomorphic operation is possible
212 */ 218 */
213int 219int
214GNUNET_CRYPTO_paillier_encrypt(const struct GNUNET_CRYPTO_PaillierPublicKey *public_key, 220GNUNET_CRYPTO_paillier_encrypt (const struct
215 const gcry_mpi_t m, 221 GNUNET_CRYPTO_PaillierPublicKey *public_key,
216 int desired_ops, 222 const gcry_mpi_t m,
217 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext) 223 int desired_ops,
224 struct GNUNET_CRYPTO_PaillierCiphertext *
225 ciphertext)
218{ 226{
219 int possible_opts; 227 int possible_opts;
220 gcry_mpi_t n_square; 228 gcry_mpi_t n_square;
@@ -229,87 +237,88 @@ GNUNET_CRYPTO_paillier_encrypt(const struct GNUNET_CRYPTO_PaillierPublicKey *pub
229 237
230 /* set max_num = 2^{GNUNET_CRYPTO_PAILLIER_BITS}, the largest 238 /* set max_num = 2^{GNUNET_CRYPTO_PAILLIER_BITS}, the largest
231 number we can have as a result */ 239 number we can have as a result */
232 GNUNET_assert(NULL != (max_num = gcry_mpi_set_ui(NULL, 1))); 240 GNUNET_assert (NULL != (max_num = gcry_mpi_set_ui (NULL, 1)));
233 gcry_mpi_mul_2exp(max_num, 241 gcry_mpi_mul_2exp (max_num,
234 max_num, 242 max_num,
235 GNUNET_CRYPTO_PAILLIER_BITS); 243 GNUNET_CRYPTO_PAILLIER_BITS);
236 244
237 /* Determine how many operations we could allow, assuming the other 245 /* Determine how many operations we could allow, assuming the other
238 number has the same length (or is smaller), by counting the 246 number has the same length (or is smaller), by counting the
239 number of possible operations. We essentially divide max_num by 247 number of possible operations. We essentially divide max_num by
240 2 until the result is no longer larger than 'm', incrementing the 248 2 until the result is no longer larger than 'm', incrementing the
241 maximum number of operations in each round, starting at -2 */ 249 maximum number of operations in each round, starting at -2 */
242 for (possible_opts = -2; gcry_mpi_cmp(max_num, m) > 0; possible_opts++) 250 for (possible_opts = -2; gcry_mpi_cmp (max_num, m) > 0; possible_opts++)
243 gcry_mpi_div(max_num, 251 gcry_mpi_div (max_num,
244 NULL, 252 NULL,
245 max_num, 253 max_num,
246 GCRYMPI_CONST_TWO, 254 GCRYMPI_CONST_TWO,
247 0); 255 0);
248 gcry_mpi_release(max_num); 256 gcry_mpi_release (max_num);
249 257
250 if (possible_opts < 1) 258 if (possible_opts < 1)
251 possible_opts = 0; 259 possible_opts = 0;
252 /* Enforce soft-cap by caller */ 260 /* Enforce soft-cap by caller */
253 possible_opts = GNUNET_MIN(desired_ops, possible_opts); 261 possible_opts = GNUNET_MIN (desired_ops, possible_opts);
254 ciphertext->remaining_ops = htonl(possible_opts); 262 ciphertext->remaining_ops = htonl (possible_opts);
255 263
256 GNUNET_CRYPTO_mpi_scan_unsigned(&n, 264 GNUNET_CRYPTO_mpi_scan_unsigned (&n,
257 public_key, 265 public_key,
258 sizeof(struct GNUNET_CRYPTO_PaillierPublicKey)); 266 sizeof(struct
267 GNUNET_CRYPTO_PaillierPublicKey));
259 268
260 /* check public key for number of bits, bail out if key is all zeros */ 269 /* check public key for number of bits, bail out if key is all zeros */
261 highbit = GNUNET_CRYPTO_PAILLIER_BITS - 1; 270 highbit = GNUNET_CRYPTO_PAILLIER_BITS - 1;
262 while ((!gcry_mpi_test_bit(n, highbit)) && 271 while ((! gcry_mpi_test_bit (n, highbit)) &&
263 (0 != highbit)) 272 (0 != highbit))
264 highbit--; 273 highbit--;
265 if (0 == highbit) 274 if (0 == highbit)
266 { 275 {
267 /* invalid public key */ 276 /* invalid public key */
268 GNUNET_break_op(0); 277 GNUNET_break_op (0);
269 gcry_mpi_release(n); 278 gcry_mpi_release (n);
270 return GNUNET_SYSERR; 279 return GNUNET_SYSERR;
271 } 280 }
272 281
273 /* generate r < n (without bias) */ 282 /* generate r < n (without bias) */
274 GNUNET_assert(NULL != (r = gcry_mpi_new(0))); 283 GNUNET_assert (NULL != (r = gcry_mpi_new (0)));
275 do 284 do
276 { 285 {
277 gcry_mpi_randomize(r, highbit + 1, GCRY_STRONG_RANDOM); 286 gcry_mpi_randomize (r, highbit + 1, GCRY_STRONG_RANDOM);
278 } 287 }
279 while (gcry_mpi_cmp(r, n) >= 0); 288 while (gcry_mpi_cmp (r, n) >= 0);
280 289
281 /* g = n + 1 */ 290 /* g = n + 1 */
282 GNUNET_assert(0 != (g = gcry_mpi_new(0))); 291 GNUNET_assert (0 != (g = gcry_mpi_new (0)));
283 gcry_mpi_add_ui(g, n, 1); 292 gcry_mpi_add_ui (g, n, 1);
284 293
285 /* n_square = n^2 */ 294 /* n_square = n^2 */
286 GNUNET_assert(0 != (n_square = gcry_mpi_new(0))); 295 GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
287 gcry_mpi_mul(n_square, 296 gcry_mpi_mul (n_square,
288 n, 297 n,
289 n); 298 n);
290 299
291 /* gm = g^m mod n^2 */ 300 /* gm = g^m mod n^2 */
292 GNUNET_assert(0 != (gm = gcry_mpi_new(0))); 301 GNUNET_assert (0 != (gm = gcry_mpi_new (0)));
293 gcry_mpi_powm(gm, g, m, n_square); 302 gcry_mpi_powm (gm, g, m, n_square);
294 gcry_mpi_release(g); 303 gcry_mpi_release (g);
295 304
296 /* rn <- r^n mod n^2 */ 305 /* rn <- r^n mod n^2 */
297 GNUNET_assert(0 != (rn = gcry_mpi_new(0))); 306 GNUNET_assert (0 != (rn = gcry_mpi_new (0)));
298 gcry_mpi_powm(rn, r, n, n_square); 307 gcry_mpi_powm (rn, r, n, n_square);
299 gcry_mpi_release(r); 308 gcry_mpi_release (r);
300 gcry_mpi_release(n); 309 gcry_mpi_release (n);
301 310
302 /* c <- rn * gm mod n^2 */ 311 /* c <- rn * gm mod n^2 */
303 GNUNET_assert(0 != (c = gcry_mpi_new(0))); 312 GNUNET_assert (0 != (c = gcry_mpi_new (0)));
304 gcry_mpi_mulm(c, rn, gm, n_square); 313 gcry_mpi_mulm (c, rn, gm, n_square);
305 gcry_mpi_release(n_square); 314 gcry_mpi_release (n_square);
306 gcry_mpi_release(gm); 315 gcry_mpi_release (gm);
307 gcry_mpi_release(rn); 316 gcry_mpi_release (rn);
308 317
309 GNUNET_CRYPTO_mpi_print_unsigned(ciphertext->bits, 318 GNUNET_CRYPTO_mpi_print_unsigned (ciphertext->bits,
310 sizeof(ciphertext->bits), 319 sizeof(ciphertext->bits),
311 c); 320 c);
312 gcry_mpi_release(c); 321 gcry_mpi_release (c);
313 322
314 return possible_opts; 323 return possible_opts;
315} 324}
@@ -324,10 +333,13 @@ GNUNET_CRYPTO_paillier_encrypt(const struct GNUNET_CRYPTO_PaillierPublicKey *pub
324 * @param[out] m Decryption of @a ciphertext with @private_key. 333 * @param[out] m Decryption of @a ciphertext with @private_key.
325 */ 334 */
326void 335void
327GNUNET_CRYPTO_paillier_decrypt(const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key, 336GNUNET_CRYPTO_paillier_decrypt (const struct
328 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key, 337 GNUNET_CRYPTO_PaillierPrivateKey *private_key,
329 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext, 338 const struct
330 gcry_mpi_t m) 339 GNUNET_CRYPTO_PaillierPublicKey *public_key,
340 const struct
341 GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
342 gcry_mpi_t m)
331{ 343{
332 gcry_mpi_t mu; 344 gcry_mpi_t mu;
333 gcry_mpi_t lambda; 345 gcry_mpi_t lambda;
@@ -338,48 +350,49 @@ GNUNET_CRYPTO_paillier_decrypt(const struct GNUNET_CRYPTO_PaillierPrivateKey *pr
338 gcry_mpi_t cmum1; 350 gcry_mpi_t cmum1;
339 gcry_mpi_t mod; 351 gcry_mpi_t mod;
340 352
341 GNUNET_CRYPTO_mpi_scan_unsigned(&lambda, 353 GNUNET_CRYPTO_mpi_scan_unsigned (&lambda,
342 private_key->lambda, 354 private_key->lambda,
343 sizeof(private_key->lambda)); 355 sizeof(private_key->lambda));
344 GNUNET_CRYPTO_mpi_scan_unsigned(&mu, 356 GNUNET_CRYPTO_mpi_scan_unsigned (&mu,
345 private_key->mu, 357 private_key->mu,
346 sizeof(private_key->mu)); 358 sizeof(private_key->mu));
347 GNUNET_CRYPTO_mpi_scan_unsigned(&n, 359 GNUNET_CRYPTO_mpi_scan_unsigned (&n,
348 public_key, 360 public_key,
349 sizeof(struct GNUNET_CRYPTO_PaillierPublicKey)); 361 sizeof(struct
350 GNUNET_CRYPTO_mpi_scan_unsigned(&c, 362 GNUNET_CRYPTO_PaillierPublicKey));
351 ciphertext->bits, 363 GNUNET_CRYPTO_mpi_scan_unsigned (&c,
352 sizeof(ciphertext->bits)); 364 ciphertext->bits,
365 sizeof(ciphertext->bits));
353 366
354 /* n_square = n * n */ 367 /* n_square = n * n */
355 GNUNET_assert(0 != (n_square = gcry_mpi_new(0))); 368 GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
356 gcry_mpi_mul(n_square, n, n); 369 gcry_mpi_mul (n_square, n, n);
357 370
358 /* cmu = c^lambda mod n^2 */ 371 /* cmu = c^lambda mod n^2 */
359 GNUNET_assert(0 != (cmu = gcry_mpi_new(0))); 372 GNUNET_assert (0 != (cmu = gcry_mpi_new (0)));
360 gcry_mpi_powm(cmu, 373 gcry_mpi_powm (cmu,
361 c, 374 c,
362 lambda, 375 lambda,
363 n_square); 376 n_square);
364 gcry_mpi_release(n_square); 377 gcry_mpi_release (n_square);
365 gcry_mpi_release(lambda); 378 gcry_mpi_release (lambda);
366 gcry_mpi_release(c); 379 gcry_mpi_release (c);
367 380
368 /* cmum1 = cmu - 1 */ 381 /* cmum1 = cmu - 1 */
369 GNUNET_assert(0 != (cmum1 = gcry_mpi_new(0))); 382 GNUNET_assert (0 != (cmum1 = gcry_mpi_new (0)));
370 gcry_mpi_sub_ui(cmum1, cmu, 1); 383 gcry_mpi_sub_ui (cmum1, cmu, 1);
371 gcry_mpi_release(cmu); 384 gcry_mpi_release (cmu);
372 385
373 /* mod = cmum1 / n (mod n) */ 386 /* mod = cmum1 / n (mod n) */
374 GNUNET_assert(0 != (mod = gcry_mpi_new(0))); 387 GNUNET_assert (0 != (mod = gcry_mpi_new (0)));
375 gcry_mpi_div(mod, NULL, cmum1, n, 0); 388 gcry_mpi_div (mod, NULL, cmum1, n, 0);
376 gcry_mpi_release(cmum1); 389 gcry_mpi_release (cmum1);
377 390
378 /* m = mod * mu mod n */ 391 /* m = mod * mu mod n */
379 gcry_mpi_mulm(m, mod, mu, n); 392 gcry_mpi_mulm (m, mod, mu, n);
380 gcry_mpi_release(mod); 393 gcry_mpi_release (mod);
381 gcry_mpi_release(mu); 394 gcry_mpi_release (mu);
382 gcry_mpi_release(n); 395 gcry_mpi_release (n);
383} 396}
384 397
385 398
@@ -398,10 +411,13 @@ GNUNET_CRYPTO_paillier_decrypt(const struct GNUNET_CRYPTO_PaillierPrivateKey *pr
398 * #GNUNET_SYSERR if no more homomorphic operations are remaining. 411 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
399 */ 412 */
400int 413int
401GNUNET_CRYPTO_paillier_hom_add(const struct GNUNET_CRYPTO_PaillierPublicKey *public_key, 414GNUNET_CRYPTO_paillier_hom_add (const struct
402 const struct GNUNET_CRYPTO_PaillierCiphertext *c1, 415 GNUNET_CRYPTO_PaillierPublicKey *public_key,
403 const struct GNUNET_CRYPTO_PaillierCiphertext *c2, 416 const struct
404 struct GNUNET_CRYPTO_PaillierCiphertext *result) 417 GNUNET_CRYPTO_PaillierCiphertext *c1,
418 const struct
419 GNUNET_CRYPTO_PaillierCiphertext *c2,
420 struct GNUNET_CRYPTO_PaillierCiphertext *result)
405{ 421{
406 gcry_mpi_t a; 422 gcry_mpi_t a;
407 gcry_mpi_t b; 423 gcry_mpi_t b;
@@ -411,42 +427,43 @@ GNUNET_CRYPTO_paillier_hom_add(const struct GNUNET_CRYPTO_PaillierPublicKey *pub
411 int32_t o1; 427 int32_t o1;
412 int32_t o2; 428 int32_t o2;
413 429
414 o1 = (int32_t)ntohl(c1->remaining_ops); 430 o1 = (int32_t) ntohl (c1->remaining_ops);
415 o2 = (int32_t)ntohl(c2->remaining_ops); 431 o2 = (int32_t) ntohl (c2->remaining_ops);
416 if ((0 >= o1) || (0 >= o2)) 432 if ((0 >= o1) || (0 >= o2))
417 { 433 {
418 GNUNET_break_op(0); 434 GNUNET_break_op (0);
419 return GNUNET_SYSERR; 435 return GNUNET_SYSERR;
420 } 436 }
421 437
422 GNUNET_CRYPTO_mpi_scan_unsigned(&a, 438 GNUNET_CRYPTO_mpi_scan_unsigned (&a,
423 c1->bits, 439 c1->bits,
424 sizeof(c1->bits)); 440 sizeof(c1->bits));
425 GNUNET_CRYPTO_mpi_scan_unsigned(&b, 441 GNUNET_CRYPTO_mpi_scan_unsigned (&b,
426 c2->bits, 442 c2->bits,
427 sizeof(c2->bits)); 443 sizeof(c2->bits));
428 GNUNET_CRYPTO_mpi_scan_unsigned(&n, 444 GNUNET_CRYPTO_mpi_scan_unsigned (&n,
429 public_key, 445 public_key,
430 sizeof(struct GNUNET_CRYPTO_PaillierPublicKey)); 446 sizeof(struct
447 GNUNET_CRYPTO_PaillierPublicKey));
431 448
432 /* n_square = n * n */ 449 /* n_square = n * n */
433 GNUNET_assert(0 != (n_square = gcry_mpi_new(0))); 450 GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
434 gcry_mpi_mul(n_square, n, n); 451 gcry_mpi_mul (n_square, n, n);
435 gcry_mpi_release(n); 452 gcry_mpi_release (n);
436 453
437 /* c = a * b mod n_square */ 454 /* c = a * b mod n_square */
438 GNUNET_assert(0 != (c = gcry_mpi_new(0))); 455 GNUNET_assert (0 != (c = gcry_mpi_new (0)));
439 gcry_mpi_mulm(c, a, b, n_square); 456 gcry_mpi_mulm (c, a, b, n_square);
440 gcry_mpi_release(n_square); 457 gcry_mpi_release (n_square);
441 gcry_mpi_release(a); 458 gcry_mpi_release (a);
442 gcry_mpi_release(b); 459 gcry_mpi_release (b);
443 460
444 result->remaining_ops = htonl(GNUNET_MIN(o1, o2) - 1); 461 result->remaining_ops = htonl (GNUNET_MIN (o1, o2) - 1);
445 GNUNET_CRYPTO_mpi_print_unsigned(result->bits, 462 GNUNET_CRYPTO_mpi_print_unsigned (result->bits,
446 sizeof(result->bits), 463 sizeof(result->bits),
447 c); 464 c);
448 gcry_mpi_release(c); 465 gcry_mpi_release (c);
449 return ntohl(result->remaining_ops); 466 return ntohl (result->remaining_ops);
450} 467}
451 468
452 469
@@ -457,11 +474,11 @@ GNUNET_CRYPTO_paillier_hom_add(const struct GNUNET_CRYPTO_PaillierPublicKey *pub
457 * @return the number of remaining homomorphic operations 474 * @return the number of remaining homomorphic operations
458 */ 475 */
459int 476int
460GNUNET_CRYPTO_paillier_hom_get_remaining(const struct GNUNET_CRYPTO_PaillierCiphertext *c) 477GNUNET_CRYPTO_paillier_hom_get_remaining (const struct
478 GNUNET_CRYPTO_PaillierCiphertext *c)
461{ 479{
462 GNUNET_assert(NULL != c); 480 GNUNET_assert (NULL != c);
463 return ntohl(c->remaining_ops); 481 return ntohl (c->remaining_ops);
464} 482}
465 483
466/* end of crypto_paillier.c */ 484/* end of crypto_paillier.c */
467
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index d16ba2412..f70763757 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -28,23 +28,23 @@
28#include "gnunet_crypto_lib.h" 28#include "gnunet_crypto_lib.h"
29#include <gcrypt.h> 29#include <gcrypt.h>
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-random", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__)
32 32
33#define LOG_STRERROR(kind, syscall) \ 33#define LOG_STRERROR(kind, syscall) \
34 GNUNET_log_from_strerror(kind, "util-crypto-random", syscall) 34 GNUNET_log_from_strerror (kind, "util-crypto-random", syscall)
35 35
36 36
37/* TODO: ndurner, move this to plibc? */ 37/* TODO: ndurner, move this to plibc? */
38/* The code is derived from glibc, obviously */ 38/* The code is derived from glibc, obviously */
39#if !HAVE_RANDOM || !HAVE_SRANDOM 39#if ! HAVE_RANDOM || ! HAVE_SRANDOM
40#ifdef RANDOM 40#ifdef RANDOM
41#undef RANDOM 41#undef RANDOM
42#endif 42#endif
43#ifdef SRANDOM 43#ifdef SRANDOM
44#undef SRANDOM 44#undef SRANDOM
45#endif 45#endif
46#define RANDOM() glibc_weak_rand32() 46#define RANDOM() glibc_weak_rand32 ()
47#define SRANDOM(s) glibc_weak_srand32(s) 47#define SRANDOM(s) glibc_weak_srand32 (s)
48#if defined(RAND_MAX) 48#if defined(RAND_MAX)
49#undef RAND_MAX 49#undef RAND_MAX
50#endif 50#endif
@@ -55,14 +55,14 @@ static int32_t glibc_weak_rand32_state = 1;
55 55
56 56
57void 57void
58glibc_weak_srand32(int32_t s) 58glibc_weak_srand32 (int32_t s)
59{ 59{
60 glibc_weak_rand32_state = s; 60 glibc_weak_rand32_state = s;
61} 61}
62 62
63 63
64int32_t 64int32_t
65glibc_weak_rand32() 65glibc_weak_rand32 ()
66{ 66{
67 int32_t val = glibc_weak_rand32_state; 67 int32_t val = glibc_weak_rand32_state;
68 68
@@ -78,9 +78,9 @@ glibc_weak_rand32()
78 * @return number between 0 and 1. 78 * @return number between 0 and 1.
79 */ 79 */
80static double 80static double
81get_weak_random() 81get_weak_random ()
82{ 82{
83 return((double)random() / RAND_MAX); 83 return((double) random () / RAND_MAX);
84} 84}
85 85
86 86
@@ -91,9 +91,9 @@ get_weak_random()
91 * @param seed the seed to use 91 * @param seed the seed to use
92 */ 92 */
93void 93void
94GNUNET_CRYPTO_seed_weak_random(int32_t seed) 94GNUNET_CRYPTO_seed_weak_random (int32_t seed)
95{ 95{
96 srandom(seed); 96 srandom (seed);
97} 97}
98 98
99 99
@@ -106,12 +106,12 @@ GNUNET_CRYPTO_seed_weak_random(int32_t seed)
106 * @param length buffer length 106 * @param length buffer length
107 */ 107 */
108void 108void
109GNUNET_CRYPTO_zero_keys(void *buffer, size_t length) 109GNUNET_CRYPTO_zero_keys (void *buffer, size_t length)
110{ 110{
111#if HAVE_MEMSET_S 111#if HAVE_MEMSET_S
112 memset_s(buffer, length, 0, length); 112 memset_s (buffer, length, 0, length);
113#elif HAVE_EXPLICIT_BZERO 113#elif HAVE_EXPLICIT_BZERO
114 explicit_bzero(buffer, length); 114 explicit_bzero (buffer, length);
115#else 115#else
116 volatile unsigned char *p = buffer; 116 volatile unsigned char *p = buffer;
117 while (length--) 117 while (length--)
@@ -129,40 +129,40 @@ GNUNET_CRYPTO_zero_keys(void *buffer, size_t length)
129 * @param length buffer length 129 * @param length buffer length
130 */ 130 */
131void 131void
132GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, 132GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
133 void *buffer, 133 void *buffer,
134 size_t length) 134 size_t length)
135{ 135{
136#ifdef gcry_fast_random_poll 136#ifdef gcry_fast_random_poll
137 static unsigned int invokeCount; 137 static unsigned int invokeCount;
138#endif 138#endif
139 switch (mode) 139 switch (mode)
140 { 140 {
141 case GNUNET_CRYPTO_QUALITY_STRONG: 141 case GNUNET_CRYPTO_QUALITY_STRONG:
142 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ 142 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
143#ifdef gcry_fast_random_poll 143#ifdef gcry_fast_random_poll
144 if ((invokeCount++ % 256) == 0) 144 if ((invokeCount++ % 256) == 0)
145 gcry_fast_random_poll(); 145 gcry_fast_random_poll ();
146#endif 146#endif
147 gcry_randomize(buffer, length, GCRY_STRONG_RANDOM); 147 gcry_randomize (buffer, length, GCRY_STRONG_RANDOM);
148 return; 148 return;
149 149
150 case GNUNET_CRYPTO_QUALITY_NONCE: 150 case GNUNET_CRYPTO_QUALITY_NONCE:
151 gcry_create_nonce(buffer, length); 151 gcry_create_nonce (buffer, length);
152 return; 152 return;
153 153
154 case GNUNET_CRYPTO_QUALITY_WEAK: 154 case GNUNET_CRYPTO_QUALITY_WEAK:
155 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ 155 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
156#ifdef gcry_fast_random_poll 156#ifdef gcry_fast_random_poll
157 if ((invokeCount++ % 256) == 0) 157 if ((invokeCount++ % 256) == 0)
158 gcry_fast_random_poll(); 158 gcry_fast_random_poll ();
159#endif 159#endif
160 gcry_randomize(buffer, length, GCRY_WEAK_RANDOM); 160 gcry_randomize (buffer, length, GCRY_WEAK_RANDOM);
161 return; 161 return;
162 162
163 default: 163 default:
164 GNUNET_assert(0); 164 GNUNET_assert (0);
165 } 165 }
166} 166}
167 167
168 168
@@ -174,7 +174,7 @@ GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode,
174 * @return a random value in the interval [0,i[. 174 * @return a random value in the interval [0,i[.
175 */ 175 */
176uint32_t 176uint32_t
177GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i) 177GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
178{ 178{
179#ifdef gcry_fast_random_poll 179#ifdef gcry_fast_random_poll
180 static unsigned int invokeCount; 180 static unsigned int invokeCount;
@@ -182,44 +182,44 @@ GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
182 uint32_t ret; 182 uint32_t ret;
183 uint32_t ul; 183 uint32_t ul;
184 184
185 GNUNET_assert(i > 0); 185 GNUNET_assert (i > 0);
186 186
187 switch (mode) 187 switch (mode)
188 { 188 {
189 case GNUNET_CRYPTO_QUALITY_STRONG: 189 case GNUNET_CRYPTO_QUALITY_STRONG:
190 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ 190 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
191#ifdef gcry_fast_random_poll 191#ifdef gcry_fast_random_poll
192 if ((invokeCount++ % 256) == 0) 192 if ((invokeCount++ % 256) == 0)
193 gcry_fast_random_poll(); 193 gcry_fast_random_poll ();
194#endif 194#endif
195 ul = UINT32_MAX - (UINT32_MAX % i); 195 ul = UINT32_MAX - (UINT32_MAX % i);
196 do 196 do
197 { 197 {
198 gcry_randomize((unsigned char *)&ret, 198 gcry_randomize ((unsigned char *) &ret,
199 sizeof(uint32_t), 199 sizeof(uint32_t),
200 GCRY_STRONG_RANDOM); 200 GCRY_STRONG_RANDOM);
201 }
202 while (ret >= ul);
203 return ret % i;
204
205 case GNUNET_CRYPTO_QUALITY_NONCE:
206 ul = UINT32_MAX - (UINT32_MAX % i);
207 do
208 {
209 gcry_create_nonce(&ret, sizeof(ret));
210 }
211 while (ret >= ul);
212 return ret % i;
213
214 case GNUNET_CRYPTO_QUALITY_WEAK:
215 ret = i * get_weak_random();
216 if (ret >= i)
217 ret = i - 1;
218 return ret;
219
220 default:
221 GNUNET_assert(0);
222 } 201 }
202 while (ret >= ul);
203 return ret % i;
204
205 case GNUNET_CRYPTO_QUALITY_NONCE:
206 ul = UINT32_MAX - (UINT32_MAX % i);
207 do
208 {
209 gcry_create_nonce (&ret, sizeof(ret));
210 }
211 while (ret >= ul);
212 return ret % i;
213
214 case GNUNET_CRYPTO_QUALITY_WEAK:
215 ret = i * get_weak_random ();
216 if (ret >= i)
217 ret = i - 1;
218 return ret;
219
220 default:
221 GNUNET_assert (0);
222 }
223 return 0; 223 return 0;
224} 224}
225 225
@@ -233,24 +233,24 @@ GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
233 * @return the permutation array (allocated from heap) 233 * @return the permutation array (allocated from heap)
234 */ 234 */
235unsigned int * 235unsigned int *
236GNUNET_CRYPTO_random_permute(enum GNUNET_CRYPTO_Quality mode, unsigned int n) 236GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
237{ 237{
238 unsigned int *ret; 238 unsigned int *ret;
239 unsigned int i; 239 unsigned int i;
240 unsigned int tmp; 240 unsigned int tmp;
241 uint32_t x; 241 uint32_t x;
242 242
243 GNUNET_assert(n > 0); 243 GNUNET_assert (n > 0);
244 ret = GNUNET_malloc(n * sizeof(unsigned int)); 244 ret = GNUNET_malloc (n * sizeof(unsigned int));
245 for (i = 0; i < n; i++) 245 for (i = 0; i < n; i++)
246 ret[i] = i; 246 ret[i] = i;
247 for (i = n - 1; i > 0; i--) 247 for (i = n - 1; i > 0; i--)
248 { 248 {
249 x = GNUNET_CRYPTO_random_u32(mode, i + 1); 249 x = GNUNET_CRYPTO_random_u32 (mode, i + 1);
250 tmp = ret[x]; 250 tmp = ret[x];
251 ret[x] = ret[i]; 251 ret[x] = ret[i];
252 ret[i] = tmp; 252 ret[i] = tmp;
253 } 253 }
254 return ret; 254 return ret;
255} 255}
256 256
@@ -263,44 +263,44 @@ GNUNET_CRYPTO_random_permute(enum GNUNET_CRYPTO_Quality mode, unsigned int n)
263 * @return random 64-bit number 263 * @return random 64-bit number
264 */ 264 */
265uint64_t 265uint64_t
266GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max) 266GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
267{ 267{
268 uint64_t ret; 268 uint64_t ret;
269 uint64_t ul; 269 uint64_t ul;
270 270
271 GNUNET_assert(max > 0); 271 GNUNET_assert (max > 0);
272 switch (mode) 272 switch (mode)
273 {
274 case GNUNET_CRYPTO_QUALITY_STRONG:
275 ul = UINT64_MAX - (UINT64_MAX % max);
276 do
273 { 277 {
274 case GNUNET_CRYPTO_QUALITY_STRONG: 278 gcry_randomize ((unsigned char *) &ret,
275 ul = UINT64_MAX - (UINT64_MAX % max); 279 sizeof(uint64_t),
276 do 280 GCRY_STRONG_RANDOM);
277 {
278 gcry_randomize((unsigned char *)&ret,
279 sizeof(uint64_t),
280 GCRY_STRONG_RANDOM);
281 }
282 while (ret >= ul);
283 return ret % max;
284
285 case GNUNET_CRYPTO_QUALITY_NONCE:
286 ul = UINT64_MAX - (UINT64_MAX % max);
287 do
288 {
289 gcry_create_nonce(&ret, sizeof(ret));
290 }
291 while (ret >= ul);
292
293 return ret % max;
294
295 case GNUNET_CRYPTO_QUALITY_WEAK:
296 ret = max * get_weak_random();
297 if (ret >= max)
298 ret = max - 1;
299 return ret;
300
301 default:
302 GNUNET_assert(0);
303 } 281 }
282 while (ret >= ul);
283 return ret % max;
284
285 case GNUNET_CRYPTO_QUALITY_NONCE:
286 ul = UINT64_MAX - (UINT64_MAX % max);
287 do
288 {
289 gcry_create_nonce (&ret, sizeof(ret));
290 }
291 while (ret >= ul);
292
293 return ret % max;
294
295 case GNUNET_CRYPTO_QUALITY_WEAK:
296 ret = max * get_weak_random ();
297 if (ret >= max)
298 ret = max - 1;
299 return ret;
300
301 default:
302 GNUNET_assert (0);
303 }
304 return 0; 304 return 0;
305} 305}
306 306
@@ -310,9 +310,9 @@ GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
310 * strategy of libgcrypt implementation. 310 * strategy of libgcrypt implementation.
311 */ 311 */
312static void * 312static void *
313w_malloc(size_t n) 313w_malloc (size_t n)
314{ 314{
315 return calloc(n, 1); 315 return calloc (n, 1);
316} 316}
317 317
318 318
@@ -321,9 +321,9 @@ w_malloc(size_t n)
321 * strategy of libgcrypt implementation. 321 * strategy of libgcrypt implementation.
322 */ 322 */
323static int 323static int
324w_check(const void *p) 324w_check (const void *p)
325{ 325{
326 (void)p; 326 (void) p;
327 return 0; /* not secure memory */ 327 return 0; /* not secure memory */
328} 328}
329 329
@@ -331,49 +331,49 @@ w_check(const void *p)
331/** 331/**
332 * Initialize libgcrypt. 332 * Initialize libgcrypt.
333 */ 333 */
334void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init() 334void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
335{ 335{
336 gcry_error_t rc; 336 gcry_error_t rc;
337 337
338 if (!gcry_check_version(NEED_LIBGCRYPT_VERSION)) 338 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
339 { 339 {
340 fprintf( 340 fprintf (
341 stderr, 341 stderr,
342 _("libgcrypt has not the expected version (version %s is required).\n"), 342 _ ("libgcrypt has not the expected version (version %s is required).\n"),
343 NEED_LIBGCRYPT_VERSION); 343 NEED_LIBGCRYPT_VERSION);
344 GNUNET_assert(0); 344 GNUNET_assert (0);
345 } 345 }
346 /* set custom allocators */ 346 /* set custom allocators */
347 gcry_set_allocation_handler(&w_malloc, &w_malloc, &w_check, &realloc, &free); 347 gcry_set_allocation_handler (&w_malloc, &w_malloc, &w_check, &realloc, &free);
348 /* Disable use of secure memory */ 348 /* Disable use of secure memory */
349 if ((rc = gcry_control(GCRYCTL_DISABLE_SECMEM, 0))) 349 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
350 fprintf(stderr, 350 fprintf (stderr,
351 "Failed to set libgcrypt option %s: %s\n", 351 "Failed to set libgcrypt option %s: %s\n",
352 "DISABLE_SECMEM", 352 "DISABLE_SECMEM",
353 gcry_strerror(rc)); 353 gcry_strerror (rc));
354 /* Otherwise gnunet-ecc takes forever to complete, besides 354 /* Otherwise gnunet-ecc takes forever to complete, besides
355 we are fine with "just" using GCRY_STRONG_RANDOM */ 355 we are fine with "just" using GCRY_STRONG_RANDOM */
356 if ((rc = gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0))) 356 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
357 fprintf(stderr, 357 fprintf (stderr,
358 "Failed to set libgcrypt option %s: %s\n", 358 "Failed to set libgcrypt option %s: %s\n",
359 "ENABLE_QUICK_RANDOM", 359 "ENABLE_QUICK_RANDOM",
360 gcry_strerror(rc)); 360 gcry_strerror (rc));
361 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); 361 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
362 gcry_fast_random_poll(); 362 gcry_fast_random_poll ();
363 GNUNET_CRYPTO_seed_weak_random( 363 GNUNET_CRYPTO_seed_weak_random (
364 time(NULL) ^ 364 time (NULL)
365 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX)); 365 ^ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
366} 366}
367 367
368 368
369/** 369/**
370 * Nicely shut down libgcrypt. 370 * Nicely shut down libgcrypt.
371 */ 371 */
372void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini() 372void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini ()
373{ 373{
374 gcry_set_progress_handler(NULL, NULL); 374 gcry_set_progress_handler (NULL, NULL);
375#ifdef GCRYCTL_CLOSE_RANDOM_DEVICE 375#ifdef GCRYCTL_CLOSE_RANDOM_DEVICE
376 (void)gcry_control(GCRYCTL_CLOSE_RANDOM_DEVICE, 0); 376 (void) gcry_control (GCRYCTL_CLOSE_RANDOM_DEVICE, 0);
377#endif 377#endif
378} 378}
379 379
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index 0a20fdffa..edabcd6d5 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -30,13 +30,14 @@
30#include "gnunet_crypto_lib.h" 30#include "gnunet_crypto_lib.h"
31#include "benchmark.h" 31#include "benchmark.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-rsa", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-rsa", __VA_ARGS__)
34 34
35 35
36/** 36/**
37 * The private information of an RSA key pair. 37 * The private information of an RSA key pair.
38 */ 38 */
39struct GNUNET_CRYPTO_RsaPrivateKey { 39struct GNUNET_CRYPTO_RsaPrivateKey
40{
40 /** 41 /**
41 * Libgcrypt S-expression for the RSA private key. 42 * Libgcrypt S-expression for the RSA private key.
42 */ 43 */
@@ -47,7 +48,8 @@ struct GNUNET_CRYPTO_RsaPrivateKey {
47/** 48/**
48 * The public information of an RSA key pair. 49 * The public information of an RSA key pair.
49 */ 50 */
50struct GNUNET_CRYPTO_RsaPublicKey { 51struct GNUNET_CRYPTO_RsaPublicKey
52{
51 /** 53 /**
52 * Libgcrypt S-expression for the RSA public key. 54 * Libgcrypt S-expression for the RSA public key.
53 */ 55 */
@@ -58,7 +60,8 @@ struct GNUNET_CRYPTO_RsaPublicKey {
58/** 60/**
59 * @brief an RSA signature 61 * @brief an RSA signature
60 */ 62 */
61struct GNUNET_CRYPTO_RsaSignature { 63struct GNUNET_CRYPTO_RsaSignature
64{
62 /** 65 /**
63 * Libgcrypt S-expression for the RSA signature. 66 * Libgcrypt S-expression for the RSA signature.
64 */ 67 */
@@ -69,7 +72,8 @@ struct GNUNET_CRYPTO_RsaSignature {
69/** 72/**
70 * @brief RSA blinding key 73 * @brief RSA blinding key
71 */ 74 */
72struct RsaBlindingKey { 75struct RsaBlindingKey
76{
73 /** 77 /**
74 * Random value used for blinding. 78 * Random value used for blinding.
75 */ 79 */
@@ -87,50 +91,50 @@ struct RsaBlindingKey {
87 * @return 0 on success 91 * @return 0 on success
88 */ 92 */
89static int 93static int
90key_from_sexp(gcry_mpi_t *array, 94key_from_sexp (gcry_mpi_t *array,
91 gcry_sexp_t sexp, 95 gcry_sexp_t sexp,
92 const char *topname, 96 const char *topname,
93 const char *elems) 97 const char *elems)
94{ 98{
95 gcry_sexp_t list; 99 gcry_sexp_t list;
96 gcry_sexp_t l2; 100 gcry_sexp_t l2;
97 const char *s; 101 const char *s;
98 unsigned int idx; 102 unsigned int idx;
99 103
100 if (!(list = gcry_sexp_find_token(sexp, topname, 0))) 104 if (! (list = gcry_sexp_find_token (sexp, topname, 0)))
101 return 1; 105 return 1;
102 l2 = gcry_sexp_cadr(list); 106 l2 = gcry_sexp_cadr (list);
103 gcry_sexp_release(list); 107 gcry_sexp_release (list);
104 list = l2; 108 list = l2;
105 if (!list) 109 if (! list)
106 return 2; 110 return 2;
107 idx = 0; 111 idx = 0;
108 for (s = elems; *s; s++, idx++) 112 for (s = elems; *s; s++, idx++)
113 {
114 if (! (l2 = gcry_sexp_find_token (list, s, 1)))
109 { 115 {
110 if (!(l2 = gcry_sexp_find_token(list, s, 1))) 116 for (unsigned int i = 0; i < idx; i++)
111 { 117 {
112 for (unsigned int i = 0; i < idx; i++) 118 gcry_free (array[i]);
113 { 119 array[i] = NULL;
114 gcry_free(array[i]); 120 }
115 array[i] = NULL; 121 gcry_sexp_release (list);
116 } 122 return 3; /* required parameter not found */
117 gcry_sexp_release(list);
118 return 3; /* required parameter not found */
119 }
120 array[idx] = gcry_sexp_nth_mpi(l2, 1, GCRYMPI_FMT_USG);
121 gcry_sexp_release(l2);
122 if (!array[idx])
123 {
124 for (unsigned int i = 0; i < idx; i++)
125 {
126 gcry_free(array[i]);
127 array[i] = NULL;
128 }
129 gcry_sexp_release(list);
130 return 4; /* required parameter is invalid */
131 }
132 } 123 }
133 gcry_sexp_release(list); 124 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
125 gcry_sexp_release (l2);
126 if (! array[idx])
127 {
128 for (unsigned int i = 0; i < idx; i++)
129 {
130 gcry_free (array[i]);
131 array[i] = NULL;
132 }
133 gcry_sexp_release (list);
134 return 4; /* required parameter is invalid */
135 }
136 }
137 gcry_sexp_release (list);
134 return 0; 138 return 0;
135} 139}
136 140
@@ -142,30 +146,30 @@ key_from_sexp(gcry_mpi_t *array,
142 * @return fresh private key 146 * @return fresh private key
143 */ 147 */
144struct GNUNET_CRYPTO_RsaPrivateKey * 148struct GNUNET_CRYPTO_RsaPrivateKey *
145GNUNET_CRYPTO_rsa_private_key_create(unsigned int len) 149GNUNET_CRYPTO_rsa_private_key_create (unsigned int len)
146{ 150{
147 struct GNUNET_CRYPTO_RsaPrivateKey *ret; 151 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
148 gcry_sexp_t s_key; 152 gcry_sexp_t s_key;
149 gcry_sexp_t s_keyparam; 153 gcry_sexp_t s_keyparam;
150 154
151 BENCHMARK_START(rsa_private_key_create); 155 BENCHMARK_START (rsa_private_key_create);
152 156
153 GNUNET_assert(0 == 157 GNUNET_assert (0 ==
154 gcry_sexp_build(&s_keyparam, 158 gcry_sexp_build (&s_keyparam,
155 NULL, 159 NULL,
156 "(genkey(rsa(nbits %d)))", 160 "(genkey(rsa(nbits %d)))",
157 len)); 161 len));
158 GNUNET_assert(0 == 162 GNUNET_assert (0 ==
159 gcry_pk_genkey(&s_key, 163 gcry_pk_genkey (&s_key,
160 s_keyparam)); 164 s_keyparam));
161 gcry_sexp_release(s_keyparam); 165 gcry_sexp_release (s_keyparam);
162#if EXTRA_CHECKS 166#if EXTRA_CHECKS
163 GNUNET_assert(0 == 167 GNUNET_assert (0 ==
164 gcry_pk_testkey(s_key)); 168 gcry_pk_testkey (s_key));
165#endif 169#endif
166 ret = GNUNET_new(struct GNUNET_CRYPTO_RsaPrivateKey); 170 ret = GNUNET_new (struct GNUNET_CRYPTO_RsaPrivateKey);
167 ret->sexp = s_key; 171 ret->sexp = s_key;
168 BENCHMARK_END(rsa_private_key_create); 172 BENCHMARK_END (rsa_private_key_create);
169 return ret; 173 return ret;
170} 174}
171 175
@@ -176,10 +180,10 @@ GNUNET_CRYPTO_rsa_private_key_create(unsigned int len)
176 * @param key pointer to the memory to free 180 * @param key pointer to the memory to free
177 */ 181 */
178void 182void
179GNUNET_CRYPTO_rsa_private_key_free(struct GNUNET_CRYPTO_RsaPrivateKey *key) 183GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key)
180{ 184{
181 gcry_sexp_release(key->sexp); 185 gcry_sexp_release (key->sexp);
182 GNUNET_free(key); 186 GNUNET_free (key);
183} 187}
184 188
185 189
@@ -192,22 +196,23 @@ GNUNET_CRYPTO_rsa_private_key_free(struct GNUNET_CRYPTO_RsaPrivateKey *key)
192 * @return size of memory allocated in @a buffer 196 * @return size of memory allocated in @a buffer
193 */ 197 */
194size_t 198size_t
195GNUNET_CRYPTO_rsa_private_key_encode(const struct GNUNET_CRYPTO_RsaPrivateKey *key, 199GNUNET_CRYPTO_rsa_private_key_encode (const struct
196 char **buffer) 200 GNUNET_CRYPTO_RsaPrivateKey *key,
201 char **buffer)
197{ 202{
198 size_t n; 203 size_t n;
199 char *b; 204 char *b;
200 205
201 n = gcry_sexp_sprint(key->sexp, 206 n = gcry_sexp_sprint (key->sexp,
202 GCRYSEXP_FMT_DEFAULT, 207 GCRYSEXP_FMT_DEFAULT,
203 NULL, 208 NULL,
204 0); 209 0);
205 b = GNUNET_malloc(n); 210 b = GNUNET_malloc (n);
206 GNUNET_assert((n - 1) == /* since the last byte is \0 */ 211 GNUNET_assert ((n - 1) == /* since the last byte is \0 */
207 gcry_sexp_sprint(key->sexp, 212 gcry_sexp_sprint (key->sexp,
208 GCRYSEXP_FMT_DEFAULT, 213 GCRYSEXP_FMT_DEFAULT,
209 b, 214 b,
210 n)); 215 n));
211 *buffer = b; 216 *buffer = b;
212 return n; 217 return n;
213} 218}
@@ -222,30 +227,30 @@ GNUNET_CRYPTO_rsa_private_key_encode(const struct GNUNET_CRYPTO_RsaPrivateKey *k
222 * @return NULL on error 227 * @return NULL on error
223 */ 228 */
224struct GNUNET_CRYPTO_RsaPrivateKey * 229struct GNUNET_CRYPTO_RsaPrivateKey *
225GNUNET_CRYPTO_rsa_private_key_decode(const char *buf, 230GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
226 size_t len) 231 size_t len)
227{ 232{
228 struct GNUNET_CRYPTO_RsaPrivateKey *key; 233 struct GNUNET_CRYPTO_RsaPrivateKey *key;
229 234
230 key = GNUNET_new(struct GNUNET_CRYPTO_RsaPrivateKey); 235 key = GNUNET_new (struct GNUNET_CRYPTO_RsaPrivateKey);
231 if (0 != 236 if (0 !=
232 gcry_sexp_new(&key->sexp, 237 gcry_sexp_new (&key->sexp,
233 buf, 238 buf,
234 len, 239 len,
235 0)) 240 0))
236 { 241 {
237 LOG(GNUNET_ERROR_TYPE_WARNING, 242 LOG (GNUNET_ERROR_TYPE_WARNING,
238 "Decoded private key is not valid\n"); 243 "Decoded private key is not valid\n");
239 GNUNET_free(key); 244 GNUNET_free (key);
240 return NULL; 245 return NULL;
241 } 246 }
242 if (0 != gcry_pk_testkey(key->sexp)) 247 if (0 != gcry_pk_testkey (key->sexp))
243 { 248 {
244 LOG(GNUNET_ERROR_TYPE_WARNING, 249 LOG (GNUNET_ERROR_TYPE_WARNING,
245 "Decoded private key is not valid\n"); 250 "Decoded private key is not valid\n");
246 GNUNET_CRYPTO_rsa_private_key_free(key); 251 GNUNET_CRYPTO_rsa_private_key_free (key);
247 return NULL; 252 return NULL;
248 } 253 }
249 return key; 254 return key;
250} 255}
251 256
@@ -257,35 +262,36 @@ GNUNET_CRYPTO_rsa_private_key_decode(const char *buf,
257 * @retur NULL on error, otherwise the public key 262 * @retur NULL on error, otherwise the public key
258 */ 263 */
259struct GNUNET_CRYPTO_RsaPublicKey * 264struct GNUNET_CRYPTO_RsaPublicKey *
260GNUNET_CRYPTO_rsa_private_key_get_public(const struct GNUNET_CRYPTO_RsaPrivateKey *priv) 265GNUNET_CRYPTO_rsa_private_key_get_public (const struct
266 GNUNET_CRYPTO_RsaPrivateKey *priv)
261{ 267{
262 struct GNUNET_CRYPTO_RsaPublicKey *pub; 268 struct GNUNET_CRYPTO_RsaPublicKey *pub;
263 gcry_mpi_t ne[2]; 269 gcry_mpi_t ne[2];
264 int rc; 270 int rc;
265 gcry_sexp_t result; 271 gcry_sexp_t result;
266 272
267 BENCHMARK_START(rsa_private_key_get_public); 273 BENCHMARK_START (rsa_private_key_get_public);
268 274
269 rc = key_from_sexp(ne, priv->sexp, "public-key", "ne"); 275 rc = key_from_sexp (ne, priv->sexp, "public-key", "ne");
270 if (0 != rc) 276 if (0 != rc)
271 rc = key_from_sexp(ne, priv->sexp, "private-key", "ne"); 277 rc = key_from_sexp (ne, priv->sexp, "private-key", "ne");
272 if (0 != rc) 278 if (0 != rc)
273 rc = key_from_sexp(ne, priv->sexp, "rsa", "ne"); 279 rc = key_from_sexp (ne, priv->sexp, "rsa", "ne");
274 if (0 != rc) 280 if (0 != rc)
275 { 281 {
276 GNUNET_break_op(0); 282 GNUNET_break_op (0);
277 return NULL; 283 return NULL;
278 } 284 }
279 rc = gcry_sexp_build(&result, 285 rc = gcry_sexp_build (&result,
280 NULL, 286 NULL,
281 "(public-key(rsa(n %m)(e %m)))", 287 "(public-key(rsa(n %m)(e %m)))",
282 ne[0], 288 ne[0],
283 ne[1]); 289 ne[1]);
284 gcry_mpi_release(ne[0]); 290 gcry_mpi_release (ne[0]);
285 gcry_mpi_release(ne[1]); 291 gcry_mpi_release (ne[1]);
286 pub = GNUNET_new(struct GNUNET_CRYPTO_RsaPublicKey); 292 pub = GNUNET_new (struct GNUNET_CRYPTO_RsaPublicKey);
287 pub->sexp = result; 293 pub->sexp = result;
288 BENCHMARK_END(rsa_private_key_get_public); 294 BENCHMARK_END (rsa_private_key_get_public);
289 return pub; 295 return pub;
290} 296}
291 297
@@ -296,10 +302,10 @@ GNUNET_CRYPTO_rsa_private_key_get_public(const struct GNUNET_CRYPTO_RsaPrivateKe
296 * @param key pointer to the memory to free 302 * @param key pointer to the memory to free
297 */ 303 */
298void 304void
299GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key) 305GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key)
300{ 306{
301 gcry_sexp_release(key->sexp); 307 gcry_sexp_release (key->sexp);
302 GNUNET_free(key); 308 GNUNET_free (key);
303} 309}
304 310
305 311
@@ -312,22 +318,23 @@ GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
312 * @return size of memory allocated in @a buffer 318 * @return size of memory allocated in @a buffer
313 */ 319 */
314size_t 320size_t
315GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, 321GNUNET_CRYPTO_rsa_public_key_encode (const struct
316 char **buffer) 322 GNUNET_CRYPTO_RsaPublicKey *key,
323 char **buffer)
317{ 324{
318 size_t n; 325 size_t n;
319 char *b; 326 char *b;
320 327
321 n = gcry_sexp_sprint(key->sexp, 328 n = gcry_sexp_sprint (key->sexp,
322 GCRYSEXP_FMT_ADVANCED, 329 GCRYSEXP_FMT_ADVANCED,
323 NULL, 330 NULL,
324 0); 331 0);
325 b = GNUNET_malloc(n); 332 b = GNUNET_malloc (n);
326 GNUNET_assert((n - 1) == /* since the last byte is \0 */ 333 GNUNET_assert ((n - 1) == /* since the last byte is \0 */
327 gcry_sexp_sprint(key->sexp, 334 gcry_sexp_sprint (key->sexp,
328 GCRYSEXP_FMT_ADVANCED, 335 GCRYSEXP_FMT_ADVANCED,
329 b, 336 b,
330 n)); 337 n));
331 *buffer = b; 338 *buffer = b;
332 return n; 339 return n;
333} 340}
@@ -340,18 +347,18 @@ GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key
340 * @param hc where to store the hash code 347 * @param hc where to store the hash code
341 */ 348 */
342void 349void
343GNUNET_CRYPTO_rsa_public_key_hash(const struct GNUNET_CRYPTO_RsaPublicKey *key, 350GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
344 struct GNUNET_HashCode *hc) 351 struct GNUNET_HashCode *hc)
345{ 352{
346 char *buf; 353 char *buf;
347 size_t buf_size; 354 size_t buf_size;
348 355
349 buf_size = GNUNET_CRYPTO_rsa_public_key_encode(key, 356 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (key,
350 &buf); 357 &buf);
351 GNUNET_CRYPTO_hash(buf, 358 GNUNET_CRYPTO_hash (buf,
352 buf_size, 359 buf_size,
353 hc); 360 hc);
354 GNUNET_free(buf); 361 GNUNET_free (buf);
355} 362}
356 363
357 364
@@ -364,37 +371,37 @@ GNUNET_CRYPTO_rsa_public_key_hash(const struct GNUNET_CRYPTO_RsaPublicKey *key,
364 * @return NULL on error 371 * @return NULL on error
365 */ 372 */
366struct GNUNET_CRYPTO_RsaPublicKey * 373struct GNUNET_CRYPTO_RsaPublicKey *
367GNUNET_CRYPTO_rsa_public_key_decode(const char *buf, 374GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
368 size_t len) 375 size_t len)
369{ 376{
370 struct GNUNET_CRYPTO_RsaPublicKey *key; 377 struct GNUNET_CRYPTO_RsaPublicKey *key;
371 gcry_mpi_t n; 378 gcry_mpi_t n;
372 int ret; 379 int ret;
373 380
374 key = GNUNET_new(struct GNUNET_CRYPTO_RsaPublicKey); 381 key = GNUNET_new (struct GNUNET_CRYPTO_RsaPublicKey);
375 if (0 != 382 if (0 !=
376 gcry_sexp_new(&key->sexp, 383 gcry_sexp_new (&key->sexp,
377 buf, 384 buf,
378 len, 385 len,
379 0)) 386 0))
380 { 387 {
381 GNUNET_break_op(0); 388 GNUNET_break_op (0);
382 GNUNET_free(key); 389 GNUNET_free (key);
383 return NULL; 390 return NULL;
384 } 391 }
385 /* verify that this is an RSA public key */ 392 /* verify that this is an RSA public key */
386 ret = key_from_sexp(&n, key->sexp, "public-key", "n"); 393 ret = key_from_sexp (&n, key->sexp, "public-key", "n");
387 if (0 != ret) 394 if (0 != ret)
388 ret = key_from_sexp(&n, key->sexp, "rsa", "n"); 395 ret = key_from_sexp (&n, key->sexp, "rsa", "n");
389 if (0 != ret) 396 if (0 != ret)
390 { 397 {
391 /* this is no public RSA key */ 398 /* this is no public RSA key */
392 GNUNET_break(0); 399 GNUNET_break (0);
393 gcry_sexp_release(key->sexp); 400 gcry_sexp_release (key->sexp);
394 GNUNET_free(key); 401 GNUNET_free (key);
395 return NULL; 402 return NULL;
396 } 403 }
397 gcry_mpi_release(n); 404 gcry_mpi_release (n);
398 return key; 405 return key;
399} 406}
400 407
@@ -411,14 +418,14 @@ GNUNET_CRYPTO_rsa_public_key_decode(const char *buf,
411 * @return True if gcd(r,n) = 1, False means RSA key is malicious 418 * @return True if gcd(r,n) = 1, False means RSA key is malicious
412 */ 419 */
413static int 420static int
414rsa_gcd_validate(gcry_mpi_t r, gcry_mpi_t n) 421rsa_gcd_validate (gcry_mpi_t r, gcry_mpi_t n)
415{ 422{
416 gcry_mpi_t g; 423 gcry_mpi_t g;
417 int t; 424 int t;
418 425
419 g = gcry_mpi_new(0); 426 g = gcry_mpi_new (0);
420 t = gcry_mpi_gcd(g, r, n); 427 t = gcry_mpi_gcd (g, r, n);
421 gcry_mpi_release(g); 428 gcry_mpi_release (g);
422 return t; 429 return t;
423} 430}
424 431
@@ -431,33 +438,33 @@ rsa_gcd_validate(gcry_mpi_t r, gcry_mpi_t n)
431 * @return the newly created blinding key, NULL if RSA key is malicious 438 * @return the newly created blinding key, NULL if RSA key is malicious
432 */ 439 */
433static struct RsaBlindingKey * 440static struct RsaBlindingKey *
434rsa_blinding_key_derive(const struct GNUNET_CRYPTO_RsaPublicKey *pkey, 441rsa_blinding_key_derive (const struct GNUNET_CRYPTO_RsaPublicKey *pkey,
435 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks) 442 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks)
436{ 443{
437 char *xts = "Blinding KDF extrator HMAC key"; /* Trusts bks' randomness more */ 444 char *xts = "Blinding KDF extrator HMAC key"; /* Trusts bks' randomness more */
438 struct RsaBlindingKey *blind; 445 struct RsaBlindingKey *blind;
439 gcry_mpi_t n; 446 gcry_mpi_t n;
440 447
441 blind = GNUNET_new(struct RsaBlindingKey); 448 blind = GNUNET_new (struct RsaBlindingKey);
442 GNUNET_assert(NULL != blind); 449 GNUNET_assert (NULL != blind);
443 450
444 /* Extract the composite n from the RSA public key */ 451 /* Extract the composite n from the RSA public key */
445 GNUNET_assert(0 == key_from_sexp(&n, pkey->sexp, "rsa", "n")); 452 GNUNET_assert (0 == key_from_sexp (&n, pkey->sexp, "rsa", "n"));
446 /* Assert that it at least looks like an RSA key */ 453 /* Assert that it at least looks like an RSA key */
447 GNUNET_assert(0 == gcry_mpi_get_flag(n, GCRYMPI_FLAG_OPAQUE)); 454 GNUNET_assert (0 == gcry_mpi_get_flag (n, GCRYMPI_FLAG_OPAQUE));
448 455
449 GNUNET_CRYPTO_kdf_mod_mpi(&blind->r, 456 GNUNET_CRYPTO_kdf_mod_mpi (&blind->r,
450 n, 457 n,
451 xts, strlen(xts), 458 xts, strlen (xts),
452 bks, sizeof(*bks), 459 bks, sizeof(*bks),
453 "Blinding KDF"); 460 "Blinding KDF");
454 if (0 == rsa_gcd_validate(blind->r, n)) 461 if (0 == rsa_gcd_validate (blind->r, n))
455 { 462 {
456 GNUNET_free(blind); 463 GNUNET_free (blind);
457 blind = NULL; 464 blind = NULL;
458 } 465 }
459 466
460 gcry_mpi_release(n); 467 gcry_mpi_release (n);
461 return blind; 468 return blind;
462} 469}
463 470
@@ -513,8 +520,8 @@ rsa_blinding_key_derive(const struct GNUNET_CRYPTO_RsaPublicKey *pkey,
513 * @return 0 if the two are equal 520 * @return 0 if the two are equal
514 */ 521 */
515int 522int
516GNUNET_CRYPTO_rsa_signature_cmp(struct GNUNET_CRYPTO_RsaSignature *s1, 523GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
517 struct GNUNET_CRYPTO_RsaSignature *s2) 524 struct GNUNET_CRYPTO_RsaSignature *s2)
518{ 525{
519 char *b1; 526 char *b1;
520 char *b2; 527 char *b2;
@@ -522,18 +529,18 @@ GNUNET_CRYPTO_rsa_signature_cmp(struct GNUNET_CRYPTO_RsaSignature *s1,
522 size_t z2; 529 size_t z2;
523 int ret; 530 int ret;
524 531
525 z1 = GNUNET_CRYPTO_rsa_signature_encode(s1, 532 z1 = GNUNET_CRYPTO_rsa_signature_encode (s1,
526 &b1); 533 &b1);
527 z2 = GNUNET_CRYPTO_rsa_signature_encode(s2, 534 z2 = GNUNET_CRYPTO_rsa_signature_encode (s2,
528 &b2); 535 &b2);
529 if (z1 != z2) 536 if (z1 != z2)
530 ret = 1; 537 ret = 1;
531 else 538 else
532 ret = memcmp(b1, 539 ret = memcmp (b1,
533 b2, 540 b2,
534 z1); 541 z1);
535 GNUNET_free(b1); 542 GNUNET_free (b1);
536 GNUNET_free(b2); 543 GNUNET_free (b2);
537 return ret; 544 return ret;
538} 545}
539 546
@@ -546,8 +553,8 @@ GNUNET_CRYPTO_rsa_signature_cmp(struct GNUNET_CRYPTO_RsaSignature *s1,
546 * @return 0 if the two are equal 553 * @return 0 if the two are equal
547 */ 554 */
548int 555int
549GNUNET_CRYPTO_rsa_public_key_cmp(struct GNUNET_CRYPTO_RsaPublicKey *p1, 556GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
550 struct GNUNET_CRYPTO_RsaPublicKey *p2) 557 struct GNUNET_CRYPTO_RsaPublicKey *p2)
551{ 558{
552 char *b1; 559 char *b1;
553 char *b2; 560 char *b2;
@@ -555,18 +562,18 @@ GNUNET_CRYPTO_rsa_public_key_cmp(struct GNUNET_CRYPTO_RsaPublicKey *p1,
555 size_t z2; 562 size_t z2;
556 int ret; 563 int ret;
557 564
558 z1 = GNUNET_CRYPTO_rsa_public_key_encode(p1, 565 z1 = GNUNET_CRYPTO_rsa_public_key_encode (p1,
559 &b1); 566 &b1);
560 z2 = GNUNET_CRYPTO_rsa_public_key_encode(p2, 567 z2 = GNUNET_CRYPTO_rsa_public_key_encode (p2,
561 &b2); 568 &b2);
562 if (z1 != z2) 569 if (z1 != z2)
563 ret = 1; 570 ret = 1;
564 else 571 else
565 ret = memcmp(b1, 572 ret = memcmp (b1,
566 b2, 573 b2,
567 z1); 574 z1);
568 GNUNET_free(b1); 575 GNUNET_free (b1);
569 GNUNET_free(b2); 576 GNUNET_free (b2);
570 return ret; 577 return ret;
571} 578}
572 579
@@ -579,8 +586,8 @@ GNUNET_CRYPTO_rsa_public_key_cmp(struct GNUNET_CRYPTO_RsaPublicKey *p1,
579 * @return 0 if the two are equal 586 * @return 0 if the two are equal
580 */ 587 */
581int 588int
582GNUNET_CRYPTO_rsa_private_key_cmp(struct GNUNET_CRYPTO_RsaPrivateKey *p1, 589GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
583 struct GNUNET_CRYPTO_RsaPrivateKey *p2) 590 struct GNUNET_CRYPTO_RsaPrivateKey *p2)
584{ 591{
585 char *b1; 592 char *b1;
586 char *b2; 593 char *b2;
@@ -588,18 +595,18 @@ GNUNET_CRYPTO_rsa_private_key_cmp(struct GNUNET_CRYPTO_RsaPrivateKey *p1,
588 size_t z2; 595 size_t z2;
589 int ret; 596 int ret;
590 597
591 z1 = GNUNET_CRYPTO_rsa_private_key_encode(p1, 598 z1 = GNUNET_CRYPTO_rsa_private_key_encode (p1,
592 &b1); 599 &b1);
593 z2 = GNUNET_CRYPTO_rsa_private_key_encode(p2, 600 z2 = GNUNET_CRYPTO_rsa_private_key_encode (p2,
594 &b2); 601 &b2);
595 if (z1 != z2) 602 if (z1 != z2)
596 ret = 1; 603 ret = 1;
597 else 604 else
598 ret = memcmp(b1, 605 ret = memcmp (b1,
599 b2, 606 b2,
600 z1); 607 z1);
601 GNUNET_free(b1); 608 GNUNET_free (b1);
602 GNUNET_free(b2); 609 GNUNET_free (b2);
603 return ret; 610 return ret;
604} 611}
605 612
@@ -611,18 +618,18 @@ GNUNET_CRYPTO_rsa_private_key_cmp(struct GNUNET_CRYPTO_RsaPrivateKey *p1,
611 * @return length of the key in bits 618 * @return length of the key in bits
612 */ 619 */
613unsigned int 620unsigned int
614GNUNET_CRYPTO_rsa_public_key_len(const struct GNUNET_CRYPTO_RsaPublicKey *key) 621GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key)
615{ 622{
616 gcry_mpi_t n; 623 gcry_mpi_t n;
617 unsigned int rval; 624 unsigned int rval;
618 625
619 if (0 != key_from_sexp(&n, key->sexp, "rsa", "n")) 626 if (0 != key_from_sexp (&n, key->sexp, "rsa", "n"))
620 { /* Not an RSA public key */ 627 { /* Not an RSA public key */
621 GNUNET_break(0); 628 GNUNET_break (0);
622 return 0; 629 return 0;
623 } 630 }
624 rval = gcry_mpi_get_nbits(n); 631 rval = gcry_mpi_get_nbits (n);
625 gcry_mpi_release(n); 632 gcry_mpi_release (n);
626 return rval; 633 return rval;
627} 634}
628 635
@@ -633,10 +640,10 @@ GNUNET_CRYPTO_rsa_public_key_len(const struct GNUNET_CRYPTO_RsaPublicKey *key)
633 * @param bkey the blinding key to destroy 640 * @param bkey the blinding key to destroy
634 */ 641 */
635static void 642static void
636rsa_blinding_key_free(struct RsaBlindingKey *bkey) 643rsa_blinding_key_free (struct RsaBlindingKey *bkey)
637{ 644{
638 gcry_mpi_release(bkey->r); 645 gcry_mpi_release (bkey->r);
639 GNUNET_free(bkey); 646 GNUNET_free (bkey);
640} 647}
641 648
642 649
@@ -648,25 +655,25 @@ rsa_blinding_key_free(struct RsaBlindingKey *bkey)
648 * @return number of bytes stored in @a buffer 655 * @return number of bytes stored in @a buffer
649 */ 656 */
650static size_t 657static size_t
651numeric_mpi_alloc_n_print(gcry_mpi_t v, 658numeric_mpi_alloc_n_print (gcry_mpi_t v,
652 char **buffer) 659 char **buffer)
653{ 660{
654 size_t n; 661 size_t n;
655 char *b; 662 char *b;
656 size_t rsize; 663 size_t rsize;
657 664
658 gcry_mpi_print(GCRYMPI_FMT_USG, 665 gcry_mpi_print (GCRYMPI_FMT_USG,
659 NULL, 666 NULL,
660 0, 667 0,
661 &n, 668 &n,
662 v); 669 v);
663 b = GNUNET_malloc(n); 670 b = GNUNET_malloc (n);
664 GNUNET_assert(0 == 671 GNUNET_assert (0 ==
665 gcry_mpi_print(GCRYMPI_FMT_USG, 672 gcry_mpi_print (GCRYMPI_FMT_USG,
666 (unsigned char *)b, 673 (unsigned char *) b,
667 n, 674 n,
668 &rsize, 675 &rsize,
669 v)); 676 v));
670 *buffer = b; 677 *buffer = b;
671 return n; 678 return n;
672} 679}
@@ -685,8 +692,8 @@ numeric_mpi_alloc_n_print(gcry_mpi_t v,
685 * @return MPI value set to the FDH, NULL if RSA key is malicious 692 * @return MPI value set to the FDH, NULL if RSA key is malicious
686 */ 693 */
687static gcry_mpi_t 694static gcry_mpi_t
688rsa_full_domain_hash(const struct GNUNET_CRYPTO_RsaPublicKey *pkey, 695rsa_full_domain_hash (const struct GNUNET_CRYPTO_RsaPublicKey *pkey,
689 const struct GNUNET_HashCode *hash) 696 const struct GNUNET_HashCode *hash)
690{ 697{
691 gcry_mpi_t r, n; 698 gcry_mpi_t r, n;
692 char *xts; 699 char *xts;
@@ -694,28 +701,28 @@ rsa_full_domain_hash(const struct GNUNET_CRYPTO_RsaPublicKey *pkey,
694 int ok; 701 int ok;
695 702
696 /* Extract the composite n from the RSA public key */ 703 /* Extract the composite n from the RSA public key */
697 GNUNET_assert(0 == key_from_sexp(&n, pkey->sexp, "rsa", "n")); 704 GNUNET_assert (0 == key_from_sexp (&n, pkey->sexp, "rsa", "n"));
698 /* Assert that it at least looks like an RSA key */ 705 /* Assert that it at least looks like an RSA key */
699 GNUNET_assert(0 == gcry_mpi_get_flag(n, GCRYMPI_FLAG_OPAQUE)); 706 GNUNET_assert (0 == gcry_mpi_get_flag (n, GCRYMPI_FLAG_OPAQUE));
700 707
701 /* We key with the public denomination key as a homage to RSA-PSS by * 708 /* We key with the public denomination key as a homage to RSA-PSS by *
702 * Mihir Bellare and Phillip Rogaway. Doing this lowers the degree * 709 * Mihir Bellare and Phillip Rogaway. Doing this lowers the degree *
703 * of the hypothetical polyomial-time attack on RSA-KTI created by a * 710 * of the hypothetical polyomial-time attack on RSA-KTI created by a *
704 * polynomial-time one-more forgary attack. Yey seeding! */ 711 * polynomial-time one-more forgary attack. Yey seeding! */
705 xts_len = GNUNET_CRYPTO_rsa_public_key_encode(pkey, &xts); 712 xts_len = GNUNET_CRYPTO_rsa_public_key_encode (pkey, &xts);
706 713
707 GNUNET_CRYPTO_kdf_mod_mpi(&r, 714 GNUNET_CRYPTO_kdf_mod_mpi (&r,
708 n, 715 n,
709 xts, xts_len, 716 xts, xts_len,
710 hash, sizeof(*hash), 717 hash, sizeof(*hash),
711 "RSA-FDA FTpsW!"); 718 "RSA-FDA FTpsW!");
712 GNUNET_free(xts); 719 GNUNET_free (xts);
713 720
714 ok = rsa_gcd_validate(r, n); 721 ok = rsa_gcd_validate (r, n);
715 gcry_mpi_release(n); 722 gcry_mpi_release (n);
716 if (ok) 723 if (ok)
717 return r; 724 return r;
718 gcry_mpi_release(r); 725 gcry_mpi_release (r);
719 return NULL; 726 return NULL;
720} 727}
721 728
@@ -731,10 +738,10 @@ rsa_full_domain_hash(const struct GNUNET_CRYPTO_RsaPublicKey *pkey,
731 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious 738 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
732 */ 739 */
733int 740int
734GNUNET_CRYPTO_rsa_blind(const struct GNUNET_HashCode *hash, 741GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
735 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks, 742 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
736 struct GNUNET_CRYPTO_RsaPublicKey *pkey, 743 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
737 char **buf, size_t *buf_size) 744 char **buf, size_t *buf_size)
738{ 745{
739 struct RsaBlindingKey *bkey; 746 struct RsaBlindingKey *bkey;
740 gcry_mpi_t data; 747 gcry_mpi_t data;
@@ -743,59 +750,59 @@ GNUNET_CRYPTO_rsa_blind(const struct GNUNET_HashCode *hash,
743 gcry_mpi_t data_r_e; 750 gcry_mpi_t data_r_e;
744 int ret; 751 int ret;
745 752
746 BENCHMARK_START(rsa_blind); 753 BENCHMARK_START (rsa_blind);
747 754
748 GNUNET_assert(buf != NULL && buf_size != NULL); 755 GNUNET_assert (buf != NULL && buf_size != NULL);
749 ret = key_from_sexp(ne, pkey->sexp, "public-key", "ne"); 756 ret = key_from_sexp (ne, pkey->sexp, "public-key", "ne");
750 if (0 != ret) 757 if (0 != ret)
751 ret = key_from_sexp(ne, pkey->sexp, "rsa", "ne"); 758 ret = key_from_sexp (ne, pkey->sexp, "rsa", "ne");
752 if (0 != ret) 759 if (0 != ret)
753 { 760 {
754 GNUNET_break(0); 761 GNUNET_break (0);
755 *buf = NULL; 762 *buf = NULL;
756 *buf_size = 0; 763 *buf_size = 0;
757 return 0; 764 return 0;
758 } 765 }
759 766
760 data = rsa_full_domain_hash(pkey, hash); 767 data = rsa_full_domain_hash (pkey, hash);
761 if (NULL == data) 768 if (NULL == data)
762 goto rsa_gcd_validate_failure; 769 goto rsa_gcd_validate_failure;
763 770
764 bkey = rsa_blinding_key_derive(pkey, bks); 771 bkey = rsa_blinding_key_derive (pkey, bks);
765 if (NULL == bkey) 772 if (NULL == bkey)
766 { 773 {
767 gcry_mpi_release(data); 774 gcry_mpi_release (data);
768 goto rsa_gcd_validate_failure; 775 goto rsa_gcd_validate_failure;
769 } 776 }
770 777
771 r_e = gcry_mpi_new(0); 778 r_e = gcry_mpi_new (0);
772 gcry_mpi_powm(r_e, 779 gcry_mpi_powm (r_e,
773 bkey->r, 780 bkey->r,
774 ne[1], 781 ne[1],
775 ne[0]); 782 ne[0]);
776 data_r_e = gcry_mpi_new(0); 783 data_r_e = gcry_mpi_new (0);
777 gcry_mpi_mulm(data_r_e, 784 gcry_mpi_mulm (data_r_e,
778 data, 785 data,
779 r_e, 786 r_e,
780 ne[0]); 787 ne[0]);
781 gcry_mpi_release(data); 788 gcry_mpi_release (data);
782 gcry_mpi_release(ne[0]); 789 gcry_mpi_release (ne[0]);
783 gcry_mpi_release(ne[1]); 790 gcry_mpi_release (ne[1]);
784 gcry_mpi_release(r_e); 791 gcry_mpi_release (r_e);
785 rsa_blinding_key_free(bkey); 792 rsa_blinding_key_free (bkey);
786 793
787 *buf_size = numeric_mpi_alloc_n_print(data_r_e, buf); 794 *buf_size = numeric_mpi_alloc_n_print (data_r_e, buf);
788 gcry_mpi_release(data_r_e); 795 gcry_mpi_release (data_r_e);
789 796
790 BENCHMARK_END(rsa_blind); 797 BENCHMARK_END (rsa_blind);
791 798
792 return GNUNET_YES; 799 return GNUNET_YES;
793 800
794rsa_gcd_validate_failure: 801rsa_gcd_validate_failure:
795 /* We know the RSA key is malicious here, so warn the wallet. */ 802 /* We know the RSA key is malicious here, so warn the wallet. */
796 /* GNUNET_break_op (0); */ 803 /* GNUNET_break_op (0); */
797 gcry_mpi_release(ne[0]); 804 gcry_mpi_release (ne[0]);
798 gcry_mpi_release(ne[1]); 805 gcry_mpi_release (ne[1]);
799 *buf = NULL; 806 *buf = NULL;
800 *buf_size = 0; 807 *buf_size = 0;
801 return GNUNET_NO; 808 return GNUNET_NO;
@@ -809,15 +816,15 @@ rsa_gcd_validate_failure:
809 * @return converted s-expression 816 * @return converted s-expression
810 */ 817 */
811static gcry_sexp_t 818static gcry_sexp_t
812mpi_to_sexp(gcry_mpi_t value) 819mpi_to_sexp (gcry_mpi_t value)
813{ 820{
814 gcry_sexp_t data = NULL; 821 gcry_sexp_t data = NULL;
815 822
816 GNUNET_assert(0 == 823 GNUNET_assert (0 ==
817 gcry_sexp_build(&data, 824 gcry_sexp_build (&data,
818 NULL, 825 NULL,
819 "(data (flags raw) (value %M))", 826 "(data (flags raw) (value %M))",
820 value)); 827 value));
821 return data; 828 return data;
822} 829}
823 830
@@ -830,53 +837,54 @@ mpi_to_sexp(gcry_mpi_t value)
830 * @return NULL on error, signature on success 837 * @return NULL on error, signature on success
831 */ 838 */
832static struct GNUNET_CRYPTO_RsaSignature * 839static struct GNUNET_CRYPTO_RsaSignature *
833rsa_sign_mpi(const struct GNUNET_CRYPTO_RsaPrivateKey *key, 840rsa_sign_mpi (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
834 gcry_mpi_t value) 841 gcry_mpi_t value)
835{ 842{
836 struct GNUNET_CRYPTO_RsaSignature *sig; 843 struct GNUNET_CRYPTO_RsaSignature *sig;
837 gcry_sexp_t data; 844 gcry_sexp_t data;
838 gcry_sexp_t result; 845 gcry_sexp_t result;
839 int rc; 846 int rc;
840 847
841 data = mpi_to_sexp(value); 848 data = mpi_to_sexp (value);
842 849
843 if (0 != 850 if (0 !=
844 (rc = gcry_pk_sign(&result, 851 (rc = gcry_pk_sign (&result,
845 data, 852 data,
846 key->sexp))) 853 key->sexp)))
847 { 854 {
848 LOG(GNUNET_ERROR_TYPE_WARNING, 855 LOG (GNUNET_ERROR_TYPE_WARNING,
849 _("RSA signing failed at %s:%d: %s\n"), 856 _ ("RSA signing failed at %s:%d: %s\n"),
850 __FILE__, 857 __FILE__,
851 __LINE__, 858 __LINE__,
852 gcry_strerror(rc)); 859 gcry_strerror (rc));
853 GNUNET_break(0); 860 GNUNET_break (0);
854 return NULL; 861 return NULL;
855 } 862 }
856 863
857 /* Lenstra protection was first added to libgcrypt 1.6.4 864 /* Lenstra protection was first added to libgcrypt 1.6.4
858 * with commit c17f84bd02d7ee93845e92e20f6ddba814961588. 865 * with commit c17f84bd02d7ee93845e92e20f6ddba814961588.
859 */ 866 */
860#if GCRYPT_VERSION_NUMBER < 0x010604 867#if GCRYPT_VERSION_NUMBER < 0x010604
861 /* verify signature (guards against Lenstra's attack with fault injection...) */ 868 /* verify signature (guards against Lenstra's attack with fault injection...) */
862 struct GNUNET_CRYPTO_RsaPublicKey *public_key = GNUNET_CRYPTO_rsa_private_key_get_public(key); 869 struct GNUNET_CRYPTO_RsaPublicKey *public_key =
870 GNUNET_CRYPTO_rsa_private_key_get_public (key);
863 if (0 != 871 if (0 !=
864 gcry_pk_verify(result, 872 gcry_pk_verify (result,
865 data, 873 data,
866 public_key->sexp)) 874 public_key->sexp))
867 { 875 {
868 GNUNET_break(0); 876 GNUNET_break (0);
869 GNUNET_CRYPTO_rsa_public_key_free(public_key); 877 GNUNET_CRYPTO_rsa_public_key_free (public_key);
870 gcry_sexp_release(data); 878 gcry_sexp_release (data);
871 gcry_sexp_release(result); 879 gcry_sexp_release (result);
872 return NULL; 880 return NULL;
873 } 881 }
874 GNUNET_CRYPTO_rsa_public_key_free(public_key); 882 GNUNET_CRYPTO_rsa_public_key_free (public_key);
875#endif 883#endif
876 884
877 /* return signature */ 885 /* return signature */
878 gcry_sexp_release(data); 886 gcry_sexp_release (data);
879 sig = GNUNET_new(struct GNUNET_CRYPTO_RsaSignature); 887 sig = GNUNET_new (struct GNUNET_CRYPTO_RsaSignature);
880 sig->sexp = result; 888 sig->sexp = result;
881 return sig; 889 return sig;
882} 890}
@@ -891,25 +899,25 @@ rsa_sign_mpi(const struct GNUNET_CRYPTO_RsaPrivateKey *key,
891 * @return NULL on error, signature on success 899 * @return NULL on error, signature on success
892 */ 900 */
893struct GNUNET_CRYPTO_RsaSignature * 901struct GNUNET_CRYPTO_RsaSignature *
894GNUNET_CRYPTO_rsa_sign_blinded(const struct GNUNET_CRYPTO_RsaPrivateKey *key, 902GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
895 const void *msg, 903 const void *msg,
896 size_t msg_len) 904 size_t msg_len)
897{ 905{
898 gcry_mpi_t v = NULL; 906 gcry_mpi_t v = NULL;
899 struct GNUNET_CRYPTO_RsaSignature *sig; 907 struct GNUNET_CRYPTO_RsaSignature *sig;
900 908
901 BENCHMARK_START(rsa_sign_blinded); 909 BENCHMARK_START (rsa_sign_blinded);
902 910
903 GNUNET_assert(0 == 911 GNUNET_assert (0 ==
904 gcry_mpi_scan(&v, 912 gcry_mpi_scan (&v,
905 GCRYMPI_FMT_USG, 913 GCRYMPI_FMT_USG,
906 msg, 914 msg,
907 msg_len, 915 msg_len,
908 NULL)); 916 NULL));
909 917
910 sig = rsa_sign_mpi(key, v); 918 sig = rsa_sign_mpi (key, v);
911 gcry_mpi_release(v); 919 gcry_mpi_release (v);
912 BENCHMARK_END(rsa_sign_blinded); 920 BENCHMARK_END (rsa_sign_blinded);
913 return sig; 921 return sig;
914} 922}
915 923
@@ -922,21 +930,21 @@ GNUNET_CRYPTO_rsa_sign_blinded(const struct GNUNET_CRYPTO_RsaPrivateKey *key,
922 * @return NULL on error, including a malicious RSA key, signature on success 930 * @return NULL on error, including a malicious RSA key, signature on success
923 */ 931 */
924struct GNUNET_CRYPTO_RsaSignature * 932struct GNUNET_CRYPTO_RsaSignature *
925GNUNET_CRYPTO_rsa_sign_fdh(const struct GNUNET_CRYPTO_RsaPrivateKey *key, 933GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
926 const struct GNUNET_HashCode *hash) 934 const struct GNUNET_HashCode *hash)
927{ 935{
928 struct GNUNET_CRYPTO_RsaPublicKey *pkey; 936 struct GNUNET_CRYPTO_RsaPublicKey *pkey;
929 gcry_mpi_t v = NULL; 937 gcry_mpi_t v = NULL;
930 struct GNUNET_CRYPTO_RsaSignature *sig; 938 struct GNUNET_CRYPTO_RsaSignature *sig;
931 939
932 pkey = GNUNET_CRYPTO_rsa_private_key_get_public(key); 940 pkey = GNUNET_CRYPTO_rsa_private_key_get_public (key);
933 v = rsa_full_domain_hash(pkey, hash); 941 v = rsa_full_domain_hash (pkey, hash);
934 GNUNET_CRYPTO_rsa_public_key_free(pkey); 942 GNUNET_CRYPTO_rsa_public_key_free (pkey);
935 if (NULL == v) /* rsa_gcd_validate failed meaning */ 943 if (NULL == v) /* rsa_gcd_validate failed meaning */
936 return NULL; /* our *own* RSA key is malicious. */ 944 return NULL; /* our *own* RSA key is malicious. */
937 945
938 sig = rsa_sign_mpi(key, v); 946 sig = rsa_sign_mpi (key, v);
939 gcry_mpi_release(v); 947 gcry_mpi_release (v);
940 return sig; 948 return sig;
941} 949}
942 950
@@ -947,10 +955,10 @@ GNUNET_CRYPTO_rsa_sign_fdh(const struct GNUNET_CRYPTO_RsaPrivateKey *key,
947 * @param sig memory to freee 955 * @param sig memory to freee
948 */ 956 */
949void 957void
950GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig) 958GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig)
951{ 959{
952 gcry_sexp_release(sig->sexp); 960 gcry_sexp_release (sig->sexp);
953 GNUNET_free(sig); 961 GNUNET_free (sig);
954} 962}
955 963
956 964
@@ -962,22 +970,23 @@ GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
962 * @return size of memory allocated in @a buffer 970 * @return size of memory allocated in @a buffer
963 */ 971 */
964size_t 972size_t
965GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, 973GNUNET_CRYPTO_rsa_signature_encode (const struct
966 char **buffer) 974 GNUNET_CRYPTO_RsaSignature *sig,
975 char **buffer)
967{ 976{
968 size_t n; 977 size_t n;
969 char *b; 978 char *b;
970 979
971 n = gcry_sexp_sprint(sig->sexp, 980 n = gcry_sexp_sprint (sig->sexp,
972 GCRYSEXP_FMT_ADVANCED, 981 GCRYSEXP_FMT_ADVANCED,
973 NULL, 982 NULL,
974 0); 983 0);
975 b = GNUNET_malloc(n); 984 b = GNUNET_malloc (n);
976 GNUNET_assert((n - 1) == /* since the last byte is \0 */ 985 GNUNET_assert ((n - 1) == /* since the last byte is \0 */
977 gcry_sexp_sprint(sig->sexp, 986 gcry_sexp_sprint (sig->sexp,
978 GCRYSEXP_FMT_ADVANCED, 987 GCRYSEXP_FMT_ADVANCED,
979 b, 988 b,
980 n)); 989 n));
981 *buffer = b; 990 *buffer = b;
982 return n; 991 return n;
983} 992}
@@ -992,37 +1001,37 @@ GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig,
992 * @return NULL on error 1001 * @return NULL on error
993 */ 1002 */
994struct GNUNET_CRYPTO_RsaSignature * 1003struct GNUNET_CRYPTO_RsaSignature *
995GNUNET_CRYPTO_rsa_signature_decode(const char *buf, 1004GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
996 size_t len) 1005 size_t len)
997{ 1006{
998 struct GNUNET_CRYPTO_RsaSignature *sig; 1007 struct GNUNET_CRYPTO_RsaSignature *sig;
999 int ret; 1008 int ret;
1000 gcry_mpi_t s; 1009 gcry_mpi_t s;
1001 1010
1002 sig = GNUNET_new(struct GNUNET_CRYPTO_RsaSignature); 1011 sig = GNUNET_new (struct GNUNET_CRYPTO_RsaSignature);
1003 if (0 != 1012 if (0 !=
1004 gcry_sexp_new(&sig->sexp, 1013 gcry_sexp_new (&sig->sexp,
1005 buf, 1014 buf,
1006 len, 1015 len,
1007 0)) 1016 0))
1008 { 1017 {
1009 GNUNET_break_op(0); 1018 GNUNET_break_op (0);
1010 GNUNET_free(sig); 1019 GNUNET_free (sig);
1011 return NULL; 1020 return NULL;
1012 } 1021 }
1013 /* verify that this is an RSA signature */ 1022 /* verify that this is an RSA signature */
1014 ret = key_from_sexp(&s, sig->sexp, "sig-val", "s"); 1023 ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
1015 if (0 != ret) 1024 if (0 != ret)
1016 ret = key_from_sexp(&s, sig->sexp, "rsa", "s"); 1025 ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
1017 if (0 != ret) 1026 if (0 != ret)
1018 { 1027 {
1019 /* this is no RSA Signature */ 1028 /* this is no RSA Signature */
1020 GNUNET_break_op(0); 1029 GNUNET_break_op (0);
1021 gcry_sexp_release(sig->sexp); 1030 gcry_sexp_release (sig->sexp);
1022 GNUNET_free(sig); 1031 GNUNET_free (sig);
1023 return NULL; 1032 return NULL;
1024 } 1033 }
1025 gcry_mpi_release(s); 1034 gcry_mpi_release (s);
1026 return sig; 1035 return sig;
1027} 1036}
1028 1037
@@ -1034,19 +1043,19 @@ GNUNET_CRYPTO_rsa_signature_decode(const char *buf,
1034 * @return the duplicate key; NULL upon error 1043 * @return the duplicate key; NULL upon error
1035 */ 1044 */
1036struct GNUNET_CRYPTO_RsaPublicKey * 1045struct GNUNET_CRYPTO_RsaPublicKey *
1037GNUNET_CRYPTO_rsa_public_key_dup(const struct GNUNET_CRYPTO_RsaPublicKey *key) 1046GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key)
1038{ 1047{
1039 struct GNUNET_CRYPTO_RsaPublicKey *dup; 1048 struct GNUNET_CRYPTO_RsaPublicKey *dup;
1040 gcry_sexp_t dup_sexp; 1049 gcry_sexp_t dup_sexp;
1041 size_t erroff; 1050 size_t erroff;
1042 1051
1043 /* check if we really are exporting a public key */ 1052 /* check if we really are exporting a public key */
1044 dup_sexp = gcry_sexp_find_token(key->sexp, "public-key", 0); 1053 dup_sexp = gcry_sexp_find_token (key->sexp, "public-key", 0);
1045 GNUNET_assert(NULL != dup_sexp); 1054 GNUNET_assert (NULL != dup_sexp);
1046 gcry_sexp_release(dup_sexp); 1055 gcry_sexp_release (dup_sexp);
1047 /* copy the sexp */ 1056 /* copy the sexp */
1048 GNUNET_assert(0 == gcry_sexp_build(&dup_sexp, &erroff, "%S", key->sexp)); 1057 GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", key->sexp));
1049 dup = GNUNET_new(struct GNUNET_CRYPTO_RsaPublicKey); 1058 dup = GNUNET_new (struct GNUNET_CRYPTO_RsaPublicKey);
1050 dup->sexp = dup_sexp; 1059 dup->sexp = dup_sexp;
1051 return dup; 1060 return dup;
1052} 1061}
@@ -1063,9 +1072,9 @@ GNUNET_CRYPTO_rsa_public_key_dup(const struct GNUNET_CRYPTO_RsaPublicKey *key)
1063 * @return unblinded signature on success, NULL if RSA key is bad or malicious. 1072 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
1064 */ 1073 */
1065struct GNUNET_CRYPTO_RsaSignature * 1074struct GNUNET_CRYPTO_RsaSignature *
1066GNUNET_CRYPTO_rsa_unblind(const struct GNUNET_CRYPTO_RsaSignature *sig, 1075GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
1067 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks, 1076 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
1068 struct GNUNET_CRYPTO_RsaPublicKey *pkey) 1077 struct GNUNET_CRYPTO_RsaPublicKey *pkey)
1069{ 1078{
1070 struct RsaBlindingKey *bkey; 1079 struct RsaBlindingKey *bkey;
1071 gcry_mpi_t n; 1080 gcry_mpi_t n;
@@ -1075,70 +1084,70 @@ GNUNET_CRYPTO_rsa_unblind(const struct GNUNET_CRYPTO_RsaSignature *sig,
1075 int ret; 1084 int ret;
1076 struct GNUNET_CRYPTO_RsaSignature *sret; 1085 struct GNUNET_CRYPTO_RsaSignature *sret;
1077 1086
1078 BENCHMARK_START(rsa_unblind); 1087 BENCHMARK_START (rsa_unblind);
1079 1088
1080 ret = key_from_sexp(&n, pkey->sexp, "public-key", "n"); 1089 ret = key_from_sexp (&n, pkey->sexp, "public-key", "n");
1081 if (0 != ret) 1090 if (0 != ret)
1082 ret = key_from_sexp(&n, pkey->sexp, "rsa", "n"); 1091 ret = key_from_sexp (&n, pkey->sexp, "rsa", "n");
1083 if (0 != ret) 1092 if (0 != ret)
1084 { 1093 {
1085 GNUNET_break_op(0); 1094 GNUNET_break_op (0);
1086 return NULL; 1095 return NULL;
1087 } 1096 }
1088 ret = key_from_sexp(&s, sig->sexp, "sig-val", "s"); 1097 ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
1089 if (0 != ret) 1098 if (0 != ret)
1090 ret = key_from_sexp(&s, sig->sexp, "rsa", "s"); 1099 ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
1091 if (0 != ret) 1100 if (0 != ret)
1092 { 1101 {
1093 gcry_mpi_release(n); 1102 gcry_mpi_release (n);
1094 GNUNET_break_op(0); 1103 GNUNET_break_op (0);
1095 return NULL; 1104 return NULL;
1096 } 1105 }
1097 1106
1098 bkey = rsa_blinding_key_derive(pkey, bks); 1107 bkey = rsa_blinding_key_derive (pkey, bks);
1099 if (NULL == bkey) 1108 if (NULL == bkey)
1100 { 1109 {
1101 /* RSA key is malicious since rsa_gcd_validate failed here. 1110 /* RSA key is malicious since rsa_gcd_validate failed here.
1102 * It should have failed during GNUNET_CRYPTO_rsa_blind too though, 1111 * It should have failed during GNUNET_CRYPTO_rsa_blind too though,
1103 * so the exchange is being malicious in an unfamilair way, maybe 1112 * so the exchange is being malicious in an unfamilair way, maybe
1104 * just trying to crash us. */ 1113 * just trying to crash us. */
1105 GNUNET_break_op(0); 1114 GNUNET_break_op (0);
1106 gcry_mpi_release(n); 1115 gcry_mpi_release (n);
1107 gcry_mpi_release(s); 1116 gcry_mpi_release (s);
1108 return NULL; 1117 return NULL;
1109 } 1118 }
1110 1119
1111 r_inv = gcry_mpi_new(0); 1120 r_inv = gcry_mpi_new (0);
1112 if (1 != 1121 if (1 !=
1113 gcry_mpi_invm(r_inv, 1122 gcry_mpi_invm (r_inv,
1114 bkey->r, 1123 bkey->r,
1115 n)) 1124 n))
1116 { 1125 {
1117 /* We cannot find r mod n, so gcd(r,n) != 1, which should get * 1126 /* We cannot find r mod n, so gcd(r,n) != 1, which should get *
1118 * caught above, but we handle it the same here. */ 1127 * caught above, but we handle it the same here. */
1119 GNUNET_break_op(0); 1128 GNUNET_break_op (0);
1120 gcry_mpi_release(r_inv); 1129 gcry_mpi_release (r_inv);
1121 rsa_blinding_key_free(bkey); 1130 rsa_blinding_key_free (bkey);
1122 gcry_mpi_release(n); 1131 gcry_mpi_release (n);
1123 gcry_mpi_release(s); 1132 gcry_mpi_release (s);
1124 return NULL; 1133 return NULL;
1125 } 1134 }
1126 1135
1127 ubsig = gcry_mpi_new(0); 1136 ubsig = gcry_mpi_new (0);
1128 gcry_mpi_mulm(ubsig, s, r_inv, n); 1137 gcry_mpi_mulm (ubsig, s, r_inv, n);
1129 gcry_mpi_release(n); 1138 gcry_mpi_release (n);
1130 gcry_mpi_release(r_inv); 1139 gcry_mpi_release (r_inv);
1131 gcry_mpi_release(s); 1140 gcry_mpi_release (s);
1132 rsa_blinding_key_free(bkey); 1141 rsa_blinding_key_free (bkey);
1133 1142
1134 sret = GNUNET_new(struct GNUNET_CRYPTO_RsaSignature); 1143 sret = GNUNET_new (struct GNUNET_CRYPTO_RsaSignature);
1135 GNUNET_assert(0 == 1144 GNUNET_assert (0 ==
1136 gcry_sexp_build(&sret->sexp, 1145 gcry_sexp_build (&sret->sexp,
1137 NULL, 1146 NULL,
1138 "(sig-val (rsa (s %M)))", 1147 "(sig-val (rsa (s %M)))",
1139 ubsig)); 1148 ubsig));
1140 gcry_mpi_release(ubsig); 1149 gcry_mpi_release (ubsig);
1141 BENCHMARK_END(rsa_unblind); 1150 BENCHMARK_END (rsa_unblind);
1142 return sret; 1151 return sret;
1143} 1152}
1144 1153
@@ -1153,47 +1162,47 @@ GNUNET_CRYPTO_rsa_unblind(const struct GNUNET_CRYPTO_RsaSignature *sig,
1153 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature is invalid 1162 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature is invalid
1154 */ 1163 */
1155int 1164int
1156GNUNET_CRYPTO_rsa_verify(const struct GNUNET_HashCode *hash, 1165GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
1157 const struct GNUNET_CRYPTO_RsaSignature *sig, 1166 const struct GNUNET_CRYPTO_RsaSignature *sig,
1158 const struct GNUNET_CRYPTO_RsaPublicKey *pkey) 1167 const struct GNUNET_CRYPTO_RsaPublicKey *pkey)
1159{ 1168{
1160 gcry_sexp_t data; 1169 gcry_sexp_t data;
1161 gcry_mpi_t r; 1170 gcry_mpi_t r;
1162 int rc; 1171 int rc;
1163 1172
1164 BENCHMARK_START(rsa_verify); 1173 BENCHMARK_START (rsa_verify);
1165 1174
1166 r = rsa_full_domain_hash(pkey, hash); 1175 r = rsa_full_domain_hash (pkey, hash);
1167 if (NULL == r) 1176 if (NULL == r)
1168 { 1177 {
1169 GNUNET_break_op(0); 1178 GNUNET_break_op (0);
1170 /* RSA key is malicious since rsa_gcd_validate failed here. 1179 /* RSA key is malicious since rsa_gcd_validate failed here.
1171 * It should have failed during GNUNET_CRYPTO_rsa_blind too though, 1180 * It should have failed during GNUNET_CRYPTO_rsa_blind too though,
1172 * so the exchange is being malicious in an unfamilair way, maybe 1181 * so the exchange is being malicious in an unfamilair way, maybe
1173 * just trying to crash us. Arguably, we've only an internal error 1182 * just trying to crash us. Arguably, we've only an internal error
1174 * though because we should've detected this in our previous call 1183 * though because we should've detected this in our previous call
1175 * to GNUNET_CRYPTO_rsa_unblind. */ 1184 * to GNUNET_CRYPTO_rsa_unblind. */
1176 return GNUNET_NO; 1185 return GNUNET_NO;
1177 } 1186 }
1178 1187
1179 data = mpi_to_sexp(r); 1188 data = mpi_to_sexp (r);
1180 gcry_mpi_release(r); 1189 gcry_mpi_release (r);
1181 1190
1182 rc = gcry_pk_verify(sig->sexp, 1191 rc = gcry_pk_verify (sig->sexp,
1183 data, 1192 data,
1184 pkey->sexp); 1193 pkey->sexp);
1185 gcry_sexp_release(data); 1194 gcry_sexp_release (data);
1186 if (0 != rc) 1195 if (0 != rc)
1187 { 1196 {
1188 LOG(GNUNET_ERROR_TYPE_WARNING, 1197 LOG (GNUNET_ERROR_TYPE_WARNING,
1189 _("RSA signature verification failed at %s:%d: %s\n"), 1198 _ ("RSA signature verification failed at %s:%d: %s\n"),
1190 __FILE__, 1199 __FILE__,
1191 __LINE__, 1200 __LINE__,
1192 gcry_strerror(rc)); 1201 gcry_strerror (rc));
1193 return GNUNET_SYSERR; 1202 return GNUNET_SYSERR;
1194 BENCHMARK_END(rsa_verify); 1203 BENCHMARK_END (rsa_verify);
1195 } 1204 }
1196 BENCHMARK_END(rsa_verify); 1205 BENCHMARK_END (rsa_verify);
1197 return GNUNET_OK; 1206 return GNUNET_OK;
1198} 1207}
1199 1208
@@ -1205,19 +1214,20 @@ GNUNET_CRYPTO_rsa_verify(const struct GNUNET_HashCode *hash,
1205 * @return the duplicate key; NULL upon error 1214 * @return the duplicate key; NULL upon error
1206 */ 1215 */
1207struct GNUNET_CRYPTO_RsaPrivateKey * 1216struct GNUNET_CRYPTO_RsaPrivateKey *
1208GNUNET_CRYPTO_rsa_private_key_dup(const struct GNUNET_CRYPTO_RsaPrivateKey *key) 1217GNUNET_CRYPTO_rsa_private_key_dup (const struct
1218 GNUNET_CRYPTO_RsaPrivateKey *key)
1209{ 1219{
1210 struct GNUNET_CRYPTO_RsaPrivateKey *dup; 1220 struct GNUNET_CRYPTO_RsaPrivateKey *dup;
1211 gcry_sexp_t dup_sexp; 1221 gcry_sexp_t dup_sexp;
1212 size_t erroff; 1222 size_t erroff;
1213 1223
1214 /* check if we really are exporting a private key */ 1224 /* check if we really are exporting a private key */
1215 dup_sexp = gcry_sexp_find_token(key->sexp, "private-key", 0); 1225 dup_sexp = gcry_sexp_find_token (key->sexp, "private-key", 0);
1216 GNUNET_assert(NULL != dup_sexp); 1226 GNUNET_assert (NULL != dup_sexp);
1217 gcry_sexp_release(dup_sexp); 1227 gcry_sexp_release (dup_sexp);
1218 /* copy the sexp */ 1228 /* copy the sexp */
1219 GNUNET_assert(0 == gcry_sexp_build(&dup_sexp, &erroff, "%S", key->sexp)); 1229 GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", key->sexp));
1220 dup = GNUNET_new(struct GNUNET_CRYPTO_RsaPrivateKey); 1230 dup = GNUNET_new (struct GNUNET_CRYPTO_RsaPrivateKey);
1221 dup->sexp = dup_sexp; 1231 dup->sexp = dup_sexp;
1222 return dup; 1232 return dup;
1223} 1233}
@@ -1230,7 +1240,7 @@ GNUNET_CRYPTO_rsa_private_key_dup(const struct GNUNET_CRYPTO_RsaPrivateKey *key)
1230 * @return the duplicate key; NULL upon error 1240 * @return the duplicate key; NULL upon error
1231 */ 1241 */
1232struct GNUNET_CRYPTO_RsaSignature * 1242struct GNUNET_CRYPTO_RsaSignature *
1233GNUNET_CRYPTO_rsa_signature_dup(const struct GNUNET_CRYPTO_RsaSignature *sig) 1243GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig)
1234{ 1244{
1235 struct GNUNET_CRYPTO_RsaSignature *dup; 1245 struct GNUNET_CRYPTO_RsaSignature *dup;
1236 gcry_sexp_t dup_sexp; 1246 gcry_sexp_t dup_sexp;
@@ -1239,14 +1249,14 @@ GNUNET_CRYPTO_rsa_signature_dup(const struct GNUNET_CRYPTO_RsaSignature *sig)
1239 int ret; 1249 int ret;
1240 1250
1241 /* verify that this is an RSA signature */ 1251 /* verify that this is an RSA signature */
1242 ret = key_from_sexp(&s, sig->sexp, "sig-val", "s"); 1252 ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
1243 if (0 != ret) 1253 if (0 != ret)
1244 ret = key_from_sexp(&s, sig->sexp, "rsa", "s"); 1254 ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
1245 GNUNET_assert(0 == ret); 1255 GNUNET_assert (0 == ret);
1246 gcry_mpi_release(s); 1256 gcry_mpi_release (s);
1247 /* copy the sexp */ 1257 /* copy the sexp */
1248 GNUNET_assert(0 == gcry_sexp_build(&dup_sexp, &erroff, "%S", sig->sexp)); 1258 GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", sig->sexp));
1249 dup = GNUNET_new(struct GNUNET_CRYPTO_RsaSignature); 1259 dup = GNUNET_new (struct GNUNET_CRYPTO_RsaSignature);
1250 dup->sexp = dup_sexp; 1260 dup->sexp = dup_sexp;
1251 return dup; 1261 return dup;
1252} 1262}
diff --git a/src/util/crypto_symmetric.c b/src/util/crypto_symmetric.c
index 7fad2a884..a6e22521a 100644
--- a/src/util/crypto_symmetric.c
+++ b/src/util/crypto_symmetric.c
@@ -29,7 +29,8 @@
29#include "gnunet_crypto_lib.h" 29#include "gnunet_crypto_lib.h"
30#include <gcrypt.h> 30#include <gcrypt.h>
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-symmetric", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-symmetric", \
33 __VA_ARGS__)
33 34
34/** 35/**
35 * Create a new SessionKey (for symmetric encryption). 36 * Create a new SessionKey (for symmetric encryption).
@@ -37,14 +38,16 @@
37 * @param key session key to initialize 38 * @param key session key to initialize
38 */ 39 */
39void 40void
40GNUNET_CRYPTO_symmetric_create_session_key(struct GNUNET_CRYPTO_SymmetricSessionKey *key) 41GNUNET_CRYPTO_symmetric_create_session_key (struct
42 GNUNET_CRYPTO_SymmetricSessionKey *
43 key)
41{ 44{
42 gcry_randomize(key->aes_key, 45 gcry_randomize (key->aes_key,
43 GNUNET_CRYPTO_AES_KEY_LENGTH, 46 GNUNET_CRYPTO_AES_KEY_LENGTH,
44 GCRY_STRONG_RANDOM); 47 GCRY_STRONG_RANDOM);
45 gcry_randomize(key->twofish_key, 48 gcry_randomize (key->twofish_key,
46 GNUNET_CRYPTO_AES_KEY_LENGTH, 49 GNUNET_CRYPTO_AES_KEY_LENGTH,
47 GCRY_STRONG_RANDOM); 50 GCRY_STRONG_RANDOM);
48} 51}
49 52
50 53
@@ -57,23 +60,23 @@ GNUNET_CRYPTO_symmetric_create_session_key(struct GNUNET_CRYPTO_SymmetricSession
57 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 60 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
58 */ 61 */
59static int 62static int
60setup_cipher_aes(gcry_cipher_hd_t *handle, 63setup_cipher_aes (gcry_cipher_hd_t *handle,
61 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, 64 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
62 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv) 65 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
63{ 66{
64 int rc; 67 int rc;
65 68
66 GNUNET_assert(0 == 69 GNUNET_assert (0 ==
67 gcry_cipher_open(handle, GCRY_CIPHER_AES256, 70 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
68 GCRY_CIPHER_MODE_CFB, 0)); 71 GCRY_CIPHER_MODE_CFB, 0));
69 rc = gcry_cipher_setkey(*handle, 72 rc = gcry_cipher_setkey (*handle,
70 sessionkey->aes_key, 73 sessionkey->aes_key,
71 sizeof(sessionkey->aes_key)); 74 sizeof(sessionkey->aes_key));
72 GNUNET_assert((0 == rc) || ((char)rc == GPG_ERR_WEAK_KEY)); 75 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
73 rc = gcry_cipher_setiv(*handle, 76 rc = gcry_cipher_setiv (*handle,
74 iv->aes_iv, 77 iv->aes_iv,
75 sizeof(iv->aes_iv)); 78 sizeof(iv->aes_iv));
76 GNUNET_assert((0 == rc) || ((char)rc == GPG_ERR_WEAK_KEY)); 79 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
77 return GNUNET_OK; 80 return GNUNET_OK;
78} 81}
79 82
@@ -87,23 +90,25 @@ setup_cipher_aes(gcry_cipher_hd_t *handle,
87 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 90 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
88 */ 91 */
89static int 92static int
90setup_cipher_twofish(gcry_cipher_hd_t *handle, 93setup_cipher_twofish (gcry_cipher_hd_t *handle,
91 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, 94 const struct
92 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv) 95 GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
96 const struct
97 GNUNET_CRYPTO_SymmetricInitializationVector *iv)
93{ 98{
94 int rc; 99 int rc;
95 100
96 GNUNET_assert(0 == 101 GNUNET_assert (0 ==
97 gcry_cipher_open(handle, GCRY_CIPHER_TWOFISH, 102 gcry_cipher_open (handle, GCRY_CIPHER_TWOFISH,
98 GCRY_CIPHER_MODE_CFB, 0)); 103 GCRY_CIPHER_MODE_CFB, 0));
99 rc = gcry_cipher_setkey(*handle, 104 rc = gcry_cipher_setkey (*handle,
100 sessionkey->twofish_key, 105 sessionkey->twofish_key,
101 sizeof(sessionkey->twofish_key)); 106 sizeof(sessionkey->twofish_key));
102 GNUNET_assert((0 == rc) || ((char)rc == GPG_ERR_WEAK_KEY)); 107 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
103 rc = gcry_cipher_setiv(*handle, 108 rc = gcry_cipher_setiv (*handle,
104 iv->twofish_iv, 109 iv->twofish_iv,
105 sizeof(iv->twofish_iv)); 110 sizeof(iv->twofish_iv));
106 GNUNET_assert((0 == rc) || ((char)rc == GPG_ERR_WEAK_KEY)); 111 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
107 return GNUNET_OK; 112 return GNUNET_OK;
108} 113}
109 114
@@ -122,24 +127,26 @@ setup_cipher_twofish(gcry_cipher_hd_t *handle,
122 * this size should be the same as @c len. 127 * this size should be the same as @c len.
123 */ 128 */
124ssize_t 129ssize_t
125GNUNET_CRYPTO_symmetric_encrypt(const void *block, 130GNUNET_CRYPTO_symmetric_encrypt (const void *block,
126 size_t size, 131 size_t size,
127 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, 132 const struct
128 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, 133 GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
129 void *result) 134 const struct
135 GNUNET_CRYPTO_SymmetricInitializationVector *iv,
136 void *result)
130{ 137{
131 gcry_cipher_hd_t handle; 138 gcry_cipher_hd_t handle;
132 char tmp[size]; 139 char tmp[size];
133 140
134 if (GNUNET_OK != setup_cipher_aes(&handle, sessionkey, iv)) 141 if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
135 return -1; 142 return -1;
136 GNUNET_assert(0 == gcry_cipher_encrypt(handle, tmp, size, block, size)); 143 GNUNET_assert (0 == gcry_cipher_encrypt (handle, tmp, size, block, size));
137 gcry_cipher_close(handle); 144 gcry_cipher_close (handle);
138 if (GNUNET_OK != setup_cipher_twofish(&handle, sessionkey, iv)) 145 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
139 return -1; 146 return -1;
140 GNUNET_assert(0 == gcry_cipher_encrypt(handle, result, size, tmp, size)); 147 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, size, tmp, size));
141 gcry_cipher_close(handle); 148 gcry_cipher_close (handle);
142 memset(tmp, 0, sizeof(tmp)); 149 memset (tmp, 0, sizeof(tmp));
143 return size; 150 return size;
144} 151}
145 152
@@ -158,24 +165,26 @@ GNUNET_CRYPTO_symmetric_encrypt(const void *block,
158 * this size should be the same as @c size. 165 * this size should be the same as @c size.
159 */ 166 */
160ssize_t 167ssize_t
161GNUNET_CRYPTO_symmetric_decrypt(const void *block, 168GNUNET_CRYPTO_symmetric_decrypt (const void *block,
162 size_t size, 169 size_t size,
163 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, 170 const struct
164 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, 171 GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
165 void *result) 172 const struct
173 GNUNET_CRYPTO_SymmetricInitializationVector *iv,
174 void *result)
166{ 175{
167 gcry_cipher_hd_t handle; 176 gcry_cipher_hd_t handle;
168 char tmp[size]; 177 char tmp[size];
169 178
170 if (GNUNET_OK != setup_cipher_twofish(&handle, sessionkey, iv)) 179 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
171 return -1; 180 return -1;
172 GNUNET_assert(0 == gcry_cipher_decrypt(handle, tmp, size, block, size)); 181 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, block, size));
173 gcry_cipher_close(handle); 182 gcry_cipher_close (handle);
174 if (GNUNET_OK != setup_cipher_aes(&handle, sessionkey, iv)) 183 if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
175 return -1; 184 return -1;
176 GNUNET_assert(0 == gcry_cipher_decrypt(handle, result, size, tmp, size)); 185 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, tmp, size));
177 gcry_cipher_close(handle); 186 gcry_cipher_close (handle);
178 memset(tmp, 0, sizeof(tmp)); 187 memset (tmp, 0, sizeof(tmp));
179 return size; 188 return size;
180} 189}
181 190
@@ -190,17 +199,20 @@ GNUNET_CRYPTO_symmetric_decrypt(const void *block,
190 * @param ... pairs of void * & size_t for context chunks, terminated by NULL 199 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
191 */ 200 */
192void 201void
193GNUNET_CRYPTO_symmetric_derive_iv(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, 202GNUNET_CRYPTO_symmetric_derive_iv (struct
194 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, 203 GNUNET_CRYPTO_SymmetricInitializationVector *
195 const void *salt, 204 iv,
196 size_t salt_len, 205 const struct
197 ...) 206 GNUNET_CRYPTO_SymmetricSessionKey *skey,
207 const void *salt,
208 size_t salt_len,
209 ...)
198{ 210{
199 va_list argp; 211 va_list argp;
200 212
201 va_start(argp, salt_len); 213 va_start (argp, salt_len);
202 GNUNET_CRYPTO_symmetric_derive_iv_v(iv, skey, salt, salt_len, argp); 214 GNUNET_CRYPTO_symmetric_derive_iv_v (iv, skey, salt, salt_len, argp);
203 va_end(argp); 215 va_end (argp);
204} 216}
205 217
206 218
@@ -214,33 +226,36 @@ GNUNET_CRYPTO_symmetric_derive_iv(struct GNUNET_CRYPTO_SymmetricInitializationVe
214 * @param argp pairs of void * & size_t for context chunks, terminated by NULL 226 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
215 */ 227 */
216void 228void
217GNUNET_CRYPTO_symmetric_derive_iv_v(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, 229GNUNET_CRYPTO_symmetric_derive_iv_v (struct
218 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, 230 GNUNET_CRYPTO_SymmetricInitializationVector
219 const void *salt, 231 *iv,
220 size_t salt_len, 232 const struct
221 va_list argp) 233 GNUNET_CRYPTO_SymmetricSessionKey *skey,
234 const void *salt,
235 size_t salt_len,
236 va_list argp)
222{ 237{
223 char aes_salt[salt_len + 4]; 238 char aes_salt[salt_len + 4];
224 char twofish_salt[salt_len + 4]; 239 char twofish_salt[salt_len + 4];
225 240
226 GNUNET_memcpy(aes_salt, salt, salt_len); 241 GNUNET_memcpy (aes_salt, salt, salt_len);
227 GNUNET_memcpy(&aes_salt[salt_len], "AES!", 4); 242 GNUNET_memcpy (&aes_salt[salt_len], "AES!", 4);
228 GNUNET_memcpy(twofish_salt, salt, salt_len); 243 GNUNET_memcpy (twofish_salt, salt, salt_len);
229 GNUNET_memcpy(&twofish_salt[salt_len], "FISH", 4); 244 GNUNET_memcpy (&twofish_salt[salt_len], "FISH", 4);
230 GNUNET_CRYPTO_kdf_v(iv->aes_iv, 245 GNUNET_CRYPTO_kdf_v (iv->aes_iv,
231 sizeof(iv->aes_iv), 246 sizeof(iv->aes_iv),
232 aes_salt, 247 aes_salt,
233 salt_len + 4, 248 salt_len + 4,
234 skey->aes_key, 249 skey->aes_key,
235 sizeof(skey->aes_key), 250 sizeof(skey->aes_key),
236 argp); 251 argp);
237 GNUNET_CRYPTO_kdf_v(iv->twofish_iv, 252 GNUNET_CRYPTO_kdf_v (iv->twofish_iv,
238 sizeof(iv->twofish_iv), 253 sizeof(iv->twofish_iv),
239 twofish_salt, 254 twofish_salt,
240 salt_len + 4, 255 salt_len + 4,
241 skey->twofish_key, 256 skey->twofish_key,
242 sizeof(skey->twofish_key), 257 sizeof(skey->twofish_key),
243 argp); 258 argp);
244} 259}
245 260
246/* end of crypto_symmetric.c */ 261/* end of crypto_symmetric.c */
diff --git a/src/util/disk.c b/src/util/disk.c
index 8b474542b..32667d554 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -28,13 +28,13 @@
28#include "gnunet_strings_lib.h" 28#include "gnunet_strings_lib.h"
29#include "gnunet_disk_lib.h" 29#include "gnunet_disk_lib.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-disk", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-disk", __VA_ARGS__)
32 32
33#define LOG_STRERROR(kind, syscall) \ 33#define LOG_STRERROR(kind, syscall) \
34 GNUNET_log_from_strerror(kind, "util-disk", syscall) 34 GNUNET_log_from_strerror (kind, "util-disk", syscall)
35 35
36#define LOG_STRERROR_FILE(kind, syscall, filename) \ 36#define LOG_STRERROR_FILE(kind, syscall, filename) \
37 GNUNET_log_from_strerror_file(kind, "util-disk", syscall, filename) 37 GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename)
38 38
39/** 39/**
40 * Block size for IO for copying files. 40 * Block size for IO for copying files.
@@ -65,7 +65,8 @@
65/** 65/**
66 * Handle used to manage a pipe. 66 * Handle used to manage a pipe.
67 */ 67 */
68struct GNUNET_DISK_PipeHandle { 68struct GNUNET_DISK_PipeHandle
69{
69 /** 70 /**
70 * File descriptors for the pipe. 71 * File descriptors for the pipe.
71 * One or both of them could be NULL. 72 * One or both of them could be NULL.
@@ -78,7 +79,8 @@ struct GNUNET_DISK_PipeHandle {
78 * Closure for the recursion to determine the file size 79 * Closure for the recursion to determine the file size
79 * of a directory. 80 * of a directory.
80 */ 81 */
81struct GetFileSizeData { 82struct GetFileSizeData
83{
82 /** 84 /**
83 * Set to the total file size. 85 * Set to the total file size.
84 */ 86 */
@@ -104,7 +106,7 @@ struct GetFileSizeData {
104 * @return file permissions, UNIX style 106 * @return file permissions, UNIX style
105 */ 107 */
106static int 108static int
107translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm) 109translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm)
108{ 110{
109 int mode; 111 int mode;
110 112
@@ -141,41 +143,41 @@ translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm)
141 * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK 143 * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK
142 */ 144 */
143static int 145static int
144getSizeRec(void *cls, const char *fn) 146getSizeRec (void *cls, const char *fn)
145{ 147{
146 struct GetFileSizeData *gfsd = cls; 148 struct GetFileSizeData *gfsd = cls;
147 149
148#if defined(HAVE_STAT64) && \ 150#if defined(HAVE_STAT64) && \
149 !(defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) 151 ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
150 struct stat64 buf; 152 struct stat64 buf;
151 153
152 if (0 != stat64(fn, &buf)) 154 if (0 != stat64 (fn, &buf))
153 { 155 {
154 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "stat64", fn); 156 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat64", fn);
155 return GNUNET_SYSERR; 157 return GNUNET_SYSERR;
156 } 158 }
157#else 159#else
158 struct stat buf; 160 struct stat buf;
159 161
160 if (0 != stat(fn, &buf)) 162 if (0 != stat (fn, &buf))
161 { 163 {
162 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "stat", fn); 164 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn);
163 return GNUNET_SYSERR; 165 return GNUNET_SYSERR;
164 } 166 }
165#endif 167#endif
166 if ((S_ISDIR(buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES)) 168 if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
167 { 169 {
168 errno = EISDIR; 170 errno = EISDIR;
169 return GNUNET_SYSERR; 171 return GNUNET_SYSERR;
170 } 172 }
171 if ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) 173 if ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
172 gfsd->total += buf.st_size; 174 gfsd->total += buf.st_size;
173 if ((S_ISDIR(buf.st_mode)) && (0 == access(fn, X_OK)) && 175 if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) &&
174 ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) 176 ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
175 { 177 {
176 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan(fn, &getSizeRec, gfsd)) 178 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
177 return GNUNET_SYSERR; 179 return GNUNET_SYSERR;
178 } 180 }
179 return GNUNET_OK; 181 return GNUNET_OK;
180} 182}
181 183
@@ -187,9 +189,9 @@ getSizeRec(void *cls, const char *fn)
187 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid 189 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
188 */ 190 */
189int 191int
190GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h) 192GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
191{ 193{
192 return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; 194 return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
193} 195}
194 196
195/** 197/**
@@ -200,11 +202,11 @@ GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
200 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 202 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
201 */ 203 */
202int 204int
203GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size) 205GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size)
204{ 206{
205 struct stat sbuf; 207 struct stat sbuf;
206 208
207 if (0 != fstat(fh->fd, &sbuf)) 209 if (0 != fstat (fh->fd, &sbuf))
208 return GNUNET_SYSERR; 210 return GNUNET_SYSERR;
209 *size = sbuf.st_size; 211 *size = sbuf.st_size;
210 return GNUNET_OK; 212 return GNUNET_OK;
@@ -220,19 +222,19 @@ GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
220 * @return the new position on success, #GNUNET_SYSERR otherwise 222 * @return the new position on success, #GNUNET_SYSERR otherwise
221 */ 223 */
222off_t 224off_t
223GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, 225GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
224 off_t offset, 226 off_t offset,
225 enum GNUNET_DISK_Seek whence) 227 enum GNUNET_DISK_Seek whence)
226{ 228{
227 if (h == NULL) 229 if (h == NULL)
228 { 230 {
229 errno = EINVAL; 231 errno = EINVAL;
230 return GNUNET_SYSERR; 232 return GNUNET_SYSERR;
231 } 233 }
232 234
233 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END }; 235 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
234 236
235 return lseek(h->fd, offset, t[whence]); 237 return lseek (h->fd, offset, t[whence]);
236} 238}
237 239
238 240
@@ -251,19 +253,19 @@ GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h,
251 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 253 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
252 */ 254 */
253int 255int
254GNUNET_DISK_file_size(const char *filename, 256GNUNET_DISK_file_size (const char *filename,
255 uint64_t *size, 257 uint64_t *size,
256 int include_symbolic_links, 258 int include_symbolic_links,
257 int single_file_mode) 259 int single_file_mode)
258{ 260{
259 struct GetFileSizeData gfsd; 261 struct GetFileSizeData gfsd;
260 int ret; 262 int ret;
261 263
262 GNUNET_assert(size != NULL); 264 GNUNET_assert (size != NULL);
263 gfsd.total = 0; 265 gfsd.total = 0;
264 gfsd.include_sym_links = include_symbolic_links; 266 gfsd.include_sym_links = include_symbolic_links;
265 gfsd.single_file_mode = single_file_mode; 267 gfsd.single_file_mode = single_file_mode;
266 ret = getSizeRec(&gfsd, filename); 268 ret = getSizeRec (&gfsd, filename);
267 *size = gfsd.total; 269 *size = gfsd.total;
268 return ret; 270 return ret;
269} 271}
@@ -285,19 +287,19 @@ GNUNET_DISK_file_size(const char *filename,
285 * @return #GNUNET_OK on success 287 * @return #GNUNET_OK on success
286 */ 288 */
287int 289int
288GNUNET_DISK_file_get_identifiers(const char *filename, 290GNUNET_DISK_file_get_identifiers (const char *filename,
289 uint64_t *dev, 291 uint64_t *dev,
290 uint64_t *ino) 292 uint64_t *ino)
291{ 293{
292#if HAVE_STAT 294#if HAVE_STAT
293 { 295 {
294 struct stat sbuf; 296 struct stat sbuf;
295 297
296 if (0 != stat(filename, &sbuf)) 298 if (0 != stat (filename, &sbuf))
297 { 299 {
298 return GNUNET_SYSERR; 300 return GNUNET_SYSERR;
299 } 301 }
300 *ino = (uint64_t)sbuf.st_ino; 302 *ino = (uint64_t) sbuf.st_ino;
301 } 303 }
302#else 304#else
303 *ino = 0; 305 *ino = 0;
@@ -306,22 +308,22 @@ GNUNET_DISK_file_get_identifiers(const char *filename,
306 { 308 {
307 struct statvfs fbuf; 309 struct statvfs fbuf;
308 310
309 if (0 != statvfs(filename, &fbuf)) 311 if (0 != statvfs (filename, &fbuf))
310 { 312 {
311 return GNUNET_SYSERR; 313 return GNUNET_SYSERR;
312 } 314 }
313 *dev = (uint64_t)fbuf.f_fsid; 315 *dev = (uint64_t) fbuf.f_fsid;
314 } 316 }
315#elif HAVE_STATFS 317#elif HAVE_STATFS
316 { 318 {
317 struct statfs fbuf; 319 struct statfs fbuf;
318 320
319 if (0 != statfs(filename, &fbuf)) 321 if (0 != statfs (filename, &fbuf))
320 { 322 {
321 return GNUNET_SYSERR; 323 return GNUNET_SYSERR;
322 } 324 }
323 *dev = 325 *dev =
324 ((uint64_t)fbuf.f_fsid.val[0]) << 32 || ((uint64_t)fbuf.f_fsid.val[1]); 326 ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]);
325 } 327 }
326#else 328#else
327 *dev = 0; 329 *dev = 0;
@@ -337,28 +339,28 @@ GNUNET_DISK_file_get_identifiers(const char *filename,
337 * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error 339 * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error
338 */ 340 */
339static char * 341static char *
340mktemp_name(const char *t) 342mktemp_name (const char *t)
341{ 343{
342 const char *tmpdir; 344 const char *tmpdir;
343 char *tmpl; 345 char *tmpl;
344 char *fn; 346 char *fn;
345 347
346 if ((t[0] != '/') && (t[0] != '\\')) 348 if ((t[0] != '/') && (t[0] != '\\'))
347 { 349 {
348 /* FIXME: This uses system codepage on W32, not UTF-8 */ 350 /* FIXME: This uses system codepage on W32, not UTF-8 */
349 tmpdir = getenv("TMPDIR"); 351 tmpdir = getenv ("TMPDIR");
350 if (NULL == tmpdir) 352 if (NULL == tmpdir)
351 tmpdir = getenv("TMP"); 353 tmpdir = getenv ("TMP");
352 if (NULL == tmpdir) 354 if (NULL == tmpdir)
353 tmpdir = getenv("TEMP"); 355 tmpdir = getenv ("TEMP");
354 if (NULL == tmpdir) 356 if (NULL == tmpdir)
355 tmpdir = "/tmp"; 357 tmpdir = "/tmp";
356 GNUNET_asprintf(&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); 358 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
357 } 359 }
358 else 360 else
359 { 361 {
360 GNUNET_asprintf(&tmpl, "%s%s", t, "XXXXXX"); 362 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
361 } 363 }
362 fn = tmpl; 364 fn = tmpl;
363 return fn; 365 return fn;
364} 366}
@@ -373,9 +375,9 @@ mktemp_name(const char *t)
373 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set 375 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
374 */ 376 */
375void 377void
376GNUNET_DISK_fix_permissions(const char *fn, 378GNUNET_DISK_fix_permissions (const char *fn,
377 int require_uid_match, 379 int require_uid_match,
378 int require_gid_match) 380 int require_gid_match)
379{ 381{
380 mode_t mode; 382 mode_t mode;
381 383
@@ -384,10 +386,10 @@ GNUNET_DISK_fix_permissions(const char *fn,
384 else if (GNUNET_YES == require_gid_match) 386 else if (GNUNET_YES == require_gid_match)
385 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP; 387 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
386 else 388 else
387 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | 389 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH
388 S_IWOTH | S_IXOTH; 390 | S_IWOTH | S_IXOTH;
389 if (0 != chmod(fn, mode)) 391 if (0 != chmod (fn, mode))
390 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "chmod", fn); 392 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn);
391} 393}
392 394
393 395
@@ -403,21 +405,21 @@ GNUNET_DISK_fix_permissions(const char *fn,
403 * file on disk in directory for temporary files 405 * file on disk in directory for temporary files
404 */ 406 */
405char * 407char *
406GNUNET_DISK_mkdtemp(const char *t) 408GNUNET_DISK_mkdtemp (const char *t)
407{ 409{
408 char *fn; 410 char *fn;
409 mode_t omask; 411 mode_t omask;
410 412
411 omask = umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); 413 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
412 fn = mktemp_name(t); 414 fn = mktemp_name (t);
413 if (fn != mkdtemp(fn)) 415 if (fn != mkdtemp (fn))
414 { 416 {
415 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn); 417 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn);
416 GNUNET_free(fn); 418 GNUNET_free (fn);
417 umask(omask); 419 umask (omask);
418 return NULL; 420 return NULL;
419 } 421 }
420 umask(omask); 422 umask (omask);
421 return fn; 423 return fn;
422} 424}
423 425
@@ -430,23 +432,23 @@ GNUNET_DISK_mkdtemp(const char *t)
430 * @param fil name of the file to back up 432 * @param fil name of the file to back up
431 */ 433 */
432void 434void
433GNUNET_DISK_file_backup(const char *fil) 435GNUNET_DISK_file_backup (const char *fil)
434{ 436{
435 size_t slen; 437 size_t slen;
436 char *target; 438 char *target;
437 unsigned int num; 439 unsigned int num;
438 440
439 slen = strlen(fil) + 20; 441 slen = strlen (fil) + 20;
440 target = GNUNET_malloc(slen); 442 target = GNUNET_malloc (slen);
441 num = 0; 443 num = 0;
442 do 444 do
443 { 445 {
444 GNUNET_snprintf(target, slen, "%s.%u~", fil, num++); 446 GNUNET_snprintf (target, slen, "%s.%u~", fil, num++);
445 } 447 }
446 while (0 == access(target, F_OK)); 448 while (0 == access (target, F_OK));
447 if (0 != rename(fil, target)) 449 if (0 != rename (fil, target))
448 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "rename", fil); 450 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "rename", fil);
449 GNUNET_free(target); 451 GNUNET_free (target);
450} 452}
451 453
452 454
@@ -462,24 +464,24 @@ GNUNET_DISK_file_backup(const char *fil)
462 * file on disk in directory for temporary files 464 * file on disk in directory for temporary files
463 */ 465 */
464char * 466char *
465GNUNET_DISK_mktemp(const char *t) 467GNUNET_DISK_mktemp (const char *t)
466{ 468{
467 int fd; 469 int fd;
468 char *fn; 470 char *fn;
469 mode_t omask; 471 mode_t omask;
470 472
471 omask = umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); 473 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
472 fn = mktemp_name(t); 474 fn = mktemp_name (t);
473 if (-1 == (fd = mkstemp(fn))) 475 if (-1 == (fd = mkstemp (fn)))
474 { 476 {
475 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); 477 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
476 GNUNET_free(fn); 478 GNUNET_free (fn);
477 umask(omask); 479 umask (omask);
478 return NULL; 480 return NULL;
479 } 481 }
480 umask(omask); 482 umask (omask);
481 if (0 != close(fd)) 483 if (0 != close (fd))
482 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "close", fn); 484 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
483 return fn; 485 return fn;
484} 486}
485 487
@@ -497,34 +499,34 @@ GNUNET_DISK_mktemp(const char *t)
497 * does not exist or stat'ed 499 * does not exist or stat'ed
498 */ 500 */
499int 501int
500GNUNET_DISK_directory_test(const char *fil, int is_readable) 502GNUNET_DISK_directory_test (const char *fil, int is_readable)
501{ 503{
502 struct stat filestat; 504 struct stat filestat;
503 int ret; 505 int ret;
504 506
505 ret = stat(fil, &filestat); 507 ret = stat (fil, &filestat);
506 if (ret != 0) 508 if (ret != 0)
507 { 509 {
508 if (errno != ENOENT) 510 if (errno != ENOENT)
509 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", fil); 511 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
510 return GNUNET_SYSERR; 512 return GNUNET_SYSERR;
511 } 513 }
512 if (!S_ISDIR(filestat.st_mode)) 514 if (! S_ISDIR (filestat.st_mode))
513 { 515 {
514 LOG(GNUNET_ERROR_TYPE_INFO, 516 LOG (GNUNET_ERROR_TYPE_INFO,
515 "A file already exits with the same name %s\n", 517 "A file already exits with the same name %s\n",
516 fil); 518 fil);
517 return GNUNET_NO; 519 return GNUNET_NO;
518 } 520 }
519 if (GNUNET_YES == is_readable) 521 if (GNUNET_YES == is_readable)
520 ret = access(fil, R_OK | X_OK); 522 ret = access (fil, R_OK | X_OK);
521 else 523 else
522 ret = access(fil, X_OK); 524 ret = access (fil, X_OK);
523 if (ret < 0) 525 if (ret < 0)
524 { 526 {
525 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", fil); 527 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
526 return GNUNET_NO; 528 return GNUNET_NO;
527 } 529 }
528 return GNUNET_YES; 530 return GNUNET_YES;
529} 531}
530 532
@@ -538,40 +540,40 @@ GNUNET_DISK_directory_test(const char *fil, int is_readable)
538 * else (will print an error message in that case, too). 540 * else (will print an error message in that case, too).
539 */ 541 */
540int 542int
541GNUNET_DISK_file_test(const char *fil) 543GNUNET_DISK_file_test (const char *fil)
542{ 544{
543 struct stat filestat; 545 struct stat filestat;
544 int ret; 546 int ret;
545 char *rdir; 547 char *rdir;
546 548
547 rdir = GNUNET_STRINGS_filename_expand(fil); 549 rdir = GNUNET_STRINGS_filename_expand (fil);
548 if (rdir == NULL) 550 if (rdir == NULL)
549 return GNUNET_SYSERR; 551 return GNUNET_SYSERR;
550 552
551 ret = stat(rdir, &filestat); 553 ret = stat (rdir, &filestat);
552 if (ret != 0) 554 if (ret != 0)
555 {
556 if (errno != ENOENT)
553 { 557 {
554 if (errno != ENOENT) 558 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
555 { 559 GNUNET_free (rdir);
556 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
557 GNUNET_free(rdir);
558 return GNUNET_SYSERR;
559 }
560 GNUNET_free(rdir);
561 return GNUNET_NO;
562 }
563 if (!S_ISREG(filestat.st_mode))
564 {
565 GNUNET_free(rdir);
566 return GNUNET_NO;
567 }
568 if (access(rdir, F_OK) < 0)
569 {
570 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", rdir);
571 GNUNET_free(rdir);
572 return GNUNET_SYSERR; 560 return GNUNET_SYSERR;
573 } 561 }
574 GNUNET_free(rdir); 562 GNUNET_free (rdir);
563 return GNUNET_NO;
564 }
565 if (! S_ISREG (filestat.st_mode))
566 {
567 GNUNET_free (rdir);
568 return GNUNET_NO;
569 }
570 if (access (rdir, F_OK) < 0)
571 {
572 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
573 GNUNET_free (rdir);
574 return GNUNET_SYSERR;
575 }
576 GNUNET_free (rdir);
575 return GNUNET_YES; 577 return GNUNET_YES;
576} 578}
577 579
@@ -583,7 +585,7 @@ GNUNET_DISK_file_test(const char *fil)
583 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 585 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
584 */ 586 */
585int 587int
586GNUNET_DISK_directory_create(const char *dir) 588GNUNET_DISK_directory_create (const char *dir)
587{ 589{
588 char *rdir; 590 char *rdir;
589 unsigned int len; 591 unsigned int len;
@@ -591,14 +593,14 @@ GNUNET_DISK_directory_create(const char *dir)
591 unsigned int pos2; 593 unsigned int pos2;
592 int ret = GNUNET_OK; 594 int ret = GNUNET_OK;
593 595
594 rdir = GNUNET_STRINGS_filename_expand(dir); 596 rdir = GNUNET_STRINGS_filename_expand (dir);
595 if (rdir == NULL) 597 if (rdir == NULL)
596 { 598 {
597 GNUNET_break(0); 599 GNUNET_break (0);
598 return GNUNET_SYSERR; 600 return GNUNET_SYSERR;
599 } 601 }
600 602
601 len = strlen(rdir); 603 len = strlen (rdir);
602 604
603 pos = 1; /* skip heading '/' */ 605 pos = 1; /* skip heading '/' */
604 606
@@ -606,64 +608,64 @@ GNUNET_DISK_directory_create(const char *dir)
606 pos2 = len; 608 pos2 = len;
607 rdir[len] = DIR_SEPARATOR; 609 rdir[len] = DIR_SEPARATOR;
608 while (pos <= pos2) 610 while (pos <= pos2)
611 {
612 if (DIR_SEPARATOR == rdir[pos2])
609 { 613 {
610 if (DIR_SEPARATOR == rdir[pos2]) 614 rdir[pos2] = '\0';
611 { 615 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
612 rdir[pos2] = '\0'; 616 if (GNUNET_NO == ret)
613 ret = GNUNET_DISK_directory_test(rdir, GNUNET_NO); 617 {
614 if (GNUNET_NO == ret) 618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
615 { 619 "Creating directory `%s' failed",
616 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 620 rdir);
617 "Creating directory `%s' failed", 621 GNUNET_free (rdir);
618 rdir); 622 return GNUNET_SYSERR;
619 GNUNET_free(rdir); 623 }
620 return GNUNET_SYSERR; 624 rdir[pos2] = DIR_SEPARATOR;
621 } 625 if (GNUNET_YES == ret)
622 rdir[pos2] = DIR_SEPARATOR; 626 {
623 if (GNUNET_YES == ret) 627 pos2++;
624 { 628 break;
625 pos2++; 629 }
626 break;
627 }
628 }
629 pos2--;
630 } 630 }
631 pos2--;
632 }
631 rdir[len] = '\0'; 633 rdir[len] = '\0';
632 if (pos < pos2) 634 if (pos < pos2)
633 pos = pos2; 635 pos = pos2;
634 /* Start creating directories */ 636 /* Start creating directories */
635 while (pos <= len) 637 while (pos <= len)
638 {
639 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
636 { 640 {
637 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) 641 rdir[pos] = '\0';
642 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
643 if (GNUNET_NO == ret)
644 {
645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
646 "Creating directory `%s' failed",
647 rdir);
648 GNUNET_free (rdir);
649 return GNUNET_SYSERR;
650 }
651 if (GNUNET_SYSERR == ret)
652 {
653 ret = mkdir (rdir,
654 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH
655 | S_IXOTH); /* 755 */
656
657 if ((ret != 0) && (errno != EEXIST))
638 { 658 {
639 rdir[pos] = '\0'; 659 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
640 ret = GNUNET_DISK_directory_test(rdir, GNUNET_NO); 660 GNUNET_free (rdir);
641 if (GNUNET_NO == ret) 661 return GNUNET_SYSERR;
642 {
643 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
644 "Creating directory `%s' failed",
645 rdir);
646 GNUNET_free(rdir);
647 return GNUNET_SYSERR;
648 }
649 if (GNUNET_SYSERR == ret)
650 {
651 ret = mkdir(rdir,
652 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH |
653 S_IXOTH); /* 755 */
654
655 if ((ret != 0) && (errno != EEXIST))
656 {
657 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
658 GNUNET_free(rdir);
659 return GNUNET_SYSERR;
660 }
661 }
662 rdir[pos] = DIR_SEPARATOR;
663 } 662 }
664 pos++; 663 }
664 rdir[pos] = DIR_SEPARATOR;
665 } 665 }
666 GNUNET_free(rdir); 666 pos++;
667 }
668 GNUNET_free (rdir);
667 return GNUNET_OK; 669 return GNUNET_OK;
668} 670}
669 671
@@ -678,40 +680,40 @@ GNUNET_DISK_directory_create(const char *dir)
678 * exists but is not writeable for us 680 * exists but is not writeable for us
679 */ 681 */
680int 682int
681GNUNET_DISK_directory_create_for_file(const char *filename) 683GNUNET_DISK_directory_create_for_file (const char *filename)
682{ 684{
683 char *rdir; 685 char *rdir;
684 size_t len; 686 size_t len;
685 int ret; 687 int ret;
686 int eno; 688 int eno;
687 689
688 rdir = GNUNET_STRINGS_filename_expand(filename); 690 rdir = GNUNET_STRINGS_filename_expand (filename);
689 if (NULL == rdir) 691 if (NULL == rdir)
690 { 692 {
691 errno = EINVAL; 693 errno = EINVAL;
692 return GNUNET_SYSERR; 694 return GNUNET_SYSERR;
693 } 695 }
694 if (0 == access(rdir, W_OK)) 696 if (0 == access (rdir, W_OK))
695 { 697 {
696 GNUNET_free(rdir); 698 GNUNET_free (rdir);
697 return GNUNET_OK; 699 return GNUNET_OK;
698 } 700 }
699 701
700 len = strlen(rdir); 702 len = strlen (rdir);
701 while ((len > 0) && (rdir[len] != DIR_SEPARATOR)) 703 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
702 len--; 704 len--;
703 rdir[len] = '\0'; 705 rdir[len] = '\0';
704 /* The empty path is invalid and in this case refers to / */ 706 /* The empty path is invalid and in this case refers to / */
705 if (0 == len) 707 if (0 == len)
706 { 708 {
707 GNUNET_free(rdir); 709 GNUNET_free (rdir);
708 rdir = GNUNET_strdup("/"); 710 rdir = GNUNET_strdup ("/");
709 } 711 }
710 ret = GNUNET_DISK_directory_create(rdir); 712 ret = GNUNET_DISK_directory_create (rdir);
711 if ((GNUNET_OK == ret) && (0 != access(rdir, W_OK))) 713 if ((GNUNET_OK == ret) && (0 != access (rdir, W_OK)))
712 ret = GNUNET_NO; 714 ret = GNUNET_NO;
713 eno = errno; 715 eno = errno;
714 GNUNET_free(rdir); 716 GNUNET_free (rdir);
715 errno = eno; 717 errno = eno;
716 return ret; 718 return ret;
717} 719}
@@ -726,17 +728,17 @@ GNUNET_DISK_directory_create_for_file(const char *filename)
726 * @return the number of bytes read on success, #GNUNET_SYSERR on failure 728 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
727 */ 729 */
728ssize_t 730ssize_t
729GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, 731GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
730 void *result, 732 void *result,
731 size_t len) 733 size_t len)
732{ 734{
733 if (NULL == h) 735 if (NULL == h)
734 { 736 {
735 errno = EINVAL; 737 errno = EINVAL;
736 return GNUNET_SYSERR; 738 return GNUNET_SYSERR;
737 } 739 }
738 740
739 return read(h->fd, result, len); 741 return read (h->fd, result, len);
740} 742}
741 743
742 744
@@ -751,30 +753,30 @@ GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h,
751 * @return the number of bytes read on success, #GNUNET_SYSERR on failure 753 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
752 */ 754 */
753ssize_t 755ssize_t
754GNUNET_DISK_file_read_non_blocking(const struct GNUNET_DISK_FileHandle *h, 756GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
755 void *result, 757 void *result,
756 size_t len) 758 size_t len)
757{ 759{
758 if (NULL == h) 760 if (NULL == h)
759 { 761 {
760 errno = EINVAL; 762 errno = EINVAL;
761 return GNUNET_SYSERR; 763 return GNUNET_SYSERR;
762 } 764 }
763 765
764 int flags; 766 int flags;
765 ssize_t ret; 767 ssize_t ret;
766 768
767 /* set to non-blocking, read, then set back */ 769 /* set to non-blocking, read, then set back */
768 flags = fcntl(h->fd, F_GETFL); 770 flags = fcntl (h->fd, F_GETFL);
769 if (0 == (flags & O_NONBLOCK)) 771 if (0 == (flags & O_NONBLOCK))
770 (void)fcntl(h->fd, F_SETFL, flags | O_NONBLOCK); 772 (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
771 ret = read(h->fd, result, len); 773 ret = read (h->fd, result, len);
772 if (0 == (flags & O_NONBLOCK)) 774 if (0 == (flags & O_NONBLOCK))
773 { 775 {
774 int eno = errno; 776 int eno = errno;
775 (void)fcntl(h->fd, F_SETFL, flags); 777 (void) fcntl (h->fd, F_SETFL, flags);
776 errno = eno; 778 errno = eno;
777 } 779 }
778 return ret; 780 return ret;
779} 781}
780 782
@@ -788,18 +790,18 @@ GNUNET_DISK_file_read_non_blocking(const struct GNUNET_DISK_FileHandle *h,
788 * @return number of bytes read, #GNUNET_SYSERR on failure 790 * @return number of bytes read, #GNUNET_SYSERR on failure
789 */ 791 */
790ssize_t 792ssize_t
791GNUNET_DISK_fn_read(const char *fn, void *result, size_t len) 793GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
792{ 794{
793 struct GNUNET_DISK_FileHandle *fh; 795 struct GNUNET_DISK_FileHandle *fh;
794 ssize_t ret; 796 ssize_t ret;
795 int eno; 797 int eno;
796 798
797 fh = GNUNET_DISK_file_open(fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); 799 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
798 if (NULL == fh) 800 if (NULL == fh)
799 return GNUNET_SYSERR; 801 return GNUNET_SYSERR;
800 ret = GNUNET_DISK_file_read(fh, result, len); 802 ret = GNUNET_DISK_file_read (fh, result, len);
801 eno = errno; 803 eno = errno;
802 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); 804 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
803 errno = eno; 805 errno = eno;
804 return ret; 806 return ret;
805} 807}
@@ -814,18 +816,18 @@ GNUNET_DISK_fn_read(const char *fn, void *result, size_t len)
814 * @return number of bytes written on success, #GNUNET_SYSERR on error 816 * @return number of bytes written on success, #GNUNET_SYSERR on error
815 */ 817 */
816ssize_t 818ssize_t
817GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, 819GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
818 const void *buffer, 820 const void *buffer,
819 size_t n) 821 size_t n)
820{ 822{
821 if (NULL == h) 823 if (NULL == h)
822 { 824 {
823 errno = EINVAL; 825 errno = EINVAL;
824 return GNUNET_SYSERR; 826 return GNUNET_SYSERR;
825 } 827 }
826 828
827 829
828 return write(h->fd, buffer, n); 830 return write (h->fd, buffer, n);
829} 831}
830 832
831 833
@@ -838,27 +840,27 @@ GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h,
838 * @return number of bytes written on success, #GNUNET_SYSERR on error 840 * @return number of bytes written on success, #GNUNET_SYSERR on error
839 */ 841 */
840ssize_t 842ssize_t
841GNUNET_DISK_file_write_blocking(const struct GNUNET_DISK_FileHandle *h, 843GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
842 const void *buffer, 844 const void *buffer,
843 size_t n) 845 size_t n)
844{ 846{
845 if (NULL == h) 847 if (NULL == h)
846 { 848 {
847 errno = EINVAL; 849 errno = EINVAL;
848 return GNUNET_SYSERR; 850 return GNUNET_SYSERR;
849 } 851 }
850 852
851 853
852 int flags; 854 int flags;
853 ssize_t ret; 855 ssize_t ret;
854 856
855 /* set to blocking, write, then set back */ 857 /* set to blocking, write, then set back */
856 flags = fcntl(h->fd, F_GETFL); 858 flags = fcntl (h->fd, F_GETFL);
857 if (0 != (flags & O_NONBLOCK)) 859 if (0 != (flags & O_NONBLOCK))
858 (void)fcntl(h->fd, F_SETFL, flags - O_NONBLOCK); 860 (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK);
859 ret = write(h->fd, buffer, n); 861 ret = write (h->fd, buffer, n);
860 if (0 == (flags & O_NONBLOCK)) 862 if (0 == (flags & O_NONBLOCK))
861 (void)fcntl(h->fd, F_SETFL, flags); 863 (void) fcntl (h->fd, F_SETFL, flags);
862 return ret; 864 return ret;
863} 865}
864 866
@@ -874,23 +876,23 @@ GNUNET_DISK_file_write_blocking(const struct GNUNET_DISK_FileHandle *h,
874 * @return number of bytes written on success, #GNUNET_SYSERR on error 876 * @return number of bytes written on success, #GNUNET_SYSERR on error
875 */ 877 */
876ssize_t 878ssize_t
877GNUNET_DISK_fn_write(const char *fn, 879GNUNET_DISK_fn_write (const char *fn,
878 const void *buffer, 880 const void *buffer,
879 size_t n, 881 size_t n,
880 enum GNUNET_DISK_AccessPermissions mode) 882 enum GNUNET_DISK_AccessPermissions mode)
881{ 883{
882 struct GNUNET_DISK_FileHandle *fh; 884 struct GNUNET_DISK_FileHandle *fh;
883 ssize_t ret; 885 ssize_t ret;
884 886
885 fh = 887 fh =
886 GNUNET_DISK_file_open(fn, 888 GNUNET_DISK_file_open (fn,
887 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | 889 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
888 GNUNET_DISK_OPEN_CREATE, 890 | GNUNET_DISK_OPEN_CREATE,
889 mode); 891 mode);
890 if (!fh) 892 if (! fh)
891 return GNUNET_SYSERR; 893 return GNUNET_SYSERR;
892 ret = GNUNET_DISK_file_write(fh, buffer, n); 894 ret = GNUNET_DISK_file_write (fh, buffer, n);
893 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); 895 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
894 return ret; 896 return ret;
895} 897}
896 898
@@ -906,9 +908,9 @@ GNUNET_DISK_fn_write(const char *fn,
906 * ieration aborted by callback returning #GNUNET_SYSERR 908 * ieration aborted by callback returning #GNUNET_SYSERR
907 */ 909 */
908int 910int
909GNUNET_DISK_directory_scan(const char *dir_name, 911GNUNET_DISK_directory_scan (const char *dir_name,
910 GNUNET_FileNameCallback callback, 912 GNUNET_FileNameCallback callback,
911 void *callback_cls) 913 void *callback_cls)
912{ 914{
913 DIR *dinfo; 915 DIR *dinfo;
914 struct dirent *finfo; 916 struct dirent *finfo;
@@ -920,80 +922,80 @@ GNUNET_DISK_directory_scan(const char *dir_name,
920 unsigned int name_len; 922 unsigned int name_len;
921 unsigned int n_size; 923 unsigned int n_size;
922 924
923 GNUNET_assert(NULL != dir_name); 925 GNUNET_assert (NULL != dir_name);
924 dname = GNUNET_STRINGS_filename_expand(dir_name); 926 dname = GNUNET_STRINGS_filename_expand (dir_name);
925 if (NULL == dname) 927 if (NULL == dname)
926 return GNUNET_SYSERR; 928 return GNUNET_SYSERR;
927 while ((strlen(dname) > 0) && (dname[strlen(dname) - 1] == DIR_SEPARATOR)) 929 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
928 dname[strlen(dname) - 1] = '\0'; 930 dname[strlen (dname) - 1] = '\0';
929 if (0 != stat(dname, &istat)) 931 if (0 != stat (dname, &istat))
930 { 932 {
931 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", dname); 933 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
932 GNUNET_free(dname); 934 GNUNET_free (dname);
933 return GNUNET_SYSERR; 935 return GNUNET_SYSERR;
934 } 936 }
935 if (!S_ISDIR(istat.st_mode)) 937 if (! S_ISDIR (istat.st_mode))
936 { 938 {
937 LOG(GNUNET_ERROR_TYPE_WARNING, 939 LOG (GNUNET_ERROR_TYPE_WARNING,
938 _("Expected `%s' to be a directory!\n"), 940 _ ("Expected `%s' to be a directory!\n"),
939 dir_name); 941 dir_name);
940 GNUNET_free(dname); 942 GNUNET_free (dname);
941 return GNUNET_SYSERR; 943 return GNUNET_SYSERR;
942 } 944 }
943 errno = 0; 945 errno = 0;
944 dinfo = opendir(dname); 946 dinfo = opendir (dname);
945 if ((EACCES == errno) || (NULL == dinfo)) 947 if ((EACCES == errno) || (NULL == dinfo))
946 { 948 {
947 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "opendir", dname); 949 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
948 if (NULL != dinfo) 950 if (NULL != dinfo)
949 closedir(dinfo); 951 closedir (dinfo);
950 GNUNET_free(dname); 952 GNUNET_free (dname);
951 return GNUNET_SYSERR; 953 return GNUNET_SYSERR;
952 } 954 }
953 name_len = 256; 955 name_len = 256;
954 n_size = strlen(dname) + name_len + strlen(DIR_SEPARATOR_STR) + 1; 956 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
955 name = GNUNET_malloc(n_size); 957 name = GNUNET_malloc (n_size);
956 while (NULL != (finfo = readdir(dinfo))) 958 while (NULL != (finfo = readdir (dinfo)))
959 {
960 if ((0 == strcmp (finfo->d_name, ".")) ||
961 (0 == strcmp (finfo->d_name, "..")))
962 continue;
963 if (NULL != callback)
957 { 964 {
958 if ((0 == strcmp(finfo->d_name, ".")) || 965 if (name_len < strlen (finfo->d_name))
959 (0 == strcmp(finfo->d_name, ".."))) 966 {
960 continue; 967 GNUNET_free (name);
961 if (NULL != callback) 968 name_len = strlen (finfo->d_name);
962 { 969 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
963 if (name_len < strlen(finfo->d_name)) 970 name = GNUNET_malloc (n_size);
964 { 971 }
965 GNUNET_free(name); 972 /* dname can end in "/" only if dname == "/";
966 name_len = strlen(finfo->d_name); 973 * if dname does not end in "/", we need to add
967 n_size = strlen(dname) + name_len + strlen(DIR_SEPARATOR_STR) + 1; 974 * a "/" (otherwise, we must not!) */
968 name = GNUNET_malloc(n_size); 975 GNUNET_snprintf (name,
969 } 976 n_size,
970 /* dname can end in "/" only if dname == "/"; 977 "%s%s%s",
971 * if dname does not end in "/", we need to add 978 dname,
972 * a "/" (otherwise, we must not!) */ 979 (0 == strcmp (dname, DIR_SEPARATOR_STR))
973 GNUNET_snprintf(name, 980 ? ""
974 n_size, 981 : DIR_SEPARATOR_STR,
975 "%s%s%s", 982 finfo->d_name);
976 dname, 983 ret = callback (callback_cls, name);
977 (0 == strcmp(dname, DIR_SEPARATOR_STR)) 984 if (GNUNET_OK != ret)
978 ? "" 985 {
979 : DIR_SEPARATOR_STR, 986 closedir (dinfo);
980 finfo->d_name); 987 GNUNET_free (name);
981 ret = callback(callback_cls, name); 988 GNUNET_free (dname);
982 if (GNUNET_OK != ret) 989 if (GNUNET_NO == ret)
983 { 990 return count;
984 closedir(dinfo); 991 return GNUNET_SYSERR;
985 GNUNET_free(name); 992 }
986 GNUNET_free(dname);
987 if (GNUNET_NO == ret)
988 return count;
989 return GNUNET_SYSERR;
990 }
991 }
992 count++;
993 } 993 }
994 closedir(dinfo); 994 count++;
995 GNUNET_free(name); 995 }
996 GNUNET_free(dname); 996 closedir (dinfo);
997 GNUNET_free (name);
998 GNUNET_free (dname);
997 return count; 999 return count;
998} 1000}
999 1001
@@ -1007,10 +1009,10 @@ GNUNET_DISK_directory_scan(const char *dir_name,
1007 * @return #GNUNET_OK 1009 * @return #GNUNET_OK
1008 */ 1010 */
1009static int 1011static int
1010remove_helper(void *unused, const char *fn) 1012remove_helper (void *unused, const char *fn)
1011{ 1013{
1012 (void)unused; 1014 (void) unused;
1013 (void)GNUNET_DISK_directory_remove(fn); 1015 (void) GNUNET_DISK_directory_remove (fn);
1014 return GNUNET_OK; 1016 return GNUNET_OK;
1015} 1017}
1016 1018
@@ -1023,37 +1025,37 @@ remove_helper(void *unused, const char *fn)
1023 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1025 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1024 */ 1026 */
1025int 1027int
1026GNUNET_DISK_directory_remove(const char *filename) 1028GNUNET_DISK_directory_remove (const char *filename)
1027{ 1029{
1028 struct stat istat; 1030 struct stat istat;
1029 1031
1030 if (NULL == filename) 1032 if (NULL == filename)
1031 { 1033 {
1032 GNUNET_break(0); 1034 GNUNET_break (0);
1033 return GNUNET_SYSERR; 1035 return GNUNET_SYSERR;
1034 } 1036 }
1035 if (0 != lstat(filename, &istat)) 1037 if (0 != lstat (filename, &istat))
1036 return GNUNET_NO; /* file may not exist... */ 1038 return GNUNET_NO; /* file may not exist... */
1037 (void)chmod(filename, S_IWUSR | S_IRUSR | S_IXUSR); 1039 (void) chmod (filename, S_IWUSR | S_IRUSR | S_IXUSR);
1038 if (0 == unlink(filename)) 1040 if (0 == unlink (filename))
1039 return GNUNET_OK; 1041 return GNUNET_OK;
1040 if ((errno != EISDIR) && 1042 if ((errno != EISDIR) &&
1041 /* EISDIR is not sufficient in all cases, e.g. 1043 /* EISDIR is not sufficient in all cases, e.g.
1042 * sticky /tmp directory may result in EPERM on BSD. 1044 * sticky /tmp directory may result in EPERM on BSD.
1043 * So we also explicitly check "isDirectory" */ 1045 * So we also explicitly check "isDirectory" */
1044 (GNUNET_YES != GNUNET_DISK_directory_test(filename, GNUNET_YES))) 1046 (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES)))
1045 { 1047 {
1046 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); 1048 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1047 return GNUNET_SYSERR; 1049 return GNUNET_SYSERR;
1048 } 1050 }
1049 if (GNUNET_SYSERR == 1051 if (GNUNET_SYSERR ==
1050 GNUNET_DISK_directory_scan(filename, &remove_helper, NULL)) 1052 GNUNET_DISK_directory_scan (filename, &remove_helper, NULL))
1051 return GNUNET_SYSERR; 1053 return GNUNET_SYSERR;
1052 if (0 != rmdir(filename)) 1054 if (0 != rmdir (filename))
1053 { 1055 {
1054 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); 1056 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1055 return GNUNET_SYSERR; 1057 return GNUNET_SYSERR;
1056 } 1058 }
1057 return GNUNET_OK; 1059 return GNUNET_OK;
1058} 1060}
1059 1061
@@ -1066,7 +1068,7 @@ GNUNET_DISK_directory_remove(const char *filename)
1066 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1068 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1067 */ 1069 */
1068int 1070int
1069GNUNET_DISK_file_copy(const char *src, const char *dst) 1071GNUNET_DISK_file_copy (const char *src, const char *dst)
1070{ 1072{
1071 char *buf; 1073 char *buf;
1072 uint64_t pos; 1074 uint64_t pos;
@@ -1076,55 +1078,55 @@ GNUNET_DISK_file_copy(const char *src, const char *dst)
1076 struct GNUNET_DISK_FileHandle *in; 1078 struct GNUNET_DISK_FileHandle *in;
1077 struct GNUNET_DISK_FileHandle *out; 1079 struct GNUNET_DISK_FileHandle *out;
1078 1080
1079 if (GNUNET_OK != GNUNET_DISK_file_size(src, &size, GNUNET_YES, GNUNET_YES)) 1081 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
1080 { 1082 {
1081 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "stat", src); 1083 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "stat", src);
1082 return GNUNET_SYSERR; 1084 return GNUNET_SYSERR;
1083 } 1085 }
1084 pos = 0; 1086 pos = 0;
1085 in = 1087 in =
1086 GNUNET_DISK_file_open(src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); 1088 GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
1087 if (!in) 1089 if (! in)
1088 { 1090 {
1089 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", src); 1091 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", src);
1090 return GNUNET_SYSERR; 1092 return GNUNET_SYSERR;
1091 } 1093 }
1092 out = 1094 out =
1093 GNUNET_DISK_file_open(dst, 1095 GNUNET_DISK_file_open (dst,
1094 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 1096 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
1095 GNUNET_DISK_OPEN_FAILIFEXISTS, 1097 | GNUNET_DISK_OPEN_FAILIFEXISTS,
1096 GNUNET_DISK_PERM_USER_READ | 1098 GNUNET_DISK_PERM_USER_READ
1097 GNUNET_DISK_PERM_USER_WRITE | 1099 | GNUNET_DISK_PERM_USER_WRITE
1098 GNUNET_DISK_PERM_GROUP_READ | 1100 | GNUNET_DISK_PERM_GROUP_READ
1099 GNUNET_DISK_PERM_GROUP_WRITE); 1101 | GNUNET_DISK_PERM_GROUP_WRITE);
1100 if (!out) 1102 if (! out)
1101 { 1103 {
1102 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", dst); 1104 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", dst);
1103 GNUNET_DISK_file_close(in); 1105 GNUNET_DISK_file_close (in);
1104 return GNUNET_SYSERR; 1106 return GNUNET_SYSERR;
1105 } 1107 }
1106 buf = GNUNET_malloc(COPY_BLK_SIZE); 1108 buf = GNUNET_malloc (COPY_BLK_SIZE);
1107 while (pos < size) 1109 while (pos < size)
1108 { 1110 {
1109 len = COPY_BLK_SIZE; 1111 len = COPY_BLK_SIZE;
1110 if (len > size - pos) 1112 if (len > size - pos)
1111 len = size - pos; 1113 len = size - pos;
1112 sret = GNUNET_DISK_file_read(in, buf, len); 1114 sret = GNUNET_DISK_file_read (in, buf, len);
1113 if ((sret < 0) || (len != (size_t)sret)) 1115 if ((sret < 0) || (len != (size_t) sret))
1114 goto FAIL; 1116 goto FAIL;
1115 sret = GNUNET_DISK_file_write(out, buf, len); 1117 sret = GNUNET_DISK_file_write (out, buf, len);
1116 if ((sret < 0) || (len != (size_t)sret)) 1118 if ((sret < 0) || (len != (size_t) sret))
1117 goto FAIL; 1119 goto FAIL;
1118 pos += len; 1120 pos += len;
1119 } 1121 }
1120 GNUNET_free(buf); 1122 GNUNET_free (buf);
1121 GNUNET_DISK_file_close(in); 1123 GNUNET_DISK_file_close (in);
1122 GNUNET_DISK_file_close(out); 1124 GNUNET_DISK_file_close (out);
1123 return GNUNET_OK; 1125 return GNUNET_OK;
1124FAIL: 1126FAIL:
1125 GNUNET_free(buf); 1127 GNUNET_free (buf);
1126 GNUNET_DISK_file_close(in); 1128 GNUNET_DISK_file_close (in);
1127 GNUNET_DISK_file_close(out); 1129 GNUNET_DISK_file_close (out);
1128 return GNUNET_SYSERR; 1130 return GNUNET_SYSERR;
1129} 1131}
1130 1132
@@ -1134,21 +1136,23 @@ FAIL:
1134 * @param fn the filename to canonicalize 1136 * @param fn the filename to canonicalize
1135 */ 1137 */
1136void 1138void
1137GNUNET_DISK_filename_canonicalize(char *fn) 1139GNUNET_DISK_filename_canonicalize (char *fn)
1138{ 1140{
1139 char *idx; 1141 char *idx;
1140 char c; 1142 char c;
1141 1143
1142 for (idx = fn; *idx; idx++) 1144 for (idx = fn; *idx; idx++)
1143 { 1145 {
1144 c = *idx; 1146 c = *idx;
1145 1147
1146 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || 1148 if ((c == '/') ||(c == '\\') ||(c == ':') ||(c == '*') ||(c == '?') ||(c ==
1147 c == '<' || c == '>' || c == '|') 1149 '"')
1148 { 1150 ||
1149 *idx = '_'; 1151 (c == '<') ||(c == '>') ||(c == '|') )
1150 } 1152 {
1153 *idx = '_';
1151 } 1154 }
1155 }
1152} 1156}
1153 1157
1154 1158
@@ -1160,24 +1164,24 @@ GNUNET_DISK_filename_canonicalize(char *fn)
1160 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 1164 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1161 */ 1165 */
1162int 1166int
1163GNUNET_DISK_file_change_owner(const char *filename, const char *user) 1167GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1164{ 1168{
1165 struct passwd *pws; 1169 struct passwd *pws;
1166 1170
1167 pws = getpwnam(user); 1171 pws = getpwnam (user);
1168 if (NULL == pws) 1172 if (NULL == pws)
1169 { 1173 {
1170 LOG(GNUNET_ERROR_TYPE_ERROR, 1174 LOG (GNUNET_ERROR_TYPE_ERROR,
1171 _("Cannot obtain information about user `%s': %s\n"), 1175 _ ("Cannot obtain information about user `%s': %s\n"),
1172 user, 1176 user,
1173 strerror(errno)); 1177 strerror (errno));
1174 return GNUNET_SYSERR; 1178 return GNUNET_SYSERR;
1175 } 1179 }
1176 if (0 != chown(filename, pws->pw_uid, pws->pw_gid)) 1180 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1177 { 1181 {
1178 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "chown", filename); 1182 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1179 return GNUNET_SYSERR; 1183 return GNUNET_SYSERR;
1180 } 1184 }
1181 return GNUNET_OK; 1185 return GNUNET_OK;
1182} 1186}
1183 1187
@@ -1192,26 +1196,26 @@ GNUNET_DISK_file_change_owner(const char *filename, const char *user)
1192 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1196 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1193 */ 1197 */
1194int 1198int
1195GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, 1199GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
1196 off_t lock_start, 1200 off_t lock_start,
1197 off_t lock_end, 1201 off_t lock_end,
1198 int excl) 1202 int excl)
1199{ 1203{
1200 if (fh == NULL) 1204 if (fh == NULL)
1201 { 1205 {
1202 errno = EINVAL; 1206 errno = EINVAL;
1203 return GNUNET_SYSERR; 1207 return GNUNET_SYSERR;
1204 } 1208 }
1205 1209
1206 struct flock fl; 1210 struct flock fl;
1207 1211
1208 memset(&fl, 0, sizeof(struct flock)); 1212 memset (&fl, 0, sizeof(struct flock));
1209 fl.l_type = excl ? F_WRLCK : F_RDLCK; 1213 fl.l_type = excl ? F_WRLCK : F_RDLCK;
1210 fl.l_whence = SEEK_SET; 1214 fl.l_whence = SEEK_SET;
1211 fl.l_start = lock_start; 1215 fl.l_start = lock_start;
1212 fl.l_len = lock_end; 1216 fl.l_len = lock_end;
1213 1217
1214 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; 1218 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1215} 1219}
1216 1220
1217 1221
@@ -1224,25 +1228,25 @@ GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh,
1224 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1228 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1225 */ 1229 */
1226int 1230int
1227GNUNET_DISK_file_unlock(struct GNUNET_DISK_FileHandle *fh, 1231GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
1228 off_t unlock_start, 1232 off_t unlock_start,
1229 off_t unlock_end) 1233 off_t unlock_end)
1230{ 1234{
1231 if (fh == NULL) 1235 if (fh == NULL)
1232 { 1236 {
1233 errno = EINVAL; 1237 errno = EINVAL;
1234 return GNUNET_SYSERR; 1238 return GNUNET_SYSERR;
1235 } 1239 }
1236 1240
1237 struct flock fl; 1241 struct flock fl;
1238 1242
1239 memset(&fl, 0, sizeof(struct flock)); 1243 memset (&fl, 0, sizeof(struct flock));
1240 fl.l_type = F_UNLCK; 1244 fl.l_type = F_UNLCK;
1241 fl.l_whence = SEEK_SET; 1245 fl.l_whence = SEEK_SET;
1242 fl.l_start = unlock_start; 1246 fl.l_start = unlock_start;
1243 fl.l_len = unlock_end; 1247 fl.l_len = unlock_end;
1244 1248
1245 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; 1249 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1246} 1250}
1247 1251
1248 1252
@@ -1259,9 +1263,9 @@ GNUNET_DISK_file_unlock(struct GNUNET_DISK_FileHandle *fh,
1259 * @return IO handle on success, NULL on error 1263 * @return IO handle on success, NULL on error
1260 */ 1264 */
1261struct GNUNET_DISK_FileHandle * 1265struct GNUNET_DISK_FileHandle *
1262GNUNET_DISK_file_open(const char *fn, 1266GNUNET_DISK_file_open (const char *fn,
1263 enum GNUNET_DISK_OpenFlags flags, 1267 enum GNUNET_DISK_OpenFlags flags,
1264 enum GNUNET_DISK_AccessPermissions perm) 1268 enum GNUNET_DISK_AccessPermissions perm)
1265{ 1269{
1266 char *expfn; 1270 char *expfn;
1267 struct GNUNET_DISK_FileHandle *ret; 1271 struct GNUNET_DISK_FileHandle *ret;
@@ -1270,7 +1274,7 @@ GNUNET_DISK_file_open(const char *fn,
1270 int mode; 1274 int mode;
1271 int fd; 1275 int fd;
1272 1276
1273 expfn = GNUNET_STRINGS_filename_expand(fn); 1277 expfn = GNUNET_STRINGS_filename_expand (fn);
1274 if (NULL == expfn) 1278 if (NULL == expfn)
1275 return NULL; 1279 return NULL;
1276 1280
@@ -1282,49 +1286,49 @@ GNUNET_DISK_file_open(const char *fn,
1282 else if (flags & GNUNET_DISK_OPEN_WRITE) 1286 else if (flags & GNUNET_DISK_OPEN_WRITE)
1283 oflags = O_WRONLY; 1287 oflags = O_WRONLY;
1284 else 1288 else
1285 { 1289 {
1286 GNUNET_break(0); 1290 GNUNET_break (0);
1287 GNUNET_free(expfn); 1291 GNUNET_free (expfn);
1288 return NULL; 1292 return NULL;
1289 } 1293 }
1290 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) 1294 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1291 oflags |= (O_CREAT | O_EXCL); 1295 oflags |= (O_CREAT | O_EXCL);
1292 if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1296 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1293 oflags |= O_TRUNC; 1297 oflags |= O_TRUNC;
1294 if (flags & GNUNET_DISK_OPEN_APPEND) 1298 if (flags & GNUNET_DISK_OPEN_APPEND)
1295 oflags |= O_APPEND; 1299 oflags |= O_APPEND;
1296 if (GNUNET_NO == GNUNET_DISK_file_test(fn)) 1300 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
1301 {
1302 if (flags & GNUNET_DISK_OPEN_CREATE)
1297 { 1303 {
1298 if (flags & GNUNET_DISK_OPEN_CREATE) 1304 (void) GNUNET_DISK_directory_create_for_file (expfn);
1299 { 1305 oflags |= O_CREAT;
1300 (void)GNUNET_DISK_directory_create_for_file(expfn); 1306 mode = translate_unix_perms (perm);
1301 oflags |= O_CREAT;
1302 mode = translate_unix_perms(perm);
1303 }
1304 } 1307 }
1308 }
1305 1309
1306 fd = open(expfn, 1310 fd = open (expfn,
1307 oflags 1311 oflags
1308#if O_CLOEXEC 1312#if O_CLOEXEC
1309 | O_CLOEXEC 1313 | O_CLOEXEC
1310#endif 1314#endif
1311 | O_LARGEFILE, 1315 | O_LARGEFILE,
1312 mode); 1316 mode);
1313 if (fd == -1) 1317 if (fd == -1)
1314 { 1318 {
1315 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) 1319 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1316 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "open", expfn); 1320 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1317 else 1321 else
1318 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "open", expfn); 1322 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1319 GNUNET_free(expfn); 1323 GNUNET_free (expfn);
1320 return NULL; 1324 return NULL;
1321 } 1325 }
1322 1326
1323 ret = GNUNET_new(struct GNUNET_DISK_FileHandle); 1327 ret = GNUNET_new (struct GNUNET_DISK_FileHandle);
1324 1328
1325 ret->fd = fd; 1329 ret->fd = fd;
1326 1330
1327 GNUNET_free(expfn); 1331 GNUNET_free (expfn);
1328 return ret; 1332 return ret;
1329} 1333}
1330 1334
@@ -1336,25 +1340,25 @@ GNUNET_DISK_file_open(const char *fn,
1336 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 1340 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1337 */ 1341 */
1338int 1342int
1339GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h) 1343GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1340{ 1344{
1341 int ret; 1345 int ret;
1342 1346
1343 if (h == NULL) 1347 if (h == NULL)
1344 { 1348 {
1345 errno = EINVAL; 1349 errno = EINVAL;
1346 return GNUNET_SYSERR; 1350 return GNUNET_SYSERR;
1347 } 1351 }
1348 1352
1349 ret = GNUNET_OK; 1353 ret = GNUNET_OK;
1350 1354
1351 if (close(h->fd) != 0) 1355 if (close (h->fd) != 0)
1352 { 1356 {
1353 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "close"); 1357 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1354 ret = GNUNET_SYSERR; 1358 ret = GNUNET_SYSERR;
1355 } 1359 }
1356 1360
1357 GNUNET_free(h); 1361 GNUNET_free (h);
1358 return ret; 1362 return ret;
1359} 1363}
1360 1364
@@ -1366,14 +1370,14 @@ GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
1366 * @return file handle corresponding to the descriptor, NULL on error 1370 * @return file handle corresponding to the descriptor, NULL on error
1367 */ 1371 */
1368struct GNUNET_DISK_FileHandle * 1372struct GNUNET_DISK_FileHandle *
1369GNUNET_DISK_get_handle_from_int_fd(int fno) 1373GNUNET_DISK_get_handle_from_int_fd (int fno)
1370{ 1374{
1371 struct GNUNET_DISK_FileHandle *fh; 1375 struct GNUNET_DISK_FileHandle *fh;
1372 1376
1373 if ((((off_t)-1) == lseek(fno, 0, SEEK_CUR)) && (EBADF == errno)) 1377 if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno))
1374 return NULL; /* invalid FD */ 1378 return NULL; /* invalid FD */
1375 1379
1376 fh = GNUNET_new(struct GNUNET_DISK_FileHandle); 1380 fh = GNUNET_new (struct GNUNET_DISK_FileHandle);
1377 1381
1378 fh->fd = fno; 1382 fh->fd = fno;
1379 1383
@@ -1388,22 +1392,23 @@ GNUNET_DISK_get_handle_from_int_fd(int fno)
1388 * @return file handle corresponding to the descriptor 1392 * @return file handle corresponding to the descriptor
1389 */ 1393 */
1390struct GNUNET_DISK_FileHandle * 1394struct GNUNET_DISK_FileHandle *
1391GNUNET_DISK_get_handle_from_native(FILE *fd) 1395GNUNET_DISK_get_handle_from_native (FILE *fd)
1392{ 1396{
1393 int fno; 1397 int fno;
1394 1398
1395 fno = fileno(fd); 1399 fno = fileno (fd);
1396 if (-1 == fno) 1400 if (-1 == fno)
1397 return NULL; 1401 return NULL;
1398 1402
1399 return GNUNET_DISK_get_handle_from_int_fd(fno); 1403 return GNUNET_DISK_get_handle_from_int_fd (fno);
1400} 1404}
1401 1405
1402 1406
1403/** 1407/**
1404 * Handle for a memory-mapping operation. 1408 * Handle for a memory-mapping operation.
1405 */ 1409 */
1406struct GNUNET_DISK_MapHandle { 1410struct GNUNET_DISK_MapHandle
1411{
1407 /** 1412 /**
1408 * Address where the map is in memory. 1413 * Address where the map is in memory.
1409 */ 1414 */
@@ -1417,7 +1422,7 @@ struct GNUNET_DISK_MapHandle {
1417 1422
1418 1423
1419#ifndef MAP_FAILED 1424#ifndef MAP_FAILED
1420#define MAP_FAILED ((void *)-1) 1425#define MAP_FAILED ((void *) -1)
1421#endif 1426#endif
1422 1427
1423/** 1428/**
@@ -1430,16 +1435,16 @@ struct GNUNET_DISK_MapHandle {
1430 * @return pointer to the mapped memory region, NULL on failure 1435 * @return pointer to the mapped memory region, NULL on failure
1431 */ 1436 */
1432void * 1437void *
1433GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, 1438GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1434 struct GNUNET_DISK_MapHandle **m, 1439 struct GNUNET_DISK_MapHandle **m,
1435 enum GNUNET_DISK_MapType access, 1440 enum GNUNET_DISK_MapType access,
1436 size_t len) 1441 size_t len)
1437{ 1442{
1438 if (NULL == h) 1443 if (NULL == h)
1439 { 1444 {
1440 errno = EINVAL; 1445 errno = EINVAL;
1441 return NULL; 1446 return NULL;
1442 } 1447 }
1443 1448
1444 int prot; 1449 int prot;
1445 1450
@@ -1448,14 +1453,14 @@ GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h,
1448 prot = PROT_READ; 1453 prot = PROT_READ;
1449 if (access & GNUNET_DISK_MAP_TYPE_WRITE) 1454 if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1450 prot |= PROT_WRITE; 1455 prot |= PROT_WRITE;
1451 *m = GNUNET_new(struct GNUNET_DISK_MapHandle); 1456 *m = GNUNET_new (struct GNUNET_DISK_MapHandle);
1452 (*m)->addr = mmap(NULL, len, prot, MAP_SHARED, h->fd, 0); 1457 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1453 GNUNET_assert(NULL != (*m)->addr); 1458 GNUNET_assert (NULL != (*m)->addr);
1454 if (MAP_FAILED == (*m)->addr) 1459 if (MAP_FAILED == (*m)->addr)
1455 { 1460 {
1456 GNUNET_free(*m); 1461 GNUNET_free (*m);
1457 return NULL; 1462 return NULL;
1458 } 1463 }
1459 (*m)->len = len; 1464 (*m)->len = len;
1460 return (*m)->addr; 1465 return (*m)->addr;
1461} 1466}
@@ -1466,19 +1471,19 @@ GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h,
1466 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1471 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1467 */ 1472 */
1468int 1473int
1469GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h) 1474GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1470{ 1475{
1471 int ret; 1476 int ret;
1472 1477
1473 if (h == NULL) 1478 if (h == NULL)
1474 { 1479 {
1475 errno = EINVAL; 1480 errno = EINVAL;
1476 return GNUNET_SYSERR; 1481 return GNUNET_SYSERR;
1477 } 1482 }
1478 1483
1479 ret = munmap(h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; 1484 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1480 1485
1481 GNUNET_free(h); 1486 GNUNET_free (h);
1482 return ret; 1487 return ret;
1483} 1488}
1484 1489
@@ -1489,18 +1494,18 @@ GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
1489 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1494 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1490 */ 1495 */
1491int 1496int
1492GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h) 1497GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1493{ 1498{
1494 if (h == NULL) 1499 if (h == NULL)
1495 { 1500 {
1496 errno = EINVAL; 1501 errno = EINVAL;
1497 return GNUNET_SYSERR; 1502 return GNUNET_SYSERR;
1498 } 1503 }
1499 1504
1500#if defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN) 1505#if defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN)
1501 return fsync(h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; 1506 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1502#else 1507#else
1503 return fdatasync(h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; 1508 return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1504#endif 1509#endif
1505} 1510}
1506 1511
@@ -1515,26 +1520,26 @@ GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
1515 * @return handle to the new pipe, NULL on error 1520 * @return handle to the new pipe, NULL on error
1516 */ 1521 */
1517struct GNUNET_DISK_PipeHandle * 1522struct GNUNET_DISK_PipeHandle *
1518GNUNET_DISK_pipe(int blocking_read, 1523GNUNET_DISK_pipe (int blocking_read,
1519 int blocking_write, 1524 int blocking_write,
1520 int inherit_read, 1525 int inherit_read,
1521 int inherit_write) 1526 int inherit_write)
1522{ 1527{
1523 int fd[2]; 1528 int fd[2];
1524 int ret; 1529 int ret;
1525 int eno; 1530 int eno;
1526 1531
1527 (void)inherit_read; 1532 (void) inherit_read;
1528 (void)inherit_write; 1533 (void) inherit_write;
1529 ret = pipe(fd); 1534 ret = pipe (fd);
1530 if (ret == -1) 1535 if (ret == -1)
1531 { 1536 {
1532 eno = errno; 1537 eno = errno;
1533 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "pipe"); 1538 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1534 errno = eno; 1539 errno = eno;
1535 return NULL; 1540 return NULL;
1536 } 1541 }
1537 return GNUNET_DISK_pipe_from_fd(blocking_read, blocking_write, fd); 1542 return GNUNET_DISK_pipe_from_fd (blocking_read, blocking_write, fd);
1538} 1543}
1539 1544
1540 1545
@@ -1549,11 +1554,11 @@ GNUNET_DISK_pipe(int blocking_read,
1549 * @return handle to the new pipe, NULL on error 1554 * @return handle to the new pipe, NULL on error
1550 */ 1555 */
1551struct GNUNET_DISK_PipeHandle * 1556struct GNUNET_DISK_PipeHandle *
1552GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2]) 1557GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
1553{ 1558{
1554 struct GNUNET_DISK_PipeHandle *p; 1559 struct GNUNET_DISK_PipeHandle *p;
1555 1560
1556 p = GNUNET_new(struct GNUNET_DISK_PipeHandle); 1561 p = GNUNET_new (struct GNUNET_DISK_PipeHandle);
1557 1562
1558 int ret; 1563 int ret;
1559 int flags; 1564 int flags;
@@ -1561,64 +1566,64 @@ GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2])
1561 1566
1562 ret = 0; 1567 ret = 0;
1563 if (fd[0] >= 0) 1568 if (fd[0] >= 0)
1569 {
1570 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1571 p->fd[0]->fd = fd[0];
1572 if (! blocking_read)
1564 { 1573 {
1565 p->fd[0] = GNUNET_new(struct GNUNET_DISK_FileHandle); 1574 flags = fcntl (fd[0], F_GETFL);
1566 p->fd[0]->fd = fd[0]; 1575 flags |= O_NONBLOCK;
1567 if (!blocking_read) 1576 if (0 > fcntl (fd[0], F_SETFL, flags))
1568 { 1577 {
1569 flags = fcntl(fd[0], F_GETFL); 1578 ret = -1;
1570 flags |= O_NONBLOCK; 1579 eno = errno;
1571 if (0 > fcntl(fd[0], F_SETFL, flags)) 1580 }
1572 { 1581 }
1573 ret = -1; 1582 flags = fcntl (fd[0], F_GETFD);
1574 eno = errno; 1583 flags |= FD_CLOEXEC;
1575 } 1584 if (0 > fcntl (fd[0], F_SETFD, flags))
1576 } 1585 {
1577 flags = fcntl(fd[0], F_GETFD); 1586 ret = -1;
1578 flags |= FD_CLOEXEC; 1587 eno = errno;
1579 if (0 > fcntl(fd[0], F_SETFD, flags))
1580 {
1581 ret = -1;
1582 eno = errno;
1583 }
1584 } 1588 }
1589 }
1585 1590
1586 if (fd[1] >= 0) 1591 if (fd[1] >= 0)
1592 {
1593 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1594 p->fd[1]->fd = fd[1];
1595 if (! blocking_write)
1587 { 1596 {
1588 p->fd[1] = GNUNET_new(struct GNUNET_DISK_FileHandle); 1597 flags = fcntl (fd[1], F_GETFL);
1589 p->fd[1]->fd = fd[1]; 1598 flags |= O_NONBLOCK;
1590 if (!blocking_write) 1599 if (0 > fcntl (fd[1], F_SETFL, flags))
1591 { 1600 {
1592 flags = fcntl(fd[1], F_GETFL); 1601 ret = -1;
1593 flags |= O_NONBLOCK; 1602 eno = errno;
1594 if (0 > fcntl(fd[1], F_SETFL, flags)) 1603 }
1595 {
1596 ret = -1;
1597 eno = errno;
1598 }
1599 }
1600 flags = fcntl(fd[1], F_GETFD);
1601 flags |= FD_CLOEXEC;
1602 if (0 > fcntl(fd[1], F_SETFD, flags))
1603 {
1604 ret = -1;
1605 eno = errno;
1606 }
1607 } 1604 }
1608 if (ret == -1) 1605 flags = fcntl (fd[1], F_GETFD);
1606 flags |= FD_CLOEXEC;
1607 if (0 > fcntl (fd[1], F_SETFD, flags))
1609 { 1608 {
1610 errno = eno; 1609 ret = -1;
1611 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "fcntl"); 1610 eno = errno;
1612 if (p->fd[0]->fd >= 0)
1613 GNUNET_break(0 == close(p->fd[0]->fd));
1614 if (p->fd[1]->fd >= 0)
1615 GNUNET_break(0 == close(p->fd[1]->fd));
1616 GNUNET_free_non_null(p->fd[0]);
1617 GNUNET_free_non_null(p->fd[1]);
1618 GNUNET_free(p);
1619 errno = eno;
1620 return NULL;
1621 } 1611 }
1612 }
1613 if (ret == -1)
1614 {
1615 errno = eno;
1616 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl");
1617 if (p->fd[0]->fd >= 0)
1618 GNUNET_break (0 == close (p->fd[0]->fd));
1619 if (p->fd[1]->fd >= 0)
1620 GNUNET_break (0 == close (p->fd[1]->fd));
1621 GNUNET_free_non_null (p->fd[0]);
1622 GNUNET_free_non_null (p->fd[1]);
1623 GNUNET_free (p);
1624 errno = eno;
1625 return NULL;
1626 }
1622 1627
1623 return p; 1628 return p;
1624} 1629}
@@ -1632,27 +1637,27 @@ GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2])
1632 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1637 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1633 */ 1638 */
1634int 1639int
1635GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, 1640GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
1636 enum GNUNET_DISK_PipeEnd end) 1641 enum GNUNET_DISK_PipeEnd end)
1637{ 1642{
1638 int ret = GNUNET_OK; 1643 int ret = GNUNET_OK;
1639 1644
1640 if (end == GNUNET_DISK_PIPE_END_READ) 1645 if (end == GNUNET_DISK_PIPE_END_READ)
1646 {
1647 if (p->fd[0])
1641 { 1648 {
1642 if (p->fd[0]) 1649 ret = GNUNET_DISK_file_close (p->fd[0]);
1643 { 1650 p->fd[0] = NULL;
1644 ret = GNUNET_DISK_file_close(p->fd[0]);
1645 p->fd[0] = NULL;
1646 }
1647 } 1651 }
1652 }
1648 else if (end == GNUNET_DISK_PIPE_END_WRITE) 1653 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1654 {
1655 if (p->fd[1])
1649 { 1656 {
1650 if (p->fd[1]) 1657 ret = GNUNET_DISK_file_close (p->fd[1]);
1651 { 1658 p->fd[1] = NULL;
1652 ret = GNUNET_DISK_file_close(p->fd[1]);
1653 p->fd[1] = NULL;
1654 }
1655 } 1659 }
1660 }
1656 1661
1657 return ret; 1662 return ret;
1658} 1663}
@@ -1670,27 +1675,27 @@ GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p,
1670 * (or if that end is not present or is closed). 1675 * (or if that end is not present or is closed).
1671 */ 1676 */
1672struct GNUNET_DISK_FileHandle * 1677struct GNUNET_DISK_FileHandle *
1673GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p, 1678GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
1674 enum GNUNET_DISK_PipeEnd end) 1679 enum GNUNET_DISK_PipeEnd end)
1675{ 1680{
1676 struct GNUNET_DISK_FileHandle *ret = NULL; 1681 struct GNUNET_DISK_FileHandle *ret = NULL;
1677 1682
1678 if (end == GNUNET_DISK_PIPE_END_READ) 1683 if (end == GNUNET_DISK_PIPE_END_READ)
1684 {
1685 if (p->fd[0])
1679 { 1686 {
1680 if (p->fd[0]) 1687 ret = p->fd[0];
1681 { 1688 p->fd[0] = NULL;
1682 ret = p->fd[0];
1683 p->fd[0] = NULL;
1684 }
1685 } 1689 }
1690 }
1686 else if (end == GNUNET_DISK_PIPE_END_WRITE) 1691 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1692 {
1693 if (p->fd[1])
1687 { 1694 {
1688 if (p->fd[1]) 1695 ret = p->fd[1];
1689 { 1696 p->fd[1] = NULL;
1690 ret = p->fd[1];
1691 p->fd[1] = NULL;
1692 }
1693 } 1697 }
1698 }
1694 1699
1695 return ret; 1700 return ret;
1696} 1701}
@@ -1703,7 +1708,7 @@ GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p,
1703 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1708 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1704 */ 1709 */
1705int 1710int
1706GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p) 1711GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1707{ 1712{
1708 int ret = GNUNET_OK; 1713 int ret = GNUNET_OK;
1709 1714
@@ -1712,22 +1717,22 @@ GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
1712 int read_end_close_errno; 1717 int read_end_close_errno;
1713 int write_end_close_errno; 1718 int write_end_close_errno;
1714 1719
1715 read_end_close = GNUNET_DISK_pipe_close_end(p, GNUNET_DISK_PIPE_END_READ); 1720 read_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_READ);
1716 read_end_close_errno = errno; 1721 read_end_close_errno = errno;
1717 write_end_close = GNUNET_DISK_pipe_close_end(p, GNUNET_DISK_PIPE_END_WRITE); 1722 write_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_WRITE);
1718 write_end_close_errno = errno; 1723 write_end_close_errno = errno;
1719 GNUNET_free(p); 1724 GNUNET_free (p);
1720 1725
1721 if (GNUNET_OK != read_end_close) 1726 if (GNUNET_OK != read_end_close)
1722 { 1727 {
1723 errno = read_end_close_errno; 1728 errno = read_end_close_errno;
1724 ret = read_end_close; 1729 ret = read_end_close;
1725 } 1730 }
1726 else if (GNUNET_OK != write_end_close) 1731 else if (GNUNET_OK != write_end_close)
1727 { 1732 {
1728 errno = write_end_close_errno; 1733 errno = write_end_close_errno;
1729 ret = write_end_close; 1734 ret = write_end_close;
1730 } 1735 }
1731 1736
1732 return ret; 1737 return ret;
1733} 1738}
@@ -1741,19 +1746,19 @@ GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
1741 * @return handle for the respective end 1746 * @return handle for the respective end
1742 */ 1747 */
1743const struct GNUNET_DISK_FileHandle * 1748const struct GNUNET_DISK_FileHandle *
1744GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, 1749GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
1745 enum GNUNET_DISK_PipeEnd n) 1750 enum GNUNET_DISK_PipeEnd n)
1746{ 1751{
1747 switch (n) 1752 switch (n)
1748 { 1753 {
1749 case GNUNET_DISK_PIPE_END_READ: 1754 case GNUNET_DISK_PIPE_END_READ:
1750 case GNUNET_DISK_PIPE_END_WRITE: 1755 case GNUNET_DISK_PIPE_END_WRITE:
1751 return p->fd[n]; 1756 return p->fd[n];
1752 1757
1753 default: 1758 default:
1754 GNUNET_break(0); 1759 GNUNET_break (0);
1755 return NULL; 1760 return NULL;
1756 } 1761 }
1757} 1762}
1758 1763
1759 1764
@@ -1766,16 +1771,16 @@ GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p,
1766 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 1771 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1767 */ 1772 */
1768int 1773int
1769GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, 1774GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
1770 void *dst, 1775 void *dst,
1771 size_t dst_len) 1776 size_t dst_len)
1772{ 1777{
1773 if (NULL == fh) 1778 if (NULL == fh)
1774 return GNUNET_SYSERR; 1779 return GNUNET_SYSERR;
1775 1780
1776 if (dst_len < sizeof(int)) 1781 if (dst_len < sizeof(int))
1777 return GNUNET_SYSERR; 1782 return GNUNET_SYSERR;
1778 *((int *)dst) = fh->fd; 1783 *((int *) dst) = fh->fd;
1779 1784
1780 return GNUNET_OK; 1785 return GNUNET_OK;
1781} 1786}
@@ -1789,24 +1794,24 @@ GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh,
1789 * @return #GNUNET_OK on success 1794 * @return #GNUNET_OK on success
1790 */ 1795 */
1791static int 1796static int
1792purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) 1797purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1793{ 1798{
1794 const char *option = cls; 1799 const char *option = cls;
1795 char *tmpname; 1800 char *tmpname;
1796 1801
1797 if (GNUNET_OK != 1802 if (GNUNET_OK !=
1798 GNUNET_CONFIGURATION_get_value_filename(cfg, "PATHS", option, &tmpname)) 1803 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname))
1799 { 1804 {
1800 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "PATHS", option); 1805 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PATHS", option);
1801 return GNUNET_NO; 1806 return GNUNET_NO;
1802 } 1807 }
1803 if (GNUNET_SYSERR == GNUNET_DISK_directory_remove(tmpname)) 1808 if (GNUNET_SYSERR == GNUNET_DISK_directory_remove (tmpname))
1804 { 1809 {
1805 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "remove", tmpname); 1810 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "remove", tmpname);
1806 GNUNET_free(tmpname); 1811 GNUNET_free (tmpname);
1807 return GNUNET_OK; 1812 return GNUNET_OK;
1808 } 1813 }
1809 GNUNET_free(tmpname); 1814 GNUNET_free (tmpname);
1810 return GNUNET_OK; 1815 return GNUNET_OK;
1811} 1816}
1812 1817
@@ -1819,12 +1824,12 @@ purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1819 * @param option option with the dir name to purge 1824 * @param option option with the dir name to purge
1820 */ 1825 */
1821void 1826void
1822GNUNET_DISK_purge_cfg_dir(const char *cfg_filename, const char *option) 1827GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, const char *option)
1823{ 1828{
1824 GNUNET_break(GNUNET_OK == 1829 GNUNET_break (GNUNET_OK ==
1825 GNUNET_CONFIGURATION_parse_and_run(cfg_filename, 1830 GNUNET_CONFIGURATION_parse_and_run (cfg_filename,
1826 &purge_cfg_dir, 1831 &purge_cfg_dir,
1827 (void *)option)); 1832 (void *) option));
1828} 1833}
1829 1834
1830 1835
diff --git a/src/util/disk.h b/src/util/disk.h
index c6014617c..89ff76687 100644
--- a/src/util/disk.h
+++ b/src/util/disk.h
@@ -39,7 +39,7 @@
39 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 39 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
40 */ 40 */
41int 41int
42GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, 42GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
43 void *dst, size_t dst_len); 43 void *dst, size_t dst_len);
44 44
45#endif /* GNUNET_DISK_H_ */ 45#endif /* GNUNET_DISK_H_ */
diff --git a/src/util/dnsparser.c b/src/util/dnsparser.c
index f187803bc..ab687414e 100644
--- a/src/util/dnsparser.c
+++ b/src/util/dnsparser.c
@@ -50,19 +50,19 @@
50 * #GNUNET_SYSERR if the label is not valid for DNS names 50 * #GNUNET_SYSERR if the label is not valid for DNS names
51 */ 51 */
52int 52int
53GNUNET_DNSPARSER_check_label(const char *label) 53GNUNET_DNSPARSER_check_label (const char *label)
54{ 54{
55 char *output; 55 char *output;
56 size_t slen; 56 size_t slen;
57 57
58 if (NULL != strchr(label, '.')) 58 if (NULL != strchr (label, '.'))
59 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */ 59 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
60 if (0 == strcmp(label, "@")) /* '@' is reserved for the empty label, see #GNUNET_GNS_EMPTY_LABEL_AT */ 60 if (0 == strcmp (label, "@")) /* '@' is reserved for the empty label, see #GNUNET_GNS_EMPTY_LABEL_AT */
61 return GNUNET_SYSERR; 61 return GNUNET_SYSERR;
62 if (IDNA_SUCCESS != idna_to_ascii_8z(label, &output, IDNA_ALLOW_UNASSIGNED)) 62 if (IDNA_SUCCESS != idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
63 return GNUNET_SYSERR; 63 return GNUNET_SYSERR;
64 slen = strlen(output); 64 slen = strlen (output);
65 free(output); 65 free (output);
66 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK; 66 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
67} 67}
68 68
@@ -76,25 +76,25 @@ GNUNET_DNSPARSER_check_label(const char *label)
76 * #GNUNET_SYSERR if the label is not valid for DNS names 76 * #GNUNET_SYSERR if the label is not valid for DNS names
77 */ 77 */
78int 78int
79GNUNET_DNSPARSER_check_name(const char *name) 79GNUNET_DNSPARSER_check_name (const char *name)
80{ 80{
81 char *ldup; 81 char *ldup;
82 char *output; 82 char *output;
83 size_t slen; 83 size_t slen;
84 char *tok; 84 char *tok;
85 85
86 ldup = GNUNET_strdup(name); 86 ldup = GNUNET_strdup (name);
87 for (tok = strtok(ldup, "."); NULL != tok; tok = strtok(NULL, ".")) 87 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
88 if (GNUNET_OK != GNUNET_DNSPARSER_check_label(tok)) 88 if (GNUNET_OK != GNUNET_DNSPARSER_check_label (tok))
89 { 89 {
90 GNUNET_free(ldup); 90 GNUNET_free (ldup);
91 return GNUNET_SYSERR; 91 return GNUNET_SYSERR;
92 } 92 }
93 GNUNET_free(ldup); 93 GNUNET_free (ldup);
94 if (IDNA_SUCCESS != idna_to_ascii_8z(name, &output, IDNA_ALLOW_UNASSIGNED)) 94 if (IDNA_SUCCESS != idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
95 return GNUNET_SYSERR; 95 return GNUNET_SYSERR;
96 slen = strlen(output); 96 slen = strlen (output);
97 free(output); 97 free (output);
98 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK; 98 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
99} 99}
100 100
@@ -105,13 +105,13 @@ GNUNET_DNSPARSER_check_name(const char *name)
105 * @param soa record to free 105 * @param soa record to free
106 */ 106 */
107void 107void
108GNUNET_DNSPARSER_free_soa(struct GNUNET_DNSPARSER_SoaRecord *soa) 108GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
109{ 109{
110 if (NULL == soa) 110 if (NULL == soa)
111 return; 111 return;
112 GNUNET_free_non_null(soa->mname); 112 GNUNET_free_non_null (soa->mname);
113 GNUNET_free_non_null(soa->rname); 113 GNUNET_free_non_null (soa->rname);
114 GNUNET_free(soa); 114 GNUNET_free (soa);
115} 115}
116 116
117 117
@@ -121,12 +121,12 @@ GNUNET_DNSPARSER_free_soa(struct GNUNET_DNSPARSER_SoaRecord *soa)
121 * @param cert record to free 121 * @param cert record to free
122 */ 122 */
123void 123void
124GNUNET_DNSPARSER_free_cert(struct GNUNET_DNSPARSER_CertRecord *cert) 124GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
125{ 125{
126 if (NULL == cert) 126 if (NULL == cert)
127 return; 127 return;
128 GNUNET_free_non_null(cert->certificate_data); 128 GNUNET_free_non_null (cert->certificate_data);
129 GNUNET_free(cert); 129 GNUNET_free (cert);
130} 130}
131 131
132 132
@@ -136,12 +136,12 @@ GNUNET_DNSPARSER_free_cert(struct GNUNET_DNSPARSER_CertRecord *cert)
136 * @param srv record to free 136 * @param srv record to free
137 */ 137 */
138void 138void
139GNUNET_DNSPARSER_free_srv(struct GNUNET_DNSPARSER_SrvRecord *srv) 139GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
140{ 140{
141 if (NULL == srv) 141 if (NULL == srv)
142 return; 142 return;
143 GNUNET_free_non_null(srv->target); 143 GNUNET_free_non_null (srv->target);
144 GNUNET_free(srv); 144 GNUNET_free (srv);
145} 145}
146 146
147 147
@@ -151,12 +151,12 @@ GNUNET_DNSPARSER_free_srv(struct GNUNET_DNSPARSER_SrvRecord *srv)
151 * @param mx record to free 151 * @param mx record to free
152 */ 152 */
153void 153void
154GNUNET_DNSPARSER_free_mx(struct GNUNET_DNSPARSER_MxRecord *mx) 154GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
155{ 155{
156 if (NULL == mx) 156 if (NULL == mx)
157 return; 157 return;
158 GNUNET_free_non_null(mx->mxhost); 158 GNUNET_free_non_null (mx->mxhost);
159 GNUNET_free(mx); 159 GNUNET_free (mx);
160} 160}
161 161
162 162
@@ -166,37 +166,37 @@ GNUNET_DNSPARSER_free_mx(struct GNUNET_DNSPARSER_MxRecord *mx)
166 * @param r record to free 166 * @param r record to free
167 */ 167 */
168void 168void
169GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r) 169GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
170{ 170{
171 GNUNET_free_non_null(r->name); 171 GNUNET_free_non_null (r->name);
172 switch (r->type) 172 switch (r->type)
173 { 173 {
174 case GNUNET_DNSPARSER_TYPE_MX: 174 case GNUNET_DNSPARSER_TYPE_MX:
175 GNUNET_DNSPARSER_free_mx(r->data.mx); 175 GNUNET_DNSPARSER_free_mx (r->data.mx);
176 break; 176 break;
177 177
178 case GNUNET_DNSPARSER_TYPE_SOA: 178 case GNUNET_DNSPARSER_TYPE_SOA:
179 GNUNET_DNSPARSER_free_soa(r->data.soa); 179 GNUNET_DNSPARSER_free_soa (r->data.soa);
180 break; 180 break;
181 181
182 case GNUNET_DNSPARSER_TYPE_SRV: 182 case GNUNET_DNSPARSER_TYPE_SRV:
183 GNUNET_DNSPARSER_free_srv(r->data.srv); 183 GNUNET_DNSPARSER_free_srv (r->data.srv);
184 break; 184 break;
185 185
186 case GNUNET_DNSPARSER_TYPE_CERT: 186 case GNUNET_DNSPARSER_TYPE_CERT:
187 GNUNET_DNSPARSER_free_cert(r->data.cert); 187 GNUNET_DNSPARSER_free_cert (r->data.cert);
188 break; 188 break;
189 189
190 case GNUNET_DNSPARSER_TYPE_NS: 190 case GNUNET_DNSPARSER_TYPE_NS:
191 case GNUNET_DNSPARSER_TYPE_CNAME: 191 case GNUNET_DNSPARSER_TYPE_CNAME:
192 case GNUNET_DNSPARSER_TYPE_PTR: 192 case GNUNET_DNSPARSER_TYPE_PTR:
193 GNUNET_free_non_null(r->data.hostname); 193 GNUNET_free_non_null (r->data.hostname);
194 break; 194 break;
195 195
196 default: 196 default:
197 GNUNET_free_non_null(r->data.raw.data); 197 GNUNET_free_non_null (r->data.raw.data);
198 break; 198 break;
199 } 199 }
200} 200}
201 201
202 202
@@ -211,12 +211,12 @@ GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r)
211 * @return name as 0-terminated C string on success, NULL if the payload is malformed 211 * @return name as 0-terminated C string on success, NULL if the payload is malformed
212 */ 212 */
213static char * 213static char *
214parse_name(const char *udp_payload, 214parse_name (const char *udp_payload,
215 size_t udp_payload_length, 215 size_t udp_payload_length,
216 size_t *off, 216 size_t *off,
217 unsigned int depth) 217 unsigned int depth)
218{ 218{
219 const uint8_t *input = (const uint8_t *)udp_payload; 219 const uint8_t *input = (const uint8_t *) udp_payload;
220 char *ret; 220 char *ret;
221 char *tmp; 221 char *tmp;
222 char *xstr; 222 char *xstr;
@@ -225,98 +225,98 @@ parse_name(const char *udp_payload,
225 char *utf8; 225 char *utf8;
226 Idna_rc rc; 226 Idna_rc rc;
227 227
228 ret = GNUNET_strdup(""); 228 ret = GNUNET_strdup ("");
229 while (1) 229 while (1)
230 {
231 if (*off >= udp_payload_length)
232 {
233 GNUNET_break_op (0);
234 goto error;
235 }
236 len = input[*off];
237 if (0 == len)
238 {
239 (*off)++;
240 break;
241 }
242 if (len < 64)
230 { 243 {
231 if (*off >= udp_payload_length) 244 if (*off + 1 + len > udp_payload_length)
232 { 245 {
233 GNUNET_break_op(0); 246 GNUNET_break_op (0);
234 goto error; 247 goto error;
235 } 248 }
236 len = input[*off]; 249 GNUNET_asprintf (&tmp, "%.*s", (int) len, &udp_payload[*off + 1]);
237 if (0 == len) 250 if (IDNA_SUCCESS !=
238 { 251 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
239 (*off)++; 252 {
240 break; 253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
241 } 254 _ ("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
242 if (len < 64) 255 tmp,
243 { 256 idna_strerror (rc));
244 if (*off + 1 + len > udp_payload_length) 257 GNUNET_free (tmp);
245 { 258 GNUNET_asprintf (&tmp,
246 GNUNET_break_op(0); 259 "%s%.*s.",
247 goto error; 260 ret,
248 } 261 (int) len,
249 GNUNET_asprintf(&tmp, "%.*s", (int)len, &udp_payload[*off + 1]); 262 &udp_payload[*off + 1]);
250 if (IDNA_SUCCESS != 263 }
251 (rc = idna_to_unicode_8z8z(tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
252 {
253 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
254 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
255 tmp,
256 idna_strerror(rc));
257 GNUNET_free(tmp);
258 GNUNET_asprintf(&tmp,
259 "%s%.*s.",
260 ret,
261 (int)len,
262 &udp_payload[*off + 1]);
263 }
264 else
265 {
266 GNUNET_free(tmp);
267 GNUNET_asprintf(&tmp, "%s%s.", ret, utf8);
268 free(utf8);
269 }
270 GNUNET_free(ret);
271 ret = tmp;
272 *off += 1 + len;
273 }
274 else if ((64 | 128) == (len & (64 | 128)))
275 {
276 if (depth > 32)
277 {
278 GNUNET_break_op(0);
279 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
280 }
281 /* pointer to string */
282 if (*off + 1 > udp_payload_length)
283 {
284 GNUNET_break_op(0);
285 goto error;
286 }
287 xoff = ((len - (64 | 128)) << 8) + input[*off + 1];
288 xstr = parse_name(udp_payload, udp_payload_length, &xoff, depth + 1);
289 if (NULL == xstr)
290 {
291 GNUNET_break_op(0);
292 goto error;
293 }
294 GNUNET_asprintf(&tmp, "%s%s.", ret, xstr);
295 GNUNET_free(ret);
296 GNUNET_free(xstr);
297 ret = tmp;
298 if (strlen(ret) > udp_payload_length)
299 {
300 GNUNET_break_op(0);
301 goto error; /* we are looping (building an infinite string) */
302 }
303 *off += 2;
304 /* pointers always terminate names */
305 break;
306 }
307 else 264 else
308 { 265 {
309 /* neither pointer nor inline string, not supported... */ 266 GNUNET_free (tmp);
310 GNUNET_break_op(0); 267 GNUNET_asprintf (&tmp, "%s%s.", ret, utf8);
311 goto error; 268 free (utf8);
312 } 269 }
270 GNUNET_free (ret);
271 ret = tmp;
272 *off += 1 + len;
273 }
274 else if ((64 | 128) == (len & (64 | 128)))
275 {
276 if (depth > 32)
277 {
278 GNUNET_break_op (0);
279 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
280 }
281 /* pointer to string */
282 if (*off + 1 > udp_payload_length)
283 {
284 GNUNET_break_op (0);
285 goto error;
286 }
287 xoff = ((len - (64 | 128)) << 8) + input[*off + 1];
288 xstr = parse_name (udp_payload, udp_payload_length, &xoff, depth + 1);
289 if (NULL == xstr)
290 {
291 GNUNET_break_op (0);
292 goto error;
293 }
294 GNUNET_asprintf (&tmp, "%s%s.", ret, xstr);
295 GNUNET_free (ret);
296 GNUNET_free (xstr);
297 ret = tmp;
298 if (strlen (ret) > udp_payload_length)
299 {
300 GNUNET_break_op (0);
301 goto error; /* we are looping (building an infinite string) */
302 }
303 *off += 2;
304 /* pointers always terminate names */
305 break;
306 }
307 else
308 {
309 /* neither pointer nor inline string, not supported... */
310 GNUNET_break_op (0);
311 goto error;
313 } 312 }
314 if (0 < strlen(ret)) 313 }
315 ret[strlen(ret) - 1] = '\0'; /* eat tailing '.' */ 314 if (0 < strlen (ret))
315 ret[strlen (ret) - 1] = '\0'; /* eat tailing '.' */
316 return ret; 316 return ret;
317error: 317error:
318 GNUNET_break_op(0); 318 GNUNET_break_op (0);
319 GNUNET_free(ret); 319 GNUNET_free (ret);
320 return NULL; 320 return NULL;
321} 321}
322 322
@@ -331,11 +331,11 @@ error:
331 * @return name as 0-terminated C string on success, NULL if the payload is malformed 331 * @return name as 0-terminated C string on success, NULL if the payload is malformed
332 */ 332 */
333char * 333char *
334GNUNET_DNSPARSER_parse_name(const char *udp_payload, 334GNUNET_DNSPARSER_parse_name (const char *udp_payload,
335 size_t udp_payload_length, 335 size_t udp_payload_length,
336 size_t *off) 336 size_t *off)
337{ 337{
338 return parse_name(udp_payload, udp_payload_length, off, 0); 338 return parse_name (udp_payload, udp_payload_length, off, 0);
339} 339}
340 340
341 341
@@ -350,30 +350,30 @@ GNUNET_DNSPARSER_parse_name(const char *udp_payload,
350 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed 350 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
351 */ 351 */
352int 352int
353GNUNET_DNSPARSER_parse_query(const char *udp_payload, 353GNUNET_DNSPARSER_parse_query (const char *udp_payload,
354 size_t udp_payload_length, 354 size_t udp_payload_length,
355 size_t *off, 355 size_t *off,
356 struct GNUNET_DNSPARSER_Query *q) 356 struct GNUNET_DNSPARSER_Query *q)
357{ 357{
358 char *name; 358 char *name;
359 struct GNUNET_TUN_DnsQueryLine ql; 359 struct GNUNET_TUN_DnsQueryLine ql;
360 360
361 name = GNUNET_DNSPARSER_parse_name(udp_payload, udp_payload_length, off); 361 name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
362 if (NULL == name) 362 if (NULL == name)
363 { 363 {
364 GNUNET_break_op(0); 364 GNUNET_break_op (0);
365 return GNUNET_SYSERR; 365 return GNUNET_SYSERR;
366 } 366 }
367 q->name = name; 367 q->name = name;
368 if (*off + sizeof(struct GNUNET_TUN_DnsQueryLine) > udp_payload_length) 368 if (*off + sizeof(struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
369 { 369 {
370 GNUNET_break_op(0); 370 GNUNET_break_op (0);
371 return GNUNET_SYSERR; 371 return GNUNET_SYSERR;
372 } 372 }
373 GNUNET_memcpy(&ql, &udp_payload[*off], sizeof(ql)); 373 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof(ql));
374 *off += sizeof(ql); 374 *off += sizeof(ql);
375 q->type = ntohs(ql.type); 375 q->type = ntohs (ql.type);
376 q->dns_traffic_class = ntohs(ql.dns_traffic_class); 376 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
377 return GNUNET_OK; 377 return GNUNET_OK;
378} 378}
379 379
@@ -388,36 +388,36 @@ GNUNET_DNSPARSER_parse_query(const char *udp_payload,
388 * @return the parsed SOA record, NULL on error 388 * @return the parsed SOA record, NULL on error
389 */ 389 */
390struct GNUNET_DNSPARSER_SoaRecord * 390struct GNUNET_DNSPARSER_SoaRecord *
391GNUNET_DNSPARSER_parse_soa(const char *udp_payload, 391GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
392 size_t udp_payload_length, 392 size_t udp_payload_length,
393 size_t *off) 393 size_t *off)
394{ 394{
395 struct GNUNET_DNSPARSER_SoaRecord *soa; 395 struct GNUNET_DNSPARSER_SoaRecord *soa;
396 struct GNUNET_TUN_DnsSoaRecord soa_bin; 396 struct GNUNET_TUN_DnsSoaRecord soa_bin;
397 size_t old_off; 397 size_t old_off;
398 398
399 old_off = *off; 399 old_off = *off;
400 soa = GNUNET_new(struct GNUNET_DNSPARSER_SoaRecord); 400 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
401 soa->mname = 401 soa->mname =
402 GNUNET_DNSPARSER_parse_name(udp_payload, udp_payload_length, off); 402 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
403 soa->rname = 403 soa->rname =
404 GNUNET_DNSPARSER_parse_name(udp_payload, udp_payload_length, off); 404 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
405 if ((NULL == soa->mname) || (NULL == soa->rname) || 405 if ((NULL == soa->mname) || (NULL == soa->rname) ||
406 (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length)) 406 (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length))
407 { 407 {
408 GNUNET_break_op(0); 408 GNUNET_break_op (0);
409 GNUNET_DNSPARSER_free_soa(soa); 409 GNUNET_DNSPARSER_free_soa (soa);
410 *off = old_off; 410 *off = old_off;
411 return NULL; 411 return NULL;
412 } 412 }
413 GNUNET_memcpy(&soa_bin, 413 GNUNET_memcpy (&soa_bin,
414 &udp_payload[*off], 414 &udp_payload[*off],
415 sizeof(struct GNUNET_TUN_DnsSoaRecord)); 415 sizeof(struct GNUNET_TUN_DnsSoaRecord));
416 soa->serial = ntohl(soa_bin.serial); 416 soa->serial = ntohl (soa_bin.serial);
417 soa->refresh = ntohl(soa_bin.refresh); 417 soa->refresh = ntohl (soa_bin.refresh);
418 soa->retry = ntohl(soa_bin.retry); 418 soa->retry = ntohl (soa_bin.retry);
419 soa->expire = ntohl(soa_bin.expire); 419 soa->expire = ntohl (soa_bin.expire);
420 soa->minimum_ttl = ntohl(soa_bin.minimum); 420 soa->minimum_ttl = ntohl (soa_bin.minimum);
421 (*off) += sizeof(struct GNUNET_TUN_DnsSoaRecord); 421 (*off) += sizeof(struct GNUNET_TUN_DnsSoaRecord);
422 return soa; 422 return soa;
423} 423}
@@ -433,9 +433,9 @@ GNUNET_DNSPARSER_parse_soa(const char *udp_payload,
433 * @return the parsed MX record, NULL on error 433 * @return the parsed MX record, NULL on error
434 */ 434 */
435struct GNUNET_DNSPARSER_MxRecord * 435struct GNUNET_DNSPARSER_MxRecord *
436GNUNET_DNSPARSER_parse_mx(const char *udp_payload, 436GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
437 size_t udp_payload_length, 437 size_t udp_payload_length,
438 size_t *off) 438 size_t *off)
439{ 439{
440 struct GNUNET_DNSPARSER_MxRecord *mx; 440 struct GNUNET_DNSPARSER_MxRecord *mx;
441 uint16_t mxpref; 441 uint16_t mxpref;
@@ -443,23 +443,23 @@ GNUNET_DNSPARSER_parse_mx(const char *udp_payload,
443 443
444 old_off = *off; 444 old_off = *off;
445 if (*off + sizeof(uint16_t) > udp_payload_length) 445 if (*off + sizeof(uint16_t) > udp_payload_length)
446 { 446 {
447 GNUNET_break_op(0); 447 GNUNET_break_op (0);
448 return NULL; 448 return NULL;
449 } 449 }
450 GNUNET_memcpy(&mxpref, &udp_payload[*off], sizeof(uint16_t)); 450 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof(uint16_t));
451 (*off) += sizeof(uint16_t); 451 (*off) += sizeof(uint16_t);
452 mx = GNUNET_new(struct GNUNET_DNSPARSER_MxRecord); 452 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
453 mx->preference = ntohs(mxpref); 453 mx->preference = ntohs (mxpref);
454 mx->mxhost = 454 mx->mxhost =
455 GNUNET_DNSPARSER_parse_name(udp_payload, udp_payload_length, off); 455 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
456 if (NULL == mx->mxhost) 456 if (NULL == mx->mxhost)
457 { 457 {
458 GNUNET_break_op(0); 458 GNUNET_break_op (0);
459 GNUNET_DNSPARSER_free_mx(mx); 459 GNUNET_DNSPARSER_free_mx (mx);
460 *off = old_off; 460 *off = old_off;
461 return NULL; 461 return NULL;
462 } 462 }
463 return mx; 463 return mx;
464} 464}
465 465
@@ -474,9 +474,9 @@ GNUNET_DNSPARSER_parse_mx(const char *udp_payload,
474 * @return the parsed SRV record, NULL on error 474 * @return the parsed SRV record, NULL on error
475 */ 475 */
476struct GNUNET_DNSPARSER_SrvRecord * 476struct GNUNET_DNSPARSER_SrvRecord *
477GNUNET_DNSPARSER_parse_srv(const char *udp_payload, 477GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
478 size_t udp_payload_length, 478 size_t udp_payload_length,
479 size_t *off) 479 size_t *off)
480{ 480{
481 struct GNUNET_DNSPARSER_SrvRecord *srv; 481 struct GNUNET_DNSPARSER_SrvRecord *srv;
482 struct GNUNET_TUN_DnsSrvRecord srv_bin; 482 struct GNUNET_TUN_DnsSrvRecord srv_bin;
@@ -485,22 +485,22 @@ GNUNET_DNSPARSER_parse_srv(const char *udp_payload,
485 old_off = *off; 485 old_off = *off;
486 if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length) 486 if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
487 return NULL; 487 return NULL;
488 GNUNET_memcpy(&srv_bin, 488 GNUNET_memcpy (&srv_bin,
489 &udp_payload[*off], 489 &udp_payload[*off],
490 sizeof(struct GNUNET_TUN_DnsSrvRecord)); 490 sizeof(struct GNUNET_TUN_DnsSrvRecord));
491 (*off) += sizeof(struct GNUNET_TUN_DnsSrvRecord); 491 (*off) += sizeof(struct GNUNET_TUN_DnsSrvRecord);
492 srv = GNUNET_new(struct GNUNET_DNSPARSER_SrvRecord); 492 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
493 srv->priority = ntohs(srv_bin.prio); 493 srv->priority = ntohs (srv_bin.prio);
494 srv->weight = ntohs(srv_bin.weight); 494 srv->weight = ntohs (srv_bin.weight);
495 srv->port = ntohs(srv_bin.port); 495 srv->port = ntohs (srv_bin.port);
496 srv->target = 496 srv->target =
497 GNUNET_DNSPARSER_parse_name(udp_payload, udp_payload_length, off); 497 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
498 if (NULL == srv->target) 498 if (NULL == srv->target)
499 { 499 {
500 GNUNET_DNSPARSER_free_srv(srv); 500 GNUNET_DNSPARSER_free_srv (srv);
501 *off = old_off; 501 *off = old_off;
502 return NULL; 502 return NULL;
503 } 503 }
504 return srv; 504 return srv;
505} 505}
506 506
@@ -515,31 +515,31 @@ GNUNET_DNSPARSER_parse_srv(const char *udp_payload,
515 * @return the parsed CERT record, NULL on error 515 * @return the parsed CERT record, NULL on error
516 */ 516 */
517struct GNUNET_DNSPARSER_CertRecord * 517struct GNUNET_DNSPARSER_CertRecord *
518GNUNET_DNSPARSER_parse_cert(const char *udp_payload, 518GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
519 size_t udp_payload_length, 519 size_t udp_payload_length,
520 size_t *off) 520 size_t *off)
521{ 521{
522 struct GNUNET_DNSPARSER_CertRecord *cert; 522 struct GNUNET_DNSPARSER_CertRecord *cert;
523 struct GNUNET_TUN_DnsCertRecord dcert; 523 struct GNUNET_TUN_DnsCertRecord dcert;
524 524
525 if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length) 525 if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
526 { 526 {
527 GNUNET_break_op(0); 527 GNUNET_break_op (0);
528 return NULL; 528 return NULL;
529 } 529 }
530 GNUNET_memcpy(&dcert, 530 GNUNET_memcpy (&dcert,
531 &udp_payload[*off], 531 &udp_payload[*off],
532 sizeof(struct GNUNET_TUN_DnsCertRecord)); 532 sizeof(struct GNUNET_TUN_DnsCertRecord));
533 (*off) += sizeof(struct GNUNET_TUN_DnsCertRecord); 533 (*off) += sizeof(struct GNUNET_TUN_DnsCertRecord);
534 cert = GNUNET_new(struct GNUNET_DNSPARSER_CertRecord); 534 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
535 cert->cert_type = ntohs(dcert.cert_type); 535 cert->cert_type = ntohs (dcert.cert_type);
536 cert->cert_tag = ntohs(dcert.cert_tag); 536 cert->cert_tag = ntohs (dcert.cert_tag);
537 cert->algorithm = dcert.algorithm; 537 cert->algorithm = dcert.algorithm;
538 cert->certificate_size = udp_payload_length - (*off); 538 cert->certificate_size = udp_payload_length - (*off);
539 cert->certificate_data = GNUNET_malloc(cert->certificate_size); 539 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
540 GNUNET_memcpy(cert->certificate_data, 540 GNUNET_memcpy (cert->certificate_data,
541 &udp_payload[*off], 541 &udp_payload[*off],
542 cert->certificate_size); 542 cert->certificate_size);
543 (*off) += cert->certificate_size; 543 (*off) += cert->certificate_size;
544 return cert; 544 return cert;
545} 545}
@@ -556,89 +556,89 @@ GNUNET_DNSPARSER_parse_cert(const char *udp_payload,
556 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed 556 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
557 */ 557 */
558int 558int
559GNUNET_DNSPARSER_parse_record(const char *udp_payload, 559GNUNET_DNSPARSER_parse_record (const char *udp_payload,
560 size_t udp_payload_length, 560 size_t udp_payload_length,
561 size_t *off, 561 size_t *off,
562 struct GNUNET_DNSPARSER_Record *r) 562 struct GNUNET_DNSPARSER_Record *r)
563{ 563{
564 char *name; 564 char *name;
565 struct GNUNET_TUN_DnsRecordLine rl; 565 struct GNUNET_TUN_DnsRecordLine rl;
566 size_t old_off; 566 size_t old_off;
567 uint16_t data_len; 567 uint16_t data_len;
568 568
569 name = GNUNET_DNSPARSER_parse_name(udp_payload, udp_payload_length, off); 569 name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
570 if (NULL == name) 570 if (NULL == name)
571 { 571 {
572 GNUNET_break_op(0); 572 GNUNET_break_op (0);
573 return GNUNET_SYSERR; 573 return GNUNET_SYSERR;
574 } 574 }
575 r->name = name; 575 r->name = name;
576 if (*off + sizeof(struct GNUNET_TUN_DnsRecordLine) > udp_payload_length) 576 if (*off + sizeof(struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
577 {
578 GNUNET_break_op (0);
579 return GNUNET_SYSERR;
580 }
581 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof(rl));
582 (*off) += sizeof(rl);
583 r->type = ntohs (rl.type);
584 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
585 r->expiration_time = GNUNET_TIME_relative_to_absolute (
586 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, ntohl (rl.ttl)));
587 data_len = ntohs (rl.data_len);
588 if (*off + data_len > udp_payload_length)
589 {
590 GNUNET_break_op (0);
591 return GNUNET_SYSERR;
592 }
593 old_off = *off;
594 switch (r->type)
595 {
596 case GNUNET_DNSPARSER_TYPE_NS:
597 case GNUNET_DNSPARSER_TYPE_CNAME:
598 case GNUNET_DNSPARSER_TYPE_DNAME:
599 case GNUNET_DNSPARSER_TYPE_PTR:
600 r->data.hostname =
601 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
602 if ((NULL == r->data.hostname) || (old_off + data_len != *off))
603 return GNUNET_SYSERR;
604 return GNUNET_OK;
605
606 case GNUNET_DNSPARSER_TYPE_SOA:
607 r->data.soa =
608 GNUNET_DNSPARSER_parse_soa (udp_payload, udp_payload_length, off);
609 if ((NULL == r->data.soa) || (old_off + data_len != *off))
577 { 610 {
578 GNUNET_break_op(0); 611 GNUNET_break_op (0);
579 return GNUNET_SYSERR; 612 return GNUNET_SYSERR;
580 } 613 }
581 GNUNET_memcpy(&rl, &udp_payload[*off], sizeof(rl)); 614 return GNUNET_OK;
582 (*off) += sizeof(rl); 615
583 r->type = ntohs(rl.type); 616 case GNUNET_DNSPARSER_TYPE_MX:
584 r->dns_traffic_class = ntohs(rl.dns_traffic_class); 617 r->data.mx =
585 r->expiration_time = GNUNET_TIME_relative_to_absolute( 618 GNUNET_DNSPARSER_parse_mx (udp_payload, udp_payload_length, off);
586 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, ntohl(rl.ttl))); 619 if ((NULL == r->data.mx) || (old_off + data_len != *off))
587 data_len = ntohs(rl.data_len);
588 if (*off + data_len > udp_payload_length)
589 { 620 {
590 GNUNET_break_op(0); 621 GNUNET_break_op (0);
591 return GNUNET_SYSERR; 622 return GNUNET_SYSERR;
592 } 623 }
593 old_off = *off; 624 return GNUNET_OK;
594 switch (r->type) 625
626 case GNUNET_DNSPARSER_TYPE_SRV:
627 r->data.srv =
628 GNUNET_DNSPARSER_parse_srv (udp_payload, udp_payload_length, off);
629 if ((NULL == r->data.srv) || (old_off + data_len != *off))
595 { 630 {
596 case GNUNET_DNSPARSER_TYPE_NS: 631 GNUNET_break_op (0);
597 case GNUNET_DNSPARSER_TYPE_CNAME: 632 return GNUNET_SYSERR;
598 case GNUNET_DNSPARSER_TYPE_DNAME:
599 case GNUNET_DNSPARSER_TYPE_PTR:
600 r->data.hostname =
601 GNUNET_DNSPARSER_parse_name(udp_payload, udp_payload_length, off);
602 if ((NULL == r->data.hostname) || (old_off + data_len != *off))
603 return GNUNET_SYSERR;
604 return GNUNET_OK;
605
606 case GNUNET_DNSPARSER_TYPE_SOA:
607 r->data.soa =
608 GNUNET_DNSPARSER_parse_soa(udp_payload, udp_payload_length, off);
609 if ((NULL == r->data.soa) || (old_off + data_len != *off))
610 {
611 GNUNET_break_op(0);
612 return GNUNET_SYSERR;
613 }
614 return GNUNET_OK;
615
616 case GNUNET_DNSPARSER_TYPE_MX:
617 r->data.mx =
618 GNUNET_DNSPARSER_parse_mx(udp_payload, udp_payload_length, off);
619 if ((NULL == r->data.mx) || (old_off + data_len != *off))
620 {
621 GNUNET_break_op(0);
622 return GNUNET_SYSERR;
623 }
624 return GNUNET_OK;
625
626 case GNUNET_DNSPARSER_TYPE_SRV:
627 r->data.srv =
628 GNUNET_DNSPARSER_parse_srv(udp_payload, udp_payload_length, off);
629 if ((NULL == r->data.srv) || (old_off + data_len != *off))
630 {
631 GNUNET_break_op(0);
632 return GNUNET_SYSERR;
633 }
634 return GNUNET_OK;
635
636 default:
637 r->data.raw.data = GNUNET_malloc(data_len);
638 r->data.raw.data_len = data_len;
639 GNUNET_memcpy(r->data.raw.data, &udp_payload[*off], data_len);
640 break;
641 } 633 }
634 return GNUNET_OK;
635
636 default:
637 r->data.raw.data = GNUNET_malloc (data_len);
638 r->data.raw.data_len = data_len;
639 GNUNET_memcpy (r->data.raw.data, &udp_payload[*off], data_len);
640 break;
641 }
642 (*off) += data_len; 642 (*off) += data_len;
643 return GNUNET_OK; 643 return GNUNET_OK;
644} 644}
@@ -653,7 +653,7 @@ GNUNET_DNSPARSER_parse_record(const char *udp_payload,
653 * @return NULL on error, otherwise the parsed packet 653 * @return NULL on error, otherwise the parsed packet
654 */ 654 */
655struct GNUNET_DNSPARSER_Packet * 655struct GNUNET_DNSPARSER_Packet *
656GNUNET_DNSPARSER_parse(const char *udp_payload, size_t udp_payload_length) 656GNUNET_DNSPARSER_parse (const char *udp_payload, size_t udp_payload_length)
657{ 657{
658 struct GNUNET_DNSPARSER_Packet *p; 658 struct GNUNET_DNSPARSER_Packet *p;
659 const struct GNUNET_TUN_DnsHeader *dns; 659 const struct GNUNET_TUN_DnsHeader *dns;
@@ -662,67 +662,67 @@ GNUNET_DNSPARSER_parse(const char *udp_payload, size_t udp_payload_length)
662 662
663 if (udp_payload_length < sizeof(struct GNUNET_TUN_DnsHeader)) 663 if (udp_payload_length < sizeof(struct GNUNET_TUN_DnsHeader))
664 return NULL; 664 return NULL;
665 dns = (const struct GNUNET_TUN_DnsHeader *)udp_payload; 665 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
666 off = sizeof(struct GNUNET_TUN_DnsHeader); 666 off = sizeof(struct GNUNET_TUN_DnsHeader);
667 p = GNUNET_new(struct GNUNET_DNSPARSER_Packet); 667 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
668 p->flags = dns->flags; 668 p->flags = dns->flags;
669 p->id = dns->id; 669 p->id = dns->id;
670 n = ntohs(dns->query_count); 670 n = ntohs (dns->query_count);
671 if (n > 0) 671 if (n > 0)
672 { 672 {
673 p->queries = GNUNET_new_array(n, struct GNUNET_DNSPARSER_Query); 673 p->queries = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Query);
674 p->num_queries = n; 674 p->num_queries = n;
675 for (unsigned int i = 0; i < n; i++) 675 for (unsigned int i = 0; i < n; i++)
676 if (GNUNET_OK != GNUNET_DNSPARSER_parse_query(udp_payload, 676 if (GNUNET_OK != GNUNET_DNSPARSER_parse_query (udp_payload,
677 udp_payload_length,
678 &off,
679 &p->queries[i]))
680 goto error;
681 }
682 n = ntohs (dns->answer_rcount);
683 if (n > 0)
684 {
685 p->answers = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
686 p->num_answers = n;
687 for (unsigned int i = 0; i < n; i++)
688 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
677 udp_payload_length, 689 udp_payload_length,
678 &off, 690 &off,
679 &p->queries[i])) 691 &p->answers[i]))
680 goto error; 692 goto error;
681 } 693 }
682 n = ntohs(dns->answer_rcount); 694 n = ntohs (dns->authority_rcount);
683 if (n > 0) 695 if (n > 0)
684 { 696 {
685 p->answers = GNUNET_new_array(n, struct GNUNET_DNSPARSER_Record); 697 p->authority_records = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
686 p->num_answers = n; 698 p->num_authority_records = n;
687 for (unsigned int i = 0; i < n; i++) 699 for (unsigned int i = 0; i < n; i++)
688 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record(udp_payload, 700 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
689 udp_payload_length, 701 udp_payload_length,
690 &off, 702 &off,
691 &p->answers[i])) 703 &p->authority_records[i]))
692 goto error; 704 goto error;
693 } 705 }
694 n = ntohs(dns->authority_rcount); 706 n = ntohs (dns->additional_rcount);
695 if (n > 0)
696 {
697 p->authority_records = GNUNET_new_array(n, struct GNUNET_DNSPARSER_Record);
698 p->num_authority_records = n;
699 for (unsigned int i = 0; i < n; i++)
700 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record(udp_payload,
701 udp_payload_length,
702 &off,
703 &p->authority_records[i]))
704 goto error;
705 }
706 n = ntohs(dns->additional_rcount);
707 if (n > 0) 707 if (n > 0)
708 {
709 p->additional_records =
710 GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
711 p->num_additional_records = n;
712 for (unsigned int i = 0; i < n; i++)
708 { 713 {
709 p->additional_records = 714 if (GNUNET_OK !=
710 GNUNET_new_array(n, struct GNUNET_DNSPARSER_Record); 715 GNUNET_DNSPARSER_parse_record (udp_payload,
711 p->num_additional_records = n; 716 udp_payload_length,
712 for (unsigned int i = 0; i < n; i++) 717 &off,
713 { 718 &p->additional_records[i]))
714 if (GNUNET_OK != 719 goto error;
715 GNUNET_DNSPARSER_parse_record(udp_payload,
716 udp_payload_length,
717 &off,
718 &p->additional_records[i]))
719 goto error;
720 }
721 } 720 }
721 }
722 return p; 722 return p;
723error: 723error:
724 GNUNET_break_op(0); 724 GNUNET_break_op (0);
725 GNUNET_DNSPARSER_free_packet(p); 725 GNUNET_DNSPARSER_free_packet (p);
726 return NULL; 726 return NULL;
727} 727}
728 728
@@ -734,44 +734,45 @@ error:
734 * @return the newly allocated record 734 * @return the newly allocated record
735 */ 735 */
736struct GNUNET_DNSPARSER_Record * 736struct GNUNET_DNSPARSER_Record *
737GNUNET_DNSPARSER_duplicate_record(const struct GNUNET_DNSPARSER_Record *r) 737GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
738{ 738{
739 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup(r, sizeof(*r)); 739 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof(*r));
740 740
741 dup->name = GNUNET_strdup(r->name); 741 dup->name = GNUNET_strdup (r->name);
742 switch (r->type) 742 switch (r->type)
743 { 743 {
744 case GNUNET_DNSPARSER_TYPE_NS: 744 case GNUNET_DNSPARSER_TYPE_NS:
745 case GNUNET_DNSPARSER_TYPE_CNAME: 745 case GNUNET_DNSPARSER_TYPE_CNAME:
746 case GNUNET_DNSPARSER_TYPE_PTR: { 746 case GNUNET_DNSPARSER_TYPE_PTR: {
747 dup->data.hostname = GNUNET_strdup(r->data.hostname); 747 dup->data.hostname = GNUNET_strdup (r->data.hostname);
748 break; 748 break;
749 } 749 }
750 750
751 case GNUNET_DNSPARSER_TYPE_SOA: { 751 case GNUNET_DNSPARSER_TYPE_SOA: {
752 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record(r->data.soa); 752 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
753 break; 753 break;
754 } 754 }
755 755
756 case GNUNET_DNSPARSER_TYPE_CERT: { 756 case GNUNET_DNSPARSER_TYPE_CERT: {
757 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record(r->data.cert); 757 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
758 break; 758 break;
759 } 759 }
760 760
761 case GNUNET_DNSPARSER_TYPE_MX: { 761 case GNUNET_DNSPARSER_TYPE_MX: {
762 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record(r->data.mx); 762 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
763 break; 763 break;
764 } 764 }
765 765
766 case GNUNET_DNSPARSER_TYPE_SRV: { 766 case GNUNET_DNSPARSER_TYPE_SRV: {
767 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record(r->data.srv); 767 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
768 break; 768 break;
769 } 769 }
770 770
771 default: { 771 default: {
772 dup->data.raw.data = GNUNET_memdup(r->data.raw.data, r->data.raw.data_len); 772 dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
773 } 773 r->data.raw.data_len);
774 } 774 }
775 }
775 return dup; 776 return dup;
776} 777}
777 778
@@ -783,13 +784,13 @@ GNUNET_DNSPARSER_duplicate_record(const struct GNUNET_DNSPARSER_Record *r)
783 * @return the newly allocated record 784 * @return the newly allocated record
784 */ 785 */
785struct GNUNET_DNSPARSER_SoaRecord * 786struct GNUNET_DNSPARSER_SoaRecord *
786GNUNET_DNSPARSER_duplicate_soa_record( 787GNUNET_DNSPARSER_duplicate_soa_record (
787 const struct GNUNET_DNSPARSER_SoaRecord *r) 788 const struct GNUNET_DNSPARSER_SoaRecord *r)
788{ 789{
789 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup(r, sizeof(*r)); 790 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof(*r));
790 791
791 dup->mname = GNUNET_strdup(r->mname); 792 dup->mname = GNUNET_strdup (r->mname);
792 dup->rname = GNUNET_strdup(r->rname); 793 dup->rname = GNUNET_strdup (r->rname);
793 return dup; 794 return dup;
794} 795}
795 796
@@ -801,12 +802,12 @@ GNUNET_DNSPARSER_duplicate_soa_record(
801 * @return the newly allocated record 802 * @return the newly allocated record
802 */ 803 */
803struct GNUNET_DNSPARSER_CertRecord * 804struct GNUNET_DNSPARSER_CertRecord *
804GNUNET_DNSPARSER_duplicate_cert_record( 805GNUNET_DNSPARSER_duplicate_cert_record (
805 const struct GNUNET_DNSPARSER_CertRecord *r) 806 const struct GNUNET_DNSPARSER_CertRecord *r)
806{ 807{
807 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup(r, sizeof(*r)); 808 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof(*r));
808 809
809 dup->certificate_data = GNUNET_strdup(r->certificate_data); 810 dup->certificate_data = GNUNET_strdup (r->certificate_data);
810 return dup; 811 return dup;
811} 812}
812 813
@@ -818,11 +819,11 @@ GNUNET_DNSPARSER_duplicate_cert_record(
818 * @return the newly allocated record 819 * @return the newly allocated record
819 */ 820 */
820struct GNUNET_DNSPARSER_MxRecord * 821struct GNUNET_DNSPARSER_MxRecord *
821GNUNET_DNSPARSER_duplicate_mx_record(const struct GNUNET_DNSPARSER_MxRecord *r) 822GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
822{ 823{
823 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup(r, sizeof(*r)); 824 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof(*r));
824 825
825 dup->mxhost = GNUNET_strdup(r->mxhost); 826 dup->mxhost = GNUNET_strdup (r->mxhost);
826 return dup; 827 return dup;
827} 828}
828 829
@@ -834,12 +835,12 @@ GNUNET_DNSPARSER_duplicate_mx_record(const struct GNUNET_DNSPARSER_MxRecord *r)
834 * @return the newly allocated record 835 * @return the newly allocated record
835 */ 836 */
836struct GNUNET_DNSPARSER_SrvRecord * 837struct GNUNET_DNSPARSER_SrvRecord *
837GNUNET_DNSPARSER_duplicate_srv_record( 838GNUNET_DNSPARSER_duplicate_srv_record (
838 const struct GNUNET_DNSPARSER_SrvRecord *r) 839 const struct GNUNET_DNSPARSER_SrvRecord *r)
839{ 840{
840 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup(r, sizeof(*r)); 841 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof(*r));
841 842
842 dup->target = GNUNET_strdup(r->target); 843 dup->target = GNUNET_strdup (r->target);
843 return dup; 844 return dup;
844} 845}
845 846
@@ -850,21 +851,21 @@ GNUNET_DNSPARSER_duplicate_srv_record(
850 * @param p packet to free 851 * @param p packet to free
851 */ 852 */
852void 853void
853GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p) 854GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
854{ 855{
855 for (unsigned int i = 0; i < p->num_queries; i++) 856 for (unsigned int i = 0; i < p->num_queries; i++)
856 GNUNET_free_non_null(p->queries[i].name); 857 GNUNET_free_non_null (p->queries[i].name);
857 GNUNET_free_non_null(p->queries); 858 GNUNET_free_non_null (p->queries);
858 for (unsigned int i = 0; i < p->num_answers; i++) 859 for (unsigned int i = 0; i < p->num_answers; i++)
859 GNUNET_DNSPARSER_free_record(&p->answers[i]); 860 GNUNET_DNSPARSER_free_record (&p->answers[i]);
860 GNUNET_free_non_null(p->answers); 861 GNUNET_free_non_null (p->answers);
861 for (unsigned int i = 0; i < p->num_authority_records; i++) 862 for (unsigned int i = 0; i < p->num_authority_records; i++)
862 GNUNET_DNSPARSER_free_record(&p->authority_records[i]); 863 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
863 GNUNET_free_non_null(p->authority_records); 864 GNUNET_free_non_null (p->authority_records);
864 for (unsigned int i = 0; i < p->num_additional_records; i++) 865 for (unsigned int i = 0; i < p->num_additional_records; i++)
865 GNUNET_DNSPARSER_free_record(&p->additional_records[i]); 866 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
866 GNUNET_free_non_null(p->additional_records); 867 GNUNET_free_non_null (p->additional_records);
867 GNUNET_free(p); 868 GNUNET_free (p);
868} 869}
869 870
870 871
@@ -885,10 +886,10 @@ GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
885 * #GNUNET_OK if @a name was added to @a dst 886 * #GNUNET_OK if @a name was added to @a dst
886 */ 887 */
887int 888int
888GNUNET_DNSPARSER_builder_add_name(char *dst, 889GNUNET_DNSPARSER_builder_add_name (char *dst,
889 size_t dst_len, 890 size_t dst_len,
890 size_t *off, 891 size_t *off,
891 const char *name) 892 const char *name)
892{ 893{
893 const char *dot; 894 const char *dot;
894 const char *idna_name; 895 const char *idna_name;
@@ -902,47 +903,47 @@ GNUNET_DNSPARSER_builder_add_name(char *dst,
902 return GNUNET_SYSERR; 903 return GNUNET_SYSERR;
903 904
904 if (IDNA_SUCCESS != 905 if (IDNA_SUCCESS !=
905 (rc = idna_to_ascii_8z(name, &idna_start, IDNA_ALLOW_UNASSIGNED))) 906 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
906 { 907 {
907 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
908 _( 909 _ (
909 "Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"), 910 "Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
910 name, 911 name,
911 idna_strerror(rc)); 912 idna_strerror (rc));
912 return GNUNET_NO; 913 return GNUNET_NO;
913 } 914 }
914 idna_name = idna_start; 915 idna_name = idna_start;
915 start = *off; 916 start = *off;
916 if (start + strlen(idna_name) + 2 > dst_len) 917 if (start + strlen (idna_name) + 2 > dst_len)
917 goto fail; 918 goto fail;
918 pos = start; 919 pos = start;
919 do 920 do
921 {
922 dot = strchr (idna_name, '.');
923 if (NULL == dot)
924 len = strlen (idna_name);
925 else
926 len = dot - idna_name;
927 if ((len >= 64) || (0 == len))
920 { 928 {
921 dot = strchr(idna_name, '.'); 929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
922 if (NULL == dot) 930 "Invalid DNS name `%s': label with %u characters encountered\n",
923 len = strlen(idna_name); 931 name,
924 else 932 (unsigned int) len);
925 len = dot - idna_name; 933 goto fail; /* label too long or empty */
926 if ((len >= 64) || (0 == len))
927 {
928 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
929 "Invalid DNS name `%s': label with %u characters encountered\n",
930 name,
931 (unsigned int)len);
932 goto fail; /* label too long or empty */
933 }
934 dst[pos++] = (char)(uint8_t)len;
935 GNUNET_memcpy(&dst[pos], idna_name, len);
936 pos += len;
937 idna_name += len + 1; /* also skip dot */
938 } 934 }
935 dst[pos++] = (char) (uint8_t) len;
936 GNUNET_memcpy (&dst[pos], idna_name, len);
937 pos += len;
938 idna_name += len + 1; /* also skip dot */
939 }
939 while (NULL != dot); 940 while (NULL != dot);
940 dst[pos++] = '\0'; /* terminator */ 941 dst[pos++] = '\0'; /* terminator */
941 *off = pos; 942 *off = pos;
942 free(idna_start); 943 free (idna_start);
943 return GNUNET_OK; 944 return GNUNET_OK;
944fail: 945fail:
945 free(idna_start); 946 free (idna_start);
946 return GNUNET_NO; 947 return GNUNET_NO;
947} 948}
948 949
@@ -960,25 +961,25 @@ fail:
960 * #GNUNET_OK if @a query was added to @a dst 961 * #GNUNET_OK if @a query was added to @a dst
961 */ 962 */
962int 963int
963GNUNET_DNSPARSER_builder_add_query(char *dst, 964GNUNET_DNSPARSER_builder_add_query (char *dst,
964 size_t dst_len, 965 size_t dst_len,
965 size_t *off, 966 size_t *off,
966 const struct GNUNET_DNSPARSER_Query *query) 967 const struct GNUNET_DNSPARSER_Query *query)
967{ 968{
968 int ret; 969 int ret;
969 struct GNUNET_TUN_DnsQueryLine ql; 970 struct GNUNET_TUN_DnsQueryLine ql;
970 971
971 ret = GNUNET_DNSPARSER_builder_add_name(dst, 972 ret = GNUNET_DNSPARSER_builder_add_name (dst,
972 dst_len - 973 dst_len
973 sizeof( 974 - sizeof(
974 struct GNUNET_TUN_DnsQueryLine), 975 struct GNUNET_TUN_DnsQueryLine),
975 off, 976 off,
976 query->name); 977 query->name);
977 if (ret != GNUNET_OK) 978 if (ret != GNUNET_OK)
978 return ret; 979 return ret;
979 ql.type = htons(query->type); 980 ql.type = htons (query->type);
980 ql.dns_traffic_class = htons(query->dns_traffic_class); 981 ql.dns_traffic_class = htons (query->dns_traffic_class);
981 GNUNET_memcpy(&dst[*off], &ql, sizeof(ql)); 982 GNUNET_memcpy (&dst[*off], &ql, sizeof(ql));
982 (*off) += sizeof(ql); 983 (*off) += sizeof(ql);
983 return GNUNET_OK; 984 return GNUNET_OK;
984} 985}
@@ -997,19 +998,19 @@ GNUNET_DNSPARSER_builder_add_query(char *dst,
997 * #GNUNET_OK if @a mx was added to @a dst 998 * #GNUNET_OK if @a mx was added to @a dst
998 */ 999 */
999int 1000int
1000GNUNET_DNSPARSER_builder_add_mx(char *dst, 1001GNUNET_DNSPARSER_builder_add_mx (char *dst,
1001 size_t dst_len, 1002 size_t dst_len,
1002 size_t *off, 1003 size_t *off,
1003 const struct GNUNET_DNSPARSER_MxRecord *mx) 1004 const struct GNUNET_DNSPARSER_MxRecord *mx)
1004{ 1005{
1005 uint16_t mxpref; 1006 uint16_t mxpref;
1006 1007
1007 if (*off + sizeof(uint16_t) > dst_len) 1008 if (*off + sizeof(uint16_t) > dst_len)
1008 return GNUNET_NO; 1009 return GNUNET_NO;
1009 mxpref = htons(mx->preference); 1010 mxpref = htons (mx->preference);
1010 GNUNET_memcpy(&dst[*off], &mxpref, sizeof(mxpref)); 1011 GNUNET_memcpy (&dst[*off], &mxpref, sizeof(mxpref));
1011 (*off) += sizeof(mxpref); 1012 (*off) += sizeof(mxpref);
1012 return GNUNET_DNSPARSER_builder_add_name(dst, dst_len, off, mx->mxhost); 1013 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
1013} 1014}
1014 1015
1015 1016
@@ -1026,7 +1027,7 @@ GNUNET_DNSPARSER_builder_add_mx(char *dst,
1026 * #GNUNET_OK if @a cert was added to @a dst 1027 * #GNUNET_OK if @a cert was added to @a dst
1027 */ 1028 */
1028int 1029int
1029GNUNET_DNSPARSER_builder_add_cert( 1030GNUNET_DNSPARSER_builder_add_cert (
1030 char *dst, 1031 char *dst,
1031 size_t dst_len, 1032 size_t dst_len,
1032 size_t *off, 1033 size_t *off,
@@ -1039,22 +1040,22 @@ GNUNET_DNSPARSER_builder_add_cert(
1039#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" 1040#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
1040#endif 1041#endif
1041 if ((cert->cert_type > UINT16_MAX) || (cert->algorithm > UINT8_MAX)) 1042 if ((cert->cert_type > UINT16_MAX) || (cert->algorithm > UINT8_MAX))
1042 { 1043 {
1043 GNUNET_break(0); 1044 GNUNET_break (0);
1044 return GNUNET_SYSERR; 1045 return GNUNET_SYSERR;
1045 } 1046 }
1046#ifdef __clang__ 1047#ifdef __clang__
1047#pragma clang diagnostic pop 1048#pragma clang diagnostic pop
1048#endif 1049#endif
1049 if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > 1050 if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size >
1050 dst_len) 1051 dst_len)
1051 return GNUNET_NO; 1052 return GNUNET_NO;
1052 dcert.cert_type = htons((uint16_t)cert->cert_type); 1053 dcert.cert_type = htons ((uint16_t) cert->cert_type);
1053 dcert.cert_tag = htons((uint16_t)cert->cert_tag); 1054 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1054 dcert.algorithm = (uint8_t)cert->algorithm; 1055 dcert.algorithm = (uint8_t) cert->algorithm;
1055 GNUNET_memcpy(&dst[*off], &dcert, sizeof(dcert)); 1056 GNUNET_memcpy (&dst[*off], &dcert, sizeof(dcert));
1056 (*off) += sizeof(dcert); 1057 (*off) += sizeof(dcert);
1057 GNUNET_memcpy(&dst[*off], cert->certificate_data, cert->certificate_size); 1058 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1058 (*off) += cert->certificate_size; 1059 (*off) += cert->certificate_size;
1059 return GNUNET_OK; 1060 return GNUNET_OK;
1060} 1061}
@@ -1073,29 +1074,29 @@ GNUNET_DNSPARSER_builder_add_cert(
1073 * #GNUNET_OK if @a soa was added to @a dst 1074 * #GNUNET_OK if @a soa was added to @a dst
1074 */ 1075 */
1075int 1076int
1076GNUNET_DNSPARSER_builder_add_soa(char *dst, 1077GNUNET_DNSPARSER_builder_add_soa (char *dst,
1077 size_t dst_len, 1078 size_t dst_len,
1078 size_t *off, 1079 size_t *off,
1079 const struct GNUNET_DNSPARSER_SoaRecord *soa) 1080 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1080{ 1081{
1081 struct GNUNET_TUN_DnsSoaRecord sd; 1082 struct GNUNET_TUN_DnsSoaRecord sd;
1082 int ret; 1083 int ret;
1083 1084
1084 if ((GNUNET_OK != 1085 if ((GNUNET_OK !=
1085 (ret = 1086 (ret =
1086 GNUNET_DNSPARSER_builder_add_name(dst, dst_len, off, soa->mname))) || 1087 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->mname))) ||
1087 (GNUNET_OK != 1088 (GNUNET_OK !=
1088 (ret = 1089 (ret =
1089 GNUNET_DNSPARSER_builder_add_name(dst, dst_len, off, soa->rname)))) 1090 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->rname))))
1090 return ret; 1091 return ret;
1091 if (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > dst_len) 1092 if (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1092 return GNUNET_NO; 1093 return GNUNET_NO;
1093 sd.serial = htonl(soa->serial); 1094 sd.serial = htonl (soa->serial);
1094 sd.refresh = htonl(soa->refresh); 1095 sd.refresh = htonl (soa->refresh);
1095 sd.retry = htonl(soa->retry); 1096 sd.retry = htonl (soa->retry);
1096 sd.expire = htonl(soa->expire); 1097 sd.expire = htonl (soa->expire);
1097 sd.minimum = htonl(soa->minimum_ttl); 1098 sd.minimum = htonl (soa->minimum_ttl);
1098 GNUNET_memcpy(&dst[*off], &sd, sizeof(sd)); 1099 GNUNET_memcpy (&dst[*off], &sd, sizeof(sd));
1099 (*off) += sizeof(sd); 1100 (*off) += sizeof(sd);
1100 return GNUNET_OK; 1101 return GNUNET_OK;
1101} 1102}
@@ -1114,24 +1115,24 @@ GNUNET_DNSPARSER_builder_add_soa(char *dst,
1114 * #GNUNET_OK if @a srv was added to @a dst 1115 * #GNUNET_OK if @a srv was added to @a dst
1115 */ 1116 */
1116int 1117int
1117GNUNET_DNSPARSER_builder_add_srv(char *dst, 1118GNUNET_DNSPARSER_builder_add_srv (char *dst,
1118 size_t dst_len, 1119 size_t dst_len,
1119 size_t *off, 1120 size_t *off,
1120 const struct GNUNET_DNSPARSER_SrvRecord *srv) 1121 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1121{ 1122{
1122 struct GNUNET_TUN_DnsSrvRecord sd; 1123 struct GNUNET_TUN_DnsSrvRecord sd;
1123 int ret; 1124 int ret;
1124 1125
1125 if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > dst_len) 1126 if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1126 return GNUNET_NO; 1127 return GNUNET_NO;
1127 sd.prio = htons(srv->priority); 1128 sd.prio = htons (srv->priority);
1128 sd.weight = htons(srv->weight); 1129 sd.weight = htons (srv->weight);
1129 sd.port = htons(srv->port); 1130 sd.port = htons (srv->port);
1130 GNUNET_memcpy(&dst[*off], &sd, sizeof(sd)); 1131 GNUNET_memcpy (&dst[*off], &sd, sizeof(sd));
1131 (*off) += sizeof(sd); 1132 (*off) += sizeof(sd);
1132 if (GNUNET_OK != 1133 if (GNUNET_OK !=
1133 (ret = 1134 (ret =
1134 GNUNET_DNSPARSER_builder_add_name(dst, dst_len, off, srv->target))) 1135 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, srv->target)))
1135 return ret; 1136 return ret;
1136 return GNUNET_OK; 1137 return GNUNET_OK;
1137} 1138}
@@ -1150,10 +1151,10 @@ GNUNET_DNSPARSER_builder_add_srv(char *dst,
1150 * #GNUNET_OK if @a record was added to @a dst 1151 * #GNUNET_OK if @a record was added to @a dst
1151 */ 1152 */
1152static int 1153static int
1153add_record(char *dst, 1154add_record (char *dst,
1154 size_t dst_len, 1155 size_t dst_len,
1155 size_t *off, 1156 size_t *off,
1156 const struct GNUNET_DNSPARSER_Record *record) 1157 const struct GNUNET_DNSPARSER_Record *record)
1157{ 1158{
1158 int ret; 1159 int ret;
1159 size_t start; 1160 size_t start;
@@ -1161,78 +1162,78 @@ add_record(char *dst,
1161 struct GNUNET_TUN_DnsRecordLine rl; 1162 struct GNUNET_TUN_DnsRecordLine rl;
1162 1163
1163 start = *off; 1164 start = *off;
1164 ret = GNUNET_DNSPARSER_builder_add_name(dst, 1165 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1165 dst_len - 1166 dst_len
1166 sizeof( 1167 - sizeof(
1167 struct GNUNET_TUN_DnsRecordLine), 1168 struct GNUNET_TUN_DnsRecordLine),
1168 off, 1169 off,
1169 record->name); 1170 record->name);
1170 if (GNUNET_OK != ret) 1171 if (GNUNET_OK != ret)
1171 return ret; 1172 return ret;
1172 /* '*off' is now the position where we will need to write the record line */ 1173 /* '*off' is now the position where we will need to write the record line */
1173 1174
1174 pos = *off + sizeof(struct GNUNET_TUN_DnsRecordLine); 1175 pos = *off + sizeof(struct GNUNET_TUN_DnsRecordLine);
1175 switch (record->type) 1176 switch (record->type)
1177 {
1178 case GNUNET_DNSPARSER_TYPE_MX:
1179 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1180 break;
1181
1182 case GNUNET_DNSPARSER_TYPE_CERT:
1183 ret =
1184 GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert);
1185 break;
1186
1187 case GNUNET_DNSPARSER_TYPE_SOA:
1188 ret =
1189 GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1190 break;
1191
1192 case GNUNET_DNSPARSER_TYPE_NS:
1193 case GNUNET_DNSPARSER_TYPE_CNAME:
1194 case GNUNET_DNSPARSER_TYPE_PTR:
1195 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1196 dst_len,
1197 &pos,
1198 record->data.hostname);
1199 break;
1200
1201 case GNUNET_DNSPARSER_TYPE_SRV:
1202 ret =
1203 GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1204 break;
1205
1206 default:
1207 if (pos + record->data.raw.data_len > dst_len)
1176 { 1208 {
1177 case GNUNET_DNSPARSER_TYPE_MX: 1209 ret = GNUNET_NO;
1178 ret = GNUNET_DNSPARSER_builder_add_mx(dst, dst_len, &pos, record->data.mx);
1179 break;
1180
1181 case GNUNET_DNSPARSER_TYPE_CERT:
1182 ret =
1183 GNUNET_DNSPARSER_builder_add_cert(dst, dst_len, &pos, record->data.cert);
1184 break;
1185
1186 case GNUNET_DNSPARSER_TYPE_SOA:
1187 ret =
1188 GNUNET_DNSPARSER_builder_add_soa(dst, dst_len, &pos, record->data.soa);
1189 break;
1190
1191 case GNUNET_DNSPARSER_TYPE_NS:
1192 case GNUNET_DNSPARSER_TYPE_CNAME:
1193 case GNUNET_DNSPARSER_TYPE_PTR:
1194 ret = GNUNET_DNSPARSER_builder_add_name(dst,
1195 dst_len,
1196 &pos,
1197 record->data.hostname);
1198 break;
1199
1200 case GNUNET_DNSPARSER_TYPE_SRV:
1201 ret =
1202 GNUNET_DNSPARSER_builder_add_srv(dst, dst_len, &pos, record->data.srv);
1203 break;
1204
1205 default:
1206 if (pos + record->data.raw.data_len > dst_len)
1207 {
1208 ret = GNUNET_NO;
1209 break;
1210 }
1211 GNUNET_memcpy(&dst[pos], record->data.raw.data, record->data.raw.data_len);
1212 pos += record->data.raw.data_len;
1213 ret = GNUNET_OK;
1214 break; 1210 break;
1215 } 1211 }
1212 GNUNET_memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1213 pos += record->data.raw.data_len;
1214 ret = GNUNET_OK;
1215 break;
1216 }
1216 if (GNUNET_OK != ret) 1217 if (GNUNET_OK != ret)
1217 { 1218 {
1218 *off = start; 1219 *off = start;
1219 return GNUNET_NO; 1220 return GNUNET_NO;
1220 } 1221 }
1221 1222
1222 if (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX) 1223 if (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1223 { 1224 {
1224 /* record data too long */ 1225 /* record data too long */
1225 *off = start; 1226 *off = start;
1226 return GNUNET_NO; 1227 return GNUNET_NO;
1227 } 1228 }
1228 rl.type = htons(record->type); 1229 rl.type = htons (record->type);
1229 rl.dns_traffic_class = htons(record->dns_traffic_class); 1230 rl.dns_traffic_class = htons (record->dns_traffic_class);
1230 rl.ttl = htonl( 1231 rl.ttl = htonl (
1231 GNUNET_TIME_absolute_get_remaining(record->expiration_time).rel_value_us / 1232 GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us
1232 1000LL / 1000LL); /* in seconds */ 1233 / 1000LL / 1000LL); /* in seconds */
1233 rl.data_len = htons( 1234 rl.data_len = htons (
1234 (uint16_t)(pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine)))); 1235 (uint16_t) (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine))));
1235 GNUNET_memcpy(&dst[*off], &rl, sizeof(struct GNUNET_TUN_DnsRecordLine)); 1236 GNUNET_memcpy (&dst[*off], &rl, sizeof(struct GNUNET_TUN_DnsRecordLine));
1236 *off = pos; 1237 *off = pos;
1237 return GNUNET_OK; 1238 return GNUNET_OK;
1238} 1239}
@@ -1253,10 +1254,10 @@ add_record(char *dst,
1253 * #GNUNET_OK if @a p was packed completely into @a buf 1254 * #GNUNET_OK if @a p was packed completely into @a buf
1254 */ 1255 */
1255int 1256int
1256GNUNET_DNSPARSER_pack(const struct GNUNET_DNSPARSER_Packet *p, 1257GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1257 uint16_t max, 1258 uint16_t max,
1258 char **buf, 1259 char **buf,
1259 size_t *buf_length) 1260 size_t *buf_length)
1260{ 1261{
1261 struct GNUNET_TUN_DnsHeader dns; 1262 struct GNUNET_TUN_DnsHeader dns;
1262 size_t off; 1263 size_t off;
@@ -1270,72 +1271,72 @@ GNUNET_DNSPARSER_pack(const struct GNUNET_DNSPARSER_Packet *p,
1270 return GNUNET_SYSERR; 1271 return GNUNET_SYSERR;
1271 dns.id = p->id; 1272 dns.id = p->id;
1272 dns.flags = p->flags; 1273 dns.flags = p->flags;
1273 dns.query_count = htons(p->num_queries); 1274 dns.query_count = htons (p->num_queries);
1274 dns.answer_rcount = htons(p->num_answers); 1275 dns.answer_rcount = htons (p->num_answers);
1275 dns.authority_rcount = htons(p->num_authority_records); 1276 dns.authority_rcount = htons (p->num_authority_records);
1276 dns.additional_rcount = htons(p->num_additional_records); 1277 dns.additional_rcount = htons (p->num_additional_records);
1277 1278
1278 off = sizeof(struct GNUNET_TUN_DnsHeader); 1279 off = sizeof(struct GNUNET_TUN_DnsHeader);
1279 trc = GNUNET_NO; 1280 trc = GNUNET_NO;
1280 for (unsigned int i = 0; i < p->num_queries; i++) 1281 for (unsigned int i = 0; i < p->num_queries; i++)
1282 {
1283 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1284 sizeof(tmp),
1285 &off,
1286 &p->queries[i]);
1287 if (GNUNET_SYSERR == ret)
1288 return GNUNET_SYSERR;
1289 if (GNUNET_NO == ret)
1281 { 1290 {
1282 ret = GNUNET_DNSPARSER_builder_add_query(tmp, 1291 dns.query_count = htons ((uint16_t) (i - 1));
1283 sizeof(tmp), 1292 trc = GNUNET_YES;
1284 &off, 1293 break;
1285 &p->queries[i]);
1286 if (GNUNET_SYSERR == ret)
1287 return GNUNET_SYSERR;
1288 if (GNUNET_NO == ret)
1289 {
1290 dns.query_count = htons((uint16_t)(i - 1));
1291 trc = GNUNET_YES;
1292 break;
1293 }
1294 } 1294 }
1295 }
1295 for (unsigned int i = 0; i < p->num_answers; i++) 1296 for (unsigned int i = 0; i < p->num_answers; i++)
1297 {
1298 ret = add_record (tmp, sizeof(tmp), &off, &p->answers[i]);
1299 if (GNUNET_SYSERR == ret)
1300 return GNUNET_SYSERR;
1301 if (GNUNET_NO == ret)
1296 { 1302 {
1297 ret = add_record(tmp, sizeof(tmp), &off, &p->answers[i]); 1303 dns.answer_rcount = htons ((uint16_t) (i - 1));
1298 if (GNUNET_SYSERR == ret) 1304 trc = GNUNET_YES;
1299 return GNUNET_SYSERR; 1305 break;
1300 if (GNUNET_NO == ret)
1301 {
1302 dns.answer_rcount = htons((uint16_t)(i - 1));
1303 trc = GNUNET_YES;
1304 break;
1305 }
1306 } 1306 }
1307 }
1307 for (unsigned int i = 0; i < p->num_authority_records; i++) 1308 for (unsigned int i = 0; i < p->num_authority_records; i++)
1309 {
1310 ret = add_record (tmp, sizeof(tmp), &off, &p->authority_records[i]);
1311 if (GNUNET_SYSERR == ret)
1312 return GNUNET_SYSERR;
1313 if (GNUNET_NO == ret)
1308 { 1314 {
1309 ret = add_record(tmp, sizeof(tmp), &off, &p->authority_records[i]); 1315 dns.authority_rcount = htons ((uint16_t) (i - 1));
1310 if (GNUNET_SYSERR == ret) 1316 trc = GNUNET_YES;
1311 return GNUNET_SYSERR; 1317 break;
1312 if (GNUNET_NO == ret)
1313 {
1314 dns.authority_rcount = htons((uint16_t)(i - 1));
1315 trc = GNUNET_YES;
1316 break;
1317 }
1318 } 1318 }
1319 }
1319 for (unsigned int i = 0; i < p->num_additional_records; i++) 1320 for (unsigned int i = 0; i < p->num_additional_records; i++)
1321 {
1322 ret = add_record (tmp, sizeof(tmp), &off, &p->additional_records[i]);
1323 if (GNUNET_SYSERR == ret)
1324 return GNUNET_SYSERR;
1325 if (GNUNET_NO == ret)
1320 { 1326 {
1321 ret = add_record(tmp, sizeof(tmp), &off, &p->additional_records[i]); 1327 dns.additional_rcount = htons (i - 1);
1322 if (GNUNET_SYSERR == ret) 1328 trc = GNUNET_YES;
1323 return GNUNET_SYSERR; 1329 break;
1324 if (GNUNET_NO == ret)
1325 {
1326 dns.additional_rcount = htons(i - 1);
1327 trc = GNUNET_YES;
1328 break;
1329 }
1330 } 1330 }
1331 }
1331 1332
1332 if (GNUNET_YES == trc) 1333 if (GNUNET_YES == trc)
1333 dns.flags.message_truncated = 1; 1334 dns.flags.message_truncated = 1;
1334 GNUNET_memcpy(tmp, &dns, sizeof(struct GNUNET_TUN_DnsHeader)); 1335 GNUNET_memcpy (tmp, &dns, sizeof(struct GNUNET_TUN_DnsHeader));
1335 1336
1336 *buf = GNUNET_malloc(off); 1337 *buf = GNUNET_malloc (off);
1337 *buf_length = off; 1338 *buf_length = off;
1338 GNUNET_memcpy(*buf, tmp, off); 1339 GNUNET_memcpy (*buf, tmp, off);
1339 if (GNUNET_YES == trc) 1340 if (GNUNET_YES == trc)
1340 return GNUNET_NO; 1341 return GNUNET_NO;
1341 return GNUNET_OK; 1342 return GNUNET_OK;
@@ -1350,16 +1351,16 @@ GNUNET_DNSPARSER_pack(const struct GNUNET_DNSPARSER_Packet *p,
1350 * @return HEX string (lower case) 1351 * @return HEX string (lower case)
1351 */ 1352 */
1352char * 1353char *
1353GNUNET_DNSPARSER_bin_to_hex(const void *data, size_t data_size) 1354GNUNET_DNSPARSER_bin_to_hex (const void *data, size_t data_size)
1354{ 1355{
1355 char *ret; 1356 char *ret;
1356 size_t off; 1357 size_t off;
1357 const uint8_t *idata; 1358 const uint8_t *idata;
1358 1359
1359 idata = data; 1360 idata = data;
1360 ret = GNUNET_malloc(data_size * 2 + 1); 1361 ret = GNUNET_malloc (data_size * 2 + 1);
1361 for (off = 0; off < data_size; off++) 1362 for (off = 0; off < data_size; off++)
1362 sprintf(&ret[off * 2], "%02x", idata[off]); 1363 sprintf (&ret[off * 2], "%02x", idata[off]);
1363 return ret; 1364 return ret;
1364} 1365}
1365 1366
@@ -1373,7 +1374,7 @@ GNUNET_DNSPARSER_bin_to_hex(const void *data, size_t data_size)
1373 * @return number of bytes written to data 1374 * @return number of bytes written to data
1374 */ 1375 */
1375size_t 1376size_t
1376GNUNET_DNSPARSER_hex_to_bin(const char *hex, void *data) 1377GNUNET_DNSPARSER_hex_to_bin (const char *hex, void *data)
1377{ 1378{
1378 size_t data_size; 1379 size_t data_size;
1379 size_t off; 1380 size_t off;
@@ -1381,17 +1382,17 @@ GNUNET_DNSPARSER_hex_to_bin(const char *hex, void *data)
1381 unsigned int h; 1382 unsigned int h;
1382 char in[3]; 1383 char in[3];
1383 1384
1384 data_size = strlen(hex) / 2; 1385 data_size = strlen (hex) / 2;
1385 idata = data; 1386 idata = data;
1386 in[2] = '\0'; 1387 in[2] = '\0';
1387 for (off = 0; off < data_size; off++) 1388 for (off = 0; off < data_size; off++)
1388 { 1389 {
1389 in[0] = tolower((unsigned char)hex[off * 2]); 1390 in[0] = tolower ((unsigned char) hex[off * 2]);
1390 in[1] = tolower((unsigned char)hex[off * 2 + 1]); 1391 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1391 if (1 != sscanf(in, "%x", &h)) 1392 if (1 != sscanf (in, "%x", &h))
1392 return off; 1393 return off;
1393 idata[off] = (uint8_t)h; 1394 idata[off] = (uint8_t) h;
1394 } 1395 }
1395 return off; 1396 return off;
1396} 1397}
1397 1398
diff --git a/src/util/dnsstub.c b/src/util/dnsstub.c
index fef4b5f5d..1ac274c92 100644
--- a/src/util/dnsstub.c
+++ b/src/util/dnsstub.c
@@ -29,7 +29,7 @@
29 * Timeout for retrying DNS queries. 29 * Timeout for retrying DNS queries.
30 */ 30 */
31#define DNS_RETRANSMIT_DELAY \ 31#define DNS_RETRANSMIT_DELAY \
32 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250) 32 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
33 33
34 34
35/** 35/**
@@ -41,7 +41,8 @@ struct DnsServer;
41/** 41/**
42 * UDP socket we are using for sending DNS requests to the Internet. 42 * UDP socket we are using for sending DNS requests to the Internet.
43 */ 43 */
44struct GNUNET_DNSSTUB_RequestSocket { 44struct GNUNET_DNSSTUB_RequestSocket
45{
45 /** 46 /**
46 * UDP socket we use for this request for IPv4 47 * UDP socket we use for this request for IPv4
47 */ 48 */
@@ -97,7 +98,8 @@ struct GNUNET_DNSSTUB_RequestSocket {
97/** 98/**
98 * DNS Server used for resolution. 99 * DNS Server used for resolution.
99 */ 100 */
100struct DnsServer { 101struct DnsServer
102{
101 /** 103 /**
102 * Kept in a DLL. 104 * Kept in a DLL.
103 */ 105 */
@@ -118,7 +120,8 @@ struct DnsServer {
118/** 120/**
119 * Handle to the stub resolver. 121 * Handle to the stub resolver.
120 */ 122 */
121struct GNUNET_DNSSTUB_Context { 123struct GNUNET_DNSSTUB_Context
124{
122 /** 125 /**
123 * Array of all open sockets for DNS requests. 126 * Array of all open sockets for DNS requests.
124 */ 127 */
@@ -152,33 +155,33 @@ struct GNUNET_DNSSTUB_Context {
152 * @param rs request socket to clean up 155 * @param rs request socket to clean up
153 */ 156 */
154static void 157static void
155cleanup_rs(struct GNUNET_DNSSTUB_RequestSocket *rs) 158cleanup_rs (struct GNUNET_DNSSTUB_RequestSocket *rs)
156{ 159{
157 if (NULL != rs->dnsout4) 160 if (NULL != rs->dnsout4)
158 { 161 {
159 GNUNET_NETWORK_socket_close(rs->dnsout4); 162 GNUNET_NETWORK_socket_close (rs->dnsout4);
160 rs->dnsout4 = NULL; 163 rs->dnsout4 = NULL;
161 } 164 }
162 if (NULL != rs->dnsout6) 165 if (NULL != rs->dnsout6)
163 { 166 {
164 GNUNET_NETWORK_socket_close(rs->dnsout6); 167 GNUNET_NETWORK_socket_close (rs->dnsout6);
165 rs->dnsout6 = NULL; 168 rs->dnsout6 = NULL;
166 } 169 }
167 if (NULL != rs->read_task) 170 if (NULL != rs->read_task)
168 { 171 {
169 GNUNET_SCHEDULER_cancel(rs->read_task); 172 GNUNET_SCHEDULER_cancel (rs->read_task);
170 rs->read_task = NULL; 173 rs->read_task = NULL;
171 } 174 }
172 if (NULL != rs->retry_task) 175 if (NULL != rs->retry_task)
173 { 176 {
174 GNUNET_SCHEDULER_cancel(rs->retry_task); 177 GNUNET_SCHEDULER_cancel (rs->retry_task);
175 rs->retry_task = NULL; 178 rs->retry_task = NULL;
176 } 179 }
177 if (NULL != rs->request) 180 if (NULL != rs->request)
178 { 181 {
179 GNUNET_free(rs->request); 182 GNUNET_free (rs->request);
180 rs->request = NULL; 183 rs->request = NULL;
181 } 184 }
182} 185}
183 186
184 187
@@ -189,7 +192,7 @@ cleanup_rs(struct GNUNET_DNSSTUB_RequestSocket *rs)
189 * @return #GNUNET_OK on success 192 * @return #GNUNET_OK on success
190 */ 193 */
191static struct GNUNET_NETWORK_Handle * 194static struct GNUNET_NETWORK_Handle *
192open_socket(int af) 195open_socket (int af)
193{ 196{
194 struct sockaddr_in a4; 197 struct sockaddr_in a4;
195 struct sockaddr_in6 a6; 198 struct sockaddr_in6 a6;
@@ -197,35 +200,35 @@ open_socket(int af)
197 socklen_t alen; 200 socklen_t alen;
198 struct GNUNET_NETWORK_Handle *ret; 201 struct GNUNET_NETWORK_Handle *ret;
199 202
200 ret = GNUNET_NETWORK_socket_create(af, SOCK_DGRAM, 0); 203 ret = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, 0);
201 if (NULL == ret) 204 if (NULL == ret)
202 return NULL; 205 return NULL;
203 switch (af) 206 switch (af)
204 { 207 {
205 case AF_INET: 208 case AF_INET:
206 memset(&a4, 0, alen = sizeof(struct sockaddr_in)); 209 memset (&a4, 0, alen = sizeof(struct sockaddr_in));
207 sa = (struct sockaddr *)&a4; 210 sa = (struct sockaddr *) &a4;
208 break; 211 break;
209 212
210 case AF_INET6: 213 case AF_INET6:
211 memset(&a6, 0, alen = sizeof(struct sockaddr_in6)); 214 memset (&a6, 0, alen = sizeof(struct sockaddr_in6));
212 sa = (struct sockaddr *)&a6; 215 sa = (struct sockaddr *) &a6;
213 break; 216 break;
214 217
215 default: 218 default:
216 GNUNET_break(0); 219 GNUNET_break (0);
217 GNUNET_NETWORK_socket_close(ret); 220 GNUNET_NETWORK_socket_close (ret);
218 return NULL; 221 return NULL;
219 } 222 }
220 sa->sa_family = af; 223 sa->sa_family = af;
221 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ret, sa, alen)) 224 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret, sa, alen))
222 { 225 {
223 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
224 _("Could not bind to any port: %s\n"), 227 _ ("Could not bind to any port: %s\n"),
225 strerror(errno)); 228 strerror (errno));
226 GNUNET_NETWORK_socket_close(ret); 229 GNUNET_NETWORK_socket_close (ret);
227 return NULL; 230 return NULL;
228 } 231 }
229 return ret; 232 return ret;
230} 233}
231 234
@@ -238,38 +241,38 @@ open_socket(int af)
238 * @return NULL on error 241 * @return NULL on error
239 */ 242 */
240static struct GNUNET_DNSSTUB_RequestSocket * 243static struct GNUNET_DNSSTUB_RequestSocket *
241get_request_socket(struct GNUNET_DNSSTUB_Context *ctx) 244get_request_socket (struct GNUNET_DNSSTUB_Context *ctx)
242{ 245{
243 struct GNUNET_DNSSTUB_RequestSocket *rs; 246 struct GNUNET_DNSSTUB_RequestSocket *rs;
244 247
245 for (unsigned int i = 0; i < 256; i++) 248 for (unsigned int i = 0; i < 256; i++)
246 { 249 {
247 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 250 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
248 ctx->num_sockets)]; 251 ctx->num_sockets)];
249 if (NULL == rs->rc) 252 if (NULL == rs->rc)
250 break; 253 break;
251 } 254 }
252 if (NULL != rs->rc) 255 if (NULL != rs->rc)
253 { 256 {
254 /* signal "failure" */ 257 /* signal "failure" */
255 rs->rc(rs->rc_cls, NULL, 0); 258 rs->rc (rs->rc_cls, NULL, 0);
256 rs->rc = NULL; 259 rs->rc = NULL;
257 } 260 }
258 if (NULL != rs->read_task) 261 if (NULL != rs->read_task)
259 { 262 {
260 GNUNET_SCHEDULER_cancel(rs->read_task); 263 GNUNET_SCHEDULER_cancel (rs->read_task);
261 rs->read_task = NULL; 264 rs->read_task = NULL;
262 } 265 }
263 if (NULL != rs->retry_task) 266 if (NULL != rs->retry_task)
264 { 267 {
265 GNUNET_SCHEDULER_cancel(rs->retry_task); 268 GNUNET_SCHEDULER_cancel (rs->retry_task);
266 rs->retry_task = NULL; 269 rs->retry_task = NULL;
267 } 270 }
268 if (NULL != rs->request) 271 if (NULL != rs->request)
269 { 272 {
270 GNUNET_free(rs->request); 273 GNUNET_free (rs->request);
271 rs->request = NULL; 274 rs->request = NULL;
272 } 275 }
273 rs->ctx = ctx; 276 rs->ctx = ctx;
274 return rs; 277 return rs;
275} 278}
@@ -284,20 +287,20 @@ get_request_socket(struct GNUNET_DNSSTUB_Context *ctx)
284 * @return #GNUNET_OK on success, #GNUNET_NO on drop, #GNUNET_SYSERR on IO-errors (closed socket) 287 * @return #GNUNET_OK on success, #GNUNET_NO on drop, #GNUNET_SYSERR on IO-errors (closed socket)
285 */ 288 */
286static int 289static int
287do_dns_read(struct GNUNET_DNSSTUB_RequestSocket *rs, 290do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
288 struct GNUNET_NETWORK_Handle *dnsout) 291 struct GNUNET_NETWORK_Handle *dnsout)
289{ 292{
290 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx; 293 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx;
291 ssize_t r; 294 ssize_t r;
292 int len; 295 int len;
293 296
294 if (0 != ioctl(GNUNET_NETWORK_get_fd(dnsout), FIONREAD, &len)) 297 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
295 { 298 {
296 /* conservative choice: */ 299 /* conservative choice: */
297 len = UINT16_MAX; 300 len = UINT16_MAX;
298 } 301 }
299 302
300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Receiving %d byte DNS reply\n", len); 303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving %d byte DNS reply\n", len);
301 { 304 {
302 unsigned char buf[len] GNUNET_ALIGN; 305 unsigned char buf[len] GNUNET_ALIGN;
303 int found; 306 int found;
@@ -306,50 +309,50 @@ do_dns_read(struct GNUNET_DNSSTUB_RequestSocket *rs,
306 struct GNUNET_TUN_DnsHeader *dns; 309 struct GNUNET_TUN_DnsHeader *dns;
307 310
308 addrlen = sizeof(addr); 311 addrlen = sizeof(addr);
309 memset(&addr, 0, sizeof(addr)); 312 memset (&addr, 0, sizeof(addr));
310 r = GNUNET_NETWORK_socket_recvfrom(dnsout, 313 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
311 buf, 314 buf,
312 sizeof(buf), 315 sizeof(buf),
313 (struct sockaddr *)&addr, 316 (struct sockaddr *) &addr,
314 &addrlen); 317 &addrlen);
315 if (-1 == r) 318 if (-1 == r)
316 { 319 {
317 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "recvfrom"); 320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
318 GNUNET_NETWORK_socket_close(dnsout); 321 GNUNET_NETWORK_socket_close (dnsout);
319 return GNUNET_SYSERR; 322 return GNUNET_SYSERR;
320 } 323 }
321 found = GNUNET_NO; 324 found = GNUNET_NO;
322 for (struct DnsServer *ds = ctx->dns_head; NULL != ds; ds = ds->next) 325 for (struct DnsServer *ds = ctx->dns_head; NULL != ds; ds = ds->next)
326 {
327 if (0 == memcmp (&addr,
328 &ds->ss,
329 GNUNET_MIN (sizeof(struct sockaddr_storage), addrlen)))
323 { 330 {
324 if (0 == memcmp(&addr, 331 found = GNUNET_YES;
325 &ds->ss, 332 break;
326 GNUNET_MIN(sizeof(struct sockaddr_storage), addrlen)))
327 {
328 found = GNUNET_YES;
329 break;
330 }
331 } 333 }
334 }
332 if (GNUNET_NO == found) 335 if (GNUNET_NO == found)
333 { 336 {
334 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Received DNS response from server we never asked (ignored)"); 338 "Received DNS response from server we never asked (ignored)");
336 return GNUNET_NO; 339 return GNUNET_NO;
337 } 340 }
338 if (sizeof(struct GNUNET_TUN_DnsHeader) > (size_t)r) 341 if (sizeof(struct GNUNET_TUN_DnsHeader) > (size_t) r)
339 { 342 {
340 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341 _("Received DNS response that is too small (%u bytes)"), 344 _ ("Received DNS response that is too small (%u bytes)"),
342 (unsigned int)r); 345 (unsigned int) r);
343 return GNUNET_NO; 346 return GNUNET_NO;
344 } 347 }
345 dns = (struct GNUNET_TUN_DnsHeader *)buf; 348 dns = (struct GNUNET_TUN_DnsHeader *) buf;
346 if (NULL == rs->rc) 349 if (NULL == rs->rc)
347 { 350 {
348 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Request timeout or cancelled; ignoring reply\n"); 352 "Request timeout or cancelled; ignoring reply\n");
350 return GNUNET_NO; 353 return GNUNET_NO;
351 } 354 }
352 rs->rc(rs->rc_cls, dns, r); 355 rs->rc (rs->rc_cls, dns, r);
353 } 356 }
354 return GNUNET_OK; 357 return GNUNET_OK;
355} 358}
@@ -361,7 +364,7 @@ do_dns_read(struct GNUNET_DNSSTUB_RequestSocket *rs,
361 * @param cls socket to read from 364 * @param cls socket to read from
362 */ 365 */
363static void 366static void
364read_response(void *cls); 367read_response (void *cls);
365 368
366 369
367/** 370/**
@@ -370,25 +373,25 @@ read_response(void *cls);
370 * @param rs request to schedule read operation for 373 * @param rs request to schedule read operation for
371 */ 374 */
372static void 375static void
373schedule_read(struct GNUNET_DNSSTUB_RequestSocket *rs) 376schedule_read (struct GNUNET_DNSSTUB_RequestSocket *rs)
374{ 377{
375 struct GNUNET_NETWORK_FDSet *rset; 378 struct GNUNET_NETWORK_FDSet *rset;
376 379
377 if (NULL != rs->read_task) 380 if (NULL != rs->read_task)
378 GNUNET_SCHEDULER_cancel(rs->read_task); 381 GNUNET_SCHEDULER_cancel (rs->read_task);
379 rset = GNUNET_NETWORK_fdset_create(); 382 rset = GNUNET_NETWORK_fdset_create ();
380 if (NULL != rs->dnsout4) 383 if (NULL != rs->dnsout4)
381 GNUNET_NETWORK_fdset_set(rset, rs->dnsout4); 384 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
382 if (NULL != rs->dnsout6) 385 if (NULL != rs->dnsout6)
383 GNUNET_NETWORK_fdset_set(rset, rs->dnsout6); 386 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
384 rs->read_task = 387 rs->read_task =
385 GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_DEFAULT, 388 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
386 GNUNET_TIME_UNIT_FOREVER_REL, 389 GNUNET_TIME_UNIT_FOREVER_REL,
387 rset, 390 rset,
388 NULL, 391 NULL,
389 &read_response, 392 &read_response,
390 rs); 393 rs);
391 GNUNET_NETWORK_fdset_destroy(rset); 394 GNUNET_NETWORK_fdset_destroy (rset);
392} 395}
393 396
394 397
@@ -398,24 +401,24 @@ schedule_read(struct GNUNET_DNSSTUB_RequestSocket *rs)
398 * @param cls `struct GNUNET_DNSSTUB_RequestSocket` to read from 401 * @param cls `struct GNUNET_DNSSTUB_RequestSocket` to read from
399 */ 402 */
400static void 403static void
401read_response(void *cls) 404read_response (void *cls)
402{ 405{
403 struct GNUNET_DNSSTUB_RequestSocket *rs = cls; 406 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
404 const struct GNUNET_SCHEDULER_TaskContext *tc; 407 const struct GNUNET_SCHEDULER_TaskContext *tc;
405 408
406 rs->read_task = NULL; 409 rs->read_task = NULL;
407 tc = GNUNET_SCHEDULER_get_task_context(); 410 tc = GNUNET_SCHEDULER_get_task_context ();
408 /* read and process ready sockets */ 411 /* read and process ready sockets */
409 if ((NULL != rs->dnsout4) && 412 if ((NULL != rs->dnsout4) &&
410 (GNUNET_NETWORK_fdset_isset(tc->read_ready, rs->dnsout4)) && 413 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
411 (GNUNET_SYSERR == do_dns_read(rs, rs->dnsout4))) 414 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout4)))
412 rs->dnsout4 = NULL; 415 rs->dnsout4 = NULL;
413 if ((NULL != rs->dnsout6) && 416 if ((NULL != rs->dnsout6) &&
414 (GNUNET_NETWORK_fdset_isset(tc->read_ready, rs->dnsout6)) && 417 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
415 (GNUNET_SYSERR == do_dns_read(rs, rs->dnsout6))) 418 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout6)))
416 rs->dnsout6 = NULL; 419 rs->dnsout6 = NULL;
417 /* re-schedule read task */ 420 /* re-schedule read task */
418 schedule_read(rs); 421 schedule_read (rs);
419} 422}
420 423
421 424
@@ -426,7 +429,7 @@ read_response(void *cls)
426 * @param cls our `struct GNUNET_DNSSTUB_RequestSocket *` 429 * @param cls our `struct GNUNET_DNSSTUB_RequestSocket *`
427 */ 430 */
428static void 431static void
429transmit_query(void *cls) 432transmit_query (void *cls)
430{ 433{
431 struct GNUNET_DNSSTUB_RequestSocket *rs = cls; 434 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
432 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx; 435 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx;
@@ -436,54 +439,54 @@ transmit_query(void *cls)
436 struct GNUNET_NETWORK_Handle *dnsout; 439 struct GNUNET_NETWORK_Handle *dnsout;
437 440
438 rs->retry_task = 441 rs->retry_task =
439 GNUNET_SCHEDULER_add_delayed(ctx->retry_freq, &transmit_query, rs); 442 GNUNET_SCHEDULER_add_delayed (ctx->retry_freq, &transmit_query, rs);
440 ds = rs->ds_pos; 443 ds = rs->ds_pos;
441 rs->ds_pos = ds->next; 444 rs->ds_pos = ds->next;
442 if (NULL == rs->ds_pos) 445 if (NULL == rs->ds_pos)
443 rs->ds_pos = ctx->dns_head; 446 rs->ds_pos = ctx->dns_head;
444 GNUNET_assert(NULL != ds); 447 GNUNET_assert (NULL != ds);
445 dnsout = NULL; 448 dnsout = NULL;
446 switch (ds->ss.ss_family) 449 switch (ds->ss.ss_family)
447 { 450 {
448 case AF_INET: 451 case AF_INET:
449 if (NULL == rs->dnsout4) 452 if (NULL == rs->dnsout4)
450 rs->dnsout4 = open_socket(AF_INET); 453 rs->dnsout4 = open_socket (AF_INET);
451 dnsout = rs->dnsout4; 454 dnsout = rs->dnsout4;
452 sa = (const struct sockaddr *)&ds->ss; 455 sa = (const struct sockaddr *) &ds->ss;
453 salen = sizeof(struct sockaddr_in); 456 salen = sizeof(struct sockaddr_in);
454 break; 457 break;
455 458
456 case AF_INET6: 459 case AF_INET6:
457 if (NULL == rs->dnsout6) 460 if (NULL == rs->dnsout6)
458 rs->dnsout6 = open_socket(AF_INET6); 461 rs->dnsout6 = open_socket (AF_INET6);
459 dnsout = rs->dnsout6; 462 dnsout = rs->dnsout6;
460 sa = (const struct sockaddr *)&ds->ss; 463 sa = (const struct sockaddr *) &ds->ss;
461 salen = sizeof(struct sockaddr_in6); 464 salen = sizeof(struct sockaddr_in6);
462 break; 465 break;
463 466
464 default: 467 default:
465 return; 468 return;
466 } 469 }
467 if (NULL == dnsout) 470 if (NULL == dnsout)
468 { 471 {
469 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
470 "Unable to use configure DNS server, skipping\n"); 473 "Unable to use configure DNS server, skipping\n");
471 return; 474 return;
472 } 475 }
473 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_sendto(dnsout, 476 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_sendto (dnsout,
474 rs->request, 477 rs->request,
475 rs->request_len, 478 rs->request_len,
476 sa, 479 sa,
477 salen)) 480 salen))
478 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
479 _("Failed to send DNS request to %s: %s\n"), 482 _ ("Failed to send DNS request to %s: %s\n"),
480 GNUNET_a2s(sa, salen), 483 GNUNET_a2s (sa, salen),
481 strerror(errno)); 484 strerror (errno));
482 else 485 else
483 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 _("Sent DNS request to %s\n"), 487 _ ("Sent DNS request to %s\n"),
485 GNUNET_a2s(sa, salen)); 488 GNUNET_a2s (sa, salen));
486 schedule_read(rs); 489 schedule_read (rs);
487} 490}
488 491
489 492
@@ -498,32 +501,32 @@ transmit_query(void *cls)
498 * @return socket used for the request, NULL on error 501 * @return socket used for the request, NULL on error
499 */ 502 */
500struct GNUNET_DNSSTUB_RequestSocket * 503struct GNUNET_DNSSTUB_RequestSocket *
501GNUNET_DNSSTUB_resolve(struct GNUNET_DNSSTUB_Context *ctx, 504GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx,
502 const void *request, 505 const void *request,
503 size_t request_len, 506 size_t request_len,
504 GNUNET_DNSSTUB_ResultCallback rc, 507 GNUNET_DNSSTUB_ResultCallback rc,
505 void *rc_cls) 508 void *rc_cls)
506{ 509{
507 struct GNUNET_DNSSTUB_RequestSocket *rs; 510 struct GNUNET_DNSSTUB_RequestSocket *rs;
508 511
509 if (NULL == ctx->dns_head) 512 if (NULL == ctx->dns_head)
510 { 513 {
511 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
512 "No DNS server configured for resolution\n"); 515 "No DNS server configured for resolution\n");
513 return NULL; 516 return NULL;
514 } 517 }
515 if (NULL == (rs = get_request_socket(ctx))) 518 if (NULL == (rs = get_request_socket (ctx)))
516 { 519 {
517 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518 "No request socket available for DNS resolution\n"); 521 "No request socket available for DNS resolution\n");
519 return NULL; 522 return NULL;
520 } 523 }
521 rs->ds_pos = ctx->dns_head; 524 rs->ds_pos = ctx->dns_head;
522 rs->rc = rc; 525 rs->rc = rc;
523 rs->rc_cls = rc_cls; 526 rs->rc_cls = rc_cls;
524 rs->request = GNUNET_memdup(request, request_len); 527 rs->request = GNUNET_memdup (request, request_len);
525 rs->request_len = request_len; 528 rs->request_len = request_len;
526 rs->retry_task = GNUNET_SCHEDULER_add_now(&transmit_query, rs); 529 rs->retry_task = GNUNET_SCHEDULER_add_now (&transmit_query, rs);
527 return rs; 530 return rs;
528} 531}
529 532
@@ -534,19 +537,19 @@ GNUNET_DNSSTUB_resolve(struct GNUNET_DNSSTUB_Context *ctx,
534 * @param rs resolution to cancel 537 * @param rs resolution to cancel
535 */ 538 */
536void 539void
537GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs) 540GNUNET_DNSSTUB_resolve_cancel (struct GNUNET_DNSSTUB_RequestSocket *rs)
538{ 541{
539 rs->rc = NULL; 542 rs->rc = NULL;
540 if (NULL != rs->retry_task) 543 if (NULL != rs->retry_task)
541 { 544 {
542 GNUNET_SCHEDULER_cancel(rs->retry_task); 545 GNUNET_SCHEDULER_cancel (rs->retry_task);
543 rs->retry_task = NULL; 546 rs->retry_task = NULL;
544 } 547 }
545 if (NULL != rs->read_task) 548 if (NULL != rs->read_task)
546 { 549 {
547 GNUNET_SCHEDULER_cancel(rs->read_task); 550 GNUNET_SCHEDULER_cancel (rs->read_task);
548 rs->read_task = NULL; 551 rs->read_task = NULL;
549 } 552 }
550} 553}
551 554
552 555
@@ -558,19 +561,19 @@ GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs)
558 * @return NULL on error 561 * @return NULL on error
559 */ 562 */
560struct GNUNET_DNSSTUB_Context * 563struct GNUNET_DNSSTUB_Context *
561GNUNET_DNSSTUB_start(unsigned int num_sockets) 564GNUNET_DNSSTUB_start (unsigned int num_sockets)
562{ 565{
563 struct GNUNET_DNSSTUB_Context *ctx; 566 struct GNUNET_DNSSTUB_Context *ctx;
564 567
565 if (0 == num_sockets) 568 if (0 == num_sockets)
566 { 569 {
567 GNUNET_break(0); 570 GNUNET_break (0);
568 return NULL; 571 return NULL;
569 } 572 }
570 ctx = GNUNET_new(struct GNUNET_DNSSTUB_Context); 573 ctx = GNUNET_new (struct GNUNET_DNSSTUB_Context);
571 ctx->num_sockets = num_sockets; 574 ctx->num_sockets = num_sockets;
572 ctx->sockets = 575 ctx->sockets =
573 GNUNET_new_array(num_sockets, struct GNUNET_DNSSTUB_RequestSocket); 576 GNUNET_new_array (num_sockets, struct GNUNET_DNSSTUB_RequestSocket);
574 ctx->retry_freq = DNS_RETRANSMIT_DELAY; 577 ctx->retry_freq = DNS_RETRANSMIT_DELAY;
575 return ctx; 578 return ctx;
576} 579}
@@ -585,45 +588,45 @@ GNUNET_DNSSTUB_start(unsigned int num_sockets)
585 * @return #GNUNET_OK on success 588 * @return #GNUNET_OK on success
586 */ 589 */
587int 590int
588GNUNET_DNSSTUB_add_dns_ip(struct GNUNET_DNSSTUB_Context *ctx, 591GNUNET_DNSSTUB_add_dns_ip (struct GNUNET_DNSSTUB_Context *ctx,
589 const char *dns_ip) 592 const char *dns_ip)
590{ 593{
591 struct DnsServer *ds; 594 struct DnsServer *ds;
592 struct in_addr i4; 595 struct in_addr i4;
593 struct in6_addr i6; 596 struct in6_addr i6;
594 597
595 ds = GNUNET_new(struct DnsServer); 598 ds = GNUNET_new (struct DnsServer);
596 if (1 == inet_pton(AF_INET, dns_ip, &i4)) 599 if (1 == inet_pton (AF_INET, dns_ip, &i4))
597 { 600 {
598 struct sockaddr_in *s4 = (struct sockaddr_in *)&ds->ss; 601 struct sockaddr_in *s4 = (struct sockaddr_in *) &ds->ss;
599 602
600 s4->sin_family = AF_INET; 603 s4->sin_family = AF_INET;
601 s4->sin_port = htons(53); 604 s4->sin_port = htons (53);
602 s4->sin_addr = i4; 605 s4->sin_addr = i4;
603#if HAVE_SOCKADDR_IN_SIN_LEN 606#if HAVE_SOCKADDR_IN_SIN_LEN
604 s4->sin_len = (u_char)sizeof(struct sockaddr_in); 607 s4->sin_len = (u_char) sizeof(struct sockaddr_in);
605#endif 608#endif
606 } 609 }
607 else if (1 == inet_pton(AF_INET6, dns_ip, &i6)) 610 else if (1 == inet_pton (AF_INET6, dns_ip, &i6))
608 { 611 {
609 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&ds->ss; 612 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &ds->ss;
610 613
611 s6->sin6_family = AF_INET6; 614 s6->sin6_family = AF_INET6;
612 s6->sin6_port = htons(53); 615 s6->sin6_port = htons (53);
613 s6->sin6_addr = i6; 616 s6->sin6_addr = i6;
614#if HAVE_SOCKADDR_IN_SIN_LEN 617#if HAVE_SOCKADDR_IN_SIN_LEN
615 s6->sin6_len = (u_char)sizeof(struct sockaddr_in6); 618 s6->sin6_len = (u_char) sizeof(struct sockaddr_in6);
616#endif 619#endif
617 } 620 }
618 else 621 else
619 { 622 {
620 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Malformed IP address `%s' for DNS server\n", 624 "Malformed IP address `%s' for DNS server\n",
622 dns_ip); 625 dns_ip);
623 GNUNET_free(ds); 626 GNUNET_free (ds);
624 return GNUNET_SYSERR; 627 return GNUNET_SYSERR;
625 } 628 }
626 GNUNET_CONTAINER_DLL_insert(ctx->dns_head, ctx->dns_tail, ds); 629 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, ctx->dns_tail, ds);
627 return GNUNET_OK; 630 return GNUNET_OK;
628} 631}
629 632
@@ -637,28 +640,28 @@ GNUNET_DNSSTUB_add_dns_ip(struct GNUNET_DNSSTUB_Context *ctx,
637 * @return #GNUNET_OK on success 640 * @return #GNUNET_OK on success
638 */ 641 */
639int 642int
640GNUNET_DNSSTUB_add_dns_sa(struct GNUNET_DNSSTUB_Context *ctx, 643GNUNET_DNSSTUB_add_dns_sa (struct GNUNET_DNSSTUB_Context *ctx,
641 const struct sockaddr *sa) 644 const struct sockaddr *sa)
642{ 645{
643 struct DnsServer *ds; 646 struct DnsServer *ds;
644 647
645 ds = GNUNET_new(struct DnsServer); 648 ds = GNUNET_new (struct DnsServer);
646 switch (sa->sa_family) 649 switch (sa->sa_family)
647 { 650 {
648 case AF_INET: 651 case AF_INET:
649 GNUNET_memcpy(&ds->ss, sa, sizeof(struct sockaddr_in)); 652 GNUNET_memcpy (&ds->ss, sa, sizeof(struct sockaddr_in));
650 break; 653 break;
651 654
652 case AF_INET6: 655 case AF_INET6:
653 GNUNET_memcpy(&ds->ss, sa, sizeof(struct sockaddr_in6)); 656 GNUNET_memcpy (&ds->ss, sa, sizeof(struct sockaddr_in6));
654 break; 657 break;
655 658
656 default: 659 default:
657 GNUNET_break(0); 660 GNUNET_break (0);
658 GNUNET_free(ds); 661 GNUNET_free (ds);
659 return GNUNET_SYSERR; 662 return GNUNET_SYSERR;
660 } 663 }
661 GNUNET_CONTAINER_DLL_insert(ctx->dns_head, ctx->dns_tail, ds); 664 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, ctx->dns_tail, ds);
662 return GNUNET_OK; 665 return GNUNET_OK;
663} 666}
664 667
@@ -671,8 +674,8 @@ GNUNET_DNSSTUB_add_dns_sa(struct GNUNET_DNSSTUB_Context *ctx,
671 * @param retry_freq how long to wait between retries 674 * @param retry_freq how long to wait between retries
672 */ 675 */
673void 676void
674GNUNET_DNSSTUB_set_retry(struct GNUNET_DNSSTUB_Context *ctx, 677GNUNET_DNSSTUB_set_retry (struct GNUNET_DNSSTUB_Context *ctx,
675 struct GNUNET_TIME_Relative retry_freq) 678 struct GNUNET_TIME_Relative retry_freq)
676{ 679{
677 ctx->retry_freq = retry_freq; 680 ctx->retry_freq = retry_freq;
678} 681}
@@ -684,19 +687,19 @@ GNUNET_DNSSTUB_set_retry(struct GNUNET_DNSSTUB_Context *ctx,
684 * @param ctx stub resolver to clean up 687 * @param ctx stub resolver to clean up
685 */ 688 */
686void 689void
687GNUNET_DNSSTUB_stop(struct GNUNET_DNSSTUB_Context *ctx) 690GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
688{ 691{
689 struct DnsServer *ds; 692 struct DnsServer *ds;
690 693
691 while (NULL != (ds = ctx->dns_head)) 694 while (NULL != (ds = ctx->dns_head))
692 { 695 {
693 GNUNET_CONTAINER_DLL_remove(ctx->dns_head, ctx->dns_tail, ds); 696 GNUNET_CONTAINER_DLL_remove (ctx->dns_head, ctx->dns_tail, ds);
694 GNUNET_free(ds); 697 GNUNET_free (ds);
695 } 698 }
696 for (unsigned int i = 0; i < ctx->num_sockets; i++) 699 for (unsigned int i = 0; i < ctx->num_sockets; i++)
697 cleanup_rs(&ctx->sockets[i]); 700 cleanup_rs (&ctx->sockets[i]);
698 GNUNET_free(ctx->sockets); 701 GNUNET_free (ctx->sockets);
699 GNUNET_free(ctx); 702 GNUNET_free (ctx);
700} 703}
701 704
702 705
diff --git a/src/util/getopt.c b/src/util/getopt.c
index 3b144457a..8ec148e15 100644
--- a/src/util/getopt.c
+++ b/src/util/getopt.c
@@ -47,17 +47,17 @@
47#endif 47#endif
48#endif 48#endif
49 49
50#define LOG(kind, ...) GNUNET_log_from(kind, "util-getopt", __VA_ARGS__) 50#define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__)
51 51
52#define LOG_STRERROR(kind, syscall) \ 52#define LOG_STRERROR(kind, syscall) \
53 GNUNET_log_from_strerror(kind, "util-getopt", syscall) 53 GNUNET_log_from_strerror (kind, "util-getopt", syscall)
54 54
55#ifndef _ 55#ifndef _
56/* This is for other GNU distributions with internationalized messages. 56/* This is for other GNU distributions with internationalized messages.
57 When compiling libc, the _ macro is predefined. */ 57 When compiling libc, the _ macro is predefined. */
58#ifdef HAVE_LIBINTL_H 58#ifdef HAVE_LIBINTL_H
59#include <libintl.h> 59#include <libintl.h>
60#define _(msgid) gettext(msgid) 60#define _(msgid) gettext (msgid)
61#else 61#else
62#define _(msgid) (msgid) 62#define _(msgid) (msgid)
63#endif 63#endif
@@ -84,7 +84,8 @@
84 one). For long options that have a zero `flag' field, `getopt' 84 one). For long options that have a zero `flag' field, `getopt'
85 returns the contents of the `val' field. */ 85 returns the contents of the `val' field. */
86 86
87struct GNoption { 87struct GNoption
88{
88 const char *name; 89 const char *name;
89 /* has_arg can't be an enum because some compilers complain about 90 /* has_arg can't be an enum because some compilers complain about
90 * type mismatches in all the code that assumes it is an int. */ 91 * type mismatches in all the code that assumes it is an int. */
@@ -188,17 +189,17 @@ static char *posixly_correct;
188 whose names are inconsistent. */ 189 whose names are inconsistent. */
189 190
190char * 191char *
191getenv(); 192getenv ();
192 193
193static char * 194static char *
194my_index(const char *str, int chr) 195my_index (const char *str, int chr)
195{ 196{
196 while (*str) 197 while (*str)
197 { 198 {
198 if (*str == chr) 199 if (*str == chr)
199 return (char *)str; 200 return (char *) str;
200 str++; 201 str++;
201 } 202 }
202 return 0; 203 return 0;
203} 204}
204 205
@@ -207,11 +208,11 @@ my_index(const char *str, int chr)
207#ifdef __GNUC__ 208#ifdef __GNUC__
208/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. 209/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
209 That was relevant to code that was here before. */ 210 That was relevant to code that was here before. */
210#if !defined(__STDC__) || !__STDC__ 211#if ! defined(__STDC__) || ! __STDC__
211/* gcc with -traditional declares the built-in strlen to return int, 212/* gcc with -traditional declares the built-in strlen to return int,
212 and has done so at least since version 2.4.5. -- rms. */ 213 and has done so at least since version 2.4.5. -- rms. */
213extern int 214extern int
214strlen(const char *); 215strlen (const char *);
215#endif /* not __STDC__ */ 216#endif /* not __STDC__ */
216#endif /* __GNUC__ */ 217#endif /* __GNUC__ */
217 218
@@ -239,11 +240,11 @@ static int last_nonopt;
239 240
240#if defined(__STDC__) && __STDC__ 241#if defined(__STDC__) && __STDC__
241static void 242static void
242exchange(char **); 243exchange (char **);
243#endif 244#endif
244 245
245static void 246static void
246exchange(char **argv) 247exchange (char **argv)
247{ 248{
248 int bottom = first_nonopt; 249 int bottom = first_nonopt;
249 int middle = last_nonopt; 250 int middle = last_nonopt;
@@ -256,42 +257,42 @@ exchange(char **argv)
256 * but it consists of two parts that need to be swapped next. */ 257 * but it consists of two parts that need to be swapped next. */
257 258
258 while (top > middle && middle > bottom) 259 while (top > middle && middle > bottom)
260 {
261 if (top - middle > middle - bottom)
259 { 262 {
260 if (top - middle > middle - bottom) 263 /* Bottom segment is the short one. */
261 { 264 int len = middle - bottom;
262 /* Bottom segment is the short one. */ 265 register int i;
263 int len = middle - bottom;
264 register int i;
265 266
266 /* Swap it with the top part of the top segment. */ 267 /* Swap it with the top part of the top segment. */
267 for (i = 0; i < len; i++) 268 for (i = 0; i < len; i++)
268 { 269 {
269 tem = argv[bottom + i]; 270 tem = argv[bottom + i];
270 argv[bottom + i] = argv[top - (middle - bottom) + i]; 271 argv[bottom + i] = argv[top - (middle - bottom) + i];
271 argv[top - (middle - bottom) + i] = tem; 272 argv[top - (middle - bottom) + i] = tem;
272 SWAP_FLAGS(bottom + i, top - (middle - bottom) + i); 273 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
273 } 274 }
274 /* Exclude the moved bottom segment from further swapping. */ 275 /* Exclude the moved bottom segment from further swapping. */
275 top -= len; 276 top -= len;
276 } 277 }
277 else 278 else
278 { 279 {
279 /* Top segment is the short one. */ 280 /* Top segment is the short one. */
280 int len = top - middle; 281 int len = top - middle;
281 register int i; 282 register int i;
282 283
283 /* Swap it with the bottom part of the bottom segment. */ 284 /* Swap it with the bottom part of the bottom segment. */
284 for (i = 0; i < len; i++) 285 for (i = 0; i < len; i++)
285 { 286 {
286 tem = argv[bottom + i]; 287 tem = argv[bottom + i];
287 argv[bottom + i] = argv[middle + i]; 288 argv[bottom + i] = argv[middle + i];
288 argv[middle + i] = tem; 289 argv[middle + i] = tem;
289 SWAP_FLAGS(bottom + i, middle + i); 290 SWAP_FLAGS (bottom + i, middle + i);
290 } 291 }
291 /* Exclude the moved top segment from further swapping. */ 292 /* Exclude the moved top segment from further swapping. */
292 bottom += len; 293 bottom += len;
293 }
294 } 294 }
295 }
295 296
296 /* Update records for the slots the non-options now occupy. */ 297 /* Update records for the slots the non-options now occupy. */
297 298
@@ -303,10 +304,10 @@ exchange(char **argv)
303 304
304#if defined(__STDC__) && __STDC__ 305#if defined(__STDC__) && __STDC__
305static const char * 306static const char *
306_getopt_initialize(int, char *const *, const char *); 307_getopt_initialize (int, char *const *, const char *);
307#endif 308#endif
308static const char * 309static const char *
309_getopt_initialize(int argc, char *const *argv, const char *optstring) 310_getopt_initialize (int argc, char *const *argv, const char *optstring)
310{ 311{
311 /* Start processing options with ARGV-element 1 (since ARGV-element 0 312 /* Start processing options with ARGV-element 1 (since ARGV-element 0
312 * is the program name); the sequence of previously skipped 313 * is the program name); the sequence of previously skipped
@@ -316,20 +317,20 @@ _getopt_initialize(int argc, char *const *argv, const char *optstring)
316 317
317 nextchar = NULL; 318 nextchar = NULL;
318 319
319 posixly_correct = getenv("POSIXLY_CORRECT"); 320 posixly_correct = getenv ("POSIXLY_CORRECT");
320 321
321 /* Determine how to handle the ordering of options and nonoptions. */ 322 /* Determine how to handle the ordering of options and nonoptions. */
322 323
323 if (optstring[0] == '-') 324 if (optstring[0] == '-')
324 { 325 {
325 ordering = RETURN_IN_ORDER; 326 ordering = RETURN_IN_ORDER;
326 ++optstring; 327 ++optstring;
327 } 328 }
328 else if (optstring[0] == '+') 329 else if (optstring[0] == '+')
329 { 330 {
330 ordering = REQUIRE_ORDER; 331 ordering = REQUIRE_ORDER;
331 ++optstring; 332 ++optstring;
332 } 333 }
333 else if (posixly_correct != NULL) 334 else if (posixly_correct != NULL)
334 ordering = REQUIRE_ORDER; 335 ordering = REQUIRE_ORDER;
335 else 336 else
@@ -395,25 +396,25 @@ _getopt_initialize(int argc, char *const *argv, const char *optstring)
395 long-named options. */ 396 long-named options. */
396 397
397static int 398static int
398GN_getopt_internal(int argc, 399GN_getopt_internal (int argc,
399 char *const *argv, 400 char *const *argv,
400 const char *optstring, 401 const char *optstring,
401 const struct GNoption *longopts, 402 const struct GNoption *longopts,
402 int *longind, 403 int *longind,
403 int long_only) 404 int long_only)
404{ 405{
405 static int __getopt_initialized = 0; 406 static int __getopt_initialized = 0;
406 static int GNopterr = 1; 407 static int GNopterr = 1;
407 408
408 GNoptarg = NULL; 409 GNoptarg = NULL;
409 410
410 if (GNoptind == 0 || !__getopt_initialized) 411 if ((GNoptind == 0)|| ! __getopt_initialized)
411 { 412 {
412 if (GNoptind == 0) 413 if (GNoptind == 0)
413 GNoptind = 1; /* Don't scan ARGV[0], the program name. */ 414 GNoptind = 1; /* Don't scan ARGV[0], the program name. */
414 optstring = _getopt_initialize(argc, argv, optstring); 415 optstring = _getopt_initialize (argc, argv, optstring);
415 __getopt_initialized = 1; 416 __getopt_initialized = 1;
416 } 417 }
417 418
418 /* Test whether ARGV[GNoptind] points to a non-option argument. 419 /* Test whether ARGV[GNoptind] points to a non-option argument.
419 * Either it does not have option syntax, or there is an environment flag 420 * Either it does not have option syntax, or there is an environment flag
@@ -421,81 +422,81 @@ GN_getopt_internal(int argc,
421 * is only used when the used in the GNU libc. */ 422 * is only used when the used in the GNU libc. */
422#define NONOPTION_P (argv[GNoptind][0] != '-' || argv[GNoptind][1] == '\0') 423#define NONOPTION_P (argv[GNoptind][0] != '-' || argv[GNoptind][1] == '\0')
423 424
424 if (nextchar == NULL || *nextchar == '\0') 425 if ((nextchar == NULL)||(*nextchar == '\0'))
425 { 426 {
426 /* Advance to the next ARGV-element. */ 427 /* Advance to the next ARGV-element. */
427 428
428 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been 429 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been
429 * moved back by the user (who may also have changed the arguments). */ 430 * moved back by the user (who may also have changed the arguments). */
430 if (last_nonopt > GNoptind) 431 if (last_nonopt > GNoptind)
431 last_nonopt = GNoptind; 432 last_nonopt = GNoptind;
432 if (first_nonopt > GNoptind) 433 if (first_nonopt > GNoptind)
433 first_nonopt = GNoptind; 434 first_nonopt = GNoptind;
434 435
435 if (ordering == PERMUTE) 436 if (ordering == PERMUTE)
436 { 437 {
437 /* If we have just processed some options following some non-options, 438 /* If we have just processed some options following some non-options,
438 * exchange them so that the options come first. */ 439 * exchange them so that the options come first. */
439 440
440 if (first_nonopt != last_nonopt && last_nonopt != GNoptind) 441 if ((first_nonopt != last_nonopt) &&(last_nonopt != GNoptind) )
441 exchange((char **)argv); 442 exchange ((char **) argv);
442 else if (last_nonopt != GNoptind) 443 else if (last_nonopt != GNoptind)
443 first_nonopt = GNoptind; 444 first_nonopt = GNoptind;
444 445
445 /* Skip any additional non-options 446 /* Skip any additional non-options
446 * and extend the range of non-options previously skipped. */ 447 * and extend the range of non-options previously skipped. */
447 448
448 while (GNoptind < argc && NONOPTION_P) 449 while (GNoptind < argc && NONOPTION_P)
449 GNoptind++; 450 GNoptind++;
450 last_nonopt = GNoptind; 451 last_nonopt = GNoptind;
451 } 452 }
452 453
453 /* The special ARGV-element `--' means premature end of options. 454 /* The special ARGV-element `--' means premature end of options.
454 * Skip it like a null option, 455 * Skip it like a null option,
455 * then exchange with previous non-options as if it were an option, 456 * then exchange with previous non-options as if it were an option,
456 * then skip everything else like a non-option. */ 457 * then skip everything else like a non-option. */
457 if (GNoptind != argc && !strcmp(argv[GNoptind], "--")) 458 if ((GNoptind != argc) && ! strcmp (argv[GNoptind], "--"))
458 { 459 {
459 GNoptind++; 460 GNoptind++;
460 461
461 if (first_nonopt != last_nonopt && last_nonopt != GNoptind) 462 if ((first_nonopt != last_nonopt) &&(last_nonopt != GNoptind) )
462 exchange((char **)argv); 463 exchange ((char **) argv);
463 else if (first_nonopt == last_nonopt) 464 else if (first_nonopt == last_nonopt)
464 first_nonopt = GNoptind; 465 first_nonopt = GNoptind;
465 last_nonopt = argc; 466 last_nonopt = argc;
466 467
467 GNoptind = argc; 468 GNoptind = argc;
468 } 469 }
469 470
470 /* If we have done all the ARGV-elements, stop the scan 471 /* If we have done all the ARGV-elements, stop the scan
471 * and back over any non-options that we skipped and permuted. */ 472 * and back over any non-options that we skipped and permuted. */
472 473
473 if (GNoptind == argc) 474 if (GNoptind == argc)
474 { 475 {
475 /* Set the next-arg-index to point at the non-options 476 /* Set the next-arg-index to point at the non-options
476 * that we previously skipped, so the caller will digest them. */ 477 * that we previously skipped, so the caller will digest them. */
477 if (first_nonopt != last_nonopt) 478 if (first_nonopt != last_nonopt)
478 GNoptind = first_nonopt; 479 GNoptind = first_nonopt;
479 return -1; 480 return -1;
480 } 481 }
481 482
482 /* If we have come to a non-option and did not permute it, 483 /* If we have come to a non-option and did not permute it,
483 * either stop the scan or describe it to the caller and pass it by. */ 484 * either stop the scan or describe it to the caller and pass it by. */
484 485
485 if (NONOPTION_P) 486 if (NONOPTION_P)
486 { 487 {
487 if (ordering == REQUIRE_ORDER) 488 if (ordering == REQUIRE_ORDER)
488 return -1; 489 return -1;
489 GNoptarg = argv[GNoptind++]; 490 GNoptarg = argv[GNoptind++];
490 return 1; 491 return 1;
491 } 492 }
492 493
493 /* We have found another option-ARGV-element. 494 /* We have found another option-ARGV-element.
494 * Skip the initial punctuation. */ 495 * Skip the initial punctuation. */
495 496
496 nextchar = 497 nextchar =
497 (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-')); 498 (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-'));
498 } 499 }
499 500
500 /* Decode the current option-ARGV-element. */ 501 /* Decode the current option-ARGV-element. */
501 502
@@ -512,362 +513,362 @@ GN_getopt_internal(int argc,
512 * 513 *
513 * This distinction seems to be the most useful approach. */ 514 * This distinction seems to be the most useful approach. */
514 515
515 if (longopts != NULL && 516 if ((longopts != NULL)&&
516 (argv[GNoptind][1] == '-' || 517 ((argv[GNoptind][1] == '-')||
517 (long_only && 518 (long_only &&
518 (argv[GNoptind][2] || !my_index(optstring, argv[GNoptind][1]))))) 519 (argv[GNoptind][2] || ! my_index (optstring, argv[GNoptind][1])))))
519 { 520 {
520 char *nameend; 521 char *nameend;
521 const struct GNoption *p; 522 const struct GNoption *p;
522 const struct GNoption *pfound = NULL; 523 const struct GNoption *pfound = NULL;
523 int exact = 0; 524 int exact = 0;
524 int ambig = 0; 525 int ambig = 0;
525 int indfound = -1; 526 int indfound = -1;
526 int option_index; 527 int option_index;
528
529 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
530 /* Do nothing. */;
531
532 /* Test all long options for either exact match
533 * or abbreviated matches. */
534 for (p = longopts, option_index = 0; p->name; p++, option_index++)
535 if (! strncmp (p->name, nextchar, nameend - nextchar))
536 {
537 if ((unsigned int) (nameend - nextchar) ==
538 (unsigned int) strlen (p->name))
539 {
540 /* Exact match found. */
541 pfound = p;
542 indfound = option_index;
543 exact = 1;
544 break;
545 }
546 else if (pfound == NULL)
547 {
548 /* First nonexact match found. */
549 pfound = p;
550 indfound = option_index;
551 }
552 else
553 /* Second or later nonexact match found. */
554 ambig = 1;
555 }
527 556
528 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 557 if (ambig && ! exact)
529 /* Do nothing. */; 558 {
559 if (GNopterr)
560 fprintf (stderr,
561 _ ("%s: option `%s' is ambiguous\n"),
562 argv[0],
563 argv[GNoptind]);
564 nextchar += strlen (nextchar);
565 GNoptind++;
566 return '?';
567 }
530 568
531 /* Test all long options for either exact match 569 if (pfound != NULL)
532 * or abbreviated matches. */ 570 {
533 for (p = longopts, option_index = 0; p->name; p++, option_index++) 571 option_index = indfound;
534 if (!strncmp(p->name, nextchar, nameend - nextchar)) 572 GNoptind++;
573 if (*nameend)
574 {
575 /* Don't test has_arg with >, because some C compilers don't
576 * allow it to be used on enums. */
577 if (pfound->has_arg)
578 GNoptarg = nameend + 1;
579 else
580 {
581 if (GNopterr)
535 { 582 {
536 if ((unsigned int)(nameend - nextchar) == 583 if (argv[GNoptind - 1][1] == '-')
537 (unsigned int)strlen(p->name)) 584 /* --option */
538 { 585 fprintf (stderr,
539 /* Exact match found. */ 586 _ ("%s: option `--%s' does not allow an argument\n"),
540 pfound = p; 587 argv[0],
541 indfound = option_index; 588 pfound->name);
542 exact = 1;
543 break;
544 }
545 else if (pfound == NULL)
546 {
547 /* First nonexact match found. */
548 pfound = p;
549 indfound = option_index;
550 }
551 else 589 else
552 /* Second or later nonexact match found. */ 590 /* +option or -option */
553 ambig = 1; 591 fprintf (stderr,
592 _ ("%s: option `%c%s' does not allow an argument\n"),
593 argv[0],
594 argv[GNoptind - 1][0],
595 pfound->name);
554 } 596 }
555 597 nextchar += strlen (nextchar);
556 if (ambig && !exact)
557 {
558 if (GNopterr)
559 fprintf(stderr,
560 _("%s: option `%s' is ambiguous\n"),
561 argv[0],
562 argv[GNoptind]);
563 nextchar += strlen(nextchar);
564 GNoptind++;
565 return '?'; 598 return '?';
566 } 599 }
567 600 }
568 if (pfound != NULL) 601 else if (pfound->has_arg == 1)
602 {
603 if (GNoptind < argc)
569 { 604 {
570 option_index = indfound; 605 GNoptarg = argv[GNoptind++];
571 GNoptind++;
572 if (*nameend)
573 {
574 /* Don't test has_arg with >, because some C compilers don't
575 * allow it to be used on enums. */
576 if (pfound->has_arg)
577 GNoptarg = nameend + 1;
578 else
579 {
580 if (GNopterr)
581 {
582 if (argv[GNoptind - 1][1] == '-')
583 /* --option */
584 fprintf(stderr,
585 _("%s: option `--%s' does not allow an argument\n"),
586 argv[0],
587 pfound->name);
588 else
589 /* +option or -option */
590 fprintf(stderr,
591 _("%s: option `%c%s' does not allow an argument\n"),
592 argv[0],
593 argv[GNoptind - 1][0],
594 pfound->name);
595 }
596 nextchar += strlen(nextchar);
597 return '?';
598 }
599 }
600 else if (pfound->has_arg == 1)
601 {
602 if (GNoptind < argc)
603 {
604 GNoptarg = argv[GNoptind++];
605 }
606 else
607 {
608 if (GNopterr)
609 {
610 fprintf(stderr,
611 _("%s: option `%s' requires an argument\n"),
612 argv[0],
613 argv[GNoptind - 1]);
614 }
615 nextchar += strlen(nextchar);
616 return (optstring[0] == ':') ? ':' : '?';
617 }
618 }
619 nextchar += strlen(nextchar);
620 if (longind != NULL)
621 *longind = option_index;
622 if (pfound->flag)
623 {
624 *(pfound->flag) = pfound->val;
625 return 0;
626 }
627 return pfound->val;
628 } 606 }
629 607 else
630 /* Can't find it as a long option. If this is not getopt_long_only,
631 * or the option starts with '--' or is not a valid short
632 * option, then it's an error.
633 * Otherwise interpret it as a short option. */
634 if (!long_only || argv[GNoptind][1] == '-' ||
635 my_index(optstring, *nextchar) == NULL)
636 { 608 {
637 if (GNopterr) 609 if (GNopterr)
638 { 610 {
639 if (argv[GNoptind][1] == '-') 611 fprintf (stderr,
640 /* --option */ 612 _ ("%s: option `%s' requires an argument\n"),
641 fprintf(stderr, 613 argv[0],
642 _("%s: unrecognized option `--%s'\n"), 614 argv[GNoptind - 1]);
643 argv[0], 615 }
644 nextchar); 616 nextchar += strlen (nextchar);
645 else 617 return (optstring[0] == ':') ? ':' : '?';
646 /* +option or -option */
647 fprintf(stderr,
648 _("%s: unrecognized option `%c%s'\n"),
649 argv[0],
650 argv[GNoptind][0],
651 nextchar);
652 }
653 nextchar = (char *)"";
654 GNoptind++;
655 return '?';
656 } 618 }
619 }
620 nextchar += strlen (nextchar);
621 if (longind != NULL)
622 *longind = option_index;
623 if (pfound->flag)
624 {
625 *(pfound->flag) = pfound->val;
626 return 0;
627 }
628 return pfound->val;
657 } 629 }
658 630
631 /* Can't find it as a long option. If this is not getopt_long_only,
632 * or the option starts with '--' or is not a valid short
633 * option, then it's an error.
634 * Otherwise interpret it as a short option. */
635 if (! long_only ||(argv[GNoptind][1] == '-') ||
636 (my_index (optstring, *nextchar) == NULL) )
637 {
638 if (GNopterr)
639 {
640 if (argv[GNoptind][1] == '-')
641 /* --option */
642 fprintf (stderr,
643 _ ("%s: unrecognized option `--%s'\n"),
644 argv[0],
645 nextchar);
646 else
647 /* +option or -option */
648 fprintf (stderr,
649 _ ("%s: unrecognized option `%c%s'\n"),
650 argv[0],
651 argv[GNoptind][0],
652 nextchar);
653 }
654 nextchar = (char *) "";
655 GNoptind++;
656 return '?';
657 }
658 }
659
659 /* Look at and handle the next short option-character. */ 660 /* Look at and handle the next short option-character. */
660 661
661 { 662 {
662 char c = *nextchar++; 663 char c = *nextchar++;
663 char *temp = my_index(optstring, c); 664 char *temp = my_index (optstring, c);
664 665
665 /* Increment `GNoptind' when we start to process its last character. */ 666 /* Increment `GNoptind' when we start to process its last character. */
666 if (*nextchar == '\0') 667 if (*nextchar == '\0')
667 ++GNoptind; 668 ++GNoptind;
668 669
669 if (temp == NULL || c == ':') 670 if ((temp == NULL)||(c == ':'))
671 {
672 if (GNopterr)
673 {
674 if (posixly_correct)
675 /* 1003.2 specifies the format of this message. */
676 fprintf (stderr, _ ("%s: illegal option -- %c\n"), argv[0], c);
677 else
678 fprintf (stderr, _ ("%s: invalid option -- %c\n"), argv[0], c);
679 }
680 return '?';
681 }
682 /* Convenience. Treat POSIX -W foo same as long option --foo */
683 if ((temp[0] == 'W')&&(temp[1] == ';'))
684 {
685 char *nameend;
686 const struct GNoption *p;
687 const struct GNoption *pfound = NULL;
688 int exact = 0;
689 int ambig = 0;
690 int indfound = 0;
691 int option_index;
692
693 /* This is an option that requires an argument. */
694 if (*nextchar != '\0')
695 {
696 GNoptarg = nextchar;
697 /* If we end this ARGV-element by taking the rest as an arg,
698 * we must advance to the next element now. */
699 GNoptind++;
700 }
701 else if (GNoptind == argc)
670 { 702 {
671 if (GNopterr) 703 if (GNopterr)
704 {
705 /* 1003.2 specifies the format of this message. */
706 fprintf (stderr,
707 _ ("%s: option requires an argument -- %c\n"),
708 argv[0],
709 c);
710 }
711 if (optstring[0] == ':')
712 c = ':';
713 else
714 c = '?';
715 return c;
716 }
717 else
718 /* We already incremented `GNoptind' once;
719 * increment it again when taking next ARGV-elt as argument. */
720 GNoptarg = argv[GNoptind++];
721
722 /* GNoptarg is now the argument, see if it's in the
723 * table of longopts. */
724
725 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '=';
726 nameend++)
727 /* Do nothing. */;
728
729 /* Test all long options for either exact match
730 * or abbreviated matches. */
731 if (longopts != NULL)
732 for (p = longopts, option_index = 0; p->name; p++, option_index++)
733 if (! strncmp (p->name, nextchar, nameend - nextchar))
672 { 734 {
673 if (posixly_correct) 735 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
674 /* 1003.2 specifies the format of this message. */ 736 {
675 fprintf(stderr, _("%s: illegal option -- %c\n"), argv[0], c); 737 /* Exact match found. */
738 pfound = p;
739 indfound = option_index;
740 exact = 1;
741 break;
742 }
743 else if (pfound == NULL)
744 {
745 /* First nonexact match found. */
746 pfound = p;
747 indfound = option_index;
748 }
676 else 749 else
677 fprintf(stderr, _("%s: invalid option -- %c\n"), argv[0], c); 750 /* Second or later nonexact match found. */
751 ambig = 1;
678 } 752 }
753 if (ambig && ! exact)
754 {
755 if (GNopterr)
756 fprintf (stderr,
757 _ ("%s: option `-W %s' is ambiguous\n"),
758 argv[0],
759 argv[GNoptind]);
760 nextchar += strlen (nextchar);
761 GNoptind++;
679 return '?'; 762 return '?';
680 } 763 }
681 /* Convenience. Treat POSIX -W foo same as long option --foo */ 764 if (pfound != NULL)
682 if (temp[0] == 'W' && temp[1] == ';')
683 { 765 {
684 char *nameend; 766 option_index = indfound;
685 const struct GNoption *p; 767 if (*nameend)
686 const struct GNoption *pfound = NULL; 768 {
687 int exact = 0; 769 /* Don't test has_arg with >, because some C compilers don't
688 int ambig = 0; 770 * allow it to be used on enums. */
689 int indfound = 0; 771 if (pfound->has_arg)
690 int option_index; 772 GNoptarg = nameend + 1;
691 773 else
692 /* This is an option that requires an argument. */
693 if (*nextchar != '\0')
694 {
695 GNoptarg = nextchar;
696 /* If we end this ARGV-element by taking the rest as an arg,
697 * we must advance to the next element now. */
698 GNoptind++;
699 }
700 else if (GNoptind == argc)
701 { 774 {
702 if (GNopterr) 775 if (GNopterr)
703 { 776 fprintf (stderr,
704 /* 1003.2 specifies the format of this message. */ 777 _ ("%s: option `-W %s' does not allow an argument\n"),
705 fprintf(stderr, 778 argv[0],
706 _("%s: option requires an argument -- %c\n"), 779 pfound->name);
707 argv[0],
708 c);
709 }
710 if (optstring[0] == ':')
711 c = ':';
712 else
713 c = '?';
714 return c;
715 }
716 else
717 /* We already incremented `GNoptind' once;
718 * increment it again when taking next ARGV-elt as argument. */
719 GNoptarg = argv[GNoptind++];
720 780
721 /* GNoptarg is now the argument, see if it's in the 781 nextchar += strlen (nextchar);
722 * table of longopts. */
723
724 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '=';
725 nameend++)
726 /* Do nothing. */;
727
728 /* Test all long options for either exact match
729 * or abbreviated matches. */
730 if (longopts != NULL)
731 for (p = longopts, option_index = 0; p->name; p++, option_index++)
732 if (!strncmp(p->name, nextchar, nameend - nextchar))
733 {
734 if ((unsigned int)(nameend - nextchar) == strlen(p->name))
735 {
736 /* Exact match found. */
737 pfound = p;
738 indfound = option_index;
739 exact = 1;
740 break;
741 }
742 else if (pfound == NULL)
743 {
744 /* First nonexact match found. */
745 pfound = p;
746 indfound = option_index;
747 }
748 else
749 /* Second or later nonexact match found. */
750 ambig = 1;
751 }
752 if (ambig && !exact)
753 {
754 if (GNopterr)
755 fprintf(stderr,
756 _("%s: option `-W %s' is ambiguous\n"),
757 argv[0],
758 argv[GNoptind]);
759 nextchar += strlen(nextchar);
760 GNoptind++;
761 return '?'; 782 return '?';
762 } 783 }
763 if (pfound != NULL) 784 }
785 else if (pfound->has_arg == 1)
786 {
787 if (GNoptind < argc)
788 GNoptarg = argv[GNoptind++];
789 else
764 { 790 {
765 option_index = indfound; 791 if (GNopterr)
766 if (*nameend) 792 fprintf (stderr,
767 { 793 _ ("%s: option `%s' requires an argument\n"),
768 /* Don't test has_arg with >, because some C compilers don't 794 argv[0],
769 * allow it to be used on enums. */ 795 argv[GNoptind - 1]);
770 if (pfound->has_arg) 796 nextchar += strlen (nextchar);
771 GNoptarg = nameend + 1; 797 return optstring[0] == ':' ? ':' : '?';
772 else
773 {
774 if (GNopterr)
775 fprintf(stderr,
776 _("%s: option `-W %s' does not allow an argument\n"),
777 argv[0],
778 pfound->name);
779
780 nextchar += strlen(nextchar);
781 return '?';
782 }
783 }
784 else if (pfound->has_arg == 1)
785 {
786 if (GNoptind < argc)
787 GNoptarg = argv[GNoptind++];
788 else
789 {
790 if (GNopterr)
791 fprintf(stderr,
792 _("%s: option `%s' requires an argument\n"),
793 argv[0],
794 argv[GNoptind - 1]);
795 nextchar += strlen(nextchar);
796 return optstring[0] == ':' ? ':' : '?';
797 }
798 }
799 nextchar += strlen(nextchar);
800 if (longind != NULL)
801 *longind = option_index;
802 if (pfound->flag)
803 {
804 *(pfound->flag) = pfound->val;
805 return 0;
806 }
807 return pfound->val;
808 } 798 }
809 nextchar = NULL; 799 }
810 return 'W'; /* Let the application handle it. */ 800 nextchar += strlen (nextchar);
801 if (longind != NULL)
802 *longind = option_index;
803 if (pfound->flag)
804 {
805 *(pfound->flag) = pfound->val;
806 return 0;
807 }
808 return pfound->val;
811 } 809 }
810 nextchar = NULL;
811 return 'W'; /* Let the application handle it. */
812 }
812 if (temp[1] == ':') 813 if (temp[1] == ':')
814 {
815 if (temp[2] == ':')
813 { 816 {
814 if (temp[2] == ':') 817 /* This is an option that accepts an argument optionally. */
815 { 818 if (*nextchar != '\0')
816 /* This is an option that accepts an argument optionally. */ 819 {
817 if (*nextchar != '\0') 820 GNoptarg = nextchar;
818 { 821 GNoptind++;
819 GNoptarg = nextchar; 822 }
820 GNoptind++;
821 }
822 else
823 GNoptarg = NULL;
824 nextchar = NULL;
825 }
826 else 823 else
824 GNoptarg = NULL;
825 nextchar = NULL;
826 }
827 else
828 {
829 /* This is an option that requires an argument. */
830 if (*nextchar != '\0')
831 {
832 GNoptarg = nextchar;
833 /* If we end this ARGV-element by taking the rest as an arg,
834 * we must advance to the next element now. */
835 GNoptind++;
836 }
837 else if (GNoptind == argc)
838 {
839 if (GNopterr)
827 { 840 {
828 /* This is an option that requires an argument. */ 841 /* 1003.2 specifies the format of this message. */
829 if (*nextchar != '\0') 842 fprintf (stderr,
830 { 843 _ ("%s: option requires an argument -- %c\n"),
831 GNoptarg = nextchar; 844 argv[0],
832 /* If we end this ARGV-element by taking the rest as an arg, 845 c);
833 * we must advance to the next element now. */
834 GNoptind++;
835 }
836 else if (GNoptind == argc)
837 {
838 if (GNopterr)
839 {
840 /* 1003.2 specifies the format of this message. */
841 fprintf(stderr,
842 _("%s: option requires an argument -- %c\n"),
843 argv[0],
844 c);
845 }
846 if (optstring[0] == ':')
847 c = ':';
848 else
849 c = '?';
850 }
851 else
852 /* We already incremented `GNoptind' once;
853 * increment it again when taking next ARGV-elt as argument. */
854 GNoptarg = argv[GNoptind++];
855 nextchar = NULL;
856 } 846 }
847 if (optstring[0] == ':')
848 c = ':';
849 else
850 c = '?';
851 }
852 else
853 /* We already incremented `GNoptind' once;
854 * increment it again when taking next ARGV-elt as argument. */
855 GNoptarg = argv[GNoptind++];
856 nextchar = NULL;
857 } 857 }
858 }
858 return c; 859 return c;
859 } 860 }
860} 861}
861 862
862 863
863static int 864static int
864GNgetopt_long(int argc, 865GNgetopt_long (int argc,
865 char *const *argv, 866 char *const *argv,
866 const char *options, 867 const char *options,
867 const struct GNoption *long_options, 868 const struct GNoption *long_options,
868 int *opt_index) 869 int *opt_index)
869{ 870{
870 return GN_getopt_internal(argc, argv, options, long_options, opt_index, 0); 871 return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0);
871} 872}
872 873
873/* ******************** now the GNUnet specific modifications... ********************* */ 874/* ******************** now the GNUnet specific modifications... ********************* */
@@ -883,10 +884,10 @@ GNgetopt_long(int argc,
883 * argument, or #GNUNET_SYSERR on error 884 * argument, or #GNUNET_SYSERR on error
884 */ 885 */
885int 886int
886GNUNET_GETOPT_run(const char *binaryOptions, 887GNUNET_GETOPT_run (const char *binaryOptions,
887 const struct GNUNET_GETOPT_CommandLineOption *allOptions, 888 const struct GNUNET_GETOPT_CommandLineOption *allOptions,
888 unsigned int argc, 889 unsigned int argc,
889 char *const *argv) 890 char *const *argv)
890{ 891{
891 struct GNoption *long_options; 892 struct GNoption *long_options;
892 struct GNUNET_GETOPT_CommandLineProcessorContext clpc; 893 struct GNUNET_GETOPT_CommandLineProcessorContext clpc;
@@ -898,7 +899,7 @@ GNUNET_GETOPT_run(const char *binaryOptions,
898 unsigned int optmatch = 0; 899 unsigned int optmatch = 0;
899 const char *have_exclusive = NULL; 900 const char *have_exclusive = NULL;
900 901
901 GNUNET_assert(argc > 0); 902 GNUNET_assert (argc > 0);
902 GNoptind = 0; 903 GNoptind = 0;
903 clpc.binaryName = argv[0]; 904 clpc.binaryName = argv[0];
904 clpc.binaryOptions = binaryOptions; 905 clpc.binaryOptions = binaryOptions;
@@ -910,20 +911,20 @@ GNUNET_GETOPT_run(const char *binaryOptions,
910 911
911 /* transform our option representation into the format 912 /* transform our option representation into the format
912 used by the GNU getopt copylib */ 913 used by the GNU getopt copylib */
913 long_options = GNUNET_new_array(count + 1, struct GNoption); 914 long_options = GNUNET_new_array (count + 1, struct GNoption);
914 seen = GNUNET_new_array(count, uint8_t); 915 seen = GNUNET_new_array (count, uint8_t);
915 shorts = GNUNET_malloc(count * 2 + 1); 916 shorts = GNUNET_malloc (count * 2 + 1);
916 spos = 0; 917 spos = 0;
917 for (unsigned i = 0; i < count; i++) 918 for (unsigned i = 0; i < count; i++)
918 { 919 {
919 long_options[i].name = allOptions[i].name; 920 long_options[i].name = allOptions[i].name;
920 long_options[i].has_arg = allOptions[i].require_argument; 921 long_options[i].has_arg = allOptions[i].require_argument;
921 long_options[i].flag = NULL; 922 long_options[i].flag = NULL;
922 long_options[i].val = allOptions[i].shortName; 923 long_options[i].val = allOptions[i].shortName;
923 shorts[spos++] = allOptions[i].shortName; 924 shorts[spos++] = allOptions[i].shortName;
924 if (allOptions[i].require_argument != 0) 925 if (allOptions[i].require_argument != 0)
925 shorts[spos++] = ':'; 926 shorts[spos++] = ':';
926 } 927 }
927 long_options[count].name = NULL; 928 long_options[count].name = NULL;
928 long_options[count].has_arg = 0; 929 long_options[count].has_arg = 0;
929 long_options[count].flag = NULL; 930 long_options[count].flag = NULL;
@@ -933,74 +934,74 @@ GNUNET_GETOPT_run(const char *binaryOptions,
933 934
934 /* main getopt loop */ 935 /* main getopt loop */
935 while (1) 936 while (1)
936 { 937 {
937 int option_index = 0; 938 int option_index = 0;
938 unsigned int i; 939 unsigned int i;
939 int c; 940 int c;
940 941
941 c = GNgetopt_long(argc, argv, shorts, long_options, &option_index); 942 c = GNgetopt_long (argc, argv, shorts, long_options, &option_index);
942 if (c == GNUNET_SYSERR) 943 if (c == GNUNET_SYSERR)
943 break; /* No more flags to process */ 944 break; /* No more flags to process */
944 945
945 /* Check which of our program's options was given by the user */ 946 /* Check which of our program's options was given by the user */
946 for (i = 0; i < count; i++) 947 for (i = 0; i < count; i++)
947 { 948 {
948 clpc.currentArgument = GNoptind - 1; 949 clpc.currentArgument = GNoptind - 1;
949 if ((char)c == allOptions[i].shortName) 950 if ((char) c == allOptions[i].shortName)
950 { 951 {
951 optmatch++; 952 optmatch++;
952 if (allOptions[i].option_exclusive) 953 if (allOptions[i].option_exclusive)
953 have_exclusive = allOptions[i].name; 954 have_exclusive = allOptions[i].name;
954 if (GNUNET_OK == cont) 955 if (GNUNET_OK == cont)
955 {
956 /* parse the option using the option-specific processor */
957 cont = allOptions[i].processor(&clpc,
958 allOptions[i].scls,
959 allOptions[i].name,
960 GNoptarg);
961 }
962 seen[i] = 1;
963 break;
964 }
965 }
966 if (i == count)
967 { 956 {
968 fprintf(stderr, _("Use %s to get a list of options.\n"), "--help"); 957 /* parse the option using the option-specific processor */
969 cont = GNUNET_SYSERR; 958 cont = allOptions[i].processor (&clpc,
959 allOptions[i].scls,
960 allOptions[i].name,
961 GNoptarg);
970 } 962 }
963 seen[i] = 1;
964 break;
965 }
966 }
967 if (i == count)
968 {
969 fprintf (stderr, _ ("Use %s to get a list of options.\n"), "--help");
970 cont = GNUNET_SYSERR;
971 } 971 }
972 GNUNET_free(shorts); 972 }
973 GNUNET_free(long_options); 973 GNUNET_free (shorts);
974 GNUNET_free (long_options);
974 975
975 /* check that if any option that was marked as exclusive 976 /* check that if any option that was marked as exclusive
976 is the only option that was provided */ 977 is the only option that was provided */
977 if ((NULL != have_exclusive) && (optmatch > 1)) 978 if ((NULL != have_exclusive) && (optmatch > 1))
978 { 979 {
979 fprintf(stderr, 980 fprintf (stderr,
980 _("Option `%s' can't be used with other options.\n"), 981 _ ("Option `%s' can't be used with other options.\n"),
981 have_exclusive); 982 have_exclusive);
982 cont = GNUNET_SYSERR; 983 cont = GNUNET_SYSERR;
983 } 984 }
984 if (GNUNET_YES == cont) 985 if (GNUNET_YES == cont)
986 {
987 /* check that all mandatory options are present */
988 for (count = 0; NULL != allOptions[count].name; count++)
985 { 989 {
986 /* check that all mandatory options are present */ 990 if ((0 == seen[count]) && (allOptions[count].option_mandatory))
987 for (count = 0; NULL != allOptions[count].name; count++) 991 {
988 { 992 fprintf (stderr,
989 if ((0 == seen[count]) && (allOptions[count].option_mandatory)) 993 _ ("Missing mandatory option `%s'.\n"),
990 { 994 allOptions[count].name);
991 fprintf(stderr, 995 cont = GNUNET_SYSERR;
992 _("Missing mandatory option `%s'.\n"), 996 }
993 allOptions[count].name);
994 cont = GNUNET_SYSERR;
995 }
996 }
997 } 997 }
998 GNUNET_free(seen); 998 }
999 GNUNET_free (seen);
999 1000
1000 /* call cleaners, if available */ 1001 /* call cleaners, if available */
1001 for (unsigned int i = 0; NULL != allOptions[i].name; i++) 1002 for (unsigned int i = 0; NULL != allOptions[i].name; i++)
1002 if (NULL != allOptions[i].cleaner) 1003 if (NULL != allOptions[i].cleaner)
1003 allOptions[i].cleaner(allOptions[i].scls); 1004 allOptions[i].cleaner (allOptions[i].scls);
1004 1005
1005 if (GNUNET_OK != cont) 1006 if (GNUNET_OK != cont)
1006 return cont; 1007 return cont;
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index a5e752546..68da423b8 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -26,7 +26,7 @@
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29#define LOG(kind, ...) GNUNET_log_from(kind, "util-getopt", __VA_ARGS__) 29#define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__)
30 30
31 31
32/** 32/**
@@ -39,16 +39,16 @@
39 * @return #GNUNET_NO (do not continue, not an error) 39 * @return #GNUNET_NO (do not continue, not an error)
40 */ 40 */
41static int 41static int
42print_version(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 42print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
43 void *scls, 43 void *scls,
44 const char *option, 44 const char *option,
45 const char *value) 45 const char *value)
46{ 46{
47 const char *version = scls; 47 const char *version = scls;
48 48
49 (void)option; 49 (void) option;
50 (void)value; 50 (void) value;
51 printf("%s v%s\n", ctx->binaryName, version); 51 printf ("%s v%s\n", ctx->binaryName, version);
52 return GNUNET_NO; 52 return GNUNET_NO;
53} 53}
54 54
@@ -60,15 +60,15 @@ print_version(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
60 * @param version string with the version number 60 * @param version string with the version number
61 */ 61 */
62struct GNUNET_GETOPT_CommandLineOption 62struct GNUNET_GETOPT_CommandLineOption
63GNUNET_GETOPT_option_version(const char *version) 63GNUNET_GETOPT_option_version (const char *version)
64{ 64{
65 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'v', 65 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'v',
66 .name = "version", 66 .name = "version",
67 .description = gettext_noop( 67 .description = gettext_noop (
68 "print the version number"), 68 "print the version number"),
69 .option_exclusive = 1, 69 .option_exclusive = 1,
70 .processor = &print_version, 70 .processor = &print_version,
71 .scls = (void *)version }; 71 .scls = (void *) version };
72 72
73 return clo; 73 return clo;
74} 74}
@@ -89,10 +89,10 @@ GNUNET_GETOPT_option_version(const char *version)
89 * @return #GNUNET_NO (do not continue, not an error) 89 * @return #GNUNET_NO (do not continue, not an error)
90 */ 90 */
91static int 91static int
92format_help(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 92format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
93 void *scls, 93 void *scls,
94 const char *option, 94 const char *option,
95 const char *value) 95 const char *value)
96{ 96{
97 const char *about = scls; 97 const char *about = scls;
98 size_t slen; 98 size_t slen;
@@ -105,86 +105,86 @@ format_help(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
105 const struct GNUNET_GETOPT_CommandLineOption *opt; 105 const struct GNUNET_GETOPT_CommandLineOption *opt;
106 const struct GNUNET_OS_ProjectData *pd; 106 const struct GNUNET_OS_ProjectData *pd;
107 107
108 (void)option; 108 (void) option;
109 (void)value; 109 (void) value;
110 if (NULL != about) 110 if (NULL != about)
111 { 111 {
112 printf("%s\n%s\n", ctx->binaryOptions, gettext(about)); 112 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
113 printf(_( 113 printf (_ (
114 "Arguments mandatory for long options are also mandatory for short options.\n")); 114 "Arguments mandatory for long options are also mandatory for short options.\n"));
115 } 115 }
116 i = 0; 116 i = 0;
117 opt = ctx->allOptions; 117 opt = ctx->allOptions;
118 while (NULL != opt[i].description) 118 while (NULL != opt[i].description)
119 {
120 if (opt[i].shortName == '\0')
121 printf (" ");
122 else
123 printf (" -%c, ", opt[i].shortName);
124 printf ("--%s", opt[i].name);
125 slen = 8 + strlen (opt[i].name);
126 if (NULL != opt[i].argumentHelp)
119 { 127 {
120 if (opt[i].shortName == '\0') 128 printf ("=%s", opt[i].argumentHelp);
121 printf(" "); 129 slen += 1 + strlen (opt[i].argumentHelp);
122 else 130 }
123 printf(" -%c, ", opt[i].shortName); 131 if (slen > BORDER)
124 printf("--%s", opt[i].name); 132 {
125 slen = 8 + strlen(opt[i].name); 133 printf ("\n%*s", BORDER, "");
126 if (NULL != opt[i].argumentHelp) 134 slen = BORDER;
127 { 135 }
128 printf("=%s", opt[i].argumentHelp); 136 if (slen < BORDER)
129 slen += 1 + strlen(opt[i].argumentHelp); 137 {
130 } 138 printf ("%*s", (int) (BORDER - slen), "");
131 if (slen > BORDER) 139 slen = BORDER;
132 { 140 }
133 printf("\n%*s", BORDER, ""); 141 if (0 < strlen (opt[i].description))
134 slen = BORDER; 142 trans = gettext (opt[i].description);
135 } 143 else
136 if (slen < BORDER) 144 trans = "";
137 { 145 ml = strlen (trans);
138 printf("%*s", (int)(BORDER - slen), ""); 146 p = 0;
139 slen = BORDER;
140 }
141 if (0 < strlen(opt[i].description))
142 trans = gettext(opt[i].description);
143 else
144 trans = "";
145 ml = strlen(trans);
146 p = 0;
147OUTER: 147OUTER:
148 while (ml - p > 78 - slen) 148 while (ml - p > 78 - slen)
149 {
150 for (j = p + 78 - slen; j > (int) p; j--)
151 {
152 if (isspace ((unsigned char) trans[j]))
149 { 153 {
150 for (j = p + 78 - slen; j > (int)p; j--) 154 scp = GNUNET_malloc (j - p + 1);
151 { 155 GNUNET_memcpy (scp, &trans[p], j - p);
152 if (isspace((unsigned char)trans[j])) 156 scp[j - p] = '\0';
153 { 157 printf ("%s\n%*s", scp, BORDER + 2, "");
154 scp = GNUNET_malloc(j - p + 1); 158 GNUNET_free (scp);
155 GNUNET_memcpy(scp, &trans[p], j - p); 159 p = j + 1;
156 scp[j - p] = '\0';
157 printf("%s\n%*s", scp, BORDER + 2, "");
158 GNUNET_free(scp);
159 p = j + 1;
160 slen = BORDER + 2;
161 goto OUTER;
162 }
163 }
164 /* could not find space to break line */
165 scp = GNUNET_malloc(78 - slen + 1);
166 GNUNET_memcpy(scp, &trans[p], 78 - slen);
167 scp[78 - slen] = '\0';
168 printf("%s\n%*s", scp, BORDER + 2, "");
169 GNUNET_free(scp);
170 slen = BORDER + 2; 160 slen = BORDER + 2;
171 p = p + 78 - slen; 161 goto OUTER;
172 } 162 }
173 /* print rest */ 163 }
174 if (p < ml) 164 /* could not find space to break line */
175 printf("%s\n", &trans[p]); 165 scp = GNUNET_malloc (78 - slen + 1);
176 if (strlen(trans) == 0) 166 GNUNET_memcpy (scp, &trans[p], 78 - slen);
177 printf("\n"); 167 scp[78 - slen] = '\0';
178 i++; 168 printf ("%s\n%*s", scp, BORDER + 2, "");
169 GNUNET_free (scp);
170 slen = BORDER + 2;
171 p = p + 78 - slen;
179 } 172 }
180 pd = GNUNET_OS_project_data_get(); 173 /* print rest */
181 printf("Report bugs to %s.\n" 174 if (p < ml)
182 "Home page: %s\n", 175 printf ("%s\n", &trans[p]);
183 pd->bug_email, 176 if (strlen (trans) == 0)
184 pd->homepage); 177 printf ("\n");
178 i++;
179 }
180 pd = GNUNET_OS_project_data_get ();
181 printf ("Report bugs to %s.\n"
182 "Home page: %s\n",
183 pd->bug_email,
184 pd->homepage);
185 185
186 if (0 != pd->is_gnu) 186 if (0 != pd->is_gnu)
187 printf("General help using GNU software: http://www.gnu.org/gethelp/\n"); 187 printf ("General help using GNU software: http://www.gnu.org/gethelp/\n");
188 188
189 return GNUNET_NO; 189 return GNUNET_NO;
190} 190}
@@ -197,15 +197,15 @@ OUTER:
197 * @param about string with brief description of the application 197 * @param about string with brief description of the application
198 */ 198 */
199struct GNUNET_GETOPT_CommandLineOption 199struct GNUNET_GETOPT_CommandLineOption
200GNUNET_GETOPT_option_help(const char *about) 200GNUNET_GETOPT_option_help (const char *about)
201{ 201{
202 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'h', 202 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'h',
203 .name = "help", 203 .name = "help",
204 .description = gettext_noop( 204 .description = gettext_noop (
205 "print this help"), 205 "print this help"),
206 .option_exclusive = 1, 206 .option_exclusive = 1,
207 .processor = format_help, 207 .processor = format_help,
208 .scls = (void *)about }; 208 .scls = (void *) about };
209 209
210 return clo; 210 return clo;
211} 211}
@@ -226,16 +226,16 @@ GNUNET_GETOPT_option_help(const char *about)
226 * @return #GNUNET_OK 226 * @return #GNUNET_OK
227 */ 227 */
228static int 228static int
229increment_value(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 229increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
230 void *scls, 230 void *scls,
231 const char *option, 231 const char *option,
232 const char *value) 232 const char *value)
233{ 233{
234 unsigned int *val = scls; 234 unsigned int *val = scls;
235 235
236 (void)ctx; 236 (void) ctx;
237 (void)option; 237 (void) option;
238 (void)value; 238 (void) value;
239 (*val)++; 239 (*val)++;
240 return GNUNET_OK; 240 return GNUNET_OK;
241} 241}
@@ -251,16 +251,16 @@ increment_value(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
251 * @param[out] val increment by 1 each time the option is present 251 * @param[out] val increment by 1 each time the option is present
252 */ 252 */
253struct GNUNET_GETOPT_CommandLineOption 253struct GNUNET_GETOPT_CommandLineOption
254GNUNET_GETOPT_option_increment_uint(char shortName, 254GNUNET_GETOPT_option_increment_uint (char shortName,
255 const char *name, 255 const char *name,
256 const char *description, 256 const char *description,
257 unsigned int *val) 257 unsigned int *val)
258{ 258{
259 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 259 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
260 .name = name, 260 .name = name,
261 .description = description, 261 .description = description,
262 .processor = &increment_value, 262 .processor = &increment_value,
263 .scls = (void *)val }; 263 .scls = (void *) val };
264 264
265 return clo; 265 return clo;
266} 266}
@@ -273,14 +273,14 @@ GNUNET_GETOPT_option_increment_uint(char shortName,
273 * @param[out] level set to the verbosity level 273 * @param[out] level set to the verbosity level
274 */ 274 */
275struct GNUNET_GETOPT_CommandLineOption 275struct GNUNET_GETOPT_CommandLineOption
276GNUNET_GETOPT_option_verbose(unsigned int *level) 276GNUNET_GETOPT_option_verbose (unsigned int *level)
277{ 277{
278 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'V', 278 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'V',
279 .name = "verbose", 279 .name = "verbose",
280 .description = 280 .description =
281 gettext_noop("be verbose"), 281 gettext_noop ("be verbose"),
282 .processor = &increment_value, 282 .processor = &increment_value,
283 .scls = (void *)level }; 283 .scls = (void *) level };
284 284
285 return clo; 285 return clo;
286} 286}
@@ -301,16 +301,16 @@ GNUNET_GETOPT_option_verbose(unsigned int *level)
301 * @return #GNUNET_OK 301 * @return #GNUNET_OK
302 */ 302 */
303static int 303static int
304set_one(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 304set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
305 void *scls, 305 void *scls,
306 const char *option, 306 const char *option,
307 const char *value) 307 const char *value)
308{ 308{
309 int *val = scls; 309 int *val = scls;
310 310
311 (void)ctx; 311 (void) ctx;
312 (void)option; 312 (void) option;
313 (void)value; 313 (void) value;
314 *val = 1; 314 *val = 1;
315 return GNUNET_OK; 315 return GNUNET_OK;
316} 316}
@@ -327,16 +327,16 @@ set_one(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
327 * @param[out] val set to 1 if the option is present 327 * @param[out] val set to 1 if the option is present
328 */ 328 */
329struct GNUNET_GETOPT_CommandLineOption 329struct GNUNET_GETOPT_CommandLineOption
330GNUNET_GETOPT_option_flag(char shortName, 330GNUNET_GETOPT_option_flag (char shortName,
331 const char *name, 331 const char *name,
332 const char *description, 332 const char *description,
333 int *val) 333 int *val)
334{ 334{
335 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 335 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
336 .name = name, 336 .name = name,
337 .description = description, 337 .description = description,
338 .processor = &set_one, 338 .processor = &set_one,
339 .scls = (void *)val }; 339 .scls = (void *) val };
340 340
341 return clo; 341 return clo;
342} 342}
@@ -357,18 +357,18 @@ GNUNET_GETOPT_option_flag(char shortName,
357 * @return #GNUNET_OK 357 * @return #GNUNET_OK
358 */ 358 */
359static int 359static int
360set_string(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 360set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
361 void *scls, 361 void *scls,
362 const char *option, 362 const char *option,
363 const char *value) 363 const char *value)
364{ 364{
365 char **val = scls; 365 char **val = scls;
366 366
367 (void)ctx; 367 (void) ctx;
368 (void)option; 368 (void) option;
369 GNUNET_assert(NULL != value); 369 GNUNET_assert (NULL != value);
370 GNUNET_free_non_null(*val); 370 GNUNET_free_non_null (*val);
371 *val = GNUNET_strdup(value); 371 *val = GNUNET_strdup (value);
372 return GNUNET_OK; 372 return GNUNET_OK;
373} 373}
374 374
@@ -383,11 +383,11 @@ set_string(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
383 * @param[out] str set to the string 383 * @param[out] str set to the string
384 */ 384 */
385struct GNUNET_GETOPT_CommandLineOption 385struct GNUNET_GETOPT_CommandLineOption
386GNUNET_GETOPT_option_string(char shortName, 386GNUNET_GETOPT_option_string (char shortName,
387 const char *name, 387 const char *name,
388 const char *argumentHelp, 388 const char *argumentHelp,
389 const char *description, 389 const char *description,
390 char **str) 390 char **str)
391{ 391{
392 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 392 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
393 .name = name, 393 .name = name,
@@ -395,7 +395,7 @@ GNUNET_GETOPT_option_string(char shortName,
395 .description = description, 395 .description = description,
396 .require_argument = 1, 396 .require_argument = 1,
397 .processor = &set_string, 397 .processor = &set_string,
398 .scls = (void *)str }; 398 .scls = (void *) str };
399 399
400 return clo; 400 return clo;
401} 401}
@@ -408,16 +408,16 @@ GNUNET_GETOPT_option_string(char shortName,
408 * @param[out] level set to the log level 408 * @param[out] level set to the log level
409 */ 409 */
410struct GNUNET_GETOPT_CommandLineOption 410struct GNUNET_GETOPT_CommandLineOption
411GNUNET_GETOPT_option_loglevel(char **level) 411GNUNET_GETOPT_option_loglevel (char **level)
412{ 412{
413 struct GNUNET_GETOPT_CommandLineOption clo = 413 struct GNUNET_GETOPT_CommandLineOption clo =
414 { .shortName = 'L', 414 { .shortName = 'L',
415 .name = "log", 415 .name = "log",
416 .argumentHelp = "LOGLEVEL", 416 .argumentHelp = "LOGLEVEL",
417 .description = gettext_noop("configure logging to use LOGLEVEL"), 417 .description = gettext_noop ("configure logging to use LOGLEVEL"),
418 .require_argument = 1, 418 .require_argument = 1,
419 .processor = &set_string, 419 .processor = &set_string,
420 .scls = (void *)level }; 420 .scls = (void *) level };
421 421
422 return clo; 422 return clo;
423} 423}
@@ -435,18 +435,18 @@ GNUNET_GETOPT_option_loglevel(char **level)
435 * @return #GNUNET_OK 435 * @return #GNUNET_OK
436 */ 436 */
437static int 437static int
438set_filename(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 438set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
439 void *scls, 439 void *scls,
440 const char *option, 440 const char *option,
441 const char *value) 441 const char *value)
442{ 442{
443 char **val = scls; 443 char **val = scls;
444 444
445 (void)ctx; 445 (void) ctx;
446 (void)option; 446 (void) option;
447 GNUNET_assert(NULL != value); 447 GNUNET_assert (NULL != value);
448 GNUNET_free_non_null(*val); 448 GNUNET_free_non_null (*val);
449 *val = GNUNET_STRINGS_filename_expand(value); 449 *val = GNUNET_STRINGS_filename_expand (value);
450 return GNUNET_OK; 450 return GNUNET_OK;
451} 451}
452 452
@@ -461,11 +461,11 @@ set_filename(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
461 * @param[out] str set to the string 461 * @param[out] str set to the string
462 */ 462 */
463struct GNUNET_GETOPT_CommandLineOption 463struct GNUNET_GETOPT_CommandLineOption
464GNUNET_GETOPT_option_filename(char shortName, 464GNUNET_GETOPT_option_filename (char shortName,
465 const char *name, 465 const char *name,
466 const char *argumentHelp, 466 const char *argumentHelp,
467 const char *description, 467 const char *description,
468 char **str) 468 char **str)
469{ 469{
470 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 470 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
471 .name = name, 471 .name = name,
@@ -473,7 +473,7 @@ GNUNET_GETOPT_option_filename(char shortName,
473 .description = description, 473 .description = description,
474 .require_argument = 1, 474 .require_argument = 1,
475 .processor = &set_filename, 475 .processor = &set_filename,
476 .scls = (void *)str }; 476 .scls = (void *) str };
477 477
478 return clo; 478 return clo;
479} 479}
@@ -485,17 +485,17 @@ GNUNET_GETOPT_option_filename(char shortName,
485 * @param[out] logfn set to the name of the logfile 485 * @param[out] logfn set to the name of the logfile
486 */ 486 */
487struct GNUNET_GETOPT_CommandLineOption 487struct GNUNET_GETOPT_CommandLineOption
488GNUNET_GETOPT_option_logfile(char **logfn) 488GNUNET_GETOPT_option_logfile (char **logfn)
489{ 489{
490 struct GNUNET_GETOPT_CommandLineOption clo = 490 struct GNUNET_GETOPT_CommandLineOption clo =
491 { .shortName = 'l', 491 { .shortName = 'l',
492 .name = "logfile", 492 .name = "logfile",
493 .argumentHelp = "FILENAME", 493 .argumentHelp = "FILENAME",
494 .description = 494 .description =
495 gettext_noop("configure logging to write logs to FILENAME"), 495 gettext_noop ("configure logging to write logs to FILENAME"),
496 .require_argument = 1, 496 .require_argument = 1,
497 .processor = &set_filename, 497 .processor = &set_filename,
498 .scls = (void *)logfn }; 498 .scls = (void *) logfn };
499 499
500 return clo; 500 return clo;
501} 501}
@@ -507,16 +507,16 @@ GNUNET_GETOPT_option_logfile(char **logfn)
507 * @param[out] fn set to the name of the configuration file 507 * @param[out] fn set to the name of the configuration file
508 */ 508 */
509struct GNUNET_GETOPT_CommandLineOption 509struct GNUNET_GETOPT_CommandLineOption
510GNUNET_GETOPT_option_cfgfile(char **fn) 510GNUNET_GETOPT_option_cfgfile (char **fn)
511{ 511{
512 struct GNUNET_GETOPT_CommandLineOption clo = 512 struct GNUNET_GETOPT_CommandLineOption clo =
513 { .shortName = 'c', 513 { .shortName = 'c',
514 .name = "config", 514 .name = "config",
515 .argumentHelp = "FILENAME", 515 .argumentHelp = "FILENAME",
516 .description = gettext_noop("use configuration file FILENAME"), 516 .description = gettext_noop ("use configuration file FILENAME"),
517 .require_argument = 1, 517 .require_argument = 1,
518 .processor = &set_filename, 518 .processor = &set_filename,
519 .scls = (void *)fn }; 519 .scls = (void *) fn };
520 520
521 return clo; 521 return clo;
522} 522}
@@ -536,22 +536,22 @@ GNUNET_GETOPT_option_cfgfile(char **fn)
536 * @return #GNUNET_OK if parsing the value worked 536 * @return #GNUNET_OK if parsing the value worked
537 */ 537 */
538static int 538static int
539set_ulong(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 539set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
540 void *scls, 540 void *scls,
541 const char *option, 541 const char *option,
542 const char *value) 542 const char *value)
543{ 543{
544 unsigned long long *val = scls; 544 unsigned long long *val = scls;
545 char dummy[2]; 545 char dummy[2];
546 546
547 (void)ctx; 547 (void) ctx;
548 if (1 != sscanf(value, "%llu%1s", val, dummy)) 548 if (1 != sscanf (value, "%llu%1s", val, dummy))
549 { 549 {
550 fprintf(stderr, 550 fprintf (stderr,
551 _("You must pass a number to the `%s' option.\n"), 551 _ ("You must pass a number to the `%s' option.\n"),
552 option); 552 option);
553 return GNUNET_SYSERR; 553 return GNUNET_SYSERR;
554 } 554 }
555 return GNUNET_OK; 555 return GNUNET_OK;
556} 556}
557 557
@@ -566,11 +566,11 @@ set_ulong(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
566 * @param[out] val set to the value specified at the command line 566 * @param[out] val set to the value specified at the command line
567 */ 567 */
568struct GNUNET_GETOPT_CommandLineOption 568struct GNUNET_GETOPT_CommandLineOption
569GNUNET_GETOPT_option_ulong(char shortName, 569GNUNET_GETOPT_option_ulong (char shortName,
570 const char *name, 570 const char *name,
571 const char *argumentHelp, 571 const char *argumentHelp,
572 const char *description, 572 const char *description,
573 unsigned long long *val) 573 unsigned long long *val)
574{ 574{
575 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 575 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
576 .name = name, 576 .name = name,
@@ -578,7 +578,7 @@ GNUNET_GETOPT_option_ulong(char shortName,
578 .description = description, 578 .description = description,
579 .require_argument = 1, 579 .require_argument = 1,
580 .processor = &set_ulong, 580 .processor = &set_ulong,
581 .scls = (void *)val }; 581 .scls = (void *) val };
582 582
583 return clo; 583 return clo;
584} 584}
@@ -598,21 +598,21 @@ GNUNET_GETOPT_option_ulong(char shortName,
598 * @return #GNUNET_OK if parsing the value worked 598 * @return #GNUNET_OK if parsing the value worked
599 */ 599 */
600static int 600static int
601set_relative_time(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 601set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
602 void *scls, 602 void *scls,
603 const char *option, 603 const char *option,
604 const char *value) 604 const char *value)
605{ 605{
606 struct GNUNET_TIME_Relative *val = scls; 606 struct GNUNET_TIME_Relative *val = scls;
607 607
608 (void)ctx; 608 (void) ctx;
609 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative(value, val)) 609 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val))
610 { 610 {
611 fprintf(stderr, 611 fprintf (stderr,
612 _("You must pass relative time to the `%s' option.\n"), 612 _ ("You must pass relative time to the `%s' option.\n"),
613 option); 613 option);
614 return GNUNET_SYSERR; 614 return GNUNET_SYSERR;
615 } 615 }
616 return GNUNET_OK; 616 return GNUNET_OK;
617} 617}
618 618
@@ -628,19 +628,20 @@ set_relative_time(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
628 * @param[out] val set to the time specified at the command line 628 * @param[out] val set to the time specified at the command line
629 */ 629 */
630struct GNUNET_GETOPT_CommandLineOption 630struct GNUNET_GETOPT_CommandLineOption
631GNUNET_GETOPT_option_relative_time(char shortName, 631GNUNET_GETOPT_option_relative_time (char shortName,
632 const char *name, 632 const char *name,
633 const char *argumentHelp, 633 const char *argumentHelp,
634 const char *description, 634 const char *description,
635 struct GNUNET_TIME_Relative *val) 635 struct GNUNET_TIME_Relative *val)
636{ 636{
637 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 637 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
638 .name = name, 638 .name = name,
639 .argumentHelp = argumentHelp, 639 .argumentHelp = argumentHelp,
640 .description = description, 640 .description = description,
641 .require_argument = 1, 641 .require_argument = 1,
642 .processor = &set_relative_time, 642 .processor =
643 .scls = (void *)val }; 643 &set_relative_time,
644 .scls = (void *) val };
644 645
645 return clo; 646 return clo;
646} 647}
@@ -660,21 +661,21 @@ GNUNET_GETOPT_option_relative_time(char shortName,
660 * @return #GNUNET_OK if parsing the value worked 661 * @return #GNUNET_OK if parsing the value worked
661 */ 662 */
662static int 663static int
663set_absolute_time(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 664set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
664 void *scls, 665 void *scls,
665 const char *option, 666 const char *option,
666 const char *value) 667 const char *value)
667{ 668{
668 struct GNUNET_TIME_Absolute *val = scls; 669 struct GNUNET_TIME_Absolute *val = scls;
669 670
670 (void)ctx; 671 (void) ctx;
671 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute(value, val)) 672 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val))
672 { 673 {
673 fprintf(stderr, 674 fprintf (stderr,
674 _("You must pass absolute time to the `%s' option.\n"), 675 _ ("You must pass absolute time to the `%s' option.\n"),
675 option); 676 option);
676 return GNUNET_SYSERR; 677 return GNUNET_SYSERR;
677 } 678 }
678 return GNUNET_OK; 679 return GNUNET_OK;
679} 680}
680 681
@@ -690,19 +691,20 @@ set_absolute_time(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
690 * @param[out] val set to the time specified at the command line 691 * @param[out] val set to the time specified at the command line
691 */ 692 */
692struct GNUNET_GETOPT_CommandLineOption 693struct GNUNET_GETOPT_CommandLineOption
693GNUNET_GETOPT_option_absolute_time(char shortName, 694GNUNET_GETOPT_option_absolute_time (char shortName,
694 const char *name, 695 const char *name,
695 const char *argumentHelp, 696 const char *argumentHelp,
696 const char *description, 697 const char *description,
697 struct GNUNET_TIME_Absolute *val) 698 struct GNUNET_TIME_Absolute *val)
698{ 699{
699 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 700 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
700 .name = name, 701 .name = name,
701 .argumentHelp = argumentHelp, 702 .argumentHelp = argumentHelp,
702 .description = description, 703 .description = description,
703 .require_argument = 1, 704 .require_argument = 1,
704 .processor = &set_absolute_time, 705 .processor =
705 .scls = (void *)val }; 706 &set_absolute_time,
707 .scls = (void *) val };
706 708
707 return clo; 709 return clo;
708} 710}
@@ -722,30 +724,30 @@ GNUNET_GETOPT_option_absolute_time(char shortName,
722 * @return #GNUNET_OK if parsing the value worked 724 * @return #GNUNET_OK if parsing the value worked
723 */ 725 */
724static int 726static int
725set_uint(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 727set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
726 void *scls, 728 void *scls,
727 const char *option, 729 const char *option,
728 const char *value) 730 const char *value)
729{ 731{
730 unsigned int *val = scls; 732 unsigned int *val = scls;
731 char dummy[2]; 733 char dummy[2];
732 734
733 (void)ctx; 735 (void) ctx;
734 if ('-' == *value) 736 if ('-' == *value)
735 { 737 {
736 fprintf(stderr, 738 fprintf (stderr,
737 _( 739 _ (
738 "Your input for the '%s' option has to be a non negative number \n"), 740 "Your input for the '%s' option has to be a non negative number \n"),
739 option); 741 option);
740 return GNUNET_SYSERR; 742 return GNUNET_SYSERR;
741 } 743 }
742 if (1 != sscanf(value, "%u%1s", val, dummy)) 744 if (1 != sscanf (value, "%u%1s", val, dummy))
743 { 745 {
744 fprintf(stderr, 746 fprintf (stderr,
745 _("You must pass a number to the `%s' option.\n"), 747 _ ("You must pass a number to the `%s' option.\n"),
746 option); 748 option);
747 return GNUNET_SYSERR; 749 return GNUNET_SYSERR;
748 } 750 }
749 return GNUNET_OK; 751 return GNUNET_OK;
750} 752}
751 753
@@ -760,11 +762,11 @@ set_uint(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
760 * @param[out] val set to the value specified at the command line 762 * @param[out] val set to the value specified at the command line
761 */ 763 */
762struct GNUNET_GETOPT_CommandLineOption 764struct GNUNET_GETOPT_CommandLineOption
763GNUNET_GETOPT_option_uint(char shortName, 765GNUNET_GETOPT_option_uint (char shortName,
764 const char *name, 766 const char *name,
765 const char *argumentHelp, 767 const char *argumentHelp,
766 const char *description, 768 const char *description,
767 unsigned int *val) 769 unsigned int *val)
768{ 770{
769 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 771 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
770 .name = name, 772 .name = name,
@@ -772,7 +774,7 @@ GNUNET_GETOPT_option_uint(char shortName,
772 .description = description, 774 .description = description,
773 .require_argument = 1, 775 .require_argument = 1,
774 .processor = &set_uint, 776 .processor = &set_uint,
775 .scls = (void *)val }; 777 .scls = (void *) val };
776 778
777 return clo; 779 return clo;
778} 780}
@@ -792,32 +794,32 @@ GNUNET_GETOPT_option_uint(char shortName,
792 * @return #GNUNET_OK if parsing the value worked 794 * @return #GNUNET_OK if parsing the value worked
793 */ 795 */
794static int 796static int
795set_uint16(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 797set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
796 void *scls, 798 void *scls,
797 const char *option, 799 const char *option,
798 const char *value) 800 const char *value)
799{ 801{
800 uint16_t *val = scls; 802 uint16_t *val = scls;
801 unsigned int v; 803 unsigned int v;
802 char dummy[2]; 804 char dummy[2];
803 805
804 (void)ctx; 806 (void) ctx;
805 if (1 != sscanf(value, "%u%1s", &v, dummy)) 807 if (1 != sscanf (value, "%u%1s", &v, dummy))
806 { 808 {
807 fprintf(stderr, 809 fprintf (stderr,
808 _("You must pass a number to the `%s' option.\n"), 810 _ ("You must pass a number to the `%s' option.\n"),
809 option); 811 option);
810 return GNUNET_SYSERR; 812 return GNUNET_SYSERR;
811 } 813 }
812 if (v > UINT16_MAX) 814 if (v > UINT16_MAX)
813 { 815 {
814 fprintf(stderr, 816 fprintf (stderr,
815 _("You must pass a number below %u to the `%s' option.\n"), 817 _ ("You must pass a number below %u to the `%s' option.\n"),
816 (unsigned int)UINT16_MAX, 818 (unsigned int) UINT16_MAX,
817 option); 819 option);
818 return GNUNET_SYSERR; 820 return GNUNET_SYSERR;
819 } 821 }
820 *val = (uint16_t)v; 822 *val = (uint16_t) v;
821 return GNUNET_OK; 823 return GNUNET_OK;
822} 824}
823 825
@@ -832,11 +834,11 @@ set_uint16(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
832 * @param[out] val set to the value specified at the command line 834 * @param[out] val set to the value specified at the command line
833 */ 835 */
834struct GNUNET_GETOPT_CommandLineOption 836struct GNUNET_GETOPT_CommandLineOption
835GNUNET_GETOPT_option_uint16(char shortName, 837GNUNET_GETOPT_option_uint16 (char shortName,
836 const char *name, 838 const char *name,
837 const char *argumentHelp, 839 const char *argumentHelp,
838 const char *description, 840 const char *description,
839 uint16_t *val) 841 uint16_t *val)
840{ 842{
841 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 843 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
842 .name = name, 844 .name = name,
@@ -844,7 +846,7 @@ GNUNET_GETOPT_option_uint16(char shortName,
844 .description = description, 846 .description = description,
845 .require_argument = 1, 847 .require_argument = 1,
846 .processor = &set_uint16, 848 .processor = &set_uint16,
847 .scls = (void *)val }; 849 .scls = (void *) val };
848 850
849 return clo; 851 return clo;
850} 852}
@@ -853,7 +855,8 @@ GNUNET_GETOPT_option_uint16(char shortName,
853/** 855/**
854 * Closure for #set_base32(). 856 * Closure for #set_base32().
855 */ 857 */
856struct Base32Context { 858struct Base32Context
859{
857 /** 860 /**
858 * Value to initialize (already allocated) 861 * Value to initialize (already allocated)
859 */ 862 */
@@ -880,26 +883,26 @@ struct Base32Context {
880 * @return #GNUNET_OK if parsing the value worked 883 * @return #GNUNET_OK if parsing the value worked
881 */ 884 */
882static int 885static int
883set_base32(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 886set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
884 void *scls, 887 void *scls,
885 const char *option, 888 const char *option,
886 const char *value) 889 const char *value)
887{ 890{
888 struct Base32Context *bc = scls; 891 struct Base32Context *bc = scls;
889 892
890 (void)ctx; 893 (void) ctx;
891 if (GNUNET_OK != GNUNET_STRINGS_string_to_data(value, 894 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (value,
892 strlen(value), 895 strlen (value),
893 bc->val, 896 bc->val,
894 bc->val_size)) 897 bc->val_size))
895 { 898 {
896 fprintf( 899 fprintf (
897 stderr, 900 stderr,
898 _( 901 _ (
899 "Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), 902 "Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"),
900 option); 903 option);
901 return GNUNET_SYSERR; 904 return GNUNET_SYSERR;
902 } 905 }
903 return GNUNET_OK; 906 return GNUNET_OK;
904} 907}
905 908
@@ -911,9 +914,9 @@ set_base32(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
911 * @param cls value to GNUNET_free() 914 * @param cls value to GNUNET_free()
912 */ 915 */
913static void 916static void
914free_bc(void *cls) 917free_bc (void *cls)
915{ 918{
916 GNUNET_free(cls); 919 GNUNET_free (cls);
917} 920}
918 921
919 922
@@ -929,14 +932,14 @@ free_bc(void *cls)
929 * @param val_size size of @a val in bytes 932 * @param val_size size of @a val in bytes
930 */ 933 */
931struct GNUNET_GETOPT_CommandLineOption 934struct GNUNET_GETOPT_CommandLineOption
932GNUNET_GETOPT_option_base32_fixed_size(char shortName, 935GNUNET_GETOPT_option_base32_fixed_size (char shortName,
933 const char *name, 936 const char *name,
934 const char *argumentHelp, 937 const char *argumentHelp,
935 const char *description, 938 const char *description,
936 void *val, 939 void *val,
937 size_t val_size) 940 size_t val_size)
938{ 941{
939 struct Base32Context *bc = GNUNET_new(struct Base32Context); 942 struct Base32Context *bc = GNUNET_new (struct Base32Context);
940 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 943 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
941 .name = name, 944 .name = name,
942 .argumentHelp = argumentHelp, 945 .argumentHelp = argumentHelp,
@@ -944,7 +947,7 @@ GNUNET_GETOPT_option_base32_fixed_size(char shortName,
944 .require_argument = 1, 947 .require_argument = 1,
945 .processor = &set_base32, 948 .processor = &set_base32,
946 .cleaner = &free_bc, 949 .cleaner = &free_bc,
947 .scls = (void *)bc }; 950 .scls = (void *) bc };
948 951
949 bc->val = val; 952 bc->val = val;
950 bc->val_size = val_size; 953 bc->val_size = val_size;
@@ -959,7 +962,7 @@ GNUNET_GETOPT_option_base32_fixed_size(char shortName,
959 * @return @a opt with the mandatory flag set. 962 * @return @a opt with the mandatory flag set.
960 */ 963 */
961struct GNUNET_GETOPT_CommandLineOption 964struct GNUNET_GETOPT_CommandLineOption
962GNUNET_GETOPT_option_mandatory(struct GNUNET_GETOPT_CommandLineOption opt) 965GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
963{ 966{
964 opt.option_mandatory = 1; 967 opt.option_mandatory = 1;
965 return opt; 968 return opt;
@@ -973,7 +976,7 @@ GNUNET_GETOPT_option_mandatory(struct GNUNET_GETOPT_CommandLineOption opt)
973 * @return @a opt with the exclusive flag set. 976 * @return @a opt with the exclusive flag set.
974 */ 977 */
975struct GNUNET_GETOPT_CommandLineOption 978struct GNUNET_GETOPT_CommandLineOption
976GNUNET_GETOPT_option_exclusive(struct GNUNET_GETOPT_CommandLineOption opt) 979GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt)
977{ 980{
978 opt.option_exclusive = 1; 981 opt.option_exclusive = 1;
979 return opt; 982 return opt;
diff --git a/src/util/gnunet-config-diff.c b/src/util/gnunet-config-diff.c
index 214d6d73f..207b9518a 100644
--- a/src/util/gnunet-config-diff.c
+++ b/src/util/gnunet-config-diff.c
@@ -2,22 +2,22 @@
2#include <gnunet_util_lib.h> 2#include <gnunet_util_lib.h>
3 3
4int 4int
5main(int argc, char **argv) 5main (int argc, char **argv)
6{ 6{
7 struct GNUNET_CONFIGURATION_Handle *i1; 7 struct GNUNET_CONFIGURATION_Handle *i1;
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;
14 }
15 i1 = GNUNET_CONFIGURATION_create();
16 i2 = GNUNET_CONFIGURATION_create();
17 if ((GNUNET_OK != GNUNET_CONFIGURATION_load(i1, argv[1])) ||
18 (GNUNET_OK != GNUNET_CONFIGURATION_load(i2, argv[2])))
19 return 1; 13 return 1;
20 if (GNUNET_OK != GNUNET_CONFIGURATION_write_diffs(i1, i2, argv[2])) 14 }
15 i1 = GNUNET_CONFIGURATION_create ();
16 i2 = GNUNET_CONFIGURATION_create ();
17 if ((GNUNET_OK != GNUNET_CONFIGURATION_load (i1, argv[1])) ||
18 (GNUNET_OK != GNUNET_CONFIGURATION_load (i2, argv[2])))
19 return 1;
20 if (GNUNET_OK != GNUNET_CONFIGURATION_write_diffs (i1, i2, argv[2]))
21 return 2; 21 return 2;
22 return 0; 22 return 0;
23} 23}
diff --git a/src/util/gnunet-config.c b/src/util/gnunet-config.c
index c67237a27..c225f8f1f 100644
--- a/src/util/gnunet-config.c
+++ b/src/util/gnunet-config.c
@@ -80,36 +80,36 @@ static int rewrite;
80 * @param value value of the option 80 * @param value value of the option
81 */ 81 */
82static void 82static void
83print_option(void *cls, 83print_option (void *cls,
84 const char *section, 84 const char *section,
85 const char *option, 85 const char *option,
86 const char *value) 86 const char *value)
87{ 87{
88 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 88 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
89 89
90 (void)section; 90 (void) section;
91 if (is_filename) 91 if (is_filename)
92 { 92 {
93 char *value_fn; 93 char *value_fn;
94 char *fn; 94 char *fn;
95 95
96 GNUNET_assert(GNUNET_OK == 96 GNUNET_assert (GNUNET_OK ==
97 GNUNET_CONFIGURATION_get_value_filename(cfg, 97 GNUNET_CONFIGURATION_get_value_filename (cfg,
98 section, 98 section,
99 option, 99 option,
100 &value_fn)); 100 &value_fn));
101 fn = GNUNET_STRINGS_filename_expand(value_fn); 101 fn = GNUNET_STRINGS_filename_expand (value_fn);
102 if (NULL == fn) 102 if (NULL == fn)
103 fn = value_fn; 103 fn = value_fn;
104 else 104 else
105 GNUNET_free(value_fn); 105 GNUNET_free (value_fn);
106 fprintf(stdout, "%s = %s\n", option, fn); 106 fprintf (stdout, "%s = %s\n", option, fn);
107 GNUNET_free(fn); 107 GNUNET_free (fn);
108 } 108 }
109 else 109 else
110 { 110 {
111 fprintf(stdout, "%s = %s\n", option, value); 111 fprintf (stdout, "%s = %s\n", option, value);
112 } 112 }
113} 113}
114 114
115 115
@@ -120,10 +120,10 @@ print_option(void *cls,
120 * @param section a section in the configuration file 120 * @param section a section in the configuration file
121 */ 121 */
122static void 122static void
123print_section_name(void *cls, const char *section) 123print_section_name (void *cls, const char *section)
124{ 124{
125 (void)cls; 125 (void) cls;
126 fprintf(stdout, "%s\n", section); 126 fprintf (stdout, "%s\n", section);
127} 127}
128 128
129 129
@@ -137,133 +137,133 @@ print_section_name(void *cls, const char *section)
137 * @param cfg configuration 137 * @param cfg configuration
138 */ 138 */
139static void 139static void
140run(void *cls, 140run (void *cls,
141 char *const *args, 141 char *const *args,
142 const char *cfgfile, 142 const char *cfgfile,
143 const struct GNUNET_CONFIGURATION_Handle *cfg) 143 const struct GNUNET_CONFIGURATION_Handle *cfg)
144{ 144{
145 struct GNUNET_CONFIGURATION_Handle *out = NULL; 145 struct GNUNET_CONFIGURATION_Handle *out = NULL;
146 struct GNUNET_CONFIGURATION_Handle *diff = NULL; 146 struct GNUNET_CONFIGURATION_Handle *diff = NULL;
147 char *cfg_fn; 147 char *cfg_fn;
148 148
149 (void)cls; 149 (void) cls;
150 (void)args; 150 (void) args;
151 if (NULL != backend_check) 151 if (NULL != backend_check)
152 { 152 {
153 char *name; 153 char *name;
154
155 GNUNET_asprintf (&name, "libgnunet_plugin_%s", backend_check);
156 global_ret = (GNUNET_OK == GNUNET_PLUGIN_test (name)) ? 0 : 77;
157 GNUNET_free (name);
158 return;
159 }
160 if (rewrite)
161 {
162 struct GNUNET_CONFIGURATION_Handle *def;
154 163
155 GNUNET_asprintf(&name, "libgnunet_plugin_%s", backend_check); 164 def = GNUNET_CONFIGURATION_create ();
156 global_ret = (GNUNET_OK == GNUNET_PLUGIN_test(name)) ? 0 : 77; 165 if (GNUNET_OK != GNUNET_CONFIGURATION_load (def, NULL))
157 GNUNET_free(name); 166 {
167 fprintf (stderr, _ ("failed to load configuration defaults"));
168 global_ret = 1;
158 return; 169 return;
159 } 170 }
160 if (rewrite) 171 diff = GNUNET_CONFIGURATION_get_diff (def, cfg);
172 cfg = diff;
173 }
174 if (((! rewrite) && (NULL == section)) || list_sections)
175 {
176 if (! list_sections)
161 { 177 {
162 struct GNUNET_CONFIGURATION_Handle *def; 178 fprintf (stderr,
163 179 _ ("%s or %s argument is required\n"),
164 def = GNUNET_CONFIGURATION_create(); 180 "--section",
165 if (GNUNET_OK != GNUNET_CONFIGURATION_load(def, NULL)) 181 "--list-sections");
166 { 182 global_ret = 1;
167 fprintf(stderr, _("failed to load configuration defaults"));
168 global_ret = 1;
169 return;
170 }
171 diff = GNUNET_CONFIGURATION_get_diff(def, cfg);
172 cfg = diff;
173 } 183 }
174 if (((!rewrite) && (NULL == section)) || list_sections) 184 else
175 { 185 {
176 if (!list_sections) 186 fprintf (stderr, _ ("The following sections are available:\n"));
177 { 187 GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL);
178 fprintf(stderr,
179 _("%s or %s argument is required\n"),
180 "--section",
181 "--list-sections");
182 global_ret = 1;
183 }
184 else
185 {
186 fprintf(stderr, _("The following sections are available:\n"));
187 GNUNET_CONFIGURATION_iterate_sections(cfg, &print_section_name, NULL);
188 }
189 goto cleanup;
190 } 188 }
189 goto cleanup;
190 }
191 191
192 if ((NULL != section) && (NULL == value)) 192 if ((NULL != section) && (NULL == value))
193 {
194 if (NULL == option)
195 {
196 GNUNET_CONFIGURATION_iterate_section_values (cfg,
197 section,
198 &print_option,
199 (void *) cfg);
200 }
201 else
193 { 202 {
194 if (NULL == option) 203 if (is_filename)
204 {
205 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
206 section,
207 option,
208 &value))
195 { 209 {
196 GNUNET_CONFIGURATION_iterate_section_values(cfg, 210 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option);
197 section, 211 global_ret = 3;
198 &print_option, 212 goto cleanup;
199 (void *)cfg);
200 } 213 }
214 }
201 else 215 else
216 {
217 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
218 section,
219 option,
220 &value))
202 { 221 {
203 if (is_filename) 222 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option);
204 { 223 global_ret = 3;
205 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, 224 goto cleanup;
206 section,
207 option,
208 &value))
209 {
210 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, option);
211 global_ret = 3;
212 goto cleanup;
213 }
214 }
215 else
216 {
217 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg,
218 section,
219 option,
220 &value))
221 {
222 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, option);
223 global_ret = 3;
224 goto cleanup;
225 }
226 }
227 fprintf(stdout, "%s\n", value);
228 } 225 }
226 }
227 fprintf (stdout, "%s\n", value);
229 } 228 }
229 }
230 else if (NULL != section) 230 else if (NULL != section)
231 {
232 if (NULL == option)
231 { 233 {
232 if (NULL == option) 234 fprintf (stderr, _ ("--option argument required to set value\n"));
233 { 235 global_ret = 1;
234 fprintf(stderr, _("--option argument required to set value\n")); 236 goto cleanup;
235 global_ret = 1;
236 goto cleanup;
237 }
238 out = GNUNET_CONFIGURATION_dup(cfg);
239 GNUNET_CONFIGURATION_set_value_string(out, section, option, value);
240 } 237 }
238 out = GNUNET_CONFIGURATION_dup (cfg);
239 GNUNET_CONFIGURATION_set_value_string (out, section, option, value);
240 }
241 cfg_fn = NULL; 241 cfg_fn = NULL;
242 if (NULL == cfgfile) 242 if (NULL == cfgfile)
243 { 243 {
244 const char *xdg = getenv("XDG_CONFIG_HOME"); 244 const char *xdg = getenv ("XDG_CONFIG_HOME");
245 if (NULL != xdg) 245 if (NULL != xdg)
246 GNUNET_asprintf(&cfg_fn, 246 GNUNET_asprintf (&cfg_fn,
247 "%s%s%s", 247 "%s%s%s",
248 xdg, 248 xdg,
249 DIR_SEPARATOR_STR, 249 DIR_SEPARATOR_STR,
250 GNUNET_OS_project_data_get()->config_file); 250 GNUNET_OS_project_data_get ()->config_file);
251 else 251 else
252 cfg_fn = GNUNET_strdup(GNUNET_OS_project_data_get()->user_config_file); 252 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
253 cfgfile = cfg_fn; 253 cfgfile = cfg_fn;
254 } 254 }
255 if ((NULL != diff) || (NULL != out)) 255 if ((NULL != diff) || (NULL != out))
256 { 256 {
257 if (GNUNET_OK != 257 if (GNUNET_OK !=
258 GNUNET_CONFIGURATION_write((NULL == out) ? diff : out, cfgfile)) 258 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, cfgfile))
259 global_ret = 2; 259 global_ret = 2;
260 } 260 }
261 GNUNET_free_non_null(cfg_fn); 261 GNUNET_free_non_null (cfg_fn);
262 if (NULL != out) 262 if (NULL != out)
263 GNUNET_CONFIGURATION_destroy(out); 263 GNUNET_CONFIGURATION_destroy (out);
264cleanup: 264cleanup:
265 if (NULL != diff) 265 if (NULL != diff)
266 GNUNET_CONFIGURATION_destroy(diff); 266 GNUNET_CONFIGURATION_destroy (diff);
267} 267}
268 268
269 269
@@ -275,63 +275,63 @@ cleanup:
275 * @return 0 ok, 1 on error 275 * @return 0 ok, 1 on error
276 */ 276 */
277int 277int
278main(int argc, char *const *argv) 278main (int argc, char *const *argv)
279{ 279{
280 struct GNUNET_GETOPT_CommandLineOption options[] = 280 struct GNUNET_GETOPT_CommandLineOption options[] =
281 { GNUNET_GETOPT_option_flag( 281 { GNUNET_GETOPT_option_flag (
282 'f', 282 'f',
283 "filename", 283 "filename",
284 gettext_noop("interpret option value as a filename (with $-expansion)"), 284 gettext_noop ("interpret option value as a filename (with $-expansion)"),
285 &is_filename), 285 &is_filename),
286 GNUNET_GETOPT_option_exclusive(GNUNET_GETOPT_option_string( 286 GNUNET_GETOPT_option_exclusive (GNUNET_GETOPT_option_string (
287 'b', 287 'b',
288 "supported-backend", 288 "supported-backend",
289 "BACKEND", 289 "BACKEND",
290 gettext_noop( 290 gettext_noop (
291 "test if the current installation supports the specified BACKEND"), 291 "test if the current installation supports the specified BACKEND"),
292 &backend_check)), 292 &backend_check)),
293 GNUNET_GETOPT_option_string('s', 293 GNUNET_GETOPT_option_string ('s',
294 "section", 294 "section",
295 "SECTION", 295 "SECTION",
296 gettext_noop( 296 gettext_noop (
297 "name of the section to access"), 297 "name of the section to access"),
298 &section), 298 &section),
299 GNUNET_GETOPT_option_string('o', 299 GNUNET_GETOPT_option_string ('o',
300 "option", 300 "option",
301 "OPTION", 301 "OPTION",
302 gettext_noop("name of the option to access"), 302 gettext_noop ("name of the option to access"),
303 &option), 303 &option),
304 GNUNET_GETOPT_option_string('V', 304 GNUNET_GETOPT_option_string ('V',
305 "value", 305 "value",
306 "VALUE", 306 "VALUE",
307 gettext_noop("value to set"), 307 gettext_noop ("value to set"),
308 &value), 308 &value),
309 GNUNET_GETOPT_option_flag('S', 309 GNUNET_GETOPT_option_flag ('S',
310 "list-sections", 310 "list-sections",
311 gettext_noop( 311 gettext_noop (
312 "print available configuration sections"), 312 "print available configuration sections"),
313 &list_sections), 313 &list_sections),
314 GNUNET_GETOPT_option_flag( 314 GNUNET_GETOPT_option_flag (
315 'w', 315 'w',
316 "rewrite", 316 "rewrite",
317 gettext_noop( 317 gettext_noop (
318 "write configuration file that only contains delta to defaults"), 318 "write configuration file that only contains delta to defaults"),
319 &rewrite), 319 &rewrite),
320 GNUNET_GETOPT_OPTION_END }; 320 GNUNET_GETOPT_OPTION_END };
321 int ret; 321 int ret;
322 322
323 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 323 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
324 return 2; 324 return 2;
325 325
326 ret = 326 ret =
327 GNUNET_PROGRAM_run(argc, 327 GNUNET_PROGRAM_run (argc,
328 argv, 328 argv,
329 "gnunet-config [OPTIONS]", 329 "gnunet-config [OPTIONS]",
330 gettext_noop("Manipulate GNUnet configuration files"), 330 gettext_noop ("Manipulate GNUnet configuration files"),
331 options, 331 options,
332 &run, 332 &run,
333 NULL); 333 NULL);
334 GNUNET_free((void *)argv); 334 GNUNET_free ((void *) argv);
335 if (GNUNET_OK == ret) 335 if (GNUNET_OK == ret)
336 return global_ret; 336 return global_ret;
337 return ret; 337 return ret;
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c
index 7fb8150f6..72e50b2a8 100644
--- a/src/util/gnunet-ecc.c
+++ b/src/util/gnunet-ecc.c
@@ -76,7 +76,7 @@ static unsigned int make_keys;
76 * @param prefix Desired prefix for the public keys, NULL if any key is OK. 76 * @param prefix Desired prefix for the public keys, NULL if any key is OK.
77 */ 77 */
78static void 78static void
79create_keys(const char *fn, const char *prefix) 79create_keys (const char *fn, const char *prefix)
80{ 80{
81 FILE *f; 81 FILE *f;
82 struct GNUNET_CRYPTO_EddsaPrivateKey *pk; 82 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
@@ -89,120 +89,120 @@ create_keys(const char *fn, const char *prefix)
89 unsigned target_byte; 89 unsigned target_byte;
90 char *s; 90 char *s;
91 91
92 if (NULL == (f = fopen(fn, "w+"))) 92 if (NULL == (f = fopen (fn, "w+")))
93 { 93 {
94 fprintf(stderr, _("Failed to open `%s': %s\n"), fn, strerror(errno)); 94 fprintf (stderr, _ ("Failed to open `%s': %s\n"), fn, strerror (errno));
95 return; 95 return;
96 } 96 }
97 if (NULL != prefix) 97 if (NULL != prefix)
98 { 98 {
99 len = GNUNET_strlcpy(vanity, prefix, sizeof(vanity)); 99 len = GNUNET_strlcpy (vanity, prefix, sizeof(vanity));
100 n = len * 5 / 8; 100 n = len * 5 / 8;
101 rest = len * 5 % 8; 101 rest = len * 5 % 8;
102 102
103 memset(&vanity[len], '0', KEY_STR_LEN - len); 103 memset (&vanity[len], '0', KEY_STR_LEN - len);
104 vanity[KEY_STR_LEN] = '\0'; 104 vanity[KEY_STR_LEN] = '\0';
105 GNUNET_assert(GNUNET_OK == 105 GNUNET_assert (GNUNET_OK ==
106 GNUNET_CRYPTO_eddsa_public_key_from_string(vanity, 106 GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
107 KEY_STR_LEN, 107 KEY_STR_LEN,
108 &target_pub)); 108 &target_pub));
109 if (0 != rest) 109 if (0 != rest)
110 { 110 {
111 /** 111 /**
112 * Documentation by example: 112 * Documentation by example:
113 * vanity = "A" 113 * vanity = "A"
114 * len = 1 114 * len = 1
115 * n = 5/8 = 0 (bytes) 115 * n = 5/8 = 0 (bytes)
116 * rest = 5%8 = 5 (bits) 116 * rest = 5%8 = 5 (bits)
117 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000 117 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
118 */ 118 */
119 mask = ~((int)pow(2, 8 - rest) - 1); 119 mask = ~((int) pow (2, 8 - rest) - 1);
120 target_byte = ((unsigned char *)&target_pub)[n] & mask; 120 target_byte = ((unsigned char *) &target_pub)[n] & mask;
121 }
122 else
123 {
124 /* Just so old (debian) versions of GCC calm down with the warnings. */
125 mask = target_byte = 0;
126 }
127 s = GNUNET_CRYPTO_eddsa_public_key_to_string(&target_pub);
128 fprintf(stderr,
129 _("Generating %u keys like %s, please wait"),
130 make_keys,
131 s);
132 GNUNET_free(s);
133 fprintf(stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int)n, mask);
134 } 121 }
135 else 122 else
136 { 123 {
137 fprintf(stderr, _("Generating %u keys, please wait"), make_keys);
138 /* Just so old (debian) versions of GCC calm down with the warnings. */ 124 /* Just so old (debian) versions of GCC calm down with the warnings. */
139 n = rest = target_byte = mask = 0; 125 mask = target_byte = 0;
140 } 126 }
127 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
128 fprintf (stderr,
129 _ ("Generating %u keys like %s, please wait"),
130 make_keys,
131 s);
132 GNUNET_free (s);
133 fprintf (stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int) n, mask);
134 }
135 else
136 {
137 fprintf (stderr, _ ("Generating %u keys, please wait"), make_keys);
138 /* Just so old (debian) versions of GCC calm down with the warnings. */
139 n = rest = target_byte = mask = 0;
140 }
141 141
142 while (0 < make_keys--) 142 while (0 < make_keys--)
143 {
144 fprintf (stderr, ".");
145 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
143 { 146 {
144 fprintf(stderr, "."); 147 GNUNET_break (0);
145 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create())) 148 break;
146 { 149 }
147 GNUNET_break(0); 150 if (NULL != prefix)
148 break; 151 {
149 } 152 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
150 if (NULL != prefix) 153
151 { 154 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
152 struct GNUNET_CRYPTO_EddsaPublicKey newkey; 155 if (0 != memcmp (&target_pub, &newkey, n))
153 156 {
154 GNUNET_CRYPTO_eddsa_key_get_public(pk, &newkey); 157 make_keys++;
155 if (0 != memcmp(&target_pub, &newkey, n)) 158 continue;
156 { 159 }
157 make_keys++; 160 if (0 != rest)
158 continue; 161 {
159 } 162 unsigned char new_byte;
160 if (0 != rest) 163
161 { 164 new_byte = ((unsigned char *) &newkey)[n] & mask;
162 unsigned char new_byte; 165 if (target_byte != new_byte)
163
164 new_byte = ((unsigned char *)&newkey)[n] & mask;
165 if (target_byte != new_byte)
166 {
167 make_keys++;
168 continue;
169 }
170 }
171 }
172 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
173 fwrite(pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f))
174 { 166 {
175 fprintf(stderr, 167 make_keys++;
176 _("\nFailed to write to `%s': %s\n"), 168 continue;
177 fn,
178 strerror(errno));
179 GNUNET_free(pk);
180 break;
181 } 169 }
182 GNUNET_free(pk); 170 }
171 }
172 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
173 fwrite (pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f))
174 {
175 fprintf (stderr,
176 _ ("\nFailed to write to `%s': %s\n"),
177 fn,
178 strerror (errno));
179 GNUNET_free (pk);
180 break;
183 } 181 }
182 GNUNET_free (pk);
183 }
184 if (UINT_MAX == make_keys) 184 if (UINT_MAX == make_keys)
185 fprintf(stderr, _("\nFinished!\n")); 185 fprintf (stderr, _ ("\nFinished!\n"));
186 else 186 else
187 fprintf(stderr, _("\nError, %u keys not generated\n"), make_keys); 187 fprintf (stderr, _ ("\nError, %u keys not generated\n"), make_keys);
188 fclose(f); 188 fclose (f);
189} 189}
190 190
191 191
192static void 192static void
193print_hex(const char *msg, const void *buf, size_t size) 193print_hex (const char *msg, const void *buf, size_t size)
194{ 194{
195 printf("%s: ", msg); 195 printf ("%s: ", msg);
196 for (size_t i = 0; i < size; i++) 196 for (size_t i = 0; i < size; i++)
197 { 197 {
198 printf("%02hhx", ((const uint8_t *)buf)[i]); 198 printf ("%02hhx", ((const uint8_t *) buf)[i]);
199 } 199 }
200 printf("\n"); 200 printf ("\n");
201} 201}
202 202
203 203
204static void 204static void
205print_examples_ecdh() 205print_examples_ecdh ()
206{ 206{
207 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1; 207 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
208 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1; 208 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
@@ -211,40 +211,40 @@ print_examples_ecdh()
211 struct GNUNET_HashCode hash; 211 struct GNUNET_HashCode hash;
212 char buf[128]; 212 char buf[128];
213 213
214 dh_pub1 = GNUNET_new(struct GNUNET_CRYPTO_EcdhePublicKey); 214 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
215 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create(); 215 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
216 dh_pub2 = GNUNET_new(struct GNUNET_CRYPTO_EcdhePublicKey); 216 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
217 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create(); 217 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
218 GNUNET_CRYPTO_ecdhe_key_get_public(dh_priv1, dh_pub1); 218 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
219 GNUNET_CRYPTO_ecdhe_key_get_public(dh_priv2, dh_pub2); 219 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
220 220
221 GNUNET_assert(NULL != 221 GNUNET_assert (NULL !=
222 GNUNET_STRINGS_data_to_string(dh_priv1, 32, buf, 128)); 222 GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
223 printf("ECDHE key 1:\n"); 223 printf ("ECDHE key 1:\n");
224 printf("private: %s\n", buf); 224 printf ("private: %s\n", buf);
225 print_hex("private(hex)", dh_priv1, sizeof *dh_priv1); 225 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
226 GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(dh_pub1, 32, buf, 128)); 226 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
227 printf("public: %s\n", buf); 227 printf ("public: %s\n", buf);
228 print_hex("public(hex)", dh_pub1, sizeof *dh_pub1); 228 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
229 229
230 GNUNET_assert(NULL != 230 GNUNET_assert (NULL !=
231 GNUNET_STRINGS_data_to_string(dh_priv2, 32, buf, 128)); 231 GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
232 printf("ECDHE key 2:\n"); 232 printf ("ECDHE key 2:\n");
233 printf("private: %s\n", buf); 233 printf ("private: %s\n", buf);
234 print_hex("private(hex)", dh_priv2, sizeof *dh_priv2); 234 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
235 GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(dh_pub2, 32, buf, 128)); 235 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
236 printf("public: %s\n", buf); 236 printf ("public: %s\n", buf);
237 print_hex("public(hex)", dh_pub2, sizeof *dh_pub2); 237 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
238 238
239 GNUNET_assert(GNUNET_OK == 239 GNUNET_assert (GNUNET_OK ==
240 GNUNET_CRYPTO_ecc_ecdh(dh_priv1, dh_pub2, &hash)); 240 GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
241 GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(&hash, 64, buf, 128)); 241 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
242 printf("ECDH shared secret: %s\n", buf); 242 printf ("ECDH shared secret: %s\n", buf);
243 243
244 GNUNET_free(dh_priv1); 244 GNUNET_free (dh_priv1);
245 GNUNET_free(dh_priv2); 245 GNUNET_free (dh_priv2);
246 GNUNET_free(dh_pub1); 246 GNUNET_free (dh_pub1);
247 GNUNET_free(dh_pub2); 247 GNUNET_free (dh_pub2);
248} 248}
249 249
250 250
@@ -252,16 +252,16 @@ print_examples_ecdh()
252 * Print some random example operations to stdout. 252 * Print some random example operations to stdout.
253 */ 253 */
254static void 254static void
255print_examples() 255print_examples ()
256{ 256{
257 print_examples_ecdh(); 257 print_examples_ecdh ();
258 // print_examples_ecdsa (); 258 // print_examples_ecdsa ();
259 // print_examples_eddsa (); 259 // print_examples_eddsa ();
260} 260}
261 261
262 262
263static void 263static void
264print_key(const char *filename) 264print_key (const char *filename)
265{ 265{
266 struct GNUNET_DISK_FileHandle *fd; 266 struct GNUNET_DISK_FileHandle *fd;
267 struct GNUNET_CRYPTO_EddsaPrivateKey private_key; 267 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
@@ -273,64 +273,64 @@ print_key(const char *filename)
273 unsigned int c; 273 unsigned int c;
274 ssize_t sret; 274 ssize_t sret;
275 275
276 if (GNUNET_YES != GNUNET_DISK_file_test(filename)) 276 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
277 { 277 {
278 fprintf(stderr, _("Hostkeys file `%s' not found\n"), filename); 278 fprintf (stderr, _ ("Hostkeys file `%s' not found\n"), filename);
279 return; 279 return;
280 } 280 }
281 281
282 /* Check hostkey file size, read entire thing into memory */ 282 /* Check hostkey file size, read entire thing into memory */
283 if (GNUNET_OK != 283 if (GNUNET_OK !=
284 GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES)) 284 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
285 fs = 0; 285 fs = 0;
286 if (0 == fs) 286 if (0 == fs)
287 { 287 {
288 fprintf(stderr, _("Hostkeys file `%s' is empty\n"), filename); 288 fprintf (stderr, _ ("Hostkeys file `%s' is empty\n"), filename);
289 return; /* File is empty */ 289 return; /* File is empty */
290 } 290 }
291 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE)) 291 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
292 { 292 {
293 fprintf(stderr, _("Incorrect hostkey file format: %s\n"), filename); 293 fprintf (stderr, _ ("Incorrect hostkey file format: %s\n"), filename);
294 return; 294 return;
295 } 295 }
296 fd = GNUNET_DISK_file_open(filename, 296 fd = GNUNET_DISK_file_open (filename,
297 GNUNET_DISK_OPEN_READ, 297 GNUNET_DISK_OPEN_READ,
298 GNUNET_DISK_PERM_NONE); 298 GNUNET_DISK_PERM_NONE);
299 if (NULL == fd) 299 if (NULL == fd)
300 { 300 {
301 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", filename); 301 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
302 return; 302 return;
303 } 303 }
304 hostkeys_data = GNUNET_malloc(fs); 304 hostkeys_data = GNUNET_malloc (fs);
305 sret = GNUNET_DISK_file_read(fd, hostkeys_data, fs); 305 sret = GNUNET_DISK_file_read (fd, hostkeys_data, fs);
306 if ((sret < 0) || (fs != (size_t)sret)) 306 if ((sret < 0) || (fs != (size_t) sret))
307 { 307 {
308 fprintf(stderr, _("Could not read hostkey file: %s\n"), filename); 308 fprintf (stderr, _ ("Could not read hostkey file: %s\n"), filename);
309 GNUNET_free(hostkeys_data); 309 GNUNET_free (hostkeys_data);
310 GNUNET_DISK_file_close(fd); 310 GNUNET_DISK_file_close (fd);
311 return; 311 return;
312 } 312 }
313 GNUNET_DISK_file_close(fd); 313 GNUNET_DISK_file_close (fd);
314 314
315 if (NULL == hostkeys_data) 315 if (NULL == hostkeys_data)
316 return; 316 return;
317 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE; 317 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
318 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++) 318 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
319 {
320 GNUNET_memcpy (&private_key,
321 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
322 GNUNET_TESTING_HOSTKEYFILESIZE);
323 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
324 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
325 if (NULL != hostkey_str)
319 { 326 {
320 GNUNET_memcpy(&private_key, 327 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
321 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE), 328 GNUNET_free (hostkey_str);
322 GNUNET_TESTING_HOSTKEYFILESIZE);
323 GNUNET_CRYPTO_eddsa_key_get_public(&private_key, &public_key);
324 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string(&public_key);
325 if (NULL != hostkey_str)
326 {
327 fprintf(stderr, "%4u: %s\n", c, hostkey_str);
328 GNUNET_free(hostkey_str);
329 }
330 else
331 fprintf(stderr, "%4u: %s\n", c, "invalid");
332 } 329 }
333 GNUNET_free(hostkeys_data); 330 else
331 fprintf (stderr, "%4u: %s\n", c, "invalid");
332 }
333 GNUNET_free (hostkeys_data);
334} 334}
335 335
336 336
@@ -343,69 +343,69 @@ print_key(const char *filename)
343 * @param cfg configuration 343 * @param cfg configuration
344 */ 344 */
345static void 345static void
346run(void *cls, 346run (void *cls,
347 char *const *args, 347 char *const *args,
348 const char *cfgfile, 348 const char *cfgfile,
349 const struct GNUNET_CONFIGURATION_Handle *cfg) 349 const struct GNUNET_CONFIGURATION_Handle *cfg)
350{ 350{
351 (void)cls; 351 (void) cls;
352 (void)cfgfile; 352 (void) cfgfile;
353 (void)cfg; 353 (void) cfg;
354 354
355 if (print_examples_flag) 355 if (print_examples_flag)
356 { 356 {
357 print_examples(); 357 print_examples ();
358 return; 358 return;
359 } 359 }
360 if (NULL == args[0]) 360 if (NULL == args[0])
361 { 361 {
362 fprintf(stderr, "%s", _("No hostkey file specified on command line\n")); 362 fprintf (stderr, "%s", _ ("No hostkey file specified on command line\n"));
363 return; 363 return;
364 } 364 }
365 if (list_keys) 365 if (list_keys)
366 { 366 {
367 print_key(args[0]); 367 print_key (args[0]);
368 return; 368 return;
369 } 369 }
370 if (make_keys > 0) 370 if (make_keys > 0)
371 { 371 {
372 create_keys(args[0], args[1]); 372 create_keys (args[0], args[1]);
373 return; 373 return;
374 } 374 }
375 if (print_public_key || print_public_key_hex || print_private_key) 375 if (print_public_key || print_public_key_hex || print_private_key)
376 {
377 char *str;
378 struct GNUNET_DISK_FileHandle *keyfile;
379 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
380 struct GNUNET_CRYPTO_EddsaPublicKey pub;
381
382 keyfile = GNUNET_DISK_file_open (args[0],
383 GNUNET_DISK_OPEN_READ,
384 GNUNET_DISK_PERM_NONE);
385 if (NULL == keyfile)
386 return;
387 while (sizeof(pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof(pk)))
376 { 388 {
377 char *str; 389 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
378 struct GNUNET_DISK_FileHandle *keyfile; 390 if (print_public_key_hex)
379 struct GNUNET_CRYPTO_EddsaPrivateKey pk; 391 {
380 struct GNUNET_CRYPTO_EddsaPublicKey pub; 392 print_hex ("HEX:", &pub, sizeof(pub));
381 393 }
382 keyfile = GNUNET_DISK_file_open(args[0], 394 else if (print_public_key)
383 GNUNET_DISK_OPEN_READ, 395 {
384 GNUNET_DISK_PERM_NONE); 396 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
385 if (NULL == keyfile) 397 fprintf (stdout, "%s\n", str);
386 return; 398 GNUNET_free (str);
387 while (sizeof(pk) == GNUNET_DISK_file_read(keyfile, &pk, sizeof(pk))) 399 }
388 { 400 else if (print_private_key)
389 GNUNET_CRYPTO_eddsa_key_get_public(&pk, &pub); 401 {
390 if (print_public_key_hex) 402 str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk);
391 { 403 fprintf (stdout, "%s\n", str);
392 print_hex("HEX:", &pub, sizeof(pub)); 404 GNUNET_free (str);
393 } 405 }
394 else if (print_public_key)
395 {
396 str = GNUNET_CRYPTO_eddsa_public_key_to_string(&pub);
397 fprintf(stdout, "%s\n", str);
398 GNUNET_free(str);
399 }
400 else if (print_private_key)
401 {
402 str = GNUNET_CRYPTO_eddsa_private_key_to_string(&pk);
403 fprintf(stdout, "%s\n", str);
404 GNUNET_free(str);
405 }
406 }
407 GNUNET_DISK_file_close(keyfile);
408 } 406 }
407 GNUNET_DISK_file_close (keyfile);
408 }
409} 409}
410 410
411 411
@@ -417,66 +417,66 @@ run(void *cls,
417 * @return 0 ok, 1 on error 417 * @return 0 ok, 1 on error
418 */ 418 */
419int 419int
420main(int argc, char *const *argv) 420main (int argc, char *const *argv)
421{ 421{
422 struct GNUNET_GETOPT_CommandLineOption options[] = 422 struct GNUNET_GETOPT_CommandLineOption options[] =
423 { GNUNET_GETOPT_option_flag('i', 423 { GNUNET_GETOPT_option_flag ('i',
424 "iterate", 424 "iterate",
425 gettext_noop( 425 gettext_noop (
426 "list keys included in a file (for testing)"), 426 "list keys included in a file (for testing)"),
427 &list_keys), 427 &list_keys),
428 GNUNET_GETOPT_option_uint( 428 GNUNET_GETOPT_option_uint (
429 'e', 429 'e',
430 "end=", 430 "end=",
431 "COUNT", 431 "COUNT",
432 gettext_noop("number of keys to list included in a file (for testing)"), 432 gettext_noop ("number of keys to list included in a file (for testing)"),
433 &list_keys_count), 433 &list_keys_count),
434 GNUNET_GETOPT_option_uint( 434 GNUNET_GETOPT_option_uint (
435 'g', 435 'g',
436 "generate-keys", 436 "generate-keys",
437 "COUNT", 437 "COUNT",
438 gettext_noop("create COUNT public-private key pairs (for testing)"), 438 gettext_noop ("create COUNT public-private key pairs (for testing)"),
439 &make_keys), 439 &make_keys),
440 GNUNET_GETOPT_option_flag('p', 440 GNUNET_GETOPT_option_flag ('p',
441 "print-public-key", 441 "print-public-key",
442 gettext_noop( 442 gettext_noop (
443 "print the public key in ASCII format"), 443 "print the public key in ASCII format"),
444 &print_public_key), 444 &print_public_key),
445 GNUNET_GETOPT_option_flag('P', 445 GNUNET_GETOPT_option_flag ('P',
446 "print-private-key", 446 "print-private-key",
447 gettext_noop( 447 gettext_noop (
448 "print the private key in ASCII format"), 448 "print the private key in ASCII format"),
449 &print_private_key), 449 &print_private_key),
450 GNUNET_GETOPT_option_flag('x', 450 GNUNET_GETOPT_option_flag ('x',
451 "print-hex", 451 "print-hex",
452 gettext_noop( 452 gettext_noop (
453 "print the public key in HEX format"), 453 "print the public key in HEX format"),
454 &print_public_key_hex), 454 &print_public_key_hex),
455 GNUNET_GETOPT_option_flag( 455 GNUNET_GETOPT_option_flag (
456 'E', 456 'E',
457 "examples", 457 "examples",
458 gettext_noop( 458 gettext_noop (
459 "print examples of ECC operations (used for compatibility testing)"), 459 "print examples of ECC operations (used for compatibility testing)"),
460 &print_examples_flag), 460 &print_examples_flag),
461 GNUNET_GETOPT_OPTION_END }; 461 GNUNET_GETOPT_OPTION_END };
462 int ret; 462 int ret;
463 463
464 list_keys_count = UINT32_MAX; 464 list_keys_count = UINT32_MAX;
465 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 465 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
466 return 2; 466 return 2;
467 467
468 ret = (GNUNET_OK == 468 ret = (GNUNET_OK ==
469 GNUNET_PROGRAM_run(argc, 469 GNUNET_PROGRAM_run (argc,
470 argv, 470 argv,
471 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]", 471 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
472 gettext_noop( 472 gettext_noop (
473 "Manipulate GNUnet private ECC key files"), 473 "Manipulate GNUnet private ECC key files"),
474 options, 474 options,
475 &run, 475 &run,
476 NULL)) 476 NULL))
477 ? 0 477 ? 0
478 : 1; 478 : 1;
479 GNUNET_free((void *)argv); 479 GNUNET_free ((void *) argv);
480 return ret; 480 return ret;
481} 481}
482 482
diff --git a/src/util/gnunet-qr.c b/src/util/gnunet-qr.c
index 91c36dbdf..af1a0e950 100644
--- a/src/util/gnunet-qr.c
+++ b/src/util/gnunet-qr.c
@@ -31,7 +31,7 @@
31 31
32#define LOG(fmt, ...) \ 32#define LOG(fmt, ...) \
33 if (verbose == true) \ 33 if (verbose == true) \
34 printf (fmt, ## __VA_ARGS__) 34 printf (fmt, ## __VA_ARGS__)
35 35
36/** 36/**
37 * Video device to capture from. Sane default for GNU/Linux systems. 37 * Video device to capture from. Sane default for GNU/Linux systems.
@@ -72,14 +72,14 @@ static struct GNUNET_DISK_PipeHandle *sigpipe;
72 * @param cls closure, NULL 72 * @param cls closure, NULL
73 */ 73 */
74static void 74static void
75maint_child_death(void *cls) 75maint_child_death (void *cls)
76{ 76{
77 enum GNUNET_OS_ProcessStatusType type; 77 enum GNUNET_OS_ProcessStatusType type;
78 78
79 if ((GNUNET_OK != GNUNET_OS_process_status(p, &type, &exit_code)) || 79 if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) ||
80 (type != GNUNET_OS_PROCESS_EXITED)) 80 (type != GNUNET_OS_PROCESS_EXITED))
81 GNUNET_break(0 == GNUNET_OS_process_kill(p, GNUNET_TERM_SIG)); 81 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
82 GNUNET_OS_process_destroy(p); 82 GNUNET_OS_process_destroy (p);
83} 83}
84 84
85 85
@@ -92,10 +92,10 @@ maint_child_death(void *cls)
92 * @param cfg configuration 92 * @param cfg configuration
93 */ 93 */
94static void 94static void
95gnunet_uri(void *cls, 95gnunet_uri (void *cls,
96 const char *uri, 96 const char *uri,
97 const char *cfgfile, 97 const char *cfgfile,
98 const struct GNUNET_CONFIGURATION_Handle *cfg) 98 const struct GNUNET_CONFIGURATION_Handle *cfg)
99{ 99{
100 const char *orig_uri; 100 const char *orig_uri;
101 const char *slash; 101 const char *slash;
@@ -104,45 +104,45 @@ gnunet_uri(void *cls,
104 struct GNUNET_SCHEDULER_Task *rt; 104 struct GNUNET_SCHEDULER_Task *rt;
105 105
106 orig_uri = uri; 106 orig_uri = uri;
107 if (0 != strncasecmp("gnunet://", uri, strlen("gnunet://"))) 107 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
108 { 108 {
109 fprintf(stderr, 109 fprintf (stderr,
110 _("Invalid URI: does not start with `%s'\n"), 110 _ ("Invalid URI: does not start with `%s'\n"),
111 "gnunet://"); 111 "gnunet://");
112 return; 112 return;
113 } 113 }
114 uri += strlen("gnunet://"); 114 uri += strlen ("gnunet://");
115 if (NULL == (slash = strchr(uri, '/'))) 115 if (NULL == (slash = strchr (uri, '/')))
116 { 116 {
117 fprintf(stderr, _("Invalid URI: fails to specify subsystem\n")); 117 fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
118 return; 118 return;
119 } 119 }
120 subsystem = GNUNET_strndup(uri, slash - uri); 120 subsystem = GNUNET_strndup (uri, slash - uri);
121 if (GNUNET_OK != 121 if (GNUNET_OK !=
122 GNUNET_CONFIGURATION_get_value_string(cfg, "uri", subsystem, &program)) 122 GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
123 { 123 {
124 fprintf(stderr, _("No handler known for subsystem `%s'\n"), subsystem); 124 fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
125 GNUNET_free(subsystem); 125 GNUNET_free (subsystem);
126 return; 126 return;
127 } 127 }
128 GNUNET_free(subsystem); 128 GNUNET_free (subsystem);
129 rt = GNUNET_SCHEDULER_add_read_file( 129 rt = GNUNET_SCHEDULER_add_read_file (
130 GNUNET_TIME_UNIT_FOREVER_REL, 130 GNUNET_TIME_UNIT_FOREVER_REL,
131 GNUNET_DISK_pipe_handle(sigpipe, GNUNET_DISK_PIPE_END_READ), 131 GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
132 &maint_child_death, 132 &maint_child_death,
133 NULL); 133 NULL);
134 p = GNUNET_OS_start_process(GNUNET_NO, 134 p = GNUNET_OS_start_process (GNUNET_NO,
135 0, 135 0,
136 NULL, 136 NULL,
137 NULL, 137 NULL,
138 NULL, 138 NULL,
139 program, 139 program,
140 program, 140 program,
141 orig_uri, 141 orig_uri,
142 NULL); 142 NULL);
143 GNUNET_free(program); 143 GNUNET_free (program);
144 if (NULL == p) 144 if (NULL == p)
145 GNUNET_SCHEDULER_cancel(rt); 145 GNUNET_SCHEDULER_cancel (rt);
146} 146}
147 147
148 148
@@ -153,54 +153,54 @@ gnunet_uri(void *cls,
153 * @return NULL on error 153 * @return NULL on error
154 */ 154 */
155static const zbar_symbol_t * 155static const zbar_symbol_t *
156get_symbol(zbar_processor_t *proc) 156get_symbol (zbar_processor_t *proc)
157{ 157{
158 const zbar_symbol_set_t *symbols; 158 const zbar_symbol_set_t *symbols;
159 int rc; 159 int rc;
160 int n; 160 int n;
161 161
162 if (0 != zbar_processor_parse_config(proc, "enable")) 162 if (0 != zbar_processor_parse_config (proc, "enable"))
163 { 163 {
164 GNUNET_break(0); 164 GNUNET_break (0);
165 return NULL; 165 return NULL;
166 } 166 }
167 167
168 /* initialize the Processor */ 168 /* initialize the Processor */
169 if (0 != (rc = zbar_processor_init(proc, device, 1))) 169 if (0 != (rc = zbar_processor_init (proc, device, 1)))
170 { 170 {
171 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 "Failed to open device `%s': %d\n", 172 "Failed to open device `%s': %d\n",
173 device, 173 device,
174 rc); 174 rc);
175 return NULL; 175 return NULL;
176 } 176 }
177 177
178 /* enable the preview window */ 178 /* enable the preview window */
179 if ((0 != (rc = zbar_processor_set_visible(proc, 1))) || 179 if ((0 != (rc = zbar_processor_set_visible (proc, 1))) ||
180 (0 != (rc = zbar_processor_set_active(proc, 1)))) 180 (0 != (rc = zbar_processor_set_active (proc, 1))))
181 { 181 {
182 GNUNET_break(0); 182 GNUNET_break (0);
183 return NULL; 183 return NULL;
184 } 184 }
185 185
186 /* read at least one barcode (or until window closed) */ 186 /* read at least one barcode (or until window closed) */
187 LOG("Capturing\n"); 187 LOG ("Capturing\n");
188 n = zbar_process_one(proc, -1); 188 n = zbar_process_one (proc, -1);
189 189
190 /* hide the preview window */ 190 /* hide the preview window */
191 (void)zbar_processor_set_active(proc, 0); 191 (void) zbar_processor_set_active (proc, 0);
192 (void)zbar_processor_set_visible(proc, 0); 192 (void) zbar_processor_set_visible (proc, 0);
193 if (-1 == n) 193 if (-1 == n)
194 return NULL; /* likely user closed the window */ 194 return NULL; /* likely user closed the window */
195 LOG("Got %i images\n", n); 195 LOG ("Got %i images\n", n);
196 /* extract results */ 196 /* extract results */
197 symbols = zbar_processor_get_results(proc); 197 symbols = zbar_processor_get_results (proc);
198 if (NULL == symbols) 198 if (NULL == symbols)
199 { 199 {
200 GNUNET_break(0); 200 GNUNET_break (0);
201 return NULL; 201 return NULL;
202 } 202 }
203 return zbar_symbol_set_first_symbol(symbols); 203 return zbar_symbol_set_first_symbol (symbols);
204} 204}
205 205
206 206
@@ -210,7 +210,7 @@ get_symbol(zbar_processor_t *proc)
210 * @return NULL on error, otherwise the URI that we found 210 * @return NULL on error, otherwise the URI that we found
211 */ 211 */
212static char * 212static char *
213run_zbar() 213run_zbar ()
214{ 214{
215 zbar_processor_t *proc; 215 zbar_processor_t *proc;
216 const char *data; 216 const char *data;
@@ -218,32 +218,32 @@ run_zbar()
218 const zbar_symbol_t *symbol; 218 const zbar_symbol_t *symbol;
219 219
220 /* configure the Processor */ 220 /* configure the Processor */
221 proc = zbar_processor_create(1); 221 proc = zbar_processor_create (1);
222 if (NULL == proc) 222 if (NULL == proc)
223 { 223 {
224 GNUNET_break(0); 224 GNUNET_break (0);
225 return NULL; 225 return NULL;
226 } 226 }
227 227
228 symbol = get_symbol(proc); 228 symbol = get_symbol (proc);
229 if (NULL == symbol) 229 if (NULL == symbol)
230 { 230 {
231 zbar_processor_destroy(proc); 231 zbar_processor_destroy (proc);
232 return NULL; 232 return NULL;
233 } 233 }
234 data = zbar_symbol_get_data(symbol); 234 data = zbar_symbol_get_data (symbol);
235 if (NULL == data) 235 if (NULL == data)
236 { 236 {
237 GNUNET_break(0); 237 GNUNET_break (0);
238 zbar_processor_destroy(proc); 238 zbar_processor_destroy (proc);
239 return NULL; 239 return NULL;
240 } 240 }
241 LOG("Found %s \"%s\"\n", 241 LOG ("Found %s \"%s\"\n",
242 zbar_get_symbol_name(zbar_symbol_get_type(symbol)), 242 zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
243 data); 243 data);
244 ret = GNUNET_strdup(data); 244 ret = GNUNET_strdup (data);
245 /* clean up */ 245 /* clean up */
246 zbar_processor_destroy(proc); 246 zbar_processor_destroy (proc);
247 return ret; 247 return ret;
248} 248}
249 249
@@ -257,55 +257,55 @@ run_zbar()
257 * @param cfg configuration 257 * @param cfg configuration
258 */ 258 */
259static void 259static void
260run(void *cls, 260run (void *cls,
261 char *const *args, 261 char *const *args,
262 const char *cfgfile, 262 const char *cfgfile,
263 const struct GNUNET_CONFIGURATION_Handle *cfg) 263 const struct GNUNET_CONFIGURATION_Handle *cfg)
264{ 264{
265 char *data; 265 char *data;
266 266
267 data = run_zbar(); 267 data = run_zbar ();
268 if (NULL == data) 268 if (NULL == data)
269 return; 269 return;
270 gnunet_uri(cls, data, cfgfile, cfg); 270 gnunet_uri (cls, data, cfgfile, cfg);
271 if (exit_code != 0) 271 if (exit_code != 0)
272 { 272 {
273 printf("Failed to add URI %s\n", data); 273 printf ("Failed to add URI %s\n", data);
274 } 274 }
275 else 275 else
276 { 276 {
277 printf("Added URI %s\n", data); 277 printf ("Added URI %s\n", data);
278 } 278 }
279 GNUNET_free(data); 279 GNUNET_free (data);
280}; 280};
281 281
282 282
283int 283int
284main(int argc, char *const *argv) 284main (int argc, char *const *argv)
285{ 285{
286 int ret; 286 int ret;
287 struct GNUNET_GETOPT_CommandLineOption options[] = 287 struct GNUNET_GETOPT_CommandLineOption options[] =
288 { GNUNET_GETOPT_option_string( 288 { GNUNET_GETOPT_option_string (
289 'd', 289 'd',
290 "device", 290 "device",
291 "DEVICE", 291 "DEVICE",
292 gettext_noop("use video-device DEVICE (default: /dev/video0"), 292 gettext_noop ("use video-device DEVICE (default: /dev/video0"),
293 &device), 293 &device),
294 GNUNET_GETOPT_option_flag('\0', 294 GNUNET_GETOPT_option_flag ('\0',
295 "verbose", 295 "verbose",
296 gettext_noop("be verbose"), 296 gettext_noop ("be verbose"),
297 &verbose), 297 &verbose),
298 GNUNET_GETOPT_option_flag('s', 298 GNUNET_GETOPT_option_flag ('s',
299 "silent", 299 "silent",
300 gettext_noop("do not show preview windows"), 300 gettext_noop ("do not show preview windows"),
301 &silent), 301 &silent),
302 GNUNET_GETOPT_OPTION_END }; 302 GNUNET_GETOPT_OPTION_END };
303 303
304 ret = GNUNET_PROGRAM_run( 304 ret = GNUNET_PROGRAM_run (
305 argc, 305 argc,
306 argv, 306 argv,
307 "gnunet-qr", 307 "gnunet-qr",
308 gettext_noop( 308 gettext_noop (
309 "Scan a QR code using a video device and import the uri read"), 309 "Scan a QR code using a video device and import the uri read"),
310 options, 310 options,
311 &run, 311 &run,
diff --git a/src/util/gnunet-resolver.c b/src/util/gnunet-resolver.c
index dd1e8f5e0..83329635a 100644
--- a/src/util/gnunet-resolver.c
+++ b/src/util/gnunet-resolver.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_resolver_service.h" 28#include "gnunet_resolver_service.h"
29 29
30#define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 30#define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
31 31
32/** 32/**
33 * Flag for reverse lookup. 33 * Flag for reverse lookup.
@@ -43,15 +43,15 @@ static int reverse;
43 * on the last call to the callback 43 * on the last call to the callback
44 */ 44 */
45static void 45static void
46print_hostname(void *cls, 46print_hostname (void *cls,
47 const char *hostname) 47 const char *hostname)
48{ 48{
49 (void)cls; 49 (void) cls;
50 if (NULL == hostname) 50 if (NULL == hostname)
51 return; 51 return;
52 fprintf(stdout, 52 fprintf (stdout,
53 "%s\n", 53 "%s\n",
54 hostname); 54 hostname);
55} 55}
56 56
57 57
@@ -63,17 +63,17 @@ print_hostname(void *cls,
63 * @param addrlen length of the address 63 * @param addrlen length of the address
64 */ 64 */
65static void 65static void
66print_sockaddr(void *cls, 66print_sockaddr (void *cls,
67 const struct sockaddr *addr, 67 const struct sockaddr *addr,
68 socklen_t addrlen) 68 socklen_t addrlen)
69{ 69{
70 (void)cls; 70 (void) cls;
71 if (NULL == addr) 71 if (NULL == addr)
72 return; 72 return;
73 fprintf(stdout, 73 fprintf (stdout,
74 "%s\n", 74 "%s\n",
75 GNUNET_a2s(addr, 75 GNUNET_a2s (addr,
76 addrlen)); 76 addrlen));
77} 77}
78 78
79 79
@@ -86,69 +86,69 @@ print_sockaddr(void *cls,
86 * @param cfg configuration 86 * @param cfg configuration
87 */ 87 */
88static void 88static void
89run(void *cls, 89run (void *cls,
90 char *const *args, 90 char *const *args,
91 const char *cfgfile, 91 const char *cfgfile,
92 const struct GNUNET_CONFIGURATION_Handle *cfg) 92 const struct GNUNET_CONFIGURATION_Handle *cfg)
93{ 93{
94 const struct sockaddr *sa; 94 const struct sockaddr *sa;
95 socklen_t salen; 95 socklen_t salen;
96 struct sockaddr_in v4; 96 struct sockaddr_in v4;
97 struct sockaddr_in6 v6; 97 struct sockaddr_in6 v6;
98 98
99 (void)cls; 99 (void) cls;
100 (void)cfgfile; 100 (void) cfgfile;
101 (void)cfg; 101 (void) cfg;
102 if (NULL == args[0]) 102 if (NULL == args[0])
103 return; 103 return;
104 if (!reverse) 104 if (! reverse)
105 { 105 {
106 GNUNET_RESOLVER_ip_get(args[0], 106 GNUNET_RESOLVER_ip_get (args[0],
107 AF_UNSPEC, 107 AF_UNSPEC,
108 GET_TIMEOUT, 108 GET_TIMEOUT,
109 &print_sockaddr, 109 &print_sockaddr,
110 NULL); 110 NULL);
111 return; 111 return;
112 } 112 }
113 113
114 sa = NULL; 114 sa = NULL;
115 memset(&v4, 0, sizeof(v4)); 115 memset (&v4, 0, sizeof(v4));
116 v4.sin_family = AF_INET; 116 v4.sin_family = AF_INET;
117#if HAVE_SOCKADDR_IN_SIN_LEN 117#if HAVE_SOCKADDR_IN_SIN_LEN
118 v4.sin_len = sizeof(v4); 118 v4.sin_len = sizeof(v4);
119#endif 119#endif
120 if (1 == inet_pton(AF_INET, 120 if (1 == inet_pton (AF_INET,
121 args[0], 121 args[0],
122 &v4.sin_addr)) 122 &v4.sin_addr))
123 { 123 {
124 sa = (struct sockaddr *)&v4; 124 sa = (struct sockaddr *) &v4;
125 salen = sizeof(v4); 125 salen = sizeof(v4);
126 } 126 }
127 memset(&v6, 0, sizeof(v6)); 127 memset (&v6, 0, sizeof(v6));
128 v6.sin6_family = AF_INET6; 128 v6.sin6_family = AF_INET6;
129#if HAVE_SOCKADDR_IN_SIN_LEN 129#if HAVE_SOCKADDR_IN_SIN_LEN
130 v6.sin6_len = sizeof(v6); 130 v6.sin6_len = sizeof(v6);
131#endif 131#endif
132 if (1 == inet_pton(AF_INET6, 132 if (1 == inet_pton (AF_INET6,
133 args[0], 133 args[0],
134 &v6.sin6_addr)) 134 &v6.sin6_addr))
135 { 135 {
136 sa = (struct sockaddr *)&v6; 136 sa = (struct sockaddr *) &v6;
137 salen = sizeof(v6); 137 salen = sizeof(v6);
138 } 138 }
139 if (NULL == sa) 139 if (NULL == sa)
140 { 140 {
141 fprintf(stderr, 141 fprintf (stderr,
142 "`%s' is not a valid IP: %s\n", 142 "`%s' is not a valid IP: %s\n",
143 args[0], 143 args[0],
144 strerror(errno)); 144 strerror (errno));
145 return; 145 return;
146 } 146 }
147 GNUNET_RESOLVER_hostname_get(sa, salen, 147 GNUNET_RESOLVER_hostname_get (sa, salen,
148 GNUNET_YES, 148 GNUNET_YES,
149 GET_TIMEOUT, 149 GET_TIMEOUT,
150 &print_hostname, 150 &print_hostname,
151 NULL); 151 NULL);
152} 152}
153 153
154 154
@@ -160,25 +160,25 @@ run(void *cls,
160 * @return 0 ok, 1 on error 160 * @return 0 ok, 1 on error
161 */ 161 */
162int 162int
163main(int argc, char *const *argv) 163main (int argc, char *const *argv)
164{ 164{
165 struct GNUNET_GETOPT_CommandLineOption options[] = { 165 struct GNUNET_GETOPT_CommandLineOption options[] = {
166 GNUNET_GETOPT_option_flag('r', 166 GNUNET_GETOPT_option_flag ('r',
167 "reverse", 167 "reverse",
168 gettext_noop("perform a reverse lookup"), 168 gettext_noop ("perform a reverse lookup"),
169 &reverse), 169 &reverse),
170 GNUNET_GETOPT_OPTION_END 170 GNUNET_GETOPT_OPTION_END
171 }; 171 };
172 int ret; 172 int ret;
173 173
174 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 174 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
175 return 2; 175 return 2;
176 176
177 ret = (GNUNET_OK == 177 ret = (GNUNET_OK ==
178 GNUNET_PROGRAM_run(argc, argv, "gnunet-resolver [hostname]", 178 GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]",
179 gettext_noop("Use build-in GNUnet stub resolver"), 179 gettext_noop ("Use build-in GNUnet stub resolver"),
180 options, &run, NULL)) ? 0 : 1; 180 options, &run, NULL)) ? 0 : 1;
181 GNUNET_free((void*)argv); 181 GNUNET_free ((void*) argv);
182 return ret; 182 return ret;
183} 183}
184 184
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c
index 182ab346e..e5de0ba73 100644
--- a/src/util/gnunet-scrypt.c
+++ b/src/util/gnunet-scrypt.c
@@ -55,15 +55,15 @@ static char *pwfn;
55 * @param cls closure 55 * @param cls closure
56 */ 56 */
57static void 57static void
58shutdown_task(void *cls) 58shutdown_task (void *cls)
59{ 59{
60 (void)cls; 60 (void) cls;
61 if (sizeof(proof) != GNUNET_DISK_fn_write(pwfn, 61 if (sizeof(proof) != GNUNET_DISK_fn_write (pwfn,
62 &proof, 62 &proof,
63 sizeof(proof), 63 sizeof(proof),
64 GNUNET_DISK_PERM_USER_READ | 64 GNUNET_DISK_PERM_USER_READ
65 GNUNET_DISK_PERM_USER_WRITE)) 65 | GNUNET_DISK_PERM_USER_WRITE))
66 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "write", pwfn); 66 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", pwfn);
67} 67}
68 68
69 69
@@ -75,18 +75,18 @@ shutdown_task(void *cls)
75 * @param result where to write the resulting hash 75 * @param result where to write the resulting hash
76 */ 76 */
77static void 77static void
78pow_hash(const void *buf, size_t buf_len, struct GNUNET_HashCode *result) 78pow_hash (const void *buf, size_t buf_len, struct GNUNET_HashCode *result)
79{ 79{
80 GNUNET_break( 80 GNUNET_break (
81 0 == gcry_kdf_derive(buf, 81 0 == gcry_kdf_derive (buf,
82 buf_len, 82 buf_len,
83 GCRY_KDF_SCRYPT, 83 GCRY_KDF_SCRYPT,
84 1 /* subalgo */, 84 1 /* subalgo */,
85 "gnunet-proof-of-work", 85 "gnunet-proof-of-work",
86 strlen("gnunet-proof-of-work"), 86 strlen ("gnunet-proof-of-work"),
87 2 /* iterations; keep cost of individual op small */, 87 2 /* iterations; keep cost of individual op small */,
88 sizeof(struct GNUNET_HashCode), 88 sizeof(struct GNUNET_HashCode),
89 result)); 89 result));
90} 90}
91 91
92 92
@@ -97,12 +97,12 @@ pow_hash(const void *buf, size_t buf_len, struct GNUNET_HashCode *result)
97 * @return the number of leading zero bits. 97 * @return the number of leading zero bits.
98 */ 98 */
99static unsigned int 99static unsigned int
100count_leading_zeroes(const struct GNUNET_HashCode *hash) 100count_leading_zeroes (const struct GNUNET_HashCode *hash)
101{ 101{
102 unsigned int hash_count; 102 unsigned int hash_count;
103 103
104 hash_count = 0; 104 hash_count = 0;
105 while (0 == GNUNET_CRYPTO_hash_get_bit(hash, hash_count)) 105 while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
106 hash_count++; 106 hash_count++;
107 return hash_count; 107 return hash_count;
108} 108}
@@ -115,68 +115,68 @@ count_leading_zeroes(const struct GNUNET_HashCode *hash)
115 * @param tc task context 115 * @param tc task context
116 */ 116 */
117static void 117static void
118find_proof(void *cls) 118find_proof (void *cls)
119{ 119{
120#define ROUND_SIZE 10 120#define ROUND_SIZE 10
121 uint64_t counter; 121 uint64_t counter;
122 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey) + 122 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
123 sizeof(uint64_t)] GNUNET_ALIGN; 123 + sizeof(uint64_t)] GNUNET_ALIGN;
124 struct GNUNET_HashCode result; 124 struct GNUNET_HashCode result;
125 unsigned int i; 125 unsigned int i;
126 struct GNUNET_TIME_Absolute timestamp; 126 struct GNUNET_TIME_Absolute timestamp;
127 struct GNUNET_TIME_Relative elapsed; 127 struct GNUNET_TIME_Relative elapsed;
128 128
129 (void)cls; 129 (void) cls;
130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Got Proof of Work %llu\n", 131 "Got Proof of Work %llu\n",
132 (unsigned long long)proof); 132 (unsigned long long) proof);
133 proof_task = NULL; 133 proof_task = NULL;
134 GNUNET_memcpy(&buf[sizeof(uint64_t)], 134 GNUNET_memcpy (&buf[sizeof(uint64_t)],
135 &pub, 135 &pub,
136 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)); 136 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey));
137 i = 0; 137 i = 0;
138 counter = proof; 138 counter = proof;
139 timestamp = GNUNET_TIME_absolute_get(); 139 timestamp = GNUNET_TIME_absolute_get ();
140 while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) 140 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
141 {
142 GNUNET_memcpy (buf, &counter, sizeof(uint64_t));
143 pow_hash (buf, sizeof(buf), &result);
144 if (nse_work_required <= count_leading_zeroes (&result))
141 { 145 {
142 GNUNET_memcpy(buf, &counter, sizeof(uint64_t));
143 pow_hash(buf, sizeof(buf), &result);
144 if (nse_work_required <= count_leading_zeroes(&result))
145 {
146 proof = counter;
147 fprintf(stdout,
148 "Proof of work found: %llu!\n",
149 (unsigned long long)proof);
150 GNUNET_SCHEDULER_shutdown();
151 return;
152 }
153 counter++;
154 i++;
155 }
156 elapsed = GNUNET_TIME_absolute_get_duration(timestamp);
157 elapsed = GNUNET_TIME_relative_divide(elapsed, ROUND_SIZE);
158 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
159 "Current: %llu [%s/proof]\n",
160 (unsigned long long)counter,
161 GNUNET_STRINGS_relative_time_to_string(elapsed, 0));
162 if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
163 {
164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
165 "Testing proofs currently at %llu\n",
166 (unsigned long long)counter);
167 /* remember progress every 100 rounds */
168 proof = counter; 146 proof = counter;
169 shutdown_task(NULL); 147 fprintf (stdout,
148 "Proof of work found: %llu!\n",
149 (unsigned long long) proof);
150 GNUNET_SCHEDULER_shutdown ();
151 return;
170 } 152 }
153 counter++;
154 i++;
155 }
156 elapsed = GNUNET_TIME_absolute_get_duration (timestamp);
157 elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE);
158 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
159 "Current: %llu [%s/proof]\n",
160 (unsigned long long) counter,
161 GNUNET_STRINGS_relative_time_to_string (elapsed, 0));
162 if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
163 {
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Testing proofs currently at %llu\n",
166 (unsigned long long) counter);
167 /* remember progress every 100 rounds */
168 proof = counter;
169 shutdown_task (NULL);
170 }
171 else 171 else
172 { 172 {
173 proof = counter; 173 proof = counter;
174 } 174 }
175 proof_task = 175 proof_task =
176 GNUNET_SCHEDULER_add_delayed_with_priority(proof_find_delay, 176 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
177 GNUNET_SCHEDULER_PRIORITY_IDLE, 177 GNUNET_SCHEDULER_PRIORITY_IDLE,
178 &find_proof, 178 &find_proof,
179 NULL); 179 NULL);
180} 180}
181 181
182 182
@@ -189,101 +189,101 @@ find_proof(void *cls)
189 * @param cfg configuration 189 * @param cfg configuration
190 */ 190 */
191static void 191static void
192run(void *cls, 192run (void *cls,
193 char *const *args, 193 char *const *args,
194 const char *cfgfile, 194 const char *cfgfile,
195 const struct GNUNET_CONFIGURATION_Handle *config) 195 const struct GNUNET_CONFIGURATION_Handle *config)
196{ 196{
197 struct GNUNET_CRYPTO_EddsaPrivateKey *pk; 197 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
198 char *pids; 198 char *pids;
199 199
200 (void)cls; 200 (void) cls;
201 (void)args; 201 (void) args;
202 (void)cfgfile; 202 (void) cfgfile;
203 cfg = config; 203 cfg = config;
204 /* load proof of work */ 204 /* load proof of work */
205 if (NULL == pwfn) 205 if (NULL == pwfn)
206 {
207 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
208 "NSE",
209 "PROOFFILE",
210 &pwfn))
206 { 211 {
207 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, 212 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE");
208 "NSE", 213 GNUNET_SCHEDULER_shutdown ();
209 "PROOFFILE", 214 return;
210 &pwfn))
211 {
212 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE");
213 GNUNET_SCHEDULER_shutdown();
214 return;
215 }
216 } 215 }
217 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn); 216 }
218 if ((GNUNET_YES != GNUNET_DISK_file_test(pwfn)) || 217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn);
219 (sizeof(proof) != GNUNET_DISK_fn_read(pwfn, &proof, sizeof(proof)))) 218 if ((GNUNET_YES != GNUNET_DISK_file_test (pwfn)) ||
219 (sizeof(proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof(proof))))
220 proof = 0; 220 proof = 0;
221 221
222 /* load private key */ 222 /* load private key */
223 if (NULL == pkfn) 223 if (NULL == pkfn)
224 {
225 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
226 "PEER",
227 "PRIVATE_KEY",
228 &pkfn))
224 { 229 {
225 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, 230 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
226 "PEER", 231 "PEER",
227 "PRIVATE_KEY", 232 "PRIVATE_KEY");
228 &pkfn))
229 {
230 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
231 "PEER",
232 "PRIVATE_KEY");
233 return;
234 }
235 }
236 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
237 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file(pkfn)))
238 {
239 fprintf(stderr, _("Loading hostkey from `%s' failed.\n"), pkfn);
240 GNUNET_free(pkfn);
241 return; 233 return;
242 } 234 }
243 GNUNET_free(pkfn); 235 }
244 GNUNET_CRYPTO_eddsa_key_get_public(pk, &pub); 236 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
245 GNUNET_free(pk); 237 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
246 pids = GNUNET_CRYPTO_eddsa_public_key_to_string(&pub); 238 {
247 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids); 239 fprintf (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn);
248 GNUNET_free(pids); 240 GNUNET_free (pkfn);
241 return;
242 }
243 GNUNET_free (pkfn);
244 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
245 GNUNET_free (pk);
246 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids);
248 GNUNET_free (pids);
249 249
250 /* get target bit amount */ 250 /* get target bit amount */
251 if (0 == nse_work_required) 251 if (0 == nse_work_required)
252 {
253 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
254 "NSE",
255 "WORKBITS",
256 &nse_work_required))
257 {
258 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS");
259 GNUNET_SCHEDULER_shutdown ();
260 return;
261 }
262 if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8)
252 { 263 {
253 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, 264 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
254 "NSE", 265 "NSE",
255 "WORKBITS", 266 "WORKBITS",
256 &nse_work_required)) 267 _ ("Value is too large.\n"));
257 { 268 GNUNET_SCHEDULER_shutdown ();
258 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS"); 269 return;
259 GNUNET_SCHEDULER_shutdown(); 270 }
260 return; 271 else if (0 == nse_work_required)
261 } 272 {
262 if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8) 273 GNUNET_SCHEDULER_shutdown ();
263 { 274 return;
264 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR,
265 "NSE",
266 "WORKBITS",
267 _("Value is too large.\n"));
268 GNUNET_SCHEDULER_shutdown();
269 return;
270 }
271 else if (0 == nse_work_required)
272 {
273 GNUNET_SCHEDULER_shutdown();
274 return;
275 }
276 } 275 }
277 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required); 276 }
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required);
278 278
279 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Delay between tries: %s\n", 280 "Delay between tries: %s\n",
281 GNUNET_STRINGS_relative_time_to_string(proof_find_delay, 1)); 281 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1));
282 proof_task = 282 proof_task =
283 GNUNET_SCHEDULER_add_with_priority(GNUNET_SCHEDULER_PRIORITY_IDLE, 283 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
284 &find_proof, 284 &find_proof,
285 NULL); 285 NULL);
286 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL); 286 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
287} 287}
288 288
289 289
@@ -295,52 +295,52 @@ run(void *cls,
295 * @return 0 ok, 1 on error 295 * @return 0 ok, 1 on error
296 */ 296 */
297int 297int
298main(int argc, char *const *argv) 298main (int argc, char *const *argv)
299{ 299{
300 struct GNUNET_GETOPT_CommandLineOption options[] = 300 struct GNUNET_GETOPT_CommandLineOption options[] =
301 { GNUNET_GETOPT_option_ulong( 301 { GNUNET_GETOPT_option_ulong (
302 'b', 302 'b',
303 "bits", 303 "bits",
304 "BITS", 304 "BITS",
305 gettext_noop("number of bits to require for the proof of work"), 305 gettext_noop ("number of bits to require for the proof of work"),
306 &nse_work_required), 306 &nse_work_required),
307 GNUNET_GETOPT_option_filename( 307 GNUNET_GETOPT_option_filename (
308 'k', 308 'k',
309 "keyfile", 309 "keyfile",
310 "FILE", 310 "FILE",
311 gettext_noop("file with private key, otherwise default is used"), 311 gettext_noop ("file with private key, otherwise default is used"),
312 &pkfn), 312 &pkfn),
313 GNUNET_GETOPT_option_filename( 313 GNUNET_GETOPT_option_filename (
314 'o', 314 'o',
315 "outfile", 315 "outfile",
316 "FILE", 316 "FILE",
317 gettext_noop("file with proof of work, otherwise default is used"), 317 gettext_noop ("file with proof of work, otherwise default is used"),
318 &pwfn), 318 &pwfn),
319 GNUNET_GETOPT_option_relative_time('t', 319 GNUNET_GETOPT_option_relative_time ('t',
320 "timeout", 320 "timeout",
321 "TIME", 321 "TIME",
322 gettext_noop( 322 gettext_noop (
323 "time to wait between calculations"), 323 "time to wait between calculations"),
324 &proof_find_delay), 324 &proof_find_delay),
325 GNUNET_GETOPT_OPTION_END }; 325 GNUNET_GETOPT_OPTION_END };
326 int ret; 326 int ret;
327 327
328 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 328 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
329 return 2; 329 return 2;
330 330
331 ret = 331 ret =
332 (GNUNET_OK == 332 (GNUNET_OK ==
333 GNUNET_PROGRAM_run(argc, 333 GNUNET_PROGRAM_run (argc,
334 argv, 334 argv,
335 "gnunet-scrypt [OPTIONS] prooffile", 335 "gnunet-scrypt [OPTIONS] prooffile",
336 gettext_noop("Manipulate GNUnet proof of work files"), 336 gettext_noop ("Manipulate GNUnet proof of work files"),
337 options, 337 options,
338 &run, 338 &run,
339 NULL)) 339 NULL))
340 ? 0 340 ? 0
341 : 1; 341 : 1;
342 GNUNET_free((void *)argv); 342 GNUNET_free ((void *) argv);
343 GNUNET_free_non_null(pwfn); 343 GNUNET_free_non_null (pwfn);
344 return ret; 344 return ret;
345} 345}
346 346
diff --git a/src/util/gnunet-service-resolver.c b/src/util/gnunet-service-resolver.c
index a69a7bd66..23ba39fee 100644
--- a/src/util/gnunet-service-resolver.c
+++ b/src/util/gnunet-service-resolver.c
@@ -33,7 +33,7 @@
33/** 33/**
34 * How long do we wait for DNS answers? 34 * How long do we wait for DNS answers?
35 */ 35 */
36#define DNS_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30) 36#define DNS_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37 37
38/** 38/**
39 * Maximum number of hostnames we cache results for. 39 * Maximum number of hostnames we cache results for.
@@ -43,7 +43,8 @@
43/** 43/**
44 * Entry in list of cached DNS records for a hostname. 44 * Entry in list of cached DNS records for a hostname.
45 */ 45 */
46struct RecordListEntry { 46struct RecordListEntry
47{
47 /** 48 /**
48 * This is a doubly linked list. 49 * This is a doubly linked list.
49 */ 50 */
@@ -64,7 +65,8 @@ struct RecordListEntry {
64/** 65/**
65 * A cached DNS lookup result. 66 * A cached DNS lookup result.
66 */ 67 */
67struct ResolveCache { 68struct ResolveCache
69{
68 /** 70 /**
69 * This is a doubly linked list. 71 * This is a doubly linked list.
70 */ 72 */
@@ -95,7 +97,8 @@ struct ResolveCache {
95/** 97/**
96 * Information about pending lookups. 98 * Information about pending lookups.
97 */ 99 */
98struct ActiveLookup { 100struct ActiveLookup
101{
99 /** 102 /**
100 * Stored in a DLL. 103 * Stored in a DLL.
101 */ 104 */
@@ -203,21 +206,21 @@ static unsigned int cache_size;
203 * @param rc entry to free 206 * @param rc entry to free
204 */ 207 */
205static void 208static void
206free_cache_entry(struct ResolveCache *rc) 209free_cache_entry (struct ResolveCache *rc)
207{ 210{
208 struct RecordListEntry *pos; 211 struct RecordListEntry *pos;
209 212
210 while (NULL != (pos = rc->records_head)) 213 while (NULL != (pos = rc->records_head))
211 { 214 {
212 GNUNET_CONTAINER_DLL_remove(rc->records_head, rc->records_tail, pos); 215 GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos);
213 GNUNET_DNSPARSER_free_record(pos->record); 216 GNUNET_DNSPARSER_free_record (pos->record);
214 GNUNET_free(pos->record); 217 GNUNET_free (pos->record);
215 GNUNET_free(pos); 218 GNUNET_free (pos);
216 } 219 }
217 GNUNET_free_non_null(rc->hostname); 220 GNUNET_free_non_null (rc->hostname);
218 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, rc); 221 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, rc);
219 cache_size--; 222 cache_size--;
220 GNUNET_free(rc); 223 GNUNET_free (rc);
221} 224}
222 225
223 226
@@ -227,21 +230,21 @@ free_cache_entry(struct ResolveCache *rc)
227 * @param rc entry to free 230 * @param rc entry to free
228 */ 231 */
229static void 232static void
230free_hosts_entry(struct ResolveCache *rc) 233free_hosts_entry (struct ResolveCache *rc)
231{ 234{
232 struct RecordListEntry *pos; 235 struct RecordListEntry *pos;
233 236
234 while (NULL != (pos = rc->records_head)) 237 while (NULL != (pos = rc->records_head))
235 { 238 {
236 GNUNET_CONTAINER_DLL_remove(rc->records_head, rc->records_tail, pos); 239 GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos);
237 GNUNET_DNSPARSER_free_record(pos->record); 240 GNUNET_DNSPARSER_free_record (pos->record);
238 GNUNET_free(pos->record); 241 GNUNET_free (pos->record);
239 GNUNET_free(pos); 242 GNUNET_free (pos);
240 } 243 }
241 GNUNET_free_non_null(rc->hostname); 244 GNUNET_free_non_null (rc->hostname);
242 GNUNET_CONTAINER_DLL_remove(hosts_head, hosts_tail, rc); 245 GNUNET_CONTAINER_DLL_remove (hosts_head, hosts_tail, rc);
243 cache_size--; 246 cache_size--;
244 GNUNET_free(rc); 247 GNUNET_free (rc);
245} 248}
246 249
247 250
@@ -251,21 +254,21 @@ free_hosts_entry(struct ResolveCache *rc)
251 * @param al an active lookup 254 * @param al an active lookup
252 */ 255 */
253static void 256static void
254free_active_lookup(struct ActiveLookup *al) 257free_active_lookup (struct ActiveLookup *al)
255{ 258{
256 GNUNET_CONTAINER_DLL_remove(lookup_head, lookup_tail, al); 259 GNUNET_CONTAINER_DLL_remove (lookup_head, lookup_tail, al);
257 if (NULL != al->resolve_handle) 260 if (NULL != al->resolve_handle)
258 { 261 {
259 GNUNET_DNSSTUB_resolve_cancel(al->resolve_handle); 262 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
260 al->resolve_handle = NULL; 263 al->resolve_handle = NULL;
261 } 264 }
262 if (NULL != al->timeout_task) 265 if (NULL != al->timeout_task)
263 { 266 {
264 GNUNET_SCHEDULER_cancel(al->timeout_task); 267 GNUNET_SCHEDULER_cancel (al->timeout_task);
265 al->timeout_task = NULL; 268 al->timeout_task = NULL;
266 } 269 }
267 GNUNET_free_non_null(al->hostname); 270 GNUNET_free_non_null (al->hostname);
268 GNUNET_free(al); 271 GNUNET_free (al);
269} 272}
270 273
271 274
@@ -279,11 +282,11 @@ free_active_lookup(struct ActiveLookup *al)
279 * @return NULL if no nameserver is configured in this @a line 282 * @return NULL if no nameserver is configured in this @a line
280 */ 283 */
281static char * 284static char *
282extract_dns_server(const char *line, size_t line_len) 285extract_dns_server (const char *line, size_t line_len)
283{ 286{
284 if (0 == strncmp(line, "nameserver ", strlen("nameserver "))) 287 if (0 == strncmp (line, "nameserver ", strlen ("nameserver ")))
285 return GNUNET_strndup(line + strlen("nameserver "), 288 return GNUNET_strndup (line + strlen ("nameserver "),
286 line_len - strlen("nameserver ")); 289 line_len - strlen ("nameserver "));
287 return NULL; 290 return NULL;
288} 291}
289 292
@@ -298,11 +301,11 @@ extract_dns_server(const char *line, size_t line_len)
298 * @return NULL if no nameserver is configured in this @a line 301 * @return NULL if no nameserver is configured in this @a line
299 */ 302 */
300static char * 303static char *
301extract_search_domain(const char *line, size_t line_len) 304extract_search_domain (const char *line, size_t line_len)
302{ 305{
303 if (0 == strncmp(line, "search ", strlen("search "))) 306 if (0 == strncmp (line, "search ", strlen ("search ")))
304 return GNUNET_strndup(line + strlen("search "), 307 return GNUNET_strndup (line + strlen ("search "),
305 line_len - strlen("search ")); 308 line_len - strlen ("search "));
306 return NULL; 309 return NULL;
307} 310}
308 311
@@ -314,7 +317,7 @@ extract_search_domain(const char *line, size_t line_len)
314 * @return the number of server addresses in @server_addrs, -1 on error 317 * @return the number of server addresses in @server_addrs, -1 on error
315 */ 318 */
316static int 319static int
317lookup_dns_servers(char ***server_addrs) 320lookup_dns_servers (char ***server_addrs)
318{ 321{
319 struct GNUNET_DISK_FileHandle *fh; 322 struct GNUNET_DISK_FileHandle *fh;
320 struct GNUNET_DISK_MapHandle *mh; 323 struct GNUNET_DISK_MapHandle *mh;
@@ -323,63 +326,63 @@ lookup_dns_servers(char ***server_addrs)
323 size_t read_offset; 326 size_t read_offset;
324 unsigned int num_dns_servers; 327 unsigned int num_dns_servers;
325 328
326 fh = GNUNET_DISK_file_open("/etc/resolv.conf", 329 fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
327 GNUNET_DISK_OPEN_READ, 330 GNUNET_DISK_OPEN_READ,
328 GNUNET_DISK_PERM_NONE); 331 GNUNET_DISK_PERM_NONE);
329 if (NULL == fh) 332 if (NULL == fh)
330 { 333 {
331 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332 "Could not open /etc/resolv.conf. " 335 "Could not open /etc/resolv.conf. "
333 "DNS resolution will not be possible.\n"); 336 "DNS resolution will not be possible.\n");
334 return -1; 337 return -1;
335 } 338 }
336 if (GNUNET_OK != GNUNET_DISK_file_handle_size(fh, &bytes_read)) 339 if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
337 { 340 {
338 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
339 "Could not determine size of /etc/resolv.conf. " 342 "Could not determine size of /etc/resolv.conf. "
340 "DNS resolution will not be possible.\n"); 343 "DNS resolution will not be possible.\n");
341 GNUNET_DISK_file_close(fh); 344 GNUNET_DISK_file_close (fh);
342 return -1; 345 return -1;
343 } 346 }
344 if (((unsigned long long)bytes_read) > (unsigned long long)SIZE_MAX) 347 if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
345 { 348 {
346 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347 "/etc/resolv.conf file too large to mmap. " 350 "/etc/resolv.conf file too large to mmap. "
348 "DNS resolution will not be possible.\n"); 351 "DNS resolution will not be possible.\n");
349 GNUNET_DISK_file_close(fh); 352 GNUNET_DISK_file_close (fh);
350 return -1; 353 return -1;
351 } 354 }
352 buf = GNUNET_DISK_file_map(fh, 355 buf = GNUNET_DISK_file_map (fh,
353 &mh, 356 &mh,
354 GNUNET_DISK_MAP_TYPE_READ, 357 GNUNET_DISK_MAP_TYPE_READ,
355 (size_t)bytes_read); 358 (size_t) bytes_read);
356 *server_addrs = NULL; 359 *server_addrs = NULL;
357 read_offset = 0; 360 read_offset = 0;
358 num_dns_servers = 0; 361 num_dns_servers = 0;
359 while (read_offset < (size_t)bytes_read) 362 while (read_offset < (size_t) bytes_read)
363 {
364 const char *newline;
365 size_t line_len;
366 char *dns_server;
367
368 newline = strchr (buf + read_offset, '\n');
369 if (NULL == newline)
370 break;
371 line_len = newline - buf - read_offset;
372 dns_server = extract_dns_server (buf + read_offset, line_len);
373 if (NULL != dns_server)
360 { 374 {
361 const char *newline; 375 GNUNET_array_append (*server_addrs, num_dns_servers, dns_server);
362 size_t line_len;
363 char *dns_server;
364
365 newline = strchr(buf + read_offset, '\n');
366 if (NULL == newline)
367 break;
368 line_len = newline - buf - read_offset;
369 dns_server = extract_dns_server(buf + read_offset, line_len);
370 if (NULL != dns_server)
371 {
372 GNUNET_array_append(*server_addrs, num_dns_servers, dns_server);
373 }
374 else if (NULL == my_domain)
375 {
376 my_domain = extract_search_domain(buf + read_offset, line_len);
377 }
378 read_offset += line_len + 1;
379 } 376 }
380 GNUNET_DISK_file_unmap(mh); 377 else if (NULL == my_domain)
381 GNUNET_DISK_file_close(fh); 378 {
382 return (int)num_dns_servers; 379 my_domain = extract_search_domain (buf + read_offset, line_len);
380 }
381 read_offset += line_len + 1;
382 }
383 GNUNET_DISK_file_unmap (mh);
384 GNUNET_DISK_file_close (fh);
385 return (int) num_dns_servers;
383} 386}
384 387
385 388
@@ -390,52 +393,52 @@ lookup_dns_servers(char ***server_addrs)
390 * @param af address family of @a ip, AF_INET or AF_INET6 393 * @param af address family of @a ip, AF_INET or AF_INET6
391 */ 394 */
392static char * 395static char *
393make_reverse_hostname(const void *ip, int af) 396make_reverse_hostname (const void *ip, int af)
394{ 397{
395 char *buf = GNUNET_new_array(80, char); 398 char *buf = GNUNET_new_array (80, char);
396 int pos = 0; 399 int pos = 0;
397 400
398 if (AF_INET == af) 401 if (AF_INET == af)
402 {
403 struct in_addr *addr = (struct in_addr *) ip;
404 uint32_t ip_int = addr->s_addr;
405
406 for (int i = 3; i >= 0; i--)
399 { 407 {
400 struct in_addr *addr = (struct in_addr *)ip; 408 int n =
401 uint32_t ip_int = addr->s_addr; 409 GNUNET_snprintf (buf + pos, 80 - pos, "%u.", ((uint8_t *) &ip_int)[i]);
402 410 if (n < 0)
403 for (int i = 3; i >= 0; i--) 411 {
404 { 412 GNUNET_free (buf);
405 int n = 413 return NULL;
406 GNUNET_snprintf(buf + pos, 80 - pos, "%u.", ((uint8_t *)&ip_int)[i]); 414 }
407 if (n < 0) 415 pos += n;
408 {
409 GNUNET_free(buf);
410 return NULL;
411 }
412 pos += n;
413 }
414 pos += GNUNET_snprintf(buf + pos, 80 - pos, "in-addr.arpa");
415 } 416 }
417 pos += GNUNET_snprintf (buf + pos, 80 - pos, "in-addr.arpa");
418 }
416 else if (AF_INET6 == af) 419 else if (AF_INET6 == af)
420 {
421 struct in6_addr *addr = (struct in6_addr *) ip;
422 for (int i = 15; i >= 0; i--)
417 { 423 {
418 struct in6_addr *addr = (struct in6_addr *)ip; 424 int n =
419 for (int i = 15; i >= 0; i--) 425 GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] & 0xf);
420 { 426 if (n < 0)
421 int n = 427 {
422 GNUNET_snprintf(buf + pos, 80 - pos, "%x.", addr->s6_addr[i] & 0xf); 428 GNUNET_free (buf);
423 if (n < 0) 429 return NULL;
424 { 430 }
425 GNUNET_free(buf); 431 pos += n;
426 return NULL; 432 n = GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] >> 4);
427 } 433 if (n < 0)
428 pos += n; 434 {
429 n = GNUNET_snprintf(buf + pos, 80 - pos, "%x.", addr->s6_addr[i] >> 4); 435 GNUNET_free (buf);
430 if (n < 0) 436 return NULL;
431 { 437 }
432 GNUNET_free(buf); 438 pos += n;
433 return NULL;
434 }
435 pos += n;
436 }
437 pos += GNUNET_snprintf(buf + pos, 80 - pos, "ip6.arpa");
438 } 439 }
440 pos += GNUNET_snprintf (buf + pos, 80 - pos, "ip6.arpa");
441 }
439 buf[pos] = '\0'; 442 buf[pos] = '\0';
440 return buf; 443 return buf;
441} 444}
@@ -453,10 +456,10 @@ make_reverse_hostname(const void *ip, int af)
453 * does not match @a record_type 456 * does not match @a record_type
454 */ 457 */
455static int 458static int
456send_reply(struct GNUNET_DNSPARSER_Record *record, 459send_reply (struct GNUNET_DNSPARSER_Record *record,
457 uint16_t record_type, 460 uint16_t record_type,
458 uint32_t client_request_id, 461 uint32_t client_request_id,
459 struct GNUNET_SERVICE_Client *client) 462 struct GNUNET_SERVICE_Client *client)
460{ 463{
461 struct GNUNET_RESOLVER_ResponseMessage *msg; 464 struct GNUNET_RESOLVER_ResponseMessage *msg;
462 struct GNUNET_MQ_Envelope *env; 465 struct GNUNET_MQ_Envelope *env;
@@ -464,49 +467,49 @@ send_reply(struct GNUNET_DNSPARSER_Record *record,
464 size_t payload_len; 467 size_t payload_len;
465 468
466 switch (record->type) 469 switch (record->type)
467 { 470 {
468 case GNUNET_DNSPARSER_TYPE_CNAME: 471 case GNUNET_DNSPARSER_TYPE_CNAME:
469 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type) 472 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
470 return GNUNET_NO; 473 return GNUNET_NO;
471 payload = record->data.hostname; 474 payload = record->data.hostname;
472 payload_len = strlen(record->data.hostname) + 1; 475 payload_len = strlen (record->data.hostname) + 1;
473 break; 476 break;
474
475 case GNUNET_DNSPARSER_TYPE_PTR:
476 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
477 return GNUNET_NO;
478 payload = record->data.hostname;
479 payload_len = strlen(record->data.hostname) + 1;
480 break;
481 477
482 case GNUNET_DNSPARSER_TYPE_A: 478 case GNUNET_DNSPARSER_TYPE_PTR:
483 if ((GNUNET_DNSPARSER_TYPE_A != record_type) && 479 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
484 (GNUNET_DNSPARSER_TYPE_ALL != record_type)) 480 return GNUNET_NO;
485 return GNUNET_NO; 481 payload = record->data.hostname;
486 payload = record->data.raw.data; 482 payload_len = strlen (record->data.hostname) + 1;
487 payload_len = record->data.raw.data_len; 483 break;
488 break;
489 484
490 case GNUNET_DNSPARSER_TYPE_AAAA: 485 case GNUNET_DNSPARSER_TYPE_A:
491 if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) && 486 if ((GNUNET_DNSPARSER_TYPE_A != record_type) &&
492 (GNUNET_DNSPARSER_TYPE_ALL != record_type)) 487 (GNUNET_DNSPARSER_TYPE_ALL != record_type))
493 return GNUNET_NO; 488 return GNUNET_NO;
494 payload = record->data.raw.data; 489 payload = record->data.raw.data;
495 payload_len = record->data.raw.data_len; 490 payload_len = record->data.raw.data_len;
496 break; 491 break;
497 492
498 default: 493 case GNUNET_DNSPARSER_TYPE_AAAA:
499 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 494 if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
500 "Cannot handle DNS response type %u: not supported here\n", 495 (GNUNET_DNSPARSER_TYPE_ALL != record_type))
501 record->type);
502 return GNUNET_NO; 496 return GNUNET_NO;
503 } 497 payload = record->data.raw.data;
504 env = GNUNET_MQ_msg_extra(msg, 498 payload_len = record->data.raw.data_len;
505 payload_len, 499 break;
506 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 500
501 default:
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Cannot handle DNS response type %u: not supported here\n",
504 record->type);
505 return GNUNET_NO;
506 }
507 env = GNUNET_MQ_msg_extra (msg,
508 payload_len,
509 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
507 msg->client_id = client_request_id; 510 msg->client_id = client_request_id;
508 GNUNET_memcpy(&msg[1], payload, payload_len); 511 GNUNET_memcpy (&msg[1], payload, payload_len);
509 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env); 512 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
510 return GNUNET_YES; 513 return GNUNET_YES;
511} 514}
512 515
@@ -519,15 +522,15 @@ send_reply(struct GNUNET_DNSPARSER_Record *record,
519 * @param client where to send @a record 522 * @param client where to send @a record
520 */ 523 */
521static void 524static void
522send_end_msg(uint32_t client_request_id, struct GNUNET_SERVICE_Client *client) 525send_end_msg (uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
523{ 526{
524 struct GNUNET_RESOLVER_ResponseMessage *msg; 527 struct GNUNET_RESOLVER_ResponseMessage *msg;
525 struct GNUNET_MQ_Envelope *env; 528 struct GNUNET_MQ_Envelope *env;
526 529
527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n"); 530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n");
528 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); 531 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
529 msg->client_id = client_request_id; 532 msg->client_id = client_request_id;
530 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env); 533 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
531} 534}
532 535
533 536
@@ -539,27 +542,27 @@ send_end_msg(uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
539 * #GNUNET_NO if some entries are left 542 * #GNUNET_NO if some entries are left
540 */ 543 */
541static int 544static int
542remove_expired(struct ResolveCache *rc) 545remove_expired (struct ResolveCache *rc)
543{ 546{
544 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); 547 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
545 struct RecordListEntry *n; 548 struct RecordListEntry *n;
546 549
547 for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n) 550 for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n)
551 {
552 n = pos->next;
553 if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
548 { 554 {
549 n = pos->next; 555 GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos);
550 if (now.abs_value_us > pos->record->expiration_time.abs_value_us) 556 GNUNET_DNSPARSER_free_record (pos->record);
551 { 557 GNUNET_free (pos->record);
552 GNUNET_CONTAINER_DLL_remove(rc->records_head, rc->records_tail, pos); 558 GNUNET_free (pos);
553 GNUNET_DNSPARSER_free_record(pos->record);
554 GNUNET_free(pos->record);
555 GNUNET_free(pos);
556 }
557 } 559 }
560 }
558 if (NULL == rc->records_head) 561 if (NULL == rc->records_head)
559 { 562 {
560 free_cache_entry(rc); 563 free_cache_entry (rc);
561 return GNUNET_YES; 564 return GNUNET_YES;
562 } 565 }
563 return GNUNET_NO; 566 return GNUNET_NO;
564} 567}
565 568
@@ -574,10 +577,10 @@ remove_expired(struct ResolveCache *rc)
574 * @param client who should get the result? 577 * @param client who should get the result?
575 */ 578 */
576static void 579static void
577process_get(const char *hostname, 580process_get (const char *hostname,
578 uint16_t record_type, 581 uint16_t record_type,
579 uint32_t client_request_id, 582 uint32_t client_request_id,
580 struct GNUNET_SERVICE_Client *client); 583 struct GNUNET_SERVICE_Client *client);
581 584
582 585
583/** 586/**
@@ -592,10 +595,10 @@ process_get(const char *hostname,
592 * @param client handle to the client making the request (for sending the reply) 595 * @param client handle to the client making the request (for sending the reply)
593 */ 596 */
594static int 597static int
595try_cache(const char *hostname, 598try_cache (const char *hostname,
596 uint16_t record_type, 599 uint16_t record_type,
597 uint32_t client_request_id, 600 uint32_t client_request_id,
598 struct GNUNET_SERVICE_Client *client) 601 struct GNUNET_SERVICE_Client *client)
599{ 602{
600 struct ResolveCache *pos; 603 struct ResolveCache *pos;
601 struct ResolveCache *next; 604 struct ResolveCache *next;
@@ -604,57 +607,57 @@ try_cache(const char *hostname,
604 607
605 in_hosts = GNUNET_NO; 608 in_hosts = GNUNET_NO;
606 for (pos = hosts_head; NULL != pos; pos = pos->next) 609 for (pos = hosts_head; NULL != pos; pos = pos->next)
607 if (0 == strcmp(pos->hostname, hostname)) 610 if (0 == strcmp (pos->hostname, hostname))
608 {
609 in_hosts = GNUNET_YES;
610 break;
611 }
612 if (NULL == pos)
613 { 611 {
614 next = cache_head; 612 in_hosts = GNUNET_YES;
615 for (pos = next; NULL != pos; pos = next) 613 break;
616 {
617 next = pos->next;
618 if (GNUNET_YES == remove_expired(pos))
619 continue;
620 if (0 == strcmp(pos->hostname, hostname))
621 break;
622 }
623 } 614 }
624 if (NULL == pos) 615 if (NULL == pos)
616 {
617 next = cache_head;
618 for (pos = next; NULL != pos; pos = next)
625 { 619 {
626 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname); 620 next = pos->next;
627 return GNUNET_NO; 621 if (GNUNET_YES == remove_expired (pos))
622 continue;
623 if (0 == strcmp (pos->hostname, hostname))
624 break;
628 } 625 }
626 }
627 if (NULL == pos)
628 {
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname);
630 return GNUNET_NO;
631 }
629 if ((GNUNET_NO == in_hosts) && (cache_head != pos)) 632 if ((GNUNET_NO == in_hosts) && (cache_head != pos))
630 { 633 {
631 /* move result to head to achieve LRU for cache eviction */ 634 /* move result to head to achieve LRU for cache eviction */
632 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, pos); 635 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, pos);
633 GNUNET_CONTAINER_DLL_insert(cache_head, cache_tail, pos); 636 GNUNET_CONTAINER_DLL_insert (cache_head, cache_tail, pos);
634 } 637 }
635 found = GNUNET_NO; 638 found = GNUNET_NO;
636 for (struct RecordListEntry *rle = pos->records_head; NULL != rle; 639 for (struct RecordListEntry *rle = pos->records_head; NULL != rle;
637 rle = rle->next) 640 rle = rle->next)
641 {
642 const struct GNUNET_DNSPARSER_Record *record = rle->record;
643
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Found cache entry for '%s', record type '%u'\n",
646 hostname,
647 record_type);
648 if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
649 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found))
638 { 650 {
639 const struct GNUNET_DNSPARSER_Record *record = rle->record; 651 const char *hostname = record->data.hostname;
640 652
641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 653 process_get (hostname, record_type, client_request_id, client);
642 "Found cache entry for '%s', record type '%u'\n", 654 return GNUNET_YES; /* counts as a cache "hit" */
643 hostname,
644 record_type);
645 if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
646 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found))
647 {
648 const char *hostname = record->data.hostname;
649
650 process_get(hostname, record_type, client_request_id, client);
651 return GNUNET_YES; /* counts as a cache "hit" */
652 }
653 found |= send_reply(rle->record, record_type, client_request_id, client);
654 } 655 }
656 found |= send_reply (rle->record, record_type, client_request_id, client);
657 }
655 if (GNUNET_NO == found) 658 if (GNUNET_NO == found)
656 return GNUNET_NO; /* had records, but none matched! */ 659 return GNUNET_NO; /* had records, but none matched! */
657 send_end_msg(client_request_id, client); 660 send_end_msg (client_request_id, client);
658 return GNUNET_YES; 661 return GNUNET_YES;
659} 662}
660 663
@@ -671,64 +674,64 @@ try_cache(const char *hostname,
671 * @return #GNUNET_OK on success 674 * @return #GNUNET_OK on success
672 */ 675 */
673static int 676static int
674pack(const char *hostname, 677pack (const char *hostname,
675 uint16_t type, 678 uint16_t type,
676 uint16_t dns_id, 679 uint16_t dns_id,
677 char **packet_buf, 680 char **packet_buf,
678 size_t *packet_size) 681 size_t *packet_size)
679{ 682{
680 struct GNUNET_DNSPARSER_Query query; 683 struct GNUNET_DNSPARSER_Query query;
681 struct GNUNET_DNSPARSER_Packet packet; 684 struct GNUNET_DNSPARSER_Packet packet;
682 685
683 query.name = (char *)hostname; 686 query.name = (char *) hostname;
684 query.type = type; 687 query.type = type;
685 query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET; 688 query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
686 memset(&packet, 0, sizeof(packet)); 689 memset (&packet, 0, sizeof(packet));
687 packet.num_queries = 1; 690 packet.num_queries = 1;
688 packet.queries = &query; 691 packet.queries = &query;
689 packet.id = htons(dns_id); 692 packet.id = htons (dns_id);
690 packet.flags.recursion_desired = 1; 693 packet.flags.recursion_desired = 1;
691 if (GNUNET_OK != 694 if (GNUNET_OK !=
692 GNUNET_DNSPARSER_pack(&packet, UINT16_MAX, packet_buf, packet_size)) 695 GNUNET_DNSPARSER_pack (&packet, UINT16_MAX, packet_buf, packet_size))
693 { 696 {
694 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
695 "Failed to pack query for hostname `%s'\n", 698 "Failed to pack query for hostname `%s'\n",
696 hostname); 699 hostname);
697 packet_buf = NULL; 700 packet_buf = NULL;
698 return GNUNET_SYSERR; 701 return GNUNET_SYSERR;
699 } 702 }
700 return GNUNET_OK; 703 return GNUNET_OK;
701} 704}
702 705
703static void 706static void
704cache_answers(const char *name, 707cache_answers (const char *name,
705 struct GNUNET_DNSPARSER_Record *records, 708 struct GNUNET_DNSPARSER_Record *records,
706 unsigned int num_records) 709 unsigned int num_records)
707{ 710{
708 struct ResolveCache *rc; 711 struct ResolveCache *rc;
709 struct GNUNET_DNSPARSER_Record *record; 712 struct GNUNET_DNSPARSER_Record *record;
710 struct RecordListEntry *rle; 713 struct RecordListEntry *rle;
711 714
712 for (unsigned int i = 0; i != num_records; i++) 715 for (unsigned int i = 0; i != num_records; i++)
716 {
717 record = &records[i];
718
719 for (rc = cache_head; NULL != rc; rc = rc->next)
720 if (0 == strcasecmp (rc->hostname, name))
721 break;
722 if (NULL == rc)
713 { 723 {
714 record = &records[i]; 724 rc = GNUNET_new (struct ResolveCache);
715 725 rc->hostname = GNUNET_strdup (name);
716 for (rc = cache_head; NULL != rc; rc = rc->next) 726 GNUNET_CONTAINER_DLL_insert (cache_head, cache_tail, rc);
717 if (0 == strcasecmp(rc->hostname, name)) 727 cache_size++;
718 break;
719 if (NULL == rc)
720 {
721 rc = GNUNET_new(struct ResolveCache);
722 rc->hostname = GNUNET_strdup(name);
723 GNUNET_CONTAINER_DLL_insert(cache_head, cache_tail, rc);
724 cache_size++;
725 }
726 /* TODO: ought to check first if we have this exact record
727 already in the cache! */
728 rle = GNUNET_new(struct RecordListEntry);
729 rle->record = GNUNET_DNSPARSER_duplicate_record(record);
730 GNUNET_CONTAINER_DLL_insert(rc->records_head, rc->records_tail, rle);
731 } 728 }
729 /* TODO: ought to check first if we have this exact record
730 already in the cache! */
731 rle = GNUNET_new (struct RecordListEntry);
732 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
733 GNUNET_CONTAINER_DLL_insert (rc->records_head, rc->records_tail, rle);
734 }
732} 735}
733 736
734/** 737/**
@@ -740,107 +743,107 @@ cache_answers(const char *name,
740 * @param dns_len number of bytes in @a dns 743 * @param dns_len number of bytes in @a dns
741 */ 744 */
742static void 745static void
743handle_resolve_result(void *cls, 746handle_resolve_result (void *cls,
744 const struct GNUNET_TUN_DnsHeader *dns, 747 const struct GNUNET_TUN_DnsHeader *dns,
745 size_t dns_len) 748 size_t dns_len)
746{ 749{
747 struct ActiveLookup *al = cls; 750 struct ActiveLookup *al = cls;
748 struct GNUNET_DNSPARSER_Packet *parsed; 751 struct GNUNET_DNSPARSER_Packet *parsed;
749 752
750 parsed = GNUNET_DNSPARSER_parse((const char *)dns, dns_len); 753 parsed = GNUNET_DNSPARSER_parse ((const char *) dns, dns_len);
751 if (NULL == parsed) 754 if (NULL == parsed)
755 {
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
758 al->hostname,
759 al->dns_id);
760 return;
761 }
762 if (al->dns_id != ntohs (parsed->id))
763 {
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Request ID in DNS reply does not match\n");
766 GNUNET_DNSPARSER_free_packet (parsed);
767 return;
768 }
769 if (0 == parsed->num_answers + parsed->num_authority_records
770 + parsed->num_additional_records)
771 {
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "DNS reply (hostname %s, request ID %u) contains no answers\n",
774 al->hostname,
775 (unsigned int) al->client_request_id);
776 /* resume by trying again from cache */
777 if (GNUNET_NO == try_cache (al->hostname,
778 al->record_type,
779 al->client_request_id,
780 al->client))
781 /* cache failed, tell client we could not get an answer */
752 { 782 {
753 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 783 send_end_msg (al->client_request_id, al->client);
754 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
755 al->hostname,
756 al->dns_id);
757 return;
758 }
759 if (al->dns_id != ntohs(parsed->id))
760 {
761 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
762 "Request ID in DNS reply does not match\n");
763 GNUNET_DNSPARSER_free_packet(parsed);
764 return;
765 }
766 if (0 == parsed->num_answers + parsed->num_authority_records +
767 parsed->num_additional_records)
768 {
769 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
770 "DNS reply (hostname %s, request ID %u) contains no answers\n",
771 al->hostname,
772 (unsigned int)al->client_request_id);
773 /* resume by trying again from cache */
774 if (GNUNET_NO == try_cache(al->hostname,
775 al->record_type,
776 al->client_request_id,
777 al->client))
778 /* cache failed, tell client we could not get an answer */
779 {
780 send_end_msg(al->client_request_id, al->client);
781 }
782 GNUNET_DNSPARSER_free_packet(parsed);
783 free_active_lookup(al);
784 return;
785 } 784 }
785 GNUNET_DNSPARSER_free_packet (parsed);
786 free_active_lookup (al);
787 return;
788 }
786 /* LRU-based cache eviction: we remove from tail */ 789 /* LRU-based cache eviction: we remove from tail */
787 while (cache_size > MAX_CACHE) 790 while (cache_size > MAX_CACHE)
788 free_cache_entry(cache_tail); 791 free_cache_entry (cache_tail);
789 792
790 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Got reply for hostname %s and request ID %u\n", 794 "Got reply for hostname %s and request ID %u\n",
792 al->hostname, 795 al->hostname,
793 (unsigned int)al->client_request_id); 796 (unsigned int) al->client_request_id);
794 /* add to cache */ 797 /* add to cache */
795 cache_answers(al->hostname, parsed->answers, parsed->num_answers); 798 cache_answers (al->hostname, parsed->answers, parsed->num_answers);
796 cache_answers(al->hostname, 799 cache_answers (al->hostname,
797 parsed->authority_records, 800 parsed->authority_records,
798 parsed->num_authority_records); 801 parsed->num_authority_records);
799 cache_answers(al->hostname, 802 cache_answers (al->hostname,
800 parsed->additional_records, 803 parsed->additional_records,
801 parsed->num_additional_records); 804 parsed->num_additional_records);
802 805
803 /* see if we need to do the 2nd request for AAAA records */ 806 /* see if we need to do the 2nd request for AAAA records */
804 if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) && 807 if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
805 (GNUNET_NO == al->did_aaaa)) 808 (GNUNET_NO == al->did_aaaa))
806 { 809 {
807 char *packet_buf; 810 char *packet_buf;
808 size_t packet_size; 811 size_t packet_size;
809 uint16_t dns_id; 812 uint16_t dns_id;
810 813
811 dns_id = (uint16_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 814 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
812 UINT16_MAX); 815 UINT16_MAX);
813 if (GNUNET_OK == pack(al->hostname, 816 if (GNUNET_OK == pack (al->hostname,
814 GNUNET_DNSPARSER_TYPE_AAAA, 817 GNUNET_DNSPARSER_TYPE_AAAA,
815 dns_id, 818 dns_id,
816 &packet_buf, 819 &packet_buf,
817 &packet_size)) 820 &packet_size))
818 { 821 {
819 al->did_aaaa = GNUNET_YES; 822 al->did_aaaa = GNUNET_YES;
820 al->dns_id = dns_id; 823 al->dns_id = dns_id;
821 GNUNET_DNSSTUB_resolve_cancel(al->resolve_handle); 824 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
822 al->resolve_handle = GNUNET_DNSSTUB_resolve(dnsstub_ctx, 825 al->resolve_handle = GNUNET_DNSSTUB_resolve (dnsstub_ctx,
823 packet_buf, 826 packet_buf,
824 packet_size, 827 packet_size,
825 &handle_resolve_result, 828 &handle_resolve_result,
826 al); 829 al);
827 GNUNET_free(packet_buf); 830 GNUNET_free (packet_buf);
828 GNUNET_DNSPARSER_free_packet(parsed); 831 GNUNET_DNSPARSER_free_packet (parsed);
829 return; 832 return;
830 }
831 } 833 }
834 }
832 835
833 /* resume by trying again from cache */ 836 /* resume by trying again from cache */
834 if (GNUNET_NO == try_cache(al->hostname, 837 if (GNUNET_NO == try_cache (al->hostname,
835 al->record_type, 838 al->record_type,
836 al->client_request_id, 839 al->client_request_id,
837 al->client)) 840 al->client))
838 /* cache failed, tell client we could not get an answer */ 841 /* cache failed, tell client we could not get an answer */
839 { 842 {
840 send_end_msg(al->client_request_id, al->client); 843 send_end_msg (al->client_request_id, al->client);
841 } 844 }
842 free_active_lookup(al); 845 free_active_lookup (al);
843 GNUNET_DNSPARSER_free_packet(parsed); 846 GNUNET_DNSPARSER_free_packet (parsed);
844} 847}
845 848
846 849
@@ -851,14 +854,14 @@ handle_resolve_result(void *cls,
851 * @param cls a `struct ActiveLookup` 854 * @param cls a `struct ActiveLookup`
852 */ 855 */
853static void 856static void
854handle_resolve_timeout(void *cls) 857handle_resolve_timeout (void *cls)
855{ 858{
856 struct ActiveLookup *al = cls; 859 struct ActiveLookup *al = cls;
857 860
858 al->timeout_task = NULL; 861 al->timeout_task = NULL;
859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n"); 862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n");
860 send_end_msg(al->client_request_id, al->client); 863 send_end_msg (al->client_request_id, al->client);
861 free_active_lookup(al); 864 free_active_lookup (al);
862} 865}
863 866
864 867
@@ -873,10 +876,10 @@ handle_resolve_timeout(void *cls)
873 * @return #GNUNET_OK if the DNS query is now pending 876 * @return #GNUNET_OK if the DNS query is now pending
874 */ 877 */
875static int 878static int
876resolve_and_cache(const char *hostname, 879resolve_and_cache (const char *hostname,
877 uint16_t record_type, 880 uint16_t record_type,
878 uint32_t client_request_id, 881 uint32_t client_request_id,
879 struct GNUNET_SERVICE_Client *client) 882 struct GNUNET_SERVICE_Client *client)
880{ 883{
881 char *packet_buf; 884 char *packet_buf;
882 size_t packet_size; 885 size_t packet_size;
@@ -884,42 +887,42 @@ resolve_and_cache(const char *hostname,
884 uint16_t dns_id; 887 uint16_t dns_id;
885 uint16_t type; 888 uint16_t type;
886 889
887 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname); 890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname);
888 dns_id = (uint16_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 891 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
889 UINT16_MAX); 892 UINT16_MAX);
890 893
891 if (GNUNET_DNSPARSER_TYPE_ALL == record_type) 894 if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
892 type = GNUNET_DNSPARSER_TYPE_A; 895 type = GNUNET_DNSPARSER_TYPE_A;
893 else 896 else
894 type = record_type; 897 type = record_type;
895 if (GNUNET_OK != pack(hostname, type, dns_id, &packet_buf, &packet_size)) 898 if (GNUNET_OK != pack (hostname, type, dns_id, &packet_buf, &packet_size))
896 { 899 {
897 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
898 "Failed to pack query for hostname `%s'\n", 901 "Failed to pack query for hostname `%s'\n",
899 hostname); 902 hostname);
900 return GNUNET_SYSERR; 903 return GNUNET_SYSERR;
901 } 904 }
902 905
903 al = GNUNET_new(struct ActiveLookup); 906 al = GNUNET_new (struct ActiveLookup);
904 al->hostname = GNUNET_strdup(hostname); 907 al->hostname = GNUNET_strdup (hostname);
905 al->record_type = record_type; 908 al->record_type = record_type;
906 al->client_request_id = client_request_id; 909 al->client_request_id = client_request_id;
907 al->dns_id = dns_id; 910 al->dns_id = dns_id;
908 al->client = client; 911 al->client = client;
909 al->timeout_task = 912 al->timeout_task =
910 GNUNET_SCHEDULER_add_delayed(DNS_TIMEOUT, &handle_resolve_timeout, al); 913 GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT, &handle_resolve_timeout, al);
911 al->resolve_handle = GNUNET_DNSSTUB_resolve(dnsstub_ctx, 914 al->resolve_handle = GNUNET_DNSSTUB_resolve (dnsstub_ctx,
912 packet_buf, 915 packet_buf,
913 packet_size, 916 packet_size,
914 &handle_resolve_result, 917 &handle_resolve_result,
915 al); 918 al);
916 GNUNET_free(packet_buf); 919 GNUNET_free (packet_buf);
917 GNUNET_CONTAINER_DLL_insert(lookup_head, lookup_tail, al); 920 GNUNET_CONTAINER_DLL_insert (lookup_head, lookup_tail, al);
918 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919 "Resolving %s, client_request_id = %u, dns_id = %u\n", 922 "Resolving %s, client_request_id = %u, dns_id = %u\n",
920 hostname, 923 hostname,
921 (unsigned int)client_request_id, 924 (unsigned int) client_request_id,
922 (unsigned int)dns_id); 925 (unsigned int) dns_id);
923 return GNUNET_OK; 926 return GNUNET_OK;
924} 927}
925 928
@@ -934,38 +937,38 @@ resolve_and_cache(const char *hostname,
934 * @param client who should get the result? 937 * @param client who should get the result?
935 */ 938 */
936static void 939static void
937process_get(const char *hostname, 940process_get (const char *hostname,
938 uint16_t record_type, 941 uint16_t record_type,
939 uint32_t client_request_id, 942 uint32_t client_request_id,
940 struct GNUNET_SERVICE_Client *client) 943 struct GNUNET_SERVICE_Client *client)
941{ 944{
942 char fqdn[255]; 945 char fqdn[255];
943 946
944 if (GNUNET_NO != try_cache(hostname, record_type, client_request_id, client)) 947 if (GNUNET_NO != try_cache (hostname, record_type, client_request_id, client))
945 return; 948 return;
946 if ((NULL != my_domain) && (NULL == strchr(hostname, (unsigned char)'.')) && 949 if ((NULL != my_domain) && (NULL == strchr (hostname, (unsigned char) '.')) &&
947 (strlen(hostname) + strlen(my_domain) <= 253)) 950 (strlen (hostname) + strlen (my_domain) <= 253))
948 { 951 {
949 GNUNET_snprintf(fqdn, sizeof(fqdn), "%s.%s", hostname, my_domain); 952 GNUNET_snprintf (fqdn, sizeof(fqdn), "%s.%s", hostname, my_domain);
950 } 953 }
951 else if (strlen(hostname) < 255) 954 else if (strlen (hostname) < 255)
952 { 955 {
953 GNUNET_snprintf(fqdn, sizeof(fqdn), "%s", hostname); 956 GNUNET_snprintf (fqdn, sizeof(fqdn), "%s", hostname);
954 } 957 }
955 else 958 else
959 {
960 GNUNET_break (0);
961 GNUNET_SERVICE_client_drop (client);
962 return;
963 }
964 if (GNUNET_NO == try_cache (fqdn, record_type, client_request_id, client))
965 {
966 if (GNUNET_OK !=
967 resolve_and_cache (fqdn, record_type, client_request_id, client))
956 { 968 {
957 GNUNET_break(0); 969 send_end_msg (client_request_id, client);
958 GNUNET_SERVICE_client_drop(client);
959 return;
960 }
961 if (GNUNET_NO == try_cache(fqdn, record_type, client_request_id, client))
962 {
963 if (GNUNET_OK !=
964 resolve_and_cache(fqdn, record_type, client_request_id, client))
965 {
966 send_end_msg(client_request_id, client);
967 }
968 } 970 }
971 }
969} 972}
970 973
971 974
@@ -977,43 +980,43 @@ process_get(const char *hostname,
977 * @return #GNUNET_OK if @a get is well-formed 980 * @return #GNUNET_OK if @a get is well-formed
978 */ 981 */
979static int 982static int
980check_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *get) 983check_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
981{ 984{
982 uint16_t size; 985 uint16_t size;
983 int direction; 986 int direction;
984 int af; 987 int af;
985 988
986 (void)cls; 989 (void) cls;
987 size = ntohs(get->header.size) - sizeof(*get); 990 size = ntohs (get->header.size) - sizeof(*get);
988 direction = ntohl(get->direction); 991 direction = ntohl (get->direction);
989 if (GNUNET_NO == direction) 992 if (GNUNET_NO == direction)
990 { 993 {
991 GNUNET_MQ_check_zero_termination(get); 994 GNUNET_MQ_check_zero_termination (get);
992 return GNUNET_OK; 995 return GNUNET_OK;
993 } 996 }
994 af = ntohl(get->af); 997 af = ntohl (get->af);
995 switch (af) 998 switch (af)
999 {
1000 case AF_INET:
1001 if (size != sizeof(struct in_addr))
996 { 1002 {
997 case AF_INET: 1003 GNUNET_break (0);
998 if (size != sizeof(struct in_addr)) 1004 return GNUNET_SYSERR;
999 { 1005 }
1000 GNUNET_break(0); 1006 break;
1001 return GNUNET_SYSERR;
1002 }
1003 break;
1004
1005 case AF_INET6:
1006 if (size != sizeof(struct in6_addr))
1007 {
1008 GNUNET_break(0);
1009 return GNUNET_SYSERR;
1010 }
1011 break;
1012 1007
1013 default: 1008 case AF_INET6:
1014 GNUNET_break(0); 1009 if (size != sizeof(struct in6_addr))
1010 {
1011 GNUNET_break (0);
1015 return GNUNET_SYSERR; 1012 return GNUNET_SYSERR;
1016 } 1013 }
1014 break;
1015
1016 default:
1017 GNUNET_break (0);
1018 return GNUNET_SYSERR;
1019 }
1017 return GNUNET_OK; 1020 return GNUNET_OK;
1018} 1021}
1019 1022
@@ -1025,7 +1028,7 @@ check_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
1025 * @param msg the actual message 1028 * @param msg the actual message
1026 */ 1029 */
1027static void 1030static void
1028handle_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *msg) 1031handle_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
1029{ 1032{
1030 struct GNUNET_SERVICE_Client *client = cls; 1033 struct GNUNET_SERVICE_Client *client = cls;
1031 int direction; 1034 int direction;
@@ -1033,59 +1036,59 @@ handle_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
1033 uint32_t client_request_id; 1036 uint32_t client_request_id;
1034 char *hostname; 1037 char *hostname;
1035 1038
1036 direction = ntohl(msg->direction); 1039 direction = ntohl (msg->direction);
1037 af = ntohl(msg->af); 1040 af = ntohl (msg->af);
1038 client_request_id = msg->client_id; 1041 client_request_id = msg->client_id;
1039 GNUNET_SERVICE_client_continue(client); 1042 GNUNET_SERVICE_client_continue (client);
1040 if (GNUNET_NO == direction) 1043 if (GNUNET_NO == direction)
1044 {
1045 /* IP from hostname */
1046 hostname = GNUNET_strdup ((const char *) &msg[1]);
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Client asks to resolve `%s'\n",
1049 hostname);
1050 switch (af)
1041 { 1051 {
1042 /* IP from hostname */ 1052 case AF_UNSPEC: {
1043 hostname = GNUNET_strdup((const char *)&msg[1]); 1053 process_get (hostname,
1044 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1054 GNUNET_DNSPARSER_TYPE_ALL,
1045 "Client asks to resolve `%s'\n", 1055 client_request_id,
1046 hostname); 1056 client);
1047 switch (af) 1057 break;
1048 { 1058 }
1049 case AF_UNSPEC: { 1059
1050 process_get(hostname, 1060 case AF_INET: {
1051 GNUNET_DNSPARSER_TYPE_ALL, 1061 process_get (hostname,
1052 client_request_id, 1062 GNUNET_DNSPARSER_TYPE_A,
1053 client); 1063 client_request_id,
1054 break; 1064 client);
1055 } 1065 break;
1056 1066 }
1057 case AF_INET: { 1067
1058 process_get(hostname, 1068 case AF_INET6: {
1059 GNUNET_DNSPARSER_TYPE_A, 1069 process_get (hostname,
1060 client_request_id, 1070 GNUNET_DNSPARSER_TYPE_AAAA,
1061 client); 1071 client_request_id,
1062 break; 1072 client);
1063 } 1073 break;
1064 1074 }
1065 case AF_INET6: { 1075
1066 process_get(hostname, 1076 default: {
1067 GNUNET_DNSPARSER_TYPE_AAAA, 1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1068 client_request_id, 1078 GNUNET_assert (0);
1069 client); 1079 }
1070 break;
1071 }
1072
1073 default: {
1074 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1075 GNUNET_assert(0);
1076 }
1077 }
1078 } 1080 }
1081 }
1079 else 1082 else
1080 { 1083 {
1081 /* hostname from IP */ 1084 /* hostname from IP */
1082 hostname = make_reverse_hostname(&msg[1], af); 1085 hostname = make_reverse_hostname (&msg[1], af);
1083 process_get(hostname, 1086 process_get (hostname,
1084 GNUNET_DNSPARSER_TYPE_PTR, 1087 GNUNET_DNSPARSER_TYPE_PTR,
1085 client_request_id, 1088 client_request_id,
1086 client); 1089 client);
1087 } 1090 }
1088 GNUNET_free_non_null(hostname); 1091 GNUNET_free_non_null (hostname);
1089} 1092}
1090 1093
1091 1094
@@ -1095,18 +1098,18 @@ handle_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
1095 * @param cls NULL, unused 1098 * @param cls NULL, unused
1096 */ 1099 */
1097static void 1100static void
1098shutdown_task(void *cls) 1101shutdown_task (void *cls)
1099{ 1102{
1100 (void)cls; 1103 (void) cls;
1101 1104
1102 while (NULL != lookup_head) 1105 while (NULL != lookup_head)
1103 free_active_lookup(lookup_head); 1106 free_active_lookup (lookup_head);
1104 while (NULL != cache_head) 1107 while (NULL != cache_head)
1105 free_cache_entry(cache_head); 1108 free_cache_entry (cache_head);
1106 while (NULL != hosts_head) 1109 while (NULL != hosts_head)
1107 free_hosts_entry(hosts_head); 1110 free_hosts_entry (hosts_head);
1108 GNUNET_DNSSTUB_stop(dnsstub_ctx); 1111 GNUNET_DNSSTUB_stop (dnsstub_ctx);
1109 GNUNET_free_non_null(my_domain); 1112 GNUNET_free_non_null (my_domain);
1110} 1113}
1111 1114
1112 1115
@@ -1120,28 +1123,28 @@ shutdown_task(void *cls)
1120 * @param data_size number of bytes in @a data 1123 * @param data_size number of bytes in @a data
1121 */ 1124 */
1122static void 1125static void
1123add_host(const char *hostname, 1126add_host (const char *hostname,
1124 uint16_t rec_type, 1127 uint16_t rec_type,
1125 const void *data, 1128 const void *data,
1126 size_t data_size) 1129 size_t data_size)
1127{ 1130{
1128 struct ResolveCache *rc; 1131 struct ResolveCache *rc;
1129 struct RecordListEntry *rle; 1132 struct RecordListEntry *rle;
1130 struct GNUNET_DNSPARSER_Record *rec; 1133 struct GNUNET_DNSPARSER_Record *rec;
1131 1134
1132 rec = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record)); 1135 rec = GNUNET_malloc (sizeof(struct GNUNET_DNSPARSER_Record));
1133 rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS; 1136 rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
1134 rec->type = rec_type; 1137 rec->type = rec_type;
1135 rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET; 1138 rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1136 rec->name = GNUNET_strdup(hostname); 1139 rec->name = GNUNET_strdup (hostname);
1137 rec->data.raw.data = GNUNET_memdup(data, data_size); 1140 rec->data.raw.data = GNUNET_memdup (data, data_size);
1138 rec->data.raw.data_len = data_size; 1141 rec->data.raw.data_len = data_size;
1139 rle = GNUNET_new(struct RecordListEntry); 1142 rle = GNUNET_new (struct RecordListEntry);
1140 rle->record = rec; 1143 rle->record = rec;
1141 rc = GNUNET_new(struct ResolveCache); 1144 rc = GNUNET_new (struct ResolveCache);
1142 rc->hostname = GNUNET_strdup(hostname); 1145 rc->hostname = GNUNET_strdup (hostname);
1143 GNUNET_CONTAINER_DLL_insert(rc->records_head, rc->records_tail, rle); 1146 GNUNET_CONTAINER_DLL_insert (rc->records_head, rc->records_tail, rle);
1144 GNUNET_CONTAINER_DLL_insert(hosts_head, hosts_tail, rc); 1147 GNUNET_CONTAINER_DLL_insert (hosts_head, hosts_tail, rc);
1145} 1148}
1146 1149
1147 1150
@@ -1152,7 +1155,7 @@ add_host(const char *hostname,
1152 * @param line_len number of bytes in @a line 1155 * @param line_len number of bytes in @a line
1153 */ 1156 */
1154static void 1157static void
1155extract_hosts(const char *line, size_t line_len) 1158extract_hosts (const char *line, size_t line_len)
1156{ 1159{
1157 const char *c; 1160 const char *c;
1158 struct in_addr v4; 1161 struct in_addr v4;
@@ -1161,33 +1164,33 @@ extract_hosts(const char *line, size_t line_len)
1161 char *tok; 1164 char *tok;
1162 1165
1163 /* ignore everything after '#' */ 1166 /* ignore everything after '#' */
1164 c = memrchr(line, (unsigned char)'#', line_len); 1167 c = memrchr (line, (unsigned char) '#', line_len);
1165 if (NULL != c) 1168 if (NULL != c)
1166 line_len = c - line; 1169 line_len = c - line;
1167 /* ignore leading whitespace */ 1170 /* ignore leading whitespace */
1168 while ((0 < line_len) && isspace((unsigned char)*line)) 1171 while ((0 < line_len) && isspace ((unsigned char) *line))
1169 { 1172 {
1170 line++; 1173 line++;
1171 line_len--; 1174 line_len--;
1172 } 1175 }
1173 tbuf = GNUNET_strndup(line, line_len); 1176 tbuf = GNUNET_strndup (line, line_len);
1174 tok = strtok(tbuf, " \t"); 1177 tok = strtok (tbuf, " \t");
1175 if (NULL == tok) 1178 if (NULL == tok)
1176 { 1179 {
1177 GNUNET_free(tbuf); 1180 GNUNET_free (tbuf);
1178 return; 1181 return;
1179 } 1182 }
1180 if (1 == inet_pton(AF_INET, tok, &v4)) 1183 if (1 == inet_pton (AF_INET, tok, &v4))
1181 { 1184 {
1182 while (NULL != (tok = strtok(NULL, " \t"))) 1185 while (NULL != (tok = strtok (NULL, " \t")))
1183 add_host(tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof(struct in_addr)); 1186 add_host (tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof(struct in_addr));
1184 } 1187 }
1185 else if (1 == inet_pton(AF_INET6, tok, &v6)) 1188 else if (1 == inet_pton (AF_INET6, tok, &v6))
1186 { 1189 {
1187 while (NULL != (tok = strtok(NULL, " \t"))) 1190 while (NULL != (tok = strtok (NULL, " \t")))
1188 add_host(tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof(struct in6_addr)); 1191 add_host (tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof(struct in6_addr));
1189 } 1192 }
1190 GNUNET_free(tbuf); 1193 GNUNET_free (tbuf);
1191} 1194}
1192 1195
1193 1196
@@ -1195,7 +1198,7 @@ extract_hosts(const char *line, size_t line_len)
1195 * Reads the list of hosts from /etc/hosts. 1198 * Reads the list of hosts from /etc/hosts.
1196 */ 1199 */
1197static void 1200static void
1198load_etc_hosts(void) 1201load_etc_hosts (void)
1199{ 1202{
1200 struct GNUNET_DISK_FileHandle *fh; 1203 struct GNUNET_DISK_FileHandle *fh;
1201 struct GNUNET_DISK_MapHandle *mh; 1204 struct GNUNET_DISK_MapHandle *mh;
@@ -1203,49 +1206,49 @@ load_etc_hosts(void)
1203 const char *buf; 1206 const char *buf;
1204 size_t read_offset; 1207 size_t read_offset;
1205 1208
1206 fh = GNUNET_DISK_file_open("/etc/hosts", 1209 fh = GNUNET_DISK_file_open ("/etc/hosts",
1207 GNUNET_DISK_OPEN_READ, 1210 GNUNET_DISK_OPEN_READ,
1208 GNUNET_DISK_PERM_NONE); 1211 GNUNET_DISK_PERM_NONE);
1209 if (NULL == fh) 1212 if (NULL == fh)
1210 { 1213 {
1211 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts"); 1214 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts");
1212 return; 1215 return;
1213 } 1216 }
1214 if (GNUNET_OK != GNUNET_DISK_file_handle_size(fh, &bytes_read)) 1217 if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
1215 { 1218 {
1216 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1217 "Could not determin size of /etc/hosts. " 1220 "Could not determin size of /etc/hosts. "
1218 "DNS resolution will not be possible.\n"); 1221 "DNS resolution will not be possible.\n");
1219 GNUNET_DISK_file_close(fh); 1222 GNUNET_DISK_file_close (fh);
1220 return; 1223 return;
1221 } 1224 }
1222 if (((unsigned long long)bytes_read) > (unsigned long long)SIZE_MAX) 1225 if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
1223 { 1226 {
1224 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1225 "/etc/hosts file too large to mmap. " 1228 "/etc/hosts file too large to mmap. "
1226 "DNS resolution will not be possible.\n"); 1229 "DNS resolution will not be possible.\n");
1227 GNUNET_DISK_file_close(fh); 1230 GNUNET_DISK_file_close (fh);
1228 return; 1231 return;
1229 } 1232 }
1230 buf = GNUNET_DISK_file_map(fh, 1233 buf = GNUNET_DISK_file_map (fh,
1231 &mh, 1234 &mh,
1232 GNUNET_DISK_MAP_TYPE_READ, 1235 GNUNET_DISK_MAP_TYPE_READ,
1233 (size_t)bytes_read); 1236 (size_t) bytes_read);
1234 read_offset = 0; 1237 read_offset = 0;
1235 while (read_offset < (size_t)bytes_read) 1238 while (read_offset < (size_t) bytes_read)
1236 { 1239 {
1237 const char *newline; 1240 const char *newline;
1238 size_t line_len; 1241 size_t line_len;
1239 1242
1240 newline = strchr(buf + read_offset, '\n'); 1243 newline = strchr (buf + read_offset, '\n');
1241 if (NULL == newline) 1244 if (NULL == newline)
1242 break; 1245 break;
1243 line_len = newline - buf - read_offset; 1246 line_len = newline - buf - read_offset;
1244 extract_hosts(buf + read_offset, line_len); 1247 extract_hosts (buf + read_offset, line_len);
1245 read_offset += line_len + 1; 1248 read_offset += line_len + 1;
1246 } 1249 }
1247 GNUNET_DISK_file_unmap(mh); 1250 GNUNET_DISK_file_unmap (mh);
1248 GNUNET_DISK_file_close(fh); 1251 GNUNET_DISK_file_close (fh);
1249} 1252}
1250 1253
1251 1254
@@ -1257,37 +1260,37 @@ load_etc_hosts(void)
1257 * @param sh service handle 1260 * @param sh service handle
1258 */ 1261 */
1259static void 1262static void
1260init_cb(void *cls, 1263init_cb (void *cls,
1261 const struct GNUNET_CONFIGURATION_Handle *cfg, 1264 const struct GNUNET_CONFIGURATION_Handle *cfg,
1262 struct GNUNET_SERVICE_Handle *sh) 1265 struct GNUNET_SERVICE_Handle *sh)
1263{ 1266{
1264 char **dns_servers; 1267 char **dns_servers;
1265 int num_dns_servers; 1268 int num_dns_servers;
1266 1269
1267 (void)cfg; 1270 (void) cfg;
1268 (void)sh; 1271 (void) sh;
1269 load_etc_hosts(); 1272 load_etc_hosts ();
1270 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, cls); 1273 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, cls);
1271 dnsstub_ctx = GNUNET_DNSSTUB_start(128); 1274 dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1272 dns_servers = NULL; 1275 dns_servers = NULL;
1273 num_dns_servers = lookup_dns_servers(&dns_servers); 1276 num_dns_servers = lookup_dns_servers (&dns_servers);
1274 if (0 >= num_dns_servers) 1277 if (0 >= num_dns_servers)
1275 { 1278 {
1276 GNUNET_log( 1279 GNUNET_log (
1277 GNUNET_ERROR_TYPE_ERROR, 1280 GNUNET_ERROR_TYPE_ERROR,
1278 _("No DNS server available. DNS resolution will not be possible.\n")); 1281 _ ("No DNS server available. DNS resolution will not be possible.\n"));
1279 return; 1282 return;
1280 } 1283 }
1281 for (int i = 0; i < num_dns_servers; i++) 1284 for (int i = 0; i < num_dns_servers; i++)
1282 { 1285 {
1283 int result = GNUNET_DNSSTUB_add_dns_ip(dnsstub_ctx, dns_servers[i]); 1286 int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Adding DNS server '%s': %s\n", 1288 "Adding DNS server '%s': %s\n",
1286 dns_servers[i], 1289 dns_servers[i],
1287 GNUNET_OK == result ? "success" : "failure"); 1290 GNUNET_OK == result ? "success" : "failure");
1288 GNUNET_free(dns_servers[i]); 1291 GNUNET_free (dns_servers[i]);
1289 } 1292 }
1290 GNUNET_free_non_null(dns_servers); 1293 GNUNET_free_non_null (dns_servers);
1291} 1294}
1292 1295
1293 1296
@@ -1300,12 +1303,12 @@ init_cb(void *cls,
1300 * @return @a c 1303 * @return @a c
1301 */ 1304 */
1302static void * 1305static void *
1303connect_cb(void *cls, 1306connect_cb (void *cls,
1304 struct GNUNET_SERVICE_Client *c, 1307 struct GNUNET_SERVICE_Client *c,
1305 struct GNUNET_MQ_Handle *mq) 1308 struct GNUNET_MQ_Handle *mq)
1306{ 1309{
1307 (void)cls; 1310 (void) cls;
1308 (void)mq; 1311 (void) mq;
1309 1312
1310 return c; 1313 return c;
1311} 1314}
@@ -1319,38 +1322,38 @@ connect_cb(void *cls,
1319 * @param internal_cls should be equal to @a c 1322 * @param internal_cls should be equal to @a c
1320 */ 1323 */
1321static void 1324static void
1322disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls) 1325disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
1323{ 1326{
1324 struct ActiveLookup *n; 1327 struct ActiveLookup *n;
1325 1328
1326 (void)cls; 1329 (void) cls;
1327 1330
1328 GNUNET_assert(c == internal_cls); 1331 GNUNET_assert (c == internal_cls);
1329 n = lookup_head; 1332 n = lookup_head;
1330 for (struct ActiveLookup *al = n; NULL != al; al = n) 1333 for (struct ActiveLookup *al = n; NULL != al; al = n)
1331 { 1334 {
1332 n = al->next; 1335 n = al->next;
1333 if (al->client == c) 1336 if (al->client == c)
1334 free_active_lookup(al); 1337 free_active_lookup (al);
1335 } 1338 }
1336} 1339}
1337 1340
1338 1341
1339/** 1342/**
1340 * Define "main" method using service macro. 1343 * Define "main" method using service macro.
1341 */ 1344 */
1342GNUNET_SERVICE_MAIN( 1345GNUNET_SERVICE_MAIN (
1343 "resolver", 1346 "resolver",
1344 GNUNET_SERVICE_OPTION_NONE, 1347 GNUNET_SERVICE_OPTION_NONE,
1345 &init_cb, 1348 &init_cb,
1346 &connect_cb, 1349 &connect_cb,
1347 &disconnect_cb, 1350 &disconnect_cb,
1348 NULL, 1351 NULL,
1349 GNUNET_MQ_hd_var_size(get, 1352 GNUNET_MQ_hd_var_size (get,
1350 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, 1353 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1351 struct GNUNET_RESOLVER_GetMessage, 1354 struct GNUNET_RESOLVER_GetMessage,
1352 NULL), 1355 NULL),
1353 GNUNET_MQ_handler_end()); 1356 GNUNET_MQ_handler_end ());
1354 1357
1355 1358
1356#if defined(LINUX) && defined(__GLIBC__) 1359#if defined(LINUX) && defined(__GLIBC__)
@@ -1359,11 +1362,11 @@ GNUNET_SERVICE_MAIN(
1359/** 1362/**
1360 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 1363 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1361 */ 1364 */
1362void __attribute__ ((constructor)) GNUNET_RESOLVER_memory_init() 1365void __attribute__ ((constructor)) GNUNET_RESOLVER_memory_init ()
1363{ 1366{
1364 mallopt(M_TRIM_THRESHOLD, 4 * 1024); 1367 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1365 mallopt(M_TOP_PAD, 1 * 1024); 1368 mallopt (M_TOP_PAD, 1 * 1024);
1366 malloc_trim(0); 1369 malloc_trim (0);
1367} 1370}
1368#endif 1371#endif
1369 1372
diff --git a/src/util/gnunet-timeout.c b/src/util/gnunet-timeout.c
index 2b1754a5e..cd6707db8 100644
--- a/src/util/gnunet-timeout.c
+++ b/src/util/gnunet-timeout.c
@@ -35,85 +35,85 @@ static pid_t child;
35 35
36 36
37static void 37static void
38sigchld_handler(int val) 38sigchld_handler (int val)
39{ 39{
40 int status = 0; 40 int status = 0;
41 int ret = 0; 41 int ret = 0;
42 42
43 (void)val; 43 (void) val;
44 waitpid(child, &status, 0); 44 waitpid (child, &status, 0);
45 if (WIFEXITED(status) != 0) 45 if (WIFEXITED (status) != 0)
46 { 46 {
47 ret = WEXITSTATUS(status); 47 ret = WEXITSTATUS (status);
48 fprintf(stderr, "Process exited with result %u\n", ret); 48 fprintf (stderr, "Process exited with result %u\n", ret);
49 _exit(ret); /* return same status code */ 49 _exit (ret); /* return same status code */
50 } 50 }
51 if (WIFSIGNALED(status) != 0) 51 if (WIFSIGNALED (status) != 0)
52 { 52 {
53 ret = WTERMSIG(status); 53 ret = WTERMSIG (status);
54 fprintf(stderr, "Process received signal %u\n", ret); 54 fprintf (stderr, "Process received signal %u\n", ret);
55 kill(getpid(), ret); /* kill self with the same signal */ 55 kill (getpid (), ret); /* kill self with the same signal */
56 } 56 }
57 _exit(-1); 57 _exit (-1);
58} 58}
59 59
60 60
61static void 61static void
62sigint_handler(int val) 62sigint_handler (int val)
63{ 63{
64 kill(0, val); 64 kill (0, val);
65 _exit(val); 65 _exit (val);
66} 66}
67 67
68 68
69int 69int
70main(int argc, char *argv[]) 70main (int argc, char *argv[])
71{ 71{
72 int timeout = 0; 72 int timeout = 0;
73 pid_t gpid = 0; 73 pid_t gpid = 0;
74 74
75 if (argc < 3) 75 if (argc < 3)
76 { 76 {
77 fprintf(stderr, 77 fprintf (stderr,
78 "arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n"); 78 "arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");
79 exit(-1); 79 exit (-1);
80 } 80 }
81 81
82 timeout = atoi(argv[1]); 82 timeout = atoi (argv[1]);
83 83
84 if (timeout == 0) 84 if (timeout == 0)
85 timeout = 600; 85 timeout = 600;
86 86
87 /* with getpgid() it does not compile, but getpgrp is the BSD version and working */ 87 /* with getpgid() it does not compile, but getpgrp is the BSD version and working */
88 gpid = getpgrp(); 88 gpid = getpgrp ();
89 89
90 signal(SIGCHLD, sigchld_handler); 90 signal (SIGCHLD, sigchld_handler);
91 signal(SIGABRT, sigint_handler); 91 signal (SIGABRT, sigint_handler);
92 signal(SIGFPE, sigint_handler); 92 signal (SIGFPE, sigint_handler);
93 signal(SIGILL, sigint_handler); 93 signal (SIGILL, sigint_handler);
94 signal(SIGINT, sigint_handler); 94 signal (SIGINT, sigint_handler);
95 signal(SIGSEGV, sigint_handler); 95 signal (SIGSEGV, sigint_handler);
96 signal(SIGTERM, sigint_handler); 96 signal (SIGTERM, sigint_handler);
97 97
98 child = fork(); 98 child = fork ();
99 if (child == 0) 99 if (child == 0)
100 { 100 {
101 /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */ 101 /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */
102 //setpgrp (0, pid_t gpid); 102 // setpgrp (0, pid_t gpid);
103 if (-1 != gpid) 103 if (-1 != gpid)
104 setpgid(0, gpid); 104 setpgid (0, gpid);
105 execvp(argv[2], &argv[2]); 105 execvp (argv[2], &argv[2]);
106 exit(-1); 106 exit (-1);
107 } 107 }
108 if (child > 0) 108 if (child > 0)
109 { 109 {
110 sleep(timeout); 110 sleep (timeout);
111 printf("Child processes were killed after timeout of %u seconds\n", 111 printf ("Child processes were killed after timeout of %u seconds\n",
112 timeout); 112 timeout);
113 kill(0, SIGTERM); 113 kill (0, SIGTERM);
114 exit(3); 114 exit (3);
115 } 115 }
116 exit(-1); 116 exit (-1);
117} 117}
118 118
119/* end of timeout_watchdog.c */ 119/* end of timeout_watchdog.c */
diff --git a/src/util/gnunet-uri.c b/src/util/gnunet-uri.c
index 75667b30f..a80ac0997 100644
--- a/src/util/gnunet-uri.c
+++ b/src/util/gnunet-uri.c
@@ -49,15 +49,15 @@ static struct GNUNET_DISK_PipeHandle *sigpipe;
49 * @param cls closure, NULL 49 * @param cls closure, NULL
50 */ 50 */
51static void 51static void
52maint_child_death(void *cls) 52maint_child_death (void *cls)
53{ 53{
54 enum GNUNET_OS_ProcessStatusType type; 54 enum GNUNET_OS_ProcessStatusType type;
55 55
56 (void)cls; 56 (void) cls;
57 if ((GNUNET_OK != GNUNET_OS_process_status(p, &type, &exit_code)) || 57 if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) ||
58 (type != GNUNET_OS_PROCESS_EXITED)) 58 (type != GNUNET_OS_PROCESS_EXITED))
59 GNUNET_break(0 == GNUNET_OS_process_kill(p, GNUNET_TERM_SIG)); 59 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
60 GNUNET_OS_process_destroy(p); 60 GNUNET_OS_process_destroy (p);
61} 61}
62 62
63 63
@@ -70,10 +70,10 @@ maint_child_death(void *cls)
70 * @param cfg configuration 70 * @param cfg configuration
71 */ 71 */
72static void 72static void
73run(void *cls, 73run (void *cls,
74 char *const *args, 74 char *const *args,
75 const char *cfgfile, 75 const char *cfgfile,
76 const struct GNUNET_CONFIGURATION_Handle *cfg) 76 const struct GNUNET_CONFIGURATION_Handle *cfg)
77{ 77{
78 const char *uri; 78 const char *uri;
79 const char *slash; 79 const char *slash;
@@ -81,52 +81,52 @@ run(void *cls,
81 char *program; 81 char *program;
82 struct GNUNET_SCHEDULER_Task *rt; 82 struct GNUNET_SCHEDULER_Task *rt;
83 83
84 (void)cls; 84 (void) cls;
85 (void)cfgfile; 85 (void) cfgfile;
86 if (NULL == (uri = args[0])) 86 if (NULL == (uri = args[0]))
87 { 87 {
88 fprintf(stderr, _("No URI specified on command line\n")); 88 fprintf (stderr, _ ("No URI specified on command line\n"));
89 return; 89 return;
90 } 90 }
91 if (0 != strncasecmp("gnunet://", uri, strlen("gnunet://"))) 91 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
92 { 92 {
93 fprintf(stderr, 93 fprintf (stderr,
94 _("Invalid URI: does not start with `%s'\n"), 94 _ ("Invalid URI: does not start with `%s'\n"),
95 "gnunet://"); 95 "gnunet://");
96 return; 96 return;
97 } 97 }
98 uri += strlen("gnunet://"); 98 uri += strlen ("gnunet://");
99 if (NULL == (slash = strchr(uri, '/'))) 99 if (NULL == (slash = strchr (uri, '/')))
100 { 100 {
101 fprintf(stderr, _("Invalid URI: fails to specify subsystem\n")); 101 fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
102 return; 102 return;
103 } 103 }
104 subsystem = GNUNET_strndup(uri, slash - uri); 104 subsystem = GNUNET_strndup (uri, slash - uri);
105 if (GNUNET_OK != 105 if (GNUNET_OK !=
106 GNUNET_CONFIGURATION_get_value_string(cfg, "uri", subsystem, &program)) 106 GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
107 { 107 {
108 fprintf(stderr, _("No handler known for subsystem `%s'\n"), subsystem); 108 fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
109 GNUNET_free(subsystem); 109 GNUNET_free (subsystem);
110 return; 110 return;
111 } 111 }
112 GNUNET_free(subsystem); 112 GNUNET_free (subsystem);
113 rt = GNUNET_SCHEDULER_add_read_file( 113 rt = GNUNET_SCHEDULER_add_read_file (
114 GNUNET_TIME_UNIT_FOREVER_REL, 114 GNUNET_TIME_UNIT_FOREVER_REL,
115 GNUNET_DISK_pipe_handle(sigpipe, GNUNET_DISK_PIPE_END_READ), 115 GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
116 &maint_child_death, 116 &maint_child_death,
117 NULL); 117 NULL);
118 p = GNUNET_OS_start_process(GNUNET_NO, 118 p = GNUNET_OS_start_process (GNUNET_NO,
119 0, 119 0,
120 NULL, 120 NULL,
121 NULL, 121 NULL,
122 NULL, 122 NULL,
123 program, 123 program,
124 program, 124 program,
125 args[0], 125 args[0],
126 NULL); 126 NULL);
127 GNUNET_free(program); 127 GNUNET_free (program);
128 if (NULL == p) 128 if (NULL == p)
129 GNUNET_SCHEDULER_cancel(rt); 129 GNUNET_SCHEDULER_cancel (rt);
130} 130}
131 131
132 132
@@ -135,17 +135,17 @@ run(void *cls,
135 * respective handler by writing to the trigger pipe. 135 * respective handler by writing to the trigger pipe.
136 */ 136 */
137static void 137static void
138sighandler_child_death() 138sighandler_child_death ()
139{ 139{
140 static char c; 140 static char c;
141 int old_errno = errno; /* back-up errno */ 141 int old_errno = errno; /* back-up errno */
142 142
143 GNUNET_break( 143 GNUNET_break (
144 1 == 144 1 ==
145 GNUNET_DISK_file_write(GNUNET_DISK_pipe_handle(sigpipe, 145 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
146 GNUNET_DISK_PIPE_END_WRITE), 146 GNUNET_DISK_PIPE_END_WRITE),
147 &c, 147 &c,
148 sizeof(c))); 148 sizeof(c)));
149 errno = old_errno; /* restore errno */ 149 errno = old_errno; /* restore errno */
150} 150}
151 151
@@ -158,7 +158,7 @@ sighandler_child_death()
158 * @return 0 ok, 1 on error 158 * @return 0 ok, 1 on error
159 */ 159 */
160int 160int
161main(int argc, char *const *argv) 161main (int argc, char *const *argv)
162{ 162{
163 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 163 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
164 GNUNET_GETOPT_OPTION_END 164 GNUNET_GETOPT_OPTION_END
@@ -166,25 +166,25 @@ main(int argc, char *const *argv)
166 struct GNUNET_SIGNAL_Context *shc_chld; 166 struct GNUNET_SIGNAL_Context *shc_chld;
167 int ret; 167 int ret;
168 168
169 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 169 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
170 return 2; 170 return 2;
171 sigpipe = GNUNET_DISK_pipe(GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO); 171 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
172 GNUNET_assert(sigpipe != NULL); 172 GNUNET_assert (sigpipe != NULL);
173 shc_chld = 173 shc_chld =
174 GNUNET_SIGNAL_handler_install(GNUNET_SIGCHLD, &sighandler_child_death); 174 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
175 ret = GNUNET_PROGRAM_run(argc, 175 ret = GNUNET_PROGRAM_run (argc,
176 argv, 176 argv,
177 "gnunet-uri URI", 177 "gnunet-uri URI",
178 gettext_noop( 178 gettext_noop (
179 "Perform default-actions for GNUnet URIs"), 179 "Perform default-actions for GNUnet URIs"),
180 options, 180 options,
181 &run, 181 &run,
182 NULL); 182 NULL);
183 GNUNET_SIGNAL_handler_uninstall(shc_chld); 183 GNUNET_SIGNAL_handler_uninstall (shc_chld);
184 shc_chld = NULL; 184 shc_chld = NULL;
185 GNUNET_DISK_pipe_close(sigpipe); 185 GNUNET_DISK_pipe_close (sigpipe);
186 sigpipe = NULL; 186 sigpipe = NULL;
187 GNUNET_free((void *)argv); 187 GNUNET_free ((void *) argv);
188 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1; 188 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
189} 189}
190 190
diff --git a/src/util/helper.c b/src/util/helper.c
index f0892a485..9ebbdb188 100644
--- a/src/util/helper.c
+++ b/src/util/helper.c
@@ -33,7 +33,8 @@
33/** 33/**
34 * Entry in the queue of messages we need to transmit to the helper. 34 * Entry in the queue of messages we need to transmit to the helper.
35 */ 35 */
36struct GNUNET_HELPER_SendHandle { 36struct GNUNET_HELPER_SendHandle
37{
37 /** 38 /**
38 * This is an entry in a DLL. 39 * This is an entry in a DLL.
39 */ 40 */
@@ -74,7 +75,8 @@ struct GNUNET_HELPER_SendHandle {
74/** 75/**
75 * The handle to a helper process. 76 * The handle to a helper process.
76 */ 77 */
77struct GNUNET_HELPER_Handle { 78struct GNUNET_HELPER_Handle
79{
78 /** 80 /**
79 * PipeHandle to receive data from the helper 81 * PipeHandle to receive data from the helper
80 */ 82 */
@@ -172,40 +174,40 @@ struct GNUNET_HELPER_Handle {
172 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error 174 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
173 */ 175 */
174int 176int
175GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill) 177GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill)
176{ 178{
177 struct GNUNET_HELPER_SendHandle *sh; 179 struct GNUNET_HELPER_SendHandle *sh;
178 int ret; 180 int ret;
179 181
180 while (NULL != (sh = h->sh_head)) 182 while (NULL != (sh = h->sh_head))
181 { 183 {
182 GNUNET_CONTAINER_DLL_remove(h->sh_head, h->sh_tail, sh); 184 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
183 if (NULL != sh->cont) 185 if (NULL != sh->cont)
184 sh->cont(sh->cont_cls, GNUNET_NO); 186 sh->cont (sh->cont_cls, GNUNET_NO);
185 GNUNET_free(sh); 187 GNUNET_free (sh);
186 } 188 }
187 if (NULL != h->restart_task) 189 if (NULL != h->restart_task)
188 { 190 {
189 GNUNET_SCHEDULER_cancel(h->restart_task); 191 GNUNET_SCHEDULER_cancel (h->restart_task);
190 h->restart_task = NULL; 192 h->restart_task = NULL;
191 } 193 }
192 if (NULL != h->read_task) 194 if (NULL != h->read_task)
193 { 195 {
194 GNUNET_SCHEDULER_cancel(h->read_task); 196 GNUNET_SCHEDULER_cancel (h->read_task);
195 h->read_task = NULL; 197 h->read_task = NULL;
196 } 198 }
197 if (NULL == h->helper_proc) 199 if (NULL == h->helper_proc)
198 return GNUNET_SYSERR; 200 return GNUNET_SYSERR;
199 if (GNUNET_YES == soft_kill) 201 if (GNUNET_YES == soft_kill)
200 { 202 {
201 /* soft-kill only possible with pipes */ 203 /* soft-kill only possible with pipes */
202 GNUNET_assert(NULL != h->helper_in); 204 GNUNET_assert (NULL != h->helper_in);
203 ret = GNUNET_DISK_pipe_close(h->helper_in); 205 ret = GNUNET_DISK_pipe_close (h->helper_in);
204 h->helper_in = NULL; 206 h->helper_in = NULL;
205 h->fh_to_helper = NULL; 207 h->fh_to_helper = NULL;
206 return ret; 208 return ret;
207 } 209 }
208 if (0 != GNUNET_OS_process_kill(h->helper_proc, GNUNET_TERM_SIG)) 210 if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
209 return GNUNET_SYSERR; 211 return GNUNET_SYSERR;
210 return GNUNET_OK; 212 return GNUNET_OK;
211} 213}
@@ -220,50 +222,50 @@ GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill)
220 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error 222 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
221 */ 223 */
222int 224int
223GNUNET_HELPER_wait(struct GNUNET_HELPER_Handle *h) 225GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
224{ 226{
225 struct GNUNET_HELPER_SendHandle *sh; 227 struct GNUNET_HELPER_SendHandle *sh;
226 int ret; 228 int ret;
227 229
228 ret = GNUNET_SYSERR; 230 ret = GNUNET_SYSERR;
229 if (NULL != h->helper_proc) 231 if (NULL != h->helper_proc)
230 { 232 {
231 ret = GNUNET_OS_process_wait(h->helper_proc); 233 ret = GNUNET_OS_process_wait (h->helper_proc);
232 GNUNET_OS_process_destroy(h->helper_proc); 234 GNUNET_OS_process_destroy (h->helper_proc);
233 h->helper_proc = NULL; 235 h->helper_proc = NULL;
234 } 236 }
235 if (NULL != h->read_task) 237 if (NULL != h->read_task)
236 { 238 {
237 GNUNET_SCHEDULER_cancel(h->read_task); 239 GNUNET_SCHEDULER_cancel (h->read_task);
238 h->read_task = NULL; 240 h->read_task = NULL;
239 } 241 }
240 if (NULL != h->write_task) 242 if (NULL != h->write_task)
241 { 243 {
242 GNUNET_SCHEDULER_cancel(h->write_task); 244 GNUNET_SCHEDULER_cancel (h->write_task);
243 h->write_task = NULL; 245 h->write_task = NULL;
244 } 246 }
245 if (NULL != h->helper_in) 247 if (NULL != h->helper_in)
246 { 248 {
247 GNUNET_DISK_pipe_close(h->helper_in); 249 GNUNET_DISK_pipe_close (h->helper_in);
248 h->helper_in = NULL; 250 h->helper_in = NULL;
249 h->fh_to_helper = NULL; 251 h->fh_to_helper = NULL;
250 } 252 }
251 if (NULL != h->helper_out) 253 if (NULL != h->helper_out)
252 { 254 {
253 GNUNET_DISK_pipe_close(h->helper_out); 255 GNUNET_DISK_pipe_close (h->helper_out);
254 h->helper_out = NULL; 256 h->helper_out = NULL;
255 h->fh_from_helper = NULL; 257 h->fh_from_helper = NULL;
256 } 258 }
257 while (NULL != (sh = h->sh_head)) 259 while (NULL != (sh = h->sh_head))
258 { 260 {
259 GNUNET_CONTAINER_DLL_remove(h->sh_head, h->sh_tail, sh); 261 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
260 if (NULL != sh->cont) 262 if (NULL != sh->cont)
261 sh->cont(sh->cont_cls, GNUNET_NO); 263 sh->cont (sh->cont_cls, GNUNET_NO);
262 GNUNET_free(sh); 264 GNUNET_free (sh);
263 } 265 }
264 /* purge MST buffer */ 266 /* purge MST buffer */
265 if (NULL != h->mst) 267 if (NULL != h->mst)
266 (void)GNUNET_MST_from_buffer(h->mst, NULL, 0, GNUNET_YES, GNUNET_NO); 268 (void) GNUNET_MST_from_buffer (h->mst, NULL, 0, GNUNET_YES, GNUNET_NO);
267 return ret; 269 return ret;
268} 270}
269 271
@@ -276,18 +278,18 @@ GNUNET_HELPER_wait(struct GNUNET_HELPER_Handle *h)
276 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper 278 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
277 */ 279 */
278static void 280static void
279stop_helper(struct GNUNET_HELPER_Handle *h, int soft_kill) 281stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
280{ 282{
281 if (NULL != h->restart_task) 283 if (NULL != h->restart_task)
282 { 284 {
283 GNUNET_SCHEDULER_cancel(h->restart_task); 285 GNUNET_SCHEDULER_cancel (h->restart_task);
284 h->restart_task = NULL; 286 h->restart_task = NULL;
285 } 287 }
286 else 288 else
287 { 289 {
288 GNUNET_break(GNUNET_OK == GNUNET_HELPER_kill(h, soft_kill)); 290 GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
289 GNUNET_break(GNUNET_OK == GNUNET_HELPER_wait(h)); 291 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
290 } 292 }
291} 293}
292 294
293 295
@@ -297,7 +299,7 @@ stop_helper(struct GNUNET_HELPER_Handle *h, int soft_kill)
297 * @param cls handle to the helper process 299 * @param cls handle to the helper process
298 */ 300 */
299static void 301static void
300restart_task(void *cls); 302restart_task (void *cls);
301 303
302 304
303/** 305/**
@@ -306,87 +308,87 @@ restart_task(void *cls);
306 * @param cls handle to the helper process 308 * @param cls handle to the helper process
307 */ 309 */
308static void 310static void
309helper_read(void *cls) 311helper_read (void *cls)
310{ 312{
311 struct GNUNET_HELPER_Handle *h = cls; 313 struct GNUNET_HELPER_Handle *h = cls;
312 char buf[GNUNET_MAX_MESSAGE_SIZE] GNUNET_ALIGN; 314 char buf[GNUNET_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
313 ssize_t t; 315 ssize_t t;
314 316
315 h->read_task = NULL; 317 h->read_task = NULL;
316 t = GNUNET_DISK_file_read(h->fh_from_helper, &buf, sizeof(buf)); 318 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof(buf));
317 if (t < 0) 319 if (t < 0)
320 {
321 /* On read-error, restart the helper */
322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
323 _ ("Error reading from `%s': %s\n"),
324 h->binary_name,
325 strerror (errno));
326 if (NULL != h->exp_cb)
318 { 327 {
319 /* On read-error, restart the helper */ 328 h->exp_cb (h->cb_cls);
320 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 329 GNUNET_HELPER_stop (h, GNUNET_NO);
321 _("Error reading from `%s': %s\n"),
322 h->binary_name,
323 strerror(errno));
324 if (NULL != h->exp_cb)
325 {
326 h->exp_cb(h->cb_cls);
327 GNUNET_HELPER_stop(h, GNUNET_NO);
328 return;
329 }
330 stop_helper(h, GNUNET_NO);
331 /* Restart the helper */
332 h->restart_task = GNUNET_SCHEDULER_add_delayed(
333 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
334 h->retry_back_off),
335 &restart_task,
336 h);
337 return; 330 return;
338 } 331 }
332 stop_helper (h, GNUNET_NO);
333 /* Restart the helper */
334 h->restart_task = GNUNET_SCHEDULER_add_delayed (
335 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
336 h->retry_back_off),
337 &restart_task,
338 h);
339 return;
340 }
339 if (0 == t) 341 if (0 == t)
342 {
343 /* this happens if the helper is shut down via a
344 signal, so it is not a "hard" error */
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Got 0 bytes from helper `%s' (EOF)\n",
347 h->binary_name);
348 if (NULL != h->exp_cb)
340 { 349 {
341 /* this happens if the helper is shut down via a 350 h->exp_cb (h->cb_cls);
342 signal, so it is not a "hard" error */ 351 GNUNET_HELPER_stop (h, GNUNET_NO);
343 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
344 "Got 0 bytes from helper `%s' (EOF)\n",
345 h->binary_name);
346 if (NULL != h->exp_cb)
347 {
348 h->exp_cb(h->cb_cls);
349 GNUNET_HELPER_stop(h, GNUNET_NO);
350 return;
351 }
352 stop_helper(h, GNUNET_NO);
353 /* Restart the helper */
354 h->restart_task = GNUNET_SCHEDULER_add_delayed(
355 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
356 h->retry_back_off),
357 &restart_task,
358 h);
359 return; 352 return;
360 } 353 }
361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 354 stop_helper (h, GNUNET_NO);
362 "Got %u bytes from helper `%s'\n", 355 /* Restart the helper */
363 (unsigned int)t, 356 h->restart_task = GNUNET_SCHEDULER_add_delayed (
364 h->binary_name); 357 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
365 h->read_task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 358 h->retry_back_off),
366 h->fh_from_helper, 359 &restart_task,
367 &helper_read, 360 h);
368 h); 361 return;
362 }
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "Got %u bytes from helper `%s'\n",
365 (unsigned int) t,
366 h->binary_name);
367 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
368 h->fh_from_helper,
369 &helper_read,
370 h);
369 if (GNUNET_SYSERR == 371 if (GNUNET_SYSERR ==
370 GNUNET_MST_from_buffer(h->mst, buf, t, GNUNET_NO, GNUNET_NO)) 372 GNUNET_MST_from_buffer (h->mst, buf, t, GNUNET_NO, GNUNET_NO))
373 {
374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
375 _ ("Failed to parse inbound message from helper `%s'\n"),
376 h->binary_name);
377 if (NULL != h->exp_cb)
371 { 378 {
372 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 379 h->exp_cb (h->cb_cls);
373 _("Failed to parse inbound message from helper `%s'\n"), 380 GNUNET_HELPER_stop (h, GNUNET_NO);
374 h->binary_name);
375 if (NULL != h->exp_cb)
376 {
377 h->exp_cb(h->cb_cls);
378 GNUNET_HELPER_stop(h, GNUNET_NO);
379 return;
380 }
381 stop_helper(h, GNUNET_NO);
382 /* Restart the helper */
383 h->restart_task = GNUNET_SCHEDULER_add_delayed(
384 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
385 h->retry_back_off),
386 &restart_task,
387 h);
388 return; 381 return;
389 } 382 }
383 stop_helper (h, GNUNET_NO);
384 /* Restart the helper */
385 h->restart_task = GNUNET_SCHEDULER_add_delayed (
386 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
387 h->retry_back_off),
388 &restart_task,
389 h);
390 return;
391 }
390} 392}
391 393
392 394
@@ -396,55 +398,55 @@ helper_read(void *cls)
396 * @param h handle to the helper process 398 * @param h handle to the helper process
397 */ 399 */
398static void 400static void
399start_helper(struct GNUNET_HELPER_Handle *h) 401start_helper (struct GNUNET_HELPER_Handle *h)
400{ 402{
401 h->helper_in = 403 h->helper_in =
402 GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 404 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
403 h->helper_out = 405 h->helper_out =
404 GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 406 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
405 if ((h->helper_in == NULL) || (h->helper_out == NULL)) 407 if ((h->helper_in == NULL) || (h->helper_out == NULL))
406 { 408 {
407 /* out of file descriptors? try again later... */ 409 /* out of file descriptors? try again later... */
408 stop_helper(h, GNUNET_NO); 410 stop_helper (h, GNUNET_NO);
409 h->restart_task = GNUNET_SCHEDULER_add_delayed( 411 h->restart_task = GNUNET_SCHEDULER_add_delayed (
410 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 412 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
411 h->retry_back_off), 413 h->retry_back_off),
412 &restart_task, 414 &restart_task,
413 h); 415 h);
414 return; 416 return;
415 } 417 }
416 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Starting HELPER process `%s'\n", 419 "Starting HELPER process `%s'\n",
418 h->binary_name); 420 h->binary_name);
419 h->fh_from_helper = 421 h->fh_from_helper =
420 GNUNET_DISK_pipe_handle(h->helper_out, GNUNET_DISK_PIPE_END_READ); 422 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
421 h->fh_to_helper = 423 h->fh_to_helper =
422 GNUNET_DISK_pipe_handle(h->helper_in, GNUNET_DISK_PIPE_END_WRITE); 424 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
423 h->helper_proc = GNUNET_OS_start_process_vap(h->with_control_pipe, 425 h->helper_proc = GNUNET_OS_start_process_vap (h->with_control_pipe,
424 GNUNET_OS_INHERIT_STD_ERR, 426 GNUNET_OS_INHERIT_STD_ERR,
425 h->helper_in, 427 h->helper_in,
426 h->helper_out, 428 h->helper_out,
427 NULL, 429 NULL,
428 h->binary_name, 430 h->binary_name,
429 h->binary_argv); 431 h->binary_argv);
430 if (NULL == h->helper_proc) 432 if (NULL == h->helper_proc)
431 { 433 {
432 /* failed to start process? try again later... */ 434 /* failed to start process? try again later... */
433 stop_helper(h, GNUNET_NO); 435 stop_helper (h, GNUNET_NO);
434 h->restart_task = GNUNET_SCHEDULER_add_delayed( 436 h->restart_task = GNUNET_SCHEDULER_add_delayed (
435 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 437 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
436 h->retry_back_off), 438 h->retry_back_off),
437 &restart_task, 439 &restart_task,
438 h); 440 h);
439 return; 441 return;
440 } 442 }
441 GNUNET_DISK_pipe_close_end(h->helper_out, GNUNET_DISK_PIPE_END_WRITE); 443 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
442 GNUNET_DISK_pipe_close_end(h->helper_in, GNUNET_DISK_PIPE_END_READ); 444 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
443 if (NULL != h->mst) 445 if (NULL != h->mst)
444 h->read_task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 446 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
445 h->fh_from_helper, 447 h->fh_from_helper,
446 &helper_read, 448 &helper_read,
447 h); 449 h);
448} 450}
449 451
450 452
@@ -454,16 +456,16 @@ start_helper(struct GNUNET_HELPER_Handle *h)
454 * @param cls handle to the helper process 456 * @param cls handle to the helper process
455 */ 457 */
456static void 458static void
457restart_task(void *cls) 459restart_task (void *cls)
458{ 460{
459 struct GNUNET_HELPER_Handle *h = cls; 461 struct GNUNET_HELPER_Handle *h = cls;
460 462
461 h->restart_task = NULL; 463 h->restart_task = NULL;
462 h->retry_back_off++; 464 h->retry_back_off++;
463 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 465 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
464 "Restarting helper with back-off %u\n", 466 "Restarting helper with back-off %u\n",
465 h->retry_back_off); 467 h->retry_back_off);
466 start_helper(h); 468 start_helper (h);
467} 469}
468 470
469 471
@@ -484,35 +486,35 @@ restart_task(void *cls)
484 * @return the new Handle, NULL on error 486 * @return the new Handle, NULL on error
485 */ 487 */
486struct GNUNET_HELPER_Handle * 488struct GNUNET_HELPER_Handle *
487GNUNET_HELPER_start(int with_control_pipe, 489GNUNET_HELPER_start (int with_control_pipe,
488 const char *binary_name, 490 const char *binary_name,
489 char *const binary_argv[], 491 char *const binary_argv[],
490 GNUNET_MessageTokenizerCallback cb, 492 GNUNET_MessageTokenizerCallback cb,
491 GNUNET_HELPER_ExceptionCallback exp_cb, 493 GNUNET_HELPER_ExceptionCallback exp_cb,
492 void *cb_cls) 494 void *cb_cls)
493{ 495{
494 struct GNUNET_HELPER_Handle *h; 496 struct GNUNET_HELPER_Handle *h;
495 unsigned int c; 497 unsigned int c;
496 498
497 h = GNUNET_new(struct GNUNET_HELPER_Handle); 499 h = GNUNET_new (struct GNUNET_HELPER_Handle);
498 h->with_control_pipe = with_control_pipe; 500 h->with_control_pipe = with_control_pipe;
499 /* Lookup in libexec path only if we are starting gnunet helpers */ 501 /* Lookup in libexec path only if we are starting gnunet helpers */
500 if (NULL != strstr(binary_name, "gnunet")) 502 if (NULL != strstr (binary_name, "gnunet"))
501 h->binary_name = GNUNET_OS_get_libexec_binary_path(binary_name); 503 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
502 else 504 else
503 h->binary_name = GNUNET_strdup(binary_name); 505 h->binary_name = GNUNET_strdup (binary_name);
504 for (c = 0; NULL != binary_argv[c]; c++) 506 for (c = 0; NULL != binary_argv[c]; c++)
505 ; 507 ;
506 h->binary_argv = GNUNET_malloc(sizeof(char *) * (c + 1)); 508 h->binary_argv = GNUNET_malloc (sizeof(char *) * (c + 1));
507 for (c = 0; NULL != binary_argv[c]; c++) 509 for (c = 0; NULL != binary_argv[c]; c++)
508 h->binary_argv[c] = GNUNET_strdup(binary_argv[c]); 510 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
509 h->binary_argv[c] = NULL; 511 h->binary_argv[c] = NULL;
510 h->cb_cls = cb_cls; 512 h->cb_cls = cb_cls;
511 if (NULL != cb) 513 if (NULL != cb)
512 h->mst = GNUNET_MST_create(cb, h->cb_cls); 514 h->mst = GNUNET_MST_create (cb, h->cb_cls);
513 h->exp_cb = exp_cb; 515 h->exp_cb = exp_cb;
514 h->retry_back_off = 0; 516 h->retry_back_off = 0;
515 start_helper(h); 517 start_helper (h);
516 return h; 518 return h;
517} 519}
518 520
@@ -523,32 +525,32 @@ GNUNET_HELPER_start(int with_control_pipe,
523 * @param h the helper handle to free 525 * @param h the helper handle to free
524 */ 526 */
525void 527void
526GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h) 528GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
527{ 529{
528 unsigned int c; 530 unsigned int c;
529 struct GNUNET_HELPER_SendHandle *sh; 531 struct GNUNET_HELPER_SendHandle *sh;
530 532
531 if (NULL != h->write_task) 533 if (NULL != h->write_task)
532 { 534 {
533 GNUNET_SCHEDULER_cancel(h->write_task); 535 GNUNET_SCHEDULER_cancel (h->write_task);
534 h->write_task = NULL; 536 h->write_task = NULL;
535 } 537 }
536 GNUNET_assert(NULL == h->read_task); 538 GNUNET_assert (NULL == h->read_task);
537 GNUNET_assert(NULL == h->restart_task); 539 GNUNET_assert (NULL == h->restart_task);
538 while (NULL != (sh = h->sh_head)) 540 while (NULL != (sh = h->sh_head))
539 { 541 {
540 GNUNET_CONTAINER_DLL_remove(h->sh_head, h->sh_tail, sh); 542 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
541 if (NULL != sh->cont) 543 if (NULL != sh->cont)
542 sh->cont(sh->cont_cls, GNUNET_SYSERR); 544 sh->cont (sh->cont_cls, GNUNET_SYSERR);
543 GNUNET_free(sh); 545 GNUNET_free (sh);
544 } 546 }
545 if (NULL != h->mst) 547 if (NULL != h->mst)
546 GNUNET_MST_destroy(h->mst); 548 GNUNET_MST_destroy (h->mst);
547 GNUNET_free(h->binary_name); 549 GNUNET_free (h->binary_name);
548 for (c = 0; h->binary_argv[c] != NULL; c++) 550 for (c = 0; h->binary_argv[c] != NULL; c++)
549 GNUNET_free(h->binary_argv[c]); 551 GNUNET_free (h->binary_argv[c]);
550 GNUNET_free(h->binary_argv); 552 GNUNET_free (h->binary_argv);
551 GNUNET_free(h); 553 GNUNET_free (h);
552} 554}
553 555
554 556
@@ -560,11 +562,11 @@ GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
560 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper 562 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
561 */ 563 */
562void 564void
563GNUNET_HELPER_stop(struct GNUNET_HELPER_Handle *h, int soft_kill) 565GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill)
564{ 566{
565 h->exp_cb = NULL; 567 h->exp_cb = NULL;
566 stop_helper(h, soft_kill); 568 stop_helper (h, soft_kill);
567 GNUNET_HELPER_destroy(h); 569 GNUNET_HELPER_destroy (h);
568} 570}
569 571
570 572
@@ -574,7 +576,7 @@ GNUNET_HELPER_stop(struct GNUNET_HELPER_Handle *h, int soft_kill)
574 * @param cls handle to the helper process 576 * @param cls handle to the helper process
575 */ 577 */
576static void 578static void
577helper_write(void *cls) 579helper_write (void *cls)
578{ 580{
579 struct GNUNET_HELPER_Handle *h = cls; 581 struct GNUNET_HELPER_Handle *h = cls;
580 struct GNUNET_HELPER_SendHandle *sh; 582 struct GNUNET_HELPER_SendHandle *sh;
@@ -583,56 +585,56 @@ helper_write(void *cls)
583 585
584 h->write_task = NULL; 586 h->write_task = NULL;
585 if (NULL == (sh = h->sh_head)) 587 if (NULL == (sh = h->sh_head))
586 { 588 {
587 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n"); 589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
588 return; /* how did this happen? */ 590 return; /* how did this happen? */
589 } 591 }
590 buf = (const char *)sh->msg; 592 buf = (const char *) sh->msg;
591 t = GNUNET_DISK_file_write(h->fh_to_helper, 593 t = GNUNET_DISK_file_write (h->fh_to_helper,
592 &buf[sh->wpos], 594 &buf[sh->wpos],
593 ntohs(sh->msg->size) - sh->wpos); 595 ntohs (sh->msg->size) - sh->wpos);
594 if (-1 == t) 596 if (-1 == t)
597 {
598 /* On write-error, restart the helper */
599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600 _ ("Error writing to `%s': %s\n"),
601 h->binary_name,
602 strerror (errno));
603 if (NULL != h->exp_cb)
595 { 604 {
596 /* On write-error, restart the helper */ 605 h->exp_cb (h->cb_cls);
597 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 606 GNUNET_HELPER_stop (h, GNUNET_NO);
598 _("Error writing to `%s': %s\n"),
599 h->binary_name,
600 strerror(errno));
601 if (NULL != h->exp_cb)
602 {
603 h->exp_cb(h->cb_cls);
604 GNUNET_HELPER_stop(h, GNUNET_NO);
605 return;
606 }
607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
608 "Stopping and restarting helper task!\n");
609 stop_helper(h, GNUNET_NO);
610 /* Restart the helper */
611 h->restart_task = GNUNET_SCHEDULER_add_delayed(
612 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
613 h->retry_back_off),
614 &restart_task,
615 h);
616 return; 607 return;
617 } 608 }
618 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Transmitted %u bytes to %s\n", 610 "Stopping and restarting helper task!\n");
620 (unsigned int)t, 611 stop_helper (h, GNUNET_NO);
621 h->binary_name); 612 /* Restart the helper */
613 h->restart_task = GNUNET_SCHEDULER_add_delayed (
614 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
615 h->retry_back_off),
616 &restart_task,
617 h);
618 return;
619 }
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Transmitted %u bytes to %s\n",
622 (unsigned int) t,
623 h->binary_name);
622 sh->wpos += t; 624 sh->wpos += t;
623 if (sh->wpos == ntohs(sh->msg->size)) 625 if (sh->wpos == ntohs (sh->msg->size))
624 { 626 {
625 GNUNET_CONTAINER_DLL_remove(h->sh_head, h->sh_tail, sh); 627 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
626 if (NULL != sh->cont) 628 if (NULL != sh->cont)
627 sh->cont(sh->cont_cls, GNUNET_YES); 629 sh->cont (sh->cont_cls, GNUNET_YES);
628 GNUNET_free(sh); 630 GNUNET_free (sh);
629 } 631 }
630 if (NULL != h->sh_head) 632 if (NULL != h->sh_head)
631 h->write_task = 633 h->write_task =
632 GNUNET_SCHEDULER_add_write_file(GNUNET_TIME_UNIT_FOREVER_REL, 634 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
633 h->fh_to_helper, 635 h->fh_to_helper,
634 &helper_write, 636 &helper_write,
635 h); 637 h);
636} 638}
637 639
638 640
@@ -650,11 +652,11 @@ helper_write(void *cls)
650 * not be abortable) 652 * not be abortable)
651 */ 653 */
652struct GNUNET_HELPER_SendHandle * 654struct GNUNET_HELPER_SendHandle *
653GNUNET_HELPER_send(struct GNUNET_HELPER_Handle *h, 655GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
654 const struct GNUNET_MessageHeader *msg, 656 const struct GNUNET_MessageHeader *msg,
655 int can_drop, 657 int can_drop,
656 GNUNET_HELPER_Continuation cont, 658 GNUNET_HELPER_Continuation cont,
657 void *cont_cls) 659 void *cont_cls)
658{ 660{
659 struct GNUNET_HELPER_SendHandle *sh; 661 struct GNUNET_HELPER_SendHandle *sh;
660 uint16_t mlen; 662 uint16_t mlen;
@@ -663,20 +665,20 @@ GNUNET_HELPER_send(struct GNUNET_HELPER_Handle *h,
663 return NULL; 665 return NULL;
664 if ((GNUNET_YES == can_drop) && (NULL != h->sh_head)) 666 if ((GNUNET_YES == can_drop) && (NULL != h->sh_head))
665 return NULL; 667 return NULL;
666 mlen = ntohs(msg->size); 668 mlen = ntohs (msg->size);
667 sh = GNUNET_malloc(sizeof(struct GNUNET_HELPER_SendHandle) + mlen); 669 sh = GNUNET_malloc (sizeof(struct GNUNET_HELPER_SendHandle) + mlen);
668 sh->msg = (const struct GNUNET_MessageHeader *)&sh[1]; 670 sh->msg = (const struct GNUNET_MessageHeader *) &sh[1];
669 GNUNET_memcpy(&sh[1], msg, mlen); 671 GNUNET_memcpy (&sh[1], msg, mlen);
670 sh->h = h; 672 sh->h = h;
671 sh->cont = cont; 673 sh->cont = cont;
672 sh->cont_cls = cont_cls; 674 sh->cont_cls = cont_cls;
673 GNUNET_CONTAINER_DLL_insert_tail(h->sh_head, h->sh_tail, sh); 675 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head, h->sh_tail, sh);
674 if (NULL == h->write_task) 676 if (NULL == h->write_task)
675 h->write_task = 677 h->write_task =
676 GNUNET_SCHEDULER_add_write_file(GNUNET_TIME_UNIT_FOREVER_REL, 678 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
677 h->fh_to_helper, 679 h->fh_to_helper,
678 &helper_write, 680 &helper_write,
679 h); 681 h);
680 682
681 return sh; 683 return sh;
682} 684}
@@ -689,22 +691,22 @@ GNUNET_HELPER_send(struct GNUNET_HELPER_Handle *h,
689 * @param sh operation to cancel 691 * @param sh operation to cancel
690 */ 692 */
691void 693void
692GNUNET_HELPER_send_cancel(struct GNUNET_HELPER_SendHandle *sh) 694GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
693{ 695{
694 struct GNUNET_HELPER_Handle *h = sh->h; 696 struct GNUNET_HELPER_Handle *h = sh->h;
695 697
696 sh->cont = NULL; 698 sh->cont = NULL;
697 sh->cont_cls = NULL; 699 sh->cont_cls = NULL;
698 if (0 == sh->wpos) 700 if (0 == sh->wpos)
701 {
702 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
703 GNUNET_free (sh);
704 if (NULL == h->sh_head)
699 { 705 {
700 GNUNET_CONTAINER_DLL_remove(h->sh_head, h->sh_tail, sh); 706 GNUNET_SCHEDULER_cancel (h->write_task);
701 GNUNET_free(sh); 707 h->write_task = NULL;
702 if (NULL == h->sh_head)
703 {
704 GNUNET_SCHEDULER_cancel(h->write_task);
705 h->write_task = NULL;
706 }
707 } 708 }
709 }
708} 710}
709 711
710 712
diff --git a/src/util/load.c b/src/util/load.c
index 5c4d9d37b..bab2f375e 100644
--- a/src/util/load.c
+++ b/src/util/load.c
@@ -27,12 +27,13 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-load", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-load", __VA_ARGS__)
31 31
32/** 32/**
33 * Values we track for load calculations. 33 * Values we track for load calculations.
34 */ 34 */
35struct GNUNET_LOAD_Value { 35struct GNUNET_LOAD_Value
36{
36 /** 37 /**
37 * How fast should the load decline if no values are added? 38 * How fast should the load decline if no values are added?
38 */ 39 */
@@ -79,34 +80,35 @@ struct GNUNET_LOAD_Value {
79 80
80 81
81static void 82static void
82internal_update(struct GNUNET_LOAD_Value *load) 83internal_update (struct GNUNET_LOAD_Value *load)
83{ 84{
84 struct GNUNET_TIME_Relative delta; 85 struct GNUNET_TIME_Relative delta;
85 unsigned int n; 86 unsigned int n;
86 87
87 if (load->autodecline.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 88 if (load->autodecline.rel_value_us ==
89 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
88 return; 90 return;
89 delta = GNUNET_TIME_absolute_get_duration(load->last_update); 91 delta = GNUNET_TIME_absolute_get_duration (load->last_update);
90 if (delta.rel_value_us < load->autodecline.rel_value_us) 92 if (delta.rel_value_us < load->autodecline.rel_value_us)
91 return; 93 return;
92 if (0 == load->autodecline.rel_value_us) 94 if (0 == load->autodecline.rel_value_us)
93 { 95 {
94 load->runavg_delay = 0.0; 96 load->runavg_delay = 0.0;
95 load->load = 0; 97 load->load = 0;
96 return; 98 return;
97 } 99 }
98 n = delta.rel_value_us / load->autodecline.rel_value_us; 100 n = delta.rel_value_us / load->autodecline.rel_value_us;
99 if (n > 16) 101 if (n > 16)
100 { 102 {
101 load->runavg_delay = 0.0; 103 load->runavg_delay = 0.0;
102 load->load = 0; 104 load->load = 0;
103 return; 105 return;
104 } 106 }
105 while (n > 0) 107 while (n > 0)
106 { 108 {
107 n--; 109 n--;
108 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; 110 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
109 } 111 }
110} 112}
111 113
112 114
@@ -119,13 +121,13 @@ internal_update(struct GNUNET_LOAD_Value *load)
119 * @return the new load value 121 * @return the new load value
120 */ 122 */
121struct GNUNET_LOAD_Value * 123struct GNUNET_LOAD_Value *
122GNUNET_LOAD_value_init(struct GNUNET_TIME_Relative autodecline) 124GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
123{ 125{
124 struct GNUNET_LOAD_Value *ret; 126 struct GNUNET_LOAD_Value *ret;
125 127
126 ret = GNUNET_new(struct GNUNET_LOAD_Value); 128 ret = GNUNET_new (struct GNUNET_LOAD_Value);
127 ret->autodecline = autodecline; 129 ret->autodecline = autodecline;
128 ret->last_update = GNUNET_TIME_absolute_get(); 130 ret->last_update = GNUNET_TIME_absolute_get ();
129 return ret; 131 return ret;
130} 132}
131 133
@@ -137,10 +139,10 @@ GNUNET_LOAD_value_init(struct GNUNET_TIME_Relative autodecline)
137 * @param autodecline frequency of load decline 139 * @param autodecline frequency of load decline
138 */ 140 */
139void 141void
140GNUNET_LOAD_value_set_decline(struct GNUNET_LOAD_Value *load, 142GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
141 struct GNUNET_TIME_Relative autodecline) 143 struct GNUNET_TIME_Relative autodecline)
142{ 144{
143 internal_update(load); 145 internal_update (load);
144 load->autodecline = autodecline; 146 load->autodecline = autodecline;
145} 147}
146 148
@@ -151,7 +153,7 @@ GNUNET_LOAD_value_set_decline(struct GNUNET_LOAD_Value *load,
151 * @param load load to update 153 * @param load load to update
152 */ 154 */
153static void 155static void
154calculate_load(struct GNUNET_LOAD_Value *load) 156calculate_load (struct GNUNET_LOAD_Value *load)
155{ 157{
156 double stddev; 158 double stddev;
157 double avgdel; 159 double avgdel;
@@ -168,13 +170,13 @@ calculate_load(struct GNUNET_LOAD_Value *load)
168 * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1) 170 * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
169 * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1) 171 * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
170 */ 172 */
171 sum_val_i = (double)load->cummulative_delay; 173 sum_val_i = (double) load->cummulative_delay;
172 n = ((double)load->cummulative_request_count); 174 n = ((double) load->cummulative_request_count);
173 nm1 = n - 1.0; 175 nm1 = n - 1.0;
174 avgdel = sum_val_i / n; 176 avgdel = sum_val_i / n;
175 stddev = 177 stddev =
176 (((double)load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + 178 (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i
177 n * avgdel * avgdel) / nm1; 179 + n * avgdel * avgdel) / nm1;
178 if (stddev <= 0) 180 if (stddev <= 0)
179 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */ 181 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
180 /* now calculate load based on how far out we are from 182 /* now calculate load based on how far out we are from
@@ -196,10 +198,10 @@ calculate_load(struct GNUNET_LOAD_Value *load)
196 * that we could not do proper calculations 198 * that we could not do proper calculations
197 */ 199 */
198double 200double
199GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load) 201GNUNET_LOAD_get_load (struct GNUNET_LOAD_Value *load)
200{ 202{
201 internal_update(load); 203 internal_update (load);
202 calculate_load(load); 204 calculate_load (load);
203 return load->load; 205 return load->load;
204} 206}
205 207
@@ -211,16 +213,16 @@ GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load)
211 * @return zero if update was never called 213 * @return zero if update was never called
212 */ 214 */
213double 215double
214GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load) 216GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load)
215{ 217{
216 double n; 218 double n;
217 double sum_val_i; 219 double sum_val_i;
218 220
219 internal_update(load); 221 internal_update (load);
220 if (load->cummulative_request_count == 0) 222 if (load->cummulative_request_count == 0)
221 return 0.0; 223 return 0.0;
222 n = ((double)load->cummulative_request_count); 224 n = ((double) load->cummulative_request_count);
223 sum_val_i = (double)load->cummulative_delay; 225 sum_val_i = (double) load->cummulative_delay;
224 return sum_val_i / n; 226 return sum_val_i / n;
225} 227}
226 228
@@ -232,19 +234,19 @@ GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load)
232 * @param data latest measurement value (for example, delay) 234 * @param data latest measurement value (for example, delay)
233 */ 235 */
234void 236void
235GNUNET_LOAD_update(struct GNUNET_LOAD_Value *load, uint64_t data) 237GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data)
236{ 238{
237 uint32_t dv; 239 uint32_t dv;
238 240
239 internal_update(load); 241 internal_update (load);
240 load->last_update = GNUNET_TIME_absolute_get(); 242 load->last_update = GNUNET_TIME_absolute_get ();
241 if (data > 64 * 1024) 243 if (data > 64 * 1024)
242 { 244 {
243 /* very large */ 245 /* very large */
244 load->load = 100.0; 246 load->load = 100.0;
245 return; 247 return;
246 } 248 }
247 dv = (uint32_t)data; 249 dv = (uint32_t) data;
248 load->cummulative_delay += dv; 250 load->cummulative_delay += dv;
249 load->cummulative_squared_delay += dv * dv; 251 load->cummulative_squared_delay += dv * dv;
250 load->cummulative_request_count++; 252 load->cummulative_request_count++;
diff --git a/src/util/mq.c b/src/util/mq.c
index 188606fb4..1c1f2893f 100644
--- a/src/util/mq.c
+++ b/src/util/mq.c
@@ -26,10 +26,11 @@
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29#define LOG(kind, ...) GNUNET_log_from(kind, "util-mq", __VA_ARGS__) 29#define LOG(kind, ...) GNUNET_log_from (kind, "util-mq", __VA_ARGS__)
30 30
31 31
32struct GNUNET_MQ_Envelope { 32struct GNUNET_MQ_Envelope
33{
33 /** 34 /**
34 * Messages are stored in a linked list. 35 * Messages are stored in a linked list.
35 * Each queue has its own list of envelopes. 36 * Each queue has its own list of envelopes.
@@ -81,7 +82,8 @@ struct GNUNET_MQ_Envelope {
81/** 82/**
82 * Handle to a message queue. 83 * Handle to a message queue.
83 */ 84 */
84struct GNUNET_MQ_Handle { 85struct GNUNET_MQ_Handle
86{
85 /** 87 /**
86 * Handlers array, or NULL if the queue should not receive messages 88 * Handlers array, or NULL if the queue should not receive messages
87 */ 89 */
@@ -197,17 +199,17 @@ struct GNUNET_MQ_Handle {
197 * @param mh message to dispatch 199 * @param mh message to dispatch
198 */ 200 */
199void 201void
200GNUNET_MQ_inject_message(struct GNUNET_MQ_Handle *mq, 202GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
201 const struct GNUNET_MessageHeader *mh) 203 const struct GNUNET_MessageHeader *mh)
202{ 204{
203 int ret; 205 int ret;
204 206
205 ret = GNUNET_MQ_handle_message(mq->handlers, mh); 207 ret = GNUNET_MQ_handle_message (mq->handlers, mh);
206 if (GNUNET_SYSERR == ret) 208 if (GNUNET_SYSERR == ret)
207 { 209 {
208 GNUNET_MQ_inject_error(mq, GNUNET_MQ_ERROR_MALFORMED); 210 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_MALFORMED);
209 return; 211 return;
210 } 212 }
211} 213}
212 214
213 215
@@ -224,62 +226,62 @@ GNUNET_MQ_inject_message(struct GNUNET_MQ_Handle *mq,
224 * #GNUNET_SYSERR if message was rejected by check function 226 * #GNUNET_SYSERR if message was rejected by check function
225 */ 227 */
226int 228int
227GNUNET_MQ_handle_message(const struct GNUNET_MQ_MessageHandler *handlers, 229GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers,
228 const struct GNUNET_MessageHeader *mh) 230 const struct GNUNET_MessageHeader *mh)
229{ 231{
230 const struct GNUNET_MQ_MessageHandler *handler; 232 const struct GNUNET_MQ_MessageHandler *handler;
231 int handled = GNUNET_NO; 233 int handled = GNUNET_NO;
232 uint16_t msize = ntohs(mh->size); 234 uint16_t msize = ntohs (mh->size);
233 uint16_t mtype = ntohs(mh->type); 235 uint16_t mtype = ntohs (mh->type);
234 236
235 LOG(GNUNET_ERROR_TYPE_DEBUG, 237 LOG (GNUNET_ERROR_TYPE_DEBUG,
236 "Received message of type %u and size %u\n", 238 "Received message of type %u and size %u\n",
237 mtype, 239 mtype,
238 msize); 240 msize);
239 241
240 if (NULL == handlers) 242 if (NULL == handlers)
241 goto done; 243 goto done;
242 for (handler = handlers; NULL != handler->cb; handler++) 244 for (handler = handlers; NULL != handler->cb; handler++)
245 {
246 if (handler->type == mtype)
243 { 247 {
244 if (handler->type == mtype) 248 handled = GNUNET_YES;
245 { 249 if ((handler->expected_size > msize) ||
246 handled = GNUNET_YES; 250 ((handler->expected_size != msize) && (NULL == handler->mv)))
247 if ((handler->expected_size > msize) || 251 {
248 ((handler->expected_size != msize) && (NULL == handler->mv))) 252 /* Too small, or not an exact size and
249 { 253 no 'mv' handler to check rest */
250 /* Too small, or not an exact size and 254 LOG (GNUNET_ERROR_TYPE_ERROR,
251 no 'mv' handler to check rest */ 255 "Received malformed message of type %u\n",
252 LOG(GNUNET_ERROR_TYPE_ERROR, 256 (unsigned int) handler->type);
253 "Received malformed message of type %u\n", 257 return GNUNET_SYSERR;
254 (unsigned int)handler->type); 258 }
255 return GNUNET_SYSERR; 259 if ((NULL == handler->mv) ||
256 } 260 (GNUNET_OK == handler->mv (handler->cls, mh)))
257 if ((NULL == handler->mv) || 261 {
258 (GNUNET_OK == handler->mv(handler->cls, mh))) 262 /* message well-formed, pass to handler */
259 { 263 handler->cb (handler->cls, mh);
260 /* message well-formed, pass to handler */ 264 }
261 handler->cb(handler->cls, mh); 265 else
262 } 266 {
263 else 267 /* Message rejected by check routine */
264 { 268 LOG (GNUNET_ERROR_TYPE_ERROR,
265 /* Message rejected by check routine */ 269 "Received malformed message of type %u\n",
266 LOG(GNUNET_ERROR_TYPE_ERROR, 270 (unsigned int) handler->type);
267 "Received malformed message of type %u\n", 271 return GNUNET_SYSERR;
268 (unsigned int)handler->type); 272 }
269 return GNUNET_SYSERR; 273 break;
270 }
271 break;
272 }
273 } 274 }
275 }
274done: 276done:
275 if (GNUNET_NO == handled) 277 if (GNUNET_NO == handled)
276 { 278 {
277 LOG(GNUNET_ERROR_TYPE_INFO, 279 LOG (GNUNET_ERROR_TYPE_INFO,
278 "No handler for message of type %u and size %u\n", 280 "No handler for message of type %u and size %u\n",
279 mtype, 281 mtype,
280 msize); 282 msize);
281 return GNUNET_NO; 283 return GNUNET_NO;
282 } 284 }
283 return GNUNET_OK; 285 return GNUNET_OK;
284} 286}
285 287
@@ -295,16 +297,16 @@ done:
295 * @param error the error type 297 * @param error the error type
296 */ 298 */
297void 299void
298GNUNET_MQ_inject_error(struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_Error error) 300GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_Error error)
299{ 301{
300 if (NULL == mq->error_handler) 302 if (NULL == mq->error_handler)
301 { 303 {
302 LOG(GNUNET_ERROR_TYPE_WARNING, 304 LOG (GNUNET_ERROR_TYPE_WARNING,
303 "Got error %d, but no handler installed\n", 305 "Got error %d, but no handler installed\n",
304 (int)error); 306 (int) error);
305 return; 307 return;
306 } 308 }
307 mq->error_handler(mq->error_handler_cls, error); 309 mq->error_handler (mq->error_handler_cls, error);
308} 310}
309 311
310 312
@@ -316,10 +318,10 @@ GNUNET_MQ_inject_error(struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_Error error)
316 * @param mqm the message to discard 318 * @param mqm the message to discard
317 */ 319 */
318void 320void
319GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *ev) 321GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
320{ 322{
321 GNUNET_assert(NULL == ev->parent_queue); 323 GNUNET_assert (NULL == ev->parent_queue);
322 GNUNET_free(ev); 324 GNUNET_free (ev);
323} 325}
324 326
325 327
@@ -330,12 +332,12 @@ GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *ev)
330 * @return number of queued, non-transmitted messages 332 * @return number of queued, non-transmitted messages
331 */ 333 */
332unsigned int 334unsigned int
333GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq) 335GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
334{ 336{
335 if (GNUNET_YES != mq->in_flight) 337 if (GNUNET_YES != mq->in_flight)
336 { 338 {
337 return mq->queue_length; 339 return mq->queue_length;
338 } 340 }
339 return mq->queue_length - 1; 341 return mq->queue_length - 1;
340} 342}
341 343
@@ -348,36 +350,36 @@ GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
348 * @param ev the envelope with the message to send. 350 * @param ev the envelope with the message to send.
349 */ 351 */
350void 352void
351GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev) 353GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
352{ 354{
353 GNUNET_assert(NULL != mq); 355 GNUNET_assert (NULL != mq);
354 GNUNET_assert(NULL == ev->parent_queue); 356 GNUNET_assert (NULL == ev->parent_queue);
355 357
356 mq->queue_length++; 358 mq->queue_length++;
357 if (mq->queue_length >= 10000) 359 if (mq->queue_length >= 10000)
358 { 360 {
359 /* This would seem like a bug... */ 361 /* This would seem like a bug... */
360 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
361 "MQ with %u entries extended by message of type %u (FC broken?)\n", 363 "MQ with %u entries extended by message of type %u (FC broken?)\n",
362 (unsigned int)mq->queue_length, 364 (unsigned int) mq->queue_length,
363 (unsigned int)ntohs(ev->mh->type)); 365 (unsigned int) ntohs (ev->mh->type));
364 } 366 }
365 ev->parent_queue = mq; 367 ev->parent_queue = mq;
366 /* is the implementation busy? queue it! */ 368 /* is the implementation busy? queue it! */
367 if ((NULL != mq->current_envelope) || (NULL != mq->send_task)) 369 if ((NULL != mq->current_envelope) || (NULL != mq->send_task))
368 { 370 {
369 GNUNET_CONTAINER_DLL_insert_tail(mq->envelope_head, mq->envelope_tail, ev); 371 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head, mq->envelope_tail, ev);
370 return; 372 return;
371 } 373 }
372 GNUNET_assert(NULL == mq->envelope_head); 374 GNUNET_assert (NULL == mq->envelope_head);
373 mq->current_envelope = ev; 375 mq->current_envelope = ev;
374 376
375 LOG(GNUNET_ERROR_TYPE_DEBUG, 377 LOG (GNUNET_ERROR_TYPE_DEBUG,
376 "sending message of type %u, queue empty (MQ: %p)\n", 378 "sending message of type %u, queue empty (MQ: %p)\n",
377 ntohs(ev->mh->type), 379 ntohs (ev->mh->type),
378 mq); 380 mq);
379 381
380 mq->send_impl(mq, ev->mh, mq->impl_state); 382 mq->send_impl (mq, ev->mh, mq->impl_state);
381} 383}
382 384
383 385
@@ -389,12 +391,12 @@ GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
389 * @return NULL if queue is empty (or has no envelope that is not under transmission) 391 * @return NULL if queue is empty (or has no envelope that is not under transmission)
390 */ 392 */
391struct GNUNET_MQ_Envelope * 393struct GNUNET_MQ_Envelope *
392GNUNET_MQ_unsent_head(struct GNUNET_MQ_Handle *mq) 394GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq)
393{ 395{
394 struct GNUNET_MQ_Envelope *env; 396 struct GNUNET_MQ_Envelope *env;
395 397
396 env = mq->envelope_head; 398 env = mq->envelope_head;
397 GNUNET_CONTAINER_DLL_remove(mq->envelope_head, mq->envelope_tail, env); 399 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, env);
398 mq->queue_length--; 400 mq->queue_length--;
399 env->parent_queue = NULL; 401 env->parent_queue = NULL;
400 return env; 402 return env;
@@ -409,13 +411,13 @@ GNUNET_MQ_unsent_head(struct GNUNET_MQ_Handle *mq)
409 * @return copy of @a env 411 * @return copy of @a env
410 */ 412 */
411struct GNUNET_MQ_Envelope * 413struct GNUNET_MQ_Envelope *
412GNUNET_MQ_env_copy(struct GNUNET_MQ_Envelope *env) 414GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
413{ 415{
414 GNUNET_assert(NULL == env->next); 416 GNUNET_assert (NULL == env->next);
415 GNUNET_assert(NULL == env->parent_queue); 417 GNUNET_assert (NULL == env->parent_queue);
416 GNUNET_assert(NULL == env->sent_cb); 418 GNUNET_assert (NULL == env->sent_cb);
417 GNUNET_assert(GNUNET_NO == env->have_custom_options); 419 GNUNET_assert (GNUNET_NO == env->have_custom_options);
418 return GNUNET_MQ_msg_copy(env->mh); 420 return GNUNET_MQ_msg_copy (env->mh);
419} 421}
420 422
421 423
@@ -427,19 +429,19 @@ GNUNET_MQ_env_copy(struct GNUNET_MQ_Envelope *env)
427 * @param ev the envelope with the message to send. 429 * @param ev the envelope with the message to send.
428 */ 430 */
429void 431void
430GNUNET_MQ_send_copy(struct GNUNET_MQ_Handle *mq, 432GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
431 const struct GNUNET_MQ_Envelope *ev) 433 const struct GNUNET_MQ_Envelope *ev)
432{ 434{
433 struct GNUNET_MQ_Envelope *env; 435 struct GNUNET_MQ_Envelope *env;
434 uint16_t msize; 436 uint16_t msize;
435 437
436 msize = ntohs(ev->mh->size); 438 msize = ntohs (ev->mh->size);
437 env = GNUNET_malloc(sizeof(struct GNUNET_MQ_Envelope) + msize); 439 env = GNUNET_malloc (sizeof(struct GNUNET_MQ_Envelope) + msize);
438 env->mh = (struct GNUNET_MessageHeader *)&env[1]; 440 env->mh = (struct GNUNET_MessageHeader *) &env[1];
439 env->sent_cb = ev->sent_cb; 441 env->sent_cb = ev->sent_cb;
440 env->sent_cls = ev->sent_cls; 442 env->sent_cls = ev->sent_cls;
441 GNUNET_memcpy(&env[1], ev->mh, msize); 443 GNUNET_memcpy (&env[1], ev->mh, msize);
442 GNUNET_MQ_send(mq, env); 444 GNUNET_MQ_send (mq, env);
443} 445}
444 446
445 447
@@ -451,7 +453,7 @@ GNUNET_MQ_send_copy(struct GNUNET_MQ_Handle *mq,
451 * @param cls message queue to send the next message with 453 * @param cls message queue to send the next message with
452 */ 454 */
453static void 455static void
454impl_send_continue(void *cls) 456impl_send_continue (void *cls)
455{ 457{
456 struct GNUNET_MQ_Handle *mq = cls; 458 struct GNUNET_MQ_Handle *mq = cls;
457 459
@@ -461,15 +463,15 @@ impl_send_continue(void *cls)
461 if (NULL == mq->envelope_head) 463 if (NULL == mq->envelope_head)
462 return; 464 return;
463 mq->current_envelope = mq->envelope_head; 465 mq->current_envelope = mq->envelope_head;
464 GNUNET_CONTAINER_DLL_remove(mq->envelope_head, 466 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
465 mq->envelope_tail, 467 mq->envelope_tail,
466 mq->current_envelope); 468 mq->current_envelope);
467 469
468 LOG(GNUNET_ERROR_TYPE_DEBUG, 470 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "sending message of type %u from queue\n", 471 "sending message of type %u from queue\n",
470 ntohs(mq->current_envelope->mh->type)); 472 ntohs (mq->current_envelope->mh->type));
471 473
472 mq->send_impl(mq, mq->current_envelope->mh, mq->impl_state); 474 mq->send_impl (mq, mq->current_envelope->mh, mq->impl_state);
473} 475}
474 476
475 477
@@ -481,25 +483,25 @@ impl_send_continue(void *cls)
481 * @param mq message queue to send the next message with 483 * @param mq message queue to send the next message with
482 */ 484 */
483void 485void
484GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq) 486GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
485{ 487{
486 struct GNUNET_MQ_Envelope *current_envelope; 488 struct GNUNET_MQ_Envelope *current_envelope;
487 GNUNET_SCHEDULER_TaskCallback cb; 489 GNUNET_SCHEDULER_TaskCallback cb;
488 490
489 GNUNET_assert(0 < mq->queue_length); 491 GNUNET_assert (0 < mq->queue_length);
490 mq->queue_length--; 492 mq->queue_length--;
491 mq->in_flight = GNUNET_NO; 493 mq->in_flight = GNUNET_NO;
492 current_envelope = mq->current_envelope; 494 current_envelope = mq->current_envelope;
493 current_envelope->parent_queue = NULL; 495 current_envelope->parent_queue = NULL;
494 mq->current_envelope = NULL; 496 mq->current_envelope = NULL;
495 GNUNET_assert(NULL == mq->send_task); 497 GNUNET_assert (NULL == mq->send_task);
496 mq->send_task = GNUNET_SCHEDULER_add_now(&impl_send_continue, mq); 498 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue, mq);
497 if (NULL != (cb = current_envelope->sent_cb)) 499 if (NULL != (cb = current_envelope->sent_cb))
498 { 500 {
499 current_envelope->sent_cb = NULL; 501 current_envelope->sent_cb = NULL;
500 cb(current_envelope->sent_cls); 502 cb (current_envelope->sent_cls);
501 } 503 }
502 GNUNET_free(current_envelope); 504 GNUNET_free (current_envelope);
503} 505}
504 506
505 507
@@ -514,7 +516,7 @@ GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
514 * @param mq message queue to send the next message with 516 * @param mq message queue to send the next message with
515 */ 517 */
516void 518void
517GNUNET_MQ_impl_send_in_flight(struct GNUNET_MQ_Handle *mq) 519GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
518{ 520{
519 struct GNUNET_MQ_Envelope *current_envelope; 521 struct GNUNET_MQ_Envelope *current_envelope;
520 GNUNET_SCHEDULER_TaskCallback cb; 522 GNUNET_SCHEDULER_TaskCallback cb;
@@ -523,14 +525,14 @@ GNUNET_MQ_impl_send_in_flight(struct GNUNET_MQ_Handle *mq)
523 /* call is only valid if we're actually currently sending 525 /* call is only valid if we're actually currently sending
524 * a message */ 526 * a message */
525 current_envelope = mq->current_envelope; 527 current_envelope = mq->current_envelope;
526 GNUNET_assert(NULL != current_envelope); 528 GNUNET_assert (NULL != current_envelope);
527 /* can't call cancel from now on anymore */ 529 /* can't call cancel from now on anymore */
528 current_envelope->parent_queue = NULL; 530 current_envelope->parent_queue = NULL;
529 if (NULL != (cb = current_envelope->sent_cb)) 531 if (NULL != (cb = current_envelope->sent_cb))
530 { 532 {
531 current_envelope->sent_cb = NULL; 533 current_envelope->sent_cb = NULL;
532 cb(current_envelope->sent_cls); 534 cb (current_envelope->sent_cls);
533 } 535 }
534} 536}
535 537
536 538
@@ -547,21 +549,21 @@ GNUNET_MQ_impl_send_in_flight(struct GNUNET_MQ_Handle *mq)
547 * @return a new message queue 549 * @return a new message queue
548 */ 550 */
549struct GNUNET_MQ_Handle * 551struct GNUNET_MQ_Handle *
550GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, 552GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
551 GNUNET_MQ_DestroyImpl destroy, 553 GNUNET_MQ_DestroyImpl destroy,
552 GNUNET_MQ_CancelImpl cancel, 554 GNUNET_MQ_CancelImpl cancel,
553 void *impl_state, 555 void *impl_state,
554 const struct GNUNET_MQ_MessageHandler *handlers, 556 const struct GNUNET_MQ_MessageHandler *handlers,
555 GNUNET_MQ_ErrorHandler error_handler, 557 GNUNET_MQ_ErrorHandler error_handler,
556 void *error_handler_cls) 558 void *error_handler_cls)
557{ 559{
558 struct GNUNET_MQ_Handle *mq; 560 struct GNUNET_MQ_Handle *mq;
559 561
560 mq = GNUNET_new(struct GNUNET_MQ_Handle); 562 mq = GNUNET_new (struct GNUNET_MQ_Handle);
561 mq->send_impl = send; 563 mq->send_impl = send;
562 mq->destroy_impl = destroy; 564 mq->destroy_impl = destroy;
563 mq->cancel_impl = cancel; 565 mq->cancel_impl = cancel;
564 mq->handlers = GNUNET_MQ_copy_handlers(handlers); 566 mq->handlers = GNUNET_MQ_copy_handlers (handlers);
565 mq->error_handler = error_handler; 567 mq->error_handler = error_handler;
566 mq->error_handler_cls = error_handler_cls; 568 mq->error_handler_cls = error_handler_cls;
567 mq->impl_state = impl_state; 569 mq->impl_state = impl_state;
@@ -578,7 +580,7 @@ GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send,
578 * @param handlers_cls new closure to use 580 * @param handlers_cls new closure to use
579 */ 581 */
580void 582void
581GNUNET_MQ_set_handlers_closure(struct GNUNET_MQ_Handle *mq, void *handlers_cls) 583GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq, void *handlers_cls)
582{ 584{
583 if (NULL == mq->handlers) 585 if (NULL == mq->handlers)
584 return; 586 return;
@@ -597,10 +599,10 @@ GNUNET_MQ_set_handlers_closure(struct GNUNET_MQ_Handle *mq, void *handlers_cls)
597 * @return message to send, never NULL 599 * @return message to send, never NULL
598 */ 600 */
599const struct GNUNET_MessageHeader * 601const struct GNUNET_MessageHeader *
600GNUNET_MQ_impl_current(struct GNUNET_MQ_Handle *mq) 602GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
601{ 603{
602 GNUNET_assert(NULL != mq->current_envelope); 604 GNUNET_assert (NULL != mq->current_envelope);
603 GNUNET_assert(NULL != mq->current_envelope->mh); 605 GNUNET_assert (NULL != mq->current_envelope->mh);
604 return mq->current_envelope->mh; 606 return mq->current_envelope->mh;
605} 607}
606 608
@@ -620,21 +622,21 @@ GNUNET_MQ_impl_current(struct GNUNET_MQ_Handle *mq)
620 * @return message to send, never NULL 622 * @return message to send, never NULL
621 */ 623 */
622void * 624void *
623GNUNET_MQ_impl_state(struct GNUNET_MQ_Handle *mq) 625GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
624{ 626{
625 return mq->impl_state; 627 return mq->impl_state;
626} 628}
627 629
628 630
629struct GNUNET_MQ_Envelope * 631struct GNUNET_MQ_Envelope *
630GNUNET_MQ_msg_(struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type) 632GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
631{ 633{
632 struct GNUNET_MQ_Envelope *ev; 634 struct GNUNET_MQ_Envelope *ev;
633 635
634 ev = GNUNET_malloc(size + sizeof(struct GNUNET_MQ_Envelope)); 636 ev = GNUNET_malloc (size + sizeof(struct GNUNET_MQ_Envelope));
635 ev->mh = (struct GNUNET_MessageHeader *)&ev[1]; 637 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
636 ev->mh->size = htons(size); 638 ev->mh->size = htons (size);
637 ev->mh->type = htons(type); 639 ev->mh->type = htons (type);
638 if (NULL != mhp) 640 if (NULL != mhp)
639 *mhp = ev->mh; 641 *mhp = ev->mh;
640 return ev; 642 return ev;
@@ -648,14 +650,14 @@ GNUNET_MQ_msg_(struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
648 * @return envelope containing @a hdr 650 * @return envelope containing @a hdr
649 */ 651 */
650struct GNUNET_MQ_Envelope * 652struct GNUNET_MQ_Envelope *
651GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr) 653GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
652{ 654{
653 struct GNUNET_MQ_Envelope *mqm; 655 struct GNUNET_MQ_Envelope *mqm;
654 uint16_t size = ntohs(hdr->size); 656 uint16_t size = ntohs (hdr->size);
655 657
656 mqm = GNUNET_malloc(sizeof(*mqm) + size); 658 mqm = GNUNET_malloc (sizeof(*mqm) + size);
657 mqm->mh = (struct GNUNET_MessageHeader *)&mqm[1]; 659 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
658 GNUNET_memcpy(mqm->mh, hdr, size); 660 GNUNET_memcpy (mqm->mh, hdr, size);
659 return mqm; 661 return mqm;
660} 662}
661 663
@@ -670,27 +672,27 @@ GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
670 * @param nested_mh the message to append to the message after base_size 672 * @param nested_mh the message to append to the message after base_size
671 */ 673 */
672struct GNUNET_MQ_Envelope * 674struct GNUNET_MQ_Envelope *
673GNUNET_MQ_msg_nested_mh_(struct GNUNET_MessageHeader **mhp, 675GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
674 uint16_t base_size, 676 uint16_t base_size,
675 uint16_t type, 677 uint16_t type,
676 const struct GNUNET_MessageHeader *nested_mh) 678 const struct GNUNET_MessageHeader *nested_mh)
677{ 679{
678 struct GNUNET_MQ_Envelope *mqm; 680 struct GNUNET_MQ_Envelope *mqm;
679 uint16_t size; 681 uint16_t size;
680 682
681 if (NULL == nested_mh) 683 if (NULL == nested_mh)
682 return GNUNET_MQ_msg_(mhp, base_size, type); 684 return GNUNET_MQ_msg_ (mhp, base_size, type);
683 685
684 size = base_size + ntohs(nested_mh->size); 686 size = base_size + ntohs (nested_mh->size);
685 687
686 /* check for uint16_t overflow */ 688 /* check for uint16_t overflow */
687 if (size < base_size) 689 if (size < base_size)
688 return NULL; 690 return NULL;
689 691
690 mqm = GNUNET_MQ_msg_(mhp, size, type); 692 mqm = GNUNET_MQ_msg_ (mhp, size, type);
691 GNUNET_memcpy((char *)mqm->mh + base_size, 693 GNUNET_memcpy ((char *) mqm->mh + base_size,
692 nested_mh, 694 nested_mh,
693 ntohs(nested_mh->size)); 695 ntohs (nested_mh->size));
694 696
695 return mqm; 697 return mqm;
696} 698}
@@ -703,22 +705,22 @@ GNUNET_MQ_msg_nested_mh_(struct GNUNET_MessageHeader **mhp,
703 * @param assoc_data to associate 705 * @param assoc_data to associate
704 */ 706 */
705uint32_t 707uint32_t
706GNUNET_MQ_assoc_add(struct GNUNET_MQ_Handle *mq, void *assoc_data) 708GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data)
707{ 709{
708 uint32_t id; 710 uint32_t id;
709 711
710 if (NULL == mq->assoc_map) 712 if (NULL == mq->assoc_map)
711 { 713 {
712 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create(8); 714 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
713 mq->assoc_id = 1; 715 mq->assoc_id = 1;
714 } 716 }
715 id = mq->assoc_id++; 717 id = mq->assoc_id++;
716 GNUNET_assert(GNUNET_OK == 718 GNUNET_assert (GNUNET_OK ==
717 GNUNET_CONTAINER_multihashmap32_put( 719 GNUNET_CONTAINER_multihashmap32_put (
718 mq->assoc_map, 720 mq->assoc_map,
719 id, 721 id,
720 assoc_data, 722 assoc_data,
721 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
722 return id; 724 return id;
723} 725}
724 726
@@ -731,11 +733,11 @@ GNUNET_MQ_assoc_add(struct GNUNET_MQ_Handle *mq, void *assoc_data)
731 * @return the associated data 733 * @return the associated data
732 */ 734 */
733void * 735void *
734GNUNET_MQ_assoc_get(struct GNUNET_MQ_Handle *mq, uint32_t request_id) 736GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
735{ 737{
736 if (NULL == mq->assoc_map) 738 if (NULL == mq->assoc_map)
737 return NULL; 739 return NULL;
738 return GNUNET_CONTAINER_multihashmap32_get(mq->assoc_map, request_id); 740 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
739} 741}
740 742
741 743
@@ -747,14 +749,14 @@ GNUNET_MQ_assoc_get(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
747 * @return the associated data 749 * @return the associated data
748 */ 750 */
749void * 751void *
750GNUNET_MQ_assoc_remove(struct GNUNET_MQ_Handle *mq, uint32_t request_id) 752GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
751{ 753{
752 void *val; 754 void *val;
753 755
754 if (NULL == mq->assoc_map) 756 if (NULL == mq->assoc_map)
755 return NULL; 757 return NULL;
756 val = GNUNET_CONTAINER_multihashmap32_get(mq->assoc_map, request_id); 758 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
757 GNUNET_CONTAINER_multihashmap32_remove_all(mq->assoc_map, request_id); 759 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map, request_id);
758 return val; 760 return val;
759} 761}
760 762
@@ -769,12 +771,12 @@ GNUNET_MQ_assoc_remove(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
769 * @param cb_cls closure for the callback 771 * @param cb_cls closure for the callback
770 */ 772 */
771void 773void
772GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, 774GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
773 GNUNET_SCHEDULER_TaskCallback cb, 775 GNUNET_SCHEDULER_TaskCallback cb,
774 void *cb_cls) 776 void *cb_cls)
775{ 777{
776 /* allow setting *OR* clearing callback */ 778 /* allow setting *OR* clearing callback */
777 GNUNET_assert((NULL == ev->sent_cb) || (NULL == cb)); 779 GNUNET_assert ((NULL == ev->sent_cb) || (NULL == cb));
778 ev->sent_cb = cb; 780 ev->sent_cb = cb;
779 ev->sent_cls = cb_cls; 781 ev->sent_cls = cb_cls;
780} 782}
@@ -784,7 +786,8 @@ GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev,
784 * Handle we return for callbacks registered to be 786 * Handle we return for callbacks registered to be
785 * notified when #GNUNET_MQ_destroy() is called on a queue. 787 * notified when #GNUNET_MQ_destroy() is called on a queue.
786 */ 788 */
787struct GNUNET_MQ_DestroyNotificationHandle { 789struct GNUNET_MQ_DestroyNotificationHandle
790{
788 /** 791 /**
789 * Kept in a DLL. 792 * Kept in a DLL.
790 */ 793 */
@@ -818,86 +821,86 @@ struct GNUNET_MQ_DestroyNotificationHandle {
818 * @param mq message queue to destroy 821 * @param mq message queue to destroy
819 */ 822 */
820void 823void
821GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq) 824GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
822{ 825{
823 struct GNUNET_MQ_DestroyNotificationHandle *dnh; 826 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
824 827
825 if (NULL != mq->destroy_impl) 828 if (NULL != mq->destroy_impl)
826 { 829 {
827 mq->destroy_impl(mq, mq->impl_state); 830 mq->destroy_impl (mq, mq->impl_state);
828 } 831 }
829 if (NULL != mq->send_task) 832 if (NULL != mq->send_task)
830 { 833 {
831 GNUNET_SCHEDULER_cancel(mq->send_task); 834 GNUNET_SCHEDULER_cancel (mq->send_task);
832 mq->send_task = NULL; 835 mq->send_task = NULL;
833 } 836 }
834 while (NULL != mq->envelope_head) 837 while (NULL != mq->envelope_head)
835 { 838 {
836 struct GNUNET_MQ_Envelope *ev; 839 struct GNUNET_MQ_Envelope *ev;
837 840
838 ev = mq->envelope_head; 841 ev = mq->envelope_head;
839 ev->parent_queue = NULL; 842 ev->parent_queue = NULL;
840 GNUNET_CONTAINER_DLL_remove(mq->envelope_head, mq->envelope_tail, ev); 843 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, ev);
841 GNUNET_assert(0 < mq->queue_length); 844 GNUNET_assert (0 < mq->queue_length);
842 mq->queue_length--; 845 mq->queue_length--;
843 LOG(GNUNET_ERROR_TYPE_DEBUG, 846 LOG (GNUNET_ERROR_TYPE_DEBUG,
844 "MQ destroy drops message of type %u\n", 847 "MQ destroy drops message of type %u\n",
845 ntohs(ev->mh->type)); 848 ntohs (ev->mh->type));
846 GNUNET_MQ_discard(ev); 849 GNUNET_MQ_discard (ev);
847 } 850 }
848 if (NULL != mq->current_envelope) 851 if (NULL != mq->current_envelope)
849 { 852 {
850 /* we can only discard envelopes that 853 /* we can only discard envelopes that
851 * are not queued! */ 854 * are not queued! */
852 mq->current_envelope->parent_queue = NULL; 855 mq->current_envelope->parent_queue = NULL;
853 LOG(GNUNET_ERROR_TYPE_DEBUG, 856 LOG (GNUNET_ERROR_TYPE_DEBUG,
854 "MQ destroy drops current message of type %u\n", 857 "MQ destroy drops current message of type %u\n",
855 ntohs(mq->current_envelope->mh->type)); 858 ntohs (mq->current_envelope->mh->type));
856 GNUNET_MQ_discard(mq->current_envelope); 859 GNUNET_MQ_discard (mq->current_envelope);
857 mq->current_envelope = NULL; 860 mq->current_envelope = NULL;
858 GNUNET_assert(0 < mq->queue_length); 861 GNUNET_assert (0 < mq->queue_length);
859 mq->queue_length--; 862 mq->queue_length--;
860 } 863 }
861 GNUNET_assert(0 == mq->queue_length); 864 GNUNET_assert (0 == mq->queue_length);
862 while (NULL != (dnh = mq->dnh_head)) 865 while (NULL != (dnh = mq->dnh_head))
863 { 866 {
864 dnh->cb(dnh->cb_cls); 867 dnh->cb (dnh->cb_cls);
865 GNUNET_MQ_destroy_notify_cancel(dnh); 868 GNUNET_MQ_destroy_notify_cancel (dnh);
866 } 869 }
867 if (NULL != mq->assoc_map) 870 if (NULL != mq->assoc_map)
868 { 871 {
869 GNUNET_CONTAINER_multihashmap32_destroy(mq->assoc_map); 872 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
870 mq->assoc_map = NULL; 873 mq->assoc_map = NULL;
871 } 874 }
872 GNUNET_free_non_null(mq->handlers); 875 GNUNET_free_non_null (mq->handlers);
873 GNUNET_free(mq); 876 GNUNET_free (mq);
874} 877}
875 878
876 879
877const struct GNUNET_MessageHeader * 880const struct GNUNET_MessageHeader *
878GNUNET_MQ_extract_nested_mh_(const struct GNUNET_MessageHeader *mh, 881GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
879 uint16_t base_size) 882 uint16_t base_size)
880{ 883{
881 uint16_t whole_size; 884 uint16_t whole_size;
882 uint16_t nested_size; 885 uint16_t nested_size;
883 const struct GNUNET_MessageHeader *nested_msg; 886 const struct GNUNET_MessageHeader *nested_msg;
884 887
885 whole_size = ntohs(mh->size); 888 whole_size = ntohs (mh->size);
886 GNUNET_assert(whole_size >= base_size); 889 GNUNET_assert (whole_size >= base_size);
887 nested_size = whole_size - base_size; 890 nested_size = whole_size - base_size;
888 if (0 == nested_size) 891 if (0 == nested_size)
889 return NULL; 892 return NULL;
890 if (nested_size < sizeof(struct GNUNET_MessageHeader)) 893 if (nested_size < sizeof(struct GNUNET_MessageHeader))
891 { 894 {
892 GNUNET_break_op(0); 895 GNUNET_break_op (0);
893 return NULL; 896 return NULL;
894 } 897 }
895 nested_msg = (const struct GNUNET_MessageHeader *)((char *)mh + base_size); 898 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
896 if (ntohs(nested_msg->size) != nested_size) 899 if (ntohs (nested_msg->size) != nested_size)
897 { 900 {
898 GNUNET_break_op(0); 901 GNUNET_break_op (0);
899 return NULL; 902 return NULL;
900 } 903 }
901 return nested_msg; 904 return nested_msg;
902} 905}
903 906
@@ -910,53 +913,53 @@ GNUNET_MQ_extract_nested_mh_(const struct GNUNET_MessageHeader *mh,
910 * @param ev queued envelope to cancel 913 * @param ev queued envelope to cancel
911 */ 914 */
912void 915void
913GNUNET_MQ_send_cancel(struct GNUNET_MQ_Envelope *ev) 916GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
914{ 917{
915 struct GNUNET_MQ_Handle *mq = ev->parent_queue; 918 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
916 919
917 GNUNET_assert(NULL != mq); 920 GNUNET_assert (NULL != mq);
918 GNUNET_assert(NULL != mq->cancel_impl); 921 GNUNET_assert (NULL != mq->cancel_impl);
919 922
920 mq->evacuate_called = GNUNET_NO; 923 mq->evacuate_called = GNUNET_NO;
921 924
922 if (mq->current_envelope == ev) 925 if (mq->current_envelope == ev)
926 {
927 /* complex case, we already started with transmitting
928 the message using the callbacks. */
929 GNUNET_assert (GNUNET_NO == mq->in_flight);
930 GNUNET_assert (0 < mq->queue_length);
931 mq->queue_length--;
932 mq->cancel_impl (mq, mq->impl_state);
933 /* continue sending the next message, if any */
934 mq->current_envelope = mq->envelope_head;
935 if (NULL != mq->current_envelope)
923 { 936 {
924 /* complex case, we already started with transmitting 937 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
925 the message using the callbacks. */ 938 mq->envelope_tail,
926 GNUNET_assert(GNUNET_NO == mq->in_flight); 939 mq->current_envelope);
927 GNUNET_assert(0 < mq->queue_length); 940
928 mq->queue_length--; 941 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 mq->cancel_impl(mq, mq->impl_state); 942 "sending canceled message of type %u queue\n",
930 /* continue sending the next message, if any */ 943 ntohs (ev->mh->type));
931 mq->current_envelope = mq->envelope_head; 944
932 if (NULL != mq->current_envelope) 945 mq->send_impl (mq, mq->current_envelope->mh, mq->impl_state);
933 {
934 GNUNET_CONTAINER_DLL_remove(mq->envelope_head,
935 mq->envelope_tail,
936 mq->current_envelope);
937
938 LOG(GNUNET_ERROR_TYPE_DEBUG,
939 "sending canceled message of type %u queue\n",
940 ntohs(ev->mh->type));
941
942 mq->send_impl(mq, mq->current_envelope->mh, mq->impl_state);
943 }
944 } 946 }
947 }
945 else 948 else
946 { 949 {
947 /* simple case, message is still waiting in the queue */ 950 /* simple case, message is still waiting in the queue */
948 GNUNET_CONTAINER_DLL_remove(mq->envelope_head, mq->envelope_tail, ev); 951 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, ev);
949 GNUNET_assert(0 < mq->queue_length); 952 GNUNET_assert (0 < mq->queue_length);
950 mq->queue_length--; 953 mq->queue_length--;
951 } 954 }
952 955
953 if (GNUNET_YES != mq->evacuate_called) 956 if (GNUNET_YES != mq->evacuate_called)
954 { 957 {
955 ev->parent_queue = NULL; 958 ev->parent_queue = NULL;
956 ev->mh = NULL; 959 ev->mh = NULL;
957 /* also frees ev */ 960 /* also frees ev */
958 GNUNET_free(ev); 961 GNUNET_free (ev);
959 } 962 }
960} 963}
961 964
962 965
@@ -968,7 +971,7 @@ GNUNET_MQ_send_cancel(struct GNUNET_MQ_Envelope *ev)
968 * @return the current envelope 971 * @return the current envelope
969 */ 972 */
970struct GNUNET_MQ_Envelope * 973struct GNUNET_MQ_Envelope *
971GNUNET_MQ_get_current_envelope(struct GNUNET_MQ_Handle *mq) 974GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
972{ 975{
973 return mq->current_envelope; 976 return mq->current_envelope;
974} 977}
@@ -981,7 +984,7 @@ GNUNET_MQ_get_current_envelope(struct GNUNET_MQ_Handle *mq)
981 * @return the last envelope in the queue 984 * @return the last envelope in the queue
982 */ 985 */
983struct GNUNET_MQ_Envelope * 986struct GNUNET_MQ_Envelope *
984GNUNET_MQ_get_last_envelope(struct GNUNET_MQ_Handle *mq) 987GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
985{ 988{
986 if (NULL != mq->envelope_tail) 989 if (NULL != mq->envelope_tail)
987 return mq->envelope_tail; 990 return mq->envelope_tail;
@@ -999,8 +1002,8 @@ GNUNET_MQ_get_last_envelope(struct GNUNET_MQ_Handle *mq)
999 * @param pp priorities and preferences to apply 1002 * @param pp priorities and preferences to apply
1000 */ 1003 */
1001void 1004void
1002GNUNET_MQ_env_set_options(struct GNUNET_MQ_Envelope *env, 1005GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1003 enum GNUNET_MQ_PriorityPreferences pp) 1006 enum GNUNET_MQ_PriorityPreferences pp)
1004{ 1007{
1005 env->priority = pp; 1008 env->priority = pp;
1006 env->have_custom_options = GNUNET_YES; 1009 env->have_custom_options = GNUNET_YES;
@@ -1014,7 +1017,7 @@ GNUNET_MQ_env_set_options(struct GNUNET_MQ_Envelope *env,
1014 * @return priorities and preferences to apply for @a env 1017 * @return priorities and preferences to apply for @a env
1015 */ 1018 */
1016enum GNUNET_MQ_PriorityPreferences 1019enum GNUNET_MQ_PriorityPreferences
1017GNUNET_MQ_env_get_options(struct GNUNET_MQ_Envelope *env) 1020GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env)
1018{ 1021{
1019 struct GNUNET_MQ_Handle *mq = env->parent_queue; 1022 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1020 1023
@@ -1035,12 +1038,12 @@ GNUNET_MQ_env_get_options(struct GNUNET_MQ_Envelope *env)
1035 * @return combined priority and preferences to use 1038 * @return combined priority and preferences to use
1036 */ 1039 */
1037enum GNUNET_MQ_PriorityPreferences 1040enum GNUNET_MQ_PriorityPreferences
1038GNUNET_MQ_env_combine_options(enum GNUNET_MQ_PriorityPreferences p1, 1041GNUNET_MQ_env_combine_options (enum GNUNET_MQ_PriorityPreferences p1,
1039 enum GNUNET_MQ_PriorityPreferences p2) 1042 enum GNUNET_MQ_PriorityPreferences p2)
1040{ 1043{
1041 enum GNUNET_MQ_PriorityPreferences ret; 1044 enum GNUNET_MQ_PriorityPreferences ret;
1042 1045
1043 ret = GNUNET_MAX(p1 & GNUNET_MQ_PRIORITY_MASK, p2 & GNUNET_MQ_PRIORITY_MASK); 1046 ret = GNUNET_MAX (p1 & GNUNET_MQ_PRIORITY_MASK, p2 & GNUNET_MQ_PRIORITY_MASK);
1044 ret |= ((p1 & GNUNET_MQ_PREF_UNRELIABLE) & (p2 & GNUNET_MQ_PREF_UNRELIABLE)); 1047 ret |= ((p1 & GNUNET_MQ_PREF_UNRELIABLE) & (p2 & GNUNET_MQ_PREF_UNRELIABLE));
1045 ret |= 1048 ret |=
1046 ((p1 & GNUNET_MQ_PREF_LOW_LATENCY) | (p2 & GNUNET_MQ_PREF_LOW_LATENCY)); 1049 ((p1 & GNUNET_MQ_PREF_LOW_LATENCY) | (p2 & GNUNET_MQ_PREF_LOW_LATENCY));
@@ -1060,8 +1063,8 @@ GNUNET_MQ_env_combine_options(enum GNUNET_MQ_PriorityPreferences p1,
1060 * @param pp priorities and preferences to apply 1063 * @param pp priorities and preferences to apply
1061 */ 1064 */
1062void 1065void
1063GNUNET_MQ_set_options(struct GNUNET_MQ_Handle *mq, 1066GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1064 enum GNUNET_MQ_PriorityPreferences pp) 1067 enum GNUNET_MQ_PriorityPreferences pp)
1065{ 1068{
1066 mq->priority = pp; 1069 mq->priority = pp;
1067} 1070}
@@ -1074,7 +1077,7 @@ GNUNET_MQ_set_options(struct GNUNET_MQ_Handle *mq,
1074 * @return message contained in the envelope 1077 * @return message contained in the envelope
1075 */ 1078 */
1076const struct GNUNET_MessageHeader * 1079const struct GNUNET_MessageHeader *
1077GNUNET_MQ_env_get_msg(const struct GNUNET_MQ_Envelope *env) 1080GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env)
1078{ 1081{
1079 return env->mh; 1082 return env->mh;
1080} 1083}
@@ -1087,7 +1090,7 @@ GNUNET_MQ_env_get_msg(const struct GNUNET_MQ_Envelope *env)
1087 * @return next one, or NULL 1090 * @return next one, or NULL
1088 */ 1091 */
1089const struct GNUNET_MQ_Envelope * 1092const struct GNUNET_MQ_Envelope *
1090GNUNET_MQ_env_next(const struct GNUNET_MQ_Envelope *env) 1093GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env)
1091{ 1094{
1092 return env->next; 1095 return env->next;
1093} 1096}
@@ -1103,17 +1106,17 @@ GNUNET_MQ_env_next(const struct GNUNET_MQ_Envelope *env)
1103 * @return handle for #GNUNET_MQ_destroy_notify_cancel(). 1106 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1104 */ 1107 */
1105struct GNUNET_MQ_DestroyNotificationHandle * 1108struct GNUNET_MQ_DestroyNotificationHandle *
1106GNUNET_MQ_destroy_notify(struct GNUNET_MQ_Handle *mq, 1109GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1107 GNUNET_SCHEDULER_TaskCallback cb, 1110 GNUNET_SCHEDULER_TaskCallback cb,
1108 void *cb_cls) 1111 void *cb_cls)
1109{ 1112{
1110 struct GNUNET_MQ_DestroyNotificationHandle *dnh; 1113 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1111 1114
1112 dnh = GNUNET_new(struct GNUNET_MQ_DestroyNotificationHandle); 1115 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1113 dnh->mq = mq; 1116 dnh->mq = mq;
1114 dnh->cb = cb; 1117 dnh->cb = cb;
1115 dnh->cb_cls = cb_cls; 1118 dnh->cb_cls = cb_cls;
1116 GNUNET_CONTAINER_DLL_insert(mq->dnh_head, mq->dnh_tail, dnh); 1119 GNUNET_CONTAINER_DLL_insert (mq->dnh_head, mq->dnh_tail, dnh);
1117 return dnh; 1120 return dnh;
1118} 1121}
1119 1122
@@ -1124,13 +1127,13 @@ GNUNET_MQ_destroy_notify(struct GNUNET_MQ_Handle *mq,
1124 * @param dnh handle for registration to cancel 1127 * @param dnh handle for registration to cancel
1125 */ 1128 */
1126void 1129void
1127GNUNET_MQ_destroy_notify_cancel( 1130GNUNET_MQ_destroy_notify_cancel (
1128 struct GNUNET_MQ_DestroyNotificationHandle *dnh) 1131 struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1129{ 1132{
1130 struct GNUNET_MQ_Handle *mq = dnh->mq; 1133 struct GNUNET_MQ_Handle *mq = dnh->mq;
1131 1134
1132 GNUNET_CONTAINER_DLL_remove(mq->dnh_head, mq->dnh_tail, dnh); 1135 GNUNET_CONTAINER_DLL_remove (mq->dnh_head, mq->dnh_tail, dnh);
1133 GNUNET_free(dnh); 1136 GNUNET_free (dnh);
1134} 1137}
1135 1138
1136 1139
@@ -1147,11 +1150,11 @@ GNUNET_MQ_destroy_notify_cancel(
1147 * @param[in|out] env element to insert at the tail 1150 * @param[in|out] env element to insert at the tail
1148 */ 1151 */
1149void 1152void
1150GNUNET_MQ_dll_insert_head(struct GNUNET_MQ_Envelope **env_head, 1153GNUNET_MQ_dll_insert_head (struct GNUNET_MQ_Envelope **env_head,
1151 struct GNUNET_MQ_Envelope **env_tail, 1154 struct GNUNET_MQ_Envelope **env_tail,
1152 struct GNUNET_MQ_Envelope *env) 1155 struct GNUNET_MQ_Envelope *env)
1153{ 1156{
1154 GNUNET_CONTAINER_DLL_insert(*env_head, *env_tail, env); 1157 GNUNET_CONTAINER_DLL_insert (*env_head, *env_tail, env);
1155} 1158}
1156 1159
1157 1160
@@ -1168,11 +1171,11 @@ GNUNET_MQ_dll_insert_head(struct GNUNET_MQ_Envelope **env_head,
1168 * @param[in|out] env element to insert at the tail 1171 * @param[in|out] env element to insert at the tail
1169 */ 1172 */
1170void 1173void
1171GNUNET_MQ_dll_insert_tail(struct GNUNET_MQ_Envelope **env_head, 1174GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
1172 struct GNUNET_MQ_Envelope **env_tail, 1175 struct GNUNET_MQ_Envelope **env_tail,
1173 struct GNUNET_MQ_Envelope *env) 1176 struct GNUNET_MQ_Envelope *env)
1174{ 1177{
1175 GNUNET_CONTAINER_DLL_insert_tail(*env_head, *env_tail, env); 1178 GNUNET_CONTAINER_DLL_insert_tail (*env_head, *env_tail, env);
1176} 1179}
1177 1180
1178 1181
@@ -1189,11 +1192,11 @@ GNUNET_MQ_dll_insert_tail(struct GNUNET_MQ_Envelope **env_head,
1189 * @param[in|out] env element to remove from the DLL 1192 * @param[in|out] env element to remove from the DLL
1190 */ 1193 */
1191void 1194void
1192GNUNET_MQ_dll_remove(struct GNUNET_MQ_Envelope **env_head, 1195GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
1193 struct GNUNET_MQ_Envelope **env_tail, 1196 struct GNUNET_MQ_Envelope **env_tail,
1194 struct GNUNET_MQ_Envelope *env) 1197 struct GNUNET_MQ_Envelope *env)
1195{ 1198{
1196 GNUNET_CONTAINER_DLL_remove(*env_head, *env_tail, env); 1199 GNUNET_CONTAINER_DLL_remove (*env_head, *env_tail, env);
1197} 1200}
1198 1201
1199 1202
@@ -1208,7 +1211,7 @@ GNUNET_MQ_dll_remove(struct GNUNET_MQ_Envelope **env_head,
1208 * Needs to be freed with #GNUNET_free. 1211 * Needs to be freed with #GNUNET_free.
1209 */ 1212 */
1210struct GNUNET_MQ_MessageHandler * 1213struct GNUNET_MQ_MessageHandler *
1211GNUNET_MQ_copy_handlers(const struct GNUNET_MQ_MessageHandler *handlers) 1214GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1212{ 1215{
1213 struct GNUNET_MQ_MessageHandler *copy; 1216 struct GNUNET_MQ_MessageHandler *copy;
1214 unsigned int count; 1217 unsigned int count;
@@ -1216,11 +1219,11 @@ GNUNET_MQ_copy_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
1216 if (NULL == handlers) 1219 if (NULL == handlers)
1217 return NULL; 1220 return NULL;
1218 1221
1219 count = GNUNET_MQ_count_handlers(handlers); 1222 count = GNUNET_MQ_count_handlers (handlers);
1220 copy = GNUNET_new_array(count + 1, struct GNUNET_MQ_MessageHandler); 1223 copy = GNUNET_new_array (count + 1, struct GNUNET_MQ_MessageHandler);
1221 GNUNET_memcpy(copy, 1224 GNUNET_memcpy (copy,
1222 handlers, 1225 handlers,
1223 count * sizeof(struct GNUNET_MQ_MessageHandler)); 1226 count * sizeof(struct GNUNET_MQ_MessageHandler));
1224 return copy; 1227 return copy;
1225} 1228}
1226 1229
@@ -1238,20 +1241,20 @@ GNUNET_MQ_copy_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
1238 * Needs to be freed with #GNUNET_free. 1241 * Needs to be freed with #GNUNET_free.
1239 */ 1242 */
1240struct GNUNET_MQ_MessageHandler * 1243struct GNUNET_MQ_MessageHandler *
1241GNUNET_MQ_copy_handlers2(const struct GNUNET_MQ_MessageHandler *handlers, 1244GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
1242 GNUNET_MQ_MessageCallback agpl_handler, 1245 GNUNET_MQ_MessageCallback agpl_handler,
1243 void *agpl_cls) 1246 void *agpl_cls)
1244{ 1247{
1245 struct GNUNET_MQ_MessageHandler *copy; 1248 struct GNUNET_MQ_MessageHandler *copy;
1246 unsigned int count; 1249 unsigned int count;
1247 1250
1248 if (NULL == handlers) 1251 if (NULL == handlers)
1249 return NULL; 1252 return NULL;
1250 count = GNUNET_MQ_count_handlers(handlers); 1253 count = GNUNET_MQ_count_handlers (handlers);
1251 copy = GNUNET_new_array(count + 2, struct GNUNET_MQ_MessageHandler); 1254 copy = GNUNET_new_array (count + 2, struct GNUNET_MQ_MessageHandler);
1252 GNUNET_memcpy(copy, 1255 GNUNET_memcpy (copy,
1253 handlers, 1256 handlers,
1254 count * sizeof(struct GNUNET_MQ_MessageHandler)); 1257 count * sizeof(struct GNUNET_MQ_MessageHandler));
1255 copy[count].mv = NULL; 1258 copy[count].mv = NULL;
1256 copy[count].cb = agpl_handler; 1259 copy[count].cb = agpl_handler;
1257 copy[count].cls = agpl_cls; 1260 copy[count].cls = agpl_cls;
@@ -1268,7 +1271,7 @@ GNUNET_MQ_copy_handlers2(const struct GNUNET_MQ_MessageHandler *handlers,
1268 * @return The number of handlers in the array. 1271 * @return The number of handlers in the array.
1269 */ 1272 */
1270unsigned int 1273unsigned int
1271GNUNET_MQ_count_handlers(const struct GNUNET_MQ_MessageHandler *handlers) 1274GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1272{ 1275{
1273 unsigned int i; 1276 unsigned int i;
1274 1277
@@ -1289,22 +1292,22 @@ GNUNET_MQ_count_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
1289 * @return a string or NULL if invalid 1292 * @return a string or NULL if invalid
1290 */ 1293 */
1291const char * 1294const char *
1292GNUNET_MQ_preference_to_string(enum GNUNET_MQ_PreferenceKind type) 1295GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type)
1293{ 1296{
1294 switch (type) 1297 switch (type)
1295 { 1298 {
1296 case GNUNET_MQ_PREFERENCE_NONE: 1299 case GNUNET_MQ_PREFERENCE_NONE:
1297 return "NONE"; 1300 return "NONE";
1298 1301
1299 case GNUNET_MQ_PREFERENCE_BANDWIDTH: 1302 case GNUNET_MQ_PREFERENCE_BANDWIDTH:
1300 return "BANDWIDTH"; 1303 return "BANDWIDTH";
1301 1304
1302 case GNUNET_MQ_PREFERENCE_LATENCY: 1305 case GNUNET_MQ_PREFERENCE_LATENCY:
1303 return "LATENCY"; 1306 return "LATENCY";
1304 1307
1305 case GNUNET_MQ_PREFERENCE_RELIABILITY: 1308 case GNUNET_MQ_PREFERENCE_RELIABILITY:
1306 return "RELIABILITY"; 1309 return "RELIABILITY";
1307 } 1310 }
1308 ; 1311 ;
1309 return NULL; 1312 return NULL;
1310} 1313}
diff --git a/src/util/mst.c b/src/util/mst.c
index 4ccf4988f..a3f1368e4 100644
--- a/src/util/mst.c
+++ b/src/util/mst.c
@@ -34,13 +34,14 @@
34#define ALIGN_FACTOR 8 34#define ALIGN_FACTOR 8
35#endif 35#endif
36 36
37#define LOG(kind, ...) GNUNET_log_from(kind, "util-mst", __VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log_from (kind, "util-mst", __VA_ARGS__)
38 38
39 39
40/** 40/**
41 * Handle to a message stream tokenizer. 41 * Handle to a message stream tokenizer.
42 */ 42 */
43struct GNUNET_MessageStreamTokenizer { 43struct GNUNET_MessageStreamTokenizer
44{
44 /** 45 /**
45 * Function to call on completed messages. 46 * Function to call on completed messages.
46 */ 47 */
@@ -81,13 +82,13 @@ struct GNUNET_MessageStreamTokenizer {
81 * @return handle to tokenizer 82 * @return handle to tokenizer
82 */ 83 */
83struct GNUNET_MessageStreamTokenizer * 84struct GNUNET_MessageStreamTokenizer *
84GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, 85GNUNET_MST_create (GNUNET_MessageTokenizerCallback cb,
85 void *cb_cls) 86 void *cb_cls)
86{ 87{
87 struct GNUNET_MessageStreamTokenizer *ret; 88 struct GNUNET_MessageStreamTokenizer *ret;
88 89
89 ret = GNUNET_new(struct GNUNET_MessageStreamTokenizer); 90 ret = GNUNET_new (struct GNUNET_MessageStreamTokenizer);
90 ret->hdr = GNUNET_malloc(GNUNET_MIN_MESSAGE_SIZE); 91 ret->hdr = GNUNET_malloc (GNUNET_MIN_MESSAGE_SIZE);
91 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE; 92 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
92 ret->cb = cb; 93 ret->cb = cb;
93 ret->cb_cls = cb_cls; 94 ret->cb_cls = cb_cls;
@@ -110,11 +111,11 @@ GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb,
110 * #GNUNET_SYSERR if the data stream is corrupt 111 * #GNUNET_SYSERR if the data stream is corrupt
111 */ 112 */
112int 113int
113GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst, 114GNUNET_MST_from_buffer (struct GNUNET_MessageStreamTokenizer *mst,
114 const char *buf, 115 const char *buf,
115 size_t size, 116 size_t size,
116 int purge, 117 int purge,
117 int one_shot) 118 int one_shot)
118{ 119{
119 const struct GNUNET_MessageHeader *hdr; 120 const struct GNUNET_MessageHeader *hdr;
120 size_t delta; 121 size_t delta;
@@ -125,203 +126,203 @@ GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst,
125 int ret; 126 int ret;
126 int cbret; 127 int cbret;
127 128
128 GNUNET_assert(mst->off <= mst->pos); 129 GNUNET_assert (mst->off <= mst->pos);
129 GNUNET_assert(mst->pos <= mst->curr_buf); 130 GNUNET_assert (mst->pos <= mst->curr_buf);
130 LOG(GNUNET_ERROR_TYPE_DEBUG, 131 LOG (GNUNET_ERROR_TYPE_DEBUG,
131 "MST receives %u bytes with %u bytes already in private buffer\n", 132 "MST receives %u bytes with %u bytes already in private buffer\n",
132 (unsigned int)size, 133 (unsigned int) size,
133 (unsigned int)(mst->pos - mst->off)); 134 (unsigned int) (mst->pos - mst->off));
134 ret = GNUNET_OK; 135 ret = GNUNET_OK;
135 ibuf = (char *)mst->hdr; 136 ibuf = (char *) mst->hdr;
136 while (mst->pos > 0) 137 while (mst->pos > 0)
137 { 138 {
138do_align: 139do_align:
139 GNUNET_assert(mst->pos >= mst->off); 140 GNUNET_assert (mst->pos >= mst->off);
140 if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) || 141 if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) ||
141 (0 != (mst->off % ALIGN_FACTOR))) 142 (0 != (mst->off % ALIGN_FACTOR)))
142 { 143 {
143 /* need to align or need more space */ 144 /* need to align or need more space */
144 mst->pos -= mst->off; 145 mst->pos -= mst->off;
145 memmove(ibuf, 146 memmove (ibuf,
146 &ibuf[mst->off], 147 &ibuf[mst->off],
147 mst->pos); 148 mst->pos);
148 mst->off = 0; 149 mst->off = 0;
149 } 150 }
150 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader)) 151 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
151 { 152 {
152 delta 153 delta
153 = GNUNET_MIN(sizeof(struct GNUNET_MessageHeader) 154 = GNUNET_MIN (sizeof(struct GNUNET_MessageHeader)
154 - (mst->pos - mst->off), 155 - (mst->pos - mst->off),
155 size); 156 size);
156 GNUNET_memcpy(&ibuf[mst->pos], 157 GNUNET_memcpy (&ibuf[mst->pos],
157 buf, 158 buf,
158 delta); 159 delta);
159 mst->pos += delta; 160 mst->pos += delta;
160 buf += delta; 161 buf += delta;
161 size -= delta; 162 size -= delta;
162 } 163 }
163 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader)) 164 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
164 { 165 {
165 if (purge) 166 if (purge)
166 { 167 {
167 mst->off = 0; 168 mst->off = 0;
168 mst->pos = 0; 169 mst->pos = 0;
169 } 170 }
170 return GNUNET_OK; 171 return GNUNET_OK;
171 } 172 }
172 hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off]; 173 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
173 want = ntohs(hdr->size); 174 want = ntohs (hdr->size);
175 if (want < sizeof(struct GNUNET_MessageHeader))
176 {
177 GNUNET_break_op (0);
178 return GNUNET_SYSERR;
179 }
180 if ((mst->curr_buf - mst->off < want) &&
181 (mst->off > 0))
182 {
183 /* can get more space by moving */
184 mst->pos -= mst->off;
185 memmove (ibuf,
186 &ibuf[mst->off],
187 mst->pos);
188 mst->off = 0;
189 }
190 if (mst->curr_buf < want)
191 {
192 /* need to get more space by growing buffer */
193 GNUNET_assert (0 == mst->off);
194 mst->hdr = GNUNET_realloc (mst->hdr,
195 want);
196 ibuf = (char *) mst->hdr;
197 mst->curr_buf = want;
198 }
199 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
200 if (mst->pos - mst->off < want)
201 {
202 delta = GNUNET_MIN (want - (mst->pos - mst->off),
203 size);
204 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
205 GNUNET_memcpy (&ibuf[mst->pos],
206 buf,
207 delta);
208 mst->pos += delta;
209 buf += delta;
210 size -= delta;
211 }
212 if (mst->pos - mst->off < want)
213 {
214 if (purge)
215 {
216 mst->off = 0;
217 mst->pos = 0;
218 }
219 return GNUNET_OK;
220 }
221 if (one_shot == GNUNET_SYSERR)
222 {
223 /* cannot call callback again, but return value saying that
224 * we have another full message in the buffer */
225 ret = GNUNET_NO;
226 goto copy;
227 }
228 if (one_shot == GNUNET_YES)
229 one_shot = GNUNET_SYSERR;
230 mst->off += want;
231 if (GNUNET_OK !=
232 (cbret = mst->cb (mst->cb_cls,
233 hdr)))
234 {
235 if (GNUNET_SYSERR == cbret)
236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
237 "Failure processing message of type %u and size %u\n",
238 ntohs (hdr->type),
239 ntohs (hdr->size));
240 return GNUNET_SYSERR;
241 }
242 if (mst->off == mst->pos)
243 {
244 /* reset to beginning of buffer, it's free right now! */
245 mst->off = 0;
246 mst->pos = 0;
247 }
248 }
249 GNUNET_assert (0 == mst->pos);
250 while (size > 0)
251 {
252 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 "Server-mst has %u bytes left in inbound buffer\n",
254 (unsigned int) size);
255 if (size < sizeof(struct GNUNET_MessageHeader))
256 break;
257 offset = (unsigned long) buf;
258 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
259 if (GNUNET_NO == need_align)
260 {
261 /* can try to do zero-copy and process directly from original buffer */
262 hdr = (const struct GNUNET_MessageHeader *) buf;
263 want = ntohs (hdr->size);
174 if (want < sizeof(struct GNUNET_MessageHeader)) 264 if (want < sizeof(struct GNUNET_MessageHeader))
175 { 265 {
176 GNUNET_break_op(0); 266 GNUNET_break_op (0);
177 return GNUNET_SYSERR; 267 mst->off = 0;
178 } 268 return GNUNET_SYSERR;
179 if ((mst->curr_buf - mst->off < want) && 269 }
180 (mst->off > 0)) 270 if (size < want)
181 { 271 break; /* or not: buffer incomplete, so copy to private buffer... */
182 /* can get more space by moving */
183 mst->pos -= mst->off;
184 memmove(ibuf,
185 &ibuf[mst->off],
186 mst->pos);
187 mst->off = 0;
188 }
189 if (mst->curr_buf < want)
190 {
191 /* need to get more space by growing buffer */
192 GNUNET_assert(0 == mst->off);
193 mst->hdr = GNUNET_realloc(mst->hdr,
194 want);
195 ibuf = (char *)mst->hdr;
196 mst->curr_buf = want;
197 }
198 hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off];
199 if (mst->pos - mst->off < want)
200 {
201 delta = GNUNET_MIN(want - (mst->pos - mst->off),
202 size);
203 GNUNET_assert(mst->pos + delta <= mst->curr_buf);
204 GNUNET_memcpy(&ibuf[mst->pos],
205 buf,
206 delta);
207 mst->pos += delta;
208 buf += delta;
209 size -= delta;
210 }
211 if (mst->pos - mst->off < want)
212 {
213 if (purge)
214 {
215 mst->off = 0;
216 mst->pos = 0;
217 }
218 return GNUNET_OK;
219 }
220 if (one_shot == GNUNET_SYSERR) 272 if (one_shot == GNUNET_SYSERR)
221 { 273 {
222 /* cannot call callback again, but return value saying that 274 /* cannot call callback again, but return value saying that
223 * we have another full message in the buffer */ 275 * we have another full message in the buffer */
224 ret = GNUNET_NO; 276 ret = GNUNET_NO;
225 goto copy; 277 goto copy;
226 } 278 }
227 if (one_shot == GNUNET_YES) 279 if (one_shot == GNUNET_YES)
228 one_shot = GNUNET_SYSERR; 280 one_shot = GNUNET_SYSERR;
229 mst->off += want;
230 if (GNUNET_OK != 281 if (GNUNET_OK !=
231 (cbret = mst->cb(mst->cb_cls, 282 (cbret = mst->cb (mst->cb_cls,
232 hdr))) 283 hdr)))
233 { 284 {
234 if (GNUNET_SYSERR == cbret) 285 if (GNUNET_SYSERR == cbret)
235 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236 "Failure processing message of type %u and size %u\n", 287 "Failure processing message of type %u and size %u\n",
237 ntohs(hdr->type), 288 ntohs (hdr->type),
238 ntohs(hdr->size)); 289 ntohs (hdr->size));
239 return GNUNET_SYSERR; 290 return GNUNET_SYSERR;
240 } 291 }
241 if (mst->off == mst->pos) 292 buf += want;
242 { 293 size -= want;
243 /* reset to beginning of buffer, it's free right now! */
244 mst->off = 0;
245 mst->pos = 0;
246 }
247 } 294 }
248 GNUNET_assert(0 == mst->pos); 295 else
249 while (size > 0)
250 { 296 {
251 LOG(GNUNET_ERROR_TYPE_DEBUG, 297 /* need to copy to private buffer to align;
252 "Server-mst has %u bytes left in inbound buffer\n", 298 * yes, we go a bit more spagetti than usual here */
253 (unsigned int)size); 299 goto do_align;
254 if (size < sizeof(struct GNUNET_MessageHeader))
255 break;
256 offset = (unsigned long)buf;
257 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
258 if (GNUNET_NO == need_align)
259 {
260 /* can try to do zero-copy and process directly from original buffer */
261 hdr = (const struct GNUNET_MessageHeader *)buf;
262 want = ntohs(hdr->size);
263 if (want < sizeof(struct GNUNET_MessageHeader))
264 {
265 GNUNET_break_op(0);
266 mst->off = 0;
267 return GNUNET_SYSERR;
268 }
269 if (size < want)
270 break; /* or not: buffer incomplete, so copy to private buffer... */
271 if (one_shot == GNUNET_SYSERR)
272 {
273 /* cannot call callback again, but return value saying that
274 * we have another full message in the buffer */
275 ret = GNUNET_NO;
276 goto copy;
277 }
278 if (one_shot == GNUNET_YES)
279 one_shot = GNUNET_SYSERR;
280 if (GNUNET_OK !=
281 (cbret = mst->cb(mst->cb_cls,
282 hdr)))
283 {
284 if (GNUNET_SYSERR == cbret)
285 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
286 "Failure processing message of type %u and size %u\n",
287 ntohs(hdr->type),
288 ntohs(hdr->size));
289 return GNUNET_SYSERR;
290 }
291 buf += want;
292 size -= want;
293 }
294 else
295 {
296 /* need to copy to private buffer to align;
297 * yes, we go a bit more spagetti than usual here */
298 goto do_align;
299 }
300 } 300 }
301 }
301copy: 302copy:
302 if ((size > 0) && (!purge)) 303 if ((size > 0) && (! purge))
304 {
305 if (size + mst->pos > mst->curr_buf)
303 { 306 {
304 if (size + mst->pos > mst->curr_buf) 307 mst->hdr = GNUNET_realloc (mst->hdr,
305 { 308 size + mst->pos);
306 mst->hdr = GNUNET_realloc(mst->hdr, 309 ibuf = (char *) mst->hdr;
307 size + mst->pos); 310 mst->curr_buf = size + mst->pos;
308 ibuf = (char *)mst->hdr;
309 mst->curr_buf = size + mst->pos;
310 }
311 GNUNET_assert(size + mst->pos <= mst->curr_buf);
312 GNUNET_memcpy(&ibuf[mst->pos],
313 buf,
314 size);
315 mst->pos += size;
316 } 311 }
312 GNUNET_assert (size + mst->pos <= mst->curr_buf);
313 GNUNET_memcpy (&ibuf[mst->pos],
314 buf,
315 size);
316 mst->pos += size;
317 }
317 if (purge) 318 if (purge)
318 { 319 {
319 mst->off = 0; 320 mst->off = 0;
320 mst->pos = 0; 321 mst->pos = 0;
321 } 322 }
322 LOG(GNUNET_ERROR_TYPE_DEBUG, 323 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Server-mst leaves %u bytes in private buffer\n", 324 "Server-mst leaves %u bytes in private buffer\n",
324 (unsigned int)(mst->pos - mst->off)); 325 (unsigned int) (mst->pos - mst->off));
325 return ret; 326 return ret;
326} 327}
327 328
@@ -341,40 +342,40 @@ copy:
341 * #GNUNET_SYSERR if the data stream is corrupt 342 * #GNUNET_SYSERR if the data stream is corrupt
342 */ 343 */
343int 344int
344GNUNET_MST_read(struct GNUNET_MessageStreamTokenizer *mst, 345GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst,
345 struct GNUNET_NETWORK_Handle *sock, 346 struct GNUNET_NETWORK_Handle *sock,
346 int purge, 347 int purge,
347 int one_shot) 348 int one_shot)
348{ 349{
349 ssize_t ret; 350 ssize_t ret;
350 size_t left; 351 size_t left;
351 char *buf; 352 char *buf;
352 353
353 left = mst->curr_buf - mst->pos; 354 left = mst->curr_buf - mst->pos;
354 buf = (char *)mst->hdr; 355 buf = (char *) mst->hdr;
355 ret = GNUNET_NETWORK_socket_recv(sock, 356 ret = GNUNET_NETWORK_socket_recv (sock,
356 &buf[mst->pos], 357 &buf[mst->pos],
357 left); 358 left);
358 if (-1 == ret) 359 if (-1 == ret)
359 { 360 {
360 if ((EAGAIN == errno) || 361 if ((EAGAIN == errno) ||
361 (EINTR == errno)) 362 (EINTR == errno))
362 return GNUNET_OK; 363 return GNUNET_OK;
363 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, 364 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
364 "recv"); 365 "recv");
365 return GNUNET_SYSERR; 366 return GNUNET_SYSERR;
366 } 367 }
367 if (0 == ret) 368 if (0 == ret)
368 { 369 {
369 /* other side closed connection, treat as error */ 370 /* other side closed connection, treat as error */
370 return GNUNET_SYSERR; 371 return GNUNET_SYSERR;
371 } 372 }
372 mst->pos += ret; 373 mst->pos += ret;
373 return GNUNET_MST_from_buffer(mst, 374 return GNUNET_MST_from_buffer (mst,
374 NULL, 375 NULL,
375 0, 376 0,
376 purge, 377 purge,
377 one_shot); 378 one_shot);
378} 379}
379 380
380 381
@@ -390,14 +391,14 @@ GNUNET_MST_read(struct GNUNET_MessageStreamTokenizer *mst,
390 * #GNUNET_SYSERR if the data stream is corrupt 391 * #GNUNET_SYSERR if the data stream is corrupt
391 */ 392 */
392int 393int
393GNUNET_MST_next(struct GNUNET_MessageStreamTokenizer *mst, 394GNUNET_MST_next (struct GNUNET_MessageStreamTokenizer *mst,
394 int one_shot) 395 int one_shot)
395{ 396{
396 return GNUNET_MST_from_buffer(mst, 397 return GNUNET_MST_from_buffer (mst,
397 NULL, 398 NULL,
398 0, 399 0,
399 GNUNET_NO, 400 GNUNET_NO,
400 one_shot); 401 one_shot);
401} 402}
402 403
403 404
@@ -407,10 +408,10 @@ GNUNET_MST_next(struct GNUNET_MessageStreamTokenizer *mst,
407 * @param mst tokenizer to destroy 408 * @param mst tokenizer to destroy
408 */ 409 */
409void 410void
410GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst) 411GNUNET_MST_destroy (struct GNUNET_MessageStreamTokenizer *mst)
411{ 412{
412 GNUNET_free(mst->hdr); 413 GNUNET_free (mst->hdr);
413 GNUNET_free(mst); 414 GNUNET_free (mst);
414} 415}
415 416
416 417
diff --git a/src/util/nc.c b/src/util/nc.c
index ab7ef122d..9db321b8c 100644
--- a/src/util/nc.c
+++ b/src/util/nc.c
@@ -28,13 +28,14 @@
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-nc", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-nc", __VA_ARGS__)
32 32
33 33
34/** 34/**
35 * Lists of subscribers we manage for notifications. 35 * Lists of subscribers we manage for notifications.
36 */ 36 */
37struct SubscriberList { 37struct SubscriberList
38{
38 /** 39 /**
39 * This is a doubly linked list. 40 * This is a doubly linked list.
40 */ 41 */
@@ -71,7 +72,8 @@ struct SubscriberList {
71 * (notification) messages are queued up until the subscriber is able to 72 * (notification) messages are queued up until the subscriber is able to
72 * read them. 73 * read them.
73 */ 74 */
74struct GNUNET_NotificationContext { 75struct GNUNET_NotificationContext
76{
75 /** 77 /**
76 * Head of list of subscribers receiving notifications. 78 * Head of list of subscribers receiving notifications.
77 */ 79 */
@@ -95,15 +97,15 @@ struct GNUNET_NotificationContext {
95 * @param cls our `struct SubscriberList *` 97 * @param cls our `struct SubscriberList *`
96 */ 98 */
97static void 99static void
98handle_mq_destroy(void *cls) 100handle_mq_destroy (void *cls)
99{ 101{
100 struct SubscriberList *pos = cls; 102 struct SubscriberList *pos = cls;
101 struct GNUNET_NotificationContext *nc = pos->nc; 103 struct GNUNET_NotificationContext *nc = pos->nc;
102 104
103 GNUNET_CONTAINER_DLL_remove(nc->subscribers_head, 105 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
104 nc->subscribers_tail, 106 nc->subscribers_tail,
105 pos); 107 pos);
106 GNUNET_free(pos); 108 GNUNET_free (pos);
107} 109}
108 110
109 111
@@ -116,11 +118,11 @@ handle_mq_destroy(void *cls)
116 * @return handle to the notification context 118 * @return handle to the notification context
117 */ 119 */
118struct GNUNET_NotificationContext * 120struct GNUNET_NotificationContext *
119GNUNET_notification_context_create(unsigned int queue_length) 121GNUNET_notification_context_create (unsigned int queue_length)
120{ 122{
121 struct GNUNET_NotificationContext *nc; 123 struct GNUNET_NotificationContext *nc;
122 124
123 nc = GNUNET_new(struct GNUNET_NotificationContext); 125 nc = GNUNET_new (struct GNUNET_NotificationContext);
124 nc->queue_length = queue_length; 126 nc->queue_length = queue_length;
125 return nc; 127 return nc;
126} 128}
@@ -132,19 +134,19 @@ GNUNET_notification_context_create(unsigned int queue_length)
132 * @param nc context to destroy. 134 * @param nc context to destroy.
133 */ 135 */
134void 136void
135GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc) 137GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc)
136{ 138{
137 struct SubscriberList *pos; 139 struct SubscriberList *pos;
138 140
139 while (NULL != (pos = nc->subscribers_head)) 141 while (NULL != (pos = nc->subscribers_head))
140 { 142 {
141 GNUNET_CONTAINER_DLL_remove(nc->subscribers_head, 143 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
142 nc->subscribers_tail, 144 nc->subscribers_tail,
143 pos); 145 pos);
144 GNUNET_MQ_destroy_notify_cancel(pos->mq_nh); 146 GNUNET_MQ_destroy_notify_cancel (pos->mq_nh);
145 GNUNET_free(pos); 147 GNUNET_free (pos);
146 } 148 }
147 GNUNET_free(nc); 149 GNUNET_free (nc);
148} 150}
149 151
150 152
@@ -155,8 +157,8 @@ GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc)
155 * @param mq message queue add 157 * @param mq message queue add
156 */ 158 */
157void 159void
158GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc, 160GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc,
159 struct GNUNET_MQ_Handle *mq) 161 struct GNUNET_MQ_Handle *mq)
160{ 162{
161 struct SubscriberList *cl; 163 struct SubscriberList *cl;
162 164
@@ -164,15 +166,15 @@ GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc,
164 if (cl->mq == mq) 166 if (cl->mq == mq)
165 return; 167 return;
166 /* already present */ 168 /* already present */
167 cl = GNUNET_new(struct SubscriberList); 169 cl = GNUNET_new (struct SubscriberList);
168 GNUNET_CONTAINER_DLL_insert(nc->subscribers_head, 170 GNUNET_CONTAINER_DLL_insert (nc->subscribers_head,
169 nc->subscribers_tail, 171 nc->subscribers_tail,
170 cl); 172 cl);
171 cl->nc = nc; 173 cl->nc = nc;
172 cl->mq = mq; 174 cl->mq = mq;
173 cl->mq_nh = GNUNET_MQ_destroy_notify(cl->mq, 175 cl->mq_nh = GNUNET_MQ_destroy_notify (cl->mq,
174 &handle_mq_destroy, 176 &handle_mq_destroy,
175 cl); 177 cl);
176} 178}
177 179
178 180
@@ -184,22 +186,22 @@ GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc,
184 * @param can_drop can this message be dropped due to queue length limitations 186 * @param can_drop can this message be dropped due to queue length limitations
185 */ 187 */
186void 188void
187GNUNET_notification_context_broadcast(struct GNUNET_NotificationContext *nc, 189GNUNET_notification_context_broadcast (struct GNUNET_NotificationContext *nc,
188 const struct GNUNET_MessageHeader *msg, 190 const struct GNUNET_MessageHeader *msg,
189 int can_drop) 191 int can_drop)
190{ 192{
191 struct SubscriberList *pos; 193 struct SubscriberList *pos;
192 struct GNUNET_MQ_Envelope *env; 194 struct GNUNET_MQ_Envelope *env;
193 195
194 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next) 196 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)
195 { 197 {
196 if ((GNUNET_YES == can_drop) && 198 if ((GNUNET_YES == can_drop) &&
197 (GNUNET_MQ_get_length(pos->mq) > nc->queue_length)) 199 (GNUNET_MQ_get_length (pos->mq) > nc->queue_length))
198 continue; 200 continue;
199 env = GNUNET_MQ_msg_copy(msg); 201 env = GNUNET_MQ_msg_copy (msg);
200 GNUNET_MQ_send(pos->mq, 202 GNUNET_MQ_send (pos->mq,
201 env); 203 env);
202 } 204 }
203} 205}
204 206
205 207
@@ -210,7 +212,7 @@ GNUNET_notification_context_broadcast(struct GNUNET_NotificationContext *nc,
210 * @return number of current subscribers 212 * @return number of current subscribers
211 */ 213 */
212unsigned int 214unsigned int
213GNUNET_notification_context_get_size(struct GNUNET_NotificationContext *nc) 215GNUNET_notification_context_get_size (struct GNUNET_NotificationContext *nc)
214{ 216{
215 unsigned int num; 217 unsigned int num;
216 struct SubscriberList *pos; 218 struct SubscriberList *pos;
diff --git a/src/util/network.c b/src/util/network.c
index 6f8a8ff5a..ce4f2f896 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -28,9 +28,15 @@
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29#include "disk.h" 29#include "disk.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-network", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
32#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-network", syscall, filename) 32#define LOG_STRERROR_FILE(kind, syscall, \
33#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror(kind, "util-network", syscall) 33 filename) GNUNET_log_from_strerror_file (kind, \
34 "util-network", \
35 syscall, \
36 filename)
37#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
38 "util-network", \
39 syscall)
34 40
35#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING 41#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
36 42
@@ -43,7 +49,8 @@
43/** 49/**
44 * @brief handle to a socket 50 * @brief handle to a socket
45 */ 51 */
46struct GNUNET_NETWORK_Handle { 52struct GNUNET_NETWORK_Handle
53{
47 int fd; 54 int fd;
48 55
49 /** 56 /**
@@ -75,7 +82,7 @@ struct GNUNET_NETWORK_Handle {
75 * @return #GNUNET_OK if the PF is supported 82 * @return #GNUNET_OK if the PF is supported
76 */ 83 */
77int 84int
78GNUNET_NETWORK_test_pf(int pf) 85GNUNET_NETWORK_test_pf (int pf)
79{ 86{
80 static int cache_v4 = -1; 87 static int cache_v4 = -1;
81 static int cache_v6 = -1; 88 static int cache_v6 = -1;
@@ -84,56 +91,56 @@ GNUNET_NETWORK_test_pf(int pf)
84 int ret; 91 int ret;
85 92
86 switch (pf) 93 switch (pf)
87 { 94 {
88 case PF_INET: 95 case PF_INET:
89 if (-1 != cache_v4) 96 if (-1 != cache_v4)
90 return cache_v4; 97 return cache_v4;
91 break; 98 break;
92 99
93 case PF_INET6: 100 case PF_INET6:
94 if (-1 != cache_v6) 101 if (-1 != cache_v6)
95 return cache_v6; 102 return cache_v6;
96 break; 103 break;
97 104
98#ifdef PF_UNIX 105#ifdef PF_UNIX
99 case PF_UNIX: 106 case PF_UNIX:
100 if (-1 != cache_un) 107 if (-1 != cache_un)
101 return cache_un; 108 return cache_un;
102 break; 109 break;
103#endif 110#endif
104 } 111 }
105 s = socket(pf, SOCK_STREAM, 0); 112 s = socket (pf, SOCK_STREAM, 0);
106 if (-1 == s) 113 if (-1 == s)
114 {
115 if (EAFNOSUPPORT != errno)
107 { 116 {
108 if (EAFNOSUPPORT != errno) 117 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
109 { 118 "socket");
110 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 119 return GNUNET_SYSERR;
111 "socket");
112 return GNUNET_SYSERR;
113 }
114 ret = GNUNET_NO;
115 } 120 }
121 ret = GNUNET_NO;
122 }
116 else 123 else
117 { 124 {
118 close(s); 125 close (s);
119 ret = GNUNET_OK; 126 ret = GNUNET_OK;
120 } 127 }
121 switch (pf) 128 switch (pf)
122 { 129 {
123 case PF_INET: 130 case PF_INET:
124 cache_v4 = ret; 131 cache_v4 = ret;
125 break; 132 break;
126 133
127 case PF_INET6: 134 case PF_INET6:
128 cache_v6 = ret; 135 cache_v6 = ret;
129 break; 136 break;
130 137
131#ifdef PF_UNIX 138#ifdef PF_UNIX
132 case PF_UNIX: 139 case PF_UNIX:
133 cache_un = ret; 140 cache_un = ret;
134 break; 141 break;
135#endif 142#endif
136 } 143 }
137 return ret; 144 return ret;
138} 145}
139 146
@@ -148,7 +155,7 @@ GNUNET_NETWORK_test_pf(int pf)
148 * @return shortened unixpath, NULL on error 155 * @return shortened unixpath, NULL on error
149 */ 156 */
150char * 157char *
151GNUNET_NETWORK_shorten_unixpath(char *unixpath) 158GNUNET_NETWORK_shorten_unixpath (char *unixpath)
152{ 159{
153 struct sockaddr_un dummy; 160 struct sockaddr_un dummy;
154 size_t slen; 161 size_t slen;
@@ -158,25 +165,26 @@ GNUNET_NETWORK_shorten_unixpath(char *unixpath)
158 size_t upm; 165 size_t upm;
159 166
160 upm = sizeof(dummy.sun_path); 167 upm = sizeof(dummy.sun_path);
161 slen = strlen(unixpath); 168 slen = strlen (unixpath);
162 if (slen < upm) 169 if (slen < upm)
163 return unixpath; /* no shortening required */ 170 return unixpath; /* no shortening required */
164 GNUNET_CRYPTO_hash(unixpath, slen, &sh); 171 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
165 while (16 + strlen(unixpath) >= upm) 172 while (16 + strlen (unixpath) >= upm)
173 {
174 if (NULL == (end = strrchr (unixpath, '/')))
166 { 175 {
167 if (NULL == (end = strrchr(unixpath, '/'))) 176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 { 177 _ (
169 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 178 "Unable to shorten unix path `%s' while keeping name unique\n"),
170 _("Unable to shorten unix path `%s' while keeping name unique\n"), 179 unixpath);
171 unixpath); 180 GNUNET_free (unixpath);
172 GNUNET_free(unixpath); 181 return NULL;
173 return NULL;
174 }
175 *end = '\0';
176 } 182 }
177 GNUNET_CRYPTO_hash_to_enc(&sh, &ae); 183 *end = '\0';
184 }
185 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
178 ae.encoding[16] = '\0'; 186 ae.encoding[16] = '\0';
179 strcat(unixpath, (char *)ae.encoding); 187 strcat (unixpath, (char *) ae.encoding);
180 return unixpath; 188 return unixpath;
181} 189}
182 190
@@ -191,48 +199,48 @@ GNUNET_NETWORK_shorten_unixpath(char *unixpath)
191 * @param un unix domain socket address to check 199 * @param un unix domain socket address to check
192 */ 200 */
193void 201void
194GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un) 202GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
195{ 203{
196 int s; 204 int s;
197 int eno; 205 int eno;
198 struct stat sbuf; 206 struct stat sbuf;
199 int ret; 207 int ret;
200 208
201 s = socket(AF_UNIX, SOCK_STREAM, 0); 209 s = socket (AF_UNIX, SOCK_STREAM, 0);
202 if (-1 == s) 210 if (-1 == s)
203 { 211 {
204 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
205 "Failed to open AF_UNIX socket"); 213 "Failed to open AF_UNIX socket");
206 return; 214 return;
207 } 215 }
208 ret = connect(s, 216 ret = connect (s,
209 (struct sockaddr *)un, 217 (struct sockaddr *) un,
210 sizeof(struct sockaddr_un)); 218 sizeof(struct sockaddr_un));
211 eno = errno; 219 eno = errno;
212 GNUNET_break(0 == close(s)); 220 GNUNET_break (0 == close (s));
213 if (0 == ret) 221 if (0 == ret)
214 return; /* another process is listening, do not remove! */ 222 return; /* another process is listening, do not remove! */
215 if (ECONNREFUSED != eno) 223 if (ECONNREFUSED != eno)
216 return; /* some other error, likely "no such file or directory" -- all well */ 224 return; /* some other error, likely "no such file or directory" -- all well */
217 /* should unlink, but sanity checks first */ 225 /* should unlink, but sanity checks first */
218 if (0 != stat(un->sun_path, 226 if (0 != stat (un->sun_path,
219 &sbuf)) 227 &sbuf))
220 return; /* failed to 'stat', likely does not exist after all */ 228 return; /* failed to 'stat', likely does not exist after all */
221 if (S_IFSOCK != (S_IFMT & sbuf.st_mode)) 229 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
222 return; /* refuse to unlink anything except sockets */ 230 return; /* refuse to unlink anything except sockets */
223 /* finally, really unlink */ 231 /* finally, really unlink */
224 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
225 "Removing left-over `%s' from previous exeuction\n", 233 "Removing left-over `%s' from previous exeuction\n",
226 un->sun_path); 234 un->sun_path);
227 if (0 != unlink(un->sun_path)) 235 if (0 != unlink (un->sun_path))
228 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, 236 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
229 "unlink", 237 "unlink",
230 un->sun_path); 238 un->sun_path);
231} 239}
232 240
233 241
234#ifndef FD_COPY 242#ifndef FD_COPY
235#define FD_COPY(s, d) do { GNUNET_memcpy((d), (s), sizeof(fd_set)); } while (0) 243#define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof(fd_set)); } while (0)
236#endif 244#endif
237 245
238 246
@@ -244,31 +252,31 @@ GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un)
244 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 252 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
245 */ 253 */
246int 254int
247GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd, 255GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
248 int doBlock) 256 int doBlock)
249{ 257{
250 int flags = fcntl(fd->fd, F_GETFL); 258 int flags = fcntl (fd->fd, F_GETFL);
251 259
252 if (flags == -1) 260 if (flags == -1)
253 { 261 {
254 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 262 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
255 "fcntl"); 263 "fcntl");
256 return GNUNET_SYSERR; 264 return GNUNET_SYSERR;
257 } 265 }
258 if (doBlock) 266 if (doBlock)
259 flags &= ~O_NONBLOCK; 267 flags &= ~O_NONBLOCK;
260 268
261 else 269 else
262 flags |= O_NONBLOCK; 270 flags |= O_NONBLOCK;
263 if (0 != fcntl(fd->fd, 271 if (0 != fcntl (fd->fd,
264 F_SETFL, 272 F_SETFL,
265 flags)) 273 flags))
266 274
267 { 275 {
268 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 276 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
269 "fcntl"); 277 "fcntl");
270 return GNUNET_SYSERR; 278 return GNUNET_SYSERR;
271 } 279 }
272 return GNUNET_OK; 280 return GNUNET_OK;
273} 281}
274 282
@@ -281,16 +289,16 @@ GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd,
281 * @warning Not implemented on Windows 289 * @warning Not implemented on Windows
282 */ 290 */
283static int 291static int
284socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h) 292socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
285{ 293{
286 int i; 294 int i;
287 i = fcntl(h->fd, F_GETFD); 295 i = fcntl (h->fd, F_GETFD);
288 if (i < 0) 296 if (i < 0)
289 return GNUNET_SYSERR; 297 return GNUNET_SYSERR;
290 if (i == (i | FD_CLOEXEC)) 298 if (i == (i | FD_CLOEXEC))
291 return GNUNET_OK; 299 return GNUNET_OK;
292 i |= FD_CLOEXEC; 300 i |= FD_CLOEXEC;
293 if (fcntl(h->fd, F_SETFD, i) < 0) 301 if (fcntl (h->fd, F_SETFD, i) < 0)
294 return GNUNET_SYSERR; 302 return GNUNET_SYSERR;
295 303
296 return GNUNET_OK; 304 return GNUNET_OK;
@@ -304,14 +312,14 @@ socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
304 * @param h the socket to make non-delaying 312 * @param h the socket to make non-delaying
305 */ 313 */
306static int 314static int
307socket_set_nosigpipe(const struct GNUNET_NETWORK_Handle *h) 315socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
308{ 316{
309 int abs_value = 1; 317 int abs_value = 1;
310 318
311 if (0 != 319 if (0 !=
312 setsockopt(h->fd, SOL_SOCKET, SO_NOSIGPIPE, 320 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
313 (const void *)&abs_value, 321 (const void *) &abs_value,
314 sizeof(abs_value))) 322 sizeof(abs_value)))
315 return GNUNET_SYSERR; 323 return GNUNET_SYSERR;
316 return GNUNET_OK; 324 return GNUNET_OK;
317} 325}
@@ -326,17 +334,17 @@ socket_set_nosigpipe(const struct GNUNET_NETWORK_Handle *h)
326 * @param h the socket to make non-delaying 334 * @param h the socket to make non-delaying
327 */ 335 */
328static void 336static void
329socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h) 337socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
330{ 338{
331 int value = 1; 339 int value = 1;
332 340
333 if (0 != 341 if (0 !=
334 setsockopt(h->fd, 342 setsockopt (h->fd,
335 IPPROTO_TCP, 343 IPPROTO_TCP,
336 TCP_NODELAY, 344 TCP_NODELAY,
337 &value, sizeof(value))) 345 &value, sizeof(value)))
338 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
339 "setsockopt"); 347 "setsockopt");
340} 348}
341 349
342 350
@@ -353,57 +361,57 @@ socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
353 * failed and the handle was destroyed 361 * failed and the handle was destroyed
354 */ 362 */
355static int 363static int
356initialize_network_handle(struct GNUNET_NETWORK_Handle *h, 364initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
357 int af, 365 int af,
358 int type) 366 int type)
359{ 367{
360 int eno; 368 int eno;
361 369
362 h->af = af; 370 h->af = af;
363 h->type = type; 371 h->type = type;
364 if (h->fd == INVALID_SOCKET) 372 if (h->fd == INVALID_SOCKET)
365 { 373 {
366 eno = errno; 374 eno = errno;
367 GNUNET_free(h); 375 GNUNET_free (h);
368 errno = eno; 376 errno = eno;
369 return GNUNET_SYSERR; 377 return GNUNET_SYSERR;
370 } 378 }
371 379
372 if (h->fd >= FD_SETSIZE) 380 if (h->fd >= FD_SETSIZE)
373 { 381 {
374 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h)); 382 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
375 errno = EMFILE; 383 errno = EMFILE;
376 return GNUNET_SYSERR; 384 return GNUNET_SYSERR;
377 } 385 }
378 386
379 if (GNUNET_OK != socket_set_inheritable(h)) 387 if (GNUNET_OK != socket_set_inheritable (h))
380 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 388 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
381 "socket_set_inheritable"); 389 "socket_set_inheritable");
382 390
383 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking(h, GNUNET_NO)) 391 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
384 { 392 {
385 eno = errno; 393 eno = errno;
386 GNUNET_break(0); 394 GNUNET_break (0);
387 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h)); 395 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
388 errno = eno; 396 errno = eno;
389 return GNUNET_SYSERR; 397 return GNUNET_SYSERR;
390 } 398 }
391#ifdef DARWIN 399#ifdef DARWIN
392 if (GNUNET_SYSERR == socket_set_nosigpipe(h)) 400 if (GNUNET_SYSERR == socket_set_nosigpipe (h))
393 { 401 {
394 eno = errno; 402 eno = errno;
395 GNUNET_break(0); 403 GNUNET_break (0);
396 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h)); 404 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
397 errno = eno; 405 errno = eno;
398 return GNUNET_SYSERR; 406 return GNUNET_SYSERR;
399 } 407 }
400#endif 408#endif
401 if ((type == SOCK_STREAM) 409 if ((type == SOCK_STREAM)
402#ifdef AF_UNIX 410#ifdef AF_UNIX
403 && (af != AF_UNIX) 411 && (af != AF_UNIX)
404#endif 412#endif
405 ) 413 )
406 socket_set_nodelay(h); 414 socket_set_nodelay (h);
407 return GNUNET_OK; 415 return GNUNET_OK;
408} 416}
409 417
@@ -417,47 +425,48 @@ initialize_network_handle(struct GNUNET_NETWORK_Handle *h,
417 * @return client socket 425 * @return client socket
418 */ 426 */
419struct GNUNET_NETWORK_Handle * 427struct GNUNET_NETWORK_Handle *
420GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, 428GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
421 struct sockaddr *address, 429 struct sockaddr *address,
422 socklen_t *address_len) 430 socklen_t *address_len)
423{ 431{
424 struct GNUNET_NETWORK_Handle *ret; 432 struct GNUNET_NETWORK_Handle *ret;
425 int eno; 433 int eno;
426 434
427 ret = GNUNET_new(struct GNUNET_NETWORK_Handle); 435 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
428#if DEBUG_NETWORK 436#if DEBUG_NETWORK
429 { 437 {
430 struct sockaddr_storage name; 438 struct sockaddr_storage name;
431 socklen_t namelen = sizeof(name); 439 socklen_t namelen = sizeof(name);
432 440
433 int gsn = getsockname(desc->fd, 441 int gsn = getsockname (desc->fd,
434 (struct sockaddr *)&name, 442 (struct sockaddr *) &name,
435 &namelen); 443 &namelen);
436 444
437 if (0 == gsn) 445 if (0 == gsn)
438 LOG(GNUNET_ERROR_TYPE_DEBUG, 446 LOG (GNUNET_ERROR_TYPE_DEBUG,
439 "Accepting connection on `%s'\n", 447 "Accepting connection on `%s'\n",
440 GNUNET_a2s((const struct sockaddr *)&name, 448 GNUNET_a2s ((const struct sockaddr *) &name,
441 namelen)); 449 namelen));
442 } 450 }
443#endif 451#endif
444 ret->fd = accept(desc->fd, 452 ret->fd = accept (desc->fd,
445 address, 453 address,
446 address_len); 454 address_len);
447 if (-1 == ret->fd) 455 if (-1 == ret->fd)
448 { 456 {
449 eno = errno; 457 eno = errno;
450 GNUNET_free(ret); 458 GNUNET_free (ret);
451 errno = eno; 459 errno = eno;
452 return NULL; 460 return NULL;
453 } 461 }
454 if (GNUNET_OK != 462 if (GNUNET_OK !=
455 initialize_network_handle(ret, 463 initialize_network_handle (ret,
456 (NULL != address) ? address->sa_family : desc->af, 464 (NULL != address) ? address->sa_family :
457 SOCK_STREAM)) 465 desc->af,
458 { 466 SOCK_STREAM))
459 return NULL; 467 {
460 } 468 return NULL;
469 }
461 return ret; 470 return ret;
462} 471}
463 472
@@ -471,9 +480,9 @@ GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc,
471 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 480 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
472 */ 481 */
473int 482int
474GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, 483GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
475 const struct sockaddr *address, 484 const struct sockaddr *address,
476 socklen_t address_len) 485 socklen_t address_len)
477{ 486{
478 int ret; 487 int ret;
479 488
@@ -483,55 +492,56 @@ GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc,
483 const int on = 1; 492 const int on = 1;
484 493
485 if (AF_INET6 == desc->af) 494 if (AF_INET6 == desc->af)
486 if (setsockopt(desc->fd, 495 if (setsockopt (desc->fd,
487 IPPROTO_IPV6, 496 IPPROTO_IPV6,
488 IPV6_V6ONLY, 497 IPV6_V6ONLY,
489 (const void *)&on, 498 (const void *) &on,
490 sizeof(on))) 499 sizeof(on)))
491 LOG_STRERROR(GNUNET_ERROR_TYPE_DEBUG, 500 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
492 "setsockopt"); 501 "setsockopt");
493 } 502 }
494#endif 503#endif
495#endif 504#endif
496 if (AF_UNIX == address->sa_family) 505 if (AF_UNIX == address->sa_family)
497 GNUNET_NETWORK_unix_precheck((const struct sockaddr_un *)address); 506 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
498 { 507 {
499 const int on = 1; 508 const int on = 1;
500 509
501 /* This is required here for TCP sockets, but only on UNIX */ 510 /* This is required here for TCP sockets, but only on UNIX */
502 if ((SOCK_STREAM == desc->type) && 511 if ((SOCK_STREAM == desc->type) &&
503 (0 != setsockopt(desc->fd, 512 (0 != setsockopt (desc->fd,
504 SOL_SOCKET, 513 SOL_SOCKET,
505 SO_REUSEADDR, 514 SO_REUSEADDR,
506 &on, sizeof(on)))) 515 &on, sizeof(on))))
507 LOG_STRERROR(GNUNET_ERROR_TYPE_DEBUG, 516 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
508 "setsockopt"); 517 "setsockopt");
509 } 518 }
510 { 519 {
511 /* set permissions of newly created non-abstract UNIX domain socket to 520 /* set permissions of newly created non-abstract UNIX domain socket to
512 "user-only"; applications can choose to relax this later */ 521 "user-only"; applications can choose to relax this later */
513 mode_t old_mask = 0; /* assigned to make compiler happy */ 522 mode_t old_mask = 0; /* assigned to make compiler happy */
514 const struct sockaddr_un *un = (const struct sockaddr_un *)address; 523 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
515 int not_abstract = 0; 524 int not_abstract = 0;
516 525
517 if ((AF_UNIX == address->sa_family) 526 if ((AF_UNIX == address->sa_family)
518 && ('\0' != un->sun_path[0])) /* Not an abstract socket */ 527 && ('\0' != un->sun_path[0])) /* Not an abstract socket */
519 not_abstract = 1; 528 not_abstract = 1;
520 if (not_abstract) 529 if (not_abstract)
521 old_mask = umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH); 530 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
531 | S_IXOTH);
522 532
523 ret = bind(desc->fd, 533 ret = bind (desc->fd,
524 address, 534 address,
525 address_len); 535 address_len);
526 536
527 if (not_abstract) 537 if (not_abstract)
528 (void)umask(old_mask); 538 (void) umask (old_mask);
529} 539 }
530 if (0 != ret) 540 if (0 != ret)
531 return GNUNET_SYSERR; 541 return GNUNET_SYSERR;
532 542
533 desc->addr = GNUNET_malloc(address_len); 543 desc->addr = GNUNET_malloc (address_len);
534 GNUNET_memcpy(desc->addr, address, address_len); 544 GNUNET_memcpy (desc->addr, address, address_len);
535 desc->addrlen = address_len; 545 desc->addrlen = address_len;
536 546
537 return GNUNET_OK; 547 return GNUNET_OK;
@@ -545,58 +555,58 @@ GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc,
545 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 555 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
546 */ 556 */
547int 557int
548GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc) 558GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
549{ 559{
550 int ret; 560 int ret;
551 561
552 ret = close(desc->fd); 562 ret = close (desc->fd);
553 563
554 const struct sockaddr_un *un = (const struct sockaddr_un *)desc->addr; 564 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
555 565
556 /* Cleanup the UNIX domain socket and its parent directories in case of non 566 /* Cleanup the UNIX domain socket and its parent directories in case of non
557 abstract sockets */ 567 abstract sockets */
558 if ((AF_UNIX == desc->af) && 568 if ((AF_UNIX == desc->af) &&
559 (NULL != desc->addr) && 569 (NULL != desc->addr) &&
560 ('\0' != un->sun_path[0])) 570 ('\0' != un->sun_path[0]))
561 { 571 {
562 char *dirname = GNUNET_strndup(un->sun_path, 572 char *dirname = GNUNET_strndup (un->sun_path,
563 sizeof(un->sun_path)); 573 sizeof(un->sun_path));
564 574
565 if (0 != unlink(dirname)) 575 if (0 != unlink (dirname))
566 { 576 {
567 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, 577 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
568 "unlink", 578 "unlink",
569 dirname); 579 dirname);
570 } 580 }
571 else 581 else
582 {
583 size_t len;
584
585 len = strlen (dirname);
586 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
587 len--;
588 dirname[len] = '\0';
589 if ((0 != len) && (0 != rmdir (dirname)))
590 {
591 switch (errno)
572 { 592 {
573 size_t len; 593 case EACCES:
574 594 case ENOTEMPTY:
575 len = strlen(dirname); 595 case EPERM:
576 while ((len > 0) && (dirname[len] != DIR_SEPARATOR)) 596 /* these are normal and can just be ignored */
577 len--; 597 break;
578 dirname[len] = '\0'; 598
579 if ((0 != len) && (0 != rmdir(dirname))) 599 default:
580 { 600 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
581 switch (errno) 601 "rmdir",
582 { 602 dirname);
583 case EACCES: 603 break;
584 case ENOTEMPTY:
585 case EPERM:
586 /* these are normal and can just be ignored */
587 break;
588
589 default:
590 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
591 "rmdir",
592 dirname);
593 break;
594 }
595 }
596 } 604 }
597 GNUNET_free(dirname); 605 }
598 } 606 }
599 GNUNET_NETWORK_socket_free_memory_only_(desc); 607 GNUNET_free (dirname);
608 }
609 GNUNET_NETWORK_socket_free_memory_only_ (desc);
600 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR; 610 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
601} 611}
602 612
@@ -607,10 +617,10 @@ GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
607 * @param desc socket 617 * @param desc socket
608 */ 618 */
609void 619void
610GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc) 620GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
611{ 621{
612 GNUNET_free_non_null(desc->addr); 622 GNUNET_free_non_null (desc->addr);
613 GNUNET_free(desc); 623 GNUNET_free (desc);
614} 624}
615 625
616 626
@@ -621,13 +631,13 @@ GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
621 * @return NULL on error (including not supported on target platform) 631 * @return NULL on error (including not supported on target platform)
622 */ 632 */
623struct GNUNET_NETWORK_Handle * 633struct GNUNET_NETWORK_Handle *
624GNUNET_NETWORK_socket_box_native(SOCKTYPE fd) 634GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
625{ 635{
626 struct GNUNET_NETWORK_Handle *ret; 636 struct GNUNET_NETWORK_Handle *ret;
627 637
628 if (fcntl(fd, F_GETFD) < 0) 638 if (fcntl (fd, F_GETFD) < 0)
629 return NULL; /* invalid FD */ 639 return NULL; /* invalid FD */
630 ret = GNUNET_new(struct GNUNET_NETWORK_Handle); 640 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
631 ret->fd = fd; 641 ret->fd = fd;
632 ret->af = AF_UNSPEC; 642 ret->af = AF_UNSPEC;
633 return ret; 643 return ret;
@@ -643,15 +653,15 @@ GNUNET_NETWORK_socket_box_native(SOCKTYPE fd)
643 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 653 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
644 */ 654 */
645int 655int
646GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, 656GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
647 const struct sockaddr *address, 657 const struct sockaddr *address,
648 socklen_t address_len) 658 socklen_t address_len)
649{ 659{
650 int ret; 660 int ret;
651 661
652 ret = connect(desc->fd, 662 ret = connect (desc->fd,
653 address, 663 address,
654 address_len); 664 address_len);
655 665
656 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 666 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
657} 667}
@@ -668,18 +678,18 @@ GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc,
668 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 678 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
669 */ 679 */
670int 680int
671GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, 681GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
672 int level, 682 int level,
673 int optname, 683 int optname,
674 void *optval, 684 void *optval,
675 socklen_t *optlen) 685 socklen_t *optlen)
676{ 686{
677 int ret; 687 int ret;
678 688
679 ret = getsockopt(desc->fd, 689 ret = getsockopt (desc->fd,
680 level, 690 level,
681 optname, 691 optname,
682 optval, optlen); 692 optval, optlen);
683 693
684 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 694 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
685} 695}
@@ -693,13 +703,13 @@ GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc,
693 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 703 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
694 */ 704 */
695int 705int
696GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, 706GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
697 int backlog) 707 int backlog)
698{ 708{
699 int ret; 709 int ret;
700 710
701 ret = listen(desc->fd, 711 ret = listen (desc->fd,
702 backlog); 712 backlog);
703 713
704 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 714 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
705} 715}
@@ -712,18 +722,18 @@ GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc,
712 * @returns #GNUNET_SYSERR if no data is available, or on error! 722 * @returns #GNUNET_SYSERR if no data is available, or on error!
713 */ 723 */
714ssize_t 724ssize_t
715GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc) 725GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
716{ 726{
717 int error; 727 int error;
718 728
719 /* How much is there to be read? */ 729 /* How much is there to be read? */
720 int pending; 730 int pending;
721 731
722 error = ioctl(desc->fd, 732 error = ioctl (desc->fd,
723 FIONREAD, 733 FIONREAD,
724 &pending); 734 &pending);
725 if (0 == error) 735 if (0 == error)
726 return (ssize_t)pending; 736 return (ssize_t) pending;
727 return GNUNET_SYSERR; 737 return GNUNET_SYSERR;
728} 738}
729 739
@@ -739,11 +749,11 @@ GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
739 * @param addrlen length of the @a src_addr 749 * @param addrlen length of the @a src_addr
740 */ 750 */
741ssize_t 751ssize_t
742GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc, 752GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
743 void *buffer, 753 void *buffer,
744 size_t length, 754 size_t length,
745 struct sockaddr *src_addr, 755 struct sockaddr *src_addr,
746 socklen_t *addrlen) 756 socklen_t *addrlen)
747{ 757{
748 int ret; 758 int ret;
749 int flags; 759 int flags;
@@ -753,12 +763,12 @@ GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc,
753#ifdef MSG_DONTWAIT 763#ifdef MSG_DONTWAIT
754 flags |= MSG_DONTWAIT; 764 flags |= MSG_DONTWAIT;
755#endif 765#endif
756 ret = recvfrom(desc->fd, 766 ret = recvfrom (desc->fd,
757 buffer, 767 buffer,
758 length, 768 length,
759 flags, 769 flags,
760 src_addr, 770 src_addr,
761 addrlen); 771 addrlen);
762 return ret; 772 return ret;
763} 773}
764 774
@@ -772,9 +782,9 @@ GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc,
772 * @return number of bytes received, -1 on error 782 * @return number of bytes received, -1 on error
773 */ 783 */
774ssize_t 784ssize_t
775GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, 785GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
776 void *buffer, 786 void *buffer,
777 size_t length) 787 size_t length)
778{ 788{
779 int ret; 789 int ret;
780 int flags; 790 int flags;
@@ -784,10 +794,10 @@ GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc,
784#ifdef MSG_DONTWAIT 794#ifdef MSG_DONTWAIT
785 flags |= MSG_DONTWAIT; 795 flags |= MSG_DONTWAIT;
786#endif 796#endif
787 ret = recv(desc->fd, 797 ret = recv (desc->fd,
788 buffer, 798 buffer,
789 length, 799 length,
790 flags); 800 flags);
791 return ret; 801 return ret;
792} 802}
793 803
@@ -801,9 +811,9 @@ GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc,
801 * @return number of bytes sent, #GNUNET_SYSERR on error 811 * @return number of bytes sent, #GNUNET_SYSERR on error
802 */ 812 */
803ssize_t 813ssize_t
804GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, 814GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
805 const void *buffer, 815 const void *buffer,
806 size_t length) 816 size_t length)
807{ 817{
808 int ret; 818 int ret;
809 int flags; 819 int flags;
@@ -815,10 +825,10 @@ GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc,
815#ifdef MSG_NOSIGNAL 825#ifdef MSG_NOSIGNAL
816 flags |= MSG_NOSIGNAL; 826 flags |= MSG_NOSIGNAL;
817#endif 827#endif
818 ret = send(desc->fd, 828 ret = send (desc->fd,
819 buffer, 829 buffer,
820 length, 830 length,
821 flags); 831 flags);
822 return ret; 832 return ret;
823} 833}
824 834
@@ -835,11 +845,11 @@ GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc,
835 * @return number of bytes sent, #GNUNET_SYSERR on error 845 * @return number of bytes sent, #GNUNET_SYSERR on error
836 */ 846 */
837ssize_t 847ssize_t
838GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc, 848GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
839 const void *message, 849 const void *message,
840 size_t length, 850 size_t length,
841 const struct sockaddr *dest_addr, 851 const struct sockaddr *dest_addr,
842 socklen_t dest_len) 852 socklen_t dest_len)
843{ 853{
844 int ret; 854 int ret;
845 int flags; 855 int flags;
@@ -852,7 +862,7 @@ GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc,
852#ifdef MSG_NOSIGNAL 862#ifdef MSG_NOSIGNAL
853 flags |= MSG_NOSIGNAL; 863 flags |= MSG_NOSIGNAL;
854#endif 864#endif
855 ret = sendto(desc->fd, message, length, flags, dest_addr, dest_len); 865 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
856 return ret; 866 return ret;
857} 867}
858 868
@@ -868,19 +878,19 @@ GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc,
868 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 878 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
869 */ 879 */
870int 880int
871GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, 881GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
872 int level, 882 int level,
873 int option_name, 883 int option_name,
874 const void *option_value, 884 const void *option_value,
875 socklen_t option_len) 885 socklen_t option_len)
876{ 886{
877 int ret; 887 int ret;
878 888
879 ret = setsockopt(fd->fd, 889 ret = setsockopt (fd->fd,
880 level, 890 level,
881 option_name, 891 option_name,
882 option_value, 892 option_value,
883 option_len); 893 option_len);
884 894
885 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 895 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
886} 896}
@@ -897,22 +907,22 @@ GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd,
897 * @return new socket, NULL on error 907 * @return new socket, NULL on error
898 */ 908 */
899struct GNUNET_NETWORK_Handle * 909struct GNUNET_NETWORK_Handle *
900GNUNET_NETWORK_socket_create(int domain, 910GNUNET_NETWORK_socket_create (int domain,
901 int type, 911 int type,
902 int protocol) 912 int protocol)
903{ 913{
904 struct GNUNET_NETWORK_Handle *ret; 914 struct GNUNET_NETWORK_Handle *ret;
905 int fd; 915 int fd;
906 916
907 fd = socket(domain, type, protocol); 917 fd = socket (domain, type, protocol);
908 if (-1 == fd) 918 if (-1 == fd)
909 return NULL; 919 return NULL;
910 ret = GNUNET_new(struct GNUNET_NETWORK_Handle); 920 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
911 ret->fd = fd; 921 ret->fd = fd;
912 if (GNUNET_OK != 922 if (GNUNET_OK !=
913 initialize_network_handle(ret, 923 initialize_network_handle (ret,
914 domain, 924 domain,
915 type)) 925 type))
916 return NULL; 926 return NULL;
917 return ret; 927 return ret;
918} 928}
@@ -925,12 +935,12 @@ GNUNET_NETWORK_socket_create(int domain,
925 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 935 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
926 */ 936 */
927int 937int
928GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, 938GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
929 int how) 939 int how)
930{ 940{
931 int ret; 941 int ret;
932 942
933 ret = shutdown(desc->fd, how); 943 ret = shutdown (desc->fd, how);
934 944
935 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR; 945 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
936} 946}
@@ -946,7 +956,7 @@ GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc,
946 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 956 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
947 */ 957 */
948int 958int
949GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc) 959GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
950{ 960{
951 int ret = 0; 961 int ret = 0;
952 962
@@ -955,22 +965,22 @@ GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
955 965
956 if (0 != 966 if (0 !=
957 (ret = 967 (ret =
958 setsockopt(desc->fd, 968 setsockopt (desc->fd,
959 SOL_SOCKET, 969 SOL_SOCKET,
960 SO_SNDBUF, 970 SO_SNDBUF,
961 &value, 971 &value,
962 sizeof(value)))) 972 sizeof(value))))
963 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 973 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
964 "setsockopt"); 974 "setsockopt");
965 if (0 != 975 if (0 !=
966 (ret = 976 (ret =
967 setsockopt(desc->fd, 977 setsockopt (desc->fd,
968 SOL_SOCKET, 978 SOL_SOCKET,
969 SO_RCVBUF, 979 SO_RCVBUF,
970 &value, 980 &value,
971 sizeof(value)))) 981 sizeof(value))))
972 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 982 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
973 "setsockopt"); 983 "setsockopt");
974#endif 984#endif
975 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 985 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
976} 986}
@@ -982,9 +992,9 @@ GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
982 * @param fds fd set 992 * @param fds fd set
983 */ 993 */
984void 994void
985GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds) 995GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
986{ 996{
987 FD_ZERO(&fds->sds); 997 FD_ZERO (&fds->sds);
988 fds->nsds = 0; 998 fds->nsds = 0;
989} 999}
990 1000
@@ -996,13 +1006,13 @@ GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
996 * @param desc socket to add 1006 * @param desc socket to add
997 */ 1007 */
998void 1008void
999GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds, 1009GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1000 const struct GNUNET_NETWORK_Handle *desc) 1010 const struct GNUNET_NETWORK_Handle *desc)
1001{ 1011{
1002 FD_SET(desc->fd, 1012 FD_SET (desc->fd,
1003 &fds->sds); 1013 &fds->sds);
1004 fds->nsds = GNUNET_MAX(fds->nsds, 1014 fds->nsds = GNUNET_MAX (fds->nsds,
1005 desc->fd + 1); 1015 desc->fd + 1);
1006} 1016}
1007 1017
1008 1018
@@ -1014,11 +1024,11 @@ GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
1014 * @return 0 if the FD is not set 1024 * @return 0 if the FD is not set
1015 */ 1025 */
1016int 1026int
1017GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, 1027GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1018 const struct GNUNET_NETWORK_Handle *desc) 1028 const struct GNUNET_NETWORK_Handle *desc)
1019{ 1029{
1020 return FD_ISSET(desc->fd, 1030 return FD_ISSET (desc->fd,
1021 &fds->sds); 1031 &fds->sds);
1022} 1032}
1023 1033
1024 1034
@@ -1029,16 +1039,16 @@ GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
1029 * @param src the fd set to add from 1039 * @param src the fd set to add from
1030 */ 1040 */
1031void 1041void
1032GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst, 1042GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1033 const struct GNUNET_NETWORK_FDSet *src) 1043 const struct GNUNET_NETWORK_FDSet *src)
1034{ 1044{
1035 int nfds; 1045 int nfds;
1036 1046
1037 for (nfds = src->nsds; nfds >= 0; nfds--) 1047 for (nfds = src->nsds; nfds >= 0; nfds--)
1038 if (FD_ISSET(nfds, &src->sds)) 1048 if (FD_ISSET (nfds, &src->sds))
1039 FD_SET(nfds, &dst->sds); 1049 FD_SET (nfds, &dst->sds);
1040 dst->nsds = GNUNET_MAX(dst->nsds, 1050 dst->nsds = GNUNET_MAX (dst->nsds,
1041 src->nsds); 1051 src->nsds);
1042} 1052}
1043 1053
1044 1054
@@ -1049,11 +1059,11 @@ GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst,
1049 * @param from source 1059 * @param from source
1050 */ 1060 */
1051void 1061void
1052GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to, 1062GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1053 const struct GNUNET_NETWORK_FDSet *from) 1063 const struct GNUNET_NETWORK_FDSet *from)
1054{ 1064{
1055 FD_COPY(&from->sds, 1065 FD_COPY (&from->sds,
1056 &to->sds); 1066 &to->sds);
1057 to->nsds = from->nsds; 1067 to->nsds = from->nsds;
1058} 1068}
1059 1069
@@ -1065,7 +1075,7 @@ GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
1065 * @return POSIX file descriptor 1075 * @return POSIX file descriptor
1066 */ 1076 */
1067int 1077int
1068GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc) 1078GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1069{ 1079{
1070 return desc->fd; 1080 return desc->fd;
1071} 1081}
@@ -1078,7 +1088,7 @@ GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
1078 * @return sockaddr 1088 * @return sockaddr
1079 */ 1089 */
1080struct sockaddr* 1090struct sockaddr*
1081GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc) 1091GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1082{ 1092{
1083 return desc->addr; 1093 return desc->addr;
1084} 1094}
@@ -1091,7 +1101,7 @@ GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc)
1091 * @return socklen_t for sockaddr 1101 * @return socklen_t for sockaddr
1092 */ 1102 */
1093socklen_t 1103socklen_t
1094GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc) 1104GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1095{ 1105{
1096 return desc->addrlen; 1106 return desc->addrlen;
1097} 1107}
@@ -1105,12 +1115,12 @@ GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc)
1105 * @param nfds the biggest socket number in from + 1 1115 * @param nfds the biggest socket number in from + 1
1106 */ 1116 */
1107void 1117void
1108GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to, 1118GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1109 const fd_set *from, 1119 const fd_set *from,
1110 int nfds) 1120 int nfds)
1111{ 1121{
1112 FD_COPY(from, 1122 FD_COPY (from,
1113 &to->sds); 1123 &to->sds);
1114 to->nsds = nfds; 1124 to->nsds = nfds;
1115} 1125}
1116 1126
@@ -1122,13 +1132,13 @@ GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to,
1122 * @param nfd native FD to set 1132 * @param nfd native FD to set
1123 */ 1133 */
1124void 1134void
1125GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to, 1135GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1126 int nfd) 1136 int nfd)
1127{ 1137{
1128 GNUNET_assert((nfd >= 0) && (nfd < FD_SETSIZE)); 1138 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1129 FD_SET(nfd, &to->sds); 1139 FD_SET (nfd, &to->sds);
1130 to->nsds = GNUNET_MAX(nfd + 1, 1140 to->nsds = GNUNET_MAX (nfd + 1,
1131 to->nsds); 1141 to->nsds);
1132} 1142}
1133 1143
1134 1144
@@ -1140,13 +1150,13 @@ GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to,
1140 * @return #GNUNET_YES if FD is set in the set 1150 * @return #GNUNET_YES if FD is set in the set
1141 */ 1151 */
1142int 1152int
1143GNUNET_NETWORK_fdset_test_native(const struct GNUNET_NETWORK_FDSet *to, 1153GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1144 int nfd) 1154 int nfd)
1145{ 1155{
1146 if ((-1 == nfd) || 1156 if ((-1 == nfd) ||
1147 (NULL == to)) 1157 (NULL == to))
1148 return GNUNET_NO; 1158 return GNUNET_NO;
1149 return FD_ISSET(nfd, &to->sds) ? GNUNET_YES : GNUNET_NO; 1159 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1150} 1160}
1151 1161
1152 1162
@@ -1156,19 +1166,19 @@ GNUNET_NETWORK_fdset_test_native(const struct GNUNET_NETWORK_FDSet *to,
1156 * @param h the file handle to add 1166 * @param h the file handle to add
1157 */ 1167 */
1158void 1168void
1159GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, 1169GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1160 const struct GNUNET_DISK_FileHandle *h) 1170 const struct GNUNET_DISK_FileHandle *h)
1161{ 1171{
1162 int fd; 1172 int fd;
1163 1173
1164 GNUNET_assert(GNUNET_OK == 1174 GNUNET_assert (GNUNET_OK ==
1165 GNUNET_DISK_internal_file_handle_(h, 1175 GNUNET_DISK_internal_file_handle_ (h,
1166 &fd, 1176 &fd,
1167 sizeof(int))); 1177 sizeof(int)));
1168 FD_SET(fd, 1178 FD_SET (fd,
1169 &fds->sds); 1179 &fds->sds);
1170 fds->nsds = GNUNET_MAX(fd + 1, 1180 fds->nsds = GNUNET_MAX (fd + 1,
1171 fds->nsds); 1181 fds->nsds);
1172} 1182}
1173 1183
1174 1184
@@ -1178,10 +1188,10 @@ GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds,
1178 * @param h the file handle to add 1188 * @param h the file handle to add
1179 */ 1189 */
1180void 1190void
1181GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds, 1191GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1182 const struct GNUNET_DISK_FileHandle *h) 1192 const struct GNUNET_DISK_FileHandle *h)
1183{ 1193{
1184 GNUNET_NETWORK_fdset_handle_set(fds, h); 1194 GNUNET_NETWORK_fdset_handle_set (fds, h);
1185} 1195}
1186 1196
1187 1197
@@ -1193,11 +1203,11 @@ GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds,
1193 * @return #GNUNET_YES if the file handle is part of the set 1203 * @return #GNUNET_YES if the file handle is part of the set
1194 */ 1204 */
1195int 1205int
1196GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds, 1206GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1197 const struct GNUNET_DISK_FileHandle *h) 1207 const struct GNUNET_DISK_FileHandle *h)
1198{ 1208{
1199 return FD_ISSET(h->fd, 1209 return FD_ISSET (h->fd,
1200 &fds->sds); 1210 &fds->sds);
1201} 1211}
1202 1212
1203 1213
@@ -1209,22 +1219,22 @@ GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds,
1209 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise 1219 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1210 */ 1220 */
1211int 1221int
1212GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1, 1222GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1213 const struct GNUNET_NETWORK_FDSet *fds2) 1223 const struct GNUNET_NETWORK_FDSet *fds2)
1214{ 1224{
1215 int nfds; 1225 int nfds;
1216 1226
1217 nfds = GNUNET_MIN(fds1->nsds, 1227 nfds = GNUNET_MIN (fds1->nsds,
1218 fds2->nsds); 1228 fds2->nsds);
1219 while (nfds > 0) 1229 while (nfds > 0)
1220 { 1230 {
1221 nfds--; 1231 nfds--;
1222 if ((FD_ISSET(nfds, 1232 if ((FD_ISSET (nfds,
1223 &fds1->sds)) && 1233 &fds1->sds)) &&
1224 (FD_ISSET(nfds, 1234 (FD_ISSET (nfds,
1225 &fds2->sds))) 1235 &fds2->sds)))
1226 return GNUNET_YES; 1236 return GNUNET_YES;
1227 } 1237 }
1228 return GNUNET_NO; 1238 return GNUNET_NO;
1229} 1239}
1230 1240
@@ -1235,12 +1245,12 @@ GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1,
1235 * @return a new fd set 1245 * @return a new fd set
1236 */ 1246 */
1237struct GNUNET_NETWORK_FDSet * 1247struct GNUNET_NETWORK_FDSet *
1238GNUNET_NETWORK_fdset_create() 1248GNUNET_NETWORK_fdset_create ()
1239{ 1249{
1240 struct GNUNET_NETWORK_FDSet *fds; 1250 struct GNUNET_NETWORK_FDSet *fds;
1241 1251
1242 fds = GNUNET_new(struct GNUNET_NETWORK_FDSet); 1252 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1243 GNUNET_NETWORK_fdset_zero(fds); 1253 GNUNET_NETWORK_fdset_zero (fds);
1244 return fds; 1254 return fds;
1245} 1255}
1246 1256
@@ -1251,9 +1261,9 @@ GNUNET_NETWORK_fdset_create()
1251 * @param fds fd set 1261 * @param fds fd set
1252 */ 1262 */
1253void 1263void
1254GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds) 1264GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1255{ 1265{
1256 GNUNET_free(fds); 1266 GNUNET_free (fds);
1257} 1267}
1258 1268
1259 1269
@@ -1265,8 +1275,8 @@ GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
1265 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not 1275 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1266 */ 1276 */
1267int 1277int
1268GNUNET_NETWORK_test_port_free(int ipproto, 1278GNUNET_NETWORK_test_port_free (int ipproto,
1269 uint16_t port) 1279 uint16_t port)
1270{ 1280{
1271 struct GNUNET_NETWORK_Handle *socket; 1281 struct GNUNET_NETWORK_Handle *socket;
1272 int bind_status; 1282 int bind_status;
@@ -1276,13 +1286,13 @@ GNUNET_NETWORK_test_port_free(int ipproto,
1276 struct addrinfo *ret; 1286 struct addrinfo *ret;
1277 struct addrinfo *ai; 1287 struct addrinfo *ai;
1278 1288
1279 GNUNET_snprintf(open_port_str, 1289 GNUNET_snprintf (open_port_str,
1280 sizeof(open_port_str), 1290 sizeof(open_port_str),
1281 "%u", 1291 "%u",
1282 (unsigned int)port); 1292 (unsigned int) port);
1283 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM; 1293 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1284 ret = NULL; 1294 ret = NULL;
1285 memset(&hint, 0, sizeof(hint)); 1295 memset (&hint, 0, sizeof(hint));
1286 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */ 1296 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1287 hint.ai_socktype = socktype; 1297 hint.ai_socktype = socktype;
1288 hint.ai_protocol = ipproto; 1298 hint.ai_protocol = ipproto;
@@ -1291,26 +1301,26 @@ GNUNET_NETWORK_test_port_free(int ipproto,
1291 hint.ai_canonname = NULL; 1301 hint.ai_canonname = NULL;
1292 hint.ai_next = NULL; 1302 hint.ai_next = NULL;
1293 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */ 1303 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1294 GNUNET_assert(0 == getaddrinfo(NULL, 1304 GNUNET_assert (0 == getaddrinfo (NULL,
1295 open_port_str, 1305 open_port_str,
1296 &hint, 1306 &hint,
1297 &ret)); 1307 &ret));
1298 bind_status = GNUNET_NO; 1308 bind_status = GNUNET_NO;
1299 for (ai = ret; NULL != ai; ai = ai->ai_next) 1309 for (ai = ret; NULL != ai; ai = ai->ai_next)
1300 { 1310 {
1301 socket = GNUNET_NETWORK_socket_create(ai->ai_family, 1311 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1302 ai->ai_socktype, 1312 ai->ai_socktype,
1303 ai->ai_protocol); 1313 ai->ai_protocol);
1304 if (NULL == socket) 1314 if (NULL == socket)
1305 continue; 1315 continue;
1306 bind_status = GNUNET_NETWORK_socket_bind(socket, 1316 bind_status = GNUNET_NETWORK_socket_bind (socket,
1307 ai->ai_addr, 1317 ai->ai_addr,
1308 ai->ai_addrlen); 1318 ai->ai_addrlen);
1309 GNUNET_NETWORK_socket_close(socket); 1319 GNUNET_NETWORK_socket_close (socket);
1310 if (GNUNET_OK != bind_status) 1320 if (GNUNET_OK != bind_status)
1311 break; 1321 break;
1312 } 1322 }
1313 freeaddrinfo(ret); 1323 freeaddrinfo (ret);
1314 return bind_status; 1324 return bind_status;
1315} 1325}
1316 1326
@@ -1325,10 +1335,10 @@ GNUNET_NETWORK_test_port_free(int ipproto,
1325 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error 1335 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1326 */ 1336 */
1327int 1337int
1328GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds, 1338GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1329 struct GNUNET_NETWORK_FDSet *wfds, 1339 struct GNUNET_NETWORK_FDSet *wfds,
1330 struct GNUNET_NETWORK_FDSet *efds, 1340 struct GNUNET_NETWORK_FDSet *efds,
1331 const struct GNUNET_TIME_Relative timeout) 1341 const struct GNUNET_TIME_Relative timeout)
1332{ 1342{
1333 int nfds; 1343 int nfds;
1334 struct timeval tv; 1344 struct timeval tv;
@@ -1338,37 +1348,41 @@ GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds,
1338 else 1348 else
1339 nfds = 0; 1349 nfds = 0;
1340 if (NULL != wfds) 1350 if (NULL != wfds)
1341 nfds = GNUNET_MAX(nfds, 1351 nfds = GNUNET_MAX (nfds,
1342 wfds->nsds); 1352 wfds->nsds);
1343 if (NULL != efds) 1353 if (NULL != efds)
1344 nfds = GNUNET_MAX(nfds, 1354 nfds = GNUNET_MAX (nfds,
1345 efds->nsds); 1355 efds->nsds);
1346 if ((0 == nfds) && 1356 if ((0 == nfds) &&
1347 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)) 1357 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1348 { 1358 {
1349 GNUNET_break(0); 1359 GNUNET_break (0);
1350 LOG(GNUNET_ERROR_TYPE_ERROR, 1360 LOG (GNUNET_ERROR_TYPE_ERROR,
1351 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 1361 _ (
1352 "select"); 1362 "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1353 } 1363 "select");
1354 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long)LONG_MAX) 1364 }
1355 { 1365 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1356 tv.tv_sec = LONG_MAX; 1366 long long)
1357 tv.tv_usec = 999999L; 1367 LONG_MAX)
1358 } 1368 {
1369 tv.tv_sec = LONG_MAX;
1370 tv.tv_usec = 999999L;
1371 }
1359 else 1372 else
1360 { 1373 {
1361 tv.tv_sec = (long)(timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us); 1374 tv.tv_sec = (long) (timeout.rel_value_us
1362 tv.tv_usec = 1375 / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1363 (timeout.rel_value_us - 1376 tv.tv_usec =
1364 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us)); 1377 (timeout.rel_value_us
1365 } 1378 - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1366 return select(nfds, 1379 }
1367 (NULL != rfds) ? &rfds->sds : NULL, 1380 return select (nfds,
1368 (NULL != wfds) ? &wfds->sds : NULL, 1381 (NULL != rfds) ? &rfds->sds : NULL,
1369 (NULL != efds) ? &efds->sds : NULL, 1382 (NULL != wfds) ? &wfds->sds : NULL,
1370 (timeout.rel_value_us == 1383 (NULL != efds) ? &efds->sds : NULL,
1371 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv); 1384 (timeout.rel_value_us ==
1385 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1372} 1386}
1373 1387
1374/* end of network.c */ 1388/* end of network.c */
diff --git a/src/util/op.c b/src/util/op.c
index 20a0d3b0d..647fedb53 100644
--- a/src/util/op.c
+++ b/src/util/op.c
@@ -30,9 +30,10 @@
30#include "platform.h" 30#include "platform.h"
31#include "gnunet_util_lib.h" 31#include "gnunet_util_lib.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from(kind, "util-op", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "util-op", __VA_ARGS__)
34 34
35struct OperationListItem { 35struct OperationListItem
36{
36 struct OperationListItem *prev; 37 struct OperationListItem *prev;
37 struct OperationListItem *next; 38 struct OperationListItem *next;
38 39
@@ -62,7 +63,8 @@ struct OperationListItem {
62 * Operations handle. 63 * Operations handle.
63 */ 64 */
64 65
65struct GNUNET_OP_Handle { 66struct GNUNET_OP_Handle
67{
66 /** 68 /**
67 * First operation in the linked list. 69 * First operation in the linked list.
68 */ 70 */
@@ -84,9 +86,9 @@ struct GNUNET_OP_Handle {
84 * Create new operations handle. 86 * Create new operations handle.
85 */ 87 */
86struct GNUNET_OP_Handle * 88struct GNUNET_OP_Handle *
87GNUNET_OP_create() 89GNUNET_OP_create ()
88{ 90{
89 return GNUNET_new(struct GNUNET_OP_Handle); 91 return GNUNET_new (struct GNUNET_OP_Handle);
90} 92}
91 93
92 94
@@ -94,9 +96,9 @@ GNUNET_OP_create()
94 * Destroy operations handle. 96 * Destroy operations handle.
95 */ 97 */
96void 98void
97GNUNET_OP_destroy(struct GNUNET_OP_Handle *h) 99GNUNET_OP_destroy (struct GNUNET_OP_Handle *h)
98{ 100{
99 GNUNET_free(h); 101 GNUNET_free (h);
100} 102}
101 103
102 104
@@ -109,7 +111,7 @@ GNUNET_OP_destroy(struct GNUNET_OP_Handle *h)
109 * @return Operation ID to use. 111 * @return Operation ID to use.
110 */ 112 */
111uint64_t 113uint64_t
112GNUNET_OP_get_next_id(struct GNUNET_OP_Handle *h) 114GNUNET_OP_get_next_id (struct GNUNET_OP_Handle *h)
113{ 115{
114 return ++h->last_op_id; 116 return ++h->last_op_id;
115} 117}
@@ -126,8 +128,8 @@ GNUNET_OP_get_next_id(struct GNUNET_OP_Handle *h)
126 * @return Operation, or NULL if not found. 128 * @return Operation, or NULL if not found.
127 */ 129 */
128static struct OperationListItem * 130static struct OperationListItem *
129op_find(struct GNUNET_OP_Handle *h, 131op_find (struct GNUNET_OP_Handle *h,
130 uint64_t op_id) 132 uint64_t op_id)
131{ 133{
132 struct OperationListItem *op; 134 struct OperationListItem *op;
133 135
@@ -156,24 +158,24 @@ op_find(struct GNUNET_OP_Handle *h,
156 * #GNUNET_NO if not found. 158 * #GNUNET_NO if not found.
157 */ 159 */
158int 160int
159GNUNET_OP_get(struct GNUNET_OP_Handle *h, 161GNUNET_OP_get (struct GNUNET_OP_Handle *h,
160 uint64_t op_id, 162 uint64_t op_id,
161 GNUNET_ResultCallback *result_cb, 163 GNUNET_ResultCallback *result_cb,
162 void **cls, 164 void **cls,
163 void **ctx) 165 void **ctx)
164{ 166{
165 struct OperationListItem *op = op_find(h, op_id); 167 struct OperationListItem *op = op_find (h, op_id);
166 168
167 if (NULL != op) 169 if (NULL != op)
168 { 170 {
169 if (NULL != result_cb) 171 if (NULL != result_cb)
170 *result_cb = op->result_cb; 172 *result_cb = op->result_cb;
171 if (NULL != cls) 173 if (NULL != cls)
172 *cls = op->cls; 174 *cls = op->cls;
173 if (NULL != ctx) 175 if (NULL != ctx)
174 *ctx = op->ctx; 176 *ctx = op->ctx;
175 return GNUNET_YES; 177 return GNUNET_YES;
176 } 178 }
177 return GNUNET_NO; 179 return GNUNET_NO;
178} 180}
179 181
@@ -193,24 +195,24 @@ GNUNET_OP_get(struct GNUNET_OP_Handle *h,
193 * @return ID of the new operation. 195 * @return ID of the new operation.
194 */ 196 */
195uint64_t 197uint64_t
196GNUNET_OP_add(struct GNUNET_OP_Handle *h, 198GNUNET_OP_add (struct GNUNET_OP_Handle *h,
197 GNUNET_ResultCallback result_cb, 199 GNUNET_ResultCallback result_cb,
198 void *cls, 200 void *cls,
199 void *ctx) 201 void *ctx)
200{ 202{
201 struct OperationListItem *op; 203 struct OperationListItem *op;
202 204
203 op = GNUNET_new(struct OperationListItem); 205 op = GNUNET_new (struct OperationListItem);
204 op->op_id = GNUNET_OP_get_next_id(h); 206 op->op_id = GNUNET_OP_get_next_id (h);
205 op->result_cb = result_cb; 207 op->result_cb = result_cb;
206 op->cls = cls; 208 op->cls = cls;
207 op->ctx = ctx; 209 op->ctx = ctx;
208 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, 210 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
209 h->op_tail, 211 h->op_tail,
210 op); 212 op);
211 LOG(GNUNET_ERROR_TYPE_DEBUG, 213 LOG (GNUNET_ERROR_TYPE_DEBUG,
212 "%p Added operation #%" PRIu64 "\n", 214 "%p Added operation #%" PRIu64 "\n",
213 h, op->op_id); 215 h, op->op_id);
214 return op->op_id; 216 return op->op_id;
215} 217}
216 218
@@ -240,38 +242,38 @@ GNUNET_OP_add(struct GNUNET_OP_Handle *h,
240 * #GNUNET_NO if the operation was not found. 242 * #GNUNET_NO if the operation was not found.
241 */ 243 */
242static int 244static int
243op_result(struct GNUNET_OP_Handle *h, 245op_result (struct GNUNET_OP_Handle *h,
244 uint64_t op_id, 246 uint64_t op_id,
245 int64_t result_code, 247 int64_t result_code,
246 const void *data, 248 const void *data,
247 uint16_t data_size, 249 uint16_t data_size,
248 void **ctx, 250 void **ctx,
249 uint8_t cancel) 251 uint8_t cancel)
250{ 252{
251 if (0 == op_id) 253 if (0 == op_id)
252 return GNUNET_NO; 254 return GNUNET_NO;
253 255
254 struct OperationListItem *op = op_find(h, op_id); 256 struct OperationListItem *op = op_find (h, op_id);
255 if (NULL == op) 257 if (NULL == op)
256 { 258 {
257 LOG(GNUNET_ERROR_TYPE_WARNING, 259 LOG (GNUNET_ERROR_TYPE_WARNING,
258 "Could not find operation #%" PRIu64 "\n", op_id); 260 "Could not find operation #%" PRIu64 "\n", op_id);
259 return GNUNET_NO; 261 return GNUNET_NO;
260 } 262 }
261 263
262 if (NULL != ctx) 264 if (NULL != ctx)
263 *ctx = op->ctx; 265 *ctx = op->ctx;
264 266
265 GNUNET_CONTAINER_DLL_remove(h->op_head, 267 GNUNET_CONTAINER_DLL_remove (h->op_head,
266 h->op_tail, 268 h->op_tail,
267 op); 269 op);
268 270
269 if ((GNUNET_YES != cancel) && 271 if ((GNUNET_YES != cancel) &&
270 (NULL != op->result_cb)) 272 (NULL != op->result_cb))
271 op->result_cb(op->cls, 273 op->result_cb (op->cls,
272 result_code, data, 274 result_code, data,
273 data_size); 275 data_size);
274 GNUNET_free(op); 276 GNUNET_free (op);
275 return GNUNET_YES; 277 return GNUNET_YES;
276} 278}
277 279
@@ -296,17 +298,17 @@ op_result(struct GNUNET_OP_Handle *h,
296 * #GNUNET_NO if the operation was not found. 298 * #GNUNET_NO if the operation was not found.
297 */ 299 */
298int 300int
299GNUNET_OP_result(struct GNUNET_OP_Handle *h, 301GNUNET_OP_result (struct GNUNET_OP_Handle *h,
300 uint64_t op_id, 302 uint64_t op_id,
301 int64_t result_code, 303 int64_t result_code,
302 const void *data, 304 const void *data,
303 uint16_t data_size, 305 uint16_t data_size,
304 void **ctx) 306 void **ctx)
305{ 307{
306 LOG(GNUNET_ERROR_TYPE_DEBUG, 308 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 "%p Received result for operation #%" PRIu64 ": %" PRId64 " (size: %u)\n", 309 "%p Received result for operation #%" PRIu64 ": %" PRId64 " (size: %u)\n",
308 h, op_id, result_code, data_size); 310 h, op_id, result_code, data_size);
309 return op_result(h, op_id, result_code, data, data_size, ctx, GNUNET_NO); 311 return op_result (h, op_id, result_code, data, data_size, ctx, GNUNET_NO);
310} 312}
311 313
312 314
@@ -322,11 +324,11 @@ GNUNET_OP_result(struct GNUNET_OP_Handle *h,
322 * #GNUNET_NO if the operation was not found. 324 * #GNUNET_NO if the operation was not found.
323 */ 325 */
324int 326int
325GNUNET_OP_remove(struct GNUNET_OP_Handle *h, 327GNUNET_OP_remove (struct GNUNET_OP_Handle *h,
326 uint64_t op_id) 328 uint64_t op_id)
327{ 329{
328 LOG(GNUNET_ERROR_TYPE_DEBUG, 330 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "%p Cancelling operation #%" PRIu64 "\n", 331 "%p Cancelling operation #%" PRIu64 "\n",
330 h, op_id); 332 h, op_id);
331 return op_result(h, op_id, 0, NULL, 0, NULL, GNUNET_YES); 333 return op_result (h, op_id, 0, NULL, 0, NULL, GNUNET_YES);
332} 334}
diff --git a/src/util/os_installation.c b/src/util/os_installation.c
index c8590c8fb..a4ef199cc 100644
--- a/src/util/os_installation.c
+++ b/src/util/os_installation.c
@@ -43,13 +43,13 @@
43 43
44 44
45#define LOG(kind, ...) \ 45#define LOG(kind, ...) \
46 GNUNET_log_from(kind, "util-os-installation", __VA_ARGS__) 46 GNUNET_log_from (kind, "util-os-installation", __VA_ARGS__)
47 47
48#define LOG_STRERROR_FILE(kind, syscall, filename) \ 48#define LOG_STRERROR_FILE(kind, syscall, filename) \
49 GNUNET_log_from_strerror_file(kind, \ 49 GNUNET_log_from_strerror_file (kind, \
50 "util-os-installation", \ 50 "util-os-installation", \
51 syscall, \ 51 syscall, \
52 filename) 52 filename)
53 53
54 54
55/** 55/**
@@ -89,7 +89,7 @@ static int gettextinit = 0;
89 * Return default project data used by 'libgnunetutil' for GNUnet. 89 * Return default project data used by 'libgnunetutil' for GNUnet.
90 */ 90 */
91const struct GNUNET_OS_ProjectData * 91const struct GNUNET_OS_ProjectData *
92GNUNET_OS_project_data_default(void) 92GNUNET_OS_project_data_default (void)
93{ 93{
94 return &default_pd; 94 return &default_pd;
95} 95}
@@ -99,16 +99,16 @@ GNUNET_OS_project_data_default(void)
99 * @return current project data. 99 * @return current project data.
100 */ 100 */
101const struct GNUNET_OS_ProjectData * 101const struct GNUNET_OS_ProjectData *
102GNUNET_OS_project_data_get() 102GNUNET_OS_project_data_get ()
103{ 103{
104 if (0 == gettextinit) 104 if (0 == gettextinit)
105 { 105 {
106 char *path = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LOCALEDIR); 106 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
107 if (NULL != path) 107 if (NULL != path)
108 bindtextdomain(PACKAGE, path); 108 bindtextdomain (PACKAGE, path);
109 GNUNET_free(path); 109 GNUNET_free (path);
110 gettextinit = 1; 110 gettextinit = 1;
111 } 111 }
112 return current_pd; 112 return current_pd;
113} 113}
114 114
@@ -119,17 +119,17 @@ GNUNET_OS_project_data_get()
119 * @param pd project data used to determine paths 119 * @param pd project data used to determine paths
120 */ 120 */
121void 121void
122GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd) 122GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd)
123{ 123{
124 if (0 == gettextinit) 124 if (0 == gettextinit)
125 { 125 {
126 char *path = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LOCALEDIR); 126 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
127 if (NULL != path) 127 if (NULL != path)
128 bindtextdomain(PACKAGE, path); 128 bindtextdomain (PACKAGE, path);
129 GNUNET_free(path); 129 GNUNET_free (path);
130 gettextinit = 1; 130 gettextinit = 1;
131 } 131 }
132 GNUNET_assert(NULL != pd); 132 GNUNET_assert (NULL != pd);
133 current_pd = pd; 133 current_pd = pd;
134} 134}
135 135
@@ -141,7 +141,7 @@ GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
141 * @return NULL on error 141 * @return NULL on error
142 */ 142 */
143static char * 143static char *
144get_path_from_proc_maps() 144get_path_from_proc_maps ()
145{ 145{
146 char fn[64]; 146 char fn[64];
147 char line[1024]; 147 char line[1024];
@@ -149,22 +149,22 @@ get_path_from_proc_maps()
149 FILE *f; 149 FILE *f;
150 char *lgu; 150 char *lgu;
151 151
152 GNUNET_snprintf(fn, sizeof(fn), "/proc/%u/maps", getpid()); 152 GNUNET_snprintf (fn, sizeof(fn), "/proc/%u/maps", getpid ());
153 if (NULL == (f = fopen(fn, "r"))) 153 if (NULL == (f = fopen (fn, "r")))
154 return NULL; 154 return NULL;
155 while (NULL != fgets(line, sizeof(line), f)) 155 while (NULL != fgets (line, sizeof(line), f))
156 {
157 if ((1 == sscanf (line,
158 "%*x-%*x %*c%*c%*c%*c %*x %*2x:%*2x %*u%*[ ]%1023s",
159 dir)) &&
160 (NULL != (lgu = strstr (dir, current_pd->libname))))
156 { 161 {
157 if ((1 == sscanf(line, 162 lgu[0] = '\0';
158 "%*x-%*x %*c%*c%*c%*c %*x %*2x:%*2x %*u%*[ ]%1023s", 163 fclose (f);
159 dir)) && 164 return GNUNET_strdup (dir);
160 (NULL != (lgu = strstr(dir, current_pd->libname))))
161 {
162 lgu[0] = '\0';
163 fclose(f);
164 return GNUNET_strdup(dir);
165 }
166 } 165 }
167 fclose(f); 166 }
167 fclose (f);
168 return NULL; 168 return NULL;
169} 169}
170 170
@@ -175,37 +175,37 @@ get_path_from_proc_maps()
175 * @return NULL on error 175 * @return NULL on error
176 */ 176 */
177static char * 177static char *
178get_path_from_proc_exe() 178get_path_from_proc_exe ()
179{ 179{
180 char fn[64]; 180 char fn[64];
181 char lnk[1024]; 181 char lnk[1024];
182 ssize_t size; 182 ssize_t size;
183 char *lep; 183 char *lep;
184 184
185 GNUNET_snprintf(fn, sizeof(fn), "/proc/%u/exe", getpid()); 185 GNUNET_snprintf (fn, sizeof(fn), "/proc/%u/exe", getpid ());
186 size = readlink(fn, lnk, sizeof(lnk) - 1); 186 size = readlink (fn, lnk, sizeof(lnk) - 1);
187 if (size <= 0) 187 if (size <= 0)
188 { 188 {
189 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "readlink", fn); 189 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
190 return NULL; 190 return NULL;
191 } 191 }
192 GNUNET_assert(((size_t)size) < sizeof(lnk)); 192 GNUNET_assert (((size_t) size) < sizeof(lnk));
193 lnk[size] = '\0'; 193 lnk[size] = '\0';
194 while ((lnk[size] != '/') && (size > 0)) 194 while ((lnk[size] != '/') && (size > 0))
195 size--; 195 size--;
196 GNUNET_asprintf(&lep, "/%s/libexec/", current_pd->project_dirname); 196 GNUNET_asprintf (&lep, "/%s/libexec/", current_pd->project_dirname);
197 /* test for being in lib/gnunet/libexec/ or lib/MULTIARCH/gnunet/libexec */ 197 /* test for being in lib/gnunet/libexec/ or lib/MULTIARCH/gnunet/libexec */
198 if ((((size_t)size) > strlen(lep)) && 198 if ((((size_t) size) > strlen (lep)) &&
199 (0 == strcmp(lep, &lnk[size - strlen(lep)]))) 199 (0 == strcmp (lep, &lnk[size - strlen (lep)])))
200 size -= strlen(lep) - 1; 200 size -= strlen (lep) - 1;
201 GNUNET_free(lep); 201 GNUNET_free (lep);
202 if ((size < 4) || (lnk[size - 4] != '/')) 202 if ((size < 4) || (lnk[size - 4] != '/'))
203 { 203 {
204 /* not installed in "/bin/" -- binary path probably useless */ 204 /* not installed in "/bin/" -- binary path probably useless */
205 return NULL; 205 return NULL;
206 } 206 }
207 lnk[size] = '\0'; 207 lnk[size] = '\0';
208 return GNUNET_strdup(lnk); 208 return GNUNET_strdup (lnk);
209} 209}
210#endif 210#endif
211 211
@@ -227,7 +227,7 @@ typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t *bufsize);
227 * @return NULL on error 227 * @return NULL on error
228 */ 228 */
229static char * 229static char *
230get_path_from_NSGetExecutablePath() 230get_path_from_NSGetExecutablePath ()
231{ 231{
232 static char zero = '\0'; 232 static char zero = '\0';
233 char *path; 233 char *path;
@@ -236,22 +236,22 @@ get_path_from_NSGetExecutablePath()
236 236
237 path = NULL; 237 path = NULL;
238 if (NULL == 238 if (NULL ==
239 (func = (MyNSGetExecutablePathProto)dlsym(RTLD_DEFAULT, 239 (func = (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT,
240 "_NSGetExecutablePath"))) 240 "_NSGetExecutablePath")))
241 return NULL; 241 return NULL;
242 path = &zero; 242 path = &zero;
243 len = 0; 243 len = 0;
244 /* get the path len, including the trailing \0 */ 244 /* get the path len, including the trailing \0 */
245 (void)func(path, &len); 245 (void) func (path, &len);
246 if (0 == len) 246 if (0 == len)
247 return NULL; 247 return NULL;
248 path = GNUNET_malloc(len); 248 path = GNUNET_malloc (len);
249 if (0 != func(path, &len)) 249 if (0 != func (path, &len))
250 { 250 {
251 GNUNET_free(path); 251 GNUNET_free (path);
252 return NULL; 252 return NULL;
253 } 253 }
254 len = strlen(path); 254 len = strlen (path);
255 while ((path[len] != '/') && (len > 0)) 255 while ((path[len] != '/') && (len > 0))
256 len--; 256 len--;
257 path[len] = '\0'; 257 path[len] = '\0';
@@ -265,7 +265,7 @@ get_path_from_NSGetExecutablePath()
265 * @return NULL on error 265 * @return NULL on error
266 */ 266 */
267static char * 267static char *
268get_path_from_dyld_image() 268get_path_from_dyld_image ()
269{ 269{
270 const char *path; 270 const char *path;
271 char *p; 271 char *p;
@@ -273,23 +273,23 @@ get_path_from_dyld_image()
273 unsigned int i; 273 unsigned int i;
274 int c; 274 int c;
275 275
276 c = _dyld_image_count(); 276 c = _dyld_image_count ();
277 for (i = 0; i < c; i++) 277 for (i = 0; i < c; i++)
278 { 278 {
279 if (((const void *)_dyld_get_image_header(i)) != 279 if (((const void *) _dyld_get_image_header (i)) !=
280 ((const void *)&_mh_dylib_header)) 280 ((const void *) &_mh_dylib_header))
281 continue; 281 continue;
282 path = _dyld_get_image_name(i); 282 path = _dyld_get_image_name (i);
283 if ((NULL == path) || (0 == strlen(path))) 283 if ((NULL == path) || (0 == strlen (path)))
284 continue; 284 continue;
285 p = GNUNET_strdup(path); 285 p = GNUNET_strdup (path);
286 s = p + strlen(p); 286 s = p + strlen (p);
287 while ((s > p) && ('/' != *s)) 287 while ((s > p) && ('/' != *s))
288 s--; 288 s--;
289 s++; 289 s++;
290 *s = '\0'; 290 *s = '\0';
291 return p; 291 return p;
292 } 292 }
293 return NULL; 293 return NULL;
294} 294}
295#endif 295#endif
@@ -303,7 +303,7 @@ get_path_from_dyld_image()
303 * @return path to binary, NULL if not found 303 * @return path to binary, NULL if not found
304 */ 304 */
305static char * 305static char *
306get_path_from_PATH(const char *binary) 306get_path_from_PATH (const char *binary)
307{ 307{
308 char *path; 308 char *path;
309 char *pos; 309 char *pos;
@@ -311,36 +311,36 @@ get_path_from_PATH(const char *binary)
311 char *buf; 311 char *buf;
312 const char *p; 312 const char *p;
313 313
314 if (NULL == (p = getenv("PATH"))) 314 if (NULL == (p = getenv ("PATH")))
315 return NULL; 315 return NULL;
316 316
317 path = GNUNET_strdup(p); /* because we write on it */ 317 path = GNUNET_strdup (p); /* because we write on it */
318 318
319 buf = GNUNET_malloc(strlen(path) + strlen(binary) + 1 + 1); 319 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
320 pos = path; 320 pos = path;
321 while (NULL != (end = strchr(pos, PATH_SEPARATOR))) 321 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
322 { 322 {
323 *end = '\0'; 323 *end = '\0';
324 sprintf(buf, "%s/%s", pos, binary); 324 sprintf (buf, "%s/%s", pos, binary);
325 if (GNUNET_DISK_file_test(buf) == GNUNET_YES) 325 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
326 {
327 pos = GNUNET_strdup(pos);
328 GNUNET_free(buf);
329 GNUNET_free(path);
330 return pos;
331 }
332 pos = end + 1;
333 }
334 sprintf(buf, "%s/%s", pos, binary);
335 if (GNUNET_YES == GNUNET_DISK_file_test(buf))
336 { 326 {
337 pos = GNUNET_strdup(pos); 327 pos = GNUNET_strdup (pos);
338 GNUNET_free(buf); 328 GNUNET_free (buf);
339 GNUNET_free(path); 329 GNUNET_free (path);
340 return pos; 330 return pos;
341 } 331 }
342 GNUNET_free(buf); 332 pos = end + 1;
343 GNUNET_free(path); 333 }
334 sprintf (buf, "%s/%s", pos, binary);
335 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
336 {
337 pos = GNUNET_strdup (pos);
338 GNUNET_free (buf);
339 GNUNET_free (path);
340 return pos;
341 }
342 GNUNET_free (buf);
343 GNUNET_free (path);
344 return NULL; 344 return NULL;
345} 345}
346 346
@@ -352,16 +352,16 @@ get_path_from_PATH(const char *binary)
352 * @return NULL on error (environment variable not set) 352 * @return NULL on error (environment variable not set)
353 */ 353 */
354static char * 354static char *
355get_path_from_GNUNET_PREFIX() 355get_path_from_GNUNET_PREFIX ()
356{ 356{
357 const char *p; 357 const char *p;
358 358
359 if ((NULL != current_pd->env_varname) && 359 if ((NULL != current_pd->env_varname) &&
360 (NULL != (p = getenv(current_pd->env_varname)))) 360 (NULL != (p = getenv (current_pd->env_varname))))
361 return GNUNET_strdup(p); 361 return GNUNET_strdup (p);
362 if ((NULL != current_pd->env_varname_alt) && 362 if ((NULL != current_pd->env_varname_alt) &&
363 (NULL != (p = getenv(current_pd->env_varname_alt)))) 363 (NULL != (p = getenv (current_pd->env_varname_alt))))
364 return GNUNET_strdup(p); 364 return GNUNET_strdup (p);
365 return NULL; 365 return NULL;
366} 366}
367 367
@@ -373,37 +373,37 @@ get_path_from_GNUNET_PREFIX()
373 * @return a pointer to the executable path, or NULL on error 373 * @return a pointer to the executable path, or NULL on error
374 */ 374 */
375static char * 375static char *
376os_get_gnunet_path() 376os_get_gnunet_path ()
377{ 377{
378 char *ret; 378 char *ret;
379 379
380 if (NULL != (ret = get_path_from_GNUNET_PREFIX())) 380 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
381 return ret; 381 return ret;
382#if LINUX 382#if LINUX
383 if (NULL != (ret = get_path_from_proc_maps())) 383 if (NULL != (ret = get_path_from_proc_maps ()))
384 return ret; 384 return ret;
385 /* try path *first*, before /proc/exe, as /proc/exe can be wrong */ 385 /* try path *first*, before /proc/exe, as /proc/exe can be wrong */
386 if ((NULL != current_pd->binary_name) && 386 if ((NULL != current_pd->binary_name) &&
387 (NULL != (ret = get_path_from_PATH(current_pd->binary_name)))) 387 (NULL != (ret = get_path_from_PATH (current_pd->binary_name))))
388 return ret; 388 return ret;
389 if (NULL != (ret = get_path_from_proc_exe())) 389 if (NULL != (ret = get_path_from_proc_exe ()))
390 return ret; 390 return ret;
391#endif 391#endif
392#if DARWIN 392#if DARWIN
393 if (NULL != (ret = get_path_from_dyld_image())) 393 if (NULL != (ret = get_path_from_dyld_image ()))
394 return ret; 394 return ret;
395 if (NULL != (ret = get_path_from_NSGetExecutablePath())) 395 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
396 return ret; 396 return ret;
397#endif 397#endif
398 if ((NULL != current_pd->binary_name) && 398 if ((NULL != current_pd->binary_name) &&
399 (NULL != (ret = get_path_from_PATH(current_pd->binary_name)))) 399 (NULL != (ret = get_path_from_PATH (current_pd->binary_name))))
400 return ret; 400 return ret;
401 /* other attempts here */ 401 /* other attempts here */
402 LOG(GNUNET_ERROR_TYPE_ERROR, 402 LOG (GNUNET_ERROR_TYPE_ERROR,
403 _( 403 _ (
404 "Could not determine installation path for %s. Set `%s' environment variable.\n"), 404 "Could not determine installation path for %s. Set `%s' environment variable.\n"),
405 current_pd->project_dirname, 405 current_pd->project_dirname,
406 current_pd->env_varname); 406 current_pd->env_varname);
407 return NULL; 407 return NULL;
408} 408}
409 409
@@ -413,16 +413,16 @@ os_get_gnunet_path()
413 * @return a pointer to the executable path, or NULL on error 413 * @return a pointer to the executable path, or NULL on error
414 */ 414 */
415static char * 415static char *
416os_get_exec_path() 416os_get_exec_path ()
417{ 417{
418 char *ret = NULL; 418 char *ret = NULL;
419 419
420#if LINUX 420#if LINUX
421 if (NULL != (ret = get_path_from_proc_exe())) 421 if (NULL != (ret = get_path_from_proc_exe ()))
422 return ret; 422 return ret;
423#endif 423#endif
424#if DARWIN 424#if DARWIN
425 if (NULL != (ret = get_path_from_NSGetExecutablePath())) 425 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
426 return ret; 426 return ret;
427#endif 427#endif
428 /* other attempts here */ 428 /* other attempts here */
@@ -436,7 +436,7 @@ os_get_exec_path()
436 * @return a pointer to the dir path (to be freed by the caller) 436 * @return a pointer to the dir path (to be freed by the caller)
437 */ 437 */
438char * 438char *
439GNUNET_OS_installation_get_path(enum GNUNET_OS_InstallationPathKind dirkind) 439GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
440{ 440{
441 size_t n; 441 size_t n;
442 char *dirname; 442 char *dirname;
@@ -448,215 +448,215 @@ GNUNET_OS_installation_get_path(enum GNUNET_OS_InstallationPathKind dirkind)
448 448
449 /* if wanted, try to get the current app's bin/ */ 449 /* if wanted, try to get the current app's bin/ */
450 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX) 450 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
451 execpath = os_get_exec_path(); 451 execpath = os_get_exec_path ();
452 452
453 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some 453 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
454 * guess for the current app */ 454 * guess for the current app */
455 if (NULL == execpath) 455 if (NULL == execpath)
456 execpath = os_get_gnunet_path(); 456 execpath = os_get_gnunet_path ();
457 457
458 if (NULL == execpath) 458 if (NULL == execpath)
459 return NULL; 459 return NULL;
460 460
461 n = strlen(execpath); 461 n = strlen (execpath);
462 if (0 == n) 462 if (0 == n)
463 { 463 {
464 /* should never happen, but better safe than sorry */ 464 /* should never happen, but better safe than sorry */
465 GNUNET_free(execpath); 465 GNUNET_free (execpath);
466 return NULL; 466 return NULL;
467 } 467 }
468 /* remove filename itself */ 468 /* remove filename itself */
469 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1])) 469 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
470 execpath[--n] = '\0'; 470 execpath[--n] = '\0';
471 471
472 isbasedir = 1; 472 isbasedir = 1;
473 if ((n > 6) && ((0 == strcasecmp(&execpath[n - 6], "/lib32")) || 473 if ((n > 6) && ((0 == strcasecmp (&execpath[n - 6], "/lib32")) ||
474 (0 == strcasecmp(&execpath[n - 6], "/lib64")))) 474 (0 == strcasecmp (&execpath[n - 6], "/lib64"))))
475 { 475 {
476 if ((GNUNET_OS_IPK_LIBDIR != dirkind) && 476 if ((GNUNET_OS_IPK_LIBDIR != dirkind) &&
477 (GNUNET_OS_IPK_LIBEXECDIR != dirkind)) 477 (GNUNET_OS_IPK_LIBEXECDIR != dirkind))
478 {
479 /* strip '/lib32' or '/lib64' */
480 execpath[n - 6] = '\0';
481 n -= 6;
482 }
483 else
484 isbasedir = 0;
485 }
486 else if ((n > 4) && ((0 == strcasecmp(&execpath[n - 4], "/bin")) ||
487 (0 == strcasecmp(&execpath[n - 4], "/lib"))))
488 { 478 {
489 /* strip '/bin' or '/lib' */ 479 /* strip '/lib32' or '/lib64' */
490 execpath[n - 4] = '\0'; 480 execpath[n - 6] = '\0';
491 n -= 4; 481 n -= 6;
492 } 482 }
483 else
484 isbasedir = 0;
485 }
486 else if ((n > 4) && ((0 == strcasecmp (&execpath[n - 4], "/bin")) ||
487 (0 == strcasecmp (&execpath[n - 4], "/lib"))))
488 {
489 /* strip '/bin' or '/lib' */
490 execpath[n - 4] = '\0';
491 n -= 4;
492 }
493 multiarch = NULL; 493 multiarch = NULL;
494 if (NULL != (libdir = strstr(execpath, "/lib/"))) 494 if (NULL != (libdir = strstr (execpath, "/lib/")))
495 { 495 {
496 /* test for multi-arch path of the form "PREFIX/lib/MULTIARCH/"; 496 /* test for multi-arch path of the form "PREFIX/lib/MULTIARCH/";
497 here we need to re-add 'multiarch' to lib and libexec paths later! */ 497 here we need to re-add 'multiarch' to lib and libexec paths later! */
498 multiarch = &libdir[5]; 498 multiarch = &libdir[5];
499 if (NULL == strchr(multiarch, '/')) 499 if (NULL == strchr (multiarch, '/'))
500 libdir[0] = 500 libdir[0] =
501 '\0'; /* Debian multiarch format, cut of from 'execpath' but preserve in multicarch */ 501 '\0'; /* Debian multiarch format, cut of from 'execpath' but preserve in multicarch */
502 else 502 else
503 multiarch = 503 multiarch =
504 NULL; /* maybe not, multiarch still has a '/', which is not OK */ 504 NULL; /* maybe not, multiarch still has a '/', which is not OK */
505 } 505 }
506 /* in case this was a directory named foo-bin, remove "foo-" */ 506 /* in case this was a directory named foo-bin, remove "foo-" */
507 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) 507 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
508 execpath[--n] = '\0'; 508 execpath[--n] = '\0';
509 switch (dirkind) 509 switch (dirkind)
510 {
511 case GNUNET_OS_IPK_PREFIX:
512 case GNUNET_OS_IPK_SELF_PREFIX:
513 dirname = GNUNET_strdup (DIR_SEPARATOR_STR);
514 break;
515
516 case GNUNET_OS_IPK_BINDIR:
517 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR);
518 break;
519
520 case GNUNET_OS_IPK_LIBDIR:
521 if (isbasedir)
522 {
523 GNUNET_asprintf (&tmp,
524 "%s%s%s%s%s%s%s",
525 execpath,
526 DIR_SEPARATOR_STR "lib",
527 (NULL != multiarch) ? DIR_SEPARATOR_STR : "",
528 (NULL != multiarch) ? multiarch : "",
529 DIR_SEPARATOR_STR,
530 current_pd->project_dirname,
531 DIR_SEPARATOR_STR);
532 if (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES))
533 {
534 GNUNET_free (execpath);
535 return tmp;
536 }
537 GNUNET_free (tmp);
538 tmp = NULL;
539 dirname = NULL;
540 if (4 == sizeof(void *))
541 {
542 GNUNET_asprintf (&dirname,
543 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
544 "%s" DIR_SEPARATOR_STR,
545 current_pd->project_dirname);
546 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
547 }
548 if (8 == sizeof(void *))
549 {
550 GNUNET_asprintf (&dirname,
551 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
552 "%s" DIR_SEPARATOR_STR,
553 current_pd->project_dirname);
554 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
555 }
556
557 if ((NULL != tmp) &&
558 (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES)))
559 {
560 GNUNET_free (execpath);
561 GNUNET_free_non_null (dirname);
562 return tmp;
563 }
564 GNUNET_free (tmp);
565 GNUNET_free_non_null (dirname);
566 }
567 GNUNET_asprintf (&dirname,
568 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
569 current_pd->project_dirname);
570 break;
571
572 case GNUNET_OS_IPK_DATADIR:
573 GNUNET_asprintf (&dirname,
574 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
575 "%s" DIR_SEPARATOR_STR,
576 current_pd->project_dirname);
577 break;
578
579 case GNUNET_OS_IPK_LOCALEDIR:
580 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
581 "locale" DIR_SEPARATOR_STR);
582 break;
583
584 case GNUNET_OS_IPK_ICONDIR:
585 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
586 "icons" DIR_SEPARATOR_STR);
587 break;
588
589 case GNUNET_OS_IPK_DOCDIR:
590 GNUNET_asprintf (&dirname,
591 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
592 "doc" DIR_SEPARATOR_STR
593 "%s" DIR_SEPARATOR_STR,
594 current_pd->project_dirname);
595 break;
596
597 case GNUNET_OS_IPK_LIBEXECDIR:
598 if (isbasedir)
510 { 599 {
511 case GNUNET_OS_IPK_PREFIX: 600 GNUNET_asprintf (&dirname,
512 case GNUNET_OS_IPK_SELF_PREFIX: 601 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
513 dirname = GNUNET_strdup(DIR_SEPARATOR_STR); 602 "libexec" DIR_SEPARATOR_STR,
514 break; 603 current_pd->project_dirname);
515 604 GNUNET_asprintf (&tmp,
516 case GNUNET_OS_IPK_BINDIR: 605 "%s%s%s%s",
517 dirname = GNUNET_strdup(DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR); 606 execpath,
518 break; 607 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR,
519 608 (NULL != multiarch) ? multiarch : "",
520 case GNUNET_OS_IPK_LIBDIR: 609 dirname);
521 if (isbasedir) 610 if (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES))
522 { 611 {
523 GNUNET_asprintf(&tmp, 612 GNUNET_free (execpath);
524 "%s%s%s%s%s%s%s", 613 GNUNET_free (dirname);
525 execpath, 614 return tmp;
526 DIR_SEPARATOR_STR "lib", 615 }
527 (NULL != multiarch) ? DIR_SEPARATOR_STR : "", 616 GNUNET_free (tmp);
528 (NULL != multiarch) ? multiarch : "", 617 tmp = NULL;
529 DIR_SEPARATOR_STR, 618 dirname = NULL;
530 current_pd->project_dirname, 619 if (4 == sizeof(void *))
531 DIR_SEPARATOR_STR); 620 {
532 if (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES)) 621 GNUNET_asprintf (&dirname,
533 { 622 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
534 GNUNET_free(execpath); 623 "%s" DIR_SEPARATOR_STR
535 return tmp; 624 "libexec" DIR_SEPARATOR_STR,
536 } 625 current_pd->project_dirname);
537 GNUNET_free(tmp); 626 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
538 tmp = NULL; 627 }
539 dirname = NULL; 628 if (8 == sizeof(void *))
540 if (4 == sizeof(void *)) 629 {
541 { 630 GNUNET_asprintf (&dirname,
542 GNUNET_asprintf(&dirname, 631 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
543 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR 632 "%s" DIR_SEPARATOR_STR
544 "%s" DIR_SEPARATOR_STR, 633 "libexec" DIR_SEPARATOR_STR,
545 current_pd->project_dirname); 634 current_pd->project_dirname);
546 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname); 635 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
547 } 636 }
548 if (8 == sizeof(void *)) 637 if ((NULL != tmp) &&
549 { 638 (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES)))
550 GNUNET_asprintf(&dirname, 639 {
551 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR 640 GNUNET_free (execpath);
552 "%s" DIR_SEPARATOR_STR, 641 GNUNET_free_non_null (dirname);
553 current_pd->project_dirname); 642 return tmp;
554 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname); 643 }
555 } 644 GNUNET_free (tmp);
556 645 GNUNET_free_non_null (dirname);
557 if ((NULL != tmp) &&
558 (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES)))
559 {
560 GNUNET_free(execpath);
561 GNUNET_free_non_null(dirname);
562 return tmp;
563 }
564 GNUNET_free(tmp);
565 GNUNET_free_non_null(dirname);
566 }
567 GNUNET_asprintf(&dirname,
568 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
569 current_pd->project_dirname);
570 break;
571
572 case GNUNET_OS_IPK_DATADIR:
573 GNUNET_asprintf(&dirname,
574 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
575 "%s" DIR_SEPARATOR_STR,
576 current_pd->project_dirname);
577 break;
578
579 case GNUNET_OS_IPK_LOCALEDIR:
580 dirname = GNUNET_strdup(DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
581 "locale" DIR_SEPARATOR_STR);
582 break;
583
584 case GNUNET_OS_IPK_ICONDIR:
585 dirname = GNUNET_strdup(DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
586 "icons" DIR_SEPARATOR_STR);
587 break;
588
589 case GNUNET_OS_IPK_DOCDIR:
590 GNUNET_asprintf(&dirname,
591 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
592 "doc" DIR_SEPARATOR_STR
593 "%s" DIR_SEPARATOR_STR,
594 current_pd->project_dirname);
595 break;
596
597 case GNUNET_OS_IPK_LIBEXECDIR:
598 if (isbasedir)
599 {
600 GNUNET_asprintf(&dirname,
601 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
602 "libexec" DIR_SEPARATOR_STR,
603 current_pd->project_dirname);
604 GNUNET_asprintf(&tmp,
605 "%s%s%s%s",
606 execpath,
607 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR,
608 (NULL != multiarch) ? multiarch : "",
609 dirname);
610 if (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES))
611 {
612 GNUNET_free(execpath);
613 GNUNET_free(dirname);
614 return tmp;
615 }
616 GNUNET_free(tmp);
617 tmp = NULL;
618 dirname = NULL;
619 if (4 == sizeof(void *))
620 {
621 GNUNET_asprintf(&dirname,
622 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
623 "%s" DIR_SEPARATOR_STR
624 "libexec" DIR_SEPARATOR_STR,
625 current_pd->project_dirname);
626 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname);
627 }
628 if (8 == sizeof(void *))
629 {
630 GNUNET_asprintf(&dirname,
631 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
632 "%s" DIR_SEPARATOR_STR
633 "libexec" DIR_SEPARATOR_STR,
634 current_pd->project_dirname);
635 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname);
636 }
637 if ((NULL != tmp) &&
638 (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES)))
639 {
640 GNUNET_free(execpath);
641 GNUNET_free_non_null(dirname);
642 return tmp;
643 }
644 GNUNET_free(tmp);
645 GNUNET_free_non_null(dirname);
646 }
647 GNUNET_asprintf(&dirname,
648 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
649 "libexec" DIR_SEPARATOR_STR,
650 current_pd->project_dirname);
651 break;
652
653 default:
654 GNUNET_free(execpath);
655 return NULL;
656 } 646 }
657 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname); 647 GNUNET_asprintf (&dirname,
658 GNUNET_free(dirname); 648 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
659 GNUNET_free(execpath); 649 "libexec" DIR_SEPARATOR_STR,
650 current_pd->project_dirname);
651 break;
652
653 default:
654 GNUNET_free (execpath);
655 return NULL;
656 }
657 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
658 GNUNET_free (dirname);
659 GNUNET_free (execpath);
660 return tmp; 660 return tmp;
661} 661}
662 662
@@ -670,7 +670,7 @@ GNUNET_OS_installation_get_path(enum GNUNET_OS_InstallationPathKind dirkind)
670 * @return full path to the binary, if possible, otherwise copy of 'progname' 670 * @return full path to the binary, if possible, otherwise copy of 'progname'
671 */ 671 */
672char * 672char *
673GNUNET_OS_get_libexec_binary_path(const char *progname) 673GNUNET_OS_get_libexec_binary_path (const char *progname)
674{ 674{
675 static char *cache; 675 static char *cache;
676 char *libexecdir; 676 char *libexecdir;
@@ -678,15 +678,15 @@ GNUNET_OS_get_libexec_binary_path(const char *progname)
678 678
679 if ((DIR_SEPARATOR == progname[0]) || 679 if ((DIR_SEPARATOR == progname[0]) ||
680 (GNUNET_YES == 680 (GNUNET_YES ==
681 GNUNET_STRINGS_path_is_absolute(progname, GNUNET_NO, NULL, NULL))) 681 GNUNET_STRINGS_path_is_absolute (progname, GNUNET_NO, NULL, NULL)))
682 return GNUNET_strdup(progname); 682 return GNUNET_strdup (progname);
683 if (NULL != cache) 683 if (NULL != cache)
684 libexecdir = cache; 684 libexecdir = cache;
685 else 685 else
686 libexecdir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LIBEXECDIR); 686 libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR);
687 if (NULL == libexecdir) 687 if (NULL == libexecdir)
688 return GNUNET_strdup(progname); 688 return GNUNET_strdup (progname);
689 GNUNET_asprintf(&binary, "%s%s", libexecdir, progname); 689 GNUNET_asprintf (&binary, "%s%s", libexecdir, progname);
690 cache = libexecdir; 690 cache = libexecdir;
691 return binary; 691 return binary;
692} 692}
@@ -705,8 +705,8 @@ GNUNET_OS_get_libexec_binary_path(const char *progname)
705 * otherwise 705 * otherwise
706 */ 706 */
707char * 707char *
708GNUNET_OS_get_suid_binary_path(const struct GNUNET_CONFIGURATION_Handle *cfg, 708GNUNET_OS_get_suid_binary_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
709 const char *progname) 709 const char *progname)
710{ 710{
711 static char *cache; 711 static char *cache;
712 char *binary = NULL; 712 char *binary = NULL;
@@ -714,26 +714,26 @@ GNUNET_OS_get_suid_binary_path(const struct GNUNET_CONFIGURATION_Handle *cfg,
714 size_t path_len; 714 size_t path_len;
715 715
716 if (GNUNET_YES == 716 if (GNUNET_YES ==
717 GNUNET_STRINGS_path_is_absolute(progname, GNUNET_NO, NULL, NULL)) 717 GNUNET_STRINGS_path_is_absolute (progname, GNUNET_NO, NULL, NULL))
718 { 718 {
719 return GNUNET_strdup(progname); 719 return GNUNET_strdup (progname);
720 } 720 }
721 if (NULL != cache) 721 if (NULL != cache)
722 path = cache; 722 path = cache;
723 else 723 else
724 GNUNET_CONFIGURATION_get_value_string(cfg, 724 GNUNET_CONFIGURATION_get_value_string (cfg,
725 "PATHS", 725 "PATHS",
726 "SUID_BINARY_PATH", 726 "SUID_BINARY_PATH",
727 &path); 727 &path);
728 if ((NULL == path) || (0 == strlen(path))) 728 if ((NULL == path) || (0 == strlen (path)))
729 return GNUNET_OS_get_libexec_binary_path(progname); 729 return GNUNET_OS_get_libexec_binary_path (progname);
730 path_len = strlen(path); 730 path_len = strlen (path);
731 GNUNET_asprintf(&binary, 731 GNUNET_asprintf (&binary,
732 "%s%s%s", 732 "%s%s%s",
733 path, 733 path,
734 (path[path_len - 1] == DIR_SEPARATOR) ? "" 734 (path[path_len - 1] == DIR_SEPARATOR) ? ""
735 : DIR_SEPARATOR_STR, 735 : DIR_SEPARATOR_STR,
736 progname); 736 progname);
737 cache = path; 737 cache = path;
738 return binary; 738 return binary;
739} 739}
@@ -756,72 +756,72 @@ GNUNET_OS_get_suid_binary_path(const struct GNUNET_CONFIGURATION_Handle *cfg,
756 * #GNUNET_SYSERR on error (no such binary or not executable) 756 * #GNUNET_SYSERR on error (no such binary or not executable)
757 */ 757 */
758int 758int
759GNUNET_OS_check_helper_binary(const char *binary, 759GNUNET_OS_check_helper_binary (const char *binary,
760 int check_suid, 760 int check_suid,
761 const char *params) 761 const char *params)
762{ 762{
763 struct stat statbuf; 763 struct stat statbuf;
764 char *p; 764 char *p;
765 char *pf; 765 char *pf;
766 766
767 if ((GNUNET_YES == 767 if ((GNUNET_YES ==
768 GNUNET_STRINGS_path_is_absolute(binary, GNUNET_NO, NULL, NULL)) || 768 GNUNET_STRINGS_path_is_absolute (binary, GNUNET_NO, NULL, NULL)) ||
769 (0 == strncmp(binary, "./", 2))) 769 (0 == strncmp (binary, "./", 2)))
770 { 770 {
771 p = GNUNET_strdup(binary); 771 p = GNUNET_strdup (binary);
772 } 772 }
773 else 773 else
774 {
775 p = get_path_from_PATH (binary);
776 if (NULL != p)
774 { 777 {
775 p = get_path_from_PATH(binary); 778 GNUNET_asprintf (&pf, "%s/%s", p, binary);
776 if (NULL != p) 779 GNUNET_free (p);
777 { 780 p = pf;
778 GNUNET_asprintf(&pf, "%s/%s", p, binary);
779 GNUNET_free(p);
780 p = pf;
781 }
782 } 781 }
782 }
783 783
784 if (NULL == p) 784 if (NULL == p)
785 { 785 {
786 LOG(GNUNET_ERROR_TYPE_INFO, 786 LOG (GNUNET_ERROR_TYPE_INFO,
787 _("Could not find binary `%s' in PATH!\n"), 787 _ ("Could not find binary `%s' in PATH!\n"),
788 binary); 788 binary);
789 return GNUNET_SYSERR; 789 return GNUNET_SYSERR;
790 } 790 }
791 if (0 != access(p, X_OK)) 791 if (0 != access (p, X_OK))
792 { 792 {
793 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", p); 793 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p);
794 GNUNET_free(p); 794 GNUNET_free (p);
795 return GNUNET_SYSERR; 795 return GNUNET_SYSERR;
796 } 796 }
797 797
798 if (0 == getuid()) 798 if (0 == getuid ())
799 { 799 {
800 /* as we run as root, we don't insist on SUID */ 800 /* as we run as root, we don't insist on SUID */
801 GNUNET_free(p); 801 GNUNET_free (p);
802 return GNUNET_YES; 802 return GNUNET_YES;
803 } 803 }
804 804
805 if (0 != stat(p, &statbuf)) 805 if (0 != stat (p, &statbuf))
806 { 806 {
807 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", p); 807 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p);
808 GNUNET_free(p); 808 GNUNET_free (p);
809 return GNUNET_SYSERR; 809 return GNUNET_SYSERR;
810 } 810 }
811 if (check_suid) 811 if (check_suid)
812 {
813 (void) params;
814 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid))
812 { 815 {
813 (void)params; 816 GNUNET_free (p);
814 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid)) 817 return GNUNET_YES;
815 {
816 GNUNET_free(p);
817 return GNUNET_YES;
818 }
819 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
820 _("Binary `%s' exists, but is not SUID\n"),
821 p);
822 /* binary exists, but not SUID */
823 } 818 }
824 GNUNET_free(p); 819 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
820 _ ("Binary `%s' exists, but is not SUID\n"),
821 p);
822 /* binary exists, but not SUID */
823 }
824 GNUNET_free (p);
825 return GNUNET_NO; 825 return GNUNET_NO;
826} 826}
827 827
diff --git a/src/util/os_network.c b/src/util/os_network.c
index 526678047..e310455e0 100644
--- a/src/util/os_network.c
+++ b/src/util/os_network.c
@@ -31,11 +31,15 @@
31#include "gnunet_util_lib.h" 31#include "gnunet_util_lib.h"
32 32
33 33
34#define LOG(kind, ...) GNUNET_log_from(kind, "util-os-network", __VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from (kind, "util-os-network", __VA_ARGS__)
35#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-os-network", syscall, filename) 35#define LOG_STRERROR_FILE(kind, syscall, \
36 filename) GNUNET_log_from_strerror_file (kind, \
37 "util-os-network", \
38 syscall, \
39 filename)
36 40
37 41
38#if !(HAVE_GETIFADDRS && HAVE_FREEIFADDRS) 42#if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS)
39/** 43/**
40 * Try to enumerate all network interfaces using 'ifconfig'. 44 * Try to enumerate all network interfaces using 'ifconfig'.
41 * 45 *
@@ -44,8 +48,8 @@
44 * @return #GNUNET_OK if it worked 48 * @return #GNUNET_OK if it worked
45 */ 49 */
46static int 50static int
47try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc, 51try_ifconfig (GNUNET_OS_NetworkInterfaceProcessor proc,
48 void *proc_cls) 52 void *proc_cls)
49{ 53{
50 int i; 54 int i;
51 char line[1024]; 55 char line[1024];
@@ -70,159 +74,161 @@ try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc,
70 static char *pcall; 74 static char *pcall;
71 75
72 if (NULL == pcall) 76 if (NULL == pcall)
73 { 77 {
74 const char *sbin_ifconfig; 78 const char *sbin_ifconfig;
75 79
76#ifdef IFCONFIG 80#ifdef IFCONFIG
77 if (0 == access(IFCONFIG, X_OK)) 81 if (0 == access (IFCONFIG, X_OK))
78 sbin_ifconfig = IFCONFIG; 82 sbin_ifconfig = IFCONFIG;
79 else 83 else
80#endif 84#endif
81 if (0 == access("/sbin/ifconfig", X_OK)) 85 if (0 == access ("/sbin/ifconfig", X_OK))
82 sbin_ifconfig = "/sbin/ifconfig"; 86 sbin_ifconfig = "/sbin/ifconfig";
83 else if (0 == access("/usr/sbin/ifconfig", X_OK)) 87 else if (0 == access ("/usr/sbin/ifconfig", X_OK))
84 sbin_ifconfig = "/usr/sbin/ifconfig"; 88 sbin_ifconfig = "/usr/sbin/ifconfig";
85 else 89 else
86 sbin_ifconfig = "ifconfig"; 90 sbin_ifconfig = "ifconfig";
87 GNUNET_asprintf(&pcall, 91 GNUNET_asprintf (&pcall,
88 "%s -a 2> /dev/null", 92 "%s -a 2> /dev/null",
89 sbin_ifconfig); 93 sbin_ifconfig);
90 } 94 }
91 f = popen(pcall, "r"); 95 f = popen (pcall, "r");
92 if (NULL == f) 96 if (NULL == f)
93 { 97 {
94 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 98 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
95 "popen", 99 "popen",
96 "ifconfig"); 100 "ifconfig");
97 101
98 return GNUNET_SYSERR; 102 return GNUNET_SYSERR;
99 } 103 }
100 104
101 have_ifc = GNUNET_NO; 105 have_ifc = GNUNET_NO;
102 ifc[11] = '\0'; 106 ifc[11] = '\0';
103 while (NULL != fgets(line, sizeof(line), f)) 107 while (NULL != fgets (line, sizeof(line), f))
108 {
109 if (strlen (line) == 0)
104 { 110 {
105 if (strlen(line) == 0) 111 have_ifc = GNUNET_NO;
106 { 112 continue;
107 have_ifc = GNUNET_NO; 113 }
108 continue; 114 if (! isspace (line[0]))
109 } 115 {
110 if (!isspace(line[0])) 116 have_ifc = (1 == sscanf (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
111 { 117 /* would end with ':' on OSX, fix it! */
112 have_ifc = (1 == sscanf(line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO; 118 if (ifc[strlen (ifc) - 1] == ':')
113 /* would end with ':' on OSX, fix it! */ 119 ifc[strlen (ifc) - 1] = '\0';
114 if (ifc[strlen(ifc) - 1] == ':') 120 continue;
115 ifc[strlen(ifc) - 1] = '\0'; 121 }
116 continue; 122 if (! have_ifc)
117 } 123 continue; /* strange input, hope for the best */
118 if (!have_ifc)
119 continue; /* strange input, hope for the best */
120 124
121 /* make parsing of ipv6 addresses easier */ 125 /* make parsing of ipv6 addresses easier */
122 for (replace = line; *replace != '\0'; replace++) 126 for (replace = line; *replace != '\0'; replace++)
123 { 127 {
124 if (*replace == '/') 128 if (*replace == '/')
125 *replace = ' '; 129 *replace = ' ';
126 } 130 }
127 prefixlen = -1; 131 prefixlen = -1;
128 132
129 start = line; 133 start = line;
130 while (('\0' != *start) && (isspace(*start))) 134 while (('\0' != *start) && (isspace (*start)))
131 start++; 135 start++;
132 136
133 /* Zero-out stack allocated values */ 137 /* Zero-out stack allocated values */
134 memset(addrstr, 0, 128); 138 memset (addrstr, 0, 128);
135 memset(netmaskstr, 0, 128); 139 memset (netmaskstr, 0, 128);
136 memset(bcstr, 0, 128); 140 memset (bcstr, 0, 128);
137 prefixlen = 0; 141 prefixlen = 0;
138 142
139 if ( /* Linux */ 143 if ( /* Linux */
140 (3 == sscanf(start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr, bcstr, netmaskstr)) || 144 (3 == sscanf (start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr,
141 (2 == sscanf(start, "inet addr:%127s Mask:%127s", addrstr, netmaskstr)) || 145 bcstr, netmaskstr)) ||
142 (2 == sscanf(start, "inet6 addr:%127s %d", addrstr, &prefixlen)) || 146 (2 == sscanf (start, "inet addr:%127s Mask:%127s", addrstr,
143 /* Solaris, OS X */ 147 netmaskstr)) ||
144 (1 == sscanf(start, "inet %127s", addrstr)) || 148 (2 == sscanf (start, "inet6 addr:%127s %d", addrstr, &prefixlen)) ||
145 (1 == sscanf(start, "inet6 %127s", addrstr))) 149 /* Solaris, OS X */
146 { 150 (1 == sscanf (start, "inet %127s", addrstr)) ||
147 /* IPv4 */ 151 (1 == sscanf (start, "inet6 %127s", addrstr)))
148 if (1 == inet_pton(AF_INET, addrstr, &v4)) 152 {
149 { 153 /* IPv4 */
150 memset(&a4, 0, sizeof(a4)); 154 if (1 == inet_pton (AF_INET, addrstr, &v4))
151 a4.sin_family = AF_INET; 155 {
156 memset (&a4, 0, sizeof(a4));
157 a4.sin_family = AF_INET;
152#if HAVE_SOCKADDR_IN_SIN_LEN 158#if HAVE_SOCKADDR_IN_SIN_LEN
153 a4.sin_len = (u_char)sizeof(struct sockaddr_in); 159 a4.sin_len = (u_char) sizeof(struct sockaddr_in);
154#endif 160#endif
155 a4.sin_addr = v4; 161 a4.sin_addr = v4;
156 162
157 pass_bcaddr = NULL; 163 pass_bcaddr = NULL;
158 pass_netmask = NULL; 164 pass_netmask = NULL;
159 if (1 == inet_pton(AF_INET, bcstr, &v4)) 165 if (1 == inet_pton (AF_INET, bcstr, &v4))
160 { 166 {
161 memset(&bcaddr, 0, sizeof(bcaddr)); 167 memset (&bcaddr, 0, sizeof(bcaddr));
162 bcaddr.sin_family = AF_INET; 168 bcaddr.sin_family = AF_INET;
163#if HAVE_SOCKADDR_IN_SIN_LEN 169#if HAVE_SOCKADDR_IN_SIN_LEN
164 bcaddr.sin_len = (u_char)sizeof(struct sockaddr_in); 170 bcaddr.sin_len = (u_char) sizeof(struct sockaddr_in);
165#endif 171#endif
166 bcaddr.sin_addr = v4; 172 bcaddr.sin_addr = v4;
167 pass_bcaddr = (struct sockaddr *)&bcaddr; 173 pass_bcaddr = (struct sockaddr *) &bcaddr;
168 } 174 }
169 if (1 == inet_pton(AF_INET, netmaskstr, &v4)) 175 if (1 == inet_pton (AF_INET, netmaskstr, &v4))
170 { 176 {
171 memset(&netmask, 0, sizeof(netmask)); 177 memset (&netmask, 0, sizeof(netmask));
172 netmask.sin_family = AF_INET; 178 netmask.sin_family = AF_INET;
173#if HAVE_SOCKADDR_IN_SIN_LEN 179#if HAVE_SOCKADDR_IN_SIN_LEN
174 netmask.sin_len = (u_char)sizeof(struct sockaddr_in); 180 netmask.sin_len = (u_char) sizeof(struct sockaddr_in);
175#endif 181#endif
176 netmask.sin_addr = v4; 182 netmask.sin_addr = v4;
177 pass_netmask = (struct sockaddr *)&netmask; 183 pass_netmask = (struct sockaddr *) &netmask;
178 } 184 }
179 185
180 186
181 if (GNUNET_OK != 187 if (GNUNET_OK !=
182 proc(proc_cls, ifc, 0 == strcmp(ifc, GNUNET_DEFAULT_INTERFACE), 188 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
183 (const struct sockaddr *)&a4, 189 (const struct sockaddr *) &a4,
184 pass_bcaddr, pass_netmask, sizeof(a4))) 190 pass_bcaddr, pass_netmask, sizeof(a4)))
185 break; 191 break;
186 continue; 192 continue;
187 } 193 }
188 /* IPv6 */ 194 /* IPv6 */
189 if (1 == inet_pton(AF_INET6, addrstr, &v6)) 195 if (1 == inet_pton (AF_INET6, addrstr, &v6))
190 { 196 {
191 memset(&a6, 0, sizeof(a6)); 197 memset (&a6, 0, sizeof(a6));
192 a6.sin6_family = AF_INET6; 198 a6.sin6_family = AF_INET6;
193#if HAVE_SOCKADDR_IN_SIN_LEN 199#if HAVE_SOCKADDR_IN_SIN_LEN
194 a6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 200 a6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
195#endif 201#endif
196 a6.sin6_addr = v6; 202 a6.sin6_addr = v6;
197 203
198 pass_netmask = NULL; 204 pass_netmask = NULL;
199 if (prefixlen != -1) 205 if (prefixlen != -1)
200 { 206 {
201 memset(v6.s6_addr, 0, sizeof(v6.s6_addr)); 207 memset (v6.s6_addr, 0, sizeof(v6.s6_addr));
202 for (i = 0; i < prefixlen; i++) 208 for (i = 0; i < prefixlen; i++)
203 { 209 {
204 v6.s6_addr[i >> 3] |= 1 << (i & 7); 210 v6.s6_addr[i >> 3] |= 1 << (i & 7);
205 } 211 }
206 memset(&netmask6, 0, sizeof(netmask6)); 212 memset (&netmask6, 0, sizeof(netmask6));
207 netmask6.sin6_family = AF_INET6; 213 netmask6.sin6_family = AF_INET6;
208#if HAVE_SOCKADDR_IN_SIN_LEN 214#if HAVE_SOCKADDR_IN_SIN_LEN
209 netmask6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 215 netmask6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
210#endif 216#endif
211 netmask6.sin6_addr = v6; 217 netmask6.sin6_addr = v6;
212 218
213 pass_netmask = (struct sockaddr *)&netmask6; 219 pass_netmask = (struct sockaddr *) &netmask6;
214 }
215
216 if (GNUNET_OK !=
217 proc(proc_cls, ifc, 0 == strcmp(ifc, GNUNET_DEFAULT_INTERFACE),
218 (const struct sockaddr *)&a6,
219 NULL, pass_netmask, sizeof(a6)))
220 break;
221 continue;
222 }
223 } 220 }
221
222 if (GNUNET_OK !=
223 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
224 (const struct sockaddr *) &a6,
225 NULL, pass_netmask, sizeof(a6)))
226 break;
227 continue;
228 }
224 } 229 }
225 pclose(f); 230 }
231 pclose (f);
226 return GNUNET_OK; 232 return GNUNET_OK;
227} 233}
228 234
@@ -235,8 +241,8 @@ try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc,
235 * @return #GNUNET_OK if it worked 241 * @return #GNUNET_OK if it worked
236 */ 242 */
237static int 243static int
238try_ip(GNUNET_OS_NetworkInterfaceProcessor proc, 244try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
239 void *proc_cls) 245 void *proc_cls)
240{ 246{
241 char line[1024]; 247 char line[1024];
242 char *replace; 248 char *replace;
@@ -255,124 +261,124 @@ try_ip(GNUNET_OS_NetworkInterfaceProcessor proc,
255 static char *pcall; 261 static char *pcall;
256 262
257 if (NULL == pcall) 263 if (NULL == pcall)
258 { 264 {
259 const char *sbin_ip; 265 const char *sbin_ip;
260 266
261#ifdef IFCONFIG 267#ifdef IFCONFIG
262 if (0 == access(PATH_TO_IP, X_OK)) 268 if (0 == access (PATH_TO_IP, X_OK))
263 sbin_ip = PATH_TO_IP; 269 sbin_ip = PATH_TO_IP;
264 else 270 else
265#endif 271#endif
266 if (0 == access("/sbin/ip", X_OK)) 272 if (0 == access ("/sbin/ip", X_OK))
267 sbin_ip = "/sbin/ip"; 273 sbin_ip = "/sbin/ip";
268 else if (0 == access("/usr/sbin/ip", X_OK)) 274 else if (0 == access ("/usr/sbin/ip", X_OK))
269 sbin_ip = "/usr/sbin/ip"; 275 sbin_ip = "/usr/sbin/ip";
270 else 276 else
271 sbin_ip = "if"; 277 sbin_ip = "if";
272 GNUNET_asprintf(&pcall, 278 GNUNET_asprintf (&pcall,
273 "%s -o add 2> /dev/null", 279 "%s -o add 2> /dev/null",
274 sbin_ip); 280 sbin_ip);
275 } 281 }
276 f = popen(pcall, "r"); 282 f = popen (pcall, "r");
277 if (!f) 283 if (! f)
284 {
285 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
286 "popen",
287 "ip");
288 return GNUNET_SYSERR;
289 }
290
291 while (NULL != fgets (line, sizeof(line), f))
292 {
293 /* make parsing easier */
294 for (replace = line; *replace != '\0'; replace++)
278 { 295 {
279 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 296 if (*replace == '/')
280 "popen", 297 *replace = ' ';
281 "ip");
282 return GNUNET_SYSERR;
283 } 298 }
284 299 /* Zero-out stack allocated values */
285 while (NULL != fgets(line, sizeof(line), f)) 300 memset (ifname, 0, 64);
301 memset (afstr, 0, 6);
302 memset (addrstr, 0, 128);
303 if (4 != sscanf (line,
304 "%*u: %63s %5s %127s %6u",
305 ifname,
306 afstr,
307 addrstr,
308 &prefixlen))
309 continue;
310 /* IPv4 */
311 if ((0 == strcasecmp ("inet",
312 afstr)) &&
313 (1 == inet_pton (AF_INET,
314 addrstr,
315 &v4)))
286 { 316 {
287 /* make parsing easier */ 317 memset (&a4, 0, sizeof(a4));
288 for (replace = line; *replace != '\0'; replace++) 318 a4.sin_family = AF_INET;
289 {
290 if (*replace == '/')
291 *replace = ' ';
292 }
293 /* Zero-out stack allocated values */
294 memset(ifname, 0, 64);
295 memset(afstr, 0, 6);
296 memset(addrstr, 0, 128);
297 if (4 != sscanf(line,
298 "%*u: %63s %5s %127s %6u",
299 ifname,
300 afstr,
301 addrstr,
302 &prefixlen))
303 continue;
304 /* IPv4 */
305 if ((0 == strcasecmp("inet",
306 afstr)) &&
307 (1 == inet_pton(AF_INET,
308 addrstr,
309 &v4)))
310 {
311 memset(&a4, 0, sizeof(a4));
312 a4.sin_family = AF_INET;
313#if HAVE_SOCKADDR_IN_SIN_LEN 319#if HAVE_SOCKADDR_IN_SIN_LEN
314 a4.sin_len = (u_char)sizeof(struct sockaddr_in); 320 a4.sin_len = (u_char) sizeof(struct sockaddr_in);
315#endif 321#endif
316 a4.sin_addr = v4; 322 a4.sin_addr = v4;
317 323
318 memset(&v4.s_addr, 0, sizeof(v4.s_addr)); 324 memset (&v4.s_addr, 0, sizeof(v4.s_addr));
319 for (i = 0; i < prefixlen; i++) 325 for (i = 0; i < prefixlen; i++)
320 v4.s_addr |= 1 << (i & 7); 326 v4.s_addr |= 1 << (i & 7);
321 memset(&netmask, 0, sizeof(netmask)); 327 memset (&netmask, 0, sizeof(netmask));
322 netmask.sin_family = AF_INET; 328 netmask.sin_family = AF_INET;
323#if HAVE_SOCKADDR_IN_SIN_LEN 329#if HAVE_SOCKADDR_IN_SIN_LEN
324 netmask.sin_len = (u_char)sizeof(struct sockaddr_in); 330 netmask.sin_len = (u_char) sizeof(struct sockaddr_in);
325#endif 331#endif
326 netmask.sin_addr = v4; 332 netmask.sin_addr = v4;
327 333
328 if (GNUNET_OK != 334 if (GNUNET_OK !=
329 proc(proc_cls, 335 proc (proc_cls,
330 ifname, 336 ifname,
331 (0 == strcmp(ifname, 337 (0 == strcmp (ifname,
332 GNUNET_DEFAULT_INTERFACE)), 338 GNUNET_DEFAULT_INTERFACE)),
333 (const struct sockaddr *)&a4, 339 (const struct sockaddr *) &a4,
334 NULL, 340 NULL,
335 (const struct sockaddr *)&netmask, 341 (const struct sockaddr *) &netmask,
336 sizeof(a4))) 342 sizeof(a4)))
337 break; 343 break;
338 } 344 }
339 /* IPv6 */ 345 /* IPv6 */
340 if ((0 == strcasecmp("inet6", 346 if ((0 == strcasecmp ("inet6",
341 afstr)) && 347 afstr)) &&
342 (1 == inet_pton(AF_INET6, 348 (1 == inet_pton (AF_INET6,
343 addrstr, 349 addrstr,
344 &v6))) 350 &v6)))
345 { 351 {
346 memset(&a6, 0, sizeof(a6)); 352 memset (&a6, 0, sizeof(a6));
347 a6.sin6_family = AF_INET6; 353 a6.sin6_family = AF_INET6;
348#if HAVE_SOCKADDR_IN_SIN_LEN 354#if HAVE_SOCKADDR_IN_SIN_LEN
349 a6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 355 a6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
350#endif 356#endif
351 a6.sin6_addr = v6; 357 a6.sin6_addr = v6;
352 358
353 memset(v6.s6_addr, 0, sizeof(v6.s6_addr)); 359 memset (v6.s6_addr, 0, sizeof(v6.s6_addr));
354 for (i = 0; i < prefixlen; i++) 360 for (i = 0; i < prefixlen; i++)
355 v6.s6_addr[i >> 3] |= 1 << (i & 7); 361 v6.s6_addr[i >> 3] |= 1 << (i & 7);
356 memset(&netmask6, 0, sizeof(netmask6)); 362 memset (&netmask6, 0, sizeof(netmask6));
357 netmask6.sin6_family = AF_INET6; 363 netmask6.sin6_family = AF_INET6;
358#if HAVE_SOCKADDR_IN_SIN_LEN 364#if HAVE_SOCKADDR_IN_SIN_LEN
359 netmask6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 365 netmask6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
360#endif 366#endif
361 netmask6.sin6_addr = v6; 367 netmask6.sin6_addr = v6;
362 368
363 if (GNUNET_OK != 369 if (GNUNET_OK !=
364 proc(proc_cls, 370 proc (proc_cls,
365 ifname, 371 ifname,
366 (0 == strcmp(ifname, 372 (0 == strcmp (ifname,
367 GNUNET_DEFAULT_INTERFACE)), 373 GNUNET_DEFAULT_INTERFACE)),
368 (const struct sockaddr *)&a6, 374 (const struct sockaddr *) &a6,
369 NULL, 375 NULL,
370 (const struct sockaddr *)&netmask6, 376 (const struct sockaddr *) &netmask6,
371 sizeof(a6))) 377 sizeof(a6)))
372 break; 378 break;
373 }
374 } 379 }
375 pclose(f); 380 }
381 pclose (f);
376 return GNUNET_OK; 382 return GNUNET_OK;
377} 383}
378#endif 384#endif
@@ -385,49 +391,49 @@ try_ip(GNUNET_OS_NetworkInterfaceProcessor proc,
385 * @param proc_cls closure for @a proc 391 * @param proc_cls closure for @a proc
386 */ 392 */
387void 393void
388GNUNET_OS_network_interfaces_list(GNUNET_OS_NetworkInterfaceProcessor proc, 394GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
389 void *proc_cls) 395 void *proc_cls)
390{ 396{
391#if HAVE_GETIFADDRS && HAVE_FREEIFADDRS 397#if HAVE_GETIFADDRS && HAVE_FREEIFADDRS
392 struct ifaddrs *ifa_first; 398 struct ifaddrs *ifa_first;
393 struct ifaddrs *ifa_ptr; 399 struct ifaddrs *ifa_ptr;
394 socklen_t alen; 400 socklen_t alen;
395 401
396 if (getifaddrs(&ifa_first) == 0) 402 if (getifaddrs (&ifa_first) == 0)
403 {
404 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
397 { 405 {
398 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) 406 if ((ifa_ptr->ifa_name != NULL) &&(ifa_ptr->ifa_addr != NULL) &&
399 { 407 ( (ifa_ptr->ifa_flags & IFF_UP) != 0) )
400 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL && 408 {
401 (ifa_ptr->ifa_flags & IFF_UP) != 0) 409 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
402 { 410 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
403 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) && 411 continue;
404 (ifa_ptr->ifa_addr->sa_family != AF_INET6)) 412 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
405 continue; 413 alen = sizeof(struct sockaddr_in);
406 if (ifa_ptr->ifa_addr->sa_family == AF_INET) 414 else
407 alen = sizeof(struct sockaddr_in); 415 alen = sizeof(struct sockaddr_in6);
408 else 416 if (GNUNET_OK !=
409 alen = sizeof(struct sockaddr_in6); 417 proc (proc_cls, ifa_ptr->ifa_name,
410 if (GNUNET_OK != 418 (0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE)),
411 proc(proc_cls, ifa_ptr->ifa_name, 419 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
412 0 == strcmp(ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE), 420 ifa_ptr->ifa_netmask, alen))
413 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr, 421 break;
414 ifa_ptr->ifa_netmask, alen)) 422 }
415 break;
416 }
417 }
418 freeifaddrs(ifa_first);
419 } 423 }
424 freeifaddrs (ifa_first);
425 }
420#else 426#else
421 if (GNUNET_OK == 427 if (GNUNET_OK ==
422 try_ip(proc, 428 try_ip (proc,
423 proc_cls)) 429 proc_cls))
424 return; 430 return;
425 if (GNUNET_OK == 431 if (GNUNET_OK ==
426 try_ifconfig(proc, 432 try_ifconfig (proc,
427 proc_cls)) 433 proc_cls))
428 return; 434 return;
429 LOG(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 435 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
430 "Failed to enumerate network interfaces\n"); 436 "Failed to enumerate network interfaces\n");
431#endif 437#endif
432} 438}
433 439
diff --git a/src/util/os_priority.c b/src/util/os_priority.c
index e25623af5..ad510a9b3 100644
--- a/src/util/os_priority.c
+++ b/src/util/os_priority.c
@@ -29,18 +29,19 @@
29#include "disk.h" 29#include "disk.h"
30#include <unistr.h> 30#include <unistr.h>
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-os-priority", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)
33 33
34#define LOG_STRERROR(kind, syscall) \ 34#define LOG_STRERROR(kind, syscall) \
35 GNUNET_log_from_strerror(kind, "util-os-priority", syscall) 35 GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
36 36
37#define LOG_STRERROR_FILE(kind, syscall, filename) \ 37#define LOG_STRERROR_FILE(kind, syscall, filename) \
38 GNUNET_log_from_strerror_file(kind, "util-os-priority", syscall, filename) 38 GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
39 39
40#define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE" 40#define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
41 41
42 42
43struct GNUNET_OS_Process { 43struct GNUNET_OS_Process
44{
44 /** 45 /**
45 * PID of the process. 46 * PID of the process.
46 */ 47 */
@@ -77,13 +78,13 @@ static struct GNUNET_SCHEDULER_Task *spch;
77 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe 78 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
78 */ 79 */
79static void 80static void
80shutdown_pch(void *cls) 81shutdown_pch (void *cls)
81{ 82{
82 struct GNUNET_DISK_FileHandle *control_pipe = cls; 83 struct GNUNET_DISK_FileHandle *control_pipe = cls;
83 84
84 GNUNET_SCHEDULER_cancel(pch); 85 GNUNET_SCHEDULER_cancel (pch);
85 pch = NULL; 86 pch = NULL;
86 GNUNET_DISK_file_close(control_pipe); 87 GNUNET_DISK_file_close (control_pipe);
87 control_pipe = NULL; 88 control_pipe = NULL;
88} 89}
89 90
@@ -94,7 +95,7 @@ shutdown_pch(void *cls)
94 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe 95 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
95 */ 96 */
96static void 97static void
97parent_control_handler(void *cls) 98parent_control_handler (void *cls)
98{ 99{
99 struct GNUNET_DISK_FileHandle *control_pipe = cls; 100 struct GNUNET_DISK_FileHandle *control_pipe = cls;
100 char sig; 101 char sig;
@@ -102,29 +103,29 @@ parent_control_handler(void *cls)
102 ssize_t ret; 103 ssize_t ret;
103 104
104 pch = NULL; 105 pch = NULL;
105 ret = GNUNET_DISK_file_read(control_pipe, &sig, sizeof(sig)); 106 ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof(sig));
106 if (sizeof(sig) != ret) 107 if (sizeof(sig) != ret)
107 { 108 {
108 if (-1 == ret) 109 if (-1 == ret)
109 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read"); 110 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
110 LOG(GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n"); 111 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
111 GNUNET_DISK_file_close(control_pipe); 112 GNUNET_DISK_file_close (control_pipe);
112 control_pipe = NULL; 113 control_pipe = NULL;
113 GNUNET_SCHEDULER_cancel(spch); 114 GNUNET_SCHEDULER_cancel (spch);
114 spch = NULL; 115 spch = NULL;
115 return; 116 return;
116 } 117 }
117 pipe_fd = getenv(GNUNET_OS_CONTROL_PIPE); 118 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
118 GNUNET_assert((NULL == pipe_fd) || (strlen(pipe_fd) <= 0)); 119 GNUNET_assert ((NULL == pipe_fd) || (strlen (pipe_fd) <= 0));
119 LOG(GNUNET_ERROR_TYPE_DEBUG, 120 LOG (GNUNET_ERROR_TYPE_DEBUG,
120 "Got control code %d from parent via pipe %s\n", 121 "Got control code %d from parent via pipe %s\n",
121 sig, 122 sig,
122 pipe_fd); 123 pipe_fd);
123 pch = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 124 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
124 control_pipe, 125 control_pipe,
125 &parent_control_handler, 126 &parent_control_handler,
126 control_pipe); 127 control_pipe);
127 GNUNET_SIGNAL_raise((int)sig); 128 GNUNET_SIGNAL_raise ((int) sig);
128} 129}
129 130
130 131
@@ -137,63 +138,63 @@ parent_control_handler(void *cls)
137 * @param cls closure (unused) 138 * @param cls closure (unused)
138 */ 139 */
139void 140void
140GNUNET_OS_install_parent_control_handler(void *cls) 141GNUNET_OS_install_parent_control_handler (void *cls)
141{ 142{
142 const char *env_buf; 143 const char *env_buf;
143 char *env_buf_end; 144 char *env_buf_end;
144 struct GNUNET_DISK_FileHandle *control_pipe; 145 struct GNUNET_DISK_FileHandle *control_pipe;
145 uint64_t pipe_fd; 146 uint64_t pipe_fd;
146 147
147 (void)cls; 148 (void) cls;
148 if (NULL != pch) 149 if (NULL != pch)
149 { 150 {
150 /* already done, we've been called twice... */ 151 /* already done, we've been called twice... */
151 GNUNET_break(0); 152 GNUNET_break (0);
152 return; 153 return;
153 } 154 }
154 env_buf = getenv(GNUNET_OS_CONTROL_PIPE); 155 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
155 if ((NULL == env_buf) || (strlen(env_buf) <= 0)) 156 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
156 { 157 {
157 LOG(GNUNET_ERROR_TYPE_DEBUG, 158 LOG (GNUNET_ERROR_TYPE_DEBUG,
158 "Not installing a handler because $%s is empty\n", 159 "Not installing a handler because $%s is empty\n",
159 GNUNET_OS_CONTROL_PIPE); 160 GNUNET_OS_CONTROL_PIPE);
160 putenv(GNUNET_OS_CONTROL_PIPE "="); 161 putenv (GNUNET_OS_CONTROL_PIPE "=");
161 return; 162 return;
162 } 163 }
163 errno = 0; 164 errno = 0;
164 pipe_fd = strtoull(env_buf, &env_buf_end, 16); 165 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
165 if ((0 != errno) || (env_buf == env_buf_end)) 166 if ((0 != errno) || (env_buf == env_buf_end))
166 { 167 {
167 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf); 168 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf);
168 putenv(GNUNET_OS_CONTROL_PIPE "="); 169 putenv (GNUNET_OS_CONTROL_PIPE "=");
169 return; 170 return;
170 } 171 }
171 if (pipe_fd >= FD_SETSIZE) 172 if (pipe_fd >= FD_SETSIZE)
172 { 173 {
173 LOG(GNUNET_ERROR_TYPE_ERROR, 174 LOG (GNUNET_ERROR_TYPE_ERROR,
174 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n", 175 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
175 env_buf); 176 env_buf);
176 putenv(GNUNET_OS_CONTROL_PIPE "="); 177 putenv (GNUNET_OS_CONTROL_PIPE "=");
177 return; 178 return;
178 } 179 }
179 180
180 control_pipe = GNUNET_DISK_get_handle_from_int_fd((int)pipe_fd); 181 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
181 182
182 if (NULL == control_pipe) 183 if (NULL == control_pipe)
183 { 184 {
184 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "open", env_buf); 185 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
185 putenv(GNUNET_OS_CONTROL_PIPE "="); 186 putenv (GNUNET_OS_CONTROL_PIPE "=");
186 return; 187 return;
187 } 188 }
188 LOG(GNUNET_ERROR_TYPE_DEBUG, 189 LOG (GNUNET_ERROR_TYPE_DEBUG,
189 "Adding parent control handler pipe `%s' to the scheduler\n", 190 "Adding parent control handler pipe `%s' to the scheduler\n",
190 env_buf); 191 env_buf);
191 pch = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 192 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
192 control_pipe, 193 control_pipe,
193 &parent_control_handler, 194 &parent_control_handler,
194 control_pipe); 195 control_pipe);
195 spch = GNUNET_SCHEDULER_add_shutdown(&shutdown_pch, control_pipe); 196 spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch, control_pipe);
196 putenv(GNUNET_OS_CONTROL_PIPE "="); 197 putenv (GNUNET_OS_CONTROL_PIPE "=");
197} 198}
198 199
199 200
@@ -206,7 +207,7 @@ GNUNET_OS_install_parent_control_handler(void *cls)
206 * @return pointer to the process sturcutre for this process 207 * @return pointer to the process sturcutre for this process
207 */ 208 */
208struct GNUNET_OS_Process * 209struct GNUNET_OS_Process *
209GNUNET_OS_process_current() 210GNUNET_OS_process_current ()
210{ 211{
211 current_process.pid = 0; 212 current_process.pid = 0;
212 return &current_process; 213 return &current_process;
@@ -221,44 +222,44 @@ GNUNET_OS_process_current()
221 * @return 0 on success, -1 on error 222 * @return 0 on success, -1 on error
222 */ 223 */
223int 224int
224GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig) 225GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
225{ 226{
226 int ret; 227 int ret;
227 char csig; 228 char csig;
228 229
229 csig = (char)sig; 230 csig = (char) sig;
230 if (NULL != proc->control_pipe) 231 if (NULL != proc->control_pipe)
231 { 232 {
232 LOG(GNUNET_ERROR_TYPE_DEBUG, 233 LOG (GNUNET_ERROR_TYPE_DEBUG,
233 "Sending signal %d to pid: %u via pipe\n", 234 "Sending signal %d to pid: %u via pipe\n",
234 sig, 235 sig,
235 proc->pid); 236 proc->pid);
236 ret = GNUNET_DISK_file_write(proc->control_pipe, &csig, sizeof(csig)); 237 ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof(csig));
237 if (sizeof(csig) == ret) 238 if (sizeof(csig) == ret)
238 return 0; 239 return 0;
239 } 240 }
240 /* pipe failed or non-existent, try other methods */ 241 /* pipe failed or non-existent, try other methods */
241 switch (sig) 242 switch (sig)
242 { 243 {
243 case SIGHUP: 244 case SIGHUP:
244 case SIGINT: 245 case SIGINT:
245 case SIGKILL: 246 case SIGKILL:
246 case SIGTERM: 247 case SIGTERM:
247#if (SIGTERM != GNUNET_TERM_SIG) 248#if (SIGTERM != GNUNET_TERM_SIG)
248 case GNUNET_TERM_SIG: 249 case GNUNET_TERM_SIG:
249#endif 250#endif
250 LOG(GNUNET_ERROR_TYPE_DEBUG, 251 LOG (GNUNET_ERROR_TYPE_DEBUG,
251 "Sending signal %d to pid: %u via system call\n", 252 "Sending signal %d to pid: %u via system call\n",
252 sig, 253 sig,
253 proc->pid); 254 proc->pid);
254 return kill(proc->pid, sig); 255 return kill (proc->pid, sig);
255 default: 256 default:
256 LOG(GNUNET_ERROR_TYPE_DEBUG, 257 LOG (GNUNET_ERROR_TYPE_DEBUG,
257 "Sending signal %d to pid: %u via system call\n", 258 "Sending signal %d to pid: %u via system call\n",
258 sig, 259 sig,
259 proc->pid); 260 proc->pid);
260 return kill(proc->pid, sig); 261 return kill (proc->pid, sig);
261 } 262 }
262} 263}
263 264
264 265
@@ -270,7 +271,7 @@ GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
270 * @return the current process id 271 * @return the current process id
271 */ 272 */
272pid_t 273pid_t
273GNUNET_OS_process_get_pid(struct GNUNET_OS_Process *proc) 274GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc)
274{ 275{
275 return proc->pid; 276 return proc->pid;
276} 277}
@@ -283,12 +284,12 @@ GNUNET_OS_process_get_pid(struct GNUNET_OS_Process *proc)
283 * @param proc pointer to process structure 284 * @param proc pointer to process structure
284 */ 285 */
285void 286void
286GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc) 287GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
287{ 288{
288 if (NULL != proc->control_pipe) 289 if (NULL != proc->control_pipe)
289 GNUNET_DISK_file_close(proc->control_pipe); 290 GNUNET_DISK_file_close (proc->control_pipe);
290 291
291 GNUNET_free(proc); 292 GNUNET_free (proc);
292} 293}
293 294
294 295
@@ -301,25 +302,25 @@ GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
301 * @param flags open flags (O_RDONLY, O_WRONLY) 302 * @param flags open flags (O_RDONLY, O_WRONLY)
302 */ 303 */
303static void 304static void
304open_dev_null(int target_fd, int flags) 305open_dev_null (int target_fd, int flags)
305{ 306{
306 int fd; 307 int fd;
307 308
308 fd = open("/dev/null", flags); 309 fd = open ("/dev/null", flags);
309 if (-1 == fd) 310 if (-1 == fd)
310 { 311 {
311 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null"); 312 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
312 return; 313 return;
313 } 314 }
314 if (fd == target_fd) 315 if (fd == target_fd)
315 return; 316 return;
316 if (-1 == dup2(fd, target_fd)) 317 if (-1 == dup2 (fd, target_fd))
317 { 318 {
318 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "dup2"); 319 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
319 (void)close(fd); 320 (void) close (fd);
320 return; 321 return;
321 } 322 }
322 GNUNET_break(0 == close(fd)); 323 GNUNET_break (0 == close (fd));
323} 324}
324 325
325 326
@@ -341,14 +342,14 @@ open_dev_null(int target_fd, int flags)
341 * @return process ID of the new process, -1 on error 342 * @return process ID of the new process, -1 on error
342 */ 343 */
343static struct GNUNET_OS_Process * 344static struct GNUNET_OS_Process *
344start_process(int pipe_control, 345start_process (int pipe_control,
345 enum GNUNET_OS_InheritStdioFlags std_inheritance, 346 enum GNUNET_OS_InheritStdioFlags std_inheritance,
346 struct GNUNET_DISK_PipeHandle *pipe_stdin, 347 struct GNUNET_DISK_PipeHandle *pipe_stdin,
347 struct GNUNET_DISK_PipeHandle *pipe_stdout, 348 struct GNUNET_DISK_PipeHandle *pipe_stdout,
348 struct GNUNET_DISK_PipeHandle *pipe_stderr, 349 struct GNUNET_DISK_PipeHandle *pipe_stderr,
349 const SOCKTYPE *lsocks, 350 const SOCKTYPE *lsocks,
350 const char *filename, 351 const char *filename,
351 char *const argv[]) 352 char *const argv[])
352{ 353{
353 pid_t ret; 354 pid_t ret;
354 char fds[16]; 355 char fds[16];
@@ -371,223 +372,223 @@ start_process(int pipe_control,
371 int fd_stdin_write; 372 int fd_stdin_write;
372 373
373 if (GNUNET_SYSERR == 374 if (GNUNET_SYSERR ==
374 GNUNET_OS_check_helper_binary(filename, GNUNET_NO, NULL)) 375 GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
375 return NULL; /* not executable */ 376 return NULL; /* not executable */
376 if (GNUNET_YES == pipe_control) 377 if (GNUNET_YES == pipe_control)
378 {
379 struct GNUNET_DISK_PipeHandle *childpipe;
380 int dup_childpipe_read_fd = -1;
381
382 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
383 if (NULL == childpipe)
384 return NULL;
385 childpipe_read =
386 GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
387 childpipe_write =
388 GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
389 GNUNET_DISK_pipe_close (childpipe);
390 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
391 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
392 &childpipe_read_fd,
393 sizeof(int))) ||
394 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
377 { 395 {
378 struct GNUNET_DISK_PipeHandle *childpipe; 396 if (NULL != childpipe_read)
379 int dup_childpipe_read_fd = -1; 397 GNUNET_DISK_file_close (childpipe_read);
380 398 if (NULL != childpipe_write)
381 childpipe = GNUNET_DISK_pipe(GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO); 399 GNUNET_DISK_file_close (childpipe_write);
382 if (NULL == childpipe) 400 if (0 <= dup_childpipe_read_fd)
383 return NULL; 401 close (dup_childpipe_read_fd);
384 childpipe_read = 402 return NULL;
385 GNUNET_DISK_pipe_detach_end(childpipe, GNUNET_DISK_PIPE_END_READ);
386 childpipe_write =
387 GNUNET_DISK_pipe_detach_end(childpipe, GNUNET_DISK_PIPE_END_WRITE);
388 GNUNET_DISK_pipe_close(childpipe);
389 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
390 (GNUNET_OK != GNUNET_DISK_internal_file_handle_(childpipe_read,
391 &childpipe_read_fd,
392 sizeof(int))) ||
393 (-1 == (dup_childpipe_read_fd = dup(childpipe_read_fd))))
394 {
395 if (NULL != childpipe_read)
396 GNUNET_DISK_file_close(childpipe_read);
397 if (NULL != childpipe_write)
398 GNUNET_DISK_file_close(childpipe_write);
399 if (0 <= dup_childpipe_read_fd)
400 close(dup_childpipe_read_fd);
401 return NULL;
402 }
403 childpipe_read_fd = dup_childpipe_read_fd;
404 GNUNET_DISK_file_close(childpipe_read);
405 } 403 }
404 childpipe_read_fd = dup_childpipe_read_fd;
405 GNUNET_DISK_file_close (childpipe_read);
406 }
406 else 407 else
407 { 408 {
408 childpipe_write = NULL; 409 childpipe_write = NULL;
409 childpipe_read_fd = -1; 410 childpipe_read_fd = -1;
410 } 411 }
411 if (NULL != pipe_stdin) 412 if (NULL != pipe_stdin)
412 { 413 {
413 GNUNET_assert( 414 GNUNET_assert (
414 GNUNET_OK == 415 GNUNET_OK ==
415 GNUNET_DISK_internal_file_handle_( 416 GNUNET_DISK_internal_file_handle_ (
416 GNUNET_DISK_pipe_handle(pipe_stdin, GNUNET_DISK_PIPE_END_READ), 417 GNUNET_DISK_pipe_handle (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
417 &fd_stdin_read, 418 &fd_stdin_read,
418 sizeof(int))); 419 sizeof(int)));
419 GNUNET_assert( 420 GNUNET_assert (
420 GNUNET_OK == 421 GNUNET_OK ==
421 GNUNET_DISK_internal_file_handle_( 422 GNUNET_DISK_internal_file_handle_ (
422 GNUNET_DISK_pipe_handle(pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), 423 GNUNET_DISK_pipe_handle (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
423 &fd_stdin_write, 424 &fd_stdin_write,
424 sizeof(int))); 425 sizeof(int)));
425 } 426 }
426 if (NULL != pipe_stdout) 427 if (NULL != pipe_stdout)
427 { 428 {
428 GNUNET_assert( 429 GNUNET_assert (
429 GNUNET_OK == 430 GNUNET_OK ==
430 GNUNET_DISK_internal_file_handle_( 431 GNUNET_DISK_internal_file_handle_ (
431 GNUNET_DISK_pipe_handle(pipe_stdout, GNUNET_DISK_PIPE_END_WRITE), 432 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE),
432 &fd_stdout_write, 433 &fd_stdout_write,
433 sizeof(int))); 434 sizeof(int)));
434 GNUNET_assert( 435 GNUNET_assert (
435 GNUNET_OK == 436 GNUNET_OK ==
436 GNUNET_DISK_internal_file_handle_( 437 GNUNET_DISK_internal_file_handle_ (
437 GNUNET_DISK_pipe_handle(pipe_stdout, GNUNET_DISK_PIPE_END_READ), 438 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
438 &fd_stdout_read, 439 &fd_stdout_read,
439 sizeof(int))); 440 sizeof(int)));
440 } 441 }
441 if (NULL != pipe_stderr) 442 if (NULL != pipe_stderr)
442 { 443 {
443 GNUNET_assert( 444 GNUNET_assert (
444 GNUNET_OK == 445 GNUNET_OK ==
445 GNUNET_DISK_internal_file_handle_( 446 GNUNET_DISK_internal_file_handle_ (
446 GNUNET_DISK_pipe_handle(pipe_stderr, GNUNET_DISK_PIPE_END_READ), 447 GNUNET_DISK_pipe_handle (pipe_stderr, GNUNET_DISK_PIPE_END_READ),
447 &fd_stderr_read, 448 &fd_stderr_read,
448 sizeof(int))); 449 sizeof(int)));
449 GNUNET_assert( 450 GNUNET_assert (
450 GNUNET_OK == 451 GNUNET_OK ==
451 GNUNET_DISK_internal_file_handle_( 452 GNUNET_DISK_internal_file_handle_ (
452 GNUNET_DISK_pipe_handle(pipe_stderr, GNUNET_DISK_PIPE_END_WRITE), 453 GNUNET_DISK_pipe_handle (pipe_stderr, GNUNET_DISK_PIPE_END_WRITE),
453 &fd_stderr_write, 454 &fd_stderr_write,
454 sizeof(int))); 455 sizeof(int)));
455 } 456 }
456 lscp = NULL; 457 lscp = NULL;
457 ls = 0; 458 ls = 0;
458 if (NULL != lsocks) 459 if (NULL != lsocks)
459 { 460 {
460 i = 0; 461 i = 0;
461 while (-1 != (k = lsocks[i++])) 462 while (-1 != (k = lsocks[i++]))
462 GNUNET_array_append(lscp, ls, k); 463 GNUNET_array_append (lscp, ls, k);
463 GNUNET_array_append(lscp, ls, -1); 464 GNUNET_array_append (lscp, ls, -1);
464 } 465 }
465#if DARWIN 466#if DARWIN
466 /* see https://web.archive.org/web/20150924082249/gnunet.org/vfork */ 467 /* see https://web.archive.org/web/20150924082249/gnunet.org/vfork */
467 ret = vfork(); 468 ret = vfork ();
468#else 469#else
469 ret = fork(); 470 ret = fork ();
470#endif 471#endif
471 if (-1 == ret) 472 if (-1 == ret)
472 { 473 {
473 int eno = errno; 474 int eno = errno;
474 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "fork"); 475 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
475 GNUNET_array_grow(lscp, ls, 0); 476 GNUNET_array_grow (lscp, ls, 0);
476 if (NULL != childpipe_write) 477 if (NULL != childpipe_write)
477 GNUNET_DISK_file_close(childpipe_write); 478 GNUNET_DISK_file_close (childpipe_write);
478 if (0 <= childpipe_read_fd) 479 if (0 <= childpipe_read_fd)
479 close(childpipe_read_fd); 480 close (childpipe_read_fd);
480 errno = eno; 481 errno = eno;
481 return NULL; 482 return NULL;
482 } 483 }
483 if (0 != ret) 484 if (0 != ret)
485 {
486 unsetenv (GNUNET_OS_CONTROL_PIPE);
487 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
488 gnunet_proc->pid = ret;
489 gnunet_proc->control_pipe = childpipe_write;
490 if (GNUNET_YES == pipe_control)
484 { 491 {
485 unsetenv(GNUNET_OS_CONTROL_PIPE); 492 close (childpipe_read_fd);
486 gnunet_proc = GNUNET_new(struct GNUNET_OS_Process);
487 gnunet_proc->pid = ret;
488 gnunet_proc->control_pipe = childpipe_write;
489 if (GNUNET_YES == pipe_control)
490 {
491 close(childpipe_read_fd);
492 }
493 GNUNET_array_grow(lscp, ls, 0);
494 return gnunet_proc;
495 } 493 }
494 GNUNET_array_grow (lscp, ls, 0);
495 return gnunet_proc;
496 }
496 if (0 <= childpipe_read_fd) 497 if (0 <= childpipe_read_fd)
497 { 498 {
498 char fdbuf[100]; 499 char fdbuf[100];
499#ifndef DARWIN 500#ifndef DARWIN
500 /* due to vfork, we must NOT free memory on DARWIN! */ 501 /* due to vfork, we must NOT free memory on DARWIN! */
501 GNUNET_DISK_file_close(childpipe_write); 502 GNUNET_DISK_file_close (childpipe_write);
502#endif 503#endif
503 snprintf(fdbuf, 100, "%x", childpipe_read_fd); 504 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
504 setenv(GNUNET_OS_CONTROL_PIPE, fdbuf, 1); 505 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
505 } 506 }
506 else 507 else
507 unsetenv(GNUNET_OS_CONTROL_PIPE); 508 unsetenv (GNUNET_OS_CONTROL_PIPE);
508 if (NULL != pipe_stdin) 509 if (NULL != pipe_stdin)
509 { 510 {
510 GNUNET_break(0 == close(fd_stdin_write)); 511 GNUNET_break (0 == close (fd_stdin_write));
511 if (-1 == dup2(fd_stdin_read, 0)) 512 if (-1 == dup2 (fd_stdin_read, 0))
512 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "dup2"); 513 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
513 GNUNET_break(0 == close(fd_stdin_read)); 514 GNUNET_break (0 == close (fd_stdin_read));
514 } 515 }
515 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN)) 516 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
516 { 517 {
517 GNUNET_break(0 == close(0)); 518 GNUNET_break (0 == close (0));
518 open_dev_null(0, O_RDONLY); 519 open_dev_null (0, O_RDONLY);
519 } 520 }
520 if (NULL != pipe_stdout) 521 if (NULL != pipe_stdout)
521 { 522 {
522 GNUNET_break(0 == close(fd_stdout_read)); 523 GNUNET_break (0 == close (fd_stdout_read));
523 if (-1 == dup2(fd_stdout_write, 1)) 524 if (-1 == dup2 (fd_stdout_write, 1))
524 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "dup2"); 525 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
525 GNUNET_break(0 == close(fd_stdout_write)); 526 GNUNET_break (0 == close (fd_stdout_write));
526 } 527 }
527 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)) 528 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
528 { 529 {
529 GNUNET_break(0 == close(1)); 530 GNUNET_break (0 == close (1));
530 open_dev_null(1, O_WRONLY); 531 open_dev_null (1, O_WRONLY);
531 } 532 }
532 if (NULL != pipe_stderr) 533 if (NULL != pipe_stderr)
533 { 534 {
534 GNUNET_break(0 == close(fd_stderr_read)); 535 GNUNET_break (0 == close (fd_stderr_read));
535 if (-1 == dup2(fd_stderr_write, 2)) 536 if (-1 == dup2 (fd_stderr_write, 2))
536 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "dup2"); 537 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
537 GNUNET_break(0 == close(fd_stderr_write)); 538 GNUNET_break (0 == close (fd_stderr_write));
538 } 539 }
539 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)) 540 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
540 { 541 {
541 GNUNET_break(0 == close(2)); 542 GNUNET_break (0 == close (2));
542 open_dev_null(2, O_WRONLY); 543 open_dev_null (2, O_WRONLY);
543 } 544 }
544 if (NULL != lscp) 545 if (NULL != lscp)
546 {
547 /* read systemd documentation... */
548 i = 0;
549 tgt = 3;
550 while (-1 != lscp[i])
545 { 551 {
546 /* read systemd documentation... */ 552 j = i + 1;
547 i = 0; 553 while (-1 != lscp[j])
548 tgt = 3; 554 {
549 while (-1 != lscp[i]) 555 if (lscp[j] == tgt)
550 { 556 {
551 j = i + 1; 557 /* dup away */
552 while (-1 != lscp[j]) 558 k = dup (lscp[j]);
553 { 559 GNUNET_assert (-1 != k);
554 if (lscp[j] == tgt) 560 GNUNET_assert (0 == close (lscp[j]));
555 { 561 lscp[j] = k;
556 /* dup away */ 562 break;
557 k = dup(lscp[j]);
558 GNUNET_assert(-1 != k);
559 GNUNET_assert(0 == close(lscp[j]));
560 lscp[j] = k;
561 break;
562 }
563 j++;
564 }
565 if (lscp[i] != tgt)
566 {
567 /* Bury any existing FD, no matter what; they should all be closed
568 * on exec anyway and the important onces have been dup'ed away */
569 (void)close(tgt);
570 GNUNET_assert(-1 != dup2(lscp[i], tgt));
571 }
572 /* unset close-on-exec flag */
573 flags = fcntl(tgt, F_GETFD);
574 GNUNET_assert(flags >= 0);
575 flags &= ~FD_CLOEXEC;
576 fflush(stderr);
577 (void)fcntl(tgt, F_SETFD, flags);
578 tgt++;
579 i++;
580 } 563 }
581 GNUNET_snprintf(fds, sizeof(fds), "%u", i); 564 j++;
582 setenv("LISTEN_FDS", fds, 1); 565 }
566 if (lscp[i] != tgt)
567 {
568 /* Bury any existing FD, no matter what; they should all be closed
569 * on exec anyway and the important onces have been dup'ed away */
570 (void) close (tgt);
571 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
572 }
573 /* unset close-on-exec flag */
574 flags = fcntl (tgt, F_GETFD);
575 GNUNET_assert (flags >= 0);
576 flags &= ~FD_CLOEXEC;
577 fflush (stderr);
578 (void) fcntl (tgt, F_SETFD, flags);
579 tgt++;
580 i++;
583 } 581 }
582 GNUNET_snprintf (fds, sizeof(fds), "%u", i);
583 setenv ("LISTEN_FDS", fds, 1);
584 }
584#ifndef DARWIN 585#ifndef DARWIN
585 /* due to vfork, we must NOT free memory on DARWIN! */ 586 /* due to vfork, we must NOT free memory on DARWIN! */
586 GNUNET_array_grow(lscp, ls, 0); 587 GNUNET_array_grow (lscp, ls, 0);
587#endif 588#endif
588 execvp(filename, argv); 589 execvp (filename, argv);
589 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "execvp", filename); 590 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
590 _exit(1); 591 _exit (1);
591} 592}
592 593
593 594
@@ -604,22 +605,22 @@ start_process(int pipe_control,
604 * @return pointer to process structure of the new process, NULL on error 605 * @return pointer to process structure of the new process, NULL on error
605 */ 606 */
606struct GNUNET_OS_Process * 607struct GNUNET_OS_Process *
607GNUNET_OS_start_process_vap(int pipe_control, 608GNUNET_OS_start_process_vap (int pipe_control,
608 enum GNUNET_OS_InheritStdioFlags std_inheritance, 609 enum GNUNET_OS_InheritStdioFlags std_inheritance,
609 struct GNUNET_DISK_PipeHandle *pipe_stdin, 610 struct GNUNET_DISK_PipeHandle *pipe_stdin,
610 struct GNUNET_DISK_PipeHandle *pipe_stdout, 611 struct GNUNET_DISK_PipeHandle *pipe_stdout,
611 struct GNUNET_DISK_PipeHandle *pipe_stderr, 612 struct GNUNET_DISK_PipeHandle *pipe_stderr,
612 const char *filename, 613 const char *filename,
613 char *const argv[]) 614 char *const argv[])
614{ 615{
615 return start_process(pipe_control, 616 return start_process (pipe_control,
616 std_inheritance, 617 std_inheritance,
617 pipe_stdin, 618 pipe_stdin,
618 pipe_stdout, 619 pipe_stdout,
619 pipe_stderr, 620 pipe_stderr,
620 NULL, 621 NULL,
621 filename, 622 filename,
622 argv); 623 argv);
623} 624}
624 625
625 626
@@ -636,13 +637,13 @@ GNUNET_OS_start_process_vap(int pipe_control,
636 * @return pointer to process structure of the new process, NULL on error 637 * @return pointer to process structure of the new process, NULL on error
637 */ 638 */
638struct GNUNET_OS_Process * 639struct GNUNET_OS_Process *
639GNUNET_OS_start_process_va(int pipe_control, 640GNUNET_OS_start_process_va (int pipe_control,
640 enum GNUNET_OS_InheritStdioFlags std_inheritance, 641 enum GNUNET_OS_InheritStdioFlags std_inheritance,
641 struct GNUNET_DISK_PipeHandle *pipe_stdin, 642 struct GNUNET_DISK_PipeHandle *pipe_stdin,
642 struct GNUNET_DISK_PipeHandle *pipe_stdout, 643 struct GNUNET_DISK_PipeHandle *pipe_stdout,
643 struct GNUNET_DISK_PipeHandle *pipe_stderr, 644 struct GNUNET_DISK_PipeHandle *pipe_stderr,
644 const char *filename, 645 const char *filename,
645 va_list va) 646 va_list va)
646{ 647{
647 struct GNUNET_OS_Process *ret; 648 struct GNUNET_OS_Process *ret;
648 va_list ap; 649 va_list ap;
@@ -650,24 +651,24 @@ GNUNET_OS_start_process_va(int pipe_control,
650 int argc; 651 int argc;
651 652
652 argc = 0; 653 argc = 0;
653 va_copy(ap, va); 654 va_copy (ap, va);
654 while (NULL != va_arg(ap, char *)) 655 while (NULL != va_arg (ap, char *))
655 argc++; 656 argc++;
656 va_end(ap); 657 va_end (ap);
657 argv = GNUNET_malloc(sizeof(char *) * (argc + 1)); 658 argv = GNUNET_malloc (sizeof(char *) * (argc + 1));
658 argc = 0; 659 argc = 0;
659 va_copy(ap, va); 660 va_copy (ap, va);
660 while (NULL != (argv[argc] = va_arg(ap, char *))) 661 while (NULL != (argv[argc] = va_arg (ap, char *)))
661 argc++; 662 argc++;
662 va_end(ap); 663 va_end (ap);
663 ret = GNUNET_OS_start_process_vap(pipe_control, 664 ret = GNUNET_OS_start_process_vap (pipe_control,
664 std_inheritance, 665 std_inheritance,
665 pipe_stdin, 666 pipe_stdin,
666 pipe_stdout, 667 pipe_stdout,
667 pipe_stderr, 668 pipe_stderr,
668 filename, 669 filename,
669 argv); 670 argv);
670 GNUNET_free(argv); 671 GNUNET_free (argv);
671 return ret; 672 return ret;
672} 673}
673 674
@@ -684,26 +685,26 @@ GNUNET_OS_start_process_va(int pipe_control,
684 * @return pointer to process structure of the new process, NULL on error 685 * @return pointer to process structure of the new process, NULL on error
685 */ 686 */
686struct GNUNET_OS_Process * 687struct GNUNET_OS_Process *
687GNUNET_OS_start_process(int pipe_control, 688GNUNET_OS_start_process (int pipe_control,
688 enum GNUNET_OS_InheritStdioFlags std_inheritance, 689 enum GNUNET_OS_InheritStdioFlags std_inheritance,
689 struct GNUNET_DISK_PipeHandle *pipe_stdin, 690 struct GNUNET_DISK_PipeHandle *pipe_stdin,
690 struct GNUNET_DISK_PipeHandle *pipe_stdout, 691 struct GNUNET_DISK_PipeHandle *pipe_stdout,
691 struct GNUNET_DISK_PipeHandle *pipe_stderr, 692 struct GNUNET_DISK_PipeHandle *pipe_stderr,
692 const char *filename, 693 const char *filename,
693 ...) 694 ...)
694{ 695{
695 struct GNUNET_OS_Process *ret; 696 struct GNUNET_OS_Process *ret;
696 va_list ap; 697 va_list ap;
697 698
698 va_start(ap, filename); 699 va_start (ap, filename);
699 ret = GNUNET_OS_start_process_va(pipe_control, 700 ret = GNUNET_OS_start_process_va (pipe_control,
700 std_inheritance, 701 std_inheritance,
701 pipe_stdin, 702 pipe_stdin,
702 pipe_stdout, 703 pipe_stdout,
703 pipe_stderr, 704 pipe_stderr,
704 filename, 705 filename,
705 ap); 706 ap);
706 va_end(ap); 707 va_end (ap);
707 return ret; 708 return ret;
708} 709}
709 710
@@ -723,20 +724,20 @@ GNUNET_OS_start_process(int pipe_control,
723 * @return process ID of the new process, -1 on error 724 * @return process ID of the new process, -1 on error
724 */ 725 */
725struct GNUNET_OS_Process * 726struct GNUNET_OS_Process *
726GNUNET_OS_start_process_v(int pipe_control, 727GNUNET_OS_start_process_v (int pipe_control,
727 enum GNUNET_OS_InheritStdioFlags std_inheritance, 728 enum GNUNET_OS_InheritStdioFlags std_inheritance,
728 const SOCKTYPE *lsocks, 729 const SOCKTYPE *lsocks,
729 const char *filename, 730 const char *filename,
730 char *const argv[]) 731 char *const argv[])
731{ 732{
732 return start_process(pipe_control, 733 return start_process (pipe_control,
733 std_inheritance, 734 std_inheritance,
734 NULL, 735 NULL,
735 NULL, 736 NULL,
736 NULL, 737 NULL,
737 lsocks, 738 lsocks,
738 filename, 739 filename,
739 argv); 740 argv);
740} 741}
741 742
742 743
@@ -759,11 +760,11 @@ GNUNET_OS_start_process_v(int pipe_control,
759 * @return pointer to process structure of the new process, NULL on error 760 * @return pointer to process structure of the new process, NULL on error
760 */ 761 */
761struct GNUNET_OS_Process * 762struct GNUNET_OS_Process *
762GNUNET_OS_start_process_s(int pipe_control, 763GNUNET_OS_start_process_s (int pipe_control,
763 unsigned int std_inheritance, 764 unsigned int std_inheritance,
764 const SOCKTYPE *lsocks, 765 const SOCKTYPE *lsocks,
765 const char *filename, 766 const char *filename,
766 ...) 767 ...)
767{ 768{
768 va_list ap; 769 va_list ap;
769 char **argv; 770 char **argv;
@@ -780,103 +781,103 @@ GNUNET_OS_start_process_s(int pipe_control,
780 size_t len; 781 size_t len;
781 782
782 argv_size = 1; 783 argv_size = 1;
783 va_start(ap, filename); 784 va_start (ap, filename);
784 arg = filename; 785 arg = filename;
785 last = NULL; 786 last = NULL;
786 do 787 do
788 {
789 rpos = arg;
790 quote_on = 0;
791 while ('\0' != *rpos)
787 { 792 {
788 rpos = arg; 793 if ('"' == *rpos)
789 quote_on = 0; 794 {
790 while ('\0' != *rpos) 795 if (1 == quote_on)
791 { 796 quote_on = 0;
792 if ('"' == *rpos) 797 else
793 { 798 quote_on = 1;
794 if (1 == quote_on) 799 }
795 quote_on = 0; 800 if ((' ' == *rpos) && (0 == quote_on))
796 else 801 {
797 quote_on = 1; 802 if (NULL != last)
798 } 803 argv_size++;
799 if ((' ' == *rpos) && (0 == quote_on)) 804 last = NULL;
800 { 805 rpos++;
801 if (NULL != last) 806 while (' ' == *rpos)
802 argv_size++; 807 rpos++;
803 last = NULL; 808 }
804 rpos++; 809 if ((NULL == last) && ('\0' != *rpos)) // FIXME: == or !=?
805 while (' ' == *rpos) 810 last = rpos;
806 rpos++; 811 if ('\0' != *rpos)
807 } 812 rpos++;
808 if ((NULL == last) && ('\0' != *rpos)) // FIXME: == or !=?
809 last = rpos;
810 if ('\0' != *rpos)
811 rpos++;
812 }
813 if (NULL != last)
814 argv_size++;
815 } 813 }
816 while (NULL != (arg = (va_arg(ap, const char *)))); 814 if (NULL != last)
817 va_end(ap); 815 argv_size++;
816 }
817 while (NULL != (arg = (va_arg (ap, const char *))));
818 va_end (ap);
818 819
819 argv = GNUNET_malloc(argv_size * sizeof(char *)); 820 argv = GNUNET_malloc (argv_size * sizeof(char *));
820 argv_size = 0; 821 argv_size = 0;
821 va_start(ap, filename); 822 va_start (ap, filename);
822 arg = filename; 823 arg = filename;
823 last = NULL; 824 last = NULL;
824 do 825 do
826 {
827 cp = GNUNET_strdup (arg);
828 quote_on = 0;
829 pos = cp;
830 while ('\0' != *pos)
825 { 831 {
826 cp = GNUNET_strdup(arg); 832 if ('"' == *pos)
827 quote_on = 0; 833 {
828 pos = cp; 834 if (1 == quote_on)
829 while ('\0' != *pos) 835 quote_on = 0;
830 { 836 else
831 if ('"' == *pos) 837 quote_on = 1;
832 { 838 }
833 if (1 == quote_on) 839 if ((' ' == *pos) && (0 == quote_on))
834 quote_on = 0; 840 {
835 else 841 *pos = '\0';
836 quote_on = 1; 842 if (NULL != last)
837 } 843 argv[argv_size++] = GNUNET_strdup (last);
838 if ((' ' == *pos) && (0 == quote_on)) 844 last = NULL;
839 { 845 pos++;
840 *pos = '\0'; 846 while (' ' == *pos)
841 if (NULL != last) 847 pos++;
842 argv[argv_size++] = GNUNET_strdup(last); 848 }
843 last = NULL; 849 if ((NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
844 pos++; 850 last = pos;
845 while (' ' == *pos) 851 if ('\0' != *pos)
846 pos++; 852 pos++;
847 }
848 if ((NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
849 last = pos;
850 if ('\0' != *pos)
851 pos++;
852 }
853 if (NULL != last)
854 argv[argv_size++] = GNUNET_strdup(last);
855 last = NULL;
856 GNUNET_free(cp);
857 } 853 }
858 while (NULL != (arg = (va_arg(ap, const char *)))); 854 if (NULL != last)
859 va_end(ap); 855 argv[argv_size++] = GNUNET_strdup (last);
856 last = NULL;
857 GNUNET_free (cp);
858 }
859 while (NULL != (arg = (va_arg (ap, const char *))));
860 va_end (ap);
860 argv[argv_size] = NULL; 861 argv[argv_size] = NULL;
861 862
862 for (i = 0; i < argv_size; i++) 863 for (i = 0; i < argv_size; i++)
864 {
865 len = strlen (argv[i]);
866 if ((argv[i][0] == '"') && (argv[i][len - 1] == '"'))
863 { 867 {
864 len = strlen(argv[i]); 868 memmove (&argv[i][0], &argv[i][1], len - 2);
865 if ((argv[i][0] == '"') && (argv[i][len - 1] == '"')) 869 argv[i][len - 2] = '\0';
866 {
867 memmove(&argv[i][0], &argv[i][1], len - 2);
868 argv[i][len - 2] = '\0';
869 }
870 } 870 }
871 }
871 binary_path = argv[0]; 872 binary_path = argv[0];
872 proc = GNUNET_OS_start_process_v(pipe_control, 873 proc = GNUNET_OS_start_process_v (pipe_control,
873 std_inheritance, 874 std_inheritance,
874 lsocks, 875 lsocks,
875 binary_path, 876 binary_path,
876 argv); 877 argv);
877 while (argv_size > 0) 878 while (argv_size > 0)
878 GNUNET_free(argv[--argv_size]); 879 GNUNET_free (argv[--argv_size]);
879 GNUNET_free(argv); 880 GNUNET_free (argv);
880 return proc; 881 return proc;
881} 882}
882 883
@@ -892,59 +893,59 @@ GNUNET_OS_start_process_s(int pipe_control,
892 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise 893 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
893 */ 894 */
894static int 895static int
895process_status(struct GNUNET_OS_Process *proc, 896process_status (struct GNUNET_OS_Process *proc,
896 enum GNUNET_OS_ProcessStatusType *type, 897 enum GNUNET_OS_ProcessStatusType *type,
897 unsigned long *code, 898 unsigned long *code,
898 int options) 899 int options)
899{ 900{
900 int status; 901 int status;
901 int ret; 902 int ret;
902 903
903 GNUNET_assert(0 != proc); 904 GNUNET_assert (0 != proc);
904 ret = waitpid(proc->pid, &status, options); 905 ret = waitpid (proc->pid, &status, options);
905 if (ret < 0) 906 if (ret < 0)
906 { 907 {
907 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "waitpid"); 908 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
908 return GNUNET_SYSERR; 909 return GNUNET_SYSERR;
909 } 910 }
910 if (0 == ret) 911 if (0 == ret)
911 { 912 {
912 *type = GNUNET_OS_PROCESS_RUNNING; 913 *type = GNUNET_OS_PROCESS_RUNNING;
913 *code = 0; 914 *code = 0;
914 return GNUNET_NO; 915 return GNUNET_NO;
915 } 916 }
916 if (proc->pid != ret) 917 if (proc->pid != ret)
917 { 918 {
918 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "waitpid"); 919 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
919 return GNUNET_SYSERR; 920 return GNUNET_SYSERR;
920 } 921 }
921 if (WIFEXITED(status)) 922 if (WIFEXITED (status))
922 { 923 {
923 *type = GNUNET_OS_PROCESS_EXITED; 924 *type = GNUNET_OS_PROCESS_EXITED;
924 *code = WEXITSTATUS(status); 925 *code = WEXITSTATUS (status);
925 } 926 }
926 else if (WIFSIGNALED(status)) 927 else if (WIFSIGNALED (status))
927 { 928 {
928 *type = GNUNET_OS_PROCESS_SIGNALED; 929 *type = GNUNET_OS_PROCESS_SIGNALED;
929 *code = WTERMSIG(status); 930 *code = WTERMSIG (status);
930 } 931 }
931 else if (WIFSTOPPED(status)) 932 else if (WIFSTOPPED (status))
932 { 933 {
933 *type = GNUNET_OS_PROCESS_SIGNALED; 934 *type = GNUNET_OS_PROCESS_SIGNALED;
934 *code = WSTOPSIG(status); 935 *code = WSTOPSIG (status);
935 } 936 }
936#ifdef WIFCONTINUED 937#ifdef WIFCONTINUED
937 else if (WIFCONTINUED(status)) 938 else if (WIFCONTINUED (status))
938 { 939 {
939 *type = GNUNET_OS_PROCESS_RUNNING; 940 *type = GNUNET_OS_PROCESS_RUNNING;
940 *code = 0; 941 *code = 0;
941 } 942 }
942#endif 943#endif
943 else 944 else
944 { 945 {
945 *type = GNUNET_OS_PROCESS_UNKNOWN; 946 *type = GNUNET_OS_PROCESS_UNKNOWN;
946 *code = 0; 947 *code = 0;
947 } 948 }
948 949
949 return GNUNET_OK; 950 return GNUNET_OK;
950} 951}
@@ -960,11 +961,11 @@ process_status(struct GNUNET_OS_Process *proc,
960 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise 961 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
961 */ 962 */
962int 963int
963GNUNET_OS_process_status(struct GNUNET_OS_Process *proc, 964GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
964 enum GNUNET_OS_ProcessStatusType *type, 965 enum GNUNET_OS_ProcessStatusType *type,
965 unsigned long *code) 966 unsigned long *code)
966{ 967{
967 return process_status(proc, type, code, WNOHANG); 968 return process_status (proc, type, code, WNOHANG);
968} 969}
969 970
970 971
@@ -978,11 +979,11 @@ GNUNET_OS_process_status(struct GNUNET_OS_Process *proc,
978 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise 979 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
979 */ 980 */
980int 981int
981GNUNET_OS_process_wait_status(struct GNUNET_OS_Process *proc, 982GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
982 enum GNUNET_OS_ProcessStatusType *type, 983 enum GNUNET_OS_ProcessStatusType *type,
983 unsigned long *code) 984 unsigned long *code)
984{ 985{
985 return process_status(proc, type, code, 0); 986 return process_status (proc, type, code, 0);
986} 987}
987 988
988 989
@@ -997,18 +998,18 @@ GNUNET_OS_process_wait_status(struct GNUNET_OS_Process *proc,
997 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 998 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
998 */ 999 */
999int 1000int
1000GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc) 1001GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1001{ 1002{
1002 pid_t pid = proc->pid; 1003 pid_t pid = proc->pid;
1003 pid_t ret; 1004 pid_t ret;
1004 1005
1005 while ((pid != (ret = waitpid(pid, NULL, 0))) && (EINTR == errno)) 1006 while ((pid != (ret = waitpid (pid, NULL, 0))) && (EINTR == errno))
1006 ; 1007 ;
1007 if (pid != ret) 1008 if (pid != ret)
1008 { 1009 {
1009 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "waitpid"); 1010 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1010 return GNUNET_SYSERR; 1011 return GNUNET_SYSERR;
1011 } 1012 }
1012 return GNUNET_OK; 1013 return GNUNET_OK;
1013} 1014}
1014 1015
@@ -1016,7 +1017,8 @@ GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
1016/** 1017/**
1017 * Handle to a command. 1018 * Handle to a command.
1018 */ 1019 */
1019struct GNUNET_OS_CommandHandle { 1020struct GNUNET_OS_CommandHandle
1021{
1020 /** 1022 /**
1021 * Process handle. 1023 * Process handle.
1022 */ 1024 */
@@ -1072,18 +1074,18 @@ struct GNUNET_OS_CommandHandle {
1072 * @param cmd handle to the process 1074 * @param cmd handle to the process
1073 */ 1075 */
1074void 1076void
1075GNUNET_OS_command_stop(struct GNUNET_OS_CommandHandle *cmd) 1077GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1076{ 1078{
1077 if (NULL != cmd->proc) 1079 if (NULL != cmd->proc)
1078 { 1080 {
1079 GNUNET_assert(NULL != cmd->rtask); 1081 GNUNET_assert (NULL != cmd->rtask);
1080 GNUNET_SCHEDULER_cancel(cmd->rtask); 1082 GNUNET_SCHEDULER_cancel (cmd->rtask);
1081 } 1083 }
1082 (void)GNUNET_OS_process_kill(cmd->eip, SIGKILL); 1084 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1083 GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(cmd->eip)); 1085 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1084 GNUNET_OS_process_destroy(cmd->eip); 1086 GNUNET_OS_process_destroy (cmd->eip);
1085 GNUNET_DISK_pipe_close(cmd->opipe); 1087 GNUNET_DISK_pipe_close (cmd->opipe);
1086 GNUNET_free(cmd); 1088 GNUNET_free (cmd);
1087} 1089}
1088 1090
1089 1091
@@ -1093,7 +1095,7 @@ GNUNET_OS_command_stop(struct GNUNET_OS_CommandHandle *cmd)
1093 * @param cls the `struct GNUNET_OS_CommandHandle *` 1095 * @param cls the `struct GNUNET_OS_CommandHandle *`
1094 */ 1096 */
1095static void 1097static void
1096cmd_read(void *cls) 1098cmd_read (void *cls)
1097{ 1099{
1098 struct GNUNET_OS_CommandHandle *cmd = cls; 1100 struct GNUNET_OS_CommandHandle *cmd = cls;
1099 const struct GNUNET_SCHEDULER_TaskContext *tc; 1101 const struct GNUNET_SCHEDULER_TaskContext *tc;
@@ -1102,46 +1104,46 @@ cmd_read(void *cls)
1102 ssize_t ret; 1104 ssize_t ret;
1103 1105
1104 cmd->rtask = NULL; 1106 cmd->rtask = NULL;
1105 tc = GNUNET_SCHEDULER_get_task_context(); 1107 tc = GNUNET_SCHEDULER_get_task_context ();
1106 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset(tc->read_ready, cmd->r)) 1108 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1107 { 1109 {
1108 /* timeout */ 1110 /* timeout */
1109 proc = cmd->proc; 1111 proc = cmd->proc;
1110 cmd->proc = NULL; 1112 cmd->proc = NULL;
1111 proc(cmd->proc_cls, NULL); 1113 proc (cmd->proc_cls, NULL);
1112 return; 1114 return;
1113 } 1115 }
1114 ret = GNUNET_DISK_file_read(cmd->r, 1116 ret = GNUNET_DISK_file_read (cmd->r,
1115 &cmd->buf[cmd->off], 1117 &cmd->buf[cmd->off],
1116 sizeof(cmd->buf) - cmd->off); 1118 sizeof(cmd->buf) - cmd->off);
1117 if (ret <= 0) 1119 if (ret <= 0)
1120 {
1121 if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf)))
1118 { 1122 {
1119 if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf))) 1123 cmd->buf[cmd->off] = '\0';
1120 { 1124 cmd->proc (cmd->proc_cls, cmd->buf);
1121 cmd->buf[cmd->off] = '\0';
1122 cmd->proc(cmd->proc_cls, cmd->buf);
1123 }
1124 proc = cmd->proc;
1125 cmd->proc = NULL;
1126 proc(cmd->proc_cls, NULL);
1127 return;
1128 } 1125 }
1129 end = memchr(&cmd->buf[cmd->off], '\n', ret); 1126 proc = cmd->proc;
1127 cmd->proc = NULL;
1128 proc (cmd->proc_cls, NULL);
1129 return;
1130 }
1131 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1130 cmd->off += ret; 1132 cmd->off += ret;
1131 while (NULL != end) 1133 while (NULL != end)
1132 { 1134 {
1133 *end = '\0'; 1135 *end = '\0';
1134 cmd->proc(cmd->proc_cls, cmd->buf); 1136 cmd->proc (cmd->proc_cls, cmd->buf);
1135 memmove(cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf)); 1137 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1136 cmd->off -= (end + 1 - cmd->buf); 1138 cmd->off -= (end + 1 - cmd->buf);
1137 end = memchr(cmd->buf, '\n', cmd->off); 1139 end = memchr (cmd->buf, '\n', cmd->off);
1138 } 1140 }
1139 cmd->rtask = 1141 cmd->rtask =
1140 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_absolute_get_remaining( 1142 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining (
1141 cmd->timeout), 1143 cmd->timeout),
1142 cmd->r, 1144 cmd->r,
1143 &cmd_read, 1145 &cmd_read,
1144 cmd); 1146 cmd);
1145} 1147}
1146 1148
1147 1149
@@ -1157,39 +1159,39 @@ cmd_read(void *cls)
1157 * @return NULL on error 1159 * @return NULL on error
1158 */ 1160 */
1159struct GNUNET_OS_CommandHandle * 1161struct GNUNET_OS_CommandHandle *
1160GNUNET_OS_command_run(GNUNET_OS_LineProcessor proc, 1162GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1161 void *proc_cls, 1163 void *proc_cls,
1162 struct GNUNET_TIME_Relative timeout, 1164 struct GNUNET_TIME_Relative timeout,
1163 const char *binary, 1165 const char *binary,
1164 ...) 1166 ...)
1165{ 1167{
1166 struct GNUNET_OS_CommandHandle *cmd; 1168 struct GNUNET_OS_CommandHandle *cmd;
1167 struct GNUNET_OS_Process *eip; 1169 struct GNUNET_OS_Process *eip;
1168 struct GNUNET_DISK_PipeHandle *opipe; 1170 struct GNUNET_DISK_PipeHandle *opipe;
1169 va_list ap; 1171 va_list ap;
1170 1172
1171 opipe = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 1173 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1172 if (NULL == opipe) 1174 if (NULL == opipe)
1173 return NULL; 1175 return NULL;
1174 va_start(ap, binary); 1176 va_start (ap, binary);
1175 /* redirect stdout, don't inherit stderr/stdin */ 1177 /* redirect stdout, don't inherit stderr/stdin */
1176 eip = 1178 eip =
1177 GNUNET_OS_start_process_va(GNUNET_NO, 0, NULL, opipe, NULL, binary, ap); 1179 GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL, opipe, NULL, binary, ap);
1178 va_end(ap); 1180 va_end (ap);
1179 if (NULL == eip) 1181 if (NULL == eip)
1180 { 1182 {
1181 GNUNET_DISK_pipe_close(opipe); 1183 GNUNET_DISK_pipe_close (opipe);
1182 return NULL; 1184 return NULL;
1183 } 1185 }
1184 GNUNET_DISK_pipe_close_end(opipe, GNUNET_DISK_PIPE_END_WRITE); 1186 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1185 cmd = GNUNET_new(struct GNUNET_OS_CommandHandle); 1187 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1186 cmd->timeout = GNUNET_TIME_relative_to_absolute(timeout); 1188 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1187 cmd->eip = eip; 1189 cmd->eip = eip;
1188 cmd->opipe = opipe; 1190 cmd->opipe = opipe;
1189 cmd->proc = proc; 1191 cmd->proc = proc;
1190 cmd->proc_cls = proc_cls; 1192 cmd->proc_cls = proc_cls;
1191 cmd->r = GNUNET_DISK_pipe_handle(opipe, GNUNET_DISK_PIPE_END_READ); 1193 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1192 cmd->rtask = GNUNET_SCHEDULER_add_read_file(timeout, cmd->r, &cmd_read, cmd); 1194 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1193 return cmd; 1195 return cmd;
1194} 1196}
1195 1197
diff --git a/src/util/perf_crypto_asymmetric.c b/src/util/perf_crypto_asymmetric.c
index 91679afa5..1f2182909 100644
--- a/src/util/perf_crypto_asymmetric.c
+++ b/src/util/perf_crypto_asymmetric.c
@@ -31,7 +31,8 @@ static struct GNUNET_TIME_Absolute start;
31 31
32#define l 500 32#define l 500
33 33
34struct TestSig { 34struct TestSig
35{
35 struct GNUNET_CRYPTO_EccSignaturePurpose purp; 36 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
36 struct GNUNET_HashCode h; 37 struct GNUNET_HashCode h;
37 struct GNUNET_CRYPTO_EddsaSignature sig; 38 struct GNUNET_CRYPTO_EddsaSignature sig;
@@ -39,26 +40,26 @@ struct TestSig {
39 40
40 41
41static void 42static void
42log_duration(const char *cryptosystem, 43log_duration (const char *cryptosystem,
43 const char *description) 44 const char *description)
44{ 45{
45 struct GNUNET_TIME_Relative t; 46 struct GNUNET_TIME_Relative t;
46 char s[64]; 47 char s[64];
47 48
48 sprintf(s, "%6s %15s", cryptosystem, description); 49 sprintf (s, "%6s %15s", cryptosystem, description);
49 t = GNUNET_TIME_absolute_get_duration(start); 50 t = GNUNET_TIME_absolute_get_duration (start);
50 t = GNUNET_TIME_relative_divide(t, l); 51 t = GNUNET_TIME_relative_divide (t, l);
51 fprintf(stdout, 52 fprintf (stdout,
52 "%s: %10s\n", 53 "%s: %10s\n",
53 s, 54 s,
54 GNUNET_STRINGS_relative_time_to_string(t, 55 GNUNET_STRINGS_relative_time_to_string (t,
55 GNUNET_NO)); 56 GNUNET_NO));
56 GAUGER("UTIL", s, t.rel_value_us, "us"); 57 GAUGER ("UTIL", s, t.rel_value_us, "us");
57} 58}
58 59
59 60
60int 61int
61main(int argc, char *argv[]) 62main (int argc, char *argv[])
62{ 63{
63 int i; 64 int i;
64 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe[l]; 65 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe[l];
@@ -67,62 +68,62 @@ main(int argc, char *argv[])
67 struct GNUNET_CRYPTO_EddsaPublicKey dspub[l]; 68 struct GNUNET_CRYPTO_EddsaPublicKey dspub[l];
68 struct TestSig sig[l]; 69 struct TestSig sig[l];
69 70
70 start = GNUNET_TIME_absolute_get(); 71 start = GNUNET_TIME_absolute_get ();
71 for (i = 0; i < l; i++)
72 {
73 sig[i].purp.purpose = 0;
74 sig[i].purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
75 + sizeof(struct GNUNET_HashCode));
76 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK,
77 &sig[i].h,
78 sizeof(sig[i].h));
79 }
80 log_duration("", "Init");
81
82 start = GNUNET_TIME_absolute_get();
83 for (i = 0; i < l; i++) 72 for (i = 0; i < l; i++)
84 eddsa[i] = GNUNET_CRYPTO_eddsa_key_create(); 73 {
85 log_duration("EdDSA", "create key"); 74 sig[i].purp.purpose = 0;
86 75 sig[i].purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
87 start = GNUNET_TIME_absolute_get(); 76 + sizeof(struct GNUNET_HashCode));
77 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
78 &sig[i].h,
79 sizeof(sig[i].h));
80 }
81 log_duration ("", "Init");
82
83 start = GNUNET_TIME_absolute_get ();
88 for (i = 0; i < l; i++) 84 for (i = 0; i < l; i++)
89 GNUNET_CRYPTO_eddsa_key_get_public(eddsa[i], &dspub[i]); 85 eddsa[i] = GNUNET_CRYPTO_eddsa_key_create ();
90 log_duration("EdDSA", "get public"); 86 log_duration ("EdDSA", "create key");
91 87
92 start = GNUNET_TIME_absolute_get(); 88 start = GNUNET_TIME_absolute_get ();
93 for (i = 0; i < l; i++) 89 for (i = 0; i < l; i++)
94 GNUNET_assert(GNUNET_OK == 90 GNUNET_CRYPTO_eddsa_key_get_public (eddsa[i], &dspub[i]);
95 GNUNET_CRYPTO_eddsa_sign(eddsa[i], 91 log_duration ("EdDSA", "get public");
96 &sig[i].purp,
97 &sig[i].sig));
98 log_duration("EdDSA", "sign HashCode");
99 92
100 start = GNUNET_TIME_absolute_get(); 93 start = GNUNET_TIME_absolute_get ();
101 for (i = 0; i < l; i++) 94 for (i = 0; i < l; i++)
102 GNUNET_assert(GNUNET_OK == 95 GNUNET_assert (GNUNET_OK ==
103 GNUNET_CRYPTO_eddsa_verify(0, 96 GNUNET_CRYPTO_eddsa_sign (eddsa[i],
104 &sig[i].purp, 97 &sig[i].purp,
105 &sig[i].sig, 98 &sig[i].sig));
106 &dspub[i])); 99 log_duration ("EdDSA", "sign HashCode");
107 log_duration("EdDSA", "verify HashCode");
108 100
109 start = GNUNET_TIME_absolute_get(); 101 start = GNUNET_TIME_absolute_get ();
102 for (i = 0; i < l; i++)
103 GNUNET_assert (GNUNET_OK ==
104 GNUNET_CRYPTO_eddsa_verify (0,
105 &sig[i].purp,
106 &sig[i].sig,
107 &dspub[i]));
108 log_duration ("EdDSA", "verify HashCode");
109
110 start = GNUNET_TIME_absolute_get ();
110 for (i = 0; i < l; i++) 111 for (i = 0; i < l; i++)
111 ecdhe[i] = GNUNET_CRYPTO_ecdhe_key_create(); 112 ecdhe[i] = GNUNET_CRYPTO_ecdhe_key_create ();
112 log_duration("ECDH", "create key"); 113 log_duration ("ECDH", "create key");
113 114
114 start = GNUNET_TIME_absolute_get(); 115 start = GNUNET_TIME_absolute_get ();
115 for (i = 0; i < l; i++) 116 for (i = 0; i < l; i++)
116 GNUNET_CRYPTO_ecdhe_key_get_public(ecdhe[i], &dhpub[i]); 117 GNUNET_CRYPTO_ecdhe_key_get_public (ecdhe[i], &dhpub[i]);
117 log_duration("ECDH", "get public"); 118 log_duration ("ECDH", "get public");
118 119
119 start = GNUNET_TIME_absolute_get(); 120 start = GNUNET_TIME_absolute_get ();
120 for (i = 0; i < l - 1; i += 2) 121 for (i = 0; i < l - 1; i += 2)
121 { 122 {
122 GNUNET_CRYPTO_ecc_ecdh(ecdhe[i], &dhpub[i + 1], &sig[i].h); 123 GNUNET_CRYPTO_ecc_ecdh (ecdhe[i], &dhpub[i + 1], &sig[i].h);
123 GNUNET_CRYPTO_ecc_ecdh(ecdhe[i + 1], &dhpub[i], &sig[i + 1].h); 124 GNUNET_CRYPTO_ecc_ecdh (ecdhe[i + 1], &dhpub[i], &sig[i + 1].h);
124 } 125 }
125 log_duration("ECDH", "do DH"); 126 log_duration ("ECDH", "do DH");
126 127
127 return 0; 128 return 0;
128} 129}
diff --git a/src/util/perf_crypto_ecc_dlog.c b/src/util/perf_crypto_ecc_dlog.c
index b09a6ed6f..106b07026 100644
--- a/src/util/perf_crypto_ecc_dlog.c
+++ b/src/util/perf_crypto_ecc_dlog.c
@@ -65,8 +65,8 @@
65 * @param do_dlog #GNUNET_YES if we want to actually do the bencharked operation 65 * @param do_dlog #GNUNET_YES if we want to actually do the bencharked operation
66 */ 66 */
67static void 67static void
68test_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc, 68test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
69 int do_dlog) 69 int do_dlog)
70{ 70{
71 gcry_mpi_t fact; 71 gcry_mpi_t fact;
72 gcry_mpi_t n; 72 gcry_mpi_t n;
@@ -77,96 +77,99 @@ test_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc,
77 int x; 77 int x;
78 int iret; 78 int iret;
79 79
80 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, NULL, CURVE)); 80 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
81 g = gcry_mpi_ec_get_point("g", ctx, 0); 81 g = gcry_mpi_ec_get_point ("g", ctx, 0);
82 GNUNET_assert(NULL != g); 82 GNUNET_assert (NULL != g);
83 n = gcry_mpi_ec_get_mpi("n", ctx, 0); 83 n = gcry_mpi_ec_get_mpi ("n", ctx, 0);
84 q = gcry_mpi_point_new(0); 84 q = gcry_mpi_point_new (0);
85 fact = gcry_mpi_new(0); 85 fact = gcry_mpi_new (0);
86 for (i = 0; i < TEST_ITER; i++) 86 for (i = 0; i < TEST_ITER; i++)
87 {
88 fprintf (stderr, ".");
89 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
90 MAX_FACT);
91 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
92 2))
87 { 93 {
88 fprintf(stderr, "."); 94 gcry_mpi_set_ui (fact, x);
89 x = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 95 gcry_mpi_sub (fact, n, fact);
90 MAX_FACT); 96 x = -x;
91 if (0 == GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
92 2))
93 {
94 gcry_mpi_set_ui(fact, x);
95 gcry_mpi_sub(fact, n, fact);
96 x = -x;
97 }
98 else
99 {
100 gcry_mpi_set_ui(fact, x);
101 }
102 gcry_mpi_ec_mul(q, fact, g, ctx);
103 if ((GNUNET_YES == do_dlog) &&
104 (x !=
105 (iret = GNUNET_CRYPTO_ecc_dlog(edc,
106 q))))
107 {
108 fprintf(stderr,
109 "DLOG failed for value %d (%d)\n",
110 x,
111 iret);
112 GNUNET_assert(0);
113 }
114 } 97 }
115 gcry_mpi_release(fact); 98 else
116 gcry_mpi_release(n); 99 {
117 gcry_mpi_point_release(g); 100 gcry_mpi_set_ui (fact, x);
118 gcry_mpi_point_release(q); 101 }
119 gcry_ctx_release(ctx); 102 gcry_mpi_ec_mul (q, fact, g, ctx);
120 fprintf(stderr, "\n"); 103 if ((GNUNET_YES == do_dlog) &&
104 (x !=
105 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
106 q))))
107 {
108 fprintf (stderr,
109 "DLOG failed for value %d (%d)\n",
110 x,
111 iret);
112 GNUNET_assert (0);
113 }
114 }
115 gcry_mpi_release (fact);
116 gcry_mpi_release (n);
117 gcry_mpi_point_release (g);
118 gcry_mpi_point_release (q);
119 gcry_ctx_release (ctx);
120 fprintf (stderr, "\n");
121} 121}
122 122
123 123
124int 124int
125main(int argc, char *argv[]) 125main (int argc, char *argv[])
126{ 126{
127 struct GNUNET_CRYPTO_EccDlogContext *edc; 127 struct GNUNET_CRYPTO_EccDlogContext *edc;
128 struct GNUNET_TIME_Absolute start; 128 struct GNUNET_TIME_Absolute start;
129 struct GNUNET_TIME_Relative delta; 129 struct GNUNET_TIME_Relative delta;
130 130
131 if (!gcry_check_version("1.6.0")) 131 if (! gcry_check_version ("1.6.0"))
132 { 132 {
133 fprintf(stderr, 133 fprintf (stderr,
134 _ 134 _
135 ("libgcrypt has not the expected version (version %s is required).\n"), 135 (
136 "1.6.0"); 136 "libgcrypt has not the expected version (version %s is required).\n"),
137 return 0; 137 "1.6.0");
138 } 138 return 0;
139 if (getenv("GNUNET_GCRYPT_DEBUG")) 139 }
140 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 140 if (getenv ("GNUNET_GCRYPT_DEBUG"))
141 GNUNET_log_setup("perf-crypto-ecc-dlog", 141 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
142 "WARNING", 142 GNUNET_log_setup ("perf-crypto-ecc-dlog",
143 NULL); 143 "WARNING",
144 start = GNUNET_TIME_absolute_get(); 144 NULL);
145 edc = GNUNET_CRYPTO_ecc_dlog_prepare(MAX_FACT, 145 start = GNUNET_TIME_absolute_get ();
146 MAX_MEM); 146 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
147 printf("DLOG precomputation 1M/1K took %s\n", 147 MAX_MEM);
148 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 148 printf ("DLOG precomputation 1M/1K took %s\n",
149 GNUNET_YES)); 149 GNUNET_STRINGS_relative_time_to_string (
150 GAUGER("UTIL", "ECC DLOG initialization", 150 GNUNET_TIME_absolute_get_duration (start),
151 GNUNET_TIME_absolute_get_duration 151 GNUNET_YES));
152 (start).rel_value_us / 1000LL, "ms/op"); 152 GAUGER ("UTIL", "ECC DLOG initialization",
153 start = GNUNET_TIME_absolute_get(); 153 GNUNET_TIME_absolute_get_duration
154 (start).rel_value_us / 1000LL, "ms/op");
155 start = GNUNET_TIME_absolute_get ();
154 /* first do a baseline run without the DLOG */ 156 /* first do a baseline run without the DLOG */
155 test_dlog(edc, GNUNET_NO); 157 test_dlog (edc, GNUNET_NO);
156 delta = GNUNET_TIME_absolute_get_duration(start); 158 delta = GNUNET_TIME_absolute_get_duration (start);
157 start = GNUNET_TIME_absolute_get(); 159 start = GNUNET_TIME_absolute_get ();
158 test_dlog(edc, GNUNET_YES); 160 test_dlog (edc, GNUNET_YES);
159 delta = GNUNET_TIME_relative_subtract(GNUNET_TIME_absolute_get_duration(start), 161 delta = GNUNET_TIME_relative_subtract (GNUNET_TIME_absolute_get_duration (
160 delta); 162 start),
161 printf("%u DLOG calculations took %s\n", 163 delta);
162 TEST_ITER, 164 printf ("%u DLOG calculations took %s\n",
163 GNUNET_STRINGS_relative_time_to_string(delta, 165 TEST_ITER,
164 GNUNET_YES)); 166 GNUNET_STRINGS_relative_time_to_string (delta,
165 GAUGER("UTIL", "ECC DLOG operations", 167 GNUNET_YES));
166 delta.rel_value_us / 1000LL / TEST_ITER, 168 GAUGER ("UTIL", "ECC DLOG operations",
167 "ms/op"); 169 delta.rel_value_us / 1000LL / TEST_ITER,
168 170 "ms/op");
169 GNUNET_CRYPTO_ecc_dlog_release(edc); 171
172 GNUNET_CRYPTO_ecc_dlog_release (edc);
170 return 0; 173 return 0;
171} 174}
172 175
diff --git a/src/util/perf_crypto_hash.c b/src/util/perf_crypto_hash.c
index cdcff2824..95eca8e7a 100644
--- a/src/util/perf_crypto_hash.c
+++ b/src/util/perf_crypto_hash.c
@@ -30,80 +30,83 @@
30 30
31 31
32static void 32static void
33perfHash() 33perfHash ()
34{ 34{
35 struct GNUNET_HashCode hc; 35 struct GNUNET_HashCode hc;
36 unsigned int i; 36 unsigned int i;
37 char buf[64 * 1024]; 37 char buf[64 * 1024];
38 38
39 memset(buf, 1, sizeof(buf)); 39 memset (buf, 1, sizeof(buf));
40 for (i = 0; i < 1024; i++) 40 for (i = 0; i < 1024; i++)
41 GNUNET_CRYPTO_hash(buf, sizeof(buf), &hc); 41 GNUNET_CRYPTO_hash (buf, sizeof(buf), &hc);
42} 42}
43 43
44 44
45static void 45static void
46perfHashSmall() 46perfHashSmall ()
47{ 47{
48 struct GNUNET_HashCode hc; 48 struct GNUNET_HashCode hc;
49 unsigned int i; 49 unsigned int i;
50 char buf[64]; 50 char buf[64];
51 51
52 memset(buf, 1, sizeof(buf)); 52 memset (buf, 1, sizeof(buf));
53 for (i = 0; i < 1024; i++) 53 for (i = 0; i < 1024; i++)
54 GNUNET_CRYPTO_hash(buf, sizeof(buf), &hc); 54 GNUNET_CRYPTO_hash (buf, sizeof(buf), &hc);
55} 55}
56 56
57 57
58static void 58static void
59perfHKDF() 59perfHKDF ()
60{ 60{
61 unsigned int i; 61 unsigned int i;
62 char res[128]; 62 char res[128];
63 char buf[128]; 63 char buf[128];
64 char skm[64]; 64 char skm[64];
65 65
66 memset(buf, 1, sizeof(buf)); 66 memset (buf, 1, sizeof(buf));
67 memset(skm, 2, sizeof(skm)); 67 memset (skm, 2, sizeof(skm));
68 for (i = 0; i < 1024; i++) 68 for (i = 0; i < 1024; i++)
69 GNUNET_CRYPTO_hkdf(res, sizeof(res), 69 GNUNET_CRYPTO_hkdf (res, sizeof(res),
70 GCRY_MD_SHA512, GCRY_MD_SHA256, 70 GCRY_MD_SHA512, GCRY_MD_SHA256,
71 buf, sizeof(buf), 71 buf, sizeof(buf),
72 skm, sizeof(skm), 72 skm, sizeof(skm),
73 "test", (size_t)4, 73 "test", (size_t) 4,
74 NULL, 0); 74 NULL, 0);
75} 75}
76 76
77 77
78int 78int
79main(int argc, char *argv[]) 79main (int argc, char *argv[])
80{ 80{
81 struct GNUNET_TIME_Absolute start; 81 struct GNUNET_TIME_Absolute start;
82 82
83 start = GNUNET_TIME_absolute_get(); 83 start = GNUNET_TIME_absolute_get ();
84 perfHashSmall(); 84 perfHashSmall ();
85 printf("1024x 64-byte Hash perf took %s\n", 85 printf ("1024x 64-byte Hash perf took %s\n",
86 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 86 GNUNET_STRINGS_relative_time_to_string (
87 GNUNET_YES)); 87 GNUNET_TIME_absolute_get_duration (start),
88 88 GNUNET_YES));
89 start = GNUNET_TIME_absolute_get(); 89
90 perfHash(); 90 start = GNUNET_TIME_absolute_get ();
91 printf("1024x 64k Hash perf took %s\n", 91 perfHash ();
92 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 92 printf ("1024x 64k Hash perf took %s\n",
93 GNUNET_YES)); 93 GNUNET_STRINGS_relative_time_to_string (
94 GAUGER("UTIL", "Cryptographic hashing", 94 GNUNET_TIME_absolute_get_duration (start),
95 64 * 1024 / (1 + 95 GNUNET_YES));
96 GNUNET_TIME_absolute_get_duration 96 GAUGER ("UTIL", "Cryptographic hashing",
97 (start).rel_value_us / 1000LL), "kb/ms"); 97 64 * 1024 / (1
98 start = GNUNET_TIME_absolute_get(); 98 + GNUNET_TIME_absolute_get_duration
99 perfHKDF(); 99 (start).rel_value_us / 1000LL), "kb/ms");
100 printf("HKDF perf took %s\n", 100 start = GNUNET_TIME_absolute_get ();
101 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 101 perfHKDF ();
102 GNUNET_YES)); 102 printf ("HKDF perf took %s\n",
103 GAUGER("UTIL", "Cryptographic HKDF", 103 GNUNET_STRINGS_relative_time_to_string (
104 64 * 1024 / (1 + 104 GNUNET_TIME_absolute_get_duration (start),
105 GNUNET_TIME_absolute_get_duration 105 GNUNET_YES));
106 (start).rel_value_us / 1000LL), "kb/ms"); 106 GAUGER ("UTIL", "Cryptographic HKDF",
107 64 * 1024 / (1
108 + GNUNET_TIME_absolute_get_duration
109 (start).rel_value_us / 1000LL), "kb/ms");
107 return 0; 110 return 0;
108} 111}
109 112
diff --git a/src/util/perf_crypto_paillier.c b/src/util/perf_crypto_paillier.c
index f2aa132d5..dd5622108 100644
--- a/src/util/perf_crypto_paillier.c
+++ b/src/util/perf_crypto_paillier.c
@@ -29,7 +29,7 @@
29 29
30 30
31int 31int
32main(int argc, char *argv[]) 32main (int argc, char *argv[])
33{ 33{
34 struct GNUNET_TIME_Absolute start; 34 struct GNUNET_TIME_Absolute start;
35 struct GNUNET_CRYPTO_PaillierPublicKey public_key; 35 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
@@ -38,51 +38,54 @@ main(int argc, char *argv[])
38 gcry_mpi_t m1; 38 gcry_mpi_t m1;
39 unsigned int i; 39 unsigned int i;
40 40
41 start = GNUNET_TIME_absolute_get(); 41 start = GNUNET_TIME_absolute_get ();
42 for (i = 0; i < 10; i++) 42 for (i = 0; i < 10; i++)
43 GNUNET_CRYPTO_paillier_create(&public_key, 43 GNUNET_CRYPTO_paillier_create (&public_key,
44 &private_key); 44 &private_key);
45 printf("10x key generation took %s\n", 45 printf ("10x key generation took %s\n",
46 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 46 GNUNET_STRINGS_relative_time_to_string (
47 GNUNET_YES)); 47 GNUNET_TIME_absolute_get_duration (start),
48 GAUGER("UTIL", "Paillier key generation", 48 GNUNET_YES));
49 64 * 1024 / (1 + 49 GAUGER ("UTIL", "Paillier key generation",
50 GNUNET_TIME_absolute_get_duration 50 64 * 1024 / (1
51 (start).rel_value_us / 1000LL), "keys/ms"); 51 + GNUNET_TIME_absolute_get_duration
52 (start).rel_value_us / 1000LL), "keys/ms");
52 53
53 m1 = gcry_mpi_new(0); 54 m1 = gcry_mpi_new (0);
54 m1 = gcry_mpi_set_ui(m1, 1); 55 m1 = gcry_mpi_set_ui (m1, 1);
55 /* m1 = m1 * 2 ^ (GCPB - 3) */ 56 /* m1 = m1 * 2 ^ (GCPB - 3) */
56 gcry_mpi_mul_2exp(m1, 57 gcry_mpi_mul_2exp (m1,
57 m1, 58 m1,
58 GNUNET_CRYPTO_PAILLIER_BITS - 3); 59 GNUNET_CRYPTO_PAILLIER_BITS - 3);
59 start = GNUNET_TIME_absolute_get(); 60 start = GNUNET_TIME_absolute_get ();
60 for (i = 0; i < 10; i++) 61 for (i = 0; i < 10; i++)
61 GNUNET_CRYPTO_paillier_encrypt(&public_key, 62 GNUNET_CRYPTO_paillier_encrypt (&public_key,
62 m1, 63 m1,
63 2, 64 2,
64 &c1); 65 &c1);
65 printf("10x encryption took %s\n", 66 printf ("10x encryption took %s\n",
66 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 67 GNUNET_STRINGS_relative_time_to_string (
67 GNUNET_YES)); 68 GNUNET_TIME_absolute_get_duration (start),
68 GAUGER("UTIL", "Paillier encryption", 69 GNUNET_YES));
69 64 * 1024 / (1 + 70 GAUGER ("UTIL", "Paillier encryption",
70 GNUNET_TIME_absolute_get_duration 71 64 * 1024 / (1
71 (start).rel_value_us / 1000LL), "ops/ms"); 72 + GNUNET_TIME_absolute_get_duration
73 (start).rel_value_us / 1000LL), "ops/ms");
72 74
73 start = GNUNET_TIME_absolute_get(); 75 start = GNUNET_TIME_absolute_get ();
74 for (i = 0; i < 10; i++) 76 for (i = 0; i < 10; i++)
75 GNUNET_CRYPTO_paillier_decrypt(&private_key, 77 GNUNET_CRYPTO_paillier_decrypt (&private_key,
76 &public_key, 78 &public_key,
77 &c1, 79 &c1,
78 m1); 80 m1);
79 printf("10x decryption took %s\n", 81 printf ("10x decryption took %s\n",
80 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 82 GNUNET_STRINGS_relative_time_to_string (
81 GNUNET_YES)); 83 GNUNET_TIME_absolute_get_duration (start),
82 GAUGER("UTIL", "Paillier decryption", 84 GNUNET_YES));
83 64 * 1024 / (1 + 85 GAUGER ("UTIL", "Paillier decryption",
84 GNUNET_TIME_absolute_get_duration 86 64 * 1024 / (1
85 (start).rel_value_us / 1000LL), "ops/ms"); 87 + GNUNET_TIME_absolute_get_duration
88 (start).rel_value_us / 1000LL), "ops/ms");
86 89
87 90
88 return 0; 91 return 0;
diff --git a/src/util/perf_crypto_rsa.c b/src/util/perf_crypto_rsa.c
index 33740666e..c65739f1f 100644
--- a/src/util/perf_crypto_rsa.c
+++ b/src/util/perf_crypto_rsa.c
@@ -34,7 +34,7 @@
34 * @param len keylength to evaluate with 34 * @param len keylength to evaluate with
35 */ 35 */
36static void 36static void
37eval(unsigned int len) 37eval (unsigned int len)
38{ 38{
39 struct GNUNET_TIME_Absolute start; 39 struct GNUNET_TIME_Absolute start;
40 struct GNUNET_CRYPTO_RsaSignature *sig; 40 struct GNUNET_CRYPTO_RsaSignature *sig;
@@ -48,29 +48,30 @@ eval(unsigned int len)
48 size_t bbuf_len; 48 size_t bbuf_len;
49 struct GNUNET_HashCode hc; 49 struct GNUNET_HashCode hc;
50 50
51 start = GNUNET_TIME_absolute_get(); 51 start = GNUNET_TIME_absolute_get ();
52 for (i = 0; i < 10; i++) 52 for (i = 0; i < 10; i++)
53 { 53 {
54 private_key = GNUNET_CRYPTO_rsa_private_key_create(len); 54 private_key = GNUNET_CRYPTO_rsa_private_key_create (len);
55 GNUNET_CRYPTO_rsa_private_key_free(private_key); 55 GNUNET_CRYPTO_rsa_private_key_free (private_key);
56 } 56 }
57 printf("10x %u-key generation took %s\n", 57 printf ("10x %u-key generation took %s\n",
58 len, 58 len,
59 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 59 GNUNET_STRINGS_relative_time_to_string (
60 GNUNET_YES)); 60 GNUNET_TIME_absolute_get_duration (start),
61 GNUNET_snprintf(sbuf, 61 GNUNET_YES));
62 sizeof(sbuf), 62 GNUNET_snprintf (sbuf,
63 "RSA %u-key generation", 63 sizeof(sbuf),
64 len); 64 "RSA %u-key generation",
65 GAUGER("UTIL", sbuf, 65 len);
66 64 * 1024 / (1 + 66 GAUGER ("UTIL", sbuf,
67 GNUNET_TIME_absolute_get_duration 67 64 * 1024 / (1
68 (start).rel_value_us / 1000LL), "keys/ms"); 68 + GNUNET_TIME_absolute_get_duration
69 private_key = GNUNET_CRYPTO_rsa_private_key_create(len); 69 (start).rel_value_us / 1000LL), "keys/ms");
70 public_key = GNUNET_CRYPTO_rsa_private_key_get_public(private_key); 70 private_key = GNUNET_CRYPTO_rsa_private_key_create (len);
71 public_key = GNUNET_CRYPTO_rsa_private_key_get_public (private_key);
71 for (i = 0; i < 10; i++) 72 for (i = 0; i < 10; i++)
72 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, 73 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
73 &bsec[i], sizeof(bsec[0])); 74 &bsec[i], sizeof(bsec[0]));
74 /* 75 /*
75 start = GNUNET_TIME_absolute_get (); 76 start = GNUNET_TIME_absolute_get ();
76 for (i=0;i<10;i++) 77 for (i=0;i<10;i++)
@@ -88,113 +89,117 @@ eval(unsigned int len)
88 GNUNET_TIME_absolute_get_duration 89 GNUNET_TIME_absolute_get_duration
89 (start).rel_value_us / 1000LL), "keys/ms"); 90 (start).rel_value_us / 1000LL), "keys/ms");
90 */ 91 */
91 start = GNUNET_TIME_absolute_get(); 92 start = GNUNET_TIME_absolute_get ();
92 GNUNET_CRYPTO_hash("test", 4, &hc); 93 GNUNET_CRYPTO_hash ("test", 4, &hc);
93 for (i = 0; i < 10; i++) 94 for (i = 0; i < 10; i++)
94 { 95 {
95 GNUNET_CRYPTO_rsa_blind(&hc, 96 GNUNET_CRYPTO_rsa_blind (&hc,
96 &bsec[i], 97 &bsec[i],
97 public_key, 98 public_key,
98 &bbuf, &bbuf_len); 99 &bbuf, &bbuf_len);
99 GNUNET_free(bbuf); 100 GNUNET_free (bbuf);
100 } 101 }
101 printf("10x %u-blinding took %s\n", 102 printf ("10x %u-blinding took %s\n",
102 len, 103 len,
103 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 104 GNUNET_STRINGS_relative_time_to_string (
104 GNUNET_YES)); 105 GNUNET_TIME_absolute_get_duration (start),
105 GNUNET_snprintf(sbuf, 106 GNUNET_YES));
106 sizeof(sbuf), 107 GNUNET_snprintf (sbuf,
107 "RSA %u-blinding", 108 sizeof(sbuf),
108 len); 109 "RSA %u-blinding",
109 GAUGER("UTIL", 110 len);
110 sbuf, 111 GAUGER ("UTIL",
111 64 * 1024 / (1 + 112 sbuf,
112 GNUNET_TIME_absolute_get_duration 113 64 * 1024 / (1
113 (start).rel_value_us / 1000LL), "ops/ms"); 114 + GNUNET_TIME_absolute_get_duration
114 GNUNET_CRYPTO_rsa_blind(&hc, 115 (start).rel_value_us / 1000LL), "ops/ms");
115 &bsec[0], 116 GNUNET_CRYPTO_rsa_blind (&hc,
116 public_key, 117 &bsec[0],
117 &bbuf, &bbuf_len); 118 public_key,
118 start = GNUNET_TIME_absolute_get(); 119 &bbuf, &bbuf_len);
120 start = GNUNET_TIME_absolute_get ();
119 for (i = 0; i < 10; i++) 121 for (i = 0; i < 10; i++)
120 { 122 {
121 sig = GNUNET_CRYPTO_rsa_sign_blinded(private_key, 123 sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key,
122 bbuf, bbuf_len); 124 bbuf, bbuf_len);
123 GNUNET_CRYPTO_rsa_signature_free(sig); 125 GNUNET_CRYPTO_rsa_signature_free (sig);
124 } 126 }
125 printf("10x %u-signing took %s\n", 127 printf ("10x %u-signing took %s\n",
126 len, 128 len,
127 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 129 GNUNET_STRINGS_relative_time_to_string (
128 GNUNET_YES)); 130 GNUNET_TIME_absolute_get_duration (start),
129 GNUNET_snprintf(sbuf, 131 GNUNET_YES));
130 sizeof(sbuf), 132 GNUNET_snprintf (sbuf,
131 "RSA %u-signing", 133 sizeof(sbuf),
132 len); 134 "RSA %u-signing",
133 GAUGER("UTIL", 135 len);
134 sbuf, 136 GAUGER ("UTIL",
135 64 * 1024 / (1 + 137 sbuf,
136 GNUNET_TIME_absolute_get_duration 138 64 * 1024 / (1
137 (start).rel_value_us / 1000LL), "ops/ms"); 139 + GNUNET_TIME_absolute_get_duration
138 sig = GNUNET_CRYPTO_rsa_sign_blinded(private_key, 140 (start).rel_value_us / 1000LL), "ops/ms");
139 bbuf, 141 sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key,
140 bbuf_len); 142 bbuf,
141 start = GNUNET_TIME_absolute_get(); 143 bbuf_len);
144 start = GNUNET_TIME_absolute_get ();
142 for (i = 0; i < 10; i++) 145 for (i = 0; i < 10; i++)
143 { 146 {
144 rsig = GNUNET_CRYPTO_rsa_unblind(sig, 147 rsig = GNUNET_CRYPTO_rsa_unblind (sig,
145 &bsec[0], 148 &bsec[0],
146 public_key); 149 public_key);
147 GNUNET_CRYPTO_rsa_signature_free(rsig); 150 GNUNET_CRYPTO_rsa_signature_free (rsig);
148 } 151 }
149 printf("10x %u-unblinding took %s\n", 152 printf ("10x %u-unblinding took %s\n",
150 len, 153 len,
151 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 154 GNUNET_STRINGS_relative_time_to_string (
152 GNUNET_YES)); 155 GNUNET_TIME_absolute_get_duration (start),
153 GNUNET_snprintf(sbuf, 156 GNUNET_YES));
154 sizeof(sbuf), 157 GNUNET_snprintf (sbuf,
155 "RSA %u-unblinding", 158 sizeof(sbuf),
156 len); 159 "RSA %u-unblinding",
157 GAUGER("UTIL", 160 len);
158 sbuf, 161 GAUGER ("UTIL",
159 64 * 1024 / (1 + 162 sbuf,
160 GNUNET_TIME_absolute_get_duration 163 64 * 1024 / (1
161 (start).rel_value_us / 1000LL), "ops/ms"); 164 + GNUNET_TIME_absolute_get_duration
162 rsig = GNUNET_CRYPTO_rsa_unblind(sig, 165 (start).rel_value_us / 1000LL), "ops/ms");
163 &bsec[0], 166 rsig = GNUNET_CRYPTO_rsa_unblind (sig,
164 public_key); 167 &bsec[0],
165 start = GNUNET_TIME_absolute_get(); 168 public_key);
169 start = GNUNET_TIME_absolute_get ();
166 for (i = 0; i < 10; i++) 170 for (i = 0; i < 10; i++)
167 { 171 {
168 GNUNET_assert(GNUNET_OK == 172 GNUNET_assert (GNUNET_OK ==
169 GNUNET_CRYPTO_rsa_verify(&hc, 173 GNUNET_CRYPTO_rsa_verify (&hc,
170 rsig, 174 rsig,
171 public_key)); 175 public_key));
172 } 176 }
173 printf("10x %u-verifying took %s\n", 177 printf ("10x %u-verifying took %s\n",
174 len, 178 len,
175 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 179 GNUNET_STRINGS_relative_time_to_string (
176 GNUNET_YES)); 180 GNUNET_TIME_absolute_get_duration (start),
177 GNUNET_snprintf(sbuf, 181 GNUNET_YES));
178 sizeof(sbuf), 182 GNUNET_snprintf (sbuf,
179 "RSA %u-verification", 183 sizeof(sbuf),
180 len); 184 "RSA %u-verification",
181 GAUGER("UTIL", 185 len);
182 sbuf, 186 GAUGER ("UTIL",
183 64 * 1024 / (1 + 187 sbuf,
184 GNUNET_TIME_absolute_get_duration 188 64 * 1024 / (1
185 (start).rel_value_us / 1000LL), "ops/ms"); 189 + GNUNET_TIME_absolute_get_duration
186 GNUNET_CRYPTO_rsa_signature_free(sig); 190 (start).rel_value_us / 1000LL), "ops/ms");
187 GNUNET_CRYPTO_rsa_public_key_free(public_key); 191 GNUNET_CRYPTO_rsa_signature_free (sig);
188 GNUNET_CRYPTO_rsa_private_key_free(private_key); 192 GNUNET_CRYPTO_rsa_public_key_free (public_key);
189 GNUNET_free(bbuf); 193 GNUNET_CRYPTO_rsa_private_key_free (private_key);
194 GNUNET_free (bbuf);
190} 195}
191 196
192 197
193int 198int
194main(int argc, char *argv[]) 199main (int argc, char *argv[])
195{ 200{
196 eval(1024); 201 eval (1024);
197 eval(2048); 202 eval (2048);
198 /* eval (4096); */ 203 /* eval (4096); */
199 return 0; 204 return 0;
200} 205}
diff --git a/src/util/perf_crypto_symmetric.c b/src/util/perf_crypto_symmetric.c
index 01bec8f11..c05700155 100644
--- a/src/util/perf_crypto_symmetric.c
+++ b/src/util/perf_crypto_symmetric.c
@@ -29,7 +29,7 @@
29 29
30 30
31static void 31static void
32perfEncrypt() 32perfEncrypt ()
33{ 33{
34 unsigned int i; 34 unsigned int i;
35 char buf[64 * 1024]; 35 char buf[64 * 1024];
@@ -37,38 +37,39 @@ perfEncrypt()
37 struct GNUNET_CRYPTO_SymmetricSessionKey sk; 37 struct GNUNET_CRYPTO_SymmetricSessionKey sk;
38 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 38 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
39 39
40 GNUNET_CRYPTO_symmetric_create_session_key(&sk); 40 GNUNET_CRYPTO_symmetric_create_session_key (&sk);
41 41
42 memset(buf, 1, sizeof(buf)); 42 memset (buf, 1, sizeof(buf));
43 for (i = 0; i < 1024; i++) 43 for (i = 0; i < 1024; i++)
44 { 44 {
45 memset(&iv, (int8_t)i, sizeof(iv)); 45 memset (&iv, (int8_t) i, sizeof(iv));
46 GNUNET_CRYPTO_symmetric_encrypt(buf, sizeof(buf), 46 GNUNET_CRYPTO_symmetric_encrypt (buf, sizeof(buf),
47 &sk, &iv, 47 &sk, &iv,
48 rbuf); 48 rbuf);
49 GNUNET_CRYPTO_symmetric_decrypt(rbuf, sizeof(buf), 49 GNUNET_CRYPTO_symmetric_decrypt (rbuf, sizeof(buf),
50 &sk, &iv, 50 &sk, &iv,
51 buf); 51 buf);
52 } 52 }
53 memset(rbuf, 1, sizeof(rbuf)); 53 memset (rbuf, 1, sizeof(rbuf));
54 GNUNET_assert(0 == memcmp(rbuf, buf, sizeof(buf))); 54 GNUNET_assert (0 == memcmp (rbuf, buf, sizeof(buf)));
55} 55}
56 56
57 57
58int 58int
59main(int argc, char *argv[]) 59main (int argc, char *argv[])
60{ 60{
61 struct GNUNET_TIME_Absolute start; 61 struct GNUNET_TIME_Absolute start;
62 62
63 start = GNUNET_TIME_absolute_get(); 63 start = GNUNET_TIME_absolute_get ();
64 perfEncrypt(); 64 perfEncrypt ();
65 printf("Encrypt perf took %s\n", 65 printf ("Encrypt perf took %s\n",
66 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 66 GNUNET_STRINGS_relative_time_to_string (
67 GNUNET_YES)); 67 GNUNET_TIME_absolute_get_duration (start),
68 GAUGER("UTIL", "Symmetric encryption", 68 GNUNET_YES));
69 64 * 1024 / (1 + 69 GAUGER ("UTIL", "Symmetric encryption",
70 GNUNET_TIME_absolute_get_duration 70 64 * 1024 / (1
71 (start).rel_value_us / 1000LL), "kb/ms"); 71 + GNUNET_TIME_absolute_get_duration
72 (start).rel_value_us / 1000LL), "kb/ms");
72 return 0; 73 return 0;
73} 74}
74 75
diff --git a/src/util/perf_malloc.c b/src/util/perf_malloc.c
index ee2281edd..3682777bd 100644
--- a/src/util/perf_malloc.c
+++ b/src/util/perf_malloc.c
@@ -28,36 +28,37 @@
28#include <gauger.h> 28#include <gauger.h>
29 29
30static uint64_t 30static uint64_t
31perfMalloc() 31perfMalloc ()
32{ 32{
33 size_t i; 33 size_t i;
34 uint64_t ret; 34 uint64_t ret;
35 35
36 ret = 0; 36 ret = 0;
37 for (i = 1; i < 1024 * 1024; i += 1024) 37 for (i = 1; i < 1024 * 1024; i += 1024)
38 { 38 {
39 ret += i; 39 ret += i;
40 GNUNET_free(GNUNET_malloc(i)); 40 GNUNET_free (GNUNET_malloc (i));
41 } 41 }
42 return ret; 42 return ret;
43} 43}
44 44
45 45
46int 46int
47main(int argc, char *argv[]) 47main (int argc, char *argv[])
48{ 48{
49 struct GNUNET_TIME_Absolute start; 49 struct GNUNET_TIME_Absolute start;
50 uint64_t kb; 50 uint64_t kb;
51 51
52 start = GNUNET_TIME_absolute_get(); 52 start = GNUNET_TIME_absolute_get ();
53 kb = perfMalloc(); 53 kb = perfMalloc ();
54 printf("Malloc perf took %s\n", 54 printf ("Malloc perf took %s\n",
55 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 55 GNUNET_STRINGS_relative_time_to_string (
56 GNUNET_YES)); 56 GNUNET_TIME_absolute_get_duration (start),
57 GAUGER("UTIL", "Allocation", 57 GNUNET_YES));
58 kb / 1024 / (1 + 58 GAUGER ("UTIL", "Allocation",
59 GNUNET_TIME_absolute_get_duration 59 kb / 1024 / (1
60 (start).rel_value_us / 1000LL), "kb/ms"); 60 + GNUNET_TIME_absolute_get_duration
61 (start).rel_value_us / 1000LL), "kb/ms");
61 return 0; 62 return 0;
62} 63}
63 64
diff --git a/src/util/plugin.c b/src/util/plugin.c
index 2b11e5c3c..bb310ada8 100644
--- a/src/util/plugin.c
+++ b/src/util/plugin.c
@@ -28,12 +28,13 @@
28#include <ltdl.h> 28#include <ltdl.h>
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-plugin", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-plugin", __VA_ARGS__)
32 32
33/** 33/**
34 * Linked list of active plugins. 34 * Linked list of active plugins.
35 */ 35 */
36struct PluginList { 36struct PluginList
37{
37 /** 38 /**
38 * This is a linked list. 39 * This is a linked list.
39 */ 40 */
@@ -71,40 +72,40 @@ static struct PluginList *plugins;
71 * Setup libtool paths. 72 * Setup libtool paths.
72 */ 73 */
73static void 74static void
74plugin_init() 75plugin_init ()
75{ 76{
76 int err; 77 int err;
77 const char *opath; 78 const char *opath;
78 char *path; 79 char *path;
79 char *cpath; 80 char *cpath;
80 81
81 err = lt_dlinit(); 82 err = lt_dlinit ();
82 if (err > 0) 83 if (err > 0)
83 { 84 {
84 fprintf(stderr, 85 fprintf (stderr,
85 _("Initialization of plugin mechanism failed: %s!\n"), 86 _ ("Initialization of plugin mechanism failed: %s!\n"),
86 lt_dlerror()); 87 lt_dlerror ());
87 return; 88 return;
88 } 89 }
89 opath = lt_dlgetsearchpath(); 90 opath = lt_dlgetsearchpath ();
90 if (NULL != opath) 91 if (NULL != opath)
91 old_dlsearchpath = GNUNET_strdup(opath); 92 old_dlsearchpath = GNUNET_strdup (opath);
92 path = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LIBDIR); 93 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
93 if (NULL != path) 94 if (NULL != path)
95 {
96 if (NULL != opath)
94 { 97 {
95 if (NULL != opath) 98 GNUNET_asprintf (&cpath, "%s:%s", opath, path);
96 { 99 lt_dlsetsearchpath (cpath);
97 GNUNET_asprintf(&cpath, "%s:%s", opath, path); 100 GNUNET_free (path);
98 lt_dlsetsearchpath(cpath); 101 GNUNET_free (cpath);
99 GNUNET_free(path);
100 GNUNET_free(cpath);
101 }
102 else
103 {
104 lt_dlsetsearchpath(path);
105 GNUNET_free(path);
106 }
107 } 102 }
103 else
104 {
105 lt_dlsetsearchpath (path);
106 GNUNET_free (path);
107 }
108 }
108} 109}
109 110
110 111
@@ -112,15 +113,15 @@ plugin_init()
112 * Shutdown libtool. 113 * Shutdown libtool.
113 */ 114 */
114static void 115static void
115plugin_fini() 116plugin_fini ()
116{ 117{
117 lt_dlsetsearchpath(old_dlsearchpath); 118 lt_dlsetsearchpath (old_dlsearchpath);
118 if (NULL != old_dlsearchpath) 119 if (NULL != old_dlsearchpath)
119 { 120 {
120 GNUNET_free(old_dlsearchpath); 121 GNUNET_free (old_dlsearchpath);
121 old_dlsearchpath = NULL; 122 old_dlsearchpath = NULL;
122 } 123 }
123 lt_dlexit(); 124 lt_dlexit ();
124} 125}
125 126
126 127
@@ -132,22 +133,22 @@ plugin_fini()
132 * @return NULL if the symbol was not found 133 * @return NULL if the symbol was not found
133 */ 134 */
134static GNUNET_PLUGIN_Callback 135static GNUNET_PLUGIN_Callback
135resolve_function(struct PluginList *plug, const char *name) 136resolve_function (struct PluginList *plug, const char *name)
136{ 137{
137 char *initName; 138 char *initName;
138 void *mptr; 139 void *mptr;
139 140
140 GNUNET_asprintf(&initName, "_%s_%s", plug->name, name); 141 GNUNET_asprintf (&initName, "_%s_%s", plug->name, name);
141 mptr = lt_dlsym(plug->handle, &initName[1]); 142 mptr = lt_dlsym (plug->handle, &initName[1]);
142 if (NULL == mptr) 143 if (NULL == mptr)
143 mptr = lt_dlsym(plug->handle, initName); 144 mptr = lt_dlsym (plug->handle, initName);
144 if (NULL == mptr) 145 if (NULL == mptr)
145 LOG(GNUNET_ERROR_TYPE_ERROR, 146 LOG (GNUNET_ERROR_TYPE_ERROR,
146 _("`%s' failed to resolve method '%s' with error: %s\n"), 147 _ ("`%s' failed to resolve method '%s' with error: %s\n"),
147 "lt_dlsym", 148 "lt_dlsym",
148 &initName[1], 149 &initName[1],
149 lt_dlerror()); 150 lt_dlerror ());
150 GNUNET_free(initName); 151 GNUNET_free (initName);
151 return mptr; 152 return mptr;
152} 153}
153 154
@@ -162,30 +163,30 @@ resolve_function(struct PluginList *plug, const char *name)
162 * @return #GNUNET_YES if the plugin exists, #GNUNET_NO if not 163 * @return #GNUNET_YES if the plugin exists, #GNUNET_NO if not
163 */ 164 */
164int 165int
165GNUNET_PLUGIN_test(const char *library_name) 166GNUNET_PLUGIN_test (const char *library_name)
166{ 167{
167 void *libhandle; 168 void *libhandle;
168 GNUNET_PLUGIN_Callback init; 169 GNUNET_PLUGIN_Callback init;
169 struct PluginList plug; 170 struct PluginList plug;
170 171
171 if (!initialized) 172 if (! initialized)
172 { 173 {
173 initialized = GNUNET_YES; 174 initialized = GNUNET_YES;
174 plugin_init(); 175 plugin_init ();
175 } 176 }
176 libhandle = lt_dlopenext(library_name); 177 libhandle = lt_dlopenext (library_name);
177 if (NULL == libhandle) 178 if (NULL == libhandle)
178 return GNUNET_NO; 179 return GNUNET_NO;
179 plug.handle = libhandle; 180 plug.handle = libhandle;
180 plug.name = (char *)library_name; 181 plug.name = (char *) library_name;
181 init = resolve_function(&plug, "init"); 182 init = resolve_function (&plug, "init");
182 if (NULL == init) 183 if (NULL == init)
183 { 184 {
184 GNUNET_break(0); 185 GNUNET_break (0);
185 lt_dlclose(libhandle); 186 lt_dlclose (libhandle);
186 return GNUNET_NO; 187 return GNUNET_NO;
187 } 188 }
188 lt_dlclose(libhandle); 189 lt_dlclose (libhandle);
189 return GNUNET_YES; 190 return GNUNET_YES;
190} 191}
191 192
@@ -203,42 +204,42 @@ GNUNET_PLUGIN_test(const char *library_name)
203 * @return whatever the initialization function returned 204 * @return whatever the initialization function returned
204 */ 205 */
205void * 206void *
206GNUNET_PLUGIN_load(const char *library_name, void *arg) 207GNUNET_PLUGIN_load (const char *library_name, void *arg)
207{ 208{
208 void *libhandle; 209 void *libhandle;
209 struct PluginList *plug; 210 struct PluginList *plug;
210 GNUNET_PLUGIN_Callback init; 211 GNUNET_PLUGIN_Callback init;
211 void *ret; 212 void *ret;
212 213
213 if (!initialized) 214 if (! initialized)
214 { 215 {
215 initialized = GNUNET_YES; 216 initialized = GNUNET_YES;
216 plugin_init(); 217 plugin_init ();
217 } 218 }
218 libhandle = lt_dlopenext(library_name); 219 libhandle = lt_dlopenext (library_name);
219 if (libhandle == NULL) 220 if (libhandle == NULL)
220 { 221 {
221 LOG(GNUNET_ERROR_TYPE_ERROR, 222 LOG (GNUNET_ERROR_TYPE_ERROR,
222 _("`%s' failed for library `%s' with error: %s\n"), 223 _ ("`%s' failed for library `%s' with error: %s\n"),
223 "lt_dlopenext", 224 "lt_dlopenext",
224 library_name, 225 library_name,
225 lt_dlerror()); 226 lt_dlerror ());
226 return NULL; 227 return NULL;
227 } 228 }
228 plug = GNUNET_new(struct PluginList); 229 plug = GNUNET_new (struct PluginList);
229 plug->handle = libhandle; 230 plug->handle = libhandle;
230 plug->name = GNUNET_strdup(library_name); 231 plug->name = GNUNET_strdup (library_name);
231 plug->next = plugins; 232 plug->next = plugins;
232 plugins = plug; 233 plugins = plug;
233 init = resolve_function(plug, "init"); 234 init = resolve_function (plug, "init");
234 if ((init == NULL) || (NULL == (ret = init(arg)))) 235 if ((init == NULL) || (NULL == (ret = init (arg))))
235 { 236 {
236 lt_dlclose(libhandle); 237 lt_dlclose (libhandle);
237 GNUNET_free(plug->name); 238 GNUNET_free (plug->name);
238 plugins = plug->next; 239 plugins = plug->next;
239 GNUNET_free(plug); 240 GNUNET_free (plug);
240 return NULL; 241 return NULL;
241 } 242 }
242 return ret; 243 return ret;
243} 244}
244 245
@@ -252,7 +253,7 @@ GNUNET_PLUGIN_load(const char *library_name, void *arg)
252 * @return whatever the shutdown function returned 253 * @return whatever the shutdown function returned
253 */ 254 */
254void * 255void *
255GNUNET_PLUGIN_unload(const char *library_name, void *arg) 256GNUNET_PLUGIN_unload (const char *library_name, void *arg)
256{ 257{
257 struct PluginList *pos; 258 struct PluginList *pos;
258 struct PluginList *prev; 259 struct PluginList *prev;
@@ -261,30 +262,30 @@ GNUNET_PLUGIN_unload(const char *library_name, void *arg)
261 262
262 prev = NULL; 263 prev = NULL;
263 pos = plugins; 264 pos = plugins;
264 while ((NULL != pos) && (0 != strcmp(pos->name, library_name))) 265 while ((NULL != pos) && (0 != strcmp (pos->name, library_name)))
265 { 266 {
266 prev = pos; 267 prev = pos;
267 pos = pos->next; 268 pos = pos->next;
268 } 269 }
269 if (NULL == pos) 270 if (NULL == pos)
270 return NULL; 271 return NULL;
271 272
272 done = resolve_function(pos, "done"); 273 done = resolve_function (pos, "done");
273 ret = NULL; 274 ret = NULL;
274 if (NULL != done) 275 if (NULL != done)
275 ret = done(arg); 276 ret = done (arg);
276 if (NULL == prev) 277 if (NULL == prev)
277 plugins = pos->next; 278 plugins = pos->next;
278 else 279 else
279 prev->next = pos->next; 280 prev->next = pos->next;
280 lt_dlclose(pos->handle); 281 lt_dlclose (pos->handle);
281 GNUNET_free(pos->name); 282 GNUNET_free (pos->name);
282 GNUNET_free(pos); 283 GNUNET_free (pos);
283 if (NULL == plugins) 284 if (NULL == plugins)
284 { 285 {
285 plugin_fini(); 286 plugin_fini ();
286 initialized = GNUNET_NO; 287 initialized = GNUNET_NO;
287 } 288 }
288 return ret; 289 return ret;
289} 290}
290 291
@@ -292,7 +293,8 @@ GNUNET_PLUGIN_unload(const char *library_name, void *arg)
292/** 293/**
293 * Closure for #find_libraries(). 294 * Closure for #find_libraries().
294 */ 295 */
295struct LoadAllContext { 296struct LoadAllContext
297{
296 /** 298 /**
297 * Prefix the plugin names we find have to match. 299 * Prefix the plugin names we find have to match.
298 */ 300 */
@@ -325,7 +327,7 @@ struct LoadAllContext {
325 * @return #GNUNET_OK (continue loading) 327 * @return #GNUNET_OK (continue loading)
326 */ 328 */
327static int 329static int
328find_libraries(void *cls, const char *filename) 330find_libraries (void *cls, const char *filename)
329{ 331{
330 struct LoadAllContext *lac = cls; 332 struct LoadAllContext *lac = cls;
331 const char *slashpos; 333 const char *slashpos;
@@ -336,20 +338,20 @@ find_libraries(void *cls, const char *filename)
336 size_t n; 338 size_t n;
337 339
338 libname = filename; 340 libname = filename;
339 while (NULL != (slashpos = strstr(libname, DIR_SEPARATOR_STR))) 341 while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR)))
340 libname = slashpos + 1; 342 libname = slashpos + 1;
341 n = strlen(libname); 343 n = strlen (libname);
342 if (0 != strncmp(lac->basename, libname, strlen(lac->basename))) 344 if (0 != strncmp (lac->basename, libname, strlen (lac->basename)))
343 return GNUNET_OK; /* wrong name */ 345 return GNUNET_OK; /* wrong name */
344 if ((n > 3) && (0 == strcmp(&libname[n - 3], ".la"))) 346 if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la")))
345 return GNUNET_OK; /* .la file */ 347 return GNUNET_OK; /* .la file */
346 basename = GNUNET_strdup(libname); 348 basename = GNUNET_strdup (libname);
347 if (NULL != (dot = strstr(basename, "."))) 349 if (NULL != (dot = strstr (basename, ".")))
348 *dot = '\0'; 350 *dot = '\0';
349 lib_ret = GNUNET_PLUGIN_load(basename, lac->arg); 351 lib_ret = GNUNET_PLUGIN_load (basename, lac->arg);
350 if (NULL != lib_ret) 352 if (NULL != lib_ret)
351 lac->cb(lac->cb_cls, basename, lib_ret); 353 lac->cb (lac->cb_cls, basename, lib_ret);
352 GNUNET_free(basename); 354 GNUNET_free (basename);
353 return GNUNET_OK; 355 return GNUNET_OK;
354} 356}
355 357
@@ -367,27 +369,27 @@ find_libraries(void *cls, const char *filename)
367 * @param cb_cls closure for @a cb 369 * @param cb_cls closure for @a cb
368 */ 370 */
369void 371void
370GNUNET_PLUGIN_load_all(const char *basename, 372GNUNET_PLUGIN_load_all (const char *basename,
371 void *arg, 373 void *arg,
372 GNUNET_PLUGIN_LoaderCallback cb, 374 GNUNET_PLUGIN_LoaderCallback cb,
373 void *cb_cls) 375 void *cb_cls)
374{ 376{
375 struct LoadAllContext lac; 377 struct LoadAllContext lac;
376 char *path; 378 char *path;
377 379
378 path = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LIBDIR); 380 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
379 if (NULL == path) 381 if (NULL == path)
380 { 382 {
381 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 _("Could not determine plugin installation path.\n")); 384 _ ("Could not determine plugin installation path.\n"));
383 return; 385 return;
384 } 386 }
385 lac.basename = basename; 387 lac.basename = basename;
386 lac.arg = arg; 388 lac.arg = arg;
387 lac.cb = cb; 389 lac.cb = cb;
388 lac.cb_cls = cb_cls; 390 lac.cb_cls = cb_cls;
389 GNUNET_DISK_directory_scan(path, &find_libraries, &lac); 391 GNUNET_DISK_directory_scan (path, &find_libraries, &lac);
390 GNUNET_free(path); 392 GNUNET_free (path);
391} 393}
392 394
393 395
diff --git a/src/util/proc_compat.c b/src/util/proc_compat.c
index 3d73d7889..6e852cb35 100644
--- a/src/util/proc_compat.c
+++ b/src/util/proc_compat.c
@@ -35,17 +35,15 @@
35 * @param n search character limit 35 * @param n search character limit
36 */ 36 */
37void* 37void*
38GN_memrchr_(const void *s, 38GN_memrchr_ (const void *s,
39 int c, 39 int c,
40 size_t n) 40 size_t n)
41{ 41{
42 const unsigned char *ucs = s; 42 const unsigned char *ucs = s;
43 ssize_t i; 43 ssize_t i;
44 44
45 for (i = n - 1; i >= 0; i--) 45 for (i = n - 1; i >= 0; i--)
46 if (c == (int)ucs[i]) 46 if (c == (int) ucs[i])
47 return (void *)&ucs[i]; 47 return (void *) &ucs[i];
48 return NULL; 48 return NULL;
49} 49}
50
51
diff --git a/src/util/program.c b/src/util/program.c
index 9bd3d64f9..630ec6205 100644
--- a/src/util/program.c
+++ b/src/util/program.c
@@ -31,15 +31,16 @@
31#include "speedup.h" 31#include "speedup.h"
32#include <gcrypt.h> 32#include <gcrypt.h>
33 33
34#define LOG(kind, ...) GNUNET_log_from(kind, "util-program", __VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from (kind, "util-program", __VA_ARGS__)
35 35
36#define LOG_STRERROR_FILE(kind, syscall, filename) \ 36#define LOG_STRERROR_FILE(kind, syscall, filename) \
37 GNUNET_log_from_strerror_file(kind, "util-program", syscall, filename) 37 GNUNET_log_from_strerror_file (kind, "util-program", syscall, filename)
38 38
39/** 39/**
40 * Context for the command. 40 * Context for the command.
41 */ 41 */
42struct CommandContext { 42struct CommandContext
43{
43 /** 44 /**
44 * Argv argument. 45 * Argv argument.
45 */ 46 */
@@ -71,10 +72,10 @@ struct CommandContext {
71 * task run when the scheduler shuts down 72 * task run when the scheduler shuts down
72 */ 73 */
73static void 74static void
74shutdown_task(void *cls) 75shutdown_task (void *cls)
75{ 76{
76 (void)cls; 77 (void) cls;
77 GNUNET_SPEEDUP_stop_(); 78 GNUNET_SPEEDUP_stop_ ();
78} 79}
79 80
80 81
@@ -83,14 +84,14 @@ shutdown_task(void *cls)
83 * program. Runs the program-specific main task. 84 * program. Runs the program-specific main task.
84 */ 85 */
85static void 86static void
86program_main(void *cls) 87program_main (void *cls)
87{ 88{
88 struct CommandContext *cc = cls; 89 struct CommandContext *cc = cls;
89 90
90 GNUNET_SPEEDUP_start_(cc->cfg); 91 GNUNET_SPEEDUP_start_ (cc->cfg);
91 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL); 92 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
92 GNUNET_RESOLVER_connect(cc->cfg); 93 GNUNET_RESOLVER_connect (cc->cfg);
93 cc->task(cc->task_cls, cc->args, cc->cfgfile, cc->cfg); 94 cc->task (cc->task_cls, cc->args, cc->cfgfile, cc->cfg);
94} 95}
95 96
96 97
@@ -102,16 +103,16 @@ program_main(void *cls)
102 * @param a2 second command line option 103 * @param a2 second command line option
103 */ 104 */
104static int 105static int
105cmd_sorter(const void *a1, const void *a2) 106cmd_sorter (const void *a1, const void *a2)
106{ 107{
107 const struct GNUNET_GETOPT_CommandLineOption *c1 = a1; 108 const struct GNUNET_GETOPT_CommandLineOption *c1 = a1;
108 const struct GNUNET_GETOPT_CommandLineOption *c2 = a2; 109 const struct GNUNET_GETOPT_CommandLineOption *c2 = a2;
109 110
110 if (toupper((unsigned char)c1->shortName) > 111 if (toupper ((unsigned char) c1->shortName) >
111 toupper((unsigned char)c2->shortName)) 112 toupper ((unsigned char) c2->shortName))
112 return 1; 113 return 1;
113 if (toupper((unsigned char)c1->shortName) < 114 if (toupper ((unsigned char) c1->shortName) <
114 toupper((unsigned char)c2->shortName)) 115 toupper ((unsigned char) c2->shortName))
115 return -1; 116 return -1;
116 if (c1->shortName > c2->shortName) 117 if (c1->shortName > c2->shortName)
117 return 1; 118 return 1;
@@ -137,14 +138,14 @@ cmd_sorter(const void *a1, const void *a2)
137 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 138 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
138 */ 139 */
139int 140int
140GNUNET_PROGRAM_run2(int argc, 141GNUNET_PROGRAM_run2 (int argc,
141 char *const *argv, 142 char *const *argv,
142 const char *binaryName, 143 const char *binaryName,
143 const char *binaryHelp, 144 const char *binaryHelp,
144 const struct GNUNET_GETOPT_CommandLineOption *options, 145 const struct GNUNET_GETOPT_CommandLineOption *options,
145 GNUNET_PROGRAM_Main task, 146 GNUNET_PROGRAM_Main task,
146 void *task_cls, 147 void *task_cls,
147 int run_without_scheduler) 148 int run_without_scheduler)
148{ 149{
149 struct CommandContext cc; 150 struct CommandContext cc;
150 151
@@ -161,191 +162,191 @@ GNUNET_PROGRAM_run2(int argc,
161 unsigned long long skew_variance; 162 unsigned long long skew_variance;
162 long long clock_offset; 163 long long clock_offset;
163 struct GNUNET_CONFIGURATION_Handle *cfg; 164 struct GNUNET_CONFIGURATION_Handle *cfg;
164 const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get(); 165 const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get ();
165 struct GNUNET_GETOPT_CommandLineOption defoptions[] = 166 struct GNUNET_GETOPT_CommandLineOption defoptions[] =
166 { GNUNET_GETOPT_option_cfgfile(&cc.cfgfile), 167 { GNUNET_GETOPT_option_cfgfile (&cc.cfgfile),
167 GNUNET_GETOPT_option_help(binaryHelp), 168 GNUNET_GETOPT_option_help (binaryHelp),
168 GNUNET_GETOPT_option_loglevel(&loglev), 169 GNUNET_GETOPT_option_loglevel (&loglev),
169 GNUNET_GETOPT_option_logfile(&logfile), 170 GNUNET_GETOPT_option_logfile (&logfile),
170 GNUNET_GETOPT_option_version(pd->version) }; 171 GNUNET_GETOPT_option_version (pd->version) };
171 struct GNUNET_GETOPT_CommandLineOption *allopts; 172 struct GNUNET_GETOPT_CommandLineOption *allopts;
172 const char *gargs; 173 const char *gargs;
173 char *lpfx; 174 char *lpfx;
174 char *spc; 175 char *spc;
175 176
176 logfile = NULL; 177 logfile = NULL;
177 gargs = getenv("GNUNET_ARGS"); 178 gargs = getenv ("GNUNET_ARGS");
178 if (NULL != gargs) 179 if (NULL != gargs)
179 { 180 {
180 char **gargv; 181 char **gargv;
181 unsigned int gargc; 182 unsigned int gargc;
182 char *cargs; 183 char *cargs;
183 184
184 gargv = NULL; 185 gargv = NULL;
185 gargc = 0; 186 gargc = 0;
186 for (int i = 0; i < argc; i++) 187 for (int i = 0; i < argc; i++)
187 GNUNET_array_append(gargv, gargc, GNUNET_strdup(argv[i])); 188 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
188 cargs = GNUNET_strdup(gargs); 189 cargs = GNUNET_strdup (gargs);
189 for (char *tok = strtok(cargs, " "); NULL != tok; tok = strtok(NULL, " ")) 190 for (char *tok = strtok (cargs, " "); NULL != tok; tok = strtok (NULL, " "))
190 GNUNET_array_append(gargv, gargc, GNUNET_strdup(tok)); 191 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
191 GNUNET_free(cargs); 192 GNUNET_free (cargs);
192 GNUNET_array_append(gargv, gargc, NULL); 193 GNUNET_array_append (gargv, gargc, NULL);
193 argv = (char *const *)gargv; 194 argv = (char *const *) gargv;
194 argc = gargc - 1; 195 argc = gargc - 1;
195 } 196 }
196 memset(&cc, 0, sizeof(cc)); 197 memset (&cc, 0, sizeof(cc));
197 loglev = NULL; 198 loglev = NULL;
198 cc.task = task; 199 cc.task = task;
199 cc.task_cls = task_cls; 200 cc.task_cls = task_cls;
200 cc.cfg = cfg = GNUNET_CONFIGURATION_create(); 201 cc.cfg = cfg = GNUNET_CONFIGURATION_create ();
201 /* prepare */ 202 /* prepare */
202#if ENABLE_NLS 203#if ENABLE_NLS
203 if (NULL != pd->gettext_domain) 204 if (NULL != pd->gettext_domain)
205 {
206 setlocale (LC_ALL, "");
207 path = (NULL == pd->gettext_path)
208 ? GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR)
209 : GNUNET_strdup (pd->gettext_path);
210 if (NULL != path)
204 { 211 {
205 setlocale(LC_ALL, ""); 212 bindtextdomain (pd->gettext_domain, path);
206 path = (NULL == pd->gettext_path) 213 GNUNET_free (path);
207 ? GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LOCALEDIR)
208 : GNUNET_strdup(pd->gettext_path);
209 if (NULL != path)
210 {
211 bindtextdomain(pd->gettext_domain, path);
212 GNUNET_free(path);
213 }
214 textdomain(pd->gettext_domain);
215 } 214 }
215 textdomain (pd->gettext_domain);
216 }
216#endif 217#endif
217 cnt = 0; 218 cnt = 0;
218 while (NULL != options[cnt].name) 219 while (NULL != options[cnt].name)
219 cnt++; 220 cnt++;
220 allopts = 221 allopts =
221 GNUNET_malloc((cnt + 1) * sizeof(struct GNUNET_GETOPT_CommandLineOption) + 222 GNUNET_malloc ((cnt + 1) * sizeof(struct GNUNET_GETOPT_CommandLineOption)
222 sizeof(defoptions)); 223 + sizeof(defoptions));
223 GNUNET_memcpy(allopts, defoptions, sizeof(defoptions)); 224 GNUNET_memcpy (allopts, defoptions, sizeof(defoptions));
224 GNUNET_memcpy(&allopts[sizeof(defoptions) / 225 GNUNET_memcpy (&allopts[sizeof(defoptions)
225 sizeof(struct GNUNET_GETOPT_CommandLineOption)], 226 / sizeof(struct GNUNET_GETOPT_CommandLineOption)],
226 options, 227 options,
227 (cnt + 1) * sizeof(struct GNUNET_GETOPT_CommandLineOption)); 228 (cnt + 1) * sizeof(struct GNUNET_GETOPT_CommandLineOption));
228 cnt += sizeof(defoptions) / sizeof(struct GNUNET_GETOPT_CommandLineOption); 229 cnt += sizeof(defoptions) / sizeof(struct GNUNET_GETOPT_CommandLineOption);
229 qsort(allopts, 230 qsort (allopts,
230 cnt, 231 cnt,
231 sizeof(struct GNUNET_GETOPT_CommandLineOption), 232 sizeof(struct GNUNET_GETOPT_CommandLineOption),
232 &cmd_sorter); 233 &cmd_sorter);
233 loglev = NULL; 234 loglev = NULL;
234 xdg = getenv("XDG_CONFIG_HOME"); 235 xdg = getenv ("XDG_CONFIG_HOME");
235 if (NULL != xdg) 236 if (NULL != xdg)
236 GNUNET_asprintf(&cfg_fn, 237 GNUNET_asprintf (&cfg_fn,
237 "%s%s%s", 238 "%s%s%s",
238 xdg, 239 xdg,
239 DIR_SEPARATOR_STR, 240 DIR_SEPARATOR_STR,
240 pd->config_file); 241 pd->config_file);
241 else 242 else
242 cfg_fn = GNUNET_strdup(pd->user_config_file); 243 cfg_fn = GNUNET_strdup (pd->user_config_file);
243 lpfx = GNUNET_strdup(binaryName); 244 lpfx = GNUNET_strdup (binaryName);
244 if (NULL != (spc = strstr(lpfx, " "))) 245 if (NULL != (spc = strstr (lpfx, " ")))
245 *spc = '\0'; 246 *spc = '\0';
246 ret = GNUNET_GETOPT_run(binaryName, allopts, (unsigned int)argc, argv); 247 ret = GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv);
247 if ((GNUNET_OK > ret) || 248 if ((GNUNET_OK > ret) ||
248 (GNUNET_OK != GNUNET_log_setup(lpfx, loglev, logfile))) 249 (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
249 { 250 {
250 GNUNET_free(allopts); 251 GNUNET_free (allopts);
251 GNUNET_free(lpfx); 252 GNUNET_free (lpfx);
252 goto cleanup; 253 goto cleanup;
253 } 254 }
254 if (NULL != cc.cfgfile) 255 if (NULL != cc.cfgfile)
256 {
257 if ((GNUNET_YES != GNUNET_DISK_file_test (cc.cfgfile)) ||
258 (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cc.cfgfile)))
255 { 259 {
256 if ((GNUNET_YES != GNUNET_DISK_file_test(cc.cfgfile)) || 260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 (GNUNET_SYSERR == GNUNET_CONFIGURATION_load(cfg, cc.cfgfile))) 261 _ (
258 { 262 "Unreadable or malformed configuration file `%s', exit ...\n"),
259 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 263 cc.cfgfile);
260 _( 264 ret = GNUNET_SYSERR;
261 "Unreadable or malformed configuration file `%s', exit ...\n"), 265 GNUNET_free (allopts);
262 cc.cfgfile); 266 GNUNET_free (lpfx);
263 ret = GNUNET_SYSERR; 267 goto cleanup;
264 GNUNET_free(allopts);
265 GNUNET_free(lpfx);
266 goto cleanup;
267 }
268 } 268 }
269 }
269 else 270 else
271 {
272 if (GNUNET_YES == GNUNET_DISK_file_test (cfg_fn))
270 { 273 {
271 if (GNUNET_YES == GNUNET_DISK_file_test(cfg_fn)) 274 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cfg_fn))
272 { 275 {
273 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load(cfg, cfg_fn)) 276 GNUNET_log (
274 { 277 GNUNET_ERROR_TYPE_ERROR,
275 GNUNET_log( 278 _ (
276 GNUNET_ERROR_TYPE_ERROR, 279 "Unreadable or malformed default configuration file `%s', exit ...\n"),
277 _( 280 cfg_fn);
278 "Unreadable or malformed default configuration file `%s', exit ...\n"), 281 ret = GNUNET_SYSERR;
279 cfg_fn); 282 GNUNET_free (allopts);
280 ret = GNUNET_SYSERR; 283 GNUNET_free (lpfx);
281 GNUNET_free(allopts); 284 goto cleanup;
282 GNUNET_free(lpfx); 285 }
283 goto cleanup;
284 }
285 }
286 else
287 {
288 GNUNET_free(cfg_fn);
289 cfg_fn = NULL;
290 if (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg, NULL))
291 {
292 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
293 _("Unreadable or malformed configuration, exit ...\n"));
294 ret = GNUNET_SYSERR;
295 GNUNET_free(allopts);
296 GNUNET_free(lpfx);
297 goto cleanup;
298 }
299 }
300 } 286 }
301 GNUNET_free(allopts); 287 else
302 GNUNET_free(lpfx);
303 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cc.cfg,
304 "testing",
305 "skew_offset",
306 &skew_offset) &&
307 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cc.cfg,
308 "testing",
309 "skew_variance",
310 &skew_variance)))
311 { 288 {
312 clock_offset = skew_offset - skew_variance; 289 GNUNET_free (cfg_fn);
313 GNUNET_TIME_set_offset(clock_offset); 290 cfg_fn = NULL;
291 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, NULL))
292 {
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 _ ("Unreadable or malformed configuration, exit ...\n"));
295 ret = GNUNET_SYSERR;
296 GNUNET_free (allopts);
297 GNUNET_free (lpfx);
298 goto cleanup;
299 }
314 } 300 }
301 }
302 GNUNET_free (allopts);
303 GNUNET_free (lpfx);
304 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg,
305 "testing",
306 "skew_offset",
307 &skew_offset)) &&
308 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg,
309 "testing",
310 "skew_variance",
311 &skew_variance)))
312 {
313 clock_offset = skew_offset - skew_variance;
314 GNUNET_TIME_set_offset (clock_offset);
315 }
315 /* ARM needs to know which configuration file to use when starting 316 /* ARM needs to know which configuration file to use when starting
316 services. If we got a command-line option *and* if nothing is 317 services. If we got a command-line option *and* if nothing is
317 specified in the configuration, remember the command-line option 318 specified in the configuration, remember the command-line option
318 in "cfg". This is typically really only having an effect if we 319 in "cfg". This is typically really only having an effect if we
319 are running code in src/arm/, as obviously the rest of the code 320 are running code in src/arm/, as obviously the rest of the code
320 has little business with ARM-specific options. */ 321 has little business with ARM-specific options. */
321 if (GNUNET_YES != GNUNET_CONFIGURATION_have_value(cfg, "arm", "CONFIG")) 322 if (GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg, "arm", "CONFIG"))
322 { 323 {
323 if (NULL != cc.cfgfile) 324 if (NULL != cc.cfgfile)
324 GNUNET_CONFIGURATION_set_value_string(cfg, "arm", "CONFIG", cc.cfgfile); 325 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", cc.cfgfile);
325 else if (NULL != cfg_fn) 326 else if (NULL != cfg_fn)
326 GNUNET_CONFIGURATION_set_value_string(cfg, "arm", "CONFIG", cfg_fn); 327 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", cfg_fn);
327 } 328 }
328 329
329 /* run */ 330 /* run */
330 cc.args = &argv[ret]; 331 cc.args = &argv[ret];
331 if ((NULL == cc.cfgfile) && (NULL != cfg_fn)) 332 if ((NULL == cc.cfgfile) && (NULL != cfg_fn))
332 cc.cfgfile = GNUNET_strdup(cfg_fn); 333 cc.cfgfile = GNUNET_strdup (cfg_fn);
333 if (GNUNET_NO == run_without_scheduler) 334 if (GNUNET_NO == run_without_scheduler)
334 { 335 {
335 GNUNET_SCHEDULER_run(&program_main, &cc); 336 GNUNET_SCHEDULER_run (&program_main, &cc);
336 } 337 }
337 else 338 else
338 { 339 {
339 GNUNET_RESOLVER_connect(cc.cfg); 340 GNUNET_RESOLVER_connect (cc.cfg);
340 cc.task(cc.task_cls, cc.args, cc.cfgfile, cc.cfg); 341 cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg);
341 } 342 }
342 ret = GNUNET_OK; 343 ret = GNUNET_OK;
343cleanup: 344cleanup:
344 GNUNET_CONFIGURATION_destroy(cfg); 345 GNUNET_CONFIGURATION_destroy (cfg);
345 GNUNET_free_non_null(cc.cfgfile); 346 GNUNET_free_non_null (cc.cfgfile);
346 GNUNET_free_non_null(cfg_fn); 347 GNUNET_free_non_null (cfg_fn);
347 GNUNET_free_non_null(loglev); 348 GNUNET_free_non_null (loglev);
348 GNUNET_free_non_null(logfile); 349 GNUNET_free_non_null (logfile);
349 return ret; 350 return ret;
350} 351}
351 352
@@ -364,22 +365,22 @@ cleanup:
364 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 365 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
365 */ 366 */
366int 367int
367GNUNET_PROGRAM_run(int argc, 368GNUNET_PROGRAM_run (int argc,
368 char *const *argv, 369 char *const *argv,
369 const char *binaryName, 370 const char *binaryName,
370 const char *binaryHelp, 371 const char *binaryHelp,
371 const struct GNUNET_GETOPT_CommandLineOption *options, 372 const struct GNUNET_GETOPT_CommandLineOption *options,
372 GNUNET_PROGRAM_Main task, 373 GNUNET_PROGRAM_Main task,
373 void *task_cls) 374 void *task_cls)
374{ 375{
375 return GNUNET_PROGRAM_run2(argc, 376 return GNUNET_PROGRAM_run2 (argc,
376 argv, 377 argv,
377 binaryName, 378 binaryName,
378 binaryHelp, 379 binaryHelp,
379 options, 380 options,
380 task, 381 task,
381 task_cls, 382 task_cls,
382 GNUNET_NO); 383 GNUNET_NO);
383} 384}
384 385
385 386
diff --git a/src/util/regex.c b/src/util/regex.c
index 24e83cfd1..d3ab35652 100644
--- a/src/util/regex.c
+++ b/src/util/regex.c
@@ -42,15 +42,15 @@
42 * bytes long. 42 * bytes long.
43 */ 43 */
44void 44void
45GNUNET_TUN_ipv4toregexsearch(const struct in_addr *ip, 45GNUNET_TUN_ipv4toregexsearch (const struct in_addr *ip,
46 uint16_t port, 46 uint16_t port,
47 char *rxstr) 47 char *rxstr)
48{ 48{
49 GNUNET_snprintf(rxstr, 49 GNUNET_snprintf (rxstr,
50 GNUNET_TUN_IPV4_REGEXLEN, 50 GNUNET_TUN_IPV4_REGEXLEN,
51 "4-%04X-%08X", 51 "4-%04X-%08X",
52 (unsigned int)port, 52 (unsigned int) port,
53 ntohl(ip->s_addr)); 53 ntohl (ip->s_addr));
54} 54}
55 55
56 56
@@ -63,21 +63,21 @@ GNUNET_TUN_ipv4toregexsearch(const struct in_addr *ip,
63 * bytes long. 63 * bytes long.
64 */ 64 */
65void 65void
66GNUNET_TUN_ipv6toregexsearch(const struct in6_addr *ipv6, 66GNUNET_TUN_ipv6toregexsearch (const struct in6_addr *ipv6,
67 uint16_t port, 67 uint16_t port,
68 char *rxstr) 68 char *rxstr)
69{ 69{
70 const uint32_t *addr; 70 const uint32_t *addr;
71 71
72 addr = (const uint32_t *)ipv6; 72 addr = (const uint32_t *) ipv6;
73 GNUNET_snprintf(rxstr, 73 GNUNET_snprintf (rxstr,
74 GNUNET_TUN_IPV6_REGEXLEN, 74 GNUNET_TUN_IPV6_REGEXLEN,
75 "6-%04X-%08X%08X%08X%08X", 75 "6-%04X-%08X%08X%08X%08X",
76 (unsigned int)port, 76 (unsigned int) port,
77 ntohl(addr[0]), 77 ntohl (addr[0]),
78 ntohl(addr[1]), 78 ntohl (addr[1]),
79 ntohl(addr[2]), 79 ntohl (addr[2]),
80 ntohl(addr[3])); 80 ntohl (addr[3]));
81} 81}
82 82
83 83
@@ -88,59 +88,59 @@ GNUNET_TUN_ipv6toregexsearch(const struct in6_addr *ipv6,
88 * @param mask which bits in value are wildcards (any value)? 88 * @param mask which bits in value are wildcards (any value)?
89 */ 89 */
90static char * 90static char *
91nibble_to_regex(uint8_t value, 91nibble_to_regex (uint8_t value,
92 uint8_t mask) 92 uint8_t mask)
93{ 93{
94 char *ret; 94 char *ret;
95 95
96 value &= mask; 96 value &= mask;
97 switch (mask) 97 switch (mask)
98 { 98 {
99 case 0: 99 case 0:
100 return GNUNET_strdup(DOT); 100 return GNUNET_strdup (DOT);
101 101
102 case 8: 102 case 8:
103 GNUNET_asprintf(&ret, 103 GNUNET_asprintf (&ret,
104 "(%X|%X|%X|%X|%X|%X|%X|%X)", 104 "(%X|%X|%X|%X|%X|%X|%X|%X)",
105 value, 105 value,
106 value + 1, 106 value + 1,
107 value + 2, 107 value + 2,
108 value + 3, 108 value + 3,
109 value + 4, 109 value + 4,
110 value + 5, 110 value + 5,
111 value + 6, 111 value + 6,
112 value + 7); 112 value + 7);
113 return ret; 113 return ret;
114 114
115 case 12: 115 case 12:
116 GNUNET_asprintf(&ret, 116 GNUNET_asprintf (&ret,
117 "(%X|%X|%X|%X)", 117 "(%X|%X|%X|%X)",
118 value, 118 value,
119 value + 1, 119 value + 1,
120 value + 2, 120 value + 2,
121 value + 3); 121 value + 3);
122 return ret; 122 return ret;
123 123
124 case 14: 124 case 14:
125 GNUNET_asprintf(&ret, 125 GNUNET_asprintf (&ret,
126 "(%X|%X)", 126 "(%X|%X)",
127 value, 127 value,
128 value + 1); 128 value + 1);
129 return ret; 129 return ret;
130 130
131 case 15: 131 case 15:
132 GNUNET_asprintf(&ret, 132 GNUNET_asprintf (&ret,
133 "%X", 133 "%X",
134 value); 134 value);
135 return ret; 135 return ret;
136 136
137 default: 137 default:
138 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 138 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
139 _("Bad mask: %d\n"), 139 _ ("Bad mask: %d\n"),
140 mask); 140 mask);
141 GNUNET_break(0); 141 GNUNET_break (0);
142 return NULL; 142 return NULL;
143 } 143 }
144} 144}
145 145
146 146
@@ -151,33 +151,33 @@ nibble_to_regex(uint8_t value,
151 * @param mask which bits in value are wildcards (any value)? 151 * @param mask which bits in value are wildcards (any value)?
152 */ 152 */
153static char * 153static char *
154num_to_regex(uint16_t value, 154num_to_regex (uint16_t value,
155 uint16_t mask) 155 uint16_t mask)
156{ 156{
157 const uint8_t *v = (const uint8_t *)&value; 157 const uint8_t *v = (const uint8_t *) &value;
158 const uint8_t *m = (const uint8_t *)&mask; 158 const uint8_t *m = (const uint8_t *) &mask;
159 char *a; 159 char *a;
160 char *b; 160 char *b;
161 char *c; 161 char *c;
162 char *d; 162 char *d;
163 char *ret; 163 char *ret;
164 164
165 a = nibble_to_regex(v[0] >> 4, m[0] >> 4); 165 a = nibble_to_regex (v[0] >> 4, m[0] >> 4);
166 b = nibble_to_regex(v[0] & 15, m[0] & 15); 166 b = nibble_to_regex (v[0] & 15, m[0] & 15);
167 c = nibble_to_regex(v[1] >> 4, m[1] >> 4); 167 c = nibble_to_regex (v[1] >> 4, m[1] >> 4);
168 d = nibble_to_regex(v[1] & 15, m[1] & 15); 168 d = nibble_to_regex (v[1] & 15, m[1] & 15);
169 ret = NULL; 169 ret = NULL;
170 if ((NULL != a) && 170 if ((NULL != a) &&
171 (NULL != b) && 171 (NULL != b) &&
172 (NULL != c) && 172 (NULL != c) &&
173 (NULL != d)) 173 (NULL != d))
174 GNUNET_asprintf(&ret, 174 GNUNET_asprintf (&ret,
175 "%s%s%s%s", 175 "%s%s%s%s",
176 a, b, c, d); 176 a, b, c, d);
177 GNUNET_free_non_null(a); 177 GNUNET_free_non_null (a);
178 GNUNET_free_non_null(b); 178 GNUNET_free_non_null (b);
179 GNUNET_free_non_null(c); 179 GNUNET_free_non_null (c);
180 GNUNET_free_non_null(d); 180 GNUNET_free_non_null (d);
181 return ret; 181 return ret;
182} 182}
183 183
@@ -190,36 +190,36 @@ num_to_regex(uint16_t value,
190 * #GNUNET_NO if not 190 * #GNUNET_NO if not
191 */ 191 */
192static int 192static int
193needs_parens(const char *arg) 193needs_parens (const char *arg)
194{ 194{
195 size_t off; 195 size_t off;
196 size_t len; 196 size_t len;
197 unsigned int op; 197 unsigned int op;
198 198
199 op = 0; 199 op = 0;
200 len = strlen(arg); 200 len = strlen (arg);
201 for (off = 0; off < len; off++) 201 for (off = 0; off < len; off++)
202 {
203 switch (arg[off])
202 { 204 {
203 switch (arg[off]) 205 case '(':
204 { 206 op++;
205 case '(': 207 break;
206 op++; 208
207 break; 209 case ')':
208 210 GNUNET_assert (op > 0);
209 case ')': 211 op--;
210 GNUNET_assert(op > 0); 212 break;
211 op--; 213
212 break; 214 case '|':
213 215 if (0 == op)
214 case '|': 216 return GNUNET_YES;
215 if (0 == op) 217 break;
216 return GNUNET_YES; 218
217 break; 219 default:
218 220 break;
219 default:
220 break;
221 }
222 } 221 }
222 }
223 return GNUNET_NO; 223 return GNUNET_NO;
224} 224}
225 225
@@ -235,10 +235,10 @@ needs_parens(const char *arg)
235 * @return corresponding regex 235 * @return corresponding regex
236 */ 236 */
237static char * 237static char *
238compute_policy(unsigned int start, 238compute_policy (unsigned int start,
239 unsigned int end, 239 unsigned int end,
240 unsigned int step, 240 unsigned int step,
241 const struct GNUNET_STRINGS_PortPolicy *pp) 241 const struct GNUNET_STRINGS_PortPolicy *pp)
242{ 242{
243 unsigned int i; 243 unsigned int i;
244 char before[36]; /* 16 * 2 + 3 dots + 0-terminator */ 244 char before[36]; /* 16 * 2 + 3 dots + 0-terminator */
@@ -249,7 +249,7 @@ compute_policy(unsigned int start,
249 char middlehp[33 + 2]; /* 16 * 2 + 0-terminator + () */ 249 char middlehp[33 + 2]; /* 16 * 2 + 0-terminator + () */
250 char middlelp[33 + 2]; /* 16 * 2 + 0-terminator + () */ 250 char middlelp[33 + 2]; /* 16 * 2 + 0-terminator + () */
251 char afterp[36 + 2]; /* 16 * 2 + 3 dots + 0-terminator + () */ 251 char afterp[36 + 2]; /* 16 * 2 + 3 dots + 0-terminator + () */
252 char dots[5 * strlen(DOT)]; 252 char dots[5 * strlen (DOT)];
253 char buf[3]; 253 char buf[3];
254 char *middle; 254 char *middle;
255 char *ret; 255 char *ret;
@@ -261,248 +261,248 @@ compute_policy(unsigned int start,
261 unsigned int start_port; 261 unsigned int start_port;
262 unsigned int end_port; 262 unsigned int end_port;
263 263
264 GNUNET_assert(GNUNET_YES == pp->negate_portrange); 264 GNUNET_assert (GNUNET_YES == pp->negate_portrange);
265 start_port = pp->start_port; 265 start_port = pp->start_port;
266 if (1 == start_port) 266 if (1 == start_port)
267 start_port = 0; 267 start_port = 0;
268 end_port = pp->end_port; 268 end_port = pp->end_port;
269 GNUNET_assert((end - start) / step <= 0xF); 269 GNUNET_assert ((end - start) / step <= 0xF);
270 before[0] = '\0'; 270 before[0] = '\0';
271 middlel[0] = '\0'; 271 middlel[0] = '\0';
272 middleh[0] = '\0'; 272 middleh[0] = '\0';
273 after[0] = '\0'; 273 after[0] = '\0';
274 for (i = start; i <= end; i += step) 274 for (i = start; i <= end; i += step)
275 { 275 {
276 GNUNET_snprintf(buf, 276 GNUNET_snprintf (buf,
277 sizeof(buf), 277 sizeof(buf),
278 "%X|", 278 "%X|",
279 (i - start) / step); 279 (i - start) / step);
280 if (i / step < start_port / step) 280 if (i / step < start_port / step)
281 strcat(before, buf); 281 strcat (before, buf);
282 else if (i / step > end_port / step) 282 else if (i / step > end_port / step)
283 strcat(after, buf); 283 strcat (after, buf);
284 else if (i / step == start_port / step) 284 else if (i / step == start_port / step)
285 strcat(middlel, buf); 285 strcat (middlel, buf);
286 else if (i / step == end_port / step) 286 else if (i / step == end_port / step)
287 strcat(middleh, buf); 287 strcat (middleh, buf);
288 } 288 }
289 if (strlen(before) > 0) 289 if (strlen (before) > 0)
290 before[strlen(before) - 1] = '\0'; 290 before[strlen (before) - 1] = '\0';
291 if (strlen(middlel) > 0) 291 if (strlen (middlel) > 0)
292 middlel[strlen(middlel) - 1] = '\0'; 292 middlel[strlen (middlel) - 1] = '\0';
293 if (strlen(middleh) > 0) 293 if (strlen (middleh) > 0)
294 middleh[strlen(middleh) - 1] = '\0'; 294 middleh[strlen (middleh) - 1] = '\0';
295 if (strlen(after) > 0) 295 if (strlen (after) > 0)
296 after[strlen(after) - 1] = '\0'; 296 after[strlen (after) - 1] = '\0';
297 if (needs_parens(before)) 297 if (needs_parens (before))
298 GNUNET_snprintf(beforep, 298 GNUNET_snprintf (beforep,
299 sizeof(beforep), 299 sizeof(beforep),
300 "(%s)", 300 "(%s)",
301 before); 301 before);
302 else 302 else
303 strcpy(beforep, before); 303 strcpy (beforep, before);
304 if (needs_parens(middlel)) 304 if (needs_parens (middlel))
305 GNUNET_snprintf(middlelp, 305 GNUNET_snprintf (middlelp,
306 sizeof(middlelp), 306 sizeof(middlelp),
307 "(%s)", 307 "(%s)",
308 middlel); 308 middlel);
309 else 309 else
310 strcpy(middlelp, middlel); 310 strcpy (middlelp, middlel);
311 if (needs_parens(middleh)) 311 if (needs_parens (middleh))
312 GNUNET_snprintf(middlehp, 312 GNUNET_snprintf (middlehp,
313 sizeof(middlehp), 313 sizeof(middlehp),
314 "(%s)", 314 "(%s)",
315 middleh); 315 middleh);
316 else 316 else
317 strcpy(middlehp, middleh); 317 strcpy (middlehp, middleh);
318 if (needs_parens(after)) 318 if (needs_parens (after))
319 GNUNET_snprintf(afterp, 319 GNUNET_snprintf (afterp,
320 sizeof(afterp), 320 sizeof(afterp),
321 "(%s)", 321 "(%s)",
322 after); 322 after);
323 else 323 else
324 strcpy(afterp, after); 324 strcpy (afterp, after);
325 dots[0] = '\0'; 325 dots[0] = '\0';
326 for (xstep = step / 16; xstep > 0; xstep /= 16) 326 for (xstep = step / 16; xstep > 0; xstep /= 16)
327 strcat(dots, DOT); 327 strcat (dots, DOT);
328 if (step >= 16) 328 if (step >= 16)
329 { 329 {
330 if (strlen(middlel) > 0) 330 if (strlen (middlel) > 0)
331 recl = compute_policy((start_port / step) * step, 331 recl = compute_policy ((start_port / step) * step,
332 (start_port / step) * step + step - 1, 332 (start_port / step) * step + step - 1,
333 step / 16, 333 step / 16,
334 pp); 334 pp);
335 else 335 else
336 recl = GNUNET_strdup(""); 336 recl = GNUNET_strdup ("");
337 if (strlen(middleh) > 0) 337 if (strlen (middleh) > 0)
338 rech = compute_policy((end_port / step) * step, 338 rech = compute_policy ((end_port / step) * step,
339 (end_port / step) * step + step - 1, 339 (end_port / step) * step + step - 1,
340 step / 16, 340 step / 16,
341 pp); 341 pp);
342 else 342 else
343 rech = GNUNET_strdup(""); 343 rech = GNUNET_strdup ("");
344 } 344 }
345 else 345 else
346 { 346 {
347 recl = GNUNET_strdup(""); 347 recl = GNUNET_strdup ("");
348 rech = GNUNET_strdup(""); 348 rech = GNUNET_strdup ("");
349 middlel[0] = '\0'; 349 middlel[0] = '\0';
350 middlelp[0] = '\0'; 350 middlelp[0] = '\0';
351 middleh[0] = '\0'; 351 middleh[0] = '\0';
352 middlehp[0] = '\0'; 352 middlehp[0] = '\0';
353 } 353 }
354 if (needs_parens(recl)) 354 if (needs_parens (recl))
355 GNUNET_asprintf(&reclp, 355 GNUNET_asprintf (&reclp,
356 "(%s)", 356 "(%s)",
357 recl); 357 recl);
358 else 358 else
359 reclp = GNUNET_strdup(recl); 359 reclp = GNUNET_strdup (recl);
360 if (needs_parens(rech)) 360 if (needs_parens (rech))
361 GNUNET_asprintf(&rechp, 361 GNUNET_asprintf (&rechp,
362 "(%s)", 362 "(%s)",
363 rech); 363 rech);
364 else 364 else
365 rechp = GNUNET_strdup(rech); 365 rechp = GNUNET_strdup (rech);
366 366
367 if ((strlen(middleh) > 0) && 367 if ((strlen (middleh) > 0) &&
368 (strlen(rech) > 0) && 368 (strlen (rech) > 0) &&
369 (strlen(middlel) > 0) && 369 (strlen (middlel) > 0) &&
370 (strlen(recl) > 0)) 370 (strlen (recl) > 0))
371 { 371 {
372 GNUNET_asprintf(&middle, 372 GNUNET_asprintf (&middle,
373 "%s%s|%s%s", 373 "%s%s|%s%s",
374 middlel, 374 middlel,
375 reclp, 375 reclp,
376 middleh, 376 middleh,
377 rechp); 377 rechp);
378 } 378 }
379 else if ((strlen(middleh) > 0) && 379 else if ((strlen (middleh) > 0) &&
380 (strlen(rech) > 0)) 380 (strlen (rech) > 0))
381 { 381 {
382 GNUNET_asprintf(&middle, 382 GNUNET_asprintf (&middle,
383 "%s%s", 383 "%s%s",
384 middleh, 384 middleh,
385 rechp); 385 rechp);
386 } 386 }
387 else if ((strlen(middlel) > 0) && 387 else if ((strlen (middlel) > 0) &&
388 (strlen(recl) > 0)) 388 (strlen (recl) > 0))
389 { 389 {
390 GNUNET_asprintf(&middle, 390 GNUNET_asprintf (&middle,
391 "%s%s", 391 "%s%s",
392 middlel, 392 middlel,
393 reclp); 393 reclp);
394 } 394 }
395 else 395 else
396 {
397 middle = GNUNET_strdup ("");
398 }
399 if ((strlen (before) > 0) &&
400 (strlen (after) > 0))
401 {
402 if (strlen (dots) > 0)
396 { 403 {
397 middle = GNUNET_strdup(""); 404 if (strlen (middle) > 0)
405 GNUNET_asprintf (&ret,
406 "(%s%s|%s|%s%s)",
407 beforep, dots,
408 middle,
409 afterp, dots);
410 else
411 GNUNET_asprintf (&ret,
412 "(%s|%s)%s",
413 beforep,
414 afterp,
415 dots);
398 } 416 }
399 if ((strlen(before) > 0) && 417 else
400 (strlen(after) > 0))
401 { 418 {
402 if (strlen(dots) > 0) 419 if (strlen (middle) > 0)
403 { 420 GNUNET_asprintf (&ret,
404 if (strlen(middle) > 0) 421 "(%s|%s|%s)",
405 GNUNET_asprintf(&ret, 422 before,
406 "(%s%s|%s|%s%s)", 423 middle,
407 beforep, dots, 424 after);
408 middle, 425 else if (1 == step)
409 afterp, dots); 426 GNUNET_asprintf (&ret,
410 else 427 "%s|%s",
411 GNUNET_asprintf(&ret, 428 before,
412 "(%s|%s)%s", 429 after);
413 beforep,
414 afterp,
415 dots);
416 }
417 else 430 else
418 { 431 GNUNET_asprintf (&ret,
419 if (strlen(middle) > 0) 432 "(%s|%s)",
420 GNUNET_asprintf(&ret, 433 before,
421 "(%s|%s|%s)", 434 after);
422 before,
423 middle,
424 after);
425 else if (1 == step)
426 GNUNET_asprintf(&ret,
427 "%s|%s",
428 before,
429 after);
430 else
431 GNUNET_asprintf(&ret,
432 "(%s|%s)",
433 before,
434 after);
435 }
436 } 435 }
437 else if (strlen(before) > 0) 436 }
437 else if (strlen (before) > 0)
438 {
439 if (strlen (dots) > 0)
438 { 440 {
439 if (strlen(dots) > 0) 441 if (strlen (middle) > 0)
440 { 442 GNUNET_asprintf (&ret,
441 if (strlen(middle) > 0) 443 "(%s%s|%s)",
442 GNUNET_asprintf(&ret, 444 beforep, dots,
443 "(%s%s|%s)", 445 middle);
444 beforep, dots,
445 middle);
446 else
447 GNUNET_asprintf(&ret,
448 "%s%s",
449 beforep, dots);
450 }
451 else 446 else
452 { 447 GNUNET_asprintf (&ret,
453 if (strlen(middle) > 0) 448 "%s%s",
454 GNUNET_asprintf(&ret, 449 beforep, dots);
455 "(%s|%s)",
456 before,
457 middle);
458 else
459 GNUNET_asprintf(&ret,
460 "%s",
461 before);
462 }
463 } 450 }
464 else if (strlen(after) > 0) 451 else
465 { 452 {
466 if (strlen(dots) > 0) 453 if (strlen (middle) > 0)
467 { 454 GNUNET_asprintf (&ret,
468 if (strlen(middle) > 0) 455 "(%s|%s)",
469 GNUNET_asprintf(&ret, 456 before,
470 "(%s|%s%s)", 457 middle);
471 middle,
472 afterp, dots);
473 else
474 GNUNET_asprintf(&ret,
475 "%s%s",
476 afterp, dots);
477 }
478 else 458 else
479 { 459 GNUNET_asprintf (&ret,
480 if (strlen(middle) > 0) 460 "%s",
481 GNUNET_asprintf(&ret, 461 before);
482 "%s|%s",
483 middle,
484 after);
485 else
486 GNUNET_asprintf(&ret,
487 "%s",
488 after);
489 }
490 } 462 }
491 else if (strlen(middle) > 0) 463 }
464 else if (strlen (after) > 0)
465 {
466 if (strlen (dots) > 0)
492 { 467 {
493 GNUNET_asprintf(&ret, 468 if (strlen (middle) > 0)
494 "%s", 469 GNUNET_asprintf (&ret,
495 middle); 470 "(%s|%s%s)",
471 middle,
472 afterp, dots);
473 else
474 GNUNET_asprintf (&ret,
475 "%s%s",
476 afterp, dots);
496 } 477 }
497 else 478 else
498 { 479 {
499 ret = GNUNET_strdup(""); 480 if (strlen (middle) > 0)
481 GNUNET_asprintf (&ret,
482 "%s|%s",
483 middle,
484 after);
485 else
486 GNUNET_asprintf (&ret,
487 "%s",
488 after);
500 } 489 }
501 GNUNET_free(middle); 490 }
502 GNUNET_free(reclp); 491 else if (strlen (middle) > 0)
503 GNUNET_free(rechp); 492 {
504 GNUNET_free(recl); 493 GNUNET_asprintf (&ret,
505 GNUNET_free(rech); 494 "%s",
495 middle);
496 }
497 else
498 {
499 ret = GNUNET_strdup ("");
500 }
501 GNUNET_free (middle);
502 GNUNET_free (reclp);
503 GNUNET_free (rechp);
504 GNUNET_free (recl);
505 GNUNET_free (rech);
506 return ret; 506 return ret;
507} 507}
508 508
@@ -517,7 +517,7 @@ compute_policy(unsigned int start,
517 * @return NULL on error 517 * @return NULL on error
518 */ 518 */
519static char * 519static char *
520port_to_regex(const struct GNUNET_STRINGS_PortPolicy *pp) 520port_to_regex (const struct GNUNET_STRINGS_PortPolicy *pp)
521{ 521{
522 char *reg; 522 char *reg;
523 char *ret; 523 char *ret;
@@ -529,53 +529,53 @@ port_to_regex(const struct GNUNET_STRINGS_PortPolicy *pp)
529 ((1 == pp->start_port) && 529 ((1 == pp->start_port) &&
530 (0xFFFF == pp->end_port) && 530 (0xFFFF == pp->end_port) &&
531 (GNUNET_NO == pp->negate_portrange))) 531 (GNUNET_NO == pp->negate_portrange)))
532 return GNUNET_strdup(DOT DOT DOT DOT); 532 return GNUNET_strdup (DOT DOT DOT DOT);
533 if ((pp->start_port == pp->end_port) && 533 if ((pp->start_port == pp->end_port) &&
534 (GNUNET_NO == pp->negate_portrange)) 534 (GNUNET_NO == pp->negate_portrange))
535 { 535 {
536 GNUNET_asprintf(&ret, 536 GNUNET_asprintf (&ret,
537 "%04X", 537 "%04X",
538 pp->start_port); 538 pp->start_port);
539 return ret; 539 return ret;
540 } 540 }
541 if (pp->end_port < pp->start_port) 541 if (pp->end_port < pp->start_port)
542 return NULL; 542 return NULL;
543 543
544 if (GNUNET_YES == pp->negate_portrange) 544 if (GNUNET_YES == pp->negate_portrange)
545 { 545 {
546 ret = compute_policy(0, 0xFFFF, 0x1000, pp); 546 ret = compute_policy (0, 0xFFFF, 0x1000, pp);
547 } 547 }
548 else 548 else
549 {
550 cnt = pp->end_port - pp->start_port + 1;
551 reg = GNUNET_malloc (cnt * 5 + 1);
552 pos = reg;
553 for (i = 1; i <= 0xFFFF; i++)
549 { 554 {
550 cnt = pp->end_port - pp->start_port + 1; 555 if ((i >= pp->start_port) && (i <= pp->end_port))
551 reg = GNUNET_malloc(cnt * 5 + 1); 556 {
552 pos = reg; 557 if (pos == reg)
553 for (i = 1; i <= 0xFFFF; i++) 558 {
559 GNUNET_snprintf (pos,
560 5,
561 "%04X",
562 i);
563 }
564 else
554 { 565 {
555 if ((i >= pp->start_port) && (i <= pp->end_port)) 566 GNUNET_snprintf (pos,
556 { 567 6,
557 if (pos == reg) 568 "|%04X",
558 { 569 i);
559 GNUNET_snprintf(pos,
560 5,
561 "%04X",
562 i);
563 }
564 else
565 {
566 GNUNET_snprintf(pos,
567 6,
568 "|%04X",
569 i);
570 }
571 pos += strlen(pos);
572 }
573 } 570 }
574 GNUNET_asprintf(&ret, 571 pos += strlen (pos);
575 "(%s)", 572 }
576 reg);
577 GNUNET_free(reg);
578 } 573 }
574 GNUNET_asprintf (&ret,
575 "(%s)",
576 reg);
577 GNUNET_free (reg);
578 }
579 return ret; 579 return ret;
580} 580}
581 581
@@ -589,9 +589,9 @@ port_to_regex(const struct GNUNET_STRINGS_PortPolicy *pp)
589 * @return NULL on error, otherwise regex for the address 589 * @return NULL on error, otherwise regex for the address
590 */ 590 */
591static char * 591static char *
592address_to_regex(const void *addr, 592address_to_regex (const void *addr,
593 const void *mask, 593 const void *mask,
594 size_t len) 594 size_t len)
595{ 595{
596 const uint16_t *a = addr; 596 const uint16_t *a = addr;
597 const uint16_t *m = mask; 597 const uint16_t *m = mask;
@@ -601,29 +601,29 @@ address_to_regex(const void *addr,
601 unsigned int i; 601 unsigned int i;
602 602
603 ret = NULL; 603 ret = NULL;
604 GNUNET_assert(1 != (len % 2)); 604 GNUNET_assert (1 != (len % 2));
605 for (i = 0; i < len / 2; i++) 605 for (i = 0; i < len / 2; i++)
606 {
607 reg = num_to_regex (a[i], m[i]);
608 if (NULL == reg)
606 { 609 {
607 reg = num_to_regex(a[i], m[i]); 610 GNUNET_free_non_null (ret);
608 if (NULL == reg) 611 return NULL;
609 { 612 }
610 GNUNET_free_non_null(ret); 613 if (NULL == ret)
611 return NULL; 614 {
612 } 615 ret = reg;
613 if (NULL == ret) 616 }
614 { 617 else
615 ret = reg; 618 {
616 } 619 GNUNET_asprintf (&tmp,
617 else 620 "%s%s",
618 { 621 ret, reg);
619 GNUNET_asprintf(&tmp, 622 GNUNET_free (ret);
620 "%s%s", 623 GNUNET_free (reg);
621 ret, reg); 624 ret = tmp;
622 GNUNET_free(ret);
623 GNUNET_free(reg);
624 ret = tmp;
625 }
626 } 625 }
626 }
627 return ret; 627 return ret;
628} 628}
629 629
@@ -635,28 +635,28 @@ address_to_regex(const void *addr,
635 * @return NULL on error 635 * @return NULL on error
636 */ 636 */
637static char * 637static char *
638ipv4_to_regex(const struct GNUNET_STRINGS_IPv4NetworkPolicy *v4) 638ipv4_to_regex (const struct GNUNET_STRINGS_IPv4NetworkPolicy *v4)
639{ 639{
640 char *reg; 640 char *reg;
641 char *pp; 641 char *pp;
642 char *ret; 642 char *ret;
643 643
644 reg = address_to_regex(&v4->network, 644 reg = address_to_regex (&v4->network,
645 &v4->netmask, 645 &v4->netmask,
646 sizeof(struct in_addr)); 646 sizeof(struct in_addr));
647 if (NULL == reg) 647 if (NULL == reg)
648 return NULL; 648 return NULL;
649 pp = port_to_regex(&v4->pp); 649 pp = port_to_regex (&v4->pp);
650 if (NULL == pp) 650 if (NULL == pp)
651 { 651 {
652 GNUNET_free(reg); 652 GNUNET_free (reg);
653 return NULL; 653 return NULL;
654 } 654 }
655 GNUNET_asprintf(&ret, 655 GNUNET_asprintf (&ret,
656 "4-%s-%s", 656 "4-%s-%s",
657 pp, reg); 657 pp, reg);
658 GNUNET_free(pp); 658 GNUNET_free (pp);
659 GNUNET_free(reg); 659 GNUNET_free (reg);
660 return ret; 660 return ret;
661} 661}
662 662
@@ -668,28 +668,28 @@ ipv4_to_regex(const struct GNUNET_STRINGS_IPv4NetworkPolicy *v4)
668 * @return NULL on error 668 * @return NULL on error
669 */ 669 */
670static char * 670static char *
671ipv6_to_regex(const struct GNUNET_STRINGS_IPv6NetworkPolicy *v6) 671ipv6_to_regex (const struct GNUNET_STRINGS_IPv6NetworkPolicy *v6)
672{ 672{
673 char *reg; 673 char *reg;
674 char *pp; 674 char *pp;
675 char *ret; 675 char *ret;
676 676
677 reg = address_to_regex(&v6->network, 677 reg = address_to_regex (&v6->network,
678 &v6->netmask, 678 &v6->netmask,
679 sizeof(struct in6_addr)); 679 sizeof(struct in6_addr));
680 if (NULL == reg) 680 if (NULL == reg)
681 return NULL; 681 return NULL;
682 pp = port_to_regex(&v6->pp); 682 pp = port_to_regex (&v6->pp);
683 if (NULL == pp) 683 if (NULL == pp)
684 { 684 {
685 GNUNET_free(reg); 685 GNUNET_free (reg);
686 return NULL; 686 return NULL;
687 } 687 }
688 GNUNET_asprintf(&ret, 688 GNUNET_asprintf (&ret,
689 "6-%s-%s", 689 "6-%s-%s",
690 pp, reg); 690 pp, reg);
691 GNUNET_free(pp); 691 GNUNET_free (pp);
692 GNUNET_free(reg); 692 GNUNET_free (reg);
693 return ret; 693 return ret;
694} 694}
695 695
@@ -704,7 +704,7 @@ ipv6_to_regex(const struct GNUNET_STRINGS_IPv6NetworkPolicy *v6)
704 * @return regular expression, NULL on error 704 * @return regular expression, NULL on error
705 */ 705 */
706char * 706char *
707GNUNET_TUN_ipv4policy2regex(const char *policy) 707GNUNET_TUN_ipv4policy2regex (const char *policy)
708{ 708{
709 struct GNUNET_STRINGS_IPv4NetworkPolicy *np; 709 struct GNUNET_STRINGS_IPv4NetworkPolicy *np;
710 char *reg; 710 char *reg;
@@ -712,36 +712,36 @@ GNUNET_TUN_ipv4policy2regex(const char *policy)
712 char *line; 712 char *line;
713 unsigned int i; 713 unsigned int i;
714 714
715 np = GNUNET_STRINGS_parse_ipv4_policy(policy); 715 np = GNUNET_STRINGS_parse_ipv4_policy (policy);
716 if (NULL == np) 716 if (NULL == np)
717 return NULL; 717 return NULL;
718 reg = NULL; 718 reg = NULL;
719 for (i = 0; (0 == i) || (0 != np[i].network.s_addr); i++) 719 for (i = 0; (0 == i) || (0 != np[i].network.s_addr); i++)
720 {
721 line = ipv4_to_regex (&np[i]);
722 if (NULL == line)
720 { 723 {
721 line = ipv4_to_regex(&np[i]); 724 GNUNET_free_non_null (reg);
722 if (NULL == line) 725 GNUNET_free (np);
723 { 726 return NULL;
724 GNUNET_free_non_null(reg); 727 }
725 GNUNET_free(np); 728 if (NULL == reg)
726 return NULL; 729 {
727 } 730 reg = line;
728 if (NULL == reg)
729 {
730 reg = line;
731 }
732 else
733 {
734 GNUNET_asprintf(&tmp,
735 "%s|(%s)",
736 reg, line);
737 GNUNET_free(reg);
738 GNUNET_free(line);
739 reg = tmp;
740 }
741 if (0 == np[i].network.s_addr)
742 break;
743 } 731 }
744 GNUNET_free(np); 732 else
733 {
734 GNUNET_asprintf (&tmp,
735 "%s|(%s)",
736 reg, line);
737 GNUNET_free (reg);
738 GNUNET_free (line);
739 reg = tmp;
740 }
741 if (0 == np[i].network.s_addr)
742 break;
743 }
744 GNUNET_free (np);
745 return reg; 745 return reg;
746} 746}
747 747
@@ -756,7 +756,7 @@ GNUNET_TUN_ipv4policy2regex(const char *policy)
756 * @return regular expression, NULL on error 756 * @return regular expression, NULL on error
757 */ 757 */
758char * 758char *
759GNUNET_TUN_ipv6policy2regex(const char *policy) 759GNUNET_TUN_ipv6policy2regex (const char *policy)
760{ 760{
761 struct in6_addr zero; 761 struct in6_addr zero;
762 struct GNUNET_STRINGS_IPv6NetworkPolicy *np; 762 struct GNUNET_STRINGS_IPv6NetworkPolicy *np;
@@ -765,37 +765,39 @@ GNUNET_TUN_ipv6policy2regex(const char *policy)
765 char *line; 765 char *line;
766 unsigned int i; 766 unsigned int i;
767 767
768 np = GNUNET_STRINGS_parse_ipv6_policy(policy); 768 np = GNUNET_STRINGS_parse_ipv6_policy (policy);
769 if (NULL == np) 769 if (NULL == np)
770 return NULL; 770 return NULL;
771 reg = NULL; 771 reg = NULL;
772 memset(&zero, 0, sizeof(struct in6_addr)); 772 memset (&zero, 0, sizeof(struct in6_addr));
773 for (i = 0; (0 == i) || (0 != memcmp(&zero, &np[i].network, sizeof(struct in6_addr))); i++) 773 for (i = 0; (0 == i) || (0 != memcmp (&zero, &np[i].network, sizeof(struct
774 in6_addr)));
775 i++)
776 {
777 line = ipv6_to_regex (&np[i]);
778 if (NULL == line)
774 { 779 {
775 line = ipv6_to_regex(&np[i]); 780 GNUNET_free_non_null (reg);
776 if (NULL == line) 781 GNUNET_free (np);
777 { 782 return NULL;
778 GNUNET_free_non_null(reg); 783 }
779 GNUNET_free(np); 784 if (NULL == reg)
780 return NULL; 785 {
781 } 786 reg = line;
782 if (NULL == reg) 787 }
783 { 788 else
784 reg = line; 789 {
785 } 790 GNUNET_asprintf (&tmp,
786 else 791 "%s|(%s)",
787 { 792 reg, line);
788 GNUNET_asprintf(&tmp, 793 GNUNET_free (reg);
789 "%s|(%s)", 794 GNUNET_free (line);
790 reg, line); 795 reg = tmp;
791 GNUNET_free(reg);
792 GNUNET_free(line);
793 reg = tmp;
794 }
795 if (0 == memcmp(&zero, &np[i].network, sizeof(struct in6_addr)))
796 break;
797 } 796 }
798 GNUNET_free(np); 797 if (0 == memcmp (&zero, &np[i].network, sizeof(struct in6_addr)))
798 break;
799 }
800 GNUNET_free (np);
799 return reg; 801 return reg;
800} 802}
801 803
@@ -809,12 +811,12 @@ GNUNET_TUN_ipv6policy2regex(const char *policy)
809 * @param hc corresponding hash 811 * @param hc corresponding hash
810 */ 812 */
811void 813void
812GNUNET_TUN_service_name_to_hash(const char *service_name, 814GNUNET_TUN_service_name_to_hash (const char *service_name,
813 struct GNUNET_HashCode *hc) 815 struct GNUNET_HashCode *hc)
814{ 816{
815 GNUNET_CRYPTO_hash(service_name, 817 GNUNET_CRYPTO_hash (service_name,
816 strlen(service_name), 818 strlen (service_name),
817 hc); 819 hc);
818} 820}
819 821
820 822
@@ -828,16 +830,16 @@ GNUNET_TUN_service_name_to_hash(const char *service_name,
828 * @param[out] cadet_port CADET port to use 830 * @param[out] cadet_port CADET port to use
829 */ 831 */
830void 832void
831GNUNET_TUN_compute_service_cadet_port(const struct GNUNET_HashCode *desc, 833GNUNET_TUN_compute_service_cadet_port (const struct GNUNET_HashCode *desc,
832 uint16_t ip_port, 834 uint16_t ip_port,
833 struct GNUNET_HashCode *cadet_port) 835 struct GNUNET_HashCode *cadet_port)
834{ 836{
835 uint16_t be_port = htons(ip_port); 837 uint16_t be_port = htons (ip_port);
836 838
837 *cadet_port = *desc; 839 *cadet_port = *desc;
838 GNUNET_memcpy(cadet_port, 840 GNUNET_memcpy (cadet_port,
839 &be_port, 841 &be_port,
840 sizeof(uint16_t)); 842 sizeof(uint16_t));
841} 843}
842 844
843 845
diff --git a/src/util/resolver.h b/src/util/resolver.h
index 9b75b0c66..e487f6e6f 100644
--- a/src/util/resolver.h
+++ b/src/util/resolver.h
@@ -40,7 +40,8 @@ GNUNET_NETWORK_STRUCT_BEGIN
40 * will just be a header without any data (used to indicate the end of 40 * will just be a header without any data (used to indicate the end of
41 * the list). 41 * the list).
42 */ 42 */
43struct GNUNET_RESOLVER_GetMessage { 43struct GNUNET_RESOLVER_GetMessage
44{
44 /** 45 /**
45 * Type: #GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST 46 * Type: #GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST
46 */ 47 */
@@ -68,7 +69,8 @@ struct GNUNET_RESOLVER_GetMessage {
68}; 69};
69 70
70 71
71struct GNUNET_RESOLVER_ResponseMessage { 72struct GNUNET_RESOLVER_ResponseMessage
73{
72 /** 74 /**
73 * Type: #GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE 75 * Type: #GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE
74 */ 76 */
diff --git a/src/util/resolver_api.c b/src/util/resolver_api.c
index 6c56786b0..a1e30bbd4 100644
--- a/src/util/resolver_api.c
+++ b/src/util/resolver_api.c
@@ -29,9 +29,11 @@
29#include "gnunet_resolver_service.h" 29#include "gnunet_resolver_service.h"
30#include "resolver.h" 30#include "resolver.h"
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-resolver-api", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-resolver-api", __VA_ARGS__)
33 33
34#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror(kind, "util-resolver-api", syscall) 34#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
35 "util-resolver-api", \
36 syscall)
35 37
36/** 38/**
37 * Maximum supported length for a hostname 39 * Maximum supported length for a hostname
@@ -97,7 +99,8 @@ static struct GNUNET_SCHEDULER_Task *s_task;
97 * the request prior to the timeout or successful execution. Also 99 * the request prior to the timeout or successful execution. Also
98 * used to track our internal state for the request. 100 * used to track our internal state for the request.
99 */ 101 */
100struct GNUNET_RESOLVER_RequestHandle { 102struct GNUNET_RESOLVER_RequestHandle
103{
101 /** 104 /**
102 * Next entry in DLL of requests. 105 * Next entry in DLL of requests.
103 */ 106 */
@@ -184,61 +187,63 @@ struct GNUNET_RESOLVER_RequestHandle {
184 * #GNUNET_SYSERR otherwise. 187 * #GNUNET_SYSERR otherwise.
185 */ 188 */
186static int 189static int
187check_config() 190check_config ()
188{ 191{
189 char *hostname; 192 char *hostname;
190 struct sockaddr_in v4; 193 struct sockaddr_in v4;
191 struct sockaddr_in6 v6; 194 struct sockaddr_in6 v6;
192 195
193 if (GNUNET_OK == 196 if (GNUNET_OK ==
194 GNUNET_CONFIGURATION_have_value(resolver_cfg, 197 GNUNET_CONFIGURATION_have_value (resolver_cfg,
195 "resolver", 198 "resolver",
196 "UNIXPATH")) 199 "UNIXPATH"))
197 return GNUNET_OK; 200 return GNUNET_OK;
198 memset(&v4, 0, sizeof(v4)); 201 memset (&v4, 0, sizeof(v4));
199 v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 202 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
200 v4.sin_family = AF_INET; 203 v4.sin_family = AF_INET;
201#if HAVE_SOCKADDR_IN_SIN_LEN 204#if HAVE_SOCKADDR_IN_SIN_LEN
202 v4.sin_len = sizeof(v4); 205 v4.sin_len = sizeof(v4);
203#endif 206#endif
204 memset(&v6, 0, sizeof(v6)); 207 memset (&v6, 0, sizeof(v6));
205 v6.sin6_family = AF_INET6; 208 v6.sin6_family = AF_INET6;
206#if HAVE_SOCKADDR_IN_SIN_LEN 209#if HAVE_SOCKADDR_IN_SIN_LEN
207 v6.sin6_len = sizeof(v6); 210 v6.sin6_len = sizeof(v6);
208#endif 211#endif
209 if (GNUNET_OK != 212 if (GNUNET_OK !=
210 GNUNET_CONFIGURATION_get_value_string(resolver_cfg, 213 GNUNET_CONFIGURATION_get_value_string (resolver_cfg,
211 "resolver", 214 "resolver",
212 "HOSTNAME", 215 "HOSTNAME",
213 &hostname)) 216 &hostname))
214 { 217 {
215 LOG(GNUNET_ERROR_TYPE_INFO, 218 LOG (GNUNET_ERROR_TYPE_INFO,
216 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"), 219 _ (
217 "HOSTNAME", 220 "Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
218 "resolver"); 221 "HOSTNAME",
219 return GNUNET_SYSERR; 222 "resolver");
220 } 223 return GNUNET_SYSERR;
221 if ((1 == inet_pton(AF_INET, hostname, &v4)) || 224 }
222 (1 == inet_pton(AF_INET6, hostname, &v6))) 225 if ((1 == inet_pton (AF_INET, hostname, &v4)) ||
223 { 226 (1 == inet_pton (AF_INET6, hostname, &v6)))
224 GNUNET_free(hostname); 227 {
225 return GNUNET_OK; 228 GNUNET_free (hostname);
226 } 229 return GNUNET_OK;
230 }
227 for (unsigned int i = 0; 231 for (unsigned int i = 0;
228 NULL != loopback[i]; 232 NULL != loopback[i];
229 i++) 233 i++)
230 if (0 == strcasecmp(loopback[i], 234 if (0 == strcasecmp (loopback[i],
231 hostname)) 235 hostname))
232 { 236 {
233 GNUNET_free(hostname); 237 GNUNET_free (hostname);
234 return GNUNET_OK; 238 return GNUNET_OK;
235 } 239 }
236 LOG(GNUNET_ERROR_TYPE_INFO, 240 LOG (GNUNET_ERROR_TYPE_INFO,
237 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"), 241 _ (
238 "localhost", 242 "Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
239 "HOSTNAME", 243 "localhost",
240 "resolver"); 244 "HOSTNAME",
241 GNUNET_free(hostname); 245 "resolver");
246 GNUNET_free (hostname);
242 return GNUNET_SYSERR; 247 return GNUNET_SYSERR;
243} 248}
244 249
@@ -249,9 +254,9 @@ check_config()
249 * @param cfg configuration to use 254 * @param cfg configuration to use
250 */ 255 */
251void 256void
252GNUNET_RESOLVER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg) 257GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
253{ 258{
254 GNUNET_assert(NULL != cfg); 259 GNUNET_assert (NULL != cfg);
255 backoff = GNUNET_TIME_UNIT_MILLISECONDS; 260 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
256 resolver_cfg = cfg; 261 resolver_cfg = cfg;
257} 262}
@@ -261,35 +266,35 @@ GNUNET_RESOLVER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
261 * Destroy the connection to the resolver service. 266 * Destroy the connection to the resolver service.
262 */ 267 */
263void 268void
264GNUNET_RESOLVER_disconnect() 269GNUNET_RESOLVER_disconnect ()
265{ 270{
266 struct GNUNET_RESOLVER_RequestHandle *rh; 271 struct GNUNET_RESOLVER_RequestHandle *rh;
267 272
268 while (NULL != (rh = req_head)) 273 while (NULL != (rh = req_head))
269 { 274 {
270 GNUNET_assert(GNUNET_SYSERR == rh->was_transmitted); 275 GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted);
271 GNUNET_CONTAINER_DLL_remove(req_head, 276 GNUNET_CONTAINER_DLL_remove (req_head,
272 req_tail, 277 req_tail,
273 rh); 278 rh);
274 GNUNET_free(rh); 279 GNUNET_free (rh);
275 } 280 }
276 if (NULL != mq) 281 if (NULL != mq)
277 { 282 {
278 LOG(GNUNET_ERROR_TYPE_DEBUG, 283 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "Disconnecting from DNS service\n"); 284 "Disconnecting from DNS service\n");
280 GNUNET_MQ_destroy(mq); 285 GNUNET_MQ_destroy (mq);
281 mq = NULL; 286 mq = NULL;
282 } 287 }
283 if (NULL != r_task) 288 if (NULL != r_task)
284 { 289 {
285 GNUNET_SCHEDULER_cancel(r_task); 290 GNUNET_SCHEDULER_cancel (r_task);
286 r_task = NULL; 291 r_task = NULL;
287 } 292 }
288 if (NULL != s_task) 293 if (NULL != s_task)
289 { 294 {
290 GNUNET_SCHEDULER_cancel(s_task); 295 GNUNET_SCHEDULER_cancel (s_task);
291 s_task = NULL; 296 s_task = NULL;
292 } 297 }
293} 298}
294 299
295 300
@@ -297,11 +302,11 @@ GNUNET_RESOLVER_disconnect()
297 * Task executed on system shutdown. 302 * Task executed on system shutdown.
298 */ 303 */
299static void 304static void
300shutdown_task(void *cls) 305shutdown_task (void *cls)
301{ 306{
302 (void)cls; 307 (void) cls;
303 s_task = NULL; 308 s_task = NULL;
304 GNUNET_RESOLVER_disconnect(); 309 GNUNET_RESOLVER_disconnect ();
305 backoff = GNUNET_TIME_UNIT_MILLISECONDS; 310 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
306} 311}
307 312
@@ -310,7 +315,7 @@ shutdown_task(void *cls)
310 * Consider disconnecting if we have no further requests pending. 315 * Consider disconnecting if we have no further requests pending.
311 */ 316 */
312static void 317static void
313check_disconnect() 318check_disconnect ()
314{ 319{
315 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head; 320 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
316 NULL != rh; 321 NULL != rh;
@@ -318,15 +323,15 @@ check_disconnect()
318 if (GNUNET_SYSERR != rh->was_transmitted) 323 if (GNUNET_SYSERR != rh->was_transmitted)
319 return; 324 return;
320 if (NULL != r_task) 325 if (NULL != r_task)
321 { 326 {
322 GNUNET_SCHEDULER_cancel(r_task); 327 GNUNET_SCHEDULER_cancel (r_task);
323 r_task = NULL; 328 r_task = NULL;
324 } 329 }
325 if (NULL != s_task) 330 if (NULL != s_task)
326 return; 331 return;
327 s_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MILLISECONDS, 332 s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
328 &shutdown_task, 333 &shutdown_task,
329 NULL); 334 NULL);
330} 335}
331 336
332 337
@@ -339,49 +344,49 @@ check_disconnect()
339 * @return address as a string, NULL on error 344 * @return address as a string, NULL on error
340 */ 345 */
341static char * 346static char *
342no_resolve(int af, 347no_resolve (int af,
343 const void *ip, 348 const void *ip,
344 socklen_t ip_len) 349 socklen_t ip_len)
345{ 350{
346 char buf[INET6_ADDRSTRLEN]; 351 char buf[INET6_ADDRSTRLEN];
347 352
348 switch (af) 353 switch (af)
354 {
355 case AF_INET:
356 if (ip_len != sizeof(struct in_addr))
357 return NULL;
358 if (NULL ==
359 inet_ntop (AF_INET,
360 ip,
361 buf,
362 sizeof(buf)))
349 { 363 {
350 case AF_INET: 364 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
351 if (ip_len != sizeof(struct in_addr)) 365 "inet_ntop");
352 return NULL; 366 return NULL;
353 if (NULL == 367 }
354 inet_ntop(AF_INET, 368 break;
355 ip,
356 buf,
357 sizeof(buf)))
358 {
359 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
360 "inet_ntop");
361 return NULL;
362 }
363 break;
364
365 case AF_INET6:
366 if (ip_len != sizeof(struct in6_addr))
367 return NULL;
368 if (NULL ==
369 inet_ntop(AF_INET6,
370 ip,
371 buf,
372 sizeof(buf)))
373 {
374 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
375 "inet_ntop");
376 return NULL;
377 }
378 break;
379 369
380 default: 370 case AF_INET6:
381 GNUNET_break(0); 371 if (ip_len != sizeof(struct in6_addr))
372 return NULL;
373 if (NULL ==
374 inet_ntop (AF_INET6,
375 ip,
376 buf,
377 sizeof(buf)))
378 {
379 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
380 "inet_ntop");
382 return NULL; 381 return NULL;
383 } 382 }
384 return GNUNET_strdup(buf); 383 break;
384
385 default:
386 GNUNET_break (0);
387 return NULL;
388 }
389 return GNUNET_strdup (buf);
385} 390}
386 391
387 392
@@ -389,7 +394,7 @@ no_resolve(int af,
389 * Adjust exponential back-off and reconnect to the service. 394 * Adjust exponential back-off and reconnect to the service.
390 */ 395 */
391static void 396static void
392reconnect(void); 397reconnect (void);
393 398
394 399
395/** 400/**
@@ -401,16 +406,16 @@ reconnect(void);
401 * @param error error code 406 * @param error error code
402 */ 407 */
403static void 408static void
404mq_error_handler(void *cls, 409mq_error_handler (void *cls,
405 enum GNUNET_MQ_Error error) 410 enum GNUNET_MQ_Error error)
406{ 411{
407 (void)cls; 412 (void) cls;
408 GNUNET_MQ_destroy(mq); 413 GNUNET_MQ_destroy (mq);
409 mq = NULL; 414 mq = NULL;
410 LOG(GNUNET_ERROR_TYPE_DEBUG, 415 LOG (GNUNET_ERROR_TYPE_DEBUG,
411 "MQ error %d, reconnecting\n", 416 "MQ error %d, reconnecting\n",
412 error); 417 error);
413 reconnect(); 418 reconnect ();
414} 419}
415 420
416 421
@@ -418,44 +423,44 @@ mq_error_handler(void *cls,
418 * Process pending requests to the resolver. 423 * Process pending requests to the resolver.
419 */ 424 */
420static void 425static void
421process_requests() 426process_requests ()
422{ 427{
423 struct GNUNET_RESOLVER_GetMessage *msg; 428 struct GNUNET_RESOLVER_GetMessage *msg;
424 struct GNUNET_MQ_Envelope *env; 429 struct GNUNET_MQ_Envelope *env;
425 struct GNUNET_RESOLVER_RequestHandle *rh = req_head; 430 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
426 431
427 if (NULL == mq) 432 if (NULL == mq)
428 { 433 {
429 reconnect(); 434 reconnect ();
430 return; 435 return;
431 } 436 }
432 if (NULL == rh) 437 if (NULL == rh)
433 { 438 {
434 /* nothing to do, release socket really soon if there is nothing 439 /* nothing to do, release socket really soon if there is nothing
435 * else happening... */ 440 * else happening... */
436 if (NULL == s_task) 441 if (NULL == s_task)
437 s_task = 442 s_task =
438 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MILLISECONDS, 443 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
439 &shutdown_task, 444 &shutdown_task,
440 NULL); 445 NULL);
441 return; 446 return;
442 } 447 }
443 if (GNUNET_NO != rh->was_transmitted) 448 if (GNUNET_NO != rh->was_transmitted)
444 return; /* waiting for reply */ 449 return; /* waiting for reply */
445 env = GNUNET_MQ_msg_extra(msg, 450 env = GNUNET_MQ_msg_extra (msg,
446 rh->data_len, 451 rh->data_len,
447 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST); 452 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
448 msg->direction = htonl(rh->direction); 453 msg->direction = htonl (rh->direction);
449 msg->af = htonl(rh->af); 454 msg->af = htonl (rh->af);
450 msg->client_id = rh->id; 455 msg->client_id = rh->id;
451 GNUNET_memcpy(&msg[1], 456 GNUNET_memcpy (&msg[1],
452 &rh[1], 457 &rh[1],
453 rh->data_len); 458 rh->data_len);
454 LOG(GNUNET_ERROR_TYPE_DEBUG, 459 LOG (GNUNET_ERROR_TYPE_DEBUG,
455 "Transmitting DNS resolution request (ID %u) to DNS service\n", 460 "Transmitting DNS resolution request (ID %u) to DNS service\n",
456 rh->id); 461 rh->id);
457 GNUNET_MQ_send(mq, 462 GNUNET_MQ_send (mq,
458 env); 463 env);
459 rh->was_transmitted = GNUNET_YES; 464 rh->was_transmitted = GNUNET_YES;
460} 465}
461 466
@@ -467,11 +472,11 @@ process_requests()
467 * @param msg message with the hostname 472 * @param msg message with the hostname
468 */ 473 */
469static int 474static int
470check_response(void *cls, 475check_response (void *cls,
471 const struct GNUNET_RESOLVER_ResponseMessage *msg) 476 const struct GNUNET_RESOLVER_ResponseMessage *msg)
472{ 477{
473 (void)cls; 478 (void) cls;
474 (void)msg; 479 (void) msg;
475 480
476 /* implemented in #handle_response() for now */ 481 /* implemented in #handle_response() for now */
477 return GNUNET_OK; 482 return GNUNET_OK;
@@ -487,8 +492,8 @@ check_response(void *cls,
487 * @param msg message with the response 492 * @param msg message with the response
488 */ 493 */
489static void 494static void
490handle_response(void *cls, 495handle_response (void *cls,
491 const struct GNUNET_RESOLVER_ResponseMessage *msg) 496 const struct GNUNET_RESOLVER_ResponseMessage *msg)
492{ 497{
493 struct GNUNET_RESOLVER_RequestHandle *rh = req_head; 498 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
494 uint16_t size; 499 uint16_t size;
@@ -496,141 +501,142 @@ handle_response(void *cls,
496 uint32_t client_request_id = msg->client_id; 501 uint32_t client_request_id = msg->client_id;
497 502
498 for (; rh != NULL; rh = rh->next) 503 for (; rh != NULL; rh = rh->next)
499 { 504 {
500 if (rh->id == client_request_id) 505 if (rh->id == client_request_id)
501 break; 506 break;
502 } 507 }
503 508
504 (void)cls; 509 (void) cls;
505 if (NULL == rh) 510 if (NULL == rh)
506 { 511 {
507 /* Resolver service sent extra replies to query (after terminator)? Bad! */ 512 /* Resolver service sent extra replies to query (after terminator)? Bad! */
508 GNUNET_break(0); 513 GNUNET_break (0);
509 GNUNET_MQ_destroy(mq); 514 GNUNET_MQ_destroy (mq);
510 mq = NULL; 515 mq = NULL;
511 reconnect(); 516 reconnect ();
512 return; 517 return;
513 } 518 }
514 size = ntohs(msg->header.size); 519 size = ntohs (msg->header.size);
515 if (size == sizeof(struct GNUNET_RESOLVER_ResponseMessage)) 520 if (size == sizeof(struct GNUNET_RESOLVER_ResponseMessage))
521 {
522 LOG (GNUNET_ERROR_TYPE_DEBUG,
523 "Received empty response from DNS service\n");
524 /* message contains not data, just header; end of replies */
525 /* check if request was canceled */
526 if (GNUNET_SYSERR != rh->was_transmitted)
516 { 527 {
517 LOG(GNUNET_ERROR_TYPE_DEBUG, 528 /* no reverse lookup was successful, return IP as string */
518 "Received empty response from DNS service\n"); 529 if (NULL != rh->name_callback)
519 /* message contains not data, just header; end of replies */ 530 {
520 /* check if request was canceled */ 531 if (GNUNET_NO == rh->received_response)
521 if (GNUNET_SYSERR != rh->was_transmitted)
522 { 532 {
523 /* no reverse lookup was successful, return IP as string */ 533 nret = no_resolve (rh->af,
524 if (NULL != rh->name_callback) 534 &rh[1],
525 { 535 rh->data_len);
526 if (GNUNET_NO == rh->received_response) 536 rh->name_callback (rh->cls, nret);
527 { 537 GNUNET_free (nret);
528 nret = no_resolve(rh->af,
529 &rh[1],
530 rh->data_len);
531 rh->name_callback(rh->cls, nret);
532 GNUNET_free(nret);
533 }
534 /* finally, make termination call */
535 if (GNUNET_SYSERR != rh->was_transmitted)
536 rh->name_callback(rh->cls,
537 NULL);
538 }
539 if ((NULL != rh->addr_callback) &&
540 (GNUNET_SYSERR != rh->was_transmitted))
541 rh->addr_callback(rh->cls,
542 NULL,
543 0);
544 } 538 }
545 rh->was_transmitted = GNUNET_NO; 539 /* finally, make termination call */
546 GNUNET_RESOLVER_request_cancel(rh); 540 if (GNUNET_SYSERR != rh->was_transmitted)
547 process_requests(); 541 rh->name_callback (rh->cls,
548 return; 542 NULL);
543 }
544 if ((NULL != rh->addr_callback) &&
545 (GNUNET_SYSERR != rh->was_transmitted))
546 rh->addr_callback (rh->cls,
547 NULL,
548 0);
549 } 549 }
550 rh->was_transmitted = GNUNET_NO;
551 GNUNET_RESOLVER_request_cancel (rh);
552 process_requests ();
553 return;
554 }
550 /* return reverse lookup results to caller */ 555 /* return reverse lookup results to caller */
551 if (NULL != rh->name_callback) 556 if (NULL != rh->name_callback)
552 { 557 {
553 const char *hostname; 558 const char *hostname;
554 559
555 hostname = (const char *)&msg[1]; 560 hostname = (const char *) &msg[1];
556 if (hostname[size - sizeof(struct GNUNET_RESOLVER_ResponseMessage) - 1] != '\0') 561 if (hostname[size - sizeof(struct GNUNET_RESOLVER_ResponseMessage) - 1] !=
557 { 562 '\0')
558 GNUNET_break(0); 563 {
559 if (GNUNET_SYSERR != rh->was_transmitted) 564 GNUNET_break (0);
560 rh->name_callback(rh->cls, 565 if (GNUNET_SYSERR != rh->was_transmitted)
561 NULL); 566 rh->name_callback (rh->cls,
562 rh->was_transmitted = GNUNET_NO; 567 NULL);
563 GNUNET_RESOLVER_request_cancel(rh); 568 rh->was_transmitted = GNUNET_NO;
564 GNUNET_MQ_destroy(mq); 569 GNUNET_RESOLVER_request_cancel (rh);
565 mq = NULL; 570 GNUNET_MQ_destroy (mq);
566 reconnect(); 571 mq = NULL;
567 return; 572 reconnect ();
568 } 573 return;
569 LOG(GNUNET_ERROR_TYPE_DEBUG,
570 "Resolver returns `%s' for IP `%s'.\n",
571 hostname,
572 GNUNET_a2s((const void *)&rh[1],
573 rh->data_len));
574 if (rh->was_transmitted != GNUNET_SYSERR)
575 rh->name_callback(rh->cls,
576 hostname);
577 rh->received_response = GNUNET_YES;
578 } 574 }
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "Resolver returns `%s' for IP `%s'.\n",
577 hostname,
578 GNUNET_a2s ((const void *) &rh[1],
579 rh->data_len));
580 if (rh->was_transmitted != GNUNET_SYSERR)
581 rh->name_callback (rh->cls,
582 hostname);
583 rh->received_response = GNUNET_YES;
584 }
579 /* return lookup results to caller */ 585 /* return lookup results to caller */
580 if (NULL != rh->addr_callback) 586 if (NULL != rh->addr_callback)
587 {
588 struct sockaddr_in v4;
589 struct sockaddr_in6 v6;
590 const struct sockaddr *sa;
591 socklen_t salen;
592 const void *ip;
593 size_t ip_len;
594
595 ip = &msg[1];
596 ip_len = size - sizeof(struct GNUNET_RESOLVER_ResponseMessage);
597 if (ip_len == sizeof(struct in_addr))
581 { 598 {
582 struct sockaddr_in v4; 599 memset (&v4, 0, sizeof(v4));
583 struct sockaddr_in6 v6; 600 v4.sin_family = AF_INET;
584 const struct sockaddr *sa; 601 v4.sin_addr = *(struct in_addr*) ip;
585 socklen_t salen;
586 const void *ip;
587 size_t ip_len;
588
589 ip = &msg[1];
590 ip_len = size - sizeof(struct GNUNET_RESOLVER_ResponseMessage);
591 if (ip_len == sizeof(struct in_addr))
592 {
593 memset(&v4, 0, sizeof(v4));
594 v4.sin_family = AF_INET;
595 v4.sin_addr = *(struct in_addr*)ip;
596#if HAVE_SOCKADDR_IN_SIN_LEN 602#if HAVE_SOCKADDR_IN_SIN_LEN
597 v4.sin_len = sizeof(v4); 603 v4.sin_len = sizeof(v4);
598#endif 604#endif
599 salen = sizeof(v4); 605 salen = sizeof(v4);
600 sa = (const struct sockaddr *)&v4; 606 sa = (const struct sockaddr *) &v4;
601 } 607 }
602 else if (ip_len == sizeof(struct in6_addr)) 608 else if (ip_len == sizeof(struct in6_addr))
603 { 609 {
604 memset(&v6, 0, sizeof(v6)); 610 memset (&v6, 0, sizeof(v6));
605 v6.sin6_family = AF_INET6; 611 v6.sin6_family = AF_INET6;
606 v6.sin6_addr = *(struct in6_addr*)ip; 612 v6.sin6_addr = *(struct in6_addr*) ip;
607#if HAVE_SOCKADDR_IN_SIN_LEN 613#if HAVE_SOCKADDR_IN_SIN_LEN
608 v6.sin6_len = sizeof(v6); 614 v6.sin6_len = sizeof(v6);
609#endif 615#endif
610 salen = sizeof(v6); 616 salen = sizeof(v6);
611 sa = (const struct sockaddr *)&v6; 617 sa = (const struct sockaddr *) &v6;
612 } 618 }
613 else 619 else
614 { 620 {
615 GNUNET_break(0); 621 GNUNET_break (0);
616 if (GNUNET_SYSERR != rh->was_transmitted)
617 rh->addr_callback(rh->cls,
618 NULL,
619 0);
620 rh->was_transmitted = GNUNET_NO;
621 GNUNET_RESOLVER_request_cancel(rh);
622 GNUNET_MQ_destroy(mq);
623 mq = NULL;
624 reconnect();
625 return;
626 }
627 LOG(GNUNET_ERROR_TYPE_DEBUG,
628 "Received IP from DNS service\n");
629 if (GNUNET_SYSERR != rh->was_transmitted) 622 if (GNUNET_SYSERR != rh->was_transmitted)
630 rh->addr_callback(rh->cls, 623 rh->addr_callback (rh->cls,
631 sa, 624 NULL,
632 salen); 625 0);
626 rh->was_transmitted = GNUNET_NO;
627 GNUNET_RESOLVER_request_cancel (rh);
628 GNUNET_MQ_destroy (mq);
629 mq = NULL;
630 reconnect ();
631 return;
633 } 632 }
633 LOG (GNUNET_ERROR_TYPE_DEBUG,
634 "Received IP from DNS service\n");
635 if (GNUNET_SYSERR != rh->was_transmitted)
636 rh->addr_callback (rh->cls,
637 sa,
638 salen);
639 }
634} 640}
635 641
636 642
@@ -642,7 +648,7 @@ handle_response(void *cls,
642 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request 648 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
643 */ 649 */
644static void 650static void
645numeric_resolution(void *cls) 651numeric_resolution (void *cls)
646{ 652{
647 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 653 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
648 struct sockaddr_in v4; 654 struct sockaddr_in v4;
@@ -650,63 +656,63 @@ numeric_resolution(void *cls)
650 const char *hostname; 656 const char *hostname;
651 657
652 rh->task = NULL; 658 rh->task = NULL;
653 memset(&v4, 0, sizeof(v4)); 659 memset (&v4, 0, sizeof(v4));
654 v4.sin_family = AF_INET; 660 v4.sin_family = AF_INET;
655#if HAVE_SOCKADDR_IN_SIN_LEN 661#if HAVE_SOCKADDR_IN_SIN_LEN
656 v4.sin_len = sizeof(v4); 662 v4.sin_len = sizeof(v4);
657#endif 663#endif
658 memset(&v6, 0, sizeof(v6)); 664 memset (&v6, 0, sizeof(v6));
659 v6.sin6_family = AF_INET6; 665 v6.sin6_family = AF_INET6;
660#if HAVE_SOCKADDR_IN_SIN_LEN 666#if HAVE_SOCKADDR_IN_SIN_LEN
661 v6.sin6_len = sizeof(v6); 667 v6.sin6_len = sizeof(v6);
662#endif 668#endif
663 hostname = (const char *)&rh[1]; 669 hostname = (const char *) &rh[1];
664 if (((rh->af == AF_UNSPEC) || 670 if (((rh->af == AF_UNSPEC) ||
665 (rh->af == AF_INET)) && 671 (rh->af == AF_INET)) &&
666 (1 == inet_pton(AF_INET, 672 (1 == inet_pton (AF_INET,
667 hostname, 673 hostname,
668 &v4.sin_addr))) 674 &v4.sin_addr)))
675 {
676 rh->addr_callback (rh->cls,
677 (const struct sockaddr *) &v4,
678 sizeof(v4));
679 if ((rh->af == AF_UNSPEC) &&
680 (GNUNET_SYSERR != rh->was_transmitted) &&
681 (1 == inet_pton (AF_INET6,
682 hostname,
683 &v6.sin6_addr)))
669 { 684 {
670 rh->addr_callback(rh->cls, 685 /* this can happen on some systems IF "hostname" is "localhost" */
671 (const struct sockaddr *)&v4, 686 rh->addr_callback (rh->cls,
672 sizeof(v4)); 687 (const struct sockaddr *) &v6,
673 if ((rh->af == AF_UNSPEC) && 688 sizeof(v6));
674 (GNUNET_SYSERR != rh->was_transmitted) &&
675 (1 == inet_pton(AF_INET6,
676 hostname,
677 &v6.sin6_addr)))
678 {
679 /* this can happen on some systems IF "hostname" is "localhost" */
680 rh->addr_callback(rh->cls,
681 (const struct sockaddr *)&v6,
682 sizeof(v6));
683 }
684 if (GNUNET_SYSERR != rh->was_transmitted)
685 rh->addr_callback(rh->cls,
686 NULL,
687 0);
688 GNUNET_free(rh);
689 return;
690 } 689 }
690 if (GNUNET_SYSERR != rh->was_transmitted)
691 rh->addr_callback (rh->cls,
692 NULL,
693 0);
694 GNUNET_free (rh);
695 return;
696 }
691 if (((rh->af == AF_UNSPEC) || 697 if (((rh->af == AF_UNSPEC) ||
692 (rh->af == AF_INET6)) && 698 (rh->af == AF_INET6)) &&
693 (1 == inet_pton(AF_INET6, 699 (1 == inet_pton (AF_INET6,
694 hostname, 700 hostname,
695 &v6.sin6_addr))) 701 &v6.sin6_addr)))
696 { 702 {
697 rh->addr_callback(rh->cls, 703 rh->addr_callback (rh->cls,
698 (const struct sockaddr *)&v6, 704 (const struct sockaddr *) &v6,
699 sizeof(v6)); 705 sizeof(v6));
700 if (GNUNET_SYSERR != rh->was_transmitted) 706 if (GNUNET_SYSERR != rh->was_transmitted)
701 rh->addr_callback(rh->cls, 707 rh->addr_callback (rh->cls,
702 NULL, 708 NULL,
703 0); 709 0);
704 GNUNET_free(rh); 710 GNUNET_free (rh);
705 return; 711 return;
706 } 712 }
707 /* why are we here? this task should not have been scheduled! */ 713 /* why are we here? this task should not have been scheduled! */
708 GNUNET_assert(0); 714 GNUNET_assert (0);
709 GNUNET_free(rh); 715 GNUNET_free (rh);
710} 716}
711 717
712 718
@@ -718,61 +724,61 @@ numeric_resolution(void *cls)
718 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request 724 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
719 */ 725 */
720static void 726static void
721loopback_resolution(void *cls) 727loopback_resolution (void *cls)
722{ 728{
723 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 729 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
724 struct sockaddr_in v4; 730 struct sockaddr_in v4;
725 struct sockaddr_in6 v6; 731 struct sockaddr_in6 v6;
726 732
727 rh->task = NULL; 733 rh->task = NULL;
728 memset(&v4, 0, sizeof(v4)); 734 memset (&v4, 0, sizeof(v4));
729 v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 735 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
730 v4.sin_family = AF_INET; 736 v4.sin_family = AF_INET;
731#if HAVE_SOCKADDR_IN_SIN_LEN 737#if HAVE_SOCKADDR_IN_SIN_LEN
732 v4.sin_len = sizeof(v4); 738 v4.sin_len = sizeof(v4);
733#endif 739#endif
734 memset(&v6, 0, sizeof(v6)); 740 memset (&v6, 0, sizeof(v6));
735 v6.sin6_family = AF_INET6; 741 v6.sin6_family = AF_INET6;
736#if HAVE_SOCKADDR_IN_SIN_LEN 742#if HAVE_SOCKADDR_IN_SIN_LEN
737 v6.sin6_len = sizeof(v6); 743 v6.sin6_len = sizeof(v6);
738#endif 744#endif
739 v6.sin6_addr = in6addr_loopback; 745 v6.sin6_addr = in6addr_loopback;
740 switch (rh->af) 746 switch (rh->af)
741 { 747 {
742 case AF_INET: 748 case AF_INET:
743 rh->addr_callback(rh->cls, 749 rh->addr_callback (rh->cls,
744 (const struct sockaddr *)&v4, 750 (const struct sockaddr *) &v4,
745 sizeof(v4)); 751 sizeof(v4));
746 break; 752 break;
747 753
748 case AF_INET6: 754 case AF_INET6:
749 rh->addr_callback(rh->cls, 755 rh->addr_callback (rh->cls,
750 (const struct sockaddr *)&v6, 756 (const struct sockaddr *) &v6,
751 sizeof(v6)); 757 sizeof(v6));
752 break; 758 break;
753 759
754 case AF_UNSPEC: 760 case AF_UNSPEC:
755 rh->addr_callback(rh->cls, 761 rh->addr_callback (rh->cls,
756 (const struct sockaddr *)&v6, 762 (const struct sockaddr *) &v6,
757 sizeof(v6)); 763 sizeof(v6));
758 rh->addr_callback(rh->cls, 764 rh->addr_callback (rh->cls,
759 (const struct sockaddr *)&v4, 765 (const struct sockaddr *) &v4,
760 sizeof(v4)); 766 sizeof(v4));
761 767
762 break; 768 break;
763 769
764 default: 770 default:
765 GNUNET_break(0); 771 GNUNET_break (0);
766 break; 772 break;
767 } 773 }
768 if (GNUNET_SYSERR != rh->was_transmitted) 774 if (GNUNET_SYSERR != rh->was_transmitted)
769 rh->addr_callback(rh->cls, 775 rh->addr_callback (rh->cls,
770 NULL, 776 NULL,
771 0); 777 0);
772 LOG(GNUNET_ERROR_TYPE_DEBUG, 778 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "Finished resolving hostname `%s'.\n", 779 "Finished resolving hostname `%s'.\n",
774 (const char *)&rh[1]); 780 (const char *) &rh[1]);
775 GNUNET_free(rh); 781 GNUNET_free (rh);
776} 782}
777 783
778 784
@@ -782,35 +788,35 @@ loopback_resolution(void *cls)
782 * @param cls NULL 788 * @param cls NULL
783 */ 789 */
784static void 790static void
785reconnect_task(void *cls) 791reconnect_task (void *cls)
786{ 792{
787 struct GNUNET_MQ_MessageHandler handlers[] = { 793 struct GNUNET_MQ_MessageHandler handlers[] = {
788 GNUNET_MQ_hd_var_size(response, 794 GNUNET_MQ_hd_var_size (response,
789 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE, 795 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
790 struct GNUNET_RESOLVER_ResponseMessage, 796 struct GNUNET_RESOLVER_ResponseMessage,
791 NULL), 797 NULL),
792 GNUNET_MQ_handler_end() 798 GNUNET_MQ_handler_end ()
793 }; 799 };
794 800
795 (void)cls; 801 (void) cls;
796 r_task = NULL; 802 r_task = NULL;
797 if (NULL == req_head) 803 if (NULL == req_head)
798 return; /* no work pending */ 804 return; /* no work pending */
799 LOG(GNUNET_ERROR_TYPE_DEBUG, 805 LOG (GNUNET_ERROR_TYPE_DEBUG,
800 "Trying to connect to DNS service\n"); 806 "Trying to connect to DNS service\n");
801 mq = GNUNET_CLIENT_connect(resolver_cfg, 807 mq = GNUNET_CLIENT_connect (resolver_cfg,
802 "resolver", 808 "resolver",
803 handlers, 809 handlers,
804 &mq_error_handler, 810 &mq_error_handler,
805 NULL); 811 NULL);
806 if (NULL == mq) 812 if (NULL == mq)
807 { 813 {
808 LOG(GNUNET_ERROR_TYPE_DEBUG, 814 LOG (GNUNET_ERROR_TYPE_DEBUG,
809 "Failed to connect, will try again later\n"); 815 "Failed to connect, will try again later\n");
810 reconnect(); 816 reconnect ();
811 return; 817 return;
812 } 818 }
813 process_requests(); 819 process_requests ();
814} 820}
815 821
816 822
@@ -818,49 +824,49 @@ reconnect_task(void *cls)
818 * Adjust exponential back-off and reconnect to the service. 824 * Adjust exponential back-off and reconnect to the service.
819 */ 825 */
820static void 826static void
821reconnect() 827reconnect ()
822{ 828{
823 struct GNUNET_RESOLVER_RequestHandle *rh; 829 struct GNUNET_RESOLVER_RequestHandle *rh;
824 830
825 if (NULL != r_task) 831 if (NULL != r_task)
826 return; 832 return;
827 GNUNET_assert(NULL == mq); 833 GNUNET_assert (NULL == mq);
828 if (NULL != (rh = req_head)) 834 if (NULL != (rh = req_head))
835 {
836 switch (rh->was_transmitted)
829 { 837 {
830 switch (rh->was_transmitted) 838 case GNUNET_NO:
831 { 839 /* nothing more to do */
832 case GNUNET_NO: 840 break;
833 /* nothing more to do */ 841
834 break; 842 case GNUNET_YES:
835 843 /* disconnected, transmit again! */
836 case GNUNET_YES: 844 rh->was_transmitted = GNUNET_NO;
837 /* disconnected, transmit again! */ 845 break;
838 rh->was_transmitted = GNUNET_NO; 846
839 break; 847 case GNUNET_SYSERR:
840 848 /* request was cancelled, remove entirely */
841 case GNUNET_SYSERR: 849 GNUNET_CONTAINER_DLL_remove (req_head,
842 /* request was cancelled, remove entirely */ 850 req_tail,
843 GNUNET_CONTAINER_DLL_remove(req_head, 851 rh);
844 req_tail, 852 GNUNET_free (rh);
845 rh); 853 check_disconnect ();
846 GNUNET_free(rh); 854 break;
847 check_disconnect(); 855
848 break; 856 default:
849 857 GNUNET_assert (0);
850 default: 858 break;
851 GNUNET_assert(0);
852 break;
853 }
854 } 859 }
855 LOG(GNUNET_ERROR_TYPE_DEBUG, 860 }
856 "Will try to connect to DNS service in %s\n", 861 LOG (GNUNET_ERROR_TYPE_DEBUG,
857 GNUNET_STRINGS_relative_time_to_string(backoff, 862 "Will try to connect to DNS service in %s\n",
858 GNUNET_YES)); 863 GNUNET_STRINGS_relative_time_to_string (backoff,
859 GNUNET_assert(NULL != resolver_cfg); 864 GNUNET_YES));
860 r_task = GNUNET_SCHEDULER_add_delayed(backoff, 865 GNUNET_assert (NULL != resolver_cfg);
861 &reconnect_task, 866 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
862 NULL); 867 &reconnect_task,
863 backoff = GNUNET_TIME_STD_BACKOFF(backoff); 868 NULL);
869 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
864} 870}
865 871
866 872
@@ -870,52 +876,52 @@ reconnect()
870 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *` 876 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
871 */ 877 */
872static void 878static void
873handle_lookup_timeout(void *cls) 879handle_lookup_timeout (void *cls)
874{ 880{
875 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 881 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
876 882
877 rh->task = NULL; 883 rh->task = NULL;
878 if (GNUNET_NO == rh->direction) 884 if (GNUNET_NO == rh->direction)
879 { 885 {
880 LOG(GNUNET_ERROR_TYPE_INFO, 886 LOG (GNUNET_ERROR_TYPE_INFO,
881 _("Timeout trying to resolve hostname `%s'.\n"), 887 _ ("Timeout trying to resolve hostname `%s'.\n"),
882 (const char *)&rh[1]); 888 (const char *) &rh[1]);
883 if (NULL != rh->addr_callback) 889 if (NULL != rh->addr_callback)
884 rh->addr_callback(rh->cls, 890 rh->addr_callback (rh->cls,
885 NULL, 891 NULL,
886 0); 892 0);
887 } 893 }
888 else 894 else
889 { 895 {
890#if !defined(GNUNET_CULL_LOGGING) 896#if ! defined(GNUNET_CULL_LOGGING)
891 char buf[INET6_ADDRSTRLEN]; 897 char buf[INET6_ADDRSTRLEN];
892 898
893 LOG(GNUNET_ERROR_TYPE_INFO, 899 LOG (GNUNET_ERROR_TYPE_INFO,
894 _("Timeout trying to resolve IP address `%s'.\n"), 900 _ ("Timeout trying to resolve IP address `%s'.\n"),
895 inet_ntop(rh->af, 901 inet_ntop (rh->af,
896 (const void *)&rh[1], 902 (const void *) &rh[1],
897 buf, 903 buf,
898 sizeof(buf))); 904 sizeof(buf)));
899#endif 905#endif
900 if (GNUNET_NO == rh->received_response) 906 if (GNUNET_NO == rh->received_response)
901 { 907 {
902 char *nret; 908 char *nret;
903 909
904 nret = no_resolve(rh->af, 910 nret = no_resolve (rh->af,
905 &rh[1], 911 &rh[1],
906 rh->data_len); 912 rh->data_len);
907 if (NULL != rh->name_callback)
908 rh->name_callback(rh->cls, nret);
909 GNUNET_free(nret);
910 }
911 /* finally, make termination call */
912 if (NULL != rh->name_callback) 913 if (NULL != rh->name_callback)
913 rh->name_callback(rh->cls, 914 rh->name_callback (rh->cls, nret);
914 NULL); 915 GNUNET_free (nret);
915 } 916 }
917 /* finally, make termination call */
918 if (NULL != rh->name_callback)
919 rh->name_callback (rh->cls,
920 NULL);
921 }
916 rh->was_transmitted = GNUNET_NO; 922 rh->was_transmitted = GNUNET_NO;
917 GNUNET_RESOLVER_request_cancel(rh); 923 GNUNET_RESOLVER_request_cancel (rh);
918 process_requests(); 924 process_requests ();
919} 925}
920 926
921 927
@@ -930,83 +936,83 @@ handle_lookup_timeout(void *cls)
930 * @return handle that can be used to cancel the request, NULL on error 936 * @return handle that can be used to cancel the request, NULL on error
931 */ 937 */
932struct GNUNET_RESOLVER_RequestHandle * 938struct GNUNET_RESOLVER_RequestHandle *
933GNUNET_RESOLVER_ip_get(const char *hostname, 939GNUNET_RESOLVER_ip_get (const char *hostname,
934 int af, 940 int af,
935 struct GNUNET_TIME_Relative timeout, 941 struct GNUNET_TIME_Relative timeout,
936 GNUNET_RESOLVER_AddressCallback callback, 942 GNUNET_RESOLVER_AddressCallback callback,
937 void *callback_cls) 943 void *callback_cls)
938{ 944{
939 struct GNUNET_RESOLVER_RequestHandle *rh; 945 struct GNUNET_RESOLVER_RequestHandle *rh;
940 size_t slen; 946 size_t slen;
941 struct in_addr v4; 947 struct in_addr v4;
942 struct in6_addr v6; 948 struct in6_addr v6;
943 949
944 slen = strlen(hostname) + 1; 950 slen = strlen (hostname) + 1;
945 if (slen + sizeof(struct GNUNET_RESOLVER_GetMessage) >= 951 if (slen + sizeof(struct GNUNET_RESOLVER_GetMessage) >=
946 GNUNET_MAX_MESSAGE_SIZE) 952 GNUNET_MAX_MESSAGE_SIZE)
947 { 953 {
948 GNUNET_break(0); 954 GNUNET_break (0);
949 return NULL; 955 return NULL;
950 } 956 }
951 LOG(GNUNET_ERROR_TYPE_DEBUG, 957 LOG (GNUNET_ERROR_TYPE_DEBUG,
952 "Trying to resolve hostname `%s'.\n", 958 "Trying to resolve hostname `%s'.\n",
953 hostname); 959 hostname);
954 rh = GNUNET_malloc(sizeof(struct GNUNET_RESOLVER_RequestHandle) + slen); 960 rh = GNUNET_malloc (sizeof(struct GNUNET_RESOLVER_RequestHandle) + slen);
955 rh->af = af; 961 rh->af = af;
956 rh->id = ++last_request_id; 962 rh->id = ++last_request_id;
957 rh->addr_callback = callback; 963 rh->addr_callback = callback;
958 rh->cls = callback_cls; 964 rh->cls = callback_cls;
959 GNUNET_memcpy(&rh[1], 965 GNUNET_memcpy (&rh[1],
960 hostname, 966 hostname,
961 slen); 967 slen);
962 rh->data_len = slen; 968 rh->data_len = slen;
963 rh->timeout = GNUNET_TIME_relative_to_absolute(timeout); 969 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
964 rh->direction = GNUNET_NO; 970 rh->direction = GNUNET_NO;
965 /* first, check if this is a numeric address */ 971 /* first, check if this is a numeric address */
966 if (((1 == inet_pton(AF_INET, 972 if (((1 == inet_pton (AF_INET,
967 hostname, 973 hostname,
968 &v4)) && 974 &v4)) &&
969 ((af == AF_INET) || 975 ((af == AF_INET) ||
970 (af == AF_UNSPEC))) || 976 (af == AF_UNSPEC))) ||
971 ((1 == inet_pton(AF_INET6, 977 ((1 == inet_pton (AF_INET6,
972 hostname, 978 hostname,
973 &v6)) && 979 &v6)) &&
974 ((af == AF_INET6) || 980 ((af == AF_INET6) ||
975 (af == AF_UNSPEC)))) 981 (af == AF_UNSPEC))))
976 { 982 {
977 rh->task = GNUNET_SCHEDULER_add_now(&numeric_resolution, 983 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
978 rh); 984 rh);
979 return rh; 985 return rh;
980 } 986 }
981 /* then, check if this is a loopback address */ 987 /* then, check if this is a loopback address */
982 for (unsigned int i = 0; 988 for (unsigned int i = 0;
983 NULL != loopback[i]; 989 NULL != loopback[i];
984 i++) 990 i++)
985 if (0 == strcasecmp(loopback[i], 991 if (0 == strcasecmp (loopback[i],
986 hostname)) 992 hostname))
987 {
988 rh->task = GNUNET_SCHEDULER_add_now(&loopback_resolution,
989 rh);
990 return rh;
991 }
992 if (GNUNET_OK != check_config())
993 { 993 {
994 GNUNET_free(rh); 994 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
995 return NULL; 995 rh);
996 return rh;
996 } 997 }
997 rh->task = GNUNET_SCHEDULER_add_delayed(timeout, 998 if (GNUNET_OK != check_config ())
998 &handle_lookup_timeout, 999 {
999 rh); 1000 GNUNET_free (rh);
1000 GNUNET_CONTAINER_DLL_insert_tail(req_head, 1001 return NULL;
1001 req_tail, 1002 }
1002 rh); 1003 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1004 &handle_lookup_timeout,
1005 rh);
1006 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1007 req_tail,
1008 rh);
1003 rh->was_queued = GNUNET_YES; 1009 rh->was_queued = GNUNET_YES;
1004 if (NULL != s_task) 1010 if (NULL != s_task)
1005 { 1011 {
1006 GNUNET_SCHEDULER_cancel(s_task); 1012 GNUNET_SCHEDULER_cancel (s_task);
1007 s_task = NULL; 1013 s_task = NULL;
1008 } 1014 }
1009 process_requests(); 1015 process_requests ();
1010 return rh; 1016 return rh;
1011} 1017}
1012 1018
@@ -1020,32 +1026,32 @@ GNUNET_RESOLVER_ip_get(const char *hostname,
1020 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request 1026 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
1021 */ 1027 */
1022static void 1028static void
1023numeric_reverse(void *cls) 1029numeric_reverse (void *cls)
1024{ 1030{
1025 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 1031 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
1026 char *result; 1032 char *result;
1027 1033
1028 rh->task = NULL; 1034 rh->task = NULL;
1029 result = no_resolve(rh->af, 1035 result = no_resolve (rh->af,
1030 &rh[1], 1036 &rh[1],
1031 rh->data_len); 1037 rh->data_len);
1032 LOG(GNUNET_ERROR_TYPE_DEBUG, 1038 LOG (GNUNET_ERROR_TYPE_DEBUG,
1033 "Resolver returns `%s'.\n", 1039 "Resolver returns `%s'.\n",
1034 result); 1040 result);
1035 if (NULL != result) 1041 if (NULL != result)
1036 { 1042 {
1037 rh->name_callback(rh->cls, 1043 rh->name_callback (rh->cls,
1038 result); 1044 result);
1039 GNUNET_free(result); 1045 GNUNET_free (result);
1040 } 1046 }
1041 rh->name_callback(rh->cls, 1047 rh->name_callback (rh->cls,
1042 NULL); 1048 NULL);
1043 if (NULL != rh->task) 1049 if (NULL != rh->task)
1044 { 1050 {
1045 GNUNET_SCHEDULER_cancel(rh->task); 1051 GNUNET_SCHEDULER_cancel (rh->task);
1046 rh->task = NULL; 1052 rh->task = NULL;
1047 } 1053 }
1048 GNUNET_free(rh); 1054 GNUNET_free (rh);
1049} 1055}
1050 1056
1051 1057
@@ -1062,73 +1068,73 @@ numeric_reverse(void *cls)
1062 * @return handle that can be used to cancel the request 1068 * @return handle that can be used to cancel the request
1063 */ 1069 */
1064struct GNUNET_RESOLVER_RequestHandle * 1070struct GNUNET_RESOLVER_RequestHandle *
1065GNUNET_RESOLVER_hostname_get(const struct sockaddr *sa, 1071GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1066 socklen_t salen, 1072 socklen_t salen,
1067 int do_resolve, 1073 int do_resolve,
1068 struct GNUNET_TIME_Relative timeout, 1074 struct GNUNET_TIME_Relative timeout,
1069 GNUNET_RESOLVER_HostnameCallback callback, 1075 GNUNET_RESOLVER_HostnameCallback callback,
1070 void *cls) 1076 void *cls)
1071{ 1077{
1072 struct GNUNET_RESOLVER_RequestHandle *rh; 1078 struct GNUNET_RESOLVER_RequestHandle *rh;
1073 size_t ip_len; 1079 size_t ip_len;
1074 const void *ip; 1080 const void *ip;
1075 1081
1076 if (GNUNET_OK != check_config()) 1082 if (GNUNET_OK != check_config ())
1077 { 1083 {
1078 LOG(GNUNET_ERROR_TYPE_ERROR, 1084 LOG (GNUNET_ERROR_TYPE_ERROR,
1079 _("Resolver not configured correctly.\n")); 1085 _ ("Resolver not configured correctly.\n"));
1080 return NULL; 1086 return NULL;
1081 } 1087 }
1082 1088
1083 switch (sa->sa_family) 1089 switch (sa->sa_family)
1084 { 1090 {
1085 case AF_INET: 1091 case AF_INET:
1086 GNUNET_assert(salen == sizeof(struct sockaddr_in)); 1092 GNUNET_assert (salen == sizeof(struct sockaddr_in));
1087 ip_len = sizeof(struct in_addr); 1093 ip_len = sizeof(struct in_addr);
1088 ip = &((const struct sockaddr_in*)sa)->sin_addr; 1094 ip = &((const struct sockaddr_in*) sa)->sin_addr;
1089 break; 1095 break;
1090 1096
1091 case AF_INET6: 1097 case AF_INET6:
1092 GNUNET_assert(salen == sizeof(struct sockaddr_in6)); 1098 GNUNET_assert (salen == sizeof(struct sockaddr_in6));
1093 ip_len = sizeof(struct in6_addr); 1099 ip_len = sizeof(struct in6_addr);
1094 ip = &((const struct sockaddr_in6*)sa)->sin6_addr; 1100 ip = &((const struct sockaddr_in6*) sa)->sin6_addr;
1095 break; 1101 break;
1096 1102
1097 default: 1103 default:
1098 GNUNET_break(0); 1104 GNUNET_break (0);
1099 return NULL; 1105 return NULL;
1100 } 1106 }
1101 rh = GNUNET_malloc(sizeof(struct GNUNET_RESOLVER_RequestHandle) + salen); 1107 rh = GNUNET_malloc (sizeof(struct GNUNET_RESOLVER_RequestHandle) + salen);
1102 rh->name_callback = callback; 1108 rh->name_callback = callback;
1103 rh->cls = cls; 1109 rh->cls = cls;
1104 rh->af = sa->sa_family; 1110 rh->af = sa->sa_family;
1105 rh->id = ++last_request_id; 1111 rh->id = ++last_request_id;
1106 rh->timeout = GNUNET_TIME_relative_to_absolute(timeout); 1112 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1107 GNUNET_memcpy(&rh[1], 1113 GNUNET_memcpy (&rh[1],
1108 ip, 1114 ip,
1109 ip_len); 1115 ip_len);
1110 rh->data_len = ip_len; 1116 rh->data_len = ip_len;
1111 rh->direction = GNUNET_YES; 1117 rh->direction = GNUNET_YES;
1112 rh->received_response = GNUNET_NO; 1118 rh->received_response = GNUNET_NO;
1113 if (GNUNET_NO == do_resolve) 1119 if (GNUNET_NO == do_resolve)
1114 { 1120 {
1115 rh->task = GNUNET_SCHEDULER_add_now(&numeric_reverse, 1121 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1116 rh); 1122 rh);
1117 return rh; 1123 return rh;
1118 } 1124 }
1119 rh->task = GNUNET_SCHEDULER_add_delayed(timeout, 1125 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1120 &handle_lookup_timeout, 1126 &handle_lookup_timeout,
1121 rh); 1127 rh);
1122 GNUNET_CONTAINER_DLL_insert_tail(req_head, 1128 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1123 req_tail, 1129 req_tail,
1124 rh); 1130 rh);
1125 rh->was_queued = GNUNET_YES; 1131 rh->was_queued = GNUNET_YES;
1126 if (NULL != s_task) 1132 if (NULL != s_task)
1127 { 1133 {
1128 GNUNET_SCHEDULER_cancel(s_task); 1134 GNUNET_SCHEDULER_cancel (s_task);
1129 s_task = NULL; 1135 s_task = NULL;
1130 } 1136 }
1131 process_requests(); 1137 process_requests ();
1132 return rh; 1138 return rh;
1133} 1139}
1134 1140
@@ -1139,78 +1145,78 @@ GNUNET_RESOLVER_hostname_get(const struct sockaddr *sa,
1139 * @return fqdn 1145 * @return fqdn
1140 */ 1146 */
1141char * 1147char *
1142GNUNET_RESOLVER_local_fqdn_get() 1148GNUNET_RESOLVER_local_fqdn_get ()
1143{ 1149{
1144 char hostname[GNUNET_OS_get_hostname_max_length() + 1]; 1150 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1145 1151
1146 if (0 != gethostname(hostname, 1152 if (0 != gethostname (hostname,
1147 sizeof(hostname) - 1)) 1153 sizeof(hostname) - 1))
1148 { 1154 {
1149 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1155 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1150 "gethostname"); 1156 "gethostname");
1151 return NULL; 1157 return NULL;
1152 } 1158 }
1153 LOG(GNUNET_ERROR_TYPE_DEBUG, 1159 LOG (GNUNET_ERROR_TYPE_DEBUG,
1154 "Resolving our FQDN `%s'\n", 1160 "Resolving our FQDN `%s'\n",
1155 hostname); 1161 hostname);
1156#if HAVE_GETADDRINFO 1162#if HAVE_GETADDRINFO
1157 { 1163 {
1158 struct addrinfo *ai; 1164 struct addrinfo *ai;
1159 int ret; 1165 int ret;
1160 char *rval; 1166 char *rval;
1161 1167
1162 if (0 != (ret = getaddrinfo(hostname, 1168 if (0 != (ret = getaddrinfo (hostname,
1163 NULL, 1169 NULL,
1164 NULL, 1170 NULL,
1165 &ai))) 1171 &ai)))
1166 { 1172 {
1167 LOG(GNUNET_ERROR_TYPE_ERROR, 1173 LOG (GNUNET_ERROR_TYPE_ERROR,
1168 _("Could not resolve our FQDN: %s\n"), 1174 _ ("Could not resolve our FQDN: %s\n"),
1169 gai_strerror(ret)); 1175 gai_strerror (ret));
1170 return NULL; 1176 return NULL;
1171 } 1177 }
1172 if (NULL != ai->ai_canonname) 1178 if (NULL != ai->ai_canonname)
1173 rval = GNUNET_strdup(ai->ai_canonname); 1179 rval = GNUNET_strdup (ai->ai_canonname);
1174 else 1180 else
1175 rval = GNUNET_strdup(hostname); 1181 rval = GNUNET_strdup (hostname);
1176 freeaddrinfo(ai); 1182 freeaddrinfo (ai);
1177 return rval; 1183 return rval;
1178 } 1184 }
1179#elif HAVE_GETHOSTBYNAME2 1185#elif HAVE_GETHOSTBYNAME2
1180 { 1186 {
1181 struct hostent *host; 1187 struct hostent *host;
1182 1188
1183 host = gethostbyname2(hostname, 1189 host = gethostbyname2 (hostname,
1184 AF_INET); 1190 AF_INET);
1185 if (NULL == host) 1191 if (NULL == host)
1186 host = gethostbyname2(hostname, 1192 host = gethostbyname2 (hostname,
1187 AF_INET6); 1193 AF_INET6);
1188 if (NULL == host) 1194 if (NULL == host)
1189 { 1195 {
1190 LOG(GNUNET_ERROR_TYPE_ERROR, 1196 LOG (GNUNET_ERROR_TYPE_ERROR,
1191 _("Could not resolve our FQDN: %s\n"), 1197 _ ("Could not resolve our FQDN: %s\n"),
1192 hstrerror(h_errno)); 1198 hstrerror (h_errno));
1193 return NULL; 1199 return NULL;
1194 } 1200 }
1195 return GNUNET_strdup(host->h_name); 1201 return GNUNET_strdup (host->h_name);
1196 } 1202 }
1197#elif HAVE_GETHOSTBYNAME 1203#elif HAVE_GETHOSTBYNAME
1198 { 1204 {
1199 struct hostent *host; 1205 struct hostent *host;
1200 1206
1201 host = gethostbyname(hostname); 1207 host = gethostbyname (hostname);
1202 if (NULL == host) 1208 if (NULL == host)
1203 { 1209 {
1204 LOG(GNUNET_ERROR_TYPE_ERROR, 1210 LOG (GNUNET_ERROR_TYPE_ERROR,
1205 _("Could not resolve our FQDN: %s\n"), 1211 _ ("Could not resolve our FQDN: %s\n"),
1206 hstrerror(h_errno)); 1212 hstrerror (h_errno));
1207 return NULL; 1213 return NULL;
1208 } 1214 }
1209 return GNUNET_strdup(host->h_name); 1215 return GNUNET_strdup (host->h_name);
1210 } 1216 }
1211#else 1217#else
1212 /* fallback: just hope name is already FQDN */ 1218 /* fallback: just hope name is already FQDN */
1213 return GNUNET_strdup(hostname); 1219 return GNUNET_strdup (hostname);
1214#endif 1220#endif
1215} 1221}
1216 1222
@@ -1225,27 +1231,27 @@ GNUNET_RESOLVER_local_fqdn_get()
1225 * @return handle that can be used to cancel the request, NULL on error 1231 * @return handle that can be used to cancel the request, NULL on error
1226 */ 1232 */
1227struct GNUNET_RESOLVER_RequestHandle * 1233struct GNUNET_RESOLVER_RequestHandle *
1228GNUNET_RESOLVER_hostname_resolve(int af, 1234GNUNET_RESOLVER_hostname_resolve (int af,
1229 struct GNUNET_TIME_Relative timeout, 1235 struct GNUNET_TIME_Relative timeout,
1230 GNUNET_RESOLVER_AddressCallback callback, 1236 GNUNET_RESOLVER_AddressCallback callback,
1231 void *cls) 1237 void *cls)
1232{ 1238{
1233 char hostname[GNUNET_OS_get_hostname_max_length() + 1]; 1239 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1234 1240
1235 if (0 != gethostname(hostname, sizeof(hostname) - 1)) 1241 if (0 != gethostname (hostname, sizeof(hostname) - 1))
1236 { 1242 {
1237 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1243 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1238 "gethostname"); 1244 "gethostname");
1239 return NULL; 1245 return NULL;
1240 } 1246 }
1241 LOG(GNUNET_ERROR_TYPE_DEBUG, 1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1242 "Resolving our hostname `%s'\n", 1248 "Resolving our hostname `%s'\n",
1243 hostname); 1249 hostname);
1244 return GNUNET_RESOLVER_ip_get(hostname, 1250 return GNUNET_RESOLVER_ip_get (hostname,
1245 af, 1251 af,
1246 timeout, 1252 timeout,
1247 callback, 1253 callback,
1248 cls); 1254 cls);
1249} 1255}
1250 1256
1251 1257
@@ -1258,30 +1264,30 @@ GNUNET_RESOLVER_hostname_resolve(int af,
1258 * @param rh handle of request to cancel 1264 * @param rh handle of request to cancel
1259 */ 1265 */
1260void 1266void
1261GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh) 1267GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1262{ 1268{
1263 if (GNUNET_NO == rh->direction) 1269 if (GNUNET_NO == rh->direction)
1264 LOG(GNUNET_ERROR_TYPE_DEBUG, 1270 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "Asked to cancel request to resolve hostname `%s'.\n", 1271 "Asked to cancel request to resolve hostname `%s'.\n",
1266 (const char *)&rh[1]); 1272 (const char *) &rh[1]);
1267 if (NULL != rh->task) 1273 if (NULL != rh->task)
1268 { 1274 {
1269 GNUNET_SCHEDULER_cancel(rh->task); 1275 GNUNET_SCHEDULER_cancel (rh->task);
1270 rh->task = NULL; 1276 rh->task = NULL;
1271 } 1277 }
1272 if (GNUNET_NO == rh->was_transmitted) 1278 if (GNUNET_NO == rh->was_transmitted)
1273 { 1279 {
1274 if (GNUNET_YES == rh->was_queued) 1280 if (GNUNET_YES == rh->was_queued)
1275 GNUNET_CONTAINER_DLL_remove(req_head, 1281 GNUNET_CONTAINER_DLL_remove (req_head,
1276 req_tail, 1282 req_tail,
1277 rh); 1283 rh);
1278 GNUNET_free(rh); 1284 GNUNET_free (rh);
1279 check_disconnect(); 1285 check_disconnect ();
1280 return; 1286 return;
1281 } 1287 }
1282 GNUNET_assert(GNUNET_YES == rh->was_transmitted); 1288 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1283 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */ 1289 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1284 check_disconnect(); 1290 check_disconnect ();
1285} 1291}
1286 1292
1287 1293
diff --git a/src/util/scheduler.c b/src/util/scheduler.c
index f379bce42..89dba3799 100644
--- a/src/util/scheduler.c
+++ b/src/util/scheduler.c
@@ -28,9 +28,11 @@
28// DEBUG 28// DEBUG
29#include <inttypes.h> 29#include <inttypes.h>
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-scheduler", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
32 32
33#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror(kind, "util-scheduler", syscall) 33#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
34 "util-scheduler", \
35 syscall)
34 36
35 37
36#if HAVE_EXECINFO_H 38#if HAVE_EXECINFO_H
@@ -76,7 +78,8 @@
76 * #GNUNET_SCHEDULER_do_work(). Contains the 78 * #GNUNET_SCHEDULER_do_work(). Contains the
77 * scheduler's internal state. 79 * scheduler's internal state.
78 */ 80 */
79struct GNUNET_SCHEDULER_Handle { 81struct GNUNET_SCHEDULER_Handle
82{
80 /** 83 /**
81 * Passed here to avoid constantly allocating/deallocating 84 * Passed here to avoid constantly allocating/deallocating
82 * this element, but generally we want to get rid of this. 85 * this element, but generally we want to get rid of this.
@@ -128,7 +131,8 @@ struct GNUNET_SCHEDULER_Handle {
128/** 131/**
129 * Entry in list of pending tasks. 132 * Entry in list of pending tasks.
130 */ 133 */
131struct GNUNET_SCHEDULER_Task { 134struct GNUNET_SCHEDULER_Task
135{
132 /** 136 /**
133 * This is a linked list. 137 * This is a linked list.
134 */ 138 */
@@ -247,7 +251,8 @@ struct GNUNET_SCHEDULER_Task {
247/** 251/**
248 * A struct representing an event the select driver is waiting for 252 * A struct representing an event the select driver is waiting for
249 */ 253 */
250struct Scheduled { 254struct Scheduled
255{
251 struct Scheduled *prev; 256 struct Scheduled *prev;
252 257
253 struct Scheduled *next; 258 struct Scheduled *next;
@@ -274,7 +279,8 @@ struct Scheduled {
274/** 279/**
275 * Driver context used by GNUNET_SCHEDULER_run 280 * Driver context used by GNUNET_SCHEDULER_run
276 */ 281 */
277struct DriverContext { 282struct DriverContext
283{
278 /** 284 /**
279 * the head of a DLL containing information about the events the 285 * the head of a DLL containing information about the events the
280 * select driver is waiting for 286 * select driver is waiting for
@@ -356,12 +362,14 @@ static struct GNUNET_SCHEDULER_Task *active_task;
356/** 362/**
357 * Head of list of tasks ready to run right now, grouped by importance. 363 * Head of list of tasks ready to run right now, grouped by importance.
358 */ 364 */
359static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT]; 365static struct
366GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
360 367
361/** 368/**
362 * Tail of list of tasks ready to run right now, grouped by importance. 369 * Tail of list of tasks ready to run right now, grouped by importance.
363 */ 370 */
364static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT]; 371static struct
372GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
365 373
366/** 374/**
367 * Task for installing parent control handlers (it might happen that the 375 * Task for installing parent control handlers (it might happen that the
@@ -423,8 +431,8 @@ static void *scheduler_select_cls;
423 * @return previously used select function, NULL for default 431 * @return previously used select function, NULL for default
424 */ 432 */
425void 433void
426GNUNET_SCHEDULER_set_select(GNUNET_SCHEDULER_select new_select, 434GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
427 void *new_select_cls) 435 void *new_select_cls)
428{ 436{
429 scheduler_select = new_select; 437 scheduler_select = new_select;
430 scheduler_select_cls = new_select_cls; 438 scheduler_select_cls = new_select_cls;
@@ -438,11 +446,11 @@ GNUNET_SCHEDULER_set_select(GNUNET_SCHEDULER_select new_select,
438 * @return p on success, 0 on error 446 * @return p on success, 0 on error
439 */ 447 */
440static enum GNUNET_SCHEDULER_Priority 448static enum GNUNET_SCHEDULER_Priority
441check_priority(enum GNUNET_SCHEDULER_Priority p) 449check_priority (enum GNUNET_SCHEDULER_Priority p)
442{ 450{
443 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT)) 451 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
444 return p; 452 return p;
445 GNUNET_assert(0); 453 GNUNET_assert (0);
446 return 0; /* make compiler happy */ 454 return 0; /* make compiler happy */
447} 455}
448 456
@@ -453,38 +461,39 @@ check_priority(enum GNUNET_SCHEDULER_Priority p)
453 * callback) 461 * callback)
454 */ 462 */
455struct GNUNET_TIME_Absolute 463struct GNUNET_TIME_Absolute
456get_timeout() 464get_timeout ()
457{ 465{
458 struct GNUNET_SCHEDULER_Task *pos; 466 struct GNUNET_SCHEDULER_Task *pos;
459 struct GNUNET_TIME_Absolute now; 467 struct GNUNET_TIME_Absolute now;
460 struct GNUNET_TIME_Absolute timeout; 468 struct GNUNET_TIME_Absolute timeout;
461 469
462 pos = pending_timeout_head; 470 pos = pending_timeout_head;
463 now = GNUNET_TIME_absolute_get(); 471 now = GNUNET_TIME_absolute_get ();
464 timeout = GNUNET_TIME_UNIT_FOREVER_ABS; 472 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
465 if (NULL != pos) 473 if (NULL != pos)
474 {
475 if (0 != pos->reason)
466 { 476 {
467 if (0 != pos->reason) 477 return now;
468 { 478 }
469 return now; 479 else
470 } 480 {
471 else 481 timeout = pos->timeout;
472 {
473 timeout = pos->timeout;
474 }
475 } 482 }
483 }
476 for (pos = pending_head; NULL != pos; pos = pos->next) 484 for (pos = pending_head; NULL != pos; pos = pos->next)
485 {
486 if (0 != pos->reason)
477 { 487 {
478 if (0 != pos->reason) 488 return now;
479 { 489 }
480 return now; 490 else if ((pos->timeout.abs_value_us !=
481 } 491 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
482 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) && 492 (timeout.abs_value_us > pos->timeout.abs_value_us))
483 (timeout.abs_value_us > pos->timeout.abs_value_us)) 493 {
484 { 494 timeout = pos->timeout;
485 timeout = pos->timeout;
486 }
487 } 495 }
496 }
488 return timeout; 497 return timeout;
489} 498}
490 499
@@ -495,13 +504,13 @@ get_timeout()
495 * @param task task ready for execution 504 * @param task task ready for execution
496 */ 505 */
497static void 506static void
498queue_ready_task(struct GNUNET_SCHEDULER_Task *task) 507queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
499{ 508{
500 enum GNUNET_SCHEDULER_Priority p = check_priority(task->priority); 509 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
501 510
502 GNUNET_CONTAINER_DLL_insert(ready_head[p], 511 GNUNET_CONTAINER_DLL_insert (ready_head[p],
503 ready_tail[p], 512 ready_tail[p],
504 task); 513 task);
505 task->in_ready_list = GNUNET_YES; 514 task->in_ready_list = GNUNET_YES;
506 ready_count++; 515 ready_count++;
507} 516}
@@ -514,30 +523,30 @@ queue_ready_task(struct GNUNET_SCHEDULER_Task *task)
514 * will be delayed until the next shutdown signal. 523 * will be delayed until the next shutdown signal.
515 */ 524 */
516void 525void
517GNUNET_SCHEDULER_shutdown() 526GNUNET_SCHEDULER_shutdown ()
518{ 527{
519 struct GNUNET_SCHEDULER_Task *pos; 528 struct GNUNET_SCHEDULER_Task *pos;
520 529
521 LOG(GNUNET_ERROR_TYPE_DEBUG, 530 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "GNUNET_SCHEDULER_shutdown\n"); 531 "GNUNET_SCHEDULER_shutdown\n");
523 if (NULL != install_parent_control_task) 532 if (NULL != install_parent_control_task)
524 { 533 {
525 GNUNET_SCHEDULER_cancel(install_parent_control_task); 534 GNUNET_SCHEDULER_cancel (install_parent_control_task);
526 install_parent_control_task = NULL; 535 install_parent_control_task = NULL;
527 } 536 }
528 if (NULL != shutdown_pipe_task) 537 if (NULL != shutdown_pipe_task)
529 { 538 {
530 GNUNET_SCHEDULER_cancel(shutdown_pipe_task); 539 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
531 shutdown_pipe_task = NULL; 540 shutdown_pipe_task = NULL;
532 } 541 }
533 while (NULL != (pos = shutdown_head)) 542 while (NULL != (pos = shutdown_head))
534 { 543 {
535 GNUNET_CONTAINER_DLL_remove(shutdown_head, 544 GNUNET_CONTAINER_DLL_remove (shutdown_head,
536 shutdown_tail, 545 shutdown_tail,
537 pos); 546 pos);
538 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; 547 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
539 queue_ready_task(pos); 548 queue_ready_task (pos);
540 } 549 }
541} 550}
542 551
543 552
@@ -547,17 +556,17 @@ GNUNET_SCHEDULER_shutdown()
547 * @param t task to dump stack trace of 556 * @param t task to dump stack trace of
548 */ 557 */
549static void 558static void
550dump_backtrace(struct GNUNET_SCHEDULER_Task *t) 559dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
551{ 560{
552#if EXECINFO 561#if EXECINFO
553 for (unsigned int i = 0; i < t->num_backtrace_strings; i++) 562 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
554 LOG(GNUNET_ERROR_TYPE_WARNING, 563 LOG (GNUNET_ERROR_TYPE_WARNING,
555 "Task %p trace %u: %s\n", 564 "Task %p trace %u: %s\n",
556 t, 565 t,
557 i, 566 i,
558 t->backtrace_strings[i]); 567 t->backtrace_strings[i]);
559#else 568#else
560 (void)t; 569 (void) t;
561#endif 570#endif
562} 571}
563 572
@@ -568,40 +577,41 @@ dump_backtrace(struct GNUNET_SCHEDULER_Task *t)
568 * @param t task to destroy 577 * @param t task to destroy
569 */ 578 */
570static void 579static void
571destroy_task(struct GNUNET_SCHEDULER_Task *t) 580destroy_task (struct GNUNET_SCHEDULER_Task *t)
572{ 581{
573 unsigned int i; 582 unsigned int i;
574 583
575 LOG(GNUNET_ERROR_TYPE_DEBUG, 584 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "destroying task %p\n", 585 "destroying task %p\n",
577 t); 586 t);
578 587
579 if (GNUNET_YES == t->own_handles) 588 if (GNUNET_YES == t->own_handles)
589 {
590 for (i = 0; i != t->fds_len; ++i)
580 { 591 {
581 for (i = 0; i != t->fds_len; ++i) 592 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
582 { 593 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
583 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd; 594 if (fd)
584 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh; 595 {
585 if (fd) 596 GNUNET_NETWORK_socket_free_memory_only_ ((struct
586 { 597 GNUNET_NETWORK_Handle *) fd);
587 GNUNET_NETWORK_socket_free_memory_only_((struct GNUNET_NETWORK_Handle *)fd); 598 }
588 } 599 if (fh)
589 if (fh) 600 {
590 { 601 // FIXME: on WIN32 this is not enough! A function
591 // FIXME: on WIN32 this is not enough! A function 602 // GNUNET_DISK_file_free_memory_only would be nice
592 // GNUNET_DISK_file_free_memory_only would be nice 603 GNUNET_free ((void *) fh);
593 GNUNET_free((void *)fh); 604 }
594 }
595 }
596 } 605 }
606 }
597 if (t->fds_len > 1) 607 if (t->fds_len > 1)
598 { 608 {
599 GNUNET_array_grow(t->fds, t->fds_len, 0); 609 GNUNET_array_grow (t->fds, t->fds_len, 0);
600 } 610 }
601#if EXECINFO 611#if EXECINFO
602 GNUNET_free(t->backtrace_strings); 612 GNUNET_free (t->backtrace_strings);
603#endif 613#endif
604 GNUNET_free(t); 614 GNUNET_free (t);
605} 615}
606 616
607 617
@@ -620,7 +630,7 @@ static pid_t my_pid;
620 * Signal handler called for SIGPIPE. 630 * Signal handler called for SIGPIPE.
621 */ 631 */
622static void 632static void
623sighandler_pipe() 633sighandler_pipe ()
624{ 634{
625 return; 635 return;
626} 636}
@@ -634,37 +644,37 @@ sighandler_pipe()
634// * 644// *
635// * @param ms how many ms to wait 645// * @param ms how many ms to wait
636// */ 646// */
637//static void 647// static void
638//short_wait (unsigned int ms) 648// short_wait (unsigned int ms)
639//{ 649// {
640// struct GNUNET_TIME_Relative timeout; 650// struct GNUNET_TIME_Relative timeout;
641// 651//
642// timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms); 652// timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
643// (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout); 653// (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
644//} 654// }
645 655
646 656
647/** 657/**
648 * Signal handler called for signals that should cause us to shutdown. 658 * Signal handler called for signals that should cause us to shutdown.
649 */ 659 */
650static void 660static void
651sighandler_shutdown() 661sighandler_shutdown ()
652{ 662{
653 static char c; 663 static char c;
654 int old_errno = errno; /* backup errno */ 664 int old_errno = errno; /* backup errno */
655 665
656 if (getpid() != my_pid) 666 if (getpid () != my_pid)
657 _exit(1); /* we have fork'ed since the signal handler was created, 667 _exit (1); /* we have fork'ed since the signal handler was created,
658 * ignore the signal, see https://gnunet.org/vfork discussion */ 668 * ignore the signal, see https://gnunet.org/vfork discussion */
659 GNUNET_DISK_file_write(GNUNET_DISK_pipe_handle 669 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
660 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), 670 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
661 &c, sizeof(c)); 671 &c, sizeof(c));
662 errno = old_errno; 672 errno = old_errno;
663} 673}
664 674
665 675
666static void 676static void
667shutdown_if_no_lifeness() 677shutdown_if_no_lifeness ()
668{ 678{
669 struct GNUNET_SCHEDULER_Task *t; 679 struct GNUNET_SCHEDULER_Task *t;
670 680
@@ -680,13 +690,13 @@ shutdown_if_no_lifeness()
680 if (GNUNET_YES == t->lifeness) 690 if (GNUNET_YES == t->lifeness)
681 return; 691 return;
682 /* No lifeness! */ 692 /* No lifeness! */
683 GNUNET_SCHEDULER_shutdown(); 693 GNUNET_SCHEDULER_shutdown ();
684} 694}
685 695
686 696
687static int 697static int
688select_loop(struct GNUNET_SCHEDULER_Handle *sh, 698select_loop (struct GNUNET_SCHEDULER_Handle *sh,
689 struct DriverContext *context); 699 struct DriverContext *context);
690 700
691 701
692/** 702/**
@@ -704,26 +714,26 @@ select_loop(struct GNUNET_SCHEDULER_Handle *sh,
704 * @param task_cls closure of @a task 714 * @param task_cls closure of @a task
705 */ 715 */
706void 716void
707GNUNET_SCHEDULER_run(GNUNET_SCHEDULER_TaskCallback task, 717GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
708 void *task_cls) 718 void *task_cls)
709{ 719{
710 struct GNUNET_SCHEDULER_Handle *sh; 720 struct GNUNET_SCHEDULER_Handle *sh;
711 struct GNUNET_SCHEDULER_Driver *driver; 721 struct GNUNET_SCHEDULER_Driver *driver;
712 struct DriverContext context = { .scheduled_head = NULL, 722 struct DriverContext context = { .scheduled_head = NULL,
713 .scheduled_tail = NULL, 723 .scheduled_tail = NULL,
714 .timeout = GNUNET_TIME_absolute_get() }; 724 .timeout = GNUNET_TIME_absolute_get () };
715 725
716 driver = GNUNET_SCHEDULER_driver_select(); 726 driver = GNUNET_SCHEDULER_driver_select ();
717 driver->cls = &context; 727 driver->cls = &context;
718 sh = GNUNET_SCHEDULER_driver_init(driver); 728 sh = GNUNET_SCHEDULER_driver_init (driver);
719 GNUNET_SCHEDULER_add_with_reason_and_priority(task, 729 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
720 task_cls, 730 task_cls,
721 GNUNET_SCHEDULER_REASON_STARTUP, 731 GNUNET_SCHEDULER_REASON_STARTUP,
722 GNUNET_SCHEDULER_PRIORITY_DEFAULT); 732 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
723 select_loop(sh, 733 select_loop (sh,
724 &context); 734 &context);
725 GNUNET_SCHEDULER_driver_done(sh); 735 GNUNET_SCHEDULER_driver_done (sh);
726 GNUNET_free(driver); 736 GNUNET_free (driver);
727} 737}
728 738
729 739
@@ -734,9 +744,9 @@ GNUNET_SCHEDULER_run(GNUNET_SCHEDULER_TaskCallback task,
734 * @return current tasks' scheduler context 744 * @return current tasks' scheduler context
735 */ 745 */
736const struct GNUNET_SCHEDULER_TaskContext * 746const struct GNUNET_SCHEDULER_TaskContext *
737GNUNET_SCHEDULER_get_task_context() 747GNUNET_SCHEDULER_get_task_context ()
738{ 748{
739 GNUNET_assert(NULL != active_task); 749 GNUNET_assert (NULL != active_task);
740 return &tc; 750 return &tc;
741} 751}
742 752
@@ -751,33 +761,33 @@ GNUNET_SCHEDULER_get_task_context()
751 * @return number of tasks pending right now 761 * @return number of tasks pending right now
752 */ 762 */
753unsigned int 763unsigned int
754GNUNET_SCHEDULER_get_load(enum GNUNET_SCHEDULER_Priority p) 764GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
755{ 765{
756 struct GNUNET_SCHEDULER_Task *pos; 766 struct GNUNET_SCHEDULER_Task *pos;
757 unsigned int ret; 767 unsigned int ret;
758 768
759 GNUNET_assert(NULL != active_task); 769 GNUNET_assert (NULL != active_task);
760 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT) 770 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
761 return ready_count; 771 return ready_count;
762 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP) 772 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
763 p = current_priority; 773 p = current_priority;
764 ret = 0; 774 ret = 0;
765 for (pos = ready_head[check_priority(p)]; NULL != pos; pos = pos->next) 775 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
766 ret++; 776 ret++;
767 return ret; 777 return ret;
768} 778}
769 779
770 780
771void 781void
772init_fd_info(struct GNUNET_SCHEDULER_Task *t, 782init_fd_info (struct GNUNET_SCHEDULER_Task *t,
773 const struct GNUNET_NETWORK_Handle *const *read_nh, 783 const struct GNUNET_NETWORK_Handle *const *read_nh,
774 unsigned int read_nh_len, 784 unsigned int read_nh_len,
775 const struct GNUNET_NETWORK_Handle *const *write_nh, 785 const struct GNUNET_NETWORK_Handle *const *write_nh,
776 unsigned int write_nh_len, 786 unsigned int write_nh_len,
777 const struct GNUNET_DISK_FileHandle *const *read_fh, 787 const struct GNUNET_DISK_FileHandle *const *read_fh,
778 unsigned int read_fh_len, 788 unsigned int read_fh_len,
779 const struct GNUNET_DISK_FileHandle *const *write_fh, 789 const struct GNUNET_DISK_FileHandle *const *write_fh,
780 unsigned int write_fh_len) 790 unsigned int write_fh_len)
781{ 791{
782 // FIXME: if we have exactly two network handles / exactly two file handles 792 // FIXME: if we have exactly two network handles / exactly two file handles
783 // and they are equal, we can make one FdInfo with both 793 // and they are equal, we can make one FdInfo with both
@@ -786,90 +796,90 @@ init_fd_info(struct GNUNET_SCHEDULER_Task *t,
786 796
787 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len; 797 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
788 if (1 == t->fds_len) 798 if (1 == t->fds_len)
799 {
800 fdi = &t->fdx;
801 t->fds = fdi;
802 if (1 == read_nh_len)
789 { 803 {
790 fdi = &t->fdx; 804 GNUNET_assert (NULL != read_nh);
791 t->fds = fdi; 805 GNUNET_assert (NULL != *read_nh);
792 if (1 == read_nh_len) 806 fdi->fd = *read_nh;
793 { 807 fdi->et = GNUNET_SCHEDULER_ET_IN;
794 GNUNET_assert(NULL != read_nh); 808 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
795 GNUNET_assert(NULL != *read_nh); 809 t->read_fd = fdi->sock;
796 fdi->fd = *read_nh; 810 t->write_fd = -1;
797 fdi->et = GNUNET_SCHEDULER_ET_IN;
798 fdi->sock = GNUNET_NETWORK_get_fd(*read_nh);
799 t->read_fd = fdi->sock;
800 t->write_fd = -1;
801 }
802 else if (1 == write_nh_len)
803 {
804 GNUNET_assert(NULL != write_nh);
805 GNUNET_assert(NULL != *write_nh);
806 fdi->fd = *write_nh;
807 fdi->et = GNUNET_SCHEDULER_ET_OUT;
808 fdi->sock = GNUNET_NETWORK_get_fd(*write_nh);
809 t->read_fd = -1;
810 t->write_fd = fdi->sock;
811 }
812 else if (1 == read_fh_len)
813 {
814 GNUNET_assert(NULL != read_fh);
815 GNUNET_assert(NULL != *read_fh);
816 fdi->fh = *read_fh;
817 fdi->et = GNUNET_SCHEDULER_ET_IN;
818 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
819 t->read_fd = fdi->sock;
820 t->write_fd = -1;
821 }
822 else
823 {
824 GNUNET_assert(NULL != write_fh);
825 GNUNET_assert(NULL != *write_fh);
826 fdi->fh = *write_fh;
827 fdi->et = GNUNET_SCHEDULER_ET_OUT;
828 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
829 t->read_fd = -1;
830 t->write_fd = fdi->sock;
831 }
832 } 811 }
833 else 812 else if (1 == write_nh_len)
834 { 813 {
835 fdi = GNUNET_new_array(t->fds_len, struct GNUNET_SCHEDULER_FdInfo); 814 GNUNET_assert (NULL != write_nh);
836 t->fds = fdi; 815 GNUNET_assert (NULL != *write_nh);
816 fdi->fd = *write_nh;
817 fdi->et = GNUNET_SCHEDULER_ET_OUT;
818 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
837 t->read_fd = -1; 819 t->read_fd = -1;
820 t->write_fd = fdi->sock;
821 }
822 else if (1 == read_fh_len)
823 {
824 GNUNET_assert (NULL != read_fh);
825 GNUNET_assert (NULL != *read_fh);
826 fdi->fh = *read_fh;
827 fdi->et = GNUNET_SCHEDULER_ET_IN;
828 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
829 t->read_fd = fdi->sock;
838 t->write_fd = -1; 830 t->write_fd = -1;
839 unsigned int i;
840 for (i = 0; i != read_nh_len; ++i)
841 {
842 fdi->fd = read_nh[i];
843 GNUNET_assert(NULL != fdi->fd);
844 fdi->et = GNUNET_SCHEDULER_ET_IN;
845 fdi->sock = GNUNET_NETWORK_get_fd(read_nh[i]);
846 ++fdi;
847 }
848 for (i = 0; i != write_nh_len; ++i)
849 {
850 fdi->fd = write_nh[i];
851 GNUNET_assert(NULL != fdi->fd);
852 fdi->et = GNUNET_SCHEDULER_ET_OUT;
853 fdi->sock = GNUNET_NETWORK_get_fd(write_nh[i]);
854 ++fdi;
855 }
856 for (i = 0; i != read_fh_len; ++i)
857 {
858 fdi->fh = read_fh[i];
859 GNUNET_assert(NULL != fdi->fh);
860 fdi->et = GNUNET_SCHEDULER_ET_IN;
861 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
862 ++fdi;
863 }
864 for (i = 0; i != write_fh_len; ++i)
865 {
866 fdi->fh = write_fh[i];
867 GNUNET_assert(NULL != fdi->fh);
868 fdi->et = GNUNET_SCHEDULER_ET_OUT;
869 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
870 ++fdi;
871 }
872 } 831 }
832 else
833 {
834 GNUNET_assert (NULL != write_fh);
835 GNUNET_assert (NULL != *write_fh);
836 fdi->fh = *write_fh;
837 fdi->et = GNUNET_SCHEDULER_ET_OUT;
838 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
839 t->read_fd = -1;
840 t->write_fd = fdi->sock;
841 }
842 }
843 else
844 {
845 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
846 t->fds = fdi;
847 t->read_fd = -1;
848 t->write_fd = -1;
849 unsigned int i;
850 for (i = 0; i != read_nh_len; ++i)
851 {
852 fdi->fd = read_nh[i];
853 GNUNET_assert (NULL != fdi->fd);
854 fdi->et = GNUNET_SCHEDULER_ET_IN;
855 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
856 ++fdi;
857 }
858 for (i = 0; i != write_nh_len; ++i)
859 {
860 fdi->fd = write_nh[i];
861 GNUNET_assert (NULL != fdi->fd);
862 fdi->et = GNUNET_SCHEDULER_ET_OUT;
863 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
864 ++fdi;
865 }
866 for (i = 0; i != read_fh_len; ++i)
867 {
868 fdi->fh = read_fh[i];
869 GNUNET_assert (NULL != fdi->fh);
870 fdi->et = GNUNET_SCHEDULER_ET_IN;
871 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
872 ++fdi;
873 }
874 for (i = 0; i != write_fh_len; ++i)
875 {
876 fdi->fh = write_fh[i];
877 GNUNET_assert (NULL != fdi->fh);
878 fdi->et = GNUNET_SCHEDULER_ET_OUT;
879 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
880 ++fdi;
881 }
882 }
873} 883}
874 884
875 885
@@ -887,56 +897,56 @@ init_fd_info(struct GNUNET_SCHEDULER_Task *t,
887 * @a driver_func on it, or -1 if no updating not desired. 897 * @a driver_func on it, or -1 if no updating not desired.
888 */ 898 */
889static void 899static void
890driver_add_multiple(struct GNUNET_SCHEDULER_Task *t) 900driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
891{ 901{
892 struct GNUNET_SCHEDULER_FdInfo *fdi; 902 struct GNUNET_SCHEDULER_FdInfo *fdi;
893 int success = GNUNET_YES; 903 int success = GNUNET_YES;
894 904
895 for (unsigned int i = 0; i != t->fds_len; ++i) 905 for (unsigned int i = 0; i != t->fds_len; ++i)
896 { 906 {
897 fdi = &t->fds[i]; 907 fdi = &t->fds[i];
898 success = scheduler_driver->add(scheduler_driver->cls, 908 success = scheduler_driver->add (scheduler_driver->cls,
899 t, 909 t,
900 fdi) && success; 910 fdi) && success;
901 fdi->et = GNUNET_SCHEDULER_ET_NONE; 911 fdi->et = GNUNET_SCHEDULER_ET_NONE;
902 } 912 }
903 if (GNUNET_YES != success) 913 if (GNUNET_YES != success)
904 { 914 {
905 LOG(GNUNET_ERROR_TYPE_ERROR, 915 LOG (GNUNET_ERROR_TYPE_ERROR,
906 "driver could not add task\n"); 916 "driver could not add task\n");
907 } 917 }
908} 918}
909 919
910 920
911static void 921static void
912install_parent_control_handler(void *cls) 922install_parent_control_handler (void *cls)
913{ 923{
914 (void)cls; 924 (void) cls;
915 install_parent_control_task = NULL; 925 install_parent_control_task = NULL;
916 GNUNET_OS_install_parent_control_handler(NULL); 926 GNUNET_OS_install_parent_control_handler (NULL);
917} 927}
918 928
919 929
920static void 930static void
921shutdown_pipe_cb(void *cls) 931shutdown_pipe_cb (void *cls)
922{ 932{
923 char c; 933 char c;
924 const struct GNUNET_DISK_FileHandle *pr; 934 const struct GNUNET_DISK_FileHandle *pr;
925 935
926 (void)cls; 936 (void) cls;
927 shutdown_pipe_task = NULL; 937 shutdown_pipe_task = NULL;
928 pr = GNUNET_DISK_pipe_handle(shutdown_pipe_handle, 938 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
929 GNUNET_DISK_PIPE_END_READ); 939 GNUNET_DISK_PIPE_END_READ);
930 GNUNET_assert(!GNUNET_DISK_handle_invalid(pr)); 940 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
931 /* consume the signal */ 941 /* consume the signal */
932 GNUNET_DISK_file_read(pr, &c, sizeof(c)); 942 GNUNET_DISK_file_read (pr, &c, sizeof(c));
933 /* mark all active tasks as ready due to shutdown */ 943 /* mark all active tasks as ready due to shutdown */
934 GNUNET_SCHEDULER_shutdown(); 944 GNUNET_SCHEDULER_shutdown ();
935 shutdown_pipe_task = 945 shutdown_pipe_task =
936 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 946 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
937 pr, 947 pr,
938 &shutdown_pipe_cb, 948 &shutdown_pipe_cb,
939 NULL); 949 NULL);
940} 950}
941 951
942 952
@@ -953,62 +963,62 @@ shutdown_pipe_cb(void *cls)
953 * @return original closure of the task 963 * @return original closure of the task
954 */ 964 */
955void * 965void *
956GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task) 966GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
957{ 967{
958 enum GNUNET_SCHEDULER_Priority p; 968 enum GNUNET_SCHEDULER_Priority p;
959 int is_fd_task; 969 int is_fd_task;
960 void *ret; 970 void *ret;
961 971
962 LOG(GNUNET_ERROR_TYPE_DEBUG, 972 LOG (GNUNET_ERROR_TYPE_DEBUG,
963 "canceling task %p\n", 973 "canceling task %p\n",
964 task); 974 task);
965 975
966 /* scheduler must be running */ 976 /* scheduler must be running */
967 GNUNET_assert(NULL != scheduler_driver); 977 GNUNET_assert (NULL != scheduler_driver);
968 is_fd_task = (NULL != task->fds); 978 is_fd_task = (NULL != task->fds);
969 if (is_fd_task) 979 if (is_fd_task)
980 {
981 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
982 if (GNUNET_OK != del_result)
970 { 983 {
971 int del_result = scheduler_driver->del(scheduler_driver->cls, task); 984 LOG (GNUNET_ERROR_TYPE_ERROR,
972 if (GNUNET_OK != del_result) 985 "driver could not delete task\n");
973 { 986 GNUNET_assert (0);
974 LOG(GNUNET_ERROR_TYPE_ERROR,
975 "driver could not delete task\n");
976 GNUNET_assert(0);
977 }
978 } 987 }
979 if (!task->in_ready_list) 988 }
989 if (! task->in_ready_list)
990 {
991 if (is_fd_task)
980 { 992 {
981 if (is_fd_task) 993 GNUNET_CONTAINER_DLL_remove (pending_head,
982 { 994 pending_tail,
983 GNUNET_CONTAINER_DLL_remove(pending_head, 995 task);
984 pending_tail,
985 task);
986 }
987 else if (GNUNET_YES == task->on_shutdown)
988 {
989 GNUNET_CONTAINER_DLL_remove(shutdown_head,
990 shutdown_tail,
991 task);
992 }
993 else
994 {
995 GNUNET_CONTAINER_DLL_remove(pending_timeout_head,
996 pending_timeout_tail,
997 task);
998 if (pending_timeout_last == task)
999 pending_timeout_last = NULL;
1000 }
1001 } 996 }
1002 else 997 else if (GNUNET_YES == task->on_shutdown)
1003 { 998 {
1004 p = check_priority(task->priority); 999 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1005 GNUNET_CONTAINER_DLL_remove(ready_head[p], 1000 shutdown_tail,
1006 ready_tail[p], 1001 task);
1007 task); 1002 }
1008 ready_count--; 1003 else
1004 {
1005 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1006 pending_timeout_tail,
1007 task);
1008 if (pending_timeout_last == task)
1009 pending_timeout_last = NULL;
1009 } 1010 }
1011 }
1012 else
1013 {
1014 p = check_priority (task->priority);
1015 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1016 ready_tail[p],
1017 task);
1018 ready_count--;
1019 }
1010 ret = task->callback_cls; 1020 ret = task->callback_cls;
1011 destroy_task(task); 1021 destroy_task (task);
1012 return ret; 1022 return ret;
1013} 1023}
1014 1024
@@ -1019,19 +1029,19 @@ GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
1019 * @param t task to initialize 1029 * @param t task to initialize
1020 */ 1030 */
1021static void 1031static void
1022init_backtrace(struct GNUNET_SCHEDULER_Task *t) 1032init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1023{ 1033{
1024#if EXECINFO 1034#if EXECINFO
1025 void *backtrace_array[MAX_TRACE_DEPTH]; 1035 void *backtrace_array[MAX_TRACE_DEPTH];
1026 1036
1027 t->num_backtrace_strings 1037 t->num_backtrace_strings
1028 = backtrace(backtrace_array, MAX_TRACE_DEPTH); 1038 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1029 t->backtrace_strings = 1039 t->backtrace_strings =
1030 backtrace_symbols(backtrace_array, 1040 backtrace_symbols (backtrace_array,
1031 t->num_backtrace_strings); 1041 t->num_backtrace_strings);
1032 dump_backtrace(t); 1042 dump_backtrace (t);
1033#else 1043#else
1034 (void)t; 1044 (void) t;
1035#endif 1045#endif
1036} 1046}
1037 1047
@@ -1047,32 +1057,35 @@ init_backtrace(struct GNUNET_SCHEDULER_Task *t)
1047 * @param priority priority to use for the task 1057 * @param priority priority to use for the task
1048 */ 1058 */
1049void 1059void
1050GNUNET_SCHEDULER_add_with_reason_and_priority(GNUNET_SCHEDULER_TaskCallback task, 1060GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback
1051 void *task_cls, 1061 task,
1052 enum GNUNET_SCHEDULER_Reason reason, 1062 void *task_cls,
1053 enum GNUNET_SCHEDULER_Priority priority) 1063 enum GNUNET_SCHEDULER_Reason
1064 reason,
1065 enum GNUNET_SCHEDULER_Priority
1066 priority)
1054{ 1067{
1055 struct GNUNET_SCHEDULER_Task *t; 1068 struct GNUNET_SCHEDULER_Task *t;
1056 1069
1057 /* scheduler must be running */ 1070 /* scheduler must be running */
1058 GNUNET_assert(NULL != scheduler_driver); 1071 GNUNET_assert (NULL != scheduler_driver);
1059 GNUNET_assert(NULL != task); 1072 GNUNET_assert (NULL != task);
1060 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1073 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1061 t->read_fd = -1; 1074 t->read_fd = -1;
1062 t->write_fd = -1; 1075 t->write_fd = -1;
1063 t->callback = task; 1076 t->callback = task;
1064 t->callback_cls = task_cls; 1077 t->callback_cls = task_cls;
1065#if PROFILE_DELAYS 1078#if PROFILE_DELAYS
1066 t->start_time = GNUNET_TIME_absolute_get(); 1079 t->start_time = GNUNET_TIME_absolute_get ();
1067#endif 1080#endif
1068 t->reason = reason; 1081 t->reason = reason;
1069 t->priority = check_priority(priority); 1082 t->priority = check_priority (priority);
1070 t->lifeness = current_lifeness; 1083 t->lifeness = current_lifeness;
1071 LOG(GNUNET_ERROR_TYPE_DEBUG, 1084 LOG (GNUNET_ERROR_TYPE_DEBUG,
1072 "Adding continuation task %p\n", 1085 "Adding continuation task %p\n",
1073 t); 1086 t);
1074 init_backtrace(t); 1087 init_backtrace (t);
1075 queue_ready_task(t); 1088 queue_ready_task (t);
1076} 1089}
1077 1090
1078 1091
@@ -1088,68 +1101,69 @@ GNUNET_SCHEDULER_add_with_reason_and_priority(GNUNET_SCHEDULER_TaskCallback task
1088 * only valid until @a task is started! 1101 * only valid until @a task is started!
1089 */ 1102 */
1090struct GNUNET_SCHEDULER_Task * 1103struct GNUNET_SCHEDULER_Task *
1091GNUNET_SCHEDULER_add_at_with_priority(struct GNUNET_TIME_Absolute at, 1104GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1092 enum GNUNET_SCHEDULER_Priority priority, 1105 enum GNUNET_SCHEDULER_Priority priority,
1093 GNUNET_SCHEDULER_TaskCallback task, 1106 GNUNET_SCHEDULER_TaskCallback task,
1094 void *task_cls) 1107 void *task_cls)
1095{ 1108{
1096 struct GNUNET_SCHEDULER_Task *t; 1109 struct GNUNET_SCHEDULER_Task *t;
1097 struct GNUNET_SCHEDULER_Task *pos; 1110 struct GNUNET_SCHEDULER_Task *pos;
1098 struct GNUNET_SCHEDULER_Task *prev; 1111 struct GNUNET_SCHEDULER_Task *prev;
1099 1112
1100 /* scheduler must be running */ 1113 /* scheduler must be running */
1101 GNUNET_assert(NULL != scheduler_driver); 1114 GNUNET_assert (NULL != scheduler_driver);
1102 GNUNET_assert(NULL != task); 1115 GNUNET_assert (NULL != task);
1103 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1116 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1104 GNUNET_async_scope_get(&t->scope); 1117 GNUNET_async_scope_get (&t->scope);
1105 t->callback = task; 1118 t->callback = task;
1106 t->callback_cls = task_cls; 1119 t->callback_cls = task_cls;
1107 t->read_fd = -1; 1120 t->read_fd = -1;
1108 t->write_fd = -1; 1121 t->write_fd = -1;
1109#if PROFILE_DELAYS 1122#if PROFILE_DELAYS
1110 t->start_time = GNUNET_TIME_absolute_get(); 1123 t->start_time = GNUNET_TIME_absolute_get ();
1111#endif 1124#endif
1112 t->timeout = at; 1125 t->timeout = at;
1113 t->priority = check_priority(priority); 1126 t->priority = check_priority (priority);
1114 t->lifeness = current_lifeness; 1127 t->lifeness = current_lifeness;
1115 /* try tail first (optimization in case we are 1128 /* try tail first (optimization in case we are
1116 * appending to a long list of tasks with timeouts) */ 1129 * appending to a long list of tasks with timeouts) */
1117 if ((NULL == pending_timeout_head) || 1130 if ((NULL == pending_timeout_head) ||
1118 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us)) 1131 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us))
1119 { 1132 {
1120 GNUNET_CONTAINER_DLL_insert(pending_timeout_head, 1133 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1121 pending_timeout_tail, 1134 pending_timeout_tail,
1122 t); 1135 t);
1123 } 1136 }
1124 else 1137 else
1138 {
1139 /* first move from heuristic start backwards to before start time */
1140 prev = pending_timeout_last;
1141 while ((NULL != prev) &&
1142 (prev->timeout.abs_value_us > t->timeout.abs_value_us))
1143 prev = prev->prev;
1144 /* now, move from heuristic start (or head of list) forward to insertion point */
1145 if (NULL == prev)
1146 pos = pending_timeout_head;
1147 else
1148 pos = prev->next;
1149 while ((NULL != pos) && (pos->timeout.abs_value_us <=
1150 t->timeout.abs_value_us))
1125 { 1151 {
1126 /* first move from heuristic start backwards to before start time */ 1152 prev = pos;
1127 prev = pending_timeout_last; 1153 pos = pos->next;
1128 while ((NULL != prev) &&
1129 (prev->timeout.abs_value_us > t->timeout.abs_value_us))
1130 prev = prev->prev;
1131 /* now, move from heuristic start (or head of list) forward to insertion point */
1132 if (NULL == prev)
1133 pos = pending_timeout_head;
1134 else
1135 pos = prev->next;
1136 while ((NULL != pos) && (pos->timeout.abs_value_us <= t->timeout.abs_value_us))
1137 {
1138 prev = pos;
1139 pos = pos->next;
1140 }
1141 GNUNET_CONTAINER_DLL_insert_after(pending_timeout_head,
1142 pending_timeout_tail,
1143 prev,
1144 t);
1145 } 1154 }
1155 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1156 pending_timeout_tail,
1157 prev,
1158 t);
1159 }
1146 /* finally, update heuristic insertion point to last insertion... */ 1160 /* finally, update heuristic insertion point to last insertion... */
1147 pending_timeout_last = t; 1161 pending_timeout_last = t;
1148 1162
1149 LOG(GNUNET_ERROR_TYPE_DEBUG, 1163 LOG (GNUNET_ERROR_TYPE_DEBUG,
1150 "Adding task %p\n", 1164 "Adding task %p\n",
1151 t); 1165 t);
1152 init_backtrace(t); 1166 init_backtrace (t);
1153 return t; 1167 return t;
1154} 1168}
1155 1169
@@ -1166,15 +1180,17 @@ GNUNET_SCHEDULER_add_at_with_priority(struct GNUNET_TIME_Absolute at,
1166 * only valid until @a task is started! 1180 * only valid until @a task is started!
1167 */ 1181 */
1168struct GNUNET_SCHEDULER_Task * 1182struct GNUNET_SCHEDULER_Task *
1169GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay, 1183GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1170 enum GNUNET_SCHEDULER_Priority priority, 1184 enum GNUNET_SCHEDULER_Priority
1171 GNUNET_SCHEDULER_TaskCallback task, 1185 priority,
1172 void *task_cls) 1186 GNUNET_SCHEDULER_TaskCallback task,
1187 void *task_cls)
1173{ 1188{
1174 return GNUNET_SCHEDULER_add_at_with_priority(GNUNET_TIME_relative_to_absolute(delay), 1189 return GNUNET_SCHEDULER_add_at_with_priority (
1175 priority, 1190 GNUNET_TIME_relative_to_absolute (delay),
1176 task, 1191 priority,
1177 task_cls); 1192 task,
1193 task_cls);
1178} 1194}
1179 1195
1180 1196
@@ -1188,14 +1204,14 @@ GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay,
1188 * only valid until @a task is started! 1204 * only valid until @a task is started!
1189 */ 1205 */
1190struct GNUNET_SCHEDULER_Task * 1206struct GNUNET_SCHEDULER_Task *
1191GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio, 1207GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1192 GNUNET_SCHEDULER_TaskCallback task, 1208 GNUNET_SCHEDULER_TaskCallback task,
1193 void *task_cls) 1209 void *task_cls)
1194{ 1210{
1195 return GNUNET_SCHEDULER_add_delayed_with_priority(GNUNET_TIME_UNIT_ZERO, 1211 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1196 prio, 1212 prio,
1197 task, 1213 task,
1198 task_cls); 1214 task_cls);
1199} 1215}
1200 1216
1201 1217
@@ -1211,14 +1227,14 @@ GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio,
1211 * only valid until @a task is started! 1227 * only valid until @a task is started!
1212 */ 1228 */
1213struct GNUNET_SCHEDULER_Task * 1229struct GNUNET_SCHEDULER_Task *
1214GNUNET_SCHEDULER_add_at(struct GNUNET_TIME_Absolute at, 1230GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1215 GNUNET_SCHEDULER_TaskCallback task, 1231 GNUNET_SCHEDULER_TaskCallback task,
1216 void *task_cls) 1232 void *task_cls)
1217{ 1233{
1218 return GNUNET_SCHEDULER_add_at_with_priority(at, 1234 return GNUNET_SCHEDULER_add_at_with_priority (at,
1219 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1235 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1220 task, 1236 task,
1221 task_cls); 1237 task_cls);
1222} 1238}
1223 1239
1224 1240
@@ -1234,14 +1250,14 @@ GNUNET_SCHEDULER_add_at(struct GNUNET_TIME_Absolute at,
1234 * only valid until @a task is started! 1250 * only valid until @a task is started!
1235 */ 1251 */
1236struct GNUNET_SCHEDULER_Task * 1252struct GNUNET_SCHEDULER_Task *
1237GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, 1253GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1238 GNUNET_SCHEDULER_TaskCallback task, 1254 GNUNET_SCHEDULER_TaskCallback task,
1239 void *task_cls) 1255 void *task_cls)
1240{ 1256{
1241 return GNUNET_SCHEDULER_add_delayed_with_priority(delay, 1257 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1242 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1258 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1243 task, 1259 task,
1244 task_cls); 1260 task_cls);
1245} 1261}
1246 1262
1247 1263
@@ -1261,12 +1277,12 @@ GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay,
1261 * only valid until @a task is started! 1277 * only valid until @a task is started!
1262 */ 1278 */
1263struct GNUNET_SCHEDULER_Task * 1279struct GNUNET_SCHEDULER_Task *
1264GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, 1280GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1265 void *task_cls) 1281 void *task_cls)
1266{ 1282{
1267 return GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_ZERO, 1283 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1268 task, 1284 task,
1269 task_cls); 1285 task_cls);
1270} 1286}
1271 1287
1272 1288
@@ -1281,34 +1297,34 @@ GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task,
1281 * only valid until @a task is started! 1297 * only valid until @a task is started!
1282 */ 1298 */
1283struct GNUNET_SCHEDULER_Task * 1299struct GNUNET_SCHEDULER_Task *
1284GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, 1300GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1285 void *task_cls) 1301 void *task_cls)
1286{ 1302{
1287 struct GNUNET_SCHEDULER_Task *t; 1303 struct GNUNET_SCHEDULER_Task *t;
1288 1304
1289 /* scheduler must be running */ 1305 /* scheduler must be running */
1290 GNUNET_assert(NULL != scheduler_driver); 1306 GNUNET_assert (NULL != scheduler_driver);
1291 GNUNET_assert(NULL != task); 1307 GNUNET_assert (NULL != task);
1292 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1308 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1293 GNUNET_async_scope_get(&t->scope); 1309 GNUNET_async_scope_get (&t->scope);
1294 t->callback = task; 1310 t->callback = task;
1295 t->callback_cls = task_cls; 1311 t->callback_cls = task_cls;
1296 t->read_fd = -1; 1312 t->read_fd = -1;
1297 t->write_fd = -1; 1313 t->write_fd = -1;
1298#if PROFILE_DELAYS 1314#if PROFILE_DELAYS
1299 t->start_time = GNUNET_TIME_absolute_get(); 1315 t->start_time = GNUNET_TIME_absolute_get ();
1300#endif 1316#endif
1301 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS; 1317 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1302 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN; 1318 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1303 t->on_shutdown = GNUNET_YES; 1319 t->on_shutdown = GNUNET_YES;
1304 t->lifeness = GNUNET_NO; 1320 t->lifeness = GNUNET_NO;
1305 GNUNET_CONTAINER_DLL_insert(shutdown_head, 1321 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1306 shutdown_tail, 1322 shutdown_tail,
1307 t); 1323 t);
1308 LOG(GNUNET_ERROR_TYPE_DEBUG, 1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Adding shutdown task %p\n", 1325 "Adding shutdown task %p\n",
1310 t); 1326 t);
1311 init_backtrace(t); 1327 init_backtrace (t);
1312 return t; 1328 return t;
1313} 1329}
1314 1330
@@ -1328,13 +1344,13 @@ GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task,
1328 * only valid until @a task is started! 1344 * only valid until @a task is started!
1329 */ 1345 */
1330struct GNUNET_SCHEDULER_Task * 1346struct GNUNET_SCHEDULER_Task *
1331GNUNET_SCHEDULER_add_now_with_lifeness(int lifeness, 1347GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1332 GNUNET_SCHEDULER_TaskCallback task, 1348 GNUNET_SCHEDULER_TaskCallback task,
1333 void *task_cls) 1349 void *task_cls)
1334{ 1350{
1335 struct GNUNET_SCHEDULER_Task *ret; 1351 struct GNUNET_SCHEDULER_Task *ret;
1336 1352
1337 ret = GNUNET_SCHEDULER_add_now(task, task_cls); 1353 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1338 ret->lifeness = lifeness; 1354 ret->lifeness = lifeness;
1339 return ret; 1355 return ret;
1340} 1356}
@@ -1348,21 +1364,21 @@ GNUNET_SCHEDULER_add_now_with_lifeness(int lifeness,
1348 * @param raw_fd the raw file descriptor to check 1364 * @param raw_fd the raw file descriptor to check
1349 */ 1365 */
1350void 1366void
1351check_fd(struct GNUNET_SCHEDULER_Task *t, int raw_fd) 1367check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1352{ 1368{
1353 if (-1 != raw_fd) 1369 if (-1 != raw_fd)
1354 { 1370 {
1355 int flags = fcntl(raw_fd, F_GETFD); 1371 int flags = fcntl (raw_fd, F_GETFD);
1356 1372
1357 if ((flags == -1) && (errno == EBADF)) 1373 if ((flags == -1) && (errno == EBADF))
1358 { 1374 {
1359 LOG(GNUNET_ERROR_TYPE_ERROR, 1375 LOG (GNUNET_ERROR_TYPE_ERROR,
1360 "Got invalid file descriptor %d!\n", 1376 "Got invalid file descriptor %d!\n",
1361 raw_fd); 1377 raw_fd);
1362 init_backtrace(t); 1378 init_backtrace (t);
1363 GNUNET_assert(0); 1379 GNUNET_assert (0);
1364 }
1365 } 1380 }
1381 }
1366} 1382}
1367#endif 1383#endif
1368 1384
@@ -1393,52 +1409,53 @@ check_fd(struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1393 * only valid until @a task is started! 1409 * only valid until @a task is started!
1394 */ 1410 */
1395static struct GNUNET_SCHEDULER_Task * 1411static struct GNUNET_SCHEDULER_Task *
1396add_without_sets(struct GNUNET_TIME_Relative delay, 1412add_without_sets (struct GNUNET_TIME_Relative delay,
1397 enum GNUNET_SCHEDULER_Priority priority, 1413 enum GNUNET_SCHEDULER_Priority priority,
1398 const struct GNUNET_NETWORK_Handle *read_nh, 1414 const struct GNUNET_NETWORK_Handle *read_nh,
1399 const struct GNUNET_NETWORK_Handle *write_nh, 1415 const struct GNUNET_NETWORK_Handle *write_nh,
1400 const struct GNUNET_DISK_FileHandle *read_fh, 1416 const struct GNUNET_DISK_FileHandle *read_fh,
1401 const struct GNUNET_DISK_FileHandle *write_fh, 1417 const struct GNUNET_DISK_FileHandle *write_fh,
1402 GNUNET_SCHEDULER_TaskCallback task, 1418 GNUNET_SCHEDULER_TaskCallback task,
1403 void *task_cls) 1419 void *task_cls)
1404{ 1420{
1405 struct GNUNET_SCHEDULER_Task *t; 1421 struct GNUNET_SCHEDULER_Task *t;
1406 1422
1407 /* scheduler must be running */ 1423 /* scheduler must be running */
1408 GNUNET_assert(NULL != scheduler_driver); 1424 GNUNET_assert (NULL != scheduler_driver);
1409 GNUNET_assert(NULL != task); 1425 GNUNET_assert (NULL != task);
1410 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1426 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1411 GNUNET_async_scope_get(&t->scope); 1427 GNUNET_async_scope_get (&t->scope);
1412 init_fd_info(t, 1428 init_fd_info (t,
1413 &read_nh, 1429 &read_nh,
1414 read_nh ? 1 : 0, 1430 read_nh ? 1 : 0,
1415 &write_nh, 1431 &write_nh,
1416 write_nh ? 1 : 0, 1432 write_nh ? 1 : 0,
1417 &read_fh, 1433 &read_fh,
1418 read_fh ? 1 : 0, 1434 read_fh ? 1 : 0,
1419 &write_fh, 1435 &write_fh,
1420 write_fh ? 1 : 0); 1436 write_fh ? 1 : 0);
1421 t->callback = task; 1437 t->callback = task;
1422 t->callback_cls = task_cls; 1438 t->callback_cls = task_cls;
1423#if DEBUG_FDS 1439#if DEBUG_FDS
1424 check_fd(t, NULL != read_nh ? GNUNET_NETWORK_get_fd(read_nh) : -1); 1440 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1425 check_fd(t, NULL != write_nh ? GNUNET_NETWORK_get_fd(write_nh) : -1); 1441 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1426 check_fd(t, NULL != read_fh ? read_fh->fd : -1); 1442 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1427 check_fd(t, NULL != write_fh ? write_fh->fd : -1); 1443 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1428#endif 1444#endif
1429#if PROFILE_DELAYS 1445#if PROFILE_DELAYS
1430 t->start_time = GNUNET_TIME_absolute_get(); 1446 t->start_time = GNUNET_TIME_absolute_get ();
1431#endif 1447#endif
1432 t->timeout = GNUNET_TIME_relative_to_absolute(delay); 1448 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1433 t->priority = check_priority((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority); 1449 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ?
1450 current_priority : priority);
1434 t->lifeness = current_lifeness; 1451 t->lifeness = current_lifeness;
1435 GNUNET_CONTAINER_DLL_insert(pending_head, 1452 GNUNET_CONTAINER_DLL_insert (pending_head,
1436 pending_tail, 1453 pending_tail,
1437 t); 1454 t);
1438 driver_add_multiple(t); 1455 driver_add_multiple (t);
1439 max_priority_added = GNUNET_MAX(max_priority_added, 1456 max_priority_added = GNUNET_MAX (max_priority_added,
1440 t->priority); 1457 t->priority);
1441 init_backtrace(t); 1458 init_backtrace (t);
1442 return t; 1459 return t;
1443} 1460}
1444 1461
@@ -1464,14 +1481,14 @@ add_without_sets(struct GNUNET_TIME_Relative delay,
1464 * only valid until @a task is started! 1481 * only valid until @a task is started!
1465 */ 1482 */
1466struct GNUNET_SCHEDULER_Task * 1483struct GNUNET_SCHEDULER_Task *
1467GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, 1484GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1468 struct GNUNET_NETWORK_Handle *rfd, 1485 struct GNUNET_NETWORK_Handle *rfd,
1469 GNUNET_SCHEDULER_TaskCallback task, 1486 GNUNET_SCHEDULER_TaskCallback task,
1470 void *task_cls) 1487 void *task_cls)
1471{ 1488{
1472 return GNUNET_SCHEDULER_add_read_net_with_priority(delay, 1489 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1473 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1490 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1474 rfd, task, task_cls); 1491 rfd, task, task_cls);
1475} 1492}
1476 1493
1477 1494
@@ -1498,17 +1515,18 @@ GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay,
1498 * only valid until @a task is started! 1515 * only valid until @a task is started!
1499 */ 1516 */
1500struct GNUNET_SCHEDULER_Task * 1517struct GNUNET_SCHEDULER_Task *
1501GNUNET_SCHEDULER_add_read_net_with_priority(struct GNUNET_TIME_Relative delay, 1518GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1502 enum GNUNET_SCHEDULER_Priority priority, 1519 enum GNUNET_SCHEDULER_Priority
1503 struct GNUNET_NETWORK_Handle *rfd, 1520 priority,
1504 GNUNET_SCHEDULER_TaskCallback task, 1521 struct GNUNET_NETWORK_Handle *rfd,
1505 void *task_cls) 1522 GNUNET_SCHEDULER_TaskCallback task,
1523 void *task_cls)
1506{ 1524{
1507 return GNUNET_SCHEDULER_add_net_with_priority(delay, priority, 1525 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1508 rfd, 1526 rfd,
1509 GNUNET_YES, 1527 GNUNET_YES,
1510 GNUNET_NO, 1528 GNUNET_NO,
1511 task, task_cls); 1529 task, task_cls);
1512} 1530}
1513 1531
1514 1532
@@ -1534,16 +1552,16 @@ GNUNET_SCHEDULER_add_read_net_with_priority(struct GNUNET_TIME_Relative delay,
1534 * only valid until @a task is started! 1552 * only valid until @a task is started!
1535 */ 1553 */
1536struct GNUNET_SCHEDULER_Task * 1554struct GNUNET_SCHEDULER_Task *
1537GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay, 1555GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1538 struct GNUNET_NETWORK_Handle *wfd, 1556 struct GNUNET_NETWORK_Handle *wfd,
1539 GNUNET_SCHEDULER_TaskCallback task, 1557 GNUNET_SCHEDULER_TaskCallback task,
1540 void *task_cls) 1558 void *task_cls)
1541{ 1559{
1542 return GNUNET_SCHEDULER_add_net_with_priority(delay, 1560 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1543 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1561 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1544 wfd, 1562 wfd,
1545 GNUNET_NO, GNUNET_YES, 1563 GNUNET_NO, GNUNET_YES,
1546 task, task_cls); 1564 task, task_cls);
1547} 1565}
1548 1566
1549/** 1567/**
@@ -1570,24 +1588,24 @@ GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay,
1570 * only valid until "task" is started! 1588 * only valid until "task" is started!
1571 */ 1589 */
1572struct GNUNET_SCHEDULER_Task * 1590struct GNUNET_SCHEDULER_Task *
1573GNUNET_SCHEDULER_add_net_with_priority(struct GNUNET_TIME_Relative delay, 1591GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1574 enum GNUNET_SCHEDULER_Priority priority, 1592 enum GNUNET_SCHEDULER_Priority priority,
1575 struct GNUNET_NETWORK_Handle *fd, 1593 struct GNUNET_NETWORK_Handle *fd,
1576 int on_read, 1594 int on_read,
1577 int on_write, 1595 int on_write,
1578 GNUNET_SCHEDULER_TaskCallback task, 1596 GNUNET_SCHEDULER_TaskCallback task,
1579 void *task_cls) 1597 void *task_cls)
1580{ 1598{
1581 /* scheduler must be running */ 1599 /* scheduler must be running */
1582 GNUNET_assert(NULL != scheduler_driver); 1600 GNUNET_assert (NULL != scheduler_driver);
1583 GNUNET_assert(on_read || on_write); 1601 GNUNET_assert (on_read || on_write);
1584 GNUNET_assert(GNUNET_NETWORK_get_fd(fd) >= 0); 1602 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1585 return add_without_sets(delay, priority, 1603 return add_without_sets (delay, priority,
1586 on_read ? fd : NULL, 1604 on_read ? fd : NULL,
1587 on_write ? fd : NULL, 1605 on_write ? fd : NULL,
1588 NULL, 1606 NULL,
1589 NULL, 1607 NULL,
1590 task, task_cls); 1608 task, task_cls);
1591} 1609}
1592 1610
1593 1611
@@ -1612,11 +1630,12 @@ GNUNET_SCHEDULER_add_net_with_priority(struct GNUNET_TIME_Relative delay,
1612 * only valid until @a task is started! 1630 * only valid until @a task is started!
1613 */ 1631 */
1614struct GNUNET_SCHEDULER_Task * 1632struct GNUNET_SCHEDULER_Task *
1615GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, 1633GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1616 const struct GNUNET_DISK_FileHandle *rfd, 1634 const struct GNUNET_DISK_FileHandle *rfd,
1617 GNUNET_SCHEDULER_TaskCallback task, void *task_cls) 1635 GNUNET_SCHEDULER_TaskCallback task,
1636 void *task_cls)
1618{ 1637{
1619 return GNUNET_SCHEDULER_add_file_with_priority( 1638 return GNUNET_SCHEDULER_add_file_with_priority (
1620 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1639 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1621 rfd, GNUNET_YES, GNUNET_NO, 1640 rfd, GNUNET_YES, GNUNET_NO,
1622 task, task_cls); 1641 task, task_cls);
@@ -1644,11 +1663,12 @@ GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay,
1644 * only valid until @a task is started! 1663 * only valid until @a task is started!
1645 */ 1664 */
1646struct GNUNET_SCHEDULER_Task * 1665struct GNUNET_SCHEDULER_Task *
1647GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay, 1666GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1648 const struct GNUNET_DISK_FileHandle *wfd, 1667 const struct GNUNET_DISK_FileHandle *wfd,
1649 GNUNET_SCHEDULER_TaskCallback task, void *task_cls) 1668 GNUNET_SCHEDULER_TaskCallback task,
1669 void *task_cls)
1650{ 1670{
1651 return GNUNET_SCHEDULER_add_file_with_priority( 1671 return GNUNET_SCHEDULER_add_file_with_priority (
1652 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1672 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1653 wfd, GNUNET_NO, GNUNET_YES, 1673 wfd, GNUNET_NO, GNUNET_YES,
1654 task, task_cls); 1674 task, task_cls);
@@ -1679,31 +1699,34 @@ GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay,
1679 * only valid until @a task is started! 1699 * only valid until @a task is started!
1680 */ 1700 */
1681struct GNUNET_SCHEDULER_Task * 1701struct GNUNET_SCHEDULER_Task *
1682GNUNET_SCHEDULER_add_file_with_priority(struct GNUNET_TIME_Relative delay, 1702GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1683 enum GNUNET_SCHEDULER_Priority priority, 1703 enum GNUNET_SCHEDULER_Priority
1684 const struct GNUNET_DISK_FileHandle *fd, 1704 priority,
1685 int on_read, int on_write, 1705 const struct
1686 GNUNET_SCHEDULER_TaskCallback task, void *task_cls) 1706 GNUNET_DISK_FileHandle *fd,
1707 int on_read, int on_write,
1708 GNUNET_SCHEDULER_TaskCallback task,
1709 void *task_cls)
1687{ 1710{
1688 /* scheduler must be running */ 1711 /* scheduler must be running */
1689 GNUNET_assert(NULL != scheduler_driver); 1712 GNUNET_assert (NULL != scheduler_driver);
1690 GNUNET_assert(on_read || on_write); 1713 GNUNET_assert (on_read || on_write);
1691 GNUNET_assert(fd->fd >= 0); 1714 GNUNET_assert (fd->fd >= 0);
1692 return add_without_sets(delay, priority, 1715 return add_without_sets (delay, priority,
1693 NULL, 1716 NULL,
1694 NULL, 1717 NULL,
1695 on_read ? fd : NULL, 1718 on_read ? fd : NULL,
1696 on_write ? fd : NULL, 1719 on_write ? fd : NULL,
1697 task, task_cls); 1720 task, task_cls);
1698} 1721}
1699 1722
1700 1723
1701void 1724void
1702extract_handles(const struct GNUNET_NETWORK_FDSet *fdset, 1725extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1703 const struct GNUNET_NETWORK_Handle ***ntarget, 1726 const struct GNUNET_NETWORK_Handle ***ntarget,
1704 unsigned int *extracted_nhandles, 1727 unsigned int *extracted_nhandles,
1705 const struct GNUNET_DISK_FileHandle ***ftarget, 1728 const struct GNUNET_DISK_FileHandle ***ftarget,
1706 unsigned int *extracted_fhandles) 1729 unsigned int *extracted_fhandles)
1707{ 1730{
1708 // FIXME: this implementation only works for unix, for WIN32 the file handles 1731 // FIXME: this implementation only works for unix, for WIN32 the file handles
1709 // in fdset must be handled separately 1732 // in fdset must be handled separately
@@ -1717,31 +1740,31 @@ extract_handles(const struct GNUNET_NETWORK_FDSet *fdset,
1717 nhandles_len = 0; 1740 nhandles_len = 0;
1718 fhandles_len = 0; 1741 fhandles_len = 0;
1719 for (int sock = 0; sock != fdset->nsds; ++sock) 1742 for (int sock = 0; sock != fdset->nsds; ++sock)
1743 {
1744 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1720 { 1745 {
1721 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native(fdset, sock)) 1746 struct GNUNET_NETWORK_Handle *nhandle;
1747 struct GNUNET_DISK_FileHandle *fhandle;
1748
1749 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1750 if (NULL != nhandle)
1751 {
1752 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1753 }
1754 else
1755 {
1756 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1757 if (NULL != fhandle)
1758 {
1759 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1760 }
1761 else
1722 { 1762 {
1723 struct GNUNET_NETWORK_Handle *nhandle; 1763 GNUNET_assert (0);
1724 struct GNUNET_DISK_FileHandle *fhandle;
1725
1726 nhandle = GNUNET_NETWORK_socket_box_native(sock);
1727 if (NULL != nhandle)
1728 {
1729 GNUNET_array_append(nhandles, nhandles_len, nhandle);
1730 }
1731 else
1732 {
1733 fhandle = GNUNET_DISK_get_handle_from_int_fd(sock);
1734 if (NULL != fhandle)
1735 {
1736 GNUNET_array_append(fhandles, fhandles_len, fhandle);
1737 }
1738 else
1739 {
1740 GNUNET_assert(0);
1741 }
1742 }
1743 } 1764 }
1765 }
1744 } 1766 }
1767 }
1745 *ntarget = nhandles_len > 0 ? nhandles : NULL; 1768 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1746 *ftarget = fhandles_len > 0 ? fhandles : NULL; 1769 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1747 *extracted_nhandles = nhandles_len; 1770 *extracted_nhandles = nhandles_len;
@@ -1781,12 +1804,12 @@ extract_handles(const struct GNUNET_NETWORK_FDSet *fdset,
1781 * only valid until @a task is started! 1804 * only valid until @a task is started!
1782 */ 1805 */
1783struct GNUNET_SCHEDULER_Task * 1806struct GNUNET_SCHEDULER_Task *
1784GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio, 1807GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1785 struct GNUNET_TIME_Relative delay, 1808 struct GNUNET_TIME_Relative delay,
1786 const struct GNUNET_NETWORK_FDSet *rs, 1809 const struct GNUNET_NETWORK_FDSet *rs,
1787 const struct GNUNET_NETWORK_FDSet *ws, 1810 const struct GNUNET_NETWORK_FDSet *ws,
1788 GNUNET_SCHEDULER_TaskCallback task, 1811 GNUNET_SCHEDULER_TaskCallback task,
1789 void *task_cls) 1812 void *task_cls)
1790{ 1813{
1791 struct GNUNET_SCHEDULER_Task *t; 1814 struct GNUNET_SCHEDULER_Task *t;
1792 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL; 1815 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
@@ -1799,8 +1822,8 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1799 unsigned int write_fhandles_len = 0; 1822 unsigned int write_fhandles_len = 0;
1800 1823
1801 /* scheduler must be running */ 1824 /* scheduler must be running */
1802 GNUNET_assert(NULL != scheduler_driver); 1825 GNUNET_assert (NULL != scheduler_driver);
1803 GNUNET_assert(NULL != task); 1826 GNUNET_assert (NULL != task);
1804 int no_rs = (NULL == rs); 1827 int no_rs = (NULL == rs);
1805 int no_ws = (NULL == ws); 1828 int no_ws = (NULL == ws);
1806 int empty_rs = (NULL != rs) && (0 == rs->nsds); 1829 int empty_rs = (NULL != rs) && (0 == rs->nsds);
@@ -1809,25 +1832,25 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1809 (empty_rs && empty_ws) || 1832 (empty_rs && empty_ws) ||
1810 (no_rs && empty_ws) || 1833 (no_rs && empty_ws) ||
1811 (no_ws && empty_rs); 1834 (no_ws && empty_rs);
1812 if (!no_fds) 1835 if (! no_fds)
1836 {
1837 if (NULL != rs)
1813 { 1838 {
1814 if (NULL != rs) 1839 extract_handles (rs,
1815 { 1840 &read_nhandles,
1816 extract_handles(rs, 1841 &read_nhandles_len,
1817 &read_nhandles, 1842 &read_fhandles,
1818 &read_nhandles_len, 1843 &read_fhandles_len);
1819 &read_fhandles,
1820 &read_fhandles_len);
1821 }
1822 if (NULL != ws)
1823 {
1824 extract_handles(ws,
1825 &write_nhandles,
1826 &write_nhandles_len,
1827 &write_fhandles,
1828 &write_fhandles_len);
1829 }
1830 } 1844 }
1845 if (NULL != ws)
1846 {
1847 extract_handles (ws,
1848 &write_nhandles,
1849 &write_nhandles_len,
1850 &write_fhandles,
1851 &write_fhandles_len);
1852 }
1853 }
1831 /** 1854 /**
1832 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty 1855 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1833 * although its maximum FD number (nsds) is greater than 0. We handle 1856 * although its maximum FD number (nsds) is greater than 0. We handle
@@ -1840,49 +1863,49 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1840 (0 == write_nhandles_len) && 1863 (0 == write_nhandles_len) &&
1841 (0 == write_fhandles_len); 1864 (0 == write_fhandles_len);
1842 if (no_fds || no_fds_extracted) 1865 if (no_fds || no_fds_extracted)
1843 return GNUNET_SCHEDULER_add_delayed_with_priority(delay, 1866 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1844 prio, 1867 prio,
1845 task, 1868 task,
1846 task_cls); 1869 task_cls);
1847 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1870 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1848 GNUNET_async_scope_get(&t->scope); 1871 GNUNET_async_scope_get (&t->scope);
1849 init_fd_info(t, 1872 init_fd_info (t,
1850 read_nhandles, 1873 read_nhandles,
1851 read_nhandles_len, 1874 read_nhandles_len,
1852 write_nhandles, 1875 write_nhandles,
1853 write_nhandles_len, 1876 write_nhandles_len,
1854 read_fhandles, 1877 read_fhandles,
1855 read_fhandles_len, 1878 read_fhandles_len,
1856 write_fhandles, 1879 write_fhandles,
1857 write_fhandles_len); 1880 write_fhandles_len);
1858 t->callback = task; 1881 t->callback = task;
1859 t->callback_cls = task_cls; 1882 t->callback_cls = task_cls;
1860 t->own_handles = GNUNET_YES; 1883 t->own_handles = GNUNET_YES;
1861 /* free the arrays of pointers to network / file handles, the actual 1884 /* free the arrays of pointers to network / file handles, the actual
1862 * handles will be freed in destroy_task */ 1885 * handles will be freed in destroy_task */
1863 GNUNET_array_grow(read_nhandles, read_nhandles_len, 0); 1886 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1864 GNUNET_array_grow(write_nhandles, write_nhandles_len, 0); 1887 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1865 GNUNET_array_grow(read_fhandles, read_fhandles_len, 0); 1888 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1866 GNUNET_array_grow(write_fhandles, write_fhandles_len, 0); 1889 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1867#if PROFILE_DELAYS 1890#if PROFILE_DELAYS
1868 t->start_time = GNUNET_TIME_absolute_get(); 1891 t->start_time = GNUNET_TIME_absolute_get ();
1869#endif 1892#endif
1870 t->timeout = GNUNET_TIME_relative_to_absolute(delay); 1893 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1871 t->priority = 1894 t->priority =
1872 check_priority((prio == 1895 check_priority ((prio ==
1873 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : 1896 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1874 prio); 1897 prio);
1875 t->lifeness = current_lifeness; 1898 t->lifeness = current_lifeness;
1876 GNUNET_CONTAINER_DLL_insert(pending_head, 1899 GNUNET_CONTAINER_DLL_insert (pending_head,
1877 pending_tail, 1900 pending_tail,
1878 t); 1901 t);
1879 driver_add_multiple(t); 1902 driver_add_multiple (t);
1880 max_priority_added = GNUNET_MAX(max_priority_added, 1903 max_priority_added = GNUNET_MAX (max_priority_added,
1881 t->priority); 1904 t->priority);
1882 LOG(GNUNET_ERROR_TYPE_DEBUG, 1905 LOG (GNUNET_ERROR_TYPE_DEBUG,
1883 "Adding task %p\n", 1906 "Adding task %p\n",
1884 t); 1907 t);
1885 init_backtrace(t); 1908 init_backtrace (t);
1886 return t; 1909 return t;
1887} 1910}
1888 1911
@@ -1899,8 +1922,8 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1899 * @param fdi information about the related FD 1922 * @param fdi information about the related FD
1900 */ 1923 */
1901void 1924void
1902GNUNET_SCHEDULER_task_ready(struct GNUNET_SCHEDULER_Task *task, 1925GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1903 struct GNUNET_SCHEDULER_FdInfo *fdi) 1926 struct GNUNET_SCHEDULER_FdInfo *fdi)
1904{ 1927{
1905 enum GNUNET_SCHEDULER_Reason reason; 1928 enum GNUNET_SCHEDULER_Reason reason;
1906 1929
@@ -1914,12 +1937,12 @@ GNUNET_SCHEDULER_task_ready(struct GNUNET_SCHEDULER_Task *task,
1914 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE; 1937 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1915 task->reason = reason; 1938 task->reason = reason;
1916 if (GNUNET_NO == task->in_ready_list) 1939 if (GNUNET_NO == task->in_ready_list)
1917 { 1940 {
1918 GNUNET_CONTAINER_DLL_remove(pending_head, 1941 GNUNET_CONTAINER_DLL_remove (pending_head,
1919 pending_tail, 1942 pending_tail,
1920 task); 1943 task);
1921 queue_ready_task(task); 1944 queue_ready_task (task);
1922 } 1945 }
1923} 1946}
1924 1947
1925 1948
@@ -1944,173 +1967,174 @@ GNUNET_SCHEDULER_task_ready(struct GNUNET_SCHEDULER_Task *task,
1944 * if we are done running tasks (yield to block) 1967 * if we are done running tasks (yield to block)
1945 */ 1968 */
1946int 1969int
1947GNUNET_SCHEDULER_do_work(struct GNUNET_SCHEDULER_Handle *sh) 1970GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
1948{ 1971{
1949 enum GNUNET_SCHEDULER_Priority p; 1972 enum GNUNET_SCHEDULER_Priority p;
1950 struct GNUNET_SCHEDULER_Task *pos; 1973 struct GNUNET_SCHEDULER_Task *pos;
1951 struct GNUNET_TIME_Absolute now; 1974 struct GNUNET_TIME_Absolute now;
1952 1975
1953 /* check for tasks that reached the timeout! */ 1976 /* check for tasks that reached the timeout! */
1954 now = GNUNET_TIME_absolute_get(); 1977 now = GNUNET_TIME_absolute_get ();
1955 pos = pending_timeout_head; 1978 pos = pending_timeout_head;
1956 while (NULL != pos) 1979 while (NULL != pos)
1957 { 1980 {
1958 struct GNUNET_SCHEDULER_Task *next = pos->next; 1981 struct GNUNET_SCHEDULER_Task *next = pos->next;
1959 if (now.abs_value_us >= pos->timeout.abs_value_us) 1982 if (now.abs_value_us >= pos->timeout.abs_value_us)
1960 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; 1983 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1961 if (0 == pos->reason) 1984 if (0 == pos->reason)
1962 break; 1985 break;
1963 GNUNET_CONTAINER_DLL_remove(pending_timeout_head, 1986 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1964 pending_timeout_tail, 1987 pending_timeout_tail,
1965 pos); 1988 pos);
1966 if (pending_timeout_last == pos) 1989 if (pending_timeout_last == pos)
1967 pending_timeout_last = NULL; 1990 pending_timeout_last = NULL;
1968 queue_ready_task(pos); 1991 queue_ready_task (pos);
1969 pos = next; 1992 pos = next;
1970 } 1993 }
1971 pos = pending_head; 1994 pos = pending_head;
1972 while (NULL != pos) 1995 while (NULL != pos)
1996 {
1997 struct GNUNET_SCHEDULER_Task *next = pos->next;
1998 if (now.abs_value_us >= pos->timeout.abs_value_us)
1973 { 1999 {
1974 struct GNUNET_SCHEDULER_Task *next = pos->next; 2000 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1975 if (now.abs_value_us >= pos->timeout.abs_value_us) 2001 GNUNET_CONTAINER_DLL_remove (pending_head,
1976 { 2002 pending_tail,
1977 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; 2003 pos);
1978 GNUNET_CONTAINER_DLL_remove(pending_head, 2004 queue_ready_task (pos);
1979 pending_tail,
1980 pos);
1981 queue_ready_task(pos);
1982 }
1983 pos = next;
1984 } 2005 }
2006 pos = next;
2007 }
1985 2008
1986 if (0 == ready_count) 2009 if (0 == ready_count)
1987 { 2010 {
1988 struct GNUNET_TIME_Absolute timeout = get_timeout(); 2011 struct GNUNET_TIME_Absolute timeout = get_timeout ();
1989 2012
1990 if (timeout.abs_value_us > now.abs_value_us) 2013 if (timeout.abs_value_us > now.abs_value_us)
1991 { 2014 {
1992 /** 2015 /**
1993 * The event loop called this function before the current timeout was 2016 * The event loop called this function before the current timeout was
1994 * reached (and no FD tasks are ready). This is acceptable if 2017 * reached (and no FD tasks are ready). This is acceptable if
1995 * 2018 *
1996 * - the system time was changed while the driver was waiting for 2019 * - the system time was changed while the driver was waiting for
1997 * the timeout 2020 * the timeout
1998 * - an external event loop called GNUnet API functions outside of 2021 * - an external event loop called GNUnet API functions outside of
1999 * the callbacks called in GNUNET_SCHEDULER_do_work and thus 2022 * the callbacks called in GNUNET_SCHEDULER_do_work and thus
2000 * wasn't notified about the new timeout 2023 * wasn't notified about the new timeout
2001 * 2024 *
2002 * It might also mean we are busy-waiting because of a programming 2025 * It might also mean we are busy-waiting because of a programming
2003 * error in the external event loop. 2026 * error in the external event loop.
2004 */ 2027 */
2005 LOG(GNUNET_ERROR_TYPE_DEBUG, 2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2006 "GNUNET_SCHEDULER_do_work did not find any ready " 2029 "GNUNET_SCHEDULER_do_work did not find any ready "
2007 "tasks and timeout has not been reached yet.\n"); 2030 "tasks and timeout has not been reached yet.\n");
2008 }
2009 else
2010 {
2011 /**
2012 * the current timeout was reached but no ready tasks were found,
2013 * internal scheduler error!
2014 */
2015 GNUNET_assert(0);
2016 }
2017 } 2031 }
2032 else
2033 {
2034 /**
2035 * the current timeout was reached but no ready tasks were found,
2036 * internal scheduler error!
2037 */
2038 GNUNET_assert (0);
2039 }
2040 }
2018 else 2041 else
2042 {
2043 /* find out which task priority level we are going to
2044 process this time */
2045 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2046 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2047 /* yes, p>0 is correct, 0 is "KEEP" which should
2048 * always be an empty queue (see assertion)! */
2049 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2019 { 2050 {
2020 /* find out which task priority level we are going to 2051 pos = ready_head[p];
2021 process this time */ 2052 if (NULL != pos)
2022 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP; 2053 break;
2023 GNUNET_assert(NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]); 2054 }
2024 /* yes, p>0 is correct, 0 is "KEEP" which should 2055 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2025 * always be an empty queue (see assertion)! */
2026 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2027 {
2028 pos = ready_head[p];
2029 if (NULL != pos)
2030 break;
2031 }
2032 GNUNET_assert(NULL != pos); /* ready_count wrong? */
2033 2056
2034 /* process all tasks at this priority level, then yield */ 2057 /* process all tasks at this priority level, then yield */
2035 while (NULL != (pos = ready_head[p])) 2058 while (NULL != (pos = ready_head[p]))
2036 { 2059 {
2037 GNUNET_CONTAINER_DLL_remove(ready_head[p], 2060 GNUNET_CONTAINER_DLL_remove (ready_head[p],
2038 ready_tail[p], 2061 ready_tail[p],
2039 pos); 2062 pos);
2040 ready_count--; 2063 ready_count--;
2041 current_priority = pos->priority; 2064 current_priority = pos->priority;
2042 current_lifeness = pos->lifeness; 2065 current_lifeness = pos->lifeness;
2043 active_task = pos; 2066 active_task = pos;
2044#if PROFILE_DELAYS 2067#if PROFILE_DELAYS
2045 if (GNUNET_TIME_absolute_get_duration(pos->start_time).rel_value_us > 2068 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2046 DELAY_THRESHOLD.rel_value_us) 2069 DELAY_THRESHOLD.rel_value_us)
2047 { 2070 {
2048 LOG(GNUNET_ERROR_TYPE_DEBUG, 2071 LOG (GNUNET_ERROR_TYPE_DEBUG,
2049 "Task %p took %s to be scheduled\n", 2072 "Task %p took %s to be scheduled\n",
2050 pos, 2073 pos,
2051 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(pos->start_time), 2074 GNUNET_STRINGS_relative_time_to_string (
2052 GNUNET_YES)); 2075 GNUNET_TIME_absolute_get_duration (pos->start_time),
2053 } 2076 GNUNET_YES));
2077 }
2054#endif 2078#endif
2055 tc.reason = pos->reason; 2079 tc.reason = pos->reason;
2056 GNUNET_NETWORK_fdset_zero(sh->rs); 2080 GNUNET_NETWORK_fdset_zero (sh->rs);
2057 GNUNET_NETWORK_fdset_zero(sh->ws); 2081 GNUNET_NETWORK_fdset_zero (sh->ws);
2058 // FIXME: do we have to remove FdInfos from fds if they are not ready? 2082 // FIXME: do we have to remove FdInfos from fds if they are not ready?
2059 tc.fds_len = pos->fds_len; 2083 tc.fds_len = pos->fds_len;
2060 tc.fds = pos->fds; 2084 tc.fds = pos->fds;
2061 for (unsigned int i = 0; i != pos->fds_len; ++i) 2085 for (unsigned int i = 0; i != pos->fds_len; ++i)
2062 { 2086 {
2063 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i]; 2087 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
2064 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) 2088 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
2065 { 2089 {
2066 GNUNET_NETWORK_fdset_set_native(sh->rs, 2090 GNUNET_NETWORK_fdset_set_native (sh->rs,
2067 fdi->sock); 2091 fdi->sock);
2068 }
2069 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2070 {
2071 GNUNET_NETWORK_fdset_set_native(sh->ws,
2072 fdi->sock);
2073 }
2074 }
2075 tc.read_ready = sh->rs;
2076 tc.write_ready = sh->ws;
2077 LOG(GNUNET_ERROR_TYPE_DEBUG,
2078 "Running task %p\n",
2079 pos);
2080 GNUNET_assert(NULL != pos->callback);
2081 {
2082 struct GNUNET_AsyncScopeSave old_scope;
2083 if (pos->scope.have_scope)
2084 GNUNET_async_scope_enter(&pos->scope.scope_id, &old_scope);
2085 else
2086 GNUNET_async_scope_get(&old_scope);
2087 pos->callback(pos->callback_cls);
2088 GNUNET_async_scope_restore(&old_scope);
2089 }
2090 if (NULL != pos->fds)
2091 {
2092 int del_result = scheduler_driver->del(scheduler_driver->cls, pos);
2093 if (GNUNET_OK != del_result)
2094 {
2095 LOG(GNUNET_ERROR_TYPE_ERROR,
2096 "driver could not delete task %p\n", pos);
2097 GNUNET_assert(0);
2098 }
2099 }
2100 active_task = NULL;
2101 dump_backtrace(pos);
2102 destroy_task(pos);
2103 } 2092 }
2093 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2094 {
2095 GNUNET_NETWORK_fdset_set_native (sh->ws,
2096 fdi->sock);
2097 }
2098 }
2099 tc.read_ready = sh->rs;
2100 tc.write_ready = sh->ws;
2101 LOG (GNUNET_ERROR_TYPE_DEBUG,
2102 "Running task %p\n",
2103 pos);
2104 GNUNET_assert (NULL != pos->callback);
2105 {
2106 struct GNUNET_AsyncScopeSave old_scope;
2107 if (pos->scope.have_scope)
2108 GNUNET_async_scope_enter (&pos->scope.scope_id, &old_scope);
2109 else
2110 GNUNET_async_scope_get (&old_scope);
2111 pos->callback (pos->callback_cls);
2112 GNUNET_async_scope_restore (&old_scope);
2113 }
2114 if (NULL != pos->fds)
2115 {
2116 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2117 if (GNUNET_OK != del_result)
2118 {
2119 LOG (GNUNET_ERROR_TYPE_ERROR,
2120 "driver could not delete task %p\n", pos);
2121 GNUNET_assert (0);
2122 }
2123 }
2124 active_task = NULL;
2125 dump_backtrace (pos);
2126 destroy_task (pos);
2104 } 2127 }
2105 shutdown_if_no_lifeness(); 2128 }
2129 shutdown_if_no_lifeness ();
2106 if (0 == ready_count) 2130 if (0 == ready_count)
2107 { 2131 {
2108 scheduler_driver->set_wakeup(scheduler_driver->cls, 2132 scheduler_driver->set_wakeup (scheduler_driver->cls,
2109 get_timeout()); 2133 get_timeout ());
2110 return GNUNET_NO; 2134 return GNUNET_NO;
2111 } 2135 }
2112 scheduler_driver->set_wakeup(scheduler_driver->cls, 2136 scheduler_driver->set_wakeup (scheduler_driver->cls,
2113 GNUNET_TIME_absolute_get()); 2137 GNUNET_TIME_absolute_get ());
2114 return GNUNET_YES; 2138 return GNUNET_YES;
2115} 2139}
2116 2140
@@ -2147,62 +2171,62 @@ GNUNET_SCHEDULER_do_work(struct GNUNET_SCHEDULER_Handle *sh)
2147 * #GNUNET_SCHEDULER_driver_done 2171 * #GNUNET_SCHEDULER_driver_done
2148 */ 2172 */
2149struct GNUNET_SCHEDULER_Handle * 2173struct GNUNET_SCHEDULER_Handle *
2150GNUNET_SCHEDULER_driver_init(const struct GNUNET_SCHEDULER_Driver *driver) 2174GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
2151{ 2175{
2152 struct GNUNET_SCHEDULER_Handle *sh; 2176 struct GNUNET_SCHEDULER_Handle *sh;
2153 const struct GNUNET_DISK_FileHandle *pr; 2177 const struct GNUNET_DISK_FileHandle *pr;
2154 2178
2155 /* scheduler must not be running */ 2179 /* scheduler must not be running */
2156 GNUNET_assert(NULL == scheduler_driver); 2180 GNUNET_assert (NULL == scheduler_driver);
2157 GNUNET_assert(NULL == shutdown_pipe_handle); 2181 GNUNET_assert (NULL == shutdown_pipe_handle);
2158 /* general set-up */ 2182 /* general set-up */
2159 sh = GNUNET_new(struct GNUNET_SCHEDULER_Handle); 2183 sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
2160 shutdown_pipe_handle = GNUNET_DISK_pipe(GNUNET_NO, 2184 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2161 GNUNET_NO, 2185 GNUNET_NO,
2162 GNUNET_NO, 2186 GNUNET_NO,
2163 GNUNET_NO); 2187 GNUNET_NO);
2164 GNUNET_assert(NULL != shutdown_pipe_handle); 2188 GNUNET_assert (NULL != shutdown_pipe_handle);
2165 pr = GNUNET_DISK_pipe_handle(shutdown_pipe_handle, 2189 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2166 GNUNET_DISK_PIPE_END_READ); 2190 GNUNET_DISK_PIPE_END_READ);
2167 my_pid = getpid(); 2191 my_pid = getpid ();
2168 scheduler_driver = driver; 2192 scheduler_driver = driver;
2169 2193
2170 /* install signal handlers */ 2194 /* install signal handlers */
2171 LOG(GNUNET_ERROR_TYPE_DEBUG, 2195 LOG (GNUNET_ERROR_TYPE_DEBUG,
2172 "Registering signal handlers\n"); 2196 "Registering signal handlers\n");
2173 sh->shc_int = GNUNET_SIGNAL_handler_install(SIGINT, 2197 sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2174 &sighandler_shutdown);
2175 sh->shc_term = GNUNET_SIGNAL_handler_install(SIGTERM,
2176 &sighandler_shutdown); 2198 &sighandler_shutdown);
2177#if (SIGTERM != GNUNET_TERM_SIG) 2199 sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2178 sh->shc_gterm = GNUNET_SIGNAL_handler_install(GNUNET_TERM_SIG,
2179 &sighandler_shutdown); 2200 &sighandler_shutdown);
2201#if (SIGTERM != GNUNET_TERM_SIG)
2202 sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2203 &sighandler_shutdown);
2180#endif 2204#endif
2181 sh->shc_pipe = GNUNET_SIGNAL_handler_install(SIGPIPE, 2205 sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2182 &sighandler_pipe); 2206 &sighandler_pipe);
2183 sh->shc_quit = GNUNET_SIGNAL_handler_install(SIGQUIT, 2207 sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2208 &sighandler_shutdown);
2209 sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2184 &sighandler_shutdown); 2210 &sighandler_shutdown);
2185 sh->shc_hup = GNUNET_SIGNAL_handler_install(SIGHUP,
2186 &sighandler_shutdown);
2187 2211
2188 /* Setup initial tasks */ 2212 /* Setup initial tasks */
2189 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT; 2213 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2190 current_lifeness = GNUNET_NO; 2214 current_lifeness = GNUNET_NO;
2191 install_parent_control_task = 2215 install_parent_control_task =
2192 GNUNET_SCHEDULER_add_now(&install_parent_control_handler, 2216 GNUNET_SCHEDULER_add_now (&install_parent_control_handler,
2193 NULL); 2217 NULL);
2194 shutdown_pipe_task = 2218 shutdown_pipe_task =
2195 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 2219 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2196 pr, 2220 pr,
2197 &shutdown_pipe_cb, 2221 &shutdown_pipe_cb,
2198 NULL); 2222 NULL);
2199 current_lifeness = GNUNET_YES; 2223 current_lifeness = GNUNET_YES;
2200 scheduler_driver->set_wakeup(scheduler_driver->cls, 2224 scheduler_driver->set_wakeup (scheduler_driver->cls,
2201 get_timeout()); 2225 get_timeout ());
2202 /* begin main event loop */ 2226 /* begin main event loop */
2203 sh->rs = GNUNET_NETWORK_fdset_create(); 2227 sh->rs = GNUNET_NETWORK_fdset_create ();
2204 sh->ws = GNUNET_NETWORK_fdset_create(); 2228 sh->ws = GNUNET_NETWORK_fdset_create ();
2205 GNUNET_NETWORK_fdset_handle_set(sh->rs, pr); 2229 GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
2206 return sh; 2230 return sh;
2207} 2231}
2208 2232
@@ -2222,241 +2246,242 @@ GNUNET_SCHEDULER_driver_init(const struct GNUNET_SCHEDULER_Driver *driver)
2222 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init 2246 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
2223 */ 2247 */
2224void 2248void
2225GNUNET_SCHEDULER_driver_done(struct GNUNET_SCHEDULER_Handle *sh) 2249GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
2226{ 2250{
2227 GNUNET_assert(NULL == pending_head); 2251 GNUNET_assert (NULL == pending_head);
2228 GNUNET_assert(NULL == pending_timeout_head); 2252 GNUNET_assert (NULL == pending_timeout_head);
2229 GNUNET_assert(NULL == shutdown_head); 2253 GNUNET_assert (NULL == shutdown_head);
2230 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i) 2254 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2231 { 2255 {
2232 GNUNET_assert(NULL == ready_head[i]); 2256 GNUNET_assert (NULL == ready_head[i]);
2233 } 2257 }
2234 GNUNET_NETWORK_fdset_destroy(sh->rs); 2258 GNUNET_NETWORK_fdset_destroy (sh->rs);
2235 GNUNET_NETWORK_fdset_destroy(sh->ws); 2259 GNUNET_NETWORK_fdset_destroy (sh->ws);
2236 2260
2237 /* uninstall signal handlers */ 2261 /* uninstall signal handlers */
2238 GNUNET_SIGNAL_handler_uninstall(sh->shc_int); 2262 GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
2239 GNUNET_SIGNAL_handler_uninstall(sh->shc_term); 2263 GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
2240#if (SIGTERM != GNUNET_TERM_SIG) 2264#if (SIGTERM != GNUNET_TERM_SIG)
2241 GNUNET_SIGNAL_handler_uninstall(sh->shc_gterm); 2265 GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
2242#endif 2266#endif
2243 GNUNET_SIGNAL_handler_uninstall(sh->shc_pipe); 2267 GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
2244 GNUNET_SIGNAL_handler_uninstall(sh->shc_quit); 2268 GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
2245 GNUNET_SIGNAL_handler_uninstall(sh->shc_hup); 2269 GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
2246 GNUNET_DISK_pipe_close(shutdown_pipe_handle); 2270 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2247 shutdown_pipe_handle = NULL; 2271 shutdown_pipe_handle = NULL;
2248 scheduler_driver = NULL; 2272 scheduler_driver = NULL;
2249 GNUNET_free(sh); 2273 GNUNET_free (sh);
2250} 2274}
2251 2275
2252 2276
2253static int 2277static int
2254select_loop(struct GNUNET_SCHEDULER_Handle *sh, 2278select_loop (struct GNUNET_SCHEDULER_Handle *sh,
2255 struct DriverContext *context) 2279 struct DriverContext *context)
2256{ 2280{
2257 struct GNUNET_NETWORK_FDSet *rs; 2281 struct GNUNET_NETWORK_FDSet *rs;
2258 struct GNUNET_NETWORK_FDSet *ws; 2282 struct GNUNET_NETWORK_FDSet *ws;
2259 int select_result; 2283 int select_result;
2260 2284
2261 GNUNET_assert(NULL != context); 2285 GNUNET_assert (NULL != context);
2262 rs = GNUNET_NETWORK_fdset_create(); 2286 rs = GNUNET_NETWORK_fdset_create ();
2263 ws = GNUNET_NETWORK_fdset_create(); 2287 ws = GNUNET_NETWORK_fdset_create ();
2264 while ((NULL != context->scheduled_head) || 2288 while ((NULL != context->scheduled_head) ||
2265 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != context->timeout.abs_value_us)) 2289 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
2290 context->timeout.abs_value_us))
2291 {
2292 LOG (GNUNET_ERROR_TYPE_DEBUG,
2293 "select timeout = %s\n",
2294 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2295
2296 GNUNET_NETWORK_fdset_zero (rs);
2297 GNUNET_NETWORK_fdset_zero (ws);
2298
2299 for (struct Scheduled *pos = context->scheduled_head;
2300 NULL != pos;
2301 pos = pos->next)
2266 { 2302 {
2267 LOG(GNUNET_ERROR_TYPE_DEBUG, 2303 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2268 "select timeout = %s\n", 2304 {
2269 GNUNET_STRINGS_absolute_time_to_string(context->timeout)); 2305 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2270 2306 }
2271 GNUNET_NETWORK_fdset_zero(rs); 2307 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2272 GNUNET_NETWORK_fdset_zero(ws); 2308 {
2273 2309 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2274 for (struct Scheduled *pos = context->scheduled_head; 2310 }
2275 NULL != pos; 2311 }
2276 pos = pos->next) 2312 struct GNUNET_TIME_Relative time_remaining =
2277 { 2313 GNUNET_TIME_absolute_get_remaining (context->timeout);
2278 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et)) 2314 if (NULL == scheduler_select)
2279 { 2315 {
2280 GNUNET_NETWORK_fdset_set_native(rs, pos->fdi->sock); 2316 select_result = GNUNET_NETWORK_socket_select (rs,
2281 } 2317 ws,
2282 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et)) 2318 NULL,
2283 { 2319 time_remaining);
2284 GNUNET_NETWORK_fdset_set_native(ws, pos->fdi->sock); 2320 }
2285 } 2321 else
2286 } 2322 {
2287 struct GNUNET_TIME_Relative time_remaining = 2323 select_result = scheduler_select (scheduler_select_cls,
2288 GNUNET_TIME_absolute_get_remaining(context->timeout); 2324 rs,
2289 if (NULL == scheduler_select) 2325 ws,
2290 { 2326 NULL,
2291 select_result = GNUNET_NETWORK_socket_select(rs, 2327 time_remaining);
2292 ws, 2328 }
2293 NULL, 2329 if (select_result == GNUNET_SYSERR)
2294 time_remaining); 2330 {
2295 } 2331 if (errno == EINTR)
2296 else 2332 continue;
2297 {
2298 select_result = scheduler_select(scheduler_select_cls,
2299 rs,
2300 ws,
2301 NULL,
2302 time_remaining);
2303 }
2304 if (select_result == GNUNET_SYSERR)
2305 {
2306 if (errno == EINTR)
2307 continue;
2308 2333
2309 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, 2334 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
2310 "select"); 2335 "select");
2311#if USE_LSOF 2336#if USE_LSOF
2312 char lsof[512]; 2337 char lsof[512];
2313 2338
2314 snprintf(lsof, 2339 snprintf (lsof,
2315 sizeof(lsof), 2340 sizeof(lsof),
2316 "lsof -p %d", 2341 "lsof -p %d",
2317 getpid()); 2342 getpid ());
2318 (void)close(1); 2343 (void) close (1);
2319 (void)dup2(2, 1); 2344 (void) dup2 (2, 1);
2320 if (0 != system(lsof)) 2345 if (0 != system (lsof))
2321 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 2346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2322 "system"); 2347 "system");
2323#endif 2348#endif
2324#if DEBUG_FDS 2349#if DEBUG_FDS
2325 for (struct Scheduled *s = context->scheduled_head; 2350 for (struct Scheduled *s = context->scheduled_head;
2326 NULL != s; 2351 NULL != s;
2327 s = s->next) 2352 s = s->next)
2328 { 2353 {
2329 int flags = fcntl(s->fdi->sock, 2354 int flags = fcntl (s->fdi->sock,
2330 F_GETFD); 2355 F_GETFD);
2331 2356
2332 if ((flags == -1) && 2357 if ((flags == -1) &&
2333 (EBADF == errno)) 2358 (EBADF == errno))
2334 { 2359 {
2335 LOG(GNUNET_ERROR_TYPE_ERROR, 2360 LOG (GNUNET_ERROR_TYPE_ERROR,
2336 "Got invalid file descriptor %d!\n", 2361 "Got invalid file descriptor %d!\n",
2337 s->fdi->sock); 2362 s->fdi->sock);
2338#if EXECINFO 2363#if EXECINFO
2339 dump_backtrace(s->task); 2364 dump_backtrace (s->task);
2340#endif 2365#endif
2341 } 2366 }
2342 } 2367 }
2343#endif 2368#endif
2344 GNUNET_assert(0); 2369 GNUNET_assert (0);
2345 GNUNET_NETWORK_fdset_destroy(rs); 2370 GNUNET_NETWORK_fdset_destroy (rs);
2346 GNUNET_NETWORK_fdset_destroy(ws); 2371 GNUNET_NETWORK_fdset_destroy (ws);
2347 return GNUNET_SYSERR; 2372 return GNUNET_SYSERR;
2373 }
2374 if (select_result > 0)
2375 {
2376 for (struct Scheduled *pos = context->scheduled_head;
2377 NULL != pos;
2378 pos = pos->next)
2379 {
2380 int is_ready = GNUNET_NO;
2381
2382 if ((0 != (GNUNET_SCHEDULER_ET_IN & pos->et)) &&
2383 (GNUNET_YES ==
2384 GNUNET_NETWORK_fdset_test_native (rs,
2385 pos->fdi->sock)) )
2386 {
2387 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2388 is_ready = GNUNET_YES;
2348 } 2389 }
2349 if (select_result > 0) 2390 if ((0 != (GNUNET_SCHEDULER_ET_OUT & pos->et)) &&
2391 (GNUNET_YES ==
2392 GNUNET_NETWORK_fdset_test_native (ws,
2393 pos->fdi->sock)) )
2350 { 2394 {
2351 for (struct Scheduled *pos = context->scheduled_head; 2395 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2352 NULL != pos; 2396 is_ready = GNUNET_YES;
2353 pos = pos->next)
2354 {
2355 int is_ready = GNUNET_NO;
2356
2357 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2358 GNUNET_YES ==
2359 GNUNET_NETWORK_fdset_test_native(rs,
2360 pos->fdi->sock))
2361 {
2362 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2363 is_ready = GNUNET_YES;
2364 }
2365 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2366 GNUNET_YES ==
2367 GNUNET_NETWORK_fdset_test_native(ws,
2368 pos->fdi->sock))
2369 {
2370 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2371 is_ready = GNUNET_YES;
2372 }
2373 if (GNUNET_YES == is_ready)
2374 {
2375 GNUNET_SCHEDULER_task_ready(pos->task,
2376 pos->fdi);
2377 }
2378 }
2379 } 2397 }
2380 if (GNUNET_YES == GNUNET_SCHEDULER_do_work(sh)) 2398 if (GNUNET_YES == is_ready)
2381 { 2399 {
2382 LOG(GNUNET_ERROR_TYPE_DEBUG, 2400 GNUNET_SCHEDULER_task_ready (pos->task,
2383 "scheduler has more tasks ready!\n"); 2401 pos->fdi);
2384 } 2402 }
2403 }
2404 }
2405 if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
2406 {
2407 LOG (GNUNET_ERROR_TYPE_DEBUG,
2408 "scheduler has more tasks ready!\n");
2385 } 2409 }
2386 GNUNET_NETWORK_fdset_destroy(rs); 2410 }
2387 GNUNET_NETWORK_fdset_destroy(ws); 2411 GNUNET_NETWORK_fdset_destroy (rs);
2412 GNUNET_NETWORK_fdset_destroy (ws);
2388 return GNUNET_OK; 2413 return GNUNET_OK;
2389} 2414}
2390 2415
2391 2416
2392static int 2417static int
2393select_add(void *cls, 2418select_add (void *cls,
2394 struct GNUNET_SCHEDULER_Task *task, 2419 struct GNUNET_SCHEDULER_Task *task,
2395 struct GNUNET_SCHEDULER_FdInfo *fdi) 2420 struct GNUNET_SCHEDULER_FdInfo *fdi)
2396{ 2421{
2397 struct DriverContext *context = cls; 2422 struct DriverContext *context = cls;
2398 2423
2399 GNUNET_assert(NULL != context); 2424 GNUNET_assert (NULL != context);
2400 GNUNET_assert(NULL != task); 2425 GNUNET_assert (NULL != task);
2401 GNUNET_assert(NULL != fdi); 2426 GNUNET_assert (NULL != fdi);
2402 GNUNET_assert(0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) || 2427 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2403 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)); 2428 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2404 2429
2405 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0)) 2430 if (! ((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2406 { 2431 {
2407 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */ 2432 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2408 return GNUNET_SYSERR; 2433 return GNUNET_SYSERR;
2409 } 2434 }
2410 2435
2411 struct Scheduled *scheduled = GNUNET_new(struct Scheduled); 2436 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2412 scheduled->task = task; 2437 scheduled->task = task;
2413 scheduled->fdi = fdi; 2438 scheduled->fdi = fdi;
2414 scheduled->et = fdi->et; 2439 scheduled->et = fdi->et;
2415 2440
2416 GNUNET_CONTAINER_DLL_insert(context->scheduled_head, 2441 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2417 context->scheduled_tail, 2442 context->scheduled_tail,
2418 scheduled); 2443 scheduled);
2419 return GNUNET_OK; 2444 return GNUNET_OK;
2420} 2445}
2421 2446
2422 2447
2423static int 2448static int
2424select_del(void *cls, 2449select_del (void *cls,
2425 struct GNUNET_SCHEDULER_Task *task) 2450 struct GNUNET_SCHEDULER_Task *task)
2426{ 2451{
2427 struct DriverContext *context; 2452 struct DriverContext *context;
2428 struct Scheduled *pos; 2453 struct Scheduled *pos;
2429 int ret; 2454 int ret;
2430 2455
2431 GNUNET_assert(NULL != cls); 2456 GNUNET_assert (NULL != cls);
2432 2457
2433 context = cls; 2458 context = cls;
2434 ret = GNUNET_SYSERR; 2459 ret = GNUNET_SYSERR;
2435 pos = context->scheduled_head; 2460 pos = context->scheduled_head;
2436 while (NULL != pos) 2461 while (NULL != pos)
2462 {
2463 struct Scheduled *next = pos->next;
2464 if (pos->task == task)
2437 { 2465 {
2438 struct Scheduled *next = pos->next; 2466 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2439 if (pos->task == task) 2467 context->scheduled_tail,
2440 { 2468 pos);
2441 GNUNET_CONTAINER_DLL_remove(context->scheduled_head, 2469 GNUNET_free (pos);
2442 context->scheduled_tail, 2470 ret = GNUNET_OK;
2443 pos);
2444 GNUNET_free(pos);
2445 ret = GNUNET_OK;
2446 }
2447 pos = next;
2448 } 2471 }
2472 pos = next;
2473 }
2449 return ret; 2474 return ret;
2450} 2475}
2451 2476
2452 2477
2453static void 2478static void
2454select_set_wakeup(void *cls, 2479select_set_wakeup (void *cls,
2455 struct GNUNET_TIME_Absolute dt) 2480 struct GNUNET_TIME_Absolute dt)
2456{ 2481{
2457 struct DriverContext *context = cls; 2482 struct DriverContext *context = cls;
2458 2483
2459 GNUNET_assert(NULL != context); 2484 GNUNET_assert (NULL != context);
2460 context->timeout = dt; 2485 context->timeout = dt;
2461} 2486}
2462 2487
@@ -2467,11 +2492,11 @@ select_set_wakeup(void *cls,
2467 * @return NULL on error 2492 * @return NULL on error
2468 */ 2493 */
2469struct GNUNET_SCHEDULER_Driver * 2494struct GNUNET_SCHEDULER_Driver *
2470GNUNET_SCHEDULER_driver_select() 2495GNUNET_SCHEDULER_driver_select ()
2471{ 2496{
2472 struct GNUNET_SCHEDULER_Driver *select_driver; 2497 struct GNUNET_SCHEDULER_Driver *select_driver;
2473 2498
2474 select_driver = GNUNET_new(struct GNUNET_SCHEDULER_Driver); 2499 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2475 2500
2476 select_driver->add = &select_add; 2501 select_driver->add = &select_add;
2477 select_driver->del = &select_del; 2502 select_driver->del = &select_del;
@@ -2494,14 +2519,14 @@ GNUNET_SCHEDULER_driver_select()
2494 * @param aid the asynchronous scope id to enter 2519 * @param aid the asynchronous scope id to enter
2495 */ 2520 */
2496void 2521void
2497GNUNET_SCHEDULER_begin_async_scope(struct GNUNET_AsyncScopeId *aid) 2522GNUNET_SCHEDULER_begin_async_scope (struct GNUNET_AsyncScopeId *aid)
2498{ 2523{
2499 struct GNUNET_AsyncScopeSave dummy_old_scope; 2524 struct GNUNET_AsyncScopeSave dummy_old_scope;
2500 2525
2501 GNUNET_assert(NULL != active_task); 2526 GNUNET_assert (NULL != active_task);
2502 /* Since we're in a task, the context will be automatically 2527 /* Since we're in a task, the context will be automatically
2503 restored by the scheduler. */ 2528 restored by the scheduler. */
2504 GNUNET_async_scope_enter(aid, &dummy_old_scope); 2529 GNUNET_async_scope_enter (aid, &dummy_old_scope);
2505} 2530}
2506 2531
2507 2532
diff --git a/src/util/signal.c b/src/util/signal.c
index ada4db5ad..fd7eecdd3 100644
--- a/src/util/signal.c
+++ b/src/util/signal.c
@@ -27,10 +27,11 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-signal", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-signal", __VA_ARGS__)
31 31
32 32
33struct GNUNET_SIGNAL_Context { 33struct GNUNET_SIGNAL_Context
34{
34 struct GNUNET_SIGNAL_Context *next; 35 struct GNUNET_SIGNAL_Context *next;
35 36
36 struct GNUNET_SIGNAL_Context *prev; 37 struct GNUNET_SIGNAL_Context *prev;
@@ -47,40 +48,40 @@ static struct GNUNET_SIGNAL_Context *sc_head;
47static struct GNUNET_SIGNAL_Context *sc_tail; 48static struct GNUNET_SIGNAL_Context *sc_tail;
48 49
49struct GNUNET_SIGNAL_Context * 50struct GNUNET_SIGNAL_Context *
50GNUNET_SIGNAL_handler_install(int signum, GNUNET_SIGNAL_Handler handler) 51GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler)
51{ 52{
52 struct GNUNET_SIGNAL_Context *ret; 53 struct GNUNET_SIGNAL_Context *ret;
53 54
54 struct sigaction sig; 55 struct sigaction sig;
55 56
56 ret = GNUNET_new(struct GNUNET_SIGNAL_Context); 57 ret = GNUNET_new (struct GNUNET_SIGNAL_Context);
57 ret->sig = signum; 58 ret->sig = signum;
58 ret->method = handler; 59 ret->method = handler;
59 60
60 memset(&sig, 0, sizeof(sig)); 61 memset (&sig, 0, sizeof(sig));
61 sig.sa_handler = (void *)handler; 62 sig.sa_handler = (void *) handler;
62 sigemptyset(&sig.sa_mask); 63 sigemptyset (&sig.sa_mask);
63#ifdef SA_INTERRUPT 64#ifdef SA_INTERRUPT
64 sig.sa_flags = SA_INTERRUPT; /* SunOS */ 65 sig.sa_flags = SA_INTERRUPT; /* SunOS */
65#else 66#else
66 sig.sa_flags = SA_RESTART; 67 sig.sa_flags = SA_RESTART;
67#endif 68#endif
68 sigaction(signum, &sig, &ret->oldsig); 69 sigaction (signum, &sig, &ret->oldsig);
69 70
70 GNUNET_CONTAINER_DLL_insert_tail(sc_head, sc_tail, ret); 71 GNUNET_CONTAINER_DLL_insert_tail (sc_head, sc_tail, ret);
71 return ret; 72 return ret;
72} 73}
73 74
74void 75void
75GNUNET_SIGNAL_handler_uninstall(struct GNUNET_SIGNAL_Context *ctx) 76GNUNET_SIGNAL_handler_uninstall (struct GNUNET_SIGNAL_Context *ctx)
76{ 77{
77 struct sigaction sig; 78 struct sigaction sig;
78 79
79 sigemptyset(&sig.sa_mask); 80 sigemptyset (&sig.sa_mask);
80 sigaction(ctx->sig, &ctx->oldsig, &sig); 81 sigaction (ctx->sig, &ctx->oldsig, &sig);
81 82
82 GNUNET_CONTAINER_DLL_remove(sc_head, sc_tail, ctx); 83 GNUNET_CONTAINER_DLL_remove (sc_head, sc_tail, ctx);
83 GNUNET_free(ctx); 84 GNUNET_free (ctx);
84} 85}
85 86
86 87
@@ -92,16 +93,16 @@ GNUNET_SIGNAL_handler_uninstall(struct GNUNET_SIGNAL_Context *ctx)
92 * @param sig the signal to raise 93 * @param sig the signal to raise
93 */ 94 */
94void 95void
95GNUNET_SIGNAL_raise(const int sig) 96GNUNET_SIGNAL_raise (const int sig)
96{ 97{
97 struct GNUNET_SIGNAL_Context *ctx; 98 struct GNUNET_SIGNAL_Context *ctx;
98 99
99 for (ctx = sc_head; NULL != ctx; ctx = ctx->next) 100 for (ctx = sc_head; NULL != ctx; ctx = ctx->next)
100 { 101 {
101 if (sig != ctx->sig) 102 if (sig != ctx->sig)
102 continue; 103 continue;
103 if (NULL == ctx->method) 104 if (NULL == ctx->method)
104 continue; 105 continue;
105 ctx->method(); 106 ctx->method ();
106 } 107 }
107} 108}
diff --git a/src/util/socks.c b/src/util/socks.c
index ca3d1e8c4..c2e012f1c 100644
--- a/src/util/socks.c
+++ b/src/util/socks.c
@@ -29,10 +29,10 @@
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30 30
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-socks", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-socks", __VA_ARGS__)
33 33
34#define LOG_STRERROR(kind, syscall) \ 34#define LOG_STRERROR(kind, syscall) \
35 GNUNET_log_from_strerror(kind, "util-socks", syscall) 35 GNUNET_log_from_strerror (kind, "util-socks", syscall)
36 36
37 37
38/* SOCKS5 authentication methods */ 38/* SOCKS5 authentication methods */
@@ -58,43 +58,43 @@
58#define SOCKS5_REP_INVADDR 0x09 /* Inalid address */ 58#define SOCKS5_REP_INVADDR 0x09 /* Inalid address */
59 59
60const char * 60const char *
61SOCKS5_REP_names(int rep) 61SOCKS5_REP_names (int rep)
62{ 62{
63 switch (rep) 63 switch (rep)
64 { 64 {
65 case SOCKS5_REP_SUCCEEDED: 65 case SOCKS5_REP_SUCCEEDED:
66 return "succeeded"; 66 return "succeeded";
67 67
68 case SOCKS5_REP_FAIL: 68 case SOCKS5_REP_FAIL:
69 return "general SOCKS server failure"; 69 return "general SOCKS server failure";
70 70
71 case SOCKS5_REP_NALLOWED: 71 case SOCKS5_REP_NALLOWED:
72 return "connection not allowed by ruleset"; 72 return "connection not allowed by ruleset";
73 73
74 case SOCKS5_REP_NUNREACH: 74 case SOCKS5_REP_NUNREACH:
75 return "Network unreachable"; 75 return "Network unreachable";
76 76
77 case SOCKS5_REP_HUNREACH: 77 case SOCKS5_REP_HUNREACH:
78 return "Host unreachable"; 78 return "Host unreachable";
79 79
80 case SOCKS5_REP_REFUSED: 80 case SOCKS5_REP_REFUSED:
81 return "connection refused"; 81 return "connection refused";
82 82
83 case SOCKS5_REP_EXPIRED: 83 case SOCKS5_REP_EXPIRED:
84 return "TTL expired"; 84 return "TTL expired";
85 85
86 case SOCKS5_REP_CNOTSUP: 86 case SOCKS5_REP_CNOTSUP:
87 return "Command not supported"; 87 return "Command not supported";
88 88
89 case SOCKS5_REP_ANOTSUP: 89 case SOCKS5_REP_ANOTSUP:
90 return "Address not supported"; 90 return "Address not supported";
91 91
92 case SOCKS5_REP_INVADDR: 92 case SOCKS5_REP_INVADDR:
93 return "Invalid address"; 93 return "Invalid address";
94 94
95 default: 95 default:
96 return NULL; 96 return NULL;
97 } 97 }
98}; 98};
99 99
100 100
@@ -108,18 +108,18 @@ SOCKS5_REP_names(int rep)
108 * @return pointer to the end of the encoded string in the buffer 108 * @return pointer to the end of the encoded string in the buffer
109 */ 109 */
110unsigned char * 110unsigned char *
111SOCK5_proto_string(unsigned char *b, const char *s) 111SOCK5_proto_string (unsigned char *b, const char *s)
112{ 112{
113 size_t l = strlen(s); 113 size_t l = strlen (s);
114 114
115 if (l > 255) 115 if (l > 255)
116 { 116 {
117 LOG(GNUNET_ERROR_TYPE_WARNING, 117 LOG (GNUNET_ERROR_TYPE_WARNING,
118 "SOCKS5 cannot handle hostnames, usernames, or passwords over 255 bytes, truncating.\n"); 118 "SOCKS5 cannot handle hostnames, usernames, or passwords over 255 bytes, truncating.\n");
119 l = 255; 119 l = 255;
120 } 120 }
121 *(b++) = (unsigned char)l; 121 *(b++) = (unsigned char) l;
122 memcpy(b, s, l); 122 memcpy (b, s, l);
123 return b + l; 123 return b + l;
124} 124}
125 125
@@ -132,7 +132,8 @@ SOCK5_proto_string(unsigned char *b, const char *s)
132/** 132/**
133 * State of the SOCKS5 handshake. 133 * State of the SOCKS5 handshake.
134 */ 134 */
135struct GNUNET_SOCKS_Handshake { 135struct GNUNET_SOCKS_Handshake
136{
136 /** 137 /**
137 * Connection handle used for SOCKS5 138 * Connection handle used for SOCKS5
138 */ 139 */
@@ -179,7 +180,7 @@ struct GNUNET_SOCKS_Handshake {
179/* Regitering prototypes */ 180/* Regitering prototypes */
180 181
181void 182void
182register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want); 183register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want);
183 184
184/* In fact, the client sends first rule in GNUnet suggests one could take 185/* In fact, the client sends first rule in GNUnet suggests one could take
185 * large mac read sizes without fear of screwing up the proxied protocol, 186 * large mac read sizes without fear of screwing up the proxied protocol,
@@ -188,7 +189,7 @@ register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want);
188 189
189 190
190struct GNUNET_CONNECTION_TransmitHandle * 191struct GNUNET_CONNECTION_TransmitHandle *
191register_sender(struct GNUNET_SOCKS_Handshake *ih); 192register_sender (struct GNUNET_SOCKS_Handshake *ih);
192 193
193 194
194/** 195/**
@@ -199,9 +200,9 @@ register_sender(struct GNUNET_SOCKS_Handshake *ih);
199 * @return Connection handle that becomes usable when the handshake completes. 200 * @return Connection handle that becomes usable when the handshake completes.
200 */ 201 */
201void 202void
202SOCKS5_handshake_done(struct GNUNET_SOCKS_Handshake *ih) 203SOCKS5_handshake_done (struct GNUNET_SOCKS_Handshake *ih)
203{ 204{
204 GNUNET_CONNECTION_acivate_proxied(ih->target_connection); 205 GNUNET_CONNECTION_acivate_proxied (ih->target_connection);
205} 206}
206 207
207 208
@@ -211,109 +212,109 @@ SOCKS5_handshake_done(struct GNUNET_SOCKS_Handshake *ih)
211 * @param ih SOCKS5 Handshake 212 * @param ih SOCKS5 Handshake
212 */ 213 */
213void 214void
214SOCKS5_handshake_step(struct GNUNET_SOCKS_Handshake *ih) 215SOCKS5_handshake_step (struct GNUNET_SOCKS_Handshake *ih)
215{ 216{
216 unsigned char *b = ih->instart; 217 unsigned char *b = ih->instart;
217 size_t available = ih->inend - b; 218 size_t available = ih->inend - b;
218 219
219 int want = register_reciever_wants(ih); 220 int want = register_reciever_wants (ih);
220 221
221 if (available < want) 222 if (available < want)
223 {
224 register_reciever (ih, want - available);
225 return;
226 }
227 GNUNET_assert (SOCKS5_step_done > ih->step && ih->step >= 0);
228 switch (ih->step)
229 {
230 case SOCKS5_step_greet: /* SOCKS5 server's greeting */
231 if (b[0] != 5)
222 { 232 {
223 register_reciever(ih, want - available); 233 LOG (GNUNET_ERROR_TYPE_ERROR, "Not a SOCKS5 server\n");
224 return; 234 GNUNET_assert (0);
225 } 235 }
226 GNUNET_assert(SOCKS5_step_done > ih->step && ih->step >= 0); 236 switch (b[1])
227 switch (ih->step)
228 { 237 {
229 case SOCKS5_step_greet: /* SOCKS5 server's greeting */ 238 case SOCKS5_AUTH_NOAUTH:
230 if (b[0] != 5) 239 ih->step = SOCKS5_step_cmd; /* no authentication to do */
231 {
232 LOG(GNUNET_ERROR_TYPE_ERROR, "Not a SOCKS5 server\n");
233 GNUNET_assert(0);
234 }
235 switch (b[1])
236 {
237 case SOCKS5_AUTH_NOAUTH:
238 ih->step = SOCKS5_step_cmd; /* no authentication to do */
239 break;
240
241 case SOCKS5_AUTH_USERPASS:
242 ih->step = SOCKS5_step_auth;
243 break;
244
245 case SOCKS5_AUTH_REJECT:
246 LOG(GNUNET_ERROR_TYPE_ERROR, "No authentication method accepted\n");
247 return;
248
249 default:
250 LOG(GNUNET_ERROR_TYPE_ERROR,
251 "Not a SOCKS5 server / Nonsensical authentication\n");
252 return;
253 }
254 b += 2;
255 break; 240 break;
256 241
257 case SOCKS5_step_auth: /* SOCKS5 server's responce to authentication */ 242 case SOCKS5_AUTH_USERPASS:
258 if (b[1] != 0) 243 ih->step = SOCKS5_step_auth;
259 {
260 LOG(GNUNET_ERROR_TYPE_ERROR, "SOCKS5 authentication failed\n");
261 GNUNET_assert(0);
262 }
263 ih->step = SOCKS5_step_cmd;
264 b += 2;
265 break; 244 break;
266 245
267 case SOCKS5_step_cmd: /* SOCKS5 server's responce to command */ 246 case SOCKS5_AUTH_REJECT:
268 if (b[0] != 5) 247 LOG (GNUNET_ERROR_TYPE_ERROR, "No authentication method accepted\n");
269 { 248 return;
270 LOG(GNUNET_ERROR_TYPE_ERROR, "SOCKS5 protocol error\n"); 249
271 GNUNET_assert(0); 250 default:
272 } 251 LOG (GNUNET_ERROR_TYPE_ERROR,
273 if (0 != b[1]) 252 "Not a SOCKS5 server / Nonsensical authentication\n");
274 {
275 LOG(GNUNET_ERROR_TYPE_ERROR,
276 "SOCKS5 connection error : %s\n",
277 SOCKS5_REP_names(b[1]));
278 return;
279 }
280 b += 3;
281 /* There is no reason to verify host and port afaik. */
282 switch (*(b++))
283 {
284 case 1: /* IPv4 */
285 b += sizeof(struct in_addr); /* 4 */
286 break;
287
288 case 4: /* IPv6 */
289 b += sizeof(struct in6_addr); /* 16 */
290 break;
291
292 case 3: /* hostname */
293 b += *b;
294 break;
295 }
296 b += 2; /* port */
297 if (b > ih->inend)
298 {
299 register_reciever(ih, b - ih->inend);
300 return;
301 }
302 ih->step = SOCKS5_step_done;
303 LOG(GNUNET_ERROR_TYPE_DEBUG,
304 "SOCKS5 server : %s\n",
305 SOCKS5_REP_names(b[1]));
306 ih->instart = b;
307 SOCKS5_handshake_done(ih);
308 return; 253 return;
254 }
255 b += 2;
256 break;
309 257
310 case SOCKS5_step_done: 258 case SOCKS5_step_auth: /* SOCKS5 server's responce to authentication */
311 GNUNET_assert(0); 259 if (b[1] != 0)
260 {
261 LOG (GNUNET_ERROR_TYPE_ERROR, "SOCKS5 authentication failed\n");
262 GNUNET_assert (0);
312 } 263 }
264 ih->step = SOCKS5_step_cmd;
265 b += 2;
266 break;
267
268 case SOCKS5_step_cmd: /* SOCKS5 server's responce to command */
269 if (b[0] != 5)
270 {
271 LOG (GNUNET_ERROR_TYPE_ERROR, "SOCKS5 protocol error\n");
272 GNUNET_assert (0);
273 }
274 if (0 != b[1])
275 {
276 LOG (GNUNET_ERROR_TYPE_ERROR,
277 "SOCKS5 connection error : %s\n",
278 SOCKS5_REP_names (b[1]));
279 return;
280 }
281 b += 3;
282 /* There is no reason to verify host and port afaik. */
283 switch (*(b++))
284 {
285 case 1: /* IPv4 */
286 b += sizeof(struct in_addr); /* 4 */
287 break;
288
289 case 4: /* IPv6 */
290 b += sizeof(struct in6_addr); /* 16 */
291 break;
292
293 case 3: /* hostname */
294 b += *b;
295 break;
296 }
297 b += 2; /* port */
298 if (b > ih->inend)
299 {
300 register_reciever (ih, b - ih->inend);
301 return;
302 }
303 ih->step = SOCKS5_step_done;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "SOCKS5 server : %s\n",
306 SOCKS5_REP_names (b[1]));
307 ih->instart = b;
308 SOCKS5_handshake_done (ih);
309 return;
310
311 case SOCKS5_step_done:
312 GNUNET_assert (0);
313 }
313 ih->instart = b; 314 ih->instart = b;
314 /* Do not reschedule the sender unless we're done reading. 315 /* Do not reschedule the sender unless we're done reading.
315 * I imagine this lets us avoid ever cancelling the transmit handle. */ 316 * I imagine this lets us avoid ever cancelling the transmit handle. */
316 register_sender(ih); 317 register_sender (ih);
317} 318}
318 319
319 320
@@ -325,19 +326,19 @@ SOCKS5_handshake_step(struct GNUNET_SOCKS_Handshake *ih)
325 * @param handler_cls closure for @a handler 326 * @param handler_cls closure for @a handler
326 */ 327 */
327void 328void
328reciever(void *cls, 329reciever (void *cls,
329 const void *buf, 330 const void *buf,
330 size_t available, 331 size_t available,
331 const struct sockaddr *addr, 332 const struct sockaddr *addr,
332 socklen_t addrlen, 333 socklen_t addrlen,
333 int errCode) 334 int errCode)
334{ 335{
335 struct GNUNET_SOCKS_Handshake *ih = cls; 336 struct GNUNET_SOCKS_Handshake *ih = cls;
336 337
337 GNUNET_assert(&ih->inend[available] < &ih->inbuf[1024]); 338 GNUNET_assert (&ih->inend[available] < &ih->inbuf[1024]);
338 GNUNET_memcpy(ih->inend, buf, available); 339 GNUNET_memcpy (ih->inend, buf, available);
339 ih->inend += available; 340 ih->inend += available;
340 SOCKS5_handshake_step(ih); 341 SOCKS5_handshake_step (ih);
341} 342}
342 343
343 344
@@ -349,13 +350,13 @@ reciever(void *cls,
349 * @param handler_cls closure for @a handler 350 * @param handler_cls closure for @a handler
350 */ 351 */
351void 352void
352register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want) 353register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want)
353{ 354{
354 GNUNET_CONNECTION_receive(ih->socks5_connection, 355 GNUNET_CONNECTION_receive (ih->socks5_connection,
355 want, 356 want,
356 GNUNET_TIME_relative_get_minute_(), 357 GNUNET_TIME_relative_get_minute_ (),
357 &reciever, 358 &reciever,
358 ih); 359 ih);
359} 360}
360 361
361 362
@@ -368,7 +369,7 @@ register_reciever(struct GNUNET_SOCKS_Handshake *ih, int want)
368 * @return number of bytes written to @a buf 369 * @return number of bytes written to @a buf
369 */ 370 */
370size_t 371size_t
371transmit_ready(void *cls, size_t size, void *buf) 372transmit_ready (void *cls, size_t size, void *buf)
372{ 373{
373 struct GNUNET_SOCKS_Handshake *ih = cls; 374 struct GNUNET_SOCKS_Handshake *ih = cls;
374 375
@@ -388,32 +389,32 @@ transmit_ready(void *cls, size_t size, void *buf)
388 * maybe that should change for error handling pruposes. It appears that 389 * maybe that should change for error handling pruposes. It appears that
389 * successful operations, including DNS resolution, do not use this. */ 390 * successful operations, including DNS resolution, do not use this. */
390 if (NULL == buf) 391 if (NULL == buf)
392 {
393 if (0 == ih->step)
394 {
395 LOG (GNUNET_ERROR_TYPE_WARNING,
396 "Timeout contacting SOCKS server, retrying indefinitely, but probably hopeless.\n");
397 register_sender (ih);
398 }
399 else
391 { 400 {
392 if (0 == ih->step) 401 LOG (GNUNET_ERROR_TYPE_ERROR,
393 { 402 "Timeout during mid SOCKS handshake (step %u), probably not a SOCKS server.\n",
394 LOG(GNUNET_ERROR_TYPE_WARNING, 403 ih->step);
395 "Timeout contacting SOCKS server, retrying indefinitely, but probably hopeless.\n"); 404 GNUNET_break (0);
396 register_sender(ih);
397 }
398 else
399 {
400 LOG(GNUNET_ERROR_TYPE_ERROR,
401 "Timeout during mid SOCKS handshake (step %u), probably not a SOCKS server.\n",
402 ih->step);
403 GNUNET_break(0);
404 }
405 return 0;
406 } 405 }
406 return 0;
407 }
407 408
408 GNUNET_assert((1024 >= size) && (size > 0)); 409 GNUNET_assert ((1024 >= size) && (size > 0));
409 GNUNET_assert((SOCKS5_step_done > ih->step) && (ih->step >= 0)); 410 GNUNET_assert ((SOCKS5_step_done > ih->step) && (ih->step >= 0));
410 unsigned char *b = ih->outstep[ih->step]; 411 unsigned char *b = ih->outstep[ih->step];
411 unsigned char *e = ih->outstep[ih->step + 1]; 412 unsigned char *e = ih->outstep[ih->step + 1];
412 GNUNET_assert(e <= &ih->outbuf[1024]); 413 GNUNET_assert (e <= &ih->outbuf[1024]);
413 unsigned int l = e - b; 414 unsigned int l = e - b;
414 GNUNET_assert(size >= l); 415 GNUNET_assert (size >= l);
415 GNUNET_memcpy(buf, b, l); 416 GNUNET_memcpy (buf, b, l);
416 register_reciever(ih, register_reciever_wants(ih)); 417 register_reciever (ih, register_reciever_wants (ih));
417 return l; 418 return l;
418} 419}
419 420
@@ -426,22 +427,22 @@ transmit_ready(void *cls, size_t size, void *buf)
426 * NULL if we are already going to notify someone else (busy) 427 * NULL if we are already going to notify someone else (busy)
427 */ 428 */
428struct GNUNET_CONNECTION_TransmitHandle * 429struct GNUNET_CONNECTION_TransmitHandle *
429register_sender(struct GNUNET_SOCKS_Handshake *ih) 430register_sender (struct GNUNET_SOCKS_Handshake *ih)
430{ 431{
431 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_MINUTES; 432 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_MINUTES;
432 433
433 GNUNET_assert(SOCKS5_step_done > ih->step); 434 GNUNET_assert (SOCKS5_step_done > ih->step);
434 GNUNET_assert(ih->step >= 0); 435 GNUNET_assert (ih->step >= 0);
435 if (0 == ih->step) 436 if (0 == ih->step)
436 timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 3); 437 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 3);
437 unsigned char *b = ih->outstep[ih->step]; 438 unsigned char *b = ih->outstep[ih->step];
438 unsigned char *e = ih->outstep[ih->step + 1]; 439 unsigned char *e = ih->outstep[ih->step + 1];
439 GNUNET_assert(ih->outbuf <= b && b < e && e < &ih->outbuf[1024]); 440 GNUNET_assert (ih->outbuf <= b && b < e && e < &ih->outbuf[1024]);
440 ih->th = GNUNET_CONNECTION_notify_transmit_ready(ih->socks5_connection, 441 ih->th = GNUNET_CONNECTION_notify_transmit_ready (ih->socks5_connection,
441 e - b, 442 e - b,
442 timeout, 443 timeout,
443 &transmit_ready, 444 &transmit_ready,
444 ih); 445 ih);
445 return ih->th; 446 return ih->th;
446} 447}
447 448
@@ -456,10 +457,10 @@ register_sender(struct GNUNET_SOCKS_Handshake *ih)
456 * @return Valid SOCKS5 hanbdshake handle 457 * @return Valid SOCKS5 hanbdshake handle
457 */ 458 */
458struct GNUNET_SOCKS_Handshake * 459struct GNUNET_SOCKS_Handshake *
459GNUNET_SOCKS_init_handshake(const char *user, const char *pass) 460GNUNET_SOCKS_init_handshake (const char *user, const char *pass)
460{ 461{
461 struct GNUNET_SOCKS_Handshake *ih = 462 struct GNUNET_SOCKS_Handshake *ih =
462 GNUNET_new(struct GNUNET_SOCKS_Handshake); 463 GNUNET_new (struct GNUNET_SOCKS_Handshake);
463 unsigned char *b = ih->outbuf; 464 unsigned char *b = ih->outbuf;
464 465
465 ih->outstep[SOCKS5_step_greet] = b; 466 ih->outstep[SOCKS5_step_greet] = b;
@@ -471,10 +472,10 @@ GNUNET_SOCKS_init_handshake(const char *user, const char *pass)
471 * And some SOCKS5 servers might require this. */ 472 * And some SOCKS5 servers might require this. */
472 *(b++) = SOCKS5_AUTH_NOAUTH; 473 *(b++) = SOCKS5_AUTH_NOAUTH;
473 if (NULL != user) 474 if (NULL != user)
474 { 475 {
475 *(b++) = SOCKS5_AUTH_USERPASS; 476 *(b++) = SOCKS5_AUTH_USERPASS;
476 (*n)++; 477 (*n)++;
477 } 478 }
478 /* There is no apperent reason to support authentication methods beyond 479 /* There is no apperent reason to support authentication methods beyond
479 * username and password since afaik Tor does not support them. */ 480 * username and password since afaik Tor does not support them. */
480 481
@@ -487,8 +488,8 @@ GNUNET_SOCKS_init_handshake(const char *user, const char *pass)
487 488
488 ih->outstep[SOCKS5_step_auth] = b; 489 ih->outstep[SOCKS5_step_auth] = b;
489 *(b++) = 1; /* subnegotiation ver.: 1 */ 490 *(b++) = 1; /* subnegotiation ver.: 1 */
490 b = SOCK5_proto_string(b, user); 491 b = SOCK5_proto_string (b, user);
491 b = SOCK5_proto_string(b, pass); 492 b = SOCK5_proto_string (b, pass);
492 493
493 ih->outstep[SOCKS5_step_cmd] = b; 494 ih->outstep[SOCKS5_step_cmd] = b;
494 495
@@ -505,9 +506,9 @@ GNUNET_SOCKS_init_handshake(const char *user, const char *pass)
505 * @return Valid SOCKS5 hanbdshake handle 506 * @return Valid SOCKS5 hanbdshake handle
506 */ 507 */
507struct GNUNET_SOCKS_Handshake * 508struct GNUNET_SOCKS_Handshake *
508GNUNET_SOCKS_init_handshake_noauth() 509GNUNET_SOCKS_init_handshake_noauth ()
509{ 510{
510 return GNUNET_SOCKS_init_handshake(NULL, NULL); 511 return GNUNET_SOCKS_init_handshake (NULL, NULL);
511} 512}
512 513
513 514
@@ -520,11 +521,12 @@ GNUNET_SOCKS_init_handshake_noauth()
520 * @param port 521 * @param port
521 */ 522 */
522void 523void
523GNUNET_SOCKS_set_handshake_destination(struct GNUNET_SOCKS_Handshake *ih, 524GNUNET_SOCKS_set_handshake_destination (struct GNUNET_SOCKS_Handshake *ih,
524 const char *host, 525 const char *host,
525 uint16_t port) 526 uint16_t port)
526{ 527{
527 union { 528 union
529 {
528 struct in_addr in4; 530 struct in_addr in4;
529 struct in6_addr in6; 531 struct in6_addr in6;
530 } ia; 532 } ia;
@@ -535,26 +537,26 @@ GNUNET_SOCKS_set_handshake_destination(struct GNUNET_SOCKS_Handshake *ih,
535 *(b++) = 0; /* reserved */ 537 *(b++) = 0; /* reserved */
536 538
537 /* Specify destination */ 539 /* Specify destination */
538 if (1 == inet_pton(AF_INET, host, &ia.in4)) 540 if (1 == inet_pton (AF_INET, host, &ia.in4))
539 { 541 {
540 *(b++) = 1; /* IPv4 */ 542 *(b++) = 1; /* IPv4 */
541 GNUNET_memcpy(b, &ia.in4, sizeof(struct in_addr)); 543 GNUNET_memcpy (b, &ia.in4, sizeof(struct in_addr));
542 b += sizeof(struct in_addr); /* 4 */ 544 b += sizeof(struct in_addr); /* 4 */
543 } 545 }
544 else if (1 == inet_pton(AF_INET6, host, &ia.in6)) 546 else if (1 == inet_pton (AF_INET6, host, &ia.in6))
545 { 547 {
546 *(b++) = 4; /* IPv6 */ 548 *(b++) = 4; /* IPv6 */
547 GNUNET_memcpy(b, &ia.in6, sizeof(struct in6_addr)); 549 GNUNET_memcpy (b, &ia.in6, sizeof(struct in6_addr));
548 b += sizeof(struct in6_addr); /* 16 */ 550 b += sizeof(struct in6_addr); /* 16 */
549 } 551 }
550 else 552 else
551 { 553 {
552 *(b++) = 3; /* hostname */ 554 *(b++) = 3; /* hostname */
553 b = SOCK5_proto_string(b, host); 555 b = SOCK5_proto_string (b, host);
554 } 556 }
555 557
556 /* Specify port */ 558 /* Specify port */
557 *(uint16_t *)b = htons(port); 559 *(uint16_t *) b = htons (port);
558 b += 2; 560 b += 2;
559 561
560 ih->outstep[SOCKS5_step_done] = b; 562 ih->outstep[SOCKS5_step_done] = b;
@@ -569,12 +571,12 @@ GNUNET_SOCKS_set_handshake_destination(struct GNUNET_SOCKS_Handshake *ih,
569 * @return Connection handle that becomes usable when the SOCKS5 handshake completes. 571 * @return Connection handle that becomes usable when the SOCKS5 handshake completes.
570 */ 572 */
571struct GNUNET_CONNECTION_Handle * 573struct GNUNET_CONNECTION_Handle *
572GNUNET_SOCKS_run_handshake(struct GNUNET_SOCKS_Handshake *ih, 574GNUNET_SOCKS_run_handshake (struct GNUNET_SOCKS_Handshake *ih,
573 struct GNUNET_CONNECTION_Handle *c) 575 struct GNUNET_CONNECTION_Handle *c)
574{ 576{
575 ih->socks5_connection = c; 577 ih->socks5_connection = c;
576 ih->target_connection = GNUNET_CONNECTION_create_proxied_from_handshake(c); 578 ih->target_connection = GNUNET_CONNECTION_create_proxied_from_handshake (c);
577 register_sender(ih); 579 register_sender (ih);
578 580
579 return ih->target_connection; 581 return ih->target_connection;
580} 582}
@@ -589,11 +591,11 @@ GNUNET_SOCKS_run_handshake(struct GNUNET_SOCKS_Handshake *ih,
589 * @return GNUNET_YES if so, GNUNET_NO if not 591 * @return GNUNET_YES if so, GNUNET_NO if not
590 */ 592 */
591int 593int
592GNUNET_SOCKS_check_service(const char *service_name, 594GNUNET_SOCKS_check_service (const char *service_name,
593 const struct GNUNET_CONFIGURATION_Handle *cfg) 595 const struct GNUNET_CONFIGURATION_Handle *cfg)
594{ 596{
595 return GNUNET_CONFIGURATION_have_value(cfg, service_name, "SOCKSPORT") || 597 return GNUNET_CONFIGURATION_have_value (cfg, service_name, "SOCKSPORT") ||
596 GNUNET_CONFIGURATION_have_value(cfg, service_name, "SOCKSHOST"); 598 GNUNET_CONFIGURATION_have_value (cfg, service_name, "SOCKSHOST");
597} 599}
598 600
599 601
@@ -606,8 +608,8 @@ GNUNET_SOCKS_check_service(const char *service_name,
606 * NULL if SOCKS not configured or not configured properly 608 * NULL if SOCKS not configured or not configured properly
607 */ 609 */
608struct GNUNET_CONNECTION_Handle * 610struct GNUNET_CONNECTION_Handle *
609GNUNET_SOCKS_do_connect(const char *service_name, 611GNUNET_SOCKS_do_connect (const char *service_name,
610 const struct GNUNET_CONFIGURATION_Handle *cfg) 612 const struct GNUNET_CONFIGURATION_Handle *cfg)
611{ 613{
612 struct GNUNET_SOCKS_Handshake *ih; 614 struct GNUNET_SOCKS_Handshake *ih;
613 struct GNUNET_CONNECTION_Handle *socks5; /* *proxied */ 615 struct GNUNET_CONNECTION_Handle *socks5; /* *proxied */
@@ -618,70 +620,70 @@ GNUNET_SOCKS_do_connect(const char *service_name,
618 unsigned long long port0; 620 unsigned long long port0;
619 unsigned long long port1; 621 unsigned long long port1;
620 622
621 if (GNUNET_YES != GNUNET_SOCKS_check_service(service_name, cfg)) 623 if (GNUNET_YES != GNUNET_SOCKS_check_service (service_name, cfg))
622 return NULL; 624 return NULL;
623 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, 625 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
624 service_name, 626 service_name,
625 "SOCKSPORT", 627 "SOCKSPORT",
626 &port0)) 628 &port0))
627 port0 = 9050; 629 port0 = 9050;
628 /* A typical Tor client should usually try port 9150 for the TBB too, but 630 /* A typical Tor client should usually try port 9150 for the TBB too, but
629 * GNUnet can probably assume a system Tor installation. */ 631 * GNUnet can probably assume a system Tor installation. */
630 if (port0 > 65535 || port0 <= 0) 632 if ((port0 > 65535)||(port0 <= 0))
631 { 633 {
632 LOG(GNUNET_ERROR_TYPE_WARNING, 634 LOG (GNUNET_ERROR_TYPE_WARNING,
633 _( 635 _ (
634 "Attempting to use invalid port %d as SOCKS proxy for service `%s'.\n"), 636 "Attempting to use invalid port %d as SOCKS proxy for service `%s'.\n"),
635 port0, 637 port0,
636 service_name); 638 service_name);
637 return NULL; 639 return NULL;
638 } 640 }
639 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, 641 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
640 service_name, 642 service_name,
641 "PORT", 643 "PORT",
642 &port1)) || 644 &port1)) ||
643 (port1 > 65535) || (port1 <= 0) || 645 (port1 > 65535) || (port1 <= 0) ||
644 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, 646 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
645 service_name, 647 service_name,
646 "HOSTNAME", 648 "HOSTNAME",
647 &host1))) 649 &host1)))
648 { 650 {
649 LOG(GNUNET_ERROR_TYPE_WARNING, 651 LOG (GNUNET_ERROR_TYPE_WARNING,
650 _( 652 _ (
651 "Attempting to proxy service `%s' to invalid port %d or hostname.\n"), 653 "Attempting to proxy service `%s' to invalid port %d or hostname.\n"),
652 service_name, 654 service_name,
653 port1); 655 port1);
654 return NULL; 656 return NULL;
655 } 657 }
656 /* Appeared to still work after host0 corrupted, so either test case is broken, or 658 /* Appeared to still work after host0 corrupted, so either test case is broken, or
657 this whole routine is not being called. */ 659 this whole routine is not being called. */
658 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, 660 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
659 service_name, 661 service_name,
660 "SOCKSHOST", 662 "SOCKSHOST",
661 &host0)) 663 &host0))
662 host0 = NULL; 664 host0 = NULL;
663 socks5 = GNUNET_CONNECTION_create_from_connect(cfg, 665 socks5 = GNUNET_CONNECTION_create_from_connect (cfg,
664 (host0 != NULL) ? host0 666 (host0 != NULL) ? host0
665 : "127.0.0.1", 667 : "127.0.0.1",
666 port0); 668 port0);
667 GNUNET_free_non_null(host0); 669 GNUNET_free_non_null (host0);
668 670
669 /* Sets to NULL if they do not exist */ 671 /* Sets to NULL if they do not exist */
670 (void)GNUNET_CONFIGURATION_get_value_string(cfg, 672 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
671 service_name, 673 service_name,
672 "SOCKSUSER", 674 "SOCKSUSER",
673 &user); 675 &user);
674 (void)GNUNET_CONFIGURATION_get_value_string(cfg, 676 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
675 service_name, 677 service_name,
676 "SOCKSPASS", 678 "SOCKSPASS",
677 &pass); 679 &pass);
678 ih = GNUNET_SOCKS_init_handshake(user, pass); 680 ih = GNUNET_SOCKS_init_handshake (user, pass);
679 GNUNET_free_non_null(user); 681 GNUNET_free_non_null (user);
680 GNUNET_free_non_null(pass); 682 GNUNET_free_non_null (pass);
681 683
682 GNUNET_SOCKS_set_handshake_destination(ih, host1, port1); 684 GNUNET_SOCKS_set_handshake_destination (ih, host1, port1);
683 GNUNET_free(host1); 685 GNUNET_free (host1);
684 return GNUNET_SOCKS_run_handshake(ih, socks5); 686 return GNUNET_SOCKS_run_handshake (ih, socks5);
685} 687}
686 688
687/* socks.c */ 689/* socks.c */
diff --git a/src/util/speedup.c b/src/util/speedup.c
index 735da18c3..81fdab48a 100644
--- a/src/util/speedup.c
+++ b/src/util/speedup.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "speedup.h" 28#include "speedup.h"
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-speedup", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-speedup", __VA_ARGS__)
31 31
32 32
33static struct GNUNET_TIME_Relative interval; 33static struct GNUNET_TIME_Relative interval;
@@ -38,20 +38,20 @@ static struct GNUNET_SCHEDULER_Task *speedup_task;
38 38
39 39
40static void 40static void
41do_speedup(void *cls) 41do_speedup (void *cls)
42{ 42{
43 static long long current_offset; 43 static long long current_offset;
44 44
45 (void)cls; 45 (void) cls;
46 speedup_task = NULL; 46 speedup_task = NULL;
47 current_offset += delta.rel_value_us; 47 current_offset += delta.rel_value_us;
48 GNUNET_TIME_set_offset(current_offset); 48 GNUNET_TIME_set_offset (current_offset);
49 LOG(GNUNET_ERROR_TYPE_DEBUG, 49 LOG (GNUNET_ERROR_TYPE_DEBUG,
50 "Speeding up execution time by %s\n", 50 "Speeding up execution time by %s\n",
51 GNUNET_STRINGS_relative_time_to_string(delta, GNUNET_NO)); 51 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
52 speedup_task = GNUNET_SCHEDULER_add_delayed(interval, 52 speedup_task = GNUNET_SCHEDULER_add_delayed (interval,
53 &do_speedup, 53 &do_speedup,
54 NULL); 54 NULL);
55} 55}
56 56
57 57
@@ -62,38 +62,38 @@ do_speedup(void *cls)
62 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured 62 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured
63 */ 63 */
64int 64int
65GNUNET_SPEEDUP_start_(const struct GNUNET_CONFIGURATION_Handle *cfg) 65GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg)
66{ 66{
67 GNUNET_assert(NULL == speedup_task); 67 GNUNET_assert (NULL == speedup_task);
68 if (GNUNET_OK != 68 if (GNUNET_OK !=
69 GNUNET_CONFIGURATION_get_value_time(cfg, 69 GNUNET_CONFIGURATION_get_value_time (cfg,
70 "testing", 70 "testing",
71 "SPEEDUP_INTERVAL", 71 "SPEEDUP_INTERVAL",
72 &interval)) 72 &interval))
73 return GNUNET_SYSERR; 73 return GNUNET_SYSERR;
74 if (GNUNET_OK != 74 if (GNUNET_OK !=
75 GNUNET_CONFIGURATION_get_value_time(cfg, 75 GNUNET_CONFIGURATION_get_value_time (cfg,
76 "testing", 76 "testing",
77 "SPEEDUP_DELTA", 77 "SPEEDUP_DELTA",
78 &delta)) 78 &delta))
79 return GNUNET_SYSERR; 79 return GNUNET_SYSERR;
80 80
81 if ((0 == interval.rel_value_us) || 81 if ((0 == interval.rel_value_us) ||
82 (0 == delta.rel_value_us)) 82 (0 == delta.rel_value_us))
83 { 83 {
84 LOG(GNUNET_ERROR_TYPE_DEBUG, 84 LOG (GNUNET_ERROR_TYPE_DEBUG,
85 "Speed up disabled\n"); 85 "Speed up disabled\n");
86 return GNUNET_OK; 86 return GNUNET_OK;
87 } 87 }
88 LOG(GNUNET_ERROR_TYPE_DEBUG, 88 LOG (GNUNET_ERROR_TYPE_DEBUG,
89 "Speed up execution by %s\n", 89 "Speed up execution by %s\n",
90 GNUNET_STRINGS_relative_time_to_string(delta, GNUNET_NO)); 90 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
91 LOG(GNUNET_ERROR_TYPE_DEBUG, 91 LOG (GNUNET_ERROR_TYPE_DEBUG,
92 "Speed up executed every %s\n", 92 "Speed up executed every %s\n",
93 GNUNET_STRINGS_relative_time_to_string(interval, GNUNET_NO)); 93 GNUNET_STRINGS_relative_time_to_string (interval, GNUNET_NO));
94 speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness(GNUNET_NO, 94 speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
95 &do_speedup, 95 &do_speedup,
96 NULL); 96 NULL);
97 return GNUNET_OK; 97 return GNUNET_OK;
98} 98}
99 99
@@ -102,17 +102,17 @@ GNUNET_SPEEDUP_start_(const struct GNUNET_CONFIGURATION_Handle *cfg)
102 * Stop tasks that modify clock behavior. 102 * Stop tasks that modify clock behavior.
103 */ 103 */
104void 104void
105GNUNET_SPEEDUP_stop_() 105GNUNET_SPEEDUP_stop_ ()
106{ 106{
107 if (NULL != speedup_task) 107 if (NULL != speedup_task)
108 { 108 {
109 GNUNET_SCHEDULER_cancel(speedup_task); 109 GNUNET_SCHEDULER_cancel (speedup_task);
110 speedup_task = NULL; 110 speedup_task = NULL;
111 } 111 }
112 if ((0 != interval.rel_value_us) && 112 if ((0 != interval.rel_value_us) &&
113 (0 != delta.rel_value_us)) 113 (0 != delta.rel_value_us))
114 LOG(GNUNET_ERROR_TYPE_DEBUG, 114 LOG (GNUNET_ERROR_TYPE_DEBUG,
115 "Stopped execution speed up\n"); 115 "Stopped execution speed up\n");
116} 116}
117 117
118/* end of speedup.c */ 118/* end of speedup.c */
diff --git a/src/util/speedup.h b/src/util/speedup.h
index 232d74f7d..505e73bf4 100644
--- a/src/util/speedup.h
+++ b/src/util/speedup.h
@@ -34,12 +34,12 @@
34 * @return GNUNET_OK on success, GNUNET_SYSERR if the speedup was not configured 34 * @return GNUNET_OK on success, GNUNET_SYSERR if the speedup was not configured
35 */ 35 */
36int 36int
37GNUNET_SPEEDUP_start_(const struct GNUNET_CONFIGURATION_Handle *cfg); 37GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
38 38
39/** 39/**
40 * Stop tasks that modify clock behavior. 40 * Stop tasks that modify clock behavior.
41 */ 41 */
42void 42void
43GNUNET_SPEEDUP_stop_(void); 43GNUNET_SPEEDUP_stop_ (void);
44 44
45#endif /* SPEEDUP_H_ */ 45#endif /* SPEEDUP_H_ */
diff --git a/src/util/strings.c b/src/util/strings.c
index 25b09f8e2..3442a6bf7 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -34,10 +34,10 @@
34#include <unistr.h> 34#include <unistr.h>
35#include <uniconv.h> 35#include <uniconv.h>
36 36
37#define LOG(kind, ...) GNUNET_log_from(kind, "util-strings", __VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log_from (kind, "util-strings", __VA_ARGS__)
38 38
39#define LOG_STRERROR(kind, syscall) \ 39#define LOG_STRERROR(kind, syscall) \
40 GNUNET_log_from_strerror(kind, "util-strings", syscall) 40 GNUNET_log_from_strerror (kind, "util-strings", syscall)
41 41
42 42
43/** 43/**
@@ -62,7 +62,7 @@
62 * (or number of bytes that would have been written) 62 * (or number of bytes that would have been written)
63 */ 63 */
64size_t 64size_t
65GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count, ...) 65GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
66{ 66{
67 size_t needed; 67 size_t needed;
68 size_t slen; 68 size_t slen;
@@ -70,21 +70,21 @@ GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count, ...)
70 va_list ap; 70 va_list ap;
71 71
72 needed = 0; 72 needed = 0;
73 va_start(ap, count); 73 va_start (ap, count);
74 while (count > 0) 74 while (count > 0)
75 { 75 {
76 s = va_arg(ap, const char *); 76 s = va_arg (ap, const char *);
77 77
78 slen = strlen(s) + 1; 78 slen = strlen (s) + 1;
79 if (buffer != NULL) 79 if (buffer != NULL)
80 { 80 {
81 GNUNET_assert(needed + slen <= size); 81 GNUNET_assert (needed + slen <= size);
82 GNUNET_memcpy(&buffer[needed], s, slen); 82 GNUNET_memcpy (&buffer[needed], s, slen);
83 }
84 needed += slen;
85 count--;
86 } 83 }
87 va_end(ap); 84 needed += slen;
85 count--;
86 }
87 va_end (ap);
88 return needed; 88 return needed;
89} 89}
90 90
@@ -97,23 +97,23 @@ GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count, ...)
97 * @return string representing the array of @a pids 97 * @return string representing the array of @a pids
98 */ 98 */
99char * 99char *
100GNUNET_STRINGS_pp2s(const struct GNUNET_PeerIdentity *pids, 100GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids,
101 unsigned int num_pids) 101 unsigned int num_pids)
102{ 102{
103 char *buf; 103 char *buf;
104 size_t off; 104 size_t off;
105 size_t plen = num_pids * 5 + 1; 105 size_t plen = num_pids * 5 + 1;
106 106
107 off = 0; 107 off = 0;
108 buf = GNUNET_malloc(plen); 108 buf = GNUNET_malloc (plen);
109 for (unsigned int i = 0; i < num_pids; i++) 109 for (unsigned int i = 0; i < num_pids; i++)
110 { 110 {
111 off += GNUNET_snprintf(&buf[off], 111 off += GNUNET_snprintf (&buf[off],
112 plen - off, 112 plen - off,
113 "%s%s", 113 "%s%s",
114 GNUNET_i2s(&pids[i]), 114 GNUNET_i2s (&pids[i]),
115 (i == num_pids - 1) ? "" : "-"); 115 (i == num_pids - 1) ? "" : "-");
116 } 116 }
117 return buf; 117 return buf;
118} 118}
119 119
@@ -132,10 +132,10 @@ GNUNET_STRINGS_pp2s(const struct GNUNET_PeerIdentity *pids,
132 * in the buffer, or 0 on error. 132 * in the buffer, or 0 on error.
133 */ 133 */
134unsigned int 134unsigned int
135GNUNET_STRINGS_buffer_tokenize(const char *buffer, 135GNUNET_STRINGS_buffer_tokenize (const char *buffer,
136 size_t size, 136 size_t size,
137 unsigned int count, 137 unsigned int count,
138 ...) 138 ...)
139{ 139{
140 unsigned int start; 140 unsigned int start;
141 unsigned int needed; 141 unsigned int needed;
@@ -143,24 +143,24 @@ GNUNET_STRINGS_buffer_tokenize(const char *buffer,
143 va_list ap; 143 va_list ap;
144 144
145 needed = 0; 145 needed = 0;
146 va_start(ap, count); 146 va_start (ap, count);
147 while (count > 0) 147 while (count > 0)
148 { 148 {
149 r = va_arg(ap, const char **); 149 r = va_arg (ap, const char **);
150 150
151 start = needed; 151 start = needed;
152 while ((needed < size) && (buffer[needed] != '\0')) 152 while ((needed < size) && (buffer[needed] != '\0'))
153 needed++; 153 needed++;
154 if (needed == size) 154 if (needed == size)
155 { 155 {
156 va_end(ap); 156 va_end (ap);
157 return 0; /* error */ 157 return 0; /* error */
158 }
159 *r = &buffer[start];
160 needed++; /* skip 0-termination */
161 count--;
162 } 158 }
163 va_end(ap); 159 *r = &buffer[start];
160 needed++; /* skip 0-termination */
161 count--;
162 }
163 va_end (ap);
164 return needed; 164 return needed;
165} 165}
166 166
@@ -172,33 +172,33 @@ GNUNET_STRINGS_buffer_tokenize(const char *buffer,
172 * @return fancy representation of the size (possibly rounded) for humans 172 * @return fancy representation of the size (possibly rounded) for humans
173 */ 173 */
174char * 174char *
175GNUNET_STRINGS_byte_size_fancy(unsigned long long size) 175GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
176{ 176{
177 const char *unit = _(/* size unit */ "b"); 177 const char *unit = _ (/* size unit */ "b");
178 char *ret; 178 char *ret;
179 179
180 if (size > 5 * 1024) 180 if (size > 5 * 1024)
181 {
182 size = size / 1024;
183 unit = "KiB";
184 if (size > 5 * 1024)
181 { 185 {
182 size = size / 1024; 186 size = size / 1024;
183 unit = "KiB"; 187 unit = "MiB";
184 if (size > 5 * 1024) 188 if (size > 5 * 1024)
189 {
190 size = size / 1024;
191 unit = "GiB";
192 if (size > 5 * 1024)
185 { 193 {
186 size = size / 1024; 194 size = size / 1024;
187 unit = "MiB"; 195 unit = "TiB";
188 if (size > 5 * 1024)
189 {
190 size = size / 1024;
191 unit = "GiB";
192 if (size > 5 * 1024)
193 {
194 size = size / 1024;
195 unit = "TiB";
196 }
197 }
198 } 196 }
197 }
199 } 198 }
200 ret = GNUNET_malloc(32); 199 }
201 GNUNET_snprintf(ret, 32, "%llu %s", size, unit); 200 ret = GNUNET_malloc (32);
201 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
202 return ret; 202 return ret;
203} 203}
204 204
@@ -216,13 +216,13 @@ GNUNET_STRINGS_byte_size_fancy(unsigned long long size)
216 * null byte 216 * null byte
217 */ 217 */
218size_t 218size_t
219GNUNET_strlcpy(char *dst, const char *src, size_t n) 219GNUNET_strlcpy (char *dst, const char *src, size_t n)
220{ 220{
221 size_t slen; 221 size_t slen;
222 222
223 GNUNET_assert(0 != n); 223 GNUNET_assert (0 != n);
224 slen = strnlen(src, n - 1); 224 slen = strnlen (src, n - 1);
225 memcpy(dst, src, slen); 225 memcpy (dst, src, slen);
226 dst[slen] = '\0'; 226 dst[slen] = '\0';
227 return slen; 227 return slen;
228} 228}
@@ -231,7 +231,8 @@ GNUNET_strlcpy(char *dst, const char *src, size_t n)
231/** 231/**
232 * Unit conversion table entry for 'convert_with_table'. 232 * Unit conversion table entry for 'convert_with_table'.
233 */ 233 */
234struct ConversionTable { 234struct ConversionTable
235{
235 /** 236 /**
236 * Name of the unit (or NULL for end of table). 237 * Name of the unit (or NULL for end of table).
237 */ 238 */
@@ -256,9 +257,9 @@ struct ConversionTable {
256 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 257 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
257 */ 258 */
258static int 259static int
259convert_with_table(const char *input, 260convert_with_table (const char *input,
260 const struct ConversionTable *table, 261 const struct ConversionTable *table,
261 unsigned long long *output) 262 unsigned long long *output)
262{ 263{
263 unsigned long long ret; 264 unsigned long long ret;
264 char *in; 265 char *in;
@@ -268,41 +269,41 @@ convert_with_table(const char *input,
268 269
269 ret = 0; 270 ret = 0;
270 last = 0; 271 last = 0;
271 in = GNUNET_strdup(input); 272 in = GNUNET_strdup (input);
272 for (tok = strtok(in, " "); tok != NULL; tok = strtok(NULL, " ")) 273 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
274 {
275 do
273 { 276 {
274 do 277 i = 0;
278 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
279 i++;
280 if (table[i].name != NULL)
281 {
282 last *= table[i].value;
283 break; /* next tok */
284 }
285 else
286 {
287 char *endptr;
288 ret += last;
289 errno = 0;
290 last = strtoull (tok, &endptr, 10);
291 if ((0 != errno) || (endptr == tok))
275 { 292 {
276 i = 0; 293 GNUNET_free (in);
277 while ((table[i].name != NULL) && (0 != strcasecmp(table[i].name, tok))) 294 return GNUNET_SYSERR; /* expected number */
278 i++;
279 if (table[i].name != NULL)
280 {
281 last *= table[i].value;
282 break; /* next tok */
283 }
284 else
285 {
286 char *endptr;
287 ret += last;
288 errno = 0;
289 last = strtoull(tok, &endptr, 10);
290 if ((0 != errno) || (endptr == tok))
291 {
292 GNUNET_free(in);
293 return GNUNET_SYSERR; /* expected number */
294 }
295 if ('\0' == endptr[0])
296 break; /* next tok */
297 else
298 tok = endptr; /* and re-check (handles times like "10s") */
299 }
300 } 295 }
301 while (GNUNET_YES); 296 if ('\0' == endptr[0])
297 break; /* next tok */
298 else
299 tok = endptr; /* and re-check (handles times like "10s") */
300 }
302 } 301 }
302 while (GNUNET_YES);
303 }
303 ret += last; 304 ret += last;
304 *output = ret; 305 *output = ret;
305 GNUNET_free(in); 306 GNUNET_free (in);
306 return GNUNET_OK; 307 return GNUNET_OK;
307} 308}
308 309
@@ -315,8 +316,8 @@ convert_with_table(const char *input,
315 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 316 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
316 */ 317 */
317int 318int
318GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size, 319GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
319 unsigned long long *size) 320 unsigned long long *size)
320{ 321{
321 static const struct ConversionTable table[] = 322 static const struct ConversionTable table[] =
322 { { "B", 1 }, 323 { { "B", 1 },
@@ -334,7 +335,7 @@ GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size,
334 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL }, 335 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
335 { NULL, 0 } }; 336 { NULL, 0 } };
336 337
337 return convert_with_table(fancy_size, table, size); 338 return convert_with_table (fancy_size, table, size);
338} 339}
339 340
340 341
@@ -347,8 +348,8 @@ GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size,
347 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 348 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
348 */ 349 */
349int 350int
350GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, 351GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
351 struct GNUNET_TIME_Relative *rtime) 352 struct GNUNET_TIME_Relative *rtime)
352{ 353{
353 static const struct ConversionTable table[] = 354 static const struct ConversionTable table[] =
354 { { "us", 1 }, 355 { { "us", 1 },
@@ -377,13 +378,13 @@ GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time,
377 int ret; 378 int ret;
378 unsigned long long val; 379 unsigned long long val;
379 380
380 if (0 == strcasecmp("forever", fancy_time)) 381 if (0 == strcasecmp ("forever", fancy_time))
381 { 382 {
382 *rtime = GNUNET_TIME_UNIT_FOREVER_REL; 383 *rtime = GNUNET_TIME_UNIT_FOREVER_REL;
383 return GNUNET_OK; 384 return GNUNET_OK;
384 } 385 }
385 ret = convert_with_table(fancy_time, table, &val); 386 ret = convert_with_table (fancy_time, table, &val);
386 rtime->rel_value_us = (uint64_t)val; 387 rtime->rel_value_us = (uint64_t) val;
387 return ret; 388 return ret;
388} 389}
389 390
@@ -398,33 +399,33 @@ GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time,
398 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 399 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
399 */ 400 */
400int 401int
401GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, 402GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
402 struct GNUNET_TIME_Absolute *atime) 403 struct GNUNET_TIME_Absolute *atime)
403{ 404{
404 struct tm tv; 405 struct tm tv;
405 time_t t; 406 time_t t;
406 const char *eos; 407 const char *eos;
407 408
408 if (0 == strcasecmp("end of time", fancy_time)) 409 if (0 == strcasecmp ("end of time", fancy_time))
409 { 410 {
410 *atime = GNUNET_TIME_UNIT_FOREVER_ABS; 411 *atime = GNUNET_TIME_UNIT_FOREVER_ABS;
411 return GNUNET_OK; 412 return GNUNET_OK;
412 } 413 }
413 eos = &fancy_time[strlen(fancy_time)]; 414 eos = &fancy_time[strlen (fancy_time)];
414 memset(&tv, 0, sizeof(tv)); 415 memset (&tv, 0, sizeof(tv));
415 if ((eos != strptime(fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) && 416 if ((eos != strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) &&
416 (eos != strptime(fancy_time, "%c", &tv)) && 417 (eos != strptime (fancy_time, "%c", &tv)) &&
417 (eos != strptime(fancy_time, "%Ec", &tv)) && 418 (eos != strptime (fancy_time, "%Ec", &tv)) &&
418 (eos != strptime(fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && 419 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) &&
419 (eos != strptime(fancy_time, "%Y-%m-%d %H:%M", &tv)) && 420 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) &&
420 (eos != strptime(fancy_time, "%x", &tv)) && 421 (eos != strptime (fancy_time, "%x", &tv)) &&
421 (eos != strptime(fancy_time, "%Ex", &tv)) && 422 (eos != strptime (fancy_time, "%Ex", &tv)) &&
422 (eos != strptime(fancy_time, "%Y-%m-%d", &tv)) && 423 (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) &&
423 (eos != strptime(fancy_time, "%Y-%m", &tv)) && 424 (eos != strptime (fancy_time, "%Y-%m", &tv)) &&
424 (eos != strptime(fancy_time, "%Y", &tv))) 425 (eos != strptime (fancy_time, "%Y", &tv)))
425 return GNUNET_SYSERR; 426 return GNUNET_SYSERR;
426 t = mktime(&tv); 427 t = mktime (&tv);
427 atime->abs_value_us = (uint64_t)((uint64_t)t * 1000LL * 1000LL); 428 atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL);
428 return GNUNET_OK; 429 return GNUNET_OK;
429} 430}
430 431
@@ -443,10 +444,10 @@ GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time,
443 * string is returned. 444 * string is returned.
444 */ 445 */
445char * 446char *
446GNUNET_STRINGS_conv(const char *input, 447GNUNET_STRINGS_conv (const char *input,
447 size_t len, 448 size_t len,
448 const char *input_charset, 449 const char *input_charset,
449 const char *output_charset) 450 const char *output_charset)
450{ 451{
451 char *ret; 452 char *ret;
452 uint8_t *u8_string; 453 uint8_t *u8_string;
@@ -454,51 +455,51 @@ GNUNET_STRINGS_conv(const char *input,
454 size_t u8_string_length; 455 size_t u8_string_length;
455 size_t encoded_string_length; 456 size_t encoded_string_length;
456 457
457 u8_string = u8_conv_from_encoding(input_charset, 458 u8_string = u8_conv_from_encoding (input_charset,
458 iconveh_error, 459 iconveh_error,
459 input, 460 input,
460 len, 461 len,
461 NULL, 462 NULL,
462 NULL, 463 NULL,
463 &u8_string_length); 464 &u8_string_length);
464 if (NULL == u8_string) 465 if (NULL == u8_string)
465 { 466 {
466 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); 467 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
467 goto fail; 468 goto fail;
468 } 469 }
469 if (0 == strcmp(output_charset, "UTF-8")) 470 if (0 == strcmp (output_charset, "UTF-8"))
470 { 471 {
471 ret = GNUNET_malloc(u8_string_length + 1); 472 ret = GNUNET_malloc (u8_string_length + 1);
472 GNUNET_memcpy(ret, u8_string, u8_string_length); 473 GNUNET_memcpy (ret, u8_string, u8_string_length);
473 ret[u8_string_length] = '\0'; 474 ret[u8_string_length] = '\0';
474 free(u8_string); 475 free (u8_string);
475 return ret; 476 return ret;
476 } 477 }
477 encoded_string = u8_conv_to_encoding(output_charset, 478 encoded_string = u8_conv_to_encoding (output_charset,
478 iconveh_error, 479 iconveh_error,
479 u8_string, 480 u8_string,
480 u8_string_length, 481 u8_string_length,
481 NULL, 482 NULL,
482 NULL, 483 NULL,
483 &encoded_string_length); 484 &encoded_string_length);
484 free(u8_string); 485 free (u8_string);
485 if (NULL == encoded_string) 486 if (NULL == encoded_string)
486 { 487 {
487 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding"); 488 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
488 goto fail; 489 goto fail;
489 } 490 }
490 ret = GNUNET_malloc(encoded_string_length + 1); 491 ret = GNUNET_malloc (encoded_string_length + 1);
491 GNUNET_memcpy(ret, encoded_string, encoded_string_length); 492 GNUNET_memcpy (ret, encoded_string, encoded_string_length);
492 ret[encoded_string_length] = '\0'; 493 ret[encoded_string_length] = '\0';
493 free(encoded_string); 494 free (encoded_string);
494 return ret; 495 return ret;
495fail: 496fail:
496 LOG(GNUNET_ERROR_TYPE_WARNING, 497 LOG (GNUNET_ERROR_TYPE_WARNING,
497 _("Character sets requested were `%s'->`%s'\n"), 498 _ ("Character sets requested were `%s'->`%s'\n"),
498 "UTF-8", 499 "UTF-8",
499 output_charset); 500 output_charset);
500 ret = GNUNET_malloc(len + 1); 501 ret = GNUNET_malloc (len + 1);
501 GNUNET_memcpy(ret, input, len); 502 GNUNET_memcpy (ret, input, len);
502 ret[len] = '\0'; 503 ret[len] = '\0';
503 return ret; 504 return ret;
504} 505}
@@ -517,9 +518,9 @@ fail:
517 * string is returned. 518 * string is returned.
518 */ 519 */
519char * 520char *
520GNUNET_STRINGS_to_utf8(const char *input, size_t len, const char *charset) 521GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
521{ 522{
522 return GNUNET_STRINGS_conv(input, len, charset, "UTF-8"); 523 return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
523} 524}
524 525
525 526
@@ -535,9 +536,9 @@ GNUNET_STRINGS_to_utf8(const char *input, size_t len, const char *charset)
535 * string is returned. 536 * string is returned.
536 */ 537 */
537char * 538char *
538GNUNET_STRINGS_from_utf8(const char *input, size_t len, const char *charset) 539GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
539{ 540{
540 return GNUNET_STRINGS_conv(input, len, "UTF-8", charset); 541 return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
541} 542}
542 543
543 544
@@ -549,20 +550,20 @@ GNUNET_STRINGS_from_utf8(const char *input, size_t len, const char *charset)
549 * @param output output buffer 550 * @param output output buffer
550 */ 551 */
551void 552void
552GNUNET_STRINGS_utf8_tolower(const char *input, char *output) 553GNUNET_STRINGS_utf8_tolower (const char *input, char *output)
553{ 554{
554 uint8_t *tmp_in; 555 uint8_t *tmp_in;
555 size_t len; 556 size_t len;
556 557
557 tmp_in = u8_tolower((uint8_t *)input, 558 tmp_in = u8_tolower ((uint8_t *) input,
558 strlen((char *)input), 559 strlen ((char *) input),
559 NULL, 560 NULL,
560 UNINORM_NFD, 561 UNINORM_NFD,
561 NULL, 562 NULL,
562 &len); 563 &len);
563 GNUNET_memcpy(output, tmp_in, len); 564 GNUNET_memcpy (output, tmp_in, len);
564 output[len] = '\0'; 565 output[len] = '\0';
565 free(tmp_in); 566 free (tmp_in);
566} 567}
567 568
568 569
@@ -574,20 +575,20 @@ GNUNET_STRINGS_utf8_tolower(const char *input, char *output)
574 * @param output output buffer 575 * @param output output buffer
575 */ 576 */
576void 577void
577GNUNET_STRINGS_utf8_toupper(const char *input, char *output) 578GNUNET_STRINGS_utf8_toupper (const char *input, char *output)
578{ 579{
579 uint8_t *tmp_in; 580 uint8_t *tmp_in;
580 size_t len; 581 size_t len;
581 582
582 tmp_in = u8_toupper((uint8_t *)input, 583 tmp_in = u8_toupper ((uint8_t *) input,
583 strlen((char *)input), 584 strlen ((char *) input),
584 NULL, 585 NULL,
585 UNINORM_NFD, 586 UNINORM_NFD,
586 NULL, 587 NULL,
587 &len); 588 &len);
588 GNUNET_memcpy(output, tmp_in, len); 589 GNUNET_memcpy (output, tmp_in, len);
589 output[len] = '\0'; 590 output[len] = '\0';
590 free(tmp_in); 591 free (tmp_in);
591} 592}
592 593
593 594
@@ -599,7 +600,7 @@ GNUNET_STRINGS_utf8_toupper(const char *input, char *output)
599 * NULL is returned on error 600 * NULL is returned on error
600 */ 601 */
601char * 602char *
602GNUNET_STRINGS_filename_expand(const char *fil) 603GNUNET_STRINGS_filename_expand (const char *fil)
603{ 604{
604 char *buffer; 605 char *buffer;
605 size_t len; 606 size_t len;
@@ -611,64 +612,64 @@ GNUNET_STRINGS_filename_expand(const char *fil)
611 612
612 if (fil[0] == DIR_SEPARATOR) 613 if (fil[0] == DIR_SEPARATOR)
613 /* absolute path, just copy */ 614 /* absolute path, just copy */
614 return GNUNET_strdup(fil); 615 return GNUNET_strdup (fil);
615 if (fil[0] == '~') 616 if (fil[0] == '~')
617 {
618 fm = getenv ("HOME");
619 if (fm == NULL)
616 { 620 {
617 fm = getenv("HOME"); 621 LOG (GNUNET_ERROR_TYPE_WARNING,
618 if (fm == NULL) 622 _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
619 { 623 return NULL;
620 LOG(GNUNET_ERROR_TYPE_WARNING,
621 _("Failed to expand `$HOME': environment variable `HOME' not set"));
622 return NULL;
623 }
624 fm = GNUNET_strdup(fm);
625 /* do not copy '~' */
626 fil_ptr = fil + 1;
627
628 /* skip over dir seperator to be consistent */
629 if (fil_ptr[0] == DIR_SEPARATOR)
630 fil_ptr++;
631 } 624 }
625 fm = GNUNET_strdup (fm);
626 /* do not copy '~' */
627 fil_ptr = fil + 1;
628
629 /* skip over dir seperator to be consistent */
630 if (fil_ptr[0] == DIR_SEPARATOR)
631 fil_ptr++;
632 }
632 else 633 else
634 {
635 /* relative path */
636 fil_ptr = fil;
637 len = 512;
638 fm = NULL;
639 while (1)
633 { 640 {
634 /* relative path */ 641 buffer = GNUNET_malloc (len);
635 fil_ptr = fil; 642 if (getcwd (buffer, len) != NULL)
636 len = 512; 643 {
637 fm = NULL; 644 fm = buffer;
638 while (1) 645 break;
639 { 646 }
640 buffer = GNUNET_malloc(len); 647 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
641 if (getcwd(buffer, len) != NULL) 648 {
642 { 649 len *= 2;
643 fm = buffer; 650 GNUNET_free (buffer);
644 break; 651 continue;
645 } 652 }
646 if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) 653 GNUNET_free (buffer);
647 { 654 break;
648 len *= 2; 655 }
649 GNUNET_free(buffer); 656 if (fm == NULL)
650 continue; 657 {
651 } 658 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
652 GNUNET_free(buffer); 659 buffer = getenv ("PWD"); /* alternative */
653 break; 660 if (buffer != NULL)
654 } 661 fm = GNUNET_strdup (buffer);
655 if (fm == NULL)
656 {
657 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "getcwd");
658 buffer = getenv("PWD"); /* alternative */
659 if (buffer != NULL)
660 fm = GNUNET_strdup(buffer);
661 }
662 if (fm == NULL)
663 fm = GNUNET_strdup("./"); /* give up */
664 } 662 }
665 GNUNET_asprintf(&buffer, 663 if (fm == NULL)
666 "%s%s%s", 664 fm = GNUNET_strdup ("./"); /* give up */
667 fm, 665 }
668 (fm[strlen(fm) - 1] == DIR_SEPARATOR) ? "" 666 GNUNET_asprintf (&buffer,
669 : DIR_SEPARATOR_STR, 667 "%s%s%s",
670 fil_ptr); 668 fm,
671 GNUNET_free(fm); 669 (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
670 : DIR_SEPARATOR_STR,
671 fil_ptr);
672 GNUNET_free (fm);
672 return buffer; 673 return buffer;
673} 674}
674 675
@@ -683,47 +684,47 @@ GNUNET_STRINGS_filename_expand(const char *fil)
683 * @return time as human-readable string 684 * @return time as human-readable string
684 */ 685 */
685const char * 686const char *
686GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, 687GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
687 int do_round) 688 int do_round)
688{ 689{
689 static char buf[128]; 690 static char buf[128];
690 const char *unit = _(/* time unit */ "µs"); 691 const char *unit = _ (/* time unit */ "µs");
691 uint64_t dval = delta.rel_value_us; 692 uint64_t dval = delta.rel_value_us;
692 693
693 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) 694 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us)
694 return _("forever"); 695 return _ ("forever");
695 if (0 == delta.rel_value_us) 696 if (0 == delta.rel_value_us)
696 return _("0 ms"); 697 return _ ("0 ms");
697 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) 698 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
699 {
700 dval = dval / 1000;
701 unit = _ (/* time unit */ "ms");
702 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
698 { 703 {
699 dval = dval / 1000; 704 dval = dval / 1000;
700 unit = _(/* time unit */ "ms"); 705 unit = _ (/* time unit */ "s");
701 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) 706 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
707 {
708 dval = dval / 60;
709 unit = _ (/* time unit */ "m");
710 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
702 { 711 {
703 dval = dval / 1000; 712 dval = dval / 60;
704 unit = _(/* time unit */ "s"); 713 unit = _ (/* time unit */ "h");
705 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) 714 if (((GNUNET_YES == do_round) && (dval > 5 * 24)) ||
706 { 715 (0 == (dval % 24)))
707 dval = dval / 60; 716 {
708 unit = _(/* time unit */ "m"); 717 dval = dval / 24;
709 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) 718 if (1 == dval)
710 { 719 unit = _ (/* time unit */ "day");
711 dval = dval / 60; 720 else
712 unit = _(/* time unit */ "h"); 721 unit = _ (/* time unit */ "days");
713 if (((GNUNET_YES == do_round) && (dval > 5 * 24)) || 722 }
714 (0 == (dval % 24)))
715 {
716 dval = dval / 24;
717 if (1 == dval)
718 unit = _(/* time unit */ "day");
719 else
720 unit = _(/* time unit */ "days");
721 }
722 }
723 }
724 } 723 }
724 }
725 } 725 }
726 GNUNET_snprintf(buf, sizeof(buf), "%llu %s", dval, unit); 726 }
727 GNUNET_snprintf (buf, sizeof(buf), "%llu %s", dval, unit);
727 return buf; 728 return buf;
728} 729}
729 730
@@ -738,16 +739,16 @@ GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta,
738 * @return timestamp in human-readable form in local time 739 * @return timestamp in human-readable form in local time
739 */ 740 */
740const char * 741const char *
741GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t) 742GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
742{ 743{
743 static char buf[255]; 744 static char buf[255];
744 time_t tt; 745 time_t tt;
745 struct tm *tp; 746 struct tm *tp;
746 747
747 if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 748 if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
748 return _("end of time"); 749 return _ ("end of time");
749 tt = t.abs_value_us / 1000LL / 1000LL; 750 tt = t.abs_value_us / 1000LL / 1000LL;
750 tp = localtime(&tt); 751 tp = localtime (&tt);
751 /* This is hacky, but i don't know a way to detect libc character encoding. 752 /* This is hacky, but i don't know a way to detect libc character encoding.
752 * Just expect utf8 from glibc these days. 753 * Just expect utf8 from glibc these days.
753 * As for msvcrt, use the wide variant, which always returns utf16 754 * As for msvcrt, use the wide variant, which always returns utf16
@@ -755,7 +756,7 @@ GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
755 * set conversion routines to convert to UTF8). 756 * set conversion routines to convert to UTF8).
756 */ 757 */
757 758
758 strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp); 759 strftime (buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp);
759 760
760 return buf; 761 return buf;
761} 762}
@@ -773,12 +774,12 @@ GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
773 * itself. 774 * itself.
774 */ 775 */
775const char * 776const char *
776GNUNET_STRINGS_get_short_name(const char *filename) 777GNUNET_STRINGS_get_short_name (const char *filename)
777{ 778{
778 const char *short_fn = filename; 779 const char *short_fn = filename;
779 const char *ss; 780 const char *ss;
780 781
781 while (NULL != (ss = strstr(short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0')) 782 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
782 short_fn = 1 + ss; 783 short_fn = 1 + ss;
783 return short_fn; 784 return short_fn;
784} 785}
@@ -792,51 +793,51 @@ GNUNET_STRINGS_get_short_name(const char *filename)
792 * @return corresponding numeric value 793 * @return corresponding numeric value
793 */ 794 */
794static unsigned int 795static unsigned int
795getValue__(unsigned char a) 796getValue__ (unsigned char a)
796{ 797{
797 unsigned int dec; 798 unsigned int dec;
798 799
799 switch (a) 800 switch (a)
800 { 801 {
801 case 'O': 802 case 'O':
802 case 'o': 803 case 'o':
803 a = '0'; 804 a = '0';
804 break; 805 break;
805 806
806 case 'i': 807 case 'i':
807 case 'I': 808 case 'I':
808 case 'l': 809 case 'l':
809 case 'L': 810 case 'L':
810 a = '1'; 811 a = '1';
811 break; 812 break;
812 813
813 /* also consider U to be V */ 814 /* also consider U to be V */
814 case 'u': 815 case 'u':
815 case 'U': 816 case 'U':
816 a = 'V'; 817 a = 'V';
817 break; 818 break;
818 819
819 default: 820 default:
820 break; 821 break;
821 } 822 }
822 if ((a >= '0') && (a <= '9')) 823 if ((a >= '0') && (a <= '9'))
823 return a - '0'; 824 return a - '0';
824 if ((a >= 'a') && (a <= 'z')) 825 if ((a >= 'a') && (a <= 'z'))
825 a = toupper(a); 826 a = toupper (a);
826 /* return (a - 'a' + 10); */ 827 /* return (a - 'a' + 10); */
827 dec = 0; 828 dec = 0;
828 if ((a >= 'A') && (a <= 'Z')) 829 if ((a >= 'A') && (a <= 'Z'))
829 { 830 {
830 if ('I' < a) 831 if ('I' < a)
831 dec++; 832 dec++;
832 if ('L' < a) 833 if ('L' < a)
833 dec++; 834 dec++;
834 if ('O' < a) 835 if ('O' < a)
835 dec++; 836 dec++;
836 if ('U' < a) 837 if ('U' < a)
837 dec++; 838 dec++;
838 return(a - 'A' + 10 - dec); 839 return(a - 'A' + 10 - dec);
839 } 840 }
840 return -1; 841 return -1;
841} 842}
842 843
@@ -854,10 +855,10 @@ getValue__(unsigned char a)
854 * @return pointer to the next byte in @a out or NULL on error. 855 * @return pointer to the next byte in @a out or NULL on error.
855 */ 856 */
856char * 857char *
857GNUNET_STRINGS_data_to_string(const void *data, 858GNUNET_STRINGS_data_to_string (const void *data,
858 size_t size, 859 size_t size,
859 char *out, 860 char *out,
860 size_t out_size) 861 size_t out_size)
861{ 862{
862 /** 863 /**
863 * 32 characters for encoding 864 * 32 characters for encoding
@@ -871,36 +872,36 @@ GNUNET_STRINGS_data_to_string(const void *data,
871 872
872 udata = data; 873 udata = data;
873 if (out_size < (size * 8 + 4) / 5) 874 if (out_size < (size * 8 + 4) / 5)
874 { 875 {
875 GNUNET_break(0); 876 GNUNET_break (0);
876 return NULL; 877 return NULL;
877 } 878 }
878 vbit = 0; 879 vbit = 0;
879 wpos = 0; 880 wpos = 0;
880 rpos = 0; 881 rpos = 0;
881 bits = 0; 882 bits = 0;
882 while ((rpos < size) || (vbit > 0)) 883 while ((rpos < size) || (vbit > 0))
884 {
885 if ((rpos < size) && (vbit < 5))
883 { 886 {
884 if ((rpos < size) && (vbit < 5)) 887 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
885 { 888 vbit += 8;
886 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
887 vbit += 8;
888 }
889 if (vbit < 5)
890 {
891 bits <<= (5 - vbit); /* zero-padding */
892 GNUNET_assert(vbit == ((size * 8) % 5));
893 vbit = 5;
894 }
895 if (wpos >= out_size)
896 {
897 GNUNET_break(0);
898 return NULL;
899 }
900 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
901 vbit -= 5;
902 } 889 }
903 GNUNET_assert(0 == vbit); 890 if (vbit < 5)
891 {
892 bits <<= (5 - vbit); /* zero-padding */
893 GNUNET_assert (vbit == ((size * 8) % 5));
894 vbit = 5;
895 }
896 if (wpos >= out_size)
897 {
898 GNUNET_break (0);
899 return NULL;
900 }
901 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
902 vbit -= 5;
903 }
904 GNUNET_assert (0 == vbit);
904 if (wpos < out_size) 905 if (wpos < out_size)
905 out[wpos] = '\0'; 906 out[wpos] = '\0';
906 return &out[wpos]; 907 return &out[wpos];
@@ -918,7 +919,7 @@ GNUNET_STRINGS_data_to_string(const void *data,
918 * @return freshly allocated, null-terminated string 919 * @return freshly allocated, null-terminated string
919 */ 920 */
920char * 921char *
921GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size) 922GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size)
922{ 923{
923 char *str_buf; 924 char *str_buf;
924 size_t len = size * 8; 925 size_t len = size * 8;
@@ -927,13 +928,13 @@ GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
927 if (len % 5 > 0) 928 if (len % 5 > 0)
928 len += 5 - len % 5; 929 len += 5 - len % 5;
929 len /= 5; 930 len /= 5;
930 str_buf = GNUNET_malloc(len + 1); 931 str_buf = GNUNET_malloc (len + 1);
931 end = GNUNET_STRINGS_data_to_string(buf, size, str_buf, len); 932 end = GNUNET_STRINGS_data_to_string (buf, size, str_buf, len);
932 if (NULL == end) 933 if (NULL == end)
933 { 934 {
934 GNUNET_free(str_buf); 935 GNUNET_free (str_buf);
935 return NULL; 936 return NULL;
936 } 937 }
937 *end = '\0'; 938 *end = '\0';
938 return str_buf; 939 return str_buf;
939} 940}
@@ -950,10 +951,10 @@ GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
950 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding 951 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
951 */ 952 */
952int 953int
953GNUNET_STRINGS_string_to_data(const char *enc, 954GNUNET_STRINGS_string_to_data (const char *enc,
954 size_t enclen, 955 size_t enclen,
955 void *out, 956 void *out,
956 size_t out_size) 957 size_t out_size)
957{ 958{
958 unsigned int rpos; 959 unsigned int rpos;
959 unsigned int wpos; 960 unsigned int wpos;
@@ -965,48 +966,48 @@ GNUNET_STRINGS_string_to_data(const char *enc,
965 unsigned int encoded_len = out_size * 8; 966 unsigned int encoded_len = out_size * 8;
966 967
967 if (0 == enclen) 968 if (0 == enclen)
968 { 969 {
969 if (0 == out_size) 970 if (0 == out_size)
970 return GNUNET_OK; 971 return GNUNET_OK;
971 return GNUNET_SYSERR; 972 return GNUNET_SYSERR;
972 } 973 }
973 uout = out; 974 uout = out;
974 wpos = out_size; 975 wpos = out_size;
975 rpos = enclen; 976 rpos = enclen;
976 if ((encoded_len % 5) > 0) 977 if ((encoded_len % 5) > 0)
977 { 978 {
978 vbit = encoded_len % 5; /* padding! */ 979 vbit = encoded_len % 5; /* padding! */
979 shift = 5 - vbit; 980 shift = 5 - vbit;
980 bits = (ret = getValue__(enc[--rpos])) >> shift; 981 bits = (ret = getValue__ (enc[--rpos])) >> shift;
981 } 982 }
982 else 983 else
983 { 984 {
984 vbit = 5; 985 vbit = 5;
985 shift = 0; 986 shift = 0;
986 bits = (ret = getValue__(enc[--rpos])); 987 bits = (ret = getValue__ (enc[--rpos]));
987 } 988 }
988 if ((encoded_len + shift) / 5 != enclen) 989 if ((encoded_len + shift) / 5 != enclen)
989 return GNUNET_SYSERR; 990 return GNUNET_SYSERR;
990 if (-1 == ret) 991 if (-1 == ret)
991 return GNUNET_SYSERR; 992 return GNUNET_SYSERR;
992 while (wpos > 0) 993 while (wpos > 0)
994 {
995 if (0 == rpos)
993 { 996 {
994 if (0 == rpos) 997 GNUNET_break (0);
995 { 998 return GNUNET_SYSERR;
996 GNUNET_break(0); 999 }
997 return GNUNET_SYSERR; 1000 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
998 } 1001 if (-1 == ret)
999 bits = ((ret = getValue__(enc[--rpos])) << vbit) | bits; 1002 return GNUNET_SYSERR;
1000 if (-1 == ret) 1003 vbit += 5;
1001 return GNUNET_SYSERR; 1004 if (vbit >= 8)
1002 vbit += 5; 1005 {
1003 if (vbit >= 8) 1006 uout[--wpos] = (unsigned char) bits;
1004 { 1007 bits >>= 8;
1005 uout[--wpos] = (unsigned char)bits; 1008 vbit -= 8;
1006 bits >>= 8;
1007 vbit -= 8;
1008 }
1009 } 1009 }
1010 }
1010 if ((0 != rpos) || (0 != vbit)) 1011 if ((0 != rpos) || (0 != vbit))
1011 return GNUNET_SYSERR; 1012 return GNUNET_SYSERR;
1012 return GNUNET_OK; 1013 return GNUNET_OK;
@@ -1030,9 +1031,9 @@ GNUNET_STRINGS_string_to_data(const char *enc,
1030 * (if they weren't NULL). 1031 * (if they weren't NULL).
1031 */ 1032 */
1032int 1033int
1033GNUNET_STRINGS_parse_uri(const char *path, 1034GNUNET_STRINGS_parse_uri (const char *path,
1034 char **scheme_part, 1035 char **scheme_part,
1035 const char **path_part) 1036 const char **path_part)
1036{ 1037{
1037 size_t len; 1038 size_t len;
1038 size_t i; 1039 size_t i;
@@ -1040,51 +1041,51 @@ GNUNET_STRINGS_parse_uri(const char *path,
1040 int pp_state = 0; 1041 int pp_state = 0;
1041 const char *post_scheme_part = NULL; 1042 const char *post_scheme_part = NULL;
1042 1043
1043 len = strlen(path); 1044 len = strlen (path);
1044 for (end = 0, i = 0; !end && i < len; i++) 1045 for (end = 0, i = 0; ! end && i < len; i++)
1046 {
1047 switch (pp_state)
1045 { 1048 {
1046 switch (pp_state) 1049 case 0:
1047 { 1050 if ((path[i] == ':') && (i > 0))
1048 case 0: 1051 {
1049 if ((path[i] == ':') && (i > 0)) 1052 pp_state += 1;
1050 { 1053 continue;
1051 pp_state += 1; 1054 }
1052 continue; 1055 if (! (((path[i] >= 'A') &&(path[i] <= 'Z') ) ||
1053 } 1056 ((path[i] >= 'a') &&(path[i] <= 'z') ) ||
1054 if (!((path[i] >= 'A' && path[i] <= 'Z') || 1057 ((path[i] >= '0') &&(path[i] <= '9') ) ||(path[i] == '+') ||
1055 (path[i] >= 'a' && path[i] <= 'z') || 1058 (path[i] == '-') || (path[i] == '.')))
1056 (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || 1059 end = 1;
1057 path[i] == '-' || (path[i] == '.'))) 1060 break;
1058 end = 1;
1059 break;
1060 1061
1061 case 1: 1062 case 1:
1062 case 2: 1063 case 2:
1063 if (path[i] == '/') 1064 if (path[i] == '/')
1064 { 1065 {
1065 pp_state += 1; 1066 pp_state += 1;
1066 continue; 1067 continue;
1067 } 1068 }
1068 end = 1; 1069 end = 1;
1069 break; 1070 break;
1070 1071
1071 case 3: 1072 case 3:
1072 post_scheme_part = &path[i]; 1073 post_scheme_part = &path[i];
1073 end = 1; 1074 end = 1;
1074 break; 1075 break;
1075 1076
1076 default: 1077 default:
1077 end = 1; 1078 end = 1;
1078 }
1079 } 1079 }
1080 }
1080 if (post_scheme_part == NULL) 1081 if (post_scheme_part == NULL)
1081 return GNUNET_NO; 1082 return GNUNET_NO;
1082 if (scheme_part) 1083 if (scheme_part)
1083 { 1084 {
1084 *scheme_part = GNUNET_malloc(post_scheme_part - path + 1); 1085 *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
1085 GNUNET_memcpy(*scheme_part, path, post_scheme_part - path); 1086 GNUNET_memcpy (*scheme_part, path, post_scheme_part - path);
1086 (*scheme_part)[post_scheme_part - path] = '\0'; 1087 (*scheme_part)[post_scheme_part - path] = '\0';
1087 } 1088 }
1088 if (path_part) 1089 if (path_part)
1089 *path_part = post_scheme_part; 1090 *path_part = post_scheme_part;
1090 return GNUNET_YES; 1091 return GNUNET_YES;
@@ -1106,10 +1107,10 @@ GNUNET_STRINGS_parse_uri(const char *path,
1106 * @return #GNUNET_YES if @a filename is absolute, #GNUNET_NO otherwise. 1107 * @return #GNUNET_YES if @a filename is absolute, #GNUNET_NO otherwise.
1107 */ 1108 */
1108int 1109int
1109GNUNET_STRINGS_path_is_absolute(const char *filename, 1110GNUNET_STRINGS_path_is_absolute (const char *filename,
1110 int can_be_uri, 1111 int can_be_uri,
1111 int *r_is_uri, 1112 int *r_is_uri,
1112 char **r_uri_scheme) 1113 char **r_uri_scheme)
1113{ 1114{
1114 const char *post_scheme_path; 1115 const char *post_scheme_path;
1115 int is_uri; 1116 int is_uri;
@@ -1120,28 +1121,28 @@ GNUNET_STRINGS_path_is_absolute(const char *filename,
1120 if (filename[0] == '/') 1121 if (filename[0] == '/')
1121 return GNUNET_YES; 1122 return GNUNET_YES;
1122 if (can_be_uri) 1123 if (can_be_uri)
1124 {
1125 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1126 if (r_is_uri)
1127 *r_is_uri = is_uri;
1128 if (is_uri)
1123 { 1129 {
1124 is_uri = GNUNET_STRINGS_parse_uri(filename, &uri, &post_scheme_path); 1130 if (r_uri_scheme)
1125 if (r_is_uri) 1131 *r_uri_scheme = uri;
1126 *r_is_uri = is_uri; 1132 else
1127 if (is_uri) 1133 GNUNET_free_non_null (uri);
1128 {
1129 if (r_uri_scheme)
1130 *r_uri_scheme = uri;
1131 else
1132 GNUNET_free_non_null(uri);
1133 1134
1134 return GNUNET_STRINGS_path_is_absolute(post_scheme_path, 1135 return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1135 GNUNET_NO, 1136 GNUNET_NO,
1136 NULL, 1137 NULL,
1137 NULL); 1138 NULL);
1138 }
1139 } 1139 }
1140 }
1140 else 1141 else
1141 { 1142 {
1142 if (r_is_uri) 1143 if (r_is_uri)
1143 *r_is_uri = GNUNET_NO; 1144 *r_is_uri = GNUNET_NO;
1144 } 1145 }
1145 1146
1146 return GNUNET_NO; 1147 return GNUNET_NO;
1147} 1148}
@@ -1156,33 +1157,33 @@ GNUNET_STRINGS_path_is_absolute(const char *filename,
1156 * fails, #GNUNET_SYSERR when a check can't be performed 1157 * fails, #GNUNET_SYSERR when a check can't be performed
1157 */ 1158 */
1158int 1159int
1159GNUNET_STRINGS_check_filename(const char *filename, 1160GNUNET_STRINGS_check_filename (const char *filename,
1160 enum GNUNET_STRINGS_FilenameCheck checks) 1161 enum GNUNET_STRINGS_FilenameCheck checks)
1161{ 1162{
1162 struct stat st; 1163 struct stat st;
1163 1164
1164 if ((NULL == filename) || (filename[0] == '\0')) 1165 if ((NULL == filename) || (filename[0] == '\0'))
1165 return GNUNET_SYSERR; 1166 return GNUNET_SYSERR;
1166 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) 1167 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1167 if (!GNUNET_STRINGS_path_is_absolute(filename, GNUNET_NO, NULL, NULL)) 1168 if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
1168 return GNUNET_NO; 1169 return GNUNET_NO;
1169 if (0 != (checks & 1170 if (0 != (checks
1170 (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY | 1171 & (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY
1171 GNUNET_STRINGS_CHECK_IS_LINK))) 1172 | GNUNET_STRINGS_CHECK_IS_LINK)))
1173 {
1174 if (0 != stat (filename, &st))
1172 { 1175 {
1173 if (0 != stat(filename, &st)) 1176 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1174 { 1177 return GNUNET_NO;
1175 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) 1178 else
1176 return GNUNET_NO; 1179 return GNUNET_SYSERR;
1177 else
1178 return GNUNET_SYSERR;
1179 }
1180 } 1180 }
1181 }
1181 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) 1182 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1182 if (!S_ISDIR(st.st_mode)) 1183 if (! S_ISDIR (st.st_mode))
1183 return GNUNET_NO; 1184 return GNUNET_NO;
1184 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) 1185 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1185 if (!S_ISLNK(st.st_mode)) 1186 if (! S_ISLNK (st.st_mode))
1186 return GNUNET_NO; 1187 return GNUNET_NO;
1187 return GNUNET_YES; 1188 return GNUNET_YES;
1188} 1189}
@@ -1201,9 +1202,9 @@ GNUNET_STRINGS_check_filename(const char *filename,
1201 * case the contents of @a r_buf are undefined. 1202 * case the contents of @a r_buf are undefined.
1202 */ 1203 */
1203int 1204int
1204GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, 1205GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1205 uint16_t addrlen, 1206 uint16_t addrlen,
1206 struct sockaddr_in6 *r_buf) 1207 struct sockaddr_in6 *r_buf)
1207{ 1208{
1208 char zbuf[addrlen + 1]; 1209 char zbuf[addrlen + 1];
1209 int ret; 1210 int ret;
@@ -1213,51 +1214,51 @@ GNUNET_STRINGS_to_address_ipv6(const char *zt_addr,
1213 1214
1214 if (addrlen < 6) 1215 if (addrlen < 6)
1215 return GNUNET_SYSERR; 1216 return GNUNET_SYSERR;
1216 GNUNET_memcpy(zbuf, zt_addr, addrlen); 1217 GNUNET_memcpy (zbuf, zt_addr, addrlen);
1217 if ('[' != zbuf[0]) 1218 if ('[' != zbuf[0])
1218 { 1219 {
1219 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1220 _("IPv6 address did not start with `['\n")); 1221 _ ("IPv6 address did not start with `['\n"));
1221 return GNUNET_SYSERR; 1222 return GNUNET_SYSERR;
1222 } 1223 }
1223 zbuf[addrlen] = '\0'; 1224 zbuf[addrlen] = '\0';
1224 port_colon = strrchr(zbuf, ':'); 1225 port_colon = strrchr (zbuf, ':');
1225 if (NULL == port_colon) 1226 if (NULL == port_colon)
1226 { 1227 {
1227 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1228 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1228 _("IPv6 address did contain ':' to separate port number\n")); 1229 _ ("IPv6 address did contain ':' to separate port number\n"));
1229 return GNUNET_SYSERR; 1230 return GNUNET_SYSERR;
1230 } 1231 }
1231 if (']' != *(port_colon - 1)) 1232 if (']' != *(port_colon - 1))
1232 { 1233 {
1233 GNUNET_log( 1234 GNUNET_log (
1234 GNUNET_ERROR_TYPE_WARNING, 1235 GNUNET_ERROR_TYPE_WARNING,
1235 _("IPv6 address did contain ']' before ':' to separate port number\n")); 1236 _ ("IPv6 address did contain ']' before ':' to separate port number\n"));
1236 return GNUNET_SYSERR; 1237 return GNUNET_SYSERR;
1237 } 1238 }
1238 ret = sscanf(port_colon, ":%u%1s", &port, dummy); 1239 ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1239 if ((1 != ret) || (port > 65535)) 1240 if ((1 != ret) || (port > 65535))
1240 { 1241 {
1241 GNUNET_log( 1242 GNUNET_log (
1242 GNUNET_ERROR_TYPE_WARNING, 1243 GNUNET_ERROR_TYPE_WARNING,
1243 _("IPv6 address did contain a valid port number after the last ':'\n")); 1244 _ ("IPv6 address did contain a valid port number after the last ':'\n"));
1244 return GNUNET_SYSERR; 1245 return GNUNET_SYSERR;
1245 } 1246 }
1246 *(port_colon - 1) = '\0'; 1247 *(port_colon - 1) = '\0';
1247 memset(r_buf, 0, sizeof(struct sockaddr_in6)); 1248 memset (r_buf, 0, sizeof(struct sockaddr_in6));
1248 ret = inet_pton(AF_INET6, &zbuf[1], &r_buf->sin6_addr); 1249 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1249 if (ret <= 0) 1250 if (ret <= 0)
1250 { 1251 {
1251 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1252 _("Invalid IPv6 address `%s': %s\n"), 1253 _ ("Invalid IPv6 address `%s': %s\n"),
1253 &zbuf[1], 1254 &zbuf[1],
1254 strerror(errno)); 1255 strerror (errno));
1255 return GNUNET_SYSERR; 1256 return GNUNET_SYSERR;
1256 } 1257 }
1257 r_buf->sin6_port = htons(port); 1258 r_buf->sin6_port = htons (port);
1258 r_buf->sin6_family = AF_INET6; 1259 r_buf->sin6_family = AF_INET6;
1259#if HAVE_SOCKADDR_IN_SIN_LEN 1260#if HAVE_SOCKADDR_IN_SIN_LEN
1260 r_buf->sin6_len = (u_char)sizeof(struct sockaddr_in6); 1261 r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1261#endif 1262#endif
1262 return GNUNET_OK; 1263 return GNUNET_OK;
1263} 1264}
@@ -1275,9 +1276,9 @@ GNUNET_STRINGS_to_address_ipv6(const char *zt_addr,
1275 * the contents of @a r_buf are undefined. 1276 * the contents of @a r_buf are undefined.
1276 */ 1277 */
1277int 1278int
1278GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, 1279GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
1279 uint16_t addrlen, 1280 uint16_t addrlen,
1280 struct sockaddr_in *r_buf) 1281 struct sockaddr_in *r_buf)
1281{ 1282{
1282 unsigned int temps[4]; 1283 unsigned int temps[4];
1283 unsigned int port; 1284 unsigned int port;
@@ -1286,14 +1287,14 @@ GNUNET_STRINGS_to_address_ipv4(const char *zt_addr,
1286 1287
1287 if (addrlen < 9) 1288 if (addrlen < 9)
1288 return GNUNET_SYSERR; 1289 return GNUNET_SYSERR;
1289 cnt = sscanf(zt_addr, 1290 cnt = sscanf (zt_addr,
1290 "%u.%u.%u.%u:%u%1s", 1291 "%u.%u.%u.%u:%u%1s",
1291 &temps[0], 1292 &temps[0],
1292 &temps[1], 1293 &temps[1],
1293 &temps[2], 1294 &temps[2],
1294 &temps[3], 1295 &temps[3],
1295 &port, 1296 &port,
1296 dummy); 1297 dummy);
1297 if (5 != cnt) 1298 if (5 != cnt)
1298 return GNUNET_SYSERR; 1299 return GNUNET_SYSERR;
1299 for (cnt = 0; cnt < 4; cnt++) 1300 for (cnt = 0; cnt < 4; cnt++)
@@ -1302,11 +1303,11 @@ GNUNET_STRINGS_to_address_ipv4(const char *zt_addr,
1302 if (port > 65535) 1303 if (port > 65535)
1303 return GNUNET_SYSERR; 1304 return GNUNET_SYSERR;
1304 r_buf->sin_family = AF_INET; 1305 r_buf->sin_family = AF_INET;
1305 r_buf->sin_port = htons(port); 1306 r_buf->sin_port = htons (port);
1306 r_buf->sin_addr.s_addr = 1307 r_buf->sin_addr.s_addr =
1307 htonl((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]); 1308 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1308#if HAVE_SOCKADDR_IN_SIN_LEN 1309#if HAVE_SOCKADDR_IN_SIN_LEN
1309 r_buf->sin_len = (u_char)sizeof(struct sockaddr_in); 1310 r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1310#endif 1311#endif
1311 return GNUNET_OK; 1312 return GNUNET_OK;
1312} 1313}
@@ -1324,17 +1325,17 @@ GNUNET_STRINGS_to_address_ipv4(const char *zt_addr,
1324 * case the contents of @a r_buf are undefined. 1325 * case the contents of @a r_buf are undefined.
1325 */ 1326 */
1326int 1327int
1327GNUNET_STRINGS_to_address_ip(const char *addr, 1328GNUNET_STRINGS_to_address_ip (const char *addr,
1328 uint16_t addrlen, 1329 uint16_t addrlen,
1329 struct sockaddr_storage *r_buf) 1330 struct sockaddr_storage *r_buf)
1330{ 1331{
1331 if (addr[0] == '[') 1332 if (addr[0] == '[')
1332 return GNUNET_STRINGS_to_address_ipv6(addr, 1333 return GNUNET_STRINGS_to_address_ipv6 (addr,
1333 addrlen, 1334 addrlen,
1334 (struct sockaddr_in6 *)r_buf); 1335 (struct sockaddr_in6 *) r_buf);
1335 return GNUNET_STRINGS_to_address_ipv4(addr, 1336 return GNUNET_STRINGS_to_address_ipv4 (addr,
1336 addrlen, 1337 addrlen,
1337 (struct sockaddr_in *)r_buf); 1338 (struct sockaddr_in *) r_buf);
1338} 1339}
1339 1340
1340 1341
@@ -1348,49 +1349,49 @@ GNUNET_STRINGS_to_address_ip(const char *addr,
1348 * @return 0 on error, otherwise number of bytes in @a sa 1349 * @return 0 on error, otherwise number of bytes in @a sa
1349 */ 1350 */
1350size_t 1351size_t
1351GNUNET_STRINGS_parse_socket_addr(const char *addr, 1352GNUNET_STRINGS_parse_socket_addr (const char *addr,
1352 uint8_t *af, 1353 uint8_t *af,
1353 struct sockaddr **sa) 1354 struct sockaddr **sa)
1354{ 1355{
1355 char *cp = GNUNET_strdup(addr); 1356 char *cp = GNUNET_strdup (addr);
1356 1357
1357 *af = AF_UNSPEC; 1358 *af = AF_UNSPEC;
1358 if ('[' == *addr) 1359 if ('[' == *addr)
1360 {
1361 /* IPv6 */
1362 *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1363 if (GNUNET_OK !=
1364 GNUNET_STRINGS_to_address_ipv6 (cp,
1365 strlen (cp),
1366 (struct sockaddr_in6 *) *sa))
1359 { 1367 {
1360 /* IPv6 */ 1368 GNUNET_free (*sa);
1361 *sa = GNUNET_malloc(sizeof(struct sockaddr_in6)); 1369 *sa = NULL;
1362 if (GNUNET_OK != 1370 GNUNET_free (cp);
1363 GNUNET_STRINGS_to_address_ipv6(cp, 1371 return 0;
1364 strlen(cp),
1365 (struct sockaddr_in6 *)*sa))
1366 {
1367 GNUNET_free(*sa);
1368 *sa = NULL;
1369 GNUNET_free(cp);
1370 return 0;
1371 }
1372 *af = AF_INET6;
1373 GNUNET_free(cp);
1374 return sizeof(struct sockaddr_in6);
1375 } 1372 }
1373 *af = AF_INET6;
1374 GNUNET_free (cp);
1375 return sizeof(struct sockaddr_in6);
1376 }
1376 else 1377 else
1378 {
1379 /* IPv4 */
1380 *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1381 if (GNUNET_OK !=
1382 GNUNET_STRINGS_to_address_ipv4 (cp,
1383 strlen (cp),
1384 (struct sockaddr_in *) *sa))
1377 { 1385 {
1378 /* IPv4 */ 1386 GNUNET_free (*sa);
1379 *sa = GNUNET_malloc(sizeof(struct sockaddr_in)); 1387 *sa = NULL;
1380 if (GNUNET_OK != 1388 GNUNET_free (cp);
1381 GNUNET_STRINGS_to_address_ipv4(cp, 1389 return 0;
1382 strlen(cp),
1383 (struct sockaddr_in *)*sa))
1384 {
1385 GNUNET_free(*sa);
1386 *sa = NULL;
1387 GNUNET_free(cp);
1388 return 0;
1389 }
1390 *af = AF_INET;
1391 GNUNET_free(cp);
1392 return sizeof(struct sockaddr_in);
1393 } 1390 }
1391 *af = AF_INET;
1392 GNUNET_free (cp);
1393 return sizeof(struct sockaddr_in);
1394 }
1394} 1395}
1395 1396
1396 1397
@@ -1399,7 +1400,7 @@ GNUNET_STRINGS_parse_socket_addr(const char *addr,
1399 * freed with a single call to GNUNET_free(); 1400 * freed with a single call to GNUNET_free();
1400 */ 1401 */
1401static char *const * 1402static char *const *
1402_make_continuous_arg_copy(int argc, char *const *argv) 1403_make_continuous_arg_copy (int argc, char *const *argv)
1403{ 1404{
1404 size_t argvsize = 0; 1405 size_t argvsize = 0;
1405 int i; 1406 int i;
@@ -1407,17 +1408,17 @@ _make_continuous_arg_copy(int argc, char *const *argv)
1407 char *p; 1408 char *p;
1408 1409
1409 for (i = 0; i < argc; i++) 1410 for (i = 0; i < argc; i++)
1410 argvsize += strlen(argv[i]) + 1 + sizeof(char *); 1411 argvsize += strlen (argv[i]) + 1 + sizeof(char *);
1411 new_argv = GNUNET_malloc(argvsize + sizeof(char *)); 1412 new_argv = GNUNET_malloc (argvsize + sizeof(char *));
1412 p = (char *)&new_argv[argc + 1]; 1413 p = (char *) &new_argv[argc + 1];
1413 for (i = 0; i < argc; i++) 1414 for (i = 0; i < argc; i++)
1414 { 1415 {
1415 new_argv[i] = p; 1416 new_argv[i] = p;
1416 strcpy(p, argv[i]); 1417 strcpy (p, argv[i]);
1417 p += strlen(argv[i]) + 1; 1418 p += strlen (argv[i]) + 1;
1418 } 1419 }
1419 new_argv[argc] = NULL; 1420 new_argv[argc] = NULL;
1420 return (char *const *)new_argv; 1421 return (char *const *) new_argv;
1421} 1422}
1422 1423
1423 1424
@@ -1436,13 +1437,13 @@ _make_continuous_arg_copy(int argc, char *const *argv)
1436 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 1437 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1437 */ 1438 */
1438int 1439int
1439GNUNET_STRINGS_get_utf8_args(int argc, 1440GNUNET_STRINGS_get_utf8_args (int argc,
1440 char *const *argv, 1441 char *const *argv,
1441 int *u8argc, 1442 int *u8argc,
1442 char *const **u8argv) 1443 char *const **u8argv)
1443{ 1444{
1444 char *const *new_argv = 1445 char *const *new_argv =
1445 (char *const *)_make_continuous_arg_copy(argc, argv); 1446 (char *const *) _make_continuous_arg_copy (argc, argv);
1446 *u8argv = new_argv; 1447 *u8argv = new_argv;
1447 *u8argc = argc; 1448 *u8argc = argc;
1448 return GNUNET_OK; 1449 return GNUNET_OK;
@@ -1459,8 +1460,8 @@ GNUNET_STRINGS_get_utf8_args(int argc,
1459 * @a port_policy is malformed 1460 * @a port_policy is malformed
1460 */ 1461 */
1461static int 1462static int
1462parse_port_policy(const char *port_policy, 1463parse_port_policy (const char *port_policy,
1463 struct GNUNET_STRINGS_PortPolicy *pp) 1464 struct GNUNET_STRINGS_PortPolicy *pp)
1464{ 1465{
1465 const char *pos; 1466 const char *pos;
1466 int s; 1467 int s;
@@ -1469,36 +1470,36 @@ parse_port_policy(const char *port_policy,
1469 1470
1470 pos = port_policy; 1471 pos = port_policy;
1471 if ('!' == *pos) 1472 if ('!' == *pos)
1473 {
1474 pp->negate_portrange = GNUNET_YES;
1475 pos++;
1476 }
1477 if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol))
1478 {
1479 if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF))
1472 { 1480 {
1473 pp->negate_portrange = GNUNET_YES; 1481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1474 pos++; 1482 return GNUNET_SYSERR;
1475 } 1483 }
1476 if (2 == sscanf(pos, "%u-%u%1s", &s, &e, eol)) 1484 pp->start_port = (uint16_t) s;
1485 pp->end_port = (uint16_t) e;
1486 return GNUNET_OK;
1487 }
1488 if (1 == sscanf (pos, "%u%1s", &s, eol))
1489 {
1490 if ((0 == s) || (s > 0xFFFF))
1477 { 1491 {
1478 if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF)) 1492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1479 { 1493 return GNUNET_SYSERR;
1480 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("Port not in range\n"));
1481 return GNUNET_SYSERR;
1482 }
1483 pp->start_port = (uint16_t)s;
1484 pp->end_port = (uint16_t)e;
1485 return GNUNET_OK;
1486 } 1494 }
1487 if (1 == sscanf(pos, "%u%1s", &s, eol))
1488 {
1489 if ((0 == s) || (s > 0xFFFF))
1490 {
1491 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("Port not in range\n"));
1492 return GNUNET_SYSERR;
1493 }
1494 1495
1495 pp->start_port = (uint16_t)s; 1496 pp->start_port = (uint16_t) s;
1496 pp->end_port = (uint16_t)s; 1497 pp->end_port = (uint16_t) s;
1497 return GNUNET_OK; 1498 return GNUNET_OK;
1498 } 1499 }
1499 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1500 _("Malformed port policy `%s'\n"), 1501 _ ("Malformed port policy `%s'\n"),
1501 port_policy); 1502 port_policy);
1502 return GNUNET_SYSERR; 1503 return GNUNET_SYSERR;
1503} 1504}
1504 1505
@@ -1516,7 +1517,7 @@ parse_port_policy(const char *port_policy,
1516 * NULL if the synatx is flawed 1517 * NULL if the synatx is flawed
1517 */ 1518 */
1518struct GNUNET_STRINGS_IPv4NetworkPolicy * 1519struct GNUNET_STRINGS_IPv4NetworkPolicy *
1519GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX) 1520GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1520{ 1521{
1521 unsigned int count; 1522 unsigned int count;
1522 unsigned int i; 1523 unsigned int i;
@@ -1534,164 +1535,164 @@ GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX)
1534 1535
1535 if (NULL == routeListX) 1536 if (NULL == routeListX)
1536 return NULL; 1537 return NULL;
1537 len = strlen(routeListX); 1538 len = strlen (routeListX);
1538 if (0 == len) 1539 if (0 == len)
1539 return NULL; 1540 return NULL;
1540 routeList = GNUNET_strdup(routeListX); 1541 routeList = GNUNET_strdup (routeListX);
1541 count = 0; 1542 count = 0;
1542 for (i = 0; i < len; i++) 1543 for (i = 0; i < len; i++)
1543 if (routeList[i] == ';') 1544 if (routeList[i] == ';')
1544 count++; 1545 count++;
1545 result = GNUNET_malloc(sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy) * 1546 result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy)
1546 (count + 1)); 1547 * (count + 1));
1547 i = 0; 1548 i = 0;
1548 pos = 0; 1549 pos = 0;
1549 while (i < count) 1550 while (i < count)
1550 { 1551 {
1551 for (colon = pos; ':' != routeList[colon]; colon++) 1552 for (colon = pos; ':' != routeList[colon]; colon++)
1552 if ((';' == routeList[colon]) || ('\0' == routeList[colon])) 1553 if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1553 break; 1554 break;
1554 for (end = colon; ';' != routeList[end]; end++) 1555 for (end = colon; ';' != routeList[end]; end++)
1555 if ('\0' == routeList[end])
1556 break;
1557 if ('\0' == routeList[end]) 1556 if ('\0' == routeList[end])
1558 break; 1557 break;
1559 routeList[end] = '\0'; 1558 if ('\0' == routeList[end])
1560 if (':' == routeList[colon]) 1559 break;
1561 { 1560 routeList[end] = '\0';
1562 routeList[colon] = '\0'; 1561 if (':' == routeList[colon])
1563 if (GNUNET_OK != parse_port_policy(&routeList[colon + 1], &result[i].pp)) 1562 {
1564 break; 1563 routeList[colon] = '\0';
1565 } 1564 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1566 cnt = sscanf(&routeList[pos], 1565 break;
1567 "%u.%u.%u.%u/%u.%u.%u.%u%1s", 1566 }
1568 &temps[0], 1567 cnt = sscanf (&routeList[pos],
1569 &temps[1], 1568 "%u.%u.%u.%u/%u.%u.%u.%u%1s",
1570 &temps[2], 1569 &temps[0],
1571 &temps[3], 1570 &temps[1],
1572 &temps[4], 1571 &temps[2],
1573 &temps[5], 1572 &temps[3],
1574 &temps[6], 1573 &temps[4],
1575 &temps[7], 1574 &temps[5],
1576 dummy); 1575 &temps[6],
1577 if (8 == cnt) 1576 &temps[7],
1577 dummy);
1578 if (8 == cnt)
1579 {
1580 for (j = 0; j < 8; j++)
1581 if (temps[j] > 0xFF)
1578 { 1582 {
1579 for (j = 0; j < 8; j++) 1583 LOG (GNUNET_ERROR_TYPE_WARNING,
1580 if (temps[j] > 0xFF) 1584 _ ("Invalid format for IP: `%s'\n"),
1581 { 1585 &routeList[pos]);
1582 LOG(GNUNET_ERROR_TYPE_WARNING, 1586 GNUNET_free (result);
1583 _("Invalid format for IP: `%s'\n"), 1587 GNUNET_free (routeList);
1584 &routeList[pos]); 1588 return NULL;
1585 GNUNET_free(result);
1586 GNUNET_free(routeList);
1587 return NULL;
1588 }
1589 result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) +
1590 (temps[2] << 8) + temps[3]);
1591 result[i].netmask.s_addr = htonl((temps[4] << 24) + (temps[5] << 16) +
1592 (temps[6] << 8) + temps[7]);
1593 pos = end + 1;
1594 i++;
1595 continue;
1596 } 1589 }
1597 /* try second notation */ 1590 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1598 cnt = sscanf(&routeList[pos], 1591 + (temps[2] << 8) + temps[3]);
1599 "%u.%u.%u.%u/%u%1s", 1592 result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1600 &temps[0], 1593 + (temps[6] << 8) + temps[7]);
1601 &temps[1], 1594 pos = end + 1;
1602 &temps[2], 1595 i++;
1603 &temps[3], 1596 continue;
1604 &slash, 1597 }
1605 dummy); 1598 /* try second notation */
1606 if (5 == cnt) 1599 cnt = sscanf (&routeList[pos],
1600 "%u.%u.%u.%u/%u%1s",
1601 &temps[0],
1602 &temps[1],
1603 &temps[2],
1604 &temps[3],
1605 &slash,
1606 dummy);
1607 if (5 == cnt)
1608 {
1609 for (j = 0; j < 4; j++)
1610 if (temps[j] > 0xFF)
1607 { 1611 {
1608 for (j = 0; j < 4; j++) 1612 LOG (GNUNET_ERROR_TYPE_WARNING,
1609 if (temps[j] > 0xFF) 1613 _ ("Invalid format for IP: `%s'\n"),
1610 { 1614 &routeList[pos]);
1611 LOG(GNUNET_ERROR_TYPE_WARNING, 1615 GNUNET_free (result);
1612 _("Invalid format for IP: `%s'\n"), 1616 GNUNET_free (routeList);
1613 &routeList[pos]); 1617 return NULL;
1614 GNUNET_free(result);
1615 GNUNET_free(routeList);
1616 return NULL;
1617 }
1618 result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) +
1619 (temps[2] << 8) + temps[3]);
1620 if ((slash <= 32) && (slash >= 0))
1621 {
1622 result[i].netmask.s_addr = 0;
1623 while (slash > 0)
1624 {
1625 result[i].netmask.s_addr =
1626 (result[i].netmask.s_addr >> 1) + 0x80000000;
1627 slash--;
1628 }
1629 result[i].netmask.s_addr = htonl(result[i].netmask.s_addr);
1630 pos = end + 1;
1631 i++;
1632 continue;
1633 }
1634 else
1635 {
1636 LOG(GNUNET_ERROR_TYPE_WARNING,
1637 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1638 slash);
1639 GNUNET_free(result);
1640 GNUNET_free(routeList);
1641 return NULL; /* error */
1642 }
1643 } 1618 }
1644 /* try third notation */ 1619 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1645 slash = 32; 1620 + (temps[2] << 8) + temps[3]);
1646 cnt = sscanf(&routeList[pos], 1621 if ((slash <= 32) && (slash >= 0))
1647 "%u.%u.%u.%u%1s", 1622 {
1648 &temps[0], 1623 result[i].netmask.s_addr = 0;
1649 &temps[1], 1624 while (slash > 0)
1650 &temps[2],
1651 &temps[3],
1652 dummy);
1653 if (4 == cnt)
1654 { 1625 {
1655 for (j = 0; j < 4; j++) 1626 result[i].netmask.s_addr =
1656 if (temps[j] > 0xFF) 1627 (result[i].netmask.s_addr >> 1) + 0x80000000;
1657 { 1628 slash--;
1658 LOG(GNUNET_ERROR_TYPE_WARNING,
1659 _("Invalid format for IP: `%s'\n"),
1660 &routeList[pos]);
1661 GNUNET_free(result);
1662 GNUNET_free(routeList);
1663 return NULL;
1664 }
1665 result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) +
1666 (temps[2] << 8) + temps[3]);
1667 result[i].netmask.s_addr = 0;
1668 while (slash > 0)
1669 {
1670 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
1671 slash--;
1672 }
1673 result[i].netmask.s_addr = htonl(result[i].netmask.s_addr);
1674 pos = end + 1;
1675 i++;
1676 continue;
1677 } 1629 }
1678 LOG(GNUNET_ERROR_TYPE_WARNING, 1630 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1679 _("Invalid format for IP: `%s'\n"), 1631 pos = end + 1;
1680 &routeList[pos]); 1632 i++;
1681 GNUNET_free(result); 1633 continue;
1682 GNUNET_free(routeList); 1634 }
1683 return NULL; /* error */ 1635 else
1636 {
1637 LOG (GNUNET_ERROR_TYPE_WARNING,
1638 _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1639 slash);
1640 GNUNET_free (result);
1641 GNUNET_free (routeList);
1642 return NULL; /* error */
1643 }
1684 } 1644 }
1685 if (pos < strlen(routeList)) 1645 /* try third notation */
1646 slash = 32;
1647 cnt = sscanf (&routeList[pos],
1648 "%u.%u.%u.%u%1s",
1649 &temps[0],
1650 &temps[1],
1651 &temps[2],
1652 &temps[3],
1653 dummy);
1654 if (4 == cnt)
1686 { 1655 {
1687 LOG(GNUNET_ERROR_TYPE_WARNING, 1656 for (j = 0; j < 4; j++)
1688 _("Invalid format: `%s'\n"), 1657 if (temps[j] > 0xFF)
1689 &routeListX[pos]); 1658 {
1690 GNUNET_free(result); 1659 LOG (GNUNET_ERROR_TYPE_WARNING,
1691 GNUNET_free(routeList); 1660 _ ("Invalid format for IP: `%s'\n"),
1692 return NULL; /* oops */ 1661 &routeList[pos]);
1662 GNUNET_free (result);
1663 GNUNET_free (routeList);
1664 return NULL;
1665 }
1666 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1667 + (temps[2] << 8) + temps[3]);
1668 result[i].netmask.s_addr = 0;
1669 while (slash > 0)
1670 {
1671 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
1672 slash--;
1673 }
1674 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1675 pos = end + 1;
1676 i++;
1677 continue;
1693 } 1678 }
1694 GNUNET_free(routeList); 1679 LOG (GNUNET_ERROR_TYPE_WARNING,
1680 _ ("Invalid format for IP: `%s'\n"),
1681 &routeList[pos]);
1682 GNUNET_free (result);
1683 GNUNET_free (routeList);
1684 return NULL; /* error */
1685 }
1686 if (pos < strlen (routeList))
1687 {
1688 LOG (GNUNET_ERROR_TYPE_WARNING,
1689 _ ("Invalid format: `%s'\n"),
1690 &routeListX[pos]);
1691 GNUNET_free (result);
1692 GNUNET_free (routeList);
1693 return NULL; /* oops */
1694 }
1695 GNUNET_free (routeList);
1695 return result; /* ok */ 1696 return result; /* ok */
1696} 1697}
1697 1698
@@ -1708,7 +1709,7 @@ GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX)
1708 * @return the converted list, 0-terminated, NULL if the synatx is flawed 1709 * @return the converted list, 0-terminated, NULL if the synatx is flawed
1709 */ 1710 */
1710struct GNUNET_STRINGS_IPv6NetworkPolicy * 1711struct GNUNET_STRINGS_IPv6NetworkPolicy *
1711GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX) 1712GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1712{ 1713{
1713 unsigned int count; 1714 unsigned int count;
1714 unsigned int i; 1715 unsigned int i;
@@ -1727,110 +1728,110 @@ GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX)
1727 1728
1728 if (NULL == routeListX) 1729 if (NULL == routeListX)
1729 return NULL; 1730 return NULL;
1730 len = strlen(routeListX); 1731 len = strlen (routeListX);
1731 if (0 == len) 1732 if (0 == len)
1732 return NULL; 1733 return NULL;
1733 routeList = GNUNET_strdup(routeListX); 1734 routeList = GNUNET_strdup (routeListX);
1734 count = 0; 1735 count = 0;
1735 for (i = 0; i < len; i++) 1736 for (i = 0; i < len; i++)
1736 if (';' == routeList[i]) 1737 if (';' == routeList[i])
1737 count++; 1738 count++;
1738 if (';' != routeList[len - 1]) 1739 if (';' != routeList[len - 1])
1739 { 1740 {
1740 LOG(GNUNET_ERROR_TYPE_WARNING, 1741 LOG (GNUNET_ERROR_TYPE_WARNING,
1741 _("Invalid network notation (does not end with ';': `%s')\n"), 1742 _ ("Invalid network notation (does not end with ';': `%s')\n"),
1742 routeList); 1743 routeList);
1743 GNUNET_free(routeList); 1744 GNUNET_free (routeList);
1744 return NULL; 1745 return NULL;
1745 } 1746 }
1746 1747
1747 result = GNUNET_malloc(sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy) * 1748 result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy)
1748 (count + 1)); 1749 * (count + 1));
1749 i = 0; 1750 i = 0;
1750 pos = 0; 1751 pos = 0;
1751 while (i < count) 1752 while (i < count)
1753 {
1754 start = pos;
1755 while (';' != routeList[pos])
1756 pos++;
1757 slash = pos;
1758 while ((slash >= start) && (routeList[slash] != '/'))
1759 slash--;
1760
1761 if (slash < start)
1752 { 1762 {
1753 start = pos; 1763 memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr));
1754 while (';' != routeList[pos])
1755 pos++;
1756 slash = pos; 1764 slash = pos;
1757 while ((slash >= start) && (routeList[slash] != '/')) 1765 }
1758 slash--; 1766 else
1759 1767 {
1760 if (slash < start) 1768 routeList[pos] = '\0';
1761 { 1769 for (colon = pos; ':' != routeList[colon]; colon--)
1762 memset(&result[i].netmask, 0xFF, sizeof(struct in6_addr)); 1770 if ('/' == routeList[colon])
1763 slash = pos; 1771 break;
1764 } 1772 if (':' == routeList[colon])
1765 else 1773 {
1774 routeList[colon] = '\0';
1775 if (GNUNET_OK !=
1776 parse_port_policy (&routeList[colon + 1], &result[i].pp))
1766 { 1777 {
1767 routeList[pos] = '\0'; 1778 GNUNET_free (result);
1768 for (colon = pos; ':' != routeList[colon]; colon--) 1779 GNUNET_free (routeList);
1769 if ('/' == routeList[colon]) 1780 return NULL;
1770 break;
1771 if (':' == routeList[colon])
1772 {
1773 routeList[colon] = '\0';
1774 if (GNUNET_OK !=
1775 parse_port_policy(&routeList[colon + 1], &result[i].pp))
1776 {
1777 GNUNET_free(result);
1778 GNUNET_free(routeList);
1779 return NULL;
1780 }
1781 }
1782 ret = inet_pton(AF_INET6, &routeList[slash + 1], &result[i].netmask);
1783 if (ret <= 0)
1784 {
1785 save = errno;
1786 if ((1 != sscanf(&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1787 (bits > 128))
1788 {
1789 if (0 == ret)
1790 LOG(GNUNET_ERROR_TYPE_WARNING,
1791 _("Wrong format `%s' for netmask\n"),
1792 &routeList[slash + 1]);
1793 else
1794 {
1795 errno = save;
1796 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1797 }
1798 GNUNET_free(result);
1799 GNUNET_free(routeList);
1800 return NULL;
1801 }
1802 off = 0;
1803 while (bits > 8)
1804 {
1805 result[i].netmask.s6_addr[off++] = 0xFF;
1806 bits -= 8;
1807 }
1808 while (bits > 0)
1809 {
1810 result[i].netmask.s6_addr[off] =
1811 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1812 bits--;
1813 }
1814 }
1815 } 1781 }
1816 routeList[slash] = '\0'; 1782 }
1817 ret = inet_pton(AF_INET6, &routeList[start], &result[i].network); 1783 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
1818 if (ret <= 0) 1784 if (ret <= 0)
1785 {
1786 save = errno;
1787 if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1788 (bits > 128))
1819 { 1789 {
1820 if (0 == ret) 1790 if (0 == ret)
1821 LOG(GNUNET_ERROR_TYPE_WARNING, 1791 LOG (GNUNET_ERROR_TYPE_WARNING,
1822 _("Wrong format `%s' for network\n"), 1792 _ ("Wrong format `%s' for netmask\n"),
1823 &routeList[slash + 1]); 1793 &routeList[slash + 1]);
1824 else 1794 else
1825 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "inet_pton"); 1795 {
1826 GNUNET_free(result); 1796 errno = save;
1827 GNUNET_free(routeList); 1797 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1798 }
1799 GNUNET_free (result);
1800 GNUNET_free (routeList);
1828 return NULL; 1801 return NULL;
1829 } 1802 }
1830 pos++; 1803 off = 0;
1831 i++; 1804 while (bits > 8)
1805 {
1806 result[i].netmask.s6_addr[off++] = 0xFF;
1807 bits -= 8;
1808 }
1809 while (bits > 0)
1810 {
1811 result[i].netmask.s6_addr[off] =
1812 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1813 bits--;
1814 }
1815 }
1832 } 1816 }
1833 GNUNET_free(routeList); 1817 routeList[slash] = '\0';
1818 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
1819 if (ret <= 0)
1820 {
1821 if (0 == ret)
1822 LOG (GNUNET_ERROR_TYPE_WARNING,
1823 _ ("Wrong format `%s' for network\n"),
1824 &routeList[slash + 1]);
1825 else
1826 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
1827 GNUNET_free (result);
1828 GNUNET_free (routeList);
1829 return NULL;
1830 }
1831 pos++;
1832 i++;
1833 }
1834 GNUNET_free (routeList);
1834 return result; 1835 return result;
1835} 1836}
1836 1837
@@ -1853,46 +1854,46 @@ static char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1853 * @return the size of the output 1854 * @return the size of the output
1854 */ 1855 */
1855size_t 1856size_t
1856GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output) 1857GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
1857{ 1858{
1858 const char *data = in; 1859 const char *data = in;
1859 size_t ret; 1860 size_t ret;
1860 char *opt; 1861 char *opt;
1861 1862
1862 ret = 0; 1863 ret = 0;
1863 opt = GNUNET_malloc(2 + (len * 4 / 3) + 8); 1864 opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1864 for (size_t i = 0; i < len; ++i) 1865 for (size_t i = 0; i < len; ++i)
1866 {
1867 char c;
1868
1869 c = (data[i] >> 2) & 0x3f;
1870 opt[ret++] = cvt[(int) c];
1871 c = (data[i] << 4) & 0x3f;
1872 if (++i < len)
1873 c |= (data[i] >> 4) & 0x0f;
1874 opt[ret++] = cvt[(int) c];
1875 if (i < len)
1865 { 1876 {
1866 char c; 1877 c = (data[i] << 2) & 0x3f;
1867
1868 c = (data[i] >> 2) & 0x3f;
1869 opt[ret++] = cvt[(int)c];
1870 c = (data[i] << 4) & 0x3f;
1871 if (++i < len) 1878 if (++i < len)
1872 c |= (data[i] >> 4) & 0x0f; 1879 c |= (data[i] >> 6) & 0x03;
1873 opt[ret++] = cvt[(int)c]; 1880 opt[ret++] = cvt[(int) c];
1874 if (i < len)
1875 {
1876 c = (data[i] << 2) & 0x3f;
1877 if (++i < len)
1878 c |= (data[i] >> 6) & 0x03;
1879 opt[ret++] = cvt[(int)c];
1880 }
1881 else
1882 {
1883 ++i;
1884 opt[ret++] = FILLCHAR;
1885 }
1886 if (i < len)
1887 {
1888 c = data[i] & 0x3f;
1889 opt[ret++] = cvt[(int)c];
1890 }
1891 else
1892 {
1893 opt[ret++] = FILLCHAR;
1894 }
1895 } 1881 }
1882 else
1883 {
1884 ++i;
1885 opt[ret++] = FILLCHAR;
1886 }
1887 if (i < len)
1888 {
1889 c = data[i] & 0x3f;
1890 opt[ret++] = cvt[(int) c];
1891 }
1892 else
1893 {
1894 opt[ret++] = FILLCHAR;
1895 }
1896 }
1896 *output = opt; 1897 *output = opt;
1897 return ret; 1898 return ret;
1898} 1899}
@@ -1917,61 +1918,61 @@ GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
1917 * @return the size of the output 1918 * @return the size of the output
1918 */ 1919 */
1919size_t 1920size_t
1920GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out) 1921GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **out)
1921{ 1922{
1922 char *output; 1923 char *output;
1923 size_t ret = 0; 1924 size_t ret = 0;
1924 1925
1925#define CHECK_CRLF \ 1926#define CHECK_CRLF \
1926 while (data[i] == '\r' || data[i] == '\n') \ 1927 while (data[i] == '\r' || data[i] == '\n') \
1927 { \ 1928 { \
1928 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \ 1929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
1929 "ignoring CR/LF\n"); \ 1930 "ignoring CR/LF\n"); \
1930 i++; \ 1931 i++; \
1931 if (i >= len) \ 1932 if (i >= len) \
1932 goto END; \ 1933 goto END; \
1933 } 1934 }
1934 1935
1935 output = GNUNET_malloc((len * 3 / 4) + 8); 1936 output = GNUNET_malloc ((len * 3 / 4) + 8);
1936 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "base64_decode decoding len=%d\n", 1938 "base64_decode decoding len=%d\n",
1938 (int)len); 1939 (int) len);
1939 for (size_t i = 0; i < len; ++i) 1940 for (size_t i = 0; i < len; ++i)
1940 { 1941 {
1941 char c; 1942 char c;
1942 char c1; 1943 char c1;
1943 1944
1945 CHECK_CRLF;
1946 if (FILLCHAR == data[i])
1947 break;
1948 c = (char) cvtfind (data[i]);
1949 ++i;
1950 CHECK_CRLF;
1951 c1 = (char) cvtfind (data[i]);
1952 c = (c << 2) | ((c1 >> 4) & 0x3);
1953 output[ret++] = c;
1954 if (++i < len)
1955 {
1944 CHECK_CRLF; 1956 CHECK_CRLF;
1945 if (FILLCHAR == data[i]) 1957 c = data[i];
1958 if (FILLCHAR == c)
1946 break; 1959 break;
1947 c = (char)cvtfind(data[i]); 1960 c = (char) cvtfind (c);
1948 ++i; 1961 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1962 output[ret++] = c1;
1963 }
1964 if (++i < len)
1965 {
1949 CHECK_CRLF; 1966 CHECK_CRLF;
1950 c1 = (char)cvtfind(data[i]); 1967 c1 = data[i];
1951 c = (c << 2) | ((c1 >> 4) & 0x3); 1968 if (FILLCHAR == c1)
1969 break;
1970
1971 c1 = (char) cvtfind (c1);
1972 c = ((c << 6) & 0xc0) | c1;
1952 output[ret++] = c; 1973 output[ret++] = c;
1953 if (++i < len)
1954 {
1955 CHECK_CRLF;
1956 c = data[i];
1957 if (FILLCHAR == c)
1958 break;
1959 c = (char)cvtfind(c);
1960 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1961 output[ret++] = c1;
1962 }
1963 if (++i < len)
1964 {
1965 CHECK_CRLF;
1966 c1 = data[i];
1967 if (FILLCHAR == c1)
1968 break;
1969
1970 c1 = (char)cvtfind(c1);
1971 c = ((c << 6) & 0xc0) | c1;
1972 output[ret++] = c;
1973 }
1974 } 1974 }
1975 }
1975END: 1976END:
1976 *out = output; 1977 *out = output;
1977 return ret; 1978 return ret;
diff --git a/src/util/test_bio.c b/src/util/test_bio.c
index c8887e246..180cada76 100644
--- a/src/util/test_bio.c
+++ b/src/util/test_bio.c
@@ -28,215 +28,215 @@
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30#define TESTSTRING "testString" 30#define TESTSTRING "testString"
31#define TESTNUMBER64 ((int64_t)100000L) 31#define TESTNUMBER64 ((int64_t) 100000L)
32 32
33static int 33static int
34test_normal_rw() 34test_normal_rw ()
35{ 35{
36 char *msg; 36 char *msg;
37 int64_t testNum; 37 int64_t testNum;
38 char *readResultString; 38 char *readResultString;
39 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 39 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
40 struct GNUNET_BIO_WriteHandle *fileW; 40 struct GNUNET_BIO_WriteHandle *fileW;
41 struct GNUNET_BIO_ReadHandle *fileR; 41 struct GNUNET_BIO_ReadHandle *fileR;
42 struct GNUNET_CONTAINER_MetaData *metaDataW; 42 struct GNUNET_CONTAINER_MetaData *metaDataW;
43 struct GNUNET_CONTAINER_MetaData *metaDataR; 43 struct GNUNET_CONTAINER_MetaData *metaDataR;
44 44
45 metaDataW = GNUNET_CONTAINER_meta_data_create(); 45 metaDataW = GNUNET_CONTAINER_meta_data_create ();
46 metaDataR = NULL; 46 metaDataR = NULL;
47 GNUNET_CONTAINER_meta_data_add_publication_date(metaDataW); 47 GNUNET_CONTAINER_meta_data_add_publication_date (metaDataW);
48 48
49 fileW = GNUNET_BIO_write_open(fileName); 49 fileW = GNUNET_BIO_write_open (fileName);
50 GNUNET_assert(NULL != fileW); 50 GNUNET_assert (NULL != fileW);
51 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_string(fileW, TESTSTRING)); 51 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, TESTSTRING));
52 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_meta_data(fileW, metaDataW)); 52 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_meta_data (fileW, metaDataW));
53 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_int64(fileW, TESTNUMBER64)); 53 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int64 (fileW, TESTNUMBER64));
54 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 54 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
55 55
56 fileR = GNUNET_BIO_read_open(fileName); 56 fileR = GNUNET_BIO_read_open (fileName);
57 GNUNET_assert(NULL != fileR); 57 GNUNET_assert (NULL != fileR);
58 readResultString = NULL; 58 readResultString = NULL;
59 GNUNET_assert(GNUNET_OK == 59 GNUNET_assert (GNUNET_OK ==
60 GNUNET_BIO_read_string(fileR, "Read string error", 60 GNUNET_BIO_read_string (fileR, "Read string error",
61 &readResultString, 200)); 61 &readResultString, 200));
62 GNUNET_assert(NULL != readResultString); 62 GNUNET_assert (NULL != readResultString);
63 GNUNET_assert(0 == strcmp(TESTSTRING, readResultString)); 63 GNUNET_assert (0 == strcmp (TESTSTRING, readResultString));
64 GNUNET_free(readResultString); 64 GNUNET_free (readResultString);
65 GNUNET_assert(GNUNET_OK == 65 GNUNET_assert (GNUNET_OK ==
66 GNUNET_BIO_read_meta_data(fileR, "Read meta error", 66 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
67 &metaDataR)); 67 &metaDataR));
68 GNUNET_assert(GNUNET_YES == 68 GNUNET_assert (GNUNET_YES ==
69 GNUNET_CONTAINER_meta_data_test_equal(metaDataR, metaDataW)); 69 GNUNET_CONTAINER_meta_data_test_equal (metaDataR, metaDataW));
70 GNUNET_assert(GNUNET_OK == GNUNET_BIO_read_int64(fileR, &testNum)); 70 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_int64 (fileR, &testNum));
71 GNUNET_BIO_read_close(fileR, &msg); 71 GNUNET_BIO_read_close (fileR, &msg);
72 GNUNET_CONTAINER_meta_data_destroy(metaDataW); 72 GNUNET_CONTAINER_meta_data_destroy (metaDataW);
73 GNUNET_CONTAINER_meta_data_destroy(metaDataR); 73 GNUNET_CONTAINER_meta_data_destroy (metaDataR);
74 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 74 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
75 GNUNET_free(fileName); 75 GNUNET_free (fileName);
76 return 0; 76 return 0;
77} 77}
78 78
79static int 79static int
80test_nullstring_rw() 80test_nullstring_rw ()
81{ 81{
82 char *msg; 82 char *msg;
83 char *readResultString = (char *)"not null"; 83 char *readResultString = (char *) "not null";
84 struct GNUNET_BIO_WriteHandle *fileW; 84 struct GNUNET_BIO_WriteHandle *fileW;
85 struct GNUNET_BIO_ReadHandle *fileR; 85 struct GNUNET_BIO_ReadHandle *fileR;
86 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 86 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
87 87
88 fileW = GNUNET_BIO_write_open(fileName); 88 fileW = GNUNET_BIO_write_open (fileName);
89 GNUNET_assert(NULL != fileW); 89 GNUNET_assert (NULL != fileW);
90 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_string(fileW, NULL)); 90 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, NULL));
91 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 91 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
92 92
93 fileR = GNUNET_BIO_read_open(fileName); 93 fileR = GNUNET_BIO_read_open (fileName);
94 GNUNET_assert(NULL != fileR); 94 GNUNET_assert (NULL != fileR);
95 GNUNET_assert(GNUNET_OK == 95 GNUNET_assert (GNUNET_OK ==
96 GNUNET_BIO_read_string(fileR, "Read string error", 96 GNUNET_BIO_read_string (fileR, "Read string error",
97 &readResultString, 200)); 97 &readResultString, 200));
98 GNUNET_assert(NULL == readResultString); 98 GNUNET_assert (NULL == readResultString);
99 GNUNET_BIO_read_close(fileR, &msg); 99 GNUNET_BIO_read_close (fileR, &msg);
100 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 100 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
101 GNUNET_free(fileName); 101 GNUNET_free (fileName);
102 102
103 return 0; 103 return 0;
104} 104}
105 105
106static int 106static int
107test_emptystring_rw() 107test_emptystring_rw ()
108{ 108{
109 char *msg; 109 char *msg;
110 char *readResultString; 110 char *readResultString;
111 struct GNUNET_BIO_WriteHandle *fileW; 111 struct GNUNET_BIO_WriteHandle *fileW;
112 struct GNUNET_BIO_ReadHandle *fileR; 112 struct GNUNET_BIO_ReadHandle *fileR;
113 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 113 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
114 114
115 fileW = GNUNET_BIO_write_open(fileName); 115 fileW = GNUNET_BIO_write_open (fileName);
116 GNUNET_assert(NULL != fileW); 116 GNUNET_assert (NULL != fileW);
117 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_string(fileW, "")); 117 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, ""));
118 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 118 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
119 119
120 fileR = GNUNET_BIO_read_open(fileName); 120 fileR = GNUNET_BIO_read_open (fileName);
121 GNUNET_assert(NULL != fileR); 121 GNUNET_assert (NULL != fileR);
122 readResultString = NULL; 122 readResultString = NULL;
123 GNUNET_assert(GNUNET_OK == 123 GNUNET_assert (GNUNET_OK ==
124 GNUNET_BIO_read_string(fileR, "Read string error", 124 GNUNET_BIO_read_string (fileR, "Read string error",
125 &readResultString, 200)); 125 &readResultString, 200));
126 GNUNET_free(readResultString); 126 GNUNET_free (readResultString);
127 GNUNET_BIO_read_close(fileR, &msg); 127 GNUNET_BIO_read_close (fileR, &msg);
128 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 128 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
129 GNUNET_free(fileName); 129 GNUNET_free (fileName);
130 return 0; 130 return 0;
131} 131}
132 132
133static int 133static int
134test_bigstring_rw() 134test_bigstring_rw ()
135{ 135{
136 char *msg; 136 char *msg;
137 char *readResultString; 137 char *readResultString;
138 struct GNUNET_BIO_WriteHandle *fileW; 138 struct GNUNET_BIO_WriteHandle *fileW;
139 struct GNUNET_BIO_ReadHandle *fileR; 139 struct GNUNET_BIO_ReadHandle *fileR;
140 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 140 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
141 141
142 fileW = GNUNET_BIO_write_open(fileName); 142 fileW = GNUNET_BIO_write_open (fileName);
143 GNUNET_assert(NULL != fileW); 143 GNUNET_assert (NULL != fileW);
144 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_string(fileW, TESTSTRING)); 144 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, TESTSTRING));
145 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 145 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
146 146
147 fileR = GNUNET_BIO_read_open(fileName); 147 fileR = GNUNET_BIO_read_open (fileName);
148 GNUNET_assert(NULL != fileR); 148 GNUNET_assert (NULL != fileR);
149 readResultString = NULL; 149 readResultString = NULL;
150 GNUNET_assert(GNUNET_SYSERR == 150 GNUNET_assert (GNUNET_SYSERR ==
151 GNUNET_BIO_read_string(fileR, "Read string error", 151 GNUNET_BIO_read_string (fileR, "Read string error",
152 &readResultString, 1)); 152 &readResultString, 1));
153 GNUNET_assert(NULL == readResultString); 153 GNUNET_assert (NULL == readResultString);
154 msg = NULL; 154 msg = NULL;
155 GNUNET_BIO_read_close(fileR, &msg); 155 GNUNET_BIO_read_close (fileR, &msg);
156 GNUNET_free(msg); 156 GNUNET_free (msg);
157 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 157 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
158 GNUNET_free(fileName); 158 GNUNET_free (fileName);
159 return 0; 159 return 0;
160} 160}
161 161
162static int 162static int
163test_bigmeta_rw() 163test_bigmeta_rw ()
164{ 164{
165 char *msg; 165 char *msg;
166 static char meta[1024 * 1024 * 10]; 166 static char meta[1024 * 1024 * 10];
167 struct GNUNET_BIO_WriteHandle *fileW; 167 struct GNUNET_BIO_WriteHandle *fileW;
168 struct GNUNET_BIO_ReadHandle *fileR; 168 struct GNUNET_BIO_ReadHandle *fileR;
169 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 169 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
170 struct GNUNET_CONTAINER_MetaData *metaDataR; 170 struct GNUNET_CONTAINER_MetaData *metaDataR;
171 171
172 memset(meta, 'b', sizeof(meta)); 172 memset (meta, 'b', sizeof(meta));
173 meta[sizeof(meta) - 1] = '\0'; 173 meta[sizeof(meta) - 1] = '\0';
174 fileW = GNUNET_BIO_write_open(fileName); 174 fileW = GNUNET_BIO_write_open (fileName);
175 GNUNET_assert(NULL != fileW); 175 GNUNET_assert (NULL != fileW);
176 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_int32(fileW, sizeof(meta))); 176 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, sizeof(meta)));
177 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write(fileW, meta, sizeof(meta))); 177 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write (fileW, meta, sizeof(meta)));
178 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 178 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
179 179
180 fileR = GNUNET_BIO_read_open(fileName); 180 fileR = GNUNET_BIO_read_open (fileName);
181 GNUNET_assert(NULL != fileR); 181 GNUNET_assert (NULL != fileR);
182 metaDataR = NULL; 182 metaDataR = NULL;
183 GNUNET_assert(GNUNET_SYSERR == 183 GNUNET_assert (GNUNET_SYSERR ==
184 GNUNET_BIO_read_meta_data(fileR, "Read meta error", 184 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
185 &metaDataR)); 185 &metaDataR));
186 msg = NULL; 186 msg = NULL;
187 GNUNET_BIO_read_close(fileR, &msg); 187 GNUNET_BIO_read_close (fileR, &msg);
188 GNUNET_free(msg); 188 GNUNET_free (msg);
189 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 189 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
190 GNUNET_assert(NULL == metaDataR); 190 GNUNET_assert (NULL == metaDataR);
191 GNUNET_free(fileName); 191 GNUNET_free (fileName);
192 return 0; 192 return 0;
193} 193}
194 194
195static int 195static int
196test_directory_r() 196test_directory_r ()
197{ 197{
198#if LINUX 198#if LINUX
199 char *msg; 199 char *msg;
200 char readResult[200]; 200 char readResult[200];
201 struct GNUNET_BIO_ReadHandle *fileR; 201 struct GNUNET_BIO_ReadHandle *fileR;
202 202
203 fileR = GNUNET_BIO_read_open("/dev"); 203 fileR = GNUNET_BIO_read_open ("/dev");
204 GNUNET_assert(NULL != fileR); 204 GNUNET_assert (NULL != fileR);
205 GNUNET_assert(GNUNET_SYSERR == 205 GNUNET_assert (GNUNET_SYSERR ==
206 GNUNET_BIO_read(fileR, "Read error", readResult, 206 GNUNET_BIO_read (fileR, "Read error", readResult,
207 sizeof(readResult))); 207 sizeof(readResult)));
208 msg = NULL; 208 msg = NULL;
209 GNUNET_BIO_read_close(fileR, &msg); 209 GNUNET_BIO_read_close (fileR, &msg);
210 GNUNET_free(msg); 210 GNUNET_free (msg);
211#endif 211#endif
212 return 0; 212 return 0;
213} 213}
214 214
215static int 215static int
216test_nullfile_rw() 216test_nullfile_rw ()
217{ 217{
218 static char fileNameNO[102401]; 218 static char fileNameNO[102401];
219 struct GNUNET_BIO_WriteHandle *fileWNO; 219 struct GNUNET_BIO_WriteHandle *fileWNO;
220 struct GNUNET_BIO_ReadHandle *fileRNO; 220 struct GNUNET_BIO_ReadHandle *fileRNO;
221 221
222 memset(fileNameNO, 'a', sizeof(fileNameNO)); 222 memset (fileNameNO, 'a', sizeof(fileNameNO));
223 fileNameNO[sizeof(fileNameNO) - 1] = '\0'; 223 fileNameNO[sizeof(fileNameNO) - 1] = '\0';
224 224
225 GNUNET_log_skip(1, GNUNET_NO); 225 GNUNET_log_skip (1, GNUNET_NO);
226 fileWNO = GNUNET_BIO_write_open(fileNameNO); 226 fileWNO = GNUNET_BIO_write_open (fileNameNO);
227 GNUNET_log_skip(0, GNUNET_YES); 227 GNUNET_log_skip (0, GNUNET_YES);
228 GNUNET_assert(NULL == fileWNO); 228 GNUNET_assert (NULL == fileWNO);
229 229
230 GNUNET_log_skip(1, GNUNET_NO); 230 GNUNET_log_skip (1, GNUNET_NO);
231 fileRNO = GNUNET_BIO_read_open(fileNameNO); 231 fileRNO = GNUNET_BIO_read_open (fileNameNO);
232 GNUNET_log_skip(0, GNUNET_YES); 232 GNUNET_log_skip (0, GNUNET_YES);
233 GNUNET_assert(NULL == fileRNO); 233 GNUNET_assert (NULL == fileRNO);
234 return 0; 234 return 0;
235} 235}
236 236
237 237
238static int 238static int
239test_fullfile_rw() 239test_fullfile_rw ()
240{ 240{
241#ifdef LINUX 241#ifdef LINUX
242 /* /dev/full only seems to exist on Linux */ 242 /* /dev/full only seems to exist on Linux */
@@ -249,168 +249,168 @@ test_fullfile_rw()
249 struct GNUNET_CONTAINER_MetaData *metaDataW; 249 struct GNUNET_CONTAINER_MetaData *metaDataW;
250 struct GNUNET_CONTAINER_MetaData *metaDataR; 250 struct GNUNET_CONTAINER_MetaData *metaDataR;
251 251
252 metaDataW = GNUNET_CONTAINER_meta_data_create(); 252 metaDataW = GNUNET_CONTAINER_meta_data_create ();
253 GNUNET_CONTAINER_meta_data_add_publication_date(metaDataW); 253 GNUNET_CONTAINER_meta_data_add_publication_date (metaDataW);
254 254
255 fileW = GNUNET_BIO_write_open("/dev/full"); 255 fileW = GNUNET_BIO_write_open ("/dev/full");
256 GNUNET_assert(NULL != fileW); 256 GNUNET_assert (NULL != fileW);
257 (void)GNUNET_BIO_write(fileW, TESTSTRING, strlen(TESTSTRING)); 257 (void) GNUNET_BIO_write (fileW, TESTSTRING, strlen (TESTSTRING));
258 (void)GNUNET_BIO_write_string(fileW, TESTSTRING); 258 (void) GNUNET_BIO_write_string (fileW, TESTSTRING);
259 (void)GNUNET_BIO_write_meta_data(fileW, metaDataW); 259 (void) GNUNET_BIO_write_meta_data (fileW, metaDataW);
260 GNUNET_assert(GNUNET_SYSERR == GNUNET_BIO_write_close(fileW)); 260 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_close (fileW));
261 GNUNET_CONTAINER_meta_data_destroy(metaDataW); 261 GNUNET_CONTAINER_meta_data_destroy (metaDataW);
262 262
263 fileW = GNUNET_BIO_write_open("/dev/full"); 263 fileW = GNUNET_BIO_write_open ("/dev/full");
264 GNUNET_assert(NULL != fileW); 264 GNUNET_assert (NULL != fileW);
265 GNUNET_assert(GNUNET_SYSERR == GNUNET_BIO_write_close(fileW)); 265 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_close (fileW));
266 266
267 fileR = GNUNET_BIO_read_open("/dev/null"); 267 fileR = GNUNET_BIO_read_open ("/dev/null");
268 GNUNET_assert(NULL != fileR); 268 GNUNET_assert (NULL != fileR);
269 GNUNET_assert(GNUNET_SYSERR == 269 GNUNET_assert (GNUNET_SYSERR ==
270 GNUNET_BIO_read(fileR, "Read error", readResult, 270 GNUNET_BIO_read (fileR, "Read error", readResult,
271 sizeof(readResult))); 271 sizeof(readResult)));
272 readResultString = NULL; 272 readResultString = NULL;
273 GNUNET_assert(GNUNET_SYSERR == 273 GNUNET_assert (GNUNET_SYSERR ==
274 GNUNET_BIO_read_string(fileR, "Read string error", 274 GNUNET_BIO_read_string (fileR, "Read string error",
275 &readResultString, 200)); 275 &readResultString, 200));
276 GNUNET_assert(NULL == readResultString); 276 GNUNET_assert (NULL == readResultString);
277 GNUNET_assert(GNUNET_SYSERR == GNUNET_BIO_read_int64(fileR, &testNum)); 277 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_int64 (fileR, &testNum));
278 metaDataR = NULL; 278 metaDataR = NULL;
279 GNUNET_assert(GNUNET_SYSERR == 279 GNUNET_assert (GNUNET_SYSERR ==
280 GNUNET_BIO_read_meta_data(fileR, "Read meta error", 280 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
281 &metaDataR)); 281 &metaDataR));
282 msg = NULL; 282 msg = NULL;
283 GNUNET_BIO_read_close(fileR, &msg); 283 GNUNET_BIO_read_close (fileR, &msg);
284 GNUNET_free(msg); 284 GNUNET_free (msg);
285 GNUNET_assert(NULL == metaDataR); 285 GNUNET_assert (NULL == metaDataR);
286#endif 286#endif
287 return 0; 287 return 0;
288} 288}
289 289
290static int 290static int
291test_fakestring_rw() 291test_fakestring_rw ()
292{ 292{
293 char *msg; 293 char *msg;
294 int32_t tmpInt = 2; 294 int32_t tmpInt = 2;
295 char *readResult; 295 char *readResult;
296 struct GNUNET_BIO_WriteHandle *fileW; 296 struct GNUNET_BIO_WriteHandle *fileW;
297 struct GNUNET_BIO_ReadHandle *fileR; 297 struct GNUNET_BIO_ReadHandle *fileR;
298 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 298 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
299 299
300 fileW = GNUNET_BIO_write_open(fileName); 300 fileW = GNUNET_BIO_write_open (fileName);
301 GNUNET_assert(NULL != fileW); 301 GNUNET_assert (NULL != fileW);
302 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_int32(fileW, tmpInt)); 302 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, tmpInt));
303 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 303 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
304 304
305 fileR = GNUNET_BIO_read_open(fileName); 305 fileR = GNUNET_BIO_read_open (fileName);
306 GNUNET_assert(NULL != fileR); 306 GNUNET_assert (NULL != fileR);
307 GNUNET_assert(GNUNET_SYSERR == 307 GNUNET_assert (GNUNET_SYSERR ==
308 GNUNET_BIO_read_string(fileR, "Read string error", 308 GNUNET_BIO_read_string (fileR, "Read string error",
309 &readResult, 200)); 309 &readResult, 200));
310 msg = NULL; 310 msg = NULL;
311 GNUNET_BIO_read_close(fileR, &msg); 311 GNUNET_BIO_read_close (fileR, &msg);
312 GNUNET_free(msg); 312 GNUNET_free (msg);
313 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 313 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
314 GNUNET_free(fileName); 314 GNUNET_free (fileName);
315 return 0; 315 return 0;
316} 316}
317 317
318static int 318static int
319test_fakemeta_rw() 319test_fakemeta_rw ()
320{ 320{
321 char *msg; 321 char *msg;
322 int32_t tmpInt = 2; 322 int32_t tmpInt = 2;
323 struct GNUNET_BIO_WriteHandle *fileW; 323 struct GNUNET_BIO_WriteHandle *fileW;
324 struct GNUNET_BIO_ReadHandle *fileR; 324 struct GNUNET_BIO_ReadHandle *fileR;
325 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 325 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
326 struct GNUNET_CONTAINER_MetaData *metaDataR; 326 struct GNUNET_CONTAINER_MetaData *metaDataR;
327 327
328 fileW = GNUNET_BIO_write_open(fileName); 328 fileW = GNUNET_BIO_write_open (fileName);
329 GNUNET_assert(NULL != fileW); 329 GNUNET_assert (NULL != fileW);
330 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_int32(fileW, tmpInt)); 330 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, tmpInt));
331 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 331 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
332 332
333 fileR = GNUNET_BIO_read_open(fileName); 333 fileR = GNUNET_BIO_read_open (fileName);
334 GNUNET_assert(NULL != fileR); 334 GNUNET_assert (NULL != fileR);
335 metaDataR = NULL; 335 metaDataR = NULL;
336 GNUNET_assert(GNUNET_SYSERR == 336 GNUNET_assert (GNUNET_SYSERR ==
337 GNUNET_BIO_read_meta_data(fileR, "Read meta error", 337 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
338 &metaDataR)); 338 &metaDataR));
339 GNUNET_assert(NULL == metaDataR); 339 GNUNET_assert (NULL == metaDataR);
340 msg = NULL; 340 msg = NULL;
341 GNUNET_BIO_read_close(fileR, &msg); 341 GNUNET_BIO_read_close (fileR, &msg);
342 GNUNET_free(msg); 342 GNUNET_free (msg);
343 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 343 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
344 GNUNET_free(fileName); 344 GNUNET_free (fileName);
345 return 0; 345 return 0;
346} 346}
347 347
348static int 348static int
349test_fakebigmeta_rw() 349test_fakebigmeta_rw ()
350{ 350{
351 char *msg; 351 char *msg;
352 int32_t tmpInt = 1024 * 1024 * 10; 352 int32_t tmpInt = 1024 * 1024 * 10;
353 struct GNUNET_BIO_WriteHandle *fileW; 353 struct GNUNET_BIO_WriteHandle *fileW;
354 struct GNUNET_BIO_ReadHandle *fileR; 354 struct GNUNET_BIO_ReadHandle *fileR;
355 char *fileName = GNUNET_DISK_mktemp("gnunet_bio"); 355 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
356 struct GNUNET_CONTAINER_MetaData *metaDataR; 356 struct GNUNET_CONTAINER_MetaData *metaDataR;
357 357
358 fileW = GNUNET_BIO_write_open(fileName); 358 fileW = GNUNET_BIO_write_open (fileName);
359 GNUNET_assert(NULL != fileW); 359 GNUNET_assert (NULL != fileW);
360 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_int32(fileW, tmpInt)); 360 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, tmpInt));
361 GNUNET_assert(GNUNET_OK == GNUNET_BIO_write_close(fileW)); 361 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
362 362
363 fileR = GNUNET_BIO_read_open(fileName); 363 fileR = GNUNET_BIO_read_open (fileName);
364 GNUNET_assert(NULL != fileR); 364 GNUNET_assert (NULL != fileR);
365 metaDataR = NULL; 365 metaDataR = NULL;
366 GNUNET_assert(GNUNET_SYSERR == 366 GNUNET_assert (GNUNET_SYSERR ==
367 GNUNET_BIO_read_meta_data(fileR, "Read meta error", 367 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
368 &metaDataR)); 368 &metaDataR));
369 msg = NULL; 369 msg = NULL;
370 GNUNET_BIO_read_close(fileR, &msg); 370 GNUNET_BIO_read_close (fileR, &msg);
371 GNUNET_free(msg); 371 GNUNET_free (msg);
372 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_remove(fileName)); 372 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
373 GNUNET_assert(NULL == metaDataR); 373 GNUNET_assert (NULL == metaDataR);
374 GNUNET_free(fileName); 374 GNUNET_free (fileName);
375 return 0; 375 return 0;
376} 376}
377 377
378static int 378static int
379check_string_rw() 379check_string_rw ()
380{ 380{
381 GNUNET_assert(0 == test_nullstring_rw()); 381 GNUNET_assert (0 == test_nullstring_rw ());
382 GNUNET_assert(0 == test_emptystring_rw()); 382 GNUNET_assert (0 == test_emptystring_rw ());
383 GNUNET_assert(0 == test_bigstring_rw()); 383 GNUNET_assert (0 == test_bigstring_rw ());
384 GNUNET_assert(0 == test_fakestring_rw()); 384 GNUNET_assert (0 == test_fakestring_rw ());
385 return 0; 385 return 0;
386} 386}
387 387
388static int 388static int
389check_metadata_rw() 389check_metadata_rw ()
390{ 390{
391 GNUNET_assert(0 == test_fakebigmeta_rw()); 391 GNUNET_assert (0 == test_fakebigmeta_rw ());
392 GNUNET_assert(0 == test_fakemeta_rw()); 392 GNUNET_assert (0 == test_fakemeta_rw ());
393 GNUNET_assert(0 == test_bigmeta_rw()); 393 GNUNET_assert (0 == test_bigmeta_rw ());
394 return 0; 394 return 0;
395} 395}
396 396
397static int 397static int
398check_file_rw() 398check_file_rw ()
399{ 399{
400 GNUNET_assert(0 == test_normal_rw()); 400 GNUNET_assert (0 == test_normal_rw ());
401 GNUNET_assert(0 == test_nullfile_rw()); 401 GNUNET_assert (0 == test_nullfile_rw ());
402 GNUNET_assert(0 == test_fullfile_rw()); 402 GNUNET_assert (0 == test_fullfile_rw ());
403 GNUNET_assert(0 == test_directory_r()); 403 GNUNET_assert (0 == test_directory_r ());
404 return 0; 404 return 0;
405} 405}
406 406
407int 407int
408main(int argc, char *argv[]) 408main (int argc, char *argv[])
409{ 409{
410 GNUNET_log_setup("test-bio", "WARNING", NULL); 410 GNUNET_log_setup ("test-bio", "WARNING", NULL);
411 GNUNET_assert(0 == check_file_rw()); 411 GNUNET_assert (0 == check_file_rw ());
412 GNUNET_assert(0 == check_metadata_rw()); 412 GNUNET_assert (0 == check_metadata_rw ());
413 GNUNET_assert(0 == check_string_rw()); 413 GNUNET_assert (0 == check_string_rw ());
414 return 0; 414 return 0;
415} 415}
416 416
diff --git a/src/util/test_client.c b/src/util/test_client.c
index b49fbcd25..fafb967b4 100644
--- a/src/util/test_client.c
+++ b/src/util/test_client.c
@@ -36,31 +36,31 @@ static struct GNUNET_MQ_Handle *client_mq;
36 * Callback that just bounces the message back to the sender. 36 * Callback that just bounces the message back to the sender.
37 */ 37 */
38static void 38static void
39handle_echo(void *cls, 39handle_echo (void *cls,
40 const struct GNUNET_MessageHeader *message) 40 const struct GNUNET_MessageHeader *message)
41{ 41{
42 struct GNUNET_SERVICE_Client *c = cls; 42 struct GNUNET_SERVICE_Client *c = cls;
43 struct GNUNET_MQ_Handle *mq = GNUNET_SERVICE_client_get_mq(c); 43 struct GNUNET_MQ_Handle *mq = GNUNET_SERVICE_client_get_mq (c);
44 struct GNUNET_MQ_Envelope *env; 44 struct GNUNET_MQ_Envelope *env;
45 45
46 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 46 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
47 "Receiving message from client, bouncing back\n"); 47 "Receiving message from client, bouncing back\n");
48 env = GNUNET_MQ_msg_copy(message); 48 env = GNUNET_MQ_msg_copy (message);
49 GNUNET_MQ_send(mq, 49 GNUNET_MQ_send (mq,
50 env); 50 env);
51 GNUNET_SERVICE_client_continue(c); 51 GNUNET_SERVICE_client_continue (c);
52} 52}
53 53
54 54
55static void 55static void
56handle_bounce(void *cls, 56handle_bounce (void *cls,
57 const struct GNUNET_MessageHeader *got) 57 const struct GNUNET_MessageHeader *got)
58{ 58{
59 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 59 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
60 "Receiving bounce, checking content\n"); 60 "Receiving bounce, checking content\n");
61 GNUNET_assert(NULL != got); 61 GNUNET_assert (NULL != got);
62 global_ret = 2; 62 global_ret = 2;
63 GNUNET_MQ_destroy(client_mq); 63 GNUNET_MQ_destroy (client_mq);
64 client_mq = NULL; 64 client_mq = NULL;
65} 65}
66 66
@@ -74,45 +74,45 @@ handle_bounce(void *cls,
74 * @param error error code 74 * @param error error code
75 */ 75 */
76static void 76static void
77mq_error_handler(void *cls, 77mq_error_handler (void *cls,
78 enum GNUNET_MQ_Error error) 78 enum GNUNET_MQ_Error error)
79{ 79{
80 GNUNET_assert(0); /* should never happen */ 80 GNUNET_assert (0); /* should never happen */
81} 81}
82 82
83 83
84static void 84static void
85task(void *cls, 85task (void *cls,
86 const struct GNUNET_CONFIGURATION_Handle *cfg, 86 const struct GNUNET_CONFIGURATION_Handle *cfg,
87 struct GNUNET_SERVICE_Handle *sh) 87 struct GNUNET_SERVICE_Handle *sh)
88{ 88{
89 struct GNUNET_MQ_MessageHandler chandlers[] = { 89 struct GNUNET_MQ_MessageHandler chandlers[] = {
90 GNUNET_MQ_hd_fixed_size(bounce, 90 GNUNET_MQ_hd_fixed_size (bounce,
91 MY_TYPE, 91 MY_TYPE,
92 struct GNUNET_MessageHeader, 92 struct GNUNET_MessageHeader,
93 cls), 93 cls),
94 GNUNET_MQ_handler_end() 94 GNUNET_MQ_handler_end ()
95 }; 95 };
96 struct GNUNET_MQ_Envelope *env; 96 struct GNUNET_MQ_Envelope *env;
97 struct GNUNET_MessageHeader *msg; 97 struct GNUNET_MessageHeader *msg;
98 98
99 /* test that ill-configured client fails instantly */ 99 /* test that ill-configured client fails instantly */
100 GNUNET_assert(NULL == 100 GNUNET_assert (NULL ==
101 GNUNET_CLIENT_connect(cfg, 101 GNUNET_CLIENT_connect (cfg,
102 "invalid-service", 102 "invalid-service",
103 NULL, 103 NULL,
104 &mq_error_handler, 104 &mq_error_handler,
105 NULL)); 105 NULL));
106 client_mq = GNUNET_CLIENT_connect(cfg, 106 client_mq = GNUNET_CLIENT_connect (cfg,
107 "test_client", 107 "test_client",
108 chandlers, 108 chandlers,
109 &mq_error_handler, 109 &mq_error_handler,
110 NULL); 110 NULL);
111 GNUNET_assert(NULL != client_mq); 111 GNUNET_assert (NULL != client_mq);
112 env = GNUNET_MQ_msg(msg, 112 env = GNUNET_MQ_msg (msg,
113 MY_TYPE); 113 MY_TYPE);
114 GNUNET_MQ_send(client_mq, 114 GNUNET_MQ_send (client_mq,
115 env); 115 env);
116} 116}
117 117
118 118
@@ -125,9 +125,9 @@ task(void *cls,
125 * @return @a c 125 * @return @a c
126 */ 126 */
127static void * 127static void *
128connect_cb(void *cls, 128connect_cb (void *cls,
129 struct GNUNET_SERVICE_Client *c, 129 struct GNUNET_SERVICE_Client *c,
130 struct GNUNET_MQ_Handle *mq) 130 struct GNUNET_MQ_Handle *mq)
131{ 131{
132 return c; 132 return c;
133} 133}
@@ -141,53 +141,53 @@ connect_cb(void *cls,
141 * @param internal_cls must match @a c 141 * @param internal_cls must match @a c
142 */ 142 */
143static void 143static void
144disconnect_cb(void *cls, 144disconnect_cb (void *cls,
145 struct GNUNET_SERVICE_Client *c, 145 struct GNUNET_SERVICE_Client *c,
146 void *internal_cls) 146 void *internal_cls)
147{ 147{
148 if (2 == global_ret) 148 if (2 == global_ret)
149 { 149 {
150 GNUNET_SCHEDULER_shutdown(); 150 GNUNET_SCHEDULER_shutdown ();
151 global_ret = 0; 151 global_ret = 0;
152 } 152 }
153} 153}
154 154
155 155
156int 156int
157main(int argc, 157main (int argc,
158 char *argv[]) 158 char *argv[])
159{ 159{
160 struct GNUNET_MQ_MessageHandler shandlers[] = { 160 struct GNUNET_MQ_MessageHandler shandlers[] = {
161 GNUNET_MQ_hd_fixed_size(echo, 161 GNUNET_MQ_hd_fixed_size (echo,
162 MY_TYPE, 162 MY_TYPE,
163 struct GNUNET_MessageHeader, 163 struct GNUNET_MessageHeader,
164 NULL), 164 NULL),
165 GNUNET_MQ_handler_end() 165 GNUNET_MQ_handler_end ()
166 }; 166 };
167 char * test_argv[] = { 167 char *test_argv[] = {
168 (char *)"test_client", 168 (char *) "test_client",
169 "-c", 169 "-c",
170 "test_client_data.conf", 170 "test_client_data.conf",
171 NULL 171 NULL
172 }; 172 };
173 173
174 GNUNET_log_setup("test_client", 174 GNUNET_log_setup ("test_client",
175 "WARNING", 175 "WARNING",
176 NULL); 176 NULL);
177 if (0 != strstr(argv[0], 177 if (0 != strstr (argv[0],
178 "unix")) 178 "unix"))
179 test_argv[2] = "test_client_unix.conf"; 179 test_argv[2] = "test_client_unix.conf";
180 global_ret = 1; 180 global_ret = 1;
181 if (0 != 181 if (0 !=
182 GNUNET_SERVICE_run_(3, 182 GNUNET_SERVICE_run_ (3,
183 test_argv, 183 test_argv,
184 "test_client", 184 "test_client",
185 GNUNET_SERVICE_OPTION_NONE, 185 GNUNET_SERVICE_OPTION_NONE,
186 &task, 186 &task,
187 &connect_cb, 187 &connect_cb,
188 &disconnect_cb, 188 &disconnect_cb,
189 NULL, 189 NULL,
190 shandlers)) 190 shandlers))
191 global_ret = 3; 191 global_ret = 3;
192 return global_ret; 192 return global_ret;
193} 193}
diff --git a/src/util/test_common_allocation.c b/src/util/test_common_allocation.c
index e67360044..1c41a10bc 100644
--- a/src/util/test_common_allocation.c
+++ b/src/util/test_common_allocation.c
@@ -27,7 +27,7 @@
27 27
28 28
29static int 29static int
30check() 30check ()
31{ 31{
32#define MAX_TESTVAL 1024 32#define MAX_TESTVAL 1024
33 char *ptrs[MAX_TESTVAL]; 33 char *ptrs[MAX_TESTVAL];
@@ -41,112 +41,112 @@ check()
41 /* GNUNET_malloc/GNUNET_free test */ 41 /* GNUNET_malloc/GNUNET_free test */
42 k = 352; /* random start value */ 42 k = 352; /* random start value */
43 for (i = 1; i < MAX_TESTVAL; i++) 43 for (i = 1; i < MAX_TESTVAL; i++)
44 { 44 {
45 ptrs[i] = GNUNET_malloc(i); 45 ptrs[i] = GNUNET_malloc (i);
46 for (j = 0; j < i; j++) 46 for (j = 0; j < i; j++)
47 ptrs[i][j] = k++; 47 ptrs[i][j] = k++;
48 } 48 }
49 49
50 for (i = MAX_TESTVAL - 1; i >= 1; i--) 50 for (i = MAX_TESTVAL - 1; i >= 1; i--)
51 { 51 {
52 for (j = i - 1; j >= 0; j--) 52 for (j = i - 1; j >= 0; j--)
53 if (ptrs[i][j] != (char)--k) 53 if (ptrs[i][j] != (char) --k)
54 return 1; 54 return 1;
55 GNUNET_free(ptrs[i]); 55 GNUNET_free (ptrs[i]);
56 } 56 }
57 57
58 /* GNUNET_free_non_null test */ 58 /* GNUNET_free_non_null test */
59 GNUNET_free_non_null(NULL); 59 GNUNET_free_non_null (NULL);
60 GNUNET_free_non_null(GNUNET_malloc(4)); 60 GNUNET_free_non_null (GNUNET_malloc (4));
61 61
62 /* GNUNET_strdup tests */ 62 /* GNUNET_strdup tests */
63 ptrs[0] = GNUNET_strdup("bar"); 63 ptrs[0] = GNUNET_strdup ("bar");
64 if (0 != strcmp(ptrs[0], "bar")) 64 if (0 != strcmp (ptrs[0], "bar"))
65 return 3; 65 return 3;
66 /* now realloc */ 66 /* now realloc */
67 ptrs[0] = GNUNET_realloc(ptrs[0], 12); 67 ptrs[0] = GNUNET_realloc (ptrs[0], 12);
68 strcpy(ptrs[0], "Hello World"); 68 strcpy (ptrs[0], "Hello World");
69 69
70 GNUNET_free(ptrs[0]); 70 GNUNET_free (ptrs[0]);
71 GNUNET_asprintf(&ptrs[0], "%s %s", "Hello", "World"); 71 GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
72 GNUNET_assert(strlen(ptrs[0]) == 11); 72 GNUNET_assert (strlen (ptrs[0]) == 11);
73 GNUNET_free(ptrs[0]); 73 GNUNET_free (ptrs[0]);
74 74
75 /* GNUNET_array_grow tests */ 75 /* GNUNET_array_grow tests */
76 ptrs[0] = NULL; 76 ptrs[0] = NULL;
77 ui = 0; 77 ui = 0;
78 GNUNET_array_grow(ptrs[0], ui, 42); 78 GNUNET_array_grow (ptrs[0], ui, 42);
79 if (ui != 42) 79 if (ui != 42)
80 return 4; 80 return 4;
81 GNUNET_array_grow(ptrs[0], ui, 22); 81 GNUNET_array_grow (ptrs[0], ui, 22);
82 if (ui != 22) 82 if (ui != 22)
83 return 5; 83 return 5;
84 for (j = 0; j < 22; j++) 84 for (j = 0; j < 22; j++)
85 ptrs[0][j] = j; 85 ptrs[0][j] = j;
86 GNUNET_array_grow(ptrs[0], ui, 32); 86 GNUNET_array_grow (ptrs[0], ui, 32);
87 for (j = 0; j < 22; j++) 87 for (j = 0; j < 22; j++)
88 if (ptrs[0][j] != j) 88 if (ptrs[0][j] != j)
89 return 6; 89 return 6;
90 for (j = 22; j < 32; j++) 90 for (j = 22; j < 32; j++)
91 if (ptrs[0][j] != 0) 91 if (ptrs[0][j] != 0)
92 return 7; 92 return 7;
93 GNUNET_array_grow(ptrs[0], ui, 0); 93 GNUNET_array_grow (ptrs[0], ui, 0);
94 if (i != 0) 94 if (i != 0)
95 return 8; 95 return 8;
96 if (ptrs[0] != NULL) 96 if (ptrs[0] != NULL)
97 return 9; 97 return 9;
98 98
99 /* GNUNET_new_array_2d tests */ 99 /* GNUNET_new_array_2d tests */
100 a2 = GNUNET_new_array_2d(17, 22, unsigned int); 100 a2 = GNUNET_new_array_2d (17, 22, unsigned int);
101 for (i = 0; i < 17; i++) 101 for (i = 0; i < 17; i++)
102 {
103 for (j = 0; j < 22; j++)
102 { 104 {
103 for (j = 0; j < 22; j++) 105 if (0 != a2[i][j])
104 { 106 {
105 if (0 != a2[i][j]) 107 GNUNET_free (a2);
106 { 108 return 10;
107 GNUNET_free(a2); 109 }
108 return 10; 110 a2[i][j] = i * 100 + j;
109 }
110 a2[i][j] = i * 100 + j;
111 }
112 } 111 }
113 GNUNET_free(a2); 112 }
113 GNUNET_free (a2);
114 114
115 /* GNUNET_new_array_3d tests */ 115 /* GNUNET_new_array_3d tests */
116 a3 = GNUNET_new_array_3d(2, 3, 4, char); 116 a3 = GNUNET_new_array_3d (2, 3, 4, char);
117 for (i = 0; i < 2; i++) 117 for (i = 0; i < 2; i++)
118 {
119 for (j = 0; j < 3; j++)
118 { 120 {
119 for (j = 0; j < 3; j++) 121 for (k = 0; k < 4; k++)
122 {
123 if (0 != a3[i][j][k])
120 { 124 {
121 for (k = 0; k < 4; k++) 125 GNUNET_free (a3);
122 { 126 return 11;
123 if (0 != a3[i][j][k])
124 {
125 GNUNET_free(a3);
126 return 11;
127 }
128 a3[i][j][k] = i * 100 + j * 10 + k;
129 }
130 } 127 }
128 a3[i][j][k] = i * 100 + j * 10 + k;
129 }
131 } 130 }
132 GNUNET_free(a3); 131 }
132 GNUNET_free (a3);
133 return 0; 133 return 0;
134} 134}
135 135
136 136
137int 137int
138main(int argc, char *argv[]) 138main (int argc, char *argv[])
139{ 139{
140 int ret; 140 int ret;
141 141
142 GNUNET_log_setup("test-common-allocation", 142 GNUNET_log_setup ("test-common-allocation",
143 "WARNING", 143 "WARNING",
144 NULL); 144 NULL);
145 ret = check(); 145 ret = check ();
146 if (ret != 0) 146 if (ret != 0)
147 fprintf(stderr, 147 fprintf (stderr,
148 "ERROR %d.\n", 148 "ERROR %d.\n",
149 ret); 149 ret);
150 return ret; 150 return ret;
151} 151}
152 152
diff --git a/src/util/test_common_endian.c b/src/util/test_common_endian.c
index 02de4b5c6..0fc23fb44 100644
--- a/src/util/test_common_endian.c
+++ b/src/util/test_common_endian.c
@@ -24,17 +24,17 @@
24#include "platform.h" 24#include "platform.h"
25#include "gnunet_util_lib.h" 25#include "gnunet_util_lib.h"
26 26
27#define CHECK(n) if (n != GNUNET_htonll(GNUNET_ntohll(n))) return 1; 27#define CHECK(n) if (n != GNUNET_htonll (GNUNET_ntohll (n))) return 1;
28 28
29int 29int
30main(int argc, char *argv[]) 30main (int argc, char *argv[])
31{ 31{
32 GNUNET_log_setup("test-common-endian", "WARNING", NULL); 32 GNUNET_log_setup ("test-common-endian", "WARNING", NULL);
33 CHECK(1); 33 CHECK (1);
34 CHECK(0x12345678); 34 CHECK (0x12345678);
35 CHECK(123456789012345LL); 35 CHECK (123456789012345LL);
36 if ((0x1234567890ABCDEFLL != GNUNET_htonll(0xEFCDAB9078563412LL)) && 36 if ((0x1234567890ABCDEFLL != GNUNET_htonll (0xEFCDAB9078563412LL)) &&
37 42 != htonl(42)) 37 (42 != htonl (42)) )
38 return 1; 38 return 1;
39 return 0; 39 return 0;
40} 40}
diff --git a/src/util/test_common_logging.c b/src/util/test_common_logging.c
index 8492d3c35..d905a3bf3 100644
--- a/src/util/test_common_logging.c
+++ b/src/util/test_common_logging.c
@@ -27,8 +27,8 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
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
@@ -38,61 +38,61 @@ my_log(void *ctx, enum GNUNET_ErrorType kind, const char *component,
38 38
39 39
40int 40int
41main(int argc, char *argv[]) 41main (int argc, char *argv[])
42{ 42{
43 unsigned int failureCount = 0; 43 unsigned int failureCount = 0;
44 unsigned int logs = 0; 44 unsigned int logs = 0;
45 45
46 if (0 != putenv("GNUNET_FORCE_LOG=")) 46 if (0 != putenv ("GNUNET_FORCE_LOG="))
47 fprintf(stderr, "Failed to putenv: %s\n", strerror(errno)); 47 fprintf (stderr, "Failed to putenv: %s\n", strerror (errno));
48 GNUNET_log_setup("test-common-logging", "DEBUG", "/dev/null"); 48 GNUNET_log_setup ("test-common-logging", "DEBUG", "/dev/null");
49 GNUNET_logger_add(&my_log, &logs); 49 GNUNET_logger_add (&my_log, &logs);
50 GNUNET_logger_add(&my_log, &logs); 50 GNUNET_logger_add (&my_log, &logs);
51 GNUNET_log(GNUNET_ERROR_TYPE_BULK, "Testing...\n"); 51 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
52 GNUNET_log(GNUNET_ERROR_TYPE_BULK, "Testing...\n"); 52 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
53 GNUNET_log(GNUNET_ERROR_TYPE_BULK, "Testing...\n"); 53 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
54 GNUNET_log(GNUNET_ERROR_TYPE_BULK, "Testing...\n"); 54 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
55 GNUNET_log(GNUNET_ERROR_TYPE_BULK, "Testing...\n"); 55 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
56 GNUNET_log(GNUNET_ERROR_TYPE_BULK, "Testing...\n"); 56 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
57 GNUNET_logger_remove(&my_log, &logs); 57 GNUNET_logger_remove (&my_log, &logs);
58 GNUNET_log(GNUNET_ERROR_TYPE_BULK, "Flusher...\n"); 58 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Flusher...\n");
59 /* the last 6 calls should be merged (repated bulk messages!) */ 59 /* the last 6 calls should be merged (repated bulk messages!) */
60 GNUNET_logger_remove(&my_log, &logs); 60 GNUNET_logger_remove (&my_log, &logs);
61 if (logs != 4) 61 if (logs != 4)
62 { 62 {
63 fprintf(stdout, "Expected 4 log calls, got %u\n", logs); 63 fprintf (stdout, "Expected 4 log calls, got %u\n", logs);
64 failureCount++; 64 failureCount++;
65 } 65 }
66 GNUNET_break(0 == 66 GNUNET_break (0 ==
67 strcmp(_("ERROR"), 67 strcmp (_ ("ERROR"),
68 GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_ERROR))); 68 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_ERROR)));
69 GNUNET_break(0 == 69 GNUNET_break (0 ==
70 strcmp(_("WARNING"), 70 strcmp (_ ("WARNING"),
71 GNUNET_error_type_to_string 71 GNUNET_error_type_to_string
72 (GNUNET_ERROR_TYPE_WARNING))); 72 (GNUNET_ERROR_TYPE_WARNING)));
73 GNUNET_break(0 == 73 GNUNET_break (0 ==
74 strcmp(_("INFO"), 74 strcmp (_ ("INFO"),
75 GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_INFO))); 75 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_INFO)));
76 GNUNET_break(0 == 76 GNUNET_break (0 ==
77 strcmp(_("DEBUG"), 77 strcmp (_ ("DEBUG"),
78 GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG))); 78 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG)));
79 GNUNET_log_setup("test_common_logging", "WARNING", "/dev/null"); 79 GNUNET_log_setup ("test_common_logging", "WARNING", "/dev/null");
80 logs = 0; 80 logs = 0;
81 GNUNET_logger_add(&my_log, &logs); 81 GNUNET_logger_add (&my_log, &logs);
82 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Checker...\n"); 82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Checker...\n");
83 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Drop me...\n"); 83 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Drop me...\n");
84 GNUNET_logger_remove(&my_log, &logs); 84 GNUNET_logger_remove (&my_log, &logs);
85 if (logs != 1) 85 if (logs != 1)
86 { 86 {
87 fprintf(stdout, "Expected 1 log call, got %u\n", logs); 87 fprintf (stdout, "Expected 1 log call, got %u\n", logs);
88 failureCount++; 88 failureCount++;
89 } 89 }
90 90
91 if (failureCount != 0) 91 if (failureCount != 0)
92 { 92 {
93 fprintf(stdout, "%u TESTS FAILED!\n", failureCount); 93 fprintf (stdout, "%u TESTS FAILED!\n", failureCount);
94 return -1; 94 return -1;
95 } 95 }
96 return 0; 96 return 0;
97} /* end of main */ 97} /* end of main */
98 98
diff --git a/src/util/test_common_logging_dummy.c b/src/util/test_common_logging_dummy.c
index dc80eeccd..9a08714ec 100644
--- a/src/util/test_common_logging_dummy.c
+++ b/src/util/test_common_logging_dummy.c
@@ -36,31 +36,31 @@
36 * on a slow machine for a non-skipped one. 36 * on a slow machine for a non-skipped one.
37 */ 37 */
38#define OUTPUT_DELAY \ 38#define OUTPUT_DELAY \
39 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MICROSECONDS, 1000) 39 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 1000)
40 40
41static void 41static void
42my_log(void *ctx, 42my_log (void *ctx,
43 enum GNUNET_ErrorType kind, 43 enum GNUNET_ErrorType kind,
44 const char *component, 44 const char *component,
45 const char *date, 45 const char *date,
46 const char *msg) 46 const char *msg)
47{ 47{
48 (void)ctx; 48 (void) ctx;
49 (void)kind; 49 (void) kind;
50 (void)component; 50 (void) component;
51 (void)date; 51 (void) date;
52 if (strncmp("test-common-logging-dummy", component, 25) != 0) 52 if (strncmp ("test-common-logging-dummy", component, 25) != 0)
53 return; 53 return;
54 fprintf(stdout, "%s", msg); 54 fprintf (stdout, "%s", msg);
55 fflush(stdout); 55 fflush (stdout);
56} 56}
57 57
58 58
59#if !defined(GNUNET_CULL_LOGGING) 59#if ! defined(GNUNET_CULL_LOGGING)
60static int 60static int
61expensive_func() 61expensive_func ()
62{ 62{
63 return GNUNET_NETWORK_socket_select(NULL, NULL, NULL, OUTPUT_DELAY); 63 return GNUNET_NETWORK_socket_select (NULL, NULL, NULL, OUTPUT_DELAY);
64} 64}
65#endif 65#endif
66 66
@@ -68,51 +68,51 @@ expensive_func()
68#define pr(kind, lvl) \ 68#define pr(kind, lvl) \
69 { \ 69 { \
70 struct GNUNET_TIME_Absolute t1, t2; \ 70 struct GNUNET_TIME_Absolute t1, t2; \
71 t1 = GNUNET_TIME_absolute_get(); \ 71 t1 = GNUNET_TIME_absolute_get (); \
72 GNUNET_log(kind, "L%s %d\n", lvl, expensive_func()); \ 72 GNUNET_log (kind, "L%s %d\n", lvl, expensive_func ()); \
73 t2 = GNUNET_TIME_absolute_get(); \ 73 t2 = GNUNET_TIME_absolute_get (); \
74 printf("1%s %llu\n", \ 74 printf ("1%s %llu\n", \
75 lvl, \ 75 lvl, \
76 (unsigned long long)GNUNET_TIME_absolute_get_difference(t1, t2) \ 76 (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2) \
77 .rel_value_us); \ 77 .rel_value_us); \
78 } 78 }
79 79
80#define pr2(kind, lvl) \ 80#define pr2(kind, lvl) \
81 { \ 81 { \
82 struct GNUNET_TIME_Absolute t1, t2; \ 82 struct GNUNET_TIME_Absolute t1, t2; \
83 t1 = GNUNET_TIME_absolute_get(); \ 83 t1 = GNUNET_TIME_absolute_get (); \
84 GNUNET_log(kind, "L%s %d\n", lvl, expensive_func()); \ 84 GNUNET_log (kind, "L%s %d\n", lvl, expensive_func ()); \
85 t2 = GNUNET_TIME_absolute_get(); \ 85 t2 = GNUNET_TIME_absolute_get (); \
86 printf("2%s %llu\n", \ 86 printf ("2%s %llu\n", \
87 lvl, \ 87 lvl, \
88 (unsigned long long)GNUNET_TIME_absolute_get_difference(t1, t2) \ 88 (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2) \
89 .rel_value_us); \ 89 .rel_value_us); \
90 } 90 }
91 91
92 92
93int 93int
94main(int argc, char *argv[]) 94main (int argc, char *argv[])
95{ 95{
96 (void)argc; 96 (void) argc;
97 (void)argv; 97 (void) argv;
98 /* We set up logging with NULL level - will be overrided by 98 /* We set up logging with NULL level - will be overrided by
99 * GNUNET_LOG or GNUNET_FORCE_LOG at runtime. 99 * GNUNET_LOG or GNUNET_FORCE_LOG at runtime.
100 */ 100 */
101 GNUNET_log_setup("test-common-logging-dummy", NULL, "/dev/null"); 101 GNUNET_log_setup ("test-common-logging-dummy", NULL, "/dev/null");
102 GNUNET_logger_add(&my_log, NULL); 102 GNUNET_logger_add (&my_log, NULL);
103 pr(GNUNET_ERROR_TYPE_ERROR, "ERROR"); 103 pr (GNUNET_ERROR_TYPE_ERROR, "ERROR");
104 pr(GNUNET_ERROR_TYPE_WARNING, "WARNING"); 104 pr (GNUNET_ERROR_TYPE_WARNING, "WARNING");
105 pr(GNUNET_ERROR_TYPE_INFO, "INFO"); 105 pr (GNUNET_ERROR_TYPE_INFO, "INFO");
106 pr(GNUNET_ERROR_TYPE_DEBUG, "DEBUG"); 106 pr (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
107 107
108 /* We set up logging with WARNING level - will onle be overrided by 108 /* We set up logging with WARNING level - will onle be overrided by
109 * GNUNET_FORCE_LOG at runtime. 109 * GNUNET_FORCE_LOG at runtime.
110 */ 110 */
111 GNUNET_log_setup("test-common-logging-dummy", "WARNING", "/dev/null"); 111 GNUNET_log_setup ("test-common-logging-dummy", "WARNING", "/dev/null");
112 pr2(GNUNET_ERROR_TYPE_ERROR, "ERROR"); 112 pr2 (GNUNET_ERROR_TYPE_ERROR, "ERROR");
113 pr2(GNUNET_ERROR_TYPE_WARNING, "WARNING"); 113 pr2 (GNUNET_ERROR_TYPE_WARNING, "WARNING");
114 pr2(GNUNET_ERROR_TYPE_INFO, "INFO"); 114 pr2 (GNUNET_ERROR_TYPE_INFO, "INFO");
115 pr2(GNUNET_ERROR_TYPE_DEBUG, "DEBUG"); 115 pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
116 return 0; 116 return 0;
117} /* end of main */ 117} /* end of main */
118 118
diff --git a/src/util/test_common_logging_runtime_loglevels.c b/src/util/test_common_logging_runtime_loglevels.c
index 4ba7b544c..b82138492 100644
--- a/src/util/test_common_logging_runtime_loglevels.c
+++ b/src/util/test_common_logging_runtime_loglevels.c
@@ -32,13 +32,15 @@
32 * How much time the child is allowed to waste on skipped log calls, at most. 32 * How much time the child is allowed to waste on skipped log calls, at most.
33 * Raspberry Pi takes 113 microseconds tops, this is 3x that value. 33 * Raspberry Pi takes 113 microseconds tops, this is 3x that value.
34 */ 34 */
35#define MAX_SKIP_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MICROSECONDS, 400).rel_value_us 35#define MAX_SKIP_DELAY GNUNET_TIME_relative_multiply ( \
36 GNUNET_TIME_UNIT_MICROSECONDS, 400).rel_value_us
36 37
37/** 38/**
38 * How much time non-skipped log call should take, at least. 39 * How much time non-skipped log call should take, at least.
39 * Keep in sync with the value in the dummy! 40 * Keep in sync with the value in the dummy!
40 */ 41 */
41#define OUTPUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MICROSECONDS, 1000).rel_value_us 42#define OUTPUT_DELAY GNUNET_TIME_relative_multiply ( \
43 GNUNET_TIME_UNIT_MICROSECONDS, 1000).rel_value_us
42 44
43static int ok; 45static int ok;
44 46
@@ -49,56 +51,56 @@ static struct GNUNET_OS_Process *proc;
49/* Pipe to read from started processes stdout (on read end) */ 51/* Pipe to read from started processes stdout (on read end) */
50static struct GNUNET_DISK_PipeHandle *pipe_stdout; 52static struct GNUNET_DISK_PipeHandle *pipe_stdout;
51 53
52static struct GNUNET_SCHEDULER_Task * die_task; 54static struct GNUNET_SCHEDULER_Task *die_task;
53 55
54static struct GNUNET_SCHEDULER_Task * read_task; 56static struct GNUNET_SCHEDULER_Task *read_task;
55 57
56static void 58static void
57runone(void); 59runone (void);
58 60
59 61
60static void 62static void
61end_task(void *cls) 63end_task (void *cls)
62{ 64{
63 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase, 65 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase,
64 ok); 66 ok);
65 if (NULL != proc) 67 if (NULL != proc)
68 {
69 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
66 { 70 {
67 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) 71 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
68 {
69 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
70 }
71 GNUNET_OS_process_wait(proc);
72 GNUNET_OS_process_destroy(proc);
73 proc = NULL;
74 } 72 }
73 GNUNET_OS_process_wait (proc);
74 GNUNET_OS_process_destroy (proc);
75 proc = NULL;
76 }
75 if (NULL != read_task) 77 if (NULL != read_task)
76 { 78 {
77 GNUNET_SCHEDULER_cancel(read_task); 79 GNUNET_SCHEDULER_cancel (read_task);
78 read_task = NULL; 80 read_task = NULL;
79 } 81 }
80 GNUNET_DISK_pipe_close(pipe_stdout); 82 GNUNET_DISK_pipe_close (pipe_stdout);
81 if (ok == 1) 83 if (ok == 1)
84 {
85 if (phase < 9)
82 { 86 {
83 if (phase < 9) 87 phase += 1;
84 { 88 runone ();
85 phase += 1;
86 runone();
87 }
88 else
89 ok = 0;
90 } 89 }
90 else
91 ok = 0;
92 }
91 else 93 else
92 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "failing\n"); 94 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
93} 95}
94 96
95 97
96static char * 98static char *
97read_output_line(int phase_from1, int phase_to1, int phase_from2, 99read_output_line (int phase_from1, int phase_to1, int phase_from2,
98 int phase_to2, char c, const char *expect_level, 100 int phase_to2, char c, const char *expect_level,
99 long delay_morethan, long delay_lessthan, int phase, 101 long delay_morethan, long delay_lessthan, int phase,
100 char *p, 102 char *p,
101 int *len, long *delay, char level[8]) 103 int *len, long *delay, char level[8])
102{ 104{
103 char *r = p; 105 char *r = p;
104 char t[7]; 106 char t[7];
@@ -111,83 +113,84 @@ read_output_line(int phase_from1, int phase_to1, int phase_from2,
111 j = 0; 113 j = 0;
112 int stage = 0; 114 int stage = 0;
113 115
114 if (!(phase >= phase_from1 && phase <= phase_to1) && 116 if (! ((phase >= phase_from1)&&(phase <= phase_to1)) &&
115 !(phase >= phase_from2 && phase <= phase_to2)) 117 ! ((phase >= phase_from2)&&(phase <= phase_to2)))
116 return p; 118 return p;
117#if 0 119#if 0
118 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p); 121 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p);
120#endif 122#endif
121 for (i = 0; i < *len && !stop; i++) 123 for (i = 0; i < *len && ! stop; i++)
124 {
125 switch (stage)
122 { 126 {
123 switch (stage) 127 case 0: /* read first char */
124 { 128 if (r[i] != c)
125 case 0: /* read first char */ 129 {
126 if (r[i] != c) 130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c,
127 { 131 r[i]);
128 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c, 132 GNUNET_break (0);
129 r[i]); 133 return NULL;
130 GNUNET_break(0); 134 }
131 return NULL; 135 stage += 1;
132 } 136 break;
133 stage += 1; 137
134 break; 138 case 1: /* read at most 7 char-long error level string, finished by ' ' */
135 139 if (r[i] == ' ')
136 case 1: /* read at most 7 char-long error level string, finished by ' ' */ 140 {
137 if (r[i] == ' ') 141 level[j] = '\0';
138 { 142 stage += 1;
139 level[j] = '\0'; 143 j = 0;
140 stage += 1; 144 }
141 j = 0; 145 else if (i == 8)
142 } 146 {
143 else if (i == 8) 147 GNUNET_break (0);
144 { 148 ok = 2;
145 GNUNET_break(0); 149 return NULL;
146 ok = 2; 150 }
147 return NULL; 151 else
148 } 152 level[j++] = r[i];
149 else 153 break;
150 level[j++] = r[i]; 154
151 break; 155 case 2: /* read the delay, finished by '\n' */
152 156 t[j++] = r[i];
153 case 2: /* read the delay, finished by '\n' */ 157 if (r[i] == '\n')
154 t[j++] = r[i]; 158 {
155 if (r[i] == '\n') 159 t[j - 1] = '\0';
156 { 160 *delay = strtol (t, NULL, 10);
157 t[j - 1] = '\0'; 161 stop = 1;
158 *delay = strtol(t, NULL, 10); 162 }
159 stop = 1; 163 break;
160 }
161 break;
162 }
163 } 164 }
164 level_matches = (strcmp(expect_level, level) == 0); 165 }
166 level_matches = (strcmp (expect_level, level) == 0);
165 delay_is_sane = (*delay >= 0) && (*delay <= 1000000); 167 delay_is_sane = (*delay >= 0) && (*delay <= 1000000);
166 delay_is_a_dummy = (c == 'L'); 168 delay_is_a_dummy = (c == 'L');
167 /* Delay must be either less than 'lessthan' (log call is skipped) 169 /* Delay must be either less than 'lessthan' (log call is skipped)
168 * or more than 'morethan' (log call is not skipped) 170 * or more than 'morethan' (log call is not skipped)
169 */ 171 */
170 delay_outside_of_range = ((*delay < delay_lessthan) || (*delay >= delay_morethan)); 172 delay_outside_of_range = ((*delay < delay_lessthan) || (*delay >=
173 delay_morethan));
171 if (delay_is_a_dummy) 174 if (delay_is_a_dummy)
172 delay_outside_of_range = 1; 175 delay_outside_of_range = 1;
173 176
174 if (!stop) 177 if (! stop)
175 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 "Wrong log format?\n"); 179 "Wrong log format?\n");
177 if (!level_matches) 180 if (! level_matches)
178 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 "Wrong log level\n"); 182 "Wrong log level\n");
180 if (!delay_is_sane) 183 if (! delay_is_sane)
181 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Delay %ld is insane\n", 185 "Delay %ld is insane\n",
183 *delay); 186 *delay);
184 if (!delay_outside_of_range) 187 if (! delay_outside_of_range)
185 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 "Delay %ld is not outside of range (%ld ; %ld)\n", 189 "Delay %ld is not outside of range (%ld ; %ld)\n",
187 *delay, 190 *delay,
188 delay_lessthan, 191 delay_lessthan,
189 delay_morethan); 192 delay_morethan);
190 if (!stop || !level_matches || !delay_is_sane || !delay_outside_of_range) 193 if (! stop || ! level_matches || ! delay_is_sane || ! delay_outside_of_range)
191 return NULL; 194 return NULL;
192 *len = *len - i; 195 *len = *len - i;
193 return &r[i]; 196 return &r[i];
@@ -203,7 +206,7 @@ read_output_line(int phase_from1, int phase_to1, int phase_from2,
203 */ 206 */
204#define LOG_MAX_LINE_LENGTH (17) 207#define LOG_MAX_LINE_LENGTH (17)
205 208
206#define LOG_BUFFER_SIZE LOG_MAX_NUM_LINES * LOG_MAX_LINE_LENGTH 209#define LOG_BUFFER_SIZE LOG_MAX_NUM_LINES *LOG_MAX_LINE_LENGTH
207 210
208static char buf[LOG_BUFFER_SIZE]; 211static char buf[LOG_BUFFER_SIZE];
209 212
@@ -213,7 +216,7 @@ static int bytes;
213 216
214 217
215static void 218static void
216read_call(void *cls) 219read_call (void *cls)
217{ 220{
218 const struct GNUNET_DISK_FileHandle *stdout_read_handle = cls; 221 const struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
219 char level[8]; 222 char level[8];
@@ -222,24 +225,24 @@ read_call(void *cls)
222 int rd; 225 int rd;
223 226
224 read_task = NULL; 227 read_task = NULL;
225 rd = GNUNET_DISK_file_read(stdout_read_handle, buf_ptr, 228 rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr,
226 sizeof(buf) - bytes); 229 sizeof(buf) - bytes);
227 if (rd > 0) 230 if (rd > 0)
228 { 231 {
229 buf_ptr += rd; 232 buf_ptr += rd;
230 bytes += rd; 233 bytes += rd;
231#if VERBOSE 234#if VERBOSE
232 fprintf(stderr, "got %d bytes, reading more\n", rd); 235 fprintf (stderr, "got %d bytes, reading more\n", rd);
233#endif 236#endif
234 read_task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 237 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
235 stdout_read_handle, 238 stdout_read_handle,
236 &read_call, 239 &read_call,
237 (void*)stdout_read_handle); 240 (void*) stdout_read_handle);
238 return; 241 return;
239 } 242 }
240 243
241#if VERBOSE 244#if VERBOSE
242 fprintf(stderr, "bytes is %d:%s\n", bytes, buf); 245 fprintf (stderr, "bytes is %d:%s\n", bytes, buf);
243#endif 246#endif
244 247
245 /* +------CHILD OUTPUT-- 248 /* +------CHILD OUTPUT--
@@ -259,193 +262,194 @@ read_call(void *cls)
259 */ 262 */
260 char *p = buf; 263 char *p = buf;
261 264
262 if (bytes == LOG_BUFFER_SIZE || 265 if ((bytes == LOG_BUFFER_SIZE)||
263 !(p = 266 ! (p =
264 read_output_line(0, 3, 4, 9, 'L', "ERROR", -1, 267 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1,
265 1, phase, p, 268 1, phase, p,
266 &bytes, &delay, level)) || 269 &bytes, &delay, level)) ||
267 !(p = 270 ! (p =
268 read_output_line(0, 3, 4, 9, '1', "ERROR", OUTPUT_DELAY, 271 read_output_line (0, 3, 4, 9, '1', "ERROR", OUTPUT_DELAY,
269 MAX_SKIP_DELAY, phase, p, 272 MAX_SKIP_DELAY, phase, p,
270 &bytes, &delays[0], level)) || 273 &bytes, &delays[0], level)) ||
271 !(p = 274 ! (p =
272 read_output_line(1, 3, 5, 9, 'L', "WARNING", -1, 275 read_output_line (1, 3, 5, 9, 'L', "WARNING", -1,
273 1, phase, p, 276 1, phase, p,
274 &bytes, &delay, level)) || 277 &bytes, &delay, level)) ||
275 !(p = 278 ! (p =
276 read_output_line(0, 3, 4, 9, '1', "WARNING", OUTPUT_DELAY, 279 read_output_line (0, 3, 4, 9, '1', "WARNING", OUTPUT_DELAY,
277 MAX_SKIP_DELAY, phase, p, 280 MAX_SKIP_DELAY, phase, p,
278 &bytes, &delays[1], level)) || 281 &bytes, &delays[1], level)) ||
279 !(p = 282 ! (p =
280 read_output_line(2, 3, 6, 7, 'L', "INFO", -1, 283 read_output_line (2, 3, 6, 7, 'L', "INFO", -1,
281 1, phase, p, 284 1, phase, p,
282 &bytes, &delay, level)) || 285 &bytes, &delay, level)) ||
283 !(p = 286 ! (p =
284 read_output_line(0, 3, 4, 9, '1', "INFO", OUTPUT_DELAY, 287 read_output_line (0, 3, 4, 9, '1', "INFO", OUTPUT_DELAY,
285 MAX_SKIP_DELAY, phase, p, 288 MAX_SKIP_DELAY, phase, p,
286 &bytes, &delays[2], level)) || 289 &bytes, &delays[2], level)) ||
287 !(p = 290 ! (p =
288 read_output_line(3, 3, 7, 7, 'L', "DEBUG", -1, 291 read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1,
289 1, phase, p, 292 1, phase, p,
290 &bytes, &delay, level)) || 293 &bytes, &delay, level)) ||
291 !(p = 294 ! (p =
292 read_output_line(0, 3, 4, 9, '1', "DEBUG", OUTPUT_DELAY, 295 read_output_line (0, 3, 4, 9, '1', "DEBUG", OUTPUT_DELAY,
293 MAX_SKIP_DELAY, phase, p, 296 MAX_SKIP_DELAY, phase, p,
294 &bytes, &delays[3], level)) || 297 &bytes, &delays[3], level)) ||
295 !(p = 298 ! (p =
296 read_output_line(0, 3, 4, 9, 'L', "ERROR", -1, 299 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1,
297 1, phase, p, 300 1, phase, p,
298 &bytes, &delay, level)) || 301 &bytes, &delay, level)) ||
299 !(p = 302 ! (p =
300 read_output_line(0, 3, 4, 9, '2', "ERROR", OUTPUT_DELAY, 303 read_output_line (0, 3, 4, 9, '2', "ERROR", OUTPUT_DELAY,
301 MAX_SKIP_DELAY, phase, p, 304 MAX_SKIP_DELAY, phase, p,
302 &bytes, &delays[4], level)) || 305 &bytes, &delays[4], level)) ||
303 !(p = 306 ! (p =
304 read_output_line(0, 3, 5, 9, 'L', "WARNING", -1, 307 read_output_line (0, 3, 5, 9, 'L', "WARNING", -1,
305 1, phase, p, 308 1, phase, p,
306 &bytes, &delay, level)) || 309 &bytes, &delay, level)) ||
307 !(p = 310 ! (p =
308 read_output_line(0, 3, 4, 9, '2', "WARNING", OUTPUT_DELAY, 311 read_output_line (0, 3, 4, 9, '2', "WARNING", OUTPUT_DELAY,
309 MAX_SKIP_DELAY, phase, p, 312 MAX_SKIP_DELAY, phase, p,
310 &bytes, &delays[5], level)) || 313 &bytes, &delays[5], level)) ||
311 !(p = 314 ! (p =
312 read_output_line(-1, -1, 6, 7, 'L', "INFO", -1, 315 read_output_line (-1, -1, 6, 7, 'L', "INFO", -1,
313 1, phase, p, 316 1, phase, p,
314 &bytes, &delay, level)) || 317 &bytes, &delay, level)) ||
315 !(p = 318 ! (p =
316 read_output_line(0, 3, 4, 9, '2', "INFO", OUTPUT_DELAY, 319 read_output_line (0, 3, 4, 9, '2', "INFO", OUTPUT_DELAY,
317 MAX_SKIP_DELAY, phase, p, 320 MAX_SKIP_DELAY, phase, p,
318 &bytes, &delays[6], level)) || 321 &bytes, &delays[6], level)) ||
319 !(p = 322 ! (p =
320 read_output_line(-1, -1, 7, 7, 'L', "DEBUG", -1, 323 read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1,
321 1, phase, p, 324 1, phase, p,
322 &bytes, &delay, level)) || 325 &bytes, &delay, level)) ||
323 !(p = 326 ! (p =
324 read_output_line(0, 3, 4, 9, '2', "DEBUG", OUTPUT_DELAY, 327 read_output_line (0, 3, 4, 9, '2', "DEBUG", OUTPUT_DELAY,
325 MAX_SKIP_DELAY, phase, p, 328 MAX_SKIP_DELAY, phase, p,
326 &bytes, &delays[7], level))) 329 &bytes, &delays[7], level)))
327 { 330 {
328 if (bytes == LOG_BUFFER_SIZE) 331 if (bytes == LOG_BUFFER_SIZE)
329 fprintf(stderr, "%s", "Ran out of buffer space!\n"); 332 fprintf (stderr, "%s", "Ran out of buffer space!\n");
330 GNUNET_break(0); 333 GNUNET_break (0);
331 ok = 2; 334 ok = 2;
332 GNUNET_SCHEDULER_cancel(die_task); 335 GNUNET_SCHEDULER_cancel (die_task);
333 GNUNET_SCHEDULER_add_now(&end_task, NULL); 336 GNUNET_SCHEDULER_add_now (&end_task, NULL);
334 return; 337 return;
335 } 338 }
336 339
337 GNUNET_SCHEDULER_cancel(die_task); 340 GNUNET_SCHEDULER_cancel (die_task);
338 GNUNET_SCHEDULER_add_now(&end_task, NULL); 341 GNUNET_SCHEDULER_add_now (&end_task, NULL);
339} 342}
340 343
341 344
342static void 345static void
343runone() 346runone ()
344{ 347{
345 const struct GNUNET_DISK_FileHandle *stdout_read_handle; 348 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
346 349
347 pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 350 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
351 GNUNET_YES);
348 352
349 if (pipe_stdout == NULL) 353 if (pipe_stdout == NULL)
350 { 354 {
351 GNUNET_break(0); 355 GNUNET_break (0);
352 ok = 2; 356 ok = 2;
353 return; 357 return;
354 } 358 }
355 359
356 putenv("GNUNET_LOG="); 360 putenv ("GNUNET_LOG=");
357 putenv("GNUNET_FORCE_LOG="); 361 putenv ("GNUNET_FORCE_LOG=");
358 putenv("GNUNET_FORCE_LOGFILE="); 362 putenv ("GNUNET_FORCE_LOGFILE=");
359 switch (phase) 363 switch (phase)
360 { 364 {
361 case 0: 365 case 0:
362 putenv("GNUNET_LOG=;;;;ERROR"); 366 putenv ("GNUNET_LOG=;;;;ERROR");
363 break; 367 break;
364 368
365 case 1: 369 case 1:
366 putenv("GNUNET_LOG=;;;;WARNING"); 370 putenv ("GNUNET_LOG=;;;;WARNING");
367 break; 371 break;
368 372
369 case 2: 373 case 2:
370 putenv("GNUNET_LOG=;;;;INFO"); 374 putenv ("GNUNET_LOG=;;;;INFO");
371 break; 375 break;
372 376
373 case 3: 377 case 3:
374 putenv("GNUNET_LOG=;;;;DEBUG"); 378 putenv ("GNUNET_LOG=;;;;DEBUG");
375 break; 379 break;
376 380
377 case 4: 381 case 4:
378 putenv("GNUNET_FORCE_LOG=;;;;ERROR"); 382 putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
379 break; 383 break;
380 384
381 case 5: 385 case 5:
382 putenv("GNUNET_FORCE_LOG=;;;;WARNING"); 386 putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
383 break; 387 break;
384 388
385 case 6: 389 case 6:
386 putenv("GNUNET_FORCE_LOG=;;;;INFO"); 390 putenv ("GNUNET_FORCE_LOG=;;;;INFO");
387 break; 391 break;
388 392
389 case 7: 393 case 7:
390 putenv("GNUNET_FORCE_LOG=;;;;DEBUG"); 394 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
391 break; 395 break;
392 396
393 case 8: 397 case 8:
394 putenv("GNUNET_LOG=blah;;;;ERROR"); 398 putenv ("GNUNET_LOG=blah;;;;ERROR");
395 break; 399 break;
396 400
397 case 9: 401 case 9:
398 putenv("GNUNET_FORCE_LOG=blah;;;;ERROR"); 402 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
399 break; 403 break;
400 } 404 }
401 405
402 proc = GNUNET_OS_start_process(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 406 proc = GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
403 NULL, pipe_stdout, NULL, 407 NULL, pipe_stdout, NULL,
404 "./test_common_logging_dummy", 408 "./test_common_logging_dummy",
405 "test_common_logging_dummy", NULL); 409 "test_common_logging_dummy", NULL);
406 GNUNET_assert(NULL != proc); 410 GNUNET_assert (NULL != proc);
407 putenv("GNUNET_FORCE_LOG="); 411 putenv ("GNUNET_FORCE_LOG=");
408 putenv("GNUNET_LOG="); 412 putenv ("GNUNET_LOG=");
409 413
410 /* Close the write end of the read pipe */ 414 /* Close the write end of the read pipe */
411 GNUNET_DISK_pipe_close_end(pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); 415 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
412 416
413 stdout_read_handle = 417 stdout_read_handle =
414 GNUNET_DISK_pipe_handle(pipe_stdout, GNUNET_DISK_PIPE_END_READ); 418 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
415 419
416 die_task = 420 die_task =
417 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply 421 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
418 (GNUNET_TIME_UNIT_SECONDS, 10), 422 (GNUNET_TIME_UNIT_SECONDS, 10),
419 &end_task, 423 &end_task,
420 NULL); 424 NULL);
421 425
422 bytes = 0; 426 bytes = 0;
423 buf_ptr = buf; 427 buf_ptr = buf;
424 memset(&buf, 0, sizeof(buf)); 428 memset (&buf, 0, sizeof(buf));
425 429
426 read_task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 430 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
427 stdout_read_handle, 431 stdout_read_handle,
428 &read_call, 432 &read_call,
429 (void*)stdout_read_handle); 433 (void*) stdout_read_handle);
430} 434}
431 435
432 436
433static void 437static void
434task(void *cls) 438task (void *cls)
435{ 439{
436 phase = 0; 440 phase = 0;
437 runone(); 441 runone ();
438} 442}
439 443
440 444
441int 445int
442main(int argc, char *argv[]) 446main (int argc, char *argv[])
443{ 447{
444 GNUNET_log_setup("test-common-logging-runtime-loglevels", 448 GNUNET_log_setup ("test-common-logging-runtime-loglevels",
445 "WARNING", 449 "WARNING",
446 NULL); 450 NULL);
447 ok = 1; 451 ok = 1;
448 GNUNET_SCHEDULER_run(&task, &ok); 452 GNUNET_SCHEDULER_run (&task, &ok);
449 return ok; 453 return ok;
450} 454}
451 455
diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c
index b5b1e238c..c3a773ab6 100644
--- a/src/util/test_configuration.c
+++ b/src/util/test_configuration.c
@@ -28,7 +28,8 @@
28 28
29 29
30/* Test Configuration Diffs Options */ 30/* Test Configuration Diffs Options */
31enum { 31enum
32{
32 EDIT_NOTHING, 33 EDIT_NOTHING,
33 EDIT_SECTION, 34 EDIT_SECTION,
34 EDIT_ALL, 35 EDIT_ALL,
@@ -43,7 +44,8 @@ enum {
43static struct GNUNET_CONFIGURATION_Handle *cfg; 44static struct GNUNET_CONFIGURATION_Handle *cfg;
44static struct GNUNET_CONFIGURATION_Handle *cfg_default; 45static struct GNUNET_CONFIGURATION_Handle *cfg_default;
45 46
46struct DiffsCBData { 47struct DiffsCBData
48{
47 struct GNUNET_CONFIGURATION_Handle *cfg; 49 struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_CONFIGURATION_Handle *cfgDiffs; 50 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
49 const char *section; 51 const char *section;
@@ -53,7 +55,7 @@ struct DiffsCBData {
53 55
54 56
55static void 57static void
56initDiffsCBData(struct DiffsCBData *cbData) 58initDiffsCBData (struct DiffsCBData *cbData)
57{ 59{
58 cbData->section = NULL; 60 cbData->section = NULL;
59 cbData->cfg = NULL; 61 cbData->cfg = NULL;
@@ -68,136 +70,136 @@ initDiffsCBData(struct DiffsCBData *cbData)
68 * and comparing configuration 70 * and comparing configuration
69 */ 71 */
70static void 72static void
71diffsCallBack(void *cls, const char *section, const char *option, 73diffsCallBack (void *cls, const char *section, const char *option,
72 const char *value) 74 const char *value)
73{ 75{
74 struct DiffsCBData *cbData = cls; 76 struct DiffsCBData *cbData = cls;
75 int cbOption = cbData->callBackOption; 77 int cbOption = cbData->callBackOption;
76 78
77 switch (cbOption) 79 switch (cbOption)
80 {
81 case EDIT_SECTION:
82 if (NULL == cbData->section)
83 cbData->section = section;
84 if (strcmp (cbData->section, section) == 0)
78 { 85 {
79 case EDIT_SECTION: 86 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
80 if (NULL == cbData->section) 87 "new-value");
81 cbData->section = section; 88 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
82 if (strcmp(cbData->section, section) == 0) 89 "new-value");
83 { 90 }
84 GNUNET_CONFIGURATION_set_value_string(cbData->cfg, section, option, 91 break;
85 "new-value");
86 GNUNET_CONFIGURATION_set_value_string(cbData->cfgDiffs, section, option,
87 "new-value");
88 }
89 break;
90 92
91 case EDIT_ALL: 93 case EDIT_ALL:
92 GNUNET_CONFIGURATION_set_value_string(cbData->cfg, section, option, 94 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
93 "new-value"); 95 "new-value");
94 GNUNET_CONFIGURATION_set_value_string(cbData->cfgDiffs, section, option, 96 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
95 "new-value"); 97 "new-value");
96 break; 98 break;
97 99
98 case ADD_NEW_ENTRY: 100 case ADD_NEW_ENTRY:
99 { 101 {
100 static int hit = 0; 102 static int hit = 0;
101 103
102 if (hit == 0) 104 if (hit == 0)
103 { 105 {
104 hit = 1; 106 hit = 1;
105 GNUNET_CONFIGURATION_set_value_string(cbData->cfg, section, "new-key", 107 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
106 "new-value"); 108 "new-value");
107 GNUNET_CONFIGURATION_set_value_string(cbData->cfgDiffs, section, 109 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
108 "new-key", "new-value"); 110 "new-key", "new-value");
109 } 111 }
110 break; 112 break;
111 } 113 }
112 114
113 case COMPARE: 115 case COMPARE:
114 { 116 {
115 int ret; 117 int ret;
116 char *diffValue; 118 char *diffValue;
117 119
118 diffValue = NULL; 120 diffValue = NULL;
119 ret = 121 ret =
120 GNUNET_CONFIGURATION_get_value_string(cbData->cfgDiffs, section, 122 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
121 option, &diffValue); 123 option, &diffValue);
122 if (NULL != diffValue) 124 if (NULL != diffValue)
123 { 125 {
124 if (ret == GNUNET_SYSERR || strcmp(diffValue, value) != 0) 126 if ((ret == GNUNET_SYSERR) ||(strcmp (diffValue, value) != 0) )
125 cbData->status = 1; 127 cbData->status = 1;
126 } 128 }
127 else 129 else
128 cbData->status = 1; 130 cbData->status = 1;
129 GNUNET_free_non_null(diffValue); 131 GNUNET_free_non_null (diffValue);
130 break; 132 break;
131 } 133 }
132 134
133#if 0 135#if 0
134 case PRINT: 136 case PRINT:
135 if (NULL == cbData->section) 137 if (NULL == cbData->section)
136 { 138 {
137 cbData->section = section; 139 cbData->section = section;
138 printf("\nSection: %s\n", section); 140 printf ("\nSection: %s\n", section);
139 }
140 else if (strcmp(cbData->section, section) != 0)
141 {
142 cbData->section = section;
143 printf("\nSection: %s\n", section);
144 }
145 printf("%s = %s\n", option, value);
146#endif
147 default:
148 break;
149 } 141 }
142 else if (strcmp (cbData->section, section) != 0)
143 {
144 cbData->section = section;
145 printf ("\nSection: %s\n", section);
146 }
147 printf ("%s = %s\n", option, value);
148#endif
149 default:
150 break;
151 }
150} 152}
151 153
152 154
153static struct GNUNET_CONFIGURATION_Handle * 155static struct GNUNET_CONFIGURATION_Handle *
154editConfiguration(struct GNUNET_CONFIGURATION_Handle *cfg, int option) 156editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
155{ 157{
156 struct DiffsCBData diffsCB; 158 struct DiffsCBData diffsCB;
157 159
158 initDiffsCBData(&diffsCB); 160 initDiffsCBData (&diffsCB);
159 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create(); 161 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
160 162
161 switch (option) 163 switch (option)
162 { 164 {
163 case EDIT_SECTION: 165 case EDIT_SECTION:
164 case EDIT_ALL: 166 case EDIT_ALL:
165 case ADD_NEW_ENTRY: 167 case ADD_NEW_ENTRY:
166 diffsCB.callBackOption = option; 168 diffsCB.callBackOption = option;
167 diffsCB.cfg = cfg; 169 diffsCB.cfg = cfg;
168 GNUNET_CONFIGURATION_iterate(cfg, diffsCallBack, &diffsCB); 170 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
169 break; 171 break;
170 172
171 case EDIT_NOTHING: 173 case EDIT_NOTHING:
172 /* Do nothing */ 174 /* Do nothing */
173 break; 175 break;
174 176
175 case ADD_NEW_SECTION: 177 case ADD_NEW_SECTION:
176 { 178 {
177 int i; 179 int i;
178 char *key; 180 char *key;
179 181
180 for (i = 0; i < 5; i++) 182 for (i = 0; i < 5; i++)
181 { 183 {
182 GNUNET_asprintf(&key, "key%d", i); 184 GNUNET_asprintf (&key, "key%d", i);
183 GNUNET_CONFIGURATION_set_value_string(cfg, "new-section", key, 185 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
184 "new-value"); 186 "new-value");
185 GNUNET_CONFIGURATION_set_value_string(diffsCB.cfgDiffs, "new-section", 187 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
186 key, "new-value"); 188 key, "new-value");
187 GNUNET_free(key); 189 GNUNET_free (key);
188 } 190 }
189 break; 191 break;
190 } 192 }
191 193
192 case REMOVE_SECTION: 194 case REMOVE_SECTION:
193 break; 195 break;
194 196
195 case REMOVE_ENTRY: 197 case REMOVE_ENTRY:
196 break; 198 break;
197 199
198 default: 200 default:
199 break; 201 break;
200 } 202 }
201 203
202 return diffsCB.cfgDiffs; 204 return diffsCB.cfgDiffs;
203} 205}
@@ -206,7 +208,7 @@ editConfiguration(struct GNUNET_CONFIGURATION_Handle *cfg, int option)
206 * Checking configuration diffs 208 * Checking configuration diffs
207 */ 209 */
208static int 210static int
209checkDiffs(struct GNUNET_CONFIGURATION_Handle *cfg_default, int option) 211checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
210{ 212{
211 struct GNUNET_CONFIGURATION_Handle *cfg; 213 struct GNUNET_CONFIGURATION_Handle *cfg;
212 struct GNUNET_CONFIGURATION_Handle *cfgDiffs; 214 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
@@ -214,135 +216,136 @@ checkDiffs(struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
214 int ret; 216 int ret;
215 char *diffsFileName; 217 char *diffsFileName;
216 218
217 initDiffsCBData(&cbData); 219 initDiffsCBData (&cbData);
218 220
219 cfg = GNUNET_CONFIGURATION_create(); 221 cfg = GNUNET_CONFIGURATION_create ();
220 /* load defaults */ 222 /* load defaults */
221 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(cfg, NULL)); 223 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
222 224
223 /* Modify configuration and save it */ 225 /* Modify configuration and save it */
224 cfgDiffs = editConfiguration(cfg, option); 226 cfgDiffs = editConfiguration (cfg, option);
225 diffsFileName = GNUNET_DISK_mktemp("gnunet-test-configurations-diffs.conf"); 227 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
226 if (diffsFileName == NULL) 228 if (diffsFileName == NULL)
227 { 229 {
228 GNUNET_break(0); 230 GNUNET_break (0);
229 GNUNET_CONFIGURATION_destroy(cfg); 231 GNUNET_CONFIGURATION_destroy (cfg);
230 GNUNET_CONFIGURATION_destroy(cfgDiffs); 232 GNUNET_CONFIGURATION_destroy (cfgDiffs);
231 return 1; 233 return 1;
232 } 234 }
233 GNUNET_CONFIGURATION_write_diffs(cfg_default, cfg, diffsFileName); 235 GNUNET_CONFIGURATION_write_diffs (cfg_default, cfg, diffsFileName);
234 GNUNET_CONFIGURATION_destroy(cfg); 236 GNUNET_CONFIGURATION_destroy (cfg);
235 237
236 /* Compare the dumped configuration with modifications done */ 238 /* Compare the dumped configuration with modifications done */
237 cfg = GNUNET_CONFIGURATION_create(); 239 cfg = GNUNET_CONFIGURATION_create ();
238 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_parse(cfg, diffsFileName)); 240 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
239 if (0 != remove(diffsFileName)) 241 if (0 != remove (diffsFileName))
240 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "remove", diffsFileName); 242 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove",
243 diffsFileName);
241 cbData.callBackOption = COMPARE; 244 cbData.callBackOption = COMPARE;
242 cbData.cfgDiffs = cfgDiffs; 245 cbData.cfgDiffs = cfgDiffs;
243 GNUNET_CONFIGURATION_iterate(cfg, diffsCallBack, &cbData); 246 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
244 if (1 == (ret = cbData.status)) 247 if (1 == (ret = cbData.status))
245 { 248 {
246 fprintf(stderr, "%s", 249 fprintf (stderr, "%s",
247 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n"); 250 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
248 goto housekeeping; 251 goto housekeeping;
249 } 252 }
250 cbData.cfgDiffs = cfg; 253 cbData.cfgDiffs = cfg;
251 GNUNET_CONFIGURATION_iterate(cfgDiffs, diffsCallBack, &cbData); 254 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
252 if ((ret = cbData.status) == 1) 255 if ((ret = cbData.status) == 1)
253 fprintf(stderr, "%s", 256 fprintf (stderr, "%s",
254 "Incorrect Configuration Diffs: Data may be missing in diffs\n"); 257 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
255 258
256housekeeping: 259housekeeping:
257#if 0 260#if 0
258 cbData.section = NULL; 261 cbData.section = NULL;
259 cbData.callBackOption = PRINT; 262 cbData.callBackOption = PRINT;
260 printf("\nExpected Diffs:\n"); 263 printf ("\nExpected Diffs:\n");
261 GNUNET_CONFIGURATION_iterate(cfgDiffs, diffsCallBack, &cbData); 264 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
262 cbData.section = NULL; 265 cbData.section = NULL;
263 printf("\nActual Diffs:\n"); 266 printf ("\nActual Diffs:\n");
264 GNUNET_CONFIGURATION_iterate(cfg, diffsCallBack, &cbData); 267 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
265#endif 268#endif
266 GNUNET_CONFIGURATION_destroy(cfg); 269 GNUNET_CONFIGURATION_destroy (cfg);
267 GNUNET_CONFIGURATION_destroy(cfgDiffs); 270 GNUNET_CONFIGURATION_destroy (cfgDiffs);
268 GNUNET_free(diffsFileName); 271 GNUNET_free (diffsFileName);
269 return ret; 272 return ret;
270} 273}
271 274
272 275
273static int 276static int
274testConfig() 277testConfig ()
275{ 278{
276 char *c; 279 char *c;
277 unsigned long long l; 280 unsigned long long l;
278 281
279 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "test", "b", &c)) 282 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
280 return 1; 283 return 1;
281 if (0 != strcmp("b", c)) 284 if (0 != strcmp ("b", c))
282 { 285 {
283 fprintf(stderr, "Got `%s'\n", c); 286 fprintf (stderr, "Got `%s'\n", c);
284 GNUNET_free(c); 287 GNUNET_free (c);
285 return 2; 288 return 2;
286 } 289 }
287 GNUNET_free(c); 290 GNUNET_free (c);
288 if (GNUNET_OK != 291 if (GNUNET_OK !=
289 GNUNET_CONFIGURATION_get_value_number(cfg, "test", "five", &l)) 292 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
290 { 293 {
291 GNUNET_break(0); 294 GNUNET_break (0);
292 return 3; 295 return 3;
293 } 296 }
294 if (5 != l) 297 if (5 != l)
295 { 298 {
296 GNUNET_break(0); 299 GNUNET_break (0);
297 return 4; 300 return 4;
298 } 301 }
299 GNUNET_CONFIGURATION_set_value_string(cfg, "more", "c", "YES"); 302 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
300 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, "more", "c")) 303 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
301 { 304 {
302 GNUNET_break(0); 305 GNUNET_break (0);
303 return 5; 306 return 5;
304 } 307 }
305 GNUNET_CONFIGURATION_set_value_number(cfg, "NUMBERS", "TEN", 10); 308 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
306 if (GNUNET_OK != 309 if (GNUNET_OK !=
307 GNUNET_CONFIGURATION_get_value_string(cfg, "NUMBERS", "TEN", &c)) 310 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
308 { 311 {
309 GNUNET_break(0); 312 GNUNET_break (0);
310 return 6; 313 return 6;
311 } 314 }
312 if (0 != strcmp(c, "10")) 315 if (0 != strcmp (c, "10"))
313 { 316 {
314 GNUNET_free(c); 317 GNUNET_free (c);
315 GNUNET_break(0); 318 GNUNET_break (0);
316 return 7; 319 return 7;
317 } 320 }
318 GNUNET_free(c); 321 GNUNET_free (c);
319 322
320 if (GNUNET_OK != 323 if (GNUNET_OK !=
321 GNUNET_CONFIGURATION_get_value_filename(cfg, "last", "test", &c)) 324 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
322 { 325 {
323 GNUNET_break(0); 326 GNUNET_break (0);
324 return 8; 327 return 8;
325 } 328 }
326 329
327 if (0 != strcmp(c, "/hello/world")) 330 if (0 != strcmp (c, "/hello/world"))
328 { 331 {
329 GNUNET_break(0); 332 GNUNET_break (0);
330 GNUNET_free(c); 333 GNUNET_free (c);
331 return 9; 334 return 9;
332 } 335 }
333 GNUNET_free(c); 336 GNUNET_free (c);
334 337
335 if (GNUNET_OK != 338 if (GNUNET_OK !=
336 GNUNET_CONFIGURATION_get_value_size(cfg, "last", "size", &l)) 339 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
337 { 340 {
338 GNUNET_break(0); 341 GNUNET_break (0);
339 return 10; 342 return 10;
340 } 343 }
341 if (l != 512 * 1024) 344 if (l != 512 * 1024)
342 { 345 {
343 GNUNET_break(0); 346 GNUNET_break (0);
344 return 11; 347 return 11;
345 } 348 }
346 return 0; 349 return 0;
347} 350}
348 351
@@ -355,198 +358,198 @@ static const char *want[] = {
355}; 358};
356 359
357static int 360static int
358check(void *data, const char *fn) 361check (void *data, const char *fn)
359{ 362{
360 int *idx = data; 363 int *idx = data;
361 364
362 if (0 == strcmp(want[*idx], fn)) 365 if (0 == strcmp (want[*idx], fn))
363 { 366 {
364 (*idx)++; 367 (*idx)++;
365 return GNUNET_OK; 368 return GNUNET_OK;
366 } 369 }
367 GNUNET_break(0); 370 GNUNET_break (0);
368 return GNUNET_SYSERR; 371 return GNUNET_SYSERR;
369} 372}
370 373
371static int 374static int
372testConfigFilenames() 375testConfigFilenames ()
373{ 376{
374 int idx; 377 int idx;
375 378
376 idx = 0; 379 idx = 0;
377 if (3 != 380 if (3 !=
378 GNUNET_CONFIGURATION_iterate_value_filenames(cfg, "FILENAMES", "test", 381 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
379 &check, &idx)) 382 &check, &idx))
380 { 383 {
381 GNUNET_break(0); 384 GNUNET_break (0);
382 return 8; 385 return 8;
383 } 386 }
384 if (idx != 3) 387 if (idx != 3)
385 return 16; 388 return 16;
386 if (GNUNET_OK != 389 if (GNUNET_OK !=
387 GNUNET_CONFIGURATION_remove_value_filename(cfg, "FILENAMES", "test", 390 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
388 "/File Name")) 391 "/File Name"))
389 { 392 {
390 GNUNET_break(0); 393 GNUNET_break (0);
391 return 24; 394 return 24;
392 } 395 }
393 396
394 if (GNUNET_NO != 397 if (GNUNET_NO !=
395 GNUNET_CONFIGURATION_remove_value_filename(cfg, "FILENAMES", "test", 398 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
396 "/File Name")) 399 "/File Name"))
397 { 400 {
398 GNUNET_break(0); 401 GNUNET_break (0);
399 return 32; 402 return 32;
400 } 403 }
401 if (GNUNET_NO != 404 if (GNUNET_NO !=
402 GNUNET_CONFIGURATION_remove_value_filename(cfg, "FILENAMES", "test", 405 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
403 "Stuff")) 406 "Stuff"))
404 { 407 {
405 GNUNET_break(0); 408 GNUNET_break (0);
406 return 40; 409 return 40;
407 } 410 }
408 411
409 if (GNUNET_NO != 412 if (GNUNET_NO !=
410 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test", 413 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
411 "/Hello")) 414 "/Hello"))
412 { 415 {
413 GNUNET_break(0); 416 GNUNET_break (0);
414 return 48; 417 return 48;
415 } 418 }
416 if (GNUNET_NO != 419 if (GNUNET_NO !=
417 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test", 420 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
418 "/World")) 421 "/World"))
419 { 422 {
420 GNUNET_break(0); 423 GNUNET_break (0);
421 return 56; 424 return 56;
422 } 425 }
423 426
424 if (GNUNET_YES != 427 if (GNUNET_YES !=
425 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test", 428 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
426 "/File 1")) 429 "/File 1"))
427 { 430 {
428 GNUNET_break(0); 431 GNUNET_break (0);
429 return 64; 432 return 64;
430 } 433 }
431 434
432 if (GNUNET_YES != 435 if (GNUNET_YES !=
433 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test", 436 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
434 "/File 2")) 437 "/File 2"))
435 { 438 {
436 GNUNET_break(0); 439 GNUNET_break (0);
437 return 72; 440 return 72;
438 } 441 }
439 442
440 idx = 0; 443 idx = 0;
441 want[1] = "/World"; 444 want[1] = "/World";
442 want[2] = "/File 1"; 445 want[2] = "/File 1";
443 want[3] = "/File 2"; 446 want[3] = "/File 2";
444 if (4 != 447 if (4 !=
445 GNUNET_CONFIGURATION_iterate_value_filenames(cfg, "FILENAMES", "test", 448 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
446 &check, &idx)) 449 &check, &idx))
447 { 450 {
448 GNUNET_break(0); 451 GNUNET_break (0);
449 return 80; 452 return 80;
450 } 453 }
451 if (idx != 4) 454 if (idx != 4)
452 { 455 {
453 GNUNET_break(0); 456 GNUNET_break (0);
454 return 88; 457 return 88;
455 } 458 }
456 return 0; 459 return 0;
457} 460}
458 461
459 462
460int 463int
461main(int argc, char *argv[]) 464main (int argc, char *argv[])
462{ 465{
463 int failureCount = 0; 466 int failureCount = 0;
464 char *c; 467 char *c;
465 468
466 GNUNET_log_setup("test_configuration", "WARNING", NULL); 469 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
467 cfg = GNUNET_CONFIGURATION_create(); 470 cfg = GNUNET_CONFIGURATION_create ();
468 GNUNET_assert(cfg != NULL); 471 GNUNET_assert (cfg != NULL);
469 if (GNUNET_OK != 472 if (GNUNET_OK !=
470 GNUNET_CONFIGURATION_parse(cfg, "test_configuration_data.conf")) 473 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
471 { 474 {
472 fprintf(stderr, "%s", "Failed to parse configuration file\n"); 475 fprintf (stderr, "%s", "Failed to parse configuration file\n");
473 GNUNET_CONFIGURATION_destroy(cfg); 476 GNUNET_CONFIGURATION_destroy (cfg);
474 return 1; 477 return 1;
475 } 478 }
476 failureCount += testConfig(); 479 failureCount += testConfig ();
477 if (failureCount > 0) 480 if (failureCount > 0)
478 goto error; 481 goto error;
479 482
480 failureCount = testConfigFilenames(); 483 failureCount = testConfigFilenames ();
481 if (failureCount > 0) 484 if (failureCount > 0)
482 goto error; 485 goto error;
483 486
484 if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg, "/tmp/gnunet-test.conf")) 487 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
485 { 488 {
486 fprintf(stderr, "%s", "Failed to write configuration file\n"); 489 fprintf (stderr, "%s", "Failed to write configuration file\n");
487 GNUNET_CONFIGURATION_destroy(cfg); 490 GNUNET_CONFIGURATION_destroy (cfg);
488 return 1; 491 return 1;
489 } 492 }
490 GNUNET_CONFIGURATION_destroy(cfg); 493 GNUNET_CONFIGURATION_destroy (cfg);
491 GNUNET_assert(0 == unlink("/tmp/gnunet-test.conf")); 494 GNUNET_assert (0 == unlink ("/tmp/gnunet-test.conf"));
492 495
493 cfg = GNUNET_CONFIGURATION_create(); 496 cfg = GNUNET_CONFIGURATION_create ();
494 if (GNUNET_OK != 497 if (GNUNET_OK !=
495 GNUNET_CONFIGURATION_load(cfg, "test_configuration_data.conf")) 498 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
496 { 499 {
497 GNUNET_break(0); 500 GNUNET_break (0);
498 GNUNET_CONFIGURATION_destroy(cfg); 501 GNUNET_CONFIGURATION_destroy (cfg);
499 return 1; 502 return 1;
500 } 503 }
501 if (GNUNET_OK != 504 if (GNUNET_OK !=
502 GNUNET_CONFIGURATION_get_value_string(cfg, "TESTING", "WEAKRANDOM", &c)) 505 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
503 { 506 {
504 GNUNET_break(0); 507 GNUNET_break (0);
505 GNUNET_CONFIGURATION_destroy(cfg); 508 GNUNET_CONFIGURATION_destroy (cfg);
506 return 1; 509 return 1;
507 } 510 }
508 if (0 != strcmp(c, "YES")) 511 if (0 != strcmp (c, "YES"))
509 { 512 {
510 GNUNET_break(0); 513 GNUNET_break (0);
511 GNUNET_free(c); 514 GNUNET_free (c);
512 GNUNET_CONFIGURATION_destroy(cfg); 515 GNUNET_CONFIGURATION_destroy (cfg);
513 return 1; 516 return 1;
514 } 517 }
515 518
516 GNUNET_free(c); 519 GNUNET_free (c);
517 GNUNET_CONFIGURATION_destroy(cfg); 520 GNUNET_CONFIGURATION_destroy (cfg);
518 521
519 /* Testing configuration diffs */ 522 /* Testing configuration diffs */
520 cfg_default = GNUNET_CONFIGURATION_create(); 523 cfg_default = GNUNET_CONFIGURATION_create ();
521 if (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_default, NULL)) 524 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_default, NULL))
522 { 525 {
523 GNUNET_break(0); 526 GNUNET_break (0);
524 GNUNET_CONFIGURATION_destroy(cfg_default); 527 GNUNET_CONFIGURATION_destroy (cfg_default);
525 return 1; 528 return 1;
526 } 529 }
527 530
528 /* Nothing changed in the new configuration */ 531 /* Nothing changed in the new configuration */
529 failureCount += checkDiffs(cfg_default, EDIT_NOTHING); 532 failureCount += checkDiffs (cfg_default, EDIT_NOTHING);
530 533
531 /* Modify all entries of the last section */ 534 /* Modify all entries of the last section */
532 failureCount += checkDiffs(cfg_default, EDIT_SECTION); 535 failureCount += checkDiffs (cfg_default, EDIT_SECTION);
533 536
534 /* Add a new section */ 537 /* Add a new section */
535 failureCount += checkDiffs(cfg_default, ADD_NEW_SECTION); 538 failureCount += checkDiffs (cfg_default, ADD_NEW_SECTION);
536 539
537 /* Add a new entry to the last section */ 540 /* Add a new entry to the last section */
538 failureCount += checkDiffs(cfg_default, ADD_NEW_ENTRY); 541 failureCount += checkDiffs (cfg_default, ADD_NEW_ENTRY);
539 542
540 /* Modify all entries in the configuration */ 543 /* Modify all entries in the configuration */
541 failureCount += checkDiffs(cfg_default, EDIT_ALL); 544 failureCount += checkDiffs (cfg_default, EDIT_ALL);
542 545
543 GNUNET_CONFIGURATION_destroy(cfg_default); 546 GNUNET_CONFIGURATION_destroy (cfg_default);
544 547
545error: 548error:
546 if (failureCount != 0) 549 if (failureCount != 0)
547 { 550 {
548 fprintf(stderr, "Test failed: %u\n", failureCount); 551 fprintf (stderr, "Test failed: %u\n", failureCount);
549 return 1; 552 return 1;
550 } 553 }
551 return 0; 554 return 0;
552} 555}
diff --git a/src/util/test_container_bloomfilter.c b/src/util/test_container_bloomfilter.c
index d295f465b..0217bad44 100644
--- a/src/util/test_container_bloomfilter.c
+++ b/src/util/test_container_bloomfilter.c
@@ -35,26 +35,26 @@
35 * Generate a random hashcode. 35 * Generate a random hashcode.
36 */ 36 */
37static void 37static void
38nextHC(struct GNUNET_HashCode *hc) 38nextHC (struct GNUNET_HashCode *hc)
39{ 39{
40 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, hc); 40 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, hc);
41} 41}
42 42
43static int 43static int
44add_iterator(void *cls, struct GNUNET_HashCode *next) 44add_iterator (void *cls, struct GNUNET_HashCode *next)
45{ 45{
46 int *ret = cls; 46 int *ret = cls;
47 struct GNUNET_HashCode pos; 47 struct GNUNET_HashCode pos;
48 48
49 if (0 == (*ret)--) 49 if (0 == (*ret)--)
50 return GNUNET_NO; 50 return GNUNET_NO;
51 nextHC(&pos); 51 nextHC (&pos);
52 *next = pos; 52 *next = pos;
53 return GNUNET_YES; 53 return GNUNET_YES;
54} 54}
55 55
56int 56int
57main(int argc, char *argv[]) 57main (int argc, char *argv[])
58{ 58{
59 struct GNUNET_CONTAINER_BloomFilter *bf; 59 struct GNUNET_CONTAINER_BloomFilter *bf;
60 struct GNUNET_CONTAINER_BloomFilter *bfi; 60 struct GNUNET_CONTAINER_BloomFilter *bfi;
@@ -66,186 +66,186 @@ main(int argc, char *argv[])
66 char buf[SIZE]; 66 char buf[SIZE];
67 struct stat sbuf; 67 struct stat sbuf;
68 68
69 GNUNET_log_setup("test-container-bloomfilter", "WARNING", NULL); 69 GNUNET_log_setup ("test-container-bloomfilter", "WARNING", NULL);
70 GNUNET_CRYPTO_seed_weak_random(1); 70 GNUNET_CRYPTO_seed_weak_random (1);
71 if (0 == stat(TESTFILE, &sbuf)) 71 if (0 == stat (TESTFILE, &sbuf))
72 if (0 != unlink(TESTFILE)) 72 if (0 != unlink (TESTFILE))
73 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "unlink", TESTFILE); 73 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", TESTFILE);
74 bf = GNUNET_CONTAINER_bloomfilter_load(TESTFILE, SIZE, K); 74 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K);
75 75
76 for (i = 0; i < 200; i++) 76 for (i = 0; i < 200; i++)
77 { 77 {
78 nextHC(&tmp); 78 nextHC (&tmp);
79 GNUNET_CONTAINER_bloomfilter_add(bf, &tmp); 79 GNUNET_CONTAINER_bloomfilter_add (bf, &tmp);
80 } 80 }
81 GNUNET_CRYPTO_seed_weak_random(1); 81 GNUNET_CRYPTO_seed_weak_random (1);
82 ok1 = 0; 82 ok1 = 0;
83 for (i = 0; i < 200; i++) 83 for (i = 0; i < 200; i++)
84 { 84 {
85 nextHC(&tmp); 85 nextHC (&tmp);
86 if (GNUNET_CONTAINER_bloomfilter_test(bf, &tmp) == GNUNET_YES) 86 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
87 ok1++; 87 ok1++;
88 } 88 }
89 if (ok1 != 200) 89 if (ok1 != 200)
90 { 90 {
91 printf("Got %d elements out of" 91 printf ("Got %d elements out of"
92 "200 expected after insertion.\n", 92 "200 expected after insertion.\n",
93 ok1); 93 ok1);
94 GNUNET_CONTAINER_bloomfilter_free(bf); 94 GNUNET_CONTAINER_bloomfilter_free (bf);
95 return -1; 95 return -1;
96 } 96 }
97 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data(bf, buf, SIZE)) 97 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, buf, SIZE))
98 { 98 {
99 GNUNET_CONTAINER_bloomfilter_free(bf); 99 GNUNET_CONTAINER_bloomfilter_free (bf);
100 return -1; 100 return -1;
101 } 101 }
102 102
103 GNUNET_CONTAINER_bloomfilter_free(bf); 103 GNUNET_CONTAINER_bloomfilter_free (bf);
104 104
105 bf = GNUNET_CONTAINER_bloomfilter_load(TESTFILE, SIZE, K); 105 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K);
106 GNUNET_assert(bf != NULL); 106 GNUNET_assert (bf != NULL);
107 bfi = GNUNET_CONTAINER_bloomfilter_init(buf, SIZE, K); 107 bfi = GNUNET_CONTAINER_bloomfilter_init (buf, SIZE, K);
108 GNUNET_assert(bfi != NULL); 108 GNUNET_assert (bfi != NULL);
109 109
110 GNUNET_CRYPTO_seed_weak_random(1); 110 GNUNET_CRYPTO_seed_weak_random (1);
111 ok1 = 0; 111 ok1 = 0;
112 ok2 = 0; 112 ok2 = 0;
113 for (i = 0; i < 200; i++) 113 for (i = 0; i < 200; i++)
114 { 114 {
115 nextHC(&tmp); 115 nextHC (&tmp);
116 if (GNUNET_CONTAINER_bloomfilter_test(bf, &tmp) == GNUNET_YES) 116 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
117 ok1++; 117 ok1++;
118 if (GNUNET_CONTAINER_bloomfilter_test(bfi, &tmp) == GNUNET_YES) 118 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
119 ok2++; 119 ok2++;
120 } 120 }
121 if (ok1 != 200) 121 if (ok1 != 200)
122 { 122 {
123 printf("Got %d elements out of 200 " 123 printf ("Got %d elements out of 200 "
124 "expected after reloading.\n", 124 "expected after reloading.\n",
125 ok1); 125 ok1);
126 GNUNET_CONTAINER_bloomfilter_free(bf); 126 GNUNET_CONTAINER_bloomfilter_free (bf);
127 GNUNET_CONTAINER_bloomfilter_free(bfi); 127 GNUNET_CONTAINER_bloomfilter_free (bfi);
128 return -1; 128 return -1;
129 } 129 }
130 130
131 if (ok2 != 200) 131 if (ok2 != 200)
132 { 132 {
133 printf("Got %d elements out of 200 " 133 printf ("Got %d elements out of 200 "
134 "expected after initialization.\n", 134 "expected after initialization.\n",
135 ok2); 135 ok2);
136 GNUNET_CONTAINER_bloomfilter_free(bf); 136 GNUNET_CONTAINER_bloomfilter_free (bf);
137 GNUNET_CONTAINER_bloomfilter_free(bfi); 137 GNUNET_CONTAINER_bloomfilter_free (bfi);
138 return -1; 138 return -1;
139 } 139 }
140 140
141 GNUNET_CRYPTO_seed_weak_random(1); 141 GNUNET_CRYPTO_seed_weak_random (1);
142 for (i = 0; i < 100; i++) 142 for (i = 0; i < 100; i++)
143 { 143 {
144 nextHC(&tmp); 144 nextHC (&tmp);
145 GNUNET_CONTAINER_bloomfilter_remove(bf, &tmp); 145 GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp);
146 GNUNET_CONTAINER_bloomfilter_remove(bfi, &tmp); 146 GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp);
147 } 147 }
148 148
149 GNUNET_CRYPTO_seed_weak_random(1); 149 GNUNET_CRYPTO_seed_weak_random (1);
150 150
151 ok1 = 0; 151 ok1 = 0;
152 ok2 = 0; 152 ok2 = 0;
153 for (i = 0; i < 200; i++) 153 for (i = 0; i < 200; i++)
154 { 154 {
155 nextHC(&tmp); 155 nextHC (&tmp);
156 if (GNUNET_CONTAINER_bloomfilter_test(bf, &tmp) == GNUNET_YES) 156 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
157 ok1++; 157 ok1++;
158 if (GNUNET_CONTAINER_bloomfilter_test(bfi, &tmp) == GNUNET_YES) 158 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
159 ok2++; 159 ok2++;
160 } 160 }
161 161
162 if (ok1 != 100) 162 if (ok1 != 100)
163 { 163 {
164 printf("Expected 100 elements in loaded filter" 164 printf ("Expected 100 elements in loaded filter"
165 " after adding 200 and deleting 100, got %d\n", 165 " after adding 200 and deleting 100, got %d\n",
166 ok1); 166 ok1);
167 GNUNET_CONTAINER_bloomfilter_free(bf); 167 GNUNET_CONTAINER_bloomfilter_free (bf);
168 GNUNET_CONTAINER_bloomfilter_free(bfi); 168 GNUNET_CONTAINER_bloomfilter_free (bfi);
169 return -1; 169 return -1;
170 } 170 }
171 if (ok2 != 200) 171 if (ok2 != 200)
172 { 172 {
173 printf("Expected 200 elements in initialized filter" 173 printf ("Expected 200 elements in initialized filter"
174 " after adding 200 and deleting 100 " 174 " after adding 200 and deleting 100 "
175 "(which should do nothing for a filter not backed by a file), got %d\n", 175 "(which should do nothing for a filter not backed by a file), got %d\n",
176 ok2); 176 ok2);
177 GNUNET_CONTAINER_bloomfilter_free(bf); 177 GNUNET_CONTAINER_bloomfilter_free (bf);
178 GNUNET_CONTAINER_bloomfilter_free(bfi); 178 GNUNET_CONTAINER_bloomfilter_free (bfi);
179 return -1; 179 return -1;
180 } 180 }
181 181
182 GNUNET_CRYPTO_seed_weak_random(3); 182 GNUNET_CRYPTO_seed_weak_random (3);
183 183
184 GNUNET_CONTAINER_bloomfilter_clear(bf); 184 GNUNET_CONTAINER_bloomfilter_clear (bf);
185 falseok = 0; 185 falseok = 0;
186 for (i = 0; i < 1000; i++) 186 for (i = 0; i < 1000; i++)
187 { 187 {
188 nextHC(&tmp); 188 nextHC (&tmp);
189 if (GNUNET_CONTAINER_bloomfilter_test(bf, &tmp) == GNUNET_YES) 189 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
190 falseok++; 190 falseok++;
191 } 191 }
192 if (falseok > 0) 192 if (falseok > 0)
193 { 193 {
194 GNUNET_CONTAINER_bloomfilter_free(bf); 194 GNUNET_CONTAINER_bloomfilter_free (bf);
195 GNUNET_CONTAINER_bloomfilter_free(bfi); 195 GNUNET_CONTAINER_bloomfilter_free (bfi);
196 return -1; 196 return -1;
197 } 197 }
198 198
199 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or(bf, buf, SIZE)) 199 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or (bf, buf, SIZE))
200 { 200 {
201 GNUNET_CONTAINER_bloomfilter_free(bf); 201 GNUNET_CONTAINER_bloomfilter_free (bf);
202 GNUNET_CONTAINER_bloomfilter_free(bfi); 202 GNUNET_CONTAINER_bloomfilter_free (bfi);
203 return -1; 203 return -1;
204 } 204 }
205 205
206 GNUNET_CRYPTO_seed_weak_random(2); 206 GNUNET_CRYPTO_seed_weak_random (2);
207 i = 20; 207 i = 20;
208 GNUNET_CONTAINER_bloomfilter_resize(bfi, &add_iterator, &i, SIZE * 2, K); 208 GNUNET_CONTAINER_bloomfilter_resize (bfi, &add_iterator, &i, SIZE * 2, K);
209 209
210 GNUNET_CRYPTO_seed_weak_random(2); 210 GNUNET_CRYPTO_seed_weak_random (2);
211 i = 20; 211 i = 20;
212 GNUNET_CONTAINER_bloomfilter_resize(bf, &add_iterator, &i, SIZE * 2, K); 212 GNUNET_CONTAINER_bloomfilter_resize (bf, &add_iterator, &i, SIZE * 2, K);
213 GNUNET_CRYPTO_seed_weak_random(2); 213 GNUNET_CRYPTO_seed_weak_random (2);
214 214
215 ok1 = 0; 215 ok1 = 0;
216 ok2 = 0; 216 ok2 = 0;
217 for (i = 0; i < 20; i++) 217 for (i = 0; i < 20; i++)
218 { 218 {
219 nextHC(&tmp); 219 nextHC (&tmp);
220 if (GNUNET_CONTAINER_bloomfilter_test(bf, &tmp) == GNUNET_YES) 220 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
221 ok1++; 221 ok1++;
222 if (GNUNET_CONTAINER_bloomfilter_test(bfi, &tmp) == GNUNET_YES) 222 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
223 ok2++; 223 ok2++;
224 } 224 }
225 225
226 if (ok1 != 20) 226 if (ok1 != 20)
227 { 227 {
228 printf("Expected 20 elements in resized file-backed filter" 228 printf ("Expected 20 elements in resized file-backed filter"
229 " after adding 20, got %d\n", 229 " after adding 20, got %d\n",
230 ok1); 230 ok1);
231 GNUNET_CONTAINER_bloomfilter_free(bf); 231 GNUNET_CONTAINER_bloomfilter_free (bf);
232 GNUNET_CONTAINER_bloomfilter_free(bfi); 232 GNUNET_CONTAINER_bloomfilter_free (bfi);
233 return -1; 233 return -1;
234 } 234 }
235 if (ok2 != 20) 235 if (ok2 != 20)
236 { 236 {
237 printf("Expected 20 elements in resized filter" 237 printf ("Expected 20 elements in resized filter"
238 " after adding 20, got %d\n", 238 " after adding 20, got %d\n",
239 ok2); 239 ok2);
240 GNUNET_CONTAINER_bloomfilter_free(bf); 240 GNUNET_CONTAINER_bloomfilter_free (bf);
241 GNUNET_CONTAINER_bloomfilter_free(bfi); 241 GNUNET_CONTAINER_bloomfilter_free (bfi);
242 return -1; 242 return -1;
243 } 243 }
244 244
245 245
246 GNUNET_CONTAINER_bloomfilter_free(bf); 246 GNUNET_CONTAINER_bloomfilter_free (bf);
247 GNUNET_CONTAINER_bloomfilter_free(bfi); 247 GNUNET_CONTAINER_bloomfilter_free (bfi);
248 248
249 GNUNET_break(0 == unlink(TESTFILE)); 249 GNUNET_break (0 == unlink (TESTFILE));
250 return 0; 250 return 0;
251} 251}
diff --git a/src/util/test_container_dll.c b/src/util/test_container_dll.c
index 7203cde22..b5c238511 100644
--- a/src/util/test_container_dll.c
+++ b/src/util/test_container_dll.c
@@ -30,7 +30,8 @@
30/** 30/**
31 * Element in the DLL. 31 * Element in the DLL.
32 */ 32 */
33struct Element { 33struct Element
34{
34 /** 35 /**
35 * Required pointer to previous element. 36 * Required pointer to previous element.
36 */ 37 */
@@ -57,9 +58,9 @@ struct Element {
57 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO 58 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
58 */ 59 */
59static int 60static int
60cmp_elem(void *cls, 61cmp_elem (void *cls,
61 struct Element *e1, 62 struct Element *e1,
62 struct Element *e2) 63 struct Element *e2)
63{ 64{
64 if (e1->value == e2->value) 65 if (e1->value == e2->value)
65 return 0; 66 return 0;
@@ -68,7 +69,7 @@ cmp_elem(void *cls,
68 69
69 70
70int 71int
71main(int argc, char **argv) 72main (int argc, char **argv)
72{ 73{
73 unsigned int values[] = { 74 unsigned int values[] = {
74 4, 5, 8, 6, 9, 3, 7, 2, 1, 0 75 4, 5, 8, 6, 9, 3, 7, 2, 1, 0
@@ -78,33 +79,33 @@ main(int argc, char **argv)
78 struct Element *e; 79 struct Element *e;
79 unsigned int want; 80 unsigned int want;
80 81
81 GNUNET_log_setup("test-container-dll", 82 GNUNET_log_setup ("test-container-dll",
82 "WARNING", 83 "WARNING",
83 NULL); 84 NULL);
84 for (unsigned int off = 0; 85 for (unsigned int off = 0;
85 0 != values[off]; 86 0 != values[off];
86 off++) 87 off++)
87 { 88 {
88 e = GNUNET_new(struct Element); 89 e = GNUNET_new (struct Element);
89 e->value = values[off]; 90 e->value = values[off];
90 GNUNET_CONTAINER_DLL_insert_sorted(struct Element, 91 GNUNET_CONTAINER_DLL_insert_sorted (struct Element,
91 cmp_elem, 92 cmp_elem,
92 NULL, 93 NULL,
93 head, 94 head,
94 tail, 95 tail,
95 e); 96 e);
96 } 97 }
97 98
98 want = 1; 99 want = 1;
99 while (NULL != (e = head)) 100 while (NULL != (e = head))
100 { 101 {
101 GNUNET_assert(e->value == want); 102 GNUNET_assert (e->value == want);
102 GNUNET_CONTAINER_DLL_remove(head, 103 GNUNET_CONTAINER_DLL_remove (head,
103 tail, 104 tail,
104 e); 105 e);
105 GNUNET_free(e); 106 GNUNET_free (e);
106 want++; 107 want++;
107 } 108 }
108 return 0; 109 return 0;
109} 110}
110 111
diff --git a/src/util/test_container_heap.c b/src/util/test_container_heap.c
index 8df6553b6..2530f3dbc 100644
--- a/src/util/test_container_heap.c
+++ b/src/util/test_container_heap.c
@@ -28,23 +28,23 @@
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30static int 30static int
31iterator_callback(void *cls, 31iterator_callback (void *cls,
32 struct GNUNET_CONTAINER_HeapNode *node, 32 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}
37 37
38static int 38static int
39nstrcmp(const char *a, const char *b) 39nstrcmp (const char *a, const char *b)
40{ 40{
41 GNUNET_assert(a != NULL); 41 GNUNET_assert (a != NULL);
42 GNUNET_assert(b != NULL); 42 GNUNET_assert (b != NULL);
43 return strcmp(a, b); 43 return strcmp (a, b);
44} 44}
45 45
46static int 46static int
47check() 47check ()
48{ 48{
49 struct GNUNET_CONTAINER_Heap *myHeap; 49 struct GNUNET_CONTAINER_Heap *myHeap;
50 struct GNUNET_CONTAINER_HeapNode *n1; 50 struct GNUNET_CONTAINER_HeapNode *n1;
@@ -57,234 +57,234 @@ check()
57 struct GNUNET_CONTAINER_HeapNode *n8; 57 struct GNUNET_CONTAINER_HeapNode *n8;
58 const char *r; 58 const char *r;
59 59
60 myHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN); 60 myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
61 61
62 // GNUNET_CONTAINER_heap_remove_root heap empty, taking if-branch 62 // GNUNET_CONTAINER_heap_remove_root heap empty, taking if-branch
63 n1 = GNUNET_CONTAINER_heap_remove_root(myHeap); 63 n1 = GNUNET_CONTAINER_heap_remove_root (myHeap);
64 GNUNET_assert(NULL == n1); 64 GNUNET_assert (NULL == n1);
65 65
66 // GNUNET_CONTAINER_heap_peek heap empty, taking if-branch 66 // GNUNET_CONTAINER_heap_peek heap empty, taking if-branch
67 n1 = GNUNET_CONTAINER_heap_peek(myHeap); 67 n1 = GNUNET_CONTAINER_heap_peek (myHeap);
68 GNUNET_assert(NULL == n1); 68 GNUNET_assert (NULL == n1);
69 69
70 // GNUNET_CONTAINER_heap_walk_get_next: heap empty, taking if-branch 70 // GNUNET_CONTAINER_heap_walk_get_next: heap empty, taking if-branch
71 n1 = GNUNET_CONTAINER_heap_walk_get_next(myHeap); 71 n1 = GNUNET_CONTAINER_heap_walk_get_next (myHeap);
72 GNUNET_assert(NULL == n1); 72 GNUNET_assert (NULL == n1);
73 73
74 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "11", 11); 74 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "11", 11);
75 GNUNET_assert(NULL != n1); 75 GNUNET_assert (NULL != n1);
76 76
77 77
78 // GNUNET_CONTAINER_heap_peek not empty, taking if-branch 78 // GNUNET_CONTAINER_heap_peek not empty, taking if-branch
79 n2 = NULL; 79 n2 = NULL;
80 n2 = GNUNET_CONTAINER_heap_peek(myHeap); 80 n2 = GNUNET_CONTAINER_heap_peek (myHeap);
81 GNUNET_assert(NULL != n2); 81 GNUNET_assert (NULL != n2);
82 82
83 // GNUNET_CONTAINER_heap_walk_get_next: 1 element 83 // GNUNET_CONTAINER_heap_walk_get_next: 1 element
84 n1 = NULL; 84 n1 = NULL;
85 n1 = GNUNET_CONTAINER_heap_walk_get_next(myHeap); 85 n1 = GNUNET_CONTAINER_heap_walk_get_next (myHeap);
86 GNUNET_assert(NULL != n1); 86 GNUNET_assert (NULL != n1);
87 87
88 GNUNET_CONTAINER_heap_iterate(myHeap, &iterator_callback, NULL); 88 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
89 GNUNET_assert(1 == GNUNET_CONTAINER_heap_get_size(myHeap)); 89 GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
90 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "78", 78); 90 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "78", 78);
91 GNUNET_assert(2 == GNUNET_CONTAINER_heap_get_size(myHeap)); 91 GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
92 GNUNET_assert(0 == strcmp("78", GNUNET_CONTAINER_heap_remove_node(n2))); 92 GNUNET_assert (0 == strcmp ("78", GNUNET_CONTAINER_heap_remove_node (n2)));
93 GNUNET_assert(1 == GNUNET_CONTAINER_heap_get_size(myHeap)); 93 GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
94 GNUNET_CONTAINER_heap_iterate(myHeap, &iterator_callback, NULL); 94 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
95 95
96 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "15", 5); 96 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "15", 5);
97 GNUNET_CONTAINER_heap_update_cost(n3, 15); 97 GNUNET_CONTAINER_heap_update_cost (n3, 15);
98 GNUNET_assert(2 == GNUNET_CONTAINER_heap_get_size(myHeap)); 98 GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
99 GNUNET_CONTAINER_heap_iterate(myHeap, &iterator_callback, NULL); 99 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
100 100
101 n4 = GNUNET_CONTAINER_heap_insert(myHeap, "50", 50); 101 n4 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
102 GNUNET_CONTAINER_heap_update_cost(n4, 50); 102 GNUNET_CONTAINER_heap_update_cost (n4, 50);
103 GNUNET_assert(3 == GNUNET_CONTAINER_heap_get_size(myHeap)); 103 GNUNET_assert (3 == GNUNET_CONTAINER_heap_get_size (myHeap));
104 GNUNET_CONTAINER_heap_iterate(myHeap, &iterator_callback, NULL); 104 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
105 105
106 n5 = GNUNET_CONTAINER_heap_insert(myHeap, "100", 100); 106 n5 = GNUNET_CONTAINER_heap_insert (myHeap, "100", 100);
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(n6, 200); 113 GNUNET_CONTAINER_heap_update_cost (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));
122 122
123 GNUNET_CONTAINER_heap_destroy(myHeap); 123 GNUNET_CONTAINER_heap_destroy (myHeap);
124 124
125 // My additions to a complete testcase 125 // My additions to a complete testcase
126 // Testing a GNUNET_CONTAINER_HEAP_ORDER_MIN 126 // Testing a GNUNET_CONTAINER_HEAP_ORDER_MIN
127 // Testing remove_node 127 // Testing remove_node
128 128
129 myHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN); 129 myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
130 130
131 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 131 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
132 GNUNET_CONTAINER_heap_update_cost(n1, 15); 132 GNUNET_CONTAINER_heap_update_cost (n1, 15);
133 133
134 r = GNUNET_CONTAINER_heap_remove_node(n1); 134 r = GNUNET_CONTAINER_heap_remove_node (n1);
135 GNUNET_assert(NULL != r); 135 GNUNET_assert (NULL != r);
136 GNUNET_assert(0 == strcmp("10", r)); 136 GNUNET_assert (0 == strcmp ("10", r));
137 137
138 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 138 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
139 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "20", 10); 139 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
140 140
141 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 141 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
142 r = GNUNET_CONTAINER_heap_remove_node(n2); 142 r = GNUNET_CONTAINER_heap_remove_node (n2);
143 GNUNET_assert(NULL != r); 143 GNUNET_assert (NULL != r);
144 GNUNET_assert(0 == strcmp("20", r)); 144 GNUNET_assert (0 == strcmp ("20", r));
145 r = GNUNET_CONTAINER_heap_remove_node(n1); 145 r = GNUNET_CONTAINER_heap_remove_node (n1);
146 GNUNET_assert(NULL != r); 146 GNUNET_assert (NULL != r);
147 GNUNET_assert(0 == strcmp("10", r)); 147 GNUNET_assert (0 == strcmp ("10", r));
148 148
149 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 149 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
150 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "20", 10); 150 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
151 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "30", 10); 151 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
152 152
153 GNUNET_CONTAINER_heap_remove_node(n2); 153 GNUNET_CONTAINER_heap_remove_node (n2);
154 GNUNET_CONTAINER_heap_remove_node(n1); 154 GNUNET_CONTAINER_heap_remove_node (n1);
155 r = GNUNET_CONTAINER_heap_remove_root(myHeap); 155 r = GNUNET_CONTAINER_heap_remove_root (myHeap);
156 GNUNET_assert(NULL != r); 156 GNUNET_assert (NULL != r);
157 GNUNET_assert(0 == strcmp("30", r)); 157 GNUNET_assert (0 == strcmp ("30", r));
158 158
159 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 159 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
160 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "20", 10); 160 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
161 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "30", 10); 161 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
162 162
163 GNUNET_CONTAINER_heap_remove_node(n2); 163 GNUNET_CONTAINER_heap_remove_node (n2);
164 GNUNET_CONTAINER_heap_remove_node(n1); 164 GNUNET_CONTAINER_heap_remove_node (n1);
165 r = GNUNET_CONTAINER_heap_remove_node(n3); 165 r = GNUNET_CONTAINER_heap_remove_node (n3);
166 GNUNET_assert(NULL != r); 166 GNUNET_assert (NULL != r);
167 GNUNET_assert(0 == strcmp("30", r)); 167 GNUNET_assert (0 == strcmp ("30", r));
168 168
169 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 169 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
170 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "20", 20); 170 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20);
171 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "30", 30); 171 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 30);
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);
181 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "30", 30); 181 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 30);
182 n4 = GNUNET_CONTAINER_heap_insert(myHeap, "40", 40); 182 n4 = GNUNET_CONTAINER_heap_insert (myHeap, "40", 40);
183 n5 = GNUNET_CONTAINER_heap_insert(myHeap, "50", 50); 183 n5 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
184 n6 = GNUNET_CONTAINER_heap_insert(myHeap, "60", 60); 184 n6 = GNUNET_CONTAINER_heap_insert (myHeap, "60", 60);
185 185
186 // Inserting nodes deeper in the tree with lower costs 186 // Inserting nodes deeper in the tree with lower costs
187 n7 = GNUNET_CONTAINER_heap_insert(myHeap, "70", 10); 187 n7 = GNUNET_CONTAINER_heap_insert (myHeap, "70", 10);
188 n8 = GNUNET_CONTAINER_heap_insert(myHeap, "80", 10); 188 n8 = GNUNET_CONTAINER_heap_insert (myHeap, "80", 10);
189 189
190 GNUNET_assert(0 == nstrcmp("30", GNUNET_CONTAINER_heap_remove_node(n3))); 190 GNUNET_assert (0 == nstrcmp ("30", GNUNET_CONTAINER_heap_remove_node (n3)));
191 191
192 // Cleaning up... 192 // Cleaning up...
193 GNUNET_assert(0 == nstrcmp("60", GNUNET_CONTAINER_heap_remove_node(n6))); 193 GNUNET_assert (0 == nstrcmp ("60", GNUNET_CONTAINER_heap_remove_node (n6)));
194 GNUNET_assert(0 == nstrcmp("50", GNUNET_CONTAINER_heap_remove_node(n5))); 194 GNUNET_assert (0 == nstrcmp ("50", GNUNET_CONTAINER_heap_remove_node (n5)));
195 195
196 // Testing heap_walk_get_next 196 // Testing heap_walk_get_next
197 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 197 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
198 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 198 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
199 GNUNET_CONTAINER_heap_walk_get_next(myHeap);; 199 GNUNET_CONTAINER_heap_walk_get_next (myHeap);;
200 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 200 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
201 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 201 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
202 202
203 GNUNET_assert(0 == nstrcmp("10", GNUNET_CONTAINER_heap_remove_node(n1))); 203 GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
204 GNUNET_assert(0 == nstrcmp("20", GNUNET_CONTAINER_heap_remove_node(n2))); 204 GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
205 GNUNET_assert(0 == nstrcmp("40", GNUNET_CONTAINER_heap_remove_node(n4))); 205 GNUNET_assert (0 == nstrcmp ("40", GNUNET_CONTAINER_heap_remove_node (n4)));
206 GNUNET_assert(0 == nstrcmp("70", GNUNET_CONTAINER_heap_remove_node(n7))); 206 GNUNET_assert (0 == nstrcmp ("70", GNUNET_CONTAINER_heap_remove_node (n7)));
207 GNUNET_assert(0 == nstrcmp("80", GNUNET_CONTAINER_heap_remove_node(n8))); 207 GNUNET_assert (0 == nstrcmp ("80", GNUNET_CONTAINER_heap_remove_node (n8)));
208 208
209 // End Testing remove_node 209 // End Testing remove_node
210 210
211 // Testing a GNUNET_CONTAINER_HEAP_ORDER_MAX 211 // Testing a GNUNET_CONTAINER_HEAP_ORDER_MAX
212 GNUNET_CONTAINER_heap_destroy(myHeap); 212 GNUNET_CONTAINER_heap_destroy (myHeap);
213 213
214 myHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MAX); 214 myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
215 215
216 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 216 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
217 GNUNET_CONTAINER_heap_update_cost(n1, 15); 217 GNUNET_CONTAINER_heap_update_cost (n1, 15);
218 218
219 GNUNET_assert(0 == nstrcmp("10", GNUNET_CONTAINER_heap_remove_node(n1))); 219 GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
220 220
221 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 221 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
222 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "20", 10); 222 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
223 223
224 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 224 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
225 GNUNET_assert(0 == nstrcmp("20", GNUNET_CONTAINER_heap_remove_node(n2))); 225 GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
226 GNUNET_assert(0 == nstrcmp("10", GNUNET_CONTAINER_heap_remove_node(n1))); 226 GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
227 227
228 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 228 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
229 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "20", 10); 229 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
230 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "30", 10); 230 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
231 231
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);
239 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "30", 10); 239 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
240 240
241 GNUNET_CONTAINER_heap_remove_node(n2); 241 GNUNET_CONTAINER_heap_remove_node (n2);
242 GNUNET_CONTAINER_heap_remove_node(n1); 242 GNUNET_CONTAINER_heap_remove_node (n1);
243 GNUNET_assert(0 == nstrcmp("30", GNUNET_CONTAINER_heap_remove_node(n3))); 243 GNUNET_assert (0 == nstrcmp ("30", GNUNET_CONTAINER_heap_remove_node (n3)));
244 244
245 n1 = GNUNET_CONTAINER_heap_insert(myHeap, "10", 10); 245 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
246 n2 = GNUNET_CONTAINER_heap_insert(myHeap, "20", 20); 246 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20);
247 n3 = GNUNET_CONTAINER_heap_insert(myHeap, "30", 30); 247 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 30);
248 n4 = GNUNET_CONTAINER_heap_insert(myHeap, "40", 40); 248 n4 = GNUNET_CONTAINER_heap_insert (myHeap, "40", 40);
249 n5 = GNUNET_CONTAINER_heap_insert(myHeap, "50", 50); 249 n5 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
250 n6 = GNUNET_CONTAINER_heap_insert(myHeap, "60", 60); 250 n6 = GNUNET_CONTAINER_heap_insert (myHeap, "60", 60);
251 251
252 // Inserting nodes deeper in the tree with lower costs 252 // Inserting nodes deeper in the tree with lower costs
253 n7 = GNUNET_CONTAINER_heap_insert(myHeap, "70", 10); 253 n7 = GNUNET_CONTAINER_heap_insert (myHeap, "70", 10);
254 n8 = GNUNET_CONTAINER_heap_insert(myHeap, "80", 10); 254 n8 = GNUNET_CONTAINER_heap_insert (myHeap, "80", 10);
255 255
256 GNUNET_assert(0 == nstrcmp("30", GNUNET_CONTAINER_heap_remove_node(n3))); 256 GNUNET_assert (0 == nstrcmp ("30", GNUNET_CONTAINER_heap_remove_node (n3)));
257 257
258 // Cleaning up... 258 // Cleaning up...
259 GNUNET_assert(0 == nstrcmp("60", GNUNET_CONTAINER_heap_remove_node(n6))); 259 GNUNET_assert (0 == nstrcmp ("60", GNUNET_CONTAINER_heap_remove_node (n6)));
260 GNUNET_assert(0 == nstrcmp("50", GNUNET_CONTAINER_heap_remove_node(n5))); 260 GNUNET_assert (0 == nstrcmp ("50", GNUNET_CONTAINER_heap_remove_node (n5)));
261 261
262 // Testing heap_walk_get_next 262 // Testing heap_walk_get_next
263 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 263 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
264 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 264 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
265 GNUNET_CONTAINER_heap_walk_get_next(myHeap);; 265 GNUNET_CONTAINER_heap_walk_get_next (myHeap);;
266 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 266 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
267 GNUNET_CONTAINER_heap_walk_get_next(myHeap); 267 GNUNET_CONTAINER_heap_walk_get_next (myHeap);
268 268
269 GNUNET_assert(0 == nstrcmp("10", GNUNET_CONTAINER_heap_remove_node(n1))); 269 GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
270 GNUNET_assert(0 == nstrcmp("20", GNUNET_CONTAINER_heap_remove_node(n2))); 270 GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
271 GNUNET_assert(0 == nstrcmp("40", GNUNET_CONTAINER_heap_remove_node(n4))); 271 GNUNET_assert (0 == nstrcmp ("40", GNUNET_CONTAINER_heap_remove_node (n4)));
272 GNUNET_assert(0 == nstrcmp("70", GNUNET_CONTAINER_heap_remove_node(n7))); 272 GNUNET_assert (0 == nstrcmp ("70", GNUNET_CONTAINER_heap_remove_node (n7)));
273 GNUNET_assert(0 == nstrcmp("80", GNUNET_CONTAINER_heap_remove_node(n8))); 273 GNUNET_assert (0 == nstrcmp ("80", GNUNET_CONTAINER_heap_remove_node (n8)));
274 274
275 // End Testing remove_node 275 // End Testing remove_node
276 276
277 GNUNET_CONTAINER_heap_destroy(myHeap); 277 GNUNET_CONTAINER_heap_destroy (myHeap);
278 278
279 return 0; 279 return 0;
280} 280}
281 281
282 282
283int 283int
284main(int argc, char **argv) 284main (int argc, char **argv)
285{ 285{
286 GNUNET_log_setup("test-container-heap", "WARNING", NULL); 286 GNUNET_log_setup ("test-container-heap", "WARNING", NULL);
287 return check(); 287 return check ();
288} 288}
289 289
290/* end of test_container_heap.c */ 290/* end of test_container_heap.c */
diff --git a/src/util/test_container_meta_data.c b/src/util/test_container_meta_data.c
index 854498f28..d768f3699 100644
--- a/src/util/test_container_meta_data.c
+++ b/src/util/test_container_meta_data.c
@@ -27,11 +27,13 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define ABORT(m) { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); if (m != NULL) GNUNET_CONTAINER_meta_data_destroy (m); return 1; } 30#define ABORT(m) { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
31 if (m != NULL) GNUNET_CONTAINER_meta_data_destroy (m); \
32 return 1; }
31 33
32 34
33static int 35static int
34testMeta(int i) 36testMeta (int i)
35{ 37{
36 struct GNUNET_CONTAINER_MetaData *m; 38 struct GNUNET_CONTAINER_MetaData *m;
37 char val[256]; 39 char val[256];
@@ -39,95 +41,111 @@ testMeta(int i)
39 int j; 41 int j;
40 unsigned int size; 42 unsigned int size;
41 43
42 m = GNUNET_CONTAINER_meta_data_create(); 44 m = GNUNET_CONTAINER_meta_data_create ();
43 if (GNUNET_OK != 45 if (GNUNET_OK !=
44 GNUNET_CONTAINER_meta_data_insert(m, "<test>", EXTRACTOR_METATYPE_TITLE, 46 GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE,
45 EXTRACTOR_METAFORMAT_UTF8, 47 EXTRACTOR_METAFORMAT_UTF8,
46 "text/plain", "TestTitle", 48 "text/plain", "TestTitle",
47 strlen("TestTitle") + 1)) 49 strlen ("TestTitle") + 1))
48 ABORT(m); 50 ABORT (m);
49 if (GNUNET_OK != 51 if (GNUNET_OK !=
50 GNUNET_CONTAINER_meta_data_insert(m, "<test>", 52 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
51 EXTRACTOR_METATYPE_AUTHOR_NAME, 53 EXTRACTOR_METATYPE_AUTHOR_NAME,
52 EXTRACTOR_METAFORMAT_UTF8, 54 EXTRACTOR_METAFORMAT_UTF8,
53 "text/plain", "TestTitle", 55 "text/plain", "TestTitle",
54 strlen("TestTitle") + 1)) 56 strlen ("TestTitle") + 1))
55 ABORT(m); 57 ABORT (m);
56 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert(m, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen("TestTitle") + 1)) /* dup! */ 58 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>",
57 ABORT(m); 59 EXTRACTOR_METATYPE_TITLE,
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 EXTRACTOR_METAFORMAT_UTF8,
59 ABORT(m); 61 "text/plain",
60 if (2 != GNUNET_CONTAINER_meta_data_iterate(m, NULL, NULL)) 62 "TestTitle", strlen (
61 ABORT(m); 63 "TestTitle") + 1)) /* dup! */
64 ABORT (m);
65 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>",
66 EXTRACTOR_METATYPE_AUTHOR_NAME,
67 EXTRACTOR_METAFORMAT_UTF8,
68 "text/plain",
69 "TestTitle", strlen (
70 "TestTitle") + 1)) /* dup! */
71 ABORT (m);
72 if (2 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
73 ABORT (m);
62 if (GNUNET_OK != 74 if (GNUNET_OK !=
63 GNUNET_CONTAINER_meta_data_delete(m, EXTRACTOR_METATYPE_AUTHOR_NAME, 75 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME,
64 "TestTitle", strlen("TestTitle") + 1)) 76 "TestTitle", strlen ("TestTitle") + 1))
65 ABORT(m); 77 ABORT (m);
66 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete(m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen("TestTitle") + 1)) /* already gone */ 78 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m,
67 ABORT(m); 79 EXTRACTOR_METATYPE_AUTHOR_NAME,
68 if (1 != GNUNET_CONTAINER_meta_data_iterate(m, NULL, NULL)) 80 "TestTitle", strlen (
69 ABORT(m); 81 "TestTitle") + 1)) /* already gone */
82 ABORT (m);
83 if (1 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
84 ABORT (m);
70 if (GNUNET_OK != 85 if (GNUNET_OK !=
71 GNUNET_CONTAINER_meta_data_delete(m, EXTRACTOR_METATYPE_TITLE, 86 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE,
72 "TestTitle", strlen("TestTitle") + 1)) 87 "TestTitle", strlen ("TestTitle") + 1))
73 ABORT(m); 88 ABORT (m);
74 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete(m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen("TestTitle") + 1)) /* already gone */ 89 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m,
75 ABORT(m); 90 EXTRACTOR_METATYPE_TITLE,
76 if (0 != GNUNET_CONTAINER_meta_data_iterate(m, NULL, NULL)) 91 "TestTitle", strlen (
77 ABORT(m); 92 "TestTitle") + 1)) /* already gone */
93 ABORT (m);
94 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
95 ABORT (m);
78 for (j = 0; j < i; j++) 96 for (j = 0; j < i; j++)
79 { 97 {
80 GNUNET_snprintf(val, sizeof(val), "%s.%d", 98 GNUNET_snprintf (val, sizeof(val), "%s.%d",
81 "A teststring that should compress well.", j); 99 "A teststring that should compress well.", j);
82 if (GNUNET_OK != 100 if (GNUNET_OK !=
83 GNUNET_CONTAINER_meta_data_insert(m, "<test>", 101 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
84 EXTRACTOR_METATYPE_UNKNOWN, 102 EXTRACTOR_METATYPE_UNKNOWN,
85 EXTRACTOR_METAFORMAT_UTF8, 103 EXTRACTOR_METAFORMAT_UTF8,
86 "text/plain", val, strlen(val) + 1)) 104 "text/plain", val, strlen (val) + 1))
87 ABORT(m); 105 ABORT (m);
88 } 106 }
89 if (i != GNUNET_CONTAINER_meta_data_iterate(m, NULL, NULL)) 107 if (i != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
90 ABORT(m); 108 ABORT (m);
91 109
92 size = GNUNET_CONTAINER_meta_data_get_serialized_size(m); 110 size = GNUNET_CONTAINER_meta_data_get_serialized_size (m);
93 sval = NULL; 111 sval = NULL;
94 if (size != 112 if (size !=
95 GNUNET_CONTAINER_meta_data_serialize(m, &sval, size, 113 GNUNET_CONTAINER_meta_data_serialize (m, &sval, size,
96 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) 114 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
97 { 115 {
98 GNUNET_free_non_null(sval); 116 GNUNET_free_non_null (sval);
99 ABORT(m); 117 ABORT (m);
100 } 118 }
101 GNUNET_CONTAINER_meta_data_destroy(m); 119 GNUNET_CONTAINER_meta_data_destroy (m);
102 m = GNUNET_CONTAINER_meta_data_deserialize(sval, size); 120 m = GNUNET_CONTAINER_meta_data_deserialize (sval, size);
103 GNUNET_free(sval); 121 GNUNET_free (sval);
104 if (m == NULL) 122 if (m == NULL)
105 ABORT(m); 123 ABORT (m);
106 for (j = 0; j < i; j++) 124 for (j = 0; j < i; j++)
125 {
126 GNUNET_snprintf (val,
127 sizeof(val),
128 "%s.%d",
129 "A teststring that should compress well.",
130 j);
131 if (GNUNET_OK !=
132 GNUNET_CONTAINER_meta_data_delete (m,
133 EXTRACTOR_METATYPE_UNKNOWN,
134 val,
135 strlen (val) + 1))
107 { 136 {
108 GNUNET_snprintf(val, 137 ABORT (m);
109 sizeof(val),
110 "%s.%d",
111 "A teststring that should compress well.",
112 j);
113 if (GNUNET_OK !=
114 GNUNET_CONTAINER_meta_data_delete(m,
115 EXTRACTOR_METATYPE_UNKNOWN,
116 val,
117 strlen(val) + 1))
118 {
119 ABORT(m);
120 }
121 } 138 }
122 if (0 != GNUNET_CONTAINER_meta_data_iterate(m, NULL, NULL)) 139 }
123 ABORT(m); 140 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
124 GNUNET_CONTAINER_meta_data_destroy(m); 141 ABORT (m);
142 GNUNET_CONTAINER_meta_data_destroy (m);
125 return 0; 143 return 0;
126} 144}
127 145
128 146
129static int 147static int
130testMetaMore(int i) 148testMetaMore (int i)
131{ 149{
132 struct GNUNET_CONTAINER_MetaData *meta; 150 struct GNUNET_CONTAINER_MetaData *meta;
133 int q; 151 int q;
@@ -135,68 +153,69 @@ testMetaMore(int i)
135 char *data; 153 char *data;
136 unsigned long long size; 154 unsigned long long size;
137 155
138 meta = GNUNET_CONTAINER_meta_data_create(); 156 meta = GNUNET_CONTAINER_meta_data_create ();
139 for (q = 0; q <= i; q++) 157 for (q = 0; q <= i; q++)
140 { 158 {
141 GNUNET_snprintf(txt, 128, "%u -- %u\n", i, q); 159 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
142 GNUNET_CONTAINER_meta_data_insert(meta, "<test>", 160 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
143 q % 42 /* EXTRACTOR_metatype_get_max () */, 161 q
144 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 162 % 42 /* EXTRACTOR_metatype_get_max () */,
145 txt, strlen(txt) + 1); 163 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
146 } 164 txt, strlen (txt) + 1);
147 size = GNUNET_CONTAINER_meta_data_get_serialized_size(meta); 165 }
148 data = GNUNET_malloc(size * 4); 166 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
167 data = GNUNET_malloc (size * 4);
149 if (size != 168 if (size !=
150 GNUNET_CONTAINER_meta_data_serialize(meta, &data, size * 4, 169 GNUNET_CONTAINER_meta_data_serialize (meta, &data, size * 4,
151 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)) 170 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
152 { 171 {
153 GNUNET_free(data); 172 GNUNET_free (data);
154 ABORT(meta); 173 ABORT (meta);
155 } 174 }
156 GNUNET_CONTAINER_meta_data_destroy(meta); 175 GNUNET_CONTAINER_meta_data_destroy (meta);
157 GNUNET_free(data); 176 GNUNET_free (data);
158 return 0; 177 return 0;
159} 178}
160 179
161 180
162static int 181static int
163testMetaLink() 182testMetaLink ()
164{ 183{
165 struct GNUNET_CONTAINER_MetaData *m; 184 struct GNUNET_CONTAINER_MetaData *m;
166 char *val; 185 char *val;
167 unsigned int size; 186 unsigned int size;
168 187
169 m = GNUNET_CONTAINER_meta_data_create(); 188 m = GNUNET_CONTAINER_meta_data_create ();
170 if (GNUNET_OK != 189 if (GNUNET_OK !=
171 GNUNET_CONTAINER_meta_data_insert(m, "<test>", 190 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
172 EXTRACTOR_METATYPE_UNKNOWN, 191 EXTRACTOR_METATYPE_UNKNOWN,
173 EXTRACTOR_METAFORMAT_UTF8, 192 EXTRACTOR_METAFORMAT_UTF8,
174 "text/plain", "link", 193 "text/plain", "link",
175 strlen("link") + 1)) 194 strlen ("link") + 1))
176 ABORT(m); 195 ABORT (m);
177 if (GNUNET_OK != 196 if (GNUNET_OK !=
178 GNUNET_CONTAINER_meta_data_insert(m, "<test>", 197 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
179 EXTRACTOR_METATYPE_FILENAME, 198 EXTRACTOR_METATYPE_FILENAME,
180 EXTRACTOR_METAFORMAT_UTF8, 199 EXTRACTOR_METAFORMAT_UTF8,
181 "text/plain", "lib-link.m4", 200 "text/plain", "lib-link.m4",
182 strlen("lib-link.m4") + 1)) 201 strlen ("lib-link.m4") + 1))
183 ABORT(m); 202 ABORT (m);
184 val = NULL; 203 val = NULL;
185 size = 204 size =
186 GNUNET_CONTAINER_meta_data_serialize(m, &val, (size_t)-1, 205 GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) -1,
187 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL); 206 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
188 GNUNET_CONTAINER_meta_data_destroy(m); 207 GNUNET_CONTAINER_meta_data_destroy (m);
189 m = GNUNET_CONTAINER_meta_data_deserialize(val, size); 208 m = GNUNET_CONTAINER_meta_data_deserialize (val, size);
190 GNUNET_free(val); 209 GNUNET_free (val);
191 if (m == NULL) 210 if (m == NULL)
192 ABORT(m); 211 ABORT (m);
193 GNUNET_CONTAINER_meta_data_destroy(m); 212 GNUNET_CONTAINER_meta_data_destroy (m);
194 return 0; 213 return 0;
195} 214}
196 215
197 216
198static int 217static int
199check() 218check ()
200{ 219{
201 struct GNUNET_CONTAINER_MetaData *meta; 220 struct GNUNET_CONTAINER_MetaData *meta;
202 struct GNUNET_CONTAINER_MetaData *meta2; 221 struct GNUNET_CONTAINER_MetaData *meta2;
@@ -206,143 +225,143 @@ check()
206 char *str; 225 char *str;
207 unsigned char *thumb; 226 unsigned char *thumb;
208 227
209 meta = GNUNET_CONTAINER_meta_data_create(); 228 meta = GNUNET_CONTAINER_meta_data_create ();
210 meta2 = GNUNET_CONTAINER_meta_data_create(); 229 meta2 = GNUNET_CONTAINER_meta_data_create ();
211 for (q = 0; q <= i; q++) 230 for (q = 0; q <= i; q++)
212 { 231 {
213 GNUNET_snprintf(txt, 128, "%u -- %u\n", i, q); 232 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
214 GNUNET_CONTAINER_meta_data_insert(meta, "<test>", 233 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
215 EXTRACTOR_METATYPE_UNKNOWN, 234 EXTRACTOR_METATYPE_UNKNOWN,
216 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 235 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
217 "TestTitle", strlen("TestTitle") + 1); 236 "TestTitle", strlen ("TestTitle") + 1);
218 GNUNET_CONTAINER_meta_data_insert(meta2, "<test>", 237 GNUNET_CONTAINER_meta_data_insert (meta2, "<test>",
219 EXTRACTOR_METATYPE_UNKNOWN, 238 EXTRACTOR_METATYPE_UNKNOWN,
220 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 239 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
221 "TestTitle", strlen("TestTitle") + 1); 240 "TestTitle", strlen ("TestTitle") + 1);
222 } 241 }
223 242
224 //check meta_data_test_equal 243 // check meta_data_test_equal
225 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal(meta, meta2)) 244 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
226 { 245 {
227 GNUNET_CONTAINER_meta_data_destroy(meta2); 246 GNUNET_CONTAINER_meta_data_destroy (meta2);
228 ABORT(meta); 247 ABORT (meta);
229 } 248 }
230 249
231 //check meta_data_clear 250 // check meta_data_clear
232 GNUNET_CONTAINER_meta_data_clear(meta2); 251 GNUNET_CONTAINER_meta_data_clear (meta2);
233 if (0 != GNUNET_CONTAINER_meta_data_iterate(meta2, NULL, NULL)) 252 if (0 != GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
234 { 253 {
235 GNUNET_CONTAINER_meta_data_destroy(meta2); 254 GNUNET_CONTAINER_meta_data_destroy (meta2);
236 ABORT(meta); 255 ABORT (meta);
237 } 256 }
238 // check equal branch in meta_data_test_equal 257 // check equal branch in meta_data_test_equal
239 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal(meta, meta)) 258 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta))
240 { 259 {
241 GNUNET_CONTAINER_meta_data_destroy(meta2); 260 GNUNET_CONTAINER_meta_data_destroy (meta2);
242 ABORT(meta); 261 ABORT (meta);
243 } 262 }
244 // check "count" branch in meta_data_test_equal 263 // check "count" branch in meta_data_test_equal
245 if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal(meta, meta2)) 264 if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
246 { 265 {
247 GNUNET_CONTAINER_meta_data_destroy(meta2); 266 GNUNET_CONTAINER_meta_data_destroy (meta2);
248 ABORT(meta); 267 ABORT (meta);
249 } 268 }
250 269
251 // check meta_data_add_publication_date 270 // check meta_data_add_publication_date
252 GNUNET_CONTAINER_meta_data_add_publication_date(meta2); 271 GNUNET_CONTAINER_meta_data_add_publication_date (meta2);
253 272
254 // check meta_data_merge 273 // check meta_data_merge
255 GNUNET_CONTAINER_meta_data_clear(meta2); 274 GNUNET_CONTAINER_meta_data_clear (meta2);
256 GNUNET_CONTAINER_meta_data_merge(meta2, meta); 275 GNUNET_CONTAINER_meta_data_merge (meta2, meta);
257 if (100 == GNUNET_CONTAINER_meta_data_iterate(meta2, NULL, NULL)) 276 if (100 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
258 { 277 {
259 GNUNET_CONTAINER_meta_data_destroy(meta2); 278 GNUNET_CONTAINER_meta_data_destroy (meta2);
260 ABORT(meta); 279 ABORT (meta);
261 } 280 }
262 281
263 // check meta_data_get_by_type 282 // check meta_data_get_by_type
264 GNUNET_CONTAINER_meta_data_clear(meta2); 283 GNUNET_CONTAINER_meta_data_clear (meta2);
265 if (NULL != 284 if (NULL !=
266 (str = 285 (str =
267 GNUNET_CONTAINER_meta_data_get_by_type(meta2, 286 GNUNET_CONTAINER_meta_data_get_by_type (meta2,
268 EXTRACTOR_METATYPE_UNKNOWN))) 287 EXTRACTOR_METATYPE_UNKNOWN)))
269 { 288 {
270 GNUNET_CONTAINER_meta_data_destroy(meta2); 289 GNUNET_CONTAINER_meta_data_destroy (meta2);
271 GNUNET_free(str); 290 GNUNET_free (str);
272 ABORT(meta); 291 ABORT (meta);
273 } 292 }
274 293
275 str = 294 str =
276 GNUNET_CONTAINER_meta_data_get_by_type(meta, EXTRACTOR_METATYPE_UNKNOWN); 295 GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN);
277 GNUNET_assert(NULL != str); 296 GNUNET_assert (NULL != str);
278 if (str[0] != 'T') 297 if (str[0] != 'T')
279 { 298 {
280 GNUNET_CONTAINER_meta_data_destroy(meta2); 299 GNUNET_CONTAINER_meta_data_destroy (meta2);
281 GNUNET_free(str); 300 GNUNET_free (str);
282 ABORT(meta); 301 ABORT (meta);
283 } 302 }
284 GNUNET_free(str); 303 GNUNET_free (str);
285 304
286 // check branch 305 // check branch
287 if (NULL != 306 if (NULL !=
288 (str = 307 (str =
289 GNUNET_CONTAINER_meta_data_get_by_type(meta, 308 GNUNET_CONTAINER_meta_data_get_by_type (meta,
290 EXTRACTOR_METATYPE_PUBLICATION_DATE))) 309 EXTRACTOR_METATYPE_PUBLICATION_DATE)))
291 { 310 {
292 GNUNET_free(str); 311 GNUNET_free (str);
293 GNUNET_CONTAINER_meta_data_destroy(meta2); 312 GNUNET_CONTAINER_meta_data_destroy (meta2);
294 ABORT(meta); 313 ABORT (meta);
295 } 314 }
296 315
297 //check meta_data_get_first_by_types 316 // check meta_data_get_first_by_types
298 str = 317 str =
299 GNUNET_CONTAINER_meta_data_get_first_by_types(meta, 318 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
300 EXTRACTOR_METATYPE_UNKNOWN, 319 EXTRACTOR_METATYPE_UNKNOWN,
301 -1); 320 -1);
302 GNUNET_assert(NULL != str); 321 GNUNET_assert (NULL != str);
303 if (str[0] != 'T') 322 if (str[0] != 'T')
304 { 323 {
305 GNUNET_CONTAINER_meta_data_destroy(meta2); 324 GNUNET_CONTAINER_meta_data_destroy (meta2);
306 GNUNET_free(str); 325 GNUNET_free (str);
307 ABORT(meta); 326 ABORT (meta);
308 } 327 }
309 GNUNET_free(str); 328 GNUNET_free (str);
310 329
311 //check meta_data_get_thumbnail 330 // check meta_data_get_thumbnail
312 if (GNUNET_CONTAINER_meta_data_get_thumbnail(meta, &thumb) != 0) 331 if (GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb) != 0)
313 { 332 {
314 GNUNET_free(thumb); 333 GNUNET_free (thumb);
315 GNUNET_CONTAINER_meta_data_destroy(meta2); 334 GNUNET_CONTAINER_meta_data_destroy (meta2);
316 ABORT(meta); 335 ABORT (meta);
317 } 336 }
318 GNUNET_CONTAINER_meta_data_destroy(meta2); 337 GNUNET_CONTAINER_meta_data_destroy (meta2);
319 //check meta_data_duplicate 338 // check meta_data_duplicate
320 meta2 = GNUNET_CONTAINER_meta_data_duplicate(meta); 339 meta2 = GNUNET_CONTAINER_meta_data_duplicate (meta);
321 if (200 == GNUNET_CONTAINER_meta_data_iterate(meta2, NULL, NULL)) 340 if (200 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
322 { 341 {
323 GNUNET_CONTAINER_meta_data_destroy(meta2); 342 GNUNET_CONTAINER_meta_data_destroy (meta2);
324 ABORT(meta); 343 ABORT (meta);
325 } 344 }
326 GNUNET_CONTAINER_meta_data_destroy(meta2); 345 GNUNET_CONTAINER_meta_data_destroy (meta2);
327 GNUNET_CONTAINER_meta_data_destroy(meta); 346 GNUNET_CONTAINER_meta_data_destroy (meta);
328 return 0; 347 return 0;
329} 348}
330 349
331 350
332int 351int
333main(int argc, char *argv[]) 352main (int argc, char *argv[])
334{ 353{
335 int failureCount = 0; 354 int failureCount = 0;
336 int i; 355 int i;
337 356
338 GNUNET_log_setup("test-container-meta-data", "WARNING", NULL); 357 GNUNET_log_setup ("test-container-meta-data", "WARNING", NULL);
339 for (i = 0; i < 255; i++) 358 for (i = 0; i < 255; i++)
340 failureCount += testMeta(i); 359 failureCount += testMeta (i);
341 for (i = 1; i < 255; i++) 360 for (i = 1; i < 255; i++)
342 failureCount += testMetaMore(i); 361 failureCount += testMetaMore (i);
343 failureCount += testMetaLink(); 362 failureCount += testMetaLink ();
344 363
345 int ret = check(); 364 int ret = check ();
346 365
347 if (ret == 1) 366 if (ret == 1)
348 return 1; 367 return 1;
diff --git a/src/util/test_container_multihashmap.c b/src/util/test_container_multihashmap.c
index 482bcb997..1d82bd701 100644
--- a/src/util/test_container_multihashmap.c
+++ b/src/util/test_container_multihashmap.c
@@ -27,11 +27,16 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); if (m != NULL) GNUNET_CONTAINER_multihashmap_destroy (m); if (NULL != iter) GNUNET_CONTAINER_multihashmap_iterator_destroy (iter); return 1; } 30#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
31#define CHECK(c) { if (!(c)) ABORT (); } 31 if (m != NULL) GNUNET_CONTAINER_multihashmap_destroy (m); \
32 if (NULL != \
33 iter) \
34 GNUNET_CONTAINER_multihashmap_iterator_destroy (iter); \
35 return 1; }
36#define CHECK(c) { if (! (c)) ABORT (); }
32 37
33static int 38static int
34testMap(int i) 39testMap (int i)
35{ 40{
36 struct GNUNET_CONTAINER_MultiHashMap *m; 41 struct GNUNET_CONTAINER_MultiHashMap *m;
37 struct GNUNET_HashCode k1; 42 struct GNUNET_HashCode k1;
@@ -41,77 +46,89 @@ testMap(int i)
41 const char *ret; 46 const char *ret;
42 int j; 47 int j;
43 48
44 CHECK(NULL != (m = GNUNET_CONTAINER_multihashmap_create(i, GNUNET_NO))); 49 CHECK (NULL != (m = GNUNET_CONTAINER_multihashmap_create (i, GNUNET_NO)));
45 memset(&k1, 0, sizeof(k1)); 50 memset (&k1, 0, sizeof(k1));
46 memset(&k2, 1, sizeof(k2)); 51 memset (&k2, 1, sizeof(k2));
47 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(m, &k1)); 52 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k1));
48 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(m, &k2)); 53 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2));
49 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove(m, &k1, NULL)); 54 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (m, &k1, NULL));
50 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove(m, &k2, NULL)); 55 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (m, &k2, NULL));
51 CHECK(NULL == GNUNET_CONTAINER_multihashmap_get(m, &k1)); 56 CHECK (NULL == GNUNET_CONTAINER_multihashmap_get (m, &k1));
52 CHECK(NULL == GNUNET_CONTAINER_multihashmap_get(m, &k2)); 57 CHECK (NULL == GNUNET_CONTAINER_multihashmap_get (m, &k2));
53 CHECK(0 == GNUNET_CONTAINER_multihashmap_remove_all(m, &k1)); 58 CHECK (0 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1));
54 CHECK(0 == GNUNET_CONTAINER_multihashmap_size(m)); 59 CHECK (0 == GNUNET_CONTAINER_multihashmap_size (m));
55 CHECK(0 == GNUNET_CONTAINER_multihashmap_iterate(m, NULL, NULL)); 60 CHECK (0 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL));
56 CHECK(0 == GNUNET_CONTAINER_multihashmap_get_multiple(m, &k1, NULL, NULL)); 61 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
57 62
58 CHECK(GNUNET_OK == 63 CHECK (GNUNET_OK ==
59 GNUNET_CONTAINER_multihashmap_put(m, &k1, "v1", 64 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
60 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 65 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
61 CHECK(1 == GNUNET_CONTAINER_multihashmap_size(m)); 66 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
62 ret = GNUNET_CONTAINER_multihashmap_get(m, &k1); 67 ret = GNUNET_CONTAINER_multihashmap_get (m, &k1);
63 GNUNET_assert(ret != NULL); 68 GNUNET_assert (ret != NULL);
64 CHECK(0 == strcmp("v1", ret)); 69 CHECK (0 == strcmp ("v1", ret));
65 CHECK(GNUNET_NO == 70 CHECK (GNUNET_NO ==
66 GNUNET_CONTAINER_multihashmap_put(m, &k1, "v1", 71 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
67 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 72 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
68 CHECK(1 == GNUNET_CONTAINER_multihashmap_size(m)); 73 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
69 CHECK(GNUNET_OK == 74 CHECK (GNUNET_OK ==
70 GNUNET_CONTAINER_multihashmap_put(m, &k1, "v2", 75 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
71 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 76 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
72 CHECK(GNUNET_OK == 77 CHECK (GNUNET_OK ==
73 GNUNET_CONTAINER_multihashmap_put(m, &k1, "v3", 78 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3",
74 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 79 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
75 CHECK(3 == GNUNET_CONTAINER_multihashmap_size(m)); 80 CHECK (3 == GNUNET_CONTAINER_multihashmap_size (m));
76 CHECK(GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove(m, &k1, "v3")); 81 CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (m, &k1, "v3"));
77 CHECK(2 == GNUNET_CONTAINER_multihashmap_size(m)); 82 CHECK (2 == GNUNET_CONTAINER_multihashmap_size (m));
78 CHECK(GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(m, &k1)); 83 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (m, &k1));
79 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(m, &k2)); 84 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2));
80 CHECK(2 == GNUNET_CONTAINER_multihashmap_get_multiple(m, &k1, NULL, NULL)); 85 CHECK (2 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
81 CHECK(0 == GNUNET_CONTAINER_multihashmap_get_multiple(m, &k2, NULL, NULL)); 86 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL));
82 CHECK(2 == GNUNET_CONTAINER_multihashmap_iterate(m, NULL, NULL)); 87 CHECK (2 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL));
83 iter = GNUNET_CONTAINER_multihashmap_iterator_create(m); 88 iter = GNUNET_CONTAINER_multihashmap_iterator_create (m);
84 CHECK(GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next(iter, &key_ret, (const void **)&ret)); 89 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next (iter,
85 CHECK(0 == memcmp(&key_ret, &k1, sizeof(key_ret))); 90 &key_ret,
86 CHECK(GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next(iter, &key_ret, (const void **)&ret)); 91 (const
87 CHECK(0 == memcmp(&key_ret, &k1, sizeof(key_ret))); 92 void **) &
88 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap_iterator_next(iter, NULL, NULL)); 93 ret));
89 GNUNET_free(iter); 94 CHECK (0 == memcmp (&key_ret, &k1, sizeof(key_ret)));
95 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next (iter,
96 &key_ret,
97 (const
98 void **) &
99 ret));
100 CHECK (0 == memcmp (&key_ret, &k1, sizeof(key_ret)));
101 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL,
102 NULL));
103 GNUNET_free (iter);
90 104
91 CHECK(2 == GNUNET_CONTAINER_multihashmap_remove_all(m, &k1)); 105 CHECK (2 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1));
92 for (j = 0; j < 1024; j++) 106 for (j = 0; j < 1024; j++)
93 CHECK(GNUNET_OK == 107 CHECK (GNUNET_OK ==
94 GNUNET_CONTAINER_multihashmap_put(m, &k1, "v2", 108 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
95 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 109 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
96 iter = GNUNET_CONTAINER_multihashmap_iterator_create(m); 110 iter = GNUNET_CONTAINER_multihashmap_iterator_create (m);
97 for (j = 0; j < GNUNET_CONTAINER_multihashmap_size(m); j++) 111 for (j = 0; j < GNUNET_CONTAINER_multihashmap_size (m); j++)
98 CHECK(GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next(iter, NULL, NULL)); 112 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next (iter,
99 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap_iterator_next(iter, NULL, NULL)); 113 NULL,
100 GNUNET_CONTAINER_multihashmap_iterator_destroy(iter); 114 NULL));
115 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL,
116 NULL));
117 GNUNET_CONTAINER_multihashmap_iterator_destroy (iter);
101 118
102 GNUNET_CONTAINER_multihashmap_destroy(m); 119 GNUNET_CONTAINER_multihashmap_destroy (m);
103 return 0; 120 return 0;
104} 121}
105 122
106int 123int
107main(int argc, char *argv[]) 124main (int argc, char *argv[])
108{ 125{
109 int failureCount = 0; 126 int failureCount = 0;
110 int i; 127 int i;
111 128
112 GNUNET_log_setup("test-container-multihashmap", "WARNING", NULL); 129 GNUNET_log_setup ("test-container-multihashmap", "WARNING", NULL);
113 for (i = 1; i < 255; i++) 130 for (i = 1; i < 255; i++)
114 failureCount += testMap(i); 131 failureCount += testMap (i);
115 if (failureCount != 0) 132 if (failureCount != 0)
116 return 1; 133 return 1;
117 return 0; 134 return 0;
diff --git a/src/util/test_container_multihashmap32.c b/src/util/test_container_multihashmap32.c
index d61dea782..81b941d5e 100644
--- a/src/util/test_container_multihashmap32.c
+++ b/src/util/test_container_multihashmap32.c
@@ -28,11 +28,13 @@
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30 30
31#define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); if (m != NULL) GNUNET_CONTAINER_multihashmap32_destroy (m); return 1; } 31#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
32#define CHECK(c) { if (!(c)) ABORT (); } 32 if (m != NULL) GNUNET_CONTAINER_multihashmap32_destroy (m); \
33 return 1; }
34#define CHECK(c) { if (! (c)) ABORT (); }
33 35
34static int 36static int
35testMap(int i) 37testMap (int i)
36{ 38{
37 struct GNUNET_CONTAINER_MultiHashMap32 *m; 39 struct GNUNET_CONTAINER_MultiHashMap32 *m;
38 uint32_t k1; 40 uint32_t k1;
@@ -40,63 +42,63 @@ testMap(int i)
40 const char *ret; 42 const char *ret;
41 int j; 43 int j;
42 44
43 CHECK(NULL != (m = GNUNET_CONTAINER_multihashmap32_create(i))); 45 CHECK (NULL != (m = GNUNET_CONTAINER_multihashmap32_create (i)));
44 k1 = 0; 46 k1 = 0;
45 k2 = UINT32_MAX; 47 k2 = UINT32_MAX;
46 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains(m, k1)); 48 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains (m, k1));
47 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains(m, k2)); 49 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains (m, k2));
48 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap32_remove(m, k1, NULL)); 50 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_remove (m, k1, NULL));
49 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap32_remove(m, k2, NULL)); 51 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_remove (m, k2, NULL));
50 CHECK(NULL == GNUNET_CONTAINER_multihashmap32_get(m, k1)); 52 CHECK (NULL == GNUNET_CONTAINER_multihashmap32_get (m, k1));
51 CHECK(NULL == GNUNET_CONTAINER_multihashmap32_get(m, k2)); 53 CHECK (NULL == GNUNET_CONTAINER_multihashmap32_get (m, k2));
52 CHECK(0 == GNUNET_CONTAINER_multihashmap32_remove_all(m, k1)); 54 CHECK (0 == GNUNET_CONTAINER_multihashmap32_remove_all (m, k1));
53 CHECK(0 == GNUNET_CONTAINER_multihashmap32_size(m)); 55 CHECK (0 == GNUNET_CONTAINER_multihashmap32_size (m));
54 CHECK(0 == GNUNET_CONTAINER_multihashmap32_iterate(m, NULL, NULL)); 56 CHECK (0 == GNUNET_CONTAINER_multihashmap32_iterate (m, NULL, NULL));
55 CHECK(0 == GNUNET_CONTAINER_multihashmap32_get_multiple(m, k1, NULL, NULL)); 57 CHECK (0 == GNUNET_CONTAINER_multihashmap32_get_multiple (m, k1, NULL, NULL));
56 58
57 CHECK(GNUNET_OK == 59 CHECK (GNUNET_OK ==
58 GNUNET_CONTAINER_multihashmap32_put(m, k1, "v1", 60 GNUNET_CONTAINER_multihashmap32_put (m, k1, "v1",
59 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 61 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
60 CHECK(1 == GNUNET_CONTAINER_multihashmap32_size(m)); 62 CHECK (1 == GNUNET_CONTAINER_multihashmap32_size (m));
61 ret = GNUNET_CONTAINER_multihashmap32_get(m, k1); 63 ret = GNUNET_CONTAINER_multihashmap32_get (m, k1);
62 GNUNET_assert(ret != NULL); 64 GNUNET_assert (ret != NULL);
63 CHECK(0 == strcmp("v1", ret)); 65 CHECK (0 == strcmp ("v1", ret));
64 CHECK(GNUNET_NO == 66 CHECK (GNUNET_NO ==
65 GNUNET_CONTAINER_multihashmap32_put(m, k1, "v1", 67 GNUNET_CONTAINER_multihashmap32_put (m, k1, "v1",
66 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 68 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
67 CHECK(1 == GNUNET_CONTAINER_multihashmap32_size(m)); 69 CHECK (1 == GNUNET_CONTAINER_multihashmap32_size (m));
68 CHECK(GNUNET_OK == 70 CHECK (GNUNET_OK ==
69 GNUNET_CONTAINER_multihashmap32_put(m, k1, "v2", 71 GNUNET_CONTAINER_multihashmap32_put (m, k1, "v2",
70 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 72 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
71 CHECK(GNUNET_OK == 73 CHECK (GNUNET_OK ==
72 GNUNET_CONTAINER_multihashmap32_put(m, k1, "v3", 74 GNUNET_CONTAINER_multihashmap32_put (m, k1, "v3",
73 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
74 CHECK(3 == GNUNET_CONTAINER_multihashmap32_size(m));
75 CHECK(GNUNET_OK == GNUNET_CONTAINER_multihashmap32_remove(m, k1, "v3"));
76 CHECK(2 == GNUNET_CONTAINER_multihashmap32_size(m));
77 CHECK(GNUNET_YES == GNUNET_CONTAINER_multihashmap32_contains(m, k1));
78 CHECK(GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains(m, k2));
79 CHECK(2 == GNUNET_CONTAINER_multihashmap32_get_multiple(m, k1, NULL, NULL));
80 CHECK(0 == GNUNET_CONTAINER_multihashmap32_get_multiple(m, k2, NULL, NULL));
81 CHECK(2 == GNUNET_CONTAINER_multihashmap32_iterate(m, NULL, NULL));
82 CHECK(2 == GNUNET_CONTAINER_multihashmap32_remove_all(m, k1));
83 for (j = 0; j < 1024; j++)
84 CHECK(GNUNET_OK ==
85 GNUNET_CONTAINER_multihashmap32_put(m, k1, "v2",
86 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 75 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
87 GNUNET_CONTAINER_multihashmap32_destroy(m); 76 CHECK (3 == GNUNET_CONTAINER_multihashmap32_size (m));
77 CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap32_remove (m, k1, "v3"));
78 CHECK (2 == GNUNET_CONTAINER_multihashmap32_size (m));
79 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap32_contains (m, k1));
80 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains (m, k2));
81 CHECK (2 == GNUNET_CONTAINER_multihashmap32_get_multiple (m, k1, NULL, NULL));
82 CHECK (0 == GNUNET_CONTAINER_multihashmap32_get_multiple (m, k2, NULL, NULL));
83 CHECK (2 == GNUNET_CONTAINER_multihashmap32_iterate (m, NULL, NULL));
84 CHECK (2 == GNUNET_CONTAINER_multihashmap32_remove_all (m, k1));
85 for (j = 0; j < 1024; j++)
86 CHECK (GNUNET_OK ==
87 GNUNET_CONTAINER_multihashmap32_put (m, k1, "v2",
88 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
89 GNUNET_CONTAINER_multihashmap32_destroy (m);
88 return 0; 90 return 0;
89} 91}
90 92
91int 93int
92main(int argc, char *argv[]) 94main (int argc, char *argv[])
93{ 95{
94 int failureCount = 0; 96 int failureCount = 0;
95 int i; 97 int i;
96 98
97 GNUNET_log_setup("test-container-multihashmap", "WARNING", NULL); 99 GNUNET_log_setup ("test-container-multihashmap", "WARNING", NULL);
98 for (i = 1; i < 255; i++) 100 for (i = 1; i < 255; i++)
99 failureCount += testMap(i); 101 failureCount += testMap (i);
100 if (failureCount != 0) 102 if (failureCount != 0)
101 return 1; 103 return 1;
102 return 0; 104 return 0;
diff --git a/src/util/test_container_multipeermap.c b/src/util/test_container_multipeermap.c
index 67bb5d3db..19cd67080 100644
--- a/src/util/test_container_multipeermap.c
+++ b/src/util/test_container_multipeermap.c
@@ -27,11 +27,16 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); if (NULL != m) GNUNET_CONTAINER_multipeermap_destroy (m); if (NULL != iter) GNUNET_CONTAINER_multipeermap_iterator_destroy (iter); return 1; } 30#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
31#define CHECK(c) { if (!(c)) ABORT (); } 31 if (NULL != m) GNUNET_CONTAINER_multipeermap_destroy (m); \
32 if (NULL != \
33 iter) \
34 GNUNET_CONTAINER_multipeermap_iterator_destroy (iter); \
35 return 1; }
36#define CHECK(c) { if (! (c)) ABORT (); }
32 37
33static int 38static int
34testMap(int i) 39testMap (int i)
35{ 40{
36 struct GNUNET_CONTAINER_MultiPeerMap *m; 41 struct GNUNET_CONTAINER_MultiPeerMap *m;
37 struct GNUNET_PeerIdentity k1; 42 struct GNUNET_PeerIdentity k1;
@@ -41,77 +46,89 @@ testMap(int i)
41 const char *ret; 46 const char *ret;
42 int j; 47 int j;
43 48
44 CHECK(NULL != (m = GNUNET_CONTAINER_multipeermap_create(i, GNUNET_NO))); 49 CHECK (NULL != (m = GNUNET_CONTAINER_multipeermap_create (i, GNUNET_NO)));
45 memset(&k1, 0, sizeof(k1)); 50 memset (&k1, 0, sizeof(k1));
46 memset(&k2, 1, sizeof(k2)); 51 memset (&k2, 1, sizeof(k2));
47 CHECK(GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(m, &k1)); 52 CHECK (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (m, &k1));
48 CHECK(GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(m, &k2)); 53 CHECK (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (m, &k2));
49 CHECK(GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove(m, &k1, NULL)); 54 CHECK (GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove (m, &k1, NULL));
50 CHECK(GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove(m, &k2, NULL)); 55 CHECK (GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove (m, &k2, NULL));
51 CHECK(NULL == GNUNET_CONTAINER_multipeermap_get(m, &k1)); 56 CHECK (NULL == GNUNET_CONTAINER_multipeermap_get (m, &k1));
52 CHECK(NULL == GNUNET_CONTAINER_multipeermap_get(m, &k2)); 57 CHECK (NULL == GNUNET_CONTAINER_multipeermap_get (m, &k2));
53 CHECK(0 == GNUNET_CONTAINER_multipeermap_remove_all(m, &k1)); 58 CHECK (0 == GNUNET_CONTAINER_multipeermap_remove_all (m, &k1));
54 CHECK(0 == GNUNET_CONTAINER_multipeermap_size(m)); 59 CHECK (0 == GNUNET_CONTAINER_multipeermap_size (m));
55 CHECK(0 == GNUNET_CONTAINER_multipeermap_iterate(m, NULL, NULL)); 60 CHECK (0 == GNUNET_CONTAINER_multipeermap_iterate (m, NULL, NULL));
56 CHECK(0 == GNUNET_CONTAINER_multipeermap_get_multiple(m, &k1, NULL, NULL)); 61 CHECK (0 == GNUNET_CONTAINER_multipeermap_get_multiple (m, &k1, NULL, NULL));
57 62
58 CHECK(GNUNET_OK == 63 CHECK (GNUNET_OK ==
59 GNUNET_CONTAINER_multipeermap_put(m, &k1, "v1", 64 GNUNET_CONTAINER_multipeermap_put (m, &k1, "v1",
60 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 65 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
61 CHECK(1 == GNUNET_CONTAINER_multipeermap_size(m)); 66 CHECK (1 == GNUNET_CONTAINER_multipeermap_size (m));
62 ret = GNUNET_CONTAINER_multipeermap_get(m, &k1); 67 ret = GNUNET_CONTAINER_multipeermap_get (m, &k1);
63 GNUNET_assert(ret != NULL); 68 GNUNET_assert (ret != NULL);
64 CHECK(0 == strcmp("v1", ret)); 69 CHECK (0 == strcmp ("v1", ret));
65 CHECK(GNUNET_NO == 70 CHECK (GNUNET_NO ==
66 GNUNET_CONTAINER_multipeermap_put(m, &k1, "v1", 71 GNUNET_CONTAINER_multipeermap_put (m, &k1, "v1",
67 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); 72 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
68 CHECK(1 == GNUNET_CONTAINER_multipeermap_size(m)); 73 CHECK (1 == GNUNET_CONTAINER_multipeermap_size (m));
69 CHECK(GNUNET_OK == 74 CHECK (GNUNET_OK ==
70 GNUNET_CONTAINER_multipeermap_put(m, &k1, "v2", 75 GNUNET_CONTAINER_multipeermap_put (m, &k1, "v2",
71 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 76 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
72 CHECK(GNUNET_OK == 77 CHECK (GNUNET_OK ==
73 GNUNET_CONTAINER_multipeermap_put(m, &k1, "v3", 78 GNUNET_CONTAINER_multipeermap_put (m, &k1, "v3",
74 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 79 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
75 CHECK(3 == GNUNET_CONTAINER_multipeermap_size(m)); 80 CHECK (3 == GNUNET_CONTAINER_multipeermap_size (m));
76 CHECK(GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove(m, &k1, "v3")); 81 CHECK (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (m, &k1, "v3"));
77 CHECK(2 == GNUNET_CONTAINER_multipeermap_size(m)); 82 CHECK (2 == GNUNET_CONTAINER_multipeermap_size (m));
78 CHECK(GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(m, &k1)); 83 CHECK (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (m, &k1));
79 CHECK(GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(m, &k2)); 84 CHECK (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (m, &k2));
80 CHECK(2 == GNUNET_CONTAINER_multipeermap_get_multiple(m, &k1, NULL, NULL)); 85 CHECK (2 == GNUNET_CONTAINER_multipeermap_get_multiple (m, &k1, NULL, NULL));
81 CHECK(0 == GNUNET_CONTAINER_multipeermap_get_multiple(m, &k2, NULL, NULL)); 86 CHECK (0 == GNUNET_CONTAINER_multipeermap_get_multiple (m, &k2, NULL, NULL));
82 CHECK(2 == GNUNET_CONTAINER_multipeermap_iterate(m, NULL, NULL)); 87 CHECK (2 == GNUNET_CONTAINER_multipeermap_iterate (m, NULL, NULL));
83 iter = GNUNET_CONTAINER_multipeermap_iterator_create(m); 88 iter = GNUNET_CONTAINER_multipeermap_iterator_create (m);
84 CHECK(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next(iter, &key_ret, (const void **)&ret)); 89 CHECK (GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iter,
85 CHECK(0 == memcmp(&key_ret, &k1, sizeof(key_ret))); 90 &key_ret,
86 CHECK(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next(iter, &key_ret, (const void **)&ret)); 91 (const
87 CHECK(0 == memcmp(&key_ret, &k1, sizeof(key_ret))); 92 void **) &
88 CHECK(GNUNET_NO == GNUNET_CONTAINER_multipeermap_iterator_next(iter, NULL, NULL)); 93 ret));
89 GNUNET_free(iter); 94 CHECK (0 == memcmp (&key_ret, &k1, sizeof(key_ret)));
95 CHECK (GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iter,
96 &key_ret,
97 (const
98 void **) &
99 ret));
100 CHECK (0 == memcmp (&key_ret, &k1, sizeof(key_ret)));
101 CHECK (GNUNET_NO == GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL,
102 NULL));
103 GNUNET_free (iter);
90 104
91 CHECK(2 == GNUNET_CONTAINER_multipeermap_remove_all(m, &k1)); 105 CHECK (2 == GNUNET_CONTAINER_multipeermap_remove_all (m, &k1));
92 for (j = 0; j < 1024; j++) 106 for (j = 0; j < 1024; j++)
93 CHECK(GNUNET_OK == 107 CHECK (GNUNET_OK ==
94 GNUNET_CONTAINER_multipeermap_put(m, &k1, "v2", 108 GNUNET_CONTAINER_multipeermap_put (m, &k1, "v2",
95 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 109 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
96 iter = GNUNET_CONTAINER_multipeermap_iterator_create(m); 110 iter = GNUNET_CONTAINER_multipeermap_iterator_create (m);
97 for (j = 0; j < GNUNET_CONTAINER_multipeermap_size(m); j++) 111 for (j = 0; j < GNUNET_CONTAINER_multipeermap_size (m); j++)
98 CHECK(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next(iter, NULL, NULL)); 112 CHECK (GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iter,
99 CHECK(GNUNET_NO == GNUNET_CONTAINER_multipeermap_iterator_next(iter, NULL, NULL)); 113 NULL,
100 GNUNET_CONTAINER_multipeermap_iterator_destroy(iter); 114 NULL));
115 CHECK (GNUNET_NO == GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL,
116 NULL));
117 GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
101 118
102 GNUNET_CONTAINER_multipeermap_destroy(m); 119 GNUNET_CONTAINER_multipeermap_destroy (m);
103 return 0; 120 return 0;
104} 121}
105 122
106int 123int
107main(int argc, char *argv[]) 124main (int argc, char *argv[])
108{ 125{
109 int failureCount = 0; 126 int failureCount = 0;
110 int i; 127 int i;
111 128
112 GNUNET_log_setup("test-container-multipeermap", "WARNING", NULL); 129 GNUNET_log_setup ("test-container-multipeermap", "WARNING", NULL);
113 for (i = 1; i < 255; i++) 130 for (i = 1; i < 255; i++)
114 failureCount += testMap(i); 131 failureCount += testMap (i);
115 if (failureCount != 0) 132 if (failureCount != 0)
116 return 1; 133 return 1;
117 return 0; 134 return 0;
diff --git a/src/util/test_crypto_crc.c b/src/util/test_crypto_crc.c
index fb9d5361b..4507deadd 100644
--- a/src/util/test_crypto_crc.c
+++ b/src/util/test_crypto_crc.c
@@ -205,16 +205,16 @@ static int expected[] = {
205}; 205};
206 206
207int 207int
208main(int argc, char *argv[]) 208main (int argc, char *argv[])
209{ 209{
210 char buf[1024]; 210 char buf[1024];
211 int i; 211 int i;
212 212
213 GNUNET_log_setup("test-crypto-crc", "WARNING", NULL); 213 GNUNET_log_setup ("test-crypto-crc", "WARNING", NULL);
214 for (i = 0; i < 1024; i++) 214 for (i = 0; i < 1024; i++)
215 buf[i] = (char)i; 215 buf[i] = (char) i;
216 for (i = 0; i < 1024; i++) 216 for (i = 0; i < 1024; i++)
217 if (expected[i] != GNUNET_CRYPTO_crc32_n(&buf[i], 1024 - i)) 217 if (expected[i] != GNUNET_CRYPTO_crc32_n (&buf[i], 1024 - i))
218 return 1; 218 return 1;
219 return 0; 219 return 0;
220} 220}
diff --git a/src/util/test_crypto_ecc_dlog.c b/src/util/test_crypto_ecc_dlog.c
index 1be7bb678..b3b2a7480 100644
--- a/src/util/test_crypto_ecc_dlog.c
+++ b/src/util/test_crypto_ecc_dlog.c
@@ -63,7 +63,7 @@
63 * @param edc context for ECC operations 63 * @param edc context for ECC operations
64 */ 64 */
65static void 65static void
66test_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc) 66test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc)
67{ 67{
68 gcry_mpi_t fact; 68 gcry_mpi_t fact;
69 gcry_mpi_t n; 69 gcry_mpi_t n;
@@ -74,46 +74,46 @@ test_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc)
74 int x; 74 int x;
75 int iret; 75 int iret;
76 76
77 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, NULL, CURVE)); 77 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
78 g = gcry_mpi_ec_get_point("g", ctx, 0); 78 g = gcry_mpi_ec_get_point ("g", ctx, 0);
79 GNUNET_assert(NULL != g); 79 GNUNET_assert (NULL != g);
80 n = gcry_mpi_ec_get_mpi("n", ctx, 0); 80 n = gcry_mpi_ec_get_mpi ("n", ctx, 0);
81 q = gcry_mpi_point_new(0); 81 q = gcry_mpi_point_new (0);
82 fact = gcry_mpi_new(0); 82 fact = gcry_mpi_new (0);
83 for (i = 0; i < TEST_ITER; i++) 83 for (i = 0; i < TEST_ITER; i++)
84 {
85 fprintf (stderr, ".");
86 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
87 MAX_FACT);
88 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
89 2))
84 { 90 {
85 fprintf(stderr, "."); 91 gcry_mpi_set_ui (fact, x);
86 x = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 92 gcry_mpi_sub (fact, n, fact);
87 MAX_FACT); 93 x = -x;
88 if (0 == GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
89 2))
90 {
91 gcry_mpi_set_ui(fact, x);
92 gcry_mpi_sub(fact, n, fact);
93 x = -x;
94 }
95 else
96 {
97 gcry_mpi_set_ui(fact, x);
98 }
99 gcry_mpi_ec_mul(q, fact, g, ctx);
100 if (x !=
101 (iret = GNUNET_CRYPTO_ecc_dlog(edc,
102 q)))
103 {
104 fprintf(stderr,
105 "DLOG failed for value %d (%d)\n",
106 x,
107 iret);
108 GNUNET_assert(0);
109 }
110 } 94 }
111 gcry_mpi_release(fact); 95 else
112 gcry_mpi_release(n); 96 {
113 gcry_mpi_point_release(g); 97 gcry_mpi_set_ui (fact, x);
114 gcry_mpi_point_release(q); 98 }
115 gcry_ctx_release(ctx); 99 gcry_mpi_ec_mul (q, fact, g, ctx);
116 fprintf(stderr, "\n"); 100 if (x !=
101 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
102 q)))
103 {
104 fprintf (stderr,
105 "DLOG failed for value %d (%d)\n",
106 x,
107 iret);
108 GNUNET_assert (0);
109 }
110 }
111 gcry_mpi_release (fact);
112 gcry_mpi_release (n);
113 gcry_mpi_point_release (g);
114 gcry_mpi_point_release (q);
115 gcry_ctx_release (ctx);
116 fprintf (stderr, "\n");
117} 117}
118 118
119 119
@@ -123,7 +123,7 @@ test_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc)
123 * @param edc context for ECC operations 123 * @param edc context for ECC operations
124 */ 124 */
125static void 125static void
126test_math(struct GNUNET_CRYPTO_EccDlogContext *edc) 126test_math (struct GNUNET_CRYPTO_EccDlogContext *edc)
127{ 127{
128 int i; 128 int i;
129 int j; 129 int j;
@@ -136,58 +136,59 @@ test_math(struct GNUNET_CRYPTO_EccDlogContext *edc)
136 gcry_mpi_point_t sum; 136 gcry_mpi_point_t sum;
137 137
138 for (i = -MATH_MAX; i < MATH_MAX; i++) 138 for (i = -MATH_MAX; i < MATH_MAX; i++)
139 {
140 ip = GNUNET_CRYPTO_ecc_dexp (edc, i);
141 for (j = -MATH_MAX; j < MATH_MAX; j++)
139 { 142 {
140 ip = GNUNET_CRYPTO_ecc_dexp(edc, i); 143 fprintf (stderr, ".");
141 for (j = -MATH_MAX; j < MATH_MAX; j++) 144 jp = GNUNET_CRYPTO_ecc_dexp (edc, j);
142 { 145 GNUNET_CRYPTO_ecc_rnd (edc,
143 fprintf(stderr, "."); 146 &r,
144 jp = GNUNET_CRYPTO_ecc_dexp(edc, j); 147 &r_inv);
145 GNUNET_CRYPTO_ecc_rnd(edc, 148 ir = GNUNET_CRYPTO_ecc_add (edc, ip, r);
146 &r, 149 irj = GNUNET_CRYPTO_ecc_add (edc, ir, jp);
147 &r_inv); 150 sum = GNUNET_CRYPTO_ecc_add (edc, irj, r_inv);
148 ir = GNUNET_CRYPTO_ecc_add(edc, ip, r); 151 GNUNET_assert (i + j ==
149 irj = GNUNET_CRYPTO_ecc_add(edc, ir, jp); 152 GNUNET_CRYPTO_ecc_dlog (edc,
150 sum = GNUNET_CRYPTO_ecc_add(edc, irj, r_inv); 153 sum));
151 GNUNET_assert(i + j == 154 GNUNET_CRYPTO_ecc_free (jp);
152 GNUNET_CRYPTO_ecc_dlog(edc, 155 GNUNET_CRYPTO_ecc_free (ir);
153 sum)); 156 GNUNET_CRYPTO_ecc_free (irj);
154 GNUNET_CRYPTO_ecc_free(jp); 157 GNUNET_CRYPTO_ecc_free (r);
155 GNUNET_CRYPTO_ecc_free(ir); 158 GNUNET_CRYPTO_ecc_free (r_inv);
156 GNUNET_CRYPTO_ecc_free(irj); 159 GNUNET_CRYPTO_ecc_free (sum);
157 GNUNET_CRYPTO_ecc_free(r);
158 GNUNET_CRYPTO_ecc_free(r_inv);
159 GNUNET_CRYPTO_ecc_free(sum);
160 }
161 GNUNET_CRYPTO_ecc_free(ip);
162 } 160 }
163 fprintf(stderr, "\n"); 161 GNUNET_CRYPTO_ecc_free (ip);
162 }
163 fprintf (stderr, "\n");
164} 164}
165 165
166 166
167 167
168int 168int
169main(int argc, char *argv[]) 169main (int argc, char *argv[])
170{ 170{
171 struct GNUNET_CRYPTO_EccDlogContext *edc; 171 struct GNUNET_CRYPTO_EccDlogContext *edc;
172 172
173 if (!gcry_check_version("1.6.0")) 173 if (! gcry_check_version ("1.6.0"))
174 { 174 {
175 fprintf(stderr, 175 fprintf (stderr,
176 _ 176 _
177 ("libgcrypt has not the expected version (version %s is required).\n"), 177 (
178 "1.6.0"); 178 "libgcrypt has not the expected version (version %s is required).\n"),
179 return 0; 179 "1.6.0");
180 } 180 return 0;
181 if (getenv("GNUNET_GCRYPT_DEBUG")) 181 }
182 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 182 if (getenv ("GNUNET_GCRYPT_DEBUG"))
183 GNUNET_log_setup("test-crypto-ecc-dlog", 183 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
184 "WARNING", 184 GNUNET_log_setup ("test-crypto-ecc-dlog",
185 NULL); 185 "WARNING",
186 edc = GNUNET_CRYPTO_ecc_dlog_prepare(MAX_FACT, 186 NULL);
187 MAX_MEM); 187 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
188 test_dlog(edc); 188 MAX_MEM);
189 test_math(edc); 189 test_dlog (edc);
190 GNUNET_CRYPTO_ecc_dlog_release(edc); 190 test_math (edc);
191 GNUNET_CRYPTO_ecc_dlog_release (edc);
191 return 0; 192 return 0;
192} 193}
193 194
diff --git a/src/util/test_crypto_ecdh_eddsa.c b/src/util/test_crypto_ecdh_eddsa.c
index 5e2d930bc..68f8c4671 100644
--- a/src/util/test_crypto_ecdh_eddsa.c
+++ b/src/util/test_crypto_ecdh_eddsa.c
@@ -30,7 +30,7 @@
30 30
31 31
32static int 32static int
33test_ecdh() 33test_ecdh ()
34{ 34{
35 struct GNUNET_CRYPTO_EddsaPrivateKey *priv_dsa; 35 struct GNUNET_CRYPTO_EddsaPrivateKey *priv_dsa;
36 struct GNUNET_CRYPTO_EcdhePrivateKey *priv_ecdh; 36 struct GNUNET_CRYPTO_EcdhePrivateKey *priv_ecdh;
@@ -39,56 +39,57 @@ test_ecdh()
39 struct GNUNET_HashCode dh[2]; 39 struct GNUNET_HashCode dh[2];
40 40
41 /* Generate keys */ 41 /* Generate keys */
42 priv_dsa = GNUNET_CRYPTO_eddsa_key_create(); 42 priv_dsa = GNUNET_CRYPTO_eddsa_key_create ();
43 GNUNET_CRYPTO_eddsa_key_get_public(priv_dsa, 43 GNUNET_CRYPTO_eddsa_key_get_public (priv_dsa,
44 &id1); 44 &id1);
45 for (unsigned int j = 0; j < 4; j++) 45 for (unsigned int j = 0; j < 4; j++)
46 { 46 {
47 fprintf(stderr, ","); 47 fprintf (stderr, ",");
48 priv_ecdh = GNUNET_CRYPTO_ecdhe_key_create(); 48 priv_ecdh = GNUNET_CRYPTO_ecdhe_key_create ();
49 /* Extract public keys */ 49 /* Extract public keys */
50 GNUNET_CRYPTO_ecdhe_key_get_public(priv_ecdh, 50 GNUNET_CRYPTO_ecdhe_key_get_public (priv_ecdh,
51 &id2); 51 &id2);
52 /* Do ECDH */ 52 /* Do ECDH */
53 GNUNET_assert(GNUNET_OK == 53 GNUNET_assert (GNUNET_OK ==
54 GNUNET_CRYPTO_eddsa_ecdh(priv_dsa, 54 GNUNET_CRYPTO_eddsa_ecdh (priv_dsa,
55 &id2, 55 &id2,
56 &dh[0])); 56 &dh[0]));
57 GNUNET_assert(GNUNET_OK == 57 GNUNET_assert (GNUNET_OK ==
58 GNUNET_CRYPTO_ecdh_eddsa(priv_ecdh, 58 GNUNET_CRYPTO_ecdh_eddsa (priv_ecdh,
59 &id1, 59 &id1,
60 &dh[1])); 60 &dh[1]));
61 /* Check that both DH results are equal. */ 61 /* Check that both DH results are equal. */
62 GNUNET_assert(0 == memcmp(&dh[0], 62 GNUNET_assert (0 == memcmp (&dh[0],
63 &dh[1], 63 &dh[1],
64 sizeof(struct GNUNET_HashCode))); 64 sizeof(struct GNUNET_HashCode)));
65 GNUNET_free(priv_ecdh); 65 GNUNET_free (priv_ecdh);
66 } 66 }
67 GNUNET_free(priv_dsa); 67 GNUNET_free (priv_dsa);
68 return 0; 68 return 0;
69} 69}
70 70
71 71
72int 72int
73main(int argc, char *argv[]) 73main (int argc, char *argv[])
74{ 74{
75 if (!gcry_check_version("1.6.0")) 75 if (! gcry_check_version ("1.6.0"))
76 { 76 {
77 fprintf(stderr, 77 fprintf (stderr,
78 _("libgcrypt has not the expected version (version %s is required).\n"), 78 _ (
79 "1.6.0"); 79 "libgcrypt has not the expected version (version %s is required).\n"),
80 return 0; 80 "1.6.0");
81 } 81 return 0;
82 if (getenv("GNUNET_GCRYPT_DEBUG")) 82 }
83 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 83 if (getenv ("GNUNET_GCRYPT_DEBUG"))
84 GNUNET_log_setup("test-crypto-ecdh-eddsa", "WARNING", NULL); 84 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
85 GNUNET_log_setup ("test-crypto-ecdh-eddsa", "WARNING", NULL);
85 for (unsigned int i = 0; i < 4; i++) 86 for (unsigned int i = 0; i < 4; i++)
86 { 87 {
87 fprintf(stderr, 88 fprintf (stderr,
88 "."); 89 ".");
89 if (0 != test_ecdh()) 90 if (0 != test_ecdh ())
90 return 1; 91 return 1;
91 } 92 }
92 return 0; 93 return 0;
93} 94}
94 95
diff --git a/src/util/test_crypto_ecdhe.c b/src/util/test_crypto_ecdhe.c
index d833abc1d..5e5b34c43 100644
--- a/src/util/test_crypto_ecdhe.c
+++ b/src/util/test_crypto_ecdhe.c
@@ -29,7 +29,7 @@
29 29
30 30
31int 31int
32main(int argc, char *argv[]) 32main (int argc, char *argv[])
33{ 33{
34 struct GNUNET_CRYPTO_EcdhePrivateKey *priv1; 34 struct GNUNET_CRYPTO_EcdhePrivateKey *priv1;
35 struct GNUNET_CRYPTO_EcdhePrivateKey *priv2; 35 struct GNUNET_CRYPTO_EcdhePrivateKey *priv2;
@@ -38,33 +38,34 @@ main(int argc, char *argv[])
38 struct GNUNET_HashCode ecdh1; 38 struct GNUNET_HashCode ecdh1;
39 struct GNUNET_HashCode ecdh2; 39 struct GNUNET_HashCode ecdh2;
40 40
41 if (!gcry_check_version("1.6.0")) 41 if (! gcry_check_version ("1.6.0"))
42 { 42 {
43 fprintf(stderr, 43 fprintf (stderr,
44 _ 44 _
45 ("libgcrypt has not the expected version (version %s is required).\n"), 45 (
46 "1.6.0"); 46 "libgcrypt has not the expected version (version %s is required).\n"),
47 return 0; 47 "1.6.0");
48 } 48 return 0;
49 if (getenv("GNUNET_GCRYPT_DEBUG")) 49 }
50 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 50 if (getenv ("GNUNET_GCRYPT_DEBUG"))
51 GNUNET_log_setup("test-crypto-ecdhe", "WARNING", NULL); 51 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
52 GNUNET_log_setup ("test-crypto-ecdhe", "WARNING", NULL);
52 53
53 for (unsigned int i = 0; i < 100; i++) 54 for (unsigned int i = 0; i < 100; i++)
54 { 55 {
55 fprintf(stderr, 56 fprintf (stderr,
56 "."); 57 ".");
57 priv1 = GNUNET_CRYPTO_ecdhe_key_create(); 58 priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
58 priv2 = GNUNET_CRYPTO_ecdhe_key_create(); 59 priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
59 GNUNET_CRYPTO_ecdhe_key_get_public(priv1, &pub1); 60 GNUNET_CRYPTO_ecdhe_key_get_public (priv1, &pub1);
60 GNUNET_CRYPTO_ecdhe_key_get_public(priv2, &pub2); 61 GNUNET_CRYPTO_ecdhe_key_get_public (priv2, &pub2);
61 GNUNET_CRYPTO_ecc_ecdh(priv1, &pub2, &ecdh1); 62 GNUNET_CRYPTO_ecc_ecdh (priv1, &pub2, &ecdh1);
62 GNUNET_CRYPTO_ecc_ecdh(priv2, &pub1, &ecdh2); 63 GNUNET_CRYPTO_ecc_ecdh (priv2, &pub1, &ecdh2);
63 GNUNET_assert(0 == memcmp(&ecdh1, &ecdh2, 64 GNUNET_assert (0 == memcmp (&ecdh1, &ecdh2,
64 sizeof(struct GNUNET_HashCode))); 65 sizeof(struct GNUNET_HashCode)));
65 GNUNET_free(priv1); 66 GNUNET_free (priv1);
66 GNUNET_free(priv2); 67 GNUNET_free (priv2);
67 } 68 }
68 return 0; 69 return 0;
69} 70}
70 71
diff --git a/src/util/test_crypto_ecdsa.c b/src/util/test_crypto_ecdsa.c
index 797d61285..d6f406b47 100644
--- a/src/util/test_crypto_ecdsa.c
+++ b/src/util/test_crypto_ecdsa.c
@@ -37,7 +37,7 @@ static struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
37 37
38 38
39static int 39static int
40testSignVerify() 40testSignVerify ()
41{ 41{
42 struct GNUNET_CRYPTO_EcdsaSignature sig; 42 struct GNUNET_CRYPTO_EcdsaSignature sig;
43 struct GNUNET_CRYPTO_EccSignaturePurpose purp; 43 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
@@ -46,48 +46,49 @@ testSignVerify()
46 struct GNUNET_TIME_Absolute start; 46 struct GNUNET_TIME_Absolute start;
47 int ok = GNUNET_OK; 47 int ok = GNUNET_OK;
48 48
49 fprintf(stderr, "%s", "W"); 49 fprintf (stderr, "%s", "W");
50 GNUNET_CRYPTO_ecdsa_key_get_public(key, &pkey); 50 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
51 start = GNUNET_TIME_absolute_get(); 51 start = GNUNET_TIME_absolute_get ();
52 purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); 52 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
53 purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST); 53 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
54 54
55 for (i = 0; i < ITER; i++) 55 for (i = 0; i < ITER; i++)
56 {
57 fprintf (stderr, "%s", "."); fflush (stderr);
58 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
56 { 59 {
57 fprintf(stderr, "%s", "."); fflush(stderr); 60 fprintf (stderr,
58 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign(key, &purp, &sig)) 61 "%s",
59 { 62 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
60 fprintf(stderr, 63 ok = GNUNET_SYSERR;
61 "%s", 64 continue;
62 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
63 ok = GNUNET_SYSERR;
64 continue;
65 }
66 if (GNUNET_SYSERR ==
67 GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68 &pkey))
69 {
70 printf("GNUNET_CRYPTO_ecdsa_verify failed!\n");
71 ok = GNUNET_SYSERR;
72 continue;
73 }
74 if (GNUNET_SYSERR !=
75 GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76 &purp, &sig, &pkey))
77 {
78 printf("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
79 ok = GNUNET_SYSERR;
80 continue;
81 }
82 } 65 }
83 printf("%d ECDSA sign/verify operations %s\n", ITER, 66 if (GNUNET_SYSERR ==
84 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES)); 67 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68 &pkey))
69 {
70 printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
71 ok = GNUNET_SYSERR;
72 continue;
73 }
74 if (GNUNET_SYSERR !=
75 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76 &purp, &sig, &pkey))
77 {
78 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
79 ok = GNUNET_SYSERR;
80 continue;
81 }
82 }
83 printf ("%d ECDSA sign/verify operations %s\n", ITER,
84 GNUNET_STRINGS_relative_time_to_string (
85 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
85 return ok; 86 return ok;
86} 87}
87 88
88 89
89static int 90static int
90testDeriveSignVerify() 91testDeriveSignVerify ()
91{ 92{
92 struct GNUNET_CRYPTO_EcdsaSignature sig; 93 struct GNUNET_CRYPTO_EcdsaSignature sig;
93 struct GNUNET_CRYPTO_EccSignaturePurpose purp; 94 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
@@ -95,52 +96,54 @@ testDeriveSignVerify()
95 struct GNUNET_CRYPTO_EcdsaPublicKey pkey; 96 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
96 struct GNUNET_CRYPTO_EcdsaPublicKey dpub; 97 struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
97 98
98 dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive(key, "test-derive", "test-CTX"); 99 dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (key, "test-derive",
99 GNUNET_CRYPTO_ecdsa_key_get_public(key, &pkey); 100 "test-CTX");
100 GNUNET_CRYPTO_ecdsa_public_key_derive(&pkey, "test-derive", "test-CTX", &dpub); 101 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
101 purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); 102 GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey, "test-derive", "test-CTX",
102 purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST); 103 &dpub);
103 104 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
104 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign(dpriv, &purp, &sig)) 105 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
105 { 106
106 fprintf(stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n"); 107 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig))
107 GNUNET_free(dpriv); 108 {
108 return GNUNET_SYSERR; 109 fprintf (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
109 } 110 GNUNET_free (dpriv);
111 return GNUNET_SYSERR;
112 }
110 if (GNUNET_SYSERR == 113 if (GNUNET_SYSERR ==
111 GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_TEST, 114 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
112 &purp, &sig, 115 &purp, &sig,
113 &dpub)) 116 &dpub))
114 { 117 {
115 printf("GNUNET_CRYPTO_ecdsa_verify failed!\n"); 118 printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
116 GNUNET_free(dpriv); 119 GNUNET_free (dpriv);
117 return GNUNET_SYSERR; 120 return GNUNET_SYSERR;
118 } 121 }
119 if (GNUNET_SYSERR != 122 if (GNUNET_SYSERR !=
120 GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_TEST, 123 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
121 &purp, &sig, 124 &purp, &sig,
122 &pkey)) 125 &pkey))
123 { 126 {
124 printf("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n"); 127 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
125 GNUNET_free(dpriv); 128 GNUNET_free (dpriv);
126 return GNUNET_SYSERR; 129 return GNUNET_SYSERR;
127 } 130 }
128 if (GNUNET_SYSERR != 131 if (GNUNET_SYSERR !=
129 GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, 132 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
130 &purp, &sig, &dpub)) 133 &purp, &sig, &dpub))
131 { 134 {
132 printf("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n"); 135 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
133 GNUNET_free(dpriv); 136 GNUNET_free (dpriv);
134 return GNUNET_SYSERR; 137 return GNUNET_SYSERR;
135 } 138 }
136 GNUNET_free(dpriv); 139 GNUNET_free (dpriv);
137 return GNUNET_OK; 140 return GNUNET_OK;
138} 141}
139 142
140 143
141#if PERF 144#if PERF
142static int 145static int
143testSignPerformance() 146testSignPerformance ()
144{ 147{
145 struct GNUNET_CRYPTO_EccSignaturePurpose purp; 148 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
146 struct GNUNET_CRYPTO_EcdsaSignature sig; 149 struct GNUNET_CRYPTO_EcdsaSignature sig;
@@ -149,93 +152,96 @@ testSignPerformance()
149 struct GNUNET_TIME_Absolute start; 152 struct GNUNET_TIME_Absolute start;
150 int ok = GNUNET_OK; 153 int ok = GNUNET_OK;
151 154
152 purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); 155 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
153 purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST); 156 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
154 fprintf(stderr, "%s", "W"); 157 fprintf (stderr, "%s", "W");
155 GNUNET_CRYPTO_ecdsa_key_get_public(key, &pkey); 158 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
156 start = GNUNET_TIME_absolute_get(); 159 start = GNUNET_TIME_absolute_get ();
157 for (i = 0; i < ITER; i++) 160 for (i = 0; i < ITER; i++)
161 {
162 fprintf (stderr, "%s", "."); fflush (stderr);
163 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
158 { 164 {
159 fprintf(stderr, "%s", "."); fflush(stderr); 165 fprintf (stderr, "%s",
160 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign(key, &purp, &sig)) 166 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
161 { 167 ok = GNUNET_SYSERR;
162 fprintf(stderr, "%s", 168 continue;
163 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
164 ok = GNUNET_SYSERR;
165 continue;
166 }
167 } 169 }
168 printf("%d ECC sign operations %s\n", ITER, 170 }
169 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 171 printf ("%d ECC sign operations %s\n", ITER,
170 GNUNET_YES)); 172 GNUNET_STRINGS_relative_time_to_string (
173 GNUNET_TIME_absolute_get_duration (start),
174 GNUNET_YES));
171 return ok; 175 return ok;
172} 176}
173#endif 177#endif
174 178
175 179
176static void 180static void
177perf_keygen() 181perf_keygen ()
178{ 182{
179 struct GNUNET_TIME_Absolute start; 183 struct GNUNET_TIME_Absolute start;
180 struct GNUNET_CRYPTO_EcdsaPrivateKey *pk; 184 struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
181 int i; 185 int i;
182 186
183 fprintf(stderr, "%s", "W"); 187 fprintf (stderr, "%s", "W");
184 start = GNUNET_TIME_absolute_get(); 188 start = GNUNET_TIME_absolute_get ();
185 for (i = 0; i < 10; i++) 189 for (i = 0; i < 10; i++)
186 { 190 {
187 fprintf(stderr, "."); fflush(stderr); 191 fprintf (stderr, "."); fflush (stderr);
188 pk = GNUNET_CRYPTO_ecdsa_key_create(); 192 pk = GNUNET_CRYPTO_ecdsa_key_create ();
189 GNUNET_free(pk); 193 GNUNET_free (pk);
190 } 194 }
191 for (; i < 25; i++) 195 for (; i < 25; i++)
192 fprintf(stderr, "."); 196 fprintf (stderr, ".");
193 fflush(stderr); 197 fflush (stderr);
194 printf("10 ECDSA keys created in %s\n", 198 printf ("10 ECDSA keys created in %s\n",
195 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES)); 199 GNUNET_STRINGS_relative_time_to_string (
200 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
196} 201}
197 202
198 203
199int 204int
200main(int argc, char *argv[]) 205main (int argc, char *argv[])
201{ 206{
202 int failure_count = 0; 207 int failure_count = 0;
203 208
204 if (!gcry_check_version("1.6.0")) 209 if (! gcry_check_version ("1.6.0"))
205 { 210 {
206 fprintf(stderr, 211 fprintf (stderr,
207 _ 212 _
208 ("libgcrypt has not the expected version (version %s is required).\n"), 213 (
209 "1.6.0"); 214 "libgcrypt has not the expected version (version %s is required).\n"),
210 return 0; 215 "1.6.0");
211 } 216 return 0;
212 if (getenv("GNUNET_GCRYPT_DEBUG")) 217 }
213 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 218 if (getenv ("GNUNET_GCRYPT_DEBUG"))
214 GNUNET_log_setup("test-crypto-ecc", "WARNING", NULL); 219 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
215 key = GNUNET_CRYPTO_ecdsa_key_create(); 220 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
216 if (GNUNET_OK != testDeriveSignVerify()) 221 key = GNUNET_CRYPTO_ecdsa_key_create ();
217 { 222 if (GNUNET_OK != testDeriveSignVerify ())
218 failure_count++; 223 {
219 fprintf(stderr, 224 failure_count++;
220 "\n\n%d TESTS FAILED!\n\n", failure_count); 225 fprintf (stderr,
221 return -1; 226 "\n\n%d TESTS FAILED!\n\n", failure_count);
222 } 227 return -1;
228 }
223#if PERF 229#if PERF
224 if (GNUNET_OK != testSignPerformance()) 230 if (GNUNET_OK != testSignPerformance ())
225 failure_count++; 231 failure_count++;
226#endif 232#endif
227 if (GNUNET_OK != testSignVerify()) 233 if (GNUNET_OK != testSignVerify ())
228 failure_count++; 234 failure_count++;
229 GNUNET_free(key); 235 GNUNET_free (key);
230 perf_keygen(); 236 perf_keygen ();
231 237
232 if (0 != failure_count) 238 if (0 != failure_count)
233 { 239 {
234 fprintf(stderr, 240 fprintf (stderr,
235 "\n\n%d TESTS FAILED!\n\n", 241 "\n\n%d TESTS FAILED!\n\n",
236 failure_count); 242 failure_count);
237 return -1; 243 return -1;
238 } 244 }
239 return 0; 245 return 0;
240} 246}
241 247
diff --git a/src/util/test_crypto_eddsa.c b/src/util/test_crypto_eddsa.c
index 2119e3080..f51316463 100644
--- a/src/util/test_crypto_eddsa.c
+++ b/src/util/test_crypto_eddsa.c
@@ -39,7 +39,7 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *key;
39 39
40 40
41static int 41static int
42testSignVerify() 42testSignVerify ()
43{ 43{
44 struct GNUNET_CRYPTO_EddsaSignature sig; 44 struct GNUNET_CRYPTO_EddsaSignature sig;
45 struct GNUNET_CRYPTO_EccSignaturePurpose purp; 45 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
@@ -48,47 +48,48 @@ testSignVerify()
48 struct GNUNET_TIME_Absolute start; 48 struct GNUNET_TIME_Absolute start;
49 int ok = GNUNET_OK; 49 int ok = GNUNET_OK;
50 50
51 fprintf(stderr, "%s", "W"); 51 fprintf (stderr, "%s", "W");
52 GNUNET_CRYPTO_eddsa_key_get_public(key, &pkey); 52 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
53 start = GNUNET_TIME_absolute_get(); 53 start = GNUNET_TIME_absolute_get ();
54 purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); 54 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
55 purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST); 55 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
56 56
57 for (i = 0; i < ITER; i++) 57 for (i = 0; i < ITER; i++)
58 {
59 fprintf (stderr, "%s", "."); fflush (stderr);
60 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
58 { 61 {
59 fprintf(stderr, "%s", "."); fflush(stderr); 62 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
60 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign(key, &purp, &sig)) 63 ok = GNUNET_SYSERR;
61 { 64 continue;
62 fprintf(stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
63 ok = GNUNET_SYSERR;
64 continue;
65 }
66 if (GNUNET_SYSERR ==
67 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68 &pkey))
69 {
70 printf("GNUNET_CRYPTO_eddsa_verify failed!\n");
71 ok = GNUNET_SYSERR;
72 continue;
73 }
74 if (GNUNET_SYSERR !=
75 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76 &purp, &sig, &pkey))
77 {
78 printf("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
79 ok = GNUNET_SYSERR;
80 continue;
81 }
82 } 65 }
83 printf("%d EdDSA sign/verify operations %s\n", ITER, 66 if (GNUNET_SYSERR ==
84 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES)); 67 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68 &pkey))
69 {
70 printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
71 ok = GNUNET_SYSERR;
72 continue;
73 }
74 if (GNUNET_SYSERR !=
75 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76 &purp, &sig, &pkey))
77 {
78 printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
79 ok = GNUNET_SYSERR;
80 continue;
81 }
82 }
83 printf ("%d EdDSA sign/verify operations %s\n", ITER,
84 GNUNET_STRINGS_relative_time_to_string (
85 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
85 return ok; 86 return ok;
86} 87}
87 88
88 89
89#if PERF 90#if PERF
90static int 91static int
91testSignPerformance() 92testSignPerformance ()
92{ 93{
93 struct GNUNET_CRYPTO_EccSignaturePurpose purp; 94 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
94 struct GNUNET_CRYPTO_EddsaSignature sig; 95 struct GNUNET_CRYPTO_EddsaSignature sig;
@@ -97,112 +98,115 @@ testSignPerformance()
97 struct GNUNET_TIME_Absolute start; 98 struct GNUNET_TIME_Absolute start;
98 int ok = GNUNET_OK; 99 int ok = GNUNET_OK;
99 100
100 purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); 101 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
101 purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST); 102 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
102 fprintf(stderr, "%s", "W"); 103 fprintf (stderr, "%s", "W");
103 GNUNET_CRYPTO_eddsa_key_get_public(key, &pkey); 104 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
104 start = GNUNET_TIME_absolute_get(); 105 start = GNUNET_TIME_absolute_get ();
105 for (i = 0; i < ITER; i++) 106 for (i = 0; i < ITER; i++)
107 {
108 fprintf (stderr, "%s", "."); fflush (stderr);
109 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
106 { 110 {
107 fprintf(stderr, "%s", "."); fflush(stderr); 111 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
108 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign(key, &purp, &sig)) 112 ok = GNUNET_SYSERR;
109 { 113 continue;
110 fprintf(stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
111 ok = GNUNET_SYSERR;
112 continue;
113 }
114 } 114 }
115 printf("%d EdDSA sign operations %s\n", ITER, 115 }
116 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), 116 printf ("%d EdDSA sign operations %s\n", ITER,
117 GNUNET_YES)); 117 GNUNET_STRINGS_relative_time_to_string (
118 GNUNET_TIME_absolute_get_duration (start),
119 GNUNET_YES));
118 return ok; 120 return ok;
119} 121}
120#endif 122#endif
121 123
122 124
123static int 125static int
124testCreateFromFile() 126testCreateFromFile ()
125{ 127{
126 struct GNUNET_CRYPTO_EddsaPublicKey p1; 128 struct GNUNET_CRYPTO_EddsaPublicKey p1;
127 struct GNUNET_CRYPTO_EddsaPublicKey p2; 129 struct GNUNET_CRYPTO_EddsaPublicKey p2;
128 130
129 key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE); 131 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
130 GNUNET_assert(NULL != key); 132 GNUNET_assert (NULL != key);
131 GNUNET_CRYPTO_eddsa_key_get_public(key, &p1); 133 GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
132 GNUNET_free(key); 134 GNUNET_free (key);
133 key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE); 135 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
134 GNUNET_assert(NULL != key); 136 GNUNET_assert (NULL != key);
135 GNUNET_CRYPTO_eddsa_key_get_public(key, &p2); 137 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
136 GNUNET_assert(0 == memcmp(&p1, &p2, sizeof(p1))); 138 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof(p1)));
137 GNUNET_free(key); 139 GNUNET_free (key);
138 GNUNET_assert(0 == unlink(KEYFILE)); 140 GNUNET_assert (0 == unlink (KEYFILE));
139 key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE); 141 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
140 GNUNET_assert(NULL != key); 142 GNUNET_assert (NULL != key);
141 GNUNET_CRYPTO_eddsa_key_get_public(key, &p2); 143 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
142 GNUNET_assert(0 != memcmp(&p1, &p2, sizeof(p1))); 144 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof(p1)));
143 GNUNET_free(key); 145 GNUNET_free (key);
144 return GNUNET_OK; 146 return GNUNET_OK;
145} 147}
146 148
147 149
148static void 150static void
149perf_keygen() 151perf_keygen ()
150{ 152{
151 struct GNUNET_TIME_Absolute start; 153 struct GNUNET_TIME_Absolute start;
152 struct GNUNET_CRYPTO_EddsaPrivateKey *pk; 154 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
153 int i; 155 int i;
154 156
155 fprintf(stderr, "%s", "W"); 157 fprintf (stderr, "%s", "W");
156 start = GNUNET_TIME_absolute_get(); 158 start = GNUNET_TIME_absolute_get ();
157 for (i = 0; i < 10; i++) 159 for (i = 0; i < 10; i++)
158 { 160 {
159 fprintf(stderr, "."); fflush(stderr); 161 fprintf (stderr, "."); fflush (stderr);
160 pk = GNUNET_CRYPTO_eddsa_key_create(); 162 pk = GNUNET_CRYPTO_eddsa_key_create ();
161 GNUNET_free(pk); 163 GNUNET_free (pk);
162 } 164 }
163 for (; i < 25; i++) 165 for (; i < 25; i++)
164 fprintf(stderr, "."); 166 fprintf (stderr, ".");
165 fflush(stderr); 167 fflush (stderr);
166 printf("10 EdDSA keys created in %s\n", 168 printf ("10 EdDSA keys created in %s\n",
167 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES)); 169 GNUNET_STRINGS_relative_time_to_string (
170 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
168} 171}
169 172
170 173
171int 174int
172main(int argc, char *argv[]) 175main (int argc, char *argv[])
173{ 176{
174 int failure_count = 0; 177 int failure_count = 0;
175 178
176 if (!gcry_check_version("1.6.0")) 179 if (! gcry_check_version ("1.6.0"))
177 { 180 {
178 fprintf(stderr, 181 fprintf (stderr,
179 _("libgcrypt has not the expected version (version %s is required).\n"), 182 _ (
180 "1.6.0"); 183 "libgcrypt has not the expected version (version %s is required).\n"),
181 return 0; 184 "1.6.0");
182 } 185 return 0;
183 if (getenv("GNUNET_GCRYPT_DEBUG")) 186 }
184 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 187 if (getenv ("GNUNET_GCRYPT_DEBUG"))
185 GNUNET_log_setup("test-crypto-eddsa", "WARNING", NULL); 188 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
186 key = GNUNET_CRYPTO_eddsa_key_create(); 189 GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
190 key = GNUNET_CRYPTO_eddsa_key_create ();
187#if PERF 191#if PERF
188 if (GNUNET_OK != testSignPerformance()) 192 if (GNUNET_OK != testSignPerformance ())
189 failure_count++; 193 failure_count++;
190#endif 194#endif
191 if (GNUNET_OK != testSignVerify()) 195 if (GNUNET_OK != testSignVerify ())
192 failure_count++; 196 failure_count++;
193 GNUNET_free(key); 197 GNUNET_free (key);
194 if (GNUNET_OK != testCreateFromFile()) 198 if (GNUNET_OK != testCreateFromFile ())
195 failure_count++; 199 failure_count++;
196 GNUNET_assert(0 == unlink(KEYFILE)); 200 GNUNET_assert (0 == unlink (KEYFILE));
197 perf_keygen(); 201 perf_keygen ();
198 202
199 if (0 != failure_count) 203 if (0 != failure_count)
200 { 204 {
201 fprintf(stderr, 205 fprintf (stderr,
202 "\n\n%d TESTS FAILED!\n\n", 206 "\n\n%d TESTS FAILED!\n\n",
203 failure_count); 207 failure_count);
204 return -1; 208 return -1;
205 } 209 }
206 return 0; 210 return 0;
207} 211}
208 212
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c
index 0e8246595..0118dd754 100644
--- a/src/util/test_crypto_hash.c
+++ b/src/util/test_crypto_hash.c
@@ -31,37 +31,37 @@ static char block[65536];
31#define FILENAME "testblock.dat" 31#define FILENAME "testblock.dat"
32 32
33static int 33static int
34test(int number) 34test (int number)
35{ 35{
36 struct GNUNET_HashCode h1; 36 struct GNUNET_HashCode h1;
37 struct GNUNET_HashCode h2; 37 struct GNUNET_HashCode h2;
38 struct GNUNET_CRYPTO_HashAsciiEncoded enc; 38 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
39 39
40 memset(&h1, number, sizeof(struct GNUNET_HashCode)); 40 memset (&h1, number, sizeof(struct GNUNET_HashCode));
41 GNUNET_CRYPTO_hash_to_enc(&h1, &enc); 41 GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
42 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string((char *)&enc, &h2)) 42 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
43 { 43 {
44 printf("enc2hash failed!\n"); 44 printf ("enc2hash failed!\n");
45 return 1; 45 return 1;
46 } 46 }
47 if (0 != memcmp(&h1, &h2, sizeof(struct GNUNET_HashCode))) 47 if (0 != memcmp (&h1, &h2, sizeof(struct GNUNET_HashCode)))
48 return 1; 48 return 1;
49 return 0; 49 return 0;
50} 50}
51 51
52static int 52static int
53testEncoding() 53testEncoding ()
54{ 54{
55 int i; 55 int i;
56 56
57 for (i = 0; i < 255; i++) 57 for (i = 0; i < 255; i++)
58 if (0 != test(i)) 58 if (0 != test (i))
59 return 1; 59 return 1;
60 return 0; 60 return 0;
61} 61}
62 62
63static int 63static int
64testArithmetic() 64testArithmetic ()
65{ 65{
66 struct GNUNET_HashCode h1; 66 struct GNUNET_HashCode h1;
67 struct GNUNET_HashCode h2; 67 struct GNUNET_HashCode h2;
@@ -70,43 +70,43 @@ testArithmetic()
70 struct GNUNET_CRYPTO_SymmetricSessionKey skey; 70 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
71 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 71 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
72 72
73 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &h1); 73 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
74 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &h2); 74 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
75 if (GNUNET_CRYPTO_hash_distance_u32(&h1, &h2) != 75 if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
76 GNUNET_CRYPTO_hash_distance_u32(&h2, &h1)) 76 GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
77 return 1; 77 return 1;
78 GNUNET_CRYPTO_hash_difference(&h1, &h2, &d); 78 GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
79 GNUNET_CRYPTO_hash_sum(&h1, &d, &s); 79 GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
80 if (0 != GNUNET_CRYPTO_hash_cmp(&s, &h2)) 80 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
81 return 1; 81 return 1;
82 GNUNET_CRYPTO_hash_xor(&h1, &h2, &d); 82 GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
83 GNUNET_CRYPTO_hash_xor(&h1, &d, &s); 83 GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
84 if (0 != GNUNET_CRYPTO_hash_cmp(&s, &h2)) 84 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
85 return 1; 85 return 1;
86 if (0 != GNUNET_CRYPTO_hash_xorcmp(&s, &h2, &h1)) 86 if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
87 return 1; 87 return 1;
88 if (-1 != GNUNET_CRYPTO_hash_xorcmp(&h1, &h2, &h1)) 88 if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
89 return 1; 89 return 1;
90 if (1 != GNUNET_CRYPTO_hash_xorcmp(&h1, &h2, &h2)) 90 if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
91 return 1; 91 return 1;
92 memset(&d, 0xF0, sizeof(d)); 92 memset (&d, 0xF0, sizeof(d));
93 if (0 != GNUNET_CRYPTO_hash_get_bit(&d, 3)) 93 if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
94 return 1; 94 return 1;
95 if (1 != GNUNET_CRYPTO_hash_get_bit(&d, 6)) 95 if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
96 return 1; 96 return 1;
97 memset(&d, 0, sizeof(d)); 97 memset (&d, 0, sizeof(d));
98 GNUNET_CRYPTO_hash_to_aes_key(&d, &skey, &iv); 98 GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
99 return 0; 99 return 0;
100} 100}
101 101
102static void 102static void
103finished_task(void *cls, const struct GNUNET_HashCode * res) 103finished_task (void *cls, const struct GNUNET_HashCode *res)
104{ 104{
105 int *ret = cls; 105 int *ret = cls;
106 struct GNUNET_HashCode want; 106 struct GNUNET_HashCode want;
107 107
108 GNUNET_CRYPTO_hash(block, sizeof(block), &want); 108 GNUNET_CRYPTO_hash (block, sizeof(block), &want);
109 if (0 != memcmp(res, &want, sizeof(want))) 109 if (0 != memcmp (res, &want, sizeof(want)))
110 *ret = 2; 110 *ret = 2;
111 else 111 else
112 *ret = 0; 112 *ret = 0;
@@ -114,43 +114,43 @@ finished_task(void *cls, const struct GNUNET_HashCode * res)
114 114
115 115
116static void 116static void
117file_hasher(void *cls) 117file_hasher (void *cls)
118{ 118{
119 GNUNET_assert(NULL != 119 GNUNET_assert (NULL !=
120 GNUNET_CRYPTO_hash_file(GNUNET_SCHEDULER_PRIORITY_DEFAULT, 120 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
121 FILENAME, 1024, &finished_task, cls)); 121 FILENAME, 1024, &finished_task, cls));
122} 122}
123 123
124 124
125static int 125static int
126testFileHash() 126testFileHash ()
127{ 127{
128 int ret; 128 int ret;
129 FILE *f; 129 FILE *f;
130 130
131 memset(block, 42, sizeof(block) / 2); 131 memset (block, 42, sizeof(block) / 2);
132 memset(&block[sizeof(block) / 2], 43, sizeof(block) / 2); 132 memset (&block[sizeof(block) / 2], 43, sizeof(block) / 2);
133 GNUNET_assert(NULL != (f = fopen(FILENAME, "w+"))); 133 GNUNET_assert (NULL != (f = fopen (FILENAME, "w+")));
134 GNUNET_break(sizeof(block) == fwrite(block, 1, sizeof(block), f)); 134 GNUNET_break (sizeof(block) == fwrite (block, 1, sizeof(block), f));
135 GNUNET_break(0 == fclose(f)); 135 GNUNET_break (0 == fclose (f));
136 ret = 1; 136 ret = 1;
137 GNUNET_SCHEDULER_run(&file_hasher, &ret); 137 GNUNET_SCHEDULER_run (&file_hasher, &ret);
138 GNUNET_break(0 == unlink(FILENAME)); 138 GNUNET_break (0 == unlink (FILENAME));
139 return ret; 139 return ret;
140} 140}
141 141
142 142
143int 143int
144main(int argc, char *argv[]) 144main (int argc, char *argv[])
145{ 145{
146 int failureCount = 0; 146 int failureCount = 0;
147 int i; 147 int i;
148 148
149 GNUNET_log_setup("test-crypto-hash", "WARNING", NULL); 149 GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
150 for (i = 0; i < 10; i++) 150 for (i = 0; i < 10; i++)
151 failureCount += testEncoding(); 151 failureCount += testEncoding ();
152 failureCount += testArithmetic(); 152 failureCount += testArithmetic ();
153 failureCount += testFileHash(); 153 failureCount += testFileHash ();
154 if (failureCount != 0) 154 if (failureCount != 0)
155 return 1; 155 return 1;
156 return 0; 156 return 0;
diff --git a/src/util/test_crypto_hash_context.c b/src/util/test_crypto_hash_context.c
index 778b70eec..d744b52b4 100644
--- a/src/util/test_crypto_hash_context.c
+++ b/src/util/test_crypto_hash_context.c
@@ -27,23 +27,22 @@
27 27
28#define LEN 1234 28#define LEN 1234
29 29
30int main() 30int main ()
31{ 31{
32 char data[1234]; 32 char data[1234];
33 struct GNUNET_HashCode hc1; 33 struct GNUNET_HashCode hc1;
34 struct GNUNET_HashCode hc2; 34 struct GNUNET_HashCode hc2;
35 struct GNUNET_HashContext *hctx; 35 struct GNUNET_HashContext *hctx;
36 36
37 memset(data, 42, LEN); 37 memset (data, 42, LEN);
38 38
39 hctx = GNUNET_CRYPTO_hash_context_start(); 39 hctx = GNUNET_CRYPTO_hash_context_start ();
40 GNUNET_CRYPTO_hash_context_read(hctx, data, LEN); 40 GNUNET_CRYPTO_hash_context_read (hctx, data, LEN);
41 GNUNET_CRYPTO_hash_context_finish(hctx, &hc1); 41 GNUNET_CRYPTO_hash_context_finish (hctx, &hc1);
42 42
43 GNUNET_CRYPTO_hash(data, LEN, &hc2); 43 GNUNET_CRYPTO_hash (data, LEN, &hc2);
44 44
45 if (0 == memcmp(&hc1, &hc2, sizeof(struct GNUNET_HashCode))) 45 if (0 == memcmp (&hc1, &hc2, sizeof(struct GNUNET_HashCode)))
46 return 0; 46 return 0;
47 return 1; 47 return 1;
48} 48}
49
diff --git a/src/util/test_crypto_hkdf.c b/src/util/test_crypto_hkdf.c
index d4829791d..c41c3362b 100644
--- a/src/util/test_crypto_hkdf.c
+++ b/src/util/test_crypto_hkdf.c
@@ -33,7 +33,7 @@
33#include "gnunet_crypto_lib.h" 33#include "gnunet_crypto_lib.h"
34 34
35void 35void
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,
@@ -51,17 +51,17 @@ tc1()
51 unsigned char result[44]; 51 unsigned char result[44];
52 int l = 42; 52 int l = 42;
53 53
54 memset(result, 0, sizeof(result)); 54 memset (result, 0, sizeof(result));
55 GNUNET_assert(GNUNET_CRYPTO_hkdf 55 GNUNET_assert (GNUNET_CRYPTO_hkdf
56 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, 56 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt,
57 sizeof(salt), ikm, sizeof(ikm), info, sizeof(info), 57 sizeof(salt), ikm, sizeof(ikm), info, sizeof(info),
58 NULL) == GNUNET_YES); 58 NULL) == GNUNET_YES);
59 GNUNET_assert(memcmp(result, okm, l) == 0); 59 GNUNET_assert (memcmp (result, okm, l) == 0);
60 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 60 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
61} 61}
62 62
63void 63void
64tc2() 64tc2 ()
65{ 65{
66 unsigned char ikm[80] = 66 unsigned char ikm[80] =
67 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 67 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
@@ -98,17 +98,17 @@ tc2()
98 char result[84]; 98 char result[84];
99 int l = 82; 99 int l = 82;
100 100
101 memset(result, 0, sizeof(result)); 101 memset (result, 0, sizeof(result));
102 GNUNET_assert(GNUNET_CRYPTO_hkdf 102 GNUNET_assert (GNUNET_CRYPTO_hkdf
103 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt, 103 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, salt,
104 sizeof(salt), ikm, sizeof(ikm), info, sizeof(info), 104 sizeof(salt), ikm, sizeof(ikm), info, sizeof(info),
105 NULL) == GNUNET_YES); 105 NULL) == GNUNET_YES);
106 GNUNET_assert(memcmp(result, okm, l) == 0); 106 GNUNET_assert (memcmp (result, okm, l) == 0);
107 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 107 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
108} 108}
109 109
110void 110void
111tc3() 111tc3 ()
112{ 112{
113 unsigned char ikm[22] = 113 unsigned char ikm[22] =
114 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 114 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
@@ -121,16 +121,16 @@ tc3()
121 unsigned char result[44]; 121 unsigned char result[44];
122 int l = 42; 122 int l = 42;
123 123
124 memset(result, 0, sizeof(result)); 124 memset (result, 0, sizeof(result));
125 GNUNET_assert(GNUNET_CRYPTO_hkdf 125 GNUNET_assert (GNUNET_CRYPTO_hkdf
126 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, NULL, 0, ikm, 126 (result, l, GCRY_MD_SHA256, GCRY_MD_SHA256, NULL, 0, ikm,
127 sizeof(ikm), NULL, 0, NULL) == GNUNET_YES); 127 sizeof(ikm), NULL, 0, NULL) == GNUNET_YES);
128 GNUNET_assert(memcmp(result, okm, l) == 0); 128 GNUNET_assert (memcmp (result, okm, l) == 0);
129 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 129 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
130} 130}
131 131
132void 132void
133tc4() 133tc4 ()
134{ 134{
135 unsigned char ikm[11] = 135 unsigned char ikm[11] =
136 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 136 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
@@ -148,16 +148,16 @@ tc4()
148 char result[84]; 148 char result[84];
149 int l = 42; 149 int l = 42;
150 150
151 memset(result, 0, sizeof(result)); 151 memset (result, 0, sizeof(result));
152 GNUNET_assert(GNUNET_CRYPTO_hkdf 152 GNUNET_assert (GNUNET_CRYPTO_hkdf
153 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof(salt), 153 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof(salt),
154 ikm, sizeof(ikm), info, sizeof(info), NULL) == GNUNET_YES); 154 ikm, sizeof(ikm), info, sizeof(info), NULL) == GNUNET_YES);
155 GNUNET_assert(memcmp(result, okm, l) == 0); 155 GNUNET_assert (memcmp (result, okm, l) == 0);
156 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 156 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
157} 157}
158 158
159void 159void
160tc5() 160tc5 ()
161{ 161{
162 unsigned char ikm[80] = 162 unsigned char ikm[80] =
163 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 163 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
@@ -194,16 +194,16 @@ tc5()
194 char result[84]; 194 char result[84];
195 int l = 82; 195 int l = 82;
196 196
197 memset(result, 0, sizeof(result)); 197 memset (result, 0, sizeof(result));
198 GNUNET_assert(GNUNET_CRYPTO_hkdf 198 GNUNET_assert (GNUNET_CRYPTO_hkdf
199 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof(salt), 199 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof(salt),
200 ikm, sizeof(ikm), info, sizeof(info), NULL) == GNUNET_YES); 200 ikm, sizeof(ikm), info, sizeof(info), NULL) == GNUNET_YES);
201 GNUNET_assert(memcmp(result, okm, l) == 0); 201 GNUNET_assert (memcmp (result, okm, l) == 0);
202 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 202 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
203} 203}
204 204
205void 205void
206tc6() 206tc6 ()
207{ 207{
208 unsigned char ikm[22] = 208 unsigned char ikm[22] =
209 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 209 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
@@ -216,16 +216,16 @@ tc6()
216 char result[44]; 216 char result[44];
217 int l = 42; 217 int l = 42;
218 218
219 memset(result, 0, sizeof(result)); 219 memset (result, 0, sizeof(result));
220 GNUNET_assert(GNUNET_CRYPTO_hkdf 220 GNUNET_assert (GNUNET_CRYPTO_hkdf
221 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm, 221 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, NULL, 0, ikm,
222 sizeof(ikm), NULL, 0, NULL) == GNUNET_YES); 222 sizeof(ikm), NULL, 0, NULL) == GNUNET_YES);
223 GNUNET_assert(memcmp(result, okm, l) == 0); 223 GNUNET_assert (memcmp (result, okm, l) == 0);
224 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 224 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
225} 225}
226 226
227void 227void
228tc7() 228tc7 ()
229{ 229{
230 unsigned char ikm[80] = 230 unsigned char ikm[80] =
231 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 231 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
@@ -244,13 +244,18 @@ tc7()
244 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 244 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
245 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf }; 245 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf };
246 unsigned char info1[34] = { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 246 unsigned char info1[34] = { 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
247 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 247 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
248 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 248 0xc0, 0xc1, 0xc2, 0xc3,
249 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
250 0xcc, 0xcd, 0xce, 0xcf,
249 0xd0, 0xd1 }; 251 0xd0, 0xd1 };
250 unsigned char info2[46] = { 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 252 unsigned char info2[46] = { 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
251 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 253 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1,
252 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 254 0xe2, 0xe3, 0xe4, 0xe5,
253 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 255 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed,
256 0xee, 0xef, 0xf0, 0xf1,
257 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
258 0xfa, 0xfb, 0xfc, 0xfd,
254 0xfe, 0xff }; 259 0xfe, 0xff };
255 unsigned char okm[82] = 260 unsigned char okm[82] =
256 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 261 { 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1,
@@ -263,17 +268,17 @@ tc7()
263 char result[84]; 268 char result[84];
264 int l = 82; 269 int l = 82;
265 270
266 memset(result, 0, sizeof(result)); 271 memset (result, 0, sizeof(result));
267 GNUNET_assert(GNUNET_CRYPTO_hkdf 272 GNUNET_assert (GNUNET_CRYPTO_hkdf
268 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof(salt), 273 (result, l, GCRY_MD_SHA1, GCRY_MD_SHA1, salt, sizeof(salt),
269 ikm, sizeof(ikm), info1, sizeof(info1), info2, 274 ikm, sizeof(ikm), info1, sizeof(info1), info2,
270 sizeof(info2), NULL) == GNUNET_YES); 275 sizeof(info2), NULL) == GNUNET_YES);
271 GNUNET_assert(memcmp(result, okm, l) == 0); 276 GNUNET_assert (memcmp (result, okm, l) == 0);
272 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 277 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
273} 278}
274 279
275void 280void
276tc8() 281tc8 ()
277{ 282{
278 unsigned char ikm[32] = 283 unsigned char ikm[32] =
279 { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72, 284 { 0xbf, 0x16, 0x6e, 0x46, 0x3a, 0x6c, 0xf3, 0x93, 0xa7, 0x72,
@@ -295,31 +300,31 @@ tc8()
295 char result[18]; 300 char result[18];
296 int l = 16; 301 int l = 16;
297 302
298 memset(result, 0, sizeof(result)); 303 memset (result, 0, sizeof(result));
299 GNUNET_assert(GNUNET_CRYPTO_hkdf 304 GNUNET_assert (GNUNET_CRYPTO_hkdf
300 (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt, 305 (result, l, GCRY_MD_SHA512, GCRY_MD_SHA256, salt,
301 sizeof(salt), ikm, sizeof(ikm), info, sizeof(info), 306 sizeof(salt), ikm, sizeof(ikm), info, sizeof(info),
302 NULL) == GNUNET_YES); 307 NULL) == GNUNET_YES);
303 GNUNET_assert(memcmp(result, okm, l) == 0); 308 GNUNET_assert (memcmp (result, okm, l) == 0);
304 GNUNET_assert(memcmp(result + l, "\0", 2) == 0); 309 GNUNET_assert (memcmp (result + l, "\0", 2) == 0);
305} 310}
306 311
307int 312int
308main() 313main ()
309{ 314{
310 GNUNET_log_setup("test-crypto-hkdf", "WARNING", NULL); 315 GNUNET_log_setup ("test-crypto-hkdf", "WARNING", NULL);
311 316
312 /* Official test vectors */ 317 /* Official test vectors */
313 tc1(); 318 tc1 ();
314 tc2(); 319 tc2 ();
315 tc3(); 320 tc3 ();
316 tc4(); 321 tc4 ();
317 tc5(); 322 tc5 ();
318 tc6(); 323 tc6 ();
319 324
320 /* Additional tests */ 325 /* Additional tests */
321 tc7(); 326 tc7 ();
322 tc8(); 327 tc8 ();
323 328
324 return 0; 329 return 0;
325} 330}
diff --git a/src/util/test_crypto_kdf.c b/src/util/test_crypto_kdf.c
index a5c9bba20..7c33e0ba8 100644
--- a/src/util/test_crypto_kdf.c
+++ b/src/util/test_crypto_kdf.c
@@ -33,39 +33,39 @@
33 33
34 34
35int 35int
36main() 36main ()
37{ 37{
38#define RND_BLK_SIZE 4096 38#define RND_BLK_SIZE 4096
39 unsigned char rnd_blk[RND_BLK_SIZE]; 39 unsigned char rnd_blk[RND_BLK_SIZE];
40 int i; 40 int i;
41 gcry_mpi_t r, n; 41 gcry_mpi_t r, n;
42 42
43 GNUNET_log_setup("test-crypto-kdf", "WARNING", NULL); 43 GNUNET_log_setup ("test-crypto-kdf", "WARNING", NULL);
44 44
45 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, 45 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
46 rnd_blk, 46 rnd_blk,
47 RND_BLK_SIZE); 47 RND_BLK_SIZE);
48 48
49 /* test full domain hash size */ 49 /* test full domain hash size */
50 for (i = 0; i < 100; i++) 50 for (i = 0; i < 100; i++)
51 { 51 {
52 gcry_mpi_scan(&n, 52 gcry_mpi_scan (&n,
53 GCRYMPI_FMT_USG, 53 GCRYMPI_FMT_USG,
54 rnd_blk, RND_BLK_SIZE, 54 rnd_blk, RND_BLK_SIZE,
55 NULL); 55 NULL);
56 GNUNET_CRYPTO_kdf_mod_mpi(&r, n, 56 GNUNET_CRYPTO_kdf_mod_mpi (&r, n,
57 "", 0, 57 "", 0,
58 "", 0, 58 "", 0,
59 ""); 59 "");
60 GNUNET_assert(0 > gcry_mpi_cmp(r, n)); 60 GNUNET_assert (0 > gcry_mpi_cmp (r, n));
61 61
62 /* Is it worth checking that it's not too small? */ 62 /* Is it worth checking that it's not too small? */
63 /* GNUNET_assert (gcry_mpi_get_nbits(r) > 3*RND_BLK_SIZE/4); */ 63 /* GNUNET_assert (gcry_mpi_get_nbits(r) > 3*RND_BLK_SIZE/4); */
64 /* This test necessarily randomly fails with probability 2^(3 - RND_BLK_SIZE/4) */ 64 /* This test necessarily randomly fails with probability 2^(3 - RND_BLK_SIZE/4) */
65 65
66 gcry_mpi_release(n); 66 gcry_mpi_release (n);
67 gcry_mpi_release(r); 67 gcry_mpi_release (r);
68 } 68 }
69 69
70 return 0; 70 return 0;
71} 71}
diff --git a/src/util/test_crypto_paillier.c b/src/util/test_crypto_paillier.c
index c4555fd10..755d0ff38 100644
--- a/src/util/test_crypto_paillier.c
+++ b/src/util/test_crypto_paillier.c
@@ -30,7 +30,7 @@
30 30
31 31
32static int 32static int
33test_crypto() 33test_crypto ()
34{ 34{
35 gcry_mpi_t plaintext; 35 gcry_mpi_t plaintext;
36 gcry_mpi_t plaintext_result; 36 gcry_mpi_t plaintext_result;
@@ -39,43 +39,43 @@ test_crypto()
39 struct GNUNET_CRYPTO_PaillierPrivateKey private_key; 39 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
40 int ret = 0; 40 int ret = 0;
41 41
42 GNUNET_CRYPTO_paillier_create(&public_key, 42 GNUNET_CRYPTO_paillier_create (&public_key,
43 &private_key); 43 &private_key);
44 GNUNET_assert(NULL != (plaintext = gcry_mpi_new(0))); 44 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
45 GNUNET_assert(NULL != (plaintext_result = gcry_mpi_new(0))); 45 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
46 gcry_mpi_randomize(plaintext, 46 gcry_mpi_randomize (plaintext,
47 GNUNET_CRYPTO_PAILLIER_BITS / 2, 47 GNUNET_CRYPTO_PAILLIER_BITS / 2,
48 GCRY_WEAK_RANDOM); 48 GCRY_WEAK_RANDOM);
49 49
50 GNUNET_CRYPTO_paillier_encrypt(&public_key, 50 GNUNET_CRYPTO_paillier_encrypt (&public_key,
51 plaintext, 51 plaintext,
52 0 /* 0 hom ops */, 52 0 /* 0 hom ops */,
53 &ciphertext); 53 &ciphertext);
54 GNUNET_CRYPTO_paillier_decrypt(&private_key, 54 GNUNET_CRYPTO_paillier_decrypt (&private_key,
55 &public_key, 55 &public_key,
56 &ciphertext, 56 &ciphertext,
57 plaintext_result); 57 plaintext_result);
58 if (0 != gcry_mpi_cmp(plaintext, 58 if (0 != gcry_mpi_cmp (plaintext,
59 plaintext_result)) 59 plaintext_result))
60 { 60 {
61 fprintf(stderr, 61 fprintf (stderr,
62 "Paillier decryption failed with plaintext of size %u\n", 62 "Paillier decryption failed with plaintext of size %u\n",
63 gcry_mpi_get_nbits(plaintext)); 63 gcry_mpi_get_nbits (plaintext));
64 gcry_log_debugmpi("\n", 64 gcry_log_debugmpi ("\n",
65 plaintext); 65 plaintext);
66 gcry_log_debugmpi("\n", 66 gcry_log_debugmpi ("\n",
67 plaintext_result); 67 plaintext_result);
68 ret = 1; 68 ret = 1;
69 } 69 }
70 gcry_mpi_release(plaintext); 70 gcry_mpi_release (plaintext);
71 gcry_mpi_release(plaintext_result); 71 gcry_mpi_release (plaintext_result);
72 return ret; 72 return ret;
73} 73}
74 74
75 75
76static int 76static int
77test_hom_simple(unsigned int a, 77test_hom_simple (unsigned int a,
78 unsigned int b) 78 unsigned int b)
79{ 79{
80 gcry_mpi_t m1; 80 gcry_mpi_t m1;
81 gcry_mpi_t m2; 81 gcry_mpi_t m2;
@@ -88,52 +88,52 @@ test_hom_simple(unsigned int a,
88 struct GNUNET_CRYPTO_PaillierPrivateKey private_key; 88 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
89 int ret = 0; 89 int ret = 0;
90 90
91 GNUNET_CRYPTO_paillier_create(&public_key, 91 GNUNET_CRYPTO_paillier_create (&public_key,
92 &private_key); 92 &private_key);
93 93
94 GNUNET_assert(NULL != (m1 = gcry_mpi_new(0))); 94 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
95 GNUNET_assert(NULL != (m2 = gcry_mpi_new(0))); 95 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
96 GNUNET_assert(NULL != (result = gcry_mpi_new(0))); 96 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
97 GNUNET_assert(NULL != (hom_result = gcry_mpi_new(0))); 97 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
98 m1 = gcry_mpi_set_ui(m1, a); 98 m1 = gcry_mpi_set_ui (m1, a);
99 m2 = gcry_mpi_set_ui(m2, b); 99 m2 = gcry_mpi_set_ui (m2, b);
100 gcry_mpi_add(result, 100 gcry_mpi_add (result,
101 m1, 101 m1,
102 m2); 102 m2);
103 GNUNET_CRYPTO_paillier_encrypt(&public_key, 103 GNUNET_CRYPTO_paillier_encrypt (&public_key,
104 m1, 104 m1,
105 2, 105 2,
106 &c1); 106 &c1);
107 GNUNET_CRYPTO_paillier_encrypt(&public_key, 107 GNUNET_CRYPTO_paillier_encrypt (&public_key,
108 m2, 108 m2,
109 2, 109 2,
110 &c2); 110 &c2);
111 GNUNET_CRYPTO_paillier_hom_add(&public_key, 111 GNUNET_CRYPTO_paillier_hom_add (&public_key,
112 &c1, 112 &c1,
113 &c2, 113 &c2,
114 &c_result); 114 &c_result);
115 GNUNET_CRYPTO_paillier_decrypt(&private_key, 115 GNUNET_CRYPTO_paillier_decrypt (&private_key,
116 &public_key, 116 &public_key,
117 &c_result, 117 &c_result,
118 hom_result); 118 hom_result);
119 if (0 != gcry_mpi_cmp(result, hom_result)) 119 if (0 != gcry_mpi_cmp (result, hom_result))
120 { 120 {
121 fprintf(stderr, 121 fprintf (stderr,
122 "GNUNET_CRYPTO_paillier failed simple math!\n"); 122 "GNUNET_CRYPTO_paillier failed simple math!\n");
123 gcry_log_debugmpi("got ", hom_result); 123 gcry_log_debugmpi ("got ", hom_result);
124 gcry_log_debugmpi("wanted ", result); 124 gcry_log_debugmpi ("wanted ", result);
125 ret = 1; 125 ret = 1;
126 } 126 }
127 gcry_mpi_release(m1); 127 gcry_mpi_release (m1);
128 gcry_mpi_release(m2); 128 gcry_mpi_release (m2);
129 gcry_mpi_release(result); 129 gcry_mpi_release (result);
130 gcry_mpi_release(hom_result); 130 gcry_mpi_release (hom_result);
131 return ret; 131 return ret;
132} 132}
133 133
134 134
135static int 135static int
136test_hom() 136test_hom ()
137{ 137{
138 int ret; 138 int ret;
139 gcry_mpi_t m1; 139 gcry_mpi_t m1;
@@ -146,100 +146,100 @@ test_hom()
146 struct GNUNET_CRYPTO_PaillierPublicKey public_key; 146 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
147 struct GNUNET_CRYPTO_PaillierPrivateKey private_key; 147 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
148 148
149 GNUNET_CRYPTO_paillier_create(&public_key, 149 GNUNET_CRYPTO_paillier_create (&public_key,
150 &private_key); 150 &private_key);
151 151
152 GNUNET_assert(NULL != (m1 = gcry_mpi_new(0))); 152 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
153 GNUNET_assert(NULL != (m2 = gcry_mpi_new(0))); 153 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
154 GNUNET_assert(NULL != (result = gcry_mpi_new(0))); 154 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
155 GNUNET_assert(NULL != (hom_result = gcry_mpi_new(0))); 155 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
156 m1 = gcry_mpi_set_ui(m1, 1); 156 m1 = gcry_mpi_set_ui (m1, 1);
157 /* m1 = m1 * 2 ^ (GCPB - 3) */ 157 /* m1 = m1 * 2 ^ (GCPB - 3) */
158 gcry_mpi_mul_2exp(m1, 158 gcry_mpi_mul_2exp (m1,
159 m1, 159 m1,
160 GNUNET_CRYPTO_PAILLIER_BITS - 3); 160 GNUNET_CRYPTO_PAILLIER_BITS - 3);
161 m2 = gcry_mpi_set_ui(m2, 15); 161 m2 = gcry_mpi_set_ui (m2, 15);
162 /* m1 = m1 * 2 ^ (GCPB / 2) */ 162 /* m1 = m1 * 2 ^ (GCPB / 2) */
163 gcry_mpi_mul_2exp(m2, 163 gcry_mpi_mul_2exp (m2,
164 m2, 164 m2,
165 GNUNET_CRYPTO_PAILLIER_BITS / 2); 165 GNUNET_CRYPTO_PAILLIER_BITS / 2);
166 gcry_mpi_add(result, 166 gcry_mpi_add (result,
167 m1, 167 m1,
168 m2); 168 m2);
169 169
170 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt(&public_key, 170 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
171 m1, 171 m1,
172 2, 172 2,
173 &c1))) 173 &c1)))
174 { 174 {
175 fprintf(stderr, 175 fprintf (stderr,
176 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n", 176 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n",
177 ret); 177 ret);
178 ret = 1; 178 ret = 1;
179 goto out; 179 goto out;
180 } 180 }
181 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt(&public_key, 181 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
182 m2, 182 m2,
183 2, 183 2,
184 &c2))) 184 &c2)))
185 { 185 {
186 fprintf(stderr, 186 fprintf (stderr,
187 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n", 187 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n",
188 ret); 188 ret);
189 ret = 1; 189 ret = 1;
190 goto out; 190 goto out;
191 } 191 }
192 192
193 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add(&public_key, 193 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key,
194 &c1, 194 &c1,
195 &c2, 195 &c2,
196 &c_result))) 196 &c_result)))
197 { 197 {
198 fprintf(stderr, 198 fprintf (stderr,
199 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n", 199 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n",
200 ret); 200 ret);
201 ret = 1; 201 ret = 1;
202 goto out; 202 goto out;
203 } 203 }
204 204
205 GNUNET_CRYPTO_paillier_decrypt(&private_key, 205 GNUNET_CRYPTO_paillier_decrypt (&private_key,
206 &public_key, 206 &public_key,
207 &c_result, 207 &c_result,
208 hom_result); 208 hom_result);
209 209
210 if (0 != gcry_mpi_cmp(result, hom_result)) 210 if (0 != gcry_mpi_cmp (result, hom_result))
211 { 211 {
212 fprintf(stderr, 212 fprintf (stderr,
213 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n"); 213 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n");
214 gcry_log_debugmpi("got", hom_result); 214 gcry_log_debugmpi ("got", hom_result);
215 gcry_log_debugmpi("wanted", result); 215 gcry_log_debugmpi ("wanted", result);
216 ret = 1; 216 ret = 1;
217 } 217 }
218out: 218out:
219 gcry_mpi_release(m1); 219 gcry_mpi_release (m1);
220 gcry_mpi_release(m2); 220 gcry_mpi_release (m2);
221 gcry_mpi_release(result); 221 gcry_mpi_release (result);
222 gcry_mpi_release(hom_result); 222 gcry_mpi_release (hom_result);
223 return ret; 223 return ret;
224} 224}
225 225
226 226
227int 227int
228main(int argc, 228main (int argc,
229 char *argv[]) 229 char *argv[])
230{ 230{
231 int ret; 231 int ret;
232 232
233 ret = test_crypto(); 233 ret = test_crypto ();
234 if (0 != ret) 234 if (0 != ret)
235 return ret; 235 return ret;
236 ret = test_hom_simple(2, 4); 236 ret = test_hom_simple (2, 4);
237 if (0 != ret) 237 if (0 != ret)
238 return ret; 238 return ret;
239 ret = test_hom_simple(13, 17); 239 ret = test_hom_simple (13, 17);
240 if (0 != ret) 240 if (0 != ret)
241 return ret; 241 return ret;
242 ret = test_hom(); 242 ret = test_hom ();
243 return ret; 243 return ret;
244} 244}
245 245
diff --git a/src/util/test_crypto_random.c b/src/util/test_crypto_random.c
index 11f87a25e..e0a8fb72a 100644
--- a/src/util/test_crypto_random.c
+++ b/src/util/test_crypto_random.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29static int 29static int
30test(enum GNUNET_CRYPTO_Quality mode) 30test (enum GNUNET_CRYPTO_Quality mode)
31{ 31{
32 int buf[1024]; 32 int buf[1024];
33 unsigned int *b2; 33 unsigned int *b2;
@@ -35,34 +35,34 @@ test(enum GNUNET_CRYPTO_Quality mode)
35 unsigned long long n; 35 unsigned long long n;
36 36
37 for (i = 0; i < 1024; i++) 37 for (i = 0; i < 1024; i++)
38 GNUNET_break(1024 > (buf[i] = GNUNET_CRYPTO_random_u32(mode, 1024))); 38 GNUNET_break (1024 > (buf[i] = GNUNET_CRYPTO_random_u32 (mode, 1024)));
39 for (i = 0; i < 10; i++) 39 for (i = 0; i < 10; i++)
40 {
41 b2 = GNUNET_CRYPTO_random_permute (mode, 1024);
42 if (0 == memcmp (b2, buf, sizeof(buf)))
40 { 43 {
41 b2 = GNUNET_CRYPTO_random_permute(mode, 1024); 44 fprintf (stderr, "%s", "!");
42 if (0 == memcmp(b2, buf, sizeof(buf))) 45 GNUNET_free (b2);
43 { 46 continue;
44 fprintf(stderr, "%s", "!");
45 GNUNET_free(b2);
46 continue;
47 }
48 GNUNET_free(b2);
49 break;
50 } 47 }
48 GNUNET_free (b2);
49 break;
50 }
51 if (i == 10) 51 if (i == 10)
52 return 1; /* virtually impossible... */ 52 return 1; /* virtually impossible... */
53 53
54 for (n = 10; n < 1024LL * 1024LL * 1024LL; n *= 10) 54 for (n = 10; n < 1024LL * 1024LL * 1024LL; n *= 10)
55 GNUNET_break(n > GNUNET_CRYPTO_random_u64(mode, n)); 55 GNUNET_break (n > GNUNET_CRYPTO_random_u64 (mode, n));
56 return 0; 56 return 0;
57} 57}
58 58
59int 59int
60main(int argc, char *argv[]) 60main (int argc, char *argv[])
61{ 61{
62 GNUNET_log_setup("test-crypto-random", "WARNING", NULL); 62 GNUNET_log_setup ("test-crypto-random", "WARNING", NULL);
63 if (0 != test(GNUNET_CRYPTO_QUALITY_WEAK)) 63 if (0 != test (GNUNET_CRYPTO_QUALITY_WEAK))
64 return 1; 64 return 1;
65 if (0 != test(GNUNET_CRYPTO_QUALITY_STRONG)) 65 if (0 != test (GNUNET_CRYPTO_QUALITY_STRONG))
66 return 1; 66 return 1;
67 67
68 return 0; 68 return 0;
diff --git a/src/util/test_crypto_rsa.c b/src/util/test_crypto_rsa.c
index 0d8827c84..423f1d6f1 100644
--- a/src/util/test_crypto_rsa.c
+++ b/src/util/test_crypto_rsa.c
@@ -32,8 +32,8 @@
32 32
33 33
34int 34int
35main(int argc, 35main (int argc,
36 char *argv[]) 36 char *argv[])
37{ 37{
38#define RND_BLK_SIZE 4096 38#define RND_BLK_SIZE 4096
39 unsigned char rnd_blk[RND_BLK_SIZE]; 39 unsigned char rnd_blk[RND_BLK_SIZE];
@@ -49,79 +49,79 @@ main(int argc,
49 char *blind_buf; 49 char *blind_buf;
50 size_t bsize; 50 size_t bsize;
51 51
52 GNUNET_log_setup("test-rsa", "WARNING", NULL); 52 GNUNET_log_setup ("test-rsa", "WARNING", NULL);
53 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, 53 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
54 rnd_blk, 54 rnd_blk,
55 RND_BLK_SIZE); 55 RND_BLK_SIZE);
56 GNUNET_CRYPTO_hash(rnd_blk, 56 GNUNET_CRYPTO_hash (rnd_blk,
57 RND_BLK_SIZE, 57 RND_BLK_SIZE,
58 &hash); 58 &hash);
59 priv = GNUNET_CRYPTO_rsa_private_key_create(KEY_SIZE); 59 priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
60 priv_copy = GNUNET_CRYPTO_rsa_private_key_dup(priv); 60 priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
61 GNUNET_assert(NULL != priv_copy); 61 GNUNET_assert (NULL != priv_copy);
62 GNUNET_assert(0 == GNUNET_CRYPTO_rsa_private_key_cmp(priv, priv_copy)); 62 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
63 pub = GNUNET_CRYPTO_rsa_private_key_get_public(priv); 63 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
64 64
65 /* Encoding */ 65 /* Encoding */
66 size_t size; 66 size_t size;
67 char *enc; 67 char *enc;
68 enc = NULL; 68 enc = NULL;
69 size = GNUNET_CRYPTO_rsa_private_key_encode(priv, &enc); 69 size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
70 70
71 /* Decoding */ 71 /* Decoding */
72 GNUNET_CRYPTO_rsa_private_key_free(priv); 72 GNUNET_CRYPTO_rsa_private_key_free (priv);
73 priv = NULL; 73 priv = NULL;
74 priv = GNUNET_CRYPTO_rsa_private_key_decode(enc, size); 74 priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
75 GNUNET_assert(NULL != priv); 75 GNUNET_assert (NULL != priv);
76 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, 76 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
77 enc, size); 77 enc, size);
78 GNUNET_assert(NULL == GNUNET_CRYPTO_rsa_private_key_decode(enc, size)); 78 GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
79 (void)fprintf(stderr, "The above warning is expected.\n"); 79 (void) fprintf (stderr, "The above warning is expected.\n");
80 GNUNET_free(enc); 80 GNUNET_free (enc);
81 81
82 /* try ordinary sig first */ 82 /* try ordinary sig first */
83 sig = GNUNET_CRYPTO_rsa_sign_fdh(priv, 83 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
84 &hash); 84 &hash);
85 sig_copy = GNUNET_CRYPTO_rsa_signature_dup(sig); 85 sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
86 GNUNET_assert(NULL != sig); 86 GNUNET_assert (NULL != sig);
87 GNUNET_assert(0 == GNUNET_CRYPTO_rsa_signature_cmp(sig, sig_copy)); 87 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
88 pub_copy = GNUNET_CRYPTO_rsa_public_key_dup(pub); 88 pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
89 GNUNET_assert(NULL != pub_copy); 89 GNUNET_assert (NULL != pub_copy);
90 GNUNET_assert(GNUNET_OK == 90 GNUNET_assert (GNUNET_OK ==
91 GNUNET_CRYPTO_rsa_verify(&hash, sig, pub_copy)); 91 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
92 /* corrupt our hash and see if the signature is still valid */ 92 /* corrupt our hash and see if the signature is still valid */
93 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, &hash, 93 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
94 sizeof(struct GNUNET_HashCode)); 94 sizeof(struct GNUNET_HashCode));
95 GNUNET_assert(GNUNET_OK != GNUNET_CRYPTO_rsa_verify(&hash, 95 GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
96 sig, 96 sig,
97 pub)); 97 pub));
98 (void)fprintf(stderr, "The above warning is expected.\n"); 98 (void) fprintf (stderr, "The above warning is expected.\n");
99 GNUNET_CRYPTO_rsa_signature_free(sig); 99 GNUNET_CRYPTO_rsa_signature_free (sig);
100 100
101 /* test blind signing */ 101 /* test blind signing */
102 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, 102 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
103 &bsec, 103 &bsec,
104 sizeof(bsec)); 104 sizeof(bsec));
105 GNUNET_CRYPTO_rsa_blind(&hash, 105 GNUNET_CRYPTO_rsa_blind (&hash,
106 &bsec, 106 &bsec,
107 pub, 107 pub,
108 &blind_buf, &bsize); 108 &blind_buf, &bsize);
109 GNUNET_assert(0 != bsize); 109 GNUNET_assert (0 != bsize);
110 bsig = GNUNET_CRYPTO_rsa_sign_blinded(priv, 110 bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
111 blind_buf, 111 blind_buf,
112 bsize); 112 bsize);
113 GNUNET_free(blind_buf); 113 GNUNET_free (blind_buf);
114 sig = GNUNET_CRYPTO_rsa_unblind(bsig, 114 sig = GNUNET_CRYPTO_rsa_unblind (bsig,
115 &bsec, 115 &bsec,
116 pub); 116 pub);
117 GNUNET_CRYPTO_rsa_signature_free(bsig); 117 GNUNET_CRYPTO_rsa_signature_free (bsig);
118 GNUNET_assert(GNUNET_OK == 118 GNUNET_assert (GNUNET_OK ==
119 GNUNET_CRYPTO_rsa_verify(&hash, sig, pub)); 119 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
120 GNUNET_CRYPTO_rsa_signature_free(sig); 120 GNUNET_CRYPTO_rsa_signature_free (sig);
121 GNUNET_CRYPTO_rsa_signature_free(sig_copy); 121 GNUNET_CRYPTO_rsa_signature_free (sig_copy);
122 GNUNET_CRYPTO_rsa_private_key_free(priv); 122 GNUNET_CRYPTO_rsa_private_key_free (priv);
123 GNUNET_CRYPTO_rsa_private_key_free(priv_copy); 123 GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
124 GNUNET_CRYPTO_rsa_public_key_free(pub); 124 GNUNET_CRYPTO_rsa_public_key_free (pub);
125 GNUNET_CRYPTO_rsa_public_key_free(pub_copy); 125 GNUNET_CRYPTO_rsa_public_key_free (pub_copy);
126 return 0; 126 return 0;
127} 127}
diff --git a/src/util/test_crypto_symmetric.c b/src/util/test_crypto_symmetric.c
index c6854711f..0d83d5aa1 100644
--- a/src/util/test_crypto_symmetric.c
+++ b/src/util/test_crypto_symmetric.c
@@ -30,71 +30,69 @@
30#define INITVALUE "InitializationVectorValueinitializationvectorvalue" 30#define INITVALUE "InitializationVectorValueinitializationvectorvalue"
31 31
32static int 32static int
33testSymcipher() 33testSymcipher ()
34{ 34{
35 struct GNUNET_CRYPTO_SymmetricSessionKey key; 35 struct GNUNET_CRYPTO_SymmetricSessionKey key;
36 char result[100]; 36 char result[100];
37 int size; 37 int size;
38 char res[100]; 38 char res[100];
39 39
40 GNUNET_CRYPTO_symmetric_create_session_key(&key); 40 GNUNET_CRYPTO_symmetric_create_session_key (&key);
41 size = 41 size =
42 GNUNET_CRYPTO_symmetric_encrypt(TESTSTRING, strlen(TESTSTRING) + 1, &key, 42 GNUNET_CRYPTO_symmetric_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key,
43 (const struct 43 (const struct
44 GNUNET_CRYPTO_SymmetricInitializationVector *) 44 GNUNET_CRYPTO_SymmetricInitializationVector
45 INITVALUE, result); 45 *)
46 INITVALUE, result);
46 if (size == -1) 47 if (size == -1)
47 { 48 {
48 printf("symciphertest failed: encryptBlock returned %d\n", size); 49 printf ("symciphertest failed: encryptBlock returned %d\n", size);
49 return 1; 50 return 1;
50 } 51 }
51 size = 52 size =
52 GNUNET_CRYPTO_symmetric_decrypt(result, size, &key, 53 GNUNET_CRYPTO_symmetric_decrypt (result, size, &key,
53 (const struct 54 (const struct
54 GNUNET_CRYPTO_SymmetricInitializationVector *) 55 GNUNET_CRYPTO_SymmetricInitializationVector
55 INITVALUE, res); 56 *)
56 if (strlen(TESTSTRING) + 1 != size) 57 INITVALUE, res);
57 { 58 if (strlen (TESTSTRING) + 1 != size)
58 printf("symciphertest failed: decryptBlock returned %d\n", size); 59 {
59 return 1; 60 printf ("symciphertest failed: decryptBlock returned %d\n", size);
60 } 61 return 1;
61 if (0 != strcmp(res, TESTSTRING)) 62 }
62 { 63 if (0 != strcmp (res, TESTSTRING))
63 printf("symciphertest failed: %s != %s\n", res, TESTSTRING); 64 {
64 return 1; 65 printf ("symciphertest failed: %s != %s\n", res, TESTSTRING);
65 } 66 return 1;
67 }
66 else 68 else
67 return 0; 69 return 0;
68} 70}
69 71
70 72
71static int 73static int
72verifyCrypto() 74verifyCrypto ()
73{ 75{
74 struct GNUNET_CRYPTO_SymmetricSessionKey key; 76 struct GNUNET_CRYPTO_SymmetricSessionKey key;
75 char result[GNUNET_CRYPTO_AES_KEY_LENGTH]; 77 char result[GNUNET_CRYPTO_AES_KEY_LENGTH];
76 char *res; 78 char *res;
77 int ret; 79 int ret;
78 80
79 unsigned char plain[] = 81 unsigned char plain[] = {
80 {
81 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249, 82 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249,
82 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164 83 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164
83 }; 84 };
84 unsigned char raw_key_aes[] = 85 unsigned char raw_key_aes[] = {
85 {
86 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, 86 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25,
87 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, 87 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184,
88 34, 191 88 34, 191
89 }; 89 };
90 unsigned char raw_key_twofish[] = 90 unsigned char raw_key_twofish[] = {
91 {
92 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, 91 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25,
93 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, 92 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184,
94 34, 191, 106, 74, 209, 88 93 34, 191, 106, 74, 209, 88
95 }; 94 };
96 unsigned char encrresult[] = 95 unsigned char encrresult[] = {
97 {
98 155, 88, 106, 174, 124, 172, 47, 149, 85, 15, 208, 176, 65, 124, 155, 96 155, 88, 106, 174, 124, 172, 47, 149, 85, 15, 208, 176, 65, 124, 155,
99 74, 215, 25, 177, 231, 162, 109, 165, 4, 133, 165, 93, 44, 213, 77, 97 74, 215, 25, 177, 231, 162, 109, 165, 4, 133, 165, 93, 44, 213, 77,
100 206, 204, 1 98 206, 204, 1
@@ -103,68 +101,73 @@ verifyCrypto()
103 res = NULL; 101 res = NULL;
104 ret = 0; 102 ret = 0;
105 103
106 GNUNET_memcpy(key.aes_key, raw_key_aes, GNUNET_CRYPTO_AES_KEY_LENGTH); 104 GNUNET_memcpy (key.aes_key, raw_key_aes, GNUNET_CRYPTO_AES_KEY_LENGTH);
107 GNUNET_memcpy(key.twofish_key, raw_key_twofish, GNUNET_CRYPTO_AES_KEY_LENGTH); 105 GNUNET_memcpy (key.twofish_key, raw_key_twofish,
106 GNUNET_CRYPTO_AES_KEY_LENGTH);
108 if (GNUNET_CRYPTO_AES_KEY_LENGTH != 107 if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
109 GNUNET_CRYPTO_symmetric_encrypt(plain, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, 108 GNUNET_CRYPTO_symmetric_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH,
110 (const struct 109 &key,
111 GNUNET_CRYPTO_SymmetricInitializationVector *) 110 (const struct
112 "testtesttesttesttesttesttesttest", 111 GNUNET_CRYPTO_SymmetricInitializationVector
113 result)) 112 *)
114 { 113 "testtesttesttesttesttesttesttest",
115 printf("Wrong return value from encrypt block.\n"); 114 result))
116 ret = 1; 115 {
117 goto error; 116 printf ("Wrong return value from encrypt block.\n");
118 } 117 ret = 1;
119 118 goto error;
120 if (0 != memcmp(encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH)) 119 }
121 { 120
122 int i; 121 if (0 != memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH))
123 printf("Encrypted result wrong.\n"); 122 {
124 for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) 123 int i;
125 printf("%u, ", (uint8_t)result[i]); 124 printf ("Encrypted result wrong.\n");
126 ret = 1; 125 for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++)
127 goto error; 126 printf ("%u, ", (uint8_t) result[i]);
128 } 127 ret = 1;
129 128 goto error;
130 res = GNUNET_malloc(GNUNET_CRYPTO_AES_KEY_LENGTH); 129 }
130
131 res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH);
131 if (GNUNET_CRYPTO_AES_KEY_LENGTH != 132 if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
132 GNUNET_CRYPTO_symmetric_decrypt(result, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, 133 GNUNET_CRYPTO_symmetric_decrypt (result, GNUNET_CRYPTO_AES_KEY_LENGTH,
133 (const struct 134 &key,
134 GNUNET_CRYPTO_SymmetricInitializationVector *) 135 (const struct
135 "testtesttesttesttesttesttesttest", res)) 136 GNUNET_CRYPTO_SymmetricInitializationVector
136 { 137 *)
137 printf("Wrong return value from decrypt block.\n"); 138 "testtesttesttesttesttesttesttest", res))
138 ret = 1; 139 {
139 goto error; 140 printf ("Wrong return value from decrypt block.\n");
140 } 141 ret = 1;
141 if (0 != memcmp(res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH)) 142 goto error;
142 { 143 }
143 printf("Decrypted result does not match input.\n"); 144 if (0 != memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH))
144 ret = 1; 145 {
145 } 146 printf ("Decrypted result does not match input.\n");
147 ret = 1;
148 }
146error: 149error:
147 GNUNET_free_non_null(res); 150 GNUNET_free_non_null (res);
148 return ret; 151 return ret;
149} 152}
150 153
151 154
152int 155int
153main(int argc, char *argv[]) 156main (int argc, char *argv[])
154{ 157{
155 int failureCount = 0; 158 int failureCount = 0;
156 159
157 GNUNET_log_setup("test-crypto-aes", "WARNING", NULL); 160 GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL);
158 GNUNET_assert(strlen(INITVALUE) > 161 GNUNET_assert (strlen (INITVALUE) >
159 sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector)); 162 sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector));
160 failureCount += testSymcipher(); 163 failureCount += testSymcipher ();
161 failureCount += verifyCrypto(); 164 failureCount += verifyCrypto ();
162 165
163 if (failureCount != 0) 166 if (failureCount != 0)
164 { 167 {
165 printf("%d TESTS FAILED!\n", failureCount); 168 printf ("%d TESTS FAILED!\n", failureCount);
166 return -1; 169 return -1;
167 } 170 }
168 return 0; 171 return 0;
169} 172}
170 173
diff --git a/src/util/test_disk.c b/src/util/test_disk.c
index cacbe8be0..7ed61c332 100644
--- a/src/util/test_disk.c
+++ b/src/util/test_disk.c
@@ -30,51 +30,51 @@
30 30
31 31
32static int 32static int
33testReadWrite() 33testReadWrite ()
34{ 34{
35 char tmp[100 + 1]; 35 char tmp[100 + 1];
36 int ret; 36 int ret;
37 37
38 if (strlen(TESTSTRING) != 38 if (strlen (TESTSTRING) !=
39 GNUNET_DISK_fn_write(".testfile", TESTSTRING, strlen(TESTSTRING), 39 GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
40 GNUNET_DISK_PERM_USER_READ | 40 GNUNET_DISK_PERM_USER_READ
41 GNUNET_DISK_PERM_USER_WRITE)) 41 | GNUNET_DISK_PERM_USER_WRITE))
42 return 1; 42 return 1;
43 if (GNUNET_OK != GNUNET_DISK_file_test(".testfile")) 43 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
44 return 1; 44 return 1;
45 ret = GNUNET_DISK_fn_read(".testfile", tmp, sizeof(tmp) - 1); 45 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof(tmp) - 1);
46 if (ret < 0) 46 if (ret < 0)
47 { 47 {
48 fprintf(stderr, "Error reading file `%s' in testReadWrite\n", ".testfile"); 48 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
49 return 1; 49 return 1;
50 } 50 }
51 tmp[ret] = '\0'; 51 tmp[ret] = '\0';
52 if (0 != memcmp(tmp, TESTSTRING, strlen(TESTSTRING) + 1)) 52 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
53 { 53 {
54 fprintf(stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, 54 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
55 TESTSTRING, ".testfile"); 55 TESTSTRING, ".testfile");
56 return 1; 56 return 1;
57 } 57 }
58 GNUNET_DISK_file_copy(".testfile", ".testfile2"); 58 GNUNET_DISK_file_copy (".testfile", ".testfile2");
59 memset(tmp, 0, sizeof(tmp)); 59 memset (tmp, 0, sizeof(tmp));
60 ret = GNUNET_DISK_fn_read(".testfile2", tmp, sizeof(tmp) - 1); 60 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof(tmp) - 1);
61 if (ret < 0) 61 if (ret < 0)
62 { 62 {
63 fprintf(stderr, "Error reading file `%s' in testReadWrite\n", 63 fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
64 ".testfile2"); 64 ".testfile2");
65 return 1; 65 return 1;
66 } 66 }
67 tmp[ret] = '\0'; 67 tmp[ret] = '\0';
68 if (0 != memcmp(tmp, TESTSTRING, strlen(TESTSTRING) + 1)) 68 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
69 { 69 {
70 fprintf(stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp, 70 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
71 TESTSTRING, ".testfile2"); 71 TESTSTRING, ".testfile2");
72 return 1; 72 return 1;
73 } 73 }
74 74
75 GNUNET_break(0 == unlink(".testfile")); 75 GNUNET_break (0 == unlink (".testfile"));
76 GNUNET_break(0 == unlink(".testfile2")); 76 GNUNET_break (0 == unlink (".testfile2"));
77 if (GNUNET_NO != GNUNET_DISK_file_test(".testfile")) 77 if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
78 return 1; 78 return 1;
79 79
80 return 0; 80 return 0;
@@ -82,24 +82,25 @@ testReadWrite()
82 82
83 83
84static int 84static int
85testOpenClose() 85testOpenClose ()
86{ 86{
87 struct GNUNET_DISK_FileHandle *fh; 87 struct GNUNET_DISK_FileHandle *fh;
88 uint64_t size; 88 uint64_t size;
89 89
90 fh = GNUNET_DISK_file_open(".testfile", 90 fh = GNUNET_DISK_file_open (".testfile",
91 GNUNET_DISK_OPEN_READWRITE | 91 GNUNET_DISK_OPEN_READWRITE
92 GNUNET_DISK_OPEN_CREATE, 92 | GNUNET_DISK_OPEN_CREATE,
93 GNUNET_DISK_PERM_USER_READ | 93 GNUNET_DISK_PERM_USER_READ
94 GNUNET_DISK_PERM_USER_WRITE); 94 | GNUNET_DISK_PERM_USER_WRITE);
95 GNUNET_assert(GNUNET_NO == GNUNET_DISK_handle_invalid(fh)); 95 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
96 GNUNET_break(5 == GNUNET_DISK_file_write(fh, "Hello", 5)); 96 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
97 GNUNET_DISK_file_close(fh); 97 GNUNET_DISK_file_close (fh);
98 GNUNET_break(GNUNET_OK == 98 GNUNET_break (GNUNET_OK ==
99 GNUNET_DISK_file_size(".testfile", &size, GNUNET_NO, GNUNET_YES)); 99 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO,
100 GNUNET_YES));
100 if (size != 5) 101 if (size != 5)
101 return 1; 102 return 1;
102 GNUNET_break(0 == unlink(".testfile")); 103 GNUNET_break (0 == unlink (".testfile"));
103 104
104 return 0; 105 return 0;
105} 106}
@@ -108,48 +109,48 @@ static int ok;
108 109
109 110
110static int 111static int
111scan_callback(void *want, const char *filename) 112scan_callback (void *want, const char *filename)
112{ 113{
113 if (NULL != strstr(filename, want)) 114 if (NULL != strstr (filename, want))
114 ok++; 115 ok++;
115 return GNUNET_OK; 116 return GNUNET_OK;
116} 117}
117 118
118 119
119static int 120static int
120testDirScan() 121testDirScan ()
121{ 122{
122 if (GNUNET_OK != 123 if (GNUNET_OK !=
123 GNUNET_DISK_directory_create("test" DIR_SEPARATOR_STR "entry")) 124 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
124 { 125 {
125 GNUNET_break(0); 126 GNUNET_break (0);
126 return 1; 127 return 1;
127 } 128 }
128 if (GNUNET_OK != 129 if (GNUNET_OK !=
129 GNUNET_DISK_directory_create("test" DIR_SEPARATOR_STR "entry_more")) 130 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
130 { 131 {
131 GNUNET_break(0); 132 GNUNET_break (0);
132 return 1; 133 return 1;
133 } 134 }
134 GNUNET_DISK_directory_scan("test", &scan_callback, 135 GNUNET_DISK_directory_scan ("test", &scan_callback,
135 "test" DIR_SEPARATOR_STR "entry"); 136 "test" DIR_SEPARATOR_STR "entry");
136 if (GNUNET_OK != GNUNET_DISK_directory_remove("test")) 137 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
137 { 138 {
138 GNUNET_break(0); 139 GNUNET_break (0);
139 return 1; 140 return 1;
140 } 141 }
141 if (ok < 2) 142 if (ok < 2)
142 { 143 {
143 GNUNET_break(0); 144 GNUNET_break (0);
144 return 1; 145 return 1;
145 } 146 }
146 return 0; 147 return 0;
147} 148}
148 149
149 150
150static int 151static int
151iter_callback(void *cls, 152iter_callback (void *cls,
152 const char *filename) 153 const char *filename)
153{ 154{
154 int *i = cls; 155 int *i = cls;
155 156
@@ -159,130 +160,130 @@ iter_callback(void *cls,
159 160
160 161
161static int 162static int
162testDirIter() 163testDirIter ()
163{ 164{
164 int i; 165 int i;
165 166
166 i = 0; 167 i = 0;
167 if (GNUNET_OK != GNUNET_DISK_directory_create("test/entry")) 168 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
168 { 169 {
169 GNUNET_break(0); 170 GNUNET_break (0);
170 return 1; 171 return 1;
171 } 172 }
172 if (GNUNET_OK != GNUNET_DISK_directory_create("test/entry_many")) 173 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
173 { 174 {
174 GNUNET_break(0); 175 GNUNET_break (0);
175 return 1; 176 return 1;
176 } 177 }
177 if (GNUNET_OK != GNUNET_DISK_directory_create("test/entry_more")) 178 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
178 { 179 {
179 GNUNET_break(0); 180 GNUNET_break (0);
180 return 1; 181 return 1;
181 } 182 }
182 GNUNET_DISK_directory_scan("test", 183 GNUNET_DISK_directory_scan ("test",
183 &iter_callback, 184 &iter_callback,
184 &i); 185 &i);
185 if (GNUNET_OK != GNUNET_DISK_directory_remove("test")) 186 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
186 { 187 {
187 GNUNET_break(0); 188 GNUNET_break (0);
188 return 1; 189 return 1;
189 } 190 }
190 if (i < 3) 191 if (i < 3)
191 { 192 {
192 GNUNET_break(0); 193 GNUNET_break (0);
193 return 1; 194 return 1;
194 } 195 }
195 return 0; 196 return 0;
196} 197}
197 198
198 199
199static int 200static int
200testCanonicalize() 201testCanonicalize ()
201{ 202{
202 char *fn = GNUNET_strdup("ab?><|cd*ef:/g\""); 203 char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
203 204
204 GNUNET_DISK_filename_canonicalize(fn); 205 GNUNET_DISK_filename_canonicalize (fn);
205 if (0 != strcmp(fn, "ab____cd_ef__g_")) 206 if (0 != strcmp (fn, "ab____cd_ef__g_"))
206 { 207 {
207 GNUNET_free(fn); 208 GNUNET_free (fn);
208 return 1; 209 return 1;
209 } 210 }
210 GNUNET_free(fn); 211 GNUNET_free (fn);
211 return 0; 212 return 0;
212} 213}
213 214
214 215
215static int 216static int
216testChangeOwner() 217testChangeOwner ()
217{ 218{
218 GNUNET_log_skip(1, GNUNET_NO); 219 GNUNET_log_skip (1, GNUNET_NO);
219 if (GNUNET_OK == GNUNET_DISK_file_change_owner("/dev/null", "unknownuser")) 220 if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
220 return 1; 221 return 1;
221 return 0; 222 return 0;
222} 223}
223 224
224 225
225static int 226static int
226testDirMani() 227testDirMani ()
227{ 228{
228 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file("test/ing")) 229 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
229 { 230 {
230 GNUNET_break(0); 231 GNUNET_break (0);
231 return 1; 232 return 1;
232 } 233 }
233 if (GNUNET_NO != GNUNET_DISK_file_test("test")) 234 if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
234 { 235 {
235 GNUNET_break(0); 236 GNUNET_break (0);
236 return 1; 237 return 1;
237 } 238 }
238 if (GNUNET_NO != GNUNET_DISK_file_test("test/ing")) 239 if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
239 { 240 {
240 GNUNET_break(0); 241 GNUNET_break (0);
241 return 1; 242 return 1;
242 } 243 }
243 if (GNUNET_OK != GNUNET_DISK_directory_remove("test")) 244 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
244 { 245 {
245 GNUNET_break(0); 246 GNUNET_break (0);
246 return 1; 247 return 1;
247 } 248 }
248 if (GNUNET_OK != GNUNET_DISK_directory_create("test")) 249 if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
249 { 250 {
250 GNUNET_break(0); 251 GNUNET_break (0);
251 return 1; 252 return 1;
252 } 253 }
253 if (GNUNET_YES != GNUNET_DISK_directory_test("test", GNUNET_YES)) 254 if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
254 { 255 {
255 GNUNET_break(0); 256 GNUNET_break (0);
256 return 1; 257 return 1;
257 } 258 }
258 if (GNUNET_OK != GNUNET_DISK_directory_remove("test")) 259 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
259 { 260 {
260 GNUNET_break(0); 261 GNUNET_break (0);
261 return 1; 262 return 1;
262 } 263 }
263 return 0; 264 return 0;
264} 265}
265 266
266 267
267int 268int
268main(int argc, char *argv[]) 269main (int argc, char *argv[])
269{ 270{
270 unsigned int failureCount = 0; 271 unsigned int failureCount = 0;
271 272
272 GNUNET_log_setup("test-disk", "WARNING", NULL); 273 GNUNET_log_setup ("test-disk", "WARNING", NULL);
273 failureCount += testReadWrite(); 274 failureCount += testReadWrite ();
274 failureCount += testOpenClose(); 275 failureCount += testOpenClose ();
275 failureCount += testDirScan(); 276 failureCount += testDirScan ();
276 failureCount += testDirIter(); 277 failureCount += testDirIter ();
277 failureCount += testCanonicalize(); 278 failureCount += testCanonicalize ();
278 failureCount += testChangeOwner(); 279 failureCount += testChangeOwner ();
279 failureCount += testDirMani(); 280 failureCount += testDirMani ();
280 if (0 != failureCount) 281 if (0 != failureCount)
281 { 282 {
282 fprintf(stderr, 283 fprintf (stderr,
283 "\n%u TESTS FAILED!\n", 284 "\n%u TESTS FAILED!\n",
284 failureCount); 285 failureCount);
285 return -1; 286 return -1;
286 } 287 }
287 return 0; 288 return 0;
288} /* end of main */ 289} /* end of main */
diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c
index 77b8c8b2f..7252530e2 100644
--- a/src/util/test_getopt.c
+++ b/src/util/test_getopt.c
@@ -26,14 +26,14 @@
26 26
27 27
28static int 28static int
29testMinimal() 29testMinimal ()
30{ 30{
31 char *const emptyargv[] = { "test", NULL }; 31 char *const emptyargv[] = { "test", NULL };
32 const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = { 32 const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
33 GNUNET_GETOPT_OPTION_END 33 GNUNET_GETOPT_OPTION_END
34 }; 34 };
35 35
36 if (1 != GNUNET_GETOPT_run("test", emptyoptionlist, 1, emptyargv)) 36 if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
37 return 1; 37 return 1;
38 38
39 return 0; 39 return 0;
@@ -41,95 +41,95 @@ testMinimal()
41 41
42 42
43static int 43static int
44testVerbose() 44testVerbose ()
45{ 45{
46 char *const myargv[] = { "test", "-V", "-V", "more", NULL }; 46 char *const myargv[] = { "test", "-V", "-V", "more", NULL };
47 unsigned int vflags = 0; 47 unsigned int vflags = 0;
48 48
49 const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = 49 const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] =
50 { GNUNET_GETOPT_option_verbose(&vflags), GNUNET_GETOPT_OPTION_END }; 50 { GNUNET_GETOPT_option_verbose (&vflags), GNUNET_GETOPT_OPTION_END };
51 51
52 if (3 != GNUNET_GETOPT_run("test", verboseoptionlist, 4, myargv)) 52 if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
53 { 53 {
54 GNUNET_break(0); 54 GNUNET_break (0);
55 return 1; 55 return 1;
56 } 56 }
57 if (vflags != 2) 57 if (vflags != 2)
58 { 58 {
59 GNUNET_break(0); 59 GNUNET_break (0);
60 return 1; 60 return 1;
61 } 61 }
62 return 0; 62 return 0;
63} 63}
64 64
65 65
66static int 66static int
67testVersion() 67testVersion ()
68{ 68{
69 char *const myargv[] = { "test_getopt", "-v", NULL }; 69 char *const myargv[] = { "test_getopt", "-v", NULL };
70 const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = 70 const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] =
71 { GNUNET_GETOPT_option_version(PACKAGE_VERSION " " VCS_VERSION), 71 { GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
72 GNUNET_GETOPT_OPTION_END }; 72 GNUNET_GETOPT_OPTION_END };
73 73
74 if (0 != GNUNET_GETOPT_run("test_getopt", versionoptionlist, 2, myargv)) 74 if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
75 { 75 {
76 GNUNET_break(0); 76 GNUNET_break (0);
77 return 1; 77 return 1;
78 } 78 }
79 return 0; 79 return 0;
80} 80}
81 81
82 82
83static int 83static int
84testAbout() 84testAbout ()
85{ 85{
86 char *const myargv[] = { "test_getopt", "-h", NULL }; 86 char *const myargv[] = { "test_getopt", "-h", NULL };
87 const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = 87 const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] =
88 { GNUNET_GETOPT_option_help("Testing"), GNUNET_GETOPT_OPTION_END }; 88 { GNUNET_GETOPT_option_help ("Testing"), GNUNET_GETOPT_OPTION_END };
89 89
90 if (0 != GNUNET_GETOPT_run("test_getopt", aboutoptionlist, 2, myargv)) 90 if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
91 { 91 {
92 GNUNET_break(0); 92 GNUNET_break (0);
93 return 1; 93 return 1;
94 } 94 }
95 return 0; 95 return 0;
96} 96}
97 97
98 98
99static int 99static int
100testLogOpts() 100testLogOpts ()
101{ 101{
102 char *const myargv[] = 102 char *const myargv[] =
103 { "test_getopt", "-l", "filename", "-L", "WARNING", NULL }; 103 { "test_getopt", "-l", "filename", "-L", "WARNING", NULL };
104 char *level = GNUNET_strdup("stuff"); 104 char *level = GNUNET_strdup ("stuff");
105 char *fn = NULL; 105 char *fn = NULL;
106 106
107 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = 107 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
108 { GNUNET_GETOPT_option_logfile(&fn), 108 { GNUNET_GETOPT_option_logfile (&fn),
109 GNUNET_GETOPT_option_loglevel(&level), 109 GNUNET_GETOPT_option_loglevel (&level),
110 GNUNET_GETOPT_OPTION_END }; 110 GNUNET_GETOPT_OPTION_END };
111 111
112 if (5 != GNUNET_GETOPT_run("test_getopt", logoptionlist, 5, myargv)) 112 if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
113 { 113 {
114 GNUNET_break(0); 114 GNUNET_break (0);
115 return 1; 115 return 1;
116 } 116 }
117 GNUNET_assert(NULL != fn); 117 GNUNET_assert (NULL != fn);
118 if ((0 != strcmp(level, "WARNING")) || (NULL == strstr(fn, "/filename"))) 118 if ((0 != strcmp (level, "WARNING")) || (NULL == strstr (fn, "/filename")))
119 { 119 {
120 GNUNET_break(0); 120 GNUNET_break (0);
121 GNUNET_free(level); 121 GNUNET_free (level);
122 GNUNET_free(fn); 122 GNUNET_free (fn);
123 return 1; 123 return 1;
124 } 124 }
125 GNUNET_free(level); 125 GNUNET_free (level);
126 GNUNET_free(fn); 126 GNUNET_free (fn);
127 return 0; 127 return 0;
128} 128}
129 129
130 130
131static int 131static int
132testFlagNum() 132testFlagNum ()
133{ 133{
134 char *const myargv[] = { "test_getopt", "-f", "-n", "42", "-N", "42", NULL }; 134 char *const myargv[] = { "test_getopt", "-f", "-n", "42", "-N", "42", NULL };
135 int flag = 0; 135 int flag = 0;
@@ -137,46 +137,46 @@ testFlagNum()
137 unsigned long long lnum = 0; 137 unsigned long long lnum = 0;
138 138
139 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = 139 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
140 { GNUNET_GETOPT_option_flag('f', "--flag", "helptext", &flag), 140 { GNUNET_GETOPT_option_flag ('f', "--flag", "helptext", &flag),
141 GNUNET_GETOPT_option_uint('n', "--num", "ARG", "helptext", &num), 141 GNUNET_GETOPT_option_uint ('n', "--num", "ARG", "helptext", &num),
142 GNUNET_GETOPT_option_ulong('N', "--lnum", "ARG", "helptext", &lnum), 142 GNUNET_GETOPT_option_ulong ('N', "--lnum", "ARG", "helptext", &lnum),
143 GNUNET_GETOPT_OPTION_END }; 143 GNUNET_GETOPT_OPTION_END };
144 144
145 if (6 != GNUNET_GETOPT_run("test_getopt", logoptionlist, 6, myargv)) 145 if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
146 { 146 {
147 GNUNET_break(0); 147 GNUNET_break (0);
148 return 1; 148 return 1;
149 } 149 }
150 if ((1 != flag) || (42 != num) || (42 != lnum)) 150 if ((1 != flag) || (42 != num) || (42 != lnum))
151 { 151 {
152 GNUNET_break(0); 152 GNUNET_break (0);
153 return 1; 153 return 1;
154 } 154 }
155 return 0; 155 return 0;
156} 156}
157 157
158 158
159int 159int
160main(int argc, char *argv[]) 160main (int argc, char *argv[])
161{ 161{
162 int errCnt = 0; 162 int errCnt = 0;
163 163
164 GNUNET_log_setup("test_getopt", "WARNING", NULL); 164 GNUNET_log_setup ("test_getopt", "WARNING", NULL);
165 /* suppress output from -h, -v options */ 165 /* suppress output from -h, -v options */
166 166
167 GNUNET_break(0 == close(1)); 167 GNUNET_break (0 == close (1));
168 168
169 if (0 != testMinimal()) 169 if (0 != testMinimal ())
170 errCnt++; 170 errCnt++;
171 if (0 != testVerbose()) 171 if (0 != testVerbose ())
172 errCnt++; 172 errCnt++;
173 if (0 != testVersion()) 173 if (0 != testVersion ())
174 errCnt++; 174 errCnt++;
175 if (0 != testAbout()) 175 if (0 != testAbout ())
176 errCnt++; 176 errCnt++;
177 if (0 != testLogOpts()) 177 if (0 != testLogOpts ())
178 errCnt++; 178 errCnt++;
179 if (0 != testFlagNum()) 179 if (0 != testFlagNum ())
180 errCnt++; 180 errCnt++;
181 return errCnt; 181 return errCnt;
182} 182}
diff --git a/src/util/test_hexcoder.c b/src/util/test_hexcoder.c
index 69e2061cb..923652a00 100644
--- a/src/util/test_hexcoder.c
+++ b/src/util/test_hexcoder.c
@@ -32,23 +32,23 @@
32 32
33 33
34int 34int
35main(int argc, 35main (int argc,
36 char *argv[]) 36 char *argv[])
37{ 37{
38 char buf[strlen(TESTSTRING) + 1]; 38 char buf[strlen (TESTSTRING) + 1];
39 char *ret; 39 char *ret;
40 40
41 GNUNET_log_setup("test-hexcoder", "WARNING", NULL); 41 GNUNET_log_setup ("test-hexcoder", "WARNING", NULL);
42 ret = GNUNET_DNSPARSER_bin_to_hex(TESTSTRING, 42 ret = GNUNET_DNSPARSER_bin_to_hex (TESTSTRING,
43 strlen(TESTSTRING) + 1); 43 strlen (TESTSTRING) + 1);
44 GNUNET_assert(NULL != ret); 44 GNUNET_assert (NULL != ret);
45 GNUNET_assert(sizeof(buf) == 45 GNUNET_assert (sizeof(buf) ==
46 GNUNET_DNSPARSER_hex_to_bin(ret, 46 GNUNET_DNSPARSER_hex_to_bin (ret,
47 buf)); 47 buf));
48 GNUNET_assert(0 == memcmp(TESTSTRING, 48 GNUNET_assert (0 == memcmp (TESTSTRING,
49 buf, 49 buf,
50 sizeof(buf))); 50 sizeof(buf)));
51 GNUNET_free(ret); 51 GNUNET_free (ret);
52 return 0; 52 return 0;
53} 53}
54 54
diff --git a/src/util/test_mq.c b/src/util/test_mq.c
index 4282f06b7..dc34f2da4 100644
--- a/src/util/test_mq.c
+++ b/src/util/test_mq.c
@@ -32,14 +32,16 @@
32/** 32/**
33 * How long does the receiver take per message? 33 * How long does the receiver take per message?
34 */ 34 */
35#define RECEIVER_THROTTLE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1) 35#define RECEIVER_THROTTLE GNUNET_TIME_relative_multiply ( \
36 GNUNET_TIME_UNIT_MILLISECONDS, 1)
36 37
37static unsigned int received_cnt; 38static unsigned int received_cnt;
38 39
39 40
40GNUNET_NETWORK_STRUCT_BEGIN 41GNUNET_NETWORK_STRUCT_BEGIN
41 42
42struct MyMessage { 43struct MyMessage
44{
43 struct GNUNET_MessageHeader header; 45 struct GNUNET_MessageHeader header;
44 uint32_t x GNUNET_PACKED; 46 uint32_t x GNUNET_PACKED;
45}; 47};
@@ -56,28 +58,28 @@ static struct GNUNET_MQ_Handle *cmq;
56 58
57 59
58static void 60static void
59do_shutdown(void *cls) 61do_shutdown (void *cls)
60{ 62{
61 (void)cls; 63 (void) cls;
62 if (NULL != tt) 64 if (NULL != tt)
63 { 65 {
64 GNUNET_SCHEDULER_cancel(tt); 66 GNUNET_SCHEDULER_cancel (tt);
65 tt = NULL; 67 tt = NULL;
66 } 68 }
67 if (NULL != cmq) 69 if (NULL != cmq)
68 { 70 {
69 GNUNET_MQ_destroy(cmq); 71 GNUNET_MQ_destroy (cmq);
70 cmq = NULL; 72 cmq = NULL;
71 } 73 }
72} 74}
73 75
74 76
75static void 77static void
76do_timeout(void *cls) 78do_timeout (void *cls)
77{ 79{
78 (void)cls; 80 (void) cls;
79 tt = NULL; 81 tt = NULL;
80 GNUNET_SCHEDULER_shutdown(); 82 GNUNET_SCHEDULER_shutdown ();
81 global_ret = 1; 83 global_ret = 1;
82} 84}
83 85
@@ -92,59 +94,59 @@ do_timeout(void *cls)
92 * @param error error code 94 * @param error error code
93 */ 95 */
94static void 96static void
95error_cb(void *cls, 97error_cb (void *cls,
96 enum GNUNET_MQ_Error error) 98 enum GNUNET_MQ_Error error)
97{ 99{
98 GNUNET_break(0); 100 GNUNET_break (0);
99 global_ret = 3; 101 global_ret = 3;
100 GNUNET_SCHEDULER_shutdown(); 102 GNUNET_SCHEDULER_shutdown ();
101} 103}
102 104
103 105
104static void 106static void
105client_continue(void *cls) 107client_continue (void *cls)
106{ 108{
107 struct GNUNET_SERVICE_Client *c = cls; 109 struct GNUNET_SERVICE_Client *c = cls;
108 110
109 dt = NULL; 111 dt = NULL;
110 GNUNET_SERVICE_client_continue(c); 112 GNUNET_SERVICE_client_continue (c);
111} 113}
112 114
113 115
114static void 116static void
115handle_dummy(void *cls, 117handle_dummy (void *cls,
116 const struct MyMessage *msg) 118 const struct MyMessage *msg)
117{ 119{
118 struct GNUNET_SERVICE_Client *c = cls; 120 struct GNUNET_SERVICE_Client *c = cls;
119 121
120 GNUNET_assert(NULL == dt); 122 GNUNET_assert (NULL == dt);
121 /* artificially make receiver slower than sender */ 123 /* artificially make receiver slower than sender */
122 dt = GNUNET_SCHEDULER_add_delayed(RECEIVER_THROTTLE, 124 dt = GNUNET_SCHEDULER_add_delayed (RECEIVER_THROTTLE,
123 &client_continue, 125 &client_continue,
124 c); 126 c);
125 if (received_cnt != ntohl(msg->x)) 127 if (received_cnt != ntohl (msg->x))
126 { 128 {
127 GNUNET_break(0); 129 GNUNET_break (0);
128 global_ret = 4; 130 global_ret = 4;
129 GNUNET_SCHEDULER_shutdown(); 131 GNUNET_SCHEDULER_shutdown ();
130 } 132 }
131 received_cnt++; 133 received_cnt++;
132} 134}
133 135
134 136
135static void 137static void
136handle_dummy2(void *cls, 138handle_dummy2 (void *cls,
137 const struct MyMessage *msg) 139 const struct MyMessage *msg)
138{ 140{
139 struct GNUNET_SERVICE_Client *c = cls; 141 struct GNUNET_SERVICE_Client *c = cls;
140 142
141 GNUNET_SERVICE_client_continue(c); 143 GNUNET_SERVICE_client_continue (c);
142 if (NUM_TRANSMISSIONS != received_cnt) 144 if (NUM_TRANSMISSIONS != received_cnt)
143 { 145 {
144 GNUNET_break(0); 146 GNUNET_break (0);
145 global_ret = 5; 147 global_ret = 5;
146 } 148 }
147 GNUNET_SCHEDULER_shutdown(); 149 GNUNET_SCHEDULER_shutdown ();
148} 150}
149 151
150 152
@@ -152,19 +154,19 @@ handle_dummy2(void *cls,
152 * Function called whenever MQ has sent a message. 154 * Function called whenever MQ has sent a message.
153 */ 155 */
154static void 156static void
155notify_sent_cb(void *cls) 157notify_sent_cb (void *cls)
156{ 158{
157 static unsigned int seen; 159 static unsigned int seen;
158 unsigned int *cnt = cls; 160 unsigned int *cnt = cls;
159 161
160 if (seen != *cnt) 162 if (seen != *cnt)
161 { 163 {
162 GNUNET_break(0); 164 GNUNET_break (0);
163 global_ret = 6; 165 global_ret = 6;
164 GNUNET_SCHEDULER_shutdown(); 166 GNUNET_SCHEDULER_shutdown ();
165 } 167 }
166 seen++; 168 seen++;
167 GNUNET_free(cnt); 169 GNUNET_free (cnt);
168} 170}
169 171
170 172
@@ -176,47 +178,47 @@ notify_sent_cb(void *cls)
176 * @param sh handle to the newly create service 178 * @param sh handle to the newly create service
177 */ 179 */
178static void 180static void
179run(void *cls, 181run (void *cls,
180 const struct GNUNET_CONFIGURATION_Handle *cfg, 182 const struct GNUNET_CONFIGURATION_Handle *cfg,
181 struct GNUNET_SERVICE_Handle *sh) 183 struct GNUNET_SERVICE_Handle *sh)
182{ 184{
183 struct GNUNET_MQ_MessageHandler ch[] = { 185 struct GNUNET_MQ_MessageHandler ch[] = {
184 GNUNET_MQ_handler_end() 186 GNUNET_MQ_handler_end ()
185 }; 187 };
186 struct GNUNET_MQ_Envelope *env; 188 struct GNUNET_MQ_Envelope *env;
187 struct MyMessage *m; 189 struct MyMessage *m;
188 190
189 (void)cls; 191 (void) cls;
190 (void)sh; 192 (void) sh;
191 cmq = GNUNET_CLIENT_connect(cfg, 193 cmq = GNUNET_CLIENT_connect (cfg,
192 "test_client", 194 "test_client",
193 ch, 195 ch,
194 &error_cb, 196 &error_cb,
195 NULL); 197 NULL);
196 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, 198 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
197 NULL); 199 NULL);
198 tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES, 200 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
199 &do_timeout, 201 &do_timeout,
200 NULL); 202 NULL);
201 for (unsigned int i = 0; i < NUM_TRANSMISSIONS; i++) 203 for (unsigned int i = 0; i < NUM_TRANSMISSIONS; i++)
202 { 204 {
203 unsigned int *cnt; 205 unsigned int *cnt;
204 206
205 cnt = GNUNET_new(unsigned int); 207 cnt = GNUNET_new (unsigned int);
206 *cnt = i; 208 *cnt = i;
207 env = GNUNET_MQ_msg(m, 209 env = GNUNET_MQ_msg (m,
208 GNUNET_MESSAGE_TYPE_DUMMY); 210 GNUNET_MESSAGE_TYPE_DUMMY);
209 GNUNET_MQ_notify_sent(env, 211 GNUNET_MQ_notify_sent (env,
210 &notify_sent_cb, 212 &notify_sent_cb,
211 cnt); 213 cnt);
212 m->x = htonl(i); 214 m->x = htonl (i);
213 GNUNET_MQ_send(cmq, 215 GNUNET_MQ_send (cmq,
214 env); 216 env);
215 } 217 }
216 env = GNUNET_MQ_msg(m, 218 env = GNUNET_MQ_msg (m,
217 GNUNET_MESSAGE_TYPE_DUMMY2); 219 GNUNET_MESSAGE_TYPE_DUMMY2);
218 GNUNET_MQ_send(cmq, 220 GNUNET_MQ_send (cmq,
219 env); 221 env);
220} 222}
221 223
222 224
@@ -229,12 +231,12 @@ run(void *cls,
229 * @return the client-specific (`internal') closure 231 * @return the client-specific (`internal') closure
230 */ 232 */
231static void * 233static void *
232connect_cb(void *cls, 234connect_cb (void *cls,
233 struct GNUNET_SERVICE_Client *c, 235 struct GNUNET_SERVICE_Client *c,
234 struct GNUNET_MQ_Handle *mq) 236 struct GNUNET_MQ_Handle *mq)
235{ 237{
236 (void)cls; 238 (void) cls;
237 (void)mq; 239 (void) mq;
238 return c; 240 return c;
239} 241}
240 242
@@ -247,18 +249,18 @@ connect_cb(void *cls,
247 * @param internal_cls the client-specific (`internal') closure 249 * @param internal_cls the client-specific (`internal') closure
248 */ 250 */
249static void 251static void
250disconnect_cb(void *cls, 252disconnect_cb (void *cls,
251 struct GNUNET_SERVICE_Client *c, 253 struct GNUNET_SERVICE_Client *c,
252 void *internal_cls) 254 void *internal_cls)
253{ 255{
254 (void)cls; 256 (void) cls;
255 (void)c; 257 (void) c;
256 (void)internal_cls; 258 (void) internal_cls;
257} 259}
258 260
259 261
260static void 262static void
261test1() 263test1 ()
262{ 264{
263 struct GNUNET_MQ_Envelope *mqm; 265 struct GNUNET_MQ_Envelope *mqm;
264 struct MyMessage *mm; 266 struct MyMessage *mm;
@@ -266,74 +268,74 @@ test1()
266 mm = NULL; 268 mm = NULL;
267 mqm = NULL; 269 mqm = NULL;
268 270
269 mqm = GNUNET_MQ_msg(mm, 271 mqm = GNUNET_MQ_msg (mm,
270 GNUNET_MESSAGE_TYPE_DUMMY); 272 GNUNET_MESSAGE_TYPE_DUMMY);
271 GNUNET_assert(NULL != mqm); 273 GNUNET_assert (NULL != mqm);
272 GNUNET_assert(NULL != mm); 274 GNUNET_assert (NULL != mm);
273 GNUNET_assert(GNUNET_MESSAGE_TYPE_DUMMY == ntohs(mm->header.type)); 275 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mm->header.type));
274 GNUNET_assert(sizeof(struct MyMessage) == ntohs(mm->header.size)); 276 GNUNET_assert (sizeof(struct MyMessage) == ntohs (mm->header.size));
275 GNUNET_MQ_discard(mqm); 277 GNUNET_MQ_discard (mqm);
276} 278}
277 279
278 280
279static void 281static void
280test2() 282test2 ()
281{ 283{
282 struct GNUNET_MQ_Envelope *mqm; 284 struct GNUNET_MQ_Envelope *mqm;
283 struct GNUNET_MessageHeader *mh; 285 struct GNUNET_MessageHeader *mh;
284 286
285 mqm = GNUNET_MQ_msg_header(GNUNET_MESSAGE_TYPE_DUMMY); 287 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_DUMMY);
286 /* how could the above be checked? */ 288 /* how could the above be checked? */
287 289
288 GNUNET_MQ_discard(mqm); 290 GNUNET_MQ_discard (mqm);
289 291
290 mqm = GNUNET_MQ_msg_header_extra(mh, 292 mqm = GNUNET_MQ_msg_header_extra (mh,
291 20, 293 20,
292 GNUNET_MESSAGE_TYPE_DUMMY); 294 GNUNET_MESSAGE_TYPE_DUMMY);
293 GNUNET_assert(GNUNET_MESSAGE_TYPE_DUMMY == ntohs(mh->type)); 295 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mh->type));
294 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) + 20 == ntohs(mh->size)); 296 GNUNET_assert (sizeof(struct GNUNET_MessageHeader) + 20 == ntohs (mh->size));
295 GNUNET_MQ_discard(mqm); 297 GNUNET_MQ_discard (mqm);
296} 298}
297 299
298 300
299int 301int
300main(int argc, char **argv) 302main (int argc, char **argv)
301{ 303{
302 char * test_argv[] = { 304 char *test_argv[] = {
303 (char *)"test_client", 305 (char *) "test_client",
304 "-c", 306 "-c",
305 "test_client_data.conf", 307 "test_client_data.conf",
306 NULL 308 NULL
307 }; 309 };
308 struct GNUNET_MQ_MessageHandler mh[] = { 310 struct GNUNET_MQ_MessageHandler mh[] = {
309 GNUNET_MQ_hd_fixed_size(dummy, 311 GNUNET_MQ_hd_fixed_size (dummy,
310 GNUNET_MESSAGE_TYPE_DUMMY, 312 GNUNET_MESSAGE_TYPE_DUMMY,
311 struct MyMessage, 313 struct MyMessage,
312 NULL), 314 NULL),
313 GNUNET_MQ_hd_fixed_size(dummy2, 315 GNUNET_MQ_hd_fixed_size (dummy2,
314 GNUNET_MESSAGE_TYPE_DUMMY2, 316 GNUNET_MESSAGE_TYPE_DUMMY2,
315 struct MyMessage, 317 struct MyMessage,
316 NULL), 318 NULL),
317 GNUNET_MQ_handler_end() 319 GNUNET_MQ_handler_end ()
318 }; 320 };
319 321
320 (void)argc; 322 (void) argc;
321 (void)argv; 323 (void) argv;
322 GNUNET_log_setup("test-mq", 324 GNUNET_log_setup ("test-mq",
323 "INFO", 325 "INFO",
324 NULL); 326 NULL);
325 test1(); 327 test1 ();
326 test2(); 328 test2 ();
327 if (0 != 329 if (0 !=
328 GNUNET_SERVICE_run_(3, 330 GNUNET_SERVICE_run_ (3,
329 test_argv, 331 test_argv,
330 "test_client", 332 "test_client",
331 GNUNET_SERVICE_OPTION_NONE, 333 GNUNET_SERVICE_OPTION_NONE,
332 &run, 334 &run,
333 &connect_cb, 335 &connect_cb,
334 &disconnect_cb, 336 &disconnect_cb,
335 NULL, 337 NULL,
336 mh)) 338 mh))
337 return 1; 339 return 1;
338 return global_ret; 340 return global_ret;
339} 341}
diff --git a/src/util/test_os_network.c b/src/util/test_os_network.c
index f6b0e3482..8e4e31f2e 100644
--- a/src/util/test_os_network.c
+++ b/src/util/test_os_network.c
@@ -31,17 +31,17 @@
31 * (success). 31 * (success).
32 */ 32 */
33static int 33static int
34proc(void *cls, 34proc (void *cls,
35 const char *name, 35 const char *name,
36 int isDefault, 36 int isDefault,
37 const struct sockaddr *addr, 37 const struct sockaddr *addr,
38 const struct sockaddr *broadcast_addr, 38 const struct sockaddr *broadcast_addr,
39 const struct sockaddr *netmask, 39 const struct sockaddr *netmask,
40 socklen_t addrlen) 40 socklen_t addrlen)
41{ 41{
42 int *ok = cls; 42 int *ok = cls;
43 char buf[INET6_ADDRSTRLEN]; 43 char buf[INET6_ADDRSTRLEN];
44 const char * protocol; 44 const char *protocol;
45 45
46 if (NULL == addr) 46 if (NULL == addr)
47 return GNUNET_OK; 47 return GNUNET_OK;
@@ -49,41 +49,41 @@ proc(void *cls,
49 protocol = "IPv4"; 49 protocol = "IPv4";
50 else 50 else
51 protocol = "IPv6"; 51 protocol = "IPv6";
52 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 52 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
53 "%s Address `%s'\n", 53 "%s Address `%s'\n",
54 protocol, 54 protocol,
55 GNUNET_a2s((const struct sockaddr *)addr, 55 GNUNET_a2s ((const struct sockaddr *) addr,
56 addrlen)); 56 addrlen));
57 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 57 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
58 "Netmask `%s'\n", 58 "Netmask `%s'\n",
59 GNUNET_a2s((const struct sockaddr *)netmask, 59 GNUNET_a2s ((const struct sockaddr *) netmask,
60 addrlen)); 60 addrlen));
61 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "`%s'\n", 62 "`%s'\n",
63 GNUNET_a2s((const struct sockaddr *)broadcast_addr, 63 GNUNET_a2s ((const struct sockaddr *) broadcast_addr,
64 addrlen)); 64 addrlen));
65 inet_ntop(addr->sa_family, 65 inet_ntop (addr->sa_family,
66 (addr->sa_family == 66 (addr->sa_family ==
67 AF_INET) ? (void *)&((struct sockaddr_in *)addr)->sin_addr 67 AF_INET) ? (void *) &((struct sockaddr_in *) addr)->sin_addr
68 : (void *)&((struct sockaddr_in6 *)addr)->sin6_addr, buf, 68 : (void *) &((struct sockaddr_in6 *) addr)->sin6_addr, buf,
69 sizeof(buf)); 69 sizeof(buf));
70 if ((0 == strcmp("::1", buf)) || (0 == strcmp("127.0.0.1", buf))) 70 if ((0 == strcmp ("::1", buf)) || (0 == strcmp ("127.0.0.1", buf)))
71 *ok = 0; 71 *ok = 0;
72 return GNUNET_OK; 72 return GNUNET_OK;
73} 73}
74 74
75 75
76int 76int
77main(int argc, char *argv[]) 77main (int argc, char *argv[])
78{ 78{
79 int ret; 79 int ret;
80 80
81 GNUNET_log_setup("test-os-network", 81 GNUNET_log_setup ("test-os-network",
82 "WARNING", 82 "WARNING",
83 NULL); 83 NULL);
84 ret = 1; 84 ret = 1;
85 GNUNET_OS_network_interfaces_list(&proc, 85 GNUNET_OS_network_interfaces_list (&proc,
86 &ret); 86 &ret);
87 return ret; 87 return ret;
88} 88}
89 89
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c
index abf00a09e..fbcfa6729 100644
--- a/src/util/test_os_start_process.c
+++ b/src/util/test_os_start_process.c
@@ -46,9 +46,10 @@ static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
46 */ 46 */
47static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout; 47static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
48 48
49static struct GNUNET_SCHEDULER_Task * die_task; 49static struct GNUNET_SCHEDULER_Task *die_task;
50 50
51struct read_context { 51struct read_context
52{
52 char buf[16]; 53 char buf[16];
53 int buf_offset; 54 int buf_offset;
54 const struct GNUNET_DISK_FileHandle *stdout_read_handle; 55 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
@@ -59,122 +60,124 @@ static struct read_context rc;
59 60
60 61
61static void 62static void
62end_task(void *cls) 63end_task (void *cls)
63{ 64{
64 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) 65 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
65 { 66 {
66 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 67 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
67 } 68 }
68 GNUNET_assert(GNUNET_OK == GNUNET_OS_process_wait(proc)); 69 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
69 GNUNET_OS_process_destroy(proc); 70 GNUNET_OS_process_destroy (proc);
70 proc = NULL; 71 proc = NULL;
71 GNUNET_DISK_pipe_close(hello_pipe_stdout); 72 GNUNET_DISK_pipe_close (hello_pipe_stdout);
72 GNUNET_DISK_pipe_close(hello_pipe_stdin); 73 GNUNET_DISK_pipe_close (hello_pipe_stdin);
73} 74}
74 75
75 76
76static void 77static void
77read_call(void *cls) 78read_call (void *cls)
78{ 79{
79 int bytes; 80 int bytes;
80 81
81 bytes = GNUNET_DISK_file_read(rc.stdout_read_handle, 82 bytes = GNUNET_DISK_file_read (rc.stdout_read_handle,
82 &rc.buf[rc.buf_offset], 83 &rc.buf[rc.buf_offset],
83 sizeof(rc.buf) - rc.buf_offset); 84 sizeof(rc.buf) - rc.buf_offset);
84 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "bytes is %d\n", 86 "bytes is %d\n",
86 bytes); 87 bytes);
87 88
88 if (bytes < 1) 89 if (bytes < 1)
89 { 90 {
90 GNUNET_break(0); 91 GNUNET_break (0);
91 ok = 1; 92 ok = 1;
92 GNUNET_SCHEDULER_cancel(die_task); 93 GNUNET_SCHEDULER_cancel (die_task);
93 (void)GNUNET_SCHEDULER_add_now(&end_task, NULL); 94 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
94 return; 95 return;
95 } 96 }
96 97
97 ok = strncmp(rc.buf, test_phrase, strlen(test_phrase)); 98 ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
98 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "read %s\n", 100 "read %s\n",
100 &rc.buf[rc.buf_offset]); 101 &rc.buf[rc.buf_offset]);
101 rc.buf_offset += bytes; 102 rc.buf_offset += bytes;
102 103
103 if (0 == ok) 104 if (0 == ok)
104 { 105 {
105 GNUNET_SCHEDULER_cancel(die_task); 106 GNUNET_SCHEDULER_cancel (die_task);
106 (void)GNUNET_SCHEDULER_add_now(&end_task, NULL); 107 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
107 return; 108 return;
108 } 109 }
109 110
110 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 111 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
111 rc.stdout_read_handle, 112 rc.stdout_read_handle,
112 &read_call, 113 &read_call,
113 NULL); 114 NULL);
114} 115}
115 116
116 117
117static void 118static void
118run_task(void *cls) 119run_task (void *cls)
119{ 120{
120 char *fn; 121 char *fn;
121 const struct GNUNET_DISK_FileHandle *stdout_read_handle; 122 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
122 const struct GNUNET_DISK_FileHandle *wh; 123 const struct GNUNET_DISK_FileHandle *wh;
123 124
124 GNUNET_asprintf(&fn, "cat"); 125 GNUNET_asprintf (&fn, "cat");
125 126
126 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 127 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
127 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 128 GNUNET_NO);
129 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
130 GNUNET_YES);
128 131
129 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 132 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
130 { 133 {
131 GNUNET_break(0); 134 GNUNET_break (0);
132 ok = 1; 135 ok = 1;
133 GNUNET_free(fn); 136 GNUNET_free (fn);
134 return; 137 return;
135 } 138 }
136 139
137 proc = 140 proc =
138 GNUNET_OS_start_process(GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, 141 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
139 hello_pipe_stdin, hello_pipe_stdout, NULL, 142 hello_pipe_stdin, hello_pipe_stdout, NULL,
140 fn, 143 fn,
141 "test_gnunet_echo_hello", "-", NULL); 144 "test_gnunet_echo_hello", "-", NULL);
142 GNUNET_free(fn); 145 GNUNET_free (fn);
143 146
144 /* Close the write end of the read pipe */ 147 /* Close the write end of the read pipe */
145 GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); 148 GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
146 /* Close the read end of the write pipe */ 149 /* Close the read end of the write pipe */
147 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ); 150 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
148 151
149 wh = GNUNET_DISK_pipe_handle(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); 152 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
150 153
151 /* Write the test_phrase to the cat process */ 154 /* Write the test_phrase to the cat process */
152 if (GNUNET_DISK_file_write(wh, test_phrase, strlen(test_phrase) + 1) != 155 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
153 strlen(test_phrase) + 1) 156 strlen (test_phrase) + 1)
154 { 157 {
155 GNUNET_break(0); 158 GNUNET_break (0);
156 ok = 1; 159 ok = 1;
157 return; 160 return;
158 } 161 }
159 162
160 /* Close the write end to end the cycle! */ 163 /* Close the write end to end the cycle! */
161 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); 164 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
162 165
163 stdout_read_handle = 166 stdout_read_handle =
164 GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ); 167 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
165 168
166 die_task = 169 die_task =
167 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply 170 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
168 (GNUNET_TIME_UNIT_MINUTES, 1), 171 (GNUNET_TIME_UNIT_MINUTES, 1),
169 &end_task, 172 &end_task,
170 NULL); 173 NULL);
171 174
172 memset(&rc, 0, sizeof(rc)); 175 memset (&rc, 0, sizeof(rc));
173 rc.stdout_read_handle = stdout_read_handle; 176 rc.stdout_read_handle = stdout_read_handle;
174 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 177 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
175 stdout_read_handle, 178 stdout_read_handle,
176 &read_call, 179 &read_call,
177 NULL); 180 NULL);
178} 181}
179 182
180 183
@@ -183,10 +186,10 @@ run_task(void *cls)
183 * checks that "ok" is correct at the end. 186 * checks that "ok" is correct at the end.
184 */ 187 */
185static int 188static int
186check_run() 189check_run ()
187{ 190{
188 ok = 1; 191 ok = 1;
189 GNUNET_SCHEDULER_run(&run_task, &ok); 192 GNUNET_SCHEDULER_run (&run_task, &ok);
190 return ok; 193 return ok;
191} 194}
192 195
@@ -195,40 +198,42 @@ check_run()
195 * Test killing via pipe. 198 * Test killing via pipe.
196 */ 199 */
197static int 200static int
198check_kill() 201check_kill ()
199{ 202{
200 char *fn; 203 char *fn;
201 204
202 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 205 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
203 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 206 GNUNET_NO);
207 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
208 GNUNET_YES);
204 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 209 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
205 { 210 {
206 return 1; 211 return 1;
207 } 212 }
208 fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); 213 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
209 proc = 214 proc =
210 GNUNET_OS_start_process(GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, 215 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
211 hello_pipe_stdin, 216 hello_pipe_stdin,
212 hello_pipe_stdout, 217 hello_pipe_stdout,
213 NULL, 218 NULL,
214 fn, 219 fn,
215 "gnunet-service-resolver", "-", 220 "gnunet-service-resolver", "-",
216 NULL); 221 NULL);
217 if (NULL == proc) 222 if (NULL == proc)
218 { 223 {
219 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n"); 225 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
221 return 77; 226 return 77;
222 } 227 }
223 sleep(1); /* give process time to start, so we actually use the pipe-kill mechanism! */ 228 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
224 GNUNET_free(fn); 229 GNUNET_free (fn);
225 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) 230 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
226 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 231 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
227 GNUNET_assert(GNUNET_OK == GNUNET_OS_process_wait(proc)); 232 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
228 GNUNET_OS_process_destroy(proc); 233 GNUNET_OS_process_destroy (proc);
229 proc = NULL; 234 proc = NULL;
230 GNUNET_DISK_pipe_close(hello_pipe_stdout); 235 GNUNET_DISK_pipe_close (hello_pipe_stdout);
231 GNUNET_DISK_pipe_close(hello_pipe_stdin); 236 GNUNET_DISK_pipe_close (hello_pipe_stdin);
232 return 0; 237 return 0;
233} 238}
234 239
@@ -237,55 +242,57 @@ check_kill()
237 * Test killing via pipe. 242 * Test killing via pipe.
238 */ 243 */
239static int 244static int
240check_instant_kill() 245check_instant_kill ()
241{ 246{
242 char *fn; 247 char *fn;
243 248
244 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 249 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
245 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 250 GNUNET_NO);
251 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
252 GNUNET_YES);
246 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 253 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
247 { 254 {
248 return 1; 255 return 1;
249 } 256 }
250 fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); 257 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
251 proc = 258 proc =
252 GNUNET_OS_start_process(GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, 259 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
253 hello_pipe_stdin, hello_pipe_stdout, NULL, 260 hello_pipe_stdin, hello_pipe_stdout, NULL,
254 fn, 261 fn,
255 "gnunet-service-resolver", "-", NULL); 262 "gnunet-service-resolver", "-", NULL);
256 if (NULL == proc) 263 if (NULL == proc)
257 { 264 {
258 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n"); 266 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
260 return 77; 267 return 77;
261 } 268 }
262 if (0 != GNUNET_OS_process_kill(proc, 269 if (0 != GNUNET_OS_process_kill (proc,
263 GNUNET_TERM_SIG)) 270 GNUNET_TERM_SIG))
264 { 271 {
265 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 272 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
266 } 273 }
267 GNUNET_free(fn); 274 GNUNET_free (fn);
268 GNUNET_assert(GNUNET_OK == GNUNET_OS_process_wait(proc)); 275 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
269 GNUNET_OS_process_destroy(proc); 276 GNUNET_OS_process_destroy (proc);
270 proc = NULL; 277 proc = NULL;
271 GNUNET_DISK_pipe_close(hello_pipe_stdout); 278 GNUNET_DISK_pipe_close (hello_pipe_stdout);
272 GNUNET_DISK_pipe_close(hello_pipe_stdin); 279 GNUNET_DISK_pipe_close (hello_pipe_stdin);
273 return 0; 280 return 0;
274} 281}
275 282
276 283
277int 284int
278main(int argc, char *argv[]) 285main (int argc, char *argv[])
279{ 286{
280 int ret; 287 int ret;
281 288
282 GNUNET_log_setup("test-os-start-process", 289 GNUNET_log_setup ("test-os-start-process",
283 "WARNING", 290 "WARNING",
284 NULL); 291 NULL);
285 ret = 0; 292 ret = 0;
286 ret |= check_run(); 293 ret |= check_run ();
287 ret |= check_kill(); 294 ret |= check_kill ();
288 ret |= check_instant_kill(); 295 ret |= check_instant_kill ();
289 return ret; 296 return ret;
290} 297}
291 298
diff --git a/src/util/test_peer.c b/src/util/test_peer.c
index 46a803e39..5a7203507 100644
--- a/src/util/test_peer.c
+++ b/src/util/test_peer.c
@@ -36,102 +36,104 @@ static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];
36 36
37 37
38static void 38static void
39generatePeerIdList() 39generatePeerIdList ()
40{ 40{
41 for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++) 41 for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++)
42 { 42 {
43 gcry_randomize(&pidArr[i], 43 gcry_randomize (&pidArr[i],
44 sizeof(struct GNUNET_PeerIdentity), 44 sizeof(struct GNUNET_PeerIdentity),
45 GCRY_STRONG_RANDOM); 45 GCRY_STRONG_RANDOM);
46 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 46 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
47 "Peer %u: %s\n", 47 "Peer %u: %s\n",
48 i, 48 i,
49 GNUNET_i2s(&pidArr[i])); 49 GNUNET_i2s (&pidArr[i]));
50 } 50 }
51} 51}
52 52
53 53
54static int 54static int
55check() 55check ()
56{ 56{
57 GNUNET_PEER_Id pid; 57 GNUNET_PEER_Id pid;
58 struct GNUNET_PeerIdentity res; 58 struct GNUNET_PeerIdentity res;
59 GNUNET_PEER_Id ids[] = { 1, 2, 3 }; 59 GNUNET_PEER_Id ids[] = { 1, 2, 3 };
60 60
61 GNUNET_assert(0 == GNUNET_PEER_intern(NULL)); 61 GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
62 /* Insert Peers into PeerEntry table and hashmap */ 62 /* Insert Peers into PeerEntry table and hashmap */
63 for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++) 63 for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++)
64 {
65 pid = GNUNET_PEER_intern (&pidArr[i]);
66 if (pid != (i + 1))
64 { 67 {
65 pid = GNUNET_PEER_intern(&pidArr[i]); 68 fprintf (stderr, "%s",
66 if (pid != (i + 1)) 69 "Unexpected Peer ID returned by intern function\n");
67 { 70 return 1;
68 fprintf(stderr, "%s", "Unexpected Peer ID returned by intern function\n");
69 return 1;
70 }
71 } 71 }
72 }
72 73
73 /* Referencing the first 3 peers once again */ 74 /* Referencing the first 3 peers once again */
74 for (unsigned int i = 0; i < 3; i++) 75 for (unsigned int i = 0; i < 3; i++)
76 {
77 pid = GNUNET_PEER_intern (&pidArr[i]);
78 if (pid != (i + 1))
75 { 79 {
76 pid = GNUNET_PEER_intern(&pidArr[i]); 80 fprintf (stderr, "%s",
77 if (pid != (i + 1)) 81 "Unexpected Peer ID returned by intern function\n");
78 { 82 return 1;
79 fprintf(stderr, "%s", "Unexpected Peer ID returned by intern function\n");
80 return 1;
81 }
82 } 83 }
84 }
83 85
84 /* Dereferencing the first 3 peers once [decrementing their reference count] */ 86 /* Dereferencing the first 3 peers once [decrementing their reference count] */
85 GNUNET_PEER_decrement_rcs(ids, 3); 87 GNUNET_PEER_decrement_rcs (ids, 3);
86 88
87 /* re-referencing the first 3 peers using the change_rc function */ 89 /* re-referencing the first 3 peers using the change_rc function */
88 for (unsigned int i = 1; i <= 3; i++) 90 for (unsigned int i = 1; i <= 3; i++)
89 GNUNET_PEER_change_rc(i, 1); 91 GNUNET_PEER_change_rc (i, 1);
90 92
91 /* Removing the second Peer from the PeerEntry hash map */ 93 /* Removing the second Peer from the PeerEntry hash map */
92 GNUNET_PEER_change_rc(2, -2); 94 GNUNET_PEER_change_rc (2, -2);
93 95
94 /* convert the pid of the first PeerEntry into that of the third */ 96 /* convert the pid of the first PeerEntry into that of the third */
95 GNUNET_PEER_resolve(1, 97 GNUNET_PEER_resolve (1,
96 &res); 98 &res);
97 GNUNET_assert(0 == 99 GNUNET_assert (0 ==
98 GNUNET_memcmp(&res, 100 GNUNET_memcmp (&res,
99 &pidArr[0])); 101 &pidArr[0]));
100 102
101 /* 103 /*
102 * Attempt to convert pid = 0 (which is reserved) 104 * Attempt to convert pid = 0 (which is reserved)
103 * into a peer identity object, the peer identity memory 105 * into a peer identity object, the peer identity memory
104 * is expected to be set to zero 106 * is expected to be set to zero
105 */ 107 */
106 GNUNET_log_skip(1, GNUNET_YES); 108 GNUNET_log_skip (1, GNUNET_YES);
107 GNUNET_PEER_resolve(0, &res); 109 GNUNET_PEER_resolve (0, &res);
108 GNUNET_assert(0 == 110 GNUNET_assert (0 ==
109 GNUNET_is_zero(&res)); 111 GNUNET_is_zero (&res));
110 112
111 /* Removing peer entries 1 and 3 from table using the list decrement function */ 113 /* Removing peer entries 1 and 3 from table using the list decrement function */
112 /* If count = 0, nothing should be done whatsoever */ 114 /* If count = 0, nothing should be done whatsoever */
113 GNUNET_PEER_decrement_rcs(ids, 0); 115 GNUNET_PEER_decrement_rcs (ids, 0);
114 116
115 ids[1] = 3; 117 ids[1] = 3;
116 GNUNET_PEER_decrement_rcs(ids, 2); 118 GNUNET_PEER_decrement_rcs (ids, 2);
117 GNUNET_PEER_decrement_rcs(ids, 2); 119 GNUNET_PEER_decrement_rcs (ids, 2);
118 120
119 return 0; 121 return 0;
120} 122}
121 123
122 124
123int 125int
124main() 126main ()
125{ 127{
126 GNUNET_log_setup("test-peer", 128 GNUNET_log_setup ("test-peer",
127 "ERROR", 129 "ERROR",
128 NULL); 130 NULL);
129 for (unsigned int i = 0; i < 1; i++) 131 for (unsigned int i = 0; i < 1; i++)
130 { 132 {
131 generatePeerIdList(); 133 generatePeerIdList ();
132 if (0 != check()) 134 if (0 != check ())
133 return 1; 135 return 1;
134 } 136 }
135 return 0; 137 return 0;
136} 138}
137 139
diff --git a/src/util/test_plugin.c b/src/util/test_plugin.c
index 98e6173d5..d93af8db1 100644
--- a/src/util/test_plugin.c
+++ b/src/util/test_plugin.c
@@ -26,42 +26,42 @@
26 26
27 27
28static void 28static void
29test_cb(void *cls, const char *libname, void *lib_ret) 29test_cb (void *cls, const char *libname, void *lib_ret)
30{ 30{
31 void *ret; 31 void *ret;
32 32
33 GNUNET_assert(0 == strcmp(cls, "test")); 33 GNUNET_assert (0 == strcmp (cls, "test"));
34 GNUNET_assert(0 == strcmp(lib_ret, "Hello")); 34 GNUNET_assert (0 == strcmp (lib_ret, "Hello"));
35 ret = GNUNET_PLUGIN_unload(libname, "out"); 35 ret = GNUNET_PLUGIN_unload (libname, "out");
36 GNUNET_assert(NULL != ret); 36 GNUNET_assert (NULL != ret);
37 GNUNET_assert(0 == strcmp(ret, "World")); 37 GNUNET_assert (0 == strcmp (ret, "World"));
38 free(ret); 38 free (ret);
39} 39}
40 40
41 41
42int 42int
43main(int argc, char *argv[]) 43main (int argc, char *argv[])
44{ 44{
45 void *ret; 45 void *ret;
46 46
47 GNUNET_log_setup("test-plugin", "WARNING", NULL); 47 GNUNET_log_setup ("test-plugin", "WARNING", NULL);
48 GNUNET_log_skip(1, GNUNET_NO); 48 GNUNET_log_skip (1, GNUNET_NO);
49 ret = GNUNET_PLUGIN_load("libgnunet_plugin_missing", NULL); 49 ret = GNUNET_PLUGIN_load ("libgnunet_plugin_missing", NULL);
50 GNUNET_log_skip(0, GNUNET_NO); 50 GNUNET_log_skip (0, GNUNET_NO);
51 if (ret != NULL) 51 if (ret != NULL)
52 return 1; 52 return 1;
53 ret = GNUNET_PLUGIN_load("libgnunet_plugin_test", "in"); 53 ret = GNUNET_PLUGIN_load ("libgnunet_plugin_test", "in");
54 if (ret == NULL) 54 if (ret == NULL)
55 return 1; 55 return 1;
56 if (0 != strcmp(ret, "Hello")) 56 if (0 != strcmp (ret, "Hello"))
57 return 2; 57 return 2;
58 ret = GNUNET_PLUGIN_unload("libgnunet_plugin_test", "out"); 58 ret = GNUNET_PLUGIN_unload ("libgnunet_plugin_test", "out");
59 if (ret == NULL) 59 if (ret == NULL)
60 return 3; 60 return 3;
61 if (0 != strcmp(ret, "World")) 61 if (0 != strcmp (ret, "World"))
62 return 4; 62 return 4;
63 free(ret); 63 free (ret);
64 GNUNET_PLUGIN_load_all("libgnunet_plugin_tes", "in", &test_cb, "test"); 64 GNUNET_PLUGIN_load_all ("libgnunet_plugin_tes", "in", &test_cb, "test");
65 return 0; 65 return 0;
66} 66}
67 67
diff --git a/src/util/test_plugin_plug.c b/src/util/test_plugin_plug.c
index f719a075f..b754c4c4d 100644
--- a/src/util/test_plugin_plug.c
+++ b/src/util/test_plugin_plug.c
@@ -24,18 +24,18 @@
24#include "platform.h" 24#include "platform.h"
25 25
26void * 26void *
27libgnunet_plugin_test_init(void *arg) 27libgnunet_plugin_test_init (void *arg)
28{ 28{
29 if (0 == strcmp(arg, "in")) 29 if (0 == strcmp (arg, "in"))
30 return "Hello"; 30 return "Hello";
31 return NULL; 31 return NULL;
32} 32}
33 33
34void * 34void *
35libgnunet_plugin_test_done(void *arg) 35libgnunet_plugin_test_done (void *arg)
36{ 36{
37 if (0 == strcmp(arg, "out")) 37 if (0 == strcmp (arg, "out"))
38 return strdup("World"); 38 return strdup ("World");
39 return NULL; 39 return NULL;
40} 40}
41 41
diff --git a/src/util/test_program.c b/src/util/test_program.c
index 983610f2e..5362c61cc 100644
--- a/src/util/test_program.c
+++ b/src/util/test_program.c
@@ -34,23 +34,23 @@ static int setme2;
34 * Main function that will be run. 34 * Main function that will be run.
35 */ 35 */
36static void 36static void
37runner(void *cls, 37runner (void *cls,
38 char *const *args, 38 char *const *args,
39 const char *cfgfile, 39 const char *cfgfile,
40 const struct GNUNET_CONFIGURATION_Handle *cfg) 40 const struct GNUNET_CONFIGURATION_Handle *cfg)
41{ 41{
42 int *ok = cls; 42 int *ok = cls;
43 43
44 GNUNET_assert(setme1 == 1); 44 GNUNET_assert (setme1 == 1);
45 GNUNET_assert(0 == strcmp(args[0], "extra")); 45 GNUNET_assert (0 == strcmp (args[0], "extra"));
46 GNUNET_assert(args[1] == NULL); 46 GNUNET_assert (args[1] == NULL);
47 GNUNET_assert(NULL != strstr(cfgfile, "/test_program_data.conf")); 47 GNUNET_assert (NULL != strstr (cfgfile, "/test_program_data.conf"));
48 *ok = 0; 48 *ok = 0;
49} 49}
50 50
51 51
52int 52int
53main(int argc, char *argv[]) 53main (int argc, char *argv[])
54{ 54{
55 int ok = 1; 55 int ok = 1;
56 char *const argvx[] = { 56 char *const argvx[] = {
@@ -64,72 +64,72 @@ main(int argc, char *argv[])
64 NULL 64 NULL
65 }; 65 };
66 struct GNUNET_GETOPT_CommandLineOption options1[] = { 66 struct GNUNET_GETOPT_CommandLineOption options1[] = {
67 GNUNET_GETOPT_option_flag('n', 67 GNUNET_GETOPT_option_flag ('n',
68 "name", 68 "name",
69 "description", 69 "description",
70 &setme1), 70 &setme1),
71 GNUNET_GETOPT_OPTION_END 71 GNUNET_GETOPT_OPTION_END
72 }; 72 };
73 struct GNUNET_GETOPT_CommandLineOption options2[] = { 73 struct GNUNET_GETOPT_CommandLineOption options2[] = {
74 GNUNET_GETOPT_option_flag('n', 74 GNUNET_GETOPT_option_flag ('n',
75 "name", 75 "name",
76 "description", 76 "description",
77 &setme1), 77 &setme1),
78 GNUNET_GETOPT_option_flag('N', 78 GNUNET_GETOPT_option_flag ('N',
79 "number", 79 "number",
80 "description", 80 "description",
81 &setme2), 81 &setme2),
82 GNUNET_GETOPT_OPTION_END 82 GNUNET_GETOPT_OPTION_END
83 }; 83 };
84 struct GNUNET_GETOPT_CommandLineOption options3[] = { 84 struct GNUNET_GETOPT_CommandLineOption options3[] = {
85 GNUNET_GETOPT_option_flag('N', 85 GNUNET_GETOPT_option_flag ('N',
86 "number", 86 "number",
87 "description", 87 "description",
88 &setme1), 88 &setme1),
89 GNUNET_GETOPT_option_flag('n', 89 GNUNET_GETOPT_option_flag ('n',
90 "name", 90 "name",
91 "description", 91 "description",
92 &setme2), 92 &setme2),
93 GNUNET_GETOPT_OPTION_END 93 GNUNET_GETOPT_OPTION_END
94 }; 94 };
95 struct GNUNET_GETOPT_CommandLineOption options4[] = { 95 struct GNUNET_GETOPT_CommandLineOption options4[] = {
96 GNUNET_GETOPT_option_flag('n', 96 GNUNET_GETOPT_option_flag ('n',
97 "name", 97 "name",
98 "description", 98 "description",
99 &setme1), 99 &setme1),
100 GNUNET_GETOPT_option_flag('n', 100 GNUNET_GETOPT_option_flag ('n',
101 "name", 101 "name",
102 "description", 102 "description",
103 &setme2), 103 &setme2),
104 GNUNET_GETOPT_OPTION_END 104 GNUNET_GETOPT_OPTION_END
105 }; 105 };
106 106
107 107
108 GNUNET_log_setup("test_program", 108 GNUNET_log_setup ("test_program",
109 "WARNING", 109 "WARNING",
110 NULL); 110 NULL);
111 GNUNET_assert(GNUNET_OK == 111 GNUNET_assert (GNUNET_OK ==
112 GNUNET_PROGRAM_run(7, argvx, 112 GNUNET_PROGRAM_run (7, argvx,
113 "test_program", 113 "test_program",
114 "A test", 114 "A test",
115 options1, 115 options1,
116 &runner, &ok)); 116 &runner, &ok));
117 117
118 GNUNET_assert(GNUNET_OK == 118 GNUNET_assert (GNUNET_OK ==
119 GNUNET_PROGRAM_run(7, argvx, 119 GNUNET_PROGRAM_run (7, argvx,
120 "test_program", "A test", 120 "test_program", "A test",
121 options2, 121 options2,
122 &runner, &ok)); 122 &runner, &ok));
123 GNUNET_assert(GNUNET_OK == 123 GNUNET_assert (GNUNET_OK ==
124 GNUNET_PROGRAM_run(7, argvx, 124 GNUNET_PROGRAM_run (7, argvx,
125 "test_program", "A test", 125 "test_program", "A test",
126 options3, 126 options3,
127 &runner, &ok)); 127 &runner, &ok));
128 GNUNET_assert(GNUNET_OK == 128 GNUNET_assert (GNUNET_OK ==
129 GNUNET_PROGRAM_run(7, argvx, 129 GNUNET_PROGRAM_run (7, argvx,
130 "test_program", "A test", 130 "test_program", "A test",
131 options4, 131 options4,
132 &runner, &ok)); 132 &runner, &ok));
133 133
134 return ok; 134 return ok;
135} 135}
diff --git a/src/util/test_regex.c b/src/util/test_regex.c
index c2306b971..b026ab955 100644
--- a/src/util/test_regex.c
+++ b/src/util/test_regex.c
@@ -32,12 +32,12 @@
32 32
33 33
34static int 34static int
35test_iptoregex(const char *ipv4, 35test_iptoregex (const char *ipv4,
36 uint16_t port, 36 uint16_t port,
37 const char *expectedv4, 37 const char *expectedv4,
38 const char *ipv6, 38 const char *ipv6,
39 uint16_t port6, 39 uint16_t port6,
40 const char *expectedv6) 40 const char *expectedv6)
41{ 41{
42 int error = 0; 42 int error = 0;
43 43
@@ -46,136 +46,144 @@ test_iptoregex(const char *ipv4,
46 char rxv4[GNUNET_TUN_IPV4_REGEXLEN]; 46 char rxv4[GNUNET_TUN_IPV4_REGEXLEN];
47 char rxv6[GNUNET_TUN_IPV6_REGEXLEN]; 47 char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
48 48
49 GNUNET_assert(1 == inet_pton(AF_INET, ipv4, &a)); 49 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
50 GNUNET_TUN_ipv4toregexsearch(&a, port, rxv4); 50 GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
51 51
52 if (0 != strcmp(rxv4, expectedv4)) 52 if (0 != strcmp (rxv4, expectedv4))
53 { 53 {
54 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 54 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
55 "Expected: %s but got: %s\n", 55 "Expected: %s but got: %s\n",
56 expectedv4, 56 expectedv4,
57 rxv4); 57 rxv4);
58 error++; 58 error++;
59 } 59 }
60 60
61 GNUNET_assert(1 == inet_pton(AF_INET6, ipv6, &b)); 61 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
62 GNUNET_TUN_ipv6toregexsearch(&b, port6, rxv6); 62 GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
63 if (0 != strcmp(rxv6, expectedv6)) 63 if (0 != strcmp (rxv6, expectedv6))
64 { 64 {
65 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
66 "Expected: %s but got: %s\n", 66 "Expected: %s but got: %s\n",
67 expectedv6, rxv6); 67 expectedv6, rxv6);
68 error++; 68 error++;
69 } 69 }
70 return error; 70 return error;
71} 71}
72 72
73 73
74static int 74static int
75test_policy4toregex(const char *policy, 75test_policy4toregex (const char *policy,
76 const char *regex) 76 const char *regex)
77{ 77{
78 char *r; 78 char *r;
79 int ret; 79 int ret;
80 80
81 ret = 0; 81 ret = 0;
82 r = GNUNET_TUN_ipv4policy2regex(policy); 82 r = GNUNET_TUN_ipv4policy2regex (policy);
83 if (NULL == r) 83 if (NULL == r)
84 { 84 {
85 GNUNET_break(0); 85 GNUNET_break (0);
86 return 1; 86 return 1;
87 } 87 }
88 if (0 != strcmp(regex, r)) 88 if (0 != strcmp (regex, r))
89 { 89 {
90 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 90 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
91 "Expected: `%s' but got: `%s'\n", 91 "Expected: `%s' but got: `%s'\n",
92 regex, r); 92 regex, r);
93 ret = 2; 93 ret = 2;
94 } 94 }
95 GNUNET_free(r); 95 GNUNET_free (r);
96 return ret; 96 return ret;
97} 97}
98 98
99 99
100static int 100static int
101test_policy6toregex(const char *policy, 101test_policy6toregex (const char *policy,
102 const char *regex) 102 const char *regex)
103{ 103{
104 char *r; 104 char *r;
105 int ret; 105 int ret;
106 106
107 ret = 0; 107 ret = 0;
108 r = GNUNET_TUN_ipv6policy2regex(policy); 108 r = GNUNET_TUN_ipv6policy2regex (policy);
109 if (NULL == r) 109 if (NULL == r)
110 { 110 {
111 GNUNET_break(0); 111 GNUNET_break (0);
112 return 1; 112 return 1;
113 } 113 }
114 if (0 != strcmp(regex, r)) 114 if (0 != strcmp (regex, r))
115 { 115 {
116 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 "Expected: `%s' but got: `%s'\n", 117 "Expected: `%s' but got: `%s'\n",
118 regex, r); 118 regex, r);
119 ret = 2; 119 ret = 2;
120 } 120 }
121 GNUNET_free(r); 121 GNUNET_free (r);
122 return ret; 122 return ret;
123} 123}
124 124
125 125
126int 126int
127main(int argc, char *argv[]) 127main (int argc, char *argv[])
128{ 128{
129 int error; 129 int error;
130 char *r; 130 char *r;
131 131
132 GNUNET_log_setup("test-regex", "WARNING", NULL); 132 GNUNET_log_setup ("test-regex", "WARNING", NULL);
133 error = 0; 133 error = 0;
134 134
135 /* this is just a performance test ... */ 135 /* this is just a performance test ... */
136 r = GNUNET_TUN_ipv4policy2regex("1.2.3.4/16:!25;"); 136 r = GNUNET_TUN_ipv4policy2regex ("1.2.3.4/16:!25;");
137 GNUNET_break(NULL != r); 137 GNUNET_break (NULL != r);
138 GNUNET_free(r); 138 GNUNET_free (r);
139 139
140 error += 140 error +=
141 test_iptoregex("192.1.2.3", 2086, 141 test_iptoregex ("192.1.2.3", 2086,
142 "4-0826-C0010203", 142 "4-0826-C0010203",
143 "FFFF::1", 8080, 143 "FFFF::1", 8080,
144 "6-1F90-FFFF0000000000000000000000000001"); 144 "6-1F90-FFFF0000000000000000000000000001");
145 error += 145 error +=
146 test_iptoregex("187.238.255.0", 80, 146 test_iptoregex ("187.238.255.0", 80,
147 "4-0050-BBEEFF00", 147 "4-0050-BBEEFF00",
148 "E1E1:73F9:51BE::0", 49, 148 "E1E1:73F9:51BE::0", 49,
149 "6-0031-E1E173F951BE00000000000000000000"); 149 "6-0031-E1E173F951BE00000000000000000000");
150 error += 150 error +=
151 test_policy4toregex("192.1.2.0/24:80;", 151 test_policy4toregex ("192.1.2.0/24:80;",
152 "4-0050-C00102" DOT DOT); 152 "4-0050-C00102" DOT DOT);
153 error += 153 error +=
154 test_policy4toregex("192.1.0.0/16;", 154 test_policy4toregex ("192.1.0.0/16;",
155 "4-" DOT DOT DOT DOT "-C001" DOT DOT DOT DOT); 155 "4-" DOT DOT DOT DOT "-C001" DOT DOT DOT DOT);
156 error += 156 error +=
157 test_policy4toregex("192.1.0.0/16:80-81;", 157 test_policy4toregex ("192.1.0.0/16:80-81;",
158 "4-(0050|0051)-C001" DOT DOT DOT DOT); 158 "4-(0050|0051)-C001" DOT DOT DOT DOT);
159 error += 159 error +=
160 test_policy4toregex("192.1.0.0/8:!3-65535;", 160 test_policy4toregex ("192.1.0.0/8:!3-65535;",
161 "4-000(0|1|2)-C0" DOT DOT DOT DOT DOT DOT); 161 "4-000(0|1|2)-C0" DOT DOT DOT DOT DOT DOT);
162 error += 162 error +=
163 test_policy4toregex("192.1.0.0/8:!25-56;", 163 test_policy4toregex ("192.1.0.0/8:!25-56;",
164 "4-(0(0(0"DOT "|1(0|1|2|3|4|5|6|7|8)|3(9|A|B|C|D|E|F)|(4|5|6|7|8|9|A|B|C|D|E|F)"DOT ")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT ")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT DOT ")-C0"DOT DOT DOT DOT DOT DOT); 164 "4-(0(0(0"DOT
165 "|1(0|1|2|3|4|5|6|7|8)|3(9|A|B|C|D|E|F)|(4|5|6|7|8|9|A|B|C|D|E|F)"DOT
166 ")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT
167 ")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT
168 DOT ")-C0"DOT DOT DOT DOT DOT DOT);
165 error += 169 error +=
166 test_policy6toregex("E1E1::1;", 170 test_policy6toregex ("E1E1::1;",
167 "6-"DOT DOT DOT DOT "-E1E10000000000000000000000000001"); 171 "6-"DOT DOT DOT
172 DOT "-E1E10000000000000000000000000001");
168 error += 173 error +=
169 test_policy6toregex("E1E1:ABCD::1/120;", 174 test_policy6toregex ("E1E1:ABCD::1/120;",
170 "6-"DOT DOT DOT DOT "-E1E1ABCD0000000000000000000000" DOT DOT); 175 "6-"DOT DOT DOT
176 DOT "-E1E1ABCD0000000000000000000000" DOT DOT);
171 error += 177 error +=
172 test_policy6toregex("E1E1:ABCD::ABCD/126;", 178 test_policy6toregex ("E1E1:ABCD::ABCD/126;",
173 "6-"DOT DOT DOT DOT "-E1E1ABCD00000000000000000000ABC(C|D|E|F)"); 179 "6-"DOT DOT DOT
180 DOT "-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
174 error += 181 error +=
175 test_policy6toregex("E1E1:ABCD::ABCD/127;", 182 test_policy6toregex ("E1E1:ABCD::ABCD/127;",
176 "6-"DOT DOT DOT DOT "-E1E1ABCD00000000000000000000ABC(C|D)"); 183 "6-"DOT DOT DOT
184 DOT "-E1E1ABCD00000000000000000000ABC(C|D)");
177 error += 185 error +=
178 test_policy6toregex("E1E1:ABCD::ABCD/128:80;", 186 test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
179 "6-0050-E1E1ABCD00000000000000000000ABCD"); 187 "6-0050-E1E1ABCD00000000000000000000ABCD");
180 return error; 188 return error;
181} 189}
diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c
index 0b27a2eff..13fda53bf 100644
--- a/src/util/test_resolver_api.c
+++ b/src/util/test_resolver_api.c
@@ -40,300 +40,300 @@ static int disable_rootserver_check;
40 40
41 41
42static void 42static void
43check_hostname(void *cls, 43check_hostname (void *cls,
44 const struct sockaddr *sa, 44 const struct sockaddr *sa,
45 socklen_t salen) 45 socklen_t salen)
46{ 46{
47 int *ok = cls; 47 int *ok = cls;
48 48
49 if (0 == salen) 49 if (0 == salen)
50 { 50 {
51 (*ok) &= ~8; 51 (*ok) &= ~8;
52 return; 52 return;
53 } 53 }
54 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 54 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
55 "Got IP address `%s' for our host.\n", 55 "Got IP address `%s' for our host.\n",
56 GNUNET_a2s(sa, salen)); 56 GNUNET_a2s (sa, salen));
57} 57}
58 58
59 59
60static void 60static void
61check_localhost_num(void *cls, 61check_localhost_num (void *cls,
62 const char *hostname) 62 const char *hostname)
63{ 63{
64 int *ok = cls; 64 int *ok = cls;
65 65
66 if (hostname == NULL) 66 if (hostname == NULL)
67 return; 67 return;
68 if (0 == strcmp(hostname, "127.0.0.1")) 68 if (0 == strcmp (hostname, "127.0.0.1"))
69 { 69 {
70 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71 "Received correct hostname `%s'.\n", 71 "Received correct hostname `%s'.\n",
72 hostname); 72 hostname);
73 (*ok) &= ~4; 73 (*ok) &= ~4;
74 } 74 }
75 else 75 else
76 { 76 {
77 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
78 "Received invalid hostname `%s'.\n", 78 "Received invalid hostname `%s'.\n",
79 hostname); 79 hostname);
80 GNUNET_break(0); 80 GNUNET_break (0);
81 } 81 }
82} 82}
83 83
84 84
85static void 85static void
86check_localhost(void *cls, 86check_localhost (void *cls,
87 const char *hostname) 87 const char *hostname)
88{ 88{
89 int *ok = cls; 89 int *ok = cls;
90 90
91 if (NULL == hostname) 91 if (NULL == hostname)
92 return; 92 return;
93 if (0 == strcmp(hostname, "localhost")) 93 if (0 == strcmp (hostname, "localhost"))
94 { 94 {
95 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "Received correct hostname `%s'.\n", 96 "Received correct hostname `%s'.\n",
97 hostname); 97 hostname);
98 (*ok) &= ~2; 98 (*ok) &= ~2;
99 } 99 }
100 else 100 else
101 { 101 {
102 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 102 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
103 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n", 103 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
104 hostname); 104 hostname);
105 } 105 }
106} 106}
107 107
108 108
109static void 109static void
110check_127(void *cls, const struct sockaddr *sa, socklen_t salen) 110check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
111{ 111{
112 int *ok = cls; 112 int *ok = cls;
113 const struct sockaddr_in *sai = (const struct sockaddr_in *)sa; 113 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
114 114
115 if (NULL == sa) 115 if (NULL == sa)
116 return; 116 return;
117 GNUNET_assert(sizeof(struct sockaddr_in) == salen); 117 GNUNET_assert (sizeof(struct sockaddr_in) == salen);
118 if (sai->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) 118 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
119 { 119 {
120 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Received correct address.\n"); 121 "Received correct address.\n");
122 (*ok) &= ~1; 122 (*ok) &= ~1;
123 } 123 }
124 else 124 else
125 { 125 {
126 char buf[INET_ADDRSTRLEN]; 126 char buf[INET_ADDRSTRLEN];
127 127
128 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 "Received incorrect address `%s'.\n", 129 "Received incorrect address `%s'.\n",
130 inet_ntop(AF_INET, 130 inet_ntop (AF_INET,
131 &sai->sin_addr, 131 &sai->sin_addr,
132 buf, 132 buf,
133 sizeof(buf))); 133 sizeof(buf)));
134 GNUNET_break(0); 134 GNUNET_break (0);
135 } 135 }
136} 136}
137 137
138 138
139static void 139static void
140check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen) 140check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
141{ 141{
142 int *ok = cls; 142 int *ok = cls;
143 const struct sockaddr_in *sai = (const struct sockaddr_in *)sa; 143 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
144 144
145 if (NULL == sa) 145 if (NULL == sa)
146 return; 146 return;
147 GNUNET_assert(sizeof(struct sockaddr_in) == salen); 147 GNUNET_assert (sizeof(struct sockaddr_in) == salen);
148 148
149 if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP)) 149 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
150 { 150 {
151 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Received correct rootserver ip address.\n"); 152 "Received correct rootserver ip address.\n");
153 (*ok) &= ~1; 153 (*ok) &= ~1;
154 } 154 }
155 else 155 else
156 { 156 {
157 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Received incorrect rootserver ip address.\n"); 158 "Received incorrect rootserver ip address.\n");
159 GNUNET_break(0); 159 GNUNET_break (0);
160 } 160 }
161} 161}
162 162
163 163
164static void 164static void
165check_rootserver_name(void *cls, 165check_rootserver_name (void *cls,
166 const char *hostname) 166 const char *hostname)
167{ 167{
168 int *ok = cls; 168 int *ok = cls;
169 169
170 if (NULL == hostname) 170 if (NULL == hostname)
171 return; 171 return;
172 172
173 if (0 == strcmp(hostname, ROOTSERVER_NAME)) 173 if (0 == strcmp (hostname, ROOTSERVER_NAME))
174 { 174 {
175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Received correct rootserver hostname `%s'.\n", 176 "Received correct rootserver hostname `%s'.\n",
177 hostname); 177 hostname);
178 (*ok) &= ~2; 178 (*ok) &= ~2;
179 } 179 }
180 else 180 else
181 { 181 {
182 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
183 "Received invalid rootserver hostname `%s', expected `%s'\n", 183 "Received invalid rootserver hostname `%s', expected `%s'\n",
184 hostname, 184 hostname,
185 ROOTSERVER_NAME); 185 ROOTSERVER_NAME);
186 GNUNET_break(disable_rootserver_check); 186 GNUNET_break (disable_rootserver_check);
187 } 187 }
188} 188}
189 189
190 190
191static void 191static void
192run(void *cls, char *const *args, const char *cfgfile, 192run (void *cls, char *const *args, const char *cfgfile,
193 const struct GNUNET_CONFIGURATION_Handle *cfg) 193 const struct GNUNET_CONFIGURATION_Handle *cfg)
194{ 194{
195 int *ok = cls; 195 int *ok = cls;
196 struct sockaddr_in sa; 196 struct sockaddr_in sa;
197 struct GNUNET_TIME_Relative timeout = 197 struct GNUNET_TIME_Relative timeout =
198 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30); 198 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
199 int count_ips = 0; 199 int count_ips = 0;
200 char *own_fqdn; 200 char *own_fqdn;
201 const char *rootserver_name = ROOTSERVER_NAME; 201 const char *rootserver_name = ROOTSERVER_NAME;
202 struct hostent *rootserver; 202 struct hostent *rootserver;
203 struct in_addr rootserver_addr; 203 struct in_addr rootserver_addr;
204 204
205 memset(&sa, 0, sizeof(sa)); 205 memset (&sa, 0, sizeof(sa));
206 sa.sin_family = AF_INET; 206 sa.sin_family = AF_INET;
207#if HAVE_SOCKADDR_IN_SIN_LEN 207#if HAVE_SOCKADDR_IN_SIN_LEN
208 sa.sin_len = (u_char)sizeof(sa); 208 sa.sin_len = (u_char) sizeof(sa);
209#endif 209#endif
210 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 210 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
211 211
212 /* 212 /*
213 * Looking up our own fqdn 213 * Looking up our own fqdn
214 */ 214 */
215 own_fqdn = GNUNET_RESOLVER_local_fqdn_get(); 215 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
216 /* can't really check, only thing we can safely 216 /* can't really check, only thing we can safely
217 compare against is our own identical logic... */ 217 compare against is our own identical logic... */
218 GNUNET_free_non_null(own_fqdn); 218 GNUNET_free_non_null (own_fqdn);
219 219
220 /* 220 /*
221 * Testing non-local DNS resolution 221 * Testing non-local DNS resolution
222 * DNS rootserver to test: a.root-servers.net - 198.41.0.4 222 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
223 */ 223 */
224 224
225 rootserver = gethostbyname(rootserver_name); 225 rootserver = gethostbyname (rootserver_name);
226 if (NULL == rootserver) 226 if (NULL == rootserver)
227 { 227 {
228 /* Error: resolving ip addresses does not work */ 228 /* Error: resolving ip addresses does not work */
229 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 _("gethostbyname() could not lookup IP address: %s\n"), 230 _ ("gethostbyname() could not lookup IP address: %s\n"),
231 hstrerror(h_errno)); 231 hstrerror (h_errno));
232 fprintf(stderr, 232 fprintf (stderr,
233 "%s", 233 "%s",
234 "System seems to be off-line, will not run all DNS tests\n"); 234 "System seems to be off-line, will not run all DNS tests\n");
235 *ok = 0; /* mark test as passing anyway */ 235 *ok = 0; /* mark test as passing anyway */
236 return; 236 return;
237 } 237 }
238 238
239 /* Counting returned IP addresses */ 239 /* Counting returned IP addresses */
240 while (NULL != rootserver->h_addr_list[count_ips]) 240 while (NULL != rootserver->h_addr_list[count_ips])
241 count_ips++; 241 count_ips++;
242 if (count_ips > 1) 242 if (count_ips > 1)
243 { 243 {
244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "IP received range for root name server, but a root name server has only 1 IP\n"); 245 "IP received range for root name server, but a root name server has only 1 IP\n");
246 GNUNET_break(0); 246 GNUNET_break (0);
247 } 247 }
248 248
249 /* Comparing to resolved address to the address the root name server should have */ 249 /* Comparing to resolved address to the address the root name server should have */
250 if (0 != 250 if (0 !=
251 strcmp(inet_ntoa(*(struct in_addr *)rootserver->h_addr_list[0]), 251 strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
252 ROOTSERVER_IP)) 252 ROOTSERVER_IP))
253 { 253 {
254 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255 "IP received and IP for root name server differ\n"); 255 "IP received and IP for root name server differ\n");
256 GNUNET_break(0); 256 GNUNET_break (0);
257 } 257 }
258 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
259 "System's own forward name resolution is working\n"); 259 "System's own forward name resolution is working\n");
260 /* Resolve the same using GNUNET */ 260 /* Resolve the same using GNUNET */
261 GNUNET_RESOLVER_ip_get(ROOTSERVER_NAME, AF_INET, timeout, 261 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
262 &check_rootserver_ip, cls); 262 &check_rootserver_ip, cls);
263 GNUNET_RESOLVER_ip_get(ROOTSERVER_NAME, AF_INET, timeout, 263 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
264 &check_rootserver_ip, cls); 264 &check_rootserver_ip, cls);
265 265
266 /* 266 /*
267 * Success: forward lookups work as expected 267 * Success: forward lookups work as expected
268 * Next step: reverse lookups 268 * Next step: reverse lookups
269 */ 269 */
270 if (1 != inet_pton(AF_INET, 270 if (1 != inet_pton (AF_INET,
271 ROOTSERVER_IP, 271 ROOTSERVER_IP,
272 &rootserver_addr)) 272 &rootserver_addr))
273 { 273 {
274 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Could not transform root name server IP address\n"); 275 "Could not transform root name server IP address\n");
276 GNUNET_break(0); 276 GNUNET_break (0);
277 } 277 }
278 278
279 rootserver = 279 rootserver =
280 gethostbyaddr((const void *)&rootserver_addr, 280 gethostbyaddr ((const void *) &rootserver_addr,
281 sizeof(rootserver_addr), 281 sizeof(rootserver_addr),
282 AF_INET); 282 AF_INET);
283 if (NULL == rootserver) 283 if (NULL == rootserver)
284 { 284 {
285 /* Error: resolving IP addresses does not work */ 285 /* Error: resolving IP addresses does not work */
286 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287 "gethostbyaddr() could not lookup hostname: %s\n", 287 "gethostbyaddr() could not lookup hostname: %s\n",
288 hstrerror(h_errno)); 288 hstrerror (h_errno));
289 disable_rootserver_check = GNUNET_YES; 289 disable_rootserver_check = GNUNET_YES;
290 } 290 }
291 else 291 else
292 {
293 if (0 != strcmp (rootserver->h_name,
294 ROOTSERVER_NAME))
292 { 295 {
293 if (0 != strcmp(rootserver->h_name, 296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
294 ROOTSERVER_NAME)) 297 "Received hostname and hostname for root name server differ\n");
295 { 298 disable_rootserver_check = GNUNET_YES;
296 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
297 "Received hostname and hostname for root name server differ\n");
298 disable_rootserver_check = GNUNET_YES;
299 }
300 } 299 }
300 }
301 301
302 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
303 "System's own reverse name resolution is working\n"); 303 "System's own reverse name resolution is working\n");
304 /* Resolve the same using GNUNET */ 304 /* Resolve the same using GNUNET */
305 memset(&sa, 0, sizeof(sa)); 305 memset (&sa, 0, sizeof(sa));
306 sa.sin_family = AF_INET; 306 sa.sin_family = AF_INET;
307#if HAVE_SOCKADDR_IN_SIN_LEN 307#if HAVE_SOCKADDR_IN_SIN_LEN
308 sa.sin_len = (u_char)sizeof(sa); 308 sa.sin_len = (u_char) sizeof(sa);
309#endif 309#endif
310 inet_aton(ROOTSERVER_IP, &sa.sin_addr); 310 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
311 311
312 GNUNET_RESOLVER_hostname_get((const struct sockaddr *)&sa, 312 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
313 sizeof(struct sockaddr), GNUNET_YES, timeout, 313 sizeof(struct sockaddr), GNUNET_YES, timeout,
314 &check_rootserver_name, cls); 314 &check_rootserver_name, cls);
315 315
316 memset(&sa, 0, sizeof(sa)); 316 memset (&sa, 0, sizeof(sa));
317 sa.sin_family = AF_INET; 317 sa.sin_family = AF_INET;
318#if HAVE_SOCKADDR_IN_SIN_LEN 318#if HAVE_SOCKADDR_IN_SIN_LEN
319 sa.sin_len = (u_char)sizeof(sa); 319 sa.sin_len = (u_char) sizeof(sa);
320#endif 320#endif
321 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 321 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
322 322
323 GNUNET_RESOLVER_ip_get("localhost", AF_INET, timeout, &check_127, cls); 323 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
324 GNUNET_RESOLVER_hostname_get((const struct sockaddr *)&sa, 324 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
325 sizeof(struct sockaddr), GNUNET_YES, timeout, 325 sizeof(struct sockaddr), GNUNET_YES, timeout,
326 &check_localhost, cls); 326 &check_localhost, cls);
327 327
328 GNUNET_RESOLVER_hostname_get((const struct sockaddr *)&sa, 328 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
329 sizeof(struct sockaddr), GNUNET_NO, timeout, 329 sizeof(struct sockaddr), GNUNET_NO, timeout,
330 &check_localhost_num, cls); 330 &check_localhost_num, cls);
331 GNUNET_RESOLVER_hostname_resolve(AF_UNSPEC, timeout, &check_hostname, cls); 331 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
332} 332}
333 333
334 334
335int 335int
336main(int argc, char *argv[]) 336main (int argc, char *argv[])
337{ 337{
338 int ok = 1 + 2 + 4 + 8; 338 int ok = 1 + 2 + 4 + 8;
339 char *fn; 339 char *fn;
@@ -344,32 +344,32 @@ main(int argc, char *argv[])
344 struct GNUNET_GETOPT_CommandLineOption options[] = 344 struct GNUNET_GETOPT_CommandLineOption options[] =
345 { GNUNET_GETOPT_OPTION_END }; 345 { GNUNET_GETOPT_OPTION_END };
346 346
347 GNUNET_log_setup("test-resolver-api", 347 GNUNET_log_setup ("test-resolver-api",
348 "WARNING", 348 "WARNING",
349 NULL); 349 NULL);
350 fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); 350 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
351 proc = GNUNET_OS_start_process(GNUNET_YES, 351 proc = GNUNET_OS_start_process (GNUNET_YES,
352 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 352 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
353 NULL, NULL, NULL, 353 NULL, NULL, NULL,
354 fn, 354 fn,
355 "gnunet-service-resolver", 355 "gnunet-service-resolver",
356 "-c", "test_resolver_api_data.conf", NULL); 356 "-c", "test_resolver_api_data.conf", NULL);
357 GNUNET_assert(NULL != proc); 357 GNUNET_assert (NULL != proc);
358 GNUNET_free(fn); 358 GNUNET_free (fn);
359 GNUNET_assert(GNUNET_OK == 359 GNUNET_assert (GNUNET_OK ==
360 GNUNET_PROGRAM_run((sizeof(argvx) / sizeof(char *)) - 1, 360 GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
361 argvx, "test-resolver-api", "nohelp", 361 argvx, "test-resolver-api", "nohelp",
362 options, &run, &ok)); 362 options, &run, &ok));
363 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) 363 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
364 { 364 {
365 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 365 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
366 ok = 1; 366 ok = 1;
367 } 367 }
368 GNUNET_OS_process_wait(proc); 368 GNUNET_OS_process_wait (proc);
369 GNUNET_OS_process_destroy(proc); 369 GNUNET_OS_process_destroy (proc);
370 proc = NULL; 370 proc = NULL;
371 if (0 != ok) 371 if (0 != ok)
372 fprintf(stderr, "Missed some resolutions: %u\n", ok); 372 fprintf (stderr, "Missed some resolutions: %u\n", ok);
373 return ok; 373 return ok;
374} 374}
375 375
diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c
index 8952225c2..2fe8f6f5c 100644
--- a/src/util/test_scheduler.c
+++ b/src/util/test_scheduler.c
@@ -33,135 +33,135 @@ static struct GNUNET_SCHEDULER_Task *never_run_task;
33 33
34 34
35static void 35static void
36task2(void *cls) 36task2 (void *cls)
37{ 37{
38 int *ok = cls; 38 int *ok = cls;
39 39
40 /* t3 should be ready (albeit with lower priority) */ 40 /* t3 should be ready (albeit with lower priority) */
41 GNUNET_assert(1 == 41 GNUNET_assert (1 ==
42 GNUNET_SCHEDULER_get_load(GNUNET_SCHEDULER_PRIORITY_COUNT)); 42 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
43 GNUNET_assert(2 == *ok); 43 GNUNET_assert (2 == *ok);
44 (*ok) = 3; 44 (*ok) = 3;
45} 45}
46 46
47 47
48static void 48static void
49task3(void *cls) 49task3 (void *cls)
50{ 50{
51 int *ok = cls; 51 int *ok = cls;
52 52
53 GNUNET_assert(3 == *ok); 53 GNUNET_assert (3 == *ok);
54 (*ok) = 4; 54 (*ok) = 4;
55} 55}
56 56
57 57
58static void 58static void
59taskWrt(void *cls) 59taskWrt (void *cls)
60{ 60{
61 static char c; 61 static char c;
62 int *ok = cls; 62 int *ok = cls;
63 const struct GNUNET_SCHEDULER_TaskContext *tc; 63 const struct GNUNET_SCHEDULER_TaskContext *tc;
64 64
65 tc = GNUNET_SCHEDULER_get_task_context(); 65 tc = GNUNET_SCHEDULER_get_task_context ();
66 GNUNET_assert(6 == *ok); 66 GNUNET_assert (6 == *ok);
67 GNUNET_assert(GNUNET_NETWORK_fdset_handle_isset(tc->write_ready, fds[1])); 67 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
68 (*ok) = 7; 68 (*ok) = 7;
69 GNUNET_assert(1 == GNUNET_DISK_file_write(fds[1], &c, 1)); 69 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
70} 70}
71 71
72 72
73static void 73static void
74taskNeverRun(void *cls) 74taskNeverRun (void *cls)
75{ 75{
76 GNUNET_assert(0); 76 GNUNET_assert (0);
77} 77}
78 78
79 79
80static void 80static void
81taskLastRd(void *cls) 81taskLastRd (void *cls)
82{ 82{
83 int *ok = cls; 83 int *ok = cls;
84 84
85 GNUNET_assert(8 == *ok); 85 GNUNET_assert (8 == *ok);
86 (*ok) = 0; 86 (*ok) = 0;
87} 87}
88 88
89 89
90static void 90static void
91taskLastSig(void *cls) 91taskLastSig (void *cls)
92{ 92{
93 int *ok = cls; 93 int *ok = cls;
94 94
95 GNUNET_SCHEDULER_cancel(never_run_task); 95 GNUNET_SCHEDULER_cancel (never_run_task);
96 GNUNET_assert(9 == *ok); 96 GNUNET_assert (9 == *ok);
97 (*ok) = 0; 97 (*ok) = 0;
98} 98}
99 99
100 100
101static void 101static void
102taskLastShutdown(void *cls) 102taskLastShutdown (void *cls)
103{ 103{
104 int *ok = cls; 104 int *ok = cls;
105 105
106 GNUNET_assert(10 == *ok); 106 GNUNET_assert (10 == *ok);
107 (*ok) = 0; 107 (*ok) = 0;
108} 108}
109 109
110 110
111static void 111static void
112taskRd(void *cls) 112taskRd (void *cls)
113{ 113{
114 static char c; 114 static char c;
115 int *ok = cls; 115 int *ok = cls;
116 const struct GNUNET_SCHEDULER_TaskContext *tc; 116 const struct GNUNET_SCHEDULER_TaskContext *tc;
117 117
118 tc = GNUNET_SCHEDULER_get_task_context(); 118 tc = GNUNET_SCHEDULER_get_task_context ();
119 GNUNET_assert(7 == *ok); 119 GNUNET_assert (7 == *ok);
120 GNUNET_assert(GNUNET_NETWORK_fdset_handle_isset(tc->read_ready, fds[0])); 120 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
121 GNUNET_assert(1 == GNUNET_DISK_file_read(fds[0], &c, 1)); 121 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
122 (*ok) = 8; 122 (*ok) = 8;
123 GNUNET_SCHEDULER_add_shutdown(&taskLastRd, 123 GNUNET_SCHEDULER_add_shutdown (&taskLastRd,
124 cls); 124 cls);
125 GNUNET_SCHEDULER_shutdown(); 125 GNUNET_SCHEDULER_shutdown ();
126} 126}
127 127
128 128
129static void 129static void
130task4(void *cls) 130task4 (void *cls)
131{ 131{
132 int *ok = cls; 132 int *ok = cls;
133 133
134 GNUNET_assert(4 == *ok); 134 GNUNET_assert (4 == *ok);
135 (*ok) = 6; 135 (*ok) = 6;
136 p = GNUNET_DISK_pipe(GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO); 136 p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
137 GNUNET_assert(NULL != p); 137 GNUNET_assert (NULL != p);
138 fds[0] = GNUNET_DISK_pipe_handle(p, GNUNET_DISK_PIPE_END_READ); 138 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
139 fds[1] = GNUNET_DISK_pipe_handle(p, GNUNET_DISK_PIPE_END_WRITE); 139 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
140 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 140 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
141 fds[0], 141 fds[0],
142 &taskRd, 142 &taskRd,
143 cls);
144 GNUNET_SCHEDULER_add_write_file(GNUNET_TIME_UNIT_FOREVER_REL,
145 fds[1],
146 &taskWrt,
147 cls); 143 cls);
144 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
145 fds[1],
146 &taskWrt,
147 cls);
148} 148}
149 149
150 150
151static void 151static void
152task1(void *cls) 152task1 (void *cls)
153{ 153{
154 int *ok = cls; 154 int *ok = cls;
155 155
156 GNUNET_assert(1 == *ok); 156 GNUNET_assert (1 == *ok);
157 (*ok) = 2; 157 (*ok) = 2;
158 GNUNET_SCHEDULER_add_now(&task3, cls); 158 GNUNET_SCHEDULER_add_now (&task3, cls);
159 GNUNET_SCHEDULER_add_with_priority(GNUNET_SCHEDULER_PRIORITY_UI, 159 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
160 &task2, 160 &task2,
161 cls); 161 cls);
162 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, 162 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
163 &task4, 163 &task4,
164 cls); 164 cls);
165} 165}
166 166
167 167
@@ -170,27 +170,27 @@ task1(void *cls)
170 * checks that "ok" is correct at the end. 170 * checks that "ok" is correct at the end.
171 */ 171 */
172static int 172static int
173check() 173check ()
174{ 174{
175 int ok; 175 int ok;
176 176
177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "[Check scheduling]\n"); 178 "[Check scheduling]\n");
179 ok = 1; 179 ok = 1;
180 GNUNET_SCHEDULER_run(&task1, &ok); 180 GNUNET_SCHEDULER_run (&task1, &ok);
181 return ok; 181 return ok;
182} 182}
183 183
184 184
185static void 185static void
186taskShutdown(void *cls) 186taskShutdown (void *cls)
187{ 187{
188 int *ok = cls; 188 int *ok = cls;
189 189
190 GNUNET_assert(1 == *ok); 190 GNUNET_assert (1 == *ok);
191 *ok = 10; 191 *ok = 10;
192 GNUNET_SCHEDULER_add_shutdown(&taskLastShutdown, cls); 192 GNUNET_SCHEDULER_add_shutdown (&taskLastShutdown, cls);
193 GNUNET_SCHEDULER_shutdown(); 193 GNUNET_SCHEDULER_shutdown ();
194} 194}
195 195
196 196
@@ -199,32 +199,33 @@ taskShutdown(void *cls)
199 * checks that "ok" is correct at the end. 199 * checks that "ok" is correct at the end.
200 */ 200 */
201static int 201static int
202checkShutdown() 202checkShutdown ()
203{ 203{
204 int ok; 204 int ok;
205 205
206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "[Check shutdown]\n"); 207 "[Check shutdown]\n");
208 ok = 1; 208 ok = 1;
209 GNUNET_SCHEDULER_run(&taskShutdown, &ok); 209 GNUNET_SCHEDULER_run (&taskShutdown, &ok);
210 return ok; 210 return ok;
211} 211}
212 212
213 213
214static void 214static void
215taskSig(void *cls) 215taskSig (void *cls)
216{ 216{
217 int *ok = cls; 217 int *ok = cls;
218 218
219 GNUNET_assert(1 == *ok); 219 GNUNET_assert (1 == *ok);
220 *ok = 9; 220 *ok = 9;
221 GNUNET_SCHEDULER_add_shutdown(&taskLastSig, cls); 221 GNUNET_SCHEDULER_add_shutdown (&taskLastSig, cls);
222 never_run_task = 222 never_run_task =
223 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), 223 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
224 &taskNeverRun, 224 GNUNET_TIME_UNIT_SECONDS, 5),
225 NULL); 225 &taskNeverRun,
226 GNUNET_break(0 == kill(getpid(), 226 NULL);
227 GNUNET_TERM_SIG)); 227 GNUNET_break (0 == kill (getpid (),
228 GNUNET_TERM_SIG));
228} 229}
229 230
230 231
@@ -233,26 +234,26 @@ taskSig(void *cls)
233 * checks that "ok" is correct at the end. 234 * checks that "ok" is correct at the end.
234 */ 235 */
235static int 236static int
236checkSignal() 237checkSignal ()
237{ 238{
238 int ok; 239 int ok;
239 240
240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "[Check signal handling]\n"); 242 "[Check signal handling]\n");
242 ok = 1; 243 ok = 1;
243 GNUNET_SCHEDULER_run(&taskSig, &ok); 244 GNUNET_SCHEDULER_run (&taskSig, &ok);
244 return ok; 245 return ok;
245} 246}
246 247
247 248
248static void 249static void
249taskCancel(void *cls) 250taskCancel (void *cls)
250{ 251{
251 int *ok = cls; 252 int *ok = cls;
252 253
253 GNUNET_assert(1 == *ok); 254 GNUNET_assert (1 == *ok);
254 *ok = 0; 255 *ok = 0;
255 GNUNET_SCHEDULER_cancel(GNUNET_SCHEDULER_add_now(&taskNeverRun, NULL)); 256 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now (&taskNeverRun, NULL));
256} 257}
257 258
258 259
@@ -261,29 +262,29 @@ taskCancel(void *cls)
261 * checks that "ok" is correct at the end. 262 * checks that "ok" is correct at the end.
262 */ 263 */
263static int 264static int
264checkCancel() 265checkCancel ()
265{ 266{
266 int ok; 267 int ok;
267 268
268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "[Check task cancellation]\n"); 270 "[Check task cancellation]\n");
270 ok = 1; 271 ok = 1;
271 GNUNET_SCHEDULER_run(&taskCancel, &ok); 272 GNUNET_SCHEDULER_run (&taskCancel, &ok);
272 return ok; 273 return ok;
273} 274}
274 275
275 276
276int 277int
277main(int argc, char *argv[]) 278main (int argc, char *argv[])
278{ 279{
279 int ret = 0; 280 int ret = 0;
280 281
281 GNUNET_log_setup("test_scheduler", "WARNING", NULL); 282 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
282 ret += check(); 283 ret += check ();
283 ret += checkCancel(); 284 ret += checkCancel ();
284 ret += checkSignal(); 285 ret += checkSignal ();
285 ret += checkShutdown(); 286 ret += checkShutdown ();
286 GNUNET_DISK_pipe_close(p); 287 GNUNET_DISK_pipe_close (p);
287 288
288 return ret; 289 return ret;
289} 290}
diff --git a/src/util/test_scheduler_delay.c b/src/util/test_scheduler_delay.c
index c72ee7b19..7dfb36291 100644
--- a/src/util/test_scheduler_delay.c
+++ b/src/util/test_scheduler_delay.c
@@ -43,51 +43,51 @@ static unsigned long long cumDelta;
43 * @param cls closure 43 * @param cls closure
44 */ 44 */
45static void 45static void
46test_task(void *cls) 46test_task (void *cls)
47{ 47{
48 struct GNUNET_TIME_Absolute now; 48 struct GNUNET_TIME_Absolute now;
49 49
50 now = GNUNET_TIME_absolute_get(); 50 now = GNUNET_TIME_absolute_get ();
51 if (now.abs_value_us > target.abs_value_us) 51 if (now.abs_value_us > target.abs_value_us)
52 cumDelta += (now.abs_value_us - target.abs_value_us); 52 cumDelta += (now.abs_value_us - target.abs_value_us);
53 else 53 else
54 cumDelta += (target.abs_value_us - now.abs_value_us); 54 cumDelta += (target.abs_value_us - now.abs_value_us);
55 target = 55 target =
56 GNUNET_TIME_relative_to_absolute(GNUNET_TIME_relative_multiply 56 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
57 (GNUNET_TIME_UNIT_MICROSECONDS, i)); 57 (GNUNET_TIME_UNIT_MICROSECONDS, i));
58 fprintf(stderr, "%s", "."); 58 fprintf (stderr, "%s", ".");
59 if (i > MAXV) 59 if (i > MAXV)
60 { 60 {
61 fprintf(stderr, "%s", "\n"); 61 fprintf (stderr, "%s", "\n");
62 return; 62 return;
63 } 63 }
64 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply 64 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
65 (GNUNET_TIME_UNIT_MICROSECONDS, i), 65 (GNUNET_TIME_UNIT_MICROSECONDS, i),
66 &test_task, 66 &test_task,
67 NULL); 67 NULL);
68 i += INCR; 68 i += INCR;
69} 69}
70 70
71 71
72int 72int
73main(int argc, char *argv[]) 73main (int argc, char *argv[])
74{ 74{
75 GNUNET_log_setup("test-scheduler-delay", 75 GNUNET_log_setup ("test-scheduler-delay",
76 "WARNING", 76 "WARNING",
77 NULL); 77 NULL);
78 target = GNUNET_TIME_absolute_get(); 78 target = GNUNET_TIME_absolute_get ();
79 GNUNET_SCHEDULER_run(&test_task, NULL); 79 GNUNET_SCHEDULER_run (&test_task, NULL);
80 fprintf(stdout, 80 fprintf (stdout,
81 "Sleep precision: %llu microseconds (average delta). ", 81 "Sleep precision: %llu microseconds (average delta). ",
82 cumDelta / (MAXV / INCR)); 82 cumDelta / (MAXV / INCR));
83 if (cumDelta <= 500 * MAXV / INCR) 83 if (cumDelta <= 500 * MAXV / INCR)
84 fprintf(stdout, "%s", "Timer precision is excellent.\n"); 84 fprintf (stdout, "%s", "Timer precision is excellent.\n");
85 else if (cumDelta <= 5000 * MAXV / INCR) /* 5 ms average deviation */ 85 else if (cumDelta <= 5000 * MAXV / INCR) /* 5 ms average deviation */
86 fprintf(stdout, "%s", "Timer precision is good.\n"); 86 fprintf (stdout, "%s", "Timer precision is good.\n");
87 else if (cumDelta > 25000 * MAXV / INCR) 87 else if (cumDelta > 25000 * MAXV / INCR)
88 fprintf(stdout, "%s", "Timer precision is awful.\n"); 88 fprintf (stdout, "%s", "Timer precision is awful.\n");
89 else 89 else
90 fprintf(stdout, "%s", "Timer precision is acceptable.\n"); 90 fprintf (stdout, "%s", "Timer precision is acceptable.\n");
91 return 0; 91 return 0;
92} 92}
93 93
diff --git a/src/util/test_service.c b/src/util/test_service.c
index 4edc38a0d..6db8471bd 100644
--- a/src/util/test_service.c
+++ b/src/util/test_service.c
@@ -43,20 +43,20 @@ static struct GNUNET_SCHEDULER_Task *tt;
43 43
44 44
45static void 45static void
46handle_recv(void *cls, 46handle_recv (void *cls,
47 const struct GNUNET_MessageHeader *message) 47 const struct GNUNET_MessageHeader *message)
48{ 48{
49 struct GNUNET_SERVICE_Client *client = cls; 49 struct GNUNET_SERVICE_Client *client = cls;
50 50
51 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 51 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
52 "Received client message...\n"); 52 "Received client message...\n");
53 GNUNET_SERVICE_client_continue(client); 53 GNUNET_SERVICE_client_continue (client);
54 global_ret = 2; 54 global_ret = 2;
55 if (NULL != mq) 55 if (NULL != mq)
56 { 56 {
57 GNUNET_MQ_destroy(mq); 57 GNUNET_MQ_destroy (mq);
58 mq = NULL; 58 mq = NULL;
59 } 59 }
60} 60}
61 61
62 62
@@ -69,9 +69,9 @@ handle_recv(void *cls,
69 * @return @a c so we have the client handle in the future 69 * @return @a c so we have the client handle in the future
70 */ 70 */
71static void * 71static void *
72connect_cb(void *cls, 72connect_cb (void *cls,
73 struct GNUNET_SERVICE_Client *c, 73 struct GNUNET_SERVICE_Client *c,
74 struct GNUNET_MQ_Handle *mq) 74 struct GNUNET_MQ_Handle *mq)
75{ 75{
76 /* FIXME: in the future, do something with mq 76 /* FIXME: in the future, do something with mq
77 to test sending messages to the client! */ 77 to test sending messages to the client! */
@@ -87,35 +87,35 @@ connect_cb(void *cls,
87 * @param internal_cls must match @a c 87 * @param internal_cls must match @a c
88 */ 88 */
89static void 89static void
90disconnect_cb(void *cls, 90disconnect_cb (void *cls,
91 struct GNUNET_SERVICE_Client *c, 91 struct GNUNET_SERVICE_Client *c,
92 void *internal_cls) 92 void *internal_cls)
93{ 93{
94 GNUNET_assert(c == internal_cls); 94 GNUNET_assert (c == internal_cls);
95 if (2 == global_ret) 95 if (2 == global_ret)
96 {
97 GNUNET_SCHEDULER_shutdown ();
98 global_ret = 0;
99 if (NULL != tt)
96 { 100 {
97 GNUNET_SCHEDULER_shutdown(); 101 GNUNET_SCHEDULER_cancel (tt);
98 global_ret = 0; 102 tt = NULL;
99 if (NULL != tt)
100 {
101 GNUNET_SCHEDULER_cancel(tt);
102 tt = NULL;
103 }
104 } 103 }
104 }
105} 105}
106 106
107 107
108static void 108static void
109timeout_task(void *cls) 109timeout_task (void *cls)
110{ 110{
111 tt = NULL; 111 tt = NULL;
112 if (NULL != mq) 112 if (NULL != mq)
113 { 113 {
114 GNUNET_MQ_destroy(mq); 114 GNUNET_MQ_destroy (mq);
115 mq = NULL; 115 mq = NULL;
116 } 116 }
117 global_ret = 33; 117 global_ret = 33;
118 GNUNET_SCHEDULER_shutdown(); 118 GNUNET_SCHEDULER_shutdown ();
119} 119}
120 120
121 121
@@ -128,28 +128,28 @@ timeout_task(void *cls)
128 * @param sh handle to the service 128 * @param sh handle to the service
129 */ 129 */
130static void 130static void
131service_init(void *cls, 131service_init (void *cls,
132 const struct GNUNET_CONFIGURATION_Handle *cfg, 132 const struct GNUNET_CONFIGURATION_Handle *cfg,
133 struct GNUNET_SERVICE_Handle *sh) 133 struct GNUNET_SERVICE_Handle *sh)
134{ 134{
135 const char *service_name = cls; 135 const char *service_name = cls;
136 struct GNUNET_MQ_Envelope *env; 136 struct GNUNET_MQ_Envelope *env;
137 struct GNUNET_MessageHeader *msg; 137 struct GNUNET_MessageHeader *msg;
138 138
139 GNUNET_assert(NULL == tt); 139 GNUNET_assert (NULL == tt);
140 tt = GNUNET_SCHEDULER_add_delayed(TIMEOUT, 140 tt = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
141 &timeout_task, 141 &timeout_task,
142 NULL); 142 NULL);
143 mq = GNUNET_CLIENT_connect(cfg, 143 mq = GNUNET_CLIENT_connect (cfg,
144 service_name, 144 service_name,
145 NULL, 145 NULL,
146 NULL, 146 NULL,
147 NULL); 147 NULL);
148 GNUNET_assert(NULL != mq); 148 GNUNET_assert (NULL != mq);
149 env = GNUNET_MQ_msg(msg, 149 env = GNUNET_MQ_msg (msg,
150 MY_TYPE); 150 MY_TYPE);
151 GNUNET_MQ_send(mq, 151 GNUNET_MQ_send (mq,
152 env); 152 env);
153} 153}
154 154
155 155
@@ -160,77 +160,77 @@ service_init(void *cls,
160 * @param sname name of the service to run 160 * @param sname name of the service to run
161 */ 161 */
162static int 162static int
163check(const char *sname) 163check (const char *sname)
164{ 164{
165 struct GNUNET_MQ_MessageHandler myhandlers[] = { 165 struct GNUNET_MQ_MessageHandler myhandlers[] = {
166 GNUNET_MQ_hd_fixed_size(recv, 166 GNUNET_MQ_hd_fixed_size (recv,
167 MY_TYPE, 167 MY_TYPE,
168 struct GNUNET_MessageHeader, 168 struct GNUNET_MessageHeader,
169 NULL), 169 NULL),
170 GNUNET_MQ_handler_end() 170 GNUNET_MQ_handler_end ()
171 }; 171 };
172 char *const argv[] = { 172 char *const argv[] = {
173 (char *)sname, 173 (char *) sname,
174 "-c", 174 "-c",
175 "test_service_data.conf", 175 "test_service_data.conf",
176 NULL 176 NULL
177 }; 177 };
178 178
179 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Starting `%s' service\n", 180 "Starting `%s' service\n",
181 sname); 181 sname);
182 global_ret = 1; 182 global_ret = 1;
183 GNUNET_assert(0 == 183 GNUNET_assert (0 ==
184 GNUNET_SERVICE_run_(3, 184 GNUNET_SERVICE_run_ (3,
185 argv, 185 argv,
186 sname, 186 sname,
187 GNUNET_SERVICE_OPTION_NONE, 187 GNUNET_SERVICE_OPTION_NONE,
188 &service_init, 188 &service_init,
189 &connect_cb, 189 &connect_cb,
190 &disconnect_cb, 190 &disconnect_cb,
191 (void *)sname, 191 (void *) sname,
192 myhandlers)); 192 myhandlers));
193 return global_ret; 193 return global_ret;
194} 194}
195 195
196 196
197int 197int
198main(int argc, 198main (int argc,
199 char *argv[]) 199 char *argv[])
200{ 200{
201 int ret = 0; 201 int ret = 0;
202 struct GNUNET_NETWORK_Handle *s = NULL; 202 struct GNUNET_NETWORK_Handle *s = NULL;
203 203
204 GNUNET_log_setup("test-service", 204 GNUNET_log_setup ("test-service",
205 "WARNING", 205 "WARNING",
206 NULL); 206 NULL);
207 ret += check("test_service"); 207 ret += check ("test_service");
208 ret += check("test_service"); 208 ret += check ("test_service");
209 s = GNUNET_NETWORK_socket_create(PF_INET6, 209 s = GNUNET_NETWORK_socket_create (PF_INET6,
210 SOCK_STREAM, 210 SOCK_STREAM,
211 0); 211 0);
212 212
213 if (NULL == s) 213 if (NULL == s)
214 {
215 if ((errno == ENOBUFS) ||
216 (errno == ENOMEM) ||
217 (errno == ENFILE) ||
218 (errno == EACCES))
214 { 219 {
215 if ((errno == ENOBUFS) || 220 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
216 (errno == ENOMEM) || 221 "socket");
217 (errno == ENFILE) || 222 return 1;
218 (errno == EACCES))
219 {
220 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR,
221 "socket");
222 return 1;
223 }
224 fprintf(stderr,
225 "IPv6 support seems to not be available (%s), not testing it!\n",
226 strerror(errno));
227 } 223 }
224 fprintf (stderr,
225 "IPv6 support seems to not be available (%s), not testing it!\n",
226 strerror (errno));
227 }
228 else 228 else
229 { 229 {
230 GNUNET_break(GNUNET_OK == 230 GNUNET_break (GNUNET_OK ==
231 GNUNET_NETWORK_socket_close(s)); 231 GNUNET_NETWORK_socket_close (s));
232 ret += check("test_service6"); 232 ret += check ("test_service6");
233 } 233 }
234 return ret; 234 return ret;
235} 235}
236 236
diff --git a/src/util/test_socks.c b/src/util/test_socks.c
index dc5847d14..b813937c2 100644
--- a/src/util/test_socks.c
+++ b/src/util/test_socks.c
@@ -37,24 +37,25 @@ static struct GNUNET_CONFIGURATION_Handle *cfg;
37 37
38#define MY_TYPE 130 38#define MY_TYPE 130
39 39
40struct CopyContext { 40struct CopyContext
41{
41 struct GNUNET_SERVER_Client *client; 42 struct GNUNET_SERVER_Client *client;
42 struct GNUNET_MessageHeader *cpy; 43 struct GNUNET_MessageHeader *cpy;
43}; 44};
44 45
45static size_t 46static size_t
46copy_msg(void *cls, size_t size, void *buf) 47copy_msg (void *cls, size_t size, void *buf)
47{ 48{
48 struct CopyContext *ctx = cls; 49 struct CopyContext *ctx = cls;
49 struct GNUNET_MessageHeader *cpy = ctx->cpy; 50 struct GNUNET_MessageHeader *cpy = ctx->cpy;
50 51
51 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) == ntohs(cpy->size)); 52 GNUNET_assert (sizeof(struct GNUNET_MessageHeader) == ntohs (cpy->size));
52 GNUNET_assert(size >= ntohs(cpy->size)); 53 GNUNET_assert (size >= ntohs (cpy->size));
53 GNUNET_memcpy(buf, cpy, ntohs(cpy->size)); 54 GNUNET_memcpy (buf, cpy, ntohs (cpy->size));
54 GNUNET_SERVER_receive_done(ctx->client, GNUNET_OK); 55 GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
55 GNUNET_free(cpy); 56 GNUNET_free (cpy);
56 GNUNET_free(ctx); 57 GNUNET_free (ctx);
57 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n"); 58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
58 return sizeof(struct GNUNET_MessageHeader); 59 return sizeof(struct GNUNET_MessageHeader);
59} 60}
60 61
@@ -63,25 +64,25 @@ copy_msg(void *cls, size_t size, void *buf)
63 * Callback that just bounces the message back to the sender. 64 * Callback that just bounces the message back to the sender.
64 */ 65 */
65static void 66static void
66echo_cb(void *cls, struct GNUNET_SERVER_Client *client, 67echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
67 const struct GNUNET_MessageHeader *message) 68 const struct GNUNET_MessageHeader *message)
68{ 69{
69 struct CopyContext *cc; 70 struct CopyContext *cc;
70 struct GNUNET_MessageHeader *cpy; 71 struct GNUNET_MessageHeader *cpy;
71 72
72 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
73 "Receiving message from client, bouncing back\n"); 74 "Receiving message from client, bouncing back\n");
74 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) == ntohs(message->size)); 75 GNUNET_assert (sizeof(struct GNUNET_MessageHeader) == ntohs (message->size));
75 cc = GNUNET_new(struct CopyContext); 76 cc = GNUNET_new (struct CopyContext);
76 cc->client = client; 77 cc->client = client;
77 cpy = GNUNET_malloc(ntohs(message->size)); 78 cpy = GNUNET_malloc (ntohs (message->size));
78 GNUNET_memcpy(cpy, message, ntohs(message->size)); 79 GNUNET_memcpy (cpy, message, ntohs (message->size));
79 cc->cpy = cpy; 80 cc->cpy = cpy;
80 GNUNET_assert(NULL != 81 GNUNET_assert (NULL !=
81 GNUNET_SERVER_notify_transmit_ready(client, 82 GNUNET_SERVER_notify_transmit_ready (client,
82 ntohs(message->size), 83 ntohs (message->size),
83 GNUNET_TIME_UNIT_SECONDS, 84 GNUNET_TIME_UNIT_SECONDS,
84 &copy_msg, cc)); 85 &copy_msg, cc));
85} 86}
86 87
87 88
@@ -92,17 +93,17 @@ static struct GNUNET_SERVER_MessageHandler handlers[] = {
92 93
93 94
94static void 95static void
95handle_bounce(void *cls, 96handle_bounce (void *cls,
96 const struct GNUNET_MessageHeader *got) 97 const struct GNUNET_MessageHeader *got)
97{ 98{
98 int *ok = cls; 99 int *ok = cls;
99 100
100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 "Receiving bounce, checking content\n"); 102 "Receiving bounce, checking content\n");
102 GNUNET_assert(NULL != got); 103 GNUNET_assert (NULL != got);
103 GNUNET_MQ_destroy(mq); 104 GNUNET_MQ_destroy (mq);
104 mq = NULL; 105 mq = NULL;
105 GNUNET_SERVER_destroy(server); 106 GNUNET_SERVER_destroy (server);
106 server = NULL; 107 server = NULL;
107 *ok = 0; 108 *ok = 0;
108} 109}
@@ -117,15 +118,15 @@ handle_bounce(void *cls,
117 * @param error error code 118 * @param error error code
118 */ 119 */
119static void 120static void
120mq_error_handler(void *cls, 121mq_error_handler (void *cls,
121 enum GNUNET_MQ_Error error) 122 enum GNUNET_MQ_Error error)
122{ 123{
123 GNUNET_assert(0); /* should never happen */ 124 GNUNET_assert (0); /* should never happen */
124} 125}
125 126
126 127
127static void 128static void
128task(void *cls) 129task (void *cls)
129{ 130{
130 struct sockaddr_in sa; 131 struct sockaddr_in sa;
131 struct sockaddr *sap[2]; 132 struct sockaddr *sap[2];
@@ -133,121 +134,122 @@ task(void *cls)
133 struct GNUNET_MQ_Envelope *env; 134 struct GNUNET_MQ_Envelope *env;
134 struct GNUNET_MessageHeader *msg; 135 struct GNUNET_MessageHeader *msg;
135 struct GNUNET_MQ_MessageHandler chandlers[] = { 136 struct GNUNET_MQ_MessageHandler chandlers[] = {
136 GNUNET_MQ_hd_fixed_size(bounce, 137 GNUNET_MQ_hd_fixed_size (bounce,
137 MY_TYPE, 138 MY_TYPE,
138 struct GNUNET_MessageHeader, 139 struct GNUNET_MessageHeader,
139 cls), 140 cls),
140 GNUNET_MQ_handler_end() 141 GNUNET_MQ_handler_end ()
141 }; 142 };
142 143
143 /* test IPC between client and server */ 144 /* test IPC between client and server */
144 sap[0] = (struct sockaddr *)&sa; 145 sap[0] = (struct sockaddr *) &sa;
145 slens[0] = sizeof(sa); 146 slens[0] = sizeof(sa);
146 sap[1] = NULL; 147 sap[1] = NULL;
147 slens[1] = 0; 148 slens[1] = 0;
148 memset(&sa, 0, sizeof(sa)); 149 memset (&sa, 0, sizeof(sa));
149#if HAVE_SOCKADDR_IN_SIN_LEN 150#if HAVE_SOCKADDR_IN_SIN_LEN
150 sa.sin_len = sizeof(sa); 151 sa.sin_len = sizeof(sa);
151#endif 152#endif
152 sa.sin_family = AF_INET; 153 sa.sin_family = AF_INET;
153 sa.sin_port = htons(PORT); 154 sa.sin_port = htons (PORT);
154 server = 155 server =
155 GNUNET_SERVER_create(NULL, NULL, sap, slens, 156 GNUNET_SERVER_create (NULL, NULL, sap, slens,
156 GNUNET_TIME_relative_multiply 157 GNUNET_TIME_relative_multiply
157 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO); 158 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
158 GNUNET_assert(server != NULL); 159 GNUNET_assert (server != NULL);
159 handlers[0].callback_cls = cls; 160 handlers[0].callback_cls = cls;
160 handlers[1].callback_cls = cls; 161 handlers[1].callback_cls = cls;
161 GNUNET_SERVER_add_handlers(server, handlers); 162 GNUNET_SERVER_add_handlers (server, handlers);
162 mq = GNUNET_CLIENT_connect(cfg, 163 mq = GNUNET_CLIENT_connect (cfg,
163 MYNAME, 164 MYNAME,
164 chandlers, 165 chandlers,
165 &mq_error_handler, 166 &mq_error_handler,
166 NULL); 167 NULL);
167 GNUNET_assert(NULL != mq); 168 GNUNET_assert (NULL != mq);
168 env = GNUNET_MQ_msg(msg, 169 env = GNUNET_MQ_msg (msg,
169 MY_TYPE); 170 MY_TYPE);
170 GNUNET_MQ_send(mq, 171 GNUNET_MQ_send (mq,
171 env); 172 env);
172} 173}
173 174
174 175
175int 176int
176main(int argc, char *argv[]) 177main (int argc, char *argv[])
177{ 178{
178 int ok; 179 int ok;
179 int status; 180 int status;
180 const char *socksport = "1081"; 181 const char *socksport = "1081";
181 182
182 GNUNET_log_setup("test_client", 183 GNUNET_log_setup ("test_client",
183 "WARNING", 184 "WARNING",
184 NULL); 185 NULL);
185 186
186 pid_t pid = fork(); 187 pid_t pid = fork ();
187 GNUNET_assert(pid >= 0); 188 GNUNET_assert (pid >= 0);
188 if (pid == 0) 189 if (pid == 0)
189 { 190 {
190 execlp("ssh", 191 execlp ("ssh",
191 "ssh", "-D", socksport, 192 "ssh", "-D", socksport,
192 "-o", "BatchMode yes", 193 "-o", "BatchMode yes",
193 "-o", "UserKnownHostsFile /tmp/gnunet_test_socks_ssh_garbage", 194 "-o", "UserKnownHostsFile /tmp/gnunet_test_socks_ssh_garbage",
194 "-o", "StrictHostKeyChecking no", 195 "-o", "StrictHostKeyChecking no",
195 "127.0.0.1", "-N", (char*)NULL); 196 "127.0.0.1", "-N", (char*) NULL);
196 perror("execlp (\"ssh\",\"ssh\",...,\"-D\",\"1081\",\"127.0.0.1\",\"-N\") "); 197 perror (
197 printf("" 198 "execlp (\"ssh\",\"ssh\",...,\"-D\",\"1081\",\"127.0.0.1\",\"-N\") ");
198 "Please ensure you have ssh installed and have sshd installed and running :\n" 199 printf (""
199 "\tsudo apt-get install openssh-client openssh-server\n" 200 "Please ensure you have ssh installed and have sshd installed and running :\n"
200 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n" 201 "\tsudo apt-get install openssh-client openssh-server\n"
201 "to localhost. Please either run make check from an unproxied user, or else\n" 202 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
202 "add these lines to the beginning of your ~/.ssh/config file :" 203 "to localhost. Please either run make check from an unproxied user, or else\n"
203 "\tHost 127.0.0.1 localhost\n" 204 "add these lines to the beginning of your ~/.ssh/config file :"
204 "\t CheckHostIP no\n" 205 "\tHost 127.0.0.1 localhost\n"
205 "\t Protocol 2\n" 206 "\t CheckHostIP no\n"
206 "\t ProxyCommand nc 127.0.0.1 22\n"); 207 "\t Protocol 2\n"
207 kill(getppid(), SIGALRM); 208 "\t ProxyCommand nc 127.0.0.1 22\n");
208 return 1; 209 kill (getppid (), SIGALRM);
209 } 210 return 1;
210 if (0 != sleep(1)) 211 }
211 { 212 if (0 != sleep (1))
212 /* sleep interrupted, likely SIGALRM, failure to 213 {
213 launch child, terminate */ 214 /* sleep interrupted, likely SIGALRM, failure to
214 printf("" 215 launch child, terminate */
215 "Please ensure you have ssh installed and have sshd installed and running :\n" 216 printf (""
216 "\tsudo apt-get install openssh-client openssh-server\n" 217 "Please ensure you have ssh installed and have sshd installed and running :\n"
217 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n" 218 "\tsudo apt-get install openssh-client openssh-server\n"
218 "to localhost. Please either run make check from an unproxied user, or else\n" 219 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
219 "add these lines to the beginning of your ~/.ssh/config file :" 220 "to localhost. Please either run make check from an unproxied user, or else\n"
220 "\tHost 127.0.0.1 localhost\n" 221 "add these lines to the beginning of your ~/.ssh/config file :"
221 "\t CheckHostIP no\n" 222 "\tHost 127.0.0.1 localhost\n"
222 "\t Protocol 2\n" 223 "\t CheckHostIP no\n"
223 "\t ProxyCommand nc 127.0.0.1 22\n"); 224 "\t Protocol 2\n"
224 return 77; 225 "\t ProxyCommand nc 127.0.0.1 22\n");
225 } 226 return 77;
227 }
226 /* check if child exec()ed but died */ 228 /* check if child exec()ed but died */
227 if (0 != waitpid(pid, &status, WNOHANG)) 229 if (0 != waitpid (pid, &status, WNOHANG))
228 { 230 {
229 printf("" 231 printf (""
230 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n" 232 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
231 "to localhost. Please either run make check from an unproxied user, or else\n" 233 "to localhost. Please either run make check from an unproxied user, or else\n"
232 "add these lines to the beginning of your ~/.ssh/config file :" 234 "add these lines to the beginning of your ~/.ssh/config file :"
233 "\tHost 127.0.0.1 localhost\n" 235 "\tHost 127.0.0.1 localhost\n"
234 "\t CheckHostIP no\n" 236 "\t CheckHostIP no\n"
235 "\t Protocol 2\n" 237 "\t Protocol 2\n"
236 "\t ProxyCommand nc 127.0.0.1 22\n"); 238 "\t ProxyCommand nc 127.0.0.1 22\n");
237 return 77; 239 return 77;
238 } 240 }
239 241
240 cfg = GNUNET_CONFIGURATION_create(); 242 cfg = GNUNET_CONFIGURATION_create ();
241 GNUNET_CONFIGURATION_set_value_string(cfg, MYNAME, "SOCKSHOST", "127.0.0.1"); 243 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSHOST", "127.0.0.1");
242 GNUNET_CONFIGURATION_set_value_string(cfg, MYNAME, "SOCKSPORT", socksport); 244 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSPORT", socksport);
243 GNUNET_CONFIGURATION_set_value_number(cfg, MYNAME, "PORT", PORT); 245 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
244 GNUNET_CONFIGURATION_set_value_string(cfg, MYNAME, "HOSTNAME", "127.0.0.1"); 246 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "127.0.0.1");
245 ok = 1; 247 ok = 1;
246 GNUNET_SCHEDULER_run(&task, &ok); 248 GNUNET_SCHEDULER_run (&task, &ok);
247 GNUNET_CONFIGURATION_destroy(cfg); 249 GNUNET_CONFIGURATION_destroy (cfg);
248 250
249 GNUNET_break(0 == kill(pid, SIGTERM)); 251 GNUNET_break (0 == kill (pid, SIGTERM));
250 GNUNET_break(pid == waitpid(pid, &status, 0)); 252 GNUNET_break (pid == waitpid (pid, &status, 0));
251 return ok; 253 return ok;
252} 254}
253 255
diff --git a/src/util/test_speedup.c b/src/util/test_speedup.c
index eacc879df..dfb8db915 100644
--- a/src/util/test_speedup.c
+++ b/src/util/test_speedup.c
@@ -47,20 +47,20 @@ static unsigned int cycles;
47 * @param tc scheduler context, unused 47 * @param tc scheduler context, unused
48 */ 48 */
49static void 49static void
50run(void *cls) 50run (void *cls)
51{ 51{
52 cycles++; 52 cycles++;
53 fprintf(stderr, "..%u", cycles); 53 fprintf (stderr, "..%u", cycles);
54 if (cycles <= 5) 54 if (cycles <= 5)
55 { 55 {
56 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, 56 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
57 &run, 57 &run,
58 NULL); 58 NULL);
59 return; 59 return;
60 } 60 }
61 end = GNUNET_TIME_absolute_get(); 61 end = GNUNET_TIME_absolute_get ();
62 fprintf(stderr, "\n"); 62 fprintf (stderr, "\n");
63 fflush(stdout); 63 fflush (stdout);
64} 64}
65 65
66 66
@@ -68,19 +68,19 @@ run(void *cls)
68 * 68 *
69 */ 69 */
70static void 70static void
71check(void *cls, 71check (void *cls,
72 char *const *args, 72 char *const *args,
73 const char *cfgfile, 73 const char *cfgfile,
74 const struct GNUNET_CONFIGURATION_Handle *cfg) 74 const struct GNUNET_CONFIGURATION_Handle *cfg)
75{ 75{
76 fprintf(stderr, "0"); 76 fprintf (stderr, "0");
77 fflush(stdout); 77 fflush (stdout);
78 GNUNET_SCHEDULER_add_now(&run, NULL); 78 GNUNET_SCHEDULER_add_now (&run, NULL);
79} 79}
80 80
81 81
82int 82int
83main(int argc, char *argv[]) 83main (int argc, char *argv[])
84{ 84{
85 static char *const argvn[] = { 85 static char *const argvn[] = {
86 "test-speedup", 86 "test-speedup",
@@ -94,30 +94,31 @@ main(int argc, char *argv[])
94 time_t end_real; 94 time_t end_real;
95 struct GNUNET_TIME_Relative delta; 95 struct GNUNET_TIME_Relative delta;
96 96
97 start_real = time(NULL); 97 start_real = time (NULL);
98 start = GNUNET_TIME_absolute_get(); 98 start = GNUNET_TIME_absolute_get ();
99 GNUNET_PROGRAM_run((sizeof(argvn) / sizeof(char *)) - 1, argvn, "test-speedup", 99 GNUNET_PROGRAM_run ((sizeof(argvn) / sizeof(char *)) - 1, argvn,
100 "nohelp", options, &check, NULL); 100 "test-speedup",
101 "nohelp", options, &check, NULL);
101 102
102 end_real = time(NULL); 103 end_real = time (NULL);
103 delta = GNUNET_TIME_absolute_get_difference(start, end); 104 delta = GNUNET_TIME_absolute_get_difference (start, end);
104 105
105 if (delta.rel_value_us > ((end_real - start_real) * 1500LL * 1000LL)) 106 if (delta.rel_value_us > ((end_real - start_real) * 1500LL * 1000LL))
106 { 107 {
107 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "Execution time in GNUnet time: %s\n", 109 "Execution time in GNUnet time: %s\n",
109 GNUNET_STRINGS_relative_time_to_string(delta, GNUNET_YES)); 110 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Execution time in system time: %llu ms\n", 112 "Execution time in system time: %llu ms\n",
112 (unsigned long long)((end_real - start_real) * 1000LL)); 113 (unsigned long long) ((end_real - start_real) * 1000LL));
113 return 0; 114 return 0;
114 } 115 }
115 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 "Execution time in GNUnet time: %s\n", 117 "Execution time in GNUnet time: %s\n",
117 GNUNET_STRINGS_relative_time_to_string(delta, GNUNET_YES)); 118 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
118 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 "Execution time in system time: %llu ms\n", 120 "Execution time in system time: %llu ms\n",
120 (unsigned long long)((end_real - start_real) * 1000LL)); 121 (unsigned long long) ((end_real - start_real) * 1000LL));
121 return 1; 122 return 1;
122} 123}
123 124
diff --git a/src/util/test_strings.c b/src/util/test_strings.c
index 60887c587..90d06a473 100644
--- a/src/util/test_strings.c
+++ b/src/util/test_strings.c
@@ -25,12 +25,22 @@
25#include "gnunet_util_lib.h" 25#include "gnunet_util_lib.h"
26 26
27 27
28#define WANT(a, b) if (0 != strcmp(a, b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_free(b); GNUNET_break(0); return 1; } else { GNUNET_free(b); } 28#define WANT(a, b) if (0 != strcmp (a, b)) { fprintf (stderr, \
29#define WANTNF(a, b) do { if (0 != strcmp(a, b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_break(0); return 1; } } while (0) 29 "Got `%s', wanted `%s'\n", \
30#define WANTB(a, b, l) if (0 != memcmp(a, b, l)) { GNUNET_break(0); return 1; } else { } 30 b, a); GNUNET_free (b); \
31 GNUNET_break (0); \
32 return 1; } else { GNUNET_free (b); \
33}
34#define WANTNF(a, b) do { if (0 != strcmp (a, b)) { fprintf (stderr, \
35 "Got `%s', wanted `%s'\n", \
36 b, a); \
37 GNUNET_break (0); return 1; \
38 } } while (0)
39#define WANTB(a, b, l) if (0 != memcmp (a, b, l)) { GNUNET_break (0); return 1; \
40} else { }
31 41
32int 42int
33main(int argc, char *argv[]) 43main (int argc, char *argv[])
34{ 44{
35 char buf[128]; 45 char buf[128];
36 char *r; 46 char *r;
@@ -42,89 +52,90 @@ main(int argc, char *argv[])
42 struct GNUNET_TIME_Relative rtx; 52 struct GNUNET_TIME_Relative rtx;
43 const char *hdir; 53 const char *hdir;
44 54
45 GNUNET_log_setup("test_strings", "ERROR", NULL); 55 GNUNET_log_setup ("test_strings", "ERROR", NULL);
46 sprintf(buf, "4 %s", _(/* size unit */ "b")); 56 sprintf (buf, "4 %s", _ (/* size unit */ "b"));
47 b = GNUNET_STRINGS_byte_size_fancy(4); 57 b = GNUNET_STRINGS_byte_size_fancy (4);
48 WANT(buf, b); 58 WANT (buf, b);
49 sprintf(buf, "10 %s", _(/* size unit */ "KiB")); 59 sprintf (buf, "10 %s", _ (/* size unit */ "KiB"));
50 b = GNUNET_STRINGS_byte_size_fancy(10240); 60 b = GNUNET_STRINGS_byte_size_fancy (10240);
51 WANT(buf, b); 61 WANT (buf, b);
52 sprintf(buf, "10 %s", _(/* size unit */ "TiB")); 62 sprintf (buf, "10 %s", _ (/* size unit */ "TiB"));
53 b = GNUNET_STRINGS_byte_size_fancy(10240LL * 1024LL * 1024LL * 1024LL); 63 b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
54 WANT(buf, b); 64 WANT (buf, b);
55 sprintf(buf, "4 %s", _(/* time unit */ "ms")); 65 sprintf (buf, "4 %s", _ (/* time unit */ "ms"));
56 bc = GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_relative_multiply 66 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
57 (GNUNET_TIME_UNIT_MILLISECONDS, 67 (GNUNET_TIME_UNIT_MILLISECONDS,
58 4), GNUNET_YES); 68 4), GNUNET_YES);
59 WANTNF(buf, bc); 69 WANTNF (buf, bc);
60 sprintf(buf, "7 %s", _(/* time unit */ "s")); 70 sprintf (buf, "7 %s", _ (/* time unit */ "s"));
61 bc = GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_relative_multiply 71 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
62 (GNUNET_TIME_UNIT_MILLISECONDS, 72 (GNUNET_TIME_UNIT_MILLISECONDS,
63 7 * 1000), GNUNET_YES); 73 7 * 1000), GNUNET_YES);
64 WANTNF(buf, bc); 74 WANTNF (buf, bc);
65 sprintf(buf, "7 %s", _(/* time unit */ "h")); 75 sprintf (buf, "7 %s", _ (/* time unit */ "h"));
66 bc = GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_relative_multiply 76 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
67 (GNUNET_TIME_UNIT_MILLISECONDS, 77 (GNUNET_TIME_UNIT_MILLISECONDS,
68 7 * 60 * 60 * 1000), GNUNET_YES); 78 7 * 60 * 60 * 1000),
69 WANTNF(buf, bc); 79 GNUNET_YES);
70 80 WANTNF (buf, bc);
71 hdir = getenv("HOME"); 81
72 82 hdir = getenv ("HOME");
73 GNUNET_snprintf(buf, sizeof(buf), "%s%s", hdir, DIR_SEPARATOR_STR); 83
74 b = GNUNET_STRINGS_filename_expand("~"); 84 GNUNET_snprintf (buf, sizeof(buf), "%s%s", hdir, DIR_SEPARATOR_STR);
75 GNUNET_assert(b != NULL); 85 b = GNUNET_STRINGS_filename_expand ("~");
76 WANT(buf, b); 86 GNUNET_assert (b != NULL);
77 GNUNET_STRINGS_buffer_fill(buf, sizeof(buf), 3, "a", "btx", "c"); 87 WANT (buf, b);
78 WANTB("a\0btx\0c", buf, 8); 88 GNUNET_STRINGS_buffer_fill (buf, sizeof(buf), 3, "a", "btx", "c");
79 if (6 != GNUNET_STRINGS_buffer_tokenize(buf, sizeof(buf), 2, &r, &b)) 89 WANTB ("a\0btx\0c", buf, 8);
90 if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof(buf), 2, &r, &b))
80 return 1; 91 return 1;
81 r = GNUNET_strdup(r); 92 r = GNUNET_strdup (r);
82 WANT("a", r); 93 WANT ("a", r);
83 b = GNUNET_strdup(b); 94 b = GNUNET_strdup (b);
84 WANT("btx", b); 95 WANT ("btx", b);
85 if (0 != GNUNET_STRINGS_buffer_tokenize(buf, 2, 2, &r, &b)) 96 if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
86 return 1; 97 return 1;
87 at.abs_value_us = 5000000; 98 at.abs_value_us = 5000000;
88 bc = GNUNET_STRINGS_absolute_time_to_string(at); 99 bc = GNUNET_STRINGS_absolute_time_to_string (at);
89 /* bc should be something like "Wed Dec 31 17:00:05 1969" 100 /* bc should be something like "Wed Dec 31 17:00:05 1969"
90 * where the details of the day and hour depend on the timezone; 101 * where the details of the day and hour depend on the timezone;
91 * however, the "0:05 19" should always be there; hence: */ 102 * however, the "0:05 19" should always be there; hence: */
92 if (NULL == strstr(bc, "0:05 19")) 103 if (NULL == strstr (bc, "0:05 19"))
93 { 104 {
94 fprintf(stderr, "Got %s\n", bc); 105 fprintf (stderr, "Got %s\n", bc);
95 GNUNET_break(0); 106 GNUNET_break (0);
96 return 1; 107 return 1;
97 } 108 }
98 b = GNUNET_STRINGS_to_utf8("TEST", 4, "ASCII"); 109 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
99 WANT("TEST", b); 110 WANT ("TEST", b);
100 111
101 at = GNUNET_TIME_UNIT_FOREVER_ABS; 112 at = GNUNET_TIME_UNIT_FOREVER_ABS;
102 bc = GNUNET_STRINGS_absolute_time_to_string(at); 113 bc = GNUNET_STRINGS_absolute_time_to_string (at);
103 GNUNET_assert(GNUNET_OK == 114 GNUNET_assert (GNUNET_OK ==
104 GNUNET_STRINGS_fancy_time_to_absolute(bc, &atx)); 115 GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
105 GNUNET_assert(atx.abs_value_us == at.abs_value_us); 116 GNUNET_assert (atx.abs_value_us == at.abs_value_us);
106 117
107 at.abs_value_us = 50000000000; 118 at.abs_value_us = 50000000000;
108 bc = GNUNET_STRINGS_absolute_time_to_string(at); 119 bc = GNUNET_STRINGS_absolute_time_to_string (at);
109 120
110 GNUNET_assert(GNUNET_OK == 121 GNUNET_assert (GNUNET_OK ==
111 GNUNET_STRINGS_fancy_time_to_absolute(bc, &atx)); 122 GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
112 123
113 if (atx.abs_value_us != at.abs_value_us) 124 if (atx.abs_value_us != at.abs_value_us)
114 { 125 {
115 GNUNET_assert(0); 126 GNUNET_assert (0);
116 } 127 }
117 128
118 GNUNET_log_skip(2, GNUNET_NO); 129 GNUNET_log_skip (2, GNUNET_NO);
119 b = GNUNET_STRINGS_to_utf8("TEST", 4, "unknown"); 130 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
120 GNUNET_log_skip(0, GNUNET_YES); 131 GNUNET_log_skip (0, GNUNET_YES);
121 WANT("TEST", b); 132 WANT ("TEST", b);
122 133
123 GNUNET_assert(GNUNET_OK == 134 GNUNET_assert (GNUNET_OK ==
124 GNUNET_STRINGS_fancy_time_to_relative("15m", &rt)); 135 GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
125 GNUNET_assert(GNUNET_OK == 136 GNUNET_assert (GNUNET_OK ==
126 GNUNET_STRINGS_fancy_time_to_relative("15 m", &rtx)); 137 GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
127 GNUNET_assert(rt.rel_value_us == rtx.rel_value_us); 138 GNUNET_assert (rt.rel_value_us == rtx.rel_value_us);
128 139
129 return 0; 140 return 0;
130} 141}
diff --git a/src/util/test_strings_to_data.c b/src/util/test_strings_to_data.c
index d719e950a..71861b128 100644
--- a/src/util/test_strings_to_data.c
+++ b/src/util/test_strings_to_data.c
@@ -26,7 +26,7 @@
26 26
27 27
28int 28int
29main(int argc, char *argv[]) 29main (int argc, char *argv[])
30{ 30{
31 char buf[1024]; 31 char buf[1024];
32 char *end; 32 char *end;
@@ -35,27 +35,29 @@ main(int argc, char *argv[])
35 unsigned int i; 35 unsigned int i;
36 int ret = 0; 36 int ret = 0;
37 37
38 GNUNET_log_setup("util", "DEBUG", NULL); 38 GNUNET_log_setup ("util", "DEBUG", NULL);
39 for (i = 0; i < sizeof(src); i++) 39 for (i = 0; i < sizeof(src); i++)
40 {
41 memset (src, i, sizeof(src));
42 memset (dst, i + 1, sizeof(dst));
43
44 end = GNUNET_STRINGS_data_to_string (&src, i, buf, sizeof(buf));
45 GNUNET_assert (NULL != end);
46 end[0] = '\0';
47 if (GNUNET_OK !=
48 GNUNET_STRINGS_string_to_data (buf, strlen (buf), dst, i))
49 {
50 fprintf (stderr, "%u failed decode (%u bytes)\n", i, (unsigned
51 int) strlen (buf));
52 ret = 1;
53 }
54 else if (0 != memcmp (src, dst, i))
40 { 55 {
41 memset(src, i, sizeof(src)); 56 fprintf (stderr, "%u wrong decode (%u bytes)\n", i, (unsigned
42 memset(dst, i + 1, sizeof(dst)); 57 int) strlen (buf));
43 58 ret = 1;
44 end = GNUNET_STRINGS_data_to_string(&src, i, buf, sizeof(buf));
45 GNUNET_assert(NULL != end);
46 end[0] = '\0';
47 if (GNUNET_OK !=
48 GNUNET_STRINGS_string_to_data(buf, strlen(buf), dst, i))
49 {
50 fprintf(stderr, "%u failed decode (%u bytes)\n", i, (unsigned int)strlen(buf));
51 ret = 1;
52 }
53 else if (0 != memcmp(src, dst, i))
54 {
55 fprintf(stderr, "%u wrong decode (%u bytes)\n", i, (unsigned int)strlen(buf));
56 ret = 1;
57 }
58 } 59 }
60 }
59 return ret; 61 return ret;
60} 62}
61 63
diff --git a/src/util/test_time.c b/src/util/test_time.c
index b46cac34d..cffa41de9 100644
--- a/src/util/test_time.c
+++ b/src/util/test_time.c
@@ -26,7 +26,7 @@
26 26
27 27
28int 28int
29main(int argc, char *argv[]) 29main (int argc, char *argv[])
30{ 30{
31 struct GNUNET_TIME_Absolute now; 31 struct GNUNET_TIME_Absolute now;
32 struct GNUNET_TIME_AbsoluteNBO nown; 32 struct GNUNET_TIME_AbsoluteNBO nown;
@@ -41,211 +41,221 @@ main(int argc, char *argv[])
41 struct GNUNET_TIME_RelativeNBO reln; 41 struct GNUNET_TIME_RelativeNBO reln;
42 unsigned int i; 42 unsigned int i;
43 43
44 GNUNET_log_setup("test-time", "WARNING", NULL); 44 GNUNET_log_setup ("test-time", "WARNING", NULL);
45 forever = GNUNET_TIME_UNIT_FOREVER_ABS; 45 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
46 relForever = GNUNET_TIME_UNIT_FOREVER_REL; 46 relForever = GNUNET_TIME_UNIT_FOREVER_REL;
47 relUnit = GNUNET_TIME_UNIT_MILLISECONDS; 47 relUnit = GNUNET_TIME_UNIT_MILLISECONDS;
48 zero.abs_value_us = 0; 48 zero.abs_value_us = 0;
49 49
50 last = now = GNUNET_TIME_absolute_get(); 50 last = now = GNUNET_TIME_absolute_get ();
51 while (now.abs_value_us == last.abs_value_us) 51 while (now.abs_value_us == last.abs_value_us)
52 now = GNUNET_TIME_absolute_get(); 52 now = GNUNET_TIME_absolute_get ();
53 GNUNET_assert(now.abs_value_us > last.abs_value_us); 53 GNUNET_assert (now.abs_value_us > last.abs_value_us);
54 54
55 /* test overflow checking in multiply */ 55 /* test overflow checking in multiply */
56 rel = GNUNET_TIME_UNIT_MILLISECONDS; 56 rel = GNUNET_TIME_UNIT_MILLISECONDS;
57 GNUNET_log_skip(1, GNUNET_NO); 57 GNUNET_log_skip (1, GNUNET_NO);
58 for (i = 0; i < 55; i++) 58 for (i = 0; i < 55; i++)
59 rel = GNUNET_TIME_relative_multiply(rel, 2); 59 rel = GNUNET_TIME_relative_multiply (rel, 2);
60 GNUNET_log_skip(0, GNUNET_NO); 60 GNUNET_log_skip (0, GNUNET_NO);
61 GNUNET_assert(rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); 61 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
62 /*check zero */ 62 /*check zero */
63 rel.rel_value_us = (UINT64_MAX)-1024; 63 rel.rel_value_us = (UINT64_MAX) -1024;
64 GNUNET_assert(GNUNET_TIME_UNIT_ZERO.rel_value_us == 64 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
65 GNUNET_TIME_relative_multiply(rel, 0).rel_value_us); 65 GNUNET_TIME_relative_multiply (rel, 0).rel_value_us);
66 66
67 /* test infinity-check for relative to absolute */ 67 /* test infinity-check for relative to absolute */
68 GNUNET_log_skip(1, GNUNET_NO); 68 GNUNET_log_skip (1, GNUNET_NO);
69 last = GNUNET_TIME_relative_to_absolute(rel); 69 last = GNUNET_TIME_relative_to_absolute (rel);
70 GNUNET_assert(last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); 70 GNUNET_assert (last.abs_value_us ==
71 GNUNET_log_skip(0, GNUNET_YES); 71 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
72 GNUNET_log_skip (0, GNUNET_YES);
72 73
73 /* check relative to absolute */ 74 /* check relative to absolute */
74 rel.rel_value_us = 1000000; 75 rel.rel_value_us = 1000000;
75 GNUNET_assert(GNUNET_TIME_absolute_get().abs_value_us < 76 GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us <
76 GNUNET_TIME_relative_to_absolute(rel).abs_value_us); 77 GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
77 /*check forever */ 78 /*check forever */
78 rel.rel_value_us = UINT64_MAX; 79 rel.rel_value_us = UINT64_MAX;
79 GNUNET_assert(GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == 80 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us ==
80 GNUNET_TIME_relative_to_absolute(rel).abs_value_us); 81 GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
81 /* check overflow for r2a */ 82 /* check overflow for r2a */
82 rel.rel_value_us = (UINT64_MAX)-1024; 83 rel.rel_value_us = (UINT64_MAX) -1024;
83 GNUNET_log_skip(1, GNUNET_NO); 84 GNUNET_log_skip (1, GNUNET_NO);
84 last = GNUNET_TIME_relative_to_absolute(rel); 85 last = GNUNET_TIME_relative_to_absolute (rel);
85 GNUNET_log_skip(0, GNUNET_NO); 86 GNUNET_log_skip (0, GNUNET_NO);
86 GNUNET_assert(last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); 87 GNUNET_assert (last.abs_value_us ==
88 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
87 89
88 /* check overflow for relative add */ 90 /* check overflow for relative add */
89 GNUNET_log_skip(1, GNUNET_NO); 91 GNUNET_log_skip (1, GNUNET_NO);
90 rel = GNUNET_TIME_relative_add(rel, rel); 92 rel = GNUNET_TIME_relative_add (rel, rel);
91 GNUNET_log_skip(0, GNUNET_NO); 93 GNUNET_log_skip (0, GNUNET_NO);
92 GNUNET_assert(rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); 94 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
93 95
94 GNUNET_log_skip(1, GNUNET_NO); 96 GNUNET_log_skip (1, GNUNET_NO);
95 rel = GNUNET_TIME_relative_add(relForever, relForever); 97 rel = GNUNET_TIME_relative_add (relForever, relForever);
96 GNUNET_log_skip(0, GNUNET_NO); 98 GNUNET_log_skip (0, GNUNET_NO);
97 GNUNET_assert(rel.rel_value_us == relForever.rel_value_us); 99 GNUNET_assert (rel.rel_value_us == relForever.rel_value_us);
98 100
99 GNUNET_log_skip(1, GNUNET_NO); 101 GNUNET_log_skip (1, GNUNET_NO);
100 rel = GNUNET_TIME_relative_add(relUnit, relUnit); 102 rel = GNUNET_TIME_relative_add (relUnit, relUnit);
101 GNUNET_assert(rel.rel_value_us == 2 * relUnit.rel_value_us); 103 GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us);
102 104
103 /* check relation check in get_duration */ 105 /* check relation check in get_duration */
104 future.abs_value_us = now.abs_value_us + 1000000; 106 future.abs_value_us = now.abs_value_us + 1000000;
105 GNUNET_assert(GNUNET_TIME_absolute_get_difference(now, future).rel_value_us == 107 GNUNET_assert (GNUNET_TIME_absolute_get_difference (now,
106 1000000); 108 future).rel_value_us ==
107 GNUNET_assert(GNUNET_TIME_absolute_get_difference(future, now).rel_value_us == 109 1000000);
108 0); 110 GNUNET_assert (GNUNET_TIME_absolute_get_difference (future,
111 now).rel_value_us ==
112 0);
109 113
110 GNUNET_assert(GNUNET_TIME_absolute_get_difference(zero, forever).rel_value_us 114 GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero,
111 == forever.abs_value_us); 115 forever).rel_value_us
116 == forever.abs_value_us);
112 117
113 past.abs_value_us = now.abs_value_us - 1000000; 118 past.abs_value_us = now.abs_value_us - 1000000;
114 rel = GNUNET_TIME_absolute_get_duration(future); 119 rel = GNUNET_TIME_absolute_get_duration (future);
115 GNUNET_assert(rel.rel_value_us == 0); 120 GNUNET_assert (rel.rel_value_us == 0);
116 rel = GNUNET_TIME_absolute_get_duration(past); 121 rel = GNUNET_TIME_absolute_get_duration (past);
117 GNUNET_assert(rel.rel_value_us >= 1000000); 122 GNUNET_assert (rel.rel_value_us >= 1000000);
118 123
119 /* check get remaining */ 124 /* check get remaining */
120 rel = GNUNET_TIME_absolute_get_remaining(now); 125 rel = GNUNET_TIME_absolute_get_remaining (now);
121 GNUNET_assert(rel.rel_value_us == 0); 126 GNUNET_assert (rel.rel_value_us == 0);
122 rel = GNUNET_TIME_absolute_get_remaining(past); 127 rel = GNUNET_TIME_absolute_get_remaining (past);
123 GNUNET_assert(rel.rel_value_us == 0); 128 GNUNET_assert (rel.rel_value_us == 0);
124 rel = GNUNET_TIME_absolute_get_remaining(future); 129 rel = GNUNET_TIME_absolute_get_remaining (future);
125 GNUNET_assert(rel.rel_value_us > 0); 130 GNUNET_assert (rel.rel_value_us > 0);
126 GNUNET_assert(rel.rel_value_us <= 1000000); 131 GNUNET_assert (rel.rel_value_us <= 1000000);
127 forever = GNUNET_TIME_UNIT_FOREVER_ABS; 132 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
128 GNUNET_assert(GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == 133 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
129 GNUNET_TIME_absolute_get_remaining(forever).rel_value_us); 134 GNUNET_TIME_absolute_get_remaining (forever).rel_value_us);
130 135
131 /* check endianess */ 136 /* check endianess */
132 reln = GNUNET_TIME_relative_hton(rel); 137 reln = GNUNET_TIME_relative_hton (rel);
133 GNUNET_assert(rel.rel_value_us == GNUNET_TIME_relative_ntoh(reln).rel_value_us); 138 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (
134 nown = GNUNET_TIME_absolute_hton(now); 139 reln).rel_value_us);
135 GNUNET_assert(now.abs_value_us == GNUNET_TIME_absolute_ntoh(nown).abs_value_us); 140 nown = GNUNET_TIME_absolute_hton (now);
141 GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (
142 nown).abs_value_us);
136 143
137 /* check absolute addition */ 144 /* check absolute addition */
138 future = GNUNET_TIME_absolute_add(now, GNUNET_TIME_UNIT_SECONDS); 145 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
139 GNUNET_assert(future.abs_value_us == now.abs_value_us + 1000 * 1000LL); 146 GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL);
140 147
141 future = GNUNET_TIME_absolute_add(forever, GNUNET_TIME_UNIT_ZERO); 148 future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
142 GNUNET_assert(future.abs_value_us == forever.abs_value_us); 149 GNUNET_assert (future.abs_value_us == forever.abs_value_us);
143 150
144 rel.rel_value_us = (UINT64_MAX)-1024; 151 rel.rel_value_us = (UINT64_MAX) -1024;
145 now.abs_value_us = rel.rel_value_us; 152 now.abs_value_us = rel.rel_value_us;
146 future = GNUNET_TIME_absolute_add(now, rel); 153 future = GNUNET_TIME_absolute_add (now, rel);
147 GNUNET_assert(future.abs_value_us == forever.abs_value_us); 154 GNUNET_assert (future.abs_value_us == forever.abs_value_us);
148 155
149 /* check zero */ 156 /* check zero */
150 future = GNUNET_TIME_absolute_add(now, GNUNET_TIME_UNIT_ZERO); 157 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
151 GNUNET_assert(future.abs_value_us == now.abs_value_us); 158 GNUNET_assert (future.abs_value_us == now.abs_value_us);
152 159
153 GNUNET_assert(forever.abs_value_us == 160 GNUNET_assert (forever.abs_value_us ==
154 GNUNET_TIME_absolute_subtract(forever, 161 GNUNET_TIME_absolute_subtract (forever,
155 GNUNET_TIME_UNIT_MINUTES).abs_value_us); 162 GNUNET_TIME_UNIT_MINUTES).
163 abs_value_us);
156 /*check absolute subtract */ 164 /*check absolute subtract */
157 now.abs_value_us = 50000; 165 now.abs_value_us = 50000;
158 rel.rel_value_us = 100000; 166 rel.rel_value_us = 100000;
159 GNUNET_assert(GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == 167 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
160 (GNUNET_TIME_absolute_subtract(now, rel)).abs_value_us); 168 (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
161 rel.rel_value_us = 10000; 169 rel.rel_value_us = 10000;
162 GNUNET_assert(40000 == (GNUNET_TIME_absolute_subtract(now, rel)).abs_value_us); 170 GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now,
171 rel)).abs_value_us);
163 172
164 /*check relative divide */ 173 /*check relative divide */
165 GNUNET_assert(GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == 174 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
166 (GNUNET_TIME_relative_divide(rel, 0)).rel_value_us); 175 (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us);
167 176
168 rel = GNUNET_TIME_UNIT_FOREVER_REL; 177 rel = GNUNET_TIME_UNIT_FOREVER_REL;
169 GNUNET_assert(GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == 178 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
170 (GNUNET_TIME_relative_divide(rel, 2)).rel_value_us); 179 (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us);
171 180
172 rel = GNUNET_TIME_relative_divide(relUnit, 2); 181 rel = GNUNET_TIME_relative_divide (relUnit, 2);
173 GNUNET_assert(rel.rel_value_us == relUnit.rel_value_us / 2); 182 GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2);
174 183
175 184
176 /* check Return absolute time of 0ms */ 185 /* check Return absolute time of 0ms */
177 zero = GNUNET_TIME_UNIT_ZERO_ABS; 186 zero = GNUNET_TIME_UNIT_ZERO_ABS;
178 187
179 /* check GNUNET_TIME_calculate_eta */ 188 /* check GNUNET_TIME_calculate_eta */
180 last.abs_value_us = GNUNET_TIME_absolute_get().abs_value_us - 1024; 189 last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024;
181 forever = GNUNET_TIME_UNIT_FOREVER_ABS; 190 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
182 forever.abs_value_us = forever.abs_value_us - 1024; 191 forever.abs_value_us = forever.abs_value_us - 1024;
183 GNUNET_assert(GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == 192 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
184 GNUNET_TIME_calculate_eta(forever, 50000, 100000).rel_value_us); 193 GNUNET_TIME_calculate_eta (forever, 50000,
194 100000).rel_value_us);
185 /* check zero */ 195 /* check zero */
186 GNUNET_log_skip(1, GNUNET_NO); 196 GNUNET_log_skip (1, GNUNET_NO);
187 GNUNET_assert(GNUNET_TIME_UNIT_ZERO.rel_value_us == 197 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
188 (GNUNET_TIME_calculate_eta(last, 60000, 50000)).rel_value_us); 198 (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us);
189 GNUNET_log_skip(0, GNUNET_YES); 199 GNUNET_log_skip (0, GNUNET_YES);
190 /*check forever */ 200 /*check forever */
191 GNUNET_assert(GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == 201 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
192 (GNUNET_TIME_calculate_eta(last, 0, 50000)).rel_value_us); 202 (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us);
193 203
194 /*check relative subtract */ 204 /*check relative subtract */
195 now = GNUNET_TIME_absolute_get(); 205 now = GNUNET_TIME_absolute_get ();
196 rel.rel_value_us = now.abs_value_us; 206 rel.rel_value_us = now.abs_value_us;
197 relForever.rel_value_us = rel.rel_value_us + 1024; 207 relForever.rel_value_us = rel.rel_value_us + 1024;
198 GNUNET_assert(1024 == 208 GNUNET_assert (1024 ==
199 GNUNET_TIME_relative_subtract(relForever, rel).rel_value_us); 209 GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us);
200 /*check zero */ 210 /*check zero */
201 GNUNET_assert(GNUNET_TIME_UNIT_ZERO.rel_value_us == 211 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
202 GNUNET_TIME_relative_subtract(rel, relForever).rel_value_us); 212 GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
203 /*check forever */ 213 /*check forever */
204 rel.rel_value_us = UINT64_MAX; 214 rel.rel_value_us = UINT64_MAX;
205 GNUNET_assert(GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == 215 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
206 GNUNET_TIME_relative_subtract(rel, relForever).rel_value_us); 216 GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
207 217
208 /*check GNUNET_TIME_relative_min */ 218 /*check GNUNET_TIME_relative_min */
209 now = GNUNET_TIME_absolute_get(); 219 now = GNUNET_TIME_absolute_get ();
210 rel.rel_value_us = now.abs_value_us; 220 rel.rel_value_us = now.abs_value_us;
211 relForever.rel_value_us = rel.rel_value_us - 1024; 221 relForever.rel_value_us = rel.rel_value_us - 1024;
212 GNUNET_assert(relForever.rel_value_us == 222 GNUNET_assert (relForever.rel_value_us ==
213 GNUNET_TIME_relative_min(rel, relForever).rel_value_us); 223 GNUNET_TIME_relative_min (rel, relForever).rel_value_us);
214 224
215 /*check GNUNET_TIME_relative_max */ 225 /*check GNUNET_TIME_relative_max */
216 GNUNET_assert(rel.rel_value_us == 226 GNUNET_assert (rel.rel_value_us ==
217 GNUNET_TIME_relative_max(rel, relForever).rel_value_us); 227 GNUNET_TIME_relative_max (rel, relForever).rel_value_us);
218 228
219 /*check GNUNET_TIME_absolute_min */ 229 /*check GNUNET_TIME_absolute_min */
220 now = GNUNET_TIME_absolute_get(); 230 now = GNUNET_TIME_absolute_get ();
221 last.abs_value_us = now.abs_value_us - 1024; 231 last.abs_value_us = now.abs_value_us - 1024;
222 GNUNET_assert(last.abs_value_us == 232 GNUNET_assert (last.abs_value_us ==
223 GNUNET_TIME_absolute_min(now, last).abs_value_us); 233 GNUNET_TIME_absolute_min (now, last).abs_value_us);
224 234
225 /*check GNUNET_TIME_absolute_max */ 235 /*check GNUNET_TIME_absolute_max */
226 GNUNET_assert(now.abs_value_us == 236 GNUNET_assert (now.abs_value_us ==
227 GNUNET_TIME_absolute_max(now, last).abs_value_us); 237 GNUNET_TIME_absolute_max (now, last).abs_value_us);
228 for (unsigned int i = 0; i < 30; i++) 238 for (unsigned int i = 0; i < 30; i++)
229 { 239 {
230 struct GNUNET_CONFIGURATION_Handle *cfg; 240 struct GNUNET_CONFIGURATION_Handle *cfg;
231 241
232 cfg = GNUNET_CONFIGURATION_create(); 242 cfg = GNUNET_CONFIGURATION_create ();
233 last = GNUNET_TIME_absolute_get_monotonic(cfg); 243 last = GNUNET_TIME_absolute_get_monotonic (cfg);
234 now = GNUNET_TIME_absolute_get_monotonic(cfg); 244 now = GNUNET_TIME_absolute_get_monotonic (cfg);
235 GNUNET_assert(now.abs_value_us > last.abs_value_us); 245 GNUNET_assert (now.abs_value_us > last.abs_value_us);
236 (void)GNUNET_TIME_absolute_get_monotonic(NULL); 246 (void) GNUNET_TIME_absolute_get_monotonic (NULL);
237 GNUNET_CONFIGURATION_set_value_string(cfg, 247 GNUNET_CONFIGURATION_set_value_string (cfg,
238 "util", 248 "util",
239 "MONOTONIC_TIME_FILENAME", 249 "MONOTONIC_TIME_FILENAME",
240 "monotonic-time.dat"); 250 "monotonic-time.dat");
241 last = GNUNET_TIME_absolute_get_monotonic(cfg); 251 last = GNUNET_TIME_absolute_get_monotonic (cfg);
242 now = GNUNET_TIME_absolute_get_monotonic(cfg); 252 now = GNUNET_TIME_absolute_get_monotonic (cfg);
243 (void)GNUNET_TIME_absolute_get_monotonic(NULL); 253 (void) GNUNET_TIME_absolute_get_monotonic (NULL);
244 GNUNET_assert(now.abs_value_us > last.abs_value_us); 254 GNUNET_assert (now.abs_value_us > last.abs_value_us);
245 GNUNET_CONFIGURATION_destroy(cfg); 255 GNUNET_CONFIGURATION_destroy (cfg);
246 } 256 }
247 GNUNET_break(GNUNET_OK == 257 GNUNET_break (GNUNET_OK ==
248 GNUNET_DISK_directory_remove("monotonic-time.dat")); 258 GNUNET_DISK_directory_remove ("monotonic-time.dat"));
249 259
250 return 0; 260 return 0;
251} 261}
diff --git a/src/util/test_tun.c b/src/util/test_tun.c
index f94e9c8ff..ecb3dd664 100644
--- a/src/util/test_tun.c
+++ b/src/util/test_tun.c
@@ -29,9 +29,9 @@
29static int ret; 29static int ret;
30 30
31static void 31static void
32test_udp(size_t pll, 32test_udp (size_t pll,
33 int pl_fill, 33 int pl_fill,
34 uint16_t crc) 34 uint16_t crc)
35{ 35{
36 struct GNUNET_TUN_IPv4Header ip; 36 struct GNUNET_TUN_IPv4Header ip;
37 struct GNUNET_TUN_UdpHeader udp; 37 struct GNUNET_TUN_UdpHeader udp;
@@ -39,36 +39,36 @@ test_udp(size_t pll,
39 struct in_addr src; 39 struct in_addr src;
40 struct in_addr dst; 40 struct in_addr dst;
41 41
42 GNUNET_assert(1 == inet_pton(AF_INET, "1.2.3.4", &src)); 42 GNUNET_assert (1 == inet_pton (AF_INET, "1.2.3.4", &src));
43 GNUNET_assert(1 == inet_pton(AF_INET, "122.2.3.5", &dst)); 43 GNUNET_assert (1 == inet_pton (AF_INET, "122.2.3.5", &dst));
44 memset(payload, pl_fill, sizeof(payload)); 44 memset (payload, pl_fill, sizeof(payload));
45 GNUNET_TUN_initialize_ipv4_header(&ip, 45 GNUNET_TUN_initialize_ipv4_header (&ip,
46 IPPROTO_UDP, 46 IPPROTO_UDP,
47 pll + sizeof(udp), 47 pll + sizeof(udp),
48 &src, 48 &src,
49 &dst); 49 &dst);
50 udp.source_port = htons(4242); 50 udp.source_port = htons (4242);
51 udp.destination_port = htons(4242); 51 udp.destination_port = htons (4242);
52 udp.len = htons(pll); 52 udp.len = htons (pll);
53 GNUNET_TUN_calculate_udp4_checksum(&ip, 53 GNUNET_TUN_calculate_udp4_checksum (&ip,
54 &udp, 54 &udp,
55 payload, 55 payload,
56 pll); 56 pll);
57 if (crc != ntohs(udp.crc)) 57 if (crc != ntohs (udp.crc))
58 { 58 {
59 fprintf(stderr, "Got CRC: %u, wanted: %u\n", 59 fprintf (stderr, "Got CRC: %u, wanted: %u\n",
60 ntohs(udp.crc), 60 ntohs (udp.crc),
61 crc); 61 crc);
62 ret = 1; 62 ret = 1;
63 } 63 }
64} 64}
65 65
66int main(int argc, 66int main (int argc,
67 char **argv) 67 char **argv)
68{ 68{
69 test_udp(4, 3, 22439); 69 test_udp (4, 3, 22439);
70 test_udp(4, 1, 23467); 70 test_udp (4, 1, 23467);
71 test_udp(7, 17, 6516); 71 test_udp (7, 17, 6516);
72 test_udp(12451, 251, 42771); 72 test_udp (12451, 251, 42771);
73 return ret; 73 return ret;
74} 74}
diff --git a/src/util/time.c b/src/util/time.c
index 3df7b469e..5b4833511 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -37,7 +37,7 @@
37#endif 37#endif
38#endif 38#endif
39 39
40#define LOG(kind, ...) GNUNET_log_from(kind, "util-time", __VA_ARGS__) 40#define LOG(kind, ...) GNUNET_log_from (kind, "util-time", __VA_ARGS__)
41 41
42/** 42/**
43 * Variable used to simulate clock skew. Used for testing, never in production. 43 * Variable used to simulate clock skew. Used for testing, never in production.
@@ -50,7 +50,7 @@ static long long timestamp_offset;
50 * @param offset the offset to skew the locale time by 50 * @param offset the offset to skew the locale time by
51 */ 51 */
52void 52void
53GNUNET_TIME_set_offset(long long offset) 53GNUNET_TIME_set_offset (long long offset)
54{ 54{
55 timestamp_offset = offset; 55 timestamp_offset = offset;
56} 56}
@@ -62,7 +62,7 @@ GNUNET_TIME_set_offset(long long offset)
62 * @return the offset we currently skew the locale time by 62 * @return the offset we currently skew the locale time by
63 */ 63 */
64long long 64long long
65GNUNET_TIME_get_offset() 65GNUNET_TIME_get_offset ()
66{ 66{
67 return timestamp_offset; 67 return timestamp_offset;
68} 68}
@@ -77,7 +77,7 @@ GNUNET_TIME_get_offset()
77 * it was just now rounded 77 * it was just now rounded
78 */ 78 */
79int 79int
80GNUNET_TIME_round_abs(struct GNUNET_TIME_Absolute *at) 80GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at)
81{ 81{
82 if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 82 if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
83 return GNUNET_OK; 83 return GNUNET_OK;
@@ -97,7 +97,7 @@ GNUNET_TIME_round_abs(struct GNUNET_TIME_Absolute *at)
97 * it was just now rounded 97 * it was just now rounded
98 */ 98 */
99int 99int
100GNUNET_TIME_round_rel(struct GNUNET_TIME_Relative *rt) 100GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt)
101{ 101{
102 if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 102 if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
103 return GNUNET_OK; 103 return GNUNET_OK;
@@ -115,15 +115,15 @@ GNUNET_TIME_round_rel(struct GNUNET_TIME_Relative *rt)
115 * @return the current time 115 * @return the current time
116 */ 116 */
117struct GNUNET_TIME_Absolute 117struct GNUNET_TIME_Absolute
118GNUNET_TIME_absolute_get() 118GNUNET_TIME_absolute_get ()
119{ 119{
120 struct GNUNET_TIME_Absolute ret; 120 struct GNUNET_TIME_Absolute ret;
121 struct timeval tv; 121 struct timeval tv;
122 122
123 gettimeofday(&tv, NULL); 123 gettimeofday (&tv, NULL);
124 ret.abs_value_us = (uint64_t)(((uint64_t)tv.tv_sec * 1000LL * 1000LL) + 124 ret.abs_value_us = (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL)
125 ((uint64_t)tv.tv_usec)) + 125 + ((uint64_t) tv.tv_usec))
126 timestamp_offset; 126 + timestamp_offset;
127 return ret; 127 return ret;
128} 128}
129 129
@@ -132,7 +132,7 @@ GNUNET_TIME_absolute_get()
132 * Return relative time of 0ms. 132 * Return relative time of 0ms.
133 */ 133 */
134struct GNUNET_TIME_Relative 134struct GNUNET_TIME_Relative
135GNUNET_TIME_relative_get_zero_() 135GNUNET_TIME_relative_get_zero_ ()
136{ 136{
137 static struct GNUNET_TIME_Relative zero; 137 static struct GNUNET_TIME_Relative zero;
138 138
@@ -144,7 +144,7 @@ GNUNET_TIME_relative_get_zero_()
144 * Return absolute time of 0ms. 144 * Return absolute time of 0ms.
145 */ 145 */
146struct GNUNET_TIME_Absolute 146struct GNUNET_TIME_Absolute
147GNUNET_TIME_absolute_get_zero_() 147GNUNET_TIME_absolute_get_zero_ ()
148{ 148{
149 static struct GNUNET_TIME_Absolute zero; 149 static struct GNUNET_TIME_Absolute zero;
150 150
@@ -156,7 +156,7 @@ GNUNET_TIME_absolute_get_zero_()
156 * Return relative time of 1us. 156 * Return relative time of 1us.
157 */ 157 */
158struct GNUNET_TIME_Relative 158struct GNUNET_TIME_Relative
159GNUNET_TIME_relative_get_unit_() 159GNUNET_TIME_relative_get_unit_ ()
160{ 160{
161 static struct GNUNET_TIME_Relative one = { 1 }; 161 static struct GNUNET_TIME_Relative one = { 1 };
162 162
@@ -168,7 +168,7 @@ GNUNET_TIME_relative_get_unit_()
168 * Return relative time of 1ms. 168 * Return relative time of 1ms.
169 */ 169 */
170struct GNUNET_TIME_Relative 170struct GNUNET_TIME_Relative
171GNUNET_TIME_relative_get_millisecond_() 171GNUNET_TIME_relative_get_millisecond_ ()
172{ 172{
173 static struct GNUNET_TIME_Relative one = { 1000 }; 173 static struct GNUNET_TIME_Relative one = { 1000 };
174 174
@@ -180,7 +180,7 @@ GNUNET_TIME_relative_get_millisecond_()
180 * Return relative time of 1s. 180 * Return relative time of 1s.
181 */ 181 */
182struct GNUNET_TIME_Relative 182struct GNUNET_TIME_Relative
183GNUNET_TIME_relative_get_second_() 183GNUNET_TIME_relative_get_second_ ()
184{ 184{
185 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL }; 185 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL };
186 186
@@ -192,7 +192,7 @@ GNUNET_TIME_relative_get_second_()
192 * Return relative time of 1 minute. 192 * Return relative time of 1 minute.
193 */ 193 */
194struct GNUNET_TIME_Relative 194struct GNUNET_TIME_Relative
195GNUNET_TIME_relative_get_minute_() 195GNUNET_TIME_relative_get_minute_ ()
196{ 196{
197 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL }; 197 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL };
198 198
@@ -204,7 +204,7 @@ GNUNET_TIME_relative_get_minute_()
204 * Return relative time of 1 hour. 204 * Return relative time of 1 hour.
205 */ 205 */
206struct GNUNET_TIME_Relative 206struct GNUNET_TIME_Relative
207GNUNET_TIME_relative_get_hour_() 207GNUNET_TIME_relative_get_hour_ ()
208{ 208{
209 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL }; 209 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL };
210 210
@@ -216,7 +216,7 @@ GNUNET_TIME_relative_get_hour_()
216 * Return "forever". 216 * Return "forever".
217 */ 217 */
218struct GNUNET_TIME_Relative 218struct GNUNET_TIME_Relative
219GNUNET_TIME_relative_get_forever_() 219GNUNET_TIME_relative_get_forever_ ()
220{ 220{
221 static struct GNUNET_TIME_Relative forever = { UINT64_MAX }; 221 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
222 222
@@ -228,7 +228,7 @@ GNUNET_TIME_relative_get_forever_()
228 * Return "forever". 228 * Return "forever".
229 */ 229 */
230struct GNUNET_TIME_Absolute 230struct GNUNET_TIME_Absolute
231GNUNET_TIME_absolute_get_forever_() 231GNUNET_TIME_absolute_get_forever_ ()
232{ 232{
233 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX }; 233 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
234 234
@@ -243,19 +243,19 @@ GNUNET_TIME_absolute_get_forever_()
243 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow) 243 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
244 */ 244 */
245struct GNUNET_TIME_Absolute 245struct GNUNET_TIME_Absolute
246GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel) 246GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
247{ 247{
248 struct GNUNET_TIME_Absolute ret; 248 struct GNUNET_TIME_Absolute ret;
249 249
250 if (rel.rel_value_us == UINT64_MAX) 250 if (rel.rel_value_us == UINT64_MAX)
251 return GNUNET_TIME_UNIT_FOREVER_ABS; 251 return GNUNET_TIME_UNIT_FOREVER_ABS;
252 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); 252 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
253 253
254 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us) 254 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
255 { 255 {
256 GNUNET_break(0); /* overflow... */ 256 GNUNET_break (0); /* overflow... */
257 return GNUNET_TIME_UNIT_FOREVER_ABS; 257 return GNUNET_TIME_UNIT_FOREVER_ABS;
258 } 258 }
259 ret.abs_value_us = rel.rel_value_us + now.abs_value_us; 259 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
260 return ret; 260 return ret;
261} 261}
@@ -269,8 +269,8 @@ GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
269 * @return timestamp that is smaller 269 * @return timestamp that is smaller
270 */ 270 */
271struct GNUNET_TIME_Relative 271struct GNUNET_TIME_Relative
272GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1, 272GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
273 struct GNUNET_TIME_Relative t2) 273 struct GNUNET_TIME_Relative t2)
274{ 274{
275 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2; 275 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2;
276} 276}
@@ -284,8 +284,8 @@ GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1,
284 * @return timestamp that is larger 284 * @return timestamp that is larger
285 */ 285 */
286struct GNUNET_TIME_Relative 286struct GNUNET_TIME_Relative
287GNUNET_TIME_relative_max(struct GNUNET_TIME_Relative t1, 287GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
288 struct GNUNET_TIME_Relative t2) 288 struct GNUNET_TIME_Relative t2)
289{ 289{
290 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2; 290 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2;
291} 291}
@@ -299,8 +299,8 @@ GNUNET_TIME_relative_max(struct GNUNET_TIME_Relative t1,
299 * @return timestamp that is smaller 299 * @return timestamp that is smaller
300 */ 300 */
301struct GNUNET_TIME_Absolute 301struct GNUNET_TIME_Absolute
302GNUNET_TIME_absolute_min(struct GNUNET_TIME_Absolute t1, 302GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
303 struct GNUNET_TIME_Absolute t2) 303 struct GNUNET_TIME_Absolute t2)
304{ 304{
305 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2; 305 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2;
306} 306}
@@ -314,8 +314,8 @@ GNUNET_TIME_absolute_min(struct GNUNET_TIME_Absolute t1,
314 * @return timestamp that is bigger 314 * @return timestamp that is bigger
315 */ 315 */
316struct GNUNET_TIME_Absolute 316struct GNUNET_TIME_Absolute
317GNUNET_TIME_absolute_max(struct GNUNET_TIME_Absolute t1, 317GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
318 struct GNUNET_TIME_Absolute t2) 318 struct GNUNET_TIME_Absolute t2)
319{ 319{
320 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2; 320 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2;
321} 321}
@@ -328,13 +328,13 @@ GNUNET_TIME_absolute_max(struct GNUNET_TIME_Absolute t1,
328 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER. 328 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
329 */ 329 */
330struct GNUNET_TIME_Relative 330struct GNUNET_TIME_Relative
331GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future) 331GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
332{ 332{
333 struct GNUNET_TIME_Relative ret; 333 struct GNUNET_TIME_Relative ret;
334 334
335 if (future.abs_value_us == UINT64_MAX) 335 if (future.abs_value_us == UINT64_MAX)
336 return GNUNET_TIME_UNIT_FOREVER_REL; 336 return GNUNET_TIME_UNIT_FOREVER_REL;
337 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); 337 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
338 338
339 if (now.abs_value_us > future.abs_value_us) 339 if (now.abs_value_us > future.abs_value_us)
340 return GNUNET_TIME_UNIT_ZERO; 340 return GNUNET_TIME_UNIT_ZERO;
@@ -350,8 +350,8 @@ GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
350 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start 350 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
351 */ 351 */
352struct GNUNET_TIME_Relative 352struct GNUNET_TIME_Relative
353GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start, 353GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
354 struct GNUNET_TIME_Absolute end) 354 struct GNUNET_TIME_Absolute end)
355{ 355{
356 struct GNUNET_TIME_Relative ret; 356 struct GNUNET_TIME_Relative ret;
357 357
@@ -370,12 +370,12 @@ GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start,
370 * @return 0 if whence > now, otherwise now-whence. 370 * @return 0 if whence > now, otherwise now-whence.
371 */ 371 */
372struct GNUNET_TIME_Relative 372struct GNUNET_TIME_Relative
373GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence) 373GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
374{ 374{
375 struct GNUNET_TIME_Absolute now; 375 struct GNUNET_TIME_Absolute now;
376 struct GNUNET_TIME_Relative ret; 376 struct GNUNET_TIME_Relative ret;
377 377
378 now = GNUNET_TIME_absolute_get(); 378 now = GNUNET_TIME_absolute_get ();
379 if (whence.abs_value_us > now.abs_value_us) 379 if (whence.abs_value_us > now.abs_value_us)
380 return GNUNET_TIME_UNIT_ZERO; 380 return GNUNET_TIME_UNIT_ZERO;
381 ret.rel_value_us = now.abs_value_us - whence.abs_value_us; 381 ret.rel_value_us = now.abs_value_us - whence.abs_value_us;
@@ -390,8 +390,8 @@ GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
390 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise 390 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
391 */ 391 */
392struct GNUNET_TIME_Absolute 392struct GNUNET_TIME_Absolute
393GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, 393GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
394 struct GNUNET_TIME_Relative duration) 394 struct GNUNET_TIME_Relative duration)
395{ 395{
396 struct GNUNET_TIME_Absolute ret; 396 struct GNUNET_TIME_Absolute ret;
397 397
@@ -399,10 +399,10 @@ GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start,
399 (duration.rel_value_us == UINT64_MAX)) 399 (duration.rel_value_us == UINT64_MAX))
400 return GNUNET_TIME_UNIT_FOREVER_ABS; 400 return GNUNET_TIME_UNIT_FOREVER_ABS;
401 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us) 401 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
402 { 402 {
403 GNUNET_break(0); 403 GNUNET_break (0);
404 return GNUNET_TIME_UNIT_FOREVER_ABS; 404 return GNUNET_TIME_UNIT_FOREVER_ABS;
405 } 405 }
406 ret.abs_value_us = start.abs_value_us + duration.rel_value_us; 406 ret.abs_value_us = start.abs_value_us + duration.rel_value_us;
407 return ret; 407 return ret;
408} 408}
@@ -417,8 +417,8 @@ GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start,
417 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise 417 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
418 */ 418 */
419struct GNUNET_TIME_Absolute 419struct GNUNET_TIME_Absolute
420GNUNET_TIME_absolute_subtract(struct GNUNET_TIME_Absolute start, 420GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
421 struct GNUNET_TIME_Relative duration) 421 struct GNUNET_TIME_Relative duration)
422{ 422{
423 struct GNUNET_TIME_Absolute ret; 423 struct GNUNET_TIME_Absolute ret;
424 424
@@ -437,8 +437,8 @@ GNUNET_TIME_absolute_subtract(struct GNUNET_TIME_Absolute start,
437 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 437 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
438 */ 438 */
439struct GNUNET_TIME_Relative 439struct GNUNET_TIME_Relative
440GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, 440GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
441 unsigned long long factor) 441 unsigned long long factor)
442{ 442{
443 struct GNUNET_TIME_Relative ret; 443 struct GNUNET_TIME_Relative ret;
444 444
@@ -448,10 +448,10 @@ GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel,
448 return GNUNET_TIME_UNIT_FOREVER_REL; 448 return GNUNET_TIME_UNIT_FOREVER_REL;
449 ret.rel_value_us = rel.rel_value_us * factor; 449 ret.rel_value_us = rel.rel_value_us * factor;
450 if (ret.rel_value_us / factor != rel.rel_value_us) 450 if (ret.rel_value_us / factor != rel.rel_value_us)
451 { 451 {
452 GNUNET_break(0); 452 GNUNET_break (0);
453 return GNUNET_TIME_UNIT_FOREVER_REL; 453 return GNUNET_TIME_UNIT_FOREVER_REL;
454 } 454 }
455 return ret; 455 return ret;
456} 456}
457 457
@@ -463,27 +463,27 @@ GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel,
463 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 463 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
464 */ 464 */
465struct GNUNET_TIME_Relative 465struct GNUNET_TIME_Relative
466relative_multiply_double(struct GNUNET_TIME_Relative rel, double factor) 466relative_multiply_double (struct GNUNET_TIME_Relative rel, double factor)
467{ 467{
468 struct GNUNET_TIME_Relative out; 468 struct GNUNET_TIME_Relative out;
469 double m; 469 double m;
470 470
471 GNUNET_assert(0 <= factor); 471 GNUNET_assert (0 <= factor);
472 472
473 if (0 == factor) 473 if (0 == factor)
474 return GNUNET_TIME_UNIT_ZERO; 474 return GNUNET_TIME_UNIT_ZERO;
475 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 475 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
476 return GNUNET_TIME_UNIT_FOREVER_REL; 476 return GNUNET_TIME_UNIT_FOREVER_REL;
477 477
478 m = ((double)rel.rel_value_us) * factor; 478 m = ((double) rel.rel_value_us) * factor;
479 479
480 if (m >= (double)(GNUNET_TIME_UNIT_FOREVER_REL).rel_value_us) 480 if (m >= (double) (GNUNET_TIME_UNIT_FOREVER_REL).rel_value_us)
481 { 481 {
482 GNUNET_break(0); 482 GNUNET_break (0);
483 return GNUNET_TIME_UNIT_FOREVER_REL; 483 return GNUNET_TIME_UNIT_FOREVER_REL;
484 } 484 }
485 485
486 out.rel_value_us = (uint64_t)m; 486 out.rel_value_us = (uint64_t) m;
487 return out; 487 return out;
488} 488}
489 489
@@ -496,8 +496,8 @@ relative_multiply_double(struct GNUNET_TIME_Relative rel, double factor)
496 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 496 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
497 */ 497 */
498struct GNUNET_TIME_Relative 498struct GNUNET_TIME_Relative
499GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel, 499GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
500 unsigned long long factor) 500 unsigned long long factor)
501{ 501{
502 struct GNUNET_TIME_Relative ret; 502 struct GNUNET_TIME_Relative ret;
503 503
@@ -507,9 +507,9 @@ GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel,
507 return GNUNET_TIME_UNIT_FOREVER_REL; 507 return GNUNET_TIME_UNIT_FOREVER_REL;
508 ret.rel_value_us = rel.rel_value_us * factor; 508 ret.rel_value_us = rel.rel_value_us * factor;
509 if (ret.rel_value_us / factor != rel.rel_value_us) 509 if (ret.rel_value_us / factor != rel.rel_value_us)
510 { 510 {
511 return GNUNET_TIME_UNIT_FOREVER_REL; 511 return GNUNET_TIME_UNIT_FOREVER_REL;
512 } 512 }
513 return ret; 513 return ret;
514} 514}
515 515
@@ -522,8 +522,8 @@ GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel,
522 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor 522 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
523 */ 523 */
524struct GNUNET_TIME_Relative 524struct GNUNET_TIME_Relative
525GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel, 525GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
526 unsigned long long factor) 526 unsigned long long factor)
527{ 527{
528 struct GNUNET_TIME_Relative ret; 528 struct GNUNET_TIME_Relative ret;
529 529
@@ -546,22 +546,22 @@ GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel,
546 * assuming it continues at the same speed 546 * assuming it continues at the same speed
547 */ 547 */
548struct GNUNET_TIME_Relative 548struct GNUNET_TIME_Relative
549GNUNET_TIME_calculate_eta(struct GNUNET_TIME_Absolute start, 549GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
550 uint64_t finished, 550 uint64_t finished,
551 uint64_t total) 551 uint64_t total)
552{ 552{
553 struct GNUNET_TIME_Relative dur; 553 struct GNUNET_TIME_Relative dur;
554 double exp; 554 double exp;
555 struct GNUNET_TIME_Relative ret; 555 struct GNUNET_TIME_Relative ret;
556 556
557 GNUNET_break(finished <= total); 557 GNUNET_break (finished <= total);
558 if (finished >= total) 558 if (finished >= total)
559 return GNUNET_TIME_UNIT_ZERO; 559 return GNUNET_TIME_UNIT_ZERO;
560 if (0 == finished) 560 if (0 == finished)
561 return GNUNET_TIME_UNIT_FOREVER_REL; 561 return GNUNET_TIME_UNIT_FOREVER_REL;
562 dur = GNUNET_TIME_absolute_get_duration(start); 562 dur = GNUNET_TIME_absolute_get_duration (start);
563 exp = ((double)dur.rel_value_us) * ((double)total) / ((double)finished); 563 exp = ((double) dur.rel_value_us) * ((double) total) / ((double) finished);
564 ret.rel_value_us = ((uint64_t)exp) - dur.rel_value_us; 564 ret.rel_value_us = ((uint64_t) exp) - dur.rel_value_us;
565 return ret; 565 return ret;
566} 566}
567 567
@@ -574,18 +574,18 @@ GNUNET_TIME_calculate_eta(struct GNUNET_TIME_Absolute start,
574 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise 574 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
575 */ 575 */
576struct GNUNET_TIME_Relative 576struct GNUNET_TIME_Relative
577GNUNET_TIME_relative_add(struct GNUNET_TIME_Relative a1, 577GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
578 struct GNUNET_TIME_Relative a2) 578 struct GNUNET_TIME_Relative a2)
579{ 579{
580 struct GNUNET_TIME_Relative ret; 580 struct GNUNET_TIME_Relative ret;
581 581
582 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX)) 582 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
583 return GNUNET_TIME_UNIT_FOREVER_REL; 583 return GNUNET_TIME_UNIT_FOREVER_REL;
584 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us) 584 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
585 { 585 {
586 GNUNET_break(0); 586 GNUNET_break (0);
587 return GNUNET_TIME_UNIT_FOREVER_REL; 587 return GNUNET_TIME_UNIT_FOREVER_REL;
588 } 588 }
589 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us; 589 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
590 return ret; 590 return ret;
591} 591}
@@ -599,8 +599,8 @@ GNUNET_TIME_relative_add(struct GNUNET_TIME_Relative a1,
599 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise 599 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
600 */ 600 */
601struct GNUNET_TIME_Relative 601struct GNUNET_TIME_Relative
602GNUNET_TIME_relative_subtract(struct GNUNET_TIME_Relative a1, 602GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
603 struct GNUNET_TIME_Relative a2) 603 struct GNUNET_TIME_Relative a2)
604{ 604{
605 struct GNUNET_TIME_Relative ret; 605 struct GNUNET_TIME_Relative ret;
606 606
@@ -620,11 +620,11 @@ GNUNET_TIME_relative_subtract(struct GNUNET_TIME_Relative a1,
620 * @return time in network byte order 620 * @return time in network byte order
621 */ 621 */
622struct GNUNET_TIME_RelativeNBO 622struct GNUNET_TIME_RelativeNBO
623GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a) 623GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
624{ 624{
625 struct GNUNET_TIME_RelativeNBO ret; 625 struct GNUNET_TIME_RelativeNBO ret;
626 626
627 ret.rel_value_us__ = GNUNET_htonll(a.rel_value_us); 627 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
628 return ret; 628 return ret;
629} 629}
630 630
@@ -636,11 +636,11 @@ GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a)
636 * @return time in host byte order 636 * @return time in host byte order
637 */ 637 */
638struct GNUNET_TIME_Relative 638struct GNUNET_TIME_Relative
639GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a) 639GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
640{ 640{
641 struct GNUNET_TIME_Relative ret; 641 struct GNUNET_TIME_Relative ret;
642 642
643 ret.rel_value_us = GNUNET_ntohll(a.rel_value_us__); 643 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
644 return ret; 644 return ret;
645} 645}
646 646
@@ -652,11 +652,11 @@ GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
652 * @return time in network byte order 652 * @return time in network byte order
653 */ 653 */
654struct GNUNET_TIME_AbsoluteNBO 654struct GNUNET_TIME_AbsoluteNBO
655GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a) 655GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
656{ 656{
657 struct GNUNET_TIME_AbsoluteNBO ret; 657 struct GNUNET_TIME_AbsoluteNBO ret;
658 658
659 ret.abs_value_us__ = GNUNET_htonll(a.abs_value_us); 659 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
660 return ret; 660 return ret;
661} 661}
662 662
@@ -668,11 +668,11 @@ GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
668 * @return time in host byte order 668 * @return time in host byte order
669 */ 669 */
670struct GNUNET_TIME_Absolute 670struct GNUNET_TIME_Absolute
671GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a) 671GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
672{ 672{
673 struct GNUNET_TIME_Absolute ret; 673 struct GNUNET_TIME_Absolute ret;
674 674
675 ret.abs_value_us = GNUNET_ntohll(a.abs_value_us__); 675 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
676 return ret; 676 return ret;
677} 677}
678 678
@@ -681,13 +681,13 @@ GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
681 * Return the current year (i.e. '2011'). 681 * Return the current year (i.e. '2011').
682 */ 682 */
683unsigned int 683unsigned int
684GNUNET_TIME_get_current_year() 684GNUNET_TIME_get_current_year ()
685{ 685{
686 time_t tp; 686 time_t tp;
687 struct tm *t; 687 struct tm *t;
688 688
689 tp = time(NULL); 689 tp = time (NULL);
690 t = gmtime(&tp); 690 t = gmtime (&tp);
691 if (t == NULL) 691 if (t == NULL)
692 return 0; 692 return 0;
693 return t->tm_year + 1900; 693 return t->tm_year + 1900;
@@ -701,13 +701,13 @@ GNUNET_TIME_get_current_year()
701 * @return year a year (after 1970), 0 on error 701 * @return year a year (after 1970), 0 on error
702 */ 702 */
703unsigned int 703unsigned int
704GNUNET_TIME_time_to_year(struct GNUNET_TIME_Absolute at) 704GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
705{ 705{
706 struct tm *t; 706 struct tm *t;
707 time_t tp; 707 time_t tp;
708 708
709 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */ 709 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
710 t = gmtime(&tp); 710 t = gmtime (&tp);
711 if (t == NULL) 711 if (t == NULL)
712 return 0; 712 return 0;
713 return t->tm_year + 1900; 713 return t->tm_year + 1900;
@@ -721,25 +721,25 @@ GNUNET_TIME_time_to_year(struct GNUNET_TIME_Absolute at)
721 * @return absolute time for January 1st of that year. 721 * @return absolute time for January 1st of that year.
722 */ 722 */
723struct GNUNET_TIME_Absolute 723struct GNUNET_TIME_Absolute
724GNUNET_TIME_year_to_time(unsigned int year) 724GNUNET_TIME_year_to_time (unsigned int year)
725{ 725{
726 struct GNUNET_TIME_Absolute ret; 726 struct GNUNET_TIME_Absolute ret;
727 time_t tp; 727 time_t tp;
728 struct tm t; 728 struct tm t;
729 729
730 memset(&t, 0, sizeof(t)); 730 memset (&t, 0, sizeof(t));
731 if (year < 1900) 731 if (year < 1900)
732 { 732 {
733 GNUNET_break(0); 733 GNUNET_break (0);
734 return GNUNET_TIME_absolute_get(); /* now */ 734 return GNUNET_TIME_absolute_get (); /* now */
735 } 735 }
736 t.tm_year = year - 1900; 736 t.tm_year = year - 1900;
737 t.tm_mday = 1; 737 t.tm_mday = 1;
738 t.tm_mon = 0; 738 t.tm_mon = 0;
739 t.tm_wday = 1; 739 t.tm_wday = 1;
740 t.tm_yday = 1; 740 t.tm_yday = 1;
741 tp = mktime(&t); 741 tp = mktime (&t);
742 GNUNET_break(tp != (time_t)-1); 742 GNUNET_break (tp != (time_t) -1);
743 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */ 743 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */
744 return ret; 744 return ret;
745} 745}
@@ -755,16 +755,16 @@ GNUNET_TIME_year_to_time(unsigned int year)
755 * @return the next backoff time 755 * @return the next backoff time
756 */ 756 */
757struct GNUNET_TIME_Relative 757struct GNUNET_TIME_Relative
758GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, 758GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt,
759 struct GNUNET_TIME_Relative threshold) 759 struct GNUNET_TIME_Relative threshold)
760{ 760{
761 double r = (rand() % 500) / 1000.0; 761 double r = (rand () % 500) / 1000.0;
762 struct GNUNET_TIME_Relative t; 762 struct GNUNET_TIME_Relative t;
763 763
764 t = relative_multiply_double( 764 t = relative_multiply_double (
765 GNUNET_TIME_relative_max(GNUNET_TIME_UNIT_MILLISECONDS, rt), 765 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, rt),
766 2 + r); 766 2 + r);
767 return GNUNET_TIME_relative_min(threshold, t); 767 return GNUNET_TIME_relative_min (threshold, t);
768} 768}
769 769
770 770
@@ -775,11 +775,11 @@ GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt,
775 * @return randomized time 775 * @return randomized time
776 */ 776 */
777struct GNUNET_TIME_Relative 777struct GNUNET_TIME_Relative
778GNUNET_TIME_randomize(struct GNUNET_TIME_Relative r) 778GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r)
779{ 779{
780 double d = ((rand() % 1001) - 500) / 1000.0; 780 double d = ((rand () % 1001) - 500) / 1000.0;
781 781
782 return relative_multiply_double(r, d); 782 return relative_multiply_double (r, d);
783} 783}
784 784
785 785
@@ -802,7 +802,7 @@ GNUNET_TIME_randomize(struct GNUNET_TIME_Relative r)
802 * @return monotonically increasing time 802 * @return monotonically increasing time
803 */ 803 */
804struct GNUNET_TIME_Absolute 804struct GNUNET_TIME_Absolute
805GNUNET_TIME_absolute_get_monotonic( 805GNUNET_TIME_absolute_get_monotonic (
806 const struct GNUNET_CONFIGURATION_Handle *cfg) 806 const struct GNUNET_CONFIGURATION_Handle *cfg)
807{ 807{
808 static const struct GNUNET_CONFIGURATION_Handle *last_cfg; 808 static const struct GNUNET_CONFIGURATION_Handle *last_cfg;
@@ -811,114 +811,114 @@ GNUNET_TIME_absolute_get_monotonic(
811 static ATOMIC volatile uint64_t *map; 811 static ATOMIC volatile uint64_t *map;
812 struct GNUNET_TIME_Absolute now; 812 struct GNUNET_TIME_Absolute now;
813 813
814 now = GNUNET_TIME_absolute_get(); 814 now = GNUNET_TIME_absolute_get ();
815 if (last_cfg != cfg) 815 if (last_cfg != cfg)
816 {
817 char *filename;
818
819 if (NULL != map_handle)
816 { 820 {
817 char *filename; 821 GNUNET_DISK_file_unmap (map_handle);
822 map_handle = NULL;
823 }
824 map = NULL;
825
826 last_cfg = cfg;
827 if ((NULL != cfg) &&
828 (GNUNET_OK ==
829 GNUNET_CONFIGURATION_get_value_filename (cfg,
830 "util",
831 "MONOTONIC_TIME_FILENAME",
832 &filename)))
833 {
834 struct GNUNET_DISK_FileHandle *fh;
835
836 fh = GNUNET_DISK_file_open (filename,
837 GNUNET_DISK_OPEN_READWRITE
838 | GNUNET_DISK_OPEN_CREATE,
839 GNUNET_DISK_PERM_USER_WRITE
840 | GNUNET_DISK_PERM_GROUP_WRITE
841 | GNUNET_DISK_PERM_USER_READ
842 | GNUNET_DISK_PERM_GROUP_READ);
843 if (NULL == fh)
844 {
845 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
846 _ ("Failed to map `%s', cannot assure monotonic time!\n"),
847 filename);
848 }
849 else
850 {
851 off_t size;
852
853 size = 0;
854 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &size));
855 if (size < (off_t) sizeof(*map))
856 {
857 struct GNUNET_TIME_AbsoluteNBO o;
818 858
819 if (NULL != map_handle) 859 o = GNUNET_TIME_absolute_hton (now);
860 if (sizeof(o) != GNUNET_DISK_file_write (fh, &o, sizeof(o)))
861 size = 0;
862 else
863 size = sizeof(o);
864 }
865 if (size == sizeof(*map))
820 { 866 {
821 GNUNET_DISK_file_unmap(map_handle); 867 map = GNUNET_DISK_file_map (fh,
822 map_handle = NULL; 868 &map_handle,
869 GNUNET_DISK_MAP_TYPE_READWRITE,
870 sizeof(*map));
871 if (NULL == map)
872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
873 _ (
874 "Failed to map `%s', cannot assure monotonic time!\n"),
875 filename);
823 } 876 }
824 map = NULL; 877 else
825
826 last_cfg = cfg;
827 if ((NULL != cfg) &&
828 (GNUNET_OK ==
829 GNUNET_CONFIGURATION_get_value_filename(cfg,
830 "util",
831 "MONOTONIC_TIME_FILENAME",
832 &filename)))
833 { 878 {
834 struct GNUNET_DISK_FileHandle *fh; 879 GNUNET_log (
835 880 GNUNET_ERROR_TYPE_WARNING,
836 fh = GNUNET_DISK_file_open(filename, 881 _ (
837 GNUNET_DISK_OPEN_READWRITE | 882 "Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"),
838 GNUNET_DISK_OPEN_CREATE, 883 filename);
839 GNUNET_DISK_PERM_USER_WRITE |
840 GNUNET_DISK_PERM_GROUP_WRITE |
841 GNUNET_DISK_PERM_USER_READ |
842 GNUNET_DISK_PERM_GROUP_READ);
843 if (NULL == fh)
844 {
845 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
846 _("Failed to map `%s', cannot assure monotonic time!\n"),
847 filename);
848 }
849 else
850 {
851 off_t size;
852
853 size = 0;
854 GNUNET_break(GNUNET_OK == GNUNET_DISK_file_handle_size(fh, &size));
855 if (size < (off_t)sizeof(*map))
856 {
857 struct GNUNET_TIME_AbsoluteNBO o;
858
859 o = GNUNET_TIME_absolute_hton(now);
860 if (sizeof(o) != GNUNET_DISK_file_write(fh, &o, sizeof(o)))
861 size = 0;
862 else
863 size = sizeof(o);
864 }
865 if (size == sizeof(*map))
866 {
867 map = GNUNET_DISK_file_map(fh,
868 &map_handle,
869 GNUNET_DISK_MAP_TYPE_READWRITE,
870 sizeof(*map));
871 if (NULL == map)
872 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
873 _(
874 "Failed to map `%s', cannot assure monotonic time!\n"),
875 filename);
876 }
877 else
878 {
879 GNUNET_log(
880 GNUNET_ERROR_TYPE_WARNING,
881 _(
882 "Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"),
883 filename);
884 }
885 }
886 GNUNET_DISK_file_close(fh);
887 GNUNET_free(filename);
888 } 884 }
885 }
886 GNUNET_DISK_file_close (fh);
887 GNUNET_free (filename);
889 } 888 }
889 }
890 if (NULL != map) 890 if (NULL != map)
891 { 891 {
892 struct GNUNET_TIME_AbsoluteNBO mt; 892 struct GNUNET_TIME_AbsoluteNBO mt;
893 893
894#if __STDC_NO_ATOMICS__ 894#if __STDC_NO_ATOMICS__
895#if __GNUC__ 895#if __GNUC__
896 mt.abs_value_us__ = __sync_fetch_and_or(map, 0); 896 mt.abs_value_us__ = __sync_fetch_and_or (map, 0);
897#else 897#else
898 mt.abs_value_us__ = *map; /* godspeed, pray this is atomic */ 898 mt.abs_value_us__ = *map; /* godspeed, pray this is atomic */
899#endif 899#endif
900#else 900#else
901 mt.abs_value_us__ = atomic_load(map); 901 mt.abs_value_us__ = atomic_load (map);
902#endif 902#endif
903 last_time = 903 last_time =
904 GNUNET_TIME_absolute_max(GNUNET_TIME_absolute_ntoh(mt), last_time); 904 GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), last_time);
905 } 905 }
906 if (now.abs_value_us <= last_time.abs_value_us) 906 if (now.abs_value_us <= last_time.abs_value_us)
907 now.abs_value_us = last_time.abs_value_us + 1; 907 now.abs_value_us = last_time.abs_value_us + 1;
908 last_time = now; 908 last_time = now;
909 if (NULL != map) 909 if (NULL != map)
910 { 910 {
911 uint64_t val = GNUNET_TIME_absolute_hton(now).abs_value_us__; 911 uint64_t val = GNUNET_TIME_absolute_hton (now).abs_value_us__;
912#if __STDC_NO_ATOMICS__ 912#if __STDC_NO_ATOMICS__
913#if __GNUC__ 913#if __GNUC__
914 (void)__sync_lock_test_and_set(map, val); 914 (void) __sync_lock_test_and_set (map, val);
915#else 915#else
916 *map = val; /* godspeed, pray this is atomic */ 916 *map = val; /* godspeed, pray this is atomic */
917#endif 917#endif
918#else 918#else
919 atomic_store(map, val); 919 atomic_store (map, val);
920#endif 920#endif
921 } 921 }
922 return now; 922 return now;
923} 923}
924 924
@@ -926,9 +926,9 @@ GNUNET_TIME_absolute_get_monotonic(
926/** 926/**
927 * Destructor 927 * Destructor
928 */ 928 */
929void __attribute__ ((destructor)) GNUNET_util_time_fini() 929void __attribute__ ((destructor)) GNUNET_util_time_fini ()
930{ 930{
931 (void)GNUNET_TIME_absolute_get_monotonic(NULL); 931 (void) GNUNET_TIME_absolute_get_monotonic (NULL);
932} 932}
933 933
934/* end of time.c */ 934/* end of time.c */
diff --git a/src/util/tun.c b/src/util/tun.c
index f3b97b860..0e3017551 100644
--- a/src/util/tun.c
+++ b/src/util/tun.c
@@ -43,28 +43,28 @@
43 * @param dst destination IP address to use 43 * @param dst destination IP address to use
44 */ 44 */
45void 45void
46GNUNET_TUN_initialize_ipv4_header(struct GNUNET_TUN_IPv4Header *ip, 46GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip,
47 uint8_t protocol, 47 uint8_t protocol,
48 uint16_t payload_length, 48 uint16_t payload_length,
49 const struct in_addr *src, 49 const struct in_addr *src,
50 const struct in_addr *dst) 50 const struct in_addr *dst)
51{ 51{
52 GNUNET_assert(20 == sizeof(struct GNUNET_TUN_IPv4Header)); 52 GNUNET_assert (20 == sizeof(struct GNUNET_TUN_IPv4Header));
53 GNUNET_assert(payload_length <= 53 GNUNET_assert (payload_length <=
54 UINT16_MAX - sizeof(struct GNUNET_TUN_IPv4Header)); 54 UINT16_MAX - sizeof(struct GNUNET_TUN_IPv4Header));
55 memset(ip, 0, sizeof(struct GNUNET_TUN_IPv4Header)); 55 memset (ip, 0, sizeof(struct GNUNET_TUN_IPv4Header));
56 ip->header_length = sizeof(struct GNUNET_TUN_IPv4Header) / 4; 56 ip->header_length = sizeof(struct GNUNET_TUN_IPv4Header) / 4;
57 ip->version = 4; 57 ip->version = 4;
58 ip->total_length = 58 ip->total_length =
59 htons(sizeof(struct GNUNET_TUN_IPv4Header) + payload_length); 59 htons (sizeof(struct GNUNET_TUN_IPv4Header) + payload_length);
60 ip->identification = 60 ip->identification =
61 (uint16_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 65536); 61 (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 65536);
62 ip->ttl = FRESH_TTL; 62 ip->ttl = FRESH_TTL;
63 ip->protocol = protocol; 63 ip->protocol = protocol;
64 ip->source_address = *src; 64 ip->source_address = *src;
65 ip->destination_address = *dst; 65 ip->destination_address = *dst;
66 ip->checksum = 66 ip->checksum =
67 GNUNET_CRYPTO_crc16_n(ip, sizeof(struct GNUNET_TUN_IPv4Header)); 67 GNUNET_CRYPTO_crc16_n (ip, sizeof(struct GNUNET_TUN_IPv4Header));
68} 68}
69 69
70 70
@@ -78,19 +78,19 @@ GNUNET_TUN_initialize_ipv4_header(struct GNUNET_TUN_IPv4Header *ip,
78 * @param dst destination IP address to use 78 * @param dst destination IP address to use
79 */ 79 */
80void 80void
81GNUNET_TUN_initialize_ipv6_header(struct GNUNET_TUN_IPv6Header *ip, 81GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip,
82 uint8_t protocol, 82 uint8_t protocol,
83 uint16_t payload_length, 83 uint16_t payload_length,
84 const struct in6_addr *src, 84 const struct in6_addr *src,
85 const struct in6_addr *dst) 85 const struct in6_addr *dst)
86{ 86{
87 GNUNET_assert(40 == sizeof(struct GNUNET_TUN_IPv6Header)); 87 GNUNET_assert (40 == sizeof(struct GNUNET_TUN_IPv6Header));
88 GNUNET_assert(payload_length <= 88 GNUNET_assert (payload_length <=
89 UINT16_MAX - sizeof(struct GNUNET_TUN_IPv6Header)); 89 UINT16_MAX - sizeof(struct GNUNET_TUN_IPv6Header));
90 memset(ip, 0, sizeof(struct GNUNET_TUN_IPv6Header)); 90 memset (ip, 0, sizeof(struct GNUNET_TUN_IPv6Header));
91 ip->version = 6; 91 ip->version = 6;
92 ip->next_header = protocol; 92 ip->next_header = protocol;
93 ip->payload_length = htons((uint16_t)payload_length); 93 ip->payload_length = htons ((uint16_t) payload_length);
94 ip->hop_limit = FRESH_TTL; 94 ip->hop_limit = FRESH_TTL;
95 ip->destination_address = *dst; 95 ip->destination_address = *dst;
96 ip->source_address = *src; 96 ip->source_address = *src;
@@ -106,32 +106,32 @@ GNUNET_TUN_initialize_ipv6_header(struct GNUNET_TUN_IPv6Header *ip,
106 * @param payload_length number of bytes of TCP payload 106 * @param payload_length number of bytes of TCP payload
107 */ 107 */
108void 108void
109GNUNET_TUN_calculate_tcp4_checksum(const struct GNUNET_TUN_IPv4Header *ip, 109GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
110 struct GNUNET_TUN_TcpHeader *tcp, 110 struct GNUNET_TUN_TcpHeader *tcp,
111 const void *payload, 111 const void *payload,
112 uint16_t payload_length) 112 uint16_t payload_length)
113{ 113{
114 uint32_t sum; 114 uint32_t sum;
115 uint16_t tmp; 115 uint16_t tmp;
116 116
117 GNUNET_assert(20 == sizeof(struct GNUNET_TUN_TcpHeader)); 117 GNUNET_assert (20 == sizeof(struct GNUNET_TUN_TcpHeader));
118 GNUNET_assert(payload_length + sizeof(struct GNUNET_TUN_IPv4Header) + 118 GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_IPv4Header)
119 sizeof(struct GNUNET_TUN_TcpHeader) == 119 + sizeof(struct GNUNET_TUN_TcpHeader) ==
120 ntohs(ip->total_length)); 120 ntohs (ip->total_length));
121 GNUNET_assert(IPPROTO_TCP == ip->protocol); 121 GNUNET_assert (IPPROTO_TCP == ip->protocol);
122 122
123 tcp->crc = 0; 123 tcp->crc = 0;
124 sum = GNUNET_CRYPTO_crc16_step(0, 124 sum = GNUNET_CRYPTO_crc16_step (0,
125 &ip->source_address, 125 &ip->source_address,
126 sizeof(struct in_addr) * 2); 126 sizeof(struct in_addr) * 2);
127 tmp = htons(IPPROTO_TCP); 127 tmp = htons (IPPROTO_TCP);
128 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint16_t)); 128 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
129 tmp = htons(payload_length + sizeof(struct GNUNET_TUN_TcpHeader)); 129 tmp = htons (payload_length + sizeof(struct GNUNET_TUN_TcpHeader));
130 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint16_t)); 130 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
131 sum = 131 sum =
132 GNUNET_CRYPTO_crc16_step(sum, tcp, sizeof(struct GNUNET_TUN_TcpHeader)); 132 GNUNET_CRYPTO_crc16_step (sum, tcp, sizeof(struct GNUNET_TUN_TcpHeader));
133 sum = GNUNET_CRYPTO_crc16_step(sum, payload, payload_length); 133 sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
134 tcp->crc = GNUNET_CRYPTO_crc16_finish(sum); 134 tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
135} 135}
136 136
137 137
@@ -144,30 +144,30 @@ GNUNET_TUN_calculate_tcp4_checksum(const struct GNUNET_TUN_IPv4Header *ip,
144 * @param payload_length number of bytes of TCP payload 144 * @param payload_length number of bytes of TCP payload
145 */ 145 */
146void 146void
147GNUNET_TUN_calculate_tcp6_checksum(const struct GNUNET_TUN_IPv6Header *ip, 147GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
148 struct GNUNET_TUN_TcpHeader *tcp, 148 struct GNUNET_TUN_TcpHeader *tcp,
149 const void *payload, 149 const void *payload,
150 uint16_t payload_length) 150 uint16_t payload_length)
151{ 151{
152 uint32_t sum; 152 uint32_t sum;
153 uint32_t tmp; 153 uint32_t tmp;
154 154
155 GNUNET_assert(20 == sizeof(struct GNUNET_TUN_TcpHeader)); 155 GNUNET_assert (20 == sizeof(struct GNUNET_TUN_TcpHeader));
156 GNUNET_assert(payload_length + sizeof(struct GNUNET_TUN_TcpHeader) == 156 GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_TcpHeader) ==
157 ntohs(ip->payload_length)); 157 ntohs (ip->payload_length));
158 GNUNET_assert(IPPROTO_TCP == ip->next_header); 158 GNUNET_assert (IPPROTO_TCP == ip->next_header);
159 tcp->crc = 0; 159 tcp->crc = 0;
160 sum = GNUNET_CRYPTO_crc16_step(0, 160 sum = GNUNET_CRYPTO_crc16_step (0,
161 &ip->source_address, 161 &ip->source_address,
162 2 * sizeof(struct in6_addr)); 162 2 * sizeof(struct in6_addr));
163 tmp = htonl(sizeof(struct GNUNET_TUN_TcpHeader) + payload_length); 163 tmp = htonl (sizeof(struct GNUNET_TUN_TcpHeader) + payload_length);
164 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint32_t)); 164 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
165 tmp = htonl(IPPROTO_TCP); 165 tmp = htonl (IPPROTO_TCP);
166 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint32_t)); 166 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
167 sum = 167 sum =
168 GNUNET_CRYPTO_crc16_step(sum, tcp, sizeof(struct GNUNET_TUN_TcpHeader)); 168 GNUNET_CRYPTO_crc16_step (sum, tcp, sizeof(struct GNUNET_TUN_TcpHeader));
169 sum = GNUNET_CRYPTO_crc16_step(sum, payload, payload_length); 169 sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
170 tcp->crc = GNUNET_CRYPTO_crc16_finish(sum); 170 tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
171} 171}
172 172
173 173
@@ -180,33 +180,33 @@ GNUNET_TUN_calculate_tcp6_checksum(const struct GNUNET_TUN_IPv6Header *ip,
180 * @param payload_length number of bytes of UDP payload 180 * @param payload_length number of bytes of UDP payload
181 */ 181 */
182void 182void
183GNUNET_TUN_calculate_udp4_checksum(const struct GNUNET_TUN_IPv4Header *ip, 183GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
184 struct GNUNET_TUN_UdpHeader *udp, 184 struct GNUNET_TUN_UdpHeader *udp,
185 const void *payload, 185 const void *payload,
186 uint16_t payload_length) 186 uint16_t payload_length)
187{ 187{
188 uint32_t sum; 188 uint32_t sum;
189 uint16_t tmp; 189 uint16_t tmp;
190 190
191 GNUNET_assert(8 == sizeof(struct GNUNET_TUN_UdpHeader)); 191 GNUNET_assert (8 == sizeof(struct GNUNET_TUN_UdpHeader));
192 GNUNET_assert(payload_length + sizeof(struct GNUNET_TUN_IPv4Header) + 192 GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_IPv4Header)
193 sizeof(struct GNUNET_TUN_UdpHeader) == 193 + sizeof(struct GNUNET_TUN_UdpHeader) ==
194 ntohs(ip->total_length)); 194 ntohs (ip->total_length));
195 GNUNET_assert(IPPROTO_UDP == ip->protocol); 195 GNUNET_assert (IPPROTO_UDP == ip->protocol);
196 196
197 udp->crc = 197 udp->crc =
198 0; /* technically optional, but we calculate it anyway, just to be sure */ 198 0; /* technically optional, but we calculate it anyway, just to be sure */
199 sum = GNUNET_CRYPTO_crc16_step(0, 199 sum = GNUNET_CRYPTO_crc16_step (0,
200 &ip->source_address, 200 &ip->source_address,
201 sizeof(struct in_addr) * 2); 201 sizeof(struct in_addr) * 2);
202 tmp = htons(IPPROTO_UDP); 202 tmp = htons (IPPROTO_UDP);
203 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint16_t)); 203 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
204 tmp = htons(sizeof(struct GNUNET_TUN_UdpHeader) + payload_length); 204 tmp = htons (sizeof(struct GNUNET_TUN_UdpHeader) + payload_length);
205 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint16_t)); 205 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
206 sum = 206 sum =
207 GNUNET_CRYPTO_crc16_step(sum, udp, sizeof(struct GNUNET_TUN_UdpHeader)); 207 GNUNET_CRYPTO_crc16_step (sum, udp, sizeof(struct GNUNET_TUN_UdpHeader));
208 sum = GNUNET_CRYPTO_crc16_step(sum, payload, payload_length); 208 sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
209 udp->crc = GNUNET_CRYPTO_crc16_finish(sum); 209 udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
210} 210}
211 211
212 212
@@ -219,33 +219,33 @@ GNUNET_TUN_calculate_udp4_checksum(const struct GNUNET_TUN_IPv4Header *ip,
219 * @param payload_length number of bytes of UDP payload 219 * @param payload_length number of bytes of UDP payload
220 */ 220 */
221void 221void
222GNUNET_TUN_calculate_udp6_checksum(const struct GNUNET_TUN_IPv6Header *ip, 222GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
223 struct GNUNET_TUN_UdpHeader *udp, 223 struct GNUNET_TUN_UdpHeader *udp,
224 const void *payload, 224 const void *payload,
225 uint16_t payload_length) 225 uint16_t payload_length)
226{ 226{
227 uint32_t sum; 227 uint32_t sum;
228 uint32_t tmp; 228 uint32_t tmp;
229 229
230 GNUNET_assert(payload_length + sizeof(struct GNUNET_TUN_UdpHeader) == 230 GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_UdpHeader) ==
231 ntohs(ip->payload_length)); 231 ntohs (ip->payload_length));
232 GNUNET_assert(payload_length + sizeof(struct GNUNET_TUN_UdpHeader) == 232 GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_UdpHeader) ==
233 ntohs(udp->len)); 233 ntohs (udp->len));
234 GNUNET_assert(IPPROTO_UDP == ip->next_header); 234 GNUNET_assert (IPPROTO_UDP == ip->next_header);
235 235
236 udp->crc = 0; 236 udp->crc = 0;
237 sum = GNUNET_CRYPTO_crc16_step(0, 237 sum = GNUNET_CRYPTO_crc16_step (0,
238 &ip->source_address, 238 &ip->source_address,
239 sizeof(struct in6_addr) * 2); 239 sizeof(struct in6_addr) * 2);
240 tmp = htons(sizeof(struct GNUNET_TUN_UdpHeader) + 240 tmp = htons (sizeof(struct GNUNET_TUN_UdpHeader)
241 payload_length); /* aka udp->len */ 241 + payload_length); /* aka udp->len */
242 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint32_t)); 242 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
243 tmp = htons(ip->next_header); 243 tmp = htons (ip->next_header);
244 sum = GNUNET_CRYPTO_crc16_step(sum, &tmp, sizeof(uint32_t)); 244 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
245 sum = 245 sum =
246 GNUNET_CRYPTO_crc16_step(sum, udp, sizeof(struct GNUNET_TUN_UdpHeader)); 246 GNUNET_CRYPTO_crc16_step (sum, udp, sizeof(struct GNUNET_TUN_UdpHeader));
247 sum = GNUNET_CRYPTO_crc16_step(sum, payload, payload_length); 247 sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
248 udp->crc = GNUNET_CRYPTO_crc16_finish(sum); 248 udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
249} 249}
250 250
251 251
@@ -257,18 +257,18 @@ GNUNET_TUN_calculate_udp6_checksum(const struct GNUNET_TUN_IPv6Header *ip,
257 * @param payload_length number of bytes of ICMP payload 257 * @param payload_length number of bytes of ICMP payload
258 */ 258 */
259void 259void
260GNUNET_TUN_calculate_icmp_checksum(struct GNUNET_TUN_IcmpHeader *icmp, 260GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp,
261 const void *payload, 261 const void *payload,
262 uint16_t payload_length) 262 uint16_t payload_length)
263{ 263{
264 uint32_t sum; 264 uint32_t sum;
265 265
266 GNUNET_assert(8 == sizeof(struct GNUNET_TUN_IcmpHeader)); 266 GNUNET_assert (8 == sizeof(struct GNUNET_TUN_IcmpHeader));
267 icmp->crc = 0; 267 icmp->crc = 0;
268 sum = 268 sum =
269 GNUNET_CRYPTO_crc16_step(0, icmp, sizeof(struct GNUNET_TUN_IcmpHeader)); 269 GNUNET_CRYPTO_crc16_step (0, icmp, sizeof(struct GNUNET_TUN_IcmpHeader));
270 sum = GNUNET_CRYPTO_crc16_step(sum, payload, payload_length); 270 sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
271 icmp->crc = GNUNET_CRYPTO_crc16_finish(sum); 271 icmp->crc = GNUNET_CRYPTO_crc16_finish (sum);
272} 272}
273 273
274 274
@@ -281,37 +281,38 @@ GNUNET_TUN_calculate_icmp_checksum(struct GNUNET_TUN_IcmpHeader *icmp,
281 * @return #GNUNET_YES if they are equal 281 * @return #GNUNET_YES if they are equal
282 */ 282 */
283int 283int
284GNUNET_TUN_sockaddr_cmp(const struct sockaddr *sa, 284GNUNET_TUN_sockaddr_cmp (const struct sockaddr *sa,
285 const struct sockaddr *sb, 285 const struct sockaddr *sb,
286 int include_port) 286 int include_port)
287{ 287{
288 if (sa->sa_family != sb->sa_family) 288 if (sa->sa_family != sb->sa_family)
289 return GNUNET_NO; 289 return GNUNET_NO;
290 290
291 switch (sa->sa_family) 291 switch (sa->sa_family)
292 { 292 {
293 case AF_INET: { 293 case AF_INET: {
294 const struct sockaddr_in *sa4 = (const struct sockaddr_in *)sa; 294 const struct sockaddr_in *sa4 = (const struct sockaddr_in *) sa;
295 const struct sockaddr_in *sb4 = (const struct sockaddr_in *)sb; 295 const struct sockaddr_in *sb4 = (const struct sockaddr_in *) sb;
296 if ((include_port) && (sa4->sin_port != sb4->sin_port)) 296 if ((include_port) && (sa4->sin_port != sb4->sin_port))
297 return GNUNET_NO; 297 return GNUNET_NO;
298 return(sa4->sin_addr.s_addr == sb4->sin_addr.s_addr); 298 return(sa4->sin_addr.s_addr == sb4->sin_addr.s_addr);
299 } 299 }
300 300
301 case AF_INET6: { 301 case AF_INET6: {
302 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa; 302 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *) sa;
303 const struct sockaddr_in6 *sb6 = (const struct sockaddr_in6 *)sb; 303 const struct sockaddr_in6 *sb6 = (const struct sockaddr_in6 *) sb;
304 304
305 if ((include_port) && (sa6->sin6_port != sb6->sin6_port)) 305 if ((include_port) && (sa6->sin6_port != sb6->sin6_port))
306 return GNUNET_NO; 306 return GNUNET_NO;
307 return( 307 return(
308 0 == memcmp(&sa6->sin6_addr, &sb6->sin6_addr, sizeof(struct in6_addr))); 308 0 == memcmp (&sa6->sin6_addr, &sb6->sin6_addr, sizeof(struct
309 in6_addr)));
309 } 310 }
310 311
311 default: 312 default:
312 GNUNET_break(0); 313 GNUNET_break (0);
313 return GNUNET_SYSERR; 314 return GNUNET_SYSERR;
314 } 315 }
315} 316}
316 317
317 318