aboutsummaryrefslogtreecommitdiff
path: root/src/nse
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
commit502af2167f7c218366666ca4944bd7cc54b5b19a (patch)
treea91fec5cc9769d260640bd91c6633cb9cf395524 /src/nse
parent03af5a603b7cc53432249d5854cd412aa90dde0d (diff)
downloadgnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.tar.gz
gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.zip
indentation
Diffstat (limited to 'src/nse')
-rw-r--r--src/nse/gnunet-nse-profiler.c615
-rw-r--r--src/nse/gnunet-service-nse.c1097
-rw-r--r--src/nse/nse_api.c156
-rw-r--r--src/nse/test_nse_api.c51
-rw-r--r--src/nse/test_nse_multipeer.c155
5 files changed, 990 insertions, 1084 deletions
diff --git a/src/nse/gnunet-nse-profiler.c b/src/nse/gnunet-nse-profiler.c
index d32456a1c..b88dbc3fc 100644
--- a/src/nse/gnunet-nse-profiler.c
+++ b/src/nse/gnunet-nse-profiler.c
@@ -148,48 +148,47 @@ static void
148shutdown_callback (void *cls, const char *emsg) 148shutdown_callback (void *cls, const char *emsg)
149{ 149{
150 if (emsg != NULL) 150 if (emsg != NULL)
151 { 151 {
152#if VERBOSE 152#if VERBOSE
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n"); 153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
154#endif 154#endif
155 if (ok == 0) 155 if (ok == 0)
156 ok = 666; 156 ok = 666;
157 } 157 }
158 else 158 else
159 { 159 {
160#if VERBOSE 160#if VERBOSE
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
162 "All peers successfully shut down!\n");
163#endif 162#endif
164 ok = 0; 163 ok = 0;
165 } 164 }
166} 165}
167 166
168 167
169static void 168static void
170shutdown_task (void *cls, 169shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 const struct GNUNET_SCHEDULER_TaskContext *tc)
172{ 170{
173 struct NSEPeer *pos; 171 struct NSEPeer *pos;
172
174#if VERBOSE 173#if VERBOSE
175 fprintf(stderr, "Ending test.\n"); 174 fprintf (stderr, "Ending test.\n");
176#endif 175#endif
177 176
178 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK) 177 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
179 { 178 {
180 GNUNET_SCHEDULER_cancel(disconnect_task); 179 GNUNET_SCHEDULER_cancel (disconnect_task);
181 disconnect_task = GNUNET_SCHEDULER_NO_TASK; 180 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
182 } 181 }
183 while (NULL != (pos = peer_head)) 182 while (NULL != (pos = peer_head))
184 { 183 {
185 if (pos->nse_handle != NULL) 184 if (pos->nse_handle != NULL)
186 GNUNET_NSE_disconnect(pos->nse_handle); 185 GNUNET_NSE_disconnect (pos->nse_handle);
187 GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos); 186 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
188 GNUNET_free(pos); 187 GNUNET_free (pos);
189 } 188 }
190 189
191 if (data_file != NULL) 190 if (data_file != NULL)
192 GNUNET_DISK_file_close(data_file); 191 GNUNET_DISK_file_close (data_file);
193 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); 192 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
194} 193}
195 194
@@ -205,72 +204,68 @@ shutdown_task (void *cls,
205 * 204 *
206 */ 205 */
207static void 206static void
208handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp, double estimate, double std_dev) 207handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
208 double estimate, double std_dev)
209{ 209{
210 struct NSEPeer *peer = cls; 210 struct NSEPeer *peer = cls;
211 char *output_buffer; 211 char *output_buffer;
212 size_t size; 212 size_t size;
213 213
214 if (output_file != NULL) 214 if (output_file != NULL)
215 { 215 {
216 size = GNUNET_asprintf(&output_buffer, 216 size = GNUNET_asprintf (&output_buffer,
217 "%s %llu %llu %f %f %f\n", 217 "%s %llu %llu %f %f %f\n",
218 GNUNET_i2s(&peer->daemon->id), 218 GNUNET_i2s (&peer->daemon->id),
219 peers_running, 219 peers_running,
220 timestamp.abs_value, 220 timestamp.abs_value,
221 GNUNET_NSE_log_estimate_to_n(estimate), 221 GNUNET_NSE_log_estimate_to_n (estimate),
222 estimate, 222 estimate, std_dev);
223 std_dev); 223 if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
224 if (size != GNUNET_DISK_file_write(output_file, output_buffer, size)) 224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
225 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 225 GNUNET_free (output_buffer);
226 "Unable to write to file!\n"); 226 }
227 GNUNET_free (output_buffer);
228 }
229 else 227 else
230 fprintf(stderr, 228 fprintf (stderr,
231 "Received network size estimate from peer %s. Size: %f std.dev. %f\n", 229 "Received network size estimate from peer %s. Size: %f std.dev. %f\n",
232 GNUNET_i2s(&peer->daemon->id), 230 GNUNET_i2s (&peer->daemon->id), estimate, std_dev);
233 estimate,
234 std_dev);
235 231
236} 232}
237 233
238 234
239static void 235static void
240connect_nse_service (void *cls, 236connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241 const struct GNUNET_SCHEDULER_TaskContext *tc)
242{ 237{
243 struct NSEPeer *current_peer; 238 struct NSEPeer *current_peer;
244 unsigned int i; 239 unsigned int i;
245 240
246#if VERBOSE 241#if VERBOSE
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
248 "Connecting to nse service of peers\n");
249#endif 243#endif
250 for (i = 0; i < num_peers; i++) 244 for (i = 0; i < num_peers; i++)
251 { 245 {
252 if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0)) 246 if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
253 continue; 247 continue;
254#if VERBOSE 248#if VERBOSE
255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "nse-profiler: connecting to nse service of peer %d\n", i); 249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
250 "nse-profiler: connecting to nse service of peer %d\n", i);
256#endif 251#endif
257 current_peer = GNUNET_malloc(sizeof(struct NSEPeer)); 252 current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
258 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i); 253 current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i);
259 if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i))) 254 if (GNUNET_YES ==
260 { 255 GNUNET_TESTING_daemon_running (GNUNET_TESTING_daemon_get (pg, i)))
261 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg, 256 {
262 &handle_estimate, 257 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
263 current_peer); 258 &handle_estimate,
264 GNUNET_assert(current_peer->nse_handle != NULL); 259 current_peer);
265 } 260 GNUNET_assert (current_peer->nse_handle != NULL);
266 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
267 } 261 }
262 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
263 }
268} 264}
269 265
270 266
271static void 267static void
272churn_peers (void *cls, 268churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
273 const struct GNUNET_SCHEDULER_TaskContext *tc);
274 269
275 270
276/** 271/**
@@ -280,31 +275,30 @@ churn_peers (void *cls,
280 * @param success GNUNET_OK if statistics were 275 * @param success GNUNET_OK if statistics were
281 * successfully obtained, GNUNET_SYSERR if not. 276 * successfully obtained, GNUNET_SYSERR if not.
282 */ 277 */
283static void 278static void
284stats_finished_callback (void *cls, int success) 279stats_finished_callback (void *cls, int success)
285{ 280{
286 struct StatsContext *stats_context = cls; 281 struct StatsContext *stats_context = cls;
287 char *buf; 282 char *buf;
288 int buf_len; 283 int buf_len;
289 284
290 if ( (GNUNET_OK == success) && 285 if ((GNUNET_OK == success) && (data_file != NULL))
291 (data_file != NULL) ) 286 {
287 /* Stats lookup successful, write out data */
288 buf = NULL;
289 buf_len = GNUNET_asprintf (&buf,
290 "TOTAL_NSE_BYTES: %u\n",
291 stats_context->total_nse_bytes);
292 if (buf_len > 0)
292 { 293 {
293 /* Stats lookup successful, write out data */ 294 GNUNET_DISK_file_write (data_file, buf, buf_len);
294 buf = NULL;
295 buf_len = GNUNET_asprintf(&buf,
296 "TOTAL_NSE_BYTES: %u\n",
297 stats_context->total_nse_bytes);
298 if (buf_len > 0)
299 {
300 GNUNET_DISK_file_write(data_file, buf, buf_len);
301 }
302 GNUNET_free_non_null(buf);
303 } 295 }
296 GNUNET_free_non_null (buf);
297 }
304 298
305 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle); 299 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
306 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL); 300 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
307 GNUNET_free(stats_context); 301 GNUNET_free (stats_context);
308} 302}
309 303
310 304
@@ -319,69 +313,62 @@ stats_finished_callback (void *cls, int success)
319 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not 313 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
320 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration 314 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
321 */ 315 */
322static int 316static int
323statistics_iterator (void *cls, 317statistics_iterator (void *cls,
324 const struct GNUNET_PeerIdentity *peer, 318 const struct GNUNET_PeerIdentity *peer,
325 const char *subsystem, 319 const char *subsystem,
326 const char *name, 320 const char *name, uint64_t value, int is_persistent)
327 uint64_t value,
328 int is_persistent)
329{ 321{
330 struct StatsContext *stats_context = cls; 322 struct StatsContext *stats_context = cls;
331 323
332 if ( (0 == strstr(subsystem, "nse")) && 324 if ((0 == strstr (subsystem, "nse")) &&
333 (0 == strstr(name, "# flood messages received")) ) 325 (0 == strstr (name, "# flood messages received")))
334 stats_context->total_nse_bytes += value; 326 stats_context->total_nse_bytes += value;
335 return GNUNET_OK; 327 return GNUNET_OK;
336} 328}
337 329
338 330
339static void 331static void
340disconnect_nse_peers (void *cls, 332disconnect_nse_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 const struct GNUNET_SCHEDULER_TaskContext *tc)
342{ 333{
343 struct NSEPeer *pos; 334 struct NSEPeer *pos;
344 char *buf; 335 char *buf;
345 struct StatsContext *stats_context; 336 struct StatsContext *stats_context;
346 337
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
348 "disconnecting nse service of peers\n");
349 disconnect_task = GNUNET_SCHEDULER_NO_TASK; 339 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
350 pos = peer_head; 340 pos = peer_head;
351 while (NULL != (pos = peer_head)) 341 while (NULL != (pos = peer_head))
342 {
343 if (pos->nse_handle != NULL)
352 { 344 {
353 if (pos->nse_handle != NULL) 345 GNUNET_NSE_disconnect (pos->nse_handle);
354 { 346 pos->nse_handle = NULL;
355 GNUNET_NSE_disconnect(pos->nse_handle);
356 pos->nse_handle = NULL;
357 }
358 GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
359 GNUNET_free(pos);
360 }
361
362 GNUNET_asprintf(&buf,
363 "round%llu",
364 current_round);
365 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg,
366 "nse-profiler",
367 buf,
368 &peers_next_round))
369 {
370 current_round++;
371 GNUNET_assert(churn_task == GNUNET_SCHEDULER_NO_TASK);
372 churn_task = GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
373 } 347 }
374 else /* No more rounds, let's shut it down! */ 348 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
375 { 349 GNUNET_free (pos);
376 stats_context = GNUNET_malloc(sizeof(struct StatsContext)); 350 }
377 GNUNET_SCHEDULER_cancel(shutdown_handle); 351
378 shutdown_handle = GNUNET_SCHEDULER_NO_TASK; 352 GNUNET_asprintf (&buf, "round%llu", current_round);
379 GNUNET_TESTING_get_statistics(pg, 353 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg,
380 &stats_finished_callback, 354 "nse-profiler",
381 &statistics_iterator, 355 buf,
382 stats_context); 356 &peers_next_round))
383 } 357 {
384 GNUNET_free(buf); 358 current_round++;
359 GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
360 churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
361 }
362 else /* No more rounds, let's shut it down! */
363 {
364 stats_context = GNUNET_malloc (sizeof (struct StatsContext));
365 GNUNET_SCHEDULER_cancel (shutdown_handle);
366 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
367 GNUNET_TESTING_get_statistics (pg,
368 &stats_finished_callback,
369 &statistics_iterator, stats_context);
370 }
371 GNUNET_free (buf);
385} 372}
386 373
387 374
@@ -391,12 +378,12 @@ disconnect_nse_peers (void *cls,
391 * @param cls unused 378 * @param cls unused
392 * @param emsg NULL on success 379 * @param emsg NULL on success
393 */ 380 */
394static void 381static void
395topology_output_callback (void *cls, const char *emsg) 382topology_output_callback (void *cls, const char *emsg)
396{ 383{
397 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, 384 disconnect_task = GNUNET_SCHEDULER_add_delayed (wait_time,
398 &disconnect_nse_peers, NULL); 385 &disconnect_nse_peers, NULL);
399 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL); 386 GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
400} 387}
401 388
402 389
@@ -411,131 +398,117 @@ churn_callback (void *cls, const char *emsg)
411{ 398{
412 char *temp_output_file; 399 char *temp_output_file;
413 400
414 if (emsg == NULL) /* Everything is okay! */ 401 if (emsg == NULL) /* Everything is okay! */
415 { 402 {
416 peers_running = peers_next_round; 403 peers_running = peers_next_round;
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418 "Round %llu, churn finished successfully.\n", 405 "Round %llu, churn finished successfully.\n", current_round);
419 current_round); 406 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
420 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK); 407 GNUNET_asprintf (&temp_output_file,
421 GNUNET_asprintf(&temp_output_file, 408 "%s_%llu.dot", topology_file, current_round);
422 "%s_%llu.dot", 409 GNUNET_TESTING_peergroup_topology_to_file (pg,
423 topology_file, 410 temp_output_file,
424 current_round); 411 &topology_output_callback, NULL);
425 GNUNET_TESTING_peergroup_topology_to_file(pg, 412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426 temp_output_file, 413 "Writing topology to file %s\n", temp_output_file);
427 &topology_output_callback, 414 GNUNET_free (temp_output_file);
428 NULL); 415 }
429 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
430 "Writing topology to file %s\n",
431 temp_output_file);
432 GNUNET_free(temp_output_file);
433 }
434 else 416 else
435 { 417 {
436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
437 "Round %llu, churn FAILED!!\n", 419 "Round %llu, churn FAILED!!\n", current_round);
438 current_round); 420 GNUNET_SCHEDULER_cancel (shutdown_handle);
439 GNUNET_SCHEDULER_cancel(shutdown_handle); 421 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
440 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL); 422 }
441 }
442} 423}
443 424
444 425
445static void 426static void
446churn_peers (void *cls, 427churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
447 const struct GNUNET_SCHEDULER_TaskContext *tc)
448{ 428{
449 /* peers_running = GNUNET_TESTING_daemons_running(pg); */ 429 /* peers_running = GNUNET_TESTING_daemons_running(pg); */
450 churn_task = GNUNET_SCHEDULER_NO_TASK; 430 churn_task = GNUNET_SCHEDULER_NO_TASK;
451 if (peers_next_round == peers_running) 431 if (peers_next_round == peers_running)
452 { 432 {
453 /* Nothing to do... */ 433 /* Nothing to do... */
454 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL); 434 GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
455 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK); 435 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
456 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, 436 disconnect_task = GNUNET_SCHEDULER_add_delayed (wait_time,
457 &disconnect_nse_peers, NULL); 437 &disconnect_nse_peers,
458 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 438 NULL);
459 "Round %lu, doing nothing!\n", 439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
460 current_round); 440 current_round);
461 } 441 }
462 else 442 else
443 {
444 if (peers_next_round > num_peers)
463 { 445 {
464 if (peers_next_round > num_peers) 446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
465 { 447 "Asked to turn on more peers than we have!!\n");
466 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 448 GNUNET_SCHEDULER_cancel (shutdown_handle);
467 "Asked to turn on more peers than we have!!\n"); 449 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
468 GNUNET_SCHEDULER_cancel(shutdown_handle);
469 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
470 }
471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
472 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
473 current_round,
474 (peers_running > peers_next_round)
475 ? peers_running - peers_next_round
476 : 0,
477 (peers_next_round > peers_running)
478 ? peers_next_round - peers_running
479 : 0);
480 GNUNET_TESTING_daemons_churn (pg, "nse",
481 (peers_running > peers_next_round)
482 ? peers_running - peers_next_round
483 : 0,
484 (peers_next_round > peers_running)
485 ? peers_next_round - peers_running
486 : 0,
487 wait_time,
488 &churn_callback, NULL);
489 } 450 }
451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
452 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
453 current_round,
454 (peers_running > peers_next_round)
455 ? peers_running - peers_next_round
456 : 0,
457 (peers_next_round > peers_running)
458 ? peers_next_round - peers_running : 0);
459 GNUNET_TESTING_daemons_churn (pg, "nse",
460 (peers_running > peers_next_round)
461 ? peers_running - peers_next_round
462 : 0,
463 (peers_next_round > peers_running)
464 ? peers_next_round - peers_running
465 : 0, wait_time, &churn_callback, NULL);
466 }
490} 467}
491 468
492 469
493static void 470static void
494nse_started_cb(void *cls, const char *emsg) 471nse_started_cb (void *cls, const char *emsg)
495{ 472{
496 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL); 473 GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
497 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_nse_peers, NULL); 474 disconnect_task =
475 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
498} 476}
499 477
500 478
501static void 479static void
502my_cb (void *cls, 480my_cb (void *cls, const char *emsg)
503 const char *emsg)
504{ 481{
505 char *buf; 482 char *buf;
506 int buf_len; 483 int buf_len;
484
507 if (emsg != NULL) 485 if (emsg != NULL)
508 { 486 {
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Peergroup callback called with error, aborting test!\n"); 488 "Peergroup callback called with error, aborting test!\n");
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n"); 489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
512 ok = 1; 490 ok = 1;
513 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); 491 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
514 return; 492 return;
515 } 493 }
516#if VERBOSE 494#if VERBOSE
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Peer Group started successfully, connecting to NSE service for each peer!\n"); 496 "Peer Group started successfully, connecting to NSE service for each peer!\n");
519#endif 497#endif
520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
521 "Have %u connections\n", 499 "Have %u connections\n", total_connections);
522 total_connections);
523 if (data_file != NULL) 500 if (data_file != NULL)
524 { 501 {
525 buf = NULL; 502 buf = NULL;
526 buf_len = GNUNET_asprintf(&buf, 503 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
527 "CONNECTIONS_0: %u\n", 504 if (buf_len > 0)
528 total_connections); 505 GNUNET_DISK_file_write (data_file, buf, buf_len);
529 if (buf_len > 0) 506 GNUNET_free (buf);
530 GNUNET_DISK_file_write(data_file, buf, buf_len); 507 }
531 GNUNET_free (buf); 508 peers_running = GNUNET_TESTING_daemons_running (pg);
532 }
533 peers_running = GNUNET_TESTING_daemons_running(pg);
534 GNUNET_TESTING_daemons_start_service (pg, 509 GNUNET_TESTING_daemons_start_service (pg,
535 "nse", 510 "nse",
536 wait_time, 511 wait_time, &nse_started_cb, NULL);
537 &nse_started_cb,
538 NULL);
539 512
540} 513}
541 514
@@ -554,16 +527,15 @@ my_cb (void *cls,
554 * @param second_daemon handle for the second daemon 527 * @param second_daemon handle for the second daemon
555 * @param emsg error message (NULL on success) 528 * @param emsg error message (NULL on success)
556 */ 529 */
557static void 530static void
558connect_cb (void *cls, 531connect_cb (void *cls,
559 const struct GNUNET_PeerIdentity *first, 532 const struct GNUNET_PeerIdentity *first,
560 const struct GNUNET_PeerIdentity *second, 533 const struct GNUNET_PeerIdentity *second,
561 uint32_t distance, 534 uint32_t distance,
562 const struct GNUNET_CONFIGURATION_Handle *first_cfg, 535 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
563 const struct GNUNET_CONFIGURATION_Handle *second_cfg, 536 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
564 struct GNUNET_TESTING_Daemon *first_daemon, 537 struct GNUNET_TESTING_Daemon *first_daemon,
565 struct GNUNET_TESTING_Daemon *second_daemon, 538 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
566 const char *emsg)
567{ 539{
568 if (emsg == NULL) 540 if (emsg == NULL)
569 total_connections++; 541 total_connections++;
@@ -580,109 +552,108 @@ run (void *cls,
580 struct GNUNET_TESTING_Host *hosts; 552 struct GNUNET_TESTING_Host *hosts;
581 553
582 ok = 1; 554 ok = 1;
583 testing_cfg = GNUNET_CONFIGURATION_create(); 555 testing_cfg = GNUNET_CONFIGURATION_create ();
584#if VERBOSE 556#if VERBOSE
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n"); 557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
586 GNUNET_CONFIGURATION_set_value_string (testing_cfg, 558 GNUNET_CONFIGURATION_set_value_string (testing_cfg,
587 "testing", 559 "testing", "use_progressbars", "YES");
588 "use_progressbars",
589 "YES");
590#endif 560#endif
591 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, 561 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
592 "testing", 562 "testing",
593 "num_peers", &num_peers)) 563 "num_peers",
594 { 564 &num_peers))
595 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option TESTING:NUM_PEERS is required!\n"); 565 {
596 return; 566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
597 } 567 "Option TESTING:NUM_PEERS is required!\n");
598 568 return;
599 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, 569 }
600 "nse-profiler", 570
601 "wait_time", 571 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
602 &temp_wait)) 572 "nse-profiler",
603 { 573 "wait_time",
604 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 574 &temp_wait))
605 "Option nse-profiler:wait_time is required!\n"); 575 {
606 return; 576 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
607 } 577 "Option nse-profiler:wait_time is required!\n");
608 578 return;
609 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, 579 }
610 "nse-profiler", "connection_limit", 580
611 &connection_limit)) 581 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
612 { 582 "nse-profiler",
613 connection_limit = 0; 583 "connection_limit",
614 } 584 &connection_limit))
615 585 {
616 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg, 586 connection_limit = 0;
617 "nse-profiler", "topology_output_file", 587 }
618 &topology_file)) 588
619 { 589 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
620 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 590 "nse-profiler",
621 "Option nse-profiler:topology_output_file is required!\n"); 591 "topology_output_file",
622 return; 592 &topology_file))
623 } 593 {
624 594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
625 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg, 595 "Option nse-profiler:topology_output_file is required!\n");
626 "nse-profiler", "data_output_file", 596 return;
627 &data_filename)) 597 }
628 { 598
629 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 599 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
630 "Option nse-profiler:data_output_file is required!\n"); 600 "nse-profiler",
631 return; 601 "data_output_file",
632 } 602 &data_filename))
633 603 {
634 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (testing_cfg, 604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
635 "nse-profiler", 605 "Option nse-profiler:data_output_file is required!\n");
636 "skew_clock")) 606 return;
637 { 607 }
638 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 608
639 "Setting our clock as skewed...\n"); 609 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (testing_cfg,
640 clock_skew = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, 610 "nse-profiler",
641 GNUNET_TIME_UNIT_MINUTES.rel_value); 611 "skew_clock"))
642 } 612 {
613 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Setting our clock as skewed...\n");
614 clock_skew = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
615 GNUNET_TIME_UNIT_MINUTES.rel_value);
616 }
643 617
644 618
645 data_file = GNUNET_DISK_file_open (data_filename, 619 data_file = GNUNET_DISK_file_open (data_filename,
646 GNUNET_DISK_OPEN_READWRITE 620 GNUNET_DISK_OPEN_READWRITE
647 | GNUNET_DISK_OPEN_CREATE, 621 | GNUNET_DISK_OPEN_CREATE,
648 GNUNET_DISK_PERM_USER_READ | 622 GNUNET_DISK_PERM_USER_READ |
649 GNUNET_DISK_PERM_USER_WRITE); 623 GNUNET_DISK_PERM_USER_WRITE);
650 if (data_file == NULL) 624 if (data_file == NULL)
651 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
652 "Failed to open %s for output!\n", 626 "Failed to open %s for output!\n", data_filename);
653 data_filename); 627 GNUNET_free (data_filename);
654 GNUNET_free(data_filename); 628
655 629 wait_time =
656 wait_time = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_wait); 630 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
657 631
658 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(cfg, 632 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg,
659 "nse-profiler", 633 "nse-profiler",
660 "output_file", 634 "output_file",
661 &temp_str)) 635 &temp_str))
662 { 636 {
663 output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE 637 output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE
664 | GNUNET_DISK_OPEN_CREATE, 638 | GNUNET_DISK_OPEN_CREATE,
665 GNUNET_DISK_PERM_USER_READ | 639 GNUNET_DISK_PERM_USER_READ |
666 GNUNET_DISK_PERM_USER_WRITE); 640 GNUNET_DISK_PERM_USER_WRITE);
667 if (output_file == NULL) 641 if (output_file == NULL)
668 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 642 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
669 "Failed to open %s for output!\n", 643 "Failed to open %s for output!\n", temp_str);
670 temp_str); 644 }
671 } 645 GNUNET_free_non_null (temp_str);
672 GNUNET_free_non_null(temp_str);
673 646
674 hosts = GNUNET_TESTING_hosts_load (testing_cfg); 647 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
675 648
676 pg = GNUNET_TESTING_peergroup_start(testing_cfg, 649 pg = GNUNET_TESTING_peergroup_start (testing_cfg,
677 num_peers, 650 num_peers,
678 TIMEOUT, 651 TIMEOUT,
679 &connect_cb, 652 &connect_cb, &my_cb, NULL, hosts);
680 &my_cb, NULL,
681 hosts);
682 GNUNET_assert (pg != NULL); 653 GNUNET_assert (pg != NULL);
683 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(), 654 shutdown_handle =
684 &shutdown_task, 655 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
685 NULL); 656 &shutdown_task, NULL);
686} 657}
687 658
688 659
@@ -709,10 +680,10 @@ main (int argc, char *argv[])
709#endif 680#endif
710 NULL); 681 NULL);
711 GNUNET_PROGRAM_run (argc, 682 GNUNET_PROGRAM_run (argc,
712 argv, "nse-profiler", 683 argv, "nse-profiler",
713 gettext_noop ("Measure quality and performance of the NSE service."), 684 gettext_noop
714 options, 685 ("Measure quality and performance of the NSE service."),
715 &run, NULL); 686 options, &run, NULL);
716#if REMOVE_DIR 687#if REMOVE_DIR
717 GNUNET_DISK_directory_remove ("/tmp/nse-profiler"); 688 GNUNET_DISK_directory_remove ("/tmp/nse-profiler");
718#endif 689#endif
diff --git a/src/nse/gnunet-service-nse.c b/src/nse/gnunet-service-nse.c
index 84a4ea9f0..9da9abba2 100644
--- a/src/nse/gnunet-service-nse.c
+++ b/src/nse/gnunet-service-nse.c
@@ -314,35 +314,33 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
314 sum = 0.0; 314 sum = 0.0;
315 sumweight = 0.0; 315 sumweight = 0.0;
316 for (i = 0; i < estimate_count; i++) 316 for (i = 0; i < estimate_count; i++)
317 { 317 {
318 val = htonl (size_estimate_messages[(estimate_index - i + HISTORY_SIZE) 318 val = htonl (size_estimate_messages[(estimate_index - i + HISTORY_SIZE)
319 % HISTORY_SIZE].matching_bits); 319 % HISTORY_SIZE].matching_bits);
320 weight = 1; /* was: estimate_count + 1 - i; */ 320 weight = 1; /* was: estimate_count + 1 - i; */
321 321
322 temp = weight + sumweight; 322 temp = weight + sumweight;
323 q = val - mean; 323 q = val - mean;
324 r = q * weight / temp; 324 r = q * weight / temp;
325 sum += sumweight * q * r; 325 sum += sumweight * q * r;
326 mean += r; 326 mean += r;
327 sumweight = temp; 327 sumweight = temp;
328 } 328 }
329 variance = sum / (sumweight - 1.0); 329 variance = sum / (sumweight - 1.0);
330 GNUNET_assert (variance >= 0); 330 GNUNET_assert (variance >= 0);
331 std_dev = sqrt (variance); 331 std_dev = sqrt (variance);
332 current_std_dev = std_dev; 332 current_std_dev = std_dev;
333 current_size_estimate = mean; 333 current_size_estimate = mean;
334 334
335 em->header.size 335 em->header.size = htons (sizeof (struct GNUNET_NSE_ClientMessage));
336 = htons (sizeof(struct GNUNET_NSE_ClientMessage)); 336 em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
337 em->header.type
338 = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
339 em->reserved = htonl (0); 337 em->reserved = htonl (0);
340 em->timestamp = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get()); 338 em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
341 em->size_estimate = mean - 1.0/3.0; 339 em->size_estimate = mean - 1.0 / 3.0;
342 em->std_deviation = std_dev; 340 em->std_deviation = std_dev;
343 GNUNET_STATISTICS_set (stats, 341 GNUNET_STATISTICS_set (stats,
344 "# nodes in the network (estimate)", 342 "# nodes in the network (estimate)",
345 (uint64_t) pow (2, mean - 1.0/3.0), GNUNET_NO); 343 (uint64_t) pow (2, mean - 1.0 / 3.0), GNUNET_NO);
346} 344}
347 345
348 346
@@ -357,18 +355,18 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
357 * @param message the message received 355 * @param message the message received
358 */ 356 */
359static void 357static void
360handle_start_message(void *cls, struct GNUNET_SERVER_Client *client, 358handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
361 const struct GNUNET_MessageHeader *message) 359 const struct GNUNET_MessageHeader *message)
362{ 360{
363 struct GNUNET_NSE_ClientMessage em; 361 struct GNUNET_NSE_ClientMessage em;
364 362
365#if DEBUG_NSE 363#if DEBUG_NSE
366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n");
367 "Received START message from client\n");
368#endif 365#endif
369 GNUNET_SERVER_notification_context_add (nc, client); 366 GNUNET_SERVER_notification_context_add (nc, client);
370 setup_estimate_message (&em); 367 setup_estimate_message (&em);
371 GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES); 368 GNUNET_SERVER_notification_context_unicast (nc, client, &em.header,
369 GNUNET_YES);
372 GNUNET_SERVER_receive_done (client, GNUNET_OK); 370 GNUNET_SERVER_receive_done (client, GNUNET_OK);
373} 371}
374 372
@@ -382,13 +380,15 @@ handle_start_message(void *cls, struct GNUNET_SERVER_Client *client,
382static double 380static double
383get_matching_bits_delay (uint32_t matching_bits) 381get_matching_bits_delay (uint32_t matching_bits)
384{ 382{
385 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p'))*/ 383 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p')) */
386 // S is next_timestamp (ignored in return value) 384 // S is next_timestamp (ignored in return value)
387 // f is frequency (gnunet_nse_interval) 385 // f is frequency (gnunet_nse_interval)
388 // x is matching_bits 386 // x is matching_bits
389 // p' is current_size_estimate 387 // p' is current_size_estimate
390 return ((double) gnunet_nse_interval.rel_value / (double) 2.0) 388 return ((double) gnunet_nse_interval.rel_value / (double) 2.0)
391 - ((gnunet_nse_interval.rel_value / M_PI) * atan (matching_bits - current_size_estimate)); 389 -
390 ((gnunet_nse_interval.rel_value / M_PI) *
391 atan (matching_bits - current_size_estimate));
392} 392}
393 393
394 394
@@ -398,7 +398,7 @@ get_matching_bits_delay (uint32_t matching_bits)
398 * @param matching_bits number of matching bits 398 * @param matching_bits number of matching bits
399 * @return random delay to apply 399 * @return random delay to apply
400 */ 400 */
401static struct GNUNET_TIME_Relative 401static struct GNUNET_TIME_Relative
402get_delay_randomization (uint32_t matching_bits) 402get_delay_randomization (uint32_t matching_bits)
403{ 403{
404#if USE_RANDOM_DELAYS 404#if USE_RANDOM_DELAYS
@@ -407,7 +407,11 @@ get_delay_randomization (uint32_t matching_bits)
407 if (matching_bits == 0) 407 if (matching_bits == 0)
408 return GNUNET_TIME_UNIT_ZERO; 408 return GNUNET_TIME_UNIT_ZERO;
409 ret.rel_value = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 409 ret.rel_value = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
410 (uint32_t) (get_matching_bits_delay (matching_bits - 1) / (double) (hop_count_max + 1))); 410 (uint32_t) (get_matching_bits_delay
411 (matching_bits -
412 1) /
413 (double) (hop_count_max
414 + 1)));
411 return ret; 415 return ret;
412#else 416#else
413 return GNUNET_TIME_UNIT_ZERO; 417 return GNUNET_TIME_UNIT_ZERO;
@@ -424,15 +428,13 @@ get_delay_randomization (uint32_t matching_bits)
424 */ 428 */
425static uint32_t 429static uint32_t
426get_matching_bits (struct GNUNET_TIME_Absolute timestamp, 430get_matching_bits (struct GNUNET_TIME_Absolute timestamp,
427 const struct GNUNET_PeerIdentity *id) 431 const struct GNUNET_PeerIdentity *id)
428{ 432{
429 GNUNET_HashCode timestamp_hash; 433 GNUNET_HashCode timestamp_hash;
430 434
431 GNUNET_CRYPTO_hash (&timestamp.abs_value, 435 GNUNET_CRYPTO_hash (&timestamp.abs_value,
432 sizeof(timestamp.abs_value), 436 sizeof (timestamp.abs_value), &timestamp_hash);
433 &timestamp_hash); 437 return GNUNET_CRYPTO_hash_matching_bits (&timestamp_hash, &id->hashPubKey);
434 return GNUNET_CRYPTO_hash_matching_bits (&timestamp_hash,
435 &id->hashPubKey);
436} 438}
437 439
438 440
@@ -453,38 +455,38 @@ get_transmit_delay (int round_offset)
453 uint32_t matching_bits; 455 uint32_t matching_bits;
454 456
455 switch (round_offset) 457 switch (round_offset)
456 { 458 {
457 case -1: 459 case -1:
458 /* previous round is randomized between 0 and 50 ms */ 460 /* previous round is randomized between 0 and 50 ms */
459#if USE_RANDOM_DELAYS 461#if USE_RANDOM_DELAYS
460 ret.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 462 ret.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 50);
461 50);
462#else 463#else
463 ret = GNUNET_TIME_UNIT_ZERO; 464 ret = GNUNET_TIME_UNIT_ZERO;
464#endif 465#endif
465#if DEBUG_NSE 466#if DEBUG_NSE
466 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "Transmitting previous round behind schedule in %llu ms\n", 468 "Transmitting previous round behind schedule in %llu ms\n",
468 (unsigned long long) ret.rel_value); 469 (unsigned long long) ret.rel_value);
469#endif 470#endif
470 return ret; 471 return ret;
471 case 0: 472 case 0:
472 /* current round is based on best-known matching_bits */ 473 /* current round is based on best-known matching_bits */
473 matching_bits = ntohl (size_estimate_messages[estimate_index].matching_bits); 474 matching_bits =
474 dist_delay = get_matching_bits_delay (matching_bits); 475 ntohl (size_estimate_messages[estimate_index].matching_bits);
475 dist_delay += get_delay_randomization (matching_bits).rel_value; 476 dist_delay = get_matching_bits_delay (matching_bits);
476 ret.rel_value = (uint64_t) dist_delay; 477 dist_delay += get_delay_randomization (matching_bits).rel_value;
478 ret.rel_value = (uint64_t) dist_delay;
477#if DEBUG_NSE 479#if DEBUG_NSE
478 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "For round %llu, delay for %u matching bits is %llu ms\n", 481 "For round %llu, delay for %u matching bits is %llu ms\n",
480 (unsigned long long) current_timestamp.abs_value, 482 (unsigned long long) current_timestamp.abs_value,
481 (unsigned int) matching_bits, 483 (unsigned int) matching_bits,
482 (unsigned long long) ret.rel_value); 484 (unsigned long long) ret.rel_value);
483#endif 485#endif
484 /* now consider round start time and add delay to it */ 486 /* now consider round start time and add delay to it */
485 tgt = GNUNET_TIME_absolute_add (current_timestamp, ret); 487 tgt = GNUNET_TIME_absolute_add (current_timestamp, ret);
486 return GNUNET_TIME_absolute_get_remaining (tgt); 488 return GNUNET_TIME_absolute_get_remaining (tgt);
487 } 489 }
488 GNUNET_break (0); 490 GNUNET_break (0);
489 return GNUNET_TIME_UNIT_FOREVER_REL; 491 return GNUNET_TIME_UNIT_FOREVER_REL;
490} 492}
@@ -497,8 +499,7 @@ get_transmit_delay (int round_offset)
497 * @param tc scheduler context 499 * @param tc scheduler context
498 */ 500 */
499static void 501static void
500transmit_task (void *cls, 502transmit_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
501 const struct GNUNET_SCHEDULER_TaskContext *tc);
502 503
503 504
504/** 505/**
@@ -518,52 +519,45 @@ transmit_ready (void *cls, size_t size, void *buf)
518 519
519 peer_entry->th = NULL; 520 peer_entry->th = NULL;
520 if (buf == NULL) 521 if (buf == NULL)
521 { 522 {
522 /* client disconnected */ 523 /* client disconnected */
523 return 0; 524 return 0;
524 } 525 }
525 GNUNET_assert (size >= sizeof (struct GNUNET_NSE_FloodMessage)); 526 GNUNET_assert (size >= sizeof (struct GNUNET_NSE_FloodMessage));
526 idx = estimate_index; 527 idx = estimate_index;
527 if (peer_entry->previous_round == GNUNET_NO) 528 if (peer_entry->previous_round == GNUNET_NO)
528 { 529 {
529 idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE; 530 idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE;
530 peer_entry->previous_round = GNUNET_YES; 531 peer_entry->previous_round = GNUNET_YES;
531 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0), 532 peer_entry->transmit_task =
532 &transmit_task, 533 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0), &transmit_task,
533 peer_entry); 534 peer_entry);
534 } 535 }
535 if ( (ntohl (size_estimate_messages[idx].hop_count) == 0) && 536 if ((ntohl (size_estimate_messages[idx].hop_count) == 0) &&
536 (GNUNET_SCHEDULER_NO_TASK != proof_task) ) 537 (GNUNET_SCHEDULER_NO_TASK != proof_task))
537 { 538 {
538 GNUNET_STATISTICS_update (stats, 539 GNUNET_STATISTICS_update (stats,
539 "# flood messages not generated (no proof yet)", 540 "# flood messages not generated (no proof yet)",
540 1, 541 1, GNUNET_NO);
541 GNUNET_NO); 542 return 0;
542 return 0; 543 }
543 }
544#if DEBUG_NSE 544#if DEBUG_NSE
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "In round %llu, sending to `%s' estimate with %u bits\n", 546 "In round %llu, sending to `%s' estimate with %u bits\n",
547 (unsigned long long) GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].timestamp).abs_value, 547 (unsigned long long)
548 GNUNET_i2s (&peer_entry->id), 548 GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].
549 (unsigned int) ntohl (size_estimate_messages[idx].matching_bits)); 549 timestamp).abs_value,
550 GNUNET_i2s (&peer_entry->id),
551 (unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
550#endif 552#endif
551 if (ntohl (size_estimate_messages[idx].hop_count) == 0) 553 if (ntohl (size_estimate_messages[idx].hop_count) == 0)
552 GNUNET_STATISTICS_update (stats, 554 GNUNET_STATISTICS_update (stats, "# flood messages started", 1, GNUNET_NO);
553 "# flood messages started", 555 GNUNET_STATISTICS_update (stats,
554 1, 556 "# flood messages transmitted", 1, GNUNET_NO);
555 GNUNET_NO);
556 GNUNET_STATISTICS_update (stats,
557 "# flood messages transmitted",
558 1,
559 GNUNET_NO);
560 memcpy (buf, 557 memcpy (buf,
561 &size_estimate_messages[idx], 558 &size_estimate_messages[idx],
562 sizeof (struct GNUNET_NSE_FloodMessage)); 559 sizeof (struct GNUNET_NSE_FloodMessage));
563 GNUNET_STATISTICS_update (stats, 560 GNUNET_STATISTICS_update (stats, "# flood messages sent", 1, GNUNET_NO);
564 "# flood messages sent",
565 1,
566 GNUNET_NO);
567 return sizeof (struct GNUNET_NSE_FloodMessage); 561 return sizeof (struct GNUNET_NSE_FloodMessage);
568} 562}
569 563
@@ -575,21 +569,21 @@ transmit_ready (void *cls, size_t size, void *buf)
575 * @param tc scheduler context 569 * @param tc scheduler context
576 */ 570 */
577static void 571static void
578transmit_task (void *cls, 572transmit_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
579 const struct GNUNET_SCHEDULER_TaskContext *tc)
580{ 573{
581 struct NSEPeerEntry *peer_entry = cls; 574 struct NSEPeerEntry *peer_entry = cls;
582 575
583 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK; 576 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
584 GNUNET_assert (NULL == peer_entry->th); 577 GNUNET_assert (NULL == peer_entry->th);
585 peer_entry->th 578 peer_entry->th
586 = GNUNET_CORE_notify_transmit_ready (coreAPI, 579 = GNUNET_CORE_notify_transmit_ready (coreAPI,
587 GNUNET_NO, 580 GNUNET_NO,
588 NSE_PRIORITY, 581 NSE_PRIORITY,
589 GNUNET_TIME_UNIT_FOREVER_REL, 582 GNUNET_TIME_UNIT_FOREVER_REL,
590 &peer_entry->id, 583 &peer_entry->id,
591 sizeof (struct GNUNET_NSE_FloodMessage), 584 sizeof (struct
592 &transmit_ready, peer_entry); 585 GNUNET_NSE_FloodMessage),
586 &transmit_ready, peer_entry);
593} 587}
594 588
595 589
@@ -605,9 +599,7 @@ update_network_size_estimate ()
605 struct GNUNET_NSE_ClientMessage em; 599 struct GNUNET_NSE_ClientMessage em;
606 600
607 setup_estimate_message (&em); 601 setup_estimate_message (&em);
608 GNUNET_SERVER_notification_context_broadcast (nc, 602 GNUNET_SERVER_notification_context_broadcast (nc, &em.header, GNUNET_YES);
609 &em.header,
610 GNUNET_YES);
611} 603}
612 604
613 605
@@ -619,29 +611,26 @@ update_network_size_estimate ()
619 * @param ts timestamp to use 611 * @param ts timestamp to use
620 */ 612 */
621static void 613static void
622setup_flood_message (unsigned int slot, 614setup_flood_message (unsigned int slot, struct GNUNET_TIME_Absolute ts)
623 struct GNUNET_TIME_Absolute ts)
624{ 615{
625 struct GNUNET_NSE_FloodMessage *fm; 616 struct GNUNET_NSE_FloodMessage *fm;
626 uint32_t matching_bits; 617 uint32_t matching_bits;
627 618
628 matching_bits = get_matching_bits (ts, &my_identity); 619 matching_bits = get_matching_bits (ts, &my_identity);
629 fm = &size_estimate_messages[slot]; 620 fm = &size_estimate_messages[slot];
630 fm->header.size = htons (sizeof(struct GNUNET_NSE_FloodMessage)); 621 fm->header.size = htons (sizeof (struct GNUNET_NSE_FloodMessage));
631 fm->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD); 622 fm->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD);
632 fm->hop_count = htonl (0); 623 fm->hop_count = htonl (0);
633 fm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_NSE_SEND); 624 fm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_NSE_SEND);
634 fm->purpose.size = htonl (sizeof(struct GNUNET_NSE_FloodMessage) 625 fm->purpose.size = htonl (sizeof (struct GNUNET_NSE_FloodMessage)
635 - sizeof (struct GNUNET_MessageHeader) 626 - sizeof (struct GNUNET_MessageHeader)
636 - sizeof (uint32_t) 627 - sizeof (uint32_t)
637 - sizeof (struct GNUNET_CRYPTO_RsaSignature)); 628 - sizeof (struct GNUNET_CRYPTO_RsaSignature));
638 fm->matching_bits = htonl (matching_bits); 629 fm->matching_bits = htonl (matching_bits);
639 fm->timestamp = GNUNET_TIME_absolute_hton (ts); 630 fm->timestamp = GNUNET_TIME_absolute_hton (ts);
640 fm->pkey = my_public_key; 631 fm->pkey = my_public_key;
641 fm->proof_of_work = my_proof; 632 fm->proof_of_work = my_proof;
642 GNUNET_CRYPTO_rsa_sign (my_private_key, 633 GNUNET_CRYPTO_rsa_sign (my_private_key, &fm->purpose, &fm->signature);
643 &fm->purpose,
644 &fm->signature);
645} 634}
646 635
647 636
@@ -655,27 +644,25 @@ setup_flood_message (unsigned int slot,
655 * @return GNUNET_OK (continue to iterate) 644 * @return GNUNET_OK (continue to iterate)
656 */ 645 */
657static int 646static int
658schedule_current_round (void *cls, 647schedule_current_round (void *cls, const GNUNET_HashCode * key, void *value)
659 const GNUNET_HashCode *key,
660 void *value)
661{ 648{
662 struct NSEPeerEntry *peer_entry = value; 649 struct NSEPeerEntry *peer_entry = value;
663 struct GNUNET_TIME_Relative delay; 650 struct GNUNET_TIME_Relative delay;
664 651
665 if (peer_entry->th != NULL) 652 if (peer_entry->th != NULL)
666 { 653 {
667 peer_entry->previous_round = GNUNET_NO; 654 peer_entry->previous_round = GNUNET_NO;
668 return GNUNET_OK; 655 return GNUNET_OK;
669 } 656 }
670 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK) 657 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
671 { 658 {
672 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task); 659 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
673 peer_entry->previous_round = GNUNET_NO; 660 peer_entry->previous_round = GNUNET_NO;
674 } 661 }
675 delay = get_transmit_delay ((peer_entry->previous_round == GNUNET_NO) ? -1 : 0); 662 delay =
676 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (delay, 663 get_transmit_delay ((peer_entry->previous_round == GNUNET_NO) ? -1 : 0);
677 &transmit_task, 664 peer_entry->transmit_task =
678 peer_entry); 665 GNUNET_SCHEDULER_add_delayed (delay, &transmit_task, peer_entry);
679 return GNUNET_OK; 666 return GNUNET_OK;
680} 667}
681 668
@@ -687,8 +674,7 @@ schedule_current_round (void *cls,
687 * @param tc context for this message 674 * @param tc context for this message
688 */ 675 */
689static void 676static void
690update_flood_message(void *cls, 677update_flood_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
691 const struct GNUNET_SCHEDULER_TaskContext *tc)
692{ 678{
693 struct GNUNET_TIME_Relative offset; 679 struct GNUNET_TIME_Relative offset;
694 unsigned int i; 680 unsigned int i;
@@ -696,40 +682,38 @@ update_flood_message(void *cls,
696 flood_task = GNUNET_SCHEDULER_NO_TASK; 682 flood_task = GNUNET_SCHEDULER_NO_TASK;
697 offset = GNUNET_TIME_absolute_get_remaining (next_timestamp); 683 offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
698 if (0 != offset.rel_value) 684 if (0 != offset.rel_value)
699 { 685 {
700 /* somehow run early, delay more */ 686 /* somehow run early, delay more */
701 flood_task 687 flood_task
702 = GNUNET_SCHEDULER_add_delayed (offset, 688 = GNUNET_SCHEDULER_add_delayed (offset, &update_flood_message, NULL);
703 &update_flood_message, 689 return;
704 NULL); 690 }
705 return;
706 }
707 current_timestamp = next_timestamp; 691 current_timestamp = next_timestamp;
708 next_timestamp = GNUNET_TIME_absolute_add (current_timestamp, 692 next_timestamp = GNUNET_TIME_absolute_add (current_timestamp,
709 gnunet_nse_interval); 693 gnunet_nse_interval);
710 estimate_index = (estimate_index + 1) % HISTORY_SIZE; 694 estimate_index = (estimate_index + 1) % HISTORY_SIZE;
711 if (estimate_count < HISTORY_SIZE) 695 if (estimate_count < HISTORY_SIZE)
712 estimate_count++; 696 estimate_count++;
713 if (next_timestamp.abs_value == 697 if (next_timestamp.abs_value ==
714 GNUNET_TIME_absolute_ntoh (next_message.timestamp).abs_value) 698 GNUNET_TIME_absolute_ntoh (next_message.timestamp).abs_value)
715 { 699 {
716 /* we received a message for this round way early, use it! */ 700 /* we received a message for this round way early, use it! */
717 size_estimate_messages[estimate_index] = next_message; 701 size_estimate_messages[estimate_index] = next_message;
718 size_estimate_messages[estimate_index].hop_count 702 size_estimate_messages[estimate_index].hop_count
719 = htonl (1 + ntohl (next_message.hop_count)); 703 = htonl (1 + ntohl (next_message.hop_count));
720 } 704 }
721 else 705 else
722 setup_flood_message (estimate_index, current_timestamp); 706 setup_flood_message (estimate_index, current_timestamp);
723 next_message.matching_bits = htonl (0); /* reset for 'next' round */ 707 next_message.matching_bits = htonl (0); /* reset for 'next' round */
724 hop_count_max = 0; 708 hop_count_max = 0;
725 for (i = 0; i < HISTORY_SIZE; i++) 709 for (i = 0; i < HISTORY_SIZE; i++)
726 hop_count_max = GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count), 710 hop_count_max = GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count),
727 hop_count_max); 711 hop_count_max);
728 GNUNET_CONTAINER_multihashmap_iterate (peers, 712 GNUNET_CONTAINER_multihashmap_iterate (peers, &schedule_current_round, NULL);
729 &schedule_current_round, 713 flood_task =
730 NULL); 714 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
731 flood_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_timestamp), 715 (next_timestamp), &update_flood_message,
732 &update_flood_message, NULL); 716 NULL);
733} 717}
734 718
735 719
@@ -739,13 +723,13 @@ update_flood_message(void *cls,
739 * @param hash 723 * @param hash
740 * @return the number of leading zero bits. 724 * @return the number of leading zero bits.
741 */ 725 */
742static unsigned int 726static unsigned int
743count_leading_zeroes(const GNUNET_HashCode *hash) 727count_leading_zeroes (const GNUNET_HashCode * hash)
744{ 728{
745 unsigned int hash_count; 729 unsigned int hash_count;
746 730
747 hash_count = 0; 731 hash_count = 0;
748 while ((0 == GNUNET_CRYPTO_hash_get_bit(hash, hash_count))) 732 while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
749 hash_count++; 733 hash_count++;
750 return hash_count; 734 return hash_count;
751} 735}
@@ -761,20 +745,19 @@ count_leading_zeroes(const GNUNET_HashCode *hash)
761 * @return GNUNET_YES if valid, GNUNET_NO if not 745 * @return GNUNET_YES if valid, GNUNET_NO if not
762 */ 746 */
763static int 747static int
764check_proof_of_work(const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey, 748check_proof_of_work (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
765 uint64_t val) 749 uint64_t val)
766{ 750{
767 char buf[sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sizeof(val)]; 751 char buf[sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
752 sizeof (val)];
768 GNUNET_HashCode result; 753 GNUNET_HashCode result;
769 754
770 memcpy (buf, 755 memcpy (buf, &val, sizeof (val));
771 &val, 756 memcpy (&buf[sizeof (val)],
772 sizeof (val)); 757 pkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
773 memcpy (&buf[sizeof(val)],
774 pkey,
775 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
776 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result); 758 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
777 return (count_leading_zeroes (&result) >= nse_work_required) ? GNUNET_YES : GNUNET_NO; 759 return (count_leading_zeroes (&result) >=
760 nse_work_required) ? GNUNET_YES : GNUNET_NO;
778} 761}
779 762
780 763
@@ -786,19 +769,16 @@ write_proof ()
786{ 769{
787 char *proof; 770 char *proof;
788 771
789 if (GNUNET_OK != 772 if (GNUNET_OK !=
790 GNUNET_CONFIGURATION_get_value_filename (cfg, 773 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
791 "NSE", "PROOFFILE", 774 return;
792 &proof))
793 return;
794 if (sizeof (my_proof) != 775 if (sizeof (my_proof) !=
795 GNUNET_DISK_fn_write (proof, 776 GNUNET_DISK_fn_write (proof,
796 &my_proof, 777 &my_proof,
797 sizeof (my_proof), 778 sizeof (my_proof),
798 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE)) 779 GNUNET_DISK_PERM_USER_READ |
799 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 780 GNUNET_DISK_PERM_USER_WRITE))
800 "write", 781 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", proof);
801 proof);
802 GNUNET_free (proof); 782 GNUNET_free (proof);
803 783
804} 784}
@@ -811,67 +791,64 @@ write_proof ()
811 * @param tc task context 791 * @param tc task context
812 */ 792 */
813static void 793static void
814find_proof (void *cls, 794find_proof (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
815 const struct GNUNET_SCHEDULER_TaskContext *tc)
816{ 795{
817#define ROUND_SIZE 10 796#define ROUND_SIZE 10
818 uint64_t counter; 797 uint64_t counter;
819 char buf[sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sizeof(uint64_t)]; 798 char buf[sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
799 sizeof (uint64_t)];
820 GNUNET_HashCode result; 800 GNUNET_HashCode result;
821 unsigned int i; 801 unsigned int i;
822 802
823 proof_task = GNUNET_SCHEDULER_NO_TASK; 803 proof_task = GNUNET_SCHEDULER_NO_TASK;
824 memcpy (&buf[sizeof(uint64_t)], 804 memcpy (&buf[sizeof (uint64_t)],
825 &my_public_key, 805 &my_public_key,
826 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)); 806 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
827 i = 0; 807 i = 0;
828 counter = my_proof; 808 counter = my_proof;
829 while ( (counter != UINT64_MAX) && (i < ROUND_SIZE) ) 809 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
810 {
811 memcpy (buf, &counter, sizeof (uint64_t));
812 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
813 if (nse_work_required <= count_leading_zeroes (&result))
830 { 814 {
831 memcpy (buf, 815 my_proof = counter;
832 &counter,
833 sizeof(uint64_t));
834 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
835 if (nse_work_required <= count_leading_zeroes(&result))
836 {
837 my_proof = counter;
838#if DEBUG_NSE 816#if DEBUG_NSE
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840 "Proof of work found: %llu!\n", 818 "Proof of work found: %llu!\n",
841 (unsigned long long) GNUNET_ntohll (counter)); 819 (unsigned long long) GNUNET_ntohll (counter));
842#endif 820#endif
843 for (i=0;i<HISTORY_SIZE;i++) 821 for (i = 0; i < HISTORY_SIZE; i++)
844 if (ntohl (size_estimate_messages[i].hop_count) == 0) 822 if (ntohl (size_estimate_messages[i].hop_count) == 0)
845 { 823 {
846 size_estimate_messages[i].proof_of_work = my_proof; 824 size_estimate_messages[i].proof_of_work = my_proof;
847 GNUNET_CRYPTO_rsa_sign (my_private_key, 825 GNUNET_CRYPTO_rsa_sign (my_private_key,
848 &size_estimate_messages[i].purpose, 826 &size_estimate_messages[i].purpose,
849 &size_estimate_messages[i].signature); 827 &size_estimate_messages[i].signature);
850 } 828 }
851 write_proof (); 829 write_proof ();
852 return; 830 return;
853 }
854 counter++;
855 i++;
856 } 831 }
832 counter++;
833 i++;
834 }
857 if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE)) 835 if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
858 { 836 {
859#if DEBUG_NSE 837#if DEBUG_NSE
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Testing proofs currently at %llu\n", 839 "Testing proofs currently at %llu\n",
862 (unsigned long long) counter); 840 (unsigned long long) counter);
863#endif 841#endif
864 /* remember progress every 100 rounds */ 842 /* remember progress every 100 rounds */
865 my_proof = counter; 843 my_proof = counter;
866 write_proof (); 844 write_proof ();
867 } 845 }
868 else 846 else
869 { 847 {
870 my_proof = counter; 848 my_proof = counter;
871 } 849 }
872 proof_task = GNUNET_SCHEDULER_add_delayed (proof_find_delay, 850 proof_task = GNUNET_SCHEDULER_add_delayed (proof_find_delay,
873 &find_proof, 851 &find_proof, NULL);
874 NULL);
875} 852}
876 853
877 854
@@ -885,28 +862,29 @@ find_proof (void *cls,
885 * @return GNUNET_YES if the message is verified 862 * @return GNUNET_YES if the message is verified
886 * GNUNET_NO if the key/signature don't verify 863 * GNUNET_NO if the key/signature don't verify
887 */ 864 */
888static int 865static int
889verify_message_crypto(const struct GNUNET_NSE_FloodMessage *incoming_flood) 866verify_message_crypto (const struct GNUNET_NSE_FloodMessage *incoming_flood)
890{ 867{
891 if (GNUNET_YES != 868 if (GNUNET_YES !=
892 check_proof_of_work (&incoming_flood->pkey, 869 check_proof_of_work (&incoming_flood->pkey,
893 incoming_flood->proof_of_work)) 870 incoming_flood->proof_of_work))
894 { 871 {
895 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 872 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
896 _("Proof of work invalid: %llu!\n"), 873 _("Proof of work invalid: %llu!\n"),
897 (unsigned long long) GNUNET_ntohll (incoming_flood->proof_of_work)); 874 (unsigned long long)
898 GNUNET_break_op (0); 875 GNUNET_ntohll (incoming_flood->proof_of_work));
899 return GNUNET_NO; 876 GNUNET_break_op (0);
900 } 877 return GNUNET_NO;
901 if (GNUNET_OK != 878 }
879 if (GNUNET_OK !=
902 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND, 880 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
903 &incoming_flood->purpose, 881 &incoming_flood->purpose,
904 &incoming_flood->signature, 882 &incoming_flood->signature,
905 &incoming_flood->pkey)) 883 &incoming_flood->pkey))
906 { 884 {
907 GNUNET_break_op (0); 885 GNUNET_break_op (0);
908 return GNUNET_NO; 886 return GNUNET_NO;
909 } 887 }
910 return GNUNET_YES; 888 return GNUNET_YES;
911} 889}
912 890
@@ -921,35 +899,33 @@ verify_message_crypto(const struct GNUNET_NSE_FloodMessage *incoming_flood)
921 * @return GNUNET_OK (continue to iterate) 899 * @return GNUNET_OK (continue to iterate)
922 */ 900 */
923static int 901static int
924update_flood_times (void *cls, 902update_flood_times (void *cls, const GNUNET_HashCode * key, void *value)
925 const GNUNET_HashCode *key,
926 void *value)
927{ 903{
928 struct NSEPeerEntry *exclude = cls; 904 struct NSEPeerEntry *exclude = cls;
929 struct NSEPeerEntry *peer_entry = value; 905 struct NSEPeerEntry *peer_entry = value;
930 struct GNUNET_TIME_Relative delay; 906 struct GNUNET_TIME_Relative delay;
931 907
932 if (peer_entry->th != NULL) 908 if (peer_entry->th != NULL)
933 return GNUNET_OK; /* already active */ 909 return GNUNET_OK; /* already active */
934 if (peer_entry == exclude) 910 if (peer_entry == exclude)
935 return GNUNET_OK; /* trigger of the update */ 911 return GNUNET_OK; /* trigger of the update */
936 if (peer_entry->previous_round == GNUNET_NO) 912 if (peer_entry->previous_round == GNUNET_NO)
937 { 913 {
938 /* still stuck in previous round, no point to update, check that 914 /* still stuck in previous round, no point to update, check that
939 we are active here though... */ 915 * we are active here though... */
940 GNUNET_break ( (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK) || 916 GNUNET_break ((peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK) ||
941 (peer_entry->th != NULL) ); 917 (peer_entry->th != NULL));
942 return GNUNET_OK; 918 return GNUNET_OK;
943 } 919 }
944 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK) 920 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
945 { 921 {
946 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task); 922 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
947 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK; 923 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
948 } 924 }
949 delay = get_transmit_delay (0); 925 delay = get_transmit_delay (0);
950 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (delay, 926 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (delay,
951 &transmit_task, 927 &transmit_task,
952 peer_entry); 928 peer_entry);
953 return GNUNET_OK; 929 return GNUNET_OK;
954} 930}
955 931
@@ -964,15 +940,15 @@ update_flood_times (void *cls,
964 * 940 *
965 */ 941 */
966static int 942static int
967handle_p2p_size_estimate(void *cls, 943handle_p2p_size_estimate (void *cls,
968 const struct GNUNET_PeerIdentity *peer, 944 const struct GNUNET_PeerIdentity *peer,
969 const struct GNUNET_MessageHeader *message, 945 const struct GNUNET_MessageHeader *message,
970 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 946 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
971{ 947{
972 const struct GNUNET_NSE_FloodMessage *incoming_flood; 948 const struct GNUNET_NSE_FloodMessage *incoming_flood;
973 struct GNUNET_TIME_Absolute ts; 949 struct GNUNET_TIME_Absolute ts;
974 struct NSEPeerEntry *peer_entry; 950 struct NSEPeerEntry *peer_entry;
975 uint32_t matching_bits; 951 uint32_t matching_bits;
976 unsigned int idx; 952 unsigned int idx;
977 953
978#if ENABLE_HISTOGRAM 954#if ENABLE_HISTOGRAM
@@ -980,10 +956,7 @@ handle_p2p_size_estimate(void *cls,
980 GNUNET_BIO_write_int64 (wh, GNUNET_TIME_absolute_get ().abs_value); 956 GNUNET_BIO_write_int64 (wh, GNUNET_TIME_absolute_get ().abs_value);
981#endif 957#endif
982 incoming_flood = (const struct GNUNET_NSE_FloodMessage *) message; 958 incoming_flood = (const struct GNUNET_NSE_FloodMessage *) message;
983 GNUNET_STATISTICS_update (stats, 959 GNUNET_STATISTICS_update (stats, "# flood messages received", 1, GNUNET_NO);
984 "# flood messages received",
985 1,
986 GNUNET_NO);
987 matching_bits = ntohl (incoming_flood->matching_bits); 960 matching_bits = ntohl (incoming_flood->matching_bits);
988#if DEBUG_NSE 961#if DEBUG_NSE
989 { 962 {
@@ -992,133 +965,124 @@ handle_p2p_size_estimate(void *cls,
992 struct GNUNET_PeerIdentity os; 965 struct GNUNET_PeerIdentity os;
993 966
994 GNUNET_CRYPTO_hash (&incoming_flood->pkey, 967 GNUNET_CRYPTO_hash (&incoming_flood->pkey,
995 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 968 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
996 &os.hashPubKey); 969 &os.hashPubKey);
997 GNUNET_snprintf (origin, sizeof (origin), 970 GNUNET_snprintf (origin, sizeof (origin), "%s", GNUNET_i2s (&os));
998 "%s", 971 GNUNET_snprintf (pred, sizeof (pred), "%s", GNUNET_i2s (peer));
999 GNUNET_i2s (&os));
1000 GNUNET_snprintf (pred, sizeof (pred),
1001 "%s",
1002 GNUNET_i2s (peer));
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "Flood at %llu from `%s' via `%s' at `%s' with bits %u\n", 973 "Flood at %llu from `%s' via `%s' at `%s' with bits %u\n",
1005 (unsigned long long) GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp).abs_value, 974 (unsigned long long)
1006 origin, 975 GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp).abs_value,
1007 pred, 976 origin, pred, GNUNET_i2s (&my_identity),
1008 GNUNET_i2s (&my_identity), 977 (unsigned int) matching_bits);
1009 (unsigned int) matching_bits);
1010 } 978 }
1011#endif 979#endif
1012 980
1013 peer_entry = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey); 981 peer_entry = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1014 if (NULL == peer_entry) 982 if (NULL == peer_entry)
1015 { 983 {
1016 GNUNET_break (0); 984 GNUNET_break (0);
1017 return GNUNET_OK; 985 return GNUNET_OK;
1018 } 986 }
1019 987
1020 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp); 988 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
1021 989
1022 if (ts.abs_value == current_timestamp.abs_value) 990 if (ts.abs_value == current_timestamp.abs_value)
1023 idx = estimate_index; 991 idx = estimate_index;
1024 else if (ts.abs_value == current_timestamp.abs_value - gnunet_nse_interval.rel_value) 992 else if (ts.abs_value ==
993 current_timestamp.abs_value - gnunet_nse_interval.rel_value)
1025 idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE; 994 idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1026 else if (ts.abs_value == next_timestamp.abs_value - gnunet_nse_interval.rel_value) 995 else if (ts.abs_value ==
996 next_timestamp.abs_value - gnunet_nse_interval.rel_value)
997 {
998 if (matching_bits <= ntohl (next_message.matching_bits))
999 return GNUNET_OK; /* ignore, simply too early/late */
1000 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1027 { 1001 {
1028 if (matching_bits <= ntohl (next_message.matching_bits)) 1002 GNUNET_break_op (0);
1029 return GNUNET_OK; /* ignore, simply too early/late */
1030 if (GNUNET_YES !=
1031 verify_message_crypto (incoming_flood))
1032 {
1033 GNUNET_break_op (0);
1034 return GNUNET_OK;
1035 }
1036 next_message = *incoming_flood;
1037 return GNUNET_OK; 1003 return GNUNET_OK;
1038 } 1004 }
1005 next_message = *incoming_flood;
1006 return GNUNET_OK;
1007 }
1039 else 1008 else
1009 {
1010 GNUNET_STATISTICS_update (stats,
1011 "# flood messages discarded (clock skew too large)",
1012 1, GNUNET_NO);
1013 return GNUNET_OK;
1014 }
1015 if (0 == (memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity))))
1016 {
1017 /* send to self, update our own estimate IF this also comes from us! */
1018 if (0 == memcmp (&incoming_flood->pkey,
1019 &my_public_key, sizeof (my_public_key)))
1020 update_network_size_estimate ();
1021 return GNUNET_OK;
1022 }
1023 if (matching_bits >= ntohl (size_estimate_messages[idx].matching_bits))
1024 {
1025 /* cancel transmission from us to this peer for this round */
1026 if (idx == estimate_index)
1040 { 1027 {
1041 GNUNET_STATISTICS_update (stats, 1028 if (peer_entry->previous_round == GNUNET_YES)
1042 "# flood messages discarded (clock skew too large)", 1029 {
1043 1, 1030 /* cancel any activity for current round */
1044 GNUNET_NO); 1031 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1045 return GNUNET_OK; 1032 {
1033 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1034 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
1035 }
1036 if (peer_entry->th != NULL)
1037 {
1038 GNUNET_CORE_notify_transmit_ready_cancel (peer_entry->th);
1039 peer_entry->th = NULL;
1040 }
1041 }
1046 } 1042 }
1047 if (0 == (memcmp (peer, &my_identity, sizeof(struct GNUNET_PeerIdentity)))) 1043 else
1048 { 1044 {
1049 /* send to self, update our own estimate IF this also comes from us! */ 1045 /* cancel previous round only */
1050 if (0 == memcmp (&incoming_flood->pkey, 1046 peer_entry->previous_round = GNUNET_YES;
1051 &my_public_key,
1052 sizeof (my_public_key)))
1053 update_network_size_estimate ();
1054 return GNUNET_OK;
1055 }
1056 if (matching_bits >= ntohl (size_estimate_messages[idx].matching_bits))
1057 {
1058 /* cancel transmission from us to this peer for this round */
1059 if (idx == estimate_index)
1060 {
1061 if (peer_entry->previous_round == GNUNET_YES)
1062 {
1063 /* cancel any activity for current round */
1064 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1065 {
1066 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1067 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
1068 }
1069 if (peer_entry->th != NULL)
1070 {
1071 GNUNET_CORE_notify_transmit_ready_cancel (peer_entry->th);
1072 peer_entry->th = NULL;
1073 }
1074 }
1075 }
1076 else
1077 {
1078 /* cancel previous round only */
1079 peer_entry->previous_round = GNUNET_YES;
1080 }
1081 } 1047 }
1048 }
1082 if (matching_bits == ntohl (size_estimate_messages[idx].matching_bits)) 1049 if (matching_bits == ntohl (size_estimate_messages[idx].matching_bits))
1083 return GNUNET_OK; 1050 return GNUNET_OK;
1084 if (matching_bits <= ntohl (size_estimate_messages[idx].matching_bits)) 1051 if (matching_bits <= ntohl (size_estimate_messages[idx].matching_bits))
1085 { 1052 {
1086 if ( (idx < estimate_index) && 1053 if ((idx < estimate_index) && (peer_entry->previous_round == GNUNET_YES))
1087 (peer_entry->previous_round == GNUNET_YES) ) 1054 peer_entry->previous_round = GNUNET_NO;
1088 peer_entry->previous_round = GNUNET_NO; 1055 /* push back our result now, that peer is spreading bad information... */
1089 /* push back our result now, that peer is spreading bad information... */ 1056 if (NULL == peer_entry->th)
1090 if (NULL == peer_entry->th)
1091 {
1092 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1093 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1094 peer_entry->transmit_task = GNUNET_SCHEDULER_add_now (&transmit_task,
1095 peer_entry);
1096 }
1097 /* Not closer than our most recent message, no need to do work here */
1098 GNUNET_STATISTICS_update (stats,
1099 "# flood messages ignored (had closer already)",
1100 1,
1101 GNUNET_NO);
1102 return GNUNET_OK;
1103 }
1104 if (GNUNET_YES !=
1105 verify_message_crypto (incoming_flood))
1106 { 1057 {
1107 GNUNET_break_op (0); 1058 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1108 return GNUNET_OK; 1059 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1060 peer_entry->transmit_task = GNUNET_SCHEDULER_add_now (&transmit_task,
1061 peer_entry);
1109 } 1062 }
1063 /* Not closer than our most recent message, no need to do work here */
1064 GNUNET_STATISTICS_update (stats,
1065 "# flood messages ignored (had closer already)",
1066 1, GNUNET_NO);
1067 return GNUNET_OK;
1068 }
1069 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1070 {
1071 GNUNET_break_op (0);
1072 return GNUNET_OK;
1073 }
1110 size_estimate_messages[idx] = *incoming_flood; 1074 size_estimate_messages[idx] = *incoming_flood;
1111 size_estimate_messages[idx].hop_count = htonl (ntohl (incoming_flood->hop_count) + 1); 1075 size_estimate_messages[idx].hop_count =
1112 hop_count_max = GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1, 1076 htonl (ntohl (incoming_flood->hop_count) + 1);
1113 hop_count_max); 1077 hop_count_max =
1078 GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1, hop_count_max);
1114 1079
1115 /* have a new, better size estimate, inform clients */ 1080 /* have a new, better size estimate, inform clients */
1116 update_network_size_estimate (); 1081 update_network_size_estimate ();
1117 1082
1118 /* flood to rest */ 1083 /* flood to rest */
1119 GNUNET_CONTAINER_multihashmap_iterate (peers, 1084 GNUNET_CONTAINER_multihashmap_iterate (peers,
1120 &update_flood_times, 1085 &update_flood_times, peer_entry);
1121 peer_entry);
1122 return GNUNET_OK; 1086 return GNUNET_OK;
1123} 1087}
1124 1088
@@ -1132,25 +1096,24 @@ handle_p2p_size_estimate(void *cls,
1132 * @param atsi performance data 1096 * @param atsi performance data
1133 */ 1097 */
1134static void 1098static void
1135handle_core_connect(void *cls, const struct GNUNET_PeerIdentity *peer, 1099handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
1136 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 1100 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1137{ 1101{
1138 struct NSEPeerEntry *peer_entry; 1102 struct NSEPeerEntry *peer_entry;
1139 1103
1140 #if DEBUG_NSE 1104#if DEBUG_NSE
1141 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Peer `%s' connected to us\n", 1106 "Peer `%s' connected to us\n", GNUNET_i2s (peer));
1143 GNUNET_i2s (peer));
1144#endif 1107#endif
1145 peer_entry = GNUNET_malloc(sizeof(struct NSEPeerEntry)); 1108 peer_entry = GNUNET_malloc (sizeof (struct NSEPeerEntry));
1146 peer_entry->id = *peer; 1109 peer_entry->id = *peer;
1147 GNUNET_CONTAINER_multihashmap_put (peers, 1110 GNUNET_CONTAINER_multihashmap_put (peers,
1148 &peer->hashPubKey, 1111 &peer->hashPubKey,
1149 peer_entry, 1112 peer_entry,
1150 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1151 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1), 1114 peer_entry->transmit_task =
1152 &transmit_task, 1115 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1), &transmit_task,
1153 peer_entry); 1116 peer_entry);
1154} 1117}
1155 1118
1156 1119
@@ -1161,34 +1124,31 @@ handle_core_connect(void *cls, const struct GNUNET_PeerIdentity *peer,
1161 * @param peer peer identity this notification is about 1124 * @param peer peer identity this notification is about
1162 */ 1125 */
1163static void 1126static void
1164handle_core_disconnect(void *cls, const struct GNUNET_PeerIdentity *peer) 1127handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1165{ 1128{
1166 struct NSEPeerEntry *pos; 1129 struct NSEPeerEntry *pos;
1167 1130
1168 #if DEBUG_NSE 1131#if DEBUG_NSE
1169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Peer `%s' disconnected from us\n", 1133 "Peer `%s' disconnected from us\n", GNUNET_i2s (peer));
1171 GNUNET_i2s (peer));
1172#endif 1134#endif
1173 pos = GNUNET_CONTAINER_multihashmap_get (peers, 1135 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1174 &peer->hashPubKey);
1175 if (NULL == pos) 1136 if (NULL == pos)
1176 { 1137 {
1177 GNUNET_break (0); 1138 GNUNET_break (0);
1178 return; 1139 return;
1179 } 1140 }
1180 GNUNET_assert (GNUNET_YES == 1141 GNUNET_assert (GNUNET_YES ==
1181 GNUNET_CONTAINER_multihashmap_remove (peers, 1142 GNUNET_CONTAINER_multihashmap_remove (peers,
1182 &peer->hashPubKey, 1143 &peer->hashPubKey, pos));
1183 pos));
1184 if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK) 1144 if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1185 GNUNET_SCHEDULER_cancel (pos->transmit_task); 1145 GNUNET_SCHEDULER_cancel (pos->transmit_task);
1186 if (pos->th != NULL) 1146 if (pos->th != NULL)
1187 { 1147 {
1188 GNUNET_CORE_notify_transmit_ready_cancel (pos->th); 1148 GNUNET_CORE_notify_transmit_ready_cancel (pos->th);
1189 pos->th = NULL; 1149 pos->th = NULL;
1190 } 1150 }
1191 GNUNET_free(pos); 1151 GNUNET_free (pos);
1192} 1152}
1193 1153
1194 1154
@@ -1199,51 +1159,50 @@ handle_core_disconnect(void *cls, const struct GNUNET_PeerIdentity *peer)
1199 * @param tc unused 1159 * @param tc unused
1200 */ 1160 */
1201static void 1161static void
1202shutdown_task(void *cls, 1162shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1203 const struct GNUNET_SCHEDULER_TaskContext *tc)
1204{ 1163{
1205 if (flood_task != GNUNET_SCHEDULER_NO_TASK) 1164 if (flood_task != GNUNET_SCHEDULER_NO_TASK)
1206 { 1165 {
1207 GNUNET_SCHEDULER_cancel (flood_task); 1166 GNUNET_SCHEDULER_cancel (flood_task);
1208 flood_task = GNUNET_SCHEDULER_NO_TASK; 1167 flood_task = GNUNET_SCHEDULER_NO_TASK;
1209 } 1168 }
1210 if (proof_task != GNUNET_SCHEDULER_NO_TASK) 1169 if (proof_task != GNUNET_SCHEDULER_NO_TASK)
1211 { 1170 {
1212 GNUNET_SCHEDULER_cancel (proof_task); 1171 GNUNET_SCHEDULER_cancel (proof_task);
1213 proof_task = GNUNET_SCHEDULER_NO_TASK; 1172 proof_task = GNUNET_SCHEDULER_NO_TASK;
1214 write_proof (); /* remember progress */ 1173 write_proof (); /* remember progress */
1215 } 1174 }
1216 if (nc != NULL) 1175 if (nc != NULL)
1217 { 1176 {
1218 GNUNET_SERVER_notification_context_destroy (nc); 1177 GNUNET_SERVER_notification_context_destroy (nc);
1219 nc = NULL; 1178 nc = NULL;
1220 } 1179 }
1221 if (coreAPI != NULL) 1180 if (coreAPI != NULL)
1222 { 1181 {
1223 GNUNET_CORE_disconnect (coreAPI); 1182 GNUNET_CORE_disconnect (coreAPI);
1224 coreAPI = NULL; 1183 coreAPI = NULL;
1225 } 1184 }
1226 if (stats != NULL) 1185 if (stats != NULL)
1227 { 1186 {
1228 GNUNET_STATISTICS_destroy (stats, GNUNET_NO); 1187 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1229 stats = NULL; 1188 stats = NULL;
1230 } 1189 }
1231 if (peers != NULL) 1190 if (peers != NULL)
1232 { 1191 {
1233 GNUNET_CONTAINER_multihashmap_destroy (peers); 1192 GNUNET_CONTAINER_multihashmap_destroy (peers);
1234 peers = NULL; 1193 peers = NULL;
1235 } 1194 }
1236 if (my_private_key != NULL) 1195 if (my_private_key != NULL)
1237 { 1196 {
1238 GNUNET_CRYPTO_rsa_key_free (my_private_key); 1197 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1239 my_private_key = NULL; 1198 my_private_key = NULL;
1240 } 1199 }
1241#if ENABLE_HISTOGRAM 1200#if ENABLE_HISTOGRAM
1242 if (wh != NULL) 1201 if (wh != NULL)
1243 { 1202 {
1244 GNUNET_BIO_write_close (wh); 1203 GNUNET_BIO_write_close (wh);
1245 wh = NULL; 1204 wh = NULL;
1246 } 1205 }
1247#endif 1206#endif
1248} 1207}
1249 1208
@@ -1258,37 +1217,45 @@ shutdown_task(void *cls,
1258 */ 1217 */
1259static void 1218static void
1260core_init (void *cls, struct GNUNET_CORE_Handle *server, 1219core_init (void *cls, struct GNUNET_CORE_Handle *server,
1261 const struct GNUNET_PeerIdentity *identity, 1220 const struct GNUNET_PeerIdentity *identity,
1262 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey) 1221 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1263{ 1222{
1264 struct GNUNET_TIME_Absolute now; 1223 struct GNUNET_TIME_Absolute now;
1265 struct GNUNET_TIME_Absolute prev_time; 1224 struct GNUNET_TIME_Absolute prev_time;
1266 unsigned int i; 1225 unsigned int i;
1267 1226
1268 if (server == NULL) 1227 if (server == NULL)
1269 { 1228 {
1270#if DEBUG_NSE 1229#if DEBUG_NSE
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection to core FAILED!\n");
1272 "Connection to core FAILED!\n");
1273#endif 1231#endif
1274 GNUNET_SCHEDULER_shutdown (); 1232 GNUNET_SCHEDULER_shutdown ();
1275 return; 1233 return;
1276 } 1234 }
1277 GNUNET_assert (0 == memcmp (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity))); 1235 GNUNET_assert (0 ==
1236 memcmp (&my_identity, identity,
1237 sizeof (struct GNUNET_PeerIdentity)));
1278 now = GNUNET_TIME_absolute_get (); 1238 now = GNUNET_TIME_absolute_get ();
1279 current_timestamp.abs_value = (now.abs_value / gnunet_nse_interval.rel_value) * gnunet_nse_interval.rel_value; 1239 current_timestamp.abs_value =
1280 next_timestamp.abs_value = current_timestamp.abs_value + gnunet_nse_interval.rel_value; 1240 (now.abs_value / gnunet_nse_interval.rel_value) *
1281 1241 gnunet_nse_interval.rel_value;
1242 next_timestamp.abs_value =
1243 current_timestamp.abs_value + gnunet_nse_interval.rel_value;
1244
1282 for (i = 0; i < HISTORY_SIZE; i++) 1245 for (i = 0; i < HISTORY_SIZE; i++)
1283 { 1246 {
1284 prev_time.abs_value = current_timestamp.abs_value - (HISTORY_SIZE - i - 1) * gnunet_nse_interval.rel_value; 1247 prev_time.abs_value =
1285 setup_flood_message (i, prev_time); 1248 current_timestamp.abs_value - (HISTORY_SIZE - i -
1286 } 1249 1) * gnunet_nse_interval.rel_value;
1250 setup_flood_message (i, prev_time);
1251 }
1287 estimate_index = HISTORY_SIZE - 1; 1252 estimate_index = HISTORY_SIZE - 1;
1288 estimate_count = 2; 1253 estimate_count = 2;
1289 flood_task 1254 flood_task
1290 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_timestamp), 1255 =
1291 &update_flood_message, NULL); 1256 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1257 (next_timestamp), &update_flood_message,
1258 NULL);
1292} 1259}
1293 1260
1294 1261
@@ -1300,131 +1267,128 @@ core_init (void *cls, struct GNUNET_CORE_Handle *server,
1300 * @param c configuration to use 1267 * @param c configuration to use
1301 */ 1268 */
1302static void 1269static void
1303run(void *cls, struct GNUNET_SERVER_Handle *server, 1270run (void *cls, struct GNUNET_SERVER_Handle *server,
1304 const struct GNUNET_CONFIGURATION_Handle *c) 1271 const struct GNUNET_CONFIGURATION_Handle *c)
1305{ 1272{
1306 char *keyfile; 1273 char *keyfile;
1307 char *proof; 1274 char *proof;
1308 1275
1309 static const struct GNUNET_SERVER_MessageHandler handlers[] = 1276 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1310 { 1277 {&handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START,
1311 { &handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START, sizeof (struct GNUNET_MessageHeader) }, 1278 sizeof (struct GNUNET_MessageHeader)},
1312 { NULL, NULL, 0, 0 } 1279 {NULL, NULL, 0, 0}
1313 }; 1280 };
1314 static const struct GNUNET_CORE_MessageHandler core_handlers[] = 1281 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
1315 { 1282 {&handle_p2p_size_estimate, GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD,
1316 { &handle_p2p_size_estimate, GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, sizeof (struct GNUNET_NSE_FloodMessage) }, 1283 sizeof (struct GNUNET_NSE_FloodMessage)},
1317 { NULL, 0, 0 } 1284 {NULL, 0, 0}
1318 }; 1285 };
1319 cfg = c; 1286 cfg = c;
1320 1287
1321 if ( (GNUNET_OK != 1288 if ((GNUNET_OK !=
1322 GNUNET_CONFIGURATION_get_value_time (cfg, 1289 GNUNET_CONFIGURATION_get_value_time (cfg,
1323 "NSE", "INTERVAL", 1290 "NSE", "INTERVAL",
1324 &gnunet_nse_interval)) || 1291 &gnunet_nse_interval)) ||
1325 (GNUNET_OK != 1292 (GNUNET_OK !=
1326 GNUNET_CONFIGURATION_get_value_time (cfg, 1293 GNUNET_CONFIGURATION_get_value_time (cfg,
1327 "NSE", "WORKDELAY", 1294 "NSE", "WORKDELAY",
1328 &proof_find_delay)) || 1295 &proof_find_delay)) ||
1329 (GNUNET_OK != 1296 (GNUNET_OK !=
1330 GNUNET_CONFIGURATION_get_value_number (cfg, 1297 GNUNET_CONFIGURATION_get_value_number (cfg,
1331 "NSE", "WORKBITS", 1298 "NSE", "WORKBITS",
1332 &nse_work_required)) ) 1299 &nse_work_required)))
1333 { 1300 {
1334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1335 _ ("NSE service is lacking key configuration settings. Exiting.\n")); 1302 _
1336 GNUNET_SCHEDULER_shutdown (); 1303 ("NSE service is lacking key configuration settings. Exiting.\n"));
1337 return; 1304 GNUNET_SCHEDULER_shutdown ();
1338 } 1305 return;
1306 }
1339 if (nse_work_required >= sizeof (GNUNET_HashCode) * 8) 1307 if (nse_work_required >= sizeof (GNUNET_HashCode) * 8)
1340 { 1308 {
1341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1342 _ ("Invalid work requirement for NSE service. Exiting.\n")); 1310 _("Invalid work requirement for NSE service. Exiting.\n"));
1343 GNUNET_SCHEDULER_shutdown (); 1311 GNUNET_SCHEDULER_shutdown ();
1344 return; 1312 return;
1345 } 1313 }
1346 1314
1347 1315
1348 if (GNUNET_OK != 1316 if (GNUNET_OK !=
1349 GNUNET_CONFIGURATION_get_value_filename (cfg, 1317 GNUNET_CONFIGURATION_get_value_filename (cfg,
1350 "GNUNETD", "HOSTKEY", 1318 "GNUNETD", "HOSTKEY", &keyfile))
1351 &keyfile)) 1319 {
1352 { 1320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1321 _
1354 _ ("NSE service is lacking key configuration settings. Exiting.\n")); 1322 ("NSE service is lacking key configuration settings. Exiting.\n"));
1355 GNUNET_SCHEDULER_shutdown (); 1323 GNUNET_SCHEDULER_shutdown ();
1356 return; 1324 return;
1357 } 1325 }
1358 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); 1326 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1359 GNUNET_free (keyfile); 1327 GNUNET_free (keyfile);
1360 if (my_private_key == NULL) 1328 if (my_private_key == NULL)
1361 { 1329 {
1362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1363 _("NSE service could not access hostkey. Exiting.\n")); 1331 _("NSE service could not access hostkey. Exiting.\n"));
1364 GNUNET_SCHEDULER_shutdown (); 1332 GNUNET_SCHEDULER_shutdown ();
1365 return; 1333 return;
1366 } 1334 }
1367 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key); 1335 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1368 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey); 1336 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
1369 if (GNUNET_OK != 1337 &my_identity.hashPubKey);
1370 GNUNET_CONFIGURATION_get_value_filename (cfg, 1338 if (GNUNET_OK !=
1371 "NSE", "PROOFFILE", 1339 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
1372 &proof)) 1340 {
1341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1342 _
1343 ("NSE service is lacking key configuration settings. Exiting.\n"));
1344 if (my_private_key != NULL)
1373 { 1345 {
1374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1346 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1375 _ ("NSE service is lacking key configuration settings. Exiting.\n")); 1347 my_private_key = NULL;
1376 if (my_private_key != NULL)
1377 {
1378 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1379 my_private_key = NULL;
1380 }
1381 GNUNET_SCHEDULER_shutdown ();
1382 return;
1383 } 1348 }
1384 if ( (GNUNET_YES != GNUNET_DISK_file_test (proof)) || 1349 GNUNET_SCHEDULER_shutdown ();
1385 (sizeof (my_proof) != 1350 return;
1386 GNUNET_DISK_fn_read (proof, 1351 }
1387 &my_proof, 1352 if ((GNUNET_YES != GNUNET_DISK_file_test (proof)) ||
1388 sizeof (my_proof))) ) 1353 (sizeof (my_proof) !=
1389 my_proof = 0; 1354 GNUNET_DISK_fn_read (proof, &my_proof, sizeof (my_proof))))
1355 my_proof = 0;
1390 GNUNET_free (proof); 1356 GNUNET_free (proof);
1391 proof_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, 1357 proof_task =
1392 &find_proof, 1358 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1393 NULL); 1359 &find_proof, NULL);
1394 1360
1395 peers = GNUNET_CONTAINER_multihashmap_create (128); 1361 peers = GNUNET_CONTAINER_multihashmap_create (128);
1396 GNUNET_SERVER_add_handlers (server, handlers); 1362 GNUNET_SERVER_add_handlers (server, handlers);
1397 nc = GNUNET_SERVER_notification_context_create (server, 1); 1363 nc = GNUNET_SERVER_notification_context_create (server, 1);
1398 /* Connect to core service and register core handlers */ 1364 /* Connect to core service and register core handlers */
1399 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */ 1365 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
1400 CORE_QUEUE_SIZE, /* queue size */ 1366 CORE_QUEUE_SIZE, /* queue size */
1401 NULL, /* Closure passed to functions */ 1367 NULL, /* Closure passed to functions */
1402 &core_init, /* Call core_init once connected */ 1368 &core_init, /* Call core_init once connected */
1403 &handle_core_connect, /* Handle connects */ 1369 &handle_core_connect, /* Handle connects */
1404 &handle_core_disconnect, /* Handle disconnects */ 1370 &handle_core_disconnect, /* Handle disconnects */
1405 NULL, /* Do we care about "status" updates? */ 1371 NULL, /* Do we care about "status" updates? */
1406 NULL, /* Don't want notified about all incoming messages */ 1372 NULL, /* Don't want notified about all incoming messages */
1407 GNUNET_NO, /* For header only inbound notification */ 1373 GNUNET_NO, /* For header only inbound notification */
1408 NULL, /* Don't want notified about all outbound messages */ 1374 NULL, /* Don't want notified about all outbound messages */
1409 GNUNET_NO, /* For header only outbound notification */ 1375 GNUNET_NO, /* For header only outbound notification */
1410 core_handlers); /* Register these handlers */ 1376 core_handlers); /* Register these handlers */
1411 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 1377 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1412 &shutdown_task, NULL); 1378 &shutdown_task, NULL);
1413#if ENABLE_HISTOGRAM 1379#if ENABLE_HISTOGRAM
1414 if (GNUNET_OK == 1380 if (GNUNET_OK ==
1415 GNUNET_CONFIGURATION_get_value_filename (cfg, 1381 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "HISTOGRAM", &proof))
1416 "NSE", "HISTOGRAM", 1382 {
1417 &proof)) 1383 wh = GNUNET_BIO_write_open (proof);
1418 { 1384 GNUNET_free (proof);
1419 wh = GNUNET_BIO_write_open (proof); 1385 }
1420 GNUNET_free (proof);
1421 }
1422#endif 1386#endif
1423 if (coreAPI == NULL) 1387 if (coreAPI == NULL)
1424 { 1388 {
1425 GNUNET_SCHEDULER_shutdown (); 1389 GNUNET_SCHEDULER_shutdown ();
1426 return; 1390 return;
1427 } 1391 }
1428 stats = GNUNET_STATISTICS_create ("nse", cfg); 1392 stats = GNUNET_STATISTICS_create ("nse", cfg);
1429} 1393}
1430 1394
@@ -1437,13 +1401,12 @@ run(void *cls, struct GNUNET_SERVER_Handle *server,
1437 * @return 0 ok, 1 on error 1401 * @return 0 ok, 1 on error
1438 */ 1402 */
1439int 1403int
1440main(int argc, char * const *argv) 1404main (int argc, char *const *argv)
1441{ 1405{
1442 return (GNUNET_OK == GNUNET_SERVICE_run (argc, argv, 1406 return (GNUNET_OK == GNUNET_SERVICE_run (argc, argv,
1443 "nse", 1407 "nse",
1444 GNUNET_SERVICE_OPTION_NONE, &run, 1408 GNUNET_SERVICE_OPTION_NONE, &run,
1445 NULL)) ? 0 : 1; 1409 NULL)) ? 0 : 1;
1446} 1410}
1447 1411
1448/* end of gnunet-service-nse.c */ 1412/* end of gnunet-service-nse.c */
1449
diff --git a/src/nse/nse_api.c b/src/nse/nse_api.c
index 3be140c01..62c58ca77 100644
--- a/src/nse/nse_api.c
+++ b/src/nse/nse_api.c
@@ -86,8 +86,7 @@ struct GNUNET_NSE_Handle
86 * @param tc scheduler context 86 * @param tc scheduler context
87 */ 87 */
88static void 88static void
89reconnect (void *cls, 89reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
90 const struct GNUNET_SCHEDULER_TaskContext *tc);
91 90
92 91
93/** 92/**
@@ -97,38 +96,33 @@ reconnect (void *cls,
97 * @param cls closure 96 * @param cls closure
98 * @param msg message received, NULL on timeout or fatal error 97 * @param msg message received, NULL on timeout or fatal error
99 */ 98 */
100static void 99static void
101message_handler (void *cls, 100message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
102 const struct GNUNET_MessageHeader * msg)
103{ 101{
104 struct GNUNET_NSE_Handle *h = cls; 102 struct GNUNET_NSE_Handle *h = cls;
105 const struct GNUNET_NSE_ClientMessage *client_msg; 103 const struct GNUNET_NSE_ClientMessage *client_msg;
106 104
107 if (msg == NULL) 105 if (msg == NULL)
108 { 106 {
109 /* Error, timeout, death */ 107 /* Error, timeout, death */
110 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 108 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
111 h->client = NULL; 109 h->client = NULL;
112 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, 110 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
113 &reconnect, 111 &reconnect, h);
114 h); 112 return;
115 return; 113 }
116 } 114 if ((ntohs (msg->size) != sizeof (struct GNUNET_NSE_ClientMessage)) ||
117 if ( (ntohs (msg->size) != sizeof(struct GNUNET_NSE_ClientMessage)) || 115 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE))
118 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE) ) 116 {
119 { 117 GNUNET_break (0);
120 GNUNET_break (0); 118 return;
121 return; 119 }
122 } 120 client_msg = (const struct GNUNET_NSE_ClientMessage *) msg;
123 client_msg = (const struct GNUNET_NSE_ClientMessage *)msg; 121 h->recv_cb (h->recv_cb_cls,
124 h->recv_cb (h->recv_cb_cls, 122 GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
125 GNUNET_TIME_absolute_ntoh(client_msg->timestamp), 123 client_msg->size_estimate, client_msg->std_deviation);
126 client_msg->size_estimate,
127 client_msg->std_deviation);
128 GNUNET_CLIENT_receive (h->client, 124 GNUNET_CLIENT_receive (h->client,
129 &message_handler, 125 &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
130 h,
131 GNUNET_TIME_UNIT_FOREVER_REL);
132} 126}
133 127
134 128
@@ -144,15 +138,15 @@ reschedule_connect (struct GNUNET_NSE_Handle *h)
144 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK); 138 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
145 139
146 if (NULL != h->th) 140 if (NULL != h->th)
147 { 141 {
148 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); 142 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
149 h->th = NULL; 143 h->th = NULL;
150 } 144 }
151 if (NULL != h->client) 145 if (NULL != h->client)
152 { 146 {
153 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 147 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
154 h->client = NULL; 148 h->client = NULL;
155 } 149 }
156 150
157#if DEBUG_NSE 151#if DEBUG_NSE
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -160,18 +154,17 @@ reschedule_connect (struct GNUNET_NSE_Handle *h)
160 h->reconnect_delay.rel_value); 154 h->reconnect_delay.rel_value);
161#endif 155#endif
162 h->reconnect_task 156 h->reconnect_task
163 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, 157 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
164 &reconnect, h);
165 if (h->reconnect_delay.rel_value == 0) 158 if (h->reconnect_delay.rel_value == 0)
166 { 159 {
167 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS; 160 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
168 } 161 }
169 else 162 else
170 { 163 {
171 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2); 164 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
172 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, 165 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
173 h->reconnect_delay); 166 h->reconnect_delay);
174 } 167 }
175} 168}
176 169
177 170
@@ -191,23 +184,21 @@ send_start (void *cls, size_t size, void *buf)
191 184
192 h->th = NULL; 185 h->th = NULL;
193 if (buf == NULL) 186 if (buf == NULL)
194 { 187 {
195 /* Connect error... */ 188 /* Connect error... */
196#if DEBUG_NSE 189#if DEBUG_NSE
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Shutdown while trying to transmit `%s' request.\n", 191 "Shutdown while trying to transmit `%s' request.\n", "START");
199 "START");
200#endif 192#endif
201 reschedule_connect(h); 193 reschedule_connect (h);
202 return 0; 194 return 0;
203 } 195 }
204#if DEBUG_NSE 196#if DEBUG_NSE
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
206 "Transmitting `%s' request.\n", "START");
207#endif 198#endif
208 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); 199 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
209 200
210 msg = (struct GNUNET_MessageHeader *)buf; 201 msg = (struct GNUNET_MessageHeader *) buf;
211 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 202 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
212 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START); 203 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
213 GNUNET_CLIENT_receive (h->client, 204 GNUNET_CLIENT_receive (h->client,
@@ -223,17 +214,16 @@ send_start (void *cls, size_t size, void *buf)
223 * @param tc scheduler context 214 * @param tc scheduler context
224 */ 215 */
225static void 216static void
226reconnect (void *cls, 217reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 const struct GNUNET_SCHEDULER_TaskContext *tc)
228{ 218{
229 struct GNUNET_NSE_Handle *h = cls; 219 struct GNUNET_NSE_Handle *h = cls;
230 220
231 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 221 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
232 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 222 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
233 { 223 {
234 /* shutdown, just give up */ 224 /* shutdown, just give up */
235 return; 225 return;
236 } 226 }
237#if DEBUG_NSE 227#if DEBUG_NSE
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Connecting to network size estimation service.\n"); 229 "Connecting to network size estimation service.\n");
@@ -243,13 +233,11 @@ reconnect (void *cls,
243 GNUNET_assert (h->client != NULL); 233 GNUNET_assert (h->client != NULL);
244 234
245 h->th = 235 h->th =
246 GNUNET_CLIENT_notify_transmit_ready (h->client, 236 GNUNET_CLIENT_notify_transmit_ready (h->client,
247 sizeof(struct GNUNET_MessageHeader), 237 sizeof (struct GNUNET_MessageHeader),
248 GNUNET_TIME_UNIT_FOREVER_REL, 238 GNUNET_TIME_UNIT_FOREVER_REL,
249 GNUNET_NO, 239 GNUNET_NO, &send_start, h);
250 &send_start, 240 GNUNET_assert (h->th != NULL);
251 h);
252 GNUNET_assert(h->th != NULL);
253} 241}
254 242
255 243
@@ -287,23 +275,23 @@ GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
287void 275void
288GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h) 276GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
289{ 277{
290 GNUNET_assert(h != NULL); 278 GNUNET_assert (h != NULL);
291 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK) 279 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
292 { 280 {
293 GNUNET_SCHEDULER_cancel(h->reconnect_task); 281 GNUNET_SCHEDULER_cancel (h->reconnect_task);
294 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 282 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
295 } 283 }
296 if (h->th != NULL) 284 if (h->th != NULL)
297 { 285 {
298 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th); 286 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
299 h->th = NULL; 287 h->th = NULL;
300 } 288 }
301 if (h->client != NULL) 289 if (h->client != NULL)
302 { 290 {
303 GNUNET_CLIENT_disconnect(h->client, GNUNET_NO); 291 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
304 h->client = NULL; 292 h->client = NULL;
305 } 293 }
306 GNUNET_free(h); 294 GNUNET_free (h);
307} 295}
308 296
309/* end of nse_api.c */ 297/* end of nse_api.c */
diff --git a/src/nse/test_nse_api.c b/src/nse/test_nse_api.c
index aaaad1fc6..c06b2be8b 100644
--- a/src/nse/test_nse_api.c
+++ b/src/nse/test_nse_api.c
@@ -68,15 +68,13 @@ stop_arm (struct PeerContext *p)
68 * @param tc context information (why was this task triggered now) 68 * @param tc context information (why was this task triggered now)
69 */ 69 */
70static void 70static void
71end_test (void *cls, 71end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
72 const struct GNUNET_SCHEDULER_TaskContext * tc)
73{ 72{
74 if (h != NULL) 73 if (h != NULL)
75 { 74 {
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from NSE service.\n");
77 "Disconnecting from NSE service.\n"); 76 GNUNET_NSE_disconnect (h);
78 GNUNET_NSE_disconnect (h); 77 }
79 }
80} 78}
81 79
82/** 80/**
@@ -90,20 +88,20 @@ end_test (void *cls,
90 * 88 *
91 */ 89 */
92static void 90static void
93check_nse_message (void *cls, 91check_nse_message (void *cls,
94 struct GNUNET_TIME_Absolute timestamp, 92 struct GNUNET_TIME_Absolute timestamp,
95 double estimate, double std_dev) 93 double estimate, double std_dev)
96{ 94{
97 int *ok = cls; 95 int *ok = cls;
98 96
99 fprintf (stderr, 97 fprintf (stderr,
100 "Received NSE message, estimate %f, standard deviation %f.\n", 98 "Received NSE message, estimate %f, standard deviation %f.\n",
101 estimate, std_dev); 99 estimate, std_dev);
102 /* Fantastic check below. Expect NaN, the only thing not equal to itself. */ 100 /* Fantastic check below. Expect NaN, the only thing not equal to itself. */
103 (*ok) = 0; 101 (*ok) = 0;
104 if (die_task != GNUNET_SCHEDULER_NO_TASK) 102 if (die_task != GNUNET_SCHEDULER_NO_TASK)
105 GNUNET_SCHEDULER_cancel(die_task); 103 GNUNET_SCHEDULER_cancel (die_task);
106 die_task = GNUNET_SCHEDULER_add_now(&end_test, NULL); 104 die_task = GNUNET_SCHEDULER_add_now (&end_test, NULL);
107} 105}
108 106
109 107
@@ -113,12 +111,12 @@ setup_peer (struct PeerContext *p, const char *cfgname)
113 p->cfg = GNUNET_CONFIGURATION_create (); 111 p->cfg = GNUNET_CONFIGURATION_create ();
114#if START_ARM 112#if START_ARM
115 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, 113 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
116 "gnunet-service-arm", 114 "gnunet-service-arm",
117 "gnunet-service-arm", 115 "gnunet-service-arm",
118#if VERBOSE_ARM 116#if VERBOSE_ARM
119 "-L", "DEBUG", 117 "-L", "DEBUG",
120#endif 118#endif
121 "-c", cfgname, NULL); 119 "-c", cfgname, NULL);
122#endif 120#endif
123 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); 121 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
124 122
@@ -129,8 +127,7 @@ setup_peer (struct PeerContext *p, const char *cfgname)
129static void 127static void
130run (void *cls, 128run (void *cls,
131 char *const *args, 129 char *const *args,
132 const char *cfgfile, 130 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
133 const struct GNUNET_CONFIGURATION_Handle *cfg)
134{ 131{
135 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 132 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
136 (GNUNET_TIME_UNIT_MINUTES, 1), 133 (GNUNET_TIME_UNIT_MINUTES, 1),
@@ -138,8 +135,7 @@ run (void *cls,
138 135
139 setup_peer (&p1, cfgfile); 136 setup_peer (&p1, cfgfile);
140 h = GNUNET_NSE_connect (cfg, &check_nse_message, cls); 137 h = GNUNET_NSE_connect (cfg, &check_nse_message, cls);
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to NSE service.\n");
142 "Connecting to NSE service.\n");
143 GNUNET_assert (h != NULL); 139 GNUNET_assert (h != NULL);
144} 140}
145 141
@@ -148,13 +144,14 @@ static int
148check () 144check ()
149{ 145{
150 int ok = 1; 146 int ok = 1;
147
151 char *const argv[] = { "test-nse-api", 148 char *const argv[] = { "test-nse-api",
152 "-c", 149 "-c",
153 "test_nse.conf", 150 "test_nse.conf",
154#if DEBUG_NSE 151#if DEBUG_NSE
155 "-L", "DEBUG", 152 "-L", "DEBUG",
156#else 153#else
157 "-L", "WARNING", 154 "-L", "WARNING",
158#endif 155#endif
159 NULL 156 NULL
160 }; 157 };
@@ -162,10 +159,8 @@ check ()
162 GNUNET_GETOPT_OPTION_END 159 GNUNET_GETOPT_OPTION_END
163 }; 160 };
164 161
165 GNUNET_PROGRAM_run (5, argv, "test-nse-api", "nohelp", 162 GNUNET_PROGRAM_run (5, argv, "test-nse-api", "nohelp", options, &run, &ok);
166 options, &run, &ok); 163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping arm.\n");
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Stopping arm.\n");
169 stop_arm (&p1); 164 stop_arm (&p1);
170 return ok; 165 return ok;
171} 166}
diff --git a/src/nse/test_nse_multipeer.c b/src/nse/test_nse_multipeer.c
index 0f5edeb48..29e5cd6cc 100644
--- a/src/nse/test_nse_multipeer.c
+++ b/src/nse/test_nse_multipeer.c
@@ -70,40 +70,38 @@ static void
70shutdown_callback (void *cls, const char *emsg) 70shutdown_callback (void *cls, const char *emsg)
71{ 71{
72 if (emsg != NULL) 72 if (emsg != NULL)
73 { 73 {
74#if VERBOSE 74#if VERBOSE
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76 "Shutdown of peers failed: %s!\n", 76 "Shutdown of peers failed: %s!\n", emsg);
77 emsg);
78#endif 77#endif
79 if (ok == 0) 78 if (ok == 0)
80 ok = 666; 79 ok = 666;
81 } 80 }
82 else 81 else
83 { 82 {
84#if VERBOSE 83#if VERBOSE
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
86 "All peers successfully shut down!\n");
87#endif 85#endif
88 ok = 0; 86 ok = 0;
89 } 87 }
90} 88}
91 89
92static void 90static void
93shutdown_task (void *cls, 91shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 const struct GNUNET_SCHEDULER_TaskContext *tc)
95{ 92{
96 struct NSEPeer *pos; 93 struct NSEPeer *pos;
94
97#if VERBOSE 95#if VERBOSE
98 fprintf(stderr, "Ending test.\n"); 96 fprintf (stderr, "Ending test.\n");
99#endif 97#endif
100 98
101 while (NULL != (pos = peer_head)) 99 while (NULL != (pos = peer_head))
102 { 100 {
103 GNUNET_NSE_disconnect(pos->nse_handle); 101 GNUNET_NSE_disconnect (pos->nse_handle);
104 GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos); 102 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
105 GNUNET_free(pos); 103 GNUNET_free (pos);
106 } 104 }
107 105
108 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); 106 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
109} 107}
@@ -120,64 +118,61 @@ shutdown_task (void *cls,
120 */ 118 */
121static void 119static void
122handle_estimate (void *cls, 120handle_estimate (void *cls,
123 struct GNUNET_TIME_Absolute timestamp, 121 struct GNUNET_TIME_Absolute timestamp,
124 double estimate, double std_dev) 122 double estimate, double std_dev)
125{ 123{
126 struct NSEPeer *peer = cls; 124 struct NSEPeer *peer = cls;
127 125
128 fprintf (stderr, 126 fprintf (stderr,
129 "Received network size estimate from peer %s. logSize: %f std.dev. %f (%f/%u)\n", 127 "Received network size estimate from peer %s. logSize: %f std.dev. %f (%f/%u)\n",
130 GNUNET_i2s(&peer->daemon->id), 128 GNUNET_i2s (&peer->daemon->id),
131 estimate, 129 estimate,
132 std_dev, 130 std_dev, GNUNET_NSE_log_estimate_to_n (estimate), num_peers);
133 GNUNET_NSE_log_estimate_to_n (estimate),
134 num_peers);
135} 131}
136 132
137 133
138static void 134static void
139connect_nse_service (void *cls, 135connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 const struct GNUNET_SCHEDULER_TaskContext *tc)
141{ 136{
142 struct NSEPeer *current_peer; 137 struct NSEPeer *current_peer;
143 unsigned int i; 138 unsigned int i;
139
144#if VERBOSE 140#if VERBOSE
145 fprintf(stderr, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n"); 141 fprintf (stderr, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
146#endif 142#endif
147 for (i = 0; i < num_peers; i++) 143 for (i = 0; i < num_peers; i++)
148 { 144 {
149 current_peer = GNUNET_malloc(sizeof(struct NSEPeer)); 145 current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
150 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i); 146 current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i);
151 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg, 147 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
152 &handle_estimate, 148 &handle_estimate,
153 current_peer); 149 current_peer);
154 GNUNET_assert(current_peer->nse_handle != NULL); 150 GNUNET_assert (current_peer->nse_handle != NULL);
155 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer); 151 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
156 } 152 }
157} 153}
158 154
159 155
160static void 156static void
161my_cb (void *cls, 157my_cb (void *cls, const char *emsg)
162 const char *emsg)
163{ 158{
164 if (emsg != NULL) 159 if (emsg != NULL)
165 { 160 {
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "Peergroup callback called with error, aborting test!\n"); 162 "Peergroup callback called with error, aborting test!\n");
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n"); 163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
169 ok = 1; 164 ok = 1;
170 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); 165 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
171 return; 166 return;
172 } 167 }
173#if VERBOSE 168#if VERBOSE
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Peer Group started successfully, connecting to NSE service for each peer!\n"); 170 "Peer Group started successfully, connecting to NSE service for each peer!\n");
176#endif 171#endif
177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
178 "Have %u connections\n", total_connections); 173 "Have %u connections\n", total_connections);
179 174
180 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL); 175 GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
181} 176}
182 177
183 178
@@ -195,22 +190,21 @@ my_cb (void *cls,
195 * @param second_daemon handle for the second daemon 190 * @param second_daemon handle for the second daemon
196 * @param emsg error message (NULL on success) 191 * @param emsg error message (NULL on success)
197 */ 192 */
198static void 193static void
199connect_cb (void *cls, 194connect_cb (void *cls,
200 const struct GNUNET_PeerIdentity *first, 195 const struct GNUNET_PeerIdentity *first,
201 const struct GNUNET_PeerIdentity *second, 196 const struct GNUNET_PeerIdentity *second,
202 uint32_t distance, 197 uint32_t distance,
203 const struct GNUNET_CONFIGURATION_Handle *first_cfg, 198 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
204 const struct GNUNET_CONFIGURATION_Handle *second_cfg, 199 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
205 struct GNUNET_TESTING_Daemon *first_daemon, 200 struct GNUNET_TESTING_Daemon *first_daemon,
206 struct GNUNET_TESTING_Daemon *second_daemon, 201 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
207 const char *emsg)
208{ 202{
209 if (emsg == NULL) 203 if (emsg == NULL)
210 { 204 {
211 //fprintf(stderr, "Connected %s -> %s\n", GNUNET_i2s(first), second_id); 205 //fprintf(stderr, "Connected %s -> %s\n", GNUNET_i2s(first), second_id);
212 total_connections++; 206 total_connections++;
213 } 207 }
214} 208}
215 209
216 210
@@ -224,30 +218,26 @@ run (void *cls,
224 unsigned long long total_peers; 218 unsigned long long total_peers;
225 219
226 ok = 1; 220 ok = 1;
227 testing_cfg = GNUNET_CONFIGURATION_create(); 221 testing_cfg = GNUNET_CONFIGURATION_create ();
228 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(testing_cfg, cfgfile)); 222 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (testing_cfg, cfgfile));
229 223
230#if VERBOSE 224#if VERBOSE
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n"); 225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
232 GNUNET_CONFIGURATION_set_value_string (testing_cfg, 226 GNUNET_CONFIGURATION_set_value_string (testing_cfg,
233 "testing", 227 "testing", "use_progressbars", "YES");
234 "use_progressbars",
235 "YES");
236#endif 228#endif
237 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, 229 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
238 "testing", 230 "testing",
239 "num_peers", 231 "num_peers",
240 &total_peers)) 232 &total_peers))
241 total_peers = NUM_PEERS; 233 total_peers = NUM_PEERS;
242 234
243 peers_left = total_peers; 235 peers_left = total_peers;
244 num_peers = peers_left; 236 num_peers = peers_left;
245 pg = GNUNET_TESTING_peergroup_start(testing_cfg, 237 pg = GNUNET_TESTING_peergroup_start (testing_cfg,
246 peers_left, 238 peers_left,
247 TIMEOUT, 239 TIMEOUT,
248 &connect_cb, 240 &connect_cb, &my_cb, NULL, NULL);
249 &my_cb, NULL,
250 NULL);
251 GNUNET_assert (pg != NULL); 241 GNUNET_assert (pg != NULL);
252 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL); 242 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
253} 243}
@@ -268,8 +258,7 @@ check ()
268 GNUNET_GETOPT_OPTION_END 258 GNUNET_GETOPT_OPTION_END
269 }; 259 };
270 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 260 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
271 argv, "test-nse-multipeer", "nohelp", 261 argv, "test-nse-multipeer", "nohelp", options, &run, &ok);
272 options, &run, &ok);
273 return ok; 262 return ok;
274} 263}
275 264