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