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