- fix error messages
[oweals/gnunet.git] / src / transport / transport_api_monitoring.c
index d7bc56e09f6b84f27bbe6f38d8c2627cf690660f..b753ce73a6b228e14c1f48344363d16ee452a146 100644 (file)
@@ -19,7 +19,7 @@
 */
 
 /**
- * @file transport/transport_api_montoring.c
+ * @file transport/transport_api_monitoring.c
  * @brief montoring api for transport peer status and validation entries
  *
  * This api provides the ability to query the transport service about
@@ -140,6 +140,94 @@ struct GNUNET_TRANSPORT_ValidationMonitoringContext
   int one_shot;
 };
 
+/**
+ * Check if a state is defined as connected
+ *
+ * @param state the state value
+ * @return GNUNET_YES or GNUNET_NO
+ */
+int
+GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state)
+{
+  switch (state)
+  {
+  case GNUNET_TRANSPORT_NOT_CONNECTED:
+  case GNUNET_TRANSPORT_INIT_ATS:
+  case GNUNET_TRANSPORT_INIT_BLACKLIST:
+  case GNUNET_TRANSPORT_CONNECT_SENT:
+  case GNUNET_TRANSPORT_CONNECT_RECV_BLACKLIST_INBOUND:
+  case GNUNET_TRANSPORT_CONNECT_RECV_ATS:
+  case GNUNET_TRANSPORT_CONNECT_RECV_BLACKLIST:
+  case GNUNET_TRANSPORT_CONNECT_RECV_ACK:
+    return GNUNET_NO;
+  case GNUNET_TRANSPORT_CONNECTED:
+  case GNUNET_TRANSPORT_RECONNECT_ATS:
+  case GNUNET_TRANSPORT_RECONNECT_BLACKLIST:
+  case GNUNET_TRANSPORT_RECONNECT_SENT:
+  case GNUNET_TRANSPORT_CONNECTED_SWITCHING_BLACKLIST:
+  case GNUNET_TRANSPORT_CONNECTED_SWITCHING_CONNECT_SENT:
+    return GNUNET_YES;
+  case GNUNET_TRANSPORT_DISCONNECT:
+  case GNUNET_TRANSPORT_DISCONNECT_FINISHED:
+    return GNUNET_NO;
+  default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Unhandled state `%s' \n",
+                GNUNET_TRANSPORT_p2s (state));
+    GNUNET_break (0);
+    break;
+  }
+  return GNUNET_SYSERR;
+}
+
+/**
+ * Convert state to human-readable string.
+ *
+ * @param state the state value
+ * @return corresponding string
+ */
+const char *
+GNUNET_TRANSPORT_p2s (enum GNUNET_TRANSPORT_PeerState state)
+{
+  switch (state)
+  {
+  case GNUNET_TRANSPORT_NOT_CONNECTED:
+    return "S_NOT_CONNECTED";
+  case GNUNET_TRANSPORT_INIT_ATS:
+    return "S_INIT_ATS";
+  case GNUNET_TRANSPORT_INIT_BLACKLIST:
+    return "S_INIT_BLACKLIST";
+  case GNUNET_TRANSPORT_CONNECT_SENT:
+    return "S_CONNECT_SENT";
+  case GNUNET_TRANSPORT_CONNECT_RECV_BLACKLIST_INBOUND:
+    return "S_CONNECT_RECV_BLACKLIST_INBOUND";
+  case GNUNET_TRANSPORT_CONNECT_RECV_ATS:
+    return "S_CONNECT_RECV_ATS";
+  case GNUNET_TRANSPORT_CONNECT_RECV_BLACKLIST:
+    return "S_CONNECT_RECV_BLACKLIST";
+  case GNUNET_TRANSPORT_CONNECT_RECV_ACK:
+    return "S_CONNECT_RECV_ACK";
+  case GNUNET_TRANSPORT_CONNECTED:
+    return "S_CONNECTED";
+  case GNUNET_TRANSPORT_RECONNECT_ATS:
+    return "S_RECONNECT_ATS";
+  case GNUNET_TRANSPORT_RECONNECT_BLACKLIST:
+    return "S_RECONNECT_BLACKLIST";
+  case GNUNET_TRANSPORT_RECONNECT_SENT:
+    return "S_RECONNECT_SENT";
+  case GNUNET_TRANSPORT_CONNECTED_SWITCHING_BLACKLIST:
+    return "S_CONNECTED_SWITCHING_BLACKLIST";
+  case GNUNET_TRANSPORT_CONNECTED_SWITCHING_CONNECT_SENT:
+    return "S_CONNECTED_SWITCHING_CONNECT_SENT";
+  case GNUNET_TRANSPORT_DISCONNECT:
+    return "S_DISCONNECT";
+  case GNUNET_TRANSPORT_DISCONNECT_FINISHED:
+    return "S_DISCONNECT_FINISHED";
+  default:
+    GNUNET_break (0);
+    return "UNDEFINED";
+  }
+}
 
 
 /**
@@ -150,7 +238,7 @@ struct GNUNET_TRANSPORT_ValidationMonitoringContext
  *        message with the human-readable address
  */
 static void
-peer_address_response_processor (void *cls,
+peer_response_processor (void *cls,
                                  const struct GNUNET_MessageHeader *msg);
 
 
@@ -162,10 +250,10 @@ peer_address_response_processor (void *cls,
 static void
 send_request (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
 {
-  struct PeerIterateMessage msg;
+  struct PeerMonitorMessage msg;
 
-  msg.header.size = htons (sizeof (struct PeerIterateMessage));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
+  msg.header.size = htons (sizeof (struct PeerMonitorMessage));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
   msg.one_shot = htonl (pal_ctx->one_shot);
   msg.timeout = GNUNET_TIME_absolute_hton (pal_ctx->timeout);
   msg.peer = pal_ctx->peer;
@@ -174,7 +262,7 @@ send_request (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
                                                          &msg.header,
                                                           GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout),
                                                          GNUNET_YES,
-                                                          &peer_address_response_processor,
+                                                          &peer_response_processor,
                                                           pal_ctx));
 }
 
@@ -218,16 +306,16 @@ reconnect (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
 /**
  * Function called with responses from the service.
  *
- * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
+ * @param cls our 'struct GNUNET_TRANSPORT_PeerMonitoringContext*'
  * @param msg NULL on timeout or error, otherwise presumably a
  *        message with the human-readable address
  */
 static void
-peer_address_response_processor (void *cls,
+peer_response_processor (void *cls,
                                  const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
-  struct PeerIterateResponseMessage *air_msg;
+  struct PeerIterateResponseMessage *pir_msg;
   struct GNUNET_HELLO_Address *address;
   const char *addr;
   const char *transport_name;
@@ -240,7 +328,7 @@ peer_address_response_processor (void *cls,
     if (pal_ctx->one_shot)
     {
       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
-          S_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
+          GNUNET_TRANSPORT_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
     }
     else
@@ -251,14 +339,14 @@ peer_address_response_processor (void *cls,
   }
   size = ntohs (msg->size);
   GNUNET_break (ntohs (msg->type) ==
-                GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
+      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
   if (size == sizeof (struct GNUNET_MessageHeader))
   {
     /* done! */
     if (pal_ctx->one_shot)
     {
       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
-          S_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
+          GNUNET_TRANSPORT_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
     }
     else
@@ -270,13 +358,13 @@ peer_address_response_processor (void *cls,
 
   if ((size < sizeof (struct PeerIterateResponseMessage)) ||
       (ntohs (msg->type) !=
-       GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE))
+          GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE))
   {
     GNUNET_break (0);
     if (pal_ctx->one_shot)
     {
       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
-          S_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
+          GNUNET_TRANSPORT_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
     }
     else
@@ -286,9 +374,9 @@ peer_address_response_processor (void *cls,
     return;
   }
 
-  air_msg = (struct PeerIterateResponseMessage *) msg;
-  tlen = ntohl (air_msg->pluginlen);
-  alen = ntohl (air_msg->addrlen);
+  pir_msg = (struct PeerIterateResponseMessage *) msg;
+  tlen = ntohl (pir_msg->pluginlen);
+  alen = ntohl (pir_msg->addrlen);
 
   if (size != sizeof (struct PeerIterateResponseMessage) + tlen + alen)
   {
@@ -296,7 +384,7 @@ peer_address_response_processor (void *cls,
     if (pal_ctx->one_shot)
     {
       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
-          S_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
+          GNUNET_TRANSPORT_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
     }
     else
@@ -308,12 +396,12 @@ peer_address_response_processor (void *cls,
 
   if (alen == 0 && tlen == 0)
   {
-    pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, NULL,
-        S_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
+    pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, NULL,
+        GNUNET_TRANSPORT_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
   }
   else
   {
-    addr = (const char *) &air_msg[1];
+    addr = (const char *) &pir_msg[1];
     transport_name = &addr[alen];
 
     if (transport_name[tlen - 1] != '\0')
@@ -322,7 +410,7 @@ peer_address_response_processor (void *cls,
       if (pal_ctx->one_shot)   
       {
        pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
-           S_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
+           GNUNET_TRANSPORT_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
        GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
       }
       else
@@ -333,16 +421,16 @@ peer_address_response_processor (void *cls,
     }
 
     /* notify client */
-    address = GNUNET_HELLO_address_allocate (&air_msg->peer,
+    address = GNUNET_HELLO_address_allocate (&pir_msg->peer,
         transport_name, addr, alen);
-    pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address,
-        ntohl(air_msg->state),
-        GNUNET_TIME_absolute_ntoh (air_msg->state_timeout));
+    pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, address,
+        ntohl(pir_msg->state),
+        GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
     GNUNET_HELLO_address_free (address);
   }
 
   /* expect more replies */
-  GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor,
+  GNUNET_CLIENT_receive (pal_ctx->client, &peer_response_processor,
                          pal_ctx,
                          GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
 }
@@ -357,21 +445,24 @@ peer_address_response_processor (void *cls,
  * time with 'NULL' for the address and the peer.  After this, the operation must no
  * longer be explicitly canceled.
  *
+ * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
+ * the peer_callback!
+ *
  * @param cfg configuration to use
  * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
  * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
  *                 GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly canceled)
  * @param timeout how long is the lookup allowed to take at most (irrelevant if one_shot is set to GNUNET_NO)
- * @param peer_address_callback function to call with the results
- * @param peer_address_callback_cls closure for peer_address_callback
+ * @param peer_callback function to call with the results
+ * @param peer_callback_cls closure for peer_address_callback
  */
 struct GNUNET_TRANSPORT_PeerMonitoringContext *
 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
     const struct GNUNET_PeerIdentity *peer,
     int one_shot,
     struct GNUNET_TIME_Relative timeout,
-    GNUNET_TRANSPORT_PeerIterateCallback peer_address_callback,
-    void *peer_address_callback_cls)
+    GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
+    void *peer_callback_cls)
 {
   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx;
   struct GNUNET_CLIENT_Connection *client;
@@ -382,8 +473,8 @@ GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
   if (GNUNET_YES != one_shot)
     timeout = GNUNET_TIME_UNIT_FOREVER_REL;
   pal_ctx = GNUNET_new (struct GNUNET_TRANSPORT_PeerMonitoringContext);
-  pal_ctx->cb = peer_address_callback;
-  pal_ctx->cb_cls = peer_address_callback_cls;
+  pal_ctx->cb = peer_callback;
+  pal_ctx->cb_cls = peer_callback_cls;
   pal_ctx->cfg = cfg;
   pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
   if (NULL != peer)
@@ -397,30 +488,30 @@ GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
 
 /**
- * Cancel request for address conversion.
+ * Cancel request to monitor peers
  *
- * @param alc handle for the request to cancel
+ * @param pic handle for the request to cancel
  */
 void
-GNUNET_TRANSPORT_monitor_peers_cancel (
-    struct GNUNET_TRANSPORT_PeerMonitoringContext *alc)
+GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic)
 {
-  if (NULL != alc->client)
+  if (NULL != pic->client)
   {
-    GNUNET_CLIENT_disconnect (alc->client);
-    alc->client = NULL;
+    GNUNET_CLIENT_disconnect (pic->client);
+    pic->client = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != alc->reconnect_task)
+  if (GNUNET_SCHEDULER_NO_TASK != pic->reconnect_task)
   {
-    GNUNET_SCHEDULER_cancel (alc->reconnect_task);
-    alc->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    GNUNET_SCHEDULER_cancel (pic->reconnect_task);
+    pic->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  GNUNET_free (alc);
+  GNUNET_free (pic);
 }
 
 
 /**
- * Return information about a peer's or all current pending validation operations
+ * Return information about pending address validation operations for a specific
+ * or all peers
  *
  * @param cfg configuration to use
  * @param peer a specific peer identity to obtain validation entries for,
@@ -428,8 +519,8 @@ GNUNET_TRANSPORT_monitor_peers_cancel (
  * @param one_shot GNUNET_YES to return all entries and then end (with NULL+NULL),
  *                 GNUNET_NO to monitor validation entries continuously
  * @param timeout how long is the lookup allowed to take at most
- * @param peer_address_callback function to call with the results
- * @param peer_address_callback_cls closure for peer_address_callback
+ * @param validation_callback function to call with the results
+ * @param validation_callback_cls closure for peer_address_callback
  */
 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
 GNUNET_TRANSPORT_monitor_validation_entries (const struct
@@ -457,4 +548,4 @@ GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_Vali
 }
 
 
-/* end of transport_api_montoring.c */
+/* end of transport_api_monitoring.c */