#include "gnunet_signal_lib.h"
#include "gnunet_util_lib.h"
#include "gnunet_datacache_lib.h"
+#include "gnunet_transport_service.h"
+#include "gnunet_hello_lib.h"
#include "dht.h"
/**
*/
static struct GNUNET_CORE_Handle *coreAPI;
+/**
+ * Handle to the transport service, for getting our hello
+ */
+static struct GNUNET_TRANSPORT_Handle *transport_handle;
+
/**
* The identity of our peer.
*/
static struct GNUNET_PeerIdentity my_identity;
+/**
+ * Our HELLO
+ */
+static struct GNUNET_MessageHeader *my_hello;
+
/**
* Task to run when we shut down, cleaning up all our trash
*/
pending_message = GNUNET_malloc (sizeof (struct PendingMessage));
pending_message->msg = &reply->header;
- pending_message->next = NULL; /* We insert at the end of the list */
add_pending_message (client, pending_message);
}
handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg,
struct DHT_MessageContext *message_context)
{
+ struct GNUNET_DHT_FindPeerResultMessage *find_peer_result;
+ size_t hello_size;
+ size_t tsize;
#if DEBUG_DHT
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
GNUNET_assert (ntohs (find_msg->header.size) >=
sizeof (struct GNUNET_DHT_FindPeerMessage));
+ if (my_hello == NULL)
+ {
+#if DEBUG_DHT
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "`%s': Our HELLO is null, can't return.\n",
+ "DHT");
+#endif
+
+ return;
+ }
+
+ /* Simplistic find_peer functionality, always return our hello */
+ hello_size = ntohs(my_hello->size);
+ tsize = hello_size + sizeof (struct GNUNET_DHT_FindPeerResultMessage);
+ find_peer_result = GNUNET_malloc (tsize);
+ find_peer_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
+ find_peer_result->header.size = htons (tsize);
+ find_peer_result->data_size = htons (hello_size);
+ memcpy(&find_peer_result->peer, &my_identity, sizeof(struct GNUNET_PeerIdentity));
+ memcpy (&find_peer_result[1], &my_hello, hello_size);
+
+ send_reply_to_client(message_context->client, &find_peer_result->header, message_context->unique_id);
+
/* FIXME: Implement find peer functionality here */
}
return GNUNET_YES;
}
+
+/**
+ * Receive the HELLO from transport service,
+ * free current and replace if necessary.
+ *
+ * @param cls NULL
+ * @param message HELLO message of peer
+ */
+static void
+process_hello (void *cls, const struct GNUNET_MessageHeader *message)
+{
+#if DEBUG_DHT
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received our `%s' from transport service\n",
+ "HELLO");
+#endif
+
+ GNUNET_assert (message != NULL);
+ GNUNET_free_non_null(my_hello);
+ my_hello = GNUNET_malloc(ntohs(message->size));
+ memcpy(my_hello, message, ntohs(message->size));
+}
+
/**
* Task run during shutdown.
*
static void
shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
+ if (transport_handle != NULL)
+ {
+ GNUNET_free_non_null(my_hello);
+ GNUNET_TRANSPORT_get_hello_cancel(transport_handle, &process_hello, NULL);
+ GNUNET_TRANSPORT_disconnect(transport_handle);
+ }
GNUNET_CORE_disconnect (coreAPI);
}
+
/**
* To be called on core init/fail.
*
coreAPI = server;
}
+
/**
* Process dht requests.
*
GNUNET_NO, /* For header only outbound notification */
core_handlers); /* Register these handlers */
+ transport_handle = GNUNET_TRANSPORT_connect(sched, cfg, NULL, NULL, NULL, NULL);
+
+ if (transport_handle != NULL)
+ GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
+ else
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to connect to transport service!\n");
+
+
if (coreAPI == NULL)
return;
}
+
+/**
+ * Iterator called on each result obtained from a find peer
+ * operation
+ *
+ * @param cls closure (NULL)
+ * @param peer the peer we learned about
+ * @param reply response
+ */
+void test_find_peer_processor (void *cls,
+ const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *reply)
+{
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(peer));
+
+ GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
+ GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+}
+
+
/**
* Signature of the main function of a task.
*
peer->find_peer_handle =
GNUNET_DHT_find_peer_start (peer->dht_handle, TIMEOUT, 0, NULL, &hash,
- NULL, NULL, &test_find_peer_stop, &p1);
+ &test_find_peer_processor, NULL, NULL, NULL);
if (peer->find_peer_handle == NULL)
GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);