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