From: Christian Grothoff Date: Sat, 21 Apr 2012 15:16:45 +0000 (+0000) Subject: -misc offline hacking / code cleanup in util X-Git-Tag: initial-import-from-subversion-38251~13851 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=a5ca53b73e643a0e62ded4ac9595bff38d08cb0b;p=oweals%2Fgnunet.git -misc offline hacking / code cleanup in util --- 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, cfg = c; server = serv; GNUNET_assert (serv != NULL); - GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES); GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL); 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, {NULL, NULL, 0, 0} }; /* process client requests */ - GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES); GNUNET_SERVER_add_handlers (server, handlers); } 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 *th); -/** - * Configure this connection to ignore shutdown signals. - * - * @param connection connection handle - * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default - */ -void -GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection, - int do_ignore); - #if 0 /* keep Emacsens' auto-indent happy */ { 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 @@ -397,22 +397,6 @@ void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client); -/** - * Configure this server's connections to continue handling client - * requests as usual even after we get a shutdown signal. The change - * only applies to clients that connect to the server from the outside - * using TCP after this call. Clients managed previously or those - * added using GNUNET_SERVER_connect_socket and - * GNUNET_SERVER_connect_callback are not affected by this option. - * - * @param h server handle - * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default - */ -void -GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore); - - - /** * Disable the "CORK" feature for communication with the given client, * forcing the OS to immediately flush the buffer on transmission 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, cfg = c; srv = server; GNUNET_SERVER_add_handlers (server, handlers); - GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES); nc = GNUNET_SERVER_notification_context_create (server, 16); GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL); 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 @@ /* This file is part of GNUnet. - (C) 2001, 2002, 2006, 2008, 2009 Christian Grothoff (and other contributing authors) + (C) 2001, 2002, 2006, 2008, 2009, 2012 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -26,7 +26,6 @@ * Generic TCP code for reliable, record-oriented TCP * connections between clients and service providers. */ - #include "platform.h" #include "gnunet_common.h" #include "gnunet_client_lib.h" @@ -52,7 +51,7 @@ struct GNUNET_CLIENT_TransmitHandle /** * Connection state. */ - struct GNUNET_CLIENT_Connection *sock; + struct GNUNET_CLIENT_Connection *client; /** * Function to call to get the data for transmission. @@ -112,7 +111,7 @@ struct TransmitGetResponseContext /** * Client handle. */ - struct GNUNET_CLIENT_Connection *sock; + struct GNUNET_CLIENT_Connection *client; /** * Message to transmit; do not free, allocated @@ -146,9 +145,9 @@ struct GNUNET_CLIENT_Connection { /** - * the socket handle, NULL if not live + * The connection handle, NULL if not live */ - struct GNUNET_CONNECTION_Handle *sock; + struct GNUNET_CONNECTION_Handle *connection; /** * Our configuration. @@ -260,26 +259,28 @@ static struct GNUNET_CONNECTION_Handle * do_connect (const char *service_name, const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt) { - struct GNUNET_CONNECTION_Handle *sock; + struct GNUNET_CONNECTION_Handle *connection; char *hostname; char *unixpath; unsigned long long port; - sock = NULL; + connection = NULL; #if AF_UNIX if (0 == (attempt % 2)) { /* on even rounds, try UNIX */ unixpath = NULL; - 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? */ + if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && + (0 < strlen (unixpath))) { - sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); - if (sock != NULL) + /* We have a non-NULL unixpath, need to validate it */ + connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); + if (NULL != connection) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath); GNUNET_free (unixpath); - return sock; + return connection; } } GNUNET_free_non_null (unixpath); @@ -316,7 +317,7 @@ do_connect (const char *service_name, port = 0; hostname = NULL; } - if (port == 0) + if (0 == port) { #if AF_UNIX if (0 != (attempt % 2)) @@ -328,13 +329,13 @@ do_connect (const char *service_name, &unixpath)) && (0 < strlen (unixpath))) { - sock = + connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); - if (sock != NULL) + if (NULL != connection) { GNUNET_free (unixpath); GNUNET_free_non_null (hostname); - return sock; + return connection; } } GNUNET_free_non_null (unixpath); @@ -346,10 +347,9 @@ do_connect (const char *service_name, GNUNET_free_non_null (hostname); return NULL; } - - sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port); + connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port); GNUNET_free (hostname); - return sock; + return connection; } @@ -364,17 +364,17 @@ struct GNUNET_CLIENT_Connection * GNUNET_CLIENT_connect (const char *service_name, const struct GNUNET_CONFIGURATION_Handle *cfg) { - struct GNUNET_CLIENT_Connection *ret; - struct GNUNET_CONNECTION_Handle *sock; - - sock = do_connect (service_name, cfg, 0); - ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection)); - ret->attempts = 1; - ret->sock = sock; - ret->service_name = GNUNET_strdup (service_name); - ret->cfg = cfg; - ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS; - return ret; + struct GNUNET_CLIENT_Connection *client; + struct GNUNET_CONNECTION_Handle *connection; + + connection = do_connect (service_name, cfg, 0); + client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection)); + client->attempts = 1; + client->connection = connection; + client->service_name = GNUNET_strdup (service_name); + client->cfg = cfg; + client->back_off = GNUNET_TIME_UNIT_MILLISECONDS; + return client; } @@ -389,54 +389,57 @@ GNUNET_CLIENT_connect (const char *service_name, * destroyed (unless, of course, there is an error with the server in * which case the message may still be lost). * - * @param sock handle to the service connection + * @param client handle to the service connection */ void -GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock) +GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client) { - if (sock->in_receive == GNUNET_YES) + if (GNUNET_YES == client->in_receive) { - GNUNET_CONNECTION_receive_cancel (sock->sock); - sock->in_receive = GNUNET_NO; + GNUNET_CONNECTION_receive_cancel (client->connection); + client->in_receive = GNUNET_NO; } - if (sock->th != NULL) + if (NULL != client->th) { - GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th); - sock->th = NULL; + GNUNET_CLIENT_notify_transmit_ready_cancel (client->th); + client->th = NULL; } - if (NULL != sock->sock) + if (NULL != client->connection) { - GNUNET_CONNECTION_destroy (sock->sock); - sock->sock = NULL; + GNUNET_CONNECTION_destroy (client->connection); + client->connection = NULL; } - if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != client->receive_task) { - GNUNET_SCHEDULER_cancel (sock->receive_task); - sock->receive_task = GNUNET_SCHEDULER_NO_TASK; + GNUNET_SCHEDULER_cancel (client->receive_task); + client->receive_task = GNUNET_SCHEDULER_NO_TASK; } - if (sock->tag != NULL) + if (NULL != client->tag) { - GNUNET_free (sock->tag); - sock->tag = NULL; + GNUNET_free (client->tag); + client->tag = NULL; } - sock->receiver_handler = NULL; - GNUNET_array_grow (sock->received_buf, sock->received_size, 0); - GNUNET_free (sock->service_name); - GNUNET_free (sock); + client->receiver_handler = NULL; + GNUNET_array_grow (client->received_buf, client->received_size, 0); + GNUNET_free (client->service_name); + GNUNET_free (client); } /** - * Check if message is complete + * Check if message is complete. Sets the "msg_complete" member + * in the client struct. + * + * @param client connection with the buffer to check */ static void -check_complete (struct GNUNET_CLIENT_Connection *conn) +check_complete (struct GNUNET_CLIENT_Connection *client) { - if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) && - (conn->received_pos >= - ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)-> + if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) && + (client->received_pos >= + ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)-> size))) - conn->msg_complete = GNUNET_YES; + client->msg_complete = GNUNET_YES; } @@ -456,52 +459,50 @@ static void receive_helper (void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode) { - struct GNUNET_CLIENT_Connection *conn = cls; + struct GNUNET_CLIENT_Connection *client = cls; struct GNUNET_TIME_Relative remaining; GNUNET_CLIENT_MessageHandler receive_handler; void *receive_handler_cls; - GNUNET_assert (conn->msg_complete == GNUNET_NO); - conn->in_receive = GNUNET_NO; - if ((available == 0) || (conn->sock == NULL) || (errCode != 0)) + GNUNET_assert (GNUNET_NO == client->msg_complete); + client->in_receive = GNUNET_NO; + if ((0 == available) || (NULL == client->connection) || (0 != errCode)) { /* signal timeout! */ LOG (GNUNET_ERROR_TYPE_DEBUG, - "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n", - (unsigned int) available, conn->sock == NULL ? "NULL" : "non-NULL", + "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n", + (unsigned int) available, NULL == client->connection ? "NULL" : "non-NULL", STRERROR (errCode)); - if (NULL != (receive_handler = conn->receiver_handler)) + if (NULL != (receive_handler = client->receiver_handler)) { - receive_handler_cls = conn->receiver_handler_cls; - conn->receiver_handler = NULL; + receive_handler_cls = client->receiver_handler_cls; + client->receiver_handler = NULL; receive_handler (receive_handler_cls, NULL); } return; } - /* FIXME: optimize for common fast case where buf contains the * entire message and we need no copying... */ - /* slow path: append to array */ - if (conn->received_size < conn->received_pos + available) - GNUNET_array_grow (conn->received_buf, conn->received_size, - conn->received_pos + available); - memcpy (&conn->received_buf[conn->received_pos], buf, available); - conn->received_pos += available; - check_complete (conn); + if (client->received_size < client->received_pos + available) + GNUNET_array_grow (client->received_buf, client->received_size, + client->received_pos + available); + memcpy (&client->received_buf[client->received_pos], buf, available); + client->received_pos += available; + check_complete (client); /* check for timeout */ - remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout); - if (remaining.rel_value == 0) + remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout); + if (0 == remaining.rel_value) { /* signal timeout! */ - if (NULL != conn->receiver_handler) - conn->receiver_handler (conn->receiver_handler_cls, NULL); + if (NULL != client->receiver_handler) + client->receiver_handler (client->receiver_handler_cls, NULL); return; } /* back to receive -- either for more data or to call callback! */ - GNUNET_CLIENT_receive (conn, conn->receiver_handler, - conn->receiver_handler_cls, remaining); + GNUNET_CLIENT_receive (client, client->receiver_handler, + client->receiver_handler_cls, remaining); } @@ -514,28 +515,28 @@ receive_helper (void *cls, const void *buf, size_t available, static void receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { - struct GNUNET_CLIENT_Connection *sock = cls; - GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler; + struct GNUNET_CLIENT_Connection *client = cls; + GNUNET_CLIENT_MessageHandler handler = client->receiver_handler; const struct GNUNET_MessageHeader *cmsg = - (const struct GNUNET_MessageHeader *) sock->received_buf; - void *handler_cls = sock->receiver_handler_cls; + (const struct GNUNET_MessageHeader *) client->received_buf; + void *handler_cls = client->receiver_handler_cls; uint16_t msize = ntohs (cmsg->size); char mbuf[msize]; struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf; LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n", ntohs (cmsg->type), msize); - sock->receive_task = GNUNET_SCHEDULER_NO_TASK; - GNUNET_assert (GNUNET_YES == sock->msg_complete); - GNUNET_assert (sock->received_pos >= msize); + client->receive_task = GNUNET_SCHEDULER_NO_TASK; + GNUNET_assert (GNUNET_YES == client->msg_complete); + GNUNET_assert (client->received_pos >= msize); memcpy (msg, cmsg, msize); - memmove (sock->received_buf, &sock->received_buf[msize], - sock->received_pos - msize); - sock->received_pos -= msize; - sock->msg_complete = GNUNET_NO; - sock->receiver_handler = NULL; - check_complete (sock); - if (handler != NULL) + memmove (client->received_buf, &client->received_buf[msize], + client->received_pos - msize); + client->received_pos -= msize; + client->msg_complete = GNUNET_NO; + client->receiver_handler = NULL; + check_complete (client); + if (NULL != handler) handler (handler_cls, msg); } @@ -543,17 +544,17 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) /** * Read from the service. * - * @param sock the service + * @param client the service * @param handler function to call with the message * @param handler_cls closure for handler * @param timeout how long to wait until timing out */ void -GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock, +GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client, GNUNET_CLIENT_MessageHandler handler, void *handler_cls, struct GNUNET_TIME_Relative timeout) { - if (sock->sock == NULL) + if (NULL == client->connection) { /* already disconnected, fail instantly! */ GNUNET_break (0); /* this should not happen in well-written code! */ @@ -561,21 +562,21 @@ GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock, handler (handler_cls, NULL); return; } - sock->receiver_handler = handler; - sock->receiver_handler_cls = handler_cls; - sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); - if (GNUNET_YES == sock->msg_complete) + client->receiver_handler = handler; + client->receiver_handler_cls = handler_cls; + client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); + if (GNUNET_YES == client->msg_complete) { - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task); - sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task); + client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client); } else { - GNUNET_assert (sock->in_receive == GNUNET_NO); - sock->in_receive = GNUNET_YES; + GNUNET_assert (GNUNET_NO == client->in_receive); + client->in_receive = GNUNET_YES; LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n"); - GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, - timeout, &receive_helper, sock); + GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, + timeout, &receive_helper, client); } } @@ -600,23 +601,23 @@ service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls) static void confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg) { - struct GNUNET_CLIENT_Connection *conn = cls; + struct GNUNET_CLIENT_Connection *client = cls; /* We may want to consider looking at the reply in more * detail in the future, for example, is this the * correct service? FIXME! */ - if (msg != NULL) + if (NULL != msg) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Received confirmation that service is running.\n"); - GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls, + GNUNET_SCHEDULER_add_continuation (client->test_cb, client->test_cb_cls, GNUNET_SCHEDULER_REASON_PREREQ_DONE); } else { - service_test_error (conn->test_cb, conn->test_cb_cls); + service_test_error (client->test_cb, client->test_cb_cls); } - GNUNET_CLIENT_disconnect (conn); + GNUNET_CLIENT_disconnect (client); } @@ -632,23 +633,23 @@ confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg) static size_t write_test (void *cls, size_t size, void *buf) { - struct GNUNET_CLIENT_Connection *conn = cls; + struct GNUNET_CLIENT_Connection *client = cls; struct GNUNET_MessageHeader *msg; if (size < sizeof (struct GNUNET_MessageHeader)) { LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n")); - service_test_error (conn->test_cb, conn->test_cb_cls); - GNUNET_CLIENT_disconnect (conn); + service_test_error (client->test_cb, client->test_cb_cls); + GNUNET_CLIENT_disconnect (client); return 0; /* client disconnected */ } LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST"); msg = (struct GNUNET_MessageHeader *) buf; msg->type = htons (GNUNET_MESSAGE_TYPE_TEST); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); - GNUNET_CLIENT_receive (conn, &confirm_handler, conn, + GNUNET_CLIENT_receive (client, &confirm_handler, client, GNUNET_TIME_absolute_get_remaining - (conn->test_deadline)); + (client->test_deadline)); return sizeof (struct GNUNET_MessageHeader); } @@ -675,7 +676,7 @@ GNUNET_CLIENT_service_test (const char *service, char *hostname; unsigned long long port; struct GNUNET_NETWORK_Handle *sock; - struct GNUNET_CLIENT_Connection *conn; + struct GNUNET_CLIENT_Connection *client; LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n", service); @@ -698,7 +699,7 @@ GNUNET_CLIENT_service_test (const char *service, else { sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); - if (sock != NULL) + if (NULL != sock) { memset (&s_un, 0, sizeof (s_un)); s_un.sun_family = AF_UNIX; @@ -764,7 +765,7 @@ GNUNET_CLIENT_service_test (const char *service, s_in.sin_port = htons (port); sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); - if (sock != NULL) + if (NULL != sock) { if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in, @@ -798,7 +799,7 @@ GNUNET_CLIENT_service_test (const char *service, s_in6.sin6_port = htons (port); sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0); - if (sock != NULL) + if (NULL != sock) { if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6, @@ -830,8 +831,8 @@ GNUNET_CLIENT_service_test (const char *service, GNUNET_free_non_null (hostname); /* non-localhost, try 'connect' method */ - conn = GNUNET_CLIENT_connect (service, cfg); - if (conn == NULL) + client = GNUNET_CLIENT_connect (service, cfg); + if (NULL == client) { LOG (GNUNET_ERROR_TYPE_INFO, _("Could not connect to service `%s', must not be running.\n"), @@ -839,18 +840,18 @@ GNUNET_CLIENT_service_test (const char *service, service_test_error (task, task_cls); return; } - conn->test_cb = task; - conn->test_cb_cls = task_cls; - conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout); - if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn, + client->test_cb = task; + client->test_cb_cls = task_cls; + client->test_deadline = GNUNET_TIME_relative_to_absolute (timeout); + if (NULL == GNUNET_CLIENT_notify_transmit_ready (client, sizeof (struct GNUNET_MessageHeader), timeout, GNUNET_YES, &write_test, - conn)) + client)) { LOG (GNUNET_ERROR_TYPE_WARNING, _("Failure to transmit request to service `%s'\n"), service); service_test_error (task, task_cls); - GNUNET_CLIENT_disconnect (conn); + GNUNET_CLIENT_disconnect (client); return; } } @@ -884,25 +885,17 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) struct GNUNET_TIME_Relative delay; th->reconnect_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n"); - th->sock->th = NULL; - th->notify (th->notify_cls, 0, NULL); - GNUNET_free (th); - return; - } - th->sock->sock = - do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++); - if (NULL == th->sock->sock) + th->client->connection = + do_connect (th->client->service_name, th->client->cfg, th->client->attempts++); + if (NULL == th->client->connection) { /* could happen if we're out of sockets */ delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining - (th->timeout), th->sock->back_off); - th->sock->back_off = + (th->timeout), th->client->back_off); + th->client->back_off = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply - (th->sock->back_off, 2), + (th->client->back_off, 2), GNUNET_TIME_UNIT_SECONDS); LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed %u times, trying again in %llums.\n", @@ -913,14 +906,14 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) return; } th->th = - GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size, + GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size, GNUNET_TIME_absolute_get_remaining (th->timeout), &client_notify, th); - if (th->th == NULL) + if (NULL == th->th) { GNUNET_break (0); - th->sock->th = NULL; + th->client->th = NULL; th->notify (th->notify_cls, 0, NULL); GNUNET_free (th); return; @@ -941,19 +934,17 @@ static size_t client_notify (void *cls, size_t size, void *buf) { struct GNUNET_CLIENT_TransmitHandle *th = cls; - struct GNUNET_CLIENT_Connection *sock = th->sock; + struct GNUNET_CLIENT_Connection *client = th->client; size_t ret; struct GNUNET_TIME_Relative delay; th->th = NULL; - sock->th = NULL; - if (buf == NULL) + client->th = NULL; + if (NULL == buf) { delay = GNUNET_TIME_absolute_get_remaining (th->timeout); delay.rel_value /= 2; - if ((0 != - (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) || - (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) || + if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) || (delay.rel_value < 1)) { LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -966,24 +957,24 @@ client_notify (void *cls, size_t size, void *buf) /* auto-retry */ LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect to `%s', automatically trying again.\n", - sock->service_name); - if (sock->in_receive == GNUNET_YES) + client->service_name); + if (GNUNET_YES == client->in_receive) { - GNUNET_CONNECTION_receive_cancel (sock->sock); - sock->in_receive = GNUNET_NO; + GNUNET_CONNECTION_receive_cancel (client->connection); + client->in_receive = GNUNET_NO; } - GNUNET_CONNECTION_destroy (sock->sock); - sock->sock = NULL; - delay = GNUNET_TIME_relative_min (delay, sock->back_off); - sock->back_off = + GNUNET_CONNECTION_destroy (client->connection); + client->connection = NULL; + delay = GNUNET_TIME_relative_min (delay, client->back_off); + client->back_off = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply - (sock->back_off, 2), + (client->back_off, 2), GNUNET_TIME_UNIT_SECONDS); LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed %u times, trying again in %llums.\n", MAX_ATTEMPTS - th->attempts_left, (unsigned long long) delay.rel_value); - sock->th = th; + client->th = th; th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th); return 0; @@ -1000,7 +991,7 @@ client_notify (void *cls, size_t size, void *buf) * are free in the transmission buffer. May call the notify * method immediately if enough space is available. * - * @param sock connection to the service + * @param client connection to the service * @param size number of bytes to send * @param timeout after how long should we give up (and call * notify with buf NULL and size 0)? @@ -1015,7 +1006,7 @@ client_notify (void *cls, size_t size, void *buf) * a handle if the notify callback was queued (can be used to cancel) */ struct GNUNET_CLIENT_TransmitHandle * -GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, +GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client, size_t size, struct GNUNET_TIME_Relative timeout, int auto_retry, @@ -1024,7 +1015,7 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, { struct GNUNET_CLIENT_TransmitHandle *th; - if (NULL != sock->th) + if (NULL != client->th) { /* If this breaks, you most likley called this function twice without waiting * for completion or canceling the request */ @@ -1032,31 +1023,31 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, return NULL; } th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle)); - th->sock = sock; + th->client = client; th->size = size; th->timeout = GNUNET_TIME_relative_to_absolute (timeout); th->auto_retry = auto_retry; th->notify = notify; th->notify_cls = notify_cls; th->attempts_left = MAX_ATTEMPTS; - sock->th = th; - if (sock->sock == NULL) + client->th = th; + if (NULL == client->connection) { th->reconnect_task = - GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry, + GNUNET_SCHEDULER_add_delayed (client->back_off, &client_delayed_retry, th); } else { th->th = - GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout, + GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, timeout, &client_notify, th); if (NULL == th->th) { GNUNET_break (0); GNUNET_free (th); - sock->th = NULL; + client->th = NULL; return NULL; } } @@ -1073,7 +1064,7 @@ void GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle *th) { - if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != th->reconnect_task) { GNUNET_assert (NULL == th->th); GNUNET_SCHEDULER_cancel (th->reconnect_task); @@ -1084,7 +1075,7 @@ GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle GNUNET_assert (NULL != th->th); GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th); } - th->sock->th = NULL; + th->client->th = NULL; GNUNET_free (th); } @@ -1106,7 +1097,7 @@ transmit_for_response (void *cls, size_t size, void *buf) struct TransmitGetResponseContext *tc = cls; uint16_t msize; - tc->sock->tag = NULL; + tc->client->tag = NULL; msize = ntohs (tc->hdr->size); if (NULL == buf) { @@ -1119,7 +1110,7 @@ transmit_for_response (void *cls, size_t size, void *buf) } GNUNET_assert (size >= msize); memcpy (buf, tc->hdr, msize); - GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls, + GNUNET_CLIENT_receive (tc->client, tc->rn, tc->rn_cls, GNUNET_TIME_absolute_get_remaining (tc->timeout)); GNUNET_free (tc); return msize; @@ -1133,7 +1124,7 @@ transmit_for_response (void *cls, size_t size, void *buf) * will be called with a "NULL" response (in which * case the connection should probably be destroyed). * - * @param sock connection to use + * @param client connection to use * @param hdr message to transmit * @param timeout when to give up (for both transmission * and for waiting for a response) @@ -1148,7 +1139,7 @@ transmit_for_response (void *cls, size_t size, void *buf) * is already pending */ int -GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, +GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client, const struct GNUNET_MessageHeader *hdr, struct GNUNET_TIME_Relative timeout, int auto_retry, @@ -1158,26 +1149,26 @@ GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, struct TransmitGetResponseContext *tc; uint16_t msize; - if (NULL != sock->th) + if (NULL != client->th) return GNUNET_SYSERR; - GNUNET_assert (sock->tag == NULL); + GNUNET_assert (NULL == client->tag); msize = ntohs (hdr->size); tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize); - tc->sock = sock; + tc->client = client; tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1]; memcpy (&tc[1], hdr, msize); tc->timeout = GNUNET_TIME_relative_to_absolute (timeout); tc->rn = rn; tc->rn_cls = rn_cls; if (NULL == - GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry, + GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry, &transmit_for_response, tc)) { GNUNET_break (0); GNUNET_free (tc); return GNUNET_SYSERR; } - sock->tag = tc; + client->tag = tc; return GNUNET_OK; } 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 @@ /** * @file src/util/configuration.c * @brief configuration management - * * @author Christian Grothoff */ 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 @@ /* This file is part of GNUnet. - (C) 2009 Christian Grothoff (and other contributing authors) + (C) 2009, 2012 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -44,28 +44,6 @@ #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) -/** - * Possible functions to call after connect failed or succeeded. - */ -enum ConnectContinuations -{ - /** - * Call nothing. - */ - COCO_NONE = 0, - - /** - * Call "receive_again". - */ - COCO_RECEIVE_AGAIN = 1, - - /** - * Call "transmit_ready". - */ - COCO_TRANSMIT_READY = 2 - -}; - /** * Transmission handle. There can only be one for each connection. @@ -253,21 +231,11 @@ struct GNUNET_CONNECTION_Handle */ struct GNUNET_TIME_Absolute receive_timeout; - /** - * Functions to call after connect failed or succeeded. - */ - enum ConnectContinuations ccs; - /** * Maximum number of bytes to read (for receiving). */ size_t max; - /** - * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this connection. - */ - int ignore_shutdown; - /** * Port to connect to. */ @@ -283,6 +251,7 @@ struct GNUNET_CONNECTION_Handle }; + /** * Set the persist option on this connection handle. Indicates * that the underlying socket or fd should never really be closed. @@ -361,7 +330,6 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, void *uaddr; struct GNUNET_CONNECTION_Credentials *gcp; struct GNUNET_CONNECTION_Credentials gc; - #ifdef SO_PEERCRED struct ucred uc; socklen_t olen; @@ -384,7 +352,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, sa = (struct sockaddr *) addr; v6 = (struct sockaddr_in6 *) addr; - if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr))) + if ((AF_INET6 == sa->sa_family) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr))) { /* convert to V4 address */ v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); @@ -409,7 +377,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, gcp = NULL; gc.uid = 0; gc.gid = 0; - if (sa->sa_family == AF_UNIX) + if (AF_UNIX == sa->sa_family) { #if HAVE_GETPEEREID /* most BSDs */ @@ -445,10 +413,10 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, #endif } - if ((access != NULL) && + if ((NULL != access) && (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen)))) { - if (aret == GNUNET_NO) + if (GNUNET_NO == aret) LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"), GNUNET_a2s (uaddr, addrlen)); GNUNET_break (GNUNET_OK == @@ -482,7 +450,7 @@ int GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection, void **addr, size_t * addrlen) { - if ((connection->addr == NULL) || (connection->addrlen == 0)) + if ((NULL == connection->addr) || (0 == connection->addrlen)) return GNUNET_NO; *addr = GNUNET_malloc (connection->addrlen); memcpy (*addr, connection->addr, connection->addrlen); @@ -492,72 +460,135 @@ GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection, /** - * This function is called after establishing a connection either has - * succeeded or timed out. Note that it is possible that the attempt - * timed out and that we're immediately retrying. If we are retrying, - * we need to wait again (or timeout); if we succeeded, we need to - * wait for data (or timeout). + * Tell the receiver callback that we had an IO error. * - * @param cls our connection handle - * @param tc task context describing why we are here + * @param connection connection to signal error + * @param errcode error code to send */ static void -receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); +signal_receive_error (struct GNUNET_CONNECTION_Handle *connection, int errcode) +{ + GNUNET_CONNECTION_Receiver receiver; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Receive encounters error (%s), connection closed (%p)\n", + STRERROR (errcode), + connection); + GNUNET_assert (NULL != (receiver = connection->receiver)); + connection->receiver = NULL; + receiver (connection->receiver_cls, NULL, 0, connection->addr, connection->addrlen, errcode); +} /** - * See if we are now connected. If not, wait longer for - * connect to succeed. If connected, we should be able - * to write now as well, unless we timed out. + * Tell the receiver callback that a timeout was reached. * - * @param cls our connection handle - * @param tc task context describing why we are here + * @param connection connection to signal for */ static void -transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); +signal_receive_timeout (struct GNUNET_CONNECTION_Handle *connection) +{ + GNUNET_CONNECTION_Receiver receiver; + + LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection signals timeout to receiver (%p)!\n", + connection); + GNUNET_assert (NULL != (receiver = connection->receiver)); + connection->receiver = NULL; + receiver (connection->receiver_cls, NULL, 0, NULL, 0, 0); +} /** - * We've failed for good to establish a connection. + * We failed to transmit data to the service, signal the error. * - * @param h the connection we tried to establish + * @param connection handle that had trouble + * @param ecode error code (errno) */ static void -connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) +signal_transmit_error (struct GNUNET_CONNECTION_Handle *connection, + int ecode) { - LOG (GNUNET_ERROR_TYPE_INFO, - _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), - h->hostname, h->port); - /* connect failed / timed out */ - GNUNET_break (h->ap_head == NULL); - GNUNET_break (h->ap_tail == NULL); - GNUNET_break (h->dns_active == GNUNET_NO); - GNUNET_break (h->sock == NULL); - - /* trigger jobs that used to wait on "connect_task" */ - if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) + GNUNET_CONNECTION_TransmitReadyNotify notify; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Transmission encounterd error (%s), connection closed (%p)\n", + STRERROR (ecode), + connection); + if (NULL != connection->sock) { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "connect_fail_continuation triggers receive_again (%p)\n", h); - h->ccs -= COCO_RECEIVE_AGAIN; - h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); + GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock)); + connection->sock = NULL; + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task); } - if (0 != (h->ccs & COCO_TRANSMIT_READY)) + if (GNUNET_SCHEDULER_NO_TASK != connection->read_task) { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n", - h); - GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); - GNUNET_SCHEDULER_cancel (h->nth.timeout_task); - h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; - h->ccs -= COCO_TRANSMIT_READY; - GNUNET_assert (h->nth.notify_ready != NULL); - GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); - h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h); + /* send errors trigger read errors... */ + GNUNET_SCHEDULER_cancel (connection->read_task); + connection->read_task = GNUNET_SCHEDULER_NO_TASK; + signal_receive_timeout (connection); + return; + } + if (NULL == connection->nth.notify_ready) + return; /* nobody to tell about it */ + notify = connection->nth.notify_ready; + connection->nth.notify_ready = NULL; + notify (connection->nth.notify_ready_cls, 0, NULL); +} + + +/** + * We've failed for good to establish a connection (timeout or + * no more addresses to try). + * + * @param connection the connection we tried to establish + */ +static void +connect_fail_continuation (struct GNUNET_CONNECTION_Handle *connection) +{ + LOG (GNUNET_ERROR_TYPE_INFO, + _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), + connection->hostname, connection->port); + GNUNET_break (NULL == connection->ap_head); + GNUNET_break (NULL == connection->ap_tail); + GNUNET_break (GNUNET_NO == connection->dns_active); + GNUNET_break (NULL == connection->sock); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task); + + /* signal errors for jobs that used to wait on the connection */ + if (NULL != connection->receiver) + signal_receive_error (connection, ECONNREFUSED); + if (NULL != connection->nth.notify_ready) + { + GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); + GNUNET_SCHEDULER_cancel (connection->nth.timeout_task); + connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; + signal_transmit_error (connection, ECONNREFUSED); } } +/** + * We are ready to transmit (or got a timeout). + * + * @param cls our connection handle + * @param tc task context describing why we are here + */ +static void +transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); + + +/** + * This function is called once we either timeout or have data ready + * to read. + * + * @param cls connection to read from + * @param tc scheduler context + */ +static void +receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); + + /** * We've succeeded in establishing a connection. * @@ -566,27 +597,29 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) static void connect_success_continuation (struct GNUNET_CONNECTION_Handle *connection) { - LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n", + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Connection to `%s' succeeded! (%p)\n", GNUNET_a2s (connection->addr, connection->addrlen), connection); /* trigger jobs that waited for the connection */ - if (0 != (connection->ccs & COCO_RECEIVE_AGAIN)) + if (NULL != connection->receiver) { LOG (GNUNET_ERROR_TYPE_DEBUG, - "connect_success_continuation runs receive_again (%p)\n", connection); - connection->ccs -= COCO_RECEIVE_AGAIN; - connection->read_task = GNUNET_SCHEDULER_add_now (&receive_again, connection); + "Connection succeeded, starting with receiving data (%p)\n", + connection); + connection->read_task = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining + (connection->receive_timeout), connection->sock, + &receive_ready, connection); } - if (0 != (connection->ccs & COCO_TRANSMIT_READY)) + if (NULL != connection->nth.notify_ready) { LOG (GNUNET_ERROR_TYPE_DEBUG, - "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", + "Connection succeeded, starting with sending data (%p)\n", connection); GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); GNUNET_SCHEDULER_cancel (connection->nth.timeout_task); connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; - connection->ccs -= COCO_TRANSMIT_READY; GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK); - GNUNET_assert (connection->nth.notify_ready != NULL); connection->write_task = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining (connection->nth.transmit_timeout), connection->sock, @@ -612,7 +645,7 @@ connect_probe_continuation (void *cls, int error; socklen_t len; - GNUNET_assert (ap->sock != NULL); + GNUNET_assert (NULL != ap->sock); GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, ap); len = sizeof (error); errno = 0; @@ -620,17 +653,17 @@ connect_probe_continuation (void *cls, if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || (GNUNET_OK != GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error, - &len)) || (error != 0)) + &len)) || (0 != error)) { GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); GNUNET_free (ap); - if ((NULL == connection->ap_head) && (connection->dns_active == GNUNET_NO)) + if ((NULL == connection->ap_head) && (GNUNET_NO == connection->dns_active)) connect_fail_continuation (connection); return; } - GNUNET_assert (connection->sock == NULL); + GNUNET_assert (NULL == connection->sock); connection->sock = ap->sock; - GNUNET_assert (connection->addr == NULL); + GNUNET_assert (NULL == connection->addr); connection->addr = GNUNET_malloc (ap->addrlen); memcpy (connection->addr, ap->addr, ap->addrlen); connection->addrlen = ap->addrlen; @@ -663,16 +696,16 @@ try_connect_using_address (void *cls, const struct sockaddr *addr, struct AddressProbe *ap; struct GNUNET_TIME_Relative delay; - if (addr == NULL) + if (NULL == addr) { connection->dns_active = NULL; if ((NULL == connection->ap_head) && (NULL == connection->sock)) connect_fail_continuation (connection); return; } - if (connection->sock != NULL) + if (NULL != connection->sock) return; /* already connected */ - GNUNET_assert (connection->addr == NULL); + GNUNET_assert (NULL == connection->addr); /* try to connect */ LOG (GNUNET_ERROR_TYPE_DEBUG, "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, return; /* not supported by us */ } ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0); - if (ap->sock == NULL) + if (NULL == ap->sock) { GNUNET_free (ap); return; /* not supported by OS */ @@ -706,7 +739,7 @@ try_connect_using_address (void *cls, const struct sockaddr *addr, GNUNET_a2s (ap->addr, ap->addrlen), connection); if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && - (errno != EINPROGRESS)) + (EINPROGRESS != errno)) { /* maybe refused / unsupported address, try next */ LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); @@ -720,12 +753,12 @@ try_connect_using_address (void *cls, const struct sockaddr *addr, } GNUNET_CONTAINER_DLL_insert (connection->ap_head, connection->ap_tail, ap); delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT; - if (connection->nth.notify_ready != NULL) + if (NULL != connection->nth.notify_ready) delay = GNUNET_TIME_relative_min (delay, GNUNET_TIME_absolute_get_remaining (connection-> nth.transmit_timeout)); - if (connection->receiver != NULL) + if (NULL != connection->receiver) delay = GNUNET_TIME_relative_min (delay, GNUNET_TIME_absolute_get_remaining @@ -852,15 +885,14 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family, struct GNUNET_NETWORK_Handle *s; struct GNUNET_CONNECTION_Handle *connection; - s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); - if (s == NULL) + if (NULL == s) { LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket"); return NULL; } if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && - (errno != EINPROGRESS)) + (EINPROGRESS != errno)) { /* maybe refused / unsupported address, try next */ LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); @@ -890,9 +922,9 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family, int GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection) { - if ((connection->ap_head != NULL) || (connection->dns_active != NULL)) + if ((NULL != connection->ap_head) || (NULL != connection->dns_active)) return GNUNET_YES; /* still trying to connect */ - return (connection->sock == NULL) ? GNUNET_NO : GNUNET_YES; + return (NULL == connection->sock) ? GNUNET_NO : GNUNET_YES; } @@ -911,19 +943,18 @@ GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection) LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection); GNUNET_assert (NULL == connection->nth.notify_ready); GNUNET_assert (NULL == connection->receiver); - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->read_task); - if (connection->write_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != connection->write_task) { GNUNET_SCHEDULER_cancel (connection->write_task); connection->write_task = GNUNET_SCHEDULER_NO_TASK; connection->write_buffer_off = 0; } - if (connection->read_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != connection->read_task) { GNUNET_SCHEDULER_cancel (connection->read_task); connection->read_task = GNUNET_SCHEDULER_NO_TASK; } - if (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != connection->nth.timeout_task) { GNUNET_SCHEDULER_cancel (connection->nth.timeout_task); connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; @@ -942,16 +973,16 @@ GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection) GNUNET_free (pos); } if ( (NULL != connection->sock) && - (connection->persist != GNUNET_YES) ) + (GNUNET_YES != connection->persist) ) { if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR)) && - (errno != ENOTCONN) && - (errno != ECONNRESET) ) + (ENOTCONN != errno) && + (ECONNRESET != errno) ) LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown"); } - if (connection->sock != NULL) + if (NULL != connection->sock) { - if (connection->persist != GNUNET_YES) + if (GNUNET_YES != connection->persist) GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock)); else GNUNET_free (connection->sock); /* at least no memory leak (we deliberately @@ -964,93 +995,55 @@ GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection) } -/** - * Tell the receiver callback that a timeout was reached. - */ -static void -signal_timeout (struct GNUNET_CONNECTION_Handle *connection) -{ - GNUNET_CONNECTION_Receiver receiver; - - LOG (GNUNET_ERROR_TYPE_DEBUG, "Network signals time out to receiver (%p)!\n", - connection); - GNUNET_assert (NULL != (receiver = connection->receiver)); - connection->receiver = NULL; - receiver (connection->receiver_cls, NULL, 0, NULL, 0, 0); -} - - -/** - * Tell the receiver callback that we had an IO error. - */ -static void -signal_error (struct GNUNET_CONNECTION_Handle *connection, int errcode) -{ - GNUNET_CONNECTION_Receiver receiver; - - GNUNET_assert (NULL != (receiver = connection->receiver)); - connection->receiver = NULL; - receiver (connection->receiver_cls, NULL, 0, connection->addr, connection->addrlen, errcode); -} - - /** * This function is called once we either timeout * or have data ready to read. + * + * @param cls connection to read from + * @param tc scheduler context */ static void receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_CONNECTION_Handle *connection = cls; - struct GNUNET_TIME_Absolute now; char buffer[connection->max]; ssize_t ret; GNUNET_CONNECTION_Receiver receiver; connection->read_task = GNUNET_SCHEDULER_NO_TASK; - if ((GNUNET_YES == connection->ignore_shutdown) && - (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) { /* ignore shutdown request, go again immediately */ - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Ignoring shutdown signal per configuration\n"); connection->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (connection->receive_timeout), connection->sock, &receive_ready, connection); return; } - now = GNUNET_TIME_absolute_get (); - if ((now.abs_value > connection->receive_timeout.abs_value) || - (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) || - (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) { - if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Receive from `%s' encounters error: time out by %llums... (%p)\n", - GNUNET_a2s (connection->addr, connection->addrlen), - GNUNET_TIME_absolute_get_duration (connection->receive_timeout).rel_value, - connection); - signal_timeout (connection); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Receive from `%s' encounters error: timeout (%p)\n", + GNUNET_a2s (connection->addr, connection->addrlen), + GNUNET_TIME_absolute_get_duration (connection->receive_timeout).rel_value, + connection); + signal_receive_timeout (connection); return; } - if (connection->sock == NULL) + if (NULL == connection->sock) { /* connect failed for good */ - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Receive encounters error, connection closed... (%p)\n", connection); - signal_error (connection, ECONNREFUSED); + signal_receive_error (connection, ECONNREFUSED); return; } GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, connection->sock)); RETRY: ret = GNUNET_NETWORK_socket_recv (connection->sock, buffer, connection->max); - if (ret == -1) + if (-1 == ret) { - if (errno == EINTR) + if (EINTR == errno) goto RETRY; - LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno)); - signal_error (connection, errno); + signal_receive_error (connection, errno); return; } LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -1062,49 +1055,6 @@ RETRY: } -/** - * This function is called after establishing a connection either has - * succeeded or timed out. Note that it is possible that the attempt - * timed out and that we're immediately retrying. If we are retrying, - * we need to wait again (or timeout); if we succeeded, we need to - * wait for data (or timeout). - * - * @param cls our connection handle - * @param tc task context describing why we are here - */ -static void -receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct GNUNET_CONNECTION_Handle *connection = cls; - struct GNUNET_TIME_Absolute now; - - connection->read_task = GNUNET_SCHEDULER_NO_TASK; - if (connection->sock == NULL) - { - /* not connected and no longer trying */ - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Receive encounters error, connection closed (%p)...\n", connection); - signal_error (connection, ECONNREFUSED); - return; - } - now = GNUNET_TIME_absolute_get (); - if ((now.abs_value > connection->receive_timeout.abs_value) || - (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Receive encounters error: time out (%p)...\n", connection); - signal_timeout (connection); - return; - } - GNUNET_assert (connection->sock != NULL); - /* connect succeeded, wait for data! */ - connection->read_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining - (connection->receive_timeout), connection->sock, - &receive_ready, connection); -} - - /** * Receive data from the given connection. Note that this function will * call "receiver" asynchronously using the scheduler. It will @@ -1124,42 +1074,25 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection, size_t m GNUNET_CONNECTION_Receiver receiver, void *receiver_cls) { - struct GNUNET_SCHEDULER_TaskContext tc; - - GNUNET_assert ((connection->read_task == GNUNET_SCHEDULER_NO_TASK) && - (0 == (connection->ccs & COCO_RECEIVE_AGAIN)) && - (connection->receiver == NULL)); + GNUNET_assert ((GNUNET_SCHEDULER_NO_TASK == connection->read_task) && + (NULL == connection->receiver)); connection->receiver = receiver; connection->receiver_cls = receiver_cls; connection->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); connection->max = max; - if (connection->sock != NULL) + if (NULL != connection->sock) { - memset (&tc, 0, sizeof (tc)); - tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; - receive_again (connection, &tc); + connection->read_task = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining + (connection->receive_timeout), connection->sock, + &receive_ready, connection); return; } - if ((connection->dns_active == NULL) && (connection->ap_head == NULL)) + if ((NULL == connection->dns_active) && (NULL == connection->ap_head)) { receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); return; } - connection->ccs += COCO_RECEIVE_AGAIN; -} - - -/** - * Configure this connection to ignore shutdown signals. - * - * @param connection connection handle - * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default - */ -void -GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection, - int do_ignore) -{ - connection->ignore_shutdown = do_ignore; } @@ -1174,16 +1107,11 @@ GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection, void * GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection) { - if (connection->read_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK == connection->read_task) { GNUNET_assert (connection == GNUNET_SCHEDULER_cancel (connection->read_task)); connection->read_task = GNUNET_SCHEDULER_NO_TASK; } - else - { - GNUNET_assert (0 != (connection->ccs & COCO_RECEIVE_AGAIN)); - connection->ccs -= COCO_RECEIVE_AGAIN; - } connection->receiver = NULL; return connection->receiver_cls; } @@ -1204,7 +1132,7 @@ process_notify (struct GNUNET_CONNECTION_Handle *connection) size_t size; GNUNET_CONNECTION_TransmitReadyNotify notify; - GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task); if (NULL == (notify = connection->nth.notify_ready)) return GNUNET_NO; used = connection->write_buffer_off - connection->write_buffer_pos; @@ -1255,9 +1183,8 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", connection->hostname, connection->port, GNUNET_a2s (connection->addr, connection->addrlen), connection); - GNUNET_assert (0 != (connection->ccs & COCO_TRANSMIT_READY)); - connection->ccs -= COCO_TRANSMIT_READY; /* remove request */ notify = connection->nth.notify_ready; + GNUNET_assert (NULL != notify); connection->nth.notify_ready = NULL; notify (connection->nth.notify_ready_cls, 0, NULL); } @@ -1289,41 +1216,7 @@ connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) /** - * FIXME - * - * @param connection FIXME - */ -static void -transmit_error (struct GNUNET_CONNECTION_Handle *connection) -{ - GNUNET_CONNECTION_TransmitReadyNotify notify; - - if (NULL != connection->sock) - { - GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock)); - connection->sock = NULL; - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task); - } - if (connection->read_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (connection->read_task); - connection->read_task = GNUNET_SCHEDULER_NO_TASK; - signal_timeout (connection); - return; - } - if (connection->nth.notify_ready == NULL) - return; /* nobody to tell about it */ - notify = connection->nth.notify_ready; - connection->nth.notify_ready = NULL; - notify (connection->nth.notify_ready_cls, 0, NULL); -} - - -/** - * See if we are now connected. If not, wait longer for - * connect to succeed. If connected, we should be able - * to write now as well, unless we timed out. + * We are ready to transmit (or got a timeout). * * @param cls our connection handle * @param tc task context describing why we are here @@ -1337,12 +1230,12 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) size_t have; LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", connection); - GNUNET_assert (connection->write_task != GNUNET_SCHEDULER_NO_TASK); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != connection->write_task); connection->write_task = GNUNET_SCHEDULER_NO_TASK; - GNUNET_assert (connection->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task); if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) { - if ((connection->ignore_shutdown == GNUNET_YES) && (NULL != connection->sock)) + if (NULL != connection->sock) goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmit to `%s' fails, shutdown happened (%p).\n", @@ -1367,25 +1260,22 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) return; } GNUNET_assert (NULL != connection->sock); - if (tc->write_ready == NULL) + if (NULL == tc->write_ready) { - /* special circumstances (in particular, - * PREREQ_DONE after connect): not yet ready to write, - * but no "fatal" error either. Hence retry. */ + /* special circumstances (in particular, PREREQ_DONE after + * connect): not yet ready to write, but no "fatal" error either. + * Hence retry. */ goto SCHEDULE_WRITE; } if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, connection->sock)) { - LOG (GNUNET_ERROR_TYPE_INFO, - _ - ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"), - connection); GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task); - transmit_error (connection); - return; /* connect failed for good, we're finished */ + /* special circumstances (in particular, shutdown): not yet ready + * to write, but no "fatal" error either. Hence retry. */ + goto SCHEDULE_WRITE; } GNUNET_assert (connection->write_buffer_off >= connection->write_buffer_pos); - if ((connection->nth.notify_ready != NULL) && + if ((NULL != connection->nth.notify_ready) && (connection->write_buffer_size < connection->nth.notify_size)) { connection->write_buffer = @@ -1394,7 +1284,7 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) } process_notify (connection); have = connection->write_buffer_off - connection->write_buffer_pos; - if (have == 0) + if (0 == have) { /* no data ready for writing, terminate write loop */ return; @@ -1407,21 +1297,20 @@ RETRY: GNUNET_NETWORK_socket_send (connection->sock, &connection->write_buffer[connection->write_buffer_pos], have); - if (ret == -1) + if (-1 == ret) { - if (errno == EINTR) + if (EINTR == errno) goto RETRY; - LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send"); if (GNUNET_SCHEDULER_NO_TASK != connection->write_task) { GNUNET_SCHEDULER_cancel (connection->write_task); connection->write_task = GNUNET_SCHEDULER_NO_TASK; } - transmit_error (connection); + signal_transmit_error (connection, errno); return; } LOG (GNUNET_ERROR_TYPE_DEBUG, - "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", + "Connection transmitted %u/%u bytes to `%s' (%p)\n", (unsigned int) ret, have, GNUNET_a2s (connection->addr, connection->addrlen), connection); connection->write_buffer_pos += ret; if (connection->write_buffer_pos == connection->write_buffer_off) @@ -1430,15 +1319,15 @@ RETRY: connection->write_buffer_pos = 0; connection->write_buffer_off = 0; } - if ((connection->write_buffer_off == 0) && (NULL == connection->nth.notify_ready)) + if ((0 == connection->write_buffer_off) && (NULL == connection->nth.notify_ready)) return; /* all data sent! */ /* not done writing, schedule more */ SCHEDULE_WRITE: LOG (GNUNET_ERROR_TYPE_DEBUG, "Re-scheduling transmit_ready (more to do) (%p).\n", connection); have = connection->write_buffer_off - connection->write_buffer_pos; - GNUNET_assert ((connection->nth.notify_ready != NULL) || (have > 0)); - if (connection->write_task == GNUNET_SCHEDULER_NO_TASK) + GNUNET_assert ((NULL != connection->nth.notify_ready) || (have > 0)); + if (GNUNET_SCHEDULER_NO_TASK == connection->write_task) connection->write_task = GNUNET_SCHEDULER_add_write_net ((connection->nth.notify_ready == NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : @@ -1469,12 +1358,12 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connec GNUNET_CONNECTION_TransmitReadyNotify notify, void *notify_cls) { - if (connection->nth.notify_ready != NULL) + if (NULL != connection->nth.notify_ready) { GNUNET_assert (0); return NULL; } - GNUNET_assert (notify != NULL); + GNUNET_assert (NULL != notify); GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); GNUNET_assert (connection->write_buffer_off <= connection->write_buffer_size); GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size); @@ -1485,32 +1374,32 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connec connection->nth.notify_size = size; connection->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout); GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task); - if ((connection->sock == NULL) && (connection->ap_head == NULL) && - (connection->dns_active == NULL)) + if ((NULL == connection->sock) && + (NULL == connection->ap_head) && + (NULL == connection->dns_active)) { - if (connection->write_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != connection->write_task) GNUNET_SCHEDULER_cancel (connection->write_task); connection->write_task = GNUNET_SCHEDULER_add_now (&connect_error, connection); return &connection->nth; } if (GNUNET_SCHEDULER_NO_TASK != connection->write_task) - return &connection->nth; - if (connection->sock != NULL) + return &connection->nth; /* previous transmission still in progress */ + if (NULL != connection->sock) { - LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", connection); + /* connected, try to transmit now */ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmission (%p).\n", connection); connection->write_task = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining (connection->nth.transmit_timeout), connection->sock, &transmit_ready, connection); + return &connection->nth; } - else - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", connection); - connection->ccs |= COCO_TRANSMIT_READY; - connection->nth.timeout_task = - GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection); - } + /* not yet connected, wait for connection */ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Need to wait to schedule transmission for connection, adding timeout task (%p).\n", connection); + connection->nth.timeout_task = + GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection); return &connection->nth; } @@ -1525,24 +1414,18 @@ GNUNET_CONNECTION_notify_transmit_ready_cancel (struct GNUNET_CONNECTION_TransmitHandle *th) { - GNUNET_assert (th->notify_ready != NULL); - if (0 != (th->connection->ccs & COCO_TRANSMIT_READY)) + GNUNET_assert (NULL != th->notify_ready); + th->notify_ready = NULL; + if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task) { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th); GNUNET_SCHEDULER_cancel (th->timeout_task); th->timeout_task = GNUNET_SCHEDULER_NO_TASK; - th->connection->ccs -= COCO_TRANSMIT_READY; } - else + if (GNUNET_SCHEDULER_NO_TASK != th->connection->write_task) { - if (th->connection->write_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel (th->connection->write_task); - th->connection->write_task = GNUNET_SCHEDULER_NO_TASK; - } + GNUNET_SCHEDULER_cancel (th->connection->write_task); + th->connection->write_task = GNUNET_SCHEDULER_NO_TASK; } - th->notify_ready = NULL; } /* 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 @@ #define DEBUG_HKDF 0 + #if GNUNET_BUILD #include "platform.h" #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 @@ #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) -#define DEBUG_NPIPE GNUNET_EXTRA_LOGGING - -#define DEBUG_PIPE GNUNET_EXTRA_LOGGING - /** * Block size for IO for copying files. */ @@ -732,27 +728,18 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, } else { -#if DEBUG_PIPE - LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to read\n"); -#endif if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead)) { if (GetLastError () != ERROR_IO_PENDING) { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ()); -#endif SetErrnoFromWinError (GetLastError ()); return GNUNET_SYSERR; } -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); -#endif GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); } -#if DEBUG_PIPE - LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead); -#endif + LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from pipe\n", bytesRead); } return bytesRead; #else @@ -794,33 +781,24 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h, } else { -#if DEBUG_PIPE - LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe, trying to read\n"); -#endif if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead)) { if (GetLastError () != ERROR_IO_PENDING) { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ()); -#endif SetErrnoFromWinError (GetLastError ()); return GNUNET_SYSERR; } else { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadFile() queued a read, cancelling\n"); -#endif CancelIo (h->h); errno = EAGAIN; return GNUNET_SYSERR; } } -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead); -#endif } return bytesRead; #else @@ -893,31 +871,23 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, } else { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); -#endif if (!WriteFile (h->h, buffer, n, &bytesWritten, h->oOverlapWrite)) { if (GetLastError () != ERROR_IO_PENDING) { SetErrnoFromWinError (GetLastError ()); -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", GetLastError ()); -#endif return GNUNET_SYSERR; } -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); -#endif if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE)) { SetErrnoFromWinError (GetLastError ()); -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Error getting overlapped result while writing to pipe: %u\n", GetLastError ()); -#endif return GNUNET_SYSERR; } } @@ -926,35 +896,27 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, DWORD ovr; if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE)) { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Error getting control overlapped result while writing to pipe: %u\n", GetLastError ()); -#endif } else { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes (ovr says %u), picking the greatest\n", bytesWritten, ovr); -#endif } } if (bytesWritten == 0) { if (n > 0) { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytesWritten); -#endif errno = EAGAIN; return GNUNET_SYSERR; } } -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten); -#endif } return bytesWritten; #else @@ -983,37 +945,27 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h, #ifdef MINGW DWORD bytesWritten; /* We do a non-overlapped write, which is as blocking as it gets */ -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n); -#endif if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) { SetErrnoFromWinError (GetLastError ()); -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", GetLastError ()); -#endif return GNUNET_SYSERR; } if (bytesWritten == 0 && n > 0) { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n"); -#endif WaitForSingleObject (h->h, INFINITE); if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) { SetErrnoFromWinError (GetLastError ()); -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", GetLastError ()); -#endif return GNUNET_SYSERR; } } -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten); -#endif return bytesWritten; #else int flags; @@ -2021,10 +1973,8 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize); -#endif /* Use CreateNamedPipe instead of CreatePipe, because the latter * returns a write handle that does not permit FILE_READ_ATTRIBUTES * access, on versions of win32 earlier than WinXP SP2. @@ -2041,9 +1991,7 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, if (read_pipe != INVALID_HANDLE_VALUE) { -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe); -#endif break; } @@ -2054,33 +2002,24 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, case ERROR_PIPE_BUSY: /* The pipe is already open with compatible parameters. * Pick a new name and retry. */ -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); -#endif continue; case ERROR_ACCESS_DENIED: /* The pipe is already open with incompatible parameters. * Pick a new name and retry. */ -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); -#endif continue; case ERROR_CALL_NOT_IMPLEMENTED: /* We are on an older Win9x platform without named pipes. * Return an anonymous pipe as the best approximation. */ -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe not implemented, resorting to " "CreatePipe: size = %lu\n", psize); -#endif if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) { -#if DEBUG_PIPE - LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", - *read_pipe_ptr); - LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", + LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p, write handle = %p\n", + *read_pipe_ptr, *write_pipe_ptr); -#endif return GNUNET_OK; } err = GetLastError (); @@ -2092,9 +2031,7 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, } /* NOTREACHED */ } -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); -#endif /* Open the named pipe for writing. * Be sure to permit FILE_READ_ATTRIBUTES access. */ @@ -2107,15 +2044,11 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, /* Failure. */ DWORD err = GetLastError (); -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); -#endif CloseHandle (read_pipe); return err; } -#if DEBUG_PIPE LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); -#endif /* Success. */ *read_pipe_ptr = read_pipe; *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 @@ #include "platform.h" #include "gnunet_load_lib.h" -#define DEBUG_LOAD GNUNET_EXTRA_LOGGING #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) 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, if (GNUNET_CONTAINER_slist_contains (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle))) { -#if DEBUG_NETWORK - LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); -#endif return GNUNET_YES; } 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 @@ /* This file is part of GNUnet. - (C) 2009 Christian Grothoff (and other contributing authors) + (C) 2009, 2012 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -26,11 +26,7 @@ #include "platform.h" #include "gnunet_common.h" -#include "gnunet_connection_lib.h" -#include "gnunet_scheduler_lib.h" -#include "gnunet_server_lib.h" -#include "gnunet_time_lib.h" -#include "gnunet_disk_lib.h" +#include "gnunet_util_lib.h" #include "gnunet_protocols.h" #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) @@ -63,10 +59,15 @@ struct HandlerList struct NotifyList { /** - * This is a linked list. + * This is a doubly linked list. */ struct NotifyList *next; + /** + * This is a doubly linked list. + */ + struct NotifyList *prev; + /** * Function to call. */ @@ -95,9 +96,14 @@ struct GNUNET_SERVER_Handle struct GNUNET_SERVER_Client *clients; /** - * Linked list of functions to call on disconnects by clients. + * Head of linked list of functions to call on disconnects by clients. */ - struct NotifyList *disconnect_notify_list; + struct NotifyList *disconnect_notify_list_head; + + /** + * Tail of linked list of functions to call on disconnects by clients. + */ + struct NotifyList *disconnect_notify_list_tail; /** * Function to call for access control. @@ -132,13 +138,6 @@ struct GNUNET_SERVER_Handle */ int require_found; - /** - * Should all of the clients of this server continue to process - * connections as usual even if we get a shutdown request? (the - * listen socket always ignores shutdown). - */ - int clients_ignore_shutdown; - /** * Alternative function to create a MST instance. */ @@ -338,12 +337,10 @@ process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CONNECTION_create_from_accept (server->access, server->access_cls, server->listen_sockets[i]); - if (sock != NULL) + if (NULL != sock) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n"); client = GNUNET_SERVER_connect_socket (server, sock); - GNUNET_CONNECTION_ignore_shutdown (sock, - server->clients_ignore_shutdown); /* decrement reference count, we don't keep "client" alive */ GNUNET_SERVER_client_drop (client); } @@ -397,14 +394,14 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) errno = 0; return NULL; } - if (port != 0) + if (0 != port) { if (GNUNET_NETWORK_socket_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); #ifdef IPV6_V6ONLY - if ((serverAddr->sa_family == AF_INET6) && + if ((AF_INET6 == serverAddr->sa_family) && (GNUNET_NETWORK_socket_setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, @@ -412,30 +409,30 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) #endif } /* bind the socket */ - if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK) + if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen)) { eno = errno; - if (errno != EADDRINUSE) + if (EADDRINUSE != errno) { /* we don't log 'EADDRINUSE' here since an IPv4 bind may * fail if we already took the port on IPv6; if both IPv4 and * IPv6 binds fail, then our caller will log using the * errno preserved in 'eno' */ LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind"); - if (port != 0) + if (0 != port) LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"), "bind", port, - (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); + (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6"); eno = 0; } else { - if (port != 0) + if (0 != port) LOG (GNUNET_ERROR_TYPE_WARNING, _("`%s' failed for port %d (%s): address already in use\n"), "bind", port, - (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6"); - else if (serverAddr->sa_family == AF_UNIX) + (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6"); + else if (AF_UNIX == serverAddr->sa_family) LOG (GNUNET_ERROR_TYPE_WARNING, _("`%s' failed for `%s': address already in use\n"), "bind", ((const struct sockaddr_un *) serverAddr)->sun_path); @@ -452,7 +449,7 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) errno = 0; return NULL; } - if (port != 0) + if (0 != port) LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n", port); return sock; @@ -478,29 +475,29 @@ GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, struct GNUNET_TIME_Relative idle_timeout, int require_found) { - struct GNUNET_SERVER_Handle *ret; + struct GNUNET_SERVER_Handle *server; struct GNUNET_NETWORK_FDSet *r; int i; - ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle)); - ret->idle_timeout = idle_timeout; - ret->listen_sockets = lsocks; - ret->access = access; - ret->access_cls = access_cls; - ret->require_found = require_found; - if (lsocks != NULL) + server = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle)); + server->idle_timeout = idle_timeout; + server->listen_sockets = lsocks; + server->access = access; + server->access_cls = access_cls; + server->require_found = require_found; + if (NULL != lsocks) { r = GNUNET_NETWORK_fdset_create (); i = 0; - while (NULL != ret->listen_sockets[i]) - GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]); - ret->listen_task = + while (NULL != server->listen_sockets[i]) + GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]); + server->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, GNUNET_TIME_UNIT_FOREVER_REL, r, NULL, - &process_listen_socket, ret); + &process_listen_socket, server); GNUNET_NETWORK_fdset_destroy (r); } - return ret; + return server; } @@ -529,23 +526,23 @@ GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls, unsigned int j; i = 0; - while (serverAddr[i] != NULL) + while (NULL != serverAddr[i]) i++; if (i > 0) { lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1)); i = 0; j = 0; - while (serverAddr[i] != NULL) + while (NULL != serverAddr[i]) { lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]); - if (lsocks[j] != NULL) + if (NULL != lsocks[j]) j++; i++; } - if (j == 0) + if (0 == j) { - if (errno != 0) + if (0 != errno) LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind"); GNUNET_free (lsocks); lsocks = NULL; @@ -563,44 +560,46 @@ GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls, /** * Free resources held by this server. * - * @param s server to destroy + * @param server server to destroy */ void -GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s) +GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server) { struct HandlerList *hpos; struct NotifyList *npos; unsigned int i; LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n"); - if (GNUNET_SCHEDULER_NO_TASK != s->listen_task) + if (GNUNET_SCHEDULER_NO_TASK != server->listen_task) { - GNUNET_SCHEDULER_cancel (s->listen_task); - s->listen_task = GNUNET_SCHEDULER_NO_TASK; + GNUNET_SCHEDULER_cancel (server->listen_task); + server->listen_task = GNUNET_SCHEDULER_NO_TASK; } - if (s->listen_sockets != NULL) + if (NULL != server->listen_sockets) { i = 0; - while (s->listen_sockets[i] != NULL) + while (NULL != server->listen_sockets[i]) GNUNET_break (GNUNET_OK == - GNUNET_NETWORK_socket_close (s->listen_sockets[i++])); - GNUNET_free (s->listen_sockets); - s->listen_sockets = NULL; + GNUNET_NETWORK_socket_close (server->listen_sockets[i++])); + GNUNET_free (server->listen_sockets); + server->listen_sockets = NULL; } - while (s->clients != NULL) - GNUNET_SERVER_client_disconnect (s->clients); - while (NULL != (hpos = s->handlers)) + while (NULL != server->clients) + GNUNET_SERVER_client_disconnect (server->clients); + while (NULL != (hpos = server->handlers)) { - s->handlers = hpos->next; + server->handlers = hpos->next; GNUNET_free (hpos); } - while (NULL != (npos = s->disconnect_notify_list)) + while (NULL != (npos = server->disconnect_notify_list_head)) { npos->callback (npos->callback_cls, NULL); - s->disconnect_notify_list = npos->next; + GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head, + server->disconnect_notify_list_tail, + npos); GNUNET_free (npos); } - GNUNET_free (s); + GNUNET_free (server); } @@ -630,6 +629,16 @@ GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server, } +/** + * Change functions used by the server to tokenize the message stream. + * (very rarely used). + * + * @param server server to modify + * @param create new tokenizer initialization function + * @param destroy new tokenizer destruction function + * @param receive new tokenizer receive function + * @param cls closure for 'create', 'receive', 'destroy' + */ void GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server, GNUNET_SERVER_MstCreateCallback create, @@ -718,9 +727,8 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, LOG (GNUNET_ERROR_TYPE_DEBUG, "Server schedules transmission of %u-byte message of type %u to client.\n", size, type); - pos = server->handlers; found = GNUNET_NO; - while (pos != NULL) + for (pos = server->handlers; NULL != pos; pos = pos->next) { i = 0; while (pos->handlers[i].callback != NULL) @@ -728,7 +736,7 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, mh = &pos->handlers[i]; if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL)) { - if ((mh->expected_size != 0) && (mh->expected_size != size)) + if ((0 != mh->expected_size) && (mh->expected_size != size)) { #if GNUNET8_NETWORK_IS_DEAD LOG (GNUNET_ERROR_TYPE_WARNING, @@ -738,7 +746,7 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, #endif return GNUNET_SYSERR; } - if (sender != NULL) + if (NULL != sender) { if (0 == sender->suspended) { @@ -755,13 +763,12 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, } i++; } - pos = pos->next; } - if (found == GNUNET_NO) + if (GNUNET_NO == found) { LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, "Received message of unknown type %d\n", type); - if (server->require_found == GNUNET_YES) + if (GNUNET_YES == server->require_found) return GNUNET_SYSERR; } return GNUNET_OK; @@ -799,10 +806,10 @@ process_incoming (void *cls, const void *buf, size_t available, static void process_mst (struct GNUNET_SERVER_Client *client, int ret) { - while ((ret != GNUNET_SYSERR) && (client->server != NULL) && + while ((GNUNET_SYSERR != ret) && (NULL != client->server) && (GNUNET_YES != client->shutdown_now) && (0 == client->suspended)) { - if (ret == GNUNET_OK) + if (GNUNET_OK == ret) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Server re-enters receive loop, timeout: %llu.\n", @@ -816,7 +823,7 @@ process_mst (struct GNUNET_SERVER_Client *client, int ret) } LOG (GNUNET_ERROR_TYPE_DEBUG, "Server processes additional messages instantly.\n"); - if (client->server->mst_receive != NULL) + if (NULL != client->server->mst_receive) ret = client->server->mst_receive (client->server->mst_cls, client->mst, client, NULL, 0, GNUNET_NO, GNUNET_YES); @@ -828,13 +835,13 @@ process_mst (struct GNUNET_SERVER_Client *client, int ret) LOG (GNUNET_ERROR_TYPE_DEBUG, "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n", ret, client->server, client->shutdown_now, client->suspended); - if (ret == GNUNET_NO) + if (GNUNET_NO == ret) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Server has more data pending but is suspended.\n"); client->receive_pending = GNUNET_SYSERR; /* data pending */ } - if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now)) + if ((GNUNET_SYSERR == ret) || (GNUNET_YES == client->shutdown_now)) GNUNET_SERVER_client_disconnect (client); GNUNET_SERVER_client_drop (client); } @@ -860,13 +867,13 @@ process_incoming (void *cls, const void *buf, size_t available, struct GNUNET_TIME_Absolute now; int ret; - GNUNET_assert (client->receive_pending == GNUNET_YES); + GNUNET_assert (GNUNET_YES == client->receive_pending); client->receive_pending = GNUNET_NO; now = GNUNET_TIME_absolute_get (); end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout); - if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) && - (client->shutdown_now != GNUNET_YES) && (server != NULL) && + if ((NULL == buf) && (0 == available) && (NULL == addr) && (0 == errCode) && + (GNUNET_YES != client->shutdown_now) && (NULL != server) && (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) && (end.abs_value > now.abs_value)) { @@ -881,8 +888,8 @@ process_incoming (void *cls, const void *buf, size_t available, &process_incoming, client); return; } - if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) || - (client->shutdown_now == GNUNET_YES) || + if ((NULL == buf) || (0 == available) || (0 != errCode) || (NULL == server) || + (GNUNET_YES == client->shutdown_now) || (GNUNET_YES != GNUNET_CONNECTION_check (client->connection))) { /* other side closed connection, error connecting, etc. */ @@ -894,7 +901,7 @@ process_incoming (void *cls, const void *buf, size_t available, GNUNET_SERVER_client_keep (client); client->last_activity = now; - if (server->mst_receive != NULL) + if (NULL != server->mst_receive) ret = client->server->mst_receive (client->server->mst_cls, client->mst, client, buf, available, GNUNET_NO, GNUNET_YES); @@ -902,7 +909,6 @@ process_incoming (void *cls, const void *buf, size_t available, ret = GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, GNUNET_YES); - process_mst (client, ret); } @@ -918,16 +924,9 @@ static void restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_SERVER_Client *client = cls; - struct GNUNET_SERVER_Handle *server = client->server; client->restart_task = GNUNET_SCHEDULER_NO_TASK; - if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && - (GNUNET_NO == server->clients_ignore_shutdown)) - { - GNUNET_SERVER_client_disconnect (client); - return; - } - if (client->receive_pending == GNUNET_NO) + if (GNUNET_NO == client->receive_pending) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n"); client->receive_pending = GNUNET_YES; @@ -1054,7 +1053,7 @@ GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client) { GNUNET_assert (client->reference_count > 0); client->reference_count--; - if ((client->shutdown_now == GNUNET_YES) && (client->reference_count == 0)) + if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count)) GNUNET_SERVER_client_disconnect (client); } @@ -1095,8 +1094,9 @@ GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server, n = GNUNET_malloc (sizeof (struct NotifyList)); n->callback = callback; n->callback_cls = callback_cls; - n->next = server->disconnect_notify_list; - server->disconnect_notify_list = n; + GNUNET_CONTAINER_DLL_insert (server->disconnect_notify_list_head, + server->disconnect_notify_list_tail, + n); } @@ -1113,26 +1113,18 @@ GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server, callback, void *callback_cls) { struct NotifyList *pos; - struct NotifyList *prev; - prev = NULL; - pos = server->disconnect_notify_list; - while (pos != NULL) - { + for (pos = server->disconnect_notify_list_head; NULL != pos; pos = pos->next) if ((pos->callback == callback) && (pos->callback_cls == callback_cls)) break; - prev = pos; - pos = pos->next; - } - if (pos == NULL) + if (NULL == pos) { GNUNET_break (0); return; } - if (prev == NULL) - server->disconnect_notify_list = pos->next; - else - prev->next = pos->next; + GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head, + server->disconnect_notify_list_tail, + pos); GNUNET_free (pos); } @@ -1156,12 +1148,12 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client) LOG (GNUNET_ERROR_TYPE_DEBUG, "Client is being disconnected from the server.\n"); - if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != client->restart_task) { GNUNET_SCHEDULER_cancel (client->restart_task); client->restart_task = GNUNET_SCHEDULER_NO_TASK; } - if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) { GNUNET_SCHEDULER_cancel (client->warn_task); client->warn_task = GNUNET_SCHEDULER_NO_TASK; @@ -1171,40 +1163,35 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client) GNUNET_CONNECTION_receive_cancel (client->connection); client->receive_pending = GNUNET_NO; } - rc = client->reference_count; - if (client->shutdown_now != GNUNET_YES) + if (GNUNET_YES != client->shutdown_now) { server = client->server; client->shutdown_now = GNUNET_YES; prev = NULL; pos = server->clients; - while ((pos != NULL) && (pos != client)) + while ((NULL != pos) && (pos != client)) { prev = pos; pos = pos->next; } - GNUNET_assert (pos != NULL); - if (prev == NULL) + GNUNET_assert (NULL != pos); + if (NULL == prev) server->clients = pos->next; else prev->next = pos->next; - if (client->restart_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != client->restart_task) { GNUNET_SCHEDULER_cancel (client->restart_task); client->restart_task = GNUNET_SCHEDULER_NO_TASK; } - if (client->warn_task != GNUNET_SCHEDULER_NO_TASK) + if (GNUNET_SCHEDULER_NO_TASK != client->warn_task) { GNUNET_SCHEDULER_cancel (client->warn_task); client->warn_task = GNUNET_SCHEDULER_NO_TASK; } - n = server->disconnect_notify_list; - while (n != NULL) - { + for (n = server->disconnect_notify_list_head; NULL != n; n = n->next) n->callback (n->callback_cls, client); - n = n->next; - } } if (rc > 0) { @@ -1212,24 +1199,23 @@ GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client) "RC still positive, not destroying everything.\n"); return; } - if (client->in_process_client_buffer == GNUNET_YES) + if (GNUNET_YES == client->in_process_client_buffer) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Still processing inputs, not destroying everything.\n"); return; } - if (client->persist == GNUNET_YES) + if (GNUNET_YES == client->persist) GNUNET_CONNECTION_persist_ (client->connection); if (NULL != client->th.cth) GNUNET_SERVER_notify_transmit_ready_cancel (&client->th); GNUNET_CONNECTION_destroy (client->connection); - if (client->server->mst_destroy != NULL) + if (NULL != client->server->mst_destroy) client->server->mst_destroy (client->server->mst_cls, client->mst); else GNUNET_SERVER_mst_destroy (client->mst); - GNUNET_free (client); } @@ -1351,11 +1337,11 @@ GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client) void GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success) { - if (client == NULL) + if (NULL == client) return; GNUNET_assert (client->suspended > 0); client->suspended--; - if (success != GNUNET_OK) + if (GNUNET_OK != success) { LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_SERVER_receive_done called with failure indication\n"); @@ -1373,13 +1359,13 @@ GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success) GNUNET_SCHEDULER_cancel (client->warn_task); client->warn_task = GNUNET_SCHEDULER_NO_TASK; } - if (client->in_process_client_buffer == GNUNET_YES) + if (GNUNET_YES == client->in_process_client_buffer) { LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_SERVER_receive_done called while still in processing loop\n"); return; } - if ((client->server == NULL) || (GNUNET_YES == client->shutdown_now)) + if ((NULL == client->server) || (GNUNET_YES == client->shutdown_now)) { GNUNET_SERVER_client_disconnect (client); return; @@ -1391,21 +1377,4 @@ GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success) } -/** - * Configure this server's connections to continue handling client - * requests as usual even after we get a shutdown signal. The change - * only applies to clients that connect to the server from the outside - * using TCP after this call. Clients managed previously or those - * added using GNUNET_SERVER_connect_socket and - * GNUNET_SERVER_connect_callback are not affected by this option. - * - * @param h server handle - * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default - */ -void -GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore) -{ - h->clients_ignore_shutdown = do_ignore; -} - /* 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 @@ #include "gnunet_server_lib.h" #include "gnunet_time_lib.h" -#define DEBUG_SERVER_MST GNUNET_EXTRA_LOGGING #if HAVE_UNALIGNED_64_ACCESS #define ALIGN_FACTOR 4 @@ -134,11 +133,9 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, GNUNET_assert (mst->off <= mst->pos); GNUNET_assert (mst->pos <= mst->curr_buf); -#if DEBUG_SERVER_MST LOG (GNUNET_ERROR_TYPE_DEBUG, "Server-mst receives %u bytes with %u bytes already in private buffer\n", (unsigned int) size, (unsigned int) (mst->pos - mst->off)); -#endif ret = GNUNET_OK; ibuf = (char *) mst->hdr; while (mst->pos > 0) @@ -235,11 +232,9 @@ do_align: GNUNET_assert (0 == mst->pos); while (size > 0) { -#if DEBUG_SERVER_MST LOG (GNUNET_ERROR_TYPE_DEBUG, "Server-mst has %u bytes left in inbound buffer\n", (unsigned int) size); -#endif if (size < sizeof (struct GNUNET_MessageHeader)) break; offset = (unsigned long) buf; @@ -295,11 +290,9 @@ copy: mst->off = 0; mst->pos = 0; } -#if DEBUG_SERVER_MST LOG (GNUNET_ERROR_TYPE_DEBUG, "Server-mst leaves %u bytes in private buffer\n", (unsigned int) (mst->pos - mst->off)); -#endif return ret; } 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 @@ #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) -#define DEBUG_SERVER_NC GNUNET_EXTRA_LOGGING - /** * Entry in list of messages pending to be transmitted. */ @@ -171,11 +169,9 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) } if (pos == NULL) return; -#if DEBUG_SERVER_NC LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected, cleaning up %u messages in NC queue\n", pos->num_pending); -#endif if (prev == NULL) nc->clients = pos->next; else @@ -297,10 +293,8 @@ transmit_message (void *cls, size_t size, void *buf) if (buf == NULL) { /* 'cl' should be freed via disconnect notification shortly */ -#if DEBUG_SERVER_NC LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit message from NC queue to client\n"); -#endif return 0; } ret = 0; @@ -310,11 +304,9 @@ transmit_message (void *cls, size_t size, void *buf) if (size < msize) break; GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml); -#if DEBUG_SERVER_NC LOG (GNUNET_ERROR_TYPE_DEBUG, "Copying message of type %u and size %u from pending queue to transmission buffer\n", ntohs (pml->msg->type), msize); -#endif memcpy (&cbuf[ret], pml->msg, msize); ret += msize; size -= msize; @@ -323,11 +315,9 @@ transmit_message (void *cls, size_t size, void *buf) } if (pml != NULL) { -#if DEBUG_SERVER_NC LOG (GNUNET_ERROR_TYPE_DEBUG, "Have %u messages left in NC queue, will try transmission again\n", cl->num_pending); -#endif cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size), GNUNET_TIME_UNIT_FOREVER_REL, @@ -372,11 +362,9 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc, pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size); pml->msg = (const struct GNUNET_MessageHeader *) &pml[1]; pml->can_drop = can_drop; -#if DEBUG_SERVER_NC LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding message of type %u and size %u to pending queue (which has %u entries)\n", ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length); -#endif memcpy (&pml[1], msg, size); /* append */ 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 @@ /* This file is part of GNUnet. - (C) 2009 Christian Grothoff (and other contributing authors) + (C) 2009, 2012 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -42,7 +42,6 @@ #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) -#define DEBUG_SERVICE GNUNET_EXTRA_LOGGING /* ******************* access control ******************** */ @@ -51,7 +50,14 @@ */ struct IPv4NetworkSet { + /** + * IPv4 address. + */ struct in_addr network; + + /** + * IPv4 netmask. + */ struct in_addr netmask; }; @@ -60,7 +66,14 @@ struct IPv4NetworkSet */ struct IPv6NetworkSet { + /** + * IPv6 address. + */ struct in6_addr network; + + /** + * IPv6 netmask. + */ struct in6_addr netmask; }; @@ -72,7 +85,7 @@ struct IPv6NetworkSet * with a semicolon). The network must be given in dotted-decimal * notation. The netmask can be given in CIDR notation (/16) or * in dotted-decimal (/255.255.0.0). - *

