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