porting gnunet-transport-list-connections to binary address
authorMatthias Wachs <wachs@net.in.tum.de>
Wed, 7 Sep 2011 16:07:48 +0000 (16:07 +0000)
committerMatthias Wachs <wachs@net.in.tum.de>
Wed, 7 Sep 2011 16:07:48 +0000 (16:07 +0000)
src/include/gnunet_transport_service.h
src/transport/gnunet-service-transport_clients.c
src/transport/gnunet-service-transport_neighbours.c
src/transport/gnunet-service-transport_validation.c
src/transport/gnunet-transport-list-connections.c
src/transport/transport_api_address_iterate.c

index 6c443cee893f3ee84aa0180cb64782440abbee70..8435271f97b6504d6114eb8b7552d3485dd167c4 100644 (file)
@@ -446,7 +446,7 @@ typedef void (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
 
 
 /**
- * Function to call with a human-readable format of an address
+ * Function to call with a binary format of an address
  *
  * @param cls closure
  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
@@ -455,6 +455,25 @@ typedef void (*GNUNET_TRANSPORT_AddressLookUpCallback) (void *cls,
                                                         const char *address);
 
 
+/**
+ * Function to call with a binary format of an address
+ *
+ * @param cls closure
+ * @param peer peer identity
+ * @param transport transport plugin
+ * @param addr address
+ * @param addrlen address length
+ */
+typedef void (*GNUNET_TRANSPORT_AddressLookUpBinaryCallback) (void *cls,
+                                                              const struct
+                                                              GNUNET_PeerIdentity
+                                                              * peer,
+                                                              const char
+                                                              *transport,
+                                                              const void *addr,
+                                                              size_t addrlen);
+
+
 /**
  * Connect to the transport service.  Note that the connection may
  * complete (or fail) asynchronously.
@@ -672,7 +691,7 @@ GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle
 void
 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                   struct GNUNET_TIME_Relative timeout,
-                                  GNUNET_TRANSPORT_AddressLookUpCallback
+                                  GNUNET_TRANSPORT_AddressLookUpBinaryCallback
                                   peer_address_callback,
                                   void *peer_address_callback_cls);
 
index 140b154544a5d5be69a10447bc2c4ebb78b68ea9..8676f335e0ccd17ad69d31be2ba3657154858cbe 100644 (file)
@@ -625,18 +625,42 @@ clients_handle_set_quota (void *cls, struct GNUNET_SERVER_Client *client,
  * @param address the resolved name, NULL to indicate the last response
  */
 static void
-transmit_address_to_client (void *cls, const char *address)
+transmit_address_to_client (void *cls, const char *buf)
 {
   struct GNUNET_SERVER_TransmitContext *tc = cls;
 
-  if (NULL == address)
+  if (NULL == buf)
   {
     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
     return;
   }
-  GNUNET_SERVER_transmit_context_append_data (tc, address, strlen (address) + 1,
+  GNUNET_SERVER_transmit_context_append_data (tc, buf, strlen (buf + 1),
+                                              GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
+}
+
+
+/**
+ * Take the given address and append it to the set of results sent back to
+ * the client.
+ *
+ * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
+ * @param address the resolved name, NULL to indicate the last response
+ */
+static void
+transmit_binary_to_client (void *cls, void *buf, size_t size)
+{
+  struct GNUNET_SERVER_TransmitContext *tc = cls;
+
+  if (NULL == buf)
+  {
+    GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
+                                                GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
+    GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
+    return;
+  }
+  GNUNET_SERVER_transmit_context_append_data (tc, buf, size,
                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
 }
 
@@ -781,18 +805,22 @@ clients_handle_peer_address_lookup (void *cls,
  * @param ats_count number of entries in ats (excluding 0-termination)
  */
 static void
-output_addresses (void *cls, const struct GNUNET_PeerIdentity *neighbour,
+output_addresses (void *cls, const struct GNUNET_PeerIdentity *peer,
                   const struct GNUNET_TRANSPORT_ATS_Information *ats,
                   uint32_t ats_count)
 {
   struct GNUNET_SERVER_TransmitContext *tc = cls;
-  char *addr_buf;
+  struct AddressIterateResponseMessage msg;
+  size_t size;
 
-  /* FIXME: move to a binary format!!! */
-  GNUNET_asprintf (&addr_buf, "%s: %s", GNUNET_i2s (neighbour),
-                   GST_plugins_a2s ("FIXME", NULL, 0));
-  transmit_address_to_client (tc, addr_buf);
-  GNUNET_free (addr_buf);
+  size =
+      sizeof (struct AddressIterateResponseMessage) -
+      sizeof (struct GNUNET_MessageHeader);
+  memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
+  msg.addrlen = ntohs (0);
+  msg.pluginlen = ntohs (0);
+
+  transmit_binary_to_client (tc, &msg, size);
 }
 
 
index 8f7d34b5d484340699adc2e68f6e1c899d26252f..9c6aaba6790e67bd1e96d73b213f79357fc1491f 100644 (file)
@@ -403,10 +403,8 @@ disconnect_neighbour (struct NeighbourMapEntry *n)
     GNUNET_assert (neighbours_connected > 0);
     neighbours_connected--;
 
-    GNUNET_STATISTICS_update (GST_stats,
-                              gettext_noop
-                              ("# peers connected"),
-                              -1, GNUNET_NO);
+    GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
+                              GNUNET_NO);
     disconnect_notify_cb (callback_cls, &n->id);
   }
   GNUNET_assert (GNUNET_YES ==
@@ -597,10 +595,8 @@ try_connect_using_address (void *cls, const struct GNUNET_PeerIdentity *target,
     return;
 
   neighbours_connected++;
-  GNUNET_STATISTICS_update (GST_stats,
-                            gettext_noop
-                            ("# peers connected"),
-                            1, GNUNET_NO);
+  GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
+                            GNUNET_NO);
   connect_notify_cb (callback_cls, target, n->ats, n->ats_count);
 }
 
index 395f4e87a494049e0f839c1e979c54368fae075b..56b4037b492cd63269c6371f71b55912f882e176 100644 (file)
@@ -409,6 +409,7 @@ add_valid_address (void *cls, const char *tname,
   ve = find_validation_entry (&public_key, &pid, tname, addr, addrlen);
   ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
   struct GNUNET_TRANSPORT_ATS_Information ats;
+
   ats.type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
   ats.value = htonl (0);
 
@@ -918,7 +919,6 @@ GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
 
   if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE!\n");
     GNUNET_break_op (0);
     return;
   }
@@ -983,6 +983,7 @@ GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
   /* validity achieved, remember it! */
   ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
   struct GNUNET_TRANSPORT_ATS_Information ats;
+
   ats.type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
   ats.value = htonl (0);
   GNUNET_ATS_address_update (GST_ats, &ve->pid, ve->valid_until, ve->transport_name, NULL, ve->addr, ve->addrlen, &ats, 1);     /* FIXME: compute and add latency here... */
index 0542a5cd9f9150eab51c70668c4558fa54d713ba..64b67dcd4a209f3820c9202499464ee43ae3d3ff 100644 (file)
@@ -53,13 +53,22 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
  */
 static void
-process_address (void *cls, const char *address)
+process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
+                 const char *transport, const void *addr, size_t addrlen)
 {
 #if VERBOSE
   connection_count++;
 #endif
-  if (address != NULL)
-    fprintf (stdout, "%s\n", address);
+  if ((peer != NULL) || (transport != NULL) ||
+      ((addr != NULL) && (addrlen > 0)))
+    fprintf (stdout, "Peer `%s' plugin: `%s' address `%s'\n",
+             (peer != NULL) ? GNUNET_i2s (peer) : "<unknown>",
+             (transport != NULL) ? transport : "<unknown>", ((addr != NULL) &&
+                                                             (addrlen > 0) &&
+                                                             (transport !=
+                                                              NULL)) ?
+             "how do i resolve the name without transport service?" :
+             "<unknown>");
 }
 
 
index 833b82d7e0aba37b687b214f8c3ec4a0eaa31963..da333798f55cd24b6d44528fa1cd9745f2e980f2 100644 (file)
@@ -47,7 +47,7 @@ struct AddressLookupCtx
   /**
    * Function to call with the human-readable address.
    */
-  GNUNET_TRANSPORT_AddressLookUpCallback cb;
+  GNUNET_TRANSPORT_AddressLookUpBinaryCallback cb;
 
   /**
    * Closure for cb.
@@ -78,41 +78,50 @@ peer_address_response_processor (void *cls,
                                  const struct GNUNET_MessageHeader *msg)
 {
   struct AddressLookupCtx *alucb = cls;
-  const char *address;
+  struct AddressIterateResponseMessage *address;
   uint16_t size;
 
+
   if (msg == NULL)
   {
-    alucb->cb (alucb->cb_cls, NULL);
+    alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
     GNUNET_free (alucb);
     return;
   }
+
   GNUNET_break (ntohs (msg->type) ==
                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
   size = ntohs (msg->size);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %u size %u\n",
+              ntohs (msg->type), size);
   if (size == sizeof (struct GNUNET_MessageHeader))
   {
     /* done! */
-    alucb->cb (alucb->cb_cls, NULL);
+    alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
     GNUNET_free (alucb);
     return;
   }
-  address = (const char *) &msg[1];
-  if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
+  if (size != sizeof (struct AddressIterateResponseMessage))
   {
     /* invalid reply */
     GNUNET_break (0);
-    alucb->cb (alucb->cb_cls, NULL);
+    alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
     GNUNET_free (alucb);
     return;
   }
+
+  address = (struct AddressIterateResponseMessage *) msg;
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PEER: %s\n",
+              GNUNET_i2s (&address->peer));
+
+
   /* expect more replies */
   GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb,
                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
-  alucb->cb (alucb->cb_cls, address);
+  alucb->cb (alucb->cb_cls, &address->peer, NULL, NULL, 0);
 }
 
 
@@ -127,7 +136,7 @@ peer_address_response_processor (void *cls,
 void
 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                   struct GNUNET_TIME_Relative timeout,
-                                  GNUNET_TRANSPORT_AddressLookUpCallback
+                                  GNUNET_TRANSPORT_AddressLookUpBinaryCallback
                                   peer_address_callback,
                                   void *peer_address_callback_cls)
 {
@@ -139,12 +148,12 @@ GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
   client = GNUNET_CLIENT_connect ("transport", cfg);
   if (client == NULL)
   {
-    peer_address_callback (peer_address_callback_cls, NULL);
+    peer_address_callback (peer_address_callback_cls, NULL, NULL, NULL, 0);
     return;
   }
   abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
 
-  msg.header.size = htons (sizeof (struct AddressLookupMessage));
+  msg.header.size = htons (sizeof (struct AddressIterateMessage));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
   msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
   peer_address_lookup_cb = GNUNET_malloc (sizeof (struct AddressLookupCtx));