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