+ * * @param routeList a string specifying the forbidden networks * @return the converted list, NULL if the synatx is flawed */ @@ -89,18 +102,16 @@ parse_ipv4_specification (const char *routeList) int slash; struct IPv4NetworkSet *result; - if (routeList == NULL) + if (NULL == routeList) return NULL; len = strlen (routeList); - if (len == 0) + if (0 == len) return NULL; count = 0; for (i = 0; i < len; i++) if (routeList[i] == ';') count++; result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1)); - /* add termination */ - memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1)); i = 0; pos = 0; while (i < count) @@ -109,7 +120,7 @@ parse_ipv4_specification (const char *routeList) SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0], &temps[1], &temps[2], &temps[3], &temps[4], &temps[5], &temps[6], &temps[7]); - if (cnt == 8) + if (8 == cnt) { for (j = 0; j < 8; j++) if (temps[j] > 0xFF) @@ -135,7 +146,7 @@ parse_ipv4_specification (const char *routeList) cnt = SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1], &temps[2], &temps[3], &slash); - if (cnt == 5) + if (5 == cnt) { for (j = 0; j < 4; j++) if (temps[j] > 0xFF) @@ -158,7 +169,7 @@ parse_ipv4_specification (const char *routeList) slash--; } result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); - while (routeList[pos] != ';') + while (';' != routeList[pos]) pos++; pos++; i++; @@ -178,7 +189,7 @@ parse_ipv4_specification (const char *routeList) cnt = SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1], &temps[2], &temps[3]); - if (cnt == 4) + if (4 == cnt) { for (j = 0; j < 4; j++) if (temps[j] > 0xFF) @@ -227,7 +238,7 @@ parse_ipv4_specification (const char *routeList) * with a semicolon). The network must be given in colon-hex * notation. The netmask must be given in CIDR notation (/16) or * can be omitted to specify a single host. - *

