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 =
{NULL, NULL, 0, 0}
};
/* process client requests */
- GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
GNUNET_SERVER_add_handlers (server, handlers);
}
*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 */
{
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
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);
/*
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
* 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"
/**
* Connection state.
*/
- struct GNUNET_CLIENT_Connection *sock;
+ struct GNUNET_CLIENT_Connection *client;
/**
* Function to call to get the data for transmission.
/**
* Client handle.
*/
- struct GNUNET_CLIENT_Connection *sock;
+ struct GNUNET_CLIENT_Connection *client;
/**
* Message to transmit; do not free, allocated
{
/**
- * 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.
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);
port = 0;
hostname = NULL;
}
- if (port == 0)
+ if (0 == port)
{
#if AF_UNIX
if (0 != (attempt % 2))
&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);
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;
}
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;
}
* 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;
}
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);
}
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);
}
/**
* 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! */
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);
}
}
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);
}
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);
}
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);
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;
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,
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,
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"),
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;
}
}
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",
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;
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,
/* 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;
* 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)?
* 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,
{
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 */
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;
}
}
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);
GNUNET_assert (NULL != th->th);
GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
}
- th->sock->th = NULL;
+ th->client->th = NULL;
GNUNET_free (th);
}
struct TransmitGetResponseContext *tc = cls;
uint16_t msize;
- tc->sock->tag = NULL;
+ tc->client->tag = NULL;
msize = ntohs (tc->hdr->size);
if (NULL == 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;
* 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)
* 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,
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;
}
/**
* @file src/util/configuration.c
* @brief configuration management
- *
* @author Christian Grothoff
*/
/*
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
#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.
*/
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.
*/
};
+
/**
* Set the persist option on this connection handle. Indicates
* that the underlying socket or fd should never really be closed.
void *uaddr;
struct GNUNET_CONNECTION_Credentials *gcp;
struct GNUNET_CONNECTION_Credentials gc;
-
#ifdef SO_PEERCRED
struct ucred uc;
socklen_t olen;
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));
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 */
#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 ==
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);
/**
- * 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.
*
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,
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;
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;
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,
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 */
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");
}
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
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");
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;
}
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;
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
}
-/**
- * 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,
}
-/**
- * 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
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;
}
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;
}
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;
"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);
}
/**
- * 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
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",
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 =
}
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;
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)
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 :
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);
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;
}
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 */
#define DEBUG_HKDF 0
+
#if GNUNET_BUILD
#include "platform.h"
#include "gnunet_crypto_lib.h"
#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.
*/
}
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
}
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
}
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;
}
}
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
#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;
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.
if (read_pipe != INVALID_HANDLE_VALUE)
{
-#if DEBUG_PIPE
LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe);
-#endif
break;
}
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 ();
}
/* 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. */
/* 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;
#include "platform.h"
#include "gnunet_load_lib.h"
-#define DEBUG_LOAD GNUNET_EXTRA_LOGGING
#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
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);
/*
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
#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__)
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.
*/
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.
*/
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.
*/
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);
}
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,
#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);
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;
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;
}
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;
/**
* 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);
}
}
+/**
+ * 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,
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)
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,
#endif
return GNUNET_SYSERR;
}
- if (sender != NULL)
+ if (NULL != sender)
{
if (0 == sender->suspended)
{
}
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;
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",
}
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);
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);
}
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))
{
&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. */
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);
ret =
GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
GNUNET_YES);
-
process_mst (client, ret);
}
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;
{
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);
}
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);
}
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);
}
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;
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)
{
"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);
}
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");
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;
}
-/**
- * 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 */
#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
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)
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;
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;
}
#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.
*/
}
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
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;
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;
}
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,
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,
/*
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
#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
-#define DEBUG_SERVICE GNUNET_EXTRA_LOGGING
/* ******************* access control ******************** */
*/
struct IPv4NetworkSet
{
+ /**
+ * IPv4 address.
+ */
struct in_addr network;
+
+ /**
+ * IPv4 netmask.
+ */
struct in_addr netmask;
};
*/
struct IPv6NetworkSet
{
+ /**
+ * IPv6 address.
+ */
struct in6_addr network;
+
+ /**
+ * IPv6 netmask.
+ */
struct in6_addr netmask;
};
* 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).
- * <p>
+ *
* @param routeList a string specifying the forbidden networks
* @return the converted list, NULL if the synatx is flawed
*/
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)
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)
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)
slash--;
}
result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
- while (routeList[pos] != ';')
+ while (';' != routeList[pos])
pos++;
pos++;
i++;
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)
* 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.
- * <p>
+ *
* @param routeListX a string specifying the forbidden networks
* @return the converted list, NULL if the synatx is flawed
*/
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"),
}
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] != '/'))
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
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
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) ==
return GNUNET_NO;
}
+
/**
* Check if the given IP address is in the list of IP addresses.
*
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])) !=
/* ****************** message handlers ****************** */
+/**
+ *
+ * @param cls
+ * @param size
+ * @param buf
+ * @return
+ */
static size_t
write_test (void *cls, size_t size, void *buf)
{
return sizeof (struct GNUNET_MessageHeader);
}
+
/**
* Handler for TEST message.
*
};
-
/* ****************** service core routines ************** */
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;
}
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:
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;
/**
* 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 !=
/**
* 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,
/**
- * 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,
return GNUNET_OK;
}
+
/**
* Add the given UNIX domain path as an address to the
* list (as the first entry).
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;
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);
}
#endif
- if ((port == 0) && (unixpath == NULL))
+ if ((0 == port) && (NULL == unixpath))
{
LOG (GNUNET_ERROR_TYPE_ERROR,
_
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));
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;
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]);
}
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]);
#ifdef MINGW
/**
+ *
+ *
+ * @param sctx
* @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
* and GNUNET_SYSERR on error.
*/
{
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
{
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));
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;
{
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
* - 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
}
#endif
- if ((sctx->lsocks == NULL) &&
+ if ((NULL == sctx->lsocks) &&
(GNUNET_SYSERR ==
GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
&sctx->addrs, &sctx->addrlens)))
/**
* 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 !=
/**
* Write PID file.
+ *
+ * @param sctx
+ * @param pid
+ * @return
*/
static int
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))
}
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);
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);
{
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)
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);
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]));
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]));
/**
* Detach from terminal.
+ *
+ * @param sctx service context
+ * @return
*/
static int
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;
(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
/**
* Set user ID.
+ *
+ * @param sctx service context
+ * @return
*/
static int
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,
/**
* 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);
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)) &&
{
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");
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);
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);
return sctx;
}
+
/**
* Obtain the server used by a service. Note that the server must NOT
* be destroyed by the caller.
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);
}
#include "platform.h"
#include "gnunet_common.h"
-#define DEBUG_WINPROC 0
#ifdef MINGW