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