+ * * @param routeListX a string specifying the forbidden networks * @return the converted list, NULL if the synatx is flawed */ @@ -247,17 +258,17 @@ parse_ipv6_specification (const char *routeListX) unsigned int off; int save; - if (routeListX == NULL) + if (NULL == routeListX) return NULL; len = strlen (routeListX); - if (len == 0) + if (0 == len) return NULL; routeList = GNUNET_strdup (routeListX); count = 0; for (i = 0; i < len; i++) - if (routeList[i] == ';') + if (';' == routeList[i]) count++; - if (routeList[len - 1] != ';') + if (';' != routeList[len - 1]) { LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid network notation (does not end with ';': `%s')\n"), @@ -267,13 +278,12 @@ parse_ipv6_specification (const char *routeListX) } result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1)); - memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1)); i = 0; pos = 0; while (i < count) { start = pos; - while (routeList[pos] != ';') + while (';' != routeList[pos]) pos++; slash = pos; while ((slash >= start) && (routeList[slash] != '/')) @@ -292,7 +302,7 @@ parse_ipv6_specification (const char *routeListX) save = errno; if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128)) { - if (ret == 0) + if (0 == ret) LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"), &routeList[slash + 1]); else @@ -322,7 +332,7 @@ parse_ipv6_specification (const char *routeListX) ret = inet_pton (AF_INET6, &routeList[start], &result[i].network); if (ret <= 0) { - if (ret == 0) + if (0 == ret) LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"), &routeList[slash + 1]); else @@ -349,12 +359,11 @@ parse_ipv6_specification (const char *routeListX) static int check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add) { - int i; + unsigned int i; - i = 0; - if (list == NULL) + if (NULL == list) return GNUNET_NO; - + i = 0; while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0)) { 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) return GNUNET_NO; } + /** * Check if the given IP address is in the list of IP addresses. * @@ -379,13 +389,12 @@ check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip) unsigned int j; struct in6_addr zero; - if (list == NULL) + if (NULL == list) return GNUNET_NO; - memset (&zero, 0, sizeof (struct in6_addr)); i = 0; NEXT: - while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0) + while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr))) { for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++) if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) != @@ -526,6 +535,13 @@ struct GNUNET_SERVICE_Context /* ****************** message handlers ****************** */ +/** + * + * @param cls + * @param size + * @param buf + * @return + */ static size_t write_test (void *cls, size_t size, void *buf) { @@ -544,6 +560,7 @@ write_test (void *cls, size_t size, void *buf) return sizeof (struct GNUNET_MessageHeader); } + /** * Handler for TEST message. * @@ -577,7 +594,6 @@ static const struct GNUNET_SERVER_MessageHandler defhandlers[] = { }; - /* ****************** service core routines ************** */ @@ -605,32 +621,32 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, case AF_INET: GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); i4 = (const struct sockaddr_in *) addr; - ret = ((sctx->v4_allowed == NULL) || + ret = ((NULL == sctx->v4_allowed) || (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) && - ((sctx->v4_denied == NULL) || + ((NULL == sctx->v4_denied) || (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr))); break; case AF_INET6: GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); i6 = (const struct sockaddr_in6 *) addr; - ret = ((sctx->v6_allowed == NULL) || + ret = ((NULL == sctx->v6_allowed) || (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) && - ((sctx->v6_denied == NULL) || + ((NULL == sctx->v6_denied) || (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr))); break; #ifndef WINDOWS case AF_UNIX: ret = GNUNET_OK; /* always OK for now */ - if (sctx->match_uid == GNUNET_YES) + if (GNUNET_YES == sctx->match_uid) { /* UID match required */ - ret = (uc != NULL) && (uc->uid == geteuid ()); + ret = (NULL != uc) && (uc->uid == geteuid ()); } - else if ( (sctx->match_gid == GNUNET_YES) && - ( (uc == NULL) || (uc->uid != geteuid ()) ) ) + else if ( (GNUNET_YES == sctx->match_gid) && + ( (NULL == uc) || (uc->uid != geteuid ()) ) ) { /* group match required and UID does not match */ - if (uc == NULL) + if (NULL == uc) { /* no credentials, group match not possible */ ret = GNUNET_NO; @@ -667,7 +683,7 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, } if (GNUNET_NO == ret) LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"), - (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid); + (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid); break; #endif default: @@ -675,11 +691,11 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, addr->sa_family); return GNUNET_SYSERR; } - if (ret != GNUNET_OK) + if (GNUNET_OK != ret) { LOG (GNUNET_ERROR_TYPE_WARNING, - _("Access from `%s' denied to service `%s'\n"), GNUNET_a2s (addr, - addrlen), + _("Access from `%s' denied to service `%s'\n"), + GNUNET_a2s (addr, addrlen), sctx->serviceName); } return ret; @@ -689,11 +705,13 @@ check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc, /** * Get the name of the file where we will * write the PID of the service. + * + * @param sctx service context + * @return name of the file for the process ID */ static char * get_pid_file_name (struct GNUNET_SERVICE_Context *sctx) { - char *pif; if (GNUNET_OK != @@ -706,6 +724,11 @@ get_pid_file_name (struct GNUNET_SERVICE_Context *sctx) /** * Parse an IPv4 access control list. + * + * @param ret + * @param sctx + * @param option + * @return */ static int process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, @@ -733,7 +756,12 @@ process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, /** - * Parse an IPv4 access control list. + * Parse an IPv6 access control list. + * + * @param ret + * @param sctx + * @param option + * @return */ static int process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, @@ -759,6 +787,7 @@ process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx, return GNUNET_OK; } + /** * Add the given UNIX domain path as an address to the * list (as the first entry). @@ -859,8 +888,8 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); if (NULL == desc) { - if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || - (errno == EACCES)) + if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) || + (EACCES == errno)) { LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); return GNUNET_SYSERR; @@ -927,8 +956,8 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); if (NULL == desc) { - if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || - (errno == EACCES)) + if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) || + (EACCES == errno)) { LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket"); GNUNET_free_non_null (hostname); @@ -950,7 +979,7 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, } #endif - if ((port == 0) && (unixpath == NULL)) + if ((0 == port) && (NULL == unixpath)) { LOG (GNUNET_ERROR_TYPE_ERROR, _ @@ -959,7 +988,7 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, GNUNET_free_non_null (hostname); return GNUNET_SYSERR; } - if (port == 0) + if (0 == port) { saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *)); saddrlens = GNUNET_malloc (2 * sizeof (socklen_t)); @@ -971,13 +1000,11 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, return 1; } - if (hostname != NULL) + if (NULL != hostname) { -#if DEBUG_SERVICE LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolving `%s' since that is where `%s' will bind to.\n", hostname, serviceName); -#endif memset (&hints, 0, sizeof (struct addrinfo)); if (disablev6) hints.ai_family = AF_INET; @@ -1023,19 +1050,17 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, while (NULL != (pos = next)) { next = pos->ai_next; - if ((disablev6) && (pos->ai_family == AF_INET6)) + if ((disablev6) && (AF_INET6 == pos->ai_family)) continue; - if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0)) + if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol)) continue; /* not TCP */ - if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0)) + if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype)) continue; /* huh? */ -#if DEBUG_SERVICE LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n", serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen)); -#endif - if (pos->ai_family == AF_INET) + if (AF_INET == pos->ai_family) { - GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in)); + GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen); saddrlens[i] = pos->ai_addrlen; saddrs[i] = GNUNET_malloc (saddrlens[i]); memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); @@ -1043,8 +1068,8 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, } else { - GNUNET_assert (pos->ai_family == AF_INET6); - GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6)); + GNUNET_assert (AF_INET6 == pos->ai_family); + GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen); saddrlens[i] = pos->ai_addrlen; saddrs[i] = GNUNET_malloc (saddrlens[i]); memcpy (saddrs[i], pos->ai_addr, saddrlens[i]); @@ -1121,6 +1146,9 @@ GNUNET_SERVICE_get_server_addresses (const char *serviceName, #ifdef MINGW /** + * + * + * @param sctx * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself), * and GNUNET_SYSERR on error. */ @@ -1129,22 +1157,20 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx) { const char *env_buf; int fail; - uint64_t count, i; + uint64_t count; + uint64_t i; HANDLE lsocks_pipe; env_buf = getenv ("GNUNET_OS_READ_LSOCKS"); - if ((env_buf == NULL) || (strlen (env_buf) <= 0)) - { + if ((NULL == env_buf) || (strlen (env_buf) <= 0)) return GNUNET_NO; - } /* Using W32 API directly here, because this pipe will * never be used outside of this function, and it's just too much of a bother * to create a GNUnet API that boxes a HANDLE (the way it is done with socks) */ lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10); - if (lsocks_pipe == 0 || lsocks_pipe == INVALID_HANDLE_VALUE) - return GNUNET_NO; - + if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe)) + return GNUNET_NO fail = 1; do { @@ -1153,7 +1179,7 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx) DWORD rd; ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL); - if (ret == 0 || rd != sizeof (count) || count == 0) + if ((0 == ret) || (sizeof (count) != rd) || (0 == count)) break; sctx->lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1)); @@ -1164,15 +1190,16 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx) WSAPROTOCOL_INFOA pi; uint64_t size; SOCKET s; + ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL); - if (ret == 0 || rd != sizeof (size) || size != sizeof (pi)) + if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) ) break; ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL); - if (ret == 0 || rd != sizeof (pi)) + if ( (0 == ret) || (sizeof (pi) != rd)) break; s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED); sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s); - if (sctx->lsocks[i] == NULL) + if (NULL == sctx->lsocks[i]) break; else if (i == count - 1) fail2 = 0; @@ -1190,13 +1217,12 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx) { LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not access a pre-bound socket, will try to bind myself\n")); - for (i = 0; i < count && sctx->lsocks[i] != NULL; i++) + for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++) GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i])); GNUNET_free_non_null (sctx->lsocks); sctx->lsocks = NULL; return GNUNET_NO; } - return GNUNET_YES; } #endif @@ -1217,6 +1243,7 @@ receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx) * - REJECT_FROM (disallow allow connections from specified IPv4 subnets) * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets) * + * @param sctx * @return GNUNET_OK if configuration succeeded */ static int @@ -1306,7 +1333,7 @@ setup_service (struct GNUNET_SERVICE_Context *sctx) } #endif - if ((sctx->lsocks == NULL) && + if ((NULL == sctx->lsocks) && (GNUNET_SYSERR == GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg, &sctx->addrs, &sctx->addrlens))) @@ -1330,11 +1357,13 @@ setup_service (struct GNUNET_SERVICE_Context *sctx) /** * Get the name of the user that'll be used * to provide the service. + * + * @param sctx + * @return */ static char * get_user_name (struct GNUNET_SERVICE_Context *sctx) { - char *un; if (GNUNET_OK != @@ -1346,6 +1375,10 @@ get_user_name (struct GNUNET_SERVICE_Context *sctx) /** * Write PID file. + * + * @param sctx + * @param pid + * @return */ static int write_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) /* we get to create a directory -- and claim it * as ours! */ GNUNET_DISK_directory_create (rdir); - if ((user != NULL) && (0 < strlen (user))) + if ((NULL != user) && (0 < strlen (user))) GNUNET_DISK_file_change_owner (rdir, user); } if (0 != ACCESS (rdir, W_OK | X_OK)) @@ -1382,7 +1415,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid) } GNUNET_free (rdir); pidfd = FOPEN (pif, "w"); - if (pidfd == NULL) + if (NULL == pidfd) { LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif); GNUNET_free (pif); @@ -1392,7 +1425,7 @@ write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid) if (0 > FPRINTF (pidfd, "%u", pid)) LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif); GNUNET_break (0 == FCLOSE (pidfd)); - if ((user != NULL) && (0 < strlen (user))) + if ((NULL != user) && (0 < strlen (user))) GNUNET_DISK_file_change_owner (pif, user); GNUNET_free_non_null (user); GNUNET_free (pif); @@ -1411,12 +1444,17 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_SERVER_Handle *server = cls; + // FIXME: we should not unconditionally destroy the server + // here (often only stopping 'listening' would be better) GNUNET_SERVER_destroy (server); } /** * Initial task for the service. + * + * @param cls service context + * @param tc unused */ static void service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) @@ -1425,7 +1463,7 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) unsigned int i; GNUNET_RESOLVER_connect (sctx->cfg); - if (sctx->lsocks != NULL) + if (NULL != sctx->lsocks) sctx->server = GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, sctx->timeout, sctx->require_found); @@ -1433,12 +1471,12 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sctx->server = GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens, sctx->timeout, sctx->require_found); - if (sctx->server == NULL) + if (NULL == sctx->server) { - if (sctx->addrs != NULL) + if (NULL != sctx->addrs) { i = 0; - while (sctx->addrs[i] != NULL) + while (NULL != sctx->addrs[i]) { LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"), sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i])); @@ -1458,20 +1496,20 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers)); memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers)); i = 0; - while ((sctx->my_handlers[i].callback != NULL)) + while (NULL != sctx->my_handlers[i].callback) sctx->my_handlers[i++].callback_cls = sctx; GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers); - if (sctx->ready_confirm_fd != -1) + if (-1 != sctx->ready_confirm_fd) { GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1)); GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd)); sctx->ready_confirm_fd = -1; write_pid_file (sctx, getpid ()); } - if (sctx->addrs != NULL) + if (NULL != sctx->addrs) { i = 0; - while (sctx->addrs[i] != NULL) + while (NULL != sctx->addrs[i]) { LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"), sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i])); @@ -1484,6 +1522,9 @@ service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) /** * Detach from terminal. + * + * @param sctx service context + * @return */ static int detach_terminal (struct GNUNET_SERVICE_Context *sctx) @@ -1504,7 +1545,7 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx) LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork"); return GNUNET_SYSERR; } - if (pid != 0) + if (0 != pid) { /* Parent */ char c; @@ -1548,7 +1589,7 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx) (void) CLOSE (nullfd); /* Detach from controlling terminal */ pid = setsid (); - if (pid == -1) + if (-1 == pid) LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid"); sctx->ready_confirm_fd = filedes[1]; #else @@ -1562,6 +1603,9 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx) /** * Set user ID. + * + * @param sctx service context + * @return */ static int set_user_id (struct GNUNET_SERVICE_Context *sctx) @@ -1575,7 +1619,7 @@ set_user_id (struct GNUNET_SERVICE_Context *sctx) errno = 0; pws = getpwnam (user); - if (pws == NULL) + if (NULL == pws) { LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot obtain information about user `%s': %s\n"), user, @@ -1606,13 +1650,15 @@ set_user_id (struct GNUNET_SERVICE_Context *sctx) /** * Delete the PID file that was created by our parent. + * + * @param sctx service context */ static void pid_file_delete (struct GNUNET_SERVICE_Context *sctx) { char *pif = get_pid_file_name (sctx); - if (pif == NULL) + if (NULL == pif) return; /* no PID file */ if (0 != UNLINK (pif)) LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif); @@ -1687,14 +1733,12 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, goto shutdown; if (GNUNET_OK != setup_service (&sctx)) goto shutdown; - if ((do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx))) + if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx))) HANDLE_ERROR; if (GNUNET_OK != set_user_id (&sctx)) goto shutdown; -#if DEBUG_SERVICE LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn); -#endif if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING", "SKEW_OFFSET", &skew_offset)) && @@ -1704,21 +1748,19 @@ GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName, { clock_offset = skew_offset - skew_variance; GNUNET_TIME_set_offset (clock_offset); -#if DEBUG_SERVICE LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset); -#endif } /* actually run service */ err = 0; GNUNET_SCHEDULER_run (&service_task, &sctx); /* shutdown */ - if ((do_daemonize == 1) && (sctx.server != NULL)) + if ((1 == do_daemonize) && (NULL != sctx.server)) pid_file_delete (&sctx); GNUNET_free_non_null (sctx.my_handlers); shutdown: - if (sctx.ready_confirm_fd != -1) + if (-1 != sctx.ready_confirm_fd) { if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1)) LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write"); @@ -1727,8 +1769,8 @@ shutdown: GNUNET_CONFIGURATION_destroy (cfg); i = 0; - if (sctx.addrs != NULL) - while (sctx.addrs[i] != NULL) + if (NULL != sctx.addrs) + while (NULL != sctx.addrs[i]) GNUNET_free (sctx.addrs[i++]); GNUNET_free_non_null (sctx.addrs); GNUNET_free_non_null (sctx.addrlens); @@ -1772,7 +1814,7 @@ GNUNET_SERVICE_start (const char *serviceName, GNUNET_SERVICE_stop (sctx); return NULL; } - if (sctx->lsocks != NULL) + if (NULL != sctx->lsocks) sctx->server = GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks, sctx->timeout, sctx->require_found); @@ -1795,6 +1837,7 @@ GNUNET_SERVICE_start (const char *serviceName, return sctx; } + /** * Obtain the server used by a service. Note that the server must NOT * be destroyed by the caller. @@ -1822,10 +1865,10 @@ GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx) if (NULL != sctx->server) GNUNET_SERVER_destroy (sctx->server); GNUNET_free_non_null (sctx->my_handlers); - if (sctx->addrs != NULL) + if (NULL != sctx->addrs) { i = 0; - while (sctx->addrs[i] != NULL) + while (NULL != sctx->addrs[i]) GNUNET_free (sctx->addrs[i++]); GNUNET_free (sctx->addrs); } 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 @@ #include "platform.h" #include "gnunet_common.h" -#define DEBUG_WINPROC 0 #ifdef MINGW