-improve UDP logging
[oweals/gnunet.git] / src / core / gnunet-service-core_clients.c
index 5a30138a096e80e24a8263e256d270d2bfbc6132..98072a2cab226697526b892bfb29b539d37abd96 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2010, 2011 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
@@ -255,14 +255,15 @@ send_to_all_clients (const struct GNUNET_PeerIdentity *partner,
 
 
 /**
- * Handle CORE_INIT request.
+ * Handle #GNUNET_MESSAGE_TYPE_CORE_INIT request.
  *
  * @param cls unused
- * @param client new client that sent INIT
- * @param message the 'struct InitMessage' (presumably)
+ * @param client new client that sent #GNUNET_MESSAGE_TYPE_CORE_INIT
+ * @param message the `struct InitMessage` (presumably)
  */
 static void
-handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
+handle_client_init (void *cls,
+                    struct GNUNET_SERVER_Client *client,
                     const struct GNUNET_MessageHeader *message)
 {
   const struct InitMessage *im;
@@ -324,14 +325,15 @@ handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
 
 
 /**
- * Handle CORE_SEND_REQUEST message.
+ * Handle #GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST message.
  *
  * @param cls unused
- * @param client new client that sent CORE_SEND_REQUEST
- * @param message the 'struct SendMessageRequest' (presumably)
+ * @param client new client that sent a #GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST
+ * @param message the `struct SendMessageRequest` (presumably)
  */
 static void
-handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
+handle_client_send_request (void *cls,
+                            struct GNUNET_SERVER_Client *client,
                             const struct GNUNET_MessageHeader *message)
 {
   const struct SendMessageRequest *req;
@@ -341,23 +343,26 @@ handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
 
   req = (const struct SendMessageRequest *) message;
   c = find_client (client);
-  if (c == NULL)
+  if (NULL == c)
   {
     /* client did not send INIT first! */
     GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    GNUNET_SERVER_receive_done (client,
+                                GNUNET_SYSERR);
     return;
   }
-  if (c->requests == NULL)
-    c->requests = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_NO);
+  if (NULL == c->requests)
+    c->requests = GNUNET_CONTAINER_multipeermap_create (16,
+                                                        GNUNET_NO);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Client asked for transmission to `%s'\n",
               GNUNET_i2s (&req->peer));
   is_loopback =
       (0 ==
-       memcmp (&req->peer, &GSC_my_identity,
+       memcmp (&req->peer,
+               &GSC_my_identity,
                sizeof (struct GNUNET_PeerIdentity)));
-  if ((!is_loopback) &&
+  if ((! is_loopback) &&
       (GNUNET_YES !=
        GNUNET_CONTAINER_multipeermap_contains (c->connectmap,
                                                &req->peer)))
@@ -369,11 +374,13 @@ handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
                               gettext_noop
                               ("# send requests dropped (disconnected)"), 1,
                               GNUNET_NO);
-    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    GNUNET_SERVER_receive_done (client,
+                                GNUNET_OK);
     return;
   }
 
-  car = GNUNET_CONTAINER_multipeermap_get (c->requests, &req->peer);
+  car = GNUNET_CONTAINER_multipeermap_get (c->requests,
+                                           &req->peer);
   if (NULL == car)
   {
     /* create new entry */
@@ -387,6 +394,8 @@ handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
   }
   else
   {
+    /* dequeue and recycle memory from pending request, there can only
+       be at most one per client and peer */
     GSC_SESSIONS_dequeue_request (car);
   }
   car->target = req->peer;
@@ -439,7 +448,8 @@ struct TokenizerContext
  * @param message the `struct SendMessage`
  */
 static void
-handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
+handle_client_send (void *cls,
+                    struct GNUNET_SERVER_Client *client,
                     const struct GNUNET_MessageHeader *message)
 {
   const struct SendMessage *sm;
@@ -549,8 +559,8 @@ client_tokenizer_callback (void *cls, void *client,
   else
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Delivering message of type %u to %s\n",
-                ntohs (message->type),
+                "Delivering message of type %u and size %u to %s\n",
+                ntohs (message->type), ntohs (message->size),
                 GNUNET_i2s (&car->target));
     GSC_CLIENTS_deliver_message (&car->target, message,
                                 ntohs (message->size),
@@ -600,11 +610,12 @@ destroy_active_client_request (void *cls,
  * @param client identification of the client
  */
 static void
-handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
+handle_client_disconnect (void *cls,
+                          struct GNUNET_SERVER_Client *client)
 {
   struct GSC_Client *c;
 
-  if (client == NULL)
+  if (NULL == client)
     return;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Client %p has disconnected from core service.\n", client);
@@ -698,8 +709,7 @@ GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car)
  */
 void
 GSC_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
-                                           const struct GNUNET_PeerIdentity
-                                           *neighbour,
+                                           const struct GNUNET_PeerIdentity *neighbour,
                                            const struct GSC_TypeMap *tmap_old,
                                            const struct GSC_TypeMap *tmap_new)
 {
@@ -735,7 +745,8 @@ GSC_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
     cnm->header.size = htons (size);
     cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
     cnm->reserved = htonl (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Sending `%s' message to client.\n",
                 "NOTIFY_CONNECT");
     cnm->peer = *neighbour;
     send_to_client (client, &cnm->header, GNUNET_NO);
@@ -769,14 +780,13 @@ GSC_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
  * @param tmap_new updated type map for the neighbour, NULL for disconnect
  */
 void
-GSC_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity
-                                            *neighbour,
+GSC_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity *neighbour,
                                             const struct GSC_TypeMap *tmap_old,
                                             const struct GSC_TypeMap *tmap_new)
 {
   struct GSC_Client *c;
 
-  for (c = client_head; c != NULL; c = c->next)
+  for (c = client_head; NULL != c; c = c->next)
     GSC_CLIENTS_notify_client_about_neighbour (c, neighbour,
                                                tmap_old, tmap_new);
 }
@@ -814,7 +824,8 @@ GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
     return; /* no client cares about this message notification */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Core service passes message from `%4s' of type %u to client.\n",
-              GNUNET_i2s (sender), (unsigned int) ntohs (msg->type));
+              GNUNET_i2s (sender),
+              (unsigned int) ntohs (msg->type));
   GSC_SESSIONS_add_to_typemap (sender, ntohs (msg->type));
   ntm = (struct NotifyTrafficMessage *) buf;
   ntm->header.size = htons (size);
@@ -823,8 +834,13 @@ GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
   else
     ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND);
   ntm->peer = *sender;
-  memcpy (&ntm[1], msg, msize);
-  send_to_all_clients (sender, &ntm->header, GNUNET_YES, options,
+  memcpy (&ntm[1],
+          msg,
+          msize);
+  send_to_all_clients (sender,
+                       &ntm->header,
+                       GNUNET_YES,
+                       options,
                        ntohs (msg->type));
 }
 
@@ -840,8 +856,8 @@ GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_client_init, NULL,
      GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
-    {&GSC_SESSIONS_handle_client_iterate_peers, NULL,
-     GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
+    {&GSC_KX_handle_client_monitor_peers, NULL,
+     GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS,
      sizeof (struct GNUNET_MessageHeader)},
     {&handle_client_send_request, NULL,
      GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
@@ -855,7 +871,8 @@ GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
   client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
   notifier =
       GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
-  GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
+  GNUNET_SERVER_disconnect_notify (server,
+                                   &handle_client_disconnect, NULL);
   GNUNET_SERVER_add_handlers (server, handlers);
 }