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