fix
[oweals/gnunet.git] / src / dht / gnunet-service-dht.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file dht/gnunet-service-dht.c
23  * @brief main DHT service shell, building block for DHT implementations
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27
28 #include "platform.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_service_lib.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_signal_lib.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_datacache_lib.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_hello_lib.h"
40 #include "dht.h"
41
42 /**
43  * Handle to the datacache service (for inserting/retrieving data)
44  */
45 static struct GNUNET_DATACACHE_Handle *datacache;
46
47 /**
48  * The main scheduler to use for the DHT service
49  */
50 static struct GNUNET_SCHEDULER_Handle *sched;
51
52 /**
53  * The configuration the DHT service is running with
54  */
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
56
57 /**
58  * Handle to the core service
59  */
60 static struct GNUNET_CORE_Handle *coreAPI;
61
62 /**
63  * Handle to the transport service, for getting our hello
64  */
65 static struct GNUNET_TRANSPORT_Handle *transport_handle;
66
67 /**
68  * The identity of our peer.
69  */
70 static struct GNUNET_PeerIdentity my_identity;
71
72 /**
73  * Short id of the peer, for printing
74  */
75 static char *my_short_id;
76
77 /**
78  * Our HELLO
79  */
80 static struct GNUNET_MessageHeader *my_hello;
81
82 /**
83  * Task to run when we shut down, cleaning up all our trash
84  */
85 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
86
87
88 /**
89  * Linked list of messages to send to clients.
90  */
91 struct PendingMessage
92 {
93   /**
94    * Pointer to next item in the list
95    */
96   struct PendingMessage *next;
97
98   /**
99    * Pointer to previous item in the list
100    */
101   struct PendingMessage *prev;
102
103   /**
104    * Actual message to be sent; // avoid allocation
105    */
106   const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
107
108 };
109
110 /**
111  * Struct containing information about a client,
112  * handle to connect to it, and any pending messages
113  * that need to be sent to it.
114  */
115 struct ClientList
116 {
117   /**
118    * Linked list of active clients
119    */
120   struct ClientList *next;
121
122   /**
123    * The handle to this client
124    */
125   struct GNUNET_SERVER_Client *client_handle;
126
127   /**
128    * Handle to the current transmission request, NULL
129    * if none pending.
130    */
131   struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
132
133   /**
134    * Linked list of pending messages for this client
135    */
136   struct PendingMessage *pending_head;
137
138   /**
139    * Tail of linked list of pending messages for this client
140    */
141   struct PendingMessage *pending_tail;
142
143 };
144
145 /**
146  * Context for handling results from a get request.
147  */
148 struct DatacacheGetContext
149 {
150   /**
151    * The client to send the result to.
152    */
153   struct ClientList *client;
154
155   /**
156    * The unique id of this request
157    */
158   unsigned long long unique_id;
159 };
160
161 /**
162  * Context containing information about a DHT message received.
163  */
164 struct DHT_MessageContext
165 {
166   /**
167    * The client this request was received from.
168    */
169   struct ClientList *client;
170
171   /**
172    * The key this request was about
173    */
174   const GNUNET_HashCode *key;
175
176   /**
177    * The unique identifier of this request
178    */
179   unsigned long long unique_id;
180
181   /**
182    * Desired replication level
183    */
184   size_t replication;
185
186   /**
187    * Any message options for this request
188    */
189   size_t msg_options;
190 };
191
192 /**
193  * List of active clients.
194  */
195 static struct ClientList *client_list;
196
197 /**
198  * Forward declaration.
199  */
200 static size_t send_generic_reply (void *cls, size_t size, void *buf);
201
202
203 /**
204  * Task run to check for messages that need to be sent to a client.
205  *
206  * @param client a ClientList, containing the client and any messages to be sent to it
207  */
208 static void
209 process_pending_messages (struct ClientList *client)
210
211   if (client->pending_head == NULL) 
212     return;    
213   if (client->transmit_handle != NULL) 
214     return;
215   client->transmit_handle =
216     GNUNET_SERVER_notify_transmit_ready (client->client_handle,
217                                          ntohs (client->pending_head->msg->
218                                                 size),
219                                          GNUNET_TIME_UNIT_FOREVER_REL,
220                                          &send_generic_reply, client);
221 }
222
223 /**
224  * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
225  * request.  A ClientList is passed as closure, take the head of the list
226  * and copy it into buf, which has the result of sending the message to the
227  * client.
228  *
229  * @param cls closure to this call
230  * @param size maximum number of bytes available to send
231  * @param buf where to copy the actual message to
232  *
233  * @return the number of bytes actually copied, 0 indicates failure
234  */
235 static size_t
236 send_generic_reply (void *cls, size_t size, void *buf)
237 {
238   struct ClientList *client = cls;
239   char *cbuf = buf;
240   struct PendingMessage *reply;
241   size_t off;
242   size_t msize;
243
244   client->transmit_handle = NULL;
245   if (buf == NULL)             
246     {
247       /* client disconnected */
248       return 0;
249     }
250   off = 0;
251   while ( (NULL != (reply = client->pending_head)) &&
252           (size >= off + (msize = ntohs (reply->msg->size))))
253     {
254       GNUNET_CONTAINER_DLL_remove (client->pending_head,
255                                    client->pending_tail,
256                                    reply);
257       memcpy (&cbuf[off], reply->msg, msize);
258       GNUNET_free (reply);
259       off += msize;
260     }
261   process_pending_messages (client);
262   return off;
263 }
264
265
266 /**
267  * Add a PendingMessage to the clients list of messages to be sent
268  *
269  * @param client the active client to send the message to
270  * @param pending_message the actual message to send
271  */
272 static void
273 add_pending_message (struct ClientList *client,
274                      struct PendingMessage *pending_message)
275 {
276   GNUNET_CONTAINER_DLL_insert_after (client->pending_head,
277                                      client->pending_tail,
278                                      client->pending_tail,
279                                      pending_message);
280   process_pending_messages (client);
281 }
282
283
284 /**
285  * Called when a reply needs to be sent to a client, as
286  * a result it found to a GET or FIND PEER request.
287  *
288  * @param client the client to send the reply to
289  * @param message the encapsulated message to send
290  * @param uid the unique identifier of this request
291  */
292 static void
293 send_reply_to_client (struct ClientList *client,
294                       const struct GNUNET_MessageHeader *message,
295                       unsigned long long uid)
296 {
297   struct GNUNET_DHT_RouteResultMessage *reply;
298   struct PendingMessage *pending_message;
299   uint16_t msize;
300   size_t tsize;
301 #if DEBUG_DHT
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303               "`%s': Sending reply to client.\n", "DHT");
304 #endif
305   msize = ntohs (message->size);
306   tsize = sizeof (struct GNUNET_DHT_RouteResultMessage) + msize;
307   if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
308     {
309       GNUNET_break_op (0);
310       return;
311     }
312
313   pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
314   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
315   reply = (struct GNUNET_DHT_RouteResultMessage *)&pending_message[1];
316   reply->header.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_DHT_ROUTE_RESULT);
317   reply->header.size = htons (tsize);
318   reply->unique_id = GNUNET_htonll (uid);
319   memcpy (&reply[1], message, msize);
320
321   add_pending_message (client, pending_message);
322 }
323
324
325 /**
326  * Iterator for local get request results,
327  *
328  * @param cls closure for iterator, a DatacacheGetContext
329  * @param exp when does this value expire?
330  * @param key the key this data is stored under
331  * @param size the size of the data identified by key
332  * @param data the actual data
333  * @param type the type of the data
334  *
335  * @return GNUNET_OK to continue iteration, anything else
336  * to stop iteration.
337  */
338 static int
339 datacache_get_iterator (void *cls,
340                         struct GNUNET_TIME_Absolute exp,
341                         const GNUNET_HashCode * key,
342                         uint32_t size, const char *data, uint32_t type)
343 {
344   struct DatacacheGetContext *datacache_get_ctx = cls;
345   struct GNUNET_DHT_GetResultMessage *get_result;
346 #if DEBUG_DHT
347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348               "`%s': Received `%s' response from datacache\n", "DHT", "GET");
349 #endif
350   get_result =
351     GNUNET_malloc (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
352   get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
353   get_result->header.size =
354     htons (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
355   get_result->expiration = GNUNET_TIME_absolute_hton (exp);
356   get_result->type = htons (type);
357   memcpy (&get_result[1], data, size);
358   send_reply_to_client (datacache_get_ctx->client, &get_result->header,
359                         datacache_get_ctx->unique_id);
360   GNUNET_free (get_result);
361   return GNUNET_OK;
362 }
363
364
365 /**
366  * Server handler for initiating local dht get requests
367  *
368  * @param cls closure for service
369  * @param msg the actual get message
370  * @param message_context struct containing pertinent information about the get request
371  */
372 static void
373 handle_dht_get (void *cls, 
374                 const struct GNUNET_MessageHeader *msg,
375                 struct DHT_MessageContext *message_context)
376 {
377   const struct GNUNET_DHT_GetMessage *get_msg;
378   uint16_t get_type;
379   unsigned int results;
380   struct DatacacheGetContext datacache_get_context;
381
382   get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
383   if (ntohs (get_msg->header.size) != sizeof (struct GNUNET_DHT_GetMessage))
384     {
385       GNUNET_break (0);
386       return;
387     }
388
389   get_type = ntohs (get_msg->type);
390 #if DEBUG_DHT
391   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392               "`%s': Received `%s' request from client, message type %u, key %s, uid %llu\n",
393               "DHT", "GET", get_type, GNUNET_h2s (message_context->key),
394               message_context->unique_id);
395 #endif
396   datacache_get_context.client = message_context->client;
397   datacache_get_context.unique_id = message_context->unique_id;
398   results = 0;
399   if (datacache != NULL)
400     results =
401       GNUNET_DATACACHE_get (datacache, message_context->key, get_type,
402                             &datacache_get_iterator, &datacache_get_context);
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404               "`%s': Found %d results for local `%s' request\n", "DHT",
405               results, "GET");
406 }
407
408
409 /**
410  * Server handler for initiating local dht find peer requests
411  *
412  * @param cls closure for service
413  * @param find_msg the actual find peer message
414  * @param message_context struct containing pertinent information about the request
415  *
416  */
417 static void
418 handle_dht_find_peer (void *cls, 
419                       const struct GNUNET_MessageHeader *find_msg,
420                       struct DHT_MessageContext *message_context)
421 {
422   struct GNUNET_MessageHeader *find_peer_result;
423   size_t hello_size;
424   size_t tsize;
425
426 #if DEBUG_DHT
427   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428               "`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
429               "DHT", "FIND PEER", GNUNET_h2s (message_context->key),
430               ntohs (find_msg->size),
431               sizeof (struct GNUNET_MessageHeader));
432 #endif
433   if (my_hello == NULL)
434   {
435 #if DEBUG_DHT
436     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437                 "`%s': Our HELLO is null, can't return.\n",
438                 "DHT");
439 #endif
440     return;
441   }
442   /* Simplistic find_peer functionality, always return our hello */
443   hello_size = ntohs(my_hello->size);
444   tsize = hello_size + sizeof (struct GNUNET_MessageHeader);
445
446   if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
447     {
448       GNUNET_break_op (0);
449       return;
450     }
451   find_peer_result = GNUNET_malloc (tsize);
452   find_peer_result->type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
453   find_peer_result->size = htons (tsize);
454   memcpy (&find_peer_result[1], my_hello, hello_size);
455 #if DEBUG_DHT_HELLO
456     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457                 "`%s': Sending hello size %d to client.\n",
458                 "DHT", hello_size);
459 #endif
460   send_reply_to_client(message_context->client, find_peer_result, message_context->unique_id);
461   GNUNET_free(find_peer_result);
462 }
463
464
465 /**
466  * Server handler for initiating local dht put requests
467  *
468  * @param cls closure for service
469  * @param msg the actual put message
470  * @param message_context struct containing pertinent information about the request
471  */
472 static void
473 handle_dht_put (void *cls,
474                 const struct GNUNET_MessageHeader *msg,
475                 struct DHT_MessageContext *message_context)
476 {
477   struct GNUNET_DHT_PutMessage *put_msg;
478   size_t put_type;
479   size_t data_size;
480
481   GNUNET_assert (ntohs (msg->size) >=
482                  sizeof (struct GNUNET_DHT_PutMessage));
483   put_msg = (struct GNUNET_DHT_PutMessage *)msg;
484   put_type = ntohs (put_msg->type);
485   data_size = ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
486 #if DEBUG_DHT
487   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488               "`%s': Received `%s' request from client, message type %d, key %s\n",
489               "DHT", "PUT", put_type, GNUNET_h2s (message_context->key));
490 #endif
491   if (datacache != NULL)
492     GNUNET_DATACACHE_put (datacache, message_context->key, data_size,
493                           (char *) &put_msg[1], put_type,
494                           GNUNET_TIME_absolute_ntoh(put_msg->expiration));
495   else
496     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497                 "`%s': %s request received locally, but have no datacache!\n",
498                 "DHT", "PUT");
499 }
500
501
502 /**
503  * Find a client if it exists, add it otherwise.
504  *
505  * @param client the server handle to the client
506  *
507  * @return the client if found, a new client otherwise
508  */
509 static struct ClientList *
510 find_active_client (struct GNUNET_SERVER_Client *client)
511 {
512   struct ClientList *pos = client_list;
513   struct ClientList *ret;
514
515   while (pos != NULL)
516     {
517       if (pos->client_handle == client)
518         return pos;
519       pos = pos->next;
520     }
521
522   ret = GNUNET_malloc (sizeof (struct ClientList));
523   ret->client_handle = client;
524   ret->next = client_list;
525   client_list = ret;
526   return ret;
527 }
528
529 /**
530  * Handler for any generic DHT messages, calls the appropriate handler
531  * depending on message type, sends confirmation if responses aren't otherwise
532  * expected.
533  *
534  * @param cls closure for the service
535  * @param client the client we received this message from
536  * @param message the actual message received
537  */
538 static void
539 handle_dht_start_message (void *cls, struct GNUNET_SERVER_Client *client,
540                           const struct GNUNET_MessageHeader *message)
541 {
542   const struct GNUNET_DHT_RouteMessage *dht_msg = (const struct GNUNET_DHT_RouteMessage *) message;
543   const struct GNUNET_MessageHeader *enc_msg;
544   struct DHT_MessageContext *message_context;
545   int handle_locally;
546   size_t enc_type;
547
548   enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
549   enc_type = ntohs (enc_msg->type);
550
551
552 #if DEBUG_DHT
553   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554               "`%s': Received `%s' request from client, message type %d, key %s, uid %llu\n",
555               "DHT", "GENERIC", enc_type, GNUNET_h2s (&dht_msg->key),
556               GNUNET_ntohll (dht_msg->unique_id));
557 #endif
558
559   message_context = GNUNET_malloc (sizeof (struct DHT_MessageContext));
560   message_context->client = find_active_client (client);
561   message_context->key = &dht_msg->key;
562   message_context->unique_id = GNUNET_ntohll (dht_msg->unique_id);
563   message_context->replication = ntohl (dht_msg->desired_replication_level);
564   message_context->msg_options = ntohl (dht_msg->options);
565
566   /* TODO: Steps to be added by students */
567   /* FIXME: Implement *remote* DHT operations here (forward request) */
568   /* Implement generic route function and call here. */
569   /* FIXME: *IF* handling should be local, then do this: */
570   /* 1. find if this peer is closest based on whatever metric the DHT uses
571    * 2. if this peer is closest _OR_ the message options indicate it should
572    *    be processed everywhere _AND_ we want it processed everywhere, then
573    *    handle it locally.
574    */
575   handle_locally = GNUNET_NO;
576   if (handle_locally == GNUNET_YES)
577     {
578       switch (enc_type)
579         {
580         case GNUNET_MESSAGE_TYPE_DHT_GET:
581           handle_dht_get (cls, enc_msg,
582                           message_context);
583           break;
584         case GNUNET_MESSAGE_TYPE_DHT_PUT:
585           handle_dht_put (cls, enc_msg,
586                           message_context);
587           break;
588         case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
589           handle_dht_find_peer (cls,
590                                 enc_msg,
591                                 message_context);
592           break;
593         default:
594           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
595                       "`%s': Message type (%d) not handled\n", "DHT", enc_type);
596         }
597     }
598   else
599     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600                 "`%s': Message type (%d) not handled\n", "DHT", enc_type);
601   GNUNET_free (message_context);
602   GNUNET_SERVER_receive_done (client, GNUNET_OK);
603
604 }
605
606 /**
607  * Handler for any generic DHT stop messages, calls the appropriate handler
608  * depending on message type (if processed locally)
609  *
610  * @param cls closure for the service
611  * @param client the client we received this message from
612  * @param message the actual message received
613  *
614  * TODO: once messages are remembered by unique id, add code to
615  *       forget them here
616  */
617 static void
618 handle_dht_stop_message (void *cls, struct GNUNET_SERVER_Client *client,
619                          const struct GNUNET_MessageHeader *message)
620 {
621   const struct GNUNET_DHT_StopMessage *dht_stop_msg =
622     (const struct GNUNET_DHT_StopMessage *) message;
623   uint64_t uid;
624 #if DEBUG_DHT
625   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626               "`%s': Received `%s' request from client, uid %llu\n", "DHT",
627               "GENERIC STOP", GNUNET_ntohll (dht_stop_msg->unique_id));
628 #endif
629
630   uid = GNUNET_ntohll(dht_stop_msg->unique_id);
631   /* TODO: actually stop... free associated resources for the request
632    * lookup request by uid and remove state. */
633
634   GNUNET_SERVER_receive_done (client, GNUNET_OK);
635 }
636
637
638 /**
639  * Core handler for p2p route requests.
640  */
641 static int
642 handle_dht_p2p_route_request (void *cls,
643                               const struct GNUNET_PeerIdentity *peer,
644                               const struct GNUNET_MessageHeader *message,
645                               struct GNUNET_TIME_Relative latency, uint32_t distance)
646 {
647 #if DEBUG_DHT
648   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649               "`%s': Received `%s' request from another peer\n", "DHT",
650               "GET");
651 #endif
652   // FIXME: setup tracking for sending replies to peer (with timeout)
653   // FIXME: call code from handle_dht_start_message (refactor...)
654   return GNUNET_YES;
655 }
656
657
658 /**
659  * Core handler for p2p route results.
660  */
661 static int
662 handle_dht_p2p_route_result (void *cls,
663                              const struct GNUNET_PeerIdentity *peer,
664                              const struct GNUNET_MessageHeader *message,
665                              struct GNUNET_TIME_Relative latency, uint32_t distance)
666 {
667 #if DEBUG_DHT
668   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669               "`%s': Received `%s' request from another peer\n", "DHT",
670               "GET");
671 #endif
672   // FIXME: setup tracking for sending replies to peer
673   // FIXME: possibly call code from handle_dht_stop_message? (unique result?) (refactor...)
674   return GNUNET_YES;
675 }
676
677
678 /**
679  * Receive the HELLO from transport service,
680  * free current and replace if necessary.
681  *
682  * @param cls NULL
683  * @param message HELLO message of peer
684  */
685 static void
686 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
687 {
688 #if DEBUG_DHT
689   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690               "Received our `%s' from transport service\n",
691               "HELLO");
692 #endif
693
694   GNUNET_assert (message != NULL);
695   GNUNET_free_non_null(my_hello);
696   my_hello = GNUNET_malloc(ntohs(message->size));
697   memcpy(my_hello, message, ntohs(message->size));
698 }
699
700 /**
701  * Task run during shutdown.
702  *
703  * @param cls unused
704  * @param tc unused
705  */
706 static void
707 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
708 {
709   if (transport_handle != NULL)
710   {
711     GNUNET_free_non_null(my_hello);
712     GNUNET_TRANSPORT_get_hello_cancel(transport_handle, &process_hello, NULL);
713     GNUNET_TRANSPORT_disconnect(transport_handle);
714   }
715   if (coreAPI != NULL)
716     GNUNET_CORE_disconnect (coreAPI);
717   if (datacache != NULL)
718     GNUNET_DATACACHE_destroy (datacache);
719   if (my_short_id != NULL)
720     GNUNET_free(my_short_id);
721 }
722
723
724 /**
725  * To be called on core init/fail.
726  *
727  * @param cls service closure
728  * @param server handle to the server for this service
729  * @param identity the public identity of this peer
730  * @param publicKey the public key of this peer
731  */
732 void
733 core_init (void *cls,
734            struct GNUNET_CORE_Handle *server,
735            const struct GNUNET_PeerIdentity *identity,
736            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
737 {
738
739   if (server == NULL)
740     {
741 #if DEBUG_DHT
742   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743               "%s: Connection to core FAILED!\n", "dht",
744               GNUNET_i2s (identity));
745 #endif
746       GNUNET_SCHEDULER_cancel (sched, cleanup_task);
747       GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
748       return;
749     }
750 #if DEBUG_DHT
751   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752               "%s: Core connection initialized, I am peer: %s\n", "dht",
753               GNUNET_i2s (identity));
754 #endif
755   /* Copy our identity so we can use it */
756   memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
757   my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
758   /* Set the server to local variable */
759   coreAPI = server;
760 }
761
762
763 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
764   {&handle_dht_start_message, NULL, GNUNET_MESSAGE_TYPE_LOCAL_DHT_ROUTE, 0},
765   {&handle_dht_stop_message, NULL, GNUNET_MESSAGE_TYPE_DHT_ROUTE_STOP, 0},
766   {NULL, NULL, 0, 0}
767 };
768
769
770 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
771   {&handle_dht_p2p_route_request, GNUNET_MESSAGE_TYPE_P2P_DHT_ROUTE, 0},
772   {&handle_dht_p2p_route_result, GNUNET_MESSAGE_TYPE_P2P_DHT_ROUTE_RESULT, 0},
773   {NULL, 0, 0}
774 };
775
776 /**
777  * Method called whenever a peer connects.
778  *
779  * @param cls closure
780  * @param peer peer identity this notification is about
781  * @param latency reported latency of the connection with peer
782  * @param distance reported distance (DV) to peer
783  */
784 void handle_core_connect (void *cls,
785                           const struct GNUNET_PeerIdentity * peer,
786                           struct GNUNET_TIME_Relative latency,
787                           uint32_t distance)
788 {
789   int ret;
790   char *data;
791   size_t data_size = 42;
792   data = GNUNET_malloc (data_size);
793   memset (data, 43, data_size);
794
795 #if DEBUG_DHT
796   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797               "%s:%s Receives core connect message for peer %s distance %d!\n", my_short_id, "dht", GNUNET_i2s(peer), distance);
798 #endif
799   if (datacache != NULL)
800   {
801     ret = GNUNET_DATACACHE_put (datacache, &peer->hashPubKey, data_size,
802                                 data, 130,
803                                 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
804                                                           GNUNET_TIME_UNIT_MINUTES));
805 #if DEBUG_DHT
806     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807                 "%s Inserting data %s, type %d into datacache, return value was %d\n", my_short_id, GNUNET_h2s(&peer->hashPubKey), 130, ret);
808 #endif
809   }
810   else
811     GNUNET_free(data);
812 }
813
814 /**
815  * Process dht requests.
816  *
817  * @param cls closure
818  * @param scheduler scheduler to use
819  * @param server the initialized server
820  * @param c configuration to use
821  */
822 static void
823 run (void *cls,
824      struct GNUNET_SCHEDULER_Handle *scheduler,
825      struct GNUNET_SERVER_Handle *server,
826      const struct GNUNET_CONFIGURATION_Handle *c)
827 {
828   sched = scheduler;
829   cfg = c;
830   datacache = GNUNET_DATACACHE_create (sched, cfg, "dhtcache");
831   GNUNET_SERVER_add_handlers (server, plugin_handlers);
832   coreAPI = GNUNET_CORE_connect (sched, /* Main scheduler */
833                                  cfg,   /* Main configuration */
834                                  GNUNET_TIME_UNIT_FOREVER_REL,
835                                  NULL,  /* FIXME: anything we want to pass around? */
836                                  &core_init,    /* Call core_init once connected */
837                                  &handle_core_connect,  /* Don't care about connects */
838                                  NULL,  /* Don't care about disconnects */
839                                  NULL,  /* Don't care about peer status changes */
840                                  NULL,  /* Don't want notified about all incoming messages */
841                                  GNUNET_NO,     /* For header only inbound notification */
842                                  NULL,  /* Don't want notified about all outbound messages */
843                                  GNUNET_NO,     /* For header only outbound notification */
844                                  core_handlers);        /* Register these handlers */
845   if (coreAPI == NULL)
846     return;
847   transport_handle = GNUNET_TRANSPORT_connect(sched, cfg, NULL, NULL, NULL, NULL);
848   if (transport_handle != NULL)
849     GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
850   else
851     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to connect to transport service!\n");
852   /* Scheduled the task to clean up when shutdown is called */
853   cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
854                                                GNUNET_TIME_UNIT_FOREVER_REL,
855                                                &shutdown_task, NULL);
856 }
857
858 /**
859  * The main function for the dht service.
860  *
861  * @param argc number of arguments from the command line
862  * @param argv command line arguments
863  * @return 0 ok, 1 on error
864  */
865 int
866 main (int argc, char *const *argv)
867 {
868   return (GNUNET_OK ==
869           GNUNET_SERVICE_run (argc,
870                               argv,
871                               "dht",
872                               GNUNET_SERVICE_OPTION_NONE,
873                               &run, NULL)) ? 0 : 1;
874 }