f93f101d10a5444864b3dcbc6be0aa09cd245a76
[oweals/gnunet.git] / src / dv / gnunet-service-dv.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 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 3, 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 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_hello_lib.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_set_service.h"
38 #include "gnunet_ats_service.h"
39 #include "dv.h"
40 #include <gcrypt.h>
41
42
43 /**
44  * How often do we establish the consensu?
45  */
46 #define GNUNET_DV_CONSENSUS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
47
48 /**
49  * Maximum number of messages we queue per peer.
50  */
51 #define MAX_QUEUE_SIZE 16
52
53 /**
54  * Maximum number of messages we queue towards the clients/plugin.
55  */
56 #define MAX_QUEUE_SIZE_PLUGIN 1024
57
58 /**
59  * The default fisheye depth, from how many hops away will
60  * we keep peers?
61  */
62 #define DEFAULT_FISHEYE_DEPTH 3
63
64 /**
65  * How many hops is a direct neighbor away?
66  */
67 #define DIRECT_NEIGHBOR_COST 1
68
69
70 GNUNET_NETWORK_STRUCT_BEGIN
71
72 /**
73  * Information about a peer DV can route to.  These entries are what
74  * we use as the binary format to establish consensus to create our
75  * routing table and as the address format in the HELLOs.
76  */
77 struct Target
78 {
79
80   /**
81    * Identity of the peer we can reach.
82    */
83   struct GNUNET_PeerIdentity peer;
84
85   /**
86    * How many hops (1-3) is this peer away? in network byte order
87    */
88   uint32_t distance GNUNET_PACKED;
89
90 };
91
92
93 /**
94  * Message exchanged between DV services (via core), requesting a
95  * message to be routed.
96  */
97 struct RouteMessage
98 {
99   /**
100    * Type: GNUNET_MESSAGE_TYPE_DV_ROUTE
101    */
102   struct GNUNET_MessageHeader header;
103
104   /**
105    * Expected (remaining) distance.  Must be always smaller than
106    * DEFAULT_FISHEYE_DEPTH, should be zero at the target.  Must
107    * be decremented by one at each hop.  Peers must not forward
108    * these messages further once the counter has reached zero.
109    */
110   uint32_t distance GNUNET_PACKED;
111
112   /**
113    * The (actual) target of the message (this peer, if distance is zero).
114    */
115   struct GNUNET_PeerIdentity target;
116
117   /**
118    * The (actual) sender of the message.
119    */
120   struct GNUNET_PeerIdentity sender;
121
122 };
123
124 GNUNET_NETWORK_STRUCT_END
125
126
127 /**
128  * Linked list of messages to send to clients.
129  */
130 struct PendingMessage
131 {
132   /**
133    * Pointer to next item in the list
134    */
135   struct PendingMessage *next;
136
137   /**
138    * Pointer to previous item in the list
139    */
140   struct PendingMessage *prev;
141
142   /**
143    * Actual message to be sent, allocated after this struct.
144    */
145   const struct GNUNET_MessageHeader *msg;
146
147   /**
148    * Ultimate target for the message.
149    */
150   struct GNUNET_PeerIdentity ultimate_target;
151
152   /**
153    * Unique ID of the message.
154    */
155   uint32_t uid;
156
157 };
158
159
160 /**
161  * Information about a direct neighbor (core-level, excluding
162  * DV-links, only DV-enabled peers).
163  */
164 struct DirectNeighbor
165 {
166
167   /**
168    * Identity of the peer.
169    */
170   struct GNUNET_PeerIdentity peer;
171
172   /**
173    * Session ID we use whenever we create a set union with
174    * this neighbor; constructed from the XOR of our peer
175    * IDs and then salted with "DV-SALT" to avoid conflicts
176    * with other applications.
177    */
178   struct GNUNET_HashCode real_session_id;
179
180   /**
181    * Head of linked list of messages to send to this peer.
182    */
183   struct PendingMessage *pm_head;
184
185   /**
186    * Tail of linked list of messages to send to this peer.
187    */
188   struct PendingMessage *pm_tail;
189
190   /**
191    * Transmit handle to core service.
192    */
193   struct GNUNET_CORE_TransmitHandle *cth;
194
195   /**
196    * Routing table of the neighbor, NULL if not yet established.
197    * Keys are peer identities, values are 'struct Target' entries.
198    * Note that the distances in the targets are from the point-of-view
199    * of the peer, not from us!
200    */
201   struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table;
202
203   /**
204    * Updated routing table of the neighbor, under construction,
205    * NULL if we are not currently building it.
206    * Keys are peer identities, values are 'struct Target' entries.
207    * Note that the distances in the targets are from the point-of-view
208    * of the peer, not from us!
209    */
210   struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table_consensus;
211
212   /**
213    * Our current (exposed) routing table as a set.
214    */
215   struct GNUNET_SET_Handle *my_set;
216
217   /**
218    * Handle for our current active set union operation.
219    */
220   struct GNUNET_SET_OperationHandle *set_op;
221
222   /**
223    * Handle used if we are listening for this peer, waiting for the
224    * other peer to initiate construction of the set union.  NULL if
225    * we ar the initiating peer.
226    */
227   struct GNUNET_SET_ListenHandle *listen_handle;
228
229   /**
230    * ID of the task we use to (periodically) update our consensus
231    * with this peer.  Used if we are the initiating peer.
232    */
233   GNUNET_SCHEDULER_TaskIdentifier initiate_task;
234
235   /**
236    * At what offset are we, with respect to inserting our own routes
237    * into the consensus?
238    */
239   unsigned int consensus_insertion_offset;
240
241   /**
242    * At what distance are we, with respect to inserting our own routes
243    * into the consensus?
244    */
245   unsigned int consensus_insertion_distance;
246
247   /**
248    * Number of messages currently in the 'pm_XXXX'-DLL.
249    */
250   unsigned int pm_queue_size;
251
252   /**
253    * Elements in consensus
254    */
255   unsigned int consensus_elements;
256
257   /**
258    * Direct one hop route
259    */
260   struct Route *direct_route;
261
262   /**
263    * Flag set within 'check_target_removed' to trigger full global route refresh.
264    */
265   int target_removed;
266
267   /**
268    * Our distance to this peer, 0 for unknown.
269    */
270   uint32_t distance;
271
272   /**
273    * Is this neighbor connected at the core level?
274    */
275   int connected;
276
277 };
278
279
280 /**
281  * A route includes information about the next hop,
282  * the target, and the ultimate distance to the
283  * target.
284  */
285 struct Route
286 {
287
288   /**
289    * Which peer do we need to forward the message to?
290    */
291   struct DirectNeighbor *next_hop;
292
293   /**
294    * What would be the target, and how far is it away?
295    */
296   struct Target target;
297
298   /**
299    * Offset of this target in the respective consensus set.
300    */
301   unsigned int set_offset;
302
303 };
304
305
306 /**
307  * Set of targets we bring to a consensus; all targets in a set have a
308  * distance equal to the sets distance (which is implied by the array
309  * index of the set).
310  */
311 struct ConsensusSet
312 {
313
314   /**
315    * Array of targets in the set, may include NULL entries if a
316    * neighbor has disconnected; the targets are allocated with the
317    * respective container (all_routes), not here.
318    */
319   struct Route **targets;
320
321   /**
322    * Size of the 'targets' array.
323    */
324   unsigned int array_length;
325
326 };
327
328
329 /**
330  * Peermap of all of our neighbors; processing these usually requires
331  * first checking to see if the peer is core-connected and if the
332  * distance is 1, in which case they are direct neighbors.
333  */
334 static struct GNUNET_CONTAINER_MultiPeerMap *direct_neighbors;
335
336 /**
337  * Hashmap with all routes that we currently support; contains
338  * routing information for all peers from distance 2
339  * up to distance DEFAULT_FISHEYE_DEPTH.
340  */
341 static struct GNUNET_CONTAINER_MultiPeerMap *all_routes;
342
343 /**
344  * Array of consensus sets we expose to the outside world.  Sets
345  * are structured by the distance to the target.
346  */
347 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
348
349 /**
350  * Handle to the core service api.
351  */
352 static struct GNUNET_CORE_Handle *core_api;
353
354 /**
355  * The identity of our peer.
356  */
357 static struct GNUNET_PeerIdentity my_identity;
358
359 /**
360  * The configuration for this service.
361  */
362 static const struct GNUNET_CONFIGURATION_Handle *cfg;
363
364 /**
365  * The client, the DV plugin connected to us (or an event monitor).
366  * Hopefully this client will never change, although if the plugin
367  * dies and returns for some reason it may happen.
368  */
369 static struct GNUNET_SERVER_NotificationContext *nc;
370
371 /**
372  * Handle for the statistics service.
373  */
374 static struct GNUNET_STATISTICS_Handle *stats;
375
376 /**
377  * Handle to ATS service.
378  */
379 static struct GNUNET_ATS_PerformanceHandle *ats;
380
381
382 /**
383  * Start creating a new DV set union by initiating the connection.
384  *
385  * @param cls the 'struct DirectNeighbor' of the peer we're building
386  *        a routing consensus with
387  * @param tc scheduler context
388  */
389 static void
390 initiate_set_union (void *cls,
391                     const struct GNUNET_SCHEDULER_TaskContext *tc);
392
393
394 /**
395  * Start creating a new DV set union construction, our neighbour has
396  * asked for it (callback for listening peer).
397  *
398  * @param cls the 'struct DirectNeighbor' of the peer we're building
399  *        a routing consensus with
400  * @param other_peer the other peer
401  * @param context_msg message with application specific information from
402  *        the other peer
403  * @param request request from the other peer, use GNUNET_SET_accept
404  *        to accept it, otherwise the request will be refused
405  *        Note that we don't use a return value here, as it is also
406  *        necessary to specify the set we want to do the operation with,
407  *        whith sometimes can be derived from the context message.
408  *        Also necessary to specify the timeout.
409  */
410 static void
411 listen_set_union (void *cls,
412                   const struct GNUNET_PeerIdentity *other_peer,
413                   const struct GNUNET_MessageHeader *context_msg,
414                   struct GNUNET_SET_Request *request);
415
416
417 /**
418  * Forward a message from another peer to the plugin.
419  *
420  * @param message the message to send to the plugin
421  * @param origin the original sender of the message
422  * @param distance distance to the original sender of the message
423  */
424 static void
425 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
426                      const struct GNUNET_PeerIdentity *origin,
427                      uint32_t distance)
428 {
429   struct GNUNET_DV_ReceivedMessage *received_msg;
430   size_t size;
431
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433               "Delivering message from peer `%s'\n",
434               GNUNET_i2s (origin));
435   size = sizeof (struct GNUNET_DV_ReceivedMessage) +
436     ntohs (message->size);
437   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
438   {
439     GNUNET_break (0); /* too big */
440     return;
441   }
442   received_msg = GNUNET_malloc (size);
443   received_msg->header.size = htons (size);
444   received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
445   received_msg->distance = htonl (distance);
446   received_msg->sender = *origin;
447   memcpy (&received_msg[1], message, ntohs (message->size));
448   GNUNET_SERVER_notification_context_broadcast (nc,
449                                                 &received_msg->header,
450                                                 GNUNET_YES);
451   GNUNET_free (received_msg);
452 }
453
454
455 /**
456  * Forward a control message to the plugin.
457  *
458  * @param message the message to send to the plugin
459  */
460 static void
461 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
462 {
463   GNUNET_SERVER_notification_context_broadcast (nc,
464                                                 message,
465                                                 GNUNET_NO);
466 }
467
468
469 /**
470  * Give an (N)ACK message to the plugin, we transmitted a message for it.
471  *
472  * @param target peer that received the message
473  * @param uid plugin-chosen UID for the message
474  * @param nack GNUNET_NO to send ACK, GNUNET_YES to send NACK
475  */
476 static void
477 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
478                     uint32_t uid,
479                     int nack)
480 {
481   struct GNUNET_DV_AckMessage ack_msg;
482
483   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484               "Delivering ACK for message to peer `%s'\n",
485               GNUNET_i2s (target));
486   ack_msg.header.size = htons (sizeof (ack_msg));
487   ack_msg.header.type = htons ((GNUNET_YES == nack)
488                                ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
489                                : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
490   ack_msg.uid = htonl (uid);
491   ack_msg.target = *target;
492   send_control_to_plugin (&ack_msg.header);
493 }
494
495
496 /**
497  * Send a DISTANCE_CHANGED message to the plugin.
498  *
499  * @param peer peer with a changed distance
500  * @param distance new distance to the peer
501  */
502 static void
503 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
504                                 uint32_t distance)
505 {
506   struct GNUNET_DV_DistanceUpdateMessage du_msg;
507
508   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509               "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
510               GNUNET_i2s (peer));
511   du_msg.header.size = htons (sizeof (du_msg));
512   du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
513   du_msg.distance = htonl (distance);
514   du_msg.peer = *peer;
515   send_control_to_plugin (&du_msg.header);
516 }
517
518
519 /**
520  * Give a CONNECT message to the plugin.
521  *
522  * @param target peer that connected
523  * @param distance distance to the target
524  */
525 static void
526 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
527                         uint32_t distance)
528 {
529   struct GNUNET_DV_ConnectMessage cm;
530
531   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532               "Delivering CONNECT about peer `%s'\n",
533               GNUNET_i2s (target));
534   cm.header.size = htons (sizeof (cm));
535   cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
536   cm.distance = htonl (distance);
537   cm.peer = *target;
538   send_control_to_plugin (&cm.header);
539 }
540
541
542 /**
543  * Give a DISCONNECT message to the plugin.
544  *
545  * @param target peer that disconnected
546  */
547 static void
548 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
549 {
550   struct GNUNET_DV_DisconnectMessage dm;
551
552   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553               "Delivering DISCONNECT about peer `%s'\n",
554               GNUNET_i2s (target));
555   dm.header.size = htons (sizeof (dm));
556   dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
557   dm.reserved = htonl (0);
558   dm.peer = *target;
559   send_control_to_plugin (&dm.header);
560 }
561
562
563 /**
564  * Function called to transfer a message to another peer
565  * via core.
566  *
567  * @param cls closure with the direct neighbor
568  * @param size number of bytes available in buf
569  * @param buf where the callee should write the message
570  * @return number of bytes written to buf
571  */
572 static size_t
573 core_transmit_notify (void *cls, size_t size, void *buf)
574 {
575   struct DirectNeighbor *dn = cls;
576   char *cbuf = buf;
577   struct PendingMessage *pending;
578   size_t off;
579   size_t msize;
580
581   dn->cth = NULL;
582   if (NULL == buf)
583   {
584     /* peer disconnected */
585     return 0;
586   }
587   off = 0;
588   pending = dn->pm_head;
589   off = 0;
590   while ( (NULL != (pending = dn->pm_head)) &&
591           (size >= off + (msize = ntohs (pending->msg->size))))
592   {
593     dn->pm_queue_size--;
594     GNUNET_CONTAINER_DLL_remove (dn->pm_head,
595                                  dn->pm_tail,
596                                  pending);
597     memcpy (&cbuf[off], pending->msg, msize);
598     if (0 != pending->uid)
599       send_ack_to_plugin (&pending->ultimate_target,
600                           pending->uid,
601                           GNUNET_NO);
602     GNUNET_free (pending);
603     off += msize;
604   }
605   if (NULL != dn->pm_head)
606     dn->cth =
607       GNUNET_CORE_notify_transmit_ready (core_api,
608                                          GNUNET_YES /* cork */,
609                                          0 /* priority */,
610                                          GNUNET_TIME_UNIT_FOREVER_REL,
611                                          &dn->peer,
612                                          msize,                                 
613                                          &core_transmit_notify, dn);
614   return off;
615 }
616
617
618 /**
619  * Forward the given payload to the given target.
620  *
621  * @param target where to send the message
622  * @param uid unique ID for the message
623  * @param ultimate_target ultimate recipient for the message
624  * @param distance expected (remaining) distance to the target
625  * @param sender original sender of the message
626  * @param payload payload of the message
627  */
628 static void
629 forward_payload (struct DirectNeighbor *target,
630                  uint32_t distance,
631                  uint32_t uid,
632                  const struct GNUNET_PeerIdentity *sender,
633                  const struct GNUNET_PeerIdentity *ultimate_target,
634                  const struct GNUNET_MessageHeader *payload)
635 {
636   struct PendingMessage *pm;
637   struct RouteMessage *rm;
638   size_t msize;
639
640   if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
641        (0 != memcmp (sender,
642                      &my_identity,
643                      sizeof (struct GNUNET_PeerIdentity))) )
644   {
645     GNUNET_break (0 == uid);
646     return;
647   }
648   msize = sizeof (struct RouteMessage) + ntohs (payload->size);
649   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
650   {
651     GNUNET_break (0);
652     return;
653   }
654   pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
655   pm->ultimate_target = *ultimate_target;
656   pm->uid = uid;
657   pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
658   rm = (struct RouteMessage *) &pm[1];
659   rm->header.size = htons ((uint16_t) msize);
660   rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
661   rm->distance = htonl (distance);
662   rm->target = target->peer;
663   rm->sender = *sender;
664   memcpy (&rm[1], payload, ntohs (payload->size));
665   GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
666                                     target->pm_tail,
667                                     pm);
668   target->pm_queue_size++;
669   if (NULL == target->cth)
670     target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
671                                                      GNUNET_YES /* cork */,
672                                                      0 /* priority */,
673                                                      GNUNET_TIME_UNIT_FOREVER_REL,
674                                                      &target->peer,
675                                                      msize,                                     
676                                                      &core_transmit_notify, target);
677 }
678
679
680 /**
681  * Find a free slot for storing a 'route' in the 'consensi'
682  * set at the given distance.
683  *
684  * @param distance distance to use for the set slot
685  */
686 static unsigned int
687 get_consensus_slot (uint32_t distance)
688 {
689   struct ConsensusSet *cs;
690   unsigned int i;
691
692   cs = &consensi[distance];
693   i = 0;
694   while ( (i < cs->array_length) &&
695           (NULL != cs->targets[i]) ) i++;
696   if (i == cs->array_length)
697   {
698     GNUNET_array_grow (cs->targets,
699                        cs->array_length,
700                        cs->array_length * 2 + 2);
701   }
702   return i;
703 }
704
705
706 /**
707  * Allocate a slot in the consensus set for a route.
708  *
709  * @param route route to initialize
710  * @param distance which consensus set to use
711  */
712 static void
713 allocate_route (struct Route *route,
714                 uint32_t distance)
715 {
716   unsigned int i;
717
718   i = get_consensus_slot (distance);
719   route->set_offset = i;
720   consensi[distance].targets[i] = route;
721   route->target.distance = htonl (distance);
722 }
723
724
725 /**
726  * Release a slot in the consensus set for a route.
727  *
728  * @param route route to release the slot from
729  */
730 static void
731 release_route (struct Route *route)
732 {
733   consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
734   route->set_offset = UINT_MAX; /* indicate invalid slot */
735 }
736
737
738 /**
739  * Move a route from one consensus set to another.
740  *
741  * @param route route to move
742  * @param new_distance new distance for the route (destination set)
743  */
744 static void
745 move_route (struct Route *route,
746             uint32_t new_distance)
747 {
748   unsigned int i;
749
750   release_route (route);
751   i = get_consensus_slot (new_distance);
752   route->set_offset = i;
753   consensi[new_distance].targets[i] = route;
754   route->target.distance = htonl (new_distance);
755 }
756
757
758 /**
759  * Initialize this neighbors 'my_set' and when done give
760  * it to the pending set operation for execution.
761  *
762  * Add a single element to the set per call:
763  *
764  * If we reached the last element of a consensus element: increase distance
765  *
766  *
767  * @param cls the neighbor for which we are building the set
768  */
769 static void
770 build_set (void *cls)
771 {
772   struct DirectNeighbor *neighbor = cls;
773   struct GNUNET_SET_Element element;
774   struct Target *target;
775   target = NULL;
776   while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
777           (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
778   {
779     /* If we reached the last element of a consensus array element: increase distance and start with next array */
780     neighbor->consensus_insertion_offset = 0;
781     neighbor->consensus_insertion_distance++;
782     /* skip over NULL entries */
783     while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
784             (consensi[neighbor->consensus_insertion_distance].array_length  > neighbor->consensus_insertion_offset) &&
785             (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
786       neighbor->consensus_insertion_offset++;
787   }
788   if (DEFAULT_FISHEYE_DEPTH - 1 == neighbor->consensus_insertion_distance)
789   {
790     /* we have added all elements to the set, run the operation */
791     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792                 "Finished building my SET for peer `%s' with %u elements, committing\n",
793                 GNUNET_i2s(&neighbor->peer),
794                 neighbor->consensus_elements);
795     GNUNET_SET_commit (neighbor->set_op,
796                        neighbor->my_set);
797     GNUNET_SET_destroy (neighbor->my_set);
798     neighbor->my_set = NULL;
799     return;
800   }
801
802   target = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]->target;
803   element.size = sizeof (struct Target);
804   element.type = htons (0); /* do we need this? */
805   element.data = target;
806
807   /* Find next non-NULL entry */
808   neighbor->consensus_insertion_offset++;
809   /* skip over NULL entries */
810   while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
811           (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
812           (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
813   {
814     neighbor->consensus_insertion_offset++;
815   }
816
817   if ( (0 != memcmp(&target->peer, &my_identity, sizeof (my_identity))) &&
818        (0 != memcmp(&target->peer, &neighbor->peer, sizeof (neighbor->peer))) )
819   {
820     /* Add target if it is not the neighbor or this peer */
821     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822                 "Adding peer `%s' with distance %u to SET\n",
823                 GNUNET_i2s (&target->peer),
824                 ntohl (target->distance));
825     GNUNET_SET_add_element (neighbor->my_set,
826                             &element,
827                             &build_set, neighbor);
828     neighbor->consensus_elements++;
829   }
830   else
831     build_set(neighbor);
832 }
833
834
835 /**
836  * A peer is now connected to us at distance 1.  Initiate DV exchange.
837  *
838  * @param neighbor entry for the neighbor at distance 1
839  */
840 static void
841 handle_direct_connect (struct DirectNeighbor *neighbor)
842 {
843   struct Route *route;
844   struct GNUNET_HashCode h1;
845   struct GNUNET_HashCode h2;
846   struct GNUNET_HashCode session_id;
847
848   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849               "Direct connection to %s established, routing table exchange begins.\n",
850               GNUNET_i2s (&neighbor->peer));
851   GNUNET_STATISTICS_update (stats,
852                             "# peers connected (1-hop)",
853                             1, GNUNET_NO);
854   route = GNUNET_CONTAINER_multipeermap_get (all_routes,
855                                              &neighbor->peer);
856   if (NULL != route)
857   {
858     send_disconnect_to_plugin (&neighbor->peer);
859     release_route (route);
860     GNUNET_free (route);
861   }
862
863   neighbor->direct_route = GNUNET_new (struct Route);
864   neighbor->direct_route->next_hop = neighbor;
865   neighbor->direct_route->target.peer= neighbor->peer;
866   neighbor->direct_route->target.distance = DIRECT_NEIGHBOR_COST;
867   allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
868
869   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870               "Adding direct route to %s\n",
871               GNUNET_i2s (&neighbor->direct_route->target.peer));
872
873
874   /* construct session ID seed as XOR of both peer's identities */
875   GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &h1);
876   GNUNET_CRYPTO_hash (&neighbor->peer, sizeof (struct GNUNET_PeerIdentity), &h2);
877   GNUNET_CRYPTO_hash_xor (&h1,
878                           &h2,
879                           &session_id);
880   /* make sure session ID is unique across applications by salting it with 'DV' */
881   GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
882                       GCRY_MD_SHA512, GCRY_MD_SHA256,
883                       "DV-SALT", 2,
884                       &session_id, sizeof (session_id),
885                       NULL, 0);
886   if (0 < memcmp (&neighbor->peer,
887                   &my_identity,
888                   sizeof (struct GNUNET_PeerIdentity)))
889   {
890     if (NULL != neighbor->listen_handle)
891     {
892       GNUNET_break (0);
893     }
894     else
895       neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
896                                                         neighbor);
897   }
898   else
899   {
900     if (NULL != neighbor->listen_handle)
901     {
902       GNUNET_break (0);
903     }
904     else
905     {
906       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907                   "Starting SET listen operation with peer `%s'\n",
908                   GNUNET_i2s(&neighbor->peer));
909       neighbor->listen_handle = GNUNET_SET_listen (cfg,
910                                                    GNUNET_SET_OPERATION_UNION,
911                                                    &neighbor->real_session_id,
912                                                    &listen_set_union,
913                                                    neighbor);
914     }
915   }
916 }
917
918
919 /**
920  * Method called whenever a peer connects.
921  *
922  * @param cls closure
923  * @param peer peer identity this notification is about
924  */
925 static void
926 handle_core_connect (void *cls,
927                      const struct GNUNET_PeerIdentity *peer)
928 {
929   struct DirectNeighbor *neighbor;
930
931   /* Check for connect to self message */
932   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
933     return;
934   /* check if entry exists */
935   neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
936                                                 peer);
937   if (NULL != neighbor)
938   {
939     GNUNET_break (GNUNET_YES != neighbor->connected);
940     neighbor->connected = GNUNET_YES;
941     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942                 "Core connected to %s (distance %u)\n",
943                 GNUNET_i2s (peer),
944                 (unsigned int) neighbor->distance);
945     if (DIRECT_NEIGHBOR_COST != neighbor->distance)
946       return;
947     handle_direct_connect (neighbor);
948     return;
949   }
950   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951               "Core connected to %s (distance unknown)\n",
952               GNUNET_i2s (peer));
953   neighbor = GNUNET_new (struct DirectNeighbor);
954   neighbor->peer = *peer;
955   GNUNET_assert (GNUNET_YES ==
956                  GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
957                                                     peer,
958                                                     neighbor,
959                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
960   neighbor->connected = GNUNET_YES;
961   neighbor->distance = 0; /* unknown */
962 }
963
964
965 /**
966  * Called for each 'target' in a neighbor table to free the associated memory.
967  *
968  * @param cls NULL
969  * @param key key of the value
970  * @param value value to free
971  * @return GNUNET_OK to continue to iterate
972  */
973 static int
974 free_targets (void *cls,
975               const struct GNUNET_PeerIdentity *key,
976               void *value)
977 {
978   GNUNET_free (value);
979   return GNUNET_OK;
980 }
981
982
983 /**
984  * Multipeerhmap iterator for checking if a given route is
985  * (now) useful to this peer.
986  *
987  * @param cls the direct neighbor for the given route
988  * @param key key value stored under
989  * @param value a 'struct Target' that may or may not be useful; not that
990  *        the distance in 'target' does not include the first hop yet
991  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
992  */
993 static int
994 check_possible_route (void *cls,
995                       const struct GNUNET_PeerIdentity *key,
996                       void *value)
997 {
998   struct DirectNeighbor *neighbor = cls;
999   struct Target *target = value;
1000   struct Route *route;
1001
1002   route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1003                                              key);
1004   if (NULL != route)
1005   {
1006     if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
1007     {
1008       /* this 'target' is cheaper than the existing route; switch to alternative route! */
1009       move_route (route, ntohl (target->distance) + 1);
1010       route->next_hop = neighbor;
1011       send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1012     }
1013     return GNUNET_YES; /* got a route to this target already */
1014   }
1015   route = GNUNET_new (struct Route);
1016   route->next_hop = neighbor;
1017   route->target.distance = htonl (ntohl (target->distance) + 1);
1018   route->target.peer = target->peer;
1019   allocate_route (route, ntohl (route->target.distance));
1020   GNUNET_assert (GNUNET_YES ==
1021                  GNUNET_CONTAINER_multipeermap_put (all_routes,
1022                                                     &route->target.peer,
1023                                                     route,
1024                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1025   send_connect_to_plugin (&route->target.peer, ntohl (target->distance));
1026   return GNUNET_YES;
1027 }
1028
1029
1030 /**
1031  * Multipeermap iterator for finding routes that were previously
1032  * "hidden" due to a better route (called after a disconnect event).
1033  *
1034  * @param cls NULL
1035  * @param key peer identity of the given direct neighbor
1036  * @param value a 'struct DirectNeighbor' to check for additional routes
1037  * @return GNUNET_YES to continue iteration
1038  */
1039 static int
1040 refresh_routes (void *cls,
1041                 const struct GNUNET_PeerIdentity *key,
1042                 void *value)
1043 {
1044   struct DirectNeighbor *neighbor = value;
1045
1046   if ( (GNUNET_YES != neighbor->connected) ||
1047        (DIRECT_NEIGHBOR_COST != neighbor->distance) )
1048     return GNUNET_YES;
1049   if (NULL != neighbor->neighbor_table)
1050     GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1051                                            &check_possible_route,
1052                                            neighbor);
1053   return GNUNET_YES;
1054 }
1055
1056
1057 /**
1058  * Get distance information from 'atsi'.
1059  *
1060  * @param atsi performance data
1061  * @param atsi_count number of entries in atsi
1062  * @return connected transport distance
1063  */
1064 static uint32_t
1065 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
1066                    uint32_t atsi_count)
1067 {
1068   uint32_t i;
1069
1070   for (i = 0; i < atsi_count; i++)
1071     if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
1072       return (0 == ntohl (atsi->value)) ? DIRECT_NEIGHBOR_COST : ntohl (atsi->value); // FIXME: 0 check should not be required once ATS is fixed!
1073   /* If we do not have explicit distance data, assume direct neighbor. */
1074   return DIRECT_NEIGHBOR_COST;
1075 }
1076
1077
1078 /**
1079  * Multipeermap iterator for freeing routes that go via a particular
1080  * neighbor that disconnected and is thus no longer available.
1081  *
1082  * @param cls the direct neighbor that is now unavailable
1083  * @param key key value stored under
1084  * @param value a 'struct Route' that may or may not go via neighbor
1085  *
1086  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1087  */
1088 static int
1089 cull_routes (void *cls,
1090              const struct GNUNET_PeerIdentity *key,
1091              void *value)
1092 {
1093   struct DirectNeighbor *neighbor = cls;
1094   struct Route *route = value;
1095
1096   if (route->next_hop != neighbor)
1097     return GNUNET_YES; /* not affected */
1098   GNUNET_assert (GNUNET_YES ==
1099                  GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1100   release_route (route);
1101   send_disconnect_to_plugin (&route->target.peer);
1102   GNUNET_free (route);
1103   return GNUNET_YES;
1104 }
1105
1106
1107 /**
1108  * Handle the case that a direct connection to a peer is
1109  * disrupted.  Remove all routes via that peer and
1110  * stop the consensus with it.
1111  *
1112  * @param neighbor peer that was disconnected (or at least is no
1113  *    longer at distance 1)
1114  */
1115 static void
1116 handle_direct_disconnect (struct DirectNeighbor *neighbor)
1117 {
1118   GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1119                                          &cull_routes,
1120                                          neighbor);
1121   if (NULL != neighbor->cth)
1122   {
1123     GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1124     neighbor->cth = NULL;
1125   }
1126
1127   if (NULL != neighbor->direct_route)
1128   {
1129     release_route (neighbor->direct_route);
1130     GNUNET_free (neighbor->direct_route);
1131     neighbor->direct_route = NULL;
1132   }
1133
1134   if (NULL != neighbor->neighbor_table_consensus)
1135   {
1136     GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1137                                            &free_targets,
1138                                            NULL);
1139     GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1140     neighbor->neighbor_table_consensus = NULL;
1141   }
1142   if (NULL != neighbor->neighbor_table)
1143   {
1144     GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1145                                            &free_targets,
1146                                            NULL);
1147     GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1148     neighbor->neighbor_table = NULL;
1149   }
1150   if (NULL != neighbor->set_op)
1151   {
1152     GNUNET_SET_operation_cancel (neighbor->set_op);
1153     neighbor->set_op = NULL;
1154   }
1155   if (NULL != neighbor->my_set)
1156   {
1157     GNUNET_SET_destroy (neighbor->my_set);
1158     neighbor->my_set = NULL;
1159   }
1160   if (NULL != neighbor->listen_handle)
1161   {
1162     GNUNET_SET_listen_cancel (neighbor->listen_handle);
1163     neighbor->listen_handle = NULL;
1164   }
1165   if (GNUNET_SCHEDULER_NO_TASK != neighbor->initiate_task)
1166   {
1167     GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
1168     neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1169   }
1170 }
1171
1172
1173 /**
1174  * Function that is called with QoS information about an address; used
1175  * to update our current distance to another peer.
1176  *
1177  * @param cls closure
1178  * @param address the address
1179  * @param active is this address in active use
1180  * @param bandwidth_out assigned outbound bandwidth for the connection
1181  * @param bandwidth_in assigned inbound bandwidth for the connection
1182  * @param ats performance data for the address (as far as known)
1183  * @param ats_count number of performance records in 'ats'
1184  */
1185 static void
1186 handle_ats_update (void *cls,
1187                    const struct GNUNET_HELLO_Address *address,
1188                    int active,
1189                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1190                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1191                    const struct GNUNET_ATS_Information *ats,
1192                    uint32_t ats_count)
1193 {
1194   struct DirectNeighbor *neighbor;
1195   uint32_t distance;
1196
1197   if (GNUNET_NO == active)
1198         return;
1199   distance = get_atsi_distance (ats, ats_count);
1200   /*
1201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202               "ATS says distance to %s is %u\n",
1203               GNUNET_i2s (&address->peer),
1204               (unsigned int) distance);*/
1205   /* check if entry exists */
1206   neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1207                                                 &address->peer);
1208   if (NULL != neighbor)
1209   {
1210     if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
1211          (DIRECT_NEIGHBOR_COST == distance) )
1212       return; /* no change */
1213     if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1214     {
1215       neighbor->distance = distance;
1216       GNUNET_STATISTICS_update (stats,
1217                                 "# peers connected (1-hop)",
1218                                 -1, GNUNET_NO);
1219       handle_direct_disconnect (neighbor);
1220       GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1221                                              &refresh_routes,
1222                                              NULL);
1223       return;
1224     }
1225     neighbor->distance = distance;
1226     if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1227       return;
1228     if (GNUNET_YES != neighbor->connected)
1229       return;
1230     handle_direct_connect (neighbor);
1231     return;
1232   }
1233   neighbor = GNUNET_new (struct DirectNeighbor);
1234   neighbor->peer = address->peer;
1235   GNUNET_assert (GNUNET_YES ==
1236                  GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
1237                                                     &address->peer,
1238                                                     neighbor,
1239                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1240   neighbor->connected = GNUNET_NO; /* not yet */
1241   neighbor->distance = distance;
1242 }
1243
1244
1245 /**
1246  * Check if a target was removed from the set of the other peer; if so,
1247  * if we also used it for our route, we need to remove it from our
1248  * 'all_routes' set (and later check if an alternative path now exists).
1249  *
1250  * @param cls the 'struct DirectNeighbor'
1251  * @param key peer identity for the target
1252  * @param value a 'struct Target' previously reachable via the given neighbor
1253  */
1254 static int
1255 check_target_removed (void *cls,
1256                       const struct GNUNET_PeerIdentity *key,
1257                       void *value)
1258 {
1259   struct DirectNeighbor *neighbor = cls;
1260   struct Target *new_target;
1261   struct Route *current_route;
1262
1263   new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
1264                                                   key);
1265   if (NULL == new_target)
1266   {
1267     /* target was revoked, check if it was used */
1268     current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1269                                                        key);
1270     if ( (NULL == current_route) ||
1271          (current_route->next_hop != neighbor) )
1272     {
1273       /* didn't matter, wasn't used */
1274       return GNUNET_OK;
1275     }
1276     /* remove existing route */
1277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278                 "Lost route to %s\n",
1279                 GNUNET_i2s (&current_route->target.peer));
1280     GNUNET_assert (GNUNET_YES ==
1281                    GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
1282     send_disconnect_to_plugin (&current_route->target.peer);
1283     GNUNET_free (current_route);
1284     neighbor->target_removed = GNUNET_YES;
1285     return GNUNET_OK;
1286   }
1287   return GNUNET_OK;
1288 }
1289
1290
1291 /**
1292  * Check if a target was added to the set of the other peer; if it
1293  * was added or impoves the existing route, do the needed updates.
1294  *
1295  * @param cls the 'struct DirectNeighbor'
1296  * @param key peer identity for the target
1297  * @param value a 'struct Target' now reachable via the given neighbor
1298  */
1299 static int
1300 check_target_added (void *cls,
1301                     const struct GNUNET_PeerIdentity *key,
1302                     void *value)
1303 {
1304   struct DirectNeighbor *neighbor = cls;
1305   struct Target *target = value;
1306   struct Route *current_route;
1307
1308   /* target was revoked, check if it was used */
1309   current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1310                                                      key);
1311   if (NULL != current_route)
1312   {
1313     /* route exists */
1314     if (current_route->next_hop == neighbor)
1315     {
1316       /* we had the same route before, no change */
1317       if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1318       {
1319         current_route->target.distance = htonl (ntohl (target->distance) + 1);
1320         send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1321       }
1322       return GNUNET_OK;
1323     }
1324     if (ntohl (current_route->target.distance) >= ntohl (target->distance) + 1)
1325     {
1326       /* alternative, shorter route exists, ignore */
1327       return GNUNET_OK;
1328     }
1329     /* new route is better than the existing one, take over! */
1330     /* NOTE: minor security issue: malicious peers may advertise
1331        very short routes to take over longer paths; as we don't
1332        check that the shorter routes actually work, a malicious
1333        direct neighbor can use this to DoS our long routes */
1334     current_route->next_hop = neighbor;
1335     current_route->target.distance = htonl (ntohl (target->distance) + 1);
1336     send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1337     return GNUNET_OK;
1338   }
1339   /* new route */
1340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341               "Discovered new route to %s using %u hops\n",
1342               GNUNET_i2s (&target->peer),
1343               (unsigned int) (ntohl (target->distance) + 1));
1344   current_route = GNUNET_new (struct Route);
1345   current_route->next_hop = neighbor;
1346   current_route->target.peer = target->peer;
1347   current_route->target.distance = htonl (ntohl (target->distance) + 1);
1348   GNUNET_assert (GNUNET_YES ==
1349                  GNUNET_CONTAINER_multipeermap_put (all_routes,
1350                                                     &current_route->target.peer,
1351                                                     current_route,
1352                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1353   send_connect_to_plugin (&current_route->target.peer,
1354                           ntohl (current_route->target.distance));
1355   return GNUNET_OK;
1356 }
1357
1358
1359 /**
1360  * Callback for set operation results. Called for each element
1361  * in the result set.
1362  * We have learned a new route from the other peer.  Add it to the
1363  * route set we're building.
1364  *
1365  * @param cls the 'struct DirectNeighbor' we're building the consensus with
1366  * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
1367  * @param status see enum GNUNET_SET_Status
1368  */
1369 static void
1370 handle_set_union_result (void *cls,
1371                          const struct GNUNET_SET_Element *element,
1372                          enum GNUNET_SET_Status status)
1373 {
1374   struct DirectNeighbor *neighbor = cls;
1375   struct Target *target;
1376   char *status_str;
1377
1378   switch (status) {
1379     case GNUNET_SET_STATUS_OK:
1380       status_str = "GNUNET_SET_STATUS_OK";
1381       break;
1382     case GNUNET_SET_STATUS_TIMEOUT:
1383       status_str = "GNUNET_SET_STATUS_TIMEOUT";
1384       break;
1385     case GNUNET_SET_STATUS_FAILURE:
1386       status_str = "GNUNET_SET_STATUS_FAILURE";
1387       break;
1388     case GNUNET_SET_STATUS_HALF_DONE:
1389       status_str = "GNUNET_SET_STATUS_HALF_DONE";
1390       break;
1391     case GNUNET_SET_STATUS_DONE:
1392       status_str = "GNUNET_SET_STATUS_DONE";
1393       break;
1394     default:
1395       status_str = "UNDEFINED";
1396       break;
1397   }
1398
1399   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400               "Got SET union result: %s\n",
1401               status_str);
1402   switch (status)
1403   {
1404   case GNUNET_SET_STATUS_OK:
1405     if (sizeof (struct Target) != element->size)
1406     {
1407       GNUNET_break_op (0);
1408       return;
1409     }
1410     target = GNUNET_new (struct Target);
1411     memcpy (target, element->data, sizeof (struct Target));
1412     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413                 "Received information about peer `%s' with distance %u\n",
1414                 GNUNET_i2s (&target->peer), ntohl(target->distance) + 1);
1415     if (NULL == neighbor->neighbor_table_consensus)
1416       neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1417     if (GNUNET_YES !=
1418         GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
1419                                            &target->peer,
1420                                            target,
1421                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1422     {
1423       GNUNET_break_op (0);
1424       GNUNET_free (target);
1425     }
1426     break;
1427   case GNUNET_SET_STATUS_TIMEOUT:
1428   case GNUNET_SET_STATUS_FAILURE:
1429     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430                 "Failed to establish DV union, will try again later\n");
1431     neighbor->set_op = NULL;
1432     if (NULL != neighbor->neighbor_table_consensus)
1433     {
1434       GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1435                                              &free_targets,
1436                                              NULL);
1437       GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1438       neighbor->neighbor_table_consensus = NULL;
1439     }
1440     if (0 < memcmp (&neighbor->peer,
1441                     &my_identity,
1442                     sizeof (struct GNUNET_PeerIdentity)))
1443       neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1444                                                               &initiate_set_union,
1445                                                               neighbor);
1446     break;
1447   case GNUNET_SET_STATUS_HALF_DONE:
1448     break;
1449   case GNUNET_SET_STATUS_DONE:
1450     /* we got all of our updates; integrate routing table! */
1451     neighbor->target_removed = GNUNET_NO;
1452     if (NULL == neighbor->neighbor_table_consensus)
1453       neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1454     if (NULL != neighbor->neighbor_table)
1455       GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1456                                            &check_target_removed,
1457                                            neighbor);
1458     if (GNUNET_YES == neighbor->target_removed)
1459     {
1460       /* check if we got an alternative for the removed routes */
1461       GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1462                                              &refresh_routes,
1463                                              NULL);
1464     }
1465     /* add targets that appeared (and check for improved routes) */
1466     GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1467                                            &check_target_added,
1468                                            neighbor);
1469     if (NULL != neighbor->neighbor_table)
1470     {
1471       GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1472                                              &free_targets,
1473                                              NULL);
1474       GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1475       neighbor->neighbor_table = NULL;
1476     }
1477     neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1478     neighbor->neighbor_table_consensus = NULL;
1479
1480     /* operation done, schedule next run! */
1481     neighbor->set_op = NULL;
1482     if (0 < memcmp (&neighbor->peer,
1483                     &my_identity,
1484                     sizeof (struct GNUNET_PeerIdentity)))
1485       neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1486                                                               &initiate_set_union,
1487                                                               neighbor);
1488     break;
1489   default:
1490     GNUNET_break (0);
1491     return;
1492   }
1493 }
1494
1495
1496 /**
1497  * Start creating a new DV set union construction, our neighbour has
1498  * asked for it (callback for listening peer).
1499  *
1500  * @param cls the 'struct DirectNeighbor' of the peer we're building
1501  *        a routing consensus with
1502  * @param other_peer the other peer
1503  * @param context_msg message with application specific information from
1504  *        the other peer
1505  * @param request request from the other peer, use GNUNET_SET_accept
1506  *        to accept it, otherwise the request will be refused
1507  *        Note that we don't use a return value here, as it is also
1508  *        necessary to specify the set we want to do the operation with,
1509  *        whith sometimes can be derived from the context message.
1510  *        Also necessary to specify the timeout.
1511  */
1512 static void
1513 listen_set_union (void *cls,
1514                   const struct GNUNET_PeerIdentity *other_peer,
1515                   const struct GNUNET_MessageHeader *context_msg,
1516                   struct GNUNET_SET_Request *request)
1517 {
1518   struct DirectNeighbor *neighbor = cls;
1519
1520   if (NULL == request)
1521     return; /* why??? */
1522   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1523               "Starting to create consensus with %s\n",
1524               GNUNET_i2s (&neighbor->peer));
1525   if (NULL != neighbor->set_op)
1526   {
1527     GNUNET_SET_operation_cancel (neighbor->set_op);
1528     neighbor->set_op = NULL;
1529   }
1530   if (NULL != neighbor->my_set)
1531   {
1532     GNUNET_SET_destroy (neighbor->my_set);
1533     neighbor->my_set = NULL;
1534   }
1535   neighbor->my_set = GNUNET_SET_create (cfg,
1536                                         GNUNET_SET_OPERATION_UNION);
1537   neighbor->set_op = GNUNET_SET_accept (request,
1538                                         GNUNET_SET_RESULT_ADDED,
1539                                         &handle_set_union_result,
1540                                         neighbor);
1541   neighbor->consensus_insertion_offset = 0;
1542   neighbor->consensus_insertion_distance = 0;
1543   neighbor->consensus_elements = 0;
1544   build_set (neighbor);
1545 }
1546
1547
1548 /**
1549  * Start creating a new DV set union by initiating the connection.
1550  *
1551  * @param cls the 'struct DirectNeighbor' of the peer we're building
1552  *        a routing consensus with
1553  * @param tc scheduler context
1554  */
1555 static void
1556 initiate_set_union (void *cls,
1557                     const struct GNUNET_SCHEDULER_TaskContext *tc)
1558 {
1559   struct DirectNeighbor *neighbor = cls;
1560
1561   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562               "Initiating SET union with peer `%s'\n",
1563               GNUNET_i2s (&neighbor->peer));
1564   neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1565   neighbor->my_set = GNUNET_SET_create (cfg,
1566                                         GNUNET_SET_OPERATION_UNION);
1567   neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1568                                          &neighbor->real_session_id,
1569                                          NULL,
1570                                          0 /* FIXME: salt */,
1571                                          GNUNET_SET_RESULT_ADDED,
1572                                          &handle_set_union_result,
1573                                          neighbor);
1574   neighbor->consensus_insertion_offset = 0;
1575   neighbor->consensus_insertion_distance = 0;
1576   neighbor->consensus_elements = 0;
1577   build_set (neighbor);
1578 }
1579
1580
1581 /**
1582  * Core handler for DV data messages.  Whatever this message
1583  * contains all we really have to do is rip it out of its
1584  * DV layering and give it to our pal the DV plugin to report
1585  * in with.
1586  *
1587  * @param cls closure
1588  * @param peer peer which sent the message (immediate sender)
1589  * @param message the message
1590  * @return GNUNET_OK on success, GNUNET_SYSERR if the other peer violated the protocol
1591  */
1592 static int
1593 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
1594                          const struct GNUNET_MessageHeader *message)
1595 {
1596   const struct RouteMessage *rm;
1597   const struct GNUNET_MessageHeader *payload;
1598   struct Route *route;
1599
1600   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601               "Handling DV message\n");
1602   if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1603   {
1604     GNUNET_break_op (0);
1605     return GNUNET_SYSERR;
1606   }
1607   rm = (const struct RouteMessage *) message;
1608   payload = (const struct GNUNET_MessageHeader *) &rm[1];
1609   if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1610   {
1611     GNUNET_break_op (0);
1612     return GNUNET_SYSERR;
1613   }
1614   if (0 == memcmp (&rm->target,
1615                    &my_identity,
1616                    sizeof (struct GNUNET_PeerIdentity)))
1617   {
1618     /* message is for me, check reverse route! */
1619     route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1620                                                &rm->sender);
1621     if (NULL == route)
1622     {
1623       /* don't have reverse route, drop */
1624       GNUNET_STATISTICS_update (stats,
1625                                 "# message discarded (no reverse route)",
1626                                 1, GNUNET_NO);
1627       return GNUNET_OK;
1628     }
1629     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630                 "Delivering %u bytes to myself!\n",
1631                 ntohs (payload->size));
1632     send_data_to_plugin (payload,
1633                          &rm->sender,
1634                          ntohl (route->target.distance));
1635     return GNUNET_OK;
1636   }
1637   route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1638                                              &rm->target);
1639   if (NULL == route)
1640   {
1641     GNUNET_STATISTICS_update (stats,
1642                               "# messages discarded (no route)",
1643                               1, GNUNET_NO);
1644     return GNUNET_OK;
1645   }
1646   if (ntohl (route->target.distance) > ntohl (rm->distance) + 1)
1647   {
1648     GNUNET_STATISTICS_update (stats,
1649                               "# messages discarded (target too far)",
1650                               1, GNUNET_NO);
1651     return GNUNET_OK;
1652   }
1653   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1654               "Forwarding message to %s\n",
1655               GNUNET_i2s (&rm->target));
1656   forward_payload (route->next_hop,
1657                    ntohl (route->target.distance),
1658                    0,
1659                    &rm->target,
1660                    &rm->sender,
1661                    payload);
1662   return GNUNET_OK;
1663 }
1664
1665
1666 /**
1667  * Service server's handler for message send requests (which come
1668  * bubbling up to us through the DV plugin).
1669  *
1670  * @param cls closure
1671  * @param client identification of the client
1672  * @param message the actual message
1673  */
1674 static void
1675 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1676                         const struct GNUNET_MessageHeader *message)
1677 {
1678   struct Route *route;
1679   const struct GNUNET_DV_SendMessage *msg;
1680   const struct GNUNET_MessageHeader *payload;
1681
1682   if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1683   {
1684     GNUNET_break (0);
1685     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1686     return;
1687   }
1688   msg = (const struct GNUNET_DV_SendMessage *) message;
1689   GNUNET_break (0 != ntohl (msg->uid));
1690   payload = (const struct GNUNET_MessageHeader *) &msg[1];
1691   if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1692   {
1693     GNUNET_break (0);
1694     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1695     return;
1696   }
1697   route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1698                                              &msg->target);
1699   if (NULL == route)
1700   {
1701     /* got disconnected */
1702     GNUNET_STATISTICS_update (stats,
1703                               "# local messages discarded (no route)",
1704                               1, GNUNET_NO);
1705     send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
1706     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1707     return;
1708   }
1709   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710               "Forwarding %u bytes to %s\n",
1711               ntohs (payload->size),
1712               GNUNET_i2s (&msg->target));
1713
1714   forward_payload (route->next_hop,
1715                    ntohl (route->target.distance),
1716                    htonl (msg->uid),
1717                    &msg->target,
1718                    &my_identity,
1719                    payload);
1720   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1721 }
1722
1723
1724 /**
1725  * Cleanup all of the data structures associated with a given neighbor.
1726  *
1727  * @param neighbor neighbor to clean up
1728  */
1729 static void
1730 cleanup_neighbor (struct DirectNeighbor *neighbor)
1731 {
1732   struct PendingMessage *pending;
1733
1734   while (NULL != (pending = neighbor->pm_head))
1735   {
1736     neighbor->pm_queue_size--;
1737     GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1738                                  neighbor->pm_tail,
1739                                  pending);
1740     GNUNET_free (pending);
1741   }
1742   handle_direct_disconnect (neighbor);
1743   GNUNET_assert (GNUNET_YES ==
1744                  GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
1745                                                        &neighbor->peer,
1746                                                        neighbor));
1747   GNUNET_free (neighbor);
1748 }
1749
1750
1751 /**
1752  * Method called whenever a given peer disconnects.
1753  *
1754  * @param cls closure
1755  * @param peer peer identity this notification is about
1756  */
1757 static void
1758 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1759 {
1760   struct DirectNeighbor *neighbor;
1761
1762   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763               "Received core peer disconnect message for peer `%s'!\n",
1764               GNUNET_i2s (peer));
1765   /* Check for disconnect from self message */
1766   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1767     return;
1768   neighbor =
1769       GNUNET_CONTAINER_multipeermap_get (direct_neighbors, peer);
1770   if (NULL == neighbor)
1771   {
1772     GNUNET_break (0);
1773     return;
1774   }
1775   GNUNET_break (GNUNET_YES == neighbor->connected);
1776   neighbor->connected = GNUNET_NO;
1777   if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1778   {
1779
1780     GNUNET_STATISTICS_update (stats,
1781                               "# peers connected (1-hop)",
1782                               -1, GNUNET_NO);
1783   }
1784   cleanup_neighbor (neighbor);
1785   GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1786                                          &refresh_routes,
1787                                          NULL);
1788 }
1789
1790
1791 /**
1792  * Multipeermap iterator for freeing routes.  Should never be called.
1793  *
1794  * @param cls NULL
1795  * @param key key value stored under
1796  * @param value the route to be freed
1797  *
1798  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1799  */
1800 static int
1801 free_route (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
1802 {
1803   struct Route *route = value;
1804
1805   GNUNET_break (0);
1806   GNUNET_assert (GNUNET_YES ==
1807                  GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1808   release_route (route);
1809   send_disconnect_to_plugin (&route->target.peer);
1810   GNUNET_free (route);
1811   return GNUNET_YES;
1812 }
1813
1814
1815 /**
1816  * Multipeermap iterator for freeing direct neighbors. Should never be called.
1817  *
1818  * @param cls NULL
1819  * @param key key value stored under
1820  * @param value the direct neighbor to be freed
1821  *
1822  * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1823  */
1824 static int
1825 free_direct_neighbors (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
1826 {
1827   struct DirectNeighbor *neighbor = value;
1828
1829   GNUNET_break (0);
1830   cleanup_neighbor (neighbor);
1831   return GNUNET_YES;
1832 }
1833
1834
1835 /**
1836  * Task run during shutdown.
1837  *
1838  * @param cls unused
1839  * @param tc unused
1840  */
1841 static void
1842 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1843 {
1844   unsigned int i;
1845
1846   GNUNET_CORE_disconnect (core_api);
1847   core_api = NULL;
1848   GNUNET_ATS_performance_done (ats);
1849   ats = NULL;
1850   GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1851                                          &free_direct_neighbors, NULL);
1852   GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1853                                          &free_route, NULL);
1854   GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
1855   GNUNET_CONTAINER_multipeermap_destroy (all_routes);
1856   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1857   stats = NULL;
1858   GNUNET_SERVER_notification_context_destroy (nc);
1859   nc = NULL;
1860   for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
1861     GNUNET_array_grow (consensi[i].targets,
1862                        consensi[i].array_length,
1863                        0);
1864 }
1865
1866
1867 /**
1868  * Notify newly connected client about an existing route.
1869  *
1870  * @param cls the 'struct GNUNET_SERVER_Client'
1871  * @param key peer identity
1872  * @param value the XXX.
1873  * @return GNUNET_OK (continue to iterate)
1874  */
1875 static int
1876 add_route (void *cls,
1877            const struct GNUNET_PeerIdentity *key,
1878            void *value)
1879 {
1880   struct GNUNET_SERVER_Client *client = cls;
1881   struct Route *route = value;
1882   struct GNUNET_DV_ConnectMessage cm;
1883
1884   cm.header.size = htons (sizeof (cm));
1885   cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1886   cm.distance = htonl (route->target.distance);
1887   cm.peer = route->target.peer;
1888
1889   GNUNET_SERVER_notification_context_unicast (nc,
1890                                               client,
1891                                               &cm.header,
1892                                               GNUNET_NO);
1893   return GNUNET_OK;
1894 }
1895
1896
1897 /**
1898  * Handle START-message.  This is the first message sent to us
1899  * by the client (can only be one!).
1900  *
1901  * @param cls closure (always NULL)
1902  * @param client identification of the client
1903  * @param message the actual message
1904  */
1905 static void
1906 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1907               const struct GNUNET_MessageHeader *message)
1908 {
1909   GNUNET_SERVER_notification_context_add (nc, client);
1910   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1911   GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1912                                          &add_route,
1913                                          client);
1914 }
1915
1916
1917 /**
1918  * Called on core init.
1919  *
1920  * @param cls unused
1921  * @param identity this peer's identity
1922  */
1923 static void
1924 core_init (void *cls,
1925            const struct GNUNET_PeerIdentity *identity)
1926 {
1927   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928               "I am peer: %s\n",
1929               GNUNET_i2s (identity));
1930   my_identity = *identity;
1931 }
1932
1933
1934 /**
1935  * Process dv requests.
1936  *
1937  * @param cls closure
1938  * @param server the initialized server
1939  * @param c configuration to use
1940  */
1941 static void
1942 run (void *cls, struct GNUNET_SERVER_Handle *server,
1943      const struct GNUNET_CONFIGURATION_Handle *c)
1944 {
1945   static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1946     {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
1947     {NULL, 0, 0}
1948   };
1949   static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1950     {&handle_start, NULL,
1951      GNUNET_MESSAGE_TYPE_DV_START,
1952      sizeof (struct GNUNET_MessageHeader) },
1953     { &handle_dv_send_message, NULL,
1954       GNUNET_MESSAGE_TYPE_DV_SEND,
1955       0},
1956     {NULL, NULL, 0, 0}
1957   };
1958
1959   cfg = c;
1960   direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1961   all_routes = GNUNET_CONTAINER_multipeermap_create (65536, GNUNET_NO);
1962   core_api = GNUNET_CORE_connect (cfg, NULL,
1963                                   &core_init,
1964                                   &handle_core_connect,
1965                                   &handle_core_disconnect,
1966                                   NULL, GNUNET_NO,
1967                                   NULL, GNUNET_NO,
1968                                   core_handlers);
1969
1970   if (NULL == core_api)
1971     return;
1972   ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
1973   if (NULL == ats)
1974   {
1975     GNUNET_CORE_disconnect (core_api);
1976     return;
1977   }
1978   nc = GNUNET_SERVER_notification_context_create (server,
1979                                                   MAX_QUEUE_SIZE_PLUGIN);
1980   stats = GNUNET_STATISTICS_create ("dv", cfg);
1981   GNUNET_SERVER_add_handlers (server, plugin_handlers);
1982   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1983                                 &shutdown_task, NULL);
1984 }
1985
1986
1987 /**
1988  * The main function for the dv service.
1989  *
1990  * @param argc number of arguments from the command line
1991  * @param argv command line arguments
1992  * @return 0 ok, 1 on error
1993  */
1994 int
1995 main (int argc, char *const *argv)
1996 {
1997   return (GNUNET_OK ==
1998           GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
1999                               &run, NULL)) ? 0 : 1;
2000 }
2001
2002 /* end of gnunet-service-dv.c */