859a5378bf36854abd41339ff0c8c9dbe852210a
[oweals/gnunet.git] / src / cadet / cadet_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011, 2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file cadet/cadet_api.c
22  * @brief cadet api: client implementation of cadet service
23  * @author Bartlomiej Polot
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
30 #include "cadet.h"
31 #include "cadet_protocol.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
34
35 /******************************************************************************/
36 /************************      DATA STRUCTURES     ****************************/
37 /******************************************************************************/
38
39 /**
40  * Transmission queue to the service
41  */
42 struct GNUNET_CADET_TransmitHandle
43 {
44   /**
45    * Double Linked list
46    */
47   struct GNUNET_CADET_TransmitHandle *next;
48
49   /**
50    * Double Linked list
51    */
52   struct GNUNET_CADET_TransmitHandle *prev;
53
54   /**
55    * Channel this message is sent on / for (may be NULL for control messages).
56    */
57   struct GNUNET_CADET_Channel *channel;
58
59   /**
60    * Request data task.
61    */
62   struct GNUNET_SCHEDULER_Task *request_data_task;
63
64   /**
65    * Callback to obtain the message to transmit, or NULL if we
66    * got the message in 'data'.  Notice that messages built
67    * by 'notify' need to be encapsulated with information about
68    * the 'target'.
69    */
70   GNUNET_CONNECTION_TransmitReadyNotify notify;
71
72   /**
73    * Closure for 'notify'
74    */
75   void *notify_cls;
76
77   /**
78    * Size of the payload.
79    */
80   size_t size;
81 };
82
83
84 union CadetInfoCB
85 {
86
87   /**
88    * Channel callback.
89    */
90   GNUNET_CADET_ChannelCB channel_cb;
91
92   /**
93    * Monitor callback
94    */
95   GNUNET_CADET_PeersCB peers_cb;
96
97   /**
98    * Monitor callback
99    */
100   GNUNET_CADET_PeerCB peer_cb;
101
102   /**
103    * Monitor callback
104    */
105   GNUNET_CADET_TunnelsCB tunnels_cb;
106
107   /**
108    * Tunnel callback.
109    */
110   GNUNET_CADET_TunnelCB tunnel_cb;
111 };
112
113
114 /**
115  * Opaque handle to the service.
116  */
117 struct GNUNET_CADET_Handle
118 {
119   /**
120    * Message queue (if available).
121    */
122   struct GNUNET_MQ_Handle *mq;
123
124   /**
125    * Set of handlers used for processing incoming messages in the channels
126    */
127   const struct GNUNET_CADET_MessageHandler *message_handlers;
128
129   /**
130    * Number of handlers in the handlers array.
131    */
132   unsigned int n_handlers;
133
134   /**
135    * Ports open.
136    */
137   struct GNUNET_CONTAINER_MultiHashMap *ports;
138
139   /**
140    * Double linked list of the channels this client is connected to, head.
141    */
142   struct GNUNET_CADET_Channel *channels_head;
143
144   /**
145    * Double linked list of the channels this client is connected to, tail.
146    */
147   struct GNUNET_CADET_Channel *channels_tail;
148
149   /**
150    * Callback for inbound channel disconnection
151    */
152   GNUNET_CADET_ChannelEndHandler *cleaner;
153
154   /**
155    * Closure for all the handlers given by the client
156    */
157   void *cls;
158
159   /**
160    * Messages to send to the service, head.
161    */
162   struct GNUNET_CADET_TransmitHandle *th_head;
163
164   /**
165    * Messages to send to the service, tail.
166    */
167   struct GNUNET_CADET_TransmitHandle *th_tail;
168
169   /**
170    * child of the next channel to create (to avoid reusing IDs often)
171    */
172   struct GNUNET_CADET_ClientChannelNumber next_ccn;
173
174   /**
175    * Configuration given by the client, in case of reconnection
176    */
177   const struct GNUNET_CONFIGURATION_Handle *cfg;
178
179   /**
180    * Time to the next reconnect in case one reconnect fails
181    */
182   struct GNUNET_TIME_Relative reconnect_time;
183
184   /**
185    * Task for trying to reconnect.
186    */
187   struct GNUNET_SCHEDULER_Task * reconnect_task;
188
189   /**
190    * Callback for an info task (only one active at a time).
191    */
192   union CadetInfoCB info_cb;
193
194   /**
195    * Info callback closure for @c info_cb.
196    */
197   void *info_cls;
198 };
199
200
201 /**
202  * Description of a peer
203  */
204 struct GNUNET_CADET_Peer
205 {
206   /**
207    * ID of the peer in short form
208    */
209   GNUNET_PEER_Id id;
210
211   /**
212    * Channel this peer belongs to
213    */
214   struct GNUNET_CADET_Channel *t;
215 };
216
217
218 /**
219  * Opaque handle to a channel.
220  */
221 struct GNUNET_CADET_Channel
222 {
223   /**
224    * DLL next
225    */
226   struct GNUNET_CADET_Channel *next;
227
228   /**
229    * DLL prev
230    */
231   struct GNUNET_CADET_Channel *prev;
232
233   /**
234    * Handle to the cadet this channel belongs to
235    */
236   struct GNUNET_CADET_Handle *cadet;
237
238   /**
239    * Local ID of the channel
240    */
241   struct GNUNET_CADET_ClientChannelNumber ccn;
242
243   /**
244    * Channel's port, if any.
245    */
246   struct GNUNET_CADET_Port *port;
247
248   /**
249    * Other end of the channel.
250    */
251   GNUNET_PEER_Id peer;
252
253   /**
254    * Any data the caller wants to put in here
255    */
256   void *ctx;
257
258   /**
259    * Channel options: reliability, etc.
260    */
261   enum GNUNET_CADET_ChannelOption options;
262
263   /**
264    * Are we allowed to send to the service?
265    */
266   unsigned int allow_send;
267
268 };
269
270
271 /**
272  * Opaque handle to a port.
273  */
274 struct GNUNET_CADET_Port
275 {
276   /**
277    * Handle to the CADET session this port belongs to.
278    */
279   struct GNUNET_CADET_Handle *cadet;
280
281   /**
282    * Port ID.
283    */
284   struct GNUNET_HashCode *hash;
285
286   /**
287    * Callback handler for incoming channels on this port.
288    */
289   GNUNET_CADET_InboundChannelNotificationHandler *handler;
290
291   /**
292    * Closure for @a handler.
293    */
294   void *cls;
295 };
296
297
298 /**
299  * Implementation state for cadet's message queue.
300  */
301 struct CadetMQState
302 {
303   /**
304    * The current transmit handle, or NULL
305    * if no transmit is active.
306    */
307   struct GNUNET_CADET_TransmitHandle *th;
308
309   /**
310    * Channel to send the data over.
311    */
312   struct GNUNET_CADET_Channel *channel;
313 };
314
315
316 /******************************************************************************/
317 /***********************     AUXILIARY FUNCTIONS      *************************/
318 /******************************************************************************/
319
320 /**
321  * Check if transmission is a payload packet.
322  *
323  * @param th Transmission handle.
324  *
325  * @return #GNUNET_YES if it is a payload packet,
326  *         #GNUNET_NO if it is a cadet management packet.
327  */
328 static int
329 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
330 {
331   return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
332 }
333
334
335 /**
336  * Find the Port struct for a hash.
337  *
338  * @param h CADET handle.
339  * @param hash HashCode for the port number.
340  *
341  * @return The port handle if known, NULL otherwise.
342  */
343 static struct GNUNET_CADET_Port *
344 find_port (const struct GNUNET_CADET_Handle *h,
345            const struct GNUNET_HashCode *hash)
346 {
347   struct GNUNET_CADET_Port *p;
348
349   p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
350
351   return p;
352 }
353
354
355 /**
356  * Get the channel handler for the channel specified by id from the given handle
357  *
358  * @param h Cadet handle
359  * @param ccn ID of the wanted channel
360  * @return handle to the required channel or NULL if not found
361  */
362 static struct GNUNET_CADET_Channel *
363 retrieve_channel (struct GNUNET_CADET_Handle *h,
364                   struct GNUNET_CADET_ClientChannelNumber ccn)
365 {
366   struct GNUNET_CADET_Channel *ch;
367
368   for (ch = h->channels_head; NULL != ch; ch = ch->next)
369     if (ch->ccn.channel_of_client == ccn.channel_of_client)
370       return ch;
371   return NULL;
372 }
373
374
375 /**
376  * Create a new channel and insert it in the channel list of the cadet handle
377  *
378  * @param h Cadet handle
379  * @param ccn Desired ccn of the channel, 0 to assign one automatically.
380  *
381  * @return Handle to the created channel.
382  */
383 static struct GNUNET_CADET_Channel *
384 create_channel (struct GNUNET_CADET_Handle *h,
385                 struct GNUNET_CADET_ClientChannelNumber ccn)
386 {
387   struct GNUNET_CADET_Channel *ch;
388
389   ch = GNUNET_new (struct GNUNET_CADET_Channel);
390   GNUNET_CONTAINER_DLL_insert (h->channels_head,
391                                h->channels_tail,
392                                ch);
393   ch->cadet = h;
394   if (0 == ccn.channel_of_client)
395   {
396     ch->ccn = h->next_ccn;
397     while (NULL != retrieve_channel (h,
398                                      h->next_ccn))
399     {
400       h->next_ccn.channel_of_client
401         = htonl (1 + ntohl (h->next_ccn.channel_of_client));
402       if (0 == ntohl (h->next_ccn.channel_of_client))
403         h->next_ccn.channel_of_client
404           = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
405     }
406   }
407   else
408   {
409     ch->ccn = ccn;
410   }
411   return ch;
412 }
413
414
415 /**
416  * Destroy the specified channel.
417  * - Destroys all peers, calling the disconnect callback on each if needed
418  * - Cancels all outgoing traffic for that channel, calling respective notifys
419  * - Calls cleaner if channel was inbound
420  * - Frees all memory used
421  *
422  * @param ch Pointer to the channel.
423  * @param call_cleaner Whether to call the cleaner handler.
424  *
425  * @return Handle to the required channel or NULL if not found.
426  */
427 // FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
428 static void
429 destroy_channel (struct GNUNET_CADET_Channel *ch,
430                  int call_cleaner)
431 {
432   struct GNUNET_CADET_Handle *h;
433   struct GNUNET_CADET_TransmitHandle *th;
434   struct GNUNET_CADET_TransmitHandle *next;
435
436   if (NULL == ch)
437   {
438     GNUNET_break (0);
439     return;
440   }
441   h = ch->cadet;
442   LOG (GNUNET_ERROR_TYPE_DEBUG,
443        " destroy_channel %X of %p\n",
444        ch->ccn,
445        h);
446
447   GNUNET_CONTAINER_DLL_remove (h->channels_head,
448                                h->channels_tail,
449                                ch);
450
451   /* signal channel destruction */
452   if ( (NULL != h->cleaner) &&
453        (0 != ch->peer) &&
454        (GNUNET_YES == call_cleaner) )
455   {
456     LOG (GNUNET_ERROR_TYPE_DEBUG,
457          " calling cleaner\n");
458     h->cleaner (h->cls, ch, ch->ctx);
459   }
460
461   /* check that clients did not leave messages behind in the queue */
462   for (th = h->th_head; NULL != th; th = next)
463   {
464     next = th->next;
465     if (th->channel != ch)
466       continue;
467     /* Clients should have aborted their requests already.
468      * Management traffic should be ok, as clients can't cancel that.
469      * If the service crashed and we are reconnecting, it's ok.
470      */
471     GNUNET_break (GNUNET_NO == th_is_payload (th));
472     GNUNET_CADET_notify_transmit_ready_cancel (th);
473   }
474
475   if (0 != ch->peer)
476     GNUNET_PEER_change_rc (ch->peer, -1);
477   GNUNET_free (ch);
478 }
479
480
481 /**
482  * Add a transmit handle to the transmission queue and set the
483  * timeout if needed.
484  *
485  * @param h cadet handle with the queue head and tail
486  * @param th handle to the packet to be transmitted
487  */
488 static void
489 add_to_queue (struct GNUNET_CADET_Handle *h,
490               struct GNUNET_CADET_TransmitHandle *th)
491 {
492   GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
493 }
494
495
496 /**
497  * Remove a transmit handle from the transmission queue, if present.
498  *
499  * Safe to call even if not queued.
500  *
501  * @param th handle to the packet to be unqueued.
502  */
503 static void
504 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
505 {
506   struct GNUNET_CADET_Handle *h = th->channel->cadet;
507
508   /* It might or might not have been queued (rarely not), but check anyway. */
509   if (NULL != th->next || h->th_tail == th)
510   {
511     GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
512   }
513 }
514
515
516 /**
517  * Send an ack on the channel to confirm the processing of a message.
518  *
519  * @param ch Channel on which to send the ACK.
520  */
521 static void
522 send_ack (struct GNUNET_CADET_Channel *ch)
523 {
524   struct GNUNET_CADET_LocalAck *msg;
525   struct GNUNET_MQ_Envelope *env;
526
527   env = GNUNET_MQ_msg (msg,
528                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
529
530   LOG (GNUNET_ERROR_TYPE_DEBUG,
531        "Sending ACK on channel %X\n",
532        ch->ccn.channel_of_client);
533   msg->ccn = ch->ccn;
534   GNUNET_MQ_send (ch->cadet->mq,
535                   env);
536 }
537
538
539
540 /******************************************************************************/
541 /***********************      RECEIVE HANDLERS     ****************************/
542 /******************************************************************************/
543
544
545 /**
546  * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
547  * request the data to send over MQ. Since MQ manages the queue, this function
548  * is scheduled immediatly after a transmit ready notification.
549  *
550  * @param cls Closure (transmit handle).
551  */
552 static void
553 request_data (void *cls)
554 {
555   struct GNUNET_CADET_TransmitHandle *th = cls;
556   struct GNUNET_CADET_LocalData *msg;
557   struct GNUNET_MQ_Envelope *env;
558   size_t osize;
559
560   LOG (GNUNET_ERROR_TYPE_DEBUG,
561        "Requesting Data: %u bytes (allow send is %u)\n",
562        th->size,
563        th->channel->allow_send);
564
565   GNUNET_assert (0 < th->channel->allow_send);
566   th->channel->allow_send--;
567   /* NOTE: we may be allowed to send another packet immediately,
568      albeit the current logic waits for the ACK. */
569   th->request_data_task = NULL;
570   remove_from_queue (th);
571
572   env = GNUNET_MQ_msg_extra (msg,
573                              th->size,
574                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
575   msg->ccn = th->channel->ccn;
576   osize = th->notify (th->notify_cls,
577                       th->size,
578                       &msg[1]);
579   GNUNET_assert (osize == th->size);
580   GNUNET_MQ_send (th->channel->cadet->mq,
581                   env);
582   GNUNET_free (th);
583 }
584
585
586 /**
587  * Process the new channel notification and add it to the channels in the handle
588  *
589  * @param h     The cadet handle
590  * @param msg   A message with the details of the new incoming channel
591  */
592 static void
593 handle_channel_created (void *cls,
594                         const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
595 {
596   struct GNUNET_CADET_Handle *h = cls;
597   struct GNUNET_CADET_Channel *ch;
598   struct GNUNET_CADET_Port *port;
599   const struct GNUNET_HashCode *port_number;
600   struct GNUNET_CADET_ClientChannelNumber ccn;
601
602   ccn = msg->ccn;
603   port_number = &msg->port;
604   LOG (GNUNET_ERROR_TYPE_DEBUG,
605        "Creating incoming channel %X [%s]\n",
606        ntohl (ccn.channel_of_client),
607        GNUNET_h2s (port_number));
608   if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
609   {
610     GNUNET_break (0);
611     return;
612   }
613   port = find_port (h, port_number);
614   if (NULL != port)
615   {
616     void *ctx;
617
618     ch = create_channel (h, ccn);
619     ch->peer = GNUNET_PEER_intern (&msg->peer);
620     ch->cadet = h;
621     ch->ccn = ccn;
622     ch->port = port;
623     ch->options = ntohl (msg->opt);
624
625     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
626     ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
627     if (NULL != ctx)
628       ch->ctx = ctx;
629     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
630   }
631   else
632   {
633     struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
634     struct GNUNET_MQ_Envelope *env;
635
636     LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
637     env = GNUNET_MQ_msg (d_msg,
638                          GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
639     d_msg->ccn = msg->ccn;
640     GNUNET_MQ_send (h->mq, env);
641   }
642   return;
643 }
644
645
646 /**
647  * Process the channel destroy notification and free associated resources
648  *
649  * @param h     The cadet handle
650  * @param msg   A message with the details of the channel being destroyed
651  */
652 static void
653 handle_channel_destroy (void *cls,
654                         const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
655 {
656   struct GNUNET_CADET_Handle *h = cls;
657   struct GNUNET_CADET_Channel *ch;
658   struct GNUNET_CADET_ClientChannelNumber ccn;
659
660   ccn = msg->ccn;
661   LOG (GNUNET_ERROR_TYPE_DEBUG,
662        "Channel %X Destroy from service\n",
663        ntohl (ccn.channel_of_client));
664   ch = retrieve_channel (h,
665                          ccn);
666
667   if (NULL == ch)
668   {
669     LOG (GNUNET_ERROR_TYPE_DEBUG,
670          "channel %X unknown\n",
671          ntohl (ccn.channel_of_client));
672     return;
673   }
674   destroy_channel (ch,
675                    GNUNET_YES);
676 }
677
678
679 /**
680  * Check that message received from CADET service is well-formed.
681  *
682  * @param cls the `struct GNUNET_CADET_Handle`
683  * @param message the message we got
684  * @return #GNUNET_OK if the message is well-formed,
685  *         #GNUNET_SYSERR otherwise
686  */
687 static int
688 check_local_data (void *cls,
689                   const struct GNUNET_CADET_LocalData *message)
690 {
691   struct GNUNET_CADET_Handle *h = cls;
692   struct GNUNET_CADET_Channel *ch;
693   uint16_t size;
694
695   size = ntohs (message->header.size);
696   if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
697   {
698     GNUNET_break_op (0);
699     return GNUNET_SYSERR;
700   }
701
702   ch = retrieve_channel (h,
703                          message->ccn);
704   if (NULL == ch)
705   {
706     GNUNET_break_op (0);
707     return GNUNET_SYSERR;
708   }
709
710   return GNUNET_OK;
711 }
712
713
714 /**
715  * Process the incoming data packets, call appropriate handlers.
716  *
717  * @param h       The cadet handle
718  * @param message A message encapsulating the data
719  */
720 static void
721 handle_local_data (void *cls,
722                    const struct GNUNET_CADET_LocalData *message)
723 {
724   struct GNUNET_CADET_Handle *h = cls;
725   const struct GNUNET_MessageHeader *payload;
726   const struct GNUNET_CADET_MessageHandler *handler;
727   struct GNUNET_CADET_Channel *ch;
728   unsigned int i;
729   uint16_t type;
730
731   ch = retrieve_channel (h,
732                          message->ccn);
733   GNUNET_assert (NULL != ch);
734
735   payload = (struct GNUNET_MessageHeader *) &message[1];
736   type = ntohs (payload->type);
737   LOG (GNUNET_ERROR_TYPE_DEBUG,
738        "Got a %s data on channel %s [%X] of type %s\n",
739        GC_f2s (ntohl (ch->ccn.channel_of_client) >=
740                GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
741        GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
742        ntohl (message->ccn.channel_of_client),
743        GC_m2s (type));
744
745   for (i = 0; i < h->n_handlers; i++)
746   {
747     handler = &h->message_handlers[i];
748     if (handler->type == type)
749     {
750       if (GNUNET_OK !=
751           handler->callback (h->cls, ch, &ch->ctx, payload))
752       {
753         LOG (GNUNET_ERROR_TYPE_DEBUG,
754              "callback caused disconnection\n");
755         GNUNET_CADET_channel_destroy (ch);
756         break;
757       }
758       break;
759     }
760   }
761 }
762
763
764 /**
765  * Process a local ACK message, enabling the client to send
766  * more data to the service.
767  *
768  * @param h Cadet handle.
769  * @param message Message itself.
770  */
771 static void
772 handle_local_ack (void *cls,
773                   const struct GNUNET_CADET_LocalAck *message)
774 {
775   struct GNUNET_CADET_Handle *h = cls;
776   struct GNUNET_CADET_Channel *ch;
777   struct GNUNET_CADET_ClientChannelNumber ccn;
778   struct GNUNET_CADET_TransmitHandle *th;
779
780   ccn = message->ccn;
781   ch = retrieve_channel (h, ccn);
782   if (NULL == ch)
783   {
784     LOG (GNUNET_ERROR_TYPE_DEBUG,
785          "ACK on unknown channel %X\n",
786          ntohl (ccn.channel_of_client));
787     return;
788   }
789   ch->allow_send++;
790   LOG (GNUNET_ERROR_TYPE_DEBUG,
791        "Got an ACK on channel %X, allow send now %u!\n",
792        ntohl (ch->ccn.channel_of_client),
793        ch->allow_send);
794   for (th = h->th_head; NULL != th; th = th->next)
795   {
796     if ( (th->channel == ch) &&
797          (NULL == th->request_data_task) )
798     {
799       th->request_data_task
800         = GNUNET_SCHEDULER_add_now (&request_data,
801                                     th);
802       break;
803     }
804   }
805 }
806
807 /**
808  * Reconnect to the service, retransmit all infomation to try to restore the
809  * original state.
810  *
811  * @param h handle to the cadet
812  *
813  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
814  */
815 static void
816 reconnect (struct GNUNET_CADET_Handle *h);
817
818
819 /**
820  * Reconnect callback: tries to reconnect again after a failer previous
821  * reconnection.
822  *
823  * @param cls closure (cadet handle)
824  */
825 static void
826 reconnect_cbk (void *cls);
827
828
829 /**
830  * Generic error handler, called with the appropriate error code and
831  * the same closure specified at the creation of the message queue.
832  * Not every message queue implementation supports an error handler.
833  *
834  * @param cls closure, a `struct GNUNET_CORE_Handle *`
835  * @param error error code
836  */
837 static void
838 handle_mq_error (void *cls,
839                  enum GNUNET_MQ_Error error)
840 {
841   struct GNUNET_CADET_Handle *h = cls;
842
843   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
844   GNUNET_MQ_destroy (h->mq);
845   h->mq = NULL;
846   reconnect (h);
847 }
848
849
850 /*
851  * Process a local reply about info on all channels, pass info to the user.
852  *
853  * @param h Cadet handle.
854  * @param message Message itself.
855  */
856 // static void
857 // process_get_channels (struct GNUNET_CADET_Handle *h,
858 //                      const struct GNUNET_MessageHeader *message)
859 // {
860 //   struct GNUNET_CADET_LocalInfo *msg;
861 //
862 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
863 //
864 //   if (NULL == h->channels_cb)
865 //   {
866 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
867 //     return;
868 //   }
869 //
870 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
871 //   if (ntohs (message->size) !=
872 //       (sizeof (struct GNUNET_CADET_LocalInfo) +
873 //        sizeof (struct GNUNET_PeerIdentity)))
874 //   {
875 //     GNUNET_break_op (0);
876 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
877 //                 "Get channels message: size %hu - expected %u\n",
878 //                 ntohs (message->size),
879 //                 sizeof (struct GNUNET_CADET_LocalInfo));
880 //     return;
881 //   }
882 //   h->channels_cb (h->channels_cls,
883 //                   ntohl (msg->channel_id),
884 //                   &msg->owner,
885 //                   &msg->destination);
886 // }
887
888
889
890 /*
891  * Process a local monitor_channel reply, pass info to the user.
892  *
893  * @param h Cadet handle.
894  * @param message Message itself.
895  */
896 // static void
897 // process_show_channel (struct GNUNET_CADET_Handle *h,
898 //                      const struct GNUNET_MessageHeader *message)
899 // {
900 //   struct GNUNET_CADET_LocalInfo *msg;
901 //   size_t esize;
902 //
903 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
904 //
905 //   if (NULL == h->channel_cb)
906 //   {
907 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
908 //     return;
909 //   }
910 //
911 //   /* Verify message sanity */
912 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
913 //   esize = sizeof (struct GNUNET_CADET_LocalInfo);
914 //   if (ntohs (message->size) != esize)
915 //   {
916 //     GNUNET_break_op (0);
917 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918 //                 "Show channel message: size %hu - expected %u\n",
919 //                 ntohs (message->size),
920 //                 esize);
921 //
922 //     h->channel_cb (h->channel_cls, NULL, NULL);
923 //     h->channel_cb = NULL;
924 //     h->channel_cls = NULL;
925 //
926 //     return;
927 //   }
928 //
929 //   h->channel_cb (h->channel_cls,
930 //                  &msg->destination,
931 //                  &msg->owner);
932 // }
933
934
935
936 /**
937  * Check that message received from CADET service is well-formed.
938  *
939  * @param cls the `struct GNUNET_CADET_Handle`
940  * @param message the message we got
941  * @return #GNUNET_OK if the message is well-formed,
942  *         #GNUNET_SYSERR otherwise
943  */
944 static int
945 check_get_peers (void *cls,
946                  const struct GNUNET_CADET_LocalInfoPeer *message)
947 {
948   struct GNUNET_CADET_Handle *h = cls;
949   uint16_t size;
950
951   if (NULL == h->info_cb.peers_cb)
952   {
953     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954                 "  no handler for peesr monitor message!\n");
955     return GNUNET_SYSERR;
956   }
957
958   size = ntohs (message->header.size);
959   if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
960   {
961     h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
962     h->info_cb.peers_cb = NULL;
963     h->info_cls = NULL;
964     return GNUNET_SYSERR;
965   }
966
967   return GNUNET_OK;
968 }
969
970
971 /**
972  * Process a local reply about info on all tunnels, pass info to the user.
973  *
974  * @param cls Closure (Cadet handle).
975  * @param msg Message itself.
976  */
977 static void
978 handle_get_peers (void *cls,
979                   const struct GNUNET_CADET_LocalInfoPeer *msg)
980 {
981   struct GNUNET_CADET_Handle *h = cls;
982   h->info_cb.peers_cb (h->info_cls, &msg->destination,
983                        (int) ntohs (msg->tunnel),
984                        (unsigned int ) ntohs (msg->paths),
985                        0);
986 }
987
988
989 /**
990  * Check that message received from CADET service is well-formed.
991  *
992  * @param cls the `struct GNUNET_CADET_Handle`
993  * @param message the message we got
994  * @return #GNUNET_OK if the message is well-formed,
995  *         #GNUNET_SYSERR otherwise
996  */
997 static int
998 check_get_peer (void *cls,
999                 const struct GNUNET_CADET_LocalInfoPeer *message)
1000 {
1001   struct GNUNET_CADET_Handle *h = cls;
1002   const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1003   struct GNUNET_PeerIdentity *paths_array;
1004   size_t esize;
1005   unsigned int epaths;
1006   unsigned int paths;
1007   unsigned int peers;
1008
1009   if (NULL == h->info_cb.peer_cb)
1010   {
1011     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1012                 "  no handler for peer monitor message!\n");
1013     goto clean_cls;
1014   }
1015
1016   /* Verify message sanity */
1017   esize = ntohs (message->header.size);
1018   if (esize < msize)
1019   {
1020     GNUNET_break_op (0);
1021     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1022     goto clean_cls;
1023   }
1024   if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1025   {
1026     GNUNET_break_op (0);
1027     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1028     goto clean_cls;
1029
1030   }
1031   peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1032   epaths = (unsigned int) ntohs (message->paths);
1033   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1034   paths = 0;
1035   for (int i = 0; i < peers; i++)
1036   {
1037     if (0 == memcmp (&paths_array[i], &message->destination,
1038                      sizeof (struct GNUNET_PeerIdentity)))
1039     {
1040       paths++;
1041     }
1042   }
1043   if (paths != epaths)
1044   {
1045     GNUNET_break_op (0);
1046     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1047     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1048     goto clean_cls;
1049   }
1050
1051   return GNUNET_OK;
1052
1053 clean_cls:
1054   h->info_cb.peer_cb = NULL;
1055   h->info_cls = NULL;
1056   return GNUNET_SYSERR;
1057 }
1058
1059
1060 /**
1061  * Process a local peer info reply, pass info to the user.
1062  *
1063  * @param cls Closure (Cadet handle).
1064  * @param message Message itself.
1065  */
1066 static void
1067 handle_get_peer (void *cls,
1068                  const struct GNUNET_CADET_LocalInfoPeer *message)
1069 {
1070   struct GNUNET_CADET_Handle *h = cls;
1071   struct GNUNET_PeerIdentity *paths_array;
1072   unsigned int paths;
1073   unsigned int path_length;
1074   int neighbor;
1075   unsigned int peers;
1076
1077   paths = (unsigned int) ntohs (message->paths);
1078   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1079   peers = (ntohs (message->header.size) - sizeof (*message))
1080           / sizeof (struct GNUNET_PeerIdentity);
1081   path_length = 0;
1082   neighbor = GNUNET_NO;
1083
1084   for (int i = 0; i < peers; i++)
1085   {
1086     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1087     path_length++;
1088     if (0 == memcmp (&paths_array[i], &message->destination,
1089                      sizeof (struct GNUNET_PeerIdentity)))
1090     {
1091       if (1 == path_length)
1092         neighbor = GNUNET_YES;
1093       path_length = 0;
1094     }
1095   }
1096
1097   /* Call Callback with tunnel info. */
1098   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1099   h->info_cb.peer_cb (h->info_cls,
1100                       &message->destination,
1101                       (int) ntohs (message->tunnel),
1102                       neighbor,
1103                       paths,
1104                       paths_array);
1105 }
1106
1107
1108 /**
1109  * Check that message received from CADET service is well-formed.
1110  *
1111  * @param cls the `struct GNUNET_CADET_Handle`
1112  * @param msg the message we got
1113  * @return #GNUNET_OK if the message is well-formed,
1114  *         #GNUNET_SYSERR otherwise
1115  */
1116 static int
1117 check_get_tunnels (void *cls,
1118                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1119 {
1120   struct GNUNET_CADET_Handle *h = cls;
1121   uint16_t size;
1122
1123   if (NULL == h->info_cb.tunnels_cb)
1124   {
1125     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1126                 "  no handler for tunnels monitor message!\n");
1127     return GNUNET_SYSERR;
1128   }
1129
1130   size = ntohs (msg->header.size);
1131   if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1132   {
1133     h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1134     h->info_cb.tunnels_cb = NULL;
1135     h->info_cls = NULL;
1136     return GNUNET_SYSERR;
1137   }
1138   return GNUNET_OK;
1139 }
1140
1141
1142 /**
1143  * Process a local reply about info on all tunnels, pass info to the user.
1144  *
1145  * @param cls Closure (Cadet handle).
1146  * @param message Message itself.
1147  */
1148 static void
1149 handle_get_tunnels (void *cls,
1150                     const struct GNUNET_CADET_LocalInfoTunnel *msg)
1151 {
1152   struct GNUNET_CADET_Handle *h = cls;
1153
1154   h->info_cb.tunnels_cb (h->info_cls,
1155                          &msg->destination,
1156                          ntohl (msg->channels),
1157                          ntohl (msg->connections),
1158                          ntohs (msg->estate),
1159                          ntohs (msg->cstate));
1160
1161 }
1162
1163
1164 /**
1165  * Check that message received from CADET service is well-formed.
1166  *
1167  * @param cls the `struct GNUNET_CADET_Handle`
1168  * @param msg the message we got
1169  * @return #GNUNET_OK if the message is well-formed,
1170  *         #GNUNET_SYSERR otherwise
1171  */
1172 static int
1173 check_get_tunnel (void *cls,
1174                   const struct GNUNET_CADET_LocalInfoTunnel *msg)
1175 {
1176   struct GNUNET_CADET_Handle *h = cls;
1177   unsigned int ch_n;
1178   unsigned int c_n;
1179   size_t esize;
1180   size_t msize;
1181
1182   if (NULL == h->info_cb.tunnel_cb)
1183   {
1184     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1185                 "  no handler for tunnel monitor message!\n");
1186     goto clean_cls;
1187   }
1188
1189   /* Verify message sanity */
1190   msize = ntohs (msg->header.size);
1191   esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1192   if (esize > msize)
1193   {
1194     GNUNET_break_op (0);
1195     h->info_cb.tunnel_cb (h->info_cls,
1196                           NULL, 0, 0, NULL, NULL, 0, 0);
1197     goto clean_cls;
1198   }
1199   ch_n = ntohl (msg->channels);
1200   c_n = ntohl (msg->connections);
1201   esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1202   esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1203   if (msize != esize)
1204   {
1205     GNUNET_break_op (0);
1206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207                 "m:%u, e: %u (%u ch, %u conn)\n",
1208                 (unsigned int) msize,
1209                 (unsigned int) esize,
1210                 ch_n,
1211                 c_n);
1212     h->info_cb.tunnel_cb (h->info_cls,
1213                           NULL, 0, 0, NULL, NULL, 0, 0);
1214     goto clean_cls;
1215   }
1216
1217   return GNUNET_OK;
1218
1219 clean_cls:
1220   h->info_cb.tunnel_cb = NULL;
1221   h->info_cls = NULL;
1222   return GNUNET_SYSERR;
1223 }
1224
1225
1226 /**
1227  * Process a local tunnel info reply, pass info to the user.
1228  *
1229  * @param cls Closure (Cadet handle).
1230  * @param msg Message itself.
1231  */
1232 static void
1233 handle_get_tunnel (void *cls,
1234                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1235 {
1236   struct GNUNET_CADET_Handle *h = cls;
1237   unsigned int ch_n;
1238   unsigned int c_n;
1239   const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1240   const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1241
1242   ch_n = ntohl (msg->channels);
1243   c_n = ntohl (msg->connections);
1244
1245   /* Call Callback with tunnel info. */
1246   conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1247   chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1248   h->info_cb.tunnel_cb (h->info_cls,
1249                         &msg->destination,
1250                         ch_n,
1251                         c_n,
1252                         chns,
1253                         conns,
1254                         ntohs (msg->estate),
1255                         ntohs (msg->cstate));
1256 }
1257
1258
1259
1260 /**
1261  * Reconnect to the service, retransmit all infomation to try to restore the
1262  * original state.
1263  *
1264  * @param h handle to the cadet
1265  * @return #GNUNET_YES in case of success, #GNUNET_NO otherwise (service down...)
1266  */
1267 static int
1268 do_reconnect (struct GNUNET_CADET_Handle *h)
1269 {
1270   struct GNUNET_MQ_MessageHandler handlers[] = {
1271     GNUNET_MQ_hd_fixed_size (channel_created,
1272                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1273                              struct GNUNET_CADET_LocalChannelCreateMessage,
1274                              h),
1275     GNUNET_MQ_hd_fixed_size (channel_destroy,
1276                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1277                              struct GNUNET_CADET_LocalChannelDestroyMessage,
1278                              h),
1279     GNUNET_MQ_hd_var_size (local_data,
1280                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1281                            struct GNUNET_CADET_LocalData,
1282                            h),
1283     GNUNET_MQ_hd_fixed_size (local_ack,
1284                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1285                              struct GNUNET_CADET_LocalAck,
1286                              h),
1287     GNUNET_MQ_hd_var_size (get_peers,
1288                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1289                            struct GNUNET_CADET_LocalInfoPeer,
1290                            h),
1291     GNUNET_MQ_hd_var_size (get_peer,
1292                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1293                            struct GNUNET_CADET_LocalInfoPeer,
1294                            h),
1295     GNUNET_MQ_hd_var_size (get_tunnels,
1296                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1297                            struct GNUNET_CADET_LocalInfoTunnel,
1298                            h),
1299     GNUNET_MQ_hd_var_size (get_tunnel,
1300                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1301                            struct GNUNET_CADET_LocalInfoTunnel,
1302                            h),
1303   // FIXME
1304 //   GNUNET_MQ_hd_fixed_Y       size (channel_destroyed,
1305 //                            GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1306 //                            struct GNUNET_CADET_ChannelDestroyMessage);
1307     GNUNET_MQ_handler_end ()
1308   };
1309
1310   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1311
1312   GNUNET_assert (NULL == h->mq);
1313   h->mq = GNUNET_CLIENT_connect (h->cfg,
1314                                  "cadet",
1315                                  handlers,
1316                                  &handle_mq_error,
1317                                  h);
1318   if (NULL == h->mq)
1319   {
1320     reconnect (h);
1321     return GNUNET_NO;
1322   }
1323   else
1324   {
1325     h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1326   }
1327   return GNUNET_YES;
1328 }
1329
1330 /**
1331  * Reconnect callback: tries to reconnect again after a failer previous
1332  * reconnecttion
1333  *
1334  * @param cls closure (cadet handle)
1335  */
1336 static void
1337 reconnect_cbk (void *cls)
1338 {
1339   struct GNUNET_CADET_Handle *h = cls;
1340
1341   h->reconnect_task = NULL;
1342   do_reconnect (h);
1343 }
1344
1345
1346 /**
1347  * Reconnect to the service, retransmit all infomation to try to restore the
1348  * original state.
1349  *
1350  * @param h handle to the cadet
1351  *
1352  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1353  */
1354 static void
1355 reconnect (struct GNUNET_CADET_Handle *h)
1356 {
1357   struct GNUNET_CADET_Channel *ch;
1358
1359   LOG (GNUNET_ERROR_TYPE_DEBUG,
1360        "Requested RECONNECT, destroying all channels\n");
1361   while (NULL != (ch = h->channels_head))
1362     destroy_channel (ch, GNUNET_YES);
1363   if (NULL == h->reconnect_task)
1364     h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1365                                                       &reconnect_cbk, h);
1366 }
1367
1368
1369 /******************************************************************************/
1370 /**********************      API CALL DEFINITIONS     *************************/
1371 /******************************************************************************/
1372
1373 struct GNUNET_CADET_Handle *
1374 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1375                       void *cls,
1376                       GNUNET_CADET_ChannelEndHandler cleaner,
1377                       const struct GNUNET_CADET_MessageHandler *handlers)
1378 {
1379   struct GNUNET_CADET_Handle *h;
1380
1381   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1382   h = GNUNET_new (struct GNUNET_CADET_Handle);
1383   LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1384   h->cfg = cfg;
1385   h->cleaner = cleaner;
1386   h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1387   do_reconnect (h);
1388   if (h->mq == NULL)
1389   {
1390     GNUNET_break (0);
1391     GNUNET_CADET_disconnect (h);
1392     return NULL;
1393   }
1394   h->cls = cls;
1395   h->message_handlers = handlers;
1396   h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1397   h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1398   h->reconnect_task = NULL;
1399
1400   /* count handlers */
1401   for (h->n_handlers = 0;
1402        handlers && handlers[h->n_handlers].type;
1403        h->n_handlers++) ;
1404   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1405   return h;
1406 }
1407
1408
1409 /**
1410  * Disconnect from the cadet service. All channels will be destroyed. All channel
1411  * disconnect callbacks will be called on any still connected peers, notifying
1412  * about their disconnection. The registered inbound channel cleaner will be
1413  * called should any inbound channels still exist.
1414  *
1415  * @param handle connection to cadet to disconnect
1416  */
1417 void
1418 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1419 {
1420   struct GNUNET_CADET_Channel *ch;
1421   struct GNUNET_CADET_Channel *aux;
1422   struct GNUNET_CADET_TransmitHandle *th;
1423
1424   LOG (GNUNET_ERROR_TYPE_DEBUG,
1425        "CADET DISCONNECT\n");
1426   ch = handle->channels_head;
1427   while (NULL != ch)
1428   {
1429     aux = ch->next;
1430     if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1431     {
1432       GNUNET_break (0);
1433       LOG (GNUNET_ERROR_TYPE_DEBUG,
1434            "channel %X not destroyed\n",
1435            ntohl (ch->ccn.channel_of_client));
1436     }
1437     destroy_channel (ch,
1438                      GNUNET_YES);
1439     ch = aux;
1440   }
1441   while (NULL != (th = handle->th_head))
1442   {
1443     struct GNUNET_MessageHeader *msg;
1444
1445     /* Make sure it is an allowed packet (everything else should have been
1446      * already canceled).
1447      */
1448     GNUNET_break (GNUNET_NO == th_is_payload (th));
1449     msg = (struct GNUNET_MessageHeader *) &th[1];
1450     switch (ntohs(msg->type))
1451     {
1452       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1453       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1454       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1455       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1456       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1457       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1458       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1459       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1460       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1461       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1462         break;
1463       default:
1464         GNUNET_break (0);
1465         LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1466              GC_m2s (ntohs(msg->type)));
1467     }
1468
1469     GNUNET_CADET_notify_transmit_ready_cancel (th);
1470   }
1471
1472   if (NULL != handle->mq)
1473   {
1474     GNUNET_MQ_destroy (handle->mq);
1475     handle->mq = NULL;
1476   }
1477   if (NULL != handle->reconnect_task)
1478   {
1479     GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1480     handle->reconnect_task = NULL;
1481   }
1482
1483   GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1484   handle->ports = NULL;
1485   GNUNET_free (handle);
1486 }
1487
1488
1489 /**
1490  * Open a port to receive incomming channels.
1491  *
1492  * @param h CADET handle.
1493  * @param port Hash representing the port number.
1494  * @param new_channel Function called when an channel is received.
1495  * @param new_channel_cls Closure for @a new_channel.
1496  * @return Port handle.
1497  */
1498 struct GNUNET_CADET_Port *
1499 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1500                         const struct GNUNET_HashCode *port,
1501                         GNUNET_CADET_InboundChannelNotificationHandler
1502                             new_channel,
1503                         void *new_channel_cls)
1504 {
1505   struct GNUNET_CADET_PortMessage *msg;
1506   struct GNUNET_MQ_Envelope *env;
1507   struct GNUNET_CADET_Port *p;
1508
1509   GNUNET_assert (NULL != new_channel);
1510   p = GNUNET_new (struct GNUNET_CADET_Port);
1511   p->cadet = h;
1512   p->hash = GNUNET_new (struct GNUNET_HashCode);
1513   *p->hash = *port;
1514   p->handler = new_channel;
1515   p->cls = new_channel_cls;
1516   GNUNET_assert (GNUNET_OK ==
1517                  GNUNET_CONTAINER_multihashmap_put (h->ports,
1518                                                     p->hash,
1519                                                     p,
1520                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1521
1522   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1523   msg->port = *p->hash;
1524   GNUNET_MQ_send (h->mq, env);
1525
1526   return p;
1527 }
1528
1529 /**
1530  * Close a port opened with @a GNUNET_CADET_open_port.
1531  * The @a new_channel callback will no longer be called.
1532  *
1533  * @param p Port handle.
1534  */
1535 void
1536 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1537 {
1538   struct GNUNET_CADET_PortMessage *msg;
1539   struct GNUNET_MQ_Envelope *env;
1540
1541   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1542
1543   msg->port = *p->hash;
1544   GNUNET_MQ_send (p->cadet->mq, env);
1545   GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1546   GNUNET_free (p->hash);
1547   GNUNET_free (p);
1548 }
1549
1550
1551 /**
1552  * Create a new channel towards a remote peer.
1553  *
1554  * If the destination port is not open by any peer or the destination peer
1555  * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1556  * for this channel.
1557  *
1558  * @param h cadet handle
1559  * @param channel_ctx client's channel context to associate with the channel
1560  * @param peer peer identity the channel should go to
1561  * @param port Port hash (port number).
1562  * @param options CadetOption flag field, with all desired option bits set to 1.
1563  * @return handle to the channel
1564  */
1565 struct GNUNET_CADET_Channel *
1566 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1567                             void *channel_ctx,
1568                             const struct GNUNET_PeerIdentity *peer,
1569                             const struct GNUNET_HashCode *port,
1570                             enum GNUNET_CADET_ChannelOption options)
1571 {
1572   struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1573   struct GNUNET_MQ_Envelope *env;
1574   struct GNUNET_CADET_Channel *ch;
1575   struct GNUNET_CADET_ClientChannelNumber ccn;
1576
1577   LOG (GNUNET_ERROR_TYPE_DEBUG,
1578        "Creating new channel to %s:%u\n",
1579        GNUNET_i2s (peer), port);
1580   ccn.channel_of_client = htonl (0);
1581   ch = create_channel (h, ccn);
1582   LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", ch);
1583   LOG (GNUNET_ERROR_TYPE_DEBUG, "  number %X\n",
1584        ntohl (ch->ccn.channel_of_client));
1585   ch->ctx = channel_ctx;
1586   ch->peer = GNUNET_PEER_intern (peer);
1587
1588   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1589   msg->ccn = ch->ccn;
1590   msg->port = *port;
1591   msg->peer = *peer;
1592   msg->opt = htonl (options);
1593   GNUNET_MQ_send (h->mq,
1594                   env);
1595   return ch;
1596 }
1597
1598
1599 void
1600 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1601 {
1602   struct GNUNET_CADET_Handle *h;
1603   struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1604   struct GNUNET_MQ_Envelope *env;
1605   struct GNUNET_CADET_TransmitHandle *th;
1606   struct GNUNET_CADET_TransmitHandle *next;
1607
1608   LOG (GNUNET_ERROR_TYPE_DEBUG,
1609        "Destroying channel\n");
1610   h = channel->cadet;
1611   for  (th = h->th_head; th != NULL; th = next)
1612   {
1613     next = th->next;
1614     if (th->channel == channel)
1615     {
1616       GNUNET_break (0);
1617       if (GNUNET_YES == th_is_payload (th))
1618       {
1619         /* applications should cancel before destroying channel */
1620         LOG (GNUNET_ERROR_TYPE_WARNING,
1621              "Channel destroyed without cancelling transmission requests\n");
1622         th->notify (th->notify_cls, 0, NULL);
1623       }
1624       else
1625       {
1626         LOG (GNUNET_ERROR_TYPE_WARNING,
1627              "no meta-traffic should be queued\n");
1628       }
1629       GNUNET_CONTAINER_DLL_remove (h->th_head,
1630                                    h->th_tail,
1631                                    th);
1632       GNUNET_CADET_notify_transmit_ready_cancel (th);
1633     }
1634   }
1635
1636   env = GNUNET_MQ_msg (msg,
1637                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1638   msg->ccn = channel->ccn;
1639   GNUNET_MQ_send (h->mq,
1640                   env);
1641
1642   destroy_channel (channel,
1643                    GNUNET_YES);
1644 }
1645
1646
1647 /**
1648  * Get information about a channel.
1649  *
1650  * @param channel Channel handle.
1651  * @param option Query (GNUNET_CADET_OPTION_*).
1652  * @param ... dependant on option, currently not used
1653  *
1654  * @return Union with an answer to the query.
1655  */
1656 const union GNUNET_CADET_ChannelInfo *
1657 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1658                               enum GNUNET_CADET_ChannelOption option, ...)
1659 {
1660   static int bool_flag;
1661   const union GNUNET_CADET_ChannelInfo *ret;
1662
1663   switch (option)
1664   {
1665     case GNUNET_CADET_OPTION_NOBUFFER:
1666     case GNUNET_CADET_OPTION_RELIABLE:
1667     case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1668       if (0 != (option & channel->options))
1669         bool_flag = GNUNET_YES;
1670       else
1671         bool_flag = GNUNET_NO;
1672       ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1673       break;
1674     case GNUNET_CADET_OPTION_PEER:
1675       ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1676       break;
1677     default:
1678       GNUNET_break (0);
1679       return NULL;
1680   }
1681
1682   return ret;
1683 }
1684
1685
1686 struct GNUNET_CADET_TransmitHandle *
1687 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1688                                     int cork,
1689                                     struct GNUNET_TIME_Relative maxdelay,
1690                                     size_t notify_size,
1691                                     GNUNET_CONNECTION_TransmitReadyNotify notify,
1692                                     void *notify_cls)
1693 {
1694   struct GNUNET_CADET_TransmitHandle *th;
1695
1696   GNUNET_assert (NULL != channel);
1697   GNUNET_assert (NULL != notify);
1698   GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1699   LOG (GNUNET_ERROR_TYPE_DEBUG,
1700        "CADET NOTIFY TRANSMIT READY on channel %X allow_send is %u to %s with %u bytes\n",
1701        channel->ccn,
1702        channel->allow_send,
1703        (ntohl (channel->ccn.channel_of_client) >=
1704         GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1705        ? "origin"
1706        : "destination",
1707        (unsigned int) notify_size);
1708   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1709   {
1710     LOG (GNUNET_ERROR_TYPE_WARNING,
1711          "CADET transmit ready timeout is deprected (has no effect)\n");
1712   }
1713
1714   th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1715   th->channel = channel;
1716   th->size = notify_size;
1717   th->notify = notify;
1718   th->notify_cls = notify_cls;
1719   if (0 != channel->allow_send)
1720     th->request_data_task
1721       = GNUNET_SCHEDULER_add_now (&request_data,
1722                                   th);
1723   else
1724     add_to_queue (channel->cadet,
1725                   th);
1726   return th;
1727 }
1728
1729
1730 void
1731 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1732 {
1733   if (NULL != th->request_data_task)
1734   {
1735     GNUNET_SCHEDULER_cancel (th->request_data_task);
1736     th->request_data_task = NULL;
1737   }
1738   remove_from_queue (th);
1739   GNUNET_free (th);
1740 }
1741
1742
1743 void
1744 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1745 {
1746   send_ack (channel);
1747 }
1748
1749
1750 static void
1751 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1752 {
1753   struct GNUNET_MessageHeader *msg;
1754   struct GNUNET_MQ_Envelope *env;
1755
1756   LOG (GNUNET_ERROR_TYPE_DEBUG,
1757        " Sending %s monitor message to service\n",
1758        GC_m2s(type));
1759
1760   env = GNUNET_MQ_msg (msg, type);
1761   GNUNET_MQ_send (h->mq, env);
1762 }
1763
1764
1765 /**
1766  * Request a debug dump on the service's STDERR.
1767  *
1768  * WARNING: unstable API, likely to change in the future!
1769  *
1770  * @param h cadet handle
1771  */
1772 void
1773 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1774 {
1775   LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1776   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1777 }
1778
1779
1780 /**
1781  * Request information about peers known to the running cadet service.
1782  * The callback will be called for every peer known to the service.
1783  * Only one info request (of any kind) can be active at once.
1784  *
1785  *
1786  * WARNING: unstable API, likely to change in the future!
1787  *
1788  * @param h Handle to the cadet peer.
1789  * @param callback Function to call with the requested data.
1790  * @param callback_cls Closure for @c callback.
1791  *
1792  * @return #GNUNET_OK / #GNUNET_SYSERR
1793  */
1794 int
1795 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1796                        GNUNET_CADET_PeersCB callback,
1797                        void *callback_cls)
1798 {
1799   if (NULL != h->info_cb.peers_cb)
1800   {
1801     GNUNET_break (0);
1802     return GNUNET_SYSERR;
1803   }
1804   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1805   h->info_cb.peers_cb = callback;
1806   h->info_cls = callback_cls;
1807   return GNUNET_OK;
1808 }
1809
1810
1811 /**
1812  * Cancel a peer info request. The callback will not be called (anymore).
1813  *
1814  * WARNING: unstable API, likely to change in the future!
1815  *
1816  * @param h Cadet handle.
1817  *
1818  * @return Closure given to GNUNET_CADET_get_peers.
1819  */
1820 void *
1821 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1822 {
1823   void *cls;
1824
1825   cls = h->info_cls;
1826   h->info_cb.peers_cb = NULL;
1827   h->info_cls = NULL;
1828   return cls;
1829 }
1830
1831
1832 /**
1833  * Request information about a peer known to the running cadet peer.
1834  * The callback will be called for the tunnel once.
1835  * Only one info request (of any kind) can be active at once.
1836  *
1837  * WARNING: unstable API, likely to change in the future!
1838  *
1839  * @param h Handle to the cadet peer.
1840  * @param id Peer whose tunnel to examine.
1841  * @param callback Function to call with the requested data.
1842  * @param callback_cls Closure for @c callback.
1843  *
1844  * @return #GNUNET_OK / #GNUNET_SYSERR
1845  */
1846 int
1847 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1848                        const struct GNUNET_PeerIdentity *id,
1849                        GNUNET_CADET_PeerCB callback,
1850                        void *callback_cls)
1851 {
1852   struct GNUNET_CADET_LocalInfo *msg;
1853   struct GNUNET_MQ_Envelope *env;
1854
1855   if (NULL != h->info_cb.peer_cb)
1856   {
1857     GNUNET_break (0);
1858     return GNUNET_SYSERR;
1859   }
1860
1861   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1862   msg->peer = *id;
1863   GNUNET_MQ_send (h->mq, env);
1864
1865   h->info_cb.peer_cb = callback;
1866   h->info_cls = callback_cls;
1867   return GNUNET_OK;
1868 }
1869
1870
1871 /**
1872  * Request information about tunnels of the running cadet peer.
1873  * The callback will be called for every tunnel of the service.
1874  * Only one info request (of any kind) can be active at once.
1875  *
1876  * WARNING: unstable API, likely to change in the future!
1877  *
1878  * @param h Handle to the cadet peer.
1879  * @param callback Function to call with the requested data.
1880  * @param callback_cls Closure for @c callback.
1881  *
1882  * @return #GNUNET_OK / #GNUNET_SYSERR
1883  */
1884 int
1885 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1886                          GNUNET_CADET_TunnelsCB callback,
1887                          void *callback_cls)
1888 {
1889   if (NULL != h->info_cb.tunnels_cb)
1890   {
1891     GNUNET_break (0);
1892     return GNUNET_SYSERR;
1893   }
1894   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1895   h->info_cb.tunnels_cb = callback;
1896   h->info_cls = callback_cls;
1897   return GNUNET_OK;
1898 }
1899
1900
1901 /**
1902  * Cancel a monitor request. The monitor callback will not be called.
1903  *
1904  * @param h Cadet handle.
1905  *
1906  * @return Closure given to GNUNET_CADET_get_tunnels.
1907  */
1908 void *
1909 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1910 {
1911   void *cls;
1912
1913   h->info_cb.tunnels_cb = NULL;
1914   cls = h->info_cls;
1915   h->info_cls = NULL;
1916
1917   return cls;
1918 }
1919
1920
1921
1922 /**
1923  * Request information about a tunnel of the running cadet peer.
1924  * The callback will be called for the tunnel once.
1925  * Only one info request (of any kind) can be active at once.
1926  *
1927  * WARNING: unstable API, likely to change in the future!
1928  *
1929  * @param h Handle to the cadet peer.
1930  * @param id Peer whose tunnel to examine.
1931  * @param callback Function to call with the requested data.
1932  * @param callback_cls Closure for @c callback.
1933  *
1934  * @return #GNUNET_OK / #GNUNET_SYSERR
1935  */
1936 int
1937 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1938                         const struct GNUNET_PeerIdentity *id,
1939                         GNUNET_CADET_TunnelCB callback,
1940                         void *callback_cls)
1941 {
1942   struct GNUNET_CADET_LocalInfo *msg;
1943   struct GNUNET_MQ_Envelope *env;
1944
1945   if (NULL != h->info_cb.tunnel_cb)
1946   {
1947     GNUNET_break (0);
1948     return GNUNET_SYSERR;
1949   }
1950
1951   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1952   msg->peer = *id;
1953   GNUNET_MQ_send (h->mq, env);
1954
1955   h->info_cb.tunnel_cb = callback;
1956   h->info_cls = callback_cls;
1957   return GNUNET_OK;
1958 }
1959
1960
1961 /**
1962  * Request information about a specific channel of the running cadet peer.
1963  *
1964  * WARNING: unstable API, likely to change in the future!
1965  * FIXME Add destination option.
1966  *
1967  * @param h Handle to the cadet peer.
1968  * @param initiator ID of the owner of the channel.
1969  * @param channel_number Channel number.
1970  * @param callback Function to call with the requested data.
1971  * @param callback_cls Closure for @c callback.
1972  *
1973  * @return #GNUNET_OK / #GNUNET_SYSERR
1974  */
1975 int
1976 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1977                            struct GNUNET_PeerIdentity *initiator,
1978                            unsigned int channel_number,
1979                            GNUNET_CADET_ChannelCB callback,
1980                            void *callback_cls)
1981 {
1982   struct GNUNET_CADET_LocalInfo *msg;
1983   struct GNUNET_MQ_Envelope *env;
1984
1985   if (NULL != h->info_cb.channel_cb)
1986   {
1987     GNUNET_break (0);
1988     return GNUNET_SYSERR;
1989   }
1990
1991   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
1992   msg->peer = *initiator;
1993   msg->ccn.channel_of_client = htonl (channel_number);
1994   GNUNET_MQ_send (h->mq, env);
1995
1996   h->info_cb.channel_cb = callback;
1997   h->info_cls = callback_cls;
1998   return GNUNET_OK;
1999 }
2000
2001
2002 /**
2003  * Function called to notify a client about the connection
2004  * begin ready to queue more data.  "buf" will be
2005  * NULL and "size" zero if the connection was closed for
2006  * writing in the meantime.
2007  *
2008  * @param cls closure
2009  * @param size number of bytes available in buf
2010  * @param buf where the callee should write the message
2011  * @return number of bytes written to buf
2012  */
2013 static size_t
2014 cadet_mq_ntr (void *cls, size_t size,
2015              void *buf)
2016 {
2017   struct GNUNET_MQ_Handle *mq = cls;
2018   struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2019   const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2020   uint16_t msize;
2021
2022   state->th = NULL;
2023   if (NULL == buf)
2024   {
2025     GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2026     return 0;
2027   }
2028   msize = ntohs (msg->size);
2029   GNUNET_assert (msize <= size);
2030   GNUNET_memcpy (buf, msg, msize);
2031   GNUNET_MQ_impl_send_continue (mq);
2032   return msize;
2033 }
2034
2035
2036 /**
2037  * Signature of functions implementing the
2038  * sending functionality of a message queue.
2039  *
2040  * @param mq the message queue
2041  * @param msg the message to send
2042  * @param impl_state state of the implementation
2043  */
2044 static void
2045 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2046                     const struct GNUNET_MessageHeader *msg,
2047                     void *impl_state)
2048 {
2049   struct CadetMQState *state = impl_state;
2050
2051   GNUNET_assert (NULL == state->th);
2052   state->th =
2053       GNUNET_CADET_notify_transmit_ready (state->channel,
2054                                          /* FIXME: add option for corking */
2055                                          GNUNET_NO,
2056                                          GNUNET_TIME_UNIT_FOREVER_REL,
2057                                          ntohs (msg->size),
2058                                          &cadet_mq_ntr, mq);
2059
2060 }
2061
2062
2063 /**
2064  * Signature of functions implementing the
2065  * destruction of a message queue.
2066  * Implementations must not free 'mq', but should
2067  * take care of 'impl_state'.
2068  *
2069  * @param mq the message queue to destroy
2070  * @param impl_state state of the implementation
2071  */
2072 static void
2073 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2074                        void *impl_state)
2075 {
2076   struct CadetMQState *state = impl_state;
2077
2078   if (NULL != state->th)
2079     GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2080
2081   GNUNET_free (state);
2082 }
2083
2084
2085 /**
2086  * Create a message queue for a cadet channel.
2087  * The message queue can only be used to transmit messages,
2088  * not to receive them.
2089  *
2090  * @param channel the channel to create the message qeue for
2091  * @return a message queue to messages over the channel
2092  */
2093 struct GNUNET_MQ_Handle *
2094 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2095 {
2096   struct GNUNET_MQ_Handle *mq;
2097   struct CadetMQState *state;
2098
2099   state = GNUNET_new (struct CadetMQState);
2100   state->channel = channel;
2101
2102   mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2103                                       &cadet_mq_destroy_impl,
2104                                       NULL, /* FIXME: cancel impl. */
2105                                       state,
2106                                       NULL, /* no msg handlers */
2107                                       NULL, /* no err handlers */
2108                                       NULL); /* no handler cls */
2109   return mq;
2110 }
2111
2112
2113 /**
2114  * Transitional function to convert an unsigned int port to a hash value.
2115  * WARNING: local static value returned, NOT reentrant!
2116  * WARNING: do not use this function for new code!
2117  *
2118  * @param port Numerical port (unsigned int format).
2119  *
2120  * @return A GNUNET_HashCode usable for the new CADET API.
2121  */
2122 const struct GNUNET_HashCode *
2123 GC_u2h (uint32_t port)
2124 {
2125   static struct GNUNET_HashCode hash;
2126
2127   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2128               "This is a transitional function, "
2129               "use proper crypto hashes as CADET ports\n");
2130   GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
2131
2132   return &hash;
2133 }