aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-04-21 15:16:45 +0000
committerChristian Grothoff <christian@grothoff.org>2012-04-21 15:16:45 +0000
commita5ca53b73e643a0e62ded4ac9595bff38d08cb0b (patch)
treeeaf74f5b38c35e1530f9d49946db01a13cce3217 /src
parent90c9abc573f95de334a1f61faa089e79046d2f11 (diff)
downloadgnunet-a5ca53b73e643a0e62ded4ac9595bff38d08cb0b.tar.gz
gnunet-a5ca53b73e643a0e62ded4ac9595bff38d08cb0b.zip
-misc offline hacking / code cleanup in util
Diffstat (limited to 'src')
-rw-r--r--src/arm/gnunet-service-arm.c1
-rw-r--r--src/arm/mockup-service.c1
-rw-r--r--src/include/gnunet_connection_lib.h10
-rw-r--r--src/include/gnunet_server_lib.h16
-rw-r--r--src/statistics/gnunet-service-statistics.c1
-rw-r--r--src/util/client.c355
-rw-r--r--src/util/configuration.c1
-rw-r--r--src/util/connection.c551
-rw-r--r--src/util/crypto_hkdf.c1
-rw-r--r--src/util/disk.c73
-rw-r--r--src/util/load.c1
-rw-r--r--src/util/network.c3
-rw-r--r--src/util/server.c261
-rw-r--r--src/util/server_mst.c7
-rw-r--r--src/util/server_nc.c12
-rw-r--r--src/util/service.c249
-rw-r--r--src/util/winproc.c1
17 files changed, 655 insertions, 889 deletions
diff --git a/src/arm/gnunet-service-arm.c b/src/arm/gnunet-service-arm.c
index b1fab4872..459171ffb 100644
--- a/src/arm/gnunet-service-arm.c
+++ b/src/arm/gnunet-service-arm.c
@@ -1224,7 +1224,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *serv,
1224 cfg = c; 1224 cfg = c;
1225 server = serv; 1225 server = serv;
1226 GNUNET_assert (serv != NULL); 1226 GNUNET_assert (serv != NULL);
1227 GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES);
1228 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, 1227 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1229 NULL); 1228 NULL);
1230 child_death_task = 1229 child_death_task =
diff --git a/src/arm/mockup-service.c b/src/arm/mockup-service.c
index 1e9748809..6f90956db 100644
--- a/src/arm/mockup-service.c
+++ b/src/arm/mockup-service.c
@@ -87,7 +87,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
87 {NULL, NULL, 0, 0} 87 {NULL, NULL, 0, 0}
88 }; 88 };
89 /* process client requests */ 89 /* process client requests */
90 GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
91 GNUNET_SERVER_add_handlers (server, handlers); 90 GNUNET_SERVER_add_handlers (server, handlers);
92} 91}
93 92
diff --git a/src/include/gnunet_connection_lib.h b/src/include/gnunet_connection_lib.h
index 0806ccaa3..5ee46356a 100644
--- a/src/include/gnunet_connection_lib.h
+++ b/src/include/gnunet_connection_lib.h
@@ -342,16 +342,6 @@ GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
342 *th); 342 *th);
343 343
344 344
345/**
346 * Configure this connection to ignore shutdown signals.
347 *
348 * @param connection connection handle
349 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
350 */
351void
352GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection,
353 int do_ignore);
354
355 345
356#if 0 /* keep Emacsens' auto-indent happy */ 346#if 0 /* keep Emacsens' auto-indent happy */
357{ 347{
diff --git a/src/include/gnunet_server_lib.h b/src/include/gnunet_server_lib.h
index 491e15bcb..e96c51945 100644
--- a/src/include/gnunet_server_lib.h
+++ b/src/include/gnunet_server_lib.h
@@ -398,22 +398,6 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
398 398
399 399
400/** 400/**
401 * Configure this server's connections to continue handling client
402 * requests as usual even after we get a shutdown signal. The change
403 * only applies to clients that connect to the server from the outside
404 * using TCP after this call. Clients managed previously or those
405 * added using GNUNET_SERVER_connect_socket and
406 * GNUNET_SERVER_connect_callback are not affected by this option.
407 *
408 * @param h server handle
409 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
410 */
411void
412GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore);
413
414
415
416/**
417 * Disable the "CORK" feature for communication with the given client, 401 * Disable the "CORK" feature for communication with the given client,
418 * forcing the OS to immediately flush the buffer on transmission 402 * forcing the OS to immediately flush the buffer on transmission
419 * instead of potentially buffering multiple messages. 403 * instead of potentially buffering multiple messages.
diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c
index 5b96edfa0..2b60d760b 100644
--- a/src/statistics/gnunet-service-statistics.c
+++ b/src/statistics/gnunet-service-statistics.c
@@ -824,7 +824,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
824 cfg = c; 824 cfg = c;
825 srv = server; 825 srv = server;
826 GNUNET_SERVER_add_handlers (server, handlers); 826 GNUNET_SERVER_add_handlers (server, handlers);
827 GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
828 nc = GNUNET_SERVER_notification_context_create (server, 16); 827 nc = GNUNET_SERVER_notification_context_create (server, 16);
829 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL); 828 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
830 load (server); 829 load (server);
diff --git a/src/util/client.c b/src/util/client.c
index 6e270c0c1..1b28e0577 100644
--- a/src/util/client.c
+++ b/src/util/client.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2001, 2002, 2006, 2008, 2009 Christian Grothoff (and other contributing authors) 3 (C) 2001, 2002, 2006, 2008, 2009, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -26,7 +26,6 @@
26 * Generic TCP code for reliable, record-oriented TCP 26 * Generic TCP code for reliable, record-oriented TCP
27 * connections between clients and service providers. 27 * connections between clients and service providers.
28 */ 28 */
29
30#include "platform.h" 29#include "platform.h"
31#include "gnunet_common.h" 30#include "gnunet_common.h"
32#include "gnunet_client_lib.h" 31#include "gnunet_client_lib.h"
@@ -52,7 +51,7 @@ struct GNUNET_CLIENT_TransmitHandle
52 /** 51 /**
53 * Connection state. 52 * Connection state.
54 */ 53 */
55 struct GNUNET_CLIENT_Connection *sock; 54 struct GNUNET_CLIENT_Connection *client;
56 55
57 /** 56 /**
58 * Function to call to get the data for transmission. 57 * Function to call to get the data for transmission.
@@ -112,7 +111,7 @@ struct TransmitGetResponseContext
112 /** 111 /**
113 * Client handle. 112 * Client handle.
114 */ 113 */
115 struct GNUNET_CLIENT_Connection *sock; 114 struct GNUNET_CLIENT_Connection *client;
116 115
117 /** 116 /**
118 * Message to transmit; do not free, allocated 117 * Message to transmit; do not free, allocated
@@ -146,9 +145,9 @@ struct GNUNET_CLIENT_Connection
146{ 145{
147 146
148 /** 147 /**
149 * the socket handle, NULL if not live 148 * The connection handle, NULL if not live
150 */ 149 */
151 struct GNUNET_CONNECTION_Handle *sock; 150 struct GNUNET_CONNECTION_Handle *connection;
152 151
153 /** 152 /**
154 * Our configuration. 153 * Our configuration.
@@ -260,26 +259,28 @@ static struct GNUNET_CONNECTION_Handle *
260do_connect (const char *service_name, 259do_connect (const char *service_name,
261 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt) 260 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
262{ 261{
263 struct GNUNET_CONNECTION_Handle *sock; 262 struct GNUNET_CONNECTION_Handle *connection;
264 char *hostname; 263 char *hostname;
265 char *unixpath; 264 char *unixpath;
266 unsigned long long port; 265 unsigned long long port;
267 266
268 sock = NULL; 267 connection = NULL;
269#if AF_UNIX 268#if AF_UNIX
270 if (0 == (attempt % 2)) 269 if (0 == (attempt % 2))
271 { 270 {
272 /* on even rounds, try UNIX */ 271 /* on even rounds, try UNIX */
273 unixpath = NULL; 272 unixpath = NULL;
274 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */ 273 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
274 (0 < strlen (unixpath)))
275 { 275 {
276 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); 276 /* We have a non-NULL unixpath, need to validate it */
277 if (sock != NULL) 277 connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
278 if (NULL != connection)
278 { 279 {
279 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", 280 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
280 unixpath); 281 unixpath);
281 GNUNET_free (unixpath); 282 GNUNET_free (unixpath);
282 return sock; 283 return connection;
283 } 284 }
284 } 285 }
285 GNUNET_free_non_null (unixpath); 286 GNUNET_free_non_null (unixpath);
@@ -316,7 +317,7 @@ do_connect (const char *service_name,
316 port = 0; 317 port = 0;
317 hostname = NULL; 318 hostname = NULL;
318 } 319 }
319 if (port == 0) 320 if (0 == port)
320 { 321 {
321#if AF_UNIX 322#if AF_UNIX
322 if (0 != (attempt % 2)) 323 if (0 != (attempt % 2))
@@ -328,13 +329,13 @@ do_connect (const char *service_name,
328 &unixpath)) && 329 &unixpath)) &&
329 (0 < strlen (unixpath))) 330 (0 < strlen (unixpath)))
330 { 331 {
331 sock = 332 connection =
332 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); 333 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
333 if (sock != NULL) 334 if (NULL != connection)
334 { 335 {
335 GNUNET_free (unixpath); 336 GNUNET_free (unixpath);
336 GNUNET_free_non_null (hostname); 337 GNUNET_free_non_null (hostname);
337 return sock; 338 return connection;
338 } 339 }
339 } 340 }
340 GNUNET_free_non_null (unixpath); 341 GNUNET_free_non_null (unixpath);
@@ -346,10 +347,9 @@ do_connect (const char *service_name,
346 GNUNET_free_non_null (hostname); 347 GNUNET_free_non_null (hostname);
347 return NULL; 348 return NULL;
348 } 349 }
349 350 connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
350 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
351 GNUNET_free (hostname); 351 GNUNET_free (hostname);
352 return sock; 352 return connection;
353} 353}
354 354
355 355
@@ -364,17 +364,17 @@ struct GNUNET_CLIENT_Connection *
364GNUNET_CLIENT_connect (const char *service_name, 364GNUNET_CLIENT_connect (const char *service_name,
365 const struct GNUNET_CONFIGURATION_Handle *cfg) 365 const struct GNUNET_CONFIGURATION_Handle *cfg)
366{ 366{
367 struct GNUNET_CLIENT_Connection *ret; 367 struct GNUNET_CLIENT_Connection *client;
368 struct GNUNET_CONNECTION_Handle *sock; 368 struct GNUNET_CONNECTION_Handle *connection;
369 369
370 sock = do_connect (service_name, cfg, 0); 370 connection = do_connect (service_name, cfg, 0);
371 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection)); 371 client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
372 ret->attempts = 1; 372 client->attempts = 1;
373 ret->sock = sock; 373 client->connection = connection;
374 ret->service_name = GNUNET_strdup (service_name); 374 client->service_name = GNUNET_strdup (service_name);
375 ret->cfg = cfg; 375 client->cfg = cfg;
376 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS; 376 client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
377 return ret; 377 return client;
378} 378}
379 379
380 380
@@ -389,54 +389,57 @@ GNUNET_CLIENT_connect (const char *service_name,
389 * destroyed (unless, of course, there is an error with the server in 389 * destroyed (unless, of course, there is an error with the server in
390 * which case the message may still be lost). 390 * which case the message may still be lost).
391 * 391 *
392 * @param sock handle to the service connection 392 * @param client handle to the service connection
393 */ 393 */
394void 394void
395GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock) 395GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
396{ 396{
397 if (sock->in_receive == GNUNET_YES) 397 if (GNUNET_YES == client->in_receive)
398 { 398 {
399 GNUNET_CONNECTION_receive_cancel (sock->sock); 399 GNUNET_CONNECTION_receive_cancel (client->connection);
400 sock->in_receive = GNUNET_NO; 400 client->in_receive = GNUNET_NO;
401 } 401 }
402 if (sock->th != NULL) 402 if (NULL != client->th)
403 { 403 {
404 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th); 404 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
405 sock->th = NULL; 405 client->th = NULL;
406 } 406 }
407 if (NULL != sock->sock) 407 if (NULL != client->connection)
408 { 408 {
409 GNUNET_CONNECTION_destroy (sock->sock); 409 GNUNET_CONNECTION_destroy (client->connection);
410 sock->sock = NULL; 410 client->connection = NULL;
411 } 411 }
412 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK) 412 if (GNUNET_SCHEDULER_NO_TASK != client->receive_task)
413 { 413 {
414 GNUNET_SCHEDULER_cancel (sock->receive_task); 414 GNUNET_SCHEDULER_cancel (client->receive_task);
415 sock->receive_task = GNUNET_SCHEDULER_NO_TASK; 415 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
416 } 416 }
417 if (sock->tag != NULL) 417 if (NULL != client->tag)
418 { 418 {
419 GNUNET_free (sock->tag); 419 GNUNET_free (client->tag);
420 sock->tag = NULL; 420 client->tag = NULL;
421 } 421 }
422 sock->receiver_handler = NULL; 422 client->receiver_handler = NULL;
423 GNUNET_array_grow (sock->received_buf, sock->received_size, 0); 423 GNUNET_array_grow (client->received_buf, client->received_size, 0);
424 GNUNET_free (sock->service_name); 424 GNUNET_free (client->service_name);
425 GNUNET_free (sock); 425 GNUNET_free (client);
426} 426}
427 427
428 428
429/** 429/**
430 * Check if message is complete 430 * Check if message is complete. Sets the "msg_complete" member
431 * in the client struct.
432 *
433 * @param client connection with the buffer to check
431 */ 434 */
432static void 435static void
433check_complete (struct GNUNET_CLIENT_Connection *conn) 436check_complete (struct GNUNET_CLIENT_Connection *client)
434{ 437{
435 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) && 438 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
436 (conn->received_pos >= 439 (client->received_pos >=
437 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)-> 440 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
438 size))) 441 size)))
439 conn->msg_complete = GNUNET_YES; 442 client->msg_complete = GNUNET_YES;
440} 443}
441 444
442 445
@@ -456,52 +459,50 @@ static void
456receive_helper (void *cls, const void *buf, size_t available, 459receive_helper (void *cls, const void *buf, size_t available,
457 const struct sockaddr *addr, socklen_t addrlen, int errCode) 460 const struct sockaddr *addr, socklen_t addrlen, int errCode)
458{ 461{
459 struct GNUNET_CLIENT_Connection *conn = cls; 462 struct GNUNET_CLIENT_Connection *client = cls;
460 struct GNUNET_TIME_Relative remaining; 463 struct GNUNET_TIME_Relative remaining;
461 GNUNET_CLIENT_MessageHandler receive_handler; 464 GNUNET_CLIENT_MessageHandler receive_handler;
462 void *receive_handler_cls; 465 void *receive_handler_cls;
463 466
464 GNUNET_assert (conn->msg_complete == GNUNET_NO); 467 GNUNET_assert (GNUNET_NO == client->msg_complete);
465 conn->in_receive = GNUNET_NO; 468 client->in_receive = GNUNET_NO;
466 if ((available == 0) || (conn->sock == NULL) || (errCode != 0)) 469 if ((0 == available) || (NULL == client->connection) || (0 != errCode))
467 { 470 {
468 /* signal timeout! */ 471 /* signal timeout! */
469 LOG (GNUNET_ERROR_TYPE_DEBUG, 472 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n", 473 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
471 (unsigned int) available, conn->sock == NULL ? "NULL" : "non-NULL", 474 (unsigned int) available, NULL == client->connection ? "NULL" : "non-NULL",
472 STRERROR (errCode)); 475 STRERROR (errCode));
473 if (NULL != (receive_handler = conn->receiver_handler)) 476 if (NULL != (receive_handler = client->receiver_handler))
474 { 477 {
475 receive_handler_cls = conn->receiver_handler_cls; 478 receive_handler_cls = client->receiver_handler_cls;
476 conn->receiver_handler = NULL; 479 client->receiver_handler = NULL;
477 receive_handler (receive_handler_cls, NULL); 480 receive_handler (receive_handler_cls, NULL);
478 } 481 }
479 return; 482 return;
480 } 483 }
481
482 /* FIXME: optimize for common fast case where buf contains the 484 /* FIXME: optimize for common fast case where buf contains the
483 * entire message and we need no copying... */ 485 * entire message and we need no copying... */
484 486
485
486 /* slow path: append to array */ 487 /* slow path: append to array */
487 if (conn->received_size < conn->received_pos + available) 488 if (client->received_size < client->received_pos + available)
488 GNUNET_array_grow (conn->received_buf, conn->received_size, 489 GNUNET_array_grow (client->received_buf, client->received_size,
489 conn->received_pos + available); 490 client->received_pos + available);
490 memcpy (&conn->received_buf[conn->received_pos], buf, available); 491 memcpy (&client->received_buf[client->received_pos], buf, available);
491 conn->received_pos += available; 492 client->received_pos += available;
492 check_complete (conn); 493 check_complete (client);
493 /* check for timeout */ 494 /* check for timeout */
494 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout); 495 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
495 if (remaining.rel_value == 0) 496 if (0 == remaining.rel_value)
496 { 497 {
497 /* signal timeout! */ 498 /* signal timeout! */
498 if (NULL != conn->receiver_handler) 499 if (NULL != client->receiver_handler)
499 conn->receiver_handler (conn->receiver_handler_cls, NULL); 500 client->receiver_handler (client->receiver_handler_cls, NULL);
500 return; 501 return;
501 } 502 }
502 /* back to receive -- either for more data or to call callback! */ 503 /* back to receive -- either for more data or to call callback! */
503 GNUNET_CLIENT_receive (conn, conn->receiver_handler, 504 GNUNET_CLIENT_receive (client, client->receiver_handler,
504 conn->receiver_handler_cls, remaining); 505 client->receiver_handler_cls, remaining);
505} 506}
506 507
507 508
@@ -514,28 +515,28 @@ receive_helper (void *cls, const void *buf, size_t available,
514static void 515static void
515receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 516receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
516{ 517{
517 struct GNUNET_CLIENT_Connection *sock = cls; 518 struct GNUNET_CLIENT_Connection *client = cls;
518 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler; 519 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
519 const struct GNUNET_MessageHeader *cmsg = 520 const struct GNUNET_MessageHeader *cmsg =
520 (const struct GNUNET_MessageHeader *) sock->received_buf; 521 (const struct GNUNET_MessageHeader *) client->received_buf;
521 void *handler_cls = sock->receiver_handler_cls; 522 void *handler_cls = client->receiver_handler_cls;
522 uint16_t msize = ntohs (cmsg->size); 523 uint16_t msize = ntohs (cmsg->size);
523 char mbuf[msize]; 524 char mbuf[msize];
524 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf; 525 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
525 526
526 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n", 527 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
527 ntohs (cmsg->type), msize); 528 ntohs (cmsg->type), msize);
528 sock->receive_task = GNUNET_SCHEDULER_NO_TASK; 529 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
529 GNUNET_assert (GNUNET_YES == sock->msg_complete); 530 GNUNET_assert (GNUNET_YES == client->msg_complete);
530 GNUNET_assert (sock->received_pos >= msize); 531 GNUNET_assert (client->received_pos >= msize);
531 memcpy (msg, cmsg, msize); 532 memcpy (msg, cmsg, msize);
532 memmove (sock->received_buf, &sock->received_buf[msize], 533 memmove (client->received_buf, &client->received_buf[msize],
533 sock->received_pos - msize); 534 client->received_pos - msize);
534 sock->received_pos -= msize; 535 client->received_pos -= msize;
535 sock->msg_complete = GNUNET_NO; 536 client->msg_complete = GNUNET_NO;
536 sock->receiver_handler = NULL; 537 client->receiver_handler = NULL;
537 check_complete (sock); 538 check_complete (client);
538 if (handler != NULL) 539 if (NULL != handler)
539 handler (handler_cls, msg); 540 handler (handler_cls, msg);
540} 541}
541 542
@@ -543,17 +544,17 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
543/** 544/**
544 * Read from the service. 545 * Read from the service.
545 * 546 *
546 * @param sock the service 547 * @param client the service
547 * @param handler function to call with the message 548 * @param handler function to call with the message
548 * @param handler_cls closure for handler 549 * @param handler_cls closure for handler
549 * @param timeout how long to wait until timing out 550 * @param timeout how long to wait until timing out
550 */ 551 */
551void 552void
552GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock, 553GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
553 GNUNET_CLIENT_MessageHandler handler, void *handler_cls, 554 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
554 struct GNUNET_TIME_Relative timeout) 555 struct GNUNET_TIME_Relative timeout)
555{ 556{
556 if (sock->sock == NULL) 557 if (NULL == client->connection)
557 { 558 {
558 /* already disconnected, fail instantly! */ 559 /* already disconnected, fail instantly! */
559 GNUNET_break (0); /* this should not happen in well-written code! */ 560 GNUNET_break (0); /* this should not happen in well-written code! */
@@ -561,21 +562,21 @@ GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
561 handler (handler_cls, NULL); 562 handler (handler_cls, NULL);
562 return; 563 return;
563 } 564 }
564 sock->receiver_handler = handler; 565 client->receiver_handler = handler;
565 sock->receiver_handler_cls = handler_cls; 566 client->receiver_handler_cls = handler_cls;
566 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 567 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
567 if (GNUNET_YES == sock->msg_complete) 568 if (GNUNET_YES == client->msg_complete)
568 { 569 {
569 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task); 570 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task);
570 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock); 571 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
571 } 572 }
572 else 573 else
573 { 574 {
574 GNUNET_assert (sock->in_receive == GNUNET_NO); 575 GNUNET_assert (GNUNET_NO == client->in_receive);
575 sock->in_receive = GNUNET_YES; 576 client->in_receive = GNUNET_YES;
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n"); 577 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
577 GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, 578 GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
578 timeout, &receive_helper, sock); 579 timeout, &receive_helper, client);
579 } 580 }
580} 581}
581 582
@@ -600,23 +601,23 @@ service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
600static void 601static void
601confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg) 602confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
602{ 603{
603 struct GNUNET_CLIENT_Connection *conn = cls; 604 struct GNUNET_CLIENT_Connection *client = cls;
604 605
605 /* We may want to consider looking at the reply in more 606 /* We may want to consider looking at the reply in more
606 * detail in the future, for example, is this the 607 * detail in the future, for example, is this the
607 * correct service? FIXME! */ 608 * correct service? FIXME! */
608 if (msg != NULL) 609 if (NULL != msg)
609 { 610 {
610 LOG (GNUNET_ERROR_TYPE_DEBUG, 611 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "Received confirmation that service is running.\n"); 612 "Received confirmation that service is running.\n");
612 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls, 613 GNUNET_SCHEDULER_add_continuation (client->test_cb, client->test_cb_cls,
613 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 614 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
614 } 615 }
615 else 616 else
616 { 617 {
617 service_test_error (conn->test_cb, conn->test_cb_cls); 618 service_test_error (client->test_cb, client->test_cb_cls);
618 } 619 }
619 GNUNET_CLIENT_disconnect (conn); 620 GNUNET_CLIENT_disconnect (client);
620} 621}
621 622
622 623
@@ -632,23 +633,23 @@ confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
632static size_t 633static size_t
633write_test (void *cls, size_t size, void *buf) 634write_test (void *cls, size_t size, void *buf)
634{ 635{
635 struct GNUNET_CLIENT_Connection *conn = cls; 636 struct GNUNET_CLIENT_Connection *client = cls;
636 struct GNUNET_MessageHeader *msg; 637 struct GNUNET_MessageHeader *msg;
637 638
638 if (size < sizeof (struct GNUNET_MessageHeader)) 639 if (size < sizeof (struct GNUNET_MessageHeader))
639 { 640 {
640 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n")); 641 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
641 service_test_error (conn->test_cb, conn->test_cb_cls); 642 service_test_error (client->test_cb, client->test_cb_cls);
642 GNUNET_CLIENT_disconnect (conn); 643 GNUNET_CLIENT_disconnect (client);
643 return 0; /* client disconnected */ 644 return 0; /* client disconnected */
644 } 645 }
645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST"); 646 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
646 msg = (struct GNUNET_MessageHeader *) buf; 647 msg = (struct GNUNET_MessageHeader *) buf;
647 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); 648 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
648 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 649 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
649 GNUNET_CLIENT_receive (conn, &confirm_handler, conn, 650 GNUNET_CLIENT_receive (client, &confirm_handler, client,
650 GNUNET_TIME_absolute_get_remaining 651 GNUNET_TIME_absolute_get_remaining
651 (conn->test_deadline)); 652 (client->test_deadline));
652 return sizeof (struct GNUNET_MessageHeader); 653 return sizeof (struct GNUNET_MessageHeader);
653} 654}
654 655
@@ -675,7 +676,7 @@ GNUNET_CLIENT_service_test (const char *service,
675 char *hostname; 676 char *hostname;
676 unsigned long long port; 677 unsigned long long port;
677 struct GNUNET_NETWORK_Handle *sock; 678 struct GNUNET_NETWORK_Handle *sock;
678 struct GNUNET_CLIENT_Connection *conn; 679 struct GNUNET_CLIENT_Connection *client;
679 680
680 LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n", 681 LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
681 service); 682 service);
@@ -698,7 +699,7 @@ GNUNET_CLIENT_service_test (const char *service,
698 else 699 else
699 { 700 {
700 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); 701 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
701 if (sock != NULL) 702 if (NULL != sock)
702 { 703 {
703 memset (&s_un, 0, sizeof (s_un)); 704 memset (&s_un, 0, sizeof (s_un));
704 s_un.sun_family = AF_UNIX; 705 s_un.sun_family = AF_UNIX;
@@ -764,7 +765,7 @@ GNUNET_CLIENT_service_test (const char *service,
764 s_in.sin_port = htons (port); 765 s_in.sin_port = htons (port);
765 766
766 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); 767 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
767 if (sock != NULL) 768 if (NULL != sock)
768 { 769 {
769 if (GNUNET_OK != 770 if (GNUNET_OK !=
770 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in, 771 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
@@ -798,7 +799,7 @@ GNUNET_CLIENT_service_test (const char *service,
798 s_in6.sin6_port = htons (port); 799 s_in6.sin6_port = htons (port);
799 800
800 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0); 801 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
801 if (sock != NULL) 802 if (NULL != sock)
802 { 803 {
803 if (GNUNET_OK != 804 if (GNUNET_OK !=
804 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6, 805 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
@@ -830,8 +831,8 @@ GNUNET_CLIENT_service_test (const char *service,
830 GNUNET_free_non_null (hostname); 831 GNUNET_free_non_null (hostname);
831 832
832 /* non-localhost, try 'connect' method */ 833 /* non-localhost, try 'connect' method */
833 conn = GNUNET_CLIENT_connect (service, cfg); 834 client = GNUNET_CLIENT_connect (service, cfg);
834 if (conn == NULL) 835 if (NULL == client)
835 { 836 {
836 LOG (GNUNET_ERROR_TYPE_INFO, 837 LOG (GNUNET_ERROR_TYPE_INFO,
837 _("Could not connect to service `%s', must not be running.\n"), 838 _("Could not connect to service `%s', must not be running.\n"),
@@ -839,18 +840,18 @@ GNUNET_CLIENT_service_test (const char *service,
839 service_test_error (task, task_cls); 840 service_test_error (task, task_cls);
840 return; 841 return;
841 } 842 }
842 conn->test_cb = task; 843 client->test_cb = task;
843 conn->test_cb_cls = task_cls; 844 client->test_cb_cls = task_cls;
844 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout); 845 client->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
845 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn, 846 if (NULL == GNUNET_CLIENT_notify_transmit_ready (client,
846 sizeof (struct GNUNET_MessageHeader), 847 sizeof (struct GNUNET_MessageHeader),
847 timeout, GNUNET_YES, &write_test, 848 timeout, GNUNET_YES, &write_test,
848 conn)) 849 client))
849 { 850 {
850 LOG (GNUNET_ERROR_TYPE_WARNING, 851 LOG (GNUNET_ERROR_TYPE_WARNING,
851 _("Failure to transmit request to service `%s'\n"), service); 852 _("Failure to transmit request to service `%s'\n"), service);
852 service_test_error (task, task_cls); 853 service_test_error (task, task_cls);
853 GNUNET_CLIENT_disconnect (conn); 854 GNUNET_CLIENT_disconnect (client);
854 return; 855 return;
855 } 856 }
856} 857}
@@ -884,25 +885,17 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
884 struct GNUNET_TIME_Relative delay; 885 struct GNUNET_TIME_Relative delay;
885 886
886 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 887 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
887 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 888 th->client->connection =
888 { 889 do_connect (th->client->service_name, th->client->cfg, th->client->attempts++);
889 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n"); 890 if (NULL == th->client->connection)
890 th->sock->th = NULL;
891 th->notify (th->notify_cls, 0, NULL);
892 GNUNET_free (th);
893 return;
894 }
895 th->sock->sock =
896 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++);
897 if (NULL == th->sock->sock)
898 { 891 {
899 /* could happen if we're out of sockets */ 892 /* could happen if we're out of sockets */
900 delay = 893 delay =
901 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining 894 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
902 (th->timeout), th->sock->back_off); 895 (th->timeout), th->client->back_off);
903 th->sock->back_off = 896 th->client->back_off =
904 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply 897 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
905 (th->sock->back_off, 2), 898 (th->client->back_off, 2),
906 GNUNET_TIME_UNIT_SECONDS); 899 GNUNET_TIME_UNIT_SECONDS);
907 LOG (GNUNET_ERROR_TYPE_DEBUG, 900 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 "Transmission failed %u times, trying again in %llums.\n", 901 "Transmission failed %u times, trying again in %llums.\n",
@@ -913,14 +906,14 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
913 return; 906 return;
914 } 907 }
915 th->th = 908 th->th =
916 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size, 909 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
917 GNUNET_TIME_absolute_get_remaining 910 GNUNET_TIME_absolute_get_remaining
918 (th->timeout), &client_notify, 911 (th->timeout), &client_notify,
919 th); 912 th);
920 if (th->th == NULL) 913 if (NULL == th->th)
921 { 914 {
922 GNUNET_break (0); 915 GNUNET_break (0);
923 th->sock->th = NULL; 916 th->client->th = NULL;
924 th->notify (th->notify_cls, 0, NULL); 917 th->notify (th->notify_cls, 0, NULL);
925 GNUNET_free (th); 918 GNUNET_free (th);
926 return; 919 return;
@@ -941,19 +934,17 @@ static size_t
941client_notify (void *cls, size_t size, void *buf) 934client_notify (void *cls, size_t size, void *buf)
942{ 935{
943 struct GNUNET_CLIENT_TransmitHandle *th = cls; 936 struct GNUNET_CLIENT_TransmitHandle *th = cls;
944 struct GNUNET_CLIENT_Connection *sock = th->sock; 937 struct GNUNET_CLIENT_Connection *client = th->client;
945 size_t ret; 938 size_t ret;
946 struct GNUNET_TIME_Relative delay; 939 struct GNUNET_TIME_Relative delay;
947 940
948 th->th = NULL; 941 th->th = NULL;
949 sock->th = NULL; 942 client->th = NULL;
950 if (buf == NULL) 943 if (NULL == buf)
951 { 944 {
952 delay = GNUNET_TIME_absolute_get_remaining (th->timeout); 945 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
953 delay.rel_value /= 2; 946 delay.rel_value /= 2;
954 if ((0 != 947 if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
955 (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
956 (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
957 (delay.rel_value < 1)) 948 (delay.rel_value < 1))
958 { 949 {
959 LOG (GNUNET_ERROR_TYPE_DEBUG, 950 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -966,24 +957,24 @@ client_notify (void *cls, size_t size, void *buf)
966 /* auto-retry */ 957 /* auto-retry */
967 LOG (GNUNET_ERROR_TYPE_DEBUG, 958 LOG (GNUNET_ERROR_TYPE_DEBUG,
968 "Failed to connect to `%s', automatically trying again.\n", 959 "Failed to connect to `%s', automatically trying again.\n",
969 sock->service_name); 960 client->service_name);
970 if (sock->in_receive == GNUNET_YES) 961 if (GNUNET_YES == client->in_receive)
971 { 962 {
972 GNUNET_CONNECTION_receive_cancel (sock->sock); 963 GNUNET_CONNECTION_receive_cancel (client->connection);
973 sock->in_receive = GNUNET_NO; 964 client->in_receive = GNUNET_NO;
974 } 965 }
975 GNUNET_CONNECTION_destroy (sock->sock); 966 GNUNET_CONNECTION_destroy (client->connection);
976 sock->sock = NULL; 967 client->connection = NULL;
977 delay = GNUNET_TIME_relative_min (delay, sock->back_off); 968 delay = GNUNET_TIME_relative_min (delay, client->back_off);
978 sock->back_off = 969 client->back_off =
979 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply 970 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
980 (sock->back_off, 2), 971 (client->back_off, 2),
981 GNUNET_TIME_UNIT_SECONDS); 972 GNUNET_TIME_UNIT_SECONDS);
982 LOG (GNUNET_ERROR_TYPE_DEBUG, 973 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "Transmission failed %u times, trying again in %llums.\n", 974 "Transmission failed %u times, trying again in %llums.\n",
984 MAX_ATTEMPTS - th->attempts_left, 975 MAX_ATTEMPTS - th->attempts_left,
985 (unsigned long long) delay.rel_value); 976 (unsigned long long) delay.rel_value);
986 sock->th = th; 977 client->th = th;
987 th->reconnect_task = 978 th->reconnect_task =
988 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); 979 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
989 return 0; 980 return 0;
@@ -1000,7 +991,7 @@ client_notify (void *cls, size_t size, void *buf)
1000 * are free in the transmission buffer. May call the notify 991 * are free in the transmission buffer. May call the notify
1001 * method immediately if enough space is available. 992 * method immediately if enough space is available.
1002 * 993 *
1003 * @param sock connection to the service 994 * @param client connection to the service
1004 * @param size number of bytes to send 995 * @param size number of bytes to send
1005 * @param timeout after how long should we give up (and call 996 * @param timeout after how long should we give up (and call
1006 * notify with buf NULL and size 0)? 997 * notify with buf NULL and size 0)?
@@ -1015,7 +1006,7 @@ client_notify (void *cls, size_t size, void *buf)
1015 * a handle if the notify callback was queued (can be used to cancel) 1006 * a handle if the notify callback was queued (can be used to cancel)
1016 */ 1007 */
1017struct GNUNET_CLIENT_TransmitHandle * 1008struct GNUNET_CLIENT_TransmitHandle *
1018GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, 1009GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1019 size_t size, 1010 size_t size,
1020 struct GNUNET_TIME_Relative timeout, 1011 struct GNUNET_TIME_Relative timeout,
1021 int auto_retry, 1012 int auto_retry,
@@ -1024,7 +1015,7 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1024{ 1015{
1025 struct GNUNET_CLIENT_TransmitHandle *th; 1016 struct GNUNET_CLIENT_TransmitHandle *th;
1026 1017
1027 if (NULL != sock->th) 1018 if (NULL != client->th)
1028 { 1019 {
1029 /* If this breaks, you most likley called this function twice without waiting 1020 /* If this breaks, you most likley called this function twice without waiting
1030 * for completion or canceling the request */ 1021 * for completion or canceling the request */
@@ -1032,31 +1023,31 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1032 return NULL; 1023 return NULL;
1033 } 1024 }
1034 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle)); 1025 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1035 th->sock = sock; 1026 th->client = client;
1036 th->size = size; 1027 th->size = size;
1037 th->timeout = GNUNET_TIME_relative_to_absolute (timeout); 1028 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1038 th->auto_retry = auto_retry; 1029 th->auto_retry = auto_retry;
1039 th->notify = notify; 1030 th->notify = notify;
1040 th->notify_cls = notify_cls; 1031 th->notify_cls = notify_cls;
1041 th->attempts_left = MAX_ATTEMPTS; 1032 th->attempts_left = MAX_ATTEMPTS;
1042 sock->th = th; 1033 client->th = th;
1043 if (sock->sock == NULL) 1034 if (NULL == client->connection)
1044 { 1035 {
1045 th->reconnect_task = 1036 th->reconnect_task =
1046 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry, 1037 GNUNET_SCHEDULER_add_delayed (client->back_off, &client_delayed_retry,
1047 th); 1038 th);
1048 1039
1049 } 1040 }
1050 else 1041 else
1051 { 1042 {
1052 th->th = 1043 th->th =
1053 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout, 1044 GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, timeout,
1054 &client_notify, th); 1045 &client_notify, th);
1055 if (NULL == th->th) 1046 if (NULL == th->th)
1056 { 1047 {
1057 GNUNET_break (0); 1048 GNUNET_break (0);
1058 GNUNET_free (th); 1049 GNUNET_free (th);
1059 sock->th = NULL; 1050 client->th = NULL;
1060 return NULL; 1051 return NULL;
1061 } 1052 }
1062 } 1053 }
@@ -1073,7 +1064,7 @@ void
1073GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle 1064GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1074 *th) 1065 *th)
1075{ 1066{
1076 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK) 1067 if (GNUNET_SCHEDULER_NO_TASK != th->reconnect_task)
1077 { 1068 {
1078 GNUNET_assert (NULL == th->th); 1069 GNUNET_assert (NULL == th->th);
1079 GNUNET_SCHEDULER_cancel (th->reconnect_task); 1070 GNUNET_SCHEDULER_cancel (th->reconnect_task);
@@ -1084,7 +1075,7 @@ GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1084 GNUNET_assert (NULL != th->th); 1075 GNUNET_assert (NULL != th->th);
1085 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th); 1076 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1086 } 1077 }
1087 th->sock->th = NULL; 1078 th->client->th = NULL;
1088 GNUNET_free (th); 1079 GNUNET_free (th);
1089} 1080}
1090 1081
@@ -1106,7 +1097,7 @@ transmit_for_response (void *cls, size_t size, void *buf)
1106 struct TransmitGetResponseContext *tc = cls; 1097 struct TransmitGetResponseContext *tc = cls;
1107 uint16_t msize; 1098 uint16_t msize;
1108 1099
1109 tc->sock->tag = NULL; 1100 tc->client->tag = NULL;
1110 msize = ntohs (tc->hdr->size); 1101 msize = ntohs (tc->hdr->size);
1111 if (NULL == buf) 1102 if (NULL == buf)
1112 { 1103 {
@@ -1119,7 +1110,7 @@ transmit_for_response (void *cls, size_t size, void *buf)
1119 } 1110 }
1120 GNUNET_assert (size >= msize); 1111 GNUNET_assert (size >= msize);
1121 memcpy (buf, tc->hdr, msize); 1112 memcpy (buf, tc->hdr, msize);
1122 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls, 1113 GNUNET_CLIENT_receive (tc->client, tc->rn, tc->rn_cls,
1123 GNUNET_TIME_absolute_get_remaining (tc->timeout)); 1114 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1124 GNUNET_free (tc); 1115 GNUNET_free (tc);
1125 return msize; 1116 return msize;
@@ -1133,7 +1124,7 @@ transmit_for_response (void *cls, size_t size, void *buf)
1133 * will be called with a "NULL" response (in which 1124 * will be called with a "NULL" response (in which
1134 * case the connection should probably be destroyed). 1125 * case the connection should probably be destroyed).
1135 * 1126 *
1136 * @param sock connection to use 1127 * @param client connection to use
1137 * @param hdr message to transmit 1128 * @param hdr message to transmit
1138 * @param timeout when to give up (for both transmission 1129 * @param timeout when to give up (for both transmission
1139 * and for waiting for a response) 1130 * and for waiting for a response)
@@ -1148,7 +1139,7 @@ transmit_for_response (void *cls, size_t size, void *buf)
1148 * is already pending 1139 * is already pending
1149 */ 1140 */
1150int 1141int
1151GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, 1142GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1152 const struct GNUNET_MessageHeader *hdr, 1143 const struct GNUNET_MessageHeader *hdr,
1153 struct GNUNET_TIME_Relative timeout, 1144 struct GNUNET_TIME_Relative timeout,
1154 int auto_retry, 1145 int auto_retry,
@@ -1158,26 +1149,26 @@ GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
1158 struct TransmitGetResponseContext *tc; 1149 struct TransmitGetResponseContext *tc;
1159 uint16_t msize; 1150 uint16_t msize;
1160 1151
1161 if (NULL != sock->th) 1152 if (NULL != client->th)
1162 return GNUNET_SYSERR; 1153 return GNUNET_SYSERR;
1163 GNUNET_assert (sock->tag == NULL); 1154 GNUNET_assert (NULL == client->tag);
1164 msize = ntohs (hdr->size); 1155 msize = ntohs (hdr->size);
1165 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize); 1156 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1166 tc->sock = sock; 1157 tc->client = client;
1167 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1]; 1158 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1168 memcpy (&tc[1], hdr, msize); 1159 memcpy (&tc[1], hdr, msize);
1169 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout); 1160 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1170 tc->rn = rn; 1161 tc->rn = rn;
1171 tc->rn_cls = rn_cls; 1162 tc->rn_cls = rn_cls;
1172 if (NULL == 1163 if (NULL ==
1173 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry, 1164 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1174 &transmit_for_response, tc)) 1165 &transmit_for_response, tc))
1175 { 1166 {
1176 GNUNET_break (0); 1167 GNUNET_break (0);
1177 GNUNET_free (tc); 1168 GNUNET_free (tc);
1178 return GNUNET_SYSERR; 1169 return GNUNET_SYSERR;
1179 } 1170 }
1180 sock->tag = tc; 1171 client->tag = tc;
1181 return GNUNET_OK; 1172 return GNUNET_OK;
1182} 1173}
1183 1174
diff --git a/src/util/configuration.c b/src/util/configuration.c
index 35ba15b4e..308672fbe 100644
--- a/src/util/configuration.c
+++ b/src/util/configuration.c
@@ -21,7 +21,6 @@
21/** 21/**
22 * @file src/util/configuration.c 22 * @file src/util/configuration.c
23 * @brief configuration management 23 * @brief configuration management
24 *
25 * @author Christian Grothoff 24 * @author Christian Grothoff
26 */ 25 */
27 26
diff --git a/src/util/connection.c b/src/util/connection.c
index 95c8347d0..1e792adc8 100644
--- a/src/util/connection.c
+++ b/src/util/connection.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors) 3 (C) 2009, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -44,28 +44,6 @@
44 44
45#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) 45#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
46 46
47/**
48 * Possible functions to call after connect failed or succeeded.
49 */
50enum ConnectContinuations
51{
52 /**
53 * Call nothing.
54 */
55 COCO_NONE = 0,
56
57 /**
58 * Call "receive_again".
59 */
60 COCO_RECEIVE_AGAIN = 1,
61
62 /**
63 * Call "transmit_ready".
64 */
65 COCO_TRANSMIT_READY = 2
66
67};
68
69 47
70/** 48/**
71 * Transmission handle. There can only be one for each connection. 49 * Transmission handle. There can only be one for each connection.
@@ -254,21 +232,11 @@ struct GNUNET_CONNECTION_Handle
254 struct GNUNET_TIME_Absolute receive_timeout; 232 struct GNUNET_TIME_Absolute receive_timeout;
255 233
256 /** 234 /**
257 * Functions to call after connect failed or succeeded.
258 */
259 enum ConnectContinuations ccs;
260
261 /**
262 * Maximum number of bytes to read (for receiving). 235 * Maximum number of bytes to read (for receiving).
263 */ 236 */
264 size_t max; 237 size_t max;
265 238
266 /** 239 /**
267 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this connection.
268 */
269 int ignore_shutdown;
270
271 /**
272 * Port to connect to. 240 * Port to connect to.
273 */ 241 */
274 uint16_t port; 242 uint16_t port;
@@ -283,6 +251,7 @@ struct GNUNET_CONNECTION_Handle
283 251
284}; 252};
285 253
254
286/** 255/**
287 * Set the persist option on this connection handle. Indicates 256 * Set the persist option on this connection handle. Indicates
288 * that the underlying socket or fd should never really be closed. 257 * that the underlying socket or fd should never really be closed.
@@ -361,7 +330,6 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
361 void *uaddr; 330 void *uaddr;
362 struct GNUNET_CONNECTION_Credentials *gcp; 331 struct GNUNET_CONNECTION_Credentials *gcp;
363 struct GNUNET_CONNECTION_Credentials gc; 332 struct GNUNET_CONNECTION_Credentials gc;
364
365#ifdef SO_PEERCRED 333#ifdef SO_PEERCRED
366 struct ucred uc; 334 struct ucred uc;
367 socklen_t olen; 335 socklen_t olen;
@@ -384,7 +352,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
384 352
385 sa = (struct sockaddr *) addr; 353 sa = (struct sockaddr *) addr;
386 v6 = (struct sockaddr_in6 *) addr; 354 v6 = (struct sockaddr_in6 *) addr;
387 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr))) 355 if ((AF_INET6 == sa->sa_family) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
388 { 356 {
389 /* convert to V4 address */ 357 /* convert to V4 address */
390 v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); 358 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
@@ -409,7 +377,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
409 gcp = NULL; 377 gcp = NULL;
410 gc.uid = 0; 378 gc.uid = 0;
411 gc.gid = 0; 379 gc.gid = 0;
412 if (sa->sa_family == AF_UNIX) 380 if (AF_UNIX == sa->sa_family)
413 { 381 {
414#if HAVE_GETPEEREID 382#if HAVE_GETPEEREID
415 /* most BSDs */ 383 /* most BSDs */
@@ -445,10 +413,10 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
445#endif 413#endif
446 } 414 }
447 415
448 if ((access != NULL) && 416 if ((NULL != access) &&
449 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen)))) 417 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
450 { 418 {
451 if (aret == GNUNET_NO) 419 if (GNUNET_NO == aret)
452 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"), 420 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
453 GNUNET_a2s (uaddr, addrlen)); 421 GNUNET_a2s (uaddr, addrlen));
454 GNUNET_break (GNUNET_OK == 422 GNUNET_break (GNUNET_OK ==
@@ -482,7 +450,7 @@ int
482GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection, 450GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
483 void **addr, size_t * addrlen) 451 void **addr, size_t * addrlen)
484{ 452{
485 if ((connection->addr == NULL) || (connection->addrlen == 0)) 453 if ((NULL == connection->addr) || (0 == connection->addrlen))
486 return GNUNET_NO; 454 return GNUNET_NO;
487 *addr = GNUNET_malloc (connection->addrlen); 455 *addr = GNUNET_malloc (connection->addrlen);
488 memcpy (*addr, connection->addr, connection->addrlen); 456 memcpy (*addr, connection->addr, connection->addrlen);
@@ -492,73 +460,136 @@ GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
492 460
493 461
494/** 462/**
495 * This function is called after establishing a connection either has 463 * Tell the receiver callback that we had an IO error.
496 * succeeded or timed out. Note that it is possible that the attempt
497 * timed out and that we're immediately retrying. If we are retrying,
498 * we need to wait again (or timeout); if we succeeded, we need to
499 * wait for data (or timeout).
500 * 464 *
501 * @param cls our connection handle 465 * @param connection connection to signal error
502 * @param tc task context describing why we are here 466 * @param errcode error code to send
503 */ 467 */
504static void 468static void
505receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 469signal_receive_error (struct GNUNET_CONNECTION_Handle *connection, int errcode)
470{
471 GNUNET_CONNECTION_Receiver receiver;
472
473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "Receive encounters error (%s), connection closed (%p)\n",
475 STRERROR (errcode),
476 connection);
477 GNUNET_assert (NULL != (receiver = connection->receiver));
478 connection->receiver = NULL;
479 receiver (connection->receiver_cls, NULL, 0, connection->addr, connection->addrlen, errcode);
480}
506 481
507 482
508/** 483/**
509 * See if we are now connected. If not, wait longer for 484 * Tell the receiver callback that a timeout was reached.
510 * connect to succeed. If connected, we should be able
511 * to write now as well, unless we timed out.
512 * 485 *
513 * @param cls our connection handle 486 * @param connection connection to signal for
514 * @param tc task context describing why we are here
515 */ 487 */
516static void 488static void
517transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 489signal_receive_timeout (struct GNUNET_CONNECTION_Handle *connection)
490{
491 GNUNET_CONNECTION_Receiver receiver;
492
493 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection signals timeout to receiver (%p)!\n",
494 connection);
495 GNUNET_assert (NULL != (receiver = connection->receiver));
496 connection->receiver = NULL;
497 receiver (connection->receiver_cls, NULL, 0, NULL, 0, 0);
498}
518 499
519 500
520/** 501/**
521 * We've failed for good to establish a connection. 502 * We failed to transmit data to the service, signal the error.
522 * 503 *
523 * @param h the connection we tried to establish 504 * @param connection handle that had trouble
505 * @param ecode error code (errno)
524 */ 506 */
525static void 507static void
526connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) 508signal_transmit_error (struct GNUNET_CONNECTION_Handle *connection,
509 int ecode)
527{ 510{
528 LOG (GNUNET_ERROR_TYPE_INFO, 511 GNUNET_CONNECTION_TransmitReadyNotify notify;
529 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), 512
530 h->hostname, h->port); 513 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 /* connect failed / timed out */ 514 "Transmission encounterd error (%s), connection closed (%p)\n",
532 GNUNET_break (h->ap_head == NULL); 515 STRERROR (ecode),
533 GNUNET_break (h->ap_tail == NULL); 516 connection);
534 GNUNET_break (h->dns_active == GNUNET_NO); 517 if (NULL != connection->sock)
535 GNUNET_break (h->sock == NULL);
536
537 /* trigger jobs that used to wait on "connect_task" */
538 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
539 { 518 {
540 LOG (GNUNET_ERROR_TYPE_DEBUG, 519 GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR);
541 "connect_fail_continuation triggers receive_again (%p)\n", h); 520 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
542 h->ccs -= COCO_RECEIVE_AGAIN; 521 connection->sock = NULL;
543 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 522 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
544 } 523 }
545 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 524 if (GNUNET_SCHEDULER_NO_TASK != connection->read_task)
546 { 525 {
547 LOG (GNUNET_ERROR_TYPE_DEBUG, 526 /* send errors trigger read errors... */
548 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n", 527 GNUNET_SCHEDULER_cancel (connection->read_task);
549 h); 528 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
550 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 529 signal_receive_timeout (connection);
551 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 530 return;
552 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 531 }
553 h->ccs -= COCO_TRANSMIT_READY; 532 if (NULL == connection->nth.notify_ready)
554 GNUNET_assert (h->nth.notify_ready != NULL); 533 return; /* nobody to tell about it */
555 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 534 notify = connection->nth.notify_ready;
556 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h); 535 connection->nth.notify_ready = NULL;
536 notify (connection->nth.notify_ready_cls, 0, NULL);
537}
538
539
540/**
541 * We've failed for good to establish a connection (timeout or
542 * no more addresses to try).
543 *
544 * @param connection the connection we tried to establish
545 */
546static void
547connect_fail_continuation (struct GNUNET_CONNECTION_Handle *connection)
548{
549 LOG (GNUNET_ERROR_TYPE_INFO,
550 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
551 connection->hostname, connection->port);
552 GNUNET_break (NULL == connection->ap_head);
553 GNUNET_break (NULL == connection->ap_tail);
554 GNUNET_break (GNUNET_NO == connection->dns_active);
555 GNUNET_break (NULL == connection->sock);
556 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
557
558 /* signal errors for jobs that used to wait on the connection */
559 if (NULL != connection->receiver)
560 signal_receive_error (connection, ECONNREFUSED);
561 if (NULL != connection->nth.notify_ready)
562 {
563 GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
564 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
565 connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
566 signal_transmit_error (connection, ECONNREFUSED);
557 } 567 }
558} 568}
559 569
560 570
561/** 571/**
572 * We are ready to transmit (or got a timeout).
573 *
574 * @param cls our connection handle
575 * @param tc task context describing why we are here
576 */
577static void
578transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
579
580
581/**
582 * This function is called once we either timeout or have data ready
583 * to read.
584 *
585 * @param cls connection to read from
586 * @param tc scheduler context
587 */
588static void
589receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
590
591
592/**
562 * We've succeeded in establishing a connection. 593 * We've succeeded in establishing a connection.
563 * 594 *
564 * @param connection the connection we tried to establish 595 * @param connection the connection we tried to establish
@@ -566,27 +597,29 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
566static void 597static void
567connect_success_continuation (struct GNUNET_CONNECTION_Handle *connection) 598connect_success_continuation (struct GNUNET_CONNECTION_Handle *connection)
568{ 599{
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n", 600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Connection to `%s' succeeded! (%p)\n",
570 GNUNET_a2s (connection->addr, connection->addrlen), connection); 602 GNUNET_a2s (connection->addr, connection->addrlen), connection);
571 /* trigger jobs that waited for the connection */ 603 /* trigger jobs that waited for the connection */
572 if (0 != (connection->ccs & COCO_RECEIVE_AGAIN)) 604 if (NULL != connection->receiver)
573 { 605 {
574 LOG (GNUNET_ERROR_TYPE_DEBUG, 606 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 "connect_success_continuation runs receive_again (%p)\n", connection); 607 "Connection succeeded, starting with receiving data (%p)\n",
576 connection->ccs -= COCO_RECEIVE_AGAIN; 608 connection);
577 connection->read_task = GNUNET_SCHEDULER_add_now (&receive_again, connection); 609 connection->read_task =
610 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
611 (connection->receive_timeout), connection->sock,
612 &receive_ready, connection);
578 } 613 }
579 if (0 != (connection->ccs & COCO_TRANSMIT_READY)) 614 if (NULL != connection->nth.notify_ready)
580 { 615 {
581 LOG (GNUNET_ERROR_TYPE_DEBUG, 616 LOG (GNUNET_ERROR_TYPE_DEBUG,
582 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", 617 "Connection succeeded, starting with sending data (%p)\n",
583 connection); 618 connection);
584 GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 619 GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
585 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task); 620 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
586 connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 621 connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
587 connection->ccs -= COCO_TRANSMIT_READY;
588 GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK); 622 GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK);
589 GNUNET_assert (connection->nth.notify_ready != NULL);
590 connection->write_task = 623 connection->write_task =
591 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 624 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
592 (connection->nth.transmit_timeout), connection->sock, 625 (connection->nth.transmit_timeout), connection->sock,
@@ -612,7 +645,7 @@ connect_probe_continuation (void *cls,
612 int error; 645 int error;
613 socklen_t len; 646 socklen_t len;
614 647
615 GNUNET_assert (ap->sock != NULL); 648 GNUNET_assert (NULL != ap->sock);
616 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, ap); 649 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, ap);
617 len = sizeof (error); 650 len = sizeof (error);
618 errno = 0; 651 errno = 0;
@@ -620,17 +653,17 @@ connect_probe_continuation (void *cls,
620 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || 653 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
621 (GNUNET_OK != 654 (GNUNET_OK !=
622 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error, 655 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error,
623 &len)) || (error != 0)) 656 &len)) || (0 != error))
624 { 657 {
625 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 658 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
626 GNUNET_free (ap); 659 GNUNET_free (ap);
627 if ((NULL == connection->ap_head) && (connection->dns_active == GNUNET_NO)) 660 if ((NULL == connection->ap_head) && (GNUNET_NO == connection->dns_active))
628 connect_fail_continuation (connection); 661 connect_fail_continuation (connection);
629 return; 662 return;
630 } 663 }
631 GNUNET_assert (connection->sock == NULL); 664 GNUNET_assert (NULL == connection->sock);
632 connection->sock = ap->sock; 665 connection->sock = ap->sock;
633 GNUNET_assert (connection->addr == NULL); 666 GNUNET_assert (NULL == connection->addr);
634 connection->addr = GNUNET_malloc (ap->addrlen); 667 connection->addr = GNUNET_malloc (ap->addrlen);
635 memcpy (connection->addr, ap->addr, ap->addrlen); 668 memcpy (connection->addr, ap->addr, ap->addrlen);
636 connection->addrlen = ap->addrlen; 669 connection->addrlen = ap->addrlen;
@@ -663,16 +696,16 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
663 struct AddressProbe *ap; 696 struct AddressProbe *ap;
664 struct GNUNET_TIME_Relative delay; 697 struct GNUNET_TIME_Relative delay;
665 698
666 if (addr == NULL) 699 if (NULL == addr)
667 { 700 {
668 connection->dns_active = NULL; 701 connection->dns_active = NULL;
669 if ((NULL == connection->ap_head) && (NULL == connection->sock)) 702 if ((NULL == connection->ap_head) && (NULL == connection->sock))
670 connect_fail_continuation (connection); 703 connect_fail_continuation (connection);
671 return; 704 return;
672 } 705 }
673 if (connection->sock != NULL) 706 if (NULL != connection->sock)
674 return; /* already connected */ 707 return; /* already connected */
675 GNUNET_assert (connection->addr == NULL); 708 GNUNET_assert (NULL == connection->addr);
676 /* try to connect */ 709 /* try to connect */
677 LOG (GNUNET_ERROR_TYPE_DEBUG, 710 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "Trying to connect using address `%s:%u/%s:%u'\n", connection->hostname, connection->port, 711 "Trying to connect using address `%s:%u/%s:%u'\n", connection->hostname, connection->port,
@@ -697,7 +730,7 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
697 return; /* not supported by us */ 730 return; /* not supported by us */
698 } 731 }
699 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0); 732 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
700 if (ap->sock == NULL) 733 if (NULL == ap->sock)
701 { 734 {
702 GNUNET_free (ap); 735 GNUNET_free (ap);
703 return; /* not supported by OS */ 736 return; /* not supported by OS */
@@ -706,7 +739,7 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
706 GNUNET_a2s (ap->addr, ap->addrlen), connection); 739 GNUNET_a2s (ap->addr, ap->addrlen), connection);
707 if ((GNUNET_OK != 740 if ((GNUNET_OK !=
708 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && 741 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
709 (errno != EINPROGRESS)) 742 (EINPROGRESS != errno))
710 { 743 {
711 /* maybe refused / unsupported address, try next */ 744 /* maybe refused / unsupported address, try next */
712 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); 745 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
@@ -720,12 +753,12 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
720 } 753 }
721 GNUNET_CONTAINER_DLL_insert (connection->ap_head, connection->ap_tail, ap); 754 GNUNET_CONTAINER_DLL_insert (connection->ap_head, connection->ap_tail, ap);
722 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT; 755 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
723 if (connection->nth.notify_ready != NULL) 756 if (NULL != connection->nth.notify_ready)
724 delay = 757 delay =
725 GNUNET_TIME_relative_min (delay, 758 GNUNET_TIME_relative_min (delay,
726 GNUNET_TIME_absolute_get_remaining (connection-> 759 GNUNET_TIME_absolute_get_remaining (connection->
727 nth.transmit_timeout)); 760 nth.transmit_timeout));
728 if (connection->receiver != NULL) 761 if (NULL != connection->receiver)
729 delay = 762 delay =
730 GNUNET_TIME_relative_min (delay, 763 GNUNET_TIME_relative_min (delay,
731 GNUNET_TIME_absolute_get_remaining 764 GNUNET_TIME_absolute_get_remaining
@@ -852,15 +885,14 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
852 struct GNUNET_NETWORK_Handle *s; 885 struct GNUNET_NETWORK_Handle *s;
853 struct GNUNET_CONNECTION_Handle *connection; 886 struct GNUNET_CONNECTION_Handle *connection;
854 887
855
856 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); 888 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
857 if (s == NULL) 889 if (NULL == s)
858 { 890 {
859 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket"); 891 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket");
860 return NULL; 892 return NULL;
861 } 893 }
862 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && 894 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
863 (errno != EINPROGRESS)) 895 (EINPROGRESS != errno))
864 { 896 {
865 /* maybe refused / unsupported address, try next */ 897 /* maybe refused / unsupported address, try next */
866 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); 898 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
@@ -890,9 +922,9 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
890int 922int
891GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection) 923GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection)
892{ 924{
893 if ((connection->ap_head != NULL) || (connection->dns_active != NULL)) 925 if ((NULL != connection->ap_head) || (NULL != connection->dns_active))
894 return GNUNET_YES; /* still trying to connect */ 926 return GNUNET_YES; /* still trying to connect */
895 return (connection->sock == NULL) ? GNUNET_NO : GNUNET_YES; 927 return (NULL == connection->sock) ? GNUNET_NO : GNUNET_YES;
896} 928}
897 929
898 930
@@ -911,19 +943,18 @@ GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection)
911 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection); 943 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection);
912 GNUNET_assert (NULL == connection->nth.notify_ready); 944 GNUNET_assert (NULL == connection->nth.notify_ready);
913 GNUNET_assert (NULL == connection->receiver); 945 GNUNET_assert (NULL == connection->receiver);
914 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->read_task); 946 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
915 if (connection->write_task != GNUNET_SCHEDULER_NO_TASK)
916 { 947 {
917 GNUNET_SCHEDULER_cancel (connection->write_task); 948 GNUNET_SCHEDULER_cancel (connection->write_task);
918 connection->write_task = GNUNET_SCHEDULER_NO_TASK; 949 connection->write_task = GNUNET_SCHEDULER_NO_TASK;
919 connection->write_buffer_off = 0; 950 connection->write_buffer_off = 0;
920 } 951 }
921 if (connection->read_task != GNUNET_SCHEDULER_NO_TASK) 952 if (GNUNET_SCHEDULER_NO_TASK != connection->read_task)
922 { 953 {
923 GNUNET_SCHEDULER_cancel (connection->read_task); 954 GNUNET_SCHEDULER_cancel (connection->read_task);
924 connection->read_task = GNUNET_SCHEDULER_NO_TASK; 955 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
925 } 956 }
926 if (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK) 957 if (GNUNET_SCHEDULER_NO_TASK != connection->nth.timeout_task)
927 { 958 {
928 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task); 959 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
929 connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 960 connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
@@ -942,16 +973,16 @@ GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection)
942 GNUNET_free (pos); 973 GNUNET_free (pos);
943 } 974 }
944 if ( (NULL != connection->sock) && 975 if ( (NULL != connection->sock) &&
945 (connection->persist != GNUNET_YES) ) 976 (GNUNET_YES != connection->persist) )
946 { 977 {
947 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR)) && 978 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR)) &&
948 (errno != ENOTCONN) && 979 (ENOTCONN != errno) &&
949 (errno != ECONNRESET) ) 980 (ECONNRESET != errno) )
950 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown"); 981 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
951 } 982 }
952 if (connection->sock != NULL) 983 if (NULL != connection->sock)
953 { 984 {
954 if (connection->persist != GNUNET_YES) 985 if (GNUNET_YES != connection->persist)
955 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock)); 986 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
956 else 987 else
957 GNUNET_free (connection->sock); /* at least no memory leak (we deliberately 988 GNUNET_free (connection->sock); /* at least no memory leak (we deliberately
@@ -965,92 +996,54 @@ GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection)
965 996
966 997
967/** 998/**
968 * Tell the receiver callback that a timeout was reached.
969 */
970static void
971signal_timeout (struct GNUNET_CONNECTION_Handle *connection)
972{
973 GNUNET_CONNECTION_Receiver receiver;
974
975 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network signals time out to receiver (%p)!\n",
976 connection);
977 GNUNET_assert (NULL != (receiver = connection->receiver));
978 connection->receiver = NULL;
979 receiver (connection->receiver_cls, NULL, 0, NULL, 0, 0);
980}
981
982
983/**
984 * Tell the receiver callback that we had an IO error.
985 */
986static void
987signal_error (struct GNUNET_CONNECTION_Handle *connection, int errcode)
988{
989 GNUNET_CONNECTION_Receiver receiver;
990
991 GNUNET_assert (NULL != (receiver = connection->receiver));
992 connection->receiver = NULL;
993 receiver (connection->receiver_cls, NULL, 0, connection->addr, connection->addrlen, errcode);
994}
995
996
997/**
998 * This function is called once we either timeout 999 * This function is called once we either timeout
999 * or have data ready to read. 1000 * or have data ready to read.
1001 *
1002 * @param cls connection to read from
1003 * @param tc scheduler context
1000 */ 1004 */
1001static void 1005static void
1002receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 1006receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1003{ 1007{
1004 struct GNUNET_CONNECTION_Handle *connection = cls; 1008 struct GNUNET_CONNECTION_Handle *connection = cls;
1005 struct GNUNET_TIME_Absolute now;
1006 char buffer[connection->max]; 1009 char buffer[connection->max];
1007 ssize_t ret; 1010 ssize_t ret;
1008 GNUNET_CONNECTION_Receiver receiver; 1011 GNUNET_CONNECTION_Receiver receiver;
1009 1012
1010 connection->read_task = GNUNET_SCHEDULER_NO_TASK; 1013 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1011 if ((GNUNET_YES == connection->ignore_shutdown) && 1014 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1012 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1013 { 1015 {
1014 /* ignore shutdown request, go again immediately */ 1016 /* ignore shutdown request, go again immediately */
1015 LOG (GNUNET_ERROR_TYPE_DEBUG,
1016 "Ignoring shutdown signal per configuration\n");
1017 connection->read_task = 1017 connection->read_task =
1018 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1018 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1019 (connection->receive_timeout), connection->sock, 1019 (connection->receive_timeout), connection->sock,
1020 &receive_ready, connection); 1020 &receive_ready, connection);
1021 return; 1021 return;
1022 } 1022 }
1023 now = GNUNET_TIME_absolute_get (); 1023 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1024 if ((now.abs_value > connection->receive_timeout.abs_value) ||
1025 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1026 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1027 { 1024 {
1028 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1025 LOG (GNUNET_ERROR_TYPE_DEBUG,
1029 LOG (GNUNET_ERROR_TYPE_DEBUG, 1026 "Receive from `%s' encounters error: timeout (%p)\n",
1030 "Receive from `%s' encounters error: time out by %llums... (%p)\n", 1027 GNUNET_a2s (connection->addr, connection->addrlen),
1031 GNUNET_a2s (connection->addr, connection->addrlen), 1028 GNUNET_TIME_absolute_get_duration (connection->receive_timeout).rel_value,
1032 GNUNET_TIME_absolute_get_duration (connection->receive_timeout).rel_value, 1029 connection);
1033 connection); 1030 signal_receive_timeout (connection);
1034 signal_timeout (connection);
1035 return; 1031 return;
1036 } 1032 }
1037 if (connection->sock == NULL) 1033 if (NULL == connection->sock)
1038 { 1034 {
1039 /* connect failed for good */ 1035 /* connect failed for good */
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, 1036 signal_receive_error (connection, ECONNREFUSED);
1041 "Receive encounters error, connection closed... (%p)\n", connection);
1042 signal_error (connection, ECONNREFUSED);
1043 return; 1037 return;
1044 } 1038 }
1045 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, connection->sock)); 1039 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, connection->sock));
1046RETRY: 1040RETRY:
1047 ret = GNUNET_NETWORK_socket_recv (connection->sock, buffer, connection->max); 1041 ret = GNUNET_NETWORK_socket_recv (connection->sock, buffer, connection->max);
1048 if (ret == -1) 1042 if (-1 == ret)
1049 { 1043 {
1050 if (errno == EINTR) 1044 if (EINTR == errno)
1051 goto RETRY; 1045 goto RETRY;
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno)); 1046 signal_receive_error (connection, errno);
1053 signal_error (connection, errno);
1054 return; 1047 return;
1055 } 1048 }
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, 1049 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1063,49 +1056,6 @@ RETRY:
1063 1056
1064 1057
1065/** 1058/**
1066 * This function is called after establishing a connection either has
1067 * succeeded or timed out. Note that it is possible that the attempt
1068 * timed out and that we're immediately retrying. If we are retrying,
1069 * we need to wait again (or timeout); if we succeeded, we need to
1070 * wait for data (or timeout).
1071 *
1072 * @param cls our connection handle
1073 * @param tc task context describing why we are here
1074 */
1075static void
1076receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1077{
1078 struct GNUNET_CONNECTION_Handle *connection = cls;
1079 struct GNUNET_TIME_Absolute now;
1080
1081 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1082 if (connection->sock == NULL)
1083 {
1084 /* not connected and no longer trying */
1085 LOG (GNUNET_ERROR_TYPE_DEBUG,
1086 "Receive encounters error, connection closed (%p)...\n", connection);
1087 signal_error (connection, ECONNREFUSED);
1088 return;
1089 }
1090 now = GNUNET_TIME_absolute_get ();
1091 if ((now.abs_value > connection->receive_timeout.abs_value) ||
1092 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1093 {
1094 LOG (GNUNET_ERROR_TYPE_DEBUG,
1095 "Receive encounters error: time out (%p)...\n", connection);
1096 signal_timeout (connection);
1097 return;
1098 }
1099 GNUNET_assert (connection->sock != NULL);
1100 /* connect succeeded, wait for data! */
1101 connection->read_task =
1102 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1103 (connection->receive_timeout), connection->sock,
1104 &receive_ready, connection);
1105}
1106
1107
1108/**
1109 * Receive data from the given connection. Note that this function will 1059 * Receive data from the given connection. Note that this function will
1110 * call "receiver" asynchronously using the scheduler. It will 1060 * call "receiver" asynchronously using the scheduler. It will
1111 * "immediately" return. Note that there MUST only be one active 1061 * "immediately" return. Note that there MUST only be one active
@@ -1124,42 +1074,25 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection, size_t m
1124 GNUNET_CONNECTION_Receiver receiver, 1074 GNUNET_CONNECTION_Receiver receiver,
1125 void *receiver_cls) 1075 void *receiver_cls)
1126{ 1076{
1127 struct GNUNET_SCHEDULER_TaskContext tc; 1077 GNUNET_assert ((GNUNET_SCHEDULER_NO_TASK == connection->read_task) &&
1128 1078 (NULL == connection->receiver));
1129 GNUNET_assert ((connection->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1130 (0 == (connection->ccs & COCO_RECEIVE_AGAIN)) &&
1131 (connection->receiver == NULL));
1132 connection->receiver = receiver; 1079 connection->receiver = receiver;
1133 connection->receiver_cls = receiver_cls; 1080 connection->receiver_cls = receiver_cls;
1134 connection->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 1081 connection->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1135 connection->max = max; 1082 connection->max = max;
1136 if (connection->sock != NULL) 1083 if (NULL != connection->sock)
1137 { 1084 {
1138 memset (&tc, 0, sizeof (tc)); 1085 connection->read_task =
1139 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; 1086 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1140 receive_again (connection, &tc); 1087 (connection->receive_timeout), connection->sock,
1088 &receive_ready, connection);
1141 return; 1089 return;
1142 } 1090 }
1143 if ((connection->dns_active == NULL) && (connection->ap_head == NULL)) 1091 if ((NULL == connection->dns_active) && (NULL == connection->ap_head))
1144 { 1092 {
1145 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); 1093 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1146 return; 1094 return;
1147 } 1095 }
1148 connection->ccs += COCO_RECEIVE_AGAIN;
1149}
1150
1151
1152/**
1153 * Configure this connection to ignore shutdown signals.
1154 *
1155 * @param connection connection handle
1156 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1157 */
1158void
1159GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection,
1160 int do_ignore)
1161{
1162 connection->ignore_shutdown = do_ignore;
1163} 1096}
1164 1097
1165 1098
@@ -1174,16 +1107,11 @@ GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection,
1174void * 1107void *
1175GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection) 1108GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection)
1176{ 1109{
1177 if (connection->read_task != GNUNET_SCHEDULER_NO_TASK) 1110 if (GNUNET_SCHEDULER_NO_TASK == connection->read_task)
1178 { 1111 {
1179 GNUNET_assert (connection == GNUNET_SCHEDULER_cancel (connection->read_task)); 1112 GNUNET_assert (connection == GNUNET_SCHEDULER_cancel (connection->read_task));
1180 connection->read_task = GNUNET_SCHEDULER_NO_TASK; 1113 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1181 } 1114 }
1182 else
1183 {
1184 GNUNET_assert (0 != (connection->ccs & COCO_RECEIVE_AGAIN));
1185 connection->ccs -= COCO_RECEIVE_AGAIN;
1186 }
1187 connection->receiver = NULL; 1115 connection->receiver = NULL;
1188 return connection->receiver_cls; 1116 return connection->receiver_cls;
1189} 1117}
@@ -1204,7 +1132,7 @@ process_notify (struct GNUNET_CONNECTION_Handle *connection)
1204 size_t size; 1132 size_t size;
1205 GNUNET_CONNECTION_TransmitReadyNotify notify; 1133 GNUNET_CONNECTION_TransmitReadyNotify notify;
1206 1134
1207 GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK); 1135 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
1208 if (NULL == (notify = connection->nth.notify_ready)) 1136 if (NULL == (notify = connection->nth.notify_ready))
1209 return GNUNET_NO; 1137 return GNUNET_NO;
1210 used = connection->write_buffer_off - connection->write_buffer_pos; 1138 used = connection->write_buffer_off - connection->write_buffer_pos;
@@ -1255,9 +1183,8 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1255 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", 1183 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1256 connection->hostname, 1184 connection->hostname,
1257 connection->port, GNUNET_a2s (connection->addr, connection->addrlen), connection); 1185 connection->port, GNUNET_a2s (connection->addr, connection->addrlen), connection);
1258 GNUNET_assert (0 != (connection->ccs & COCO_TRANSMIT_READY));
1259 connection->ccs -= COCO_TRANSMIT_READY; /* remove request */
1260 notify = connection->nth.notify_ready; 1186 notify = connection->nth.notify_ready;
1187 GNUNET_assert (NULL != notify);
1261 connection->nth.notify_ready = NULL; 1188 connection->nth.notify_ready = NULL;
1262 notify (connection->nth.notify_ready_cls, 0, NULL); 1189 notify (connection->nth.notify_ready_cls, 0, NULL);
1263} 1190}
@@ -1289,41 +1216,7 @@ connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1289 1216
1290 1217
1291/** 1218/**
1292 * FIXME 1219 * We are ready to transmit (or got a timeout).
1293 *
1294 * @param connection FIXME
1295 */
1296static void
1297transmit_error (struct GNUNET_CONNECTION_Handle *connection)
1298{
1299 GNUNET_CONNECTION_TransmitReadyNotify notify;
1300
1301 if (NULL != connection->sock)
1302 {
1303 GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR);
1304 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
1305 connection->sock = NULL;
1306 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
1307 }
1308 if (connection->read_task != GNUNET_SCHEDULER_NO_TASK)
1309 {
1310 GNUNET_SCHEDULER_cancel (connection->read_task);
1311 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1312 signal_timeout (connection);
1313 return;
1314 }
1315 if (connection->nth.notify_ready == NULL)
1316 return; /* nobody to tell about it */
1317 notify = connection->nth.notify_ready;
1318 connection->nth.notify_ready = NULL;
1319 notify (connection->nth.notify_ready_cls, 0, NULL);
1320}
1321
1322
1323/**
1324 * See if we are now connected. If not, wait longer for
1325 * connect to succeed. If connected, we should be able
1326 * to write now as well, unless we timed out.
1327 * 1220 *
1328 * @param cls our connection handle 1221 * @param cls our connection handle
1329 * @param tc task context describing why we are here 1222 * @param tc task context describing why we are here
@@ -1337,12 +1230,12 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1337 size_t have; 1230 size_t have;
1338 1231
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", connection); 1232 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", connection);
1340 GNUNET_assert (connection->write_task != GNUNET_SCHEDULER_NO_TASK); 1233 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != connection->write_task);
1341 connection->write_task = GNUNET_SCHEDULER_NO_TASK; 1234 connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1342 GNUNET_assert (connection->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 1235 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task);
1343 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1236 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1344 { 1237 {
1345 if ((connection->ignore_shutdown == GNUNET_YES) && (NULL != connection->sock)) 1238 if (NULL != connection->sock)
1346 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ 1239 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1347 LOG (GNUNET_ERROR_TYPE_DEBUG, 1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1348 "Transmit to `%s' fails, shutdown happened (%p).\n", 1241 "Transmit to `%s' fails, shutdown happened (%p).\n",
@@ -1367,25 +1260,22 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1367 return; 1260 return;
1368 } 1261 }
1369 GNUNET_assert (NULL != connection->sock); 1262 GNUNET_assert (NULL != connection->sock);
1370 if (tc->write_ready == NULL) 1263 if (NULL == tc->write_ready)
1371 { 1264 {
1372 /* special circumstances (in particular, 1265 /* special circumstances (in particular, PREREQ_DONE after
1373 * PREREQ_DONE after connect): not yet ready to write, 1266 * connect): not yet ready to write, but no "fatal" error either.
1374 * but no "fatal" error either. Hence retry. */ 1267 * Hence retry. */
1375 goto SCHEDULE_WRITE; 1268 goto SCHEDULE_WRITE;
1376 } 1269 }
1377 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, connection->sock)) 1270 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, connection->sock))
1378 { 1271 {
1379 LOG (GNUNET_ERROR_TYPE_INFO,
1380 _
1381 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1382 connection);
1383 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task); 1272 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
1384 transmit_error (connection); 1273 /* special circumstances (in particular, shutdown): not yet ready
1385 return; /* connect failed for good, we're finished */ 1274 * to write, but no "fatal" error either. Hence retry. */
1275 goto SCHEDULE_WRITE;
1386 } 1276 }
1387 GNUNET_assert (connection->write_buffer_off >= connection->write_buffer_pos); 1277 GNUNET_assert (connection->write_buffer_off >= connection->write_buffer_pos);
1388 if ((connection->nth.notify_ready != NULL) && 1278 if ((NULL != connection->nth.notify_ready) &&
1389 (connection->write_buffer_size < connection->nth.notify_size)) 1279 (connection->write_buffer_size < connection->nth.notify_size))
1390 { 1280 {
1391 connection->write_buffer = 1281 connection->write_buffer =
@@ -1394,7 +1284,7 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1394 } 1284 }
1395 process_notify (connection); 1285 process_notify (connection);
1396 have = connection->write_buffer_off - connection->write_buffer_pos; 1286 have = connection->write_buffer_off - connection->write_buffer_pos;
1397 if (have == 0) 1287 if (0 == have)
1398 { 1288 {
1399 /* no data ready for writing, terminate write loop */ 1289 /* no data ready for writing, terminate write loop */
1400 return; 1290 return;
@@ -1407,21 +1297,20 @@ RETRY:
1407 GNUNET_NETWORK_socket_send (connection->sock, 1297 GNUNET_NETWORK_socket_send (connection->sock,
1408 &connection->write_buffer[connection->write_buffer_pos], 1298 &connection->write_buffer[connection->write_buffer_pos],
1409 have); 1299 have);
1410 if (ret == -1) 1300 if (-1 == ret)
1411 { 1301 {
1412 if (errno == EINTR) 1302 if (EINTR == errno)
1413 goto RETRY; 1303 goto RETRY;
1414 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1415 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task) 1304 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
1416 { 1305 {
1417 GNUNET_SCHEDULER_cancel (connection->write_task); 1306 GNUNET_SCHEDULER_cancel (connection->write_task);
1418 connection->write_task = GNUNET_SCHEDULER_NO_TASK; 1307 connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1419 } 1308 }
1420 transmit_error (connection); 1309 signal_transmit_error (connection, errno);
1421 return; 1310 return;
1422 } 1311 }
1423 LOG (GNUNET_ERROR_TYPE_DEBUG, 1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1424 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", 1313 "Connection transmitted %u/%u bytes to `%s' (%p)\n",
1425 (unsigned int) ret, have, GNUNET_a2s (connection->addr, connection->addrlen), connection); 1314 (unsigned int) ret, have, GNUNET_a2s (connection->addr, connection->addrlen), connection);
1426 connection->write_buffer_pos += ret; 1315 connection->write_buffer_pos += ret;
1427 if (connection->write_buffer_pos == connection->write_buffer_off) 1316 if (connection->write_buffer_pos == connection->write_buffer_off)
@@ -1430,15 +1319,15 @@ RETRY:
1430 connection->write_buffer_pos = 0; 1319 connection->write_buffer_pos = 0;
1431 connection->write_buffer_off = 0; 1320 connection->write_buffer_off = 0;
1432 } 1321 }
1433 if ((connection->write_buffer_off == 0) && (NULL == connection->nth.notify_ready)) 1322 if ((0 == connection->write_buffer_off) && (NULL == connection->nth.notify_ready))
1434 return; /* all data sent! */ 1323 return; /* all data sent! */
1435 /* not done writing, schedule more */ 1324 /* not done writing, schedule more */
1436SCHEDULE_WRITE: 1325SCHEDULE_WRITE:
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, 1326 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "Re-scheduling transmit_ready (more to do) (%p).\n", connection); 1327 "Re-scheduling transmit_ready (more to do) (%p).\n", connection);
1439 have = connection->write_buffer_off - connection->write_buffer_pos; 1328 have = connection->write_buffer_off - connection->write_buffer_pos;
1440 GNUNET_assert ((connection->nth.notify_ready != NULL) || (have > 0)); 1329 GNUNET_assert ((NULL != connection->nth.notify_ready) || (have > 0));
1441 if (connection->write_task == GNUNET_SCHEDULER_NO_TASK) 1330 if (GNUNET_SCHEDULER_NO_TASK == connection->write_task)
1442 connection->write_task = 1331 connection->write_task =
1443 GNUNET_SCHEDULER_add_write_net ((connection->nth.notify_ready == 1332 GNUNET_SCHEDULER_add_write_net ((connection->nth.notify_ready ==
1444 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : 1333 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
@@ -1469,12 +1358,12 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connec
1469 GNUNET_CONNECTION_TransmitReadyNotify 1358 GNUNET_CONNECTION_TransmitReadyNotify
1470 notify, void *notify_cls) 1359 notify, void *notify_cls)
1471{ 1360{
1472 if (connection->nth.notify_ready != NULL) 1361 if (NULL != connection->nth.notify_ready)
1473 { 1362 {
1474 GNUNET_assert (0); 1363 GNUNET_assert (0);
1475 return NULL; 1364 return NULL;
1476 } 1365 }
1477 GNUNET_assert (notify != NULL); 1366 GNUNET_assert (NULL != notify);
1478 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); 1367 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1479 GNUNET_assert (connection->write_buffer_off <= connection->write_buffer_size); 1368 GNUNET_assert (connection->write_buffer_off <= connection->write_buffer_size);
1480 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size); 1369 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size);
@@ -1485,32 +1374,32 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connec
1485 connection->nth.notify_size = size; 1374 connection->nth.notify_size = size;
1486 connection->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout); 1375 connection->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1487 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task); 1376 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task);
1488 if ((connection->sock == NULL) && (connection->ap_head == NULL) && 1377 if ((NULL == connection->sock) &&
1489 (connection->dns_active == NULL)) 1378 (NULL == connection->ap_head) &&
1379 (NULL == connection->dns_active))
1490 { 1380 {
1491 if (connection->write_task != GNUNET_SCHEDULER_NO_TASK) 1381 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
1492 GNUNET_SCHEDULER_cancel (connection->write_task); 1382 GNUNET_SCHEDULER_cancel (connection->write_task);
1493 connection->write_task = GNUNET_SCHEDULER_add_now (&connect_error, connection); 1383 connection->write_task = GNUNET_SCHEDULER_add_now (&connect_error, connection);
1494 return &connection->nth; 1384 return &connection->nth;
1495 } 1385 }
1496 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task) 1386 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
1497 return &connection->nth; 1387 return &connection->nth; /* previous transmission still in progress */
1498 if (connection->sock != NULL) 1388 if (NULL != connection->sock)
1499 { 1389 {
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", connection); 1390 /* connected, try to transmit now */
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmission (%p).\n", connection);
1501 connection->write_task = 1392 connection->write_task =
1502 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 1393 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1503 (connection->nth.transmit_timeout), 1394 (connection->nth.transmit_timeout),
1504 connection->sock, &transmit_ready, connection); 1395 connection->sock, &transmit_ready, connection);
1396 return &connection->nth;
1505 } 1397 }
1506 else 1398 /* not yet connected, wait for connection */
1507 { 1399 LOG (GNUNET_ERROR_TYPE_DEBUG,
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, 1400 "Need to wait to schedule transmission for connection, adding timeout task (%p).\n", connection);
1509 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", connection); 1401 connection->nth.timeout_task =
1510 connection->ccs |= COCO_TRANSMIT_READY; 1402 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection);
1511 connection->nth.timeout_task =
1512 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection);
1513 }
1514 return &connection->nth; 1403 return &connection->nth;
1515} 1404}
1516 1405
@@ -1525,24 +1414,18 @@ GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1525 GNUNET_CONNECTION_TransmitHandle 1414 GNUNET_CONNECTION_TransmitHandle
1526 *th) 1415 *th)
1527{ 1416{
1528 GNUNET_assert (th->notify_ready != NULL); 1417 GNUNET_assert (NULL != th->notify_ready);
1529 if (0 != (th->connection->ccs & COCO_TRANSMIT_READY)) 1418 th->notify_ready = NULL;
1419 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
1530 { 1420 {
1531 LOG (GNUNET_ERROR_TYPE_DEBUG,
1532 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1533 GNUNET_SCHEDULER_cancel (th->timeout_task); 1421 GNUNET_SCHEDULER_cancel (th->timeout_task);
1534 th->timeout_task = GNUNET_SCHEDULER_NO_TASK; 1422 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1535 th->connection->ccs -= COCO_TRANSMIT_READY;
1536 } 1423 }
1537 else 1424 if (GNUNET_SCHEDULER_NO_TASK != th->connection->write_task)
1538 { 1425 {
1539 if (th->connection->write_task != GNUNET_SCHEDULER_NO_TASK) 1426 GNUNET_SCHEDULER_cancel (th->connection->write_task);
1540 { 1427 th->connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1541 GNUNET_SCHEDULER_cancel (th->connection->write_task);
1542 th->connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1543 }
1544 } 1428 }
1545 th->notify_ready = NULL;
1546} 1429}
1547 1430
1548/* end of connection.c */ 1431/* end of connection.c */
diff --git a/src/util/crypto_hkdf.c b/src/util/crypto_hkdf.c
index 40bfa6774..c2b96778a 100644
--- a/src/util/crypto_hkdf.c
+++ b/src/util/crypto_hkdf.c
@@ -49,6 +49,7 @@
49#define DEBUG_HKDF 0 49#define DEBUG_HKDF 0
50 50
51 51
52
52#if GNUNET_BUILD 53#if GNUNET_BUILD
53#include "platform.h" 54#include "platform.h"
54#include "gnunet_crypto_lib.h" 55#include "gnunet_crypto_lib.h"
diff --git a/src/util/disk.c b/src/util/disk.c
index cebf6d033..667e350d9 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -40,10 +40,6 @@
40 40
41#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) 41#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 42
43#define DEBUG_NPIPE GNUNET_EXTRA_LOGGING
44
45#define DEBUG_PIPE GNUNET_EXTRA_LOGGING
46
47/** 43/**
48 * Block size for IO for copying files. 44 * Block size for IO for copying files.
49 */ 45 */
@@ -732,27 +728,18 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result,
732 } 728 }
733 else 729 else
734 { 730 {
735#if DEBUG_PIPE
736 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to read\n");
737#endif
738 if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead)) 731 if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead))
739 { 732 {
740 if (GetLastError () != ERROR_IO_PENDING) 733 if (GetLastError () != ERROR_IO_PENDING)
741 { 734 {
742#if DEBUG_PIPE
743 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ()); 735 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ());
744#endif
745 SetErrnoFromWinError (GetLastError ()); 736 SetErrnoFromWinError (GetLastError ());
746 return GNUNET_SYSERR; 737 return GNUNET_SYSERR;
747 } 738 }
748#if DEBUG_PIPE
749 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); 739 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
750#endif
751 GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); 740 GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE);
752 } 741 }
753#if DEBUG_PIPE 742 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from pipe\n", bytesRead);
754 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead);
755#endif
756 } 743 }
757 return bytesRead; 744 return bytesRead;
758#else 745#else
@@ -794,33 +781,24 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
794 } 781 }
795 else 782 else
796 { 783 {
797#if DEBUG_PIPE
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe, trying to read\n");
799#endif
800 if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead)) 784 if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead))
801 { 785 {
802 if (GetLastError () != ERROR_IO_PENDING) 786 if (GetLastError () != ERROR_IO_PENDING)
803 { 787 {
804#if DEBUG_PIPE
805 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ()); 788 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ());
806#endif
807 SetErrnoFromWinError (GetLastError ()); 789 SetErrnoFromWinError (GetLastError ());
808 return GNUNET_SYSERR; 790 return GNUNET_SYSERR;
809 } 791 }
810 else 792 else
811 { 793 {
812#if DEBUG_PIPE
813 LOG (GNUNET_ERROR_TYPE_DEBUG, 794 LOG (GNUNET_ERROR_TYPE_DEBUG,
814 "ReadFile() queued a read, cancelling\n"); 795 "ReadFile() queued a read, cancelling\n");
815#endif
816 CancelIo (h->h); 796 CancelIo (h->h);
817 errno = EAGAIN; 797 errno = EAGAIN;
818 return GNUNET_SYSERR; 798 return GNUNET_SYSERR;
819 } 799 }
820 } 800 }
821#if DEBUG_PIPE
822 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead); 801 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead);
823#endif
824 } 802 }
825 return bytesRead; 803 return bytesRead;
826#else 804#else
@@ -893,31 +871,23 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
893 } 871 }
894 else 872 else
895 { 873 {
896#if DEBUG_PIPE
897 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); 874 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n);
898#endif
899 if (!WriteFile (h->h, buffer, n, &bytesWritten, h->oOverlapWrite)) 875 if (!WriteFile (h->h, buffer, n, &bytesWritten, h->oOverlapWrite))
900 { 876 {
901 if (GetLastError () != ERROR_IO_PENDING) 877 if (GetLastError () != ERROR_IO_PENDING)
902 { 878 {
903 SetErrnoFromWinError (GetLastError ()); 879 SetErrnoFromWinError (GetLastError ());
904#if DEBUG_PIPE
905 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", 880 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
906 GetLastError ()); 881 GetLastError ());
907#endif
908 return GNUNET_SYSERR; 882 return GNUNET_SYSERR;
909 } 883 }
910#if DEBUG_PIPE
911 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); 884 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
912#endif
913 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE)) 885 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE))
914 { 886 {
915 SetErrnoFromWinError (GetLastError ()); 887 SetErrnoFromWinError (GetLastError ());
916#if DEBUG_PIPE
917 LOG (GNUNET_ERROR_TYPE_DEBUG, 888 LOG (GNUNET_ERROR_TYPE_DEBUG,
918 "Error getting overlapped result while writing to pipe: %u\n", 889 "Error getting overlapped result while writing to pipe: %u\n",
919 GetLastError ()); 890 GetLastError ());
920#endif
921 return GNUNET_SYSERR; 891 return GNUNET_SYSERR;
922 } 892 }
923 } 893 }
@@ -926,35 +896,27 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
926 DWORD ovr; 896 DWORD ovr;
927 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE)) 897 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE))
928 { 898 {
929#if DEBUG_PIPE
930 LOG (GNUNET_ERROR_TYPE_DEBUG, 899 LOG (GNUNET_ERROR_TYPE_DEBUG,
931 "Error getting control overlapped result while writing to pipe: %u\n", 900 "Error getting control overlapped result while writing to pipe: %u\n",
932 GetLastError ()); 901 GetLastError ());
933#endif
934 } 902 }
935 else 903 else
936 { 904 {
937#if DEBUG_PIPE
938 LOG (GNUNET_ERROR_TYPE_DEBUG, 905 LOG (GNUNET_ERROR_TYPE_DEBUG,
939 "Wrote %u bytes (ovr says %u), picking the greatest\n", 906 "Wrote %u bytes (ovr says %u), picking the greatest\n",
940 bytesWritten, ovr); 907 bytesWritten, ovr);
941#endif
942 } 908 }
943 } 909 }
944 if (bytesWritten == 0) 910 if (bytesWritten == 0)
945 { 911 {
946 if (n > 0) 912 if (n > 0)
947 { 913 {
948#if DEBUG_PIPE
949 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytesWritten); 914 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytesWritten);
950#endif
951 errno = EAGAIN; 915 errno = EAGAIN;
952 return GNUNET_SYSERR; 916 return GNUNET_SYSERR;
953 } 917 }
954 } 918 }
955#if DEBUG_PIPE
956 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten); 919 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten);
957#endif
958 } 920 }
959 return bytesWritten; 921 return bytesWritten;
960#else 922#else
@@ -983,37 +945,27 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
983#ifdef MINGW 945#ifdef MINGW
984 DWORD bytesWritten; 946 DWORD bytesWritten;
985 /* We do a non-overlapped write, which is as blocking as it gets */ 947 /* We do a non-overlapped write, which is as blocking as it gets */
986#if DEBUG_PIPE
987 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n); 948 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n);
988#endif
989 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) 949 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
990 { 950 {
991 SetErrnoFromWinError (GetLastError ()); 951 SetErrnoFromWinError (GetLastError ());
992#if DEBUG_PIPE
993 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", 952 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
994 GetLastError ()); 953 GetLastError ());
995#endif
996 return GNUNET_SYSERR; 954 return GNUNET_SYSERR;
997 } 955 }
998 if (bytesWritten == 0 && n > 0) 956 if (bytesWritten == 0 && n > 0)
999 { 957 {
1000#if DEBUG_PIPE
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n"); 958 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n");
1002#endif
1003 WaitForSingleObject (h->h, INFINITE); 959 WaitForSingleObject (h->h, INFINITE);
1004 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) 960 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
1005 { 961 {
1006 SetErrnoFromWinError (GetLastError ()); 962 SetErrnoFromWinError (GetLastError ());
1007#if DEBUG_PIPE
1008 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", 963 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
1009 GetLastError ()); 964 GetLastError ());
1010#endif
1011 return GNUNET_SYSERR; 965 return GNUNET_SYSERR;
1012 } 966 }
1013 } 967 }
1014#if DEBUG_PIPE
1015 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten); 968 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten);
1016#endif
1017 return bytesWritten; 969 return bytesWritten;
1018#else 970#else
1019 int flags; 971 int flags;
@@ -2021,10 +1973,8 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2021 1973
2022 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", 1974 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld",
2023 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); 1975 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id));
2024#if DEBUG_PIPE
2025 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n", 1976 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n",
2026 pipename, psize); 1977 pipename, psize);
2027#endif
2028 /* Use CreateNamedPipe instead of CreatePipe, because the latter 1978 /* Use CreateNamedPipe instead of CreatePipe, because the latter
2029 * returns a write handle that does not permit FILE_READ_ATTRIBUTES 1979 * returns a write handle that does not permit FILE_READ_ATTRIBUTES
2030 * access, on versions of win32 earlier than WinXP SP2. 1980 * access, on versions of win32 earlier than WinXP SP2.
@@ -2041,9 +1991,7 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2041 1991
2042 if (read_pipe != INVALID_HANDLE_VALUE) 1992 if (read_pipe != INVALID_HANDLE_VALUE)
2043 { 1993 {
2044#if DEBUG_PIPE
2045 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe); 1994 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe);
2046#endif
2047 break; 1995 break;
2048 } 1996 }
2049 1997
@@ -2054,33 +2002,24 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2054 case ERROR_PIPE_BUSY: 2002 case ERROR_PIPE_BUSY:
2055 /* The pipe is already open with compatible parameters. 2003 /* The pipe is already open with compatible parameters.
2056 * Pick a new name and retry. */ 2004 * Pick a new name and retry. */
2057#if DEBUG_PIPE
2058 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); 2005 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n");
2059#endif
2060 continue; 2006 continue;
2061 case ERROR_ACCESS_DENIED: 2007 case ERROR_ACCESS_DENIED:
2062 /* The pipe is already open with incompatible parameters. 2008 /* The pipe is already open with incompatible parameters.
2063 * Pick a new name and retry. */ 2009 * Pick a new name and retry. */
2064#if DEBUG_PIPE
2065 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); 2010 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n");
2066#endif
2067 continue; 2011 continue;
2068 case ERROR_CALL_NOT_IMPLEMENTED: 2012 case ERROR_CALL_NOT_IMPLEMENTED:
2069 /* We are on an older Win9x platform without named pipes. 2013 /* We are on an older Win9x platform without named pipes.
2070 * Return an anonymous pipe as the best approximation. */ 2014 * Return an anonymous pipe as the best approximation. */
2071#if DEBUG_PIPE
2072 LOG (GNUNET_ERROR_TYPE_DEBUG, 2015 LOG (GNUNET_ERROR_TYPE_DEBUG,
2073 "CreateNamedPipe not implemented, resorting to " 2016 "CreateNamedPipe not implemented, resorting to "
2074 "CreatePipe: size = %lu\n", psize); 2017 "CreatePipe: size = %lu\n", psize);
2075#endif
2076 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) 2018 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize))
2077 { 2019 {
2078#if DEBUG_PIPE 2020 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p, write handle = %p\n",
2079 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", 2021 *read_pipe_ptr,
2080 *read_pipe_ptr);
2081 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n",
2082 *write_pipe_ptr); 2022 *write_pipe_ptr);
2083#endif
2084 return GNUNET_OK; 2023 return GNUNET_OK;
2085 } 2024 }
2086 err = GetLastError (); 2025 err = GetLastError ();
@@ -2092,9 +2031,7 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2092 } 2031 }
2093 /* NOTREACHED */ 2032 /* NOTREACHED */
2094 } 2033 }
2095#if DEBUG_PIPE
2096 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); 2034 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename);
2097#endif
2098 2035
2099 /* Open the named pipe for writing. 2036 /* Open the named pipe for writing.
2100 * Be sure to permit FILE_READ_ATTRIBUTES access. */ 2037 * Be sure to permit FILE_READ_ATTRIBUTES access. */
@@ -2107,15 +2044,11 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2107 /* Failure. */ 2044 /* Failure. */
2108 DWORD err = GetLastError (); 2045 DWORD err = GetLastError ();
2109 2046
2110#if DEBUG_PIPE
2111 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); 2047 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err);
2112#endif
2113 CloseHandle (read_pipe); 2048 CloseHandle (read_pipe);
2114 return err; 2049 return err;
2115 } 2050 }
2116#if DEBUG_PIPE
2117 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); 2051 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe);
2118#endif
2119 /* Success. */ 2052 /* Success. */
2120 *read_pipe_ptr = read_pipe; 2053 *read_pipe_ptr = read_pipe;
2121 *write_pipe_ptr = write_pipe; 2054 *write_pipe_ptr = write_pipe;
diff --git a/src/util/load.c b/src/util/load.c
index e978a950d..146e85095 100644
--- a/src/util/load.c
+++ b/src/util/load.c
@@ -26,7 +26,6 @@
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_load_lib.h" 27#include "gnunet_load_lib.h"
28 28
29#define DEBUG_LOAD GNUNET_EXTRA_LOGGING
30 29
31#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) 30#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 31
diff --git a/src/util/network.c b/src/util/network.c
index d398d6f11..54abcaba1 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -1024,9 +1024,6 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1024 if (GNUNET_CONTAINER_slist_contains 1024 if (GNUNET_CONTAINER_slist_contains
1025 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle))) 1025 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1026 { 1026 {
1027#if DEBUG_NETWORK
1028 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1029#endif
1030 return GNUNET_YES; 1027 return GNUNET_YES;
1031 } 1028 }
1032 GNUNET_CONTAINER_slist_next (&it); 1029 GNUNET_CONTAINER_slist_next (&it);
diff --git a/src/util/server.c b/src/util/server.c
index 5a2eee28d..6a458b35b 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors) 3 (C) 2009, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -26,11 +26,7 @@
26 26
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29#include "gnunet_connection_lib.h" 29#include "gnunet_util_lib.h"
30#include "gnunet_scheduler_lib.h"
31#include "gnunet_server_lib.h"
32#include "gnunet_time_lib.h"
33#include "gnunet_disk_lib.h"
34#include "gnunet_protocols.h" 30#include "gnunet_protocols.h"
35 31
36#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) 32#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
@@ -63,11 +59,16 @@ struct HandlerList
63struct NotifyList 59struct NotifyList
64{ 60{
65 /** 61 /**
66 * This is a linked list. 62 * This is a doubly linked list.
67 */ 63 */
68 struct NotifyList *next; 64 struct NotifyList *next;
69 65
70 /** 66 /**
67 * This is a doubly linked list.
68 */
69 struct NotifyList *prev;
70
71 /**
71 * Function to call. 72 * Function to call.
72 */ 73 */
73 GNUNET_SERVER_DisconnectCallback callback; 74 GNUNET_SERVER_DisconnectCallback callback;
@@ -95,9 +96,14 @@ struct GNUNET_SERVER_Handle
95 struct GNUNET_SERVER_Client *clients; 96 struct GNUNET_SERVER_Client *clients;
96 97
97 /** 98 /**
98 * Linked list of functions to call on disconnects by clients. 99 * Head of linked list of functions to call on disconnects by clients.
99 */ 100 */
100 struct NotifyList *disconnect_notify_list; 101 struct NotifyList *disconnect_notify_list_head;
102
103 /**
104 * Tail of linked list of functions to call on disconnects by clients.
105 */
106 struct NotifyList *disconnect_notify_list_tail;
101 107
102 /** 108 /**
103 * Function to call for access control. 109 * Function to call for access control.
@@ -133,13 +139,6 @@ struct GNUNET_SERVER_Handle
133 int require_found; 139 int require_found;
134 140
135 /** 141 /**
136 * Should all of the clients of this server continue to process
137 * connections as usual even if we get a shutdown request? (the
138 * listen socket always ignores shutdown).
139 */
140 int clients_ignore_shutdown;
141
142 /**
143 * Alternative function to create a MST instance. 142 * Alternative function to create a MST instance.
144 */ 143 */
145 GNUNET_SERVER_MstCreateCallback mst_create; 144 GNUNET_SERVER_MstCreateCallback mst_create;
@@ -338,12 +337,10 @@ process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 GNUNET_CONNECTION_create_from_accept (server->access, 337 GNUNET_CONNECTION_create_from_accept (server->access,
339 server->access_cls, 338 server->access_cls,
340 server->listen_sockets[i]); 339 server->listen_sockets[i]);
341 if (sock != NULL) 340 if (NULL != sock)
342 { 341 {
343 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n"); 342 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n");
344 client = GNUNET_SERVER_connect_socket (server, sock); 343 client = GNUNET_SERVER_connect_socket (server, sock);
345 GNUNET_CONNECTION_ignore_shutdown (sock,
346 server->clients_ignore_shutdown);
347 /* decrement reference count, we don't keep "client" alive */ 344 /* decrement reference count, we don't keep "client" alive */
348 GNUNET_SERVER_client_drop (client); 345 GNUNET_SERVER_client_drop (client);
349 } 346 }
@@ -397,14 +394,14 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
397 errno = 0; 394 errno = 0;
398 return NULL; 395 return NULL;
399 } 396 }
400 if (port != 0) 397 if (0 != port)
401 { 398 {
402 if (GNUNET_NETWORK_socket_setsockopt 399 if (GNUNET_NETWORK_socket_setsockopt
403 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 400 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
404 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 401 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
405 "setsockopt"); 402 "setsockopt");
406#ifdef IPV6_V6ONLY 403#ifdef IPV6_V6ONLY
407 if ((serverAddr->sa_family == AF_INET6) && 404 if ((AF_INET6 == serverAddr->sa_family) &&
408 (GNUNET_NETWORK_socket_setsockopt 405 (GNUNET_NETWORK_socket_setsockopt
409 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) 406 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
410 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 407 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
@@ -412,30 +409,30 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
412#endif 409#endif
413 } 410 }
414 /* bind the socket */ 411 /* bind the socket */
415 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK) 412 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen))
416 { 413 {
417 eno = errno; 414 eno = errno;
418 if (errno != EADDRINUSE) 415 if (EADDRINUSE != errno)
419 { 416 {
420 /* we don't log 'EADDRINUSE' here since an IPv4 bind may 417 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
421 * fail if we already took the port on IPv6; if both IPv4 and 418 * fail if we already took the port on IPv6; if both IPv4 and
422 * IPv6 binds fail, then our caller will log using the 419 * IPv6 binds fail, then our caller will log using the
423 * errno preserved in 'eno' */ 420 * errno preserved in 'eno' */
424 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind"); 421 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
425 if (port != 0) 422 if (0 != port)
426 LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"), 423 LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"),
427 "bind", port, 424 "bind", port,
428 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); 425 (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
429 eno = 0; 426 eno = 0;
430 } 427 }
431 else 428 else
432 { 429 {
433 if (port != 0) 430 if (0 != port)
434 LOG (GNUNET_ERROR_TYPE_WARNING, 431 LOG (GNUNET_ERROR_TYPE_WARNING,
435 _("`%s' failed for port %d (%s): address already in use\n"), 432 _("`%s' failed for port %d (%s): address already in use\n"),
436 "bind", port, 433 "bind", port,
437 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); 434 (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
438 else if (serverAddr->sa_family == AF_UNIX) 435 else if (AF_UNIX == serverAddr->sa_family)
439 LOG (GNUNET_ERROR_TYPE_WARNING, 436 LOG (GNUNET_ERROR_TYPE_WARNING,
440 _("`%s' failed for `%s': address already in use\n"), "bind", 437 _("`%s' failed for `%s': address already in use\n"), "bind",
441 ((const struct sockaddr_un *) serverAddr)->sun_path); 438 ((const struct sockaddr_un *) serverAddr)->sun_path);
@@ -452,7 +449,7 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
452 errno = 0; 449 errno = 0;
453 return NULL; 450 return NULL;
454 } 451 }
455 if (port != 0) 452 if (0 != port)
456 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n", 453 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n",
457 port); 454 port);
458 return sock; 455 return sock;
@@ -478,29 +475,29 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
478 struct GNUNET_TIME_Relative idle_timeout, 475 struct GNUNET_TIME_Relative idle_timeout,
479 int require_found) 476 int require_found)
480{ 477{
481 struct GNUNET_SERVER_Handle *ret; 478 struct GNUNET_SERVER_Handle *server;
482 struct GNUNET_NETWORK_FDSet *r; 479 struct GNUNET_NETWORK_FDSet *r;
483 int i; 480 int i;
484 481
485 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle)); 482 server = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
486 ret->idle_timeout = idle_timeout; 483 server->idle_timeout = idle_timeout;
487 ret->listen_sockets = lsocks; 484 server->listen_sockets = lsocks;
488 ret->access = access; 485 server->access = access;
489 ret->access_cls = access_cls; 486 server->access_cls = access_cls;
490 ret->require_found = require_found; 487 server->require_found = require_found;
491 if (lsocks != NULL) 488 if (NULL != lsocks)
492 { 489 {
493 r = GNUNET_NETWORK_fdset_create (); 490 r = GNUNET_NETWORK_fdset_create ();
494 i = 0; 491 i = 0;
495 while (NULL != ret->listen_sockets[i]) 492 while (NULL != server->listen_sockets[i])
496 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]); 493 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
497 ret->listen_task = 494 server->listen_task =
498 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 495 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
499 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, 496 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
500 &process_listen_socket, ret); 497 &process_listen_socket, server);
501 GNUNET_NETWORK_fdset_destroy (r); 498 GNUNET_NETWORK_fdset_destroy (r);
502 } 499 }
503 return ret; 500 return server;
504} 501}
505 502
506 503
@@ -529,23 +526,23 @@ GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
529 unsigned int j; 526 unsigned int j;
530 527
531 i = 0; 528 i = 0;
532 while (serverAddr[i] != NULL) 529 while (NULL != serverAddr[i])
533 i++; 530 i++;
534 if (i > 0) 531 if (i > 0)
535 { 532 {
536 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1)); 533 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
537 i = 0; 534 i = 0;
538 j = 0; 535 j = 0;
539 while (serverAddr[i] != NULL) 536 while (NULL != serverAddr[i])
540 { 537 {
541 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]); 538 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
542 if (lsocks[j] != NULL) 539 if (NULL != lsocks[j])
543 j++; 540 j++;
544 i++; 541 i++;
545 } 542 }
546 if (j == 0) 543 if (0 == j)
547 { 544 {
548 if (errno != 0) 545 if (0 != errno)
549 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind"); 546 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
550 GNUNET_free (lsocks); 547 GNUNET_free (lsocks);
551 lsocks = NULL; 548 lsocks = NULL;
@@ -563,44 +560,46 @@ GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
563/** 560/**
564 * Free resources held by this server. 561 * Free resources held by this server.
565 * 562 *
566 * @param s server to destroy 563 * @param server server to destroy
567 */ 564 */
568void 565void
569GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s) 566GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
570{ 567{
571 struct HandlerList *hpos; 568 struct HandlerList *hpos;
572 struct NotifyList *npos; 569 struct NotifyList *npos;
573 unsigned int i; 570 unsigned int i;
574 571
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n"); 572 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
576 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task) 573 if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
577 { 574 {
578 GNUNET_SCHEDULER_cancel (s->listen_task); 575 GNUNET_SCHEDULER_cancel (server->listen_task);
579 s->listen_task = GNUNET_SCHEDULER_NO_TASK; 576 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
580 } 577 }
581 if (s->listen_sockets != NULL) 578 if (NULL != server->listen_sockets)
582 { 579 {
583 i = 0; 580 i = 0;
584 while (s->listen_sockets[i] != NULL) 581 while (NULL != server->listen_sockets[i])
585 GNUNET_break (GNUNET_OK == 582 GNUNET_break (GNUNET_OK ==
586 GNUNET_NETWORK_socket_close (s->listen_sockets[i++])); 583 GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
587 GNUNET_free (s->listen_sockets); 584 GNUNET_free (server->listen_sockets);
588 s->listen_sockets = NULL; 585 server->listen_sockets = NULL;
589 } 586 }
590 while (s->clients != NULL) 587 while (NULL != server->clients)
591 GNUNET_SERVER_client_disconnect (s->clients); 588 GNUNET_SERVER_client_disconnect (server->clients);
592 while (NULL != (hpos = s->handlers)) 589 while (NULL != (hpos = server->handlers))
593 { 590 {
594 s->handlers = hpos->next; 591 server->handlers = hpos->next;
595 GNUNET_free (hpos); 592 GNUNET_free (hpos);
596 } 593 }
597 while (NULL != (npos = s->disconnect_notify_list)) 594 while (NULL != (npos = server->disconnect_notify_list_head))
598 { 595 {
599 npos->callback (npos->callback_cls, NULL); 596 npos->callback (npos->callback_cls, NULL);
600 s->disconnect_notify_list = npos->next; 597 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
598 server->disconnect_notify_list_tail,
599 npos);
601 GNUNET_free (npos); 600 GNUNET_free (npos);
602 } 601 }
603 GNUNET_free (s); 602 GNUNET_free (server);
604} 603}
605 604
606 605
@@ -630,6 +629,16 @@ GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
630} 629}
631 630
632 631
632/**
633 * Change functions used by the server to tokenize the message stream.
634 * (very rarely used).
635 *
636 * @param server server to modify
637 * @param create new tokenizer initialization function
638 * @param destroy new tokenizer destruction function
639 * @param receive new tokenizer receive function
640 * @param cls closure for 'create', 'receive', 'destroy'
641 */
633void 642void
634GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server, 643GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
635 GNUNET_SERVER_MstCreateCallback create, 644 GNUNET_SERVER_MstCreateCallback create,
@@ -718,9 +727,8 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
718 LOG (GNUNET_ERROR_TYPE_DEBUG, 727 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 "Server schedules transmission of %u-byte message of type %u to client.\n", 728 "Server schedules transmission of %u-byte message of type %u to client.\n",
720 size, type); 729 size, type);
721 pos = server->handlers;
722 found = GNUNET_NO; 730 found = GNUNET_NO;
723 while (pos != NULL) 731 for (pos = server->handlers; NULL != pos; pos = pos->next)
724 { 732 {
725 i = 0; 733 i = 0;
726 while (pos->handlers[i].callback != NULL) 734 while (pos->handlers[i].callback != NULL)
@@ -728,7 +736,7 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
728 mh = &pos->handlers[i]; 736 mh = &pos->handlers[i];
729 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL)) 737 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
730 { 738 {
731 if ((mh->expected_size != 0) && (mh->expected_size != size)) 739 if ((0 != mh->expected_size) && (mh->expected_size != size))
732 { 740 {
733#if GNUNET8_NETWORK_IS_DEAD 741#if GNUNET8_NETWORK_IS_DEAD
734 LOG (GNUNET_ERROR_TYPE_WARNING, 742 LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -738,7 +746,7 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
738#endif 746#endif
739 return GNUNET_SYSERR; 747 return GNUNET_SYSERR;
740 } 748 }
741 if (sender != NULL) 749 if (NULL != sender)
742 { 750 {
743 if (0 == sender->suspended) 751 if (0 == sender->suspended)
744 { 752 {
@@ -755,13 +763,12 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
755 } 763 }
756 i++; 764 i++;
757 } 765 }
758 pos = pos->next;
759 } 766 }
760 if (found == GNUNET_NO) 767 if (GNUNET_NO == found)
761 { 768 {
762 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 769 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
763 "Received message of unknown type %d\n", type); 770 "Received message of unknown type %d\n", type);
764 if (server->require_found == GNUNET_YES) 771 if (GNUNET_YES == server->require_found)
765 return GNUNET_SYSERR; 772 return GNUNET_SYSERR;
766 } 773 }
767 return GNUNET_OK; 774 return GNUNET_OK;
@@ -799,10 +806,10 @@ process_incoming (void *cls, const void *buf, size_t available,
799static void 806static void
800process_mst (struct GNUNET_SERVER_Client *client, int ret) 807process_mst (struct GNUNET_SERVER_Client *client, int ret)
801{ 808{
802 while ((ret != GNUNET_SYSERR) && (client->server != NULL) && 809 while ((GNUNET_SYSERR != ret) && (NULL != client->server) &&
803 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended)) 810 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
804 { 811 {
805 if (ret == GNUNET_OK) 812 if (GNUNET_OK == ret)
806 { 813 {
807 LOG (GNUNET_ERROR_TYPE_DEBUG, 814 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "Server re-enters receive loop, timeout: %llu.\n", 815 "Server re-enters receive loop, timeout: %llu.\n",
@@ -816,7 +823,7 @@ process_mst (struct GNUNET_SERVER_Client *client, int ret)
816 } 823 }
817 LOG (GNUNET_ERROR_TYPE_DEBUG, 824 LOG (GNUNET_ERROR_TYPE_DEBUG,
818 "Server processes additional messages instantly.\n"); 825 "Server processes additional messages instantly.\n");
819 if (client->server->mst_receive != NULL) 826 if (NULL != client->server->mst_receive)
820 ret = 827 ret =
821 client->server->mst_receive (client->server->mst_cls, client->mst, 828 client->server->mst_receive (client->server->mst_cls, client->mst,
822 client, NULL, 0, GNUNET_NO, GNUNET_YES); 829 client, NULL, 0, GNUNET_NO, GNUNET_YES);
@@ -828,13 +835,13 @@ process_mst (struct GNUNET_SERVER_Client *client, int ret)
828 LOG (GNUNET_ERROR_TYPE_DEBUG, 835 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n", 836 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
830 ret, client->server, client->shutdown_now, client->suspended); 837 ret, client->server, client->shutdown_now, client->suspended);
831 if (ret == GNUNET_NO) 838 if (GNUNET_NO == ret)
832 { 839 {
833 LOG (GNUNET_ERROR_TYPE_DEBUG, 840 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Server has more data pending but is suspended.\n"); 841 "Server has more data pending but is suspended.\n");
835 client->receive_pending = GNUNET_SYSERR; /* data pending */ 842 client->receive_pending = GNUNET_SYSERR; /* data pending */
836 } 843 }
837 if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now)) 844 if ((GNUNET_SYSERR == ret) || (GNUNET_YES == client->shutdown_now))
838 GNUNET_SERVER_client_disconnect (client); 845 GNUNET_SERVER_client_disconnect (client);
839 GNUNET_SERVER_client_drop (client); 846 GNUNET_SERVER_client_drop (client);
840} 847}
@@ -860,13 +867,13 @@ process_incoming (void *cls, const void *buf, size_t available,
860 struct GNUNET_TIME_Absolute now; 867 struct GNUNET_TIME_Absolute now;
861 int ret; 868 int ret;
862 869
863 GNUNET_assert (client->receive_pending == GNUNET_YES); 870 GNUNET_assert (GNUNET_YES == client->receive_pending);
864 client->receive_pending = GNUNET_NO; 871 client->receive_pending = GNUNET_NO;
865 now = GNUNET_TIME_absolute_get (); 872 now = GNUNET_TIME_absolute_get ();
866 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout); 873 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
867 874
868 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) && 875 if ((NULL == buf) && (0 == available) && (NULL == addr) && (0 == errCode) &&
869 (client->shutdown_now != GNUNET_YES) && (server != NULL) && 876 (GNUNET_YES != client->shutdown_now) && (NULL != server) &&
870 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) && 877 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
871 (end.abs_value > now.abs_value)) 878 (end.abs_value > now.abs_value))
872 { 879 {
@@ -881,8 +888,8 @@ process_incoming (void *cls, const void *buf, size_t available,
881 &process_incoming, client); 888 &process_incoming, client);
882 return; 889 return;
883 } 890 }
884 if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) || 891 if ((NULL == buf) || (0 == available) || (0 != errCode) || (NULL == server) ||
885 (client->shutdown_now == GNUNET_YES) || 892 (GNUNET_YES == client->shutdown_now) ||
886 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection))) 893 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
887 { 894 {
888 /* other side closed connection, error connecting, etc. */ 895 /* other side closed connection, error connecting, etc. */
@@ -894,7 +901,7 @@ process_incoming (void *cls, const void *buf, size_t available,
894 GNUNET_SERVER_client_keep (client); 901 GNUNET_SERVER_client_keep (client);
895 client->last_activity = now; 902 client->last_activity = now;
896 903
897 if (server->mst_receive != NULL) 904 if (NULL != server->mst_receive)
898 ret = 905 ret =
899 client->server->mst_receive (client->server->mst_cls, client->mst, 906 client->server->mst_receive (client->server->mst_cls, client->mst,
900 client, buf, available, GNUNET_NO, GNUNET_YES); 907 client, buf, available, GNUNET_NO, GNUNET_YES);
@@ -902,7 +909,6 @@ process_incoming (void *cls, const void *buf, size_t available,
902 ret = 909 ret =
903 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, 910 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
904 GNUNET_YES); 911 GNUNET_YES);
905
906 process_mst (client, ret); 912 process_mst (client, ret);
907} 913}
908 914
@@ -918,16 +924,9 @@ static void
918restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 924restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
919{ 925{
920 struct GNUNET_SERVER_Client *client = cls; 926 struct GNUNET_SERVER_Client *client = cls;
921 struct GNUNET_SERVER_Handle *server = client->server;
922 927
923 client->restart_task = GNUNET_SCHEDULER_NO_TASK; 928 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
924 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && 929 if (GNUNET_NO == client->receive_pending)
925 (GNUNET_NO == server->clients_ignore_shutdown))
926 {
927 GNUNET_SERVER_client_disconnect (client);
928 return;
929 }
930 if (client->receive_pending == GNUNET_NO)
931 { 930 {
932 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n"); 931 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
933 client->receive_pending = GNUNET_YES; 932 client->receive_pending = GNUNET_YES;
@@ -1054,7 +1053,7 @@ GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1054{ 1053{
1055 GNUNET_assert (client->reference_count > 0); 1054 GNUNET_assert (client->reference_count > 0);
1056 client->reference_count--; 1055 client->reference_count--;
1057 if ((client->shutdown_now == GNUNET_YES) && (client->reference_count == 0)) 1056 if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count))
1058 GNUNET_SERVER_client_disconnect (client); 1057 GNUNET_SERVER_client_disconnect (client);
1059} 1058}
1060 1059
@@ -1095,8 +1094,9 @@ GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1095 n = GNUNET_malloc (sizeof (struct NotifyList)); 1094 n = GNUNET_malloc (sizeof (struct NotifyList));
1096 n->callback = callback; 1095 n->callback = callback;
1097 n->callback_cls = callback_cls; 1096 n->callback_cls = callback_cls;
1098 n->next = server->disconnect_notify_list; 1097 GNUNET_CONTAINER_DLL_insert (server->disconnect_notify_list_head,
1099 server->disconnect_notify_list = n; 1098 server->disconnect_notify_list_tail,
1099 n);
1100} 1100}
1101 1101
1102 1102
@@ -1113,26 +1113,18 @@ GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1113 callback, void *callback_cls) 1113 callback, void *callback_cls)
1114{ 1114{
1115 struct NotifyList *pos; 1115 struct NotifyList *pos;
1116 struct NotifyList *prev;
1117 1116
1118 prev = NULL; 1117 for (pos = server->disconnect_notify_list_head; NULL != pos; pos = pos->next)
1119 pos = server->disconnect_notify_list;
1120 while (pos != NULL)
1121 {
1122 if ((pos->callback == callback) && (pos->callback_cls == callback_cls)) 1118 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1123 break; 1119 break;
1124 prev = pos; 1120 if (NULL == pos)
1125 pos = pos->next;
1126 }
1127 if (pos == NULL)
1128 { 1121 {
1129 GNUNET_break (0); 1122 GNUNET_break (0);
1130 return; 1123 return;
1131 } 1124 }
1132 if (prev == NULL) 1125 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
1133 server->disconnect_notify_list = pos->next; 1126 server->disconnect_notify_list_tail,
1134 else 1127 pos);
1135 prev->next = pos->next;
1136 GNUNET_free (pos); 1128 GNUNET_free (pos);
1137} 1129}
1138 1130
@@ -1156,12 +1148,12 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1156 1148
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, 1149 LOG (GNUNET_ERROR_TYPE_DEBUG,
1158 "Client is being disconnected from the server.\n"); 1150 "Client is being disconnected from the server.\n");
1159 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) 1151 if (GNUNET_SCHEDULER_NO_TASK != client->restart_task)
1160 { 1152 {
1161 GNUNET_SCHEDULER_cancel (client->restart_task); 1153 GNUNET_SCHEDULER_cancel (client->restart_task);
1162 client->restart_task = GNUNET_SCHEDULER_NO_TASK; 1154 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1163 } 1155 }
1164 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) 1156 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1165 { 1157 {
1166 GNUNET_SCHEDULER_cancel (client->warn_task); 1158 GNUNET_SCHEDULER_cancel (client->warn_task);
1167 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 1159 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
@@ -1171,40 +1163,35 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1171 GNUNET_CONNECTION_receive_cancel (client->connection); 1163 GNUNET_CONNECTION_receive_cancel (client->connection);
1172 client->receive_pending = GNUNET_NO; 1164 client->receive_pending = GNUNET_NO;
1173 } 1165 }
1174
1175 rc = client->reference_count; 1166 rc = client->reference_count;
1176 if (client->shutdown_now != GNUNET_YES) 1167 if (GNUNET_YES != client->shutdown_now)
1177 { 1168 {
1178 server = client->server; 1169 server = client->server;
1179 client->shutdown_now = GNUNET_YES; 1170 client->shutdown_now = GNUNET_YES;
1180 prev = NULL; 1171 prev = NULL;
1181 pos = server->clients; 1172 pos = server->clients;
1182 while ((pos != NULL) && (pos != client)) 1173 while ((NULL != pos) && (pos != client))
1183 { 1174 {
1184 prev = pos; 1175 prev = pos;
1185 pos = pos->next; 1176 pos = pos->next;
1186 } 1177 }
1187 GNUNET_assert (pos != NULL); 1178 GNUNET_assert (NULL != pos);
1188 if (prev == NULL) 1179 if (NULL == prev)
1189 server->clients = pos->next; 1180 server->clients = pos->next;
1190 else 1181 else
1191 prev->next = pos->next; 1182 prev->next = pos->next;
1192 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) 1183 if (GNUNET_SCHEDULER_NO_TASK != client->restart_task)
1193 { 1184 {
1194 GNUNET_SCHEDULER_cancel (client->restart_task); 1185 GNUNET_SCHEDULER_cancel (client->restart_task);
1195 client->restart_task = GNUNET_SCHEDULER_NO_TASK; 1186 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1196 } 1187 }
1197 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) 1188 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1198 { 1189 {
1199 GNUNET_SCHEDULER_cancel (client->warn_task); 1190 GNUNET_SCHEDULER_cancel (client->warn_task);
1200 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 1191 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1201 } 1192 }
1202 n = server->disconnect_notify_list; 1193 for (n = server->disconnect_notify_list_head; NULL != n; n = n->next)
1203 while (n != NULL)
1204 {
1205 n->callback (n->callback_cls, client); 1194 n->callback (n->callback_cls, client);
1206 n = n->next;
1207 }
1208 } 1195 }
1209 if (rc > 0) 1196 if (rc > 0)
1210 { 1197 {
@@ -1212,24 +1199,23 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1212 "RC still positive, not destroying everything.\n"); 1199 "RC still positive, not destroying everything.\n");
1213 return; 1200 return;
1214 } 1201 }
1215 if (client->in_process_client_buffer == GNUNET_YES) 1202 if (GNUNET_YES == client->in_process_client_buffer)
1216 { 1203 {
1217 LOG (GNUNET_ERROR_TYPE_DEBUG, 1204 LOG (GNUNET_ERROR_TYPE_DEBUG,
1218 "Still processing inputs, not destroying everything.\n"); 1205 "Still processing inputs, not destroying everything.\n");
1219 return; 1206 return;
1220 } 1207 }
1221 1208
1222 if (client->persist == GNUNET_YES) 1209 if (GNUNET_YES == client->persist)
1223 GNUNET_CONNECTION_persist_ (client->connection); 1210 GNUNET_CONNECTION_persist_ (client->connection);
1224 if (NULL != client->th.cth) 1211 if (NULL != client->th.cth)
1225 GNUNET_SERVER_notify_transmit_ready_cancel (&client->th); 1212 GNUNET_SERVER_notify_transmit_ready_cancel (&client->th);
1226 GNUNET_CONNECTION_destroy (client->connection); 1213 GNUNET_CONNECTION_destroy (client->connection);
1227 1214
1228 if (client->server->mst_destroy != NULL) 1215 if (NULL != client->server->mst_destroy)
1229 client->server->mst_destroy (client->server->mst_cls, client->mst); 1216 client->server->mst_destroy (client->server->mst_cls, client->mst);
1230 else 1217 else
1231 GNUNET_SERVER_mst_destroy (client->mst); 1218 GNUNET_SERVER_mst_destroy (client->mst);
1232
1233 GNUNET_free (client); 1219 GNUNET_free (client);
1234} 1220}
1235 1221
@@ -1351,11 +1337,11 @@ GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1351void 1337void
1352GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success) 1338GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1353{ 1339{
1354 if (client == NULL) 1340 if (NULL == client)
1355 return; 1341 return;
1356 GNUNET_assert (client->suspended > 0); 1342 GNUNET_assert (client->suspended > 0);
1357 client->suspended--; 1343 client->suspended--;
1358 if (success != GNUNET_OK) 1344 if (GNUNET_OK != success)
1359 { 1345 {
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, 1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1361 "GNUNET_SERVER_receive_done called with failure indication\n"); 1347 "GNUNET_SERVER_receive_done called with failure indication\n");
@@ -1373,13 +1359,13 @@ GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1373 GNUNET_SCHEDULER_cancel (client->warn_task); 1359 GNUNET_SCHEDULER_cancel (client->warn_task);
1374 client->warn_task = GNUNET_SCHEDULER_NO_TASK; 1360 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1375 } 1361 }
1376 if (client->in_process_client_buffer == GNUNET_YES) 1362 if (GNUNET_YES == client->in_process_client_buffer)
1377 { 1363 {
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, 1364 LOG (GNUNET_ERROR_TYPE_DEBUG,
1379 "GNUNET_SERVER_receive_done called while still in processing loop\n"); 1365 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1380 return; 1366 return;
1381 } 1367 }
1382 if ((client->server == NULL) || (GNUNET_YES == client->shutdown_now)) 1368 if ((NULL == client->server) || (GNUNET_YES == client->shutdown_now))
1383 { 1369 {
1384 GNUNET_SERVER_client_disconnect (client); 1370 GNUNET_SERVER_client_disconnect (client);
1385 return; 1371 return;
@@ -1391,21 +1377,4 @@ GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1391} 1377}
1392 1378
1393 1379
1394/**
1395 * Configure this server's connections to continue handling client
1396 * requests as usual even after we get a shutdown signal. The change
1397 * only applies to clients that connect to the server from the outside
1398 * using TCP after this call. Clients managed previously or those
1399 * added using GNUNET_SERVER_connect_socket and
1400 * GNUNET_SERVER_connect_callback are not affected by this option.
1401 *
1402 * @param h server handle
1403 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1404 */
1405void
1406GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1407{
1408 h->clients_ignore_shutdown = do_ignore;
1409}
1410
1411/* end of server.c */ 1380/* end of server.c */
diff --git a/src/util/server_mst.c b/src/util/server_mst.c
index 61617706e..1523de91a 100644
--- a/src/util/server_mst.c
+++ b/src/util/server_mst.c
@@ -31,7 +31,6 @@
31#include "gnunet_server_lib.h" 31#include "gnunet_server_lib.h"
32#include "gnunet_time_lib.h" 32#include "gnunet_time_lib.h"
33 33
34#define DEBUG_SERVER_MST GNUNET_EXTRA_LOGGING
35 34
36#if HAVE_UNALIGNED_64_ACCESS 35#if HAVE_UNALIGNED_64_ACCESS
37#define ALIGN_FACTOR 4 36#define ALIGN_FACTOR 4
@@ -134,11 +133,9 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
134 133
135 GNUNET_assert (mst->off <= mst->pos); 134 GNUNET_assert (mst->off <= mst->pos);
136 GNUNET_assert (mst->pos <= mst->curr_buf); 135 GNUNET_assert (mst->pos <= mst->curr_buf);
137#if DEBUG_SERVER_MST
138 LOG (GNUNET_ERROR_TYPE_DEBUG, 136 LOG (GNUNET_ERROR_TYPE_DEBUG,
139 "Server-mst receives %u bytes with %u bytes already in private buffer\n", 137 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
140 (unsigned int) size, (unsigned int) (mst->pos - mst->off)); 138 (unsigned int) size, (unsigned int) (mst->pos - mst->off));
141#endif
142 ret = GNUNET_OK; 139 ret = GNUNET_OK;
143 ibuf = (char *) mst->hdr; 140 ibuf = (char *) mst->hdr;
144 while (mst->pos > 0) 141 while (mst->pos > 0)
@@ -235,11 +232,9 @@ do_align:
235 GNUNET_assert (0 == mst->pos); 232 GNUNET_assert (0 == mst->pos);
236 while (size > 0) 233 while (size > 0)
237 { 234 {
238#if DEBUG_SERVER_MST
239 LOG (GNUNET_ERROR_TYPE_DEBUG, 235 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Server-mst has %u bytes left in inbound buffer\n", 236 "Server-mst has %u bytes left in inbound buffer\n",
241 (unsigned int) size); 237 (unsigned int) size);
242#endif
243 if (size < sizeof (struct GNUNET_MessageHeader)) 238 if (size < sizeof (struct GNUNET_MessageHeader))
244 break; 239 break;
245 offset = (unsigned long) buf; 240 offset = (unsigned long) buf;
@@ -295,11 +290,9 @@ copy:
295 mst->off = 0; 290 mst->off = 0;
296 mst->pos = 0; 291 mst->pos = 0;
297 } 292 }
298#if DEBUG_SERVER_MST
299 LOG (GNUNET_ERROR_TYPE_DEBUG, 293 LOG (GNUNET_ERROR_TYPE_DEBUG,
300 "Server-mst leaves %u bytes in private buffer\n", 294 "Server-mst leaves %u bytes in private buffer\n",
301 (unsigned int) (mst->pos - mst->off)); 295 (unsigned int) (mst->pos - mst->off));
302#endif
303 return ret; 296 return ret;
304} 297}
305 298
diff --git a/src/util/server_nc.c b/src/util/server_nc.c
index 9717a4516..deb1a18c9 100644
--- a/src/util/server_nc.c
+++ b/src/util/server_nc.c
@@ -36,8 +36,6 @@
36#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) 36#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37 37
38 38
39#define DEBUG_SERVER_NC GNUNET_EXTRA_LOGGING
40
41/** 39/**
42 * Entry in list of messages pending to be transmitted. 40 * Entry in list of messages pending to be transmitted.
43 */ 41 */
@@ -171,11 +169,9 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
171 } 169 }
172 if (pos == NULL) 170 if (pos == NULL)
173 return; 171 return;
174#if DEBUG_SERVER_NC
175 LOG (GNUNET_ERROR_TYPE_DEBUG, 172 LOG (GNUNET_ERROR_TYPE_DEBUG,
176 "Client disconnected, cleaning up %u messages in NC queue\n", 173 "Client disconnected, cleaning up %u messages in NC queue\n",
177 pos->num_pending); 174 pos->num_pending);
178#endif
179 if (prev == NULL) 175 if (prev == NULL)
180 nc->clients = pos->next; 176 nc->clients = pos->next;
181 else 177 else
@@ -297,10 +293,8 @@ transmit_message (void *cls, size_t size, void *buf)
297 if (buf == NULL) 293 if (buf == NULL)
298 { 294 {
299 /* 'cl' should be freed via disconnect notification shortly */ 295 /* 'cl' should be freed via disconnect notification shortly */
300#if DEBUG_SERVER_NC
301 LOG (GNUNET_ERROR_TYPE_DEBUG, 296 LOG (GNUNET_ERROR_TYPE_DEBUG,
302 "Failed to transmit message from NC queue to client\n"); 297 "Failed to transmit message from NC queue to client\n");
303#endif
304 return 0; 298 return 0;
305 } 299 }
306 ret = 0; 300 ret = 0;
@@ -310,11 +304,9 @@ transmit_message (void *cls, size_t size, void *buf)
310 if (size < msize) 304 if (size < msize)
311 break; 305 break;
312 GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml); 306 GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
313#if DEBUG_SERVER_NC
314 LOG (GNUNET_ERROR_TYPE_DEBUG, 307 LOG (GNUNET_ERROR_TYPE_DEBUG,
315 "Copying message of type %u and size %u from pending queue to transmission buffer\n", 308 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
316 ntohs (pml->msg->type), msize); 309 ntohs (pml->msg->type), msize);
317#endif
318 memcpy (&cbuf[ret], pml->msg, msize); 310 memcpy (&cbuf[ret], pml->msg, msize);
319 ret += msize; 311 ret += msize;
320 size -= msize; 312 size -= msize;
@@ -323,11 +315,9 @@ transmit_message (void *cls, size_t size, void *buf)
323 } 315 }
324 if (pml != NULL) 316 if (pml != NULL)
325 { 317 {
326#if DEBUG_SERVER_NC
327 LOG (GNUNET_ERROR_TYPE_DEBUG, 318 LOG (GNUNET_ERROR_TYPE_DEBUG,
328 "Have %u messages left in NC queue, will try transmission again\n", 319 "Have %u messages left in NC queue, will try transmission again\n",
329 cl->num_pending); 320 cl->num_pending);
330#endif
331 cl->th = 321 cl->th =
332 GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size), 322 GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size),
333 GNUNET_TIME_UNIT_FOREVER_REL, 323 GNUNET_TIME_UNIT_FOREVER_REL,
@@ -372,11 +362,9 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
372 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size); 362 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
373 pml->msg = (const struct GNUNET_MessageHeader *) &pml[1]; 363 pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
374 pml->can_drop = can_drop; 364 pml->can_drop = can_drop;
375#if DEBUG_SERVER_NC
376 LOG (GNUNET_ERROR_TYPE_DEBUG, 365 LOG (GNUNET_ERROR_TYPE_DEBUG,
377 "Adding message of type %u and size %u to pending queue (which has %u entries)\n", 366 "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
378 ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length); 367 ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
379#endif
380 memcpy (&pml[1], msg, size); 368 memcpy (&pml[1], msg, size);
381 /* append */ 369 /* append */
382 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail, 370 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
diff --git a/src/util/service.c b/src/util/service.c
index c749c8b81..5f58bcf6a 100644
--- a/src/util/service.c
+++ b/src/util/service.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors) 3 (C) 2009, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -42,7 +42,6 @@
42 42
43#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) 43#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
44 44
45#define DEBUG_SERVICE GNUNET_EXTRA_LOGGING
46 45
47/* ******************* access control ******************** */ 46/* ******************* access control ******************** */
48 47
@@ -51,7 +50,14 @@
51 */ 50 */
52struct IPv4NetworkSet 51struct IPv4NetworkSet
53{ 52{
53 /**
54 * IPv4 address.
55 */
54 struct in_addr network; 56 struct in_addr network;
57
58 /**
59 * IPv4 netmask.
60 */
55 struct in_addr netmask; 61 struct in_addr netmask;
56}; 62};
57 63
@@ -60,7 +66,14 @@ struct IPv4NetworkSet
60 */ 66 */
61struct IPv6NetworkSet 67struct IPv6NetworkSet
62{ 68{
69 /**
70 * IPv6 address.
71 */
63 struct in6_addr network; 72 struct in6_addr network;
73
74 /**
75 * IPv6 netmask.
76 */
64 struct in6_addr netmask; 77 struct in6_addr netmask;
65}; 78};
66 79
@@ -72,7 +85,7 @@ struct IPv6NetworkSet
72 * with a semicolon). The network must be given in dotted-decimal 85 * with a semicolon). The network must be given in dotted-decimal
73 * notation. The netmask can be given in CIDR notation (/16) or 86 * notation. The netmask can be given in CIDR notation (/16) or
74 * in dotted-decimal (/255.255.0.0). 87 * in dotted-decimal (/255.255.0.0).
75 * <p> 88 *
76 * @param routeList a string specifying the forbidden networks 89 * @param routeList a string specifying the forbidden networks
77 * @return the converted list, NULL if the synatx is flawed 90 * @return the converted list, NULL if the synatx is flawed
78 */ 91 */
@@ -89,18 +102,16 @@ parse_ipv4_specification (const char *routeList)
89 int slash; 102 int slash;
90 struct IPv4NetworkSet *result; 103 struct IPv4NetworkSet *result;
91 104
92 if (routeList == NULL) 105 if (NULL == routeList)
93 return NULL; 106 return NULL;
94 len = strlen (routeList); 107 len = strlen (routeList);
95 if (len == 0) 108 if (0 == len)
96 return NULL; 109 return NULL;
97 count = 0; 110 count = 0;
98 for (i = 0; i < len; i++) 111 for (i = 0; i < len; i++)
99 if (routeList[i] == ';') 112 if (routeList[i] == ';')
100 count++; 113 count++;
101 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1)); 114 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
102 /* add termination */
103 memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1));
104 i = 0; 115 i = 0;
105 pos = 0; 116 pos = 0;
106 while (i < count) 117 while (i < count)
@@ -109,7 +120,7 @@ parse_ipv4_specification (const char *routeList)
109 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0], 120 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
110 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5], 121 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
111 &temps[6], &temps[7]); 122 &temps[6], &temps[7]);
112 if (cnt == 8) 123 if (8 == cnt)
113 { 124 {
114 for (j = 0; j < 8; j++) 125 for (j = 0; j < 8; j++)
115 if (temps[j] > 0xFF) 126 if (temps[j] > 0xFF)
@@ -135,7 +146,7 @@ parse_ipv4_specification (const char *routeList)
135 cnt = 146 cnt =
136 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1], 147 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
137 &temps[2], &temps[3], &slash); 148 &temps[2], &temps[3], &slash);
138 if (cnt == 5) 149 if (5 == cnt)
139 { 150 {
140 for (j = 0; j < 4; j++) 151 for (j = 0; j < 4; j++)
141 if (temps[j] > 0xFF) 152 if (temps[j] > 0xFF)
@@ -158,7 +169,7 @@ parse_ipv4_specification (const char *routeList)
158 slash--; 169 slash--;
159 } 170 }
160 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); 171 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
161 while (routeList[pos] != ';') 172 while (';' != routeList[pos])
162 pos++; 173 pos++;
163 pos++; 174 pos++;
164 i++; 175 i++;
@@ -178,7 +189,7 @@ parse_ipv4_specification (const char *routeList)
178 cnt = 189 cnt =
179 SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1], 190 SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
180 &temps[2], &temps[3]); 191 &temps[2], &temps[3]);
181 if (cnt == 4) 192 if (4 == cnt)
182 { 193 {
183 for (j = 0; j < 4; j++) 194 for (j = 0; j < 4; j++)
184 if (temps[j] > 0xFF) 195 if (temps[j] > 0xFF)
@@ -227,7 +238,7 @@ parse_ipv4_specification (const char *routeList)
227 * with a semicolon). The network must be given in colon-hex 238 * with a semicolon). The network must be given in colon-hex
228 * notation. The netmask must be given in CIDR notation (/16) or 239 * notation. The netmask must be given in CIDR notation (/16) or
229 * can be omitted to specify a single host. 240 * can be omitted to specify a single host.
230 * <p> 241 *
231 * @param routeListX a string specifying the forbidden networks 242 * @param routeListX a string specifying the forbidden networks
232 * @return the converted list, NULL if the synatx is flawed 243 * @return the converted list, NULL if the synatx is flawed
233 */ 244 */
@@ -247,17 +258,17 @@ parse_ipv6_specification (const char *routeListX)
247 unsigned int off; 258 unsigned int off;
248 int save; 259 int save;
249 260
250 if (routeListX == NULL) 261 if (NULL == routeListX)
251 return NULL; 262 return NULL;
252 len = strlen (routeListX); 263 len = strlen (routeListX);
253 if (len == 0) 264 if (0 == len)
254 return NULL; 265 return NULL;
255 routeList = GNUNET_strdup (routeListX); 266 routeList = GNUNET_strdup (routeListX);
256 count = 0; 267 count = 0;
257 for (i = 0; i < len; i++) 268 for (i = 0; i < len; i++)
258 if (routeList[i] == ';') 269 if (';' == routeList[i])
259 count++; 270 count++;
260 if (routeList[len - 1] != ';') 271 if (';' != routeList[len - 1])
261 { 272 {
262 LOG (GNUNET_ERROR_TYPE_ERROR, 273 LOG (GNUNET_ERROR_TYPE_ERROR,
263 _("Invalid network notation (does not end with ';': `%s')\n"), 274 _("Invalid network notation (does not end with ';': `%s')\n"),
@@ -267,13 +278,12 @@ parse_ipv6_specification (const char *routeListX)
267 } 278 }
268 279
269 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1)); 280 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
270 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
271 i = 0; 281 i = 0;
272 pos = 0; 282 pos = 0;
273 while (i < count) 283 while (i < count)
274 { 284 {
275 start = pos; 285 start = pos;
276 while (routeList[pos] != ';') 286 while (';' != routeList[pos])
277 pos++; 287 pos++;
278 slash = pos; 288 slash = pos;
279 while ((slash >= start) && (routeList[slash] != '/')) 289 while ((slash >= start) && (routeList[slash] != '/'))
@@ -292,7 +302,7 @@ parse_ipv6_specification (const char *routeListX)
292 save = errno; 302 save = errno;
293 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128)) 303 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
294 { 304 {
295 if (ret == 0) 305 if (0 == ret)
296 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"), 306 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
297 &routeList[slash + 1]); 307 &routeList[slash + 1]);
298 else 308 else
@@ -322,7 +332,7 @@ parse_ipv6_specification (const char *routeListX)
322 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network); 332 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
323 if (ret <= 0) 333 if (ret <= 0)
324 { 334 {
325 if (ret == 0) 335 if (0 == ret)
326 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"), 336 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
327 &routeList[slash + 1]); 337 &routeList[slash + 1]);
328 else 338 else
@@ -349,12 +359,11 @@ parse_ipv6_specification (const char *routeListX)
349static int 359static int
350check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add) 360check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
351{ 361{
352 int i; 362 unsigned int i;
353 363
354 i = 0; 364 if (NULL == list)
355 if (list == NULL)
356 return GNUNET_NO; 365 return GNUNET_NO;
357 366 i = 0;
358 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0)) 367 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
359 { 368 {
360 if ((add->s_addr & list[i].netmask.s_addr) == 369 if ((add->s_addr & list[i].netmask.s_addr) ==
@@ -365,6 +374,7 @@ check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
365 return GNUNET_NO; 374 return GNUNET_NO;
366} 375}
367 376
377
368/** 378/**
369 * Check if the given IP address is in the list of IP addresses. 379 * Check if the given IP address is in the list of IP addresses.
370 * 380 *
@@ -379,13 +389,12 @@ check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
379 unsigned int j; 389 unsigned int j;
380 struct in6_addr zero; 390 struct in6_addr zero;
381 391
382 if (list == NULL) 392 if (NULL == list)
383 return GNUNET_NO; 393 return GNUNET_NO;
384
385 memset (&zero, 0, sizeof (struct in6_addr)); 394 memset (&zero, 0, sizeof (struct in6_addr));
386 i = 0; 395 i = 0;
387NEXT: 396NEXT:
388 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0) 397 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
389 { 398 {
390 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++) 399 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
391 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) != 400 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
@@ -526,6 +535,13 @@ struct GNUNET_SERVICE_Context
526 535
527/* ****************** message handlers ****************** */ 536/* ****************** message handlers ****************** */
528 537
538/**
539 *
540 * @param cls
541 * @param size
542 * @param buf
543 * @return
544 */
529static size_t 545static size_t
530write_test (void *cls, size_t size, void *buf) 546write_test (void *cls, size_t size, void *buf)
531{ 547{
@@ -544,6 +560,7 @@ write_test (void *cls, size_t size, void *buf)
544 return sizeof (struct GNUNET_MessageHeader); 560 return sizeof (struct GNUNET_MessageHeader);
545} 561}
546 562
563
547/** 564/**
548 * Handler for TEST message. 565 * Handler for TEST message.
549 * 566 *
@@ -577,7 +594,6 @@ static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
577}; 594};
578 595
579 596
580
581/* ****************** service core routines ************** */ 597/* ****************** service core routines ************** */
582 598
583 599
@@ -605,32 +621,32 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
605 case AF_INET: 621 case AF_INET:
606 GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); 622 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
607 i4 = (const struct sockaddr_in *) addr; 623 i4 = (const struct sockaddr_in *) addr;
608 ret = ((sctx->v4_allowed == NULL) || 624 ret = ((NULL == sctx->v4_allowed) ||
609 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) && 625 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
610 ((sctx->v4_denied == NULL) || 626 ((NULL == sctx->v4_denied) ||
611 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr))); 627 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
612 break; 628 break;
613 case AF_INET6: 629 case AF_INET6:
614 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); 630 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
615 i6 = (const struct sockaddr_in6 *) addr; 631 i6 = (const struct sockaddr_in6 *) addr;
616 ret = ((sctx->v6_allowed == NULL) || 632 ret = ((NULL == sctx->v6_allowed) ||
617 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) && 633 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
618 ((sctx->v6_denied == NULL) || 634 ((NULL == sctx->v6_denied) ||
619 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr))); 635 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
620 break; 636 break;
621#ifndef WINDOWS 637#ifndef WINDOWS
622 case AF_UNIX: 638 case AF_UNIX:
623 ret = GNUNET_OK; /* always OK for now */ 639 ret = GNUNET_OK; /* always OK for now */
624 if (sctx->match_uid == GNUNET_YES) 640 if (GNUNET_YES == sctx->match_uid)
625 { 641 {
626 /* UID match required */ 642 /* UID match required */
627 ret = (uc != NULL) && (uc->uid == geteuid ()); 643 ret = (NULL != uc) && (uc->uid == geteuid ());
628 } 644 }
629 else if ( (sctx->match_gid == GNUNET_YES) && 645 else if ( (GNUNET_YES == sctx->match_gid) &&
630 ( (uc == NULL) || (uc->uid != geteuid ()) ) ) 646 ( (NULL == uc) || (uc->uid != geteuid ()) ) )
631 { 647 {
632 /* group match required and UID does not match */ 648 /* group match required and UID does not match */
633 if (uc == NULL) 649 if (NULL == uc)
634 { 650 {
635 /* no credentials, group match not possible */ 651 /* no credentials, group match not possible */
636 ret = GNUNET_NO; 652 ret = GNUNET_NO;
@@ -667,7 +683,7 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
667 } 683 }
668 if (GNUNET_NO == ret) 684 if (GNUNET_NO == ret)
669 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"), 685 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
670 (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid); 686 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
671 break; 687 break;
672#endif 688#endif
673 default: 689 default:
@@ -675,11 +691,11 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
675 addr->sa_family); 691 addr->sa_family);
676 return GNUNET_SYSERR; 692 return GNUNET_SYSERR;
677 } 693 }
678 if (ret != GNUNET_OK) 694 if (GNUNET_OK != ret)
679 { 695 {
680 LOG (GNUNET_ERROR_TYPE_WARNING, 696 LOG (GNUNET_ERROR_TYPE_WARNING,
681 _("Access from `%s' denied to service `%s'\n"), GNUNET_a2s (addr, 697 _("Access from `%s' denied to service `%s'\n"),
682 addrlen), 698 GNUNET_a2s (addr, addrlen),
683 sctx->serviceName); 699 sctx->serviceName);
684 } 700 }
685 return ret; 701 return ret;
@@ -689,11 +705,13 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
689/** 705/**
690 * Get the name of the file where we will 706 * Get the name of the file where we will
691 * write the PID of the service. 707 * write the PID of the service.
708 *
709 * @param sctx service context
710 * @return name of the file for the process ID
692 */ 711 */
693static char * 712static char *
694get_pid_file_name (struct GNUNET_SERVICE_Context *sctx) 713get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
695{ 714{
696
697 char *pif; 715 char *pif;
698 716
699 if (GNUNET_OK != 717 if (GNUNET_OK !=
@@ -706,6 +724,11 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
706 724
707/** 725/**
708 * Parse an IPv4 access control list. 726 * Parse an IPv4 access control list.
727 *
728 * @param ret
729 * @param sctx
730 * @param option
731 * @return
709 */ 732 */
710static int 733static int
711process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, 734process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
@@ -733,7 +756,12 @@ process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
733 756
734 757
735/** 758/**
736 * Parse an IPv4 access control list. 759 * Parse an IPv6 access control list.
760 *
761 * @param ret
762 * @param sctx
763 * @param option
764 * @return
737 */ 765 */
738static int 766static int
739process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, 767process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
@@ -759,6 +787,7 @@ process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
759 return GNUNET_OK; 787 return GNUNET_OK;
760} 788}
761 789
790
762/** 791/**
763 * Add the given UNIX domain path as an address to the 792 * Add the given UNIX domain path as an address to the
764 * list (as the first entry). 793 * list (as the first entry).
@@ -859,8 +888,8 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
859 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); 888 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
860 if (NULL == desc) 889 if (NULL == desc)
861 { 890 {
862 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || 891 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
863 (errno == EACCES)) 892 (EACCES == errno))
864 { 893 {
865 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); 894 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
866 return GNUNET_SYSERR; 895 return GNUNET_SYSERR;
@@ -927,8 +956,8 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
927 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); 956 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
928 if (NULL == desc) 957 if (NULL == desc)
929 { 958 {
930 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || 959 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
931 (errno == EACCES)) 960 (EACCES == errno))
932 { 961 {
933 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); 962 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
934 GNUNET_free_non_null (hostname); 963 GNUNET_free_non_null (hostname);
@@ -950,7 +979,7 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
950 } 979 }
951#endif 980#endif
952 981
953 if ((port == 0) && (unixpath == NULL)) 982 if ((0 == port) && (NULL == unixpath))
954 { 983 {
955 LOG (GNUNET_ERROR_TYPE_ERROR, 984 LOG (GNUNET_ERROR_TYPE_ERROR,
956 _ 985 _
@@ -959,7 +988,7 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
959 GNUNET_free_non_null (hostname); 988 GNUNET_free_non_null (hostname);
960 return GNUNET_SYSERR; 989 return GNUNET_SYSERR;
961 } 990 }
962 if (port == 0) 991 if (0 == port)
963 { 992 {
964 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *)); 993 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
965 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t)); 994 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
@@ -971,13 +1000,11 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
971 return 1; 1000 return 1;
972 } 1001 }
973 1002
974 if (hostname != NULL) 1003 if (NULL != hostname)
975 { 1004 {
976#if DEBUG_SERVICE
977 LOG (GNUNET_ERROR_TYPE_DEBUG, 1005 LOG (GNUNET_ERROR_TYPE_DEBUG,
978 "Resolving `%s' since that is where `%s' will bind to.\n", hostname, 1006 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
979 serviceName); 1007 serviceName);
980#endif
981 memset (&hints, 0, sizeof (struct addrinfo)); 1008 memset (&hints, 0, sizeof (struct addrinfo));
982 if (disablev6) 1009 if (disablev6)
983 hints.ai_family = AF_INET; 1010 hints.ai_family = AF_INET;
@@ -1023,19 +1050,17 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
1023 while (NULL != (pos = next)) 1050 while (NULL != (pos = next))
1024 { 1051 {
1025 next = pos->ai_next; 1052 next = pos->ai_next;
1026 if ((disablev6) && (pos->ai_family == AF_INET6)) 1053 if ((disablev6) && (AF_INET6 == pos->ai_family))
1027 continue; 1054 continue;
1028 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0)) 1055 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1029 continue; /* not TCP */ 1056 continue; /* not TCP */
1030 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0)) 1057 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1031 continue; /* huh? */ 1058 continue; /* huh? */
1032#if DEBUG_SERVICE
1033 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n", 1059 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1034 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen)); 1060 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1035#endif 1061 if (AF_INET == pos->ai_family)
1036 if (pos->ai_family == AF_INET)
1037 { 1062 {
1038 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in)); 1063 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1039 saddrlens[i] = pos->ai_addrlen; 1064 saddrlens[i] = pos->ai_addrlen;
1040 saddrs[i] = GNUNET_malloc (saddrlens[i]); 1065 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1041 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); 1066 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
@@ -1043,8 +1068,8 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
1043 } 1068 }
1044 else 1069 else
1045 { 1070 {
1046 GNUNET_assert (pos->ai_family == AF_INET6); 1071 GNUNET_assert (AF_INET6 == pos->ai_family);
1047 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6)); 1072 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1048 saddrlens[i] = pos->ai_addrlen; 1073 saddrlens[i] = pos->ai_addrlen;
1049 saddrs[i] = GNUNET_malloc (saddrlens[i]); 1074 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1050 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); 1075 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
@@ -1121,6 +1146,9 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName,
1121 1146
1122#ifdef MINGW 1147#ifdef MINGW
1123/** 1148/**
1149 *
1150 *
1151 * @param sctx
1124 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself), 1152 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1125 * and GNUNET_SYSERR on error. 1153 * and GNUNET_SYSERR on error.
1126 */ 1154 */
@@ -1129,22 +1157,20 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1129{ 1157{
1130 const char *env_buf; 1158 const char *env_buf;
1131 int fail; 1159 int fail;
1132 uint64_t count, i; 1160 uint64_t count;
1161 uint64_t i;
1133 HANDLE lsocks_pipe; 1162 HANDLE lsocks_pipe;
1134 1163
1135 env_buf = getenv ("GNUNET_OS_READ_LSOCKS"); 1164 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1136 if ((env_buf == NULL) || (strlen (env_buf) <= 0)) 1165 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1137 {
1138 return GNUNET_NO; 1166 return GNUNET_NO;
1139 }
1140 /* Using W32 API directly here, because this pipe will 1167 /* Using W32 API directly here, because this pipe will
1141 * never be used outside of this function, and it's just too much of a bother 1168 * never be used outside of this function, and it's just too much of a bother
1142 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks) 1169 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1143 */ 1170 */
1144 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10); 1171 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1145 if (lsocks_pipe == 0 || lsocks_pipe == INVALID_HANDLE_VALUE) 1172 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1146 return GNUNET_NO; 1173 return GNUNET_NO
1147
1148 fail = 1; 1174 fail = 1;
1149 do 1175 do
1150 { 1176 {
@@ -1153,7 +1179,7 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1153 DWORD rd; 1179 DWORD rd;
1154 1180
1155 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL); 1181 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1156 if (ret == 0 || rd != sizeof (count) || count == 0) 1182 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1157 break; 1183 break;
1158 sctx->lsocks = 1184 sctx->lsocks =
1159 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1)); 1185 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
@@ -1164,15 +1190,16 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1164 WSAPROTOCOL_INFOA pi; 1190 WSAPROTOCOL_INFOA pi;
1165 uint64_t size; 1191 uint64_t size;
1166 SOCKET s; 1192 SOCKET s;
1193
1167 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL); 1194 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1168 if (ret == 0 || rd != sizeof (size) || size != sizeof (pi)) 1195 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1169 break; 1196 break;
1170 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL); 1197 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1171 if (ret == 0 || rd != sizeof (pi)) 1198 if ( (0 == ret) || (sizeof (pi) != rd))
1172 break; 1199 break;
1173 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED); 1200 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1174 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s); 1201 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1175 if (sctx->lsocks[i] == NULL) 1202 if (NULL == sctx->lsocks[i])
1176 break; 1203 break;
1177 else if (i == count - 1) 1204 else if (i == count - 1)
1178 fail2 = 0; 1205 fail2 = 0;
@@ -1190,13 +1217,12 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1190 { 1217 {
1191 LOG (GNUNET_ERROR_TYPE_ERROR, 1218 LOG (GNUNET_ERROR_TYPE_ERROR,
1192 _("Could not access a pre-bound socket, will try to bind myself\n")); 1219 _("Could not access a pre-bound socket, will try to bind myself\n"));
1193 for (i = 0; i < count && sctx->lsocks[i] != NULL; i++) 1220 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1194 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i])); 1221 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1195 GNUNET_free_non_null (sctx->lsocks); 1222 GNUNET_free_non_null (sctx->lsocks);
1196 sctx->lsocks = NULL; 1223 sctx->lsocks = NULL;
1197 return GNUNET_NO; 1224 return GNUNET_NO;
1198 } 1225 }
1199
1200 return GNUNET_YES; 1226 return GNUNET_YES;
1201} 1227}
1202#endif 1228#endif
@@ -1217,6 +1243,7 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1217 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets) 1243 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1218 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets) 1244 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1219 * 1245 *
1246 * @param sctx
1220 * @return GNUNET_OK if configuration succeeded 1247 * @return GNUNET_OK if configuration succeeded
1221 */ 1248 */
1222static int 1249static int
@@ -1306,7 +1333,7 @@ setup_service (struct GNUNET_SERVICE_Context *sctx)
1306 } 1333 }
1307#endif 1334#endif
1308 1335
1309 if ((sctx->lsocks == NULL) && 1336 if ((NULL == sctx->lsocks) &&
1310 (GNUNET_SYSERR == 1337 (GNUNET_SYSERR ==
1311 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg, 1338 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
1312 &sctx->addrs, &sctx->addrlens))) 1339 &sctx->addrs, &sctx->addrlens)))
@@ -1330,11 +1357,13 @@ setup_service (struct GNUNET_SERVICE_Context *sctx)
1330/** 1357/**
1331 * Get the name of the user that'll be used 1358 * Get the name of the user that'll be used
1332 * to provide the service. 1359 * to provide the service.
1360 *
1361 * @param sctx
1362 * @return
1333 */ 1363 */
1334static char * 1364static char *
1335get_user_name (struct GNUNET_SERVICE_Context *sctx) 1365get_user_name (struct GNUNET_SERVICE_Context *sctx)
1336{ 1366{
1337
1338 char *un; 1367 char *un;
1339 1368
1340 if (GNUNET_OK != 1369 if (GNUNET_OK !=
@@ -1346,6 +1375,10 @@ get_user_name (struct GNUNET_SERVICE_Context *sctx)
1346 1375
1347/** 1376/**
1348 * Write PID file. 1377 * Write PID file.
1378 *
1379 * @param sctx
1380 * @param pid
1381 * @return
1349 */ 1382 */
1350static int 1383static int
1351write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid) 1384write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
@@ -1369,7 +1402,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1369 /* we get to create a directory -- and claim it 1402 /* we get to create a directory -- and claim it
1370 * as ours! */ 1403 * as ours! */
1371 GNUNET_DISK_directory_create (rdir); 1404 GNUNET_DISK_directory_create (rdir);
1372 if ((user != NULL) && (0 < strlen (user))) 1405 if ((NULL != user) && (0 < strlen (user)))
1373 GNUNET_DISK_file_change_owner (rdir, user); 1406 GNUNET_DISK_file_change_owner (rdir, user);
1374 } 1407 }
1375 if (0 != ACCESS (rdir, W_OK | X_OK)) 1408 if (0 != ACCESS (rdir, W_OK | X_OK))
@@ -1382,7 +1415,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1382 } 1415 }
1383 GNUNET_free (rdir); 1416 GNUNET_free (rdir);
1384 pidfd = FOPEN (pif, "w"); 1417 pidfd = FOPEN (pif, "w");
1385 if (pidfd == NULL) 1418 if (NULL == pidfd)
1386 { 1419 {
1387 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif); 1420 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1388 GNUNET_free (pif); 1421 GNUNET_free (pif);
@@ -1392,7 +1425,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1392 if (0 > FPRINTF (pidfd, "%u", pid)) 1425 if (0 > FPRINTF (pidfd, "%u", pid))
1393 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif); 1426 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1394 GNUNET_break (0 == FCLOSE (pidfd)); 1427 GNUNET_break (0 == FCLOSE (pidfd));
1395 if ((user != NULL) && (0 < strlen (user))) 1428 if ((NULL != user) && (0 < strlen (user)))
1396 GNUNET_DISK_file_change_owner (pif, user); 1429 GNUNET_DISK_file_change_owner (pif, user);
1397 GNUNET_free_non_null (user); 1430 GNUNET_free_non_null (user);
1398 GNUNET_free (pif); 1431 GNUNET_free (pif);
@@ -1411,12 +1444,17 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1411{ 1444{
1412 struct GNUNET_SERVER_Handle *server = cls; 1445 struct GNUNET_SERVER_Handle *server = cls;
1413 1446
1447 // FIXME: we should not unconditionally destroy the server
1448 // here (often only stopping 'listening' would be better)
1414 GNUNET_SERVER_destroy (server); 1449 GNUNET_SERVER_destroy (server);
1415} 1450}
1416 1451
1417 1452
1418/** 1453/**
1419 * Initial task for the service. 1454 * Initial task for the service.
1455 *
1456 * @param cls service context
1457 * @param tc unused
1420 */ 1458 */
1421static void 1459static void
1422service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 1460service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -1425,7 +1463,7 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1425 unsigned int i; 1463 unsigned int i;
1426 1464
1427 GNUNET_RESOLVER_connect (sctx->cfg); 1465 GNUNET_RESOLVER_connect (sctx->cfg);
1428 if (sctx->lsocks != NULL) 1466 if (NULL != sctx->lsocks)
1429 sctx->server = 1467 sctx->server =
1430 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, 1468 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1431 sctx->timeout, sctx->require_found); 1469 sctx->timeout, sctx->require_found);
@@ -1433,12 +1471,12 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1433 sctx->server = 1471 sctx->server =
1434 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, 1472 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1435 sctx->timeout, sctx->require_found); 1473 sctx->timeout, sctx->require_found);
1436 if (sctx->server == NULL) 1474 if (NULL == sctx->server)
1437 { 1475 {
1438 if (sctx->addrs != NULL) 1476 if (NULL != sctx->addrs)
1439 { 1477 {
1440 i = 0; 1478 i = 0;
1441 while (sctx->addrs[i] != NULL) 1479 while (NULL != sctx->addrs[i])
1442 { 1480 {
1443 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"), 1481 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1444 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i])); 1482 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
@@ -1458,20 +1496,20 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1458 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); 1496 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1459 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); 1497 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1460 i = 0; 1498 i = 0;
1461 while ((sctx->my_handlers[i].callback != NULL)) 1499 while (NULL != sctx->my_handlers[i].callback)
1462 sctx->my_handlers[i++].callback_cls = sctx; 1500 sctx->my_handlers[i++].callback_cls = sctx;
1463 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers); 1501 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1464 if (sctx->ready_confirm_fd != -1) 1502 if (-1 != sctx->ready_confirm_fd)
1465 { 1503 {
1466 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1)); 1504 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1467 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd)); 1505 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1468 sctx->ready_confirm_fd = -1; 1506 sctx->ready_confirm_fd = -1;
1469 write_pid_file (sctx, getpid ()); 1507 write_pid_file (sctx, getpid ());
1470 } 1508 }
1471 if (sctx->addrs != NULL) 1509 if (NULL != sctx->addrs)
1472 { 1510 {
1473 i = 0; 1511 i = 0;
1474 while (sctx->addrs[i] != NULL) 1512 while (NULL != sctx->addrs[i])
1475 { 1513 {
1476 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"), 1514 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1477 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i])); 1515 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
@@ -1484,6 +1522,9 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1484 1522
1485/** 1523/**
1486 * Detach from terminal. 1524 * Detach from terminal.
1525 *
1526 * @param sctx service context
1527 * @return
1487 */ 1528 */
1488static int 1529static int
1489detach_terminal (struct GNUNET_SERVICE_Context *sctx) 1530detach_terminal (struct GNUNET_SERVICE_Context *sctx)
@@ -1504,7 +1545,7 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1504 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); 1545 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1505 return GNUNET_SYSERR; 1546 return GNUNET_SYSERR;
1506 } 1547 }
1507 if (pid != 0) 1548 if (0 != pid)
1508 { 1549 {
1509 /* Parent */ 1550 /* Parent */
1510 char c; 1551 char c;
@@ -1548,7 +1589,7 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1548 (void) CLOSE (nullfd); 1589 (void) CLOSE (nullfd);
1549 /* Detach from controlling terminal */ 1590 /* Detach from controlling terminal */
1550 pid = setsid (); 1591 pid = setsid ();
1551 if (pid == -1) 1592 if (-1 == pid)
1552 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid"); 1593 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1553 sctx->ready_confirm_fd = filedes[1]; 1594 sctx->ready_confirm_fd = filedes[1];
1554#else 1595#else
@@ -1562,6 +1603,9 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1562 1603
1563/** 1604/**
1564 * Set user ID. 1605 * Set user ID.
1606 *
1607 * @param sctx service context
1608 * @return
1565 */ 1609 */
1566static int 1610static int
1567set_user_id (struct GNUNET_SERVICE_Context *sctx) 1611set_user_id (struct GNUNET_SERVICE_Context *sctx)
@@ -1575,7 +1619,7 @@ set_user_id (struct GNUNET_SERVICE_Context *sctx)
1575 1619
1576 errno = 0; 1620 errno = 0;
1577 pws = getpwnam (user); 1621 pws = getpwnam (user);
1578 if (pws == NULL) 1622 if (NULL == pws)
1579 { 1623 {
1580 LOG (GNUNET_ERROR_TYPE_ERROR, 1624 LOG (GNUNET_ERROR_TYPE_ERROR,
1581 _("Cannot obtain information about user `%s': %s\n"), user, 1625 _("Cannot obtain information about user `%s': %s\n"), user,
@@ -1606,13 +1650,15 @@ set_user_id (struct GNUNET_SERVICE_Context *sctx)
1606 1650
1607/** 1651/**
1608 * Delete the PID file that was created by our parent. 1652 * Delete the PID file that was created by our parent.
1653 *
1654 * @param sctx service context
1609 */ 1655 */
1610static void 1656static void
1611pid_file_delete (struct GNUNET_SERVICE_Context *sctx) 1657pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1612{ 1658{
1613 char *pif = get_pid_file_name (sctx); 1659 char *pif = get_pid_file_name (sctx);
1614 1660
1615 if (pif == NULL) 1661 if (NULL == pif)
1616 return; /* no PID file */ 1662 return; /* no PID file */
1617 if (0 != UNLINK (pif)) 1663 if (0 != UNLINK (pif))
1618 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif); 1664 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
@@ -1687,14 +1733,12 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1687 goto shutdown; 1733 goto shutdown;
1688 if (GNUNET_OK != setup_service (&sctx)) 1734 if (GNUNET_OK != setup_service (&sctx))
1689 goto shutdown; 1735 goto shutdown;
1690 if ((do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx))) 1736 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1691 HANDLE_ERROR; 1737 HANDLE_ERROR;
1692 if (GNUNET_OK != set_user_id (&sctx)) 1738 if (GNUNET_OK != set_user_id (&sctx))
1693 goto shutdown; 1739 goto shutdown;
1694#if DEBUG_SERVICE
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, 1740 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn); 1741 "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn);
1697#endif
1698 if ((GNUNET_OK == 1742 if ((GNUNET_OK ==
1699 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING", 1743 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1700 "SKEW_OFFSET", &skew_offset)) && 1744 "SKEW_OFFSET", &skew_offset)) &&
@@ -1704,21 +1748,19 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1704 { 1748 {
1705 clock_offset = skew_offset - skew_variance; 1749 clock_offset = skew_offset - skew_variance;
1706 GNUNET_TIME_set_offset (clock_offset); 1750 GNUNET_TIME_set_offset (clock_offset);
1707#if DEBUG_SERVICE
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset); 1751 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1709#endif
1710 } 1752 }
1711 /* actually run service */ 1753 /* actually run service */
1712 err = 0; 1754 err = 0;
1713 GNUNET_SCHEDULER_run (&service_task, &sctx); 1755 GNUNET_SCHEDULER_run (&service_task, &sctx);
1714 1756
1715 /* shutdown */ 1757 /* shutdown */
1716 if ((do_daemonize == 1) && (sctx.server != NULL)) 1758 if ((1 == do_daemonize) && (NULL != sctx.server))
1717 pid_file_delete (&sctx); 1759 pid_file_delete (&sctx);
1718 GNUNET_free_non_null (sctx.my_handlers); 1760 GNUNET_free_non_null (sctx.my_handlers);
1719 1761
1720shutdown: 1762shutdown:
1721 if (sctx.ready_confirm_fd != -1) 1763 if (-1 != sctx.ready_confirm_fd)
1722 { 1764 {
1723 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1)) 1765 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1724 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write"); 1766 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
@@ -1727,8 +1769,8 @@ shutdown:
1727 1769
1728 GNUNET_CONFIGURATION_destroy (cfg); 1770 GNUNET_CONFIGURATION_destroy (cfg);
1729 i = 0; 1771 i = 0;
1730 if (sctx.addrs != NULL) 1772 if (NULL != sctx.addrs)
1731 while (sctx.addrs[i] != NULL) 1773 while (NULL != sctx.addrs[i])
1732 GNUNET_free (sctx.addrs[i++]); 1774 GNUNET_free (sctx.addrs[i++]);
1733 GNUNET_free_non_null (sctx.addrs); 1775 GNUNET_free_non_null (sctx.addrs);
1734 GNUNET_free_non_null (sctx.addrlens); 1776 GNUNET_free_non_null (sctx.addrlens);
@@ -1772,7 +1814,7 @@ GNUNET_SERVICE_start (const char *serviceName,
1772 GNUNET_SERVICE_stop (sctx); 1814 GNUNET_SERVICE_stop (sctx);
1773 return NULL; 1815 return NULL;
1774 } 1816 }
1775 if (sctx->lsocks != NULL) 1817 if (NULL != sctx->lsocks)
1776 sctx->server = 1818 sctx->server =
1777 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, 1819 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1778 sctx->timeout, sctx->require_found); 1820 sctx->timeout, sctx->require_found);
@@ -1795,6 +1837,7 @@ GNUNET_SERVICE_start (const char *serviceName,
1795 return sctx; 1837 return sctx;
1796} 1838}
1797 1839
1840
1798/** 1841/**
1799 * Obtain the server used by a service. Note that the server must NOT 1842 * Obtain the server used by a service. Note that the server must NOT
1800 * be destroyed by the caller. 1843 * be destroyed by the caller.
@@ -1822,10 +1865,10 @@ GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1822 if (NULL != sctx->server) 1865 if (NULL != sctx->server)
1823 GNUNET_SERVER_destroy (sctx->server); 1866 GNUNET_SERVER_destroy (sctx->server);
1824 GNUNET_free_non_null (sctx->my_handlers); 1867 GNUNET_free_non_null (sctx->my_handlers);
1825 if (sctx->addrs != NULL) 1868 if (NULL != sctx->addrs)
1826 { 1869 {
1827 i = 0; 1870 i = 0;
1828 while (sctx->addrs[i] != NULL) 1871 while (NULL != sctx->addrs[i])
1829 GNUNET_free (sctx->addrs[i++]); 1872 GNUNET_free (sctx->addrs[i++]);
1830 GNUNET_free (sctx->addrs); 1873 GNUNET_free (sctx->addrs);
1831 } 1874 }
diff --git a/src/util/winproc.c b/src/util/winproc.c
index 7cd80a939..f49c445e5 100644
--- a/src/util/winproc.c
+++ b/src/util/winproc.c
@@ -27,7 +27,6 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29 29
30#define DEBUG_WINPROC 0
31 30
32#ifdef MINGW 31#ifdef MINGW
33 32