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