3a0d4f93617994a6f63c8dbe135ac8953f738192
[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  * DV Service Context stuff goes here...
50  */
51
52 /**
53  * Handle to the core service api.
54  */
55 static struct GNUNET_CORE_Handle *coreAPI;
56
57 /**
58  * The identity of our peer.
59  */
60 static struct GNUNET_PeerIdentity my_identity;
61
62 /**
63  * The configuration for this service.
64  */
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
66
67 /**
68  * The scheduler for this service.
69  */
70 static struct GNUNET_SCHEDULER_Handle *sched;
71
72 /**
73  * How often do we check about sending out more peer information (if
74  * we are connected to no peers previously).
75  */
76 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50000)
77
78 /**
79  * How long do we wait at most between sending out information?
80  */
81 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50000)
82
83 /**
84  * How long can we have not heard from a peer and
85  * still have it in our tables?
86  */
87 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
88
89 /**
90  * Priority for gossip.
91  */
92 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
93
94 /**
95  * How often should we check if expiration time has elapsed for
96  * some peer?
97  */
98 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
99
100 /**
101  * How long to allow a message to be delayed?
102  */
103 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
104
105 /**
106  * Priority to use for DV data messages.
107  */
108 #define DV_PRIORITY 0
109
110 /**
111  * The cost to a direct neighbor.  We used to use 0, but 1 makes more sense.
112  */
113 #define DIRECT_NEIGHBOR_COST 1
114
115 /**
116  * The default number of direct connections to store in DV (max)
117  */
118 #define DEFAULT_DIRECT_CONNECTIONS 50
119
120 /**
121  * The default size of direct + extended peers in DV (max)
122  */
123 #define DEFAULT_DV_SIZE 100
124
125 /**
126  * The default fisheye depth, from how many hops away will
127  * we keep peers?
128  */
129 #define DEFAULT_FISHEYE_DEPTH 4
130
131 /**
132  * The client, the DV plugin connected to us.  Hopefully
133  * this client will never change, although if the plugin dies
134  * and returns for some reason it may happen.
135  */
136 static struct GNUNET_SERVER_Client * client_handle;
137
138 /**
139  * Task to run when we shut down, cleaning up all our trash
140  */
141 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
142
143 /**
144  * Task to run to gossip about peers.  Will reschedule itself forever until shutdown!
145  */
146 GNUNET_SCHEDULER_TaskIdentifier gossip_task;
147
148 /**
149  * Struct where neighbor information is stored.
150  */
151 struct DistantNeighbor *referees;
152
153 static size_t default_dv_priority = 0;
154
155
156 /**
157  * Linked list of messages to send to clients.
158  */
159 struct PendingMessage
160 {
161   /**
162    * Pointer to next item in the list
163    */
164   struct PendingMessage *next;
165
166   /**
167    * Pointer to previous item in the list
168    */
169   struct PendingMessage *prev;
170
171   struct GNUNET_DV_SendResultMessage *send_result;
172   /**
173    * Actual message to be sent; // avoid allocation
174    */
175   const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
176
177 };
178
179 /**
180  * Transmit handle to the plugin.
181  */
182 struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
183
184 /**
185  * Head of DLL for client messages
186  */
187 struct PendingMessage *plugin_pending_head;
188
189 /**
190  * Tail of DLL for client messages
191  */
192 struct PendingMessage *plugin_pending_tail;
193
194 /**
195  * Handle to the peerinfo service
196  */
197 struct GNUNET_PEERINFO_Handle *peerinfo_handle;
198
199 /**
200  * Transmit handle to core service.
201  */
202 struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
203
204 /**
205  * Head of DLL for core messages
206  */
207 struct PendingMessage *core_pending_head;
208
209 /**
210  * Tail of DLL for core messages
211  */
212 struct PendingMessage *core_pending_tail;
213
214
215 struct FastGossipNeighborList
216 {
217   /**
218    * Next element of DLL
219    */
220   struct FastGossipNeighborList *next;
221
222   /**
223    * Prev element of DLL
224    */
225   struct FastGossipNeighborList *prev;
226
227   /**
228    * The neighbor to gossip about
229    */
230   struct DistantNeighbor *about;
231 };
232
233 /**
234  * Context created whenever a direct peer connects to us,
235  * used to gossip other peers to it.
236  */
237 struct NeighborSendContext
238 {
239   /**
240    * The peer we will gossip to.
241    */
242   struct DirectNeighbor *toNeighbor;
243
244   /**
245    * The task associated with this context.
246    */
247   GNUNET_SCHEDULER_TaskIdentifier task;
248
249   /**
250    * Head of DLL of peers to gossip about
251    * as fast as possible to this peer, for initial
252    * set up.
253    */
254   struct FastGossipNeighborList *fast_gossip_list_head;
255
256   /**
257    * Tail of DLL of peers to gossip about
258    * as fast as possible to this peer, for initial
259    * set up.
260    */
261   struct FastGossipNeighborList *fast_gossip_list_tail;
262
263 };
264
265
266 /**
267  * Struct to hold information for updating existing neighbors
268  */
269 struct NeighborUpdateInfo
270 {
271   /**
272    * Cost
273    */
274   unsigned int cost;
275
276   /**
277    * The existing neighbor
278    */
279   struct DistantNeighbor *neighbor;
280
281   /**
282    * The referrer of the possibly existing peer
283    */
284   struct DirectNeighbor *referrer;
285
286   /**
287    * The time we heard about this peer
288    */
289   struct GNUNET_TIME_Absolute now;
290 };
291
292 /**
293  * Struct where actual neighbor information is stored,
294  * referenced by min_heap and max_heap.  Freeing dealt
295  * with when items removed from hashmap.
296  */
297 struct DirectNeighbor
298 {
299   /**
300    * Identity of neighbor.
301    */
302   struct GNUNET_PeerIdentity identity;
303
304   /**
305    * PublicKey of neighbor.
306    */
307   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
308
309   /**
310    * Head of DLL of nodes that this direct neighbor referred to us.
311    */
312   struct DistantNeighbor *referee_head;
313
314   /**
315    * Tail of DLL of nodes that this direct neighbor referred to us.
316    */
317   struct DistantNeighbor *referee_tail;
318
319   /**
320    * The sending context for gossiping peers to this neighbor.
321    */
322   struct NeighborSendContext *send_context;
323
324   /**
325    * Is this one of the direct neighbors that we are "hiding"
326    * from DV?
327    */
328   int hidden;
329 };
330
331
332 /**
333  * Struct where actual neighbor information is stored,
334  * referenced by min_heap and max_heap.  Freeing dealt
335  * with when items removed from hashmap.
336  */
337 struct DistantNeighbor
338 {
339   /**
340    * We keep distant neighbor's of the same referrer in a DLL.
341    */
342   struct DistantNeighbor *next;
343
344   /**
345    * We keep distant neighbor's of the same referrer in a DLL.
346    */
347   struct DistantNeighbor *prev;
348
349   /**
350    * Node in min heap
351    */
352   struct GNUNET_CONTAINER_HeapNode *min_loc;
353
354   /**
355    * Node in max heap
356    */
357   struct GNUNET_CONTAINER_HeapNode *max_loc;
358
359   /**
360    * Identity of referrer (next hop towards 'neighbor').
361    */
362   struct DirectNeighbor *referrer;
363
364   /**
365    * Identity of neighbor.
366    */
367   struct GNUNET_PeerIdentity identity;
368
369   /**
370    * PublicKey of neighbor.
371    */
372   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
373
374   /**
375    * Last time we received routing information from this peer
376    */
377   struct GNUNET_TIME_Absolute last_activity;
378
379   /**
380    * Cost to neighbor, used for actual distance vector computations
381    */
382   unsigned int cost;
383
384   /**
385    * Random identifier *we* use for this peer, to be used as shortcut
386    * instead of sending full peer id for each message
387    */
388   unsigned int our_id;
389
390   /**
391    * Random identifier the *referrer* uses for this peer.
392    */
393   unsigned int referrer_id;
394
395   /**
396    * Is this one of the direct neighbors that we are "hiding"
397    * from DV?
398    */
399   int hidden;
400
401 };
402
403 struct PeerIteratorContext
404 {
405   /**
406    * The actual context, to be freed later.
407    */
408   struct GNUNET_PEERINFO_IteratorContext *ic;
409
410   /**
411    * The neighbor about which we are concerned.
412    */
413   struct DirectNeighbor *neighbor;
414
415 };
416
417 /**
418  * Context used for creating hello messages when
419  * gossips are received.
420  */
421 struct HelloContext
422 {
423   /**
424    * Identity of distant neighbor.
425    */
426   struct GNUNET_PeerIdentity distant_peer;
427
428   /**
429    * Identity of direct neighbor, via which we send this message.
430    */
431   const struct GNUNET_PeerIdentity *direct_peer;
432
433   /**
434    * How many addresses do we need to add (always starts at 1, then set to 0)
435    */
436   int addresses_to_add;
437
438 };
439
440 struct DV_SendContext
441 {
442   /**
443    * The distant peer (should always match)
444    */
445   struct GNUNET_PeerIdentity *distant_peer;
446
447   /**
448    * The direct peer, we need to verify the referrer of.
449    */
450   struct GNUNET_PeerIdentity *direct_peer;
451
452   /**
453    * The message to be sent
454    */
455   struct GNUNET_MessageHeader *message;
456
457   /**
458    * The pre-built send result message.  Simply needs to be queued
459    * and freed once send has been called!
460    */
461   struct GNUNET_DV_SendResultMessage *send_result;
462
463   /**
464    * The size of the message being sent, may be larger
465    * than message->header.size because it's multiple
466    * messages packed into one!
467    */
468   size_t message_size;
469
470   /**
471    * How important is this message?
472    */
473   unsigned int importance;
474
475   /**
476    * Timeout for this message
477    */
478   struct GNUNET_TIME_Relative timeout;
479 };
480
481 /**
482  * Global construct
483  */
484 struct GNUNET_DV_Context
485 {
486   /**
487    * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
488    * directly connected peers.
489    */
490   struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
491
492   /**
493    * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
494    * peers connected via DV (extended neighborhood).  Does ALSO
495    * include any peers that are in 'direct_neighbors'; for those
496    * peers, the cost will be zero and the referrer all zeros.
497    */
498   struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
499
500   /**
501    * We use the min heap (min refers to cost) to prefer
502    * gossipping about peers with small costs.
503    */
504   struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
505
506   /**
507    * We use the max heap (max refers to cost) for general
508    * iterations over all peers and to remove the most costly
509    * connection if we have too many.
510    */
511   struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
512
513   unsigned long long fisheye_depth;
514
515   unsigned long long max_table_size;
516
517   unsigned int neighbor_id_loc;
518
519   int closing;
520
521 };
522
523 static struct GNUNET_DV_Context ctx;
524
525 struct FindDestinationContext
526 {
527   unsigned int tid;
528   struct DistantNeighbor *dest;
529 };
530
531 struct DisconnectContext
532 {
533   /**
534    * Distant neighbor to get pid from.
535    */
536   struct DistantNeighbor *distant;
537
538   /**
539    * Direct neighbor that disconnected.
540    */
541   struct DirectNeighbor *direct;
542 };
543
544 /**
545  * We've been given a target ID based on the random numbers that
546  * we assigned to our DV-neighborhood.  Find the entry for the
547  * respective neighbor.
548  */
549 static int
550 find_destination (void *cls,
551                   struct GNUNET_CONTAINER_HeapNode *node,
552                   void *element, GNUNET_CONTAINER_HeapCostType cost)
553 {
554   struct FindDestinationContext *fdc = cls;
555   struct DistantNeighbor *dn = element;
556
557   if (fdc->tid != dn->our_id)
558     return GNUNET_YES;
559   fdc->dest = dn;
560   return GNUNET_NO;
561 }
562
563 /**
564  * Find a distant peer whose referrer_id matches what we're
565  * looking for.  For looking up a peer we've gossipped about
566  * but is now disconnected.  Need to do this because we don't
567  * want to remove those that may be accessible via a different
568  * route.
569  */
570 static int find_distant_peer (void *cls,
571                               const GNUNET_HashCode * key,
572                               void *value)
573 {
574   struct FindDestinationContext *fdc = cls;
575   struct DistantNeighbor *distant = value;
576
577   if (fdc->tid == distant->referrer_id)
578     {
579       fdc->dest = distant;
580       return GNUNET_NO;
581     }
582   return GNUNET_YES;
583 }
584
585 /**
586  * Function called to notify a client about the socket
587  * begin ready to queue more data.  "buf" will be
588  * NULL and "size" zero if the socket was closed for
589  * writing in the meantime.
590  *
591  * @param cls closure
592  * @param size number of bytes available in buf
593  * @param buf where the callee should write the message
594  * @return number of bytes written to buf
595  */
596 size_t transmit_to_plugin (void *cls,
597                            size_t size, void *buf)
598 {
599   char *cbuf = buf;
600   struct PendingMessage *reply;
601   size_t off;
602   size_t msize;
603
604   if (buf == NULL)
605     {
606       /* client disconnected */
607 #if DEBUG_DV
608       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
609 #endif
610       return 0;
611     }
612   plugin_transmit_handle = NULL;
613   off = 0;
614   while ( (NULL != (reply = plugin_pending_head)) &&
615           (size >= off + (msize = ntohs (reply->msg->size))))
616     {
617 #if DEBUG_DV
618     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (plugin) called with size %d\n", "dv service", msize);
619 #endif
620       GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
621                                    plugin_pending_tail,
622                                    reply);
623       memcpy (&cbuf[off], reply->msg, msize);
624       GNUNET_free (reply);
625       off += msize;
626     }
627
628   if (plugin_pending_head != NULL)
629     plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
630                                                                   ntohs(plugin_pending_head->msg->size),
631                                                                   GNUNET_TIME_UNIT_FOREVER_REL,
632                                                                   &transmit_to_plugin, NULL);
633
634   return off;
635 }
636
637 /**
638  * Send a message to the dv plugin.
639  *
640  * @param sender the direct sender of the message
641  * @param message the message to send to the plugin
642  *        (may be an encapsulated type)
643  * @param message_size the size of the message to be sent
644  * @param distant_neighbor the original sender of the message
645  * @param cost the cost to the original sender of the message
646  */
647 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
648                     const struct GNUNET_MessageHeader *message,
649                     size_t message_size,
650                     struct GNUNET_PeerIdentity *distant_neighbor,
651                     size_t cost)
652 {
653   struct GNUNET_DV_MessageReceived *received_msg;
654   struct PendingMessage *pending_message;
655 #if DEBUG_DV
656   struct GNUNET_MessageHeader * packed_message_header;
657   struct GNUNET_HELLO_Message *hello_msg;
658   struct GNUNET_PeerIdentity hello_identity;
659 #endif
660   char *sender_address;
661   size_t sender_address_len;
662   char *packed_msg_start;
663   int size;
664
665 #if DEBUG_DV
666   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
667 #endif
668
669   if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
670   {
671     sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
672     sender_address = GNUNET_malloc(sender_address_len);
673     memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
674     memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
675   }
676   else
677   {
678     sender_address_len = sizeof(struct GNUNET_PeerIdentity);
679     sender_address = GNUNET_malloc(sender_address_len);
680     memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
681   }
682
683   size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
684   received_msg = GNUNET_malloc(size);
685   received_msg->header.size = htons(size);
686   received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
687   received_msg->sender_address_len = htonl(sender_address_len);
688   received_msg->distance = htonl(cost);
689   received_msg->msg_len = htonl(message_size);
690   /* Set the sender in this message to be the original sender! */
691   memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
692   /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
693   memcpy(&received_msg[1], sender_address, sender_address_len);
694   GNUNET_free(sender_address);
695   /* Copy the actual message after the sender */
696   packed_msg_start = (char *)&received_msg[1];
697   packed_msg_start = &packed_msg_start[sender_address_len];
698   memcpy(packed_msg_start, message, message_size);
699 #if DEBUG_DV
700   packed_message_header = (struct GNUNET_MessageHeader *)packed_msg_start;
701   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));
702   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dv packed message len %d, type %d\n", ntohs(packed_message_header->size), ntohs(packed_message_header->type));
703   if (ntohs(packed_message_header->type) == GNUNET_MESSAGE_TYPE_HELLO)
704   {
705     hello_msg = (struct GNUNET_HELLO_Message *)packed_message_header;
706     GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(hello_msg, &hello_identity));
707     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packed HELLO message is about peer %s\n", GNUNET_i2s(&hello_identity));
708   }
709 #endif
710   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
711   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
712   memcpy(&pending_message[1], received_msg, size);
713   GNUNET_free(received_msg);
714
715   GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
716
717   if (client_handle != NULL)
718     {
719       if (plugin_transmit_handle == NULL)
720         {
721           plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
722                                                                         size, GNUNET_TIME_UNIT_FOREVER_REL,
723                                                                         &transmit_to_plugin, NULL);
724         }
725       else
726         {
727           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
728         }
729     }
730 }
731
732
733 /**
734  * Function called to notify a client about the socket
735  * being ready to queue more data.  "buf" will be
736  * NULL and "size" zero if the socket was closed for
737  * writing in the meantime.
738  *
739  * @param cls closure
740  * @param size number of bytes available in buf
741  * @param buf where the callee should write the message
742  * @return number of bytes written to buf
743  */
744 size_t core_transmit_notify (void *cls,
745                              size_t size, void *buf)
746 {
747   char *cbuf = buf;
748   struct PendingMessage *reply;
749   struct PendingMessage *client_reply;
750   size_t off;
751   size_t msize;
752
753   if (buf == NULL)
754     {
755       /* client disconnected */
756 #if DEBUG_DV
757       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
758 #endif
759       return 0;
760     }
761
762   core_transmit_handle = NULL;
763   off = 0;
764   while ( (NULL != (reply = core_pending_head)) &&
765           (size >= off + (msize = ntohs (reply->msg->size))))
766     {
767 #if DEBUG_DV
768     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
769 #endif
770       GNUNET_CONTAINER_DLL_remove (core_pending_head,
771                                    core_pending_tail,
772                                    reply);
773       if (reply->send_result != NULL) /* Will only be non-null if a real client asked for this send */
774         {
775           client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
776           client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
777           memcpy(&client_reply[1], reply->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
778           GNUNET_free(reply->send_result);
779
780           GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
781           if (client_handle != NULL)
782             {
783               if (plugin_transmit_handle == NULL)
784                 {
785                   plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
786                                                                                 sizeof(struct GNUNET_DV_SendResultMessage),
787                                                                                 GNUNET_TIME_UNIT_FOREVER_REL,
788                                                                                 &transmit_to_plugin, NULL);
789                 }
790               else
791                 {
792                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
793                 }
794             }
795         }
796       memcpy (&cbuf[off], reply->msg, msize);
797       GNUNET_free (reply);
798       off += msize;
799     }
800   return off;
801 }
802
803
804 /**
805  * Send a DV data message via DV.
806  *
807  * @param sender the original sender of the message
808  * @param specific_neighbor the specific DistantNeighbor to use, complete with referrer!
809  * @param send_context the send context
810  */
811 static int
812 send_message_via (const struct GNUNET_PeerIdentity * sender,
813               const struct DistantNeighbor * specific_neighbor,
814               struct DV_SendContext *send_context)
815 {
816   p2p_dv_MESSAGE_Data *toSend;
817   unsigned int msg_size;
818   unsigned int cost;
819   unsigned int recipient_id;
820   unsigned int sender_id;
821   struct DistantNeighbor *source;
822   struct PendingMessage *pending_message;
823 #if DEBUG_DV
824   char shortname[5];
825 #endif
826
827   msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
828
829   if (specific_neighbor == NULL)
830     {
831       /* target unknown to us, drop! */
832       return GNUNET_SYSERR;
833     }
834   recipient_id = specific_neighbor->referrer_id;
835
836   source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
837                                       &sender->hashPubKey);
838   if (source == NULL)
839     {
840       if (0 != (memcmp (&my_identity,
841                         sender, sizeof (struct GNUNET_PeerIdentity))))
842         {
843           /* sender unknown to us, drop! */
844           return GNUNET_SYSERR;
845         }
846       sender_id = 0;            /* 0 == us */
847     }
848   else
849     {
850       /* find out the number that we use when we gossip about
851          the sender */
852       sender_id = source->our_id;
853     }
854
855   cost = specific_neighbor->cost;
856   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
857   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
858   pending_message->send_result = send_context->send_result;
859   toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
860   toSend->header.size = htons (msg_size);
861   toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
862   toSend->sender = htonl (sender_id);
863   toSend->recipient = htonl (recipient_id);
864   memcpy (&toSend[1], send_context->message, send_context->message_size);
865
866 #if DEBUG_DV
867   memcpy(&shortname, GNUNET_i2s(&specific_neighbor->identity), 4);
868   shortname[4] = '\0';
869   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send to destination `%s' via `%s' size %u\n", "DV", &shortname, GNUNET_i2s(&specific_neighbor->referrer->identity), msg_size);
870 #endif
871
872   GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
873                                      core_pending_tail,
874                                      core_pending_tail,
875                                      pending_message);
876   if (core_transmit_handle == NULL)
877     core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, send_context->importance, send_context->timeout, &specific_neighbor->referrer->identity, msg_size, &core_transmit_notify, NULL);
878   else
879     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
880
881   return (int) cost;
882 }
883
884
885 /**
886  * Send a DV data message via DV.
887  *
888  * @param recipient the ultimate recipient of this message
889  * @param sender the original sender of the message
890  * @param specific_neighbor the specific neighbor to send this message via
891  * @param message the packed message
892  * @param message_size size of the message
893  * @param importance what priority to send this message with
894  * @param timeout how long to possibly delay sending this message
895  */
896 static int
897 send_message (const struct GNUNET_PeerIdentity * recipient,
898               const struct GNUNET_PeerIdentity * sender,
899               const struct DistantNeighbor * specific_neighbor,
900               const struct GNUNET_MessageHeader * message,
901               size_t message_size,
902               unsigned int importance, struct GNUNET_TIME_Relative timeout)
903 {
904   p2p_dv_MESSAGE_Data *toSend;
905   unsigned int msg_size;
906   unsigned int cost;
907   unsigned int recipient_id;
908   unsigned int sender_id;
909   struct DistantNeighbor *target;
910   struct DistantNeighbor *source;
911   struct PendingMessage *pending_message;
912
913   msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
914
915   target = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
916                                               &recipient->hashPubKey);
917   if (target == NULL)
918     {
919       /* target unknown to us, drop! */
920       return GNUNET_SYSERR;
921     }
922   recipient_id = target->referrer_id;
923
924   source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
925                                       &sender->hashPubKey);
926   if (source == NULL)
927     {
928       if (0 != (memcmp (&my_identity,
929                         sender, sizeof (struct GNUNET_PeerIdentity))))
930         {
931           /* sender unknown to us, drop! */
932           return GNUNET_SYSERR;
933         }
934       sender_id = 0;            /* 0 == us */
935     }
936   else
937     {
938       /* find out the number that we use when we gossip about
939          the sender */
940       sender_id = source->our_id;
941     }
942
943   cost = target->cost;
944   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
945   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
946   pending_message->send_result = NULL;
947   toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
948   toSend->header.size = htons (msg_size);
949   toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
950   toSend->sender = htonl (sender_id);
951   toSend->recipient = htonl (recipient_id);
952   memcpy (&toSend[1], message, message_size);
953
954   GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
955                                      core_pending_tail,
956                                      core_pending_tail,
957                                      pending_message);
958 #if DEBUG_DV
959   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));
960 #endif
961   if (core_transmit_handle == NULL)
962     core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
963
964   return (int) cost;
965 }
966
967
968 /**
969  * Core handler for dv data messages.  Whatever this message
970  * contains all we really have to do is rip it out of its
971  * DV layering and give it to our pal the DV plugin to report
972  * in with.
973  *
974  * @param cls closure
975  * @param peer peer which sent the message (immediate sender)
976  * @param message the message
977  * @param latency the latency of the connection we received the message from
978  * @param distance the distance to the immediate peer
979  */
980 static int handle_dv_data_message (void *cls,
981                              const struct GNUNET_PeerIdentity * peer,
982                              const struct GNUNET_MessageHeader * message,
983                              struct GNUNET_TIME_Relative latency,
984                              uint32_t distance)
985 {
986   const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
987   const struct GNUNET_MessageHeader *packed_message;
988   struct DirectNeighbor *dn;
989   struct DistantNeighbor *pos;
990   unsigned int sid;             /* Sender id */
991   unsigned int tid;             /* Target id */
992   struct GNUNET_PeerIdentity original_sender;
993   struct GNUNET_PeerIdentity destination;
994   struct FindDestinationContext fdc;
995   int ret;
996   size_t packed_message_size;
997   char *cbuf;
998   size_t offset;
999
1000   packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1001
1002 #if DEBUG_DV
1003   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004               "%s: Receives %s message size %d, packed message size %d!\n", "dv", "DV DATA", ntohs(incoming->header.size), packed_message_size);
1005 #endif
1006   if (ntohs (incoming->header.size) <  sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1007     {
1008 #if DEBUG_DV
1009   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010               "`%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));
1011 #endif
1012       return GNUNET_SYSERR;
1013     }
1014
1015   dn = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1016                                   &peer->hashPubKey);
1017   if (dn == NULL)
1018     {
1019 #if DEBUG_DV
1020       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021                   "%s: dn NULL!\n", "dv");
1022 #endif
1023       return GNUNET_OK;
1024     }
1025   sid = ntohl (incoming->sender);
1026   pos = dn->referee_head;
1027   while ((NULL != pos) && (pos->referrer_id != sid))
1028     pos = pos->next;
1029   if (pos == NULL)
1030     {
1031 #if DEBUG_DV
1032       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033                   "%s peer %s: unknown sender (%d)!\n", "DV SERVICE", GNUNET_i2s(&my_identity), ntohl(incoming->sender), GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors));
1034 #endif
1035       /* unknown sender */
1036       return GNUNET_OK;
1037     }
1038   original_sender = pos->identity;
1039   tid = ntohl (incoming->recipient);
1040   if (tid == 0)
1041     {
1042       /* 0 == us */
1043
1044       cbuf = (char *)&incoming[1];
1045       offset = 0;
1046       while(offset < packed_message_size)
1047         {
1048           packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1049 #if DEBUG_DV
1050           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051                       "%s: Receives %s message for me, size %d type %d, cost %u!\n", "dv", "DV DATA", ntohs(packed_message->size), ntohs(packed_message->type), pos->cost);
1052 #endif
1053           GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1054           GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1055           if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1056               (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1057           {
1058             send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
1059           }
1060           offset += ntohs(packed_message->size);
1061         }
1062
1063       return GNUNET_OK;
1064     }
1065   else
1066     {
1067       packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1068     }
1069
1070   /* FIXME: this is the *only* per-request operation we have in DV
1071      that is O(n) in relation to the number of connected peers; a
1072      hash-table lookup could easily solve this (minor performance
1073      issue) */
1074   fdc.tid = tid;
1075   fdc.dest = NULL;
1076   GNUNET_CONTAINER_heap_iterate (ctx.neighbor_max_heap,
1077                                  &find_destination, &fdc);
1078
1079 #if DEBUG_DV
1080       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081                   "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1082 #endif
1083
1084   if (fdc.dest == NULL)
1085     {
1086       return GNUNET_OK;
1087     }
1088   destination = fdc.dest->identity;
1089
1090   if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1091     {
1092       /* FIXME: create stat: routing loop-discard! */
1093 #if DEBUG_DV
1094       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1095 #endif
1096       return GNUNET_OK;
1097     }
1098
1099   /* At this point we have a message, and we need to forward it on to the
1100    * next DV hop.
1101    */
1102   /* FIXME: Can't send message on, we have to behave.
1103    * We have to tell core we have a message for the next peer, and let
1104    * transport do transport selection on how to get this message to 'em */
1105   /*ret = send_message (&destination,
1106                       &original_sender,
1107                       packed_message, DV_PRIORITY, DV_DELAY);*/
1108 #if DEBUG_DV
1109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110               "%s: Sends message size %d on!\n", "dv", packed_message_size);
1111 #endif
1112   ret = send_message(&destination, &original_sender, NULL, packed_message, packed_message_size, default_dv_priority, GNUNET_TIME_relative_get_forever());
1113
1114   if (ret != GNUNET_SYSERR)
1115     return GNUNET_OK;
1116   else
1117     return GNUNET_SYSERR;
1118 }
1119
1120
1121 /**
1122  *  Scheduled task which gossips about known direct peers to other connected
1123  *  peers.  Will run until called with reason shutdown.
1124  */
1125 static void
1126 neighbor_send_task (void *cls,
1127                     const struct GNUNET_SCHEDULER_TaskContext *tc)
1128 {
1129   struct NeighborSendContext *send_context = cls;
1130 #if DEBUG_DV_GOSSIP_SEND
1131   char * encPeerAbout;
1132   char * encPeerTo;
1133 #endif
1134   struct DistantNeighbor *about;
1135   struct DirectNeighbor *to;
1136   struct FastGossipNeighborList *about_list;
1137
1138   p2p_dv_MESSAGE_NeighborInfo *message;
1139   struct PendingMessage *pending_message;
1140
1141   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1142   {
1143 #if DEBUG_DV_GOSSIP
1144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145               "%s: Called with reason shutdown, shutting down!\n",
1146               GNUNET_i2s(&my_identity));
1147 #endif
1148     send_context->task = GNUNET_SCHEDULER_NO_TASK;
1149     return;
1150   }
1151
1152   if (send_context->fast_gossip_list_head != NULL)
1153     {
1154       about_list = send_context->fast_gossip_list_head;
1155       about = send_context->fast_gossip_list_head->about;
1156       GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1157                                   send_context->fast_gossip_list_tail,
1158                                   about_list);
1159       GNUNET_free(about_list);
1160     }
1161   else
1162     {
1163       /* FIXME: this may become a problem, because the heap walk has only one internal "walker".  This means
1164        * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1165        * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1166        * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1167        * part of the walk_get_next call.  Then the heap would have to keep a list of walks, or reset the walk
1168        * whenever a modification has been detected.  Yuck either way.  Perhaps we could iterate over the heap
1169        * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1170        * in the mean time that becomes nasty.  For now we'll just assume that the walking is done
1171        * asynchronously enough to avoid major problems (-;
1172        */
1173       about = GNUNET_CONTAINER_heap_walk_get_next (ctx.neighbor_min_heap);
1174     }
1175   to = send_context->toNeighbor;
1176
1177   if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1178 #if SUPPORT_HIDING
1179       (about->hidden == GNUNET_NO) &&
1180 #endif
1181       (to != NULL) &&
1182       (0 != memcmp (&about->identity,
1183                         &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1184       (about->pkey != NULL))
1185     {
1186 #if DEBUG_DV_GOSSIP_SEND
1187       encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1188       encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1189       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190                   "%s: Sending info about peer %s to directly connected peer %s\n",
1191                   GNUNET_i2s(&my_identity),
1192                   encPeerAbout, encPeerTo);
1193       GNUNET_free(encPeerAbout);
1194       GNUNET_free(encPeerTo);
1195 #endif
1196       pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1197       pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1198       message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1199       message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1200       message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1201       message->cost = htonl (about->cost);
1202       message->neighbor_id = htonl (about->our_id);
1203
1204       memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1205       memcpy (&message->neighbor,
1206               &about->identity, sizeof (struct GNUNET_PeerIdentity));
1207
1208       GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1209                                          core_pending_tail,
1210                                          core_pending_tail,
1211                                          pending_message);
1212
1213       if (core_transmit_handle == NULL)
1214         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);
1215
1216     }
1217
1218   if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1219     {
1220       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SERVICE: still in fast send mode\n");
1221       send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1222     }
1223   else
1224     {
1225       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SERVICE: entering slow send mode\n");
1226       send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1227     }
1228
1229   return;
1230 }
1231
1232
1233 /**
1234  * Handle START-message.  This is the first message sent to us
1235  * by the client (can only be one!).
1236  *
1237  * @param cls closure (always NULL)
1238  * @param client identification of the client
1239  * @param message the actual message
1240  */
1241 static void
1242 handle_start (void *cls,
1243               struct GNUNET_SERVER_Client *client,
1244               const struct GNUNET_MessageHeader *message)
1245 {
1246
1247 #if DEBUG_DV
1248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249               "Received `%s' request from client\n", "START");
1250 #endif
1251
1252   client_handle = client;
1253
1254   GNUNET_SERVER_client_keep(client_handle);
1255   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1256 }
1257
1258
1259 /**
1260  * Iterate over hash map entries for a distant neighbor,
1261  * if direct neighbor matches context call send message
1262  *
1263  * @param cls closure, a DV_SendContext
1264  * @param key current key code
1265  * @param value value in the hash map
1266  * @return GNUNET_YES if we should continue to
1267  *         iterate,
1268  *         GNUNET_NO if not.
1269  */
1270 int send_iterator (void *cls,
1271                    const GNUNET_HashCode * key,
1272                    void *value)
1273 {
1274   struct DV_SendContext *send_context = cls;
1275   struct DistantNeighbor *distant_neighbor = value;
1276
1277   if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1278     {
1279       send_message_via(&my_identity, distant_neighbor, send_context);
1280       return GNUNET_NO;
1281     }
1282   return GNUNET_YES;
1283 }
1284
1285 /**
1286  * Service server's handler for message send requests (which come
1287  * bubbling up to us through the DV plugin).
1288  *
1289  * @param cls closure
1290  * @param client identification of the client
1291  * @param message the actual message
1292  */
1293 void handle_dv_send_message (void *cls,
1294                              struct GNUNET_SERVER_Client * client,
1295                              const struct GNUNET_MessageHeader * message)
1296 {
1297   struct GNUNET_DV_SendMessage *send_msg;
1298   struct GNUNET_DV_SendResultMessage *send_result_msg;
1299   struct PendingMessage *pending_message;
1300   size_t address_len;
1301   size_t message_size;
1302   struct GNUNET_PeerIdentity *destination;
1303   struct GNUNET_PeerIdentity *direct;
1304   struct GNUNET_MessageHeader *message_buf;
1305   char *temp_pos;
1306   int offset;
1307   static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1308   struct DV_SendContext *send_context;
1309
1310   if (client_handle == NULL)
1311   {
1312     client_handle = client;
1313     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314               "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1315   }
1316   else if (client_handle != client)
1317   {
1318     client_handle = client;
1319     /* What should we do in this case, assert fail or just log the warning? */
1320     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1321                 "%s: Setting client handle (was a different client!)!\n", "dv");
1322   }
1323
1324   GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1325   send_msg = (struct GNUNET_DV_SendMessage *)message;
1326
1327   address_len = ntohl(send_msg->addrlen);
1328   GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1329   message_size = ntohl(send_msg->msgbuf_size);
1330
1331   GNUNET_assert(ntohs(message->size) == sizeof(struct GNUNET_DV_SendMessage) + address_len + message_size);
1332   destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1333   direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1334   message_buf = GNUNET_malloc(message_size);
1335
1336   temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1337   offset = 0; /* Offset starts at zero */
1338
1339   memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1340   offset += sizeof(struct GNUNET_PeerIdentity);
1341
1342   memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1343   offset += sizeof(struct GNUNET_PeerIdentity);
1344
1345
1346   memcpy(message_buf, &temp_pos[offset], message_size);
1347   if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1348     {
1349       GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1350       dest_hash.encoding[4] = '\0';
1351       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);
1352     }
1353
1354 #if DEBUG_DV
1355   GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1356   dest_hash.encoding[4] = '\0';
1357   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SEND called with message of size %d type %d, destination `%s' via `%s'\n", message_size, ntohs(message_buf->type), (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1358 #endif
1359   send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1360
1361   send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1362   send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1363   send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1364   send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1365
1366   send_context->importance = ntohl(send_msg->priority);
1367   send_context->timeout = send_msg->timeout;
1368   send_context->direct_peer = direct;
1369   send_context->distant_peer = destination;
1370   send_context->message = message_buf;
1371   send_context->message_size = message_size;
1372   send_context->send_result = send_result_msg;
1373
1374   /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1375   if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1376     {
1377       send_result_msg->result = htons(1);
1378       pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1379       pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1380       memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1381       GNUNET_free(send_result_msg);
1382
1383       GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1384
1385       if (client_handle != NULL)
1386         {
1387           if (plugin_transmit_handle == NULL)
1388             {
1389               plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1390                                                                             sizeof(struct GNUNET_DV_SendResultMessage),
1391                                                                             GNUNET_TIME_UNIT_FOREVER_REL,
1392                                                                             &transmit_to_plugin, NULL);
1393             }
1394           else
1395             {
1396               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1397             }
1398         }
1399       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SEND failed to send message to destination `%s' via `%s'\n", (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1400     }
1401   else
1402     {
1403
1404     }
1405
1406   GNUNET_free(message_buf);
1407   GNUNET_free(send_context);
1408   GNUNET_free(direct);
1409   GNUNET_free(destination);
1410
1411   GNUNET_SERVER_receive_done(client, GNUNET_OK);
1412 }
1413
1414 /** Forward declarations **/
1415 static int handle_dv_gossip_message (void *cls,
1416                                      const struct GNUNET_PeerIdentity *peer,
1417                                      const struct GNUNET_MessageHeader *message,
1418                                      struct GNUNET_TIME_Relative latency,
1419                                      uint32_t distance);
1420
1421 static int handle_dv_disconnect_message (void *cls,
1422                                          const struct GNUNET_PeerIdentity *peer,
1423                                          const struct GNUNET_MessageHeader *message,
1424                                          struct GNUNET_TIME_Relative latency,
1425                                          uint32_t distance);
1426 /** End forward declarations **/
1427
1428
1429 /**
1430  * List of handlers for the messages understood by this
1431  * service.
1432  *
1433  * Hmm... will we need to register some handlers with core and
1434  * some handlers with our server here?  Because core should be
1435  * getting the incoming DV messages (from whichever lower level
1436  * transport) and then our server should be getting messages
1437  * from the dv_plugin, right?
1438  */
1439 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1440   {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1441   {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1442   {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1443   {NULL, 0, 0}
1444 };
1445
1446 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1447   {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1448   {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1449   {NULL, NULL, 0, 0}
1450 };
1451
1452 /**
1453  * Free a DistantNeighbor node, including removing it
1454  * from the referer's list.
1455  */
1456 static void
1457 distant_neighbor_free (struct DistantNeighbor *referee)
1458 {
1459   struct DirectNeighbor *referrer;
1460
1461   referrer = referee->referrer;
1462   if (referrer != NULL)
1463     {
1464       GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1465                          referrer->referee_tail, referee);
1466     }
1467   GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_max_heap, referee->max_loc);
1468   GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_min_heap, referee->min_loc);
1469   GNUNET_CONTAINER_multihashmap_remove_all (ctx.extended_neighbors,
1470                                     &referee->identity.hashPubKey);
1471   GNUNET_free (referee->pkey);
1472   GNUNET_free (referee);
1473 }
1474
1475 /**
1476  * Free a DirectNeighbor node, including removing it
1477  * from the referer's list.
1478  */
1479 static void
1480 direct_neighbor_free (struct DirectNeighbor *direct)
1481 {
1482   struct NeighborSendContext *send_context;
1483   struct FastGossipNeighborList *about_list;
1484   struct FastGossipNeighborList *prev_about;
1485
1486   send_context = direct->send_context;
1487
1488   if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1489     GNUNET_SCHEDULER_cancel(sched, send_context->task);
1490
1491   about_list = send_context->fast_gossip_list_head;
1492   while (about_list != NULL)
1493     {
1494       GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
1495       prev_about = about_list;
1496       about_list = about_list->next;
1497       GNUNET_free(prev_about);
1498     }
1499   GNUNET_free(send_context);
1500   GNUNET_free(direct);
1501 }
1502
1503 /**
1504  * Multihashmap iterator for sending out disconnect messages
1505  * for a peer.
1506  *
1507  * @param cls the peer that was disconnected
1508  * @param key key value stored under
1509  * @param value the direct neighbor to send disconnect to
1510  *
1511  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1512  */
1513 static int schedule_disconnect_messages (void *cls,
1514                                     const GNUNET_HashCode * key,
1515                                     void *value)
1516 {
1517   struct DisconnectContext *disconnect_context = cls;
1518   struct DirectNeighbor *disconnected = disconnect_context->direct;
1519   struct DirectNeighbor *notify = value;
1520   struct PendingMessage *pending_message;
1521   p2p_dv_MESSAGE_Disconnect *disconnect_message;
1522
1523   if (memcmp(&notify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1524     return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
1525
1526   pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
1527   pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1528   disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
1529   disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
1530   disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
1531   disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
1532
1533   GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1534                                      core_pending_tail,
1535                                      core_pending_tail,
1536                                      pending_message);
1537
1538   if (core_transmit_handle == NULL)
1539     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);
1540
1541   return GNUNET_YES;
1542 }
1543
1544 /**
1545  * Multihashmap iterator for freeing extended neighbors.
1546  *
1547  * @param cls NULL
1548  * @param key key value stored under
1549  * @param value the distant neighbor to be freed
1550  *
1551  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1552  */
1553 static int free_extended_neighbors (void *cls,
1554                                     const GNUNET_HashCode * key,
1555                                     void *value)
1556 {
1557   struct DistantNeighbor *distant = value;
1558   distant_neighbor_free(distant);
1559   return GNUNET_YES;
1560 }
1561
1562 /**
1563  * Multihashmap iterator for freeing direct neighbors.
1564  *
1565  * @param cls NULL
1566  * @param key key value stored under
1567  * @param value the direct neighbor to be freed
1568  *
1569  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1570  */
1571 static int free_direct_neighbors (void *cls,
1572                                     const GNUNET_HashCode * key,
1573                                     void *value)
1574 {
1575   struct DirectNeighbor *direct = value;
1576   direct_neighbor_free(direct);
1577   return GNUNET_YES;
1578 }
1579
1580 /**
1581  * Task run during shutdown.
1582  *
1583  * @param cls unused
1584  * @param tc unused
1585  */
1586 static void
1587 shutdown_task (void *cls,
1588                const struct GNUNET_SCHEDULER_TaskContext *tc)
1589 {
1590 #if DEBUG_DV
1591   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
1592 #endif
1593   GNUNET_CONTAINER_multihashmap_iterate(ctx.extended_neighbors, &free_extended_neighbors, NULL);
1594   GNUNET_CONTAINER_multihashmap_destroy(ctx.extended_neighbors);
1595   GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &free_direct_neighbors, NULL);
1596   GNUNET_CONTAINER_multihashmap_destroy(ctx.direct_neighbors);
1597
1598   GNUNET_CONTAINER_heap_destroy(ctx.neighbor_max_heap);
1599   GNUNET_CONTAINER_heap_destroy(ctx.neighbor_min_heap);
1600
1601   GNUNET_CORE_disconnect (coreAPI);
1602   GNUNET_PEERINFO_disconnect(peerinfo_handle);
1603 #if DEBUG_DV
1604   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
1605 #endif
1606 }
1607
1608 /**
1609  * To be called on core init/fail.
1610  */
1611 void core_init (void *cls,
1612                 struct GNUNET_CORE_Handle * server,
1613                 const struct GNUNET_PeerIdentity *identity,
1614                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
1615 {
1616
1617   if (server == NULL)
1618     {
1619       GNUNET_SCHEDULER_cancel(sched, cleanup_task);
1620       GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
1621       return;
1622     }
1623 #if DEBUG_DV
1624   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625               "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
1626 #endif
1627   memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
1628   coreAPI = server;
1629 }
1630
1631 /**
1632  * Iterator over hash map entries.
1633  *
1634  * @param cls closure
1635  * @param key current key code
1636  * @param value value in the hash map
1637  * @return GNUNET_YES if we should continue to
1638  *         iterate,
1639  *         GNUNET_NO if not.
1640  */
1641 static int add_pkey_to_extended (void *cls,
1642                                  const GNUNET_HashCode * key,
1643                                  void *value)
1644 {
1645   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
1646   struct DistantNeighbor *distant_neighbor = value;
1647
1648   if (distant_neighbor->pkey == NULL)
1649   {
1650     distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1651     memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1652   }
1653
1654   return GNUNET_YES;
1655 }
1656
1657 /**
1658  * Iterator over hash map entries.
1659  *
1660  * @param cls closure
1661  * @param key current key code
1662  * @param value value in the hash map
1663  * @return GNUNET_YES if we should continue to
1664  *         iterate,
1665  *         GNUNET_NO if not.
1666  */
1667 static int update_matching_neighbors (void *cls,
1668                                       const GNUNET_HashCode * key,
1669                                       void *value)
1670 {
1671   struct NeighborUpdateInfo * update_info = cls;
1672   struct DistantNeighbor *distant_neighbor = value;
1673
1674   if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
1675   {
1676     /* same referrer, cost change! */
1677     GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_max_heap,
1678                                        update_info->neighbor->max_loc, update_info->cost);
1679     GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_min_heap,
1680                                        update_info->neighbor->min_loc, update_info->cost);
1681     update_info->neighbor->last_activity = update_info->now;
1682     update_info->neighbor->cost = update_info->cost;
1683     return GNUNET_NO;
1684   }
1685
1686   return GNUNET_YES;
1687 }
1688
1689
1690 #if DEBUG_DV_GOSSIP
1691 /**
1692  * Iterator over hash map entries.
1693  *
1694  * @param cls closure (NULL)
1695  * @param key current key code
1696  * @param value value in the hash map (DistantNeighbor)
1697  * @return GNUNET_YES if we should continue to
1698  *         iterate,
1699  *         GNUNET_NO if not.
1700  */
1701 int print_neighbors (void *cls,
1702                      const GNUNET_HashCode * key,
1703                      void *value)
1704 {
1705   struct DistantNeighbor *distant_neighbor = value;
1706   char my_shortname[5];
1707   char referrer_shortname[5];
1708   memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1709   my_shortname[4] = '\0';
1710   memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1711   referrer_shortname[4] = '\0';
1712
1713   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' %s: Peer `%s', distance %d, referrer `%s'\n", &my_shortname, "DV", GNUNET_i2s(&distant_neighbor->identity), distant_neighbor->cost, &referrer_shortname);
1714   return GNUNET_YES;
1715 }
1716
1717 #endif
1718
1719 /**
1720  * Handles when a peer is either added due to being newly connected
1721  * or having been gossiped about, also called when the cost for a neighbor
1722  * needs to be updated.
1723  *
1724  * @param peer identity of the peer whose info is being added/updated
1725  * @param pkey public key of the peer whose info is being added/updated
1726  * @param referrer_peer_id id to use when sending to 'peer'
1727  * @param referrer if this is a gossiped peer, who did we hear it from?
1728  * @param cost the cost of communicating with this peer via 'referrer'
1729  */
1730 static void
1731 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
1732                    unsigned int referrer_peer_id,
1733                    struct DirectNeighbor *referrer, unsigned int cost)
1734 {
1735   struct DistantNeighbor *neighbor;
1736   struct DistantNeighbor *max;
1737   struct GNUNET_TIME_Absolute now;
1738   struct NeighborUpdateInfo *neighbor_update;
1739   unsigned int our_id;
1740
1741   now = GNUNET_TIME_absolute_get ();
1742   our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, RAND_MAX - 1) + 1;
1743
1744   neighbor = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
1745                                                 &peer->hashPubKey);
1746   neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
1747   neighbor_update->neighbor = neighbor;
1748   neighbor_update->cost = cost;
1749   neighbor_update->now = now;
1750   neighbor_update->referrer = referrer;
1751
1752   /* Either we do not know this peer, or we already do but via a different immediate peer */
1753   if ((neighbor == NULL) ||
1754       (GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
1755                                                   &peer->hashPubKey,
1756                                                   &update_matching_neighbors,
1757                                                   neighbor_update) != GNUNET_SYSERR))
1758     {
1759       /* new neighbor! */
1760       if (cost > ctx.fisheye_depth)
1761         {
1762           /* too costly */
1763           GNUNET_free(neighbor_update);
1764           return;
1765         }
1766       if (ctx.max_table_size <=
1767           GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors))
1768         {
1769           /* remove most expensive entry */
1770           max = GNUNET_CONTAINER_heap_peek (ctx.neighbor_max_heap);
1771           if (cost > max->cost)
1772             {
1773               /* new entry most expensive, don't create */
1774               GNUNET_free(neighbor_update);
1775               return;
1776             }
1777           if (max->cost > 1)
1778             {
1779               /* only free if this is not a direct connection;
1780                  we could theoretically have more direct
1781                  connections than DV entries allowed total! */
1782               distant_neighbor_free (max);
1783             }
1784         }
1785
1786       neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
1787       GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
1788                          referrer->referee_tail, neighbor);
1789       neighbor->max_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_max_heap,
1790                                                         neighbor, cost);
1791       neighbor->min_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_min_heap,
1792                                                         neighbor, cost);
1793       neighbor->referrer = referrer;
1794       memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
1795       if (pkey != NULL) /* pkey will be null on direct neighbor addition */
1796       {
1797         neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1798         memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1799       }
1800       else
1801         neighbor->pkey = pkey;
1802
1803       neighbor->last_activity = now;
1804       neighbor->cost = cost;
1805       neighbor->referrer_id = referrer_peer_id;
1806       neighbor->our_id = our_id;
1807       neighbor->hidden =
1808         (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
1809                        0) : GNUNET_NO;
1810       GNUNET_CONTAINER_multihashmap_put (ctx.extended_neighbors, &peer->hashPubKey,
1811                                  neighbor,
1812                                  GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1813     }
1814   else
1815     {
1816 #if DEBUG_DV_GOSSIP
1817       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818                   "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
1819 #endif
1820     }
1821 #if DEBUG_DV
1822     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823                 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(ctx.extended_neighbors));
1824 #endif
1825   GNUNET_free(neighbor_update);
1826
1827 }
1828
1829
1830 static size_t
1831 generate_hello_address (void *cls, size_t max, void *buf)
1832 {
1833   struct HelloContext *hello_context = cls;
1834   char *addr_buffer;
1835   size_t offset;
1836   size_t size;
1837   size_t ret;
1838
1839   if (hello_context->addresses_to_add == 0)
1840     return 0;
1841
1842   /* Hello "address" will be concatenation of distant peer and direct peer identities */
1843   size = 2 * sizeof(struct GNUNET_PeerIdentity);
1844   GNUNET_assert(max >= size);
1845
1846   addr_buffer = GNUNET_malloc(size);
1847   offset = 0;
1848   /* Copy the distant peer identity to buffer */
1849   memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
1850   offset += sizeof(struct GNUNET_PeerIdentity);
1851   /* Copy the direct peer identity to buffer */
1852   memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
1853   ret = GNUNET_HELLO_add_address ("dv",
1854                                   GNUNET_TIME_relative_to_absolute
1855                                   (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
1856                                   buf, max);
1857
1858   hello_context->addresses_to_add--;
1859
1860   GNUNET_free(addr_buffer);
1861   return ret;
1862 }
1863
1864
1865 /**
1866  * Core handler for dv disconnect messages.  These will be used
1867  * by us to tell transport via the dv plugin that a peer can
1868  * no longer be contacted by us via a certain address.  We should
1869  * then propagate these messages on, given that the distance to
1870  * the peer indicates we would have gossiped about it to others.
1871  *
1872  * @param cls closure
1873  * @param peer peer which sent the message (immediate sender)
1874  * @param message the message
1875  * @param latency the latency of the connection we received the message from
1876  * @param distance the distance to the immediate peer
1877  */
1878 static int handle_dv_disconnect_message (void *cls,
1879                                          const struct GNUNET_PeerIdentity *peer,
1880                                          const struct GNUNET_MessageHeader *message,
1881                                          struct GNUNET_TIME_Relative latency,
1882                                          uint32_t distance)
1883 {
1884   struct DirectNeighbor *referrer;
1885   struct DistantNeighbor *distant;
1886   p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
1887
1888   if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
1889     {
1890       return GNUNET_SYSERR;     /* invalid message */
1891     }
1892
1893   referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1894                                                 &peer->hashPubKey);
1895   if (referrer == NULL)
1896     return GNUNET_OK;
1897
1898   distant = referrer->referee_head;
1899   while (distant != NULL)
1900     {
1901       if (distant->referrer_id == ntohl(enc_message->peer_id))
1902         {
1903           distant_neighbor_free(distant);
1904         }
1905     }
1906
1907   return GNUNET_OK;
1908 }
1909
1910
1911 /**
1912  * Core handler for dv gossip messages.  These will be used
1913  * by us to create a HELLO message for the newly peer containing
1914  * which direct peer we can connect through, and what the cost
1915  * is.  This HELLO will then be scheduled for validation by the
1916  * transport service so that it can be used by all others.
1917  *
1918  * @param cls closure
1919  * @param peer peer which sent the message (immediate sender)
1920  * @param message the message
1921  * @param latency the latency of the connection we received the message from
1922  * @param distance the distance to the immediate peer
1923  */
1924 static int handle_dv_gossip_message (void *cls,
1925                                      const struct GNUNET_PeerIdentity *peer,
1926                                      const struct GNUNET_MessageHeader *message,
1927                                      struct GNUNET_TIME_Relative latency,
1928                                      uint32_t distance)
1929 {
1930   struct HelloContext *hello_context;
1931   struct GNUNET_HELLO_Message *hello_msg;
1932   struct DirectNeighbor *referrer;
1933   p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
1934
1935   if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
1936     {
1937       return GNUNET_SYSERR;     /* invalid message */
1938     }
1939
1940 #if DEBUG_DV_GOSSIP_RECEIPT
1941   char * encPeerAbout;
1942   char * encPeerFrom;
1943
1944   encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
1945   encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
1946   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947               "%s: Receives %s message from peer %s about peer %s distance %d!\n", "dv", "DV GOSSIP", encPeerFrom, encPeerAbout, ntohl (enc_message->cost) + 1);
1948   GNUNET_free(encPeerAbout);
1949   GNUNET_free(encPeerFrom);
1950 #endif
1951
1952   referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1953                                                 &peer->hashPubKey);
1954   if (referrer == NULL)
1955     return GNUNET_OK;
1956
1957   addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
1958                      ntohl (enc_message->neighbor_id),
1959                      referrer, ntohl (enc_message->cost) + 1);
1960
1961   hello_context = GNUNET_malloc(sizeof(struct HelloContext));
1962   hello_context->direct_peer = peer;
1963   memcpy(&hello_context->distant_peer, &enc_message->neighbor, sizeof(struct GNUNET_PeerIdentity));
1964   hello_context->addresses_to_add = 1;
1965   hello_msg = GNUNET_HELLO_create(&enc_message->pkey, &generate_hello_address, hello_context);
1966
1967   send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, ntohl(enc_message->cost) + 1);
1968   GNUNET_free(hello_context);
1969   GNUNET_free(hello_msg);
1970   return GNUNET_OK;
1971 }
1972
1973
1974 /**
1975  * Iterate over all currently known peers, add them to the
1976  * fast gossip list for this peer so we get DV routing information
1977  * out as fast as possible!
1978  *
1979  * @param cls the direct neighbor we will gossip to
1980  * @param key the hashcode of the peer
1981  * @param value the distant neighbor we should add to the list
1982  *
1983  * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
1984  */
1985 static int add_all_extended_peers (void *cls,
1986                                    const GNUNET_HashCode * key,
1987                                    void *value)
1988 {
1989   struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
1990   struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
1991   struct FastGossipNeighborList *gossip_entry;
1992
1993   if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1994     return GNUNET_YES; /* Don't gossip to a peer about itself! */
1995
1996 #if SUPPORT_HIDING
1997   if (distant->hidden == GNUNET_YES)
1998     return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
1999 #endif
2000   gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2001   gossip_entry->about = distant;
2002
2003   GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2004                                     send_context->fast_gossip_list_tail,
2005                                     send_context->fast_gossip_list_tail,
2006                                     gossip_entry);
2007
2008   return GNUNET_YES;
2009 }
2010
2011
2012 /**
2013  * Iterate over all current direct peers, add DISTANT newly connected
2014  * peer to the fast gossip list for that peer so we get DV routing
2015  * information out as fast as possible!
2016  *
2017  * @param cls the newly connected neighbor we will gossip about
2018  * @param key the hashcode of the peer
2019  * @param value the direct neighbor we should gossip to
2020  *
2021  * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2022  */
2023 static int add_distant_all_direct_neighbors (void *cls,
2024                                      const GNUNET_HashCode * key,
2025                                      void *value)
2026 {
2027   struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2028   struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2029   struct NeighborSendContext *send_context = direct->send_context;
2030   struct FastGossipNeighborList *gossip_entry;
2031
2032   if (distant == NULL)
2033     {
2034       return GNUNET_YES;
2035     }
2036
2037   if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2038     {
2039       return GNUNET_YES; /* Don't gossip to a peer about itself! */
2040     }
2041
2042 #if SUPPORT_HIDING
2043   if (distant->hidden == GNUNET_YES)
2044     return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2045 #endif
2046   gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2047   gossip_entry->about = distant;
2048
2049   GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2050                                     send_context->fast_gossip_list_tail,
2051                                     send_context->fast_gossip_list_tail,
2052                                     gossip_entry);
2053   if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2054     GNUNET_SCHEDULER_cancel(sched, send_context->task);
2055
2056   send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2057   return GNUNET_YES;
2058 }
2059
2060
2061 /**
2062  * Iterate over all current direct peers, add newly connected peer
2063  * to the fast gossip list for that peer so we get DV routing
2064  * information out as fast as possible!
2065  *
2066  * @param cls the newly connected neighbor we will gossip about
2067  * @param key the hashcode of the peer
2068  * @param value the direct neighbor we should gossip to
2069  *
2070  * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2071  */
2072 static int add_all_direct_neighbors (void *cls,
2073                                      const GNUNET_HashCode * key,
2074                                      void *value)
2075 {
2076   struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2077   struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2078   struct DistantNeighbor *distant;
2079   struct NeighborSendContext *send_context = direct->send_context;
2080   struct FastGossipNeighborList *gossip_entry;
2081
2082   distant = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &to->identity.hashPubKey);
2083   if (distant == NULL)
2084     {
2085       return GNUNET_YES;
2086     }
2087
2088   if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2089     {
2090       return GNUNET_YES; /* Don't gossip to a peer about itself! */
2091     }
2092
2093 #if SUPPORT_HIDING
2094   if (distant->hidden == GNUNET_YES)
2095     return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2096 #endif
2097   gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2098   gossip_entry->about = distant;
2099
2100   GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2101                                     send_context->fast_gossip_list_tail,
2102                                     send_context->fast_gossip_list_tail,
2103                                     gossip_entry);
2104   if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2105     GNUNET_SCHEDULER_cancel(sched, send_context->task);
2106
2107   send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2108   return GNUNET_YES;
2109 }
2110
2111
2112 static void
2113 process_peerinfo (void *cls,
2114                   const struct GNUNET_PeerIdentity *peer,
2115                   const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2116 {
2117   struct PeerIteratorContext *peerinfo_iterator = cls;
2118   struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2119
2120   if ((peer == NULL))/* && (neighbor->pkey == NULL))*/
2121     {
2122       /* FIXME: Remove peer! */
2123       GNUNET_free(peerinfo_iterator);
2124       return;
2125     }
2126
2127   if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2128     return;
2129
2130   if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2131     {
2132       GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
2133                                                  &peer->hashPubKey,
2134                                                  &add_pkey_to_extended,
2135                                                  &neighbor->pkey);
2136
2137       GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2138
2139       GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &add_all_direct_neighbors, neighbor);
2140       neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2141     }
2142 }
2143
2144
2145 /**
2146  * Method called whenever a peer connects.
2147  *
2148  * @param cls closure
2149  * @param peer peer identity this notification is about
2150  * @param latency reported latency of the connection with peer
2151  * @param distance reported distance (DV) to peer
2152  */
2153 void handle_core_connect (void *cls,
2154                           const struct GNUNET_PeerIdentity * peer,
2155                           struct GNUNET_TIME_Relative latency,
2156                           uint32_t distance)
2157 {
2158   struct DirectNeighbor *neighbor;
2159   struct DistantNeighbor *about;
2160   struct PeerIteratorContext *peerinfo_iterator;
2161 #if DEBUG_DV
2162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2163               "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2164 #endif
2165
2166   if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL))
2167   {
2168     peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2169     neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2170     neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2171     neighbor->send_context->toNeighbor = neighbor;
2172     memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2173
2174     GNUNET_CONTAINER_multihashmap_put (ctx.direct_neighbors,
2175                                &peer->hashPubKey,
2176                                neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2177     addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2178     peerinfo_iterator->neighbor = neighbor;
2179     peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2180                                                      peer,
2181                                                      0,
2182                                                      GNUNET_TIME_UNIT_FOREVER_REL,
2183                                                      &process_peerinfo,
2184                                                      peerinfo_iterator);
2185
2186     /* Only add once we get the publicKey of this guy
2187      *
2188      * neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2189      */
2190   }
2191   else
2192   {
2193     about = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &peer->hashPubKey);
2194     if ((GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2195       GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &add_distant_all_direct_neighbors, about);
2196
2197 #if DEBUG_DV
2198     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199                 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2200 #endif
2201     return;
2202   }
2203 }
2204
2205 /**
2206  * Method called whenever a given peer disconnects.
2207  *
2208  * @param cls closure
2209  * @param peer peer identity this notification is about
2210  */
2211 void handle_core_disconnect (void *cls,
2212                              const struct GNUNET_PeerIdentity * peer)
2213 {
2214   struct DirectNeighbor *neighbor;
2215   struct DistantNeighbor *referee;
2216   struct FindDestinationContext fdc;
2217   struct DisconnectContext disconnect_context;
2218 #if DEBUG_DV
2219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2220               "%s: Receives core peer disconnect message!\n", "dv");
2221 #endif
2222
2223   neighbor =
2224     GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors, &peer->hashPubKey);
2225   if (neighbor == NULL)
2226     {
2227       return;
2228     }
2229   while (NULL != (referee = neighbor->referee_head))
2230     distant_neighbor_free (referee);
2231
2232   fdc.dest = NULL;
2233   fdc.tid = 0;
2234
2235   GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &find_distant_peer, &fdc);
2236
2237   if (fdc.dest != NULL)
2238     {
2239       disconnect_context.direct = neighbor;
2240       disconnect_context.distant = fdc.dest;
2241       GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2242     }
2243
2244   GNUNET_assert (neighbor->referee_tail == NULL);
2245   GNUNET_CONTAINER_multihashmap_remove (ctx.direct_neighbors,
2246                                         &peer->hashPubKey, neighbor);
2247   if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2248     GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2249   GNUNET_free (neighbor);
2250 }
2251
2252
2253 /**
2254  * Process dv requests.
2255  *
2256  * @param cls closure
2257  * @param scheduler scheduler to use
2258  * @param server the initialized server
2259  * @param c configuration to use
2260  */
2261 static void
2262 run (void *cls,
2263      struct GNUNET_SCHEDULER_Handle *scheduler,
2264      struct GNUNET_SERVER_Handle *server,
2265      const struct GNUNET_CONFIGURATION_Handle *c)
2266 {
2267   unsigned long long max_hosts;
2268   sched = scheduler;
2269   cfg = c;
2270
2271   /* FIXME: Read from config, or calculate, or something other than this! */
2272   max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2273   ctx.max_table_size = DEFAULT_DV_SIZE;
2274   ctx.fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2275
2276   if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2277     {
2278       GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts);
2279     }
2280
2281   if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2282     {
2283       GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &ctx.max_table_size);
2284     }
2285
2286   if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2287     {
2288       GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &ctx.fisheye_depth);
2289     }
2290
2291   ctx.neighbor_min_heap =
2292     GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2293   ctx.neighbor_max_heap =
2294     GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2295
2296   ctx.direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2297   ctx.extended_neighbors =
2298     GNUNET_CONTAINER_multihashmap_create (ctx.max_table_size * 3);
2299
2300   GNUNET_SERVER_add_handlers (server, plugin_handlers);
2301   coreAPI =
2302   GNUNET_CORE_connect (sched,
2303                        cfg,
2304                        GNUNET_TIME_relative_get_forever(),
2305                        NULL, /* FIXME: anything we want to pass around? */
2306                        &core_init,
2307                        &handle_core_connect,
2308                        &handle_core_disconnect,
2309                        NULL,
2310                        GNUNET_NO,
2311                        NULL,
2312                        GNUNET_NO,
2313                        core_handlers);
2314
2315   if (coreAPI == NULL)
2316     return;
2317
2318    peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
2319
2320    if (peerinfo_handle == NULL)
2321      {
2322        GNUNET_CORE_disconnect(coreAPI);
2323        return;
2324      }
2325
2326   /* Scheduled the task to clean up when shutdown is called */
2327   cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
2328                                 GNUNET_TIME_UNIT_FOREVER_REL,
2329                                 &shutdown_task,
2330                                 NULL);
2331 }
2332
2333
2334 /**
2335  * The main function for the dv service.
2336  *
2337  * @param argc number of arguments from the command line
2338  * @param argv command line arguments
2339  * @return 0 ok, 1 on error
2340  */
2341 int
2342 main (int argc, char *const *argv)
2343 {
2344   return (GNUNET_OK ==
2345           GNUNET_SERVICE_run (argc,
2346                               argv,
2347                               "dv",
2348                               GNUNET_SERVICE_OPTION_NONE,
2349                               &run, NULL)) ? 0 : 1;
2350 }