fixes from LRN for DEBUG code
[oweals/gnunet.git] / src / dv / gnunet-service-dv.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 dv/gnunet-service-dv.c
23  * @brief the distance vector service, primarily handles gossip of nearby
24  * peers and sending/receiving DV messages from core and decapsulating
25  * them
26  *
27  * @author Christian Grothoff
28  * @author Nathan Evans
29  *
30  * TODO: The gossip rates need to be worked out.  Probably many other things
31  * as well.
32  *
33  */
34 #include "platform.h"
35 #include "gnunet_client_lib.h"
36 #include "gnunet_getopt_lib.h"
37 #include "gnunet_os_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_service_lib.h"
40 #include "gnunet_core_service.h"
41 #include "gnunet_signal_lib.h"
42 #include "gnunet_util_lib.h"
43 #include "gnunet_hello_lib.h"
44 #include "gnunet_peerinfo_service.h"
45 #include "gnunet_crypto_lib.h"
46 #include "gnunet_statistics_service.h"
47 #include "dv.h"
48
49 /**
50  * For testing mostly, remember only the
51  * shortest path to a distant neighbor.
52  */
53 #define AT_MOST_ONE GNUNET_NO
54
55 #define USE_PEER_ID GNUNET_YES
56
57 /**
58  * How many outstanding messages (unknown sender) will we allow per peer?
59  */
60 #define MAX_OUTSTANDING_MESSAGES 5
61
62 /**
63  * How often do we check about sending out more peer information (if
64  * we are connected to no peers previously).
65  */
66 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
67
68 /**
69  * How long do we wait at most between sending out information?
70  */
71 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
72
73 /**
74  * How long can we have not heard from a peer and
75  * still have it in our tables?
76  */
77 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
78
79 /**
80  * Priority for gossip.
81  */
82 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
83
84 /**
85  * How often should we check if expiration time has elapsed for
86  * some peer?
87  */
88 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
89
90 /**
91  * How long to allow a message to be delayed?
92  */
93 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
94
95 /**
96  * Priority to use for DV data messages.
97  */
98 #define DV_PRIORITY 0
99
100 /**
101  * The cost to a direct neighbor.  We used to use 0, but 1 makes more sense.
102  */
103 #define DIRECT_NEIGHBOR_COST 1
104
105 /**
106  * The default number of direct connections to store in DV (max)
107  */
108 #define DEFAULT_DIRECT_CONNECTIONS 50
109
110 /**
111  * The default size of direct + extended peers in DV (max)
112  */
113 #define DEFAULT_DV_SIZE 100
114
115 /**
116  * The default fisheye depth, from how many hops away will
117  * we keep peers?
118  */
119 #define DEFAULT_FISHEYE_DEPTH 4
120
121 /**
122  * Linked list of messages to send to clients.
123  */
124 struct PendingMessage
125 {
126   /**
127    * Pointer to next item in the list
128    */
129   struct PendingMessage *next;
130
131   /**
132    * Pointer to previous item in the list
133    */
134   struct PendingMessage *prev;
135
136   /**
137    * The PeerIdentity to send to
138    */
139   struct GNUNET_PeerIdentity recipient;
140
141   /**
142    * The result of message sending.
143    */
144   struct GNUNET_DV_SendResultMessage *send_result;
145
146   /**
147    * Message importance level.
148    */
149   unsigned int importance;
150
151   /**
152    * Size of message.
153    */
154   unsigned int msg_size;
155
156   /**
157    * How long to wait before sending message.
158    */
159   struct GNUNET_TIME_Relative timeout;
160
161   /**
162    * Actual message to be sent; // avoid allocation
163    */
164   const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
165
166 };
167
168 struct FastGossipNeighborList
169 {
170   /**
171    * Next element of DLL
172    */
173   struct FastGossipNeighborList *next;
174
175   /**
176    * Prev element of DLL
177    */
178   struct FastGossipNeighborList *prev;
179
180   /**
181    * The neighbor to gossip about
182    */
183   struct DistantNeighbor *about;
184 };
185
186 /**
187  * Context created whenever a direct peer connects to us,
188  * used to gossip other peers to it.
189  */
190 struct NeighborSendContext
191 {
192   /**
193    * The peer we will gossip to.
194    */
195   struct DirectNeighbor *toNeighbor;
196
197   /**
198    * The task associated with this context.
199    */
200   GNUNET_SCHEDULER_TaskIdentifier task;
201
202   /**
203    * Head of DLL of peers to gossip about
204    * as fast as possible to this peer, for initial
205    * set up.
206    */
207   struct FastGossipNeighborList *fast_gossip_list_head;
208
209   /**
210    * Tail of DLL of peers to gossip about
211    * as fast as possible to this peer, for initial
212    * set up.
213    */
214   struct FastGossipNeighborList *fast_gossip_list_tail;
215
216 };
217
218
219 /**
220  * Struct to hold information for updating existing neighbors
221  */
222 struct NeighborUpdateInfo
223 {
224   /**
225    * Cost
226    */
227   unsigned int cost;
228
229   /**
230    * The existing neighbor
231    */
232   struct DistantNeighbor *neighbor;
233
234   /**
235    * The referrer of the possibly existing peer
236    */
237   struct DirectNeighbor *referrer;
238
239   /**
240    * The time we heard about this peer
241    */
242   struct GNUNET_TIME_Absolute now;
243
244   /**
245    * Peer id this peer uses to refer to neighbor.
246    */
247   unsigned int referrer_peer_id;
248
249 };
250
251 /**
252  * Struct to store a single message received with
253  * an unknown sender.
254  */
255 struct UnknownSenderMessage
256 {
257   /**
258    * Message sender (immediate)
259    */
260   struct GNUNET_PeerIdentity sender;
261
262   /**
263    * The actual message received
264    */
265   struct GNUNET_MessageHeader *message;
266
267   /**
268    * Latency of connection
269    */
270   struct GNUNET_TIME_Relative latency;
271
272   /**
273    * Distance to destination
274    */
275   uint32_t distance;
276
277   /**
278    * Unknown sender id
279    */
280   uint32_t sender_id;
281 };
282
283 /**
284  * Struct where actual neighbor information is stored,
285  * referenced by min_heap and max_heap.  Freeing dealt
286  * with when items removed from hashmap.
287  */
288 struct DirectNeighbor
289 {
290   /**
291    * Identity of neighbor.
292    */
293   struct GNUNET_PeerIdentity identity;
294
295   /**
296    * PublicKey of neighbor.
297    */
298   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
299
300   /**
301    * Head of DLL of nodes that this direct neighbor referred to us.
302    */
303   struct DistantNeighbor *referee_head;
304
305   /**
306    * Tail of DLL of nodes that this direct neighbor referred to us.
307    */
308   struct DistantNeighbor *referee_tail;
309
310   /**
311    * The sending context for gossiping peers to this neighbor.
312    */
313   struct NeighborSendContext *send_context;
314
315   /**
316    * Is this one of the direct neighbors that we are "hiding"
317    * from DV?
318    */
319   int hidden;
320
321   /**
322    * Save messages immediately from this direct neighbor from a
323    * distan peer we don't know on the chance that it will be
324    * gossiped about and we can deliver the message.
325    */
326   struct UnknownSenderMessage pending_messages[MAX_OUTSTANDING_MESSAGES];
327 };
328
329
330 /**
331  * Struct where actual neighbor information is stored,
332  * referenced by min_heap and max_heap.  Freeing dealt
333  * with when items removed from hashmap.
334  */
335 struct DistantNeighbor
336 {
337   /**
338    * We keep distant neighbor's of the same referrer in a DLL.
339    */
340   struct DistantNeighbor *next;
341
342   /**
343    * We keep distant neighbor's of the same referrer in a DLL.
344    */
345   struct DistantNeighbor *prev;
346
347   /**
348    * Node in min heap
349    */
350   struct GNUNET_CONTAINER_HeapNode *min_loc;
351
352   /**
353    * Node in max heap
354    */
355   struct GNUNET_CONTAINER_HeapNode *max_loc;
356
357   /**
358    * Identity of referrer (next hop towards 'neighbor').
359    */
360   struct DirectNeighbor *referrer;
361
362   /**
363    * Identity of neighbor.
364    */
365   struct GNUNET_PeerIdentity identity;
366
367   /**
368    * PublicKey of neighbor.
369    */
370   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
371
372   /**
373    * Last time we received routing information from this peer
374    */
375   struct GNUNET_TIME_Absolute last_activity;
376
377   /**
378    * Last time we sent routing information about this peer
379    */
380   struct GNUNET_TIME_Absolute last_gossip;
381
382   /**
383    * Cost to neighbor, used for actual distance vector computations
384    */
385   unsigned int cost;
386
387   /**
388    * Random identifier *we* use for this peer, to be used as shortcut
389    * instead of sending full peer id for each message
390    */
391   unsigned int our_id;
392
393   /**
394    * Random identifier the *referrer* uses for this peer.
395    */
396   unsigned int referrer_id;
397
398   /**
399    * Is this one of the direct neighbors that we are "hiding"
400    * from DV?
401    */
402   int hidden;
403
404 };
405
406 struct PeerIteratorContext
407 {
408   /**
409    * The actual context, to be freed later.
410    */
411   struct GNUNET_PEERINFO_IteratorContext *ic;
412
413   /**
414    * The neighbor about which we are concerned.
415    */
416   struct DirectNeighbor *neighbor;
417
418   /**
419    * The distant neighbor entry for this direct neighbor.
420    */
421   struct DistantNeighbor *distant;
422
423 };
424
425 /**
426  * Context used for creating hello messages when
427  * gossips are received.
428  */
429 struct HelloContext
430 {
431   /**
432    * Identity of distant neighbor.
433    */
434   struct GNUNET_PeerIdentity distant_peer;
435
436   /**
437    * Identity of direct neighbor, via which we send this message.
438    */
439   const struct GNUNET_PeerIdentity *direct_peer;
440
441   /**
442    * How many addresses do we need to add (always starts at 1, then set to 0)
443    */
444   int addresses_to_add;
445
446 };
447
448 struct DV_SendContext
449 {
450   /**
451    * The distant peer (should always match)
452    */
453   struct GNUNET_PeerIdentity *distant_peer;
454
455   /**
456    * The direct peer, we need to verify the referrer of.
457    */
458   struct GNUNET_PeerIdentity *direct_peer;
459
460   /**
461    * The message to be sent
462    */
463   struct GNUNET_MessageHeader *message;
464
465   /**
466    * The pre-built send result message.  Simply needs to be queued
467    * and freed once send has been called!
468    */
469   struct GNUNET_DV_SendResultMessage *send_result;
470
471   /**
472    * The size of the message being sent, may be larger
473    * than message->header.size because it's multiple
474    * messages packed into one!
475    */
476   size_t message_size;
477
478   /**
479    * How important is this message?
480    */
481   unsigned int importance;
482
483   /**
484    * Timeout for this message
485    */
486   struct GNUNET_TIME_Relative timeout;
487
488   /**
489    * Unique ID for DV message
490    */
491   unsigned int uid;
492 };
493
494 struct FindDestinationContext
495 {
496   unsigned int tid;
497   struct DistantNeighbor *dest;
498 };
499
500 struct FindIDContext
501 {
502   unsigned int tid;
503   struct GNUNET_PeerIdentity *dest;
504   const struct GNUNET_PeerIdentity *via;
505 };
506
507 struct DisconnectContext
508 {
509   /**
510    * Distant neighbor to get pid from.
511    */
512   struct DistantNeighbor *distant;
513
514   /**
515    * Direct neighbor that disconnected.
516    */
517   struct DirectNeighbor *direct;
518 };
519
520 struct TokenizedMessageContext
521 {
522   /**
523    * Immediate sender of this message
524    */
525   const struct GNUNET_PeerIdentity *peer;
526
527   /**
528    * Distant sender of the message
529    */
530   struct DistantNeighbor *distant;
531
532   /**
533    * Uid for this set of messages
534    */
535   uint32_t uid;
536 };
537
538 /**
539  * Context for finding the least cost peer to send to.
540  * Transport selection can only go so far.
541  */
542 struct FindLeastCostContext
543 {
544   struct DistantNeighbor *target;
545   unsigned int least_cost;
546 };
547
548 /**
549  * Handle to the core service api.
550  */
551 static struct GNUNET_CORE_Handle *coreAPI;
552
553 /**
554  * Stream tokenizer to handle messages coming in from core.
555  */
556 static struct GNUNET_SERVER_MessageStreamTokenizer *coreMST;
557
558 /**
559  * The identity of our peer.
560  */
561 static struct GNUNET_PeerIdentity my_identity;
562
563 /**
564  * The configuration for this service.
565  */
566 static const struct GNUNET_CONFIGURATION_Handle *cfg;
567
568 /**
569  * The scheduler for this service.
570  */
571 static struct GNUNET_SCHEDULER_Handle *sched;
572
573 /**
574  * The client, the DV plugin connected to us.  Hopefully
575  * this client will never change, although if the plugin dies
576  * and returns for some reason it may happen.
577  */
578 static struct GNUNET_SERVER_Client * client_handle;
579
580 /**
581  * Task to run when we shut down, cleaning up all our trash
582  */
583 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
584
585 static size_t default_dv_priority = 0;
586
587 static char *my_short_id;
588
589 /**
590  * Transmit handle to the plugin.
591  */
592 static struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
593
594 /**
595  * Head of DLL for client messages
596  */
597 static struct PendingMessage *plugin_pending_head;
598
599 /**
600  * Tail of DLL for client messages
601  */
602 static struct PendingMessage *plugin_pending_tail;
603
604 /**
605  * Handle to the peerinfo service
606  */
607 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
608
609 /**
610  * Transmit handle to core service.
611  */
612 static struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
613
614 /**
615  * Head of DLL for core messages
616  */
617 static struct PendingMessage *core_pending_head;
618
619 /**
620  * Tail of DLL for core messages
621  */
622 static struct PendingMessage *core_pending_tail;
623
624 /**
625  * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
626  * directly connected peers.
627  */
628 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
629
630 /**
631  * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
632  * peers connected via DV (extended neighborhood).  Does ALSO
633  * include any peers that are in 'direct_neighbors'; for those
634  * peers, the cost will be zero and the referrer all zeros.
635  */
636 static struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
637
638 /**
639  * We use the min heap (min refers to cost) to prefer
640  * gossipping about peers with small costs.
641  */
642 static struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
643
644 /**
645  * We use the max heap (max refers to cost) for general
646  * iterations over all peers and to remove the most costly
647  * connection if we have too many.
648  */
649 static struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
650
651 /**
652  * Handle for the statistics service.
653  */
654 struct GNUNET_STATISTICS_Handle *stats;
655
656 /**
657  * How far out to keep peers we learn about.
658  */
659 static unsigned long long fisheye_depth;
660
661 /**
662  * How many peers to store at most.
663  */
664 static unsigned long long max_table_size;
665
666 /**
667  * We've been given a target ID based on the random numbers that
668  * we assigned to our DV-neighborhood.  Find the entry for the
669  * respective neighbor.
670  */
671 static int
672 find_destination (void *cls,
673                   struct GNUNET_CONTAINER_HeapNode *node,
674                   void *element, GNUNET_CONTAINER_HeapCostType cost)
675 {
676   struct FindDestinationContext *fdc = cls;
677   struct DistantNeighbor *dn = element;
678
679   if (fdc->tid != dn->our_id)
680     return GNUNET_YES;
681   fdc->dest = dn;
682   return GNUNET_NO;
683 }
684
685
686 /**
687  * We've been given a target ID based on the random numbers that
688  * we assigned to our DV-neighborhood.  Find the entry for the
689  * respective neighbor.
690  */
691 static int
692 find_specific_id (void *cls,
693                   const GNUNET_HashCode *key,
694                   void *value)
695 {
696   struct FindIDContext *fdc = cls;
697   struct DistantNeighbor *dn = value;
698
699   if (memcmp(&dn->referrer->identity, fdc->via, sizeof(struct GNUNET_PeerIdentity)) == 0)
700     {
701       fdc->tid = dn->referrer_id;
702       return GNUNET_NO;
703     }
704   return GNUNET_YES;
705 }
706
707 /**
708  * Find a distant peer whose referrer_id matches what we're
709  * looking for.  For looking up a peer we've gossipped about
710  * but is now disconnected.  Need to do this because we don't
711  * want to remove those that may be accessible via a different
712  * route.
713  */
714 static int find_distant_peer (void *cls,
715                               const GNUNET_HashCode * key,
716                               void *value)
717 {
718   struct FindDestinationContext *fdc = cls;
719   struct DistantNeighbor *distant = value;
720
721   if (fdc->tid == distant->referrer_id)
722     {
723       fdc->dest = distant;
724       return GNUNET_NO;
725     }
726   return GNUNET_YES;
727 }
728
729 /**
730  * Function called to notify a client about the socket
731  * begin ready to queue more data.  "buf" will be
732  * NULL and "size" zero if the socket was closed for
733  * writing in the meantime.
734  *
735  * @param cls closure
736  * @param size number of bytes available in buf
737  * @param buf where the callee should write the message
738  * @return number of bytes written to buf
739  */
740 size_t transmit_to_plugin (void *cls,
741                            size_t size, void *buf)
742 {
743   char *cbuf = buf;
744   struct PendingMessage *reply;
745   size_t off;
746   size_t msize;
747
748   if (buf == NULL)
749     {
750       /* client disconnected */
751 #if DEBUG_DV_MESSAGES
752       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: %s buffer was NULL (client disconnect?)\n", my_short_id, "transmit_to_plugin");
753 #endif
754       return 0;
755     }
756   plugin_transmit_handle = NULL;
757   off = 0;
758   while ( (NULL != (reply = plugin_pending_head)) &&
759           (size >= off + (msize = ntohs (reply->msg->size))))
760     {
761       GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
762                                    plugin_pending_tail,
763                                    reply);
764       memcpy (&cbuf[off], reply->msg, msize);
765       GNUNET_free (reply);
766       off += msize;
767     }
768
769   if (plugin_pending_head != NULL)
770     plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
771                                                                   ntohs(plugin_pending_head->msg->size),
772                                                                   GNUNET_TIME_UNIT_FOREVER_REL,
773                                                                   &transmit_to_plugin, NULL);
774
775   return off;
776 }
777
778 /**
779  * Send a message to the dv plugin.
780  *
781  * @param sender the direct sender of the message
782  * @param message the message to send to the plugin
783  *        (may be an encapsulated type)
784  * @param message_size the size of the message to be sent
785  * @param distant_neighbor the original sender of the message
786  * @param cost the cost to the original sender of the message
787  */
788 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
789                     const struct GNUNET_MessageHeader *message,
790                     size_t message_size,
791                     struct GNUNET_PeerIdentity *distant_neighbor,
792                     size_t cost)
793 {
794   struct GNUNET_DV_MessageReceived *received_msg;
795   struct PendingMessage *pending_message;
796   char *sender_address;
797   size_t sender_address_len;
798   char *packed_msg_start;
799   int size;
800
801 #if DEBUG_DV
802   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
803 #endif
804
805   if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
806   {
807     sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
808     sender_address = GNUNET_malloc(sender_address_len);
809     memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
810     memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
811   }
812   else
813   {
814     sender_address_len = sizeof(struct GNUNET_PeerIdentity);
815     sender_address = GNUNET_malloc(sender_address_len);
816     memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
817   }
818
819   size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
820   received_msg = GNUNET_malloc(size);
821   received_msg->header.size = htons(size);
822   received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
823   received_msg->distance = htonl(cost);
824   received_msg->msg_len = htonl(message_size);
825   /* Set the sender in this message to be the original sender! */
826   memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
827   /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
828   memcpy(&received_msg[1], sender_address, sender_address_len);
829   GNUNET_free(sender_address);
830   /* Copy the actual message after the sender */
831   packed_msg_start = (char *)&received_msg[1];
832   packed_msg_start = &packed_msg_start[sender_address_len];
833   memcpy(packed_msg_start, message, message_size);
834   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
835   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
836   memcpy(&pending_message[1], received_msg, size);
837   GNUNET_free(received_msg);
838
839   GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
840
841   if (client_handle != NULL)
842     {
843       if (plugin_transmit_handle == NULL)
844         {
845           plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
846                                                                         size, GNUNET_TIME_UNIT_FOREVER_REL,
847                                                                         &transmit_to_plugin, NULL);
848         }
849     }
850   else
851     {
852       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, client_handle not yet set (how?)!\n");
853     }
854 }
855
856 /* Declare here so retry_core_send is aware of it */
857 size_t core_transmit_notify (void *cls,
858                              size_t size, void *buf);
859
860 /**
861  *  Try to send another message from our core sending list
862  */
863 static void
864 try_core_send (void *cls,
865                  const struct GNUNET_SCHEDULER_TaskContext *tc)
866 {
867   struct PendingMessage *pending;
868   pending = core_pending_head;
869
870   if (core_transmit_handle != NULL)
871     return; /* Message send already in progress */
872
873   if (pending != NULL)
874     core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, pending->importance, pending->timeout, &pending->recipient, pending->msg_size, &core_transmit_notify, NULL);
875 }
876
877 /**
878  * Function called to notify a client about the socket
879  * being ready to queue more data.  "buf" will be
880  * NULL and "size" zero if the socket was closed for
881  * writing in the meantime.
882  *
883  * @param cls closure (NULL)
884  * @param size number of bytes available in buf
885  * @param buf where the callee should write the message
886  * @return number of bytes written to buf
887  */
888 size_t core_transmit_notify (void *cls,
889                              size_t size, void *buf)
890 {
891   char *cbuf = buf;
892   struct PendingMessage *pending;
893   struct PendingMessage *client_reply;
894   size_t off;
895   size_t msize;
896
897   if (buf == NULL)
898     {
899       /* client disconnected */
900 #if DEBUG_DV
901       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
902 #endif
903       return 0;
904     }
905
906   core_transmit_handle = NULL;
907   off = 0;
908   pending = core_pending_head;
909   if ( (pending != NULL) &&
910           (size >= (msize = ntohs (pending->msg->size))))
911     {
912 #if DEBUG_DV
913       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
914 #endif
915       GNUNET_CONTAINER_DLL_remove (core_pending_head,
916                                    core_pending_tail,
917                                    pending);
918       if (pending->send_result != NULL) /* Will only be non-null if a real client asked for this send */
919         {
920           client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
921           client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
922           memcpy(&client_reply[1], pending->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
923           GNUNET_free(pending->send_result);
924
925           GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
926           if (client_handle != NULL)
927             {
928               if (plugin_transmit_handle == NULL)
929                 {
930                   plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
931                                                                                 sizeof(struct GNUNET_DV_SendResultMessage),
932                                                                                 GNUNET_TIME_UNIT_FOREVER_REL,
933                                                                                 &transmit_to_plugin, NULL);
934                 }
935               else
936                 {
937                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
938                 }
939             }
940         }
941       memcpy (&cbuf[off], pending->msg, msize);
942       GNUNET_free (pending);
943       off += msize;
944     }
945   /*reply = core_pending_head;*/
946
947   GNUNET_SCHEDULER_add_now(sched, &try_core_send, NULL);
948   /*if (reply != NULL)
949     core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL);*/
950
951   return off;
952 }
953
954
955 /**
956  * Send a DV data message via DV.
957  *
958  * @param sender the original sender of the message
959  * @param recipient the next hop recipient, may be our direct peer, maybe not
960  * @param send_context the send context
961  */
962 static int
963 send_message_via (const struct GNUNET_PeerIdentity *sender,
964                   const struct GNUNET_PeerIdentity *recipient,
965                   struct DV_SendContext *send_context)
966 {
967   p2p_dv_MESSAGE_Data *toSend;
968   unsigned int msg_size;
969   unsigned int recipient_id;
970   unsigned int sender_id;
971   struct DistantNeighbor *source;
972   struct PendingMessage *pending_message;
973   struct FindIDContext find_context;
974 #if DEBUG_DV
975   char shortname[5];
976 #endif
977
978   msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
979
980   find_context.dest = send_context->distant_peer;
981   find_context.via = recipient;
982   find_context.tid = 0;
983   GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors, &send_context->distant_peer->hashPubKey,
984                                               &find_specific_id, &find_context);
985
986   if (find_context.tid == 0)
987     {
988       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: find_specific_id failed to find peer!\n", my_short_id);
989       /* target unknown to us, drop! */
990       return GNUNET_SYSERR;
991     }
992   recipient_id = find_context.tid;
993
994   if (0 == (memcmp (&my_identity,
995                         sender, sizeof (struct GNUNET_PeerIdentity))))
996   {
997     sender_id = 0;
998     source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
999                                                     &sender->hashPubKey);
1000     if (source != NULL)
1001       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: send_message_via found %s, myself in extended peer list???\n", my_short_id, GNUNET_i2s(&source->identity));
1002   }
1003   else
1004   {
1005     source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1006                                                 &sender->hashPubKey);
1007     if (source == NULL)
1008       {
1009               /* sender unknown to us, drop! */
1010         return GNUNET_SYSERR;
1011       }
1012     sender_id = source->our_id;
1013   }
1014
1015   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1016   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1017   pending_message->send_result = send_context->send_result;
1018   memcpy(&pending_message->recipient, recipient, sizeof(struct GNUNET_PeerIdentity));
1019   pending_message->msg_size = msg_size;
1020   pending_message->importance = send_context->importance;
1021   pending_message->timeout = send_context->timeout;
1022   toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1023   toSend->header.size = htons (msg_size);
1024   toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1025   toSend->sender = htonl (sender_id);
1026   toSend->recipient = htonl (recipient_id);
1027 #if DEBUG_DV_MESSAGES
1028   toSend->uid = send_context->uid; /* Still sent around in network byte order */
1029 #else
1030   toSend->uid = htonl(0);
1031 #endif
1032
1033   memcpy (&toSend[1], send_context->message, send_context->message_size);
1034
1035 #if DEBUG_DV
1036   memcpy(&shortname, GNUNET_i2s(send_context->distant_peer), 4);
1037   shortname[4] = '\0';
1038   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send to destination `%s' via `%s' size %u\n", "DV", &shortname, GNUNET_i2s(recipient), msg_size);
1039 #endif
1040
1041   GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1042                                      core_pending_tail,
1043                                      core_pending_tail,
1044                                      pending_message);
1045
1046   GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1047
1048   return GNUNET_YES;
1049 }
1050
1051 /**
1052  * Given a FindLeastCostContext, and a set
1053  * of peers that match the target, return the cheapest.
1054  *
1055  * @param cls closure, a struct FindLeastCostContext
1056  * @param key the key identifying the target peer
1057  * @param value the target peer
1058  *
1059  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1060  */
1061 static int
1062 find_least_cost_peer (void *cls,
1063                   const GNUNET_HashCode *key,
1064                   void *value)
1065 {
1066   struct FindLeastCostContext *find_context = cls;
1067   struct DistantNeighbor *dn = value;
1068
1069   if (dn->cost < find_context->least_cost)
1070     {
1071       find_context->target = dn;
1072     }
1073   if (dn->cost == DIRECT_NEIGHBOR_COST)
1074     return GNUNET_NO;
1075   return GNUNET_YES;
1076 }
1077
1078 /**
1079  * Send a DV data message via DV.
1080  *
1081  * @param recipient the ultimate recipient of this message
1082  * @param sender the original sender of the message
1083  * @param specific_neighbor the specific neighbor to send this message via
1084  * @param message the packed message
1085  * @param message_size size of the message
1086  * @param importance what priority to send this message with
1087  * @param uid the unique identifier of this message (or 0 for none)
1088  * @param timeout how long to possibly delay sending this message
1089  */
1090 static int
1091 send_message (const struct GNUNET_PeerIdentity * recipient,
1092               const struct GNUNET_PeerIdentity * sender,
1093               const struct DistantNeighbor * specific_neighbor,
1094               const struct GNUNET_MessageHeader * message,
1095               size_t message_size,
1096               unsigned int importance,
1097               unsigned int uid,
1098               struct GNUNET_TIME_Relative timeout)
1099 {
1100   p2p_dv_MESSAGE_Data *toSend;
1101   unsigned int msg_size;
1102   unsigned int cost;
1103   unsigned int recipient_id;
1104   unsigned int sender_id;
1105   struct DistantNeighbor *target;
1106   struct DistantNeighbor *source;
1107   struct PendingMessage *pending_message;
1108   struct FindLeastCostContext find_least_ctx;
1109 #if DEBUG_DV_PEER_NUMBERS
1110   struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1111   struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1112   struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1113 #endif
1114   msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1115
1116   find_least_ctx.least_cost = -1;
1117   find_least_ctx.target = NULL;
1118   /*
1119    * Need to find the least cost peer, lest the transport selection keep
1120    * picking the same DV route for the same destination which results
1121    * in messages looping forever.  Relatively cheap, we don't iterate
1122    * over all known peers, just those that apply.
1123    */
1124   GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1125                                                        &recipient->hashPubKey,  &find_least_cost_peer, &find_least_ctx);
1126   target = find_least_ctx.target;
1127
1128   if (target == NULL)
1129     {
1130       /* target unknown to us, drop! */
1131       return GNUNET_SYSERR;
1132     }
1133   recipient_id = target->referrer_id;
1134
1135   source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1136                                               &sender->hashPubKey);
1137   if (source == NULL)
1138     {
1139       if (0 != (memcmp (&my_identity,
1140                         sender, sizeof (struct GNUNET_PeerIdentity))))
1141         {
1142           /* sender unknown to us, drop! */
1143           return GNUNET_SYSERR;
1144         }
1145       sender_id = 0;            /* 0 == us */
1146     }
1147   else
1148     {
1149       /* find out the number that we use when we gossip about
1150          the sender */
1151       sender_id = source->our_id;
1152     }
1153
1154 #if DEBUG_DV_PEER_NUMBERS
1155   GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1156   GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey, &encPeerVia);
1157   encPeerFrom.encoding[4] = '\0';
1158   encPeerVia.encoding[4] = '\0';
1159 #endif
1160   if ((sender_id != 0) && (0 == memcmp(&source->identity, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity))))
1161     {
1162       return 0;
1163     }
1164
1165   cost = target->cost;
1166   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1167   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1168   pending_message->send_result = NULL;
1169   pending_message->importance = importance;
1170   pending_message->timeout = timeout;
1171   memcpy(&pending_message->recipient, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity));
1172   pending_message->msg_size = msg_size;
1173   toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1174   toSend->header.size = htons (msg_size);
1175   toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1176   toSend->sender = htonl (sender_id);
1177   toSend->recipient = htonl (recipient_id);
1178 #if DEBUG_DV_MESSAGES
1179   toSend->uid = htonl(uid);
1180 #else
1181   toSend->uid = htonl(0);
1182 #endif
1183
1184 #if DEBUG_DV_PEER_NUMBERS
1185   GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1186   encPeerTo.encoding[4] = '\0';
1187   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Sending DATA message. Sender id %u, source %s, destination %s, via %s\n", GNUNET_i2s(&my_identity), sender_id, &encPeerFrom, &encPeerTo, &encPeerVia);
1188 #endif
1189   memcpy (&toSend[1], message, message_size);
1190   if ((source != NULL) && (source->pkey == NULL)) /* Test our hypothesis about message failures! */
1191     {
1192       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: Sending message, but anticipate recipient will not know sender!!!\n\n\n", my_short_id);
1193     }
1194   GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1195                                      core_pending_tail,
1196                                      core_pending_tail,
1197                                      pending_message);
1198 #if DEBUG_DV
1199   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send size %d to destination `%s'\n", "DV SEND MESSAGE", msg_size, GNUNET_i2s(recipient));
1200 #endif
1201
1202   GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1203   return (int) cost;
1204 }
1205
1206 #if USE_PEER_ID
1207 struct CheckPeerContext
1208 {
1209   /**
1210    * Peer we found
1211    */
1212   struct DistantNeighbor *peer;
1213
1214   /**
1215    * Sender id to search for
1216    */
1217   unsigned int sender_id;
1218 };
1219
1220 /**
1221  * Iterator over hash map entries.
1222  *
1223  * @param cls closure
1224  * @param key current key code
1225  * @param value value in the hash map
1226  * @return GNUNET_YES if we should continue to
1227  *         iterate,
1228  *         GNUNET_NO if not.
1229  */
1230 int checkPeerID (void *cls,
1231                  const GNUNET_HashCode * key,
1232                  void *value)
1233 {
1234   struct CheckPeerContext *ctx = cls;
1235   struct DistantNeighbor *distant = value;
1236
1237   if (memcmp(key, &ctx->sender_id, sizeof(unsigned int)) == 0)
1238   {
1239     ctx->peer = distant;
1240     return GNUNET_NO;
1241   }
1242   return GNUNET_YES;
1243
1244 }
1245 #endif
1246
1247
1248 /**
1249  * Handler for messages parsed out by the tokenizer from
1250  * DV DATA received for this peer.
1251  *
1252  * @param cls NULL
1253  * @param client the TokenizedMessageContext which contains message information
1254  * @param message the actual message
1255  */
1256 void tokenized_message_handler (void *cls,
1257                                 void *client,
1258                                 const struct GNUNET_MessageHeader *message)
1259 {
1260   struct TokenizedMessageContext *ctx = client;
1261   GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1262   GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1263   if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1264       (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1265   {
1266 #if DEBUG_DV_MESSAGES
1267     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268                 "%s: Receives %s message for me, uid %u, size %d, type %d cost %u from %s!\n", my_short_id, "DV DATA", ctx->uid, ntohs(message->size), ntohs(message->type), ctx->distant->cost, GNUNET_i2s(&ctx->distant->identity));
1269 #endif
1270     GNUNET_assert(memcmp(ctx->peer, &ctx->distant->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1271     send_to_plugin(ctx->peer, message, ntohs(message->size), &ctx->distant->identity, ctx->distant->cost);
1272   }
1273 }
1274
1275 #if DELAY_FORWARDS
1276 struct DelayedMessageContext
1277 {
1278   struct GNUNET_PeerIdentity dest;
1279   struct GNUNET_PeerIdentity sender;
1280   struct GNUNET_MessageHeader *message;
1281   size_t message_size;
1282   uint32_t uid;
1283 };
1284
1285 void send_message_delayed (void *cls,
1286                            const struct GNUNET_SCHEDULER_TaskContext *tc)
1287 {
1288   struct DelayedMessageContext *msg_ctx = cls;
1289   if (msg_ctx != NULL)
1290     {
1291       send_message(&msg_ctx->dest,
1292                    &msg_ctx->sender,
1293                    NULL,
1294                    msg_ctx->message,
1295                    msg_ctx->message_size,
1296                    default_dv_priority,
1297                    msg_ctx->uid,
1298                    GNUNET_TIME_relative_get_forever());
1299       GNUNET_free(msg_ctx->message);
1300       GNUNET_free(msg_ctx);
1301     }
1302 }
1303 #endif
1304
1305
1306 /**
1307  * Core handler for dv data messages.  Whatever this message
1308  * contains all we really have to do is rip it out of its
1309  * DV layering and give it to our pal the DV plugin to report
1310  * in with.
1311  *
1312  * @param cls closure
1313  * @param peer peer which sent the message (immediate sender)
1314  * @param message the message
1315  * @param latency the latency of the connection we received the message from
1316  * @param distance the distance to the immediate peer
1317  */
1318 static int handle_dv_data_message (void *cls,
1319                                    const struct GNUNET_PeerIdentity * peer,
1320                                    const struct GNUNET_MessageHeader * message,
1321                                    struct GNUNET_TIME_Relative latency,
1322                                    uint32_t distance)
1323 {
1324   const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1325   const struct GNUNET_MessageHeader *packed_message;
1326   struct DirectNeighbor *dn;
1327   struct DistantNeighbor *pos;
1328   unsigned int sid;             /* Sender id */
1329   unsigned int tid;             /* Target id */
1330   struct GNUNET_PeerIdentity *original_sender;
1331   struct GNUNET_PeerIdentity *destination;
1332   struct FindDestinationContext fdc;
1333   struct TokenizedMessageContext tkm_ctx;
1334   int i;
1335   int found_pos;
1336 #if DELAY_FORWARDS
1337   struct DelayedMessageContext *delayed_context;
1338 #endif
1339 #if USE_PEER_ID
1340   struct CheckPeerContext checkPeerCtx;
1341 #endif
1342 #if DEBUG_DV_MESSAGES
1343   char *sender_id;
1344 #endif
1345   int ret;
1346   size_t packed_message_size;
1347   char *cbuf;
1348
1349   packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1350 #if DEBUG_DV
1351   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352               "%s: Receives DATA message from %s size %d, packed size %d!\n", my_short_id, GNUNET_i2s(peer) , ntohs(incoming->header.size), packed_message_size);
1353 #endif
1354
1355   if (ntohs (incoming->header.size) <  sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1356     {
1357
1358 #if DEBUG_DV
1359     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360                 "`%s': Message sizes don't add up, total size %u, expected at least %u!\n", "dv service", ntohs(incoming->header.size), sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader));
1361 #endif
1362       return GNUNET_SYSERR;
1363     }
1364
1365   dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1366                                           &peer->hashPubKey);
1367   if (dn == NULL)
1368     return GNUNET_OK;
1369
1370   sid = ntohl (incoming->sender);
1371 #if USE_PEER_ID
1372   if (sid != 0)
1373   {
1374     checkPeerCtx.sender_id = sid;
1375     checkPeerCtx.peer = NULL;
1376     GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &checkPeerID, &checkPeerCtx);
1377     pos = checkPeerCtx.peer;
1378   }
1379   else
1380   {
1381     pos = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1382                                              &peer->hashPubKey);
1383   }
1384 #else
1385   pos = dn->referee_head;
1386   while ((NULL != pos) && (pos->referrer_id != sid))
1387     pos = pos->next;
1388 #endif
1389
1390   if (pos == NULL)
1391     {
1392 #if DEBUG_DV_MESSAGES
1393       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394                   "%s: unknown sender (%u), Message uid %u from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1395       pos = dn->referee_head;
1396       while ((NULL != pos) && (pos->referrer_id != sid))
1397       {
1398         sender_id = strdup(GNUNET_i2s(&pos->identity));
1399         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n", pos->referrer_id, sender_id);
1400         GNUNET_free(sender_id);
1401         pos = pos->next;
1402       }
1403 #endif
1404       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1405                   "%s: unknown sender (%u), Message uid %u from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1406
1407       found_pos = -1;
1408       for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
1409         {
1410           if (dn->pending_messages[i].sender_id == 0)
1411             {
1412               found_pos = i;
1413               break;
1414             }
1415         }
1416
1417       if (found_pos == -1)
1418         {
1419           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1420                       "%s: Too many unknown senders (%u), ignoring message! Message uid %llu from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1421         }
1422       else
1423         {
1424             dn->pending_messages[found_pos].message = GNUNET_malloc(ntohs (message->size));
1425             memcpy(dn->pending_messages[found_pos].message, message, ntohs(message->size));
1426             dn->pending_messages[found_pos].distance = distance;
1427             dn->pending_messages[found_pos].latency = latency;
1428             memcpy(&dn->pending_messages[found_pos].sender, peer, sizeof(struct GNUNET_PeerIdentity));
1429             dn->pending_messages[found_pos].sender_id = sid;
1430         }
1431       /* unknown sender */
1432       return GNUNET_OK;
1433     }
1434   original_sender = &pos->identity;
1435   tid = ntohl (incoming->recipient);
1436   if (tid == 0)
1437     {
1438       /* 0 == us */
1439       cbuf = (char *)&incoming[1];
1440
1441       tkm_ctx.peer = peer;
1442       tkm_ctx.distant = pos;
1443       tkm_ctx.uid = ntohl(incoming->uid);
1444       if (GNUNET_OK != GNUNET_SERVER_mst_receive (coreMST,
1445                                                   &tkm_ctx,
1446                                                   cbuf,
1447                                                   packed_message_size,
1448                                                   GNUNET_NO,
1449                                                   GNUNET_NO))
1450         {
1451           GNUNET_break_op(0);
1452           GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: %s Received corrupt data, discarding!", my_short_id, "DV SERVICE");
1453         }
1454       return GNUNET_OK;
1455     }
1456   else
1457     {
1458       packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1459     }
1460
1461   /* FIXME: this is the *only* per-request operation we have in DV
1462      that is O(n) in relation to the number of connected peers; a
1463      hash-table lookup could easily solve this (minor performance
1464      issue) */
1465   fdc.tid = tid;
1466   fdc.dest = NULL;
1467   GNUNET_CONTAINER_heap_iterate (neighbor_max_heap,
1468                                  &find_destination, &fdc);
1469
1470 #if DEBUG_DV
1471       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472                   "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1473 #endif
1474
1475   if (fdc.dest == NULL)
1476     {
1477 #if DEBUG_DV_MESSAGES
1478       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479                   "%s: Receives %s message uid %u for someone we don't know (id %u)!\n", my_short_id, "DV DATA", ntohl(incoming->uid), tid);
1480 #endif
1481       return GNUNET_OK;
1482     }
1483   destination = &fdc.dest->identity;
1484
1485   if (0 == memcmp (destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1486     {
1487       /* FIXME: create stat: routing loop-discard! */
1488 #if DEBUG_DV_PEER_NUMBERS
1489       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1490 #endif
1491
1492 #if DEBUG_DV_MESSAGES
1493       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494                   "%s: DROPPING MESSAGE uid %u type %d, routing loop! Message immediately from %s!\n", my_short_id, ntohl(incoming->uid), ntohs(packed_message->type), GNUNET_i2s(&dn->identity));
1495 #endif
1496       return GNUNET_OK;
1497     }
1498
1499   /* At this point we have a message, and we need to forward it on to the
1500    * next DV hop.
1501    */
1502 #if DEBUG_DV_MESSAGES
1503   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504               "%s: FORWARD %s message for %s, uid %u, size %d type %d, cost %u!\n", my_short_id, "DV DATA", GNUNET_i2s(destination), ntohl(incoming->uid), ntohs(packed_message->size), ntohs(packed_message->type), pos->cost);
1505 #endif
1506
1507 #if DELAY_FORWARDS
1508   if (GNUNET_TIME_absolute_get_duration(pos->last_gossip).abs_value < GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2).abs_value)
1509     {
1510       delayed_context = GNUNET_malloc(sizeof(struct DelayedMessageContext));
1511       memcpy(&delayed_context->dest, destination, sizeof(struct GNUNET_PeerIdentity));
1512       memcpy(&delayed_context->sender, original_sender, sizeof(struct GNUNET_PeerIdentity));
1513       delayed_context->message = GNUNET_malloc(packed_message_size);
1514       memcpy(delayed_context->message, packed_message, packed_message_size);
1515       delayed_context->message_size = packed_message_size;
1516       delayed_context->uid = ntohl(incoming->uid);
1517       GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 2500), &send_message_delayed, delayed_context);
1518       return GNUNET_OK;
1519     }
1520   else
1521 #endif
1522     {
1523       ret = send_message(destination,
1524                          original_sender,
1525                          NULL,
1526                          packed_message,
1527                          packed_message_size,
1528                          default_dv_priority,
1529                          ntohl(incoming->uid),
1530                          GNUNET_TIME_relative_get_forever());
1531     }
1532   if (ret != GNUNET_SYSERR)
1533     return GNUNET_OK;
1534   else
1535     {
1536 #if DEBUG_MESSAGE_DROP
1537       char *direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1538       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539                   "%s: DROPPING MESSAGE type %d, forwarding failed! Message immediately from %s!\n", GNUNET_i2s(&my_identity), ntohs(((struct GNUNET_MessageHeader *)&incoming[1])->type), direct_id);
1540       GNUNET_free (direct_id);
1541 #endif
1542       return GNUNET_SYSERR;
1543     }
1544 }
1545
1546 #if DEBUG_DV
1547 /**
1548  * Iterator over hash map entries.
1549  *
1550  * @param cls closure (NULL)
1551  * @param key current key code
1552  * @param value value in the hash map (DistantNeighbor)
1553  * @return GNUNET_YES if we should continue to
1554  *         iterate,
1555  *         GNUNET_NO if not.
1556  */
1557 int print_neighbors (void *cls,
1558                      const GNUNET_HashCode * key,
1559                      void *abs_value)
1560 {
1561   struct DistantNeighbor *distant_neighbor = abs_value;
1562   char my_shortname[5];
1563   char referrer_shortname[5];
1564   memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1565   my_shortname[4] = '\0';
1566   memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1567   referrer_shortname[4] = '\0';
1568
1569   GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`%s' %s: Peer `%s', distance %d, referrer `%s' pkey: %s\n", &my_shortname, "DV", GNUNET_i2s(&distant_neighbor->identity), distant_neighbor->cost, &referrer_shortname, distant_neighbor->pkey == NULL ? "no" : "yes");
1570   return GNUNET_YES;
1571 }
1572 #endif
1573
1574 /**
1575  *  Scheduled task which gossips about known direct peers to other connected
1576  *  peers.  Will run until called with reason shutdown.
1577  */
1578 static void
1579 neighbor_send_task (void *cls,
1580                     const struct GNUNET_SCHEDULER_TaskContext *tc)
1581 {
1582   struct NeighborSendContext *send_context = cls;
1583 #if DEBUG_DV_GOSSIP_SEND
1584   char * encPeerAbout;
1585   char * encPeerTo;
1586 #endif
1587   struct DistantNeighbor *about;
1588   struct DirectNeighbor *to;
1589   struct FastGossipNeighborList *about_list;
1590
1591   p2p_dv_MESSAGE_NeighborInfo *message;
1592   struct PendingMessage *pending_message;
1593
1594   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1595   {
1596 #if DEBUG_DV_GOSSIP
1597   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1598               "%s: Called with reason shutdown, shutting down!\n",
1599               GNUNET_i2s(&my_identity));
1600 #endif
1601     return;
1602   }
1603
1604   if (send_context->fast_gossip_list_head != NULL)
1605     {
1606       about_list = send_context->fast_gossip_list_head;
1607       about = about_list->about;
1608       GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1609                                   send_context->fast_gossip_list_tail,
1610                                   about_list);
1611       GNUNET_free(about_list);
1612     }
1613   else
1614     {
1615       /* FIXME: this may become a problem, because the heap walk has only one internal "walker".  This means
1616        * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1617        * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1618        * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1619        * part of the walk_get_next call.  Then the heap would have to keep a list of walks, or reset the walk
1620        * whenever a modification has been detected.  Yuck either way.  Perhaps we could iterate over the heap
1621        * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1622        * in the mean time that becomes nasty.  For now we'll just assume that the walking is done
1623        * asynchronously enough to avoid major problems (-;
1624        *
1625        * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1626        */
1627       about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1628     }
1629   to = send_context->toNeighbor;
1630
1631   if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1632 #if SUPPORT_HIDING
1633       (about->hidden == GNUNET_NO) &&
1634 #endif
1635       (to != NULL) &&
1636       (0 != memcmp (&about->identity,
1637                         &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1638       (about->pkey != NULL))
1639     {
1640 #if DEBUG_DV_GOSSIP_SEND
1641       encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1642       encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1643       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644                   "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1645                   GNUNET_i2s(&my_identity),
1646                   encPeerAbout, about->our_id, encPeerTo);
1647       GNUNET_free(encPeerAbout);
1648       GNUNET_free(encPeerTo);
1649 #endif
1650       about->last_gossip = GNUNET_TIME_absolute_get();
1651       pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1652       pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1653       pending_message->importance = default_dv_priority;
1654       pending_message->timeout = GNUNET_TIME_relative_get_forever();
1655       memcpy(&pending_message->recipient, &to->identity, sizeof(struct GNUNET_PeerIdentity));
1656       pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo);
1657       message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1658       message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1659       message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1660       message->cost = htonl (about->cost);
1661       message->neighbor_id = htonl (about->our_id);
1662
1663       memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1664       memcpy (&message->neighbor,
1665               &about->identity, sizeof (struct GNUNET_PeerIdentity));
1666
1667       GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1668                                          core_pending_tail,
1669                                          core_pending_tail,
1670                                          pending_message);
1671
1672       GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1673       /*if (core_transmit_handle == NULL)
1674         core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, default_dv_priority, GNUNET_TIME_relative_get_forever(), &to->identity, sizeof(p2p_dv_MESSAGE_NeighborInfo), &core_transmit_notify, NULL);*/
1675
1676     }
1677
1678   if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1679     {
1680 #if DEBUG_DV_PEER_NUMBERS
1681       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1682 #endif
1683       send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1684     }
1685   else
1686     {
1687 #if DEBUG_DV_PEER_NUMBERS
1688       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1689 #endif
1690       send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1691     }
1692
1693   return;
1694 }
1695
1696
1697 /**
1698  * Handle START-message.  This is the first message sent to us
1699  * by the client (can only be one!).
1700  *
1701  * @param cls closure (always NULL)
1702  * @param client identification of the client
1703  * @param message the actual message
1704  */
1705 static void
1706 handle_start (void *cls,
1707               struct GNUNET_SERVER_Client *client,
1708               const struct GNUNET_MessageHeader *message)
1709 {
1710
1711 #if DEBUG_DV
1712   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1713               "Received `%s' request from client\n", "START");
1714 #endif
1715
1716   client_handle = client;
1717
1718   GNUNET_SERVER_client_keep(client_handle);
1719   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1720 }
1721
1722 #if UNSIMPLER
1723 /**
1724  * Iterate over hash map entries for a distant neighbor,
1725  * if direct neighbor matches context call send message
1726  *
1727  * @param cls closure, a DV_SendContext
1728  * @param key current key code
1729  * @param value value in the hash map
1730  * @return GNUNET_YES if we should continue to
1731  *         iterate,
1732  *         GNUNET_NO if not.
1733  */
1734 int send_iterator (void *cls,
1735                    const GNUNET_HashCode * key,
1736                    void *abs_value)
1737 {
1738   struct DV_SendContext *send_context = cls;
1739   struct DistantNeighbor *distant_neighbor = abs_value;
1740
1741   if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1742     {
1743       send_message_via(&my_identity, distant_neighbor, send_context);
1744       return GNUNET_NO;
1745     }
1746   return GNUNET_YES;
1747 }
1748 #endif
1749
1750 /**
1751  * Service server's handler for message send requests (which come
1752  * bubbling up to us through the DV plugin).
1753  *
1754  * @param cls closure
1755  * @param client identification of the client
1756  * @param message the actual message
1757  */
1758 void handle_dv_send_message (void *cls,
1759                              struct GNUNET_SERVER_Client * client,
1760                              const struct GNUNET_MessageHeader * message)
1761 {
1762   struct GNUNET_DV_SendMessage *send_msg;
1763   struct GNUNET_DV_SendResultMessage *send_result_msg;
1764   struct PendingMessage *pending_message;
1765   size_t address_len;
1766   size_t message_size;
1767   struct GNUNET_PeerIdentity *destination;
1768   struct GNUNET_PeerIdentity *direct;
1769   struct GNUNET_MessageHeader *message_buf;
1770   char *temp_pos;
1771   int offset;
1772   static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1773   struct DV_SendContext *send_context;
1774 #if DEBUG_DV_MESSAGES
1775   char *cbuf;
1776   struct GNUNET_MessageHeader *packed_message;
1777 #endif
1778
1779   if (client_handle == NULL)
1780   {
1781     client_handle = client;
1782     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1783               "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1784   }
1785   else if (client_handle != client)
1786   {
1787     client_handle = client;
1788     /* What should we do in this case, assert fail or just log the warning? */
1789 #if DEBUG_DV
1790     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791                 "%s: Setting client handle (was a different client!)!\n", "dv");
1792 #endif
1793   }
1794
1795   GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1796   send_msg = (struct GNUNET_DV_SendMessage *)message;
1797
1798   address_len = ntohl(send_msg->addrlen);
1799   GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1800   message_size = ntohs(message->size) - sizeof(struct GNUNET_DV_SendMessage) - address_len;
1801   destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1802   direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1803   message_buf = GNUNET_malloc(message_size);
1804
1805   temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1806   offset = 0; /* Offset starts at zero */
1807
1808   memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1809   offset += sizeof(struct GNUNET_PeerIdentity);
1810
1811   memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1812   offset += sizeof(struct GNUNET_PeerIdentity);
1813
1814
1815   memcpy(message_buf, &temp_pos[offset], message_size);
1816   if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1817     {
1818       GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1819       dest_hash.encoding[4] = '\0';
1820       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: asked to send message to `%s', but address is for `%s'!", "DV SERVICE", GNUNET_i2s(&send_msg->target), (const char *)&dest_hash.encoding);
1821     }
1822
1823 #if DEBUG_DV_MESSAGES
1824   cbuf = (char *)message_buf;
1825   offset = 0;
1826   while(offset < message_size)
1827     {
1828       packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1829       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: DV PLUGIN SEND uid %u type %d to %s\n", my_short_id, ntohl(send_msg->uid), ntohs(packed_message->type), GNUNET_i2s(destination));
1830       offset += ntohs(packed_message->size);
1831     }
1832   /*GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: DV PLUGIN SEND uid %u type %d to %s\n", my_short_id, ntohl(send_msg->uid), ntohs(message_buf->type), GNUNET_i2s(destination));*/
1833 #endif
1834   GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1835   dest_hash.encoding[4] = '\0';
1836   send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1837
1838   send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1839   send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1840   send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1841   send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1842
1843   send_context->importance = ntohl(send_msg->priority);
1844   send_context->timeout = send_msg->timeout;
1845   send_context->direct_peer = direct;
1846   send_context->distant_peer = destination;
1847   send_context->message = message_buf;
1848   send_context->message_size = message_size;
1849   send_context->send_result = send_result_msg;
1850 #if DEBUG_DV_MESSAGES
1851   send_context->uid = send_msg->uid;
1852 #endif
1853
1854   if (send_message_via(&my_identity, direct, send_context) != GNUNET_YES)
1855     {
1856       send_result_msg->result = htons(1);
1857       pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1858       pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1859       memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1860       GNUNET_free(send_result_msg);
1861
1862       GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1863
1864       if (client_handle != NULL)
1865         {
1866           if (plugin_transmit_handle == NULL)
1867             {
1868               plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1869                                                                             sizeof(struct GNUNET_DV_SendResultMessage),
1870                                                                             GNUNET_TIME_UNIT_FOREVER_REL,
1871                                                                             &transmit_to_plugin, NULL);
1872             }
1873           else
1874             {
1875               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1876             }
1877         }
1878       GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1879       dest_hash.encoding[4] = '\0';
1880       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s DV SEND failed to send message to destination `%s' via `%s'\n", my_short_id, (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1881     }
1882
1883   /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1884 #if UNSIMPLER
1885   if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1886     {
1887       send_result_msg->result = htons(1);
1888       pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1889       pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1890       memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1891       GNUNET_free(send_result_msg);
1892
1893       GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1894
1895       if (client_handle != NULL)
1896         {
1897           if (plugin_transmit_handle == NULL)
1898             {
1899               plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1900                                                                             sizeof(struct GNUNET_DV_SendResultMessage),
1901                                                                             GNUNET_TIME_UNIT_FOREVER_REL,
1902                                                                             &transmit_to_plugin, NULL);
1903             }
1904           else
1905             {
1906               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1907             }
1908         }
1909       GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1910       dest_hash.encoding[4] = '\0';
1911       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s DV SEND failed to send message to destination `%s' via `%s'\n", my_short_id, (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1912     }
1913 #endif
1914   GNUNET_free(message_buf);
1915   GNUNET_free(send_context);
1916   GNUNET_free(direct);
1917   GNUNET_free(destination);
1918
1919   GNUNET_SERVER_receive_done(client, GNUNET_OK);
1920 }
1921
1922 /** Forward declarations **/
1923 static int handle_dv_gossip_message (void *cls,
1924                                      const struct GNUNET_PeerIdentity *peer,
1925                                      const struct GNUNET_MessageHeader *message,
1926                                      struct GNUNET_TIME_Relative latency,
1927                                      uint32_t distance);
1928
1929 static int handle_dv_disconnect_message (void *cls,
1930                                          const struct GNUNET_PeerIdentity *peer,
1931                                          const struct GNUNET_MessageHeader *message,
1932                                          struct GNUNET_TIME_Relative latency,
1933                                          uint32_t distance);
1934 /** End forward declarations **/
1935
1936
1937 /**
1938  * List of handlers for the messages understood by this
1939  * service.
1940  *
1941  * Hmm... will we need to register some handlers with core and
1942  * some handlers with our server here?  Because core should be
1943  * getting the incoming DV messages (from whichever lower level
1944  * transport) and then our server should be getting messages
1945  * from the dv_plugin, right?
1946  */
1947 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1948   {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1949   {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1950   {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1951   {NULL, 0, 0}
1952 };
1953
1954 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1955   {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1956   {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1957   {NULL, NULL, 0, 0}
1958 };
1959
1960 /**
1961  * Free a DistantNeighbor node, including removing it
1962  * from the referer's list.
1963  */
1964 static void
1965 distant_neighbor_free (struct DistantNeighbor *referee)
1966 {
1967   struct DirectNeighbor *referrer;
1968
1969   referrer = referee->referrer;
1970   if (referrer != NULL)
1971     {
1972       GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1973                          referrer->referee_tail, referee);
1974     }
1975   GNUNET_CONTAINER_heap_remove_node (neighbor_max_heap, referee->max_loc);
1976   GNUNET_CONTAINER_heap_remove_node (neighbor_min_heap, referee->min_loc);
1977   GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
1978                                     &referee->identity.hashPubKey);
1979   GNUNET_free_non_null (referee->pkey);
1980   GNUNET_free (referee);
1981 }
1982
1983 /**
1984  * Free a DirectNeighbor node, including removing it
1985  * from the referer's list.
1986  */
1987 static void
1988 direct_neighbor_free (struct DirectNeighbor *direct)
1989 {
1990   struct NeighborSendContext *send_context;
1991   struct FastGossipNeighborList *about_list;
1992   struct FastGossipNeighborList *prev_about;
1993
1994   send_context = direct->send_context;
1995
1996   if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1997     GNUNET_SCHEDULER_cancel(sched, send_context->task);
1998
1999   about_list = send_context->fast_gossip_list_head;
2000   while (about_list != NULL)
2001     {
2002       GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
2003       prev_about = about_list;
2004       about_list = about_list->next;
2005       GNUNET_free(prev_about);
2006     }
2007   GNUNET_free(send_context);
2008   GNUNET_free(direct);
2009 }
2010
2011 /**
2012  * Multihashmap iterator for sending out disconnect messages
2013  * for a peer.
2014  *
2015  * @param cls the peer that was disconnected
2016  * @param key key value stored under
2017  * @param value the direct neighbor to send disconnect to
2018  *
2019  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2020  */
2021 static int schedule_disconnect_messages (void *cls,
2022                                     const GNUNET_HashCode * key,
2023                                     void *value)
2024 {
2025   struct DisconnectContext *disconnect_context = cls;
2026   struct DirectNeighbor *disconnected = disconnect_context->direct;
2027   struct DirectNeighbor *notify = value;
2028   struct PendingMessage *pending_message;
2029   p2p_dv_MESSAGE_Disconnect *disconnect_message;
2030
2031   if (memcmp(&notify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2032     return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
2033
2034   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
2035   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
2036   pending_message->importance = default_dv_priority;
2037   pending_message->timeout = GNUNET_TIME_relative_get_forever();
2038   memcpy(&pending_message->recipient, &notify->identity, sizeof(struct GNUNET_PeerIdentity));
2039   pending_message->msg_size = sizeof(p2p_dv_MESSAGE_Disconnect);
2040   disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
2041   disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
2042   disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
2043   disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
2044
2045   GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
2046                                      core_pending_tail,
2047                                      core_pending_tail,
2048                                      pending_message);
2049
2050   GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
2051   /*if (core_transmit_handle == NULL)
2052     core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, default_dv_priority, GNUNET_TIME_relative_get_forever(), &notify->identity, sizeof(p2p_dv_MESSAGE_Disconnect), &core_transmit_notify, NULL);*/
2053
2054   return GNUNET_YES;
2055 }
2056
2057 /**
2058  * Multihashmap iterator for freeing extended neighbors.
2059  *
2060  * @param cls NULL
2061  * @param key key value stored under
2062  * @param value the distant neighbor to be freed
2063  *
2064  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2065  */
2066 static int free_extended_neighbors (void *cls,
2067                                     const GNUNET_HashCode * key,
2068                                     void *value)
2069 {
2070   struct DistantNeighbor *distant = value;
2071   distant_neighbor_free(distant);
2072   return GNUNET_YES;
2073 }
2074
2075 /**
2076  * Multihashmap iterator for freeing direct neighbors.
2077  *
2078  * @param cls NULL
2079  * @param key key value stored under
2080  * @param value the direct neighbor to be freed
2081  *
2082  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2083  */
2084 static int free_direct_neighbors (void *cls,
2085                                     const GNUNET_HashCode * key,
2086                                     void *value)
2087 {
2088   struct DirectNeighbor *direct = value;
2089   direct_neighbor_free(direct);
2090   return GNUNET_YES;
2091 }
2092
2093
2094 /**
2095  * Task run during shutdown.
2096  *
2097  * @param cls unused
2098  * @param tc unused
2099  */
2100 static void
2101 shutdown_task (void *cls,
2102                const struct GNUNET_SCHEDULER_TaskContext *tc)
2103 {
2104 #if DEBUG_DV
2105   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2106   GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &print_neighbors, NULL);
2107 #endif
2108   GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &free_extended_neighbors, NULL);
2109   GNUNET_CONTAINER_multihashmap_destroy(extended_neighbors);
2110   GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &free_direct_neighbors, NULL);
2111   GNUNET_CONTAINER_multihashmap_destroy(direct_neighbors);
2112
2113   GNUNET_CONTAINER_heap_destroy(neighbor_max_heap);
2114   GNUNET_CONTAINER_heap_destroy(neighbor_min_heap);
2115
2116   GNUNET_CORE_disconnect (coreAPI);
2117   GNUNET_PEERINFO_disconnect(peerinfo_handle);
2118   GNUNET_SERVER_mst_destroy(coreMST);
2119   GNUNET_free_non_null(my_short_id);
2120 #if DEBUG_DV
2121   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2122 #endif
2123 }
2124
2125 /**
2126  * To be called on core init/fail.
2127  */
2128 void core_init (void *cls,
2129                 struct GNUNET_CORE_Handle * server,
2130                 const struct GNUNET_PeerIdentity *identity,
2131                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
2132 {
2133
2134   if (server == NULL)
2135     {
2136       GNUNET_SCHEDULER_cancel(sched, cleanup_task);
2137       GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
2138       return;
2139     }
2140 #if DEBUG_DV
2141   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2142               "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
2143 #endif
2144   memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
2145   my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
2146   coreAPI = server;
2147 }
2148
2149
2150 #if PKEY_NO_NEIGHBOR_ON_ADD
2151 /**
2152  * Iterator over hash map entries.
2153  *
2154  * @param cls closure
2155  * @param key current key code
2156  * @param value value in the hash map
2157  * @return GNUNET_YES if we should continue to
2158  *         iterate,
2159  *         GNUNET_NO if not.
2160  */
2161 static int add_pkey_to_extended (void *cls,
2162                                  const GNUNET_HashCode * key,
2163                                  void *abs_value)
2164 {
2165   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2166   struct DistantNeighbor *distant_neighbor = abs_value;
2167
2168   if (distant_neighbor->pkey == NULL)
2169   {
2170     distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2171     memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2172   }
2173
2174   return GNUNET_YES;
2175 }
2176 #endif
2177
2178 /**
2179  * Iterator over hash map entries.
2180  *
2181  * @param cls closure
2182  * @param key current key code
2183  * @param value value in the hash map
2184  * @return GNUNET_YES if we should continue to
2185  *         iterate,
2186  *         GNUNET_NO if not.
2187  */
2188 static int update_matching_neighbors (void *cls,
2189                                       const GNUNET_HashCode * key,
2190                                       void *value)
2191 {
2192   struct NeighborUpdateInfo * update_info = cls;
2193   struct DistantNeighbor *distant_neighbor = value;
2194
2195   if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2196   {
2197     /* same referrer, cost change! */
2198     GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2199                                        update_info->neighbor->max_loc, update_info->cost);
2200     GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2201                                        update_info->neighbor->min_loc, update_info->cost);
2202     update_info->neighbor->last_activity = update_info->now;
2203     update_info->neighbor->cost = update_info->cost;
2204     update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2205     return GNUNET_NO;
2206   }
2207
2208   return GNUNET_YES;
2209 }
2210
2211
2212 /**
2213  * Iterate over all current direct peers, add DISTANT newly connected
2214  * peer to the fast gossip list for that peer so we get DV routing
2215  * information out as fast as possible!
2216  *
2217  * @param cls the newly connected neighbor we will gossip about
2218  * @param key the hashcode of the peer
2219  * @param value the direct neighbor we should gossip to
2220  *
2221  * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2222  */
2223 static int add_distant_all_direct_neighbors (void *cls,
2224                                      const GNUNET_HashCode * key,
2225                                      void *value)
2226 {
2227   struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2228   struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2229   struct NeighborSendContext *send_context = direct->send_context;
2230   struct FastGossipNeighborList *gossip_entry;
2231 #if DEBUG_DV
2232   char *encPeerAbout;
2233   char *encPeerTo;
2234 #endif
2235
2236   if (distant == NULL)
2237     {
2238       return GNUNET_YES;
2239     }
2240
2241   if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2242     {
2243       return GNUNET_YES; /* Don't gossip to a peer about itself! */
2244     }
2245
2246 #if SUPPORT_HIDING
2247   if (distant->hidden == GNUNET_YES)
2248     return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2249 #endif
2250   gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2251   gossip_entry->about = distant;
2252
2253   GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2254                                     send_context->fast_gossip_list_tail,
2255                                     send_context->fast_gossip_list_tail,
2256                                     gossip_entry);
2257 #if DEBUG_DV
2258   encPeerAbout = GNUNET_strdup(GNUNET_i2s(&distant->identity));
2259   encPeerTo = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2260
2261   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2262              GNUNET_i2s(&my_identity),
2263              encPeerAbout, distant->our_id, encPeerTo);
2264   GNUNET_free(encPeerAbout);
2265   GNUNET_free(encPeerTo);
2266 #endif
2267   /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2268     GNUNET_SCHEDULER_cancel(sched, send_context->task);*/
2269
2270   send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2271   return GNUNET_YES;
2272 }
2273
2274 /**
2275  * Callback for hello address creation.
2276  *
2277  * @param cls closure, a struct HelloContext
2278  * @param max maximum number of bytes that can be written to buf
2279  * @param buf where to write the address information
2280  *
2281  * @return number of bytes written, 0 to signal the
2282  *         end of the iteration.
2283  */
2284 static size_t
2285 generate_hello_address (void *cls, size_t max, void *buf)
2286 {
2287   struct HelloContext *hello_context = cls;
2288   char *addr_buffer;
2289   size_t offset;
2290   size_t size;
2291   size_t ret;
2292
2293   if (hello_context->addresses_to_add == 0)
2294     return 0;
2295
2296   /* Hello "address" will be concatenation of distant peer and direct peer identities */
2297   size = 2 * sizeof(struct GNUNET_PeerIdentity);
2298   GNUNET_assert(max >= size);
2299
2300   addr_buffer = GNUNET_malloc(size);
2301   offset = 0;
2302   /* Copy the distant peer identity to buffer */
2303   memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
2304   offset += sizeof(struct GNUNET_PeerIdentity);
2305   /* Copy the direct peer identity to buffer */
2306   memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
2307   ret = GNUNET_HELLO_add_address ("dv",
2308                                   GNUNET_TIME_relative_to_absolute
2309                                   (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2310                                   buf, max);
2311
2312   hello_context->addresses_to_add--;
2313
2314   GNUNET_free(addr_buffer);
2315   return ret;
2316 }
2317
2318
2319 /**
2320  * Handles when a peer is either added due to being newly connected
2321  * or having been gossiped about, also called when the cost for a neighbor
2322  * needs to be updated.
2323  *
2324  * @param peer identity of the peer whose info is being added/updated
2325  * @param pkey public key of the peer whose info is being added/updated
2326  * @param referrer_peer_id id to use when sending to 'peer'
2327  * @param referrer if this is a gossiped peer, who did we hear it from?
2328  * @param cost the cost of communicating with this peer via 'referrer'
2329  *
2330  * @return the added neighbor, the updated neighbor or NULL (neighbor
2331  *         not added)
2332  */
2333 static struct DistantNeighbor *
2334 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2335                    unsigned int referrer_peer_id,
2336                    struct DirectNeighbor *referrer, unsigned int cost)
2337 {
2338   struct DistantNeighbor *neighbor;
2339   struct DistantNeighbor *max;
2340   struct GNUNET_TIME_Absolute now;
2341   struct NeighborUpdateInfo *neighbor_update;
2342   struct HelloContext *hello_context;
2343   struct GNUNET_HELLO_Message *hello_msg;
2344   unsigned int our_id;
2345   char *addr1;
2346   char *addr2;
2347   int i;
2348
2349 #if DEBUG_DV_PEER_NUMBERS
2350   char *encAbout;
2351   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352               "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
2353 #endif
2354
2355   now = GNUNET_TIME_absolute_get ();
2356   neighbor = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2357                                                 &peer->hashPubKey);
2358   neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
2359   neighbor_update->neighbor = neighbor;
2360   neighbor_update->cost = cost;
2361   neighbor_update->now = now;
2362   neighbor_update->referrer = referrer;
2363   neighbor_update->referrer_peer_id = referrer_peer_id;
2364
2365   if (neighbor != NULL)
2366     {
2367 #if USE_PEER_ID
2368       memcpy(&our_id, &neighbor->identity, sizeof(unsigned int));
2369 #else
2370       our_id = neighbor->our_id;
2371 #endif
2372     }
2373   else
2374     {
2375 #if USE_PEER_ID
2376       memcpy(&our_id, peer, sizeof(unsigned int));
2377 #else
2378       our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
2379 #endif
2380     }
2381
2382   /* Either we do not know this peer, or we already do but via a different immediate peer */
2383   if ((neighbor == NULL) ||
2384       (GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2385                                                   &peer->hashPubKey,
2386                                                   &update_matching_neighbors,
2387                                                   neighbor_update) != GNUNET_SYSERR))
2388     {
2389
2390 #if AT_MOST_ONE
2391     if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2392       {
2393         distant_neighbor_free(neighbor);
2394       }
2395     else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2396       {
2397         return NULL;
2398       }
2399 #endif
2400       /* new neighbor! */
2401       if (cost > fisheye_depth)
2402         {
2403           /* too costly */
2404           GNUNET_free(neighbor_update);
2405           return NULL;
2406         }
2407
2408 #if DEBUG_DV_PEER_NUMBERS
2409       encAbout = GNUNET_strdup(GNUNET_i2s(peer));
2410       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411                   "%s: %s Chose NEW id (%u) for peer %s!\n", GNUNET_i2s(&my_identity), "DV SERVICE", our_id, encAbout);
2412       GNUNET_free(encAbout);
2413 #endif
2414
2415       if (max_table_size <=
2416           GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2417         {
2418           /* remove most expensive entry */
2419           max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2420           GNUNET_assert(max != NULL);
2421           if (cost > max->cost)
2422             {
2423               /* new entry most expensive, don't create */
2424               GNUNET_free(neighbor_update);
2425               return NULL;
2426             }
2427           if (max->cost > 1)
2428             {
2429               /* only free if this is not a direct connection;
2430                  we could theoretically have more direct
2431                  connections than DV entries allowed total! */
2432               distant_neighbor_free (max);
2433             }
2434         }
2435
2436       neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2437       GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
2438                          referrer->referee_tail, neighbor);
2439       neighbor->max_loc = GNUNET_CONTAINER_heap_insert (neighbor_max_heap,
2440                                                         neighbor, cost);
2441       neighbor->min_loc = GNUNET_CONTAINER_heap_insert (neighbor_min_heap,
2442                                                         neighbor, cost);
2443       neighbor->referrer = referrer;
2444       memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2445       if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2446       {
2447         neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2448         memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2449       }
2450       else
2451         neighbor->pkey = pkey;
2452
2453       neighbor->last_activity = now;
2454       neighbor->cost = cost;
2455       neighbor->referrer_id = referrer_peer_id;
2456       neighbor->our_id = our_id;
2457       neighbor->hidden =
2458         (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2459                        0) : GNUNET_NO;
2460
2461       GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2462                                  neighbor,
2463                                  GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2464       if (referrer_peer_id != 0)
2465         {
2466           for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
2467             {
2468               if (referrer->pending_messages[i].sender_id == referrer_peer_id) /* We have a queued message from just learned about peer! */
2469                 {
2470 #if DEBUG_DV_MESSAGES
2471                   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: learned about peer %llu from which we have a previous unknown message, processing!\n", my_short_id, referrer_peer_id);
2472 #endif
2473                   handle_dv_data_message(NULL, &referrer->pending_messages[i].sender, referrer->pending_messages[i].message, referrer->pending_messages[i].latency, referrer->pending_messages[i].distance);
2474                   GNUNET_free(referrer->pending_messages[i].message);
2475                   referrer->pending_messages[i].sender_id = 0;
2476                 }
2477             }
2478         }
2479       if ((cost != DIRECT_NEIGHBOR_COST) && (neighbor->pkey != NULL))
2480         {
2481           /* Added neighbor, now send HELLO to transport */
2482           hello_context = GNUNET_malloc(sizeof(struct HelloContext));
2483           hello_context->direct_peer = &referrer->identity;
2484           memcpy(&hello_context->distant_peer, peer, sizeof(struct GNUNET_PeerIdentity));
2485           hello_context->addresses_to_add = 1;
2486           hello_msg = GNUNET_HELLO_create(pkey, &generate_hello_address, hello_context);
2487           GNUNET_assert(memcmp(hello_context->direct_peer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)) != 0);
2488           addr1 = GNUNET_strdup(GNUNET_i2s(hello_context->direct_peer));
2489           addr2 = GNUNET_strdup(GNUNET_i2s(&hello_context->distant_peer));
2490 #if DEBUG_DV
2491           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: GIVING HELLO size %d for %s via %s to TRANSPORT\n", my_short_id, GNUNET_HELLO_size(hello_msg), addr2, addr1);
2492 #endif
2493           GNUNET_free(addr1);
2494           GNUNET_free(addr2);
2495           send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, cost);
2496           GNUNET_free(hello_context);
2497           GNUNET_free(hello_msg);
2498         }
2499
2500     }
2501   else
2502     {
2503 #if DEBUG_DV_GOSSIP
2504       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505                   "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
2506 #endif
2507     }
2508 #if DEBUG_DV
2509     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510                 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(extended_neighbors));
2511 #endif
2512
2513   GNUNET_free(neighbor_update);
2514   return neighbor;
2515 }
2516
2517
2518 /**
2519  * Core handler for dv disconnect messages.  These will be used
2520  * by us to tell transport via the dv plugin that a peer can
2521  * no longer be contacted by us via a certain address.  We should
2522  * then propagate these messages on, given that the distance to
2523  * the peer indicates we would have gossiped about it to others.
2524  *
2525  * @param cls closure
2526  * @param peer peer which sent the message (immediate sender)
2527  * @param message the message
2528  * @param latency the latency of the connection we received the message from
2529  * @param distance the distance to the immediate peer
2530  */
2531 static int handle_dv_disconnect_message (void *cls,
2532                                          const struct GNUNET_PeerIdentity *peer,
2533                                          const struct GNUNET_MessageHeader *message,
2534                                          struct GNUNET_TIME_Relative latency,
2535                                          uint32_t distance)
2536 {
2537   struct DirectNeighbor *referrer;
2538   struct DistantNeighbor *distant;
2539   p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
2540
2541   if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2542     {
2543       return GNUNET_SYSERR;     /* invalid message */
2544     }
2545
2546   referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2547                                                 &peer->hashPubKey);
2548   if (referrer == NULL)
2549     return GNUNET_OK;
2550
2551   distant = referrer->referee_head;
2552   while (distant != NULL)
2553     {
2554       if (distant->referrer_id == ntohl(enc_message->peer_id))
2555         {
2556           distant_neighbor_free(distant);
2557           distant = referrer->referee_head;
2558         }
2559       else
2560         distant = distant->next;
2561     }
2562
2563   return GNUNET_OK;
2564 }
2565
2566
2567 /**
2568  * Core handler for dv gossip messages.  These will be used
2569  * by us to create a HELLO message for the newly peer containing
2570  * which direct peer we can connect through, and what the cost
2571  * is.  This HELLO will then be scheduled for validation by the
2572  * transport service so that it can be used by all others.
2573  *
2574  * @param cls closure
2575  * @param peer peer which sent the message (immediate sender)
2576  * @param message the message
2577  * @param latency the latency of the connection we received the message from
2578  * @param distance the distance to the immediate peer
2579  */
2580 static int handle_dv_gossip_message (void *cls,
2581                                      const struct GNUNET_PeerIdentity *peer,
2582                                      const struct GNUNET_MessageHeader *message,
2583                                      struct GNUNET_TIME_Relative latency,
2584                                      uint32_t distance)
2585 {
2586   struct DirectNeighbor *referrer;
2587   p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
2588
2589   if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2590     {
2591       return GNUNET_SYSERR;     /* invalid message */
2592     }
2593
2594 #if DEBUG_DV_GOSSIP_RECEIPT
2595   char * encPeerAbout;
2596   char * encPeerFrom;
2597
2598   encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
2599   encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
2600   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601               "%s: Received %s message from peer %s about peer %s id %u distance %d!\n", GNUNET_i2s(&my_identity), "DV GOSSIP", encPeerFrom, encPeerAbout, ntohl(enc_message->neighbor_id), ntohl (enc_message->cost) + 1);
2602   GNUNET_free(encPeerAbout);
2603   GNUNET_free(encPeerFrom);
2604 #endif
2605
2606   referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2607                                                 &peer->hashPubKey);
2608   if (referrer == NULL)
2609     return GNUNET_OK;
2610
2611   addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2612                      ntohl (enc_message->neighbor_id),
2613                      referrer, ntohl (enc_message->cost) + 1);
2614
2615   return GNUNET_OK;
2616 }
2617
2618
2619 /**
2620  * Iterate over all currently known peers, add them to the
2621  * fast gossip list for this peer so we get DV routing information
2622  * out as fast as possible!
2623  *
2624  * @param cls the direct neighbor we will gossip to
2625  * @param key the hashcode of the peer
2626  * @param value the distant neighbor we should add to the list
2627  *
2628  * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2629  */
2630 static int add_all_extended_peers (void *cls,
2631                                    const GNUNET_HashCode * key,
2632                                    void *value)
2633 {
2634   struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2635   struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2636   struct FastGossipNeighborList *gossip_entry;
2637
2638   if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2639     return GNUNET_YES; /* Don't gossip to a peer about itself! */
2640
2641 #if SUPPORT_HIDING
2642   if (distant->hidden == GNUNET_YES)
2643     return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2644 #endif
2645   gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2646   gossip_entry->about = distant;
2647
2648   GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2649                                     send_context->fast_gossip_list_tail,
2650                                     send_context->fast_gossip_list_tail,
2651                                     gossip_entry);
2652
2653   return GNUNET_YES;
2654 }
2655
2656 #if INSANE_GOSSIP
2657 /**
2658  * Iterator over hash map entries.
2659  *
2660  * @param cls closure
2661  * @param key current key code
2662  * @param value value in the hash map
2663  * @return GNUNET_YES if we should continue to
2664  *         iterate,
2665  *         GNUNET_NO if not.
2666  */
2667 static int gossip_all_to_all_iterator (void *cls,
2668                                       const GNUNET_HashCode * key,
2669                                       void *abs_value)
2670 {
2671   struct DirectNeighbor *direct = abs_value;
2672
2673   GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, direct->send_context);
2674
2675   if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2676     GNUNET_SCHEDULER_cancel(sched, direct->send_context->task);
2677
2678   direct->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, direct->send_context);
2679   return GNUNET_YES;
2680 }
2681
2682 /**
2683  * Task run during shutdown.
2684  *
2685  * @param cls unused
2686  * @param tc unused
2687  */
2688 static void
2689 gossip_all_to_all (void *cls,
2690                    const struct GNUNET_SCHEDULER_TaskContext *tc)
2691 {
2692   GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &gossip_all_to_all_iterator, NULL);
2693
2694   GNUNET_SCHEDULER_add_delayed (sched,
2695                                 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2696                                 &gossip_all_to_all,
2697                                 NULL);
2698
2699 }
2700 #endif
2701 /**
2702  * Iterate over all current direct peers, add newly connected peer
2703  * to the fast gossip list for that peer so we get DV routing
2704  * information out as fast as possible!
2705  *
2706  * @param cls the newly connected neighbor we will gossip about
2707  * @param key the hashcode of the peer
2708  * @param value the direct neighbor we should gossip to
2709  *
2710  * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2711  */
2712 static int add_all_direct_neighbors (void *cls,
2713                                      const GNUNET_HashCode * key,
2714                                      void *value)
2715 {
2716   struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2717   struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2718   struct DistantNeighbor *distant;
2719   struct NeighborSendContext *send_context = direct->send_context;
2720   struct FastGossipNeighborList *gossip_entry;
2721   char *direct_id;
2722
2723
2724   distant = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &to->identity.hashPubKey);
2725   if (distant == NULL)
2726     {
2727       return GNUNET_YES;
2728     }
2729
2730   if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2731     {
2732       return GNUNET_YES; /* Don't gossip to a peer about itself! */
2733     }
2734
2735 #if SUPPORT_HIDING
2736   if (distant->hidden == GNUNET_YES)
2737     return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2738 #endif
2739   direct_id = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2740 #if DEBUG_DV_GOSSIP
2741   GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: adding peer %s to fast send list for %s\n", my_short_id, GNUNET_i2s(&distant->identity), direct_id);
2742 #endif
2743   GNUNET_free(direct_id);
2744   gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2745   gossip_entry->about = distant;
2746
2747   GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2748                                     send_context->fast_gossip_list_tail,
2749                                     send_context->fast_gossip_list_tail,
2750                                     gossip_entry);
2751   if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2752     GNUNET_SCHEDULER_cancel(sched, send_context->task);
2753
2754   send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2755   //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2756   //neighbor_send_task(send_context, &tc);
2757   return GNUNET_YES;
2758 }
2759
2760 /**
2761  * Type of an iterator over the hosts.  Note that each
2762  * host will be called with each available protocol.
2763  *
2764  * @param cls closure
2765  * @param peer id of the peer, NULL for last call
2766  * @param hello hello message for the peer (can be NULL)
2767  */
2768 static void
2769 process_peerinfo (void *cls,
2770                   const struct GNUNET_PeerIdentity *peer,
2771                   const struct GNUNET_HELLO_Message *hello)
2772 {
2773   struct PeerIteratorContext *peerinfo_iterator = cls;
2774   struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2775   struct DistantNeighbor *distant = peerinfo_iterator->distant;
2776 #if DEBUG_DV_PEER_NUMBERS
2777   char *neighbor_pid;
2778 #endif
2779   int sent;
2780
2781   if (peer == NULL)
2782     {
2783       if (distant->pkey == NULL)
2784         {
2785 #if DEBUG_DV
2786           GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to get peerinfo information for this peer, retrying!\n");
2787 #endif
2788           peerinfo_iterator->ic = GNUNET_PEERINFO_iterate(peerinfo_handle,
2789                                                           &peerinfo_iterator->neighbor->identity,
2790                                                           GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2791                                                           &process_peerinfo,
2792                                                           peerinfo_iterator);
2793         }
2794       else
2795         {
2796           GNUNET_free(peerinfo_iterator);
2797         }
2798       return;
2799     }
2800
2801   if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2802     return;
2803
2804   if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2805     {
2806       if (distant->pkey == NULL)
2807         {
2808           distant->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2809           memcpy(distant->pkey, &neighbor->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2810         }
2811
2812       sent = GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2813       if (stats != NULL)
2814         {
2815           GNUNET_STATISTICS_update (stats, "# distant peers gossiped to direct neighbors", sent, GNUNET_NO);
2816         }
2817 #if DEBUG_DV_PEER_NUMBERS
2818       neighbor_pid = GNUNET_strdup(GNUNET_i2s(&neighbor->identity));
2819       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped %d extended peers to %s\n", GNUNET_i2s(&my_identity), sent, neighbor_pid);
2820 #endif
2821       sent = GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &add_all_direct_neighbors, neighbor);
2822       if (stats != NULL)
2823         {
2824           GNUNET_STATISTICS_update (stats, "# direct peers gossiped to direct neighbors", sent, GNUNET_NO);
2825         }
2826 #if DEBUG_DV_PEER_NUMBERS
2827       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped about %s to %d direct peers\n", GNUNET_i2s(&my_identity), neighbor_pid, sent);
2828       GNUNET_free(neighbor_pid);
2829 #endif
2830       neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2831     }
2832 }
2833
2834
2835 /**
2836  * Method called whenever a peer connects.
2837  *
2838  * @param cls closure
2839  * @param peer peer identity this notification is about
2840  * @param latency reported latency of the connection with peer
2841  * @param distance reported distance (DV) to peer
2842  */
2843 void handle_core_connect (void *cls,
2844                           const struct GNUNET_PeerIdentity * peer,
2845                           struct GNUNET_TIME_Relative latency,
2846                           uint32_t distance)
2847 {
2848   struct DirectNeighbor *neighbor;
2849   struct DistantNeighbor *about;
2850   struct PeerIteratorContext *peerinfo_iterator;
2851   int sent;
2852 #if DEBUG_DV
2853   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2854               "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2855 #endif
2856
2857   if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL))
2858   {
2859     peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2860     neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2861     neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2862     neighbor->send_context->toNeighbor = neighbor;
2863     memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2864
2865     GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
2866                                &peer->hashPubKey,
2867                                neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2868     about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2869     peerinfo_iterator->distant = about;
2870     peerinfo_iterator->neighbor = neighbor;
2871     peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2872                                                      peer,
2873                                                      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2874                                                      &process_peerinfo,
2875                                                      peerinfo_iterator);
2876
2877     if ((about != NULL) && (about->pkey == NULL))
2878       {
2879 #if DEBUG_DV
2880         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Newly added peer %s has NULL pkey!\n", GNUNET_i2s(peer));
2881 #endif
2882       }
2883     else if (about != NULL)
2884       {
2885         GNUNET_free(peerinfo_iterator);
2886       }
2887   }
2888   else
2889   {
2890     about = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &peer->hashPubKey);
2891     if ((GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2892       {
2893         sent = GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &add_distant_all_direct_neighbors, about);
2894         if (stats != NULL)
2895           {
2896             GNUNET_STATISTICS_update (stats, "# direct peers gossiped to new direct neighbors", sent, GNUNET_NO);
2897           }
2898       }
2899 #if DEBUG_DV
2900     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901                 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2902 #endif
2903     return;
2904   }
2905 }
2906
2907 /**
2908  * Method called whenever a given peer disconnects.
2909  *
2910  * @param cls closure
2911  * @param peer peer identity this notification is about
2912  */
2913 void handle_core_disconnect (void *cls,
2914                              const struct GNUNET_PeerIdentity * peer)
2915 {
2916   struct DirectNeighbor *neighbor;
2917   struct DistantNeighbor *referee;
2918   struct FindDestinationContext fdc;
2919   struct DisconnectContext disconnect_context;
2920
2921 #if DEBUG_DV
2922   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2923               "%s: Receives core peer disconnect message!\n", "dv");
2924 #endif
2925
2926   neighbor =
2927     GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2928   if (neighbor == NULL)
2929     {
2930       return;
2931     }
2932   while (NULL != (referee = neighbor->referee_head))
2933     distant_neighbor_free (referee);
2934
2935   fdc.dest = NULL;
2936   fdc.tid = 0;
2937
2938   GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer, &fdc);
2939
2940   if (fdc.dest != NULL)
2941     {
2942       disconnect_context.direct = neighbor;
2943       disconnect_context.distant = fdc.dest;
2944       GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2945     }
2946
2947   GNUNET_assert (neighbor->referee_tail == NULL);
2948   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
2949                                         &peer->hashPubKey, neighbor))
2950     {
2951       GNUNET_break(0);
2952     }
2953   if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2954     GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2955   GNUNET_free (neighbor);
2956 }
2957
2958
2959 /**
2960  * Process dv requests.
2961  *
2962  * @param cls closure
2963  * @param scheduler scheduler to use
2964  * @param server the initialized server
2965  * @param c configuration to use
2966  */
2967 static void
2968 run (void *cls,
2969      struct GNUNET_SCHEDULER_Handle *scheduler,
2970      struct GNUNET_SERVER_Handle *server,
2971      const struct GNUNET_CONFIGURATION_Handle *c)
2972 {
2973   unsigned long long max_hosts;
2974   sched = scheduler;
2975   cfg = c;
2976
2977   /* FIXME: Read from config, or calculate, or something other than this! */
2978   max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2979   max_table_size = DEFAULT_DV_SIZE;
2980   fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2981
2982   if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2983     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts));
2984
2985   if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2986     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &max_table_size));
2987
2988
2989   if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2990     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &fisheye_depth));
2991
2992   neighbor_min_heap =
2993     GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2994   neighbor_max_heap =
2995     GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2996
2997   direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2998   extended_neighbors =
2999     GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
3000
3001   GNUNET_SERVER_add_handlers (server, plugin_handlers);
3002   coreAPI =
3003   GNUNET_CORE_connect (sched,
3004                        cfg,
3005                        GNUNET_TIME_relative_get_forever(),
3006                        NULL, /* FIXME: anything we want to pass around? */
3007                        &core_init,
3008                        &handle_core_connect,
3009                        &handle_core_disconnect,
3010                        NULL,
3011                        NULL,
3012                        GNUNET_NO,
3013                        NULL,
3014                        GNUNET_NO,
3015                        core_handlers);
3016
3017   if (coreAPI == NULL)
3018     return;
3019
3020   coreMST = GNUNET_SERVER_mst_create (&tokenized_message_handler,
3021                                       NULL);
3022
3023    peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
3024
3025    if (peerinfo_handle == NULL)
3026      {
3027        GNUNET_CORE_disconnect(coreAPI);
3028        return;
3029      }
3030
3031   /* Scheduled the task to clean up when shutdown is called */
3032   cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
3033                                 GNUNET_TIME_UNIT_FOREVER_REL,
3034                                 &shutdown_task,
3035                                 NULL);
3036 }
3037
3038
3039 /**
3040  * The main function for the dv service.
3041  *
3042  * @param argc number of arguments from the command line
3043  * @param argv command line arguments
3044  * @return 0 ok, 1 on error
3045  */
3046 int
3047 main (int argc, char *const *argv)
3048 {
3049   return (GNUNET_OK ==
3050           GNUNET_SERVICE_run (argc,
3051                               argv,
3052                               "dv",
3053                               GNUNET_SERVICE_OPTION_NONE,
3054                               &run, NULL)) ? 0 : 1;
3055 }