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