fa8eb6acb913e602e45cf58158795abfccc29ff6
[oweals/gnunet.git] / src / transport / gnunet-communicator-udp.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010-2014, 2018, 2019 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file transport/gnunet-communicator-udp.c
23  * @brief Transport plugin using UDP.
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - consider imposing transmission limits in the absence
28  *   of ACKs; or: maybe this should be done at TNG service level?
29  *   (at least the receiver might want to enforce limits on
30  *    KX/DH operations per sender in here) (#5552)
31  * - overall, we should look more into flow control support
32  *   (either in backchannel, or general solution in TNG service)
33  * - handle addresses discovered from broadcasts (#5551)
34  *   (think: what was the story again on address validation?
35  *    where is the API for that!?!)
36  * - support DNS names in BINDTO option (#5528)
37  * - support NAT connection reversal method (#5529)
38  * - support other UDP-specific NAT traversal methods (#) 
39  */
40 #include "platform.h"
41 #include "gnunet_util_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_constants.h"
45 #include "gnunet_nt_lib.h"
46 #include "gnunet_nat_service.h"
47 #include "gnunet_statistics_service.h"
48 #include "gnunet_transport_communication_service.h"
49
50 /**
51  * How often do we rekey based on time (at least)
52  */ 
53 #define REKEY_TIME_INTERVAL GNUNET_TIME_UNIT_DAYS
54
55 /**
56  * How long do we wait until we must have received the initial KX?
57  */ 
58 #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
59
60 /**
61  * How often do we broadcast our presence on the LAN?
62  */ 
63 #define BROADCAST_FREQUENCY GNUNET_TIME_UNIT_MINUTES
64
65 /**
66  * How often do we scan for changes to our network interfaces?
67  */ 
68 #define INTERFACE_SCAN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
69
70 /**
71  * AES key size.
72  */
73 #define AES_KEY_SIZE (256/8)
74
75 /**
76  * AES (GCM) IV size.
77  */
78 #define AES_IV_SIZE (96/8)
79
80 /**
81  * Size of the GCM tag.
82  */
83 #define GCM_TAG_SIZE (128/8)
84
85 /**
86  * If we fall below this number of available KCNs,
87  * we generate additional ACKs until we reach
88  * #KCN_TARGET.
89  * Should be large enough that we don't generate ACKs all
90  * the time and still have enough time for the ACK to
91  * arrive before the sender runs out. So really this 
92  * should ideally be based on the RTT.
93  */
94 #define KCN_THRESHOLD 92
95
96 /**
97  * How many KCNs do we keep around *after* we hit
98  * the #KCN_THRESHOLD? Should be larger than
99  * #KCN_THRESHOLD so we do not generate just one
100  * ACK at the time.
101  */
102 #define KCN_TARGET 128
103
104 /**
105  * What is the maximum delta between KCN sequence numbers
106  * that we allow. Used to expire 'ancient' KCNs that likely
107  * were dropped by the network.  Must be larger than
108  * KCN_TARGET (otherwise we generate new KCNs all the time),
109  * but not too large (otherwise packet loss may cause
110  * sender to fall back to KX needlessly when sender runs
111  * out of ACK'ed KCNs due to losses).
112  */
113 #define MAX_SQN_DELTA 160
114
115 /**
116  * How many shared master secrets do we keep around 
117  * at most per sender?  Should be large enough so 
118  * that we generally have a chance of sending an ACK
119  * before the sender already rotated out the master
120  * secret.  Generally values around #KCN_TARGET make
121  * sense. Might make sense to adapt to RTT if we had
122  * a good measurement...
123  */
124 #define MAX_SECRETS 128
125
126 /**
127  * How often do we rekey based on number of bytes transmitted?
128  * (additionally randomized).
129  */ 
130 #define REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
131
132 /**
133  * Address prefix used by the communicator.
134  */
135
136 #define COMMUNICATOR_ADDRESS_PREFIX "udp"
137
138 /**
139  * Configuration section used by the communicator.
140  */
141 #define COMMUNICATOR_CONFIG_SECTION "communicator-udp"
142
143 GNUNET_NETWORK_STRUCT_BEGIN
144
145
146 /**
147  * Signature we use to verify that the ephemeral key was really chosen by
148  * the specified sender.  If possible, the receiver should respond with
149  * a `struct UDPAck` (possibly via backchannel).
150  */
151 struct UdpHandshakeSignature
152 {
153   /**
154    * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
155    */
156   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
157
158   /**
159    * Identity of the inititor of the UDP connection (UDP client).
160    */ 
161   struct GNUNET_PeerIdentity sender;
162
163   /**
164    * Presumed identity of the target of the UDP connection (UDP server)
165    */ 
166   struct GNUNET_PeerIdentity receiver;
167
168   /**
169    * Ephemeral key used by the @e sender.
170    */ 
171   struct GNUNET_CRYPTO_EcdhePublicKey ephemeral;
172
173   /**
174    * Monotonic time of @e sender, to possibly help detect replay attacks
175    * (if receiver persists times by sender).
176    */ 
177   struct GNUNET_TIME_AbsoluteNBO monotonic_time;
178 };
179
180
181 /**
182  * "Plaintext" header at beginning of KX message. Followed
183  * by encrypted `struct UDPConfirmation`.
184  */
185 struct InitialKX
186 {
187
188   /**
189    * Ephemeral key for KX.
190    */ 
191   struct GNUNET_CRYPTO_EcdhePublicKey ephemeral;
192
193   /**
194    * HMAC for the following encrypted message, using GCM.  HMAC uses
195    * key derived from the handshake with sequence number zero.
196    */ 
197   char gcm_tag[GCM_TAG_SIZE];
198
199 };
200
201
202 /**
203  * Encrypted continuation of UDP initial handshake, followed
204  * by message header with payload.
205  */
206 struct UDPConfirmation
207 {
208   /**
209    * Sender's identity
210    */
211   struct GNUNET_PeerIdentity sender;
212
213   /**
214    * Sender's signature of type #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
215    */
216   struct GNUNET_CRYPTO_EddsaSignature sender_sig;
217
218   /**
219    * Monotonic time of @e sender, to possibly help detect replay attacks
220    * (if receiver persists times by sender).
221    */ 
222   struct GNUNET_TIME_AbsoluteNBO monotonic_time;
223
224   /* followed by messages */
225
226   /* padding may follow actual messages */
227 };
228
229
230 /**
231  * UDP key acknowledgement.  May be sent via backchannel. Allows the
232  * sender to use `struct UDPBox` with the acknowledge key henceforth.
233  */ 
234 struct UDPAck
235 {
236
237   /**
238    * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK.
239    */ 
240   struct GNUNET_MessageHeader header;
241
242   /**
243    * Sequence acknowledgement limit. Specifies current maximum sequence
244    * number supported by receiver.
245    */ 
246   uint32_t sequence_max GNUNET_PACKED;
247   
248   /**
249    * CMAC of the base key being acknowledged.
250    */ 
251   struct GNUNET_HashCode cmac;
252
253 };
254
255
256 /**
257  * Signature we use to verify that the broadcast was really made by
258  * the peer that claims to have made it.  Basically, affirms that the
259  * peer is really using this IP address (albeit possibly not in _our_
260  * LAN).  Makes it difficult for peers in the LAN to claim to 
261  * be just any global peer -- an attacker must have at least
262  * shared a LAN with the peer they're pretending to be here.
263  */
264 struct UdpBroadcastSignature
265 {
266   /**
267    * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
268    */
269   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
270
271   /**
272    * Identity of the inititor of the UDP broadcast.
273    */ 
274   struct GNUNET_PeerIdentity sender;
275
276   /**
277    * Hash of the sender's UDP address.
278    */ 
279   struct GNUNET_HashCode h_address;
280 };
281
282
283 /**
284  * Broadcast by peer in LAN announcing its presence.  Unusual in that
285  * we don't pad these to full MTU, as we cannot prevent being
286  * recognized in LAN as GNUnet peers if this feature is enabled
287  * anyway.  Also, the entire message is in cleartext.
288  */
289 struct UDPBroadcast
290 {
291
292   /**
293    * Sender's peer identity.
294    */
295   struct GNUNET_PeerIdentity sender;
296
297   /**
298    * Sender's signature of type
299    * #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
300    */
301   struct GNUNET_CRYPTO_EddsaSignature sender_sig;  
302   
303 };
304
305
306 /**
307  * UDP message box.  Always sent encrypted, only allowed after
308  * the receiver sent a `struct UDPAck` for the base key!
309  */ 
310 struct UDPBox
311 {
312
313   /**
314    * Key and IV identification code. KDF applied to an acknowledged
315    * base key and a sequence number.  Sequence numbers must be used
316    * monotonically increasing up to the maximum specified in 
317    * `struct UDPAck`. Without further `struct UDPAck`s, the sender
318    * must fall back to sending handshakes!
319    */
320   struct GNUNET_ShortHashCode kid;
321
322   /**
323    * 128-bit authentication tag for the following encrypted message,
324    * from GCM.  MAC starts at the @e body_start that follows and
325    * extends until the end of the UDP payload.  If the @e hmac is
326    * wrong, the receiver should check if the message might be a
327    * `struct UdpHandshakeSignature`.
328    */ 
329   char gcm_tag[GCM_TAG_SIZE];
330   
331 };
332
333
334 GNUNET_NETWORK_STRUCT_END
335
336 /**
337  * Shared secret we generated for a particular sender or receiver.
338  */
339 struct SharedSecret;
340
341
342 /**
343  * Pre-generated "kid" code (key and IV identification code) to
344  * quickly derive master key for a `struct UDPBox`.
345  */
346 struct KeyCacheEntry
347 {
348
349   /**
350    * Kept in a DLL.
351    */
352   struct KeyCacheEntry *next;
353   
354   /**
355    * Kept in a DLL.
356    */
357   struct KeyCacheEntry *prev;
358
359   /**
360    * Key and IV identification code. KDF applied to an acknowledged
361    * base key and a sequence number.  Sequence numbers must be used
362    * monotonically increasing up to the maximum specified in 
363    * `struct UDPAck`. Without further `struct UDPAck`s, the sender
364    * must fall back to sending handshakes!
365    */
366   struct GNUNET_ShortHashCode kid;
367
368   /**
369    * Corresponding shared secret.
370    */
371   struct SharedSecret *ss;
372
373   /**
374    * Sequence number used to derive this entry from master key.
375    */ 
376   uint32_t sequence_number;
377 };
378
379
380 /**
381  * Information we track per sender address we have recently been
382  * in contact with (decryption from sender).
383  */
384 struct SenderAddress;
385
386 /**
387  * Information we track per receiving address we have recently been
388  * in contact with (encryption to receiver).
389  */
390 struct ReceiverAddress;
391
392 /**
393  * Shared secret we generated for a particular sender or receiver.
394  */
395 struct SharedSecret
396 {
397   /**
398    * Kept in a DLL.
399    */
400   struct SharedSecret *next;
401
402   /**
403    * Kept in a DLL.
404    */
405   struct SharedSecret *prev;
406
407   /**
408    * Kept in a DLL, sorted by sequence number. Only if we are decrypting.
409    */
410   struct KeyCacheEntry *kce_head;
411   
412   /**
413    * Kept in a DLL, sorted by sequence number. Only if we are decrypting.
414    */
415   struct KeyCacheEntry *kce_tail;
416
417   /**
418    * Sender we use this shared secret with, or NULL.
419    */
420   struct SenderAddress *sender;
421
422   /**
423    * Receiver we use this shared secret with, or NULL.
424    */
425   struct ReceiverAddress *receiver;
426   
427   /**
428    * Master shared secret.
429    */ 
430   struct GNUNET_HashCode master;
431
432   /**
433    * CMAC is used to identify @e master in ACKs.
434    */ 
435   struct GNUNET_HashCode cmac;
436
437   /**
438    * Up to which sequence number did we use this @e master already?
439    * (for encrypting only)
440    */ 
441   uint32_t sequence_used;
442
443   /**
444    * Up to which sequence number did the other peer allow us to use
445    * this key, or up to which number did we allow the other peer to
446    * use this key?
447    */
448   uint32_t sequence_allowed;
449
450   /**
451    * Number of active KCN entries.
452    */ 
453   unsigned int active_kce_count;
454 };
455
456
457 /**
458  * Information we track per sender address we have recently been
459  * in contact with (we decrypt messages from the sender).
460  */
461 struct SenderAddress
462 {
463
464   /**
465    * To whom are we talking to.
466    */
467   struct GNUNET_PeerIdentity target;  
468
469   /**
470    * Entry in sender expiration heap.
471    */ 
472   struct GNUNET_CONTAINER_HeapNode *hn;
473
474   /**
475    * Shared secrets we used with @e target, first used is head.
476    */
477   struct SharedSecret *ss_head;
478
479   /**
480    * Shared secrets we used with @e target, last used is tail.
481    */
482   struct SharedSecret *ss_tail;
483   
484   /**
485    * Address of the other peer.
486    */
487   struct sockaddr *address;
488   
489   /**
490    * Length of the address.
491    */
492   socklen_t address_len;
493
494   /**
495    * Timeout for this sender.
496    */
497   struct GNUNET_TIME_Absolute timeout;
498
499   /**
500    * Length of the DLL at @a ss_head.
501    */ 
502   unsigned int num_secrets;
503   
504   /**
505    * Which network type does this queue use?
506    */
507   enum GNUNET_NetworkType nt;
508   
509 };
510
511
512 /**
513  * Information we track per receiving address we have recently been
514  * in contact with (encryption to receiver).
515  */
516 struct ReceiverAddress
517 {
518
519   /**
520    * To whom are we talking to.
521    */
522   struct GNUNET_PeerIdentity target;  
523   
524   /**
525    * Shared secrets we received from @e target, first used is head.
526    */
527   struct SharedSecret *ss_head;
528
529   /**
530    * Shared secrets we received with @e target, last used is tail.
531    */
532   struct SharedSecret *ss_tail;
533
534   /**
535    * Address of the receiver in the human-readable format
536    * with the #COMMUNICATOR_ADDRESS_PREFIX.
537    */ 
538   char *foreign_addr;
539
540   /**
541    * Address of the other peer.
542    */
543   struct sockaddr *address;
544   
545   /**
546    * Length of the address.
547    */
548   socklen_t address_len;
549
550   /**
551    * Entry in sender expiration heap.
552    */ 
553   struct GNUNET_CONTAINER_HeapNode *hn;
554
555   /**
556    * Message queue we are providing for the #ch.
557    */
558   struct GNUNET_MQ_Handle *mq;
559
560   /**
561    * handle for this queue with the #ch.
562    */
563   struct GNUNET_TRANSPORT_QueueHandle *qh;
564
565   /**
566    * Timeout for this receiver address.
567    */
568   struct GNUNET_TIME_Absolute timeout;
569
570   /**
571    * MTU we allowed transport for this receiver right now.
572    */
573   size_t mtu;
574   
575   /**
576    * Length of the DLL at @a ss_head.
577    */ 
578   unsigned int num_secrets;
579
580   /**
581    * Number of BOX keys from ACKs we have currently 
582    * available for this receiver.
583    */ 
584   unsigned int acks_available;
585   
586   /**
587    * Which network type does this queue use?
588    */
589   enum GNUNET_NetworkType nt;
590   
591 };
592
593
594 /**
595  * Interface we broadcast our presence on.
596  */
597 struct BroadcastInterface
598 {
599
600   /**
601    * Kept in a DLL.
602    */
603   struct BroadcastInterface *next;
604
605   /**
606    * Kept in a DLL.
607    */
608   struct BroadcastInterface *prev;
609
610   /**
611    * Task for this broadcast interface.
612    */
613   struct GNUNET_SCHEDULER_Task *broadcast_task;
614   
615   /**
616    * Sender's address of the interface.
617    */
618   struct sockaddr *sa;
619   
620   /**
621    * Broadcast address to use on the interface.
622    */
623   struct sockaddr *ba;
624
625   /**
626    * Message we broadcast on this interface.
627    */ 
628   struct UDPBroadcast bcm;
629   
630   /**
631    * If this is an IPv6 interface, this is the request
632    * we use to join/leave the group.
633    */
634   struct ipv6_mreq mcreq;
635   
636   /**
637    * Number of bytes in @e sa.
638    */ 
639   socklen_t salen;
640
641   /**
642    * Was this interface found in the last #iface_proc() scan?
643    */
644   int found;
645 };
646
647
648 /**
649  * Cache of pre-generated key IDs.
650  */
651 static struct GNUNET_CONTAINER_MultiShortmap *key_cache;
652
653 /**
654  * ID of read task
655  */
656 static struct GNUNET_SCHEDULER_Task *read_task;
657
658 /**
659  * ID of timeout task
660  */
661 static struct GNUNET_SCHEDULER_Task *timeout_task;
662
663 /**
664  * ID of master broadcast task
665  */
666 static struct GNUNET_SCHEDULER_Task *broadcast_task;
667
668 /**
669  * For logging statistics.
670  */
671 static struct GNUNET_STATISTICS_Handle *stats;
672
673 /**
674  * Our environment.
675  */
676 static struct GNUNET_TRANSPORT_CommunicatorHandle *ch;
677
678 /**
679  * Receivers (map from peer identity to `struct ReceiverAddress`)
680  */
681 static struct GNUNET_CONTAINER_MultiPeerMap *receivers;
682
683 /**
684  * Senders (map from peer identity to `struct SenderAddress`)
685  */
686 static struct GNUNET_CONTAINER_MultiPeerMap *senders;
687
688 /**
689  * Expiration heap for senders (contains `struct SenderAddress`)
690  */
691 static struct GNUNET_CONTAINER_Heap *senders_heap;
692
693 /**
694  * Expiration heap for receivers (contains `struct ReceiverAddress`)
695  */
696 static struct GNUNET_CONTAINER_Heap *receivers_heap;
697
698 /**
699  * Broadcast interface tasks. Kept in a DLL.
700  */
701 static struct BroadcastInterface *bi_head;
702
703 /**
704  * Broadcast interface tasks. Kept in a DLL.
705  */
706 static struct BroadcastInterface *bi_tail;
707
708 /**
709  * Our socket.
710  */
711 static struct GNUNET_NETWORK_Handle *udp_sock;
712
713 /** 
714  * #GNUNET_YES if #udp_sock supports IPv6.
715  */ 
716 static int have_v6_socket;
717
718 /**
719  * Our public key.
720  */
721 static struct GNUNET_PeerIdentity my_identity;
722
723 /**
724  * Our private key.
725  */
726 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
727
728 /**
729  * Our configuration.
730  */
731 static const struct GNUNET_CONFIGURATION_Handle *cfg;
732
733 /**
734  * Network scanner to determine network types.
735  */
736 static struct GNUNET_NT_InterfaceScanner *is;
737
738 /**
739  * Connection to NAT service.
740  */
741 static struct GNUNET_NAT_Handle *nat;
742
743 /**
744  * Port number to which we are actually bound.
745  */ 
746 static uint16_t my_port;
747
748
749 /**
750  * An interface went away, stop broadcasting on it.
751  *
752  * @param bi entity to close down
753  */
754 static void
755 bi_destroy (struct BroadcastInterface *bi)
756 {
757   if (AF_INET6 == bi->sa->sa_family)
758   {
759     /* Leave the multicast group */
760     if (GNUNET_OK !=
761         GNUNET_NETWORK_socket_setsockopt
762         (udp_sock,
763          IPPROTO_IPV6,
764          IPV6_LEAVE_GROUP,
765          &bi->mcreq,
766          sizeof (bi->mcreq)))
767     {
768       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
769                            "setsockopt");
770     }
771   }
772   GNUNET_CONTAINER_DLL_remove (bi_head,
773                                bi_tail,
774                                bi);
775   GNUNET_SCHEDULER_cancel (bi->broadcast_task);
776   GNUNET_free (bi->sa);
777   GNUNET_free_non_null (bi->ba);
778   GNUNET_free (bi);
779 }
780
781
782 /**
783  * Destroys a receiving state due to timeout or shutdown.
784  *
785  * @param receiver entity to close down
786  */
787 static void
788 receiver_destroy (struct ReceiverAddress *receiver)
789 {
790   struct GNUNET_MQ_Handle *mq;
791   
792   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793               "Disconnecting receiver for peer `%s'\n",
794               GNUNET_i2s (&receiver->target));
795   if (NULL != (mq = receiver->mq))
796   {
797     receiver->mq = NULL;
798     GNUNET_MQ_destroy (mq);
799   }
800   if (NULL != receiver->qh)
801   {
802     GNUNET_TRANSPORT_communicator_mq_del (receiver->qh);
803     receiver->qh = NULL;
804   }
805   GNUNET_assert (GNUNET_YES ==
806                  GNUNET_CONTAINER_multipeermap_remove (receivers,
807                                                        &receiver->target,
808                                                        receiver));
809   GNUNET_assert (receiver ==
810                  GNUNET_CONTAINER_heap_remove_node (receiver->hn));
811   GNUNET_STATISTICS_set (stats,
812                          "# receivers active",
813                          GNUNET_CONTAINER_multipeermap_size (receivers),
814                          GNUNET_NO);
815   GNUNET_free (receiver->address);
816   GNUNET_free (receiver->foreign_addr);
817   GNUNET_free (receiver);
818 }
819
820
821 /**
822  * Free memory used by key cache entry.
823  *
824  * @param kce the key cache entry
825  */ 
826 static void
827 kce_destroy (struct KeyCacheEntry *kce)
828 {
829   struct SharedSecret *ss = kce->ss;
830
831   ss->active_kce_count--;
832   GNUNET_CONTAINER_DLL_remove (ss->kce_head,
833                                ss->kce_tail,
834                                kce);
835   GNUNET_assert (GNUNET_YES ==
836                  GNUNET_CONTAINER_multishortmap_remove (key_cache,
837                                                         &kce->kid,
838                                                         kce));
839   GNUNET_free (kce);
840 }
841
842
843 /**
844  * Compute @a kid.
845  *
846  * @param msec master secret for HMAC calculation
847  * @param serial number for the @a smac calculation
848  * @param kid[out] where to write the key ID
849  */
850 static void
851 get_kid (const struct GNUNET_HashCode *msec,
852          uint32_t serial,
853          struct GNUNET_ShortHashCode *kid)
854 {
855   uint32_t sid = htonl (serial);
856
857   GNUNET_CRYPTO_hkdf (kid,
858                       sizeof (*kid),
859                       GCRY_MD_SHA512,
860                       GCRY_MD_SHA256,
861                       &sid,
862                       sizeof (sid),
863                       msec,
864                       sizeof (*msec),
865                       "UDP-KID",
866                       strlen ("UDP-KID"),
867                       NULL, 0);
868 }
869
870
871 /**
872  * Setup key cache entry for sequence number @a seq and shared secret @a ss.
873  *
874  * @param ss shared secret
875  * @param seq sequence number for the key cache entry
876  */
877 static void
878 kce_generate (struct SharedSecret *ss,
879               uint32_t seq)
880 {
881   struct KeyCacheEntry *kce;
882
883   GNUNET_assert (0 < seq);
884   kce = GNUNET_new (struct KeyCacheEntry);
885   kce->ss = ss;
886   kce->sequence_number = seq;
887   get_kid (&ss->master,
888            seq,
889            &kce->kid);
890   GNUNET_CONTAINER_DLL_insert (ss->kce_head,
891                                ss->kce_tail,
892                                kce);
893   ss->active_kce_count++;
894   (void) GNUNET_CONTAINER_multishortmap_put (key_cache,
895                                              &kce->kid,
896                                              kce,
897                                              GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
898   GNUNET_STATISTICS_set (stats,
899                          "# KIDs active",
900                          GNUNET_CONTAINER_multishortmap_size (key_cache),
901                          GNUNET_NO);
902 }
903
904
905 /**
906  * Destroy @a ss and associated key cache entries.
907  *
908  * @param ss shared secret to destroy
909  */
910 static void
911 secret_destroy (struct SharedSecret *ss)
912 {
913   struct SenderAddress *sender;
914   struct ReceiverAddress *receiver;
915   struct KeyCacheEntry *kce;
916
917   if (NULL != (sender = ss->sender))
918   {
919     GNUNET_CONTAINER_DLL_remove (sender->ss_head,
920                                  sender->ss_tail,
921                                  ss);
922     sender->num_secrets--;
923   }
924   if (NULL != (receiver = ss->receiver))
925   {
926     GNUNET_CONTAINER_DLL_remove (receiver->ss_head,
927                                  receiver->ss_tail,
928                                  ss);
929     receiver->num_secrets--;
930     receiver->acks_available
931       -= (ss->sequence_allowed - ss->sequence_used);
932   }
933   while (NULL != (kce = ss->kce_head))
934     kce_destroy (kce);
935   GNUNET_STATISTICS_update (stats,
936                             "# Secrets active",
937                             -1,
938                             GNUNET_NO);
939   GNUNET_STATISTICS_set (stats,
940                          "# KIDs active",
941                          GNUNET_CONTAINER_multishortmap_size (key_cache),
942                          GNUNET_NO);
943   GNUNET_free (ss);
944 }
945
946
947 /**
948  * Functions with this signature are called whenever we need
949  * to close a sender's state due to timeout.
950  *
951  * @param sender entity to close down
952  */
953 static void
954 sender_destroy (struct SenderAddress *sender)
955 {
956   GNUNET_assert (GNUNET_YES ==
957                  GNUNET_CONTAINER_multipeermap_remove (senders,
958                                                        &sender->target,
959                                                        sender));
960   GNUNET_assert (sender ==
961                  GNUNET_CONTAINER_heap_remove_node (sender->hn));
962   GNUNET_STATISTICS_set (stats,
963                          "# senders active",
964                          GNUNET_CONTAINER_multipeermap_size (senders),
965                          GNUNET_NO);
966   GNUNET_free (sender->address);
967   GNUNET_free (sender);
968 }
969
970
971 /**
972  * Compute @a key and @a iv.
973  *
974  * @param msec master secret for calculation
975  * @param serial number for the @a smac calculation
976  * @param key[out] where to write the decrption key
977  * @param iv[out] where to write the IV
978  */
979 static void
980 get_iv_key (const struct GNUNET_HashCode *msec,
981             uint32_t serial,
982             char key[AES_KEY_SIZE],
983             char iv[AES_IV_SIZE])
984 {
985   uint32_t sid = htonl (serial);
986   char res[AES_KEY_SIZE + AES_IV_SIZE];
987
988   GNUNET_CRYPTO_hkdf (res,
989                       sizeof (res),
990                       GCRY_MD_SHA512,
991                       GCRY_MD_SHA256,
992                       &sid,
993                       sizeof (sid),
994                       msec,
995                       sizeof (*msec),
996                       "UDP-IV-KEY",
997                       strlen ("UDP-IV-KEY"),
998                       NULL, 0);
999   memcpy (key,
1000           res,
1001           AES_KEY_SIZE);
1002   memcpy (iv,
1003           &res[AES_KEY_SIZE],
1004           AES_IV_SIZE);
1005 }
1006
1007
1008 /**
1009  * Increment sender timeout due to activity.
1010  *
1011  * @param sender address for which the timeout should be rescheduled
1012  */
1013 static void
1014 reschedule_sender_timeout (struct SenderAddress *sender)
1015 {
1016   sender->timeout
1017     = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1018   GNUNET_CONTAINER_heap_update_cost (sender->hn,
1019                                      sender->timeout.abs_value_us);
1020 }
1021
1022
1023 /**
1024  * Increment receiver timeout due to activity.
1025  *
1026  * @param receiver address for which the timeout should be rescheduled
1027  */
1028 static void
1029 reschedule_receiver_timeout (struct ReceiverAddress *receiver)
1030 {
1031   receiver->timeout
1032     = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1033   GNUNET_CONTAINER_heap_update_cost (receiver->hn,
1034                                      receiver->timeout.abs_value_us);
1035 }
1036
1037
1038 /**
1039  * Task run to check #receiver_heap and #sender_heap for timeouts.
1040  *
1041  * @param cls unused, NULL
1042  */
1043 static void
1044 check_timeouts (void *cls)
1045 {
1046   struct GNUNET_TIME_Relative st;
1047   struct GNUNET_TIME_Relative rt;
1048   struct GNUNET_TIME_Relative delay;
1049   struct ReceiverAddress *receiver;
1050   struct SenderAddress *sender;
1051   
1052   (void) cls;
1053   timeout_task = NULL;
1054   rt = GNUNET_TIME_UNIT_FOREVER_REL;
1055   while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap)))
1056   {
1057     rt = GNUNET_TIME_absolute_get_remaining (receiver->timeout);
1058     if (0 != rt.rel_value_us)
1059       break;
1060     receiver_destroy (receiver);
1061   }
1062   st = GNUNET_TIME_UNIT_FOREVER_REL;
1063   while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap)))
1064   {
1065     st = GNUNET_TIME_absolute_get_remaining (receiver->timeout);
1066     if (0 != st.rel_value_us)
1067       break;
1068     sender_destroy (sender);
1069   }
1070   delay = GNUNET_TIME_relative_min (rt,
1071                                     st);
1072   if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1073     timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
1074                                                  &check_timeouts,
1075                                                  NULL);  
1076 }
1077                           
1078
1079 /**
1080  * Calcualte cmac from master in @a ss.
1081  *
1082  * @param ss[in,out] data structure to complete
1083  */
1084 static void
1085 calculate_cmac (struct SharedSecret *ss)
1086 {
1087   GNUNET_CRYPTO_hkdf (&ss->cmac,
1088                       sizeof (ss->cmac),
1089                       GCRY_MD_SHA512,
1090                       GCRY_MD_SHA256,
1091                       "CMAC",
1092                       strlen ("CMAC"),
1093                       &ss->master,
1094                       sizeof (ss->master),
1095                       "UDP-CMAC",
1096                       strlen ("UDP-CMAC"),
1097                       NULL, 0);
1098 }
1099
1100
1101 /**
1102  * We received @a plaintext_len bytes of @a plaintext from a @a sender.
1103  * Pass it on to CORE.  
1104  *
1105  * @param queue the queue that received the plaintext
1106  * @param plaintext the plaintext that was received
1107  * @param plaintext_len number of bytes of plaintext received
1108  */ 
1109 static void
1110 pass_plaintext_to_core (struct SenderAddress *sender,
1111                         const void *plaintext,
1112                         size_t plaintext_len)
1113 {
1114   const struct GNUNET_MessageHeader *hdr = plaintext;
1115
1116   while (ntohs (hdr->size) < plaintext_len)
1117   {
1118     GNUNET_STATISTICS_update (stats,
1119                               "# bytes given to core",
1120                               ntohs (hdr->size),
1121                               GNUNET_NO);
1122     (void) GNUNET_TRANSPORT_communicator_receive (ch,
1123                                                   &sender->target,
1124                                                   hdr,
1125                                                   NULL /* no flow control possible */,
1126                                                   NULL);
1127     /* move on to next message, if any */
1128     plaintext_len -= ntohs (hdr->size);
1129     if (plaintext_len < sizeof (*hdr))
1130       break;
1131     hdr = plaintext + ntohs (hdr->size);
1132   }
1133   GNUNET_STATISTICS_update (stats,
1134                             "# bytes padding discarded",
1135                             plaintext_len,
1136                             GNUNET_NO);
1137 }
1138
1139
1140 /**
1141  * Setup @a cipher based on shared secret @a msec and
1142  * serial number @a serial.
1143  *
1144  * @param msec master shared secret
1145  * @param serial serial number of cipher to set up
1146  * @param cipher[out] cipher to initialize
1147  */
1148 static void
1149 setup_cipher (const struct GNUNET_HashCode *msec,
1150               uint32_t serial,
1151               gcry_cipher_hd_t *cipher)
1152 {
1153   char key[AES_KEY_SIZE];
1154   char iv[AES_IV_SIZE];
1155
1156   gcry_cipher_open (cipher,
1157                     GCRY_CIPHER_AES256 /* low level: go for speed */,
1158                     GCRY_CIPHER_MODE_GCM,
1159                     0 /* flags */);
1160   get_iv_key (msec,
1161               serial,
1162               key,
1163               iv);
1164   gcry_cipher_setkey (*cipher,
1165                       key,
1166                       sizeof (key));
1167   gcry_cipher_setiv (*cipher,
1168                      iv,
1169                      sizeof (iv));
1170 }
1171
1172
1173 /**
1174  * Try to decrypt @a buf using shared secret @a ss and key/iv 
1175  * derived using @a serial.
1176  *
1177  * @param ss shared secret
1178  * @param tag GCM authentication tag
1179  * @param serial serial number to use
1180  * @param in_buf input buffer to decrypt
1181  * @param in_buf_size number of bytes in @a in_buf and available in @a out_buf
1182  * @param out_buf where to write the result
1183  * @return #GNUNET_OK on success
1184  */
1185 static int
1186 try_decrypt (const struct SharedSecret *ss,
1187              const char tag[GCM_TAG_SIZE],
1188              uint32_t serial,
1189              const char *in_buf,
1190              size_t in_buf_size,
1191              char *out_buf)
1192 {
1193   gcry_cipher_hd_t cipher;
1194
1195   setup_cipher (&ss->master,
1196                 serial,
1197                 &cipher);
1198   GNUNET_assert (0 ==
1199                  gcry_cipher_decrypt (cipher,
1200                                       out_buf,
1201                                       in_buf_size,
1202                                       in_buf,
1203                                       in_buf_size));
1204   if (0 !=
1205       gcry_cipher_checktag (cipher,
1206                             tag,
1207                             GCM_TAG_SIZE))
1208   {
1209     gcry_cipher_close (cipher);
1210     GNUNET_STATISTICS_update (stats,
1211                               "# AEAD authentication failures",
1212                               1,
1213                               GNUNET_NO);
1214     return GNUNET_SYSERR;
1215   }
1216   gcry_cipher_close (cipher);
1217   return GNUNET_OK;
1218 }
1219
1220
1221 /**
1222  * Setup shared secret for decryption.
1223  *
1224  * @param ephemeral ephemeral key we received from the other peer
1225  * @return new shared secret
1226  */
1227 static struct SharedSecret *
1228 setup_shared_secret_dec (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral)
1229 {
1230   struct SharedSecret *ss;
1231
1232   ss = GNUNET_new (struct SharedSecret);
1233   GNUNET_CRYPTO_eddsa_ecdh (my_private_key,
1234                             ephemeral,
1235                             &ss->master);
1236   return ss;
1237 }
1238
1239
1240 /**
1241  * Setup shared secret for encryption.
1242  *
1243  * @param ephemeral ephemeral key we are sending to the other peer
1244  * @param receiver[in,out] queue to initialize encryption key for
1245  * @return new shared secret
1246  */
1247 static struct SharedSecret *
1248 setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral,
1249                          struct ReceiverAddress *receiver)
1250 {
1251   struct SharedSecret *ss;
1252
1253   ss = GNUNET_new (struct SharedSecret);
1254   GNUNET_CRYPTO_ecdh_eddsa (ephemeral,
1255                             &receiver->target.public_key,
1256                             &ss->master);
1257   calculate_cmac (ss);
1258   ss->receiver = receiver;
1259   GNUNET_CONTAINER_DLL_insert (receiver->ss_head,
1260                                receiver->ss_tail,
1261                                ss);
1262   receiver->num_secrets++;
1263   GNUNET_STATISTICS_update (stats,
1264                             "# Secrets active",
1265                             1,
1266                             GNUNET_NO);
1267   return ss;
1268 }
1269
1270
1271 /**
1272  * Setup the MQ for the @a receiver.  If a queue exists,
1273  * the existing one is destroyed.  Then the MTU is
1274  * recalculated and a fresh queue is initialized.
1275  *
1276  * @param receiver receiver to setup MQ for
1277  */ 
1278 static void
1279 setup_receiver_mq (struct ReceiverAddress *receiver);
1280
1281
1282 /**
1283  * We received an ACK for @a pid. Check if it is for
1284  * the receiver in @a value and if so, handle it and
1285  * return #GNUNET_NO. Otherwise, return #GNUNET_YES.
1286  *
1287  * @param cls a `const struct UDPAck`
1288  * @param pid peer the ACK is from
1289  * @param value a `struct ReceiverAddress`
1290  * @return #GNUNET_YES to continue to iterate
1291  */
1292 static int
1293 handle_ack (void *cls,
1294             const struct GNUNET_PeerIdentity *pid,
1295             void *value)
1296 {
1297   const struct UDPAck *ack = cls;
1298   struct ReceiverAddress *receiver = value;
1299
1300   (void) pid;
1301   for (struct SharedSecret *ss = receiver->ss_head;
1302        NULL != ss;
1303        ss = ss->next)
1304   {
1305     if (0 == memcmp (&ack->cmac,
1306                      &ss->cmac,
1307                      sizeof (struct GNUNET_HashCode)))
1308     {
1309       uint32_t allowed;
1310       
1311       allowed = ntohl (ack->sequence_max);
1312                             
1313       if (allowed > ss->sequence_allowed)
1314       {
1315         receiver->acks_available += (allowed - ss->sequence_allowed);
1316         if ((allowed - ss->sequence_allowed)
1317             == receiver->acks_available)
1318         {
1319           /* we just incremented from zero => MTU change! */
1320           setup_receiver_mq (receiver);
1321         }
1322         ss->sequence_allowed = allowed;
1323         /* move ss to head to avoid discarding it anytime soon! */
1324         GNUNET_CONTAINER_DLL_remove (receiver->ss_head,
1325                                      receiver->ss_tail,
1326                                      ss);
1327         GNUNET_CONTAINER_DLL_insert (receiver->ss_head,
1328                                      receiver->ss_tail,
1329                                      ss);
1330       }
1331       return GNUNET_NO;
1332     }
1333   }
1334   return GNUNET_YES;
1335 }
1336
1337
1338 /**
1339  * Test if we have received a valid message in plaintext.
1340  * If so, handle it.
1341  *
1342  * @param sender peer to process inbound plaintext for
1343  * @param buf buffer we received
1344  * @param buf_size number of bytes in @a buf
1345  */ 
1346 static void
1347 try_handle_plaintext (struct SenderAddress *sender,
1348                       const void *buf,
1349                       size_t buf_size)
1350 {
1351   const struct GNUNET_MessageHeader *hdr
1352     = (const struct GNUNET_MessageHeader *) buf;
1353   const struct UDPAck *ack
1354     = (const struct UDPAck *) buf;
1355   uint16_t type;
1356
1357   if (sizeof (*hdr) > buf_size)
1358     return; /* not even a header */
1359   if (ntohs (hdr->size) > buf_size)
1360     return; /* not even a header */
1361   type = ntohs (hdr->type);
1362   switch (type)
1363   {
1364   case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK:
1365     /* lookup master secret by 'cmac', then update sequence_max */
1366     GNUNET_CONTAINER_multipeermap_get_multiple (receivers,
1367                                                 &sender->target,
1368                                                 &handle_ack,
1369                                                 (void *) ack);
1370     /* There could be more messages after the ACK, handle those as well */
1371     buf += ntohs (hdr->size);
1372     buf_size -= ntohs (hdr->size);
1373     pass_plaintext_to_core (sender,
1374                             buf,
1375                             buf_size);
1376     break;
1377   case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD:
1378     /* skip padding */
1379     break;
1380   default:
1381     pass_plaintext_to_core (sender,
1382                             buf,
1383                             buf_size);
1384   }
1385 }
1386
1387
1388 /**
1389  * We established a shared secret with a sender. We should try to send
1390  * the sender an `struct UDPAck` at the next opportunity to allow the
1391  * sender to use @a ss longer (assuming we did not yet already
1392  * recently).
1393  *
1394  * @param ss shared secret to generate ACKs for
1395  */
1396 static void
1397 consider_ss_ack (struct SharedSecret *ss)
1398 {
1399   GNUNET_assert (NULL != ss->sender);
1400   /* drop ancient KeyCacheEntries */
1401   while ( (NULL != ss->kce_head) &&
1402           (MAX_SQN_DELTA < ss->kce_head->sequence_number - ss->kce_tail->sequence_number) )
1403     kce_destroy (ss->kce_tail);
1404   if (ss->active_kce_count < KCN_THRESHOLD)
1405   {
1406     struct UDPAck ack;
1407
1408     while (ss->active_kce_count < KCN_TARGET)
1409       kce_generate (ss,
1410                     ++ss->sequence_allowed);
1411     ack.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK);
1412     ack.header.size = htons (sizeof (ack));
1413     ack.sequence_max = htonl (ss->sequence_allowed);
1414     ack.cmac = ss->cmac;
1415     GNUNET_TRANSPORT_communicator_notify (ch,
1416                                           &ss->sender->target,
1417                                           COMMUNICATOR_ADDRESS_PREFIX,
1418                                           &ack.header);
1419   }
1420 }
1421
1422
1423 /**
1424  * We received a @a box with matching @a kce.  Decrypt and process it.
1425  *
1426  * @param box the data we received
1427  * @param box_len number of bytes in @a box
1428  * @param kce key index to decrypt @a box
1429  */ 
1430 static void
1431 decrypt_box (const struct UDPBox *box,
1432              size_t box_len,
1433              struct KeyCacheEntry *kce)
1434 {
1435   struct SharedSecret *ss = kce->ss;
1436   char out_buf[box_len - sizeof (*box)];
1437
1438   GNUNET_assert (NULL != ss->sender);
1439   if (GNUNET_OK !=
1440       try_decrypt (ss,
1441                    box->gcm_tag,
1442                    kce->sequence_number,
1443                    (const char *) &box[1],
1444                    sizeof (out_buf),
1445                    out_buf))
1446   {
1447     GNUNET_STATISTICS_update (stats,
1448                               "# Decryption failures with valid KCE",
1449                               1,
1450                               GNUNET_NO);
1451     kce_destroy (kce);
1452     return;
1453   }
1454   kce_destroy (kce);
1455   GNUNET_STATISTICS_update (stats,
1456                             "# bytes decrypted with BOX",
1457                             sizeof (out_buf),
1458                             GNUNET_NO);
1459   try_handle_plaintext (ss->sender,
1460                         out_buf,
1461                         sizeof (out_buf));
1462   consider_ss_ack (ss);
1463 }
1464
1465
1466 /**
1467  * Closure for #find_sender_by_address()
1468  */
1469 struct SearchContext
1470 {
1471   /**
1472    * Address we are looking for.
1473    */
1474   const struct sockaddr *address;
1475
1476   /**
1477    * Number of bytes in @e address.
1478    */
1479   socklen_t address_len;
1480
1481   /**
1482    * Return value to set if we found a match.
1483    */
1484   struct SenderAddress *sender;
1485 };
1486
1487
1488 /**
1489  * Find existing `struct SenderAddress` by matching addresses.
1490  *
1491  * @param cls a `struct SearchContext`
1492  * @param key ignored, must match already
1493  * @param value a `struct SenderAddress`
1494  * @return #GNUNET_YES if not found (continue to search), #GNUNET_NO if found
1495  */
1496 static int
1497 find_sender_by_address (void *cls,
1498                         const struct GNUNET_PeerIdentity *key,
1499                         void *value)
1500 {
1501   struct SearchContext *sc = cls;
1502   struct SenderAddress *sender = value;
1503
1504   if ( (sender->address_len == sc->address_len) &&
1505        (0 == memcmp (sender->address,
1506                      sc->address,
1507                      sender->address_len)) )
1508   {
1509     sc->sender = sender;
1510     return GNUNET_NO; /* stop iterating! */
1511   }
1512   return GNUNET_YES;
1513 }
1514
1515
1516 /**
1517  * Create sender address for @a target.  Note that we
1518  * might already have one, so a fresh one is only allocated
1519  * if one does not yet exist for @a address.
1520  *
1521  * @param target peer to generate address for
1522  * @param address target address 
1523  * @param address_len number of bytes in @a address
1524  * @return data structure to keep track of key material for
1525  *         decrypting data from @a target
1526  */
1527 static struct SenderAddress *
1528 setup_sender (const struct GNUNET_PeerIdentity *target,
1529               const struct sockaddr *address,
1530               socklen_t address_len)
1531 {
1532   struct SenderAddress *sender;
1533   struct SearchContext sc = {
1534     .address = address,
1535     .address_len = address_len,
1536     .sender = NULL
1537   };
1538
1539   GNUNET_CONTAINER_multipeermap_get_multiple (senders,
1540                                               target,
1541                                               &find_sender_by_address,
1542                                               &sc);
1543   if (NULL != sc.sender)
1544   {
1545     reschedule_sender_timeout (sc.sender);
1546     return sc.sender;
1547   }
1548   sender = GNUNET_new (struct SenderAddress);
1549   sender->target = *target;
1550   sender->address = GNUNET_memdup (address,
1551                                    address_len);
1552   sender->address_len = address_len;
1553   (void) GNUNET_CONTAINER_multipeermap_put (senders,
1554                                             &sender->target,
1555                                             sender,
1556                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1557   GNUNET_STATISTICS_set (stats,
1558                          "# senders active",
1559                          GNUNET_CONTAINER_multipeermap_size (receivers),
1560                          GNUNET_NO);
1561   sender->timeout
1562     = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1563   sender->hn = GNUNET_CONTAINER_heap_insert (senders_heap,
1564                                              sender,
1565                                              sender->timeout.abs_value_us);
1566   sender->nt = GNUNET_NT_scanner_get_type (is,
1567                                            address,
1568                                            address_len);
1569   if (NULL == timeout_task)
1570     timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts,
1571                                              NULL);
1572   return sender;
1573 }
1574
1575
1576 /**
1577  * Check signature from @a uc against @a ephemeral.
1578  *
1579  * @param ephermal key that is signed
1580  * @param uc signature of claimant
1581  * @return #GNUNET_OK if signature is valid
1582  */
1583 static int
1584 verify_confirmation (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
1585                      const struct UDPConfirmation *uc)
1586 {
1587   struct UdpHandshakeSignature uhs;
1588                         
1589   uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
1590   uhs.purpose.size = htonl (sizeof (uhs));
1591   uhs.sender = uc->sender;
1592   uhs.receiver = my_identity;
1593   uhs.ephemeral = *ephemeral;
1594   uhs.monotonic_time = uc->monotonic_time;
1595   return GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE,
1596                                      &uhs.purpose,
1597                                      &uc->sender_sig,
1598                                      &uc->sender.public_key);
1599 }
1600
1601
1602 /**
1603  * Socket read task. 
1604  *
1605  * @param cls NULL
1606  */
1607 static void
1608 sock_read (void *cls)
1609 {
1610   struct sockaddr_storage sa;
1611   socklen_t salen = sizeof (sa);
1612   char buf[UINT16_MAX];
1613   ssize_t rcvd;
1614
1615   (void) cls;
1616   read_task
1617       = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1618                                        udp_sock,
1619                                        &sock_read,
1620                                        NULL);
1621   rcvd = GNUNET_NETWORK_socket_recvfrom (udp_sock,
1622                                          buf,
1623                                          sizeof (buf),
1624                                          (struct sockaddr *) &sa,
1625                                          &salen);
1626   if (-1 == rcvd)
1627   {
1628     GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG,
1629                          "recv");
1630     return;
1631   }
1632
1633   /* first, see if it is a UDPBox */
1634   if (rcvd > sizeof (struct UDPBox))
1635   {
1636     const struct UDPBox *box;
1637     struct KeyCacheEntry *kce;
1638
1639     box = (const struct UDPBox *) buf;
1640     kce = GNUNET_CONTAINER_multishortmap_get (key_cache,
1641                                               &box->kid);
1642     if (NULL != kce)
1643     {
1644       decrypt_box (box,
1645                    (size_t) rcvd,
1646                    kce);
1647       return;
1648     }
1649   }
1650
1651   /* next, check if it is a broadcast */
1652   if (sizeof (struct UDPBroadcast) == rcvd)
1653   {
1654     const struct UDPBroadcast *ub;
1655     struct UdpBroadcastSignature uhs;
1656
1657     ub = (const struct UDPBroadcast *) buf;
1658     uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
1659     uhs.purpose.size = htonl (sizeof (uhs));
1660     uhs.sender = ub->sender;
1661     GNUNET_CRYPTO_hash (&sa,
1662                         salen,
1663                         &uhs.h_address);
1664     if (GNUNET_OK ==
1665         GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST,
1666                                     &uhs.purpose,
1667                                     &ub->sender_sig,
1668                                     &ub->sender.public_key))
1669     {
1670       GNUNET_STATISTICS_update (stats,
1671                                 "# broadcasts received",
1672                                 1,
1673                                 GNUNET_NO);
1674       // FIXME #5551: we effectively just got a HELLO!
1675       // trigger verification NOW!
1676       return;
1677     }
1678     /* continue with KX, mostly for statistics... */
1679   }
1680   
1681
1682   /* finally, test if it is a KX */
1683   if (rcvd < sizeof (struct UDPConfirmation) + sizeof (struct InitialKX))
1684   {
1685     GNUNET_STATISTICS_update (stats,
1686                               "# messages dropped (no kid, too small for KX)",
1687                               1,
1688                               GNUNET_NO);
1689     return;
1690   }
1691
1692   {
1693     const struct InitialKX *kx;
1694     struct SharedSecret *ss;
1695     char pbuf[rcvd - sizeof (struct InitialKX)];
1696     const struct UDPConfirmation *uc;
1697     struct SenderAddress *sender;
1698
1699     kx = (const struct InitialKX *) buf;
1700     ss = setup_shared_secret_dec (&kx->ephemeral);
1701     if (GNUNET_OK !=
1702         try_decrypt (ss,
1703                      kx->gcm_tag,
1704                      0,
1705                      &buf[sizeof (*kx)],
1706                      sizeof (pbuf),
1707                      pbuf))
1708     {
1709       GNUNET_free (ss);
1710       GNUNET_STATISTICS_update (stats,
1711                                 "# messages dropped (no kid, AEAD decryption failed)",
1712                                 1,
1713                                 GNUNET_NO);
1714       return;
1715     }
1716     uc = (const struct UDPConfirmation *) pbuf;
1717     if (GNUNET_OK !=
1718         verify_confirmation (&kx->ephemeral,
1719                              uc))
1720     {
1721       GNUNET_break_op (0);
1722       GNUNET_free (ss);
1723       GNUNET_STATISTICS_update (stats,
1724                                 "# messages dropped (sender signature invalid)",
1725                                 1,
1726                                 GNUNET_NO);
1727       return;
1728     }
1729     calculate_cmac (ss);
1730     sender = setup_sender (&uc->sender,
1731                            (const struct sockaddr *) &sa,
1732                            salen);
1733     ss->sender = sender;
1734     GNUNET_CONTAINER_DLL_insert (sender->ss_head,
1735                                  sender->ss_tail,
1736                                  ss);
1737     sender->num_secrets++;
1738     GNUNET_STATISTICS_update (stats,
1739                               "# Secrets active",
1740                               1,
1741                               GNUNET_NO);
1742     GNUNET_STATISTICS_update (stats,
1743                               "# messages decrypted without BOX",
1744                               1,
1745                               GNUNET_NO);
1746     try_handle_plaintext (sender,
1747                           &uc[1],
1748                           sizeof (pbuf) - sizeof (*uc));
1749     consider_ss_ack (ss);
1750     if (sender->num_secrets > MAX_SECRETS)
1751       secret_destroy (sender->ss_tail);
1752   }
1753 }
1754
1755
1756 /**
1757  * Convert UDP bind specification to a `struct sockaddr *`
1758  *
1759  * @param bindto bind specification to convert
1760  * @param[out] sock_len set to the length of the address
1761  * @return converted bindto specification
1762  */
1763 static struct sockaddr *
1764 udp_address_to_sockaddr (const char *bindto,
1765                          socklen_t *sock_len)
1766 {
1767   struct sockaddr *in;
1768   unsigned int port;
1769   char dummy[2];
1770   char *colon;
1771   char *cp;
1772   
1773   if (1 == SSCANF (bindto,
1774                    "%u%1s",
1775                    &port,
1776                    dummy))
1777   {
1778     /* interpreting value as just a PORT number */
1779     if (port > UINT16_MAX)
1780     {
1781       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1782                   "BINDTO specification `%s' invalid: value too large for port\n",
1783                   bindto);
1784       return NULL;
1785     }
1786     if ( (GNUNET_NO ==
1787           GNUNET_NETWORK_test_pf (PF_INET6)) ||
1788          (GNUNET_YES ==
1789           GNUNET_CONFIGURATION_get_value_yesno (cfg,
1790                                                 COMMUNICATOR_CONFIG_SECTION,
1791                                                 "DISABLE_V6")) )
1792     {
1793       struct sockaddr_in *i4;
1794       
1795       i4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1796       i4->sin_family = AF_INET;
1797       i4->sin_port = htons ((uint16_t) port);
1798       *sock_len = sizeof (struct sockaddr_in);
1799       in = (struct sockaddr *) i4;
1800     }
1801     else
1802     {
1803       struct sockaddr_in6 *i6;
1804       
1805       i6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1806       i6->sin6_family = AF_INET6;
1807       i6->sin6_port = htons ((uint16_t) port);
1808       *sock_len = sizeof (struct sockaddr_in6);
1809       in = (struct sockaddr *) i6;
1810     }
1811     return in;
1812   }
1813   cp = GNUNET_strdup (bindto);
1814   colon = strrchr (cp, ':');
1815   if (NULL != colon)
1816   {
1817     /* interpet value after colon as port */
1818     *colon = '\0';
1819     colon++;
1820     if (1 == SSCANF (colon,
1821                      "%u%1s",
1822                      &port,
1823                      dummy))
1824     {
1825       /* interpreting value as just a PORT number */
1826       if (port > UINT16_MAX)
1827       {
1828         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1829                     "BINDTO specification `%s' invalid: value too large for port\n",
1830                     bindto);
1831         GNUNET_free (cp);
1832         return NULL;
1833       }
1834     }
1835     else
1836     {
1837       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1838                   "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1839                   bindto);
1840       GNUNET_free (cp);
1841       return NULL;
1842     }
1843   }
1844   else
1845   {
1846     /* interpret missing port as 0, aka pick any free one */
1847     port = 0;
1848   }
1849   {
1850     /* try IPv4 */
1851     struct sockaddr_in v4;
1852
1853     if (1 == inet_pton (AF_INET,
1854                         cp,
1855                         &v4))
1856     {
1857       v4.sin_port = htons ((uint16_t) port);
1858       in = GNUNET_memdup (&v4,
1859                           sizeof (v4));
1860       *sock_len = sizeof (v4);
1861       GNUNET_free (cp);
1862       return in;
1863     }
1864   }
1865   {
1866     /* try IPv6 */
1867     struct sockaddr_in6 v6;
1868     const char *start;
1869
1870     start = cp;
1871     if ( ('[' == *cp) &&
1872          (']' == cp[strlen (cp)-1]) )
1873     {
1874       start++; /* skip over '[' */
1875       cp[strlen (cp) -1] = '\0'; /* eat ']' */
1876     }
1877     if (1 == inet_pton (AF_INET6,
1878                         start,
1879                         &v6))
1880     {
1881       v6.sin6_port = htons ((uint16_t) port);
1882       in = GNUNET_memdup (&v6,
1883                           sizeof (v6));
1884       *sock_len = sizeof (v6);
1885       GNUNET_free (cp);
1886       return in;
1887     }
1888   }
1889   /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
1890   GNUNET_free (cp);
1891   return NULL;
1892 }
1893
1894
1895 /**
1896  * Pad @a dgram by @a pad_size using @a out_cipher.
1897  *
1898  * @param out_cipher cipher to use
1899  * @param dgram datagram to pad
1900  * @param pad_size number of bytes of padding to append
1901  */
1902 static void
1903 do_pad (gcry_cipher_hd_t out_cipher,
1904         char *dgram,
1905         size_t pad_size)
1906 {
1907   char pad[pad_size];
1908
1909   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
1910                               pad,
1911                               sizeof (pad));
1912   if (sizeof (pad) > sizeof (struct GNUNET_MessageHeader))
1913   {
1914     struct GNUNET_MessageHeader hdr = {
1915       .size = htons (sizeof (pad)),
1916       .type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD)
1917     };
1918     
1919     memcpy (pad,
1920             &hdr,
1921             sizeof (hdr));
1922   }
1923   GNUNET_assert (0 ==
1924                  gcry_cipher_encrypt (out_cipher,
1925                                       dgram,
1926                                       sizeof (pad),
1927                                       pad,
1928                                       sizeof (pad)));
1929 }
1930
1931
1932 /**
1933  * Signature of functions implementing the sending functionality of a
1934  * message queue.
1935  *
1936  * @param mq the message queue
1937  * @param msg the message to send
1938  * @param impl_state our `struct ReceiverAddress`
1939  */
1940 static void
1941 mq_send (struct GNUNET_MQ_Handle *mq,
1942          const struct GNUNET_MessageHeader *msg,
1943          void *impl_state)
1944 {
1945   struct ReceiverAddress *receiver = impl_state;
1946   uint16_t msize = ntohs (msg->size);
1947
1948   GNUNET_assert (mq == receiver->mq);
1949   if (msize > receiver->mtu)
1950   {
1951     GNUNET_break (0);
1952     receiver_destroy (receiver);
1953     return;
1954   }
1955   reschedule_receiver_timeout (receiver);
1956   
1957   if (0 == receiver->acks_available)
1958   {
1959     /* use KX encryption method */
1960     struct UdpHandshakeSignature uhs;
1961     struct UDPConfirmation uc;
1962     struct InitialKX kx;
1963     struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
1964     char dgram[receiver->mtu +
1965                sizeof (uc) +
1966                sizeof (kx)];
1967     size_t dpos;
1968     gcry_cipher_hd_t out_cipher;
1969     struct SharedSecret *ss;
1970
1971     /* setup key material */
1972     GNUNET_assert (GNUNET_OK ==
1973                    GNUNET_CRYPTO_ecdhe_key_create2 (&epriv));
1974
1975     ss = setup_shared_secret_enc (&epriv,
1976                                   receiver);
1977     setup_cipher (&ss->master,
1978                   0,
1979                   &out_cipher);
1980     /* compute 'uc' */
1981     uc.sender = my_identity;
1982     uc.monotonic_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
1983     uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
1984     uhs.purpose.size = htonl (sizeof (uhs));
1985     uhs.sender = my_identity;
1986     uhs.receiver = receiver->target;
1987     GNUNET_CRYPTO_ecdhe_key_get_public (&epriv,
1988                                         &uhs.ephemeral);
1989     uhs.monotonic_time = uc.monotonic_time;
1990     GNUNET_assert (GNUNET_OK ==
1991                    GNUNET_CRYPTO_eddsa_sign (my_private_key,
1992                                              &uhs.purpose,
1993                                              &uc.sender_sig));
1994     /* Leave space for kx */
1995     dpos = sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
1996     /* Append encrypted uc to dgram */
1997     GNUNET_assert (0 ==
1998                    gcry_cipher_encrypt (out_cipher,
1999                                         &dgram[dpos],
2000                                         sizeof (uc),
2001                                         &uc,
2002                                         sizeof (uc)));
2003     dpos += sizeof (uc);
2004     /* Append encrypted payload to dgram */
2005     GNUNET_assert (0 ==
2006                    gcry_cipher_encrypt (out_cipher,
2007                                         &dgram[dpos],
2008                                         msize,
2009                                         msg,
2010                                         msize));
2011     dpos += msize;
2012     do_pad (out_cipher,
2013             &dgram[dpos],
2014             sizeof (dgram) - dpos);
2015     /* Datagram starts with kx */
2016     kx.ephemeral = uhs.ephemeral;
2017     GNUNET_assert (0 ==
2018                    gcry_cipher_gettag (out_cipher,
2019                                        kx.gcm_tag,
2020                                        sizeof (kx.gcm_tag)));
2021     gcry_cipher_close (out_cipher);
2022     memcpy (dgram,
2023             &kx,
2024             sizeof (kx));
2025     if (-1 ==
2026         GNUNET_NETWORK_socket_sendto (udp_sock,
2027                                       dgram,
2028                                       sizeof (dgram),
2029                                       receiver->address,
2030                                       receiver->address_len))
2031       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2032                            "send");
2033     GNUNET_MQ_impl_send_continue (mq);
2034     return;
2035   } /* End of KX encryption method */
2036
2037   /* begin "BOX" encryption method, scan for ACKs from tail! */
2038   for (struct SharedSecret *ss = receiver->ss_tail;
2039        NULL != ss;
2040        ss = ss->prev)
2041   {
2042     if (ss->sequence_used < ss->sequence_allowed)
2043     {
2044       char dgram[sizeof (struct UDPBox) + receiver->mtu];
2045       struct UDPBox *box;
2046       gcry_cipher_hd_t out_cipher;
2047       size_t dpos;
2048
2049       box = (struct UDPBox *) dgram;
2050       ss->sequence_used++;
2051       get_kid (&ss->master,
2052                ss->sequence_used,
2053                &box->kid);
2054       setup_cipher (&ss->master,
2055                     ss->sequence_used,
2056                     &out_cipher);
2057       /* Append encrypted payload to dgram */
2058       dpos = sizeof (struct UDPBox);
2059       GNUNET_assert (0 ==
2060                      gcry_cipher_encrypt (out_cipher,
2061                                           &dgram[dpos],
2062                                           msize,
2063                                           msg,
2064                                           msize));
2065       dpos += msize;
2066       do_pad (out_cipher,
2067               &dgram[dpos],
2068               sizeof (dgram) - dpos);
2069       GNUNET_assert (0 ==
2070                      gcry_cipher_gettag (out_cipher,
2071                                          box->gcm_tag,
2072                                          sizeof (box->gcm_tag)));
2073       gcry_cipher_close (out_cipher);
2074       if (-1 ==
2075           GNUNET_NETWORK_socket_sendto (udp_sock,
2076                                         dgram,
2077                                         sizeof (dgram),
2078                                         receiver->address,
2079                                         receiver->address_len))
2080         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2081                              "send");
2082       GNUNET_MQ_impl_send_continue (mq);
2083       receiver->acks_available--;
2084       if (0 == receiver->acks_available)
2085       {
2086         /* We have no more ACKs => MTU change! */
2087         setup_receiver_mq (receiver);
2088       }
2089       return;
2090     }
2091   }
2092   GNUNET_assert (0);
2093 }
2094
2095
2096 /**
2097  * Signature of functions implementing the destruction of a message
2098  * queue.  Implementations must not free @a mq, but should take care
2099  * of @a impl_state.
2100  *
2101  * @param mq the message queue to destroy
2102  * @param impl_state our `struct ReceiverAddress`
2103  */
2104 static void
2105 mq_destroy (struct GNUNET_MQ_Handle *mq,
2106             void *impl_state)
2107 {
2108   struct ReceiverAddress *receiver = impl_state;
2109
2110   if (mq == receiver->mq)
2111   {
2112     receiver->mq = NULL;
2113     receiver_destroy (receiver);
2114   }
2115 }
2116
2117
2118 /**
2119  * Implementation function that cancels the currently sent message.
2120  *
2121  * @param mq message queue
2122  * @param impl_state our `struct RecvierAddress`
2123  */
2124 static void
2125 mq_cancel (struct GNUNET_MQ_Handle *mq,
2126            void *impl_state)
2127 {
2128   /* Cancellation is impossible with UDP; bail */
2129   GNUNET_assert (0);
2130 }
2131
2132
2133 /**
2134  * Generic error handler, called with the appropriate
2135  * error code and the same closure specified at the creation of
2136  * the message queue.
2137  * Not every message queue implementation supports an error handler.
2138  *
2139  * @param cls our `struct ReceiverAddress`
2140  * @param error error code
2141  */
2142 static void
2143 mq_error (void *cls,
2144           enum GNUNET_MQ_Error error)
2145 {
2146   struct ReceiverAddress *receiver = cls;
2147
2148   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2149               "MQ error in queue to %s: %d\n",
2150               GNUNET_i2s (&receiver->target),
2151               (int) error);
2152   receiver_destroy (receiver);
2153 }
2154
2155
2156 /**
2157  * Setup the MQ for the @a receiver.  If a queue exists,
2158  * the existing one is destroyed.  Then the MTU is
2159  * recalculated and a fresh queue is initialized.
2160  *
2161  * @param receiver receiver to setup MQ for
2162  */ 
2163 static void
2164 setup_receiver_mq (struct ReceiverAddress *receiver)
2165 {
2166   size_t base_mtu;
2167   
2168   if (NULL != receiver->qh)
2169   {
2170     GNUNET_TRANSPORT_communicator_mq_del (receiver->qh);
2171     receiver->qh = NULL;
2172   }
2173   GNUNET_assert (NULL == receiver->mq);
2174   switch (receiver->address->sa_family)
2175   {
2176   case AF_INET:
2177     base_mtu
2178       = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */
2179       - sizeof (struct GNUNET_TUN_IPv4Header) /* 20 */
2180       - sizeof (struct GNUNET_TUN_UdpHeader) /* 8 */;
2181     break;
2182   case AF_INET6:
2183     base_mtu
2184       = 1280 /* Minimum MTU required by IPv6 */
2185       - sizeof (struct GNUNET_TUN_IPv6Header) /* 40 */
2186       - sizeof (struct GNUNET_TUN_UdpHeader) /* 8 */;
2187     break;
2188   default:
2189     GNUNET_assert (0);
2190     break;
2191   }
2192   if (0 == receiver->acks_available)
2193   {
2194     /* MTU based on full KX messages */
2195     receiver->mtu
2196       = base_mtu
2197       - sizeof (struct InitialKX) /* 48 */
2198       - sizeof (struct UDPConfirmation); /* 104 */
2199   }
2200   else
2201   {
2202     /* MTU based on BOXed messages */
2203     receiver->mtu
2204       = base_mtu - sizeof (struct UDPBox);
2205   }
2206   /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to
2207      1404 (IPv4 + Box) bytes, depending on circumstances... */
2208   receiver->mq
2209     = GNUNET_MQ_queue_for_callbacks (&mq_send,
2210                                      &mq_destroy,
2211                                      &mq_cancel,
2212                                      receiver,
2213                                      NULL,
2214                                      &mq_error,
2215                                      receiver);
2216   receiver->qh
2217     = GNUNET_TRANSPORT_communicator_mq_add (ch,
2218                                             &receiver->target,
2219                                             receiver->foreign_addr,
2220                                             receiver->mtu,
2221                                             receiver->nt,
2222                                             GNUNET_TRANSPORT_CS_OUTBOUND,
2223                                             receiver->mq);
2224 }
2225
2226
2227 /**
2228  * Setup a receiver for transmission.  Setup the MQ processing and
2229  * inform transport that the queue is ready. 
2230  *
2231  * @param 
2232  */ 
2233 static struct ReceiverAddress *
2234 receiver_setup (const struct GNUNET_PeerIdentity *target,
2235                 const struct sockaddr *address,
2236                 socklen_t address_len)
2237 {
2238   struct ReceiverAddress *receiver;
2239
2240   receiver = GNUNET_new (struct ReceiverAddress);
2241   receiver->address = GNUNET_memdup (address,
2242                                      address_len);
2243   receiver->address_len = address_len;
2244   receiver->target = *target;
2245   receiver->nt = GNUNET_NT_scanner_get_type (is,
2246                                              address,
2247                                              address_len);
2248   (void) GNUNET_CONTAINER_multipeermap_put (receivers,
2249                                             &receiver->target,
2250                                             receiver,
2251                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2252   receiver->timeout
2253     = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2254   receiver->hn = GNUNET_CONTAINER_heap_insert (receivers_heap,
2255                                                receiver,
2256                                                receiver->timeout.abs_value_us);
2257   GNUNET_STATISTICS_set (stats,
2258                          "# receivers active",
2259                          GNUNET_CONTAINER_multipeermap_size (receivers),
2260                          GNUNET_NO);
2261   switch (address->sa_family)
2262   {
2263   case AF_INET:
2264     GNUNET_asprintf (&receiver->foreign_addr,
2265                      "%s-%s",
2266                      COMMUNICATOR_ADDRESS_PREFIX,
2267                      GNUNET_a2s (receiver->address,
2268                                  receiver->address_len));
2269     break;
2270   case AF_INET6:
2271     GNUNET_asprintf (&receiver->foreign_addr,
2272                      "%s-%s",
2273                      COMMUNICATOR_ADDRESS_PREFIX,
2274                      GNUNET_a2s (receiver->address,
2275                                  receiver->address_len));
2276     break;
2277   default:
2278     GNUNET_assert (0);
2279   }
2280
2281   setup_receiver_mq (receiver);
2282
2283   if (NULL == timeout_task)
2284     timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts,
2285                                              NULL);
2286   return receiver;
2287 }
2288
2289
2290 /**
2291  * Function called by the transport service to initialize a
2292  * message queue given address information about another peer.
2293  * If and when the communication channel is established, the
2294  * communicator must call #GNUNET_TRANSPORT_communicator_mq_add()
2295  * to notify the service that the channel is now up.  It is
2296  * the responsibility of the communicator to manage sane
2297  * retries and timeouts for any @a peer/@a address combination
2298  * provided by the transport service.  Timeouts and retries
2299  * do not need to be signalled to the transport service.
2300  *
2301  * @param cls closure
2302  * @param peer identity of the other peer
2303  * @param address where to send the message, human-readable
2304  *        communicator-specific format, 0-terminated, UTF-8
2305  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is invalid
2306  */
2307 static int
2308 mq_init (void *cls,
2309          const struct GNUNET_PeerIdentity *peer,
2310          const char *address)
2311 {
2312   struct ReceiverAddress *receiver;
2313   const char *path;
2314   struct sockaddr *in;
2315   socklen_t in_len;
2316   
2317   if (0 != strncmp (address,
2318                     COMMUNICATOR_ADDRESS_PREFIX "-",
2319                     strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
2320   {
2321     GNUNET_break_op (0);
2322     return GNUNET_SYSERR;
2323   }
2324   path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
2325   in = udp_address_to_sockaddr (path,
2326                                 &in_len);  
2327   receiver = receiver_setup (peer,
2328                              in,
2329                              in_len);
2330   (void) receiver;
2331   return GNUNET_OK;  
2332 }
2333
2334
2335 /**
2336  * Iterator over all receivers to clean up.
2337  *
2338  * @param cls NULL
2339  * @param target unused
2340  * @param value the queue to destroy
2341  * @return #GNUNET_OK to continue to iterate
2342  */
2343 static int
2344 get_receiver_delete_it (void *cls,
2345                         const struct GNUNET_PeerIdentity *target,
2346                         void *value)
2347 {
2348   struct ReceiverAddress *receiver = value;
2349
2350   (void) cls;
2351   (void) target;
2352   receiver_destroy (receiver);
2353   return GNUNET_OK;
2354 }
2355
2356
2357 /**
2358  * Iterator over all senders to clean up.
2359  *
2360  * @param cls NULL
2361  * @param target unused
2362  * @param value the queue to destroy
2363  * @return #GNUNET_OK to continue to iterate
2364  */
2365 static int
2366 get_sender_delete_it (void *cls,
2367                       const struct GNUNET_PeerIdentity *target,
2368                       void *value)
2369 {
2370   struct SenderAddress *sender = value;
2371
2372   (void) cls;
2373   (void) target;
2374   sender_destroy (sender);
2375   return GNUNET_OK;
2376 }
2377
2378
2379 /**
2380  * Shutdown the UNIX communicator.
2381  *
2382  * @param cls NULL (always)
2383  */
2384 static void
2385 do_shutdown (void *cls)
2386 {
2387   if (NULL != nat)
2388   {
2389      GNUNET_NAT_unregister (nat);
2390      nat = NULL;
2391   }
2392   while (NULL != bi_head)
2393     bi_destroy (bi_head);
2394   if (NULL != broadcast_task)
2395   {
2396     GNUNET_SCHEDULER_cancel (broadcast_task);
2397     broadcast_task = NULL;
2398   }
2399   if (NULL != read_task)
2400   {
2401     GNUNET_SCHEDULER_cancel (read_task);
2402     read_task = NULL;
2403   }
2404   if (NULL != udp_sock)
2405   {
2406     GNUNET_break (GNUNET_OK ==
2407                   GNUNET_NETWORK_socket_close (udp_sock));
2408     udp_sock = NULL;
2409   }
2410   GNUNET_CONTAINER_multipeermap_iterate (receivers,
2411                                          &get_receiver_delete_it,
2412                                          NULL);
2413   GNUNET_CONTAINER_multipeermap_destroy (receivers);
2414   GNUNET_CONTAINER_multipeermap_iterate (senders,
2415                                          &get_sender_delete_it,
2416                                          NULL);
2417   GNUNET_CONTAINER_multipeermap_destroy (senders);
2418   GNUNET_CONTAINER_multishortmap_destroy (key_cache);
2419   GNUNET_CONTAINER_heap_destroy (senders_heap);
2420   GNUNET_CONTAINER_heap_destroy (receivers_heap);
2421   if (NULL != ch)
2422   {
2423     GNUNET_TRANSPORT_communicator_disconnect (ch);
2424     ch = NULL;
2425   }
2426   if (NULL != stats)
2427   {
2428     GNUNET_STATISTICS_destroy (stats,
2429                                GNUNET_NO);
2430     stats = NULL;
2431   }
2432   if (NULL != my_private_key)
2433   {
2434     GNUNET_free (my_private_key);
2435     my_private_key = NULL;
2436   }
2437   if (NULL != is)
2438   {
2439      GNUNET_NT_scanner_done (is);
2440      is = NULL;
2441   }
2442 }
2443
2444
2445 /**
2446  * Function called when the transport service has received a
2447  * backchannel message for this communicator (!) via a different return
2448  * path. Should be an acknowledgement.
2449  *
2450  * @param cls closure, NULL
2451  * @param sender which peer sent the notification
2452  * @param msg payload
2453  */
2454 static void
2455 enc_notify_cb (void *cls,
2456                const struct GNUNET_PeerIdentity *sender,
2457                const struct GNUNET_MessageHeader *msg)
2458 {
2459   const struct UDPAck *ack;
2460   
2461   (void) cls;
2462   if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) ||
2463        (ntohs (msg->size) != sizeof (struct UDPAck)) )
2464   {
2465     GNUNET_break_op (0);
2466     return;
2467   }
2468   ack = (const struct UDPAck *) msg;
2469   GNUNET_CONTAINER_multipeermap_get_multiple (receivers,
2470                                               sender,
2471                                               &handle_ack,
2472                                               (void *) ack);
2473 }
2474
2475
2476 /**
2477  * Signature of the callback passed to #GNUNET_NAT_register() for
2478  * a function to call whenever our set of 'valid' addresses changes.
2479  *
2480  * @param cls closure
2481  * @param app_ctx[in,out] location where the app can store stuff
2482  *                  on add and retrieve it on remove
2483  * @param add_remove #GNUNET_YES to add a new public IP address, 
2484  *                   #GNUNET_NO to remove a previous (now invalid) one
2485  * @param ac address class the address belongs to
2486  * @param addr either the previous or the new public IP address
2487  * @param addrlen actual length of the @a addr
2488  */
2489 static void
2490 nat_address_cb (void *cls,
2491                 void **app_ctx,
2492                 int add_remove,
2493                 enum GNUNET_NAT_AddressClass ac,
2494                 const struct sockaddr *addr,
2495                 socklen_t addrlen)
2496 {
2497   char *my_addr;
2498   struct GNUNET_TRANSPORT_AddressIdentifier *ai;
2499
2500   if (GNUNET_YES == add_remove)
2501   {
2502     enum GNUNET_NetworkType nt;
2503
2504     GNUNET_asprintf (&my_addr,
2505                      "%s-%s",
2506                      COMMUNICATOR_ADDRESS_PREFIX,
2507                      GNUNET_a2s (addr,
2508                                  addrlen));
2509     nt = GNUNET_NT_scanner_get_type (is,
2510                                      addr,
2511                                      addrlen); 
2512     ai = GNUNET_TRANSPORT_communicator_address_add (ch,
2513                                                     my_addr,
2514                                                     nt,
2515                                                     GNUNET_TIME_UNIT_FOREVER_REL);
2516     GNUNET_free (my_addr);
2517     *app_ctx = ai;
2518   }
2519   else
2520   {
2521     ai = *app_ctx;
2522     GNUNET_TRANSPORT_communicator_address_remove (ai);
2523     *app_ctx = NULL;
2524   }
2525 }
2526
2527
2528 /**
2529  * Broadcast our presence on one of our interfaces.
2530  *
2531  * @param cls a `struct BroadcastInterface`
2532  */
2533 static void
2534 ifc_broadcast (void *cls)
2535 {
2536   struct BroadcastInterface *bi = cls;
2537   struct GNUNET_TIME_Relative delay;
2538
2539   delay = BROADCAST_FREQUENCY;
2540   delay.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2541                                                  delay.rel_value_us);
2542   bi->broadcast_task
2543     = GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY,
2544                                     &ifc_broadcast,
2545                                     bi);
2546   
2547   switch (bi->sa->sa_family) {
2548   case AF_INET:
2549     {
2550       static int yes = 1;
2551       static int no = 0;
2552       ssize_t sent;
2553     
2554       if (GNUNET_OK !=
2555           GNUNET_NETWORK_socket_setsockopt (udp_sock,
2556                                             SOL_SOCKET,
2557                                             SO_BROADCAST,
2558                                             &yes,
2559                                             sizeof (int)))
2560         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2561                              "setsockopt");
2562       sent = GNUNET_NETWORK_socket_sendto (udp_sock,
2563                                            &bi->bcm,
2564                                            sizeof (bi->bcm),
2565                                            bi->ba,
2566                                            bi->salen);
2567       if (-1 == sent)
2568         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2569                              "sendto");
2570       if (GNUNET_OK !=
2571           GNUNET_NETWORK_socket_setsockopt (udp_sock,
2572                                             SOL_SOCKET,
2573                                             SO_BROADCAST,
2574                                             &no,
2575                                             sizeof (int)))
2576         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2577                              "setsockopt");
2578       break;
2579     }
2580   case AF_INET6:
2581     {
2582       ssize_t sent;
2583       struct sockaddr_in6 dst;
2584
2585       dst.sin6_family = AF_INET6;
2586       dst.sin6_port = htons (my_port);
2587       dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr;
2588       dst.sin6_scope_id = ((struct sockaddr_in6*) bi->ba)->sin6_scope_id;
2589
2590       sent = GNUNET_NETWORK_socket_sendto (udp_sock,
2591                                            &bi->bcm,
2592                                            sizeof (bi->bcm),
2593                                            (const struct sockaddr *)
2594                                            &dst,
2595                                            sizeof (dst));
2596       if (-1 == sent)
2597         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2598                              "sendto");
2599       break;
2600     }
2601   default:
2602     GNUNET_break (0);
2603     break;
2604   }
2605 }
2606
2607
2608 /**
2609  * Callback function invoked for each interface found.
2610  * Activates/deactivates broadcast interfaces.
2611  *
2612  * @param cls NULL
2613  * @param name name of the interface (can be NULL for unknown)
2614  * @param isDefault is this presumably the default interface
2615  * @param addr address of this interface (can be NULL for unknown or unassigned)
2616  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
2617  * @param netmask the network mask (can be NULL for unknown or unassigned)
2618  * @param addrlen length of the address
2619  * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
2620  */
2621 static int
2622 iface_proc (void *cls,
2623             const char *name,
2624             int isDefault,
2625             const struct sockaddr *addr,
2626             const struct sockaddr *broadcast_addr,
2627             const struct sockaddr *netmask, socklen_t addrlen)
2628 {
2629   struct BroadcastInterface *bi;
2630   enum GNUNET_NetworkType network;
2631   struct UdpBroadcastSignature ubs;
2632
2633   (void) cls;
2634   (void) netmask;
2635   network = GNUNET_NT_scanner_get_type (is,
2636                                         addr,
2637                                         addrlen);
2638   if (GNUNET_NT_LOOPBACK == network)
2639   {
2640     /* Broadcasting on loopback does not make sense */
2641     return GNUNET_YES;
2642   }
2643   if (NULL == addr)
2644     return GNUNET_YES; /* need to know our address! */
2645   for (bi = bi_head; NULL != bi; bi = bi->next)
2646   {
2647     if ( (bi->salen == addrlen) &&
2648          (0 == memcmp (addr,
2649                        bi->sa,
2650                        addrlen)) )
2651     {
2652       bi->found = GNUNET_YES;
2653       return GNUNET_OK;
2654     }
2655   }
2656
2657   if ( (AF_INET6 == addr->sa_family) &&
2658        (NULL == broadcast_addr) )
2659     return GNUNET_OK; /* broadcast_addr is required for IPv6! */
2660   if ( (AF_INET6 == addr->sa_family) &&
2661        (GNUNET_YES != have_v6_socket) )
2662     return GNUNET_OK; /* not using IPv6 */
2663   
2664   bi = GNUNET_new (struct BroadcastInterface);
2665   bi->sa = GNUNET_memdup (addr,
2666                           addrlen);
2667   if (NULL != broadcast_addr)
2668     bi->ba = GNUNET_memdup (broadcast_addr,
2669                             addrlen);
2670   bi->salen = addrlen;
2671   bi->found = GNUNET_YES;
2672   bi->bcm.sender = my_identity;
2673   ubs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
2674   ubs.purpose.size = htonl (sizeof (ubs));
2675   ubs.sender = my_identity;
2676   GNUNET_CRYPTO_hash (addr,
2677                       addrlen,
2678                       &ubs.h_address);
2679   GNUNET_assert (GNUNET_OK ==
2680                  GNUNET_CRYPTO_eddsa_sign (my_private_key,
2681                                            &ubs.purpose,
2682                                            &bi->bcm.sender_sig));
2683   bi->broadcast_task = GNUNET_SCHEDULER_add_now (&ifc_broadcast,
2684                                                  bi);
2685   GNUNET_CONTAINER_DLL_insert (bi_head,
2686                                bi_tail,
2687                                bi);
2688   if ( (AF_INET6 == addr->sa_family) &&
2689        (NULL != broadcast_addr) )
2690   {
2691     /* Create IPv6 multicast request */
2692     const struct sockaddr_in6 *s6
2693       = (const struct sockaddr_in6 *) broadcast_addr;
2694
2695     GNUNET_assert (1 ==
2696                    inet_pton (AF_INET6,
2697                               "FF05::13B",
2698                               &bi->mcreq.ipv6mr_multiaddr));
2699     
2700     /* http://tools.ietf.org/html/rfc2553#section-5.2:
2701      *
2702      * IPV6_JOIN_GROUP
2703      *
2704      * Join a multicast group on a specified local interface.  If the
2705      * interface index is specified as 0, the kernel chooses the local
2706      * interface.  For example, some kernels look up the multicast
2707      * group in the normal IPv6 routing table and using the resulting
2708      * interface; we do this for each interface, so no need to use
2709      * zero (anymore...).
2710      */
2711     bi->mcreq.ipv6mr_interface = s6->sin6_scope_id;
2712
2713     /* Join the multicast group */
2714     if (GNUNET_OK !=
2715         GNUNET_NETWORK_socket_setsockopt
2716         (udp_sock,
2717          IPPROTO_IPV6,
2718          IPV6_JOIN_GROUP,
2719          &bi->mcreq,
2720          sizeof (bi->mcreq)))
2721     {
2722       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2723                            "setsockopt");
2724     }
2725   }
2726   return GNUNET_OK;
2727 }
2728
2729
2730 /**
2731  * Scan interfaces to broadcast our presence on the LAN.
2732  *
2733  * @param cls NULL, unused
2734  */
2735 static void
2736 do_broadcast (void *cls)
2737 {
2738   struct BroadcastInterface *bin;
2739   
2740   (void) cls;
2741   for (struct BroadcastInterface *bi = bi_head;
2742        NULL != bi;
2743        bi = bi->next)
2744     bi->found = GNUNET_NO;
2745   GNUNET_OS_network_interfaces_list (&iface_proc,
2746                                      NULL);
2747   for (struct BroadcastInterface *bi = bi_head;
2748        NULL != bi;
2749        bi = bin)
2750   {
2751     bin = bi->next;
2752     if (GNUNET_NO == bi->found)
2753       bi_destroy (bi);
2754   }
2755   broadcast_task
2756     = GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY,
2757                                     &do_broadcast,
2758                                     NULL);
2759 }
2760
2761
2762 /**
2763  * Setup communicator and launch network interactions.
2764  *
2765  * @param cls NULL (always)
2766  * @param args remaining command-line arguments
2767  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2768  * @param c configuration
2769  */
2770 static void
2771 run (void *cls,
2772      char *const *args,
2773      const char *cfgfile,
2774      const struct GNUNET_CONFIGURATION_Handle *c)
2775 {
2776   char *bindto;
2777   struct sockaddr *in;
2778   socklen_t in_len;
2779   struct sockaddr_storage in_sto;
2780   socklen_t sto_len;
2781   
2782   (void) cls;
2783   cfg = c;
2784   if (GNUNET_OK !=
2785       GNUNET_CONFIGURATION_get_value_filename (cfg,
2786                                                COMMUNICATOR_CONFIG_SECTION,
2787                                                "BINDTO",
2788                                                &bindto))
2789   {
2790     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2791                                COMMUNICATOR_CONFIG_SECTION,
2792                                "BINDTO");
2793     return;
2794   }
2795
2796   in = udp_address_to_sockaddr (bindto,
2797                                 &in_len);
2798   if (NULL == in)
2799   {
2800     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2801                 "Failed to setup UDP socket address with path `%s'\n",
2802                 bindto);
2803     GNUNET_free (bindto);
2804     return;
2805   }
2806   udp_sock = GNUNET_NETWORK_socket_create (in->sa_family,
2807                                            SOCK_DGRAM,
2808                                            IPPROTO_UDP);
2809   if (NULL == udp_sock)
2810   {
2811     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
2812                          "socket");
2813     GNUNET_free (in);
2814     GNUNET_free (bindto);
2815     return;
2816   }
2817   if (AF_INET6 == in->sa_family)
2818     have_v6_socket = GNUNET_YES;
2819   if (GNUNET_OK !=
2820       GNUNET_NETWORK_socket_bind (udp_sock,
2821                                   in,
2822                                   in_len))
2823   {
2824     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2825                               "bind",
2826                               bindto);
2827     GNUNET_NETWORK_socket_close (udp_sock);
2828     udp_sock = NULL;
2829     GNUNET_free (in);
2830     GNUNET_free (bindto);
2831     return;
2832   }
2833   /* We might have bound to port 0, allowing the OS to figure it out;
2834      thus, get the real IN-address from the socket */
2835   sto_len = sizeof (in_sto);
2836   if (0 != getsockname (GNUNET_NETWORK_get_fd (udp_sock),
2837                         (struct sockaddr *) &in_sto,
2838                         &sto_len))
2839   {
2840     memcpy (&in_sto,
2841             in,
2842             in_len);
2843     sto_len = in_len;
2844   }
2845   GNUNET_free (in);
2846   GNUNET_free (bindto);
2847   in = (struct sockaddr *) &in_sto;
2848   in_len = sto_len;
2849   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2850               "Bound to `%s'\n",
2851               GNUNET_a2s ((const struct sockaddr *) &in_sto,
2852                           sto_len));
2853   switch (in->sa_family)
2854   {
2855   case AF_INET:
2856     my_port = ntohs (((struct sockaddr_in *) in)->sin_port);
2857     break;
2858   case AF_INET6:
2859     my_port = ntohs (((struct sockaddr_in6 *) in)->sin6_port);
2860     break;
2861   default:
2862     GNUNET_break (0);
2863     my_port = 0;
2864   }
2865   stats = GNUNET_STATISTICS_create ("C-UDP",
2866                                     cfg);
2867   senders = GNUNET_CONTAINER_multipeermap_create (32,
2868                                                   GNUNET_YES);
2869   receivers = GNUNET_CONTAINER_multipeermap_create (32,
2870                                                     GNUNET_YES);
2871   senders_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2872   receivers_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2873   key_cache = GNUNET_CONTAINER_multishortmap_create (1024,
2874                                                      GNUNET_YES);
2875   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
2876                                  NULL);
2877   is = GNUNET_NT_scanner_init ();
2878   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
2879   if (NULL == my_private_key)
2880   {
2881     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2882                 _("Transport service is lacking key configuration settings. Exiting.\n"));
2883     GNUNET_SCHEDULER_shutdown ();
2884     return;
2885   }
2886   GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
2887                                       &my_identity.public_key);
2888   /* start reading */
2889   read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2890                                              udp_sock,
2891                                              &sock_read,
2892                                              NULL);
2893   ch = GNUNET_TRANSPORT_communicator_connect (cfg,
2894                                               COMMUNICATOR_CONFIG_SECTION,
2895                                               COMMUNICATOR_ADDRESS_PREFIX,
2896                                               GNUNET_TRANSPORT_CC_UNRELIABLE,
2897                                               &mq_init,
2898                                               NULL,
2899                                               &enc_notify_cb,
2900                                               NULL);
2901   if (NULL == ch)
2902   {
2903     GNUNET_break (0);
2904     GNUNET_SCHEDULER_shutdown ();
2905     return;
2906   }
2907   /* start broadcasting */
2908   if (GNUNET_YES !=
2909       GNUNET_CONFIGURATION_get_value_yesno (cfg,
2910                                             COMMUNICATOR_CONFIG_SECTION,
2911                                             "DISABLE_BROADCAST"))
2912   {
2913     broadcast_task = GNUNET_SCHEDULER_add_now (&do_broadcast,
2914                                                NULL);
2915   }  
2916   nat = GNUNET_NAT_register (cfg,
2917                              COMMUNICATOR_CONFIG_SECTION,
2918                              IPPROTO_UDP,
2919                              1 /* one address */,
2920                              (const struct sockaddr **) &in,
2921                              &in_len,
2922                              &nat_address_cb,
2923                              NULL /* FIXME: support reversal: #5529 */,
2924                              NULL /* closure */);
2925 }
2926
2927
2928 /**
2929  * The main function for the UNIX communicator.
2930  *
2931  * @param argc number of arguments from the command line
2932  * @param argv command line arguments
2933  * @return 0 ok, 1 on error
2934  */
2935 int
2936 main (int argc,
2937       char *const *argv)
2938 {
2939   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2940     GNUNET_GETOPT_OPTION_END
2941   };
2942   int ret;
2943
2944   if (GNUNET_OK !=
2945       GNUNET_STRINGS_get_utf8_args (argc, argv,
2946                                     &argc, &argv))
2947     return 2;
2948
2949   ret =
2950       (GNUNET_OK ==
2951        GNUNET_PROGRAM_run (argc, argv,
2952                            "gnunet-communicator-udp",
2953                            _("GNUnet UDP communicator"),
2954                            options,
2955                            &run,
2956                            NULL)) ? 0 : 1;
2957   GNUNET_free ((void*) argv);
2958   return ret;
2959 }
2960
2961
2962 /* end of gnunet-communicator-udp.c */