API documentation, logging
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet-new_channel.c
1
2 /*
3      This file is part of GNUnet.
4      Copyright (C) 2001-2017 GNUnet e.V.
5
6      GNUnet is free software; you can redistribute it and/or modify
7      it under the terms of the GNU General Public License as published
8      by the Free Software Foundation; either version 3, or (at your
9      option) any later version.
10
11      GNUnet is distributed in the hope that it will be useful, but
12      WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      General Public License for more details.
15
16      You should have received a copy of the GNU General Public License
17      along with GNUnet; see the file COPYING.  If not, write to the
18      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19      Boston, MA 02110-1301, USA.
20 */
21 /**
22  * @file cadet/gnunet-service-cadet-new_channel.c
23  * @brief logical links between CADET clients
24  * @author Bartlomiej Polot
25  * @author Christian Grothoff
26  *
27  * TODO:
28  * - introduce shutdown so we can have half-closed channels, modify
29  *   destroy to include MID to have FIN-ACK equivalents, etc.
30  * - estimate max bandwidth using bursts and use to for CONGESTION CONTROL!
31  * - check that '0xFFULL' really is sufficient for flow control!
32  * - revisit handling of 'unreliable' traffic!
33  * - revisit handling of 'out-of-order' option, especially in combination with/without 'reliable'.
34  * - figure out flow control without ACKs (unreliable traffic!)
35  */
36 #include "platform.h"
37 #include "gnunet_util_lib.h"
38 #include "cadet.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet-service-cadet-new.h"
41 #include "gnunet-service-cadet-new_channel.h"
42 #include "gnunet-service-cadet-new_connection.h"
43 #include "gnunet-service-cadet-new_tunnels.h"
44 #include "gnunet-service-cadet-new_peer.h"
45 #include "gnunet-service-cadet-new_paths.h"
46
47 #define LOG(level,...) GNUNET_log_from (level,"cadet-chn",__VA_ARGS__)
48
49 /**
50  * How long do we initially wait before retransmitting?
51  */
52 #define CADET_INITIAL_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250)
53
54 /**
55  * How long do we wait before dropping state about incoming
56  * connection to closed port?
57  */
58 #define TIMEOUT_CLOSED_PORT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
59
60
61 /**
62  * All the states a connection can be in.
63  */
64 enum CadetChannelState
65 {
66   /**
67    * Uninitialized status, should never appear in operation.
68    */
69   CADET_CHANNEL_NEW,
70
71   /**
72    * Connection create message sent, waiting for ACK.
73    */
74   CADET_CHANNEL_OPEN_SENT,
75
76   /**
77    * Connection confirmed, ready to carry traffic.
78    */
79   CADET_CHANNEL_READY
80 };
81
82
83 /**
84  * Info needed to retry a message in case it gets lost.
85  * Note that we DO use this structure also for unreliable
86  * messages.
87  */
88 struct CadetReliableMessage
89 {
90   /**
91    * Double linked list, FIFO style
92    */
93   struct CadetReliableMessage *next;
94
95   /**
96    * Double linked list, FIFO style
97    */
98   struct CadetReliableMessage *prev;
99
100   /**
101    * Which channel is this message in?
102    */
103   struct CadetChannel *ch;
104
105   /**
106    * Entry in the tunnels queue for this message, NULL if it has left
107    * the tunnel.  Used to cancel transmission in case we receive an
108    * ACK in time.
109    */
110   struct CadetTunnelQueueEntry *qe;
111
112   /**
113    * How soon should we retry if we fail to get an ACK?
114    * Messages in the queue are sorted by this value.
115    */
116   struct GNUNET_TIME_Absolute next_retry;
117
118   /**
119    * How long do we wait for an ACK after transmission?
120    * Use for the back-off calculation.
121    */
122   struct GNUNET_TIME_Relative retry_delay;
123
124   /**
125    * Data message we are trying to send.
126    */
127   struct GNUNET_CADET_ChannelAppDataMessage data_message;
128
129   /* followed by variable-size payload */
130 };
131
132
133 /**
134  * List of received out-of-order data messages.
135  */
136 struct CadetOutOfOrderMessage
137 {
138   /**
139    * Double linked list, FIFO style
140    */
141   struct CadetOutOfOrderMessage *next;
142
143   /**
144    * Double linked list, FIFO style
145    */
146   struct CadetOutOfOrderMessage *prev;
147
148   /**
149    * ID of the message (messages up to this point needed
150    * before we give this one to the client).
151    */
152   struct ChannelMessageIdentifier mid;
153
154   /**
155    * The envelope with the payload of the out-of-order message
156    */
157   struct GNUNET_MQ_Envelope *env;
158
159 };
160
161
162 /**
163  * Struct containing all information regarding a channel to a remote client.
164  */
165 struct CadetChannel
166 {
167   /**
168    * Tunnel this channel is in.
169    */
170   struct CadetTunnel *t;
171
172   /**
173    * Client owner of the tunnel, if any.
174    * (Used if this channel represends the initiating end of the tunnel.)
175    */
176   struct CadetClient *owner;
177
178   /**
179    * Client destination of the tunnel, if any.
180    * (Used if this channel represents the listening end of the tunnel.)
181    */
182   struct CadetClient *dest;
183
184   /**
185    * Last entry in the tunnel's queue relating to control messages
186    * (#GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN or
187    * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK).  Used to cancel
188    * transmission in case we receive updated information.
189    */
190   struct CadetTunnelQueueEntry *last_control_qe;
191
192   /**
193    * Head of DLL of messages sent and not yet ACK'd.
194    */
195   struct CadetReliableMessage *head_sent;
196
197   /**
198    * Tail of DLL of messages sent and not yet ACK'd.
199    */
200   struct CadetReliableMessage *tail_sent;
201
202   /**
203    * Head of DLL of messages received out of order or while client was unready.
204    */
205   struct CadetOutOfOrderMessage *head_recv;
206
207   /**
208    * Tail DLL of messages received out of order or while client was unready.
209    */
210   struct CadetOutOfOrderMessage *tail_recv;
211
212   /**
213    * Task to resend/poll in case no ACK is received.
214    */
215   struct GNUNET_SCHEDULER_Task *retry_control_task;
216
217     /**
218    * Task to resend/poll in case no ACK is received.
219    */
220   struct GNUNET_SCHEDULER_Task *retry_data_task;
221
222   /**
223    * Last time the channel was used
224    */
225   struct GNUNET_TIME_Absolute timestamp;
226
227   /**
228    * Destination port of the channel.
229    */
230   struct GNUNET_HashCode port;
231
232   /**
233    * Counter for exponential backoff.
234    */
235   struct GNUNET_TIME_Relative retry_time;
236
237   /**
238    * How long does it usually take to get an ACK.
239    */
240   struct GNUNET_TIME_Relative expected_delay;
241
242   /**
243    * Bitfield of already-received messages past @e mid_recv.
244    */
245   uint64_t mid_futures;
246
247   /**
248    * Next MID expected for incoming traffic.
249    */
250   struct ChannelMessageIdentifier mid_recv;
251
252   /**
253    * Next MID to use for outgoing traffic.
254    */
255   struct ChannelMessageIdentifier mid_send;
256
257   /**
258    * Total (reliable) messages pending ACK for this channel.
259    */
260   unsigned int pending_messages;
261
262   /**
263    * Maximum (reliable) messages pending ACK for this channel
264    * before we throttle the client.
265    */
266   unsigned int max_pending_messages;
267
268   /**
269    * Number identifying this channel in its tunnel.
270    */
271   struct GNUNET_CADET_ChannelTunnelNumber ctn;
272
273   /**
274    * Local tunnel number for local client owning the channel.
275    * ( >= #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
276    */
277   struct GNUNET_CADET_ClientChannelNumber ccn;
278
279   /**
280    * Channel state.
281    */
282   enum CadetChannelState state;
283
284   /**
285    * Can we send data to the client?
286    */
287   int client_ready;
288
289   /**
290    * Can the client send data to us?
291    */
292   int client_allowed;
293
294   /**
295    * Is the tunnel bufferless (minimum latency)?
296    */
297   int nobuffer;
298
299   /**
300    * Is the tunnel reliable?
301    */
302   int reliable;
303
304   /**
305    * Is the tunnel out-of-order?
306    */
307   int out_of_order;
308
309   /**
310    * Flag to signal the destruction of the channel.  If this is set to
311    * #GNUNET_YES the channel will be destroyed once the queue is
312    * empty.
313    */
314   int destroy;
315
316 };
317
318
319 /**
320  * Get the static string for identification of the channel.
321  *
322  * @param ch Channel.
323  *
324  * @return Static string with the channel IDs.
325  */
326 const char *
327 GCCH_2s (const struct CadetChannel *ch)
328 {
329   static char buf[128];
330
331   GNUNET_snprintf (buf,
332                    sizeof (buf),
333                    "Channel %s:%s ctn:%X(%X)",
334                    GNUNET_i2s (GCP_get_id (GCT_get_destination (ch->t))),
335                    GNUNET_h2s (&ch->port),
336                    ch->ctn,
337                    ntohl (ch->ccn.channel_of_client));
338   return buf;
339 }
340
341
342 /**
343  * Get the channel's public ID.
344  *
345  * @param ch Channel.
346  *
347  * @return ID used to identify the channel with the remote peer.
348  */
349 struct GNUNET_CADET_ChannelTunnelNumber
350 GCCH_get_id (const struct CadetChannel *ch)
351 {
352   return ch->ctn;
353 }
354
355
356 /**
357  * Destroy the given channel.
358  *
359  * @param ch channel to destroy
360  */
361 static void
362 channel_destroy (struct CadetChannel *ch)
363 {
364   struct CadetReliableMessage *crm;
365   struct CadetOutOfOrderMessage *com;
366
367   while (NULL != (crm = ch->head_sent))
368   {
369     GNUNET_assert (ch == crm->ch);
370     if (NULL != crm->qe)
371     {
372       GCT_send_cancel (crm->qe);
373       crm->qe = NULL;
374     }
375     GNUNET_CONTAINER_DLL_remove (ch->head_sent,
376                                  ch->tail_sent,
377                                  crm);
378     GNUNET_free (crm);
379   }
380   while (NULL != (com = ch->head_recv))
381   {
382     GNUNET_CONTAINER_DLL_remove (ch->head_recv,
383                                  ch->tail_recv,
384                                  com);
385     GNUNET_MQ_discard (com->env);
386     GNUNET_free (com);
387   }
388   if (NULL != ch->last_control_qe)
389   {
390     GCT_send_cancel (ch->last_control_qe);
391     ch->last_control_qe = NULL;
392   }
393   if (NULL != ch->retry_data_task)
394   {
395     GNUNET_SCHEDULER_cancel (ch->retry_data_task);
396     ch->retry_data_task = NULL;
397   }
398   if (NULL != ch->retry_control_task)
399   {
400     GNUNET_SCHEDULER_cancel (ch->retry_control_task);
401     ch->retry_control_task = NULL;
402   }
403   GCT_remove_channel (ch->t,
404                       ch,
405                       ch->ctn);
406   GNUNET_free (ch);
407 }
408
409
410 /**
411  * Send a channel create message.
412  *
413  * @param cls Channel for which to send.
414  */
415 static void
416 send_channel_open (void *cls);
417
418
419 /**
420  * Function called once the tunnel confirms that we sent the
421  * create message.  Delays for a bit until we retry.
422  *
423  * @param cls our `struct CadetChannel`.
424  */
425 static void
426 channel_open_sent_cb (void *cls)
427 {
428   struct CadetChannel *ch = cls;
429
430   GNUNET_assert (NULL != ch->last_control_qe);
431   ch->last_control_qe = NULL;
432   ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
433   LOG (GNUNET_ERROR_TYPE_DEBUG,
434        "Sent CHANNEL_OPEN on %s, retrying in %s\n",
435        GCCH_2s (ch),
436        GNUNET_STRINGS_relative_time_to_string (ch->retry_time,
437                                                GNUNET_YES));
438   ch->retry_control_task
439     = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
440                                     &send_channel_open,
441                                     ch);
442 }
443
444
445 /**
446  * Send a channel open message.
447  *
448  * @param cls Channel for which to send.
449  */
450 static void
451 send_channel_open (void *cls)
452 {
453   struct CadetChannel *ch = cls;
454   struct GNUNET_CADET_ChannelOpenMessage msgcc;
455   uint32_t options;
456
457   ch->retry_control_task = NULL;
458   LOG (GNUNET_ERROR_TYPE_DEBUG,
459        "Sending CHANNEL_OPEN message for %s\n",
460        GCCH_2s (ch));
461   options = 0;
462   if (ch->nobuffer)
463     options |= GNUNET_CADET_OPTION_NOBUFFER;
464   if (ch->reliable)
465     options |= GNUNET_CADET_OPTION_RELIABLE;
466   if (ch->out_of_order)
467     options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
468   msgcc.header.size = htons (sizeof (msgcc));
469   msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
470   msgcc.opt = htonl (options);
471   msgcc.port = ch->port;
472   msgcc.ctn = ch->ctn;
473   ch->state = CADET_CHANNEL_OPEN_SENT;
474   ch->last_control_qe = GCT_send (ch->t,
475                                   &msgcc.header,
476                                   &channel_open_sent_cb,
477                                   ch);
478 }
479
480
481 /**
482  * Function called once and only once after a channel was bound
483  * to its tunnel via #GCT_add_channel() is ready for transmission.
484  * Note that this is only the case for channels that this peer
485  * initiates, as for incoming channels we assume that they are
486  * ready for transmission immediately upon receiving the open
487  * message.  Used to bootstrap the #GCT_send() process.
488  *
489  * @param ch the channel for which the tunnel is now ready
490  */
491 void
492 GCCH_tunnel_up (struct CadetChannel *ch)
493 {
494   GNUNET_assert (NULL == ch->retry_control_task);
495   LOG (GNUNET_ERROR_TYPE_DEBUG,
496        "Tunnel up, sending CHANNEL_OPEN on %s now\n",
497        GCCH_2s (ch));
498   ch->retry_control_task
499     = GNUNET_SCHEDULER_add_now (&send_channel_open,
500                                 ch);
501 }
502
503
504 /**
505  * Create a new channel.
506  *
507  * @param owner local client owning the channel
508  * @param ccn local number of this channel at the @a owner
509  * @param destination peer to which we should build the channel
510  * @param port desired port at @a destination
511  * @param options options for the channel
512  * @return handle to the new channel
513  */
514 struct CadetChannel *
515 GCCH_channel_local_new (struct CadetClient *owner,
516                         struct GNUNET_CADET_ClientChannelNumber ccn,
517                         struct CadetPeer *destination,
518                         const struct GNUNET_HashCode *port,
519                         uint32_t options)
520 {
521   struct CadetChannel *ch;
522
523   ch = GNUNET_new (struct CadetChannel);
524   ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
525   ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
526   ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
527   ch->max_pending_messages = (ch->nobuffer) ? 1 : 32; /* FIXME: 32!? Do not hardcode! */
528   ch->owner = owner;
529   ch->ccn = ccn;
530   ch->port = *port;
531   ch->t = GCP_get_tunnel (destination,
532                           GNUNET_YES);
533   ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
534   ch->ctn = GCT_add_channel (ch->t,
535                              ch);
536   GNUNET_STATISTICS_update (stats,
537                             "# channels",
538                             1,
539                             GNUNET_NO);
540   LOG (GNUNET_ERROR_TYPE_DEBUG,
541        "Created channel to port %s at peer %s for %s using %s\n",
542        GNUNET_h2s (port),
543        GCP_2s (destination),
544        GSC_2s (owner),
545        GCT_2s (ch->t));
546   return ch;
547 }
548
549
550 /**
551  * We had an incoming channel to a port that is closed.
552  * It has not been opened for a while, drop it.
553  *
554  * @param cls the channel to drop
555  */
556 static void
557 timeout_closed_cb (void *cls)
558 {
559   struct CadetChannel *ch = cls;
560
561   ch->retry_control_task = NULL;
562   LOG (GNUNET_ERROR_TYPE_DEBUG,
563        "Closing incoming channel to port %s from peer %s due to timeout\n",
564        GNUNET_h2s (&ch->port),
565        GCP_2s (GCT_get_destination (ch->t)));
566   channel_destroy (ch);
567 }
568
569
570 /**
571  * Create a new channel based on a request coming in over the network.
572  *
573  * @param t tunnel to the remote peer
574  * @param ctn identifier of this channel in the tunnel
575  * @param port desired local port
576  * @param options options for the channel
577  * @return handle to the new channel
578  */
579 struct CadetChannel *
580 GCCH_channel_incoming_new (struct CadetTunnel *t,
581                            struct GNUNET_CADET_ChannelTunnelNumber ctn,
582                            const struct GNUNET_HashCode *port,
583                            uint32_t options)
584 {
585   struct CadetChannel *ch;
586   struct CadetClient *c;
587
588   ch = GNUNET_new (struct CadetChannel);
589   ch->port = *port;
590   ch->t = t;
591   ch->ctn = ctn;
592   ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
593   ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
594   ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
595   ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
596   ch->max_pending_messages = (ch->nobuffer) ? 1 : 32; /* FIXME: 32!? Do not hardcode! */
597   GNUNET_STATISTICS_update (stats,
598                             "# channels",
599                             1,
600                             GNUNET_NO);
601
602   c = GNUNET_CONTAINER_multihashmap_get (open_ports,
603                                          port);
604   if (NULL == c)
605   {
606     /* port closed, wait for it to possibly open */
607     (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
608                                               port,
609                                               ch,
610                                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
611     ch->retry_control_task
612       = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
613                                       &timeout_closed_cb,
614                                       ch);
615     LOG (GNUNET_ERROR_TYPE_DEBUG,
616          "Created loose incoming channel to port %s from peer %s\n",
617          GNUNET_h2s (&ch->port),
618          GCP_2s (GCT_get_destination (ch->t)));
619   }
620   else
621   {
622     GCCH_bind (ch,
623                c);
624   }
625   GNUNET_STATISTICS_update (stats,
626                             "# channels",
627                             1,
628                             GNUNET_NO);
629   return ch;
630 }
631
632
633 /**
634  * Function called once the tunnel confirms that we sent the
635  * ACK message.  Just remembers it was sent, we do not expect
636  * ACKs for ACKs ;-).
637  *
638  * @param cls our `struct CadetChannel`.
639  */
640 static void
641 send_ack_cb (void *cls)
642 {
643   struct CadetChannel *ch = cls;
644
645   GNUNET_assert (NULL != ch->last_control_qe);
646   ch->last_control_qe = NULL;
647 }
648
649
650 /**
651  * Compute and send the current ACK to the other peer.
652  *
653  * @param ch channel to send the ACK for
654  */
655 static void
656 send_channel_data_ack (struct CadetChannel *ch)
657 {
658   struct GNUNET_CADET_ChannelDataAckMessage msg;
659
660   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
661   msg.header.size = htons (sizeof (msg));
662   msg.ctn = ch->ctn;
663   msg.mid.mid = htonl (ntohl (ch->mid_recv.mid) - 1);
664   msg.futures = GNUNET_htonll (ch->mid_futures);
665   if (NULL != ch->last_control_qe)
666     GCT_send_cancel (ch->last_control_qe);
667   ch->last_control_qe = GCT_send (ch->t,
668                                   &msg.header,
669                                   &send_ack_cb,
670                                   ch);
671 }
672
673
674 /**
675  * Send our initial ACK to the client confirming that the
676  * connection is up.
677  *
678  * @param cls the `struct CadetChannel`
679  */
680 static void
681 send_open_ack (void *cls)
682 {
683   struct CadetChannel *ch = cls;
684   struct GNUNET_CADET_ChannelManageMessage msg;
685
686   LOG (GNUNET_ERROR_TYPE_DEBUG,
687        "Sending CHANNEL_OPEN_ACK on channel %s\n",
688        GCCH_2s (ch));
689   ch->retry_control_task = NULL;
690   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
691   msg.header.size = htons (sizeof (msg));
692   msg.reserved = htonl (0);
693   msg.ctn = ch->ctn;
694   if (NULL != ch->last_control_qe)
695     GCT_send_cancel (ch->last_control_qe);
696   ch->last_control_qe = GCT_send (ch->t,
697                                   &msg.header,
698                                   &send_ack_cb,
699                                   ch);
700 }
701
702
703 /**
704  * We got a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for
705  * this channel.  If the binding was successful, (re)transmit the
706  * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
707  *
708  * @param ch channel that got the duplicate open
709  */
710 void
711 GCCH_handle_duplicate_open (struct CadetChannel *ch)
712 {
713   if (NULL == ch->dest)
714   {
715     LOG (GNUNET_ERROR_TYPE_DEBUG,
716          "Ignoring duplicate channel OPEN on %s: port is closed\n",
717          GCCH_2s (ch));
718     return;
719   }
720   if (NULL != ch->retry_control_task)
721   {
722     LOG (GNUNET_ERROR_TYPE_DEBUG,
723          "Ignoring duplicate channel OPEN on %s: control message is pending\n",
724          GCCH_2s (ch));
725     return;
726   }
727   LOG (GNUNET_ERROR_TYPE_DEBUG,
728        "Retransmitting OPEN_ACK on channel %s\n",
729        GCCH_2s (ch));
730   ch->retry_control_task
731     = GNUNET_SCHEDULER_add_now (&send_open_ack,
732                                 ch);
733 }
734
735
736 /**
737  * Send a LOCAL ACK to the client to solicit more messages.
738  *
739  * @param ch channel the ack is for
740  * @param c client to send the ACK to
741  */
742 static void
743 send_ack_to_client (struct CadetChannel *ch,
744                     struct CadetClient *c)
745 {
746   struct GNUNET_MQ_Envelope *env;
747   struct GNUNET_CADET_LocalAck *ack;
748
749   env = GNUNET_MQ_msg (ack,
750                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
751   ack->ccn = ch->ccn;
752   GSC_send_to_client (c,
753                       env);
754 }
755
756
757 /**
758  * A client is bound to the port that we have a channel
759  * open to.  Send the acknowledgement for the connection
760  * request and establish the link with the client.
761  *
762  * @param ch open incoming channel
763  * @param c client listening on the respective port
764  */
765 void
766 GCCH_bind (struct CadetChannel *ch,
767            struct CadetClient *c)
768 {
769   struct GNUNET_MQ_Envelope *env;
770   struct GNUNET_CADET_LocalChannelCreateMessage *tcm;
771   uint32_t options;
772
773   LOG (GNUNET_ERROR_TYPE_DEBUG,
774        "Binding %s from %s to port %s of %s\n",
775        GCCH_2s (ch),
776        GCT_2s (ch->t),
777        GNUNET_h2s (&ch->port),
778        GSC_2s (c));
779   if (NULL != ch->retry_control_task)
780   {
781     /* there might be a timeout task here */
782     GNUNET_SCHEDULER_cancel (ch->retry_control_task);
783     ch->retry_control_task = NULL;
784   }
785   options = 0;
786   if (ch->nobuffer)
787     options |= GNUNET_CADET_OPTION_NOBUFFER;
788   if (ch->reliable)
789     options |= GNUNET_CADET_OPTION_RELIABLE;
790   if (ch->out_of_order)
791     options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
792   ch->dest = c;
793   ch->ccn = GSC_bind (c,
794                       ch,
795                       GCT_get_destination (ch->t),
796                       &ch->port,
797                       options);
798   ch->mid_recv.mid = htonl (1); /* The CONNECT counts as message 0! */
799
800   /* notify other peer that we accepted the connection */
801   ch->retry_control_task
802     = GNUNET_SCHEDULER_add_now (&send_open_ack,
803                                 ch);
804   /* give client it's initial supply of ACKs */
805   env = GNUNET_MQ_msg (tcm,
806                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
807   tcm->ccn = ch->ccn;
808   tcm->peer = *GCP_get_id (GCT_get_destination (ch->t));
809   tcm->port = ch->port;
810   tcm->opt = htonl (options);
811   GSC_send_to_client (ch->dest,
812                       env);
813   for (unsigned int i=0;i<ch->max_pending_messages;i++)
814     send_ack_to_client (ch,
815                         ch->dest);
816 }
817
818
819 /**
820  * Destroy locally created channel.  Called by the
821  * local client, so no need to tell the client.
822  *
823  * @param ch channel to destroy
824  */
825 void
826 GCCH_channel_local_destroy (struct CadetChannel *ch)
827 {
828   LOG (GNUNET_ERROR_TYPE_DEBUG,
829        "Local client asks for destruction of %s which it initiated\n",
830        GCCH_2s (ch));
831   if (GNUNET_YES == ch->destroy)
832   {
833     /* other end already destroyed, with the local client gone, no need
834        to finish transmissions, just destroy immediately. */
835     channel_destroy (ch);
836     return;
837   }
838   if (NULL != ch->head_sent)
839   {
840     /* allow send queue to train first */
841     ch->destroy = GNUNET_YES;
842     return;
843   }
844   /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
845   if (CADET_CHANNEL_NEW != ch->state)
846     GCT_send_channel_destroy (ch->t,
847                               ch->ctn);
848   /* Now finish our clean up */
849   channel_destroy (ch);
850 }
851
852
853 /**
854  * Destroy channel that was incoming.  Called by the
855  * local client, so no need to tell the client.
856  *
857  * @param ch channel to destroy
858  */
859 void
860 GCCH_channel_incoming_destroy (struct CadetChannel *ch)
861 {
862   LOG (GNUNET_ERROR_TYPE_DEBUG,
863        "Local client asks for destruction of %s which it accepted\n",
864        GCCH_2s (ch));
865   if (GNUNET_YES == ch->destroy)
866   {
867     /* other end already destroyed, with the remote client gone, no need
868        to finish transmissions, just destroy immediately. */
869     channel_destroy (ch);
870     return;
871   }
872   if (NULL != ch->head_recv)
873   {
874     /* allow local client to see all data first */
875     ch->destroy = GNUNET_YES;
876     return;
877   }
878   /* Nothing left to do, just finish destruction */
879   GCT_send_channel_destroy (ch->t,
880                             ch->ctn);
881   channel_destroy (ch);
882 }
883
884
885 /**
886  * We got an acknowledgement for the creation of the channel
887  * (the port is open on the other side). Begin transmissions.
888  *
889  * @param ch channel to destroy
890  */
891 void
892 GCCH_handle_channel_open_ack (struct CadetChannel *ch)
893 {
894   switch (ch->state)
895   {
896   case CADET_CHANNEL_NEW:
897     /* this should be impossible */
898     GNUNET_break (0);
899     break;
900   case CADET_CHANNEL_OPEN_SENT:
901     if (NULL == ch->owner)
902     {
903       /* We're not the owner, wrong direction! */
904       GNUNET_break_op (0);
905       return;
906     }
907     LOG (GNUNET_ERROR_TYPE_DEBUG,
908          "Received channel OPEN_ACK for waiting %s, entering READY state\n",
909          GCCH_2s (ch));
910     GNUNET_SCHEDULER_cancel (ch->retry_control_task);
911     ch->retry_control_task = NULL;
912     ch->state = CADET_CHANNEL_READY;
913     /* On first connect, send client as many ACKs as we allow messages
914        to be buffered! */
915     for (unsigned int i=0;i<ch->max_pending_messages;i++)
916       send_ack_to_client (ch,
917                           ch->owner);
918     break;
919   case CADET_CHANNEL_READY:
920     /* duplicate ACK, maybe we retried the CREATE. Ignore. */
921     LOG (GNUNET_ERROR_TYPE_DEBUG,
922          "Received duplicate channel OPEN_ACK for %s\n",
923          GCCH_2s (ch));
924     GNUNET_STATISTICS_update (stats,
925                               "# duplicate CREATE_ACKs",
926                               1,
927                               GNUNET_NO);
928     break;
929   }
930 }
931
932
933 /**
934  * Test if element @a e1 comes before element @a e2.
935  *
936  * TODO: use opportunity to create generic list insertion sort
937  * logic in container!
938  *
939  * @param cls closure, our `struct CadetChannel`
940  * @param e1 an element of to sort
941  * @param e2 another element to sort
942  * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
943  */
944 static int
945 is_before (void *cls,
946            void *e1,
947            void *e2)
948 {
949   struct CadetOutOfOrderMessage *m1 = e1;
950   struct CadetOutOfOrderMessage *m2 = e2;
951   uint32_t v1 = ntohl (m1->mid.mid);
952   uint32_t v2 = ntohl (m2->mid.mid);
953   uint32_t delta;
954
955   delta = v1 - v2;
956   if (delta > (uint32_t) INT_MAX)
957   {
958     /* in overflow range, we can safely assume we wrapped around */
959     return GNUNET_NO;
960   }
961   else
962   {
963     return GNUNET_YES;
964   }
965 }
966
967
968 /**
969  * We got payload data for a channel.  Pass it on to the client
970  * and send an ACK to the other end (once flow control allows it!)
971  *
972  * @param ch channel that got data
973  */
974 void
975 GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
976                                     const struct GNUNET_CADET_ChannelAppDataMessage *msg)
977 {
978   struct GNUNET_MQ_Envelope *env;
979   struct GNUNET_CADET_LocalData *ld;
980   struct CadetOutOfOrderMessage *com;
981   size_t payload_size;
982
983   payload_size = ntohs (msg->header.size) - sizeof (*msg);
984   LOG (GNUNET_ERROR_TYPE_DEBUG,
985        "Receicved %u bytes of application data on %s\n",
986        (unsigned int) payload_size,
987        GCCH_2s (ch));
988   env = GNUNET_MQ_msg_extra (ld,
989                              payload_size,
990                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
991   ld->ccn = ch->ccn;
992   GNUNET_memcpy (&ld[1],
993                  &msg[1],
994                  payload_size);
995   if ( (GNUNET_YES == ch->client_ready) &&
996        ( (GNUNET_YES == ch->out_of_order) ||
997          (msg->mid.mid == ch->mid_recv.mid) ) )
998   {
999     GSC_send_to_client (ch->owner ? ch->owner : ch->dest,
1000                         env);
1001     ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid));
1002     ch->mid_futures >>= 1;
1003   }
1004   else
1005   {
1006     /* FIXME-SECURITY: if the element is WAY too far ahead,
1007        drop it (can't buffer too much!) */
1008     com = GNUNET_new (struct CadetOutOfOrderMessage);
1009     com->mid = msg->mid;
1010     com->env = env;
1011     /* sort into list ordered by "is_before" */
1012     if ( (NULL == ch->head_recv) ||
1013          (GNUNET_YES == is_before (ch,
1014                                    com,
1015                                    ch->head_recv)) )
1016     {
1017       GNUNET_CONTAINER_DLL_insert (ch->head_recv,
1018                                    ch->tail_recv,
1019                                    com);
1020     }
1021     else
1022     {
1023       struct CadetOutOfOrderMessage *pos;
1024
1025       for (pos = ch->head_recv;
1026            NULL != pos;
1027            pos = pos->next)
1028       {
1029         if (GNUNET_YES !=
1030             is_before (ch,
1031                        pos,
1032                        com))
1033           break;
1034       }
1035       if (NULL == pos)
1036         GNUNET_CONTAINER_DLL_insert_tail (ch->head_recv,
1037                                           ch->tail_recv,
1038                                           com);
1039       else
1040         GNUNET_CONTAINER_DLL_insert_after (ch->head_recv,
1041                                            ch->tail_recv,
1042                                            com,
1043                                            pos->prev);
1044     }
1045   }
1046 }
1047
1048
1049 /**
1050  * We got an acknowledgement for payload data for a channel.
1051  * Possibly resume transmissions.
1052  *
1053  * @param ch channel that got the ack
1054  * @param ack details about what was received
1055  */
1056 void
1057 GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
1058                                         const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1059 {
1060   struct CadetReliableMessage *crm;
1061
1062   if (GNUNET_NO == ch->reliable)
1063   {
1064     /* not expecting ACKs on unreliable channel, odd */
1065     GNUNET_break_op (0);
1066     return;
1067   }
1068   for (crm = ch->head_sent;
1069         NULL != crm;
1070        crm = crm->next)
1071     if (ack->mid.mid == crm->data_message.mid.mid)
1072       break;
1073   if (NULL == crm)
1074   {
1075     /* ACK for message we already dropped, might have been a
1076        duplicate ACK? Ignore. */
1077     LOG (GNUNET_ERROR_TYPE_DEBUG,
1078          "Duplicate DATA_ACK on %s, ignoring\n",
1079          GCCH_2s (ch));
1080     GNUNET_STATISTICS_update (stats,
1081                               "# duplicate DATA_ACKs",
1082                               1,
1083                               GNUNET_NO);
1084     return;
1085   }
1086   GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1087                                ch->tail_sent,
1088                                crm);
1089   ch->pending_messages--;
1090   GNUNET_free (crm);
1091   GNUNET_assert (ch->pending_messages < ch->max_pending_messages);
1092   LOG (GNUNET_ERROR_TYPE_DEBUG,
1093        "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1094        GCCH_2s (ch),
1095        (unsigned int) ntohl (ack->mid.mid),
1096        ch->pending_messages);
1097   send_ack_to_client (ch,
1098                       (NULL == ch->owner) ? ch->dest : ch->owner);
1099 }
1100
1101
1102 /**
1103  * Destroy channel, based on the other peer closing the
1104  * connection.  Also needs to remove this channel from
1105  * the tunnel.
1106  *
1107  * @param ch channel to destroy
1108  */
1109 void
1110 GCCH_handle_remote_destroy (struct CadetChannel *ch)
1111 {
1112   LOG (GNUNET_ERROR_TYPE_DEBUG,
1113        "Received remote channel DESTROY for %s\n",
1114        GCCH_2s (ch));
1115   ch->destroy = GNUNET_YES;
1116   GSC_handle_remote_channel_destroy ((NULL != ch->owner) ? ch->owner : ch->dest,
1117                                      ch->ccn,
1118                                      ch);
1119   channel_destroy (ch);
1120 }
1121
1122
1123 /**
1124  * Function called once the tunnel has sent one of our messages.
1125  * If the message is unreliable, simply frees the `crm`. If the
1126  * message was reliable, calculate retransmission time and
1127  * wait for ACK (or retransmit).
1128  *
1129  * @param cls the `struct CadetReliableMessage` that was sent
1130  */
1131 static void
1132 data_sent_cb (void *cls);
1133
1134
1135 /**
1136  * We need to retry a transmission, the last one took too long to
1137  * be acknowledged.
1138  *
1139  * @param cls the `struct CadetChannel` where we need to retransmit
1140  */
1141 static void
1142 retry_transmission (void *cls)
1143 {
1144   struct CadetChannel *ch = cls;
1145   struct CadetReliableMessage *crm = ch->head_sent;
1146
1147   ch->retry_data_task = NULL;
1148   GNUNET_assert (NULL == crm->qe);
1149   crm->qe = GCT_send (ch->t,
1150                       &crm->data_message.header,
1151                       &data_sent_cb,
1152                       crm);
1153 }
1154
1155
1156 /**
1157  * Check if we can now allow the client to transmit, and if so,
1158  * let the client know about it.
1159  *
1160  * @param ch channel to check
1161  */
1162 static void
1163 GCCH_check_allow_client (struct CadetChannel *ch)
1164 {
1165   struct GNUNET_MQ_Envelope *env;
1166   struct GNUNET_CADET_LocalAck *msg;
1167
1168   if (GNUNET_YES == ch->client_allowed)
1169     return; /* client already allowed! */
1170   if (CADET_CHANNEL_READY != ch->state)
1171   {
1172     /* destination did not yet ACK our CREATE! */
1173     LOG (GNUNET_ERROR_TYPE_DEBUG,
1174          "%s not yet ready, throttling client until ACK.\n",
1175          GCCH_2s (ch));
1176     return;
1177   }
1178   if (ch->pending_messages > ch->max_pending_messages)
1179   {
1180     /* Too many messages in queue. */
1181     LOG (GNUNET_ERROR_TYPE_DEBUG,
1182          "Message queue still too long on %s, throttling client until ACK.\n",
1183          GCCH_2s (ch));
1184     return;
1185   }
1186   if ( (NULL != ch->head_sent) &&
1187        (64 <= ntohl (ch->mid_send.mid) - ntohl (ch->head_sent->data_message.mid.mid)) )
1188   {
1189     LOG (GNUNET_ERROR_TYPE_DEBUG,
1190          "Gap in ACKs too big on %s, throttling client until ACK.\n",
1191          GCCH_2s (ch));
1192     return;
1193   }
1194   ch->client_allowed = GNUNET_YES;
1195
1196
1197   LOG (GNUNET_ERROR_TYPE_DEBUG,
1198        "Sending local ack to %s client\n",
1199        GCCH_2s (ch));
1200   env = GNUNET_MQ_msg (msg,
1201                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1202   msg->ccn = ch->ccn;
1203   GSC_send_to_client (ch->owner ? ch->owner : ch->dest,
1204                       env);
1205 }
1206
1207
1208 /**
1209  * Function called once the tunnel has sent one of our messages.
1210  * If the message is unreliable, simply frees the `crm`. If the
1211  * message was reliable, calculate retransmission time and
1212  * wait for ACK (or retransmit).
1213  *
1214  * @param cls the `struct CadetReliableMessage` that was sent
1215  */
1216 static void
1217 data_sent_cb (void *cls)
1218 {
1219   struct CadetReliableMessage *crm = cls;
1220   struct CadetChannel *ch = crm->ch;
1221   struct CadetReliableMessage *off;
1222
1223   crm->qe = NULL;
1224   GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1225                                ch->tail_sent,
1226                                crm);
1227   if (GNUNET_NO == ch->reliable)
1228   {
1229     GNUNET_free (crm);
1230     ch->pending_messages--;
1231     GCCH_check_allow_client (ch);
1232     return;
1233   }
1234   if (0 == crm->retry_delay.rel_value_us)
1235     crm->retry_delay = ch->expected_delay;
1236   crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
1237
1238   /* find position for re-insertion into the DLL */
1239   if ( (NULL == ch->head_sent) ||
1240        (crm->next_retry.abs_value_us < ch->head_sent->next_retry.abs_value_us) )
1241   {
1242     /* insert at HEAD, also (re)schedule retry task! */
1243     GNUNET_CONTAINER_DLL_insert (ch->head_sent,
1244                                  ch->tail_sent,
1245                                  crm);
1246     if (NULL != ch->retry_data_task)
1247       GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1248     ch->retry_data_task
1249       = GNUNET_SCHEDULER_add_delayed (crm->retry_delay,
1250                                       &retry_transmission,
1251                                       ch);
1252     return;
1253   }
1254   for (off = ch->head_sent; NULL != off; off = off->next)
1255     if (crm->next_retry.abs_value_us < off->next_retry.abs_value_us)
1256       break;
1257   if (NULL == off)
1258   {
1259     /* insert at tail */
1260     GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
1261                                       ch->tail_sent,
1262                                       crm);
1263   }
1264   else
1265   {
1266     /* insert before off */
1267     GNUNET_CONTAINER_DLL_insert_after (ch->head_sent,
1268                                        ch->tail_sent,
1269                                        off->prev,
1270                                        crm);
1271   }
1272 }
1273
1274
1275 /**
1276  * Handle data given by a client.
1277  *
1278  * Check whether the client is allowed to send in this tunnel, save if
1279  * channel is reliable and send an ACK to the client if there is still
1280  * buffer space in the tunnel.
1281  *
1282  * @param ch Channel.
1283  * @param message payload to transmit.
1284  * @return #GNUNET_OK if everything goes well,
1285  *         #GNUNET_SYSERR in case of an error.
1286  */
1287 int
1288 GCCH_handle_local_data (struct CadetChannel *ch,
1289                         const struct GNUNET_MessageHeader *message)
1290 {
1291   uint16_t payload_size = ntohs (message->size);
1292   struct CadetReliableMessage *crm;
1293
1294   if (GNUNET_NO == ch->client_allowed)
1295   {
1296     GNUNET_break_op (0);
1297     return GNUNET_SYSERR;
1298   }
1299   ch->client_allowed = GNUNET_NO;
1300   ch->pending_messages++;
1301
1302   /* Everything is correct, send the message. */
1303   crm = GNUNET_malloc (sizeof (*crm) + payload_size);
1304   crm->ch = ch;
1305   crm->data_message.header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + payload_size);
1306   crm->data_message.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1307   ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
1308   crm->data_message.mid = ch->mid_send;
1309   crm->data_message.ctn = ch->ctn;
1310   GNUNET_memcpy (&crm[1],
1311                  message,
1312                  payload_size);
1313   GNUNET_CONTAINER_DLL_insert (ch->head_sent,
1314                                ch->tail_sent,
1315                                crm);
1316   LOG (GNUNET_ERROR_TYPE_DEBUG,
1317        "Sending %u bytes from local client to %s\n",
1318        payload_size,
1319        GCCH_2s (ch));
1320   crm->qe = GCT_send (ch->t,
1321                       &crm->data_message.header,
1322                       &data_sent_cb,
1323                       crm);
1324   GCCH_check_allow_client (ch);
1325   return GNUNET_OK;
1326 }
1327
1328
1329 /**
1330  * Try to deliver messages to the local client, if it is ready for more.
1331  *
1332  * @param ch channel to process
1333  */
1334 static void
1335 send_client_buffered_data (struct CadetChannel *ch)
1336 {
1337   struct CadetOutOfOrderMessage *com;
1338
1339   if (GNUNET_NO == ch->client_ready)
1340     return; /* client not ready */
1341   com = ch->head_recv;
1342   if (NULL == com)
1343     return; /* none pending */
1344   if ( (com->mid.mid != ch->mid_recv.mid) &&
1345        (GNUNET_NO == ch->out_of_order) )
1346     return; /* missing next one in-order */
1347
1348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349               "Passing payload message to client on %s\n",
1350               GCCH_2s (ch));
1351
1352   /* all good, pass next message to client */
1353   GNUNET_CONTAINER_DLL_remove (ch->head_recv,
1354                                ch->tail_recv,
1355                                com);
1356   /* FIXME: if unreliable, this is not aggressive
1357      enough, as it would be OK to have lost some! */
1358   ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
1359   ch->mid_futures >>= 1; /* equivalent to division by 2 */
1360   GSC_send_to_client (ch->owner ? ch->owner : ch->dest,
1361                       com->env);
1362   GNUNET_free (com);
1363   if ( (0xFFULL == (ch->mid_futures & 0xFFULL)) &&
1364        (GNUNET_YES == ch->reliable) )
1365   {
1366     /* The next 15 messages were also already received (0xFF), this
1367        suggests that the sender may be blocked on flow control
1368        urgently waiting for an ACK from us. (As we have an inherent
1369        maximum of 64 bits, and 15 is getting too close for comfort.)
1370        So we should send one now. */
1371     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372                 "Sender on %s likely blocked on flow-control, sending ACK now.\n",
1373                 GCCH_2s (ch));
1374     if (GNUNET_YES == ch->reliable)
1375       send_channel_data_ack (ch);
1376   }
1377
1378   if (NULL != ch->head_recv)
1379     return;
1380   if (GNUNET_NO == ch->destroy)
1381     return;
1382   GCT_send_channel_destroy (ch->t,
1383                             ch->ctn);
1384   channel_destroy (ch);
1385 }
1386
1387
1388 /**
1389  * Handle ACK from client on local channel.
1390  *
1391  * @param ch channel to destroy
1392  */
1393 void
1394 GCCH_handle_local_ack (struct CadetChannel *ch)
1395 {
1396   ch->client_ready = GNUNET_YES;
1397   send_client_buffered_data (ch);
1398 }
1399
1400
1401 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
1402
1403
1404 /**
1405  * Log channel info.
1406  *
1407  * @param ch Channel.
1408  * @param level Debug level to use.
1409  */
1410 void
1411 GCCH_debug (struct CadetChannel *ch,
1412             enum GNUNET_ErrorType level)
1413 {
1414   int do_log;
1415
1416   do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1417                                        "cadet-chn",
1418                                        __FILE__, __FUNCTION__, __LINE__);
1419   if (0 == do_log)
1420     return;
1421
1422   if (NULL == ch)
1423   {
1424     LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1425     return;
1426   }
1427   LOG2 (level,
1428         "CHN %s:%X (%p)\n",
1429         GCT_2s (ch->t),
1430         ch->ctn,
1431         ch);
1432   if (NULL != ch->owner)
1433   {
1434     LOG2 (level,
1435           "CHN origin %s ready %s local-id: %u\n",
1436           GSC_2s (ch->owner),
1437           ch->client_ready ? "YES" : "NO",
1438           ntohl (ch->ccn.channel_of_client));
1439   }
1440   if (NULL != ch->dest)
1441   {
1442     LOG2 (level,
1443           "CHN destination %s ready %s local-id: %u\n",
1444           GSC_2s (ch->dest),
1445           ch->client_ready ? "YES" : "NO",
1446           ntohl (ch->ccn.channel_of_client));
1447   }
1448   LOG2 (level,
1449         "CHN  Message IDs recv: %d (%LLX), send: %d\n",
1450         ntohl (ch->mid_recv.mid),
1451         (unsigned long long) ch->mid_futures,
1452         ntohl (ch->mid_send.mid));
1453 }
1454
1455
1456
1457 /* end of gnunet-service-cadet-new_channel.c */